* arm.md (pic_load_addr): Add constraints to operand 1.
[official-gcc.git] / gcc / reload1.c
blobcad122155640d03650ced96108dbf2cf40d6add8
1 /* Reload pseudo regs into hard regs for insns that require hard regs.
2 Copyright (C) 1987, 88, 89, 92-98, 1999 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 "system.h"
25 #include "machmode.h"
26 #include "hard-reg-set.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "obstack.h"
30 #include "insn-config.h"
31 #include "insn-flags.h"
32 #include "insn-codes.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "regs.h"
37 #include "basic-block.h"
38 #include "reload.h"
39 #include "recog.h"
40 #include "output.h"
41 #include "real.h"
42 #include "toplev.h"
44 #if !defined PREFERRED_STACK_BOUNDARY && defined STACK_BOUNDARY
45 #define PREFERRED_STACK_BOUNDARY STACK_BOUNDARY
46 #endif
48 /* This file contains the reload pass of the compiler, which is
49 run after register allocation has been done. It checks that
50 each insn is valid (operands required to be in registers really
51 are in registers of the proper class) and fixes up invalid ones
52 by copying values temporarily into registers for the insns
53 that need them.
55 The results of register allocation are described by the vector
56 reg_renumber; the insns still contain pseudo regs, but reg_renumber
57 can be used to find which hard reg, if any, a pseudo reg is in.
59 The technique we always use is to free up a few hard regs that are
60 called ``reload regs'', and for each place where a pseudo reg
61 must be in a hard reg, copy it temporarily into one of the reload regs.
63 Reload regs are allocated locally for every instruction that needs
64 reloads. When there are pseudos which are allocated to a register that
65 has been chosen as a reload reg, such pseudos must be ``spilled''.
66 This means that they go to other hard regs, or to stack slots if no other
67 available hard regs can be found. Spilling can invalidate more
68 insns, requiring additional need for reloads, so we must keep checking
69 until the process stabilizes.
71 For machines with different classes of registers, we must keep track
72 of the register class needed for each reload, and make sure that
73 we allocate enough reload registers of each class.
75 The file reload.c contains the code that checks one insn for
76 validity and reports the reloads that it needs. This file
77 is in charge of scanning the entire rtl code, accumulating the
78 reload needs, spilling, assigning reload registers to use for
79 fixing up each insn, and generating the new insns to copy values
80 into the reload registers. */
83 #ifndef REGISTER_MOVE_COST
84 #define REGISTER_MOVE_COST(x, y) 2
85 #endif
87 /* During reload_as_needed, element N contains a REG rtx for the hard reg
88 into which reg N has been reloaded (perhaps for a previous insn). */
89 static rtx *reg_last_reload_reg;
91 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
92 for an output reload that stores into reg N. */
93 static char *reg_has_output_reload;
95 /* Indicates which hard regs are reload-registers for an output reload
96 in the current insn. */
97 static HARD_REG_SET reg_is_output_reload;
99 /* Element N is the constant value to which pseudo reg N is equivalent,
100 or zero if pseudo reg N is not equivalent to a constant.
101 find_reloads looks at this in order to replace pseudo reg N
102 with the constant it stands for. */
103 rtx *reg_equiv_constant;
105 /* Element N is a memory location to which pseudo reg N is equivalent,
106 prior to any register elimination (such as frame pointer to stack
107 pointer). Depending on whether or not it is a valid address, this value
108 is transferred to either reg_equiv_address or reg_equiv_mem. */
109 rtx *reg_equiv_memory_loc;
111 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
112 This is used when the address is not valid as a memory address
113 (because its displacement is too big for the machine.) */
114 rtx *reg_equiv_address;
116 /* Element N is the memory slot to which pseudo reg N is equivalent,
117 or zero if pseudo reg N is not equivalent to a memory slot. */
118 rtx *reg_equiv_mem;
120 /* Widest width in which each pseudo reg is referred to (via subreg). */
121 static int *reg_max_ref_width;
123 /* Element N is the list of insns that initialized reg N from its equivalent
124 constant or memory slot. */
125 static rtx *reg_equiv_init;
127 /* Vector to remember old contents of reg_renumber before spilling. */
128 static short *reg_old_renumber;
130 /* During reload_as_needed, element N contains the last pseudo regno reloaded
131 into hard register N. If that pseudo reg occupied more than one register,
132 reg_reloaded_contents points to that pseudo for each spill register in
133 use; all of these must remain set for an inheritance to occur. */
134 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
136 /* During reload_as_needed, element N contains the insn for which
137 hard register N was last used. Its contents are significant only
138 when reg_reloaded_valid is set for this register. */
139 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
141 /* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid */
142 static HARD_REG_SET reg_reloaded_valid;
143 /* Indicate if the register was dead at the end of the reload.
144 This is only valid if reg_reloaded_contents is set and valid. */
145 static HARD_REG_SET reg_reloaded_dead;
147 /* Number of spill-regs so far; number of valid elements of spill_regs. */
148 static int n_spills;
150 /* In parallel with spill_regs, contains REG rtx's for those regs.
151 Holds the last rtx used for any given reg, or 0 if it has never
152 been used for spilling yet. This rtx is reused, provided it has
153 the proper mode. */
154 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
156 /* In parallel with spill_regs, contains nonzero for a spill reg
157 that was stored after the last time it was used.
158 The precise value is the insn generated to do the store. */
159 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
161 /* This is the register that was stored with spill_reg_store. This is a
162 copy of reload_out / reload_out_reg when the value was stored; if
163 reload_out is a MEM, spill_reg_stored_to will be set to reload_out_reg. */
164 static rtx spill_reg_stored_to[FIRST_PSEUDO_REGISTER];
166 /* This table is the inverse mapping of spill_regs:
167 indexed by hard reg number,
168 it contains the position of that reg in spill_regs,
169 or -1 for something that is not in spill_regs.
171 ?!? This is no longer accurate. */
172 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
174 /* This reg set indicates registers that can't be used as spill registers for
175 the currently processed insn. These are the hard registers which are live
176 during the insn, but not allocated to pseudos, as well as fixed
177 registers. */
178 static HARD_REG_SET bad_spill_regs;
180 /* These are the hard registers that can't be used as spill register for any
181 insn. This includes registers used for user variables and registers that
182 we can't eliminate. A register that appears in this set also can't be used
183 to retry register allocation. */
184 static HARD_REG_SET bad_spill_regs_global;
186 /* Describes order of use of registers for reloading
187 of spilled pseudo-registers. `n_spills' is the number of
188 elements that are actually valid; new ones are added at the end.
190 Both spill_regs and spill_reg_order are used on two occasions:
191 once during find_reload_regs, where they keep track of the spill registers
192 for a single insn, but also during reload_as_needed where they show all
193 the registers ever used by reload. For the latter case, the information
194 is calculated during finish_spills. */
195 static short spill_regs[FIRST_PSEUDO_REGISTER];
197 /* This vector of reg sets indicates, for each pseudo, which hard registers
198 may not be used for retrying global allocation because the register was
199 formerly spilled from one of them. If we allowed reallocating a pseudo to
200 a register that it was already allocated to, reload might not
201 terminate. */
202 static HARD_REG_SET *pseudo_previous_regs;
204 /* This vector of reg sets indicates, for each pseudo, which hard
205 registers may not be used for retrying global allocation because they
206 are used as spill registers during one of the insns in which the
207 pseudo is live. */
208 static HARD_REG_SET *pseudo_forbidden_regs;
210 /* All hard regs that have been used as spill registers for any insn are
211 marked in this set. */
212 static HARD_REG_SET used_spill_regs;
214 /* Index of last register assigned as a spill register. We allocate in
215 a round-robin fashion. */
216 static int last_spill_reg;
218 /* Describes order of preference for putting regs into spill_regs.
219 Contains the numbers of all the hard regs, in order most preferred first.
220 This order is different for each function.
221 It is set up by order_regs_for_reload.
222 Empty elements at the end contain -1. */
223 static short potential_reload_regs[FIRST_PSEUDO_REGISTER];
225 /* Nonzero if indirect addressing is supported on the machine; this means
226 that spilling (REG n) does not require reloading it into a register in
227 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
228 value indicates the level of indirect addressing supported, e.g., two
229 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
230 a hard register. */
231 static char spill_indirect_levels;
233 /* Nonzero if indirect addressing is supported when the innermost MEM is
234 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
235 which these are valid is the same as spill_indirect_levels, above. */
236 char indirect_symref_ok;
238 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
239 char double_reg_address_ok;
241 /* Record the stack slot for each spilled hard register. */
242 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
244 /* Width allocated so far for that stack slot. */
245 static int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
247 /* Record which pseudos needed to be spilled. */
248 static regset spilled_pseudos;
250 /* First uid used by insns created by reload in this function.
251 Used in find_equiv_reg. */
252 int reload_first_uid;
254 /* Flag set by local-alloc or global-alloc if anything is live in
255 a call-clobbered reg across calls. */
256 int caller_save_needed;
258 /* Set to 1 while reload_as_needed is operating.
259 Required by some machines to handle any generated moves differently. */
260 int reload_in_progress = 0;
262 /* These arrays record the insn_code of insns that may be needed to
263 perform input and output reloads of special objects. They provide a
264 place to pass a scratch register. */
265 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
266 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
268 /* This obstack is used for allocation of rtl during register elimination.
269 The allocated storage can be freed once find_reloads has processed the
270 insn. */
271 struct obstack reload_obstack;
273 /* Points to the beginning of the reload_obstack. All insn_chain structures
274 are allocated first. */
275 char *reload_startobj;
277 /* The point after all insn_chain structures. Used to quickly deallocate
278 memory used while processing one insn. */
279 char *reload_firstobj;
281 #define obstack_chunk_alloc xmalloc
282 #define obstack_chunk_free free
284 /* List of insn_chain instructions, one for every insn that reload needs to
285 examine. */
286 struct insn_chain *reload_insn_chain;
288 #ifdef TREE_CODE
289 extern tree current_function_decl;
290 #else
291 extern union tree_node *current_function_decl;
292 #endif
294 /* List of all insns needing reloads. */
295 static struct insn_chain *insns_need_reload;
297 /* This structure is used to record information about register eliminations.
298 Each array entry describes one possible way of eliminating a register
299 in favor of another. If there is more than one way of eliminating a
300 particular register, the most preferred should be specified first. */
302 struct elim_table
304 int from; /* Register number to be eliminated. */
305 int to; /* Register number used as replacement. */
306 int initial_offset; /* Initial difference between values. */
307 int can_eliminate; /* Non-zero if this elimination can be done. */
308 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
309 insns made by reload. */
310 int offset; /* Current 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. */
321 static struct elim_table * reg_eliminate = 0;
323 /* This is an intermediate structure to initialize the table. It has
324 exactly the members provided by ELIMINABLE_REGS. */
325 static struct elim_table_1
327 int from;
328 int to;
329 } reg_eliminate_1[] =
331 /* If a set of eliminable registers was specified, define the table from it.
332 Otherwise, default to the normal case of the frame pointer being
333 replaced by the stack pointer. */
335 #ifdef ELIMINABLE_REGS
336 ELIMINABLE_REGS;
337 #else
338 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
339 #endif
341 #define NUM_ELIMINABLE_REGS (sizeof reg_eliminate_1/sizeof reg_eliminate_1[0])
343 /* Record the number of pending eliminations that have an offset not equal
344 to their initial offset. If non-zero, we use a new copy of each
345 replacement result in any insns encountered. */
346 int num_not_at_initial_offset;
348 /* Count the number of registers that we may be able to eliminate. */
349 static int num_eliminable;
350 /* And the number of registers that are equivalent to a constant that
351 can be eliminated to frame_pointer / arg_pointer + constant. */
352 static int num_eliminable_invariants;
354 /* For each label, we record the offset of each elimination. If we reach
355 a label by more than one path and an offset differs, we cannot do the
356 elimination. This information is indexed by the number of the label.
357 The first table is an array of flags that records whether we have yet
358 encountered a label and the second table is an array of arrays, one
359 entry in the latter array for each elimination. */
361 static char *offsets_known_at;
362 static int (*offsets_at)[NUM_ELIMINABLE_REGS];
364 /* Number of labels in the current function. */
366 static int num_labels;
368 struct hard_reg_n_uses
370 int regno;
371 unsigned int uses;
374 static void maybe_fix_stack_asms PROTO((void));
375 static void calculate_needs_all_insns PROTO((int));
376 static void calculate_needs PROTO((struct insn_chain *));
377 static void find_reload_regs PROTO((struct insn_chain *chain,
378 FILE *));
379 static void find_tworeg_group PROTO((struct insn_chain *, int,
380 FILE *));
381 static void find_group PROTO((struct insn_chain *, int,
382 FILE *));
383 static int possible_group_p PROTO((struct insn_chain *, int));
384 static void count_possible_groups PROTO((struct insn_chain *, int));
385 static int modes_equiv_for_class_p PROTO((enum machine_mode,
386 enum machine_mode,
387 enum reg_class));
388 static void delete_caller_save_insns PROTO((void));
390 static void spill_failure PROTO((rtx));
391 static void new_spill_reg PROTO((struct insn_chain *, int, int,
392 int, FILE *));
393 static void maybe_mark_pseudo_spilled PROTO((int));
394 static void delete_dead_insn PROTO((rtx));
395 static void alter_reg PROTO((int, int));
396 static void set_label_offsets PROTO((rtx, rtx, int));
397 static void check_eliminable_occurrences PROTO((rtx));
398 static void elimination_effects PROTO((rtx, enum machine_mode));
399 static int eliminate_regs_in_insn PROTO((rtx, int));
400 static void update_eliminable_offsets PROTO((void));
401 static void mark_not_eliminable PROTO((rtx, rtx));
402 static void set_initial_elim_offsets PROTO((void));
403 static void verify_initial_elim_offsets PROTO((void));
404 static void set_initial_label_offsets PROTO((void));
405 static void set_offsets_for_label PROTO((rtx));
406 static void init_elim_table PROTO((void));
407 static void update_eliminables PROTO((HARD_REG_SET *));
408 static void spill_hard_reg PROTO((int, FILE *, int));
409 static int finish_spills PROTO((int, FILE *));
410 static void ior_hard_reg_set PROTO((HARD_REG_SET *, HARD_REG_SET *));
411 static void scan_paradoxical_subregs PROTO((rtx));
412 static int hard_reg_use_compare PROTO((const PTR, const PTR));
413 static void count_pseudo PROTO((struct hard_reg_n_uses *, int));
414 static void order_regs_for_reload PROTO((struct insn_chain *));
415 static void reload_as_needed PROTO((int));
416 static void forget_old_reloads_1 PROTO((rtx, rtx));
417 static int reload_reg_class_lower PROTO((const PTR, const PTR));
418 static void mark_reload_reg_in_use PROTO((int, int, enum reload_type,
419 enum machine_mode));
420 static void clear_reload_reg_in_use PROTO((int, int, enum reload_type,
421 enum machine_mode));
422 static int reload_reg_free_p PROTO((int, int, enum reload_type));
423 static int reload_reg_free_for_value_p PROTO((int, int, enum reload_type, rtx, rtx, int, int));
424 static int reload_reg_reaches_end_p PROTO((int, int, enum reload_type));
425 static int allocate_reload_reg PROTO((struct insn_chain *, int, int,
426 int));
427 static void choose_reload_regs PROTO((struct insn_chain *));
428 static void merge_assigned_reloads PROTO((rtx));
429 static void emit_reload_insns PROTO((struct insn_chain *));
430 static void delete_output_reload PROTO((rtx, int, int));
431 static void delete_address_reloads PROTO((rtx, rtx));
432 static void delete_address_reloads_1 PROTO((rtx, rtx, rtx));
433 static rtx inc_for_reload PROTO((rtx, rtx, rtx, int));
434 static int constraint_accepts_reg_p PROTO((const char *, rtx));
435 static void reload_cse_regs_1 PROTO((rtx));
436 static void reload_cse_invalidate_regno PROTO((int, enum machine_mode, int));
437 static int reload_cse_mem_conflict_p PROTO((rtx, rtx));
438 static void reload_cse_invalidate_mem PROTO((rtx));
439 static void reload_cse_invalidate_rtx PROTO((rtx, rtx));
440 static int reload_cse_regno_equal_p PROTO((int, rtx, enum machine_mode));
441 static int reload_cse_noop_set_p PROTO((rtx, rtx));
442 static int reload_cse_simplify_set PROTO((rtx, rtx));
443 static int reload_cse_simplify_operands PROTO((rtx));
444 static void reload_cse_check_clobber PROTO((rtx, rtx));
445 static void reload_cse_record_set PROTO((rtx, rtx));
446 static void reload_combine PROTO((void));
447 static void reload_combine_note_use PROTO((rtx *, rtx));
448 static void reload_combine_note_store PROTO((rtx, rtx));
449 static void reload_cse_move2add PROTO((rtx));
450 static void move2add_note_store PROTO((rtx, rtx));
451 #ifdef AUTO_INC_DEC
452 static void add_auto_inc_notes PROTO((rtx, rtx));
453 #endif
454 static rtx gen_mode_int PROTO((enum machine_mode,
455 HOST_WIDE_INT));
456 extern void dump_needs PROTO((struct insn_chain *, FILE *));
458 /* Initialize the reload pass once per compilation. */
460 void
461 init_reload ()
463 register int i;
465 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
466 Set spill_indirect_levels to the number of levels such addressing is
467 permitted, zero if it is not permitted at all. */
469 register rtx tem
470 = gen_rtx_MEM (Pmode,
471 gen_rtx_PLUS (Pmode,
472 gen_rtx_REG (Pmode,
473 LAST_VIRTUAL_REGISTER + 1),
474 GEN_INT (4)));
475 spill_indirect_levels = 0;
477 while (memory_address_p (QImode, tem))
479 spill_indirect_levels++;
480 tem = gen_rtx_MEM (Pmode, tem);
483 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
485 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
486 indirect_symref_ok = memory_address_p (QImode, tem);
488 /* See if reg+reg is a valid (and offsettable) address. */
490 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
492 tem = gen_rtx_PLUS (Pmode,
493 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
494 gen_rtx_REG (Pmode, i));
496 /* This way, we make sure that reg+reg is an offsettable address. */
497 tem = plus_constant (tem, 4);
499 if (memory_address_p (QImode, tem))
501 double_reg_address_ok = 1;
502 break;
506 /* Initialize obstack for our rtl allocation. */
507 gcc_obstack_init (&reload_obstack);
508 reload_startobj = (char *) obstack_alloc (&reload_obstack, 0);
511 /* List of insn chains that are currently unused. */
512 static struct insn_chain *unused_insn_chains = 0;
514 /* Allocate an empty insn_chain structure. */
515 struct insn_chain *
516 new_insn_chain ()
518 struct insn_chain *c;
520 if (unused_insn_chains == 0)
522 c = (struct insn_chain *)
523 obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
524 c->live_before = OBSTACK_ALLOC_REG_SET (&reload_obstack);
525 c->live_after = OBSTACK_ALLOC_REG_SET (&reload_obstack);
527 else
529 c = unused_insn_chains;
530 unused_insn_chains = c->next;
532 c->is_caller_save_insn = 0;
533 c->need_operand_change = 0;
534 c->need_reload = 0;
535 c->need_elim = 0;
536 return c;
539 /* Small utility function to set all regs in hard reg set TO which are
540 allocated to pseudos in regset FROM. */
541 void
542 compute_use_by_pseudos (to, from)
543 HARD_REG_SET *to;
544 regset from;
546 int regno;
547 EXECUTE_IF_SET_IN_REG_SET
548 (from, FIRST_PSEUDO_REGISTER, regno,
550 int r = reg_renumber[regno];
551 int nregs;
552 if (r < 0)
554 /* reload_combine uses the information from
555 BASIC_BLOCK->global_live_at_start, which might still
556 contain registers that have not actually been allocated
557 since they have an equivalence. */
558 if (! reload_completed)
559 abort ();
561 else
563 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (regno));
564 while (nregs-- > 0)
565 SET_HARD_REG_BIT (*to, r + nregs);
570 /* Global variables used by reload and its subroutines. */
572 /* Set during calculate_needs if an insn needs register elimination. */
573 static int something_needs_elimination;
574 /* Set during calculate_needs if an insn needs an operand changed. */
575 int something_needs_operands_changed;
577 /* Nonzero means we couldn't get enough spill regs. */
578 static int failure;
580 /* Main entry point for the reload pass.
582 FIRST is the first insn of the function being compiled.
584 GLOBAL nonzero means we were called from global_alloc
585 and should attempt to reallocate any pseudoregs that we
586 displace from hard regs we will use for reloads.
587 If GLOBAL is zero, we do not have enough information to do that,
588 so any pseudo reg that is spilled must go to the stack.
590 DUMPFILE is the global-reg debugging dump file stream, or 0.
591 If it is nonzero, messages are written to it to describe
592 which registers are seized as reload regs, which pseudo regs
593 are spilled from them, and where the pseudo regs are reallocated to.
595 Return value is nonzero if reload failed
596 and we must not do any more for this function. */
599 reload (first, global, dumpfile)
600 rtx first;
601 int global;
602 FILE *dumpfile;
604 register int i;
605 register rtx insn;
606 register struct elim_table *ep;
608 /* The two pointers used to track the true location of the memory used
609 for label offsets. */
610 char *real_known_ptr = NULL_PTR;
611 int (*real_at_ptr)[NUM_ELIMINABLE_REGS];
613 /* Make sure even insns with volatile mem refs are recognizable. */
614 init_recog ();
616 failure = 0;
618 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
620 /* Make sure that the last insn in the chain
621 is not something that needs reloading. */
622 emit_note (NULL_PTR, NOTE_INSN_DELETED);
624 /* Enable find_equiv_reg to distinguish insns made by reload. */
625 reload_first_uid = get_max_uid ();
627 #ifdef SECONDARY_MEMORY_NEEDED
628 /* Initialize the secondary memory table. */
629 clear_secondary_mem ();
630 #endif
632 /* We don't have a stack slot for any spill reg yet. */
633 bzero ((char *) spill_stack_slot, sizeof spill_stack_slot);
634 bzero ((char *) spill_stack_slot_width, sizeof spill_stack_slot_width);
636 /* Initialize the save area information for caller-save, in case some
637 are needed. */
638 init_save_areas ();
640 /* Compute which hard registers are now in use
641 as homes for pseudo registers.
642 This is done here rather than (eg) in global_alloc
643 because this point is reached even if not optimizing. */
644 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
645 mark_home_live (i);
647 /* A function that receives a nonlocal goto must save all call-saved
648 registers. */
649 if (current_function_has_nonlocal_label)
650 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
652 if (! call_used_regs[i] && ! fixed_regs[i])
653 regs_ever_live[i] = 1;
656 /* Find all the pseudo registers that didn't get hard regs
657 but do have known equivalent constants or memory slots.
658 These include parameters (known equivalent to parameter slots)
659 and cse'd or loop-moved constant memory addresses.
661 Record constant equivalents in reg_equiv_constant
662 so they will be substituted by find_reloads.
663 Record memory equivalents in reg_mem_equiv so they can
664 be substituted eventually by altering the REG-rtx's. */
666 reg_equiv_constant = (rtx *) xcalloc (max_regno, sizeof (rtx));
667 reg_equiv_memory_loc = (rtx *) xcalloc (max_regno, sizeof (rtx));
668 reg_equiv_mem = (rtx *) xcalloc (max_regno, sizeof (rtx));
669 reg_equiv_init = (rtx *) xcalloc (max_regno, sizeof (rtx));
670 reg_equiv_address = (rtx *) xcalloc (max_regno, sizeof (rtx));
671 reg_max_ref_width = (int *) xcalloc (max_regno, sizeof (int));
672 reg_old_renumber = (short *) xcalloc (max_regno, sizeof (short));
673 bcopy ((PTR) reg_renumber, (PTR) reg_old_renumber, max_regno * sizeof (short));
674 pseudo_forbidden_regs
675 = (HARD_REG_SET *) xmalloc (max_regno * sizeof (HARD_REG_SET));
676 pseudo_previous_regs
677 = (HARD_REG_SET *) xcalloc (max_regno, sizeof (HARD_REG_SET));
679 CLEAR_HARD_REG_SET (bad_spill_regs_global);
681 /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
682 Also find all paradoxical subregs and find largest such for each pseudo.
683 On machines with small register classes, record hard registers that
684 are used for user variables. These can never be used for spills.
685 Also look for a "constant" NOTE_INSN_SETJMP. This means that all
686 caller-saved registers must be marked live. */
688 num_eliminable_invariants = 0;
689 for (insn = first; insn; insn = NEXT_INSN (insn))
691 rtx set = single_set (insn);
693 if (GET_CODE (insn) == NOTE && CONST_CALL_P (insn)
694 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
695 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
696 if (! call_used_regs[i])
697 regs_ever_live[i] = 1;
699 if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
701 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
702 if (note
703 #ifdef LEGITIMATE_PIC_OPERAND_P
704 && (! function_invariant_p (XEXP (note, 0))
705 || ! flag_pic
706 || LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))
707 #endif
710 rtx x = XEXP (note, 0);
711 i = REGNO (SET_DEST (set));
712 if (i > LAST_VIRTUAL_REGISTER)
714 if (GET_CODE (x) == MEM)
716 /* If the operand is a PLUS, the MEM may be shared,
717 so make sure we have an unshared copy here. */
718 if (GET_CODE (XEXP (x, 0)) == PLUS)
719 x = copy_rtx (x);
721 reg_equiv_memory_loc[i] = x;
723 else if (function_invariant_p (x))
725 if (GET_CODE (x) == PLUS)
727 /* This is PLUS of frame pointer and a constant,
728 and might be shared. Unshare it. */
729 reg_equiv_constant[i] = copy_rtx (x);
730 num_eliminable_invariants++;
732 else if (x == frame_pointer_rtx
733 || x == arg_pointer_rtx)
735 reg_equiv_constant[i] = x;
736 num_eliminable_invariants++;
738 else if (LEGITIMATE_CONSTANT_P (x))
739 reg_equiv_constant[i] = x;
740 else
741 reg_equiv_memory_loc[i]
742 = force_const_mem (GET_MODE (SET_DEST (set)), x);
744 else
745 continue;
747 /* If this register is being made equivalent to a MEM
748 and the MEM is not SET_SRC, the equivalencing insn
749 is one with the MEM as a SET_DEST and it occurs later.
750 So don't mark this insn now. */
751 if (GET_CODE (x) != MEM
752 || rtx_equal_p (SET_SRC (set), x))
753 reg_equiv_init[i]
754 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[i]);
759 /* If this insn is setting a MEM from a register equivalent to it,
760 this is the equivalencing insn. */
761 else if (set && GET_CODE (SET_DEST (set)) == MEM
762 && GET_CODE (SET_SRC (set)) == REG
763 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
764 && rtx_equal_p (SET_DEST (set),
765 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
766 reg_equiv_init[REGNO (SET_SRC (set))]
767 = gen_rtx_INSN_LIST (VOIDmode, insn,
768 reg_equiv_init[REGNO (SET_SRC (set))]);
770 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
771 scan_paradoxical_subregs (PATTERN (insn));
774 init_elim_table ();
776 num_labels = max_label_num () - get_first_label_num ();
778 /* Allocate the tables used to store offset information at labels. */
779 /* We used to use alloca here, but the size of what it would try to
780 allocate would occasionally cause it to exceed the stack limit and
781 cause a core dump. */
782 real_known_ptr = xmalloc (num_labels);
783 real_at_ptr
784 = (int (*)[NUM_ELIMINABLE_REGS])
785 xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
787 offsets_known_at = real_known_ptr - get_first_label_num ();
788 offsets_at
789 = (int (*)[NUM_ELIMINABLE_REGS]) (real_at_ptr - get_first_label_num ());
791 /* Alter each pseudo-reg rtx to contain its hard reg number.
792 Assign stack slots to the pseudos that lack hard regs or equivalents.
793 Do not touch virtual registers. */
795 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
796 alter_reg (i, -1);
798 /* If we have some registers we think can be eliminated, scan all insns to
799 see if there is an insn that sets one of these registers to something
800 other than itself plus a constant. If so, the register cannot be
801 eliminated. Doing this scan here eliminates an extra pass through the
802 main reload loop in the most common case where register elimination
803 cannot be done. */
804 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
805 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
806 || GET_CODE (insn) == CALL_INSN)
807 note_stores (PATTERN (insn), mark_not_eliminable);
809 maybe_fix_stack_asms ();
811 insns_need_reload = 0;
812 something_needs_elimination = 0;
814 /* Initialize to -1, which means take the first spill register. */
815 last_spill_reg = -1;
817 spilled_pseudos = ALLOCA_REG_SET ();
819 /* Spill any hard regs that we know we can't eliminate. */
820 CLEAR_HARD_REG_SET (used_spill_regs);
821 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
822 if (! ep->can_eliminate)
823 spill_hard_reg (ep->from, dumpfile, 1);
825 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
826 if (frame_pointer_needed)
827 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, dumpfile, 1);
828 #endif
829 finish_spills (global, dumpfile);
831 /* From now on, we may need to generate moves differently. We may also
832 allow modifications of insns which cause them to not be recognized.
833 Any such modifications will be cleaned up during reload itself. */
834 reload_in_progress = 1;
836 /* This loop scans the entire function each go-round
837 and repeats until one repetition spills no additional hard regs. */
838 for (;;)
840 int something_changed;
841 int did_spill;
842 struct insn_chain *chain;
844 HOST_WIDE_INT starting_frame_size;
846 /* Round size of stack frame to BIGGEST_ALIGNMENT. This must be done
847 here because the stack size may be a part of the offset computation
848 for register elimination, and there might have been new stack slots
849 created in the last iteration of this loop. */
850 assign_stack_local (BLKmode, 0, 0);
852 starting_frame_size = get_frame_size ();
854 set_initial_elim_offsets ();
855 set_initial_label_offsets ();
857 /* For each pseudo register that has an equivalent location defined,
858 try to eliminate any eliminable registers (such as the frame pointer)
859 assuming initial offsets for the replacement register, which
860 is the normal case.
862 If the resulting location is directly addressable, substitute
863 the MEM we just got directly for the old REG.
865 If it is not addressable but is a constant or the sum of a hard reg
866 and constant, it is probably not addressable because the constant is
867 out of range, in that case record the address; we will generate
868 hairy code to compute the address in a register each time it is
869 needed. Similarly if it is a hard register, but one that is not
870 valid as an address register.
872 If the location is not addressable, but does not have one of the
873 above forms, assign a stack slot. We have to do this to avoid the
874 potential of producing lots of reloads if, e.g., a location involves
875 a pseudo that didn't get a hard register and has an equivalent memory
876 location that also involves a pseudo that didn't get a hard register.
878 Perhaps at some point we will improve reload_when_needed handling
879 so this problem goes away. But that's very hairy. */
881 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
882 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
884 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
886 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
887 XEXP (x, 0)))
888 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
889 else if (CONSTANT_P (XEXP (x, 0))
890 || (GET_CODE (XEXP (x, 0)) == REG
891 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
892 || (GET_CODE (XEXP (x, 0)) == PLUS
893 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
894 && (REGNO (XEXP (XEXP (x, 0), 0))
895 < FIRST_PSEUDO_REGISTER)
896 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
897 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
898 else
900 /* Make a new stack slot. Then indicate that something
901 changed so we go back and recompute offsets for
902 eliminable registers because the allocation of memory
903 below might change some offset. reg_equiv_{mem,address}
904 will be set up for this pseudo on the next pass around
905 the loop. */
906 reg_equiv_memory_loc[i] = 0;
907 reg_equiv_init[i] = 0;
908 alter_reg (i, -1);
912 if (caller_save_needed)
913 setup_save_areas ();
915 /* If we allocated another stack slot, redo elimination bookkeeping. */
916 if (starting_frame_size != get_frame_size ())
917 continue;
919 if (caller_save_needed)
921 save_call_clobbered_regs ();
922 /* That might have allocated new insn_chain structures. */
923 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
926 calculate_needs_all_insns (global);
928 CLEAR_REG_SET (spilled_pseudos);
929 did_spill = 0;
931 something_changed = 0;
933 /* If we allocated any new memory locations, make another pass
934 since it might have changed elimination offsets. */
935 if (starting_frame_size != get_frame_size ())
936 something_changed = 1;
939 HARD_REG_SET to_spill;
940 CLEAR_HARD_REG_SET (to_spill);
941 update_eliminables (&to_spill);
942 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
943 if (TEST_HARD_REG_BIT (to_spill, i))
945 spill_hard_reg (i, dumpfile, 1);
946 did_spill = 1;
948 /* Regardless of the state of spills, if we previously had
949 a register that we thought we could eliminate, but no can
950 not eliminate, we must run another pass.
952 Consider pseudos which have an entry in reg_equiv_* which
953 reference an eliminable register. We must make another pass
954 to update reg_equiv_* so that we do not substitute in the
955 old value from when we thought the elimination could be
956 performed. */
957 something_changed = 1;
961 CLEAR_HARD_REG_SET (used_spill_regs);
962 /* Try to satisfy the needs for each insn. */
963 for (chain = insns_need_reload; chain != 0;
964 chain = chain->next_need_reload)
965 find_reload_regs (chain, dumpfile);
967 if (failure)
968 goto failed;
970 if (insns_need_reload != 0 || did_spill)
971 something_changed |= finish_spills (global, dumpfile);
973 if (! something_changed)
974 break;
976 if (caller_save_needed)
977 delete_caller_save_insns ();
980 /* If global-alloc was run, notify it of any register eliminations we have
981 done. */
982 if (global)
983 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
984 if (ep->can_eliminate)
985 mark_elimination (ep->from, ep->to);
987 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
988 If that insn didn't set the register (i.e., it copied the register to
989 memory), just delete that insn instead of the equivalencing insn plus
990 anything now dead. If we call delete_dead_insn on that insn, we may
991 delete the insn that actually sets the register if the register dies
992 there and that is incorrect. */
994 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
996 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
998 rtx list;
999 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1001 rtx equiv_insn = XEXP (list, 0);
1002 if (GET_CODE (equiv_insn) == NOTE)
1003 continue;
1004 if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1005 delete_dead_insn (equiv_insn);
1006 else
1008 PUT_CODE (equiv_insn, NOTE);
1009 NOTE_SOURCE_FILE (equiv_insn) = 0;
1010 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1016 /* Use the reload registers where necessary
1017 by generating move instructions to move the must-be-register
1018 values into or out of the reload registers. */
1020 if (insns_need_reload != 0 || something_needs_elimination
1021 || something_needs_operands_changed)
1023 int old_frame_size = get_frame_size ();
1025 reload_as_needed (global);
1027 if (old_frame_size != get_frame_size ())
1028 abort ();
1030 if (num_eliminable)
1031 verify_initial_elim_offsets ();
1034 /* If we were able to eliminate the frame pointer, show that it is no
1035 longer live at the start of any basic block. If it ls live by
1036 virtue of being in a pseudo, that pseudo will be marked live
1037 and hence the frame pointer will be known to be live via that
1038 pseudo. */
1040 if (! frame_pointer_needed)
1041 for (i = 0; i < n_basic_blocks; i++)
1042 CLEAR_REGNO_REG_SET (BASIC_BLOCK (i)->global_live_at_start,
1043 HARD_FRAME_POINTER_REGNUM);
1045 /* Come here (with failure set nonzero) if we can't get enough spill regs
1046 and we decide not to abort about it. */
1047 failed:
1049 reload_in_progress = 0;
1051 /* Now eliminate all pseudo regs by modifying them into
1052 their equivalent memory references.
1053 The REG-rtx's for the pseudos are modified in place,
1054 so all insns that used to refer to them now refer to memory.
1056 For a reg that has a reg_equiv_address, all those insns
1057 were changed by reloading so that no insns refer to it any longer;
1058 but the DECL_RTL of a variable decl may refer to it,
1059 and if so this causes the debugging info to mention the variable. */
1061 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1063 rtx addr = 0;
1064 int in_struct = 0;
1065 int is_scalar = 0;
1066 int is_readonly = 0;
1068 if (reg_equiv_memory_loc[i])
1070 in_struct = MEM_IN_STRUCT_P (reg_equiv_memory_loc[i]);
1071 is_scalar = MEM_SCALAR_P (reg_equiv_memory_loc[i]);
1072 is_readonly = RTX_UNCHANGING_P (reg_equiv_memory_loc[i]);
1075 if (reg_equiv_mem[i])
1076 addr = XEXP (reg_equiv_mem[i], 0);
1078 if (reg_equiv_address[i])
1079 addr = reg_equiv_address[i];
1081 if (addr)
1083 if (reg_renumber[i] < 0)
1085 rtx reg = regno_reg_rtx[i];
1086 PUT_CODE (reg, MEM);
1087 XEXP (reg, 0) = addr;
1088 REG_USERVAR_P (reg) = 0;
1089 RTX_UNCHANGING_P (reg) = is_readonly;
1090 MEM_IN_STRUCT_P (reg) = in_struct;
1091 MEM_SCALAR_P (reg) = is_scalar;
1092 /* We have no alias information about this newly created
1093 MEM. */
1094 MEM_ALIAS_SET (reg) = 0;
1096 else if (reg_equiv_mem[i])
1097 XEXP (reg_equiv_mem[i], 0) = addr;
1101 /* We must set reload_completed now since the cleanup_subreg_operands call
1102 below will re-recognize each insn and reload may have generated insns
1103 which are only valid during and after reload. */
1104 reload_completed = 1;
1106 /* Make a pass over all the insns and delete all USEs which we
1107 inserted only to tag a REG_EQUAL note on them. Remove all
1108 REG_DEAD and REG_UNUSED notes. Delete all CLOBBER insns and
1109 simplify (subreg (reg)) operands. Also remove all REG_RETVAL and
1110 REG_LIBCALL notes since they are no longer useful or accurate.
1111 Strip and regenerate REG_INC notes that may have been moved
1112 around. */
1114 for (insn = first; insn; insn = NEXT_INSN (insn))
1115 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1117 rtx *pnote;
1119 if ((GET_CODE (PATTERN (insn)) == USE
1120 && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1121 || GET_CODE (PATTERN (insn)) == CLOBBER)
1123 PUT_CODE (insn, NOTE);
1124 NOTE_SOURCE_FILE (insn) = 0;
1125 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1126 continue;
1129 pnote = &REG_NOTES (insn);
1130 while (*pnote != 0)
1132 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1133 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1134 || REG_NOTE_KIND (*pnote) == REG_INC
1135 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1136 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1137 *pnote = XEXP (*pnote, 1);
1138 else
1139 pnote = &XEXP (*pnote, 1);
1142 #ifdef AUTO_INC_DEC
1143 add_auto_inc_notes (insn, PATTERN (insn));
1144 #endif
1146 /* And simplify (subreg (reg)) if it appears as an operand. */
1147 cleanup_subreg_operands (insn);
1150 /* If we are doing stack checking, give a warning if this function's
1151 frame size is larger than we expect. */
1152 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1154 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1155 static int verbose_warned = 0;
1157 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1158 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1159 size += UNITS_PER_WORD;
1161 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1163 warning ("frame size too large for reliable stack checking");
1164 if (! verbose_warned)
1166 warning ("try reducing the number of local variables");
1167 verbose_warned = 1;
1172 /* Indicate that we no longer have known memory locations or constants. */
1173 if (reg_equiv_constant)
1174 free (reg_equiv_constant);
1175 reg_equiv_constant = 0;
1176 if (reg_equiv_memory_loc)
1177 free (reg_equiv_memory_loc);
1178 reg_equiv_memory_loc = 0;
1180 if (real_known_ptr)
1181 free (real_known_ptr);
1182 if (real_at_ptr)
1183 free (real_at_ptr);
1185 free (reg_equiv_mem);
1186 free (reg_equiv_init);
1187 free (reg_equiv_address);
1188 free (reg_max_ref_width);
1189 free (reg_old_renumber);
1190 free (pseudo_previous_regs);
1191 free (pseudo_forbidden_regs);
1193 FREE_REG_SET (spilled_pseudos);
1195 CLEAR_HARD_REG_SET (used_spill_regs);
1196 for (i = 0; i < n_spills; i++)
1197 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1199 /* Free all the insn_chain structures at once. */
1200 obstack_free (&reload_obstack, reload_startobj);
1201 unused_insn_chains = 0;
1203 return failure;
1206 /* Yet another special case. Unfortunately, reg-stack forces people to
1207 write incorrect clobbers in asm statements. These clobbers must not
1208 cause the register to appear in bad_spill_regs, otherwise we'll call
1209 fatal_insn later. We clear the corresponding regnos in the live
1210 register sets to avoid this.
1211 The whole thing is rather sick, I'm afraid. */
1212 static void
1213 maybe_fix_stack_asms ()
1215 #ifdef STACK_REGS
1216 const char *constraints[MAX_RECOG_OPERANDS];
1217 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1218 struct insn_chain *chain;
1220 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1222 int i, noperands;
1223 HARD_REG_SET clobbered, allowed;
1224 rtx pat;
1226 if (GET_RTX_CLASS (GET_CODE (chain->insn)) != 'i'
1227 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1228 continue;
1229 pat = PATTERN (chain->insn);
1230 if (GET_CODE (pat) != PARALLEL)
1231 continue;
1233 CLEAR_HARD_REG_SET (clobbered);
1234 CLEAR_HARD_REG_SET (allowed);
1236 /* First, make a mask of all stack regs that are clobbered. */
1237 for (i = 0; i < XVECLEN (pat, 0); i++)
1239 rtx t = XVECEXP (pat, 0, i);
1240 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1241 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1244 /* Get the operand values and constraints out of the insn. */
1245 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1246 constraints, operand_mode);
1248 /* For every operand, see what registers are allowed. */
1249 for (i = 0; i < noperands; i++)
1251 const char *p = constraints[i];
1252 /* For every alternative, we compute the class of registers allowed
1253 for reloading in CLS, and merge its contents into the reg set
1254 ALLOWED. */
1255 int cls = (int) NO_REGS;
1257 for (;;)
1259 char c = *p++;
1261 if (c == '\0' || c == ',' || c == '#')
1263 /* End of one alternative - mark the regs in the current
1264 class, and reset the class. */
1265 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1266 cls = NO_REGS;
1267 if (c == '#')
1268 do {
1269 c = *p++;
1270 } while (c != '\0' && c != ',');
1271 if (c == '\0')
1272 break;
1273 continue;
1276 switch (c)
1278 case '=': case '+': case '*': case '%': case '?': case '!':
1279 case '0': case '1': case '2': case '3': case '4': case 'm':
1280 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1281 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1282 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1283 case 'P':
1284 #ifdef EXTRA_CONSTRAINT
1285 case 'Q': case 'R': case 'S': case 'T': case 'U':
1286 #endif
1287 break;
1289 case 'p':
1290 cls = (int) reg_class_subunion[cls][(int) BASE_REG_CLASS];
1291 break;
1293 case 'g':
1294 case 'r':
1295 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1296 break;
1298 default:
1299 cls = (int) reg_class_subunion[cls][(int) REG_CLASS_FROM_LETTER (c)];
1304 /* Those of the registers which are clobbered, but allowed by the
1305 constraints, must be usable as reload registers. So clear them
1306 out of the life information. */
1307 AND_HARD_REG_SET (allowed, clobbered);
1308 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1309 if (TEST_HARD_REG_BIT (allowed, i))
1311 CLEAR_REGNO_REG_SET (chain->live_before, i);
1312 CLEAR_REGNO_REG_SET (chain->live_after, i);
1316 #endif
1320 /* Walk the chain of insns, and determine for each whether it needs reloads
1321 and/or eliminations. Build the corresponding insns_need_reload list, and
1322 set something_needs_elimination as appropriate. */
1323 static void
1324 calculate_needs_all_insns (global)
1325 int global;
1327 struct insn_chain **pprev_reload = &insns_need_reload;
1328 struct insn_chain **pchain;
1330 something_needs_elimination = 0;
1332 for (pchain = &reload_insn_chain; *pchain != 0; pchain = &(*pchain)->next)
1334 rtx insn;
1335 struct insn_chain *chain;
1337 chain = *pchain;
1338 insn = chain->insn;
1340 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1341 include REG_LABEL), we need to see what effects this has on the
1342 known offsets at labels. */
1344 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
1345 || (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1346 && REG_NOTES (insn) != 0))
1347 set_label_offsets (insn, insn, 0);
1349 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1351 rtx old_body = PATTERN (insn);
1352 int old_code = INSN_CODE (insn);
1353 rtx old_notes = REG_NOTES (insn);
1354 int did_elimination = 0;
1355 int operands_changed = 0;
1356 rtx set = single_set (insn);
1358 /* Skip insns that only set an equivalence. */
1359 if (set && GET_CODE (SET_DEST (set)) == REG
1360 && reg_renumber[REGNO (SET_DEST (set))] < 0
1361 && reg_equiv_constant[REGNO (SET_DEST (set))])
1363 /* Must clear out the shortcuts, in case they were set last
1364 time through. */
1365 chain->need_elim = 0;
1366 chain->need_reload = 0;
1367 chain->need_operand_change = 0;
1368 continue;
1371 /* If needed, eliminate any eliminable registers. */
1372 if (num_eliminable || num_eliminable_invariants)
1373 did_elimination = eliminate_regs_in_insn (insn, 0);
1375 /* Analyze the instruction. */
1376 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1377 global, spill_reg_order);
1379 /* If a no-op set needs more than one reload, this is likely
1380 to be something that needs input address reloads. We
1381 can't get rid of this cleanly later, and it is of no use
1382 anyway, so discard it now.
1383 We only do this when expensive_optimizations is enabled,
1384 since this complements reload inheritance / output
1385 reload deletion, and it can make debugging harder. */
1386 if (flag_expensive_optimizations && n_reloads > 1)
1388 rtx set = single_set (insn);
1389 if (set
1390 && SET_SRC (set) == SET_DEST (set)
1391 && GET_CODE (SET_SRC (set)) == REG
1392 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1394 PUT_CODE (insn, NOTE);
1395 NOTE_SOURCE_FILE (insn) = 0;
1396 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1397 continue;
1400 if (num_eliminable)
1401 update_eliminable_offsets ();
1403 /* Remember for later shortcuts which insns had any reloads or
1404 register eliminations. */
1405 chain->need_elim = did_elimination;
1406 chain->need_reload = n_reloads > 0;
1407 chain->need_operand_change = operands_changed;
1409 /* Discard any register replacements done. */
1410 if (did_elimination)
1412 obstack_free (&reload_obstack, reload_firstobj);
1413 PATTERN (insn) = old_body;
1414 INSN_CODE (insn) = old_code;
1415 REG_NOTES (insn) = old_notes;
1416 something_needs_elimination = 1;
1419 something_needs_operands_changed |= operands_changed;
1421 if (n_reloads != 0)
1423 *pprev_reload = chain;
1424 pprev_reload = &chain->next_need_reload;
1426 calculate_needs (chain);
1430 *pprev_reload = 0;
1433 /* Compute the most additional registers needed by one instruction,
1434 given by CHAIN. Collect information separately for each class of regs.
1436 To compute the number of reload registers of each class needed for an
1437 insn, we must simulate what choose_reload_regs can do. We do this by
1438 splitting an insn into an "input" and an "output" part. RELOAD_OTHER
1439 reloads are used in both. The input part uses those reloads,
1440 RELOAD_FOR_INPUT reloads, which must be live over the entire input section
1441 of reloads, and the maximum of all the RELOAD_FOR_INPUT_ADDRESS and
1442 RELOAD_FOR_OPERAND_ADDRESS reloads, which conflict with the inputs.
1444 The registers needed for output are RELOAD_OTHER and RELOAD_FOR_OUTPUT,
1445 which are live for the entire output portion, and the maximum of all the
1446 RELOAD_FOR_OUTPUT_ADDRESS reloads for each operand.
1448 The total number of registers needed is the maximum of the
1449 inputs and outputs. */
1451 static void
1452 calculate_needs (chain)
1453 struct insn_chain *chain;
1455 int i;
1457 /* Each `struct needs' corresponds to one RELOAD_... type. */
1458 struct {
1459 struct needs other;
1460 struct needs input;
1461 struct needs output;
1462 struct needs insn;
1463 struct needs other_addr;
1464 struct needs op_addr;
1465 struct needs op_addr_reload;
1466 struct needs in_addr[MAX_RECOG_OPERANDS];
1467 struct needs in_addr_addr[MAX_RECOG_OPERANDS];
1468 struct needs out_addr[MAX_RECOG_OPERANDS];
1469 struct needs out_addr_addr[MAX_RECOG_OPERANDS];
1470 } insn_needs;
1472 bzero ((char *) chain->group_size, sizeof chain->group_size);
1473 for (i = 0; i < N_REG_CLASSES; i++)
1474 chain->group_mode[i] = VOIDmode;
1475 bzero ((char *) &insn_needs, sizeof insn_needs);
1477 /* Count each reload once in every class
1478 containing the reload's own class. */
1480 for (i = 0; i < n_reloads; i++)
1482 register enum reg_class *p;
1483 enum reg_class class = rld[i].class;
1484 int size;
1485 enum machine_mode mode;
1486 struct needs *this_needs;
1488 /* Don't count the dummy reloads, for which one of the
1489 regs mentioned in the insn can be used for reloading.
1490 Don't count optional reloads.
1491 Don't count reloads that got combined with others. */
1492 if (rld[i].reg_rtx != 0
1493 || rld[i].optional != 0
1494 || (rld[i].out == 0 && rld[i].in == 0
1495 && ! rld[i].secondary_p))
1496 continue;
1498 mode = rld[i].inmode;
1499 if (GET_MODE_SIZE (rld[i].outmode) > GET_MODE_SIZE (mode))
1500 mode = rld[i].outmode;
1501 size = CLASS_MAX_NREGS (class, mode);
1503 /* Decide which time-of-use to count this reload for. */
1504 switch (rld[i].when_needed)
1506 case RELOAD_OTHER:
1507 this_needs = &insn_needs.other;
1508 break;
1509 case RELOAD_FOR_INPUT:
1510 this_needs = &insn_needs.input;
1511 break;
1512 case RELOAD_FOR_OUTPUT:
1513 this_needs = &insn_needs.output;
1514 break;
1515 case RELOAD_FOR_INSN:
1516 this_needs = &insn_needs.insn;
1517 break;
1518 case RELOAD_FOR_OTHER_ADDRESS:
1519 this_needs = &insn_needs.other_addr;
1520 break;
1521 case RELOAD_FOR_INPUT_ADDRESS:
1522 this_needs = &insn_needs.in_addr[rld[i].opnum];
1523 break;
1524 case RELOAD_FOR_INPADDR_ADDRESS:
1525 this_needs = &insn_needs.in_addr_addr[rld[i].opnum];
1526 break;
1527 case RELOAD_FOR_OUTPUT_ADDRESS:
1528 this_needs = &insn_needs.out_addr[rld[i].opnum];
1529 break;
1530 case RELOAD_FOR_OUTADDR_ADDRESS:
1531 this_needs = &insn_needs.out_addr_addr[rld[i].opnum];
1532 break;
1533 case RELOAD_FOR_OPERAND_ADDRESS:
1534 this_needs = &insn_needs.op_addr;
1535 break;
1536 case RELOAD_FOR_OPADDR_ADDR:
1537 this_needs = &insn_needs.op_addr_reload;
1538 break;
1539 default:
1540 abort();
1543 if (size > 1)
1545 enum machine_mode other_mode, allocate_mode;
1547 /* Count number of groups needed separately from
1548 number of individual regs needed. */
1549 this_needs->groups[(int) class]++;
1550 p = reg_class_superclasses[(int) class];
1551 while (*p != LIM_REG_CLASSES)
1552 this_needs->groups[(int) *p++]++;
1554 /* Record size and mode of a group of this class. */
1555 /* If more than one size group is needed,
1556 make all groups the largest needed size. */
1557 if (chain->group_size[(int) class] < size)
1559 other_mode = chain->group_mode[(int) class];
1560 allocate_mode = mode;
1562 chain->group_size[(int) class] = size;
1563 chain->group_mode[(int) class] = mode;
1565 else
1567 other_mode = mode;
1568 allocate_mode = chain->group_mode[(int) class];
1571 /* Crash if two dissimilar machine modes both need
1572 groups of consecutive regs of the same class. */
1574 if (other_mode != VOIDmode && other_mode != allocate_mode
1575 && ! modes_equiv_for_class_p (allocate_mode,
1576 other_mode, class))
1577 fatal_insn ("Two dissimilar machine modes both need groups of consecutive regs of the same class",
1578 chain->insn);
1580 else if (size == 1)
1582 this_needs->regs[(unsigned char)rld[i].nongroup][(int) class] += 1;
1583 p = reg_class_superclasses[(int) class];
1584 while (*p != LIM_REG_CLASSES)
1585 this_needs->regs[(unsigned char)rld[i].nongroup][(int) *p++] += 1;
1587 else
1588 abort ();
1591 /* All reloads have been counted for this insn;
1592 now merge the various times of use.
1593 This sets insn_needs, etc., to the maximum total number
1594 of registers needed at any point in this insn. */
1596 for (i = 0; i < N_REG_CLASSES; i++)
1598 int j, in_max, out_max;
1600 /* Compute normal and nongroup needs. */
1601 for (j = 0; j <= 1; j++)
1603 int k;
1604 for (in_max = 0, out_max = 0, k = 0; k < reload_n_operands; k++)
1606 in_max = MAX (in_max,
1607 (insn_needs.in_addr[k].regs[j][i]
1608 + insn_needs.in_addr_addr[k].regs[j][i]));
1609 out_max = MAX (out_max, insn_needs.out_addr[k].regs[j][i]);
1610 out_max = MAX (out_max,
1611 insn_needs.out_addr_addr[k].regs[j][i]);
1614 /* RELOAD_FOR_INSN reloads conflict with inputs, outputs,
1615 and operand addresses but not things used to reload
1616 them. Similarly, RELOAD_FOR_OPERAND_ADDRESS reloads
1617 don't conflict with things needed to reload inputs or
1618 outputs. */
1620 in_max = MAX (MAX (insn_needs.op_addr.regs[j][i],
1621 insn_needs.op_addr_reload.regs[j][i]),
1622 in_max);
1624 out_max = MAX (out_max, insn_needs.insn.regs[j][i]);
1626 insn_needs.input.regs[j][i]
1627 = MAX (insn_needs.input.regs[j][i]
1628 + insn_needs.op_addr.regs[j][i]
1629 + insn_needs.insn.regs[j][i],
1630 in_max + insn_needs.input.regs[j][i]);
1632 insn_needs.output.regs[j][i] += out_max;
1633 insn_needs.other.regs[j][i]
1634 += MAX (MAX (insn_needs.input.regs[j][i],
1635 insn_needs.output.regs[j][i]),
1636 insn_needs.other_addr.regs[j][i]);
1640 /* Now compute group needs. */
1641 for (in_max = 0, out_max = 0, j = 0; j < reload_n_operands; j++)
1643 in_max = MAX (in_max, insn_needs.in_addr[j].groups[i]);
1644 in_max = MAX (in_max, insn_needs.in_addr_addr[j].groups[i]);
1645 out_max = MAX (out_max, insn_needs.out_addr[j].groups[i]);
1646 out_max = MAX (out_max, insn_needs.out_addr_addr[j].groups[i]);
1649 in_max = MAX (MAX (insn_needs.op_addr.groups[i],
1650 insn_needs.op_addr_reload.groups[i]),
1651 in_max);
1652 out_max = MAX (out_max, insn_needs.insn.groups[i]);
1654 insn_needs.input.groups[i]
1655 = MAX (insn_needs.input.groups[i]
1656 + insn_needs.op_addr.groups[i]
1657 + insn_needs.insn.groups[i],
1658 in_max + insn_needs.input.groups[i]);
1660 insn_needs.output.groups[i] += out_max;
1661 insn_needs.other.groups[i]
1662 += MAX (MAX (insn_needs.input.groups[i],
1663 insn_needs.output.groups[i]),
1664 insn_needs.other_addr.groups[i]);
1667 /* Record the needs for later. */
1668 chain->need = insn_needs.other;
1671 /* Find a group of exactly 2 registers.
1673 First try to fill out the group by spilling a single register which
1674 would allow completion of the group.
1676 Then try to create a new group from a pair of registers, neither of
1677 which are explicitly used.
1679 Then try to create a group from any pair of registers. */
1681 static void
1682 find_tworeg_group (chain, class, dumpfile)
1683 struct insn_chain *chain;
1684 int class;
1685 FILE *dumpfile;
1687 int i;
1688 /* First, look for a register that will complete a group. */
1689 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1691 int j, other;
1693 j = potential_reload_regs[i];
1694 if (j >= 0 && ! TEST_HARD_REG_BIT (bad_spill_regs, j)
1695 && ((j > 0 && (other = j - 1, spill_reg_order[other] >= 0)
1696 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1697 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1698 && HARD_REGNO_MODE_OK (other, chain->group_mode[class])
1699 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, other)
1700 /* We don't want one part of another group.
1701 We could get "two groups" that overlap! */
1702 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, other))
1703 || (j < FIRST_PSEUDO_REGISTER - 1
1704 && (other = j + 1, spill_reg_order[other] >= 0)
1705 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1706 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1707 && HARD_REGNO_MODE_OK (j, chain->group_mode[class])
1708 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, other)
1709 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, other))))
1711 register enum reg_class *p;
1713 /* We have found one that will complete a group,
1714 so count off one group as provided. */
1715 chain->need.groups[class]--;
1716 p = reg_class_superclasses[class];
1717 while (*p != LIM_REG_CLASSES)
1719 if (chain->group_size [(int) *p] <= chain->group_size [class])
1720 chain->need.groups[(int) *p]--;
1721 p++;
1724 /* Indicate both these regs are part of a group. */
1725 SET_HARD_REG_BIT (chain->counted_for_groups, j);
1726 SET_HARD_REG_BIT (chain->counted_for_groups, other);
1727 break;
1730 /* We can't complete a group, so start one. */
1731 if (i == FIRST_PSEUDO_REGISTER)
1732 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1734 int j, k;
1735 j = potential_reload_regs[i];
1736 /* Verify that J+1 is a potential reload reg. */
1737 for (k = 0; k < FIRST_PSEUDO_REGISTER; k++)
1738 if (potential_reload_regs[k] == j + 1)
1739 break;
1740 if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1741 && k < FIRST_PSEUDO_REGISTER
1742 && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1743 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1744 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1745 && HARD_REGNO_MODE_OK (j, chain->group_mode[class])
1746 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, j + 1)
1747 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + 1))
1748 break;
1751 /* I should be the index in potential_reload_regs
1752 of the new reload reg we have found. */
1754 new_spill_reg (chain, i, class, 0, dumpfile);
1757 /* Find a group of more than 2 registers.
1758 Look for a sufficient sequence of unspilled registers, and spill them all
1759 at once. */
1761 static void
1762 find_group (chain, class, dumpfile)
1763 struct insn_chain *chain;
1764 int class;
1765 FILE *dumpfile;
1767 int i;
1769 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1771 int j = potential_reload_regs[i];
1773 if (j >= 0
1774 && j + chain->group_size[class] <= FIRST_PSEUDO_REGISTER
1775 && HARD_REGNO_MODE_OK (j, chain->group_mode[class]))
1777 int k;
1778 /* Check each reg in the sequence. */
1779 for (k = 0; k < chain->group_size[class]; k++)
1780 if (! (spill_reg_order[j + k] < 0
1781 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + k)
1782 && TEST_HARD_REG_BIT (reg_class_contents[class], j + k)))
1783 break;
1784 /* We got a full sequence, so spill them all. */
1785 if (k == chain->group_size[class])
1787 register enum reg_class *p;
1788 for (k = 0; k < chain->group_size[class]; k++)
1790 int idx;
1791 SET_HARD_REG_BIT (chain->counted_for_groups, j + k);
1792 for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
1793 if (potential_reload_regs[idx] == j + k)
1794 break;
1795 new_spill_reg (chain, idx, class, 0, dumpfile);
1798 /* We have found one that will complete a group,
1799 so count off one group as provided. */
1800 chain->need.groups[class]--;
1801 p = reg_class_superclasses[class];
1802 while (*p != LIM_REG_CLASSES)
1804 if (chain->group_size [(int) *p]
1805 <= chain->group_size [class])
1806 chain->need.groups[(int) *p]--;
1807 p++;
1809 return;
1813 /* There are no groups left. */
1814 spill_failure (chain->insn);
1815 failure = 1;
1818 /* If pseudo REG conflicts with one of our reload registers, mark it as
1819 spilled. */
1820 static void
1821 maybe_mark_pseudo_spilled (reg)
1822 int reg;
1824 int i;
1825 int r = reg_renumber[reg];
1826 int nregs;
1828 if (r < 0)
1829 abort ();
1830 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
1831 for (i = 0; i < n_spills; i++)
1832 if (r <= spill_regs[i] && r + nregs > spill_regs[i])
1834 SET_REGNO_REG_SET (spilled_pseudos, reg);
1835 return;
1839 /* Find more reload regs to satisfy the remaining need of an insn, which
1840 is given by CHAIN.
1841 Do it by ascending class number, since otherwise a reg
1842 might be spilled for a big class and might fail to count
1843 for a smaller class even though it belongs to that class.
1845 Count spilled regs in `spills', and add entries to
1846 `spill_regs' and `spill_reg_order'.
1848 ??? Note there is a problem here.
1849 When there is a need for a group in a high-numbered class,
1850 and also need for non-group regs that come from a lower class,
1851 the non-group regs are chosen first. If there aren't many regs,
1852 they might leave no room for a group.
1854 This was happening on the 386. To fix it, we added the code
1855 that calls possible_group_p, so that the lower class won't
1856 break up the last possible group.
1858 Really fixing the problem would require changes above
1859 in counting the regs already spilled, and in choose_reload_regs.
1860 It might be hard to avoid introducing bugs there. */
1862 static void
1863 find_reload_regs (chain, dumpfile)
1864 struct insn_chain *chain;
1865 FILE *dumpfile;
1867 int i, class;
1868 short *group_needs = chain->need.groups;
1869 short *simple_needs = chain->need.regs[0];
1870 short *nongroup_needs = chain->need.regs[1];
1872 if (dumpfile)
1873 fprintf (dumpfile, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1875 /* Compute the order of preference for hard registers to spill.
1876 Store them by decreasing preference in potential_reload_regs. */
1878 order_regs_for_reload (chain);
1880 /* So far, no hard regs have been spilled. */
1881 n_spills = 0;
1882 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1883 spill_reg_order[i] = -1;
1885 CLEAR_HARD_REG_SET (chain->used_spill_regs);
1886 CLEAR_HARD_REG_SET (chain->counted_for_groups);
1887 CLEAR_HARD_REG_SET (chain->counted_for_nongroups);
1889 for (class = 0; class < N_REG_CLASSES; class++)
1891 /* First get the groups of registers.
1892 If we got single registers first, we might fragment
1893 possible groups. */
1894 while (group_needs[class] > 0)
1896 /* If any single spilled regs happen to form groups,
1897 count them now. Maybe we don't really need
1898 to spill another group. */
1899 count_possible_groups (chain, class);
1901 if (group_needs[class] <= 0)
1902 break;
1904 /* Groups of size 2, the only groups used on most machines,
1905 are treated specially. */
1906 if (chain->group_size[class] == 2)
1907 find_tworeg_group (chain, class, dumpfile);
1908 else
1909 find_group (chain, class, dumpfile);
1910 if (failure)
1911 return;
1914 /* Now similarly satisfy all need for single registers. */
1916 while (simple_needs[class] > 0 || nongroup_needs[class] > 0)
1918 /* If we spilled enough regs, but they weren't counted
1919 against the non-group need, see if we can count them now.
1920 If so, we can avoid some actual spilling. */
1921 if (simple_needs[class] <= 0 && nongroup_needs[class] > 0)
1922 for (i = 0; i < n_spills; i++)
1924 int regno = spill_regs[i];
1925 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1926 && !TEST_HARD_REG_BIT (chain->counted_for_groups, regno)
1927 && !TEST_HARD_REG_BIT (chain->counted_for_nongroups, regno)
1928 && nongroup_needs[class] > 0)
1930 register enum reg_class *p;
1932 SET_HARD_REG_BIT (chain->counted_for_nongroups, regno);
1933 nongroup_needs[class]--;
1934 p = reg_class_superclasses[class];
1935 while (*p != LIM_REG_CLASSES)
1936 nongroup_needs[(int) *p++]--;
1940 if (simple_needs[class] <= 0 && nongroup_needs[class] <= 0)
1941 break;
1943 /* Consider the potential reload regs that aren't
1944 yet in use as reload regs, in order of preference.
1945 Find the most preferred one that's in this class. */
1947 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1949 int regno = potential_reload_regs[i];
1950 if (regno >= 0
1951 && TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1952 /* If this reg will not be available for groups,
1953 pick one that does not foreclose possible groups.
1954 This is a kludge, and not very general,
1955 but it should be sufficient to make the 386 work,
1956 and the problem should not occur on machines with
1957 more registers. */
1958 && (nongroup_needs[class] == 0
1959 || possible_group_p (chain, regno)))
1960 break;
1963 /* If we couldn't get a register, try to get one even if we
1964 might foreclose possible groups. This may cause problems
1965 later, but that's better than aborting now, since it is
1966 possible that we will, in fact, be able to form the needed
1967 group even with this allocation. */
1969 if (i >= FIRST_PSEUDO_REGISTER
1970 && asm_noperands (chain->insn) < 0)
1971 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1972 if (potential_reload_regs[i] >= 0
1973 && TEST_HARD_REG_BIT (reg_class_contents[class],
1974 potential_reload_regs[i]))
1975 break;
1977 /* I should be the index in potential_reload_regs
1978 of the new reload reg we have found. */
1980 new_spill_reg (chain, i, class, 1, dumpfile);
1981 if (failure)
1982 return;
1986 /* We know which hard regs to use, now mark the pseudos that live in them
1987 as needing to be kicked out. */
1988 EXECUTE_IF_SET_IN_REG_SET
1989 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
1991 maybe_mark_pseudo_spilled (i);
1993 EXECUTE_IF_SET_IN_REG_SET
1994 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
1996 maybe_mark_pseudo_spilled (i);
1999 IOR_HARD_REG_SET (used_spill_regs, chain->used_spill_regs);
2002 void
2003 dump_needs (chain, dumpfile)
2004 struct insn_chain *chain;
2005 FILE *dumpfile;
2007 static const char * const reg_class_names[] = REG_CLASS_NAMES;
2008 int i;
2009 struct needs *n = &chain->need;
2011 for (i = 0; i < N_REG_CLASSES; i++)
2013 if (n->regs[i][0] > 0)
2014 fprintf (dumpfile,
2015 ";; Need %d reg%s of class %s.\n",
2016 n->regs[i][0], n->regs[i][0] == 1 ? "" : "s",
2017 reg_class_names[i]);
2018 if (n->regs[i][1] > 0)
2019 fprintf (dumpfile,
2020 ";; Need %d nongroup reg%s of class %s.\n",
2021 n->regs[i][1], n->regs[i][1] == 1 ? "" : "s",
2022 reg_class_names[i]);
2023 if (n->groups[i] > 0)
2024 fprintf (dumpfile,
2025 ";; Need %d group%s (%smode) of class %s.\n",
2026 n->groups[i], n->groups[i] == 1 ? "" : "s",
2027 GET_MODE_NAME(chain->group_mode[i]),
2028 reg_class_names[i]);
2032 /* Delete all insns that were inserted by emit_caller_save_insns during
2033 this iteration. */
2034 static void
2035 delete_caller_save_insns ()
2037 struct insn_chain *c = reload_insn_chain;
2039 while (c != 0)
2041 while (c != 0 && c->is_caller_save_insn)
2043 struct insn_chain *next = c->next;
2044 rtx insn = c->insn;
2046 if (insn == BLOCK_HEAD (c->block))
2047 BLOCK_HEAD (c->block) = NEXT_INSN (insn);
2048 if (insn == BLOCK_END (c->block))
2049 BLOCK_END (c->block) = PREV_INSN (insn);
2050 if (c == reload_insn_chain)
2051 reload_insn_chain = next;
2053 if (NEXT_INSN (insn) != 0)
2054 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2055 if (PREV_INSN (insn) != 0)
2056 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2058 if (next)
2059 next->prev = c->prev;
2060 if (c->prev)
2061 c->prev->next = next;
2062 c->next = unused_insn_chains;
2063 unused_insn_chains = c;
2064 c = next;
2066 if (c != 0)
2067 c = c->next;
2071 /* Nonzero if, after spilling reg REGNO for non-groups,
2072 it will still be possible to find a group if we still need one. */
2074 static int
2075 possible_group_p (chain, regno)
2076 struct insn_chain *chain;
2077 int regno;
2079 int i;
2080 int class = (int) NO_REGS;
2082 for (i = 0; i < (int) N_REG_CLASSES; i++)
2083 if (chain->need.groups[i] > 0)
2085 class = i;
2086 break;
2089 if (class == (int) NO_REGS)
2090 return 1;
2092 /* Consider each pair of consecutive registers. */
2093 for (i = 0; i < FIRST_PSEUDO_REGISTER - 1; i++)
2095 /* Ignore pairs that include reg REGNO. */
2096 if (i == regno || i + 1 == regno)
2097 continue;
2099 /* Ignore pairs that are outside the class that needs the group.
2100 ??? Here we fail to handle the case where two different classes
2101 independently need groups. But this never happens with our
2102 current machine descriptions. */
2103 if (! (TEST_HARD_REG_BIT (reg_class_contents[class], i)
2104 && TEST_HARD_REG_BIT (reg_class_contents[class], i + 1)))
2105 continue;
2107 /* A pair of consecutive regs we can still spill does the trick. */
2108 if (spill_reg_order[i] < 0 && spill_reg_order[i + 1] < 0
2109 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2110 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1))
2111 return 1;
2113 /* A pair of one already spilled and one we can spill does it
2114 provided the one already spilled is not otherwise reserved. */
2115 if (spill_reg_order[i] < 0
2116 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2117 && spill_reg_order[i + 1] >= 0
2118 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, i + 1)
2119 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, i + 1))
2120 return 1;
2121 if (spill_reg_order[i + 1] < 0
2122 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1)
2123 && spill_reg_order[i] >= 0
2124 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, i)
2125 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, i))
2126 return 1;
2129 return 0;
2132 /* Count any groups of CLASS that can be formed from the registers recently
2133 spilled. */
2135 static void
2136 count_possible_groups (chain, class)
2137 struct insn_chain *chain;
2138 int class;
2140 HARD_REG_SET new;
2141 int i, j;
2143 /* Now find all consecutive groups of spilled registers
2144 and mark each group off against the need for such groups.
2145 But don't count them against ordinary need, yet. */
2147 if (chain->group_size[class] == 0)
2148 return;
2150 CLEAR_HARD_REG_SET (new);
2152 /* Make a mask of all the regs that are spill regs in class I. */
2153 for (i = 0; i < n_spills; i++)
2155 int regno = spill_regs[i];
2157 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
2158 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, regno)
2159 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, regno))
2160 SET_HARD_REG_BIT (new, regno);
2163 /* Find each consecutive group of them. */
2164 for (i = 0; i < FIRST_PSEUDO_REGISTER && chain->need.groups[class] > 0; i++)
2165 if (TEST_HARD_REG_BIT (new, i)
2166 && i + chain->group_size[class] <= FIRST_PSEUDO_REGISTER
2167 && HARD_REGNO_MODE_OK (i, chain->group_mode[class]))
2169 for (j = 1; j < chain->group_size[class]; j++)
2170 if (! TEST_HARD_REG_BIT (new, i + j))
2171 break;
2173 if (j == chain->group_size[class])
2175 /* We found a group. Mark it off against this class's need for
2176 groups, and against each superclass too. */
2177 register enum reg_class *p;
2179 chain->need.groups[class]--;
2180 p = reg_class_superclasses[class];
2181 while (*p != LIM_REG_CLASSES)
2183 if (chain->group_size [(int) *p] <= chain->group_size [class])
2184 chain->need.groups[(int) *p]--;
2185 p++;
2188 /* Don't count these registers again. */
2189 for (j = 0; j < chain->group_size[class]; j++)
2190 SET_HARD_REG_BIT (chain->counted_for_groups, i + j);
2193 /* Skip to the last reg in this group. When i is incremented above,
2194 it will then point to the first reg of the next possible group. */
2195 i += j - 1;
2199 /* ALLOCATE_MODE is a register mode that needs to be reloaded. OTHER_MODE is
2200 another mode that needs to be reloaded for the same register class CLASS.
2201 If any reg in CLASS allows ALLOCATE_MODE but not OTHER_MODE, fail.
2202 ALLOCATE_MODE will never be smaller than OTHER_MODE.
2204 This code used to also fail if any reg in CLASS allows OTHER_MODE but not
2205 ALLOCATE_MODE. This test is unnecessary, because we will never try to put
2206 something of mode ALLOCATE_MODE into an OTHER_MODE register. Testing this
2207 causes unnecessary failures on machines requiring alignment of register
2208 groups when the two modes are different sizes, because the larger mode has
2209 more strict alignment rules than the smaller mode. */
2211 static int
2212 modes_equiv_for_class_p (allocate_mode, other_mode, class)
2213 enum machine_mode allocate_mode, other_mode;
2214 enum reg_class class;
2216 register int regno;
2217 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2219 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno)
2220 && HARD_REGNO_MODE_OK (regno, allocate_mode)
2221 && ! HARD_REGNO_MODE_OK (regno, other_mode))
2222 return 0;
2224 return 1;
2227 /* Handle the failure to find a register to spill.
2228 INSN should be one of the insns which needed this particular spill reg. */
2230 static void
2231 spill_failure (insn)
2232 rtx insn;
2234 if (asm_noperands (PATTERN (insn)) >= 0)
2235 error_for_asm (insn, "`asm' needs too many reloads");
2236 else
2237 fatal_insn ("Unable to find a register to spill.", insn);
2240 /* Add a new register to the tables of available spill-registers.
2241 CHAIN is the insn for which the register will be used; we decrease the
2242 needs of that insn.
2243 I is the index of this register in potential_reload_regs.
2244 CLASS is the regclass whose need is being satisfied.
2245 NONGROUP is 0 if this register is part of a group.
2246 DUMPFILE is the same as the one that `reload' got. */
2248 static void
2249 new_spill_reg (chain, i, class, nongroup, dumpfile)
2250 struct insn_chain *chain;
2251 int i;
2252 int class;
2253 int nongroup;
2254 FILE *dumpfile;
2256 register enum reg_class *p;
2257 int regno = potential_reload_regs[i];
2259 if (i >= FIRST_PSEUDO_REGISTER)
2261 spill_failure (chain->insn);
2262 failure = 1;
2263 return;
2266 if (TEST_HARD_REG_BIT (bad_spill_regs, regno))
2268 static const char * const reg_class_names[] = REG_CLASS_NAMES;
2270 if (asm_noperands (PATTERN (chain->insn)) < 0)
2272 /* The error message is still correct - we know only that it wasn't
2273 an asm statement that caused the problem, but one of the global
2274 registers declared by the users might have screwed us. */
2275 error ("fixed or forbidden register %d (%s) was spilled for class %s.",
2276 regno, reg_names[regno], reg_class_names[class]);
2277 error ("This may be due to a compiler bug or to impossible asm");
2278 error ("statements or clauses.");
2279 fatal_insn ("This is the instruction:", chain->insn);
2281 error_for_asm (chain->insn, "Invalid `asm' statement:");
2282 error_for_asm (chain->insn,
2283 "fixed or forbidden register %d (%s) was spilled for class %s.",
2284 regno, reg_names[regno], reg_class_names[class]);
2285 failure = 1;
2286 return;
2289 /* Make reg REGNO an additional reload reg. */
2291 potential_reload_regs[i] = -1;
2292 spill_regs[n_spills] = regno;
2293 spill_reg_order[regno] = n_spills;
2294 if (dumpfile)
2295 fprintf (dumpfile, "Spilling reg %d.\n", regno);
2296 SET_HARD_REG_BIT (chain->used_spill_regs, regno);
2298 /* Clear off the needs we just satisfied. */
2300 chain->need.regs[0][class]--;
2301 p = reg_class_superclasses[class];
2302 while (*p != LIM_REG_CLASSES)
2303 chain->need.regs[0][(int) *p++]--;
2305 if (nongroup && chain->need.regs[1][class] > 0)
2307 SET_HARD_REG_BIT (chain->counted_for_nongroups, regno);
2308 chain->need.regs[1][class]--;
2309 p = reg_class_superclasses[class];
2310 while (*p != LIM_REG_CLASSES)
2311 chain->need.regs[1][(int) *p++]--;
2314 n_spills++;
2317 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2318 data that is dead in INSN. */
2320 static void
2321 delete_dead_insn (insn)
2322 rtx insn;
2324 rtx prev = prev_real_insn (insn);
2325 rtx prev_dest;
2327 /* If the previous insn sets a register that dies in our insn, delete it
2328 too. */
2329 if (prev && GET_CODE (PATTERN (prev)) == SET
2330 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
2331 && reg_mentioned_p (prev_dest, PATTERN (insn))
2332 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
2333 && ! side_effects_p (SET_SRC (PATTERN (prev))))
2334 delete_dead_insn (prev);
2336 PUT_CODE (insn, NOTE);
2337 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2338 NOTE_SOURCE_FILE (insn) = 0;
2341 /* Modify the home of pseudo-reg I.
2342 The new home is present in reg_renumber[I].
2344 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2345 or it may be -1, meaning there is none or it is not relevant.
2346 This is used so that all pseudos spilled from a given hard reg
2347 can share one stack slot. */
2349 static void
2350 alter_reg (i, from_reg)
2351 register int i;
2352 int from_reg;
2354 /* When outputting an inline function, this can happen
2355 for a reg that isn't actually used. */
2356 if (regno_reg_rtx[i] == 0)
2357 return;
2359 /* If the reg got changed to a MEM at rtl-generation time,
2360 ignore it. */
2361 if (GET_CODE (regno_reg_rtx[i]) != REG)
2362 return;
2364 /* Modify the reg-rtx to contain the new hard reg
2365 number or else to contain its pseudo reg number. */
2366 REGNO (regno_reg_rtx[i])
2367 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
2369 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2370 allocate a stack slot for it. */
2372 if (reg_renumber[i] < 0
2373 && REG_N_REFS (i) > 0
2374 && reg_equiv_constant[i] == 0
2375 && reg_equiv_memory_loc[i] == 0)
2377 register rtx x;
2378 int inherent_size = PSEUDO_REGNO_BYTES (i);
2379 int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2380 int adjust = 0;
2382 /* Each pseudo reg has an inherent size which comes from its own mode,
2383 and a total size which provides room for paradoxical subregs
2384 which refer to the pseudo reg in wider modes.
2386 We can use a slot already allocated if it provides both
2387 enough inherent space and enough total space.
2388 Otherwise, we allocate a new slot, making sure that it has no less
2389 inherent space, and no less total space, then the previous slot. */
2390 if (from_reg == -1)
2392 /* No known place to spill from => no slot to reuse. */
2393 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
2394 inherent_size == total_size ? 0 : -1);
2395 if (BYTES_BIG_ENDIAN)
2396 /* Cancel the big-endian correction done in assign_stack_local.
2397 Get the address of the beginning of the slot.
2398 This is so we can do a big-endian correction unconditionally
2399 below. */
2400 adjust = inherent_size - total_size;
2402 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2404 /* Reuse a stack slot if possible. */
2405 else if (spill_stack_slot[from_reg] != 0
2406 && spill_stack_slot_width[from_reg] >= total_size
2407 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2408 >= inherent_size))
2409 x = spill_stack_slot[from_reg];
2410 /* Allocate a bigger slot. */
2411 else
2413 /* Compute maximum size needed, both for inherent size
2414 and for total size. */
2415 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2416 rtx stack_slot;
2417 if (spill_stack_slot[from_reg])
2419 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2420 > inherent_size)
2421 mode = GET_MODE (spill_stack_slot[from_reg]);
2422 if (spill_stack_slot_width[from_reg] > total_size)
2423 total_size = spill_stack_slot_width[from_reg];
2425 /* Make a slot with that size. */
2426 x = assign_stack_local (mode, total_size,
2427 inherent_size == total_size ? 0 : -1);
2428 stack_slot = x;
2429 if (BYTES_BIG_ENDIAN)
2431 /* Cancel the big-endian correction done in assign_stack_local.
2432 Get the address of the beginning of the slot.
2433 This is so we can do a big-endian correction unconditionally
2434 below. */
2435 adjust = GET_MODE_SIZE (mode) - total_size;
2436 if (adjust)
2437 stack_slot = gen_rtx_MEM (mode_for_size (total_size
2438 * BITS_PER_UNIT,
2439 MODE_INT, 1),
2440 plus_constant (XEXP (x, 0), adjust));
2442 spill_stack_slot[from_reg] = stack_slot;
2443 spill_stack_slot_width[from_reg] = total_size;
2446 /* On a big endian machine, the "address" of the slot
2447 is the address of the low part that fits its inherent mode. */
2448 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2449 adjust += (total_size - inherent_size);
2451 /* If we have any adjustment to make, or if the stack slot is the
2452 wrong mode, make a new stack slot. */
2453 if (adjust != 0 || GET_MODE (x) != GET_MODE (regno_reg_rtx[i]))
2455 x = gen_rtx_MEM (GET_MODE (regno_reg_rtx[i]),
2456 plus_constant (XEXP (x, 0), adjust));
2458 /* If this was shared among registers, must ensure we never
2459 set it readonly since that can cause scheduling
2460 problems. Note we would only have in this adjustment
2461 case in any event, since the code above doesn't set it. */
2463 if (from_reg == -1)
2464 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2467 /* Save the stack slot for later. */
2468 reg_equiv_memory_loc[i] = x;
2472 /* Mark the slots in regs_ever_live for the hard regs
2473 used by pseudo-reg number REGNO. */
2475 void
2476 mark_home_live (regno)
2477 int regno;
2479 register int i, lim;
2480 i = reg_renumber[regno];
2481 if (i < 0)
2482 return;
2483 lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2484 while (i < lim)
2485 regs_ever_live[i++] = 1;
2488 /* This function handles the tracking of elimination offsets around branches.
2490 X is a piece of RTL being scanned.
2492 INSN is the insn that it came from, if any.
2494 INITIAL_P is non-zero if we are to set the offset to be the initial
2495 offset and zero if we are setting the offset of the label to be the
2496 current offset. */
2498 static void
2499 set_label_offsets (x, insn, initial_p)
2500 rtx x;
2501 rtx insn;
2502 int initial_p;
2504 enum rtx_code code = GET_CODE (x);
2505 rtx tem;
2506 unsigned int i;
2507 struct elim_table *p;
2509 switch (code)
2511 case LABEL_REF:
2512 if (LABEL_REF_NONLOCAL_P (x))
2513 return;
2515 x = XEXP (x, 0);
2517 /* ... fall through ... */
2519 case CODE_LABEL:
2520 /* If we know nothing about this label, set the desired offsets. Note
2521 that this sets the offset at a label to be the offset before a label
2522 if we don't know anything about the label. This is not correct for
2523 the label after a BARRIER, but is the best guess we can make. If
2524 we guessed wrong, we will suppress an elimination that might have
2525 been possible had we been able to guess correctly. */
2527 if (! offsets_known_at[CODE_LABEL_NUMBER (x)])
2529 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2530 offsets_at[CODE_LABEL_NUMBER (x)][i]
2531 = (initial_p ? reg_eliminate[i].initial_offset
2532 : reg_eliminate[i].offset);
2533 offsets_known_at[CODE_LABEL_NUMBER (x)] = 1;
2536 /* Otherwise, if this is the definition of a label and it is
2537 preceded by a BARRIER, set our offsets to the known offset of
2538 that label. */
2540 else if (x == insn
2541 && (tem = prev_nonnote_insn (insn)) != 0
2542 && GET_CODE (tem) == BARRIER)
2543 set_offsets_for_label (insn);
2544 else
2545 /* If neither of the above cases is true, compare each offset
2546 with those previously recorded and suppress any eliminations
2547 where the offsets disagree. */
2549 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2550 if (offsets_at[CODE_LABEL_NUMBER (x)][i]
2551 != (initial_p ? reg_eliminate[i].initial_offset
2552 : reg_eliminate[i].offset))
2553 reg_eliminate[i].can_eliminate = 0;
2555 return;
2557 case JUMP_INSN:
2558 set_label_offsets (PATTERN (insn), insn, initial_p);
2560 /* ... fall through ... */
2562 case INSN:
2563 case CALL_INSN:
2564 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2565 and hence must have all eliminations at their initial offsets. */
2566 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2567 if (REG_NOTE_KIND (tem) == REG_LABEL)
2568 set_label_offsets (XEXP (tem, 0), insn, 1);
2569 return;
2571 case ADDR_VEC:
2572 case ADDR_DIFF_VEC:
2573 /* Each of the labels in the address vector must be at their initial
2574 offsets. We want the first field for ADDR_VEC and the second
2575 field for ADDR_DIFF_VEC. */
2577 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2578 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2579 insn, initial_p);
2580 return;
2582 case SET:
2583 /* We only care about setting PC. If the source is not RETURN,
2584 IF_THEN_ELSE, or a label, disable any eliminations not at
2585 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2586 isn't one of those possibilities. For branches to a label,
2587 call ourselves recursively.
2589 Note that this can disable elimination unnecessarily when we have
2590 a non-local goto since it will look like a non-constant jump to
2591 someplace in the current function. This isn't a significant
2592 problem since such jumps will normally be when all elimination
2593 pairs are back to their initial offsets. */
2595 if (SET_DEST (x) != pc_rtx)
2596 return;
2598 switch (GET_CODE (SET_SRC (x)))
2600 case PC:
2601 case RETURN:
2602 return;
2604 case LABEL_REF:
2605 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2606 return;
2608 case IF_THEN_ELSE:
2609 tem = XEXP (SET_SRC (x), 1);
2610 if (GET_CODE (tem) == LABEL_REF)
2611 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2612 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2613 break;
2615 tem = XEXP (SET_SRC (x), 2);
2616 if (GET_CODE (tem) == LABEL_REF)
2617 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2618 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2619 break;
2620 return;
2622 default:
2623 break;
2626 /* If we reach here, all eliminations must be at their initial
2627 offset because we are doing a jump to a variable address. */
2628 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2629 if (p->offset != p->initial_offset)
2630 p->can_eliminate = 0;
2631 break;
2633 default:
2634 break;
2638 /* Scan X and replace any eliminable registers (such as fp) with a
2639 replacement (such as sp), plus an offset.
2641 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2642 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2643 MEM, we are allowed to replace a sum of a register and the constant zero
2644 with the register, which we cannot do outside a MEM. In addition, we need
2645 to record the fact that a register is referenced outside a MEM.
2647 If INSN is an insn, it is the insn containing X. If we replace a REG
2648 in a SET_DEST with an equivalent MEM and INSN is non-zero, write a
2649 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2650 the REG is being modified.
2652 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2653 That's used when we eliminate in expressions stored in notes.
2654 This means, do not set ref_outside_mem even if the reference
2655 is outside of MEMs.
2657 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2658 replacements done assuming all offsets are at their initial values. If
2659 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2660 encounter, return the actual location so that find_reloads will do
2661 the proper thing. */
2664 eliminate_regs (x, mem_mode, insn)
2665 rtx x;
2666 enum machine_mode mem_mode;
2667 rtx insn;
2669 enum rtx_code code = GET_CODE (x);
2670 struct elim_table *ep;
2671 int regno;
2672 rtx new;
2673 int i, j;
2674 const char *fmt;
2675 int copied = 0;
2677 if (! current_function_decl)
2678 return x;
2680 switch (code)
2682 case CONST_INT:
2683 case CONST_DOUBLE:
2684 case CONST:
2685 case SYMBOL_REF:
2686 case CODE_LABEL:
2687 case PC:
2688 case CC0:
2689 case ASM_INPUT:
2690 case ADDR_VEC:
2691 case ADDR_DIFF_VEC:
2692 case RETURN:
2693 return x;
2695 case ADDRESSOF:
2696 /* This is only for the benefit of the debugging backends, which call
2697 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2698 removed after CSE. */
2699 new = eliminate_regs (XEXP (x, 0), 0, insn);
2700 if (GET_CODE (new) == MEM)
2701 return XEXP (new, 0);
2702 return x;
2704 case REG:
2705 regno = REGNO (x);
2707 /* First handle the case where we encounter a bare register that
2708 is eliminable. Replace it with a PLUS. */
2709 if (regno < FIRST_PSEUDO_REGISTER)
2711 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2712 ep++)
2713 if (ep->from_rtx == x && ep->can_eliminate)
2714 return plus_constant (ep->to_rtx, ep->previous_offset);
2717 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2718 && reg_equiv_constant[regno]
2719 && ! CONSTANT_P (reg_equiv_constant[regno]))
2720 return eliminate_regs (copy_rtx (reg_equiv_constant[regno]),
2721 mem_mode, insn);
2722 return x;
2724 /* You might think handling MINUS in a manner similar to PLUS is a
2725 good idea. It is not. It has been tried multiple times and every
2726 time the change has had to have been reverted.
2728 Other parts of reload know a PLUS is special (gen_reload for example)
2729 and require special code to handle code a reloaded PLUS operand.
2731 Also consider backends where the flags register is clobbered by a
2732 MINUS, but we can emit a PLUS that does not clobber flags (ia32,
2733 lea instruction comes to mind). If we try to reload a MINUS, we
2734 may kill the flags register that was holding a useful value.
2736 So, please before trying to handle MINUS, consider reload as a
2737 whole instead of this little section as well as the backend issues. */
2738 case PLUS:
2739 /* If this is the sum of an eliminable register and a constant, rework
2740 the sum. */
2741 if (GET_CODE (XEXP (x, 0)) == REG
2742 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2743 && CONSTANT_P (XEXP (x, 1)))
2745 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2746 ep++)
2747 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2749 /* The only time we want to replace a PLUS with a REG (this
2750 occurs when the constant operand of the PLUS is the negative
2751 of the offset) is when we are inside a MEM. We won't want
2752 to do so at other times because that would change the
2753 structure of the insn in a way that reload can't handle.
2754 We special-case the commonest situation in
2755 eliminate_regs_in_insn, so just replace a PLUS with a
2756 PLUS here, unless inside a MEM. */
2757 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2758 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2759 return ep->to_rtx;
2760 else
2761 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2762 plus_constant (XEXP (x, 1),
2763 ep->previous_offset));
2766 /* If the register is not eliminable, we are done since the other
2767 operand is a constant. */
2768 return x;
2771 /* If this is part of an address, we want to bring any constant to the
2772 outermost PLUS. We will do this by doing register replacement in
2773 our operands and seeing if a constant shows up in one of them.
2775 Note that there is no risk of modifying the structure of the insn,
2776 since we only get called for its operands, thus we are either
2777 modifying the address inside a MEM, or something like an address
2778 operand of a load-address insn. */
2781 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2782 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2784 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2786 /* If one side is a PLUS and the other side is a pseudo that
2787 didn't get a hard register but has a reg_equiv_constant,
2788 we must replace the constant here since it may no longer
2789 be in the position of any operand. */
2790 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2791 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2792 && reg_renumber[REGNO (new1)] < 0
2793 && reg_equiv_constant != 0
2794 && reg_equiv_constant[REGNO (new1)] != 0)
2795 new1 = reg_equiv_constant[REGNO (new1)];
2796 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2797 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2798 && reg_renumber[REGNO (new0)] < 0
2799 && reg_equiv_constant[REGNO (new0)] != 0)
2800 new0 = reg_equiv_constant[REGNO (new0)];
2802 new = form_sum (new0, new1);
2804 /* As above, if we are not inside a MEM we do not want to
2805 turn a PLUS into something else. We might try to do so here
2806 for an addition of 0 if we aren't optimizing. */
2807 if (! mem_mode && GET_CODE (new) != PLUS)
2808 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2809 else
2810 return new;
2813 return x;
2815 case MULT:
2816 /* If this is the product of an eliminable register and a
2817 constant, apply the distribute law and move the constant out
2818 so that we have (plus (mult ..) ..). This is needed in order
2819 to keep load-address insns valid. This case is pathological.
2820 We ignore the possibility of overflow here. */
2821 if (GET_CODE (XEXP (x, 0)) == REG
2822 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2823 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2824 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2825 ep++)
2826 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2828 if (! mem_mode
2829 /* Refs inside notes don't count for this purpose. */
2830 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2831 || GET_CODE (insn) == INSN_LIST)))
2832 ep->ref_outside_mem = 1;
2834 return
2835 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2836 ep->previous_offset * INTVAL (XEXP (x, 1)));
2839 /* ... fall through ... */
2841 case CALL:
2842 case COMPARE:
2843 /* See comments before PLUS about handling MINUS. */
2844 case MINUS:
2845 case DIV: case UDIV:
2846 case MOD: case UMOD:
2847 case AND: case IOR: case XOR:
2848 case ROTATERT: case ROTATE:
2849 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2850 case NE: case EQ:
2851 case GE: case GT: case GEU: case GTU:
2852 case LE: case LT: case LEU: case LTU:
2854 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2855 rtx new1
2856 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
2858 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2859 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2861 return x;
2863 case EXPR_LIST:
2864 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2865 if (XEXP (x, 0))
2867 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2868 if (new != XEXP (x, 0))
2870 /* If this is a REG_DEAD note, it is not valid anymore.
2871 Using the eliminated version could result in creating a
2872 REG_DEAD note for the stack or frame pointer. */
2873 if (GET_MODE (x) == REG_DEAD)
2874 return (XEXP (x, 1)
2875 ? eliminate_regs (XEXP (x, 1), mem_mode, insn)
2876 : NULL_RTX);
2878 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2882 /* ... fall through ... */
2884 case INSN_LIST:
2885 /* Now do eliminations in the rest of the chain. If this was
2886 an EXPR_LIST, this might result in allocating more memory than is
2887 strictly needed, but it simplifies the code. */
2888 if (XEXP (x, 1))
2890 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2891 if (new != XEXP (x, 1))
2892 return gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2894 return x;
2896 case PRE_INC:
2897 case POST_INC:
2898 case PRE_DEC:
2899 case POST_DEC:
2900 case STRICT_LOW_PART:
2901 case NEG: case NOT:
2902 case SIGN_EXTEND: case ZERO_EXTEND:
2903 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2904 case FLOAT: case FIX:
2905 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2906 case ABS:
2907 case SQRT:
2908 case FFS:
2909 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2910 if (new != XEXP (x, 0))
2911 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2912 return x;
2914 case SUBREG:
2915 /* Similar to above processing, but preserve SUBREG_WORD.
2916 Convert (subreg (mem)) to (mem) if not paradoxical.
2917 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2918 pseudo didn't get a hard reg, we must replace this with the
2919 eliminated version of the memory location because push_reloads
2920 may do the replacement in certain circumstances. */
2921 if (GET_CODE (SUBREG_REG (x)) == REG
2922 && (GET_MODE_SIZE (GET_MODE (x))
2923 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2924 && reg_equiv_memory_loc != 0
2925 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2927 new = SUBREG_REG (x);
2929 else
2930 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
2932 if (new != XEXP (x, 0))
2934 int x_size = GET_MODE_SIZE (GET_MODE (x));
2935 int new_size = GET_MODE_SIZE (GET_MODE (new));
2937 if (GET_CODE (new) == MEM
2938 && ((x_size < new_size
2939 #ifdef WORD_REGISTER_OPERATIONS
2940 /* On these machines, combine can create rtl of the form
2941 (set (subreg:m1 (reg:m2 R) 0) ...)
2942 where m1 < m2, and expects something interesting to
2943 happen to the entire word. Moreover, it will use the
2944 (reg:m2 R) later, expecting all bits to be preserved.
2945 So if the number of words is the same, preserve the
2946 subreg so that push_reloads can see it. */
2947 && ! ((x_size-1)/UNITS_PER_WORD == (new_size-1)/UNITS_PER_WORD)
2948 #endif
2950 || (x_size == new_size))
2953 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
2954 enum machine_mode mode = GET_MODE (x);
2956 if (BYTES_BIG_ENDIAN)
2957 offset += (MIN (UNITS_PER_WORD,
2958 GET_MODE_SIZE (GET_MODE (new)))
2959 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
2961 PUT_MODE (new, mode);
2962 XEXP (new, 0) = plus_constant (XEXP (new, 0), offset);
2963 return new;
2965 else
2966 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_WORD (x));
2969 return x;
2971 case MEM:
2972 /* This is only for the benefit of the debugging backends, which call
2973 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2974 removed after CSE. */
2975 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
2976 return eliminate_regs (XEXP (XEXP (x, 0), 0), 0, insn);
2978 /* Our only special processing is to pass the mode of the MEM to our
2979 recursive call and copy the flags. While we are here, handle this
2980 case more efficiently. */
2981 new = eliminate_regs (XEXP (x, 0), GET_MODE (x), insn);
2982 if (new != XEXP (x, 0))
2984 new = gen_rtx_MEM (GET_MODE (x), new);
2985 new->volatil = x->volatil;
2986 new->unchanging = x->unchanging;
2987 new->in_struct = x->in_struct;
2988 return new;
2990 else
2991 return x;
2993 case USE:
2994 case CLOBBER:
2995 case ASM_OPERANDS:
2996 case SET:
2997 abort ();
2999 default:
3000 break;
3003 /* Process each of our operands recursively. If any have changed, make a
3004 copy of the rtx. */
3005 fmt = GET_RTX_FORMAT (code);
3006 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3008 if (*fmt == 'e')
3010 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
3011 if (new != XEXP (x, i) && ! copied)
3013 rtx new_x = rtx_alloc (code);
3014 bcopy ((char *) x, (char *) new_x,
3015 (sizeof (*new_x) - sizeof (new_x->fld)
3016 + sizeof (new_x->fld[0]) * GET_RTX_LENGTH (code)));
3017 x = new_x;
3018 copied = 1;
3020 XEXP (x, i) = new;
3022 else if (*fmt == 'E')
3024 int copied_vec = 0;
3025 for (j = 0; j < XVECLEN (x, i); j++)
3027 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
3028 if (new != XVECEXP (x, i, j) && ! copied_vec)
3030 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
3031 XVEC (x, i)->elem);
3032 if (! copied)
3034 rtx new_x = rtx_alloc (code);
3035 bcopy ((char *) x, (char *) new_x,
3036 (sizeof (*new_x) - sizeof (new_x->fld)
3037 + (sizeof (new_x->fld[0])
3038 * GET_RTX_LENGTH (code))));
3039 x = new_x;
3040 copied = 1;
3042 XVEC (x, i) = new_v;
3043 copied_vec = 1;
3045 XVECEXP (x, i, j) = new;
3050 return x;
3053 /* Scan rtx X for modifications of elimination target registers. Update
3054 the table of eliminables to reflect the changed state. MEM_MODE is
3055 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
3057 static void
3058 elimination_effects (x, mem_mode)
3059 rtx x;
3060 enum machine_mode mem_mode;
3063 enum rtx_code code = GET_CODE (x);
3064 struct elim_table *ep;
3065 int regno;
3066 int i, j;
3067 const char *fmt;
3069 switch (code)
3071 case CONST_INT:
3072 case CONST_DOUBLE:
3073 case CONST:
3074 case SYMBOL_REF:
3075 case CODE_LABEL:
3076 case PC:
3077 case CC0:
3078 case ASM_INPUT:
3079 case ADDR_VEC:
3080 case ADDR_DIFF_VEC:
3081 case RETURN:
3082 return;
3084 case ADDRESSOF:
3085 abort ();
3087 case REG:
3088 regno = REGNO (x);
3090 /* First handle the case where we encounter a bare register that
3091 is eliminable. Replace it with a PLUS. */
3092 if (regno < FIRST_PSEUDO_REGISTER)
3094 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3095 ep++)
3096 if (ep->from_rtx == x && ep->can_eliminate)
3098 if (! mem_mode)
3099 ep->ref_outside_mem = 1;
3100 return;
3104 else if (reg_renumber[regno] < 0 && reg_equiv_constant
3105 && reg_equiv_constant[regno]
3106 && ! CONSTANT_P (reg_equiv_constant[regno]))
3107 elimination_effects (reg_equiv_constant[regno], mem_mode);
3108 return;
3110 case PRE_INC:
3111 case POST_INC:
3112 case PRE_DEC:
3113 case POST_DEC:
3114 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3115 if (ep->to_rtx == XEXP (x, 0))
3117 int size = GET_MODE_SIZE (mem_mode);
3119 /* If more bytes than MEM_MODE are pushed, account for them. */
3120 #ifdef PUSH_ROUNDING
3121 if (ep->to_rtx == stack_pointer_rtx)
3122 size = PUSH_ROUNDING (size);
3123 #endif
3124 if (code == PRE_DEC || code == POST_DEC)
3125 ep->offset += size;
3126 else
3127 ep->offset -= size;
3130 /* Fall through to generic unary operation case. */
3131 case STRICT_LOW_PART:
3132 case NEG: case NOT:
3133 case SIGN_EXTEND: case ZERO_EXTEND:
3134 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3135 case FLOAT: case FIX:
3136 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3137 case ABS:
3138 case SQRT:
3139 case FFS:
3140 elimination_effects (XEXP (x, 0), mem_mode);
3141 return;
3143 case SUBREG:
3144 if (GET_CODE (SUBREG_REG (x)) == REG
3145 && (GET_MODE_SIZE (GET_MODE (x))
3146 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3147 && reg_equiv_memory_loc != 0
3148 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
3149 return;
3151 elimination_effects (SUBREG_REG (x), mem_mode);
3152 return;
3154 case USE:
3155 /* If using a register that is the source of an eliminate we still
3156 think can be performed, note it cannot be performed since we don't
3157 know how this register is used. */
3158 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3159 if (ep->from_rtx == XEXP (x, 0))
3160 ep->can_eliminate = 0;
3162 elimination_effects (XEXP (x, 0), mem_mode);
3163 return;
3165 case CLOBBER:
3166 /* If clobbering a register that is the replacement register for an
3167 elimination we still think can be performed, note that it cannot
3168 be performed. Otherwise, we need not be concerned about it. */
3169 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3170 if (ep->to_rtx == XEXP (x, 0))
3171 ep->can_eliminate = 0;
3173 elimination_effects (XEXP (x, 0), mem_mode);
3174 return;
3176 case SET:
3177 /* Check for setting a register that we know about. */
3178 if (GET_CODE (SET_DEST (x)) == REG)
3180 /* See if this is setting the replacement register for an
3181 elimination.
3183 If DEST is the hard frame pointer, we do nothing because we
3184 assume that all assignments to the frame pointer are for
3185 non-local gotos and are being done at a time when they are valid
3186 and do not disturb anything else. Some machines want to
3187 eliminate a fake argument pointer (or even a fake frame pointer)
3188 with either the real frame or the stack pointer. Assignments to
3189 the hard frame pointer must not prevent this elimination. */
3191 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3192 ep++)
3193 if (ep->to_rtx == SET_DEST (x)
3194 && SET_DEST (x) != hard_frame_pointer_rtx)
3196 /* If it is being incremented, adjust the offset. Otherwise,
3197 this elimination can't be done. */
3198 rtx src = SET_SRC (x);
3200 if (GET_CODE (src) == PLUS
3201 && XEXP (src, 0) == SET_DEST (x)
3202 && GET_CODE (XEXP (src, 1)) == CONST_INT)
3203 ep->offset -= INTVAL (XEXP (src, 1));
3204 else
3205 ep->can_eliminate = 0;
3209 elimination_effects (SET_DEST (x), 0);
3210 elimination_effects (SET_SRC (x), 0);
3211 return;
3213 case MEM:
3214 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
3215 abort ();
3217 /* Our only special processing is to pass the mode of the MEM to our
3218 recursive call. */
3219 elimination_effects (XEXP (x, 0), GET_MODE (x));
3220 return;
3222 default:
3223 break;
3226 fmt = GET_RTX_FORMAT (code);
3227 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3229 if (*fmt == 'e')
3230 elimination_effects (XEXP (x, i), mem_mode);
3231 else if (*fmt == 'E')
3232 for (j = 0; j < XVECLEN (x, i); j++)
3233 elimination_effects (XVECEXP (x, i, j), mem_mode);
3237 /* Descend through rtx X and verify that no references to eliminable registers
3238 remain. If any do remain, mark the involved register as not
3239 eliminable. */
3240 static void
3241 check_eliminable_occurrences (x)
3242 rtx x;
3244 const char *fmt;
3245 int i;
3246 enum rtx_code code;
3248 if (x == 0)
3249 return;
3251 code = GET_CODE (x);
3253 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
3255 struct elim_table *ep;
3257 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3258 if (ep->from_rtx == x && ep->can_eliminate)
3259 ep->can_eliminate = 0;
3260 return;
3263 fmt = GET_RTX_FORMAT (code);
3264 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3266 if (*fmt == 'e')
3267 check_eliminable_occurrences (XEXP (x, i));
3268 else if (*fmt == 'E')
3270 int j;
3271 for (j = 0; j < XVECLEN (x, i); j++)
3272 check_eliminable_occurrences (XVECEXP (x, i, j));
3277 /* Scan INSN and eliminate all eliminable registers in it.
3279 If REPLACE is nonzero, do the replacement destructively. Also
3280 delete the insn as dead it if it is setting an eliminable register.
3282 If REPLACE is zero, do all our allocations in reload_obstack.
3284 If no eliminations were done and this insn doesn't require any elimination
3285 processing (these are not identical conditions: it might be updating sp,
3286 but not referencing fp; this needs to be seen during reload_as_needed so
3287 that the offset between fp and sp can be taken into consideration), zero
3288 is returned. Otherwise, 1 is returned. */
3290 static int
3291 eliminate_regs_in_insn (insn, replace)
3292 rtx insn;
3293 int replace;
3295 int icode = recog_memoized (insn);
3296 rtx old_body = PATTERN (insn);
3297 int insn_is_asm = asm_noperands (old_body) >= 0;
3298 rtx old_set = single_set (insn);
3299 rtx new_body;
3300 int val = 0;
3301 int i, any_changes;
3302 rtx substed_operand[MAX_RECOG_OPERANDS];
3303 rtx orig_operand[MAX_RECOG_OPERANDS];
3304 struct elim_table *ep;
3306 if (! insn_is_asm && icode < 0)
3308 if (GET_CODE (PATTERN (insn)) == USE
3309 || GET_CODE (PATTERN (insn)) == CLOBBER
3310 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3311 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3312 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
3313 return 0;
3314 abort ();
3317 if (! replace)
3318 push_obstacks (&reload_obstack, &reload_obstack);
3320 if (old_set != 0 && GET_CODE (SET_DEST (old_set)) == REG
3321 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3323 /* Check for setting an eliminable register. */
3324 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3325 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3327 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3328 /* If this is setting the frame pointer register to the
3329 hardware frame pointer register and this is an elimination
3330 that will be done (tested above), this insn is really
3331 adjusting the frame pointer downward to compensate for
3332 the adjustment done before a nonlocal goto. */
3333 if (ep->from == FRAME_POINTER_REGNUM
3334 && ep->to == HARD_FRAME_POINTER_REGNUM)
3336 rtx src = SET_SRC (old_set);
3337 int offset = 0, ok = 0;
3338 rtx prev_insn, prev_set;
3340 if (src == ep->to_rtx)
3341 offset = 0, ok = 1;
3342 else if (GET_CODE (src) == PLUS
3343 && GET_CODE (XEXP (src, 0)) == CONST_INT
3344 && XEXP (src, 1) == ep->to_rtx)
3345 offset = INTVAL (XEXP (src, 0)), ok = 1;
3346 else if (GET_CODE (src) == PLUS
3347 && GET_CODE (XEXP (src, 1)) == CONST_INT
3348 && XEXP (src, 0) == ep->to_rtx)
3349 offset = INTVAL (XEXP (src, 1)), ok = 1;
3350 else if ((prev_insn = prev_nonnote_insn (insn)) != 0
3351 && (prev_set = single_set (prev_insn)) != 0
3352 && rtx_equal_p (SET_DEST (prev_set), src))
3354 src = SET_SRC (prev_set);
3355 if (src == ep->to_rtx)
3356 offset = 0, ok = 1;
3357 else if (GET_CODE (src) == PLUS
3358 && GET_CODE (XEXP (src, 0)) == CONST_INT
3359 && XEXP (src, 1) == ep->to_rtx)
3360 offset = INTVAL (XEXP (src, 0)), ok = 1;
3361 else if (GET_CODE (src) == PLUS
3362 && GET_CODE (XEXP (src, 1)) == CONST_INT
3363 && XEXP (src, 0) == ep->to_rtx)
3364 offset = INTVAL (XEXP (src, 1)), ok = 1;
3367 if (ok)
3369 if (replace)
3371 rtx src
3372 = plus_constant (ep->to_rtx, offset - ep->offset);
3374 /* First see if this insn remains valid when we
3375 make the change. If not, keep the INSN_CODE
3376 the same and let reload fit it up. */
3377 validate_change (insn, &SET_SRC (old_set), src, 1);
3378 validate_change (insn, &SET_DEST (old_set),
3379 ep->to_rtx, 1);
3380 if (! apply_change_group ())
3382 SET_SRC (old_set) = src;
3383 SET_DEST (old_set) = ep->to_rtx;
3387 val = 1;
3388 goto done;
3391 #endif
3393 /* In this case this insn isn't serving a useful purpose. We
3394 will delete it in reload_as_needed once we know that this
3395 elimination is, in fact, being done.
3397 If REPLACE isn't set, we can't delete this insn, but needn't
3398 process it since it won't be used unless something changes. */
3399 if (replace)
3401 delete_dead_insn (insn);
3402 return 1;
3404 val = 1;
3405 goto done;
3408 /* Check for (set (reg) (plus (reg from) (offset))) where the offset
3409 in the insn is the negative of the offset in FROM. Substitute
3410 (set (reg) (reg to)) for the insn and change its code.
3412 We have to do this here, rather than in eliminate_regs, so that we can
3413 change the insn code. */
3415 if (GET_CODE (SET_SRC (old_set)) == PLUS
3416 && GET_CODE (XEXP (SET_SRC (old_set), 0)) == REG
3417 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT)
3418 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3419 ep++)
3420 if (ep->from_rtx == XEXP (SET_SRC (old_set), 0)
3421 && ep->can_eliminate)
3423 /* We must stop at the first elimination that will be used.
3424 If this one would replace the PLUS with a REG, do it
3425 now. Otherwise, quit the loop and let eliminate_regs
3426 do its normal replacement. */
3427 if (ep->offset == - INTVAL (XEXP (SET_SRC (old_set), 1)))
3429 /* We assume here that we don't need a PARALLEL of
3430 any CLOBBERs for this assignment. There's not
3431 much we can do if we do need it. */
3432 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3433 SET_DEST (old_set),
3434 ep->to_rtx);
3435 INSN_CODE (insn) = -1;
3436 val = 1;
3437 goto done;
3440 break;
3444 /* Determine the effects of this insn on elimination offsets. */
3445 elimination_effects (old_body, 0);
3447 /* Eliminate all eliminable registers occurring in operands that
3448 can be handled by reload. */
3449 extract_insn (insn);
3450 any_changes = 0;
3451 for (i = 0; i < recog_data.n_operands; i++)
3453 orig_operand[i] = recog_data.operand[i];
3454 substed_operand[i] = recog_data.operand[i];
3456 /* For an asm statement, every operand is eliminable. */
3457 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3459 /* Check for setting a register that we know about. */
3460 if (recog_data.operand_type[i] != OP_IN
3461 && GET_CODE (orig_operand[i]) == REG)
3463 /* If we are assigning to a register that can be eliminated, it
3464 must be as part of a PARALLEL, since the code above handles
3465 single SETs. We must indicate that we can no longer
3466 eliminate this reg. */
3467 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3468 ep++)
3469 if (ep->from_rtx == orig_operand[i] && ep->can_eliminate)
3470 ep->can_eliminate = 0;
3473 substed_operand[i] = eliminate_regs (recog_data.operand[i], 0,
3474 replace ? insn : NULL_RTX);
3475 if (substed_operand[i] != orig_operand[i])
3476 val = any_changes = 1;
3477 /* Terminate the search in check_eliminable_occurrences at
3478 this point. */
3479 *recog_data.operand_loc[i] = 0;
3481 /* If an output operand changed from a REG to a MEM and INSN is an
3482 insn, write a CLOBBER insn. */
3483 if (recog_data.operand_type[i] != OP_IN
3484 && GET_CODE (orig_operand[i]) == REG
3485 && GET_CODE (substed_operand[i]) == MEM
3486 && replace)
3487 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3488 insn);
3492 for (i = 0; i < recog_data.n_dups; i++)
3493 *recog_data.dup_loc[i]
3494 = *recog_data.operand_loc[(int)recog_data.dup_num[i]];
3496 /* If any eliminable remain, they aren't eliminable anymore. */
3497 check_eliminable_occurrences (old_body);
3499 /* Substitute the operands; the new values are in the substed_operand
3500 array. */
3501 for (i = 0; i < recog_data.n_operands; i++)
3502 *recog_data.operand_loc[i] = substed_operand[i];
3503 for (i = 0; i < recog_data.n_dups; i++)
3504 *recog_data.dup_loc[i] = substed_operand[(int)recog_data.dup_num[i]];
3506 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3507 re-recognize the insn. We do this in case we had a simple addition
3508 but now can do this as a load-address. This saves an insn in this
3509 common case.
3510 If re-recognition fails, the old insn code number will still be used,
3511 and some register operands may have changed into PLUS expressions.
3512 These will be handled by find_reloads by loading them into a register
3513 again.*/
3515 if (val)
3517 /* If we aren't replacing things permanently and we changed something,
3518 make another copy to ensure that all the RTL is new. Otherwise
3519 things can go wrong if find_reload swaps commutative operands
3520 and one is inside RTL that has been copied while the other is not. */
3521 new_body = old_body;
3522 if (! replace)
3523 new_body = copy_insn (old_body);
3524 PATTERN (insn) = new_body;
3526 /* If we had a move insn but now we don't, rerecognize it. This will
3527 cause spurious re-recognition if the old move had a PARALLEL since
3528 the new one still will, but we can't call single_set without
3529 having put NEW_BODY into the insn and the re-recognition won't
3530 hurt in this rare case. */
3531 /* ??? Why this huge if statement - why don't we just rerecognize the
3532 thing always? */
3533 if (! insn_is_asm
3534 && old_set != 0
3535 && ((GET_CODE (SET_SRC (old_set)) == REG
3536 && (GET_CODE (new_body) != SET
3537 || GET_CODE (SET_SRC (new_body)) != REG))
3538 /* If this was a load from or store to memory, compare
3539 the MEM in recog_data.operand to the one in the insn.
3540 If they are not equal, then rerecognize the insn. */
3541 || (old_set != 0
3542 && ((GET_CODE (SET_SRC (old_set)) == MEM
3543 && SET_SRC (old_set) != recog_data.operand[1])
3544 || (GET_CODE (SET_DEST (old_set)) == MEM
3545 && SET_DEST (old_set) != recog_data.operand[0])))
3546 /* If this was an add insn before, rerecognize. */
3547 || GET_CODE (SET_SRC (old_set)) == PLUS))
3549 int new_icode = recog (PATTERN (insn), insn, 0);
3550 if (new_icode < 0)
3551 INSN_CODE (insn) = icode;
3555 /* Restore the old body. If there were any changes to it, we made a copy
3556 of it while the changes were still in place, so we'll correctly return
3557 a modified insn below. */
3558 if (! replace)
3560 /* Restore the old body. */
3561 for (i = 0; i < recog_data.n_operands; i++)
3562 *recog_data.operand_loc[i] = orig_operand[i];
3563 for (i = 0; i < recog_data.n_dups; i++)
3564 *recog_data.dup_loc[i] = orig_operand[(int)recog_data.dup_num[i]];
3567 /* Update all elimination pairs to reflect the status after the current
3568 insn. The changes we make were determined by the earlier call to
3569 elimination_effects.
3571 We also detect a cases where register elimination cannot be done,
3572 namely, if a register would be both changed and referenced outside a MEM
3573 in the resulting insn since such an insn is often undefined and, even if
3574 not, we cannot know what meaning will be given to it. Note that it is
3575 valid to have a register used in an address in an insn that changes it
3576 (presumably with a pre- or post-increment or decrement).
3578 If anything changes, return nonzero. */
3580 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3582 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3583 ep->can_eliminate = 0;
3585 ep->ref_outside_mem = 0;
3587 if (ep->previous_offset != ep->offset)
3588 val = 1;
3591 done:
3592 /* If we changed something, perform elimination in REG_NOTES. This is
3593 needed even when REPLACE is zero because a REG_DEAD note might refer
3594 to a register that we eliminate and could cause a different number
3595 of spill registers to be needed in the final reload pass than in
3596 the pre-passes. */
3597 if (val && REG_NOTES (insn) != 0)
3598 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
3600 if (! replace)
3601 pop_obstacks ();
3603 return val;
3606 /* Loop through all elimination pairs.
3607 Recalculate the number not at initial offset.
3609 Compute the maximum offset (minimum offset if the stack does not
3610 grow downward) for each elimination pair. */
3612 static void
3613 update_eliminable_offsets ()
3615 struct elim_table *ep;
3617 num_not_at_initial_offset = 0;
3618 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3620 ep->previous_offset = ep->offset;
3621 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3622 num_not_at_initial_offset++;
3626 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3627 replacement we currently believe is valid, mark it as not eliminable if X
3628 modifies DEST in any way other than by adding a constant integer to it.
3630 If DEST is the frame pointer, we do nothing because we assume that
3631 all assignments to the hard frame pointer are nonlocal gotos and are being
3632 done at a time when they are valid and do not disturb anything else.
3633 Some machines want to eliminate a fake argument pointer with either the
3634 frame or stack pointer. Assignments to the hard frame pointer must not
3635 prevent this elimination.
3637 Called via note_stores from reload before starting its passes to scan
3638 the insns of the function. */
3640 static void
3641 mark_not_eliminable (dest, x)
3642 rtx dest;
3643 rtx x;
3645 register unsigned int i;
3647 /* A SUBREG of a hard register here is just changing its mode. We should
3648 not see a SUBREG of an eliminable hard register, but check just in
3649 case. */
3650 if (GET_CODE (dest) == SUBREG)
3651 dest = SUBREG_REG (dest);
3653 if (dest == hard_frame_pointer_rtx)
3654 return;
3656 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3657 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3658 && (GET_CODE (x) != SET
3659 || GET_CODE (SET_SRC (x)) != PLUS
3660 || XEXP (SET_SRC (x), 0) != dest
3661 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3663 reg_eliminate[i].can_eliminate_previous
3664 = reg_eliminate[i].can_eliminate = 0;
3665 num_eliminable--;
3669 /* Verify that the initial elimination offsets did not change since the
3670 last call to set_initial_elim_offsets. This is used to catch cases
3671 where something illegal happened during reload_as_needed that could
3672 cause incorrect code to be generated if we did not check for it. */
3673 static void
3674 verify_initial_elim_offsets ()
3676 int t;
3678 #ifdef ELIMINABLE_REGS
3679 struct elim_table *ep;
3681 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3683 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3684 if (t != ep->initial_offset)
3685 abort ();
3687 #else
3688 INITIAL_FRAME_POINTER_OFFSET (t);
3689 if (t != reg_eliminate[0].initial_offset)
3690 abort ();
3691 #endif
3694 /* Reset all offsets on eliminable registers to their initial values. */
3695 static void
3696 set_initial_elim_offsets ()
3698 struct elim_table *ep = reg_eliminate;
3700 #ifdef ELIMINABLE_REGS
3701 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3703 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3704 ep->previous_offset = ep->offset = ep->initial_offset;
3706 #else
3707 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3708 ep->previous_offset = ep->offset = ep->initial_offset;
3709 #endif
3711 num_not_at_initial_offset = 0;
3714 /* Initialize the known label offsets.
3715 Set a known offset for each forced label to be at the initial offset
3716 of each elimination. We do this because we assume that all
3717 computed jumps occur from a location where each elimination is
3718 at its initial offset.
3719 For all other labels, show that we don't know the offsets. */
3721 static void
3722 set_initial_label_offsets ()
3724 rtx x;
3725 bzero ((char *) &offsets_known_at[get_first_label_num ()], num_labels);
3727 for (x = forced_labels; x; x = XEXP (x, 1))
3728 if (XEXP (x, 0))
3729 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3732 /* Set all elimination offsets to the known values for the code label given
3733 by INSN. */
3734 static void
3735 set_offsets_for_label (insn)
3736 rtx insn;
3738 unsigned int i;
3739 int label_nr = CODE_LABEL_NUMBER (insn);
3740 struct elim_table *ep;
3742 num_not_at_initial_offset = 0;
3743 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3745 ep->offset = ep->previous_offset = offsets_at[label_nr][i];
3746 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3747 num_not_at_initial_offset++;
3751 /* See if anything that happened changes which eliminations are valid.
3752 For example, on the Sparc, whether or not the frame pointer can
3753 be eliminated can depend on what registers have been used. We need
3754 not check some conditions again (such as flag_omit_frame_pointer)
3755 since they can't have changed. */
3757 static void
3758 update_eliminables (pset)
3759 HARD_REG_SET *pset;
3761 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3762 int previous_frame_pointer_needed = frame_pointer_needed;
3763 #endif
3764 struct elim_table *ep;
3766 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3767 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3768 #ifdef ELIMINABLE_REGS
3769 || ! CAN_ELIMINATE (ep->from, ep->to)
3770 #endif
3772 ep->can_eliminate = 0;
3774 /* Look for the case where we have discovered that we can't replace
3775 register A with register B and that means that we will now be
3776 trying to replace register A with register C. This means we can
3777 no longer replace register C with register B and we need to disable
3778 such an elimination, if it exists. This occurs often with A == ap,
3779 B == sp, and C == fp. */
3781 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3783 struct elim_table *op;
3784 register int new_to = -1;
3786 if (! ep->can_eliminate && ep->can_eliminate_previous)
3788 /* Find the current elimination for ep->from, if there is a
3789 new one. */
3790 for (op = reg_eliminate;
3791 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3792 if (op->from == ep->from && op->can_eliminate)
3794 new_to = op->to;
3795 break;
3798 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3799 disable it. */
3800 for (op = reg_eliminate;
3801 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3802 if (op->from == new_to && op->to == ep->to)
3803 op->can_eliminate = 0;
3807 /* See if any registers that we thought we could eliminate the previous
3808 time are no longer eliminable. If so, something has changed and we
3809 must spill the register. Also, recompute the number of eliminable
3810 registers and see if the frame pointer is needed; it is if there is
3811 no elimination of the frame pointer that we can perform. */
3813 frame_pointer_needed = 1;
3814 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3816 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3817 && ep->to != HARD_FRAME_POINTER_REGNUM)
3818 frame_pointer_needed = 0;
3820 if (! ep->can_eliminate && ep->can_eliminate_previous)
3822 ep->can_eliminate_previous = 0;
3823 SET_HARD_REG_BIT (*pset, ep->from);
3824 num_eliminable--;
3828 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3829 /* If we didn't need a frame pointer last time, but we do now, spill
3830 the hard frame pointer. */
3831 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3832 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3833 #endif
3836 /* Initialize the table of registers to eliminate. */
3837 static void
3838 init_elim_table ()
3840 struct elim_table *ep;
3841 #ifdef ELIMINABLE_REGS
3842 struct elim_table_1 *ep1;
3843 #endif
3845 if (!reg_eliminate)
3846 reg_eliminate = (struct elim_table *)
3847 xcalloc(sizeof(struct elim_table), NUM_ELIMINABLE_REGS);
3849 /* Does this function require a frame pointer? */
3851 frame_pointer_needed = (! flag_omit_frame_pointer
3852 #ifdef EXIT_IGNORE_STACK
3853 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3854 and restore sp for alloca. So we can't eliminate
3855 the frame pointer in that case. At some point,
3856 we should improve this by emitting the
3857 sp-adjusting insns for this case. */
3858 || (current_function_calls_alloca
3859 && EXIT_IGNORE_STACK)
3860 #endif
3861 || FRAME_POINTER_REQUIRED);
3863 num_eliminable = 0;
3865 #ifdef ELIMINABLE_REGS
3866 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3867 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3869 ep->from = ep1->from;
3870 ep->to = ep1->to;
3871 ep->can_eliminate = ep->can_eliminate_previous
3872 = (CAN_ELIMINATE (ep->from, ep->to)
3873 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3875 #else
3876 reg_eliminate[0].from = reg_eliminate_1[0].from;
3877 reg_eliminate[0].to = reg_eliminate_1[0].to;
3878 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3879 = ! frame_pointer_needed;
3880 #endif
3882 /* Count the number of eliminable registers and build the FROM and TO
3883 REG rtx's. Note that code in gen_rtx will cause, e.g.,
3884 gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3885 We depend on this. */
3886 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3888 num_eliminable += ep->can_eliminate;
3889 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3890 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3894 /* Kick all pseudos out of hard register REGNO.
3895 If DUMPFILE is nonzero, log actions taken on that file.
3897 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3898 because we found we can't eliminate some register. In the case, no pseudos
3899 are allowed to be in the register, even if they are only in a block that
3900 doesn't require spill registers, unlike the case when we are spilling this
3901 hard reg to produce another spill register.
3903 Return nonzero if any pseudos needed to be kicked out. */
3905 static void
3906 spill_hard_reg (regno, dumpfile, cant_eliminate)
3907 register int regno;
3908 FILE *dumpfile ATTRIBUTE_UNUSED;
3909 int cant_eliminate;
3911 register int i;
3913 if (cant_eliminate)
3915 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3916 regs_ever_live[regno] = 1;
3919 /* Spill every pseudo reg that was allocated to this reg
3920 or to something that overlaps this reg. */
3922 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3923 if (reg_renumber[i] >= 0
3924 && reg_renumber[i] <= regno
3925 && (reg_renumber[i]
3926 + HARD_REGNO_NREGS (reg_renumber[i],
3927 PSEUDO_REGNO_MODE (i))
3928 > regno))
3929 SET_REGNO_REG_SET (spilled_pseudos, i);
3932 /* I'm getting weird preprocessor errors if I use IOR_HARD_REG_SET
3933 from within EXECUTE_IF_SET_IN_REG_SET. Hence this awkwardness. */
3934 static void
3935 ior_hard_reg_set (set1, set2)
3936 HARD_REG_SET *set1, *set2;
3938 IOR_HARD_REG_SET (*set1, *set2);
3941 /* After find_reload_regs has been run for all insn that need reloads,
3942 and/or spill_hard_regs was called, this function is used to actually
3943 spill pseudo registers and try to reallocate them. It also sets up the
3944 spill_regs array for use by choose_reload_regs. */
3946 static int
3947 finish_spills (global, dumpfile)
3948 int global;
3949 FILE *dumpfile;
3951 struct insn_chain *chain;
3952 int something_changed = 0;
3953 int i;
3955 /* Build the spill_regs array for the function. */
3956 /* If there are some registers still to eliminate and one of the spill regs
3957 wasn't ever used before, additional stack space may have to be
3958 allocated to store this register. Thus, we may have changed the offset
3959 between the stack and frame pointers, so mark that something has changed.
3961 One might think that we need only set VAL to 1 if this is a call-used
3962 register. However, the set of registers that must be saved by the
3963 prologue is not identical to the call-used set. For example, the
3964 register used by the call insn for the return PC is a call-used register,
3965 but must be saved by the prologue. */
3967 n_spills = 0;
3968 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3969 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3971 spill_reg_order[i] = n_spills;
3972 spill_regs[n_spills++] = i;
3973 if (num_eliminable && ! regs_ever_live[i])
3974 something_changed = 1;
3975 regs_ever_live[i] = 1;
3977 else
3978 spill_reg_order[i] = -1;
3980 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3981 if (REGNO_REG_SET_P (spilled_pseudos, i))
3983 /* Record the current hard register the pseudo is allocated to in
3984 pseudo_previous_regs so we avoid reallocating it to the same
3985 hard reg in a later pass. */
3986 if (reg_renumber[i] < 0)
3987 abort ();
3988 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3989 /* Mark it as no longer having a hard register home. */
3990 reg_renumber[i] = -1;
3991 /* We will need to scan everything again. */
3992 something_changed = 1;
3995 /* Retry global register allocation if possible. */
3996 if (global)
3998 bzero ((char *) pseudo_forbidden_regs, max_regno * sizeof (HARD_REG_SET));
3999 /* For every insn that needs reloads, set the registers used as spill
4000 regs in pseudo_forbidden_regs for every pseudo live across the
4001 insn. */
4002 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
4004 EXECUTE_IF_SET_IN_REG_SET
4005 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
4007 ior_hard_reg_set (pseudo_forbidden_regs + i,
4008 &chain->used_spill_regs);
4010 EXECUTE_IF_SET_IN_REG_SET
4011 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
4013 ior_hard_reg_set (pseudo_forbidden_regs + i,
4014 &chain->used_spill_regs);
4018 /* Retry allocating the spilled pseudos. For each reg, merge the
4019 various reg sets that indicate which hard regs can't be used,
4020 and call retry_global_alloc.
4021 We change spill_pseudos here to only contain pseudos that did not
4022 get a new hard register. */
4023 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4024 if (reg_old_renumber[i] != reg_renumber[i])
4026 HARD_REG_SET forbidden;
4027 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
4028 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
4029 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
4030 retry_global_alloc (i, forbidden);
4031 if (reg_renumber[i] >= 0)
4032 CLEAR_REGNO_REG_SET (spilled_pseudos, i);
4036 /* Fix up the register information in the insn chain.
4037 This involves deleting those of the spilled pseudos which did not get
4038 a new hard register home from the live_{before,after} sets. */
4039 for (chain = reload_insn_chain; chain; chain = chain->next)
4041 HARD_REG_SET used_by_pseudos;
4042 HARD_REG_SET used_by_pseudos2;
4044 AND_COMPL_REG_SET (chain->live_before, spilled_pseudos);
4045 AND_COMPL_REG_SET (chain->live_after, spilled_pseudos);
4047 /* Mark any unallocated hard regs as available for spills. That
4048 makes inheritance work somewhat better. */
4049 if (chain->need_reload)
4051 REG_SET_TO_HARD_REG_SET (used_by_pseudos, chain->live_before);
4052 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, chain->live_after);
4053 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
4055 /* Save the old value for the sanity test below. */
4056 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
4058 compute_use_by_pseudos (&used_by_pseudos, chain->live_before);
4059 compute_use_by_pseudos (&used_by_pseudos, chain->live_after);
4060 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
4061 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
4063 /* Make sure we only enlarge the set. */
4064 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
4065 abort ();
4066 ok:;
4070 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
4071 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4073 int regno = reg_renumber[i];
4074 if (reg_old_renumber[i] == regno)
4075 continue;
4077 alter_reg (i, reg_old_renumber[i]);
4078 reg_old_renumber[i] = regno;
4079 if (dumpfile)
4081 if (regno == -1)
4082 fprintf (dumpfile, " Register %d now on stack.\n\n", i);
4083 else
4084 fprintf (dumpfile, " Register %d now in %d.\n\n",
4085 i, reg_renumber[i]);
4089 return something_changed;
4092 /* Find all paradoxical subregs within X and update reg_max_ref_width.
4093 Also mark any hard registers used to store user variables as
4094 forbidden from being used for spill registers. */
4096 static void
4097 scan_paradoxical_subregs (x)
4098 register rtx x;
4100 register int i;
4101 register const char *fmt;
4102 register enum rtx_code code = GET_CODE (x);
4104 switch (code)
4106 case REG:
4107 #if 0
4108 if (SMALL_REGISTER_CLASSES && REGNO (x) < FIRST_PSEUDO_REGISTER
4109 && REG_USERVAR_P (x))
4110 SET_HARD_REG_BIT (bad_spill_regs_global, REGNO (x));
4111 #endif
4112 return;
4114 case CONST_INT:
4115 case CONST:
4116 case SYMBOL_REF:
4117 case LABEL_REF:
4118 case CONST_DOUBLE:
4119 case CC0:
4120 case PC:
4121 case USE:
4122 case CLOBBER:
4123 return;
4125 case SUBREG:
4126 if (GET_CODE (SUBREG_REG (x)) == REG
4127 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
4128 reg_max_ref_width[REGNO (SUBREG_REG (x))]
4129 = GET_MODE_SIZE (GET_MODE (x));
4130 return;
4132 default:
4133 break;
4136 fmt = GET_RTX_FORMAT (code);
4137 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4139 if (fmt[i] == 'e')
4140 scan_paradoxical_subregs (XEXP (x, i));
4141 else if (fmt[i] == 'E')
4143 register int j;
4144 for (j = XVECLEN (x, i) - 1; j >=0; j--)
4145 scan_paradoxical_subregs (XVECEXP (x, i, j));
4150 static int
4151 hard_reg_use_compare (p1p, p2p)
4152 const PTR p1p;
4153 const PTR p2p;
4155 const struct hard_reg_n_uses *p1 = (const struct hard_reg_n_uses *)p1p;
4156 const struct hard_reg_n_uses *p2 = (const struct hard_reg_n_uses *)p2p;
4157 int bad1 = TEST_HARD_REG_BIT (bad_spill_regs, p1->regno);
4158 int bad2 = TEST_HARD_REG_BIT (bad_spill_regs, p2->regno);
4159 if (bad1 && bad2)
4160 return p1->regno - p2->regno;
4161 if (bad1)
4162 return 1;
4163 if (bad2)
4164 return -1;
4165 if (p1->uses > p2->uses)
4166 return 1;
4167 if (p1->uses < p2->uses)
4168 return -1;
4169 /* If regs are equally good, sort by regno,
4170 so that the results of qsort leave nothing to chance. */
4171 return p1->regno - p2->regno;
4174 /* Used for communication between order_regs_for_reload and count_pseudo.
4175 Used to avoid counting one pseudo twice. */
4176 static regset pseudos_counted;
4178 /* Update the costs in N_USES, considering that pseudo REG is live. */
4179 static void
4180 count_pseudo (n_uses, reg)
4181 struct hard_reg_n_uses *n_uses;
4182 int reg;
4184 int r = reg_renumber[reg];
4185 int nregs;
4187 if (REGNO_REG_SET_P (pseudos_counted, reg))
4188 return;
4189 SET_REGNO_REG_SET (pseudos_counted, reg);
4191 if (r < 0)
4192 abort ();
4194 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
4195 while (nregs-- > 0)
4196 n_uses[r++].uses += REG_N_REFS (reg);
4198 /* Choose the order to consider regs for use as reload registers
4199 based on how much trouble would be caused by spilling one.
4200 Store them in order of decreasing preference in potential_reload_regs. */
4202 static void
4203 order_regs_for_reload (chain)
4204 struct insn_chain *chain;
4206 register int i;
4207 register int o = 0;
4208 struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
4210 pseudos_counted = ALLOCA_REG_SET ();
4212 COPY_HARD_REG_SET (bad_spill_regs, bad_spill_regs_global);
4214 /* Count number of uses of each hard reg by pseudo regs allocated to it
4215 and then order them by decreasing use. */
4217 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4219 hard_reg_n_uses[i].regno = i;
4220 hard_reg_n_uses[i].uses = 0;
4222 /* Test the various reasons why we can't use a register for
4223 spilling in this insn. */
4224 if (fixed_regs[i]
4225 || REGNO_REG_SET_P (chain->live_before, i)
4226 || REGNO_REG_SET_P (chain->live_after, i))
4227 SET_HARD_REG_BIT (bad_spill_regs, i);
4230 /* Now compute hard_reg_n_uses. */
4231 CLEAR_REG_SET (pseudos_counted);
4233 EXECUTE_IF_SET_IN_REG_SET
4234 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
4236 count_pseudo (hard_reg_n_uses, i);
4238 EXECUTE_IF_SET_IN_REG_SET
4239 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
4241 count_pseudo (hard_reg_n_uses, i);
4244 FREE_REG_SET (pseudos_counted);
4246 /* Prefer registers not so far used, for use in temporary loading.
4247 Among them, if REG_ALLOC_ORDER is defined, use that order.
4248 Otherwise, prefer registers not preserved by calls. */
4250 #ifdef REG_ALLOC_ORDER
4251 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4253 int regno = reg_alloc_order[i];
4255 if (hard_reg_n_uses[regno].uses == 0
4256 && ! TEST_HARD_REG_BIT (bad_spill_regs, regno))
4257 potential_reload_regs[o++] = regno;
4259 #else
4260 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4262 if (hard_reg_n_uses[i].uses == 0 && call_used_regs[i]
4263 && ! TEST_HARD_REG_BIT (bad_spill_regs, i))
4264 potential_reload_regs[o++] = i;
4266 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4268 if (hard_reg_n_uses[i].uses == 0 && ! call_used_regs[i]
4269 && ! TEST_HARD_REG_BIT (bad_spill_regs, i))
4270 potential_reload_regs[o++] = i;
4272 #endif
4274 qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
4275 sizeof hard_reg_n_uses[0], hard_reg_use_compare);
4277 /* Now add the regs that are already used,
4278 preferring those used less often. The fixed and otherwise forbidden
4279 registers will be at the end of this list. */
4281 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4282 if (hard_reg_n_uses[i].uses != 0
4283 && ! TEST_HARD_REG_BIT (bad_spill_regs, hard_reg_n_uses[i].regno))
4284 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
4285 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4286 if (TEST_HARD_REG_BIT (bad_spill_regs, hard_reg_n_uses[i].regno))
4287 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
4290 /* Reload pseudo-registers into hard regs around each insn as needed.
4291 Additional register load insns are output before the insn that needs it
4292 and perhaps store insns after insns that modify the reloaded pseudo reg.
4294 reg_last_reload_reg and reg_reloaded_contents keep track of
4295 which registers are already available in reload registers.
4296 We update these for the reloads that we perform,
4297 as the insns are scanned. */
4299 static void
4300 reload_as_needed (live_known)
4301 int live_known;
4303 struct insn_chain *chain;
4304 #if defined (AUTO_INC_DEC) || defined (INSN_CLOBBERS_REGNO_P)
4305 register int i;
4306 #endif
4307 rtx x;
4309 bzero ((char *) spill_reg_rtx, sizeof spill_reg_rtx);
4310 bzero ((char *) spill_reg_store, sizeof spill_reg_store);
4311 reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
4312 bzero ((char *) reg_last_reload_reg, max_regno * sizeof (rtx));
4313 reg_has_output_reload = (char *) alloca (max_regno);
4314 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4316 set_initial_elim_offsets ();
4318 for (chain = reload_insn_chain; chain; chain = chain->next)
4320 rtx prev;
4321 rtx insn = chain->insn;
4322 rtx old_next = NEXT_INSN (insn);
4324 /* If we pass a label, copy the offsets from the label information
4325 into the current offsets of each elimination. */
4326 if (GET_CODE (insn) == CODE_LABEL)
4327 set_offsets_for_label (insn);
4329 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4331 rtx oldpat = PATTERN (insn);
4333 /* If this is a USE and CLOBBER of a MEM, ensure that any
4334 references to eliminable registers have been removed. */
4336 if ((GET_CODE (PATTERN (insn)) == USE
4337 || GET_CODE (PATTERN (insn)) == CLOBBER)
4338 && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
4339 XEXP (XEXP (PATTERN (insn), 0), 0)
4340 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
4341 GET_MODE (XEXP (PATTERN (insn), 0)),
4342 NULL_RTX);
4344 /* If we need to do register elimination processing, do so.
4345 This might delete the insn, in which case we are done. */
4346 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
4348 eliminate_regs_in_insn (insn, 1);
4349 if (GET_CODE (insn) == NOTE)
4351 update_eliminable_offsets ();
4352 continue;
4356 /* If need_elim is nonzero but need_reload is zero, one might think
4357 that we could simply set n_reloads to 0. However, find_reloads
4358 could have done some manipulation of the insn (such as swapping
4359 commutative operands), and these manipulations are lost during
4360 the first pass for every insn that needs register elimination.
4361 So the actions of find_reloads must be redone here. */
4363 if (! chain->need_elim && ! chain->need_reload
4364 && ! chain->need_operand_change)
4365 n_reloads = 0;
4366 /* First find the pseudo regs that must be reloaded for this insn.
4367 This info is returned in the tables reload_... (see reload.h).
4368 Also modify the body of INSN by substituting RELOAD
4369 rtx's for those pseudo regs. */
4370 else
4372 bzero (reg_has_output_reload, max_regno);
4373 CLEAR_HARD_REG_SET (reg_is_output_reload);
4375 find_reloads (insn, 1, spill_indirect_levels, live_known,
4376 spill_reg_order);
4379 if (num_eliminable && chain->need_elim)
4380 update_eliminable_offsets ();
4382 if (n_reloads > 0)
4384 rtx next = NEXT_INSN (insn);
4385 rtx p;
4387 prev = PREV_INSN (insn);
4389 /* Now compute which reload regs to reload them into. Perhaps
4390 reusing reload regs from previous insns, or else output
4391 load insns to reload them. Maybe output store insns too.
4392 Record the choices of reload reg in reload_reg_rtx. */
4393 choose_reload_regs (chain);
4395 /* Merge any reloads that we didn't combine for fear of
4396 increasing the number of spill registers needed but now
4397 discover can be safely merged. */
4398 if (SMALL_REGISTER_CLASSES)
4399 merge_assigned_reloads (insn);
4401 /* Generate the insns to reload operands into or out of
4402 their reload regs. */
4403 emit_reload_insns (chain);
4405 /* Substitute the chosen reload regs from reload_reg_rtx
4406 into the insn's body (or perhaps into the bodies of other
4407 load and store insn that we just made for reloading
4408 and that we moved the structure into). */
4409 subst_reloads ();
4411 /* If this was an ASM, make sure that all the reload insns
4412 we have generated are valid. If not, give an error
4413 and delete them. */
4415 if (asm_noperands (PATTERN (insn)) >= 0)
4416 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4417 if (p != insn && GET_RTX_CLASS (GET_CODE (p)) == 'i'
4418 && (recog_memoized (p) < 0
4419 || (extract_insn (p), ! constrain_operands (1))))
4421 error_for_asm (insn,
4422 "`asm' operand requires impossible reload");
4423 PUT_CODE (p, NOTE);
4424 NOTE_SOURCE_FILE (p) = 0;
4425 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
4428 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4429 is no longer validly lying around to save a future reload.
4430 Note that this does not detect pseudos that were reloaded
4431 for this insn in order to be stored in
4432 (obeying register constraints). That is correct; such reload
4433 registers ARE still valid. */
4434 note_stores (oldpat, forget_old_reloads_1);
4436 /* There may have been CLOBBER insns placed after INSN. So scan
4437 between INSN and NEXT and use them to forget old reloads. */
4438 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
4439 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
4440 note_stores (PATTERN (x), forget_old_reloads_1);
4442 #ifdef AUTO_INC_DEC
4443 /* Likewise for regs altered by auto-increment in this insn.
4444 REG_INC notes have been changed by reloading:
4445 find_reloads_address_1 records substitutions for them,
4446 which have been performed by subst_reloads above. */
4447 for (i = n_reloads - 1; i >= 0; i--)
4449 rtx in_reg = rld[i].in_reg;
4450 if (in_reg)
4452 enum rtx_code code = GET_CODE (in_reg);
4453 /* PRE_INC / PRE_DEC will have the reload register ending up
4454 with the same value as the stack slot, but that doesn't
4455 hold true for POST_INC / POST_DEC. Either we have to
4456 convert the memory access to a true POST_INC / POST_DEC,
4457 or we can't use the reload register for inheritance. */
4458 if ((code == POST_INC || code == POST_DEC)
4459 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4460 REGNO (rld[i].reg_rtx))
4461 /* Make sure it is the inc/dec pseudo, and not
4462 some other (e.g. output operand) pseudo. */
4463 && (reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4464 == REGNO (XEXP (in_reg, 0))))
4467 rtx reload_reg = rld[i].reg_rtx;
4468 enum machine_mode mode = GET_MODE (reload_reg);
4469 int n = 0;
4470 rtx p;
4472 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4474 /* We really want to ignore REG_INC notes here, so
4475 use PATTERN (p) as argument to reg_set_p . */
4476 if (reg_set_p (reload_reg, PATTERN (p)))
4477 break;
4478 n = count_occurrences (PATTERN (p), reload_reg);
4479 if (! n)
4480 continue;
4481 if (n == 1)
4483 n = validate_replace_rtx (reload_reg,
4484 gen_rtx (code, mode,
4485 reload_reg),
4488 /* We must also verify that the constraints
4489 are met after the replacement. */
4490 extract_insn (p);
4491 if (n)
4492 n = constrain_operands (1);
4493 else
4494 break;
4496 /* If the constraints were not met, then
4497 undo the replacement. */
4498 if (!n)
4500 validate_replace_rtx (gen_rtx (code, mode,
4501 reload_reg),
4502 reload_reg, p);
4503 break;
4507 break;
4509 if (n == 1)
4511 REG_NOTES (p)
4512 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
4513 REG_NOTES (p));
4514 /* Mark this as having an output reload so that the
4515 REG_INC processing code below won't invalidate
4516 the reload for inheritance. */
4517 SET_HARD_REG_BIT (reg_is_output_reload,
4518 REGNO (reload_reg));
4519 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4521 else
4522 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX);
4524 else if ((code == PRE_INC || code == PRE_DEC)
4525 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4526 REGNO (rld[i].reg_rtx))
4527 /* Make sure it is the inc/dec pseudo, and not
4528 some other (e.g. output operand) pseudo. */
4529 && (reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4530 == REGNO (XEXP (in_reg, 0))))
4532 SET_HARD_REG_BIT (reg_is_output_reload,
4533 REGNO (rld[i].reg_rtx));
4534 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4538 /* If a pseudo that got a hard register is auto-incremented,
4539 we must purge records of copying it into pseudos without
4540 hard registers. */
4541 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4542 if (REG_NOTE_KIND (x) == REG_INC)
4544 /* See if this pseudo reg was reloaded in this insn.
4545 If so, its last-reload info is still valid
4546 because it is based on this insn's reload. */
4547 for (i = 0; i < n_reloads; i++)
4548 if (rld[i].out == XEXP (x, 0))
4549 break;
4551 if (i == n_reloads)
4552 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX);
4554 #endif
4556 /* A reload reg's contents are unknown after a label. */
4557 if (GET_CODE (insn) == CODE_LABEL)
4558 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4560 /* Don't assume a reload reg is still good after a call insn
4561 if it is a call-used reg. */
4562 else if (GET_CODE (insn) == CALL_INSN)
4563 AND_COMPL_HARD_REG_SET(reg_reloaded_valid, call_used_reg_set);
4565 /* In case registers overlap, allow certain insns to invalidate
4566 particular hard registers. */
4568 #ifdef INSN_CLOBBERS_REGNO_P
4569 for (i = 0 ; i < FIRST_PSEUDO_REGISTER; i++)
4570 if (TEST_HARD_REG_BIT (reg_reloaded_valid, i)
4571 && INSN_CLOBBERS_REGNO_P (insn, i))
4572 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i);
4573 #endif
4575 #ifdef USE_C_ALLOCA
4576 alloca (0);
4577 #endif
4581 /* Discard all record of any value reloaded from X,
4582 or reloaded in X from someplace else;
4583 unless X is an output reload reg of the current insn.
4585 X may be a hard reg (the reload reg)
4586 or it may be a pseudo reg that was reloaded from. */
4588 static void
4589 forget_old_reloads_1 (x, ignored)
4590 rtx x;
4591 rtx ignored ATTRIBUTE_UNUSED;
4593 register int regno;
4594 int nr;
4595 int offset = 0;
4597 /* note_stores does give us subregs of hard regs. */
4598 while (GET_CODE (x) == SUBREG)
4600 offset += SUBREG_WORD (x);
4601 x = SUBREG_REG (x);
4604 if (GET_CODE (x) != REG)
4605 return;
4607 regno = REGNO (x) + offset;
4609 if (regno >= FIRST_PSEUDO_REGISTER)
4610 nr = 1;
4611 else
4613 int i;
4614 nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
4615 /* Storing into a spilled-reg invalidates its contents.
4616 This can happen if a block-local pseudo is allocated to that reg
4617 and it wasn't spilled because this block's total need is 0.
4618 Then some insn might have an optional reload and use this reg. */
4619 for (i = 0; i < nr; i++)
4620 /* But don't do this if the reg actually serves as an output
4621 reload reg in the current instruction. */
4622 if (n_reloads == 0
4623 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4624 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4627 /* Since value of X has changed,
4628 forget any value previously copied from it. */
4630 while (nr-- > 0)
4631 /* But don't forget a copy if this is the output reload
4632 that establishes the copy's validity. */
4633 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4634 reg_last_reload_reg[regno + nr] = 0;
4637 /* Comparison function for qsort to decide which of two reloads
4638 should be handled first. *P1 and *P2 are the reload numbers. */
4640 static int
4641 reload_reg_class_lower (r1p, r2p)
4642 const PTR r1p;
4643 const PTR r2p;
4645 register int r1 = *(const short *)r1p, r2 = *(const short *)r2p;
4646 register int t;
4648 /* Consider required reloads before optional ones. */
4649 t = rld[r1].optional - rld[r2].optional;
4650 if (t != 0)
4651 return t;
4653 /* Count all solitary classes before non-solitary ones. */
4654 t = ((reg_class_size[(int) rld[r2].class] == 1)
4655 - (reg_class_size[(int) rld[r1].class] == 1));
4656 if (t != 0)
4657 return t;
4659 /* Aside from solitaires, consider all multi-reg groups first. */
4660 t = rld[r2].nregs - rld[r1].nregs;
4661 if (t != 0)
4662 return t;
4664 /* Consider reloads in order of increasing reg-class number. */
4665 t = (int) rld[r1].class - (int) rld[r2].class;
4666 if (t != 0)
4667 return t;
4669 /* If reloads are equally urgent, sort by reload number,
4670 so that the results of qsort leave nothing to chance. */
4671 return r1 - r2;
4674 /* The following HARD_REG_SETs indicate when each hard register is
4675 used for a reload of various parts of the current insn. */
4677 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4678 static HARD_REG_SET reload_reg_used;
4679 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4680 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4681 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4682 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4683 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4684 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4685 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4686 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4687 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4688 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4689 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4690 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4691 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4692 static HARD_REG_SET reload_reg_used_in_op_addr;
4693 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4694 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4695 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4696 static HARD_REG_SET reload_reg_used_in_insn;
4697 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4698 static HARD_REG_SET reload_reg_used_in_other_addr;
4700 /* If reg is in use as a reload reg for any sort of reload. */
4701 static HARD_REG_SET reload_reg_used_at_all;
4703 /* If reg is use as an inherited reload. We just mark the first register
4704 in the group. */
4705 static HARD_REG_SET reload_reg_used_for_inherit;
4707 /* Records which hard regs are used in any way, either as explicit use or
4708 by being allocated to a pseudo during any point of the current insn. */
4709 static HARD_REG_SET reg_used_in_insn;
4711 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4712 TYPE. MODE is used to indicate how many consecutive regs are
4713 actually used. */
4715 static void
4716 mark_reload_reg_in_use (regno, opnum, type, mode)
4717 int regno;
4718 int opnum;
4719 enum reload_type type;
4720 enum machine_mode mode;
4722 int nregs = HARD_REGNO_NREGS (regno, mode);
4723 int i;
4725 for (i = regno; i < nregs + regno; i++)
4727 switch (type)
4729 case RELOAD_OTHER:
4730 SET_HARD_REG_BIT (reload_reg_used, i);
4731 break;
4733 case RELOAD_FOR_INPUT_ADDRESS:
4734 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4735 break;
4737 case RELOAD_FOR_INPADDR_ADDRESS:
4738 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4739 break;
4741 case RELOAD_FOR_OUTPUT_ADDRESS:
4742 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4743 break;
4745 case RELOAD_FOR_OUTADDR_ADDRESS:
4746 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4747 break;
4749 case RELOAD_FOR_OPERAND_ADDRESS:
4750 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4751 break;
4753 case RELOAD_FOR_OPADDR_ADDR:
4754 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4755 break;
4757 case RELOAD_FOR_OTHER_ADDRESS:
4758 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4759 break;
4761 case RELOAD_FOR_INPUT:
4762 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4763 break;
4765 case RELOAD_FOR_OUTPUT:
4766 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4767 break;
4769 case RELOAD_FOR_INSN:
4770 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4771 break;
4774 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4778 /* Similarly, but show REGNO is no longer in use for a reload. */
4780 static void
4781 clear_reload_reg_in_use (regno, opnum, type, mode)
4782 int regno;
4783 int opnum;
4784 enum reload_type type;
4785 enum machine_mode mode;
4787 int nregs = HARD_REGNO_NREGS (regno, mode);
4788 int start_regno, end_regno;
4789 int i;
4790 /* A complication is that for some reload types, inheritance might
4791 allow multiple reloads of the same types to share a reload register.
4792 We set check_opnum if we have to check only reloads with the same
4793 operand number, and check_any if we have to check all reloads. */
4794 int check_opnum = 0;
4795 int check_any = 0;
4796 HARD_REG_SET *used_in_set;
4798 switch (type)
4800 case RELOAD_OTHER:
4801 used_in_set = &reload_reg_used;
4802 break;
4804 case RELOAD_FOR_INPUT_ADDRESS:
4805 used_in_set = &reload_reg_used_in_input_addr[opnum];
4806 break;
4808 case RELOAD_FOR_INPADDR_ADDRESS:
4809 check_opnum = 1;
4810 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4811 break;
4813 case RELOAD_FOR_OUTPUT_ADDRESS:
4814 used_in_set = &reload_reg_used_in_output_addr[opnum];
4815 break;
4817 case RELOAD_FOR_OUTADDR_ADDRESS:
4818 check_opnum = 1;
4819 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4820 break;
4822 case RELOAD_FOR_OPERAND_ADDRESS:
4823 used_in_set = &reload_reg_used_in_op_addr;
4824 break;
4826 case RELOAD_FOR_OPADDR_ADDR:
4827 check_any = 1;
4828 used_in_set = &reload_reg_used_in_op_addr_reload;
4829 break;
4831 case RELOAD_FOR_OTHER_ADDRESS:
4832 used_in_set = &reload_reg_used_in_other_addr;
4833 check_any = 1;
4834 break;
4836 case RELOAD_FOR_INPUT:
4837 used_in_set = &reload_reg_used_in_input[opnum];
4838 break;
4840 case RELOAD_FOR_OUTPUT:
4841 used_in_set = &reload_reg_used_in_output[opnum];
4842 break;
4844 case RELOAD_FOR_INSN:
4845 used_in_set = &reload_reg_used_in_insn;
4846 break;
4847 default:
4848 abort ();
4850 /* We resolve conflicts with remaining reloads of the same type by
4851 excluding the intervals of of reload registers by them from the
4852 interval of freed reload registers. Since we only keep track of
4853 one set of interval bounds, we might have to exclude somewhat
4854 more then what would be necessary if we used a HARD_REG_SET here.
4855 But this should only happen very infrequently, so there should
4856 be no reason to worry about it. */
4858 start_regno = regno;
4859 end_regno = regno + nregs;
4860 if (check_opnum || check_any)
4862 for (i = n_reloads - 1; i >= 0; i--)
4864 if (rld[i].when_needed == type
4865 && (check_any || rld[i].opnum == opnum)
4866 && rld[i].reg_rtx)
4868 int conflict_start = true_regnum (rld[i].reg_rtx);
4869 int conflict_end
4870 = (conflict_start
4871 + HARD_REGNO_NREGS (conflict_start, rld[i].mode));
4873 /* If there is an overlap with the first to-be-freed register,
4874 adjust the interval start. */
4875 if (conflict_start <= start_regno && conflict_end > start_regno)
4876 start_regno = conflict_end;
4877 /* Otherwise, if there is a conflict with one of the other
4878 to-be-freed registers, adjust the interval end. */
4879 if (conflict_start > start_regno && conflict_start < end_regno)
4880 end_regno = conflict_start;
4884 for (i = start_regno; i < end_regno; i++)
4885 CLEAR_HARD_REG_BIT (*used_in_set, i);
4888 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4889 specified by OPNUM and TYPE. */
4891 static int
4892 reload_reg_free_p (regno, opnum, type)
4893 int regno;
4894 int opnum;
4895 enum reload_type type;
4897 int i;
4899 /* In use for a RELOAD_OTHER means it's not available for anything. */
4900 if (TEST_HARD_REG_BIT (reload_reg_used, regno))
4901 return 0;
4903 switch (type)
4905 case RELOAD_OTHER:
4906 /* In use for anything means we can't use it for RELOAD_OTHER. */
4907 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4908 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4909 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4910 return 0;
4912 for (i = 0; i < reload_n_operands; i++)
4913 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4914 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4915 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4916 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4917 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4918 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4919 return 0;
4921 return 1;
4923 case RELOAD_FOR_INPUT:
4924 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4925 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4926 return 0;
4928 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4929 return 0;
4931 /* If it is used for some other input, can't use it. */
4932 for (i = 0; i < reload_n_operands; i++)
4933 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4934 return 0;
4936 /* If it is used in a later operand's address, can't use it. */
4937 for (i = opnum + 1; i < reload_n_operands; i++)
4938 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4939 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4940 return 0;
4942 return 1;
4944 case RELOAD_FOR_INPUT_ADDRESS:
4945 /* Can't use a register if it is used for an input address for this
4946 operand or used as an input in an earlier one. */
4947 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4948 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4949 return 0;
4951 for (i = 0; i < opnum; i++)
4952 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4953 return 0;
4955 return 1;
4957 case RELOAD_FOR_INPADDR_ADDRESS:
4958 /* Can't use a register if it is used for an input address
4959 for this operand or used as an input in an earlier
4960 one. */
4961 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4962 return 0;
4964 for (i = 0; i < opnum; i++)
4965 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4966 return 0;
4968 return 1;
4970 case RELOAD_FOR_OUTPUT_ADDRESS:
4971 /* Can't use a register if it is used for an output address for this
4972 operand or used as an output in this or a later operand. */
4973 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4974 return 0;
4976 for (i = opnum; i < reload_n_operands; i++)
4977 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4978 return 0;
4980 return 1;
4982 case RELOAD_FOR_OUTADDR_ADDRESS:
4983 /* Can't use a register if it is used for an output address
4984 for this operand or used as an output in this or a
4985 later operand. */
4986 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4987 return 0;
4989 for (i = opnum; i < reload_n_operands; i++)
4990 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4991 return 0;
4993 return 1;
4995 case RELOAD_FOR_OPERAND_ADDRESS:
4996 for (i = 0; i < reload_n_operands; i++)
4997 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4998 return 0;
5000 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5001 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
5003 case RELOAD_FOR_OPADDR_ADDR:
5004 for (i = 0; i < reload_n_operands; i++)
5005 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5006 return 0;
5008 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
5010 case RELOAD_FOR_OUTPUT:
5011 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
5012 outputs, or an operand address for this or an earlier output. */
5013 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
5014 return 0;
5016 for (i = 0; i < reload_n_operands; i++)
5017 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5018 return 0;
5020 for (i = 0; i <= opnum; i++)
5021 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5022 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5023 return 0;
5025 return 1;
5027 case RELOAD_FOR_INSN:
5028 for (i = 0; i < reload_n_operands; i++)
5029 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
5030 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5031 return 0;
5033 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5034 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
5036 case RELOAD_FOR_OTHER_ADDRESS:
5037 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
5039 abort ();
5042 /* Return 1 if the value in reload reg REGNO, as used by a reload
5043 needed for the part of the insn specified by OPNUM and TYPE,
5044 is still available in REGNO at the end of the insn.
5046 We can assume that the reload reg was already tested for availability
5047 at the time it is needed, and we should not check this again,
5048 in case the reg has already been marked in use. */
5050 static int
5051 reload_reg_reaches_end_p (regno, opnum, type)
5052 int regno;
5053 int opnum;
5054 enum reload_type type;
5056 int i;
5058 switch (type)
5060 case RELOAD_OTHER:
5061 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
5062 its value must reach the end. */
5063 return 1;
5065 /* If this use is for part of the insn,
5066 its value reaches if no subsequent part uses the same register.
5067 Just like the above function, don't try to do this with lots
5068 of fallthroughs. */
5070 case RELOAD_FOR_OTHER_ADDRESS:
5071 /* Here we check for everything else, since these don't conflict
5072 with anything else and everything comes later. */
5074 for (i = 0; i < reload_n_operands; i++)
5075 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5076 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5077 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
5078 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5079 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5080 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5081 return 0;
5083 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5084 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5085 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
5087 case RELOAD_FOR_INPUT_ADDRESS:
5088 case RELOAD_FOR_INPADDR_ADDRESS:
5089 /* Similar, except that we check only for this and subsequent inputs
5090 and the address of only subsequent inputs and we do not need
5091 to check for RELOAD_OTHER objects since they are known not to
5092 conflict. */
5094 for (i = opnum; i < reload_n_operands; i++)
5095 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5096 return 0;
5098 for (i = opnum + 1; i < reload_n_operands; i++)
5099 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5100 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
5101 return 0;
5103 for (i = 0; i < reload_n_operands; i++)
5104 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5105 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5106 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5107 return 0;
5109 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
5110 return 0;
5112 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5113 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
5115 case RELOAD_FOR_INPUT:
5116 /* Similar to input address, except we start at the next operand for
5117 both input and input address and we do not check for
5118 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
5119 would conflict. */
5121 for (i = opnum + 1; i < reload_n_operands; i++)
5122 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5123 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5124 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5125 return 0;
5127 /* ... fall through ... */
5129 case RELOAD_FOR_OPERAND_ADDRESS:
5130 /* Check outputs and their addresses. */
5132 for (i = 0; i < reload_n_operands; i++)
5133 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5134 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5135 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5136 return 0;
5138 return 1;
5140 case RELOAD_FOR_OPADDR_ADDR:
5141 for (i = 0; i < reload_n_operands; i++)
5142 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5143 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5144 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5145 return 0;
5147 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5148 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
5150 case RELOAD_FOR_INSN:
5151 /* These conflict with other outputs with RELOAD_OTHER. So
5152 we need only check for output addresses. */
5154 opnum = -1;
5156 /* ... fall through ... */
5158 case RELOAD_FOR_OUTPUT:
5159 case RELOAD_FOR_OUTPUT_ADDRESS:
5160 case RELOAD_FOR_OUTADDR_ADDRESS:
5161 /* We already know these can't conflict with a later output. So the
5162 only thing to check are later output addresses. */
5163 for (i = opnum + 1; i < reload_n_operands; i++)
5164 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5165 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5166 return 0;
5168 return 1;
5171 abort ();
5174 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
5175 Return 0 otherwise.
5177 This function uses the same algorithm as reload_reg_free_p above. */
5180 reloads_conflict (r1, r2)
5181 int r1, r2;
5183 enum reload_type r1_type = rld[r1].when_needed;
5184 enum reload_type r2_type = rld[r2].when_needed;
5185 int r1_opnum = rld[r1].opnum;
5186 int r2_opnum = rld[r2].opnum;
5188 /* RELOAD_OTHER conflicts with everything. */
5189 if (r2_type == RELOAD_OTHER)
5190 return 1;
5192 /* Otherwise, check conflicts differently for each type. */
5194 switch (r1_type)
5196 case RELOAD_FOR_INPUT:
5197 return (r2_type == RELOAD_FOR_INSN
5198 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
5199 || r2_type == RELOAD_FOR_OPADDR_ADDR
5200 || r2_type == RELOAD_FOR_INPUT
5201 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
5202 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
5203 && r2_opnum > r1_opnum));
5205 case RELOAD_FOR_INPUT_ADDRESS:
5206 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
5207 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5209 case RELOAD_FOR_INPADDR_ADDRESS:
5210 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
5211 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5213 case RELOAD_FOR_OUTPUT_ADDRESS:
5214 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
5215 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
5217 case RELOAD_FOR_OUTADDR_ADDRESS:
5218 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
5219 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
5221 case RELOAD_FOR_OPERAND_ADDRESS:
5222 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
5223 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5225 case RELOAD_FOR_OPADDR_ADDR:
5226 return (r2_type == RELOAD_FOR_INPUT
5227 || r2_type == RELOAD_FOR_OPADDR_ADDR);
5229 case RELOAD_FOR_OUTPUT:
5230 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
5231 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
5232 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
5233 && r2_opnum >= r1_opnum));
5235 case RELOAD_FOR_INSN:
5236 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
5237 || r2_type == RELOAD_FOR_INSN
5238 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5240 case RELOAD_FOR_OTHER_ADDRESS:
5241 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
5243 case RELOAD_OTHER:
5244 return 1;
5246 default:
5247 abort ();
5251 /* Vector of reload-numbers showing the order in which the reloads should
5252 be processed. */
5253 short reload_order[MAX_RELOADS];
5255 /* Indexed by reload number, 1 if incoming value
5256 inherited from previous insns. */
5257 char reload_inherited[MAX_RELOADS];
5259 /* For an inherited reload, this is the insn the reload was inherited from,
5260 if we know it. Otherwise, this is 0. */
5261 rtx reload_inheritance_insn[MAX_RELOADS];
5263 /* If non-zero, this is a place to get the value of the reload,
5264 rather than using reload_in. */
5265 rtx reload_override_in[MAX_RELOADS];
5267 /* For each reload, the hard register number of the register used,
5268 or -1 if we did not need a register for this reload. */
5269 int reload_spill_index[MAX_RELOADS];
5271 /* Return 1 if the value in reload reg REGNO, as used by a reload
5272 needed for the part of the insn specified by OPNUM and TYPE,
5273 may be used to load VALUE into it.
5275 Other read-only reloads with the same value do not conflict
5276 unless OUT is non-zero and these other reloads have to live while
5277 output reloads live.
5278 If OUT is CONST0_RTX, this is a special case: it means that the
5279 test should not be for using register REGNO as reload register, but
5280 for copying from register REGNO into the reload register.
5282 RELOADNUM is the number of the reload we want to load this value for;
5283 a reload does not conflict with itself.
5285 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
5286 reloads that load an address for the very reload we are considering.
5288 The caller has to make sure that there is no conflict with the return
5289 register. */
5290 static int
5291 reload_reg_free_for_value_p (regno, opnum, type, value, out, reloadnum,
5292 ignore_address_reloads)
5293 int regno;
5294 int opnum;
5295 enum reload_type type;
5296 rtx value, out;
5297 int reloadnum;
5298 int ignore_address_reloads;
5300 int time1;
5301 int i;
5302 int copy = 0;
5304 if (out == const0_rtx)
5306 copy = 1;
5307 out = NULL_RTX;
5310 /* We use some pseudo 'time' value to check if the lifetimes of the
5311 new register use would overlap with the one of a previous reload
5312 that is not read-only or uses a different value.
5313 The 'time' used doesn't have to be linear in any shape or form, just
5314 monotonic.
5315 Some reload types use different 'buckets' for each operand.
5316 So there are MAX_RECOG_OPERANDS different time values for each
5317 such reload type.
5318 We compute TIME1 as the time when the register for the prospective
5319 new reload ceases to be live, and TIME2 for each existing
5320 reload as the time when that the reload register of that reload
5321 becomes live.
5322 Where there is little to be gained by exact lifetime calculations,
5323 we just make conservative assumptions, i.e. a longer lifetime;
5324 this is done in the 'default:' cases. */
5325 switch (type)
5327 case RELOAD_FOR_OTHER_ADDRESS:
5328 time1 = 0;
5329 break;
5330 case RELOAD_OTHER:
5331 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
5332 break;
5333 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
5334 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
5335 respectively, to the time values for these, we get distinct time
5336 values. To get distinct time values for each operand, we have to
5337 multiply opnum by at least three. We round that up to four because
5338 multiply by four is often cheaper. */
5339 case RELOAD_FOR_INPADDR_ADDRESS:
5340 time1 = opnum * 4 + 2;
5341 break;
5342 case RELOAD_FOR_INPUT_ADDRESS:
5343 time1 = opnum * 4 + 3;
5344 break;
5345 case RELOAD_FOR_INPUT:
5346 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
5347 executes (inclusive). */
5348 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
5349 break;
5350 case RELOAD_FOR_OPADDR_ADDR:
5351 /* opnum * 4 + 4
5352 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
5353 time1 = MAX_RECOG_OPERANDS * 4 + 1;
5354 break;
5355 case RELOAD_FOR_OPERAND_ADDRESS:
5356 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
5357 is executed. */
5358 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
5359 break;
5360 case RELOAD_FOR_OUTADDR_ADDRESS:
5361 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
5362 break;
5363 case RELOAD_FOR_OUTPUT_ADDRESS:
5364 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
5365 break;
5366 default:
5367 time1 = MAX_RECOG_OPERANDS * 5 + 5;
5370 for (i = 0; i < n_reloads; i++)
5372 rtx reg = rld[i].reg_rtx;
5373 if (reg && GET_CODE (reg) == REG
5374 && ((unsigned) regno - true_regnum (reg)
5375 <= HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)) - (unsigned)1)
5376 && i != reloadnum)
5378 if (! rld[i].in || ! rtx_equal_p (rld[i].in, value)
5379 || rld[i].out || out)
5381 int j, time2;
5382 switch (rld[i].when_needed)
5384 case RELOAD_FOR_OTHER_ADDRESS:
5385 time2 = 0;
5386 break;
5387 case RELOAD_FOR_INPADDR_ADDRESS:
5388 /* find_reloads makes sure that a
5389 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
5390 by at most one - the first -
5391 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
5392 address reload is inherited, the address address reload
5393 goes away, so we can ignore this conflict. */
5394 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
5395 && ignore_address_reloads
5396 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
5397 Then the address address is still needed to store
5398 back the new address. */
5399 && ! rld[reloadnum].out)
5400 continue;
5401 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
5402 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
5403 reloads go away. */
5404 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5405 && ignore_address_reloads
5406 /* Unless we are reloading an auto_inc expression. */
5407 && ! rld[reloadnum].out)
5408 continue;
5409 time2 = rld[i].opnum * 4 + 2;
5410 break;
5411 case RELOAD_FOR_INPUT_ADDRESS:
5412 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5413 && ignore_address_reloads
5414 && ! rld[reloadnum].out)
5415 continue;
5416 time2 = rld[i].opnum * 4 + 3;
5417 break;
5418 case RELOAD_FOR_INPUT:
5419 time2 = rld[i].opnum * 4 + 4;
5420 break;
5421 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
5422 == MAX_RECOG_OPERAND * 4 */
5423 case RELOAD_FOR_OPADDR_ADDR:
5424 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
5425 && ignore_address_reloads
5426 && ! rld[reloadnum].out)
5427 continue;
5428 time2 = MAX_RECOG_OPERANDS * 4 + 1;
5429 break;
5430 case RELOAD_FOR_OPERAND_ADDRESS:
5431 time2 = MAX_RECOG_OPERANDS * 4 + 2;
5432 break;
5433 case RELOAD_FOR_INSN:
5434 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5435 break;
5436 case RELOAD_FOR_OUTPUT:
5437 /* All RELOAD_FOR_OUTPUT reloads become live just after the
5438 instruction is executed. */
5439 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5440 break;
5441 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
5442 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
5443 value. */
5444 case RELOAD_FOR_OUTADDR_ADDRESS:
5445 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
5446 && ignore_address_reloads
5447 && ! rld[reloadnum].out)
5448 continue;
5449 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
5450 break;
5451 case RELOAD_FOR_OUTPUT_ADDRESS:
5452 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
5453 break;
5454 case RELOAD_OTHER:
5455 /* If there is no conflict in the input part, handle this
5456 like an output reload. */
5457 if (! rld[i].in || rtx_equal_p (rld[i].in, value))
5459 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5460 /* Earlyclobbered outputs must conflict with inputs. */
5461 for (j = 0; j < n_earlyclobbers; j++)
5462 if (rld[i].out == reload_earlyclobbers[j])
5463 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5465 break;
5467 time2 = 1;
5468 /* RELOAD_OTHER might be live beyond instruction execution,
5469 but this is not obvious when we set time2 = 1. So check
5470 here if there might be a problem with the new reload
5471 clobbering the register used by the RELOAD_OTHER. */
5472 if (out)
5473 return 0;
5474 break;
5475 default:
5476 return 0;
5478 if ((time1 >= time2
5479 && (! rld[i].in || rld[i].out
5480 || ! rtx_equal_p (rld[i].in, value)))
5481 || (out && rld[reloadnum].out_reg
5482 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
5483 return 0;
5487 return 1;
5490 /* Find a spill register to use as a reload register for reload R.
5491 LAST_RELOAD is non-zero if this is the last reload for the insn being
5492 processed.
5494 Set rld[R].reg_rtx to the register allocated.
5496 If NOERROR is nonzero, we return 1 if successful,
5497 or 0 if we couldn't find a spill reg and we didn't change anything. */
5499 static int
5500 allocate_reload_reg (chain, r, last_reload, noerror)
5501 struct insn_chain *chain;
5502 int r;
5503 int last_reload;
5504 int noerror;
5506 rtx insn = chain->insn;
5507 int i, pass, count, regno;
5508 rtx new;
5510 /* If we put this reload ahead, thinking it is a group,
5511 then insist on finding a group. Otherwise we can grab a
5512 reg that some other reload needs.
5513 (That can happen when we have a 68000 DATA_OR_FP_REG
5514 which is a group of data regs or one fp reg.)
5515 We need not be so restrictive if there are no more reloads
5516 for this insn.
5518 ??? Really it would be nicer to have smarter handling
5519 for that kind of reg class, where a problem like this is normal.
5520 Perhaps those classes should be avoided for reloading
5521 by use of more alternatives. */
5523 int force_group = rld[r].nregs > 1 && ! last_reload;
5525 /* If we want a single register and haven't yet found one,
5526 take any reg in the right class and not in use.
5527 If we want a consecutive group, here is where we look for it.
5529 We use two passes so we can first look for reload regs to
5530 reuse, which are already in use for other reloads in this insn,
5531 and only then use additional registers.
5532 I think that maximizing reuse is needed to make sure we don't
5533 run out of reload regs. Suppose we have three reloads, and
5534 reloads A and B can share regs. These need two regs.
5535 Suppose A and B are given different regs.
5536 That leaves none for C. */
5537 for (pass = 0; pass < 2; pass++)
5539 /* I is the index in spill_regs.
5540 We advance it round-robin between insns to use all spill regs
5541 equally, so that inherited reloads have a chance
5542 of leapfrogging each other. Don't do this, however, when we have
5543 group needs and failure would be fatal; if we only have a relatively
5544 small number of spill registers, and more than one of them has
5545 group needs, then by starting in the middle, we may end up
5546 allocating the first one in such a way that we are not left with
5547 sufficient groups to handle the rest. */
5549 if (noerror || ! force_group)
5550 i = last_spill_reg;
5551 else
5552 i = -1;
5554 for (count = 0; count < n_spills; count++)
5556 int class = (int) rld[r].class;
5557 int regnum;
5559 i++;
5560 if (i >= n_spills)
5561 i -= n_spills;
5562 regnum = spill_regs[i];
5564 if ((reload_reg_free_p (regnum, rld[r].opnum,
5565 rld[r].when_needed)
5566 || (rld[r].in
5567 /* We check reload_reg_used to make sure we
5568 don't clobber the return register. */
5569 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5570 && reload_reg_free_for_value_p (regnum,
5571 rld[r].opnum,
5572 rld[r].when_needed,
5573 rld[r].in,
5574 rld[r].out, r, 1)))
5575 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5576 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5577 /* Look first for regs to share, then for unshared. But
5578 don't share regs used for inherited reloads; they are
5579 the ones we want to preserve. */
5580 && (pass
5581 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5582 regnum)
5583 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5584 regnum))))
5586 int nr = HARD_REGNO_NREGS (regnum, rld[r].mode);
5587 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5588 (on 68000) got us two FP regs. If NR is 1,
5589 we would reject both of them. */
5590 if (force_group)
5591 nr = CLASS_MAX_NREGS (rld[r].class, rld[r].mode);
5592 /* If we need only one reg, we have already won. */
5593 if (nr == 1)
5595 /* But reject a single reg if we demand a group. */
5596 if (force_group)
5597 continue;
5598 break;
5600 /* Otherwise check that as many consecutive regs as we need
5601 are available here.
5602 Also, don't use for a group registers that are
5603 needed for nongroups. */
5604 if (! TEST_HARD_REG_BIT (chain->counted_for_nongroups, regnum))
5605 while (nr > 1)
5607 regno = regnum + nr - 1;
5608 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5609 && spill_reg_order[regno] >= 0
5610 && reload_reg_free_p (regno, rld[r].opnum,
5611 rld[r].when_needed)
5612 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups,
5613 regno)))
5614 break;
5615 nr--;
5617 if (nr == 1)
5618 break;
5622 /* If we found something on pass 1, omit pass 2. */
5623 if (count < n_spills)
5624 break;
5627 /* We should have found a spill register by now. */
5628 if (count == n_spills)
5630 if (noerror)
5631 return 0;
5632 goto failure;
5635 /* I is the index in SPILL_REG_RTX of the reload register we are to
5636 allocate. Get an rtx for it and find its register number. */
5638 new = spill_reg_rtx[i];
5640 if (new == 0 || GET_MODE (new) != rld[r].mode)
5641 spill_reg_rtx[i] = new
5642 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5644 regno = true_regnum (new);
5646 /* Detect when the reload reg can't hold the reload mode.
5647 This used to be one `if', but Sequent compiler can't handle that. */
5648 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5650 enum machine_mode test_mode = VOIDmode;
5651 if (rld[r].in)
5652 test_mode = GET_MODE (rld[r].in);
5653 /* If rld[r].in has VOIDmode, it means we will load it
5654 in whatever mode the reload reg has: to wit, rld[r].mode.
5655 We have already tested that for validity. */
5656 /* Aside from that, we need to test that the expressions
5657 to reload from or into have modes which are valid for this
5658 reload register. Otherwise the reload insns would be invalid. */
5659 if (! (rld[r].in != 0 && test_mode != VOIDmode
5660 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5661 if (! (rld[r].out != 0
5662 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5664 /* The reg is OK. */
5665 last_spill_reg = i;
5667 /* Mark as in use for this insn the reload regs we use
5668 for this. */
5669 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5670 rld[r].when_needed, rld[r].mode);
5672 rld[r].reg_rtx = new;
5673 reload_spill_index[r] = spill_regs[i];
5674 return 1;
5678 /* The reg is not OK. */
5679 if (noerror)
5680 return 0;
5682 failure:
5683 if (asm_noperands (PATTERN (insn)) < 0)
5684 /* It's the compiler's fault. */
5685 fatal_insn ("Could not find a spill register", insn);
5687 /* It's the user's fault; the operand's mode and constraint
5688 don't match. Disable this reload so we don't crash in final. */
5689 error_for_asm (insn,
5690 "`asm' operand constraint incompatible with operand size");
5691 rld[r].in = 0;
5692 rld[r].out = 0;
5693 rld[r].reg_rtx = 0;
5694 rld[r].optional = 1;
5695 rld[r].secondary_p = 1;
5697 return 1;
5700 /* Assign hard reg targets for the pseudo-registers we must reload
5701 into hard regs for this insn.
5702 Also output the instructions to copy them in and out of the hard regs.
5704 For machines with register classes, we are responsible for
5705 finding a reload reg in the proper class. */
5707 static void
5708 choose_reload_regs (chain)
5709 struct insn_chain *chain;
5711 rtx insn = chain->insn;
5712 register int i, j;
5713 int max_group_size = 1;
5714 enum reg_class group_class = NO_REGS;
5715 int inheritance;
5716 int pass;
5718 rtx save_reload_reg_rtx[MAX_RELOADS];
5719 char save_reload_inherited[MAX_RELOADS];
5720 rtx save_reload_inheritance_insn[MAX_RELOADS];
5721 rtx save_reload_override_in[MAX_RELOADS];
5722 int save_reload_spill_index[MAX_RELOADS];
5723 HARD_REG_SET save_reload_reg_used;
5724 HARD_REG_SET save_reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
5725 HARD_REG_SET save_reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
5726 HARD_REG_SET save_reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
5727 HARD_REG_SET save_reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
5728 HARD_REG_SET save_reload_reg_used_in_input[MAX_RECOG_OPERANDS];
5729 HARD_REG_SET save_reload_reg_used_in_output[MAX_RECOG_OPERANDS];
5730 HARD_REG_SET save_reload_reg_used_in_op_addr;
5731 HARD_REG_SET save_reload_reg_used_in_op_addr_reload;
5732 HARD_REG_SET save_reload_reg_used_in_insn;
5733 HARD_REG_SET save_reload_reg_used_in_other_addr;
5734 HARD_REG_SET save_reload_reg_used_at_all;
5736 bzero (reload_inherited, MAX_RELOADS);
5737 bzero ((char *) reload_inheritance_insn, MAX_RELOADS * sizeof (rtx));
5738 bzero ((char *) reload_override_in, MAX_RELOADS * sizeof (rtx));
5740 CLEAR_HARD_REG_SET (reload_reg_used);
5741 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5742 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5743 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5744 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5745 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5747 CLEAR_HARD_REG_SET (reg_used_in_insn);
5749 HARD_REG_SET tmp;
5750 REG_SET_TO_HARD_REG_SET (tmp, chain->live_before);
5751 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5752 REG_SET_TO_HARD_REG_SET (tmp, chain->live_after);
5753 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5754 compute_use_by_pseudos (&reg_used_in_insn, chain->live_before);
5755 compute_use_by_pseudos (&reg_used_in_insn, chain->live_after);
5757 for (i = 0; i < reload_n_operands; i++)
5759 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5760 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5761 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5762 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5763 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5764 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5767 IOR_COMPL_HARD_REG_SET (reload_reg_used, chain->used_spill_regs);
5769 #if 0 /* Not needed, now that we can always retry without inheritance. */
5770 /* See if we have more mandatory reloads than spill regs.
5771 If so, then we cannot risk optimizations that could prevent
5772 reloads from sharing one spill register.
5774 Since we will try finding a better register than reload_reg_rtx
5775 unless it is equal to reload_in or reload_out, count such reloads. */
5778 int tem = 0;
5779 for (j = 0; j < n_reloads; j++)
5780 if (! rld[j].optional
5781 && (rld[j].in != 0 || rld[j].out != 0 || rld[j].secondary_p)
5782 && (rld[j].reg_rtx == 0
5783 || (! rtx_equal_p (rld[j].reg_rtx, rld[j].in)
5784 && ! rtx_equal_p (rld[j].reg_rtx, rld[j].out))))
5785 tem++;
5786 if (tem > n_spills)
5787 must_reuse = 1;
5789 #endif
5791 /* In order to be certain of getting the registers we need,
5792 we must sort the reloads into order of increasing register class.
5793 Then our grabbing of reload registers will parallel the process
5794 that provided the reload registers.
5796 Also note whether any of the reloads wants a consecutive group of regs.
5797 If so, record the maximum size of the group desired and what
5798 register class contains all the groups needed by this insn. */
5800 for (j = 0; j < n_reloads; j++)
5802 reload_order[j] = j;
5803 reload_spill_index[j] = -1;
5805 rld[j].mode = ((rld[j].inmode == VOIDmode
5806 || (GET_MODE_SIZE (rld[j].outmode)
5807 > GET_MODE_SIZE (rld[j].inmode)))
5808 ? rld[j].outmode : rld[j].inmode);
5810 rld[j].nregs = CLASS_MAX_NREGS (rld[j].class, rld[j].mode);
5812 if (rld[j].nregs > 1)
5814 max_group_size = MAX (rld[j].nregs, max_group_size);
5815 group_class = reg_class_superunion[(int)rld[j].class][(int)group_class];
5818 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5819 /* If we have already decided to use a certain register,
5820 don't use it in another way. */
5821 if (rld[j].reg_rtx)
5822 mark_reload_reg_in_use (REGNO (rld[j].reg_rtx), rld[j].opnum,
5823 rld[j].when_needed, rld[j].mode);
5826 if (n_reloads > 1)
5827 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5829 bcopy (reload_inherited, save_reload_inherited, sizeof reload_inherited);
5830 bcopy ((char *) reload_inheritance_insn,
5831 (char *) save_reload_inheritance_insn,
5832 sizeof reload_inheritance_insn);
5833 bcopy ((char *) reload_override_in, (char *) save_reload_override_in,
5834 sizeof reload_override_in);
5835 bcopy ((char *) reload_spill_index, (char *) save_reload_spill_index,
5836 sizeof reload_spill_index);
5837 COPY_HARD_REG_SET (save_reload_reg_used, reload_reg_used);
5838 COPY_HARD_REG_SET (save_reload_reg_used_at_all, reload_reg_used_at_all);
5839 COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr,
5840 reload_reg_used_in_op_addr);
5842 COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr_reload,
5843 reload_reg_used_in_op_addr_reload);
5845 COPY_HARD_REG_SET (save_reload_reg_used_in_insn,
5846 reload_reg_used_in_insn);
5847 COPY_HARD_REG_SET (save_reload_reg_used_in_other_addr,
5848 reload_reg_used_in_other_addr);
5850 for (i = 0; i < reload_n_operands; i++)
5852 COPY_HARD_REG_SET (save_reload_reg_used_in_output[i],
5853 reload_reg_used_in_output[i]);
5854 COPY_HARD_REG_SET (save_reload_reg_used_in_input[i],
5855 reload_reg_used_in_input[i]);
5856 COPY_HARD_REG_SET (save_reload_reg_used_in_input_addr[i],
5857 reload_reg_used_in_input_addr[i]);
5858 COPY_HARD_REG_SET (save_reload_reg_used_in_inpaddr_addr[i],
5859 reload_reg_used_in_inpaddr_addr[i]);
5860 COPY_HARD_REG_SET (save_reload_reg_used_in_output_addr[i],
5861 reload_reg_used_in_output_addr[i]);
5862 COPY_HARD_REG_SET (save_reload_reg_used_in_outaddr_addr[i],
5863 reload_reg_used_in_outaddr_addr[i]);
5866 /* If -O, try first with inheritance, then turning it off.
5867 If not -O, don't do inheritance.
5868 Using inheritance when not optimizing leads to paradoxes
5869 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5870 because one side of the comparison might be inherited. */
5872 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5874 /* Process the reloads in order of preference just found.
5875 Beyond this point, subregs can be found in reload_reg_rtx.
5877 This used to look for an existing reloaded home for all
5878 of the reloads, and only then perform any new reloads.
5879 But that could lose if the reloads were done out of reg-class order
5880 because a later reload with a looser constraint might have an old
5881 home in a register needed by an earlier reload with a tighter constraint.
5883 To solve this, we make two passes over the reloads, in the order
5884 described above. In the first pass we try to inherit a reload
5885 from a previous insn. If there is a later reload that needs a
5886 class that is a proper subset of the class being processed, we must
5887 also allocate a spill register during the first pass.
5889 Then make a second pass over the reloads to allocate any reloads
5890 that haven't been given registers yet. */
5892 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5894 for (j = 0; j < n_reloads; j++)
5896 register int r = reload_order[j];
5897 rtx search_equiv = NULL_RTX;
5899 /* Ignore reloads that got marked inoperative. */
5900 if (rld[r].out == 0 && rld[r].in == 0
5901 && ! rld[r].secondary_p)
5902 continue;
5904 /* If find_reloads chose to use reload_in or reload_out as a reload
5905 register, we don't need to chose one. Otherwise, try even if it
5906 found one since we might save an insn if we find the value lying
5907 around.
5908 Try also when reload_in is a pseudo without a hard reg. */
5909 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5910 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5911 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5912 && GET_CODE (rld[r].in) != MEM
5913 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5914 continue;
5916 #if 0 /* No longer needed for correct operation.
5917 It might give better code, or might not; worth an experiment? */
5918 /* If this is an optional reload, we can't inherit from earlier insns
5919 until we are sure that any non-optional reloads have been allocated.
5920 The following code takes advantage of the fact that optional reloads
5921 are at the end of reload_order. */
5922 if (rld[r].optional != 0)
5923 for (i = 0; i < j; i++)
5924 if ((rld[reload_order[i]].out != 0
5925 || rld[reload_order[i]].in != 0
5926 || rld[reload_order[i]].secondary_p)
5927 && ! rld[reload_order[i]].optional
5928 && rld[reload_order[i]].reg_rtx == 0)
5929 allocate_reload_reg (chain, reload_order[i], 0, inheritance);
5930 #endif
5932 /* First see if this pseudo is already available as reloaded
5933 for a previous insn. We cannot try to inherit for reloads
5934 that are smaller than the maximum number of registers needed
5935 for groups unless the register we would allocate cannot be used
5936 for the groups.
5938 We could check here to see if this is a secondary reload for
5939 an object that is already in a register of the desired class.
5940 This would avoid the need for the secondary reload register.
5941 But this is complex because we can't easily determine what
5942 objects might want to be loaded via this reload. So let a
5943 register be allocated here. In `emit_reload_insns' we suppress
5944 one of the loads in the case described above. */
5946 if (inheritance)
5948 int word = 0;
5949 register int regno = -1;
5950 enum machine_mode mode = VOIDmode;
5952 if (rld[r].in == 0)
5954 else if (GET_CODE (rld[r].in) == REG)
5956 regno = REGNO (rld[r].in);
5957 mode = GET_MODE (rld[r].in);
5959 else if (GET_CODE (rld[r].in_reg) == REG)
5961 regno = REGNO (rld[r].in_reg);
5962 mode = GET_MODE (rld[r].in_reg);
5964 else if (GET_CODE (rld[r].in_reg) == SUBREG
5965 && GET_CODE (SUBREG_REG (rld[r].in_reg)) == REG)
5967 word = SUBREG_WORD (rld[r].in_reg);
5968 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5969 if (regno < FIRST_PSEUDO_REGISTER)
5970 regno += word;
5971 mode = GET_MODE (rld[r].in_reg);
5973 #ifdef AUTO_INC_DEC
5974 else if ((GET_CODE (rld[r].in_reg) == PRE_INC
5975 || GET_CODE (rld[r].in_reg) == PRE_DEC
5976 || GET_CODE (rld[r].in_reg) == POST_INC
5977 || GET_CODE (rld[r].in_reg) == POST_DEC)
5978 && GET_CODE (XEXP (rld[r].in_reg, 0)) == REG)
5980 regno = REGNO (XEXP (rld[r].in_reg, 0));
5981 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5982 rld[r].out = rld[r].in;
5984 #endif
5985 #if 0
5986 /* This won't work, since REGNO can be a pseudo reg number.
5987 Also, it takes much more hair to keep track of all the things
5988 that can invalidate an inherited reload of part of a pseudoreg. */
5989 else if (GET_CODE (rld[r].in) == SUBREG
5990 && GET_CODE (SUBREG_REG (rld[r].in)) == REG)
5991 regno = REGNO (SUBREG_REG (rld[r].in)) + SUBREG_WORD (rld[r].in);
5992 #endif
5994 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5996 enum reg_class class = rld[r].class, last_class;
5997 rtx last_reg = reg_last_reload_reg[regno];
5999 i = REGNO (last_reg) + word;
6000 last_class = REGNO_REG_CLASS (i);
6001 if ((GET_MODE_SIZE (GET_MODE (last_reg))
6002 >= GET_MODE_SIZE (mode) + word * UNITS_PER_WORD)
6003 && reg_reloaded_contents[i] == regno
6004 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
6005 && HARD_REGNO_MODE_OK (i, rld[r].mode)
6006 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
6007 /* Even if we can't use this register as a reload
6008 register, we might use it for reload_override_in,
6009 if copying it to the desired class is cheap
6010 enough. */
6011 || ((REGISTER_MOVE_COST (last_class, class)
6012 < MEMORY_MOVE_COST (mode, class, 1))
6013 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6014 && (SECONDARY_INPUT_RELOAD_CLASS (class, mode,
6015 last_reg)
6016 == NO_REGS)
6017 #endif
6018 #ifdef SECONDARY_MEMORY_NEEDED
6019 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
6020 mode)
6021 #endif
6024 && (rld[r].nregs == max_group_size
6025 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
6027 && reload_reg_free_for_value_p (i, rld[r].opnum,
6028 rld[r].when_needed,
6029 rld[r].in,
6030 const0_rtx, r, 1))
6032 /* If a group is needed, verify that all the subsequent
6033 registers still have their values intact. */
6034 int nr
6035 = HARD_REGNO_NREGS (i, rld[r].mode);
6036 int k;
6038 for (k = 1; k < nr; k++)
6039 if (reg_reloaded_contents[i + k] != regno
6040 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
6041 break;
6043 if (k == nr)
6045 int i1;
6047 last_reg = (GET_MODE (last_reg) == mode
6048 ? last_reg : gen_rtx_REG (mode, i));
6050 /* We found a register that contains the
6051 value we need. If this register is the
6052 same as an `earlyclobber' operand of the
6053 current insn, just mark it as a place to
6054 reload from since we can't use it as the
6055 reload register itself. */
6057 for (i1 = 0; i1 < n_earlyclobbers; i1++)
6058 if (reg_overlap_mentioned_for_reload_p
6059 (reg_last_reload_reg[regno],
6060 reload_earlyclobbers[i1]))
6061 break;
6063 if (i1 != n_earlyclobbers
6064 || ! (reload_reg_free_for_value_p
6065 (i, rld[r].opnum, rld[r].when_needed,
6066 rld[r].in, rld[r].out, r, 1))
6067 /* Don't use it if we'd clobber a pseudo reg. */
6068 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
6069 && rld[r].out
6070 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
6071 /* Don't clobber the frame pointer. */
6072 || (i == HARD_FRAME_POINTER_REGNUM
6073 && rld[r].out)
6074 /* Don't really use the inherited spill reg
6075 if we need it wider than we've got it. */
6076 || (GET_MODE_SIZE (rld[r].mode)
6077 > GET_MODE_SIZE (mode))
6078 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
6081 /* If find_reloads chose reload_out as reload
6082 register, stay with it - that leaves the
6083 inherited register for subsequent reloads. */
6084 || (rld[r].out && rld[r].reg_rtx
6085 && rtx_equal_p (rld[r].out,
6086 rld[r].reg_rtx)))
6088 reload_override_in[r] = last_reg;
6089 reload_inheritance_insn[r]
6090 = reg_reloaded_insn[i];
6092 else
6094 int k;
6095 /* We can use this as a reload reg. */
6096 /* Mark the register as in use for this part of
6097 the insn. */
6098 mark_reload_reg_in_use (i,
6099 rld[r].opnum,
6100 rld[r].when_needed,
6101 rld[r].mode);
6102 rld[r].reg_rtx = last_reg;
6103 reload_inherited[r] = 1;
6104 reload_inheritance_insn[r]
6105 = reg_reloaded_insn[i];
6106 reload_spill_index[r] = i;
6107 for (k = 0; k < nr; k++)
6108 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6109 i + k);
6116 /* Here's another way to see if the value is already lying around. */
6117 if (inheritance
6118 && rld[r].in != 0
6119 && ! reload_inherited[r]
6120 && rld[r].out == 0
6121 && (CONSTANT_P (rld[r].in)
6122 || GET_CODE (rld[r].in) == PLUS
6123 || GET_CODE (rld[r].in) == REG
6124 || GET_CODE (rld[r].in) == MEM)
6125 && (rld[r].nregs == max_group_size
6126 || ! reg_classes_intersect_p (rld[r].class, group_class)))
6127 search_equiv = rld[r].in;
6128 /* If this is an output reload from a simple move insn, look
6129 if an equivalence for the input is available. */
6130 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
6132 rtx set = single_set (insn);
6134 if (set
6135 && rtx_equal_p (rld[r].out, SET_DEST (set))
6136 && CONSTANT_P (SET_SRC (set)))
6137 search_equiv = SET_SRC (set);
6140 if (search_equiv)
6142 register rtx equiv
6143 = find_equiv_reg (search_equiv, insn, rld[r].class,
6144 -1, NULL_PTR, 0, rld[r].mode);
6145 int regno = 0;
6147 if (equiv != 0)
6149 if (GET_CODE (equiv) == REG)
6150 regno = REGNO (equiv);
6151 else if (GET_CODE (equiv) == SUBREG)
6153 /* This must be a SUBREG of a hard register.
6154 Make a new REG since this might be used in an
6155 address and not all machines support SUBREGs
6156 there. */
6157 regno = REGNO (SUBREG_REG (equiv)) + SUBREG_WORD (equiv);
6158 equiv = gen_rtx_REG (rld[r].mode, regno);
6160 else
6161 abort ();
6164 /* If we found a spill reg, reject it unless it is free
6165 and of the desired class. */
6166 if (equiv != 0
6167 && ((TEST_HARD_REG_BIT (reload_reg_used_at_all, regno)
6168 && ! reload_reg_free_for_value_p (regno, rld[r].opnum,
6169 rld[r].when_needed,
6170 rld[r].in,
6171 rld[r].out, r, 1))
6172 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
6173 regno)))
6174 equiv = 0;
6176 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
6177 equiv = 0;
6179 /* We found a register that contains the value we need.
6180 If this register is the same as an `earlyclobber' operand
6181 of the current insn, just mark it as a place to reload from
6182 since we can't use it as the reload register itself. */
6184 if (equiv != 0)
6185 for (i = 0; i < n_earlyclobbers; i++)
6186 if (reg_overlap_mentioned_for_reload_p (equiv,
6187 reload_earlyclobbers[i]))
6189 reload_override_in[r] = equiv;
6190 equiv = 0;
6191 break;
6194 /* If the equiv register we have found is explicitly clobbered
6195 in the current insn, it depends on the reload type if we
6196 can use it, use it for reload_override_in, or not at all.
6197 In particular, we then can't use EQUIV for a
6198 RELOAD_FOR_OUTPUT_ADDRESS reload. */
6200 if (equiv != 0 && regno_clobbered_p (regno, insn))
6202 switch (rld[r].when_needed)
6204 case RELOAD_FOR_OTHER_ADDRESS:
6205 case RELOAD_FOR_INPADDR_ADDRESS:
6206 case RELOAD_FOR_INPUT_ADDRESS:
6207 case RELOAD_FOR_OPADDR_ADDR:
6208 break;
6209 case RELOAD_OTHER:
6210 case RELOAD_FOR_INPUT:
6211 case RELOAD_FOR_OPERAND_ADDRESS:
6212 reload_override_in[r] = equiv;
6213 /* Fall through. */
6214 default:
6215 equiv = 0;
6216 break;
6220 /* If we found an equivalent reg, say no code need be generated
6221 to load it, and use it as our reload reg. */
6222 if (equiv != 0 && regno != HARD_FRAME_POINTER_REGNUM)
6224 int nr = HARD_REGNO_NREGS (regno, rld[r].mode);
6225 int k;
6226 rld[r].reg_rtx = equiv;
6227 reload_inherited[r] = 1;
6229 /* If reg_reloaded_valid is not set for this register,
6230 there might be a stale spill_reg_store lying around.
6231 We must clear it, since otherwise emit_reload_insns
6232 might delete the store. */
6233 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
6234 spill_reg_store[regno] = NULL_RTX;
6235 /* If any of the hard registers in EQUIV are spill
6236 registers, mark them as in use for this insn. */
6237 for (k = 0; k < nr; k++)
6239 i = spill_reg_order[regno + k];
6240 if (i >= 0)
6242 mark_reload_reg_in_use (regno, rld[r].opnum,
6243 rld[r].when_needed,
6244 rld[r].mode);
6245 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6246 regno + k);
6252 /* If we found a register to use already, or if this is an optional
6253 reload, we are done. */
6254 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
6255 continue;
6257 #if 0 /* No longer needed for correct operation. Might or might not
6258 give better code on the average. Want to experiment? */
6260 /* See if there is a later reload that has a class different from our
6261 class that intersects our class or that requires less register
6262 than our reload. If so, we must allocate a register to this
6263 reload now, since that reload might inherit a previous reload
6264 and take the only available register in our class. Don't do this
6265 for optional reloads since they will force all previous reloads
6266 to be allocated. Also don't do this for reloads that have been
6267 turned off. */
6269 for (i = j + 1; i < n_reloads; i++)
6271 int s = reload_order[i];
6273 if ((rld[s].in == 0 && rld[s].out == 0
6274 && ! rld[s].secondary_p)
6275 || rld[s].optional)
6276 continue;
6278 if ((rld[s].class != rld[r].class
6279 && reg_classes_intersect_p (rld[r].class,
6280 rld[s].class))
6281 || rld[s].nregs < rld[r].nregs)
6282 break;
6285 if (i == n_reloads)
6286 continue;
6288 allocate_reload_reg (chain, r, j == n_reloads - 1, inheritance);
6289 #endif
6292 /* Now allocate reload registers for anything non-optional that
6293 didn't get one yet. */
6294 for (j = 0; j < n_reloads; j++)
6296 register int r = reload_order[j];
6298 /* Ignore reloads that got marked inoperative. */
6299 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
6300 continue;
6302 /* Skip reloads that already have a register allocated or are
6303 optional. */
6304 if (rld[r].reg_rtx != 0 || rld[r].optional)
6305 continue;
6307 if (! allocate_reload_reg (chain, r, j == n_reloads - 1, inheritance))
6308 break;
6311 /* If that loop got all the way, we have won. */
6312 if (j == n_reloads)
6313 break;
6315 /* Loop around and try without any inheritance. */
6316 /* First undo everything done by the failed attempt
6317 to allocate with inheritance. */
6318 for (i = 0; i < n_reloads; i++)
6319 rld[i].reg_rtx = save_reload_reg_rtx[i];
6320 bcopy ((char *) save_reload_inherited, (char *) reload_inherited,
6321 sizeof reload_inherited);
6322 bcopy ((char *) save_reload_inheritance_insn,
6323 (char *) reload_inheritance_insn,
6324 sizeof reload_inheritance_insn);
6325 bcopy ((char *) save_reload_override_in, (char *) reload_override_in,
6326 sizeof reload_override_in);
6327 bcopy ((char *) save_reload_spill_index, (char *) reload_spill_index,
6328 sizeof reload_spill_index);
6329 COPY_HARD_REG_SET (reload_reg_used, save_reload_reg_used);
6330 COPY_HARD_REG_SET (reload_reg_used_at_all, save_reload_reg_used_at_all);
6331 COPY_HARD_REG_SET (reload_reg_used_in_op_addr,
6332 save_reload_reg_used_in_op_addr);
6333 COPY_HARD_REG_SET (reload_reg_used_in_op_addr_reload,
6334 save_reload_reg_used_in_op_addr_reload);
6335 COPY_HARD_REG_SET (reload_reg_used_in_insn,
6336 save_reload_reg_used_in_insn);
6337 COPY_HARD_REG_SET (reload_reg_used_in_other_addr,
6338 save_reload_reg_used_in_other_addr);
6340 for (i = 0; i < reload_n_operands; i++)
6342 COPY_HARD_REG_SET (reload_reg_used_in_input[i],
6343 save_reload_reg_used_in_input[i]);
6344 COPY_HARD_REG_SET (reload_reg_used_in_output[i],
6345 save_reload_reg_used_in_output[i]);
6346 COPY_HARD_REG_SET (reload_reg_used_in_input_addr[i],
6347 save_reload_reg_used_in_input_addr[i]);
6348 COPY_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i],
6349 save_reload_reg_used_in_inpaddr_addr[i]);
6350 COPY_HARD_REG_SET (reload_reg_used_in_output_addr[i],
6351 save_reload_reg_used_in_output_addr[i]);
6352 COPY_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i],
6353 save_reload_reg_used_in_outaddr_addr[i]);
6357 /* If we thought we could inherit a reload, because it seemed that
6358 nothing else wanted the same reload register earlier in the insn,
6359 verify that assumption, now that all reloads have been assigned.
6360 Likewise for reloads where reload_override_in has been set. */
6362 /* If doing expensive optimizations, do one preliminary pass that doesn't
6363 cancel any inheritance, but removes reloads that have been needed only
6364 for reloads that we know can be inherited. */
6365 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
6367 for (j = 0; j < n_reloads; j++)
6369 register int r = reload_order[j];
6370 rtx check_reg;
6371 if (reload_inherited[r] && rld[r].reg_rtx)
6372 check_reg = rld[r].reg_rtx;
6373 else if (reload_override_in[r]
6374 && (GET_CODE (reload_override_in[r]) == REG
6375 || GET_CODE (reload_override_in[r]) == SUBREG))
6376 check_reg = reload_override_in[r];
6377 else
6378 continue;
6379 if (! reload_reg_free_for_value_p (true_regnum (check_reg),
6380 rld[r].opnum,
6381 rld[r].when_needed,
6382 rld[r].in,
6383 (reload_inherited[r]
6384 ? rld[r].out : const0_rtx),
6385 r, 1))
6387 if (pass)
6388 continue;
6389 reload_inherited[r] = 0;
6390 reload_override_in[r] = 0;
6392 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
6393 reload_override_in, then we do not need its related
6394 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
6395 likewise for other reload types.
6396 We handle this by removing a reload when its only replacement
6397 is mentioned in reload_in of the reload we are going to inherit.
6398 A special case are auto_inc expressions; even if the input is
6399 inherited, we still need the address for the output. We can
6400 recognize them because they have RELOAD_OUT set to RELOAD_IN.
6401 If we suceeded removing some reload and we are doing a preliminary
6402 pass just to remove such reloads, make another pass, since the
6403 removal of one reload might allow us to inherit another one. */
6404 else if (rld[r].in
6405 && rld[r].out != rld[r].in
6406 && remove_address_replacements (rld[r].in) && pass)
6407 pass = 2;
6411 /* Now that reload_override_in is known valid,
6412 actually override reload_in. */
6413 for (j = 0; j < n_reloads; j++)
6414 if (reload_override_in[j])
6415 rld[j].in = reload_override_in[j];
6417 /* If this reload won't be done because it has been cancelled or is
6418 optional and not inherited, clear reload_reg_rtx so other
6419 routines (such as subst_reloads) don't get confused. */
6420 for (j = 0; j < n_reloads; j++)
6421 if (rld[j].reg_rtx != 0
6422 && ((rld[j].optional && ! reload_inherited[j])
6423 || (rld[j].in == 0 && rld[j].out == 0
6424 && ! rld[j].secondary_p)))
6426 int regno = true_regnum (rld[j].reg_rtx);
6428 if (spill_reg_order[regno] >= 0)
6429 clear_reload_reg_in_use (regno, rld[j].opnum,
6430 rld[j].when_needed, rld[j].mode);
6431 rld[j].reg_rtx = 0;
6434 /* Record which pseudos and which spill regs have output reloads. */
6435 for (j = 0; j < n_reloads; j++)
6437 register int r = reload_order[j];
6439 i = reload_spill_index[r];
6441 /* I is nonneg if this reload uses a register.
6442 If rld[r].reg_rtx is 0, this is an optional reload
6443 that we opted to ignore. */
6444 if (rld[r].out_reg != 0 && GET_CODE (rld[r].out_reg) == REG
6445 && rld[r].reg_rtx != 0)
6447 register int nregno = REGNO (rld[r].out_reg);
6448 int nr = 1;
6450 if (nregno < FIRST_PSEUDO_REGISTER)
6451 nr = HARD_REGNO_NREGS (nregno, rld[r].mode);
6453 while (--nr >= 0)
6454 reg_has_output_reload[nregno + nr] = 1;
6456 if (i >= 0)
6458 nr = HARD_REGNO_NREGS (i, rld[r].mode);
6459 while (--nr >= 0)
6460 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
6463 if (rld[r].when_needed != RELOAD_OTHER
6464 && rld[r].when_needed != RELOAD_FOR_OUTPUT
6465 && rld[r].when_needed != RELOAD_FOR_INSN)
6466 abort ();
6471 /* Deallocate the reload register for reload R. This is called from
6472 remove_address_replacements. */
6473 void
6474 deallocate_reload_reg (r)
6475 int r;
6477 int regno;
6479 if (! rld[r].reg_rtx)
6480 return;
6481 regno = true_regnum (rld[r].reg_rtx);
6482 rld[r].reg_rtx = 0;
6483 if (spill_reg_order[regno] >= 0)
6484 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
6485 rld[r].mode);
6486 reload_spill_index[r] = -1;
6489 /* If SMALL_REGISTER_CLASSES is non-zero, we may not have merged two
6490 reloads of the same item for fear that we might not have enough reload
6491 registers. However, normally they will get the same reload register
6492 and hence actually need not be loaded twice.
6494 Here we check for the most common case of this phenomenon: when we have
6495 a number of reloads for the same object, each of which were allocated
6496 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6497 reload, and is not modified in the insn itself. If we find such,
6498 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6499 This will not increase the number of spill registers needed and will
6500 prevent redundant code. */
6502 static void
6503 merge_assigned_reloads (insn)
6504 rtx insn;
6506 int i, j;
6508 /* Scan all the reloads looking for ones that only load values and
6509 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6510 assigned and not modified by INSN. */
6512 for (i = 0; i < n_reloads; i++)
6514 int conflicting_input = 0;
6515 int max_input_address_opnum = -1;
6516 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6518 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6519 || rld[i].out != 0 || rld[i].reg_rtx == 0
6520 || reg_set_p (rld[i].reg_rtx, insn))
6521 continue;
6523 /* Look at all other reloads. Ensure that the only use of this
6524 reload_reg_rtx is in a reload that just loads the same value
6525 as we do. Note that any secondary reloads must be of the identical
6526 class since the values, modes, and result registers are the
6527 same, so we need not do anything with any secondary reloads. */
6529 for (j = 0; j < n_reloads; j++)
6531 if (i == j || rld[j].reg_rtx == 0
6532 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6533 rld[i].reg_rtx))
6534 continue;
6536 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6537 && rld[j].opnum > max_input_address_opnum)
6538 max_input_address_opnum = rld[j].opnum;
6540 /* If the reload regs aren't exactly the same (e.g, different modes)
6541 or if the values are different, we can't merge this reload.
6542 But if it is an input reload, we might still merge
6543 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6545 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6546 || rld[j].out != 0 || rld[j].in == 0
6547 || ! rtx_equal_p (rld[i].in, rld[j].in))
6549 if (rld[j].when_needed != RELOAD_FOR_INPUT
6550 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6551 || rld[i].opnum > rld[j].opnum)
6552 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6553 break;
6554 conflicting_input = 1;
6555 if (min_conflicting_input_opnum > rld[j].opnum)
6556 min_conflicting_input_opnum = rld[j].opnum;
6560 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6561 we, in fact, found any matching reloads. */
6563 if (j == n_reloads
6564 && max_input_address_opnum <= min_conflicting_input_opnum)
6566 for (j = 0; j < n_reloads; j++)
6567 if (i != j && rld[j].reg_rtx != 0
6568 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6569 && (! conflicting_input
6570 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6571 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6573 rld[i].when_needed = RELOAD_OTHER;
6574 rld[j].in = 0;
6575 reload_spill_index[j] = -1;
6576 transfer_replacements (i, j);
6579 /* If this is now RELOAD_OTHER, look for any reloads that load
6580 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6581 if they were for inputs, RELOAD_OTHER for outputs. Note that
6582 this test is equivalent to looking for reloads for this operand
6583 number. */
6585 if (rld[i].when_needed == RELOAD_OTHER)
6586 for (j = 0; j < n_reloads; j++)
6587 if (rld[j].in != 0
6588 && rld[i].when_needed != RELOAD_OTHER
6589 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6590 rld[i].in))
6591 rld[j].when_needed
6592 = ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
6593 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6594 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6600 /* Output insns to reload values in and out of the chosen reload regs. */
6602 static void
6603 emit_reload_insns (chain)
6604 struct insn_chain *chain;
6606 rtx insn = chain->insn;
6608 register int j;
6609 rtx input_reload_insns[MAX_RECOG_OPERANDS];
6610 rtx other_input_address_reload_insns = 0;
6611 rtx other_input_reload_insns = 0;
6612 rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6613 rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6614 rtx output_reload_insns[MAX_RECOG_OPERANDS];
6615 rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6616 rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6617 rtx operand_reload_insns = 0;
6618 rtx other_operand_reload_insns = 0;
6619 rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6620 rtx following_insn = NEXT_INSN (insn);
6621 rtx before_insn = PREV_INSN (insn);
6622 int special;
6623 /* Values to be put in spill_reg_store are put here first. */
6624 rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6625 HARD_REG_SET reg_reloaded_died;
6627 CLEAR_HARD_REG_SET (reg_reloaded_died);
6629 for (j = 0; j < reload_n_operands; j++)
6630 input_reload_insns[j] = input_address_reload_insns[j]
6631 = inpaddr_address_reload_insns[j]
6632 = output_reload_insns[j] = output_address_reload_insns[j]
6633 = outaddr_address_reload_insns[j]
6634 = other_output_reload_insns[j] = 0;
6636 /* Now output the instructions to copy the data into and out of the
6637 reload registers. Do these in the order that the reloads were reported,
6638 since reloads of base and index registers precede reloads of operands
6639 and the operands may need the base and index registers reloaded. */
6641 for (j = 0; j < n_reloads; j++)
6643 register rtx old;
6644 rtx oldequiv_reg = 0;
6645 rtx this_reload_insn = 0;
6646 int expect_occurrences = 1;
6648 if (rld[j].reg_rtx
6649 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
6650 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
6652 old = (rld[j].in && GET_CODE (rld[j].in) == MEM
6653 ? rld[j].in_reg : rld[j].in);
6655 if (old != 0
6656 /* AUTO_INC reloads need to be handled even if inherited. We got an
6657 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
6658 && (! reload_inherited[j] || (rld[j].out && ! rld[j].out_reg))
6659 && ! rtx_equal_p (rld[j].reg_rtx, old)
6660 && rld[j].reg_rtx != 0)
6662 register rtx reloadreg = rld[j].reg_rtx;
6663 rtx oldequiv = 0;
6664 enum machine_mode mode;
6665 rtx *where;
6667 /* Determine the mode to reload in.
6668 This is very tricky because we have three to choose from.
6669 There is the mode the insn operand wants (rld[J].inmode).
6670 There is the mode of the reload register RELOADREG.
6671 There is the intrinsic mode of the operand, which we could find
6672 by stripping some SUBREGs.
6673 It turns out that RELOADREG's mode is irrelevant:
6674 we can change that arbitrarily.
6676 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6677 then the reload reg may not support QImode moves, so use SImode.
6678 If foo is in memory due to spilling a pseudo reg, this is safe,
6679 because the QImode value is in the least significant part of a
6680 slot big enough for a SImode. If foo is some other sort of
6681 memory reference, then it is impossible to reload this case,
6682 so previous passes had better make sure this never happens.
6684 Then consider a one-word union which has SImode and one of its
6685 members is a float, being fetched as (SUBREG:SF union:SI).
6686 We must fetch that as SFmode because we could be loading into
6687 a float-only register. In this case OLD's mode is correct.
6689 Consider an immediate integer: it has VOIDmode. Here we need
6690 to get a mode from something else.
6692 In some cases, there is a fourth mode, the operand's
6693 containing mode. If the insn specifies a containing mode for
6694 this operand, it overrides all others.
6696 I am not sure whether the algorithm here is always right,
6697 but it does the right things in those cases. */
6699 mode = GET_MODE (old);
6700 if (mode == VOIDmode)
6701 mode = rld[j].inmode;
6703 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6704 /* If we need a secondary register for this operation, see if
6705 the value is already in a register in that class. Don't
6706 do this if the secondary register will be used as a scratch
6707 register. */
6709 if (rld[j].secondary_in_reload >= 0
6710 && rld[j].secondary_in_icode == CODE_FOR_nothing
6711 && optimize)
6712 oldequiv
6713 = find_equiv_reg (old, insn,
6714 rld[rld[j].secondary_in_reload].class,
6715 -1, NULL_PTR, 0, mode);
6716 #endif
6718 /* If reloading from memory, see if there is a register
6719 that already holds the same value. If so, reload from there.
6720 We can pass 0 as the reload_reg_p argument because
6721 any other reload has either already been emitted,
6722 in which case find_equiv_reg will see the reload-insn,
6723 or has yet to be emitted, in which case it doesn't matter
6724 because we will use this equiv reg right away. */
6726 if (oldequiv == 0 && optimize
6727 && (GET_CODE (old) == MEM
6728 || (GET_CODE (old) == REG
6729 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6730 && reg_renumber[REGNO (old)] < 0)))
6731 oldequiv = find_equiv_reg (old, insn, ALL_REGS,
6732 -1, NULL_PTR, 0, mode);
6734 if (oldequiv)
6736 int regno = true_regnum (oldequiv);
6738 /* Don't use OLDEQUIV if any other reload changes it at an
6739 earlier stage of this insn or at this stage. */
6740 if (! reload_reg_free_for_value_p (regno, rld[j].opnum,
6741 rld[j].when_needed,
6742 rld[j].in, const0_rtx, j,
6744 oldequiv = 0;
6746 /* If it is no cheaper to copy from OLDEQUIV into the
6747 reload register than it would be to move from memory,
6748 don't use it. Likewise, if we need a secondary register
6749 or memory. */
6751 if (oldequiv != 0
6752 && ((REGNO_REG_CLASS (regno) != rld[j].class
6753 && (REGISTER_MOVE_COST (REGNO_REG_CLASS (regno),
6754 rld[j].class)
6755 >= MEMORY_MOVE_COST (mode, rld[j].class, 1)))
6756 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6757 || (SECONDARY_INPUT_RELOAD_CLASS (rld[j].class,
6758 mode, oldequiv)
6759 != NO_REGS)
6760 #endif
6761 #ifdef SECONDARY_MEMORY_NEEDED
6762 || SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (regno),
6763 rld[j].class,
6764 mode)
6765 #endif
6767 oldequiv = 0;
6770 /* delete_output_reload is only invoked properly if old contains
6771 the original pseudo register. Since this is replaced with a
6772 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6773 find the pseudo in RELOAD_IN_REG. */
6774 if (oldequiv == 0
6775 && reload_override_in[j]
6776 && GET_CODE (rld[j].in_reg) == REG)
6778 oldequiv = old;
6779 old = rld[j].in_reg;
6781 if (oldequiv == 0)
6782 oldequiv = old;
6783 else if (GET_CODE (oldequiv) == REG)
6784 oldequiv_reg = oldequiv;
6785 else if (GET_CODE (oldequiv) == SUBREG)
6786 oldequiv_reg = SUBREG_REG (oldequiv);
6788 /* If we are reloading from a register that was recently stored in
6789 with an output-reload, see if we can prove there was
6790 actually no need to store the old value in it. */
6792 if (optimize && GET_CODE (oldequiv) == REG
6793 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6794 && spill_reg_store[REGNO (oldequiv)]
6795 && GET_CODE (old) == REG
6796 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6797 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6798 rld[j].out_reg)))
6799 delete_output_reload (insn, j, REGNO (oldequiv));
6801 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6802 then load RELOADREG from OLDEQUIV. Note that we cannot use
6803 gen_lowpart_common since it can do the wrong thing when
6804 RELOADREG has a multi-word mode. Note that RELOADREG
6805 must always be a REG here. */
6807 if (GET_MODE (reloadreg) != mode)
6808 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6809 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6810 oldequiv = SUBREG_REG (oldequiv);
6811 if (GET_MODE (oldequiv) != VOIDmode
6812 && mode != GET_MODE (oldequiv))
6813 oldequiv = gen_rtx_SUBREG (mode, oldequiv, 0);
6815 /* Switch to the right place to emit the reload insns. */
6816 switch (rld[j].when_needed)
6818 case RELOAD_OTHER:
6819 where = &other_input_reload_insns;
6820 break;
6821 case RELOAD_FOR_INPUT:
6822 where = &input_reload_insns[rld[j].opnum];
6823 break;
6824 case RELOAD_FOR_INPUT_ADDRESS:
6825 where = &input_address_reload_insns[rld[j].opnum];
6826 break;
6827 case RELOAD_FOR_INPADDR_ADDRESS:
6828 where = &inpaddr_address_reload_insns[rld[j].opnum];
6829 break;
6830 case RELOAD_FOR_OUTPUT_ADDRESS:
6831 where = &output_address_reload_insns[rld[j].opnum];
6832 break;
6833 case RELOAD_FOR_OUTADDR_ADDRESS:
6834 where = &outaddr_address_reload_insns[rld[j].opnum];
6835 break;
6836 case RELOAD_FOR_OPERAND_ADDRESS:
6837 where = &operand_reload_insns;
6838 break;
6839 case RELOAD_FOR_OPADDR_ADDR:
6840 where = &other_operand_reload_insns;
6841 break;
6842 case RELOAD_FOR_OTHER_ADDRESS:
6843 where = &other_input_address_reload_insns;
6844 break;
6845 default:
6846 abort ();
6849 push_to_sequence (*where);
6850 special = 0;
6852 /* Auto-increment addresses must be reloaded in a special way. */
6853 if (rld[j].out && ! rld[j].out_reg)
6855 /* We are not going to bother supporting the case where a
6856 incremented register can't be copied directly from
6857 OLDEQUIV since this seems highly unlikely. */
6858 if (rld[j].secondary_in_reload >= 0)
6859 abort ();
6861 if (reload_inherited[j])
6862 oldequiv = reloadreg;
6864 old = XEXP (rld[j].in_reg, 0);
6866 if (optimize && GET_CODE (oldequiv) == REG
6867 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6868 && spill_reg_store[REGNO (oldequiv)]
6869 && GET_CODE (old) == REG
6870 && (dead_or_set_p (insn,
6871 spill_reg_stored_to[REGNO (oldequiv)])
6872 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6873 old)))
6874 delete_output_reload (insn, j, REGNO (oldequiv));
6876 /* Prevent normal processing of this reload. */
6877 special = 1;
6878 /* Output a special code sequence for this case. */
6879 new_spill_reg_store[REGNO (reloadreg)]
6880 = inc_for_reload (reloadreg, oldequiv, rld[j].out,
6881 rld[j].inc);
6884 /* If we are reloading a pseudo-register that was set by the previous
6885 insn, see if we can get rid of that pseudo-register entirely
6886 by redirecting the previous insn into our reload register. */
6888 else if (optimize && GET_CODE (old) == REG
6889 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6890 && dead_or_set_p (insn, old)
6891 /* This is unsafe if some other reload
6892 uses the same reg first. */
6893 && reload_reg_free_for_value_p (REGNO (reloadreg),
6894 rld[j].opnum,
6895 rld[j].when_needed,
6896 old, rld[j].out,
6897 j, 0))
6899 rtx temp = PREV_INSN (insn);
6900 while (temp && GET_CODE (temp) == NOTE)
6901 temp = PREV_INSN (temp);
6902 if (temp
6903 && GET_CODE (temp) == INSN
6904 && GET_CODE (PATTERN (temp)) == SET
6905 && SET_DEST (PATTERN (temp)) == old
6906 /* Make sure we can access insn_operand_constraint. */
6907 && asm_noperands (PATTERN (temp)) < 0
6908 /* This is unsafe if prev insn rejects our reload reg. */
6909 && constraint_accepts_reg_p (insn_data[recog_memoized (temp)].operand[0].constraint,
6910 reloadreg)
6911 /* This is unsafe if operand occurs more than once in current
6912 insn. Perhaps some occurrences aren't reloaded. */
6913 && count_occurrences (PATTERN (insn), old) == 1
6914 /* Don't risk splitting a matching pair of operands. */
6915 && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
6917 /* Store into the reload register instead of the pseudo. */
6918 SET_DEST (PATTERN (temp)) = reloadreg;
6920 /* If the previous insn is an output reload, the source is
6921 a reload register, and its spill_reg_store entry will
6922 contain the previous destination. This is now
6923 invalid. */
6924 if (GET_CODE (SET_SRC (PATTERN (temp))) == REG
6925 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6927 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6928 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6931 /* If these are the only uses of the pseudo reg,
6932 pretend for GDB it lives in the reload reg we used. */
6933 if (REG_N_DEATHS (REGNO (old)) == 1
6934 && REG_N_SETS (REGNO (old)) == 1)
6936 reg_renumber[REGNO (old)] = REGNO (rld[j].reg_rtx);
6937 alter_reg (REGNO (old), -1);
6939 special = 1;
6943 /* We can't do that, so output an insn to load RELOADREG. */
6945 if (! special)
6947 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6948 rtx second_reload_reg = 0;
6949 enum insn_code icode;
6951 /* If we have a secondary reload, pick up the secondary register
6952 and icode, if any. If OLDEQUIV and OLD are different or
6953 if this is an in-out reload, recompute whether or not we
6954 still need a secondary register and what the icode should
6955 be. If we still need a secondary register and the class or
6956 icode is different, go back to reloading from OLD if using
6957 OLDEQUIV means that we got the wrong type of register. We
6958 cannot have different class or icode due to an in-out reload
6959 because we don't make such reloads when both the input and
6960 output need secondary reload registers. */
6962 if (rld[j].secondary_in_reload >= 0)
6964 int secondary_reload = rld[j].secondary_in_reload;
6965 rtx real_oldequiv = oldequiv;
6966 rtx real_old = old;
6967 rtx tmp;
6969 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6970 and similarly for OLD.
6971 See comments in get_secondary_reload in reload.c. */
6972 /* If it is a pseudo that cannot be replaced with its
6973 equivalent MEM, we must fall back to reload_in, which
6974 will have all the necessary substitutions registered.
6975 Likewise for a pseudo that can't be replaced with its
6976 equivalent constant.
6978 Take extra care for subregs of such pseudos. Note that
6979 we cannot use reg_equiv_mem in this case because it is
6980 not in the right mode. */
6982 tmp = oldequiv;
6983 if (GET_CODE (tmp) == SUBREG)
6984 tmp = SUBREG_REG (tmp);
6985 if (GET_CODE (tmp) == REG
6986 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6987 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6988 || reg_equiv_constant[REGNO (tmp)] != 0))
6990 if (! reg_equiv_mem[REGNO (tmp)]
6991 || num_not_at_initial_offset
6992 || GET_CODE (oldequiv) == SUBREG)
6993 real_oldequiv = rld[j].in;
6994 else
6995 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6998 tmp = old;
6999 if (GET_CODE (tmp) == SUBREG)
7000 tmp = SUBREG_REG (tmp);
7001 if (GET_CODE (tmp) == REG
7002 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
7003 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
7004 || reg_equiv_constant[REGNO (tmp)] != 0))
7006 if (! reg_equiv_mem[REGNO (tmp)]
7007 || num_not_at_initial_offset
7008 || GET_CODE (old) == SUBREG)
7009 real_old = rld[j].in;
7010 else
7011 real_old = reg_equiv_mem[REGNO (tmp)];
7014 second_reload_reg = rld[secondary_reload].reg_rtx;
7015 icode = rld[j].secondary_in_icode;
7017 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
7018 || (rld[j].in != 0 && rld[j].out != 0))
7020 enum reg_class new_class
7021 = SECONDARY_INPUT_RELOAD_CLASS (rld[j].class,
7022 mode, real_oldequiv);
7024 if (new_class == NO_REGS)
7025 second_reload_reg = 0;
7026 else
7028 enum insn_code new_icode;
7029 enum machine_mode new_mode;
7031 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
7032 REGNO (second_reload_reg)))
7033 oldequiv = old, real_oldequiv = real_old;
7034 else
7036 new_icode = reload_in_optab[(int) mode];
7037 if (new_icode != CODE_FOR_nothing
7038 && ((insn_data[(int) new_icode].operand[0].predicate
7039 && ! ((*insn_data[(int) new_icode].operand[0].predicate)
7040 (reloadreg, mode)))
7041 || (insn_data[(int) new_icode].operand[1].predicate
7042 && ! ((*insn_data[(int) new_icode].operand[1].predicate)
7043 (real_oldequiv, mode)))))
7044 new_icode = CODE_FOR_nothing;
7046 if (new_icode == CODE_FOR_nothing)
7047 new_mode = mode;
7048 else
7049 new_mode = insn_data[(int) new_icode].operand[2].mode;
7051 if (GET_MODE (second_reload_reg) != new_mode)
7053 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
7054 new_mode))
7055 oldequiv = old, real_oldequiv = real_old;
7056 else
7057 second_reload_reg
7058 = gen_rtx_REG (new_mode,
7059 REGNO (second_reload_reg));
7065 /* If we still need a secondary reload register, check
7066 to see if it is being used as a scratch or intermediate
7067 register and generate code appropriately. If we need
7068 a scratch register, use REAL_OLDEQUIV since the form of
7069 the insn may depend on the actual address if it is
7070 a MEM. */
7072 if (second_reload_reg)
7074 if (icode != CODE_FOR_nothing)
7076 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
7077 second_reload_reg));
7078 special = 1;
7080 else
7082 /* See if we need a scratch register to load the
7083 intermediate register (a tertiary reload). */
7084 enum insn_code tertiary_icode
7085 = rld[secondary_reload].secondary_in_icode;
7087 if (tertiary_icode != CODE_FOR_nothing)
7089 rtx third_reload_reg
7090 = rld[rld[secondary_reload].secondary_in_reload].reg_rtx;
7092 emit_insn ((GEN_FCN (tertiary_icode)
7093 (second_reload_reg, real_oldequiv,
7094 third_reload_reg)));
7096 else
7097 gen_reload (second_reload_reg, real_oldequiv,
7098 rld[j].opnum,
7099 rld[j].when_needed);
7101 oldequiv = second_reload_reg;
7105 #endif
7107 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
7109 rtx real_oldequiv = oldequiv;
7111 if ((GET_CODE (oldequiv) == REG
7112 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
7113 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
7114 || reg_equiv_constant[REGNO (oldequiv)] != 0))
7115 || (GET_CODE (oldequiv) == SUBREG
7116 && GET_CODE (SUBREG_REG (oldequiv)) == REG
7117 && (REGNO (SUBREG_REG (oldequiv))
7118 >= FIRST_PSEUDO_REGISTER)
7119 && ((reg_equiv_memory_loc
7120 [REGNO (SUBREG_REG (oldequiv))] != 0)
7121 || (reg_equiv_constant
7122 [REGNO (SUBREG_REG (oldequiv))] != 0))))
7123 real_oldequiv = rld[j].in;
7124 gen_reload (reloadreg, real_oldequiv, rld[j].opnum,
7125 rld[j].when_needed);
7130 this_reload_insn = get_last_insn ();
7131 /* End this sequence. */
7132 *where = get_insns ();
7133 end_sequence ();
7135 /* Update reload_override_in so that delete_address_reloads_1
7136 can see the actual register usage. */
7137 if (oldequiv_reg)
7138 reload_override_in[j] = oldequiv;
7141 /* When inheriting a wider reload, we have a MEM in rld[j].in,
7142 e.g. inheriting a SImode output reload for
7143 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
7144 if (optimize && reload_inherited[j] && rld[j].in
7145 && GET_CODE (rld[j].in) == MEM
7146 && GET_CODE (rld[j].in_reg) == MEM
7147 && reload_spill_index[j] >= 0
7148 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
7150 expect_occurrences
7151 = count_occurrences (PATTERN (insn), rld[j].in) == 1 ? 0 : -1;
7152 rld[j].in
7153 = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
7156 /* If we are reloading a register that was recently stored in with an
7157 output-reload, see if we can prove there was
7158 actually no need to store the old value in it. */
7160 if (optimize
7161 && (reload_inherited[j] || reload_override_in[j])
7162 && rld[j].reg_rtx
7163 && GET_CODE (rld[j].reg_rtx) == REG
7164 && spill_reg_store[REGNO (rld[j].reg_rtx)] != 0
7165 #if 0
7166 /* There doesn't seem to be any reason to restrict this to pseudos
7167 and doing so loses in the case where we are copying from a
7168 register of the wrong class. */
7169 && (REGNO (spill_reg_stored_to[REGNO (rld[j].reg_rtx)])
7170 >= FIRST_PSEUDO_REGISTER)
7171 #endif
7172 /* The insn might have already some references to stackslots
7173 replaced by MEMs, while reload_out_reg still names the
7174 original pseudo. */
7175 && (dead_or_set_p (insn,
7176 spill_reg_stored_to[REGNO (rld[j].reg_rtx)])
7177 || rtx_equal_p (spill_reg_stored_to[REGNO (rld[j].reg_rtx)],
7178 rld[j].out_reg)))
7179 delete_output_reload (insn, j, REGNO (rld[j].reg_rtx));
7181 /* Input-reloading is done. Now do output-reloading,
7182 storing the value from the reload-register after the main insn
7183 if rld[j].out is nonzero.
7185 ??? At some point we need to support handling output reloads of
7186 JUMP_INSNs or insns that set cc0. */
7188 /* If this is an output reload that stores something that is
7189 not loaded in this same reload, see if we can eliminate a previous
7190 store. */
7192 rtx pseudo = rld[j].out_reg;
7194 if (pseudo
7195 && GET_CODE (pseudo) == REG
7196 && ! rtx_equal_p (rld[j].in_reg, pseudo)
7197 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
7198 && reg_last_reload_reg[REGNO (pseudo)])
7200 int pseudo_no = REGNO (pseudo);
7201 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
7203 /* We don't need to test full validity of last_regno for
7204 inherit here; we only want to know if the store actually
7205 matches the pseudo. */
7206 if (reg_reloaded_contents[last_regno] == pseudo_no
7207 && spill_reg_store[last_regno]
7208 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
7209 delete_output_reload (insn, j, last_regno);
7213 old = rld[j].out_reg;
7214 if (old != 0
7215 && rld[j].reg_rtx != old
7216 && rld[j].reg_rtx != 0)
7218 register rtx reloadreg = rld[j].reg_rtx;
7219 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
7220 register rtx second_reloadreg = 0;
7221 #endif
7222 rtx note, p;
7223 enum machine_mode mode;
7224 int special = 0;
7226 /* An output operand that dies right away does need a reload,
7227 but need not be copied from it. Show the new location in the
7228 REG_UNUSED note. */
7229 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
7230 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
7232 XEXP (note, 0) = rld[j].reg_rtx;
7233 continue;
7235 /* Likewise for a SUBREG of an operand that dies. */
7236 else if (GET_CODE (old) == SUBREG
7237 && GET_CODE (SUBREG_REG (old)) == REG
7238 && 0 != (note = find_reg_note (insn, REG_UNUSED,
7239 SUBREG_REG (old))))
7241 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
7242 rld[j].reg_rtx);
7243 continue;
7245 else if (GET_CODE (old) == SCRATCH)
7246 /* If we aren't optimizing, there won't be a REG_UNUSED note,
7247 but we don't want to make an output reload. */
7248 continue;
7250 #if 0
7251 /* Strip off of OLD any size-increasing SUBREGs such as
7252 (SUBREG:SI foo:QI 0). */
7254 while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
7255 && (GET_MODE_SIZE (GET_MODE (old))
7256 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
7257 old = SUBREG_REG (old);
7258 #endif
7260 /* If is a JUMP_INSN, we can't support output reloads yet. */
7261 if (GET_CODE (insn) == JUMP_INSN)
7262 abort ();
7264 if (rld[j].when_needed == RELOAD_OTHER)
7265 start_sequence ();
7266 else
7267 push_to_sequence (output_reload_insns[rld[j].opnum]);
7269 old = rld[j].out;
7271 /* Determine the mode to reload in.
7272 See comments above (for input reloading). */
7274 mode = GET_MODE (old);
7275 if (mode == VOIDmode)
7277 /* VOIDmode should never happen for an output. */
7278 if (asm_noperands (PATTERN (insn)) < 0)
7279 /* It's the compiler's fault. */
7280 fatal_insn ("VOIDmode on an output", insn);
7281 error_for_asm (insn, "output operand is constant in `asm'");
7282 /* Prevent crash--use something we know is valid. */
7283 mode = word_mode;
7284 old = gen_rtx_REG (mode, REGNO (reloadreg));
7287 if (GET_MODE (reloadreg) != mode)
7288 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
7290 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
7292 /* If we need two reload regs, set RELOADREG to the intermediate
7293 one, since it will be stored into OLD. We might need a secondary
7294 register only for an input reload, so check again here. */
7296 if (rld[j].secondary_out_reload >= 0)
7298 rtx real_old = old;
7300 if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
7301 && reg_equiv_mem[REGNO (old)] != 0)
7302 real_old = reg_equiv_mem[REGNO (old)];
7304 if((SECONDARY_OUTPUT_RELOAD_CLASS (rld[j].class,
7305 mode, real_old)
7306 != NO_REGS))
7308 second_reloadreg = reloadreg;
7309 reloadreg = rld[rld[j].secondary_out_reload].reg_rtx;
7311 /* See if RELOADREG is to be used as a scratch register
7312 or as an intermediate register. */
7313 if (rld[j].secondary_out_icode != CODE_FOR_nothing)
7315 emit_insn ((GEN_FCN (rld[j].secondary_out_icode)
7316 (real_old, second_reloadreg, reloadreg)));
7317 special = 1;
7319 else
7321 /* See if we need both a scratch and intermediate reload
7322 register. */
7324 int secondary_reload = rld[j].secondary_out_reload;
7325 enum insn_code tertiary_icode
7326 = rld[secondary_reload].secondary_out_icode;
7328 if (GET_MODE (reloadreg) != mode)
7329 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
7331 if (tertiary_icode != CODE_FOR_nothing)
7333 rtx third_reloadreg
7334 = rld[rld[secondary_reload].secondary_out_reload].reg_rtx;
7335 rtx tem;
7337 /* Copy primary reload reg to secondary reload reg.
7338 (Note that these have been swapped above, then
7339 secondary reload reg to OLD using our insn. */
7341 /* If REAL_OLD is a paradoxical SUBREG, remove it
7342 and try to put the opposite SUBREG on
7343 RELOADREG. */
7344 if (GET_CODE (real_old) == SUBREG
7345 && (GET_MODE_SIZE (GET_MODE (real_old))
7346 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
7347 && 0 != (tem = gen_lowpart_common
7348 (GET_MODE (SUBREG_REG (real_old)),
7349 reloadreg)))
7350 real_old = SUBREG_REG (real_old), reloadreg = tem;
7352 gen_reload (reloadreg, second_reloadreg,
7353 rld[j].opnum, rld[j].when_needed);
7354 emit_insn ((GEN_FCN (tertiary_icode)
7355 (real_old, reloadreg, third_reloadreg)));
7356 special = 1;
7359 else
7360 /* Copy between the reload regs here and then to
7361 OUT later. */
7363 gen_reload (reloadreg, second_reloadreg,
7364 rld[j].opnum, rld[j].when_needed);
7368 #endif
7370 /* Output the last reload insn. */
7371 if (! special)
7373 rtx set;
7375 /* Don't output the last reload if OLD is not the dest of
7376 INSN and is in the src and is clobbered by INSN. */
7377 if (! flag_expensive_optimizations
7378 || GET_CODE (old) != REG
7379 || !(set = single_set (insn))
7380 || rtx_equal_p (old, SET_DEST (set))
7381 || !reg_mentioned_p (old, SET_SRC (set))
7382 || !regno_clobbered_p (REGNO (old), insn))
7383 gen_reload (old, reloadreg, rld[j].opnum,
7384 rld[j].when_needed);
7387 /* Look at all insns we emitted, just to be safe. */
7388 for (p = get_insns (); p; p = NEXT_INSN (p))
7389 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
7391 rtx pat = PATTERN (p);
7393 /* If this output reload doesn't come from a spill reg,
7394 clear any memory of reloaded copies of the pseudo reg.
7395 If this output reload comes from a spill reg,
7396 reg_has_output_reload will make this do nothing. */
7397 note_stores (pat, forget_old_reloads_1);
7399 if (reg_mentioned_p (rld[j].reg_rtx, pat))
7401 rtx set = single_set (insn);
7402 if (reload_spill_index[j] < 0
7403 && set
7404 && SET_SRC (set) == rld[j].reg_rtx)
7406 int src = REGNO (SET_SRC (set));
7408 reload_spill_index[j] = src;
7409 SET_HARD_REG_BIT (reg_is_output_reload, src);
7410 if (find_regno_note (insn, REG_DEAD, src))
7411 SET_HARD_REG_BIT (reg_reloaded_died, src);
7413 if (REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
7415 int s = rld[j].secondary_out_reload;
7416 set = single_set (p);
7417 /* If this reload copies only to the secondary reload
7418 register, the secondary reload does the actual
7419 store. */
7420 if (s >= 0 && set == NULL_RTX)
7421 ; /* We can't tell what function the secondary reload
7422 has and where the actual store to the pseudo is
7423 made; leave new_spill_reg_store alone. */
7424 else if (s >= 0
7425 && SET_SRC (set) == rld[j].reg_rtx
7426 && SET_DEST (set) == rld[s].reg_rtx)
7428 /* Usually the next instruction will be the
7429 secondary reload insn; if we can confirm
7430 that it is, setting new_spill_reg_store to
7431 that insn will allow an extra optimization. */
7432 rtx s_reg = rld[s].reg_rtx;
7433 rtx next = NEXT_INSN (p);
7434 rld[s].out = rld[j].out;
7435 rld[s].out_reg = rld[j].out_reg;
7436 set = single_set (next);
7437 if (set && SET_SRC (set) == s_reg
7438 && ! new_spill_reg_store[REGNO (s_reg)])
7440 SET_HARD_REG_BIT (reg_is_output_reload,
7441 REGNO (s_reg));
7442 new_spill_reg_store[REGNO (s_reg)] = next;
7445 else
7446 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = p;
7451 if (rld[j].when_needed == RELOAD_OTHER)
7453 emit_insns (other_output_reload_insns[rld[j].opnum]);
7454 other_output_reload_insns[rld[j].opnum] = get_insns ();
7456 else
7457 output_reload_insns[rld[j].opnum] = get_insns ();
7459 end_sequence ();
7463 /* Now write all the insns we made for reloads in the order expected by
7464 the allocation functions. Prior to the insn being reloaded, we write
7465 the following reloads:
7467 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7469 RELOAD_OTHER reloads.
7471 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7472 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7473 RELOAD_FOR_INPUT reload for the operand.
7475 RELOAD_FOR_OPADDR_ADDRS reloads.
7477 RELOAD_FOR_OPERAND_ADDRESS reloads.
7479 After the insn being reloaded, we write the following:
7481 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7482 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7483 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7484 reloads for the operand. The RELOAD_OTHER output reloads are
7485 output in descending order by reload number. */
7487 emit_insns_before (other_input_address_reload_insns, insn);
7488 emit_insns_before (other_input_reload_insns, insn);
7490 for (j = 0; j < reload_n_operands; j++)
7492 emit_insns_before (inpaddr_address_reload_insns[j], insn);
7493 emit_insns_before (input_address_reload_insns[j], insn);
7494 emit_insns_before (input_reload_insns[j], insn);
7497 emit_insns_before (other_operand_reload_insns, insn);
7498 emit_insns_before (operand_reload_insns, insn);
7500 for (j = 0; j < reload_n_operands; j++)
7502 emit_insns_before (outaddr_address_reload_insns[j], following_insn);
7503 emit_insns_before (output_address_reload_insns[j], following_insn);
7504 emit_insns_before (output_reload_insns[j], following_insn);
7505 emit_insns_before (other_output_reload_insns[j], following_insn);
7508 /* Keep basic block info up to date. */
7509 if (n_basic_blocks)
7511 if (BLOCK_HEAD (chain->block) == insn)
7512 BLOCK_HEAD (chain->block) = NEXT_INSN (before_insn);
7513 if (BLOCK_END (chain->block) == insn)
7514 BLOCK_END (chain->block) = PREV_INSN (following_insn);
7517 /* For all the spill regs newly reloaded in this instruction,
7518 record what they were reloaded from, so subsequent instructions
7519 can inherit the reloads.
7521 Update spill_reg_store for the reloads of this insn.
7522 Copy the elements that were updated in the loop above. */
7524 for (j = 0; j < n_reloads; j++)
7526 register int r = reload_order[j];
7527 register int i = reload_spill_index[r];
7529 /* If this is a non-inherited input reload from a pseudo, we must
7530 clear any memory of a previous store to the same pseudo. Only do
7531 something if there will not be an output reload for the pseudo
7532 being reloaded. */
7533 if (rld[r].in_reg != 0
7534 && ! (reload_inherited[r] || reload_override_in[r]))
7536 rtx reg = rld[r].in_reg;
7538 if (GET_CODE (reg) == SUBREG)
7539 reg = SUBREG_REG (reg);
7541 if (GET_CODE (reg) == REG
7542 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7543 && ! reg_has_output_reload[REGNO (reg)])
7545 int nregno = REGNO (reg);
7547 if (reg_last_reload_reg[nregno])
7549 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7551 if (reg_reloaded_contents[last_regno] == nregno)
7552 spill_reg_store[last_regno] = 0;
7557 /* I is nonneg if this reload used a register.
7558 If rld[r].reg_rtx is 0, this is an optional reload
7559 that we opted to ignore. */
7561 if (i >= 0 && rld[r].reg_rtx != 0)
7563 int nr
7564 = HARD_REGNO_NREGS (i, GET_MODE (rld[r].reg_rtx));
7565 int k;
7566 int part_reaches_end = 0;
7567 int all_reaches_end = 1;
7569 /* For a multi register reload, we need to check if all or part
7570 of the value lives to the end. */
7571 for (k = 0; k < nr; k++)
7573 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7574 rld[r].when_needed))
7575 part_reaches_end = 1;
7576 else
7577 all_reaches_end = 0;
7580 /* Ignore reloads that don't reach the end of the insn in
7581 entirety. */
7582 if (all_reaches_end)
7584 /* First, clear out memory of what used to be in this spill reg.
7585 If consecutive registers are used, clear them all. */
7587 for (k = 0; k < nr; k++)
7588 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7590 /* Maybe the spill reg contains a copy of reload_out. */
7591 if (rld[r].out != 0
7592 && (GET_CODE (rld[r].out) == REG
7593 #ifdef AUTO_INC_DEC
7594 || ! rld[r].out_reg
7595 #endif
7596 || GET_CODE (rld[r].out_reg) == REG))
7598 rtx out = (GET_CODE (rld[r].out) == REG
7599 ? rld[r].out
7600 : rld[r].out_reg
7601 ? rld[r].out_reg
7602 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7603 register int nregno = REGNO (out);
7604 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7605 : HARD_REGNO_NREGS (nregno,
7606 GET_MODE (rld[r].reg_rtx)));
7608 spill_reg_store[i] = new_spill_reg_store[i];
7609 spill_reg_stored_to[i] = out;
7610 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7612 /* If NREGNO is a hard register, it may occupy more than
7613 one register. If it does, say what is in the
7614 rest of the registers assuming that both registers
7615 agree on how many words the object takes. If not,
7616 invalidate the subsequent registers. */
7618 if (nregno < FIRST_PSEUDO_REGISTER)
7619 for (k = 1; k < nnr; k++)
7620 reg_last_reload_reg[nregno + k]
7621 = (nr == nnr
7622 ? gen_rtx_REG (reg_raw_mode[REGNO (rld[r].reg_rtx) + k],
7623 REGNO (rld[r].reg_rtx) + k)
7624 : 0);
7626 /* Now do the inverse operation. */
7627 for (k = 0; k < nr; k++)
7629 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7630 reg_reloaded_contents[i + k]
7631 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7632 ? nregno
7633 : nregno + k);
7634 reg_reloaded_insn[i + k] = insn;
7635 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7639 /* Maybe the spill reg contains a copy of reload_in. Only do
7640 something if there will not be an output reload for
7641 the register being reloaded. */
7642 else if (rld[r].out_reg == 0
7643 && rld[r].in != 0
7644 && ((GET_CODE (rld[r].in) == REG
7645 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7646 && ! reg_has_output_reload[REGNO (rld[r].in)])
7647 || (GET_CODE (rld[r].in_reg) == REG
7648 && ! reg_has_output_reload[REGNO (rld[r].in_reg)]))
7649 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7651 register int nregno;
7652 int nnr;
7654 if (GET_CODE (rld[r].in) == REG
7655 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7656 nregno = REGNO (rld[r].in);
7657 else if (GET_CODE (rld[r].in_reg) == REG)
7658 nregno = REGNO (rld[r].in_reg);
7659 else
7660 nregno = REGNO (XEXP (rld[r].in_reg, 0));
7662 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7663 : HARD_REGNO_NREGS (nregno,
7664 GET_MODE (rld[r].reg_rtx)));
7666 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7668 if (nregno < FIRST_PSEUDO_REGISTER)
7669 for (k = 1; k < nnr; k++)
7670 reg_last_reload_reg[nregno + k]
7671 = (nr == nnr
7672 ? gen_rtx_REG (reg_raw_mode[REGNO (rld[r].reg_rtx) + k],
7673 REGNO (rld[r].reg_rtx) + k)
7674 : 0);
7676 /* Unless we inherited this reload, show we haven't
7677 recently done a store.
7678 Previous stores of inherited auto_inc expressions
7679 also have to be discarded. */
7680 if (! reload_inherited[r]
7681 || (rld[r].out && ! rld[r].out_reg))
7682 spill_reg_store[i] = 0;
7684 for (k = 0; k < nr; k++)
7686 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7687 reg_reloaded_contents[i + k]
7688 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7689 ? nregno
7690 : nregno + k);
7691 reg_reloaded_insn[i + k] = insn;
7692 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7697 /* However, if part of the reload reaches the end, then we must
7698 invalidate the old info for the part that survives to the end. */
7699 else if (part_reaches_end)
7701 for (k = 0; k < nr; k++)
7702 if (reload_reg_reaches_end_p (i + k,
7703 rld[r].opnum,
7704 rld[r].when_needed))
7705 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7709 /* The following if-statement was #if 0'd in 1.34 (or before...).
7710 It's reenabled in 1.35 because supposedly nothing else
7711 deals with this problem. */
7713 /* If a register gets output-reloaded from a non-spill register,
7714 that invalidates any previous reloaded copy of it.
7715 But forget_old_reloads_1 won't get to see it, because
7716 it thinks only about the original insn. So invalidate it here. */
7717 if (i < 0 && rld[r].out != 0
7718 && (GET_CODE (rld[r].out) == REG
7719 || (GET_CODE (rld[r].out) == MEM
7720 && GET_CODE (rld[r].out_reg) == REG)))
7722 rtx out = (GET_CODE (rld[r].out) == REG
7723 ? rld[r].out : rld[r].out_reg);
7724 register int nregno = REGNO (out);
7725 if (nregno >= FIRST_PSEUDO_REGISTER)
7727 rtx src_reg, store_insn = NULL_RTX;
7729 reg_last_reload_reg[nregno] = 0;
7731 /* If we can find a hard register that is stored, record
7732 the storing insn so that we may delete this insn with
7733 delete_output_reload. */
7734 src_reg = rld[r].reg_rtx;
7736 /* If this is an optional reload, try to find the source reg
7737 from an input reload. */
7738 if (! src_reg)
7740 rtx set = single_set (insn);
7741 if (set && SET_DEST (set) == rld[r].out)
7743 int k;
7745 src_reg = SET_SRC (set);
7746 store_insn = insn;
7747 for (k = 0; k < n_reloads; k++)
7749 if (rld[k].in == src_reg)
7751 src_reg = rld[k].reg_rtx;
7752 break;
7757 else
7758 store_insn = new_spill_reg_store[REGNO (src_reg)];
7759 if (src_reg && GET_CODE (src_reg) == REG
7760 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7762 int src_regno = REGNO (src_reg);
7763 int nr = HARD_REGNO_NREGS (src_regno, rld[r].mode);
7764 /* The place where to find a death note varies with
7765 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7766 necessarily checked exactly in the code that moves
7767 notes, so just check both locations. */
7768 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7769 if (! note)
7770 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7771 while (nr-- > 0)
7773 spill_reg_store[src_regno + nr] = store_insn;
7774 spill_reg_stored_to[src_regno + nr] = out;
7775 reg_reloaded_contents[src_regno + nr] = nregno;
7776 reg_reloaded_insn[src_regno + nr] = store_insn;
7777 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7778 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7779 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7780 if (note)
7781 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7782 else
7783 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7785 reg_last_reload_reg[nregno] = src_reg;
7788 else
7790 int num_regs = HARD_REGNO_NREGS (nregno,GET_MODE (rld[r].out));
7792 while (num_regs-- > 0)
7793 reg_last_reload_reg[nregno + num_regs] = 0;
7797 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7800 /* Emit code to perform a reload from IN (which may be a reload register) to
7801 OUT (which may also be a reload register). IN or OUT is from operand
7802 OPNUM with reload type TYPE.
7804 Returns first insn emitted. */
7807 gen_reload (out, in, opnum, type)
7808 rtx out;
7809 rtx in;
7810 int opnum;
7811 enum reload_type type;
7813 rtx last = get_last_insn ();
7814 rtx tem;
7816 /* If IN is a paradoxical SUBREG, remove it and try to put the
7817 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7818 if (GET_CODE (in) == SUBREG
7819 && (GET_MODE_SIZE (GET_MODE (in))
7820 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7821 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7822 in = SUBREG_REG (in), out = tem;
7823 else if (GET_CODE (out) == SUBREG
7824 && (GET_MODE_SIZE (GET_MODE (out))
7825 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7826 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7827 out = SUBREG_REG (out), in = tem;
7829 /* How to do this reload can get quite tricky. Normally, we are being
7830 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7831 register that didn't get a hard register. In that case we can just
7832 call emit_move_insn.
7834 We can also be asked to reload a PLUS that adds a register or a MEM to
7835 another register, constant or MEM. This can occur during frame pointer
7836 elimination and while reloading addresses. This case is handled by
7837 trying to emit a single insn to perform the add. If it is not valid,
7838 we use a two insn sequence.
7840 Finally, we could be called to handle an 'o' constraint by putting
7841 an address into a register. In that case, we first try to do this
7842 with a named pattern of "reload_load_address". If no such pattern
7843 exists, we just emit a SET insn and hope for the best (it will normally
7844 be valid on machines that use 'o').
7846 This entire process is made complex because reload will never
7847 process the insns we generate here and so we must ensure that
7848 they will fit their constraints and also by the fact that parts of
7849 IN might be being reloaded separately and replaced with spill registers.
7850 Because of this, we are, in some sense, just guessing the right approach
7851 here. The one listed above seems to work.
7853 ??? At some point, this whole thing needs to be rethought. */
7855 if (GET_CODE (in) == PLUS
7856 && (GET_CODE (XEXP (in, 0)) == REG
7857 || GET_CODE (XEXP (in, 0)) == SUBREG
7858 || GET_CODE (XEXP (in, 0)) == MEM)
7859 && (GET_CODE (XEXP (in, 1)) == REG
7860 || GET_CODE (XEXP (in, 1)) == SUBREG
7861 || CONSTANT_P (XEXP (in, 1))
7862 || GET_CODE (XEXP (in, 1)) == MEM))
7864 /* We need to compute the sum of a register or a MEM and another
7865 register, constant, or MEM, and put it into the reload
7866 register. The best possible way of doing this is if the machine
7867 has a three-operand ADD insn that accepts the required operands.
7869 The simplest approach is to try to generate such an insn and see if it
7870 is recognized and matches its constraints. If so, it can be used.
7872 It might be better not to actually emit the insn unless it is valid,
7873 but we need to pass the insn as an operand to `recog' and
7874 `extract_insn' and it is simpler to emit and then delete the insn if
7875 not valid than to dummy things up. */
7877 rtx op0, op1, tem, insn;
7878 int code;
7880 op0 = find_replacement (&XEXP (in, 0));
7881 op1 = find_replacement (&XEXP (in, 1));
7883 /* Since constraint checking is strict, commutativity won't be
7884 checked, so we need to do that here to avoid spurious failure
7885 if the add instruction is two-address and the second operand
7886 of the add is the same as the reload reg, which is frequently
7887 the case. If the insn would be A = B + A, rearrange it so
7888 it will be A = A + B as constrain_operands expects. */
7890 if (GET_CODE (XEXP (in, 1)) == REG
7891 && REGNO (out) == REGNO (XEXP (in, 1)))
7892 tem = op0, op0 = op1, op1 = tem;
7894 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7895 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7897 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
7898 code = recog_memoized (insn);
7900 if (code >= 0)
7902 extract_insn (insn);
7903 /* We want constrain operands to treat this insn strictly in
7904 its validity determination, i.e., the way it would after reload
7905 has completed. */
7906 if (constrain_operands (1))
7907 return insn;
7910 delete_insns_since (last);
7912 /* If that failed, we must use a conservative two-insn sequence.
7914 Use a move to copy one operand into the reload register. Prefer
7915 to reload a constant, MEM or pseudo since the move patterns can
7916 handle an arbitrary operand. If OP1 is not a constant, MEM or
7917 pseudo and OP1 is not a valid operand for an add instruction, then
7918 reload OP1.
7920 After reloading one of the operands into the reload register, add
7921 the reload register to the output register.
7923 If there is another way to do this for a specific machine, a
7924 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7925 we emit below. */
7927 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7929 if (CONSTANT_P (op1) || GET_CODE (op1) == MEM || GET_CODE (op1) == SUBREG
7930 || (GET_CODE (op1) == REG
7931 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7932 || (code != CODE_FOR_nothing
7933 && ! ((*insn_data[code].operand[2].predicate)
7934 (op1, insn_data[code].operand[2].mode))))
7935 tem = op0, op0 = op1, op1 = tem;
7937 gen_reload (out, op0, opnum, type);
7939 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7940 This fixes a problem on the 32K where the stack pointer cannot
7941 be used as an operand of an add insn. */
7943 if (rtx_equal_p (op0, op1))
7944 op1 = out;
7946 insn = emit_insn (gen_add2_insn (out, op1));
7948 /* If that failed, copy the address register to the reload register.
7949 Then add the constant to the reload register. */
7951 code = recog_memoized (insn);
7953 if (code >= 0)
7955 extract_insn (insn);
7956 /* We want constrain operands to treat this insn strictly in
7957 its validity determination, i.e., the way it would after reload
7958 has completed. */
7959 if (constrain_operands (1))
7961 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7962 REG_NOTES (insn)
7963 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7964 return insn;
7968 delete_insns_since (last);
7970 gen_reload (out, op1, opnum, type);
7971 insn = emit_insn (gen_add2_insn (out, op0));
7972 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7975 #ifdef SECONDARY_MEMORY_NEEDED
7976 /* If we need a memory location to do the move, do it that way. */
7977 else if (GET_CODE (in) == REG && REGNO (in) < FIRST_PSEUDO_REGISTER
7978 && GET_CODE (out) == REG && REGNO (out) < FIRST_PSEUDO_REGISTER
7979 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (in)),
7980 REGNO_REG_CLASS (REGNO (out)),
7981 GET_MODE (out)))
7983 /* Get the memory to use and rewrite both registers to its mode. */
7984 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7986 if (GET_MODE (loc) != GET_MODE (out))
7987 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7989 if (GET_MODE (loc) != GET_MODE (in))
7990 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7992 gen_reload (loc, in, opnum, type);
7993 gen_reload (out, loc, opnum, type);
7995 #endif
7997 /* If IN is a simple operand, use gen_move_insn. */
7998 else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
7999 emit_insn (gen_move_insn (out, in));
8001 #ifdef HAVE_reload_load_address
8002 else if (HAVE_reload_load_address)
8003 emit_insn (gen_reload_load_address (out, in));
8004 #endif
8006 /* Otherwise, just write (set OUT IN) and hope for the best. */
8007 else
8008 emit_insn (gen_rtx_SET (VOIDmode, out, in));
8010 /* Return the first insn emitted.
8011 We can not just return get_last_insn, because there may have
8012 been multiple instructions emitted. Also note that gen_move_insn may
8013 emit more than one insn itself, so we can not assume that there is one
8014 insn emitted per emit_insn_before call. */
8016 return last ? NEXT_INSN (last) : get_insns ();
8019 /* Delete a previously made output-reload
8020 whose result we now believe is not needed.
8021 First we double-check.
8023 INSN is the insn now being processed.
8024 LAST_RELOAD_REG is the hard register number for which we want to delete
8025 the last output reload.
8026 J is the reload-number that originally used REG. The caller has made
8027 certain that reload J doesn't use REG any longer for input. */
8029 static void
8030 delete_output_reload (insn, j, last_reload_reg)
8031 rtx insn;
8032 int j;
8033 int last_reload_reg;
8035 rtx output_reload_insn = spill_reg_store[last_reload_reg];
8036 rtx reg = spill_reg_stored_to[last_reload_reg];
8037 int k;
8038 int n_occurrences;
8039 int n_inherited = 0;
8040 register rtx i1;
8041 rtx substed;
8043 /* Get the raw pseudo-register referred to. */
8045 while (GET_CODE (reg) == SUBREG)
8046 reg = SUBREG_REG (reg);
8047 substed = reg_equiv_memory_loc[REGNO (reg)];
8049 /* This is unsafe if the operand occurs more often in the current
8050 insn than it is inherited. */
8051 for (k = n_reloads - 1; k >= 0; k--)
8053 rtx reg2 = rld[k].in;
8054 if (! reg2)
8055 continue;
8056 if (GET_CODE (reg2) == MEM || reload_override_in[k])
8057 reg2 = rld[k].in_reg;
8058 #ifdef AUTO_INC_DEC
8059 if (rld[k].out && ! rld[k].out_reg)
8060 reg2 = XEXP (rld[k].in_reg, 0);
8061 #endif
8062 while (GET_CODE (reg2) == SUBREG)
8063 reg2 = SUBREG_REG (reg2);
8064 if (rtx_equal_p (reg2, reg))
8066 if (reload_inherited[k] || reload_override_in[k] || k == j)
8068 n_inherited++;
8069 reg2 = rld[k].out_reg;
8070 if (! reg2)
8071 continue;
8072 while (GET_CODE (reg2) == SUBREG)
8073 reg2 = XEXP (reg2, 0);
8074 if (rtx_equal_p (reg2, reg))
8075 n_inherited++;
8077 else
8078 return;
8081 n_occurrences = count_occurrences (PATTERN (insn), reg);
8082 if (substed)
8083 n_occurrences += count_occurrences (PATTERN (insn), substed);
8084 if (n_occurrences > n_inherited)
8085 return;
8087 /* If the pseudo-reg we are reloading is no longer referenced
8088 anywhere between the store into it and here,
8089 and no jumps or labels intervene, then the value can get
8090 here through the reload reg alone.
8091 Otherwise, give up--return. */
8092 for (i1 = NEXT_INSN (output_reload_insn);
8093 i1 != insn; i1 = NEXT_INSN (i1))
8095 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
8096 return;
8097 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
8098 && reg_mentioned_p (reg, PATTERN (i1)))
8100 /* If this is USE in front of INSN, we only have to check that
8101 there are no more references than accounted for by inheritance. */
8102 while (GET_CODE (i1) == INSN && GET_CODE (PATTERN (i1)) == USE)
8104 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
8105 i1 = NEXT_INSN (i1);
8107 if (n_occurrences <= n_inherited && i1 == insn)
8108 break;
8109 return;
8113 /* The caller has already checked that REG dies or is set in INSN.
8114 It has also checked that we are optimizing, and thus some inaccurancies
8115 in the debugging information are acceptable.
8116 So we could just delete output_reload_insn.
8117 But in some cases we can improve the debugging information without
8118 sacrificing optimization - maybe even improving the code:
8119 See if the pseudo reg has been completely replaced
8120 with reload regs. If so, delete the store insn
8121 and forget we had a stack slot for the pseudo. */
8122 if (rld[j].out != rld[j].in
8123 && REG_N_DEATHS (REGNO (reg)) == 1
8124 && REG_N_SETS (REGNO (reg)) == 1
8125 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
8126 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
8128 rtx i2;
8130 /* We know that it was used only between here
8131 and the beginning of the current basic block.
8132 (We also know that the last use before INSN was
8133 the output reload we are thinking of deleting, but never mind that.)
8134 Search that range; see if any ref remains. */
8135 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8137 rtx set = single_set (i2);
8139 /* Uses which just store in the pseudo don't count,
8140 since if they are the only uses, they are dead. */
8141 if (set != 0 && SET_DEST (set) == reg)
8142 continue;
8143 if (GET_CODE (i2) == CODE_LABEL
8144 || GET_CODE (i2) == JUMP_INSN)
8145 break;
8146 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
8147 && reg_mentioned_p (reg, PATTERN (i2)))
8149 /* Some other ref remains; just delete the output reload we
8150 know to be dead. */
8151 delete_address_reloads (output_reload_insn, insn);
8152 PUT_CODE (output_reload_insn, NOTE);
8153 NOTE_SOURCE_FILE (output_reload_insn) = 0;
8154 NOTE_LINE_NUMBER (output_reload_insn) = NOTE_INSN_DELETED;
8155 return;
8159 /* Delete the now-dead stores into this pseudo. */
8160 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8162 rtx set = single_set (i2);
8164 if (set != 0 && SET_DEST (set) == reg)
8166 delete_address_reloads (i2, insn);
8167 /* This might be a basic block head,
8168 thus don't use delete_insn. */
8169 PUT_CODE (i2, NOTE);
8170 NOTE_SOURCE_FILE (i2) = 0;
8171 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
8173 if (GET_CODE (i2) == CODE_LABEL
8174 || GET_CODE (i2) == JUMP_INSN)
8175 break;
8178 /* For the debugging info,
8179 say the pseudo lives in this reload reg. */
8180 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
8181 alter_reg (REGNO (reg), -1);
8183 delete_address_reloads (output_reload_insn, insn);
8184 PUT_CODE (output_reload_insn, NOTE);
8185 NOTE_SOURCE_FILE (output_reload_insn) = 0;
8186 NOTE_LINE_NUMBER (output_reload_insn) = NOTE_INSN_DELETED;
8190 /* We are going to delete DEAD_INSN. Recursively delete loads of
8191 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
8192 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
8193 static void
8194 delete_address_reloads (dead_insn, current_insn)
8195 rtx dead_insn, current_insn;
8197 rtx set = single_set (dead_insn);
8198 rtx set2, dst, prev, next;
8199 if (set)
8201 rtx dst = SET_DEST (set);
8202 if (GET_CODE (dst) == MEM)
8203 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
8205 /* If we deleted the store from a reloaded post_{in,de}c expression,
8206 we can delete the matching adds. */
8207 prev = PREV_INSN (dead_insn);
8208 next = NEXT_INSN (dead_insn);
8209 if (! prev || ! next)
8210 return;
8211 set = single_set (next);
8212 set2 = single_set (prev);
8213 if (! set || ! set2
8214 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
8215 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
8216 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
8217 return;
8218 dst = SET_DEST (set);
8219 if (! rtx_equal_p (dst, SET_DEST (set2))
8220 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
8221 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
8222 || (INTVAL (XEXP (SET_SRC (set), 1))
8223 != - INTVAL (XEXP (SET_SRC (set2), 1))))
8224 return;
8225 delete_insn (prev);
8226 delete_insn (next);
8229 /* Subfunction of delete_address_reloads: process registers found in X. */
8230 static void
8231 delete_address_reloads_1 (dead_insn, x, current_insn)
8232 rtx dead_insn, x, current_insn;
8234 rtx prev, set, dst, i2;
8235 int i, j;
8236 enum rtx_code code = GET_CODE (x);
8238 if (code != REG)
8240 const char *fmt= GET_RTX_FORMAT (code);
8241 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8243 if (fmt[i] == 'e')
8244 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
8245 else if (fmt[i] == 'E')
8247 for (j = XVECLEN (x, i) - 1; j >=0; j--)
8248 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
8249 current_insn);
8252 return;
8255 if (spill_reg_order[REGNO (x)] < 0)
8256 return;
8258 /* Scan backwards for the insn that sets x. This might be a way back due
8259 to inheritance. */
8260 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
8262 code = GET_CODE (prev);
8263 if (code == CODE_LABEL || code == JUMP_INSN)
8264 return;
8265 if (GET_RTX_CLASS (code) != 'i')
8266 continue;
8267 if (reg_set_p (x, PATTERN (prev)))
8268 break;
8269 if (reg_referenced_p (x, PATTERN (prev)))
8270 return;
8272 if (! prev || INSN_UID (prev) < reload_first_uid)
8273 return;
8274 /* Check that PREV only sets the reload register. */
8275 set = single_set (prev);
8276 if (! set)
8277 return;
8278 dst = SET_DEST (set);
8279 if (GET_CODE (dst) != REG
8280 || ! rtx_equal_p (dst, x))
8281 return;
8282 if (! reg_set_p (dst, PATTERN (dead_insn)))
8284 /* Check if DST was used in a later insn -
8285 it might have been inherited. */
8286 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
8288 if (GET_CODE (i2) == CODE_LABEL)
8289 break;
8290 if (GET_RTX_CLASS (GET_CODE (i2)) != 'i')
8291 continue;
8292 if (reg_referenced_p (dst, PATTERN (i2)))
8294 /* If there is a reference to the register in the current insn,
8295 it might be loaded in a non-inherited reload. If no other
8296 reload uses it, that means the register is set before
8297 referenced. */
8298 if (i2 == current_insn)
8300 for (j = n_reloads - 1; j >= 0; j--)
8301 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8302 || reload_override_in[j] == dst)
8303 return;
8304 for (j = n_reloads - 1; j >= 0; j--)
8305 if (rld[j].in && rld[j].reg_rtx == dst)
8306 break;
8307 if (j >= 0)
8308 break;
8310 return;
8312 if (GET_CODE (i2) == JUMP_INSN)
8313 break;
8314 /* If DST is still live at CURRENT_INSN, check if it is used for
8315 any reload. Note that even if CURRENT_INSN sets DST, we still
8316 have to check the reloads. */
8317 if (i2 == current_insn)
8319 for (j = n_reloads - 1; j >= 0; j--)
8320 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8321 || reload_override_in[j] == dst)
8322 return;
8323 /* ??? We can't finish the loop here, because dst might be
8324 allocated to a pseudo in this block if no reload in this
8325 block needs any of the clsses containing DST - see
8326 spill_hard_reg. There is no easy way to tell this, so we
8327 have to scan till the end of the basic block. */
8329 if (reg_set_p (dst, PATTERN (i2)))
8330 break;
8333 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
8334 reg_reloaded_contents[REGNO (dst)] = -1;
8335 /* Can't use delete_insn here because PREV might be a basic block head. */
8336 PUT_CODE (prev, NOTE);
8337 NOTE_LINE_NUMBER (prev) = NOTE_INSN_DELETED;
8338 NOTE_SOURCE_FILE (prev) = 0;
8341 /* Output reload-insns to reload VALUE into RELOADREG.
8342 VALUE is an autoincrement or autodecrement RTX whose operand
8343 is a register or memory location;
8344 so reloading involves incrementing that location.
8345 IN is either identical to VALUE, or some cheaper place to reload from.
8347 INC_AMOUNT is the number to increment or decrement by (always positive).
8348 This cannot be deduced from VALUE.
8350 Return the instruction that stores into RELOADREG. */
8352 static rtx
8353 inc_for_reload (reloadreg, in, value, inc_amount)
8354 rtx reloadreg;
8355 rtx in, value;
8356 int inc_amount;
8358 /* REG or MEM to be copied and incremented. */
8359 rtx incloc = XEXP (value, 0);
8360 /* Nonzero if increment after copying. */
8361 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
8362 rtx last;
8363 rtx inc;
8364 rtx add_insn;
8365 int code;
8366 rtx store;
8367 rtx real_in = in == value ? XEXP (in, 0) : in;
8369 /* No hard register is equivalent to this register after
8370 inc/dec operation. If REG_LAST_RELOAD_REG were non-zero,
8371 we could inc/dec that register as well (maybe even using it for
8372 the source), but I'm not sure it's worth worrying about. */
8373 if (GET_CODE (incloc) == REG)
8374 reg_last_reload_reg[REGNO (incloc)] = 0;
8376 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
8377 inc_amount = - inc_amount;
8379 inc = GEN_INT (inc_amount);
8381 /* If this is post-increment, first copy the location to the reload reg. */
8382 if (post && real_in != reloadreg)
8383 emit_insn (gen_move_insn (reloadreg, real_in));
8385 if (in == value)
8387 /* See if we can directly increment INCLOC. Use a method similar to
8388 that in gen_reload. */
8390 last = get_last_insn ();
8391 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
8392 gen_rtx_PLUS (GET_MODE (incloc),
8393 incloc, inc)));
8395 code = recog_memoized (add_insn);
8396 if (code >= 0)
8398 extract_insn (add_insn);
8399 if (constrain_operands (1))
8401 /* If this is a pre-increment and we have incremented the value
8402 where it lives, copy the incremented value to RELOADREG to
8403 be used as an address. */
8405 if (! post)
8406 emit_insn (gen_move_insn (reloadreg, incloc));
8408 return add_insn;
8411 delete_insns_since (last);
8414 /* If couldn't do the increment directly, must increment in RELOADREG.
8415 The way we do this depends on whether this is pre- or post-increment.
8416 For pre-increment, copy INCLOC to the reload register, increment it
8417 there, then save back. */
8419 if (! post)
8421 if (in != reloadreg)
8422 emit_insn (gen_move_insn (reloadreg, real_in));
8423 emit_insn (gen_add2_insn (reloadreg, inc));
8424 store = emit_insn (gen_move_insn (incloc, reloadreg));
8426 else
8428 /* Postincrement.
8429 Because this might be a jump insn or a compare, and because RELOADREG
8430 may not be available after the insn in an input reload, we must do
8431 the incrementation before the insn being reloaded for.
8433 We have already copied IN to RELOADREG. Increment the copy in
8434 RELOADREG, save that back, then decrement RELOADREG so it has
8435 the original value. */
8437 emit_insn (gen_add2_insn (reloadreg, inc));
8438 store = emit_insn (gen_move_insn (incloc, reloadreg));
8439 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
8442 return store;
8445 /* Return 1 if we are certain that the constraint-string STRING allows
8446 the hard register REG. Return 0 if we can't be sure of this. */
8448 static int
8449 constraint_accepts_reg_p (string, reg)
8450 const char *string;
8451 rtx reg;
8453 int value = 0;
8454 int regno = true_regnum (reg);
8455 int c;
8457 /* Initialize for first alternative. */
8458 value = 0;
8459 /* Check that each alternative contains `g' or `r'. */
8460 while (1)
8461 switch (c = *string++)
8463 case 0:
8464 /* If an alternative lacks `g' or `r', we lose. */
8465 return value;
8466 case ',':
8467 /* If an alternative lacks `g' or `r', we lose. */
8468 if (value == 0)
8469 return 0;
8470 /* Initialize for next alternative. */
8471 value = 0;
8472 break;
8473 case 'g':
8474 case 'r':
8475 /* Any general reg wins for this alternative. */
8476 if (TEST_HARD_REG_BIT (reg_class_contents[(int) GENERAL_REGS], regno))
8477 value = 1;
8478 break;
8479 default:
8480 /* Any reg in specified class wins for this alternative. */
8482 enum reg_class class = REG_CLASS_FROM_LETTER (c);
8484 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno))
8485 value = 1;
8490 /* Return the number of places FIND appears within X, but don't count
8491 an occurrence if some SET_DEST is FIND. */
8494 count_occurrences (x, find)
8495 register rtx x, find;
8497 register int i, j;
8498 register enum rtx_code code;
8499 register const char *format_ptr;
8500 int count;
8502 if (x == find)
8503 return 1;
8504 if (x == 0)
8505 return 0;
8507 code = GET_CODE (x);
8509 switch (code)
8511 case REG:
8512 case QUEUED:
8513 case CONST_INT:
8514 case CONST_DOUBLE:
8515 case SYMBOL_REF:
8516 case CODE_LABEL:
8517 case PC:
8518 case CC0:
8519 return 0;
8521 case MEM:
8522 if (GET_CODE (find) == MEM && rtx_equal_p (x, find))
8523 return 1;
8524 break;
8525 case SET:
8526 if (SET_DEST (x) == find)
8527 return count_occurrences (SET_SRC (x), find);
8528 break;
8530 default:
8531 break;
8534 format_ptr = GET_RTX_FORMAT (code);
8535 count = 0;
8537 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8539 switch (*format_ptr++)
8541 case 'e':
8542 count += count_occurrences (XEXP (x, i), find);
8543 break;
8545 case 'E':
8546 if (XVEC (x, i) != NULL)
8548 for (j = 0; j < XVECLEN (x, i); j++)
8549 count += count_occurrences (XVECEXP (x, i, j), find);
8551 break;
8554 return count;
8557 /* This array holds values which are equivalent to a hard register
8558 during reload_cse_regs. Each array element is an EXPR_LIST of
8559 values. Each time a hard register is set, we set the corresponding
8560 array element to the value. Each time a hard register is copied
8561 into memory, we add the memory location to the corresponding array
8562 element. We don't store values or memory addresses with side
8563 effects in this array.
8565 If the value is a CONST_INT, then the mode of the containing
8566 EXPR_LIST is the mode in which that CONST_INT was referenced.
8568 We sometimes clobber a specific entry in a list. In that case, we
8569 just set XEXP (list-entry, 0) to 0. */
8571 static rtx *reg_values;
8573 /* This is a preallocated REG rtx which we use as a temporary in
8574 reload_cse_invalidate_regno, so that we don't need to allocate a
8575 new one each time through a loop in that function. */
8577 static rtx invalidate_regno_rtx;
8579 /* Invalidate any entries in reg_values which depend on REGNO,
8580 including those for REGNO itself. This is called if REGNO is
8581 changing. If CLOBBER is true, then always forget anything we
8582 currently know about REGNO. MODE is the mode of the assignment to
8583 REGNO, which is used to determine how many hard registers are being
8584 changed. If MODE is VOIDmode, then only REGNO is being changed;
8585 this is used when invalidating call clobbered registers across a
8586 call. */
8588 static void
8589 reload_cse_invalidate_regno (regno, mode, clobber)
8590 int regno;
8591 enum machine_mode mode;
8592 int clobber;
8594 int endregno;
8595 register int i;
8597 /* Our callers don't always go through true_regnum; we may see a
8598 pseudo-register here from a CLOBBER or the like. We probably
8599 won't ever see a pseudo-register that has a real register number,
8600 for we check anyhow for safety. */
8601 if (regno >= FIRST_PSEUDO_REGISTER)
8602 regno = reg_renumber[regno];
8603 if (regno < 0)
8604 return;
8606 if (mode == VOIDmode)
8607 endregno = regno + 1;
8608 else
8609 endregno = regno + HARD_REGNO_NREGS (regno, mode);
8611 if (clobber)
8612 for (i = regno; i < endregno; i++)
8613 reg_values[i] = 0;
8615 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8617 rtx x;
8619 for (x = reg_values[i]; x; x = XEXP (x, 1))
8621 if (XEXP (x, 0) != 0
8622 && refers_to_regno_p (regno, endregno, XEXP (x, 0), NULL_PTR))
8624 /* If this is the only entry on the list, clear
8625 reg_values[i]. Otherwise, just clear this entry on
8626 the list. */
8627 if (XEXP (x, 1) == 0 && x == reg_values[i])
8629 reg_values[i] = 0;
8630 break;
8632 XEXP (x, 0) = 0;
8637 /* We must look at earlier registers, in case REGNO is part of a
8638 multi word value but is not the first register. If an earlier
8639 register has a value in a mode which overlaps REGNO, then we must
8640 invalidate that earlier register. Note that we do not need to
8641 check REGNO or later registers (we must not check REGNO itself,
8642 because we would incorrectly conclude that there was a conflict). */
8644 for (i = 0; i < regno; i++)
8646 rtx x;
8648 for (x = reg_values[i]; x; x = XEXP (x, 1))
8650 if (XEXP (x, 0) != 0)
8652 PUT_MODE (invalidate_regno_rtx, GET_MODE (x));
8653 REGNO (invalidate_regno_rtx) = i;
8654 if (refers_to_regno_p (regno, endregno, invalidate_regno_rtx,
8655 NULL_PTR))
8657 reload_cse_invalidate_regno (i, VOIDmode, 1);
8658 break;
8665 /* The memory at address MEM_BASE is being changed.
8666 Return whether this change will invalidate VAL. */
8668 static int
8669 reload_cse_mem_conflict_p (mem_base, val)
8670 rtx mem_base;
8671 rtx val;
8673 enum rtx_code code;
8674 const char *fmt;
8675 int i;
8677 code = GET_CODE (val);
8678 switch (code)
8680 /* Get rid of a few simple cases quickly. */
8681 case REG:
8682 case PC:
8683 case CC0:
8684 case SCRATCH:
8685 case CONST:
8686 case CONST_INT:
8687 case CONST_DOUBLE:
8688 case SYMBOL_REF:
8689 case LABEL_REF:
8690 return 0;
8692 case MEM:
8693 if (GET_MODE (mem_base) == BLKmode
8694 || GET_MODE (val) == BLKmode)
8695 return 1;
8696 if (anti_dependence (val, mem_base))
8697 return 1;
8698 /* The address may contain nested MEMs. */
8699 break;
8701 default:
8702 break;
8705 fmt = GET_RTX_FORMAT (code);
8707 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8709 if (fmt[i] == 'e')
8711 if (reload_cse_mem_conflict_p (mem_base, XEXP (val, i)))
8712 return 1;
8714 else if (fmt[i] == 'E')
8716 int j;
8718 for (j = 0; j < XVECLEN (val, i); j++)
8719 if (reload_cse_mem_conflict_p (mem_base, XVECEXP (val, i, j)))
8720 return 1;
8724 return 0;
8727 /* Invalidate any entries in reg_values which are changed because of a
8728 store to MEM_RTX. If this is called because of a non-const call
8729 instruction, MEM_RTX is (mem:BLK const0_rtx). */
8731 static void
8732 reload_cse_invalidate_mem (mem_rtx)
8733 rtx mem_rtx;
8735 register int i;
8737 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8739 rtx x;
8741 for (x = reg_values[i]; x; x = XEXP (x, 1))
8743 if (XEXP (x, 0) != 0
8744 && reload_cse_mem_conflict_p (mem_rtx, XEXP (x, 0)))
8746 /* If this is the only entry on the list, clear
8747 reg_values[i]. Otherwise, just clear this entry on
8748 the list. */
8749 if (XEXP (x, 1) == 0 && x == reg_values[i])
8751 reg_values[i] = 0;
8752 break;
8754 XEXP (x, 0) = 0;
8760 /* Invalidate DEST, which is being assigned to or clobbered. The
8761 second parameter exists so that this function can be passed to
8762 note_stores; it is ignored. */
8764 static void
8765 reload_cse_invalidate_rtx (dest, ignore)
8766 rtx dest;
8767 rtx ignore ATTRIBUTE_UNUSED;
8769 while (GET_CODE (dest) == STRICT_LOW_PART
8770 || GET_CODE (dest) == SIGN_EXTRACT
8771 || GET_CODE (dest) == ZERO_EXTRACT
8772 || GET_CODE (dest) == SUBREG)
8773 dest = XEXP (dest, 0);
8775 if (GET_CODE (dest) == REG)
8776 reload_cse_invalidate_regno (REGNO (dest), GET_MODE (dest), 1);
8777 else if (GET_CODE (dest) == MEM)
8778 reload_cse_invalidate_mem (dest);
8781 /* Do a very simple CSE pass over the hard registers.
8783 This function detects no-op moves where we happened to assign two
8784 different pseudo-registers to the same hard register, and then
8785 copied one to the other. Reload will generate a useless
8786 instruction copying a register to itself.
8788 This function also detects cases where we load a value from memory
8789 into two different registers, and (if memory is more expensive than
8790 registers) changes it to simply copy the first register into the
8791 second register.
8793 Another optimization is performed that scans the operands of each
8794 instruction to see whether the value is already available in a
8795 hard register. It then replaces the operand with the hard register
8796 if possible, much like an optional reload would. */
8798 static void
8799 reload_cse_regs_1 (first)
8800 rtx first;
8802 char *firstobj;
8803 rtx callmem;
8804 register int i;
8805 rtx insn;
8807 init_alias_analysis ();
8809 reg_values = (rtx *) alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
8810 bzero ((char *)reg_values, FIRST_PSEUDO_REGISTER * sizeof (rtx));
8812 /* Create our EXPR_LIST structures on reload_obstack, so that we can
8813 free them when we are done. */
8814 push_obstacks (&reload_obstack, &reload_obstack);
8815 firstobj = (char *) obstack_alloc (&reload_obstack, 0);
8817 /* We pass this to reload_cse_invalidate_mem to invalidate all of
8818 memory for a non-const call instruction. */
8819 callmem = gen_rtx_MEM (BLKmode, const0_rtx);
8821 /* This is used in reload_cse_invalidate_regno to avoid consing a
8822 new REG in a loop in that function. */
8823 invalidate_regno_rtx = gen_rtx_REG (VOIDmode, 0);
8825 for (insn = first; insn; insn = NEXT_INSN (insn))
8827 rtx body;
8829 if (GET_CODE (insn) == CODE_LABEL)
8831 /* Forget all the register values at a code label. We don't
8832 try to do anything clever around jumps. */
8833 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8834 reg_values[i] = 0;
8836 continue;
8839 #ifdef NON_SAVING_SETJMP
8840 if (NON_SAVING_SETJMP && GET_CODE (insn) == NOTE
8841 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
8843 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8844 reg_values[i] = 0;
8846 continue;
8848 #endif
8850 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
8851 continue;
8853 /* If this is a call instruction, forget anything stored in a
8854 call clobbered register, or, if this is not a const call, in
8855 memory. */
8856 if (GET_CODE (insn) == CALL_INSN)
8858 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8859 if (call_used_regs[i])
8860 reload_cse_invalidate_regno (i, VOIDmode, 1);
8862 if (! CONST_CALL_P (insn))
8863 reload_cse_invalidate_mem (callmem);
8867 /* Forget all the register values at a volatile asm. */
8868 if (GET_CODE (insn) == INSN
8869 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
8870 && MEM_VOLATILE_P (PATTERN (insn)))
8871 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8872 reg_values[i] = 0;
8874 body = PATTERN (insn);
8875 if (GET_CODE (body) == SET)
8877 int count = 0;
8878 if (reload_cse_noop_set_p (body, insn))
8880 /* If this sets the return value of the function, we must keep
8881 a USE around, in case this is in a different basic block
8882 than the final USE. Otherwise, we could loose important
8883 register lifeness information on SMALL_REGISTER_CLASSES
8884 machines, where return registers might be used as spills:
8885 subsequent passes assume that spill registers are dead at
8886 the end of a basic block. */
8887 if (REG_FUNCTION_VALUE_P (SET_DEST (body)))
8889 pop_obstacks ();
8890 PATTERN (insn) = gen_rtx_USE (VOIDmode, SET_DEST (body));
8891 INSN_CODE (insn) = -1;
8892 REG_NOTES (insn) = NULL_RTX;
8893 push_obstacks (&reload_obstack, &reload_obstack);
8895 else
8897 PUT_CODE (insn, NOTE);
8898 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8899 NOTE_SOURCE_FILE (insn) = 0;
8902 /* We're done with this insn. */
8903 continue;
8906 /* It's not a no-op, but we can try to simplify it. */
8907 count += reload_cse_simplify_set (body, insn);
8909 if (count > 0)
8910 apply_change_group ();
8911 else
8912 reload_cse_simplify_operands (insn);
8914 reload_cse_record_set (body, body);
8916 else if (GET_CODE (body) == PARALLEL)
8918 int count = 0;
8919 rtx value = NULL_RTX;
8921 /* If every action in a PARALLEL is a noop, we can delete
8922 the entire PARALLEL. */
8923 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8925 rtx part = XVECEXP (body, 0, i);
8926 if (GET_CODE (part) == SET)
8928 if (! reload_cse_noop_set_p (part, insn))
8929 break;
8930 if (REG_FUNCTION_VALUE_P (SET_DEST (part)))
8932 if (value)
8933 break;
8934 value = SET_DEST (part);
8937 else if (GET_CODE (part) != CLOBBER)
8938 break;
8940 if (i < 0)
8942 if (value)
8944 pop_obstacks ();
8945 PATTERN (insn) = gen_rtx_USE (VOIDmode, value);
8946 INSN_CODE (insn) = -1;
8947 REG_NOTES (insn) = NULL_RTX;
8948 push_obstacks (&reload_obstack, &reload_obstack);
8950 else
8952 PUT_CODE (insn, NOTE);
8953 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8954 NOTE_SOURCE_FILE (insn) = 0;
8957 /* We're done with this insn. */
8958 continue;
8961 /* It's not a no-op, but we can try to simplify it. */
8962 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8963 if (GET_CODE (XVECEXP (body, 0, i)) == SET)
8964 count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
8966 if (count > 0)
8967 apply_change_group ();
8968 else
8969 reload_cse_simplify_operands (insn);
8971 /* Look through the PARALLEL and record the values being
8972 set, if possible. Also handle any CLOBBERs. */
8973 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8975 rtx x = XVECEXP (body, 0, i);
8977 if (GET_CODE (x) == SET)
8978 reload_cse_record_set (x, body);
8979 else
8980 note_stores (x, reload_cse_invalidate_rtx);
8983 else
8984 note_stores (body, reload_cse_invalidate_rtx);
8986 #ifdef AUTO_INC_DEC
8987 /* Clobber any registers which appear in REG_INC notes. We
8988 could keep track of the changes to their values, but it is
8989 unlikely to help. */
8991 rtx x;
8993 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
8994 if (REG_NOTE_KIND (x) == REG_INC)
8995 reload_cse_invalidate_rtx (XEXP (x, 0), NULL_RTX);
8997 #endif
8999 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
9000 after we have processed the insn. */
9001 if (GET_CODE (insn) == CALL_INSN)
9003 rtx x;
9005 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
9006 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
9007 reload_cse_invalidate_rtx (XEXP (XEXP (x, 0), 0), NULL_RTX);
9011 /* Free all the temporary structures we created, and go back to the
9012 regular obstacks. */
9013 obstack_free (&reload_obstack, firstobj);
9014 pop_obstacks ();
9017 /* Call cse / combine like post-reload optimization phases.
9018 FIRST is the first instruction. */
9019 void
9020 reload_cse_regs (first)
9021 rtx first;
9023 reload_cse_regs_1 (first);
9024 reload_combine ();
9025 reload_cse_move2add (first);
9026 if (flag_expensive_optimizations)
9027 reload_cse_regs_1 (first);
9030 /* Return whether the values known for REGNO are equal to VAL. MODE
9031 is the mode of the object that VAL is being copied to; this matters
9032 if VAL is a CONST_INT. */
9034 static int
9035 reload_cse_regno_equal_p (regno, val, mode)
9036 int regno;
9037 rtx val;
9038 enum machine_mode mode;
9040 rtx x;
9042 if (val == 0)
9043 return 0;
9045 for (x = reg_values[regno]; x; x = XEXP (x, 1))
9046 if (XEXP (x, 0) != 0
9047 && rtx_equal_p (XEXP (x, 0), val)
9048 && (! flag_float_store || GET_CODE (XEXP (x, 0)) != MEM
9049 || GET_MODE_CLASS (GET_MODE (x)) != MODE_FLOAT)
9050 && (GET_CODE (val) != CONST_INT
9051 || mode == GET_MODE (x)
9052 || (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
9053 /* On a big endian machine if the value spans more than
9054 one register then this register holds the high part of
9055 it and we can't use it.
9057 ??? We should also compare with the high part of the
9058 value. */
9059 && !(WORDS_BIG_ENDIAN
9060 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
9061 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
9062 GET_MODE_BITSIZE (GET_MODE (x))))))
9063 return 1;
9065 return 0;
9068 /* See whether a single set is a noop. SET is the set instruction we
9069 are should check, and INSN is the instruction from which it came. */
9071 static int
9072 reload_cse_noop_set_p (set, insn)
9073 rtx set;
9074 rtx insn ATTRIBUTE_UNUSED;
9076 rtx src, dest;
9077 enum machine_mode dest_mode;
9078 int dreg, sreg;
9079 int ret;
9081 src = SET_SRC (set);
9082 dest = SET_DEST (set);
9083 dest_mode = GET_MODE (dest);
9085 if (side_effects_p (src))
9086 return 0;
9088 dreg = true_regnum (dest);
9089 sreg = true_regnum (src);
9091 /* Check for setting a register to itself. In this case, we don't
9092 have to worry about REG_DEAD notes. */
9093 if (dreg >= 0 && dreg == sreg)
9094 return 1;
9096 ret = 0;
9097 if (dreg >= 0)
9099 /* Check for setting a register to itself. */
9100 if (dreg == sreg)
9101 ret = 1;
9103 /* Check for setting a register to a value which we already know
9104 is in the register. */
9105 else if (reload_cse_regno_equal_p (dreg, src, dest_mode))
9106 ret = 1;
9108 /* Check for setting a register DREG to another register SREG
9109 where SREG is equal to a value which is already in DREG. */
9110 else if (sreg >= 0)
9112 rtx x;
9114 for (x = reg_values[sreg]; x; x = XEXP (x, 1))
9116 rtx tmp;
9118 if (XEXP (x, 0) == 0)
9119 continue;
9121 if (dest_mode == GET_MODE (x))
9122 tmp = XEXP (x, 0);
9123 else if (GET_MODE_BITSIZE (dest_mode)
9124 < GET_MODE_BITSIZE (GET_MODE (x)))
9125 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
9126 else
9127 continue;
9129 if (tmp
9130 && reload_cse_regno_equal_p (dreg, tmp, dest_mode))
9132 ret = 1;
9133 break;
9138 else if (GET_CODE (dest) == MEM)
9140 /* Check for storing a register to memory when we know that the
9141 register is equivalent to the memory location. */
9142 if (sreg >= 0
9143 && reload_cse_regno_equal_p (sreg, dest, dest_mode)
9144 && ! side_effects_p (dest))
9145 ret = 1;
9148 return ret;
9151 /* Try to simplify a single SET instruction. SET is the set pattern.
9152 INSN is the instruction it came from.
9153 This function only handles one case: if we set a register to a value
9154 which is not a register, we try to find that value in some other register
9155 and change the set into a register copy. */
9157 static int
9158 reload_cse_simplify_set (set, insn)
9159 rtx set;
9160 rtx insn;
9162 int dreg;
9163 rtx src;
9164 enum machine_mode dest_mode;
9165 enum reg_class dclass;
9166 register int i;
9168 dreg = true_regnum (SET_DEST (set));
9169 if (dreg < 0)
9170 return 0;
9172 src = SET_SRC (set);
9173 if (side_effects_p (src) || true_regnum (src) >= 0)
9174 return 0;
9176 dclass = REGNO_REG_CLASS (dreg);
9178 /* If memory loads are cheaper than register copies, don't change them. */
9179 if (GET_CODE (src) == MEM
9180 && MEMORY_MOVE_COST (GET_MODE (src), dclass, 1) < 2)
9181 return 0;
9183 /* If the constant is cheaper than a register, don't change it. */
9184 if (CONSTANT_P (src)
9185 && rtx_cost (src, SET) < 2)
9186 return 0;
9188 dest_mode = GET_MODE (SET_DEST (set));
9189 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
9191 if (i != dreg
9192 && REGISTER_MOVE_COST (REGNO_REG_CLASS (i), dclass) == 2
9193 && reload_cse_regno_equal_p (i, src, dest_mode))
9195 int validated;
9197 /* Pop back to the real obstacks while changing the insn. */
9198 pop_obstacks ();
9200 validated = validate_change (insn, &SET_SRC (set),
9201 gen_rtx_REG (dest_mode, i), 1);
9203 /* Go back to the obstack we are using for temporary
9204 storage. */
9205 push_obstacks (&reload_obstack, &reload_obstack);
9207 if (validated)
9208 return 1;
9211 return 0;
9214 /* Try to replace operands in INSN with equivalent values that are already
9215 in registers. This can be viewed as optional reloading.
9217 For each non-register operand in the insn, see if any hard regs are
9218 known to be equivalent to that operand. Record the alternatives which
9219 can accept these hard registers. Among all alternatives, select the
9220 ones which are better or equal to the one currently matching, where
9221 "better" is in terms of '?' and '!' constraints. Among the remaining
9222 alternatives, select the one which replaces most operands with
9223 hard registers. */
9225 static int
9226 reload_cse_simplify_operands (insn)
9227 rtx insn;
9229 int i,j;
9231 const char *constraints[MAX_RECOG_OPERANDS];
9233 /* Vector recording how bad an alternative is. */
9234 int *alternative_reject;
9235 /* Vector recording how many registers can be introduced by choosing
9236 this alternative. */
9237 int *alternative_nregs;
9238 /* Array of vectors recording, for each operand and each alternative,
9239 which hard register to substitute, or -1 if the operand should be
9240 left as it is. */
9241 int *op_alt_regno[MAX_RECOG_OPERANDS];
9242 /* Array of alternatives, sorted in order of decreasing desirability. */
9243 int *alternative_order;
9244 rtx reg = gen_rtx_REG (VOIDmode, -1);
9246 extract_insn (insn);
9248 if (recog_data.n_alternatives == 0 || recog_data.n_operands == 0)
9249 return 0;
9251 /* Figure out which alternative currently matches. */
9252 if (! constrain_operands (1))
9253 fatal_insn_not_found (insn);
9255 alternative_reject = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9256 alternative_nregs = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9257 alternative_order = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9258 bzero ((char *)alternative_reject, recog_data.n_alternatives * sizeof (int));
9259 bzero ((char *)alternative_nregs, recog_data.n_alternatives * sizeof (int));
9261 for (i = 0; i < recog_data.n_operands; i++)
9263 enum machine_mode mode;
9264 int regno;
9265 const char *p;
9267 op_alt_regno[i] = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9268 for (j = 0; j < recog_data.n_alternatives; j++)
9269 op_alt_regno[i][j] = -1;
9271 p = constraints[i] = recog_data.constraints[i];
9272 mode = recog_data.operand_mode[i];
9274 /* Add the reject values for each alternative given by the constraints
9275 for this operand. */
9276 j = 0;
9277 while (*p != '\0')
9279 char c = *p++;
9280 if (c == ',')
9281 j++;
9282 else if (c == '?')
9283 alternative_reject[j] += 3;
9284 else if (c == '!')
9285 alternative_reject[j] += 300;
9288 /* We won't change operands which are already registers. We
9289 also don't want to modify output operands. */
9290 regno = true_regnum (recog_data.operand[i]);
9291 if (regno >= 0
9292 || constraints[i][0] == '='
9293 || constraints[i][0] == '+')
9294 continue;
9296 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9298 int class = (int) NO_REGS;
9300 if (! reload_cse_regno_equal_p (regno, recog_data.operand[i], mode))
9301 continue;
9303 REGNO (reg) = regno;
9304 PUT_MODE (reg, mode);
9306 /* We found a register equal to this operand. Now look for all
9307 alternatives that can accept this register and have not been
9308 assigned a register they can use yet. */
9309 j = 0;
9310 p = constraints[i];
9311 for (;;)
9313 char c = *p++;
9315 switch (c)
9317 case '=': case '+': case '?':
9318 case '#': case '&': case '!':
9319 case '*': case '%':
9320 case '0': case '1': case '2': case '3': case '4':
9321 case '5': case '6': case '7': case '8': case '9':
9322 case 'm': case '<': case '>': case 'V': case 'o':
9323 case 'E': case 'F': case 'G': case 'H':
9324 case 's': case 'i': case 'n':
9325 case 'I': case 'J': case 'K': case 'L':
9326 case 'M': case 'N': case 'O': case 'P':
9327 #ifdef EXTRA_CONSTRAINT
9328 case 'Q': case 'R': case 'S': case 'T': case 'U':
9329 #endif
9330 case 'p': case 'X':
9331 /* These don't say anything we care about. */
9332 break;
9334 case 'g': case 'r':
9335 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
9336 break;
9338 default:
9339 class
9340 = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER ((unsigned char)c)];
9341 break;
9343 case ',': case '\0':
9344 /* See if REGNO fits this alternative, and set it up as the
9345 replacement register if we don't have one for this
9346 alternative yet and the operand being replaced is not
9347 a cheap CONST_INT. */
9348 if (op_alt_regno[i][j] == -1
9349 && reg_fits_class_p (reg, class, 0, mode)
9350 && (GET_CODE (recog_data.operand[i]) != CONST_INT
9351 || (rtx_cost (recog_data.operand[i], SET)
9352 > rtx_cost (reg, SET))))
9354 alternative_nregs[j]++;
9355 op_alt_regno[i][j] = regno;
9357 j++;
9358 break;
9361 if (c == '\0')
9362 break;
9367 /* Record all alternatives which are better or equal to the currently
9368 matching one in the alternative_order array. */
9369 for (i = j = 0; i < recog_data.n_alternatives; i++)
9370 if (alternative_reject[i] <= alternative_reject[which_alternative])
9371 alternative_order[j++] = i;
9372 recog_data.n_alternatives = j;
9374 /* Sort it. Given a small number of alternatives, a dumb algorithm
9375 won't hurt too much. */
9376 for (i = 0; i < recog_data.n_alternatives - 1; i++)
9378 int best = i;
9379 int best_reject = alternative_reject[alternative_order[i]];
9380 int best_nregs = alternative_nregs[alternative_order[i]];
9381 int tmp;
9383 for (j = i + 1; j < recog_data.n_alternatives; j++)
9385 int this_reject = alternative_reject[alternative_order[j]];
9386 int this_nregs = alternative_nregs[alternative_order[j]];
9388 if (this_reject < best_reject
9389 || (this_reject == best_reject && this_nregs < best_nregs))
9391 best = j;
9392 best_reject = this_reject;
9393 best_nregs = this_nregs;
9397 tmp = alternative_order[best];
9398 alternative_order[best] = alternative_order[i];
9399 alternative_order[i] = tmp;
9402 /* Substitute the operands as determined by op_alt_regno for the best
9403 alternative. */
9404 j = alternative_order[0];
9406 /* Pop back to the real obstacks while changing the insn. */
9407 pop_obstacks ();
9409 for (i = 0; i < recog_data.n_operands; i++)
9411 enum machine_mode mode = recog_data.operand_mode[i];
9412 if (op_alt_regno[i][j] == -1)
9413 continue;
9415 validate_change (insn, recog_data.operand_loc[i],
9416 gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
9419 for (i = recog_data.n_dups - 1; i >= 0; i--)
9421 int op = recog_data.dup_num[i];
9422 enum machine_mode mode = recog_data.operand_mode[op];
9424 if (op_alt_regno[op][j] == -1)
9425 continue;
9427 validate_change (insn, recog_data.dup_loc[i],
9428 gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
9431 /* Go back to the obstack we are using for temporary
9432 storage. */
9433 push_obstacks (&reload_obstack, &reload_obstack);
9435 return apply_change_group ();
9438 /* These two variables are used to pass information from
9439 reload_cse_record_set to reload_cse_check_clobber. */
9441 static int reload_cse_check_clobbered;
9442 static rtx reload_cse_check_src;
9444 /* See if DEST overlaps with RELOAD_CSE_CHECK_SRC. If it does, set
9445 RELOAD_CSE_CHECK_CLOBBERED. This is called via note_stores. The
9446 second argument, which is passed by note_stores, is ignored. */
9448 static void
9449 reload_cse_check_clobber (dest, ignore)
9450 rtx dest;
9451 rtx ignore ATTRIBUTE_UNUSED;
9453 if (reg_overlap_mentioned_p (dest, reload_cse_check_src))
9454 reload_cse_check_clobbered = 1;
9457 /* Record the result of a SET instruction. SET is the set pattern.
9458 BODY is the pattern of the insn that it came from. */
9460 static void
9461 reload_cse_record_set (set, body)
9462 rtx set;
9463 rtx body;
9465 rtx dest, src, x;
9466 int dreg, sreg;
9467 enum machine_mode dest_mode;
9469 dest = SET_DEST (set);
9470 src = SET_SRC (set);
9471 dreg = true_regnum (dest);
9472 sreg = true_regnum (src);
9473 dest_mode = GET_MODE (dest);
9475 /* Some machines don't define AUTO_INC_DEC, but they still use push
9476 instructions. We need to catch that case here in order to
9477 invalidate the stack pointer correctly. Note that invalidating
9478 the stack pointer is different from invalidating DEST. */
9479 x = dest;
9480 while (GET_CODE (x) == SUBREG
9481 || GET_CODE (x) == ZERO_EXTRACT
9482 || GET_CODE (x) == SIGN_EXTRACT
9483 || GET_CODE (x) == STRICT_LOW_PART)
9484 x = XEXP (x, 0);
9485 if (push_operand (x, GET_MODE (x)))
9487 reload_cse_invalidate_rtx (stack_pointer_rtx, NULL_RTX);
9488 reload_cse_invalidate_rtx (dest, NULL_RTX);
9489 return;
9492 /* We can only handle an assignment to a register, or a store of a
9493 register to a memory location. For other cases, we just clobber
9494 the destination. We also have to just clobber if there are side
9495 effects in SRC or DEST. */
9496 if ((dreg < 0 && GET_CODE (dest) != MEM)
9497 || side_effects_p (src)
9498 || side_effects_p (dest))
9500 reload_cse_invalidate_rtx (dest, NULL_RTX);
9501 return;
9504 #ifdef HAVE_cc0
9505 /* We don't try to handle values involving CC, because it's a pain
9506 to keep track of when they have to be invalidated. */
9507 if (reg_mentioned_p (cc0_rtx, src)
9508 || reg_mentioned_p (cc0_rtx, dest))
9510 reload_cse_invalidate_rtx (dest, NULL_RTX);
9511 return;
9513 #endif
9515 /* If BODY is a PARALLEL, then we need to see whether the source of
9516 SET is clobbered by some other instruction in the PARALLEL. */
9517 if (GET_CODE (body) == PARALLEL)
9519 int i;
9521 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
9523 rtx x;
9525 x = XVECEXP (body, 0, i);
9526 if (x == set)
9527 continue;
9529 reload_cse_check_clobbered = 0;
9530 reload_cse_check_src = src;
9531 note_stores (x, reload_cse_check_clobber);
9532 if (reload_cse_check_clobbered)
9534 reload_cse_invalidate_rtx (dest, NULL_RTX);
9535 return;
9540 if (dreg >= 0)
9542 int i;
9544 /* This is an assignment to a register. Update the value we
9545 have stored for the register. */
9546 if (sreg >= 0)
9548 rtx x;
9550 /* This is a copy from one register to another. Any values
9551 which were valid for SREG are now valid for DREG. If the
9552 mode changes, we use gen_lowpart_common to extract only
9553 the part of the value that is copied. */
9554 reg_values[dreg] = 0;
9555 for (x = reg_values[sreg]; x; x = XEXP (x, 1))
9557 rtx tmp;
9559 if (XEXP (x, 0) == 0)
9560 continue;
9561 if (dest_mode == GET_MODE (XEXP (x, 0)))
9562 tmp = XEXP (x, 0);
9563 else if (GET_MODE_BITSIZE (dest_mode)
9564 > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
9565 continue;
9566 else
9567 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
9568 if (tmp)
9569 reg_values[dreg] = gen_rtx_EXPR_LIST (dest_mode, tmp,
9570 reg_values[dreg]);
9573 else
9574 reg_values[dreg] = gen_rtx_EXPR_LIST (dest_mode, src, NULL_RTX);
9576 /* We've changed DREG, so invalidate any values held by other
9577 registers that depend upon it. */
9578 reload_cse_invalidate_regno (dreg, dest_mode, 0);
9580 /* If this assignment changes more than one hard register,
9581 forget anything we know about the others. */
9582 for (i = 1; i < HARD_REGNO_NREGS (dreg, dest_mode); i++)
9583 reg_values[dreg + i] = 0;
9585 else if (GET_CODE (dest) == MEM)
9587 /* Invalidate conflicting memory locations. */
9588 reload_cse_invalidate_mem (dest);
9590 /* If we're storing a register to memory, add DEST to the list
9591 in REG_VALUES. */
9592 if (sreg >= 0 && ! side_effects_p (dest))
9593 reg_values[sreg] = gen_rtx_EXPR_LIST (dest_mode, dest,
9594 reg_values[sreg]);
9596 else
9598 /* We should have bailed out earlier. */
9599 abort ();
9603 /* If reload couldn't use reg+reg+offset addressing, try to use reg+reg
9604 addressing now.
9605 This code might also be useful when reload gave up on reg+reg addresssing
9606 because of clashes between the return register and INDEX_REG_CLASS. */
9608 /* The maximum number of uses of a register we can keep track of to
9609 replace them with reg+reg addressing. */
9610 #define RELOAD_COMBINE_MAX_USES 6
9612 /* INSN is the insn where a register has ben used, and USEP points to the
9613 location of the register within the rtl. */
9614 struct reg_use { rtx insn, *usep; };
9616 /* If the register is used in some unknown fashion, USE_INDEX is negative.
9617 If it is dead, USE_INDEX is RELOAD_COMBINE_MAX_USES, and STORE_RUID
9618 indicates where it becomes live again.
9619 Otherwise, USE_INDEX is the index of the last encountered use of the
9620 register (which is first among these we have seen since we scan backwards),
9621 OFFSET contains the constant offset that is added to the register in
9622 all encountered uses, and USE_RUID indicates the first encountered, i.e.
9623 last, of these uses.
9624 STORE_RUID is always meaningful if we only want to use a value in a
9625 register in a different place: it denotes the next insn in the insn
9626 stream (i.e. the last ecountered) that sets or clobbers the register. */
9627 static struct
9629 struct reg_use reg_use[RELOAD_COMBINE_MAX_USES];
9630 int use_index;
9631 rtx offset;
9632 int store_ruid;
9633 int use_ruid;
9634 } reg_state[FIRST_PSEUDO_REGISTER];
9636 /* Reverse linear uid. This is increased in reload_combine while scanning
9637 the instructions from last to first. It is used to set last_label_ruid
9638 and the store_ruid / use_ruid fields in reg_state. */
9639 static int reload_combine_ruid;
9641 #define LABEL_LIVE(LABEL) \
9642 (label_live[CODE_LABEL_NUMBER (LABEL) - min_labelno])
9644 static void
9645 reload_combine ()
9647 rtx insn, set;
9648 int first_index_reg = 1, last_index_reg = 0;
9649 int i;
9650 int last_label_ruid;
9651 int min_labelno, n_labels;
9652 HARD_REG_SET ever_live_at_start, *label_live;
9654 /* If reg+reg can be used in offsetable memory adresses, the main chunk of
9655 reload has already used it where appropriate, so there is no use in
9656 trying to generate it now. */
9657 if (double_reg_address_ok && INDEX_REG_CLASS != NO_REGS)
9658 return;
9660 /* To avoid wasting too much time later searching for an index register,
9661 determine the minimum and maximum index register numbers. */
9662 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9664 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i))
9666 if (! last_index_reg)
9667 last_index_reg = i;
9668 first_index_reg = i;
9671 /* If no index register is available, we can quit now. */
9672 if (first_index_reg > last_index_reg)
9673 return;
9675 /* Set up LABEL_LIVE and EVER_LIVE_AT_START. The register lifetime
9676 information is a bit fuzzy immediately after reload, but it's
9677 still good enough to determine which registers are live at a jump
9678 destination. */
9679 min_labelno = get_first_label_num ();
9680 n_labels = max_label_num () - min_labelno;
9681 label_live = (HARD_REG_SET *) xmalloc (n_labels * sizeof (HARD_REG_SET));
9682 CLEAR_HARD_REG_SET (ever_live_at_start);
9683 for (i = n_basic_blocks - 1; i >= 0; i--)
9685 insn = BLOCK_HEAD (i);
9686 if (GET_CODE (insn) == CODE_LABEL)
9688 HARD_REG_SET live;
9690 REG_SET_TO_HARD_REG_SET (live, BASIC_BLOCK (i)->global_live_at_start);
9691 compute_use_by_pseudos (&live, BASIC_BLOCK (i)->global_live_at_start);
9692 COPY_HARD_REG_SET (LABEL_LIVE (insn), live);
9693 IOR_HARD_REG_SET (ever_live_at_start, live);
9697 /* Initialize last_label_ruid, reload_combine_ruid and reg_state. */
9698 last_label_ruid = reload_combine_ruid = 0;
9699 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9701 reg_state[i].store_ruid = reload_combine_ruid;
9702 if (fixed_regs[i])
9703 reg_state[i].use_index = -1;
9704 else
9705 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9708 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
9710 rtx note;
9712 /* We cannot do our optimization across labels. Invalidating all the use
9713 information we have would be costly, so we just note where the label
9714 is and then later disable any optimization that would cross it. */
9715 if (GET_CODE (insn) == CODE_LABEL)
9716 last_label_ruid = reload_combine_ruid;
9717 if (GET_CODE (insn) == BARRIER)
9719 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9720 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9722 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
9723 continue;
9724 reload_combine_ruid++;
9726 /* Look for (set (REGX) (CONST_INT))
9727 (set (REGX) (PLUS (REGX) (REGY)))
9729 ... (MEM (REGX)) ...
9730 and convert it to
9731 (set (REGZ) (CONST_INT))
9733 ... (MEM (PLUS (REGZ) (REGY)))... .
9735 First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
9736 and that we know all uses of REGX before it dies. */
9737 set = single_set (insn);
9738 if (set != NULL_RTX
9739 && GET_CODE (SET_DEST (set)) == REG
9740 && (HARD_REGNO_NREGS (REGNO (SET_DEST (set)),
9741 GET_MODE (SET_DEST (set)))
9742 == 1)
9743 && GET_CODE (SET_SRC (set)) == PLUS
9744 && GET_CODE (XEXP (SET_SRC (set), 1)) == REG
9745 && rtx_equal_p (XEXP (SET_SRC (set), 0), SET_DEST (set))
9746 && last_label_ruid < reg_state[REGNO (SET_DEST (set))].use_ruid)
9748 rtx reg = SET_DEST (set);
9749 rtx plus = SET_SRC (set);
9750 rtx base = XEXP (plus, 1);
9751 rtx prev = prev_nonnote_insn (insn);
9752 rtx prev_set = prev ? single_set (prev) : NULL_RTX;
9753 int regno = REGNO (reg);
9754 rtx const_reg = NULL_RTX;
9755 rtx reg_sum = NULL_RTX;
9757 /* Now, we need an index register.
9758 We'll set index_reg to this index register, const_reg to the
9759 register that is to be loaded with the constant
9760 (denoted as REGZ in the substitution illustration above),
9761 and reg_sum to the register-register that we want to use to
9762 substitute uses of REG (typically in MEMs) with.
9763 First check REG and BASE for being index registers;
9764 we can use them even if they are not dead. */
9765 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], regno)
9766 || TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
9767 REGNO (base)))
9769 const_reg = reg;
9770 reg_sum = plus;
9772 else
9774 /* Otherwise, look for a free index register. Since we have
9775 checked above that neiter REG nor BASE are index registers,
9776 if we find anything at all, it will be different from these
9777 two registers. */
9778 for (i = first_index_reg; i <= last_index_reg; i++)
9780 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i)
9781 && reg_state[i].use_index == RELOAD_COMBINE_MAX_USES
9782 && reg_state[i].store_ruid <= reg_state[regno].use_ruid
9783 && HARD_REGNO_NREGS (i, GET_MODE (reg)) == 1)
9785 rtx index_reg = gen_rtx_REG (GET_MODE (reg), i);
9786 const_reg = index_reg;
9787 reg_sum = gen_rtx_PLUS (GET_MODE (reg), index_reg, base);
9788 break;
9792 /* Check that PREV_SET is indeed (set (REGX) (CONST_INT)) and that
9793 (REGY), i.e. BASE, is not clobbered before the last use we'll
9794 create. */
9795 if (prev_set
9796 && GET_CODE (SET_SRC (prev_set)) == CONST_INT
9797 && rtx_equal_p (SET_DEST (prev_set), reg)
9798 && reg_state[regno].use_index >= 0
9799 && reg_state[REGNO (base)].store_ruid <= reg_state[regno].use_ruid
9800 && reg_sum)
9802 int i;
9804 /* Change destination register and - if necessary - the
9805 constant value in PREV, the constant loading instruction. */
9806 validate_change (prev, &SET_DEST (prev_set), const_reg, 1);
9807 if (reg_state[regno].offset != const0_rtx)
9808 validate_change (prev,
9809 &SET_SRC (prev_set),
9810 GEN_INT (INTVAL (SET_SRC (prev_set))
9811 + INTVAL (reg_state[regno].offset)),
9813 /* Now for every use of REG that we have recorded, replace REG
9814 with REG_SUM. */
9815 for (i = reg_state[regno].use_index;
9816 i < RELOAD_COMBINE_MAX_USES; i++)
9817 validate_change (reg_state[regno].reg_use[i].insn,
9818 reg_state[regno].reg_use[i].usep,
9819 reg_sum, 1);
9821 if (apply_change_group ())
9823 rtx *np;
9825 /* Delete the reg-reg addition. */
9826 PUT_CODE (insn, NOTE);
9827 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9828 NOTE_SOURCE_FILE (insn) = 0;
9830 if (reg_state[regno].offset != const0_rtx)
9832 /* Previous REG_EQUIV / REG_EQUAL notes for PREV
9833 are now invalid. */
9834 for (np = &REG_NOTES (prev); *np; )
9836 if (REG_NOTE_KIND (*np) == REG_EQUAL
9837 || REG_NOTE_KIND (*np) == REG_EQUIV)
9838 *np = XEXP (*np, 1);
9839 else
9840 np = &XEXP (*np, 1);
9843 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
9844 reg_state[REGNO (const_reg)].store_ruid = reload_combine_ruid;
9845 continue;
9849 note_stores (PATTERN (insn), reload_combine_note_store);
9850 if (GET_CODE (insn) == CALL_INSN)
9852 rtx link;
9854 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9856 if (call_used_regs[i])
9858 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9859 reg_state[i].store_ruid = reload_combine_ruid;
9862 for (link = CALL_INSN_FUNCTION_USAGE (insn); link;
9863 link = XEXP (link, 1))
9865 rtx use = XEXP (link, 0);
9866 int regno = REGNO (XEXP (use, 0));
9867 if (GET_CODE (use) == CLOBBER)
9869 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
9870 reg_state[regno].store_ruid = reload_combine_ruid;
9872 else
9873 reg_state[regno].use_index = -1;
9876 if (GET_CODE (insn) == JUMP_INSN && GET_CODE (PATTERN (insn)) != RETURN)
9878 /* Non-spill registers might be used at the call destination in
9879 some unknown fashion, so we have to mark the unknown use. */
9880 HARD_REG_SET *live;
9881 if ((condjump_p (insn) || condjump_in_parallel_p (insn))
9882 && JUMP_LABEL (insn))
9883 live = &LABEL_LIVE (JUMP_LABEL (insn));
9884 else
9885 live = &ever_live_at_start;
9886 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9888 if (TEST_HARD_REG_BIT (*live, i))
9889 reg_state[i].use_index = -1;
9892 reload_combine_note_use (&PATTERN (insn), insn);
9893 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
9895 if (REG_NOTE_KIND (note) == REG_INC
9896 && GET_CODE (XEXP (note, 0)) == REG)
9898 int regno = REGNO (XEXP (note, 0));
9900 reg_state[regno].store_ruid = reload_combine_ruid;
9901 reg_state[regno].use_index = -1;
9905 free (label_live);
9908 /* Check if DST is a register or a subreg of a register; if it is,
9909 update reg_state[regno].store_ruid and reg_state[regno].use_index
9910 accordingly. Called via note_stores from reload_combine. */
9911 static void
9912 reload_combine_note_store (dst, set)
9913 rtx dst, set;
9915 int regno = 0;
9916 int i;
9917 unsigned size = GET_MODE_SIZE (GET_MODE (dst));
9919 if (GET_CODE (dst) == SUBREG)
9921 regno = SUBREG_WORD (dst);
9922 dst = SUBREG_REG (dst);
9924 if (GET_CODE (dst) != REG)
9925 return;
9926 regno += REGNO (dst);
9928 /* note_stores might have stripped a STRICT_LOW_PART, so we have to be
9929 careful with registers / register parts that are not full words.
9931 Similarly for ZERO_EXTRACT and SIGN_EXTRACT. */
9932 if (GET_CODE (set) != SET
9933 || GET_CODE (SET_DEST (set)) == ZERO_EXTRACT
9934 || GET_CODE (SET_DEST (set)) == SIGN_EXTRACT
9935 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
9937 for (i = (size - 1) / UNITS_PER_WORD + regno; i >= regno; i--)
9939 reg_state[i].use_index = -1;
9940 reg_state[i].store_ruid = reload_combine_ruid;
9943 else
9945 for (i = (size - 1) / UNITS_PER_WORD + regno; i >= regno; i--)
9947 reg_state[i].store_ruid = reload_combine_ruid;
9948 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9953 /* XP points to a piece of rtl that has to be checked for any uses of
9954 registers.
9955 *XP is the pattern of INSN, or a part of it.
9956 Called from reload_combine, and recursively by itself. */
9957 static void
9958 reload_combine_note_use (xp, insn)
9959 rtx *xp, insn;
9961 rtx x = *xp;
9962 enum rtx_code code = x->code;
9963 const char *fmt;
9964 int i, j;
9965 rtx offset = const0_rtx; /* For the REG case below. */
9967 switch (code)
9969 case SET:
9970 if (GET_CODE (SET_DEST (x)) == REG)
9972 reload_combine_note_use (&SET_SRC (x), insn);
9973 return;
9975 break;
9977 case CLOBBER:
9978 if (GET_CODE (SET_DEST (x)) == REG)
9979 return;
9980 break;
9982 case PLUS:
9983 /* We are interested in (plus (reg) (const_int)) . */
9984 if (GET_CODE (XEXP (x, 0)) != REG || GET_CODE (XEXP (x, 1)) != CONST_INT)
9985 break;
9986 offset = XEXP (x, 1);
9987 x = XEXP (x, 0);
9988 /* Fall through. */
9989 case REG:
9991 int regno = REGNO (x);
9992 int use_index;
9994 /* Some spurious USEs of pseudo registers might remain.
9995 Just ignore them. */
9996 if (regno >= FIRST_PSEUDO_REGISTER)
9997 return;
9999 /* If this register is already used in some unknown fashion, we
10000 can't do anything.
10001 If we decrement the index from zero to -1, we can't store more
10002 uses, so this register becomes used in an unknown fashion. */
10003 use_index = --reg_state[regno].use_index;
10004 if (use_index < 0)
10005 return;
10007 if (use_index != RELOAD_COMBINE_MAX_USES - 1)
10009 /* We have found another use for a register that is already
10010 used later. Check if the offsets match; if not, mark the
10011 register as used in an unknown fashion. */
10012 if (! rtx_equal_p (offset, reg_state[regno].offset))
10014 reg_state[regno].use_index = -1;
10015 return;
10018 else
10020 /* This is the first use of this register we have seen since we
10021 marked it as dead. */
10022 reg_state[regno].offset = offset;
10023 reg_state[regno].use_ruid = reload_combine_ruid;
10025 reg_state[regno].reg_use[use_index].insn = insn;
10026 reg_state[regno].reg_use[use_index].usep = xp;
10027 return;
10030 default:
10031 break;
10034 /* Recursively process the components of X. */
10035 fmt = GET_RTX_FORMAT (code);
10036 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10038 if (fmt[i] == 'e')
10039 reload_combine_note_use (&XEXP (x, i), insn);
10040 else if (fmt[i] == 'E')
10042 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10043 reload_combine_note_use (&XVECEXP (x, i, j), insn);
10048 /* See if we can reduce the cost of a constant by replacing a move with
10049 an add. */
10050 /* We cannot do our optimization across labels. Invalidating all the
10051 information about register contents we have would be costly, so we
10052 use last_label_luid (local variable of reload_cse_move2add) to note
10053 where the label is and then later disable any optimization that would
10054 cross it.
10055 reg_offset[n] / reg_base_reg[n] / reg_mode[n] are only valid if
10056 reg_set_luid[n] is larger than last_label_luid[n] . */
10057 static int reg_set_luid[FIRST_PSEUDO_REGISTER];
10058 /* reg_offset[n] has to be CONST_INT for it and reg_base_reg[n] /
10059 reg_mode[n] to be valid.
10060 If reg_offset[n] is a CONST_INT and reg_base_reg[n] is negative, register n
10061 has been set to reg_offset[n] in mode reg_mode[n] .
10062 If reg_offset[n] is a CONST_INT and reg_base_reg[n] is non-negative,
10063 register n has been set to the sum of reg_offset[n] and register
10064 reg_base_reg[n], calculated in mode reg_mode[n] . */
10065 static rtx reg_offset[FIRST_PSEUDO_REGISTER];
10066 static int reg_base_reg[FIRST_PSEUDO_REGISTER];
10067 static enum machine_mode reg_mode[FIRST_PSEUDO_REGISTER];
10068 /* move2add_luid is linearily increased while scanning the instructions
10069 from first to last. It is used to set reg_set_luid in
10070 reload_cse_move2add and move2add_note_store. */
10071 static int move2add_luid;
10073 /* Generate a CONST_INT and force it in the range of MODE. */
10074 static rtx
10075 gen_mode_int (mode, value)
10076 enum machine_mode mode;
10077 HOST_WIDE_INT value;
10079 HOST_WIDE_INT cval = value & GET_MODE_MASK (mode);
10080 int width = GET_MODE_BITSIZE (mode);
10082 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative number,
10083 sign extend it. */
10084 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
10085 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
10086 cval |= (HOST_WIDE_INT) -1 << width;
10088 return GEN_INT (cval);
10091 static void
10092 reload_cse_move2add (first)
10093 rtx first;
10095 int i;
10096 rtx insn;
10097 int last_label_luid;
10099 for (i = FIRST_PSEUDO_REGISTER-1; i >= 0; i--)
10100 reg_set_luid[i] = 0;
10102 last_label_luid = 0;
10103 move2add_luid = 1;
10104 for (insn = first; insn; insn = NEXT_INSN (insn), move2add_luid++)
10106 rtx pat, note;
10108 if (GET_CODE (insn) == CODE_LABEL)
10109 last_label_luid = move2add_luid;
10110 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
10111 continue;
10112 pat = PATTERN (insn);
10113 /* For simplicity, we only perform this optimization on
10114 straightforward SETs. */
10115 if (GET_CODE (pat) == SET
10116 && GET_CODE (SET_DEST (pat)) == REG)
10118 rtx reg = SET_DEST (pat);
10119 int regno = REGNO (reg);
10120 rtx src = SET_SRC (pat);
10122 /* Check if we have valid information on the contents of this
10123 register in the mode of REG. */
10124 /* ??? We don't know how zero / sign extension is handled, hence
10125 we can't go from a narrower to a wider mode. */
10126 if (reg_set_luid[regno] > last_label_luid
10127 && (GET_MODE_SIZE (GET_MODE (reg))
10128 <= GET_MODE_SIZE (reg_mode[regno]))
10129 && GET_CODE (reg_offset[regno]) == CONST_INT)
10131 /* Try to transform (set (REGX) (CONST_INT A))
10133 (set (REGX) (CONST_INT B))
10135 (set (REGX) (CONST_INT A))
10137 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
10139 if (GET_CODE (src) == CONST_INT && reg_base_reg[regno] < 0)
10141 int success = 0;
10142 rtx new_src
10143 = gen_mode_int (GET_MODE (reg),
10144 INTVAL (src) - INTVAL (reg_offset[regno]));
10145 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
10146 use (set (reg) (reg)) instead.
10147 We don't delete this insn, nor do we convert it into a
10148 note, to avoid losing register notes or the return
10149 value flag. jump2 already knowns how to get rid of
10150 no-op moves. */
10151 if (new_src == const0_rtx)
10152 success = validate_change (insn, &SET_SRC (pat), reg, 0);
10153 else if (rtx_cost (new_src, PLUS) < rtx_cost (src, SET)
10154 && have_add2_insn (GET_MODE (reg)))
10155 success = validate_change (insn, &PATTERN (insn),
10156 gen_add2_insn (reg, new_src), 0);
10157 reg_set_luid[regno] = move2add_luid;
10158 reg_mode[regno] = GET_MODE (reg);
10159 reg_offset[regno] = src;
10160 continue;
10163 /* Try to transform (set (REGX) (REGY))
10164 (set (REGX) (PLUS (REGX) (CONST_INT A)))
10166 (set (REGX) (REGY))
10167 (set (REGX) (PLUS (REGX) (CONST_INT B)))
10169 (REGX) (REGY))
10170 (set (REGX) (PLUS (REGX) (CONST_INT A)))
10172 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
10173 else if (GET_CODE (src) == REG
10174 && reg_base_reg[regno] == REGNO (src)
10175 && reg_set_luid[regno] > reg_set_luid[REGNO (src)])
10177 rtx next = next_nonnote_insn (insn);
10178 rtx set = NULL_RTX;
10179 if (next)
10180 set = single_set (next);
10181 if (next
10182 && set
10183 && SET_DEST (set) == reg
10184 && GET_CODE (SET_SRC (set)) == PLUS
10185 && XEXP (SET_SRC (set), 0) == reg
10186 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
10188 rtx src3 = XEXP (SET_SRC (set), 1);
10189 rtx new_src
10190 = gen_mode_int (GET_MODE (reg),
10191 INTVAL (src3)
10192 - INTVAL (reg_offset[regno]));
10193 int success = 0;
10195 if (new_src == const0_rtx)
10196 /* See above why we create (set (reg) (reg)) here. */
10197 success
10198 = validate_change (next, &SET_SRC (set), reg, 0);
10199 else if ((rtx_cost (new_src, PLUS)
10200 < 2 + rtx_cost (src3, SET))
10201 && have_add2_insn (GET_MODE (reg)))
10202 success
10203 = validate_change (next, &PATTERN (next),
10204 gen_add2_insn (reg, new_src), 0);
10205 if (success)
10207 /* INSN might be the first insn in a basic block
10208 if the preceding insn is a conditional jump
10209 or a possible-throwing call. */
10210 PUT_CODE (insn, NOTE);
10211 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
10212 NOTE_SOURCE_FILE (insn) = 0;
10214 insn = next;
10215 reg_set_luid[regno] = move2add_luid;
10216 reg_mode[regno] = GET_MODE (reg);
10217 reg_offset[regno] = src3;
10218 continue;
10224 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
10226 if (REG_NOTE_KIND (note) == REG_INC
10227 && GET_CODE (XEXP (note, 0)) == REG)
10229 /* Indicate that this register has been recently written to,
10230 but the exact contents are not available. */
10231 int regno = REGNO (XEXP (note, 0));
10232 if (regno < FIRST_PSEUDO_REGISTER)
10234 reg_set_luid[regno] = move2add_luid;
10235 reg_offset[regno] = note;
10239 note_stores (PATTERN (insn), move2add_note_store);
10240 /* If this is a CALL_INSN, all call used registers are stored with
10241 unknown values. */
10242 if (GET_CODE (insn) == CALL_INSN)
10244 for (i = FIRST_PSEUDO_REGISTER-1; i >= 0; i--)
10246 if (call_used_regs[i])
10248 reg_set_luid[i] = move2add_luid;
10249 reg_offset[i] = insn; /* Invalidate contents. */
10256 /* SET is a SET or CLOBBER that sets DST.
10257 Update reg_set_luid, reg_offset and reg_base_reg accordingly.
10258 Called from reload_cse_move2add via note_stores. */
10259 static void
10260 move2add_note_store (dst, set)
10261 rtx dst, set;
10263 int regno = 0;
10264 int i;
10266 enum machine_mode mode = GET_MODE (dst);
10267 if (GET_CODE (dst) == SUBREG)
10269 regno = SUBREG_WORD (dst);
10270 dst = SUBREG_REG (dst);
10272 if (GET_CODE (dst) != REG)
10273 return;
10275 regno += REGNO (dst);
10277 if (HARD_REGNO_NREGS (regno, mode) == 1 && GET_CODE (set) == SET
10278 && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
10279 && GET_CODE (SET_DEST (set)) != SIGN_EXTRACT
10280 && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
10282 rtx src = SET_SRC (set);
10284 reg_mode[regno] = mode;
10285 switch (GET_CODE (src))
10287 case PLUS:
10289 rtx src0 = XEXP (src, 0);
10290 if (GET_CODE (src0) == REG)
10292 if (REGNO (src0) != regno
10293 || reg_offset[regno] != const0_rtx)
10295 reg_base_reg[regno] = REGNO (src0);
10296 reg_set_luid[regno] = move2add_luid;
10298 reg_offset[regno] = XEXP (src, 1);
10299 break;
10301 reg_set_luid[regno] = move2add_luid;
10302 reg_offset[regno] = set; /* Invalidate contents. */
10303 break;
10306 case REG:
10307 reg_base_reg[regno] = REGNO (SET_SRC (set));
10308 reg_offset[regno] = const0_rtx;
10309 reg_set_luid[regno] = move2add_luid;
10310 break;
10312 default:
10313 reg_base_reg[regno] = -1;
10314 reg_offset[regno] = SET_SRC (set);
10315 reg_set_luid[regno] = move2add_luid;
10316 break;
10319 else
10321 for (i = regno + HARD_REGNO_NREGS (regno, mode) - 1; i >= regno; i--)
10323 /* Indicate that this register has been recently written to,
10324 but the exact contents are not available. */
10325 reg_set_luid[i] = move2add_luid;
10326 reg_offset[i] = dst;
10331 #ifdef AUTO_INC_DEC
10332 static void
10333 add_auto_inc_notes (insn, x)
10334 rtx insn;
10335 rtx x;
10337 enum rtx_code code = GET_CODE (x);
10338 const char *fmt;
10339 int i, j;
10341 if (code == MEM && auto_inc_p (XEXP (x, 0)))
10343 REG_NOTES (insn)
10344 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
10345 return;
10348 /* Scan all the operand sub-expressions. */
10349 fmt = GET_RTX_FORMAT (code);
10350 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10352 if (fmt[i] == 'e')
10353 add_auto_inc_notes (insn, XEXP (x, i));
10354 else if (fmt[i] == 'E')
10355 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10356 add_auto_inc_notes (insn, XVECEXP (x, i, j));
10359 #endif