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