2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / reload.c
blob079f0aaa3fb1059a720d36a1f6690e0a5430e6a4
1 /* Search an insn for pseudo regs that must be in hard regs and are not.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains subroutines used only from the file reload1.c.
21 It knows how to scan one insn for operands and values
22 that need to be copied into registers to make valid code.
23 It also finds other operands and values which are valid
24 but for which equivalent values in registers exist and
25 ought to be used instead.
27 Before processing the first insn of the function, call `init_reload'.
28 init_reload actually has to be called earlier anyway.
30 To scan an insn, call `find_reloads'. This does two things:
31 1. sets up tables describing which values must be reloaded
32 for this insn, and what kind of hard regs they must be reloaded into;
33 2. optionally record the locations where those values appear in
34 the data, so they can be replaced properly later.
35 This is done only if the second arg to `find_reloads' is nonzero.
37 The third arg to `find_reloads' specifies the number of levels
38 of indirect addressing supported by the machine. If it is zero,
39 indirect addressing is not valid. If it is one, (MEM (REG n))
40 is valid even if (REG n) did not get a hard register; if it is two,
41 (MEM (MEM (REG n))) is also valid even if (REG n) did not get a
42 hard register, and similarly for higher values.
44 Then you must choose the hard regs to reload those pseudo regs into,
45 and generate appropriate load insns before this insn and perhaps
46 also store insns after this insn. Set up the array `reload_reg_rtx'
47 to contain the REG rtx's for the registers you used. In some
48 cases `find_reloads' will return a nonzero value in `reload_reg_rtx'
49 for certain reloads. Then that tells you which register to use,
50 so you do not need to allocate one. But you still do need to add extra
51 instructions to copy the value into and out of that register.
53 Finally you must call `subst_reloads' to substitute the reload reg rtx's
54 into the locations already recorded.
56 NOTE SIDE EFFECTS:
58 find_reloads can alter the operands of the instruction it is called on.
60 1. Two operands of any sort may be interchanged, if they are in a
61 commutative instruction.
62 This happens only if find_reloads thinks the instruction will compile
63 better that way.
65 2. Pseudo-registers that are equivalent to constants are replaced
66 with those constants if they are not in hard registers.
68 1 happens every time find_reloads is called.
69 2 happens only when REPLACE is 1, which is only when
70 actually doing the reloads, not when just counting them.
72 Using a reload register for several reloads in one insn:
74 When an insn has reloads, it is considered as having three parts:
75 the input reloads, the insn itself after reloading, and the output reloads.
76 Reloads of values used in memory addresses are often needed for only one part.
78 When this is so, reload_when_needed records which part needs the reload.
79 Two reloads for different parts of the insn can share the same reload
80 register.
82 When a reload is used for addresses in multiple parts, or when it is
83 an ordinary operand, it is classified as RELOAD_OTHER, and cannot share
84 a register with any other reload. */
86 #define REG_OK_STRICT
88 /* We do not enable this with ENABLE_CHECKING, since it is awfully slow. */
89 #undef DEBUG_RELOAD
91 #include "config.h"
92 #include "system.h"
93 #include "coretypes.h"
94 #include "tm.h"
95 #include "rtl-error.h"
96 #include "tm_p.h"
97 #include "insn-config.h"
98 #include "symtab.h"
99 #include "hard-reg-set.h"
100 #include "input.h"
101 #include "function.h"
102 #include "rtl.h"
103 #include "flags.h"
104 #include "alias.h"
105 #include "tree.h"
106 #include "expmed.h"
107 #include "dojump.h"
108 #include "explow.h"
109 #include "calls.h"
110 #include "emit-rtl.h"
111 #include "varasm.h"
112 #include "stmt.h"
113 #include "expr.h"
114 #include "insn-codes.h"
115 #include "optabs.h"
116 #include "recog.h"
117 #include "dominance.h"
118 #include "cfg.h"
119 #include "predict.h"
120 #include "basic-block.h"
121 #include "df.h"
122 #include "reload.h"
123 #include "regs.h"
124 #include "addresses.h"
125 #include "params.h"
126 #include "target.h"
127 #include "ira.h"
129 /* True if X is a constant that can be forced into the constant pool.
130 MODE is the mode of the operand, or VOIDmode if not known. */
131 #define CONST_POOL_OK_P(MODE, X) \
132 ((MODE) != VOIDmode \
133 && CONSTANT_P (X) \
134 && GET_CODE (X) != HIGH \
135 && !targetm.cannot_force_const_mem (MODE, X))
137 /* True if C is a non-empty register class that has too few registers
138 to be safely used as a reload target class. */
140 static inline bool
141 small_register_class_p (reg_class_t rclass)
143 return (reg_class_size [(int) rclass] == 1
144 || (reg_class_size [(int) rclass] >= 1
145 && targetm.class_likely_spilled_p (rclass)));
149 /* All reloads of the current insn are recorded here. See reload.h for
150 comments. */
151 int n_reloads;
152 struct reload rld[MAX_RELOADS];
154 /* All the "earlyclobber" operands of the current insn
155 are recorded here. */
156 int n_earlyclobbers;
157 rtx reload_earlyclobbers[MAX_RECOG_OPERANDS];
159 int reload_n_operands;
161 /* Replacing reloads.
163 If `replace_reloads' is nonzero, then as each reload is recorded
164 an entry is made for it in the table `replacements'.
165 Then later `subst_reloads' can look through that table and
166 perform all the replacements needed. */
168 /* Nonzero means record the places to replace. */
169 static int replace_reloads;
171 /* Each replacement is recorded with a structure like this. */
172 struct replacement
174 rtx *where; /* Location to store in */
175 int what; /* which reload this is for */
176 machine_mode mode; /* mode it must have */
179 static struct replacement replacements[MAX_RECOG_OPERANDS * ((MAX_REGS_PER_ADDRESS * 2) + 1)];
181 /* Number of replacements currently recorded. */
182 static int n_replacements;
184 /* Used to track what is modified by an operand. */
185 struct decomposition
187 int reg_flag; /* Nonzero if referencing a register. */
188 int safe; /* Nonzero if this can't conflict with anything. */
189 rtx base; /* Base address for MEM. */
190 HOST_WIDE_INT start; /* Starting offset or register number. */
191 HOST_WIDE_INT end; /* Ending offset or register number. */
194 #ifdef SECONDARY_MEMORY_NEEDED
196 /* Save MEMs needed to copy from one class of registers to another. One MEM
197 is used per mode, but normally only one or two modes are ever used.
199 We keep two versions, before and after register elimination. The one
200 after register elimination is record separately for each operand. This
201 is done in case the address is not valid to be sure that we separately
202 reload each. */
204 static rtx secondary_memlocs[NUM_MACHINE_MODES];
205 static rtx secondary_memlocs_elim[NUM_MACHINE_MODES][MAX_RECOG_OPERANDS];
206 static int secondary_memlocs_elim_used = 0;
207 #endif
209 /* The instruction we are doing reloads for;
210 so we can test whether a register dies in it. */
211 static rtx_insn *this_insn;
213 /* Nonzero if this instruction is a user-specified asm with operands. */
214 static int this_insn_is_asm;
216 /* If hard_regs_live_known is nonzero,
217 we can tell which hard regs are currently live,
218 at least enough to succeed in choosing dummy reloads. */
219 static int hard_regs_live_known;
221 /* Indexed by hard reg number,
222 element is nonnegative if hard reg has been spilled.
223 This vector is passed to `find_reloads' as an argument
224 and is not changed here. */
225 static short *static_reload_reg_p;
227 /* Set to 1 in subst_reg_equivs if it changes anything. */
228 static int subst_reg_equivs_changed;
230 /* On return from push_reload, holds the reload-number for the OUT
231 operand, which can be different for that from the input operand. */
232 static int output_reloadnum;
234 /* Compare two RTX's. */
235 #define MATCHES(x, y) \
236 (x == y || (x != 0 && (REG_P (x) \
237 ? REG_P (y) && REGNO (x) == REGNO (y) \
238 : rtx_equal_p (x, y) && ! side_effects_p (x))))
240 /* Indicates if two reloads purposes are for similar enough things that we
241 can merge their reloads. */
242 #define MERGABLE_RELOADS(when1, when2, op1, op2) \
243 ((when1) == RELOAD_OTHER || (when2) == RELOAD_OTHER \
244 || ((when1) == (when2) && (op1) == (op2)) \
245 || ((when1) == RELOAD_FOR_INPUT && (when2) == RELOAD_FOR_INPUT) \
246 || ((when1) == RELOAD_FOR_OPERAND_ADDRESS \
247 && (when2) == RELOAD_FOR_OPERAND_ADDRESS) \
248 || ((when1) == RELOAD_FOR_OTHER_ADDRESS \
249 && (when2) == RELOAD_FOR_OTHER_ADDRESS))
251 /* Nonzero if these two reload purposes produce RELOAD_OTHER when merged. */
252 #define MERGE_TO_OTHER(when1, when2, op1, op2) \
253 ((when1) != (when2) \
254 || ! ((op1) == (op2) \
255 || (when1) == RELOAD_FOR_INPUT \
256 || (when1) == RELOAD_FOR_OPERAND_ADDRESS \
257 || (when1) == RELOAD_FOR_OTHER_ADDRESS))
259 /* If we are going to reload an address, compute the reload type to
260 use. */
261 #define ADDR_TYPE(type) \
262 ((type) == RELOAD_FOR_INPUT_ADDRESS \
263 ? RELOAD_FOR_INPADDR_ADDRESS \
264 : ((type) == RELOAD_FOR_OUTPUT_ADDRESS \
265 ? RELOAD_FOR_OUTADDR_ADDRESS \
266 : (type)))
268 static int push_secondary_reload (int, rtx, int, int, enum reg_class,
269 machine_mode, enum reload_type,
270 enum insn_code *, secondary_reload_info *);
271 static enum reg_class find_valid_class (machine_mode, machine_mode,
272 int, unsigned int);
273 static void push_replacement (rtx *, int, machine_mode);
274 static void dup_replacements (rtx *, rtx *);
275 static void combine_reloads (void);
276 static int find_reusable_reload (rtx *, rtx, enum reg_class,
277 enum reload_type, int, int);
278 static rtx find_dummy_reload (rtx, rtx, rtx *, rtx *, machine_mode,
279 machine_mode, reg_class_t, int, int);
280 static int hard_reg_set_here_p (unsigned int, unsigned int, rtx);
281 static struct decomposition decompose (rtx);
282 static int immune_p (rtx, rtx, struct decomposition);
283 static bool alternative_allows_const_pool_ref (rtx, const char *, int);
284 static rtx find_reloads_toplev (rtx, int, enum reload_type, int, int,
285 rtx_insn *, int *);
286 static rtx make_memloc (rtx, int);
287 static int maybe_memory_address_addr_space_p (machine_mode, rtx,
288 addr_space_t, rtx *);
289 static int find_reloads_address (machine_mode, rtx *, rtx, rtx *,
290 int, enum reload_type, int, rtx_insn *);
291 static rtx subst_reg_equivs (rtx, rtx_insn *);
292 static rtx subst_indexed_address (rtx);
293 static void update_auto_inc_notes (rtx_insn *, int, int);
294 static int find_reloads_address_1 (machine_mode, addr_space_t, rtx, int,
295 enum rtx_code, enum rtx_code, rtx *,
296 int, enum reload_type,int, rtx_insn *);
297 static void find_reloads_address_part (rtx, rtx *, enum reg_class,
298 machine_mode, int,
299 enum reload_type, int);
300 static rtx find_reloads_subreg_address (rtx, int, enum reload_type,
301 int, rtx_insn *, int *);
302 static void copy_replacements_1 (rtx *, rtx *, int);
303 static int find_inc_amount (rtx, rtx);
304 static int refers_to_mem_for_reload_p (rtx);
305 static int refers_to_regno_for_reload_p (unsigned int, unsigned int,
306 rtx, rtx *);
308 /* Add NEW to reg_equiv_alt_mem_list[REGNO] if it's not present in the
309 list yet. */
311 static void
312 push_reg_equiv_alt_mem (int regno, rtx mem)
314 rtx it;
316 for (it = reg_equiv_alt_mem_list (regno); it; it = XEXP (it, 1))
317 if (rtx_equal_p (XEXP (it, 0), mem))
318 return;
320 reg_equiv_alt_mem_list (regno)
321 = alloc_EXPR_LIST (REG_EQUIV, mem,
322 reg_equiv_alt_mem_list (regno));
325 /* Determine if any secondary reloads are needed for loading (if IN_P is
326 nonzero) or storing (if IN_P is zero) X to or from a reload register of
327 register class RELOAD_CLASS in mode RELOAD_MODE. If secondary reloads
328 are needed, push them.
330 Return the reload number of the secondary reload we made, or -1 if
331 we didn't need one. *PICODE is set to the insn_code to use if we do
332 need a secondary reload. */
334 static int
335 push_secondary_reload (int in_p, rtx x, int opnum, int optional,
336 enum reg_class reload_class,
337 machine_mode reload_mode, enum reload_type type,
338 enum insn_code *picode, secondary_reload_info *prev_sri)
340 enum reg_class rclass = NO_REGS;
341 enum reg_class scratch_class;
342 machine_mode mode = reload_mode;
343 enum insn_code icode = CODE_FOR_nothing;
344 enum insn_code t_icode = CODE_FOR_nothing;
345 enum reload_type secondary_type;
346 int s_reload, t_reload = -1;
347 const char *scratch_constraint;
348 secondary_reload_info sri;
350 if (type == RELOAD_FOR_INPUT_ADDRESS
351 || type == RELOAD_FOR_OUTPUT_ADDRESS
352 || type == RELOAD_FOR_INPADDR_ADDRESS
353 || type == RELOAD_FOR_OUTADDR_ADDRESS)
354 secondary_type = type;
355 else
356 secondary_type = in_p ? RELOAD_FOR_INPUT_ADDRESS : RELOAD_FOR_OUTPUT_ADDRESS;
358 *picode = CODE_FOR_nothing;
360 /* If X is a paradoxical SUBREG, use the inner value to determine both the
361 mode and object being reloaded. */
362 if (paradoxical_subreg_p (x))
364 x = SUBREG_REG (x);
365 reload_mode = GET_MODE (x);
368 /* If X is a pseudo-register that has an equivalent MEM (actually, if it
369 is still a pseudo-register by now, it *must* have an equivalent MEM
370 but we don't want to assume that), use that equivalent when seeing if
371 a secondary reload is needed since whether or not a reload is needed
372 might be sensitive to the form of the MEM. */
374 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER
375 && reg_equiv_mem (REGNO (x)))
376 x = reg_equiv_mem (REGNO (x));
378 sri.icode = CODE_FOR_nothing;
379 sri.prev_sri = prev_sri;
380 rclass = (enum reg_class) targetm.secondary_reload (in_p, x, reload_class,
381 reload_mode, &sri);
382 icode = (enum insn_code) sri.icode;
384 /* If we don't need any secondary registers, done. */
385 if (rclass == NO_REGS && icode == CODE_FOR_nothing)
386 return -1;
388 if (rclass != NO_REGS)
389 t_reload = push_secondary_reload (in_p, x, opnum, optional, rclass,
390 reload_mode, type, &t_icode, &sri);
392 /* If we will be using an insn, the secondary reload is for a
393 scratch register. */
395 if (icode != CODE_FOR_nothing)
397 /* If IN_P is nonzero, the reload register will be the output in
398 operand 0. If IN_P is zero, the reload register will be the input
399 in operand 1. Outputs should have an initial "=", which we must
400 skip. */
402 /* ??? It would be useful to be able to handle only two, or more than
403 three, operands, but for now we can only handle the case of having
404 exactly three: output, input and one temp/scratch. */
405 gcc_assert (insn_data[(int) icode].n_operands == 3);
407 /* ??? We currently have no way to represent a reload that needs
408 an icode to reload from an intermediate tertiary reload register.
409 We should probably have a new field in struct reload to tag a
410 chain of scratch operand reloads onto. */
411 gcc_assert (rclass == NO_REGS);
413 scratch_constraint = insn_data[(int) icode].operand[2].constraint;
414 gcc_assert (*scratch_constraint == '=');
415 scratch_constraint++;
416 if (*scratch_constraint == '&')
417 scratch_constraint++;
418 scratch_class = (reg_class_for_constraint
419 (lookup_constraint (scratch_constraint)));
421 rclass = scratch_class;
422 mode = insn_data[(int) icode].operand[2].mode;
425 /* This case isn't valid, so fail. Reload is allowed to use the same
426 register for RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT reloads, but
427 in the case of a secondary register, we actually need two different
428 registers for correct code. We fail here to prevent the possibility of
429 silently generating incorrect code later.
431 The convention is that secondary input reloads are valid only if the
432 secondary_class is different from class. If you have such a case, you
433 can not use secondary reloads, you must work around the problem some
434 other way.
436 Allow this when a reload_in/out pattern is being used. I.e. assume
437 that the generated code handles this case. */
439 gcc_assert (!in_p || rclass != reload_class || icode != CODE_FOR_nothing
440 || t_icode != CODE_FOR_nothing);
442 /* See if we can reuse an existing secondary reload. */
443 for (s_reload = 0; s_reload < n_reloads; s_reload++)
444 if (rld[s_reload].secondary_p
445 && (reg_class_subset_p (rclass, rld[s_reload].rclass)
446 || reg_class_subset_p (rld[s_reload].rclass, rclass))
447 && ((in_p && rld[s_reload].inmode == mode)
448 || (! in_p && rld[s_reload].outmode == mode))
449 && ((in_p && rld[s_reload].secondary_in_reload == t_reload)
450 || (! in_p && rld[s_reload].secondary_out_reload == t_reload))
451 && ((in_p && rld[s_reload].secondary_in_icode == t_icode)
452 || (! in_p && rld[s_reload].secondary_out_icode == t_icode))
453 && (small_register_class_p (rclass)
454 || targetm.small_register_classes_for_mode_p (VOIDmode))
455 && MERGABLE_RELOADS (secondary_type, rld[s_reload].when_needed,
456 opnum, rld[s_reload].opnum))
458 if (in_p)
459 rld[s_reload].inmode = mode;
460 if (! in_p)
461 rld[s_reload].outmode = mode;
463 if (reg_class_subset_p (rclass, rld[s_reload].rclass))
464 rld[s_reload].rclass = rclass;
466 rld[s_reload].opnum = MIN (rld[s_reload].opnum, opnum);
467 rld[s_reload].optional &= optional;
468 rld[s_reload].secondary_p = 1;
469 if (MERGE_TO_OTHER (secondary_type, rld[s_reload].when_needed,
470 opnum, rld[s_reload].opnum))
471 rld[s_reload].when_needed = RELOAD_OTHER;
473 break;
476 if (s_reload == n_reloads)
478 #ifdef SECONDARY_MEMORY_NEEDED
479 /* If we need a memory location to copy between the two reload regs,
480 set it up now. Note that we do the input case before making
481 the reload and the output case after. This is due to the
482 way reloads are output. */
484 if (in_p && icode == CODE_FOR_nothing
485 && SECONDARY_MEMORY_NEEDED (rclass, reload_class, mode))
487 get_secondary_mem (x, reload_mode, opnum, type);
489 /* We may have just added new reloads. Make sure we add
490 the new reload at the end. */
491 s_reload = n_reloads;
493 #endif
495 /* We need to make a new secondary reload for this register class. */
496 rld[s_reload].in = rld[s_reload].out = 0;
497 rld[s_reload].rclass = rclass;
499 rld[s_reload].inmode = in_p ? mode : VOIDmode;
500 rld[s_reload].outmode = ! in_p ? mode : VOIDmode;
501 rld[s_reload].reg_rtx = 0;
502 rld[s_reload].optional = optional;
503 rld[s_reload].inc = 0;
504 /* Maybe we could combine these, but it seems too tricky. */
505 rld[s_reload].nocombine = 1;
506 rld[s_reload].in_reg = 0;
507 rld[s_reload].out_reg = 0;
508 rld[s_reload].opnum = opnum;
509 rld[s_reload].when_needed = secondary_type;
510 rld[s_reload].secondary_in_reload = in_p ? t_reload : -1;
511 rld[s_reload].secondary_out_reload = ! in_p ? t_reload : -1;
512 rld[s_reload].secondary_in_icode = in_p ? t_icode : CODE_FOR_nothing;
513 rld[s_reload].secondary_out_icode
514 = ! in_p ? t_icode : CODE_FOR_nothing;
515 rld[s_reload].secondary_p = 1;
517 n_reloads++;
519 #ifdef SECONDARY_MEMORY_NEEDED
520 if (! in_p && icode == CODE_FOR_nothing
521 && SECONDARY_MEMORY_NEEDED (reload_class, rclass, mode))
522 get_secondary_mem (x, mode, opnum, type);
523 #endif
526 *picode = icode;
527 return s_reload;
530 /* If a secondary reload is needed, return its class. If both an intermediate
531 register and a scratch register is needed, we return the class of the
532 intermediate register. */
533 reg_class_t
534 secondary_reload_class (bool in_p, reg_class_t rclass, machine_mode mode,
535 rtx x)
537 enum insn_code icode;
538 secondary_reload_info sri;
540 sri.icode = CODE_FOR_nothing;
541 sri.prev_sri = NULL;
542 rclass
543 = (enum reg_class) targetm.secondary_reload (in_p, x, rclass, mode, &sri);
544 icode = (enum insn_code) sri.icode;
546 /* If there are no secondary reloads at all, we return NO_REGS.
547 If an intermediate register is needed, we return its class. */
548 if (icode == CODE_FOR_nothing || rclass != NO_REGS)
549 return rclass;
551 /* No intermediate register is needed, but we have a special reload
552 pattern, which we assume for now needs a scratch register. */
553 return scratch_reload_class (icode);
556 /* ICODE is the insn_code of a reload pattern. Check that it has exactly
557 three operands, verify that operand 2 is an output operand, and return
558 its register class.
559 ??? We'd like to be able to handle any pattern with at least 2 operands,
560 for zero or more scratch registers, but that needs more infrastructure. */
561 enum reg_class
562 scratch_reload_class (enum insn_code icode)
564 const char *scratch_constraint;
565 enum reg_class rclass;
567 gcc_assert (insn_data[(int) icode].n_operands == 3);
568 scratch_constraint = insn_data[(int) icode].operand[2].constraint;
569 gcc_assert (*scratch_constraint == '=');
570 scratch_constraint++;
571 if (*scratch_constraint == '&')
572 scratch_constraint++;
573 rclass = reg_class_for_constraint (lookup_constraint (scratch_constraint));
574 gcc_assert (rclass != NO_REGS);
575 return rclass;
578 #ifdef SECONDARY_MEMORY_NEEDED
580 /* Return a memory location that will be used to copy X in mode MODE.
581 If we haven't already made a location for this mode in this insn,
582 call find_reloads_address on the location being returned. */
585 get_secondary_mem (rtx x ATTRIBUTE_UNUSED, machine_mode mode,
586 int opnum, enum reload_type type)
588 rtx loc;
589 int mem_valid;
591 /* By default, if MODE is narrower than a word, widen it to a word.
592 This is required because most machines that require these memory
593 locations do not support short load and stores from all registers
594 (e.g., FP registers). */
596 #ifdef SECONDARY_MEMORY_NEEDED_MODE
597 mode = SECONDARY_MEMORY_NEEDED_MODE (mode);
598 #else
599 if (GET_MODE_BITSIZE (mode) < BITS_PER_WORD && INTEGRAL_MODE_P (mode))
600 mode = mode_for_size (BITS_PER_WORD, GET_MODE_CLASS (mode), 0);
601 #endif
603 /* If we already have made a MEM for this operand in MODE, return it. */
604 if (secondary_memlocs_elim[(int) mode][opnum] != 0)
605 return secondary_memlocs_elim[(int) mode][opnum];
607 /* If this is the first time we've tried to get a MEM for this mode,
608 allocate a new one. `something_changed' in reload will get set
609 by noticing that the frame size has changed. */
611 if (secondary_memlocs[(int) mode] == 0)
613 #ifdef SECONDARY_MEMORY_NEEDED_RTX
614 secondary_memlocs[(int) mode] = SECONDARY_MEMORY_NEEDED_RTX (mode);
615 #else
616 secondary_memlocs[(int) mode]
617 = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
618 #endif
621 /* Get a version of the address doing any eliminations needed. If that
622 didn't give us a new MEM, make a new one if it isn't valid. */
624 loc = eliminate_regs (secondary_memlocs[(int) mode], VOIDmode, NULL_RTX);
625 mem_valid = strict_memory_address_addr_space_p (mode, XEXP (loc, 0),
626 MEM_ADDR_SPACE (loc));
628 if (! mem_valid && loc == secondary_memlocs[(int) mode])
629 loc = copy_rtx (loc);
631 /* The only time the call below will do anything is if the stack
632 offset is too large. In that case IND_LEVELS doesn't matter, so we
633 can just pass a zero. Adjust the type to be the address of the
634 corresponding object. If the address was valid, save the eliminated
635 address. If it wasn't valid, we need to make a reload each time, so
636 don't save it. */
638 if (! mem_valid)
640 type = (type == RELOAD_FOR_INPUT ? RELOAD_FOR_INPUT_ADDRESS
641 : type == RELOAD_FOR_OUTPUT ? RELOAD_FOR_OUTPUT_ADDRESS
642 : RELOAD_OTHER);
644 find_reloads_address (mode, &loc, XEXP (loc, 0), &XEXP (loc, 0),
645 opnum, type, 0, 0);
648 secondary_memlocs_elim[(int) mode][opnum] = loc;
649 if (secondary_memlocs_elim_used <= (int)mode)
650 secondary_memlocs_elim_used = (int)mode + 1;
651 return loc;
654 /* Clear any secondary memory locations we've made. */
656 void
657 clear_secondary_mem (void)
659 memset (secondary_memlocs, 0, sizeof secondary_memlocs);
661 #endif /* SECONDARY_MEMORY_NEEDED */
664 /* Find the largest class which has at least one register valid in
665 mode INNER, and which for every such register, that register number
666 plus N is also valid in OUTER (if in range) and is cheap to move
667 into REGNO. Such a class must exist. */
669 static enum reg_class
670 find_valid_class (machine_mode outer ATTRIBUTE_UNUSED,
671 machine_mode inner ATTRIBUTE_UNUSED, int n,
672 unsigned int dest_regno ATTRIBUTE_UNUSED)
674 int best_cost = -1;
675 int rclass;
676 int regno;
677 enum reg_class best_class = NO_REGS;
678 enum reg_class dest_class ATTRIBUTE_UNUSED = REGNO_REG_CLASS (dest_regno);
679 unsigned int best_size = 0;
680 int cost;
682 for (rclass = 1; rclass < N_REG_CLASSES; rclass++)
684 int bad = 0;
685 int good = 0;
686 for (regno = 0; regno < FIRST_PSEUDO_REGISTER - n && ! bad; regno++)
687 if (TEST_HARD_REG_BIT (reg_class_contents[rclass], regno))
689 if (HARD_REGNO_MODE_OK (regno, inner))
691 good = 1;
692 if (TEST_HARD_REG_BIT (reg_class_contents[rclass], regno + n)
693 && ! HARD_REGNO_MODE_OK (regno + n, outer))
694 bad = 1;
698 if (bad || !good)
699 continue;
700 cost = register_move_cost (outer, (enum reg_class) rclass, dest_class);
702 if ((reg_class_size[rclass] > best_size
703 && (best_cost < 0 || best_cost >= cost))
704 || best_cost > cost)
706 best_class = (enum reg_class) rclass;
707 best_size = reg_class_size[rclass];
708 best_cost = register_move_cost (outer, (enum reg_class) rclass,
709 dest_class);
713 gcc_assert (best_size != 0);
715 return best_class;
718 /* We are trying to reload a subreg of something that is not a register.
719 Find the largest class which contains only registers valid in
720 mode MODE. OUTER is the mode of the subreg, DEST_CLASS the class in
721 which we would eventually like to obtain the object. */
723 static enum reg_class
724 find_valid_class_1 (machine_mode outer ATTRIBUTE_UNUSED,
725 machine_mode mode ATTRIBUTE_UNUSED,
726 enum reg_class dest_class ATTRIBUTE_UNUSED)
728 int best_cost = -1;
729 int rclass;
730 int regno;
731 enum reg_class best_class = NO_REGS;
732 unsigned int best_size = 0;
733 int cost;
735 for (rclass = 1; rclass < N_REG_CLASSES; rclass++)
737 int bad = 0;
738 for (regno = 0; regno < FIRST_PSEUDO_REGISTER && !bad; regno++)
740 if (in_hard_reg_set_p (reg_class_contents[rclass], mode, regno)
741 && !HARD_REGNO_MODE_OK (regno, mode))
742 bad = 1;
745 if (bad)
746 continue;
748 cost = register_move_cost (outer, (enum reg_class) rclass, dest_class);
750 if ((reg_class_size[rclass] > best_size
751 && (best_cost < 0 || best_cost >= cost))
752 || best_cost > cost)
754 best_class = (enum reg_class) rclass;
755 best_size = reg_class_size[rclass];
756 best_cost = register_move_cost (outer, (enum reg_class) rclass,
757 dest_class);
761 gcc_assert (best_size != 0);
763 #ifdef LIMIT_RELOAD_CLASS
764 best_class = LIMIT_RELOAD_CLASS (mode, best_class);
765 #endif
766 return best_class;
769 /* Return the number of a previously made reload that can be combined with
770 a new one, or n_reloads if none of the existing reloads can be used.
771 OUT, RCLASS, TYPE and OPNUM are the same arguments as passed to
772 push_reload, they determine the kind of the new reload that we try to
773 combine. P_IN points to the corresponding value of IN, which can be
774 modified by this function.
775 DONT_SHARE is nonzero if we can't share any input-only reload for IN. */
777 static int
778 find_reusable_reload (rtx *p_in, rtx out, enum reg_class rclass,
779 enum reload_type type, int opnum, int dont_share)
781 rtx in = *p_in;
782 int i;
783 /* We can't merge two reloads if the output of either one is
784 earlyclobbered. */
786 if (earlyclobber_operand_p (out))
787 return n_reloads;
789 /* We can use an existing reload if the class is right
790 and at least one of IN and OUT is a match
791 and the other is at worst neutral.
792 (A zero compared against anything is neutral.)
794 For targets with small register classes, don't use existing reloads
795 unless they are for the same thing since that can cause us to need
796 more reload registers than we otherwise would. */
798 for (i = 0; i < n_reloads; i++)
799 if ((reg_class_subset_p (rclass, rld[i].rclass)
800 || reg_class_subset_p (rld[i].rclass, rclass))
801 /* If the existing reload has a register, it must fit our class. */
802 && (rld[i].reg_rtx == 0
803 || TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
804 true_regnum (rld[i].reg_rtx)))
805 && ((in != 0 && MATCHES (rld[i].in, in) && ! dont_share
806 && (out == 0 || rld[i].out == 0 || MATCHES (rld[i].out, out)))
807 || (out != 0 && MATCHES (rld[i].out, out)
808 && (in == 0 || rld[i].in == 0 || MATCHES (rld[i].in, in))))
809 && (rld[i].out == 0 || ! earlyclobber_operand_p (rld[i].out))
810 && (small_register_class_p (rclass)
811 || targetm.small_register_classes_for_mode_p (VOIDmode))
812 && MERGABLE_RELOADS (type, rld[i].when_needed, opnum, rld[i].opnum))
813 return i;
815 /* Reloading a plain reg for input can match a reload to postincrement
816 that reg, since the postincrement's value is the right value.
817 Likewise, it can match a preincrement reload, since we regard
818 the preincrementation as happening before any ref in this insn
819 to that register. */
820 for (i = 0; i < n_reloads; i++)
821 if ((reg_class_subset_p (rclass, rld[i].rclass)
822 || reg_class_subset_p (rld[i].rclass, rclass))
823 /* If the existing reload has a register, it must fit our
824 class. */
825 && (rld[i].reg_rtx == 0
826 || TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
827 true_regnum (rld[i].reg_rtx)))
828 && out == 0 && rld[i].out == 0 && rld[i].in != 0
829 && ((REG_P (in)
830 && GET_RTX_CLASS (GET_CODE (rld[i].in)) == RTX_AUTOINC
831 && MATCHES (XEXP (rld[i].in, 0), in))
832 || (REG_P (rld[i].in)
833 && GET_RTX_CLASS (GET_CODE (in)) == RTX_AUTOINC
834 && MATCHES (XEXP (in, 0), rld[i].in)))
835 && (rld[i].out == 0 || ! earlyclobber_operand_p (rld[i].out))
836 && (small_register_class_p (rclass)
837 || targetm.small_register_classes_for_mode_p (VOIDmode))
838 && MERGABLE_RELOADS (type, rld[i].when_needed,
839 opnum, rld[i].opnum))
841 /* Make sure reload_in ultimately has the increment,
842 not the plain register. */
843 if (REG_P (in))
844 *p_in = rld[i].in;
845 return i;
847 return n_reloads;
850 /* Return true if X is a SUBREG that will need reloading of its SUBREG_REG
851 expression. MODE is the mode that X will be used in. OUTPUT is true if
852 the function is invoked for the output part of an enclosing reload. */
854 static bool
855 reload_inner_reg_of_subreg (rtx x, machine_mode mode, bool output)
857 rtx inner;
859 /* Only SUBREGs are problematical. */
860 if (GET_CODE (x) != SUBREG)
861 return false;
863 inner = SUBREG_REG (x);
865 /* If INNER is a constant or PLUS, then INNER will need reloading. */
866 if (CONSTANT_P (inner) || GET_CODE (inner) == PLUS)
867 return true;
869 /* If INNER is not a hard register, then INNER will not need reloading. */
870 if (!(REG_P (inner) && HARD_REGISTER_P (inner)))
871 return false;
873 /* If INNER is not ok for MODE, then INNER will need reloading. */
874 if (!HARD_REGNO_MODE_OK (subreg_regno (x), mode))
875 return true;
877 /* If this is for an output, and the outer part is a word or smaller,
878 INNER is larger than a word and the number of registers in INNER is
879 not the same as the number of words in INNER, then INNER will need
880 reloading (with an in-out reload). */
881 return (output
882 && GET_MODE_SIZE (mode) <= UNITS_PER_WORD
883 && GET_MODE_SIZE (GET_MODE (inner)) > UNITS_PER_WORD
884 && ((GET_MODE_SIZE (GET_MODE (inner)) / UNITS_PER_WORD)
885 != (int) hard_regno_nregs[REGNO (inner)][GET_MODE (inner)]));
888 /* Return nonzero if IN can be reloaded into REGNO with mode MODE without
889 requiring an extra reload register. The caller has already found that
890 IN contains some reference to REGNO, so check that we can produce the
891 new value in a single step. E.g. if we have
892 (set (reg r13) (plus (reg r13) (const int 1))), and there is an
893 instruction that adds one to a register, this should succeed.
894 However, if we have something like
895 (set (reg r13) (plus (reg r13) (const int 999))), and the constant 999
896 needs to be loaded into a register first, we need a separate reload
897 register.
898 Such PLUS reloads are generated by find_reload_address_part.
899 The out-of-range PLUS expressions are usually introduced in the instruction
900 patterns by register elimination and substituting pseudos without a home
901 by their function-invariant equivalences. */
902 static int
903 can_reload_into (rtx in, int regno, machine_mode mode)
905 rtx dst;
906 rtx_insn *test_insn;
907 int r = 0;
908 struct recog_data_d save_recog_data;
910 /* For matching constraints, we often get notional input reloads where
911 we want to use the original register as the reload register. I.e.
912 technically this is a non-optional input-output reload, but IN is
913 already a valid register, and has been chosen as the reload register.
914 Speed this up, since it trivially works. */
915 if (REG_P (in))
916 return 1;
918 /* To test MEMs properly, we'd have to take into account all the reloads
919 that are already scheduled, which can become quite complicated.
920 And since we've already handled address reloads for this MEM, it
921 should always succeed anyway. */
922 if (MEM_P (in))
923 return 1;
925 /* If we can make a simple SET insn that does the job, everything should
926 be fine. */
927 dst = gen_rtx_REG (mode, regno);
928 test_insn = make_insn_raw (gen_rtx_SET (dst, in));
929 save_recog_data = recog_data;
930 if (recog_memoized (test_insn) >= 0)
932 extract_insn (test_insn);
933 r = constrain_operands (1, get_enabled_alternatives (test_insn));
935 recog_data = save_recog_data;
936 return r;
939 /* Record one reload that needs to be performed.
940 IN is an rtx saying where the data are to be found before this instruction.
941 OUT says where they must be stored after the instruction.
942 (IN is zero for data not read, and OUT is zero for data not written.)
943 INLOC and OUTLOC point to the places in the instructions where
944 IN and OUT were found.
945 If IN and OUT are both nonzero, it means the same register must be used
946 to reload both IN and OUT.
948 RCLASS is a register class required for the reloaded data.
949 INMODE is the machine mode that the instruction requires
950 for the reg that replaces IN and OUTMODE is likewise for OUT.
952 If IN is zero, then OUT's location and mode should be passed as
953 INLOC and INMODE.
955 STRICT_LOW is the 1 if there is a containing STRICT_LOW_PART rtx.
957 OPTIONAL nonzero means this reload does not need to be performed:
958 it can be discarded if that is more convenient.
960 OPNUM and TYPE say what the purpose of this reload is.
962 The return value is the reload-number for this reload.
964 If both IN and OUT are nonzero, in some rare cases we might
965 want to make two separate reloads. (Actually we never do this now.)
966 Therefore, the reload-number for OUT is stored in
967 output_reloadnum when we return; the return value applies to IN.
968 Usually (presently always), when IN and OUT are nonzero,
969 the two reload-numbers are equal, but the caller should be careful to
970 distinguish them. */
973 push_reload (rtx in, rtx out, rtx *inloc, rtx *outloc,
974 enum reg_class rclass, machine_mode inmode,
975 machine_mode outmode, int strict_low, int optional,
976 int opnum, enum reload_type type)
978 int i;
979 int dont_share = 0;
980 int dont_remove_subreg = 0;
981 #ifdef LIMIT_RELOAD_CLASS
982 rtx *in_subreg_loc = 0, *out_subreg_loc = 0;
983 #endif
984 int secondary_in_reload = -1, secondary_out_reload = -1;
985 enum insn_code secondary_in_icode = CODE_FOR_nothing;
986 enum insn_code secondary_out_icode = CODE_FOR_nothing;
987 enum reg_class subreg_in_class ATTRIBUTE_UNUSED;
988 subreg_in_class = NO_REGS;
990 /* INMODE and/or OUTMODE could be VOIDmode if no mode
991 has been specified for the operand. In that case,
992 use the operand's mode as the mode to reload. */
993 if (inmode == VOIDmode && in != 0)
994 inmode = GET_MODE (in);
995 if (outmode == VOIDmode && out != 0)
996 outmode = GET_MODE (out);
998 /* If find_reloads and friends until now missed to replace a pseudo
999 with a constant of reg_equiv_constant something went wrong
1000 beforehand.
1001 Note that it can't simply be done here if we missed it earlier
1002 since the constant might need to be pushed into the literal pool
1003 and the resulting memref would probably need further
1004 reloading. */
1005 if (in != 0 && REG_P (in))
1007 int regno = REGNO (in);
1009 gcc_assert (regno < FIRST_PSEUDO_REGISTER
1010 || reg_renumber[regno] >= 0
1011 || reg_equiv_constant (regno) == NULL_RTX);
1014 /* reg_equiv_constant only contains constants which are obviously
1015 not appropriate as destination. So if we would need to replace
1016 the destination pseudo with a constant we are in real
1017 trouble. */
1018 if (out != 0 && REG_P (out))
1020 int regno = REGNO (out);
1022 gcc_assert (regno < FIRST_PSEUDO_REGISTER
1023 || reg_renumber[regno] >= 0
1024 || reg_equiv_constant (regno) == NULL_RTX);
1027 /* If we have a read-write operand with an address side-effect,
1028 change either IN or OUT so the side-effect happens only once. */
1029 if (in != 0 && out != 0 && MEM_P (in) && rtx_equal_p (in, out))
1030 switch (GET_CODE (XEXP (in, 0)))
1032 case POST_INC: case POST_DEC: case POST_MODIFY:
1033 in = replace_equiv_address_nv (in, XEXP (XEXP (in, 0), 0));
1034 break;
1036 case PRE_INC: case PRE_DEC: case PRE_MODIFY:
1037 out = replace_equiv_address_nv (out, XEXP (XEXP (out, 0), 0));
1038 break;
1040 default:
1041 break;
1044 /* If we are reloading a (SUBREG constant ...), really reload just the
1045 inside expression in its own mode. Similarly for (SUBREG (PLUS ...)).
1046 If we have (SUBREG:M1 (MEM:M2 ...) ...) (or an inner REG that is still
1047 a pseudo and hence will become a MEM) with M1 wider than M2 and the
1048 register is a pseudo, also reload the inside expression.
1049 For machines that extend byte loads, do this for any SUBREG of a pseudo
1050 where both M1 and M2 are a word or smaller, M1 is wider than M2, and
1051 M2 is an integral mode that gets extended when loaded.
1052 Similar issue for (SUBREG:M1 (REG:M2 ...) ...) for a hard register R
1053 where either M1 is not valid for R or M2 is wider than a word but we
1054 only need one register to store an M2-sized quantity in R.
1055 (However, if OUT is nonzero, we need to reload the reg *and*
1056 the subreg, so do nothing here, and let following statement handle it.)
1058 Note that the case of (SUBREG (CONST_INT...)...) is handled elsewhere;
1059 we can't handle it here because CONST_INT does not indicate a mode.
1061 Similarly, we must reload the inside expression if we have a
1062 STRICT_LOW_PART (presumably, in == out in this case).
1064 Also reload the inner expression if it does not require a secondary
1065 reload but the SUBREG does.
1067 Finally, reload the inner expression if it is a register that is in
1068 the class whose registers cannot be referenced in a different size
1069 and M1 is not the same size as M2. If subreg_lowpart_p is false, we
1070 cannot reload just the inside since we might end up with the wrong
1071 register class. But if it is inside a STRICT_LOW_PART, we have
1072 no choice, so we hope we do get the right register class there. */
1074 if (in != 0 && GET_CODE (in) == SUBREG
1075 && (subreg_lowpart_p (in) || strict_low)
1076 #ifdef CANNOT_CHANGE_MODE_CLASS
1077 && !CANNOT_CHANGE_MODE_CLASS (GET_MODE (SUBREG_REG (in)), inmode, rclass)
1078 #endif
1079 && contains_reg_of_mode[(int) rclass][(int) GET_MODE (SUBREG_REG (in))]
1080 && (CONSTANT_P (SUBREG_REG (in))
1081 || GET_CODE (SUBREG_REG (in)) == PLUS
1082 || strict_low
1083 || (((REG_P (SUBREG_REG (in))
1084 && REGNO (SUBREG_REG (in)) >= FIRST_PSEUDO_REGISTER)
1085 || MEM_P (SUBREG_REG (in)))
1086 && ((GET_MODE_PRECISION (inmode)
1087 > GET_MODE_PRECISION (GET_MODE (SUBREG_REG (in))))
1088 #ifdef LOAD_EXTEND_OP
1089 || (GET_MODE_SIZE (inmode) <= UNITS_PER_WORD
1090 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1091 <= UNITS_PER_WORD)
1092 && (GET_MODE_PRECISION (inmode)
1093 > GET_MODE_PRECISION (GET_MODE (SUBREG_REG (in))))
1094 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (in)))
1095 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (in))) != UNKNOWN)
1096 #endif
1097 #ifdef WORD_REGISTER_OPERATIONS
1098 || ((GET_MODE_PRECISION (inmode)
1099 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (in))))
1100 && ((GET_MODE_SIZE (inmode) - 1) / UNITS_PER_WORD ==
1101 ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))) - 1)
1102 / UNITS_PER_WORD)))
1103 #endif
1105 || (REG_P (SUBREG_REG (in))
1106 && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER
1107 /* The case where out is nonzero
1108 is handled differently in the following statement. */
1109 && (out == 0 || subreg_lowpart_p (in))
1110 && ((GET_MODE_SIZE (inmode) <= UNITS_PER_WORD
1111 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1112 > UNITS_PER_WORD)
1113 && ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1114 / UNITS_PER_WORD)
1115 != (int) hard_regno_nregs[REGNO (SUBREG_REG (in))]
1116 [GET_MODE (SUBREG_REG (in))]))
1117 || ! HARD_REGNO_MODE_OK (subreg_regno (in), inmode)))
1118 || (secondary_reload_class (1, rclass, inmode, in) != NO_REGS
1119 && (secondary_reload_class (1, rclass, GET_MODE (SUBREG_REG (in)),
1120 SUBREG_REG (in))
1121 == NO_REGS))
1122 #ifdef CANNOT_CHANGE_MODE_CLASS
1123 || (REG_P (SUBREG_REG (in))
1124 && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER
1125 && REG_CANNOT_CHANGE_MODE_P
1126 (REGNO (SUBREG_REG (in)), GET_MODE (SUBREG_REG (in)), inmode))
1127 #endif
1130 #ifdef LIMIT_RELOAD_CLASS
1131 in_subreg_loc = inloc;
1132 #endif
1133 inloc = &SUBREG_REG (in);
1134 in = *inloc;
1135 #if ! defined (LOAD_EXTEND_OP) && ! defined (WORD_REGISTER_OPERATIONS)
1136 if (MEM_P (in))
1137 /* This is supposed to happen only for paradoxical subregs made by
1138 combine.c. (SUBREG (MEM)) isn't supposed to occur other ways. */
1139 gcc_assert (GET_MODE_SIZE (GET_MODE (in)) <= GET_MODE_SIZE (inmode));
1140 #endif
1141 inmode = GET_MODE (in);
1144 /* Similar issue for (SUBREG:M1 (REG:M2 ...) ...) for a hard register R
1145 where M1 is not valid for R if it was not handled by the code above.
1147 Similar issue for (SUBREG constant ...) if it was not handled by the
1148 code above. This can happen if SUBREG_BYTE != 0.
1150 However, we must reload the inner reg *as well as* the subreg in
1151 that case. */
1153 if (in != 0 && reload_inner_reg_of_subreg (in, inmode, false))
1155 if (REG_P (SUBREG_REG (in)))
1156 subreg_in_class
1157 = find_valid_class (inmode, GET_MODE (SUBREG_REG (in)),
1158 subreg_regno_offset (REGNO (SUBREG_REG (in)),
1159 GET_MODE (SUBREG_REG (in)),
1160 SUBREG_BYTE (in),
1161 GET_MODE (in)),
1162 REGNO (SUBREG_REG (in)));
1163 else if (GET_CODE (SUBREG_REG (in)) == SYMBOL_REF)
1164 subreg_in_class = find_valid_class_1 (inmode,
1165 GET_MODE (SUBREG_REG (in)),
1166 rclass);
1168 /* This relies on the fact that emit_reload_insns outputs the
1169 instructions for input reloads of type RELOAD_OTHER in the same
1170 order as the reloads. Thus if the outer reload is also of type
1171 RELOAD_OTHER, we are guaranteed that this inner reload will be
1172 output before the outer reload. */
1173 push_reload (SUBREG_REG (in), NULL_RTX, &SUBREG_REG (in), (rtx *) 0,
1174 subreg_in_class, VOIDmode, VOIDmode, 0, 0, opnum, type);
1175 dont_remove_subreg = 1;
1178 /* Similarly for paradoxical and problematical SUBREGs on the output.
1179 Note that there is no reason we need worry about the previous value
1180 of SUBREG_REG (out); even if wider than out, storing in a subreg is
1181 entitled to clobber it all (except in the case of a word mode subreg
1182 or of a STRICT_LOW_PART, in that latter case the constraint should
1183 label it input-output.) */
1184 if (out != 0 && GET_CODE (out) == SUBREG
1185 && (subreg_lowpart_p (out) || strict_low)
1186 #ifdef CANNOT_CHANGE_MODE_CLASS
1187 && !CANNOT_CHANGE_MODE_CLASS (GET_MODE (SUBREG_REG (out)), outmode, rclass)
1188 #endif
1189 && contains_reg_of_mode[(int) rclass][(int) GET_MODE (SUBREG_REG (out))]
1190 && (CONSTANT_P (SUBREG_REG (out))
1191 || strict_low
1192 || (((REG_P (SUBREG_REG (out))
1193 && REGNO (SUBREG_REG (out)) >= FIRST_PSEUDO_REGISTER)
1194 || MEM_P (SUBREG_REG (out)))
1195 && ((GET_MODE_PRECISION (outmode)
1196 > GET_MODE_PRECISION (GET_MODE (SUBREG_REG (out))))
1197 #ifdef WORD_REGISTER_OPERATIONS
1198 || ((GET_MODE_PRECISION (outmode)
1199 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (out))))
1200 && ((GET_MODE_SIZE (outmode) - 1) / UNITS_PER_WORD ==
1201 ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))) - 1)
1202 / UNITS_PER_WORD)))
1203 #endif
1205 || (REG_P (SUBREG_REG (out))
1206 && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER
1207 /* The case of a word mode subreg
1208 is handled differently in the following statement. */
1209 && ! (GET_MODE_SIZE (outmode) <= UNITS_PER_WORD
1210 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (out)))
1211 > UNITS_PER_WORD))
1212 && ! HARD_REGNO_MODE_OK (subreg_regno (out), outmode))
1213 || (secondary_reload_class (0, rclass, outmode, out) != NO_REGS
1214 && (secondary_reload_class (0, rclass, GET_MODE (SUBREG_REG (out)),
1215 SUBREG_REG (out))
1216 == NO_REGS))
1217 #ifdef CANNOT_CHANGE_MODE_CLASS
1218 || (REG_P (SUBREG_REG (out))
1219 && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER
1220 && REG_CANNOT_CHANGE_MODE_P (REGNO (SUBREG_REG (out)),
1221 GET_MODE (SUBREG_REG (out)),
1222 outmode))
1223 #endif
1226 #ifdef LIMIT_RELOAD_CLASS
1227 out_subreg_loc = outloc;
1228 #endif
1229 outloc = &SUBREG_REG (out);
1230 out = *outloc;
1231 #if ! defined (LOAD_EXTEND_OP) && ! defined (WORD_REGISTER_OPERATIONS)
1232 gcc_assert (!MEM_P (out)
1233 || GET_MODE_SIZE (GET_MODE (out))
1234 <= GET_MODE_SIZE (outmode));
1235 #endif
1236 outmode = GET_MODE (out);
1239 /* Similar issue for (SUBREG:M1 (REG:M2 ...) ...) for a hard register R
1240 where either M1 is not valid for R or M2 is wider than a word but we
1241 only need one register to store an M2-sized quantity in R.
1243 However, we must reload the inner reg *as well as* the subreg in
1244 that case and the inner reg is an in-out reload. */
1246 if (out != 0 && reload_inner_reg_of_subreg (out, outmode, true))
1248 enum reg_class in_out_class
1249 = find_valid_class (outmode, GET_MODE (SUBREG_REG (out)),
1250 subreg_regno_offset (REGNO (SUBREG_REG (out)),
1251 GET_MODE (SUBREG_REG (out)),
1252 SUBREG_BYTE (out),
1253 GET_MODE (out)),
1254 REGNO (SUBREG_REG (out)));
1256 /* This relies on the fact that emit_reload_insns outputs the
1257 instructions for output reloads of type RELOAD_OTHER in reverse
1258 order of the reloads. Thus if the outer reload is also of type
1259 RELOAD_OTHER, we are guaranteed that this inner reload will be
1260 output after the outer reload. */
1261 push_reload (SUBREG_REG (out), SUBREG_REG (out), &SUBREG_REG (out),
1262 &SUBREG_REG (out), in_out_class, VOIDmode, VOIDmode,
1263 0, 0, opnum, RELOAD_OTHER);
1264 dont_remove_subreg = 1;
1267 /* If IN appears in OUT, we can't share any input-only reload for IN. */
1268 if (in != 0 && out != 0 && MEM_P (out)
1269 && (REG_P (in) || MEM_P (in) || GET_CODE (in) == PLUS)
1270 && reg_overlap_mentioned_for_reload_p (in, XEXP (out, 0)))
1271 dont_share = 1;
1273 /* If IN is a SUBREG of a hard register, make a new REG. This
1274 simplifies some of the cases below. */
1276 if (in != 0 && GET_CODE (in) == SUBREG && REG_P (SUBREG_REG (in))
1277 && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER
1278 && ! dont_remove_subreg)
1279 in = gen_rtx_REG (GET_MODE (in), subreg_regno (in));
1281 /* Similarly for OUT. */
1282 if (out != 0 && GET_CODE (out) == SUBREG
1283 && REG_P (SUBREG_REG (out))
1284 && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER
1285 && ! dont_remove_subreg)
1286 out = gen_rtx_REG (GET_MODE (out), subreg_regno (out));
1288 /* Narrow down the class of register wanted if that is
1289 desirable on this machine for efficiency. */
1291 reg_class_t preferred_class = rclass;
1293 if (in != 0)
1294 preferred_class = targetm.preferred_reload_class (in, rclass);
1296 /* Output reloads may need analogous treatment, different in detail. */
1297 if (out != 0)
1298 preferred_class
1299 = targetm.preferred_output_reload_class (out, preferred_class);
1301 /* Discard what the target said if we cannot do it. */
1302 if (preferred_class != NO_REGS
1303 || (optional && type == RELOAD_FOR_OUTPUT))
1304 rclass = (enum reg_class) preferred_class;
1307 /* Make sure we use a class that can handle the actual pseudo
1308 inside any subreg. For example, on the 386, QImode regs
1309 can appear within SImode subregs. Although GENERAL_REGS
1310 can handle SImode, QImode needs a smaller class. */
1311 #ifdef LIMIT_RELOAD_CLASS
1312 if (in_subreg_loc)
1313 rclass = LIMIT_RELOAD_CLASS (inmode, rclass);
1314 else if (in != 0 && GET_CODE (in) == SUBREG)
1315 rclass = LIMIT_RELOAD_CLASS (GET_MODE (SUBREG_REG (in)), rclass);
1317 if (out_subreg_loc)
1318 rclass = LIMIT_RELOAD_CLASS (outmode, rclass);
1319 if (out != 0 && GET_CODE (out) == SUBREG)
1320 rclass = LIMIT_RELOAD_CLASS (GET_MODE (SUBREG_REG (out)), rclass);
1321 #endif
1323 /* Verify that this class is at least possible for the mode that
1324 is specified. */
1325 if (this_insn_is_asm)
1327 machine_mode mode;
1328 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
1329 mode = inmode;
1330 else
1331 mode = outmode;
1332 if (mode == VOIDmode)
1334 error_for_asm (this_insn, "cannot reload integer constant "
1335 "operand in %<asm%>");
1336 mode = word_mode;
1337 if (in != 0)
1338 inmode = word_mode;
1339 if (out != 0)
1340 outmode = word_mode;
1342 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1343 if (HARD_REGNO_MODE_OK (i, mode)
1344 && in_hard_reg_set_p (reg_class_contents[(int) rclass], mode, i))
1345 break;
1346 if (i == FIRST_PSEUDO_REGISTER)
1348 error_for_asm (this_insn, "impossible register constraint "
1349 "in %<asm%>");
1350 /* Avoid further trouble with this insn. */
1351 PATTERN (this_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
1352 /* We used to continue here setting class to ALL_REGS, but it triggers
1353 sanity check on i386 for:
1354 void foo(long double d)
1356 asm("" :: "a" (d));
1358 Returning zero here ought to be safe as we take care in
1359 find_reloads to not process the reloads when instruction was
1360 replaced by USE. */
1362 return 0;
1366 /* Optional output reloads are always OK even if we have no register class,
1367 since the function of these reloads is only to have spill_reg_store etc.
1368 set, so that the storing insn can be deleted later. */
1369 gcc_assert (rclass != NO_REGS
1370 || (optional != 0 && type == RELOAD_FOR_OUTPUT));
1372 i = find_reusable_reload (&in, out, rclass, type, opnum, dont_share);
1374 if (i == n_reloads)
1376 /* See if we need a secondary reload register to move between CLASS
1377 and IN or CLASS and OUT. Get the icode and push any required reloads
1378 needed for each of them if so. */
1380 if (in != 0)
1381 secondary_in_reload
1382 = push_secondary_reload (1, in, opnum, optional, rclass, inmode, type,
1383 &secondary_in_icode, NULL);
1384 if (out != 0 && GET_CODE (out) != SCRATCH)
1385 secondary_out_reload
1386 = push_secondary_reload (0, out, opnum, optional, rclass, outmode,
1387 type, &secondary_out_icode, NULL);
1389 /* We found no existing reload suitable for re-use.
1390 So add an additional reload. */
1392 #ifdef SECONDARY_MEMORY_NEEDED
1393 if (subreg_in_class == NO_REGS
1394 && in != 0
1395 && (REG_P (in)
1396 || (GET_CODE (in) == SUBREG && REG_P (SUBREG_REG (in))))
1397 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER)
1398 subreg_in_class = REGNO_REG_CLASS (reg_or_subregno (in));
1399 /* If a memory location is needed for the copy, make one. */
1400 if (subreg_in_class != NO_REGS
1401 && SECONDARY_MEMORY_NEEDED (subreg_in_class, rclass, inmode))
1402 get_secondary_mem (in, inmode, opnum, type);
1403 #endif
1405 i = n_reloads;
1406 rld[i].in = in;
1407 rld[i].out = out;
1408 rld[i].rclass = rclass;
1409 rld[i].inmode = inmode;
1410 rld[i].outmode = outmode;
1411 rld[i].reg_rtx = 0;
1412 rld[i].optional = optional;
1413 rld[i].inc = 0;
1414 rld[i].nocombine = 0;
1415 rld[i].in_reg = inloc ? *inloc : 0;
1416 rld[i].out_reg = outloc ? *outloc : 0;
1417 rld[i].opnum = opnum;
1418 rld[i].when_needed = type;
1419 rld[i].secondary_in_reload = secondary_in_reload;
1420 rld[i].secondary_out_reload = secondary_out_reload;
1421 rld[i].secondary_in_icode = secondary_in_icode;
1422 rld[i].secondary_out_icode = secondary_out_icode;
1423 rld[i].secondary_p = 0;
1425 n_reloads++;
1427 #ifdef SECONDARY_MEMORY_NEEDED
1428 if (out != 0
1429 && (REG_P (out)
1430 || (GET_CODE (out) == SUBREG && REG_P (SUBREG_REG (out))))
1431 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
1432 && SECONDARY_MEMORY_NEEDED (rclass,
1433 REGNO_REG_CLASS (reg_or_subregno (out)),
1434 outmode))
1435 get_secondary_mem (out, outmode, opnum, type);
1436 #endif
1438 else
1440 /* We are reusing an existing reload,
1441 but we may have additional information for it.
1442 For example, we may now have both IN and OUT
1443 while the old one may have just one of them. */
1445 /* The modes can be different. If they are, we want to reload in
1446 the larger mode, so that the value is valid for both modes. */
1447 if (inmode != VOIDmode
1448 && GET_MODE_SIZE (inmode) > GET_MODE_SIZE (rld[i].inmode))
1449 rld[i].inmode = inmode;
1450 if (outmode != VOIDmode
1451 && GET_MODE_SIZE (outmode) > GET_MODE_SIZE (rld[i].outmode))
1452 rld[i].outmode = outmode;
1453 if (in != 0)
1455 rtx in_reg = inloc ? *inloc : 0;
1456 /* If we merge reloads for two distinct rtl expressions that
1457 are identical in content, there might be duplicate address
1458 reloads. Remove the extra set now, so that if we later find
1459 that we can inherit this reload, we can get rid of the
1460 address reloads altogether.
1462 Do not do this if both reloads are optional since the result
1463 would be an optional reload which could potentially leave
1464 unresolved address replacements.
1466 It is not sufficient to call transfer_replacements since
1467 choose_reload_regs will remove the replacements for address
1468 reloads of inherited reloads which results in the same
1469 problem. */
1470 if (rld[i].in != in && rtx_equal_p (in, rld[i].in)
1471 && ! (rld[i].optional && optional))
1473 /* We must keep the address reload with the lower operand
1474 number alive. */
1475 if (opnum > rld[i].opnum)
1477 remove_address_replacements (in);
1478 in = rld[i].in;
1479 in_reg = rld[i].in_reg;
1481 else
1482 remove_address_replacements (rld[i].in);
1484 /* When emitting reloads we don't necessarily look at the in-
1485 and outmode, but also directly at the operands (in and out).
1486 So we can't simply overwrite them with whatever we have found
1487 for this (to-be-merged) reload, we have to "merge" that too.
1488 Reusing another reload already verified that we deal with the
1489 same operands, just possibly in different modes. So we
1490 overwrite the operands only when the new mode is larger.
1491 See also PR33613. */
1492 if (!rld[i].in
1493 || GET_MODE_SIZE (GET_MODE (in))
1494 > GET_MODE_SIZE (GET_MODE (rld[i].in)))
1495 rld[i].in = in;
1496 if (!rld[i].in_reg
1497 || (in_reg
1498 && GET_MODE_SIZE (GET_MODE (in_reg))
1499 > GET_MODE_SIZE (GET_MODE (rld[i].in_reg))))
1500 rld[i].in_reg = in_reg;
1502 if (out != 0)
1504 if (!rld[i].out
1505 || (out
1506 && GET_MODE_SIZE (GET_MODE (out))
1507 > GET_MODE_SIZE (GET_MODE (rld[i].out))))
1508 rld[i].out = out;
1509 if (outloc
1510 && (!rld[i].out_reg
1511 || GET_MODE_SIZE (GET_MODE (*outloc))
1512 > GET_MODE_SIZE (GET_MODE (rld[i].out_reg))))
1513 rld[i].out_reg = *outloc;
1515 if (reg_class_subset_p (rclass, rld[i].rclass))
1516 rld[i].rclass = rclass;
1517 rld[i].optional &= optional;
1518 if (MERGE_TO_OTHER (type, rld[i].when_needed,
1519 opnum, rld[i].opnum))
1520 rld[i].when_needed = RELOAD_OTHER;
1521 rld[i].opnum = MIN (rld[i].opnum, opnum);
1524 /* If the ostensible rtx being reloaded differs from the rtx found
1525 in the location to substitute, this reload is not safe to combine
1526 because we cannot reliably tell whether it appears in the insn. */
1528 if (in != 0 && in != *inloc)
1529 rld[i].nocombine = 1;
1531 #if 0
1532 /* This was replaced by changes in find_reloads_address_1 and the new
1533 function inc_for_reload, which go with a new meaning of reload_inc. */
1535 /* If this is an IN/OUT reload in an insn that sets the CC,
1536 it must be for an autoincrement. It doesn't work to store
1537 the incremented value after the insn because that would clobber the CC.
1538 So we must do the increment of the value reloaded from,
1539 increment it, store it back, then decrement again. */
1540 if (out != 0 && sets_cc0_p (PATTERN (this_insn)))
1542 out = 0;
1543 rld[i].out = 0;
1544 rld[i].inc = find_inc_amount (PATTERN (this_insn), in);
1545 /* If we did not find a nonzero amount-to-increment-by,
1546 that contradicts the belief that IN is being incremented
1547 in an address in this insn. */
1548 gcc_assert (rld[i].inc != 0);
1550 #endif
1552 /* If we will replace IN and OUT with the reload-reg,
1553 record where they are located so that substitution need
1554 not do a tree walk. */
1556 if (replace_reloads)
1558 if (inloc != 0)
1560 struct replacement *r = &replacements[n_replacements++];
1561 r->what = i;
1562 r->where = inloc;
1563 r->mode = inmode;
1565 if (outloc != 0 && outloc != inloc)
1567 struct replacement *r = &replacements[n_replacements++];
1568 r->what = i;
1569 r->where = outloc;
1570 r->mode = outmode;
1574 /* If this reload is just being introduced and it has both
1575 an incoming quantity and an outgoing quantity that are
1576 supposed to be made to match, see if either one of the two
1577 can serve as the place to reload into.
1579 If one of them is acceptable, set rld[i].reg_rtx
1580 to that one. */
1582 if (in != 0 && out != 0 && in != out && rld[i].reg_rtx == 0)
1584 rld[i].reg_rtx = find_dummy_reload (in, out, inloc, outloc,
1585 inmode, outmode,
1586 rld[i].rclass, i,
1587 earlyclobber_operand_p (out));
1589 /* If the outgoing register already contains the same value
1590 as the incoming one, we can dispense with loading it.
1591 The easiest way to tell the caller that is to give a phony
1592 value for the incoming operand (same as outgoing one). */
1593 if (rld[i].reg_rtx == out
1594 && (REG_P (in) || CONSTANT_P (in))
1595 && 0 != find_equiv_reg (in, this_insn, NO_REGS, REGNO (out),
1596 static_reload_reg_p, i, inmode))
1597 rld[i].in = out;
1600 /* If this is an input reload and the operand contains a register that
1601 dies in this insn and is used nowhere else, see if it is the right class
1602 to be used for this reload. Use it if so. (This occurs most commonly
1603 in the case of paradoxical SUBREGs and in-out reloads). We cannot do
1604 this if it is also an output reload that mentions the register unless
1605 the output is a SUBREG that clobbers an entire register.
1607 Note that the operand might be one of the spill regs, if it is a
1608 pseudo reg and we are in a block where spilling has not taken place.
1609 But if there is no spilling in this block, that is OK.
1610 An explicitly used hard reg cannot be a spill reg. */
1612 if (rld[i].reg_rtx == 0 && in != 0 && hard_regs_live_known)
1614 rtx note;
1615 int regno;
1616 machine_mode rel_mode = inmode;
1618 if (out && GET_MODE_SIZE (outmode) > GET_MODE_SIZE (inmode))
1619 rel_mode = outmode;
1621 for (note = REG_NOTES (this_insn); note; note = XEXP (note, 1))
1622 if (REG_NOTE_KIND (note) == REG_DEAD
1623 && REG_P (XEXP (note, 0))
1624 && (regno = REGNO (XEXP (note, 0))) < FIRST_PSEUDO_REGISTER
1625 && reg_mentioned_p (XEXP (note, 0), in)
1626 /* Check that a former pseudo is valid; see find_dummy_reload. */
1627 && (ORIGINAL_REGNO (XEXP (note, 0)) < FIRST_PSEUDO_REGISTER
1628 || (! bitmap_bit_p (DF_LR_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
1629 ORIGINAL_REGNO (XEXP (note, 0)))
1630 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] == 1))
1631 && ! refers_to_regno_for_reload_p (regno,
1632 end_hard_regno (rel_mode,
1633 regno),
1634 PATTERN (this_insn), inloc)
1635 && ! find_reg_fusage (this_insn, USE, XEXP (note, 0))
1636 /* If this is also an output reload, IN cannot be used as
1637 the reload register if it is set in this insn unless IN
1638 is also OUT. */
1639 && (out == 0 || in == out
1640 || ! hard_reg_set_here_p (regno,
1641 end_hard_regno (rel_mode, regno),
1642 PATTERN (this_insn)))
1643 /* ??? Why is this code so different from the previous?
1644 Is there any simple coherent way to describe the two together?
1645 What's going on here. */
1646 && (in != out
1647 || (GET_CODE (in) == SUBREG
1648 && (((GET_MODE_SIZE (GET_MODE (in)) + (UNITS_PER_WORD - 1))
1649 / UNITS_PER_WORD)
1650 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1651 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
1652 /* Make sure the operand fits in the reg that dies. */
1653 && (GET_MODE_SIZE (rel_mode)
1654 <= GET_MODE_SIZE (GET_MODE (XEXP (note, 0))))
1655 && HARD_REGNO_MODE_OK (regno, inmode)
1656 && HARD_REGNO_MODE_OK (regno, outmode))
1658 unsigned int offs;
1659 unsigned int nregs = MAX (hard_regno_nregs[regno][inmode],
1660 hard_regno_nregs[regno][outmode]);
1662 for (offs = 0; offs < nregs; offs++)
1663 if (fixed_regs[regno + offs]
1664 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
1665 regno + offs))
1666 break;
1668 if (offs == nregs
1669 && (! (refers_to_regno_for_reload_p
1670 (regno, end_hard_regno (inmode, regno), in, (rtx *) 0))
1671 || can_reload_into (in, regno, inmode)))
1673 rld[i].reg_rtx = gen_rtx_REG (rel_mode, regno);
1674 break;
1679 if (out)
1680 output_reloadnum = i;
1682 return i;
1685 /* Record an additional place we must replace a value
1686 for which we have already recorded a reload.
1687 RELOADNUM is the value returned by push_reload
1688 when the reload was recorded.
1689 This is used in insn patterns that use match_dup. */
1691 static void
1692 push_replacement (rtx *loc, int reloadnum, machine_mode mode)
1694 if (replace_reloads)
1696 struct replacement *r = &replacements[n_replacements++];
1697 r->what = reloadnum;
1698 r->where = loc;
1699 r->mode = mode;
1703 /* Duplicate any replacement we have recorded to apply at
1704 location ORIG_LOC to also be performed at DUP_LOC.
1705 This is used in insn patterns that use match_dup. */
1707 static void
1708 dup_replacements (rtx *dup_loc, rtx *orig_loc)
1710 int i, n = n_replacements;
1712 for (i = 0; i < n; i++)
1714 struct replacement *r = &replacements[i];
1715 if (r->where == orig_loc)
1716 push_replacement (dup_loc, r->what, r->mode);
1720 /* Transfer all replacements that used to be in reload FROM to be in
1721 reload TO. */
1723 void
1724 transfer_replacements (int to, int from)
1726 int i;
1728 for (i = 0; i < n_replacements; i++)
1729 if (replacements[i].what == from)
1730 replacements[i].what = to;
1733 /* IN_RTX is the value loaded by a reload that we now decided to inherit,
1734 or a subpart of it. If we have any replacements registered for IN_RTX,
1735 cancel the reloads that were supposed to load them.
1736 Return nonzero if we canceled any reloads. */
1738 remove_address_replacements (rtx in_rtx)
1740 int i, j;
1741 char reload_flags[MAX_RELOADS];
1742 int something_changed = 0;
1744 memset (reload_flags, 0, sizeof reload_flags);
1745 for (i = 0, j = 0; i < n_replacements; i++)
1747 if (loc_mentioned_in_p (replacements[i].where, in_rtx))
1748 reload_flags[replacements[i].what] |= 1;
1749 else
1751 replacements[j++] = replacements[i];
1752 reload_flags[replacements[i].what] |= 2;
1755 /* Note that the following store must be done before the recursive calls. */
1756 n_replacements = j;
1758 for (i = n_reloads - 1; i >= 0; i--)
1760 if (reload_flags[i] == 1)
1762 deallocate_reload_reg (i);
1763 remove_address_replacements (rld[i].in);
1764 rld[i].in = 0;
1765 something_changed = 1;
1768 return something_changed;
1771 /* If there is only one output reload, and it is not for an earlyclobber
1772 operand, try to combine it with a (logically unrelated) input reload
1773 to reduce the number of reload registers needed.
1775 This is safe if the input reload does not appear in
1776 the value being output-reloaded, because this implies
1777 it is not needed any more once the original insn completes.
1779 If that doesn't work, see we can use any of the registers that
1780 die in this insn as a reload register. We can if it is of the right
1781 class and does not appear in the value being output-reloaded. */
1783 static void
1784 combine_reloads (void)
1786 int i, regno;
1787 int output_reload = -1;
1788 int secondary_out = -1;
1789 rtx note;
1791 /* Find the output reload; return unless there is exactly one
1792 and that one is mandatory. */
1794 for (i = 0; i < n_reloads; i++)
1795 if (rld[i].out != 0)
1797 if (output_reload >= 0)
1798 return;
1799 output_reload = i;
1802 if (output_reload < 0 || rld[output_reload].optional)
1803 return;
1805 /* An input-output reload isn't combinable. */
1807 if (rld[output_reload].in != 0)
1808 return;
1810 /* If this reload is for an earlyclobber operand, we can't do anything. */
1811 if (earlyclobber_operand_p (rld[output_reload].out))
1812 return;
1814 /* If there is a reload for part of the address of this operand, we would
1815 need to change it to RELOAD_FOR_OTHER_ADDRESS. But that would extend
1816 its life to the point where doing this combine would not lower the
1817 number of spill registers needed. */
1818 for (i = 0; i < n_reloads; i++)
1819 if ((rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
1820 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
1821 && rld[i].opnum == rld[output_reload].opnum)
1822 return;
1824 /* Check each input reload; can we combine it? */
1826 for (i = 0; i < n_reloads; i++)
1827 if (rld[i].in && ! rld[i].optional && ! rld[i].nocombine
1828 /* Life span of this reload must not extend past main insn. */
1829 && rld[i].when_needed != RELOAD_FOR_OUTPUT_ADDRESS
1830 && rld[i].when_needed != RELOAD_FOR_OUTADDR_ADDRESS
1831 && rld[i].when_needed != RELOAD_OTHER
1832 && (ira_reg_class_max_nregs [(int)rld[i].rclass][(int) rld[i].inmode]
1833 == ira_reg_class_max_nregs [(int) rld[output_reload].rclass]
1834 [(int) rld[output_reload].outmode])
1835 && rld[i].inc == 0
1836 && rld[i].reg_rtx == 0
1837 #ifdef SECONDARY_MEMORY_NEEDED
1838 /* Don't combine two reloads with different secondary
1839 memory locations. */
1840 && (secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[i].opnum] == 0
1841 || secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum] == 0
1842 || rtx_equal_p (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 #endif
1845 && (targetm.small_register_classes_for_mode_p (VOIDmode)
1846 ? (rld[i].rclass == rld[output_reload].rclass)
1847 : (reg_class_subset_p (rld[i].rclass,
1848 rld[output_reload].rclass)
1849 || reg_class_subset_p (rld[output_reload].rclass,
1850 rld[i].rclass)))
1851 && (MATCHES (rld[i].in, rld[output_reload].out)
1852 /* Args reversed because the first arg seems to be
1853 the one that we imagine being modified
1854 while the second is the one that might be affected. */
1855 || (! reg_overlap_mentioned_for_reload_p (rld[output_reload].out,
1856 rld[i].in)
1857 /* However, if the input is a register that appears inside
1858 the output, then we also can't share.
1859 Imagine (set (mem (reg 69)) (plus (reg 69) ...)).
1860 If the same reload reg is used for both reg 69 and the
1861 result to be stored in memory, then that result
1862 will clobber the address of the memory ref. */
1863 && ! (REG_P (rld[i].in)
1864 && reg_overlap_mentioned_for_reload_p (rld[i].in,
1865 rld[output_reload].out))))
1866 && ! reload_inner_reg_of_subreg (rld[i].in, rld[i].inmode,
1867 rld[i].when_needed != RELOAD_FOR_INPUT)
1868 && (reg_class_size[(int) rld[i].rclass]
1869 || targetm.small_register_classes_for_mode_p (VOIDmode))
1870 /* We will allow making things slightly worse by combining an
1871 input and an output, but no worse than that. */
1872 && (rld[i].when_needed == RELOAD_FOR_INPUT
1873 || rld[i].when_needed == RELOAD_FOR_OUTPUT))
1875 int j;
1877 /* We have found a reload to combine with! */
1878 rld[i].out = rld[output_reload].out;
1879 rld[i].out_reg = rld[output_reload].out_reg;
1880 rld[i].outmode = rld[output_reload].outmode;
1881 /* Mark the old output reload as inoperative. */
1882 rld[output_reload].out = 0;
1883 /* The combined reload is needed for the entire insn. */
1884 rld[i].when_needed = RELOAD_OTHER;
1885 /* If the output reload had a secondary reload, copy it. */
1886 if (rld[output_reload].secondary_out_reload != -1)
1888 rld[i].secondary_out_reload
1889 = rld[output_reload].secondary_out_reload;
1890 rld[i].secondary_out_icode
1891 = rld[output_reload].secondary_out_icode;
1894 #ifdef SECONDARY_MEMORY_NEEDED
1895 /* Copy any secondary MEM. */
1896 if (secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum] != 0)
1897 secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[i].opnum]
1898 = secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum];
1899 #endif
1900 /* If required, minimize the register class. */
1901 if (reg_class_subset_p (rld[output_reload].rclass,
1902 rld[i].rclass))
1903 rld[i].rclass = rld[output_reload].rclass;
1905 /* Transfer all replacements from the old reload to the combined. */
1906 for (j = 0; j < n_replacements; j++)
1907 if (replacements[j].what == output_reload)
1908 replacements[j].what = i;
1910 return;
1913 /* If this insn has only one operand that is modified or written (assumed
1914 to be the first), it must be the one corresponding to this reload. It
1915 is safe to use anything that dies in this insn for that output provided
1916 that it does not occur in the output (we already know it isn't an
1917 earlyclobber. If this is an asm insn, give up. */
1919 if (INSN_CODE (this_insn) == -1)
1920 return;
1922 for (i = 1; i < insn_data[INSN_CODE (this_insn)].n_operands; i++)
1923 if (insn_data[INSN_CODE (this_insn)].operand[i].constraint[0] == '='
1924 || insn_data[INSN_CODE (this_insn)].operand[i].constraint[0] == '+')
1925 return;
1927 /* See if some hard register that dies in this insn and is not used in
1928 the output is the right class. Only works if the register we pick
1929 up can fully hold our output reload. */
1930 for (note = REG_NOTES (this_insn); note; note = XEXP (note, 1))
1931 if (REG_NOTE_KIND (note) == REG_DEAD
1932 && REG_P (XEXP (note, 0))
1933 && !reg_overlap_mentioned_for_reload_p (XEXP (note, 0),
1934 rld[output_reload].out)
1935 && (regno = REGNO (XEXP (note, 0))) < FIRST_PSEUDO_REGISTER
1936 && HARD_REGNO_MODE_OK (regno, rld[output_reload].outmode)
1937 && TEST_HARD_REG_BIT (reg_class_contents[(int) rld[output_reload].rclass],
1938 regno)
1939 && (hard_regno_nregs[regno][rld[output_reload].outmode]
1940 <= hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))])
1941 /* Ensure that a secondary or tertiary reload for this output
1942 won't want this register. */
1943 && ((secondary_out = rld[output_reload].secondary_out_reload) == -1
1944 || (!(TEST_HARD_REG_BIT
1945 (reg_class_contents[(int) rld[secondary_out].rclass], regno))
1946 && ((secondary_out = rld[secondary_out].secondary_out_reload) == -1
1947 || !(TEST_HARD_REG_BIT
1948 (reg_class_contents[(int) rld[secondary_out].rclass],
1949 regno)))))
1950 && !fixed_regs[regno]
1951 /* Check that a former pseudo is valid; see find_dummy_reload. */
1952 && (ORIGINAL_REGNO (XEXP (note, 0)) < FIRST_PSEUDO_REGISTER
1953 || (!bitmap_bit_p (DF_LR_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
1954 ORIGINAL_REGNO (XEXP (note, 0)))
1955 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] == 1)))
1957 rld[output_reload].reg_rtx
1958 = gen_rtx_REG (rld[output_reload].outmode, regno);
1959 return;
1963 /* Try to find a reload register for an in-out reload (expressions IN and OUT).
1964 See if one of IN and OUT is a register that may be used;
1965 this is desirable since a spill-register won't be needed.
1966 If so, return the register rtx that proves acceptable.
1968 INLOC and OUTLOC are locations where IN and OUT appear in the insn.
1969 RCLASS is the register class required for the reload.
1971 If FOR_REAL is >= 0, it is the number of the reload,
1972 and in some cases when it can be discovered that OUT doesn't need
1973 to be computed, clear out rld[FOR_REAL].out.
1975 If FOR_REAL is -1, this should not be done, because this call
1976 is just to see if a register can be found, not to find and install it.
1978 EARLYCLOBBER is nonzero if OUT is an earlyclobber operand. This
1979 puts an additional constraint on being able to use IN for OUT since
1980 IN must not appear elsewhere in the insn (it is assumed that IN itself
1981 is safe from the earlyclobber). */
1983 static rtx
1984 find_dummy_reload (rtx real_in, rtx real_out, rtx *inloc, rtx *outloc,
1985 machine_mode inmode, machine_mode outmode,
1986 reg_class_t rclass, int for_real, int earlyclobber)
1988 rtx in = real_in;
1989 rtx out = real_out;
1990 int in_offset = 0;
1991 int out_offset = 0;
1992 rtx value = 0;
1994 /* If operands exceed a word, we can't use either of them
1995 unless they have the same size. */
1996 if (GET_MODE_SIZE (outmode) != GET_MODE_SIZE (inmode)
1997 && (GET_MODE_SIZE (outmode) > UNITS_PER_WORD
1998 || GET_MODE_SIZE (inmode) > UNITS_PER_WORD))
1999 return 0;
2001 /* Note that {in,out}_offset are needed only when 'in' or 'out'
2002 respectively refers to a hard register. */
2004 /* Find the inside of any subregs. */
2005 while (GET_CODE (out) == SUBREG)
2007 if (REG_P (SUBREG_REG (out))
2008 && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER)
2009 out_offset += subreg_regno_offset (REGNO (SUBREG_REG (out)),
2010 GET_MODE (SUBREG_REG (out)),
2011 SUBREG_BYTE (out),
2012 GET_MODE (out));
2013 out = SUBREG_REG (out);
2015 while (GET_CODE (in) == SUBREG)
2017 if (REG_P (SUBREG_REG (in))
2018 && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER)
2019 in_offset += subreg_regno_offset (REGNO (SUBREG_REG (in)),
2020 GET_MODE (SUBREG_REG (in)),
2021 SUBREG_BYTE (in),
2022 GET_MODE (in));
2023 in = SUBREG_REG (in);
2026 /* Narrow down the reg class, the same way push_reload will;
2027 otherwise we might find a dummy now, but push_reload won't. */
2029 reg_class_t preferred_class = targetm.preferred_reload_class (in, rclass);
2030 if (preferred_class != NO_REGS)
2031 rclass = (enum reg_class) preferred_class;
2034 /* See if OUT will do. */
2035 if (REG_P (out)
2036 && REGNO (out) < FIRST_PSEUDO_REGISTER)
2038 unsigned int regno = REGNO (out) + out_offset;
2039 unsigned int nwords = hard_regno_nregs[regno][outmode];
2040 rtx saved_rtx;
2042 /* When we consider whether the insn uses OUT,
2043 ignore references within IN. They don't prevent us
2044 from copying IN into OUT, because those refs would
2045 move into the insn that reloads IN.
2047 However, we only ignore IN in its role as this reload.
2048 If the insn uses IN elsewhere and it contains OUT,
2049 that counts. We can't be sure it's the "same" operand
2050 so it might not go through this reload.
2052 We also need to avoid using OUT if it, or part of it, is a
2053 fixed register. Modifying such registers, even transiently,
2054 may have undefined effects on the machine, such as modifying
2055 the stack pointer. */
2056 saved_rtx = *inloc;
2057 *inloc = const0_rtx;
2059 if (regno < FIRST_PSEUDO_REGISTER
2060 && HARD_REGNO_MODE_OK (regno, outmode)
2061 && ! refers_to_regno_for_reload_p (regno, regno + nwords,
2062 PATTERN (this_insn), outloc))
2064 unsigned int i;
2066 for (i = 0; i < nwords; i++)
2067 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
2068 regno + i)
2069 || fixed_regs[regno + i])
2070 break;
2072 if (i == nwords)
2074 if (REG_P (real_out))
2075 value = real_out;
2076 else
2077 value = gen_rtx_REG (outmode, regno);
2081 *inloc = saved_rtx;
2084 /* Consider using IN if OUT was not acceptable
2085 or if OUT dies in this insn (like the quotient in a divmod insn).
2086 We can't use IN unless it is dies in this insn,
2087 which means we must know accurately which hard regs are live.
2088 Also, the result can't go in IN if IN is used within OUT,
2089 or if OUT is an earlyclobber and IN appears elsewhere in the insn. */
2090 if (hard_regs_live_known
2091 && REG_P (in)
2092 && REGNO (in) < FIRST_PSEUDO_REGISTER
2093 && (value == 0
2094 || find_reg_note (this_insn, REG_UNUSED, real_out))
2095 && find_reg_note (this_insn, REG_DEAD, real_in)
2096 && !fixed_regs[REGNO (in)]
2097 && HARD_REGNO_MODE_OK (REGNO (in),
2098 /* The only case where out and real_out might
2099 have different modes is where real_out
2100 is a subreg, and in that case, out
2101 has a real mode. */
2102 (GET_MODE (out) != VOIDmode
2103 ? GET_MODE (out) : outmode))
2104 && (ORIGINAL_REGNO (in) < FIRST_PSEUDO_REGISTER
2105 /* However only do this if we can be sure that this input
2106 operand doesn't correspond with an uninitialized pseudo.
2107 global can assign some hardreg to it that is the same as
2108 the one assigned to a different, also live pseudo (as it
2109 can ignore the conflict). We must never introduce writes
2110 to such hardregs, as they would clobber the other live
2111 pseudo. See PR 20973. */
2112 || (!bitmap_bit_p (DF_LR_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
2113 ORIGINAL_REGNO (in))
2114 /* Similarly, only do this if we can be sure that the death
2115 note is still valid. global can assign some hardreg to
2116 the pseudo referenced in the note and simultaneously a
2117 subword of this hardreg to a different, also live pseudo,
2118 because only another subword of the hardreg is actually
2119 used in the insn. This cannot happen if the pseudo has
2120 been assigned exactly one hardreg. See PR 33732. */
2121 && hard_regno_nregs[REGNO (in)][GET_MODE (in)] == 1)))
2123 unsigned int regno = REGNO (in) + in_offset;
2124 unsigned int nwords = hard_regno_nregs[regno][inmode];
2126 if (! refers_to_regno_for_reload_p (regno, regno + nwords, out, (rtx*) 0)
2127 && ! hard_reg_set_here_p (regno, regno + nwords,
2128 PATTERN (this_insn))
2129 && (! earlyclobber
2130 || ! refers_to_regno_for_reload_p (regno, regno + nwords,
2131 PATTERN (this_insn), inloc)))
2133 unsigned int i;
2135 for (i = 0; i < nwords; i++)
2136 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
2137 regno + i))
2138 break;
2140 if (i == nwords)
2142 /* If we were going to use OUT as the reload reg
2143 and changed our mind, it means OUT is a dummy that
2144 dies here. So don't bother copying value to it. */
2145 if (for_real >= 0 && value == real_out)
2146 rld[for_real].out = 0;
2147 if (REG_P (real_in))
2148 value = real_in;
2149 else
2150 value = gen_rtx_REG (inmode, regno);
2155 return value;
2158 /* This page contains subroutines used mainly for determining
2159 whether the IN or an OUT of a reload can serve as the
2160 reload register. */
2162 /* Return 1 if X is an operand of an insn that is being earlyclobbered. */
2165 earlyclobber_operand_p (rtx x)
2167 int i;
2169 for (i = 0; i < n_earlyclobbers; i++)
2170 if (reload_earlyclobbers[i] == x)
2171 return 1;
2173 return 0;
2176 /* Return 1 if expression X alters a hard reg in the range
2177 from BEG_REGNO (inclusive) to END_REGNO (exclusive),
2178 either explicitly or in the guise of a pseudo-reg allocated to REGNO.
2179 X should be the body of an instruction. */
2181 static int
2182 hard_reg_set_here_p (unsigned int beg_regno, unsigned int end_regno, rtx x)
2184 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
2186 rtx op0 = SET_DEST (x);
2188 while (GET_CODE (op0) == SUBREG)
2189 op0 = SUBREG_REG (op0);
2190 if (REG_P (op0))
2192 unsigned int r = REGNO (op0);
2194 /* See if this reg overlaps range under consideration. */
2195 if (r < end_regno
2196 && end_hard_regno (GET_MODE (op0), r) > beg_regno)
2197 return 1;
2200 else if (GET_CODE (x) == PARALLEL)
2202 int i = XVECLEN (x, 0) - 1;
2204 for (; i >= 0; i--)
2205 if (hard_reg_set_here_p (beg_regno, end_regno, XVECEXP (x, 0, i)))
2206 return 1;
2209 return 0;
2212 /* Return 1 if ADDR is a valid memory address for mode MODE
2213 in address space AS, and check that each pseudo reg has the
2214 proper kind of hard reg. */
2217 strict_memory_address_addr_space_p (machine_mode mode ATTRIBUTE_UNUSED,
2218 rtx addr, addr_space_t as)
2220 #ifdef GO_IF_LEGITIMATE_ADDRESS
2221 gcc_assert (ADDR_SPACE_GENERIC_P (as));
2222 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2223 return 0;
2225 win:
2226 return 1;
2227 #else
2228 return targetm.addr_space.legitimate_address_p (mode, addr, 1, as);
2229 #endif
2232 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
2233 if they are the same hard reg, and has special hacks for
2234 autoincrement and autodecrement.
2235 This is specifically intended for find_reloads to use
2236 in determining whether two operands match.
2237 X is the operand whose number is the lower of the two.
2239 The value is 2 if Y contains a pre-increment that matches
2240 a non-incrementing address in X. */
2242 /* ??? To be completely correct, we should arrange to pass
2243 for X the output operand and for Y the input operand.
2244 For now, we assume that the output operand has the lower number
2245 because that is natural in (SET output (... input ...)). */
2248 operands_match_p (rtx x, rtx y)
2250 int i;
2251 RTX_CODE code = GET_CODE (x);
2252 const char *fmt;
2253 int success_2;
2255 if (x == y)
2256 return 1;
2257 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
2258 && (REG_P (y) || (GET_CODE (y) == SUBREG
2259 && REG_P (SUBREG_REG (y)))))
2261 int j;
2263 if (code == SUBREG)
2265 i = REGNO (SUBREG_REG (x));
2266 if (i >= FIRST_PSEUDO_REGISTER)
2267 goto slow;
2268 i += subreg_regno_offset (REGNO (SUBREG_REG (x)),
2269 GET_MODE (SUBREG_REG (x)),
2270 SUBREG_BYTE (x),
2271 GET_MODE (x));
2273 else
2274 i = REGNO (x);
2276 if (GET_CODE (y) == SUBREG)
2278 j = REGNO (SUBREG_REG (y));
2279 if (j >= FIRST_PSEUDO_REGISTER)
2280 goto slow;
2281 j += subreg_regno_offset (REGNO (SUBREG_REG (y)),
2282 GET_MODE (SUBREG_REG (y)),
2283 SUBREG_BYTE (y),
2284 GET_MODE (y));
2286 else
2287 j = REGNO (y);
2289 /* On a REG_WORDS_BIG_ENDIAN machine, point to the last register of a
2290 multiple hard register group of scalar integer registers, so that
2291 for example (reg:DI 0) and (reg:SI 1) will be considered the same
2292 register. */
2293 if (REG_WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
2294 && SCALAR_INT_MODE_P (GET_MODE (x))
2295 && i < FIRST_PSEUDO_REGISTER)
2296 i += hard_regno_nregs[i][GET_MODE (x)] - 1;
2297 if (REG_WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (y)) > UNITS_PER_WORD
2298 && SCALAR_INT_MODE_P (GET_MODE (y))
2299 && j < FIRST_PSEUDO_REGISTER)
2300 j += hard_regno_nregs[j][GET_MODE (y)] - 1;
2302 return i == j;
2304 /* If two operands must match, because they are really a single
2305 operand of an assembler insn, then two postincrements are invalid
2306 because the assembler insn would increment only once.
2307 On the other hand, a postincrement matches ordinary indexing
2308 if the postincrement is the output operand. */
2309 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
2310 return operands_match_p (XEXP (x, 0), y);
2311 /* Two preincrements are invalid
2312 because the assembler insn would increment only once.
2313 On the other hand, a preincrement matches ordinary indexing
2314 if the preincrement is the input operand.
2315 In this case, return 2, since some callers need to do special
2316 things when this happens. */
2317 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
2318 || GET_CODE (y) == PRE_MODIFY)
2319 return operands_match_p (x, XEXP (y, 0)) ? 2 : 0;
2321 slow:
2323 /* Now we have disposed of all the cases in which different rtx codes
2324 can match. */
2325 if (code != GET_CODE (y))
2326 return 0;
2328 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
2329 if (GET_MODE (x) != GET_MODE (y))
2330 return 0;
2332 /* MEMs referring to different address space are not equivalent. */
2333 if (code == MEM && MEM_ADDR_SPACE (x) != MEM_ADDR_SPACE (y))
2334 return 0;
2336 switch (code)
2338 CASE_CONST_UNIQUE:
2339 return 0;
2341 case LABEL_REF:
2342 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
2343 case SYMBOL_REF:
2344 return XSTR (x, 0) == XSTR (y, 0);
2346 default:
2347 break;
2350 /* Compare the elements. If any pair of corresponding elements
2351 fail to match, return 0 for the whole things. */
2353 success_2 = 0;
2354 fmt = GET_RTX_FORMAT (code);
2355 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2357 int val, j;
2358 switch (fmt[i])
2360 case 'w':
2361 if (XWINT (x, i) != XWINT (y, i))
2362 return 0;
2363 break;
2365 case 'i':
2366 if (XINT (x, i) != XINT (y, i))
2367 return 0;
2368 break;
2370 case 'e':
2371 val = operands_match_p (XEXP (x, i), XEXP (y, i));
2372 if (val == 0)
2373 return 0;
2374 /* If any subexpression returns 2,
2375 we should return 2 if we are successful. */
2376 if (val == 2)
2377 success_2 = 1;
2378 break;
2380 case '0':
2381 break;
2383 case 'E':
2384 if (XVECLEN (x, i) != XVECLEN (y, i))
2385 return 0;
2386 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
2388 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j));
2389 if (val == 0)
2390 return 0;
2391 if (val == 2)
2392 success_2 = 1;
2394 break;
2396 /* It is believed that rtx's at this level will never
2397 contain anything but integers and other rtx's,
2398 except for within LABEL_REFs and SYMBOL_REFs. */
2399 default:
2400 gcc_unreachable ();
2403 return 1 + success_2;
2406 /* Describe the range of registers or memory referenced by X.
2407 If X is a register, set REG_FLAG and put the first register
2408 number into START and the last plus one into END.
2409 If X is a memory reference, put a base address into BASE
2410 and a range of integer offsets into START and END.
2411 If X is pushing on the stack, we can assume it causes no trouble,
2412 so we set the SAFE field. */
2414 static struct decomposition
2415 decompose (rtx x)
2417 struct decomposition val;
2418 int all_const = 0;
2420 memset (&val, 0, sizeof (val));
2422 switch (GET_CODE (x))
2424 case MEM:
2426 rtx base = NULL_RTX, offset = 0;
2427 rtx addr = XEXP (x, 0);
2429 if (GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == PRE_INC
2430 || GET_CODE (addr) == POST_DEC || GET_CODE (addr) == POST_INC)
2432 val.base = XEXP (addr, 0);
2433 val.start = -GET_MODE_SIZE (GET_MODE (x));
2434 val.end = GET_MODE_SIZE (GET_MODE (x));
2435 val.safe = REGNO (val.base) == STACK_POINTER_REGNUM;
2436 return val;
2439 if (GET_CODE (addr) == PRE_MODIFY || GET_CODE (addr) == POST_MODIFY)
2441 if (GET_CODE (XEXP (addr, 1)) == PLUS
2442 && XEXP (addr, 0) == XEXP (XEXP (addr, 1), 0)
2443 && CONSTANT_P (XEXP (XEXP (addr, 1), 1)))
2445 val.base = XEXP (addr, 0);
2446 val.start = -INTVAL (XEXP (XEXP (addr, 1), 1));
2447 val.end = INTVAL (XEXP (XEXP (addr, 1), 1));
2448 val.safe = REGNO (val.base) == STACK_POINTER_REGNUM;
2449 return val;
2453 if (GET_CODE (addr) == CONST)
2455 addr = XEXP (addr, 0);
2456 all_const = 1;
2458 if (GET_CODE (addr) == PLUS)
2460 if (CONSTANT_P (XEXP (addr, 0)))
2462 base = XEXP (addr, 1);
2463 offset = XEXP (addr, 0);
2465 else if (CONSTANT_P (XEXP (addr, 1)))
2467 base = XEXP (addr, 0);
2468 offset = XEXP (addr, 1);
2472 if (offset == 0)
2474 base = addr;
2475 offset = const0_rtx;
2477 if (GET_CODE (offset) == CONST)
2478 offset = XEXP (offset, 0);
2479 if (GET_CODE (offset) == PLUS)
2481 if (CONST_INT_P (XEXP (offset, 0)))
2483 base = gen_rtx_PLUS (GET_MODE (base), base, XEXP (offset, 1));
2484 offset = XEXP (offset, 0);
2486 else if (CONST_INT_P (XEXP (offset, 1)))
2488 base = gen_rtx_PLUS (GET_MODE (base), base, XEXP (offset, 0));
2489 offset = XEXP (offset, 1);
2491 else
2493 base = gen_rtx_PLUS (GET_MODE (base), base, offset);
2494 offset = const0_rtx;
2497 else if (!CONST_INT_P (offset))
2499 base = gen_rtx_PLUS (GET_MODE (base), base, offset);
2500 offset = const0_rtx;
2503 if (all_const && GET_CODE (base) == PLUS)
2504 base = gen_rtx_CONST (GET_MODE (base), base);
2506 gcc_assert (CONST_INT_P (offset));
2508 val.start = INTVAL (offset);
2509 val.end = val.start + GET_MODE_SIZE (GET_MODE (x));
2510 val.base = base;
2512 break;
2514 case REG:
2515 val.reg_flag = 1;
2516 val.start = true_regnum (x);
2517 if (val.start < 0 || val.start >= FIRST_PSEUDO_REGISTER)
2519 /* A pseudo with no hard reg. */
2520 val.start = REGNO (x);
2521 val.end = val.start + 1;
2523 else
2524 /* A hard reg. */
2525 val.end = end_hard_regno (GET_MODE (x), val.start);
2526 break;
2528 case SUBREG:
2529 if (!REG_P (SUBREG_REG (x)))
2530 /* This could be more precise, but it's good enough. */
2531 return decompose (SUBREG_REG (x));
2532 val.reg_flag = 1;
2533 val.start = true_regnum (x);
2534 if (val.start < 0 || val.start >= FIRST_PSEUDO_REGISTER)
2535 return decompose (SUBREG_REG (x));
2536 else
2537 /* A hard reg. */
2538 val.end = val.start + subreg_nregs (x);
2539 break;
2541 case SCRATCH:
2542 /* This hasn't been assigned yet, so it can't conflict yet. */
2543 val.safe = 1;
2544 break;
2546 default:
2547 gcc_assert (CONSTANT_P (x));
2548 val.safe = 1;
2549 break;
2551 return val;
2554 /* Return 1 if altering Y will not modify the value of X.
2555 Y is also described by YDATA, which should be decompose (Y). */
2557 static int
2558 immune_p (rtx x, rtx y, struct decomposition ydata)
2560 struct decomposition xdata;
2562 if (ydata.reg_flag)
2563 return !refers_to_regno_for_reload_p (ydata.start, ydata.end, x, (rtx*) 0);
2564 if (ydata.safe)
2565 return 1;
2567 gcc_assert (MEM_P (y));
2568 /* If Y is memory and X is not, Y can't affect X. */
2569 if (!MEM_P (x))
2570 return 1;
2572 xdata = decompose (x);
2574 if (! rtx_equal_p (xdata.base, ydata.base))
2576 /* If bases are distinct symbolic constants, there is no overlap. */
2577 if (CONSTANT_P (xdata.base) && CONSTANT_P (ydata.base))
2578 return 1;
2579 /* Constants and stack slots never overlap. */
2580 if (CONSTANT_P (xdata.base)
2581 && (ydata.base == frame_pointer_rtx
2582 || ydata.base == hard_frame_pointer_rtx
2583 || ydata.base == stack_pointer_rtx))
2584 return 1;
2585 if (CONSTANT_P (ydata.base)
2586 && (xdata.base == frame_pointer_rtx
2587 || xdata.base == hard_frame_pointer_rtx
2588 || xdata.base == stack_pointer_rtx))
2589 return 1;
2590 /* If either base is variable, we don't know anything. */
2591 return 0;
2594 return (xdata.start >= ydata.end || ydata.start >= xdata.end);
2597 /* Similar, but calls decompose. */
2600 safe_from_earlyclobber (rtx op, rtx clobber)
2602 struct decomposition early_data;
2604 early_data = decompose (clobber);
2605 return immune_p (op, clobber, early_data);
2608 /* Main entry point of this file: search the body of INSN
2609 for values that need reloading and record them with push_reload.
2610 REPLACE nonzero means record also where the values occur
2611 so that subst_reloads can be used.
2613 IND_LEVELS says how many levels of indirection are supported by this
2614 machine; a value of zero means that a memory reference is not a valid
2615 memory address.
2617 LIVE_KNOWN says we have valid information about which hard
2618 regs are live at each point in the program; this is true when
2619 we are called from global_alloc but false when stupid register
2620 allocation has been done.
2622 RELOAD_REG_P if nonzero is a vector indexed by hard reg number
2623 which is nonnegative if the reg has been commandeered for reloading into.
2624 It is copied into STATIC_RELOAD_REG_P and referenced from there
2625 by various subroutines.
2627 Return TRUE if some operands need to be changed, because of swapping
2628 commutative operands, reg_equiv_address substitution, or whatever. */
2631 find_reloads (rtx_insn *insn, int replace, int ind_levels, int live_known,
2632 short *reload_reg_p)
2634 int insn_code_number;
2635 int i, j;
2636 int noperands;
2637 /* These start out as the constraints for the insn
2638 and they are chewed up as we consider alternatives. */
2639 const char *constraints[MAX_RECOG_OPERANDS];
2640 /* These are the preferred classes for an operand, or NO_REGS if it isn't
2641 a register. */
2642 enum reg_class preferred_class[MAX_RECOG_OPERANDS];
2643 char pref_or_nothing[MAX_RECOG_OPERANDS];
2644 /* Nonzero for a MEM operand whose entire address needs a reload.
2645 May be -1 to indicate the entire address may or may not need a reload. */
2646 int address_reloaded[MAX_RECOG_OPERANDS];
2647 /* Nonzero for an address operand that needs to be completely reloaded.
2648 May be -1 to indicate the entire operand may or may not need a reload. */
2649 int address_operand_reloaded[MAX_RECOG_OPERANDS];
2650 /* Value of enum reload_type to use for operand. */
2651 enum reload_type operand_type[MAX_RECOG_OPERANDS];
2652 /* Value of enum reload_type to use within address of operand. */
2653 enum reload_type address_type[MAX_RECOG_OPERANDS];
2654 /* Save the usage of each operand. */
2655 enum reload_usage { RELOAD_READ, RELOAD_READ_WRITE, RELOAD_WRITE } modified[MAX_RECOG_OPERANDS];
2656 int no_input_reloads = 0, no_output_reloads = 0;
2657 int n_alternatives;
2658 reg_class_t this_alternative[MAX_RECOG_OPERANDS];
2659 char this_alternative_match_win[MAX_RECOG_OPERANDS];
2660 char this_alternative_win[MAX_RECOG_OPERANDS];
2661 char this_alternative_offmemok[MAX_RECOG_OPERANDS];
2662 char this_alternative_earlyclobber[MAX_RECOG_OPERANDS];
2663 int this_alternative_matches[MAX_RECOG_OPERANDS];
2664 reg_class_t goal_alternative[MAX_RECOG_OPERANDS];
2665 int this_alternative_number;
2666 int goal_alternative_number = 0;
2667 int operand_reloadnum[MAX_RECOG_OPERANDS];
2668 int goal_alternative_matches[MAX_RECOG_OPERANDS];
2669 int goal_alternative_matched[MAX_RECOG_OPERANDS];
2670 char goal_alternative_match_win[MAX_RECOG_OPERANDS];
2671 char goal_alternative_win[MAX_RECOG_OPERANDS];
2672 char goal_alternative_offmemok[MAX_RECOG_OPERANDS];
2673 char goal_alternative_earlyclobber[MAX_RECOG_OPERANDS];
2674 int goal_alternative_swapped;
2675 int best;
2676 int commutative;
2677 char operands_match[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
2678 rtx substed_operand[MAX_RECOG_OPERANDS];
2679 rtx body = PATTERN (insn);
2680 rtx set = single_set (insn);
2681 int goal_earlyclobber = 0, this_earlyclobber;
2682 machine_mode operand_mode[MAX_RECOG_OPERANDS];
2683 int retval = 0;
2685 this_insn = insn;
2686 n_reloads = 0;
2687 n_replacements = 0;
2688 n_earlyclobbers = 0;
2689 replace_reloads = replace;
2690 hard_regs_live_known = live_known;
2691 static_reload_reg_p = reload_reg_p;
2693 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output reloads;
2694 neither are insns that SET cc0. Insns that use CC0 are not allowed
2695 to have any input reloads. */
2696 if (JUMP_P (insn) || CALL_P (insn))
2697 no_output_reloads = 1;
2699 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (insn)))
2700 no_input_reloads = 1;
2701 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (insn)))
2702 no_output_reloads = 1;
2704 #ifdef SECONDARY_MEMORY_NEEDED
2705 /* The eliminated forms of any secondary memory locations are per-insn, so
2706 clear them out here. */
2708 if (secondary_memlocs_elim_used)
2710 memset (secondary_memlocs_elim, 0,
2711 sizeof (secondary_memlocs_elim[0]) * secondary_memlocs_elim_used);
2712 secondary_memlocs_elim_used = 0;
2714 #endif
2716 /* Dispose quickly of (set (reg..) (reg..)) if both have hard regs and it
2717 is cheap to move between them. If it is not, there may not be an insn
2718 to do the copy, so we may need a reload. */
2719 if (GET_CODE (body) == SET
2720 && REG_P (SET_DEST (body))
2721 && REGNO (SET_DEST (body)) < FIRST_PSEUDO_REGISTER
2722 && REG_P (SET_SRC (body))
2723 && REGNO (SET_SRC (body)) < FIRST_PSEUDO_REGISTER
2724 && register_move_cost (GET_MODE (SET_SRC (body)),
2725 REGNO_REG_CLASS (REGNO (SET_SRC (body))),
2726 REGNO_REG_CLASS (REGNO (SET_DEST (body)))) == 2)
2727 return 0;
2729 extract_insn (insn);
2731 noperands = reload_n_operands = recog_data.n_operands;
2732 n_alternatives = recog_data.n_alternatives;
2734 /* Just return "no reloads" if insn has no operands with constraints. */
2735 if (noperands == 0 || n_alternatives == 0)
2736 return 0;
2738 insn_code_number = INSN_CODE (insn);
2739 this_insn_is_asm = insn_code_number < 0;
2741 memcpy (operand_mode, recog_data.operand_mode,
2742 noperands * sizeof (machine_mode));
2743 memcpy (constraints, recog_data.constraints,
2744 noperands * sizeof (const char *));
2746 commutative = -1;
2748 /* If we will need to know, later, whether some pair of operands
2749 are the same, we must compare them now and save the result.
2750 Reloading the base and index registers will clobber them
2751 and afterward they will fail to match. */
2753 for (i = 0; i < noperands; i++)
2755 const char *p;
2756 int c;
2757 char *end;
2759 substed_operand[i] = recog_data.operand[i];
2760 p = constraints[i];
2762 modified[i] = RELOAD_READ;
2764 /* Scan this operand's constraint to see if it is an output operand,
2765 an in-out operand, is commutative, or should match another. */
2767 while ((c = *p))
2769 p += CONSTRAINT_LEN (c, p);
2770 switch (c)
2772 case '=':
2773 modified[i] = RELOAD_WRITE;
2774 break;
2775 case '+':
2776 modified[i] = RELOAD_READ_WRITE;
2777 break;
2778 case '%':
2780 /* The last operand should not be marked commutative. */
2781 gcc_assert (i != noperands - 1);
2783 /* We currently only support one commutative pair of
2784 operands. Some existing asm code currently uses more
2785 than one pair. Previously, that would usually work,
2786 but sometimes it would crash the compiler. We
2787 continue supporting that case as well as we can by
2788 silently ignoring all but the first pair. In the
2789 future we may handle it correctly. */
2790 if (commutative < 0)
2791 commutative = i;
2792 else
2793 gcc_assert (this_insn_is_asm);
2795 break;
2796 /* Use of ISDIGIT is tempting here, but it may get expensive because
2797 of locale support we don't want. */
2798 case '0': case '1': case '2': case '3': case '4':
2799 case '5': case '6': case '7': case '8': case '9':
2801 c = strtoul (p - 1, &end, 10);
2802 p = end;
2804 operands_match[c][i]
2805 = operands_match_p (recog_data.operand[c],
2806 recog_data.operand[i]);
2808 /* An operand may not match itself. */
2809 gcc_assert (c != i);
2811 /* If C can be commuted with C+1, and C might need to match I,
2812 then C+1 might also need to match I. */
2813 if (commutative >= 0)
2815 if (c == commutative || c == commutative + 1)
2817 int other = c + (c == commutative ? 1 : -1);
2818 operands_match[other][i]
2819 = operands_match_p (recog_data.operand[other],
2820 recog_data.operand[i]);
2822 if (i == commutative || i == commutative + 1)
2824 int other = i + (i == commutative ? 1 : -1);
2825 operands_match[c][other]
2826 = operands_match_p (recog_data.operand[c],
2827 recog_data.operand[other]);
2829 /* Note that C is supposed to be less than I.
2830 No need to consider altering both C and I because in
2831 that case we would alter one into the other. */
2838 /* Examine each operand that is a memory reference or memory address
2839 and reload parts of the addresses into index registers.
2840 Also here any references to pseudo regs that didn't get hard regs
2841 but are equivalent to constants get replaced in the insn itself
2842 with those constants. Nobody will ever see them again.
2844 Finally, set up the preferred classes of each operand. */
2846 for (i = 0; i < noperands; i++)
2848 RTX_CODE code = GET_CODE (recog_data.operand[i]);
2850 address_reloaded[i] = 0;
2851 address_operand_reloaded[i] = 0;
2852 operand_type[i] = (modified[i] == RELOAD_READ ? RELOAD_FOR_INPUT
2853 : modified[i] == RELOAD_WRITE ? RELOAD_FOR_OUTPUT
2854 : RELOAD_OTHER);
2855 address_type[i]
2856 = (modified[i] == RELOAD_READ ? RELOAD_FOR_INPUT_ADDRESS
2857 : modified[i] == RELOAD_WRITE ? RELOAD_FOR_OUTPUT_ADDRESS
2858 : RELOAD_OTHER);
2860 if (*constraints[i] == 0)
2861 /* Ignore things like match_operator operands. */
2863 else if (insn_extra_address_constraint
2864 (lookup_constraint (constraints[i])))
2866 address_operand_reloaded[i]
2867 = find_reloads_address (recog_data.operand_mode[i], (rtx*) 0,
2868 recog_data.operand[i],
2869 recog_data.operand_loc[i],
2870 i, operand_type[i], ind_levels, insn);
2872 /* If we now have a simple operand where we used to have a
2873 PLUS or MULT, re-recognize and try again. */
2874 if ((OBJECT_P (*recog_data.operand_loc[i])
2875 || GET_CODE (*recog_data.operand_loc[i]) == SUBREG)
2876 && (GET_CODE (recog_data.operand[i]) == MULT
2877 || GET_CODE (recog_data.operand[i]) == PLUS))
2879 INSN_CODE (insn) = -1;
2880 retval = find_reloads (insn, replace, ind_levels, live_known,
2881 reload_reg_p);
2882 return retval;
2885 recog_data.operand[i] = *recog_data.operand_loc[i];
2886 substed_operand[i] = recog_data.operand[i];
2888 /* Address operands are reloaded in their existing mode,
2889 no matter what is specified in the machine description. */
2890 operand_mode[i] = GET_MODE (recog_data.operand[i]);
2892 /* If the address is a single CONST_INT pick address mode
2893 instead otherwise we will later not know in which mode
2894 the reload should be performed. */
2895 if (operand_mode[i] == VOIDmode)
2896 operand_mode[i] = Pmode;
2899 else if (code == MEM)
2901 address_reloaded[i]
2902 = find_reloads_address (GET_MODE (recog_data.operand[i]),
2903 recog_data.operand_loc[i],
2904 XEXP (recog_data.operand[i], 0),
2905 &XEXP (recog_data.operand[i], 0),
2906 i, address_type[i], ind_levels, insn);
2907 recog_data.operand[i] = *recog_data.operand_loc[i];
2908 substed_operand[i] = recog_data.operand[i];
2910 else if (code == SUBREG)
2912 rtx reg = SUBREG_REG (recog_data.operand[i]);
2913 rtx op
2914 = find_reloads_toplev (recog_data.operand[i], i, address_type[i],
2915 ind_levels,
2916 set != 0
2917 && &SET_DEST (set) == recog_data.operand_loc[i],
2918 insn,
2919 &address_reloaded[i]);
2921 /* If we made a MEM to load (a part of) the stackslot of a pseudo
2922 that didn't get a hard register, emit a USE with a REG_EQUAL
2923 note in front so that we might inherit a previous, possibly
2924 wider reload. */
2926 if (replace
2927 && MEM_P (op)
2928 && REG_P (reg)
2929 && (GET_MODE_SIZE (GET_MODE (reg))
2930 >= GET_MODE_SIZE (GET_MODE (op)))
2931 && reg_equiv_constant (REGNO (reg)) == 0)
2932 set_unique_reg_note (emit_insn_before (gen_rtx_USE (VOIDmode, reg),
2933 insn),
2934 REG_EQUAL, reg_equiv_memory_loc (REGNO (reg)));
2936 substed_operand[i] = recog_data.operand[i] = op;
2938 else if (code == PLUS || GET_RTX_CLASS (code) == RTX_UNARY)
2939 /* We can get a PLUS as an "operand" as a result of register
2940 elimination. See eliminate_regs and gen_reload. We handle
2941 a unary operator by reloading the operand. */
2942 substed_operand[i] = recog_data.operand[i]
2943 = find_reloads_toplev (recog_data.operand[i], i, address_type[i],
2944 ind_levels, 0, insn,
2945 &address_reloaded[i]);
2946 else if (code == REG)
2948 /* This is equivalent to calling find_reloads_toplev.
2949 The code is duplicated for speed.
2950 When we find a pseudo always equivalent to a constant,
2951 we replace it by the constant. We must be sure, however,
2952 that we don't try to replace it in the insn in which it
2953 is being set. */
2954 int regno = REGNO (recog_data.operand[i]);
2955 if (reg_equiv_constant (regno) != 0
2956 && (set == 0 || &SET_DEST (set) != recog_data.operand_loc[i]))
2958 /* Record the existing mode so that the check if constants are
2959 allowed will work when operand_mode isn't specified. */
2961 if (operand_mode[i] == VOIDmode)
2962 operand_mode[i] = GET_MODE (recog_data.operand[i]);
2964 substed_operand[i] = recog_data.operand[i]
2965 = reg_equiv_constant (regno);
2967 if (reg_equiv_memory_loc (regno) != 0
2968 && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
2969 /* We need not give a valid is_set_dest argument since the case
2970 of a constant equivalence was checked above. */
2971 substed_operand[i] = recog_data.operand[i]
2972 = find_reloads_toplev (recog_data.operand[i], i, address_type[i],
2973 ind_levels, 0, insn,
2974 &address_reloaded[i]);
2976 /* If the operand is still a register (we didn't replace it with an
2977 equivalent), get the preferred class to reload it into. */
2978 code = GET_CODE (recog_data.operand[i]);
2979 preferred_class[i]
2980 = ((code == REG && REGNO (recog_data.operand[i])
2981 >= FIRST_PSEUDO_REGISTER)
2982 ? reg_preferred_class (REGNO (recog_data.operand[i]))
2983 : NO_REGS);
2984 pref_or_nothing[i]
2985 = (code == REG
2986 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER
2987 && reg_alternate_class (REGNO (recog_data.operand[i])) == NO_REGS);
2990 /* If this is simply a copy from operand 1 to operand 0, merge the
2991 preferred classes for the operands. */
2992 if (set != 0 && noperands >= 2 && recog_data.operand[0] == SET_DEST (set)
2993 && recog_data.operand[1] == SET_SRC (set))
2995 preferred_class[0] = preferred_class[1]
2996 = reg_class_subunion[(int) preferred_class[0]][(int) preferred_class[1]];
2997 pref_or_nothing[0] |= pref_or_nothing[1];
2998 pref_or_nothing[1] |= pref_or_nothing[0];
3001 /* Now see what we need for pseudo-regs that didn't get hard regs
3002 or got the wrong kind of hard reg. For this, we must consider
3003 all the operands together against the register constraints. */
3005 best = MAX_RECOG_OPERANDS * 2 + 600;
3007 goal_alternative_swapped = 0;
3009 /* The constraints are made of several alternatives.
3010 Each operand's constraint looks like foo,bar,... with commas
3011 separating the alternatives. The first alternatives for all
3012 operands go together, the second alternatives go together, etc.
3014 First loop over alternatives. */
3016 alternative_mask enabled = get_enabled_alternatives (insn);
3017 for (this_alternative_number = 0;
3018 this_alternative_number < n_alternatives;
3019 this_alternative_number++)
3021 int swapped;
3023 if (!TEST_BIT (enabled, this_alternative_number))
3025 int i;
3027 for (i = 0; i < recog_data.n_operands; i++)
3028 constraints[i] = skip_alternative (constraints[i]);
3030 continue;
3033 /* If insn is commutative (it's safe to exchange a certain pair
3034 of operands) then we need to try each alternative twice, the
3035 second time matching those two operands as if we had
3036 exchanged them. To do this, really exchange them in
3037 operands. */
3038 for (swapped = 0; swapped < (commutative >= 0 ? 2 : 1); swapped++)
3040 /* Loop over operands for one constraint alternative. */
3041 /* LOSERS counts those that don't fit this alternative
3042 and would require loading. */
3043 int losers = 0;
3044 /* BAD is set to 1 if it some operand can't fit this alternative
3045 even after reloading. */
3046 int bad = 0;
3047 /* REJECT is a count of how undesirable this alternative says it is
3048 if any reloading is required. If the alternative matches exactly
3049 then REJECT is ignored, but otherwise it gets this much
3050 counted against it in addition to the reloading needed. Each
3051 ? counts three times here since we want the disparaging caused by
3052 a bad register class to only count 1/3 as much. */
3053 int reject = 0;
3055 if (swapped)
3057 recog_data.operand[commutative] = substed_operand[commutative + 1];
3058 recog_data.operand[commutative + 1] = substed_operand[commutative];
3059 /* Swap the duplicates too. */
3060 for (i = 0; i < recog_data.n_dups; i++)
3061 if (recog_data.dup_num[i] == commutative
3062 || recog_data.dup_num[i] == commutative + 1)
3063 *recog_data.dup_loc[i]
3064 = recog_data.operand[(int) recog_data.dup_num[i]];
3066 std::swap (preferred_class[commutative],
3067 preferred_class[commutative + 1]);
3068 std::swap (pref_or_nothing[commutative],
3069 pref_or_nothing[commutative + 1]);
3070 std::swap (address_reloaded[commutative],
3071 address_reloaded[commutative + 1]);
3074 this_earlyclobber = 0;
3076 for (i = 0; i < noperands; i++)
3078 const char *p = constraints[i];
3079 char *end;
3080 int len;
3081 int win = 0;
3082 int did_match = 0;
3083 /* 0 => this operand can be reloaded somehow for this alternative. */
3084 int badop = 1;
3085 /* 0 => this operand can be reloaded if the alternative allows regs. */
3086 int winreg = 0;
3087 int c;
3088 int m;
3089 rtx operand = recog_data.operand[i];
3090 int offset = 0;
3091 /* Nonzero means this is a MEM that must be reloaded into a reg
3092 regardless of what the constraint says. */
3093 int force_reload = 0;
3094 int offmemok = 0;
3095 /* Nonzero if a constant forced into memory would be OK for this
3096 operand. */
3097 int constmemok = 0;
3098 int earlyclobber = 0;
3099 enum constraint_num cn;
3100 enum reg_class cl;
3102 /* If the predicate accepts a unary operator, it means that
3103 we need to reload the operand, but do not do this for
3104 match_operator and friends. */
3105 if (UNARY_P (operand) && *p != 0)
3106 operand = XEXP (operand, 0);
3108 /* If the operand is a SUBREG, extract
3109 the REG or MEM (or maybe even a constant) within.
3110 (Constants can occur as a result of reg_equiv_constant.) */
3112 while (GET_CODE (operand) == SUBREG)
3114 /* Offset only matters when operand is a REG and
3115 it is a hard reg. This is because it is passed
3116 to reg_fits_class_p if it is a REG and all pseudos
3117 return 0 from that function. */
3118 if (REG_P (SUBREG_REG (operand))
3119 && REGNO (SUBREG_REG (operand)) < FIRST_PSEUDO_REGISTER)
3121 if (simplify_subreg_regno (REGNO (SUBREG_REG (operand)),
3122 GET_MODE (SUBREG_REG (operand)),
3123 SUBREG_BYTE (operand),
3124 GET_MODE (operand)) < 0)
3125 force_reload = 1;
3126 offset += subreg_regno_offset (REGNO (SUBREG_REG (operand)),
3127 GET_MODE (SUBREG_REG (operand)),
3128 SUBREG_BYTE (operand),
3129 GET_MODE (operand));
3131 operand = SUBREG_REG (operand);
3132 /* Force reload if this is a constant or PLUS or if there may
3133 be a problem accessing OPERAND in the outer mode. */
3134 if (CONSTANT_P (operand)
3135 || GET_CODE (operand) == PLUS
3136 /* We must force a reload of paradoxical SUBREGs
3137 of a MEM because the alignment of the inner value
3138 may not be enough to do the outer reference. On
3139 big-endian machines, it may also reference outside
3140 the object.
3142 On machines that extend byte operations and we have a
3143 SUBREG where both the inner and outer modes are no wider
3144 than a word and the inner mode is narrower, is integral,
3145 and gets extended when loaded from memory, combine.c has
3146 made assumptions about the behavior of the machine in such
3147 register access. If the data is, in fact, in memory we
3148 must always load using the size assumed to be in the
3149 register and let the insn do the different-sized
3150 accesses.
3152 This is doubly true if WORD_REGISTER_OPERATIONS. In
3153 this case eliminate_regs has left non-paradoxical
3154 subregs for push_reload to see. Make sure it does
3155 by forcing the reload.
3157 ??? When is it right at this stage to have a subreg
3158 of a mem that is _not_ to be handled specially? IMO
3159 those should have been reduced to just a mem. */
3160 || ((MEM_P (operand)
3161 || (REG_P (operand)
3162 && REGNO (operand) >= FIRST_PSEUDO_REGISTER))
3163 #ifndef WORD_REGISTER_OPERATIONS
3164 && (((GET_MODE_BITSIZE (GET_MODE (operand))
3165 < BIGGEST_ALIGNMENT)
3166 && (GET_MODE_SIZE (operand_mode[i])
3167 > GET_MODE_SIZE (GET_MODE (operand))))
3168 || BYTES_BIG_ENDIAN
3169 #ifdef LOAD_EXTEND_OP
3170 || (GET_MODE_SIZE (operand_mode[i]) <= UNITS_PER_WORD
3171 && (GET_MODE_SIZE (GET_MODE (operand))
3172 <= UNITS_PER_WORD)
3173 && (GET_MODE_SIZE (operand_mode[i])
3174 > GET_MODE_SIZE (GET_MODE (operand)))
3175 && INTEGRAL_MODE_P (GET_MODE (operand))
3176 && LOAD_EXTEND_OP (GET_MODE (operand)) != UNKNOWN)
3177 #endif
3179 #endif
3182 force_reload = 1;
3185 this_alternative[i] = NO_REGS;
3186 this_alternative_win[i] = 0;
3187 this_alternative_match_win[i] = 0;
3188 this_alternative_offmemok[i] = 0;
3189 this_alternative_earlyclobber[i] = 0;
3190 this_alternative_matches[i] = -1;
3192 /* An empty constraint or empty alternative
3193 allows anything which matched the pattern. */
3194 if (*p == 0 || *p == ',')
3195 win = 1, badop = 0;
3197 /* Scan this alternative's specs for this operand;
3198 set WIN if the operand fits any letter in this alternative.
3199 Otherwise, clear BADOP if this operand could
3200 fit some letter after reloads,
3201 or set WINREG if this operand could fit after reloads
3202 provided the constraint allows some registers. */
3205 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
3207 case '\0':
3208 len = 0;
3209 break;
3210 case ',':
3211 c = '\0';
3212 break;
3214 case '?':
3215 reject += 6;
3216 break;
3218 case '!':
3219 reject = 600;
3220 break;
3222 case '#':
3223 /* Ignore rest of this alternative as far as
3224 reloading is concerned. */
3226 p++;
3227 while (*p && *p != ',');
3228 len = 0;
3229 break;
3231 case '0': case '1': case '2': case '3': case '4':
3232 case '5': case '6': case '7': case '8': case '9':
3233 m = strtoul (p, &end, 10);
3234 p = end;
3235 len = 0;
3237 this_alternative_matches[i] = m;
3238 /* We are supposed to match a previous operand.
3239 If we do, we win if that one did.
3240 If we do not, count both of the operands as losers.
3241 (This is too conservative, since most of the time
3242 only a single reload insn will be needed to make
3243 the two operands win. As a result, this alternative
3244 may be rejected when it is actually desirable.) */
3245 if ((swapped && (m != commutative || i != commutative + 1))
3246 /* If we are matching as if two operands were swapped,
3247 also pretend that operands_match had been computed
3248 with swapped.
3249 But if I is the second of those and C is the first,
3250 don't exchange them, because operands_match is valid
3251 only on one side of its diagonal. */
3252 ? (operands_match
3253 [(m == commutative || m == commutative + 1)
3254 ? 2 * commutative + 1 - m : m]
3255 [(i == commutative || i == commutative + 1)
3256 ? 2 * commutative + 1 - i : i])
3257 : operands_match[m][i])
3259 /* If we are matching a non-offsettable address where an
3260 offsettable address was expected, then we must reject
3261 this combination, because we can't reload it. */
3262 if (this_alternative_offmemok[m]
3263 && MEM_P (recog_data.operand[m])
3264 && this_alternative[m] == NO_REGS
3265 && ! this_alternative_win[m])
3266 bad = 1;
3268 did_match = this_alternative_win[m];
3270 else
3272 /* Operands don't match. */
3273 rtx value;
3274 int loc1, loc2;
3275 /* Retroactively mark the operand we had to match
3276 as a loser, if it wasn't already. */
3277 if (this_alternative_win[m])
3278 losers++;
3279 this_alternative_win[m] = 0;
3280 if (this_alternative[m] == NO_REGS)
3281 bad = 1;
3282 /* But count the pair only once in the total badness of
3283 this alternative, if the pair can be a dummy reload.
3284 The pointers in operand_loc are not swapped; swap
3285 them by hand if necessary. */
3286 if (swapped && i == commutative)
3287 loc1 = commutative + 1;
3288 else if (swapped && i == commutative + 1)
3289 loc1 = commutative;
3290 else
3291 loc1 = i;
3292 if (swapped && m == commutative)
3293 loc2 = commutative + 1;
3294 else if (swapped && m == commutative + 1)
3295 loc2 = commutative;
3296 else
3297 loc2 = m;
3298 value
3299 = find_dummy_reload (recog_data.operand[i],
3300 recog_data.operand[m],
3301 recog_data.operand_loc[loc1],
3302 recog_data.operand_loc[loc2],
3303 operand_mode[i], operand_mode[m],
3304 this_alternative[m], -1,
3305 this_alternative_earlyclobber[m]);
3307 if (value != 0)
3308 losers--;
3310 /* This can be fixed with reloads if the operand
3311 we are supposed to match can be fixed with reloads. */
3312 badop = 0;
3313 this_alternative[i] = this_alternative[m];
3315 /* If we have to reload this operand and some previous
3316 operand also had to match the same thing as this
3317 operand, we don't know how to do that. So reject this
3318 alternative. */
3319 if (! did_match || force_reload)
3320 for (j = 0; j < i; j++)
3321 if (this_alternative_matches[j]
3322 == this_alternative_matches[i])
3324 badop = 1;
3325 break;
3327 break;
3329 case 'p':
3330 /* All necessary reloads for an address_operand
3331 were handled in find_reloads_address. */
3332 this_alternative[i]
3333 = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
3334 ADDRESS, SCRATCH);
3335 win = 1;
3336 badop = 0;
3337 break;
3339 case TARGET_MEM_CONSTRAINT:
3340 if (force_reload)
3341 break;
3342 if (MEM_P (operand)
3343 || (REG_P (operand)
3344 && REGNO (operand) >= FIRST_PSEUDO_REGISTER
3345 && reg_renumber[REGNO (operand)] < 0))
3346 win = 1;
3347 if (CONST_POOL_OK_P (operand_mode[i], operand))
3348 badop = 0;
3349 constmemok = 1;
3350 break;
3352 case '<':
3353 if (MEM_P (operand)
3354 && ! address_reloaded[i]
3355 && (GET_CODE (XEXP (operand, 0)) == PRE_DEC
3356 || GET_CODE (XEXP (operand, 0)) == POST_DEC))
3357 win = 1;
3358 break;
3360 case '>':
3361 if (MEM_P (operand)
3362 && ! address_reloaded[i]
3363 && (GET_CODE (XEXP (operand, 0)) == PRE_INC
3364 || GET_CODE (XEXP (operand, 0)) == POST_INC))
3365 win = 1;
3366 break;
3368 /* Memory operand whose address is not offsettable. */
3369 case 'V':
3370 if (force_reload)
3371 break;
3372 if (MEM_P (operand)
3373 && ! (ind_levels ? offsettable_memref_p (operand)
3374 : offsettable_nonstrict_memref_p (operand))
3375 /* Certain mem addresses will become offsettable
3376 after they themselves are reloaded. This is important;
3377 we don't want our own handling of unoffsettables
3378 to override the handling of reg_equiv_address. */
3379 && !(REG_P (XEXP (operand, 0))
3380 && (ind_levels == 0
3381 || reg_equiv_address (REGNO (XEXP (operand, 0))) != 0)))
3382 win = 1;
3383 break;
3385 /* Memory operand whose address is offsettable. */
3386 case 'o':
3387 if (force_reload)
3388 break;
3389 if ((MEM_P (operand)
3390 /* If IND_LEVELS, find_reloads_address won't reload a
3391 pseudo that didn't get a hard reg, so we have to
3392 reject that case. */
3393 && ((ind_levels ? offsettable_memref_p (operand)
3394 : offsettable_nonstrict_memref_p (operand))
3395 /* A reloaded address is offsettable because it is now
3396 just a simple register indirect. */
3397 || address_reloaded[i] == 1))
3398 || (REG_P (operand)
3399 && REGNO (operand) >= FIRST_PSEUDO_REGISTER
3400 && reg_renumber[REGNO (operand)] < 0
3401 /* If reg_equiv_address is nonzero, we will be
3402 loading it into a register; hence it will be
3403 offsettable, but we cannot say that reg_equiv_mem
3404 is offsettable without checking. */
3405 && ((reg_equiv_mem (REGNO (operand)) != 0
3406 && offsettable_memref_p (reg_equiv_mem (REGNO (operand))))
3407 || (reg_equiv_address (REGNO (operand)) != 0))))
3408 win = 1;
3409 if (CONST_POOL_OK_P (operand_mode[i], operand)
3410 || MEM_P (operand))
3411 badop = 0;
3412 constmemok = 1;
3413 offmemok = 1;
3414 break;
3416 case '&':
3417 /* Output operand that is stored before the need for the
3418 input operands (and their index registers) is over. */
3419 earlyclobber = 1, this_earlyclobber = 1;
3420 break;
3422 case 'X':
3423 force_reload = 0;
3424 win = 1;
3425 break;
3427 case 'g':
3428 if (! force_reload
3429 /* A PLUS is never a valid operand, but reload can make
3430 it from a register when eliminating registers. */
3431 && GET_CODE (operand) != PLUS
3432 /* A SCRATCH is not a valid operand. */
3433 && GET_CODE (operand) != SCRATCH
3434 && (! CONSTANT_P (operand)
3435 || ! flag_pic
3436 || LEGITIMATE_PIC_OPERAND_P (operand))
3437 && (GENERAL_REGS == ALL_REGS
3438 || !REG_P (operand)
3439 || (REGNO (operand) >= FIRST_PSEUDO_REGISTER
3440 && reg_renumber[REGNO (operand)] < 0)))
3441 win = 1;
3442 cl = GENERAL_REGS;
3443 goto reg;
3445 default:
3446 cn = lookup_constraint (p);
3447 switch (get_constraint_type (cn))
3449 case CT_REGISTER:
3450 cl = reg_class_for_constraint (cn);
3451 if (cl != NO_REGS)
3452 goto reg;
3453 break;
3455 case CT_CONST_INT:
3456 if (CONST_INT_P (operand)
3457 && (insn_const_int_ok_for_constraint
3458 (INTVAL (operand), cn)))
3459 win = true;
3460 break;
3462 case CT_MEMORY:
3463 if (force_reload)
3464 break;
3465 if (constraint_satisfied_p (operand, cn))
3466 win = 1;
3467 /* If the address was already reloaded,
3468 we win as well. */
3469 else if (MEM_P (operand) && address_reloaded[i] == 1)
3470 win = 1;
3471 /* Likewise if the address will be reloaded because
3472 reg_equiv_address is nonzero. For reg_equiv_mem
3473 we have to check. */
3474 else if (REG_P (operand)
3475 && REGNO (operand) >= FIRST_PSEUDO_REGISTER
3476 && reg_renumber[REGNO (operand)] < 0
3477 && ((reg_equiv_mem (REGNO (operand)) != 0
3478 && (constraint_satisfied_p
3479 (reg_equiv_mem (REGNO (operand)),
3480 cn)))
3481 || (reg_equiv_address (REGNO (operand))
3482 != 0)))
3483 win = 1;
3485 /* If we didn't already win, we can reload
3486 constants via force_const_mem, and other
3487 MEMs by reloading the address like for 'o'. */
3488 if (CONST_POOL_OK_P (operand_mode[i], operand)
3489 || MEM_P (operand))
3490 badop = 0;
3491 constmemok = 1;
3492 offmemok = 1;
3493 break;
3495 case CT_ADDRESS:
3496 if (constraint_satisfied_p (operand, cn))
3497 win = 1;
3499 /* If we didn't already win, we can reload
3500 the address into a base register. */
3501 this_alternative[i]
3502 = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
3503 ADDRESS, SCRATCH);
3504 badop = 0;
3505 break;
3507 case CT_FIXED_FORM:
3508 if (constraint_satisfied_p (operand, cn))
3509 win = 1;
3510 break;
3512 break;
3514 reg:
3515 this_alternative[i]
3516 = reg_class_subunion[this_alternative[i]][cl];
3517 if (GET_MODE (operand) == BLKmode)
3518 break;
3519 winreg = 1;
3520 if (REG_P (operand)
3521 && reg_fits_class_p (operand, this_alternative[i],
3522 offset, GET_MODE (recog_data.operand[i])))
3523 win = 1;
3524 break;
3526 while ((p += len), c);
3528 if (swapped == (commutative >= 0 ? 1 : 0))
3529 constraints[i] = p;
3531 /* If this operand could be handled with a reg,
3532 and some reg is allowed, then this operand can be handled. */
3533 if (winreg && this_alternative[i] != NO_REGS
3534 && (win || !class_only_fixed_regs[this_alternative[i]]))
3535 badop = 0;
3537 /* Record which operands fit this alternative. */
3538 this_alternative_earlyclobber[i] = earlyclobber;
3539 if (win && ! force_reload)
3540 this_alternative_win[i] = 1;
3541 else if (did_match && ! force_reload)
3542 this_alternative_match_win[i] = 1;
3543 else
3545 int const_to_mem = 0;
3547 this_alternative_offmemok[i] = offmemok;
3548 losers++;
3549 if (badop)
3550 bad = 1;
3551 /* Alternative loses if it has no regs for a reg operand. */
3552 if (REG_P (operand)
3553 && this_alternative[i] == NO_REGS
3554 && this_alternative_matches[i] < 0)
3555 bad = 1;
3557 /* If this is a constant that is reloaded into the desired
3558 class by copying it to memory first, count that as another
3559 reload. This is consistent with other code and is
3560 required to avoid choosing another alternative when
3561 the constant is moved into memory by this function on
3562 an early reload pass. Note that the test here is
3563 precisely the same as in the code below that calls
3564 force_const_mem. */
3565 if (CONST_POOL_OK_P (operand_mode[i], operand)
3566 && ((targetm.preferred_reload_class (operand,
3567 this_alternative[i])
3568 == NO_REGS)
3569 || no_input_reloads))
3571 const_to_mem = 1;
3572 if (this_alternative[i] != NO_REGS)
3573 losers++;
3576 /* Alternative loses if it requires a type of reload not
3577 permitted for this insn. We can always reload SCRATCH
3578 and objects with a REG_UNUSED note. */
3579 if (GET_CODE (operand) != SCRATCH
3580 && modified[i] != RELOAD_READ && no_output_reloads
3581 && ! find_reg_note (insn, REG_UNUSED, operand))
3582 bad = 1;
3583 else if (modified[i] != RELOAD_WRITE && no_input_reloads
3584 && ! const_to_mem)
3585 bad = 1;
3587 /* If we can't reload this value at all, reject this
3588 alternative. Note that we could also lose due to
3589 LIMIT_RELOAD_CLASS, but we don't check that
3590 here. */
3592 if (! CONSTANT_P (operand) && this_alternative[i] != NO_REGS)
3594 if (targetm.preferred_reload_class (operand,
3595 this_alternative[i])
3596 == NO_REGS)
3597 reject = 600;
3599 if (operand_type[i] == RELOAD_FOR_OUTPUT
3600 && (targetm.preferred_output_reload_class (operand,
3601 this_alternative[i])
3602 == NO_REGS))
3603 reject = 600;
3606 /* We prefer to reload pseudos over reloading other things,
3607 since such reloads may be able to be eliminated later.
3608 If we are reloading a SCRATCH, we won't be generating any
3609 insns, just using a register, so it is also preferred.
3610 So bump REJECT in other cases. Don't do this in the
3611 case where we are forcing a constant into memory and
3612 it will then win since we don't want to have a different
3613 alternative match then. */
3614 if (! (REG_P (operand)
3615 && REGNO (operand) >= FIRST_PSEUDO_REGISTER)
3616 && GET_CODE (operand) != SCRATCH
3617 && ! (const_to_mem && constmemok))
3618 reject += 2;
3620 /* Input reloads can be inherited more often than output
3621 reloads can be removed, so penalize output reloads. */
3622 if (operand_type[i] != RELOAD_FOR_INPUT
3623 && GET_CODE (operand) != SCRATCH)
3624 reject++;
3627 /* If this operand is a pseudo register that didn't get
3628 a hard reg and this alternative accepts some
3629 register, see if the class that we want is a subset
3630 of the preferred class for this register. If not,
3631 but it intersects that class, use the preferred class
3632 instead. If it does not intersect the preferred
3633 class, show that usage of this alternative should be
3634 discouraged; it will be discouraged more still if the
3635 register is `preferred or nothing'. We do this
3636 because it increases the chance of reusing our spill
3637 register in a later insn and avoiding a pair of
3638 memory stores and loads.
3640 Don't bother with this if this alternative will
3641 accept this operand.
3643 Don't do this for a multiword operand, since it is
3644 only a small win and has the risk of requiring more
3645 spill registers, which could cause a large loss.
3647 Don't do this if the preferred class has only one
3648 register because we might otherwise exhaust the
3649 class. */
3651 if (! win && ! did_match
3652 && this_alternative[i] != NO_REGS
3653 && GET_MODE_SIZE (operand_mode[i]) <= UNITS_PER_WORD
3654 && reg_class_size [(int) preferred_class[i]] > 0
3655 && ! small_register_class_p (preferred_class[i]))
3657 if (! reg_class_subset_p (this_alternative[i],
3658 preferred_class[i]))
3660 /* Since we don't have a way of forming the intersection,
3661 we just do something special if the preferred class
3662 is a subset of the class we have; that's the most
3663 common case anyway. */
3664 if (reg_class_subset_p (preferred_class[i],
3665 this_alternative[i]))
3666 this_alternative[i] = preferred_class[i];
3667 else
3668 reject += (2 + 2 * pref_or_nothing[i]);
3673 /* Now see if any output operands that are marked "earlyclobber"
3674 in this alternative conflict with any input operands
3675 or any memory addresses. */
3677 for (i = 0; i < noperands; i++)
3678 if (this_alternative_earlyclobber[i]
3679 && (this_alternative_win[i] || this_alternative_match_win[i]))
3681 struct decomposition early_data;
3683 early_data = decompose (recog_data.operand[i]);
3685 gcc_assert (modified[i] != RELOAD_READ);
3687 if (this_alternative[i] == NO_REGS)
3689 this_alternative_earlyclobber[i] = 0;
3690 gcc_assert (this_insn_is_asm);
3691 error_for_asm (this_insn,
3692 "%<&%> constraint used with no register class");
3695 for (j = 0; j < noperands; j++)
3696 /* Is this an input operand or a memory ref? */
3697 if ((MEM_P (recog_data.operand[j])
3698 || modified[j] != RELOAD_WRITE)
3699 && j != i
3700 /* Ignore things like match_operator operands. */
3701 && !recog_data.is_operator[j]
3702 /* Don't count an input operand that is constrained to match
3703 the early clobber operand. */
3704 && ! (this_alternative_matches[j] == i
3705 && rtx_equal_p (recog_data.operand[i],
3706 recog_data.operand[j]))
3707 /* Is it altered by storing the earlyclobber operand? */
3708 && !immune_p (recog_data.operand[j], recog_data.operand[i],
3709 early_data))
3711 /* If the output is in a non-empty few-regs class,
3712 it's costly to reload it, so reload the input instead. */
3713 if (small_register_class_p (this_alternative[i])
3714 && (REG_P (recog_data.operand[j])
3715 || GET_CODE (recog_data.operand[j]) == SUBREG))
3717 losers++;
3718 this_alternative_win[j] = 0;
3719 this_alternative_match_win[j] = 0;
3721 else
3722 break;
3724 /* If an earlyclobber operand conflicts with something,
3725 it must be reloaded, so request this and count the cost. */
3726 if (j != noperands)
3728 losers++;
3729 this_alternative_win[i] = 0;
3730 this_alternative_match_win[j] = 0;
3731 for (j = 0; j < noperands; j++)
3732 if (this_alternative_matches[j] == i
3733 && this_alternative_match_win[j])
3735 this_alternative_win[j] = 0;
3736 this_alternative_match_win[j] = 0;
3737 losers++;
3742 /* If one alternative accepts all the operands, no reload required,
3743 choose that alternative; don't consider the remaining ones. */
3744 if (losers == 0)
3746 /* Unswap these so that they are never swapped at `finish'. */
3747 if (swapped)
3749 recog_data.operand[commutative] = substed_operand[commutative];
3750 recog_data.operand[commutative + 1]
3751 = substed_operand[commutative + 1];
3753 for (i = 0; i < noperands; i++)
3755 goal_alternative_win[i] = this_alternative_win[i];
3756 goal_alternative_match_win[i] = this_alternative_match_win[i];
3757 goal_alternative[i] = this_alternative[i];
3758 goal_alternative_offmemok[i] = this_alternative_offmemok[i];
3759 goal_alternative_matches[i] = this_alternative_matches[i];
3760 goal_alternative_earlyclobber[i]
3761 = this_alternative_earlyclobber[i];
3763 goal_alternative_number = this_alternative_number;
3764 goal_alternative_swapped = swapped;
3765 goal_earlyclobber = this_earlyclobber;
3766 goto finish;
3769 /* REJECT, set by the ! and ? constraint characters and when a register
3770 would be reloaded into a non-preferred class, discourages the use of
3771 this alternative for a reload goal. REJECT is incremented by six
3772 for each ? and two for each non-preferred class. */
3773 losers = losers * 6 + reject;
3775 /* If this alternative can be made to work by reloading,
3776 and it needs less reloading than the others checked so far,
3777 record it as the chosen goal for reloading. */
3778 if (! bad)
3780 if (best > losers)
3782 for (i = 0; i < noperands; i++)
3784 goal_alternative[i] = this_alternative[i];
3785 goal_alternative_win[i] = this_alternative_win[i];
3786 goal_alternative_match_win[i]
3787 = this_alternative_match_win[i];
3788 goal_alternative_offmemok[i]
3789 = this_alternative_offmemok[i];
3790 goal_alternative_matches[i] = this_alternative_matches[i];
3791 goal_alternative_earlyclobber[i]
3792 = this_alternative_earlyclobber[i];
3794 goal_alternative_swapped = swapped;
3795 best = losers;
3796 goal_alternative_number = this_alternative_number;
3797 goal_earlyclobber = this_earlyclobber;
3801 if (swapped)
3803 /* If the commutative operands have been swapped, swap
3804 them back in order to check the next alternative. */
3805 recog_data.operand[commutative] = substed_operand[commutative];
3806 recog_data.operand[commutative + 1] = substed_operand[commutative + 1];
3807 /* Unswap the duplicates too. */
3808 for (i = 0; i < recog_data.n_dups; i++)
3809 if (recog_data.dup_num[i] == commutative
3810 || recog_data.dup_num[i] == commutative + 1)
3811 *recog_data.dup_loc[i]
3812 = recog_data.operand[(int) recog_data.dup_num[i]];
3814 /* Unswap the operand related information as well. */
3815 std::swap (preferred_class[commutative],
3816 preferred_class[commutative + 1]);
3817 std::swap (pref_or_nothing[commutative],
3818 pref_or_nothing[commutative + 1]);
3819 std::swap (address_reloaded[commutative],
3820 address_reloaded[commutative + 1]);
3825 /* The operands don't meet the constraints.
3826 goal_alternative describes the alternative
3827 that we could reach by reloading the fewest operands.
3828 Reload so as to fit it. */
3830 if (best == MAX_RECOG_OPERANDS * 2 + 600)
3832 /* No alternative works with reloads?? */
3833 if (insn_code_number >= 0)
3834 fatal_insn ("unable to generate reloads for:", insn);
3835 error_for_asm (insn, "inconsistent operand constraints in an %<asm%>");
3836 /* Avoid further trouble with this insn. */
3837 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3838 n_reloads = 0;
3839 return 0;
3842 /* Jump to `finish' from above if all operands are valid already.
3843 In that case, goal_alternative_win is all 1. */
3844 finish:
3846 /* Right now, for any pair of operands I and J that are required to match,
3847 with I < J,
3848 goal_alternative_matches[J] is I.
3849 Set up goal_alternative_matched as the inverse function:
3850 goal_alternative_matched[I] = J. */
3852 for (i = 0; i < noperands; i++)
3853 goal_alternative_matched[i] = -1;
3855 for (i = 0; i < noperands; i++)
3856 if (! goal_alternative_win[i]
3857 && goal_alternative_matches[i] >= 0)
3858 goal_alternative_matched[goal_alternative_matches[i]] = i;
3860 for (i = 0; i < noperands; i++)
3861 goal_alternative_win[i] |= goal_alternative_match_win[i];
3863 /* If the best alternative is with operands 1 and 2 swapped,
3864 consider them swapped before reporting the reloads. Update the
3865 operand numbers of any reloads already pushed. */
3867 if (goal_alternative_swapped)
3869 std::swap (substed_operand[commutative],
3870 substed_operand[commutative + 1]);
3871 std::swap (recog_data.operand[commutative],
3872 recog_data.operand[commutative + 1]);
3873 std::swap (*recog_data.operand_loc[commutative],
3874 *recog_data.operand_loc[commutative + 1]);
3876 for (i = 0; i < n_reloads; i++)
3878 if (rld[i].opnum == commutative)
3879 rld[i].opnum = commutative + 1;
3880 else if (rld[i].opnum == commutative + 1)
3881 rld[i].opnum = commutative;
3885 for (i = 0; i < noperands; i++)
3887 operand_reloadnum[i] = -1;
3889 /* If this is an earlyclobber operand, we need to widen the scope.
3890 The reload must remain valid from the start of the insn being
3891 reloaded until after the operand is stored into its destination.
3892 We approximate this with RELOAD_OTHER even though we know that we
3893 do not conflict with RELOAD_FOR_INPUT_ADDRESS reloads.
3895 One special case that is worth checking is when we have an
3896 output that is earlyclobber but isn't used past the insn (typically
3897 a SCRATCH). In this case, we only need have the reload live
3898 through the insn itself, but not for any of our input or output
3899 reloads.
3900 But we must not accidentally narrow the scope of an existing
3901 RELOAD_OTHER reload - leave these alone.
3903 In any case, anything needed to address this operand can remain
3904 however they were previously categorized. */
3906 if (goal_alternative_earlyclobber[i] && operand_type[i] != RELOAD_OTHER)
3907 operand_type[i]
3908 = (find_reg_note (insn, REG_UNUSED, recog_data.operand[i])
3909 ? RELOAD_FOR_INSN : RELOAD_OTHER);
3912 /* Any constants that aren't allowed and can't be reloaded
3913 into registers are here changed into memory references. */
3914 for (i = 0; i < noperands; i++)
3915 if (! goal_alternative_win[i])
3917 rtx op = recog_data.operand[i];
3918 rtx subreg = NULL_RTX;
3919 rtx plus = NULL_RTX;
3920 machine_mode mode = operand_mode[i];
3922 /* Reloads of SUBREGs of CONSTANT RTXs are handled later in
3923 push_reload so we have to let them pass here. */
3924 if (GET_CODE (op) == SUBREG)
3926 subreg = op;
3927 op = SUBREG_REG (op);
3928 mode = GET_MODE (op);
3931 if (GET_CODE (op) == PLUS)
3933 plus = op;
3934 op = XEXP (op, 1);
3937 if (CONST_POOL_OK_P (mode, op)
3938 && ((targetm.preferred_reload_class (op, goal_alternative[i])
3939 == NO_REGS)
3940 || no_input_reloads))
3942 int this_address_reloaded;
3943 rtx tem = force_const_mem (mode, op);
3945 /* If we stripped a SUBREG or a PLUS above add it back. */
3946 if (plus != NULL_RTX)
3947 tem = gen_rtx_PLUS (mode, XEXP (plus, 0), tem);
3949 if (subreg != NULL_RTX)
3950 tem = gen_rtx_SUBREG (operand_mode[i], tem, SUBREG_BYTE (subreg));
3952 this_address_reloaded = 0;
3953 substed_operand[i] = recog_data.operand[i]
3954 = find_reloads_toplev (tem, i, address_type[i], ind_levels,
3955 0, insn, &this_address_reloaded);
3957 /* If the alternative accepts constant pool refs directly
3958 there will be no reload needed at all. */
3959 if (plus == NULL_RTX
3960 && subreg == NULL_RTX
3961 && alternative_allows_const_pool_ref (this_address_reloaded == 0
3962 ? substed_operand[i]
3963 : NULL,
3964 recog_data.constraints[i],
3965 goal_alternative_number))
3966 goal_alternative_win[i] = 1;
3970 /* Record the values of the earlyclobber operands for the caller. */
3971 if (goal_earlyclobber)
3972 for (i = 0; i < noperands; i++)
3973 if (goal_alternative_earlyclobber[i])
3974 reload_earlyclobbers[n_earlyclobbers++] = recog_data.operand[i];
3976 /* Now record reloads for all the operands that need them. */
3977 for (i = 0; i < noperands; i++)
3978 if (! goal_alternative_win[i])
3980 /* Operands that match previous ones have already been handled. */
3981 if (goal_alternative_matches[i] >= 0)
3983 /* Handle an operand with a nonoffsettable address
3984 appearing where an offsettable address will do
3985 by reloading the address into a base register.
3987 ??? We can also do this when the operand is a register and
3988 reg_equiv_mem is not offsettable, but this is a bit tricky,
3989 so we don't bother with it. It may not be worth doing. */
3990 else if (goal_alternative_matched[i] == -1
3991 && goal_alternative_offmemok[i]
3992 && MEM_P (recog_data.operand[i]))
3994 /* If the address to be reloaded is a VOIDmode constant,
3995 use the default address mode as mode of the reload register,
3996 as would have been done by find_reloads_address. */
3997 addr_space_t as = MEM_ADDR_SPACE (recog_data.operand[i]);
3998 machine_mode address_mode;
4000 address_mode = get_address_mode (recog_data.operand[i]);
4001 operand_reloadnum[i]
4002 = push_reload (XEXP (recog_data.operand[i], 0), NULL_RTX,
4003 &XEXP (recog_data.operand[i], 0), (rtx*) 0,
4004 base_reg_class (VOIDmode, as, MEM, SCRATCH),
4005 address_mode,
4006 VOIDmode, 0, 0, i, RELOAD_FOR_INPUT);
4007 rld[operand_reloadnum[i]].inc
4008 = GET_MODE_SIZE (GET_MODE (recog_data.operand[i]));
4010 /* If this operand is an output, we will have made any
4011 reloads for its address as RELOAD_FOR_OUTPUT_ADDRESS, but
4012 now we are treating part of the operand as an input, so
4013 we must change these to RELOAD_FOR_INPUT_ADDRESS. */
4015 if (modified[i] == RELOAD_WRITE)
4017 for (j = 0; j < n_reloads; j++)
4019 if (rld[j].opnum == i)
4021 if (rld[j].when_needed == RELOAD_FOR_OUTPUT_ADDRESS)
4022 rld[j].when_needed = RELOAD_FOR_INPUT_ADDRESS;
4023 else if (rld[j].when_needed
4024 == RELOAD_FOR_OUTADDR_ADDRESS)
4025 rld[j].when_needed = RELOAD_FOR_INPADDR_ADDRESS;
4030 else if (goal_alternative_matched[i] == -1)
4032 operand_reloadnum[i]
4033 = push_reload ((modified[i] != RELOAD_WRITE
4034 ? recog_data.operand[i] : 0),
4035 (modified[i] != RELOAD_READ
4036 ? recog_data.operand[i] : 0),
4037 (modified[i] != RELOAD_WRITE
4038 ? recog_data.operand_loc[i] : 0),
4039 (modified[i] != RELOAD_READ
4040 ? recog_data.operand_loc[i] : 0),
4041 (enum reg_class) goal_alternative[i],
4042 (modified[i] == RELOAD_WRITE
4043 ? VOIDmode : operand_mode[i]),
4044 (modified[i] == RELOAD_READ
4045 ? VOIDmode : operand_mode[i]),
4046 (insn_code_number < 0 ? 0
4047 : insn_data[insn_code_number].operand[i].strict_low),
4048 0, i, operand_type[i]);
4050 /* In a matching pair of operands, one must be input only
4051 and the other must be output only.
4052 Pass the input operand as IN and the other as OUT. */
4053 else if (modified[i] == RELOAD_READ
4054 && modified[goal_alternative_matched[i]] == RELOAD_WRITE)
4056 operand_reloadnum[i]
4057 = push_reload (recog_data.operand[i],
4058 recog_data.operand[goal_alternative_matched[i]],
4059 recog_data.operand_loc[i],
4060 recog_data.operand_loc[goal_alternative_matched[i]],
4061 (enum reg_class) goal_alternative[i],
4062 operand_mode[i],
4063 operand_mode[goal_alternative_matched[i]],
4064 0, 0, i, RELOAD_OTHER);
4065 operand_reloadnum[goal_alternative_matched[i]] = output_reloadnum;
4067 else if (modified[i] == RELOAD_WRITE
4068 && modified[goal_alternative_matched[i]] == RELOAD_READ)
4070 operand_reloadnum[goal_alternative_matched[i]]
4071 = push_reload (recog_data.operand[goal_alternative_matched[i]],
4072 recog_data.operand[i],
4073 recog_data.operand_loc[goal_alternative_matched[i]],
4074 recog_data.operand_loc[i],
4075 (enum reg_class) goal_alternative[i],
4076 operand_mode[goal_alternative_matched[i]],
4077 operand_mode[i],
4078 0, 0, i, RELOAD_OTHER);
4079 operand_reloadnum[i] = output_reloadnum;
4081 else
4083 gcc_assert (insn_code_number < 0);
4084 error_for_asm (insn, "inconsistent operand constraints "
4085 "in an %<asm%>");
4086 /* Avoid further trouble with this insn. */
4087 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
4088 n_reloads = 0;
4089 return 0;
4092 else if (goal_alternative_matched[i] < 0
4093 && goal_alternative_matches[i] < 0
4094 && address_operand_reloaded[i] != 1
4095 && optimize)
4097 /* For each non-matching operand that's a MEM or a pseudo-register
4098 that didn't get a hard register, make an optional reload.
4099 This may get done even if the insn needs no reloads otherwise. */
4101 rtx operand = recog_data.operand[i];
4103 while (GET_CODE (operand) == SUBREG)
4104 operand = SUBREG_REG (operand);
4105 if ((MEM_P (operand)
4106 || (REG_P (operand)
4107 && REGNO (operand) >= FIRST_PSEUDO_REGISTER))
4108 /* If this is only for an output, the optional reload would not
4109 actually cause us to use a register now, just note that
4110 something is stored here. */
4111 && (goal_alternative[i] != NO_REGS
4112 || modified[i] == RELOAD_WRITE)
4113 && ! no_input_reloads
4114 /* An optional output reload might allow to delete INSN later.
4115 We mustn't make in-out reloads on insns that are not permitted
4116 output reloads.
4117 If this is an asm, we can't delete it; we must not even call
4118 push_reload for an optional output reload in this case,
4119 because we can't be sure that the constraint allows a register,
4120 and push_reload verifies the constraints for asms. */
4121 && (modified[i] == RELOAD_READ
4122 || (! no_output_reloads && ! this_insn_is_asm)))
4123 operand_reloadnum[i]
4124 = push_reload ((modified[i] != RELOAD_WRITE
4125 ? recog_data.operand[i] : 0),
4126 (modified[i] != RELOAD_READ
4127 ? recog_data.operand[i] : 0),
4128 (modified[i] != RELOAD_WRITE
4129 ? recog_data.operand_loc[i] : 0),
4130 (modified[i] != RELOAD_READ
4131 ? recog_data.operand_loc[i] : 0),
4132 (enum reg_class) goal_alternative[i],
4133 (modified[i] == RELOAD_WRITE
4134 ? VOIDmode : operand_mode[i]),
4135 (modified[i] == RELOAD_READ
4136 ? VOIDmode : operand_mode[i]),
4137 (insn_code_number < 0 ? 0
4138 : insn_data[insn_code_number].operand[i].strict_low),
4139 1, i, operand_type[i]);
4140 /* If a memory reference remains (either as a MEM or a pseudo that
4141 did not get a hard register), yet we can't make an optional
4142 reload, check if this is actually a pseudo register reference;
4143 we then need to emit a USE and/or a CLOBBER so that reload
4144 inheritance will do the right thing. */
4145 else if (replace
4146 && (MEM_P (operand)
4147 || (REG_P (operand)
4148 && REGNO (operand) >= FIRST_PSEUDO_REGISTER
4149 && reg_renumber [REGNO (operand)] < 0)))
4151 operand = *recog_data.operand_loc[i];
4153 while (GET_CODE (operand) == SUBREG)
4154 operand = SUBREG_REG (operand);
4155 if (REG_P (operand))
4157 if (modified[i] != RELOAD_WRITE)
4158 /* We mark the USE with QImode so that we recognize
4159 it as one that can be safely deleted at the end
4160 of reload. */
4161 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, operand),
4162 insn), QImode);
4163 if (modified[i] != RELOAD_READ)
4164 emit_insn_after (gen_clobber (operand), insn);
4168 else if (goal_alternative_matches[i] >= 0
4169 && goal_alternative_win[goal_alternative_matches[i]]
4170 && modified[i] == RELOAD_READ
4171 && modified[goal_alternative_matches[i]] == RELOAD_WRITE
4172 && ! no_input_reloads && ! no_output_reloads
4173 && optimize)
4175 /* Similarly, make an optional reload for a pair of matching
4176 objects that are in MEM or a pseudo that didn't get a hard reg. */
4178 rtx operand = recog_data.operand[i];
4180 while (GET_CODE (operand) == SUBREG)
4181 operand = SUBREG_REG (operand);
4182 if ((MEM_P (operand)
4183 || (REG_P (operand)
4184 && REGNO (operand) >= FIRST_PSEUDO_REGISTER))
4185 && (goal_alternative[goal_alternative_matches[i]] != NO_REGS))
4186 operand_reloadnum[i] = operand_reloadnum[goal_alternative_matches[i]]
4187 = push_reload (recog_data.operand[goal_alternative_matches[i]],
4188 recog_data.operand[i],
4189 recog_data.operand_loc[goal_alternative_matches[i]],
4190 recog_data.operand_loc[i],
4191 (enum reg_class) goal_alternative[goal_alternative_matches[i]],
4192 operand_mode[goal_alternative_matches[i]],
4193 operand_mode[i],
4194 0, 1, goal_alternative_matches[i], RELOAD_OTHER);
4197 /* Perform whatever substitutions on the operands we are supposed
4198 to make due to commutativity or replacement of registers
4199 with equivalent constants or memory slots. */
4201 for (i = 0; i < noperands; i++)
4203 /* We only do this on the last pass through reload, because it is
4204 possible for some data (like reg_equiv_address) to be changed during
4205 later passes. Moreover, we lose the opportunity to get a useful
4206 reload_{in,out}_reg when we do these replacements. */
4208 if (replace)
4210 rtx substitution = substed_operand[i];
4212 *recog_data.operand_loc[i] = substitution;
4214 /* If we're replacing an operand with a LABEL_REF, we need to
4215 make sure that there's a REG_LABEL_OPERAND note attached to
4216 this instruction. */
4217 if (GET_CODE (substitution) == LABEL_REF
4218 && !find_reg_note (insn, REG_LABEL_OPERAND,
4219 LABEL_REF_LABEL (substitution))
4220 /* For a JUMP_P, if it was a branch target it must have
4221 already been recorded as such. */
4222 && (!JUMP_P (insn)
4223 || !label_is_jump_target_p (LABEL_REF_LABEL (substitution),
4224 insn)))
4226 add_reg_note (insn, REG_LABEL_OPERAND,
4227 LABEL_REF_LABEL (substitution));
4228 if (LABEL_P (LABEL_REF_LABEL (substitution)))
4229 ++LABEL_NUSES (LABEL_REF_LABEL (substitution));
4233 else
4234 retval |= (substed_operand[i] != *recog_data.operand_loc[i]);
4237 /* If this insn pattern contains any MATCH_DUP's, make sure that
4238 they will be substituted if the operands they match are substituted.
4239 Also do now any substitutions we already did on the operands.
4241 Don't do this if we aren't making replacements because we might be
4242 propagating things allocated by frame pointer elimination into places
4243 it doesn't expect. */
4245 if (insn_code_number >= 0 && replace)
4246 for (i = insn_data[insn_code_number].n_dups - 1; i >= 0; i--)
4248 int opno = recog_data.dup_num[i];
4249 *recog_data.dup_loc[i] = *recog_data.operand_loc[opno];
4250 dup_replacements (recog_data.dup_loc[i], recog_data.operand_loc[opno]);
4253 #if 0
4254 /* This loses because reloading of prior insns can invalidate the equivalence
4255 (or at least find_equiv_reg isn't smart enough to find it any more),
4256 causing this insn to need more reload regs than it needed before.
4257 It may be too late to make the reload regs available.
4258 Now this optimization is done safely in choose_reload_regs. */
4260 /* For each reload of a reg into some other class of reg,
4261 search for an existing equivalent reg (same value now) in the right class.
4262 We can use it as long as we don't need to change its contents. */
4263 for (i = 0; i < n_reloads; i++)
4264 if (rld[i].reg_rtx == 0
4265 && rld[i].in != 0
4266 && REG_P (rld[i].in)
4267 && rld[i].out == 0)
4269 rld[i].reg_rtx
4270 = find_equiv_reg (rld[i].in, insn, rld[i].rclass, -1,
4271 static_reload_reg_p, 0, rld[i].inmode);
4272 /* Prevent generation of insn to load the value
4273 because the one we found already has the value. */
4274 if (rld[i].reg_rtx)
4275 rld[i].in = rld[i].reg_rtx;
4277 #endif
4279 /* If we detected error and replaced asm instruction by USE, forget about the
4280 reloads. */
4281 if (GET_CODE (PATTERN (insn)) == USE
4282 && CONST_INT_P (XEXP (PATTERN (insn), 0)))
4283 n_reloads = 0;
4285 /* Perhaps an output reload can be combined with another
4286 to reduce needs by one. */
4287 if (!goal_earlyclobber)
4288 combine_reloads ();
4290 /* If we have a pair of reloads for parts of an address, they are reloading
4291 the same object, the operands themselves were not reloaded, and they
4292 are for two operands that are supposed to match, merge the reloads and
4293 change the type of the surviving reload to RELOAD_FOR_OPERAND_ADDRESS. */
4295 for (i = 0; i < n_reloads; i++)
4297 int k;
4299 for (j = i + 1; j < n_reloads; j++)
4300 if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4301 || rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4302 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4303 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4304 && (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
4305 || rld[j].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4306 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4307 || rld[j].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4308 && rtx_equal_p (rld[i].in, rld[j].in)
4309 && (operand_reloadnum[rld[i].opnum] < 0
4310 || rld[operand_reloadnum[rld[i].opnum]].optional)
4311 && (operand_reloadnum[rld[j].opnum] < 0
4312 || rld[operand_reloadnum[rld[j].opnum]].optional)
4313 && (goal_alternative_matches[rld[i].opnum] == rld[j].opnum
4314 || (goal_alternative_matches[rld[j].opnum]
4315 == rld[i].opnum)))
4317 for (k = 0; k < n_replacements; k++)
4318 if (replacements[k].what == j)
4319 replacements[k].what = i;
4321 if (rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4322 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4323 rld[i].when_needed = RELOAD_FOR_OPADDR_ADDR;
4324 else
4325 rld[i].when_needed = RELOAD_FOR_OPERAND_ADDRESS;
4326 rld[j].in = 0;
4330 /* Scan all the reloads and update their type.
4331 If a reload is for the address of an operand and we didn't reload
4332 that operand, change the type. Similarly, change the operand number
4333 of a reload when two operands match. If a reload is optional, treat it
4334 as though the operand isn't reloaded.
4336 ??? This latter case is somewhat odd because if we do the optional
4337 reload, it means the object is hanging around. Thus we need only
4338 do the address reload if the optional reload was NOT done.
4340 Change secondary reloads to be the address type of their operand, not
4341 the normal type.
4343 If an operand's reload is now RELOAD_OTHER, change any
4344 RELOAD_FOR_INPUT_ADDRESS reloads of that operand to
4345 RELOAD_FOR_OTHER_ADDRESS. */
4347 for (i = 0; i < n_reloads; i++)
4349 if (rld[i].secondary_p
4350 && rld[i].when_needed == operand_type[rld[i].opnum])
4351 rld[i].when_needed = address_type[rld[i].opnum];
4353 if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4354 || rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4355 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4356 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4357 && (operand_reloadnum[rld[i].opnum] < 0
4358 || rld[operand_reloadnum[rld[i].opnum]].optional))
4360 /* If we have a secondary reload to go along with this reload,
4361 change its type to RELOAD_FOR_OPADDR_ADDR. */
4363 if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4364 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
4365 && rld[i].secondary_in_reload != -1)
4367 int secondary_in_reload = rld[i].secondary_in_reload;
4369 rld[secondary_in_reload].when_needed = RELOAD_FOR_OPADDR_ADDR;
4371 /* If there's a tertiary reload we have to change it also. */
4372 if (secondary_in_reload > 0
4373 && rld[secondary_in_reload].secondary_in_reload != -1)
4374 rld[rld[secondary_in_reload].secondary_in_reload].when_needed
4375 = RELOAD_FOR_OPADDR_ADDR;
4378 if ((rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4379 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4380 && rld[i].secondary_out_reload != -1)
4382 int secondary_out_reload = rld[i].secondary_out_reload;
4384 rld[secondary_out_reload].when_needed = RELOAD_FOR_OPADDR_ADDR;
4386 /* If there's a tertiary reload we have to change it also. */
4387 if (secondary_out_reload
4388 && rld[secondary_out_reload].secondary_out_reload != -1)
4389 rld[rld[secondary_out_reload].secondary_out_reload].when_needed
4390 = RELOAD_FOR_OPADDR_ADDR;
4393 if (rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4394 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4395 rld[i].when_needed = RELOAD_FOR_OPADDR_ADDR;
4396 else
4397 rld[i].when_needed = RELOAD_FOR_OPERAND_ADDRESS;
4400 if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4401 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
4402 && operand_reloadnum[rld[i].opnum] >= 0
4403 && (rld[operand_reloadnum[rld[i].opnum]].when_needed
4404 == RELOAD_OTHER))
4405 rld[i].when_needed = RELOAD_FOR_OTHER_ADDRESS;
4407 if (goal_alternative_matches[rld[i].opnum] >= 0)
4408 rld[i].opnum = goal_alternative_matches[rld[i].opnum];
4411 /* Scan all the reloads, and check for RELOAD_FOR_OPERAND_ADDRESS reloads.
4412 If we have more than one, then convert all RELOAD_FOR_OPADDR_ADDR
4413 reloads to RELOAD_FOR_OPERAND_ADDRESS reloads.
4415 choose_reload_regs assumes that RELOAD_FOR_OPADDR_ADDR reloads never
4416 conflict with RELOAD_FOR_OPERAND_ADDRESS reloads. This is true for a
4417 single pair of RELOAD_FOR_OPADDR_ADDR/RELOAD_FOR_OPERAND_ADDRESS reloads.
4418 However, if there is more than one RELOAD_FOR_OPERAND_ADDRESS reload,
4419 then a RELOAD_FOR_OPADDR_ADDR reload conflicts with all
4420 RELOAD_FOR_OPERAND_ADDRESS reloads other than the one that uses it.
4421 This is complicated by the fact that a single operand can have more
4422 than one RELOAD_FOR_OPERAND_ADDRESS reload. It is very difficult to fix
4423 choose_reload_regs without affecting code quality, and cases that
4424 actually fail are extremely rare, so it turns out to be better to fix
4425 the problem here by not generating cases that choose_reload_regs will
4426 fail for. */
4427 /* There is a similar problem with RELOAD_FOR_INPUT_ADDRESS /
4428 RELOAD_FOR_OUTPUT_ADDRESS when there is more than one of a kind for
4429 a single operand.
4430 We can reduce the register pressure by exploiting that a
4431 RELOAD_FOR_X_ADDR_ADDR that precedes all RELOAD_FOR_X_ADDRESS reloads
4432 does not conflict with any of them, if it is only used for the first of
4433 the RELOAD_FOR_X_ADDRESS reloads. */
4435 int first_op_addr_num = -2;
4436 int first_inpaddr_num[MAX_RECOG_OPERANDS];
4437 int first_outpaddr_num[MAX_RECOG_OPERANDS];
4438 int need_change = 0;
4439 /* We use last_op_addr_reload and the contents of the above arrays
4440 first as flags - -2 means no instance encountered, -1 means exactly
4441 one instance encountered.
4442 If more than one instance has been encountered, we store the reload
4443 number of the first reload of the kind in question; reload numbers
4444 are known to be non-negative. */
4445 for (i = 0; i < noperands; i++)
4446 first_inpaddr_num[i] = first_outpaddr_num[i] = -2;
4447 for (i = n_reloads - 1; i >= 0; i--)
4449 switch (rld[i].when_needed)
4451 case RELOAD_FOR_OPERAND_ADDRESS:
4452 if (++first_op_addr_num >= 0)
4454 first_op_addr_num = i;
4455 need_change = 1;
4457 break;
4458 case RELOAD_FOR_INPUT_ADDRESS:
4459 if (++first_inpaddr_num[rld[i].opnum] >= 0)
4461 first_inpaddr_num[rld[i].opnum] = i;
4462 need_change = 1;
4464 break;
4465 case RELOAD_FOR_OUTPUT_ADDRESS:
4466 if (++first_outpaddr_num[rld[i].opnum] >= 0)
4468 first_outpaddr_num[rld[i].opnum] = i;
4469 need_change = 1;
4471 break;
4472 default:
4473 break;
4477 if (need_change)
4479 for (i = 0; i < n_reloads; i++)
4481 int first_num;
4482 enum reload_type type;
4484 switch (rld[i].when_needed)
4486 case RELOAD_FOR_OPADDR_ADDR:
4487 first_num = first_op_addr_num;
4488 type = RELOAD_FOR_OPERAND_ADDRESS;
4489 break;
4490 case RELOAD_FOR_INPADDR_ADDRESS:
4491 first_num = first_inpaddr_num[rld[i].opnum];
4492 type = RELOAD_FOR_INPUT_ADDRESS;
4493 break;
4494 case RELOAD_FOR_OUTADDR_ADDRESS:
4495 first_num = first_outpaddr_num[rld[i].opnum];
4496 type = RELOAD_FOR_OUTPUT_ADDRESS;
4497 break;
4498 default:
4499 continue;
4501 if (first_num < 0)
4502 continue;
4503 else if (i > first_num)
4504 rld[i].when_needed = type;
4505 else
4507 /* Check if the only TYPE reload that uses reload I is
4508 reload FIRST_NUM. */
4509 for (j = n_reloads - 1; j > first_num; j--)
4511 if (rld[j].when_needed == type
4512 && (rld[i].secondary_p
4513 ? rld[j].secondary_in_reload == i
4514 : reg_mentioned_p (rld[i].in, rld[j].in)))
4516 rld[i].when_needed = type;
4517 break;
4525 /* See if we have any reloads that are now allowed to be merged
4526 because we've changed when the reload is needed to
4527 RELOAD_FOR_OPERAND_ADDRESS or RELOAD_FOR_OTHER_ADDRESS. Only
4528 check for the most common cases. */
4530 for (i = 0; i < n_reloads; i++)
4531 if (rld[i].in != 0 && rld[i].out == 0
4532 && (rld[i].when_needed == RELOAD_FOR_OPERAND_ADDRESS
4533 || rld[i].when_needed == RELOAD_FOR_OPADDR_ADDR
4534 || rld[i].when_needed == RELOAD_FOR_OTHER_ADDRESS))
4535 for (j = 0; j < n_reloads; j++)
4536 if (i != j && rld[j].in != 0 && rld[j].out == 0
4537 && rld[j].when_needed == rld[i].when_needed
4538 && MATCHES (rld[i].in, rld[j].in)
4539 && rld[i].rclass == rld[j].rclass
4540 && !rld[i].nocombine && !rld[j].nocombine
4541 && rld[i].reg_rtx == rld[j].reg_rtx)
4543 rld[i].opnum = MIN (rld[i].opnum, rld[j].opnum);
4544 transfer_replacements (i, j);
4545 rld[j].in = 0;
4548 /* If we made any reloads for addresses, see if they violate a
4549 "no input reloads" requirement for this insn. But loads that we
4550 do after the insn (such as for output addresses) are fine. */
4551 if (HAVE_cc0 && no_input_reloads)
4552 for (i = 0; i < n_reloads; i++)
4553 gcc_assert (rld[i].in == 0
4554 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS
4555 || rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS);
4557 /* Compute reload_mode and reload_nregs. */
4558 for (i = 0; i < n_reloads; i++)
4560 rld[i].mode
4561 = (rld[i].inmode == VOIDmode
4562 || (GET_MODE_SIZE (rld[i].outmode)
4563 > GET_MODE_SIZE (rld[i].inmode)))
4564 ? rld[i].outmode : rld[i].inmode;
4566 rld[i].nregs = ira_reg_class_max_nregs [rld[i].rclass][rld[i].mode];
4569 /* Special case a simple move with an input reload and a
4570 destination of a hard reg, if the hard reg is ok, use it. */
4571 for (i = 0; i < n_reloads; i++)
4572 if (rld[i].when_needed == RELOAD_FOR_INPUT
4573 && GET_CODE (PATTERN (insn)) == SET
4574 && REG_P (SET_DEST (PATTERN (insn)))
4575 && (SET_SRC (PATTERN (insn)) == rld[i].in
4576 || SET_SRC (PATTERN (insn)) == rld[i].in_reg)
4577 && !elimination_target_reg_p (SET_DEST (PATTERN (insn))))
4579 rtx dest = SET_DEST (PATTERN (insn));
4580 unsigned int regno = REGNO (dest);
4582 if (regno < FIRST_PSEUDO_REGISTER
4583 && TEST_HARD_REG_BIT (reg_class_contents[rld[i].rclass], regno)
4584 && HARD_REGNO_MODE_OK (regno, rld[i].mode))
4586 int nr = hard_regno_nregs[regno][rld[i].mode];
4587 int ok = 1, nri;
4589 for (nri = 1; nri < nr; nri ++)
4590 if (! TEST_HARD_REG_BIT (reg_class_contents[rld[i].rclass], regno + nri))
4592 ok = 0;
4593 break;
4596 if (ok)
4597 rld[i].reg_rtx = dest;
4601 return retval;
4604 /* Return true if alternative number ALTNUM in constraint-string
4605 CONSTRAINT is guaranteed to accept a reloaded constant-pool reference.
4606 MEM gives the reference if it didn't need any reloads, otherwise it
4607 is null. */
4609 static bool
4610 alternative_allows_const_pool_ref (rtx mem ATTRIBUTE_UNUSED,
4611 const char *constraint, int altnum)
4613 int c;
4615 /* Skip alternatives before the one requested. */
4616 while (altnum > 0)
4618 while (*constraint++ != ',')
4620 altnum--;
4622 /* Scan the requested alternative for TARGET_MEM_CONSTRAINT or 'o'.
4623 If one of them is present, this alternative accepts the result of
4624 passing a constant-pool reference through find_reloads_toplev.
4626 The same is true of extra memory constraints if the address
4627 was reloaded into a register. However, the target may elect
4628 to disallow the original constant address, forcing it to be
4629 reloaded into a register instead. */
4630 for (; (c = *constraint) && c != ',' && c != '#';
4631 constraint += CONSTRAINT_LEN (c, constraint))
4633 enum constraint_num cn = lookup_constraint (constraint);
4634 if (insn_extra_memory_constraint (cn)
4635 && (mem == NULL || constraint_satisfied_p (mem, cn)))
4636 return true;
4638 return false;
4641 /* Scan X for memory references and scan the addresses for reloading.
4642 Also checks for references to "constant" regs that we want to eliminate
4643 and replaces them with the values they stand for.
4644 We may alter X destructively if it contains a reference to such.
4645 If X is just a constant reg, we return the equivalent value
4646 instead of X.
4648 IND_LEVELS says how many levels of indirect addressing this machine
4649 supports.
4651 OPNUM and TYPE identify the purpose of the reload.
4653 IS_SET_DEST is true if X is the destination of a SET, which is not
4654 appropriate to be replaced by a constant.
4656 INSN, if nonzero, is the insn in which we do the reload. It is used
4657 to determine if we may generate output reloads, and where to put USEs
4658 for pseudos that we have to replace with stack slots.
4660 ADDRESS_RELOADED. If nonzero, is a pointer to where we put the
4661 result of find_reloads_address. */
4663 static rtx
4664 find_reloads_toplev (rtx x, int opnum, enum reload_type type,
4665 int ind_levels, int is_set_dest, rtx_insn *insn,
4666 int *address_reloaded)
4668 RTX_CODE code = GET_CODE (x);
4670 const char *fmt = GET_RTX_FORMAT (code);
4671 int i;
4672 int copied;
4674 if (code == REG)
4676 /* This code is duplicated for speed in find_reloads. */
4677 int regno = REGNO (x);
4678 if (reg_equiv_constant (regno) != 0 && !is_set_dest)
4679 x = reg_equiv_constant (regno);
4680 #if 0
4681 /* This creates (subreg (mem...)) which would cause an unnecessary
4682 reload of the mem. */
4683 else if (reg_equiv_mem (regno) != 0)
4684 x = reg_equiv_mem (regno);
4685 #endif
4686 else if (reg_equiv_memory_loc (regno)
4687 && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
4689 rtx mem = make_memloc (x, regno);
4690 if (reg_equiv_address (regno)
4691 || ! rtx_equal_p (mem, reg_equiv_mem (regno)))
4693 /* If this is not a toplevel operand, find_reloads doesn't see
4694 this substitution. We have to emit a USE of the pseudo so
4695 that delete_output_reload can see it. */
4696 if (replace_reloads && recog_data.operand[opnum] != x)
4697 /* We mark the USE with QImode so that we recognize it
4698 as one that can be safely deleted at the end of
4699 reload. */
4700 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, x), insn),
4701 QImode);
4702 x = mem;
4703 i = find_reloads_address (GET_MODE (x), &x, XEXP (x, 0), &XEXP (x, 0),
4704 opnum, type, ind_levels, insn);
4705 if (!rtx_equal_p (x, mem))
4706 push_reg_equiv_alt_mem (regno, x);
4707 if (address_reloaded)
4708 *address_reloaded = i;
4711 return x;
4713 if (code == MEM)
4715 rtx tem = x;
4717 i = find_reloads_address (GET_MODE (x), &tem, XEXP (x, 0), &XEXP (x, 0),
4718 opnum, type, ind_levels, insn);
4719 if (address_reloaded)
4720 *address_reloaded = i;
4722 return tem;
4725 if (code == SUBREG && REG_P (SUBREG_REG (x)))
4727 /* Check for SUBREG containing a REG that's equivalent to a
4728 constant. If the constant has a known value, truncate it
4729 right now. Similarly if we are extracting a single-word of a
4730 multi-word constant. If the constant is symbolic, allow it
4731 to be substituted normally. push_reload will strip the
4732 subreg later. The constant must not be VOIDmode, because we
4733 will lose the mode of the register (this should never happen
4734 because one of the cases above should handle it). */
4736 int regno = REGNO (SUBREG_REG (x));
4737 rtx tem;
4739 if (regno >= FIRST_PSEUDO_REGISTER
4740 && reg_renumber[regno] < 0
4741 && reg_equiv_constant (regno) != 0)
4743 tem =
4744 simplify_gen_subreg (GET_MODE (x), reg_equiv_constant (regno),
4745 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
4746 gcc_assert (tem);
4747 if (CONSTANT_P (tem)
4748 && !targetm.legitimate_constant_p (GET_MODE (x), tem))
4750 tem = force_const_mem (GET_MODE (x), tem);
4751 i = find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
4752 &XEXP (tem, 0), opnum, type,
4753 ind_levels, insn);
4754 if (address_reloaded)
4755 *address_reloaded = i;
4757 return tem;
4760 /* If the subreg contains a reg that will be converted to a mem,
4761 attempt to convert the whole subreg to a (narrower or wider)
4762 memory reference instead. If this succeeds, we're done --
4763 otherwise fall through to check whether the inner reg still
4764 needs address reloads anyway. */
4766 if (regno >= FIRST_PSEUDO_REGISTER
4767 && reg_equiv_memory_loc (regno) != 0)
4769 tem = find_reloads_subreg_address (x, opnum, type, ind_levels,
4770 insn, address_reloaded);
4771 if (tem)
4772 return tem;
4776 for (copied = 0, i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4778 if (fmt[i] == 'e')
4780 rtx new_part = find_reloads_toplev (XEXP (x, i), opnum, type,
4781 ind_levels, is_set_dest, insn,
4782 address_reloaded);
4783 /* If we have replaced a reg with it's equivalent memory loc -
4784 that can still be handled here e.g. if it's in a paradoxical
4785 subreg - we must make the change in a copy, rather than using
4786 a destructive change. This way, find_reloads can still elect
4787 not to do the change. */
4788 if (new_part != XEXP (x, i) && ! CONSTANT_P (new_part) && ! copied)
4790 x = shallow_copy_rtx (x);
4791 copied = 1;
4793 XEXP (x, i) = new_part;
4796 return x;
4799 /* Return a mem ref for the memory equivalent of reg REGNO.
4800 This mem ref is not shared with anything. */
4802 static rtx
4803 make_memloc (rtx ad, int regno)
4805 /* We must rerun eliminate_regs, in case the elimination
4806 offsets have changed. */
4807 rtx tem
4808 = XEXP (eliminate_regs (reg_equiv_memory_loc (regno), VOIDmode, NULL_RTX),
4811 /* If TEM might contain a pseudo, we must copy it to avoid
4812 modifying it when we do the substitution for the reload. */
4813 if (rtx_varies_p (tem, 0))
4814 tem = copy_rtx (tem);
4816 tem = replace_equiv_address_nv (reg_equiv_memory_loc (regno), tem);
4817 tem = adjust_address_nv (tem, GET_MODE (ad), 0);
4819 /* Copy the result if it's still the same as the equivalence, to avoid
4820 modifying it when we do the substitution for the reload. */
4821 if (tem == reg_equiv_memory_loc (regno))
4822 tem = copy_rtx (tem);
4823 return tem;
4826 /* Returns true if AD could be turned into a valid memory reference
4827 to mode MODE in address space AS by reloading the part pointed to
4828 by PART into a register. */
4830 static int
4831 maybe_memory_address_addr_space_p (machine_mode mode, rtx ad,
4832 addr_space_t as, rtx *part)
4834 int retv;
4835 rtx tem = *part;
4836 rtx reg = gen_rtx_REG (GET_MODE (tem), max_reg_num ());
4838 *part = reg;
4839 retv = memory_address_addr_space_p (mode, ad, as);
4840 *part = tem;
4842 return retv;
4845 /* Record all reloads needed for handling memory address AD
4846 which appears in *LOC in a memory reference to mode MODE
4847 which itself is found in location *MEMREFLOC.
4848 Note that we take shortcuts assuming that no multi-reg machine mode
4849 occurs as part of an address.
4851 OPNUM and TYPE specify the purpose of this reload.
4853 IND_LEVELS says how many levels of indirect addressing this machine
4854 supports.
4856 INSN, if nonzero, is the insn in which we do the reload. It is used
4857 to determine if we may generate output reloads, and where to put USEs
4858 for pseudos that we have to replace with stack slots.
4860 Value is one if this address is reloaded or replaced as a whole; it is
4861 zero if the top level of this address was not reloaded or replaced, and
4862 it is -1 if it may or may not have been reloaded or replaced.
4864 Note that there is no verification that the address will be valid after
4865 this routine does its work. Instead, we rely on the fact that the address
4866 was valid when reload started. So we need only undo things that reload
4867 could have broken. These are wrong register types, pseudos not allocated
4868 to a hard register, and frame pointer elimination. */
4870 static int
4871 find_reloads_address (machine_mode mode, rtx *memrefloc, rtx ad,
4872 rtx *loc, int opnum, enum reload_type type,
4873 int ind_levels, rtx_insn *insn)
4875 addr_space_t as = memrefloc? MEM_ADDR_SPACE (*memrefloc)
4876 : ADDR_SPACE_GENERIC;
4877 int regno;
4878 int removed_and = 0;
4879 int op_index;
4880 rtx tem;
4882 /* If the address is a register, see if it is a legitimate address and
4883 reload if not. We first handle the cases where we need not reload
4884 or where we must reload in a non-standard way. */
4886 if (REG_P (ad))
4888 regno = REGNO (ad);
4890 if (reg_equiv_constant (regno) != 0)
4892 find_reloads_address_part (reg_equiv_constant (regno), loc,
4893 base_reg_class (mode, as, MEM, SCRATCH),
4894 GET_MODE (ad), opnum, type, ind_levels);
4895 return 1;
4898 tem = reg_equiv_memory_loc (regno);
4899 if (tem != 0)
4901 if (reg_equiv_address (regno) != 0 || num_not_at_initial_offset)
4903 tem = make_memloc (ad, regno);
4904 if (! strict_memory_address_addr_space_p (GET_MODE (tem),
4905 XEXP (tem, 0),
4906 MEM_ADDR_SPACE (tem)))
4908 rtx orig = tem;
4910 find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
4911 &XEXP (tem, 0), opnum,
4912 ADDR_TYPE (type), ind_levels, insn);
4913 if (!rtx_equal_p (tem, orig))
4914 push_reg_equiv_alt_mem (regno, tem);
4916 /* We can avoid a reload if the register's equivalent memory
4917 expression is valid as an indirect memory address.
4918 But not all addresses are valid in a mem used as an indirect
4919 address: only reg or reg+constant. */
4921 if (ind_levels > 0
4922 && strict_memory_address_addr_space_p (mode, tem, as)
4923 && (REG_P (XEXP (tem, 0))
4924 || (GET_CODE (XEXP (tem, 0)) == PLUS
4925 && REG_P (XEXP (XEXP (tem, 0), 0))
4926 && CONSTANT_P (XEXP (XEXP (tem, 0), 1)))))
4928 /* TEM is not the same as what we'll be replacing the
4929 pseudo with after reload, put a USE in front of INSN
4930 in the final reload pass. */
4931 if (replace_reloads
4932 && num_not_at_initial_offset
4933 && ! rtx_equal_p (tem, reg_equiv_mem (regno)))
4935 *loc = tem;
4936 /* We mark the USE with QImode so that we
4937 recognize it as one that can be safely
4938 deleted at the end of reload. */
4939 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, ad),
4940 insn), QImode);
4942 /* This doesn't really count as replacing the address
4943 as a whole, since it is still a memory access. */
4945 return 0;
4947 ad = tem;
4951 /* The only remaining case where we can avoid a reload is if this is a
4952 hard register that is valid as a base register and which is not the
4953 subject of a CLOBBER in this insn. */
4955 else if (regno < FIRST_PSEUDO_REGISTER
4956 && regno_ok_for_base_p (regno, mode, as, MEM, SCRATCH)
4957 && ! regno_clobbered_p (regno, this_insn, mode, 0))
4958 return 0;
4960 /* If we do not have one of the cases above, we must do the reload. */
4961 push_reload (ad, NULL_RTX, loc, (rtx*) 0,
4962 base_reg_class (mode, as, MEM, SCRATCH),
4963 GET_MODE (ad), VOIDmode, 0, 0, opnum, type);
4964 return 1;
4967 if (strict_memory_address_addr_space_p (mode, ad, as))
4969 /* The address appears valid, so reloads are not needed.
4970 But the address may contain an eliminable register.
4971 This can happen because a machine with indirect addressing
4972 may consider a pseudo register by itself a valid address even when
4973 it has failed to get a hard reg.
4974 So do a tree-walk to find and eliminate all such regs. */
4976 /* But first quickly dispose of a common case. */
4977 if (GET_CODE (ad) == PLUS
4978 && CONST_INT_P (XEXP (ad, 1))
4979 && REG_P (XEXP (ad, 0))
4980 && reg_equiv_constant (REGNO (XEXP (ad, 0))) == 0)
4981 return 0;
4983 subst_reg_equivs_changed = 0;
4984 *loc = subst_reg_equivs (ad, insn);
4986 if (! subst_reg_equivs_changed)
4987 return 0;
4989 /* Check result for validity after substitution. */
4990 if (strict_memory_address_addr_space_p (mode, ad, as))
4991 return 0;
4994 #ifdef LEGITIMIZE_RELOAD_ADDRESS
4997 if (memrefloc && ADDR_SPACE_GENERIC_P (as))
4999 LEGITIMIZE_RELOAD_ADDRESS (ad, GET_MODE (*memrefloc), opnum, type,
5000 ind_levels, win);
5002 break;
5003 win:
5004 *memrefloc = copy_rtx (*memrefloc);
5005 XEXP (*memrefloc, 0) = ad;
5006 move_replacements (&ad, &XEXP (*memrefloc, 0));
5007 return -1;
5009 while (0);
5010 #endif
5012 /* The address is not valid. We have to figure out why. First see if
5013 we have an outer AND and remove it if so. Then analyze what's inside. */
5015 if (GET_CODE (ad) == AND)
5017 removed_and = 1;
5018 loc = &XEXP (ad, 0);
5019 ad = *loc;
5022 /* One possibility for why the address is invalid is that it is itself
5023 a MEM. This can happen when the frame pointer is being eliminated, a
5024 pseudo is not allocated to a hard register, and the offset between the
5025 frame and stack pointers is not its initial value. In that case the
5026 pseudo will have been replaced by a MEM referring to the
5027 stack pointer. */
5028 if (MEM_P (ad))
5030 /* First ensure that the address in this MEM is valid. Then, unless
5031 indirect addresses are valid, reload the MEM into a register. */
5032 tem = ad;
5033 find_reloads_address (GET_MODE (ad), &tem, XEXP (ad, 0), &XEXP (ad, 0),
5034 opnum, ADDR_TYPE (type),
5035 ind_levels == 0 ? 0 : ind_levels - 1, insn);
5037 /* If tem was changed, then we must create a new memory reference to
5038 hold it and store it back into memrefloc. */
5039 if (tem != ad && memrefloc)
5041 *memrefloc = copy_rtx (*memrefloc);
5042 copy_replacements (tem, XEXP (*memrefloc, 0));
5043 loc = &XEXP (*memrefloc, 0);
5044 if (removed_and)
5045 loc = &XEXP (*loc, 0);
5048 /* Check similar cases as for indirect addresses as above except
5049 that we can allow pseudos and a MEM since they should have been
5050 taken care of above. */
5052 if (ind_levels == 0
5053 || (GET_CODE (XEXP (tem, 0)) == SYMBOL_REF && ! indirect_symref_ok)
5054 || MEM_P (XEXP (tem, 0))
5055 || ! (REG_P (XEXP (tem, 0))
5056 || (GET_CODE (XEXP (tem, 0)) == PLUS
5057 && REG_P (XEXP (XEXP (tem, 0), 0))
5058 && CONST_INT_P (XEXP (XEXP (tem, 0), 1)))))
5060 /* Must use TEM here, not AD, since it is the one that will
5061 have any subexpressions reloaded, if needed. */
5062 push_reload (tem, NULL_RTX, loc, (rtx*) 0,
5063 base_reg_class (mode, as, MEM, SCRATCH), GET_MODE (tem),
5064 VOIDmode, 0,
5065 0, opnum, type);
5066 return ! removed_and;
5068 else
5069 return 0;
5072 /* If we have address of a stack slot but it's not valid because the
5073 displacement is too large, compute the sum in a register.
5074 Handle all base registers here, not just fp/ap/sp, because on some
5075 targets (namely SH) we can also get too large displacements from
5076 big-endian corrections. */
5077 else if (GET_CODE (ad) == PLUS
5078 && REG_P (XEXP (ad, 0))
5079 && REGNO (XEXP (ad, 0)) < FIRST_PSEUDO_REGISTER
5080 && CONST_INT_P (XEXP (ad, 1))
5081 && (regno_ok_for_base_p (REGNO (XEXP (ad, 0)), mode, as, PLUS,
5082 CONST_INT)
5083 /* Similarly, if we were to reload the base register and the
5084 mem+offset address is still invalid, then we want to reload
5085 the whole address, not just the base register. */
5086 || ! maybe_memory_address_addr_space_p
5087 (mode, ad, as, &(XEXP (ad, 0)))))
5090 /* Unshare the MEM rtx so we can safely alter it. */
5091 if (memrefloc)
5093 *memrefloc = copy_rtx (*memrefloc);
5094 loc = &XEXP (*memrefloc, 0);
5095 if (removed_and)
5096 loc = &XEXP (*loc, 0);
5099 if (double_reg_address_ok
5100 && regno_ok_for_base_p (REGNO (XEXP (ad, 0)), mode, as,
5101 PLUS, CONST_INT))
5103 /* Unshare the sum as well. */
5104 *loc = ad = copy_rtx (ad);
5106 /* Reload the displacement into an index reg.
5107 We assume the frame pointer or arg pointer is a base reg. */
5108 find_reloads_address_part (XEXP (ad, 1), &XEXP (ad, 1),
5109 INDEX_REG_CLASS, GET_MODE (ad), opnum,
5110 type, ind_levels);
5111 return 0;
5113 else
5115 /* If the sum of two regs is not necessarily valid,
5116 reload the sum into a base reg.
5117 That will at least work. */
5118 find_reloads_address_part (ad, loc,
5119 base_reg_class (mode, as, MEM, SCRATCH),
5120 GET_MODE (ad), opnum, type, ind_levels);
5122 return ! removed_and;
5125 /* If we have an indexed stack slot, there are three possible reasons why
5126 it might be invalid: The index might need to be reloaded, the address
5127 might have been made by frame pointer elimination and hence have a
5128 constant out of range, or both reasons might apply.
5130 We can easily check for an index needing reload, but even if that is the
5131 case, we might also have an invalid constant. To avoid making the
5132 conservative assumption and requiring two reloads, we see if this address
5133 is valid when not interpreted strictly. If it is, the only problem is
5134 that the index needs a reload and find_reloads_address_1 will take care
5135 of it.
5137 Handle all base registers here, not just fp/ap/sp, because on some
5138 targets (namely SPARC) we can also get invalid addresses from preventive
5139 subreg big-endian corrections made by find_reloads_toplev. We
5140 can also get expressions involving LO_SUM (rather than PLUS) from
5141 find_reloads_subreg_address.
5143 If we decide to do something, it must be that `double_reg_address_ok'
5144 is true. We generate a reload of the base register + constant and
5145 rework the sum so that the reload register will be added to the index.
5146 This is safe because we know the address isn't shared.
5148 We check for the base register as both the first and second operand of
5149 the innermost PLUS and/or LO_SUM. */
5151 for (op_index = 0; op_index < 2; ++op_index)
5153 rtx operand, addend;
5154 enum rtx_code inner_code;
5156 if (GET_CODE (ad) != PLUS)
5157 continue;
5159 inner_code = GET_CODE (XEXP (ad, 0));
5160 if (!(GET_CODE (ad) == PLUS
5161 && CONST_INT_P (XEXP (ad, 1))
5162 && (inner_code == PLUS || inner_code == LO_SUM)))
5163 continue;
5165 operand = XEXP (XEXP (ad, 0), op_index);
5166 if (!REG_P (operand) || REGNO (operand) >= FIRST_PSEUDO_REGISTER)
5167 continue;
5169 addend = XEXP (XEXP (ad, 0), 1 - op_index);
5171 if ((regno_ok_for_base_p (REGNO (operand), mode, as, inner_code,
5172 GET_CODE (addend))
5173 || operand == frame_pointer_rtx
5174 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
5175 || operand == hard_frame_pointer_rtx
5176 #endif
5177 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
5178 && operand == arg_pointer_rtx)
5179 || operand == stack_pointer_rtx)
5180 && ! maybe_memory_address_addr_space_p
5181 (mode, ad, as, &XEXP (XEXP (ad, 0), 1 - op_index)))
5183 rtx offset_reg;
5184 enum reg_class cls;
5186 offset_reg = plus_constant (GET_MODE (ad), operand,
5187 INTVAL (XEXP (ad, 1)));
5189 /* Form the adjusted address. */
5190 if (GET_CODE (XEXP (ad, 0)) == PLUS)
5191 ad = gen_rtx_PLUS (GET_MODE (ad),
5192 op_index == 0 ? offset_reg : addend,
5193 op_index == 0 ? addend : offset_reg);
5194 else
5195 ad = gen_rtx_LO_SUM (GET_MODE (ad),
5196 op_index == 0 ? offset_reg : addend,
5197 op_index == 0 ? addend : offset_reg);
5198 *loc = ad;
5200 cls = base_reg_class (mode, as, MEM, GET_CODE (addend));
5201 find_reloads_address_part (XEXP (ad, op_index),
5202 &XEXP (ad, op_index), cls,
5203 GET_MODE (ad), opnum, type, ind_levels);
5204 find_reloads_address_1 (mode, as,
5205 XEXP (ad, 1 - op_index), 1, GET_CODE (ad),
5206 GET_CODE (XEXP (ad, op_index)),
5207 &XEXP (ad, 1 - op_index), opnum,
5208 type, 0, insn);
5210 return 0;
5214 /* See if address becomes valid when an eliminable register
5215 in a sum is replaced. */
5217 tem = ad;
5218 if (GET_CODE (ad) == PLUS)
5219 tem = subst_indexed_address (ad);
5220 if (tem != ad && strict_memory_address_addr_space_p (mode, tem, as))
5222 /* Ok, we win that way. Replace any additional eliminable
5223 registers. */
5225 subst_reg_equivs_changed = 0;
5226 tem = subst_reg_equivs (tem, insn);
5228 /* Make sure that didn't make the address invalid again. */
5230 if (! subst_reg_equivs_changed
5231 || strict_memory_address_addr_space_p (mode, tem, as))
5233 *loc = tem;
5234 return 0;
5238 /* If constants aren't valid addresses, reload the constant address
5239 into a register. */
5240 if (CONSTANT_P (ad) && ! strict_memory_address_addr_space_p (mode, ad, as))
5242 machine_mode address_mode = GET_MODE (ad);
5243 if (address_mode == VOIDmode)
5244 address_mode = targetm.addr_space.address_mode (as);
5246 /* If AD is an address in the constant pool, the MEM rtx may be shared.
5247 Unshare it so we can safely alter it. */
5248 if (memrefloc && GET_CODE (ad) == SYMBOL_REF
5249 && CONSTANT_POOL_ADDRESS_P (ad))
5251 *memrefloc = copy_rtx (*memrefloc);
5252 loc = &XEXP (*memrefloc, 0);
5253 if (removed_and)
5254 loc = &XEXP (*loc, 0);
5257 find_reloads_address_part (ad, loc,
5258 base_reg_class (mode, as, MEM, SCRATCH),
5259 address_mode, opnum, type, ind_levels);
5260 return ! removed_and;
5263 return find_reloads_address_1 (mode, as, ad, 0, MEM, SCRATCH, loc,
5264 opnum, type, ind_levels, insn);
5267 /* Find all pseudo regs appearing in AD
5268 that are eliminable in favor of equivalent values
5269 and do not have hard regs; replace them by their equivalents.
5270 INSN, if nonzero, is the insn in which we do the reload. We put USEs in
5271 front of it for pseudos that we have to replace with stack slots. */
5273 static rtx
5274 subst_reg_equivs (rtx ad, rtx_insn *insn)
5276 RTX_CODE code = GET_CODE (ad);
5277 int i;
5278 const char *fmt;
5280 switch (code)
5282 case HIGH:
5283 case CONST:
5284 CASE_CONST_ANY:
5285 case SYMBOL_REF:
5286 case LABEL_REF:
5287 case PC:
5288 case CC0:
5289 return ad;
5291 case REG:
5293 int regno = REGNO (ad);
5295 if (reg_equiv_constant (regno) != 0)
5297 subst_reg_equivs_changed = 1;
5298 return reg_equiv_constant (regno);
5300 if (reg_equiv_memory_loc (regno) && num_not_at_initial_offset)
5302 rtx mem = make_memloc (ad, regno);
5303 if (! rtx_equal_p (mem, reg_equiv_mem (regno)))
5305 subst_reg_equivs_changed = 1;
5306 /* We mark the USE with QImode so that we recognize it
5307 as one that can be safely deleted at the end of
5308 reload. */
5309 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, ad), insn),
5310 QImode);
5311 return mem;
5315 return ad;
5317 case PLUS:
5318 /* Quickly dispose of a common case. */
5319 if (XEXP (ad, 0) == frame_pointer_rtx
5320 && CONST_INT_P (XEXP (ad, 1)))
5321 return ad;
5322 break;
5324 default:
5325 break;
5328 fmt = GET_RTX_FORMAT (code);
5329 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5330 if (fmt[i] == 'e')
5331 XEXP (ad, i) = subst_reg_equivs (XEXP (ad, i), insn);
5332 return ad;
5335 /* Compute the sum of X and Y, making canonicalizations assumed in an
5336 address, namely: sum constant integers, surround the sum of two
5337 constants with a CONST, put the constant as the second operand, and
5338 group the constant on the outermost sum.
5340 This routine assumes both inputs are already in canonical form. */
5343 form_sum (machine_mode mode, rtx x, rtx y)
5345 rtx tem;
5347 gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
5348 gcc_assert (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode);
5350 if (CONST_INT_P (x))
5351 return plus_constant (mode, y, INTVAL (x));
5352 else if (CONST_INT_P (y))
5353 return plus_constant (mode, x, INTVAL (y));
5354 else if (CONSTANT_P (x))
5355 tem = x, x = y, y = tem;
5357 if (GET_CODE (x) == PLUS && CONSTANT_P (XEXP (x, 1)))
5358 return form_sum (mode, XEXP (x, 0), form_sum (mode, XEXP (x, 1), y));
5360 /* Note that if the operands of Y are specified in the opposite
5361 order in the recursive calls below, infinite recursion will occur. */
5362 if (GET_CODE (y) == PLUS && CONSTANT_P (XEXP (y, 1)))
5363 return form_sum (mode, form_sum (mode, x, XEXP (y, 0)), XEXP (y, 1));
5365 /* If both constant, encapsulate sum. Otherwise, just form sum. A
5366 constant will have been placed second. */
5367 if (CONSTANT_P (x) && CONSTANT_P (y))
5369 if (GET_CODE (x) == CONST)
5370 x = XEXP (x, 0);
5371 if (GET_CODE (y) == CONST)
5372 y = XEXP (y, 0);
5374 return gen_rtx_CONST (VOIDmode, gen_rtx_PLUS (mode, x, y));
5377 return gen_rtx_PLUS (mode, x, y);
5380 /* If ADDR is a sum containing a pseudo register that should be
5381 replaced with a constant (from reg_equiv_constant),
5382 return the result of doing so, and also apply the associative
5383 law so that the result is more likely to be a valid address.
5384 (But it is not guaranteed to be one.)
5386 Note that at most one register is replaced, even if more are
5387 replaceable. Also, we try to put the result into a canonical form
5388 so it is more likely to be a valid address.
5390 In all other cases, return ADDR. */
5392 static rtx
5393 subst_indexed_address (rtx addr)
5395 rtx op0 = 0, op1 = 0, op2 = 0;
5396 rtx tem;
5397 int regno;
5399 if (GET_CODE (addr) == PLUS)
5401 /* Try to find a register to replace. */
5402 op0 = XEXP (addr, 0), op1 = XEXP (addr, 1), op2 = 0;
5403 if (REG_P (op0)
5404 && (regno = REGNO (op0)) >= FIRST_PSEUDO_REGISTER
5405 && reg_renumber[regno] < 0
5406 && reg_equiv_constant (regno) != 0)
5407 op0 = reg_equiv_constant (regno);
5408 else if (REG_P (op1)
5409 && (regno = REGNO (op1)) >= FIRST_PSEUDO_REGISTER
5410 && reg_renumber[regno] < 0
5411 && reg_equiv_constant (regno) != 0)
5412 op1 = reg_equiv_constant (regno);
5413 else if (GET_CODE (op0) == PLUS
5414 && (tem = subst_indexed_address (op0)) != op0)
5415 op0 = tem;
5416 else if (GET_CODE (op1) == PLUS
5417 && (tem = subst_indexed_address (op1)) != op1)
5418 op1 = tem;
5419 else
5420 return addr;
5422 /* Pick out up to three things to add. */
5423 if (GET_CODE (op1) == PLUS)
5424 op2 = XEXP (op1, 1), op1 = XEXP (op1, 0);
5425 else if (GET_CODE (op0) == PLUS)
5426 op2 = op1, op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5428 /* Compute the sum. */
5429 if (op2 != 0)
5430 op1 = form_sum (GET_MODE (addr), op1, op2);
5431 if (op1 != 0)
5432 op0 = form_sum (GET_MODE (addr), op0, op1);
5434 return op0;
5436 return addr;
5439 /* Update the REG_INC notes for an insn. It updates all REG_INC
5440 notes for the instruction which refer to REGNO the to refer
5441 to the reload number.
5443 INSN is the insn for which any REG_INC notes need updating.
5445 REGNO is the register number which has been reloaded.
5447 RELOADNUM is the reload number. */
5449 static void
5450 update_auto_inc_notes (rtx_insn *insn ATTRIBUTE_UNUSED, int regno ATTRIBUTE_UNUSED,
5451 int reloadnum ATTRIBUTE_UNUSED)
5453 #ifdef AUTO_INC_DEC
5454 rtx link;
5456 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
5457 if (REG_NOTE_KIND (link) == REG_INC
5458 && (int) REGNO (XEXP (link, 0)) == regno)
5459 push_replacement (&XEXP (link, 0), reloadnum, VOIDmode);
5460 #endif
5463 /* Record the pseudo registers we must reload into hard registers in a
5464 subexpression of a would-be memory address, X referring to a value
5465 in mode MODE. (This function is not called if the address we find
5466 is strictly valid.)
5468 CONTEXT = 1 means we are considering regs as index regs,
5469 = 0 means we are considering them as base regs.
5470 OUTER_CODE is the code of the enclosing RTX, typically a MEM, a PLUS,
5471 or an autoinc code.
5472 If CONTEXT == 0 and OUTER_CODE is a PLUS or LO_SUM, then INDEX_CODE
5473 is the code of the index part of the address. Otherwise, pass SCRATCH
5474 for this argument.
5475 OPNUM and TYPE specify the purpose of any reloads made.
5477 IND_LEVELS says how many levels of indirect addressing are
5478 supported at this point in the address.
5480 INSN, if nonzero, is the insn in which we do the reload. It is used
5481 to determine if we may generate output reloads.
5483 We return nonzero if X, as a whole, is reloaded or replaced. */
5485 /* Note that we take shortcuts assuming that no multi-reg machine mode
5486 occurs as part of an address.
5487 Also, this is not fully machine-customizable; it works for machines
5488 such as VAXen and 68000's and 32000's, but other possible machines
5489 could have addressing modes that this does not handle right.
5490 If you add push_reload calls here, you need to make sure gen_reload
5491 handles those cases gracefully. */
5493 static int
5494 find_reloads_address_1 (machine_mode mode, addr_space_t as,
5495 rtx x, int context,
5496 enum rtx_code outer_code, enum rtx_code index_code,
5497 rtx *loc, int opnum, enum reload_type type,
5498 int ind_levels, rtx_insn *insn)
5500 #define REG_OK_FOR_CONTEXT(CONTEXT, REGNO, MODE, AS, OUTER, INDEX) \
5501 ((CONTEXT) == 0 \
5502 ? regno_ok_for_base_p (REGNO, MODE, AS, OUTER, INDEX) \
5503 : REGNO_OK_FOR_INDEX_P (REGNO))
5505 enum reg_class context_reg_class;
5506 RTX_CODE code = GET_CODE (x);
5507 bool reloaded_inner_of_autoinc = false;
5509 if (context == 1)
5510 context_reg_class = INDEX_REG_CLASS;
5511 else
5512 context_reg_class = base_reg_class (mode, as, outer_code, index_code);
5514 switch (code)
5516 case PLUS:
5518 rtx orig_op0 = XEXP (x, 0);
5519 rtx orig_op1 = XEXP (x, 1);
5520 RTX_CODE code0 = GET_CODE (orig_op0);
5521 RTX_CODE code1 = GET_CODE (orig_op1);
5522 rtx op0 = orig_op0;
5523 rtx op1 = orig_op1;
5525 if (GET_CODE (op0) == SUBREG)
5527 op0 = SUBREG_REG (op0);
5528 code0 = GET_CODE (op0);
5529 if (code0 == REG && REGNO (op0) < FIRST_PSEUDO_REGISTER)
5530 op0 = gen_rtx_REG (word_mode,
5531 (REGNO (op0) +
5532 subreg_regno_offset (REGNO (SUBREG_REG (orig_op0)),
5533 GET_MODE (SUBREG_REG (orig_op0)),
5534 SUBREG_BYTE (orig_op0),
5535 GET_MODE (orig_op0))));
5538 if (GET_CODE (op1) == SUBREG)
5540 op1 = SUBREG_REG (op1);
5541 code1 = GET_CODE (op1);
5542 if (code1 == REG && REGNO (op1) < FIRST_PSEUDO_REGISTER)
5543 /* ??? Why is this given op1's mode and above for
5544 ??? op0 SUBREGs we use word_mode? */
5545 op1 = gen_rtx_REG (GET_MODE (op1),
5546 (REGNO (op1) +
5547 subreg_regno_offset (REGNO (SUBREG_REG (orig_op1)),
5548 GET_MODE (SUBREG_REG (orig_op1)),
5549 SUBREG_BYTE (orig_op1),
5550 GET_MODE (orig_op1))));
5552 /* Plus in the index register may be created only as a result of
5553 register rematerialization for expression like &localvar*4. Reload it.
5554 It may be possible to combine the displacement on the outer level,
5555 but it is probably not worthwhile to do so. */
5556 if (context == 1)
5558 find_reloads_address (GET_MODE (x), loc, XEXP (x, 0), &XEXP (x, 0),
5559 opnum, ADDR_TYPE (type), ind_levels, insn);
5560 push_reload (*loc, NULL_RTX, loc, (rtx*) 0,
5561 context_reg_class,
5562 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5563 return 1;
5566 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
5567 || code0 == ZERO_EXTEND || code1 == MEM)
5569 find_reloads_address_1 (mode, as, orig_op0, 1, PLUS, SCRATCH,
5570 &XEXP (x, 0), opnum, type, ind_levels,
5571 insn);
5572 find_reloads_address_1 (mode, as, orig_op1, 0, PLUS, code0,
5573 &XEXP (x, 1), opnum, type, ind_levels,
5574 insn);
5577 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
5578 || code1 == ZERO_EXTEND || code0 == MEM)
5580 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, code1,
5581 &XEXP (x, 0), opnum, type, ind_levels,
5582 insn);
5583 find_reloads_address_1 (mode, as, orig_op1, 1, PLUS, SCRATCH,
5584 &XEXP (x, 1), opnum, type, ind_levels,
5585 insn);
5588 else if (code0 == CONST_INT || code0 == CONST
5589 || code0 == SYMBOL_REF || code0 == LABEL_REF)
5590 find_reloads_address_1 (mode, as, orig_op1, 0, PLUS, code0,
5591 &XEXP (x, 1), opnum, type, ind_levels,
5592 insn);
5594 else if (code1 == CONST_INT || code1 == CONST
5595 || code1 == SYMBOL_REF || code1 == LABEL_REF)
5596 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, code1,
5597 &XEXP (x, 0), opnum, type, ind_levels,
5598 insn);
5600 else if (code0 == REG && code1 == REG)
5602 if (REGNO_OK_FOR_INDEX_P (REGNO (op1))
5603 && regno_ok_for_base_p (REGNO (op0), mode, as, PLUS, REG))
5604 return 0;
5605 else if (REGNO_OK_FOR_INDEX_P (REGNO (op0))
5606 && regno_ok_for_base_p (REGNO (op1), mode, as, PLUS, REG))
5607 return 0;
5608 else if (regno_ok_for_base_p (REGNO (op0), mode, as, PLUS, REG))
5609 find_reloads_address_1 (mode, as, orig_op1, 1, PLUS, SCRATCH,
5610 &XEXP (x, 1), opnum, type, ind_levels,
5611 insn);
5612 else if (REGNO_OK_FOR_INDEX_P (REGNO (op1)))
5613 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, REG,
5614 &XEXP (x, 0), opnum, type, ind_levels,
5615 insn);
5616 else if (regno_ok_for_base_p (REGNO (op1), mode, as, PLUS, REG))
5617 find_reloads_address_1 (mode, as, orig_op0, 1, PLUS, SCRATCH,
5618 &XEXP (x, 0), opnum, type, ind_levels,
5619 insn);
5620 else if (REGNO_OK_FOR_INDEX_P (REGNO (op0)))
5621 find_reloads_address_1 (mode, as, orig_op1, 0, PLUS, REG,
5622 &XEXP (x, 1), opnum, type, ind_levels,
5623 insn);
5624 else
5626 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, REG,
5627 &XEXP (x, 0), opnum, type, ind_levels,
5628 insn);
5629 find_reloads_address_1 (mode, as, orig_op1, 1, PLUS, SCRATCH,
5630 &XEXP (x, 1), opnum, type, ind_levels,
5631 insn);
5635 else if (code0 == REG)
5637 find_reloads_address_1 (mode, as, orig_op0, 1, PLUS, SCRATCH,
5638 &XEXP (x, 0), opnum, type, ind_levels,
5639 insn);
5640 find_reloads_address_1 (mode, as, orig_op1, 0, PLUS, REG,
5641 &XEXP (x, 1), opnum, type, ind_levels,
5642 insn);
5645 else if (code1 == REG)
5647 find_reloads_address_1 (mode, as, orig_op1, 1, PLUS, SCRATCH,
5648 &XEXP (x, 1), opnum, type, ind_levels,
5649 insn);
5650 find_reloads_address_1 (mode, as, orig_op0, 0, PLUS, REG,
5651 &XEXP (x, 0), opnum, type, ind_levels,
5652 insn);
5656 return 0;
5658 case POST_MODIFY:
5659 case PRE_MODIFY:
5661 rtx op0 = XEXP (x, 0);
5662 rtx op1 = XEXP (x, 1);
5663 enum rtx_code index_code;
5664 int regno;
5665 int reloadnum;
5667 if (GET_CODE (op1) != PLUS && GET_CODE (op1) != MINUS)
5668 return 0;
5670 /* Currently, we only support {PRE,POST}_MODIFY constructs
5671 where a base register is {inc,dec}remented by the contents
5672 of another register or by a constant value. Thus, these
5673 operands must match. */
5674 gcc_assert (op0 == XEXP (op1, 0));
5676 /* Require index register (or constant). Let's just handle the
5677 register case in the meantime... If the target allows
5678 auto-modify by a constant then we could try replacing a pseudo
5679 register with its equivalent constant where applicable.
5681 We also handle the case where the register was eliminated
5682 resulting in a PLUS subexpression.
5684 If we later decide to reload the whole PRE_MODIFY or
5685 POST_MODIFY, inc_for_reload might clobber the reload register
5686 before reading the index. The index register might therefore
5687 need to live longer than a TYPE reload normally would, so be
5688 conservative and class it as RELOAD_OTHER. */
5689 if ((REG_P (XEXP (op1, 1))
5690 && !REGNO_OK_FOR_INDEX_P (REGNO (XEXP (op1, 1))))
5691 || GET_CODE (XEXP (op1, 1)) == PLUS)
5692 find_reloads_address_1 (mode, as, XEXP (op1, 1), 1, code, SCRATCH,
5693 &XEXP (op1, 1), opnum, RELOAD_OTHER,
5694 ind_levels, insn);
5696 gcc_assert (REG_P (XEXP (op1, 0)));
5698 regno = REGNO (XEXP (op1, 0));
5699 index_code = GET_CODE (XEXP (op1, 1));
5701 /* A register that is incremented cannot be constant! */
5702 gcc_assert (regno < FIRST_PSEUDO_REGISTER
5703 || reg_equiv_constant (regno) == 0);
5705 /* Handle a register that is equivalent to a memory location
5706 which cannot be addressed directly. */
5707 if (reg_equiv_memory_loc (regno) != 0
5708 && (reg_equiv_address (regno) != 0
5709 || num_not_at_initial_offset))
5711 rtx tem = make_memloc (XEXP (x, 0), regno);
5713 if (reg_equiv_address (regno)
5714 || ! rtx_equal_p (tem, reg_equiv_mem (regno)))
5716 rtx orig = tem;
5718 /* First reload the memory location's address.
5719 We can't use ADDR_TYPE (type) here, because we need to
5720 write back the value after reading it, hence we actually
5721 need two registers. */
5722 find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
5723 &XEXP (tem, 0), opnum,
5724 RELOAD_OTHER,
5725 ind_levels, insn);
5727 if (!rtx_equal_p (tem, orig))
5728 push_reg_equiv_alt_mem (regno, tem);
5730 /* Then reload the memory location into a base
5731 register. */
5732 reloadnum = push_reload (tem, tem, &XEXP (x, 0),
5733 &XEXP (op1, 0),
5734 base_reg_class (mode, as,
5735 code, index_code),
5736 GET_MODE (x), GET_MODE (x), 0,
5737 0, opnum, RELOAD_OTHER);
5739 update_auto_inc_notes (this_insn, regno, reloadnum);
5740 return 0;
5744 if (reg_renumber[regno] >= 0)
5745 regno = reg_renumber[regno];
5747 /* We require a base register here... */
5748 if (!regno_ok_for_base_p (regno, GET_MODE (x), as, code, index_code))
5750 reloadnum = push_reload (XEXP (op1, 0), XEXP (x, 0),
5751 &XEXP (op1, 0), &XEXP (x, 0),
5752 base_reg_class (mode, as,
5753 code, index_code),
5754 GET_MODE (x), GET_MODE (x), 0, 0,
5755 opnum, RELOAD_OTHER);
5757 update_auto_inc_notes (this_insn, regno, reloadnum);
5758 return 0;
5761 return 0;
5763 case POST_INC:
5764 case POST_DEC:
5765 case PRE_INC:
5766 case PRE_DEC:
5767 if (REG_P (XEXP (x, 0)))
5769 int regno = REGNO (XEXP (x, 0));
5770 int value = 0;
5771 rtx x_orig = x;
5773 /* A register that is incremented cannot be constant! */
5774 gcc_assert (regno < FIRST_PSEUDO_REGISTER
5775 || reg_equiv_constant (regno) == 0);
5777 /* Handle a register that is equivalent to a memory location
5778 which cannot be addressed directly. */
5779 if (reg_equiv_memory_loc (regno) != 0
5780 && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
5782 rtx tem = make_memloc (XEXP (x, 0), regno);
5783 if (reg_equiv_address (regno)
5784 || ! rtx_equal_p (tem, reg_equiv_mem (regno)))
5786 rtx orig = tem;
5788 /* First reload the memory location's address.
5789 We can't use ADDR_TYPE (type) here, because we need to
5790 write back the value after reading it, hence we actually
5791 need two registers. */
5792 find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
5793 &XEXP (tem, 0), opnum, type,
5794 ind_levels, insn);
5795 reloaded_inner_of_autoinc = true;
5796 if (!rtx_equal_p (tem, orig))
5797 push_reg_equiv_alt_mem (regno, tem);
5798 /* Put this inside a new increment-expression. */
5799 x = gen_rtx_fmt_e (GET_CODE (x), GET_MODE (x), tem);
5800 /* Proceed to reload that, as if it contained a register. */
5804 /* If we have a hard register that is ok in this incdec context,
5805 don't make a reload. If the register isn't nice enough for
5806 autoincdec, we can reload it. But, if an autoincrement of a
5807 register that we here verified as playing nice, still outside
5808 isn't "valid", it must be that no autoincrement is "valid".
5809 If that is true and something made an autoincrement anyway,
5810 this must be a special context where one is allowed.
5811 (For example, a "push" instruction.)
5812 We can't improve this address, so leave it alone. */
5814 /* Otherwise, reload the autoincrement into a suitable hard reg
5815 and record how much to increment by. */
5817 if (reg_renumber[regno] >= 0)
5818 regno = reg_renumber[regno];
5819 if (regno >= FIRST_PSEUDO_REGISTER
5820 || !REG_OK_FOR_CONTEXT (context, regno, mode, as, code,
5821 index_code))
5823 int reloadnum;
5825 /* If we can output the register afterwards, do so, this
5826 saves the extra update.
5827 We can do so if we have an INSN - i.e. no JUMP_INSN nor
5828 CALL_INSN - and it does not set CC0.
5829 But don't do this if we cannot directly address the
5830 memory location, since this will make it harder to
5831 reuse address reloads, and increases register pressure.
5832 Also don't do this if we can probably update x directly. */
5833 rtx equiv = (MEM_P (XEXP (x, 0))
5834 ? XEXP (x, 0)
5835 : reg_equiv_mem (regno));
5836 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
5837 if (insn && NONJUMP_INSN_P (insn) && equiv
5838 && memory_operand (equiv, GET_MODE (equiv))
5839 #if HAVE_cc0
5840 && ! sets_cc0_p (PATTERN (insn))
5841 #endif
5842 && ! (icode != CODE_FOR_nothing
5843 && insn_operand_matches (icode, 0, equiv)
5844 && insn_operand_matches (icode, 1, equiv))
5845 /* Using RELOAD_OTHER means we emit this and the reload we
5846 made earlier in the wrong order. */
5847 && !reloaded_inner_of_autoinc)
5849 /* We use the original pseudo for loc, so that
5850 emit_reload_insns() knows which pseudo this
5851 reload refers to and updates the pseudo rtx, not
5852 its equivalent memory location, as well as the
5853 corresponding entry in reg_last_reload_reg. */
5854 loc = &XEXP (x_orig, 0);
5855 x = XEXP (x, 0);
5856 reloadnum
5857 = push_reload (x, x, loc, loc,
5858 context_reg_class,
5859 GET_MODE (x), GET_MODE (x), 0, 0,
5860 opnum, RELOAD_OTHER);
5862 else
5864 reloadnum
5865 = push_reload (x, x, loc, (rtx*) 0,
5866 context_reg_class,
5867 GET_MODE (x), GET_MODE (x), 0, 0,
5868 opnum, type);
5869 rld[reloadnum].inc
5870 = find_inc_amount (PATTERN (this_insn), XEXP (x_orig, 0));
5872 value = 1;
5875 update_auto_inc_notes (this_insn, REGNO (XEXP (x_orig, 0)),
5876 reloadnum);
5878 return value;
5880 return 0;
5882 case TRUNCATE:
5883 case SIGN_EXTEND:
5884 case ZERO_EXTEND:
5885 /* Look for parts to reload in the inner expression and reload them
5886 too, in addition to this operation. Reloading all inner parts in
5887 addition to this one shouldn't be necessary, but at this point,
5888 we don't know if we can possibly omit any part that *can* be
5889 reloaded. Targets that are better off reloading just either part
5890 (or perhaps even a different part of an outer expression), should
5891 define LEGITIMIZE_RELOAD_ADDRESS. */
5892 find_reloads_address_1 (GET_MODE (XEXP (x, 0)), as, XEXP (x, 0),
5893 context, code, SCRATCH, &XEXP (x, 0), opnum,
5894 type, ind_levels, insn);
5895 push_reload (x, NULL_RTX, loc, (rtx*) 0,
5896 context_reg_class,
5897 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5898 return 1;
5900 case MEM:
5901 /* This is probably the result of a substitution, by eliminate_regs, of
5902 an equivalent address for a pseudo that was not allocated to a hard
5903 register. Verify that the specified address is valid and reload it
5904 into a register.
5906 Since we know we are going to reload this item, don't decrement for
5907 the indirection level.
5909 Note that this is actually conservative: it would be slightly more
5910 efficient to use the value of SPILL_INDIRECT_LEVELS from
5911 reload1.c here. */
5913 find_reloads_address (GET_MODE (x), loc, XEXP (x, 0), &XEXP (x, 0),
5914 opnum, ADDR_TYPE (type), ind_levels, insn);
5915 push_reload (*loc, NULL_RTX, loc, (rtx*) 0,
5916 context_reg_class,
5917 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5918 return 1;
5920 case REG:
5922 int regno = REGNO (x);
5924 if (reg_equiv_constant (regno) != 0)
5926 find_reloads_address_part (reg_equiv_constant (regno), loc,
5927 context_reg_class,
5928 GET_MODE (x), opnum, type, ind_levels);
5929 return 1;
5932 #if 0 /* This might screw code in reload1.c to delete prior output-reload
5933 that feeds this insn. */
5934 if (reg_equiv_mem (regno) != 0)
5936 push_reload (reg_equiv_mem (regno), NULL_RTX, loc, (rtx*) 0,
5937 context_reg_class,
5938 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5939 return 1;
5941 #endif
5943 if (reg_equiv_memory_loc (regno)
5944 && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
5946 rtx tem = make_memloc (x, regno);
5947 if (reg_equiv_address (regno) != 0
5948 || ! rtx_equal_p (tem, reg_equiv_mem (regno)))
5950 x = tem;
5951 find_reloads_address (GET_MODE (x), &x, XEXP (x, 0),
5952 &XEXP (x, 0), opnum, ADDR_TYPE (type),
5953 ind_levels, insn);
5954 if (!rtx_equal_p (x, tem))
5955 push_reg_equiv_alt_mem (regno, x);
5959 if (reg_renumber[regno] >= 0)
5960 regno = reg_renumber[regno];
5962 if (regno >= FIRST_PSEUDO_REGISTER
5963 || !REG_OK_FOR_CONTEXT (context, regno, mode, as, outer_code,
5964 index_code))
5966 push_reload (x, NULL_RTX, loc, (rtx*) 0,
5967 context_reg_class,
5968 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5969 return 1;
5972 /* If a register appearing in an address is the subject of a CLOBBER
5973 in this insn, reload it into some other register to be safe.
5974 The CLOBBER is supposed to make the register unavailable
5975 from before this insn to after it. */
5976 if (regno_clobbered_p (regno, this_insn, GET_MODE (x), 0))
5978 push_reload (x, NULL_RTX, loc, (rtx*) 0,
5979 context_reg_class,
5980 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5981 return 1;
5984 return 0;
5986 case SUBREG:
5987 if (REG_P (SUBREG_REG (x)))
5989 /* If this is a SUBREG of a hard register and the resulting register
5990 is of the wrong class, reload the whole SUBREG. This avoids
5991 needless copies if SUBREG_REG is multi-word. */
5992 if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
5994 int regno ATTRIBUTE_UNUSED = subreg_regno (x);
5996 if (!REG_OK_FOR_CONTEXT (context, regno, mode, as, outer_code,
5997 index_code))
5999 push_reload (x, NULL_RTX, loc, (rtx*) 0,
6000 context_reg_class,
6001 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
6002 return 1;
6005 /* If this is a SUBREG of a pseudo-register, and the pseudo-register
6006 is larger than the class size, then reload the whole SUBREG. */
6007 else
6009 enum reg_class rclass = context_reg_class;
6010 if (ira_reg_class_max_nregs [rclass][GET_MODE (SUBREG_REG (x))]
6011 > reg_class_size[(int) rclass])
6013 /* If the inner register will be replaced by a memory
6014 reference, we can do this only if we can replace the
6015 whole subreg by a (narrower) memory reference. If
6016 this is not possible, fall through and reload just
6017 the inner register (including address reloads). */
6018 if (reg_equiv_memory_loc (REGNO (SUBREG_REG (x))) != 0)
6020 rtx tem = find_reloads_subreg_address (x, opnum,
6021 ADDR_TYPE (type),
6022 ind_levels, insn,
6023 NULL);
6024 if (tem)
6026 push_reload (tem, NULL_RTX, loc, (rtx*) 0, rclass,
6027 GET_MODE (tem), VOIDmode, 0, 0,
6028 opnum, type);
6029 return 1;
6032 else
6034 push_reload (x, NULL_RTX, loc, (rtx*) 0, rclass,
6035 GET_MODE (x), VOIDmode, 0, 0, opnum, type);
6036 return 1;
6041 break;
6043 default:
6044 break;
6048 const char *fmt = GET_RTX_FORMAT (code);
6049 int i;
6051 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6053 if (fmt[i] == 'e')
6054 /* Pass SCRATCH for INDEX_CODE, since CODE can never be a PLUS once
6055 we get here. */
6056 find_reloads_address_1 (mode, as, XEXP (x, i), context,
6057 code, SCRATCH, &XEXP (x, i),
6058 opnum, type, ind_levels, insn);
6062 #undef REG_OK_FOR_CONTEXT
6063 return 0;
6066 /* X, which is found at *LOC, is a part of an address that needs to be
6067 reloaded into a register of class RCLASS. If X is a constant, or if
6068 X is a PLUS that contains a constant, check that the constant is a
6069 legitimate operand and that we are supposed to be able to load
6070 it into the register.
6072 If not, force the constant into memory and reload the MEM instead.
6074 MODE is the mode to use, in case X is an integer constant.
6076 OPNUM and TYPE describe the purpose of any reloads made.
6078 IND_LEVELS says how many levels of indirect addressing this machine
6079 supports. */
6081 static void
6082 find_reloads_address_part (rtx x, rtx *loc, enum reg_class rclass,
6083 machine_mode mode, int opnum,
6084 enum reload_type type, int ind_levels)
6086 if (CONSTANT_P (x)
6087 && (!targetm.legitimate_constant_p (mode, x)
6088 || targetm.preferred_reload_class (x, rclass) == NO_REGS))
6090 x = force_const_mem (mode, x);
6091 find_reloads_address (mode, &x, XEXP (x, 0), &XEXP (x, 0),
6092 opnum, type, ind_levels, 0);
6095 else if (GET_CODE (x) == PLUS
6096 && CONSTANT_P (XEXP (x, 1))
6097 && (!targetm.legitimate_constant_p (GET_MODE (x), XEXP (x, 1))
6098 || targetm.preferred_reload_class (XEXP (x, 1), rclass)
6099 == NO_REGS))
6101 rtx tem;
6103 tem = force_const_mem (GET_MODE (x), XEXP (x, 1));
6104 x = gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0), tem);
6105 find_reloads_address (mode, &XEXP (x, 1), XEXP (tem, 0), &XEXP (tem, 0),
6106 opnum, type, ind_levels, 0);
6109 push_reload (x, NULL_RTX, loc, (rtx*) 0, rclass,
6110 mode, VOIDmode, 0, 0, opnum, type);
6113 /* X, a subreg of a pseudo, is a part of an address that needs to be
6114 reloaded, and the pseusdo is equivalent to a memory location.
6116 Attempt to replace the whole subreg by a (possibly narrower or wider)
6117 memory reference. If this is possible, return this new memory
6118 reference, and push all required address reloads. Otherwise,
6119 return NULL.
6121 OPNUM and TYPE identify the purpose of the reload.
6123 IND_LEVELS says how many levels of indirect addressing are
6124 supported at this point in the address.
6126 INSN, if nonzero, is the insn in which we do the reload. It is used
6127 to determine where to put USEs for pseudos that we have to replace with
6128 stack slots. */
6130 static rtx
6131 find_reloads_subreg_address (rtx x, int opnum, enum reload_type type,
6132 int ind_levels, rtx_insn *insn,
6133 int *address_reloaded)
6135 machine_mode outer_mode = GET_MODE (x);
6136 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
6137 int regno = REGNO (SUBREG_REG (x));
6138 int reloaded = 0;
6139 rtx tem, orig;
6140 int offset;
6142 gcc_assert (reg_equiv_memory_loc (regno) != 0);
6144 /* We cannot replace the subreg with a modified memory reference if:
6146 - we have a paradoxical subreg that implicitly acts as a zero or
6147 sign extension operation due to LOAD_EXTEND_OP;
6149 - we have a subreg that is implicitly supposed to act on the full
6150 register due to WORD_REGISTER_OPERATIONS (see also eliminate_regs);
6152 - the address of the equivalent memory location is mode-dependent; or
6154 - we have a paradoxical subreg and the resulting memory is not
6155 sufficiently aligned to allow access in the wider mode.
6157 In addition, we choose not to perform the replacement for *any*
6158 paradoxical subreg, even if it were possible in principle. This
6159 is to avoid generating wider memory references than necessary.
6161 This corresponds to how previous versions of reload used to handle
6162 paradoxical subregs where no address reload was required. */
6164 if (paradoxical_subreg_p (x))
6165 return NULL;
6167 #ifdef WORD_REGISTER_OPERATIONS
6168 if (GET_MODE_SIZE (outer_mode) < GET_MODE_SIZE (inner_mode)
6169 && ((GET_MODE_SIZE (outer_mode) - 1) / UNITS_PER_WORD
6170 == (GET_MODE_SIZE (inner_mode) - 1) / UNITS_PER_WORD))
6171 return NULL;
6172 #endif
6174 /* Since we don't attempt to handle paradoxical subregs, we can just
6175 call into simplify_subreg, which will handle all remaining checks
6176 for us. */
6177 orig = make_memloc (SUBREG_REG (x), regno);
6178 offset = SUBREG_BYTE (x);
6179 tem = simplify_subreg (outer_mode, orig, inner_mode, offset);
6180 if (!tem || !MEM_P (tem))
6181 return NULL;
6183 /* Now push all required address reloads, if any. */
6184 reloaded = find_reloads_address (GET_MODE (tem), &tem,
6185 XEXP (tem, 0), &XEXP (tem, 0),
6186 opnum, type, ind_levels, insn);
6187 /* ??? Do we need to handle nonzero offsets somehow? */
6188 if (!offset && !rtx_equal_p (tem, orig))
6189 push_reg_equiv_alt_mem (regno, tem);
6191 /* For some processors an address may be valid in the original mode but
6192 not in a smaller mode. For example, ARM accepts a scaled index register
6193 in SImode but not in HImode. Note that this is only a problem if the
6194 address in reg_equiv_mem is already invalid in the new mode; other
6195 cases would be fixed by find_reloads_address as usual.
6197 ??? We attempt to handle such cases here by doing an additional reload
6198 of the full address after the usual processing by find_reloads_address.
6199 Note that this may not work in the general case, but it seems to cover
6200 the cases where this situation currently occurs. A more general fix
6201 might be to reload the *value* instead of the address, but this would
6202 not be expected by the callers of this routine as-is.
6204 If find_reloads_address already completed replaced the address, there
6205 is nothing further to do. */
6206 if (reloaded == 0
6207 && reg_equiv_mem (regno) != 0
6208 && !strict_memory_address_addr_space_p
6209 (GET_MODE (x), XEXP (reg_equiv_mem (regno), 0),
6210 MEM_ADDR_SPACE (reg_equiv_mem (regno))))
6212 push_reload (XEXP (tem, 0), NULL_RTX, &XEXP (tem, 0), (rtx*) 0,
6213 base_reg_class (GET_MODE (tem), MEM_ADDR_SPACE (tem),
6214 MEM, SCRATCH),
6215 GET_MODE (XEXP (tem, 0)), VOIDmode, 0, 0, opnum, type);
6216 reloaded = 1;
6219 /* If this is not a toplevel operand, find_reloads doesn't see this
6220 substitution. We have to emit a USE of the pseudo so that
6221 delete_output_reload can see it. */
6222 if (replace_reloads && recog_data.operand[opnum] != x)
6223 /* We mark the USE with QImode so that we recognize it as one that
6224 can be safely deleted at the end of reload. */
6225 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, SUBREG_REG (x)), insn),
6226 QImode);
6228 if (address_reloaded)
6229 *address_reloaded = reloaded;
6231 return tem;
6234 /* Substitute into the current INSN the registers into which we have reloaded
6235 the things that need reloading. The array `replacements'
6236 contains the locations of all pointers that must be changed
6237 and says what to replace them with.
6239 Return the rtx that X translates into; usually X, but modified. */
6241 void
6242 subst_reloads (rtx_insn *insn)
6244 int i;
6246 for (i = 0; i < n_replacements; i++)
6248 struct replacement *r = &replacements[i];
6249 rtx reloadreg = rld[r->what].reg_rtx;
6250 if (reloadreg)
6252 #ifdef DEBUG_RELOAD
6253 /* This checking takes a very long time on some platforms
6254 causing the gcc.c-torture/compile/limits-fnargs.c test
6255 to time out during testing. See PR 31850.
6257 Internal consistency test. Check that we don't modify
6258 anything in the equivalence arrays. Whenever something from
6259 those arrays needs to be reloaded, it must be unshared before
6260 being substituted into; the equivalence must not be modified.
6261 Otherwise, if the equivalence is used after that, it will
6262 have been modified, and the thing substituted (probably a
6263 register) is likely overwritten and not a usable equivalence. */
6264 int check_regno;
6266 for (check_regno = 0; check_regno < max_regno; check_regno++)
6268 #define CHECK_MODF(ARRAY) \
6269 gcc_assert (!(*reg_equivs)[check_regno].ARRAY \
6270 || !loc_mentioned_in_p (r->where, \
6271 (*reg_equivs)[check_regno].ARRAY))
6273 CHECK_MODF (constant);
6274 CHECK_MODF (memory_loc);
6275 CHECK_MODF (address);
6276 CHECK_MODF (mem);
6277 #undef CHECK_MODF
6279 #endif /* DEBUG_RELOAD */
6281 /* If we're replacing a LABEL_REF with a register, there must
6282 already be an indication (to e.g. flow) which label this
6283 register refers to. */
6284 gcc_assert (GET_CODE (*r->where) != LABEL_REF
6285 || !JUMP_P (insn)
6286 || find_reg_note (insn,
6287 REG_LABEL_OPERAND,
6288 XEXP (*r->where, 0))
6289 || label_is_jump_target_p (XEXP (*r->where, 0), insn));
6291 /* Encapsulate RELOADREG so its machine mode matches what
6292 used to be there. Note that gen_lowpart_common will
6293 do the wrong thing if RELOADREG is multi-word. RELOADREG
6294 will always be a REG here. */
6295 if (GET_MODE (reloadreg) != r->mode && r->mode != VOIDmode)
6296 reloadreg = reload_adjust_reg_for_mode (reloadreg, r->mode);
6298 *r->where = reloadreg;
6300 /* If reload got no reg and isn't optional, something's wrong. */
6301 else
6302 gcc_assert (rld[r->what].optional);
6306 /* Make a copy of any replacements being done into X and move those
6307 copies to locations in Y, a copy of X. */
6309 void
6310 copy_replacements (rtx x, rtx y)
6312 copy_replacements_1 (&x, &y, n_replacements);
6315 static void
6316 copy_replacements_1 (rtx *px, rtx *py, int orig_replacements)
6318 int i, j;
6319 rtx x, y;
6320 struct replacement *r;
6321 enum rtx_code code;
6322 const char *fmt;
6324 for (j = 0; j < orig_replacements; j++)
6325 if (replacements[j].where == px)
6327 r = &replacements[n_replacements++];
6328 r->where = py;
6329 r->what = replacements[j].what;
6330 r->mode = replacements[j].mode;
6333 x = *px;
6334 y = *py;
6335 code = GET_CODE (x);
6336 fmt = GET_RTX_FORMAT (code);
6338 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6340 if (fmt[i] == 'e')
6341 copy_replacements_1 (&XEXP (x, i), &XEXP (y, i), orig_replacements);
6342 else if (fmt[i] == 'E')
6343 for (j = XVECLEN (x, i); --j >= 0; )
6344 copy_replacements_1 (&XVECEXP (x, i, j), &XVECEXP (y, i, j),
6345 orig_replacements);
6349 /* Change any replacements being done to *X to be done to *Y. */
6351 void
6352 move_replacements (rtx *x, rtx *y)
6354 int i;
6356 for (i = 0; i < n_replacements; i++)
6357 if (replacements[i].where == x)
6358 replacements[i].where = y;
6361 /* If LOC was scheduled to be replaced by something, return the replacement.
6362 Otherwise, return *LOC. */
6365 find_replacement (rtx *loc)
6367 struct replacement *r;
6369 for (r = &replacements[0]; r < &replacements[n_replacements]; r++)
6371 rtx reloadreg = rld[r->what].reg_rtx;
6373 if (reloadreg && r->where == loc)
6375 if (r->mode != VOIDmode && GET_MODE (reloadreg) != r->mode)
6376 reloadreg = reload_adjust_reg_for_mode (reloadreg, r->mode);
6378 return reloadreg;
6380 else if (reloadreg && GET_CODE (*loc) == SUBREG
6381 && r->where == &SUBREG_REG (*loc))
6383 if (r->mode != VOIDmode && GET_MODE (reloadreg) != r->mode)
6384 reloadreg = reload_adjust_reg_for_mode (reloadreg, r->mode);
6386 return simplify_gen_subreg (GET_MODE (*loc), reloadreg,
6387 GET_MODE (SUBREG_REG (*loc)),
6388 SUBREG_BYTE (*loc));
6392 /* If *LOC is a PLUS, MINUS, or MULT, see if a replacement is scheduled for
6393 what's inside and make a new rtl if so. */
6394 if (GET_CODE (*loc) == PLUS || GET_CODE (*loc) == MINUS
6395 || GET_CODE (*loc) == MULT)
6397 rtx x = find_replacement (&XEXP (*loc, 0));
6398 rtx y = find_replacement (&XEXP (*loc, 1));
6400 if (x != XEXP (*loc, 0) || y != XEXP (*loc, 1))
6401 return gen_rtx_fmt_ee (GET_CODE (*loc), GET_MODE (*loc), x, y);
6404 return *loc;
6407 /* Return nonzero if register in range [REGNO, ENDREGNO)
6408 appears either explicitly or implicitly in X
6409 other than being stored into (except for earlyclobber operands).
6411 References contained within the substructure at LOC do not count.
6412 LOC may be zero, meaning don't ignore anything.
6414 This is similar to refers_to_regno_p in rtlanal.c except that we
6415 look at equivalences for pseudos that didn't get hard registers. */
6417 static int
6418 refers_to_regno_for_reload_p (unsigned int regno, unsigned int endregno,
6419 rtx x, rtx *loc)
6421 int i;
6422 unsigned int r;
6423 RTX_CODE code;
6424 const char *fmt;
6426 if (x == 0)
6427 return 0;
6429 repeat:
6430 code = GET_CODE (x);
6432 switch (code)
6434 case REG:
6435 r = REGNO (x);
6437 /* If this is a pseudo, a hard register must not have been allocated.
6438 X must therefore either be a constant or be in memory. */
6439 if (r >= FIRST_PSEUDO_REGISTER)
6441 if (reg_equiv_memory_loc (r))
6442 return refers_to_regno_for_reload_p (regno, endregno,
6443 reg_equiv_memory_loc (r),
6444 (rtx*) 0);
6446 gcc_assert (reg_equiv_constant (r) || reg_equiv_invariant (r));
6447 return 0;
6450 return (endregno > r
6451 && regno < r + (r < FIRST_PSEUDO_REGISTER
6452 ? hard_regno_nregs[r][GET_MODE (x)]
6453 : 1));
6455 case SUBREG:
6456 /* If this is a SUBREG of a hard reg, we can see exactly which
6457 registers are being modified. Otherwise, handle normally. */
6458 if (REG_P (SUBREG_REG (x))
6459 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
6461 unsigned int inner_regno = subreg_regno (x);
6462 unsigned int inner_endregno
6463 = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
6464 ? subreg_nregs (x) : 1);
6466 return endregno > inner_regno && regno < inner_endregno;
6468 break;
6470 case CLOBBER:
6471 case SET:
6472 if (&SET_DEST (x) != loc
6473 /* Note setting a SUBREG counts as referring to the REG it is in for
6474 a pseudo but not for hard registers since we can
6475 treat each word individually. */
6476 && ((GET_CODE (SET_DEST (x)) == SUBREG
6477 && loc != &SUBREG_REG (SET_DEST (x))
6478 && REG_P (SUBREG_REG (SET_DEST (x)))
6479 && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
6480 && refers_to_regno_for_reload_p (regno, endregno,
6481 SUBREG_REG (SET_DEST (x)),
6482 loc))
6483 /* If the output is an earlyclobber operand, this is
6484 a conflict. */
6485 || ((!REG_P (SET_DEST (x))
6486 || earlyclobber_operand_p (SET_DEST (x)))
6487 && refers_to_regno_for_reload_p (regno, endregno,
6488 SET_DEST (x), loc))))
6489 return 1;
6491 if (code == CLOBBER || loc == &SET_SRC (x))
6492 return 0;
6493 x = SET_SRC (x);
6494 goto repeat;
6496 default:
6497 break;
6500 /* X does not match, so try its subexpressions. */
6502 fmt = GET_RTX_FORMAT (code);
6503 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6505 if (fmt[i] == 'e' && loc != &XEXP (x, i))
6507 if (i == 0)
6509 x = XEXP (x, 0);
6510 goto repeat;
6512 else
6513 if (refers_to_regno_for_reload_p (regno, endregno,
6514 XEXP (x, i), loc))
6515 return 1;
6517 else if (fmt[i] == 'E')
6519 int j;
6520 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6521 if (loc != &XVECEXP (x, i, j)
6522 && refers_to_regno_for_reload_p (regno, endregno,
6523 XVECEXP (x, i, j), loc))
6524 return 1;
6527 return 0;
6530 /* Nonzero if modifying X will affect IN. If X is a register or a SUBREG,
6531 we check if any register number in X conflicts with the relevant register
6532 numbers. If X is a constant, return 0. If X is a MEM, return 1 iff IN
6533 contains a MEM (we don't bother checking for memory addresses that can't
6534 conflict because we expect this to be a rare case.
6536 This function is similar to reg_overlap_mentioned_p in rtlanal.c except
6537 that we look at equivalences for pseudos that didn't get hard registers. */
6540 reg_overlap_mentioned_for_reload_p (rtx x, rtx in)
6542 int regno, endregno;
6544 /* Overly conservative. */
6545 if (GET_CODE (x) == STRICT_LOW_PART
6546 || GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
6547 x = XEXP (x, 0);
6549 /* If either argument is a constant, then modifying X can not affect IN. */
6550 if (CONSTANT_P (x) || CONSTANT_P (in))
6551 return 0;
6552 else if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
6553 return refers_to_mem_for_reload_p (in);
6554 else if (GET_CODE (x) == SUBREG)
6556 regno = REGNO (SUBREG_REG (x));
6557 if (regno < FIRST_PSEUDO_REGISTER)
6558 regno += subreg_regno_offset (REGNO (SUBREG_REG (x)),
6559 GET_MODE (SUBREG_REG (x)),
6560 SUBREG_BYTE (x),
6561 GET_MODE (x));
6562 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
6563 ? subreg_nregs (x) : 1);
6565 return refers_to_regno_for_reload_p (regno, endregno, in, (rtx*) 0);
6567 else if (REG_P (x))
6569 regno = REGNO (x);
6571 /* If this is a pseudo, it must not have been assigned a hard register.
6572 Therefore, it must either be in memory or be a constant. */
6574 if (regno >= FIRST_PSEUDO_REGISTER)
6576 if (reg_equiv_memory_loc (regno))
6577 return refers_to_mem_for_reload_p (in);
6578 gcc_assert (reg_equiv_constant (regno));
6579 return 0;
6582 endregno = END_REGNO (x);
6584 return refers_to_regno_for_reload_p (regno, endregno, in, (rtx*) 0);
6586 else if (MEM_P (x))
6587 return refers_to_mem_for_reload_p (in);
6588 else if (GET_CODE (x) == SCRATCH || GET_CODE (x) == PC
6589 || GET_CODE (x) == CC0)
6590 return reg_mentioned_p (x, in);
6591 else
6593 gcc_assert (GET_CODE (x) == PLUS);
6595 /* We actually want to know if X is mentioned somewhere inside IN.
6596 We must not say that (plus (sp) (const_int 124)) is in
6597 (plus (sp) (const_int 64)), since that can lead to incorrect reload
6598 allocation when spuriously changing a RELOAD_FOR_OUTPUT_ADDRESS
6599 into a RELOAD_OTHER on behalf of another RELOAD_OTHER. */
6600 while (MEM_P (in))
6601 in = XEXP (in, 0);
6602 if (REG_P (in))
6603 return 0;
6604 else if (GET_CODE (in) == PLUS)
6605 return (rtx_equal_p (x, in)
6606 || reg_overlap_mentioned_for_reload_p (x, XEXP (in, 0))
6607 || reg_overlap_mentioned_for_reload_p (x, XEXP (in, 1)));
6608 else return (reg_overlap_mentioned_for_reload_p (XEXP (x, 0), in)
6609 || reg_overlap_mentioned_for_reload_p (XEXP (x, 1), in));
6612 gcc_unreachable ();
6615 /* Return nonzero if anything in X contains a MEM. Look also for pseudo
6616 registers. */
6618 static int
6619 refers_to_mem_for_reload_p (rtx x)
6621 const char *fmt;
6622 int i;
6624 if (MEM_P (x))
6625 return 1;
6627 if (REG_P (x))
6628 return (REGNO (x) >= FIRST_PSEUDO_REGISTER
6629 && reg_equiv_memory_loc (REGNO (x)));
6631 fmt = GET_RTX_FORMAT (GET_CODE (x));
6632 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6633 if (fmt[i] == 'e'
6634 && (MEM_P (XEXP (x, i))
6635 || refers_to_mem_for_reload_p (XEXP (x, i))))
6636 return 1;
6638 return 0;
6641 /* Check the insns before INSN to see if there is a suitable register
6642 containing the same value as GOAL.
6643 If OTHER is -1, look for a register in class RCLASS.
6644 Otherwise, just see if register number OTHER shares GOAL's value.
6646 Return an rtx for the register found, or zero if none is found.
6648 If RELOAD_REG_P is (short *)1,
6649 we reject any hard reg that appears in reload_reg_rtx
6650 because such a hard reg is also needed coming into this insn.
6652 If RELOAD_REG_P is any other nonzero value,
6653 it is a vector indexed by hard reg number
6654 and we reject any hard reg whose element in the vector is nonnegative
6655 as well as any that appears in reload_reg_rtx.
6657 If GOAL is zero, then GOALREG is a register number; we look
6658 for an equivalent for that register.
6660 MODE is the machine mode of the value we want an equivalence for.
6661 If GOAL is nonzero and not VOIDmode, then it must have mode MODE.
6663 This function is used by jump.c as well as in the reload pass.
6665 If GOAL is the sum of the stack pointer and a constant, we treat it
6666 as if it were a constant except that sp is required to be unchanging. */
6669 find_equiv_reg (rtx goal, rtx_insn *insn, enum reg_class rclass, int other,
6670 short *reload_reg_p, int goalreg, machine_mode mode)
6672 rtx_insn *p = insn;
6673 rtx goaltry, valtry, value;
6674 rtx_insn *where;
6675 rtx pat;
6676 int regno = -1;
6677 int valueno;
6678 int goal_mem = 0;
6679 int goal_const = 0;
6680 int goal_mem_addr_varies = 0;
6681 int need_stable_sp = 0;
6682 int nregs;
6683 int valuenregs;
6684 int num = 0;
6686 if (goal == 0)
6687 regno = goalreg;
6688 else if (REG_P (goal))
6689 regno = REGNO (goal);
6690 else if (MEM_P (goal))
6692 enum rtx_code code = GET_CODE (XEXP (goal, 0));
6693 if (MEM_VOLATILE_P (goal))
6694 return 0;
6695 if (flag_float_store && SCALAR_FLOAT_MODE_P (GET_MODE (goal)))
6696 return 0;
6697 /* An address with side effects must be reexecuted. */
6698 switch (code)
6700 case POST_INC:
6701 case PRE_INC:
6702 case POST_DEC:
6703 case PRE_DEC:
6704 case POST_MODIFY:
6705 case PRE_MODIFY:
6706 return 0;
6707 default:
6708 break;
6710 goal_mem = 1;
6712 else if (CONSTANT_P (goal))
6713 goal_const = 1;
6714 else if (GET_CODE (goal) == PLUS
6715 && XEXP (goal, 0) == stack_pointer_rtx
6716 && CONSTANT_P (XEXP (goal, 1)))
6717 goal_const = need_stable_sp = 1;
6718 else if (GET_CODE (goal) == PLUS
6719 && XEXP (goal, 0) == frame_pointer_rtx
6720 && CONSTANT_P (XEXP (goal, 1)))
6721 goal_const = 1;
6722 else
6723 return 0;
6725 num = 0;
6726 /* Scan insns back from INSN, looking for one that copies
6727 a value into or out of GOAL.
6728 Stop and give up if we reach a label. */
6730 while (1)
6732 p = PREV_INSN (p);
6733 if (p && DEBUG_INSN_P (p))
6734 continue;
6735 num++;
6736 if (p == 0 || LABEL_P (p)
6737 || num > PARAM_VALUE (PARAM_MAX_RELOAD_SEARCH_INSNS))
6738 return 0;
6740 /* Don't reuse register contents from before a setjmp-type
6741 function call; on the second return (from the longjmp) it
6742 might have been clobbered by a later reuse. It doesn't
6743 seem worthwhile to actually go and see if it is actually
6744 reused even if that information would be readily available;
6745 just don't reuse it across the setjmp call. */
6746 if (CALL_P (p) && find_reg_note (p, REG_SETJMP, NULL_RTX))
6747 return 0;
6749 if (NONJUMP_INSN_P (p)
6750 /* If we don't want spill regs ... */
6751 && (! (reload_reg_p != 0
6752 && reload_reg_p != (short *) (HOST_WIDE_INT) 1)
6753 /* ... then ignore insns introduced by reload; they aren't
6754 useful and can cause results in reload_as_needed to be
6755 different from what they were when calculating the need for
6756 spills. If we notice an input-reload insn here, we will
6757 reject it below, but it might hide a usable equivalent.
6758 That makes bad code. It may even fail: perhaps no reg was
6759 spilled for this insn because it was assumed we would find
6760 that equivalent. */
6761 || INSN_UID (p) < reload_first_uid))
6763 rtx tem;
6764 pat = single_set (p);
6766 /* First check for something that sets some reg equal to GOAL. */
6767 if (pat != 0
6768 && ((regno >= 0
6769 && true_regnum (SET_SRC (pat)) == regno
6770 && (valueno = true_regnum (valtry = SET_DEST (pat))) >= 0)
6772 (regno >= 0
6773 && true_regnum (SET_DEST (pat)) == regno
6774 && (valueno = true_regnum (valtry = SET_SRC (pat))) >= 0)
6776 (goal_const && rtx_equal_p (SET_SRC (pat), goal)
6777 /* When looking for stack pointer + const,
6778 make sure we don't use a stack adjust. */
6779 && !reg_overlap_mentioned_for_reload_p (SET_DEST (pat), goal)
6780 && (valueno = true_regnum (valtry = SET_DEST (pat))) >= 0)
6781 || (goal_mem
6782 && (valueno = true_regnum (valtry = SET_DEST (pat))) >= 0
6783 && rtx_renumbered_equal_p (goal, SET_SRC (pat)))
6784 || (goal_mem
6785 && (valueno = true_regnum (valtry = SET_SRC (pat))) >= 0
6786 && rtx_renumbered_equal_p (goal, SET_DEST (pat)))
6787 /* If we are looking for a constant,
6788 and something equivalent to that constant was copied
6789 into a reg, we can use that reg. */
6790 || (goal_const && REG_NOTES (p) != 0
6791 && (tem = find_reg_note (p, REG_EQUIV, NULL_RTX))
6792 && ((rtx_equal_p (XEXP (tem, 0), goal)
6793 && (valueno
6794 = true_regnum (valtry = SET_DEST (pat))) >= 0)
6795 || (REG_P (SET_DEST (pat))
6796 && CONST_DOUBLE_AS_FLOAT_P (XEXP (tem, 0))
6797 && SCALAR_FLOAT_MODE_P (GET_MODE (XEXP (tem, 0)))
6798 && CONST_INT_P (goal)
6799 && 0 != (goaltry
6800 = operand_subword (XEXP (tem, 0), 0, 0,
6801 VOIDmode))
6802 && rtx_equal_p (goal, goaltry)
6803 && (valtry
6804 = operand_subword (SET_DEST (pat), 0, 0,
6805 VOIDmode))
6806 && (valueno = true_regnum (valtry)) >= 0)))
6807 || (goal_const && (tem = find_reg_note (p, REG_EQUIV,
6808 NULL_RTX))
6809 && REG_P (SET_DEST (pat))
6810 && CONST_DOUBLE_AS_FLOAT_P (XEXP (tem, 0))
6811 && SCALAR_FLOAT_MODE_P (GET_MODE (XEXP (tem, 0)))
6812 && CONST_INT_P (goal)
6813 && 0 != (goaltry = operand_subword (XEXP (tem, 0), 1, 0,
6814 VOIDmode))
6815 && rtx_equal_p (goal, goaltry)
6816 && (valtry
6817 = operand_subword (SET_DEST (pat), 1, 0, VOIDmode))
6818 && (valueno = true_regnum (valtry)) >= 0)))
6820 if (other >= 0)
6822 if (valueno != other)
6823 continue;
6825 else if ((unsigned) valueno >= FIRST_PSEUDO_REGISTER)
6826 continue;
6827 else if (!in_hard_reg_set_p (reg_class_contents[(int) rclass],
6828 mode, valueno))
6829 continue;
6830 value = valtry;
6831 where = p;
6832 break;
6837 /* We found a previous insn copying GOAL into a suitable other reg VALUE
6838 (or copying VALUE into GOAL, if GOAL is also a register).
6839 Now verify that VALUE is really valid. */
6841 /* VALUENO is the register number of VALUE; a hard register. */
6843 /* Don't try to re-use something that is killed in this insn. We want
6844 to be able to trust REG_UNUSED notes. */
6845 if (REG_NOTES (where) != 0 && find_reg_note (where, REG_UNUSED, value))
6846 return 0;
6848 /* If we propose to get the value from the stack pointer or if GOAL is
6849 a MEM based on the stack pointer, we need a stable SP. */
6850 if (valueno == STACK_POINTER_REGNUM || regno == STACK_POINTER_REGNUM
6851 || (goal_mem && reg_overlap_mentioned_for_reload_p (stack_pointer_rtx,
6852 goal)))
6853 need_stable_sp = 1;
6855 /* Reject VALUE if the copy-insn moved the wrong sort of datum. */
6856 if (GET_MODE (value) != mode)
6857 return 0;
6859 /* Reject VALUE if it was loaded from GOAL
6860 and is also a register that appears in the address of GOAL. */
6862 if (goal_mem && value == SET_DEST (single_set (where))
6863 && refers_to_regno_for_reload_p (valueno, end_hard_regno (mode, valueno),
6864 goal, (rtx*) 0))
6865 return 0;
6867 /* Reject registers that overlap GOAL. */
6869 if (regno >= 0 && regno < FIRST_PSEUDO_REGISTER)
6870 nregs = hard_regno_nregs[regno][mode];
6871 else
6872 nregs = 1;
6873 valuenregs = hard_regno_nregs[valueno][mode];
6875 if (!goal_mem && !goal_const
6876 && regno + nregs > valueno && regno < valueno + valuenregs)
6877 return 0;
6879 /* Reject VALUE if it is one of the regs reserved for reloads.
6880 Reload1 knows how to reuse them anyway, and it would get
6881 confused if we allocated one without its knowledge.
6882 (Now that insns introduced by reload are ignored above,
6883 this case shouldn't happen, but I'm not positive.) */
6885 if (reload_reg_p != 0 && reload_reg_p != (short *) (HOST_WIDE_INT) 1)
6887 int i;
6888 for (i = 0; i < valuenregs; ++i)
6889 if (reload_reg_p[valueno + i] >= 0)
6890 return 0;
6893 /* Reject VALUE if it is a register being used for an input reload
6894 even if it is not one of those reserved. */
6896 if (reload_reg_p != 0)
6898 int i;
6899 for (i = 0; i < n_reloads; i++)
6900 if (rld[i].reg_rtx != 0 && rld[i].in)
6902 int regno1 = REGNO (rld[i].reg_rtx);
6903 int nregs1 = hard_regno_nregs[regno1]
6904 [GET_MODE (rld[i].reg_rtx)];
6905 if (regno1 < valueno + valuenregs
6906 && regno1 + nregs1 > valueno)
6907 return 0;
6911 if (goal_mem)
6912 /* We must treat frame pointer as varying here,
6913 since it can vary--in a nonlocal goto as generated by expand_goto. */
6914 goal_mem_addr_varies = !CONSTANT_ADDRESS_P (XEXP (goal, 0));
6916 /* Now verify that the values of GOAL and VALUE remain unaltered
6917 until INSN is reached. */
6919 p = insn;
6920 while (1)
6922 p = PREV_INSN (p);
6923 if (p == where)
6924 return value;
6926 /* Don't trust the conversion past a function call
6927 if either of the two is in a call-clobbered register, or memory. */
6928 if (CALL_P (p))
6930 int i;
6932 if (goal_mem || need_stable_sp)
6933 return 0;
6935 if (regno >= 0 && regno < FIRST_PSEUDO_REGISTER)
6936 for (i = 0; i < nregs; ++i)
6937 if (call_used_regs[regno + i]
6938 || HARD_REGNO_CALL_PART_CLOBBERED (regno + i, mode))
6939 return 0;
6941 if (valueno >= 0 && valueno < FIRST_PSEUDO_REGISTER)
6942 for (i = 0; i < valuenregs; ++i)
6943 if (call_used_regs[valueno + i]
6944 || HARD_REGNO_CALL_PART_CLOBBERED (valueno + i, mode))
6945 return 0;
6948 if (INSN_P (p))
6950 pat = PATTERN (p);
6952 /* Watch out for unspec_volatile, and volatile asms. */
6953 if (volatile_insn_p (pat))
6954 return 0;
6956 /* If this insn P stores in either GOAL or VALUE, return 0.
6957 If GOAL is a memory ref and this insn writes memory, return 0.
6958 If GOAL is a memory ref and its address is not constant,
6959 and this insn P changes a register used in GOAL, return 0. */
6961 if (GET_CODE (pat) == COND_EXEC)
6962 pat = COND_EXEC_CODE (pat);
6963 if (GET_CODE (pat) == SET || GET_CODE (pat) == CLOBBER)
6965 rtx dest = SET_DEST (pat);
6966 while (GET_CODE (dest) == SUBREG
6967 || GET_CODE (dest) == ZERO_EXTRACT
6968 || GET_CODE (dest) == STRICT_LOW_PART)
6969 dest = XEXP (dest, 0);
6970 if (REG_P (dest))
6972 int xregno = REGNO (dest);
6973 int xnregs;
6974 if (REGNO (dest) < FIRST_PSEUDO_REGISTER)
6975 xnregs = hard_regno_nregs[xregno][GET_MODE (dest)];
6976 else
6977 xnregs = 1;
6978 if (xregno < regno + nregs && xregno + xnregs > regno)
6979 return 0;
6980 if (xregno < valueno + valuenregs
6981 && xregno + xnregs > valueno)
6982 return 0;
6983 if (goal_mem_addr_varies
6984 && reg_overlap_mentioned_for_reload_p (dest, goal))
6985 return 0;
6986 if (xregno == STACK_POINTER_REGNUM && need_stable_sp)
6987 return 0;
6989 else if (goal_mem && MEM_P (dest)
6990 && ! push_operand (dest, GET_MODE (dest)))
6991 return 0;
6992 else if (MEM_P (dest) && regno >= FIRST_PSEUDO_REGISTER
6993 && reg_equiv_memory_loc (regno) != 0)
6994 return 0;
6995 else if (need_stable_sp && push_operand (dest, GET_MODE (dest)))
6996 return 0;
6998 else if (GET_CODE (pat) == PARALLEL)
7000 int i;
7001 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
7003 rtx v1 = XVECEXP (pat, 0, i);
7004 if (GET_CODE (v1) == COND_EXEC)
7005 v1 = COND_EXEC_CODE (v1);
7006 if (GET_CODE (v1) == SET || GET_CODE (v1) == CLOBBER)
7008 rtx dest = SET_DEST (v1);
7009 while (GET_CODE (dest) == SUBREG
7010 || GET_CODE (dest) == ZERO_EXTRACT
7011 || GET_CODE (dest) == STRICT_LOW_PART)
7012 dest = XEXP (dest, 0);
7013 if (REG_P (dest))
7015 int xregno = REGNO (dest);
7016 int xnregs;
7017 if (REGNO (dest) < FIRST_PSEUDO_REGISTER)
7018 xnregs = hard_regno_nregs[xregno][GET_MODE (dest)];
7019 else
7020 xnregs = 1;
7021 if (xregno < regno + nregs
7022 && xregno + xnregs > regno)
7023 return 0;
7024 if (xregno < valueno + valuenregs
7025 && xregno + xnregs > valueno)
7026 return 0;
7027 if (goal_mem_addr_varies
7028 && reg_overlap_mentioned_for_reload_p (dest,
7029 goal))
7030 return 0;
7031 if (xregno == STACK_POINTER_REGNUM && need_stable_sp)
7032 return 0;
7034 else if (goal_mem && MEM_P (dest)
7035 && ! push_operand (dest, GET_MODE (dest)))
7036 return 0;
7037 else if (MEM_P (dest) && regno >= FIRST_PSEUDO_REGISTER
7038 && reg_equiv_memory_loc (regno) != 0)
7039 return 0;
7040 else if (need_stable_sp
7041 && push_operand (dest, GET_MODE (dest)))
7042 return 0;
7047 if (CALL_P (p) && CALL_INSN_FUNCTION_USAGE (p))
7049 rtx link;
7051 for (link = CALL_INSN_FUNCTION_USAGE (p); XEXP (link, 1) != 0;
7052 link = XEXP (link, 1))
7054 pat = XEXP (link, 0);
7055 if (GET_CODE (pat) == CLOBBER)
7057 rtx dest = SET_DEST (pat);
7059 if (REG_P (dest))
7061 int xregno = REGNO (dest);
7062 int xnregs
7063 = hard_regno_nregs[xregno][GET_MODE (dest)];
7065 if (xregno < regno + nregs
7066 && xregno + xnregs > regno)
7067 return 0;
7068 else if (xregno < valueno + valuenregs
7069 && xregno + xnregs > valueno)
7070 return 0;
7071 else if (goal_mem_addr_varies
7072 && reg_overlap_mentioned_for_reload_p (dest,
7073 goal))
7074 return 0;
7077 else if (goal_mem && MEM_P (dest)
7078 && ! push_operand (dest, GET_MODE (dest)))
7079 return 0;
7080 else if (need_stable_sp
7081 && push_operand (dest, GET_MODE (dest)))
7082 return 0;
7087 #ifdef AUTO_INC_DEC
7088 /* If this insn auto-increments or auto-decrements
7089 either regno or valueno, return 0 now.
7090 If GOAL is a memory ref and its address is not constant,
7091 and this insn P increments a register used in GOAL, return 0. */
7093 rtx link;
7095 for (link = REG_NOTES (p); link; link = XEXP (link, 1))
7096 if (REG_NOTE_KIND (link) == REG_INC
7097 && REG_P (XEXP (link, 0)))
7099 int incno = REGNO (XEXP (link, 0));
7100 if (incno < regno + nregs && incno >= regno)
7101 return 0;
7102 if (incno < valueno + valuenregs && incno >= valueno)
7103 return 0;
7104 if (goal_mem_addr_varies
7105 && reg_overlap_mentioned_for_reload_p (XEXP (link, 0),
7106 goal))
7107 return 0;
7110 #endif
7115 /* Find a place where INCED appears in an increment or decrement operator
7116 within X, and return the amount INCED is incremented or decremented by.
7117 The value is always positive. */
7119 static int
7120 find_inc_amount (rtx x, rtx inced)
7122 enum rtx_code code = GET_CODE (x);
7123 const char *fmt;
7124 int i;
7126 if (code == MEM)
7128 rtx addr = XEXP (x, 0);
7129 if ((GET_CODE (addr) == PRE_DEC
7130 || GET_CODE (addr) == POST_DEC
7131 || GET_CODE (addr) == PRE_INC
7132 || GET_CODE (addr) == POST_INC)
7133 && XEXP (addr, 0) == inced)
7134 return GET_MODE_SIZE (GET_MODE (x));
7135 else if ((GET_CODE (addr) == PRE_MODIFY
7136 || GET_CODE (addr) == POST_MODIFY)
7137 && GET_CODE (XEXP (addr, 1)) == PLUS
7138 && XEXP (addr, 0) == XEXP (XEXP (addr, 1), 0)
7139 && XEXP (addr, 0) == inced
7140 && CONST_INT_P (XEXP (XEXP (addr, 1), 1)))
7142 i = INTVAL (XEXP (XEXP (addr, 1), 1));
7143 return i < 0 ? -i : i;
7147 fmt = GET_RTX_FORMAT (code);
7148 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7150 if (fmt[i] == 'e')
7152 int tem = find_inc_amount (XEXP (x, i), inced);
7153 if (tem != 0)
7154 return tem;
7156 if (fmt[i] == 'E')
7158 int j;
7159 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7161 int tem = find_inc_amount (XVECEXP (x, i, j), inced);
7162 if (tem != 0)
7163 return tem;
7168 return 0;
7171 /* Return 1 if registers from REGNO to ENDREGNO are the subjects of a
7172 REG_INC note in insn INSN. REGNO must refer to a hard register. */
7174 #ifdef AUTO_INC_DEC
7175 static int
7176 reg_inc_found_and_valid_p (unsigned int regno, unsigned int endregno,
7177 rtx insn)
7179 rtx link;
7181 gcc_assert (insn);
7183 if (! INSN_P (insn))
7184 return 0;
7186 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
7187 if (REG_NOTE_KIND (link) == REG_INC)
7189 unsigned int test = (int) REGNO (XEXP (link, 0));
7190 if (test >= regno && test < endregno)
7191 return 1;
7193 return 0;
7195 #else
7197 #define reg_inc_found_and_valid_p(regno,endregno,insn) 0
7199 #endif
7201 /* Return 1 if register REGNO is the subject of a clobber in insn INSN.
7202 If SETS is 1, also consider SETs. If SETS is 2, enable checking
7203 REG_INC. REGNO must refer to a hard register. */
7206 regno_clobbered_p (unsigned int regno, rtx_insn *insn, machine_mode mode,
7207 int sets)
7209 unsigned int nregs, endregno;
7211 /* regno must be a hard register. */
7212 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
7214 nregs = hard_regno_nregs[regno][mode];
7215 endregno = regno + nregs;
7217 if ((GET_CODE (PATTERN (insn)) == CLOBBER
7218 || (sets == 1 && GET_CODE (PATTERN (insn)) == SET))
7219 && REG_P (XEXP (PATTERN (insn), 0)))
7221 unsigned int test = REGNO (XEXP (PATTERN (insn), 0));
7223 return test >= regno && test < endregno;
7226 if (sets == 2 && reg_inc_found_and_valid_p (regno, endregno, insn))
7227 return 1;
7229 if (GET_CODE (PATTERN (insn)) == PARALLEL)
7231 int i = XVECLEN (PATTERN (insn), 0) - 1;
7233 for (; i >= 0; i--)
7235 rtx elt = XVECEXP (PATTERN (insn), 0, i);
7236 if ((GET_CODE (elt) == CLOBBER
7237 || (sets == 1 && GET_CODE (elt) == SET))
7238 && REG_P (XEXP (elt, 0)))
7240 unsigned int test = REGNO (XEXP (elt, 0));
7242 if (test >= regno && test < endregno)
7243 return 1;
7245 if (sets == 2
7246 && reg_inc_found_and_valid_p (regno, endregno, elt))
7247 return 1;
7251 return 0;
7254 /* Find the low part, with mode MODE, of a hard regno RELOADREG. */
7256 reload_adjust_reg_for_mode (rtx reloadreg, machine_mode mode)
7258 int regno;
7260 if (GET_MODE (reloadreg) == mode)
7261 return reloadreg;
7263 regno = REGNO (reloadreg);
7265 if (REG_WORDS_BIG_ENDIAN)
7266 regno += (int) hard_regno_nregs[regno][GET_MODE (reloadreg)]
7267 - (int) hard_regno_nregs[regno][mode];
7269 return gen_rtx_REG (mode, regno);
7272 static const char *const reload_when_needed_name[] =
7274 "RELOAD_FOR_INPUT",
7275 "RELOAD_FOR_OUTPUT",
7276 "RELOAD_FOR_INSN",
7277 "RELOAD_FOR_INPUT_ADDRESS",
7278 "RELOAD_FOR_INPADDR_ADDRESS",
7279 "RELOAD_FOR_OUTPUT_ADDRESS",
7280 "RELOAD_FOR_OUTADDR_ADDRESS",
7281 "RELOAD_FOR_OPERAND_ADDRESS",
7282 "RELOAD_FOR_OPADDR_ADDR",
7283 "RELOAD_OTHER",
7284 "RELOAD_FOR_OTHER_ADDRESS"
7287 /* These functions are used to print the variables set by 'find_reloads' */
7289 DEBUG_FUNCTION void
7290 debug_reload_to_stream (FILE *f)
7292 int r;
7293 const char *prefix;
7295 if (! f)
7296 f = stderr;
7297 for (r = 0; r < n_reloads; r++)
7299 fprintf (f, "Reload %d: ", r);
7301 if (rld[r].in != 0)
7303 fprintf (f, "reload_in (%s) = ",
7304 GET_MODE_NAME (rld[r].inmode));
7305 print_inline_rtx (f, rld[r].in, 24);
7306 fprintf (f, "\n\t");
7309 if (rld[r].out != 0)
7311 fprintf (f, "reload_out (%s) = ",
7312 GET_MODE_NAME (rld[r].outmode));
7313 print_inline_rtx (f, rld[r].out, 24);
7314 fprintf (f, "\n\t");
7317 fprintf (f, "%s, ", reg_class_names[(int) rld[r].rclass]);
7319 fprintf (f, "%s (opnum = %d)",
7320 reload_when_needed_name[(int) rld[r].when_needed],
7321 rld[r].opnum);
7323 if (rld[r].optional)
7324 fprintf (f, ", optional");
7326 if (rld[r].nongroup)
7327 fprintf (f, ", nongroup");
7329 if (rld[r].inc != 0)
7330 fprintf (f, ", inc by %d", rld[r].inc);
7332 if (rld[r].nocombine)
7333 fprintf (f, ", can't combine");
7335 if (rld[r].secondary_p)
7336 fprintf (f, ", secondary_reload_p");
7338 if (rld[r].in_reg != 0)
7340 fprintf (f, "\n\treload_in_reg: ");
7341 print_inline_rtx (f, rld[r].in_reg, 24);
7344 if (rld[r].out_reg != 0)
7346 fprintf (f, "\n\treload_out_reg: ");
7347 print_inline_rtx (f, rld[r].out_reg, 24);
7350 if (rld[r].reg_rtx != 0)
7352 fprintf (f, "\n\treload_reg_rtx: ");
7353 print_inline_rtx (f, rld[r].reg_rtx, 24);
7356 prefix = "\n\t";
7357 if (rld[r].secondary_in_reload != -1)
7359 fprintf (f, "%ssecondary_in_reload = %d",
7360 prefix, rld[r].secondary_in_reload);
7361 prefix = ", ";
7364 if (rld[r].secondary_out_reload != -1)
7365 fprintf (f, "%ssecondary_out_reload = %d\n",
7366 prefix, rld[r].secondary_out_reload);
7368 prefix = "\n\t";
7369 if (rld[r].secondary_in_icode != CODE_FOR_nothing)
7371 fprintf (f, "%ssecondary_in_icode = %s", prefix,
7372 insn_data[rld[r].secondary_in_icode].name);
7373 prefix = ", ";
7376 if (rld[r].secondary_out_icode != CODE_FOR_nothing)
7377 fprintf (f, "%ssecondary_out_icode = %s", prefix,
7378 insn_data[rld[r].secondary_out_icode].name);
7380 fprintf (f, "\n");
7384 DEBUG_FUNCTION void
7385 debug_reload (void)
7387 debug_reload_to_stream (stderr);