PR target/81325
[official-gcc.git] / gcc / reload.c
blobc9b946134f3ef450697a56d4b1864240acef2ada
1 /* Search an insn for pseudo regs that must be in hard regs and are not.
2 Copyright (C) 1987-2017 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 CHECKING_P, since it is awfully slow. */
89 #undef DEBUG_RELOAD
91 #include "config.h"
92 #include "system.h"
93 #include "coretypes.h"
94 #include "backend.h"
95 #include "target.h"
96 #include "rtl.h"
97 #include "tree.h"
98 #include "df.h"
99 #include "memmodel.h"
100 #include "tm_p.h"
101 #include "optabs.h"
102 #include "regs.h"
103 #include "ira.h"
104 #include "recog.h"
105 #include "rtl-error.h"
106 #include "reload.h"
107 #include "addresses.h"
108 #include "params.h"
110 /* True if X is a constant that can be forced into the constant pool.
111 MODE is the mode of the operand, or VOIDmode if not known. */
112 #define CONST_POOL_OK_P(MODE, X) \
113 ((MODE) != VOIDmode \
114 && CONSTANT_P (X) \
115 && GET_CODE (X) != HIGH \
116 && !targetm.cannot_force_const_mem (MODE, X))
118 /* True if C is a non-empty register class that has too few registers
119 to be safely used as a reload target class. */
121 static inline bool
122 small_register_class_p (reg_class_t rclass)
124 return (reg_class_size [(int) rclass] == 1
125 || (reg_class_size [(int) rclass] >= 1
126 && targetm.class_likely_spilled_p (rclass)));
130 /* All reloads of the current insn are recorded here. See reload.h for
131 comments. */
132 int n_reloads;
133 struct reload rld[MAX_RELOADS];
135 /* All the "earlyclobber" operands of the current insn
136 are recorded here. */
137 int n_earlyclobbers;
138 rtx reload_earlyclobbers[MAX_RECOG_OPERANDS];
140 int reload_n_operands;
142 /* Replacing reloads.
144 If `replace_reloads' is nonzero, then as each reload is recorded
145 an entry is made for it in the table `replacements'.
146 Then later `subst_reloads' can look through that table and
147 perform all the replacements needed. */
149 /* Nonzero means record the places to replace. */
150 static int replace_reloads;
152 /* Each replacement is recorded with a structure like this. */
153 struct replacement
155 rtx *where; /* Location to store in */
156 int what; /* which reload this is for */
157 machine_mode mode; /* mode it must have */
160 static struct replacement replacements[MAX_RECOG_OPERANDS * ((MAX_REGS_PER_ADDRESS * 2) + 1)];
162 /* Number of replacements currently recorded. */
163 static int n_replacements;
165 /* Used to track what is modified by an operand. */
166 struct decomposition
168 int reg_flag; /* Nonzero if referencing a register. */
169 int safe; /* Nonzero if this can't conflict with anything. */
170 rtx base; /* Base address for MEM. */
171 HOST_WIDE_INT start; /* Starting offset or register number. */
172 HOST_WIDE_INT end; /* Ending offset or register number. */
175 /* Save MEMs needed to copy from one class of registers to another. One MEM
176 is used per mode, but normally only one or two modes are ever used.
178 We keep two versions, before and after register elimination. The one
179 after register elimination is record separately for each operand. This
180 is done in case the address is not valid to be sure that we separately
181 reload each. */
183 static rtx secondary_memlocs[NUM_MACHINE_MODES];
184 static rtx secondary_memlocs_elim[NUM_MACHINE_MODES][MAX_RECOG_OPERANDS];
185 static int secondary_memlocs_elim_used = 0;
187 /* The instruction we are doing reloads for;
188 so we can test whether a register dies in it. */
189 static rtx_insn *this_insn;
191 /* Nonzero if this instruction is a user-specified asm with operands. */
192 static int this_insn_is_asm;
194 /* If hard_regs_live_known is nonzero,
195 we can tell which hard regs are currently live,
196 at least enough to succeed in choosing dummy reloads. */
197 static int hard_regs_live_known;
199 /* Indexed by hard reg number,
200 element is nonnegative if hard reg has been spilled.
201 This vector is passed to `find_reloads' as an argument
202 and is not changed here. */
203 static short *static_reload_reg_p;
205 /* Set to 1 in subst_reg_equivs if it changes anything. */
206 static int subst_reg_equivs_changed;
208 /* On return from push_reload, holds the reload-number for the OUT
209 operand, which can be different for that from the input operand. */
210 static int output_reloadnum;
212 /* Compare two RTX's. */
213 #define MATCHES(x, y) \
214 (x == y || (x != 0 && (REG_P (x) \
215 ? REG_P (y) && REGNO (x) == REGNO (y) \
216 : rtx_equal_p (x, y) && ! side_effects_p (x))))
218 /* Indicates if two reloads purposes are for similar enough things that we
219 can merge their reloads. */
220 #define MERGABLE_RELOADS(when1, when2, op1, op2) \
221 ((when1) == RELOAD_OTHER || (when2) == RELOAD_OTHER \
222 || ((when1) == (when2) && (op1) == (op2)) \
223 || ((when1) == RELOAD_FOR_INPUT && (when2) == RELOAD_FOR_INPUT) \
224 || ((when1) == RELOAD_FOR_OPERAND_ADDRESS \
225 && (when2) == RELOAD_FOR_OPERAND_ADDRESS) \
226 || ((when1) == RELOAD_FOR_OTHER_ADDRESS \
227 && (when2) == RELOAD_FOR_OTHER_ADDRESS))
229 /* Nonzero if these two reload purposes produce RELOAD_OTHER when merged. */
230 #define MERGE_TO_OTHER(when1, when2, op1, op2) \
231 ((when1) != (when2) \
232 || ! ((op1) == (op2) \
233 || (when1) == RELOAD_FOR_INPUT \
234 || (when1) == RELOAD_FOR_OPERAND_ADDRESS \
235 || (when1) == RELOAD_FOR_OTHER_ADDRESS))
237 /* If we are going to reload an address, compute the reload type to
238 use. */
239 #define ADDR_TYPE(type) \
240 ((type) == RELOAD_FOR_INPUT_ADDRESS \
241 ? RELOAD_FOR_INPADDR_ADDRESS \
242 : ((type) == RELOAD_FOR_OUTPUT_ADDRESS \
243 ? RELOAD_FOR_OUTADDR_ADDRESS \
244 : (type)))
246 static int push_secondary_reload (int, rtx, int, int, enum reg_class,
247 machine_mode, enum reload_type,
248 enum insn_code *, secondary_reload_info *);
249 static enum reg_class find_valid_class (machine_mode, machine_mode,
250 int, unsigned int);
251 static void push_replacement (rtx *, int, machine_mode);
252 static void dup_replacements (rtx *, rtx *);
253 static void combine_reloads (void);
254 static int find_reusable_reload (rtx *, rtx, enum reg_class,
255 enum reload_type, int, int);
256 static rtx find_dummy_reload (rtx, rtx, rtx *, rtx *, machine_mode,
257 machine_mode, reg_class_t, int, int);
258 static int hard_reg_set_here_p (unsigned int, unsigned int, rtx);
259 static struct decomposition decompose (rtx);
260 static int immune_p (rtx, rtx, struct decomposition);
261 static bool alternative_allows_const_pool_ref (rtx, const char *, int);
262 static rtx find_reloads_toplev (rtx, int, enum reload_type, int, int,
263 rtx_insn *, int *);
264 static rtx make_memloc (rtx, int);
265 static int maybe_memory_address_addr_space_p (machine_mode, rtx,
266 addr_space_t, rtx *);
267 static int find_reloads_address (machine_mode, rtx *, rtx, rtx *,
268 int, enum reload_type, int, rtx_insn *);
269 static rtx subst_reg_equivs (rtx, rtx_insn *);
270 static rtx subst_indexed_address (rtx);
271 static void update_auto_inc_notes (rtx_insn *, int, int);
272 static int find_reloads_address_1 (machine_mode, addr_space_t, rtx, int,
273 enum rtx_code, enum rtx_code, rtx *,
274 int, enum reload_type,int, rtx_insn *);
275 static void find_reloads_address_part (rtx, rtx *, enum reg_class,
276 machine_mode, int,
277 enum reload_type, int);
278 static rtx find_reloads_subreg_address (rtx, int, enum reload_type,
279 int, rtx_insn *, int *);
280 static void copy_replacements_1 (rtx *, rtx *, int);
281 static int find_inc_amount (rtx, rtx);
282 static int refers_to_mem_for_reload_p (rtx);
283 static int refers_to_regno_for_reload_p (unsigned int, unsigned int,
284 rtx, rtx *);
286 /* Add NEW to reg_equiv_alt_mem_list[REGNO] if it's not present in the
287 list yet. */
289 static void
290 push_reg_equiv_alt_mem (int regno, rtx mem)
292 rtx it;
294 for (it = reg_equiv_alt_mem_list (regno); it; it = XEXP (it, 1))
295 if (rtx_equal_p (XEXP (it, 0), mem))
296 return;
298 reg_equiv_alt_mem_list (regno)
299 = alloc_EXPR_LIST (REG_EQUIV, mem,
300 reg_equiv_alt_mem_list (regno));
303 /* Determine if any secondary reloads are needed for loading (if IN_P is
304 nonzero) or storing (if IN_P is zero) X to or from a reload register of
305 register class RELOAD_CLASS in mode RELOAD_MODE. If secondary reloads
306 are needed, push them.
308 Return the reload number of the secondary reload we made, or -1 if
309 we didn't need one. *PICODE is set to the insn_code to use if we do
310 need a secondary reload. */
312 static int
313 push_secondary_reload (int in_p, rtx x, int opnum, int optional,
314 enum reg_class reload_class,
315 machine_mode reload_mode, enum reload_type type,
316 enum insn_code *picode, secondary_reload_info *prev_sri)
318 enum reg_class rclass = NO_REGS;
319 enum reg_class scratch_class;
320 machine_mode mode = reload_mode;
321 enum insn_code icode = CODE_FOR_nothing;
322 enum insn_code t_icode = CODE_FOR_nothing;
323 enum reload_type secondary_type;
324 int s_reload, t_reload = -1;
325 const char *scratch_constraint;
326 secondary_reload_info sri;
328 if (type == RELOAD_FOR_INPUT_ADDRESS
329 || type == RELOAD_FOR_OUTPUT_ADDRESS
330 || type == RELOAD_FOR_INPADDR_ADDRESS
331 || type == RELOAD_FOR_OUTADDR_ADDRESS)
332 secondary_type = type;
333 else
334 secondary_type = in_p ? RELOAD_FOR_INPUT_ADDRESS : RELOAD_FOR_OUTPUT_ADDRESS;
336 *picode = CODE_FOR_nothing;
338 /* If X is a paradoxical SUBREG, use the inner value to determine both the
339 mode and object being reloaded. */
340 if (paradoxical_subreg_p (x))
342 x = SUBREG_REG (x);
343 reload_mode = GET_MODE (x);
346 /* If X is a pseudo-register that has an equivalent MEM (actually, if it
347 is still a pseudo-register by now, it *must* have an equivalent MEM
348 but we don't want to assume that), use that equivalent when seeing if
349 a secondary reload is needed since whether or not a reload is needed
350 might be sensitive to the form of the MEM. */
352 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER
353 && reg_equiv_mem (REGNO (x)))
354 x = reg_equiv_mem (REGNO (x));
356 sri.icode = CODE_FOR_nothing;
357 sri.prev_sri = prev_sri;
358 rclass = (enum reg_class) targetm.secondary_reload (in_p, x, reload_class,
359 reload_mode, &sri);
360 icode = (enum insn_code) sri.icode;
362 /* If we don't need any secondary registers, done. */
363 if (rclass == NO_REGS && icode == CODE_FOR_nothing)
364 return -1;
366 if (rclass != NO_REGS)
367 t_reload = push_secondary_reload (in_p, x, opnum, optional, rclass,
368 reload_mode, type, &t_icode, &sri);
370 /* If we will be using an insn, the secondary reload is for a
371 scratch register. */
373 if (icode != CODE_FOR_nothing)
375 /* If IN_P is nonzero, the reload register will be the output in
376 operand 0. If IN_P is zero, the reload register will be the input
377 in operand 1. Outputs should have an initial "=", which we must
378 skip. */
380 /* ??? It would be useful to be able to handle only two, or more than
381 three, operands, but for now we can only handle the case of having
382 exactly three: output, input and one temp/scratch. */
383 gcc_assert (insn_data[(int) icode].n_operands == 3);
385 /* ??? We currently have no way to represent a reload that needs
386 an icode to reload from an intermediate tertiary reload register.
387 We should probably have a new field in struct reload to tag a
388 chain of scratch operand reloads onto. */
389 gcc_assert (rclass == NO_REGS);
391 scratch_constraint = insn_data[(int) icode].operand[2].constraint;
392 gcc_assert (*scratch_constraint == '=');
393 scratch_constraint++;
394 if (*scratch_constraint == '&')
395 scratch_constraint++;
396 scratch_class = (reg_class_for_constraint
397 (lookup_constraint (scratch_constraint)));
399 rclass = scratch_class;
400 mode = insn_data[(int) icode].operand[2].mode;
403 /* This case isn't valid, so fail. Reload is allowed to use the same
404 register for RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT reloads, but
405 in the case of a secondary register, we actually need two different
406 registers for correct code. We fail here to prevent the possibility of
407 silently generating incorrect code later.
409 The convention is that secondary input reloads are valid only if the
410 secondary_class is different from class. If you have such a case, you
411 can not use secondary reloads, you must work around the problem some
412 other way.
414 Allow this when a reload_in/out pattern is being used. I.e. assume
415 that the generated code handles this case. */
417 gcc_assert (!in_p || rclass != reload_class || icode != CODE_FOR_nothing
418 || t_icode != CODE_FOR_nothing);
420 /* See if we can reuse an existing secondary reload. */
421 for (s_reload = 0; s_reload < n_reloads; s_reload++)
422 if (rld[s_reload].secondary_p
423 && (reg_class_subset_p (rclass, rld[s_reload].rclass)
424 || reg_class_subset_p (rld[s_reload].rclass, rclass))
425 && ((in_p && rld[s_reload].inmode == mode)
426 || (! in_p && rld[s_reload].outmode == mode))
427 && ((in_p && rld[s_reload].secondary_in_reload == t_reload)
428 || (! in_p && rld[s_reload].secondary_out_reload == t_reload))
429 && ((in_p && rld[s_reload].secondary_in_icode == t_icode)
430 || (! in_p && rld[s_reload].secondary_out_icode == t_icode))
431 && (small_register_class_p (rclass)
432 || targetm.small_register_classes_for_mode_p (VOIDmode))
433 && MERGABLE_RELOADS (secondary_type, rld[s_reload].when_needed,
434 opnum, rld[s_reload].opnum))
436 if (in_p)
437 rld[s_reload].inmode = mode;
438 if (! in_p)
439 rld[s_reload].outmode = mode;
441 if (reg_class_subset_p (rclass, rld[s_reload].rclass))
442 rld[s_reload].rclass = rclass;
444 rld[s_reload].opnum = MIN (rld[s_reload].opnum, opnum);
445 rld[s_reload].optional &= optional;
446 rld[s_reload].secondary_p = 1;
447 if (MERGE_TO_OTHER (secondary_type, rld[s_reload].when_needed,
448 opnum, rld[s_reload].opnum))
449 rld[s_reload].when_needed = RELOAD_OTHER;
451 break;
454 if (s_reload == n_reloads)
456 /* If we need a memory location to copy between the two reload regs,
457 set it up now. Note that we do the input case before making
458 the reload and the output case after. This is due to the
459 way reloads are output. */
461 if (in_p && icode == CODE_FOR_nothing
462 && targetm.secondary_memory_needed (mode, rclass, reload_class))
464 get_secondary_mem (x, reload_mode, opnum, type);
466 /* We may have just added new reloads. Make sure we add
467 the new reload at the end. */
468 s_reload = n_reloads;
471 /* We need to make a new secondary reload for this register class. */
472 rld[s_reload].in = rld[s_reload].out = 0;
473 rld[s_reload].rclass = rclass;
475 rld[s_reload].inmode = in_p ? mode : VOIDmode;
476 rld[s_reload].outmode = ! in_p ? mode : VOIDmode;
477 rld[s_reload].reg_rtx = 0;
478 rld[s_reload].optional = optional;
479 rld[s_reload].inc = 0;
480 /* Maybe we could combine these, but it seems too tricky. */
481 rld[s_reload].nocombine = 1;
482 rld[s_reload].in_reg = 0;
483 rld[s_reload].out_reg = 0;
484 rld[s_reload].opnum = opnum;
485 rld[s_reload].when_needed = secondary_type;
486 rld[s_reload].secondary_in_reload = in_p ? t_reload : -1;
487 rld[s_reload].secondary_out_reload = ! in_p ? t_reload : -1;
488 rld[s_reload].secondary_in_icode = in_p ? t_icode : CODE_FOR_nothing;
489 rld[s_reload].secondary_out_icode
490 = ! in_p ? t_icode : CODE_FOR_nothing;
491 rld[s_reload].secondary_p = 1;
493 n_reloads++;
495 if (! in_p && icode == CODE_FOR_nothing
496 && targetm.secondary_memory_needed (mode, reload_class, rclass))
497 get_secondary_mem (x, mode, opnum, type);
500 *picode = icode;
501 return s_reload;
504 /* If a secondary reload is needed, return its class. If both an intermediate
505 register and a scratch register is needed, we return the class of the
506 intermediate register. */
507 reg_class_t
508 secondary_reload_class (bool in_p, reg_class_t rclass, machine_mode mode,
509 rtx x)
511 enum insn_code icode;
512 secondary_reload_info sri;
514 sri.icode = CODE_FOR_nothing;
515 sri.prev_sri = NULL;
516 rclass
517 = (enum reg_class) targetm.secondary_reload (in_p, x, rclass, mode, &sri);
518 icode = (enum insn_code) sri.icode;
520 /* If there are no secondary reloads at all, we return NO_REGS.
521 If an intermediate register is needed, we return its class. */
522 if (icode == CODE_FOR_nothing || rclass != NO_REGS)
523 return rclass;
525 /* No intermediate register is needed, but we have a special reload
526 pattern, which we assume for now needs a scratch register. */
527 return scratch_reload_class (icode);
530 /* ICODE is the insn_code of a reload pattern. Check that it has exactly
531 three operands, verify that operand 2 is an output operand, and return
532 its register class.
533 ??? We'd like to be able to handle any pattern with at least 2 operands,
534 for zero or more scratch registers, but that needs more infrastructure. */
535 enum reg_class
536 scratch_reload_class (enum insn_code icode)
538 const char *scratch_constraint;
539 enum reg_class rclass;
541 gcc_assert (insn_data[(int) icode].n_operands == 3);
542 scratch_constraint = insn_data[(int) icode].operand[2].constraint;
543 gcc_assert (*scratch_constraint == '=');
544 scratch_constraint++;
545 if (*scratch_constraint == '&')
546 scratch_constraint++;
547 rclass = reg_class_for_constraint (lookup_constraint (scratch_constraint));
548 gcc_assert (rclass != NO_REGS);
549 return rclass;
552 /* Return a memory location that will be used to copy X in mode MODE.
553 If we haven't already made a location for this mode in this insn,
554 call find_reloads_address on the location being returned. */
557 get_secondary_mem (rtx x ATTRIBUTE_UNUSED, machine_mode mode,
558 int opnum, enum reload_type type)
560 rtx loc;
561 int mem_valid;
563 /* By default, if MODE is narrower than a word, widen it to a word.
564 This is required because most machines that require these memory
565 locations do not support short load and stores from all registers
566 (e.g., FP registers). */
568 mode = targetm.secondary_memory_needed_mode (mode);
570 /* If we already have made a MEM for this operand in MODE, return it. */
571 if (secondary_memlocs_elim[(int) mode][opnum] != 0)
572 return secondary_memlocs_elim[(int) mode][opnum];
574 /* If this is the first time we've tried to get a MEM for this mode,
575 allocate a new one. `something_changed' in reload will get set
576 by noticing that the frame size has changed. */
578 if (secondary_memlocs[(int) mode] == 0)
580 #ifdef SECONDARY_MEMORY_NEEDED_RTX
581 secondary_memlocs[(int) mode] = SECONDARY_MEMORY_NEEDED_RTX (mode);
582 #else
583 secondary_memlocs[(int) mode]
584 = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
585 #endif
588 /* Get a version of the address doing any eliminations needed. If that
589 didn't give us a new MEM, make a new one if it isn't valid. */
591 loc = eliminate_regs (secondary_memlocs[(int) mode], VOIDmode, NULL_RTX);
592 mem_valid = strict_memory_address_addr_space_p (mode, XEXP (loc, 0),
593 MEM_ADDR_SPACE (loc));
595 if (! mem_valid && loc == secondary_memlocs[(int) mode])
596 loc = copy_rtx (loc);
598 /* The only time the call below will do anything is if the stack
599 offset is too large. In that case IND_LEVELS doesn't matter, so we
600 can just pass a zero. Adjust the type to be the address of the
601 corresponding object. If the address was valid, save the eliminated
602 address. If it wasn't valid, we need to make a reload each time, so
603 don't save it. */
605 if (! mem_valid)
607 type = (type == RELOAD_FOR_INPUT ? RELOAD_FOR_INPUT_ADDRESS
608 : type == RELOAD_FOR_OUTPUT ? RELOAD_FOR_OUTPUT_ADDRESS
609 : RELOAD_OTHER);
611 find_reloads_address (mode, &loc, XEXP (loc, 0), &XEXP (loc, 0),
612 opnum, type, 0, 0);
615 secondary_memlocs_elim[(int) mode][opnum] = loc;
616 if (secondary_memlocs_elim_used <= (int)mode)
617 secondary_memlocs_elim_used = (int)mode + 1;
618 return loc;
621 /* Clear any secondary memory locations we've made. */
623 void
624 clear_secondary_mem (void)
626 memset (secondary_memlocs, 0, sizeof secondary_memlocs);
630 /* Find the largest class which has at least one register valid in
631 mode INNER, and which for every such register, that register number
632 plus N is also valid in OUTER (if in range) and is cheap to move
633 into REGNO. Such a class must exist. */
635 static enum reg_class
636 find_valid_class (machine_mode outer ATTRIBUTE_UNUSED,
637 machine_mode inner ATTRIBUTE_UNUSED, int n,
638 unsigned int dest_regno ATTRIBUTE_UNUSED)
640 int best_cost = -1;
641 int rclass;
642 int regno;
643 enum reg_class best_class = NO_REGS;
644 enum reg_class dest_class ATTRIBUTE_UNUSED = REGNO_REG_CLASS (dest_regno);
645 unsigned int best_size = 0;
646 int cost;
648 for (rclass = 1; rclass < N_REG_CLASSES; rclass++)
650 int bad = 0;
651 int good = 0;
652 for (regno = 0; regno < FIRST_PSEUDO_REGISTER - n && ! bad; regno++)
653 if (TEST_HARD_REG_BIT (reg_class_contents[rclass], regno))
655 if (targetm.hard_regno_mode_ok (regno, inner))
657 good = 1;
658 if (TEST_HARD_REG_BIT (reg_class_contents[rclass], regno + n)
659 && !targetm.hard_regno_mode_ok (regno + n, outer))
660 bad = 1;
664 if (bad || !good)
665 continue;
666 cost = register_move_cost (outer, (enum reg_class) rclass, dest_class);
668 if ((reg_class_size[rclass] > best_size
669 && (best_cost < 0 || best_cost >= cost))
670 || best_cost > cost)
672 best_class = (enum reg_class) rclass;
673 best_size = reg_class_size[rclass];
674 best_cost = register_move_cost (outer, (enum reg_class) rclass,
675 dest_class);
679 gcc_assert (best_size != 0);
681 return best_class;
684 /* We are trying to reload a subreg of something that is not a register.
685 Find the largest class which contains only registers valid in
686 mode MODE. OUTER is the mode of the subreg, DEST_CLASS the class in
687 which we would eventually like to obtain the object. */
689 static enum reg_class
690 find_valid_class_1 (machine_mode outer ATTRIBUTE_UNUSED,
691 machine_mode mode ATTRIBUTE_UNUSED,
692 enum reg_class dest_class ATTRIBUTE_UNUSED)
694 int best_cost = -1;
695 int rclass;
696 int regno;
697 enum reg_class best_class = NO_REGS;
698 unsigned int best_size = 0;
699 int cost;
701 for (rclass = 1; rclass < N_REG_CLASSES; rclass++)
703 unsigned int computed_rclass_size = 0;
705 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
707 if (in_hard_reg_set_p (reg_class_contents[rclass], mode, regno)
708 && targetm.hard_regno_mode_ok (regno, mode))
709 computed_rclass_size++;
712 cost = register_move_cost (outer, (enum reg_class) rclass, dest_class);
714 if ((computed_rclass_size > best_size
715 && (best_cost < 0 || best_cost >= cost))
716 || best_cost > cost)
718 best_class = (enum reg_class) rclass;
719 best_size = computed_rclass_size;
720 best_cost = register_move_cost (outer, (enum reg_class) rclass,
721 dest_class);
725 gcc_assert (best_size != 0);
727 #ifdef LIMIT_RELOAD_CLASS
728 best_class = LIMIT_RELOAD_CLASS (mode, best_class);
729 #endif
730 return best_class;
733 /* Return the number of a previously made reload that can be combined with
734 a new one, or n_reloads if none of the existing reloads can be used.
735 OUT, RCLASS, TYPE and OPNUM are the same arguments as passed to
736 push_reload, they determine the kind of the new reload that we try to
737 combine. P_IN points to the corresponding value of IN, which can be
738 modified by this function.
739 DONT_SHARE is nonzero if we can't share any input-only reload for IN. */
741 static int
742 find_reusable_reload (rtx *p_in, rtx out, enum reg_class rclass,
743 enum reload_type type, int opnum, int dont_share)
745 rtx in = *p_in;
746 int i;
747 /* We can't merge two reloads if the output of either one is
748 earlyclobbered. */
750 if (earlyclobber_operand_p (out))
751 return n_reloads;
753 /* We can use an existing reload if the class is right
754 and at least one of IN and OUT is a match
755 and the other is at worst neutral.
756 (A zero compared against anything is neutral.)
758 For targets with small register classes, don't use existing reloads
759 unless they are for the same thing since that can cause us to need
760 more reload registers than we otherwise would. */
762 for (i = 0; i < n_reloads; i++)
763 if ((reg_class_subset_p (rclass, rld[i].rclass)
764 || reg_class_subset_p (rld[i].rclass, rclass))
765 /* If the existing reload has a register, it must fit our class. */
766 && (rld[i].reg_rtx == 0
767 || TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
768 true_regnum (rld[i].reg_rtx)))
769 && ((in != 0 && MATCHES (rld[i].in, in) && ! dont_share
770 && (out == 0 || rld[i].out == 0 || MATCHES (rld[i].out, out)))
771 || (out != 0 && MATCHES (rld[i].out, out)
772 && (in == 0 || rld[i].in == 0 || MATCHES (rld[i].in, in))))
773 && (rld[i].out == 0 || ! earlyclobber_operand_p (rld[i].out))
774 && (small_register_class_p (rclass)
775 || targetm.small_register_classes_for_mode_p (VOIDmode))
776 && MERGABLE_RELOADS (type, rld[i].when_needed, opnum, rld[i].opnum))
777 return i;
779 /* Reloading a plain reg for input can match a reload to postincrement
780 that reg, since the postincrement's value is the right value.
781 Likewise, it can match a preincrement reload, since we regard
782 the preincrementation as happening before any ref in this insn
783 to that register. */
784 for (i = 0; i < n_reloads; i++)
785 if ((reg_class_subset_p (rclass, rld[i].rclass)
786 || reg_class_subset_p (rld[i].rclass, rclass))
787 /* If the existing reload has a register, it must fit our
788 class. */
789 && (rld[i].reg_rtx == 0
790 || TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
791 true_regnum (rld[i].reg_rtx)))
792 && out == 0 && rld[i].out == 0 && rld[i].in != 0
793 && ((REG_P (in)
794 && GET_RTX_CLASS (GET_CODE (rld[i].in)) == RTX_AUTOINC
795 && MATCHES (XEXP (rld[i].in, 0), in))
796 || (REG_P (rld[i].in)
797 && GET_RTX_CLASS (GET_CODE (in)) == RTX_AUTOINC
798 && MATCHES (XEXP (in, 0), rld[i].in)))
799 && (rld[i].out == 0 || ! earlyclobber_operand_p (rld[i].out))
800 && (small_register_class_p (rclass)
801 || targetm.small_register_classes_for_mode_p (VOIDmode))
802 && MERGABLE_RELOADS (type, rld[i].when_needed,
803 opnum, rld[i].opnum))
805 /* Make sure reload_in ultimately has the increment,
806 not the plain register. */
807 if (REG_P (in))
808 *p_in = rld[i].in;
809 return i;
811 return n_reloads;
814 /* Return true if X is a SUBREG that will need reloading of its SUBREG_REG
815 expression. MODE is the mode that X will be used in. OUTPUT is true if
816 the function is invoked for the output part of an enclosing reload. */
818 static bool
819 reload_inner_reg_of_subreg (rtx x, machine_mode mode, bool output)
821 rtx inner;
823 /* Only SUBREGs are problematical. */
824 if (GET_CODE (x) != SUBREG)
825 return false;
827 inner = SUBREG_REG (x);
829 /* If INNER is a constant or PLUS, then INNER will need reloading. */
830 if (CONSTANT_P (inner) || GET_CODE (inner) == PLUS)
831 return true;
833 /* If INNER is not a hard register, then INNER will not need reloading. */
834 if (!(REG_P (inner) && HARD_REGISTER_P (inner)))
835 return false;
837 /* If INNER is not ok for MODE, then INNER will need reloading. */
838 if (!targetm.hard_regno_mode_ok (subreg_regno (x), mode))
839 return true;
841 /* If this is for an output, and the outer part is a word or smaller,
842 INNER is larger than a word and the number of registers in INNER is
843 not the same as the number of words in INNER, then INNER will need
844 reloading (with an in-out reload). */
845 return (output
846 && GET_MODE_SIZE (mode) <= UNITS_PER_WORD
847 && GET_MODE_SIZE (GET_MODE (inner)) > UNITS_PER_WORD
848 && ((GET_MODE_SIZE (GET_MODE (inner)) / UNITS_PER_WORD)
849 != REG_NREGS (inner)));
852 /* Return nonzero if IN can be reloaded into REGNO with mode MODE without
853 requiring an extra reload register. The caller has already found that
854 IN contains some reference to REGNO, so check that we can produce the
855 new value in a single step. E.g. if we have
856 (set (reg r13) (plus (reg r13) (const int 1))), and there is an
857 instruction that adds one to a register, this should succeed.
858 However, if we have something like
859 (set (reg r13) (plus (reg r13) (const int 999))), and the constant 999
860 needs to be loaded into a register first, we need a separate reload
861 register.
862 Such PLUS reloads are generated by find_reload_address_part.
863 The out-of-range PLUS expressions are usually introduced in the instruction
864 patterns by register elimination and substituting pseudos without a home
865 by their function-invariant equivalences. */
866 static int
867 can_reload_into (rtx in, int regno, machine_mode mode)
869 rtx dst;
870 rtx_insn *test_insn;
871 int r = 0;
872 struct recog_data_d save_recog_data;
874 /* For matching constraints, we often get notional input reloads where
875 we want to use the original register as the reload register. I.e.
876 technically this is a non-optional input-output reload, but IN is
877 already a valid register, and has been chosen as the reload register.
878 Speed this up, since it trivially works. */
879 if (REG_P (in))
880 return 1;
882 /* To test MEMs properly, we'd have to take into account all the reloads
883 that are already scheduled, which can become quite complicated.
884 And since we've already handled address reloads for this MEM, it
885 should always succeed anyway. */
886 if (MEM_P (in))
887 return 1;
889 /* If we can make a simple SET insn that does the job, everything should
890 be fine. */
891 dst = gen_rtx_REG (mode, regno);
892 test_insn = make_insn_raw (gen_rtx_SET (dst, in));
893 save_recog_data = recog_data;
894 if (recog_memoized (test_insn) >= 0)
896 extract_insn (test_insn);
897 r = constrain_operands (1, get_enabled_alternatives (test_insn));
899 recog_data = save_recog_data;
900 return r;
903 /* Record one reload that needs to be performed.
904 IN is an rtx saying where the data are to be found before this instruction.
905 OUT says where they must be stored after the instruction.
906 (IN is zero for data not read, and OUT is zero for data not written.)
907 INLOC and OUTLOC point to the places in the instructions where
908 IN and OUT were found.
909 If IN and OUT are both nonzero, it means the same register must be used
910 to reload both IN and OUT.
912 RCLASS is a register class required for the reloaded data.
913 INMODE is the machine mode that the instruction requires
914 for the reg that replaces IN and OUTMODE is likewise for OUT.
916 If IN is zero, then OUT's location and mode should be passed as
917 INLOC and INMODE.
919 STRICT_LOW is the 1 if there is a containing STRICT_LOW_PART rtx.
921 OPTIONAL nonzero means this reload does not need to be performed:
922 it can be discarded if that is more convenient.
924 OPNUM and TYPE say what the purpose of this reload is.
926 The return value is the reload-number for this reload.
928 If both IN and OUT are nonzero, in some rare cases we might
929 want to make two separate reloads. (Actually we never do this now.)
930 Therefore, the reload-number for OUT is stored in
931 output_reloadnum when we return; the return value applies to IN.
932 Usually (presently always), when IN and OUT are nonzero,
933 the two reload-numbers are equal, but the caller should be careful to
934 distinguish them. */
937 push_reload (rtx in, rtx out, rtx *inloc, rtx *outloc,
938 enum reg_class rclass, machine_mode inmode,
939 machine_mode outmode, int strict_low, int optional,
940 int opnum, enum reload_type type)
942 int i;
943 int dont_share = 0;
944 int dont_remove_subreg = 0;
945 #ifdef LIMIT_RELOAD_CLASS
946 rtx *in_subreg_loc = 0, *out_subreg_loc = 0;
947 #endif
948 int secondary_in_reload = -1, secondary_out_reload = -1;
949 enum insn_code secondary_in_icode = CODE_FOR_nothing;
950 enum insn_code secondary_out_icode = CODE_FOR_nothing;
951 enum reg_class subreg_in_class ATTRIBUTE_UNUSED;
952 subreg_in_class = NO_REGS;
954 /* INMODE and/or OUTMODE could be VOIDmode if no mode
955 has been specified for the operand. In that case,
956 use the operand's mode as the mode to reload. */
957 if (inmode == VOIDmode && in != 0)
958 inmode = GET_MODE (in);
959 if (outmode == VOIDmode && out != 0)
960 outmode = GET_MODE (out);
962 /* If find_reloads and friends until now missed to replace a pseudo
963 with a constant of reg_equiv_constant something went wrong
964 beforehand.
965 Note that it can't simply be done here if we missed it earlier
966 since the constant might need to be pushed into the literal pool
967 and the resulting memref would probably need further
968 reloading. */
969 if (in != 0 && REG_P (in))
971 int regno = REGNO (in);
973 gcc_assert (regno < FIRST_PSEUDO_REGISTER
974 || reg_renumber[regno] >= 0
975 || reg_equiv_constant (regno) == NULL_RTX);
978 /* reg_equiv_constant only contains constants which are obviously
979 not appropriate as destination. So if we would need to replace
980 the destination pseudo with a constant we are in real
981 trouble. */
982 if (out != 0 && REG_P (out))
984 int regno = REGNO (out);
986 gcc_assert (regno < FIRST_PSEUDO_REGISTER
987 || reg_renumber[regno] >= 0
988 || reg_equiv_constant (regno) == NULL_RTX);
991 /* If we have a read-write operand with an address side-effect,
992 change either IN or OUT so the side-effect happens only once. */
993 if (in != 0 && out != 0 && MEM_P (in) && rtx_equal_p (in, out))
994 switch (GET_CODE (XEXP (in, 0)))
996 case POST_INC: case POST_DEC: case POST_MODIFY:
997 in = replace_equiv_address_nv (in, XEXP (XEXP (in, 0), 0));
998 break;
1000 case PRE_INC: case PRE_DEC: case PRE_MODIFY:
1001 out = replace_equiv_address_nv (out, XEXP (XEXP (out, 0), 0));
1002 break;
1004 default:
1005 break;
1008 /* If we are reloading a (SUBREG constant ...), really reload just the
1009 inside expression in its own mode. Similarly for (SUBREG (PLUS ...)).
1010 If we have (SUBREG:M1 (MEM:M2 ...) ...) (or an inner REG that is still
1011 a pseudo and hence will become a MEM) with M1 wider than M2 and the
1012 register is a pseudo, also reload the inside expression.
1013 For machines that extend byte loads, do this for any SUBREG of a pseudo
1014 where both M1 and M2 are a word or smaller, M1 is wider than M2, and
1015 M2 is an integral mode that gets extended when loaded.
1016 Similar issue for (SUBREG:M1 (REG:M2 ...) ...) for a hard register R
1017 where either M1 is not valid for R or M2 is wider than a word but we
1018 only need one register to store an M2-sized quantity in R.
1019 (However, if OUT is nonzero, we need to reload the reg *and*
1020 the subreg, so do nothing here, and let following statement handle it.)
1022 Note that the case of (SUBREG (CONST_INT...)...) is handled elsewhere;
1023 we can't handle it here because CONST_INT does not indicate a mode.
1025 Similarly, we must reload the inside expression if we have a
1026 STRICT_LOW_PART (presumably, in == out in this case).
1028 Also reload the inner expression if it does not require a secondary
1029 reload but the SUBREG does.
1031 Finally, reload the inner expression if it is a register that is in
1032 the class whose registers cannot be referenced in a different size
1033 and M1 is not the same size as M2. If subreg_lowpart_p is false, we
1034 cannot reload just the inside since we might end up with the wrong
1035 register class. But if it is inside a STRICT_LOW_PART, we have
1036 no choice, so we hope we do get the right register class there. */
1038 scalar_int_mode inner_mode;
1039 if (in != 0 && GET_CODE (in) == SUBREG
1040 && (subreg_lowpart_p (in) || strict_low)
1041 #ifdef CANNOT_CHANGE_MODE_CLASS
1042 && !CANNOT_CHANGE_MODE_CLASS (GET_MODE (SUBREG_REG (in)), inmode, rclass)
1043 #endif
1044 && contains_allocatable_reg_of_mode[rclass][GET_MODE (SUBREG_REG (in))]
1045 && (CONSTANT_P (SUBREG_REG (in))
1046 || GET_CODE (SUBREG_REG (in)) == PLUS
1047 || strict_low
1048 || (((REG_P (SUBREG_REG (in))
1049 && REGNO (SUBREG_REG (in)) >= FIRST_PSEUDO_REGISTER)
1050 || MEM_P (SUBREG_REG (in)))
1051 && (paradoxical_subreg_p (inmode, GET_MODE (SUBREG_REG (in)))
1052 || (GET_MODE_SIZE (inmode) <= UNITS_PER_WORD
1053 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (in)),
1054 &inner_mode)
1055 && GET_MODE_SIZE (inner_mode) <= UNITS_PER_WORD
1056 && paradoxical_subreg_p (inmode, inner_mode)
1057 && LOAD_EXTEND_OP (inner_mode) != UNKNOWN)
1058 || (WORD_REGISTER_OPERATIONS
1059 && partial_subreg_p (inmode, GET_MODE (SUBREG_REG (in)))
1060 && ((GET_MODE_SIZE (inmode) - 1) / UNITS_PER_WORD ==
1061 ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))) - 1)
1062 / UNITS_PER_WORD)))))
1063 || (REG_P (SUBREG_REG (in))
1064 && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER
1065 /* The case where out is nonzero
1066 is handled differently in the following statement. */
1067 && (out == 0 || subreg_lowpart_p (in))
1068 && ((GET_MODE_SIZE (inmode) <= UNITS_PER_WORD
1069 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1070 > UNITS_PER_WORD)
1071 && ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1072 / UNITS_PER_WORD)
1073 != REG_NREGS (SUBREG_REG (in))))
1074 || !targetm.hard_regno_mode_ok (subreg_regno (in), inmode)))
1075 || (secondary_reload_class (1, rclass, inmode, in) != NO_REGS
1076 && (secondary_reload_class (1, rclass, GET_MODE (SUBREG_REG (in)),
1077 SUBREG_REG (in))
1078 == NO_REGS))
1079 #ifdef CANNOT_CHANGE_MODE_CLASS
1080 || (REG_P (SUBREG_REG (in))
1081 && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER
1082 && REG_CANNOT_CHANGE_MODE_P
1083 (REGNO (SUBREG_REG (in)), GET_MODE (SUBREG_REG (in)), inmode))
1084 #endif
1087 #ifdef LIMIT_RELOAD_CLASS
1088 in_subreg_loc = inloc;
1089 #endif
1090 inloc = &SUBREG_REG (in);
1091 in = *inloc;
1093 if (!WORD_REGISTER_OPERATIONS
1094 && LOAD_EXTEND_OP (GET_MODE (in)) == UNKNOWN
1095 && MEM_P (in))
1096 /* This is supposed to happen only for paradoxical subregs made by
1097 combine.c. (SUBREG (MEM)) isn't supposed to occur other ways. */
1098 gcc_assert (GET_MODE_SIZE (GET_MODE (in)) <= GET_MODE_SIZE (inmode));
1100 inmode = GET_MODE (in);
1103 /* Similar issue for (SUBREG:M1 (REG:M2 ...) ...) for a hard register R
1104 where M1 is not valid for R if it was not handled by the code above.
1106 Similar issue for (SUBREG constant ...) if it was not handled by the
1107 code above. This can happen if SUBREG_BYTE != 0.
1109 However, we must reload the inner reg *as well as* the subreg in
1110 that case. */
1112 if (in != 0 && reload_inner_reg_of_subreg (in, inmode, false))
1114 if (REG_P (SUBREG_REG (in)))
1115 subreg_in_class
1116 = find_valid_class (inmode, GET_MODE (SUBREG_REG (in)),
1117 subreg_regno_offset (REGNO (SUBREG_REG (in)),
1118 GET_MODE (SUBREG_REG (in)),
1119 SUBREG_BYTE (in),
1120 GET_MODE (in)),
1121 REGNO (SUBREG_REG (in)));
1122 else if (CONSTANT_P (SUBREG_REG (in))
1123 || GET_CODE (SUBREG_REG (in)) == PLUS)
1124 subreg_in_class = find_valid_class_1 (inmode,
1125 GET_MODE (SUBREG_REG (in)),
1126 rclass);
1128 /* This relies on the fact that emit_reload_insns outputs the
1129 instructions for input reloads of type RELOAD_OTHER in the same
1130 order as the reloads. Thus if the outer reload is also of type
1131 RELOAD_OTHER, we are guaranteed that this inner reload will be
1132 output before the outer reload. */
1133 push_reload (SUBREG_REG (in), NULL_RTX, &SUBREG_REG (in), (rtx *) 0,
1134 subreg_in_class, VOIDmode, VOIDmode, 0, 0, opnum, type);
1135 dont_remove_subreg = 1;
1138 /* Similarly for paradoxical and problematical SUBREGs on the output.
1139 Note that there is no reason we need worry about the previous value
1140 of SUBREG_REG (out); even if wider than out, storing in a subreg is
1141 entitled to clobber it all (except in the case of a word mode subreg
1142 or of a STRICT_LOW_PART, in that latter case the constraint should
1143 label it input-output.) */
1144 if (out != 0 && GET_CODE (out) == SUBREG
1145 && (subreg_lowpart_p (out) || strict_low)
1146 #ifdef CANNOT_CHANGE_MODE_CLASS
1147 && !CANNOT_CHANGE_MODE_CLASS (GET_MODE (SUBREG_REG (out)), outmode, rclass)
1148 #endif
1149 && contains_allocatable_reg_of_mode[rclass][GET_MODE (SUBREG_REG (out))]
1150 && (CONSTANT_P (SUBREG_REG (out))
1151 || strict_low
1152 || (((REG_P (SUBREG_REG (out))
1153 && REGNO (SUBREG_REG (out)) >= FIRST_PSEUDO_REGISTER)
1154 || MEM_P (SUBREG_REG (out)))
1155 && (paradoxical_subreg_p (outmode, GET_MODE (SUBREG_REG (out)))
1156 || (WORD_REGISTER_OPERATIONS
1157 && partial_subreg_p (outmode, GET_MODE (SUBREG_REG (out)))
1158 && ((GET_MODE_SIZE (outmode) - 1) / UNITS_PER_WORD ==
1159 ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))) - 1)
1160 / UNITS_PER_WORD)))))
1161 || (REG_P (SUBREG_REG (out))
1162 && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER
1163 /* The case of a word mode subreg
1164 is handled differently in the following statement. */
1165 && ! (GET_MODE_SIZE (outmode) <= UNITS_PER_WORD
1166 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (out)))
1167 > UNITS_PER_WORD))
1168 && !targetm.hard_regno_mode_ok (subreg_regno (out), outmode))
1169 || (secondary_reload_class (0, rclass, outmode, out) != NO_REGS
1170 && (secondary_reload_class (0, rclass, GET_MODE (SUBREG_REG (out)),
1171 SUBREG_REG (out))
1172 == NO_REGS))
1173 #ifdef CANNOT_CHANGE_MODE_CLASS
1174 || (REG_P (SUBREG_REG (out))
1175 && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER
1176 && REG_CANNOT_CHANGE_MODE_P (REGNO (SUBREG_REG (out)),
1177 GET_MODE (SUBREG_REG (out)),
1178 outmode))
1179 #endif
1182 #ifdef LIMIT_RELOAD_CLASS
1183 out_subreg_loc = outloc;
1184 #endif
1185 outloc = &SUBREG_REG (out);
1186 out = *outloc;
1187 gcc_assert (WORD_REGISTER_OPERATIONS || !MEM_P (out)
1188 || GET_MODE_SIZE (GET_MODE (out))
1189 <= GET_MODE_SIZE (outmode));
1190 outmode = GET_MODE (out);
1193 /* Similar issue for (SUBREG:M1 (REG:M2 ...) ...) for a hard register R
1194 where either M1 is not valid for R or M2 is wider than a word but we
1195 only need one register to store an M2-sized quantity in R.
1197 However, we must reload the inner reg *as well as* the subreg in
1198 that case and the inner reg is an in-out reload. */
1200 if (out != 0 && reload_inner_reg_of_subreg (out, outmode, true))
1202 enum reg_class in_out_class
1203 = find_valid_class (outmode, GET_MODE (SUBREG_REG (out)),
1204 subreg_regno_offset (REGNO (SUBREG_REG (out)),
1205 GET_MODE (SUBREG_REG (out)),
1206 SUBREG_BYTE (out),
1207 GET_MODE (out)),
1208 REGNO (SUBREG_REG (out)));
1210 /* This relies on the fact that emit_reload_insns outputs the
1211 instructions for output reloads of type RELOAD_OTHER in reverse
1212 order of the reloads. Thus if the outer reload is also of type
1213 RELOAD_OTHER, we are guaranteed that this inner reload will be
1214 output after the outer reload. */
1215 push_reload (SUBREG_REG (out), SUBREG_REG (out), &SUBREG_REG (out),
1216 &SUBREG_REG (out), in_out_class, VOIDmode, VOIDmode,
1217 0, 0, opnum, RELOAD_OTHER);
1218 dont_remove_subreg = 1;
1221 /* If IN appears in OUT, we can't share any input-only reload for IN. */
1222 if (in != 0 && out != 0 && MEM_P (out)
1223 && (REG_P (in) || MEM_P (in) || GET_CODE (in) == PLUS)
1224 && reg_overlap_mentioned_for_reload_p (in, XEXP (out, 0)))
1225 dont_share = 1;
1227 /* If IN is a SUBREG of a hard register, make a new REG. This
1228 simplifies some of the cases below. */
1230 if (in != 0 && GET_CODE (in) == SUBREG && REG_P (SUBREG_REG (in))
1231 && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER
1232 && ! dont_remove_subreg)
1233 in = gen_rtx_REG (GET_MODE (in), subreg_regno (in));
1235 /* Similarly for OUT. */
1236 if (out != 0 && GET_CODE (out) == SUBREG
1237 && REG_P (SUBREG_REG (out))
1238 && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER
1239 && ! dont_remove_subreg)
1240 out = gen_rtx_REG (GET_MODE (out), subreg_regno (out));
1242 /* Narrow down the class of register wanted if that is
1243 desirable on this machine for efficiency. */
1245 reg_class_t preferred_class = rclass;
1247 if (in != 0)
1248 preferred_class = targetm.preferred_reload_class (in, rclass);
1250 /* Output reloads may need analogous treatment, different in detail. */
1251 if (out != 0)
1252 preferred_class
1253 = targetm.preferred_output_reload_class (out, preferred_class);
1255 /* Discard what the target said if we cannot do it. */
1256 if (preferred_class != NO_REGS
1257 || (optional && type == RELOAD_FOR_OUTPUT))
1258 rclass = (enum reg_class) preferred_class;
1261 /* Make sure we use a class that can handle the actual pseudo
1262 inside any subreg. For example, on the 386, QImode regs
1263 can appear within SImode subregs. Although GENERAL_REGS
1264 can handle SImode, QImode needs a smaller class. */
1265 #ifdef LIMIT_RELOAD_CLASS
1266 if (in_subreg_loc)
1267 rclass = LIMIT_RELOAD_CLASS (inmode, rclass);
1268 else if (in != 0 && GET_CODE (in) == SUBREG)
1269 rclass = LIMIT_RELOAD_CLASS (GET_MODE (SUBREG_REG (in)), rclass);
1271 if (out_subreg_loc)
1272 rclass = LIMIT_RELOAD_CLASS (outmode, rclass);
1273 if (out != 0 && GET_CODE (out) == SUBREG)
1274 rclass = LIMIT_RELOAD_CLASS (GET_MODE (SUBREG_REG (out)), rclass);
1275 #endif
1277 /* Verify that this class is at least possible for the mode that
1278 is specified. */
1279 if (this_insn_is_asm)
1281 machine_mode mode;
1282 if (paradoxical_subreg_p (inmode, outmode))
1283 mode = inmode;
1284 else
1285 mode = outmode;
1286 if (mode == VOIDmode)
1288 error_for_asm (this_insn, "cannot reload integer constant "
1289 "operand in %<asm%>");
1290 mode = word_mode;
1291 if (in != 0)
1292 inmode = word_mode;
1293 if (out != 0)
1294 outmode = word_mode;
1296 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1297 if (targetm.hard_regno_mode_ok (i, mode)
1298 && in_hard_reg_set_p (reg_class_contents[(int) rclass], mode, i))
1299 break;
1300 if (i == FIRST_PSEUDO_REGISTER)
1302 error_for_asm (this_insn, "impossible register constraint "
1303 "in %<asm%>");
1304 /* Avoid further trouble with this insn. */
1305 PATTERN (this_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
1306 /* We used to continue here setting class to ALL_REGS, but it triggers
1307 sanity check on i386 for:
1308 void foo(long double d)
1310 asm("" :: "a" (d));
1312 Returning zero here ought to be safe as we take care in
1313 find_reloads to not process the reloads when instruction was
1314 replaced by USE. */
1316 return 0;
1320 /* Optional output reloads are always OK even if we have no register class,
1321 since the function of these reloads is only to have spill_reg_store etc.
1322 set, so that the storing insn can be deleted later. */
1323 gcc_assert (rclass != NO_REGS
1324 || (optional != 0 && type == RELOAD_FOR_OUTPUT));
1326 i = find_reusable_reload (&in, out, rclass, type, opnum, dont_share);
1328 if (i == n_reloads)
1330 /* See if we need a secondary reload register to move between CLASS
1331 and IN or CLASS and OUT. Get the icode and push any required reloads
1332 needed for each of them if so. */
1334 if (in != 0)
1335 secondary_in_reload
1336 = push_secondary_reload (1, in, opnum, optional, rclass, inmode, type,
1337 &secondary_in_icode, NULL);
1338 if (out != 0 && GET_CODE (out) != SCRATCH)
1339 secondary_out_reload
1340 = push_secondary_reload (0, out, opnum, optional, rclass, outmode,
1341 type, &secondary_out_icode, NULL);
1343 /* We found no existing reload suitable for re-use.
1344 So add an additional reload. */
1346 if (subreg_in_class == NO_REGS
1347 && in != 0
1348 && (REG_P (in)
1349 || (GET_CODE (in) == SUBREG && REG_P (SUBREG_REG (in))))
1350 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER)
1351 subreg_in_class = REGNO_REG_CLASS (reg_or_subregno (in));
1352 /* If a memory location is needed for the copy, make one. */
1353 if (subreg_in_class != NO_REGS
1354 && targetm.secondary_memory_needed (inmode, subreg_in_class, rclass))
1355 get_secondary_mem (in, inmode, opnum, type);
1357 i = n_reloads;
1358 rld[i].in = in;
1359 rld[i].out = out;
1360 rld[i].rclass = rclass;
1361 rld[i].inmode = inmode;
1362 rld[i].outmode = outmode;
1363 rld[i].reg_rtx = 0;
1364 rld[i].optional = optional;
1365 rld[i].inc = 0;
1366 rld[i].nocombine = 0;
1367 rld[i].in_reg = inloc ? *inloc : 0;
1368 rld[i].out_reg = outloc ? *outloc : 0;
1369 rld[i].opnum = opnum;
1370 rld[i].when_needed = type;
1371 rld[i].secondary_in_reload = secondary_in_reload;
1372 rld[i].secondary_out_reload = secondary_out_reload;
1373 rld[i].secondary_in_icode = secondary_in_icode;
1374 rld[i].secondary_out_icode = secondary_out_icode;
1375 rld[i].secondary_p = 0;
1377 n_reloads++;
1379 if (out != 0
1380 && (REG_P (out)
1381 || (GET_CODE (out) == SUBREG && REG_P (SUBREG_REG (out))))
1382 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
1383 && (targetm.secondary_memory_needed
1384 (outmode, rclass, REGNO_REG_CLASS (reg_or_subregno (out)))))
1385 get_secondary_mem (out, outmode, opnum, type);
1387 else
1389 /* We are reusing an existing reload,
1390 but we may have additional information for it.
1391 For example, we may now have both IN and OUT
1392 while the old one may have just one of them. */
1394 /* The modes can be different. If they are, we want to reload in
1395 the larger mode, so that the value is valid for both modes. */
1396 if (inmode != VOIDmode
1397 && partial_subreg_p (rld[i].inmode, inmode))
1398 rld[i].inmode = inmode;
1399 if (outmode != VOIDmode
1400 && partial_subreg_p (rld[i].outmode, outmode))
1401 rld[i].outmode = outmode;
1402 if (in != 0)
1404 rtx in_reg = inloc ? *inloc : 0;
1405 /* If we merge reloads for two distinct rtl expressions that
1406 are identical in content, there might be duplicate address
1407 reloads. Remove the extra set now, so that if we later find
1408 that we can inherit this reload, we can get rid of the
1409 address reloads altogether.
1411 Do not do this if both reloads are optional since the result
1412 would be an optional reload which could potentially leave
1413 unresolved address replacements.
1415 It is not sufficient to call transfer_replacements since
1416 choose_reload_regs will remove the replacements for address
1417 reloads of inherited reloads which results in the same
1418 problem. */
1419 if (rld[i].in != in && rtx_equal_p (in, rld[i].in)
1420 && ! (rld[i].optional && optional))
1422 /* We must keep the address reload with the lower operand
1423 number alive. */
1424 if (opnum > rld[i].opnum)
1426 remove_address_replacements (in);
1427 in = rld[i].in;
1428 in_reg = rld[i].in_reg;
1430 else
1431 remove_address_replacements (rld[i].in);
1433 /* When emitting reloads we don't necessarily look at the in-
1434 and outmode, but also directly at the operands (in and out).
1435 So we can't simply overwrite them with whatever we have found
1436 for this (to-be-merged) reload, we have to "merge" that too.
1437 Reusing another reload already verified that we deal with the
1438 same operands, just possibly in different modes. So we
1439 overwrite the operands only when the new mode is larger.
1440 See also PR33613. */
1441 if (!rld[i].in
1442 || partial_subreg_p (GET_MODE (rld[i].in), GET_MODE (in)))
1443 rld[i].in = in;
1444 if (!rld[i].in_reg
1445 || (in_reg
1446 && partial_subreg_p (GET_MODE (rld[i].in_reg),
1447 GET_MODE (in_reg))))
1448 rld[i].in_reg = in_reg;
1450 if (out != 0)
1452 if (!rld[i].out
1453 || (out
1454 && partial_subreg_p (GET_MODE (rld[i].out),
1455 GET_MODE (out))))
1456 rld[i].out = out;
1457 if (outloc
1458 && (!rld[i].out_reg
1459 || partial_subreg_p (GET_MODE (rld[i].out_reg),
1460 GET_MODE (*outloc))))
1461 rld[i].out_reg = *outloc;
1463 if (reg_class_subset_p (rclass, rld[i].rclass))
1464 rld[i].rclass = rclass;
1465 rld[i].optional &= optional;
1466 if (MERGE_TO_OTHER (type, rld[i].when_needed,
1467 opnum, rld[i].opnum))
1468 rld[i].when_needed = RELOAD_OTHER;
1469 rld[i].opnum = MIN (rld[i].opnum, opnum);
1472 /* If the ostensible rtx being reloaded differs from the rtx found
1473 in the location to substitute, this reload is not safe to combine
1474 because we cannot reliably tell whether it appears in the insn. */
1476 if (in != 0 && in != *inloc)
1477 rld[i].nocombine = 1;
1479 #if 0
1480 /* This was replaced by changes in find_reloads_address_1 and the new
1481 function inc_for_reload, which go with a new meaning of reload_inc. */
1483 /* If this is an IN/OUT reload in an insn that sets the CC,
1484 it must be for an autoincrement. It doesn't work to store
1485 the incremented value after the insn because that would clobber the CC.
1486 So we must do the increment of the value reloaded from,
1487 increment it, store it back, then decrement again. */
1488 if (out != 0 && sets_cc0_p (PATTERN (this_insn)))
1490 out = 0;
1491 rld[i].out = 0;
1492 rld[i].inc = find_inc_amount (PATTERN (this_insn), in);
1493 /* If we did not find a nonzero amount-to-increment-by,
1494 that contradicts the belief that IN is being incremented
1495 in an address in this insn. */
1496 gcc_assert (rld[i].inc != 0);
1498 #endif
1500 /* If we will replace IN and OUT with the reload-reg,
1501 record where they are located so that substitution need
1502 not do a tree walk. */
1504 if (replace_reloads)
1506 if (inloc != 0)
1508 struct replacement *r = &replacements[n_replacements++];
1509 r->what = i;
1510 r->where = inloc;
1511 r->mode = inmode;
1513 if (outloc != 0 && outloc != inloc)
1515 struct replacement *r = &replacements[n_replacements++];
1516 r->what = i;
1517 r->where = outloc;
1518 r->mode = outmode;
1522 /* If this reload is just being introduced and it has both
1523 an incoming quantity and an outgoing quantity that are
1524 supposed to be made to match, see if either one of the two
1525 can serve as the place to reload into.
1527 If one of them is acceptable, set rld[i].reg_rtx
1528 to that one. */
1530 if (in != 0 && out != 0 && in != out && rld[i].reg_rtx == 0)
1532 rld[i].reg_rtx = find_dummy_reload (in, out, inloc, outloc,
1533 inmode, outmode,
1534 rld[i].rclass, i,
1535 earlyclobber_operand_p (out));
1537 /* If the outgoing register already contains the same value
1538 as the incoming one, we can dispense with loading it.
1539 The easiest way to tell the caller that is to give a phony
1540 value for the incoming operand (same as outgoing one). */
1541 if (rld[i].reg_rtx == out
1542 && (REG_P (in) || CONSTANT_P (in))
1543 && 0 != find_equiv_reg (in, this_insn, NO_REGS, REGNO (out),
1544 static_reload_reg_p, i, inmode))
1545 rld[i].in = out;
1548 /* If this is an input reload and the operand contains a register that
1549 dies in this insn and is used nowhere else, see if it is the right class
1550 to be used for this reload. Use it if so. (This occurs most commonly
1551 in the case of paradoxical SUBREGs and in-out reloads). We cannot do
1552 this if it is also an output reload that mentions the register unless
1553 the output is a SUBREG that clobbers an entire register.
1555 Note that the operand might be one of the spill regs, if it is a
1556 pseudo reg and we are in a block where spilling has not taken place.
1557 But if there is no spilling in this block, that is OK.
1558 An explicitly used hard reg cannot be a spill reg. */
1560 if (rld[i].reg_rtx == 0 && in != 0 && hard_regs_live_known)
1562 rtx note;
1563 int regno;
1564 machine_mode rel_mode = inmode;
1566 if (out && partial_subreg_p (rel_mode, outmode))
1567 rel_mode = outmode;
1569 for (note = REG_NOTES (this_insn); note; note = XEXP (note, 1))
1570 if (REG_NOTE_KIND (note) == REG_DEAD
1571 && REG_P (XEXP (note, 0))
1572 && (regno = REGNO (XEXP (note, 0))) < FIRST_PSEUDO_REGISTER
1573 && reg_mentioned_p (XEXP (note, 0), in)
1574 /* Check that a former pseudo is valid; see find_dummy_reload. */
1575 && (ORIGINAL_REGNO (XEXP (note, 0)) < FIRST_PSEUDO_REGISTER
1576 || (! bitmap_bit_p (DF_LR_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
1577 ORIGINAL_REGNO (XEXP (note, 0)))
1578 && REG_NREGS (XEXP (note, 0)) == 1))
1579 && ! refers_to_regno_for_reload_p (regno,
1580 end_hard_regno (rel_mode,
1581 regno),
1582 PATTERN (this_insn), inloc)
1583 && ! find_reg_fusage (this_insn, USE, XEXP (note, 0))
1584 /* If this is also an output reload, IN cannot be used as
1585 the reload register if it is set in this insn unless IN
1586 is also OUT. */
1587 && (out == 0 || in == out
1588 || ! hard_reg_set_here_p (regno,
1589 end_hard_regno (rel_mode, regno),
1590 PATTERN (this_insn)))
1591 /* ??? Why is this code so different from the previous?
1592 Is there any simple coherent way to describe the two together?
1593 What's going on here. */
1594 && (in != out
1595 || (GET_CODE (in) == SUBREG
1596 && (((GET_MODE_SIZE (GET_MODE (in)) + (UNITS_PER_WORD - 1))
1597 / UNITS_PER_WORD)
1598 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1599 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
1600 /* Make sure the operand fits in the reg that dies. */
1601 && (GET_MODE_SIZE (rel_mode)
1602 <= GET_MODE_SIZE (GET_MODE (XEXP (note, 0))))
1603 && targetm.hard_regno_mode_ok (regno, inmode)
1604 && targetm.hard_regno_mode_ok (regno, outmode))
1606 unsigned int offs;
1607 unsigned int nregs = MAX (hard_regno_nregs (regno, inmode),
1608 hard_regno_nregs (regno, outmode));
1610 for (offs = 0; offs < nregs; offs++)
1611 if (fixed_regs[regno + offs]
1612 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
1613 regno + offs))
1614 break;
1616 if (offs == nregs
1617 && (! (refers_to_regno_for_reload_p
1618 (regno, end_hard_regno (inmode, regno), in, (rtx *) 0))
1619 || can_reload_into (in, regno, inmode)))
1621 rld[i].reg_rtx = gen_rtx_REG (rel_mode, regno);
1622 break;
1627 if (out)
1628 output_reloadnum = i;
1630 return i;
1633 /* Record an additional place we must replace a value
1634 for which we have already recorded a reload.
1635 RELOADNUM is the value returned by push_reload
1636 when the reload was recorded.
1637 This is used in insn patterns that use match_dup. */
1639 static void
1640 push_replacement (rtx *loc, int reloadnum, machine_mode mode)
1642 if (replace_reloads)
1644 struct replacement *r = &replacements[n_replacements++];
1645 r->what = reloadnum;
1646 r->where = loc;
1647 r->mode = mode;
1651 /* Duplicate any replacement we have recorded to apply at
1652 location ORIG_LOC to also be performed at DUP_LOC.
1653 This is used in insn patterns that use match_dup. */
1655 static void
1656 dup_replacements (rtx *dup_loc, rtx *orig_loc)
1658 int i, n = n_replacements;
1660 for (i = 0; i < n; i++)
1662 struct replacement *r = &replacements[i];
1663 if (r->where == orig_loc)
1664 push_replacement (dup_loc, r->what, r->mode);
1668 /* Transfer all replacements that used to be in reload FROM to be in
1669 reload TO. */
1671 void
1672 transfer_replacements (int to, int from)
1674 int i;
1676 for (i = 0; i < n_replacements; i++)
1677 if (replacements[i].what == from)
1678 replacements[i].what = to;
1681 /* IN_RTX is the value loaded by a reload that we now decided to inherit,
1682 or a subpart of it. If we have any replacements registered for IN_RTX,
1683 cancel the reloads that were supposed to load them.
1684 Return nonzero if we canceled any reloads. */
1686 remove_address_replacements (rtx in_rtx)
1688 int i, j;
1689 char reload_flags[MAX_RELOADS];
1690 int something_changed = 0;
1692 memset (reload_flags, 0, sizeof reload_flags);
1693 for (i = 0, j = 0; i < n_replacements; i++)
1695 if (loc_mentioned_in_p (replacements[i].where, in_rtx))
1696 reload_flags[replacements[i].what] |= 1;
1697 else
1699 replacements[j++] = replacements[i];
1700 reload_flags[replacements[i].what] |= 2;
1703 /* Note that the following store must be done before the recursive calls. */
1704 n_replacements = j;
1706 for (i = n_reloads - 1; i >= 0; i--)
1708 if (reload_flags[i] == 1)
1710 deallocate_reload_reg (i);
1711 remove_address_replacements (rld[i].in);
1712 rld[i].in = 0;
1713 something_changed = 1;
1716 return something_changed;
1719 /* If there is only one output reload, and it is not for an earlyclobber
1720 operand, try to combine it with a (logically unrelated) input reload
1721 to reduce the number of reload registers needed.
1723 This is safe if the input reload does not appear in
1724 the value being output-reloaded, because this implies
1725 it is not needed any more once the original insn completes.
1727 If that doesn't work, see we can use any of the registers that
1728 die in this insn as a reload register. We can if it is of the right
1729 class and does not appear in the value being output-reloaded. */
1731 static void
1732 combine_reloads (void)
1734 int i, regno;
1735 int output_reload = -1;
1736 int secondary_out = -1;
1737 rtx note;
1739 /* Find the output reload; return unless there is exactly one
1740 and that one is mandatory. */
1742 for (i = 0; i < n_reloads; i++)
1743 if (rld[i].out != 0)
1745 if (output_reload >= 0)
1746 return;
1747 output_reload = i;
1750 if (output_reload < 0 || rld[output_reload].optional)
1751 return;
1753 /* An input-output reload isn't combinable. */
1755 if (rld[output_reload].in != 0)
1756 return;
1758 /* If this reload is for an earlyclobber operand, we can't do anything. */
1759 if (earlyclobber_operand_p (rld[output_reload].out))
1760 return;
1762 /* If there is a reload for part of the address of this operand, we would
1763 need to change it to RELOAD_FOR_OTHER_ADDRESS. But that would extend
1764 its life to the point where doing this combine would not lower the
1765 number of spill registers needed. */
1766 for (i = 0; i < n_reloads; i++)
1767 if ((rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
1768 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
1769 && rld[i].opnum == rld[output_reload].opnum)
1770 return;
1772 /* Check each input reload; can we combine it? */
1774 for (i = 0; i < n_reloads; i++)
1775 if (rld[i].in && ! rld[i].optional && ! rld[i].nocombine
1776 /* Life span of this reload must not extend past main insn. */
1777 && rld[i].when_needed != RELOAD_FOR_OUTPUT_ADDRESS
1778 && rld[i].when_needed != RELOAD_FOR_OUTADDR_ADDRESS
1779 && rld[i].when_needed != RELOAD_OTHER
1780 && (ira_reg_class_max_nregs [(int)rld[i].rclass][(int) rld[i].inmode]
1781 == ira_reg_class_max_nregs [(int) rld[output_reload].rclass]
1782 [(int) rld[output_reload].outmode])
1783 && rld[i].inc == 0
1784 && rld[i].reg_rtx == 0
1785 /* Don't combine two reloads with different secondary
1786 memory locations. */
1787 && (secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[i].opnum] == 0
1788 || secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum] == 0
1789 || rtx_equal_p (secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[i].opnum],
1790 secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum]))
1791 && (targetm.small_register_classes_for_mode_p (VOIDmode)
1792 ? (rld[i].rclass == rld[output_reload].rclass)
1793 : (reg_class_subset_p (rld[i].rclass,
1794 rld[output_reload].rclass)
1795 || reg_class_subset_p (rld[output_reload].rclass,
1796 rld[i].rclass)))
1797 && (MATCHES (rld[i].in, rld[output_reload].out)
1798 /* Args reversed because the first arg seems to be
1799 the one that we imagine being modified
1800 while the second is the one that might be affected. */
1801 || (! reg_overlap_mentioned_for_reload_p (rld[output_reload].out,
1802 rld[i].in)
1803 /* However, if the input is a register that appears inside
1804 the output, then we also can't share.
1805 Imagine (set (mem (reg 69)) (plus (reg 69) ...)).
1806 If the same reload reg is used for both reg 69 and the
1807 result to be stored in memory, then that result
1808 will clobber the address of the memory ref. */
1809 && ! (REG_P (rld[i].in)
1810 && reg_overlap_mentioned_for_reload_p (rld[i].in,
1811 rld[output_reload].out))))
1812 && ! reload_inner_reg_of_subreg (rld[i].in, rld[i].inmode,
1813 rld[i].when_needed != RELOAD_FOR_INPUT)
1814 && (reg_class_size[(int) rld[i].rclass]
1815 || targetm.small_register_classes_for_mode_p (VOIDmode))
1816 /* We will allow making things slightly worse by combining an
1817 input and an output, but no worse than that. */
1818 && (rld[i].when_needed == RELOAD_FOR_INPUT
1819 || rld[i].when_needed == RELOAD_FOR_OUTPUT))
1821 int j;
1823 /* We have found a reload to combine with! */
1824 rld[i].out = rld[output_reload].out;
1825 rld[i].out_reg = rld[output_reload].out_reg;
1826 rld[i].outmode = rld[output_reload].outmode;
1827 /* Mark the old output reload as inoperative. */
1828 rld[output_reload].out = 0;
1829 /* The combined reload is needed for the entire insn. */
1830 rld[i].when_needed = RELOAD_OTHER;
1831 /* If the output reload had a secondary reload, copy it. */
1832 if (rld[output_reload].secondary_out_reload != -1)
1834 rld[i].secondary_out_reload
1835 = rld[output_reload].secondary_out_reload;
1836 rld[i].secondary_out_icode
1837 = rld[output_reload].secondary_out_icode;
1840 /* Copy any secondary MEM. */
1841 if (secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum] != 0)
1842 secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[i].opnum]
1843 = secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum];
1844 /* If required, minimize the register class. */
1845 if (reg_class_subset_p (rld[output_reload].rclass,
1846 rld[i].rclass))
1847 rld[i].rclass = rld[output_reload].rclass;
1849 /* Transfer all replacements from the old reload to the combined. */
1850 for (j = 0; j < n_replacements; j++)
1851 if (replacements[j].what == output_reload)
1852 replacements[j].what = i;
1854 return;
1857 /* If this insn has only one operand that is modified or written (assumed
1858 to be the first), it must be the one corresponding to this reload. It
1859 is safe to use anything that dies in this insn for that output provided
1860 that it does not occur in the output (we already know it isn't an
1861 earlyclobber. If this is an asm insn, give up. */
1863 if (INSN_CODE (this_insn) == -1)
1864 return;
1866 for (i = 1; i < insn_data[INSN_CODE (this_insn)].n_operands; i++)
1867 if (insn_data[INSN_CODE (this_insn)].operand[i].constraint[0] == '='
1868 || insn_data[INSN_CODE (this_insn)].operand[i].constraint[0] == '+')
1869 return;
1871 /* See if some hard register that dies in this insn and is not used in
1872 the output is the right class. Only works if the register we pick
1873 up can fully hold our output reload. */
1874 for (note = REG_NOTES (this_insn); note; note = XEXP (note, 1))
1875 if (REG_NOTE_KIND (note) == REG_DEAD
1876 && REG_P (XEXP (note, 0))
1877 && !reg_overlap_mentioned_for_reload_p (XEXP (note, 0),
1878 rld[output_reload].out)
1879 && (regno = REGNO (XEXP (note, 0))) < FIRST_PSEUDO_REGISTER
1880 && targetm.hard_regno_mode_ok (regno, rld[output_reload].outmode)
1881 && TEST_HARD_REG_BIT (reg_class_contents[(int) rld[output_reload].rclass],
1882 regno)
1883 && (hard_regno_nregs (regno, rld[output_reload].outmode)
1884 <= REG_NREGS (XEXP (note, 0)))
1885 /* Ensure that a secondary or tertiary reload for this output
1886 won't want this register. */
1887 && ((secondary_out = rld[output_reload].secondary_out_reload) == -1
1888 || (!(TEST_HARD_REG_BIT
1889 (reg_class_contents[(int) rld[secondary_out].rclass], regno))
1890 && ((secondary_out = rld[secondary_out].secondary_out_reload) == -1
1891 || !(TEST_HARD_REG_BIT
1892 (reg_class_contents[(int) rld[secondary_out].rclass],
1893 regno)))))
1894 && !fixed_regs[regno]
1895 /* Check that a former pseudo is valid; see find_dummy_reload. */
1896 && (ORIGINAL_REGNO (XEXP (note, 0)) < FIRST_PSEUDO_REGISTER
1897 || (!bitmap_bit_p (DF_LR_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
1898 ORIGINAL_REGNO (XEXP (note, 0)))
1899 && REG_NREGS (XEXP (note, 0)) == 1)))
1901 rld[output_reload].reg_rtx
1902 = gen_rtx_REG (rld[output_reload].outmode, regno);
1903 return;
1907 /* Try to find a reload register for an in-out reload (expressions IN and OUT).
1908 See if one of IN and OUT is a register that may be used;
1909 this is desirable since a spill-register won't be needed.
1910 If so, return the register rtx that proves acceptable.
1912 INLOC and OUTLOC are locations where IN and OUT appear in the insn.
1913 RCLASS is the register class required for the reload.
1915 If FOR_REAL is >= 0, it is the number of the reload,
1916 and in some cases when it can be discovered that OUT doesn't need
1917 to be computed, clear out rld[FOR_REAL].out.
1919 If FOR_REAL is -1, this should not be done, because this call
1920 is just to see if a register can be found, not to find and install it.
1922 EARLYCLOBBER is nonzero if OUT is an earlyclobber operand. This
1923 puts an additional constraint on being able to use IN for OUT since
1924 IN must not appear elsewhere in the insn (it is assumed that IN itself
1925 is safe from the earlyclobber). */
1927 static rtx
1928 find_dummy_reload (rtx real_in, rtx real_out, rtx *inloc, rtx *outloc,
1929 machine_mode inmode, machine_mode outmode,
1930 reg_class_t rclass, int for_real, int earlyclobber)
1932 rtx in = real_in;
1933 rtx out = real_out;
1934 int in_offset = 0;
1935 int out_offset = 0;
1936 rtx value = 0;
1938 /* If operands exceed a word, we can't use either of them
1939 unless they have the same size. */
1940 if (GET_MODE_SIZE (outmode) != GET_MODE_SIZE (inmode)
1941 && (GET_MODE_SIZE (outmode) > UNITS_PER_WORD
1942 || GET_MODE_SIZE (inmode) > UNITS_PER_WORD))
1943 return 0;
1945 /* Note that {in,out}_offset are needed only when 'in' or 'out'
1946 respectively refers to a hard register. */
1948 /* Find the inside of any subregs. */
1949 while (GET_CODE (out) == SUBREG)
1951 if (REG_P (SUBREG_REG (out))
1952 && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER)
1953 out_offset += subreg_regno_offset (REGNO (SUBREG_REG (out)),
1954 GET_MODE (SUBREG_REG (out)),
1955 SUBREG_BYTE (out),
1956 GET_MODE (out));
1957 out = SUBREG_REG (out);
1959 while (GET_CODE (in) == SUBREG)
1961 if (REG_P (SUBREG_REG (in))
1962 && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER)
1963 in_offset += subreg_regno_offset (REGNO (SUBREG_REG (in)),
1964 GET_MODE (SUBREG_REG (in)),
1965 SUBREG_BYTE (in),
1966 GET_MODE (in));
1967 in = SUBREG_REG (in);
1970 /* Narrow down the reg class, the same way push_reload will;
1971 otherwise we might find a dummy now, but push_reload won't. */
1973 reg_class_t preferred_class = targetm.preferred_reload_class (in, rclass);
1974 if (preferred_class != NO_REGS)
1975 rclass = (enum reg_class) preferred_class;
1978 /* See if OUT will do. */
1979 if (REG_P (out)
1980 && REGNO (out) < FIRST_PSEUDO_REGISTER)
1982 unsigned int regno = REGNO (out) + out_offset;
1983 unsigned int nwords = hard_regno_nregs (regno, outmode);
1984 rtx saved_rtx;
1986 /* When we consider whether the insn uses OUT,
1987 ignore references within IN. They don't prevent us
1988 from copying IN into OUT, because those refs would
1989 move into the insn that reloads IN.
1991 However, we only ignore IN in its role as this reload.
1992 If the insn uses IN elsewhere and it contains OUT,
1993 that counts. We can't be sure it's the "same" operand
1994 so it might not go through this reload.
1996 We also need to avoid using OUT if it, or part of it, is a
1997 fixed register. Modifying such registers, even transiently,
1998 may have undefined effects on the machine, such as modifying
1999 the stack pointer. */
2000 saved_rtx = *inloc;
2001 *inloc = const0_rtx;
2003 if (regno < FIRST_PSEUDO_REGISTER
2004 && targetm.hard_regno_mode_ok (regno, outmode)
2005 && ! refers_to_regno_for_reload_p (regno, regno + nwords,
2006 PATTERN (this_insn), outloc))
2008 unsigned int i;
2010 for (i = 0; i < nwords; i++)
2011 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
2012 regno + i)
2013 || fixed_regs[regno + i])
2014 break;
2016 if (i == nwords)
2018 if (REG_P (real_out))
2019 value = real_out;
2020 else
2021 value = gen_rtx_REG (outmode, regno);
2025 *inloc = saved_rtx;
2028 /* Consider using IN if OUT was not acceptable
2029 or if OUT dies in this insn (like the quotient in a divmod insn).
2030 We can't use IN unless it is dies in this insn,
2031 which means we must know accurately which hard regs are live.
2032 Also, the result can't go in IN if IN is used within OUT,
2033 or if OUT is an earlyclobber and IN appears elsewhere in the insn. */
2034 if (hard_regs_live_known
2035 && REG_P (in)
2036 && REGNO (in) < FIRST_PSEUDO_REGISTER
2037 && (value == 0
2038 || find_reg_note (this_insn, REG_UNUSED, real_out))
2039 && find_reg_note (this_insn, REG_DEAD, real_in)
2040 && !fixed_regs[REGNO (in)]
2041 && targetm.hard_regno_mode_ok (REGNO (in),
2042 /* The only case where out and real_out
2043 might have different modes is where
2044 real_out is a subreg, and in that
2045 case, out has a real mode. */
2046 (GET_MODE (out) != VOIDmode
2047 ? GET_MODE (out) : outmode))
2048 && (ORIGINAL_REGNO (in) < FIRST_PSEUDO_REGISTER
2049 /* However only do this if we can be sure that this input
2050 operand doesn't correspond with an uninitialized pseudo.
2051 global can assign some hardreg to it that is the same as
2052 the one assigned to a different, also live pseudo (as it
2053 can ignore the conflict). We must never introduce writes
2054 to such hardregs, as they would clobber the other live
2055 pseudo. See PR 20973. */
2056 || (!bitmap_bit_p (DF_LR_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
2057 ORIGINAL_REGNO (in))
2058 /* Similarly, only do this if we can be sure that the death
2059 note is still valid. global can assign some hardreg to
2060 the pseudo referenced in the note and simultaneously a
2061 subword of this hardreg to a different, also live pseudo,
2062 because only another subword of the hardreg is actually
2063 used in the insn. This cannot happen if the pseudo has
2064 been assigned exactly one hardreg. See PR 33732. */
2065 && REG_NREGS (in) == 1)))
2067 unsigned int regno = REGNO (in) + in_offset;
2068 unsigned int nwords = hard_regno_nregs (regno, inmode);
2070 if (! refers_to_regno_for_reload_p (regno, regno + nwords, out, (rtx*) 0)
2071 && ! hard_reg_set_here_p (regno, regno + nwords,
2072 PATTERN (this_insn))
2073 && (! earlyclobber
2074 || ! refers_to_regno_for_reload_p (regno, regno + nwords,
2075 PATTERN (this_insn), inloc)))
2077 unsigned int i;
2079 for (i = 0; i < nwords; i++)
2080 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
2081 regno + i))
2082 break;
2084 if (i == nwords)
2086 /* If we were going to use OUT as the reload reg
2087 and changed our mind, it means OUT is a dummy that
2088 dies here. So don't bother copying value to it. */
2089 if (for_real >= 0 && value == real_out)
2090 rld[for_real].out = 0;
2091 if (REG_P (real_in))
2092 value = real_in;
2093 else
2094 value = gen_rtx_REG (inmode, regno);
2099 return value;
2102 /* This page contains subroutines used mainly for determining
2103 whether the IN or an OUT of a reload can serve as the
2104 reload register. */
2106 /* Return 1 if X is an operand of an insn that is being earlyclobbered. */
2109 earlyclobber_operand_p (rtx x)
2111 int i;
2113 for (i = 0; i < n_earlyclobbers; i++)
2114 if (reload_earlyclobbers[i] == x)
2115 return 1;
2117 return 0;
2120 /* Return 1 if expression X alters a hard reg in the range
2121 from BEG_REGNO (inclusive) to END_REGNO (exclusive),
2122 either explicitly or in the guise of a pseudo-reg allocated to REGNO.
2123 X should be the body of an instruction. */
2125 static int
2126 hard_reg_set_here_p (unsigned int beg_regno, unsigned int end_regno, rtx x)
2128 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
2130 rtx op0 = SET_DEST (x);
2132 while (GET_CODE (op0) == SUBREG)
2133 op0 = SUBREG_REG (op0);
2134 if (REG_P (op0))
2136 unsigned int r = REGNO (op0);
2138 /* See if this reg overlaps range under consideration. */
2139 if (r < end_regno
2140 && end_hard_regno (GET_MODE (op0), r) > beg_regno)
2141 return 1;
2144 else if (GET_CODE (x) == PARALLEL)
2146 int i = XVECLEN (x, 0) - 1;
2148 for (; i >= 0; i--)
2149 if (hard_reg_set_here_p (beg_regno, end_regno, XVECEXP (x, 0, i)))
2150 return 1;
2153 return 0;
2156 /* Return 1 if ADDR is a valid memory address for mode MODE
2157 in address space AS, and check that each pseudo reg has the
2158 proper kind of hard reg. */
2161 strict_memory_address_addr_space_p (machine_mode mode ATTRIBUTE_UNUSED,
2162 rtx addr, addr_space_t as)
2164 #ifdef GO_IF_LEGITIMATE_ADDRESS
2165 gcc_assert (ADDR_SPACE_GENERIC_P (as));
2166 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2167 return 0;
2169 win:
2170 return 1;
2171 #else
2172 return targetm.addr_space.legitimate_address_p (mode, addr, 1, as);
2173 #endif
2176 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
2177 if they are the same hard reg, and has special hacks for
2178 autoincrement and autodecrement.
2179 This is specifically intended for find_reloads to use
2180 in determining whether two operands match.
2181 X is the operand whose number is the lower of the two.
2183 The value is 2 if Y contains a pre-increment that matches
2184 a non-incrementing address in X. */
2186 /* ??? To be completely correct, we should arrange to pass
2187 for X the output operand and for Y the input operand.
2188 For now, we assume that the output operand has the lower number
2189 because that is natural in (SET output (... input ...)). */
2192 operands_match_p (rtx x, rtx y)
2194 int i;
2195 RTX_CODE code = GET_CODE (x);
2196 const char *fmt;
2197 int success_2;
2199 if (x == y)
2200 return 1;
2201 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
2202 && (REG_P (y) || (GET_CODE (y) == SUBREG
2203 && REG_P (SUBREG_REG (y)))))
2205 int j;
2207 if (code == SUBREG)
2209 i = REGNO (SUBREG_REG (x));
2210 if (i >= FIRST_PSEUDO_REGISTER)
2211 goto slow;
2212 i += subreg_regno_offset (REGNO (SUBREG_REG (x)),
2213 GET_MODE (SUBREG_REG (x)),
2214 SUBREG_BYTE (x),
2215 GET_MODE (x));
2217 else
2218 i = REGNO (x);
2220 if (GET_CODE (y) == SUBREG)
2222 j = REGNO (SUBREG_REG (y));
2223 if (j >= FIRST_PSEUDO_REGISTER)
2224 goto slow;
2225 j += subreg_regno_offset (REGNO (SUBREG_REG (y)),
2226 GET_MODE (SUBREG_REG (y)),
2227 SUBREG_BYTE (y),
2228 GET_MODE (y));
2230 else
2231 j = REGNO (y);
2233 /* On a REG_WORDS_BIG_ENDIAN machine, point to the last register of a
2234 multiple hard register group of scalar integer registers, so that
2235 for example (reg:DI 0) and (reg:SI 1) will be considered the same
2236 register. */
2237 scalar_int_mode xmode;
2238 if (REG_WORDS_BIG_ENDIAN
2239 && is_a <scalar_int_mode> (GET_MODE (x), &xmode)
2240 && GET_MODE_SIZE (xmode) > UNITS_PER_WORD
2241 && i < FIRST_PSEUDO_REGISTER)
2242 i += hard_regno_nregs (i, xmode) - 1;
2243 scalar_int_mode ymode;
2244 if (REG_WORDS_BIG_ENDIAN
2245 && is_a <scalar_int_mode> (GET_MODE (y), &ymode)
2246 && GET_MODE_SIZE (ymode) > UNITS_PER_WORD
2247 && j < FIRST_PSEUDO_REGISTER)
2248 j += hard_regno_nregs (j, ymode) - 1;
2250 return i == j;
2252 /* If two operands must match, because they are really a single
2253 operand of an assembler insn, then two postincrements are invalid
2254 because the assembler insn would increment only once.
2255 On the other hand, a postincrement matches ordinary indexing
2256 if the postincrement is the output operand. */
2257 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
2258 return operands_match_p (XEXP (x, 0), y);
2259 /* Two preincrements are invalid
2260 because the assembler insn would increment only once.
2261 On the other hand, a preincrement matches ordinary indexing
2262 if the preincrement is the input operand.
2263 In this case, return 2, since some callers need to do special
2264 things when this happens. */
2265 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
2266 || GET_CODE (y) == PRE_MODIFY)
2267 return operands_match_p (x, XEXP (y, 0)) ? 2 : 0;
2269 slow:
2271 /* Now we have disposed of all the cases in which different rtx codes
2272 can match. */
2273 if (code != GET_CODE (y))
2274 return 0;
2276 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
2277 if (GET_MODE (x) != GET_MODE (y))
2278 return 0;
2280 /* MEMs referring to different address space are not equivalent. */
2281 if (code == MEM && MEM_ADDR_SPACE (x) != MEM_ADDR_SPACE (y))
2282 return 0;
2284 switch (code)
2286 CASE_CONST_UNIQUE:
2287 return 0;
2289 case LABEL_REF:
2290 return label_ref_label (x) == label_ref_label (y);
2291 case SYMBOL_REF:
2292 return XSTR (x, 0) == XSTR (y, 0);
2294 default:
2295 break;
2298 /* Compare the elements. If any pair of corresponding elements
2299 fail to match, return 0 for the whole things. */
2301 success_2 = 0;
2302 fmt = GET_RTX_FORMAT (code);
2303 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2305 int val, j;
2306 switch (fmt[i])
2308 case 'w':
2309 if (XWINT (x, i) != XWINT (y, i))
2310 return 0;
2311 break;
2313 case 'i':
2314 if (XINT (x, i) != XINT (y, i))
2315 return 0;
2316 break;
2318 case 'e':
2319 val = operands_match_p (XEXP (x, i), XEXP (y, i));
2320 if (val == 0)
2321 return 0;
2322 /* If any subexpression returns 2,
2323 we should return 2 if we are successful. */
2324 if (val == 2)
2325 success_2 = 1;
2326 break;
2328 case '0':
2329 break;
2331 case 'E':
2332 if (XVECLEN (x, i) != XVECLEN (y, i))
2333 return 0;
2334 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
2336 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j));
2337 if (val == 0)
2338 return 0;
2339 if (val == 2)
2340 success_2 = 1;
2342 break;
2344 /* It is believed that rtx's at this level will never
2345 contain anything but integers and other rtx's,
2346 except for within LABEL_REFs and SYMBOL_REFs. */
2347 default:
2348 gcc_unreachable ();
2351 return 1 + success_2;
2354 /* Describe the range of registers or memory referenced by X.
2355 If X is a register, set REG_FLAG and put the first register
2356 number into START and the last plus one into END.
2357 If X is a memory reference, put a base address into BASE
2358 and a range of integer offsets into START and END.
2359 If X is pushing on the stack, we can assume it causes no trouble,
2360 so we set the SAFE field. */
2362 static struct decomposition
2363 decompose (rtx x)
2365 struct decomposition val;
2366 int all_const = 0;
2368 memset (&val, 0, sizeof (val));
2370 switch (GET_CODE (x))
2372 case MEM:
2374 rtx base = NULL_RTX, offset = 0;
2375 rtx addr = XEXP (x, 0);
2377 if (GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == PRE_INC
2378 || GET_CODE (addr) == POST_DEC || GET_CODE (addr) == POST_INC)
2380 val.base = XEXP (addr, 0);
2381 val.start = -GET_MODE_SIZE (GET_MODE (x));
2382 val.end = GET_MODE_SIZE (GET_MODE (x));
2383 val.safe = REGNO (val.base) == STACK_POINTER_REGNUM;
2384 return val;
2387 if (GET_CODE (addr) == PRE_MODIFY || GET_CODE (addr) == POST_MODIFY)
2389 if (GET_CODE (XEXP (addr, 1)) == PLUS
2390 && XEXP (addr, 0) == XEXP (XEXP (addr, 1), 0)
2391 && CONSTANT_P (XEXP (XEXP (addr, 1), 1)))
2393 val.base = XEXP (addr, 0);
2394 val.start = -INTVAL (XEXP (XEXP (addr, 1), 1));
2395 val.end = INTVAL (XEXP (XEXP (addr, 1), 1));
2396 val.safe = REGNO (val.base) == STACK_POINTER_REGNUM;
2397 return val;
2401 if (GET_CODE (addr) == CONST)
2403 addr = XEXP (addr, 0);
2404 all_const = 1;
2406 if (GET_CODE (addr) == PLUS)
2408 if (CONSTANT_P (XEXP (addr, 0)))
2410 base = XEXP (addr, 1);
2411 offset = XEXP (addr, 0);
2413 else if (CONSTANT_P (XEXP (addr, 1)))
2415 base = XEXP (addr, 0);
2416 offset = XEXP (addr, 1);
2420 if (offset == 0)
2422 base = addr;
2423 offset = const0_rtx;
2425 if (GET_CODE (offset) == CONST)
2426 offset = XEXP (offset, 0);
2427 if (GET_CODE (offset) == PLUS)
2429 if (CONST_INT_P (XEXP (offset, 0)))
2431 base = gen_rtx_PLUS (GET_MODE (base), base, XEXP (offset, 1));
2432 offset = XEXP (offset, 0);
2434 else if (CONST_INT_P (XEXP (offset, 1)))
2436 base = gen_rtx_PLUS (GET_MODE (base), base, XEXP (offset, 0));
2437 offset = XEXP (offset, 1);
2439 else
2441 base = gen_rtx_PLUS (GET_MODE (base), base, offset);
2442 offset = const0_rtx;
2445 else if (!CONST_INT_P (offset))
2447 base = gen_rtx_PLUS (GET_MODE (base), base, offset);
2448 offset = const0_rtx;
2451 if (all_const && GET_CODE (base) == PLUS)
2452 base = gen_rtx_CONST (GET_MODE (base), base);
2454 gcc_assert (CONST_INT_P (offset));
2456 val.start = INTVAL (offset);
2457 val.end = val.start + GET_MODE_SIZE (GET_MODE (x));
2458 val.base = base;
2460 break;
2462 case REG:
2463 val.reg_flag = 1;
2464 val.start = true_regnum (x);
2465 if (val.start < 0 || val.start >= FIRST_PSEUDO_REGISTER)
2467 /* A pseudo with no hard reg. */
2468 val.start = REGNO (x);
2469 val.end = val.start + 1;
2471 else
2472 /* A hard reg. */
2473 val.end = end_hard_regno (GET_MODE (x), val.start);
2474 break;
2476 case SUBREG:
2477 if (!REG_P (SUBREG_REG (x)))
2478 /* This could be more precise, but it's good enough. */
2479 return decompose (SUBREG_REG (x));
2480 val.reg_flag = 1;
2481 val.start = true_regnum (x);
2482 if (val.start < 0 || val.start >= FIRST_PSEUDO_REGISTER)
2483 return decompose (SUBREG_REG (x));
2484 else
2485 /* A hard reg. */
2486 val.end = val.start + subreg_nregs (x);
2487 break;
2489 case SCRATCH:
2490 /* This hasn't been assigned yet, so it can't conflict yet. */
2491 val.safe = 1;
2492 break;
2494 default:
2495 gcc_assert (CONSTANT_P (x));
2496 val.safe = 1;
2497 break;
2499 return val;
2502 /* Return 1 if altering Y will not modify the value of X.
2503 Y is also described by YDATA, which should be decompose (Y). */
2505 static int
2506 immune_p (rtx x, rtx y, struct decomposition ydata)
2508 struct decomposition xdata;
2510 if (ydata.reg_flag)
2511 return !refers_to_regno_for_reload_p (ydata.start, ydata.end, x, (rtx*) 0);
2512 if (ydata.safe)
2513 return 1;
2515 gcc_assert (MEM_P (y));
2516 /* If Y is memory and X is not, Y can't affect X. */
2517 if (!MEM_P (x))
2518 return 1;
2520 xdata = decompose (x);
2522 if (! rtx_equal_p (xdata.base, ydata.base))
2524 /* If bases are distinct symbolic constants, there is no overlap. */
2525 if (CONSTANT_P (xdata.base) && CONSTANT_P (ydata.base))
2526 return 1;
2527 /* Constants and stack slots never overlap. */
2528 if (CONSTANT_P (xdata.base)
2529 && (ydata.base == frame_pointer_rtx
2530 || ydata.base == hard_frame_pointer_rtx
2531 || ydata.base == stack_pointer_rtx))
2532 return 1;
2533 if (CONSTANT_P (ydata.base)
2534 && (xdata.base == frame_pointer_rtx
2535 || xdata.base == hard_frame_pointer_rtx
2536 || xdata.base == stack_pointer_rtx))
2537 return 1;
2538 /* If either base is variable, we don't know anything. */
2539 return 0;
2542 return (xdata.start >= ydata.end || ydata.start >= xdata.end);
2545 /* Similar, but calls decompose. */
2548 safe_from_earlyclobber (rtx op, rtx clobber)
2550 struct decomposition early_data;
2552 early_data = decompose (clobber);
2553 return immune_p (op, clobber, early_data);
2556 /* Main entry point of this file: search the body of INSN
2557 for values that need reloading and record them with push_reload.
2558 REPLACE nonzero means record also where the values occur
2559 so that subst_reloads can be used.
2561 IND_LEVELS says how many levels of indirection are supported by this
2562 machine; a value of zero means that a memory reference is not a valid
2563 memory address.
2565 LIVE_KNOWN says we have valid information about which hard
2566 regs are live at each point in the program; this is true when
2567 we are called from global_alloc but false when stupid register
2568 allocation has been done.
2570 RELOAD_REG_P if nonzero is a vector indexed by hard reg number
2571 which is nonnegative if the reg has been commandeered for reloading into.
2572 It is copied into STATIC_RELOAD_REG_P and referenced from there
2573 by various subroutines.
2575 Return TRUE if some operands need to be changed, because of swapping
2576 commutative operands, reg_equiv_address substitution, or whatever. */
2579 find_reloads (rtx_insn *insn, int replace, int ind_levels, int live_known,
2580 short *reload_reg_p)
2582 int insn_code_number;
2583 int i, j;
2584 int noperands;
2585 /* These start out as the constraints for the insn
2586 and they are chewed up as we consider alternatives. */
2587 const char *constraints[MAX_RECOG_OPERANDS];
2588 /* These are the preferred classes for an operand, or NO_REGS if it isn't
2589 a register. */
2590 enum reg_class preferred_class[MAX_RECOG_OPERANDS];
2591 char pref_or_nothing[MAX_RECOG_OPERANDS];
2592 /* Nonzero for a MEM operand whose entire address needs a reload.
2593 May be -1 to indicate the entire address may or may not need a reload. */
2594 int address_reloaded[MAX_RECOG_OPERANDS];
2595 /* Nonzero for an address operand that needs to be completely reloaded.
2596 May be -1 to indicate the entire operand may or may not need a reload. */
2597 int address_operand_reloaded[MAX_RECOG_OPERANDS];
2598 /* Value of enum reload_type to use for operand. */
2599 enum reload_type operand_type[MAX_RECOG_OPERANDS];
2600 /* Value of enum reload_type to use within address of operand. */
2601 enum reload_type address_type[MAX_RECOG_OPERANDS];
2602 /* Save the usage of each operand. */
2603 enum reload_usage { RELOAD_READ, RELOAD_READ_WRITE, RELOAD_WRITE } modified[MAX_RECOG_OPERANDS];
2604 int no_input_reloads = 0, no_output_reloads = 0;
2605 int n_alternatives;
2606 reg_class_t this_alternative[MAX_RECOG_OPERANDS];
2607 char this_alternative_match_win[MAX_RECOG_OPERANDS];
2608 char this_alternative_win[MAX_RECOG_OPERANDS];
2609 char this_alternative_offmemok[MAX_RECOG_OPERANDS];
2610 char this_alternative_earlyclobber[MAX_RECOG_OPERANDS];
2611 int this_alternative_matches[MAX_RECOG_OPERANDS];
2612 reg_class_t goal_alternative[MAX_RECOG_OPERANDS];
2613 int this_alternative_number;
2614 int goal_alternative_number = 0;
2615 int operand_reloadnum[MAX_RECOG_OPERANDS];
2616 int goal_alternative_matches[MAX_RECOG_OPERANDS];
2617 int goal_alternative_matched[MAX_RECOG_OPERANDS];
2618 char goal_alternative_match_win[MAX_RECOG_OPERANDS];
2619 char goal_alternative_win[MAX_RECOG_OPERANDS];
2620 char goal_alternative_offmemok[MAX_RECOG_OPERANDS];
2621 char goal_alternative_earlyclobber[MAX_RECOG_OPERANDS];
2622 int goal_alternative_swapped;
2623 int best;
2624 int commutative;
2625 char operands_match[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
2626 rtx substed_operand[MAX_RECOG_OPERANDS];
2627 rtx body = PATTERN (insn);
2628 rtx set = single_set (insn);
2629 int goal_earlyclobber = 0, this_earlyclobber;
2630 machine_mode operand_mode[MAX_RECOG_OPERANDS];
2631 int retval = 0;
2633 this_insn = insn;
2634 n_reloads = 0;
2635 n_replacements = 0;
2636 n_earlyclobbers = 0;
2637 replace_reloads = replace;
2638 hard_regs_live_known = live_known;
2639 static_reload_reg_p = reload_reg_p;
2641 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output reloads;
2642 neither are insns that SET cc0. Insns that use CC0 are not allowed
2643 to have any input reloads. */
2644 if (JUMP_P (insn) || CALL_P (insn))
2645 no_output_reloads = 1;
2647 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (insn)))
2648 no_input_reloads = 1;
2649 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (insn)))
2650 no_output_reloads = 1;
2652 /* The eliminated forms of any secondary memory locations are per-insn, so
2653 clear them out here. */
2655 if (secondary_memlocs_elim_used)
2657 memset (secondary_memlocs_elim, 0,
2658 sizeof (secondary_memlocs_elim[0]) * secondary_memlocs_elim_used);
2659 secondary_memlocs_elim_used = 0;
2662 /* Dispose quickly of (set (reg..) (reg..)) if both have hard regs and it
2663 is cheap to move between them. If it is not, there may not be an insn
2664 to do the copy, so we may need a reload. */
2665 if (GET_CODE (body) == SET
2666 && REG_P (SET_DEST (body))
2667 && REGNO (SET_DEST (body)) < FIRST_PSEUDO_REGISTER
2668 && REG_P (SET_SRC (body))
2669 && REGNO (SET_SRC (body)) < FIRST_PSEUDO_REGISTER
2670 && register_move_cost (GET_MODE (SET_SRC (body)),
2671 REGNO_REG_CLASS (REGNO (SET_SRC (body))),
2672 REGNO_REG_CLASS (REGNO (SET_DEST (body)))) == 2)
2673 return 0;
2675 extract_insn (insn);
2677 noperands = reload_n_operands = recog_data.n_operands;
2678 n_alternatives = recog_data.n_alternatives;
2680 /* Just return "no reloads" if insn has no operands with constraints. */
2681 if (noperands == 0 || n_alternatives == 0)
2682 return 0;
2684 insn_code_number = INSN_CODE (insn);
2685 this_insn_is_asm = insn_code_number < 0;
2687 memcpy (operand_mode, recog_data.operand_mode,
2688 noperands * sizeof (machine_mode));
2689 memcpy (constraints, recog_data.constraints,
2690 noperands * sizeof (const char *));
2692 commutative = -1;
2694 /* If we will need to know, later, whether some pair of operands
2695 are the same, we must compare them now and save the result.
2696 Reloading the base and index registers will clobber them
2697 and afterward they will fail to match. */
2699 for (i = 0; i < noperands; i++)
2701 const char *p;
2702 int c;
2703 char *end;
2705 substed_operand[i] = recog_data.operand[i];
2706 p = constraints[i];
2708 modified[i] = RELOAD_READ;
2710 /* Scan this operand's constraint to see if it is an output operand,
2711 an in-out operand, is commutative, or should match another. */
2713 while ((c = *p))
2715 p += CONSTRAINT_LEN (c, p);
2716 switch (c)
2718 case '=':
2719 modified[i] = RELOAD_WRITE;
2720 break;
2721 case '+':
2722 modified[i] = RELOAD_READ_WRITE;
2723 break;
2724 case '%':
2726 /* The last operand should not be marked commutative. */
2727 gcc_assert (i != noperands - 1);
2729 /* We currently only support one commutative pair of
2730 operands. Some existing asm code currently uses more
2731 than one pair. Previously, that would usually work,
2732 but sometimes it would crash the compiler. We
2733 continue supporting that case as well as we can by
2734 silently ignoring all but the first pair. In the
2735 future we may handle it correctly. */
2736 if (commutative < 0)
2737 commutative = i;
2738 else
2739 gcc_assert (this_insn_is_asm);
2741 break;
2742 /* Use of ISDIGIT is tempting here, but it may get expensive because
2743 of locale support we don't want. */
2744 case '0': case '1': case '2': case '3': case '4':
2745 case '5': case '6': case '7': case '8': case '9':
2747 c = strtoul (p - 1, &end, 10);
2748 p = end;
2750 operands_match[c][i]
2751 = operands_match_p (recog_data.operand[c],
2752 recog_data.operand[i]);
2754 /* An operand may not match itself. */
2755 gcc_assert (c != i);
2757 /* If C can be commuted with C+1, and C might need to match I,
2758 then C+1 might also need to match I. */
2759 if (commutative >= 0)
2761 if (c == commutative || c == commutative + 1)
2763 int other = c + (c == commutative ? 1 : -1);
2764 operands_match[other][i]
2765 = operands_match_p (recog_data.operand[other],
2766 recog_data.operand[i]);
2768 if (i == commutative || i == commutative + 1)
2770 int other = i + (i == commutative ? 1 : -1);
2771 operands_match[c][other]
2772 = operands_match_p (recog_data.operand[c],
2773 recog_data.operand[other]);
2775 /* Note that C is supposed to be less than I.
2776 No need to consider altering both C and I because in
2777 that case we would alter one into the other. */
2784 /* Examine each operand that is a memory reference or memory address
2785 and reload parts of the addresses into index registers.
2786 Also here any references to pseudo regs that didn't get hard regs
2787 but are equivalent to constants get replaced in the insn itself
2788 with those constants. Nobody will ever see them again.
2790 Finally, set up the preferred classes of each operand. */
2792 for (i = 0; i < noperands; i++)
2794 RTX_CODE code = GET_CODE (recog_data.operand[i]);
2796 address_reloaded[i] = 0;
2797 address_operand_reloaded[i] = 0;
2798 operand_type[i] = (modified[i] == RELOAD_READ ? RELOAD_FOR_INPUT
2799 : modified[i] == RELOAD_WRITE ? RELOAD_FOR_OUTPUT
2800 : RELOAD_OTHER);
2801 address_type[i]
2802 = (modified[i] == RELOAD_READ ? RELOAD_FOR_INPUT_ADDRESS
2803 : modified[i] == RELOAD_WRITE ? RELOAD_FOR_OUTPUT_ADDRESS
2804 : RELOAD_OTHER);
2806 if (*constraints[i] == 0)
2807 /* Ignore things like match_operator operands. */
2809 else if (insn_extra_address_constraint
2810 (lookup_constraint (constraints[i])))
2812 address_operand_reloaded[i]
2813 = find_reloads_address (recog_data.operand_mode[i], (rtx*) 0,
2814 recog_data.operand[i],
2815 recog_data.operand_loc[i],
2816 i, operand_type[i], ind_levels, insn);
2818 /* If we now have a simple operand where we used to have a
2819 PLUS or MULT, re-recognize and try again. */
2820 if ((OBJECT_P (*recog_data.operand_loc[i])
2821 || GET_CODE (*recog_data.operand_loc[i]) == SUBREG)
2822 && (GET_CODE (recog_data.operand[i]) == MULT
2823 || GET_CODE (recog_data.operand[i]) == PLUS))
2825 INSN_CODE (insn) = -1;
2826 retval = find_reloads (insn, replace, ind_levels, live_known,
2827 reload_reg_p);
2828 return retval;
2831 recog_data.operand[i] = *recog_data.operand_loc[i];
2832 substed_operand[i] = recog_data.operand[i];
2834 /* Address operands are reloaded in their existing mode,
2835 no matter what is specified in the machine description. */
2836 operand_mode[i] = GET_MODE (recog_data.operand[i]);
2838 /* If the address is a single CONST_INT pick address mode
2839 instead otherwise we will later not know in which mode
2840 the reload should be performed. */
2841 if (operand_mode[i] == VOIDmode)
2842 operand_mode[i] = Pmode;
2845 else if (code == MEM)
2847 address_reloaded[i]
2848 = find_reloads_address (GET_MODE (recog_data.operand[i]),
2849 recog_data.operand_loc[i],
2850 XEXP (recog_data.operand[i], 0),
2851 &XEXP (recog_data.operand[i], 0),
2852 i, address_type[i], ind_levels, insn);
2853 recog_data.operand[i] = *recog_data.operand_loc[i];
2854 substed_operand[i] = recog_data.operand[i];
2856 else if (code == SUBREG)
2858 rtx reg = SUBREG_REG (recog_data.operand[i]);
2859 rtx op
2860 = find_reloads_toplev (recog_data.operand[i], i, address_type[i],
2861 ind_levels,
2862 set != 0
2863 && &SET_DEST (set) == recog_data.operand_loc[i],
2864 insn,
2865 &address_reloaded[i]);
2867 /* If we made a MEM to load (a part of) the stackslot of a pseudo
2868 that didn't get a hard register, emit a USE with a REG_EQUAL
2869 note in front so that we might inherit a previous, possibly
2870 wider reload. */
2872 if (replace
2873 && MEM_P (op)
2874 && REG_P (reg)
2875 && (GET_MODE_SIZE (GET_MODE (reg))
2876 >= GET_MODE_SIZE (GET_MODE (op)))
2877 && reg_equiv_constant (REGNO (reg)) == 0)
2878 set_unique_reg_note (emit_insn_before (gen_rtx_USE (VOIDmode, reg),
2879 insn),
2880 REG_EQUAL, reg_equiv_memory_loc (REGNO (reg)));
2882 substed_operand[i] = recog_data.operand[i] = op;
2884 else if (code == PLUS || GET_RTX_CLASS (code) == RTX_UNARY)
2885 /* We can get a PLUS as an "operand" as a result of register
2886 elimination. See eliminate_regs and gen_reload. We handle
2887 a unary operator by reloading the operand. */
2888 substed_operand[i] = recog_data.operand[i]
2889 = find_reloads_toplev (recog_data.operand[i], i, address_type[i],
2890 ind_levels, 0, insn,
2891 &address_reloaded[i]);
2892 else if (code == REG)
2894 /* This is equivalent to calling find_reloads_toplev.
2895 The code is duplicated for speed.
2896 When we find a pseudo always equivalent to a constant,
2897 we replace it by the constant. We must be sure, however,
2898 that we don't try to replace it in the insn in which it
2899 is being set. */
2900 int regno = REGNO (recog_data.operand[i]);
2901 if (reg_equiv_constant (regno) != 0
2902 && (set == 0 || &SET_DEST (set) != recog_data.operand_loc[i]))
2904 /* Record the existing mode so that the check if constants are
2905 allowed will work when operand_mode isn't specified. */
2907 if (operand_mode[i] == VOIDmode)
2908 operand_mode[i] = GET_MODE (recog_data.operand[i]);
2910 substed_operand[i] = recog_data.operand[i]
2911 = reg_equiv_constant (regno);
2913 if (reg_equiv_memory_loc (regno) != 0
2914 && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
2915 /* We need not give a valid is_set_dest argument since the case
2916 of a constant equivalence was checked above. */
2917 substed_operand[i] = recog_data.operand[i]
2918 = find_reloads_toplev (recog_data.operand[i], i, address_type[i],
2919 ind_levels, 0, insn,
2920 &address_reloaded[i]);
2922 /* If the operand is still a register (we didn't replace it with an
2923 equivalent), get the preferred class to reload it into. */
2924 code = GET_CODE (recog_data.operand[i]);
2925 preferred_class[i]
2926 = ((code == REG && REGNO (recog_data.operand[i])
2927 >= FIRST_PSEUDO_REGISTER)
2928 ? reg_preferred_class (REGNO (recog_data.operand[i]))
2929 : NO_REGS);
2930 pref_or_nothing[i]
2931 = (code == REG
2932 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER
2933 && reg_alternate_class (REGNO (recog_data.operand[i])) == NO_REGS);
2936 /* If this is simply a copy from operand 1 to operand 0, merge the
2937 preferred classes for the operands. */
2938 if (set != 0 && noperands >= 2 && recog_data.operand[0] == SET_DEST (set)
2939 && recog_data.operand[1] == SET_SRC (set))
2941 preferred_class[0] = preferred_class[1]
2942 = reg_class_subunion[(int) preferred_class[0]][(int) preferred_class[1]];
2943 pref_or_nothing[0] |= pref_or_nothing[1];
2944 pref_or_nothing[1] |= pref_or_nothing[0];
2947 /* Now see what we need for pseudo-regs that didn't get hard regs
2948 or got the wrong kind of hard reg. For this, we must consider
2949 all the operands together against the register constraints. */
2951 best = MAX_RECOG_OPERANDS * 2 + 600;
2953 goal_alternative_swapped = 0;
2955 /* The constraints are made of several alternatives.
2956 Each operand's constraint looks like foo,bar,... with commas
2957 separating the alternatives. The first alternatives for all
2958 operands go together, the second alternatives go together, etc.
2960 First loop over alternatives. */
2962 alternative_mask enabled = get_enabled_alternatives (insn);
2963 for (this_alternative_number = 0;
2964 this_alternative_number < n_alternatives;
2965 this_alternative_number++)
2967 int swapped;
2969 if (!TEST_BIT (enabled, this_alternative_number))
2971 int i;
2973 for (i = 0; i < recog_data.n_operands; i++)
2974 constraints[i] = skip_alternative (constraints[i]);
2976 continue;
2979 /* If insn is commutative (it's safe to exchange a certain pair
2980 of operands) then we need to try each alternative twice, the
2981 second time matching those two operands as if we had
2982 exchanged them. To do this, really exchange them in
2983 operands. */
2984 for (swapped = 0; swapped < (commutative >= 0 ? 2 : 1); swapped++)
2986 /* Loop over operands for one constraint alternative. */
2987 /* LOSERS counts those that don't fit this alternative
2988 and would require loading. */
2989 int losers = 0;
2990 /* BAD is set to 1 if it some operand can't fit this alternative
2991 even after reloading. */
2992 int bad = 0;
2993 /* REJECT is a count of how undesirable this alternative says it is
2994 if any reloading is required. If the alternative matches exactly
2995 then REJECT is ignored, but otherwise it gets this much
2996 counted against it in addition to the reloading needed. Each
2997 ? counts three times here since we want the disparaging caused by
2998 a bad register class to only count 1/3 as much. */
2999 int reject = 0;
3001 if (swapped)
3003 recog_data.operand[commutative] = substed_operand[commutative + 1];
3004 recog_data.operand[commutative + 1] = substed_operand[commutative];
3005 /* Swap the duplicates too. */
3006 for (i = 0; i < recog_data.n_dups; i++)
3007 if (recog_data.dup_num[i] == commutative
3008 || recog_data.dup_num[i] == commutative + 1)
3009 *recog_data.dup_loc[i]
3010 = recog_data.operand[(int) recog_data.dup_num[i]];
3012 std::swap (preferred_class[commutative],
3013 preferred_class[commutative + 1]);
3014 std::swap (pref_or_nothing[commutative],
3015 pref_or_nothing[commutative + 1]);
3016 std::swap (address_reloaded[commutative],
3017 address_reloaded[commutative + 1]);
3020 this_earlyclobber = 0;
3022 for (i = 0; i < noperands; i++)
3024 const char *p = constraints[i];
3025 char *end;
3026 int len;
3027 int win = 0;
3028 int did_match = 0;
3029 /* 0 => this operand can be reloaded somehow for this alternative. */
3030 int badop = 1;
3031 /* 0 => this operand can be reloaded if the alternative allows regs. */
3032 int winreg = 0;
3033 int c;
3034 int m;
3035 rtx operand = recog_data.operand[i];
3036 int offset = 0;
3037 /* Nonzero means this is a MEM that must be reloaded into a reg
3038 regardless of what the constraint says. */
3039 int force_reload = 0;
3040 int offmemok = 0;
3041 /* Nonzero if a constant forced into memory would be OK for this
3042 operand. */
3043 int constmemok = 0;
3044 int earlyclobber = 0;
3045 enum constraint_num cn;
3046 enum reg_class cl;
3048 /* If the predicate accepts a unary operator, it means that
3049 we need to reload the operand, but do not do this for
3050 match_operator and friends. */
3051 if (UNARY_P (operand) && *p != 0)
3052 operand = XEXP (operand, 0);
3054 /* If the operand is a SUBREG, extract
3055 the REG or MEM (or maybe even a constant) within.
3056 (Constants can occur as a result of reg_equiv_constant.) */
3058 while (GET_CODE (operand) == SUBREG)
3060 /* Offset only matters when operand is a REG and
3061 it is a hard reg. This is because it is passed
3062 to reg_fits_class_p if it is a REG and all pseudos
3063 return 0 from that function. */
3064 if (REG_P (SUBREG_REG (operand))
3065 && REGNO (SUBREG_REG (operand)) < FIRST_PSEUDO_REGISTER)
3067 if (simplify_subreg_regno (REGNO (SUBREG_REG (operand)),
3068 GET_MODE (SUBREG_REG (operand)),
3069 SUBREG_BYTE (operand),
3070 GET_MODE (operand)) < 0)
3071 force_reload = 1;
3072 offset += subreg_regno_offset (REGNO (SUBREG_REG (operand)),
3073 GET_MODE (SUBREG_REG (operand)),
3074 SUBREG_BYTE (operand),
3075 GET_MODE (operand));
3077 operand = SUBREG_REG (operand);
3078 /* Force reload if this is a constant or PLUS or if there may
3079 be a problem accessing OPERAND in the outer mode. */
3080 scalar_int_mode inner_mode;
3081 if (CONSTANT_P (operand)
3082 || GET_CODE (operand) == PLUS
3083 /* We must force a reload of paradoxical SUBREGs
3084 of a MEM because the alignment of the inner value
3085 may not be enough to do the outer reference. On
3086 big-endian machines, it may also reference outside
3087 the object.
3089 On machines that extend byte operations and we have a
3090 SUBREG where both the inner and outer modes are no wider
3091 than a word and the inner mode is narrower, is integral,
3092 and gets extended when loaded from memory, combine.c has
3093 made assumptions about the behavior of the machine in such
3094 register access. If the data is, in fact, in memory we
3095 must always load using the size assumed to be in the
3096 register and let the insn do the different-sized
3097 accesses.
3099 This is doubly true if WORD_REGISTER_OPERATIONS. In
3100 this case eliminate_regs has left non-paradoxical
3101 subregs for push_reload to see. Make sure it does
3102 by forcing the reload.
3104 ??? When is it right at this stage to have a subreg
3105 of a mem that is _not_ to be handled specially? IMO
3106 those should have been reduced to just a mem. */
3107 || ((MEM_P (operand)
3108 || (REG_P (operand)
3109 && REGNO (operand) >= FIRST_PSEUDO_REGISTER))
3110 && (WORD_REGISTER_OPERATIONS
3111 || ((GET_MODE_BITSIZE (GET_MODE (operand))
3112 < BIGGEST_ALIGNMENT)
3113 && paradoxical_subreg_p (operand_mode[i],
3114 GET_MODE (operand)))
3115 || BYTES_BIG_ENDIAN
3116 || ((GET_MODE_SIZE (operand_mode[i])
3117 <= UNITS_PER_WORD)
3118 && (is_a <scalar_int_mode>
3119 (GET_MODE (operand), &inner_mode))
3120 && (GET_MODE_SIZE (inner_mode)
3121 <= UNITS_PER_WORD)
3122 && paradoxical_subreg_p (operand_mode[i],
3123 inner_mode)
3124 && LOAD_EXTEND_OP (inner_mode) != UNKNOWN)))
3126 force_reload = 1;
3129 this_alternative[i] = NO_REGS;
3130 this_alternative_win[i] = 0;
3131 this_alternative_match_win[i] = 0;
3132 this_alternative_offmemok[i] = 0;
3133 this_alternative_earlyclobber[i] = 0;
3134 this_alternative_matches[i] = -1;
3136 /* An empty constraint or empty alternative
3137 allows anything which matched the pattern. */
3138 if (*p == 0 || *p == ',')
3139 win = 1, badop = 0;
3141 /* Scan this alternative's specs for this operand;
3142 set WIN if the operand fits any letter in this alternative.
3143 Otherwise, clear BADOP if this operand could
3144 fit some letter after reloads,
3145 or set WINREG if this operand could fit after reloads
3146 provided the constraint allows some registers. */
3149 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
3151 case '\0':
3152 len = 0;
3153 break;
3154 case ',':
3155 c = '\0';
3156 break;
3158 case '?':
3159 reject += 6;
3160 break;
3162 case '!':
3163 reject = 600;
3164 break;
3166 case '#':
3167 /* Ignore rest of this alternative as far as
3168 reloading is concerned. */
3170 p++;
3171 while (*p && *p != ',');
3172 len = 0;
3173 break;
3175 case '0': case '1': case '2': case '3': case '4':
3176 case '5': case '6': case '7': case '8': case '9':
3177 m = strtoul (p, &end, 10);
3178 p = end;
3179 len = 0;
3181 this_alternative_matches[i] = m;
3182 /* We are supposed to match a previous operand.
3183 If we do, we win if that one did.
3184 If we do not, count both of the operands as losers.
3185 (This is too conservative, since most of the time
3186 only a single reload insn will be needed to make
3187 the two operands win. As a result, this alternative
3188 may be rejected when it is actually desirable.) */
3189 if ((swapped && (m != commutative || i != commutative + 1))
3190 /* If we are matching as if two operands were swapped,
3191 also pretend that operands_match had been computed
3192 with swapped.
3193 But if I is the second of those and C is the first,
3194 don't exchange them, because operands_match is valid
3195 only on one side of its diagonal. */
3196 ? (operands_match
3197 [(m == commutative || m == commutative + 1)
3198 ? 2 * commutative + 1 - m : m]
3199 [(i == commutative || i == commutative + 1)
3200 ? 2 * commutative + 1 - i : i])
3201 : operands_match[m][i])
3203 /* If we are matching a non-offsettable address where an
3204 offsettable address was expected, then we must reject
3205 this combination, because we can't reload it. */
3206 if (this_alternative_offmemok[m]
3207 && MEM_P (recog_data.operand[m])
3208 && this_alternative[m] == NO_REGS
3209 && ! this_alternative_win[m])
3210 bad = 1;
3212 did_match = this_alternative_win[m];
3214 else
3216 /* Operands don't match. */
3217 rtx value;
3218 int loc1, loc2;
3219 /* Retroactively mark the operand we had to match
3220 as a loser, if it wasn't already. */
3221 if (this_alternative_win[m])
3222 losers++;
3223 this_alternative_win[m] = 0;
3224 if (this_alternative[m] == NO_REGS)
3225 bad = 1;
3226 /* But count the pair only once in the total badness of
3227 this alternative, if the pair can be a dummy reload.
3228 The pointers in operand_loc are not swapped; swap
3229 them by hand if necessary. */
3230 if (swapped && i == commutative)
3231 loc1 = commutative + 1;
3232 else if (swapped && i == commutative + 1)
3233 loc1 = commutative;
3234 else
3235 loc1 = i;
3236 if (swapped && m == commutative)
3237 loc2 = commutative + 1;
3238 else if (swapped && m == commutative + 1)
3239 loc2 = commutative;
3240 else
3241 loc2 = m;
3242 value
3243 = find_dummy_reload (recog_data.operand[i],
3244 recog_data.operand[m],
3245 recog_data.operand_loc[loc1],
3246 recog_data.operand_loc[loc2],
3247 operand_mode[i], operand_mode[m],
3248 this_alternative[m], -1,
3249 this_alternative_earlyclobber[m]);
3251 if (value != 0)
3252 losers--;
3254 /* This can be fixed with reloads if the operand
3255 we are supposed to match can be fixed with reloads. */
3256 badop = 0;
3257 this_alternative[i] = this_alternative[m];
3259 /* If we have to reload this operand and some previous
3260 operand also had to match the same thing as this
3261 operand, we don't know how to do that. So reject this
3262 alternative. */
3263 if (! did_match || force_reload)
3264 for (j = 0; j < i; j++)
3265 if (this_alternative_matches[j]
3266 == this_alternative_matches[i])
3268 badop = 1;
3269 break;
3271 break;
3273 case 'p':
3274 /* All necessary reloads for an address_operand
3275 were handled in find_reloads_address. */
3276 this_alternative[i]
3277 = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
3278 ADDRESS, SCRATCH);
3279 win = 1;
3280 badop = 0;
3281 break;
3283 case TARGET_MEM_CONSTRAINT:
3284 if (force_reload)
3285 break;
3286 if (MEM_P (operand)
3287 || (REG_P (operand)
3288 && REGNO (operand) >= FIRST_PSEUDO_REGISTER
3289 && reg_renumber[REGNO (operand)] < 0))
3290 win = 1;
3291 if (CONST_POOL_OK_P (operand_mode[i], operand))
3292 badop = 0;
3293 constmemok = 1;
3294 break;
3296 case '<':
3297 if (MEM_P (operand)
3298 && ! address_reloaded[i]
3299 && (GET_CODE (XEXP (operand, 0)) == PRE_DEC
3300 || GET_CODE (XEXP (operand, 0)) == POST_DEC))
3301 win = 1;
3302 break;
3304 case '>':
3305 if (MEM_P (operand)
3306 && ! address_reloaded[i]
3307 && (GET_CODE (XEXP (operand, 0)) == PRE_INC
3308 || GET_CODE (XEXP (operand, 0)) == POST_INC))
3309 win = 1;
3310 break;
3312 /* Memory operand whose address is not offsettable. */
3313 case 'V':
3314 if (force_reload)
3315 break;
3316 if (MEM_P (operand)
3317 && ! (ind_levels ? offsettable_memref_p (operand)
3318 : offsettable_nonstrict_memref_p (operand))
3319 /* Certain mem addresses will become offsettable
3320 after they themselves are reloaded. This is important;
3321 we don't want our own handling of unoffsettables
3322 to override the handling of reg_equiv_address. */
3323 && !(REG_P (XEXP (operand, 0))
3324 && (ind_levels == 0
3325 || reg_equiv_address (REGNO (XEXP (operand, 0))) != 0)))
3326 win = 1;
3327 break;
3329 /* Memory operand whose address is offsettable. */
3330 case 'o':
3331 if (force_reload)
3332 break;
3333 if ((MEM_P (operand)
3334 /* If IND_LEVELS, find_reloads_address won't reload a
3335 pseudo that didn't get a hard reg, so we have to
3336 reject that case. */
3337 && ((ind_levels ? offsettable_memref_p (operand)
3338 : offsettable_nonstrict_memref_p (operand))
3339 /* A reloaded address is offsettable because it is now
3340 just a simple register indirect. */
3341 || address_reloaded[i] == 1))
3342 || (REG_P (operand)
3343 && REGNO (operand) >= FIRST_PSEUDO_REGISTER
3344 && reg_renumber[REGNO (operand)] < 0
3345 /* If reg_equiv_address is nonzero, we will be
3346 loading it into a register; hence it will be
3347 offsettable, but we cannot say that reg_equiv_mem
3348 is offsettable without checking. */
3349 && ((reg_equiv_mem (REGNO (operand)) != 0
3350 && offsettable_memref_p (reg_equiv_mem (REGNO (operand))))
3351 || (reg_equiv_address (REGNO (operand)) != 0))))
3352 win = 1;
3353 if (CONST_POOL_OK_P (operand_mode[i], operand)
3354 || MEM_P (operand))
3355 badop = 0;
3356 constmemok = 1;
3357 offmemok = 1;
3358 break;
3360 case '&':
3361 /* Output operand that is stored before the need for the
3362 input operands (and their index registers) is over. */
3363 earlyclobber = 1, this_earlyclobber = 1;
3364 break;
3366 case 'X':
3367 force_reload = 0;
3368 win = 1;
3369 break;
3371 case 'g':
3372 if (! force_reload
3373 /* A PLUS is never a valid operand, but reload can make
3374 it from a register when eliminating registers. */
3375 && GET_CODE (operand) != PLUS
3376 /* A SCRATCH is not a valid operand. */
3377 && GET_CODE (operand) != SCRATCH
3378 && (! CONSTANT_P (operand)
3379 || ! flag_pic
3380 || LEGITIMATE_PIC_OPERAND_P (operand))
3381 && (GENERAL_REGS == ALL_REGS
3382 || !REG_P (operand)
3383 || (REGNO (operand) >= FIRST_PSEUDO_REGISTER
3384 && reg_renumber[REGNO (operand)] < 0)))
3385 win = 1;
3386 cl = GENERAL_REGS;
3387 goto reg;
3389 default:
3390 cn = lookup_constraint (p);
3391 switch (get_constraint_type (cn))
3393 case CT_REGISTER:
3394 cl = reg_class_for_constraint (cn);
3395 if (cl != NO_REGS)
3396 goto reg;
3397 break;
3399 case CT_CONST_INT:
3400 if (CONST_INT_P (operand)
3401 && (insn_const_int_ok_for_constraint
3402 (INTVAL (operand), cn)))
3403 win = true;
3404 break;
3406 case CT_MEMORY:
3407 if (force_reload)
3408 break;
3409 if (constraint_satisfied_p (operand, cn))
3410 win = 1;
3411 /* If the address was already reloaded,
3412 we win as well. */
3413 else if (MEM_P (operand) && address_reloaded[i] == 1)
3414 win = 1;
3415 /* Likewise if the address will be reloaded because
3416 reg_equiv_address is nonzero. For reg_equiv_mem
3417 we have to check. */
3418 else if (REG_P (operand)
3419 && REGNO (operand) >= FIRST_PSEUDO_REGISTER
3420 && reg_renumber[REGNO (operand)] < 0
3421 && ((reg_equiv_mem (REGNO (operand)) != 0
3422 && (constraint_satisfied_p
3423 (reg_equiv_mem (REGNO (operand)),
3424 cn)))
3425 || (reg_equiv_address (REGNO (operand))
3426 != 0)))
3427 win = 1;
3429 /* If we didn't already win, we can reload
3430 constants via force_const_mem, and other
3431 MEMs by reloading the address like for 'o'. */
3432 if (CONST_POOL_OK_P (operand_mode[i], operand)
3433 || MEM_P (operand))
3434 badop = 0;
3435 constmemok = 1;
3436 offmemok = 1;
3437 break;
3439 case CT_SPECIAL_MEMORY:
3440 if (force_reload)
3441 break;
3442 if (constraint_satisfied_p (operand, cn))
3443 win = 1;
3444 /* Likewise if the address will be reloaded because
3445 reg_equiv_address is nonzero. For reg_equiv_mem
3446 we have to check. */
3447 else if (REG_P (operand)
3448 && REGNO (operand) >= FIRST_PSEUDO_REGISTER
3449 && reg_renumber[REGNO (operand)] < 0
3450 && reg_equiv_mem (REGNO (operand)) != 0
3451 && (constraint_satisfied_p
3452 (reg_equiv_mem (REGNO (operand)), cn)))
3453 win = 1;
3454 break;
3456 case CT_ADDRESS:
3457 if (constraint_satisfied_p (operand, cn))
3458 win = 1;
3460 /* If we didn't already win, we can reload
3461 the address into a base register. */
3462 this_alternative[i]
3463 = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
3464 ADDRESS, SCRATCH);
3465 badop = 0;
3466 break;
3468 case CT_FIXED_FORM:
3469 if (constraint_satisfied_p (operand, cn))
3470 win = 1;
3471 break;
3473 break;
3475 reg:
3476 this_alternative[i]
3477 = reg_class_subunion[this_alternative[i]][cl];
3478 if (GET_MODE (operand) == BLKmode)
3479 break;
3480 winreg = 1;
3481 if (REG_P (operand)
3482 && reg_fits_class_p (operand, this_alternative[i],
3483 offset, GET_MODE (recog_data.operand[i])))
3484 win = 1;
3485 break;
3487 while ((p += len), c);
3489 if (swapped == (commutative >= 0 ? 1 : 0))
3490 constraints[i] = p;
3492 /* If this operand could be handled with a reg,
3493 and some reg is allowed, then this operand can be handled. */
3494 if (winreg && this_alternative[i] != NO_REGS
3495 && (win || !class_only_fixed_regs[this_alternative[i]]))
3496 badop = 0;
3498 /* Record which operands fit this alternative. */
3499 this_alternative_earlyclobber[i] = earlyclobber;
3500 if (win && ! force_reload)
3501 this_alternative_win[i] = 1;
3502 else if (did_match && ! force_reload)
3503 this_alternative_match_win[i] = 1;
3504 else
3506 int const_to_mem = 0;
3508 this_alternative_offmemok[i] = offmemok;
3509 losers++;
3510 if (badop)
3511 bad = 1;
3512 /* Alternative loses if it has no regs for a reg operand. */
3513 if (REG_P (operand)
3514 && this_alternative[i] == NO_REGS
3515 && this_alternative_matches[i] < 0)
3516 bad = 1;
3518 /* If this is a constant that is reloaded into the desired
3519 class by copying it to memory first, count that as another
3520 reload. This is consistent with other code and is
3521 required to avoid choosing another alternative when
3522 the constant is moved into memory by this function on
3523 an early reload pass. Note that the test here is
3524 precisely the same as in the code below that calls
3525 force_const_mem. */
3526 if (CONST_POOL_OK_P (operand_mode[i], operand)
3527 && ((targetm.preferred_reload_class (operand,
3528 this_alternative[i])
3529 == NO_REGS)
3530 || no_input_reloads))
3532 const_to_mem = 1;
3533 if (this_alternative[i] != NO_REGS)
3534 losers++;
3537 /* Alternative loses if it requires a type of reload not
3538 permitted for this insn. We can always reload SCRATCH
3539 and objects with a REG_UNUSED note. */
3540 if (GET_CODE (operand) != SCRATCH
3541 && modified[i] != RELOAD_READ && no_output_reloads
3542 && ! find_reg_note (insn, REG_UNUSED, operand))
3543 bad = 1;
3544 else if (modified[i] != RELOAD_WRITE && no_input_reloads
3545 && ! const_to_mem)
3546 bad = 1;
3548 /* If we can't reload this value at all, reject this
3549 alternative. Note that we could also lose due to
3550 LIMIT_RELOAD_CLASS, but we don't check that
3551 here. */
3553 if (! CONSTANT_P (operand) && this_alternative[i] != NO_REGS)
3555 if (targetm.preferred_reload_class (operand,
3556 this_alternative[i])
3557 == NO_REGS)
3558 reject = 600;
3560 if (operand_type[i] == RELOAD_FOR_OUTPUT
3561 && (targetm.preferred_output_reload_class (operand,
3562 this_alternative[i])
3563 == NO_REGS))
3564 reject = 600;
3567 /* We prefer to reload pseudos over reloading other things,
3568 since such reloads may be able to be eliminated later.
3569 If we are reloading a SCRATCH, we won't be generating any
3570 insns, just using a register, so it is also preferred.
3571 So bump REJECT in other cases. Don't do this in the
3572 case where we are forcing a constant into memory and
3573 it will then win since we don't want to have a different
3574 alternative match then. */
3575 if (! (REG_P (operand)
3576 && REGNO (operand) >= FIRST_PSEUDO_REGISTER)
3577 && GET_CODE (operand) != SCRATCH
3578 && ! (const_to_mem && constmemok))
3579 reject += 2;
3581 /* Input reloads can be inherited more often than output
3582 reloads can be removed, so penalize output reloads. */
3583 if (operand_type[i] != RELOAD_FOR_INPUT
3584 && GET_CODE (operand) != SCRATCH)
3585 reject++;
3588 /* If this operand is a pseudo register that didn't get
3589 a hard reg and this alternative accepts some
3590 register, see if the class that we want is a subset
3591 of the preferred class for this register. If not,
3592 but it intersects that class, use the preferred class
3593 instead. If it does not intersect the preferred
3594 class, show that usage of this alternative should be
3595 discouraged; it will be discouraged more still if the
3596 register is `preferred or nothing'. We do this
3597 because it increases the chance of reusing our spill
3598 register in a later insn and avoiding a pair of
3599 memory stores and loads.
3601 Don't bother with this if this alternative will
3602 accept this operand.
3604 Don't do this for a multiword operand, since it is
3605 only a small win and has the risk of requiring more
3606 spill registers, which could cause a large loss.
3608 Don't do this if the preferred class has only one
3609 register because we might otherwise exhaust the
3610 class. */
3612 if (! win && ! did_match
3613 && this_alternative[i] != NO_REGS
3614 && GET_MODE_SIZE (operand_mode[i]) <= UNITS_PER_WORD
3615 && reg_class_size [(int) preferred_class[i]] > 0
3616 && ! small_register_class_p (preferred_class[i]))
3618 if (! reg_class_subset_p (this_alternative[i],
3619 preferred_class[i]))
3621 /* Since we don't have a way of forming the intersection,
3622 we just do something special if the preferred class
3623 is a subset of the class we have; that's the most
3624 common case anyway. */
3625 if (reg_class_subset_p (preferred_class[i],
3626 this_alternative[i]))
3627 this_alternative[i] = preferred_class[i];
3628 else
3629 reject += (2 + 2 * pref_or_nothing[i]);
3634 /* Now see if any output operands that are marked "earlyclobber"
3635 in this alternative conflict with any input operands
3636 or any memory addresses. */
3638 for (i = 0; i < noperands; i++)
3639 if (this_alternative_earlyclobber[i]
3640 && (this_alternative_win[i] || this_alternative_match_win[i]))
3642 struct decomposition early_data;
3644 early_data = decompose (recog_data.operand[i]);
3646 gcc_assert (modified[i] != RELOAD_READ);
3648 if (this_alternative[i] == NO_REGS)
3650 this_alternative_earlyclobber[i] = 0;
3651 gcc_assert (this_insn_is_asm);
3652 error_for_asm (this_insn,
3653 "%<&%> constraint used with no register class");
3656 for (j = 0; j < noperands; j++)
3657 /* Is this an input operand or a memory ref? */
3658 if ((MEM_P (recog_data.operand[j])
3659 || modified[j] != RELOAD_WRITE)
3660 && j != i
3661 /* Ignore things like match_operator operands. */
3662 && !recog_data.is_operator[j]
3663 /* Don't count an input operand that is constrained to match
3664 the early clobber operand. */
3665 && ! (this_alternative_matches[j] == i
3666 && rtx_equal_p (recog_data.operand[i],
3667 recog_data.operand[j]))
3668 /* Is it altered by storing the earlyclobber operand? */
3669 && !immune_p (recog_data.operand[j], recog_data.operand[i],
3670 early_data))
3672 /* If the output is in a non-empty few-regs class,
3673 it's costly to reload it, so reload the input instead. */
3674 if (small_register_class_p (this_alternative[i])
3675 && (REG_P (recog_data.operand[j])
3676 || GET_CODE (recog_data.operand[j]) == SUBREG))
3678 losers++;
3679 this_alternative_win[j] = 0;
3680 this_alternative_match_win[j] = 0;
3682 else
3683 break;
3685 /* If an earlyclobber operand conflicts with something,
3686 it must be reloaded, so request this and count the cost. */
3687 if (j != noperands)
3689 losers++;
3690 this_alternative_win[i] = 0;
3691 this_alternative_match_win[j] = 0;
3692 for (j = 0; j < noperands; j++)
3693 if (this_alternative_matches[j] == i
3694 && this_alternative_match_win[j])
3696 this_alternative_win[j] = 0;
3697 this_alternative_match_win[j] = 0;
3698 losers++;
3703 /* If one alternative accepts all the operands, no reload required,
3704 choose that alternative; don't consider the remaining ones. */
3705 if (losers == 0)
3707 /* Unswap these so that they are never swapped at `finish'. */
3708 if (swapped)
3710 recog_data.operand[commutative] = substed_operand[commutative];
3711 recog_data.operand[commutative + 1]
3712 = substed_operand[commutative + 1];
3714 for (i = 0; i < noperands; i++)
3716 goal_alternative_win[i] = this_alternative_win[i];
3717 goal_alternative_match_win[i] = this_alternative_match_win[i];
3718 goal_alternative[i] = this_alternative[i];
3719 goal_alternative_offmemok[i] = this_alternative_offmemok[i];
3720 goal_alternative_matches[i] = this_alternative_matches[i];
3721 goal_alternative_earlyclobber[i]
3722 = this_alternative_earlyclobber[i];
3724 goal_alternative_number = this_alternative_number;
3725 goal_alternative_swapped = swapped;
3726 goal_earlyclobber = this_earlyclobber;
3727 goto finish;
3730 /* REJECT, set by the ! and ? constraint characters and when a register
3731 would be reloaded into a non-preferred class, discourages the use of
3732 this alternative for a reload goal. REJECT is incremented by six
3733 for each ? and two for each non-preferred class. */
3734 losers = losers * 6 + reject;
3736 /* If this alternative can be made to work by reloading,
3737 and it needs less reloading than the others checked so far,
3738 record it as the chosen goal for reloading. */
3739 if (! bad)
3741 if (best > losers)
3743 for (i = 0; i < noperands; i++)
3745 goal_alternative[i] = this_alternative[i];
3746 goal_alternative_win[i] = this_alternative_win[i];
3747 goal_alternative_match_win[i]
3748 = this_alternative_match_win[i];
3749 goal_alternative_offmemok[i]
3750 = this_alternative_offmemok[i];
3751 goal_alternative_matches[i] = this_alternative_matches[i];
3752 goal_alternative_earlyclobber[i]
3753 = this_alternative_earlyclobber[i];
3755 goal_alternative_swapped = swapped;
3756 best = losers;
3757 goal_alternative_number = this_alternative_number;
3758 goal_earlyclobber = this_earlyclobber;
3762 if (swapped)
3764 /* If the commutative operands have been swapped, swap
3765 them back in order to check the next alternative. */
3766 recog_data.operand[commutative] = substed_operand[commutative];
3767 recog_data.operand[commutative + 1] = substed_operand[commutative + 1];
3768 /* Unswap the duplicates too. */
3769 for (i = 0; i < recog_data.n_dups; i++)
3770 if (recog_data.dup_num[i] == commutative
3771 || recog_data.dup_num[i] == commutative + 1)
3772 *recog_data.dup_loc[i]
3773 = recog_data.operand[(int) recog_data.dup_num[i]];
3775 /* Unswap the operand related information as well. */
3776 std::swap (preferred_class[commutative],
3777 preferred_class[commutative + 1]);
3778 std::swap (pref_or_nothing[commutative],
3779 pref_or_nothing[commutative + 1]);
3780 std::swap (address_reloaded[commutative],
3781 address_reloaded[commutative + 1]);
3786 /* The operands don't meet the constraints.
3787 goal_alternative describes the alternative
3788 that we could reach by reloading the fewest operands.
3789 Reload so as to fit it. */
3791 if (best == MAX_RECOG_OPERANDS * 2 + 600)
3793 /* No alternative works with reloads?? */
3794 if (insn_code_number >= 0)
3795 fatal_insn ("unable to generate reloads for:", insn);
3796 error_for_asm (insn, "inconsistent operand constraints in an %<asm%>");
3797 /* Avoid further trouble with this insn. */
3798 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3799 n_reloads = 0;
3800 return 0;
3803 /* Jump to `finish' from above if all operands are valid already.
3804 In that case, goal_alternative_win is all 1. */
3805 finish:
3807 /* Right now, for any pair of operands I and J that are required to match,
3808 with I < J,
3809 goal_alternative_matches[J] is I.
3810 Set up goal_alternative_matched as the inverse function:
3811 goal_alternative_matched[I] = J. */
3813 for (i = 0; i < noperands; i++)
3814 goal_alternative_matched[i] = -1;
3816 for (i = 0; i < noperands; i++)
3817 if (! goal_alternative_win[i]
3818 && goal_alternative_matches[i] >= 0)
3819 goal_alternative_matched[goal_alternative_matches[i]] = i;
3821 for (i = 0; i < noperands; i++)
3822 goal_alternative_win[i] |= goal_alternative_match_win[i];
3824 /* If the best alternative is with operands 1 and 2 swapped,
3825 consider them swapped before reporting the reloads. Update the
3826 operand numbers of any reloads already pushed. */
3828 if (goal_alternative_swapped)
3830 std::swap (substed_operand[commutative],
3831 substed_operand[commutative + 1]);
3832 std::swap (recog_data.operand[commutative],
3833 recog_data.operand[commutative + 1]);
3834 std::swap (*recog_data.operand_loc[commutative],
3835 *recog_data.operand_loc[commutative + 1]);
3837 for (i = 0; i < recog_data.n_dups; i++)
3838 if (recog_data.dup_num[i] == commutative
3839 || recog_data.dup_num[i] == commutative + 1)
3840 *recog_data.dup_loc[i]
3841 = recog_data.operand[(int) recog_data.dup_num[i]];
3843 for (i = 0; i < n_reloads; i++)
3845 if (rld[i].opnum == commutative)
3846 rld[i].opnum = commutative + 1;
3847 else if (rld[i].opnum == commutative + 1)
3848 rld[i].opnum = commutative;
3852 for (i = 0; i < noperands; i++)
3854 operand_reloadnum[i] = -1;
3856 /* If this is an earlyclobber operand, we need to widen the scope.
3857 The reload must remain valid from the start of the insn being
3858 reloaded until after the operand is stored into its destination.
3859 We approximate this with RELOAD_OTHER even though we know that we
3860 do not conflict with RELOAD_FOR_INPUT_ADDRESS reloads.
3862 One special case that is worth checking is when we have an
3863 output that is earlyclobber but isn't used past the insn (typically
3864 a SCRATCH). In this case, we only need have the reload live
3865 through the insn itself, but not for any of our input or output
3866 reloads.
3867 But we must not accidentally narrow the scope of an existing
3868 RELOAD_OTHER reload - leave these alone.
3870 In any case, anything needed to address this operand can remain
3871 however they were previously categorized. */
3873 if (goal_alternative_earlyclobber[i] && operand_type[i] != RELOAD_OTHER)
3874 operand_type[i]
3875 = (find_reg_note (insn, REG_UNUSED, recog_data.operand[i])
3876 ? RELOAD_FOR_INSN : RELOAD_OTHER);
3879 /* Any constants that aren't allowed and can't be reloaded
3880 into registers are here changed into memory references. */
3881 for (i = 0; i < noperands; i++)
3882 if (! goal_alternative_win[i])
3884 rtx op = recog_data.operand[i];
3885 rtx subreg = NULL_RTX;
3886 rtx plus = NULL_RTX;
3887 machine_mode mode = operand_mode[i];
3889 /* Reloads of SUBREGs of CONSTANT RTXs are handled later in
3890 push_reload so we have to let them pass here. */
3891 if (GET_CODE (op) == SUBREG)
3893 subreg = op;
3894 op = SUBREG_REG (op);
3895 mode = GET_MODE (op);
3898 if (GET_CODE (op) == PLUS)
3900 plus = op;
3901 op = XEXP (op, 1);
3904 if (CONST_POOL_OK_P (mode, op)
3905 && ((targetm.preferred_reload_class (op, goal_alternative[i])
3906 == NO_REGS)
3907 || no_input_reloads))
3909 int this_address_reloaded;
3910 rtx tem = force_const_mem (mode, op);
3912 /* If we stripped a SUBREG or a PLUS above add it back. */
3913 if (plus != NULL_RTX)
3914 tem = gen_rtx_PLUS (mode, XEXP (plus, 0), tem);
3916 if (subreg != NULL_RTX)
3917 tem = gen_rtx_SUBREG (operand_mode[i], tem, SUBREG_BYTE (subreg));
3919 this_address_reloaded = 0;
3920 substed_operand[i] = recog_data.operand[i]
3921 = find_reloads_toplev (tem, i, address_type[i], ind_levels,
3922 0, insn, &this_address_reloaded);
3924 /* If the alternative accepts constant pool refs directly
3925 there will be no reload needed at all. */
3926 if (plus == NULL_RTX
3927 && subreg == NULL_RTX
3928 && alternative_allows_const_pool_ref (this_address_reloaded != 1
3929 ? substed_operand[i]
3930 : NULL,
3931 recog_data.constraints[i],
3932 goal_alternative_number))
3933 goal_alternative_win[i] = 1;
3937 /* Record the values of the earlyclobber operands for the caller. */
3938 if (goal_earlyclobber)
3939 for (i = 0; i < noperands; i++)
3940 if (goal_alternative_earlyclobber[i])
3941 reload_earlyclobbers[n_earlyclobbers++] = recog_data.operand[i];
3943 /* Now record reloads for all the operands that need them. */
3944 for (i = 0; i < noperands; i++)
3945 if (! goal_alternative_win[i])
3947 /* Operands that match previous ones have already been handled. */
3948 if (goal_alternative_matches[i] >= 0)
3950 /* Handle an operand with a nonoffsettable address
3951 appearing where an offsettable address will do
3952 by reloading the address into a base register.
3954 ??? We can also do this when the operand is a register and
3955 reg_equiv_mem is not offsettable, but this is a bit tricky,
3956 so we don't bother with it. It may not be worth doing. */
3957 else if (goal_alternative_matched[i] == -1
3958 && goal_alternative_offmemok[i]
3959 && MEM_P (recog_data.operand[i]))
3961 /* If the address to be reloaded is a VOIDmode constant,
3962 use the default address mode as mode of the reload register,
3963 as would have been done by find_reloads_address. */
3964 addr_space_t as = MEM_ADDR_SPACE (recog_data.operand[i]);
3965 machine_mode address_mode;
3967 address_mode = get_address_mode (recog_data.operand[i]);
3968 operand_reloadnum[i]
3969 = push_reload (XEXP (recog_data.operand[i], 0), NULL_RTX,
3970 &XEXP (recog_data.operand[i], 0), (rtx*) 0,
3971 base_reg_class (VOIDmode, as, MEM, SCRATCH),
3972 address_mode,
3973 VOIDmode, 0, 0, i, RELOAD_OTHER);
3974 rld[operand_reloadnum[i]].inc
3975 = GET_MODE_SIZE (GET_MODE (recog_data.operand[i]));
3977 /* If this operand is an output, we will have made any
3978 reloads for its address as RELOAD_FOR_OUTPUT_ADDRESS, but
3979 now we are treating part of the operand as an input, so
3980 we must change these to RELOAD_FOR_OTHER_ADDRESS. */
3982 if (modified[i] == RELOAD_WRITE)
3984 for (j = 0; j < n_reloads; j++)
3986 if (rld[j].opnum == i)
3988 if (rld[j].when_needed == RELOAD_FOR_OUTPUT_ADDRESS)
3989 rld[j].when_needed = RELOAD_FOR_OTHER_ADDRESS;
3990 else if (rld[j].when_needed
3991 == RELOAD_FOR_OUTADDR_ADDRESS)
3992 rld[j].when_needed = RELOAD_FOR_OTHER_ADDRESS;
3997 else if (goal_alternative_matched[i] == -1)
3999 operand_reloadnum[i]
4000 = push_reload ((modified[i] != RELOAD_WRITE
4001 ? recog_data.operand[i] : 0),
4002 (modified[i] != RELOAD_READ
4003 ? recog_data.operand[i] : 0),
4004 (modified[i] != RELOAD_WRITE
4005 ? recog_data.operand_loc[i] : 0),
4006 (modified[i] != RELOAD_READ
4007 ? recog_data.operand_loc[i] : 0),
4008 (enum reg_class) goal_alternative[i],
4009 (modified[i] == RELOAD_WRITE
4010 ? VOIDmode : operand_mode[i]),
4011 (modified[i] == RELOAD_READ
4012 ? VOIDmode : operand_mode[i]),
4013 (insn_code_number < 0 ? 0
4014 : insn_data[insn_code_number].operand[i].strict_low),
4015 0, i, operand_type[i]);
4017 /* In a matching pair of operands, one must be input only
4018 and the other must be output only.
4019 Pass the input operand as IN and the other as OUT. */
4020 else if (modified[i] == RELOAD_READ
4021 && modified[goal_alternative_matched[i]] == RELOAD_WRITE)
4023 operand_reloadnum[i]
4024 = push_reload (recog_data.operand[i],
4025 recog_data.operand[goal_alternative_matched[i]],
4026 recog_data.operand_loc[i],
4027 recog_data.operand_loc[goal_alternative_matched[i]],
4028 (enum reg_class) goal_alternative[i],
4029 operand_mode[i],
4030 operand_mode[goal_alternative_matched[i]],
4031 0, 0, i, RELOAD_OTHER);
4032 operand_reloadnum[goal_alternative_matched[i]] = output_reloadnum;
4034 else if (modified[i] == RELOAD_WRITE
4035 && modified[goal_alternative_matched[i]] == RELOAD_READ)
4037 operand_reloadnum[goal_alternative_matched[i]]
4038 = push_reload (recog_data.operand[goal_alternative_matched[i]],
4039 recog_data.operand[i],
4040 recog_data.operand_loc[goal_alternative_matched[i]],
4041 recog_data.operand_loc[i],
4042 (enum reg_class) goal_alternative[i],
4043 operand_mode[goal_alternative_matched[i]],
4044 operand_mode[i],
4045 0, 0, i, RELOAD_OTHER);
4046 operand_reloadnum[i] = output_reloadnum;
4048 else
4050 gcc_assert (insn_code_number < 0);
4051 error_for_asm (insn, "inconsistent operand constraints "
4052 "in an %<asm%>");
4053 /* Avoid further trouble with this insn. */
4054 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
4055 n_reloads = 0;
4056 return 0;
4059 else if (goal_alternative_matched[i] < 0
4060 && goal_alternative_matches[i] < 0
4061 && address_operand_reloaded[i] != 1
4062 && optimize)
4064 /* For each non-matching operand that's a MEM or a pseudo-register
4065 that didn't get a hard register, make an optional reload.
4066 This may get done even if the insn needs no reloads otherwise. */
4068 rtx operand = recog_data.operand[i];
4070 while (GET_CODE (operand) == SUBREG)
4071 operand = SUBREG_REG (operand);
4072 if ((MEM_P (operand)
4073 || (REG_P (operand)
4074 && REGNO (operand) >= FIRST_PSEUDO_REGISTER))
4075 /* If this is only for an output, the optional reload would not
4076 actually cause us to use a register now, just note that
4077 something is stored here. */
4078 && (goal_alternative[i] != NO_REGS
4079 || modified[i] == RELOAD_WRITE)
4080 && ! no_input_reloads
4081 /* An optional output reload might allow to delete INSN later.
4082 We mustn't make in-out reloads on insns that are not permitted
4083 output reloads.
4084 If this is an asm, we can't delete it; we must not even call
4085 push_reload for an optional output reload in this case,
4086 because we can't be sure that the constraint allows a register,
4087 and push_reload verifies the constraints for asms. */
4088 && (modified[i] == RELOAD_READ
4089 || (! no_output_reloads && ! this_insn_is_asm)))
4090 operand_reloadnum[i]
4091 = push_reload ((modified[i] != RELOAD_WRITE
4092 ? recog_data.operand[i] : 0),
4093 (modified[i] != RELOAD_READ
4094 ? recog_data.operand[i] : 0),
4095 (modified[i] != RELOAD_WRITE
4096 ? recog_data.operand_loc[i] : 0),
4097 (modified[i] != RELOAD_READ
4098 ? recog_data.operand_loc[i] : 0),
4099 (enum reg_class) goal_alternative[i],
4100 (modified[i] == RELOAD_WRITE
4101 ? VOIDmode : operand_mode[i]),
4102 (modified[i] == RELOAD_READ
4103 ? VOIDmode : operand_mode[i]),
4104 (insn_code_number < 0 ? 0
4105 : insn_data[insn_code_number].operand[i].strict_low),
4106 1, i, operand_type[i]);
4107 /* If a memory reference remains (either as a MEM or a pseudo that
4108 did not get a hard register), yet we can't make an optional
4109 reload, check if this is actually a pseudo register reference;
4110 we then need to emit a USE and/or a CLOBBER so that reload
4111 inheritance will do the right thing. */
4112 else if (replace
4113 && (MEM_P (operand)
4114 || (REG_P (operand)
4115 && REGNO (operand) >= FIRST_PSEUDO_REGISTER
4116 && reg_renumber [REGNO (operand)] < 0)))
4118 operand = *recog_data.operand_loc[i];
4120 while (GET_CODE (operand) == SUBREG)
4121 operand = SUBREG_REG (operand);
4122 if (REG_P (operand))
4124 if (modified[i] != RELOAD_WRITE)
4125 /* We mark the USE with QImode so that we recognize
4126 it as one that can be safely deleted at the end
4127 of reload. */
4128 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, operand),
4129 insn), QImode);
4130 if (modified[i] != RELOAD_READ)
4131 emit_insn_after (gen_clobber (operand), insn);
4135 else if (goal_alternative_matches[i] >= 0
4136 && goal_alternative_win[goal_alternative_matches[i]]
4137 && modified[i] == RELOAD_READ
4138 && modified[goal_alternative_matches[i]] == RELOAD_WRITE
4139 && ! no_input_reloads && ! no_output_reloads
4140 && optimize)
4142 /* Similarly, make an optional reload for a pair of matching
4143 objects that are in MEM or a pseudo that didn't get a hard reg. */
4145 rtx operand = recog_data.operand[i];
4147 while (GET_CODE (operand) == SUBREG)
4148 operand = SUBREG_REG (operand);
4149 if ((MEM_P (operand)
4150 || (REG_P (operand)
4151 && REGNO (operand) >= FIRST_PSEUDO_REGISTER))
4152 && (goal_alternative[goal_alternative_matches[i]] != NO_REGS))
4153 operand_reloadnum[i] = operand_reloadnum[goal_alternative_matches[i]]
4154 = push_reload (recog_data.operand[goal_alternative_matches[i]],
4155 recog_data.operand[i],
4156 recog_data.operand_loc[goal_alternative_matches[i]],
4157 recog_data.operand_loc[i],
4158 (enum reg_class) goal_alternative[goal_alternative_matches[i]],
4159 operand_mode[goal_alternative_matches[i]],
4160 operand_mode[i],
4161 0, 1, goal_alternative_matches[i], RELOAD_OTHER);
4164 /* Perform whatever substitutions on the operands we are supposed
4165 to make due to commutativity or replacement of registers
4166 with equivalent constants or memory slots. */
4168 for (i = 0; i < noperands; i++)
4170 /* We only do this on the last pass through reload, because it is
4171 possible for some data (like reg_equiv_address) to be changed during
4172 later passes. Moreover, we lose the opportunity to get a useful
4173 reload_{in,out}_reg when we do these replacements. */
4175 if (replace)
4177 rtx substitution = substed_operand[i];
4179 *recog_data.operand_loc[i] = substitution;
4181 /* If we're replacing an operand with a LABEL_REF, we need to
4182 make sure that there's a REG_LABEL_OPERAND note attached to
4183 this instruction. */
4184 if (GET_CODE (substitution) == LABEL_REF
4185 && !find_reg_note (insn, REG_LABEL_OPERAND,
4186 label_ref_label (substitution))
4187 /* For a JUMP_P, if it was a branch target it must have
4188 already been recorded as such. */
4189 && (!JUMP_P (insn)
4190 || !label_is_jump_target_p (label_ref_label (substitution),
4191 insn)))
4193 add_reg_note (insn, REG_LABEL_OPERAND,
4194 label_ref_label (substitution));
4195 if (LABEL_P (label_ref_label (substitution)))
4196 ++LABEL_NUSES (label_ref_label (substitution));
4200 else
4201 retval |= (substed_operand[i] != *recog_data.operand_loc[i]);
4204 /* If this insn pattern contains any MATCH_DUP's, make sure that
4205 they will be substituted if the operands they match are substituted.
4206 Also do now any substitutions we already did on the operands.
4208 Don't do this if we aren't making replacements because we might be
4209 propagating things allocated by frame pointer elimination into places
4210 it doesn't expect. */
4212 if (insn_code_number >= 0 && replace)
4213 for (i = insn_data[insn_code_number].n_dups - 1; i >= 0; i--)
4215 int opno = recog_data.dup_num[i];
4216 *recog_data.dup_loc[i] = *recog_data.operand_loc[opno];
4217 dup_replacements (recog_data.dup_loc[i], recog_data.operand_loc[opno]);
4220 #if 0
4221 /* This loses because reloading of prior insns can invalidate the equivalence
4222 (or at least find_equiv_reg isn't smart enough to find it any more),
4223 causing this insn to need more reload regs than it needed before.
4224 It may be too late to make the reload regs available.
4225 Now this optimization is done safely in choose_reload_regs. */
4227 /* For each reload of a reg into some other class of reg,
4228 search for an existing equivalent reg (same value now) in the right class.
4229 We can use it as long as we don't need to change its contents. */
4230 for (i = 0; i < n_reloads; i++)
4231 if (rld[i].reg_rtx == 0
4232 && rld[i].in != 0
4233 && REG_P (rld[i].in)
4234 && rld[i].out == 0)
4236 rld[i].reg_rtx
4237 = find_equiv_reg (rld[i].in, insn, rld[i].rclass, -1,
4238 static_reload_reg_p, 0, rld[i].inmode);
4239 /* Prevent generation of insn to load the value
4240 because the one we found already has the value. */
4241 if (rld[i].reg_rtx)
4242 rld[i].in = rld[i].reg_rtx;
4244 #endif
4246 /* If we detected error and replaced asm instruction by USE, forget about the
4247 reloads. */
4248 if (GET_CODE (PATTERN (insn)) == USE
4249 && CONST_INT_P (XEXP (PATTERN (insn), 0)))
4250 n_reloads = 0;
4252 /* Perhaps an output reload can be combined with another
4253 to reduce needs by one. */
4254 if (!goal_earlyclobber)
4255 combine_reloads ();
4257 /* If we have a pair of reloads for parts of an address, they are reloading
4258 the same object, the operands themselves were not reloaded, and they
4259 are for two operands that are supposed to match, merge the reloads and
4260 change the type of the surviving reload to RELOAD_FOR_OPERAND_ADDRESS. */
4262 for (i = 0; i < n_reloads; i++)
4264 int k;
4266 for (j = i + 1; j < n_reloads; j++)
4267 if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4268 || rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4269 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4270 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4271 && (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
4272 || rld[j].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4273 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4274 || rld[j].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4275 && rtx_equal_p (rld[i].in, rld[j].in)
4276 && (operand_reloadnum[rld[i].opnum] < 0
4277 || rld[operand_reloadnum[rld[i].opnum]].optional)
4278 && (operand_reloadnum[rld[j].opnum] < 0
4279 || rld[operand_reloadnum[rld[j].opnum]].optional)
4280 && (goal_alternative_matches[rld[i].opnum] == rld[j].opnum
4281 || (goal_alternative_matches[rld[j].opnum]
4282 == rld[i].opnum)))
4284 for (k = 0; k < n_replacements; k++)
4285 if (replacements[k].what == j)
4286 replacements[k].what = i;
4288 if (rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4289 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4290 rld[i].when_needed = RELOAD_FOR_OPADDR_ADDR;
4291 else
4292 rld[i].when_needed = RELOAD_FOR_OPERAND_ADDRESS;
4293 rld[j].in = 0;
4297 /* Scan all the reloads and update their type.
4298 If a reload is for the address of an operand and we didn't reload
4299 that operand, change the type. Similarly, change the operand number
4300 of a reload when two operands match. If a reload is optional, treat it
4301 as though the operand isn't reloaded.
4303 ??? This latter case is somewhat odd because if we do the optional
4304 reload, it means the object is hanging around. Thus we need only
4305 do the address reload if the optional reload was NOT done.
4307 Change secondary reloads to be the address type of their operand, not
4308 the normal type.
4310 If an operand's reload is now RELOAD_OTHER, change any
4311 RELOAD_FOR_INPUT_ADDRESS reloads of that operand to
4312 RELOAD_FOR_OTHER_ADDRESS. */
4314 for (i = 0; i < n_reloads; i++)
4316 if (rld[i].secondary_p
4317 && rld[i].when_needed == operand_type[rld[i].opnum])
4318 rld[i].when_needed = address_type[rld[i].opnum];
4320 if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4321 || rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4322 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4323 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4324 && (operand_reloadnum[rld[i].opnum] < 0
4325 || rld[operand_reloadnum[rld[i].opnum]].optional))
4327 /* If we have a secondary reload to go along with this reload,
4328 change its type to RELOAD_FOR_OPADDR_ADDR. */
4330 if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4331 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
4332 && rld[i].secondary_in_reload != -1)
4334 int secondary_in_reload = rld[i].secondary_in_reload;
4336 rld[secondary_in_reload].when_needed = RELOAD_FOR_OPADDR_ADDR;
4338 /* If there's a tertiary reload we have to change it also. */
4339 if (secondary_in_reload > 0
4340 && rld[secondary_in_reload].secondary_in_reload != -1)
4341 rld[rld[secondary_in_reload].secondary_in_reload].when_needed
4342 = RELOAD_FOR_OPADDR_ADDR;
4345 if ((rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4346 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4347 && rld[i].secondary_out_reload != -1)
4349 int secondary_out_reload = rld[i].secondary_out_reload;
4351 rld[secondary_out_reload].when_needed = RELOAD_FOR_OPADDR_ADDR;
4353 /* If there's a tertiary reload we have to change it also. */
4354 if (secondary_out_reload
4355 && rld[secondary_out_reload].secondary_out_reload != -1)
4356 rld[rld[secondary_out_reload].secondary_out_reload].when_needed
4357 = RELOAD_FOR_OPADDR_ADDR;
4360 if (rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4361 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4362 rld[i].when_needed = RELOAD_FOR_OPADDR_ADDR;
4363 else
4364 rld[i].when_needed = RELOAD_FOR_OPERAND_ADDRESS;
4367 if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4368 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
4369 && operand_reloadnum[rld[i].opnum] >= 0
4370 && (rld[operand_reloadnum[rld[i].opnum]].when_needed
4371 == RELOAD_OTHER))
4372 rld[i].when_needed = RELOAD_FOR_OTHER_ADDRESS;
4374 if (goal_alternative_matches[rld[i].opnum] >= 0)
4375 rld[i].opnum = goal_alternative_matches[rld[i].opnum];
4378 /* Scan all the reloads, and check for RELOAD_FOR_OPERAND_ADDRESS reloads.
4379 If we have more than one, then convert all RELOAD_FOR_OPADDR_ADDR
4380 reloads to RELOAD_FOR_OPERAND_ADDRESS reloads.
4382 choose_reload_regs assumes that RELOAD_FOR_OPADDR_ADDR reloads never
4383 conflict with RELOAD_FOR_OPERAND_ADDRESS reloads. This is true for a
4384 single pair of RELOAD_FOR_OPADDR_ADDR/RELOAD_FOR_OPERAND_ADDRESS reloads.
4385 However, if there is more than one RELOAD_FOR_OPERAND_ADDRESS reload,
4386 then a RELOAD_FOR_OPADDR_ADDR reload conflicts with all
4387 RELOAD_FOR_OPERAND_ADDRESS reloads other than the one that uses it.
4388 This is complicated by the fact that a single operand can have more
4389 than one RELOAD_FOR_OPERAND_ADDRESS reload. It is very difficult to fix
4390 choose_reload_regs without affecting code quality, and cases that
4391 actually fail are extremely rare, so it turns out to be better to fix
4392 the problem here by not generating cases that choose_reload_regs will
4393 fail for. */
4394 /* There is a similar problem with RELOAD_FOR_INPUT_ADDRESS /
4395 RELOAD_FOR_OUTPUT_ADDRESS when there is more than one of a kind for
4396 a single operand.
4397 We can reduce the register pressure by exploiting that a
4398 RELOAD_FOR_X_ADDR_ADDR that precedes all RELOAD_FOR_X_ADDRESS reloads
4399 does not conflict with any of them, if it is only used for the first of
4400 the RELOAD_FOR_X_ADDRESS reloads. */
4402 int first_op_addr_num = -2;
4403 int first_inpaddr_num[MAX_RECOG_OPERANDS];
4404 int first_outpaddr_num[MAX_RECOG_OPERANDS];
4405 int need_change = 0;
4406 /* We use last_op_addr_reload and the contents of the above arrays
4407 first as flags - -2 means no instance encountered, -1 means exactly
4408 one instance encountered.
4409 If more than one instance has been encountered, we store the reload
4410 number of the first reload of the kind in question; reload numbers
4411 are known to be non-negative. */
4412 for (i = 0; i < noperands; i++)
4413 first_inpaddr_num[i] = first_outpaddr_num[i] = -2;
4414 for (i = n_reloads - 1; i >= 0; i--)
4416 switch (rld[i].when_needed)
4418 case RELOAD_FOR_OPERAND_ADDRESS:
4419 if (++first_op_addr_num >= 0)
4421 first_op_addr_num = i;
4422 need_change = 1;
4424 break;
4425 case RELOAD_FOR_INPUT_ADDRESS:
4426 if (++first_inpaddr_num[rld[i].opnum] >= 0)
4428 first_inpaddr_num[rld[i].opnum] = i;
4429 need_change = 1;
4431 break;
4432 case RELOAD_FOR_OUTPUT_ADDRESS:
4433 if (++first_outpaddr_num[rld[i].opnum] >= 0)
4435 first_outpaddr_num[rld[i].opnum] = i;
4436 need_change = 1;
4438 break;
4439 default:
4440 break;
4444 if (need_change)
4446 for (i = 0; i < n_reloads; i++)
4448 int first_num;
4449 enum reload_type type;
4451 switch (rld[i].when_needed)
4453 case RELOAD_FOR_OPADDR_ADDR:
4454 first_num = first_op_addr_num;
4455 type = RELOAD_FOR_OPERAND_ADDRESS;
4456 break;
4457 case RELOAD_FOR_INPADDR_ADDRESS:
4458 first_num = first_inpaddr_num[rld[i].opnum];
4459 type = RELOAD_FOR_INPUT_ADDRESS;
4460 break;
4461 case RELOAD_FOR_OUTADDR_ADDRESS:
4462 first_num = first_outpaddr_num[rld[i].opnum];
4463 type = RELOAD_FOR_OUTPUT_ADDRESS;
4464 break;
4465 default:
4466 continue;
4468 if (first_num < 0)
4469 continue;
4470 else if (i > first_num)
4471 rld[i].when_needed = type;
4472 else
4474 /* Check if the only TYPE reload that uses reload I is
4475 reload FIRST_NUM. */
4476 for (j = n_reloads - 1; j > first_num; j--)
4478 if (rld[j].when_needed == type
4479 && (rld[i].secondary_p
4480 ? rld[j].secondary_in_reload == i
4481 : reg_mentioned_p (rld[i].in, rld[j].in)))
4483 rld[i].when_needed = type;
4484 break;
4492 /* See if we have any reloads that are now allowed to be merged
4493 because we've changed when the reload is needed to
4494 RELOAD_FOR_OPERAND_ADDRESS or RELOAD_FOR_OTHER_ADDRESS. Only
4495 check for the most common cases. */
4497 for (i = 0; i < n_reloads; i++)
4498 if (rld[i].in != 0 && rld[i].out == 0
4499 && (rld[i].when_needed == RELOAD_FOR_OPERAND_ADDRESS
4500 || rld[i].when_needed == RELOAD_FOR_OPADDR_ADDR
4501 || rld[i].when_needed == RELOAD_FOR_OTHER_ADDRESS))
4502 for (j = 0; j < n_reloads; j++)
4503 if (i != j && rld[j].in != 0 && rld[j].out == 0
4504 && rld[j].when_needed == rld[i].when_needed
4505 && MATCHES (rld[i].in, rld[j].in)
4506 && rld[i].rclass == rld[j].rclass
4507 && !rld[i].nocombine && !rld[j].nocombine
4508 && rld[i].reg_rtx == rld[j].reg_rtx)
4510 rld[i].opnum = MIN (rld[i].opnum, rld[j].opnum);
4511 transfer_replacements (i, j);
4512 rld[j].in = 0;
4515 /* If we made any reloads for addresses, see if they violate a
4516 "no input reloads" requirement for this insn. But loads that we
4517 do after the insn (such as for output addresses) are fine. */
4518 if (HAVE_cc0 && no_input_reloads)
4519 for (i = 0; i < n_reloads; i++)
4520 gcc_assert (rld[i].in == 0
4521 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS
4522 || rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS);
4524 /* Compute reload_mode and reload_nregs. */
4525 for (i = 0; i < n_reloads; i++)
4527 rld[i].mode = rld[i].inmode;
4528 if (rld[i].mode == VOIDmode
4529 || partial_subreg_p (rld[i].mode, rld[i].outmode))
4530 rld[i].mode = rld[i].outmode;
4532 rld[i].nregs = ira_reg_class_max_nregs [rld[i].rclass][rld[i].mode];
4535 /* Special case a simple move with an input reload and a
4536 destination of a hard reg, if the hard reg is ok, use it. */
4537 for (i = 0; i < n_reloads; i++)
4538 if (rld[i].when_needed == RELOAD_FOR_INPUT
4539 && GET_CODE (PATTERN (insn)) == SET
4540 && REG_P (SET_DEST (PATTERN (insn)))
4541 && (SET_SRC (PATTERN (insn)) == rld[i].in
4542 || SET_SRC (PATTERN (insn)) == rld[i].in_reg)
4543 && !elimination_target_reg_p (SET_DEST (PATTERN (insn))))
4545 rtx dest = SET_DEST (PATTERN (insn));
4546 unsigned int regno = REGNO (dest);
4548 if (regno < FIRST_PSEUDO_REGISTER
4549 && TEST_HARD_REG_BIT (reg_class_contents[rld[i].rclass], regno)
4550 && targetm.hard_regno_mode_ok (regno, rld[i].mode))
4552 int nr = hard_regno_nregs (regno, rld[i].mode);
4553 int ok = 1, nri;
4555 for (nri = 1; nri < nr; nri ++)
4556 if (! TEST_HARD_REG_BIT (reg_class_contents[rld[i].rclass], regno + nri))
4558 ok = 0;
4559 break;
4562 if (ok)
4563 rld[i].reg_rtx = dest;
4567 return retval;
4570 /* Return true if alternative number ALTNUM in constraint-string
4571 CONSTRAINT is guaranteed to accept a reloaded constant-pool reference.
4572 MEM gives the reference if its address hasn't been fully reloaded,
4573 otherwise it is NULL. */
4575 static bool
4576 alternative_allows_const_pool_ref (rtx mem ATTRIBUTE_UNUSED,
4577 const char *constraint, int altnum)
4579 int c;
4581 /* Skip alternatives before the one requested. */
4582 while (altnum > 0)
4584 while (*constraint++ != ',')
4586 altnum--;
4588 /* Scan the requested alternative for TARGET_MEM_CONSTRAINT or 'o'.
4589 If one of them is present, this alternative accepts the result of
4590 passing a constant-pool reference through find_reloads_toplev.
4592 The same is true of extra memory constraints if the address
4593 was reloaded into a register. However, the target may elect
4594 to disallow the original constant address, forcing it to be
4595 reloaded into a register instead. */
4596 for (; (c = *constraint) && c != ',' && c != '#';
4597 constraint += CONSTRAINT_LEN (c, constraint))
4599 enum constraint_num cn = lookup_constraint (constraint);
4600 if (insn_extra_memory_constraint (cn)
4601 && (mem == NULL || constraint_satisfied_p (mem, cn)))
4602 return true;
4604 return false;
4607 /* Scan X for memory references and scan the addresses for reloading.
4608 Also checks for references to "constant" regs that we want to eliminate
4609 and replaces them with the values they stand for.
4610 We may alter X destructively if it contains a reference to such.
4611 If X is just a constant reg, we return the equivalent value
4612 instead of X.
4614 IND_LEVELS says how many levels of indirect addressing this machine
4615 supports.
4617 OPNUM and TYPE identify the purpose of the reload.
4619 IS_SET_DEST is true if X is the destination of a SET, which is not
4620 appropriate to be replaced by a constant.
4622 INSN, if nonzero, is the insn in which we do the reload. It is used
4623 to determine if we may generate output reloads, and where to put USEs
4624 for pseudos that we have to replace with stack slots.
4626 ADDRESS_RELOADED. If nonzero, is a pointer to where we put the
4627 result of find_reloads_address. */
4629 static rtx
4630 find_reloads_toplev (rtx x, int opnum, enum reload_type type,
4631 int ind_levels, int is_set_dest, rtx_insn *insn,
4632 int *address_reloaded)
4634 RTX_CODE code = GET_CODE (x);
4636 const char *fmt = GET_RTX_FORMAT (code);
4637 int i;
4638 int copied;
4640 if (code == REG)
4642 /* This code is duplicated for speed in find_reloads. */
4643 int regno = REGNO (x);
4644 if (reg_equiv_constant (regno) != 0 && !is_set_dest)
4645 x = reg_equiv_constant (regno);
4646 #if 0
4647 /* This creates (subreg (mem...)) which would cause an unnecessary
4648 reload of the mem. */
4649 else if (reg_equiv_mem (regno) != 0)
4650 x = reg_equiv_mem (regno);
4651 #endif
4652 else if (reg_equiv_memory_loc (regno)
4653 && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
4655 rtx mem = make_memloc (x, regno);
4656 if (reg_equiv_address (regno)
4657 || ! rtx_equal_p (mem, reg_equiv_mem (regno)))
4659 /* If this is not a toplevel operand, find_reloads doesn't see
4660 this substitution. We have to emit a USE of the pseudo so
4661 that delete_output_reload can see it. */
4662 if (replace_reloads && recog_data.operand[opnum] != x)
4663 /* We mark the USE with QImode so that we recognize it
4664 as one that can be safely deleted at the end of
4665 reload. */
4666 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, x), insn),
4667 QImode);
4668 x = mem;
4669 i = find_reloads_address (GET_MODE (x), &x, XEXP (x, 0), &XEXP (x, 0),
4670 opnum, type, ind_levels, insn);
4671 if (!rtx_equal_p (x, mem))
4672 push_reg_equiv_alt_mem (regno, x);
4673 if (address_reloaded)
4674 *address_reloaded = i;
4677 return x;
4679 if (code == MEM)
4681 rtx tem = x;
4683 i = find_reloads_address (GET_MODE (x), &tem, XEXP (x, 0), &XEXP (x, 0),
4684 opnum, type, ind_levels, insn);
4685 if (address_reloaded)
4686 *address_reloaded = i;
4688 return tem;
4691 if (code == SUBREG && REG_P (SUBREG_REG (x)))
4693 /* Check for SUBREG containing a REG that's equivalent to a
4694 constant. If the constant has a known value, truncate it
4695 right now. Similarly if we are extracting a single-word of a
4696 multi-word constant. If the constant is symbolic, allow it
4697 to be substituted normally. push_reload will strip the
4698 subreg later. The constant must not be VOIDmode, because we
4699 will lose the mode of the register (this should never happen
4700 because one of the cases above should handle it). */
4702 int regno = REGNO (SUBREG_REG (x));
4703 rtx tem;
4705 if (regno >= FIRST_PSEUDO_REGISTER
4706 && reg_renumber[regno] < 0
4707 && reg_equiv_constant (regno) != 0)
4709 tem =
4710 simplify_gen_subreg (GET_MODE (x), reg_equiv_constant (regno),
4711 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
4712 gcc_assert (tem);
4713 if (CONSTANT_P (tem)
4714 && !targetm.legitimate_constant_p (GET_MODE (x), tem))
4716 tem = force_const_mem (GET_MODE (x), tem);
4717 i = find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
4718 &XEXP (tem, 0), opnum, type,
4719 ind_levels, insn);
4720 if (address_reloaded)
4721 *address_reloaded = i;
4723 return tem;
4726 /* If the subreg contains a reg that will be converted to a mem,
4727 attempt to convert the whole subreg to a (narrower or wider)
4728 memory reference instead. If this succeeds, we're done --
4729 otherwise fall through to check whether the inner reg still
4730 needs address reloads anyway. */
4732 if (regno >= FIRST_PSEUDO_REGISTER
4733 && reg_equiv_memory_loc (regno) != 0)
4735 tem = find_reloads_subreg_address (x, opnum, type, ind_levels,
4736 insn, address_reloaded);
4737 if (tem)
4738 return tem;
4742 for (copied = 0, i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4744 if (fmt[i] == 'e')
4746 rtx new_part = find_reloads_toplev (XEXP (x, i), opnum, type,
4747 ind_levels, is_set_dest, insn,
4748 address_reloaded);
4749 /* If we have replaced a reg with it's equivalent memory loc -
4750 that can still be handled here e.g. if it's in a paradoxical
4751 subreg - we must make the change in a copy, rather than using
4752 a destructive change. This way, find_reloads can still elect
4753 not to do the change. */
4754 if (new_part != XEXP (x, i) && ! CONSTANT_P (new_part) && ! copied)
4756 x = shallow_copy_rtx (x);
4757 copied = 1;
4759 XEXP (x, i) = new_part;
4762 return x;
4765 /* Return a mem ref for the memory equivalent of reg REGNO.
4766 This mem ref is not shared with anything. */
4768 static rtx
4769 make_memloc (rtx ad, int regno)
4771 /* We must rerun eliminate_regs, in case the elimination
4772 offsets have changed. */
4773 rtx tem
4774 = XEXP (eliminate_regs (reg_equiv_memory_loc (regno), VOIDmode, NULL_RTX),
4777 /* If TEM might contain a pseudo, we must copy it to avoid
4778 modifying it when we do the substitution for the reload. */
4779 if (rtx_varies_p (tem, 0))
4780 tem = copy_rtx (tem);
4782 tem = replace_equiv_address_nv (reg_equiv_memory_loc (regno), tem);
4783 tem = adjust_address_nv (tem, GET_MODE (ad), 0);
4785 /* Copy the result if it's still the same as the equivalence, to avoid
4786 modifying it when we do the substitution for the reload. */
4787 if (tem == reg_equiv_memory_loc (regno))
4788 tem = copy_rtx (tem);
4789 return tem;
4792 /* Returns true if AD could be turned into a valid memory reference
4793 to mode MODE in address space AS by reloading the part pointed to
4794 by PART into a register. */
4796 static int
4797 maybe_memory_address_addr_space_p (machine_mode mode, rtx ad,
4798 addr_space_t as, rtx *part)
4800 int retv;
4801 rtx tem = *part;
4802 rtx reg = gen_rtx_REG (GET_MODE (tem), max_reg_num ());
4804 *part = reg;
4805 retv = memory_address_addr_space_p (mode, ad, as);
4806 *part = tem;
4808 return retv;
4811 /* Record all reloads needed for handling memory address AD
4812 which appears in *LOC in a memory reference to mode MODE
4813 which itself is found in location *MEMREFLOC.
4814 Note that we take shortcuts assuming that no multi-reg machine mode
4815 occurs as part of an address.
4817 OPNUM and TYPE specify the purpose of this reload.
4819 IND_LEVELS says how many levels of indirect addressing this machine
4820 supports.
4822 INSN, if nonzero, is the insn in which we do the reload. It is used
4823 to determine if we may generate output reloads, and where to put USEs
4824 for pseudos that we have to replace with stack slots.
4826 Value is one if this address is reloaded or replaced as a whole; it is
4827 zero if the top level of this address was not reloaded or replaced, and
4828 it is -1 if it may or may not have been reloaded or replaced.
4830 Note that there is no verification that the address will be valid after
4831 this routine does its work. Instead, we rely on the fact that the address
4832 was valid when reload started. So we need only undo things that reload
4833 could have broken. These are wrong register types, pseudos not allocated
4834 to a hard register, and frame pointer elimination. */
4836 static int
4837 find_reloads_address (machine_mode mode, rtx *memrefloc, rtx ad,
4838 rtx *loc, int opnum, enum reload_type type,
4839 int ind_levels, rtx_insn *insn)
4841 addr_space_t as = memrefloc? MEM_ADDR_SPACE (*memrefloc)
4842 : ADDR_SPACE_GENERIC;
4843 int regno;
4844 int removed_and = 0;
4845 int op_index;
4846 rtx tem;
4848 /* If the address is a register, see if it is a legitimate address and
4849 reload if not. We first handle the cases where we need not reload
4850 or where we must reload in a non-standard way. */
4852 if (REG_P (ad))
4854 regno = REGNO (ad);
4856 if (reg_equiv_constant (regno) != 0)
4858 find_reloads_address_part (reg_equiv_constant (regno), loc,
4859 base_reg_class (mode, as, MEM, SCRATCH),
4860 GET_MODE (ad), opnum, type, ind_levels);
4861 return 1;
4864 tem = reg_equiv_memory_loc (regno);
4865 if (tem != 0)
4867 if (reg_equiv_address (regno) != 0 || num_not_at_initial_offset)
4869 tem = make_memloc (ad, regno);
4870 if (! strict_memory_address_addr_space_p (GET_MODE (tem),
4871 XEXP (tem, 0),
4872 MEM_ADDR_SPACE (tem)))
4874 rtx orig = tem;
4876 find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
4877 &XEXP (tem, 0), opnum,
4878 ADDR_TYPE (type), ind_levels, insn);
4879 if (!rtx_equal_p (tem, orig))
4880 push_reg_equiv_alt_mem (regno, tem);
4882 /* We can avoid a reload if the register's equivalent memory
4883 expression is valid as an indirect memory address.
4884 But not all addresses are valid in a mem used as an indirect
4885 address: only reg or reg+constant. */
4887 if (ind_levels > 0
4888 && strict_memory_address_addr_space_p (mode, tem, as)
4889 && (REG_P (XEXP (tem, 0))
4890 || (GET_CODE (XEXP (tem, 0)) == PLUS
4891 && REG_P (XEXP (XEXP (tem, 0), 0))
4892 && CONSTANT_P (XEXP (XEXP (tem, 0), 1)))))
4894 /* TEM is not the same as what we'll be replacing the
4895 pseudo with after reload, put a USE in front of INSN
4896 in the final reload pass. */
4897 if (replace_reloads
4898 && num_not_at_initial_offset
4899 && ! rtx_equal_p (tem, reg_equiv_mem (regno)))
4901 *loc = tem;
4902 /* We mark the USE with QImode so that we
4903 recognize it as one that can be safely
4904 deleted at the end of reload. */
4905 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, ad),
4906 insn), QImode);
4908 /* This doesn't really count as replacing the address
4909 as a whole, since it is still a memory access. */
4911 return 0;
4913 ad = tem;
4917 /* The only remaining case where we can avoid a reload is if this is a
4918 hard register that is valid as a base register and which is not the
4919 subject of a CLOBBER in this insn. */
4921 else if (regno < FIRST_PSEUDO_REGISTER
4922 && regno_ok_for_base_p (regno, mode, as, MEM, SCRATCH)
4923 && ! regno_clobbered_p (regno, this_insn, mode, 0))
4924 return 0;
4926 /* If we do not have one of the cases above, we must do the reload. */
4927 push_reload (ad, NULL_RTX, loc, (rtx*) 0,
4928 base_reg_class (mode, as, MEM, SCRATCH),
4929 GET_MODE (ad), VOIDmode, 0, 0, opnum, type);
4930 return 1;
4933 if (strict_memory_address_addr_space_p (mode, ad, as))
4935 /* The address appears valid, so reloads are not needed.
4936 But the address may contain an eliminable register.
4937 This can happen because a machine with indirect addressing
4938 may consider a pseudo register by itself a valid address even when
4939 it has failed to get a hard reg.
4940 So do a tree-walk to find and eliminate all such regs. */
4942 /* But first quickly dispose of a common case. */
4943 if (GET_CODE (ad) == PLUS
4944 && CONST_INT_P (XEXP (ad, 1))
4945 && REG_P (XEXP (ad, 0))
4946 && reg_equiv_constant (REGNO (XEXP (ad, 0))) == 0)
4947 return 0;
4949 subst_reg_equivs_changed = 0;
4950 *loc = subst_reg_equivs (ad, insn);
4952 if (! subst_reg_equivs_changed)
4953 return 0;
4955 /* Check result for validity after substitution. */
4956 if (strict_memory_address_addr_space_p (mode, ad, as))
4957 return 0;
4960 #ifdef LEGITIMIZE_RELOAD_ADDRESS
4963 if (memrefloc && ADDR_SPACE_GENERIC_P (as))
4965 LEGITIMIZE_RELOAD_ADDRESS (ad, GET_MODE (*memrefloc), opnum, type,
4966 ind_levels, win);
4968 break;
4969 win:
4970 *memrefloc = copy_rtx (*memrefloc);
4971 XEXP (*memrefloc, 0) = ad;
4972 move_replacements (&ad, &XEXP (*memrefloc, 0));
4973 return -1;
4975 while (0);
4976 #endif
4978 /* The address is not valid. We have to figure out why. First see if
4979 we have an outer AND and remove it if so. Then analyze what's inside. */
4981 if (GET_CODE (ad) == AND)
4983 removed_and = 1;
4984 loc = &XEXP (ad, 0);
4985 ad = *loc;
4988 /* One possibility for why the address is invalid is that it is itself
4989 a MEM. This can happen when the frame pointer is being eliminated, a
4990 pseudo is not allocated to a hard register, and the offset between the
4991 frame and stack pointers is not its initial value. In that case the
4992 pseudo will have been replaced by a MEM referring to the
4993 stack pointer. */
4994 if (MEM_P (ad))
4996 /* First ensure that the address in this MEM is valid. Then, unless
4997 indirect addresses are valid, reload the MEM into a register. */
4998 tem = ad;
4999 find_reloads_address (GET_MODE (ad), &tem, XEXP (ad, 0), &XEXP (ad, 0),
5000 opnum, ADDR_TYPE (type),
5001 ind_levels == 0 ? 0 : ind_levels - 1, insn);
5003 /* If tem was changed, then we must create a new memory reference to
5004 hold it and store it back into memrefloc. */
5005 if (tem != ad && memrefloc)
5007 *memrefloc = copy_rtx (*memrefloc);
5008 copy_replacements (tem, XEXP (*memrefloc, 0));
5009 loc = &XEXP (*memrefloc, 0);
5010 if (removed_and)
5011 loc = &XEXP (*loc, 0);
5014 /* Check similar cases as for indirect addresses as above except
5015 that we can allow pseudos and a MEM since they should have been
5016 taken care of above. */
5018 if (ind_levels == 0
5019 || (GET_CODE (XEXP (tem, 0)) == SYMBOL_REF && ! indirect_symref_ok)
5020 || MEM_P (XEXP (tem, 0))
5021 || ! (REG_P (XEXP (tem, 0))
5022 || (GET_CODE (XEXP (tem, 0)) == PLUS
5023 && REG_P (XEXP (XEXP (tem, 0), 0))
5024 && CONST_INT_P (XEXP (XEXP (tem, 0), 1)))))
5026 /* Must use TEM here, not AD, since it is the one that will
5027 have any subexpressions reloaded, if needed. */
5028 push_reload (tem, NULL_RTX, loc, (rtx*) 0,
5029 base_reg_class (mode, as, MEM, SCRATCH), GET_MODE (tem),
5030 VOIDmode, 0,
5031 0, opnum, type);
5032 return ! removed_and;
5034 else
5035 return 0;
5038 /* If we have address of a stack slot but it's not valid because the
5039 displacement is too large, compute the sum in a register.
5040 Handle all base registers here, not just fp/ap/sp, because on some
5041 targets (namely SH) we can also get too large displacements from
5042 big-endian corrections. */
5043 else if (GET_CODE (ad) == PLUS
5044 && REG_P (XEXP (ad, 0))
5045 && REGNO (XEXP (ad, 0)) < FIRST_PSEUDO_REGISTER
5046 && CONST_INT_P (XEXP (ad, 1))
5047 && (regno_ok_for_base_p (REGNO (XEXP (ad, 0)), mode, as, PLUS,
5048 CONST_INT)
5049 /* Similarly, if we were to reload the base register and the
5050 mem+offset address is still invalid, then we want to reload
5051 the whole address, not just the base register. */
5052 || ! maybe_memory_address_addr_space_p
5053 (mode, ad, as, &(XEXP (ad, 0)))))
5056 /* Unshare the MEM rtx so we can safely alter it. */
5057 if (memrefloc)
5059 *memrefloc = copy_rtx (*memrefloc);
5060 loc = &XEXP (*memrefloc, 0);
5061 if (removed_and)
5062 loc = &XEXP (*loc, 0);
5065 if (double_reg_address_ok[mode]
5066 && regno_ok_for_base_p (REGNO (XEXP (ad, 0)), mode, as,
5067 PLUS, CONST_INT))
5069 /* Unshare the sum as well. */
5070 *loc = ad = copy_rtx (ad);
5072 /* Reload the displacement into an index reg.
5073 We assume the frame pointer or arg pointer is a base reg. */
5074 find_reloads_address_part (XEXP (ad, 1), &XEXP (ad, 1),
5075 INDEX_REG_CLASS, GET_MODE (ad), opnum,
5076 type, ind_levels);
5077 return 0;
5079 else
5081 /* If the sum of two regs is not necessarily valid,
5082 reload the sum into a base reg.
5083 That will at least work. */
5084 find_reloads_address_part (ad, loc,
5085 base_reg_class (mode, as, MEM, SCRATCH),
5086 GET_MODE (ad), opnum, type, ind_levels);
5088 return ! removed_and;
5091 /* If we have an indexed stack slot, there are three possible reasons why
5092 it might be invalid: The index might need to be reloaded, the address
5093 might have been made by frame pointer elimination and hence have a
5094 constant out of range, or both reasons might apply.
5096 We can easily check for an index needing reload, but even if that is the
5097 case, we might also have an invalid constant. To avoid making the
5098 conservative assumption and requiring two reloads, we see if this address
5099 is valid when not interpreted strictly. If it is, the only problem is
5100 that the index needs a reload and find_reloads_address_1 will take care
5101 of it.
5103 Handle all base registers here, not just fp/ap/sp, because on some
5104 targets (namely SPARC) we can also get invalid addresses from preventive
5105 subreg big-endian corrections made by find_reloads_toplev. We
5106 can also get expressions involving LO_SUM (rather than PLUS) from
5107 find_reloads_subreg_address.
5109 If we decide to do something, it must be that `double_reg_address_ok'
5110 is true. We generate a reload of the base register + constant and
5111 rework the sum so that the reload register will be added to the index.
5112 This is safe because we know the address isn't shared.
5114 We check for the base register as both the first and second operand of
5115 the innermost PLUS and/or LO_SUM. */
5117 for (op_index = 0; op_index < 2; ++op_index)
5119 rtx operand, addend;
5120 enum rtx_code inner_code;
5122 if (GET_CODE (ad) != PLUS)
5123 continue;
5125 inner_code = GET_CODE (XEXP (ad, 0));
5126 if (!(GET_CODE (ad) == PLUS
5127 && CONST_INT_P (XEXP (ad, 1))
5128 && (inner_code == PLUS || inner_code == LO_SUM)))
5129 continue;
5131 operand = XEXP (XEXP (ad, 0), op_index);
5132 if (!REG_P (operand) || REGNO (operand) >= FIRST_PSEUDO_REGISTER)
5133 continue;
5135 addend = XEXP (XEXP (ad, 0), 1 - op_index);
5137 if ((regno_ok_for_base_p (REGNO (operand), mode, as, inner_code,
5138 GET_CODE (addend))
5139 || operand == frame_pointer_rtx
5140 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
5141 && operand == hard_frame_pointer_rtx)
5142 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
5143 && operand == arg_pointer_rtx)
5144 || operand == stack_pointer_rtx)
5145 && ! maybe_memory_address_addr_space_p
5146 (mode, ad, as, &XEXP (XEXP (ad, 0), 1 - op_index)))
5148 rtx offset_reg;
5149 enum reg_class cls;
5151 offset_reg = plus_constant (GET_MODE (ad), operand,
5152 INTVAL (XEXP (ad, 1)));
5154 /* Form the adjusted address. */
5155 if (GET_CODE (XEXP (ad, 0)) == PLUS)
5156 ad = gen_rtx_PLUS (GET_MODE (ad),
5157 op_index == 0 ? offset_reg : addend,
5158 op_index == 0 ? addend : offset_reg);
5159 else
5160 ad = gen_rtx_LO_SUM (GET_MODE (ad),
5161 op_index == 0 ? offset_reg : addend,
5162 op_index == 0 ? addend : offset_reg);
5163 *loc = ad;
5165 cls = base_reg_class (mode, as, MEM, GET_CODE (addend));
5166 find_reloads_address_part (XEXP (ad, op_index),
5167 &XEXP (ad, op_index), cls,
5168 GET_MODE (ad), opnum, type, ind_levels);
5169 find_reloads_address_1 (mode, as,
5170 XEXP (ad, 1 - op_index), 1, GET_CODE (ad),
5171 GET_CODE (XEXP (ad, op_index)),
5172 &XEXP (ad, 1 - op_index), opnum,
5173 type, 0, insn);
5175 return 0;
5179 /* See if address becomes valid when an eliminable register
5180 in a sum is replaced. */
5182 tem = ad;
5183 if (GET_CODE (ad) == PLUS)
5184 tem = subst_indexed_address (ad);
5185 if (tem != ad && strict_memory_address_addr_space_p (mode, tem, as))
5187 /* Ok, we win that way. Replace any additional eliminable
5188 registers. */
5190 subst_reg_equivs_changed = 0;
5191 tem = subst_reg_equivs (tem, insn);
5193 /* Make sure that didn't make the address invalid again. */
5195 if (! subst_reg_equivs_changed
5196 || strict_memory_address_addr_space_p (mode, tem, as))
5198 *loc = tem;
5199 return 0;
5203 /* If constants aren't valid addresses, reload the constant address
5204 into a register. */
5205 if (CONSTANT_P (ad) && ! strict_memory_address_addr_space_p (mode, ad, as))
5207 machine_mode address_mode = GET_MODE (ad);
5208 if (address_mode == VOIDmode)
5209 address_mode = targetm.addr_space.address_mode (as);
5211 /* If AD is an address in the constant pool, the MEM rtx may be shared.
5212 Unshare it so we can safely alter it. */
5213 if (memrefloc && GET_CODE (ad) == SYMBOL_REF
5214 && CONSTANT_POOL_ADDRESS_P (ad))
5216 *memrefloc = copy_rtx (*memrefloc);
5217 loc = &XEXP (*memrefloc, 0);
5218 if (removed_and)
5219 loc = &XEXP (*loc, 0);
5222 find_reloads_address_part (ad, loc,
5223 base_reg_class (mode, as, MEM, SCRATCH),
5224 address_mode, opnum, type, ind_levels);
5225 return ! removed_and;
5228 return find_reloads_address_1 (mode, as, ad, 0, MEM, SCRATCH, loc,
5229 opnum, type, ind_levels, insn);
5232 /* Find all pseudo regs appearing in AD
5233 that are eliminable in favor of equivalent values
5234 and do not have hard regs; replace them by their equivalents.
5235 INSN, if nonzero, is the insn in which we do the reload. We put USEs in
5236 front of it for pseudos that we have to replace with stack slots. */
5238 static rtx
5239 subst_reg_equivs (rtx ad, rtx_insn *insn)
5241 RTX_CODE code = GET_CODE (ad);
5242 int i;
5243 const char *fmt;
5245 switch (code)
5247 case HIGH:
5248 case CONST:
5249 CASE_CONST_ANY:
5250 case SYMBOL_REF:
5251 case LABEL_REF:
5252 case PC:
5253 case CC0:
5254 return ad;
5256 case REG:
5258 int regno = REGNO (ad);
5260 if (reg_equiv_constant (regno) != 0)
5262 subst_reg_equivs_changed = 1;
5263 return reg_equiv_constant (regno);
5265 if (reg_equiv_memory_loc (regno) && num_not_at_initial_offset)
5267 rtx mem = make_memloc (ad, regno);
5268 if (! rtx_equal_p (mem, reg_equiv_mem (regno)))
5270 subst_reg_equivs_changed = 1;
5271 /* We mark the USE with QImode so that we recognize it
5272 as one that can be safely deleted at the end of
5273 reload. */
5274 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, ad), insn),
5275 QImode);
5276 return mem;
5280 return ad;
5282 case PLUS:
5283 /* Quickly dispose of a common case. */
5284 if (XEXP (ad, 0) == frame_pointer_rtx
5285 && CONST_INT_P (XEXP (ad, 1)))
5286 return ad;
5287 break;
5289 default:
5290 break;
5293 fmt = GET_RTX_FORMAT (code);
5294 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5295 if (fmt[i] == 'e')
5296 XEXP (ad, i) = subst_reg_equivs (XEXP (ad, i), insn);
5297 return ad;
5300 /* Compute the sum of X and Y, making canonicalizations assumed in an
5301 address, namely: sum constant integers, surround the sum of two
5302 constants with a CONST, put the constant as the second operand, and
5303 group the constant on the outermost sum.
5305 This routine assumes both inputs are already in canonical form. */
5308 form_sum (machine_mode mode, rtx x, rtx y)
5310 rtx tem;
5312 gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
5313 gcc_assert (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode);
5315 if (CONST_INT_P (x))
5316 return plus_constant (mode, y, INTVAL (x));
5317 else if (CONST_INT_P (y))
5318 return plus_constant (mode, x, INTVAL (y));
5319 else if (CONSTANT_P (x))
5320 tem = x, x = y, y = tem;
5322 if (GET_CODE (x) == PLUS && CONSTANT_P (XEXP (x, 1)))
5323 return form_sum (mode, XEXP (x, 0), form_sum (mode, XEXP (x, 1), y));
5325 /* Note that if the operands of Y are specified in the opposite
5326 order in the recursive calls below, infinite recursion will occur. */
5327 if (GET_CODE (y) == PLUS && CONSTANT_P (XEXP (y, 1)))
5328 return form_sum (mode, form_sum (mode, x, XEXP (y, 0)), XEXP (y, 1));
5330 /* If both constant, encapsulate sum. Otherwise, just form sum. A
5331 constant will have been placed second. */
5332 if (CONSTANT_P (x) && CONSTANT_P (y))
5334 if (GET_CODE (x) == CONST)
5335 x = XEXP (x, 0);
5336 if (GET_CODE (y) == CONST)
5337 y = XEXP (y, 0);
5339 return gen_rtx_CONST (VOIDmode, gen_rtx_PLUS (mode, x, y));
5342 return gen_rtx_PLUS (mode, x, y);
5345 /* If ADDR is a sum containing a pseudo register that should be
5346 replaced with a constant (from reg_equiv_constant),
5347 return the result of doing so, and also apply the associative
5348 law so that the result is more likely to be a valid address.
5349 (But it is not guaranteed to be one.)
5351 Note that at most one register is replaced, even if more are
5352 replaceable. Also, we try to put the result into a canonical form
5353 so it is more likely to be a valid address.
5355 In all other cases, return ADDR. */
5357 static rtx
5358 subst_indexed_address (rtx addr)
5360 rtx op0 = 0, op1 = 0, op2 = 0;
5361 rtx tem;
5362 int regno;
5364 if (GET_CODE (addr) == PLUS)
5366 /* Try to find a register to replace. */
5367 op0 = XEXP (addr, 0), op1 = XEXP (addr, 1), op2 = 0;
5368 if (REG_P (op0)
5369 && (regno = REGNO (op0)) >= FIRST_PSEUDO_REGISTER
5370 && reg_renumber[regno] < 0
5371 && reg_equiv_constant (regno) != 0)
5372 op0 = reg_equiv_constant (regno);
5373 else if (REG_P (op1)
5374 && (regno = REGNO (op1)) >= FIRST_PSEUDO_REGISTER
5375 && reg_renumber[regno] < 0
5376 && reg_equiv_constant (regno) != 0)
5377 op1 = reg_equiv_constant (regno);
5378 else if (GET_CODE (op0) == PLUS
5379 && (tem = subst_indexed_address (op0)) != op0)
5380 op0 = tem;
5381 else if (GET_CODE (op1) == PLUS
5382 && (tem = subst_indexed_address (op1)) != op1)
5383 op1 = tem;
5384 else
5385 return addr;
5387 /* Pick out up to three things to add. */
5388 if (GET_CODE (op1) == PLUS)
5389 op2 = XEXP (op1, 1), op1 = XEXP (op1, 0);
5390 else if (GET_CODE (op0) == PLUS)
5391 op2 = op1, op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5393 /* Compute the sum. */
5394 if (op2 != 0)
5395 op1 = form_sum (GET_MODE (addr), op1, op2);
5396 if (op1 != 0)
5397 op0 = form_sum (GET_MODE (addr), op0, op1);
5399 return op0;
5401 return addr;
5404 /* Update the REG_INC notes for an insn. It updates all REG_INC
5405 notes for the instruction which refer to REGNO the to refer
5406 to the reload number.
5408 INSN is the insn for which any REG_INC notes need updating.
5410 REGNO is the register number which has been reloaded.
5412 RELOADNUM is the reload number. */
5414 static void
5415 update_auto_inc_notes (rtx_insn *insn ATTRIBUTE_UNUSED, int regno ATTRIBUTE_UNUSED,
5416 int reloadnum ATTRIBUTE_UNUSED)
5418 if (!AUTO_INC_DEC)
5419 return;
5421 for (rtx link = REG_NOTES (insn); link; link = XEXP (link, 1))
5422 if (REG_NOTE_KIND (link) == REG_INC
5423 && (int) REGNO (XEXP (link, 0)) == regno)
5424 push_replacement (&XEXP (link, 0), reloadnum, VOIDmode);
5427 /* Record the pseudo registers we must reload into hard registers in a
5428 subexpression of a would-be memory address, X referring to a value
5429 in mode MODE. (This function is not called if the address we find
5430 is strictly valid.)
5432 CONTEXT = 1 means we are considering regs as index regs,
5433 = 0 means we are considering them as base regs.
5434 OUTER_CODE is the code of the enclosing RTX, typically a MEM, a PLUS,
5435 or an autoinc code.
5436 If CONTEXT == 0 and OUTER_CODE is a PLUS or LO_SUM, then INDEX_CODE
5437 is the code of the index part of the address. Otherwise, pass SCRATCH
5438 for this argument.
5439 OPNUM and TYPE specify the purpose of any reloads made.
5441 IND_LEVELS says how many levels of indirect addressing are
5442 supported at this point in the address.
5444 INSN, if nonzero, is the insn in which we do the reload. It is used
5445 to determine if we may generate output reloads.
5447 We return nonzero if X, as a whole, is reloaded or replaced. */
5449 /* Note that we take shortcuts assuming that no multi-reg machine mode
5450 occurs as part of an address.
5451 Also, this is not fully machine-customizable; it works for machines
5452 such as VAXen and 68000's and 32000's, but other possible machines
5453 could have addressing modes that this does not handle right.
5454 If you add push_reload calls here, you need to make sure gen_reload
5455 handles those cases gracefully. */
5457 static int
5458 find_reloads_address_1 (machine_mode mode, addr_space_t as,
5459 rtx x, int context,
5460 enum rtx_code outer_code, enum rtx_code index_code,
5461 rtx *loc, int opnum, enum reload_type type,
5462 int ind_levels, rtx_insn *insn)
5464 #define REG_OK_FOR_CONTEXT(CONTEXT, REGNO, MODE, AS, OUTER, INDEX) \
5465 ((CONTEXT) == 0 \
5466 ? regno_ok_for_base_p (REGNO, MODE, AS, OUTER, INDEX) \
5467 : REGNO_OK_FOR_INDEX_P (REGNO))
5469 enum reg_class context_reg_class;
5470 RTX_CODE code = GET_CODE (x);
5471 bool reloaded_inner_of_autoinc = false;
5473 if (context == 1)
5474 context_reg_class = INDEX_REG_CLASS;
5475 else
5476 context_reg_class = base_reg_class (mode, as, outer_code, index_code);
5478 switch (code)
5480 case PLUS:
5482 rtx orig_op0 = XEXP (x, 0);
5483 rtx orig_op1 = XEXP (x, 1);
5484 RTX_CODE code0 = GET_CODE (orig_op0);
5485 RTX_CODE code1 = GET_CODE (orig_op1);
5486 rtx op0 = orig_op0;
5487 rtx op1 = orig_op1;
5489 if (GET_CODE (op0) == SUBREG)
5491 op0 = SUBREG_REG (op0);
5492 code0 = GET_CODE (op0);
5493 if (code0 == REG && REGNO (op0) < FIRST_PSEUDO_REGISTER)
5494 op0 = gen_rtx_REG (word_mode,
5495 (REGNO (op0) +
5496 subreg_regno_offset (REGNO (SUBREG_REG (orig_op0)),
5497 GET_MODE (SUBREG_REG (orig_op0)),
5498 SUBREG_BYTE (orig_op0),
5499 GET_MODE (orig_op0))));
5502 if (GET_CODE (op1) == SUBREG)
5504 op1 = SUBREG_REG (op1);
5505 code1 = GET_CODE (op1);
5506 if (code1 == REG && REGNO (op1) < FIRST_PSEUDO_REGISTER)
5507 /* ??? Why is this given op1's mode and above for
5508 ??? op0 SUBREGs we use word_mode? */
5509 op1 = gen_rtx_REG (GET_MODE (op1),
5510 (REGNO (op1) +
5511 subreg_regno_offset (REGNO (SUBREG_REG (orig_op1)),
5512 GET_MODE (SUBREG_REG (orig_op1)),
5513 SUBREG_BYTE (orig_op1),
5514 GET_MODE (orig_op1))));
5516 /* Plus in the index register may be created only as a result of
5517 register rematerialization for expression like &localvar*4. Reload it.
5518 It may be possible to combine the displacement on the outer level,
5519 but it is probably not worthwhile to do so. */
5520 if (context == 1)
5522 find_reloads_address (GET_MODE (x), loc, XEXP (x, 0), &XEXP (x, 0),
5523 opnum, ADDR_TYPE (type), ind_levels, insn);
5524 push_reload (*loc, NULL_RTX, loc, (rtx*) 0,
5525 context_reg_class,
5526 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5527 return 1;
5530 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
5531 || code0 == ZERO_EXTEND || code1 == MEM)
5533 find_reloads_address_1 (mode, as, orig_op0, 1, PLUS, SCRATCH,
5534 &XEXP (x, 0), opnum, type, ind_levels,
5535 insn);
5536 find_reloads_address_1 (mode, as, orig_op1, 0, PLUS, code0,
5537 &XEXP (x, 1), opnum, type, ind_levels,
5538 insn);
5541 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
5542 || code1 == ZERO_EXTEND || code0 == MEM)
5544 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, code1,
5545 &XEXP (x, 0), opnum, type, ind_levels,
5546 insn);
5547 find_reloads_address_1 (mode, as, orig_op1, 1, PLUS, SCRATCH,
5548 &XEXP (x, 1), opnum, type, ind_levels,
5549 insn);
5552 else if (code0 == CONST_INT || code0 == CONST
5553 || code0 == SYMBOL_REF || code0 == LABEL_REF)
5554 find_reloads_address_1 (mode, as, orig_op1, 0, PLUS, code0,
5555 &XEXP (x, 1), opnum, type, ind_levels,
5556 insn);
5558 else if (code1 == CONST_INT || code1 == CONST
5559 || code1 == SYMBOL_REF || code1 == LABEL_REF)
5560 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, code1,
5561 &XEXP (x, 0), opnum, type, ind_levels,
5562 insn);
5564 else if (code0 == REG && code1 == REG)
5566 if (REGNO_OK_FOR_INDEX_P (REGNO (op1))
5567 && regno_ok_for_base_p (REGNO (op0), mode, as, PLUS, REG))
5568 return 0;
5569 else if (REGNO_OK_FOR_INDEX_P (REGNO (op0))
5570 && regno_ok_for_base_p (REGNO (op1), mode, as, PLUS, REG))
5571 return 0;
5572 else if (regno_ok_for_base_p (REGNO (op0), mode, as, PLUS, REG))
5573 find_reloads_address_1 (mode, as, orig_op1, 1, PLUS, SCRATCH,
5574 &XEXP (x, 1), opnum, type, ind_levels,
5575 insn);
5576 else if (REGNO_OK_FOR_INDEX_P (REGNO (op1)))
5577 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, REG,
5578 &XEXP (x, 0), opnum, type, ind_levels,
5579 insn);
5580 else if (regno_ok_for_base_p (REGNO (op1), mode, as, PLUS, REG))
5581 find_reloads_address_1 (mode, as, orig_op0, 1, PLUS, SCRATCH,
5582 &XEXP (x, 0), opnum, type, ind_levels,
5583 insn);
5584 else if (REGNO_OK_FOR_INDEX_P (REGNO (op0)))
5585 find_reloads_address_1 (mode, as, orig_op1, 0, PLUS, REG,
5586 &XEXP (x, 1), opnum, type, ind_levels,
5587 insn);
5588 else
5590 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, REG,
5591 &XEXP (x, 0), opnum, type, ind_levels,
5592 insn);
5593 find_reloads_address_1 (mode, as, orig_op1, 1, PLUS, SCRATCH,
5594 &XEXP (x, 1), opnum, type, ind_levels,
5595 insn);
5599 else if (code0 == REG)
5601 find_reloads_address_1 (mode, as, orig_op0, 1, PLUS, SCRATCH,
5602 &XEXP (x, 0), opnum, type, ind_levels,
5603 insn);
5604 find_reloads_address_1 (mode, as, orig_op1, 0, PLUS, REG,
5605 &XEXP (x, 1), opnum, type, ind_levels,
5606 insn);
5609 else if (code1 == REG)
5611 find_reloads_address_1 (mode, as, orig_op1, 1, PLUS, SCRATCH,
5612 &XEXP (x, 1), opnum, type, ind_levels,
5613 insn);
5614 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, REG,
5615 &XEXP (x, 0), opnum, type, ind_levels,
5616 insn);
5620 return 0;
5622 case POST_MODIFY:
5623 case PRE_MODIFY:
5625 rtx op0 = XEXP (x, 0);
5626 rtx op1 = XEXP (x, 1);
5627 enum rtx_code index_code;
5628 int regno;
5629 int reloadnum;
5631 if (GET_CODE (op1) != PLUS && GET_CODE (op1) != MINUS)
5632 return 0;
5634 /* Currently, we only support {PRE,POST}_MODIFY constructs
5635 where a base register is {inc,dec}remented by the contents
5636 of another register or by a constant value. Thus, these
5637 operands must match. */
5638 gcc_assert (op0 == XEXP (op1, 0));
5640 /* Require index register (or constant). Let's just handle the
5641 register case in the meantime... If the target allows
5642 auto-modify by a constant then we could try replacing a pseudo
5643 register with its equivalent constant where applicable.
5645 We also handle the case where the register was eliminated
5646 resulting in a PLUS subexpression.
5648 If we later decide to reload the whole PRE_MODIFY or
5649 POST_MODIFY, inc_for_reload might clobber the reload register
5650 before reading the index. The index register might therefore
5651 need to live longer than a TYPE reload normally would, so be
5652 conservative and class it as RELOAD_OTHER. */
5653 if ((REG_P (XEXP (op1, 1))
5654 && !REGNO_OK_FOR_INDEX_P (REGNO (XEXP (op1, 1))))
5655 || GET_CODE (XEXP (op1, 1)) == PLUS)
5656 find_reloads_address_1 (mode, as, XEXP (op1, 1), 1, code, SCRATCH,
5657 &XEXP (op1, 1), opnum, RELOAD_OTHER,
5658 ind_levels, insn);
5660 gcc_assert (REG_P (XEXP (op1, 0)));
5662 regno = REGNO (XEXP (op1, 0));
5663 index_code = GET_CODE (XEXP (op1, 1));
5665 /* A register that is incremented cannot be constant! */
5666 gcc_assert (regno < FIRST_PSEUDO_REGISTER
5667 || reg_equiv_constant (regno) == 0);
5669 /* Handle a register that is equivalent to a memory location
5670 which cannot be addressed directly. */
5671 if (reg_equiv_memory_loc (regno) != 0
5672 && (reg_equiv_address (regno) != 0
5673 || num_not_at_initial_offset))
5675 rtx tem = make_memloc (XEXP (x, 0), regno);
5677 if (reg_equiv_address (regno)
5678 || ! rtx_equal_p (tem, reg_equiv_mem (regno)))
5680 rtx orig = tem;
5682 /* First reload the memory location's address.
5683 We can't use ADDR_TYPE (type) here, because we need to
5684 write back the value after reading it, hence we actually
5685 need two registers. */
5686 find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
5687 &XEXP (tem, 0), opnum,
5688 RELOAD_OTHER,
5689 ind_levels, insn);
5691 if (!rtx_equal_p (tem, orig))
5692 push_reg_equiv_alt_mem (regno, tem);
5694 /* Then reload the memory location into a base
5695 register. */
5696 reloadnum = push_reload (tem, tem, &XEXP (x, 0),
5697 &XEXP (op1, 0),
5698 base_reg_class (mode, as,
5699 code, index_code),
5700 GET_MODE (x), GET_MODE (x), 0,
5701 0, opnum, RELOAD_OTHER);
5703 update_auto_inc_notes (this_insn, regno, reloadnum);
5704 return 0;
5708 if (reg_renumber[regno] >= 0)
5709 regno = reg_renumber[regno];
5711 /* We require a base register here... */
5712 if (!regno_ok_for_base_p (regno, GET_MODE (x), as, code, index_code))
5714 reloadnum = push_reload (XEXP (op1, 0), XEXP (x, 0),
5715 &XEXP (op1, 0), &XEXP (x, 0),
5716 base_reg_class (mode, as,
5717 code, index_code),
5718 GET_MODE (x), GET_MODE (x), 0, 0,
5719 opnum, RELOAD_OTHER);
5721 update_auto_inc_notes (this_insn, regno, reloadnum);
5722 return 0;
5725 return 0;
5727 case POST_INC:
5728 case POST_DEC:
5729 case PRE_INC:
5730 case PRE_DEC:
5731 if (REG_P (XEXP (x, 0)))
5733 int regno = REGNO (XEXP (x, 0));
5734 int value = 0;
5735 rtx x_orig = x;
5737 /* A register that is incremented cannot be constant! */
5738 gcc_assert (regno < FIRST_PSEUDO_REGISTER
5739 || reg_equiv_constant (regno) == 0);
5741 /* Handle a register that is equivalent to a memory location
5742 which cannot be addressed directly. */
5743 if (reg_equiv_memory_loc (regno) != 0
5744 && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
5746 rtx tem = make_memloc (XEXP (x, 0), regno);
5747 if (reg_equiv_address (regno)
5748 || ! rtx_equal_p (tem, reg_equiv_mem (regno)))
5750 rtx orig = tem;
5752 /* First reload the memory location's address.
5753 We can't use ADDR_TYPE (type) here, because we need to
5754 write back the value after reading it, hence we actually
5755 need two registers. */
5756 find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
5757 &XEXP (tem, 0), opnum, type,
5758 ind_levels, insn);
5759 reloaded_inner_of_autoinc = true;
5760 if (!rtx_equal_p (tem, orig))
5761 push_reg_equiv_alt_mem (regno, tem);
5762 /* Put this inside a new increment-expression. */
5763 x = gen_rtx_fmt_e (GET_CODE (x), GET_MODE (x), tem);
5764 /* Proceed to reload that, as if it contained a register. */
5768 /* If we have a hard register that is ok in this incdec context,
5769 don't make a reload. If the register isn't nice enough for
5770 autoincdec, we can reload it. But, if an autoincrement of a
5771 register that we here verified as playing nice, still outside
5772 isn't "valid", it must be that no autoincrement is "valid".
5773 If that is true and something made an autoincrement anyway,
5774 this must be a special context where one is allowed.
5775 (For example, a "push" instruction.)
5776 We can't improve this address, so leave it alone. */
5778 /* Otherwise, reload the autoincrement into a suitable hard reg
5779 and record how much to increment by. */
5781 if (reg_renumber[regno] >= 0)
5782 regno = reg_renumber[regno];
5783 if (regno >= FIRST_PSEUDO_REGISTER
5784 || !REG_OK_FOR_CONTEXT (context, regno, mode, as, code,
5785 index_code))
5787 int reloadnum;
5789 /* If we can output the register afterwards, do so, this
5790 saves the extra update.
5791 We can do so if we have an INSN - i.e. no JUMP_INSN nor
5792 CALL_INSN - and it does not set CC0.
5793 But don't do this if we cannot directly address the
5794 memory location, since this will make it harder to
5795 reuse address reloads, and increases register pressure.
5796 Also don't do this if we can probably update x directly. */
5797 rtx equiv = (MEM_P (XEXP (x, 0))
5798 ? XEXP (x, 0)
5799 : reg_equiv_mem (regno));
5800 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
5801 if (insn && NONJUMP_INSN_P (insn)
5802 #if HAVE_cc0
5803 && ! sets_cc0_p (PATTERN (insn))
5804 #endif
5805 && (regno < FIRST_PSEUDO_REGISTER
5806 || (equiv
5807 && memory_operand (equiv, GET_MODE (equiv))
5808 && ! (icode != CODE_FOR_nothing
5809 && insn_operand_matches (icode, 0, equiv)
5810 && insn_operand_matches (icode, 1, equiv))))
5811 /* Using RELOAD_OTHER means we emit this and the reload we
5812 made earlier in the wrong order. */
5813 && !reloaded_inner_of_autoinc)
5815 /* We use the original pseudo for loc, so that
5816 emit_reload_insns() knows which pseudo this
5817 reload refers to and updates the pseudo rtx, not
5818 its equivalent memory location, as well as the
5819 corresponding entry in reg_last_reload_reg. */
5820 loc = &XEXP (x_orig, 0);
5821 x = XEXP (x, 0);
5822 reloadnum
5823 = push_reload (x, x, loc, loc,
5824 context_reg_class,
5825 GET_MODE (x), GET_MODE (x), 0, 0,
5826 opnum, RELOAD_OTHER);
5828 else
5830 reloadnum
5831 = push_reload (x, x, loc, (rtx*) 0,
5832 context_reg_class,
5833 GET_MODE (x), GET_MODE (x), 0, 0,
5834 opnum, type);
5835 rld[reloadnum].inc
5836 = find_inc_amount (PATTERN (this_insn), XEXP (x_orig, 0));
5838 value = 1;
5841 update_auto_inc_notes (this_insn, REGNO (XEXP (x_orig, 0)),
5842 reloadnum);
5844 return value;
5846 return 0;
5848 case TRUNCATE:
5849 case SIGN_EXTEND:
5850 case ZERO_EXTEND:
5851 /* Look for parts to reload in the inner expression and reload them
5852 too, in addition to this operation. Reloading all inner parts in
5853 addition to this one shouldn't be necessary, but at this point,
5854 we don't know if we can possibly omit any part that *can* be
5855 reloaded. Targets that are better off reloading just either part
5856 (or perhaps even a different part of an outer expression), should
5857 define LEGITIMIZE_RELOAD_ADDRESS. */
5858 find_reloads_address_1 (GET_MODE (XEXP (x, 0)), as, XEXP (x, 0),
5859 context, code, SCRATCH, &XEXP (x, 0), opnum,
5860 type, ind_levels, insn);
5861 push_reload (x, NULL_RTX, loc, (rtx*) 0,
5862 context_reg_class,
5863 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5864 return 1;
5866 case MEM:
5867 /* This is probably the result of a substitution, by eliminate_regs, of
5868 an equivalent address for a pseudo that was not allocated to a hard
5869 register. Verify that the specified address is valid and reload it
5870 into a register.
5872 Since we know we are going to reload this item, don't decrement for
5873 the indirection level.
5875 Note that this is actually conservative: it would be slightly more
5876 efficient to use the value of SPILL_INDIRECT_LEVELS from
5877 reload1.c here. */
5879 find_reloads_address (GET_MODE (x), loc, XEXP (x, 0), &XEXP (x, 0),
5880 opnum, ADDR_TYPE (type), ind_levels, insn);
5881 push_reload (*loc, NULL_RTX, loc, (rtx*) 0,
5882 context_reg_class,
5883 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5884 return 1;
5886 case REG:
5888 int regno = REGNO (x);
5890 if (reg_equiv_constant (regno) != 0)
5892 find_reloads_address_part (reg_equiv_constant (regno), loc,
5893 context_reg_class,
5894 GET_MODE (x), opnum, type, ind_levels);
5895 return 1;
5898 #if 0 /* This might screw code in reload1.c to delete prior output-reload
5899 that feeds this insn. */
5900 if (reg_equiv_mem (regno) != 0)
5902 push_reload (reg_equiv_mem (regno), NULL_RTX, loc, (rtx*) 0,
5903 context_reg_class,
5904 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5905 return 1;
5907 #endif
5909 if (reg_equiv_memory_loc (regno)
5910 && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
5912 rtx tem = make_memloc (x, regno);
5913 if (reg_equiv_address (regno) != 0
5914 || ! rtx_equal_p (tem, reg_equiv_mem (regno)))
5916 x = tem;
5917 find_reloads_address (GET_MODE (x), &x, XEXP (x, 0),
5918 &XEXP (x, 0), opnum, ADDR_TYPE (type),
5919 ind_levels, insn);
5920 if (!rtx_equal_p (x, tem))
5921 push_reg_equiv_alt_mem (regno, x);
5925 if (reg_renumber[regno] >= 0)
5926 regno = reg_renumber[regno];
5928 if (regno >= FIRST_PSEUDO_REGISTER
5929 || !REG_OK_FOR_CONTEXT (context, regno, mode, as, outer_code,
5930 index_code))
5932 push_reload (x, NULL_RTX, loc, (rtx*) 0,
5933 context_reg_class,
5934 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5935 return 1;
5938 /* If a register appearing in an address is the subject of a CLOBBER
5939 in this insn, reload it into some other register to be safe.
5940 The CLOBBER is supposed to make the register unavailable
5941 from before this insn to after it. */
5942 if (regno_clobbered_p (regno, this_insn, GET_MODE (x), 0))
5944 push_reload (x, NULL_RTX, loc, (rtx*) 0,
5945 context_reg_class,
5946 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5947 return 1;
5950 return 0;
5952 case SUBREG:
5953 if (REG_P (SUBREG_REG (x)))
5955 /* If this is a SUBREG of a hard register and the resulting register
5956 is of the wrong class, reload the whole SUBREG. This avoids
5957 needless copies if SUBREG_REG is multi-word. */
5958 if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
5960 int regno ATTRIBUTE_UNUSED = subreg_regno (x);
5962 if (!REG_OK_FOR_CONTEXT (context, regno, mode, as, outer_code,
5963 index_code))
5965 push_reload (x, NULL_RTX, loc, (rtx*) 0,
5966 context_reg_class,
5967 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5968 return 1;
5971 /* If this is a SUBREG of a pseudo-register, and the pseudo-register
5972 is larger than the class size, then reload the whole SUBREG. */
5973 else
5975 enum reg_class rclass = context_reg_class;
5976 if (ira_reg_class_max_nregs [rclass][GET_MODE (SUBREG_REG (x))]
5977 > reg_class_size[(int) rclass])
5979 /* If the inner register will be replaced by a memory
5980 reference, we can do this only if we can replace the
5981 whole subreg by a (narrower) memory reference. If
5982 this is not possible, fall through and reload just
5983 the inner register (including address reloads). */
5984 if (reg_equiv_memory_loc (REGNO (SUBREG_REG (x))) != 0)
5986 rtx tem = find_reloads_subreg_address (x, opnum,
5987 ADDR_TYPE (type),
5988 ind_levels, insn,
5989 NULL);
5990 if (tem)
5992 push_reload (tem, NULL_RTX, loc, (rtx*) 0, rclass,
5993 GET_MODE (tem), VOIDmode, 0, 0,
5994 opnum, type);
5995 return 1;
5998 else
6000 push_reload (x, NULL_RTX, loc, (rtx*) 0, rclass,
6001 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
6002 return 1;
6007 break;
6009 default:
6010 break;
6014 const char *fmt = GET_RTX_FORMAT (code);
6015 int i;
6017 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6019 if (fmt[i] == 'e')
6020 /* Pass SCRATCH for INDEX_CODE, since CODE can never be a PLUS once
6021 we get here. */
6022 find_reloads_address_1 (mode, as, XEXP (x, i), context,
6023 code, SCRATCH, &XEXP (x, i),
6024 opnum, type, ind_levels, insn);
6028 #undef REG_OK_FOR_CONTEXT
6029 return 0;
6032 /* X, which is found at *LOC, is a part of an address that needs to be
6033 reloaded into a register of class RCLASS. If X is a constant, or if
6034 X is a PLUS that contains a constant, check that the constant is a
6035 legitimate operand and that we are supposed to be able to load
6036 it into the register.
6038 If not, force the constant into memory and reload the MEM instead.
6040 MODE is the mode to use, in case X is an integer constant.
6042 OPNUM and TYPE describe the purpose of any reloads made.
6044 IND_LEVELS says how many levels of indirect addressing this machine
6045 supports. */
6047 static void
6048 find_reloads_address_part (rtx x, rtx *loc, enum reg_class rclass,
6049 machine_mode mode, int opnum,
6050 enum reload_type type, int ind_levels)
6052 if (CONSTANT_P (x)
6053 && (!targetm.legitimate_constant_p (mode, x)
6054 || targetm.preferred_reload_class (x, rclass) == NO_REGS))
6056 x = force_const_mem (mode, x);
6057 find_reloads_address (mode, &x, XEXP (x, 0), &XEXP (x, 0),
6058 opnum, type, ind_levels, 0);
6061 else if (GET_CODE (x) == PLUS
6062 && CONSTANT_P (XEXP (x, 1))
6063 && (!targetm.legitimate_constant_p (GET_MODE (x), XEXP (x, 1))
6064 || targetm.preferred_reload_class (XEXP (x, 1), rclass)
6065 == NO_REGS))
6067 rtx tem;
6069 tem = force_const_mem (GET_MODE (x), XEXP (x, 1));
6070 x = gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0), tem);
6071 find_reloads_address (mode, &XEXP (x, 1), XEXP (tem, 0), &XEXP (tem, 0),
6072 opnum, type, ind_levels, 0);
6075 push_reload (x, NULL_RTX, loc, (rtx*) 0, rclass,
6076 mode, VOIDmode, 0, 0, opnum, type);
6079 /* X, a subreg of a pseudo, is a part of an address that needs to be
6080 reloaded, and the pseusdo is equivalent to a memory location.
6082 Attempt to replace the whole subreg by a (possibly narrower or wider)
6083 memory reference. If this is possible, return this new memory
6084 reference, and push all required address reloads. Otherwise,
6085 return NULL.
6087 OPNUM and TYPE identify the purpose of the reload.
6089 IND_LEVELS says how many levels of indirect addressing are
6090 supported at this point in the address.
6092 INSN, if nonzero, is the insn in which we do the reload. It is used
6093 to determine where to put USEs for pseudos that we have to replace with
6094 stack slots. */
6096 static rtx
6097 find_reloads_subreg_address (rtx x, int opnum, enum reload_type type,
6098 int ind_levels, rtx_insn *insn,
6099 int *address_reloaded)
6101 machine_mode outer_mode = GET_MODE (x);
6102 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
6103 int regno = REGNO (SUBREG_REG (x));
6104 int reloaded = 0;
6105 rtx tem, orig;
6106 int offset;
6108 gcc_assert (reg_equiv_memory_loc (regno) != 0);
6110 /* We cannot replace the subreg with a modified memory reference if:
6112 - we have a paradoxical subreg that implicitly acts as a zero or
6113 sign extension operation due to LOAD_EXTEND_OP;
6115 - we have a subreg that is implicitly supposed to act on the full
6116 register due to WORD_REGISTER_OPERATIONS (see also eliminate_regs);
6118 - the address of the equivalent memory location is mode-dependent; or
6120 - we have a paradoxical subreg and the resulting memory is not
6121 sufficiently aligned to allow access in the wider mode.
6123 In addition, we choose not to perform the replacement for *any*
6124 paradoxical subreg, even if it were possible in principle. This
6125 is to avoid generating wider memory references than necessary.
6127 This corresponds to how previous versions of reload used to handle
6128 paradoxical subregs where no address reload was required. */
6130 if (paradoxical_subreg_p (x))
6131 return NULL;
6133 if (WORD_REGISTER_OPERATIONS
6134 && partial_subreg_p (outer_mode, inner_mode)
6135 && ((GET_MODE_SIZE (outer_mode) - 1) / UNITS_PER_WORD
6136 == (GET_MODE_SIZE (inner_mode) - 1) / UNITS_PER_WORD))
6137 return NULL;
6139 /* Since we don't attempt to handle paradoxical subregs, we can just
6140 call into simplify_subreg, which will handle all remaining checks
6141 for us. */
6142 orig = make_memloc (SUBREG_REG (x), regno);
6143 offset = SUBREG_BYTE (x);
6144 tem = simplify_subreg (outer_mode, orig, inner_mode, offset);
6145 if (!tem || !MEM_P (tem))
6146 return NULL;
6148 /* Now push all required address reloads, if any. */
6149 reloaded = find_reloads_address (GET_MODE (tem), &tem,
6150 XEXP (tem, 0), &XEXP (tem, 0),
6151 opnum, type, ind_levels, insn);
6152 /* ??? Do we need to handle nonzero offsets somehow? */
6153 if (!offset && !rtx_equal_p (tem, orig))
6154 push_reg_equiv_alt_mem (regno, tem);
6156 /* For some processors an address may be valid in the original mode but
6157 not in a smaller mode. For example, ARM accepts a scaled index register
6158 in SImode but not in HImode. Note that this is only a problem if the
6159 address in reg_equiv_mem is already invalid in the new mode; other
6160 cases would be fixed by find_reloads_address as usual.
6162 ??? We attempt to handle such cases here by doing an additional reload
6163 of the full address after the usual processing by find_reloads_address.
6164 Note that this may not work in the general case, but it seems to cover
6165 the cases where this situation currently occurs. A more general fix
6166 might be to reload the *value* instead of the address, but this would
6167 not be expected by the callers of this routine as-is.
6169 If find_reloads_address already completed replaced the address, there
6170 is nothing further to do. */
6171 if (reloaded == 0
6172 && reg_equiv_mem (regno) != 0
6173 && !strict_memory_address_addr_space_p
6174 (GET_MODE (x), XEXP (reg_equiv_mem (regno), 0),
6175 MEM_ADDR_SPACE (reg_equiv_mem (regno))))
6177 push_reload (XEXP (tem, 0), NULL_RTX, &XEXP (tem, 0), (rtx*) 0,
6178 base_reg_class (GET_MODE (tem), MEM_ADDR_SPACE (tem),
6179 MEM, SCRATCH),
6180 GET_MODE (XEXP (tem, 0)), VOIDmode, 0, 0, opnum, type);
6181 reloaded = 1;
6184 /* If this is not a toplevel operand, find_reloads doesn't see this
6185 substitution. We have to emit a USE of the pseudo so that
6186 delete_output_reload can see it. */
6187 if (replace_reloads && recog_data.operand[opnum] != x)
6188 /* We mark the USE with QImode so that we recognize it as one that
6189 can be safely deleted at the end of reload. */
6190 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, SUBREG_REG (x)), insn),
6191 QImode);
6193 if (address_reloaded)
6194 *address_reloaded = reloaded;
6196 return tem;
6199 /* Substitute into the current INSN the registers into which we have reloaded
6200 the things that need reloading. The array `replacements'
6201 contains the locations of all pointers that must be changed
6202 and says what to replace them with.
6204 Return the rtx that X translates into; usually X, but modified. */
6206 void
6207 subst_reloads (rtx_insn *insn)
6209 int i;
6211 for (i = 0; i < n_replacements; i++)
6213 struct replacement *r = &replacements[i];
6214 rtx reloadreg = rld[r->what].reg_rtx;
6215 if (reloadreg)
6217 #ifdef DEBUG_RELOAD
6218 /* This checking takes a very long time on some platforms
6219 causing the gcc.c-torture/compile/limits-fnargs.c test
6220 to time out during testing. See PR 31850.
6222 Internal consistency test. Check that we don't modify
6223 anything in the equivalence arrays. Whenever something from
6224 those arrays needs to be reloaded, it must be unshared before
6225 being substituted into; the equivalence must not be modified.
6226 Otherwise, if the equivalence is used after that, it will
6227 have been modified, and the thing substituted (probably a
6228 register) is likely overwritten and not a usable equivalence. */
6229 int check_regno;
6231 for (check_regno = 0; check_regno < max_regno; check_regno++)
6233 #define CHECK_MODF(ARRAY) \
6234 gcc_assert (!(*reg_equivs)[check_regno].ARRAY \
6235 || !loc_mentioned_in_p (r->where, \
6236 (*reg_equivs)[check_regno].ARRAY))
6238 CHECK_MODF (constant);
6239 CHECK_MODF (memory_loc);
6240 CHECK_MODF (address);
6241 CHECK_MODF (mem);
6242 #undef CHECK_MODF
6244 #endif /* DEBUG_RELOAD */
6246 /* If we're replacing a LABEL_REF with a register, there must
6247 already be an indication (to e.g. flow) which label this
6248 register refers to. */
6249 gcc_assert (GET_CODE (*r->where) != LABEL_REF
6250 || !JUMP_P (insn)
6251 || find_reg_note (insn,
6252 REG_LABEL_OPERAND,
6253 XEXP (*r->where, 0))
6254 || label_is_jump_target_p (XEXP (*r->where, 0), insn));
6256 /* Encapsulate RELOADREG so its machine mode matches what
6257 used to be there. Note that gen_lowpart_common will
6258 do the wrong thing if RELOADREG is multi-word. RELOADREG
6259 will always be a REG here. */
6260 if (GET_MODE (reloadreg) != r->mode && r->mode != VOIDmode)
6261 reloadreg = reload_adjust_reg_for_mode (reloadreg, r->mode);
6263 *r->where = reloadreg;
6265 /* If reload got no reg and isn't optional, something's wrong. */
6266 else
6267 gcc_assert (rld[r->what].optional);
6271 /* Make a copy of any replacements being done into X and move those
6272 copies to locations in Y, a copy of X. */
6274 void
6275 copy_replacements (rtx x, rtx y)
6277 copy_replacements_1 (&x, &y, n_replacements);
6280 static void
6281 copy_replacements_1 (rtx *px, rtx *py, int orig_replacements)
6283 int i, j;
6284 rtx x, y;
6285 struct replacement *r;
6286 enum rtx_code code;
6287 const char *fmt;
6289 for (j = 0; j < orig_replacements; j++)
6290 if (replacements[j].where == px)
6292 r = &replacements[n_replacements++];
6293 r->where = py;
6294 r->what = replacements[j].what;
6295 r->mode = replacements[j].mode;
6298 x = *px;
6299 y = *py;
6300 code = GET_CODE (x);
6301 fmt = GET_RTX_FORMAT (code);
6303 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6305 if (fmt[i] == 'e')
6306 copy_replacements_1 (&XEXP (x, i), &XEXP (y, i), orig_replacements);
6307 else if (fmt[i] == 'E')
6308 for (j = XVECLEN (x, i); --j >= 0; )
6309 copy_replacements_1 (&XVECEXP (x, i, j), &XVECEXP (y, i, j),
6310 orig_replacements);
6314 /* Change any replacements being done to *X to be done to *Y. */
6316 void
6317 move_replacements (rtx *x, rtx *y)
6319 int i;
6321 for (i = 0; i < n_replacements; i++)
6322 if (replacements[i].where == x)
6323 replacements[i].where = y;
6326 /* If LOC was scheduled to be replaced by something, return the replacement.
6327 Otherwise, return *LOC. */
6330 find_replacement (rtx *loc)
6332 struct replacement *r;
6334 for (r = &replacements[0]; r < &replacements[n_replacements]; r++)
6336 rtx reloadreg = rld[r->what].reg_rtx;
6338 if (reloadreg && r->where == loc)
6340 if (r->mode != VOIDmode && GET_MODE (reloadreg) != r->mode)
6341 reloadreg = reload_adjust_reg_for_mode (reloadreg, r->mode);
6343 return reloadreg;
6345 else if (reloadreg && GET_CODE (*loc) == SUBREG
6346 && r->where == &SUBREG_REG (*loc))
6348 if (r->mode != VOIDmode && GET_MODE (reloadreg) != r->mode)
6349 reloadreg = reload_adjust_reg_for_mode (reloadreg, r->mode);
6351 return simplify_gen_subreg (GET_MODE (*loc), reloadreg,
6352 GET_MODE (SUBREG_REG (*loc)),
6353 SUBREG_BYTE (*loc));
6357 /* If *LOC is a PLUS, MINUS, or MULT, see if a replacement is scheduled for
6358 what's inside and make a new rtl if so. */
6359 if (GET_CODE (*loc) == PLUS || GET_CODE (*loc) == MINUS
6360 || GET_CODE (*loc) == MULT)
6362 rtx x = find_replacement (&XEXP (*loc, 0));
6363 rtx y = find_replacement (&XEXP (*loc, 1));
6365 if (x != XEXP (*loc, 0) || y != XEXP (*loc, 1))
6366 return gen_rtx_fmt_ee (GET_CODE (*loc), GET_MODE (*loc), x, y);
6369 return *loc;
6372 /* Return nonzero if register in range [REGNO, ENDREGNO)
6373 appears either explicitly or implicitly in X
6374 other than being stored into (except for earlyclobber operands).
6376 References contained within the substructure at LOC do not count.
6377 LOC may be zero, meaning don't ignore anything.
6379 This is similar to refers_to_regno_p in rtlanal.c except that we
6380 look at equivalences for pseudos that didn't get hard registers. */
6382 static int
6383 refers_to_regno_for_reload_p (unsigned int regno, unsigned int endregno,
6384 rtx x, rtx *loc)
6386 int i;
6387 unsigned int r;
6388 RTX_CODE code;
6389 const char *fmt;
6391 if (x == 0)
6392 return 0;
6394 repeat:
6395 code = GET_CODE (x);
6397 switch (code)
6399 case REG:
6400 r = REGNO (x);
6402 /* If this is a pseudo, a hard register must not have been allocated.
6403 X must therefore either be a constant or be in memory. */
6404 if (r >= FIRST_PSEUDO_REGISTER)
6406 if (reg_equiv_memory_loc (r))
6407 return refers_to_regno_for_reload_p (regno, endregno,
6408 reg_equiv_memory_loc (r),
6409 (rtx*) 0);
6411 gcc_assert (reg_equiv_constant (r) || reg_equiv_invariant (r));
6412 return 0;
6415 return endregno > r && regno < END_REGNO (x);
6417 case SUBREG:
6418 /* If this is a SUBREG of a hard reg, we can see exactly which
6419 registers are being modified. Otherwise, handle normally. */
6420 if (REG_P (SUBREG_REG (x))
6421 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
6423 unsigned int inner_regno = subreg_regno (x);
6424 unsigned int inner_endregno
6425 = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
6426 ? subreg_nregs (x) : 1);
6428 return endregno > inner_regno && regno < inner_endregno;
6430 break;
6432 case CLOBBER:
6433 case SET:
6434 if (&SET_DEST (x) != loc
6435 /* Note setting a SUBREG counts as referring to the REG it is in for
6436 a pseudo but not for hard registers since we can
6437 treat each word individually. */
6438 && ((GET_CODE (SET_DEST (x)) == SUBREG
6439 && loc != &SUBREG_REG (SET_DEST (x))
6440 && REG_P (SUBREG_REG (SET_DEST (x)))
6441 && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
6442 && refers_to_regno_for_reload_p (regno, endregno,
6443 SUBREG_REG (SET_DEST (x)),
6444 loc))
6445 /* If the output is an earlyclobber operand, this is
6446 a conflict. */
6447 || ((!REG_P (SET_DEST (x))
6448 || earlyclobber_operand_p (SET_DEST (x)))
6449 && refers_to_regno_for_reload_p (regno, endregno,
6450 SET_DEST (x), loc))))
6451 return 1;
6453 if (code == CLOBBER || loc == &SET_SRC (x))
6454 return 0;
6455 x = SET_SRC (x);
6456 goto repeat;
6458 default:
6459 break;
6462 /* X does not match, so try its subexpressions. */
6464 fmt = GET_RTX_FORMAT (code);
6465 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6467 if (fmt[i] == 'e' && loc != &XEXP (x, i))
6469 if (i == 0)
6471 x = XEXP (x, 0);
6472 goto repeat;
6474 else
6475 if (refers_to_regno_for_reload_p (regno, endregno,
6476 XEXP (x, i), loc))
6477 return 1;
6479 else if (fmt[i] == 'E')
6481 int j;
6482 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6483 if (loc != &XVECEXP (x, i, j)
6484 && refers_to_regno_for_reload_p (regno, endregno,
6485 XVECEXP (x, i, j), loc))
6486 return 1;
6489 return 0;
6492 /* Nonzero if modifying X will affect IN. If X is a register or a SUBREG,
6493 we check if any register number in X conflicts with the relevant register
6494 numbers. If X is a constant, return 0. If X is a MEM, return 1 iff IN
6495 contains a MEM (we don't bother checking for memory addresses that can't
6496 conflict because we expect this to be a rare case.
6498 This function is similar to reg_overlap_mentioned_p in rtlanal.c except
6499 that we look at equivalences for pseudos that didn't get hard registers. */
6502 reg_overlap_mentioned_for_reload_p (rtx x, rtx in)
6504 int regno, endregno;
6506 /* Overly conservative. */
6507 if (GET_CODE (x) == STRICT_LOW_PART
6508 || GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
6509 x = XEXP (x, 0);
6511 /* If either argument is a constant, then modifying X can not affect IN. */
6512 if (CONSTANT_P (x) || CONSTANT_P (in))
6513 return 0;
6514 else if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
6515 return refers_to_mem_for_reload_p (in);
6516 else if (GET_CODE (x) == SUBREG)
6518 regno = REGNO (SUBREG_REG (x));
6519 if (regno < FIRST_PSEUDO_REGISTER)
6520 regno += subreg_regno_offset (REGNO (SUBREG_REG (x)),
6521 GET_MODE (SUBREG_REG (x)),
6522 SUBREG_BYTE (x),
6523 GET_MODE (x));
6524 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
6525 ? subreg_nregs (x) : 1);
6527 return refers_to_regno_for_reload_p (regno, endregno, in, (rtx*) 0);
6529 else if (REG_P (x))
6531 regno = REGNO (x);
6533 /* If this is a pseudo, it must not have been assigned a hard register.
6534 Therefore, it must either be in memory or be a constant. */
6536 if (regno >= FIRST_PSEUDO_REGISTER)
6538 if (reg_equiv_memory_loc (regno))
6539 return refers_to_mem_for_reload_p (in);
6540 gcc_assert (reg_equiv_constant (regno));
6541 return 0;
6544 endregno = END_REGNO (x);
6546 return refers_to_regno_for_reload_p (regno, endregno, in, (rtx*) 0);
6548 else if (MEM_P (x))
6549 return refers_to_mem_for_reload_p (in);
6550 else if (GET_CODE (x) == SCRATCH || GET_CODE (x) == PC
6551 || GET_CODE (x) == CC0)
6552 return reg_mentioned_p (x, in);
6553 else
6555 gcc_assert (GET_CODE (x) == PLUS);
6557 /* We actually want to know if X is mentioned somewhere inside IN.
6558 We must not say that (plus (sp) (const_int 124)) is in
6559 (plus (sp) (const_int 64)), since that can lead to incorrect reload
6560 allocation when spuriously changing a RELOAD_FOR_OUTPUT_ADDRESS
6561 into a RELOAD_OTHER on behalf of another RELOAD_OTHER. */
6562 while (MEM_P (in))
6563 in = XEXP (in, 0);
6564 if (REG_P (in))
6565 return 0;
6566 else if (GET_CODE (in) == PLUS)
6567 return (rtx_equal_p (x, in)
6568 || reg_overlap_mentioned_for_reload_p (x, XEXP (in, 0))
6569 || reg_overlap_mentioned_for_reload_p (x, XEXP (in, 1)));
6570 else return (reg_overlap_mentioned_for_reload_p (XEXP (x, 0), in)
6571 || reg_overlap_mentioned_for_reload_p (XEXP (x, 1), in));
6574 gcc_unreachable ();
6577 /* Return nonzero if anything in X contains a MEM. Look also for pseudo
6578 registers. */
6580 static int
6581 refers_to_mem_for_reload_p (rtx x)
6583 const char *fmt;
6584 int i;
6586 if (MEM_P (x))
6587 return 1;
6589 if (REG_P (x))
6590 return (REGNO (x) >= FIRST_PSEUDO_REGISTER
6591 && reg_equiv_memory_loc (REGNO (x)));
6593 fmt = GET_RTX_FORMAT (GET_CODE (x));
6594 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6595 if (fmt[i] == 'e'
6596 && (MEM_P (XEXP (x, i))
6597 || refers_to_mem_for_reload_p (XEXP (x, i))))
6598 return 1;
6600 return 0;
6603 /* Check the insns before INSN to see if there is a suitable register
6604 containing the same value as GOAL.
6605 If OTHER is -1, look for a register in class RCLASS.
6606 Otherwise, just see if register number OTHER shares GOAL's value.
6608 Return an rtx for the register found, or zero if none is found.
6610 If RELOAD_REG_P is (short *)1,
6611 we reject any hard reg that appears in reload_reg_rtx
6612 because such a hard reg is also needed coming into this insn.
6614 If RELOAD_REG_P is any other nonzero value,
6615 it is a vector indexed by hard reg number
6616 and we reject any hard reg whose element in the vector is nonnegative
6617 as well as any that appears in reload_reg_rtx.
6619 If GOAL is zero, then GOALREG is a register number; we look
6620 for an equivalent for that register.
6622 MODE is the machine mode of the value we want an equivalence for.
6623 If GOAL is nonzero and not VOIDmode, then it must have mode MODE.
6625 This function is used by jump.c as well as in the reload pass.
6627 If GOAL is the sum of the stack pointer and a constant, we treat it
6628 as if it were a constant except that sp is required to be unchanging. */
6631 find_equiv_reg (rtx goal, rtx_insn *insn, enum reg_class rclass, int other,
6632 short *reload_reg_p, int goalreg, machine_mode mode)
6634 rtx_insn *p = insn;
6635 rtx goaltry, valtry, value;
6636 rtx_insn *where;
6637 rtx pat;
6638 int regno = -1;
6639 int valueno;
6640 int goal_mem = 0;
6641 int goal_const = 0;
6642 int goal_mem_addr_varies = 0;
6643 int need_stable_sp = 0;
6644 int nregs;
6645 int valuenregs;
6646 int num = 0;
6648 if (goal == 0)
6649 regno = goalreg;
6650 else if (REG_P (goal))
6651 regno = REGNO (goal);
6652 else if (MEM_P (goal))
6654 enum rtx_code code = GET_CODE (XEXP (goal, 0));
6655 if (MEM_VOLATILE_P (goal))
6656 return 0;
6657 if (flag_float_store && SCALAR_FLOAT_MODE_P (GET_MODE (goal)))
6658 return 0;
6659 /* An address with side effects must be reexecuted. */
6660 switch (code)
6662 case POST_INC:
6663 case PRE_INC:
6664 case POST_DEC:
6665 case PRE_DEC:
6666 case POST_MODIFY:
6667 case PRE_MODIFY:
6668 return 0;
6669 default:
6670 break;
6672 goal_mem = 1;
6674 else if (CONSTANT_P (goal))
6675 goal_const = 1;
6676 else if (GET_CODE (goal) == PLUS
6677 && XEXP (goal, 0) == stack_pointer_rtx
6678 && CONSTANT_P (XEXP (goal, 1)))
6679 goal_const = need_stable_sp = 1;
6680 else if (GET_CODE (goal) == PLUS
6681 && XEXP (goal, 0) == frame_pointer_rtx
6682 && CONSTANT_P (XEXP (goal, 1)))
6683 goal_const = 1;
6684 else
6685 return 0;
6687 num = 0;
6688 /* Scan insns back from INSN, looking for one that copies
6689 a value into or out of GOAL.
6690 Stop and give up if we reach a label. */
6692 while (1)
6694 p = PREV_INSN (p);
6695 if (p && DEBUG_INSN_P (p))
6696 continue;
6697 num++;
6698 if (p == 0 || LABEL_P (p)
6699 || num > PARAM_VALUE (PARAM_MAX_RELOAD_SEARCH_INSNS))
6700 return 0;
6702 /* Don't reuse register contents from before a setjmp-type
6703 function call; on the second return (from the longjmp) it
6704 might have been clobbered by a later reuse. It doesn't
6705 seem worthwhile to actually go and see if it is actually
6706 reused even if that information would be readily available;
6707 just don't reuse it across the setjmp call. */
6708 if (CALL_P (p) && find_reg_note (p, REG_SETJMP, NULL_RTX))
6709 return 0;
6711 if (NONJUMP_INSN_P (p)
6712 /* If we don't want spill regs ... */
6713 && (! (reload_reg_p != 0
6714 && reload_reg_p != (short *) HOST_WIDE_INT_1)
6715 /* ... then ignore insns introduced by reload; they aren't
6716 useful and can cause results in reload_as_needed to be
6717 different from what they were when calculating the need for
6718 spills. If we notice an input-reload insn here, we will
6719 reject it below, but it might hide a usable equivalent.
6720 That makes bad code. It may even fail: perhaps no reg was
6721 spilled for this insn because it was assumed we would find
6722 that equivalent. */
6723 || INSN_UID (p) < reload_first_uid))
6725 rtx tem;
6726 pat = single_set (p);
6728 /* First check for something that sets some reg equal to GOAL. */
6729 if (pat != 0
6730 && ((regno >= 0
6731 && true_regnum (SET_SRC (pat)) == regno
6732 && (valueno = true_regnum (valtry = SET_DEST (pat))) >= 0)
6734 (regno >= 0
6735 && true_regnum (SET_DEST (pat)) == regno
6736 && (valueno = true_regnum (valtry = SET_SRC (pat))) >= 0)
6738 (goal_const && rtx_equal_p (SET_SRC (pat), goal)
6739 /* When looking for stack pointer + const,
6740 make sure we don't use a stack adjust. */
6741 && !reg_overlap_mentioned_for_reload_p (SET_DEST (pat), goal)
6742 && (valueno = true_regnum (valtry = SET_DEST (pat))) >= 0)
6743 || (goal_mem
6744 && (valueno = true_regnum (valtry = SET_DEST (pat))) >= 0
6745 && rtx_renumbered_equal_p (goal, SET_SRC (pat)))
6746 || (goal_mem
6747 && (valueno = true_regnum (valtry = SET_SRC (pat))) >= 0
6748 && rtx_renumbered_equal_p (goal, SET_DEST (pat)))
6749 /* If we are looking for a constant,
6750 and something equivalent to that constant was copied
6751 into a reg, we can use that reg. */
6752 || (goal_const && REG_NOTES (p) != 0
6753 && (tem = find_reg_note (p, REG_EQUIV, NULL_RTX))
6754 && ((rtx_equal_p (XEXP (tem, 0), goal)
6755 && (valueno
6756 = true_regnum (valtry = SET_DEST (pat))) >= 0)
6757 || (REG_P (SET_DEST (pat))
6758 && CONST_DOUBLE_AS_FLOAT_P (XEXP (tem, 0))
6759 && SCALAR_FLOAT_MODE_P (GET_MODE (XEXP (tem, 0)))
6760 && CONST_INT_P (goal)
6761 && 0 != (goaltry
6762 = operand_subword (XEXP (tem, 0), 0, 0,
6763 VOIDmode))
6764 && rtx_equal_p (goal, goaltry)
6765 && (valtry
6766 = operand_subword (SET_DEST (pat), 0, 0,
6767 VOIDmode))
6768 && (valueno = true_regnum (valtry)) >= 0)))
6769 || (goal_const && (tem = find_reg_note (p, REG_EQUIV,
6770 NULL_RTX))
6771 && REG_P (SET_DEST (pat))
6772 && CONST_DOUBLE_AS_FLOAT_P (XEXP (tem, 0))
6773 && SCALAR_FLOAT_MODE_P (GET_MODE (XEXP (tem, 0)))
6774 && CONST_INT_P (goal)
6775 && 0 != (goaltry = operand_subword (XEXP (tem, 0), 1, 0,
6776 VOIDmode))
6777 && rtx_equal_p (goal, goaltry)
6778 && (valtry
6779 = operand_subword (SET_DEST (pat), 1, 0, VOIDmode))
6780 && (valueno = true_regnum (valtry)) >= 0)))
6782 if (other >= 0)
6784 if (valueno != other)
6785 continue;
6787 else if ((unsigned) valueno >= FIRST_PSEUDO_REGISTER)
6788 continue;
6789 else if (!in_hard_reg_set_p (reg_class_contents[(int) rclass],
6790 mode, valueno))
6791 continue;
6792 value = valtry;
6793 where = p;
6794 break;
6799 /* We found a previous insn copying GOAL into a suitable other reg VALUE
6800 (or copying VALUE into GOAL, if GOAL is also a register).
6801 Now verify that VALUE is really valid. */
6803 /* VALUENO is the register number of VALUE; a hard register. */
6805 /* Don't try to re-use something that is killed in this insn. We want
6806 to be able to trust REG_UNUSED notes. */
6807 if (REG_NOTES (where) != 0 && find_reg_note (where, REG_UNUSED, value))
6808 return 0;
6810 /* If we propose to get the value from the stack pointer or if GOAL is
6811 a MEM based on the stack pointer, we need a stable SP. */
6812 if (valueno == STACK_POINTER_REGNUM || regno == STACK_POINTER_REGNUM
6813 || (goal_mem && reg_overlap_mentioned_for_reload_p (stack_pointer_rtx,
6814 goal)))
6815 need_stable_sp = 1;
6817 /* Reject VALUE if the copy-insn moved the wrong sort of datum. */
6818 if (GET_MODE (value) != mode)
6819 return 0;
6821 /* Reject VALUE if it was loaded from GOAL
6822 and is also a register that appears in the address of GOAL. */
6824 if (goal_mem && value == SET_DEST (single_set (where))
6825 && refers_to_regno_for_reload_p (valueno, end_hard_regno (mode, valueno),
6826 goal, (rtx*) 0))
6827 return 0;
6829 /* Reject registers that overlap GOAL. */
6831 if (regno >= 0 && regno < FIRST_PSEUDO_REGISTER)
6832 nregs = hard_regno_nregs (regno, mode);
6833 else
6834 nregs = 1;
6835 valuenregs = hard_regno_nregs (valueno, mode);
6837 if (!goal_mem && !goal_const
6838 && regno + nregs > valueno && regno < valueno + valuenregs)
6839 return 0;
6841 /* Reject VALUE if it is one of the regs reserved for reloads.
6842 Reload1 knows how to reuse them anyway, and it would get
6843 confused if we allocated one without its knowledge.
6844 (Now that insns introduced by reload are ignored above,
6845 this case shouldn't happen, but I'm not positive.) */
6847 if (reload_reg_p != 0 && reload_reg_p != (short *) HOST_WIDE_INT_1)
6849 int i;
6850 for (i = 0; i < valuenregs; ++i)
6851 if (reload_reg_p[valueno + i] >= 0)
6852 return 0;
6855 /* Reject VALUE if it is a register being used for an input reload
6856 even if it is not one of those reserved. */
6858 if (reload_reg_p != 0)
6860 int i;
6861 for (i = 0; i < n_reloads; i++)
6862 if (rld[i].reg_rtx != 0
6863 && rld[i].in
6864 && (int) REGNO (rld[i].reg_rtx) < valueno + valuenregs
6865 && (int) END_REGNO (rld[i].reg_rtx) > valueno)
6866 return 0;
6869 if (goal_mem)
6870 /* We must treat frame pointer as varying here,
6871 since it can vary--in a nonlocal goto as generated by expand_goto. */
6872 goal_mem_addr_varies = !CONSTANT_ADDRESS_P (XEXP (goal, 0));
6874 /* Now verify that the values of GOAL and VALUE remain unaltered
6875 until INSN is reached. */
6877 p = insn;
6878 while (1)
6880 p = PREV_INSN (p);
6881 if (p == where)
6882 return value;
6884 /* Don't trust the conversion past a function call
6885 if either of the two is in a call-clobbered register, or memory. */
6886 if (CALL_P (p))
6888 int i;
6890 if (goal_mem || need_stable_sp)
6891 return 0;
6893 if (regno >= 0 && regno < FIRST_PSEUDO_REGISTER)
6894 for (i = 0; i < nregs; ++i)
6895 if (call_used_regs[regno + i]
6896 || targetm.hard_regno_call_part_clobbered (regno + i, mode))
6897 return 0;
6899 if (valueno >= 0 && valueno < FIRST_PSEUDO_REGISTER)
6900 for (i = 0; i < valuenregs; ++i)
6901 if (call_used_regs[valueno + i]
6902 || targetm.hard_regno_call_part_clobbered (valueno + i,
6903 mode))
6904 return 0;
6907 if (INSN_P (p))
6909 pat = PATTERN (p);
6911 /* Watch out for unspec_volatile, and volatile asms. */
6912 if (volatile_insn_p (pat))
6913 return 0;
6915 /* If this insn P stores in either GOAL or VALUE, return 0.
6916 If GOAL is a memory ref and this insn writes memory, return 0.
6917 If GOAL is a memory ref and its address is not constant,
6918 and this insn P changes a register used in GOAL, return 0. */
6920 if (GET_CODE (pat) == COND_EXEC)
6921 pat = COND_EXEC_CODE (pat);
6922 if (GET_CODE (pat) == SET || GET_CODE (pat) == CLOBBER)
6924 rtx dest = SET_DEST (pat);
6925 while (GET_CODE (dest) == SUBREG
6926 || GET_CODE (dest) == ZERO_EXTRACT
6927 || GET_CODE (dest) == STRICT_LOW_PART)
6928 dest = XEXP (dest, 0);
6929 if (REG_P (dest))
6931 int xregno = REGNO (dest);
6932 int end_xregno = END_REGNO (dest);
6933 if (xregno < regno + nregs && end_xregno > regno)
6934 return 0;
6935 if (xregno < valueno + valuenregs
6936 && end_xregno > valueno)
6937 return 0;
6938 if (goal_mem_addr_varies
6939 && reg_overlap_mentioned_for_reload_p (dest, goal))
6940 return 0;
6941 if (xregno == STACK_POINTER_REGNUM && need_stable_sp)
6942 return 0;
6944 else if (goal_mem && MEM_P (dest)
6945 && ! push_operand (dest, GET_MODE (dest)))
6946 return 0;
6947 else if (MEM_P (dest) && regno >= FIRST_PSEUDO_REGISTER
6948 && reg_equiv_memory_loc (regno) != 0)
6949 return 0;
6950 else if (need_stable_sp && push_operand (dest, GET_MODE (dest)))
6951 return 0;
6953 else if (GET_CODE (pat) == PARALLEL)
6955 int i;
6956 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
6958 rtx v1 = XVECEXP (pat, 0, i);
6959 if (GET_CODE (v1) == COND_EXEC)
6960 v1 = COND_EXEC_CODE (v1);
6961 if (GET_CODE (v1) == SET || GET_CODE (v1) == CLOBBER)
6963 rtx dest = SET_DEST (v1);
6964 while (GET_CODE (dest) == SUBREG
6965 || GET_CODE (dest) == ZERO_EXTRACT
6966 || GET_CODE (dest) == STRICT_LOW_PART)
6967 dest = XEXP (dest, 0);
6968 if (REG_P (dest))
6970 int xregno = REGNO (dest);
6971 int end_xregno = END_REGNO (dest);
6972 if (xregno < regno + nregs
6973 && end_xregno > regno)
6974 return 0;
6975 if (xregno < valueno + valuenregs
6976 && end_xregno > valueno)
6977 return 0;
6978 if (goal_mem_addr_varies
6979 && reg_overlap_mentioned_for_reload_p (dest,
6980 goal))
6981 return 0;
6982 if (xregno == STACK_POINTER_REGNUM && need_stable_sp)
6983 return 0;
6985 else if (goal_mem && MEM_P (dest)
6986 && ! push_operand (dest, GET_MODE (dest)))
6987 return 0;
6988 else if (MEM_P (dest) && regno >= FIRST_PSEUDO_REGISTER
6989 && reg_equiv_memory_loc (regno) != 0)
6990 return 0;
6991 else if (need_stable_sp
6992 && push_operand (dest, GET_MODE (dest)))
6993 return 0;
6998 if (CALL_P (p) && CALL_INSN_FUNCTION_USAGE (p))
7000 rtx link;
7002 for (link = CALL_INSN_FUNCTION_USAGE (p); XEXP (link, 1) != 0;
7003 link = XEXP (link, 1))
7005 pat = XEXP (link, 0);
7006 if (GET_CODE (pat) == CLOBBER)
7008 rtx dest = SET_DEST (pat);
7010 if (REG_P (dest))
7012 int xregno = REGNO (dest);
7013 int end_xregno = END_REGNO (dest);
7015 if (xregno < regno + nregs
7016 && end_xregno > regno)
7017 return 0;
7018 else if (xregno < valueno + valuenregs
7019 && end_xregno > valueno)
7020 return 0;
7021 else if (goal_mem_addr_varies
7022 && reg_overlap_mentioned_for_reload_p (dest,
7023 goal))
7024 return 0;
7027 else if (goal_mem && MEM_P (dest)
7028 && ! push_operand (dest, GET_MODE (dest)))
7029 return 0;
7030 else if (need_stable_sp
7031 && push_operand (dest, GET_MODE (dest)))
7032 return 0;
7037 #if AUTO_INC_DEC
7038 /* If this insn auto-increments or auto-decrements
7039 either regno or valueno, return 0 now.
7040 If GOAL is a memory ref and its address is not constant,
7041 and this insn P increments a register used in GOAL, return 0. */
7043 rtx link;
7045 for (link = REG_NOTES (p); link; link = XEXP (link, 1))
7046 if (REG_NOTE_KIND (link) == REG_INC
7047 && REG_P (XEXP (link, 0)))
7049 int incno = REGNO (XEXP (link, 0));
7050 if (incno < regno + nregs && incno >= regno)
7051 return 0;
7052 if (incno < valueno + valuenregs && incno >= valueno)
7053 return 0;
7054 if (goal_mem_addr_varies
7055 && reg_overlap_mentioned_for_reload_p (XEXP (link, 0),
7056 goal))
7057 return 0;
7060 #endif
7065 /* Find a place where INCED appears in an increment or decrement operator
7066 within X, and return the amount INCED is incremented or decremented by.
7067 The value is always positive. */
7069 static int
7070 find_inc_amount (rtx x, rtx inced)
7072 enum rtx_code code = GET_CODE (x);
7073 const char *fmt;
7074 int i;
7076 if (code == MEM)
7078 rtx addr = XEXP (x, 0);
7079 if ((GET_CODE (addr) == PRE_DEC
7080 || GET_CODE (addr) == POST_DEC
7081 || GET_CODE (addr) == PRE_INC
7082 || GET_CODE (addr) == POST_INC)
7083 && XEXP (addr, 0) == inced)
7084 return GET_MODE_SIZE (GET_MODE (x));
7085 else if ((GET_CODE (addr) == PRE_MODIFY
7086 || GET_CODE (addr) == POST_MODIFY)
7087 && GET_CODE (XEXP (addr, 1)) == PLUS
7088 && XEXP (addr, 0) == XEXP (XEXP (addr, 1), 0)
7089 && XEXP (addr, 0) == inced
7090 && CONST_INT_P (XEXP (XEXP (addr, 1), 1)))
7092 i = INTVAL (XEXP (XEXP (addr, 1), 1));
7093 return i < 0 ? -i : i;
7097 fmt = GET_RTX_FORMAT (code);
7098 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7100 if (fmt[i] == 'e')
7102 int tem = find_inc_amount (XEXP (x, i), inced);
7103 if (tem != 0)
7104 return tem;
7106 if (fmt[i] == 'E')
7108 int j;
7109 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7111 int tem = find_inc_amount (XVECEXP (x, i, j), inced);
7112 if (tem != 0)
7113 return tem;
7118 return 0;
7121 /* Return 1 if registers from REGNO to ENDREGNO are the subjects of a
7122 REG_INC note in insn INSN. REGNO must refer to a hard register. */
7124 static int
7125 reg_inc_found_and_valid_p (unsigned int regno, unsigned int endregno,
7126 rtx insn)
7128 rtx link;
7130 if (!AUTO_INC_DEC)
7131 return 0;
7133 gcc_assert (insn);
7135 if (! INSN_P (insn))
7136 return 0;
7138 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
7139 if (REG_NOTE_KIND (link) == REG_INC)
7141 unsigned int test = (int) REGNO (XEXP (link, 0));
7142 if (test >= regno && test < endregno)
7143 return 1;
7145 return 0;
7148 /* Return 1 if register REGNO is the subject of a clobber in insn INSN.
7149 If SETS is 1, also consider SETs. If SETS is 2, enable checking
7150 REG_INC. REGNO must refer to a hard register. */
7153 regno_clobbered_p (unsigned int regno, rtx_insn *insn, machine_mode mode,
7154 int sets)
7156 /* regno must be a hard register. */
7157 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
7159 unsigned int endregno = end_hard_regno (mode, regno);
7161 if ((GET_CODE (PATTERN (insn)) == CLOBBER
7162 || (sets == 1 && GET_CODE (PATTERN (insn)) == SET))
7163 && REG_P (XEXP (PATTERN (insn), 0)))
7165 unsigned int test = REGNO (XEXP (PATTERN (insn), 0));
7167 return test >= regno && test < endregno;
7170 if (sets == 2 && reg_inc_found_and_valid_p (regno, endregno, insn))
7171 return 1;
7173 if (GET_CODE (PATTERN (insn)) == PARALLEL)
7175 int i = XVECLEN (PATTERN (insn), 0) - 1;
7177 for (; i >= 0; i--)
7179 rtx elt = XVECEXP (PATTERN (insn), 0, i);
7180 if ((GET_CODE (elt) == CLOBBER
7181 || (sets == 1 && GET_CODE (elt) == SET))
7182 && REG_P (XEXP (elt, 0)))
7184 unsigned int test = REGNO (XEXP (elt, 0));
7186 if (test >= regno && test < endregno)
7187 return 1;
7189 if (sets == 2
7190 && reg_inc_found_and_valid_p (regno, endregno, elt))
7191 return 1;
7195 return 0;
7198 /* Find the low part, with mode MODE, of a hard regno RELOADREG. */
7200 reload_adjust_reg_for_mode (rtx reloadreg, machine_mode mode)
7202 int regno;
7204 if (GET_MODE (reloadreg) == mode)
7205 return reloadreg;
7207 regno = REGNO (reloadreg);
7209 if (REG_WORDS_BIG_ENDIAN)
7210 regno += ((int) REG_NREGS (reloadreg)
7211 - (int) hard_regno_nregs (regno, mode));
7213 return gen_rtx_REG (mode, regno);
7216 static const char *const reload_when_needed_name[] =
7218 "RELOAD_FOR_INPUT",
7219 "RELOAD_FOR_OUTPUT",
7220 "RELOAD_FOR_INSN",
7221 "RELOAD_FOR_INPUT_ADDRESS",
7222 "RELOAD_FOR_INPADDR_ADDRESS",
7223 "RELOAD_FOR_OUTPUT_ADDRESS",
7224 "RELOAD_FOR_OUTADDR_ADDRESS",
7225 "RELOAD_FOR_OPERAND_ADDRESS",
7226 "RELOAD_FOR_OPADDR_ADDR",
7227 "RELOAD_OTHER",
7228 "RELOAD_FOR_OTHER_ADDRESS"
7231 /* These functions are used to print the variables set by 'find_reloads' */
7233 DEBUG_FUNCTION void
7234 debug_reload_to_stream (FILE *f)
7236 int r;
7237 const char *prefix;
7239 if (! f)
7240 f = stderr;
7241 for (r = 0; r < n_reloads; r++)
7243 fprintf (f, "Reload %d: ", r);
7245 if (rld[r].in != 0)
7247 fprintf (f, "reload_in (%s) = ",
7248 GET_MODE_NAME (rld[r].inmode));
7249 print_inline_rtx (f, rld[r].in, 24);
7250 fprintf (f, "\n\t");
7253 if (rld[r].out != 0)
7255 fprintf (f, "reload_out (%s) = ",
7256 GET_MODE_NAME (rld[r].outmode));
7257 print_inline_rtx (f, rld[r].out, 24);
7258 fprintf (f, "\n\t");
7261 fprintf (f, "%s, ", reg_class_names[(int) rld[r].rclass]);
7263 fprintf (f, "%s (opnum = %d)",
7264 reload_when_needed_name[(int) rld[r].when_needed],
7265 rld[r].opnum);
7267 if (rld[r].optional)
7268 fprintf (f, ", optional");
7270 if (rld[r].nongroup)
7271 fprintf (f, ", nongroup");
7273 if (rld[r].inc != 0)
7274 fprintf (f, ", inc by %d", rld[r].inc);
7276 if (rld[r].nocombine)
7277 fprintf (f, ", can't combine");
7279 if (rld[r].secondary_p)
7280 fprintf (f, ", secondary_reload_p");
7282 if (rld[r].in_reg != 0)
7284 fprintf (f, "\n\treload_in_reg: ");
7285 print_inline_rtx (f, rld[r].in_reg, 24);
7288 if (rld[r].out_reg != 0)
7290 fprintf (f, "\n\treload_out_reg: ");
7291 print_inline_rtx (f, rld[r].out_reg, 24);
7294 if (rld[r].reg_rtx != 0)
7296 fprintf (f, "\n\treload_reg_rtx: ");
7297 print_inline_rtx (f, rld[r].reg_rtx, 24);
7300 prefix = "\n\t";
7301 if (rld[r].secondary_in_reload != -1)
7303 fprintf (f, "%ssecondary_in_reload = %d",
7304 prefix, rld[r].secondary_in_reload);
7305 prefix = ", ";
7308 if (rld[r].secondary_out_reload != -1)
7309 fprintf (f, "%ssecondary_out_reload = %d\n",
7310 prefix, rld[r].secondary_out_reload);
7312 prefix = "\n\t";
7313 if (rld[r].secondary_in_icode != CODE_FOR_nothing)
7315 fprintf (f, "%ssecondary_in_icode = %s", prefix,
7316 insn_data[rld[r].secondary_in_icode].name);
7317 prefix = ", ";
7320 if (rld[r].secondary_out_icode != CODE_FOR_nothing)
7321 fprintf (f, "%ssecondary_out_icode = %s", prefix,
7322 insn_data[rld[r].secondary_out_icode].name);
7324 fprintf (f, "\n");
7328 DEBUG_FUNCTION void
7329 debug_reload (void)
7331 debug_reload_to_stream (stderr);