* Make-lang.in (DEMANGLER_INSTALL_NAME, DEMANGLER_CROSS_NAME): New
[official-gcc.git] / gcc / reload1.c
blobed29d25e85e20e38d9d2b3cab841be727b529653
1 /* Reload pseudo regs into hard regs for insns that require hard regs.
2 Copyright (C) 1987, 88, 89, 92-7 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include <stdio.h>
24 #include "rtl.h"
25 #include "obstack.h"
26 #include "insn-config.h"
27 #include "insn-flags.h"
28 #include "insn-codes.h"
29 #include "flags.h"
30 #include "expr.h"
31 #include "regs.h"
32 #include "hard-reg-set.h"
33 #include "reload.h"
34 #include "recog.h"
35 #include "basic-block.h"
36 #include "output.h"
37 #include "real.h"
39 /* This file contains the reload pass of the compiler, which is
40 run after register allocation has been done. It checks that
41 each insn is valid (operands required to be in registers really
42 are in registers of the proper class) and fixes up invalid ones
43 by copying values temporarily into registers for the insns
44 that need them.
46 The results of register allocation are described by the vector
47 reg_renumber; the insns still contain pseudo regs, but reg_renumber
48 can be used to find which hard reg, if any, a pseudo reg is in.
50 The technique we always use is to free up a few hard regs that are
51 called ``reload regs'', and for each place where a pseudo reg
52 must be in a hard reg, copy it temporarily into one of the reload regs.
54 All the pseudos that were formerly allocated to the hard regs that
55 are now in use as reload regs must be ``spilled''. This means
56 that they go to other hard regs, or to stack slots if no other
57 available hard regs can be found. Spilling can invalidate more
58 insns, requiring additional need for reloads, so we must keep checking
59 until the process stabilizes.
61 For machines with different classes of registers, we must keep track
62 of the register class needed for each reload, and make sure that
63 we allocate enough reload registers of each class.
65 The file reload.c contains the code that checks one insn for
66 validity and reports the reloads that it needs. This file
67 is in charge of scanning the entire rtl code, accumulating the
68 reload needs, spilling, assigning reload registers to use for
69 fixing up each insn, and generating the new insns to copy values
70 into the reload registers. */
73 #ifndef REGISTER_MOVE_COST
74 #define REGISTER_MOVE_COST(x, y) 2
75 #endif
77 #ifndef MEMORY_MOVE_COST
78 #define MEMORY_MOVE_COST(x) 4
79 #endif
81 /* During reload_as_needed, element N contains a REG rtx for the hard reg
82 into which reg N has been reloaded (perhaps for a previous insn). */
83 static rtx *reg_last_reload_reg;
85 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
86 for an output reload that stores into reg N. */
87 static char *reg_has_output_reload;
89 /* Indicates which hard regs are reload-registers for an output reload
90 in the current insn. */
91 static HARD_REG_SET reg_is_output_reload;
93 /* Element N is the constant value to which pseudo reg N is equivalent,
94 or zero if pseudo reg N is not equivalent to a constant.
95 find_reloads looks at this in order to replace pseudo reg N
96 with the constant it stands for. */
97 rtx *reg_equiv_constant;
99 /* Element N is a memory location to which pseudo reg N is equivalent,
100 prior to any register elimination (such as frame pointer to stack
101 pointer). Depending on whether or not it is a valid address, this value
102 is transferred to either reg_equiv_address or reg_equiv_mem. */
103 rtx *reg_equiv_memory_loc;
105 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
106 This is used when the address is not valid as a memory address
107 (because its displacement is too big for the machine.) */
108 rtx *reg_equiv_address;
110 /* Element N is the memory slot to which pseudo reg N is equivalent,
111 or zero if pseudo reg N is not equivalent to a memory slot. */
112 rtx *reg_equiv_mem;
114 /* Widest width in which each pseudo reg is referred to (via subreg). */
115 static int *reg_max_ref_width;
117 /* Element N is the insn that initialized reg N from its equivalent
118 constant or memory slot. */
119 static rtx *reg_equiv_init;
121 /* During reload_as_needed, element N contains the last pseudo regno
122 reloaded into the Nth reload register. This vector is in parallel
123 with spill_regs. If that pseudo reg occupied more than one register,
124 reg_reloaded_contents points to that pseudo for each spill register in
125 use; all of these must remain set for an inheritance to occur. */
126 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
128 /* During reload_as_needed, element N contains the insn for which
129 the Nth reload register was last used. This vector is in parallel
130 with spill_regs, and its contents are significant only when
131 reg_reloaded_contents is significant. */
132 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
134 /* Number of spill-regs so far; number of valid elements of spill_regs. */
135 static int n_spills;
137 /* In parallel with spill_regs, contains REG rtx's for those regs.
138 Holds the last rtx used for any given reg, or 0 if it has never
139 been used for spilling yet. This rtx is reused, provided it has
140 the proper mode. */
141 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
143 /* In parallel with spill_regs, contains nonzero for a spill reg
144 that was stored after the last time it was used.
145 The precise value is the insn generated to do the store. */
146 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
148 /* This table is the inverse mapping of spill_regs:
149 indexed by hard reg number,
150 it contains the position of that reg in spill_regs,
151 or -1 for something that is not in spill_regs. */
152 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
154 /* This reg set indicates registers that may not be used for retrying global
155 allocation. The registers that may not be used include all spill registers
156 and the frame pointer (if we are using one). */
157 HARD_REG_SET forbidden_regs;
159 /* This reg set indicates registers that are not good for spill registers.
160 They will not be used to complete groups of spill registers. This includes
161 all fixed registers, registers that may be eliminated, and, if
162 SMALL_REGISTER_CLASSES is zero, registers explicitly used in the rtl.
164 (spill_reg_order prevents these registers from being used to start a
165 group.) */
166 static HARD_REG_SET bad_spill_regs;
168 /* Describes order of use of registers for reloading
169 of spilled pseudo-registers. `spills' is the number of
170 elements that are actually valid; new ones are added at the end. */
171 static short spill_regs[FIRST_PSEUDO_REGISTER];
173 /* This reg set indicates those registers that have been used a spill
174 registers. This information is used in reorg.c, to help figure out
175 what registers are live at any point. It is assumed that all spill_regs
176 are dead at every CODE_LABEL. */
178 HARD_REG_SET used_spill_regs;
180 /* Index of last register assigned as a spill register. We allocate in
181 a round-robin fashion. */
183 static int last_spill_reg;
185 /* Describes order of preference for putting regs into spill_regs.
186 Contains the numbers of all the hard regs, in order most preferred first.
187 This order is different for each function.
188 It is set up by order_regs_for_reload.
189 Empty elements at the end contain -1. */
190 static short potential_reload_regs[FIRST_PSEUDO_REGISTER];
192 /* 1 for a hard register that appears explicitly in the rtl
193 (for example, function value registers, special registers
194 used by insns, structure value pointer registers). */
195 static char regs_explicitly_used[FIRST_PSEUDO_REGISTER];
197 /* Indicates if a register was counted against the need for
198 groups. 0 means it can count against max_nongroup instead. */
199 static HARD_REG_SET counted_for_groups;
201 /* Indicates if a register was counted against the need for
202 non-groups. 0 means it can become part of a new group.
203 During choose_reload_regs, 1 here means don't use this reg
204 as part of a group, even if it seems to be otherwise ok. */
205 static HARD_REG_SET counted_for_nongroups;
207 /* Indexed by pseudo reg number N,
208 says may not delete stores into the real (memory) home of pseudo N.
209 This is set if we already substituted a memory equivalent in some uses,
210 which happens when we have to eliminate the fp from it. */
211 static char *cannot_omit_stores;
213 /* Nonzero if indirect addressing is supported on the machine; this means
214 that spilling (REG n) does not require reloading it into a register in
215 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
216 value indicates the level of indirect addressing supported, e.g., two
217 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
218 a hard register. */
220 static char spill_indirect_levels;
222 /* Nonzero if indirect addressing is supported when the innermost MEM is
223 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
224 which these are valid is the same as spill_indirect_levels, above. */
226 char indirect_symref_ok;
228 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
230 char double_reg_address_ok;
232 /* Record the stack slot for each spilled hard register. */
234 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
236 /* Width allocated so far for that stack slot. */
238 static int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
240 /* Indexed by register class and basic block number, nonzero if there is
241 any need for a spill register of that class in that basic block.
242 The pointer is 0 if we did stupid allocation and don't know
243 the structure of basic blocks. */
245 char *basic_block_needs[N_REG_CLASSES];
247 /* First uid used by insns created by reload in this function.
248 Used in find_equiv_reg. */
249 int reload_first_uid;
251 /* Flag set by local-alloc or global-alloc if anything is live in
252 a call-clobbered reg across calls. */
254 int caller_save_needed;
256 /* The register class to use for a base register when reloading an
257 address. This is normally BASE_REG_CLASS, but it may be different
258 when using SMALL_REGISTER_CLASSES and passing parameters in
259 registers. */
260 enum reg_class reload_address_base_reg_class;
262 /* The register class to use for an index register when reloading an
263 address. This is normally INDEX_REG_CLASS, but it may be different
264 when using SMALL_REGISTER_CLASSES and passing parameters in
265 registers. */
266 enum reg_class reload_address_index_reg_class;
268 /* Set to 1 while reload_as_needed is operating.
269 Required by some machines to handle any generated moves differently. */
271 int reload_in_progress = 0;
273 /* These arrays record the insn_code of insns that may be needed to
274 perform input and output reloads of special objects. They provide a
275 place to pass a scratch register. */
277 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
278 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
280 /* This obstack is used for allocation of rtl during register elimination.
281 The allocated storage can be freed once find_reloads has processed the
282 insn. */
284 struct obstack reload_obstack;
285 char *reload_firstobj;
287 #define obstack_chunk_alloc xmalloc
288 #define obstack_chunk_free free
290 /* List of labels that must never be deleted. */
291 extern rtx forced_labels;
293 /* Allocation number table from global register allocation. */
294 extern int *reg_allocno;
296 /* This structure is used to record information about register eliminations.
297 Each array entry describes one possible way of eliminating a register
298 in favor of another. If there is more than one way of eliminating a
299 particular register, the most preferred should be specified first. */
301 static struct elim_table
303 int from; /* Register number to be eliminated. */
304 int to; /* Register number used as replacement. */
305 int initial_offset; /* Initial difference between values. */
306 int can_eliminate; /* Non-zero if this elimination can be done. */
307 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
308 insns made by reload. */
309 int offset; /* Current offset between the two regs. */
310 int max_offset; /* Maximum offset between the two regs. */
311 int previous_offset; /* Offset at end of previous insn. */
312 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
313 rtx from_rtx; /* REG rtx for the register to be eliminated.
314 We cannot simply compare the number since
315 we might then spuriously replace a hard
316 register corresponding to a pseudo
317 assigned to the reg to be eliminated. */
318 rtx to_rtx; /* REG rtx for the replacement. */
319 } reg_eliminate[] =
321 /* If a set of eliminable registers was specified, define the table from it.
322 Otherwise, default to the normal case of the frame pointer being
323 replaced by the stack pointer. */
325 #ifdef ELIMINABLE_REGS
326 ELIMINABLE_REGS;
327 #else
328 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
329 #endif
331 #define NUM_ELIMINABLE_REGS (sizeof reg_eliminate / sizeof reg_eliminate[0])
333 /* Record the number of pending eliminations that have an offset not equal
334 to their initial offset. If non-zero, we use a new copy of each
335 replacement result in any insns encountered. */
336 static int num_not_at_initial_offset;
338 /* Count the number of registers that we may be able to eliminate. */
339 static int num_eliminable;
341 /* For each label, we record the offset of each elimination. If we reach
342 a label by more than one path and an offset differs, we cannot do the
343 elimination. This information is indexed by the number of the label.
344 The first table is an array of flags that records whether we have yet
345 encountered a label and the second table is an array of arrays, one
346 entry in the latter array for each elimination. */
348 static char *offsets_known_at;
349 static int (*offsets_at)[NUM_ELIMINABLE_REGS];
351 /* Number of labels in the current function. */
353 static int num_labels;
355 struct hard_reg_n_uses { int regno; int uses; };
357 static int possible_group_p PROTO((int, int *));
358 static void count_possible_groups PROTO((int *, enum machine_mode *,
359 int *, int));
360 static int modes_equiv_for_class_p PROTO((enum machine_mode,
361 enum machine_mode,
362 enum reg_class));
363 static void spill_failure PROTO((rtx));
364 static int new_spill_reg PROTO((int, int, int *, int *, int,
365 FILE *));
366 static void delete_dead_insn PROTO((rtx));
367 static void alter_reg PROTO((int, int));
368 static void mark_scratch_live PROTO((rtx));
369 static void set_label_offsets PROTO((rtx, rtx, int));
370 static int eliminate_regs_in_insn PROTO((rtx, int));
371 static void mark_not_eliminable PROTO((rtx, rtx));
372 static int spill_hard_reg PROTO((int, int, FILE *, int));
373 static void scan_paradoxical_subregs PROTO((rtx));
374 static int hard_reg_use_compare PROTO((const GENERIC_PTR, const GENERIC_PTR));
375 static void order_regs_for_reload PROTO((int));
376 static int compare_spill_regs PROTO((const GENERIC_PTR, const GENERIC_PTR));
377 static void reload_as_needed PROTO((rtx, int));
378 static void forget_old_reloads_1 PROTO((rtx, rtx));
379 static int reload_reg_class_lower PROTO((const GENERIC_PTR, const GENERIC_PTR));
380 static void mark_reload_reg_in_use PROTO((int, int, enum reload_type,
381 enum machine_mode));
382 static void clear_reload_reg_in_use PROTO((int, int, enum reload_type,
383 enum machine_mode));
384 static int reload_reg_free_p PROTO((int, int, enum reload_type));
385 static int reload_reg_free_before_p PROTO((int, int, enum reload_type));
386 static int reload_reg_reaches_end_p PROTO((int, int, enum reload_type));
387 static int reloads_conflict PROTO((int, int));
388 static int allocate_reload_reg PROTO((int, rtx, int, int));
389 static void choose_reload_regs PROTO((rtx, rtx));
390 static void merge_assigned_reloads PROTO((rtx));
391 static void emit_reload_insns PROTO((rtx));
392 static void delete_output_reload PROTO((rtx, int, rtx));
393 static void inc_for_reload PROTO((rtx, rtx, int));
394 static int constraint_accepts_reg_p PROTO((char *, rtx));
395 static void reload_cse_invalidate_regno PROTO((int, enum machine_mode, int));
396 static int reload_cse_mem_conflict_p PROTO((rtx, rtx));
397 static void reload_cse_invalidate_mem PROTO((rtx));
398 static void reload_cse_invalidate_rtx PROTO((rtx, rtx));
399 static int reload_cse_regno_equal_p PROTO((int, rtx, enum machine_mode));
400 static int reload_cse_noop_set_p PROTO((rtx, rtx));
401 static int reload_cse_simplify_set PROTO((rtx, rtx));
402 static int reload_cse_simplify_operands PROTO((rtx));
403 static void reload_cse_check_clobber PROTO((rtx, rtx));
404 static void reload_cse_record_set PROTO((rtx, rtx));
405 static void reload_cse_delete_death_notes PROTO((rtx));
406 static void reload_cse_no_longer_dead PROTO((int, enum machine_mode));
408 /* Initialize the reload pass once per compilation. */
410 void
411 init_reload ()
413 register int i;
415 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
416 Set spill_indirect_levels to the number of levels such addressing is
417 permitted, zero if it is not permitted at all. */
419 register rtx tem
420 = gen_rtx_MEM (Pmode,
421 gen_rtx_PLUS (Pmode,
422 gen_rtx_REG (Pmode, LAST_VIRTUAL_REGISTER + 1),
423 GEN_INT (4)));
424 spill_indirect_levels = 0;
426 while (memory_address_p (QImode, tem))
428 spill_indirect_levels++;
429 tem = gen_rtx_MEM (Pmode, tem);
432 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
434 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
435 indirect_symref_ok = memory_address_p (QImode, tem);
437 /* See if reg+reg is a valid (and offsettable) address. */
439 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
441 tem = gen_rtx_PLUS (Pmode,
442 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
443 gen_rtx_REG (Pmode, i));
444 /* This way, we make sure that reg+reg is an offsettable address. */
445 tem = plus_constant (tem, 4);
447 if (memory_address_p (QImode, tem))
449 double_reg_address_ok = 1;
450 break;
454 /* Initialize obstack for our rtl allocation. */
455 gcc_obstack_init (&reload_obstack);
456 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
458 /* Decide which register class should be used when reloading
459 addresses. If we are using SMALL_REGISTER_CLASSES, and any
460 parameters are passed in registers, then we do not want to use
461 those registers when reloading an address. Otherwise, if a
462 function argument needs a reload, we may wind up clobbering
463 another argument to the function which was already computed. If
464 we find a subset class which simply avoids those registers, we
465 use it instead. ??? It would be better to only use the
466 restricted class when we actually are loading function arguments,
467 but that is hard to determine. */
468 reload_address_base_reg_class = BASE_REG_CLASS;
469 reload_address_index_reg_class = INDEX_REG_CLASS;
470 if (SMALL_REGISTER_CLASSES)
472 int regno;
473 HARD_REG_SET base, index;
474 enum reg_class *p;
476 COPY_HARD_REG_SET (base, reg_class_contents[BASE_REG_CLASS]);
477 COPY_HARD_REG_SET (index, reg_class_contents[INDEX_REG_CLASS]);
478 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
480 if (FUNCTION_ARG_REGNO_P (regno))
482 CLEAR_HARD_REG_BIT (base, regno);
483 CLEAR_HARD_REG_BIT (index, regno);
487 GO_IF_HARD_REG_EQUAL (base, reg_class_contents[BASE_REG_CLASS],
488 baseok);
489 for (p = reg_class_subclasses[BASE_REG_CLASS];
490 *p != LIM_REG_CLASSES;
491 p++)
493 GO_IF_HARD_REG_EQUAL (base, reg_class_contents[*p], usebase);
494 continue;
495 usebase:
496 reload_address_base_reg_class = *p;
497 break;
499 baseok:;
501 GO_IF_HARD_REG_EQUAL (index, reg_class_contents[INDEX_REG_CLASS],
502 indexok);
503 for (p = reg_class_subclasses[INDEX_REG_CLASS];
504 *p != LIM_REG_CLASSES;
505 p++)
507 GO_IF_HARD_REG_EQUAL (index, reg_class_contents[*p], useindex);
508 continue;
509 useindex:
510 reload_address_index_reg_class = *p;
511 break;
513 indexok:;
517 /* Main entry point for the reload pass.
519 FIRST is the first insn of the function being compiled.
521 GLOBAL nonzero means we were called from global_alloc
522 and should attempt to reallocate any pseudoregs that we
523 displace from hard regs we will use for reloads.
524 If GLOBAL is zero, we do not have enough information to do that,
525 so any pseudo reg that is spilled must go to the stack.
527 DUMPFILE is the global-reg debugging dump file stream, or 0.
528 If it is nonzero, messages are written to it to describe
529 which registers are seized as reload regs, which pseudo regs
530 are spilled from them, and where the pseudo regs are reallocated to.
532 Return value is nonzero if reload failed
533 and we must not do any more for this function. */
536 reload (first, global, dumpfile)
537 rtx first;
538 int global;
539 FILE *dumpfile;
541 register int class;
542 register int i, j, k;
543 register rtx insn;
544 register struct elim_table *ep;
546 /* The two pointers used to track the true location of the memory used
547 for label offsets. */
548 char *real_known_ptr = NULL_PTR;
549 int (*real_at_ptr)[NUM_ELIMINABLE_REGS];
551 int something_changed;
552 int something_needs_reloads;
553 int something_needs_elimination;
554 int new_basic_block_needs;
555 enum reg_class caller_save_spill_class = NO_REGS;
556 int caller_save_group_size = 1;
558 /* Nonzero means we couldn't get enough spill regs. */
559 int failure = 0;
561 /* The basic block number currently being processed for INSN. */
562 int this_block;
564 /* Make sure even insns with volatile mem refs are recognizable. */
565 init_recog ();
567 /* Enable find_equiv_reg to distinguish insns made by reload. */
568 reload_first_uid = get_max_uid ();
570 for (i = 0; i < N_REG_CLASSES; i++)
571 basic_block_needs[i] = 0;
573 #ifdef SECONDARY_MEMORY_NEEDED
574 /* Initialize the secondary memory table. */
575 clear_secondary_mem ();
576 #endif
578 /* Remember which hard regs appear explicitly
579 before we merge into `regs_ever_live' the ones in which
580 pseudo regs have been allocated. */
581 bcopy (regs_ever_live, regs_explicitly_used, sizeof regs_ever_live);
583 /* We don't have a stack slot for any spill reg yet. */
584 bzero ((char *) spill_stack_slot, sizeof spill_stack_slot);
585 bzero ((char *) spill_stack_slot_width, sizeof spill_stack_slot_width);
587 /* Initialize the save area information for caller-save, in case some
588 are needed. */
589 init_save_areas ();
591 /* Compute which hard registers are now in use
592 as homes for pseudo registers.
593 This is done here rather than (eg) in global_alloc
594 because this point is reached even if not optimizing. */
595 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
596 mark_home_live (i);
598 /* A function that receives a nonlocal goto must save all call-saved
599 registers. */
600 if (current_function_has_nonlocal_label)
601 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
603 if (! call_used_regs[i] && ! fixed_regs[i])
604 regs_ever_live[i] = 1;
607 for (i = 0; i < scratch_list_length; i++)
608 if (scratch_list[i])
609 mark_scratch_live (scratch_list[i]);
611 /* Make sure that the last insn in the chain
612 is not something that needs reloading. */
613 emit_note (NULL_PTR, NOTE_INSN_DELETED);
615 /* Find all the pseudo registers that didn't get hard regs
616 but do have known equivalent constants or memory slots.
617 These include parameters (known equivalent to parameter slots)
618 and cse'd or loop-moved constant memory addresses.
620 Record constant equivalents in reg_equiv_constant
621 so they will be substituted by find_reloads.
622 Record memory equivalents in reg_mem_equiv so they can
623 be substituted eventually by altering the REG-rtx's. */
625 reg_equiv_constant = (rtx *) alloca (max_regno * sizeof (rtx));
626 bzero ((char *) reg_equiv_constant, max_regno * sizeof (rtx));
627 reg_equiv_memory_loc = (rtx *) alloca (max_regno * sizeof (rtx));
628 bzero ((char *) reg_equiv_memory_loc, max_regno * sizeof (rtx));
629 reg_equiv_mem = (rtx *) alloca (max_regno * sizeof (rtx));
630 bzero ((char *) reg_equiv_mem, max_regno * sizeof (rtx));
631 reg_equiv_init = (rtx *) alloca (max_regno * sizeof (rtx));
632 bzero ((char *) reg_equiv_init, max_regno * sizeof (rtx));
633 reg_equiv_address = (rtx *) alloca (max_regno * sizeof (rtx));
634 bzero ((char *) reg_equiv_address, max_regno * sizeof (rtx));
635 reg_max_ref_width = (int *) alloca (max_regno * sizeof (int));
636 bzero ((char *) reg_max_ref_width, max_regno * sizeof (int));
637 cannot_omit_stores = (char *) alloca (max_regno);
638 bzero (cannot_omit_stores, max_regno);
640 if (SMALL_REGISTER_CLASSES)
641 CLEAR_HARD_REG_SET (forbidden_regs);
643 /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
644 Also find all paradoxical subregs and find largest such for each pseudo.
645 On machines with small register classes, record hard registers that
646 are used for user variables. These can never be used for spills.
647 Also look for a "constant" NOTE_INSN_SETJMP. This means that all
648 caller-saved registers must be marked live. */
650 for (insn = first; insn; insn = NEXT_INSN (insn))
652 rtx set = single_set (insn);
654 if (GET_CODE (insn) == NOTE && CONST_CALL_P (insn)
655 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
656 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
657 if (! call_used_regs[i])
658 regs_ever_live[i] = 1;
660 if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
662 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
663 if (note
664 #ifdef LEGITIMATE_PIC_OPERAND_P
665 && (! CONSTANT_P (XEXP (note, 0)) || ! flag_pic
666 || LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))
667 #endif
670 rtx x = XEXP (note, 0);
671 i = REGNO (SET_DEST (set));
672 if (i > LAST_VIRTUAL_REGISTER)
674 if (GET_CODE (x) == MEM)
676 /* If the operand is a PLUS, the MEM may be shared,
677 so make sure we have an unshared copy here. */
678 if (GET_CODE (XEXP (x, 0)) == PLUS)
679 x = copy_rtx (x);
681 reg_equiv_memory_loc[i] = x;
683 else if (CONSTANT_P (x))
685 if (LEGITIMATE_CONSTANT_P (x))
686 reg_equiv_constant[i] = x;
687 else
688 reg_equiv_memory_loc[i]
689 = force_const_mem (GET_MODE (SET_DEST (set)), x);
691 else
692 continue;
694 /* If this register is being made equivalent to a MEM
695 and the MEM is not SET_SRC, the equivalencing insn
696 is one with the MEM as a SET_DEST and it occurs later.
697 So don't mark this insn now. */
698 if (GET_CODE (x) != MEM
699 || rtx_equal_p (SET_SRC (set), x))
700 reg_equiv_init[i] = insn;
705 /* If this insn is setting a MEM from a register equivalent to it,
706 this is the equivalencing insn. */
707 else if (set && GET_CODE (SET_DEST (set)) == MEM
708 && GET_CODE (SET_SRC (set)) == REG
709 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
710 && rtx_equal_p (SET_DEST (set),
711 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
712 reg_equiv_init[REGNO (SET_SRC (set))] = insn;
714 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
715 scan_paradoxical_subregs (PATTERN (insn));
718 /* Does this function require a frame pointer? */
720 frame_pointer_needed = (! flag_omit_frame_pointer
721 #ifdef EXIT_IGNORE_STACK
722 /* ?? If EXIT_IGNORE_STACK is set, we will not save
723 and restore sp for alloca. So we can't eliminate
724 the frame pointer in that case. At some point,
725 we should improve this by emitting the
726 sp-adjusting insns for this case. */
727 || (current_function_calls_alloca
728 && EXIT_IGNORE_STACK)
729 #endif
730 || FRAME_POINTER_REQUIRED);
732 num_eliminable = 0;
734 /* Initialize the table of registers to eliminate. The way we do this
735 depends on how the eliminable registers were defined. */
736 #ifdef ELIMINABLE_REGS
737 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
739 ep->can_eliminate = ep->can_eliminate_previous
740 = (CAN_ELIMINATE (ep->from, ep->to)
741 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
743 #else
744 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
745 = ! frame_pointer_needed;
746 #endif
748 /* Count the number of eliminable registers and build the FROM and TO
749 REG rtx's. Note that code in gen_rtx will cause, e.g.,
750 gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
751 We depend on this. */
752 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
754 num_eliminable += ep->can_eliminate;
755 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
756 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
759 num_labels = max_label_num () - get_first_label_num ();
761 /* Allocate the tables used to store offset information at labels. */
762 /* We used to use alloca here, but the size of what it would try to
763 allocate would occasionally cause it to exceed the stack limit and
764 cause a core dump. */
765 real_known_ptr = xmalloc (num_labels);
766 real_at_ptr
767 = (int (*)[NUM_ELIMINABLE_REGS])
768 xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
770 offsets_known_at = real_known_ptr - get_first_label_num ();
771 offsets_at
772 = (int (*)[NUM_ELIMINABLE_REGS]) (real_at_ptr - get_first_label_num ());
774 /* Alter each pseudo-reg rtx to contain its hard reg number.
775 Assign stack slots to the pseudos that lack hard regs or equivalents.
776 Do not touch virtual registers. */
778 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
779 alter_reg (i, -1);
781 /* If we have some registers we think can be eliminated, scan all insns to
782 see if there is an insn that sets one of these registers to something
783 other than itself plus a constant. If so, the register cannot be
784 eliminated. Doing this scan here eliminates an extra pass through the
785 main reload loop in the most common case where register elimination
786 cannot be done. */
787 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
788 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
789 || GET_CODE (insn) == CALL_INSN)
790 note_stores (PATTERN (insn), mark_not_eliminable);
792 #ifndef REGISTER_CONSTRAINTS
793 /* If all the pseudo regs have hard regs,
794 except for those that are never referenced,
795 we know that no reloads are needed. */
796 /* But that is not true if there are register constraints, since
797 in that case some pseudos might be in the wrong kind of hard reg. */
799 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
800 if (reg_renumber[i] == -1 && REG_N_REFS (i) != 0)
801 break;
803 if (i == max_regno && num_eliminable == 0 && ! caller_save_needed)
805 free (real_known_ptr);
806 free (real_at_ptr);
807 return;
809 #endif
811 /* Compute the order of preference for hard registers to spill.
812 Store them by decreasing preference in potential_reload_regs. */
814 order_regs_for_reload (global);
816 /* So far, no hard regs have been spilled. */
817 n_spills = 0;
818 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
819 spill_reg_order[i] = -1;
821 /* Initialize to -1, which means take the first spill register. */
822 last_spill_reg = -1;
824 /* On most machines, we can't use any register explicitly used in the
825 rtl as a spill register. But on some, we have to. Those will have
826 taken care to keep the life of hard regs as short as possible. */
828 if (! SMALL_REGISTER_CLASSES)
829 COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
831 /* Spill any hard regs that we know we can't eliminate. */
832 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
833 if (! ep->can_eliminate)
834 spill_hard_reg (ep->from, global, dumpfile, 1);
836 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
837 if (frame_pointer_needed)
838 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, global, dumpfile, 1);
839 #endif
841 if (global)
842 for (i = 0; i < N_REG_CLASSES; i++)
844 basic_block_needs[i] = (char *) alloca (n_basic_blocks);
845 bzero (basic_block_needs[i], n_basic_blocks);
848 /* From now on, we need to emit any moves without making new pseudos. */
849 reload_in_progress = 1;
851 /* This loop scans the entire function each go-round
852 and repeats until one repetition spills no additional hard regs. */
854 /* This flag is set when a pseudo reg is spilled,
855 to require another pass. Note that getting an additional reload
856 reg does not necessarily imply any pseudo reg was spilled;
857 sometimes we find a reload reg that no pseudo reg was allocated in. */
858 something_changed = 1;
859 /* This flag is set if there are any insns that require reloading. */
860 something_needs_reloads = 0;
861 /* This flag is set if there are any insns that require register
862 eliminations. */
863 something_needs_elimination = 0;
864 while (something_changed)
866 rtx after_call = 0;
868 /* For each class, number of reload regs needed in that class.
869 This is the maximum over all insns of the needs in that class
870 of the individual insn. */
871 int max_needs[N_REG_CLASSES];
872 /* For each class, size of group of consecutive regs
873 that is needed for the reloads of this class. */
874 int group_size[N_REG_CLASSES];
875 /* For each class, max number of consecutive groups needed.
876 (Each group contains group_size[CLASS] consecutive registers.) */
877 int max_groups[N_REG_CLASSES];
878 /* For each class, max number needed of regs that don't belong
879 to any of the groups. */
880 int max_nongroups[N_REG_CLASSES];
881 /* For each class, the machine mode which requires consecutive
882 groups of regs of that class.
883 If two different modes ever require groups of one class,
884 they must be the same size and equally restrictive for that class,
885 otherwise we can't handle the complexity. */
886 enum machine_mode group_mode[N_REG_CLASSES];
887 /* Record the insn where each maximum need is first found. */
888 rtx max_needs_insn[N_REG_CLASSES];
889 rtx max_groups_insn[N_REG_CLASSES];
890 rtx max_nongroups_insn[N_REG_CLASSES];
891 rtx x;
892 HOST_WIDE_INT starting_frame_size;
893 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
894 int previous_frame_pointer_needed = frame_pointer_needed;
895 #endif
896 static char *reg_class_names[] = REG_CLASS_NAMES;
898 something_changed = 0;
899 bzero ((char *) max_needs, sizeof max_needs);
900 bzero ((char *) max_groups, sizeof max_groups);
901 bzero ((char *) max_nongroups, sizeof max_nongroups);
902 bzero ((char *) max_needs_insn, sizeof max_needs_insn);
903 bzero ((char *) max_groups_insn, sizeof max_groups_insn);
904 bzero ((char *) max_nongroups_insn, sizeof max_nongroups_insn);
905 bzero ((char *) group_size, sizeof group_size);
906 for (i = 0; i < N_REG_CLASSES; i++)
907 group_mode[i] = VOIDmode;
909 /* Keep track of which basic blocks are needing the reloads. */
910 this_block = 0;
912 /* Remember whether any element of basic_block_needs
913 changes from 0 to 1 in this pass. */
914 new_basic_block_needs = 0;
916 /* Round size of stack frame to BIGGEST_ALIGNMENT. This must be done
917 here because the stack size may be a part of the offset computation
918 for register elimination, and there might have been new stack slots
919 created in the last iteration of this loop. */
920 assign_stack_local (BLKmode, 0, 0);
922 starting_frame_size = get_frame_size ();
924 /* Reset all offsets on eliminable registers to their initial values. */
925 #ifdef ELIMINABLE_REGS
926 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
928 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
929 ep->previous_offset = ep->offset
930 = ep->max_offset = ep->initial_offset;
932 #else
933 #ifdef INITIAL_FRAME_POINTER_OFFSET
934 INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
935 #else
936 if (!FRAME_POINTER_REQUIRED)
937 abort ();
938 reg_eliminate[0].initial_offset = 0;
939 #endif
940 reg_eliminate[0].previous_offset = reg_eliminate[0].max_offset
941 = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
942 #endif
944 num_not_at_initial_offset = 0;
946 bzero ((char *) &offsets_known_at[get_first_label_num ()], num_labels);
948 /* Set a known offset for each forced label to be at the initial offset
949 of each elimination. We do this because we assume that all
950 computed jumps occur from a location where each elimination is
951 at its initial offset. */
953 for (x = forced_labels; x; x = XEXP (x, 1))
954 if (XEXP (x, 0))
955 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
957 /* For each pseudo register that has an equivalent location defined,
958 try to eliminate any eliminable registers (such as the frame pointer)
959 assuming initial offsets for the replacement register, which
960 is the normal case.
962 If the resulting location is directly addressable, substitute
963 the MEM we just got directly for the old REG.
965 If it is not addressable but is a constant or the sum of a hard reg
966 and constant, it is probably not addressable because the constant is
967 out of range, in that case record the address; we will generate
968 hairy code to compute the address in a register each time it is
969 needed. Similarly if it is a hard register, but one that is not
970 valid as an address register.
972 If the location is not addressable, but does not have one of the
973 above forms, assign a stack slot. We have to do this to avoid the
974 potential of producing lots of reloads if, e.g., a location involves
975 a pseudo that didn't get a hard register and has an equivalent memory
976 location that also involves a pseudo that didn't get a hard register.
978 Perhaps at some point we will improve reload_when_needed handling
979 so this problem goes away. But that's very hairy. */
981 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
982 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
984 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
986 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
987 XEXP (x, 0)))
988 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
989 else if (CONSTANT_P (XEXP (x, 0))
990 || (GET_CODE (XEXP (x, 0)) == REG
991 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
992 || (GET_CODE (XEXP (x, 0)) == PLUS
993 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
994 && (REGNO (XEXP (XEXP (x, 0), 0))
995 < FIRST_PSEUDO_REGISTER)
996 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
997 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
998 else
1000 /* Make a new stack slot. Then indicate that something
1001 changed so we go back and recompute offsets for
1002 eliminable registers because the allocation of memory
1003 below might change some offset. reg_equiv_{mem,address}
1004 will be set up for this pseudo on the next pass around
1005 the loop. */
1006 reg_equiv_memory_loc[i] = 0;
1007 reg_equiv_init[i] = 0;
1008 alter_reg (i, -1);
1009 something_changed = 1;
1013 /* If we allocated another pseudo to the stack, redo elimination
1014 bookkeeping. */
1015 if (something_changed)
1016 continue;
1018 /* If caller-saves needs a group, initialize the group to include
1019 the size and mode required for caller-saves. */
1021 if (caller_save_group_size > 1)
1023 group_mode[(int) caller_save_spill_class] = Pmode;
1024 group_size[(int) caller_save_spill_class] = caller_save_group_size;
1027 /* Compute the most additional registers needed by any instruction.
1028 Collect information separately for each class of regs. */
1030 for (insn = first; insn; insn = NEXT_INSN (insn))
1032 if (global && this_block + 1 < n_basic_blocks
1033 && insn == basic_block_head[this_block+1])
1034 ++this_block;
1036 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which
1037 might include REG_LABEL), we need to see what effects this
1038 has on the known offsets at labels. */
1040 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
1041 || (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1042 && REG_NOTES (insn) != 0))
1043 set_label_offsets (insn, insn, 0);
1045 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1047 /* Nonzero means don't use a reload reg that overlaps
1048 the place where a function value can be returned. */
1049 rtx avoid_return_reg = 0;
1051 rtx old_body = PATTERN (insn);
1052 int old_code = INSN_CODE (insn);
1053 rtx old_notes = REG_NOTES (insn);
1054 int did_elimination = 0;
1056 /* To compute the number of reload registers of each class
1057 needed for an insn, we must simulate what choose_reload_regs
1058 can do. We do this by splitting an insn into an "input" and
1059 an "output" part. RELOAD_OTHER reloads are used in both.
1060 The input part uses those reloads, RELOAD_FOR_INPUT reloads,
1061 which must be live over the entire input section of reloads,
1062 and the maximum of all the RELOAD_FOR_INPUT_ADDRESS and
1063 RELOAD_FOR_OPERAND_ADDRESS reloads, which conflict with the
1064 inputs.
1066 The registers needed for output are RELOAD_OTHER and
1067 RELOAD_FOR_OUTPUT, which are live for the entire output
1068 portion, and the maximum of all the RELOAD_FOR_OUTPUT_ADDRESS
1069 reloads for each operand.
1071 The total number of registers needed is the maximum of the
1072 inputs and outputs. */
1074 struct needs
1076 /* [0] is normal, [1] is nongroup. */
1077 int regs[2][N_REG_CLASSES];
1078 int groups[N_REG_CLASSES];
1081 /* Each `struct needs' corresponds to one RELOAD_... type. */
1082 struct {
1083 struct needs other;
1084 struct needs input;
1085 struct needs output;
1086 struct needs insn;
1087 struct needs other_addr;
1088 struct needs op_addr;
1089 struct needs op_addr_reload;
1090 struct needs in_addr[MAX_RECOG_OPERANDS];
1091 struct needs in_addr_addr[MAX_RECOG_OPERANDS];
1092 struct needs out_addr[MAX_RECOG_OPERANDS];
1093 struct needs out_addr_addr[MAX_RECOG_OPERANDS];
1094 } insn_needs;
1096 /* If needed, eliminate any eliminable registers. */
1097 if (num_eliminable)
1098 did_elimination = eliminate_regs_in_insn (insn, 0);
1100 /* Set avoid_return_reg if this is an insn
1101 that might use the value of a function call. */
1102 if (SMALL_REGISTER_CLASSES && GET_CODE (insn) == CALL_INSN)
1104 if (GET_CODE (PATTERN (insn)) == SET)
1105 after_call = SET_DEST (PATTERN (insn));
1106 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1107 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1108 after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1109 else
1110 after_call = 0;
1112 else if (SMALL_REGISTER_CLASSES && after_call != 0
1113 && !(GET_CODE (PATTERN (insn)) == SET
1114 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx))
1116 if (reg_referenced_p (after_call, PATTERN (insn)))
1117 avoid_return_reg = after_call;
1118 after_call = 0;
1121 /* Analyze the instruction. */
1122 find_reloads (insn, 0, spill_indirect_levels, global,
1123 spill_reg_order);
1125 /* Remember for later shortcuts which insns had any reloads or
1126 register eliminations.
1128 One might think that it would be worthwhile to mark insns
1129 that need register replacements but not reloads, but this is
1130 not safe because find_reloads may do some manipulation of
1131 the insn (such as swapping commutative operands), which would
1132 be lost when we restore the old pattern after register
1133 replacement. So the actions of find_reloads must be redone in
1134 subsequent passes or in reload_as_needed.
1136 However, it is safe to mark insns that need reloads
1137 but not register replacement. */
1139 PUT_MODE (insn, (did_elimination ? QImode
1140 : n_reloads ? HImode
1141 : GET_MODE (insn) == DImode ? DImode
1142 : VOIDmode));
1144 /* Discard any register replacements done. */
1145 if (did_elimination)
1147 obstack_free (&reload_obstack, reload_firstobj);
1148 PATTERN (insn) = old_body;
1149 INSN_CODE (insn) = old_code;
1150 REG_NOTES (insn) = old_notes;
1151 something_needs_elimination = 1;
1154 /* If this insn has no reloads, we need not do anything except
1155 in the case of a CALL_INSN when we have caller-saves and
1156 caller-save needs reloads. */
1158 if (n_reloads == 0
1159 && ! (GET_CODE (insn) == CALL_INSN
1160 && caller_save_spill_class != NO_REGS))
1161 continue;
1163 something_needs_reloads = 1;
1164 bzero ((char *) &insn_needs, sizeof insn_needs);
1166 /* Count each reload once in every class
1167 containing the reload's own class. */
1169 for (i = 0; i < n_reloads; i++)
1171 register enum reg_class *p;
1172 enum reg_class class = reload_reg_class[i];
1173 int size;
1174 enum machine_mode mode;
1175 int nongroup_need;
1176 struct needs *this_needs;
1178 /* Don't count the dummy reloads, for which one of the
1179 regs mentioned in the insn can be used for reloading.
1180 Don't count optional reloads.
1181 Don't count reloads that got combined with others. */
1182 if (reload_reg_rtx[i] != 0
1183 || reload_optional[i] != 0
1184 || (reload_out[i] == 0 && reload_in[i] == 0
1185 && ! reload_secondary_p[i]))
1186 continue;
1188 /* Show that a reload register of this class is needed
1189 in this basic block. We do not use insn_needs and
1190 insn_groups because they are overly conservative for
1191 this purpose. */
1192 if (global && ! basic_block_needs[(int) class][this_block])
1194 basic_block_needs[(int) class][this_block] = 1;
1195 new_basic_block_needs = 1;
1198 mode = reload_inmode[i];
1199 if (GET_MODE_SIZE (reload_outmode[i]) > GET_MODE_SIZE (mode))
1200 mode = reload_outmode[i];
1201 size = CLASS_MAX_NREGS (class, mode);
1203 /* If this class doesn't want a group, determine if we have
1204 a nongroup need or a regular need. We have a nongroup
1205 need if this reload conflicts with a group reload whose
1206 class intersects with this reload's class. */
1208 nongroup_need = 0;
1209 if (size == 1)
1210 for (j = 0; j < n_reloads; j++)
1211 if ((CLASS_MAX_NREGS (reload_reg_class[j],
1212 (GET_MODE_SIZE (reload_outmode[j])
1213 > GET_MODE_SIZE (reload_inmode[j]))
1214 ? reload_outmode[j]
1215 : reload_inmode[j])
1216 > 1)
1217 && (!reload_optional[j])
1218 && (reload_in[j] != 0 || reload_out[j] != 0
1219 || reload_secondary_p[j])
1220 && reloads_conflict (i, j)
1221 && reg_classes_intersect_p (class,
1222 reload_reg_class[j]))
1224 nongroup_need = 1;
1225 break;
1228 /* Decide which time-of-use to count this reload for. */
1229 switch (reload_when_needed[i])
1231 case RELOAD_OTHER:
1232 this_needs = &insn_needs.other;
1233 break;
1234 case RELOAD_FOR_INPUT:
1235 this_needs = &insn_needs.input;
1236 break;
1237 case RELOAD_FOR_OUTPUT:
1238 this_needs = &insn_needs.output;
1239 break;
1240 case RELOAD_FOR_INSN:
1241 this_needs = &insn_needs.insn;
1242 break;
1243 case RELOAD_FOR_OTHER_ADDRESS:
1244 this_needs = &insn_needs.other_addr;
1245 break;
1246 case RELOAD_FOR_INPUT_ADDRESS:
1247 this_needs = &insn_needs.in_addr[reload_opnum[i]];
1248 break;
1249 case RELOAD_FOR_INPADDR_ADDRESS:
1250 this_needs = &insn_needs.in_addr_addr[reload_opnum[i]];
1251 break;
1252 case RELOAD_FOR_OUTPUT_ADDRESS:
1253 this_needs = &insn_needs.out_addr[reload_opnum[i]];
1254 break;
1255 case RELOAD_FOR_OUTADDR_ADDRESS:
1256 this_needs = &insn_needs.out_addr_addr[reload_opnum[i]];
1257 break;
1258 case RELOAD_FOR_OPERAND_ADDRESS:
1259 this_needs = &insn_needs.op_addr;
1260 break;
1261 case RELOAD_FOR_OPADDR_ADDR:
1262 this_needs = &insn_needs.op_addr_reload;
1263 break;
1266 if (size > 1)
1268 enum machine_mode other_mode, allocate_mode;
1270 /* Count number of groups needed separately from
1271 number of individual regs needed. */
1272 this_needs->groups[(int) class]++;
1273 p = reg_class_superclasses[(int) class];
1274 while (*p != LIM_REG_CLASSES)
1275 this_needs->groups[(int) *p++]++;
1277 /* Record size and mode of a group of this class. */
1278 /* If more than one size group is needed,
1279 make all groups the largest needed size. */
1280 if (group_size[(int) class] < size)
1282 other_mode = group_mode[(int) class];
1283 allocate_mode = mode;
1285 group_size[(int) class] = size;
1286 group_mode[(int) class] = mode;
1288 else
1290 other_mode = mode;
1291 allocate_mode = group_mode[(int) class];
1294 /* Crash if two dissimilar machine modes both need
1295 groups of consecutive regs of the same class. */
1297 if (other_mode != VOIDmode && other_mode != allocate_mode
1298 && ! modes_equiv_for_class_p (allocate_mode,
1299 other_mode, class))
1300 fatal_insn ("Two dissimilar machine modes both need groups of consecutive regs of the same class",
1301 insn);
1303 else if (size == 1)
1305 this_needs->regs[nongroup_need][(int) class] += 1;
1306 p = reg_class_superclasses[(int) class];
1307 while (*p != LIM_REG_CLASSES)
1308 this_needs->regs[nongroup_need][(int) *p++] += 1;
1310 else
1311 abort ();
1314 /* All reloads have been counted for this insn;
1315 now merge the various times of use.
1316 This sets insn_needs, etc., to the maximum total number
1317 of registers needed at any point in this insn. */
1319 for (i = 0; i < N_REG_CLASSES; i++)
1321 int in_max, out_max;
1323 /* Compute normal and nongroup needs. */
1324 for (j = 0; j <= 1; j++)
1326 for (in_max = 0, out_max = 0, k = 0;
1327 k < reload_n_operands; k++)
1329 in_max
1330 = MAX (in_max,
1331 (insn_needs.in_addr[k].regs[j][i]
1332 + insn_needs.in_addr_addr[k].regs[j][i]));
1333 out_max
1334 = MAX (out_max, insn_needs.out_addr[k].regs[j][i]);
1335 out_max
1336 = MAX (out_max,
1337 insn_needs.out_addr_addr[k].regs[j][i]);
1340 /* RELOAD_FOR_INSN reloads conflict with inputs, outputs,
1341 and operand addresses but not things used to reload
1342 them. Similarly, RELOAD_FOR_OPERAND_ADDRESS reloads
1343 don't conflict with things needed to reload inputs or
1344 outputs. */
1346 in_max = MAX (MAX (insn_needs.op_addr.regs[j][i],
1347 insn_needs.op_addr_reload.regs[j][i]),
1348 in_max);
1350 out_max = MAX (out_max, insn_needs.insn.regs[j][i]);
1352 insn_needs.input.regs[j][i]
1353 = MAX (insn_needs.input.regs[j][i]
1354 + insn_needs.op_addr.regs[j][i]
1355 + insn_needs.insn.regs[j][i],
1356 in_max + insn_needs.input.regs[j][i]);
1358 insn_needs.output.regs[j][i] += out_max;
1359 insn_needs.other.regs[j][i]
1360 += MAX (MAX (insn_needs.input.regs[j][i],
1361 insn_needs.output.regs[j][i]),
1362 insn_needs.other_addr.regs[j][i]);
1366 /* Now compute group needs. */
1367 for (in_max = 0, out_max = 0, j = 0;
1368 j < reload_n_operands; j++)
1370 in_max = MAX (in_max, insn_needs.in_addr[j].groups[i]);
1371 in_max = MAX (in_max,
1372 insn_needs.in_addr_addr[j].groups[i]);
1373 out_max
1374 = MAX (out_max, insn_needs.out_addr[j].groups[i]);
1375 out_max
1376 = MAX (out_max, insn_needs.out_addr_addr[j].groups[i]);
1379 in_max = MAX (MAX (insn_needs.op_addr.groups[i],
1380 insn_needs.op_addr_reload.groups[i]),
1381 in_max);
1382 out_max = MAX (out_max, insn_needs.insn.groups[i]);
1384 insn_needs.input.groups[i]
1385 = MAX (insn_needs.input.groups[i]
1386 + insn_needs.op_addr.groups[i]
1387 + insn_needs.insn.groups[i],
1388 in_max + insn_needs.input.groups[i]);
1390 insn_needs.output.groups[i] += out_max;
1391 insn_needs.other.groups[i]
1392 += MAX (MAX (insn_needs.input.groups[i],
1393 insn_needs.output.groups[i]),
1394 insn_needs.other_addr.groups[i]);
1397 /* If this is a CALL_INSN and caller-saves will need
1398 a spill register, act as if the spill register is
1399 needed for this insn. However, the spill register
1400 can be used by any reload of this insn, so we only
1401 need do something if no need for that class has
1402 been recorded.
1404 The assumption that every CALL_INSN will trigger a
1405 caller-save is highly conservative, however, the number
1406 of cases where caller-saves will need a spill register but
1407 a block containing a CALL_INSN won't need a spill register
1408 of that class should be quite rare.
1410 If a group is needed, the size and mode of the group will
1411 have been set up at the beginning of this loop. */
1413 if (GET_CODE (insn) == CALL_INSN
1414 && caller_save_spill_class != NO_REGS)
1416 /* See if this register would conflict with any reload
1417 that needs a group. */
1418 int nongroup_need = 0;
1419 int *caller_save_needs;
1421 for (j = 0; j < n_reloads; j++)
1422 if ((CLASS_MAX_NREGS (reload_reg_class[j],
1423 (GET_MODE_SIZE (reload_outmode[j])
1424 > GET_MODE_SIZE (reload_inmode[j]))
1425 ? reload_outmode[j]
1426 : reload_inmode[j])
1427 > 1)
1428 && reg_classes_intersect_p (caller_save_spill_class,
1429 reload_reg_class[j]))
1431 nongroup_need = 1;
1432 break;
1435 caller_save_needs
1436 = (caller_save_group_size > 1
1437 ? insn_needs.other.groups
1438 : insn_needs.other.regs[nongroup_need]);
1440 if (caller_save_needs[(int) caller_save_spill_class] == 0)
1442 register enum reg_class *p
1443 = reg_class_superclasses[(int) caller_save_spill_class];
1445 caller_save_needs[(int) caller_save_spill_class]++;
1447 while (*p != LIM_REG_CLASSES)
1448 caller_save_needs[(int) *p++] += 1;
1451 /* Show that this basic block will need a register of
1452 this class. */
1454 if (global
1455 && ! (basic_block_needs[(int) caller_save_spill_class]
1456 [this_block]))
1458 basic_block_needs[(int) caller_save_spill_class]
1459 [this_block] = 1;
1460 new_basic_block_needs = 1;
1464 /* If this insn stores the value of a function call,
1465 and that value is in a register that has been spilled,
1466 and if the insn needs a reload in a class
1467 that might use that register as the reload register,
1468 then add add an extra need in that class.
1469 This makes sure we have a register available that does
1470 not overlap the return value. */
1472 if (SMALL_REGISTER_CLASSES && avoid_return_reg)
1474 int regno = REGNO (avoid_return_reg);
1475 int nregs
1476 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
1477 int r;
1478 int basic_needs[N_REG_CLASSES], basic_groups[N_REG_CLASSES];
1480 /* First compute the "basic needs", which counts a
1481 need only in the smallest class in which it
1482 is required. */
1484 bcopy ((char *) insn_needs.other.regs[0],
1485 (char *) basic_needs, sizeof basic_needs);
1486 bcopy ((char *) insn_needs.other.groups,
1487 (char *) basic_groups, sizeof basic_groups);
1489 for (i = 0; i < N_REG_CLASSES; i++)
1491 enum reg_class *p;
1493 if (basic_needs[i] >= 0)
1494 for (p = reg_class_superclasses[i];
1495 *p != LIM_REG_CLASSES; p++)
1496 basic_needs[(int) *p] -= basic_needs[i];
1498 if (basic_groups[i] >= 0)
1499 for (p = reg_class_superclasses[i];
1500 *p != LIM_REG_CLASSES; p++)
1501 basic_groups[(int) *p] -= basic_groups[i];
1504 /* Now count extra regs if there might be a conflict with
1505 the return value register. */
1507 for (r = regno; r < regno + nregs; r++)
1508 if (spill_reg_order[r] >= 0)
1509 for (i = 0; i < N_REG_CLASSES; i++)
1510 if (TEST_HARD_REG_BIT (reg_class_contents[i], r))
1512 if (basic_needs[i] > 0)
1514 enum reg_class *p;
1516 insn_needs.other.regs[0][i]++;
1517 p = reg_class_superclasses[i];
1518 while (*p != LIM_REG_CLASSES)
1519 insn_needs.other.regs[0][(int) *p++]++;
1521 if (basic_groups[i] > 0)
1523 enum reg_class *p;
1525 insn_needs.other.groups[i]++;
1526 p = reg_class_superclasses[i];
1527 while (*p != LIM_REG_CLASSES)
1528 insn_needs.other.groups[(int) *p++]++;
1533 /* For each class, collect maximum need of any insn. */
1535 for (i = 0; i < N_REG_CLASSES; i++)
1537 if (max_needs[i] < insn_needs.other.regs[0][i])
1539 max_needs[i] = insn_needs.other.regs[0][i];
1540 max_needs_insn[i] = insn;
1542 if (max_groups[i] < insn_needs.other.groups[i])
1544 max_groups[i] = insn_needs.other.groups[i];
1545 max_groups_insn[i] = insn;
1547 if (max_nongroups[i] < insn_needs.other.regs[1][i])
1549 max_nongroups[i] = insn_needs.other.regs[1][i];
1550 max_nongroups_insn[i] = insn;
1554 /* Note that there is a continue statement above. */
1557 /* If we allocated any new memory locations, make another pass
1558 since it might have changed elimination offsets. */
1559 if (starting_frame_size != get_frame_size ())
1560 something_changed = 1;
1562 if (dumpfile)
1563 for (i = 0; i < N_REG_CLASSES; i++)
1565 if (max_needs[i] > 0)
1566 fprintf (dumpfile,
1567 ";; Need %d reg%s of class %s (for insn %d).\n",
1568 max_needs[i], max_needs[i] == 1 ? "" : "s",
1569 reg_class_names[i], INSN_UID (max_needs_insn[i]));
1570 if (max_nongroups[i] > 0)
1571 fprintf (dumpfile,
1572 ";; Need %d nongroup reg%s of class %s (for insn %d).\n",
1573 max_nongroups[i], max_nongroups[i] == 1 ? "" : "s",
1574 reg_class_names[i], INSN_UID (max_nongroups_insn[i]));
1575 if (max_groups[i] > 0)
1576 fprintf (dumpfile,
1577 ";; Need %d group%s (%smode) of class %s (for insn %d).\n",
1578 max_groups[i], max_groups[i] == 1 ? "" : "s",
1579 mode_name[(int) group_mode[i]],
1580 reg_class_names[i], INSN_UID (max_groups_insn[i]));
1583 /* If we have caller-saves, set up the save areas and see if caller-save
1584 will need a spill register. */
1586 if (caller_save_needed)
1588 /* Set the offsets for setup_save_areas. */
1589 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
1590 ep++)
1591 ep->previous_offset = ep->max_offset;
1593 if ( ! setup_save_areas (&something_changed)
1594 && caller_save_spill_class == NO_REGS)
1596 /* The class we will need depends on whether the machine
1597 supports the sum of two registers for an address; see
1598 find_address_reloads for details. */
1600 caller_save_spill_class
1601 = double_reg_address_ok ? INDEX_REG_CLASS : BASE_REG_CLASS;
1602 caller_save_group_size
1603 = CLASS_MAX_NREGS (caller_save_spill_class, Pmode);
1604 something_changed = 1;
1608 /* See if anything that happened changes which eliminations are valid.
1609 For example, on the Sparc, whether or not the frame pointer can
1610 be eliminated can depend on what registers have been used. We need
1611 not check some conditions again (such as flag_omit_frame_pointer)
1612 since they can't have changed. */
1614 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1615 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
1616 #ifdef ELIMINABLE_REGS
1617 || ! CAN_ELIMINATE (ep->from, ep->to)
1618 #endif
1620 ep->can_eliminate = 0;
1622 /* Look for the case where we have discovered that we can't replace
1623 register A with register B and that means that we will now be
1624 trying to replace register A with register C. This means we can
1625 no longer replace register C with register B and we need to disable
1626 such an elimination, if it exists. This occurs often with A == ap,
1627 B == sp, and C == fp. */
1629 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1631 struct elim_table *op;
1632 register int new_to = -1;
1634 if (! ep->can_eliminate && ep->can_eliminate_previous)
1636 /* Find the current elimination for ep->from, if there is a
1637 new one. */
1638 for (op = reg_eliminate;
1639 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
1640 if (op->from == ep->from && op->can_eliminate)
1642 new_to = op->to;
1643 break;
1646 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
1647 disable it. */
1648 for (op = reg_eliminate;
1649 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
1650 if (op->from == new_to && op->to == ep->to)
1651 op->can_eliminate = 0;
1655 /* See if any registers that we thought we could eliminate the previous
1656 time are no longer eliminable. If so, something has changed and we
1657 must spill the register. Also, recompute the number of eliminable
1658 registers and see if the frame pointer is needed; it is if there is
1659 no elimination of the frame pointer that we can perform. */
1661 frame_pointer_needed = 1;
1662 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1664 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
1665 && ep->to != HARD_FRAME_POINTER_REGNUM)
1666 frame_pointer_needed = 0;
1668 if (! ep->can_eliminate && ep->can_eliminate_previous)
1670 ep->can_eliminate_previous = 0;
1671 spill_hard_reg (ep->from, global, dumpfile, 1);
1672 something_changed = 1;
1673 num_eliminable--;
1677 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1678 /* If we didn't need a frame pointer last time, but we do now, spill
1679 the hard frame pointer. */
1680 if (frame_pointer_needed && ! previous_frame_pointer_needed)
1682 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, global, dumpfile, 1);
1683 something_changed = 1;
1685 #endif
1687 /* If all needs are met, we win. */
1689 for (i = 0; i < N_REG_CLASSES; i++)
1690 if (max_needs[i] > 0 || max_groups[i] > 0 || max_nongroups[i] > 0)
1691 break;
1692 if (i == N_REG_CLASSES && !new_basic_block_needs && ! something_changed)
1693 break;
1695 /* Not all needs are met; must spill some hard regs. */
1697 /* Put all registers spilled so far back in potential_reload_regs, but
1698 put them at the front, since we've already spilled most of the
1699 pseudos in them (we might have left some pseudos unspilled if they
1700 were in a block that didn't need any spill registers of a conflicting
1701 class. We used to try to mark off the need for those registers,
1702 but doing so properly is very complex and reallocating them is the
1703 simpler approach. First, "pack" potential_reload_regs by pushing
1704 any nonnegative entries towards the end. That will leave room
1705 for the registers we already spilled.
1707 Also, undo the marking of the spill registers from the last time
1708 around in FORBIDDEN_REGS since we will be probably be allocating
1709 them again below.
1711 ??? It is theoretically possible that we might end up not using one
1712 of our previously-spilled registers in this allocation, even though
1713 they are at the head of the list. It's not clear what to do about
1714 this, but it was no better before, when we marked off the needs met
1715 by the previously-spilled registers. With the current code, globals
1716 can be allocated into these registers, but locals cannot. */
1718 if (n_spills)
1720 for (i = j = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1721 if (potential_reload_regs[i] != -1)
1722 potential_reload_regs[j--] = potential_reload_regs[i];
1724 for (i = 0; i < n_spills; i++)
1726 potential_reload_regs[i] = spill_regs[i];
1727 spill_reg_order[spill_regs[i]] = -1;
1728 CLEAR_HARD_REG_BIT (forbidden_regs, spill_regs[i]);
1731 n_spills = 0;
1734 /* Now find more reload regs to satisfy the remaining need
1735 Do it by ascending class number, since otherwise a reg
1736 might be spilled for a big class and might fail to count
1737 for a smaller class even though it belongs to that class.
1739 Count spilled regs in `spills', and add entries to
1740 `spill_regs' and `spill_reg_order'.
1742 ??? Note there is a problem here.
1743 When there is a need for a group in a high-numbered class,
1744 and also need for non-group regs that come from a lower class,
1745 the non-group regs are chosen first. If there aren't many regs,
1746 they might leave no room for a group.
1748 This was happening on the 386. To fix it, we added the code
1749 that calls possible_group_p, so that the lower class won't
1750 break up the last possible group.
1752 Really fixing the problem would require changes above
1753 in counting the regs already spilled, and in choose_reload_regs.
1754 It might be hard to avoid introducing bugs there. */
1756 CLEAR_HARD_REG_SET (counted_for_groups);
1757 CLEAR_HARD_REG_SET (counted_for_nongroups);
1759 for (class = 0; class < N_REG_CLASSES; class++)
1761 /* First get the groups of registers.
1762 If we got single registers first, we might fragment
1763 possible groups. */
1764 while (max_groups[class] > 0)
1766 /* If any single spilled regs happen to form groups,
1767 count them now. Maybe we don't really need
1768 to spill another group. */
1769 count_possible_groups (group_size, group_mode, max_groups,
1770 class);
1772 if (max_groups[class] <= 0)
1773 break;
1775 /* Groups of size 2 (the only groups used on most machines)
1776 are treated specially. */
1777 if (group_size[class] == 2)
1779 /* First, look for a register that will complete a group. */
1780 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1782 int other;
1784 j = potential_reload_regs[i];
1785 if (j >= 0 && ! TEST_HARD_REG_BIT (bad_spill_regs, j)
1787 ((j > 0 && (other = j - 1, spill_reg_order[other] >= 0)
1788 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1789 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1790 && HARD_REGNO_MODE_OK (other, group_mode[class])
1791 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1792 other)
1793 /* We don't want one part of another group.
1794 We could get "two groups" that overlap! */
1795 && ! TEST_HARD_REG_BIT (counted_for_groups, other))
1797 (j < FIRST_PSEUDO_REGISTER - 1
1798 && (other = j + 1, spill_reg_order[other] >= 0)
1799 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1800 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1801 && HARD_REGNO_MODE_OK (j, group_mode[class])
1802 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1803 other)
1804 && ! TEST_HARD_REG_BIT (counted_for_groups,
1805 other))))
1807 register enum reg_class *p;
1809 /* We have found one that will complete a group,
1810 so count off one group as provided. */
1811 max_groups[class]--;
1812 p = reg_class_superclasses[class];
1813 while (*p != LIM_REG_CLASSES)
1815 if (group_size [(int) *p] <= group_size [class])
1816 max_groups[(int) *p]--;
1817 p++;
1820 /* Indicate both these regs are part of a group. */
1821 SET_HARD_REG_BIT (counted_for_groups, j);
1822 SET_HARD_REG_BIT (counted_for_groups, other);
1823 break;
1826 /* We can't complete a group, so start one. */
1827 /* Look for a pair neither of which is explicitly used. */
1828 if (SMALL_REGISTER_CLASSES && i == FIRST_PSEUDO_REGISTER)
1829 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1831 int k;
1832 j = potential_reload_regs[i];
1833 /* Verify that J+1 is a potential reload reg. */
1834 for (k = 0; k < FIRST_PSEUDO_REGISTER; k++)
1835 if (potential_reload_regs[k] == j + 1)
1836 break;
1837 if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1838 && k < FIRST_PSEUDO_REGISTER
1839 && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1840 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1841 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1842 && HARD_REGNO_MODE_OK (j, group_mode[class])
1843 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1844 j + 1)
1845 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + 1)
1846 /* Reject J at this stage
1847 if J+1 was explicitly used. */
1848 && ! regs_explicitly_used[j + 1])
1849 break;
1851 /* Now try any group at all
1852 whose registers are not in bad_spill_regs. */
1853 if (i == FIRST_PSEUDO_REGISTER)
1854 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1856 int k;
1857 j = potential_reload_regs[i];
1858 /* Verify that J+1 is a potential reload reg. */
1859 for (k = 0; k < FIRST_PSEUDO_REGISTER; k++)
1860 if (potential_reload_regs[k] == j + 1)
1861 break;
1862 if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1863 && k < FIRST_PSEUDO_REGISTER
1864 && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1865 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1866 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1867 && HARD_REGNO_MODE_OK (j, group_mode[class])
1868 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1869 j + 1)
1870 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + 1))
1871 break;
1874 /* I should be the index in potential_reload_regs
1875 of the new reload reg we have found. */
1877 if (i >= FIRST_PSEUDO_REGISTER)
1879 /* There are no groups left to spill. */
1880 spill_failure (max_groups_insn[class]);
1881 failure = 1;
1882 goto failed;
1884 else
1885 something_changed
1886 |= new_spill_reg (i, class, max_needs, NULL_PTR,
1887 global, dumpfile);
1889 else
1891 /* For groups of more than 2 registers,
1892 look for a sufficient sequence of unspilled registers,
1893 and spill them all at once. */
1894 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1896 int k;
1898 j = potential_reload_regs[i];
1899 if (j >= 0
1900 && j + group_size[class] <= FIRST_PSEUDO_REGISTER
1901 && HARD_REGNO_MODE_OK (j, group_mode[class]))
1903 /* Check each reg in the sequence. */
1904 for (k = 0; k < group_size[class]; k++)
1905 if (! (spill_reg_order[j + k] < 0
1906 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + k)
1907 && TEST_HARD_REG_BIT (reg_class_contents[class], j + k)))
1908 break;
1909 /* We got a full sequence, so spill them all. */
1910 if (k == group_size[class])
1912 register enum reg_class *p;
1913 for (k = 0; k < group_size[class]; k++)
1915 int idx;
1916 SET_HARD_REG_BIT (counted_for_groups, j + k);
1917 for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
1918 if (potential_reload_regs[idx] == j + k)
1919 break;
1920 something_changed
1921 |= new_spill_reg (idx, class,
1922 max_needs, NULL_PTR,
1923 global, dumpfile);
1926 /* We have found one that will complete a group,
1927 so count off one group as provided. */
1928 max_groups[class]--;
1929 p = reg_class_superclasses[class];
1930 while (*p != LIM_REG_CLASSES)
1932 if (group_size [(int) *p]
1933 <= group_size [class])
1934 max_groups[(int) *p]--;
1935 p++;
1937 break;
1941 /* We couldn't find any registers for this reload.
1942 Avoid going into an infinite loop. */
1943 if (i >= FIRST_PSEUDO_REGISTER)
1945 /* There are no groups left. */
1946 spill_failure (max_groups_insn[class]);
1947 failure = 1;
1948 goto failed;
1953 /* Now similarly satisfy all need for single registers. */
1955 while (max_needs[class] > 0 || max_nongroups[class] > 0)
1957 /* If we spilled enough regs, but they weren't counted
1958 against the non-group need, see if we can count them now.
1959 If so, we can avoid some actual spilling. */
1960 if (max_needs[class] <= 0 && max_nongroups[class] > 0)
1961 for (i = 0; i < n_spills; i++)
1962 if (TEST_HARD_REG_BIT (reg_class_contents[class],
1963 spill_regs[i])
1964 && !TEST_HARD_REG_BIT (counted_for_groups,
1965 spill_regs[i])
1966 && !TEST_HARD_REG_BIT (counted_for_nongroups,
1967 spill_regs[i])
1968 && max_nongroups[class] > 0)
1970 register enum reg_class *p;
1972 SET_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]);
1973 max_nongroups[class]--;
1974 p = reg_class_superclasses[class];
1975 while (*p != LIM_REG_CLASSES)
1976 max_nongroups[(int) *p++]--;
1978 if (max_needs[class] <= 0 && max_nongroups[class] <= 0)
1979 break;
1981 /* Consider the potential reload regs that aren't
1982 yet in use as reload regs, in order of preference.
1983 Find the most preferred one that's in this class. */
1985 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1986 if (potential_reload_regs[i] >= 0
1987 && TEST_HARD_REG_BIT (reg_class_contents[class],
1988 potential_reload_regs[i])
1989 /* If this reg will not be available for groups,
1990 pick one that does not foreclose possible groups.
1991 This is a kludge, and not very general,
1992 but it should be sufficient to make the 386 work,
1993 and the problem should not occur on machines with
1994 more registers. */
1995 && (max_nongroups[class] == 0
1996 || possible_group_p (potential_reload_regs[i], max_groups)))
1997 break;
1999 /* If we couldn't get a register, try to get one even if we
2000 might foreclose possible groups. This may cause problems
2001 later, but that's better than aborting now, since it is
2002 possible that we will, in fact, be able to form the needed
2003 group even with this allocation. */
2005 if (i >= FIRST_PSEUDO_REGISTER
2006 && (asm_noperands (max_needs[class] > 0
2007 ? max_needs_insn[class]
2008 : max_nongroups_insn[class])
2009 < 0))
2010 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2011 if (potential_reload_regs[i] >= 0
2012 && TEST_HARD_REG_BIT (reg_class_contents[class],
2013 potential_reload_regs[i]))
2014 break;
2016 /* I should be the index in potential_reload_regs
2017 of the new reload reg we have found. */
2019 if (i >= FIRST_PSEUDO_REGISTER)
2021 /* There are no possible registers left to spill. */
2022 spill_failure (max_needs[class] > 0 ? max_needs_insn[class]
2023 : max_nongroups_insn[class]);
2024 failure = 1;
2025 goto failed;
2027 else
2028 something_changed
2029 |= new_spill_reg (i, class, max_needs, max_nongroups,
2030 global, dumpfile);
2035 /* If global-alloc was run, notify it of any register eliminations we have
2036 done. */
2037 if (global)
2038 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2039 if (ep->can_eliminate)
2040 mark_elimination (ep->from, ep->to);
2042 /* Insert code to save and restore call-clobbered hard regs
2043 around calls. Tell if what mode to use so that we will process
2044 those insns in reload_as_needed if we have to. */
2046 if (caller_save_needed)
2047 save_call_clobbered_regs (num_eliminable ? QImode
2048 : caller_save_spill_class != NO_REGS ? HImode
2049 : VOIDmode);
2051 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
2052 If that insn didn't set the register (i.e., it copied the register to
2053 memory), just delete that insn instead of the equivalencing insn plus
2054 anything now dead. If we call delete_dead_insn on that insn, we may
2055 delete the insn that actually sets the register if the register die
2056 there and that is incorrect. */
2058 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2059 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0
2060 && GET_CODE (reg_equiv_init[i]) != NOTE)
2062 if (reg_set_p (regno_reg_rtx[i], PATTERN (reg_equiv_init[i])))
2063 delete_dead_insn (reg_equiv_init[i]);
2064 else
2066 PUT_CODE (reg_equiv_init[i], NOTE);
2067 NOTE_SOURCE_FILE (reg_equiv_init[i]) = 0;
2068 NOTE_LINE_NUMBER (reg_equiv_init[i]) = NOTE_INSN_DELETED;
2072 /* Use the reload registers where necessary
2073 by generating move instructions to move the must-be-register
2074 values into or out of the reload registers. */
2076 if (something_needs_reloads || something_needs_elimination
2077 || (caller_save_needed && num_eliminable)
2078 || caller_save_spill_class != NO_REGS)
2079 reload_as_needed (first, global);
2081 /* If we were able to eliminate the frame pointer, show that it is no
2082 longer live at the start of any basic block. If it ls live by
2083 virtue of being in a pseudo, that pseudo will be marked live
2084 and hence the frame pointer will be known to be live via that
2085 pseudo. */
2087 if (! frame_pointer_needed)
2088 for (i = 0; i < n_basic_blocks; i++)
2089 CLEAR_REGNO_REG_SET (basic_block_live_at_start[i],
2090 HARD_FRAME_POINTER_REGNUM);
2092 /* Come here (with failure set nonzero) if we can't get enough spill regs
2093 and we decide not to abort about it. */
2094 failed:
2096 reload_in_progress = 0;
2098 /* Now eliminate all pseudo regs by modifying them into
2099 their equivalent memory references.
2100 The REG-rtx's for the pseudos are modified in place,
2101 so all insns that used to refer to them now refer to memory.
2103 For a reg that has a reg_equiv_address, all those insns
2104 were changed by reloading so that no insns refer to it any longer;
2105 but the DECL_RTL of a variable decl may refer to it,
2106 and if so this causes the debugging info to mention the variable. */
2108 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2110 rtx addr = 0;
2111 int in_struct = 0;
2112 if (reg_equiv_mem[i])
2114 addr = XEXP (reg_equiv_mem[i], 0);
2115 in_struct = MEM_IN_STRUCT_P (reg_equiv_mem[i]);
2117 if (reg_equiv_address[i])
2118 addr = reg_equiv_address[i];
2119 if (addr)
2121 if (reg_renumber[i] < 0)
2123 rtx reg = regno_reg_rtx[i];
2124 XEXP (reg, 0) = addr;
2125 REG_USERVAR_P (reg) = 0;
2126 MEM_IN_STRUCT_P (reg) = in_struct;
2127 PUT_CODE (reg, MEM);
2129 else if (reg_equiv_mem[i])
2130 XEXP (reg_equiv_mem[i], 0) = addr;
2134 #ifdef PRESERVE_DEATH_INFO_REGNO_P
2135 /* Make a pass over all the insns and remove death notes for things that
2136 are no longer registers or no longer die in the insn (e.g., an input
2137 and output pseudo being tied). */
2139 for (insn = first; insn; insn = NEXT_INSN (insn))
2140 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2142 rtx note, next;
2144 for (note = REG_NOTES (insn); note; note = next)
2146 next = XEXP (note, 1);
2147 if (REG_NOTE_KIND (note) == REG_DEAD
2148 && (GET_CODE (XEXP (note, 0)) != REG
2149 || reg_set_p (XEXP (note, 0), PATTERN (insn))))
2150 remove_note (insn, note);
2153 #endif
2155 /* If we are doing stack checking, give a warning if this function's
2156 frame size is larger than we expect. */
2157 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
2159 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
2161 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2162 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
2163 size += UNITS_PER_WORD;
2165 if (size > STACK_CHECK_MAX_FRAME_SIZE)
2166 warning ("frame size too large for reliable stack checking");
2169 /* Indicate that we no longer have known memory locations or constants. */
2170 reg_equiv_constant = 0;
2171 reg_equiv_memory_loc = 0;
2173 if (real_known_ptr)
2174 free (real_known_ptr);
2175 if (real_at_ptr)
2176 free (real_at_ptr);
2178 if (scratch_list)
2179 free (scratch_list);
2180 scratch_list = 0;
2181 if (scratch_block)
2182 free (scratch_block);
2183 scratch_block = 0;
2185 CLEAR_HARD_REG_SET (used_spill_regs);
2186 for (i = 0; i < n_spills; i++)
2187 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
2189 return failure;
2192 /* Nonzero if, after spilling reg REGNO for non-groups,
2193 it will still be possible to find a group if we still need one. */
2195 static int
2196 possible_group_p (regno, max_groups)
2197 int regno;
2198 int *max_groups;
2200 int i;
2201 int class = (int) NO_REGS;
2203 for (i = 0; i < (int) N_REG_CLASSES; i++)
2204 if (max_groups[i] > 0)
2206 class = i;
2207 break;
2210 if (class == (int) NO_REGS)
2211 return 1;
2213 /* Consider each pair of consecutive registers. */
2214 for (i = 0; i < FIRST_PSEUDO_REGISTER - 1; i++)
2216 /* Ignore pairs that include reg REGNO. */
2217 if (i == regno || i + 1 == regno)
2218 continue;
2220 /* Ignore pairs that are outside the class that needs the group.
2221 ??? Here we fail to handle the case where two different classes
2222 independently need groups. But this never happens with our
2223 current machine descriptions. */
2224 if (! (TEST_HARD_REG_BIT (reg_class_contents[class], i)
2225 && TEST_HARD_REG_BIT (reg_class_contents[class], i + 1)))
2226 continue;
2228 /* A pair of consecutive regs we can still spill does the trick. */
2229 if (spill_reg_order[i] < 0 && spill_reg_order[i + 1] < 0
2230 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2231 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1))
2232 return 1;
2234 /* A pair of one already spilled and one we can spill does it
2235 provided the one already spilled is not otherwise reserved. */
2236 if (spill_reg_order[i] < 0
2237 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2238 && spill_reg_order[i + 1] >= 0
2239 && ! TEST_HARD_REG_BIT (counted_for_groups, i + 1)
2240 && ! TEST_HARD_REG_BIT (counted_for_nongroups, i + 1))
2241 return 1;
2242 if (spill_reg_order[i + 1] < 0
2243 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1)
2244 && spill_reg_order[i] >= 0
2245 && ! TEST_HARD_REG_BIT (counted_for_groups, i)
2246 && ! TEST_HARD_REG_BIT (counted_for_nongroups, i))
2247 return 1;
2250 return 0;
2253 /* Count any groups of CLASS that can be formed from the registers recently
2254 spilled. */
2256 static void
2257 count_possible_groups (group_size, group_mode, max_groups, class)
2258 int *group_size;
2259 enum machine_mode *group_mode;
2260 int *max_groups;
2261 int class;
2263 HARD_REG_SET new;
2264 int i, j;
2266 /* Now find all consecutive groups of spilled registers
2267 and mark each group off against the need for such groups.
2268 But don't count them against ordinary need, yet. */
2270 if (group_size[class] == 0)
2271 return;
2273 CLEAR_HARD_REG_SET (new);
2275 /* Make a mask of all the regs that are spill regs in class I. */
2276 for (i = 0; i < n_spills; i++)
2277 if (TEST_HARD_REG_BIT (reg_class_contents[class], spill_regs[i])
2278 && ! TEST_HARD_REG_BIT (counted_for_groups, spill_regs[i])
2279 && ! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
2280 SET_HARD_REG_BIT (new, spill_regs[i]);
2282 /* Find each consecutive group of them. */
2283 for (i = 0; i < FIRST_PSEUDO_REGISTER && max_groups[class] > 0; i++)
2284 if (TEST_HARD_REG_BIT (new, i)
2285 && i + group_size[class] <= FIRST_PSEUDO_REGISTER
2286 && HARD_REGNO_MODE_OK (i, group_mode[class]))
2288 for (j = 1; j < group_size[class]; j++)
2289 if (! TEST_HARD_REG_BIT (new, i + j))
2290 break;
2292 if (j == group_size[class])
2294 /* We found a group. Mark it off against this class's need for
2295 groups, and against each superclass too. */
2296 register enum reg_class *p;
2298 max_groups[class]--;
2299 p = reg_class_superclasses[class];
2300 while (*p != LIM_REG_CLASSES)
2302 if (group_size [(int) *p] <= group_size [class])
2303 max_groups[(int) *p]--;
2304 p++;
2307 /* Don't count these registers again. */
2308 for (j = 0; j < group_size[class]; j++)
2309 SET_HARD_REG_BIT (counted_for_groups, i + j);
2312 /* Skip to the last reg in this group. When i is incremented above,
2313 it will then point to the first reg of the next possible group. */
2314 i += j - 1;
2318 /* ALLOCATE_MODE is a register mode that needs to be reloaded. OTHER_MODE is
2319 another mode that needs to be reloaded for the same register class CLASS.
2320 If any reg in CLASS allows ALLOCATE_MODE but not OTHER_MODE, fail.
2321 ALLOCATE_MODE will never be smaller than OTHER_MODE.
2323 This code used to also fail if any reg in CLASS allows OTHER_MODE but not
2324 ALLOCATE_MODE. This test is unnecessary, because we will never try to put
2325 something of mode ALLOCATE_MODE into an OTHER_MODE register. Testing this
2326 causes unnecessary failures on machines requiring alignment of register
2327 groups when the two modes are different sizes, because the larger mode has
2328 more strict alignment rules than the smaller mode. */
2330 static int
2331 modes_equiv_for_class_p (allocate_mode, other_mode, class)
2332 enum machine_mode allocate_mode, other_mode;
2333 enum reg_class class;
2335 register int regno;
2336 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2338 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno)
2339 && HARD_REGNO_MODE_OK (regno, allocate_mode)
2340 && ! HARD_REGNO_MODE_OK (regno, other_mode))
2341 return 0;
2343 return 1;
2346 /* Handle the failure to find a register to spill.
2347 INSN should be one of the insns which needed this particular spill reg. */
2349 static void
2350 spill_failure (insn)
2351 rtx insn;
2353 if (asm_noperands (PATTERN (insn)) >= 0)
2354 error_for_asm (insn, "`asm' needs too many reloads");
2355 else
2356 fatal_insn ("Unable to find a register to spill.", insn);
2359 /* Add a new register to the tables of available spill-registers
2360 (as well as spilling all pseudos allocated to the register).
2361 I is the index of this register in potential_reload_regs.
2362 CLASS is the regclass whose need is being satisfied.
2363 MAX_NEEDS and MAX_NONGROUPS are the vectors of needs,
2364 so that this register can count off against them.
2365 MAX_NONGROUPS is 0 if this register is part of a group.
2366 GLOBAL and DUMPFILE are the same as the args that `reload' got. */
2368 static int
2369 new_spill_reg (i, class, max_needs, max_nongroups, global, dumpfile)
2370 int i;
2371 int class;
2372 int *max_needs;
2373 int *max_nongroups;
2374 int global;
2375 FILE *dumpfile;
2377 register enum reg_class *p;
2378 int val;
2379 int regno = potential_reload_regs[i];
2381 if (i >= FIRST_PSEUDO_REGISTER)
2382 abort (); /* Caller failed to find any register. */
2384 if (fixed_regs[regno] || TEST_HARD_REG_BIT (forbidden_regs, regno))
2386 static char *reg_class_names[] = REG_CLASS_NAMES;
2387 fatal ("fixed or forbidden register %d (%s) was spilled for class %s.\n\
2388 This may be due to a compiler bug or to impossible asm\n\
2389 statements or clauses.", regno, reg_names[regno], reg_class_names[class]);
2392 /* Make reg REGNO an additional reload reg. */
2394 potential_reload_regs[i] = -1;
2395 spill_regs[n_spills] = regno;
2396 spill_reg_order[regno] = n_spills;
2397 if (dumpfile)
2398 fprintf (dumpfile, "Spilling reg %d.\n", spill_regs[n_spills]);
2400 /* Clear off the needs we just satisfied. */
2402 max_needs[class]--;
2403 p = reg_class_superclasses[class];
2404 while (*p != LIM_REG_CLASSES)
2405 max_needs[(int) *p++]--;
2407 if (max_nongroups && max_nongroups[class] > 0)
2409 SET_HARD_REG_BIT (counted_for_nongroups, regno);
2410 max_nongroups[class]--;
2411 p = reg_class_superclasses[class];
2412 while (*p != LIM_REG_CLASSES)
2413 max_nongroups[(int) *p++]--;
2416 /* Spill every pseudo reg that was allocated to this reg
2417 or to something that overlaps this reg. */
2419 val = spill_hard_reg (spill_regs[n_spills], global, dumpfile, 0);
2421 /* If there are some registers still to eliminate and this register
2422 wasn't ever used before, additional stack space may have to be
2423 allocated to store this register. Thus, we may have changed the offset
2424 between the stack and frame pointers, so mark that something has changed.
2425 (If new pseudos were spilled, thus requiring more space, VAL would have
2426 been set non-zero by the call to spill_hard_reg above since additional
2427 reloads may be needed in that case.
2429 One might think that we need only set VAL to 1 if this is a call-used
2430 register. However, the set of registers that must be saved by the
2431 prologue is not identical to the call-used set. For example, the
2432 register used by the call insn for the return PC is a call-used register,
2433 but must be saved by the prologue. */
2434 if (num_eliminable && ! regs_ever_live[spill_regs[n_spills]])
2435 val = 1;
2437 regs_ever_live[spill_regs[n_spills]] = 1;
2438 n_spills++;
2440 return val;
2443 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2444 data that is dead in INSN. */
2446 static void
2447 delete_dead_insn (insn)
2448 rtx insn;
2450 rtx prev = prev_real_insn (insn);
2451 rtx prev_dest;
2453 /* If the previous insn sets a register that dies in our insn, delete it
2454 too. */
2455 if (prev && GET_CODE (PATTERN (prev)) == SET
2456 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
2457 && reg_mentioned_p (prev_dest, PATTERN (insn))
2458 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest)))
2459 delete_dead_insn (prev);
2461 PUT_CODE (insn, NOTE);
2462 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2463 NOTE_SOURCE_FILE (insn) = 0;
2466 /* Modify the home of pseudo-reg I.
2467 The new home is present in reg_renumber[I].
2469 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2470 or it may be -1, meaning there is none or it is not relevant.
2471 This is used so that all pseudos spilled from a given hard reg
2472 can share one stack slot. */
2474 static void
2475 alter_reg (i, from_reg)
2476 register int i;
2477 int from_reg;
2479 /* When outputting an inline function, this can happen
2480 for a reg that isn't actually used. */
2481 if (regno_reg_rtx[i] == 0)
2482 return;
2484 /* If the reg got changed to a MEM at rtl-generation time,
2485 ignore it. */
2486 if (GET_CODE (regno_reg_rtx[i]) != REG)
2487 return;
2489 /* Modify the reg-rtx to contain the new hard reg
2490 number or else to contain its pseudo reg number. */
2491 REGNO (regno_reg_rtx[i])
2492 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
2494 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2495 allocate a stack slot for it. */
2497 if (reg_renumber[i] < 0
2498 && REG_N_REFS (i) > 0
2499 && reg_equiv_constant[i] == 0
2500 && reg_equiv_memory_loc[i] == 0)
2502 register rtx x;
2503 int inherent_size = PSEUDO_REGNO_BYTES (i);
2504 int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2505 int adjust = 0;
2507 /* Each pseudo reg has an inherent size which comes from its own mode,
2508 and a total size which provides room for paradoxical subregs
2509 which refer to the pseudo reg in wider modes.
2511 We can use a slot already allocated if it provides both
2512 enough inherent space and enough total space.
2513 Otherwise, we allocate a new slot, making sure that it has no less
2514 inherent space, and no less total space, then the previous slot. */
2515 if (from_reg == -1)
2517 /* No known place to spill from => no slot to reuse. */
2518 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
2519 inherent_size == total_size ? 0 : -1);
2520 if (BYTES_BIG_ENDIAN)
2521 /* Cancel the big-endian correction done in assign_stack_local.
2522 Get the address of the beginning of the slot.
2523 This is so we can do a big-endian correction unconditionally
2524 below. */
2525 adjust = inherent_size - total_size;
2527 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2529 /* Reuse a stack slot if possible. */
2530 else if (spill_stack_slot[from_reg] != 0
2531 && spill_stack_slot_width[from_reg] >= total_size
2532 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2533 >= inherent_size))
2534 x = spill_stack_slot[from_reg];
2535 /* Allocate a bigger slot. */
2536 else
2538 /* Compute maximum size needed, both for inherent size
2539 and for total size. */
2540 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2541 rtx stack_slot;
2542 if (spill_stack_slot[from_reg])
2544 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2545 > inherent_size)
2546 mode = GET_MODE (spill_stack_slot[from_reg]);
2547 if (spill_stack_slot_width[from_reg] > total_size)
2548 total_size = spill_stack_slot_width[from_reg];
2550 /* Make a slot with that size. */
2551 x = assign_stack_local (mode, total_size,
2552 inherent_size == total_size ? 0 : -1);
2553 stack_slot = x;
2554 if (BYTES_BIG_ENDIAN)
2556 /* Cancel the big-endian correction done in assign_stack_local.
2557 Get the address of the beginning of the slot.
2558 This is so we can do a big-endian correction unconditionally
2559 below. */
2560 adjust = GET_MODE_SIZE (mode) - total_size;
2561 if (adjust)
2562 stack_slot = gen_rtx_MEM (mode_for_size (total_size
2563 * BITS_PER_UNIT,
2564 MODE_INT, 1),
2565 plus_constant (XEXP (x, 0), adjust));
2567 spill_stack_slot[from_reg] = stack_slot;
2568 spill_stack_slot_width[from_reg] = total_size;
2571 /* On a big endian machine, the "address" of the slot
2572 is the address of the low part that fits its inherent mode. */
2573 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2574 adjust += (total_size - inherent_size);
2576 /* If we have any adjustment to make, or if the stack slot is the
2577 wrong mode, make a new stack slot. */
2578 if (adjust != 0 || GET_MODE (x) != GET_MODE (regno_reg_rtx[i]))
2580 x = gen_rtx_MEM (GET_MODE (regno_reg_rtx[i]),
2581 plus_constant (XEXP (x, 0), adjust));
2582 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2585 /* Save the stack slot for later. */
2586 reg_equiv_memory_loc[i] = x;
2590 /* Mark the slots in regs_ever_live for the hard regs
2591 used by pseudo-reg number REGNO. */
2593 void
2594 mark_home_live (regno)
2595 int regno;
2597 register int i, lim;
2598 i = reg_renumber[regno];
2599 if (i < 0)
2600 return;
2601 lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2602 while (i < lim)
2603 regs_ever_live[i++] = 1;
2606 /* Mark the registers used in SCRATCH as being live. */
2608 static void
2609 mark_scratch_live (scratch)
2610 rtx scratch;
2612 register int i;
2613 int regno = REGNO (scratch);
2614 int lim = regno + HARD_REGNO_NREGS (regno, GET_MODE (scratch));
2616 for (i = regno; i < lim; i++)
2617 regs_ever_live[i] = 1;
2620 /* This function handles the tracking of elimination offsets around branches.
2622 X is a piece of RTL being scanned.
2624 INSN is the insn that it came from, if any.
2626 INITIAL_P is non-zero if we are to set the offset to be the initial
2627 offset and zero if we are setting the offset of the label to be the
2628 current offset. */
2630 static void
2631 set_label_offsets (x, insn, initial_p)
2632 rtx x;
2633 rtx insn;
2634 int initial_p;
2636 enum rtx_code code = GET_CODE (x);
2637 rtx tem;
2638 int i;
2639 struct elim_table *p;
2641 switch (code)
2643 case LABEL_REF:
2644 if (LABEL_REF_NONLOCAL_P (x))
2645 return;
2647 x = XEXP (x, 0);
2649 /* ... fall through ... */
2651 case CODE_LABEL:
2652 /* If we know nothing about this label, set the desired offsets. Note
2653 that this sets the offset at a label to be the offset before a label
2654 if we don't know anything about the label. This is not correct for
2655 the label after a BARRIER, but is the best guess we can make. If
2656 we guessed wrong, we will suppress an elimination that might have
2657 been possible had we been able to guess correctly. */
2659 if (! offsets_known_at[CODE_LABEL_NUMBER (x)])
2661 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2662 offsets_at[CODE_LABEL_NUMBER (x)][i]
2663 = (initial_p ? reg_eliminate[i].initial_offset
2664 : reg_eliminate[i].offset);
2665 offsets_known_at[CODE_LABEL_NUMBER (x)] = 1;
2668 /* Otherwise, if this is the definition of a label and it is
2669 preceded by a BARRIER, set our offsets to the known offset of
2670 that label. */
2672 else if (x == insn
2673 && (tem = prev_nonnote_insn (insn)) != 0
2674 && GET_CODE (tem) == BARRIER)
2676 num_not_at_initial_offset = 0;
2677 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2679 reg_eliminate[i].offset = reg_eliminate[i].previous_offset
2680 = offsets_at[CODE_LABEL_NUMBER (x)][i];
2681 if (reg_eliminate[i].can_eliminate
2682 && (reg_eliminate[i].offset
2683 != reg_eliminate[i].initial_offset))
2684 num_not_at_initial_offset++;
2688 else
2689 /* If neither of the above cases is true, compare each offset
2690 with those previously recorded and suppress any eliminations
2691 where the offsets disagree. */
2693 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2694 if (offsets_at[CODE_LABEL_NUMBER (x)][i]
2695 != (initial_p ? reg_eliminate[i].initial_offset
2696 : reg_eliminate[i].offset))
2697 reg_eliminate[i].can_eliminate = 0;
2699 return;
2701 case JUMP_INSN:
2702 set_label_offsets (PATTERN (insn), insn, initial_p);
2704 /* ... fall through ... */
2706 case INSN:
2707 case CALL_INSN:
2708 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2709 and hence must have all eliminations at their initial offsets. */
2710 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2711 if (REG_NOTE_KIND (tem) == REG_LABEL)
2712 set_label_offsets (XEXP (tem, 0), insn, 1);
2713 return;
2715 case ADDR_VEC:
2716 case ADDR_DIFF_VEC:
2717 /* Each of the labels in the address vector must be at their initial
2718 offsets. We want the first first for ADDR_VEC and the second
2719 field for ADDR_DIFF_VEC. */
2721 for (i = 0; i < XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2722 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2723 insn, initial_p);
2724 return;
2726 case SET:
2727 /* We only care about setting PC. If the source is not RETURN,
2728 IF_THEN_ELSE, or a label, disable any eliminations not at
2729 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2730 isn't one of those possibilities. For branches to a label,
2731 call ourselves recursively.
2733 Note that this can disable elimination unnecessarily when we have
2734 a non-local goto since it will look like a non-constant jump to
2735 someplace in the current function. This isn't a significant
2736 problem since such jumps will normally be when all elimination
2737 pairs are back to their initial offsets. */
2739 if (SET_DEST (x) != pc_rtx)
2740 return;
2742 switch (GET_CODE (SET_SRC (x)))
2744 case PC:
2745 case RETURN:
2746 return;
2748 case LABEL_REF:
2749 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2750 return;
2752 case IF_THEN_ELSE:
2753 tem = XEXP (SET_SRC (x), 1);
2754 if (GET_CODE (tem) == LABEL_REF)
2755 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2756 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2757 break;
2759 tem = XEXP (SET_SRC (x), 2);
2760 if (GET_CODE (tem) == LABEL_REF)
2761 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2762 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2763 break;
2764 return;
2766 default:
2767 break;
2770 /* If we reach here, all eliminations must be at their initial
2771 offset because we are doing a jump to a variable address. */
2772 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2773 if (p->offset != p->initial_offset)
2774 p->can_eliminate = 0;
2775 break;
2777 default:
2778 break;
2782 /* Used for communication between the next two function to properly share
2783 the vector for an ASM_OPERANDS. */
2785 static struct rtvec_def *old_asm_operands_vec, *new_asm_operands_vec;
2787 /* Scan X and replace any eliminable registers (such as fp) with a
2788 replacement (such as sp), plus an offset.
2790 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2791 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2792 MEM, we are allowed to replace a sum of a register and the constant zero
2793 with the register, which we cannot do outside a MEM. In addition, we need
2794 to record the fact that a register is referenced outside a MEM.
2796 If INSN is an insn, it is the insn containing X. If we replace a REG
2797 in a SET_DEST with an equivalent MEM and INSN is non-zero, write a
2798 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2799 that the REG is being modified.
2801 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2802 That's used when we eliminate in expressions stored in notes.
2803 This means, do not set ref_outside_mem even if the reference
2804 is outside of MEMs.
2806 If we see a modification to a register we know about, take the
2807 appropriate action (see case SET, below).
2809 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2810 replacements done assuming all offsets are at their initial values. If
2811 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2812 encounter, return the actual location so that find_reloads will do
2813 the proper thing. */
2816 eliminate_regs (x, mem_mode, insn)
2817 rtx x;
2818 enum machine_mode mem_mode;
2819 rtx insn;
2821 enum rtx_code code = GET_CODE (x);
2822 struct elim_table *ep;
2823 int regno;
2824 rtx new;
2825 int i, j;
2826 char *fmt;
2827 int copied = 0;
2829 switch (code)
2831 case CONST_INT:
2832 case CONST_DOUBLE:
2833 case CONST:
2834 case SYMBOL_REF:
2835 case CODE_LABEL:
2836 case PC:
2837 case CC0:
2838 case ASM_INPUT:
2839 case ADDR_VEC:
2840 case ADDR_DIFF_VEC:
2841 case RETURN:
2842 return x;
2844 case ADDRESSOF:
2845 /* This is only for the benefit of the debugging backends, which call
2846 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2847 removed after CSE. */
2848 new = eliminate_regs (XEXP (x, 0), 0, insn);
2849 if (GET_CODE (new) == MEM)
2850 return XEXP (new, 0);
2851 return x;
2853 case REG:
2854 regno = REGNO (x);
2856 /* First handle the case where we encounter a bare register that
2857 is eliminable. Replace it with a PLUS. */
2858 if (regno < FIRST_PSEUDO_REGISTER)
2860 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2861 ep++)
2862 if (ep->from_rtx == x && ep->can_eliminate)
2864 if (! mem_mode
2865 /* Refs inside notes don't count for this purpose. */
2866 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2867 || GET_CODE (insn) == INSN_LIST)))
2868 ep->ref_outside_mem = 1;
2869 return plus_constant (ep->to_rtx, ep->previous_offset);
2873 else if (reg_equiv_memory_loc && reg_equiv_memory_loc[regno]
2874 && (reg_equiv_address[regno] || num_not_at_initial_offset))
2876 /* In this case, find_reloads would attempt to either use an
2877 incorrect address (if something is not at its initial offset)
2878 or substitute an replaced address into an insn (which loses
2879 if the offset is changed by some later action). So we simply
2880 return the replaced stack slot (assuming it is changed by
2881 elimination) and ignore the fact that this is actually a
2882 reference to the pseudo. Ensure we make a copy of the
2883 address in case it is shared. */
2884 new = eliminate_regs (reg_equiv_memory_loc[regno], mem_mode, insn);
2885 if (new != reg_equiv_memory_loc[regno])
2887 cannot_omit_stores[regno] = 1;
2888 return copy_rtx (new);
2891 return x;
2893 case PLUS:
2894 /* If this is the sum of an eliminable register and a constant, rework
2895 the sum. */
2896 if (GET_CODE (XEXP (x, 0)) == REG
2897 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2898 && CONSTANT_P (XEXP (x, 1)))
2900 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2901 ep++)
2902 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2904 if (! mem_mode
2905 /* Refs inside notes don't count for this purpose. */
2906 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2907 || GET_CODE (insn) == INSN_LIST)))
2908 ep->ref_outside_mem = 1;
2910 /* The only time we want to replace a PLUS with a REG (this
2911 occurs when the constant operand of the PLUS is the negative
2912 of the offset) is when we are inside a MEM. We won't want
2913 to do so at other times because that would change the
2914 structure of the insn in a way that reload can't handle.
2915 We special-case the commonest situation in
2916 eliminate_regs_in_insn, so just replace a PLUS with a
2917 PLUS here, unless inside a MEM. */
2918 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2919 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2920 return ep->to_rtx;
2921 else
2922 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2923 plus_constant (XEXP (x, 1),
2924 ep->previous_offset));
2927 /* If the register is not eliminable, we are done since the other
2928 operand is a constant. */
2929 return x;
2932 /* If this is part of an address, we want to bring any constant to the
2933 outermost PLUS. We will do this by doing register replacement in
2934 our operands and seeing if a constant shows up in one of them.
2936 We assume here this is part of an address (or a "load address" insn)
2937 since an eliminable register is not likely to appear in any other
2938 context.
2940 If we have (plus (eliminable) (reg)), we want to produce
2941 (plus (plus (replacement) (reg) (const))). If this was part of a
2942 normal add insn, (plus (replacement) (reg)) will be pushed as a
2943 reload. This is the desired action. */
2946 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2947 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2949 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2951 /* If one side is a PLUS and the other side is a pseudo that
2952 didn't get a hard register but has a reg_equiv_constant,
2953 we must replace the constant here since it may no longer
2954 be in the position of any operand. */
2955 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2956 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2957 && reg_renumber[REGNO (new1)] < 0
2958 && reg_equiv_constant != 0
2959 && reg_equiv_constant[REGNO (new1)] != 0)
2960 new1 = reg_equiv_constant[REGNO (new1)];
2961 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2962 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2963 && reg_renumber[REGNO (new0)] < 0
2964 && reg_equiv_constant[REGNO (new0)] != 0)
2965 new0 = reg_equiv_constant[REGNO (new0)];
2967 new = form_sum (new0, new1);
2969 /* As above, if we are not inside a MEM we do not want to
2970 turn a PLUS into something else. We might try to do so here
2971 for an addition of 0 if we aren't optimizing. */
2972 if (! mem_mode && GET_CODE (new) != PLUS)
2973 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2974 else
2975 return new;
2978 return x;
2980 case MULT:
2981 /* If this is the product of an eliminable register and a
2982 constant, apply the distribute law and move the constant out
2983 so that we have (plus (mult ..) ..). This is needed in order
2984 to keep load-address insns valid. This case is pathological.
2985 We ignore the possibility of overflow here. */
2986 if (GET_CODE (XEXP (x, 0)) == REG
2987 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2988 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2989 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2990 ep++)
2991 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2993 if (! mem_mode
2994 /* Refs inside notes don't count for this purpose. */
2995 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2996 || GET_CODE (insn) == INSN_LIST)))
2997 ep->ref_outside_mem = 1;
2999 return
3000 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
3001 ep->previous_offset * INTVAL (XEXP (x, 1)));
3004 /* ... fall through ... */
3006 case CALL:
3007 case COMPARE:
3008 case MINUS:
3009 case DIV: case UDIV:
3010 case MOD: case UMOD:
3011 case AND: case IOR: case XOR:
3012 case ROTATERT: case ROTATE:
3013 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
3014 case NE: case EQ:
3015 case GE: case GT: case GEU: case GTU:
3016 case LE: case LT: case LEU: case LTU:
3018 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
3019 rtx new1
3020 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
3022 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
3023 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
3025 return x;
3027 case EXPR_LIST:
3028 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
3029 if (XEXP (x, 0))
3031 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
3032 if (new != XEXP (x, 0))
3033 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
3036 /* ... fall through ... */
3038 case INSN_LIST:
3039 /* Now do eliminations in the rest of the chain. If this was
3040 an EXPR_LIST, this might result in allocating more memory than is
3041 strictly needed, but it simplifies the code. */
3042 if (XEXP (x, 1))
3044 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
3045 if (new != XEXP (x, 1))
3046 return gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
3048 return x;
3050 case PRE_INC:
3051 case POST_INC:
3052 case PRE_DEC:
3053 case POST_DEC:
3054 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3055 if (ep->to_rtx == XEXP (x, 0))
3057 int size = GET_MODE_SIZE (mem_mode);
3059 /* If more bytes than MEM_MODE are pushed, account for them. */
3060 #ifdef PUSH_ROUNDING
3061 if (ep->to_rtx == stack_pointer_rtx)
3062 size = PUSH_ROUNDING (size);
3063 #endif
3064 if (code == PRE_DEC || code == POST_DEC)
3065 ep->offset += size;
3066 else
3067 ep->offset -= size;
3070 /* Fall through to generic unary operation case. */
3071 case STRICT_LOW_PART:
3072 case NEG: case NOT:
3073 case SIGN_EXTEND: case ZERO_EXTEND:
3074 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3075 case FLOAT: case FIX:
3076 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3077 case ABS:
3078 case SQRT:
3079 case FFS:
3080 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
3081 if (new != XEXP (x, 0))
3082 return gen_rtx_fmt_e (code, GET_MODE (x), new);
3083 return x;
3085 case SUBREG:
3086 /* Similar to above processing, but preserve SUBREG_WORD.
3087 Convert (subreg (mem)) to (mem) if not paradoxical.
3088 Also, if we have a non-paradoxical (subreg (pseudo)) and the
3089 pseudo didn't get a hard reg, we must replace this with the
3090 eliminated version of the memory location because push_reloads
3091 may do the replacement in certain circumstances. */
3092 if (GET_CODE (SUBREG_REG (x)) == REG
3093 && (GET_MODE_SIZE (GET_MODE (x))
3094 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3095 && reg_equiv_memory_loc != 0
3096 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
3098 new = eliminate_regs (reg_equiv_memory_loc[REGNO (SUBREG_REG (x))],
3099 mem_mode, insn);
3101 /* If we didn't change anything, we must retain the pseudo. */
3102 if (new == reg_equiv_memory_loc[REGNO (SUBREG_REG (x))])
3103 new = SUBREG_REG (x);
3104 else
3106 /* Otherwise, ensure NEW isn't shared in case we have to reload
3107 it. */
3108 new = copy_rtx (new);
3110 /* In this case, we must show that the pseudo is used in this
3111 insn so that delete_output_reload will do the right thing. */
3112 if (insn != 0 && GET_CODE (insn) != EXPR_LIST
3113 && GET_CODE (insn) != INSN_LIST)
3114 emit_insn_before (gen_rtx_USE (VOIDmode, SUBREG_REG (x)),
3115 insn);
3118 else
3119 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
3121 if (new != XEXP (x, 0))
3123 int x_size = GET_MODE_SIZE (GET_MODE (x));
3124 int new_size = GET_MODE_SIZE (GET_MODE (new));
3126 if (GET_CODE (new) == MEM
3127 && ((x_size < new_size
3128 #ifdef WORD_REGISTER_OPERATIONS
3129 /* On these machines, combine can create rtl of the form
3130 (set (subreg:m1 (reg:m2 R) 0) ...)
3131 where m1 < m2, and expects something interesting to
3132 happen to the entire word. Moreover, it will use the
3133 (reg:m2 R) later, expecting all bits to be preserved.
3134 So if the number of words is the same, preserve the
3135 subreg so that push_reloads can see it. */
3136 && ! ((x_size-1)/UNITS_PER_WORD == (new_size-1)/UNITS_PER_WORD)
3137 #endif
3139 || (x_size == new_size))
3140 #ifdef LOAD_EXTEND_OP
3141 /* On these machines we will be reloading what is
3142 inside the SUBREG if it originally was a pseudo and
3143 the inner and outer modes are both a word or
3144 smaller. So leave the SUBREG then. */
3145 && ! (GET_CODE (SUBREG_REG (x)) == REG
3146 && x_size <= UNITS_PER_WORD
3147 && new_size <= UNITS_PER_WORD
3148 && x_size > new_size
3149 && INTEGRAL_MODE_P (GET_MODE (new))
3150 && LOAD_EXTEND_OP (GET_MODE (new)) != NIL)
3151 #endif
3154 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
3155 enum machine_mode mode = GET_MODE (x);
3157 if (BYTES_BIG_ENDIAN)
3158 offset += (MIN (UNITS_PER_WORD,
3159 GET_MODE_SIZE (GET_MODE (new)))
3160 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
3162 PUT_MODE (new, mode);
3163 XEXP (new, 0) = plus_constant (XEXP (new, 0), offset);
3164 return new;
3166 else
3167 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_WORD (x));
3170 return x;
3172 case USE:
3173 /* If using a register that is the source of an eliminate we still
3174 think can be performed, note it cannot be performed since we don't
3175 know how this register is used. */
3176 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3177 if (ep->from_rtx == XEXP (x, 0))
3178 ep->can_eliminate = 0;
3180 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
3181 if (new != XEXP (x, 0))
3182 return gen_rtx_fmt_e (code, GET_MODE (x), new);
3183 return x;
3185 case CLOBBER:
3186 /* If clobbering a register that is the replacement register for an
3187 elimination we still think can be performed, note that it cannot
3188 be performed. Otherwise, we need not be concerned about it. */
3189 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3190 if (ep->to_rtx == XEXP (x, 0))
3191 ep->can_eliminate = 0;
3193 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
3194 if (new != XEXP (x, 0))
3195 return gen_rtx_fmt_e (code, GET_MODE (x), new);
3196 return x;
3198 case ASM_OPERANDS:
3200 rtx *temp_vec;
3201 /* Properly handle sharing input and constraint vectors. */
3202 if (ASM_OPERANDS_INPUT_VEC (x) != old_asm_operands_vec)
3204 /* When we come to a new vector not seen before,
3205 scan all its elements; keep the old vector if none
3206 of them changes; otherwise, make a copy. */
3207 old_asm_operands_vec = ASM_OPERANDS_INPUT_VEC (x);
3208 temp_vec = (rtx *) alloca (XVECLEN (x, 3) * sizeof (rtx));
3209 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
3210 temp_vec[i] = eliminate_regs (ASM_OPERANDS_INPUT (x, i),
3211 mem_mode, insn);
3213 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
3214 if (temp_vec[i] != ASM_OPERANDS_INPUT (x, i))
3215 break;
3217 if (i == ASM_OPERANDS_INPUT_LENGTH (x))
3218 new_asm_operands_vec = old_asm_operands_vec;
3219 else
3220 new_asm_operands_vec
3221 = gen_rtvec_v (ASM_OPERANDS_INPUT_LENGTH (x), temp_vec);
3224 /* If we had to copy the vector, copy the entire ASM_OPERANDS. */
3225 if (new_asm_operands_vec == old_asm_operands_vec)
3226 return x;
3228 new = gen_rtx_ASM_OPERANDS (VOIDmode, ASM_OPERANDS_TEMPLATE (x),
3229 ASM_OPERANDS_OUTPUT_CONSTRAINT (x),
3230 ASM_OPERANDS_OUTPUT_IDX (x),
3231 new_asm_operands_vec,
3232 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (x),
3233 ASM_OPERANDS_SOURCE_FILE (x),
3234 ASM_OPERANDS_SOURCE_LINE (x));
3235 new->volatil = x->volatil;
3236 return new;
3239 case SET:
3240 /* Check for setting a register that we know about. */
3241 if (GET_CODE (SET_DEST (x)) == REG)
3243 /* See if this is setting the replacement register for an
3244 elimination.
3246 If DEST is the hard frame pointer, we do nothing because we
3247 assume that all assignments to the frame pointer are for
3248 non-local gotos and are being done at a time when they are valid
3249 and do not disturb anything else. Some machines want to
3250 eliminate a fake argument pointer (or even a fake frame pointer)
3251 with either the real frame or the stack pointer. Assignments to
3252 the hard frame pointer must not prevent this elimination. */
3254 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3255 ep++)
3256 if (ep->to_rtx == SET_DEST (x)
3257 && SET_DEST (x) != hard_frame_pointer_rtx)
3259 /* If it is being incremented, adjust the offset. Otherwise,
3260 this elimination can't be done. */
3261 rtx src = SET_SRC (x);
3263 if (GET_CODE (src) == PLUS
3264 && XEXP (src, 0) == SET_DEST (x)
3265 && GET_CODE (XEXP (src, 1)) == CONST_INT)
3266 ep->offset -= INTVAL (XEXP (src, 1));
3267 else
3268 ep->can_eliminate = 0;
3271 /* Now check to see we are assigning to a register that can be
3272 eliminated. If so, it must be as part of a PARALLEL, since we
3273 will not have been called if this is a single SET. So indicate
3274 that we can no longer eliminate this reg. */
3275 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3276 ep++)
3277 if (ep->from_rtx == SET_DEST (x) && ep->can_eliminate)
3278 ep->can_eliminate = 0;
3281 /* Now avoid the loop below in this common case. */
3283 rtx new0 = eliminate_regs (SET_DEST (x), 0, insn);
3284 rtx new1 = eliminate_regs (SET_SRC (x), 0, insn);
3286 /* If SET_DEST changed from a REG to a MEM and INSN is an insn,
3287 write a CLOBBER insn. */
3288 if (GET_CODE (SET_DEST (x)) == REG && GET_CODE (new0) == MEM
3289 && insn != 0 && GET_CODE (insn) != EXPR_LIST
3290 && GET_CODE (insn) != INSN_LIST)
3291 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, SET_DEST (x)), insn);
3293 if (new0 != SET_DEST (x) || new1 != SET_SRC (x))
3294 return gen_rtx_SET (VOIDmode, new0, new1);
3297 return x;
3299 case MEM:
3300 /* This is only for the benefit of the debugging backends, which call
3301 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
3302 removed after CSE. */
3303 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
3304 return eliminate_regs (XEXP (XEXP (x, 0), 0), 0, insn);
3306 /* Our only special processing is to pass the mode of the MEM to our
3307 recursive call and copy the flags. While we are here, handle this
3308 case more efficiently. */
3309 new = eliminate_regs (XEXP (x, 0), GET_MODE (x), insn);
3310 if (new != XEXP (x, 0))
3312 new = gen_rtx_MEM (GET_MODE (x), new);
3313 new->volatil = x->volatil;
3314 new->unchanging = x->unchanging;
3315 new->in_struct = x->in_struct;
3316 return new;
3318 else
3319 return x;
3321 default:
3322 break;
3325 /* Process each of our operands recursively. If any have changed, make a
3326 copy of the rtx. */
3327 fmt = GET_RTX_FORMAT (code);
3328 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3330 if (*fmt == 'e')
3332 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
3333 if (new != XEXP (x, i) && ! copied)
3335 rtx new_x = rtx_alloc (code);
3336 bcopy ((char *) x, (char *) new_x,
3337 (sizeof (*new_x) - sizeof (new_x->fld)
3338 + sizeof (new_x->fld[0]) * GET_RTX_LENGTH (code)));
3339 x = new_x;
3340 copied = 1;
3342 XEXP (x, i) = new;
3344 else if (*fmt == 'E')
3346 int copied_vec = 0;
3347 for (j = 0; j < XVECLEN (x, i); j++)
3349 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
3350 if (new != XVECEXP (x, i, j) && ! copied_vec)
3352 rtvec new_v = gen_rtvec_vv (XVECLEN (x, i),
3353 XVEC (x, i)->elem);
3354 if (! copied)
3356 rtx new_x = rtx_alloc (code);
3357 bcopy ((char *) x, (char *) new_x,
3358 (sizeof (*new_x) - sizeof (new_x->fld)
3359 + (sizeof (new_x->fld[0])
3360 * GET_RTX_LENGTH (code))));
3361 x = new_x;
3362 copied = 1;
3364 XVEC (x, i) = new_v;
3365 copied_vec = 1;
3367 XVECEXP (x, i, j) = new;
3372 return x;
3375 /* Scan INSN and eliminate all eliminable registers in it.
3377 If REPLACE is nonzero, do the replacement destructively. Also
3378 delete the insn as dead it if it is setting an eliminable register.
3380 If REPLACE is zero, do all our allocations in reload_obstack.
3382 If no eliminations were done and this insn doesn't require any elimination
3383 processing (these are not identical conditions: it might be updating sp,
3384 but not referencing fp; this needs to be seen during reload_as_needed so
3385 that the offset between fp and sp can be taken into consideration), zero
3386 is returned. Otherwise, 1 is returned. */
3388 static int
3389 eliminate_regs_in_insn (insn, replace)
3390 rtx insn;
3391 int replace;
3393 rtx old_body = PATTERN (insn);
3394 rtx old_set = single_set (insn);
3395 rtx new_body;
3396 int val = 0;
3397 struct elim_table *ep;
3399 if (! replace)
3400 push_obstacks (&reload_obstack, &reload_obstack);
3402 if (old_set != 0 && GET_CODE (SET_DEST (old_set)) == REG
3403 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3405 /* Check for setting an eliminable register. */
3406 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3407 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3409 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3410 /* If this is setting the frame pointer register to the
3411 hardware frame pointer register and this is an elimination
3412 that will be done (tested above), this insn is really
3413 adjusting the frame pointer downward to compensate for
3414 the adjustment done before a nonlocal goto. */
3415 if (ep->from == FRAME_POINTER_REGNUM
3416 && ep->to == HARD_FRAME_POINTER_REGNUM)
3418 rtx src = SET_SRC (old_set);
3419 int offset, ok = 0;
3420 rtx prev_insn, prev_set;
3422 if (src == ep->to_rtx)
3423 offset = 0, ok = 1;
3424 else if (GET_CODE (src) == PLUS
3425 && GET_CODE (XEXP (src, 0)) == CONST_INT)
3426 offset = INTVAL (XEXP (src, 0)), ok = 1;
3427 else if ((prev_insn = prev_nonnote_insn (insn)) != 0
3428 && (prev_set = single_set (prev_insn)) != 0
3429 && rtx_equal_p (SET_DEST (prev_set), src))
3431 src = SET_SRC (prev_set);
3432 if (src == ep->to_rtx)
3433 offset = 0, ok = 1;
3434 else if (GET_CODE (src) == PLUS
3435 && GET_CODE (XEXP (src, 0)) == CONST_INT
3436 && XEXP (src, 1) == ep->to_rtx)
3437 offset = INTVAL (XEXP (src, 0)), ok = 1;
3438 else if (GET_CODE (src) == PLUS
3439 && GET_CODE (XEXP (src, 1)) == CONST_INT
3440 && XEXP (src, 0) == ep->to_rtx)
3441 offset = INTVAL (XEXP (src, 1)), ok = 1;
3444 if (ok)
3446 if (replace)
3448 rtx src
3449 = plus_constant (ep->to_rtx, offset - ep->offset);
3451 /* First see if this insn remains valid when we
3452 make the change. If not, keep the INSN_CODE
3453 the same and let reload fit it up. */
3454 validate_change (insn, &SET_SRC (old_set), src, 1);
3455 validate_change (insn, &SET_DEST (old_set),
3456 ep->to_rtx, 1);
3457 if (! apply_change_group ())
3459 SET_SRC (old_set) = src;
3460 SET_DEST (old_set) = ep->to_rtx;
3464 val = 1;
3465 goto done;
3468 #endif
3470 /* In this case this insn isn't serving a useful purpose. We
3471 will delete it in reload_as_needed once we know that this
3472 elimination is, in fact, being done.
3474 If REPLACE isn't set, we can't delete this insn, but needn't
3475 process it since it won't be used unless something changes. */
3476 if (replace)
3477 delete_dead_insn (insn);
3478 val = 1;
3479 goto done;
3482 /* Check for (set (reg) (plus (reg from) (offset))) where the offset
3483 in the insn is the negative of the offset in FROM. Substitute
3484 (set (reg) (reg to)) for the insn and change its code.
3486 We have to do this here, rather than in eliminate_regs, do that we can
3487 change the insn code. */
3489 if (GET_CODE (SET_SRC (old_set)) == PLUS
3490 && GET_CODE (XEXP (SET_SRC (old_set), 0)) == REG
3491 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT)
3492 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3493 ep++)
3494 if (ep->from_rtx == XEXP (SET_SRC (old_set), 0)
3495 && ep->can_eliminate)
3497 /* We must stop at the first elimination that will be used.
3498 If this one would replace the PLUS with a REG, do it
3499 now. Otherwise, quit the loop and let eliminate_regs
3500 do its normal replacement. */
3501 if (ep->offset == - INTVAL (XEXP (SET_SRC (old_set), 1)))
3503 /* We assume here that we don't need a PARALLEL of
3504 any CLOBBERs for this assignment. There's not
3505 much we can do if we do need it. */
3506 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3507 SET_DEST (old_set),
3508 ep->to_rtx);
3509 INSN_CODE (insn) = -1;
3510 val = 1;
3511 goto done;
3514 break;
3518 old_asm_operands_vec = 0;
3520 /* Replace the body of this insn with a substituted form. If we changed
3521 something, return non-zero.
3523 If we are replacing a body that was a (set X (plus Y Z)), try to
3524 re-recognize the insn. We do this in case we had a simple addition
3525 but now can do this as a load-address. This saves an insn in this
3526 common case. */
3528 new_body = eliminate_regs (old_body, 0, replace ? insn : NULL_RTX);
3529 if (new_body != old_body)
3531 /* If we aren't replacing things permanently and we changed something,
3532 make another copy to ensure that all the RTL is new. Otherwise
3533 things can go wrong if find_reload swaps commutative operands
3534 and one is inside RTL that has been copied while the other is not. */
3536 /* Don't copy an asm_operands because (1) there's no need and (2)
3537 copy_rtx can't do it properly when there are multiple outputs. */
3538 if (! replace && asm_noperands (old_body) < 0)
3539 new_body = copy_rtx (new_body);
3541 /* If we had a move insn but now we don't, rerecognize it. This will
3542 cause spurious re-recognition if the old move had a PARALLEL since
3543 the new one still will, but we can't call single_set without
3544 having put NEW_BODY into the insn and the re-recognition won't
3545 hurt in this rare case. */
3546 if (old_set != 0
3547 && ((GET_CODE (SET_SRC (old_set)) == REG
3548 && (GET_CODE (new_body) != SET
3549 || GET_CODE (SET_SRC (new_body)) != REG))
3550 /* If this was a load from or store to memory, compare
3551 the MEM in recog_operand to the one in the insn. If they
3552 are not equal, then rerecognize the insn. */
3553 || (old_set != 0
3554 && ((GET_CODE (SET_SRC (old_set)) == MEM
3555 && SET_SRC (old_set) != recog_operand[1])
3556 || (GET_CODE (SET_DEST (old_set)) == MEM
3557 && SET_DEST (old_set) != recog_operand[0])))
3558 /* If this was an add insn before, rerecognize. */
3559 || GET_CODE (SET_SRC (old_set)) == PLUS))
3561 if (! validate_change (insn, &PATTERN (insn), new_body, 0))
3562 /* If recognition fails, store the new body anyway.
3563 It's normal to have recognition failures here
3564 due to bizarre memory addresses; reloading will fix them. */
3565 PATTERN (insn) = new_body;
3567 else
3568 PATTERN (insn) = new_body;
3570 val = 1;
3573 /* Loop through all elimination pairs. See if any have changed and
3574 recalculate the number not at initial offset.
3576 Compute the maximum offset (minimum offset if the stack does not
3577 grow downward) for each elimination pair.
3579 We also detect a cases where register elimination cannot be done,
3580 namely, if a register would be both changed and referenced outside a MEM
3581 in the resulting insn since such an insn is often undefined and, even if
3582 not, we cannot know what meaning will be given to it. Note that it is
3583 valid to have a register used in an address in an insn that changes it
3584 (presumably with a pre- or post-increment or decrement).
3586 If anything changes, return nonzero. */
3588 num_not_at_initial_offset = 0;
3589 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3591 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3592 ep->can_eliminate = 0;
3594 ep->ref_outside_mem = 0;
3596 if (ep->previous_offset != ep->offset)
3597 val = 1;
3599 ep->previous_offset = ep->offset;
3600 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3601 num_not_at_initial_offset++;
3603 #ifdef STACK_GROWS_DOWNWARD
3604 ep->max_offset = MAX (ep->max_offset, ep->offset);
3605 #else
3606 ep->max_offset = MIN (ep->max_offset, ep->offset);
3607 #endif
3610 done:
3611 /* If we changed something, perform elimination in REG_NOTES. This is
3612 needed even when REPLACE is zero because a REG_DEAD note might refer
3613 to a register that we eliminate and could cause a different number
3614 of spill registers to be needed in the final reload pass than in
3615 the pre-passes. */
3616 if (val && REG_NOTES (insn) != 0)
3617 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
3619 if (! replace)
3620 pop_obstacks ();
3622 return val;
3625 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3626 replacement we currently believe is valid, mark it as not eliminable if X
3627 modifies DEST in any way other than by adding a constant integer to it.
3629 If DEST is the frame pointer, we do nothing because we assume that
3630 all assignments to the hard frame pointer are nonlocal gotos and are being
3631 done at a time when they are valid and do not disturb anything else.
3632 Some machines want to eliminate a fake argument pointer with either the
3633 frame or stack pointer. Assignments to the hard frame pointer must not
3634 prevent this elimination.
3636 Called via note_stores from reload before starting its passes to scan
3637 the insns of the function. */
3639 static void
3640 mark_not_eliminable (dest, x)
3641 rtx dest;
3642 rtx x;
3644 register int i;
3646 /* A SUBREG of a hard register here is just changing its mode. We should
3647 not see a SUBREG of an eliminable hard register, but check just in
3648 case. */
3649 if (GET_CODE (dest) == SUBREG)
3650 dest = SUBREG_REG (dest);
3652 if (dest == hard_frame_pointer_rtx)
3653 return;
3655 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3656 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3657 && (GET_CODE (x) != SET
3658 || GET_CODE (SET_SRC (x)) != PLUS
3659 || XEXP (SET_SRC (x), 0) != dest
3660 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3662 reg_eliminate[i].can_eliminate_previous
3663 = reg_eliminate[i].can_eliminate = 0;
3664 num_eliminable--;
3668 /* Kick all pseudos out of hard register REGNO.
3669 If GLOBAL is nonzero, try to find someplace else to put them.
3670 If DUMPFILE is nonzero, log actions taken on that file.
3672 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3673 because we found we can't eliminate some register. In the case, no pseudos
3674 are allowed to be in the register, even if they are only in a block that
3675 doesn't require spill registers, unlike the case when we are spilling this
3676 hard reg to produce another spill register.
3678 Return nonzero if any pseudos needed to be kicked out. */
3680 static int
3681 spill_hard_reg (regno, global, dumpfile, cant_eliminate)
3682 register int regno;
3683 int global;
3684 FILE *dumpfile;
3685 int cant_eliminate;
3687 enum reg_class class = REGNO_REG_CLASS (regno);
3688 int something_changed = 0;
3689 register int i;
3691 SET_HARD_REG_BIT (forbidden_regs, regno);
3693 if (cant_eliminate)
3694 regs_ever_live[regno] = 1;
3696 /* Spill every pseudo reg that was allocated to this reg
3697 or to something that overlaps this reg. */
3699 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3700 if (reg_renumber[i] >= 0
3701 && reg_renumber[i] <= regno
3702 && (reg_renumber[i]
3703 + HARD_REGNO_NREGS (reg_renumber[i],
3704 PSEUDO_REGNO_MODE (i))
3705 > regno))
3707 /* If this register belongs solely to a basic block which needed no
3708 spilling of any class that this register is contained in,
3709 leave it be, unless we are spilling this register because
3710 it was a hard register that can't be eliminated. */
3712 if (! cant_eliminate
3713 && basic_block_needs[0]
3714 && REG_BASIC_BLOCK (i) >= 0
3715 && basic_block_needs[(int) class][REG_BASIC_BLOCK (i)] == 0)
3717 enum reg_class *p;
3719 for (p = reg_class_superclasses[(int) class];
3720 *p != LIM_REG_CLASSES; p++)
3721 if (basic_block_needs[(int) *p][REG_BASIC_BLOCK (i)] > 0)
3722 break;
3724 if (*p == LIM_REG_CLASSES)
3725 continue;
3728 /* Mark it as no longer having a hard register home. */
3729 reg_renumber[i] = -1;
3730 /* We will need to scan everything again. */
3731 something_changed = 1;
3732 if (global)
3733 retry_global_alloc (i, forbidden_regs);
3735 alter_reg (i, regno);
3736 if (dumpfile)
3738 if (reg_renumber[i] == -1)
3739 fprintf (dumpfile, " Register %d now on stack.\n\n", i);
3740 else
3741 fprintf (dumpfile, " Register %d now in %d.\n\n",
3742 i, reg_renumber[i]);
3745 for (i = 0; i < scratch_list_length; i++)
3747 if (scratch_list[i] && REGNO (scratch_list[i]) == regno)
3749 if (! cant_eliminate && basic_block_needs[0]
3750 && ! basic_block_needs[(int) class][scratch_block[i]])
3752 enum reg_class *p;
3754 for (p = reg_class_superclasses[(int) class];
3755 *p != LIM_REG_CLASSES; p++)
3756 if (basic_block_needs[(int) *p][scratch_block[i]] > 0)
3757 break;
3759 if (*p == LIM_REG_CLASSES)
3760 continue;
3762 PUT_CODE (scratch_list[i], SCRATCH);
3763 scratch_list[i] = 0;
3764 something_changed = 1;
3765 continue;
3769 return something_changed;
3772 /* Find all paradoxical subregs within X and update reg_max_ref_width.
3773 Also mark any hard registers used to store user variables as
3774 forbidden from being used for spill registers. */
3776 static void
3777 scan_paradoxical_subregs (x)
3778 register rtx x;
3780 register int i;
3781 register char *fmt;
3782 register enum rtx_code code = GET_CODE (x);
3784 switch (code)
3786 case REG:
3787 if (SMALL_REGISTER_CLASSES && REGNO (x) < FIRST_PSEUDO_REGISTER
3788 && REG_USERVAR_P (x))
3789 SET_HARD_REG_BIT (forbidden_regs, REGNO (x));
3790 return;
3792 case CONST_INT:
3793 case CONST:
3794 case SYMBOL_REF:
3795 case LABEL_REF:
3796 case CONST_DOUBLE:
3797 case CC0:
3798 case PC:
3799 case USE:
3800 case CLOBBER:
3801 return;
3803 case SUBREG:
3804 if (GET_CODE (SUBREG_REG (x)) == REG
3805 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3806 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3807 = GET_MODE_SIZE (GET_MODE (x));
3808 return;
3810 default:
3811 break;
3814 fmt = GET_RTX_FORMAT (code);
3815 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3817 if (fmt[i] == 'e')
3818 scan_paradoxical_subregs (XEXP (x, i));
3819 else if (fmt[i] == 'E')
3821 register int j;
3822 for (j = XVECLEN (x, i) - 1; j >=0; j--)
3823 scan_paradoxical_subregs (XVECEXP (x, i, j));
3828 static int
3829 hard_reg_use_compare (p1p, p2p)
3830 const GENERIC_PTR p1p;
3831 const GENERIC_PTR p2p;
3833 struct hard_reg_n_uses *p1 = (struct hard_reg_n_uses *)p1p,
3834 *p2 = (struct hard_reg_n_uses *)p2p;
3835 int tem = p1->uses - p2->uses;
3836 if (tem != 0) return tem;
3837 /* If regs are equally good, sort by regno,
3838 so that the results of qsort leave nothing to chance. */
3839 return p1->regno - p2->regno;
3842 /* Choose the order to consider regs for use as reload registers
3843 based on how much trouble would be caused by spilling one.
3844 Store them in order of decreasing preference in potential_reload_regs. */
3846 static void
3847 order_regs_for_reload (global)
3848 int global;
3850 register int i;
3851 register int o = 0;
3852 int large = 0;
3854 struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
3856 CLEAR_HARD_REG_SET (bad_spill_regs);
3858 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3859 potential_reload_regs[i] = -1;
3861 /* Count number of uses of each hard reg by pseudo regs allocated to it
3862 and then order them by decreasing use. */
3864 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3866 hard_reg_n_uses[i].uses = 0;
3867 hard_reg_n_uses[i].regno = i;
3870 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3872 int regno = reg_renumber[i];
3873 if (regno >= 0)
3875 int lim = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
3876 while (regno < lim)
3878 /* If allocated by local-alloc, show more uses since
3879 we're not going to be able to reallocate it, but
3880 we might if allocated by global alloc. */
3881 if (global && reg_allocno[i] < 0)
3882 hard_reg_n_uses[regno].uses += (REG_N_REFS (i) + 1) / 2;
3884 hard_reg_n_uses[regno++].uses += REG_N_REFS (i);
3887 large += REG_N_REFS (i);
3890 /* Now fixed registers (which cannot safely be used for reloading)
3891 get a very high use count so they will be considered least desirable.
3892 Registers used explicitly in the rtl code are almost as bad. */
3894 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3896 if (fixed_regs[i])
3898 hard_reg_n_uses[i].uses += 2 * large + 2;
3899 SET_HARD_REG_BIT (bad_spill_regs, i);
3901 else if (regs_explicitly_used[i])
3903 hard_reg_n_uses[i].uses += large + 1;
3904 if (! SMALL_REGISTER_CLASSES)
3905 /* ??? We are doing this here because of the potential
3906 that bad code may be generated if a register explicitly
3907 used in an insn was used as a spill register for that
3908 insn. But not using these are spill registers may lose
3909 on some machine. We'll have to see how this works out. */
3910 SET_HARD_REG_BIT (bad_spill_regs, i);
3913 hard_reg_n_uses[HARD_FRAME_POINTER_REGNUM].uses += 2 * large + 2;
3914 SET_HARD_REG_BIT (bad_spill_regs, HARD_FRAME_POINTER_REGNUM);
3916 #ifdef ELIMINABLE_REGS
3917 /* If registers other than the frame pointer are eliminable, mark them as
3918 poor choices. */
3919 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3921 hard_reg_n_uses[reg_eliminate[i].from].uses += 2 * large + 2;
3922 SET_HARD_REG_BIT (bad_spill_regs, reg_eliminate[i].from);
3924 #endif
3926 /* Prefer registers not so far used, for use in temporary loading.
3927 Among them, if REG_ALLOC_ORDER is defined, use that order.
3928 Otherwise, prefer registers not preserved by calls. */
3930 #ifdef REG_ALLOC_ORDER
3931 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3933 int regno = reg_alloc_order[i];
3935 if (hard_reg_n_uses[regno].uses == 0)
3936 potential_reload_regs[o++] = regno;
3938 #else
3939 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3941 if (hard_reg_n_uses[i].uses == 0 && call_used_regs[i])
3942 potential_reload_regs[o++] = i;
3944 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3946 if (hard_reg_n_uses[i].uses == 0 && ! call_used_regs[i])
3947 potential_reload_regs[o++] = i;
3949 #endif
3951 qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
3952 sizeof hard_reg_n_uses[0], hard_reg_use_compare);
3954 /* Now add the regs that are already used,
3955 preferring those used less often. The fixed and otherwise forbidden
3956 registers will be at the end of this list. */
3958 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3959 if (hard_reg_n_uses[i].uses != 0)
3960 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
3963 /* Used in reload_as_needed to sort the spilled regs. */
3965 static int
3966 compare_spill_regs (r1p, r2p)
3967 const GENERIC_PTR r1p;
3968 const GENERIC_PTR r2p;
3970 short r1 = *(short *)r1p, r2 = *(short *)r2p;
3971 return r1 - r2;
3974 /* Reload pseudo-registers into hard regs around each insn as needed.
3975 Additional register load insns are output before the insn that needs it
3976 and perhaps store insns after insns that modify the reloaded pseudo reg.
3978 reg_last_reload_reg and reg_reloaded_contents keep track of
3979 which registers are already available in reload registers.
3980 We update these for the reloads that we perform,
3981 as the insns are scanned. */
3983 static void
3984 reload_as_needed (first, live_known)
3985 rtx first;
3986 int live_known;
3988 register rtx insn;
3989 register int i;
3990 int this_block = 0;
3991 rtx x;
3992 rtx after_call = 0;
3994 bzero ((char *) spill_reg_rtx, sizeof spill_reg_rtx);
3995 bzero ((char *) spill_reg_store, sizeof spill_reg_store);
3996 reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
3997 bzero ((char *) reg_last_reload_reg, max_regno * sizeof (rtx));
3998 reg_has_output_reload = (char *) alloca (max_regno);
3999 for (i = 0; i < n_spills; i++)
4001 reg_reloaded_contents[i] = -1;
4002 reg_reloaded_insn[i] = 0;
4005 /* Reset all offsets on eliminable registers to their initial values. */
4006 #ifdef ELIMINABLE_REGS
4007 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
4009 INITIAL_ELIMINATION_OFFSET (reg_eliminate[i].from, reg_eliminate[i].to,
4010 reg_eliminate[i].initial_offset);
4011 reg_eliminate[i].previous_offset
4012 = reg_eliminate[i].offset = reg_eliminate[i].initial_offset;
4014 #else
4015 INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
4016 reg_eliminate[0].previous_offset
4017 = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
4018 #endif
4020 num_not_at_initial_offset = 0;
4022 /* Order the spilled regs, so that allocate_reload_regs can guarantee to
4023 pack registers with group needs. */
4024 if (n_spills > 1)
4026 qsort (spill_regs, n_spills, sizeof (short), compare_spill_regs);
4027 for (i = 0; i < n_spills; i++)
4028 spill_reg_order[spill_regs[i]] = i;
4031 for (insn = first; insn;)
4033 register rtx next = NEXT_INSN (insn);
4035 /* Notice when we move to a new basic block. */
4036 if (live_known && this_block + 1 < n_basic_blocks
4037 && insn == basic_block_head[this_block+1])
4038 ++this_block;
4040 /* If we pass a label, copy the offsets from the label information
4041 into the current offsets of each elimination. */
4042 if (GET_CODE (insn) == CODE_LABEL)
4044 num_not_at_initial_offset = 0;
4045 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
4047 reg_eliminate[i].offset = reg_eliminate[i].previous_offset
4048 = offsets_at[CODE_LABEL_NUMBER (insn)][i];
4049 if (reg_eliminate[i].can_eliminate
4050 && (reg_eliminate[i].offset
4051 != reg_eliminate[i].initial_offset))
4052 num_not_at_initial_offset++;
4056 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4058 rtx avoid_return_reg = 0;
4059 rtx oldpat = PATTERN (insn);
4061 /* Set avoid_return_reg if this is an insn
4062 that might use the value of a function call. */
4063 if (SMALL_REGISTER_CLASSES && GET_CODE (insn) == CALL_INSN)
4065 if (GET_CODE (PATTERN (insn)) == SET)
4066 after_call = SET_DEST (PATTERN (insn));
4067 else if (GET_CODE (PATTERN (insn)) == PARALLEL
4068 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
4069 after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
4070 else
4071 after_call = 0;
4073 else if (SMALL_REGISTER_CLASSES && after_call != 0
4074 && !(GET_CODE (PATTERN (insn)) == SET
4075 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx))
4077 if (reg_referenced_p (after_call, PATTERN (insn)))
4078 avoid_return_reg = after_call;
4079 after_call = 0;
4082 /* If this is a USE and CLOBBER of a MEM, ensure that any
4083 references to eliminable registers have been removed. */
4085 if ((GET_CODE (PATTERN (insn)) == USE
4086 || GET_CODE (PATTERN (insn)) == CLOBBER)
4087 && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
4088 XEXP (XEXP (PATTERN (insn), 0), 0)
4089 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
4090 GET_MODE (XEXP (PATTERN (insn), 0)),
4091 NULL_RTX);
4093 /* If we need to do register elimination processing, do so.
4094 This might delete the insn, in which case we are done. */
4095 if (num_eliminable && GET_MODE (insn) == QImode)
4097 eliminate_regs_in_insn (insn, 1);
4098 if (GET_CODE (insn) == NOTE)
4100 insn = next;
4101 continue;
4105 if (GET_MODE (insn) == VOIDmode)
4106 n_reloads = 0;
4107 /* First find the pseudo regs that must be reloaded for this insn.
4108 This info is returned in the tables reload_... (see reload.h).
4109 Also modify the body of INSN by substituting RELOAD
4110 rtx's for those pseudo regs. */
4111 else
4113 bzero (reg_has_output_reload, max_regno);
4114 CLEAR_HARD_REG_SET (reg_is_output_reload);
4116 find_reloads (insn, 1, spill_indirect_levels, live_known,
4117 spill_reg_order);
4120 if (n_reloads > 0)
4122 rtx prev = PREV_INSN (insn), next = NEXT_INSN (insn);
4123 rtx p;
4124 int class;
4126 /* If this block has not had spilling done for a
4127 particular clas and we have any non-optionals that need a
4128 spill reg in that class, abort. */
4130 for (class = 0; class < N_REG_CLASSES; class++)
4131 if (basic_block_needs[class] != 0
4132 && basic_block_needs[class][this_block] == 0)
4133 for (i = 0; i < n_reloads; i++)
4134 if (class == (int) reload_reg_class[i]
4135 && reload_reg_rtx[i] == 0
4136 && ! reload_optional[i]
4137 && (reload_in[i] != 0 || reload_out[i] != 0
4138 || reload_secondary_p[i] != 0))
4139 fatal_insn ("Non-optional registers need a spill register", insn);
4141 /* Now compute which reload regs to reload them into. Perhaps
4142 reusing reload regs from previous insns, or else output
4143 load insns to reload them. Maybe output store insns too.
4144 Record the choices of reload reg in reload_reg_rtx. */
4145 choose_reload_regs (insn, avoid_return_reg);
4147 /* Merge any reloads that we didn't combine for fear of
4148 increasing the number of spill registers needed but now
4149 discover can be safely merged. */
4150 if (SMALL_REGISTER_CLASSES)
4151 merge_assigned_reloads (insn);
4153 /* Generate the insns to reload operands into or out of
4154 their reload regs. */
4155 emit_reload_insns (insn);
4157 /* Substitute the chosen reload regs from reload_reg_rtx
4158 into the insn's body (or perhaps into the bodies of other
4159 load and store insn that we just made for reloading
4160 and that we moved the structure into). */
4161 subst_reloads ();
4163 /* If this was an ASM, make sure that all the reload insns
4164 we have generated are valid. If not, give an error
4165 and delete them. */
4167 if (asm_noperands (PATTERN (insn)) >= 0)
4168 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4169 if (p != insn && GET_RTX_CLASS (GET_CODE (p)) == 'i'
4170 && (recog_memoized (p) < 0
4171 || (insn_extract (p),
4172 ! constrain_operands (INSN_CODE (p), 1))))
4174 error_for_asm (insn,
4175 "`asm' operand requires impossible reload");
4176 PUT_CODE (p, NOTE);
4177 NOTE_SOURCE_FILE (p) = 0;
4178 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
4181 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4182 is no longer validly lying around to save a future reload.
4183 Note that this does not detect pseudos that were reloaded
4184 for this insn in order to be stored in
4185 (obeying register constraints). That is correct; such reload
4186 registers ARE still valid. */
4187 note_stores (oldpat, forget_old_reloads_1);
4189 /* There may have been CLOBBER insns placed after INSN. So scan
4190 between INSN and NEXT and use them to forget old reloads. */
4191 for (x = NEXT_INSN (insn); x != next; x = NEXT_INSN (x))
4192 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
4193 note_stores (PATTERN (x), forget_old_reloads_1);
4195 #ifdef AUTO_INC_DEC
4196 /* Likewise for regs altered by auto-increment in this insn.
4197 But note that the reg-notes are not changed by reloading:
4198 they still contain the pseudo-regs, not the spill regs. */
4199 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4200 if (REG_NOTE_KIND (x) == REG_INC)
4202 /* See if this pseudo reg was reloaded in this insn.
4203 If so, its last-reload info is still valid
4204 because it is based on this insn's reload. */
4205 for (i = 0; i < n_reloads; i++)
4206 if (reload_out[i] == XEXP (x, 0))
4207 break;
4209 if (i == n_reloads)
4210 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX);
4212 #endif
4214 /* A reload reg's contents are unknown after a label. */
4215 if (GET_CODE (insn) == CODE_LABEL)
4216 for (i = 0; i < n_spills; i++)
4218 reg_reloaded_contents[i] = -1;
4219 reg_reloaded_insn[i] = 0;
4222 /* Don't assume a reload reg is still good after a call insn
4223 if it is a call-used reg. */
4224 else if (GET_CODE (insn) == CALL_INSN)
4225 for (i = 0; i < n_spills; i++)
4226 if (call_used_regs[spill_regs[i]])
4228 reg_reloaded_contents[i] = -1;
4229 reg_reloaded_insn[i] = 0;
4232 /* In case registers overlap, allow certain insns to invalidate
4233 particular hard registers. */
4235 #ifdef INSN_CLOBBERS_REGNO_P
4236 for (i = 0 ; i < n_spills ; i++)
4237 if (INSN_CLOBBERS_REGNO_P (insn, spill_regs[i]))
4239 reg_reloaded_contents[i] = -1;
4240 reg_reloaded_insn[i] = 0;
4242 #endif
4244 insn = next;
4246 #ifdef USE_C_ALLOCA
4247 alloca (0);
4248 #endif
4252 /* Discard all record of any value reloaded from X,
4253 or reloaded in X from someplace else;
4254 unless X is an output reload reg of the current insn.
4256 X may be a hard reg (the reload reg)
4257 or it may be a pseudo reg that was reloaded from. */
4259 static void
4260 forget_old_reloads_1 (x, ignored)
4261 rtx x;
4262 rtx ignored;
4264 register int regno;
4265 int nr;
4266 int offset = 0;
4268 /* note_stores does give us subregs of hard regs. */
4269 while (GET_CODE (x) == SUBREG)
4271 offset += SUBREG_WORD (x);
4272 x = SUBREG_REG (x);
4275 if (GET_CODE (x) != REG)
4276 return;
4278 regno = REGNO (x) + offset;
4280 if (regno >= FIRST_PSEUDO_REGISTER)
4281 nr = 1;
4282 else
4284 int i;
4285 nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
4286 /* Storing into a spilled-reg invalidates its contents.
4287 This can happen if a block-local pseudo is allocated to that reg
4288 and it wasn't spilled because this block's total need is 0.
4289 Then some insn might have an optional reload and use this reg. */
4290 for (i = 0; i < nr; i++)
4291 if (spill_reg_order[regno + i] >= 0
4292 /* But don't do this if the reg actually serves as an output
4293 reload reg in the current instruction. */
4294 && (n_reloads == 0
4295 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i)))
4297 reg_reloaded_contents[spill_reg_order[regno + i]] = -1;
4298 reg_reloaded_insn[spill_reg_order[regno + i]] = 0;
4302 /* Since value of X has changed,
4303 forget any value previously copied from it. */
4305 while (nr-- > 0)
4306 /* But don't forget a copy if this is the output reload
4307 that establishes the copy's validity. */
4308 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4309 reg_last_reload_reg[regno + nr] = 0;
4312 /* For each reload, the mode of the reload register. */
4313 static enum machine_mode reload_mode[MAX_RELOADS];
4315 /* For each reload, the largest number of registers it will require. */
4316 static int reload_nregs[MAX_RELOADS];
4318 /* Comparison function for qsort to decide which of two reloads
4319 should be handled first. *P1 and *P2 are the reload numbers. */
4321 static int
4322 reload_reg_class_lower (r1p, r2p)
4323 const GENERIC_PTR r1p;
4324 const GENERIC_PTR r2p;
4326 register int r1 = *(short *)r1p, r2 = *(short *)r2p;
4327 register int t;
4329 /* Consider required reloads before optional ones. */
4330 t = reload_optional[r1] - reload_optional[r2];
4331 if (t != 0)
4332 return t;
4334 /* Count all solitary classes before non-solitary ones. */
4335 t = ((reg_class_size[(int) reload_reg_class[r2]] == 1)
4336 - (reg_class_size[(int) reload_reg_class[r1]] == 1));
4337 if (t != 0)
4338 return t;
4340 /* Aside from solitaires, consider all multi-reg groups first. */
4341 t = reload_nregs[r2] - reload_nregs[r1];
4342 if (t != 0)
4343 return t;
4345 /* Consider reloads in order of increasing reg-class number. */
4346 t = (int) reload_reg_class[r1] - (int) reload_reg_class[r2];
4347 if (t != 0)
4348 return t;
4350 /* If reloads are equally urgent, sort by reload number,
4351 so that the results of qsort leave nothing to chance. */
4352 return r1 - r2;
4355 /* The following HARD_REG_SETs indicate when each hard register is
4356 used for a reload of various parts of the current insn. */
4358 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4359 static HARD_REG_SET reload_reg_used;
4360 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4361 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4362 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4363 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4364 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4365 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4366 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4367 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4368 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4369 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4370 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4371 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4372 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4373 static HARD_REG_SET reload_reg_used_in_op_addr;
4374 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4375 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4376 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4377 static HARD_REG_SET reload_reg_used_in_insn;
4378 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4379 static HARD_REG_SET reload_reg_used_in_other_addr;
4381 /* If reg is in use as a reload reg for any sort of reload. */
4382 static HARD_REG_SET reload_reg_used_at_all;
4384 /* If reg is use as an inherited reload. We just mark the first register
4385 in the group. */
4386 static HARD_REG_SET reload_reg_used_for_inherit;
4388 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4389 TYPE. MODE is used to indicate how many consecutive regs are
4390 actually used. */
4392 static void
4393 mark_reload_reg_in_use (regno, opnum, type, mode)
4394 int regno;
4395 int opnum;
4396 enum reload_type type;
4397 enum machine_mode mode;
4399 int nregs = HARD_REGNO_NREGS (regno, mode);
4400 int i;
4402 for (i = regno; i < nregs + regno; i++)
4404 switch (type)
4406 case RELOAD_OTHER:
4407 SET_HARD_REG_BIT (reload_reg_used, i);
4408 break;
4410 case RELOAD_FOR_INPUT_ADDRESS:
4411 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4412 break;
4414 case RELOAD_FOR_INPADDR_ADDRESS:
4415 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4416 break;
4418 case RELOAD_FOR_OUTPUT_ADDRESS:
4419 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4420 break;
4422 case RELOAD_FOR_OUTADDR_ADDRESS:
4423 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4424 break;
4426 case RELOAD_FOR_OPERAND_ADDRESS:
4427 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4428 break;
4430 case RELOAD_FOR_OPADDR_ADDR:
4431 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4432 break;
4434 case RELOAD_FOR_OTHER_ADDRESS:
4435 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4436 break;
4438 case RELOAD_FOR_INPUT:
4439 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4440 break;
4442 case RELOAD_FOR_OUTPUT:
4443 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4444 break;
4446 case RELOAD_FOR_INSN:
4447 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4448 break;
4451 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4455 /* Similarly, but show REGNO is no longer in use for a reload. */
4457 static void
4458 clear_reload_reg_in_use (regno, opnum, type, mode)
4459 int regno;
4460 int opnum;
4461 enum reload_type type;
4462 enum machine_mode mode;
4464 int nregs = HARD_REGNO_NREGS (regno, mode);
4465 int i;
4467 for (i = regno; i < nregs + regno; i++)
4469 switch (type)
4471 case RELOAD_OTHER:
4472 CLEAR_HARD_REG_BIT (reload_reg_used, i);
4473 break;
4475 case RELOAD_FOR_INPUT_ADDRESS:
4476 CLEAR_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4477 break;
4479 case RELOAD_FOR_INPADDR_ADDRESS:
4480 CLEAR_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4481 break;
4483 case RELOAD_FOR_OUTPUT_ADDRESS:
4484 CLEAR_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4485 break;
4487 case RELOAD_FOR_OUTADDR_ADDRESS:
4488 CLEAR_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4489 break;
4491 case RELOAD_FOR_OPERAND_ADDRESS:
4492 CLEAR_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4493 break;
4495 case RELOAD_FOR_OPADDR_ADDR:
4496 CLEAR_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4497 break;
4499 case RELOAD_FOR_OTHER_ADDRESS:
4500 CLEAR_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4501 break;
4503 case RELOAD_FOR_INPUT:
4504 CLEAR_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4505 break;
4507 case RELOAD_FOR_OUTPUT:
4508 CLEAR_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4509 break;
4511 case RELOAD_FOR_INSN:
4512 CLEAR_HARD_REG_BIT (reload_reg_used_in_insn, i);
4513 break;
4518 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4519 specified by OPNUM and TYPE. */
4521 static int
4522 reload_reg_free_p (regno, opnum, type)
4523 int regno;
4524 int opnum;
4525 enum reload_type type;
4527 int i;
4529 /* In use for a RELOAD_OTHER means it's not available for anything. */
4530 if (TEST_HARD_REG_BIT (reload_reg_used, regno))
4531 return 0;
4533 switch (type)
4535 case RELOAD_OTHER:
4536 /* In use for anything means we can't use it for RELOAD_OTHER. */
4537 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4538 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4539 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4540 return 0;
4542 for (i = 0; i < reload_n_operands; i++)
4543 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4544 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4545 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4546 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4547 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4548 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4549 return 0;
4551 return 1;
4553 case RELOAD_FOR_INPUT:
4554 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4555 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4556 return 0;
4558 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4559 return 0;
4561 /* If it is used for some other input, can't use it. */
4562 for (i = 0; i < reload_n_operands; i++)
4563 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4564 return 0;
4566 /* If it is used in a later operand's address, can't use it. */
4567 for (i = opnum + 1; i < reload_n_operands; i++)
4568 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4569 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4570 return 0;
4572 return 1;
4574 case RELOAD_FOR_INPUT_ADDRESS:
4575 /* Can't use a register if it is used for an input address for this
4576 operand or used as an input in an earlier one. */
4577 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4578 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4579 return 0;
4581 for (i = 0; i < opnum; i++)
4582 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4583 return 0;
4585 return 1;
4587 case RELOAD_FOR_INPADDR_ADDRESS:
4588 /* Can't use a register if it is used for an input address
4589 address for this operand or used as an input in an earlier
4590 one. */
4591 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4592 return 0;
4594 for (i = 0; i < opnum; i++)
4595 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4596 return 0;
4598 return 1;
4600 case RELOAD_FOR_OUTPUT_ADDRESS:
4601 /* Can't use a register if it is used for an output address for this
4602 operand or used as an output in this or a later operand. */
4603 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4604 return 0;
4606 for (i = opnum; i < reload_n_operands; i++)
4607 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4608 return 0;
4610 return 1;
4612 case RELOAD_FOR_OUTADDR_ADDRESS:
4613 /* Can't use a register if it is used for an output address
4614 address for this operand or used as an output in this or a
4615 later operand. */
4616 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4617 return 0;
4619 for (i = opnum; i < reload_n_operands; i++)
4620 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4621 return 0;
4623 return 1;
4625 case RELOAD_FOR_OPERAND_ADDRESS:
4626 for (i = 0; i < reload_n_operands; i++)
4627 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4628 return 0;
4630 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4631 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4633 case RELOAD_FOR_OPADDR_ADDR:
4634 for (i = 0; i < reload_n_operands; i++)
4635 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4636 return 0;
4638 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4640 case RELOAD_FOR_OUTPUT:
4641 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4642 outputs, or an operand address for this or an earlier output. */
4643 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4644 return 0;
4646 for (i = 0; i < reload_n_operands; i++)
4647 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4648 return 0;
4650 for (i = 0; i <= opnum; i++)
4651 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4652 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4653 return 0;
4655 return 1;
4657 case RELOAD_FOR_INSN:
4658 for (i = 0; i < reload_n_operands; i++)
4659 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4660 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4661 return 0;
4663 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4664 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4666 case RELOAD_FOR_OTHER_ADDRESS:
4667 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4669 abort ();
4672 /* Return 1 if the value in reload reg REGNO, as used by a reload
4673 needed for the part of the insn specified by OPNUM and TYPE,
4674 is not in use for a reload in any prior part of the insn.
4676 We can assume that the reload reg was already tested for availability
4677 at the time it is needed, and we should not check this again,
4678 in case the reg has already been marked in use. */
4680 static int
4681 reload_reg_free_before_p (regno, opnum, type)
4682 int regno;
4683 int opnum;
4684 enum reload_type type;
4686 int i;
4688 switch (type)
4690 case RELOAD_FOR_OTHER_ADDRESS:
4691 /* These always come first. */
4692 return 1;
4694 case RELOAD_OTHER:
4695 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4697 /* If this use is for part of the insn,
4698 check the reg is not in use for any prior part. It is tempting
4699 to try to do this by falling through from objecs that occur
4700 later in the insn to ones that occur earlier, but that will not
4701 correctly take into account the fact that here we MUST ignore
4702 things that would prevent the register from being allocated in
4703 the first place, since we know that it was allocated. */
4705 case RELOAD_FOR_OUTPUT_ADDRESS:
4706 case RELOAD_FOR_OUTADDR_ADDRESS:
4707 /* Earlier reloads are for earlier outputs or their addresses,
4708 any RELOAD_FOR_INSN reloads, any inputs or their addresses, or any
4709 RELOAD_FOR_OTHER_ADDRESS reloads (we know it can't conflict with
4710 RELOAD_OTHER).. */
4711 for (i = 0; i < opnum; i++)
4712 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4713 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4714 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4715 return 0;
4717 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4718 return 0;
4720 for (i = 0; i < reload_n_operands; i++)
4721 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4722 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4723 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4724 return 0;
4726 return (! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4727 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4728 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4730 case RELOAD_FOR_OUTPUT:
4731 /* This can't be used in the output address for this operand and
4732 anything that can't be used for it, except that we've already
4733 tested for RELOAD_FOR_INSN objects. */
4735 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno)
4736 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4737 return 0;
4739 for (i = 0; i < opnum; i++)
4740 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4741 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4742 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4743 return 0;
4745 for (i = 0; i < reload_n_operands; i++)
4746 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4747 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4748 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4749 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4750 return 0;
4752 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4754 case RELOAD_FOR_OPERAND_ADDRESS:
4755 /* Earlier reloads include RELOAD_FOR_OPADDR_ADDR reloads. */
4756 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4757 return 0;
4759 /* ... fall through ... */
4761 case RELOAD_FOR_OPADDR_ADDR:
4762 case RELOAD_FOR_INSN:
4763 /* These can't conflict with inputs, or each other, so all we have to
4764 test is input addresses and the addresses of OTHER items. */
4766 for (i = 0; i < reload_n_operands; i++)
4767 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4768 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4769 return 0;
4771 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4773 case RELOAD_FOR_INPUT:
4774 /* The only things earlier are the address for this and
4775 earlier inputs, other inputs (which we know we don't conflict
4776 with), and addresses of RELOAD_OTHER objects. */
4778 for (i = 0; i <= opnum; i++)
4779 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4780 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4781 return 0;
4783 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4785 case RELOAD_FOR_INPUT_ADDRESS:
4786 case RELOAD_FOR_INPADDR_ADDRESS:
4787 /* Similarly, all we have to check is for use in earlier inputs'
4788 addresses. */
4789 for (i = 0; i < opnum; i++)
4790 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4791 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4792 return 0;
4794 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4796 abort ();
4799 /* Return 1 if the value in reload reg REGNO, as used by a reload
4800 needed for the part of the insn specified by OPNUM and TYPE,
4801 is still available in REGNO at the end of the insn.
4803 We can assume that the reload reg was already tested for availability
4804 at the time it is needed, and we should not check this again,
4805 in case the reg has already been marked in use. */
4807 static int
4808 reload_reg_reaches_end_p (regno, opnum, type)
4809 int regno;
4810 int opnum;
4811 enum reload_type type;
4813 int i;
4815 switch (type)
4817 case RELOAD_OTHER:
4818 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4819 its value must reach the end. */
4820 return 1;
4822 /* If this use is for part of the insn,
4823 its value reaches if no subsequent part uses the same register.
4824 Just like the above function, don't try to do this with lots
4825 of fallthroughs. */
4827 case RELOAD_FOR_OTHER_ADDRESS:
4828 /* Here we check for everything else, since these don't conflict
4829 with anything else and everything comes later. */
4831 for (i = 0; i < reload_n_operands; i++)
4832 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4833 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4834 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4835 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4836 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4837 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4838 return 0;
4840 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4841 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4842 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4844 case RELOAD_FOR_INPUT_ADDRESS:
4845 case RELOAD_FOR_INPADDR_ADDRESS:
4846 /* Similar, except that we check only for this and subsequent inputs
4847 and the address of only subsequent inputs and we do not need
4848 to check for RELOAD_OTHER objects since they are known not to
4849 conflict. */
4851 for (i = opnum; i < reload_n_operands; i++)
4852 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4853 return 0;
4855 for (i = opnum + 1; i < reload_n_operands; i++)
4856 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4857 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4858 return 0;
4860 for (i = 0; i < reload_n_operands; i++)
4861 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4862 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4863 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4864 return 0;
4866 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4867 return 0;
4869 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4870 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
4872 case RELOAD_FOR_INPUT:
4873 /* Similar to input address, except we start at the next operand for
4874 both input and input address and we do not check for
4875 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4876 would conflict. */
4878 for (i = opnum + 1; i < reload_n_operands; i++)
4879 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4880 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4881 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4882 return 0;
4884 /* ... fall through ... */
4886 case RELOAD_FOR_OPERAND_ADDRESS:
4887 /* Check outputs and their addresses. */
4889 for (i = 0; i < reload_n_operands; i++)
4890 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4891 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4892 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4893 return 0;
4895 return 1;
4897 case RELOAD_FOR_OPADDR_ADDR:
4898 for (i = 0; i < reload_n_operands; i++)
4899 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4900 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4901 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4902 return 0;
4904 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4905 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
4907 case RELOAD_FOR_INSN:
4908 /* These conflict with other outputs with RELOAD_OTHER. So
4909 we need only check for output addresses. */
4911 opnum = -1;
4913 /* ... fall through ... */
4915 case RELOAD_FOR_OUTPUT:
4916 case RELOAD_FOR_OUTPUT_ADDRESS:
4917 case RELOAD_FOR_OUTADDR_ADDRESS:
4918 /* We already know these can't conflict with a later output. So the
4919 only thing to check are later output addresses. */
4920 for (i = opnum + 1; i < reload_n_operands; i++)
4921 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4922 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4923 return 0;
4925 return 1;
4928 abort ();
4931 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
4932 Return 0 otherwise.
4934 This function uses the same algorithm as reload_reg_free_p above. */
4936 static int
4937 reloads_conflict (r1, r2)
4938 int r1, r2;
4940 enum reload_type r1_type = reload_when_needed[r1];
4941 enum reload_type r2_type = reload_when_needed[r2];
4942 int r1_opnum = reload_opnum[r1];
4943 int r2_opnum = reload_opnum[r2];
4945 /* RELOAD_OTHER conflicts with everything. */
4946 if (r2_type == RELOAD_OTHER)
4947 return 1;
4949 /* Otherwise, check conflicts differently for each type. */
4951 switch (r1_type)
4953 case RELOAD_FOR_INPUT:
4954 return (r2_type == RELOAD_FOR_INSN
4955 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
4956 || r2_type == RELOAD_FOR_OPADDR_ADDR
4957 || r2_type == RELOAD_FOR_INPUT
4958 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
4959 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
4960 && r2_opnum > r1_opnum));
4962 case RELOAD_FOR_INPUT_ADDRESS:
4963 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
4964 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4966 case RELOAD_FOR_INPADDR_ADDRESS:
4967 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
4968 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4970 case RELOAD_FOR_OUTPUT_ADDRESS:
4971 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
4972 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
4974 case RELOAD_FOR_OUTADDR_ADDRESS:
4975 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
4976 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
4978 case RELOAD_FOR_OPERAND_ADDRESS:
4979 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
4980 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4982 case RELOAD_FOR_OPADDR_ADDR:
4983 return (r2_type == RELOAD_FOR_INPUT
4984 || r2_type == RELOAD_FOR_OPADDR_ADDR);
4986 case RELOAD_FOR_OUTPUT:
4987 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
4988 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
4989 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
4990 && r2_opnum >= r1_opnum));
4992 case RELOAD_FOR_INSN:
4993 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
4994 || r2_type == RELOAD_FOR_INSN
4995 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4997 case RELOAD_FOR_OTHER_ADDRESS:
4998 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
5000 case RELOAD_OTHER:
5001 return 1;
5003 default:
5004 abort ();
5008 /* Vector of reload-numbers showing the order in which the reloads should
5009 be processed. */
5010 short reload_order[MAX_RELOADS];
5012 /* Indexed by reload number, 1 if incoming value
5013 inherited from previous insns. */
5014 char reload_inherited[MAX_RELOADS];
5016 /* For an inherited reload, this is the insn the reload was inherited from,
5017 if we know it. Otherwise, this is 0. */
5018 rtx reload_inheritance_insn[MAX_RELOADS];
5020 /* If non-zero, this is a place to get the value of the reload,
5021 rather than using reload_in. */
5022 rtx reload_override_in[MAX_RELOADS];
5024 /* For each reload, the index in spill_regs of the spill register used,
5025 or -1 if we did not need one of the spill registers for this reload. */
5026 int reload_spill_index[MAX_RELOADS];
5028 /* Find a spill register to use as a reload register for reload R.
5029 LAST_RELOAD is non-zero if this is the last reload for the insn being
5030 processed.
5032 Set reload_reg_rtx[R] to the register allocated.
5034 If NOERROR is nonzero, we return 1 if successful,
5035 or 0 if we couldn't find a spill reg and we didn't change anything. */
5037 static int
5038 allocate_reload_reg (r, insn, last_reload, noerror)
5039 int r;
5040 rtx insn;
5041 int last_reload;
5042 int noerror;
5044 int i;
5045 int pass;
5046 int count;
5047 rtx new;
5048 int regno;
5050 /* If we put this reload ahead, thinking it is a group,
5051 then insist on finding a group. Otherwise we can grab a
5052 reg that some other reload needs.
5053 (That can happen when we have a 68000 DATA_OR_FP_REG
5054 which is a group of data regs or one fp reg.)
5055 We need not be so restrictive if there are no more reloads
5056 for this insn.
5058 ??? Really it would be nicer to have smarter handling
5059 for that kind of reg class, where a problem like this is normal.
5060 Perhaps those classes should be avoided for reloading
5061 by use of more alternatives. */
5063 int force_group = reload_nregs[r] > 1 && ! last_reload;
5065 /* If we want a single register and haven't yet found one,
5066 take any reg in the right class and not in use.
5067 If we want a consecutive group, here is where we look for it.
5069 We use two passes so we can first look for reload regs to
5070 reuse, which are already in use for other reloads in this insn,
5071 and only then use additional registers.
5072 I think that maximizing reuse is needed to make sure we don't
5073 run out of reload regs. Suppose we have three reloads, and
5074 reloads A and B can share regs. These need two regs.
5075 Suppose A and B are given different regs.
5076 That leaves none for C. */
5077 for (pass = 0; pass < 2; pass++)
5079 /* I is the index in spill_regs.
5080 We advance it round-robin between insns to use all spill regs
5081 equally, so that inherited reloads have a chance
5082 of leapfrogging each other. Don't do this, however, when we have
5083 group needs and failure would be fatal; if we only have a relatively
5084 small number of spill registers, and more than one of them has
5085 group needs, then by starting in the middle, we may end up
5086 allocating the first one in such a way that we are not left with
5087 sufficient groups to handle the rest. */
5089 if (noerror || ! force_group)
5090 i = last_spill_reg;
5091 else
5092 i = -1;
5094 for (count = 0; count < n_spills; count++)
5096 int class = (int) reload_reg_class[r];
5098 i = (i + 1) % n_spills;
5100 if (reload_reg_free_p (spill_regs[i], reload_opnum[r],
5101 reload_when_needed[r])
5102 && TEST_HARD_REG_BIT (reg_class_contents[class], spill_regs[i])
5103 && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
5104 /* Look first for regs to share, then for unshared. But
5105 don't share regs used for inherited reloads; they are
5106 the ones we want to preserve. */
5107 && (pass
5108 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5109 spill_regs[i])
5110 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5111 spill_regs[i]))))
5113 int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
5114 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5115 (on 68000) got us two FP regs. If NR is 1,
5116 we would reject both of them. */
5117 if (force_group)
5118 nr = CLASS_MAX_NREGS (reload_reg_class[r], reload_mode[r]);
5119 /* If we need only one reg, we have already won. */
5120 if (nr == 1)
5122 /* But reject a single reg if we demand a group. */
5123 if (force_group)
5124 continue;
5125 break;
5127 /* Otherwise check that as many consecutive regs as we need
5128 are available here.
5129 Also, don't use for a group registers that are
5130 needed for nongroups. */
5131 if (! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
5132 while (nr > 1)
5134 regno = spill_regs[i] + nr - 1;
5135 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5136 && spill_reg_order[regno] >= 0
5137 && reload_reg_free_p (regno, reload_opnum[r],
5138 reload_when_needed[r])
5139 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
5140 regno)))
5141 break;
5142 nr--;
5144 if (nr == 1)
5145 break;
5149 /* If we found something on pass 1, omit pass 2. */
5150 if (count < n_spills)
5151 break;
5154 /* We should have found a spill register by now. */
5155 if (count == n_spills)
5157 if (noerror)
5158 return 0;
5159 goto failure;
5162 /* I is the index in SPILL_REG_RTX of the reload register we are to
5163 allocate. Get an rtx for it and find its register number. */
5165 new = spill_reg_rtx[i];
5167 if (new == 0 || GET_MODE (new) != reload_mode[r])
5168 spill_reg_rtx[i] = new
5169 = gen_rtx_REG (reload_mode[r], spill_regs[i]);
5171 regno = true_regnum (new);
5173 /* Detect when the reload reg can't hold the reload mode.
5174 This used to be one `if', but Sequent compiler can't handle that. */
5175 if (HARD_REGNO_MODE_OK (regno, reload_mode[r]))
5177 enum machine_mode test_mode = VOIDmode;
5178 if (reload_in[r])
5179 test_mode = GET_MODE (reload_in[r]);
5180 /* If reload_in[r] has VOIDmode, it means we will load it
5181 in whatever mode the reload reg has: to wit, reload_mode[r].
5182 We have already tested that for validity. */
5183 /* Aside from that, we need to test that the expressions
5184 to reload from or into have modes which are valid for this
5185 reload register. Otherwise the reload insns would be invalid. */
5186 if (! (reload_in[r] != 0 && test_mode != VOIDmode
5187 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5188 if (! (reload_out[r] != 0
5189 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (reload_out[r]))))
5191 /* The reg is OK. */
5192 last_spill_reg = i;
5194 /* Mark as in use for this insn the reload regs we use
5195 for this. */
5196 mark_reload_reg_in_use (spill_regs[i], reload_opnum[r],
5197 reload_when_needed[r], reload_mode[r]);
5199 reload_reg_rtx[r] = new;
5200 reload_spill_index[r] = i;
5201 return 1;
5205 /* The reg is not OK. */
5206 if (noerror)
5207 return 0;
5209 failure:
5210 if (asm_noperands (PATTERN (insn)) < 0)
5211 /* It's the compiler's fault. */
5212 fatal_insn ("Could not find a spill register", insn);
5214 /* It's the user's fault; the operand's mode and constraint
5215 don't match. Disable this reload so we don't crash in final. */
5216 error_for_asm (insn,
5217 "`asm' operand constraint incompatible with operand size");
5218 reload_in[r] = 0;
5219 reload_out[r] = 0;
5220 reload_reg_rtx[r] = 0;
5221 reload_optional[r] = 1;
5222 reload_secondary_p[r] = 1;
5224 return 1;
5227 /* Assign hard reg targets for the pseudo-registers we must reload
5228 into hard regs for this insn.
5229 Also output the instructions to copy them in and out of the hard regs.
5231 For machines with register classes, we are responsible for
5232 finding a reload reg in the proper class. */
5234 static void
5235 choose_reload_regs (insn, avoid_return_reg)
5236 rtx insn;
5237 rtx avoid_return_reg;
5239 register int i, j;
5240 int max_group_size = 1;
5241 enum reg_class group_class = NO_REGS;
5242 int inheritance;
5244 rtx save_reload_reg_rtx[MAX_RELOADS];
5245 char save_reload_inherited[MAX_RELOADS];
5246 rtx save_reload_inheritance_insn[MAX_RELOADS];
5247 rtx save_reload_override_in[MAX_RELOADS];
5248 int save_reload_spill_index[MAX_RELOADS];
5249 HARD_REG_SET save_reload_reg_used;
5250 HARD_REG_SET save_reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
5251 HARD_REG_SET save_reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
5252 HARD_REG_SET save_reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
5253 HARD_REG_SET save_reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
5254 HARD_REG_SET save_reload_reg_used_in_input[MAX_RECOG_OPERANDS];
5255 HARD_REG_SET save_reload_reg_used_in_output[MAX_RECOG_OPERANDS];
5256 HARD_REG_SET save_reload_reg_used_in_op_addr;
5257 HARD_REG_SET save_reload_reg_used_in_op_addr_reload;
5258 HARD_REG_SET save_reload_reg_used_in_insn;
5259 HARD_REG_SET save_reload_reg_used_in_other_addr;
5260 HARD_REG_SET save_reload_reg_used_at_all;
5262 bzero (reload_inherited, MAX_RELOADS);
5263 bzero ((char *) reload_inheritance_insn, MAX_RELOADS * sizeof (rtx));
5264 bzero ((char *) reload_override_in, MAX_RELOADS * sizeof (rtx));
5266 CLEAR_HARD_REG_SET (reload_reg_used);
5267 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5268 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5269 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5270 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5271 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5273 for (i = 0; i < reload_n_operands; i++)
5275 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5276 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5277 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5278 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5279 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5280 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5283 /* Don't bother with avoiding the return reg
5284 if we have no mandatory reload that could use it. */
5285 if (SMALL_REGISTER_CLASSES && avoid_return_reg)
5287 int do_avoid = 0;
5288 int regno = REGNO (avoid_return_reg);
5289 int nregs
5290 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
5291 int r;
5293 for (r = regno; r < regno + nregs; r++)
5294 if (spill_reg_order[r] >= 0)
5295 for (j = 0; j < n_reloads; j++)
5296 if (!reload_optional[j] && reload_reg_rtx[j] == 0
5297 && (reload_in[j] != 0 || reload_out[j] != 0
5298 || reload_secondary_p[j])
5300 TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[j]], r))
5301 do_avoid = 1;
5302 if (!do_avoid)
5303 avoid_return_reg = 0;
5306 #if 0 /* Not needed, now that we can always retry without inheritance. */
5307 /* See if we have more mandatory reloads than spill regs.
5308 If so, then we cannot risk optimizations that could prevent
5309 reloads from sharing one spill register.
5311 Since we will try finding a better register than reload_reg_rtx
5312 unless it is equal to reload_in or reload_out, count such reloads. */
5315 int tem = SMALL_REGISTER_CLASSES? (avoid_return_reg != 0): 0;
5316 for (j = 0; j < n_reloads; j++)
5317 if (! reload_optional[j]
5318 && (reload_in[j] != 0 || reload_out[j] != 0 || reload_secondary_p[j])
5319 && (reload_reg_rtx[j] == 0
5320 || (! rtx_equal_p (reload_reg_rtx[j], reload_in[j])
5321 && ! rtx_equal_p (reload_reg_rtx[j], reload_out[j]))))
5322 tem++;
5323 if (tem > n_spills)
5324 must_reuse = 1;
5326 #endif
5328 /* Don't use the subroutine call return reg for a reload
5329 if we are supposed to avoid it. */
5330 if (SMALL_REGISTER_CLASSES && avoid_return_reg)
5332 int regno = REGNO (avoid_return_reg);
5333 int nregs
5334 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
5335 int r;
5337 for (r = regno; r < regno + nregs; r++)
5338 if (spill_reg_order[r] >= 0)
5339 SET_HARD_REG_BIT (reload_reg_used, r);
5342 /* In order to be certain of getting the registers we need,
5343 we must sort the reloads into order of increasing register class.
5344 Then our grabbing of reload registers will parallel the process
5345 that provided the reload registers.
5347 Also note whether any of the reloads wants a consecutive group of regs.
5348 If so, record the maximum size of the group desired and what
5349 register class contains all the groups needed by this insn. */
5351 for (j = 0; j < n_reloads; j++)
5353 reload_order[j] = j;
5354 reload_spill_index[j] = -1;
5356 reload_mode[j]
5357 = (reload_inmode[j] == VOIDmode
5358 || (GET_MODE_SIZE (reload_outmode[j])
5359 > GET_MODE_SIZE (reload_inmode[j])))
5360 ? reload_outmode[j] : reload_inmode[j];
5362 reload_nregs[j] = CLASS_MAX_NREGS (reload_reg_class[j], reload_mode[j]);
5364 if (reload_nregs[j] > 1)
5366 max_group_size = MAX (reload_nregs[j], max_group_size);
5367 group_class = reg_class_superunion[(int)reload_reg_class[j]][(int)group_class];
5370 /* If we have already decided to use a certain register,
5371 don't use it in another way. */
5372 if (reload_reg_rtx[j])
5373 mark_reload_reg_in_use (REGNO (reload_reg_rtx[j]), reload_opnum[j],
5374 reload_when_needed[j], reload_mode[j]);
5377 if (n_reloads > 1)
5378 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5380 bcopy ((char *) reload_reg_rtx, (char *) save_reload_reg_rtx,
5381 sizeof reload_reg_rtx);
5382 bcopy (reload_inherited, save_reload_inherited, sizeof reload_inherited);
5383 bcopy ((char *) reload_inheritance_insn,
5384 (char *) save_reload_inheritance_insn,
5385 sizeof reload_inheritance_insn);
5386 bcopy ((char *) reload_override_in, (char *) save_reload_override_in,
5387 sizeof reload_override_in);
5388 bcopy ((char *) reload_spill_index, (char *) save_reload_spill_index,
5389 sizeof reload_spill_index);
5390 COPY_HARD_REG_SET (save_reload_reg_used, reload_reg_used);
5391 COPY_HARD_REG_SET (save_reload_reg_used_at_all, reload_reg_used_at_all);
5392 COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr,
5393 reload_reg_used_in_op_addr);
5395 COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr_reload,
5396 reload_reg_used_in_op_addr_reload);
5398 COPY_HARD_REG_SET (save_reload_reg_used_in_insn,
5399 reload_reg_used_in_insn);
5400 COPY_HARD_REG_SET (save_reload_reg_used_in_other_addr,
5401 reload_reg_used_in_other_addr);
5403 for (i = 0; i < reload_n_operands; i++)
5405 COPY_HARD_REG_SET (save_reload_reg_used_in_output[i],
5406 reload_reg_used_in_output[i]);
5407 COPY_HARD_REG_SET (save_reload_reg_used_in_input[i],
5408 reload_reg_used_in_input[i]);
5409 COPY_HARD_REG_SET (save_reload_reg_used_in_input_addr[i],
5410 reload_reg_used_in_input_addr[i]);
5411 COPY_HARD_REG_SET (save_reload_reg_used_in_inpaddr_addr[i],
5412 reload_reg_used_in_inpaddr_addr[i]);
5413 COPY_HARD_REG_SET (save_reload_reg_used_in_output_addr[i],
5414 reload_reg_used_in_output_addr[i]);
5415 COPY_HARD_REG_SET (save_reload_reg_used_in_outaddr_addr[i],
5416 reload_reg_used_in_outaddr_addr[i]);
5419 /* If -O, try first with inheritance, then turning it off.
5420 If not -O, don't do inheritance.
5421 Using inheritance when not optimizing leads to paradoxes
5422 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5423 because one side of the comparison might be inherited. */
5425 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5427 /* Process the reloads in order of preference just found.
5428 Beyond this point, subregs can be found in reload_reg_rtx.
5430 This used to look for an existing reloaded home for all
5431 of the reloads, and only then perform any new reloads.
5432 But that could lose if the reloads were done out of reg-class order
5433 because a later reload with a looser constraint might have an old
5434 home in a register needed by an earlier reload with a tighter constraint.
5436 To solve this, we make two passes over the reloads, in the order
5437 described above. In the first pass we try to inherit a reload
5438 from a previous insn. If there is a later reload that needs a
5439 class that is a proper subset of the class being processed, we must
5440 also allocate a spill register during the first pass.
5442 Then make a second pass over the reloads to allocate any reloads
5443 that haven't been given registers yet. */
5445 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5447 for (j = 0; j < n_reloads; j++)
5449 register int r = reload_order[j];
5451 /* Ignore reloads that got marked inoperative. */
5452 if (reload_out[r] == 0 && reload_in[r] == 0
5453 && ! reload_secondary_p[r])
5454 continue;
5456 /* If find_reloads chose a to use reload_in or reload_out as a reload
5457 register, we don't need to chose one. Otherwise, try even if it
5458 found one since we might save an insn if we find the value lying
5459 around. */
5460 if (reload_in[r] != 0 && reload_reg_rtx[r] != 0
5461 && (rtx_equal_p (reload_in[r], reload_reg_rtx[r])
5462 || rtx_equal_p (reload_out[r], reload_reg_rtx[r])))
5463 continue;
5465 #if 0 /* No longer needed for correct operation.
5466 It might give better code, or might not; worth an experiment? */
5467 /* If this is an optional reload, we can't inherit from earlier insns
5468 until we are sure that any non-optional reloads have been allocated.
5469 The following code takes advantage of the fact that optional reloads
5470 are at the end of reload_order. */
5471 if (reload_optional[r] != 0)
5472 for (i = 0; i < j; i++)
5473 if ((reload_out[reload_order[i]] != 0
5474 || reload_in[reload_order[i]] != 0
5475 || reload_secondary_p[reload_order[i]])
5476 && ! reload_optional[reload_order[i]]
5477 && reload_reg_rtx[reload_order[i]] == 0)
5478 allocate_reload_reg (reload_order[i], insn, 0, inheritance);
5479 #endif
5481 /* First see if this pseudo is already available as reloaded
5482 for a previous insn. We cannot try to inherit for reloads
5483 that are smaller than the maximum number of registers needed
5484 for groups unless the register we would allocate cannot be used
5485 for the groups.
5487 We could check here to see if this is a secondary reload for
5488 an object that is already in a register of the desired class.
5489 This would avoid the need for the secondary reload register.
5490 But this is complex because we can't easily determine what
5491 objects might want to be loaded via this reload. So let a
5492 register be allocated here. In `emit_reload_insns' we suppress
5493 one of the loads in the case described above. */
5495 if (inheritance)
5497 register int regno = -1;
5498 enum machine_mode mode;
5500 if (reload_in[r] == 0)
5502 else if (GET_CODE (reload_in[r]) == REG)
5504 regno = REGNO (reload_in[r]);
5505 mode = GET_MODE (reload_in[r]);
5507 else if (GET_CODE (reload_in_reg[r]) == REG)
5509 regno = REGNO (reload_in_reg[r]);
5510 mode = GET_MODE (reload_in_reg[r]);
5512 #if 0
5513 /* This won't work, since REGNO can be a pseudo reg number.
5514 Also, it takes much more hair to keep track of all the things
5515 that can invalidate an inherited reload of part of a pseudoreg. */
5516 else if (GET_CODE (reload_in[r]) == SUBREG
5517 && GET_CODE (SUBREG_REG (reload_in[r])) == REG)
5518 regno = REGNO (SUBREG_REG (reload_in[r])) + SUBREG_WORD (reload_in[r]);
5519 #endif
5521 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5523 i = spill_reg_order[REGNO (reg_last_reload_reg[regno])];
5525 if (reg_reloaded_contents[i] == regno
5526 && (GET_MODE_SIZE (GET_MODE (reg_last_reload_reg[regno]))
5527 >= GET_MODE_SIZE (mode))
5528 && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
5529 && TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
5530 spill_regs[i])
5531 && (reload_nregs[r] == max_group_size
5532 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5533 spill_regs[i]))
5534 && reload_reg_free_p (spill_regs[i], reload_opnum[r],
5535 reload_when_needed[r])
5536 && reload_reg_free_before_p (spill_regs[i],
5537 reload_opnum[r],
5538 reload_when_needed[r]))
5540 /* If a group is needed, verify that all the subsequent
5541 registers still have their values intact. */
5542 int nr
5543 = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
5544 int k;
5546 for (k = 1; k < nr; k++)
5547 if (reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
5548 != regno)
5549 break;
5551 if (k == nr)
5553 int i1;
5555 /* We found a register that contains the
5556 value we need. If this register is the
5557 same as an `earlyclobber' operand of the
5558 current insn, just mark it as a place to
5559 reload from since we can't use it as the
5560 reload register itself. */
5562 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5563 if (reg_overlap_mentioned_for_reload_p
5564 (reg_last_reload_reg[regno],
5565 reload_earlyclobbers[i1]))
5566 break;
5568 if (i1 != n_earlyclobbers
5569 /* Don't really use the inherited spill reg
5570 if we need it wider than we've got it. */
5571 || (GET_MODE_SIZE (reload_mode[r])
5572 > GET_MODE_SIZE (mode)))
5573 reload_override_in[r] = reg_last_reload_reg[regno];
5574 else
5576 int k;
5577 /* We can use this as a reload reg. */
5578 /* Mark the register as in use for this part of
5579 the insn. */
5580 mark_reload_reg_in_use (spill_regs[i],
5581 reload_opnum[r],
5582 reload_when_needed[r],
5583 reload_mode[r]);
5584 reload_reg_rtx[r] = reg_last_reload_reg[regno];
5585 reload_inherited[r] = 1;
5586 reload_inheritance_insn[r]
5587 = reg_reloaded_insn[i];
5588 reload_spill_index[r] = i;
5589 for (k = 0; k < nr; k++)
5590 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5591 spill_regs[i + k]);
5598 /* Here's another way to see if the value is already lying around. */
5599 if (inheritance
5600 && reload_in[r] != 0
5601 && ! reload_inherited[r]
5602 && reload_out[r] == 0
5603 && (CONSTANT_P (reload_in[r])
5604 || GET_CODE (reload_in[r]) == PLUS
5605 || GET_CODE (reload_in[r]) == REG
5606 || GET_CODE (reload_in[r]) == MEM)
5607 && (reload_nregs[r] == max_group_size
5608 || ! reg_classes_intersect_p (reload_reg_class[r], group_class)))
5610 register rtx equiv
5611 = find_equiv_reg (reload_in[r], insn, reload_reg_class[r],
5612 -1, NULL_PTR, 0, reload_mode[r]);
5613 int regno;
5615 if (equiv != 0)
5617 if (GET_CODE (equiv) == REG)
5618 regno = REGNO (equiv);
5619 else if (GET_CODE (equiv) == SUBREG)
5621 /* This must be a SUBREG of a hard register.
5622 Make a new REG since this might be used in an
5623 address and not all machines support SUBREGs
5624 there. */
5625 regno = REGNO (SUBREG_REG (equiv)) + SUBREG_WORD (equiv);
5626 equiv = gen_rtx_REG (reload_mode[r], regno);
5628 else
5629 abort ();
5632 /* If we found a spill reg, reject it unless it is free
5633 and of the desired class. */
5634 if (equiv != 0
5635 && ((spill_reg_order[regno] >= 0
5636 && ! reload_reg_free_before_p (regno, reload_opnum[r],
5637 reload_when_needed[r]))
5638 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
5639 regno)))
5640 equiv = 0;
5642 if (equiv != 0 && TEST_HARD_REG_BIT (reload_reg_used_at_all, regno))
5643 equiv = 0;
5645 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, reload_mode[r]))
5646 equiv = 0;
5648 /* We found a register that contains the value we need.
5649 If this register is the same as an `earlyclobber' operand
5650 of the current insn, just mark it as a place to reload from
5651 since we can't use it as the reload register itself. */
5653 if (equiv != 0)
5654 for (i = 0; i < n_earlyclobbers; i++)
5655 if (reg_overlap_mentioned_for_reload_p (equiv,
5656 reload_earlyclobbers[i]))
5658 reload_override_in[r] = equiv;
5659 equiv = 0;
5660 break;
5663 /* JRV: If the equiv register we have found is
5664 explicitly clobbered in the current insn, mark but
5665 don't use, as above. */
5667 if (equiv != 0 && regno_clobbered_p (regno, insn))
5669 reload_override_in[r] = equiv;
5670 equiv = 0;
5673 /* If we found an equivalent reg, say no code need be generated
5674 to load it, and use it as our reload reg. */
5675 if (equiv != 0 && regno != HARD_FRAME_POINTER_REGNUM)
5677 int nr = HARD_REGNO_NREGS (regno, reload_mode[r]);
5678 int k;
5679 reload_reg_rtx[r] = equiv;
5680 reload_inherited[r] = 1;
5682 /* If any of the hard registers in EQUIV are spill
5683 registers, mark them as in use for this insn. */
5684 for (k = 0; k < nr; k++)
5686 i = spill_reg_order[regno + k];
5687 if (i >= 0)
5689 mark_reload_reg_in_use (regno, reload_opnum[r],
5690 reload_when_needed[r],
5691 reload_mode[r]);
5692 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5693 regno + k);
5699 /* If we found a register to use already, or if this is an optional
5700 reload, we are done. */
5701 if (reload_reg_rtx[r] != 0 || reload_optional[r] != 0)
5702 continue;
5704 #if 0 /* No longer needed for correct operation. Might or might not
5705 give better code on the average. Want to experiment? */
5707 /* See if there is a later reload that has a class different from our
5708 class that intersects our class or that requires less register
5709 than our reload. If so, we must allocate a register to this
5710 reload now, since that reload might inherit a previous reload
5711 and take the only available register in our class. Don't do this
5712 for optional reloads since they will force all previous reloads
5713 to be allocated. Also don't do this for reloads that have been
5714 turned off. */
5716 for (i = j + 1; i < n_reloads; i++)
5718 int s = reload_order[i];
5720 if ((reload_in[s] == 0 && reload_out[s] == 0
5721 && ! reload_secondary_p[s])
5722 || reload_optional[s])
5723 continue;
5725 if ((reload_reg_class[s] != reload_reg_class[r]
5726 && reg_classes_intersect_p (reload_reg_class[r],
5727 reload_reg_class[s]))
5728 || reload_nregs[s] < reload_nregs[r])
5729 break;
5732 if (i == n_reloads)
5733 continue;
5735 allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance);
5736 #endif
5739 /* Now allocate reload registers for anything non-optional that
5740 didn't get one yet. */
5741 for (j = 0; j < n_reloads; j++)
5743 register int r = reload_order[j];
5745 /* Ignore reloads that got marked inoperative. */
5746 if (reload_out[r] == 0 && reload_in[r] == 0 && ! reload_secondary_p[r])
5747 continue;
5749 /* Skip reloads that already have a register allocated or are
5750 optional. */
5751 if (reload_reg_rtx[r] != 0 || reload_optional[r])
5752 continue;
5754 if (! allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance))
5755 break;
5758 /* If that loop got all the way, we have won. */
5759 if (j == n_reloads)
5760 break;
5762 fail:
5763 /* Loop around and try without any inheritance. */
5764 /* First undo everything done by the failed attempt
5765 to allocate with inheritance. */
5766 bcopy ((char *) save_reload_reg_rtx, (char *) reload_reg_rtx,
5767 sizeof reload_reg_rtx);
5768 bcopy ((char *) save_reload_inherited, (char *) reload_inherited,
5769 sizeof reload_inherited);
5770 bcopy ((char *) save_reload_inheritance_insn,
5771 (char *) reload_inheritance_insn,
5772 sizeof reload_inheritance_insn);
5773 bcopy ((char *) save_reload_override_in, (char *) reload_override_in,
5774 sizeof reload_override_in);
5775 bcopy ((char *) save_reload_spill_index, (char *) reload_spill_index,
5776 sizeof reload_spill_index);
5777 COPY_HARD_REG_SET (reload_reg_used, save_reload_reg_used);
5778 COPY_HARD_REG_SET (reload_reg_used_at_all, save_reload_reg_used_at_all);
5779 COPY_HARD_REG_SET (reload_reg_used_in_op_addr,
5780 save_reload_reg_used_in_op_addr);
5781 COPY_HARD_REG_SET (reload_reg_used_in_op_addr_reload,
5782 save_reload_reg_used_in_op_addr_reload);
5783 COPY_HARD_REG_SET (reload_reg_used_in_insn,
5784 save_reload_reg_used_in_insn);
5785 COPY_HARD_REG_SET (reload_reg_used_in_other_addr,
5786 save_reload_reg_used_in_other_addr);
5788 for (i = 0; i < reload_n_operands; i++)
5790 COPY_HARD_REG_SET (reload_reg_used_in_input[i],
5791 save_reload_reg_used_in_input[i]);
5792 COPY_HARD_REG_SET (reload_reg_used_in_output[i],
5793 save_reload_reg_used_in_output[i]);
5794 COPY_HARD_REG_SET (reload_reg_used_in_input_addr[i],
5795 save_reload_reg_used_in_input_addr[i]);
5796 COPY_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i],
5797 save_reload_reg_used_in_inpaddr_addr[i]);
5798 COPY_HARD_REG_SET (reload_reg_used_in_output_addr[i],
5799 save_reload_reg_used_in_output_addr[i]);
5800 COPY_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i],
5801 save_reload_reg_used_in_outaddr_addr[i]);
5805 /* If we thought we could inherit a reload, because it seemed that
5806 nothing else wanted the same reload register earlier in the insn,
5807 verify that assumption, now that all reloads have been assigned. */
5809 for (j = 0; j < n_reloads; j++)
5811 register int r = reload_order[j];
5813 if (reload_inherited[r] && reload_reg_rtx[r] != 0
5814 && ! reload_reg_free_before_p (true_regnum (reload_reg_rtx[r]),
5815 reload_opnum[r],
5816 reload_when_needed[r]))
5817 reload_inherited[r] = 0;
5819 /* If we found a better place to reload from,
5820 validate it in the same fashion, if it is a reload reg. */
5821 if (reload_override_in[r]
5822 && (GET_CODE (reload_override_in[r]) == REG
5823 || GET_CODE (reload_override_in[r]) == SUBREG))
5825 int regno = true_regnum (reload_override_in[r]);
5826 if (spill_reg_order[regno] >= 0
5827 && ! reload_reg_free_before_p (regno, reload_opnum[r],
5828 reload_when_needed[r]))
5829 reload_override_in[r] = 0;
5833 /* Now that reload_override_in is known valid,
5834 actually override reload_in. */
5835 for (j = 0; j < n_reloads; j++)
5836 if (reload_override_in[j])
5837 reload_in[j] = reload_override_in[j];
5839 /* If this reload won't be done because it has been cancelled or is
5840 optional and not inherited, clear reload_reg_rtx so other
5841 routines (such as subst_reloads) don't get confused. */
5842 for (j = 0; j < n_reloads; j++)
5843 if (reload_reg_rtx[j] != 0
5844 && ((reload_optional[j] && ! reload_inherited[j])
5845 || (reload_in[j] == 0 && reload_out[j] == 0
5846 && ! reload_secondary_p[j])))
5848 int regno = true_regnum (reload_reg_rtx[j]);
5850 if (spill_reg_order[regno] >= 0)
5851 clear_reload_reg_in_use (regno, reload_opnum[j],
5852 reload_when_needed[j], reload_mode[j]);
5853 reload_reg_rtx[j] = 0;
5856 /* Record which pseudos and which spill regs have output reloads. */
5857 for (j = 0; j < n_reloads; j++)
5859 register int r = reload_order[j];
5861 i = reload_spill_index[r];
5863 /* I is nonneg if this reload used one of the spill regs.
5864 If reload_reg_rtx[r] is 0, this is an optional reload
5865 that we opted to ignore. */
5866 if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG
5867 && reload_reg_rtx[r] != 0)
5869 register int nregno = REGNO (reload_out[r]);
5870 int nr = 1;
5872 if (nregno < FIRST_PSEUDO_REGISTER)
5873 nr = HARD_REGNO_NREGS (nregno, reload_mode[r]);
5875 while (--nr >= 0)
5876 reg_has_output_reload[nregno + nr] = 1;
5878 if (i >= 0)
5880 nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
5881 while (--nr >= 0)
5882 SET_HARD_REG_BIT (reg_is_output_reload, spill_regs[i] + nr);
5885 if (reload_when_needed[r] != RELOAD_OTHER
5886 && reload_when_needed[r] != RELOAD_FOR_OUTPUT
5887 && reload_when_needed[r] != RELOAD_FOR_INSN)
5888 abort ();
5893 /* If SMALL_REGISTER_CLASSES is non-zero, we may not have merged two
5894 reloads of the same item for fear that we might not have enough reload
5895 registers. However, normally they will get the same reload register
5896 and hence actually need not be loaded twice.
5898 Here we check for the most common case of this phenomenon: when we have
5899 a number of reloads for the same object, each of which were allocated
5900 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
5901 reload, and is not modified in the insn itself. If we find such,
5902 merge all the reloads and set the resulting reload to RELOAD_OTHER.
5903 This will not increase the number of spill registers needed and will
5904 prevent redundant code. */
5906 static void
5907 merge_assigned_reloads (insn)
5908 rtx insn;
5910 int i, j;
5912 /* Scan all the reloads looking for ones that only load values and
5913 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
5914 assigned and not modified by INSN. */
5916 for (i = 0; i < n_reloads; i++)
5918 if (reload_in[i] == 0 || reload_when_needed[i] == RELOAD_OTHER
5919 || reload_out[i] != 0 || reload_reg_rtx[i] == 0
5920 || reg_set_p (reload_reg_rtx[i], insn))
5921 continue;
5923 /* Look at all other reloads. Ensure that the only use of this
5924 reload_reg_rtx is in a reload that just loads the same value
5925 as we do. Note that any secondary reloads must be of the identical
5926 class since the values, modes, and result registers are the
5927 same, so we need not do anything with any secondary reloads. */
5929 for (j = 0; j < n_reloads; j++)
5931 if (i == j || reload_reg_rtx[j] == 0
5932 || ! reg_overlap_mentioned_p (reload_reg_rtx[j],
5933 reload_reg_rtx[i]))
5934 continue;
5936 /* If the reload regs aren't exactly the same (e.g, different modes)
5937 or if the values are different, we can't merge anything with this
5938 reload register. */
5940 if (! rtx_equal_p (reload_reg_rtx[i], reload_reg_rtx[j])
5941 || reload_out[j] != 0 || reload_in[j] == 0
5942 || ! rtx_equal_p (reload_in[i], reload_in[j]))
5943 break;
5946 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
5947 we, in fact, found any matching reloads. */
5949 if (j == n_reloads)
5951 for (j = 0; j < n_reloads; j++)
5952 if (i != j && reload_reg_rtx[j] != 0
5953 && rtx_equal_p (reload_reg_rtx[i], reload_reg_rtx[j]))
5955 reload_when_needed[i] = RELOAD_OTHER;
5956 reload_in[j] = 0;
5957 transfer_replacements (i, j);
5960 /* If this is now RELOAD_OTHER, look for any reloads that load
5961 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
5962 if they were for inputs, RELOAD_OTHER for outputs. Note that
5963 this test is equivalent to looking for reloads for this operand
5964 number. */
5966 if (reload_when_needed[i] == RELOAD_OTHER)
5967 for (j = 0; j < n_reloads; j++)
5968 if (reload_in[j] != 0
5969 && reload_when_needed[i] != RELOAD_OTHER
5970 && reg_overlap_mentioned_for_reload_p (reload_in[j],
5971 reload_in[i]))
5972 reload_when_needed[j]
5973 = ((reload_when_needed[i] == RELOAD_FOR_INPUT_ADDRESS
5974 || reload_when_needed[i] == RELOAD_FOR_INPADDR_ADDRESS)
5975 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
5981 /* Output insns to reload values in and out of the chosen reload regs. */
5983 static void
5984 emit_reload_insns (insn)
5985 rtx insn;
5987 register int j;
5988 rtx input_reload_insns[MAX_RECOG_OPERANDS];
5989 rtx other_input_address_reload_insns = 0;
5990 rtx other_input_reload_insns = 0;
5991 rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
5992 rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
5993 rtx output_reload_insns[MAX_RECOG_OPERANDS];
5994 rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
5995 rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
5996 rtx operand_reload_insns = 0;
5997 rtx other_operand_reload_insns = 0;
5998 rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
5999 rtx following_insn = NEXT_INSN (insn);
6000 rtx before_insn = insn;
6001 int special;
6002 /* Values to be put in spill_reg_store are put here first. */
6003 rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6005 for (j = 0; j < reload_n_operands; j++)
6006 input_reload_insns[j] = input_address_reload_insns[j]
6007 = inpaddr_address_reload_insns[j]
6008 = output_reload_insns[j] = output_address_reload_insns[j]
6009 = outaddr_address_reload_insns[j]
6010 = other_output_reload_insns[j] = 0;
6012 /* Now output the instructions to copy the data into and out of the
6013 reload registers. Do these in the order that the reloads were reported,
6014 since reloads of base and index registers precede reloads of operands
6015 and the operands may need the base and index registers reloaded. */
6017 for (j = 0; j < n_reloads; j++)
6019 register rtx old;
6020 rtx oldequiv_reg = 0;
6021 rtx this_reload_insn = 0;
6023 if (reload_spill_index[j] >= 0)
6024 new_spill_reg_store[reload_spill_index[j]] = 0;
6026 old = reload_in[j];
6027 if (old != 0 && ! reload_inherited[j]
6028 && ! rtx_equal_p (reload_reg_rtx[j], old)
6029 && reload_reg_rtx[j] != 0)
6031 register rtx reloadreg = reload_reg_rtx[j];
6032 rtx oldequiv = 0;
6033 enum machine_mode mode;
6034 rtx *where;
6036 /* Determine the mode to reload in.
6037 This is very tricky because we have three to choose from.
6038 There is the mode the insn operand wants (reload_inmode[J]).
6039 There is the mode of the reload register RELOADREG.
6040 There is the intrinsic mode of the operand, which we could find
6041 by stripping some SUBREGs.
6042 It turns out that RELOADREG's mode is irrelevant:
6043 we can change that arbitrarily.
6045 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6046 then the reload reg may not support QImode moves, so use SImode.
6047 If foo is in memory due to spilling a pseudo reg, this is safe,
6048 because the QImode value is in the least significant part of a
6049 slot big enough for a SImode. If foo is some other sort of
6050 memory reference, then it is impossible to reload this case,
6051 so previous passes had better make sure this never happens.
6053 Then consider a one-word union which has SImode and one of its
6054 members is a float, being fetched as (SUBREG:SF union:SI).
6055 We must fetch that as SFmode because we could be loading into
6056 a float-only register. In this case OLD's mode is correct.
6058 Consider an immediate integer: it has VOIDmode. Here we need
6059 to get a mode from something else.
6061 In some cases, there is a fourth mode, the operand's
6062 containing mode. If the insn specifies a containing mode for
6063 this operand, it overrides all others.
6065 I am not sure whether the algorithm here is always right,
6066 but it does the right things in those cases. */
6068 mode = GET_MODE (old);
6069 if (mode == VOIDmode)
6070 mode = reload_inmode[j];
6072 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6073 /* If we need a secondary register for this operation, see if
6074 the value is already in a register in that class. Don't
6075 do this if the secondary register will be used as a scratch
6076 register. */
6078 if (reload_secondary_in_reload[j] >= 0
6079 && reload_secondary_in_icode[j] == CODE_FOR_nothing
6080 && optimize)
6081 oldequiv
6082 = find_equiv_reg (old, insn,
6083 reload_reg_class[reload_secondary_in_reload[j]],
6084 -1, NULL_PTR, 0, mode);
6085 #endif
6087 /* If reloading from memory, see if there is a register
6088 that already holds the same value. If so, reload from there.
6089 We can pass 0 as the reload_reg_p argument because
6090 any other reload has either already been emitted,
6091 in which case find_equiv_reg will see the reload-insn,
6092 or has yet to be emitted, in which case it doesn't matter
6093 because we will use this equiv reg right away. */
6095 if (oldequiv == 0 && optimize
6096 && (GET_CODE (old) == MEM
6097 || (GET_CODE (old) == REG
6098 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6099 && reg_renumber[REGNO (old)] < 0)))
6100 oldequiv = find_equiv_reg (old, insn, ALL_REGS,
6101 -1, NULL_PTR, 0, mode);
6103 if (oldequiv)
6105 int regno = true_regnum (oldequiv);
6107 /* If OLDEQUIV is a spill register, don't use it for this
6108 if any other reload needs it at an earlier stage of this insn
6109 or at this stage. */
6110 if (spill_reg_order[regno] >= 0
6111 && (! reload_reg_free_p (regno, reload_opnum[j],
6112 reload_when_needed[j])
6113 || ! reload_reg_free_before_p (regno, reload_opnum[j],
6114 reload_when_needed[j])))
6115 oldequiv = 0;
6117 /* If OLDEQUIV is not a spill register,
6118 don't use it if any other reload wants it. */
6119 if (spill_reg_order[regno] < 0)
6121 int k;
6122 for (k = 0; k < n_reloads; k++)
6123 if (reload_reg_rtx[k] != 0 && k != j
6124 && reg_overlap_mentioned_for_reload_p (reload_reg_rtx[k],
6125 oldequiv))
6127 oldequiv = 0;
6128 break;
6132 /* If it is no cheaper to copy from OLDEQUIV into the
6133 reload register than it would be to move from memory,
6134 don't use it. Likewise, if we need a secondary register
6135 or memory. */
6137 if (oldequiv != 0
6138 && ((REGNO_REG_CLASS (regno) != reload_reg_class[j]
6139 && (REGISTER_MOVE_COST (REGNO_REG_CLASS (regno),
6140 reload_reg_class[j])
6141 >= MEMORY_MOVE_COST (mode)))
6142 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6143 || (SECONDARY_INPUT_RELOAD_CLASS (reload_reg_class[j],
6144 mode, oldequiv)
6145 != NO_REGS)
6146 #endif
6147 #ifdef SECONDARY_MEMORY_NEEDED
6148 || SECONDARY_MEMORY_NEEDED (reload_reg_class[j],
6149 REGNO_REG_CLASS (regno),
6150 mode)
6151 #endif
6153 oldequiv = 0;
6156 if (oldequiv == 0)
6157 oldequiv = old;
6158 else if (GET_CODE (oldequiv) == REG)
6159 oldequiv_reg = oldequiv;
6160 else if (GET_CODE (oldequiv) == SUBREG)
6161 oldequiv_reg = SUBREG_REG (oldequiv);
6163 /* If we are reloading from a register that was recently stored in
6164 with an output-reload, see if we can prove there was
6165 actually no need to store the old value in it. */
6167 if (optimize && GET_CODE (oldequiv) == REG
6168 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6169 && spill_reg_order[REGNO (oldequiv)] >= 0
6170 && spill_reg_store[spill_reg_order[REGNO (oldequiv)]] != 0
6171 && find_reg_note (insn, REG_DEAD, reload_in[j])
6172 /* This is unsafe if operand occurs more than once in current
6173 insn. Perhaps some occurrences weren't reloaded. */
6174 && count_occurrences (PATTERN (insn), reload_in[j]) == 1)
6175 delete_output_reload
6176 (insn, j, spill_reg_store[spill_reg_order[REGNO (oldequiv)]]);
6178 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6179 then load RELOADREG from OLDEQUIV. Note that we cannot use
6180 gen_lowpart_common since it can do the wrong thing when
6181 RELOADREG has a multi-word mode. Note that RELOADREG
6182 must always be a REG here. */
6184 if (GET_MODE (reloadreg) != mode)
6185 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6186 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6187 oldequiv = SUBREG_REG (oldequiv);
6188 if (GET_MODE (oldequiv) != VOIDmode
6189 && mode != GET_MODE (oldequiv))
6190 oldequiv = gen_rtx_SUBREG (mode, oldequiv, 0);
6192 /* Switch to the right place to emit the reload insns. */
6193 switch (reload_when_needed[j])
6195 case RELOAD_OTHER:
6196 where = &other_input_reload_insns;
6197 break;
6198 case RELOAD_FOR_INPUT:
6199 where = &input_reload_insns[reload_opnum[j]];
6200 break;
6201 case RELOAD_FOR_INPUT_ADDRESS:
6202 where = &input_address_reload_insns[reload_opnum[j]];
6203 break;
6204 case RELOAD_FOR_INPADDR_ADDRESS:
6205 where = &inpaddr_address_reload_insns[reload_opnum[j]];
6206 break;
6207 case RELOAD_FOR_OUTPUT_ADDRESS:
6208 where = &output_address_reload_insns[reload_opnum[j]];
6209 break;
6210 case RELOAD_FOR_OUTADDR_ADDRESS:
6211 where = &outaddr_address_reload_insns[reload_opnum[j]];
6212 break;
6213 case RELOAD_FOR_OPERAND_ADDRESS:
6214 where = &operand_reload_insns;
6215 break;
6216 case RELOAD_FOR_OPADDR_ADDR:
6217 where = &other_operand_reload_insns;
6218 break;
6219 case RELOAD_FOR_OTHER_ADDRESS:
6220 where = &other_input_address_reload_insns;
6221 break;
6222 default:
6223 abort ();
6226 push_to_sequence (*where);
6227 special = 0;
6229 /* Auto-increment addresses must be reloaded in a special way. */
6230 if (GET_CODE (oldequiv) == POST_INC
6231 || GET_CODE (oldequiv) == POST_DEC
6232 || GET_CODE (oldequiv) == PRE_INC
6233 || GET_CODE (oldequiv) == PRE_DEC)
6235 /* We are not going to bother supporting the case where a
6236 incremented register can't be copied directly from
6237 OLDEQUIV since this seems highly unlikely. */
6238 if (reload_secondary_in_reload[j] >= 0)
6239 abort ();
6240 /* Prevent normal processing of this reload. */
6241 special = 1;
6242 /* Output a special code sequence for this case. */
6243 inc_for_reload (reloadreg, oldequiv, reload_inc[j]);
6246 /* If we are reloading a pseudo-register that was set by the previous
6247 insn, see if we can get rid of that pseudo-register entirely
6248 by redirecting the previous insn into our reload register. */
6250 else if (optimize && GET_CODE (old) == REG
6251 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6252 && dead_or_set_p (insn, old)
6253 /* This is unsafe if some other reload
6254 uses the same reg first. */
6255 && reload_reg_free_before_p (REGNO (reloadreg),
6256 reload_opnum[j],
6257 reload_when_needed[j]))
6259 rtx temp = PREV_INSN (insn);
6260 while (temp && GET_CODE (temp) == NOTE)
6261 temp = PREV_INSN (temp);
6262 if (temp
6263 && GET_CODE (temp) == INSN
6264 && GET_CODE (PATTERN (temp)) == SET
6265 && SET_DEST (PATTERN (temp)) == old
6266 /* Make sure we can access insn_operand_constraint. */
6267 && asm_noperands (PATTERN (temp)) < 0
6268 /* This is unsafe if prev insn rejects our reload reg. */
6269 && constraint_accepts_reg_p (insn_operand_constraint[recog_memoized (temp)][0],
6270 reloadreg)
6271 /* This is unsafe if operand occurs more than once in current
6272 insn. Perhaps some occurrences aren't reloaded. */
6273 && count_occurrences (PATTERN (insn), old) == 1
6274 /* Don't risk splitting a matching pair of operands. */
6275 && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
6277 /* Store into the reload register instead of the pseudo. */
6278 SET_DEST (PATTERN (temp)) = reloadreg;
6279 /* If these are the only uses of the pseudo reg,
6280 pretend for GDB it lives in the reload reg we used. */
6281 if (REG_N_DEATHS (REGNO (old)) == 1
6282 && REG_N_SETS (REGNO (old)) == 1)
6284 reg_renumber[REGNO (old)] = REGNO (reload_reg_rtx[j]);
6285 alter_reg (REGNO (old), -1);
6287 special = 1;
6291 /* We can't do that, so output an insn to load RELOADREG. */
6293 if (! special)
6295 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6296 rtx second_reload_reg = 0;
6297 enum insn_code icode;
6299 /* If we have a secondary reload, pick up the secondary register
6300 and icode, if any. If OLDEQUIV and OLD are different or
6301 if this is an in-out reload, recompute whether or not we
6302 still need a secondary register and what the icode should
6303 be. If we still need a secondary register and the class or
6304 icode is different, go back to reloading from OLD if using
6305 OLDEQUIV means that we got the wrong type of register. We
6306 cannot have different class or icode due to an in-out reload
6307 because we don't make such reloads when both the input and
6308 output need secondary reload registers. */
6310 if (reload_secondary_in_reload[j] >= 0)
6312 int secondary_reload = reload_secondary_in_reload[j];
6313 rtx real_oldequiv = oldequiv;
6314 rtx real_old = old;
6316 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6317 and similarly for OLD.
6318 See comments in get_secondary_reload in reload.c. */
6319 if (GET_CODE (oldequiv) == REG
6320 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
6321 && reg_equiv_mem[REGNO (oldequiv)] != 0)
6322 real_oldequiv = reg_equiv_mem[REGNO (oldequiv)];
6324 if (GET_CODE (old) == REG
6325 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6326 && reg_equiv_mem[REGNO (old)] != 0)
6327 real_old = reg_equiv_mem[REGNO (old)];
6329 second_reload_reg = reload_reg_rtx[secondary_reload];
6330 icode = reload_secondary_in_icode[j];
6332 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6333 || (reload_in[j] != 0 && reload_out[j] != 0))
6335 enum reg_class new_class
6336 = SECONDARY_INPUT_RELOAD_CLASS (reload_reg_class[j],
6337 mode, real_oldequiv);
6339 if (new_class == NO_REGS)
6340 second_reload_reg = 0;
6341 else
6343 enum insn_code new_icode;
6344 enum machine_mode new_mode;
6346 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
6347 REGNO (second_reload_reg)))
6348 oldequiv = old, real_oldequiv = real_old;
6349 else
6351 new_icode = reload_in_optab[(int) mode];
6352 if (new_icode != CODE_FOR_nothing
6353 && ((insn_operand_predicate[(int) new_icode][0]
6354 && ! ((*insn_operand_predicate[(int) new_icode][0])
6355 (reloadreg, mode)))
6356 || (insn_operand_predicate[(int) new_icode][1]
6357 && ! ((*insn_operand_predicate[(int) new_icode][1])
6358 (real_oldequiv, mode)))))
6359 new_icode = CODE_FOR_nothing;
6361 if (new_icode == CODE_FOR_nothing)
6362 new_mode = mode;
6363 else
6364 new_mode = insn_operand_mode[(int) new_icode][2];
6366 if (GET_MODE (second_reload_reg) != new_mode)
6368 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6369 new_mode))
6370 oldequiv = old, real_oldequiv = real_old;
6371 else
6372 second_reload_reg
6373 = gen_rtx_REG (new_mode,
6374 REGNO (second_reload_reg));
6380 /* If we still need a secondary reload register, check
6381 to see if it is being used as a scratch or intermediate
6382 register and generate code appropriately. If we need
6383 a scratch register, use REAL_OLDEQUIV since the form of
6384 the insn may depend on the actual address if it is
6385 a MEM. */
6387 if (second_reload_reg)
6389 if (icode != CODE_FOR_nothing)
6391 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6392 second_reload_reg));
6393 special = 1;
6395 else
6397 /* See if we need a scratch register to load the
6398 intermediate register (a tertiary reload). */
6399 enum insn_code tertiary_icode
6400 = reload_secondary_in_icode[secondary_reload];
6402 if (tertiary_icode != CODE_FOR_nothing)
6404 rtx third_reload_reg
6405 = reload_reg_rtx[reload_secondary_in_reload[secondary_reload]];
6407 emit_insn ((GEN_FCN (tertiary_icode)
6408 (second_reload_reg, real_oldequiv,
6409 third_reload_reg)));
6411 else
6412 gen_reload (second_reload_reg, oldequiv,
6413 reload_opnum[j],
6414 reload_when_needed[j]);
6416 oldequiv = second_reload_reg;
6420 #endif
6422 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
6423 gen_reload (reloadreg, oldequiv, reload_opnum[j],
6424 reload_when_needed[j]);
6426 #if defined(SECONDARY_INPUT_RELOAD_CLASS) && defined(PRESERVE_DEATH_INFO_REGNO_P)
6427 /* We may have to make a REG_DEAD note for the secondary reload
6428 register in the insns we just made. Find the last insn that
6429 mentioned the register. */
6430 if (! special && second_reload_reg
6431 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reload_reg)))
6433 rtx prev;
6435 for (prev = get_last_insn (); prev;
6436 prev = PREV_INSN (prev))
6437 if (GET_RTX_CLASS (GET_CODE (prev) == 'i')
6438 && reg_overlap_mentioned_for_reload_p (second_reload_reg,
6439 PATTERN (prev)))
6441 REG_NOTES (prev) = gen_rtx_EXPR_LIST (REG_DEAD,
6442 second_reload_reg,
6443 REG_NOTES (prev));
6444 break;
6447 #endif
6450 this_reload_insn = get_last_insn ();
6451 /* End this sequence. */
6452 *where = get_insns ();
6453 end_sequence ();
6456 /* Add a note saying the input reload reg
6457 dies in this insn, if anyone cares. */
6458 #ifdef PRESERVE_DEATH_INFO_REGNO_P
6459 if (old != 0
6460 && reload_reg_rtx[j] != old
6461 && reload_reg_rtx[j] != 0
6462 && reload_out[j] == 0
6463 && ! reload_inherited[j]
6464 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j])))
6466 register rtx reloadreg = reload_reg_rtx[j];
6468 #if 0
6469 /* We can't abort here because we need to support this for sched.c.
6470 It's not terrible to miss a REG_DEAD note, but we should try
6471 to figure out how to do this correctly. */
6472 /* The code below is incorrect for address-only reloads. */
6473 if (reload_when_needed[j] != RELOAD_OTHER
6474 && reload_when_needed[j] != RELOAD_FOR_INPUT)
6475 abort ();
6476 #endif
6478 /* Add a death note to this insn, for an input reload. */
6480 if ((reload_when_needed[j] == RELOAD_OTHER
6481 || reload_when_needed[j] == RELOAD_FOR_INPUT)
6482 && ! dead_or_set_p (insn, reloadreg))
6483 REG_NOTES (insn)
6484 = gen_rtx_EXPR_LIST (REG_DEAD,
6485 reloadreg, REG_NOTES (insn));
6488 /* When we inherit a reload, the last marked death of the reload reg
6489 may no longer really be a death. */
6490 if (reload_reg_rtx[j] != 0
6491 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j]))
6492 && reload_inherited[j])
6494 /* Handle inheriting an output reload.
6495 Remove the death note from the output reload insn. */
6496 if (reload_spill_index[j] >= 0
6497 && GET_CODE (reload_in[j]) == REG
6498 && spill_reg_store[reload_spill_index[j]] != 0
6499 && find_regno_note (spill_reg_store[reload_spill_index[j]],
6500 REG_DEAD, REGNO (reload_reg_rtx[j])))
6501 remove_death (REGNO (reload_reg_rtx[j]),
6502 spill_reg_store[reload_spill_index[j]]);
6503 /* Likewise for input reloads that were inherited. */
6504 else if (reload_spill_index[j] >= 0
6505 && GET_CODE (reload_in[j]) == REG
6506 && spill_reg_store[reload_spill_index[j]] == 0
6507 && reload_inheritance_insn[j] != 0
6508 && find_regno_note (reload_inheritance_insn[j], REG_DEAD,
6509 REGNO (reload_reg_rtx[j])))
6510 remove_death (REGNO (reload_reg_rtx[j]),
6511 reload_inheritance_insn[j]);
6512 else
6514 rtx prev;
6516 /* We got this register from find_equiv_reg.
6517 Search back for its last death note and get rid of it.
6518 But don't search back too far.
6519 Don't go past a place where this reg is set,
6520 since a death note before that remains valid. */
6521 for (prev = PREV_INSN (insn);
6522 prev && GET_CODE (prev) != CODE_LABEL;
6523 prev = PREV_INSN (prev))
6524 if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
6525 && dead_or_set_p (prev, reload_reg_rtx[j]))
6527 if (find_regno_note (prev, REG_DEAD,
6528 REGNO (reload_reg_rtx[j])))
6529 remove_death (REGNO (reload_reg_rtx[j]), prev);
6530 break;
6535 /* We might have used find_equiv_reg above to choose an alternate
6536 place from which to reload. If so, and it died, we need to remove
6537 that death and move it to one of the insns we just made. */
6539 if (oldequiv_reg != 0
6540 && PRESERVE_DEATH_INFO_REGNO_P (true_regnum (oldequiv_reg)))
6542 rtx prev, prev1;
6544 for (prev = PREV_INSN (insn); prev && GET_CODE (prev) != CODE_LABEL;
6545 prev = PREV_INSN (prev))
6546 if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
6547 && dead_or_set_p (prev, oldequiv_reg))
6549 if (find_regno_note (prev, REG_DEAD, REGNO (oldequiv_reg)))
6551 for (prev1 = this_reload_insn;
6552 prev1; prev1 = PREV_INSN (prev1))
6553 if (GET_RTX_CLASS (GET_CODE (prev1) == 'i')
6554 && reg_overlap_mentioned_for_reload_p (oldequiv_reg,
6555 PATTERN (prev1)))
6557 REG_NOTES (prev1) = gen_rtx_EXPR_LIST (REG_DEAD,
6558 oldequiv_reg,
6559 REG_NOTES (prev1));
6560 break;
6562 remove_death (REGNO (oldequiv_reg), prev);
6564 break;
6567 #endif
6569 /* If we are reloading a register that was recently stored in with an
6570 output-reload, see if we can prove there was
6571 actually no need to store the old value in it. */
6573 if (optimize && reload_inherited[j] && reload_spill_index[j] >= 0
6574 && reload_in[j] != 0
6575 && GET_CODE (reload_in[j]) == REG
6576 #if 0
6577 /* There doesn't seem to be any reason to restrict this to pseudos
6578 and doing so loses in the case where we are copying from a
6579 register of the wrong class. */
6580 && REGNO (reload_in[j]) >= FIRST_PSEUDO_REGISTER
6581 #endif
6582 && spill_reg_store[reload_spill_index[j]] != 0
6583 /* This is unsafe if some other reload uses the same reg first. */
6584 && reload_reg_free_before_p (spill_regs[reload_spill_index[j]],
6585 reload_opnum[j], reload_when_needed[j])
6586 && dead_or_set_p (insn, reload_in[j])
6587 /* This is unsafe if operand occurs more than once in current
6588 insn. Perhaps some occurrences weren't reloaded. */
6589 && count_occurrences (PATTERN (insn), reload_in[j]) == 1)
6590 delete_output_reload (insn, j,
6591 spill_reg_store[reload_spill_index[j]]);
6593 /* Input-reloading is done. Now do output-reloading,
6594 storing the value from the reload-register after the main insn
6595 if reload_out[j] is nonzero.
6597 ??? At some point we need to support handling output reloads of
6598 JUMP_INSNs or insns that set cc0. */
6599 old = reload_out[j];
6600 if (old != 0
6601 && reload_reg_rtx[j] != old
6602 && reload_reg_rtx[j] != 0)
6604 register rtx reloadreg = reload_reg_rtx[j];
6605 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
6606 register rtx second_reloadreg = 0;
6607 #endif
6608 rtx note, p;
6609 enum machine_mode mode;
6610 int special = 0;
6612 /* An output operand that dies right away does need a reload,
6613 but need not be copied from it. Show the new location in the
6614 REG_UNUSED note. */
6615 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
6616 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
6618 XEXP (note, 0) = reload_reg_rtx[j];
6619 continue;
6621 /* Likewise for a SUBREG of an operand that dies. */
6622 else if (GET_CODE (old) == SUBREG
6623 && GET_CODE (SUBREG_REG (old)) == REG
6624 && 0 != (note = find_reg_note (insn, REG_UNUSED,
6625 SUBREG_REG (old))))
6627 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
6628 reload_reg_rtx[j]);
6629 continue;
6631 else if (GET_CODE (old) == SCRATCH)
6632 /* If we aren't optimizing, there won't be a REG_UNUSED note,
6633 but we don't want to make an output reload. */
6634 continue;
6636 #if 0
6637 /* Strip off of OLD any size-increasing SUBREGs such as
6638 (SUBREG:SI foo:QI 0). */
6640 while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
6641 && (GET_MODE_SIZE (GET_MODE (old))
6642 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
6643 old = SUBREG_REG (old);
6644 #endif
6646 /* If is a JUMP_INSN, we can't support output reloads yet. */
6647 if (GET_CODE (insn) == JUMP_INSN)
6648 abort ();
6650 if (reload_when_needed[j] == RELOAD_OTHER)
6651 start_sequence ();
6652 else
6653 push_to_sequence (output_reload_insns[reload_opnum[j]]);
6655 /* Determine the mode to reload in.
6656 See comments above (for input reloading). */
6658 mode = GET_MODE (old);
6659 if (mode == VOIDmode)
6661 /* VOIDmode should never happen for an output. */
6662 if (asm_noperands (PATTERN (insn)) < 0)
6663 /* It's the compiler's fault. */
6664 fatal_insn ("VOIDmode on an output", insn);
6665 error_for_asm (insn, "output operand is constant in `asm'");
6666 /* Prevent crash--use something we know is valid. */
6667 mode = word_mode;
6668 old = gen_rtx_REG (mode, REGNO (reloadreg));
6671 if (GET_MODE (reloadreg) != mode)
6672 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6674 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
6676 /* If we need two reload regs, set RELOADREG to the intermediate
6677 one, since it will be stored into OLD. We might need a secondary
6678 register only for an input reload, so check again here. */
6680 if (reload_secondary_out_reload[j] >= 0)
6682 rtx real_old = old;
6684 if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
6685 && reg_equiv_mem[REGNO (old)] != 0)
6686 real_old = reg_equiv_mem[REGNO (old)];
6688 if((SECONDARY_OUTPUT_RELOAD_CLASS (reload_reg_class[j],
6689 mode, real_old)
6690 != NO_REGS))
6692 second_reloadreg = reloadreg;
6693 reloadreg = reload_reg_rtx[reload_secondary_out_reload[j]];
6695 /* See if RELOADREG is to be used as a scratch register
6696 or as an intermediate register. */
6697 if (reload_secondary_out_icode[j] != CODE_FOR_nothing)
6699 emit_insn ((GEN_FCN (reload_secondary_out_icode[j])
6700 (real_old, second_reloadreg, reloadreg)));
6701 special = 1;
6703 else
6705 /* See if we need both a scratch and intermediate reload
6706 register. */
6708 int secondary_reload = reload_secondary_out_reload[j];
6709 enum insn_code tertiary_icode
6710 = reload_secondary_out_icode[secondary_reload];
6712 if (GET_MODE (reloadreg) != mode)
6713 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6715 if (tertiary_icode != CODE_FOR_nothing)
6717 rtx third_reloadreg
6718 = reload_reg_rtx[reload_secondary_out_reload[secondary_reload]];
6719 rtx tem;
6721 /* Copy primary reload reg to secondary reload reg.
6722 (Note that these have been swapped above, then
6723 secondary reload reg to OLD using our insn. */
6725 /* If REAL_OLD is a paradoxical SUBREG, remove it
6726 and try to put the opposite SUBREG on
6727 RELOADREG. */
6728 if (GET_CODE (real_old) == SUBREG
6729 && (GET_MODE_SIZE (GET_MODE (real_old))
6730 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
6731 && 0 != (tem = gen_lowpart_common
6732 (GET_MODE (SUBREG_REG (real_old)),
6733 reloadreg)))
6734 real_old = SUBREG_REG (real_old), reloadreg = tem;
6736 gen_reload (reloadreg, second_reloadreg,
6737 reload_opnum[j], reload_when_needed[j]);
6738 emit_insn ((GEN_FCN (tertiary_icode)
6739 (real_old, reloadreg, third_reloadreg)));
6740 special = 1;
6743 else
6744 /* Copy between the reload regs here and then to
6745 OUT later. */
6747 gen_reload (reloadreg, second_reloadreg,
6748 reload_opnum[j], reload_when_needed[j]);
6752 #endif
6754 /* Output the last reload insn. */
6755 if (! special)
6756 gen_reload (old, reloadreg, reload_opnum[j],
6757 reload_when_needed[j]);
6759 #ifdef PRESERVE_DEATH_INFO_REGNO_P
6760 /* If final will look at death notes for this reg,
6761 put one on the last output-reload insn to use it. Similarly
6762 for any secondary register. */
6763 if (PRESERVE_DEATH_INFO_REGNO_P (REGNO (reloadreg)))
6764 for (p = get_last_insn (); p; p = PREV_INSN (p))
6765 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
6766 && reg_overlap_mentioned_for_reload_p (reloadreg,
6767 PATTERN (p)))
6768 REG_NOTES (p) = gen_rtx_EXPR_LIST (REG_DEAD,
6769 reloadreg, REG_NOTES (p));
6771 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
6772 if (! special && second_reloadreg
6773 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reloadreg)))
6774 for (p = get_last_insn (); p; p = PREV_INSN (p))
6775 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
6776 && reg_overlap_mentioned_for_reload_p (second_reloadreg,
6777 PATTERN (p)))
6778 REG_NOTES (p) = gen_rtx_EXPR_LIST (REG_DEAD,
6779 second_reloadreg,
6780 REG_NOTES (p));
6781 #endif
6782 #endif
6783 /* Look at all insns we emitted, just to be safe. */
6784 for (p = get_insns (); p; p = NEXT_INSN (p))
6785 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6787 /* If this output reload doesn't come from a spill reg,
6788 clear any memory of reloaded copies of the pseudo reg.
6789 If this output reload comes from a spill reg,
6790 reg_has_output_reload will make this do nothing. */
6791 note_stores (PATTERN (p), forget_old_reloads_1);
6793 if (reg_mentioned_p (reload_reg_rtx[j], PATTERN (p))
6794 && reload_spill_index[j] >= 0)
6795 new_spill_reg_store[reload_spill_index[j]] = p;
6798 if (reload_when_needed[j] == RELOAD_OTHER)
6800 emit_insns (other_output_reload_insns[reload_opnum[j]]);
6801 other_output_reload_insns[reload_opnum[j]] = get_insns ();
6803 else
6804 output_reload_insns[reload_opnum[j]] = get_insns ();
6806 end_sequence ();
6810 /* Now write all the insns we made for reloads in the order expected by
6811 the allocation functions. Prior to the insn being reloaded, we write
6812 the following reloads:
6814 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
6816 RELOAD_OTHER reloads.
6818 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
6819 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
6820 RELOAD_FOR_INPUT reload for the operand.
6822 RELOAD_FOR_OPADDR_ADDRS reloads.
6824 RELOAD_FOR_OPERAND_ADDRESS reloads.
6826 After the insn being reloaded, we write the following:
6828 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
6829 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
6830 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
6831 reloads for the operand. The RELOAD_OTHER output reloads are
6832 output in descending order by reload number. */
6834 emit_insns_before (other_input_address_reload_insns, before_insn);
6835 emit_insns_before (other_input_reload_insns, before_insn);
6837 for (j = 0; j < reload_n_operands; j++)
6839 emit_insns_before (inpaddr_address_reload_insns[j], before_insn);
6840 emit_insns_before (input_address_reload_insns[j], before_insn);
6841 emit_insns_before (input_reload_insns[j], before_insn);
6844 emit_insns_before (other_operand_reload_insns, before_insn);
6845 emit_insns_before (operand_reload_insns, before_insn);
6847 for (j = 0; j < reload_n_operands; j++)
6849 emit_insns_before (outaddr_address_reload_insns[j], following_insn);
6850 emit_insns_before (output_address_reload_insns[j], following_insn);
6851 emit_insns_before (output_reload_insns[j], following_insn);
6852 emit_insns_before (other_output_reload_insns[j], following_insn);
6855 /* Move death notes from INSN
6856 to output-operand-address and output reload insns. */
6857 #ifdef PRESERVE_DEATH_INFO_REGNO_P
6859 rtx insn1;
6860 /* Loop over those insns, last ones first. */
6861 for (insn1 = PREV_INSN (following_insn); insn1 != insn;
6862 insn1 = PREV_INSN (insn1))
6863 if (GET_CODE (insn1) == INSN && GET_CODE (PATTERN (insn1)) == SET)
6865 rtx source = SET_SRC (PATTERN (insn1));
6866 rtx dest = SET_DEST (PATTERN (insn1));
6868 /* The note we will examine next. */
6869 rtx reg_notes = REG_NOTES (insn);
6870 /* The place that pointed to this note. */
6871 rtx *prev_reg_note = &REG_NOTES (insn);
6873 /* If the note is for something used in the source of this
6874 reload insn, or in the output address, move the note. */
6875 while (reg_notes)
6877 rtx next_reg_notes = XEXP (reg_notes, 1);
6878 if (REG_NOTE_KIND (reg_notes) == REG_DEAD
6879 && GET_CODE (XEXP (reg_notes, 0)) == REG
6880 && ((GET_CODE (dest) != REG
6881 && reg_overlap_mentioned_for_reload_p (XEXP (reg_notes, 0),
6882 dest))
6883 || reg_overlap_mentioned_for_reload_p (XEXP (reg_notes, 0),
6884 source)))
6886 *prev_reg_note = next_reg_notes;
6887 XEXP (reg_notes, 1) = REG_NOTES (insn1);
6888 REG_NOTES (insn1) = reg_notes;
6890 else
6891 prev_reg_note = &XEXP (reg_notes, 1);
6893 reg_notes = next_reg_notes;
6897 #endif
6899 /* For all the spill regs newly reloaded in this instruction,
6900 record what they were reloaded from, so subsequent instructions
6901 can inherit the reloads.
6903 Update spill_reg_store for the reloads of this insn.
6904 Copy the elements that were updated in the loop above. */
6906 for (j = 0; j < n_reloads; j++)
6908 register int r = reload_order[j];
6909 register int i = reload_spill_index[r];
6911 /* I is nonneg if this reload used one of the spill regs.
6912 If reload_reg_rtx[r] is 0, this is an optional reload
6913 that we opted to ignore. */
6915 if (i >= 0 && reload_reg_rtx[r] != 0)
6917 int nr
6918 = HARD_REGNO_NREGS (spill_regs[i], GET_MODE (reload_reg_rtx[r]));
6919 int k;
6920 int part_reaches_end = 0;
6921 int all_reaches_end = 1;
6923 /* For a multi register reload, we need to check if all or part
6924 of the value lives to the end. */
6925 for (k = 0; k < nr; k++)
6927 if (reload_reg_reaches_end_p (spill_regs[i] + k, reload_opnum[r],
6928 reload_when_needed[r]))
6929 part_reaches_end = 1;
6930 else
6931 all_reaches_end = 0;
6934 /* Ignore reloads that don't reach the end of the insn in
6935 entirety. */
6936 if (all_reaches_end)
6938 /* First, clear out memory of what used to be in this spill reg.
6939 If consecutive registers are used, clear them all. */
6941 for (k = 0; k < nr; k++)
6943 reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]] = -1;
6944 reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = 0;
6947 /* Maybe the spill reg contains a copy of reload_out. */
6948 if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
6950 register int nregno = REGNO (reload_out[r]);
6951 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
6952 : HARD_REGNO_NREGS (nregno,
6953 GET_MODE (reload_reg_rtx[r])));
6955 spill_reg_store[i] = new_spill_reg_store[i];
6956 reg_last_reload_reg[nregno] = reload_reg_rtx[r];
6958 /* If NREGNO is a hard register, it may occupy more than
6959 one register. If it does, say what is in the
6960 rest of the registers assuming that both registers
6961 agree on how many words the object takes. If not,
6962 invalidate the subsequent registers. */
6964 if (nregno < FIRST_PSEUDO_REGISTER)
6965 for (k = 1; k < nnr; k++)
6966 reg_last_reload_reg[nregno + k]
6967 = (nr == nnr
6968 ? gen_rtx_REG (reg_raw_mode[REGNO (reload_reg_rtx[r]) + k],
6969 REGNO (reload_reg_rtx[r]) + k)
6970 : 0);
6972 /* Now do the inverse operation. */
6973 for (k = 0; k < nr; k++)
6975 reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
6976 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
6977 ? nregno
6978 : nregno + k);
6979 reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = insn;
6983 /* Maybe the spill reg contains a copy of reload_in. Only do
6984 something if there will not be an output reload for
6985 the register being reloaded. */
6986 else if (reload_out[r] == 0
6987 && reload_in[r] != 0
6988 && ((GET_CODE (reload_in[r]) == REG
6989 && ! reg_has_output_reload[REGNO (reload_in[r])])
6990 || (GET_CODE (reload_in_reg[r]) == REG
6991 && ! reg_has_output_reload[REGNO (reload_in_reg[r])])))
6993 register int nregno;
6994 int nnr;
6996 if (GET_CODE (reload_in[r]) == REG)
6997 nregno = REGNO (reload_in[r]);
6998 else
6999 nregno = REGNO (reload_in_reg[r]);
7001 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7002 : HARD_REGNO_NREGS (nregno,
7003 GET_MODE (reload_reg_rtx[r])));
7005 reg_last_reload_reg[nregno] = reload_reg_rtx[r];
7007 if (nregno < FIRST_PSEUDO_REGISTER)
7008 for (k = 1; k < nnr; k++)
7009 reg_last_reload_reg[nregno + k]
7010 = (nr == nnr
7011 ? gen_rtx_REG (reg_raw_mode[REGNO (reload_reg_rtx[r]) + k],
7012 REGNO (reload_reg_rtx[r]) + k)
7013 : 0);
7015 /* Unless we inherited this reload, show we haven't
7016 recently done a store. */
7017 if (! reload_inherited[r])
7018 spill_reg_store[i] = 0;
7020 for (k = 0; k < nr; k++)
7022 reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
7023 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7024 ? nregno
7025 : nregno + k);
7026 reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]]
7027 = insn;
7032 /* However, if part of the reload reaches the end, then we must
7033 invalidate the old info for the part that survives to the end. */
7034 else if (part_reaches_end)
7036 for (k = 0; k < nr; k++)
7037 if (reload_reg_reaches_end_p (spill_regs[i] + k,
7038 reload_opnum[r],
7039 reload_when_needed[r]))
7041 reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]] = -1;
7042 reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = 0;
7047 /* The following if-statement was #if 0'd in 1.34 (or before...).
7048 It's reenabled in 1.35 because supposedly nothing else
7049 deals with this problem. */
7051 /* If a register gets output-reloaded from a non-spill register,
7052 that invalidates any previous reloaded copy of it.
7053 But forget_old_reloads_1 won't get to see it, because
7054 it thinks only about the original insn. So invalidate it here. */
7055 if (i < 0 && reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
7057 register int nregno = REGNO (reload_out[r]);
7058 if (nregno >= FIRST_PSEUDO_REGISTER)
7059 reg_last_reload_reg[nregno] = 0;
7060 else
7062 int num_regs = HARD_REGNO_NREGS (nregno,GET_MODE (reload_out[r]));
7064 while (num_regs-- > 0)
7065 reg_last_reload_reg[nregno + num_regs] = 0;
7071 /* Emit code to perform a reload from IN (which may be a reload register) to
7072 OUT (which may also be a reload register). IN or OUT is from operand
7073 OPNUM with reload type TYPE.
7075 Returns first insn emitted. */
7078 gen_reload (out, in, opnum, type)
7079 rtx out;
7080 rtx in;
7081 int opnum;
7082 enum reload_type type;
7084 rtx last = get_last_insn ();
7085 rtx tem;
7087 /* If IN is a paradoxical SUBREG, remove it and try to put the
7088 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7089 if (GET_CODE (in) == SUBREG
7090 && (GET_MODE_SIZE (GET_MODE (in))
7091 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7092 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7093 in = SUBREG_REG (in), out = tem;
7094 else if (GET_CODE (out) == SUBREG
7095 && (GET_MODE_SIZE (GET_MODE (out))
7096 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7097 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7098 out = SUBREG_REG (out), in = tem;
7100 /* How to do this reload can get quite tricky. Normally, we are being
7101 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7102 register that didn't get a hard register. In that case we can just
7103 call emit_move_insn.
7105 We can also be asked to reload a PLUS that adds a register or a MEM to
7106 another register, constant or MEM. This can occur during frame pointer
7107 elimination and while reloading addresses. This case is handled by
7108 trying to emit a single insn to perform the add. If it is not valid,
7109 we use a two insn sequence.
7111 Finally, we could be called to handle an 'o' constraint by putting
7112 an address into a register. In that case, we first try to do this
7113 with a named pattern of "reload_load_address". If no such pattern
7114 exists, we just emit a SET insn and hope for the best (it will normally
7115 be valid on machines that use 'o').
7117 This entire process is made complex because reload will never
7118 process the insns we generate here and so we must ensure that
7119 they will fit their constraints and also by the fact that parts of
7120 IN might be being reloaded separately and replaced with spill registers.
7121 Because of this, we are, in some sense, just guessing the right approach
7122 here. The one listed above seems to work.
7124 ??? At some point, this whole thing needs to be rethought. */
7126 if (GET_CODE (in) == PLUS
7127 && (GET_CODE (XEXP (in, 0)) == REG
7128 || GET_CODE (XEXP (in, 0)) == SUBREG
7129 || GET_CODE (XEXP (in, 0)) == MEM)
7130 && (GET_CODE (XEXP (in, 1)) == REG
7131 || GET_CODE (XEXP (in, 1)) == SUBREG
7132 || CONSTANT_P (XEXP (in, 1))
7133 || GET_CODE (XEXP (in, 1)) == MEM))
7135 /* We need to compute the sum of a register or a MEM and another
7136 register, constant, or MEM, and put it into the reload
7137 register. The best possible way of doing this is if the machine
7138 has a three-operand ADD insn that accepts the required operands.
7140 The simplest approach is to try to generate such an insn and see if it
7141 is recognized and matches its constraints. If so, it can be used.
7143 It might be better not to actually emit the insn unless it is valid,
7144 but we need to pass the insn as an operand to `recog' and
7145 `insn_extract' and it is simpler to emit and then delete the insn if
7146 not valid than to dummy things up. */
7148 rtx op0, op1, tem, insn;
7149 int code;
7151 op0 = find_replacement (&XEXP (in, 0));
7152 op1 = find_replacement (&XEXP (in, 1));
7154 /* Since constraint checking is strict, commutativity won't be
7155 checked, so we need to do that here to avoid spurious failure
7156 if the add instruction is two-address and the second operand
7157 of the add is the same as the reload reg, which is frequently
7158 the case. If the insn would be A = B + A, rearrange it so
7159 it will be A = A + B as constrain_operands expects. */
7161 if (GET_CODE (XEXP (in, 1)) == REG
7162 && REGNO (out) == REGNO (XEXP (in, 1)))
7163 tem = op0, op0 = op1, op1 = tem;
7165 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7166 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7168 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
7169 code = recog_memoized (insn);
7171 if (code >= 0)
7173 insn_extract (insn);
7174 /* We want constrain operands to treat this insn strictly in
7175 its validity determination, i.e., the way it would after reload
7176 has completed. */
7177 if (constrain_operands (code, 1))
7178 return insn;
7181 delete_insns_since (last);
7183 /* If that failed, we must use a conservative two-insn sequence.
7184 use move to copy constant, MEM, or pseudo register to the reload
7185 register since "move" will be able to handle an arbitrary operand,
7186 unlike add which can't, in general. Then add the registers.
7188 If there is another way to do this for a specific machine, a
7189 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7190 we emit below. */
7192 if (CONSTANT_P (op1) || GET_CODE (op1) == MEM || GET_CODE (op1) == SUBREG
7193 || (GET_CODE (op1) == REG
7194 && REGNO (op1) >= FIRST_PSEUDO_REGISTER))
7195 tem = op0, op0 = op1, op1 = tem;
7197 gen_reload (out, op0, opnum, type);
7199 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7200 This fixes a problem on the 32K where the stack pointer cannot
7201 be used as an operand of an add insn. */
7203 if (rtx_equal_p (op0, op1))
7204 op1 = out;
7206 insn = emit_insn (gen_add2_insn (out, op1));
7208 /* If that failed, copy the address register to the reload register.
7209 Then add the constant to the reload register. */
7211 code = recog_memoized (insn);
7213 if (code >= 0)
7215 insn_extract (insn);
7216 /* We want constrain operands to treat this insn strictly in
7217 its validity determination, i.e., the way it would after reload
7218 has completed. */
7219 if (constrain_operands (code, 1))
7220 return insn;
7223 delete_insns_since (last);
7225 gen_reload (out, op1, opnum, type);
7226 emit_insn (gen_add2_insn (out, op0));
7229 #ifdef SECONDARY_MEMORY_NEEDED
7230 /* If we need a memory location to do the move, do it that way. */
7231 else if (GET_CODE (in) == REG && REGNO (in) < FIRST_PSEUDO_REGISTER
7232 && GET_CODE (out) == REG && REGNO (out) < FIRST_PSEUDO_REGISTER
7233 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (in)),
7234 REGNO_REG_CLASS (REGNO (out)),
7235 GET_MODE (out)))
7237 /* Get the memory to use and rewrite both registers to its mode. */
7238 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7240 if (GET_MODE (loc) != GET_MODE (out))
7241 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7243 if (GET_MODE (loc) != GET_MODE (in))
7244 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7246 gen_reload (loc, in, opnum, type);
7247 gen_reload (out, loc, opnum, type);
7249 #endif
7251 /* If IN is a simple operand, use gen_move_insn. */
7252 else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
7253 emit_insn (gen_move_insn (out, in));
7255 #ifdef HAVE_reload_load_address
7256 else if (HAVE_reload_load_address)
7257 emit_insn (gen_reload_load_address (out, in));
7258 #endif
7260 /* Otherwise, just write (set OUT IN) and hope for the best. */
7261 else
7262 emit_insn (gen_rtx_SET (VOIDmode, out, in));
7264 /* Return the first insn emitted.
7265 We can not just return get_last_insn, because there may have
7266 been multiple instructions emitted. Also note that gen_move_insn may
7267 emit more than one insn itself, so we can not assume that there is one
7268 insn emitted per emit_insn_before call. */
7270 return last ? NEXT_INSN (last) : get_insns ();
7273 /* Delete a previously made output-reload
7274 whose result we now believe is not needed.
7275 First we double-check.
7277 INSN is the insn now being processed.
7278 OUTPUT_RELOAD_INSN is the insn of the output reload.
7279 J is the reload-number for this insn. */
7281 static void
7282 delete_output_reload (insn, j, output_reload_insn)
7283 rtx insn;
7284 int j;
7285 rtx output_reload_insn;
7287 register rtx i1;
7289 /* Get the raw pseudo-register referred to. */
7291 rtx reg = reload_in[j];
7292 while (GET_CODE (reg) == SUBREG)
7293 reg = SUBREG_REG (reg);
7295 /* If the pseudo-reg we are reloading is no longer referenced
7296 anywhere between the store into it and here,
7297 and no jumps or labels intervene, then the value can get
7298 here through the reload reg alone.
7299 Otherwise, give up--return. */
7300 for (i1 = NEXT_INSN (output_reload_insn);
7301 i1 != insn; i1 = NEXT_INSN (i1))
7303 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
7304 return;
7305 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
7306 && reg_mentioned_p (reg, PATTERN (i1)))
7307 return;
7310 if (cannot_omit_stores[REGNO (reg)])
7311 return;
7313 /* If this insn will store in the pseudo again,
7314 the previous store can be removed. */
7315 if (reload_out[j] == reload_in[j])
7316 delete_insn (output_reload_insn);
7318 /* See if the pseudo reg has been completely replaced
7319 with reload regs. If so, delete the store insn
7320 and forget we had a stack slot for the pseudo. */
7321 else if (REG_N_DEATHS (REGNO (reg)) == 1
7322 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
7323 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
7325 rtx i2;
7327 /* We know that it was used only between here
7328 and the beginning of the current basic block.
7329 (We also know that the last use before INSN was
7330 the output reload we are thinking of deleting, but never mind that.)
7331 Search that range; see if any ref remains. */
7332 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7334 rtx set = single_set (i2);
7336 /* Uses which just store in the pseudo don't count,
7337 since if they are the only uses, they are dead. */
7338 if (set != 0 && SET_DEST (set) == reg)
7339 continue;
7340 if (GET_CODE (i2) == CODE_LABEL
7341 || GET_CODE (i2) == JUMP_INSN)
7342 break;
7343 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
7344 && reg_mentioned_p (reg, PATTERN (i2)))
7345 /* Some other ref remains;
7346 we can't do anything. */
7347 return;
7350 /* Delete the now-dead stores into this pseudo. */
7351 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7353 rtx set = single_set (i2);
7355 if (set != 0 && SET_DEST (set) == reg)
7357 /* This might be a basic block head,
7358 thus don't use delete_insn. */
7359 PUT_CODE (i2, NOTE);
7360 NOTE_SOURCE_FILE (i2) = 0;
7361 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
7363 if (GET_CODE (i2) == CODE_LABEL
7364 || GET_CODE (i2) == JUMP_INSN)
7365 break;
7368 /* For the debugging info,
7369 say the pseudo lives in this reload reg. */
7370 reg_renumber[REGNO (reg)] = REGNO (reload_reg_rtx[j]);
7371 alter_reg (REGNO (reg), -1);
7375 /* Output reload-insns to reload VALUE into RELOADREG.
7376 VALUE is an autoincrement or autodecrement RTX whose operand
7377 is a register or memory location;
7378 so reloading involves incrementing that location.
7380 INC_AMOUNT is the number to increment or decrement by (always positive).
7381 This cannot be deduced from VALUE. */
7383 static void
7384 inc_for_reload (reloadreg, value, inc_amount)
7385 rtx reloadreg;
7386 rtx value;
7387 int inc_amount;
7389 /* REG or MEM to be copied and incremented. */
7390 rtx incloc = XEXP (value, 0);
7391 /* Nonzero if increment after copying. */
7392 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
7393 rtx last;
7394 rtx inc;
7395 rtx add_insn;
7396 int code;
7398 /* No hard register is equivalent to this register after
7399 inc/dec operation. If REG_LAST_RELOAD_REG were non-zero,
7400 we could inc/dec that register as well (maybe even using it for
7401 the source), but I'm not sure it's worth worrying about. */
7402 if (GET_CODE (incloc) == REG)
7403 reg_last_reload_reg[REGNO (incloc)] = 0;
7405 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
7406 inc_amount = - inc_amount;
7408 inc = GEN_INT (inc_amount);
7410 /* If this is post-increment, first copy the location to the reload reg. */
7411 if (post)
7412 emit_insn (gen_move_insn (reloadreg, incloc));
7414 /* See if we can directly increment INCLOC. Use a method similar to that
7415 in gen_reload. */
7417 last = get_last_insn ();
7418 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
7419 gen_rtx_PLUS (GET_MODE (incloc),
7420 incloc, inc)));
7422 code = recog_memoized (add_insn);
7423 if (code >= 0)
7425 insn_extract (add_insn);
7426 if (constrain_operands (code, 1))
7428 /* If this is a pre-increment and we have incremented the value
7429 where it lives, copy the incremented value to RELOADREG to
7430 be used as an address. */
7432 if (! post)
7433 emit_insn (gen_move_insn (reloadreg, incloc));
7435 return;
7439 delete_insns_since (last);
7441 /* If couldn't do the increment directly, must increment in RELOADREG.
7442 The way we do this depends on whether this is pre- or post-increment.
7443 For pre-increment, copy INCLOC to the reload register, increment it
7444 there, then save back. */
7446 if (! post)
7448 emit_insn (gen_move_insn (reloadreg, incloc));
7449 emit_insn (gen_add2_insn (reloadreg, inc));
7450 emit_insn (gen_move_insn (incloc, reloadreg));
7452 else
7454 /* Postincrement.
7455 Because this might be a jump insn or a compare, and because RELOADREG
7456 may not be available after the insn in an input reload, we must do
7457 the incrementation before the insn being reloaded for.
7459 We have already copied INCLOC to RELOADREG. Increment the copy in
7460 RELOADREG, save that back, then decrement RELOADREG so it has
7461 the original value. */
7463 emit_insn (gen_add2_insn (reloadreg, inc));
7464 emit_insn (gen_move_insn (incloc, reloadreg));
7465 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
7468 return;
7471 /* Return 1 if we are certain that the constraint-string STRING allows
7472 the hard register REG. Return 0 if we can't be sure of this. */
7474 static int
7475 constraint_accepts_reg_p (string, reg)
7476 char *string;
7477 rtx reg;
7479 int value = 0;
7480 int regno = true_regnum (reg);
7481 int c;
7483 /* Initialize for first alternative. */
7484 value = 0;
7485 /* Check that each alternative contains `g' or `r'. */
7486 while (1)
7487 switch (c = *string++)
7489 case 0:
7490 /* If an alternative lacks `g' or `r', we lose. */
7491 return value;
7492 case ',':
7493 /* If an alternative lacks `g' or `r', we lose. */
7494 if (value == 0)
7495 return 0;
7496 /* Initialize for next alternative. */
7497 value = 0;
7498 break;
7499 case 'g':
7500 case 'r':
7501 /* Any general reg wins for this alternative. */
7502 if (TEST_HARD_REG_BIT (reg_class_contents[(int) GENERAL_REGS], regno))
7503 value = 1;
7504 break;
7505 default:
7506 /* Any reg in specified class wins for this alternative. */
7508 enum reg_class class = REG_CLASS_FROM_LETTER (c);
7510 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno))
7511 value = 1;
7516 /* Return the number of places FIND appears within X, but don't count
7517 an occurrence if some SET_DEST is FIND. */
7520 count_occurrences (x, find)
7521 register rtx x, find;
7523 register int i, j;
7524 register enum rtx_code code;
7525 register char *format_ptr;
7526 int count;
7528 if (x == find)
7529 return 1;
7530 if (x == 0)
7531 return 0;
7533 code = GET_CODE (x);
7535 switch (code)
7537 case REG:
7538 case QUEUED:
7539 case CONST_INT:
7540 case CONST_DOUBLE:
7541 case SYMBOL_REF:
7542 case CODE_LABEL:
7543 case PC:
7544 case CC0:
7545 return 0;
7547 case SET:
7548 if (SET_DEST (x) == find)
7549 return count_occurrences (SET_SRC (x), find);
7550 break;
7552 default:
7553 break;
7556 format_ptr = GET_RTX_FORMAT (code);
7557 count = 0;
7559 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7561 switch (*format_ptr++)
7563 case 'e':
7564 count += count_occurrences (XEXP (x, i), find);
7565 break;
7567 case 'E':
7568 if (XVEC (x, i) != NULL)
7570 for (j = 0; j < XVECLEN (x, i); j++)
7571 count += count_occurrences (XVECEXP (x, i, j), find);
7573 break;
7576 return count;
7579 /* This array holds values which are equivalent to a hard register
7580 during reload_cse_regs. Each array element is an EXPR_LIST of
7581 values. Each time a hard register is set, we set the corresponding
7582 array element to the value. Each time a hard register is copied
7583 into memory, we add the memory location to the corresponding array
7584 element. We don't store values or memory addresses with side
7585 effects in this array.
7587 If the value is a CONST_INT, then the mode of the containing
7588 EXPR_LIST is the mode in which that CONST_INT was referenced.
7590 We sometimes clobber a specific entry in a list. In that case, we
7591 just set XEXP (list-entry, 0) to 0. */
7593 static rtx *reg_values;
7595 /* This is a preallocated REG rtx which we use as a temporary in
7596 reload_cse_invalidate_regno, so that we don't need to allocate a
7597 new one each time through a loop in that function. */
7599 static rtx invalidate_regno_rtx;
7601 /* This is a set of registers for which we must remove REG_DEAD notes in
7602 previous insns, because our modifications made them invalid. That can
7603 happen if we introduced the register into the current insn, or we deleted
7604 the current insn which used to set the register. */
7606 static HARD_REG_SET no_longer_dead_regs;
7608 /* Invalidate any entries in reg_values which depend on REGNO,
7609 including those for REGNO itself. This is called if REGNO is
7610 changing. If CLOBBER is true, then always forget anything we
7611 currently know about REGNO. MODE is the mode of the assignment to
7612 REGNO, which is used to determine how many hard registers are being
7613 changed. If MODE is VOIDmode, then only REGNO is being changed;
7614 this is used when invalidating call clobbered registers across a
7615 call. */
7617 static void
7618 reload_cse_invalidate_regno (regno, mode, clobber)
7619 int regno;
7620 enum machine_mode mode;
7621 int clobber;
7623 int endregno;
7624 register int i;
7626 /* Our callers don't always go through true_regnum; we may see a
7627 pseudo-register here from a CLOBBER or the like. We probably
7628 won't ever see a pseudo-register that has a real register number,
7629 for we check anyhow for safety. */
7630 if (regno >= FIRST_PSEUDO_REGISTER)
7631 regno = reg_renumber[regno];
7632 if (regno < 0)
7633 return;
7635 if (mode == VOIDmode)
7636 endregno = regno + 1;
7637 else
7638 endregno = regno + HARD_REGNO_NREGS (regno, mode);
7640 if (clobber)
7641 for (i = regno; i < endregno; i++)
7642 reg_values[i] = 0;
7644 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7646 rtx x;
7648 for (x = reg_values[i]; x; x = XEXP (x, 1))
7650 if (XEXP (x, 0) != 0
7651 && refers_to_regno_p (regno, endregno, XEXP (x, 0), NULL_PTR))
7653 /* If this is the only entry on the list, clear
7654 reg_values[i]. Otherwise, just clear this entry on
7655 the list. */
7656 if (XEXP (x, 1) == 0 && x == reg_values[i])
7658 reg_values[i] = 0;
7659 break;
7661 XEXP (x, 0) = 0;
7666 /* We must look at earlier registers, in case REGNO is part of a
7667 multi word value but is not the first register. If an earlier
7668 register has a value in a mode which overlaps REGNO, then we must
7669 invalidate that earlier register. Note that we do not need to
7670 check REGNO or later registers (we must not check REGNO itself,
7671 because we would incorrectly conclude that there was a conflict). */
7673 for (i = 0; i < regno; i++)
7675 rtx x;
7677 for (x = reg_values[i]; x; x = XEXP (x, 1))
7679 if (XEXP (x, 0) != 0)
7681 PUT_MODE (invalidate_regno_rtx, GET_MODE (x));
7682 REGNO (invalidate_regno_rtx) = i;
7683 if (refers_to_regno_p (regno, endregno, invalidate_regno_rtx,
7684 NULL_PTR))
7686 reload_cse_invalidate_regno (i, VOIDmode, 1);
7687 break;
7694 /* The memory at address MEM_BASE is being changed.
7695 Return whether this change will invalidate VAL. */
7697 static int
7698 reload_cse_mem_conflict_p (mem_base, val)
7699 rtx mem_base;
7700 rtx val;
7702 enum rtx_code code;
7703 char *fmt;
7704 int i;
7706 code = GET_CODE (val);
7707 switch (code)
7709 /* Get rid of a few simple cases quickly. */
7710 case REG:
7711 case PC:
7712 case CC0:
7713 case SCRATCH:
7714 case CONST:
7715 case CONST_INT:
7716 case CONST_DOUBLE:
7717 case SYMBOL_REF:
7718 case LABEL_REF:
7719 return 0;
7721 case MEM:
7722 if (GET_MODE (mem_base) == BLKmode
7723 || GET_MODE (val) == BLKmode)
7724 return 1;
7725 if (anti_dependence (val, mem_base))
7726 return 1;
7727 /* The address may contain nested MEMs. */
7728 break;
7730 default:
7731 break;
7734 fmt = GET_RTX_FORMAT (code);
7736 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7738 if (fmt[i] == 'e')
7740 if (reload_cse_mem_conflict_p (mem_base, XEXP (val, i)))
7741 return 1;
7743 else if (fmt[i] == 'E')
7745 int j;
7747 for (j = 0; j < XVECLEN (val, i); j++)
7748 if (reload_cse_mem_conflict_p (mem_base, XVECEXP (val, i, j)))
7749 return 1;
7753 return 0;
7756 /* Invalidate any entries in reg_values which are changed because of a
7757 store to MEM_RTX. If this is called because of a non-const call
7758 instruction, MEM_RTX is (mem:BLK const0_rtx). */
7760 static void
7761 reload_cse_invalidate_mem (mem_rtx)
7762 rtx mem_rtx;
7764 register int i;
7766 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7768 rtx x;
7770 for (x = reg_values[i]; x; x = XEXP (x, 1))
7772 if (XEXP (x, 0) != 0
7773 && reload_cse_mem_conflict_p (mem_rtx, XEXP (x, 0)))
7775 /* If this is the only entry on the list, clear
7776 reg_values[i]. Otherwise, just clear this entry on
7777 the list. */
7778 if (XEXP (x, 1) == 0 && x == reg_values[i])
7780 reg_values[i] = 0;
7781 break;
7783 XEXP (x, 0) = 0;
7789 /* Invalidate DEST, which is being assigned to or clobbered. The
7790 second parameter exists so that this function can be passed to
7791 note_stores; it is ignored. */
7793 static void
7794 reload_cse_invalidate_rtx (dest, ignore)
7795 rtx dest;
7796 rtx ignore;
7798 while (GET_CODE (dest) == STRICT_LOW_PART
7799 || GET_CODE (dest) == SIGN_EXTRACT
7800 || GET_CODE (dest) == ZERO_EXTRACT
7801 || GET_CODE (dest) == SUBREG)
7802 dest = XEXP (dest, 0);
7804 if (GET_CODE (dest) == REG)
7805 reload_cse_invalidate_regno (REGNO (dest), GET_MODE (dest), 1);
7806 else if (GET_CODE (dest) == MEM)
7807 reload_cse_invalidate_mem (dest);
7810 /* Possibly delete death notes on the insns before INSN if modifying INSN
7811 extended the lifespan of the registers. */
7813 static void
7814 reload_cse_delete_death_notes (insn)
7815 rtx insn;
7817 int dreg;
7819 for (dreg = 0; dreg < FIRST_PSEUDO_REGISTER; dreg++)
7821 rtx trial;
7823 if (! TEST_HARD_REG_BIT (no_longer_dead_regs, dreg))
7824 continue;
7826 for (trial = prev_nonnote_insn (insn);
7827 (trial
7828 && GET_CODE (trial) != CODE_LABEL
7829 && GET_CODE (trial) != BARRIER);
7830 trial = prev_nonnote_insn (trial))
7832 if (find_regno_note (trial, REG_DEAD, dreg))
7834 remove_death (dreg, trial);
7835 break;
7841 /* Record that the current insn uses hard reg REGNO in mode MODE. This
7842 will be used in reload_cse_delete_death_notes to delete prior REG_DEAD
7843 notes for this register. */
7845 static void
7846 reload_cse_no_longer_dead (regno, mode)
7847 int regno;
7848 enum machine_mode mode;
7850 int nregs = HARD_REGNO_NREGS (regno, mode);
7851 while (nregs-- > 0)
7853 SET_HARD_REG_BIT (no_longer_dead_regs, regno);
7854 regno++;
7859 /* Do a very simple CSE pass over the hard registers.
7861 This function detects no-op moves where we happened to assign two
7862 different pseudo-registers to the same hard register, and then
7863 copied one to the other. Reload will generate a useless
7864 instruction copying a register to itself.
7866 This function also detects cases where we load a value from memory
7867 into two different registers, and (if memory is more expensive than
7868 registers) changes it to simply copy the first register into the
7869 second register.
7871 Another optimization is performed that scans the operands of each
7872 instruction to see whether the value is already available in a
7873 hard register. It then replaces the operand with the hard register
7874 if possible, much like an optional reload would. */
7876 void
7877 reload_cse_regs (first)
7878 rtx first;
7880 char *firstobj;
7881 rtx callmem;
7882 register int i;
7883 rtx insn;
7885 init_alias_analysis ();
7887 reg_values = (rtx *) alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
7888 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7889 reg_values[i] = 0;
7891 /* Create our EXPR_LIST structures on reload_obstack, so that we can
7892 free them when we are done. */
7893 push_obstacks (&reload_obstack, &reload_obstack);
7894 firstobj = (char *) obstack_alloc (&reload_obstack, 0);
7896 /* We pass this to reload_cse_invalidate_mem to invalidate all of
7897 memory for a non-const call instruction. */
7898 callmem = gen_rtx_MEM (BLKmode, const0_rtx);
7900 /* This is used in reload_cse_invalidate_regno to avoid consing a
7901 new REG in a loop in that function. */
7902 invalidate_regno_rtx = gen_rtx_REG (VOIDmode, 0);
7904 for (insn = first; insn; insn = NEXT_INSN (insn))
7906 rtx body;
7908 if (GET_CODE (insn) == CODE_LABEL)
7910 /* Forget all the register values at a code label. We don't
7911 try to do anything clever around jumps. */
7912 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7913 reg_values[i] = 0;
7915 continue;
7918 #ifdef NON_SAVING_SETJMP
7919 if (NON_SAVING_SETJMP && GET_CODE (insn) == NOTE
7920 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
7922 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7923 reg_values[i] = 0;
7925 continue;
7927 #endif
7929 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
7930 continue;
7932 CLEAR_HARD_REG_SET (no_longer_dead_regs);
7934 /* If this is a call instruction, forget anything stored in a
7935 call clobbered register, or, if this is not a const call, in
7936 memory. */
7937 if (GET_CODE (insn) == CALL_INSN)
7939 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7940 if (call_used_regs[i])
7941 reload_cse_invalidate_regno (i, VOIDmode, 1);
7943 if (! CONST_CALL_P (insn))
7944 reload_cse_invalidate_mem (callmem);
7947 body = PATTERN (insn);
7948 if (GET_CODE (body) == SET)
7950 int count = 0;
7951 if (reload_cse_noop_set_p (body, insn))
7953 PUT_CODE (insn, NOTE);
7954 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
7955 NOTE_SOURCE_FILE (insn) = 0;
7956 reload_cse_delete_death_notes (insn);
7958 /* We're done with this insn. */
7959 continue;
7962 /* It's not a no-op, but we can try to simplify it. */
7963 CLEAR_HARD_REG_SET (no_longer_dead_regs);
7964 count += reload_cse_simplify_set (body, insn);
7966 if (count > 0 && apply_change_group ())
7967 reload_cse_delete_death_notes (insn);
7968 else if (reload_cse_simplify_operands (insn))
7969 reload_cse_delete_death_notes (insn);
7971 reload_cse_record_set (body, body);
7973 else if (GET_CODE (body) == PARALLEL)
7975 int count = 0;
7977 /* If every action in a PARALLEL is a noop, we can delete
7978 the entire PARALLEL. */
7979 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
7980 if ((GET_CODE (XVECEXP (body, 0, i)) != SET
7981 || ! reload_cse_noop_set_p (XVECEXP (body, 0, i), insn))
7982 && GET_CODE (XVECEXP (body, 0, i)) != CLOBBER)
7983 break;
7984 if (i < 0)
7986 PUT_CODE (insn, NOTE);
7987 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
7988 NOTE_SOURCE_FILE (insn) = 0;
7989 reload_cse_delete_death_notes (insn);
7991 /* We're done with this insn. */
7992 continue;
7995 /* It's not a no-op, but we can try to simplify it. */
7996 CLEAR_HARD_REG_SET (no_longer_dead_regs);
7997 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
7998 if (GET_CODE (XVECEXP (body, 0, i)) == SET)
7999 count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
8001 if (count > 0 && apply_change_group ())
8002 reload_cse_delete_death_notes (insn);
8003 else if (reload_cse_simplify_operands (insn))
8004 reload_cse_delete_death_notes (insn);
8006 /* Look through the PARALLEL and record the values being
8007 set, if possible. Also handle any CLOBBERs. */
8008 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8010 rtx x = XVECEXP (body, 0, i);
8012 if (GET_CODE (x) == SET)
8013 reload_cse_record_set (x, body);
8014 else
8015 note_stores (x, reload_cse_invalidate_rtx);
8018 else
8019 note_stores (body, reload_cse_invalidate_rtx);
8021 #ifdef AUTO_INC_DEC
8022 /* Clobber any registers which appear in REG_INC notes. We
8023 could keep track of the changes to their values, but it is
8024 unlikely to help. */
8026 rtx x;
8028 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
8029 if (REG_NOTE_KIND (x) == REG_INC)
8030 reload_cse_invalidate_rtx (XEXP (x, 0), NULL_RTX);
8032 #endif
8034 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
8035 after we have processed the insn. */
8036 if (GET_CODE (insn) == CALL_INSN)
8038 rtx x;
8040 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
8041 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
8042 reload_cse_invalidate_rtx (XEXP (XEXP (x, 0), 0), NULL_RTX);
8046 /* Free all the temporary structures we created, and go back to the
8047 regular obstacks. */
8048 obstack_free (&reload_obstack, firstobj);
8049 pop_obstacks ();
8052 /* Return whether the values known for REGNO are equal to VAL. MODE
8053 is the mode of the object that VAL is being copied to; this matters
8054 if VAL is a CONST_INT. */
8056 static int
8057 reload_cse_regno_equal_p (regno, val, mode)
8058 int regno;
8059 rtx val;
8060 enum machine_mode mode;
8062 rtx x;
8064 if (val == 0)
8065 return 0;
8067 for (x = reg_values[regno]; x; x = XEXP (x, 1))
8068 if (XEXP (x, 0) != 0
8069 && rtx_equal_p (XEXP (x, 0), val)
8070 && (GET_CODE (val) != CONST_INT
8071 || mode == GET_MODE (x)
8072 || (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8073 /* On a big endian machine if the value spans more than
8074 one register then this register holds the high part of
8075 it and we can't use it.
8077 ??? We should also compare with the high part of the
8078 value. */
8079 && !(WORDS_BIG_ENDIAN
8080 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
8081 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
8082 GET_MODE_BITSIZE (GET_MODE (x))))))
8083 return 1;
8085 return 0;
8088 /* See whether a single set is a noop. SET is the set instruction we
8089 are should check, and INSN is the instruction from which it came. */
8091 static int
8092 reload_cse_noop_set_p (set, insn)
8093 rtx set;
8094 rtx insn;
8096 rtx src, dest;
8097 enum machine_mode dest_mode;
8098 int dreg, sreg;
8099 int ret;
8101 src = SET_SRC (set);
8102 dest = SET_DEST (set);
8103 dest_mode = GET_MODE (dest);
8105 if (side_effects_p (src))
8106 return 0;
8108 dreg = true_regnum (dest);
8109 sreg = true_regnum (src);
8111 /* Check for setting a register to itself. In this case, we don't
8112 have to worry about REG_DEAD notes. */
8113 if (dreg >= 0 && dreg == sreg)
8114 return 1;
8116 ret = 0;
8117 if (dreg >= 0)
8119 /* Check for setting a register to itself. */
8120 if (dreg == sreg)
8121 ret = 1;
8123 /* Check for setting a register to a value which we already know
8124 is in the register. */
8125 else if (reload_cse_regno_equal_p (dreg, src, dest_mode))
8126 ret = 1;
8128 /* Check for setting a register DREG to another register SREG
8129 where SREG is equal to a value which is already in DREG. */
8130 else if (sreg >= 0)
8132 rtx x;
8134 for (x = reg_values[sreg]; x; x = XEXP (x, 1))
8136 rtx tmp;
8138 if (XEXP (x, 0) == 0)
8139 continue;
8141 if (dest_mode == GET_MODE (x))
8142 tmp = XEXP (x, 0);
8143 else if (GET_MODE_BITSIZE (dest_mode)
8144 < GET_MODE_BITSIZE (GET_MODE (x)))
8145 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
8146 else
8147 continue;
8149 if (tmp
8150 && reload_cse_regno_equal_p (dreg, tmp, dest_mode))
8152 ret = 1;
8153 break;
8158 else if (GET_CODE (dest) == MEM)
8160 /* Check for storing a register to memory when we know that the
8161 register is equivalent to the memory location. */
8162 if (sreg >= 0
8163 && reload_cse_regno_equal_p (sreg, dest, dest_mode)
8164 && ! side_effects_p (dest))
8165 ret = 1;
8168 /* If we can delete this SET, then we need to look for an earlier
8169 REG_DEAD note on DREG, and remove it if it exists. */
8170 if (ret && dreg >= 0)
8172 if (! find_regno_note (insn, REG_UNUSED, dreg))
8173 reload_cse_no_longer_dead (dreg, dest_mode);
8176 return ret;
8179 /* Try to simplify a single SET instruction. SET is the set pattern.
8180 INSN is the instruction it came from.
8181 This function only handles one case: if we set a register to a value
8182 which is not a register, we try to find that value in some other register
8183 and change the set into a register copy. */
8185 static int
8186 reload_cse_simplify_set (set, insn)
8187 rtx set;
8188 rtx insn;
8190 int dreg;
8191 rtx src;
8192 enum machine_mode dest_mode;
8193 enum reg_class dclass;
8194 register int i;
8196 dreg = true_regnum (SET_DEST (set));
8197 if (dreg < 0)
8198 return 0;
8200 src = SET_SRC (set);
8201 if (side_effects_p (src) || true_regnum (src) >= 0)
8202 return 0;
8204 /* If memory loads are cheaper than register copies, don't change
8205 them. */
8206 if (GET_CODE (src) == MEM && MEMORY_MOVE_COST (GET_MODE (src)) < 2)
8207 return 0;
8209 dest_mode = GET_MODE (SET_DEST (set));
8210 dclass = REGNO_REG_CLASS (dreg);
8211 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8213 if (i != dreg
8214 && REGISTER_MOVE_COST (REGNO_REG_CLASS (i), dclass) == 2
8215 && reload_cse_regno_equal_p (i, src, dest_mode))
8217 int validated;
8219 /* Pop back to the real obstacks while changing the insn. */
8220 pop_obstacks ();
8222 validated = validate_change (insn, &SET_SRC (set),
8223 gen_rtx_REG (dest_mode, i), 1);
8225 /* Go back to the obstack we are using for temporary
8226 storage. */
8227 push_obstacks (&reload_obstack, &reload_obstack);
8229 if (validated && ! find_regno_note (insn, REG_UNUSED, i))
8231 reload_cse_no_longer_dead (i, dest_mode);
8232 return 1;
8236 return 0;
8239 /* Try to replace operands in INSN with equivalent values that are already
8240 in registers. This can be viewed as optional reloading.
8242 For each non-register operand in the insn, see if any hard regs are
8243 known to be equivalent to that operand. Record the alternatives which
8244 can accept these hard registers. Among all alternatives, select the
8245 ones which are better or equal to the one currently matching, where
8246 "better" is in terms of '?' and '!' constraints. Among the remaining
8247 alternatives, select the one which replaces most operands with
8248 hard registers. */
8250 static int
8251 reload_cse_simplify_operands (insn)
8252 rtx insn;
8254 #ifdef REGISTER_CONSTRAINTS
8255 int insn_code_number, n_operands, n_alternatives;
8256 int i,j;
8258 char *constraints[MAX_RECOG_OPERANDS];
8260 /* Vector recording how bad an alternative is. */
8261 int *alternative_reject;
8262 /* Vector recording how many registers can be introduced by choosing
8263 this alternative. */
8264 int *alternative_nregs;
8265 /* Array of vectors recording, for each operand and each alternative,
8266 which hard register to substitute, or -1 if the operand should be
8267 left as it is. */
8268 int *op_alt_regno[MAX_RECOG_OPERANDS];
8269 /* Array of alternatives, sorted in order of decreasing desirability. */
8270 int *alternative_order;
8272 /* Find out some information about this insn. */
8273 insn_code_number = recog_memoized (insn);
8274 /* We don't modify asm instructions. */
8275 if (insn_code_number < 0)
8276 return 0;
8278 n_operands = insn_n_operands[insn_code_number];
8279 n_alternatives = insn_n_alternatives[insn_code_number];
8281 if (n_alternatives == 0 || n_operands == 0)
8282 return 0;
8283 insn_extract (insn);
8285 /* Figure out which alternative currently matches. */
8286 if (! constrain_operands (insn_code_number, 1))
8287 abort ();
8289 alternative_reject = (int *) alloca (n_alternatives * sizeof (int));
8290 alternative_nregs = (int *) alloca (n_alternatives * sizeof (int));
8291 alternative_order = (int *) alloca (n_alternatives * sizeof (int));
8292 bzero ((char *)alternative_reject, n_alternatives * sizeof (int));
8293 bzero ((char *)alternative_nregs, n_alternatives * sizeof (int));
8295 for (i = 0; i < n_operands; i++)
8297 enum machine_mode mode;
8298 int regno;
8299 char *p;
8301 op_alt_regno[i] = (int *) alloca (n_alternatives * sizeof (int));
8302 for (j = 0; j < n_alternatives; j++)
8303 op_alt_regno[i][j] = -1;
8305 p = constraints[i] = insn_operand_constraint[insn_code_number][i];
8306 mode = insn_operand_mode[insn_code_number][i];
8308 /* Add the reject values for each alternative given by the constraints
8309 for this operand. */
8310 j = 0;
8311 while (*p != '\0')
8313 char c = *p++;
8314 if (c == ',')
8315 j++;
8316 else if (c == '?')
8317 alternative_reject[j] += 3;
8318 else if (c == '!')
8319 alternative_reject[j] += 300;
8322 /* We won't change operands which are already registers. We
8323 also don't want to modify output operands. */
8324 regno = true_regnum (recog_operand[i]);
8325 if (regno >= 0
8326 || constraints[i][0] == '='
8327 || constraints[i][0] == '+')
8328 continue;
8330 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8332 int class = (int) NO_REGS;
8334 if (! reload_cse_regno_equal_p (regno, recog_operand[i], mode))
8335 continue;
8337 /* We found a register equal to this operand. Now look for all
8338 alternatives that can accept this register and have not been
8339 assigned a register they can use yet. */
8340 j = 0;
8341 p = constraints[i];
8342 for (;;)
8344 char c = *p++;
8346 switch (c)
8348 case '=': case '+': case '?':
8349 case '#': case '&': case '!':
8350 case '*': case '%':
8351 case '0': case '1': case '2': case '3': case '4':
8352 case 'm': case '<': case '>': case 'V': case 'o':
8353 case 'E': case 'F': case 'G': case 'H':
8354 case 's': case 'i': case 'n':
8355 case 'I': case 'J': case 'K': case 'L':
8356 case 'M': case 'N': case 'O': case 'P':
8357 #ifdef EXTRA_CONSTRAINT
8358 case 'Q': case 'R': case 'S': case 'T': case 'U':
8359 #endif
8360 case 'p': case 'X':
8361 /* These don't say anything we care about. */
8362 break;
8364 case 'g': case 'r':
8365 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
8366 break;
8368 default:
8369 class
8370 = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER (c)];
8371 break;
8373 case ',': case '\0':
8374 /* See if REGNO fits this alternative, and set it up as the
8375 replacement register if we don't have one for this
8376 alternative yet. */
8377 if (op_alt_regno[i][j] == -1
8378 && reg_fits_class_p (gen_rtx_REG (mode, regno), class,
8379 0, mode))
8381 alternative_nregs[j]++;
8382 op_alt_regno[i][j] = regno;
8384 j++;
8385 break;
8388 if (c == '\0')
8389 break;
8394 /* Record all alternatives which are better or equal to the currently
8395 matching one in the alternative_order array. */
8396 for (i = j = 0; i < n_alternatives; i++)
8397 if (alternative_reject[i] <= alternative_reject[which_alternative])
8398 alternative_order[j++] = i;
8399 n_alternatives = j;
8401 /* Sort it. Given a small number of alternatives, a dumb algorithm
8402 won't hurt too much. */
8403 for (i = 0; i < n_alternatives - 1; i++)
8405 int best = i;
8406 int best_reject = alternative_reject[alternative_order[i]];
8407 int best_nregs = alternative_nregs[alternative_order[i]];
8408 int tmp;
8410 for (j = i + 1; j < n_alternatives; j++)
8412 int this_reject = alternative_reject[alternative_order[j]];
8413 int this_nregs = alternative_nregs[alternative_order[j]];
8415 if (this_reject < best_reject
8416 || (this_reject == best_reject && this_nregs < best_nregs))
8418 best = j;
8419 best_reject = this_reject;
8420 best_nregs = this_nregs;
8424 tmp = alternative_order[best];
8425 alternative_order[best] = alternative_order[i];
8426 alternative_order[i] = tmp;
8429 /* Substitute the operands as determined by op_alt_regno for the best
8430 alternative. */
8431 j = alternative_order[0];
8432 CLEAR_HARD_REG_SET (no_longer_dead_regs);
8434 /* Pop back to the real obstacks while changing the insn. */
8435 pop_obstacks ();
8437 for (i = 0; i < n_operands; i++)
8439 enum machine_mode mode = insn_operand_mode[insn_code_number][i];
8440 if (op_alt_regno[i][j] == -1)
8441 continue;
8443 reload_cse_no_longer_dead (op_alt_regno[i][j], mode);
8444 validate_change (insn, recog_operand_loc[i],
8445 gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
8448 for (i = insn_n_dups[insn_code_number] - 1; i >= 0; i--)
8450 int op = recog_dup_num[i];
8451 enum machine_mode mode = insn_operand_mode[insn_code_number][op];
8453 if (op_alt_regno[op][j] == -1)
8454 continue;
8456 reload_cse_no_longer_dead (op_alt_regno[op][j], mode);
8457 validate_change (insn, recog_dup_loc[i],
8458 gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
8461 /* Go back to the obstack we are using for temporary
8462 storage. */
8463 push_obstacks (&reload_obstack, &reload_obstack);
8465 return apply_change_group ();
8466 #else
8467 return 0;
8468 #endif
8471 /* These two variables are used to pass information from
8472 reload_cse_record_set to reload_cse_check_clobber. */
8474 static int reload_cse_check_clobbered;
8475 static rtx reload_cse_check_src;
8477 /* See if DEST overlaps with RELOAD_CSE_CHECK_SRC. If it does, set
8478 RELOAD_CSE_CHECK_CLOBBERED. This is called via note_stores. The
8479 second argument, which is passed by note_stores, is ignored. */
8481 static void
8482 reload_cse_check_clobber (dest, ignore)
8483 rtx dest;
8484 rtx ignore;
8486 if (reg_overlap_mentioned_p (dest, reload_cse_check_src))
8487 reload_cse_check_clobbered = 1;
8490 /* Record the result of a SET instruction. SET is the set pattern.
8491 BODY is the pattern of the insn that it came from. */
8493 static void
8494 reload_cse_record_set (set, body)
8495 rtx set;
8496 rtx body;
8498 rtx dest, src, x;
8499 int dreg, sreg;
8500 enum machine_mode dest_mode;
8502 dest = SET_DEST (set);
8503 src = SET_SRC (set);
8504 dreg = true_regnum (dest);
8505 sreg = true_regnum (src);
8506 dest_mode = GET_MODE (dest);
8508 /* Some machines don't define AUTO_INC_DEC, but they still use push
8509 instructions. We need to catch that case here in order to
8510 invalidate the stack pointer correctly. Note that invalidating
8511 the stack pointer is different from invalidating DEST. */
8512 x = dest;
8513 while (GET_CODE (x) == SUBREG
8514 || GET_CODE (x) == ZERO_EXTRACT
8515 || GET_CODE (x) == SIGN_EXTRACT
8516 || GET_CODE (x) == STRICT_LOW_PART)
8517 x = XEXP (x, 0);
8518 if (push_operand (x, GET_MODE (x)))
8520 reload_cse_invalidate_rtx (stack_pointer_rtx, NULL_RTX);
8521 reload_cse_invalidate_rtx (dest, NULL_RTX);
8522 return;
8525 /* We can only handle an assignment to a register, or a store of a
8526 register to a memory location. For other cases, we just clobber
8527 the destination. We also have to just clobber if there are side
8528 effects in SRC or DEST. */
8529 if ((dreg < 0 && GET_CODE (dest) != MEM)
8530 || side_effects_p (src)
8531 || side_effects_p (dest))
8533 reload_cse_invalidate_rtx (dest, NULL_RTX);
8534 return;
8537 #ifdef HAVE_cc0
8538 /* We don't try to handle values involving CC, because it's a pain
8539 to keep track of when they have to be invalidated. */
8540 if (reg_mentioned_p (cc0_rtx, src)
8541 || reg_mentioned_p (cc0_rtx, dest))
8543 reload_cse_invalidate_rtx (dest, NULL_RTX);
8544 return;
8546 #endif
8548 /* If BODY is a PARALLEL, then we need to see whether the source of
8549 SET is clobbered by some other instruction in the PARALLEL. */
8550 if (GET_CODE (body) == PARALLEL)
8552 int i;
8554 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8556 rtx x;
8558 x = XVECEXP (body, 0, i);
8559 if (x == set)
8560 continue;
8562 reload_cse_check_clobbered = 0;
8563 reload_cse_check_src = src;
8564 note_stores (x, reload_cse_check_clobber);
8565 if (reload_cse_check_clobbered)
8567 reload_cse_invalidate_rtx (dest, NULL_RTX);
8568 return;
8573 if (dreg >= 0)
8575 int i;
8577 /* This is an assignment to a register. Update the value we
8578 have stored for the register. */
8579 if (sreg >= 0)
8581 rtx x;
8583 /* This is a copy from one register to another. Any values
8584 which were valid for SREG are now valid for DREG. If the
8585 mode changes, we use gen_lowpart_common to extract only
8586 the part of the value that is copied. */
8587 reg_values[dreg] = 0;
8588 for (x = reg_values[sreg]; x; x = XEXP (x, 1))
8590 rtx tmp;
8592 if (XEXP (x, 0) == 0)
8593 continue;
8594 if (dest_mode == GET_MODE (XEXP (x, 0)))
8595 tmp = XEXP (x, 0);
8596 else if (GET_MODE_BITSIZE (dest_mode)
8597 > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
8598 continue;
8599 else
8600 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
8601 if (tmp)
8602 reg_values[dreg] = gen_rtx_EXPR_LIST (dest_mode, tmp,
8603 reg_values[dreg]);
8606 else
8607 reg_values[dreg] = gen_rtx_EXPR_LIST (dest_mode, src, NULL_RTX);
8609 /* We've changed DREG, so invalidate any values held by other
8610 registers that depend upon it. */
8611 reload_cse_invalidate_regno (dreg, dest_mode, 0);
8613 /* If this assignment changes more than one hard register,
8614 forget anything we know about the others. */
8615 for (i = 1; i < HARD_REGNO_NREGS (dreg, dest_mode); i++)
8616 reg_values[dreg + i] = 0;
8618 else if (GET_CODE (dest) == MEM)
8620 /* Invalidate conflicting memory locations. */
8621 reload_cse_invalidate_mem (dest);
8623 /* If we're storing a register to memory, add DEST to the list
8624 in REG_VALUES. */
8625 if (sreg >= 0 && ! side_effects_p (dest))
8626 reg_values[sreg] = gen_rtx_EXPR_LIST (dest_mode, dest,
8627 reg_values[sreg]);
8629 else
8631 /* We should have bailed out earlier. */
8632 abort ();