recog_memoized works on an rtx_insn *
[official-gcc.git] / gcc / reload.c
bloba9048ce97286b911038d502172faf7d49379a60b
1 /* Search an insn for pseudo regs that must be in hard regs and are not.
2 Copyright (C) 1987-2014 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 "expr.h"
99 #include "optabs.h"
100 #include "recog.h"
101 #include "df.h"
102 #include "reload.h"
103 #include "regs.h"
104 #include "addresses.h"
105 #include "hard-reg-set.h"
106 #include "flags.h"
107 #include "function.h"
108 #include "params.h"
109 #include "target.h"
110 #include "ira.h"
112 /* True if X is a constant that can be forced into the constant pool.
113 MODE is the mode of the operand, or VOIDmode if not known. */
114 #define CONST_POOL_OK_P(MODE, X) \
115 ((MODE) != VOIDmode \
116 && CONSTANT_P (X) \
117 && GET_CODE (X) != HIGH \
118 && !targetm.cannot_force_const_mem (MODE, X))
120 /* True if C is a non-empty register class that has too few registers
121 to be safely used as a reload target class. */
123 static inline bool
124 small_register_class_p (reg_class_t rclass)
126 return (reg_class_size [(int) rclass] == 1
127 || (reg_class_size [(int) rclass] >= 1
128 && targetm.class_likely_spilled_p (rclass)));
132 /* All reloads of the current insn are recorded here. See reload.h for
133 comments. */
134 int n_reloads;
135 struct reload rld[MAX_RELOADS];
137 /* All the "earlyclobber" operands of the current insn
138 are recorded here. */
139 int n_earlyclobbers;
140 rtx reload_earlyclobbers[MAX_RECOG_OPERANDS];
142 int reload_n_operands;
144 /* Replacing reloads.
146 If `replace_reloads' is nonzero, then as each reload is recorded
147 an entry is made for it in the table `replacements'.
148 Then later `subst_reloads' can look through that table and
149 perform all the replacements needed. */
151 /* Nonzero means record the places to replace. */
152 static int replace_reloads;
154 /* Each replacement is recorded with a structure like this. */
155 struct replacement
157 rtx *where; /* Location to store in */
158 int what; /* which reload this is for */
159 enum machine_mode mode; /* mode it must have */
162 static struct replacement replacements[MAX_RECOG_OPERANDS * ((MAX_REGS_PER_ADDRESS * 2) + 1)];
164 /* Number of replacements currently recorded. */
165 static int n_replacements;
167 /* Used to track what is modified by an operand. */
168 struct decomposition
170 int reg_flag; /* Nonzero if referencing a register. */
171 int safe; /* Nonzero if this can't conflict with anything. */
172 rtx base; /* Base address for MEM. */
173 HOST_WIDE_INT start; /* Starting offset or register number. */
174 HOST_WIDE_INT end; /* Ending offset or register number. */
177 #ifdef SECONDARY_MEMORY_NEEDED
179 /* Save MEMs needed to copy from one class of registers to another. One MEM
180 is used per mode, but normally only one or two modes are ever used.
182 We keep two versions, before and after register elimination. The one
183 after register elimination is record separately for each operand. This
184 is done in case the address is not valid to be sure that we separately
185 reload each. */
187 static rtx secondary_memlocs[NUM_MACHINE_MODES];
188 static rtx secondary_memlocs_elim[NUM_MACHINE_MODES][MAX_RECOG_OPERANDS];
189 static int secondary_memlocs_elim_used = 0;
190 #endif
192 /* The instruction we are doing reloads for;
193 so we can test whether a register dies in it. */
194 static rtx_insn *this_insn;
196 /* Nonzero if this instruction is a user-specified asm with operands. */
197 static int this_insn_is_asm;
199 /* If hard_regs_live_known is nonzero,
200 we can tell which hard regs are currently live,
201 at least enough to succeed in choosing dummy reloads. */
202 static int hard_regs_live_known;
204 /* Indexed by hard reg number,
205 element is nonnegative if hard reg has been spilled.
206 This vector is passed to `find_reloads' as an argument
207 and is not changed here. */
208 static short *static_reload_reg_p;
210 /* Set to 1 in subst_reg_equivs if it changes anything. */
211 static int subst_reg_equivs_changed;
213 /* On return from push_reload, holds the reload-number for the OUT
214 operand, which can be different for that from the input operand. */
215 static int output_reloadnum;
217 /* Compare two RTX's. */
218 #define MATCHES(x, y) \
219 (x == y || (x != 0 && (REG_P (x) \
220 ? REG_P (y) && REGNO (x) == REGNO (y) \
221 : rtx_equal_p (x, y) && ! side_effects_p (x))))
223 /* Indicates if two reloads purposes are for similar enough things that we
224 can merge their reloads. */
225 #define MERGABLE_RELOADS(when1, when2, op1, op2) \
226 ((when1) == RELOAD_OTHER || (when2) == RELOAD_OTHER \
227 || ((when1) == (when2) && (op1) == (op2)) \
228 || ((when1) == RELOAD_FOR_INPUT && (when2) == RELOAD_FOR_INPUT) \
229 || ((when1) == RELOAD_FOR_OPERAND_ADDRESS \
230 && (when2) == RELOAD_FOR_OPERAND_ADDRESS) \
231 || ((when1) == RELOAD_FOR_OTHER_ADDRESS \
232 && (when2) == RELOAD_FOR_OTHER_ADDRESS))
234 /* Nonzero if these two reload purposes produce RELOAD_OTHER when merged. */
235 #define MERGE_TO_OTHER(when1, when2, op1, op2) \
236 ((when1) != (when2) \
237 || ! ((op1) == (op2) \
238 || (when1) == RELOAD_FOR_INPUT \
239 || (when1) == RELOAD_FOR_OPERAND_ADDRESS \
240 || (when1) == RELOAD_FOR_OTHER_ADDRESS))
242 /* If we are going to reload an address, compute the reload type to
243 use. */
244 #define ADDR_TYPE(type) \
245 ((type) == RELOAD_FOR_INPUT_ADDRESS \
246 ? RELOAD_FOR_INPADDR_ADDRESS \
247 : ((type) == RELOAD_FOR_OUTPUT_ADDRESS \
248 ? RELOAD_FOR_OUTADDR_ADDRESS \
249 : (type)))
251 static int push_secondary_reload (int, rtx, int, int, enum reg_class,
252 enum machine_mode, enum reload_type,
253 enum insn_code *, secondary_reload_info *);
254 static enum reg_class find_valid_class (enum machine_mode, enum machine_mode,
255 int, unsigned int);
256 static void push_replacement (rtx *, int, enum machine_mode);
257 static void dup_replacements (rtx *, rtx *);
258 static void combine_reloads (void);
259 static int find_reusable_reload (rtx *, rtx, enum reg_class,
260 enum reload_type, int, int);
261 static rtx find_dummy_reload (rtx, rtx, rtx *, rtx *, enum machine_mode,
262 enum machine_mode, reg_class_t, int, int);
263 static int hard_reg_set_here_p (unsigned int, unsigned int, rtx);
264 static struct decomposition decompose (rtx);
265 static int immune_p (rtx, rtx, struct decomposition);
266 static bool alternative_allows_const_pool_ref (rtx, const char *, int);
267 static rtx find_reloads_toplev (rtx, int, enum reload_type, int, int,
268 rtx_insn *, int *);
269 static rtx make_memloc (rtx, int);
270 static int maybe_memory_address_addr_space_p (enum machine_mode, rtx,
271 addr_space_t, rtx *);
272 static int find_reloads_address (enum machine_mode, rtx *, rtx, rtx *,
273 int, enum reload_type, int, rtx_insn *);
274 static rtx subst_reg_equivs (rtx, rtx_insn *);
275 static rtx subst_indexed_address (rtx);
276 static void update_auto_inc_notes (rtx_insn *, int, int);
277 static int find_reloads_address_1 (enum machine_mode, addr_space_t, rtx, int,
278 enum rtx_code, enum rtx_code, rtx *,
279 int, enum reload_type,int, rtx_insn *);
280 static void find_reloads_address_part (rtx, rtx *, enum reg_class,
281 enum machine_mode, int,
282 enum reload_type, int);
283 static rtx find_reloads_subreg_address (rtx, int, enum reload_type,
284 int, rtx_insn *, int *);
285 static void copy_replacements_1 (rtx *, rtx *, int);
286 static int find_inc_amount (rtx, rtx);
287 static int refers_to_mem_for_reload_p (rtx);
288 static int refers_to_regno_for_reload_p (unsigned int, unsigned int,
289 rtx, rtx *);
291 /* Add NEW to reg_equiv_alt_mem_list[REGNO] if it's not present in the
292 list yet. */
294 static void
295 push_reg_equiv_alt_mem (int regno, rtx mem)
297 rtx it;
299 for (it = reg_equiv_alt_mem_list (regno); it; it = XEXP (it, 1))
300 if (rtx_equal_p (XEXP (it, 0), mem))
301 return;
303 reg_equiv_alt_mem_list (regno)
304 = alloc_EXPR_LIST (REG_EQUIV, mem,
305 reg_equiv_alt_mem_list (regno));
308 /* Determine if any secondary reloads are needed for loading (if IN_P is
309 nonzero) or storing (if IN_P is zero) X to or from a reload register of
310 register class RELOAD_CLASS in mode RELOAD_MODE. If secondary reloads
311 are needed, push them.
313 Return the reload number of the secondary reload we made, or -1 if
314 we didn't need one. *PICODE is set to the insn_code to use if we do
315 need a secondary reload. */
317 static int
318 push_secondary_reload (int in_p, rtx x, int opnum, int optional,
319 enum reg_class reload_class,
320 enum machine_mode reload_mode, enum reload_type type,
321 enum insn_code *picode, secondary_reload_info *prev_sri)
323 enum reg_class rclass = NO_REGS;
324 enum reg_class scratch_class;
325 enum machine_mode mode = reload_mode;
326 enum insn_code icode = CODE_FOR_nothing;
327 enum insn_code t_icode = CODE_FOR_nothing;
328 enum reload_type secondary_type;
329 int s_reload, t_reload = -1;
330 const char *scratch_constraint;
331 secondary_reload_info sri;
333 if (type == RELOAD_FOR_INPUT_ADDRESS
334 || type == RELOAD_FOR_OUTPUT_ADDRESS
335 || type == RELOAD_FOR_INPADDR_ADDRESS
336 || type == RELOAD_FOR_OUTADDR_ADDRESS)
337 secondary_type = type;
338 else
339 secondary_type = in_p ? RELOAD_FOR_INPUT_ADDRESS : RELOAD_FOR_OUTPUT_ADDRESS;
341 *picode = CODE_FOR_nothing;
343 /* If X is a paradoxical SUBREG, use the inner value to determine both the
344 mode and object being reloaded. */
345 if (paradoxical_subreg_p (x))
347 x = SUBREG_REG (x);
348 reload_mode = GET_MODE (x);
351 /* If X is a pseudo-register that has an equivalent MEM (actually, if it
352 is still a pseudo-register by now, it *must* have an equivalent MEM
353 but we don't want to assume that), use that equivalent when seeing if
354 a secondary reload is needed since whether or not a reload is needed
355 might be sensitive to the form of the MEM. */
357 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER
358 && reg_equiv_mem (REGNO (x)))
359 x = reg_equiv_mem (REGNO (x));
361 sri.icode = CODE_FOR_nothing;
362 sri.prev_sri = prev_sri;
363 rclass = (enum reg_class) targetm.secondary_reload (in_p, x, reload_class,
364 reload_mode, &sri);
365 icode = (enum insn_code) sri.icode;
367 /* If we don't need any secondary registers, done. */
368 if (rclass == NO_REGS && icode == CODE_FOR_nothing)
369 return -1;
371 if (rclass != NO_REGS)
372 t_reload = push_secondary_reload (in_p, x, opnum, optional, rclass,
373 reload_mode, type, &t_icode, &sri);
375 /* If we will be using an insn, the secondary reload is for a
376 scratch register. */
378 if (icode != CODE_FOR_nothing)
380 /* If IN_P is nonzero, the reload register will be the output in
381 operand 0. If IN_P is zero, the reload register will be the input
382 in operand 1. Outputs should have an initial "=", which we must
383 skip. */
385 /* ??? It would be useful to be able to handle only two, or more than
386 three, operands, but for now we can only handle the case of having
387 exactly three: output, input and one temp/scratch. */
388 gcc_assert (insn_data[(int) icode].n_operands == 3);
390 /* ??? We currently have no way to represent a reload that needs
391 an icode to reload from an intermediate tertiary reload register.
392 We should probably have a new field in struct reload to tag a
393 chain of scratch operand reloads onto. */
394 gcc_assert (rclass == NO_REGS);
396 scratch_constraint = insn_data[(int) icode].operand[2].constraint;
397 gcc_assert (*scratch_constraint == '=');
398 scratch_constraint++;
399 if (*scratch_constraint == '&')
400 scratch_constraint++;
401 scratch_class = (reg_class_for_constraint
402 (lookup_constraint (scratch_constraint)));
404 rclass = scratch_class;
405 mode = insn_data[(int) icode].operand[2].mode;
408 /* This case isn't valid, so fail. Reload is allowed to use the same
409 register for RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT reloads, but
410 in the case of a secondary register, we actually need two different
411 registers for correct code. We fail here to prevent the possibility of
412 silently generating incorrect code later.
414 The convention is that secondary input reloads are valid only if the
415 secondary_class is different from class. If you have such a case, you
416 can not use secondary reloads, you must work around the problem some
417 other way.
419 Allow this when a reload_in/out pattern is being used. I.e. assume
420 that the generated code handles this case. */
422 gcc_assert (!in_p || rclass != reload_class || icode != CODE_FOR_nothing
423 || t_icode != CODE_FOR_nothing);
425 /* See if we can reuse an existing secondary reload. */
426 for (s_reload = 0; s_reload < n_reloads; s_reload++)
427 if (rld[s_reload].secondary_p
428 && (reg_class_subset_p (rclass, rld[s_reload].rclass)
429 || reg_class_subset_p (rld[s_reload].rclass, rclass))
430 && ((in_p && rld[s_reload].inmode == mode)
431 || (! in_p && rld[s_reload].outmode == mode))
432 && ((in_p && rld[s_reload].secondary_in_reload == t_reload)
433 || (! in_p && rld[s_reload].secondary_out_reload == t_reload))
434 && ((in_p && rld[s_reload].secondary_in_icode == t_icode)
435 || (! in_p && rld[s_reload].secondary_out_icode == t_icode))
436 && (small_register_class_p (rclass)
437 || targetm.small_register_classes_for_mode_p (VOIDmode))
438 && MERGABLE_RELOADS (secondary_type, rld[s_reload].when_needed,
439 opnum, rld[s_reload].opnum))
441 if (in_p)
442 rld[s_reload].inmode = mode;
443 if (! in_p)
444 rld[s_reload].outmode = mode;
446 if (reg_class_subset_p (rclass, rld[s_reload].rclass))
447 rld[s_reload].rclass = rclass;
449 rld[s_reload].opnum = MIN (rld[s_reload].opnum, opnum);
450 rld[s_reload].optional &= optional;
451 rld[s_reload].secondary_p = 1;
452 if (MERGE_TO_OTHER (secondary_type, rld[s_reload].when_needed,
453 opnum, rld[s_reload].opnum))
454 rld[s_reload].when_needed = RELOAD_OTHER;
456 break;
459 if (s_reload == n_reloads)
461 #ifdef SECONDARY_MEMORY_NEEDED
462 /* If we need a memory location to copy between the two reload regs,
463 set it up now. Note that we do the input case before making
464 the reload and the output case after. This is due to the
465 way reloads are output. */
467 if (in_p && icode == CODE_FOR_nothing
468 && SECONDARY_MEMORY_NEEDED (rclass, reload_class, mode))
470 get_secondary_mem (x, reload_mode, opnum, type);
472 /* We may have just added new reloads. Make sure we add
473 the new reload at the end. */
474 s_reload = n_reloads;
476 #endif
478 /* We need to make a new secondary reload for this register class. */
479 rld[s_reload].in = rld[s_reload].out = 0;
480 rld[s_reload].rclass = rclass;
482 rld[s_reload].inmode = in_p ? mode : VOIDmode;
483 rld[s_reload].outmode = ! in_p ? mode : VOIDmode;
484 rld[s_reload].reg_rtx = 0;
485 rld[s_reload].optional = optional;
486 rld[s_reload].inc = 0;
487 /* Maybe we could combine these, but it seems too tricky. */
488 rld[s_reload].nocombine = 1;
489 rld[s_reload].in_reg = 0;
490 rld[s_reload].out_reg = 0;
491 rld[s_reload].opnum = opnum;
492 rld[s_reload].when_needed = secondary_type;
493 rld[s_reload].secondary_in_reload = in_p ? t_reload : -1;
494 rld[s_reload].secondary_out_reload = ! in_p ? t_reload : -1;
495 rld[s_reload].secondary_in_icode = in_p ? t_icode : CODE_FOR_nothing;
496 rld[s_reload].secondary_out_icode
497 = ! in_p ? t_icode : CODE_FOR_nothing;
498 rld[s_reload].secondary_p = 1;
500 n_reloads++;
502 #ifdef SECONDARY_MEMORY_NEEDED
503 if (! in_p && icode == CODE_FOR_nothing
504 && SECONDARY_MEMORY_NEEDED (reload_class, rclass, mode))
505 get_secondary_mem (x, mode, opnum, type);
506 #endif
509 *picode = icode;
510 return s_reload;
513 /* If a secondary reload is needed, return its class. If both an intermediate
514 register and a scratch register is needed, we return the class of the
515 intermediate register. */
516 reg_class_t
517 secondary_reload_class (bool in_p, reg_class_t rclass, enum machine_mode mode,
518 rtx x)
520 enum insn_code icode;
521 secondary_reload_info sri;
523 sri.icode = CODE_FOR_nothing;
524 sri.prev_sri = NULL;
525 rclass
526 = (enum reg_class) targetm.secondary_reload (in_p, x, rclass, mode, &sri);
527 icode = (enum insn_code) sri.icode;
529 /* If there are no secondary reloads at all, we return NO_REGS.
530 If an intermediate register is needed, we return its class. */
531 if (icode == CODE_FOR_nothing || rclass != NO_REGS)
532 return rclass;
534 /* No intermediate register is needed, but we have a special reload
535 pattern, which we assume for now needs a scratch register. */
536 return scratch_reload_class (icode);
539 /* ICODE is the insn_code of a reload pattern. Check that it has exactly
540 three operands, verify that operand 2 is an output operand, and return
541 its register class.
542 ??? We'd like to be able to handle any pattern with at least 2 operands,
543 for zero or more scratch registers, but that needs more infrastructure. */
544 enum reg_class
545 scratch_reload_class (enum insn_code icode)
547 const char *scratch_constraint;
548 enum reg_class rclass;
550 gcc_assert (insn_data[(int) icode].n_operands == 3);
551 scratch_constraint = insn_data[(int) icode].operand[2].constraint;
552 gcc_assert (*scratch_constraint == '=');
553 scratch_constraint++;
554 if (*scratch_constraint == '&')
555 scratch_constraint++;
556 rclass = reg_class_for_constraint (lookup_constraint (scratch_constraint));
557 gcc_assert (rclass != NO_REGS);
558 return rclass;
561 #ifdef SECONDARY_MEMORY_NEEDED
563 /* Return a memory location that will be used to copy X in mode MODE.
564 If we haven't already made a location for this mode in this insn,
565 call find_reloads_address on the location being returned. */
568 get_secondary_mem (rtx x ATTRIBUTE_UNUSED, enum machine_mode mode,
569 int opnum, enum reload_type type)
571 rtx loc;
572 int mem_valid;
574 /* By default, if MODE is narrower than a word, widen it to a word.
575 This is required because most machines that require these memory
576 locations do not support short load and stores from all registers
577 (e.g., FP registers). */
579 #ifdef SECONDARY_MEMORY_NEEDED_MODE
580 mode = SECONDARY_MEMORY_NEEDED_MODE (mode);
581 #else
582 if (GET_MODE_BITSIZE (mode) < BITS_PER_WORD && INTEGRAL_MODE_P (mode))
583 mode = mode_for_size (BITS_PER_WORD, GET_MODE_CLASS (mode), 0);
584 #endif
586 /* If we already have made a MEM for this operand in MODE, return it. */
587 if (secondary_memlocs_elim[(int) mode][opnum] != 0)
588 return secondary_memlocs_elim[(int) mode][opnum];
590 /* If this is the first time we've tried to get a MEM for this mode,
591 allocate a new one. `something_changed' in reload will get set
592 by noticing that the frame size has changed. */
594 if (secondary_memlocs[(int) mode] == 0)
596 #ifdef SECONDARY_MEMORY_NEEDED_RTX
597 secondary_memlocs[(int) mode] = SECONDARY_MEMORY_NEEDED_RTX (mode);
598 #else
599 secondary_memlocs[(int) mode]
600 = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
601 #endif
604 /* Get a version of the address doing any eliminations needed. If that
605 didn't give us a new MEM, make a new one if it isn't valid. */
607 loc = eliminate_regs (secondary_memlocs[(int) mode], VOIDmode, NULL_RTX);
608 mem_valid = strict_memory_address_addr_space_p (mode, XEXP (loc, 0),
609 MEM_ADDR_SPACE (loc));
611 if (! mem_valid && loc == secondary_memlocs[(int) mode])
612 loc = copy_rtx (loc);
614 /* The only time the call below will do anything is if the stack
615 offset is too large. In that case IND_LEVELS doesn't matter, so we
616 can just pass a zero. Adjust the type to be the address of the
617 corresponding object. If the address was valid, save the eliminated
618 address. If it wasn't valid, we need to make a reload each time, so
619 don't save it. */
621 if (! mem_valid)
623 type = (type == RELOAD_FOR_INPUT ? RELOAD_FOR_INPUT_ADDRESS
624 : type == RELOAD_FOR_OUTPUT ? RELOAD_FOR_OUTPUT_ADDRESS
625 : RELOAD_OTHER);
627 find_reloads_address (mode, &loc, XEXP (loc, 0), &XEXP (loc, 0),
628 opnum, type, 0, 0);
631 secondary_memlocs_elim[(int) mode][opnum] = loc;
632 if (secondary_memlocs_elim_used <= (int)mode)
633 secondary_memlocs_elim_used = (int)mode + 1;
634 return loc;
637 /* Clear any secondary memory locations we've made. */
639 void
640 clear_secondary_mem (void)
642 memset (secondary_memlocs, 0, sizeof secondary_memlocs);
644 #endif /* SECONDARY_MEMORY_NEEDED */
647 /* Find the largest class which has at least one register valid in
648 mode INNER, and which for every such register, that register number
649 plus N is also valid in OUTER (if in range) and is cheap to move
650 into REGNO. Such a class must exist. */
652 static enum reg_class
653 find_valid_class (enum machine_mode outer ATTRIBUTE_UNUSED,
654 enum machine_mode inner ATTRIBUTE_UNUSED, int n,
655 unsigned int dest_regno ATTRIBUTE_UNUSED)
657 int best_cost = -1;
658 int rclass;
659 int regno;
660 enum reg_class best_class = NO_REGS;
661 enum reg_class dest_class ATTRIBUTE_UNUSED = REGNO_REG_CLASS (dest_regno);
662 unsigned int best_size = 0;
663 int cost;
665 for (rclass = 1; rclass < N_REG_CLASSES; rclass++)
667 int bad = 0;
668 int good = 0;
669 for (regno = 0; regno < FIRST_PSEUDO_REGISTER - n && ! bad; regno++)
670 if (TEST_HARD_REG_BIT (reg_class_contents[rclass], regno))
672 if (HARD_REGNO_MODE_OK (regno, inner))
674 good = 1;
675 if (TEST_HARD_REG_BIT (reg_class_contents[rclass], regno + n)
676 && ! HARD_REGNO_MODE_OK (regno + n, outer))
677 bad = 1;
681 if (bad || !good)
682 continue;
683 cost = register_move_cost (outer, (enum reg_class) rclass, dest_class);
685 if ((reg_class_size[rclass] > best_size
686 && (best_cost < 0 || best_cost >= cost))
687 || best_cost > cost)
689 best_class = (enum reg_class) rclass;
690 best_size = reg_class_size[rclass];
691 best_cost = register_move_cost (outer, (enum reg_class) rclass,
692 dest_class);
696 gcc_assert (best_size != 0);
698 return best_class;
701 /* We are trying to reload a subreg of something that is not a register.
702 Find the largest class which contains only registers valid in
703 mode MODE. OUTER is the mode of the subreg, DEST_CLASS the class in
704 which we would eventually like to obtain the object. */
706 static enum reg_class
707 find_valid_class_1 (enum machine_mode outer ATTRIBUTE_UNUSED,
708 enum machine_mode mode ATTRIBUTE_UNUSED,
709 enum reg_class dest_class ATTRIBUTE_UNUSED)
711 int best_cost = -1;
712 int rclass;
713 int regno;
714 enum reg_class best_class = NO_REGS;
715 unsigned int best_size = 0;
716 int cost;
718 for (rclass = 1; rclass < N_REG_CLASSES; rclass++)
720 int bad = 0;
721 for (regno = 0; regno < FIRST_PSEUDO_REGISTER && !bad; regno++)
723 if (in_hard_reg_set_p (reg_class_contents[rclass], mode, regno)
724 && !HARD_REGNO_MODE_OK (regno, mode))
725 bad = 1;
728 if (bad)
729 continue;
731 cost = register_move_cost (outer, (enum reg_class) rclass, dest_class);
733 if ((reg_class_size[rclass] > best_size
734 && (best_cost < 0 || best_cost >= cost))
735 || best_cost > cost)
737 best_class = (enum reg_class) rclass;
738 best_size = reg_class_size[rclass];
739 best_cost = register_move_cost (outer, (enum reg_class) rclass,
740 dest_class);
744 gcc_assert (best_size != 0);
746 #ifdef LIMIT_RELOAD_CLASS
747 best_class = LIMIT_RELOAD_CLASS (mode, best_class);
748 #endif
749 return best_class;
752 /* Return the number of a previously made reload that can be combined with
753 a new one, or n_reloads if none of the existing reloads can be used.
754 OUT, RCLASS, TYPE and OPNUM are the same arguments as passed to
755 push_reload, they determine the kind of the new reload that we try to
756 combine. P_IN points to the corresponding value of IN, which can be
757 modified by this function.
758 DONT_SHARE is nonzero if we can't share any input-only reload for IN. */
760 static int
761 find_reusable_reload (rtx *p_in, rtx out, enum reg_class rclass,
762 enum reload_type type, int opnum, int dont_share)
764 rtx in = *p_in;
765 int i;
766 /* We can't merge two reloads if the output of either one is
767 earlyclobbered. */
769 if (earlyclobber_operand_p (out))
770 return n_reloads;
772 /* We can use an existing reload if the class is right
773 and at least one of IN and OUT is a match
774 and the other is at worst neutral.
775 (A zero compared against anything is neutral.)
777 For targets with small register classes, don't use existing reloads
778 unless they are for the same thing since that can cause us to need
779 more reload registers than we otherwise would. */
781 for (i = 0; i < n_reloads; i++)
782 if ((reg_class_subset_p (rclass, rld[i].rclass)
783 || reg_class_subset_p (rld[i].rclass, rclass))
784 /* If the existing reload has a register, it must fit our class. */
785 && (rld[i].reg_rtx == 0
786 || TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
787 true_regnum (rld[i].reg_rtx)))
788 && ((in != 0 && MATCHES (rld[i].in, in) && ! dont_share
789 && (out == 0 || rld[i].out == 0 || MATCHES (rld[i].out, out)))
790 || (out != 0 && MATCHES (rld[i].out, out)
791 && (in == 0 || rld[i].in == 0 || MATCHES (rld[i].in, in))))
792 && (rld[i].out == 0 || ! earlyclobber_operand_p (rld[i].out))
793 && (small_register_class_p (rclass)
794 || targetm.small_register_classes_for_mode_p (VOIDmode))
795 && MERGABLE_RELOADS (type, rld[i].when_needed, opnum, rld[i].opnum))
796 return i;
798 /* Reloading a plain reg for input can match a reload to postincrement
799 that reg, since the postincrement's value is the right value.
800 Likewise, it can match a preincrement reload, since we regard
801 the preincrementation as happening before any ref in this insn
802 to that register. */
803 for (i = 0; i < n_reloads; i++)
804 if ((reg_class_subset_p (rclass, rld[i].rclass)
805 || reg_class_subset_p (rld[i].rclass, rclass))
806 /* If the existing reload has a register, it must fit our
807 class. */
808 && (rld[i].reg_rtx == 0
809 || TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
810 true_regnum (rld[i].reg_rtx)))
811 && out == 0 && rld[i].out == 0 && rld[i].in != 0
812 && ((REG_P (in)
813 && GET_RTX_CLASS (GET_CODE (rld[i].in)) == RTX_AUTOINC
814 && MATCHES (XEXP (rld[i].in, 0), in))
815 || (REG_P (rld[i].in)
816 && GET_RTX_CLASS (GET_CODE (in)) == RTX_AUTOINC
817 && MATCHES (XEXP (in, 0), rld[i].in)))
818 && (rld[i].out == 0 || ! earlyclobber_operand_p (rld[i].out))
819 && (small_register_class_p (rclass)
820 || targetm.small_register_classes_for_mode_p (VOIDmode))
821 && MERGABLE_RELOADS (type, rld[i].when_needed,
822 opnum, rld[i].opnum))
824 /* Make sure reload_in ultimately has the increment,
825 not the plain register. */
826 if (REG_P (in))
827 *p_in = rld[i].in;
828 return i;
830 return n_reloads;
833 /* Return true if X is a SUBREG that will need reloading of its SUBREG_REG
834 expression. MODE is the mode that X will be used in. OUTPUT is true if
835 the function is invoked for the output part of an enclosing reload. */
837 static bool
838 reload_inner_reg_of_subreg (rtx x, enum machine_mode mode, bool output)
840 rtx inner;
842 /* Only SUBREGs are problematical. */
843 if (GET_CODE (x) != SUBREG)
844 return false;
846 inner = SUBREG_REG (x);
848 /* If INNER is a constant or PLUS, then INNER will need reloading. */
849 if (CONSTANT_P (inner) || GET_CODE (inner) == PLUS)
850 return true;
852 /* If INNER is not a hard register, then INNER will not need reloading. */
853 if (!(REG_P (inner) && HARD_REGISTER_P (inner)))
854 return false;
856 /* If INNER is not ok for MODE, then INNER will need reloading. */
857 if (!HARD_REGNO_MODE_OK (subreg_regno (x), mode))
858 return true;
860 /* If this is for an output, and the outer part is a word or smaller,
861 INNER is larger than a word and the number of registers in INNER is
862 not the same as the number of words in INNER, then INNER will need
863 reloading (with an in-out reload). */
864 return (output
865 && GET_MODE_SIZE (mode) <= UNITS_PER_WORD
866 && GET_MODE_SIZE (GET_MODE (inner)) > UNITS_PER_WORD
867 && ((GET_MODE_SIZE (GET_MODE (inner)) / UNITS_PER_WORD)
868 != (int) hard_regno_nregs[REGNO (inner)][GET_MODE (inner)]));
871 /* Return nonzero if IN can be reloaded into REGNO with mode MODE without
872 requiring an extra reload register. The caller has already found that
873 IN contains some reference to REGNO, so check that we can produce the
874 new value in a single step. E.g. if we have
875 (set (reg r13) (plus (reg r13) (const int 1))), and there is an
876 instruction that adds one to a register, this should succeed.
877 However, if we have something like
878 (set (reg r13) (plus (reg r13) (const int 999))), and the constant 999
879 needs to be loaded into a register first, we need a separate reload
880 register.
881 Such PLUS reloads are generated by find_reload_address_part.
882 The out-of-range PLUS expressions are usually introduced in the instruction
883 patterns by register elimination and substituting pseudos without a home
884 by their function-invariant equivalences. */
885 static int
886 can_reload_into (rtx in, int regno, enum machine_mode mode)
888 rtx dst;
889 rtx_insn *test_insn;
890 int r = 0;
891 struct recog_data_d save_recog_data;
893 /* For matching constraints, we often get notional input reloads where
894 we want to use the original register as the reload register. I.e.
895 technically this is a non-optional input-output reload, but IN is
896 already a valid register, and has been chosen as the reload register.
897 Speed this up, since it trivially works. */
898 if (REG_P (in))
899 return 1;
901 /* To test MEMs properly, we'd have to take into account all the reloads
902 that are already scheduled, which can become quite complicated.
903 And since we've already handled address reloads for this MEM, it
904 should always succeed anyway. */
905 if (MEM_P (in))
906 return 1;
908 /* If we can make a simple SET insn that does the job, everything should
909 be fine. */
910 dst = gen_rtx_REG (mode, regno);
911 test_insn = make_insn_raw (gen_rtx_SET (VOIDmode, dst, in));
912 save_recog_data = recog_data;
913 if (recog_memoized (test_insn) >= 0)
915 extract_insn (test_insn);
916 r = constrain_operands (1);
918 recog_data = save_recog_data;
919 return r;
922 /* Record one reload that needs to be performed.
923 IN is an rtx saying where the data are to be found before this instruction.
924 OUT says where they must be stored after the instruction.
925 (IN is zero for data not read, and OUT is zero for data not written.)
926 INLOC and OUTLOC point to the places in the instructions where
927 IN and OUT were found.
928 If IN and OUT are both nonzero, it means the same register must be used
929 to reload both IN and OUT.
931 RCLASS is a register class required for the reloaded data.
932 INMODE is the machine mode that the instruction requires
933 for the reg that replaces IN and OUTMODE is likewise for OUT.
935 If IN is zero, then OUT's location and mode should be passed as
936 INLOC and INMODE.
938 STRICT_LOW is the 1 if there is a containing STRICT_LOW_PART rtx.
940 OPTIONAL nonzero means this reload does not need to be performed:
941 it can be discarded if that is more convenient.
943 OPNUM and TYPE say what the purpose of this reload is.
945 The return value is the reload-number for this reload.
947 If both IN and OUT are nonzero, in some rare cases we might
948 want to make two separate reloads. (Actually we never do this now.)
949 Therefore, the reload-number for OUT is stored in
950 output_reloadnum when we return; the return value applies to IN.
951 Usually (presently always), when IN and OUT are nonzero,
952 the two reload-numbers are equal, but the caller should be careful to
953 distinguish them. */
956 push_reload (rtx in, rtx out, rtx *inloc, rtx *outloc,
957 enum reg_class rclass, enum machine_mode inmode,
958 enum machine_mode outmode, int strict_low, int optional,
959 int opnum, enum reload_type type)
961 int i;
962 int dont_share = 0;
963 int dont_remove_subreg = 0;
964 #ifdef LIMIT_RELOAD_CLASS
965 rtx *in_subreg_loc = 0, *out_subreg_loc = 0;
966 #endif
967 int secondary_in_reload = -1, secondary_out_reload = -1;
968 enum insn_code secondary_in_icode = CODE_FOR_nothing;
969 enum insn_code secondary_out_icode = CODE_FOR_nothing;
970 enum reg_class subreg_in_class ATTRIBUTE_UNUSED;
971 subreg_in_class = NO_REGS;
973 /* INMODE and/or OUTMODE could be VOIDmode if no mode
974 has been specified for the operand. In that case,
975 use the operand's mode as the mode to reload. */
976 if (inmode == VOIDmode && in != 0)
977 inmode = GET_MODE (in);
978 if (outmode == VOIDmode && out != 0)
979 outmode = GET_MODE (out);
981 /* If find_reloads and friends until now missed to replace a pseudo
982 with a constant of reg_equiv_constant something went wrong
983 beforehand.
984 Note that it can't simply be done here if we missed it earlier
985 since the constant might need to be pushed into the literal pool
986 and the resulting memref would probably need further
987 reloading. */
988 if (in != 0 && REG_P (in))
990 int regno = REGNO (in);
992 gcc_assert (regno < FIRST_PSEUDO_REGISTER
993 || reg_renumber[regno] >= 0
994 || reg_equiv_constant (regno) == NULL_RTX);
997 /* reg_equiv_constant only contains constants which are obviously
998 not appropriate as destination. So if we would need to replace
999 the destination pseudo with a constant we are in real
1000 trouble. */
1001 if (out != 0 && REG_P (out))
1003 int regno = REGNO (out);
1005 gcc_assert (regno < FIRST_PSEUDO_REGISTER
1006 || reg_renumber[regno] >= 0
1007 || reg_equiv_constant (regno) == NULL_RTX);
1010 /* If we have a read-write operand with an address side-effect,
1011 change either IN or OUT so the side-effect happens only once. */
1012 if (in != 0 && out != 0 && MEM_P (in) && rtx_equal_p (in, out))
1013 switch (GET_CODE (XEXP (in, 0)))
1015 case POST_INC: case POST_DEC: case POST_MODIFY:
1016 in = replace_equiv_address_nv (in, XEXP (XEXP (in, 0), 0));
1017 break;
1019 case PRE_INC: case PRE_DEC: case PRE_MODIFY:
1020 out = replace_equiv_address_nv (out, XEXP (XEXP (out, 0), 0));
1021 break;
1023 default:
1024 break;
1027 /* If we are reloading a (SUBREG constant ...), really reload just the
1028 inside expression in its own mode. Similarly for (SUBREG (PLUS ...)).
1029 If we have (SUBREG:M1 (MEM:M2 ...) ...) (or an inner REG that is still
1030 a pseudo and hence will become a MEM) with M1 wider than M2 and the
1031 register is a pseudo, also reload the inside expression.
1032 For machines that extend byte loads, do this for any SUBREG of a pseudo
1033 where both M1 and M2 are a word or smaller, M1 is wider than M2, and
1034 M2 is an integral mode that gets extended when loaded.
1035 Similar issue for (SUBREG:M1 (REG:M2 ...) ...) for a hard register R
1036 where either M1 is not valid for R or M2 is wider than a word but we
1037 only need one register to store an M2-sized quantity in R.
1038 (However, if OUT is nonzero, we need to reload the reg *and*
1039 the subreg, so do nothing here, and let following statement handle it.)
1041 Note that the case of (SUBREG (CONST_INT...)...) is handled elsewhere;
1042 we can't handle it here because CONST_INT does not indicate a mode.
1044 Similarly, we must reload the inside expression if we have a
1045 STRICT_LOW_PART (presumably, in == out in this case).
1047 Also reload the inner expression if it does not require a secondary
1048 reload but the SUBREG does.
1050 Finally, reload the inner expression if it is a register that is in
1051 the class whose registers cannot be referenced in a different size
1052 and M1 is not the same size as M2. If subreg_lowpart_p is false, we
1053 cannot reload just the inside since we might end up with the wrong
1054 register class. But if it is inside a STRICT_LOW_PART, we have
1055 no choice, so we hope we do get the right register class there. */
1057 if (in != 0 && GET_CODE (in) == SUBREG
1058 && (subreg_lowpart_p (in) || strict_low)
1059 #ifdef CANNOT_CHANGE_MODE_CLASS
1060 && !CANNOT_CHANGE_MODE_CLASS (GET_MODE (SUBREG_REG (in)), inmode, rclass)
1061 #endif
1062 && contains_reg_of_mode[(int) rclass][(int) GET_MODE (SUBREG_REG (in))]
1063 && (CONSTANT_P (SUBREG_REG (in))
1064 || GET_CODE (SUBREG_REG (in)) == PLUS
1065 || strict_low
1066 || (((REG_P (SUBREG_REG (in))
1067 && REGNO (SUBREG_REG (in)) >= FIRST_PSEUDO_REGISTER)
1068 || MEM_P (SUBREG_REG (in)))
1069 && ((GET_MODE_PRECISION (inmode)
1070 > GET_MODE_PRECISION (GET_MODE (SUBREG_REG (in))))
1071 #ifdef LOAD_EXTEND_OP
1072 || (GET_MODE_SIZE (inmode) <= UNITS_PER_WORD
1073 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1074 <= UNITS_PER_WORD)
1075 && (GET_MODE_PRECISION (inmode)
1076 > GET_MODE_PRECISION (GET_MODE (SUBREG_REG (in))))
1077 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (in)))
1078 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (in))) != UNKNOWN)
1079 #endif
1080 #ifdef WORD_REGISTER_OPERATIONS
1081 || ((GET_MODE_PRECISION (inmode)
1082 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (in))))
1083 && ((GET_MODE_SIZE (inmode) - 1) / UNITS_PER_WORD ==
1084 ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))) - 1)
1085 / UNITS_PER_WORD)))
1086 #endif
1088 || (REG_P (SUBREG_REG (in))
1089 && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER
1090 /* The case where out is nonzero
1091 is handled differently in the following statement. */
1092 && (out == 0 || subreg_lowpart_p (in))
1093 && ((GET_MODE_SIZE (inmode) <= UNITS_PER_WORD
1094 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1095 > UNITS_PER_WORD)
1096 && ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1097 / UNITS_PER_WORD)
1098 != (int) hard_regno_nregs[REGNO (SUBREG_REG (in))]
1099 [GET_MODE (SUBREG_REG (in))]))
1100 || ! HARD_REGNO_MODE_OK (subreg_regno (in), inmode)))
1101 || (secondary_reload_class (1, rclass, inmode, in) != NO_REGS
1102 && (secondary_reload_class (1, rclass, GET_MODE (SUBREG_REG (in)),
1103 SUBREG_REG (in))
1104 == NO_REGS))
1105 #ifdef CANNOT_CHANGE_MODE_CLASS
1106 || (REG_P (SUBREG_REG (in))
1107 && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER
1108 && REG_CANNOT_CHANGE_MODE_P
1109 (REGNO (SUBREG_REG (in)), GET_MODE (SUBREG_REG (in)), inmode))
1110 #endif
1113 #ifdef LIMIT_RELOAD_CLASS
1114 in_subreg_loc = inloc;
1115 #endif
1116 inloc = &SUBREG_REG (in);
1117 in = *inloc;
1118 #if ! defined (LOAD_EXTEND_OP) && ! defined (WORD_REGISTER_OPERATIONS)
1119 if (MEM_P (in))
1120 /* This is supposed to happen only for paradoxical subregs made by
1121 combine.c. (SUBREG (MEM)) isn't supposed to occur other ways. */
1122 gcc_assert (GET_MODE_SIZE (GET_MODE (in)) <= GET_MODE_SIZE (inmode));
1123 #endif
1124 inmode = GET_MODE (in);
1127 /* Similar issue for (SUBREG:M1 (REG:M2 ...) ...) for a hard register R
1128 where M1 is not valid for R if it was not handled by the code above.
1130 Similar issue for (SUBREG constant ...) if it was not handled by the
1131 code above. This can happen if SUBREG_BYTE != 0.
1133 However, we must reload the inner reg *as well as* the subreg in
1134 that case. */
1136 if (in != 0 && reload_inner_reg_of_subreg (in, inmode, false))
1138 if (REG_P (SUBREG_REG (in)))
1139 subreg_in_class
1140 = find_valid_class (inmode, GET_MODE (SUBREG_REG (in)),
1141 subreg_regno_offset (REGNO (SUBREG_REG (in)),
1142 GET_MODE (SUBREG_REG (in)),
1143 SUBREG_BYTE (in),
1144 GET_MODE (in)),
1145 REGNO (SUBREG_REG (in)));
1146 else if (GET_CODE (SUBREG_REG (in)) == SYMBOL_REF)
1147 subreg_in_class = find_valid_class_1 (inmode,
1148 GET_MODE (SUBREG_REG (in)),
1149 rclass);
1151 /* This relies on the fact that emit_reload_insns outputs the
1152 instructions for input reloads of type RELOAD_OTHER in the same
1153 order as the reloads. Thus if the outer reload is also of type
1154 RELOAD_OTHER, we are guaranteed that this inner reload will be
1155 output before the outer reload. */
1156 push_reload (SUBREG_REG (in), NULL_RTX, &SUBREG_REG (in), (rtx *) 0,
1157 subreg_in_class, VOIDmode, VOIDmode, 0, 0, opnum, type);
1158 dont_remove_subreg = 1;
1161 /* Similarly for paradoxical and problematical SUBREGs on the output.
1162 Note that there is no reason we need worry about the previous value
1163 of SUBREG_REG (out); even if wider than out, storing in a subreg is
1164 entitled to clobber it all (except in the case of a word mode subreg
1165 or of a STRICT_LOW_PART, in that latter case the constraint should
1166 label it input-output.) */
1167 if (out != 0 && GET_CODE (out) == SUBREG
1168 && (subreg_lowpart_p (out) || strict_low)
1169 #ifdef CANNOT_CHANGE_MODE_CLASS
1170 && !CANNOT_CHANGE_MODE_CLASS (GET_MODE (SUBREG_REG (out)), outmode, rclass)
1171 #endif
1172 && contains_reg_of_mode[(int) rclass][(int) GET_MODE (SUBREG_REG (out))]
1173 && (CONSTANT_P (SUBREG_REG (out))
1174 || strict_low
1175 || (((REG_P (SUBREG_REG (out))
1176 && REGNO (SUBREG_REG (out)) >= FIRST_PSEUDO_REGISTER)
1177 || MEM_P (SUBREG_REG (out)))
1178 && ((GET_MODE_PRECISION (outmode)
1179 > GET_MODE_PRECISION (GET_MODE (SUBREG_REG (out))))
1180 #ifdef WORD_REGISTER_OPERATIONS
1181 || ((GET_MODE_PRECISION (outmode)
1182 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (out))))
1183 && ((GET_MODE_SIZE (outmode) - 1) / UNITS_PER_WORD ==
1184 ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))) - 1)
1185 / UNITS_PER_WORD)))
1186 #endif
1188 || (REG_P (SUBREG_REG (out))
1189 && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER
1190 /* The case of a word mode subreg
1191 is handled differently in the following statement. */
1192 && ! (GET_MODE_SIZE (outmode) <= UNITS_PER_WORD
1193 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (out)))
1194 > UNITS_PER_WORD))
1195 && ! HARD_REGNO_MODE_OK (subreg_regno (out), outmode))
1196 || (secondary_reload_class (0, rclass, outmode, out) != NO_REGS
1197 && (secondary_reload_class (0, rclass, GET_MODE (SUBREG_REG (out)),
1198 SUBREG_REG (out))
1199 == NO_REGS))
1200 #ifdef CANNOT_CHANGE_MODE_CLASS
1201 || (REG_P (SUBREG_REG (out))
1202 && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER
1203 && REG_CANNOT_CHANGE_MODE_P (REGNO (SUBREG_REG (out)),
1204 GET_MODE (SUBREG_REG (out)),
1205 outmode))
1206 #endif
1209 #ifdef LIMIT_RELOAD_CLASS
1210 out_subreg_loc = outloc;
1211 #endif
1212 outloc = &SUBREG_REG (out);
1213 out = *outloc;
1214 #if ! defined (LOAD_EXTEND_OP) && ! defined (WORD_REGISTER_OPERATIONS)
1215 gcc_assert (!MEM_P (out)
1216 || GET_MODE_SIZE (GET_MODE (out))
1217 <= GET_MODE_SIZE (outmode));
1218 #endif
1219 outmode = GET_MODE (out);
1222 /* Similar issue for (SUBREG:M1 (REG:M2 ...) ...) for a hard register R
1223 where either M1 is not valid for R or M2 is wider than a word but we
1224 only need one register to store an M2-sized quantity in R.
1226 However, we must reload the inner reg *as well as* the subreg in
1227 that case and the inner reg is an in-out reload. */
1229 if (out != 0 && reload_inner_reg_of_subreg (out, outmode, true))
1231 enum reg_class in_out_class
1232 = find_valid_class (outmode, GET_MODE (SUBREG_REG (out)),
1233 subreg_regno_offset (REGNO (SUBREG_REG (out)),
1234 GET_MODE (SUBREG_REG (out)),
1235 SUBREG_BYTE (out),
1236 GET_MODE (out)),
1237 REGNO (SUBREG_REG (out)));
1239 /* This relies on the fact that emit_reload_insns outputs the
1240 instructions for output reloads of type RELOAD_OTHER in reverse
1241 order of the reloads. Thus if the outer reload is also of type
1242 RELOAD_OTHER, we are guaranteed that this inner reload will be
1243 output after the outer reload. */
1244 push_reload (SUBREG_REG (out), SUBREG_REG (out), &SUBREG_REG (out),
1245 &SUBREG_REG (out), in_out_class, VOIDmode, VOIDmode,
1246 0, 0, opnum, RELOAD_OTHER);
1247 dont_remove_subreg = 1;
1250 /* If IN appears in OUT, we can't share any input-only reload for IN. */
1251 if (in != 0 && out != 0 && MEM_P (out)
1252 && (REG_P (in) || MEM_P (in) || GET_CODE (in) == PLUS)
1253 && reg_overlap_mentioned_for_reload_p (in, XEXP (out, 0)))
1254 dont_share = 1;
1256 /* If IN is a SUBREG of a hard register, make a new REG. This
1257 simplifies some of the cases below. */
1259 if (in != 0 && GET_CODE (in) == SUBREG && REG_P (SUBREG_REG (in))
1260 && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER
1261 && ! dont_remove_subreg)
1262 in = gen_rtx_REG (GET_MODE (in), subreg_regno (in));
1264 /* Similarly for OUT. */
1265 if (out != 0 && GET_CODE (out) == SUBREG
1266 && REG_P (SUBREG_REG (out))
1267 && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER
1268 && ! dont_remove_subreg)
1269 out = gen_rtx_REG (GET_MODE (out), subreg_regno (out));
1271 /* Narrow down the class of register wanted if that is
1272 desirable on this machine for efficiency. */
1274 reg_class_t preferred_class = rclass;
1276 if (in != 0)
1277 preferred_class = targetm.preferred_reload_class (in, rclass);
1279 /* Output reloads may need analogous treatment, different in detail. */
1280 if (out != 0)
1281 preferred_class
1282 = targetm.preferred_output_reload_class (out, preferred_class);
1284 /* Discard what the target said if we cannot do it. */
1285 if (preferred_class != NO_REGS
1286 || (optional && type == RELOAD_FOR_OUTPUT))
1287 rclass = (enum reg_class) preferred_class;
1290 /* Make sure we use a class that can handle the actual pseudo
1291 inside any subreg. For example, on the 386, QImode regs
1292 can appear within SImode subregs. Although GENERAL_REGS
1293 can handle SImode, QImode needs a smaller class. */
1294 #ifdef LIMIT_RELOAD_CLASS
1295 if (in_subreg_loc)
1296 rclass = LIMIT_RELOAD_CLASS (inmode, rclass);
1297 else if (in != 0 && GET_CODE (in) == SUBREG)
1298 rclass = LIMIT_RELOAD_CLASS (GET_MODE (SUBREG_REG (in)), rclass);
1300 if (out_subreg_loc)
1301 rclass = LIMIT_RELOAD_CLASS (outmode, rclass);
1302 if (out != 0 && GET_CODE (out) == SUBREG)
1303 rclass = LIMIT_RELOAD_CLASS (GET_MODE (SUBREG_REG (out)), rclass);
1304 #endif
1306 /* Verify that this class is at least possible for the mode that
1307 is specified. */
1308 if (this_insn_is_asm)
1310 enum machine_mode mode;
1311 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
1312 mode = inmode;
1313 else
1314 mode = outmode;
1315 if (mode == VOIDmode)
1317 error_for_asm (this_insn, "cannot reload integer constant "
1318 "operand in %<asm%>");
1319 mode = word_mode;
1320 if (in != 0)
1321 inmode = word_mode;
1322 if (out != 0)
1323 outmode = word_mode;
1325 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1326 if (HARD_REGNO_MODE_OK (i, mode)
1327 && in_hard_reg_set_p (reg_class_contents[(int) rclass], mode, i))
1328 break;
1329 if (i == FIRST_PSEUDO_REGISTER)
1331 error_for_asm (this_insn, "impossible register constraint "
1332 "in %<asm%>");
1333 /* Avoid further trouble with this insn. */
1334 PATTERN (this_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
1335 /* We used to continue here setting class to ALL_REGS, but it triggers
1336 sanity check on i386 for:
1337 void foo(long double d)
1339 asm("" :: "a" (d));
1341 Returning zero here ought to be safe as we take care in
1342 find_reloads to not process the reloads when instruction was
1343 replaced by USE. */
1345 return 0;
1349 /* Optional output reloads are always OK even if we have no register class,
1350 since the function of these reloads is only to have spill_reg_store etc.
1351 set, so that the storing insn can be deleted later. */
1352 gcc_assert (rclass != NO_REGS
1353 || (optional != 0 && type == RELOAD_FOR_OUTPUT));
1355 i = find_reusable_reload (&in, out, rclass, type, opnum, dont_share);
1357 if (i == n_reloads)
1359 /* See if we need a secondary reload register to move between CLASS
1360 and IN or CLASS and OUT. Get the icode and push any required reloads
1361 needed for each of them if so. */
1363 if (in != 0)
1364 secondary_in_reload
1365 = push_secondary_reload (1, in, opnum, optional, rclass, inmode, type,
1366 &secondary_in_icode, NULL);
1367 if (out != 0 && GET_CODE (out) != SCRATCH)
1368 secondary_out_reload
1369 = push_secondary_reload (0, out, opnum, optional, rclass, outmode,
1370 type, &secondary_out_icode, NULL);
1372 /* We found no existing reload suitable for re-use.
1373 So add an additional reload. */
1375 #ifdef SECONDARY_MEMORY_NEEDED
1376 if (subreg_in_class == NO_REGS
1377 && in != 0
1378 && (REG_P (in)
1379 || (GET_CODE (in) == SUBREG && REG_P (SUBREG_REG (in))))
1380 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER)
1381 subreg_in_class = REGNO_REG_CLASS (reg_or_subregno (in));
1382 /* If a memory location is needed for the copy, make one. */
1383 if (subreg_in_class != NO_REGS
1384 && SECONDARY_MEMORY_NEEDED (subreg_in_class, rclass, inmode))
1385 get_secondary_mem (in, inmode, opnum, type);
1386 #endif
1388 i = n_reloads;
1389 rld[i].in = in;
1390 rld[i].out = out;
1391 rld[i].rclass = rclass;
1392 rld[i].inmode = inmode;
1393 rld[i].outmode = outmode;
1394 rld[i].reg_rtx = 0;
1395 rld[i].optional = optional;
1396 rld[i].inc = 0;
1397 rld[i].nocombine = 0;
1398 rld[i].in_reg = inloc ? *inloc : 0;
1399 rld[i].out_reg = outloc ? *outloc : 0;
1400 rld[i].opnum = opnum;
1401 rld[i].when_needed = type;
1402 rld[i].secondary_in_reload = secondary_in_reload;
1403 rld[i].secondary_out_reload = secondary_out_reload;
1404 rld[i].secondary_in_icode = secondary_in_icode;
1405 rld[i].secondary_out_icode = secondary_out_icode;
1406 rld[i].secondary_p = 0;
1408 n_reloads++;
1410 #ifdef SECONDARY_MEMORY_NEEDED
1411 if (out != 0
1412 && (REG_P (out)
1413 || (GET_CODE (out) == SUBREG && REG_P (SUBREG_REG (out))))
1414 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
1415 && SECONDARY_MEMORY_NEEDED (rclass,
1416 REGNO_REG_CLASS (reg_or_subregno (out)),
1417 outmode))
1418 get_secondary_mem (out, outmode, opnum, type);
1419 #endif
1421 else
1423 /* We are reusing an existing reload,
1424 but we may have additional information for it.
1425 For example, we may now have both IN and OUT
1426 while the old one may have just one of them. */
1428 /* The modes can be different. If they are, we want to reload in
1429 the larger mode, so that the value is valid for both modes. */
1430 if (inmode != VOIDmode
1431 && GET_MODE_SIZE (inmode) > GET_MODE_SIZE (rld[i].inmode))
1432 rld[i].inmode = inmode;
1433 if (outmode != VOIDmode
1434 && GET_MODE_SIZE (outmode) > GET_MODE_SIZE (rld[i].outmode))
1435 rld[i].outmode = outmode;
1436 if (in != 0)
1438 rtx in_reg = inloc ? *inloc : 0;
1439 /* If we merge reloads for two distinct rtl expressions that
1440 are identical in content, there might be duplicate address
1441 reloads. Remove the extra set now, so that if we later find
1442 that we can inherit this reload, we can get rid of the
1443 address reloads altogether.
1445 Do not do this if both reloads are optional since the result
1446 would be an optional reload which could potentially leave
1447 unresolved address replacements.
1449 It is not sufficient to call transfer_replacements since
1450 choose_reload_regs will remove the replacements for address
1451 reloads of inherited reloads which results in the same
1452 problem. */
1453 if (rld[i].in != in && rtx_equal_p (in, rld[i].in)
1454 && ! (rld[i].optional && optional))
1456 /* We must keep the address reload with the lower operand
1457 number alive. */
1458 if (opnum > rld[i].opnum)
1460 remove_address_replacements (in);
1461 in = rld[i].in;
1462 in_reg = rld[i].in_reg;
1464 else
1465 remove_address_replacements (rld[i].in);
1467 /* When emitting reloads we don't necessarily look at the in-
1468 and outmode, but also directly at the operands (in and out).
1469 So we can't simply overwrite them with whatever we have found
1470 for this (to-be-merged) reload, we have to "merge" that too.
1471 Reusing another reload already verified that we deal with the
1472 same operands, just possibly in different modes. So we
1473 overwrite the operands only when the new mode is larger.
1474 See also PR33613. */
1475 if (!rld[i].in
1476 || GET_MODE_SIZE (GET_MODE (in))
1477 > GET_MODE_SIZE (GET_MODE (rld[i].in)))
1478 rld[i].in = in;
1479 if (!rld[i].in_reg
1480 || (in_reg
1481 && GET_MODE_SIZE (GET_MODE (in_reg))
1482 > GET_MODE_SIZE (GET_MODE (rld[i].in_reg))))
1483 rld[i].in_reg = in_reg;
1485 if (out != 0)
1487 if (!rld[i].out
1488 || (out
1489 && GET_MODE_SIZE (GET_MODE (out))
1490 > GET_MODE_SIZE (GET_MODE (rld[i].out))))
1491 rld[i].out = out;
1492 if (outloc
1493 && (!rld[i].out_reg
1494 || GET_MODE_SIZE (GET_MODE (*outloc))
1495 > GET_MODE_SIZE (GET_MODE (rld[i].out_reg))))
1496 rld[i].out_reg = *outloc;
1498 if (reg_class_subset_p (rclass, rld[i].rclass))
1499 rld[i].rclass = rclass;
1500 rld[i].optional &= optional;
1501 if (MERGE_TO_OTHER (type, rld[i].when_needed,
1502 opnum, rld[i].opnum))
1503 rld[i].when_needed = RELOAD_OTHER;
1504 rld[i].opnum = MIN (rld[i].opnum, opnum);
1507 /* If the ostensible rtx being reloaded differs from the rtx found
1508 in the location to substitute, this reload is not safe to combine
1509 because we cannot reliably tell whether it appears in the insn. */
1511 if (in != 0 && in != *inloc)
1512 rld[i].nocombine = 1;
1514 #if 0
1515 /* This was replaced by changes in find_reloads_address_1 and the new
1516 function inc_for_reload, which go with a new meaning of reload_inc. */
1518 /* If this is an IN/OUT reload in an insn that sets the CC,
1519 it must be for an autoincrement. It doesn't work to store
1520 the incremented value after the insn because that would clobber the CC.
1521 So we must do the increment of the value reloaded from,
1522 increment it, store it back, then decrement again. */
1523 if (out != 0 && sets_cc0_p (PATTERN (this_insn)))
1525 out = 0;
1526 rld[i].out = 0;
1527 rld[i].inc = find_inc_amount (PATTERN (this_insn), in);
1528 /* If we did not find a nonzero amount-to-increment-by,
1529 that contradicts the belief that IN is being incremented
1530 in an address in this insn. */
1531 gcc_assert (rld[i].inc != 0);
1533 #endif
1535 /* If we will replace IN and OUT with the reload-reg,
1536 record where they are located so that substitution need
1537 not do a tree walk. */
1539 if (replace_reloads)
1541 if (inloc != 0)
1543 struct replacement *r = &replacements[n_replacements++];
1544 r->what = i;
1545 r->where = inloc;
1546 r->mode = inmode;
1548 if (outloc != 0 && outloc != inloc)
1550 struct replacement *r = &replacements[n_replacements++];
1551 r->what = i;
1552 r->where = outloc;
1553 r->mode = outmode;
1557 /* If this reload is just being introduced and it has both
1558 an incoming quantity and an outgoing quantity that are
1559 supposed to be made to match, see if either one of the two
1560 can serve as the place to reload into.
1562 If one of them is acceptable, set rld[i].reg_rtx
1563 to that one. */
1565 if (in != 0 && out != 0 && in != out && rld[i].reg_rtx == 0)
1567 rld[i].reg_rtx = find_dummy_reload (in, out, inloc, outloc,
1568 inmode, outmode,
1569 rld[i].rclass, i,
1570 earlyclobber_operand_p (out));
1572 /* If the outgoing register already contains the same value
1573 as the incoming one, we can dispense with loading it.
1574 The easiest way to tell the caller that is to give a phony
1575 value for the incoming operand (same as outgoing one). */
1576 if (rld[i].reg_rtx == out
1577 && (REG_P (in) || CONSTANT_P (in))
1578 && 0 != find_equiv_reg (in, this_insn, NO_REGS, REGNO (out),
1579 static_reload_reg_p, i, inmode))
1580 rld[i].in = out;
1583 /* If this is an input reload and the operand contains a register that
1584 dies in this insn and is used nowhere else, see if it is the right class
1585 to be used for this reload. Use it if so. (This occurs most commonly
1586 in the case of paradoxical SUBREGs and in-out reloads). We cannot do
1587 this if it is also an output reload that mentions the register unless
1588 the output is a SUBREG that clobbers an entire register.
1590 Note that the operand might be one of the spill regs, if it is a
1591 pseudo reg and we are in a block where spilling has not taken place.
1592 But if there is no spilling in this block, that is OK.
1593 An explicitly used hard reg cannot be a spill reg. */
1595 if (rld[i].reg_rtx == 0 && in != 0 && hard_regs_live_known)
1597 rtx note;
1598 int regno;
1599 enum machine_mode rel_mode = inmode;
1601 if (out && GET_MODE_SIZE (outmode) > GET_MODE_SIZE (inmode))
1602 rel_mode = outmode;
1604 for (note = REG_NOTES (this_insn); note; note = XEXP (note, 1))
1605 if (REG_NOTE_KIND (note) == REG_DEAD
1606 && REG_P (XEXP (note, 0))
1607 && (regno = REGNO (XEXP (note, 0))) < FIRST_PSEUDO_REGISTER
1608 && reg_mentioned_p (XEXP (note, 0), in)
1609 /* Check that a former pseudo is valid; see find_dummy_reload. */
1610 && (ORIGINAL_REGNO (XEXP (note, 0)) < FIRST_PSEUDO_REGISTER
1611 || (! bitmap_bit_p (DF_LR_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
1612 ORIGINAL_REGNO (XEXP (note, 0)))
1613 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] == 1))
1614 && ! refers_to_regno_for_reload_p (regno,
1615 end_hard_regno (rel_mode,
1616 regno),
1617 PATTERN (this_insn), inloc)
1618 /* If this is also an output reload, IN cannot be used as
1619 the reload register if it is set in this insn unless IN
1620 is also OUT. */
1621 && (out == 0 || in == out
1622 || ! hard_reg_set_here_p (regno,
1623 end_hard_regno (rel_mode, regno),
1624 PATTERN (this_insn)))
1625 /* ??? Why is this code so different from the previous?
1626 Is there any simple coherent way to describe the two together?
1627 What's going on here. */
1628 && (in != out
1629 || (GET_CODE (in) == SUBREG
1630 && (((GET_MODE_SIZE (GET_MODE (in)) + (UNITS_PER_WORD - 1))
1631 / UNITS_PER_WORD)
1632 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1633 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
1634 /* Make sure the operand fits in the reg that dies. */
1635 && (GET_MODE_SIZE (rel_mode)
1636 <= GET_MODE_SIZE (GET_MODE (XEXP (note, 0))))
1637 && HARD_REGNO_MODE_OK (regno, inmode)
1638 && HARD_REGNO_MODE_OK (regno, outmode))
1640 unsigned int offs;
1641 unsigned int nregs = MAX (hard_regno_nregs[regno][inmode],
1642 hard_regno_nregs[regno][outmode]);
1644 for (offs = 0; offs < nregs; offs++)
1645 if (fixed_regs[regno + offs]
1646 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
1647 regno + offs))
1648 break;
1650 if (offs == nregs
1651 && (! (refers_to_regno_for_reload_p
1652 (regno, end_hard_regno (inmode, regno), in, (rtx *) 0))
1653 || can_reload_into (in, regno, inmode)))
1655 rld[i].reg_rtx = gen_rtx_REG (rel_mode, regno);
1656 break;
1661 if (out)
1662 output_reloadnum = i;
1664 return i;
1667 /* Record an additional place we must replace a value
1668 for which we have already recorded a reload.
1669 RELOADNUM is the value returned by push_reload
1670 when the reload was recorded.
1671 This is used in insn patterns that use match_dup. */
1673 static void
1674 push_replacement (rtx *loc, int reloadnum, enum machine_mode mode)
1676 if (replace_reloads)
1678 struct replacement *r = &replacements[n_replacements++];
1679 r->what = reloadnum;
1680 r->where = loc;
1681 r->mode = mode;
1685 /* Duplicate any replacement we have recorded to apply at
1686 location ORIG_LOC to also be performed at DUP_LOC.
1687 This is used in insn patterns that use match_dup. */
1689 static void
1690 dup_replacements (rtx *dup_loc, rtx *orig_loc)
1692 int i, n = n_replacements;
1694 for (i = 0; i < n; i++)
1696 struct replacement *r = &replacements[i];
1697 if (r->where == orig_loc)
1698 push_replacement (dup_loc, r->what, r->mode);
1702 /* Transfer all replacements that used to be in reload FROM to be in
1703 reload TO. */
1705 void
1706 transfer_replacements (int to, int from)
1708 int i;
1710 for (i = 0; i < n_replacements; i++)
1711 if (replacements[i].what == from)
1712 replacements[i].what = to;
1715 /* IN_RTX is the value loaded by a reload that we now decided to inherit,
1716 or a subpart of it. If we have any replacements registered for IN_RTX,
1717 cancel the reloads that were supposed to load them.
1718 Return nonzero if we canceled any reloads. */
1720 remove_address_replacements (rtx in_rtx)
1722 int i, j;
1723 char reload_flags[MAX_RELOADS];
1724 int something_changed = 0;
1726 memset (reload_flags, 0, sizeof reload_flags);
1727 for (i = 0, j = 0; i < n_replacements; i++)
1729 if (loc_mentioned_in_p (replacements[i].where, in_rtx))
1730 reload_flags[replacements[i].what] |= 1;
1731 else
1733 replacements[j++] = replacements[i];
1734 reload_flags[replacements[i].what] |= 2;
1737 /* Note that the following store must be done before the recursive calls. */
1738 n_replacements = j;
1740 for (i = n_reloads - 1; i >= 0; i--)
1742 if (reload_flags[i] == 1)
1744 deallocate_reload_reg (i);
1745 remove_address_replacements (rld[i].in);
1746 rld[i].in = 0;
1747 something_changed = 1;
1750 return something_changed;
1753 /* If there is only one output reload, and it is not for an earlyclobber
1754 operand, try to combine it with a (logically unrelated) input reload
1755 to reduce the number of reload registers needed.
1757 This is safe if the input reload does not appear in
1758 the value being output-reloaded, because this implies
1759 it is not needed any more once the original insn completes.
1761 If that doesn't work, see we can use any of the registers that
1762 die in this insn as a reload register. We can if it is of the right
1763 class and does not appear in the value being output-reloaded. */
1765 static void
1766 combine_reloads (void)
1768 int i, regno;
1769 int output_reload = -1;
1770 int secondary_out = -1;
1771 rtx note;
1773 /* Find the output reload; return unless there is exactly one
1774 and that one is mandatory. */
1776 for (i = 0; i < n_reloads; i++)
1777 if (rld[i].out != 0)
1779 if (output_reload >= 0)
1780 return;
1781 output_reload = i;
1784 if (output_reload < 0 || rld[output_reload].optional)
1785 return;
1787 /* An input-output reload isn't combinable. */
1789 if (rld[output_reload].in != 0)
1790 return;
1792 /* If this reload is for an earlyclobber operand, we can't do anything. */
1793 if (earlyclobber_operand_p (rld[output_reload].out))
1794 return;
1796 /* If there is a reload for part of the address of this operand, we would
1797 need to change it to RELOAD_FOR_OTHER_ADDRESS. But that would extend
1798 its life to the point where doing this combine would not lower the
1799 number of spill registers needed. */
1800 for (i = 0; i < n_reloads; i++)
1801 if ((rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
1802 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
1803 && rld[i].opnum == rld[output_reload].opnum)
1804 return;
1806 /* Check each input reload; can we combine it? */
1808 for (i = 0; i < n_reloads; i++)
1809 if (rld[i].in && ! rld[i].optional && ! rld[i].nocombine
1810 /* Life span of this reload must not extend past main insn. */
1811 && rld[i].when_needed != RELOAD_FOR_OUTPUT_ADDRESS
1812 && rld[i].when_needed != RELOAD_FOR_OUTADDR_ADDRESS
1813 && rld[i].when_needed != RELOAD_OTHER
1814 && (ira_reg_class_max_nregs [(int)rld[i].rclass][(int) rld[i].inmode]
1815 == ira_reg_class_max_nregs [(int) rld[output_reload].rclass]
1816 [(int) rld[output_reload].outmode])
1817 && rld[i].inc == 0
1818 && rld[i].reg_rtx == 0
1819 #ifdef SECONDARY_MEMORY_NEEDED
1820 /* Don't combine two reloads with different secondary
1821 memory locations. */
1822 && (secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[i].opnum] == 0
1823 || secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum] == 0
1824 || rtx_equal_p (secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[i].opnum],
1825 secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum]))
1826 #endif
1827 && (targetm.small_register_classes_for_mode_p (VOIDmode)
1828 ? (rld[i].rclass == rld[output_reload].rclass)
1829 : (reg_class_subset_p (rld[i].rclass,
1830 rld[output_reload].rclass)
1831 || reg_class_subset_p (rld[output_reload].rclass,
1832 rld[i].rclass)))
1833 && (MATCHES (rld[i].in, rld[output_reload].out)
1834 /* Args reversed because the first arg seems to be
1835 the one that we imagine being modified
1836 while the second is the one that might be affected. */
1837 || (! reg_overlap_mentioned_for_reload_p (rld[output_reload].out,
1838 rld[i].in)
1839 /* However, if the input is a register that appears inside
1840 the output, then we also can't share.
1841 Imagine (set (mem (reg 69)) (plus (reg 69) ...)).
1842 If the same reload reg is used for both reg 69 and the
1843 result to be stored in memory, then that result
1844 will clobber the address of the memory ref. */
1845 && ! (REG_P (rld[i].in)
1846 && reg_overlap_mentioned_for_reload_p (rld[i].in,
1847 rld[output_reload].out))))
1848 && ! reload_inner_reg_of_subreg (rld[i].in, rld[i].inmode,
1849 rld[i].when_needed != RELOAD_FOR_INPUT)
1850 && (reg_class_size[(int) rld[i].rclass]
1851 || targetm.small_register_classes_for_mode_p (VOIDmode))
1852 /* We will allow making things slightly worse by combining an
1853 input and an output, but no worse than that. */
1854 && (rld[i].when_needed == RELOAD_FOR_INPUT
1855 || rld[i].when_needed == RELOAD_FOR_OUTPUT))
1857 int j;
1859 /* We have found a reload to combine with! */
1860 rld[i].out = rld[output_reload].out;
1861 rld[i].out_reg = rld[output_reload].out_reg;
1862 rld[i].outmode = rld[output_reload].outmode;
1863 /* Mark the old output reload as inoperative. */
1864 rld[output_reload].out = 0;
1865 /* The combined reload is needed for the entire insn. */
1866 rld[i].when_needed = RELOAD_OTHER;
1867 /* If the output reload had a secondary reload, copy it. */
1868 if (rld[output_reload].secondary_out_reload != -1)
1870 rld[i].secondary_out_reload
1871 = rld[output_reload].secondary_out_reload;
1872 rld[i].secondary_out_icode
1873 = rld[output_reload].secondary_out_icode;
1876 #ifdef SECONDARY_MEMORY_NEEDED
1877 /* Copy any secondary MEM. */
1878 if (secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum] != 0)
1879 secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[i].opnum]
1880 = secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum];
1881 #endif
1882 /* If required, minimize the register class. */
1883 if (reg_class_subset_p (rld[output_reload].rclass,
1884 rld[i].rclass))
1885 rld[i].rclass = rld[output_reload].rclass;
1887 /* Transfer all replacements from the old reload to the combined. */
1888 for (j = 0; j < n_replacements; j++)
1889 if (replacements[j].what == output_reload)
1890 replacements[j].what = i;
1892 return;
1895 /* If this insn has only one operand that is modified or written (assumed
1896 to be the first), it must be the one corresponding to this reload. It
1897 is safe to use anything that dies in this insn for that output provided
1898 that it does not occur in the output (we already know it isn't an
1899 earlyclobber. If this is an asm insn, give up. */
1901 if (INSN_CODE (this_insn) == -1)
1902 return;
1904 for (i = 1; i < insn_data[INSN_CODE (this_insn)].n_operands; i++)
1905 if (insn_data[INSN_CODE (this_insn)].operand[i].constraint[0] == '='
1906 || insn_data[INSN_CODE (this_insn)].operand[i].constraint[0] == '+')
1907 return;
1909 /* See if some hard register that dies in this insn and is not used in
1910 the output is the right class. Only works if the register we pick
1911 up can fully hold our output reload. */
1912 for (note = REG_NOTES (this_insn); note; note = XEXP (note, 1))
1913 if (REG_NOTE_KIND (note) == REG_DEAD
1914 && REG_P (XEXP (note, 0))
1915 && !reg_overlap_mentioned_for_reload_p (XEXP (note, 0),
1916 rld[output_reload].out)
1917 && (regno = REGNO (XEXP (note, 0))) < FIRST_PSEUDO_REGISTER
1918 && HARD_REGNO_MODE_OK (regno, rld[output_reload].outmode)
1919 && TEST_HARD_REG_BIT (reg_class_contents[(int) rld[output_reload].rclass],
1920 regno)
1921 && (hard_regno_nregs[regno][rld[output_reload].outmode]
1922 <= hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))])
1923 /* Ensure that a secondary or tertiary reload for this output
1924 won't want this register. */
1925 && ((secondary_out = rld[output_reload].secondary_out_reload) == -1
1926 || (!(TEST_HARD_REG_BIT
1927 (reg_class_contents[(int) rld[secondary_out].rclass], regno))
1928 && ((secondary_out = rld[secondary_out].secondary_out_reload) == -1
1929 || !(TEST_HARD_REG_BIT
1930 (reg_class_contents[(int) rld[secondary_out].rclass],
1931 regno)))))
1932 && !fixed_regs[regno]
1933 /* Check that a former pseudo is valid; see find_dummy_reload. */
1934 && (ORIGINAL_REGNO (XEXP (note, 0)) < FIRST_PSEUDO_REGISTER
1935 || (!bitmap_bit_p (DF_LR_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
1936 ORIGINAL_REGNO (XEXP (note, 0)))
1937 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] == 1)))
1939 rld[output_reload].reg_rtx
1940 = gen_rtx_REG (rld[output_reload].outmode, regno);
1941 return;
1945 /* Try to find a reload register for an in-out reload (expressions IN and OUT).
1946 See if one of IN and OUT is a register that may be used;
1947 this is desirable since a spill-register won't be needed.
1948 If so, return the register rtx that proves acceptable.
1950 INLOC and OUTLOC are locations where IN and OUT appear in the insn.
1951 RCLASS is the register class required for the reload.
1953 If FOR_REAL is >= 0, it is the number of the reload,
1954 and in some cases when it can be discovered that OUT doesn't need
1955 to be computed, clear out rld[FOR_REAL].out.
1957 If FOR_REAL is -1, this should not be done, because this call
1958 is just to see if a register can be found, not to find and install it.
1960 EARLYCLOBBER is nonzero if OUT is an earlyclobber operand. This
1961 puts an additional constraint on being able to use IN for OUT since
1962 IN must not appear elsewhere in the insn (it is assumed that IN itself
1963 is safe from the earlyclobber). */
1965 static rtx
1966 find_dummy_reload (rtx real_in, rtx real_out, rtx *inloc, rtx *outloc,
1967 enum machine_mode inmode, enum machine_mode outmode,
1968 reg_class_t rclass, int for_real, int earlyclobber)
1970 rtx in = real_in;
1971 rtx out = real_out;
1972 int in_offset = 0;
1973 int out_offset = 0;
1974 rtx value = 0;
1976 /* If operands exceed a word, we can't use either of them
1977 unless they have the same size. */
1978 if (GET_MODE_SIZE (outmode) != GET_MODE_SIZE (inmode)
1979 && (GET_MODE_SIZE (outmode) > UNITS_PER_WORD
1980 || GET_MODE_SIZE (inmode) > UNITS_PER_WORD))
1981 return 0;
1983 /* Note that {in,out}_offset are needed only when 'in' or 'out'
1984 respectively refers to a hard register. */
1986 /* Find the inside of any subregs. */
1987 while (GET_CODE (out) == SUBREG)
1989 if (REG_P (SUBREG_REG (out))
1990 && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER)
1991 out_offset += subreg_regno_offset (REGNO (SUBREG_REG (out)),
1992 GET_MODE (SUBREG_REG (out)),
1993 SUBREG_BYTE (out),
1994 GET_MODE (out));
1995 out = SUBREG_REG (out);
1997 while (GET_CODE (in) == SUBREG)
1999 if (REG_P (SUBREG_REG (in))
2000 && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER)
2001 in_offset += subreg_regno_offset (REGNO (SUBREG_REG (in)),
2002 GET_MODE (SUBREG_REG (in)),
2003 SUBREG_BYTE (in),
2004 GET_MODE (in));
2005 in = SUBREG_REG (in);
2008 /* Narrow down the reg class, the same way push_reload will;
2009 otherwise we might find a dummy now, but push_reload won't. */
2011 reg_class_t preferred_class = targetm.preferred_reload_class (in, rclass);
2012 if (preferred_class != NO_REGS)
2013 rclass = (enum reg_class) preferred_class;
2016 /* See if OUT will do. */
2017 if (REG_P (out)
2018 && REGNO (out) < FIRST_PSEUDO_REGISTER)
2020 unsigned int regno = REGNO (out) + out_offset;
2021 unsigned int nwords = hard_regno_nregs[regno][outmode];
2022 rtx saved_rtx;
2024 /* When we consider whether the insn uses OUT,
2025 ignore references within IN. They don't prevent us
2026 from copying IN into OUT, because those refs would
2027 move into the insn that reloads IN.
2029 However, we only ignore IN in its role as this reload.
2030 If the insn uses IN elsewhere and it contains OUT,
2031 that counts. We can't be sure it's the "same" operand
2032 so it might not go through this reload.
2034 We also need to avoid using OUT if it, or part of it, is a
2035 fixed register. Modifying such registers, even transiently,
2036 may have undefined effects on the machine, such as modifying
2037 the stack pointer. */
2038 saved_rtx = *inloc;
2039 *inloc = const0_rtx;
2041 if (regno < FIRST_PSEUDO_REGISTER
2042 && HARD_REGNO_MODE_OK (regno, outmode)
2043 && ! refers_to_regno_for_reload_p (regno, regno + nwords,
2044 PATTERN (this_insn), outloc))
2046 unsigned int i;
2048 for (i = 0; i < nwords; i++)
2049 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
2050 regno + i)
2051 || fixed_regs[regno + i])
2052 break;
2054 if (i == nwords)
2056 if (REG_P (real_out))
2057 value = real_out;
2058 else
2059 value = gen_rtx_REG (outmode, regno);
2063 *inloc = saved_rtx;
2066 /* Consider using IN if OUT was not acceptable
2067 or if OUT dies in this insn (like the quotient in a divmod insn).
2068 We can't use IN unless it is dies in this insn,
2069 which means we must know accurately which hard regs are live.
2070 Also, the result can't go in IN if IN is used within OUT,
2071 or if OUT is an earlyclobber and IN appears elsewhere in the insn. */
2072 if (hard_regs_live_known
2073 && REG_P (in)
2074 && REGNO (in) < FIRST_PSEUDO_REGISTER
2075 && (value == 0
2076 || find_reg_note (this_insn, REG_UNUSED, real_out))
2077 && find_reg_note (this_insn, REG_DEAD, real_in)
2078 && !fixed_regs[REGNO (in)]
2079 && HARD_REGNO_MODE_OK (REGNO (in),
2080 /* The only case where out and real_out might
2081 have different modes is where real_out
2082 is a subreg, and in that case, out
2083 has a real mode. */
2084 (GET_MODE (out) != VOIDmode
2085 ? GET_MODE (out) : outmode))
2086 && (ORIGINAL_REGNO (in) < FIRST_PSEUDO_REGISTER
2087 /* However only do this if we can be sure that this input
2088 operand doesn't correspond with an uninitialized pseudo.
2089 global can assign some hardreg to it that is the same as
2090 the one assigned to a different, also live pseudo (as it
2091 can ignore the conflict). We must never introduce writes
2092 to such hardregs, as they would clobber the other live
2093 pseudo. See PR 20973. */
2094 || (!bitmap_bit_p (DF_LR_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
2095 ORIGINAL_REGNO (in))
2096 /* Similarly, only do this if we can be sure that the death
2097 note is still valid. global can assign some hardreg to
2098 the pseudo referenced in the note and simultaneously a
2099 subword of this hardreg to a different, also live pseudo,
2100 because only another subword of the hardreg is actually
2101 used in the insn. This cannot happen if the pseudo has
2102 been assigned exactly one hardreg. See PR 33732. */
2103 && hard_regno_nregs[REGNO (in)][GET_MODE (in)] == 1)))
2105 unsigned int regno = REGNO (in) + in_offset;
2106 unsigned int nwords = hard_regno_nregs[regno][inmode];
2108 if (! refers_to_regno_for_reload_p (regno, regno + nwords, out, (rtx*) 0)
2109 && ! hard_reg_set_here_p (regno, regno + nwords,
2110 PATTERN (this_insn))
2111 && (! earlyclobber
2112 || ! refers_to_regno_for_reload_p (regno, regno + nwords,
2113 PATTERN (this_insn), inloc)))
2115 unsigned int i;
2117 for (i = 0; i < nwords; i++)
2118 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
2119 regno + i))
2120 break;
2122 if (i == nwords)
2124 /* If we were going to use OUT as the reload reg
2125 and changed our mind, it means OUT is a dummy that
2126 dies here. So don't bother copying value to it. */
2127 if (for_real >= 0 && value == real_out)
2128 rld[for_real].out = 0;
2129 if (REG_P (real_in))
2130 value = real_in;
2131 else
2132 value = gen_rtx_REG (inmode, regno);
2137 return value;
2140 /* This page contains subroutines used mainly for determining
2141 whether the IN or an OUT of a reload can serve as the
2142 reload register. */
2144 /* Return 1 if X is an operand of an insn that is being earlyclobbered. */
2147 earlyclobber_operand_p (rtx x)
2149 int i;
2151 for (i = 0; i < n_earlyclobbers; i++)
2152 if (reload_earlyclobbers[i] == x)
2153 return 1;
2155 return 0;
2158 /* Return 1 if expression X alters a hard reg in the range
2159 from BEG_REGNO (inclusive) to END_REGNO (exclusive),
2160 either explicitly or in the guise of a pseudo-reg allocated to REGNO.
2161 X should be the body of an instruction. */
2163 static int
2164 hard_reg_set_here_p (unsigned int beg_regno, unsigned int end_regno, rtx x)
2166 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
2168 rtx op0 = SET_DEST (x);
2170 while (GET_CODE (op0) == SUBREG)
2171 op0 = SUBREG_REG (op0);
2172 if (REG_P (op0))
2174 unsigned int r = REGNO (op0);
2176 /* See if this reg overlaps range under consideration. */
2177 if (r < end_regno
2178 && end_hard_regno (GET_MODE (op0), r) > beg_regno)
2179 return 1;
2182 else if (GET_CODE (x) == PARALLEL)
2184 int i = XVECLEN (x, 0) - 1;
2186 for (; i >= 0; i--)
2187 if (hard_reg_set_here_p (beg_regno, end_regno, XVECEXP (x, 0, i)))
2188 return 1;
2191 return 0;
2194 /* Return 1 if ADDR is a valid memory address for mode MODE
2195 in address space AS, and check that each pseudo reg has the
2196 proper kind of hard reg. */
2199 strict_memory_address_addr_space_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2200 rtx addr, addr_space_t as)
2202 #ifdef GO_IF_LEGITIMATE_ADDRESS
2203 gcc_assert (ADDR_SPACE_GENERIC_P (as));
2204 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2205 return 0;
2207 win:
2208 return 1;
2209 #else
2210 return targetm.addr_space.legitimate_address_p (mode, addr, 1, as);
2211 #endif
2214 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
2215 if they are the same hard reg, and has special hacks for
2216 autoincrement and autodecrement.
2217 This is specifically intended for find_reloads to use
2218 in determining whether two operands match.
2219 X is the operand whose number is the lower of the two.
2221 The value is 2 if Y contains a pre-increment that matches
2222 a non-incrementing address in X. */
2224 /* ??? To be completely correct, we should arrange to pass
2225 for X the output operand and for Y the input operand.
2226 For now, we assume that the output operand has the lower number
2227 because that is natural in (SET output (... input ...)). */
2230 operands_match_p (rtx x, rtx y)
2232 int i;
2233 RTX_CODE code = GET_CODE (x);
2234 const char *fmt;
2235 int success_2;
2237 if (x == y)
2238 return 1;
2239 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
2240 && (REG_P (y) || (GET_CODE (y) == SUBREG
2241 && REG_P (SUBREG_REG (y)))))
2243 int j;
2245 if (code == SUBREG)
2247 i = REGNO (SUBREG_REG (x));
2248 if (i >= FIRST_PSEUDO_REGISTER)
2249 goto slow;
2250 i += subreg_regno_offset (REGNO (SUBREG_REG (x)),
2251 GET_MODE (SUBREG_REG (x)),
2252 SUBREG_BYTE (x),
2253 GET_MODE (x));
2255 else
2256 i = REGNO (x);
2258 if (GET_CODE (y) == SUBREG)
2260 j = REGNO (SUBREG_REG (y));
2261 if (j >= FIRST_PSEUDO_REGISTER)
2262 goto slow;
2263 j += subreg_regno_offset (REGNO (SUBREG_REG (y)),
2264 GET_MODE (SUBREG_REG (y)),
2265 SUBREG_BYTE (y),
2266 GET_MODE (y));
2268 else
2269 j = REGNO (y);
2271 /* On a REG_WORDS_BIG_ENDIAN machine, point to the last register of a
2272 multiple hard register group of scalar integer registers, so that
2273 for example (reg:DI 0) and (reg:SI 1) will be considered the same
2274 register. */
2275 if (REG_WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
2276 && SCALAR_INT_MODE_P (GET_MODE (x))
2277 && i < FIRST_PSEUDO_REGISTER)
2278 i += hard_regno_nregs[i][GET_MODE (x)] - 1;
2279 if (REG_WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (y)) > UNITS_PER_WORD
2280 && SCALAR_INT_MODE_P (GET_MODE (y))
2281 && j < FIRST_PSEUDO_REGISTER)
2282 j += hard_regno_nregs[j][GET_MODE (y)] - 1;
2284 return i == j;
2286 /* If two operands must match, because they are really a single
2287 operand of an assembler insn, then two postincrements are invalid
2288 because the assembler insn would increment only once.
2289 On the other hand, a postincrement matches ordinary indexing
2290 if the postincrement is the output operand. */
2291 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
2292 return operands_match_p (XEXP (x, 0), y);
2293 /* Two preincrements are invalid
2294 because the assembler insn would increment only once.
2295 On the other hand, a preincrement matches ordinary indexing
2296 if the preincrement is the input operand.
2297 In this case, return 2, since some callers need to do special
2298 things when this happens. */
2299 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
2300 || GET_CODE (y) == PRE_MODIFY)
2301 return operands_match_p (x, XEXP (y, 0)) ? 2 : 0;
2303 slow:
2305 /* Now we have disposed of all the cases in which different rtx codes
2306 can match. */
2307 if (code != GET_CODE (y))
2308 return 0;
2310 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
2311 if (GET_MODE (x) != GET_MODE (y))
2312 return 0;
2314 /* MEMs referring to different address space are not equivalent. */
2315 if (code == MEM && MEM_ADDR_SPACE (x) != MEM_ADDR_SPACE (y))
2316 return 0;
2318 switch (code)
2320 CASE_CONST_UNIQUE:
2321 return 0;
2323 case LABEL_REF:
2324 return XEXP (x, 0) == XEXP (y, 0);
2325 case SYMBOL_REF:
2326 return XSTR (x, 0) == XSTR (y, 0);
2328 default:
2329 break;
2332 /* Compare the elements. If any pair of corresponding elements
2333 fail to match, return 0 for the whole things. */
2335 success_2 = 0;
2336 fmt = GET_RTX_FORMAT (code);
2337 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2339 int val, j;
2340 switch (fmt[i])
2342 case 'w':
2343 if (XWINT (x, i) != XWINT (y, i))
2344 return 0;
2345 break;
2347 case 'i':
2348 if (XINT (x, i) != XINT (y, i))
2349 return 0;
2350 break;
2352 case 'e':
2353 val = operands_match_p (XEXP (x, i), XEXP (y, i));
2354 if (val == 0)
2355 return 0;
2356 /* If any subexpression returns 2,
2357 we should return 2 if we are successful. */
2358 if (val == 2)
2359 success_2 = 1;
2360 break;
2362 case '0':
2363 break;
2365 case 'E':
2366 if (XVECLEN (x, i) != XVECLEN (y, i))
2367 return 0;
2368 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
2370 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j));
2371 if (val == 0)
2372 return 0;
2373 if (val == 2)
2374 success_2 = 1;
2376 break;
2378 /* It is believed that rtx's at this level will never
2379 contain anything but integers and other rtx's,
2380 except for within LABEL_REFs and SYMBOL_REFs. */
2381 default:
2382 gcc_unreachable ();
2385 return 1 + success_2;
2388 /* Describe the range of registers or memory referenced by X.
2389 If X is a register, set REG_FLAG and put the first register
2390 number into START and the last plus one into END.
2391 If X is a memory reference, put a base address into BASE
2392 and a range of integer offsets into START and END.
2393 If X is pushing on the stack, we can assume it causes no trouble,
2394 so we set the SAFE field. */
2396 static struct decomposition
2397 decompose (rtx x)
2399 struct decomposition val;
2400 int all_const = 0;
2402 memset (&val, 0, sizeof (val));
2404 switch (GET_CODE (x))
2406 case MEM:
2408 rtx base = NULL_RTX, offset = 0;
2409 rtx addr = XEXP (x, 0);
2411 if (GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == PRE_INC
2412 || GET_CODE (addr) == POST_DEC || GET_CODE (addr) == POST_INC)
2414 val.base = XEXP (addr, 0);
2415 val.start = -GET_MODE_SIZE (GET_MODE (x));
2416 val.end = GET_MODE_SIZE (GET_MODE (x));
2417 val.safe = REGNO (val.base) == STACK_POINTER_REGNUM;
2418 return val;
2421 if (GET_CODE (addr) == PRE_MODIFY || GET_CODE (addr) == POST_MODIFY)
2423 if (GET_CODE (XEXP (addr, 1)) == PLUS
2424 && XEXP (addr, 0) == XEXP (XEXP (addr, 1), 0)
2425 && CONSTANT_P (XEXP (XEXP (addr, 1), 1)))
2427 val.base = XEXP (addr, 0);
2428 val.start = -INTVAL (XEXP (XEXP (addr, 1), 1));
2429 val.end = INTVAL (XEXP (XEXP (addr, 1), 1));
2430 val.safe = REGNO (val.base) == STACK_POINTER_REGNUM;
2431 return val;
2435 if (GET_CODE (addr) == CONST)
2437 addr = XEXP (addr, 0);
2438 all_const = 1;
2440 if (GET_CODE (addr) == PLUS)
2442 if (CONSTANT_P (XEXP (addr, 0)))
2444 base = XEXP (addr, 1);
2445 offset = XEXP (addr, 0);
2447 else if (CONSTANT_P (XEXP (addr, 1)))
2449 base = XEXP (addr, 0);
2450 offset = XEXP (addr, 1);
2454 if (offset == 0)
2456 base = addr;
2457 offset = const0_rtx;
2459 if (GET_CODE (offset) == CONST)
2460 offset = XEXP (offset, 0);
2461 if (GET_CODE (offset) == PLUS)
2463 if (CONST_INT_P (XEXP (offset, 0)))
2465 base = gen_rtx_PLUS (GET_MODE (base), base, XEXP (offset, 1));
2466 offset = XEXP (offset, 0);
2468 else if (CONST_INT_P (XEXP (offset, 1)))
2470 base = gen_rtx_PLUS (GET_MODE (base), base, XEXP (offset, 0));
2471 offset = XEXP (offset, 1);
2473 else
2475 base = gen_rtx_PLUS (GET_MODE (base), base, offset);
2476 offset = const0_rtx;
2479 else if (!CONST_INT_P (offset))
2481 base = gen_rtx_PLUS (GET_MODE (base), base, offset);
2482 offset = const0_rtx;
2485 if (all_const && GET_CODE (base) == PLUS)
2486 base = gen_rtx_CONST (GET_MODE (base), base);
2488 gcc_assert (CONST_INT_P (offset));
2490 val.start = INTVAL (offset);
2491 val.end = val.start + GET_MODE_SIZE (GET_MODE (x));
2492 val.base = base;
2494 break;
2496 case REG:
2497 val.reg_flag = 1;
2498 val.start = true_regnum (x);
2499 if (val.start < 0 || val.start >= FIRST_PSEUDO_REGISTER)
2501 /* A pseudo with no hard reg. */
2502 val.start = REGNO (x);
2503 val.end = val.start + 1;
2505 else
2506 /* A hard reg. */
2507 val.end = end_hard_regno (GET_MODE (x), val.start);
2508 break;
2510 case SUBREG:
2511 if (!REG_P (SUBREG_REG (x)))
2512 /* This could be more precise, but it's good enough. */
2513 return decompose (SUBREG_REG (x));
2514 val.reg_flag = 1;
2515 val.start = true_regnum (x);
2516 if (val.start < 0 || val.start >= FIRST_PSEUDO_REGISTER)
2517 return decompose (SUBREG_REG (x));
2518 else
2519 /* A hard reg. */
2520 val.end = val.start + subreg_nregs (x);
2521 break;
2523 case SCRATCH:
2524 /* This hasn't been assigned yet, so it can't conflict yet. */
2525 val.safe = 1;
2526 break;
2528 default:
2529 gcc_assert (CONSTANT_P (x));
2530 val.safe = 1;
2531 break;
2533 return val;
2536 /* Return 1 if altering Y will not modify the value of X.
2537 Y is also described by YDATA, which should be decompose (Y). */
2539 static int
2540 immune_p (rtx x, rtx y, struct decomposition ydata)
2542 struct decomposition xdata;
2544 if (ydata.reg_flag)
2545 return !refers_to_regno_for_reload_p (ydata.start, ydata.end, x, (rtx*) 0);
2546 if (ydata.safe)
2547 return 1;
2549 gcc_assert (MEM_P (y));
2550 /* If Y is memory and X is not, Y can't affect X. */
2551 if (!MEM_P (x))
2552 return 1;
2554 xdata = decompose (x);
2556 if (! rtx_equal_p (xdata.base, ydata.base))
2558 /* If bases are distinct symbolic constants, there is no overlap. */
2559 if (CONSTANT_P (xdata.base) && CONSTANT_P (ydata.base))
2560 return 1;
2561 /* Constants and stack slots never overlap. */
2562 if (CONSTANT_P (xdata.base)
2563 && (ydata.base == frame_pointer_rtx
2564 || ydata.base == hard_frame_pointer_rtx
2565 || ydata.base == stack_pointer_rtx))
2566 return 1;
2567 if (CONSTANT_P (ydata.base)
2568 && (xdata.base == frame_pointer_rtx
2569 || xdata.base == hard_frame_pointer_rtx
2570 || xdata.base == stack_pointer_rtx))
2571 return 1;
2572 /* If either base is variable, we don't know anything. */
2573 return 0;
2576 return (xdata.start >= ydata.end || ydata.start >= xdata.end);
2579 /* Similar, but calls decompose. */
2582 safe_from_earlyclobber (rtx op, rtx clobber)
2584 struct decomposition early_data;
2586 early_data = decompose (clobber);
2587 return immune_p (op, clobber, early_data);
2590 /* Main entry point of this file: search the body of INSN
2591 for values that need reloading and record them with push_reload.
2592 REPLACE nonzero means record also where the values occur
2593 so that subst_reloads can be used.
2595 IND_LEVELS says how many levels of indirection are supported by this
2596 machine; a value of zero means that a memory reference is not a valid
2597 memory address.
2599 LIVE_KNOWN says we have valid information about which hard
2600 regs are live at each point in the program; this is true when
2601 we are called from global_alloc but false when stupid register
2602 allocation has been done.
2604 RELOAD_REG_P if nonzero is a vector indexed by hard reg number
2605 which is nonnegative if the reg has been commandeered for reloading into.
2606 It is copied into STATIC_RELOAD_REG_P and referenced from there
2607 by various subroutines.
2609 Return TRUE if some operands need to be changed, because of swapping
2610 commutative operands, reg_equiv_address substitution, or whatever. */
2613 find_reloads (rtx_insn *insn, int replace, int ind_levels, int live_known,
2614 short *reload_reg_p)
2616 int insn_code_number;
2617 int i, j;
2618 int noperands;
2619 /* These start out as the constraints for the insn
2620 and they are chewed up as we consider alternatives. */
2621 const char *constraints[MAX_RECOG_OPERANDS];
2622 /* These are the preferred classes for an operand, or NO_REGS if it isn't
2623 a register. */
2624 enum reg_class preferred_class[MAX_RECOG_OPERANDS];
2625 char pref_or_nothing[MAX_RECOG_OPERANDS];
2626 /* Nonzero for a MEM operand whose entire address needs a reload.
2627 May be -1 to indicate the entire address may or may not need a reload. */
2628 int address_reloaded[MAX_RECOG_OPERANDS];
2629 /* Nonzero for an address operand that needs to be completely reloaded.
2630 May be -1 to indicate the entire operand may or may not need a reload. */
2631 int address_operand_reloaded[MAX_RECOG_OPERANDS];
2632 /* Value of enum reload_type to use for operand. */
2633 enum reload_type operand_type[MAX_RECOG_OPERANDS];
2634 /* Value of enum reload_type to use within address of operand. */
2635 enum reload_type address_type[MAX_RECOG_OPERANDS];
2636 /* Save the usage of each operand. */
2637 enum reload_usage { RELOAD_READ, RELOAD_READ_WRITE, RELOAD_WRITE } modified[MAX_RECOG_OPERANDS];
2638 int no_input_reloads = 0, no_output_reloads = 0;
2639 int n_alternatives;
2640 reg_class_t this_alternative[MAX_RECOG_OPERANDS];
2641 char this_alternative_match_win[MAX_RECOG_OPERANDS];
2642 char this_alternative_win[MAX_RECOG_OPERANDS];
2643 char this_alternative_offmemok[MAX_RECOG_OPERANDS];
2644 char this_alternative_earlyclobber[MAX_RECOG_OPERANDS];
2645 int this_alternative_matches[MAX_RECOG_OPERANDS];
2646 reg_class_t goal_alternative[MAX_RECOG_OPERANDS];
2647 int this_alternative_number;
2648 int goal_alternative_number = 0;
2649 int operand_reloadnum[MAX_RECOG_OPERANDS];
2650 int goal_alternative_matches[MAX_RECOG_OPERANDS];
2651 int goal_alternative_matched[MAX_RECOG_OPERANDS];
2652 char goal_alternative_match_win[MAX_RECOG_OPERANDS];
2653 char goal_alternative_win[MAX_RECOG_OPERANDS];
2654 char goal_alternative_offmemok[MAX_RECOG_OPERANDS];
2655 char goal_alternative_earlyclobber[MAX_RECOG_OPERANDS];
2656 int goal_alternative_swapped;
2657 int best;
2658 int commutative;
2659 char operands_match[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
2660 rtx substed_operand[MAX_RECOG_OPERANDS];
2661 rtx body = PATTERN (insn);
2662 rtx set = single_set (insn);
2663 int goal_earlyclobber = 0, this_earlyclobber;
2664 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
2665 int retval = 0;
2667 this_insn = insn;
2668 n_reloads = 0;
2669 n_replacements = 0;
2670 n_earlyclobbers = 0;
2671 replace_reloads = replace;
2672 hard_regs_live_known = live_known;
2673 static_reload_reg_p = reload_reg_p;
2675 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output reloads;
2676 neither are insns that SET cc0. Insns that use CC0 are not allowed
2677 to have any input reloads. */
2678 if (JUMP_P (insn) || CALL_P (insn))
2679 no_output_reloads = 1;
2681 #ifdef HAVE_cc0
2682 if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
2683 no_input_reloads = 1;
2684 if (reg_set_p (cc0_rtx, PATTERN (insn)))
2685 no_output_reloads = 1;
2686 #endif
2688 #ifdef SECONDARY_MEMORY_NEEDED
2689 /* The eliminated forms of any secondary memory locations are per-insn, so
2690 clear them out here. */
2692 if (secondary_memlocs_elim_used)
2694 memset (secondary_memlocs_elim, 0,
2695 sizeof (secondary_memlocs_elim[0]) * secondary_memlocs_elim_used);
2696 secondary_memlocs_elim_used = 0;
2698 #endif
2700 /* Dispose quickly of (set (reg..) (reg..)) if both have hard regs and it
2701 is cheap to move between them. If it is not, there may not be an insn
2702 to do the copy, so we may need a reload. */
2703 if (GET_CODE (body) == SET
2704 && REG_P (SET_DEST (body))
2705 && REGNO (SET_DEST (body)) < FIRST_PSEUDO_REGISTER
2706 && REG_P (SET_SRC (body))
2707 && REGNO (SET_SRC (body)) < FIRST_PSEUDO_REGISTER
2708 && register_move_cost (GET_MODE (SET_SRC (body)),
2709 REGNO_REG_CLASS (REGNO (SET_SRC (body))),
2710 REGNO_REG_CLASS (REGNO (SET_DEST (body)))) == 2)
2711 return 0;
2713 extract_insn (insn);
2715 noperands = reload_n_operands = recog_data.n_operands;
2716 n_alternatives = recog_data.n_alternatives;
2718 /* Just return "no reloads" if insn has no operands with constraints. */
2719 if (noperands == 0 || n_alternatives == 0)
2720 return 0;
2722 insn_code_number = INSN_CODE (insn);
2723 this_insn_is_asm = insn_code_number < 0;
2725 memcpy (operand_mode, recog_data.operand_mode,
2726 noperands * sizeof (enum machine_mode));
2727 memcpy (constraints, recog_data.constraints,
2728 noperands * sizeof (const char *));
2730 commutative = -1;
2732 /* If we will need to know, later, whether some pair of operands
2733 are the same, we must compare them now and save the result.
2734 Reloading the base and index registers will clobber them
2735 and afterward they will fail to match. */
2737 for (i = 0; i < noperands; i++)
2739 const char *p;
2740 int c;
2741 char *end;
2743 substed_operand[i] = recog_data.operand[i];
2744 p = constraints[i];
2746 modified[i] = RELOAD_READ;
2748 /* Scan this operand's constraint to see if it is an output operand,
2749 an in-out operand, is commutative, or should match another. */
2751 while ((c = *p))
2753 p += CONSTRAINT_LEN (c, p);
2754 switch (c)
2756 case '=':
2757 modified[i] = RELOAD_WRITE;
2758 break;
2759 case '+':
2760 modified[i] = RELOAD_READ_WRITE;
2761 break;
2762 case '%':
2764 /* The last operand should not be marked commutative. */
2765 gcc_assert (i != noperands - 1);
2767 /* We currently only support one commutative pair of
2768 operands. Some existing asm code currently uses more
2769 than one pair. Previously, that would usually work,
2770 but sometimes it would crash the compiler. We
2771 continue supporting that case as well as we can by
2772 silently ignoring all but the first pair. In the
2773 future we may handle it correctly. */
2774 if (commutative < 0)
2775 commutative = i;
2776 else
2777 gcc_assert (this_insn_is_asm);
2779 break;
2780 /* Use of ISDIGIT is tempting here, but it may get expensive because
2781 of locale support we don't want. */
2782 case '0': case '1': case '2': case '3': case '4':
2783 case '5': case '6': case '7': case '8': case '9':
2785 c = strtoul (p - 1, &end, 10);
2786 p = end;
2788 operands_match[c][i]
2789 = operands_match_p (recog_data.operand[c],
2790 recog_data.operand[i]);
2792 /* An operand may not match itself. */
2793 gcc_assert (c != i);
2795 /* If C can be commuted with C+1, and C might need to match I,
2796 then C+1 might also need to match I. */
2797 if (commutative >= 0)
2799 if (c == commutative || c == commutative + 1)
2801 int other = c + (c == commutative ? 1 : -1);
2802 operands_match[other][i]
2803 = operands_match_p (recog_data.operand[other],
2804 recog_data.operand[i]);
2806 if (i == commutative || i == commutative + 1)
2808 int other = i + (i == commutative ? 1 : -1);
2809 operands_match[c][other]
2810 = operands_match_p (recog_data.operand[c],
2811 recog_data.operand[other]);
2813 /* Note that C is supposed to be less than I.
2814 No need to consider altering both C and I because in
2815 that case we would alter one into the other. */
2822 /* Examine each operand that is a memory reference or memory address
2823 and reload parts of the addresses into index registers.
2824 Also here any references to pseudo regs that didn't get hard regs
2825 but are equivalent to constants get replaced in the insn itself
2826 with those constants. Nobody will ever see them again.
2828 Finally, set up the preferred classes of each operand. */
2830 for (i = 0; i < noperands; i++)
2832 RTX_CODE code = GET_CODE (recog_data.operand[i]);
2834 address_reloaded[i] = 0;
2835 address_operand_reloaded[i] = 0;
2836 operand_type[i] = (modified[i] == RELOAD_READ ? RELOAD_FOR_INPUT
2837 : modified[i] == RELOAD_WRITE ? RELOAD_FOR_OUTPUT
2838 : RELOAD_OTHER);
2839 address_type[i]
2840 = (modified[i] == RELOAD_READ ? RELOAD_FOR_INPUT_ADDRESS
2841 : modified[i] == RELOAD_WRITE ? RELOAD_FOR_OUTPUT_ADDRESS
2842 : RELOAD_OTHER);
2844 if (*constraints[i] == 0)
2845 /* Ignore things like match_operator operands. */
2847 else if (insn_extra_address_constraint
2848 (lookup_constraint (constraints[i])))
2850 address_operand_reloaded[i]
2851 = find_reloads_address (recog_data.operand_mode[i], (rtx*) 0,
2852 recog_data.operand[i],
2853 recog_data.operand_loc[i],
2854 i, operand_type[i], ind_levels, insn);
2856 /* If we now have a simple operand where we used to have a
2857 PLUS or MULT, re-recognize and try again. */
2858 if ((OBJECT_P (*recog_data.operand_loc[i])
2859 || GET_CODE (*recog_data.operand_loc[i]) == SUBREG)
2860 && (GET_CODE (recog_data.operand[i]) == MULT
2861 || GET_CODE (recog_data.operand[i]) == PLUS))
2863 INSN_CODE (insn) = -1;
2864 retval = find_reloads (insn, replace, ind_levels, live_known,
2865 reload_reg_p);
2866 return retval;
2869 recog_data.operand[i] = *recog_data.operand_loc[i];
2870 substed_operand[i] = recog_data.operand[i];
2872 /* Address operands are reloaded in their existing mode,
2873 no matter what is specified in the machine description. */
2874 operand_mode[i] = GET_MODE (recog_data.operand[i]);
2876 /* If the address is a single CONST_INT pick address mode
2877 instead otherwise we will later not know in which mode
2878 the reload should be performed. */
2879 if (operand_mode[i] == VOIDmode)
2880 operand_mode[i] = Pmode;
2883 else if (code == MEM)
2885 address_reloaded[i]
2886 = find_reloads_address (GET_MODE (recog_data.operand[i]),
2887 recog_data.operand_loc[i],
2888 XEXP (recog_data.operand[i], 0),
2889 &XEXP (recog_data.operand[i], 0),
2890 i, address_type[i], ind_levels, insn);
2891 recog_data.operand[i] = *recog_data.operand_loc[i];
2892 substed_operand[i] = recog_data.operand[i];
2894 else if (code == SUBREG)
2896 rtx reg = SUBREG_REG (recog_data.operand[i]);
2897 rtx op
2898 = find_reloads_toplev (recog_data.operand[i], i, address_type[i],
2899 ind_levels,
2900 set != 0
2901 && &SET_DEST (set) == recog_data.operand_loc[i],
2902 insn,
2903 &address_reloaded[i]);
2905 /* If we made a MEM to load (a part of) the stackslot of a pseudo
2906 that didn't get a hard register, emit a USE with a REG_EQUAL
2907 note in front so that we might inherit a previous, possibly
2908 wider reload. */
2910 if (replace
2911 && MEM_P (op)
2912 && REG_P (reg)
2913 && (GET_MODE_SIZE (GET_MODE (reg))
2914 >= GET_MODE_SIZE (GET_MODE (op)))
2915 && reg_equiv_constant (REGNO (reg)) == 0)
2916 set_unique_reg_note (emit_insn_before (gen_rtx_USE (VOIDmode, reg),
2917 insn),
2918 REG_EQUAL, reg_equiv_memory_loc (REGNO (reg)));
2920 substed_operand[i] = recog_data.operand[i] = op;
2922 else if (code == PLUS || GET_RTX_CLASS (code) == RTX_UNARY)
2923 /* We can get a PLUS as an "operand" as a result of register
2924 elimination. See eliminate_regs and gen_reload. We handle
2925 a unary operator by reloading the operand. */
2926 substed_operand[i] = recog_data.operand[i]
2927 = find_reloads_toplev (recog_data.operand[i], i, address_type[i],
2928 ind_levels, 0, insn,
2929 &address_reloaded[i]);
2930 else if (code == REG)
2932 /* This is equivalent to calling find_reloads_toplev.
2933 The code is duplicated for speed.
2934 When we find a pseudo always equivalent to a constant,
2935 we replace it by the constant. We must be sure, however,
2936 that we don't try to replace it in the insn in which it
2937 is being set. */
2938 int regno = REGNO (recog_data.operand[i]);
2939 if (reg_equiv_constant (regno) != 0
2940 && (set == 0 || &SET_DEST (set) != recog_data.operand_loc[i]))
2942 /* Record the existing mode so that the check if constants are
2943 allowed will work when operand_mode isn't specified. */
2945 if (operand_mode[i] == VOIDmode)
2946 operand_mode[i] = GET_MODE (recog_data.operand[i]);
2948 substed_operand[i] = recog_data.operand[i]
2949 = reg_equiv_constant (regno);
2951 if (reg_equiv_memory_loc (regno) != 0
2952 && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
2953 /* We need not give a valid is_set_dest argument since the case
2954 of a constant equivalence was checked above. */
2955 substed_operand[i] = recog_data.operand[i]
2956 = find_reloads_toplev (recog_data.operand[i], i, address_type[i],
2957 ind_levels, 0, insn,
2958 &address_reloaded[i]);
2960 /* If the operand is still a register (we didn't replace it with an
2961 equivalent), get the preferred class to reload it into. */
2962 code = GET_CODE (recog_data.operand[i]);
2963 preferred_class[i]
2964 = ((code == REG && REGNO (recog_data.operand[i])
2965 >= FIRST_PSEUDO_REGISTER)
2966 ? reg_preferred_class (REGNO (recog_data.operand[i]))
2967 : NO_REGS);
2968 pref_or_nothing[i]
2969 = (code == REG
2970 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER
2971 && reg_alternate_class (REGNO (recog_data.operand[i])) == NO_REGS);
2974 /* If this is simply a copy from operand 1 to operand 0, merge the
2975 preferred classes for the operands. */
2976 if (set != 0 && noperands >= 2 && recog_data.operand[0] == SET_DEST (set)
2977 && recog_data.operand[1] == SET_SRC (set))
2979 preferred_class[0] = preferred_class[1]
2980 = reg_class_subunion[(int) preferred_class[0]][(int) preferred_class[1]];
2981 pref_or_nothing[0] |= pref_or_nothing[1];
2982 pref_or_nothing[1] |= pref_or_nothing[0];
2985 /* Now see what we need for pseudo-regs that didn't get hard regs
2986 or got the wrong kind of hard reg. For this, we must consider
2987 all the operands together against the register constraints. */
2989 best = MAX_RECOG_OPERANDS * 2 + 600;
2991 goal_alternative_swapped = 0;
2993 /* The constraints are made of several alternatives.
2994 Each operand's constraint looks like foo,bar,... with commas
2995 separating the alternatives. The first alternatives for all
2996 operands go together, the second alternatives go together, etc.
2998 First loop over alternatives. */
3000 for (this_alternative_number = 0;
3001 this_alternative_number < n_alternatives;
3002 this_alternative_number++)
3004 int swapped;
3006 if (!TEST_BIT (recog_data.enabled_alternatives, this_alternative_number))
3008 int i;
3010 for (i = 0; i < recog_data.n_operands; i++)
3011 constraints[i] = skip_alternative (constraints[i]);
3013 continue;
3016 /* If insn is commutative (it's safe to exchange a certain pair
3017 of operands) then we need to try each alternative twice, the
3018 second time matching those two operands as if we had
3019 exchanged them. To do this, really exchange them in
3020 operands. */
3021 for (swapped = 0; swapped < (commutative >= 0 ? 2 : 1); swapped++)
3023 /* Loop over operands for one constraint alternative. */
3024 /* LOSERS counts those that don't fit this alternative
3025 and would require loading. */
3026 int losers = 0;
3027 /* BAD is set to 1 if it some operand can't fit this alternative
3028 even after reloading. */
3029 int bad = 0;
3030 /* REJECT is a count of how undesirable this alternative says it is
3031 if any reloading is required. If the alternative matches exactly
3032 then REJECT is ignored, but otherwise it gets this much
3033 counted against it in addition to the reloading needed. Each
3034 ? counts three times here since we want the disparaging caused by
3035 a bad register class to only count 1/3 as much. */
3036 int reject = 0;
3038 if (swapped)
3040 enum reg_class tclass;
3041 int t;
3043 recog_data.operand[commutative] = substed_operand[commutative + 1];
3044 recog_data.operand[commutative + 1] = substed_operand[commutative];
3045 /* Swap the duplicates too. */
3046 for (i = 0; i < recog_data.n_dups; i++)
3047 if (recog_data.dup_num[i] == commutative
3048 || recog_data.dup_num[i] == commutative + 1)
3049 *recog_data.dup_loc[i]
3050 = recog_data.operand[(int) recog_data.dup_num[i]];
3052 tclass = preferred_class[commutative];
3053 preferred_class[commutative] = preferred_class[commutative + 1];
3054 preferred_class[commutative + 1] = tclass;
3056 t = pref_or_nothing[commutative];
3057 pref_or_nothing[commutative] = pref_or_nothing[commutative + 1];
3058 pref_or_nothing[commutative + 1] = t;
3060 t = address_reloaded[commutative];
3061 address_reloaded[commutative] = address_reloaded[commutative + 1];
3062 address_reloaded[commutative + 1] = t;
3065 this_earlyclobber = 0;
3067 for (i = 0; i < noperands; i++)
3069 const char *p = constraints[i];
3070 char *end;
3071 int len;
3072 int win = 0;
3073 int did_match = 0;
3074 /* 0 => this operand can be reloaded somehow for this alternative. */
3075 int badop = 1;
3076 /* 0 => this operand can be reloaded if the alternative allows regs. */
3077 int winreg = 0;
3078 int c;
3079 int m;
3080 rtx operand = recog_data.operand[i];
3081 int offset = 0;
3082 /* Nonzero means this is a MEM that must be reloaded into a reg
3083 regardless of what the constraint says. */
3084 int force_reload = 0;
3085 int offmemok = 0;
3086 /* Nonzero if a constant forced into memory would be OK for this
3087 operand. */
3088 int constmemok = 0;
3089 int earlyclobber = 0;
3090 enum constraint_num cn;
3091 enum reg_class cl;
3093 /* If the predicate accepts a unary operator, it means that
3094 we need to reload the operand, but do not do this for
3095 match_operator and friends. */
3096 if (UNARY_P (operand) && *p != 0)
3097 operand = XEXP (operand, 0);
3099 /* If the operand is a SUBREG, extract
3100 the REG or MEM (or maybe even a constant) within.
3101 (Constants can occur as a result of reg_equiv_constant.) */
3103 while (GET_CODE (operand) == SUBREG)
3105 /* Offset only matters when operand is a REG and
3106 it is a hard reg. This is because it is passed
3107 to reg_fits_class_p if it is a REG and all pseudos
3108 return 0 from that function. */
3109 if (REG_P (SUBREG_REG (operand))
3110 && REGNO (SUBREG_REG (operand)) < FIRST_PSEUDO_REGISTER)
3112 if (simplify_subreg_regno (REGNO (SUBREG_REG (operand)),
3113 GET_MODE (SUBREG_REG (operand)),
3114 SUBREG_BYTE (operand),
3115 GET_MODE (operand)) < 0)
3116 force_reload = 1;
3117 offset += subreg_regno_offset (REGNO (SUBREG_REG (operand)),
3118 GET_MODE (SUBREG_REG (operand)),
3119 SUBREG_BYTE (operand),
3120 GET_MODE (operand));
3122 operand = SUBREG_REG (operand);
3123 /* Force reload if this is a constant or PLUS or if there may
3124 be a problem accessing OPERAND in the outer mode. */
3125 if (CONSTANT_P (operand)
3126 || GET_CODE (operand) == PLUS
3127 /* We must force a reload of paradoxical SUBREGs
3128 of a MEM because the alignment of the inner value
3129 may not be enough to do the outer reference. On
3130 big-endian machines, it may also reference outside
3131 the object.
3133 On machines that extend byte operations and we have a
3134 SUBREG where both the inner and outer modes are no wider
3135 than a word and the inner mode is narrower, is integral,
3136 and gets extended when loaded from memory, combine.c has
3137 made assumptions about the behavior of the machine in such
3138 register access. If the data is, in fact, in memory we
3139 must always load using the size assumed to be in the
3140 register and let the insn do the different-sized
3141 accesses.
3143 This is doubly true if WORD_REGISTER_OPERATIONS. In
3144 this case eliminate_regs has left non-paradoxical
3145 subregs for push_reload to see. Make sure it does
3146 by forcing the reload.
3148 ??? When is it right at this stage to have a subreg
3149 of a mem that is _not_ to be handled specially? IMO
3150 those should have been reduced to just a mem. */
3151 || ((MEM_P (operand)
3152 || (REG_P (operand)
3153 && REGNO (operand) >= FIRST_PSEUDO_REGISTER))
3154 #ifndef WORD_REGISTER_OPERATIONS
3155 && (((GET_MODE_BITSIZE (GET_MODE (operand))
3156 < BIGGEST_ALIGNMENT)
3157 && (GET_MODE_SIZE (operand_mode[i])
3158 > GET_MODE_SIZE (GET_MODE (operand))))
3159 || BYTES_BIG_ENDIAN
3160 #ifdef LOAD_EXTEND_OP
3161 || (GET_MODE_SIZE (operand_mode[i]) <= UNITS_PER_WORD
3162 && (GET_MODE_SIZE (GET_MODE (operand))
3163 <= UNITS_PER_WORD)
3164 && (GET_MODE_SIZE (operand_mode[i])
3165 > GET_MODE_SIZE (GET_MODE (operand)))
3166 && INTEGRAL_MODE_P (GET_MODE (operand))
3167 && LOAD_EXTEND_OP (GET_MODE (operand)) != UNKNOWN)
3168 #endif
3170 #endif
3173 force_reload = 1;
3176 this_alternative[i] = NO_REGS;
3177 this_alternative_win[i] = 0;
3178 this_alternative_match_win[i] = 0;
3179 this_alternative_offmemok[i] = 0;
3180 this_alternative_earlyclobber[i] = 0;
3181 this_alternative_matches[i] = -1;
3183 /* An empty constraint or empty alternative
3184 allows anything which matched the pattern. */
3185 if (*p == 0 || *p == ',')
3186 win = 1, badop = 0;
3188 /* Scan this alternative's specs for this operand;
3189 set WIN if the operand fits any letter in this alternative.
3190 Otherwise, clear BADOP if this operand could
3191 fit some letter after reloads,
3192 or set WINREG if this operand could fit after reloads
3193 provided the constraint allows some registers. */
3196 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
3198 case '\0':
3199 len = 0;
3200 break;
3201 case ',':
3202 c = '\0';
3203 break;
3205 case '?':
3206 reject += 6;
3207 break;
3209 case '!':
3210 reject = 600;
3211 break;
3213 case '#':
3214 /* Ignore rest of this alternative as far as
3215 reloading is concerned. */
3217 p++;
3218 while (*p && *p != ',');
3219 len = 0;
3220 break;
3222 case '0': case '1': case '2': case '3': case '4':
3223 case '5': case '6': case '7': case '8': case '9':
3224 m = strtoul (p, &end, 10);
3225 p = end;
3226 len = 0;
3228 this_alternative_matches[i] = m;
3229 /* We are supposed to match a previous operand.
3230 If we do, we win if that one did.
3231 If we do not, count both of the operands as losers.
3232 (This is too conservative, since most of the time
3233 only a single reload insn will be needed to make
3234 the two operands win. As a result, this alternative
3235 may be rejected when it is actually desirable.) */
3236 if ((swapped && (m != commutative || i != commutative + 1))
3237 /* If we are matching as if two operands were swapped,
3238 also pretend that operands_match had been computed
3239 with swapped.
3240 But if I is the second of those and C is the first,
3241 don't exchange them, because operands_match is valid
3242 only on one side of its diagonal. */
3243 ? (operands_match
3244 [(m == commutative || m == commutative + 1)
3245 ? 2 * commutative + 1 - m : m]
3246 [(i == commutative || i == commutative + 1)
3247 ? 2 * commutative + 1 - i : i])
3248 : operands_match[m][i])
3250 /* If we are matching a non-offsettable address where an
3251 offsettable address was expected, then we must reject
3252 this combination, because we can't reload it. */
3253 if (this_alternative_offmemok[m]
3254 && MEM_P (recog_data.operand[m])
3255 && this_alternative[m] == NO_REGS
3256 && ! this_alternative_win[m])
3257 bad = 1;
3259 did_match = this_alternative_win[m];
3261 else
3263 /* Operands don't match. */
3264 rtx value;
3265 int loc1, loc2;
3266 /* Retroactively mark the operand we had to match
3267 as a loser, if it wasn't already. */
3268 if (this_alternative_win[m])
3269 losers++;
3270 this_alternative_win[m] = 0;
3271 if (this_alternative[m] == NO_REGS)
3272 bad = 1;
3273 /* But count the pair only once in the total badness of
3274 this alternative, if the pair can be a dummy reload.
3275 The pointers in operand_loc are not swapped; swap
3276 them by hand if necessary. */
3277 if (swapped && i == commutative)
3278 loc1 = commutative + 1;
3279 else if (swapped && i == commutative + 1)
3280 loc1 = commutative;
3281 else
3282 loc1 = i;
3283 if (swapped && m == commutative)
3284 loc2 = commutative + 1;
3285 else if (swapped && m == commutative + 1)
3286 loc2 = commutative;
3287 else
3288 loc2 = m;
3289 value
3290 = find_dummy_reload (recog_data.operand[i],
3291 recog_data.operand[m],
3292 recog_data.operand_loc[loc1],
3293 recog_data.operand_loc[loc2],
3294 operand_mode[i], operand_mode[m],
3295 this_alternative[m], -1,
3296 this_alternative_earlyclobber[m]);
3298 if (value != 0)
3299 losers--;
3301 /* This can be fixed with reloads if the operand
3302 we are supposed to match can be fixed with reloads. */
3303 badop = 0;
3304 this_alternative[i] = this_alternative[m];
3306 /* If we have to reload this operand and some previous
3307 operand also had to match the same thing as this
3308 operand, we don't know how to do that. So reject this
3309 alternative. */
3310 if (! did_match || force_reload)
3311 for (j = 0; j < i; j++)
3312 if (this_alternative_matches[j]
3313 == this_alternative_matches[i])
3315 badop = 1;
3316 break;
3318 break;
3320 case 'p':
3321 /* All necessary reloads for an address_operand
3322 were handled in find_reloads_address. */
3323 this_alternative[i]
3324 = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
3325 ADDRESS, SCRATCH);
3326 win = 1;
3327 badop = 0;
3328 break;
3330 case TARGET_MEM_CONSTRAINT:
3331 if (force_reload)
3332 break;
3333 if (MEM_P (operand)
3334 || (REG_P (operand)
3335 && REGNO (operand) >= FIRST_PSEUDO_REGISTER
3336 && reg_renumber[REGNO (operand)] < 0))
3337 win = 1;
3338 if (CONST_POOL_OK_P (operand_mode[i], operand))
3339 badop = 0;
3340 constmemok = 1;
3341 break;
3343 case '<':
3344 if (MEM_P (operand)
3345 && ! address_reloaded[i]
3346 && (GET_CODE (XEXP (operand, 0)) == PRE_DEC
3347 || GET_CODE (XEXP (operand, 0)) == POST_DEC))
3348 win = 1;
3349 break;
3351 case '>':
3352 if (MEM_P (operand)
3353 && ! address_reloaded[i]
3354 && (GET_CODE (XEXP (operand, 0)) == PRE_INC
3355 || GET_CODE (XEXP (operand, 0)) == POST_INC))
3356 win = 1;
3357 break;
3359 /* Memory operand whose address is not offsettable. */
3360 case 'V':
3361 if (force_reload)
3362 break;
3363 if (MEM_P (operand)
3364 && ! (ind_levels ? offsettable_memref_p (operand)
3365 : offsettable_nonstrict_memref_p (operand))
3366 /* Certain mem addresses will become offsettable
3367 after they themselves are reloaded. This is important;
3368 we don't want our own handling of unoffsettables
3369 to override the handling of reg_equiv_address. */
3370 && !(REG_P (XEXP (operand, 0))
3371 && (ind_levels == 0
3372 || reg_equiv_address (REGNO (XEXP (operand, 0))) != 0)))
3373 win = 1;
3374 break;
3376 /* Memory operand whose address is offsettable. */
3377 case 'o':
3378 if (force_reload)
3379 break;
3380 if ((MEM_P (operand)
3381 /* If IND_LEVELS, find_reloads_address won't reload a
3382 pseudo that didn't get a hard reg, so we have to
3383 reject that case. */
3384 && ((ind_levels ? offsettable_memref_p (operand)
3385 : offsettable_nonstrict_memref_p (operand))
3386 /* A reloaded address is offsettable because it is now
3387 just a simple register indirect. */
3388 || address_reloaded[i] == 1))
3389 || (REG_P (operand)
3390 && REGNO (operand) >= FIRST_PSEUDO_REGISTER
3391 && reg_renumber[REGNO (operand)] < 0
3392 /* If reg_equiv_address is nonzero, we will be
3393 loading it into a register; hence it will be
3394 offsettable, but we cannot say that reg_equiv_mem
3395 is offsettable without checking. */
3396 && ((reg_equiv_mem (REGNO (operand)) != 0
3397 && offsettable_memref_p (reg_equiv_mem (REGNO (operand))))
3398 || (reg_equiv_address (REGNO (operand)) != 0))))
3399 win = 1;
3400 if (CONST_POOL_OK_P (operand_mode[i], operand)
3401 || MEM_P (operand))
3402 badop = 0;
3403 constmemok = 1;
3404 offmemok = 1;
3405 break;
3407 case '&':
3408 /* Output operand that is stored before the need for the
3409 input operands (and their index registers) is over. */
3410 earlyclobber = 1, this_earlyclobber = 1;
3411 break;
3413 case 'X':
3414 force_reload = 0;
3415 win = 1;
3416 break;
3418 case 'g':
3419 if (! force_reload
3420 /* A PLUS is never a valid operand, but reload can make
3421 it from a register when eliminating registers. */
3422 && GET_CODE (operand) != PLUS
3423 /* A SCRATCH is not a valid operand. */
3424 && GET_CODE (operand) != SCRATCH
3425 && (! CONSTANT_P (operand)
3426 || ! flag_pic
3427 || LEGITIMATE_PIC_OPERAND_P (operand))
3428 && (GENERAL_REGS == ALL_REGS
3429 || !REG_P (operand)
3430 || (REGNO (operand) >= FIRST_PSEUDO_REGISTER
3431 && reg_renumber[REGNO (operand)] < 0)))
3432 win = 1;
3433 cl = GENERAL_REGS;
3434 goto reg;
3436 default:
3437 cn = lookup_constraint (p);
3438 switch (get_constraint_type (cn))
3440 case CT_REGISTER:
3441 cl = reg_class_for_constraint (cn);
3442 if (cl != NO_REGS)
3443 goto reg;
3444 break;
3446 case CT_CONST_INT:
3447 if (CONST_INT_P (operand)
3448 && (insn_const_int_ok_for_constraint
3449 (INTVAL (operand), cn)))
3450 win = true;
3451 break;
3453 case CT_MEMORY:
3454 if (force_reload)
3455 break;
3456 if (constraint_satisfied_p (operand, cn))
3457 win = 1;
3458 /* If the address was already reloaded,
3459 we win as well. */
3460 else if (MEM_P (operand) && address_reloaded[i] == 1)
3461 win = 1;
3462 /* Likewise if the address will be reloaded because
3463 reg_equiv_address is nonzero. For reg_equiv_mem
3464 we have to check. */
3465 else if (REG_P (operand)
3466 && REGNO (operand) >= FIRST_PSEUDO_REGISTER
3467 && reg_renumber[REGNO (operand)] < 0
3468 && ((reg_equiv_mem (REGNO (operand)) != 0
3469 && (constraint_satisfied_p
3470 (reg_equiv_mem (REGNO (operand)),
3471 cn)))
3472 || (reg_equiv_address (REGNO (operand))
3473 != 0)))
3474 win = 1;
3476 /* If we didn't already win, we can reload
3477 constants via force_const_mem, and other
3478 MEMs by reloading the address like for 'o'. */
3479 if (CONST_POOL_OK_P (operand_mode[i], operand)
3480 || MEM_P (operand))
3481 badop = 0;
3482 constmemok = 1;
3483 offmemok = 1;
3484 break;
3486 case CT_ADDRESS:
3487 if (constraint_satisfied_p (operand, cn))
3488 win = 1;
3490 /* If we didn't already win, we can reload
3491 the address into a base register. */
3492 this_alternative[i]
3493 = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
3494 ADDRESS, SCRATCH);
3495 badop = 0;
3496 break;
3498 case CT_FIXED_FORM:
3499 if (constraint_satisfied_p (operand, cn))
3500 win = 1;
3501 break;
3503 break;
3505 reg:
3506 this_alternative[i]
3507 = reg_class_subunion[this_alternative[i]][cl];
3508 if (GET_MODE (operand) == BLKmode)
3509 break;
3510 winreg = 1;
3511 if (REG_P (operand)
3512 && reg_fits_class_p (operand, this_alternative[i],
3513 offset, GET_MODE (recog_data.operand[i])))
3514 win = 1;
3515 break;
3517 while ((p += len), c);
3519 if (swapped == (commutative >= 0 ? 1 : 0))
3520 constraints[i] = p;
3522 /* If this operand could be handled with a reg,
3523 and some reg is allowed, then this operand can be handled. */
3524 if (winreg && this_alternative[i] != NO_REGS
3525 && (win || !class_only_fixed_regs[this_alternative[i]]))
3526 badop = 0;
3528 /* Record which operands fit this alternative. */
3529 this_alternative_earlyclobber[i] = earlyclobber;
3530 if (win && ! force_reload)
3531 this_alternative_win[i] = 1;
3532 else if (did_match && ! force_reload)
3533 this_alternative_match_win[i] = 1;
3534 else
3536 int const_to_mem = 0;
3538 this_alternative_offmemok[i] = offmemok;
3539 losers++;
3540 if (badop)
3541 bad = 1;
3542 /* Alternative loses if it has no regs for a reg operand. */
3543 if (REG_P (operand)
3544 && this_alternative[i] == NO_REGS
3545 && this_alternative_matches[i] < 0)
3546 bad = 1;
3548 /* If this is a constant that is reloaded into the desired
3549 class by copying it to memory first, count that as another
3550 reload. This is consistent with other code and is
3551 required to avoid choosing another alternative when
3552 the constant is moved into memory by this function on
3553 an early reload pass. Note that the test here is
3554 precisely the same as in the code below that calls
3555 force_const_mem. */
3556 if (CONST_POOL_OK_P (operand_mode[i], operand)
3557 && ((targetm.preferred_reload_class (operand,
3558 this_alternative[i])
3559 == NO_REGS)
3560 || no_input_reloads))
3562 const_to_mem = 1;
3563 if (this_alternative[i] != NO_REGS)
3564 losers++;
3567 /* Alternative loses if it requires a type of reload not
3568 permitted for this insn. We can always reload SCRATCH
3569 and objects with a REG_UNUSED note. */
3570 if (GET_CODE (operand) != SCRATCH
3571 && modified[i] != RELOAD_READ && no_output_reloads
3572 && ! find_reg_note (insn, REG_UNUSED, operand))
3573 bad = 1;
3574 else if (modified[i] != RELOAD_WRITE && no_input_reloads
3575 && ! const_to_mem)
3576 bad = 1;
3578 /* If we can't reload this value at all, reject this
3579 alternative. Note that we could also lose due to
3580 LIMIT_RELOAD_CLASS, but we don't check that
3581 here. */
3583 if (! CONSTANT_P (operand) && this_alternative[i] != NO_REGS)
3585 if (targetm.preferred_reload_class (operand,
3586 this_alternative[i])
3587 == NO_REGS)
3588 reject = 600;
3590 if (operand_type[i] == RELOAD_FOR_OUTPUT
3591 && (targetm.preferred_output_reload_class (operand,
3592 this_alternative[i])
3593 == NO_REGS))
3594 reject = 600;
3597 /* We prefer to reload pseudos over reloading other things,
3598 since such reloads may be able to be eliminated later.
3599 If we are reloading a SCRATCH, we won't be generating any
3600 insns, just using a register, so it is also preferred.
3601 So bump REJECT in other cases. Don't do this in the
3602 case where we are forcing a constant into memory and
3603 it will then win since we don't want to have a different
3604 alternative match then. */
3605 if (! (REG_P (operand)
3606 && REGNO (operand) >= FIRST_PSEUDO_REGISTER)
3607 && GET_CODE (operand) != SCRATCH
3608 && ! (const_to_mem && constmemok))
3609 reject += 2;
3611 /* Input reloads can be inherited more often than output
3612 reloads can be removed, so penalize output reloads. */
3613 if (operand_type[i] != RELOAD_FOR_INPUT
3614 && GET_CODE (operand) != SCRATCH)
3615 reject++;
3618 /* If this operand is a pseudo register that didn't get
3619 a hard reg and this alternative accepts some
3620 register, see if the class that we want is a subset
3621 of the preferred class for this register. If not,
3622 but it intersects that class, use the preferred class
3623 instead. If it does not intersect the preferred
3624 class, show that usage of this alternative should be
3625 discouraged; it will be discouraged more still if the
3626 register is `preferred or nothing'. We do this
3627 because it increases the chance of reusing our spill
3628 register in a later insn and avoiding a pair of
3629 memory stores and loads.
3631 Don't bother with this if this alternative will
3632 accept this operand.
3634 Don't do this for a multiword operand, since it is
3635 only a small win and has the risk of requiring more
3636 spill registers, which could cause a large loss.
3638 Don't do this if the preferred class has only one
3639 register because we might otherwise exhaust the
3640 class. */
3642 if (! win && ! did_match
3643 && this_alternative[i] != NO_REGS
3644 && GET_MODE_SIZE (operand_mode[i]) <= UNITS_PER_WORD
3645 && reg_class_size [(int) preferred_class[i]] > 0
3646 && ! small_register_class_p (preferred_class[i]))
3648 if (! reg_class_subset_p (this_alternative[i],
3649 preferred_class[i]))
3651 /* Since we don't have a way of forming the intersection,
3652 we just do something special if the preferred class
3653 is a subset of the class we have; that's the most
3654 common case anyway. */
3655 if (reg_class_subset_p (preferred_class[i],
3656 this_alternative[i]))
3657 this_alternative[i] = preferred_class[i];
3658 else
3659 reject += (2 + 2 * pref_or_nothing[i]);
3664 /* Now see if any output operands that are marked "earlyclobber"
3665 in this alternative conflict with any input operands
3666 or any memory addresses. */
3668 for (i = 0; i < noperands; i++)
3669 if (this_alternative_earlyclobber[i]
3670 && (this_alternative_win[i] || this_alternative_match_win[i]))
3672 struct decomposition early_data;
3674 early_data = decompose (recog_data.operand[i]);
3676 gcc_assert (modified[i] != RELOAD_READ);
3678 if (this_alternative[i] == NO_REGS)
3680 this_alternative_earlyclobber[i] = 0;
3681 gcc_assert (this_insn_is_asm);
3682 error_for_asm (this_insn,
3683 "%<&%> constraint used with no register class");
3686 for (j = 0; j < noperands; j++)
3687 /* Is this an input operand or a memory ref? */
3688 if ((MEM_P (recog_data.operand[j])
3689 || modified[j] != RELOAD_WRITE)
3690 && j != i
3691 /* Ignore things like match_operator operands. */
3692 && !recog_data.is_operator[j]
3693 /* Don't count an input operand that is constrained to match
3694 the early clobber operand. */
3695 && ! (this_alternative_matches[j] == i
3696 && rtx_equal_p (recog_data.operand[i],
3697 recog_data.operand[j]))
3698 /* Is it altered by storing the earlyclobber operand? */
3699 && !immune_p (recog_data.operand[j], recog_data.operand[i],
3700 early_data))
3702 /* If the output is in a non-empty few-regs class,
3703 it's costly to reload it, so reload the input instead. */
3704 if (small_register_class_p (this_alternative[i])
3705 && (REG_P (recog_data.operand[j])
3706 || GET_CODE (recog_data.operand[j]) == SUBREG))
3708 losers++;
3709 this_alternative_win[j] = 0;
3710 this_alternative_match_win[j] = 0;
3712 else
3713 break;
3715 /* If an earlyclobber operand conflicts with something,
3716 it must be reloaded, so request this and count the cost. */
3717 if (j != noperands)
3719 losers++;
3720 this_alternative_win[i] = 0;
3721 this_alternative_match_win[j] = 0;
3722 for (j = 0; j < noperands; j++)
3723 if (this_alternative_matches[j] == i
3724 && this_alternative_match_win[j])
3726 this_alternative_win[j] = 0;
3727 this_alternative_match_win[j] = 0;
3728 losers++;
3733 /* If one alternative accepts all the operands, no reload required,
3734 choose that alternative; don't consider the remaining ones. */
3735 if (losers == 0)
3737 /* Unswap these so that they are never swapped at `finish'. */
3738 if (swapped)
3740 recog_data.operand[commutative] = substed_operand[commutative];
3741 recog_data.operand[commutative + 1]
3742 = substed_operand[commutative + 1];
3744 for (i = 0; i < noperands; i++)
3746 goal_alternative_win[i] = this_alternative_win[i];
3747 goal_alternative_match_win[i] = this_alternative_match_win[i];
3748 goal_alternative[i] = this_alternative[i];
3749 goal_alternative_offmemok[i] = this_alternative_offmemok[i];
3750 goal_alternative_matches[i] = this_alternative_matches[i];
3751 goal_alternative_earlyclobber[i]
3752 = this_alternative_earlyclobber[i];
3754 goal_alternative_number = this_alternative_number;
3755 goal_alternative_swapped = swapped;
3756 goal_earlyclobber = this_earlyclobber;
3757 goto finish;
3760 /* REJECT, set by the ! and ? constraint characters and when a register
3761 would be reloaded into a non-preferred class, discourages the use of
3762 this alternative for a reload goal. REJECT is incremented by six
3763 for each ? and two for each non-preferred class. */
3764 losers = losers * 6 + reject;
3766 /* If this alternative can be made to work by reloading,
3767 and it needs less reloading than the others checked so far,
3768 record it as the chosen goal for reloading. */
3769 if (! bad)
3771 if (best > losers)
3773 for (i = 0; i < noperands; i++)
3775 goal_alternative[i] = this_alternative[i];
3776 goal_alternative_win[i] = this_alternative_win[i];
3777 goal_alternative_match_win[i]
3778 = this_alternative_match_win[i];
3779 goal_alternative_offmemok[i]
3780 = this_alternative_offmemok[i];
3781 goal_alternative_matches[i] = this_alternative_matches[i];
3782 goal_alternative_earlyclobber[i]
3783 = this_alternative_earlyclobber[i];
3785 goal_alternative_swapped = swapped;
3786 best = losers;
3787 goal_alternative_number = this_alternative_number;
3788 goal_earlyclobber = this_earlyclobber;
3792 if (swapped)
3794 enum reg_class tclass;
3795 int t;
3797 /* If the commutative operands have been swapped, swap
3798 them back in order to check the next alternative. */
3799 recog_data.operand[commutative] = substed_operand[commutative];
3800 recog_data.operand[commutative + 1] = substed_operand[commutative + 1];
3801 /* Unswap the duplicates too. */
3802 for (i = 0; i < recog_data.n_dups; i++)
3803 if (recog_data.dup_num[i] == commutative
3804 || recog_data.dup_num[i] == commutative + 1)
3805 *recog_data.dup_loc[i]
3806 = recog_data.operand[(int) recog_data.dup_num[i]];
3808 /* Unswap the operand related information as well. */
3809 tclass = preferred_class[commutative];
3810 preferred_class[commutative] = preferred_class[commutative + 1];
3811 preferred_class[commutative + 1] = tclass;
3813 t = pref_or_nothing[commutative];
3814 pref_or_nothing[commutative] = pref_or_nothing[commutative + 1];
3815 pref_or_nothing[commutative + 1] = t;
3817 t = address_reloaded[commutative];
3818 address_reloaded[commutative] = address_reloaded[commutative + 1];
3819 address_reloaded[commutative + 1] = t;
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 rtx tem;
3870 tem = substed_operand[commutative];
3871 substed_operand[commutative] = substed_operand[commutative + 1];
3872 substed_operand[commutative + 1] = tem;
3873 tem = recog_data.operand[commutative];
3874 recog_data.operand[commutative] = recog_data.operand[commutative + 1];
3875 recog_data.operand[commutative + 1] = tem;
3876 tem = *recog_data.operand_loc[commutative];
3877 *recog_data.operand_loc[commutative]
3878 = *recog_data.operand_loc[commutative + 1];
3879 *recog_data.operand_loc[commutative + 1] = tem;
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 enum 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 enum 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 XEXP (substitution, 0))
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 (XEXP (substitution, 0),
4229 insn)))
4231 add_reg_note (insn, REG_LABEL_OPERAND, XEXP (substitution, 0));
4232 if (LABEL_P (XEXP (substitution, 0)))
4233 ++LABEL_NUSES (XEXP (substitution, 0));
4237 else
4238 retval |= (substed_operand[i] != *recog_data.operand_loc[i]);
4241 /* If this insn pattern contains any MATCH_DUP's, make sure that
4242 they will be substituted if the operands they match are substituted.
4243 Also do now any substitutions we already did on the operands.
4245 Don't do this if we aren't making replacements because we might be
4246 propagating things allocated by frame pointer elimination into places
4247 it doesn't expect. */
4249 if (insn_code_number >= 0 && replace)
4250 for (i = insn_data[insn_code_number].n_dups - 1; i >= 0; i--)
4252 int opno = recog_data.dup_num[i];
4253 *recog_data.dup_loc[i] = *recog_data.operand_loc[opno];
4254 dup_replacements (recog_data.dup_loc[i], recog_data.operand_loc[opno]);
4257 #if 0
4258 /* This loses because reloading of prior insns can invalidate the equivalence
4259 (or at least find_equiv_reg isn't smart enough to find it any more),
4260 causing this insn to need more reload regs than it needed before.
4261 It may be too late to make the reload regs available.
4262 Now this optimization is done safely in choose_reload_regs. */
4264 /* For each reload of a reg into some other class of reg,
4265 search for an existing equivalent reg (same value now) in the right class.
4266 We can use it as long as we don't need to change its contents. */
4267 for (i = 0; i < n_reloads; i++)
4268 if (rld[i].reg_rtx == 0
4269 && rld[i].in != 0
4270 && REG_P (rld[i].in)
4271 && rld[i].out == 0)
4273 rld[i].reg_rtx
4274 = find_equiv_reg (rld[i].in, insn, rld[i].rclass, -1,
4275 static_reload_reg_p, 0, rld[i].inmode);
4276 /* Prevent generation of insn to load the value
4277 because the one we found already has the value. */
4278 if (rld[i].reg_rtx)
4279 rld[i].in = rld[i].reg_rtx;
4281 #endif
4283 /* If we detected error and replaced asm instruction by USE, forget about the
4284 reloads. */
4285 if (GET_CODE (PATTERN (insn)) == USE
4286 && CONST_INT_P (XEXP (PATTERN (insn), 0)))
4287 n_reloads = 0;
4289 /* Perhaps an output reload can be combined with another
4290 to reduce needs by one. */
4291 if (!goal_earlyclobber)
4292 combine_reloads ();
4294 /* If we have a pair of reloads for parts of an address, they are reloading
4295 the same object, the operands themselves were not reloaded, and they
4296 are for two operands that are supposed to match, merge the reloads and
4297 change the type of the surviving reload to RELOAD_FOR_OPERAND_ADDRESS. */
4299 for (i = 0; i < n_reloads; i++)
4301 int k;
4303 for (j = i + 1; j < n_reloads; j++)
4304 if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4305 || rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4306 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4307 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4308 && (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
4309 || rld[j].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4310 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4311 || rld[j].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4312 && rtx_equal_p (rld[i].in, rld[j].in)
4313 && (operand_reloadnum[rld[i].opnum] < 0
4314 || rld[operand_reloadnum[rld[i].opnum]].optional)
4315 && (operand_reloadnum[rld[j].opnum] < 0
4316 || rld[operand_reloadnum[rld[j].opnum]].optional)
4317 && (goal_alternative_matches[rld[i].opnum] == rld[j].opnum
4318 || (goal_alternative_matches[rld[j].opnum]
4319 == rld[i].opnum)))
4321 for (k = 0; k < n_replacements; k++)
4322 if (replacements[k].what == j)
4323 replacements[k].what = i;
4325 if (rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4326 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4327 rld[i].when_needed = RELOAD_FOR_OPADDR_ADDR;
4328 else
4329 rld[i].when_needed = RELOAD_FOR_OPERAND_ADDRESS;
4330 rld[j].in = 0;
4334 /* Scan all the reloads and update their type.
4335 If a reload is for the address of an operand and we didn't reload
4336 that operand, change the type. Similarly, change the operand number
4337 of a reload when two operands match. If a reload is optional, treat it
4338 as though the operand isn't reloaded.
4340 ??? This latter case is somewhat odd because if we do the optional
4341 reload, it means the object is hanging around. Thus we need only
4342 do the address reload if the optional reload was NOT done.
4344 Change secondary reloads to be the address type of their operand, not
4345 the normal type.
4347 If an operand's reload is now RELOAD_OTHER, change any
4348 RELOAD_FOR_INPUT_ADDRESS reloads of that operand to
4349 RELOAD_FOR_OTHER_ADDRESS. */
4351 for (i = 0; i < n_reloads; i++)
4353 if (rld[i].secondary_p
4354 && rld[i].when_needed == operand_type[rld[i].opnum])
4355 rld[i].when_needed = address_type[rld[i].opnum];
4357 if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4358 || rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4359 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4360 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4361 && (operand_reloadnum[rld[i].opnum] < 0
4362 || rld[operand_reloadnum[rld[i].opnum]].optional))
4364 /* If we have a secondary reload to go along with this reload,
4365 change its type to RELOAD_FOR_OPADDR_ADDR. */
4367 if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4368 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
4369 && rld[i].secondary_in_reload != -1)
4371 int secondary_in_reload = rld[i].secondary_in_reload;
4373 rld[secondary_in_reload].when_needed = RELOAD_FOR_OPADDR_ADDR;
4375 /* If there's a tertiary reload we have to change it also. */
4376 if (secondary_in_reload > 0
4377 && rld[secondary_in_reload].secondary_in_reload != -1)
4378 rld[rld[secondary_in_reload].secondary_in_reload].when_needed
4379 = RELOAD_FOR_OPADDR_ADDR;
4382 if ((rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4383 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4384 && rld[i].secondary_out_reload != -1)
4386 int secondary_out_reload = rld[i].secondary_out_reload;
4388 rld[secondary_out_reload].when_needed = RELOAD_FOR_OPADDR_ADDR;
4390 /* If there's a tertiary reload we have to change it also. */
4391 if (secondary_out_reload
4392 && rld[secondary_out_reload].secondary_out_reload != -1)
4393 rld[rld[secondary_out_reload].secondary_out_reload].when_needed
4394 = RELOAD_FOR_OPADDR_ADDR;
4397 if (rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4398 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4399 rld[i].when_needed = RELOAD_FOR_OPADDR_ADDR;
4400 else
4401 rld[i].when_needed = RELOAD_FOR_OPERAND_ADDRESS;
4404 if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4405 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
4406 && operand_reloadnum[rld[i].opnum] >= 0
4407 && (rld[operand_reloadnum[rld[i].opnum]].when_needed
4408 == RELOAD_OTHER))
4409 rld[i].when_needed = RELOAD_FOR_OTHER_ADDRESS;
4411 if (goal_alternative_matches[rld[i].opnum] >= 0)
4412 rld[i].opnum = goal_alternative_matches[rld[i].opnum];
4415 /* Scan all the reloads, and check for RELOAD_FOR_OPERAND_ADDRESS reloads.
4416 If we have more than one, then convert all RELOAD_FOR_OPADDR_ADDR
4417 reloads to RELOAD_FOR_OPERAND_ADDRESS reloads.
4419 choose_reload_regs assumes that RELOAD_FOR_OPADDR_ADDR reloads never
4420 conflict with RELOAD_FOR_OPERAND_ADDRESS reloads. This is true for a
4421 single pair of RELOAD_FOR_OPADDR_ADDR/RELOAD_FOR_OPERAND_ADDRESS reloads.
4422 However, if there is more than one RELOAD_FOR_OPERAND_ADDRESS reload,
4423 then a RELOAD_FOR_OPADDR_ADDR reload conflicts with all
4424 RELOAD_FOR_OPERAND_ADDRESS reloads other than the one that uses it.
4425 This is complicated by the fact that a single operand can have more
4426 than one RELOAD_FOR_OPERAND_ADDRESS reload. It is very difficult to fix
4427 choose_reload_regs without affecting code quality, and cases that
4428 actually fail are extremely rare, so it turns out to be better to fix
4429 the problem here by not generating cases that choose_reload_regs will
4430 fail for. */
4431 /* There is a similar problem with RELOAD_FOR_INPUT_ADDRESS /
4432 RELOAD_FOR_OUTPUT_ADDRESS when there is more than one of a kind for
4433 a single operand.
4434 We can reduce the register pressure by exploiting that a
4435 RELOAD_FOR_X_ADDR_ADDR that precedes all RELOAD_FOR_X_ADDRESS reloads
4436 does not conflict with any of them, if it is only used for the first of
4437 the RELOAD_FOR_X_ADDRESS reloads. */
4439 int first_op_addr_num = -2;
4440 int first_inpaddr_num[MAX_RECOG_OPERANDS];
4441 int first_outpaddr_num[MAX_RECOG_OPERANDS];
4442 int need_change = 0;
4443 /* We use last_op_addr_reload and the contents of the above arrays
4444 first as flags - -2 means no instance encountered, -1 means exactly
4445 one instance encountered.
4446 If more than one instance has been encountered, we store the reload
4447 number of the first reload of the kind in question; reload numbers
4448 are known to be non-negative. */
4449 for (i = 0; i < noperands; i++)
4450 first_inpaddr_num[i] = first_outpaddr_num[i] = -2;
4451 for (i = n_reloads - 1; i >= 0; i--)
4453 switch (rld[i].when_needed)
4455 case RELOAD_FOR_OPERAND_ADDRESS:
4456 if (++first_op_addr_num >= 0)
4458 first_op_addr_num = i;
4459 need_change = 1;
4461 break;
4462 case RELOAD_FOR_INPUT_ADDRESS:
4463 if (++first_inpaddr_num[rld[i].opnum] >= 0)
4465 first_inpaddr_num[rld[i].opnum] = i;
4466 need_change = 1;
4468 break;
4469 case RELOAD_FOR_OUTPUT_ADDRESS:
4470 if (++first_outpaddr_num[rld[i].opnum] >= 0)
4472 first_outpaddr_num[rld[i].opnum] = i;
4473 need_change = 1;
4475 break;
4476 default:
4477 break;
4481 if (need_change)
4483 for (i = 0; i < n_reloads; i++)
4485 int first_num;
4486 enum reload_type type;
4488 switch (rld[i].when_needed)
4490 case RELOAD_FOR_OPADDR_ADDR:
4491 first_num = first_op_addr_num;
4492 type = RELOAD_FOR_OPERAND_ADDRESS;
4493 break;
4494 case RELOAD_FOR_INPADDR_ADDRESS:
4495 first_num = first_inpaddr_num[rld[i].opnum];
4496 type = RELOAD_FOR_INPUT_ADDRESS;
4497 break;
4498 case RELOAD_FOR_OUTADDR_ADDRESS:
4499 first_num = first_outpaddr_num[rld[i].opnum];
4500 type = RELOAD_FOR_OUTPUT_ADDRESS;
4501 break;
4502 default:
4503 continue;
4505 if (first_num < 0)
4506 continue;
4507 else if (i > first_num)
4508 rld[i].when_needed = type;
4509 else
4511 /* Check if the only TYPE reload that uses reload I is
4512 reload FIRST_NUM. */
4513 for (j = n_reloads - 1; j > first_num; j--)
4515 if (rld[j].when_needed == type
4516 && (rld[i].secondary_p
4517 ? rld[j].secondary_in_reload == i
4518 : reg_mentioned_p (rld[i].in, rld[j].in)))
4520 rld[i].when_needed = type;
4521 break;
4529 /* See if we have any reloads that are now allowed to be merged
4530 because we've changed when the reload is needed to
4531 RELOAD_FOR_OPERAND_ADDRESS or RELOAD_FOR_OTHER_ADDRESS. Only
4532 check for the most common cases. */
4534 for (i = 0; i < n_reloads; i++)
4535 if (rld[i].in != 0 && rld[i].out == 0
4536 && (rld[i].when_needed == RELOAD_FOR_OPERAND_ADDRESS
4537 || rld[i].when_needed == RELOAD_FOR_OPADDR_ADDR
4538 || rld[i].when_needed == RELOAD_FOR_OTHER_ADDRESS))
4539 for (j = 0; j < n_reloads; j++)
4540 if (i != j && rld[j].in != 0 && rld[j].out == 0
4541 && rld[j].when_needed == rld[i].when_needed
4542 && MATCHES (rld[i].in, rld[j].in)
4543 && rld[i].rclass == rld[j].rclass
4544 && !rld[i].nocombine && !rld[j].nocombine
4545 && rld[i].reg_rtx == rld[j].reg_rtx)
4547 rld[i].opnum = MIN (rld[i].opnum, rld[j].opnum);
4548 transfer_replacements (i, j);
4549 rld[j].in = 0;
4552 #ifdef HAVE_cc0
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 (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);
4561 #endif
4563 /* Compute reload_mode and reload_nregs. */
4564 for (i = 0; i < n_reloads; i++)
4566 rld[i].mode
4567 = (rld[i].inmode == VOIDmode
4568 || (GET_MODE_SIZE (rld[i].outmode)
4569 > GET_MODE_SIZE (rld[i].inmode)))
4570 ? rld[i].outmode : rld[i].inmode;
4572 rld[i].nregs = ira_reg_class_max_nregs [rld[i].rclass][rld[i].mode];
4575 /* Special case a simple move with an input reload and a
4576 destination of a hard reg, if the hard reg is ok, use it. */
4577 for (i = 0; i < n_reloads; i++)
4578 if (rld[i].when_needed == RELOAD_FOR_INPUT
4579 && GET_CODE (PATTERN (insn)) == SET
4580 && REG_P (SET_DEST (PATTERN (insn)))
4581 && (SET_SRC (PATTERN (insn)) == rld[i].in
4582 || SET_SRC (PATTERN (insn)) == rld[i].in_reg)
4583 && !elimination_target_reg_p (SET_DEST (PATTERN (insn))))
4585 rtx dest = SET_DEST (PATTERN (insn));
4586 unsigned int regno = REGNO (dest);
4588 if (regno < FIRST_PSEUDO_REGISTER
4589 && TEST_HARD_REG_BIT (reg_class_contents[rld[i].rclass], regno)
4590 && HARD_REGNO_MODE_OK (regno, rld[i].mode))
4592 int nr = hard_regno_nregs[regno][rld[i].mode];
4593 int ok = 1, nri;
4595 for (nri = 1; nri < nr; nri ++)
4596 if (! TEST_HARD_REG_BIT (reg_class_contents[rld[i].rclass], regno + nri))
4598 ok = 0;
4599 break;
4602 if (ok)
4603 rld[i].reg_rtx = dest;
4607 return retval;
4610 /* Return true if alternative number ALTNUM in constraint-string
4611 CONSTRAINT is guaranteed to accept a reloaded constant-pool reference.
4612 MEM gives the reference if it didn't need any reloads, otherwise it
4613 is null. */
4615 static bool
4616 alternative_allows_const_pool_ref (rtx mem ATTRIBUTE_UNUSED,
4617 const char *constraint, int altnum)
4619 int c;
4621 /* Skip alternatives before the one requested. */
4622 while (altnum > 0)
4624 while (*constraint++ != ',')
4626 altnum--;
4628 /* Scan the requested alternative for TARGET_MEM_CONSTRAINT or 'o'.
4629 If one of them is present, this alternative accepts the result of
4630 passing a constant-pool reference through find_reloads_toplev.
4632 The same is true of extra memory constraints if the address
4633 was reloaded into a register. However, the target may elect
4634 to disallow the original constant address, forcing it to be
4635 reloaded into a register instead. */
4636 for (; (c = *constraint) && c != ',' && c != '#';
4637 constraint += CONSTRAINT_LEN (c, constraint))
4639 enum constraint_num cn = lookup_constraint (constraint);
4640 if (insn_extra_memory_constraint (cn)
4641 && (mem == NULL || constraint_satisfied_p (mem, cn)))
4642 return true;
4644 return false;
4647 /* Scan X for memory references and scan the addresses for reloading.
4648 Also checks for references to "constant" regs that we want to eliminate
4649 and replaces them with the values they stand for.
4650 We may alter X destructively if it contains a reference to such.
4651 If X is just a constant reg, we return the equivalent value
4652 instead of X.
4654 IND_LEVELS says how many levels of indirect addressing this machine
4655 supports.
4657 OPNUM and TYPE identify the purpose of the reload.
4659 IS_SET_DEST is true if X is the destination of a SET, which is not
4660 appropriate to be replaced by a constant.
4662 INSN, if nonzero, is the insn in which we do the reload. It is used
4663 to determine if we may generate output reloads, and where to put USEs
4664 for pseudos that we have to replace with stack slots.
4666 ADDRESS_RELOADED. If nonzero, is a pointer to where we put the
4667 result of find_reloads_address. */
4669 static rtx
4670 find_reloads_toplev (rtx x, int opnum, enum reload_type type,
4671 int ind_levels, int is_set_dest, rtx_insn *insn,
4672 int *address_reloaded)
4674 RTX_CODE code = GET_CODE (x);
4676 const char *fmt = GET_RTX_FORMAT (code);
4677 int i;
4678 int copied;
4680 if (code == REG)
4682 /* This code is duplicated for speed in find_reloads. */
4683 int regno = REGNO (x);
4684 if (reg_equiv_constant (regno) != 0 && !is_set_dest)
4685 x = reg_equiv_constant (regno);
4686 #if 0
4687 /* This creates (subreg (mem...)) which would cause an unnecessary
4688 reload of the mem. */
4689 else if (reg_equiv_mem (regno) != 0)
4690 x = reg_equiv_mem (regno);
4691 #endif
4692 else if (reg_equiv_memory_loc (regno)
4693 && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
4695 rtx mem = make_memloc (x, regno);
4696 if (reg_equiv_address (regno)
4697 || ! rtx_equal_p (mem, reg_equiv_mem (regno)))
4699 /* If this is not a toplevel operand, find_reloads doesn't see
4700 this substitution. We have to emit a USE of the pseudo so
4701 that delete_output_reload can see it. */
4702 if (replace_reloads && recog_data.operand[opnum] != x)
4703 /* We mark the USE with QImode so that we recognize it
4704 as one that can be safely deleted at the end of
4705 reload. */
4706 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, x), insn),
4707 QImode);
4708 x = mem;
4709 i = find_reloads_address (GET_MODE (x), &x, XEXP (x, 0), &XEXP (x, 0),
4710 opnum, type, ind_levels, insn);
4711 if (!rtx_equal_p (x, mem))
4712 push_reg_equiv_alt_mem (regno, x);
4713 if (address_reloaded)
4714 *address_reloaded = i;
4717 return x;
4719 if (code == MEM)
4721 rtx tem = x;
4723 i = find_reloads_address (GET_MODE (x), &tem, XEXP (x, 0), &XEXP (x, 0),
4724 opnum, type, ind_levels, insn);
4725 if (address_reloaded)
4726 *address_reloaded = i;
4728 return tem;
4731 if (code == SUBREG && REG_P (SUBREG_REG (x)))
4733 /* Check for SUBREG containing a REG that's equivalent to a
4734 constant. If the constant has a known value, truncate it
4735 right now. Similarly if we are extracting a single-word of a
4736 multi-word constant. If the constant is symbolic, allow it
4737 to be substituted normally. push_reload will strip the
4738 subreg later. The constant must not be VOIDmode, because we
4739 will lose the mode of the register (this should never happen
4740 because one of the cases above should handle it). */
4742 int regno = REGNO (SUBREG_REG (x));
4743 rtx tem;
4745 if (regno >= FIRST_PSEUDO_REGISTER
4746 && reg_renumber[regno] < 0
4747 && reg_equiv_constant (regno) != 0)
4749 tem =
4750 simplify_gen_subreg (GET_MODE (x), reg_equiv_constant (regno),
4751 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
4752 gcc_assert (tem);
4753 if (CONSTANT_P (tem)
4754 && !targetm.legitimate_constant_p (GET_MODE (x), tem))
4756 tem = force_const_mem (GET_MODE (x), tem);
4757 i = find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
4758 &XEXP (tem, 0), opnum, type,
4759 ind_levels, insn);
4760 if (address_reloaded)
4761 *address_reloaded = i;
4763 return tem;
4766 /* If the subreg contains a reg that will be converted to a mem,
4767 attempt to convert the whole subreg to a (narrower or wider)
4768 memory reference instead. If this succeeds, we're done --
4769 otherwise fall through to check whether the inner reg still
4770 needs address reloads anyway. */
4772 if (regno >= FIRST_PSEUDO_REGISTER
4773 && reg_equiv_memory_loc (regno) != 0)
4775 tem = find_reloads_subreg_address (x, opnum, type, ind_levels,
4776 insn, address_reloaded);
4777 if (tem)
4778 return tem;
4782 for (copied = 0, i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4784 if (fmt[i] == 'e')
4786 rtx new_part = find_reloads_toplev (XEXP (x, i), opnum, type,
4787 ind_levels, is_set_dest, insn,
4788 address_reloaded);
4789 /* If we have replaced a reg with it's equivalent memory loc -
4790 that can still be handled here e.g. if it's in a paradoxical
4791 subreg - we must make the change in a copy, rather than using
4792 a destructive change. This way, find_reloads can still elect
4793 not to do the change. */
4794 if (new_part != XEXP (x, i) && ! CONSTANT_P (new_part) && ! copied)
4796 x = shallow_copy_rtx (x);
4797 copied = 1;
4799 XEXP (x, i) = new_part;
4802 return x;
4805 /* Return a mem ref for the memory equivalent of reg REGNO.
4806 This mem ref is not shared with anything. */
4808 static rtx
4809 make_memloc (rtx ad, int regno)
4811 /* We must rerun eliminate_regs, in case the elimination
4812 offsets have changed. */
4813 rtx tem
4814 = XEXP (eliminate_regs (reg_equiv_memory_loc (regno), VOIDmode, NULL_RTX),
4817 /* If TEM might contain a pseudo, we must copy it to avoid
4818 modifying it when we do the substitution for the reload. */
4819 if (rtx_varies_p (tem, 0))
4820 tem = copy_rtx (tem);
4822 tem = replace_equiv_address_nv (reg_equiv_memory_loc (regno), tem);
4823 tem = adjust_address_nv (tem, GET_MODE (ad), 0);
4825 /* Copy the result if it's still the same as the equivalence, to avoid
4826 modifying it when we do the substitution for the reload. */
4827 if (tem == reg_equiv_memory_loc (regno))
4828 tem = copy_rtx (tem);
4829 return tem;
4832 /* Returns true if AD could be turned into a valid memory reference
4833 to mode MODE in address space AS by reloading the part pointed to
4834 by PART into a register. */
4836 static int
4837 maybe_memory_address_addr_space_p (enum machine_mode mode, rtx ad,
4838 addr_space_t as, rtx *part)
4840 int retv;
4841 rtx tem = *part;
4842 rtx reg = gen_rtx_REG (GET_MODE (tem), max_reg_num ());
4844 *part = reg;
4845 retv = memory_address_addr_space_p (mode, ad, as);
4846 *part = tem;
4848 return retv;
4851 /* Record all reloads needed for handling memory address AD
4852 which appears in *LOC in a memory reference to mode MODE
4853 which itself is found in location *MEMREFLOC.
4854 Note that we take shortcuts assuming that no multi-reg machine mode
4855 occurs as part of an address.
4857 OPNUM and TYPE specify the purpose of this reload.
4859 IND_LEVELS says how many levels of indirect addressing this machine
4860 supports.
4862 INSN, if nonzero, is the insn in which we do the reload. It is used
4863 to determine if we may generate output reloads, and where to put USEs
4864 for pseudos that we have to replace with stack slots.
4866 Value is one if this address is reloaded or replaced as a whole; it is
4867 zero if the top level of this address was not reloaded or replaced, and
4868 it is -1 if it may or may not have been reloaded or replaced.
4870 Note that there is no verification that the address will be valid after
4871 this routine does its work. Instead, we rely on the fact that the address
4872 was valid when reload started. So we need only undo things that reload
4873 could have broken. These are wrong register types, pseudos not allocated
4874 to a hard register, and frame pointer elimination. */
4876 static int
4877 find_reloads_address (enum machine_mode mode, rtx *memrefloc, rtx ad,
4878 rtx *loc, int opnum, enum reload_type type,
4879 int ind_levels, rtx_insn *insn)
4881 addr_space_t as = memrefloc? MEM_ADDR_SPACE (*memrefloc)
4882 : ADDR_SPACE_GENERIC;
4883 int regno;
4884 int removed_and = 0;
4885 int op_index;
4886 rtx tem;
4888 /* If the address is a register, see if it is a legitimate address and
4889 reload if not. We first handle the cases where we need not reload
4890 or where we must reload in a non-standard way. */
4892 if (REG_P (ad))
4894 regno = REGNO (ad);
4896 if (reg_equiv_constant (regno) != 0)
4898 find_reloads_address_part (reg_equiv_constant (regno), loc,
4899 base_reg_class (mode, as, MEM, SCRATCH),
4900 GET_MODE (ad), opnum, type, ind_levels);
4901 return 1;
4904 tem = reg_equiv_memory_loc (regno);
4905 if (tem != 0)
4907 if (reg_equiv_address (regno) != 0 || num_not_at_initial_offset)
4909 tem = make_memloc (ad, regno);
4910 if (! strict_memory_address_addr_space_p (GET_MODE (tem),
4911 XEXP (tem, 0),
4912 MEM_ADDR_SPACE (tem)))
4914 rtx orig = tem;
4916 find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
4917 &XEXP (tem, 0), opnum,
4918 ADDR_TYPE (type), ind_levels, insn);
4919 if (!rtx_equal_p (tem, orig))
4920 push_reg_equiv_alt_mem (regno, tem);
4922 /* We can avoid a reload if the register's equivalent memory
4923 expression is valid as an indirect memory address.
4924 But not all addresses are valid in a mem used as an indirect
4925 address: only reg or reg+constant. */
4927 if (ind_levels > 0
4928 && strict_memory_address_addr_space_p (mode, tem, as)
4929 && (REG_P (XEXP (tem, 0))
4930 || (GET_CODE (XEXP (tem, 0)) == PLUS
4931 && REG_P (XEXP (XEXP (tem, 0), 0))
4932 && CONSTANT_P (XEXP (XEXP (tem, 0), 1)))))
4934 /* TEM is not the same as what we'll be replacing the
4935 pseudo with after reload, put a USE in front of INSN
4936 in the final reload pass. */
4937 if (replace_reloads
4938 && num_not_at_initial_offset
4939 && ! rtx_equal_p (tem, reg_equiv_mem (regno)))
4941 *loc = tem;
4942 /* We mark the USE with QImode so that we
4943 recognize it as one that can be safely
4944 deleted at the end of reload. */
4945 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, ad),
4946 insn), QImode);
4948 /* This doesn't really count as replacing the address
4949 as a whole, since it is still a memory access. */
4951 return 0;
4953 ad = tem;
4957 /* The only remaining case where we can avoid a reload is if this is a
4958 hard register that is valid as a base register and which is not the
4959 subject of a CLOBBER in this insn. */
4961 else if (regno < FIRST_PSEUDO_REGISTER
4962 && regno_ok_for_base_p (regno, mode, as, MEM, SCRATCH)
4963 && ! regno_clobbered_p (regno, this_insn, mode, 0))
4964 return 0;
4966 /* If we do not have one of the cases above, we must do the reload. */
4967 push_reload (ad, NULL_RTX, loc, (rtx*) 0,
4968 base_reg_class (mode, as, MEM, SCRATCH),
4969 GET_MODE (ad), VOIDmode, 0, 0, opnum, type);
4970 return 1;
4973 if (strict_memory_address_addr_space_p (mode, ad, as))
4975 /* The address appears valid, so reloads are not needed.
4976 But the address may contain an eliminable register.
4977 This can happen because a machine with indirect addressing
4978 may consider a pseudo register by itself a valid address even when
4979 it has failed to get a hard reg.
4980 So do a tree-walk to find and eliminate all such regs. */
4982 /* But first quickly dispose of a common case. */
4983 if (GET_CODE (ad) == PLUS
4984 && CONST_INT_P (XEXP (ad, 1))
4985 && REG_P (XEXP (ad, 0))
4986 && reg_equiv_constant (REGNO (XEXP (ad, 0))) == 0)
4987 return 0;
4989 subst_reg_equivs_changed = 0;
4990 *loc = subst_reg_equivs (ad, insn);
4992 if (! subst_reg_equivs_changed)
4993 return 0;
4995 /* Check result for validity after substitution. */
4996 if (strict_memory_address_addr_space_p (mode, ad, as))
4997 return 0;
5000 #ifdef LEGITIMIZE_RELOAD_ADDRESS
5003 if (memrefloc && ADDR_SPACE_GENERIC_P (as))
5005 LEGITIMIZE_RELOAD_ADDRESS (ad, GET_MODE (*memrefloc), opnum, type,
5006 ind_levels, win);
5008 break;
5009 win:
5010 *memrefloc = copy_rtx (*memrefloc);
5011 XEXP (*memrefloc, 0) = ad;
5012 move_replacements (&ad, &XEXP (*memrefloc, 0));
5013 return -1;
5015 while (0);
5016 #endif
5018 /* The address is not valid. We have to figure out why. First see if
5019 we have an outer AND and remove it if so. Then analyze what's inside. */
5021 if (GET_CODE (ad) == AND)
5023 removed_and = 1;
5024 loc = &XEXP (ad, 0);
5025 ad = *loc;
5028 /* One possibility for why the address is invalid is that it is itself
5029 a MEM. This can happen when the frame pointer is being eliminated, a
5030 pseudo is not allocated to a hard register, and the offset between the
5031 frame and stack pointers is not its initial value. In that case the
5032 pseudo will have been replaced by a MEM referring to the
5033 stack pointer. */
5034 if (MEM_P (ad))
5036 /* First ensure that the address in this MEM is valid. Then, unless
5037 indirect addresses are valid, reload the MEM into a register. */
5038 tem = ad;
5039 find_reloads_address (GET_MODE (ad), &tem, XEXP (ad, 0), &XEXP (ad, 0),
5040 opnum, ADDR_TYPE (type),
5041 ind_levels == 0 ? 0 : ind_levels - 1, insn);
5043 /* If tem was changed, then we must create a new memory reference to
5044 hold it and store it back into memrefloc. */
5045 if (tem != ad && memrefloc)
5047 *memrefloc = copy_rtx (*memrefloc);
5048 copy_replacements (tem, XEXP (*memrefloc, 0));
5049 loc = &XEXP (*memrefloc, 0);
5050 if (removed_and)
5051 loc = &XEXP (*loc, 0);
5054 /* Check similar cases as for indirect addresses as above except
5055 that we can allow pseudos and a MEM since they should have been
5056 taken care of above. */
5058 if (ind_levels == 0
5059 || (GET_CODE (XEXP (tem, 0)) == SYMBOL_REF && ! indirect_symref_ok)
5060 || MEM_P (XEXP (tem, 0))
5061 || ! (REG_P (XEXP (tem, 0))
5062 || (GET_CODE (XEXP (tem, 0)) == PLUS
5063 && REG_P (XEXP (XEXP (tem, 0), 0))
5064 && CONST_INT_P (XEXP (XEXP (tem, 0), 1)))))
5066 /* Must use TEM here, not AD, since it is the one that will
5067 have any subexpressions reloaded, if needed. */
5068 push_reload (tem, NULL_RTX, loc, (rtx*) 0,
5069 base_reg_class (mode, as, MEM, SCRATCH), GET_MODE (tem),
5070 VOIDmode, 0,
5071 0, opnum, type);
5072 return ! removed_and;
5074 else
5075 return 0;
5078 /* If we have address of a stack slot but it's not valid because the
5079 displacement is too large, compute the sum in a register.
5080 Handle all base registers here, not just fp/ap/sp, because on some
5081 targets (namely SH) we can also get too large displacements from
5082 big-endian corrections. */
5083 else if (GET_CODE (ad) == PLUS
5084 && REG_P (XEXP (ad, 0))
5085 && REGNO (XEXP (ad, 0)) < FIRST_PSEUDO_REGISTER
5086 && CONST_INT_P (XEXP (ad, 1))
5087 && (regno_ok_for_base_p (REGNO (XEXP (ad, 0)), mode, as, PLUS,
5088 CONST_INT)
5089 /* Similarly, if we were to reload the base register and the
5090 mem+offset address is still invalid, then we want to reload
5091 the whole address, not just the base register. */
5092 || ! maybe_memory_address_addr_space_p
5093 (mode, ad, as, &(XEXP (ad, 0)))))
5096 /* Unshare the MEM rtx so we can safely alter it. */
5097 if (memrefloc)
5099 *memrefloc = copy_rtx (*memrefloc);
5100 loc = &XEXP (*memrefloc, 0);
5101 if (removed_and)
5102 loc = &XEXP (*loc, 0);
5105 if (double_reg_address_ok
5106 && regno_ok_for_base_p (REGNO (XEXP (ad, 0)), mode, as,
5107 PLUS, CONST_INT))
5109 /* Unshare the sum as well. */
5110 *loc = ad = copy_rtx (ad);
5112 /* Reload the displacement into an index reg.
5113 We assume the frame pointer or arg pointer is a base reg. */
5114 find_reloads_address_part (XEXP (ad, 1), &XEXP (ad, 1),
5115 INDEX_REG_CLASS, GET_MODE (ad), opnum,
5116 type, ind_levels);
5117 return 0;
5119 else
5121 /* If the sum of two regs is not necessarily valid,
5122 reload the sum into a base reg.
5123 That will at least work. */
5124 find_reloads_address_part (ad, loc,
5125 base_reg_class (mode, as, MEM, SCRATCH),
5126 GET_MODE (ad), opnum, type, ind_levels);
5128 return ! removed_and;
5131 /* If we have an indexed stack slot, there are three possible reasons why
5132 it might be invalid: The index might need to be reloaded, the address
5133 might have been made by frame pointer elimination and hence have a
5134 constant out of range, or both reasons might apply.
5136 We can easily check for an index needing reload, but even if that is the
5137 case, we might also have an invalid constant. To avoid making the
5138 conservative assumption and requiring two reloads, we see if this address
5139 is valid when not interpreted strictly. If it is, the only problem is
5140 that the index needs a reload and find_reloads_address_1 will take care
5141 of it.
5143 Handle all base registers here, not just fp/ap/sp, because on some
5144 targets (namely SPARC) we can also get invalid addresses from preventive
5145 subreg big-endian corrections made by find_reloads_toplev. We
5146 can also get expressions involving LO_SUM (rather than PLUS) from
5147 find_reloads_subreg_address.
5149 If we decide to do something, it must be that `double_reg_address_ok'
5150 is true. We generate a reload of the base register + constant and
5151 rework the sum so that the reload register will be added to the index.
5152 This is safe because we know the address isn't shared.
5154 We check for the base register as both the first and second operand of
5155 the innermost PLUS and/or LO_SUM. */
5157 for (op_index = 0; op_index < 2; ++op_index)
5159 rtx operand, addend;
5160 enum rtx_code inner_code;
5162 if (GET_CODE (ad) != PLUS)
5163 continue;
5165 inner_code = GET_CODE (XEXP (ad, 0));
5166 if (!(GET_CODE (ad) == PLUS
5167 && CONST_INT_P (XEXP (ad, 1))
5168 && (inner_code == PLUS || inner_code == LO_SUM)))
5169 continue;
5171 operand = XEXP (XEXP (ad, 0), op_index);
5172 if (!REG_P (operand) || REGNO (operand) >= FIRST_PSEUDO_REGISTER)
5173 continue;
5175 addend = XEXP (XEXP (ad, 0), 1 - op_index);
5177 if ((regno_ok_for_base_p (REGNO (operand), mode, as, inner_code,
5178 GET_CODE (addend))
5179 || operand == frame_pointer_rtx
5180 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
5181 || operand == hard_frame_pointer_rtx
5182 #endif
5183 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
5184 || operand == arg_pointer_rtx
5185 #endif
5186 || operand == stack_pointer_rtx)
5187 && ! maybe_memory_address_addr_space_p
5188 (mode, ad, as, &XEXP (XEXP (ad, 0), 1 - op_index)))
5190 rtx offset_reg;
5191 enum reg_class cls;
5193 offset_reg = plus_constant (GET_MODE (ad), operand,
5194 INTVAL (XEXP (ad, 1)));
5196 /* Form the adjusted address. */
5197 if (GET_CODE (XEXP (ad, 0)) == PLUS)
5198 ad = gen_rtx_PLUS (GET_MODE (ad),
5199 op_index == 0 ? offset_reg : addend,
5200 op_index == 0 ? addend : offset_reg);
5201 else
5202 ad = gen_rtx_LO_SUM (GET_MODE (ad),
5203 op_index == 0 ? offset_reg : addend,
5204 op_index == 0 ? addend : offset_reg);
5205 *loc = ad;
5207 cls = base_reg_class (mode, as, MEM, GET_CODE (addend));
5208 find_reloads_address_part (XEXP (ad, op_index),
5209 &XEXP (ad, op_index), cls,
5210 GET_MODE (ad), opnum, type, ind_levels);
5211 find_reloads_address_1 (mode, as,
5212 XEXP (ad, 1 - op_index), 1, GET_CODE (ad),
5213 GET_CODE (XEXP (ad, op_index)),
5214 &XEXP (ad, 1 - op_index), opnum,
5215 type, 0, insn);
5217 return 0;
5221 /* See if address becomes valid when an eliminable register
5222 in a sum is replaced. */
5224 tem = ad;
5225 if (GET_CODE (ad) == PLUS)
5226 tem = subst_indexed_address (ad);
5227 if (tem != ad && strict_memory_address_addr_space_p (mode, tem, as))
5229 /* Ok, we win that way. Replace any additional eliminable
5230 registers. */
5232 subst_reg_equivs_changed = 0;
5233 tem = subst_reg_equivs (tem, insn);
5235 /* Make sure that didn't make the address invalid again. */
5237 if (! subst_reg_equivs_changed
5238 || strict_memory_address_addr_space_p (mode, tem, as))
5240 *loc = tem;
5241 return 0;
5245 /* If constants aren't valid addresses, reload the constant address
5246 into a register. */
5247 if (CONSTANT_P (ad) && ! strict_memory_address_addr_space_p (mode, ad, as))
5249 enum machine_mode address_mode = GET_MODE (ad);
5250 if (address_mode == VOIDmode)
5251 address_mode = targetm.addr_space.address_mode (as);
5253 /* If AD is an address in the constant pool, the MEM rtx may be shared.
5254 Unshare it so we can safely alter it. */
5255 if (memrefloc && GET_CODE (ad) == SYMBOL_REF
5256 && CONSTANT_POOL_ADDRESS_P (ad))
5258 *memrefloc = copy_rtx (*memrefloc);
5259 loc = &XEXP (*memrefloc, 0);
5260 if (removed_and)
5261 loc = &XEXP (*loc, 0);
5264 find_reloads_address_part (ad, loc,
5265 base_reg_class (mode, as, MEM, SCRATCH),
5266 address_mode, opnum, type, ind_levels);
5267 return ! removed_and;
5270 return find_reloads_address_1 (mode, as, ad, 0, MEM, SCRATCH, loc,
5271 opnum, type, ind_levels, insn);
5274 /* Find all pseudo regs appearing in AD
5275 that are eliminable in favor of equivalent values
5276 and do not have hard regs; replace them by their equivalents.
5277 INSN, if nonzero, is the insn in which we do the reload. We put USEs in
5278 front of it for pseudos that we have to replace with stack slots. */
5280 static rtx
5281 subst_reg_equivs (rtx ad, rtx_insn *insn)
5283 RTX_CODE code = GET_CODE (ad);
5284 int i;
5285 const char *fmt;
5287 switch (code)
5289 case HIGH:
5290 case CONST:
5291 CASE_CONST_ANY:
5292 case SYMBOL_REF:
5293 case LABEL_REF:
5294 case PC:
5295 case CC0:
5296 return ad;
5298 case REG:
5300 int regno = REGNO (ad);
5302 if (reg_equiv_constant (regno) != 0)
5304 subst_reg_equivs_changed = 1;
5305 return reg_equiv_constant (regno);
5307 if (reg_equiv_memory_loc (regno) && num_not_at_initial_offset)
5309 rtx mem = make_memloc (ad, regno);
5310 if (! rtx_equal_p (mem, reg_equiv_mem (regno)))
5312 subst_reg_equivs_changed = 1;
5313 /* We mark the USE with QImode so that we recognize it
5314 as one that can be safely deleted at the end of
5315 reload. */
5316 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, ad), insn),
5317 QImode);
5318 return mem;
5322 return ad;
5324 case PLUS:
5325 /* Quickly dispose of a common case. */
5326 if (XEXP (ad, 0) == frame_pointer_rtx
5327 && CONST_INT_P (XEXP (ad, 1)))
5328 return ad;
5329 break;
5331 default:
5332 break;
5335 fmt = GET_RTX_FORMAT (code);
5336 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5337 if (fmt[i] == 'e')
5338 XEXP (ad, i) = subst_reg_equivs (XEXP (ad, i), insn);
5339 return ad;
5342 /* Compute the sum of X and Y, making canonicalizations assumed in an
5343 address, namely: sum constant integers, surround the sum of two
5344 constants with a CONST, put the constant as the second operand, and
5345 group the constant on the outermost sum.
5347 This routine assumes both inputs are already in canonical form. */
5350 form_sum (enum machine_mode mode, rtx x, rtx y)
5352 rtx tem;
5354 gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
5355 gcc_assert (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode);
5357 if (CONST_INT_P (x))
5358 return plus_constant (mode, y, INTVAL (x));
5359 else if (CONST_INT_P (y))
5360 return plus_constant (mode, x, INTVAL (y));
5361 else if (CONSTANT_P (x))
5362 tem = x, x = y, y = tem;
5364 if (GET_CODE (x) == PLUS && CONSTANT_P (XEXP (x, 1)))
5365 return form_sum (mode, XEXP (x, 0), form_sum (mode, XEXP (x, 1), y));
5367 /* Note that if the operands of Y are specified in the opposite
5368 order in the recursive calls below, infinite recursion will occur. */
5369 if (GET_CODE (y) == PLUS && CONSTANT_P (XEXP (y, 1)))
5370 return form_sum (mode, form_sum (mode, x, XEXP (y, 0)), XEXP (y, 1));
5372 /* If both constant, encapsulate sum. Otherwise, just form sum. A
5373 constant will have been placed second. */
5374 if (CONSTANT_P (x) && CONSTANT_P (y))
5376 if (GET_CODE (x) == CONST)
5377 x = XEXP (x, 0);
5378 if (GET_CODE (y) == CONST)
5379 y = XEXP (y, 0);
5381 return gen_rtx_CONST (VOIDmode, gen_rtx_PLUS (mode, x, y));
5384 return gen_rtx_PLUS (mode, x, y);
5387 /* If ADDR is a sum containing a pseudo register that should be
5388 replaced with a constant (from reg_equiv_constant),
5389 return the result of doing so, and also apply the associative
5390 law so that the result is more likely to be a valid address.
5391 (But it is not guaranteed to be one.)
5393 Note that at most one register is replaced, even if more are
5394 replaceable. Also, we try to put the result into a canonical form
5395 so it is more likely to be a valid address.
5397 In all other cases, return ADDR. */
5399 static rtx
5400 subst_indexed_address (rtx addr)
5402 rtx op0 = 0, op1 = 0, op2 = 0;
5403 rtx tem;
5404 int regno;
5406 if (GET_CODE (addr) == PLUS)
5408 /* Try to find a register to replace. */
5409 op0 = XEXP (addr, 0), op1 = XEXP (addr, 1), op2 = 0;
5410 if (REG_P (op0)
5411 && (regno = REGNO (op0)) >= FIRST_PSEUDO_REGISTER
5412 && reg_renumber[regno] < 0
5413 && reg_equiv_constant (regno) != 0)
5414 op0 = reg_equiv_constant (regno);
5415 else if (REG_P (op1)
5416 && (regno = REGNO (op1)) >= FIRST_PSEUDO_REGISTER
5417 && reg_renumber[regno] < 0
5418 && reg_equiv_constant (regno) != 0)
5419 op1 = reg_equiv_constant (regno);
5420 else if (GET_CODE (op0) == PLUS
5421 && (tem = subst_indexed_address (op0)) != op0)
5422 op0 = tem;
5423 else if (GET_CODE (op1) == PLUS
5424 && (tem = subst_indexed_address (op1)) != op1)
5425 op1 = tem;
5426 else
5427 return addr;
5429 /* Pick out up to three things to add. */
5430 if (GET_CODE (op1) == PLUS)
5431 op2 = XEXP (op1, 1), op1 = XEXP (op1, 0);
5432 else if (GET_CODE (op0) == PLUS)
5433 op2 = op1, op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5435 /* Compute the sum. */
5436 if (op2 != 0)
5437 op1 = form_sum (GET_MODE (addr), op1, op2);
5438 if (op1 != 0)
5439 op0 = form_sum (GET_MODE (addr), op0, op1);
5441 return op0;
5443 return addr;
5446 /* Update the REG_INC notes for an insn. It updates all REG_INC
5447 notes for the instruction which refer to REGNO the to refer
5448 to the reload number.
5450 INSN is the insn for which any REG_INC notes need updating.
5452 REGNO is the register number which has been reloaded.
5454 RELOADNUM is the reload number. */
5456 static void
5457 update_auto_inc_notes (rtx_insn *insn ATTRIBUTE_UNUSED, int regno ATTRIBUTE_UNUSED,
5458 int reloadnum ATTRIBUTE_UNUSED)
5460 #ifdef AUTO_INC_DEC
5461 rtx link;
5463 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
5464 if (REG_NOTE_KIND (link) == REG_INC
5465 && (int) REGNO (XEXP (link, 0)) == regno)
5466 push_replacement (&XEXP (link, 0), reloadnum, VOIDmode);
5467 #endif
5470 /* Record the pseudo registers we must reload into hard registers in a
5471 subexpression of a would-be memory address, X referring to a value
5472 in mode MODE. (This function is not called if the address we find
5473 is strictly valid.)
5475 CONTEXT = 1 means we are considering regs as index regs,
5476 = 0 means we are considering them as base regs.
5477 OUTER_CODE is the code of the enclosing RTX, typically a MEM, a PLUS,
5478 or an autoinc code.
5479 If CONTEXT == 0 and OUTER_CODE is a PLUS or LO_SUM, then INDEX_CODE
5480 is the code of the index part of the address. Otherwise, pass SCRATCH
5481 for this argument.
5482 OPNUM and TYPE specify the purpose of any reloads made.
5484 IND_LEVELS says how many levels of indirect addressing are
5485 supported at this point in the address.
5487 INSN, if nonzero, is the insn in which we do the reload. It is used
5488 to determine if we may generate output reloads.
5490 We return nonzero if X, as a whole, is reloaded or replaced. */
5492 /* Note that we take shortcuts assuming that no multi-reg machine mode
5493 occurs as part of an address.
5494 Also, this is not fully machine-customizable; it works for machines
5495 such as VAXen and 68000's and 32000's, but other possible machines
5496 could have addressing modes that this does not handle right.
5497 If you add push_reload calls here, you need to make sure gen_reload
5498 handles those cases gracefully. */
5500 static int
5501 find_reloads_address_1 (enum machine_mode mode, addr_space_t as,
5502 rtx x, int context,
5503 enum rtx_code outer_code, enum rtx_code index_code,
5504 rtx *loc, int opnum, enum reload_type type,
5505 int ind_levels, rtx_insn *insn)
5507 #define REG_OK_FOR_CONTEXT(CONTEXT, REGNO, MODE, AS, OUTER, INDEX) \
5508 ((CONTEXT) == 0 \
5509 ? regno_ok_for_base_p (REGNO, MODE, AS, OUTER, INDEX) \
5510 : REGNO_OK_FOR_INDEX_P (REGNO))
5512 enum reg_class context_reg_class;
5513 RTX_CODE code = GET_CODE (x);
5514 bool reloaded_inner_of_autoinc = false;
5516 if (context == 1)
5517 context_reg_class = INDEX_REG_CLASS;
5518 else
5519 context_reg_class = base_reg_class (mode, as, outer_code, index_code);
5521 switch (code)
5523 case PLUS:
5525 rtx orig_op0 = XEXP (x, 0);
5526 rtx orig_op1 = XEXP (x, 1);
5527 RTX_CODE code0 = GET_CODE (orig_op0);
5528 RTX_CODE code1 = GET_CODE (orig_op1);
5529 rtx op0 = orig_op0;
5530 rtx op1 = orig_op1;
5532 if (GET_CODE (op0) == SUBREG)
5534 op0 = SUBREG_REG (op0);
5535 code0 = GET_CODE (op0);
5536 if (code0 == REG && REGNO (op0) < FIRST_PSEUDO_REGISTER)
5537 op0 = gen_rtx_REG (word_mode,
5538 (REGNO (op0) +
5539 subreg_regno_offset (REGNO (SUBREG_REG (orig_op0)),
5540 GET_MODE (SUBREG_REG (orig_op0)),
5541 SUBREG_BYTE (orig_op0),
5542 GET_MODE (orig_op0))));
5545 if (GET_CODE (op1) == SUBREG)
5547 op1 = SUBREG_REG (op1);
5548 code1 = GET_CODE (op1);
5549 if (code1 == REG && REGNO (op1) < FIRST_PSEUDO_REGISTER)
5550 /* ??? Why is this given op1's mode and above for
5551 ??? op0 SUBREGs we use word_mode? */
5552 op1 = gen_rtx_REG (GET_MODE (op1),
5553 (REGNO (op1) +
5554 subreg_regno_offset (REGNO (SUBREG_REG (orig_op1)),
5555 GET_MODE (SUBREG_REG (orig_op1)),
5556 SUBREG_BYTE (orig_op1),
5557 GET_MODE (orig_op1))));
5559 /* Plus in the index register may be created only as a result of
5560 register rematerialization for expression like &localvar*4. Reload it.
5561 It may be possible to combine the displacement on the outer level,
5562 but it is probably not worthwhile to do so. */
5563 if (context == 1)
5565 find_reloads_address (GET_MODE (x), loc, XEXP (x, 0), &XEXP (x, 0),
5566 opnum, ADDR_TYPE (type), ind_levels, insn);
5567 push_reload (*loc, NULL_RTX, loc, (rtx*) 0,
5568 context_reg_class,
5569 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5570 return 1;
5573 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
5574 || code0 == ZERO_EXTEND || code1 == MEM)
5576 find_reloads_address_1 (mode, as, orig_op0, 1, PLUS, SCRATCH,
5577 &XEXP (x, 0), opnum, type, ind_levels,
5578 insn);
5579 find_reloads_address_1 (mode, as, orig_op1, 0, PLUS, code0,
5580 &XEXP (x, 1), opnum, type, ind_levels,
5581 insn);
5584 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
5585 || code1 == ZERO_EXTEND || code0 == MEM)
5587 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, code1,
5588 &XEXP (x, 0), opnum, type, ind_levels,
5589 insn);
5590 find_reloads_address_1 (mode, as, orig_op1, 1, PLUS, SCRATCH,
5591 &XEXP (x, 1), opnum, type, ind_levels,
5592 insn);
5595 else if (code0 == CONST_INT || code0 == CONST
5596 || code0 == SYMBOL_REF || code0 == LABEL_REF)
5597 find_reloads_address_1 (mode, as, orig_op1, 0, PLUS, code0,
5598 &XEXP (x, 1), opnum, type, ind_levels,
5599 insn);
5601 else if (code1 == CONST_INT || code1 == CONST
5602 || code1 == SYMBOL_REF || code1 == LABEL_REF)
5603 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, code1,
5604 &XEXP (x, 0), opnum, type, ind_levels,
5605 insn);
5607 else if (code0 == REG && code1 == REG)
5609 if (REGNO_OK_FOR_INDEX_P (REGNO (op1))
5610 && regno_ok_for_base_p (REGNO (op0), mode, as, PLUS, REG))
5611 return 0;
5612 else if (REGNO_OK_FOR_INDEX_P (REGNO (op0))
5613 && regno_ok_for_base_p (REGNO (op1), mode, as, PLUS, REG))
5614 return 0;
5615 else if (regno_ok_for_base_p (REGNO (op0), mode, as, PLUS, REG))
5616 find_reloads_address_1 (mode, as, orig_op1, 1, PLUS, SCRATCH,
5617 &XEXP (x, 1), opnum, type, ind_levels,
5618 insn);
5619 else if (REGNO_OK_FOR_INDEX_P (REGNO (op1)))
5620 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, REG,
5621 &XEXP (x, 0), opnum, type, ind_levels,
5622 insn);
5623 else if (regno_ok_for_base_p (REGNO (op1), mode, as, PLUS, REG))
5624 find_reloads_address_1 (mode, as, orig_op0, 1, PLUS, SCRATCH,
5625 &XEXP (x, 0), opnum, type, ind_levels,
5626 insn);
5627 else if (REGNO_OK_FOR_INDEX_P (REGNO (op0)))
5628 find_reloads_address_1 (mode, as, orig_op1, 0, PLUS, REG,
5629 &XEXP (x, 1), opnum, type, ind_levels,
5630 insn);
5631 else
5633 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, REG,
5634 &XEXP (x, 0), opnum, type, ind_levels,
5635 insn);
5636 find_reloads_address_1 (mode, as, orig_op1, 1, PLUS, SCRATCH,
5637 &XEXP (x, 1), opnum, type, ind_levels,
5638 insn);
5642 else if (code0 == REG)
5644 find_reloads_address_1 (mode, as, orig_op0, 1, PLUS, SCRATCH,
5645 &XEXP (x, 0), opnum, type, ind_levels,
5646 insn);
5647 find_reloads_address_1 (mode, as, orig_op1, 0, PLUS, REG,
5648 &XEXP (x, 1), opnum, type, ind_levels,
5649 insn);
5652 else if (code1 == REG)
5654 find_reloads_address_1 (mode, as, orig_op1, 1, PLUS, SCRATCH,
5655 &XEXP (x, 1), opnum, type, ind_levels,
5656 insn);
5657 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, REG,
5658 &XEXP (x, 0), opnum, type, ind_levels,
5659 insn);
5663 return 0;
5665 case POST_MODIFY:
5666 case PRE_MODIFY:
5668 rtx op0 = XEXP (x, 0);
5669 rtx op1 = XEXP (x, 1);
5670 enum rtx_code index_code;
5671 int regno;
5672 int reloadnum;
5674 if (GET_CODE (op1) != PLUS && GET_CODE (op1) != MINUS)
5675 return 0;
5677 /* Currently, we only support {PRE,POST}_MODIFY constructs
5678 where a base register is {inc,dec}remented by the contents
5679 of another register or by a constant value. Thus, these
5680 operands must match. */
5681 gcc_assert (op0 == XEXP (op1, 0));
5683 /* Require index register (or constant). Let's just handle the
5684 register case in the meantime... If the target allows
5685 auto-modify by a constant then we could try replacing a pseudo
5686 register with its equivalent constant where applicable.
5688 We also handle the case where the register was eliminated
5689 resulting in a PLUS subexpression.
5691 If we later decide to reload the whole PRE_MODIFY or
5692 POST_MODIFY, inc_for_reload might clobber the reload register
5693 before reading the index. The index register might therefore
5694 need to live longer than a TYPE reload normally would, so be
5695 conservative and class it as RELOAD_OTHER. */
5696 if ((REG_P (XEXP (op1, 1))
5697 && !REGNO_OK_FOR_INDEX_P (REGNO (XEXP (op1, 1))))
5698 || GET_CODE (XEXP (op1, 1)) == PLUS)
5699 find_reloads_address_1 (mode, as, XEXP (op1, 1), 1, code, SCRATCH,
5700 &XEXP (op1, 1), opnum, RELOAD_OTHER,
5701 ind_levels, insn);
5703 gcc_assert (REG_P (XEXP (op1, 0)));
5705 regno = REGNO (XEXP (op1, 0));
5706 index_code = GET_CODE (XEXP (op1, 1));
5708 /* A register that is incremented cannot be constant! */
5709 gcc_assert (regno < FIRST_PSEUDO_REGISTER
5710 || reg_equiv_constant (regno) == 0);
5712 /* Handle a register that is equivalent to a memory location
5713 which cannot be addressed directly. */
5714 if (reg_equiv_memory_loc (regno) != 0
5715 && (reg_equiv_address (regno) != 0
5716 || num_not_at_initial_offset))
5718 rtx tem = make_memloc (XEXP (x, 0), regno);
5720 if (reg_equiv_address (regno)
5721 || ! rtx_equal_p (tem, reg_equiv_mem (regno)))
5723 rtx orig = tem;
5725 /* First reload the memory location's address.
5726 We can't use ADDR_TYPE (type) here, because we need to
5727 write back the value after reading it, hence we actually
5728 need two registers. */
5729 find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
5730 &XEXP (tem, 0), opnum,
5731 RELOAD_OTHER,
5732 ind_levels, insn);
5734 if (!rtx_equal_p (tem, orig))
5735 push_reg_equiv_alt_mem (regno, tem);
5737 /* Then reload the memory location into a base
5738 register. */
5739 reloadnum = push_reload (tem, tem, &XEXP (x, 0),
5740 &XEXP (op1, 0),
5741 base_reg_class (mode, as,
5742 code, index_code),
5743 GET_MODE (x), GET_MODE (x), 0,
5744 0, opnum, RELOAD_OTHER);
5746 update_auto_inc_notes (this_insn, regno, reloadnum);
5747 return 0;
5751 if (reg_renumber[regno] >= 0)
5752 regno = reg_renumber[regno];
5754 /* We require a base register here... */
5755 if (!regno_ok_for_base_p (regno, GET_MODE (x), as, code, index_code))
5757 reloadnum = push_reload (XEXP (op1, 0), XEXP (x, 0),
5758 &XEXP (op1, 0), &XEXP (x, 0),
5759 base_reg_class (mode, as,
5760 code, index_code),
5761 GET_MODE (x), GET_MODE (x), 0, 0,
5762 opnum, RELOAD_OTHER);
5764 update_auto_inc_notes (this_insn, regno, reloadnum);
5765 return 0;
5768 return 0;
5770 case POST_INC:
5771 case POST_DEC:
5772 case PRE_INC:
5773 case PRE_DEC:
5774 if (REG_P (XEXP (x, 0)))
5776 int regno = REGNO (XEXP (x, 0));
5777 int value = 0;
5778 rtx x_orig = x;
5780 /* A register that is incremented cannot be constant! */
5781 gcc_assert (regno < FIRST_PSEUDO_REGISTER
5782 || reg_equiv_constant (regno) == 0);
5784 /* Handle a register that is equivalent to a memory location
5785 which cannot be addressed directly. */
5786 if (reg_equiv_memory_loc (regno) != 0
5787 && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
5789 rtx tem = make_memloc (XEXP (x, 0), regno);
5790 if (reg_equiv_address (regno)
5791 || ! rtx_equal_p (tem, reg_equiv_mem (regno)))
5793 rtx orig = tem;
5795 /* First reload the memory location's address.
5796 We can't use ADDR_TYPE (type) here, because we need to
5797 write back the value after reading it, hence we actually
5798 need two registers. */
5799 find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
5800 &XEXP (tem, 0), opnum, type,
5801 ind_levels, insn);
5802 reloaded_inner_of_autoinc = true;
5803 if (!rtx_equal_p (tem, orig))
5804 push_reg_equiv_alt_mem (regno, tem);
5805 /* Put this inside a new increment-expression. */
5806 x = gen_rtx_fmt_e (GET_CODE (x), GET_MODE (x), tem);
5807 /* Proceed to reload that, as if it contained a register. */
5811 /* If we have a hard register that is ok in this incdec context,
5812 don't make a reload. If the register isn't nice enough for
5813 autoincdec, we can reload it. But, if an autoincrement of a
5814 register that we here verified as playing nice, still outside
5815 isn't "valid", it must be that no autoincrement is "valid".
5816 If that is true and something made an autoincrement anyway,
5817 this must be a special context where one is allowed.
5818 (For example, a "push" instruction.)
5819 We can't improve this address, so leave it alone. */
5821 /* Otherwise, reload the autoincrement into a suitable hard reg
5822 and record how much to increment by. */
5824 if (reg_renumber[regno] >= 0)
5825 regno = reg_renumber[regno];
5826 if (regno >= FIRST_PSEUDO_REGISTER
5827 || !REG_OK_FOR_CONTEXT (context, regno, mode, as, code,
5828 index_code))
5830 int reloadnum;
5832 /* If we can output the register afterwards, do so, this
5833 saves the extra update.
5834 We can do so if we have an INSN - i.e. no JUMP_INSN nor
5835 CALL_INSN - and it does not set CC0.
5836 But don't do this if we cannot directly address the
5837 memory location, since this will make it harder to
5838 reuse address reloads, and increases register pressure.
5839 Also don't do this if we can probably update x directly. */
5840 rtx equiv = (MEM_P (XEXP (x, 0))
5841 ? XEXP (x, 0)
5842 : reg_equiv_mem (regno));
5843 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
5844 if (insn && NONJUMP_INSN_P (insn) && equiv
5845 && memory_operand (equiv, GET_MODE (equiv))
5846 #ifdef HAVE_cc0
5847 && ! sets_cc0_p (PATTERN (insn))
5848 #endif
5849 && ! (icode != CODE_FOR_nothing
5850 && insn_operand_matches (icode, 0, equiv)
5851 && insn_operand_matches (icode, 1, equiv))
5852 /* Using RELOAD_OTHER means we emit this and the reload we
5853 made earlier in the wrong order. */
5854 && !reloaded_inner_of_autoinc)
5856 /* We use the original pseudo for loc, so that
5857 emit_reload_insns() knows which pseudo this
5858 reload refers to and updates the pseudo rtx, not
5859 its equivalent memory location, as well as the
5860 corresponding entry in reg_last_reload_reg. */
5861 loc = &XEXP (x_orig, 0);
5862 x = XEXP (x, 0);
5863 reloadnum
5864 = push_reload (x, x, loc, loc,
5865 context_reg_class,
5866 GET_MODE (x), GET_MODE (x), 0, 0,
5867 opnum, RELOAD_OTHER);
5869 else
5871 reloadnum
5872 = push_reload (x, x, loc, (rtx*) 0,
5873 context_reg_class,
5874 GET_MODE (x), GET_MODE (x), 0, 0,
5875 opnum, type);
5876 rld[reloadnum].inc
5877 = find_inc_amount (PATTERN (this_insn), XEXP (x_orig, 0));
5879 value = 1;
5882 update_auto_inc_notes (this_insn, REGNO (XEXP (x_orig, 0)),
5883 reloadnum);
5885 return value;
5887 return 0;
5889 case TRUNCATE:
5890 case SIGN_EXTEND:
5891 case ZERO_EXTEND:
5892 /* Look for parts to reload in the inner expression and reload them
5893 too, in addition to this operation. Reloading all inner parts in
5894 addition to this one shouldn't be necessary, but at this point,
5895 we don't know if we can possibly omit any part that *can* be
5896 reloaded. Targets that are better off reloading just either part
5897 (or perhaps even a different part of an outer expression), should
5898 define LEGITIMIZE_RELOAD_ADDRESS. */
5899 find_reloads_address_1 (GET_MODE (XEXP (x, 0)), as, XEXP (x, 0),
5900 context, code, SCRATCH, &XEXP (x, 0), opnum,
5901 type, ind_levels, insn);
5902 push_reload (x, NULL_RTX, loc, (rtx*) 0,
5903 context_reg_class,
5904 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5905 return 1;
5907 case MEM:
5908 /* This is probably the result of a substitution, by eliminate_regs, of
5909 an equivalent address for a pseudo that was not allocated to a hard
5910 register. Verify that the specified address is valid and reload it
5911 into a register.
5913 Since we know we are going to reload this item, don't decrement for
5914 the indirection level.
5916 Note that this is actually conservative: it would be slightly more
5917 efficient to use the value of SPILL_INDIRECT_LEVELS from
5918 reload1.c here. */
5920 find_reloads_address (GET_MODE (x), loc, XEXP (x, 0), &XEXP (x, 0),
5921 opnum, ADDR_TYPE (type), ind_levels, insn);
5922 push_reload (*loc, NULL_RTX, loc, (rtx*) 0,
5923 context_reg_class,
5924 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5925 return 1;
5927 case REG:
5929 int regno = REGNO (x);
5931 if (reg_equiv_constant (regno) != 0)
5933 find_reloads_address_part (reg_equiv_constant (regno), loc,
5934 context_reg_class,
5935 GET_MODE (x), opnum, type, ind_levels);
5936 return 1;
5939 #if 0 /* This might screw code in reload1.c to delete prior output-reload
5940 that feeds this insn. */
5941 if (reg_equiv_mem (regno) != 0)
5943 push_reload (reg_equiv_mem (regno), NULL_RTX, loc, (rtx*) 0,
5944 context_reg_class,
5945 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5946 return 1;
5948 #endif
5950 if (reg_equiv_memory_loc (regno)
5951 && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
5953 rtx tem = make_memloc (x, regno);
5954 if (reg_equiv_address (regno) != 0
5955 || ! rtx_equal_p (tem, reg_equiv_mem (regno)))
5957 x = tem;
5958 find_reloads_address (GET_MODE (x), &x, XEXP (x, 0),
5959 &XEXP (x, 0), opnum, ADDR_TYPE (type),
5960 ind_levels, insn);
5961 if (!rtx_equal_p (x, tem))
5962 push_reg_equiv_alt_mem (regno, x);
5966 if (reg_renumber[regno] >= 0)
5967 regno = reg_renumber[regno];
5969 if (regno >= FIRST_PSEUDO_REGISTER
5970 || !REG_OK_FOR_CONTEXT (context, regno, mode, as, outer_code,
5971 index_code))
5973 push_reload (x, NULL_RTX, loc, (rtx*) 0,
5974 context_reg_class,
5975 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5976 return 1;
5979 /* If a register appearing in an address is the subject of a CLOBBER
5980 in this insn, reload it into some other register to be safe.
5981 The CLOBBER is supposed to make the register unavailable
5982 from before this insn to after it. */
5983 if (regno_clobbered_p (regno, this_insn, GET_MODE (x), 0))
5985 push_reload (x, NULL_RTX, loc, (rtx*) 0,
5986 context_reg_class,
5987 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5988 return 1;
5991 return 0;
5993 case SUBREG:
5994 if (REG_P (SUBREG_REG (x)))
5996 /* If this is a SUBREG of a hard register and the resulting register
5997 is of the wrong class, reload the whole SUBREG. This avoids
5998 needless copies if SUBREG_REG is multi-word. */
5999 if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
6001 int regno ATTRIBUTE_UNUSED = subreg_regno (x);
6003 if (!REG_OK_FOR_CONTEXT (context, regno, mode, as, outer_code,
6004 index_code))
6006 push_reload (x, NULL_RTX, loc, (rtx*) 0,
6007 context_reg_class,
6008 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
6009 return 1;
6012 /* If this is a SUBREG of a pseudo-register, and the pseudo-register
6013 is larger than the class size, then reload the whole SUBREG. */
6014 else
6016 enum reg_class rclass = context_reg_class;
6017 if (ira_reg_class_max_nregs [rclass][GET_MODE (SUBREG_REG (x))]
6018 > reg_class_size[(int) rclass])
6020 /* If the inner register will be replaced by a memory
6021 reference, we can do this only if we can replace the
6022 whole subreg by a (narrower) memory reference. If
6023 this is not possible, fall through and reload just
6024 the inner register (including address reloads). */
6025 if (reg_equiv_memory_loc (REGNO (SUBREG_REG (x))) != 0)
6027 rtx tem = find_reloads_subreg_address (x, opnum,
6028 ADDR_TYPE (type),
6029 ind_levels, insn,
6030 NULL);
6031 if (tem)
6033 push_reload (tem, NULL_RTX, loc, (rtx*) 0, rclass,
6034 GET_MODE (tem), VOIDmode, 0, 0,
6035 opnum, type);
6036 return 1;
6039 else
6041 push_reload (x, NULL_RTX, loc, (rtx*) 0, rclass,
6042 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
6043 return 1;
6048 break;
6050 default:
6051 break;
6055 const char *fmt = GET_RTX_FORMAT (code);
6056 int i;
6058 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6060 if (fmt[i] == 'e')
6061 /* Pass SCRATCH for INDEX_CODE, since CODE can never be a PLUS once
6062 we get here. */
6063 find_reloads_address_1 (mode, as, XEXP (x, i), context,
6064 code, SCRATCH, &XEXP (x, i),
6065 opnum, type, ind_levels, insn);
6069 #undef REG_OK_FOR_CONTEXT
6070 return 0;
6073 /* X, which is found at *LOC, is a part of an address that needs to be
6074 reloaded into a register of class RCLASS. If X is a constant, or if
6075 X is a PLUS that contains a constant, check that the constant is a
6076 legitimate operand and that we are supposed to be able to load
6077 it into the register.
6079 If not, force the constant into memory and reload the MEM instead.
6081 MODE is the mode to use, in case X is an integer constant.
6083 OPNUM and TYPE describe the purpose of any reloads made.
6085 IND_LEVELS says how many levels of indirect addressing this machine
6086 supports. */
6088 static void
6089 find_reloads_address_part (rtx x, rtx *loc, enum reg_class rclass,
6090 enum machine_mode mode, int opnum,
6091 enum reload_type type, int ind_levels)
6093 if (CONSTANT_P (x)
6094 && (!targetm.legitimate_constant_p (mode, x)
6095 || targetm.preferred_reload_class (x, rclass) == NO_REGS))
6097 x = force_const_mem (mode, x);
6098 find_reloads_address (mode, &x, XEXP (x, 0), &XEXP (x, 0),
6099 opnum, type, ind_levels, 0);
6102 else if (GET_CODE (x) == PLUS
6103 && CONSTANT_P (XEXP (x, 1))
6104 && (!targetm.legitimate_constant_p (GET_MODE (x), XEXP (x, 1))
6105 || targetm.preferred_reload_class (XEXP (x, 1), rclass)
6106 == NO_REGS))
6108 rtx tem;
6110 tem = force_const_mem (GET_MODE (x), XEXP (x, 1));
6111 x = gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0), tem);
6112 find_reloads_address (mode, &XEXP (x, 1), XEXP (tem, 0), &XEXP (tem, 0),
6113 opnum, type, ind_levels, 0);
6116 push_reload (x, NULL_RTX, loc, (rtx*) 0, rclass,
6117 mode, VOIDmode, 0, 0, opnum, type);
6120 /* X, a subreg of a pseudo, is a part of an address that needs to be
6121 reloaded, and the pseusdo is equivalent to a memory location.
6123 Attempt to replace the whole subreg by a (possibly narrower or wider)
6124 memory reference. If this is possible, return this new memory
6125 reference, and push all required address reloads. Otherwise,
6126 return NULL.
6128 OPNUM and TYPE identify the purpose of the reload.
6130 IND_LEVELS says how many levels of indirect addressing are
6131 supported at this point in the address.
6133 INSN, if nonzero, is the insn in which we do the reload. It is used
6134 to determine where to put USEs for pseudos that we have to replace with
6135 stack slots. */
6137 static rtx
6138 find_reloads_subreg_address (rtx x, int opnum, enum reload_type type,
6139 int ind_levels, rtx_insn *insn,
6140 int *address_reloaded)
6142 enum machine_mode outer_mode = GET_MODE (x);
6143 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
6144 int regno = REGNO (SUBREG_REG (x));
6145 int reloaded = 0;
6146 rtx tem, orig;
6147 int offset;
6149 gcc_assert (reg_equiv_memory_loc (regno) != 0);
6151 /* We cannot replace the subreg with a modified memory reference if:
6153 - we have a paradoxical subreg that implicitly acts as a zero or
6154 sign extension operation due to LOAD_EXTEND_OP;
6156 - we have a subreg that is implicitly supposed to act on the full
6157 register due to WORD_REGISTER_OPERATIONS (see also eliminate_regs);
6159 - the address of the equivalent memory location is mode-dependent; or
6161 - we have a paradoxical subreg and the resulting memory is not
6162 sufficiently aligned to allow access in the wider mode.
6164 In addition, we choose not to perform the replacement for *any*
6165 paradoxical subreg, even if it were possible in principle. This
6166 is to avoid generating wider memory references than necessary.
6168 This corresponds to how previous versions of reload used to handle
6169 paradoxical subregs where no address reload was required. */
6171 if (paradoxical_subreg_p (x))
6172 return NULL;
6174 #ifdef WORD_REGISTER_OPERATIONS
6175 if (GET_MODE_SIZE (outer_mode) < GET_MODE_SIZE (inner_mode)
6176 && ((GET_MODE_SIZE (outer_mode) - 1) / UNITS_PER_WORD
6177 == (GET_MODE_SIZE (inner_mode) - 1) / UNITS_PER_WORD))
6178 return NULL;
6179 #endif
6181 /* Since we don't attempt to handle paradoxical subregs, we can just
6182 call into simplify_subreg, which will handle all remaining checks
6183 for us. */
6184 orig = make_memloc (SUBREG_REG (x), regno);
6185 offset = SUBREG_BYTE (x);
6186 tem = simplify_subreg (outer_mode, orig, inner_mode, offset);
6187 if (!tem || !MEM_P (tem))
6188 return NULL;
6190 /* Now push all required address reloads, if any. */
6191 reloaded = find_reloads_address (GET_MODE (tem), &tem,
6192 XEXP (tem, 0), &XEXP (tem, 0),
6193 opnum, type, ind_levels, insn);
6194 /* ??? Do we need to handle nonzero offsets somehow? */
6195 if (!offset && !rtx_equal_p (tem, orig))
6196 push_reg_equiv_alt_mem (regno, tem);
6198 /* For some processors an address may be valid in the original mode but
6199 not in a smaller mode. For example, ARM accepts a scaled index register
6200 in SImode but not in HImode. Note that this is only a problem if the
6201 address in reg_equiv_mem is already invalid in the new mode; other
6202 cases would be fixed by find_reloads_address as usual.
6204 ??? We attempt to handle such cases here by doing an additional reload
6205 of the full address after the usual processing by find_reloads_address.
6206 Note that this may not work in the general case, but it seems to cover
6207 the cases where this situation currently occurs. A more general fix
6208 might be to reload the *value* instead of the address, but this would
6209 not be expected by the callers of this routine as-is.
6211 If find_reloads_address already completed replaced the address, there
6212 is nothing further to do. */
6213 if (reloaded == 0
6214 && reg_equiv_mem (regno) != 0
6215 && !strict_memory_address_addr_space_p
6216 (GET_MODE (x), XEXP (reg_equiv_mem (regno), 0),
6217 MEM_ADDR_SPACE (reg_equiv_mem (regno))))
6219 push_reload (XEXP (tem, 0), NULL_RTX, &XEXP (tem, 0), (rtx*) 0,
6220 base_reg_class (GET_MODE (tem), MEM_ADDR_SPACE (tem),
6221 MEM, SCRATCH),
6222 GET_MODE (XEXP (tem, 0)), VOIDmode, 0, 0, opnum, type);
6223 reloaded = 1;
6226 /* If this is not a toplevel operand, find_reloads doesn't see this
6227 substitution. We have to emit a USE of the pseudo so that
6228 delete_output_reload can see it. */
6229 if (replace_reloads && recog_data.operand[opnum] != x)
6230 /* We mark the USE with QImode so that we recognize it as one that
6231 can be safely deleted at the end of reload. */
6232 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, SUBREG_REG (x)), insn),
6233 QImode);
6235 if (address_reloaded)
6236 *address_reloaded = reloaded;
6238 return tem;
6241 /* Substitute into the current INSN the registers into which we have reloaded
6242 the things that need reloading. The array `replacements'
6243 contains the locations of all pointers that must be changed
6244 and says what to replace them with.
6246 Return the rtx that X translates into; usually X, but modified. */
6248 void
6249 subst_reloads (rtx_insn *insn)
6251 int i;
6253 for (i = 0; i < n_replacements; i++)
6255 struct replacement *r = &replacements[i];
6256 rtx reloadreg = rld[r->what].reg_rtx;
6257 if (reloadreg)
6259 #ifdef DEBUG_RELOAD
6260 /* This checking takes a very long time on some platforms
6261 causing the gcc.c-torture/compile/limits-fnargs.c test
6262 to time out during testing. See PR 31850.
6264 Internal consistency test. Check that we don't modify
6265 anything in the equivalence arrays. Whenever something from
6266 those arrays needs to be reloaded, it must be unshared before
6267 being substituted into; the equivalence must not be modified.
6268 Otherwise, if the equivalence is used after that, it will
6269 have been modified, and the thing substituted (probably a
6270 register) is likely overwritten and not a usable equivalence. */
6271 int check_regno;
6273 for (check_regno = 0; check_regno < max_regno; check_regno++)
6275 #define CHECK_MODF(ARRAY) \
6276 gcc_assert (!(*reg_equivs)[check_regno].ARRAY \
6277 || !loc_mentioned_in_p (r->where, \
6278 (*reg_equivs)[check_regno].ARRAY))
6280 CHECK_MODF (constant);
6281 CHECK_MODF (memory_loc);
6282 CHECK_MODF (address);
6283 CHECK_MODF (mem);
6284 #undef CHECK_MODF
6286 #endif /* DEBUG_RELOAD */
6288 /* If we're replacing a LABEL_REF with a register, there must
6289 already be an indication (to e.g. flow) which label this
6290 register refers to. */
6291 gcc_assert (GET_CODE (*r->where) != LABEL_REF
6292 || !JUMP_P (insn)
6293 || find_reg_note (insn,
6294 REG_LABEL_OPERAND,
6295 XEXP (*r->where, 0))
6296 || label_is_jump_target_p (XEXP (*r->where, 0), insn));
6298 /* Encapsulate RELOADREG so its machine mode matches what
6299 used to be there. Note that gen_lowpart_common will
6300 do the wrong thing if RELOADREG is multi-word. RELOADREG
6301 will always be a REG here. */
6302 if (GET_MODE (reloadreg) != r->mode && r->mode != VOIDmode)
6303 reloadreg = reload_adjust_reg_for_mode (reloadreg, r->mode);
6305 *r->where = reloadreg;
6307 /* If reload got no reg and isn't optional, something's wrong. */
6308 else
6309 gcc_assert (rld[r->what].optional);
6313 /* Make a copy of any replacements being done into X and move those
6314 copies to locations in Y, a copy of X. */
6316 void
6317 copy_replacements (rtx x, rtx y)
6319 copy_replacements_1 (&x, &y, n_replacements);
6322 static void
6323 copy_replacements_1 (rtx *px, rtx *py, int orig_replacements)
6325 int i, j;
6326 rtx x, y;
6327 struct replacement *r;
6328 enum rtx_code code;
6329 const char *fmt;
6331 for (j = 0; j < orig_replacements; j++)
6332 if (replacements[j].where == px)
6334 r = &replacements[n_replacements++];
6335 r->where = py;
6336 r->what = replacements[j].what;
6337 r->mode = replacements[j].mode;
6340 x = *px;
6341 y = *py;
6342 code = GET_CODE (x);
6343 fmt = GET_RTX_FORMAT (code);
6345 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6347 if (fmt[i] == 'e')
6348 copy_replacements_1 (&XEXP (x, i), &XEXP (y, i), orig_replacements);
6349 else if (fmt[i] == 'E')
6350 for (j = XVECLEN (x, i); --j >= 0; )
6351 copy_replacements_1 (&XVECEXP (x, i, j), &XVECEXP (y, i, j),
6352 orig_replacements);
6356 /* Change any replacements being done to *X to be done to *Y. */
6358 void
6359 move_replacements (rtx *x, rtx *y)
6361 int i;
6363 for (i = 0; i < n_replacements; i++)
6364 if (replacements[i].where == x)
6365 replacements[i].where = y;
6368 /* If LOC was scheduled to be replaced by something, return the replacement.
6369 Otherwise, return *LOC. */
6372 find_replacement (rtx *loc)
6374 struct replacement *r;
6376 for (r = &replacements[0]; r < &replacements[n_replacements]; r++)
6378 rtx reloadreg = rld[r->what].reg_rtx;
6380 if (reloadreg && r->where == loc)
6382 if (r->mode != VOIDmode && GET_MODE (reloadreg) != r->mode)
6383 reloadreg = reload_adjust_reg_for_mode (reloadreg, r->mode);
6385 return reloadreg;
6387 else if (reloadreg && GET_CODE (*loc) == SUBREG
6388 && r->where == &SUBREG_REG (*loc))
6390 if (r->mode != VOIDmode && GET_MODE (reloadreg) != r->mode)
6391 reloadreg = reload_adjust_reg_for_mode (reloadreg, r->mode);
6393 return simplify_gen_subreg (GET_MODE (*loc), reloadreg,
6394 GET_MODE (SUBREG_REG (*loc)),
6395 SUBREG_BYTE (*loc));
6399 /* If *LOC is a PLUS, MINUS, or MULT, see if a replacement is scheduled for
6400 what's inside and make a new rtl if so. */
6401 if (GET_CODE (*loc) == PLUS || GET_CODE (*loc) == MINUS
6402 || GET_CODE (*loc) == MULT)
6404 rtx x = find_replacement (&XEXP (*loc, 0));
6405 rtx y = find_replacement (&XEXP (*loc, 1));
6407 if (x != XEXP (*loc, 0) || y != XEXP (*loc, 1))
6408 return gen_rtx_fmt_ee (GET_CODE (*loc), GET_MODE (*loc), x, y);
6411 return *loc;
6414 /* Return nonzero if register in range [REGNO, ENDREGNO)
6415 appears either explicitly or implicitly in X
6416 other than being stored into (except for earlyclobber operands).
6418 References contained within the substructure at LOC do not count.
6419 LOC may be zero, meaning don't ignore anything.
6421 This is similar to refers_to_regno_p in rtlanal.c except that we
6422 look at equivalences for pseudos that didn't get hard registers. */
6424 static int
6425 refers_to_regno_for_reload_p (unsigned int regno, unsigned int endregno,
6426 rtx x, rtx *loc)
6428 int i;
6429 unsigned int r;
6430 RTX_CODE code;
6431 const char *fmt;
6433 if (x == 0)
6434 return 0;
6436 repeat:
6437 code = GET_CODE (x);
6439 switch (code)
6441 case REG:
6442 r = REGNO (x);
6444 /* If this is a pseudo, a hard register must not have been allocated.
6445 X must therefore either be a constant or be in memory. */
6446 if (r >= FIRST_PSEUDO_REGISTER)
6448 if (reg_equiv_memory_loc (r))
6449 return refers_to_regno_for_reload_p (regno, endregno,
6450 reg_equiv_memory_loc (r),
6451 (rtx*) 0);
6453 gcc_assert (reg_equiv_constant (r) || reg_equiv_invariant (r));
6454 return 0;
6457 return (endregno > r
6458 && regno < r + (r < FIRST_PSEUDO_REGISTER
6459 ? hard_regno_nregs[r][GET_MODE (x)]
6460 : 1));
6462 case SUBREG:
6463 /* If this is a SUBREG of a hard reg, we can see exactly which
6464 registers are being modified. Otherwise, handle normally. */
6465 if (REG_P (SUBREG_REG (x))
6466 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
6468 unsigned int inner_regno = subreg_regno (x);
6469 unsigned int inner_endregno
6470 = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
6471 ? subreg_nregs (x) : 1);
6473 return endregno > inner_regno && regno < inner_endregno;
6475 break;
6477 case CLOBBER:
6478 case SET:
6479 if (&SET_DEST (x) != loc
6480 /* Note setting a SUBREG counts as referring to the REG it is in for
6481 a pseudo but not for hard registers since we can
6482 treat each word individually. */
6483 && ((GET_CODE (SET_DEST (x)) == SUBREG
6484 && loc != &SUBREG_REG (SET_DEST (x))
6485 && REG_P (SUBREG_REG (SET_DEST (x)))
6486 && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
6487 && refers_to_regno_for_reload_p (regno, endregno,
6488 SUBREG_REG (SET_DEST (x)),
6489 loc))
6490 /* If the output is an earlyclobber operand, this is
6491 a conflict. */
6492 || ((!REG_P (SET_DEST (x))
6493 || earlyclobber_operand_p (SET_DEST (x)))
6494 && refers_to_regno_for_reload_p (regno, endregno,
6495 SET_DEST (x), loc))))
6496 return 1;
6498 if (code == CLOBBER || loc == &SET_SRC (x))
6499 return 0;
6500 x = SET_SRC (x);
6501 goto repeat;
6503 default:
6504 break;
6507 /* X does not match, so try its subexpressions. */
6509 fmt = GET_RTX_FORMAT (code);
6510 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6512 if (fmt[i] == 'e' && loc != &XEXP (x, i))
6514 if (i == 0)
6516 x = XEXP (x, 0);
6517 goto repeat;
6519 else
6520 if (refers_to_regno_for_reload_p (regno, endregno,
6521 XEXP (x, i), loc))
6522 return 1;
6524 else if (fmt[i] == 'E')
6526 int j;
6527 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6528 if (loc != &XVECEXP (x, i, j)
6529 && refers_to_regno_for_reload_p (regno, endregno,
6530 XVECEXP (x, i, j), loc))
6531 return 1;
6534 return 0;
6537 /* Nonzero if modifying X will affect IN. If X is a register or a SUBREG,
6538 we check if any register number in X conflicts with the relevant register
6539 numbers. If X is a constant, return 0. If X is a MEM, return 1 iff IN
6540 contains a MEM (we don't bother checking for memory addresses that can't
6541 conflict because we expect this to be a rare case.
6543 This function is similar to reg_overlap_mentioned_p in rtlanal.c except
6544 that we look at equivalences for pseudos that didn't get hard registers. */
6547 reg_overlap_mentioned_for_reload_p (rtx x, rtx in)
6549 int regno, endregno;
6551 /* Overly conservative. */
6552 if (GET_CODE (x) == STRICT_LOW_PART
6553 || GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
6554 x = XEXP (x, 0);
6556 /* If either argument is a constant, then modifying X can not affect IN. */
6557 if (CONSTANT_P (x) || CONSTANT_P (in))
6558 return 0;
6559 else if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
6560 return refers_to_mem_for_reload_p (in);
6561 else if (GET_CODE (x) == SUBREG)
6563 regno = REGNO (SUBREG_REG (x));
6564 if (regno < FIRST_PSEUDO_REGISTER)
6565 regno += subreg_regno_offset (REGNO (SUBREG_REG (x)),
6566 GET_MODE (SUBREG_REG (x)),
6567 SUBREG_BYTE (x),
6568 GET_MODE (x));
6569 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
6570 ? subreg_nregs (x) : 1);
6572 return refers_to_regno_for_reload_p (regno, endregno, in, (rtx*) 0);
6574 else if (REG_P (x))
6576 regno = REGNO (x);
6578 /* If this is a pseudo, it must not have been assigned a hard register.
6579 Therefore, it must either be in memory or be a constant. */
6581 if (regno >= FIRST_PSEUDO_REGISTER)
6583 if (reg_equiv_memory_loc (regno))
6584 return refers_to_mem_for_reload_p (in);
6585 gcc_assert (reg_equiv_constant (regno));
6586 return 0;
6589 endregno = END_HARD_REGNO (x);
6591 return refers_to_regno_for_reload_p (regno, endregno, in, (rtx*) 0);
6593 else if (MEM_P (x))
6594 return refers_to_mem_for_reload_p (in);
6595 else if (GET_CODE (x) == SCRATCH || GET_CODE (x) == PC
6596 || GET_CODE (x) == CC0)
6597 return reg_mentioned_p (x, in);
6598 else
6600 gcc_assert (GET_CODE (x) == PLUS);
6602 /* We actually want to know if X is mentioned somewhere inside IN.
6603 We must not say that (plus (sp) (const_int 124)) is in
6604 (plus (sp) (const_int 64)), since that can lead to incorrect reload
6605 allocation when spuriously changing a RELOAD_FOR_OUTPUT_ADDRESS
6606 into a RELOAD_OTHER on behalf of another RELOAD_OTHER. */
6607 while (MEM_P (in))
6608 in = XEXP (in, 0);
6609 if (REG_P (in))
6610 return 0;
6611 else if (GET_CODE (in) == PLUS)
6612 return (rtx_equal_p (x, in)
6613 || reg_overlap_mentioned_for_reload_p (x, XEXP (in, 0))
6614 || reg_overlap_mentioned_for_reload_p (x, XEXP (in, 1)));
6615 else return (reg_overlap_mentioned_for_reload_p (XEXP (x, 0), in)
6616 || reg_overlap_mentioned_for_reload_p (XEXP (x, 1), in));
6619 gcc_unreachable ();
6622 /* Return nonzero if anything in X contains a MEM. Look also for pseudo
6623 registers. */
6625 static int
6626 refers_to_mem_for_reload_p (rtx x)
6628 const char *fmt;
6629 int i;
6631 if (MEM_P (x))
6632 return 1;
6634 if (REG_P (x))
6635 return (REGNO (x) >= FIRST_PSEUDO_REGISTER
6636 && reg_equiv_memory_loc (REGNO (x)));
6638 fmt = GET_RTX_FORMAT (GET_CODE (x));
6639 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6640 if (fmt[i] == 'e'
6641 && (MEM_P (XEXP (x, i))
6642 || refers_to_mem_for_reload_p (XEXP (x, i))))
6643 return 1;
6645 return 0;
6648 /* Check the insns before INSN to see if there is a suitable register
6649 containing the same value as GOAL.
6650 If OTHER is -1, look for a register in class RCLASS.
6651 Otherwise, just see if register number OTHER shares GOAL's value.
6653 Return an rtx for the register found, or zero if none is found.
6655 If RELOAD_REG_P is (short *)1,
6656 we reject any hard reg that appears in reload_reg_rtx
6657 because such a hard reg is also needed coming into this insn.
6659 If RELOAD_REG_P is any other nonzero value,
6660 it is a vector indexed by hard reg number
6661 and we reject any hard reg whose element in the vector is nonnegative
6662 as well as any that appears in reload_reg_rtx.
6664 If GOAL is zero, then GOALREG is a register number; we look
6665 for an equivalent for that register.
6667 MODE is the machine mode of the value we want an equivalence for.
6668 If GOAL is nonzero and not VOIDmode, then it must have mode MODE.
6670 This function is used by jump.c as well as in the reload pass.
6672 If GOAL is the sum of the stack pointer and a constant, we treat it
6673 as if it were a constant except that sp is required to be unchanging. */
6676 find_equiv_reg (rtx goal, rtx_insn *insn, enum reg_class rclass, int other,
6677 short *reload_reg_p, int goalreg, enum machine_mode mode)
6679 rtx_insn *p = insn;
6680 rtx goaltry, valtry, value, where;
6681 rtx pat;
6682 int regno = -1;
6683 int valueno;
6684 int goal_mem = 0;
6685 int goal_const = 0;
6686 int goal_mem_addr_varies = 0;
6687 int need_stable_sp = 0;
6688 int nregs;
6689 int valuenregs;
6690 int num = 0;
6692 if (goal == 0)
6693 regno = goalreg;
6694 else if (REG_P (goal))
6695 regno = REGNO (goal);
6696 else if (MEM_P (goal))
6698 enum rtx_code code = GET_CODE (XEXP (goal, 0));
6699 if (MEM_VOLATILE_P (goal))
6700 return 0;
6701 if (flag_float_store && SCALAR_FLOAT_MODE_P (GET_MODE (goal)))
6702 return 0;
6703 /* An address with side effects must be reexecuted. */
6704 switch (code)
6706 case POST_INC:
6707 case PRE_INC:
6708 case POST_DEC:
6709 case PRE_DEC:
6710 case POST_MODIFY:
6711 case PRE_MODIFY:
6712 return 0;
6713 default:
6714 break;
6716 goal_mem = 1;
6718 else if (CONSTANT_P (goal))
6719 goal_const = 1;
6720 else if (GET_CODE (goal) == PLUS
6721 && XEXP (goal, 0) == stack_pointer_rtx
6722 && CONSTANT_P (XEXP (goal, 1)))
6723 goal_const = need_stable_sp = 1;
6724 else if (GET_CODE (goal) == PLUS
6725 && XEXP (goal, 0) == frame_pointer_rtx
6726 && CONSTANT_P (XEXP (goal, 1)))
6727 goal_const = 1;
6728 else
6729 return 0;
6731 num = 0;
6732 /* Scan insns back from INSN, looking for one that copies
6733 a value into or out of GOAL.
6734 Stop and give up if we reach a label. */
6736 while (1)
6738 p = PREV_INSN (p);
6739 if (p && DEBUG_INSN_P (p))
6740 continue;
6741 num++;
6742 if (p == 0 || LABEL_P (p)
6743 || num > PARAM_VALUE (PARAM_MAX_RELOAD_SEARCH_INSNS))
6744 return 0;
6746 /* Don't reuse register contents from before a setjmp-type
6747 function call; on the second return (from the longjmp) it
6748 might have been clobbered by a later reuse. It doesn't
6749 seem worthwhile to actually go and see if it is actually
6750 reused even if that information would be readily available;
6751 just don't reuse it across the setjmp call. */
6752 if (CALL_P (p) && find_reg_note (p, REG_SETJMP, NULL_RTX))
6753 return 0;
6755 if (NONJUMP_INSN_P (p)
6756 /* If we don't want spill regs ... */
6757 && (! (reload_reg_p != 0
6758 && reload_reg_p != (short *) (HOST_WIDE_INT) 1)
6759 /* ... then ignore insns introduced by reload; they aren't
6760 useful and can cause results in reload_as_needed to be
6761 different from what they were when calculating the need for
6762 spills. If we notice an input-reload insn here, we will
6763 reject it below, but it might hide a usable equivalent.
6764 That makes bad code. It may even fail: perhaps no reg was
6765 spilled for this insn because it was assumed we would find
6766 that equivalent. */
6767 || INSN_UID (p) < reload_first_uid))
6769 rtx tem;
6770 pat = single_set (p);
6772 /* First check for something that sets some reg equal to GOAL. */
6773 if (pat != 0
6774 && ((regno >= 0
6775 && true_regnum (SET_SRC (pat)) == regno
6776 && (valueno = true_regnum (valtry = SET_DEST (pat))) >= 0)
6778 (regno >= 0
6779 && true_regnum (SET_DEST (pat)) == regno
6780 && (valueno = true_regnum (valtry = SET_SRC (pat))) >= 0)
6782 (goal_const && rtx_equal_p (SET_SRC (pat), goal)
6783 /* When looking for stack pointer + const,
6784 make sure we don't use a stack adjust. */
6785 && !reg_overlap_mentioned_for_reload_p (SET_DEST (pat), goal)
6786 && (valueno = true_regnum (valtry = SET_DEST (pat))) >= 0)
6787 || (goal_mem
6788 && (valueno = true_regnum (valtry = SET_DEST (pat))) >= 0
6789 && rtx_renumbered_equal_p (goal, SET_SRC (pat)))
6790 || (goal_mem
6791 && (valueno = true_regnum (valtry = SET_SRC (pat))) >= 0
6792 && rtx_renumbered_equal_p (goal, SET_DEST (pat)))
6793 /* If we are looking for a constant,
6794 and something equivalent to that constant was copied
6795 into a reg, we can use that reg. */
6796 || (goal_const && REG_NOTES (p) != 0
6797 && (tem = find_reg_note (p, REG_EQUIV, NULL_RTX))
6798 && ((rtx_equal_p (XEXP (tem, 0), goal)
6799 && (valueno
6800 = true_regnum (valtry = SET_DEST (pat))) >= 0)
6801 || (REG_P (SET_DEST (pat))
6802 && CONST_DOUBLE_AS_FLOAT_P (XEXP (tem, 0))
6803 && SCALAR_FLOAT_MODE_P (GET_MODE (XEXP (tem, 0)))
6804 && CONST_INT_P (goal)
6805 && 0 != (goaltry
6806 = operand_subword (XEXP (tem, 0), 0, 0,
6807 VOIDmode))
6808 && rtx_equal_p (goal, goaltry)
6809 && (valtry
6810 = operand_subword (SET_DEST (pat), 0, 0,
6811 VOIDmode))
6812 && (valueno = true_regnum (valtry)) >= 0)))
6813 || (goal_const && (tem = find_reg_note (p, REG_EQUIV,
6814 NULL_RTX))
6815 && REG_P (SET_DEST (pat))
6816 && CONST_DOUBLE_AS_FLOAT_P (XEXP (tem, 0))
6817 && SCALAR_FLOAT_MODE_P (GET_MODE (XEXP (tem, 0)))
6818 && CONST_INT_P (goal)
6819 && 0 != (goaltry = operand_subword (XEXP (tem, 0), 1, 0,
6820 VOIDmode))
6821 && rtx_equal_p (goal, goaltry)
6822 && (valtry
6823 = operand_subword (SET_DEST (pat), 1, 0, VOIDmode))
6824 && (valueno = true_regnum (valtry)) >= 0)))
6826 if (other >= 0)
6828 if (valueno != other)
6829 continue;
6831 else if ((unsigned) valueno >= FIRST_PSEUDO_REGISTER)
6832 continue;
6833 else if (!in_hard_reg_set_p (reg_class_contents[(int) rclass],
6834 mode, valueno))
6835 continue;
6836 value = valtry;
6837 where = p;
6838 break;
6843 /* We found a previous insn copying GOAL into a suitable other reg VALUE
6844 (or copying VALUE into GOAL, if GOAL is also a register).
6845 Now verify that VALUE is really valid. */
6847 /* VALUENO is the register number of VALUE; a hard register. */
6849 /* Don't try to re-use something that is killed in this insn. We want
6850 to be able to trust REG_UNUSED notes. */
6851 if (REG_NOTES (where) != 0 && find_reg_note (where, REG_UNUSED, value))
6852 return 0;
6854 /* If we propose to get the value from the stack pointer or if GOAL is
6855 a MEM based on the stack pointer, we need a stable SP. */
6856 if (valueno == STACK_POINTER_REGNUM || regno == STACK_POINTER_REGNUM
6857 || (goal_mem && reg_overlap_mentioned_for_reload_p (stack_pointer_rtx,
6858 goal)))
6859 need_stable_sp = 1;
6861 /* Reject VALUE if the copy-insn moved the wrong sort of datum. */
6862 if (GET_MODE (value) != mode)
6863 return 0;
6865 /* Reject VALUE if it was loaded from GOAL
6866 and is also a register that appears in the address of GOAL. */
6868 if (goal_mem && value == SET_DEST (single_set (where))
6869 && refers_to_regno_for_reload_p (valueno, end_hard_regno (mode, valueno),
6870 goal, (rtx*) 0))
6871 return 0;
6873 /* Reject registers that overlap GOAL. */
6875 if (regno >= 0 && regno < FIRST_PSEUDO_REGISTER)
6876 nregs = hard_regno_nregs[regno][mode];
6877 else
6878 nregs = 1;
6879 valuenregs = hard_regno_nregs[valueno][mode];
6881 if (!goal_mem && !goal_const
6882 && regno + nregs > valueno && regno < valueno + valuenregs)
6883 return 0;
6885 /* Reject VALUE if it is one of the regs reserved for reloads.
6886 Reload1 knows how to reuse them anyway, and it would get
6887 confused if we allocated one without its knowledge.
6888 (Now that insns introduced by reload are ignored above,
6889 this case shouldn't happen, but I'm not positive.) */
6891 if (reload_reg_p != 0 && reload_reg_p != (short *) (HOST_WIDE_INT) 1)
6893 int i;
6894 for (i = 0; i < valuenregs; ++i)
6895 if (reload_reg_p[valueno + i] >= 0)
6896 return 0;
6899 /* Reject VALUE if it is a register being used for an input reload
6900 even if it is not one of those reserved. */
6902 if (reload_reg_p != 0)
6904 int i;
6905 for (i = 0; i < n_reloads; i++)
6906 if (rld[i].reg_rtx != 0 && rld[i].in)
6908 int regno1 = REGNO (rld[i].reg_rtx);
6909 int nregs1 = hard_regno_nregs[regno1]
6910 [GET_MODE (rld[i].reg_rtx)];
6911 if (regno1 < valueno + valuenregs
6912 && regno1 + nregs1 > valueno)
6913 return 0;
6917 if (goal_mem)
6918 /* We must treat frame pointer as varying here,
6919 since it can vary--in a nonlocal goto as generated by expand_goto. */
6920 goal_mem_addr_varies = !CONSTANT_ADDRESS_P (XEXP (goal, 0));
6922 /* Now verify that the values of GOAL and VALUE remain unaltered
6923 until INSN is reached. */
6925 p = insn;
6926 while (1)
6928 p = PREV_INSN (p);
6929 if (p == where)
6930 return value;
6932 /* Don't trust the conversion past a function call
6933 if either of the two is in a call-clobbered register, or memory. */
6934 if (CALL_P (p))
6936 int i;
6938 if (goal_mem || need_stable_sp)
6939 return 0;
6941 if (regno >= 0 && regno < FIRST_PSEUDO_REGISTER)
6942 for (i = 0; i < nregs; ++i)
6943 if (call_used_regs[regno + i]
6944 || HARD_REGNO_CALL_PART_CLOBBERED (regno + i, mode))
6945 return 0;
6947 if (valueno >= 0 && valueno < FIRST_PSEUDO_REGISTER)
6948 for (i = 0; i < valuenregs; ++i)
6949 if (call_used_regs[valueno + i]
6950 || HARD_REGNO_CALL_PART_CLOBBERED (valueno + i, mode))
6951 return 0;
6954 if (INSN_P (p))
6956 pat = PATTERN (p);
6958 /* Watch out for unspec_volatile, and volatile asms. */
6959 if (volatile_insn_p (pat))
6960 return 0;
6962 /* If this insn P stores in either GOAL or VALUE, return 0.
6963 If GOAL is a memory ref and this insn writes memory, return 0.
6964 If GOAL is a memory ref and its address is not constant,
6965 and this insn P changes a register used in GOAL, return 0. */
6967 if (GET_CODE (pat) == COND_EXEC)
6968 pat = COND_EXEC_CODE (pat);
6969 if (GET_CODE (pat) == SET || GET_CODE (pat) == CLOBBER)
6971 rtx dest = SET_DEST (pat);
6972 while (GET_CODE (dest) == SUBREG
6973 || GET_CODE (dest) == ZERO_EXTRACT
6974 || GET_CODE (dest) == STRICT_LOW_PART)
6975 dest = XEXP (dest, 0);
6976 if (REG_P (dest))
6978 int xregno = REGNO (dest);
6979 int xnregs;
6980 if (REGNO (dest) < FIRST_PSEUDO_REGISTER)
6981 xnregs = hard_regno_nregs[xregno][GET_MODE (dest)];
6982 else
6983 xnregs = 1;
6984 if (xregno < regno + nregs && xregno + xnregs > regno)
6985 return 0;
6986 if (xregno < valueno + valuenregs
6987 && xregno + xnregs > valueno)
6988 return 0;
6989 if (goal_mem_addr_varies
6990 && reg_overlap_mentioned_for_reload_p (dest, goal))
6991 return 0;
6992 if (xregno == STACK_POINTER_REGNUM && need_stable_sp)
6993 return 0;
6995 else if (goal_mem && MEM_P (dest)
6996 && ! push_operand (dest, GET_MODE (dest)))
6997 return 0;
6998 else if (MEM_P (dest) && regno >= FIRST_PSEUDO_REGISTER
6999 && reg_equiv_memory_loc (regno) != 0)
7000 return 0;
7001 else if (need_stable_sp && push_operand (dest, GET_MODE (dest)))
7002 return 0;
7004 else if (GET_CODE (pat) == PARALLEL)
7006 int i;
7007 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
7009 rtx v1 = XVECEXP (pat, 0, i);
7010 if (GET_CODE (v1) == COND_EXEC)
7011 v1 = COND_EXEC_CODE (v1);
7012 if (GET_CODE (v1) == SET || GET_CODE (v1) == CLOBBER)
7014 rtx dest = SET_DEST (v1);
7015 while (GET_CODE (dest) == SUBREG
7016 || GET_CODE (dest) == ZERO_EXTRACT
7017 || GET_CODE (dest) == STRICT_LOW_PART)
7018 dest = XEXP (dest, 0);
7019 if (REG_P (dest))
7021 int xregno = REGNO (dest);
7022 int xnregs;
7023 if (REGNO (dest) < FIRST_PSEUDO_REGISTER)
7024 xnregs = hard_regno_nregs[xregno][GET_MODE (dest)];
7025 else
7026 xnregs = 1;
7027 if (xregno < regno + nregs
7028 && xregno + xnregs > regno)
7029 return 0;
7030 if (xregno < valueno + valuenregs
7031 && xregno + xnregs > valueno)
7032 return 0;
7033 if (goal_mem_addr_varies
7034 && reg_overlap_mentioned_for_reload_p (dest,
7035 goal))
7036 return 0;
7037 if (xregno == STACK_POINTER_REGNUM && need_stable_sp)
7038 return 0;
7040 else if (goal_mem && MEM_P (dest)
7041 && ! push_operand (dest, GET_MODE (dest)))
7042 return 0;
7043 else if (MEM_P (dest) && regno >= FIRST_PSEUDO_REGISTER
7044 && reg_equiv_memory_loc (regno) != 0)
7045 return 0;
7046 else if (need_stable_sp
7047 && push_operand (dest, GET_MODE (dest)))
7048 return 0;
7053 if (CALL_P (p) && CALL_INSN_FUNCTION_USAGE (p))
7055 rtx link;
7057 for (link = CALL_INSN_FUNCTION_USAGE (p); XEXP (link, 1) != 0;
7058 link = XEXP (link, 1))
7060 pat = XEXP (link, 0);
7061 if (GET_CODE (pat) == CLOBBER)
7063 rtx dest = SET_DEST (pat);
7065 if (REG_P (dest))
7067 int xregno = REGNO (dest);
7068 int xnregs
7069 = hard_regno_nregs[xregno][GET_MODE (dest)];
7071 if (xregno < regno + nregs
7072 && xregno + xnregs > regno)
7073 return 0;
7074 else if (xregno < valueno + valuenregs
7075 && xregno + xnregs > valueno)
7076 return 0;
7077 else if (goal_mem_addr_varies
7078 && reg_overlap_mentioned_for_reload_p (dest,
7079 goal))
7080 return 0;
7083 else if (goal_mem && MEM_P (dest)
7084 && ! push_operand (dest, GET_MODE (dest)))
7085 return 0;
7086 else if (need_stable_sp
7087 && push_operand (dest, GET_MODE (dest)))
7088 return 0;
7093 #ifdef AUTO_INC_DEC
7094 /* If this insn auto-increments or auto-decrements
7095 either regno or valueno, return 0 now.
7096 If GOAL is a memory ref and its address is not constant,
7097 and this insn P increments a register used in GOAL, return 0. */
7099 rtx link;
7101 for (link = REG_NOTES (p); link; link = XEXP (link, 1))
7102 if (REG_NOTE_KIND (link) == REG_INC
7103 && REG_P (XEXP (link, 0)))
7105 int incno = REGNO (XEXP (link, 0));
7106 if (incno < regno + nregs && incno >= regno)
7107 return 0;
7108 if (incno < valueno + valuenregs && incno >= valueno)
7109 return 0;
7110 if (goal_mem_addr_varies
7111 && reg_overlap_mentioned_for_reload_p (XEXP (link, 0),
7112 goal))
7113 return 0;
7116 #endif
7121 /* Find a place where INCED appears in an increment or decrement operator
7122 within X, and return the amount INCED is incremented or decremented by.
7123 The value is always positive. */
7125 static int
7126 find_inc_amount (rtx x, rtx inced)
7128 enum rtx_code code = GET_CODE (x);
7129 const char *fmt;
7130 int i;
7132 if (code == MEM)
7134 rtx addr = XEXP (x, 0);
7135 if ((GET_CODE (addr) == PRE_DEC
7136 || GET_CODE (addr) == POST_DEC
7137 || GET_CODE (addr) == PRE_INC
7138 || GET_CODE (addr) == POST_INC)
7139 && XEXP (addr, 0) == inced)
7140 return GET_MODE_SIZE (GET_MODE (x));
7141 else if ((GET_CODE (addr) == PRE_MODIFY
7142 || GET_CODE (addr) == POST_MODIFY)
7143 && GET_CODE (XEXP (addr, 1)) == PLUS
7144 && XEXP (addr, 0) == XEXP (XEXP (addr, 1), 0)
7145 && XEXP (addr, 0) == inced
7146 && CONST_INT_P (XEXP (XEXP (addr, 1), 1)))
7148 i = INTVAL (XEXP (XEXP (addr, 1), 1));
7149 return i < 0 ? -i : i;
7153 fmt = GET_RTX_FORMAT (code);
7154 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7156 if (fmt[i] == 'e')
7158 int tem = find_inc_amount (XEXP (x, i), inced);
7159 if (tem != 0)
7160 return tem;
7162 if (fmt[i] == 'E')
7164 int j;
7165 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7167 int tem = find_inc_amount (XVECEXP (x, i, j), inced);
7168 if (tem != 0)
7169 return tem;
7174 return 0;
7177 /* Return 1 if registers from REGNO to ENDREGNO are the subjects of a
7178 REG_INC note in insn INSN. REGNO must refer to a hard register. */
7180 #ifdef AUTO_INC_DEC
7181 static int
7182 reg_inc_found_and_valid_p (unsigned int regno, unsigned int endregno,
7183 rtx insn)
7185 rtx link;
7187 gcc_assert (insn);
7189 if (! INSN_P (insn))
7190 return 0;
7192 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
7193 if (REG_NOTE_KIND (link) == REG_INC)
7195 unsigned int test = (int) REGNO (XEXP (link, 0));
7196 if (test >= regno && test < endregno)
7197 return 1;
7199 return 0;
7201 #else
7203 #define reg_inc_found_and_valid_p(regno,endregno,insn) 0
7205 #endif
7207 /* Return 1 if register REGNO is the subject of a clobber in insn INSN.
7208 If SETS is 1, also consider SETs. If SETS is 2, enable checking
7209 REG_INC. REGNO must refer to a hard register. */
7212 regno_clobbered_p (unsigned int regno, rtx_insn *insn, enum machine_mode mode,
7213 int sets)
7215 unsigned int nregs, endregno;
7217 /* regno must be a hard register. */
7218 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
7220 nregs = hard_regno_nregs[regno][mode];
7221 endregno = regno + nregs;
7223 if ((GET_CODE (PATTERN (insn)) == CLOBBER
7224 || (sets == 1 && GET_CODE (PATTERN (insn)) == SET))
7225 && REG_P (XEXP (PATTERN (insn), 0)))
7227 unsigned int test = REGNO (XEXP (PATTERN (insn), 0));
7229 return test >= regno && test < endregno;
7232 if (sets == 2 && reg_inc_found_and_valid_p (regno, endregno, insn))
7233 return 1;
7235 if (GET_CODE (PATTERN (insn)) == PARALLEL)
7237 int i = XVECLEN (PATTERN (insn), 0) - 1;
7239 for (; i >= 0; i--)
7241 rtx elt = XVECEXP (PATTERN (insn), 0, i);
7242 if ((GET_CODE (elt) == CLOBBER
7243 || (sets == 1 && GET_CODE (elt) == SET))
7244 && REG_P (XEXP (elt, 0)))
7246 unsigned int test = REGNO (XEXP (elt, 0));
7248 if (test >= regno && test < endregno)
7249 return 1;
7251 if (sets == 2
7252 && reg_inc_found_and_valid_p (regno, endregno, elt))
7253 return 1;
7257 return 0;
7260 /* Find the low part, with mode MODE, of a hard regno RELOADREG. */
7262 reload_adjust_reg_for_mode (rtx reloadreg, enum machine_mode mode)
7264 int regno;
7266 if (GET_MODE (reloadreg) == mode)
7267 return reloadreg;
7269 regno = REGNO (reloadreg);
7271 if (REG_WORDS_BIG_ENDIAN)
7272 regno += (int) hard_regno_nregs[regno][GET_MODE (reloadreg)]
7273 - (int) hard_regno_nregs[regno][mode];
7275 return gen_rtx_REG (mode, regno);
7278 static const char *const reload_when_needed_name[] =
7280 "RELOAD_FOR_INPUT",
7281 "RELOAD_FOR_OUTPUT",
7282 "RELOAD_FOR_INSN",
7283 "RELOAD_FOR_INPUT_ADDRESS",
7284 "RELOAD_FOR_INPADDR_ADDRESS",
7285 "RELOAD_FOR_OUTPUT_ADDRESS",
7286 "RELOAD_FOR_OUTADDR_ADDRESS",
7287 "RELOAD_FOR_OPERAND_ADDRESS",
7288 "RELOAD_FOR_OPADDR_ADDR",
7289 "RELOAD_OTHER",
7290 "RELOAD_FOR_OTHER_ADDRESS"
7293 /* These functions are used to print the variables set by 'find_reloads' */
7295 DEBUG_FUNCTION void
7296 debug_reload_to_stream (FILE *f)
7298 int r;
7299 const char *prefix;
7301 if (! f)
7302 f = stderr;
7303 for (r = 0; r < n_reloads; r++)
7305 fprintf (f, "Reload %d: ", r);
7307 if (rld[r].in != 0)
7309 fprintf (f, "reload_in (%s) = ",
7310 GET_MODE_NAME (rld[r].inmode));
7311 print_inline_rtx (f, rld[r].in, 24);
7312 fprintf (f, "\n\t");
7315 if (rld[r].out != 0)
7317 fprintf (f, "reload_out (%s) = ",
7318 GET_MODE_NAME (rld[r].outmode));
7319 print_inline_rtx (f, rld[r].out, 24);
7320 fprintf (f, "\n\t");
7323 fprintf (f, "%s, ", reg_class_names[(int) rld[r].rclass]);
7325 fprintf (f, "%s (opnum = %d)",
7326 reload_when_needed_name[(int) rld[r].when_needed],
7327 rld[r].opnum);
7329 if (rld[r].optional)
7330 fprintf (f, ", optional");
7332 if (rld[r].nongroup)
7333 fprintf (f, ", nongroup");
7335 if (rld[r].inc != 0)
7336 fprintf (f, ", inc by %d", rld[r].inc);
7338 if (rld[r].nocombine)
7339 fprintf (f, ", can't combine");
7341 if (rld[r].secondary_p)
7342 fprintf (f, ", secondary_reload_p");
7344 if (rld[r].in_reg != 0)
7346 fprintf (f, "\n\treload_in_reg: ");
7347 print_inline_rtx (f, rld[r].in_reg, 24);
7350 if (rld[r].out_reg != 0)
7352 fprintf (f, "\n\treload_out_reg: ");
7353 print_inline_rtx (f, rld[r].out_reg, 24);
7356 if (rld[r].reg_rtx != 0)
7358 fprintf (f, "\n\treload_reg_rtx: ");
7359 print_inline_rtx (f, rld[r].reg_rtx, 24);
7362 prefix = "\n\t";
7363 if (rld[r].secondary_in_reload != -1)
7365 fprintf (f, "%ssecondary_in_reload = %d",
7366 prefix, rld[r].secondary_in_reload);
7367 prefix = ", ";
7370 if (rld[r].secondary_out_reload != -1)
7371 fprintf (f, "%ssecondary_out_reload = %d\n",
7372 prefix, rld[r].secondary_out_reload);
7374 prefix = "\n\t";
7375 if (rld[r].secondary_in_icode != CODE_FOR_nothing)
7377 fprintf (f, "%ssecondary_in_icode = %s", prefix,
7378 insn_data[rld[r].secondary_in_icode].name);
7379 prefix = ", ";
7382 if (rld[r].secondary_out_icode != CODE_FOR_nothing)
7383 fprintf (f, "%ssecondary_out_icode = %s", prefix,
7384 insn_data[rld[r].secondary_out_icode].name);
7386 fprintf (f, "\n");
7390 DEBUG_FUNCTION void
7391 debug_reload (void)
7393 debug_reload_to_stream (stderr);