allow all arm targets to use -mstructure-size-boundary=XX
[official-gcc.git] / gcc / reload1.c
blob0a3371f539573e536e245f39b4a35528b7c34850
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_init PROTO((struct insn_chain *, rtx *));
428 static void choose_reload_regs PROTO((struct insn_chain *));
429 static void merge_assigned_reloads PROTO((rtx));
430 static void emit_reload_insns PROTO((struct insn_chain *));
431 static void delete_output_reload PROTO((rtx, int, int));
432 static void delete_address_reloads PROTO((rtx, rtx));
433 static void delete_address_reloads_1 PROTO((rtx, rtx, rtx));
434 static rtx inc_for_reload PROTO((rtx, rtx, rtx, int));
435 static int constraint_accepts_reg_p PROTO((const char *, rtx));
436 static void reload_cse_regs_1 PROTO((rtx));
437 static void reload_cse_invalidate_regno PROTO((int, enum machine_mode, int));
438 static int reload_cse_mem_conflict_p PROTO((rtx, rtx));
439 static void reload_cse_invalidate_mem PROTO((rtx));
440 static void reload_cse_invalidate_rtx PROTO((rtx, rtx));
441 static int reload_cse_regno_equal_p PROTO((int, rtx, enum machine_mode));
442 static int reload_cse_noop_set_p PROTO((rtx, rtx));
443 static int reload_cse_simplify_set PROTO((rtx, rtx));
444 static int reload_cse_simplify_operands PROTO((rtx));
445 static void reload_cse_check_clobber PROTO((rtx, rtx));
446 static void reload_cse_record_set PROTO((rtx, rtx));
447 static void reload_combine PROTO((void));
448 static void reload_combine_note_use PROTO((rtx *, rtx));
449 static void reload_combine_note_store PROTO((rtx, rtx));
450 static void reload_cse_move2add PROTO((rtx));
451 static void move2add_note_store PROTO((rtx, rtx));
452 #ifdef AUTO_INC_DEC
453 static void add_auto_inc_notes PROTO((rtx, rtx));
454 #endif
455 static rtx gen_mode_int PROTO((enum machine_mode,
456 HOST_WIDE_INT));
457 extern void dump_needs PROTO((struct insn_chain *, FILE *));
459 /* Initialize the reload pass once per compilation. */
461 void
462 init_reload ()
464 register int i;
466 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
467 Set spill_indirect_levels to the number of levels such addressing is
468 permitted, zero if it is not permitted at all. */
470 register rtx tem
471 = gen_rtx_MEM (Pmode,
472 gen_rtx_PLUS (Pmode,
473 gen_rtx_REG (Pmode,
474 LAST_VIRTUAL_REGISTER + 1),
475 GEN_INT (4)));
476 spill_indirect_levels = 0;
478 while (memory_address_p (QImode, tem))
480 spill_indirect_levels++;
481 tem = gen_rtx_MEM (Pmode, tem);
484 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
486 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
487 indirect_symref_ok = memory_address_p (QImode, tem);
489 /* See if reg+reg is a valid (and offsettable) address. */
491 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
493 tem = gen_rtx_PLUS (Pmode,
494 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
495 gen_rtx_REG (Pmode, i));
497 /* This way, we make sure that reg+reg is an offsettable address. */
498 tem = plus_constant (tem, 4);
500 if (memory_address_p (QImode, tem))
502 double_reg_address_ok = 1;
503 break;
507 /* Initialize obstack for our rtl allocation. */
508 gcc_obstack_init (&reload_obstack);
509 reload_startobj = (char *) obstack_alloc (&reload_obstack, 0);
512 /* List of insn chains that are currently unused. */
513 static struct insn_chain *unused_insn_chains = 0;
515 /* Allocate an empty insn_chain structure. */
516 struct insn_chain *
517 new_insn_chain ()
519 struct insn_chain *c;
521 if (unused_insn_chains == 0)
523 c = (struct insn_chain *)
524 obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
525 c->live_before = OBSTACK_ALLOC_REG_SET (&reload_obstack);
526 c->live_after = OBSTACK_ALLOC_REG_SET (&reload_obstack);
528 else
530 c = unused_insn_chains;
531 unused_insn_chains = c->next;
533 c->is_caller_save_insn = 0;
534 c->need_operand_change = 0;
535 c->need_reload = 0;
536 c->need_elim = 0;
537 return c;
540 /* Small utility function to set all regs in hard reg set TO which are
541 allocated to pseudos in regset FROM. */
542 void
543 compute_use_by_pseudos (to, from)
544 HARD_REG_SET *to;
545 regset from;
547 int regno;
548 EXECUTE_IF_SET_IN_REG_SET
549 (from, FIRST_PSEUDO_REGISTER, regno,
551 int r = reg_renumber[regno];
552 int nregs;
553 if (r < 0)
555 /* reload_combine uses the information from
556 BASIC_BLOCK->global_live_at_start, which might still
557 contain registers that have not actually been allocated
558 since they have an equivalence. */
559 if (! reload_completed)
560 abort ();
562 else
564 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (regno));
565 while (nregs-- > 0)
566 SET_HARD_REG_BIT (*to, r + nregs);
571 /* Global variables used by reload and its subroutines. */
573 /* Set during calculate_needs if an insn needs register elimination. */
574 static int something_needs_elimination;
575 /* Set during calculate_needs if an insn needs an operand changed. */
576 int something_needs_operands_changed;
578 /* Nonzero means we couldn't get enough spill regs. */
579 static int failure;
581 /* Main entry point for the reload pass.
583 FIRST is the first insn of the function being compiled.
585 GLOBAL nonzero means we were called from global_alloc
586 and should attempt to reallocate any pseudoregs that we
587 displace from hard regs we will use for reloads.
588 If GLOBAL is zero, we do not have enough information to do that,
589 so any pseudo reg that is spilled must go to the stack.
591 DUMPFILE is the global-reg debugging dump file stream, or 0.
592 If it is nonzero, messages are written to it to describe
593 which registers are seized as reload regs, which pseudo regs
594 are spilled from them, and where the pseudo regs are reallocated to.
596 Return value is nonzero if reload failed
597 and we must not do any more for this function. */
600 reload (first, global, dumpfile)
601 rtx first;
602 int global;
603 FILE *dumpfile;
605 register int i;
606 register rtx insn;
607 register struct elim_table *ep;
609 /* The two pointers used to track the true location of the memory used
610 for label offsets. */
611 char *real_known_ptr = NULL_PTR;
612 int (*real_at_ptr)[NUM_ELIMINABLE_REGS];
614 /* Make sure even insns with volatile mem refs are recognizable. */
615 init_recog ();
617 failure = 0;
619 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
621 /* Make sure that the last insn in the chain
622 is not something that needs reloading. */
623 emit_note (NULL_PTR, NOTE_INSN_DELETED);
625 /* Enable find_equiv_reg to distinguish insns made by reload. */
626 reload_first_uid = get_max_uid ();
628 #ifdef SECONDARY_MEMORY_NEEDED
629 /* Initialize the secondary memory table. */
630 clear_secondary_mem ();
631 #endif
633 /* We don't have a stack slot for any spill reg yet. */
634 bzero ((char *) spill_stack_slot, sizeof spill_stack_slot);
635 bzero ((char *) spill_stack_slot_width, sizeof spill_stack_slot_width);
637 /* Initialize the save area information for caller-save, in case some
638 are needed. */
639 init_save_areas ();
641 /* Compute which hard registers are now in use
642 as homes for pseudo registers.
643 This is done here rather than (eg) in global_alloc
644 because this point is reached even if not optimizing. */
645 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
646 mark_home_live (i);
648 /* A function that receives a nonlocal goto must save all call-saved
649 registers. */
650 if (current_function_has_nonlocal_label)
651 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
653 if (! call_used_regs[i] && ! fixed_regs[i])
654 regs_ever_live[i] = 1;
657 /* Find all the pseudo registers that didn't get hard regs
658 but do have known equivalent constants or memory slots.
659 These include parameters (known equivalent to parameter slots)
660 and cse'd or loop-moved constant memory addresses.
662 Record constant equivalents in reg_equiv_constant
663 so they will be substituted by find_reloads.
664 Record memory equivalents in reg_mem_equiv so they can
665 be substituted eventually by altering the REG-rtx's. */
667 reg_equiv_constant = (rtx *) xcalloc (max_regno, sizeof (rtx));
668 reg_equiv_memory_loc = (rtx *) xcalloc (max_regno, sizeof (rtx));
669 reg_equiv_mem = (rtx *) xcalloc (max_regno, sizeof (rtx));
670 reg_equiv_init = (rtx *) xcalloc (max_regno, sizeof (rtx));
671 reg_equiv_address = (rtx *) xcalloc (max_regno, sizeof (rtx));
672 reg_max_ref_width = (int *) xcalloc (max_regno, sizeof (int));
673 reg_old_renumber = (short *) xcalloc (max_regno, sizeof (short));
674 bcopy ((PTR) reg_renumber, (PTR) reg_old_renumber, max_regno * sizeof (short));
675 pseudo_forbidden_regs
676 = (HARD_REG_SET *) xmalloc (max_regno * sizeof (HARD_REG_SET));
677 pseudo_previous_regs
678 = (HARD_REG_SET *) xcalloc (max_regno, sizeof (HARD_REG_SET));
680 CLEAR_HARD_REG_SET (bad_spill_regs_global);
682 /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
683 Also find all paradoxical subregs and find largest such for each pseudo.
684 On machines with small register classes, record hard registers that
685 are used for user variables. These can never be used for spills.
686 Also look for a "constant" NOTE_INSN_SETJMP. This means that all
687 caller-saved registers must be marked live. */
689 num_eliminable_invariants = 0;
690 for (insn = first; insn; insn = NEXT_INSN (insn))
692 rtx set = single_set (insn);
694 if (GET_CODE (insn) == NOTE && CONST_CALL_P (insn)
695 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
696 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
697 if (! call_used_regs[i])
698 regs_ever_live[i] = 1;
700 if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
702 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
703 if (note
704 #ifdef LEGITIMATE_PIC_OPERAND_P
705 && (! function_invariant_p (XEXP (note, 0))
706 || ! flag_pic
707 || LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))
708 #endif
711 rtx x = XEXP (note, 0);
712 i = REGNO (SET_DEST (set));
713 if (i > LAST_VIRTUAL_REGISTER)
715 if (GET_CODE (x) == MEM)
717 /* If the operand is a PLUS, the MEM may be shared,
718 so make sure we have an unshared copy here. */
719 if (GET_CODE (XEXP (x, 0)) == PLUS)
720 x = copy_rtx (x);
722 reg_equiv_memory_loc[i] = x;
724 else if (function_invariant_p (x))
726 if (GET_CODE (x) == PLUS)
728 /* This is PLUS of frame pointer and a constant,
729 and might be shared. Unshare it. */
730 reg_equiv_constant[i] = copy_rtx (x);
731 num_eliminable_invariants++;
733 else if (x == frame_pointer_rtx
734 || x == arg_pointer_rtx)
736 reg_equiv_constant[i] = x;
737 num_eliminable_invariants++;
739 else if (LEGITIMATE_CONSTANT_P (x))
740 reg_equiv_constant[i] = x;
741 else
742 reg_equiv_memory_loc[i]
743 = force_const_mem (GET_MODE (SET_DEST (set)), x);
745 else
746 continue;
748 /* If this register is being made equivalent to a MEM
749 and the MEM is not SET_SRC, the equivalencing insn
750 is one with the MEM as a SET_DEST and it occurs later.
751 So don't mark this insn now. */
752 if (GET_CODE (x) != MEM
753 || rtx_equal_p (SET_SRC (set), x))
754 reg_equiv_init[i]
755 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[i]);
760 /* If this insn is setting a MEM from a register equivalent to it,
761 this is the equivalencing insn. */
762 else if (set && GET_CODE (SET_DEST (set)) == MEM
763 && GET_CODE (SET_SRC (set)) == REG
764 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
765 && rtx_equal_p (SET_DEST (set),
766 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
767 reg_equiv_init[REGNO (SET_SRC (set))]
768 = gen_rtx_INSN_LIST (VOIDmode, insn,
769 reg_equiv_init[REGNO (SET_SRC (set))]);
771 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
772 scan_paradoxical_subregs (PATTERN (insn));
775 init_elim_table ();
777 num_labels = max_label_num () - get_first_label_num ();
779 /* Allocate the tables used to store offset information at labels. */
780 /* We used to use alloca here, but the size of what it would try to
781 allocate would occasionally cause it to exceed the stack limit and
782 cause a core dump. */
783 real_known_ptr = xmalloc (num_labels);
784 real_at_ptr
785 = (int (*)[NUM_ELIMINABLE_REGS])
786 xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
788 offsets_known_at = real_known_ptr - get_first_label_num ();
789 offsets_at
790 = (int (*)[NUM_ELIMINABLE_REGS]) (real_at_ptr - get_first_label_num ());
792 /* Alter each pseudo-reg rtx to contain its hard reg number.
793 Assign stack slots to the pseudos that lack hard regs or equivalents.
794 Do not touch virtual registers. */
796 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
797 alter_reg (i, -1);
799 /* If we have some registers we think can be eliminated, scan all insns to
800 see if there is an insn that sets one of these registers to something
801 other than itself plus a constant. If so, the register cannot be
802 eliminated. Doing this scan here eliminates an extra pass through the
803 main reload loop in the most common case where register elimination
804 cannot be done. */
805 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
806 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
807 || GET_CODE (insn) == CALL_INSN)
808 note_stores (PATTERN (insn), mark_not_eliminable);
810 maybe_fix_stack_asms ();
812 insns_need_reload = 0;
813 something_needs_elimination = 0;
815 /* Initialize to -1, which means take the first spill register. */
816 last_spill_reg = -1;
818 spilled_pseudos = ALLOCA_REG_SET ();
820 /* Spill any hard regs that we know we can't eliminate. */
821 CLEAR_HARD_REG_SET (used_spill_regs);
822 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
823 if (! ep->can_eliminate)
824 spill_hard_reg (ep->from, dumpfile, 1);
826 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
827 if (frame_pointer_needed)
828 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, dumpfile, 1);
829 #endif
830 finish_spills (global, dumpfile);
832 /* From now on, we may need to generate moves differently. We may also
833 allow modifications of insns which cause them to not be recognized.
834 Any such modifications will be cleaned up during reload itself. */
835 reload_in_progress = 1;
837 /* This loop scans the entire function each go-round
838 and repeats until one repetition spills no additional hard regs. */
839 for (;;)
841 int something_changed;
842 int did_spill;
843 struct insn_chain *chain;
845 HOST_WIDE_INT starting_frame_size;
847 /* Round size of stack frame to BIGGEST_ALIGNMENT. This must be done
848 here because the stack size may be a part of the offset computation
849 for register elimination, and there might have been new stack slots
850 created in the last iteration of this loop. */
851 assign_stack_local (BLKmode, 0, 0);
853 starting_frame_size = get_frame_size ();
855 set_initial_elim_offsets ();
856 set_initial_label_offsets ();
858 /* For each pseudo register that has an equivalent location defined,
859 try to eliminate any eliminable registers (such as the frame pointer)
860 assuming initial offsets for the replacement register, which
861 is the normal case.
863 If the resulting location is directly addressable, substitute
864 the MEM we just got directly for the old REG.
866 If it is not addressable but is a constant or the sum of a hard reg
867 and constant, it is probably not addressable because the constant is
868 out of range, in that case record the address; we will generate
869 hairy code to compute the address in a register each time it is
870 needed. Similarly if it is a hard register, but one that is not
871 valid as an address register.
873 If the location is not addressable, but does not have one of the
874 above forms, assign a stack slot. We have to do this to avoid the
875 potential of producing lots of reloads if, e.g., a location involves
876 a pseudo that didn't get a hard register and has an equivalent memory
877 location that also involves a pseudo that didn't get a hard register.
879 Perhaps at some point we will improve reload_when_needed handling
880 so this problem goes away. But that's very hairy. */
882 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
883 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
885 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
887 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
888 XEXP (x, 0)))
889 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
890 else if (CONSTANT_P (XEXP (x, 0))
891 || (GET_CODE (XEXP (x, 0)) == REG
892 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
893 || (GET_CODE (XEXP (x, 0)) == PLUS
894 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
895 && (REGNO (XEXP (XEXP (x, 0), 0))
896 < FIRST_PSEUDO_REGISTER)
897 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
898 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
899 else
901 /* Make a new stack slot. Then indicate that something
902 changed so we go back and recompute offsets for
903 eliminable registers because the allocation of memory
904 below might change some offset. reg_equiv_{mem,address}
905 will be set up for this pseudo on the next pass around
906 the loop. */
907 reg_equiv_memory_loc[i] = 0;
908 reg_equiv_init[i] = 0;
909 alter_reg (i, -1);
913 if (caller_save_needed)
914 setup_save_areas ();
916 /* If we allocated another stack slot, redo elimination bookkeeping. */
917 if (starting_frame_size != get_frame_size ())
918 continue;
920 if (caller_save_needed)
922 save_call_clobbered_regs ();
923 /* That might have allocated new insn_chain structures. */
924 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
927 calculate_needs_all_insns (global);
929 CLEAR_REG_SET (spilled_pseudos);
930 did_spill = 0;
932 something_changed = 0;
934 /* If we allocated any new memory locations, make another pass
935 since it might have changed elimination offsets. */
936 if (starting_frame_size != get_frame_size ())
937 something_changed = 1;
940 HARD_REG_SET to_spill;
941 CLEAR_HARD_REG_SET (to_spill);
942 update_eliminables (&to_spill);
943 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
944 if (TEST_HARD_REG_BIT (to_spill, i))
946 spill_hard_reg (i, dumpfile, 1);
947 did_spill = 1;
949 /* Regardless of the state of spills, if we previously had
950 a register that we thought we could eliminate, but no can
951 not eliminate, we must run another pass.
953 Consider pseudos which have an entry in reg_equiv_* which
954 reference an eliminable register. We must make another pass
955 to update reg_equiv_* so that we do not substitute in the
956 old value from when we thought the elimination could be
957 performed. */
958 something_changed = 1;
962 CLEAR_HARD_REG_SET (used_spill_regs);
963 /* Try to satisfy the needs for each insn. */
964 for (chain = insns_need_reload; chain != 0;
965 chain = chain->next_need_reload)
966 find_reload_regs (chain, dumpfile);
968 if (failure)
969 goto failed;
971 if (insns_need_reload != 0 || did_spill)
972 something_changed |= finish_spills (global, dumpfile);
974 if (! something_changed)
975 break;
977 if (caller_save_needed)
978 delete_caller_save_insns ();
981 /* If global-alloc was run, notify it of any register eliminations we have
982 done. */
983 if (global)
984 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
985 if (ep->can_eliminate)
986 mark_elimination (ep->from, ep->to);
988 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
989 If that insn didn't set the register (i.e., it copied the register to
990 memory), just delete that insn instead of the equivalencing insn plus
991 anything now dead. If we call delete_dead_insn on that insn, we may
992 delete the insn that actually sets the register if the register dies
993 there and that is incorrect. */
995 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
997 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
999 rtx list;
1000 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1002 rtx equiv_insn = XEXP (list, 0);
1003 if (GET_CODE (equiv_insn) == NOTE)
1004 continue;
1005 if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1006 delete_dead_insn (equiv_insn);
1007 else
1009 PUT_CODE (equiv_insn, NOTE);
1010 NOTE_SOURCE_FILE (equiv_insn) = 0;
1011 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1017 /* Use the reload registers where necessary
1018 by generating move instructions to move the must-be-register
1019 values into or out of the reload registers. */
1021 if (insns_need_reload != 0 || something_needs_elimination
1022 || something_needs_operands_changed)
1024 int old_frame_size = get_frame_size ();
1026 reload_as_needed (global);
1028 if (old_frame_size != get_frame_size ())
1029 abort ();
1031 if (num_eliminable)
1032 verify_initial_elim_offsets ();
1035 /* If we were able to eliminate the frame pointer, show that it is no
1036 longer live at the start of any basic block. If it ls live by
1037 virtue of being in a pseudo, that pseudo will be marked live
1038 and hence the frame pointer will be known to be live via that
1039 pseudo. */
1041 if (! frame_pointer_needed)
1042 for (i = 0; i < n_basic_blocks; i++)
1043 CLEAR_REGNO_REG_SET (BASIC_BLOCK (i)->global_live_at_start,
1044 HARD_FRAME_POINTER_REGNUM);
1046 /* Come here (with failure set nonzero) if we can't get enough spill regs
1047 and we decide not to abort about it. */
1048 failed:
1050 reload_in_progress = 0;
1052 /* Now eliminate all pseudo regs by modifying them into
1053 their equivalent memory references.
1054 The REG-rtx's for the pseudos are modified in place,
1055 so all insns that used to refer to them now refer to memory.
1057 For a reg that has a reg_equiv_address, all those insns
1058 were changed by reloading so that no insns refer to it any longer;
1059 but the DECL_RTL of a variable decl may refer to it,
1060 and if so this causes the debugging info to mention the variable. */
1062 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1064 rtx addr = 0;
1065 int in_struct = 0;
1066 int is_scalar = 0;
1067 int is_readonly = 0;
1069 if (reg_equiv_memory_loc[i])
1071 in_struct = MEM_IN_STRUCT_P (reg_equiv_memory_loc[i]);
1072 is_scalar = MEM_SCALAR_P (reg_equiv_memory_loc[i]);
1073 is_readonly = RTX_UNCHANGING_P (reg_equiv_memory_loc[i]);
1076 if (reg_equiv_mem[i])
1077 addr = XEXP (reg_equiv_mem[i], 0);
1079 if (reg_equiv_address[i])
1080 addr = reg_equiv_address[i];
1082 if (addr)
1084 if (reg_renumber[i] < 0)
1086 rtx reg = regno_reg_rtx[i];
1087 PUT_CODE (reg, MEM);
1088 XEXP (reg, 0) = addr;
1089 REG_USERVAR_P (reg) = 0;
1090 RTX_UNCHANGING_P (reg) = is_readonly;
1091 MEM_IN_STRUCT_P (reg) = in_struct;
1092 MEM_SCALAR_P (reg) = is_scalar;
1093 /* We have no alias information about this newly created
1094 MEM. */
1095 MEM_ALIAS_SET (reg) = 0;
1097 else if (reg_equiv_mem[i])
1098 XEXP (reg_equiv_mem[i], 0) = addr;
1102 /* We must set reload_completed now since the cleanup_subreg_operands call
1103 below will re-recognize each insn and reload may have generated insns
1104 which are only valid during and after reload. */
1105 reload_completed = 1;
1107 /* Make a pass over all the insns and delete all USEs which we
1108 inserted only to tag a REG_EQUAL note on them. Remove all
1109 REG_DEAD and REG_UNUSED notes. Delete all CLOBBER insns and
1110 simplify (subreg (reg)) operands. Also remove all REG_RETVAL and
1111 REG_LIBCALL notes since they are no longer useful or accurate.
1112 Strip and regenerate REG_INC notes that may have been moved
1113 around. */
1115 for (insn = first; insn; insn = NEXT_INSN (insn))
1116 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1118 rtx *pnote;
1120 if ((GET_CODE (PATTERN (insn)) == USE
1121 && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1122 || GET_CODE (PATTERN (insn)) == CLOBBER)
1124 PUT_CODE (insn, NOTE);
1125 NOTE_SOURCE_FILE (insn) = 0;
1126 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1127 continue;
1130 pnote = &REG_NOTES (insn);
1131 while (*pnote != 0)
1133 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1134 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1135 || REG_NOTE_KIND (*pnote) == REG_INC
1136 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1137 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1138 *pnote = XEXP (*pnote, 1);
1139 else
1140 pnote = &XEXP (*pnote, 1);
1143 #ifdef AUTO_INC_DEC
1144 add_auto_inc_notes (insn, PATTERN (insn));
1145 #endif
1147 /* And simplify (subreg (reg)) if it appears as an operand. */
1148 cleanup_subreg_operands (insn);
1151 /* If we are doing stack checking, give a warning if this function's
1152 frame size is larger than we expect. */
1153 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1155 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1156 static int verbose_warned = 0;
1158 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1159 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1160 size += UNITS_PER_WORD;
1162 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1164 warning ("frame size too large for reliable stack checking");
1165 if (! verbose_warned)
1167 warning ("try reducing the number of local variables");
1168 verbose_warned = 1;
1173 /* Indicate that we no longer have known memory locations or constants. */
1174 if (reg_equiv_constant)
1175 free (reg_equiv_constant);
1176 reg_equiv_constant = 0;
1177 if (reg_equiv_memory_loc)
1178 free (reg_equiv_memory_loc);
1179 reg_equiv_memory_loc = 0;
1181 if (real_known_ptr)
1182 free (real_known_ptr);
1183 if (real_at_ptr)
1184 free (real_at_ptr);
1186 free (reg_equiv_mem);
1187 free (reg_equiv_init);
1188 free (reg_equiv_address);
1189 free (reg_max_ref_width);
1190 free (reg_old_renumber);
1191 free (pseudo_previous_regs);
1192 free (pseudo_forbidden_regs);
1194 FREE_REG_SET (spilled_pseudos);
1196 CLEAR_HARD_REG_SET (used_spill_regs);
1197 for (i = 0; i < n_spills; i++)
1198 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1200 /* Free all the insn_chain structures at once. */
1201 obstack_free (&reload_obstack, reload_startobj);
1202 unused_insn_chains = 0;
1204 return failure;
1207 /* Yet another special case. Unfortunately, reg-stack forces people to
1208 write incorrect clobbers in asm statements. These clobbers must not
1209 cause the register to appear in bad_spill_regs, otherwise we'll call
1210 fatal_insn later. We clear the corresponding regnos in the live
1211 register sets to avoid this.
1212 The whole thing is rather sick, I'm afraid. */
1213 static void
1214 maybe_fix_stack_asms ()
1216 #ifdef STACK_REGS
1217 const char *constraints[MAX_RECOG_OPERANDS];
1218 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1219 struct insn_chain *chain;
1221 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1223 int i, noperands;
1224 HARD_REG_SET clobbered, allowed;
1225 rtx pat;
1227 if (GET_RTX_CLASS (GET_CODE (chain->insn)) != 'i'
1228 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1229 continue;
1230 pat = PATTERN (chain->insn);
1231 if (GET_CODE (pat) != PARALLEL)
1232 continue;
1234 CLEAR_HARD_REG_SET (clobbered);
1235 CLEAR_HARD_REG_SET (allowed);
1237 /* First, make a mask of all stack regs that are clobbered. */
1238 for (i = 0; i < XVECLEN (pat, 0); i++)
1240 rtx t = XVECEXP (pat, 0, i);
1241 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1242 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1245 /* Get the operand values and constraints out of the insn. */
1246 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1247 constraints, operand_mode);
1249 /* For every operand, see what registers are allowed. */
1250 for (i = 0; i < noperands; i++)
1252 const char *p = constraints[i];
1253 /* For every alternative, we compute the class of registers allowed
1254 for reloading in CLS, and merge its contents into the reg set
1255 ALLOWED. */
1256 int cls = (int) NO_REGS;
1258 for (;;)
1260 char c = *p++;
1262 if (c == '\0' || c == ',' || c == '#')
1264 /* End of one alternative - mark the regs in the current
1265 class, and reset the class. */
1266 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1267 cls = NO_REGS;
1268 if (c == '#')
1269 do {
1270 c = *p++;
1271 } while (c != '\0' && c != ',');
1272 if (c == '\0')
1273 break;
1274 continue;
1277 switch (c)
1279 case '=': case '+': case '*': case '%': case '?': case '!':
1280 case '0': case '1': case '2': case '3': case '4': case 'm':
1281 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1282 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1283 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1284 case 'P':
1285 #ifdef EXTRA_CONSTRAINT
1286 case 'Q': case 'R': case 'S': case 'T': case 'U':
1287 #endif
1288 break;
1290 case 'p':
1291 cls = (int) reg_class_subunion[cls][(int) BASE_REG_CLASS];
1292 break;
1294 case 'g':
1295 case 'r':
1296 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1297 break;
1299 default:
1300 cls = (int) reg_class_subunion[cls][(int) REG_CLASS_FROM_LETTER (c)];
1305 /* Those of the registers which are clobbered, but allowed by the
1306 constraints, must be usable as reload registers. So clear them
1307 out of the life information. */
1308 AND_HARD_REG_SET (allowed, clobbered);
1309 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1310 if (TEST_HARD_REG_BIT (allowed, i))
1312 CLEAR_REGNO_REG_SET (chain->live_before, i);
1313 CLEAR_REGNO_REG_SET (chain->live_after, i);
1317 #endif
1321 /* Walk the chain of insns, and determine for each whether it needs reloads
1322 and/or eliminations. Build the corresponding insns_need_reload list, and
1323 set something_needs_elimination as appropriate. */
1324 static void
1325 calculate_needs_all_insns (global)
1326 int global;
1328 struct insn_chain **pprev_reload = &insns_need_reload;
1329 struct insn_chain *chain;
1331 something_needs_elimination = 0;
1333 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1335 rtx insn = chain->insn;
1337 /* Clear out the shortcuts, in case they were set last time through. */
1338 chain->need_elim = 0;
1339 chain->need_reload = 0;
1340 chain->need_operand_change = 0;
1342 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1343 include REG_LABEL), we need to see what effects this has on the
1344 known offsets at labels. */
1346 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
1347 || (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1348 && REG_NOTES (insn) != 0))
1349 set_label_offsets (insn, insn, 0);
1351 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1353 rtx old_body = PATTERN (insn);
1354 int old_code = INSN_CODE (insn);
1355 rtx old_notes = REG_NOTES (insn);
1356 int did_elimination = 0;
1357 int operands_changed = 0;
1358 rtx set = single_set (insn);
1360 /* Skip insns that only set an equivalence. */
1361 if (set && GET_CODE (SET_DEST (set)) == REG
1362 && reg_renumber[REGNO (SET_DEST (set))] < 0
1363 && reg_equiv_constant[REGNO (SET_DEST (set))])
1364 continue;
1366 /* If needed, eliminate any eliminable registers. */
1367 if (num_eliminable || num_eliminable_invariants)
1368 did_elimination = eliminate_regs_in_insn (insn, 0);
1370 /* Analyze the instruction. */
1371 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1372 global, spill_reg_order);
1374 /* If a no-op set needs more than one reload, this is likely
1375 to be something that needs input address reloads. We
1376 can't get rid of this cleanly later, and it is of no use
1377 anyway, so discard it now.
1378 We only do this when expensive_optimizations is enabled,
1379 since this complements reload inheritance / output
1380 reload deletion, and it can make debugging harder. */
1381 if (flag_expensive_optimizations && n_reloads > 1)
1383 rtx set = single_set (insn);
1384 if (set
1385 && SET_SRC (set) == SET_DEST (set)
1386 && GET_CODE (SET_SRC (set)) == REG
1387 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1389 PUT_CODE (insn, NOTE);
1390 NOTE_SOURCE_FILE (insn) = 0;
1391 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1392 continue;
1395 if (num_eliminable)
1396 update_eliminable_offsets ();
1398 /* Remember for later shortcuts which insns had any reloads or
1399 register eliminations. */
1400 chain->need_elim = did_elimination;
1401 chain->need_reload = n_reloads > 0;
1402 chain->need_operand_change = operands_changed;
1404 /* Discard any register replacements done. */
1405 if (did_elimination)
1407 obstack_free (&reload_obstack, reload_firstobj);
1408 PATTERN (insn) = old_body;
1409 INSN_CODE (insn) = old_code;
1410 REG_NOTES (insn) = old_notes;
1411 something_needs_elimination = 1;
1414 something_needs_operands_changed |= operands_changed;
1416 if (n_reloads != 0)
1418 *pprev_reload = chain;
1419 pprev_reload = &chain->next_need_reload;
1421 calculate_needs (chain);
1425 *pprev_reload = 0;
1428 /* Compute the most additional registers needed by one instruction,
1429 given by CHAIN. Collect information separately for each class of regs.
1431 To compute the number of reload registers of each class needed for an
1432 insn, we must simulate what choose_reload_regs can do. We do this by
1433 splitting an insn into an "input" and an "output" part. RELOAD_OTHER
1434 reloads are used in both. The input part uses those reloads,
1435 RELOAD_FOR_INPUT reloads, which must be live over the entire input section
1436 of reloads, and the maximum of all the RELOAD_FOR_INPUT_ADDRESS and
1437 RELOAD_FOR_OPERAND_ADDRESS reloads, which conflict with the inputs.
1439 The registers needed for output are RELOAD_OTHER and RELOAD_FOR_OUTPUT,
1440 which are live for the entire output portion, and the maximum of all the
1441 RELOAD_FOR_OUTPUT_ADDRESS reloads for each operand.
1443 The total number of registers needed is the maximum of the
1444 inputs and outputs. */
1446 static void
1447 calculate_needs (chain)
1448 struct insn_chain *chain;
1450 int i;
1452 /* Each `struct needs' corresponds to one RELOAD_... type. */
1453 struct {
1454 struct needs other;
1455 struct needs input;
1456 struct needs output;
1457 struct needs insn;
1458 struct needs other_addr;
1459 struct needs op_addr;
1460 struct needs op_addr_reload;
1461 struct needs in_addr[MAX_RECOG_OPERANDS];
1462 struct needs in_addr_addr[MAX_RECOG_OPERANDS];
1463 struct needs out_addr[MAX_RECOG_OPERANDS];
1464 struct needs out_addr_addr[MAX_RECOG_OPERANDS];
1465 } insn_needs;
1467 bzero ((char *) chain->group_size, sizeof chain->group_size);
1468 for (i = 0; i < N_REG_CLASSES; i++)
1469 chain->group_mode[i] = VOIDmode;
1470 bzero ((char *) &insn_needs, sizeof insn_needs);
1472 /* Count each reload once in every class
1473 containing the reload's own class. */
1475 for (i = 0; i < n_reloads; i++)
1477 register enum reg_class *p;
1478 enum reg_class class = rld[i].class;
1479 int size;
1480 enum machine_mode mode;
1481 struct needs *this_needs;
1483 /* Don't count the dummy reloads, for which one of the
1484 regs mentioned in the insn can be used for reloading.
1485 Don't count optional reloads.
1486 Don't count reloads that got combined with others. */
1487 if (rld[i].reg_rtx != 0
1488 || rld[i].optional != 0
1489 || (rld[i].out == 0 && rld[i].in == 0
1490 && ! rld[i].secondary_p))
1491 continue;
1493 mode = rld[i].mode;
1494 size = rld[i].nregs;
1496 /* Decide which time-of-use to count this reload for. */
1497 switch (rld[i].when_needed)
1499 case RELOAD_OTHER:
1500 this_needs = &insn_needs.other;
1501 break;
1502 case RELOAD_FOR_INPUT:
1503 this_needs = &insn_needs.input;
1504 break;
1505 case RELOAD_FOR_OUTPUT:
1506 this_needs = &insn_needs.output;
1507 break;
1508 case RELOAD_FOR_INSN:
1509 this_needs = &insn_needs.insn;
1510 break;
1511 case RELOAD_FOR_OTHER_ADDRESS:
1512 this_needs = &insn_needs.other_addr;
1513 break;
1514 case RELOAD_FOR_INPUT_ADDRESS:
1515 this_needs = &insn_needs.in_addr[rld[i].opnum];
1516 break;
1517 case RELOAD_FOR_INPADDR_ADDRESS:
1518 this_needs = &insn_needs.in_addr_addr[rld[i].opnum];
1519 break;
1520 case RELOAD_FOR_OUTPUT_ADDRESS:
1521 this_needs = &insn_needs.out_addr[rld[i].opnum];
1522 break;
1523 case RELOAD_FOR_OUTADDR_ADDRESS:
1524 this_needs = &insn_needs.out_addr_addr[rld[i].opnum];
1525 break;
1526 case RELOAD_FOR_OPERAND_ADDRESS:
1527 this_needs = &insn_needs.op_addr;
1528 break;
1529 case RELOAD_FOR_OPADDR_ADDR:
1530 this_needs = &insn_needs.op_addr_reload;
1531 break;
1532 default:
1533 abort();
1536 if (size > 1)
1538 enum machine_mode other_mode, allocate_mode;
1540 /* Count number of groups needed separately from
1541 number of individual regs needed. */
1542 this_needs->groups[(int) class]++;
1543 p = reg_class_superclasses[(int) class];
1544 while (*p != LIM_REG_CLASSES)
1545 this_needs->groups[(int) *p++]++;
1547 /* Record size and mode of a group of this class. */
1548 /* If more than one size group is needed,
1549 make all groups the largest needed size. */
1550 if (chain->group_size[(int) class] < size)
1552 other_mode = chain->group_mode[(int) class];
1553 allocate_mode = mode;
1555 chain->group_size[(int) class] = size;
1556 chain->group_mode[(int) class] = mode;
1558 else
1560 other_mode = mode;
1561 allocate_mode = chain->group_mode[(int) class];
1564 /* Crash if two dissimilar machine modes both need
1565 groups of consecutive regs of the same class. */
1567 if (other_mode != VOIDmode && other_mode != allocate_mode
1568 && ! modes_equiv_for_class_p (allocate_mode,
1569 other_mode, class))
1570 fatal_insn ("Two dissimilar machine modes both need groups of consecutive regs of the same class",
1571 chain->insn);
1573 else if (size == 1)
1575 this_needs->regs[(unsigned char)rld[i].nongroup][(int) class] += 1;
1576 p = reg_class_superclasses[(int) class];
1577 while (*p != LIM_REG_CLASSES)
1578 this_needs->regs[(unsigned char)rld[i].nongroup][(int) *p++] += 1;
1580 else
1581 abort ();
1584 /* All reloads have been counted for this insn;
1585 now merge the various times of use.
1586 This sets insn_needs, etc., to the maximum total number
1587 of registers needed at any point in this insn. */
1589 for (i = 0; i < N_REG_CLASSES; i++)
1591 int j, in_max, out_max;
1593 /* Compute normal and nongroup needs. */
1594 for (j = 0; j <= 1; j++)
1596 int k;
1597 for (in_max = 0, out_max = 0, k = 0; k < reload_n_operands; k++)
1599 in_max = MAX (in_max,
1600 (insn_needs.in_addr[k].regs[j][i]
1601 + insn_needs.in_addr_addr[k].regs[j][i]));
1602 out_max = MAX (out_max, insn_needs.out_addr[k].regs[j][i]);
1603 out_max = MAX (out_max,
1604 insn_needs.out_addr_addr[k].regs[j][i]);
1607 /* RELOAD_FOR_INSN reloads conflict with inputs, outputs,
1608 and operand addresses but not things used to reload
1609 them. Similarly, RELOAD_FOR_OPERAND_ADDRESS reloads
1610 don't conflict with things needed to reload inputs or
1611 outputs. */
1613 in_max = MAX (MAX (insn_needs.op_addr.regs[j][i],
1614 insn_needs.op_addr_reload.regs[j][i]),
1615 in_max);
1617 out_max = MAX (out_max, insn_needs.insn.regs[j][i]);
1619 insn_needs.input.regs[j][i]
1620 = MAX (insn_needs.input.regs[j][i]
1621 + insn_needs.op_addr.regs[j][i]
1622 + insn_needs.insn.regs[j][i],
1623 in_max + insn_needs.input.regs[j][i]);
1625 insn_needs.output.regs[j][i] += out_max;
1626 insn_needs.other.regs[j][i]
1627 += MAX (MAX (insn_needs.input.regs[j][i],
1628 insn_needs.output.regs[j][i]),
1629 insn_needs.other_addr.regs[j][i]);
1633 /* Now compute group needs. */
1634 for (in_max = 0, out_max = 0, j = 0; j < reload_n_operands; j++)
1636 in_max = MAX (in_max, insn_needs.in_addr[j].groups[i]);
1637 in_max = MAX (in_max, insn_needs.in_addr_addr[j].groups[i]);
1638 out_max = MAX (out_max, insn_needs.out_addr[j].groups[i]);
1639 out_max = MAX (out_max, insn_needs.out_addr_addr[j].groups[i]);
1642 in_max = MAX (MAX (insn_needs.op_addr.groups[i],
1643 insn_needs.op_addr_reload.groups[i]),
1644 in_max);
1645 out_max = MAX (out_max, insn_needs.insn.groups[i]);
1647 insn_needs.input.groups[i]
1648 = MAX (insn_needs.input.groups[i]
1649 + insn_needs.op_addr.groups[i]
1650 + insn_needs.insn.groups[i],
1651 in_max + insn_needs.input.groups[i]);
1653 insn_needs.output.groups[i] += out_max;
1654 insn_needs.other.groups[i]
1655 += MAX (MAX (insn_needs.input.groups[i],
1656 insn_needs.output.groups[i]),
1657 insn_needs.other_addr.groups[i]);
1660 /* Record the needs for later. */
1661 chain->need = insn_needs.other;
1664 /* Find a group of exactly 2 registers.
1666 First try to fill out the group by spilling a single register which
1667 would allow completion of the group.
1669 Then try to create a new group from a pair of registers, neither of
1670 which are explicitly used.
1672 Then try to create a group from any pair of registers. */
1674 static void
1675 find_tworeg_group (chain, class, dumpfile)
1676 struct insn_chain *chain;
1677 int class;
1678 FILE *dumpfile;
1680 int i;
1681 /* First, look for a register that will complete a group. */
1682 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1684 int j, other;
1686 j = potential_reload_regs[i];
1687 if (j >= 0 && ! TEST_HARD_REG_BIT (bad_spill_regs, j)
1688 && ((j > 0 && (other = j - 1, spill_reg_order[other] >= 0)
1689 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1690 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1691 && HARD_REGNO_MODE_OK (other, chain->group_mode[class])
1692 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, other)
1693 /* We don't want one part of another group.
1694 We could get "two groups" that overlap! */
1695 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, other))
1696 || (j < FIRST_PSEUDO_REGISTER - 1
1697 && (other = j + 1, spill_reg_order[other] >= 0)
1698 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1699 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1700 && HARD_REGNO_MODE_OK (j, chain->group_mode[class])
1701 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, other)
1702 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, other))))
1704 register enum reg_class *p;
1706 /* We have found one that will complete a group,
1707 so count off one group as provided. */
1708 chain->need.groups[class]--;
1709 p = reg_class_superclasses[class];
1710 while (*p != LIM_REG_CLASSES)
1712 if (chain->group_size [(int) *p] <= chain->group_size [class])
1713 chain->need.groups[(int) *p]--;
1714 p++;
1717 /* Indicate both these regs are part of a group. */
1718 SET_HARD_REG_BIT (chain->counted_for_groups, j);
1719 SET_HARD_REG_BIT (chain->counted_for_groups, other);
1720 break;
1723 /* We can't complete a group, so start one. */
1724 if (i == FIRST_PSEUDO_REGISTER)
1725 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1727 int j, k;
1728 j = potential_reload_regs[i];
1729 /* Verify that J+1 is a potential reload reg. */
1730 for (k = 0; k < FIRST_PSEUDO_REGISTER; k++)
1731 if (potential_reload_regs[k] == j + 1)
1732 break;
1733 if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1734 && k < FIRST_PSEUDO_REGISTER
1735 && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1736 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1737 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1738 && HARD_REGNO_MODE_OK (j, chain->group_mode[class])
1739 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, j + 1)
1740 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + 1))
1741 break;
1744 /* I should be the index in potential_reload_regs
1745 of the new reload reg we have found. */
1747 new_spill_reg (chain, i, class, 0, dumpfile);
1750 /* Find a group of more than 2 registers.
1751 Look for a sufficient sequence of unspilled registers, and spill them all
1752 at once. */
1754 static void
1755 find_group (chain, class, dumpfile)
1756 struct insn_chain *chain;
1757 int class;
1758 FILE *dumpfile;
1760 int i;
1762 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1764 int j = potential_reload_regs[i];
1766 if (j >= 0
1767 && j + chain->group_size[class] <= FIRST_PSEUDO_REGISTER
1768 && HARD_REGNO_MODE_OK (j, chain->group_mode[class]))
1770 int k;
1771 /* Check each reg in the sequence. */
1772 for (k = 0; k < chain->group_size[class]; k++)
1773 if (! (spill_reg_order[j + k] < 0
1774 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + k)
1775 && TEST_HARD_REG_BIT (reg_class_contents[class], j + k)))
1776 break;
1777 /* We got a full sequence, so spill them all. */
1778 if (k == chain->group_size[class])
1780 register enum reg_class *p;
1781 for (k = 0; k < chain->group_size[class]; k++)
1783 int idx;
1784 SET_HARD_REG_BIT (chain->counted_for_groups, j + k);
1785 for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
1786 if (potential_reload_regs[idx] == j + k)
1787 break;
1788 new_spill_reg (chain, idx, class, 0, dumpfile);
1791 /* We have found one that will complete a group,
1792 so count off one group as provided. */
1793 chain->need.groups[class]--;
1794 p = reg_class_superclasses[class];
1795 while (*p != LIM_REG_CLASSES)
1797 if (chain->group_size [(int) *p]
1798 <= chain->group_size [class])
1799 chain->need.groups[(int) *p]--;
1800 p++;
1802 return;
1806 /* There are no groups left. */
1807 spill_failure (chain->insn);
1808 failure = 1;
1811 /* If pseudo REG conflicts with one of our reload registers, mark it as
1812 spilled. */
1813 static void
1814 maybe_mark_pseudo_spilled (reg)
1815 int reg;
1817 int i;
1818 int r = reg_renumber[reg];
1819 int nregs;
1821 if (r < 0)
1822 abort ();
1823 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
1824 for (i = 0; i < n_spills; i++)
1825 if (r <= spill_regs[i] && r + nregs > spill_regs[i])
1827 SET_REGNO_REG_SET (spilled_pseudos, reg);
1828 return;
1832 /* Find more reload regs to satisfy the remaining need of an insn, which
1833 is given by CHAIN.
1834 Do it by ascending class number, since otherwise a reg
1835 might be spilled for a big class and might fail to count
1836 for a smaller class even though it belongs to that class.
1838 Count spilled regs in `spills', and add entries to
1839 `spill_regs' and `spill_reg_order'.
1841 ??? Note there is a problem here.
1842 When there is a need for a group in a high-numbered class,
1843 and also need for non-group regs that come from a lower class,
1844 the non-group regs are chosen first. If there aren't many regs,
1845 they might leave no room for a group.
1847 This was happening on the 386. To fix it, we added the code
1848 that calls possible_group_p, so that the lower class won't
1849 break up the last possible group.
1851 Really fixing the problem would require changes above
1852 in counting the regs already spilled, and in choose_reload_regs.
1853 It might be hard to avoid introducing bugs there. */
1855 static void
1856 find_reload_regs (chain, dumpfile)
1857 struct insn_chain *chain;
1858 FILE *dumpfile;
1860 int i, class;
1861 short *group_needs = chain->need.groups;
1862 short *simple_needs = chain->need.regs[0];
1863 short *nongroup_needs = chain->need.regs[1];
1865 if (dumpfile)
1866 fprintf (dumpfile, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1868 /* Compute the order of preference for hard registers to spill.
1869 Store them by decreasing preference in potential_reload_regs. */
1871 order_regs_for_reload (chain);
1873 /* So far, no hard regs have been spilled. */
1874 n_spills = 0;
1875 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1876 spill_reg_order[i] = -1;
1878 CLEAR_HARD_REG_SET (chain->used_spill_regs);
1879 CLEAR_HARD_REG_SET (chain->counted_for_groups);
1880 CLEAR_HARD_REG_SET (chain->counted_for_nongroups);
1882 for (class = 0; class < N_REG_CLASSES; class++)
1884 /* First get the groups of registers.
1885 If we got single registers first, we might fragment
1886 possible groups. */
1887 while (group_needs[class] > 0)
1889 /* If any single spilled regs happen to form groups,
1890 count them now. Maybe we don't really need
1891 to spill another group. */
1892 count_possible_groups (chain, class);
1894 if (group_needs[class] <= 0)
1895 break;
1897 /* Groups of size 2, the only groups used on most machines,
1898 are treated specially. */
1899 if (chain->group_size[class] == 2)
1900 find_tworeg_group (chain, class, dumpfile);
1901 else
1902 find_group (chain, class, dumpfile);
1903 if (failure)
1904 return;
1907 /* Now similarly satisfy all need for single registers. */
1909 while (simple_needs[class] > 0 || nongroup_needs[class] > 0)
1911 /* If we spilled enough regs, but they weren't counted
1912 against the non-group need, see if we can count them now.
1913 If so, we can avoid some actual spilling. */
1914 if (simple_needs[class] <= 0 && nongroup_needs[class] > 0)
1915 for (i = 0; i < n_spills; i++)
1917 int regno = spill_regs[i];
1918 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1919 && !TEST_HARD_REG_BIT (chain->counted_for_groups, regno)
1920 && !TEST_HARD_REG_BIT (chain->counted_for_nongroups, regno)
1921 && nongroup_needs[class] > 0)
1923 register enum reg_class *p;
1925 SET_HARD_REG_BIT (chain->counted_for_nongroups, regno);
1926 nongroup_needs[class]--;
1927 p = reg_class_superclasses[class];
1928 while (*p != LIM_REG_CLASSES)
1929 nongroup_needs[(int) *p++]--;
1933 if (simple_needs[class] <= 0 && nongroup_needs[class] <= 0)
1934 break;
1936 /* Consider the potential reload regs that aren't
1937 yet in use as reload regs, in order of preference.
1938 Find the most preferred one that's in this class. */
1940 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1942 int regno = potential_reload_regs[i];
1943 if (regno >= 0
1944 && TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1945 /* If this reg will not be available for groups,
1946 pick one that does not foreclose possible groups.
1947 This is a kludge, and not very general,
1948 but it should be sufficient to make the 386 work,
1949 and the problem should not occur on machines with
1950 more registers. */
1951 && (nongroup_needs[class] == 0
1952 || possible_group_p (chain, regno)))
1953 break;
1956 /* If we couldn't get a register, try to get one even if we
1957 might foreclose possible groups. This may cause problems
1958 later, but that's better than aborting now, since it is
1959 possible that we will, in fact, be able to form the needed
1960 group even with this allocation. */
1962 if (i >= FIRST_PSEUDO_REGISTER
1963 && asm_noperands (chain->insn) < 0)
1964 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1965 if (potential_reload_regs[i] >= 0
1966 && TEST_HARD_REG_BIT (reg_class_contents[class],
1967 potential_reload_regs[i]))
1968 break;
1970 /* I should be the index in potential_reload_regs
1971 of the new reload reg we have found. */
1973 new_spill_reg (chain, i, class, 1, dumpfile);
1974 if (failure)
1975 return;
1979 /* We know which hard regs to use, now mark the pseudos that live in them
1980 as needing to be kicked out. */
1981 EXECUTE_IF_SET_IN_REG_SET
1982 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
1984 maybe_mark_pseudo_spilled (i);
1986 EXECUTE_IF_SET_IN_REG_SET
1987 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
1989 maybe_mark_pseudo_spilled (i);
1992 IOR_HARD_REG_SET (used_spill_regs, chain->used_spill_regs);
1995 void
1996 dump_needs (chain, dumpfile)
1997 struct insn_chain *chain;
1998 FILE *dumpfile;
2000 static const char * const reg_class_names[] = REG_CLASS_NAMES;
2001 int i;
2002 struct needs *n = &chain->need;
2004 for (i = 0; i < N_REG_CLASSES; i++)
2006 if (n->regs[i][0] > 0)
2007 fprintf (dumpfile,
2008 ";; Need %d reg%s of class %s.\n",
2009 n->regs[i][0], n->regs[i][0] == 1 ? "" : "s",
2010 reg_class_names[i]);
2011 if (n->regs[i][1] > 0)
2012 fprintf (dumpfile,
2013 ";; Need %d nongroup reg%s of class %s.\n",
2014 n->regs[i][1], n->regs[i][1] == 1 ? "" : "s",
2015 reg_class_names[i]);
2016 if (n->groups[i] > 0)
2017 fprintf (dumpfile,
2018 ";; Need %d group%s (%smode) of class %s.\n",
2019 n->groups[i], n->groups[i] == 1 ? "" : "s",
2020 GET_MODE_NAME(chain->group_mode[i]),
2021 reg_class_names[i]);
2025 /* Delete all insns that were inserted by emit_caller_save_insns during
2026 this iteration. */
2027 static void
2028 delete_caller_save_insns ()
2030 struct insn_chain *c = reload_insn_chain;
2032 while (c != 0)
2034 while (c != 0 && c->is_caller_save_insn)
2036 struct insn_chain *next = c->next;
2037 rtx insn = c->insn;
2039 if (insn == BLOCK_HEAD (c->block))
2040 BLOCK_HEAD (c->block) = NEXT_INSN (insn);
2041 if (insn == BLOCK_END (c->block))
2042 BLOCK_END (c->block) = PREV_INSN (insn);
2043 if (c == reload_insn_chain)
2044 reload_insn_chain = next;
2046 if (NEXT_INSN (insn) != 0)
2047 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2048 if (PREV_INSN (insn) != 0)
2049 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2051 if (next)
2052 next->prev = c->prev;
2053 if (c->prev)
2054 c->prev->next = next;
2055 c->next = unused_insn_chains;
2056 unused_insn_chains = c;
2057 c = next;
2059 if (c != 0)
2060 c = c->next;
2064 /* Nonzero if, after spilling reg REGNO for non-groups,
2065 it will still be possible to find a group if we still need one. */
2067 static int
2068 possible_group_p (chain, regno)
2069 struct insn_chain *chain;
2070 int regno;
2072 int i;
2073 int class = (int) NO_REGS;
2075 for (i = 0; i < (int) N_REG_CLASSES; i++)
2076 if (chain->need.groups[i] > 0)
2078 class = i;
2079 break;
2082 if (class == (int) NO_REGS)
2083 return 1;
2085 /* Consider each pair of consecutive registers. */
2086 for (i = 0; i < FIRST_PSEUDO_REGISTER - 1; i++)
2088 /* Ignore pairs that include reg REGNO. */
2089 if (i == regno || i + 1 == regno)
2090 continue;
2092 /* Ignore pairs that are outside the class that needs the group.
2093 ??? Here we fail to handle the case where two different classes
2094 independently need groups. But this never happens with our
2095 current machine descriptions. */
2096 if (! (TEST_HARD_REG_BIT (reg_class_contents[class], i)
2097 && TEST_HARD_REG_BIT (reg_class_contents[class], i + 1)))
2098 continue;
2100 /* A pair of consecutive regs we can still spill does the trick. */
2101 if (spill_reg_order[i] < 0 && spill_reg_order[i + 1] < 0
2102 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2103 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1))
2104 return 1;
2106 /* A pair of one already spilled and one we can spill does it
2107 provided the one already spilled is not otherwise reserved. */
2108 if (spill_reg_order[i] < 0
2109 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2110 && spill_reg_order[i + 1] >= 0
2111 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, i + 1)
2112 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, i + 1))
2113 return 1;
2114 if (spill_reg_order[i + 1] < 0
2115 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1)
2116 && spill_reg_order[i] >= 0
2117 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, i)
2118 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, i))
2119 return 1;
2122 return 0;
2125 /* Count any groups of CLASS that can be formed from the registers recently
2126 spilled. */
2128 static void
2129 count_possible_groups (chain, class)
2130 struct insn_chain *chain;
2131 int class;
2133 HARD_REG_SET new;
2134 int i, j;
2136 /* Now find all consecutive groups of spilled registers
2137 and mark each group off against the need for such groups.
2138 But don't count them against ordinary need, yet. */
2140 if (chain->group_size[class] == 0)
2141 return;
2143 CLEAR_HARD_REG_SET (new);
2145 /* Make a mask of all the regs that are spill regs in class I. */
2146 for (i = 0; i < n_spills; i++)
2148 int regno = spill_regs[i];
2150 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
2151 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, regno)
2152 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, regno))
2153 SET_HARD_REG_BIT (new, regno);
2156 /* Find each consecutive group of them. */
2157 for (i = 0; i < FIRST_PSEUDO_REGISTER && chain->need.groups[class] > 0; i++)
2158 if (TEST_HARD_REG_BIT (new, i)
2159 && i + chain->group_size[class] <= FIRST_PSEUDO_REGISTER
2160 && HARD_REGNO_MODE_OK (i, chain->group_mode[class]))
2162 for (j = 1; j < chain->group_size[class]; j++)
2163 if (! TEST_HARD_REG_BIT (new, i + j))
2164 break;
2166 if (j == chain->group_size[class])
2168 /* We found a group. Mark it off against this class's need for
2169 groups, and against each superclass too. */
2170 register enum reg_class *p;
2172 chain->need.groups[class]--;
2173 p = reg_class_superclasses[class];
2174 while (*p != LIM_REG_CLASSES)
2176 if (chain->group_size [(int) *p] <= chain->group_size [class])
2177 chain->need.groups[(int) *p]--;
2178 p++;
2181 /* Don't count these registers again. */
2182 for (j = 0; j < chain->group_size[class]; j++)
2183 SET_HARD_REG_BIT (chain->counted_for_groups, i + j);
2186 /* Skip to the last reg in this group. When i is incremented above,
2187 it will then point to the first reg of the next possible group. */
2188 i += j - 1;
2192 /* ALLOCATE_MODE is a register mode that needs to be reloaded. OTHER_MODE is
2193 another mode that needs to be reloaded for the same register class CLASS.
2194 If any reg in CLASS allows ALLOCATE_MODE but not OTHER_MODE, fail.
2195 ALLOCATE_MODE will never be smaller than OTHER_MODE.
2197 This code used to also fail if any reg in CLASS allows OTHER_MODE but not
2198 ALLOCATE_MODE. This test is unnecessary, because we will never try to put
2199 something of mode ALLOCATE_MODE into an OTHER_MODE register. Testing this
2200 causes unnecessary failures on machines requiring alignment of register
2201 groups when the two modes are different sizes, because the larger mode has
2202 more strict alignment rules than the smaller mode. */
2204 static int
2205 modes_equiv_for_class_p (allocate_mode, other_mode, class)
2206 enum machine_mode allocate_mode, other_mode;
2207 enum reg_class class;
2209 register int regno;
2210 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2212 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno)
2213 && HARD_REGNO_MODE_OK (regno, allocate_mode)
2214 && ! HARD_REGNO_MODE_OK (regno, other_mode))
2215 return 0;
2217 return 1;
2220 /* Handle the failure to find a register to spill.
2221 INSN should be one of the insns which needed this particular spill reg. */
2223 static void
2224 spill_failure (insn)
2225 rtx insn;
2227 if (asm_noperands (PATTERN (insn)) >= 0)
2228 error_for_asm (insn, "`asm' needs too many reloads");
2229 else
2230 fatal_insn ("Unable to find a register to spill.", insn);
2233 /* Add a new register to the tables of available spill-registers.
2234 CHAIN is the insn for which the register will be used; we decrease the
2235 needs of that insn.
2236 I is the index of this register in potential_reload_regs.
2237 CLASS is the regclass whose need is being satisfied.
2238 NONGROUP is 0 if this register is part of a group.
2239 DUMPFILE is the same as the one that `reload' got. */
2241 static void
2242 new_spill_reg (chain, i, class, nongroup, dumpfile)
2243 struct insn_chain *chain;
2244 int i;
2245 int class;
2246 int nongroup;
2247 FILE *dumpfile;
2249 register enum reg_class *p;
2250 int regno = potential_reload_regs[i];
2252 if (i >= FIRST_PSEUDO_REGISTER)
2254 spill_failure (chain->insn);
2255 failure = 1;
2256 return;
2259 if (TEST_HARD_REG_BIT (bad_spill_regs, regno))
2261 static const char * const reg_class_names[] = REG_CLASS_NAMES;
2263 if (asm_noperands (PATTERN (chain->insn)) < 0)
2265 /* The error message is still correct - we know only that it wasn't
2266 an asm statement that caused the problem, but one of the global
2267 registers declared by the users might have screwed us. */
2268 error ("fixed or forbidden register %d (%s) was spilled for class %s.",
2269 regno, reg_names[regno], reg_class_names[class]);
2270 error ("This may be due to a compiler bug or to impossible asm");
2271 error ("statements or clauses.");
2272 fatal_insn ("This is the instruction:", chain->insn);
2274 error_for_asm (chain->insn, "Invalid `asm' statement:");
2275 error_for_asm (chain->insn,
2276 "fixed or forbidden register %d (%s) was spilled for class %s.",
2277 regno, reg_names[regno], reg_class_names[class]);
2278 failure = 1;
2279 return;
2282 /* Make reg REGNO an additional reload reg. */
2284 potential_reload_regs[i] = -1;
2285 spill_regs[n_spills] = regno;
2286 spill_reg_order[regno] = n_spills;
2287 if (dumpfile)
2288 fprintf (dumpfile, "Spilling reg %d.\n", regno);
2289 SET_HARD_REG_BIT (chain->used_spill_regs, regno);
2291 /* Clear off the needs we just satisfied. */
2293 chain->need.regs[0][class]--;
2294 p = reg_class_superclasses[class];
2295 while (*p != LIM_REG_CLASSES)
2296 chain->need.regs[0][(int) *p++]--;
2298 if (nongroup && chain->need.regs[1][class] > 0)
2300 SET_HARD_REG_BIT (chain->counted_for_nongroups, regno);
2301 chain->need.regs[1][class]--;
2302 p = reg_class_superclasses[class];
2303 while (*p != LIM_REG_CLASSES)
2304 chain->need.regs[1][(int) *p++]--;
2307 n_spills++;
2310 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2311 data that is dead in INSN. */
2313 static void
2314 delete_dead_insn (insn)
2315 rtx insn;
2317 rtx prev = prev_real_insn (insn);
2318 rtx prev_dest;
2320 /* If the previous insn sets a register that dies in our insn, delete it
2321 too. */
2322 if (prev && GET_CODE (PATTERN (prev)) == SET
2323 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
2324 && reg_mentioned_p (prev_dest, PATTERN (insn))
2325 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
2326 && ! side_effects_p (SET_SRC (PATTERN (prev))))
2327 delete_dead_insn (prev);
2329 PUT_CODE (insn, NOTE);
2330 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2331 NOTE_SOURCE_FILE (insn) = 0;
2334 /* Modify the home of pseudo-reg I.
2335 The new home is present in reg_renumber[I].
2337 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2338 or it may be -1, meaning there is none or it is not relevant.
2339 This is used so that all pseudos spilled from a given hard reg
2340 can share one stack slot. */
2342 static void
2343 alter_reg (i, from_reg)
2344 register int i;
2345 int from_reg;
2347 /* When outputting an inline function, this can happen
2348 for a reg that isn't actually used. */
2349 if (regno_reg_rtx[i] == 0)
2350 return;
2352 /* If the reg got changed to a MEM at rtl-generation time,
2353 ignore it. */
2354 if (GET_CODE (regno_reg_rtx[i]) != REG)
2355 return;
2357 /* Modify the reg-rtx to contain the new hard reg
2358 number or else to contain its pseudo reg number. */
2359 REGNO (regno_reg_rtx[i])
2360 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
2362 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2363 allocate a stack slot for it. */
2365 if (reg_renumber[i] < 0
2366 && REG_N_REFS (i) > 0
2367 && reg_equiv_constant[i] == 0
2368 && reg_equiv_memory_loc[i] == 0)
2370 register rtx x;
2371 int inherent_size = PSEUDO_REGNO_BYTES (i);
2372 int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2373 int adjust = 0;
2375 /* Each pseudo reg has an inherent size which comes from its own mode,
2376 and a total size which provides room for paradoxical subregs
2377 which refer to the pseudo reg in wider modes.
2379 We can use a slot already allocated if it provides both
2380 enough inherent space and enough total space.
2381 Otherwise, we allocate a new slot, making sure that it has no less
2382 inherent space, and no less total space, then the previous slot. */
2383 if (from_reg == -1)
2385 /* No known place to spill from => no slot to reuse. */
2386 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
2387 inherent_size == total_size ? 0 : -1);
2388 if (BYTES_BIG_ENDIAN)
2389 /* Cancel the big-endian correction done in assign_stack_local.
2390 Get the address of the beginning of the slot.
2391 This is so we can do a big-endian correction unconditionally
2392 below. */
2393 adjust = inherent_size - total_size;
2395 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2397 /* Reuse a stack slot if possible. */
2398 else if (spill_stack_slot[from_reg] != 0
2399 && spill_stack_slot_width[from_reg] >= total_size
2400 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2401 >= inherent_size))
2402 x = spill_stack_slot[from_reg];
2403 /* Allocate a bigger slot. */
2404 else
2406 /* Compute maximum size needed, both for inherent size
2407 and for total size. */
2408 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2409 rtx stack_slot;
2410 if (spill_stack_slot[from_reg])
2412 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2413 > inherent_size)
2414 mode = GET_MODE (spill_stack_slot[from_reg]);
2415 if (spill_stack_slot_width[from_reg] > total_size)
2416 total_size = spill_stack_slot_width[from_reg];
2418 /* Make a slot with that size. */
2419 x = assign_stack_local (mode, total_size,
2420 inherent_size == total_size ? 0 : -1);
2421 stack_slot = x;
2422 if (BYTES_BIG_ENDIAN)
2424 /* Cancel the big-endian correction done in assign_stack_local.
2425 Get the address of the beginning of the slot.
2426 This is so we can do a big-endian correction unconditionally
2427 below. */
2428 adjust = GET_MODE_SIZE (mode) - total_size;
2429 if (adjust)
2430 stack_slot = gen_rtx_MEM (mode_for_size (total_size
2431 * BITS_PER_UNIT,
2432 MODE_INT, 1),
2433 plus_constant (XEXP (x, 0), adjust));
2435 spill_stack_slot[from_reg] = stack_slot;
2436 spill_stack_slot_width[from_reg] = total_size;
2439 /* On a big endian machine, the "address" of the slot
2440 is the address of the low part that fits its inherent mode. */
2441 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2442 adjust += (total_size - inherent_size);
2444 /* If we have any adjustment to make, or if the stack slot is the
2445 wrong mode, make a new stack slot. */
2446 if (adjust != 0 || GET_MODE (x) != GET_MODE (regno_reg_rtx[i]))
2448 x = gen_rtx_MEM (GET_MODE (regno_reg_rtx[i]),
2449 plus_constant (XEXP (x, 0), adjust));
2451 /* If this was shared among registers, must ensure we never
2452 set it readonly since that can cause scheduling
2453 problems. Note we would only have in this adjustment
2454 case in any event, since the code above doesn't set it. */
2456 if (from_reg == -1)
2457 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2460 /* Save the stack slot for later. */
2461 reg_equiv_memory_loc[i] = x;
2465 /* Mark the slots in regs_ever_live for the hard regs
2466 used by pseudo-reg number REGNO. */
2468 void
2469 mark_home_live (regno)
2470 int regno;
2472 register int i, lim;
2473 i = reg_renumber[regno];
2474 if (i < 0)
2475 return;
2476 lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2477 while (i < lim)
2478 regs_ever_live[i++] = 1;
2481 /* This function handles the tracking of elimination offsets around branches.
2483 X is a piece of RTL being scanned.
2485 INSN is the insn that it came from, if any.
2487 INITIAL_P is non-zero if we are to set the offset to be the initial
2488 offset and zero if we are setting the offset of the label to be the
2489 current offset. */
2491 static void
2492 set_label_offsets (x, insn, initial_p)
2493 rtx x;
2494 rtx insn;
2495 int initial_p;
2497 enum rtx_code code = GET_CODE (x);
2498 rtx tem;
2499 unsigned int i;
2500 struct elim_table *p;
2502 switch (code)
2504 case LABEL_REF:
2505 if (LABEL_REF_NONLOCAL_P (x))
2506 return;
2508 x = XEXP (x, 0);
2510 /* ... fall through ... */
2512 case CODE_LABEL:
2513 /* If we know nothing about this label, set the desired offsets. Note
2514 that this sets the offset at a label to be the offset before a label
2515 if we don't know anything about the label. This is not correct for
2516 the label after a BARRIER, but is the best guess we can make. If
2517 we guessed wrong, we will suppress an elimination that might have
2518 been possible had we been able to guess correctly. */
2520 if (! offsets_known_at[CODE_LABEL_NUMBER (x)])
2522 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2523 offsets_at[CODE_LABEL_NUMBER (x)][i]
2524 = (initial_p ? reg_eliminate[i].initial_offset
2525 : reg_eliminate[i].offset);
2526 offsets_known_at[CODE_LABEL_NUMBER (x)] = 1;
2529 /* Otherwise, if this is the definition of a label and it is
2530 preceded by a BARRIER, set our offsets to the known offset of
2531 that label. */
2533 else if (x == insn
2534 && (tem = prev_nonnote_insn (insn)) != 0
2535 && GET_CODE (tem) == BARRIER)
2536 set_offsets_for_label (insn);
2537 else
2538 /* If neither of the above cases is true, compare each offset
2539 with those previously recorded and suppress any eliminations
2540 where the offsets disagree. */
2542 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2543 if (offsets_at[CODE_LABEL_NUMBER (x)][i]
2544 != (initial_p ? reg_eliminate[i].initial_offset
2545 : reg_eliminate[i].offset))
2546 reg_eliminate[i].can_eliminate = 0;
2548 return;
2550 case JUMP_INSN:
2551 set_label_offsets (PATTERN (insn), insn, initial_p);
2553 /* ... fall through ... */
2555 case INSN:
2556 case CALL_INSN:
2557 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2558 and hence must have all eliminations at their initial offsets. */
2559 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2560 if (REG_NOTE_KIND (tem) == REG_LABEL)
2561 set_label_offsets (XEXP (tem, 0), insn, 1);
2562 return;
2564 case ADDR_VEC:
2565 case ADDR_DIFF_VEC:
2566 /* Each of the labels in the address vector must be at their initial
2567 offsets. We want the first field for ADDR_VEC and the second
2568 field for ADDR_DIFF_VEC. */
2570 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2571 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2572 insn, initial_p);
2573 return;
2575 case SET:
2576 /* We only care about setting PC. If the source is not RETURN,
2577 IF_THEN_ELSE, or a label, disable any eliminations not at
2578 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2579 isn't one of those possibilities. For branches to a label,
2580 call ourselves recursively.
2582 Note that this can disable elimination unnecessarily when we have
2583 a non-local goto since it will look like a non-constant jump to
2584 someplace in the current function. This isn't a significant
2585 problem since such jumps will normally be when all elimination
2586 pairs are back to their initial offsets. */
2588 if (SET_DEST (x) != pc_rtx)
2589 return;
2591 switch (GET_CODE (SET_SRC (x)))
2593 case PC:
2594 case RETURN:
2595 return;
2597 case LABEL_REF:
2598 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2599 return;
2601 case IF_THEN_ELSE:
2602 tem = XEXP (SET_SRC (x), 1);
2603 if (GET_CODE (tem) == LABEL_REF)
2604 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2605 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2606 break;
2608 tem = XEXP (SET_SRC (x), 2);
2609 if (GET_CODE (tem) == LABEL_REF)
2610 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2611 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2612 break;
2613 return;
2615 default:
2616 break;
2619 /* If we reach here, all eliminations must be at their initial
2620 offset because we are doing a jump to a variable address. */
2621 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2622 if (p->offset != p->initial_offset)
2623 p->can_eliminate = 0;
2624 break;
2626 default:
2627 break;
2631 /* Scan X and replace any eliminable registers (such as fp) with a
2632 replacement (such as sp), plus an offset.
2634 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2635 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2636 MEM, we are allowed to replace a sum of a register and the constant zero
2637 with the register, which we cannot do outside a MEM. In addition, we need
2638 to record the fact that a register is referenced outside a MEM.
2640 If INSN is an insn, it is the insn containing X. If we replace a REG
2641 in a SET_DEST with an equivalent MEM and INSN is non-zero, write a
2642 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2643 the REG is being modified.
2645 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2646 That's used when we eliminate in expressions stored in notes.
2647 This means, do not set ref_outside_mem even if the reference
2648 is outside of MEMs.
2650 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2651 replacements done assuming all offsets are at their initial values. If
2652 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2653 encounter, return the actual location so that find_reloads will do
2654 the proper thing. */
2657 eliminate_regs (x, mem_mode, insn)
2658 rtx x;
2659 enum machine_mode mem_mode;
2660 rtx insn;
2662 enum rtx_code code = GET_CODE (x);
2663 struct elim_table *ep;
2664 int regno;
2665 rtx new;
2666 int i, j;
2667 const char *fmt;
2668 int copied = 0;
2670 if (! current_function_decl)
2671 return x;
2673 switch (code)
2675 case CONST_INT:
2676 case CONST_DOUBLE:
2677 case CONST:
2678 case SYMBOL_REF:
2679 case CODE_LABEL:
2680 case PC:
2681 case CC0:
2682 case ASM_INPUT:
2683 case ADDR_VEC:
2684 case ADDR_DIFF_VEC:
2685 case RETURN:
2686 return x;
2688 case ADDRESSOF:
2689 /* This is only for the benefit of the debugging backends, which call
2690 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2691 removed after CSE. */
2692 new = eliminate_regs (XEXP (x, 0), 0, insn);
2693 if (GET_CODE (new) == MEM)
2694 return XEXP (new, 0);
2695 return x;
2697 case REG:
2698 regno = REGNO (x);
2700 /* First handle the case where we encounter a bare register that
2701 is eliminable. Replace it with a PLUS. */
2702 if (regno < FIRST_PSEUDO_REGISTER)
2704 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2705 ep++)
2706 if (ep->from_rtx == x && ep->can_eliminate)
2707 return plus_constant (ep->to_rtx, ep->previous_offset);
2710 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2711 && reg_equiv_constant[regno]
2712 && ! CONSTANT_P (reg_equiv_constant[regno]))
2713 return eliminate_regs (copy_rtx (reg_equiv_constant[regno]),
2714 mem_mode, insn);
2715 return x;
2717 /* You might think handling MINUS in a manner similar to PLUS is a
2718 good idea. It is not. It has been tried multiple times and every
2719 time the change has had to have been reverted.
2721 Other parts of reload know a PLUS is special (gen_reload for example)
2722 and require special code to handle code a reloaded PLUS operand.
2724 Also consider backends where the flags register is clobbered by a
2725 MINUS, but we can emit a PLUS that does not clobber flags (ia32,
2726 lea instruction comes to mind). If we try to reload a MINUS, we
2727 may kill the flags register that was holding a useful value.
2729 So, please before trying to handle MINUS, consider reload as a
2730 whole instead of this little section as well as the backend issues. */
2731 case PLUS:
2732 /* If this is the sum of an eliminable register and a constant, rework
2733 the sum. */
2734 if (GET_CODE (XEXP (x, 0)) == REG
2735 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2736 && CONSTANT_P (XEXP (x, 1)))
2738 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2739 ep++)
2740 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2742 /* The only time we want to replace a PLUS with a REG (this
2743 occurs when the constant operand of the PLUS is the negative
2744 of the offset) is when we are inside a MEM. We won't want
2745 to do so at other times because that would change the
2746 structure of the insn in a way that reload can't handle.
2747 We special-case the commonest situation in
2748 eliminate_regs_in_insn, so just replace a PLUS with a
2749 PLUS here, unless inside a MEM. */
2750 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2751 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2752 return ep->to_rtx;
2753 else
2754 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2755 plus_constant (XEXP (x, 1),
2756 ep->previous_offset));
2759 /* If the register is not eliminable, we are done since the other
2760 operand is a constant. */
2761 return x;
2764 /* If this is part of an address, we want to bring any constant to the
2765 outermost PLUS. We will do this by doing register replacement in
2766 our operands and seeing if a constant shows up in one of them.
2768 Note that there is no risk of modifying the structure of the insn,
2769 since we only get called for its operands, thus we are either
2770 modifying the address inside a MEM, or something like an address
2771 operand of a load-address insn. */
2774 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2775 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2777 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2779 /* If one side is a PLUS and the other side is a pseudo that
2780 didn't get a hard register but has a reg_equiv_constant,
2781 we must replace the constant here since it may no longer
2782 be in the position of any operand. */
2783 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2784 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2785 && reg_renumber[REGNO (new1)] < 0
2786 && reg_equiv_constant != 0
2787 && reg_equiv_constant[REGNO (new1)] != 0)
2788 new1 = reg_equiv_constant[REGNO (new1)];
2789 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2790 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2791 && reg_renumber[REGNO (new0)] < 0
2792 && reg_equiv_constant[REGNO (new0)] != 0)
2793 new0 = reg_equiv_constant[REGNO (new0)];
2795 new = form_sum (new0, new1);
2797 /* As above, if we are not inside a MEM we do not want to
2798 turn a PLUS into something else. We might try to do so here
2799 for an addition of 0 if we aren't optimizing. */
2800 if (! mem_mode && GET_CODE (new) != PLUS)
2801 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2802 else
2803 return new;
2806 return x;
2808 case MULT:
2809 /* If this is the product of an eliminable register and a
2810 constant, apply the distribute law and move the constant out
2811 so that we have (plus (mult ..) ..). This is needed in order
2812 to keep load-address insns valid. This case is pathological.
2813 We ignore the possibility of overflow here. */
2814 if (GET_CODE (XEXP (x, 0)) == REG
2815 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2816 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2817 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2818 ep++)
2819 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2821 if (! mem_mode
2822 /* Refs inside notes don't count for this purpose. */
2823 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2824 || GET_CODE (insn) == INSN_LIST)))
2825 ep->ref_outside_mem = 1;
2827 return
2828 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2829 ep->previous_offset * INTVAL (XEXP (x, 1)));
2832 /* ... fall through ... */
2834 case CALL:
2835 case COMPARE:
2836 /* See comments before PLUS about handling MINUS. */
2837 case MINUS:
2838 case DIV: case UDIV:
2839 case MOD: case UMOD:
2840 case AND: case IOR: case XOR:
2841 case ROTATERT: case ROTATE:
2842 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2843 case NE: case EQ:
2844 case GE: case GT: case GEU: case GTU:
2845 case LE: case LT: case LEU: case LTU:
2847 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2848 rtx new1
2849 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
2851 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2852 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2854 return x;
2856 case EXPR_LIST:
2857 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2858 if (XEXP (x, 0))
2860 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2861 if (new != XEXP (x, 0))
2863 /* If this is a REG_DEAD note, it is not valid anymore.
2864 Using the eliminated version could result in creating a
2865 REG_DEAD note for the stack or frame pointer. */
2866 if (GET_MODE (x) == REG_DEAD)
2867 return (XEXP (x, 1)
2868 ? eliminate_regs (XEXP (x, 1), mem_mode, insn)
2869 : NULL_RTX);
2871 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2875 /* ... fall through ... */
2877 case INSN_LIST:
2878 /* Now do eliminations in the rest of the chain. If this was
2879 an EXPR_LIST, this might result in allocating more memory than is
2880 strictly needed, but it simplifies the code. */
2881 if (XEXP (x, 1))
2883 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2884 if (new != XEXP (x, 1))
2885 return gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2887 return x;
2889 case PRE_INC:
2890 case POST_INC:
2891 case PRE_DEC:
2892 case POST_DEC:
2893 case STRICT_LOW_PART:
2894 case NEG: case NOT:
2895 case SIGN_EXTEND: case ZERO_EXTEND:
2896 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2897 case FLOAT: case FIX:
2898 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2899 case ABS:
2900 case SQRT:
2901 case FFS:
2902 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2903 if (new != XEXP (x, 0))
2904 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2905 return x;
2907 case SUBREG:
2908 /* Similar to above processing, but preserve SUBREG_WORD.
2909 Convert (subreg (mem)) to (mem) if not paradoxical.
2910 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2911 pseudo didn't get a hard reg, we must replace this with the
2912 eliminated version of the memory location because push_reloads
2913 may do the replacement in certain circumstances. */
2914 if (GET_CODE (SUBREG_REG (x)) == REG
2915 && (GET_MODE_SIZE (GET_MODE (x))
2916 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2917 && reg_equiv_memory_loc != 0
2918 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2920 new = SUBREG_REG (x);
2922 else
2923 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
2925 if (new != XEXP (x, 0))
2927 int x_size = GET_MODE_SIZE (GET_MODE (x));
2928 int new_size = GET_MODE_SIZE (GET_MODE (new));
2930 if (GET_CODE (new) == MEM
2931 && ((x_size < new_size
2932 #ifdef WORD_REGISTER_OPERATIONS
2933 /* On these machines, combine can create rtl of the form
2934 (set (subreg:m1 (reg:m2 R) 0) ...)
2935 where m1 < m2, and expects something interesting to
2936 happen to the entire word. Moreover, it will use the
2937 (reg:m2 R) later, expecting all bits to be preserved.
2938 So if the number of words is the same, preserve the
2939 subreg so that push_reloads can see it. */
2940 && ! ((x_size-1)/UNITS_PER_WORD == (new_size-1)/UNITS_PER_WORD)
2941 #endif
2943 || (x_size == new_size))
2946 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
2947 enum machine_mode mode = GET_MODE (x);
2949 if (BYTES_BIG_ENDIAN)
2950 offset += (MIN (UNITS_PER_WORD,
2951 GET_MODE_SIZE (GET_MODE (new)))
2952 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
2954 PUT_MODE (new, mode);
2955 XEXP (new, 0) = plus_constant (XEXP (new, 0), offset);
2956 return new;
2958 else
2959 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_WORD (x));
2962 return x;
2964 case MEM:
2965 /* This is only for the benefit of the debugging backends, which call
2966 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2967 removed after CSE. */
2968 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
2969 return eliminate_regs (XEXP (XEXP (x, 0), 0), 0, insn);
2971 /* Our only special processing is to pass the mode of the MEM to our
2972 recursive call and copy the flags. While we are here, handle this
2973 case more efficiently. */
2974 new = eliminate_regs (XEXP (x, 0), GET_MODE (x), insn);
2975 if (new != XEXP (x, 0))
2977 new = gen_rtx_MEM (GET_MODE (x), new);
2978 new->volatil = x->volatil;
2979 new->unchanging = x->unchanging;
2980 new->in_struct = x->in_struct;
2981 return new;
2983 else
2984 return x;
2986 case USE:
2987 case CLOBBER:
2988 case ASM_OPERANDS:
2989 case SET:
2990 abort ();
2992 default:
2993 break;
2996 /* Process each of our operands recursively. If any have changed, make a
2997 copy of the rtx. */
2998 fmt = GET_RTX_FORMAT (code);
2999 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3001 if (*fmt == 'e')
3003 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
3004 if (new != XEXP (x, i) && ! copied)
3006 rtx new_x = rtx_alloc (code);
3007 bcopy ((char *) x, (char *) new_x,
3008 (sizeof (*new_x) - sizeof (new_x->fld)
3009 + sizeof (new_x->fld[0]) * GET_RTX_LENGTH (code)));
3010 x = new_x;
3011 copied = 1;
3013 XEXP (x, i) = new;
3015 else if (*fmt == 'E')
3017 int copied_vec = 0;
3018 for (j = 0; j < XVECLEN (x, i); j++)
3020 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
3021 if (new != XVECEXP (x, i, j) && ! copied_vec)
3023 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
3024 XVEC (x, i)->elem);
3025 if (! copied)
3027 rtx new_x = rtx_alloc (code);
3028 bcopy ((char *) x, (char *) new_x,
3029 (sizeof (*new_x) - sizeof (new_x->fld)
3030 + (sizeof (new_x->fld[0])
3031 * GET_RTX_LENGTH (code))));
3032 x = new_x;
3033 copied = 1;
3035 XVEC (x, i) = new_v;
3036 copied_vec = 1;
3038 XVECEXP (x, i, j) = new;
3043 return x;
3046 /* Scan rtx X for modifications of elimination target registers. Update
3047 the table of eliminables to reflect the changed state. MEM_MODE is
3048 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
3050 static void
3051 elimination_effects (x, mem_mode)
3052 rtx x;
3053 enum machine_mode mem_mode;
3056 enum rtx_code code = GET_CODE (x);
3057 struct elim_table *ep;
3058 int regno;
3059 int i, j;
3060 const char *fmt;
3062 switch (code)
3064 case CONST_INT:
3065 case CONST_DOUBLE:
3066 case CONST:
3067 case SYMBOL_REF:
3068 case CODE_LABEL:
3069 case PC:
3070 case CC0:
3071 case ASM_INPUT:
3072 case ADDR_VEC:
3073 case ADDR_DIFF_VEC:
3074 case RETURN:
3075 return;
3077 case ADDRESSOF:
3078 abort ();
3080 case REG:
3081 regno = REGNO (x);
3083 /* First handle the case where we encounter a bare register that
3084 is eliminable. Replace it with a PLUS. */
3085 if (regno < FIRST_PSEUDO_REGISTER)
3087 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3088 ep++)
3089 if (ep->from_rtx == x && ep->can_eliminate)
3091 if (! mem_mode)
3092 ep->ref_outside_mem = 1;
3093 return;
3097 else if (reg_renumber[regno] < 0 && reg_equiv_constant
3098 && reg_equiv_constant[regno]
3099 && ! CONSTANT_P (reg_equiv_constant[regno]))
3100 elimination_effects (reg_equiv_constant[regno], mem_mode);
3101 return;
3103 case PRE_INC:
3104 case POST_INC:
3105 case PRE_DEC:
3106 case POST_DEC:
3107 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3108 if (ep->to_rtx == XEXP (x, 0))
3110 int size = GET_MODE_SIZE (mem_mode);
3112 /* If more bytes than MEM_MODE are pushed, account for them. */
3113 #ifdef PUSH_ROUNDING
3114 if (ep->to_rtx == stack_pointer_rtx)
3115 size = PUSH_ROUNDING (size);
3116 #endif
3117 if (code == PRE_DEC || code == POST_DEC)
3118 ep->offset += size;
3119 else
3120 ep->offset -= size;
3123 /* Fall through to generic unary operation case. */
3124 case STRICT_LOW_PART:
3125 case NEG: case NOT:
3126 case SIGN_EXTEND: case ZERO_EXTEND:
3127 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3128 case FLOAT: case FIX:
3129 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3130 case ABS:
3131 case SQRT:
3132 case FFS:
3133 elimination_effects (XEXP (x, 0), mem_mode);
3134 return;
3136 case SUBREG:
3137 if (GET_CODE (SUBREG_REG (x)) == REG
3138 && (GET_MODE_SIZE (GET_MODE (x))
3139 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3140 && reg_equiv_memory_loc != 0
3141 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
3142 return;
3144 elimination_effects (SUBREG_REG (x), mem_mode);
3145 return;
3147 case USE:
3148 /* If using a register that is the source of an eliminate we still
3149 think can be performed, note it cannot be performed since we don't
3150 know how this register is used. */
3151 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3152 if (ep->from_rtx == XEXP (x, 0))
3153 ep->can_eliminate = 0;
3155 elimination_effects (XEXP (x, 0), mem_mode);
3156 return;
3158 case CLOBBER:
3159 /* If clobbering a register that is the replacement register for an
3160 elimination we still think can be performed, note that it cannot
3161 be performed. Otherwise, we need not be concerned about it. */
3162 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3163 if (ep->to_rtx == XEXP (x, 0))
3164 ep->can_eliminate = 0;
3166 elimination_effects (XEXP (x, 0), mem_mode);
3167 return;
3169 case SET:
3170 /* Check for setting a register that we know about. */
3171 if (GET_CODE (SET_DEST (x)) == REG)
3173 /* See if this is setting the replacement register for an
3174 elimination.
3176 If DEST is the hard frame pointer, we do nothing because we
3177 assume that all assignments to the frame pointer are for
3178 non-local gotos and are being done at a time when they are valid
3179 and do not disturb anything else. Some machines want to
3180 eliminate a fake argument pointer (or even a fake frame pointer)
3181 with either the real frame or the stack pointer. Assignments to
3182 the hard frame pointer must not prevent this elimination. */
3184 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3185 ep++)
3186 if (ep->to_rtx == SET_DEST (x)
3187 && SET_DEST (x) != hard_frame_pointer_rtx)
3189 /* If it is being incremented, adjust the offset. Otherwise,
3190 this elimination can't be done. */
3191 rtx src = SET_SRC (x);
3193 if (GET_CODE (src) == PLUS
3194 && XEXP (src, 0) == SET_DEST (x)
3195 && GET_CODE (XEXP (src, 1)) == CONST_INT)
3196 ep->offset -= INTVAL (XEXP (src, 1));
3197 else
3198 ep->can_eliminate = 0;
3202 elimination_effects (SET_DEST (x), 0);
3203 elimination_effects (SET_SRC (x), 0);
3204 return;
3206 case MEM:
3207 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
3208 abort ();
3210 /* Our only special processing is to pass the mode of the MEM to our
3211 recursive call. */
3212 elimination_effects (XEXP (x, 0), GET_MODE (x));
3213 return;
3215 default:
3216 break;
3219 fmt = GET_RTX_FORMAT (code);
3220 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3222 if (*fmt == 'e')
3223 elimination_effects (XEXP (x, i), mem_mode);
3224 else if (*fmt == 'E')
3225 for (j = 0; j < XVECLEN (x, i); j++)
3226 elimination_effects (XVECEXP (x, i, j), mem_mode);
3230 /* Descend through rtx X and verify that no references to eliminable registers
3231 remain. If any do remain, mark the involved register as not
3232 eliminable. */
3233 static void
3234 check_eliminable_occurrences (x)
3235 rtx x;
3237 const char *fmt;
3238 int i;
3239 enum rtx_code code;
3241 if (x == 0)
3242 return;
3244 code = GET_CODE (x);
3246 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
3248 struct elim_table *ep;
3250 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3251 if (ep->from_rtx == x && ep->can_eliminate)
3252 ep->can_eliminate = 0;
3253 return;
3256 fmt = GET_RTX_FORMAT (code);
3257 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3259 if (*fmt == 'e')
3260 check_eliminable_occurrences (XEXP (x, i));
3261 else if (*fmt == 'E')
3263 int j;
3264 for (j = 0; j < XVECLEN (x, i); j++)
3265 check_eliminable_occurrences (XVECEXP (x, i, j));
3270 /* Scan INSN and eliminate all eliminable registers in it.
3272 If REPLACE is nonzero, do the replacement destructively. Also
3273 delete the insn as dead it if it is setting an eliminable register.
3275 If REPLACE is zero, do all our allocations in reload_obstack.
3277 If no eliminations were done and this insn doesn't require any elimination
3278 processing (these are not identical conditions: it might be updating sp,
3279 but not referencing fp; this needs to be seen during reload_as_needed so
3280 that the offset between fp and sp can be taken into consideration), zero
3281 is returned. Otherwise, 1 is returned. */
3283 static int
3284 eliminate_regs_in_insn (insn, replace)
3285 rtx insn;
3286 int replace;
3288 int icode = recog_memoized (insn);
3289 rtx old_body = PATTERN (insn);
3290 int insn_is_asm = asm_noperands (old_body) >= 0;
3291 rtx old_set = single_set (insn);
3292 rtx new_body;
3293 int val = 0;
3294 int i, any_changes;
3295 rtx substed_operand[MAX_RECOG_OPERANDS];
3296 rtx orig_operand[MAX_RECOG_OPERANDS];
3297 struct elim_table *ep;
3299 if (! insn_is_asm && icode < 0)
3301 if (GET_CODE (PATTERN (insn)) == USE
3302 || GET_CODE (PATTERN (insn)) == CLOBBER
3303 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3304 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3305 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
3306 return 0;
3307 abort ();
3310 if (! replace)
3311 push_obstacks (&reload_obstack, &reload_obstack);
3313 if (old_set != 0 && GET_CODE (SET_DEST (old_set)) == REG
3314 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3316 /* Check for setting an eliminable register. */
3317 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3318 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3320 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3321 /* If this is setting the frame pointer register to the
3322 hardware frame pointer register and this is an elimination
3323 that will be done (tested above), this insn is really
3324 adjusting the frame pointer downward to compensate for
3325 the adjustment done before a nonlocal goto. */
3326 if (ep->from == FRAME_POINTER_REGNUM
3327 && ep->to == HARD_FRAME_POINTER_REGNUM)
3329 rtx src = SET_SRC (old_set);
3330 int offset = 0, ok = 0;
3331 rtx prev_insn, prev_set;
3333 if (src == ep->to_rtx)
3334 offset = 0, ok = 1;
3335 else if (GET_CODE (src) == PLUS
3336 && GET_CODE (XEXP (src, 0)) == CONST_INT
3337 && XEXP (src, 1) == ep->to_rtx)
3338 offset = INTVAL (XEXP (src, 0)), ok = 1;
3339 else if (GET_CODE (src) == PLUS
3340 && GET_CODE (XEXP (src, 1)) == CONST_INT
3341 && XEXP (src, 0) == ep->to_rtx)
3342 offset = INTVAL (XEXP (src, 1)), ok = 1;
3343 else if ((prev_insn = prev_nonnote_insn (insn)) != 0
3344 && (prev_set = single_set (prev_insn)) != 0
3345 && rtx_equal_p (SET_DEST (prev_set), src))
3347 src = SET_SRC (prev_set);
3348 if (src == ep->to_rtx)
3349 offset = 0, ok = 1;
3350 else if (GET_CODE (src) == PLUS
3351 && GET_CODE (XEXP (src, 0)) == CONST_INT
3352 && XEXP (src, 1) == ep->to_rtx)
3353 offset = INTVAL (XEXP (src, 0)), ok = 1;
3354 else if (GET_CODE (src) == PLUS
3355 && GET_CODE (XEXP (src, 1)) == CONST_INT
3356 && XEXP (src, 0) == ep->to_rtx)
3357 offset = INTVAL (XEXP (src, 1)), ok = 1;
3360 if (ok)
3362 if (replace)
3364 rtx src
3365 = plus_constant (ep->to_rtx, offset - ep->offset);
3367 /* First see if this insn remains valid when we
3368 make the change. If not, keep the INSN_CODE
3369 the same and let reload fit it up. */
3370 validate_change (insn, &SET_SRC (old_set), src, 1);
3371 validate_change (insn, &SET_DEST (old_set),
3372 ep->to_rtx, 1);
3373 if (! apply_change_group ())
3375 SET_SRC (old_set) = src;
3376 SET_DEST (old_set) = ep->to_rtx;
3380 val = 1;
3381 goto done;
3384 #endif
3386 /* In this case this insn isn't serving a useful purpose. We
3387 will delete it in reload_as_needed once we know that this
3388 elimination is, in fact, being done.
3390 If REPLACE isn't set, we can't delete this insn, but needn't
3391 process it since it won't be used unless something changes. */
3392 if (replace)
3394 delete_dead_insn (insn);
3395 return 1;
3397 val = 1;
3398 goto done;
3401 /* Check for (set (reg) (plus (reg from) (offset))) where the offset
3402 in the insn is the negative of the offset in FROM. Substitute
3403 (set (reg) (reg to)) for the insn and change its code.
3405 We have to do this here, rather than in eliminate_regs, so that we can
3406 change the insn code. */
3408 if (GET_CODE (SET_SRC (old_set)) == PLUS
3409 && GET_CODE (XEXP (SET_SRC (old_set), 0)) == REG
3410 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT)
3411 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3412 ep++)
3413 if (ep->from_rtx == XEXP (SET_SRC (old_set), 0)
3414 && ep->can_eliminate)
3416 /* We must stop at the first elimination that will be used.
3417 If this one would replace the PLUS with a REG, do it
3418 now. Otherwise, quit the loop and let eliminate_regs
3419 do its normal replacement. */
3420 if (ep->offset == - INTVAL (XEXP (SET_SRC (old_set), 1)))
3422 /* We assume here that we don't need a PARALLEL of
3423 any CLOBBERs for this assignment. There's not
3424 much we can do if we do need it. */
3425 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3426 SET_DEST (old_set),
3427 ep->to_rtx);
3428 INSN_CODE (insn) = -1;
3429 val = 1;
3430 goto done;
3433 break;
3437 /* Determine the effects of this insn on elimination offsets. */
3438 elimination_effects (old_body, 0);
3440 /* Eliminate all eliminable registers occurring in operands that
3441 can be handled by reload. */
3442 extract_insn (insn);
3443 any_changes = 0;
3444 for (i = 0; i < recog_data.n_operands; i++)
3446 orig_operand[i] = recog_data.operand[i];
3447 substed_operand[i] = recog_data.operand[i];
3449 /* For an asm statement, every operand is eliminable. */
3450 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3452 /* Check for setting a register that we know about. */
3453 if (recog_data.operand_type[i] != OP_IN
3454 && GET_CODE (orig_operand[i]) == REG)
3456 /* If we are assigning to a register that can be eliminated, it
3457 must be as part of a PARALLEL, since the code above handles
3458 single SETs. We must indicate that we can no longer
3459 eliminate this reg. */
3460 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3461 ep++)
3462 if (ep->from_rtx == orig_operand[i] && ep->can_eliminate)
3463 ep->can_eliminate = 0;
3466 substed_operand[i] = eliminate_regs (recog_data.operand[i], 0,
3467 replace ? insn : NULL_RTX);
3468 if (substed_operand[i] != orig_operand[i])
3469 val = any_changes = 1;
3470 /* Terminate the search in check_eliminable_occurrences at
3471 this point. */
3472 *recog_data.operand_loc[i] = 0;
3474 /* If an output operand changed from a REG to a MEM and INSN is an
3475 insn, write a CLOBBER insn. */
3476 if (recog_data.operand_type[i] != OP_IN
3477 && GET_CODE (orig_operand[i]) == REG
3478 && GET_CODE (substed_operand[i]) == MEM
3479 && replace)
3480 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3481 insn);
3485 for (i = 0; i < recog_data.n_dups; i++)
3486 *recog_data.dup_loc[i]
3487 = *recog_data.operand_loc[(int)recog_data.dup_num[i]];
3489 /* If any eliminable remain, they aren't eliminable anymore. */
3490 check_eliminable_occurrences (old_body);
3492 /* Substitute the operands; the new values are in the substed_operand
3493 array. */
3494 for (i = 0; i < recog_data.n_operands; i++)
3495 *recog_data.operand_loc[i] = substed_operand[i];
3496 for (i = 0; i < recog_data.n_dups; i++)
3497 *recog_data.dup_loc[i] = substed_operand[(int)recog_data.dup_num[i]];
3499 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3500 re-recognize the insn. We do this in case we had a simple addition
3501 but now can do this as a load-address. This saves an insn in this
3502 common case.
3503 If re-recognition fails, the old insn code number will still be used,
3504 and some register operands may have changed into PLUS expressions.
3505 These will be handled by find_reloads by loading them into a register
3506 again.*/
3508 if (val)
3510 /* If we aren't replacing things permanently and we changed something,
3511 make another copy to ensure that all the RTL is new. Otherwise
3512 things can go wrong if find_reload swaps commutative operands
3513 and one is inside RTL that has been copied while the other is not. */
3514 new_body = old_body;
3515 if (! replace)
3516 new_body = copy_insn (old_body);
3517 PATTERN (insn) = new_body;
3519 /* If we had a move insn but now we don't, rerecognize it. This will
3520 cause spurious re-recognition if the old move had a PARALLEL since
3521 the new one still will, but we can't call single_set without
3522 having put NEW_BODY into the insn and the re-recognition won't
3523 hurt in this rare case. */
3524 /* ??? Why this huge if statement - why don't we just rerecognize the
3525 thing always? */
3526 if (! insn_is_asm
3527 && old_set != 0
3528 && ((GET_CODE (SET_SRC (old_set)) == REG
3529 && (GET_CODE (new_body) != SET
3530 || GET_CODE (SET_SRC (new_body)) != REG))
3531 /* If this was a load from or store to memory, compare
3532 the MEM in recog_data.operand to the one in the insn.
3533 If they are not equal, then rerecognize the insn. */
3534 || (old_set != 0
3535 && ((GET_CODE (SET_SRC (old_set)) == MEM
3536 && SET_SRC (old_set) != recog_data.operand[1])
3537 || (GET_CODE (SET_DEST (old_set)) == MEM
3538 && SET_DEST (old_set) != recog_data.operand[0])))
3539 /* If this was an add insn before, rerecognize. */
3540 || GET_CODE (SET_SRC (old_set)) == PLUS))
3542 int new_icode = recog (PATTERN (insn), insn, 0);
3543 if (new_icode < 0)
3544 INSN_CODE (insn) = icode;
3548 /* Restore the old body. If there were any changes to it, we made a copy
3549 of it while the changes were still in place, so we'll correctly return
3550 a modified insn below. */
3551 if (! replace)
3553 /* Restore the old body. */
3554 for (i = 0; i < recog_data.n_operands; i++)
3555 *recog_data.operand_loc[i] = orig_operand[i];
3556 for (i = 0; i < recog_data.n_dups; i++)
3557 *recog_data.dup_loc[i] = orig_operand[(int)recog_data.dup_num[i]];
3560 /* Update all elimination pairs to reflect the status after the current
3561 insn. The changes we make were determined by the earlier call to
3562 elimination_effects.
3564 We also detect a cases where register elimination cannot be done,
3565 namely, if a register would be both changed and referenced outside a MEM
3566 in the resulting insn since such an insn is often undefined and, even if
3567 not, we cannot know what meaning will be given to it. Note that it is
3568 valid to have a register used in an address in an insn that changes it
3569 (presumably with a pre- or post-increment or decrement).
3571 If anything changes, return nonzero. */
3573 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3575 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3576 ep->can_eliminate = 0;
3578 ep->ref_outside_mem = 0;
3580 if (ep->previous_offset != ep->offset)
3581 val = 1;
3584 done:
3585 /* If we changed something, perform elimination in REG_NOTES. This is
3586 needed even when REPLACE is zero because a REG_DEAD note might refer
3587 to a register that we eliminate and could cause a different number
3588 of spill registers to be needed in the final reload pass than in
3589 the pre-passes. */
3590 if (val && REG_NOTES (insn) != 0)
3591 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
3593 if (! replace)
3594 pop_obstacks ();
3596 return val;
3599 /* Loop through all elimination pairs.
3600 Recalculate the number not at initial offset.
3602 Compute the maximum offset (minimum offset if the stack does not
3603 grow downward) for each elimination pair. */
3605 static void
3606 update_eliminable_offsets ()
3608 struct elim_table *ep;
3610 num_not_at_initial_offset = 0;
3611 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3613 ep->previous_offset = ep->offset;
3614 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3615 num_not_at_initial_offset++;
3619 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3620 replacement we currently believe is valid, mark it as not eliminable if X
3621 modifies DEST in any way other than by adding a constant integer to it.
3623 If DEST is the frame pointer, we do nothing because we assume that
3624 all assignments to the hard frame pointer are nonlocal gotos and are being
3625 done at a time when they are valid and do not disturb anything else.
3626 Some machines want to eliminate a fake argument pointer with either the
3627 frame or stack pointer. Assignments to the hard frame pointer must not
3628 prevent this elimination.
3630 Called via note_stores from reload before starting its passes to scan
3631 the insns of the function. */
3633 static void
3634 mark_not_eliminable (dest, x)
3635 rtx dest;
3636 rtx x;
3638 register unsigned int i;
3640 /* A SUBREG of a hard register here is just changing its mode. We should
3641 not see a SUBREG of an eliminable hard register, but check just in
3642 case. */
3643 if (GET_CODE (dest) == SUBREG)
3644 dest = SUBREG_REG (dest);
3646 if (dest == hard_frame_pointer_rtx)
3647 return;
3649 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3650 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3651 && (GET_CODE (x) != SET
3652 || GET_CODE (SET_SRC (x)) != PLUS
3653 || XEXP (SET_SRC (x), 0) != dest
3654 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3656 reg_eliminate[i].can_eliminate_previous
3657 = reg_eliminate[i].can_eliminate = 0;
3658 num_eliminable--;
3662 /* Verify that the initial elimination offsets did not change since the
3663 last call to set_initial_elim_offsets. This is used to catch cases
3664 where something illegal happened during reload_as_needed that could
3665 cause incorrect code to be generated if we did not check for it. */
3666 static void
3667 verify_initial_elim_offsets ()
3669 int t;
3671 #ifdef ELIMINABLE_REGS
3672 struct elim_table *ep;
3674 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3676 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3677 if (t != ep->initial_offset)
3678 abort ();
3680 #else
3681 INITIAL_FRAME_POINTER_OFFSET (t);
3682 if (t != reg_eliminate[0].initial_offset)
3683 abort ();
3684 #endif
3687 /* Reset all offsets on eliminable registers to their initial values. */
3688 static void
3689 set_initial_elim_offsets ()
3691 struct elim_table *ep = reg_eliminate;
3693 #ifdef ELIMINABLE_REGS
3694 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3696 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3697 ep->previous_offset = ep->offset = ep->initial_offset;
3699 #else
3700 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3701 ep->previous_offset = ep->offset = ep->initial_offset;
3702 #endif
3704 num_not_at_initial_offset = 0;
3707 /* Initialize the known label offsets.
3708 Set a known offset for each forced label to be at the initial offset
3709 of each elimination. We do this because we assume that all
3710 computed jumps occur from a location where each elimination is
3711 at its initial offset.
3712 For all other labels, show that we don't know the offsets. */
3714 static void
3715 set_initial_label_offsets ()
3717 rtx x;
3718 bzero ((char *) &offsets_known_at[get_first_label_num ()], num_labels);
3720 for (x = forced_labels; x; x = XEXP (x, 1))
3721 if (XEXP (x, 0))
3722 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3725 /* Set all elimination offsets to the known values for the code label given
3726 by INSN. */
3727 static void
3728 set_offsets_for_label (insn)
3729 rtx insn;
3731 unsigned int i;
3732 int label_nr = CODE_LABEL_NUMBER (insn);
3733 struct elim_table *ep;
3735 num_not_at_initial_offset = 0;
3736 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3738 ep->offset = ep->previous_offset = offsets_at[label_nr][i];
3739 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3740 num_not_at_initial_offset++;
3744 /* See if anything that happened changes which eliminations are valid.
3745 For example, on the Sparc, whether or not the frame pointer can
3746 be eliminated can depend on what registers have been used. We need
3747 not check some conditions again (such as flag_omit_frame_pointer)
3748 since they can't have changed. */
3750 static void
3751 update_eliminables (pset)
3752 HARD_REG_SET *pset;
3754 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3755 int previous_frame_pointer_needed = frame_pointer_needed;
3756 #endif
3757 struct elim_table *ep;
3759 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3760 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3761 #ifdef ELIMINABLE_REGS
3762 || ! CAN_ELIMINATE (ep->from, ep->to)
3763 #endif
3765 ep->can_eliminate = 0;
3767 /* Look for the case where we have discovered that we can't replace
3768 register A with register B and that means that we will now be
3769 trying to replace register A with register C. This means we can
3770 no longer replace register C with register B and we need to disable
3771 such an elimination, if it exists. This occurs often with A == ap,
3772 B == sp, and C == fp. */
3774 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3776 struct elim_table *op;
3777 register int new_to = -1;
3779 if (! ep->can_eliminate && ep->can_eliminate_previous)
3781 /* Find the current elimination for ep->from, if there is a
3782 new one. */
3783 for (op = reg_eliminate;
3784 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3785 if (op->from == ep->from && op->can_eliminate)
3787 new_to = op->to;
3788 break;
3791 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3792 disable it. */
3793 for (op = reg_eliminate;
3794 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3795 if (op->from == new_to && op->to == ep->to)
3796 op->can_eliminate = 0;
3800 /* See if any registers that we thought we could eliminate the previous
3801 time are no longer eliminable. If so, something has changed and we
3802 must spill the register. Also, recompute the number of eliminable
3803 registers and see if the frame pointer is needed; it is if there is
3804 no elimination of the frame pointer that we can perform. */
3806 frame_pointer_needed = 1;
3807 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3809 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3810 && ep->to != HARD_FRAME_POINTER_REGNUM)
3811 frame_pointer_needed = 0;
3813 if (! ep->can_eliminate && ep->can_eliminate_previous)
3815 ep->can_eliminate_previous = 0;
3816 SET_HARD_REG_BIT (*pset, ep->from);
3817 num_eliminable--;
3821 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3822 /* If we didn't need a frame pointer last time, but we do now, spill
3823 the hard frame pointer. */
3824 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3825 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3826 #endif
3829 /* Initialize the table of registers to eliminate. */
3830 static void
3831 init_elim_table ()
3833 struct elim_table *ep;
3834 #ifdef ELIMINABLE_REGS
3835 struct elim_table_1 *ep1;
3836 #endif
3838 if (!reg_eliminate)
3839 reg_eliminate = (struct elim_table *)
3840 xcalloc(sizeof(struct elim_table), NUM_ELIMINABLE_REGS);
3842 /* Does this function require a frame pointer? */
3844 frame_pointer_needed = (! flag_omit_frame_pointer
3845 #ifdef EXIT_IGNORE_STACK
3846 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3847 and restore sp for alloca. So we can't eliminate
3848 the frame pointer in that case. At some point,
3849 we should improve this by emitting the
3850 sp-adjusting insns for this case. */
3851 || (current_function_calls_alloca
3852 && EXIT_IGNORE_STACK)
3853 #endif
3854 || FRAME_POINTER_REQUIRED);
3856 num_eliminable = 0;
3858 #ifdef ELIMINABLE_REGS
3859 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3860 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3862 ep->from = ep1->from;
3863 ep->to = ep1->to;
3864 ep->can_eliminate = ep->can_eliminate_previous
3865 = (CAN_ELIMINATE (ep->from, ep->to)
3866 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3868 #else
3869 reg_eliminate[0].from = reg_eliminate_1[0].from;
3870 reg_eliminate[0].to = reg_eliminate_1[0].to;
3871 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3872 = ! frame_pointer_needed;
3873 #endif
3875 /* Count the number of eliminable registers and build the FROM and TO
3876 REG rtx's. Note that code in gen_rtx will cause, e.g.,
3877 gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3878 We depend on this. */
3879 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3881 num_eliminable += ep->can_eliminate;
3882 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3883 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3887 /* Kick all pseudos out of hard register REGNO.
3888 If DUMPFILE is nonzero, log actions taken on that file.
3890 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3891 because we found we can't eliminate some register. In the case, no pseudos
3892 are allowed to be in the register, even if they are only in a block that
3893 doesn't require spill registers, unlike the case when we are spilling this
3894 hard reg to produce another spill register.
3896 Return nonzero if any pseudos needed to be kicked out. */
3898 static void
3899 spill_hard_reg (regno, dumpfile, cant_eliminate)
3900 register int regno;
3901 FILE *dumpfile ATTRIBUTE_UNUSED;
3902 int cant_eliminate;
3904 register int i;
3906 if (cant_eliminate)
3908 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3909 regs_ever_live[regno] = 1;
3912 /* Spill every pseudo reg that was allocated to this reg
3913 or to something that overlaps this reg. */
3915 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3916 if (reg_renumber[i] >= 0
3917 && reg_renumber[i] <= regno
3918 && (reg_renumber[i]
3919 + HARD_REGNO_NREGS (reg_renumber[i],
3920 PSEUDO_REGNO_MODE (i))
3921 > regno))
3922 SET_REGNO_REG_SET (spilled_pseudos, i);
3925 /* I'm getting weird preprocessor errors if I use IOR_HARD_REG_SET
3926 from within EXECUTE_IF_SET_IN_REG_SET. Hence this awkwardness. */
3927 static void
3928 ior_hard_reg_set (set1, set2)
3929 HARD_REG_SET *set1, *set2;
3931 IOR_HARD_REG_SET (*set1, *set2);
3934 /* After find_reload_regs has been run for all insn that need reloads,
3935 and/or spill_hard_regs was called, this function is used to actually
3936 spill pseudo registers and try to reallocate them. It also sets up the
3937 spill_regs array for use by choose_reload_regs. */
3939 static int
3940 finish_spills (global, dumpfile)
3941 int global;
3942 FILE *dumpfile;
3944 struct insn_chain *chain;
3945 int something_changed = 0;
3946 int i;
3948 /* Build the spill_regs array for the function. */
3949 /* If there are some registers still to eliminate and one of the spill regs
3950 wasn't ever used before, additional stack space may have to be
3951 allocated to store this register. Thus, we may have changed the offset
3952 between the stack and frame pointers, so mark that something has changed.
3954 One might think that we need only set VAL to 1 if this is a call-used
3955 register. However, the set of registers that must be saved by the
3956 prologue is not identical to the call-used set. For example, the
3957 register used by the call insn for the return PC is a call-used register,
3958 but must be saved by the prologue. */
3960 n_spills = 0;
3961 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3962 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3964 spill_reg_order[i] = n_spills;
3965 spill_regs[n_spills++] = i;
3966 if (num_eliminable && ! regs_ever_live[i])
3967 something_changed = 1;
3968 regs_ever_live[i] = 1;
3970 else
3971 spill_reg_order[i] = -1;
3973 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3974 if (REGNO_REG_SET_P (spilled_pseudos, i))
3976 /* Record the current hard register the pseudo is allocated to in
3977 pseudo_previous_regs so we avoid reallocating it to the same
3978 hard reg in a later pass. */
3979 if (reg_renumber[i] < 0)
3980 abort ();
3981 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3982 /* Mark it as no longer having a hard register home. */
3983 reg_renumber[i] = -1;
3984 /* We will need to scan everything again. */
3985 something_changed = 1;
3988 /* Retry global register allocation if possible. */
3989 if (global)
3991 bzero ((char *) pseudo_forbidden_regs, max_regno * sizeof (HARD_REG_SET));
3992 /* For every insn that needs reloads, set the registers used as spill
3993 regs in pseudo_forbidden_regs for every pseudo live across the
3994 insn. */
3995 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
3997 EXECUTE_IF_SET_IN_REG_SET
3998 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
4000 ior_hard_reg_set (pseudo_forbidden_regs + i,
4001 &chain->used_spill_regs);
4003 EXECUTE_IF_SET_IN_REG_SET
4004 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
4006 ior_hard_reg_set (pseudo_forbidden_regs + i,
4007 &chain->used_spill_regs);
4011 /* Retry allocating the spilled pseudos. For each reg, merge the
4012 various reg sets that indicate which hard regs can't be used,
4013 and call retry_global_alloc.
4014 We change spill_pseudos here to only contain pseudos that did not
4015 get a new hard register. */
4016 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4017 if (reg_old_renumber[i] != reg_renumber[i])
4019 HARD_REG_SET forbidden;
4020 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
4021 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
4022 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
4023 retry_global_alloc (i, forbidden);
4024 if (reg_renumber[i] >= 0)
4025 CLEAR_REGNO_REG_SET (spilled_pseudos, i);
4029 /* Fix up the register information in the insn chain.
4030 This involves deleting those of the spilled pseudos which did not get
4031 a new hard register home from the live_{before,after} sets. */
4032 for (chain = reload_insn_chain; chain; chain = chain->next)
4034 HARD_REG_SET used_by_pseudos;
4035 HARD_REG_SET used_by_pseudos2;
4037 AND_COMPL_REG_SET (chain->live_before, spilled_pseudos);
4038 AND_COMPL_REG_SET (chain->live_after, spilled_pseudos);
4040 /* Mark any unallocated hard regs as available for spills. That
4041 makes inheritance work somewhat better. */
4042 if (chain->need_reload)
4044 REG_SET_TO_HARD_REG_SET (used_by_pseudos, chain->live_before);
4045 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, chain->live_after);
4046 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
4048 /* Save the old value for the sanity test below. */
4049 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
4051 compute_use_by_pseudos (&used_by_pseudos, chain->live_before);
4052 compute_use_by_pseudos (&used_by_pseudos, chain->live_after);
4053 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
4054 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
4056 /* Make sure we only enlarge the set. */
4057 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
4058 abort ();
4059 ok:;
4063 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
4064 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4066 int regno = reg_renumber[i];
4067 if (reg_old_renumber[i] == regno)
4068 continue;
4070 alter_reg (i, reg_old_renumber[i]);
4071 reg_old_renumber[i] = regno;
4072 if (dumpfile)
4074 if (regno == -1)
4075 fprintf (dumpfile, " Register %d now on stack.\n\n", i);
4076 else
4077 fprintf (dumpfile, " Register %d now in %d.\n\n",
4078 i, reg_renumber[i]);
4082 return something_changed;
4085 /* Find all paradoxical subregs within X and update reg_max_ref_width.
4086 Also mark any hard registers used to store user variables as
4087 forbidden from being used for spill registers. */
4089 static void
4090 scan_paradoxical_subregs (x)
4091 register rtx x;
4093 register int i;
4094 register const char *fmt;
4095 register enum rtx_code code = GET_CODE (x);
4097 switch (code)
4099 case REG:
4100 #if 0
4101 if (SMALL_REGISTER_CLASSES && REGNO (x) < FIRST_PSEUDO_REGISTER
4102 && REG_USERVAR_P (x))
4103 SET_HARD_REG_BIT (bad_spill_regs_global, REGNO (x));
4104 #endif
4105 return;
4107 case CONST_INT:
4108 case CONST:
4109 case SYMBOL_REF:
4110 case LABEL_REF:
4111 case CONST_DOUBLE:
4112 case CC0:
4113 case PC:
4114 case USE:
4115 case CLOBBER:
4116 return;
4118 case SUBREG:
4119 if (GET_CODE (SUBREG_REG (x)) == REG
4120 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
4121 reg_max_ref_width[REGNO (SUBREG_REG (x))]
4122 = GET_MODE_SIZE (GET_MODE (x));
4123 return;
4125 default:
4126 break;
4129 fmt = GET_RTX_FORMAT (code);
4130 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4132 if (fmt[i] == 'e')
4133 scan_paradoxical_subregs (XEXP (x, i));
4134 else if (fmt[i] == 'E')
4136 register int j;
4137 for (j = XVECLEN (x, i) - 1; j >=0; j--)
4138 scan_paradoxical_subregs (XVECEXP (x, i, j));
4143 static int
4144 hard_reg_use_compare (p1p, p2p)
4145 const PTR p1p;
4146 const PTR p2p;
4148 const struct hard_reg_n_uses *p1 = (const struct hard_reg_n_uses *)p1p;
4149 const struct hard_reg_n_uses *p2 = (const struct hard_reg_n_uses *)p2p;
4150 int bad1 = TEST_HARD_REG_BIT (bad_spill_regs, p1->regno);
4151 int bad2 = TEST_HARD_REG_BIT (bad_spill_regs, p2->regno);
4152 if (bad1 && bad2)
4153 return p1->regno - p2->regno;
4154 if (bad1)
4155 return 1;
4156 if (bad2)
4157 return -1;
4158 if (p1->uses > p2->uses)
4159 return 1;
4160 if (p1->uses < p2->uses)
4161 return -1;
4162 /* If regs are equally good, sort by regno,
4163 so that the results of qsort leave nothing to chance. */
4164 return p1->regno - p2->regno;
4167 /* Used for communication between order_regs_for_reload and count_pseudo.
4168 Used to avoid counting one pseudo twice. */
4169 static regset pseudos_counted;
4171 /* Update the costs in N_USES, considering that pseudo REG is live. */
4172 static void
4173 count_pseudo (n_uses, reg)
4174 struct hard_reg_n_uses *n_uses;
4175 int reg;
4177 int r = reg_renumber[reg];
4178 int nregs;
4180 if (REGNO_REG_SET_P (pseudos_counted, reg))
4181 return;
4182 SET_REGNO_REG_SET (pseudos_counted, reg);
4184 if (r < 0)
4185 abort ();
4187 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
4188 while (nregs-- > 0)
4189 n_uses[r++].uses += REG_N_REFS (reg);
4191 /* Choose the order to consider regs for use as reload registers
4192 based on how much trouble would be caused by spilling one.
4193 Store them in order of decreasing preference in potential_reload_regs. */
4195 static void
4196 order_regs_for_reload (chain)
4197 struct insn_chain *chain;
4199 register int i;
4200 register int o = 0;
4201 struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
4203 pseudos_counted = ALLOCA_REG_SET ();
4205 COPY_HARD_REG_SET (bad_spill_regs, bad_spill_regs_global);
4207 /* Count number of uses of each hard reg by pseudo regs allocated to it
4208 and then order them by decreasing use. */
4210 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4212 hard_reg_n_uses[i].regno = i;
4213 hard_reg_n_uses[i].uses = 0;
4215 /* Test the various reasons why we can't use a register for
4216 spilling in this insn. */
4217 if (fixed_regs[i]
4218 || REGNO_REG_SET_P (chain->live_before, i)
4219 || REGNO_REG_SET_P (chain->live_after, i))
4220 SET_HARD_REG_BIT (bad_spill_regs, i);
4223 /* Now compute hard_reg_n_uses. */
4224 CLEAR_REG_SET (pseudos_counted);
4226 EXECUTE_IF_SET_IN_REG_SET
4227 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
4229 count_pseudo (hard_reg_n_uses, i);
4231 EXECUTE_IF_SET_IN_REG_SET
4232 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
4234 count_pseudo (hard_reg_n_uses, i);
4237 FREE_REG_SET (pseudos_counted);
4239 /* Prefer registers not so far used, for use in temporary loading.
4240 Among them, if REG_ALLOC_ORDER is defined, use that order.
4241 Otherwise, prefer registers not preserved by calls. */
4243 #ifdef REG_ALLOC_ORDER
4244 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4246 int regno = reg_alloc_order[i];
4248 if (hard_reg_n_uses[regno].uses == 0
4249 && ! TEST_HARD_REG_BIT (bad_spill_regs, regno))
4250 potential_reload_regs[o++] = regno;
4252 #else
4253 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4255 if (hard_reg_n_uses[i].uses == 0 && call_used_regs[i]
4256 && ! TEST_HARD_REG_BIT (bad_spill_regs, i))
4257 potential_reload_regs[o++] = i;
4259 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4261 if (hard_reg_n_uses[i].uses == 0 && ! call_used_regs[i]
4262 && ! TEST_HARD_REG_BIT (bad_spill_regs, i))
4263 potential_reload_regs[o++] = i;
4265 #endif
4267 qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
4268 sizeof hard_reg_n_uses[0], hard_reg_use_compare);
4270 /* Now add the regs that are already used,
4271 preferring those used less often. The fixed and otherwise forbidden
4272 registers will be at the end of this list. */
4274 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4275 if (hard_reg_n_uses[i].uses != 0
4276 && ! TEST_HARD_REG_BIT (bad_spill_regs, hard_reg_n_uses[i].regno))
4277 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
4278 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4279 if (TEST_HARD_REG_BIT (bad_spill_regs, hard_reg_n_uses[i].regno))
4280 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
4283 /* Reload pseudo-registers into hard regs around each insn as needed.
4284 Additional register load insns are output before the insn that needs it
4285 and perhaps store insns after insns that modify the reloaded pseudo reg.
4287 reg_last_reload_reg and reg_reloaded_contents keep track of
4288 which registers are already available in reload registers.
4289 We update these for the reloads that we perform,
4290 as the insns are scanned. */
4292 static void
4293 reload_as_needed (live_known)
4294 int live_known;
4296 struct insn_chain *chain;
4297 #if defined (AUTO_INC_DEC) || defined (INSN_CLOBBERS_REGNO_P)
4298 register int i;
4299 #endif
4300 rtx x;
4302 bzero ((char *) spill_reg_rtx, sizeof spill_reg_rtx);
4303 bzero ((char *) spill_reg_store, sizeof spill_reg_store);
4304 reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
4305 bzero ((char *) reg_last_reload_reg, max_regno * sizeof (rtx));
4306 reg_has_output_reload = (char *) alloca (max_regno);
4307 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4309 set_initial_elim_offsets ();
4311 for (chain = reload_insn_chain; chain; chain = chain->next)
4313 rtx prev;
4314 rtx insn = chain->insn;
4315 rtx old_next = NEXT_INSN (insn);
4317 /* If we pass a label, copy the offsets from the label information
4318 into the current offsets of each elimination. */
4319 if (GET_CODE (insn) == CODE_LABEL)
4320 set_offsets_for_label (insn);
4322 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4324 rtx oldpat = PATTERN (insn);
4326 /* If this is a USE and CLOBBER of a MEM, ensure that any
4327 references to eliminable registers have been removed. */
4329 if ((GET_CODE (PATTERN (insn)) == USE
4330 || GET_CODE (PATTERN (insn)) == CLOBBER)
4331 && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
4332 XEXP (XEXP (PATTERN (insn), 0), 0)
4333 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
4334 GET_MODE (XEXP (PATTERN (insn), 0)),
4335 NULL_RTX);
4337 /* If we need to do register elimination processing, do so.
4338 This might delete the insn, in which case we are done. */
4339 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
4341 eliminate_regs_in_insn (insn, 1);
4342 if (GET_CODE (insn) == NOTE)
4344 update_eliminable_offsets ();
4345 continue;
4349 /* If need_elim is nonzero but need_reload is zero, one might think
4350 that we could simply set n_reloads to 0. However, find_reloads
4351 could have done some manipulation of the insn (such as swapping
4352 commutative operands), and these manipulations are lost during
4353 the first pass for every insn that needs register elimination.
4354 So the actions of find_reloads must be redone here. */
4356 if (! chain->need_elim && ! chain->need_reload
4357 && ! chain->need_operand_change)
4358 n_reloads = 0;
4359 /* First find the pseudo regs that must be reloaded for this insn.
4360 This info is returned in the tables reload_... (see reload.h).
4361 Also modify the body of INSN by substituting RELOAD
4362 rtx's for those pseudo regs. */
4363 else
4365 bzero (reg_has_output_reload, max_regno);
4366 CLEAR_HARD_REG_SET (reg_is_output_reload);
4368 find_reloads (insn, 1, spill_indirect_levels, live_known,
4369 spill_reg_order);
4372 if (num_eliminable && chain->need_elim)
4373 update_eliminable_offsets ();
4375 if (n_reloads > 0)
4377 rtx next = NEXT_INSN (insn);
4378 rtx p;
4380 prev = PREV_INSN (insn);
4382 /* Now compute which reload regs to reload them into. Perhaps
4383 reusing reload regs from previous insns, or else output
4384 load insns to reload them. Maybe output store insns too.
4385 Record the choices of reload reg in reload_reg_rtx. */
4386 choose_reload_regs (chain);
4388 /* Merge any reloads that we didn't combine for fear of
4389 increasing the number of spill registers needed but now
4390 discover can be safely merged. */
4391 if (SMALL_REGISTER_CLASSES)
4392 merge_assigned_reloads (insn);
4394 /* Generate the insns to reload operands into or out of
4395 their reload regs. */
4396 emit_reload_insns (chain);
4398 /* Substitute the chosen reload regs from reload_reg_rtx
4399 into the insn's body (or perhaps into the bodies of other
4400 load and store insn that we just made for reloading
4401 and that we moved the structure into). */
4402 subst_reloads ();
4404 /* If this was an ASM, make sure that all the reload insns
4405 we have generated are valid. If not, give an error
4406 and delete them. */
4408 if (asm_noperands (PATTERN (insn)) >= 0)
4409 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4410 if (p != insn && GET_RTX_CLASS (GET_CODE (p)) == 'i'
4411 && (recog_memoized (p) < 0
4412 || (extract_insn (p), ! constrain_operands (1))))
4414 error_for_asm (insn,
4415 "`asm' operand requires impossible reload");
4416 PUT_CODE (p, NOTE);
4417 NOTE_SOURCE_FILE (p) = 0;
4418 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
4421 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4422 is no longer validly lying around to save a future reload.
4423 Note that this does not detect pseudos that were reloaded
4424 for this insn in order to be stored in
4425 (obeying register constraints). That is correct; such reload
4426 registers ARE still valid. */
4427 note_stores (oldpat, forget_old_reloads_1);
4429 /* There may have been CLOBBER insns placed after INSN. So scan
4430 between INSN and NEXT and use them to forget old reloads. */
4431 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
4432 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
4433 note_stores (PATTERN (x), forget_old_reloads_1);
4435 #ifdef AUTO_INC_DEC
4436 /* Likewise for regs altered by auto-increment in this insn.
4437 REG_INC notes have been changed by reloading:
4438 find_reloads_address_1 records substitutions for them,
4439 which have been performed by subst_reloads above. */
4440 for (i = n_reloads - 1; i >= 0; i--)
4442 rtx in_reg = rld[i].in_reg;
4443 if (in_reg)
4445 enum rtx_code code = GET_CODE (in_reg);
4446 /* PRE_INC / PRE_DEC will have the reload register ending up
4447 with the same value as the stack slot, but that doesn't
4448 hold true for POST_INC / POST_DEC. Either we have to
4449 convert the memory access to a true POST_INC / POST_DEC,
4450 or we can't use the reload register for inheritance. */
4451 if ((code == POST_INC || code == POST_DEC)
4452 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4453 REGNO (rld[i].reg_rtx))
4454 /* Make sure it is the inc/dec pseudo, and not
4455 some other (e.g. output operand) pseudo. */
4456 && (reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4457 == REGNO (XEXP (in_reg, 0))))
4460 rtx reload_reg = rld[i].reg_rtx;
4461 enum machine_mode mode = GET_MODE (reload_reg);
4462 int n = 0;
4463 rtx p;
4465 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4467 /* We really want to ignore REG_INC notes here, so
4468 use PATTERN (p) as argument to reg_set_p . */
4469 if (reg_set_p (reload_reg, PATTERN (p)))
4470 break;
4471 n = count_occurrences (PATTERN (p), reload_reg);
4472 if (! n)
4473 continue;
4474 if (n == 1)
4476 n = validate_replace_rtx (reload_reg,
4477 gen_rtx (code, mode,
4478 reload_reg),
4481 /* We must also verify that the constraints
4482 are met after the replacement. */
4483 extract_insn (p);
4484 if (n)
4485 n = constrain_operands (1);
4486 else
4487 break;
4489 /* If the constraints were not met, then
4490 undo the replacement. */
4491 if (!n)
4493 validate_replace_rtx (gen_rtx (code, mode,
4494 reload_reg),
4495 reload_reg, p);
4496 break;
4500 break;
4502 if (n == 1)
4504 REG_NOTES (p)
4505 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
4506 REG_NOTES (p));
4507 /* Mark this as having an output reload so that the
4508 REG_INC processing code below won't invalidate
4509 the reload for inheritance. */
4510 SET_HARD_REG_BIT (reg_is_output_reload,
4511 REGNO (reload_reg));
4512 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4514 else
4515 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX);
4517 else if ((code == PRE_INC || code == PRE_DEC)
4518 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4519 REGNO (rld[i].reg_rtx))
4520 /* Make sure it is the inc/dec pseudo, and not
4521 some other (e.g. output operand) pseudo. */
4522 && (reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4523 == REGNO (XEXP (in_reg, 0))))
4525 SET_HARD_REG_BIT (reg_is_output_reload,
4526 REGNO (rld[i].reg_rtx));
4527 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4531 /* If a pseudo that got a hard register is auto-incremented,
4532 we must purge records of copying it into pseudos without
4533 hard registers. */
4534 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4535 if (REG_NOTE_KIND (x) == REG_INC)
4537 /* See if this pseudo reg was reloaded in this insn.
4538 If so, its last-reload info is still valid
4539 because it is based on this insn's reload. */
4540 for (i = 0; i < n_reloads; i++)
4541 if (rld[i].out == XEXP (x, 0))
4542 break;
4544 if (i == n_reloads)
4545 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX);
4547 #endif
4549 /* A reload reg's contents are unknown after a label. */
4550 if (GET_CODE (insn) == CODE_LABEL)
4551 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4553 /* Don't assume a reload reg is still good after a call insn
4554 if it is a call-used reg. */
4555 else if (GET_CODE (insn) == CALL_INSN)
4556 AND_COMPL_HARD_REG_SET(reg_reloaded_valid, call_used_reg_set);
4558 /* In case registers overlap, allow certain insns to invalidate
4559 particular hard registers. */
4561 #ifdef INSN_CLOBBERS_REGNO_P
4562 for (i = 0 ; i < FIRST_PSEUDO_REGISTER; i++)
4563 if (TEST_HARD_REG_BIT (reg_reloaded_valid, i)
4564 && INSN_CLOBBERS_REGNO_P (insn, i))
4565 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i);
4566 #endif
4568 #ifdef USE_C_ALLOCA
4569 alloca (0);
4570 #endif
4574 /* Discard all record of any value reloaded from X,
4575 or reloaded in X from someplace else;
4576 unless X is an output reload reg of the current insn.
4578 X may be a hard reg (the reload reg)
4579 or it may be a pseudo reg that was reloaded from. */
4581 static void
4582 forget_old_reloads_1 (x, ignored)
4583 rtx x;
4584 rtx ignored ATTRIBUTE_UNUSED;
4586 register int regno;
4587 int nr;
4588 int offset = 0;
4590 /* note_stores does give us subregs of hard regs. */
4591 while (GET_CODE (x) == SUBREG)
4593 offset += SUBREG_WORD (x);
4594 x = SUBREG_REG (x);
4597 if (GET_CODE (x) != REG)
4598 return;
4600 regno = REGNO (x) + offset;
4602 if (regno >= FIRST_PSEUDO_REGISTER)
4603 nr = 1;
4604 else
4606 int i;
4607 nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
4608 /* Storing into a spilled-reg invalidates its contents.
4609 This can happen if a block-local pseudo is allocated to that reg
4610 and it wasn't spilled because this block's total need is 0.
4611 Then some insn might have an optional reload and use this reg. */
4612 for (i = 0; i < nr; i++)
4613 /* But don't do this if the reg actually serves as an output
4614 reload reg in the current instruction. */
4615 if (n_reloads == 0
4616 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4617 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4620 /* Since value of X has changed,
4621 forget any value previously copied from it. */
4623 while (nr-- > 0)
4624 /* But don't forget a copy if this is the output reload
4625 that establishes the copy's validity. */
4626 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4627 reg_last_reload_reg[regno + nr] = 0;
4630 /* Comparison function for qsort to decide which of two reloads
4631 should be handled first. *P1 and *P2 are the reload numbers. */
4633 static int
4634 reload_reg_class_lower (r1p, r2p)
4635 const PTR r1p;
4636 const PTR r2p;
4638 register int r1 = *(const short *)r1p, r2 = *(const short *)r2p;
4639 register int t;
4641 /* Consider required reloads before optional ones. */
4642 t = rld[r1].optional - rld[r2].optional;
4643 if (t != 0)
4644 return t;
4646 /* Count all solitary classes before non-solitary ones. */
4647 t = ((reg_class_size[(int) rld[r2].class] == 1)
4648 - (reg_class_size[(int) rld[r1].class] == 1));
4649 if (t != 0)
4650 return t;
4652 /* Aside from solitaires, consider all multi-reg groups first. */
4653 t = rld[r2].nregs - rld[r1].nregs;
4654 if (t != 0)
4655 return t;
4657 /* Consider reloads in order of increasing reg-class number. */
4658 t = (int) rld[r1].class - (int) rld[r2].class;
4659 if (t != 0)
4660 return t;
4662 /* If reloads are equally urgent, sort by reload number,
4663 so that the results of qsort leave nothing to chance. */
4664 return r1 - r2;
4667 /* The following HARD_REG_SETs indicate when each hard register is
4668 used for a reload of various parts of the current insn. */
4670 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4671 static HARD_REG_SET reload_reg_used;
4672 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4673 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4674 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4675 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4676 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4677 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4678 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4679 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4680 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4681 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4682 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4683 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4684 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4685 static HARD_REG_SET reload_reg_used_in_op_addr;
4686 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4687 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4688 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4689 static HARD_REG_SET reload_reg_used_in_insn;
4690 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4691 static HARD_REG_SET reload_reg_used_in_other_addr;
4693 /* If reg is in use as a reload reg for any sort of reload. */
4694 static HARD_REG_SET reload_reg_used_at_all;
4696 /* If reg is use as an inherited reload. We just mark the first register
4697 in the group. */
4698 static HARD_REG_SET reload_reg_used_for_inherit;
4700 /* Records which hard regs are used in any way, either as explicit use or
4701 by being allocated to a pseudo during any point of the current insn. */
4702 static HARD_REG_SET reg_used_in_insn;
4704 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4705 TYPE. MODE is used to indicate how many consecutive regs are
4706 actually used. */
4708 static void
4709 mark_reload_reg_in_use (regno, opnum, type, mode)
4710 int regno;
4711 int opnum;
4712 enum reload_type type;
4713 enum machine_mode mode;
4715 int nregs = HARD_REGNO_NREGS (regno, mode);
4716 int i;
4718 for (i = regno; i < nregs + regno; i++)
4720 switch (type)
4722 case RELOAD_OTHER:
4723 SET_HARD_REG_BIT (reload_reg_used, i);
4724 break;
4726 case RELOAD_FOR_INPUT_ADDRESS:
4727 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4728 break;
4730 case RELOAD_FOR_INPADDR_ADDRESS:
4731 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4732 break;
4734 case RELOAD_FOR_OUTPUT_ADDRESS:
4735 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4736 break;
4738 case RELOAD_FOR_OUTADDR_ADDRESS:
4739 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4740 break;
4742 case RELOAD_FOR_OPERAND_ADDRESS:
4743 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4744 break;
4746 case RELOAD_FOR_OPADDR_ADDR:
4747 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4748 break;
4750 case RELOAD_FOR_OTHER_ADDRESS:
4751 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4752 break;
4754 case RELOAD_FOR_INPUT:
4755 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4756 break;
4758 case RELOAD_FOR_OUTPUT:
4759 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4760 break;
4762 case RELOAD_FOR_INSN:
4763 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4764 break;
4767 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4771 /* Similarly, but show REGNO is no longer in use for a reload. */
4773 static void
4774 clear_reload_reg_in_use (regno, opnum, type, mode)
4775 int regno;
4776 int opnum;
4777 enum reload_type type;
4778 enum machine_mode mode;
4780 int nregs = HARD_REGNO_NREGS (regno, mode);
4781 int start_regno, end_regno;
4782 int i;
4783 /* A complication is that for some reload types, inheritance might
4784 allow multiple reloads of the same types to share a reload register.
4785 We set check_opnum if we have to check only reloads with the same
4786 operand number, and check_any if we have to check all reloads. */
4787 int check_opnum = 0;
4788 int check_any = 0;
4789 HARD_REG_SET *used_in_set;
4791 switch (type)
4793 case RELOAD_OTHER:
4794 used_in_set = &reload_reg_used;
4795 break;
4797 case RELOAD_FOR_INPUT_ADDRESS:
4798 used_in_set = &reload_reg_used_in_input_addr[opnum];
4799 break;
4801 case RELOAD_FOR_INPADDR_ADDRESS:
4802 check_opnum = 1;
4803 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4804 break;
4806 case RELOAD_FOR_OUTPUT_ADDRESS:
4807 used_in_set = &reload_reg_used_in_output_addr[opnum];
4808 break;
4810 case RELOAD_FOR_OUTADDR_ADDRESS:
4811 check_opnum = 1;
4812 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4813 break;
4815 case RELOAD_FOR_OPERAND_ADDRESS:
4816 used_in_set = &reload_reg_used_in_op_addr;
4817 break;
4819 case RELOAD_FOR_OPADDR_ADDR:
4820 check_any = 1;
4821 used_in_set = &reload_reg_used_in_op_addr_reload;
4822 break;
4824 case RELOAD_FOR_OTHER_ADDRESS:
4825 used_in_set = &reload_reg_used_in_other_addr;
4826 check_any = 1;
4827 break;
4829 case RELOAD_FOR_INPUT:
4830 used_in_set = &reload_reg_used_in_input[opnum];
4831 break;
4833 case RELOAD_FOR_OUTPUT:
4834 used_in_set = &reload_reg_used_in_output[opnum];
4835 break;
4837 case RELOAD_FOR_INSN:
4838 used_in_set = &reload_reg_used_in_insn;
4839 break;
4840 default:
4841 abort ();
4843 /* We resolve conflicts with remaining reloads of the same type by
4844 excluding the intervals of of reload registers by them from the
4845 interval of freed reload registers. Since we only keep track of
4846 one set of interval bounds, we might have to exclude somewhat
4847 more then what would be necessary if we used a HARD_REG_SET here.
4848 But this should only happen very infrequently, so there should
4849 be no reason to worry about it. */
4851 start_regno = regno;
4852 end_regno = regno + nregs;
4853 if (check_opnum || check_any)
4855 for (i = n_reloads - 1; i >= 0; i--)
4857 if (rld[i].when_needed == type
4858 && (check_any || rld[i].opnum == opnum)
4859 && rld[i].reg_rtx)
4861 int conflict_start = true_regnum (rld[i].reg_rtx);
4862 int conflict_end
4863 = (conflict_start
4864 + HARD_REGNO_NREGS (conflict_start, rld[i].mode));
4866 /* If there is an overlap with the first to-be-freed register,
4867 adjust the interval start. */
4868 if (conflict_start <= start_regno && conflict_end > start_regno)
4869 start_regno = conflict_end;
4870 /* Otherwise, if there is a conflict with one of the other
4871 to-be-freed registers, adjust the interval end. */
4872 if (conflict_start > start_regno && conflict_start < end_regno)
4873 end_regno = conflict_start;
4877 for (i = start_regno; i < end_regno; i++)
4878 CLEAR_HARD_REG_BIT (*used_in_set, i);
4881 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4882 specified by OPNUM and TYPE. */
4884 static int
4885 reload_reg_free_p (regno, opnum, type)
4886 int regno;
4887 int opnum;
4888 enum reload_type type;
4890 int i;
4892 /* In use for a RELOAD_OTHER means it's not available for anything. */
4893 if (TEST_HARD_REG_BIT (reload_reg_used, regno))
4894 return 0;
4896 switch (type)
4898 case RELOAD_OTHER:
4899 /* In use for anything means we can't use it for RELOAD_OTHER. */
4900 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4901 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4902 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4903 return 0;
4905 for (i = 0; i < reload_n_operands; i++)
4906 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4907 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4908 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4909 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4910 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4911 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4912 return 0;
4914 return 1;
4916 case RELOAD_FOR_INPUT:
4917 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4918 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4919 return 0;
4921 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4922 return 0;
4924 /* If it is used for some other input, can't use it. */
4925 for (i = 0; i < reload_n_operands; i++)
4926 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4927 return 0;
4929 /* If it is used in a later operand's address, can't use it. */
4930 for (i = opnum + 1; i < reload_n_operands; i++)
4931 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4932 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4933 return 0;
4935 return 1;
4937 case RELOAD_FOR_INPUT_ADDRESS:
4938 /* Can't use a register if it is used for an input address for this
4939 operand or used as an input in an earlier one. */
4940 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4941 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4942 return 0;
4944 for (i = 0; i < opnum; i++)
4945 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4946 return 0;
4948 return 1;
4950 case RELOAD_FOR_INPADDR_ADDRESS:
4951 /* Can't use a register if it is used for an input address
4952 for this operand or used as an input in an earlier
4953 one. */
4954 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4955 return 0;
4957 for (i = 0; i < opnum; i++)
4958 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4959 return 0;
4961 return 1;
4963 case RELOAD_FOR_OUTPUT_ADDRESS:
4964 /* Can't use a register if it is used for an output address for this
4965 operand or used as an output in this or a later operand. */
4966 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4967 return 0;
4969 for (i = opnum; i < reload_n_operands; i++)
4970 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4971 return 0;
4973 return 1;
4975 case RELOAD_FOR_OUTADDR_ADDRESS:
4976 /* Can't use a register if it is used for an output address
4977 for this operand or used as an output in this or a
4978 later operand. */
4979 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4980 return 0;
4982 for (i = opnum; i < reload_n_operands; i++)
4983 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4984 return 0;
4986 return 1;
4988 case RELOAD_FOR_OPERAND_ADDRESS:
4989 for (i = 0; i < reload_n_operands; i++)
4990 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4991 return 0;
4993 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4994 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4996 case RELOAD_FOR_OPADDR_ADDR:
4997 for (i = 0; i < reload_n_operands; i++)
4998 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4999 return 0;
5001 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
5003 case RELOAD_FOR_OUTPUT:
5004 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
5005 outputs, or an operand address for this or an earlier output. */
5006 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
5007 return 0;
5009 for (i = 0; i < reload_n_operands; i++)
5010 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5011 return 0;
5013 for (i = 0; i <= opnum; i++)
5014 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5015 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5016 return 0;
5018 return 1;
5020 case RELOAD_FOR_INSN:
5021 for (i = 0; i < reload_n_operands; i++)
5022 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
5023 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5024 return 0;
5026 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5027 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
5029 case RELOAD_FOR_OTHER_ADDRESS:
5030 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
5032 abort ();
5035 /* Return 1 if the value in reload reg REGNO, as used by a reload
5036 needed for the part of the insn specified by OPNUM and TYPE,
5037 is still available in REGNO at the end of the insn.
5039 We can assume that the reload reg was already tested for availability
5040 at the time it is needed, and we should not check this again,
5041 in case the reg has already been marked in use. */
5043 static int
5044 reload_reg_reaches_end_p (regno, opnum, type)
5045 int regno;
5046 int opnum;
5047 enum reload_type type;
5049 int i;
5051 switch (type)
5053 case RELOAD_OTHER:
5054 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
5055 its value must reach the end. */
5056 return 1;
5058 /* If this use is for part of the insn,
5059 its value reaches if no subsequent part uses the same register.
5060 Just like the above function, don't try to do this with lots
5061 of fallthroughs. */
5063 case RELOAD_FOR_OTHER_ADDRESS:
5064 /* Here we check for everything else, since these don't conflict
5065 with anything else and everything comes later. */
5067 for (i = 0; i < reload_n_operands; i++)
5068 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5069 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5070 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
5071 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5072 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5073 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5074 return 0;
5076 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5077 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5078 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
5080 case RELOAD_FOR_INPUT_ADDRESS:
5081 case RELOAD_FOR_INPADDR_ADDRESS:
5082 /* Similar, except that we check only for this and subsequent inputs
5083 and the address of only subsequent inputs and we do not need
5084 to check for RELOAD_OTHER objects since they are known not to
5085 conflict. */
5087 for (i = opnum; i < reload_n_operands; i++)
5088 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5089 return 0;
5091 for (i = opnum + 1; i < reload_n_operands; i++)
5092 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5093 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
5094 return 0;
5096 for (i = 0; i < reload_n_operands; i++)
5097 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5098 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5099 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5100 return 0;
5102 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
5103 return 0;
5105 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5106 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
5108 case RELOAD_FOR_INPUT:
5109 /* Similar to input address, except we start at the next operand for
5110 both input and input address and we do not check for
5111 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
5112 would conflict. */
5114 for (i = opnum + 1; i < reload_n_operands; i++)
5115 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5116 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5117 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5118 return 0;
5120 /* ... fall through ... */
5122 case RELOAD_FOR_OPERAND_ADDRESS:
5123 /* Check outputs and their addresses. */
5125 for (i = 0; i < reload_n_operands; i++)
5126 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5127 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5128 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5129 return 0;
5131 return 1;
5133 case RELOAD_FOR_OPADDR_ADDR:
5134 for (i = 0; i < reload_n_operands; i++)
5135 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5136 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5137 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5138 return 0;
5140 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5141 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
5143 case RELOAD_FOR_INSN:
5144 /* These conflict with other outputs with RELOAD_OTHER. So
5145 we need only check for output addresses. */
5147 opnum = -1;
5149 /* ... fall through ... */
5151 case RELOAD_FOR_OUTPUT:
5152 case RELOAD_FOR_OUTPUT_ADDRESS:
5153 case RELOAD_FOR_OUTADDR_ADDRESS:
5154 /* We already know these can't conflict with a later output. So the
5155 only thing to check are later output addresses. */
5156 for (i = opnum + 1; i < reload_n_operands; i++)
5157 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5158 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5159 return 0;
5161 return 1;
5164 abort ();
5167 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
5168 Return 0 otherwise.
5170 This function uses the same algorithm as reload_reg_free_p above. */
5173 reloads_conflict (r1, r2)
5174 int r1, r2;
5176 enum reload_type r1_type = rld[r1].when_needed;
5177 enum reload_type r2_type = rld[r2].when_needed;
5178 int r1_opnum = rld[r1].opnum;
5179 int r2_opnum = rld[r2].opnum;
5181 /* RELOAD_OTHER conflicts with everything. */
5182 if (r2_type == RELOAD_OTHER)
5183 return 1;
5185 /* Otherwise, check conflicts differently for each type. */
5187 switch (r1_type)
5189 case RELOAD_FOR_INPUT:
5190 return (r2_type == RELOAD_FOR_INSN
5191 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
5192 || r2_type == RELOAD_FOR_OPADDR_ADDR
5193 || r2_type == RELOAD_FOR_INPUT
5194 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
5195 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
5196 && r2_opnum > r1_opnum));
5198 case RELOAD_FOR_INPUT_ADDRESS:
5199 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
5200 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5202 case RELOAD_FOR_INPADDR_ADDRESS:
5203 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
5204 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5206 case RELOAD_FOR_OUTPUT_ADDRESS:
5207 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
5208 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
5210 case RELOAD_FOR_OUTADDR_ADDRESS:
5211 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
5212 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
5214 case RELOAD_FOR_OPERAND_ADDRESS:
5215 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
5216 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5218 case RELOAD_FOR_OPADDR_ADDR:
5219 return (r2_type == RELOAD_FOR_INPUT
5220 || r2_type == RELOAD_FOR_OPADDR_ADDR);
5222 case RELOAD_FOR_OUTPUT:
5223 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
5224 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
5225 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
5226 && r2_opnum >= r1_opnum));
5228 case RELOAD_FOR_INSN:
5229 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
5230 || r2_type == RELOAD_FOR_INSN
5231 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5233 case RELOAD_FOR_OTHER_ADDRESS:
5234 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
5236 case RELOAD_OTHER:
5237 return 1;
5239 default:
5240 abort ();
5244 /* Vector of reload-numbers showing the order in which the reloads should
5245 be processed. */
5246 short reload_order[MAX_RELOADS];
5248 /* Indexed by reload number, 1 if incoming value
5249 inherited from previous insns. */
5250 char reload_inherited[MAX_RELOADS];
5252 /* For an inherited reload, this is the insn the reload was inherited from,
5253 if we know it. Otherwise, this is 0. */
5254 rtx reload_inheritance_insn[MAX_RELOADS];
5256 /* If non-zero, this is a place to get the value of the reload,
5257 rather than using reload_in. */
5258 rtx reload_override_in[MAX_RELOADS];
5260 /* For each reload, the hard register number of the register used,
5261 or -1 if we did not need a register for this reload. */
5262 int reload_spill_index[MAX_RELOADS];
5264 /* Return 1 if the value in reload reg REGNO, as used by a reload
5265 needed for the part of the insn specified by OPNUM and TYPE,
5266 may be used to load VALUE into it.
5268 Other read-only reloads with the same value do not conflict
5269 unless OUT is non-zero and these other reloads have to live while
5270 output reloads live.
5271 If OUT is CONST0_RTX, this is a special case: it means that the
5272 test should not be for using register REGNO as reload register, but
5273 for copying from register REGNO into the reload register.
5275 RELOADNUM is the number of the reload we want to load this value for;
5276 a reload does not conflict with itself.
5278 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
5279 reloads that load an address for the very reload we are considering.
5281 The caller has to make sure that there is no conflict with the return
5282 register. */
5283 static int
5284 reload_reg_free_for_value_p (regno, opnum, type, value, out, reloadnum,
5285 ignore_address_reloads)
5286 int regno;
5287 int opnum;
5288 enum reload_type type;
5289 rtx value, out;
5290 int reloadnum;
5291 int ignore_address_reloads;
5293 int time1;
5294 /* Set if we see an input reload that must not share its reload register
5295 with any new earlyclobber, but might otherwise share the reload
5296 register with an output or input-output reload. */
5297 int check_earlyclobber = 0;
5298 int i;
5299 int copy = 0;
5301 if (out == const0_rtx)
5303 copy = 1;
5304 out = NULL_RTX;
5307 /* We use some pseudo 'time' value to check if the lifetimes of the
5308 new register use would overlap with the one of a previous reload
5309 that is not read-only or uses a different value.
5310 The 'time' used doesn't have to be linear in any shape or form, just
5311 monotonic.
5312 Some reload types use different 'buckets' for each operand.
5313 So there are MAX_RECOG_OPERANDS different time values for each
5314 such reload type.
5315 We compute TIME1 as the time when the register for the prospective
5316 new reload ceases to be live, and TIME2 for each existing
5317 reload as the time when that the reload register of that reload
5318 becomes live.
5319 Where there is little to be gained by exact lifetime calculations,
5320 we just make conservative assumptions, i.e. a longer lifetime;
5321 this is done in the 'default:' cases. */
5322 switch (type)
5324 case RELOAD_FOR_OTHER_ADDRESS:
5325 /* RELOAD_FOR_OTHER_ADDRESS conflits with RELOAD_OTHER reloads. */
5326 time1 = copy ? 0 : 1;
5327 break;
5328 case RELOAD_OTHER:
5329 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
5330 break;
5331 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
5332 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
5333 respectively, to the time values for these, we get distinct time
5334 values. To get distinct time values for each operand, we have to
5335 multiply opnum by at least three. We round that up to four because
5336 multiply by four is often cheaper. */
5337 case RELOAD_FOR_INPADDR_ADDRESS:
5338 time1 = opnum * 4 + 2;
5339 break;
5340 case RELOAD_FOR_INPUT_ADDRESS:
5341 time1 = opnum * 4 + 3;
5342 break;
5343 case RELOAD_FOR_INPUT:
5344 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
5345 executes (inclusive). */
5346 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
5347 break;
5348 case RELOAD_FOR_OPADDR_ADDR:
5349 /* opnum * 4 + 4
5350 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
5351 time1 = MAX_RECOG_OPERANDS * 4 + 1;
5352 break;
5353 case RELOAD_FOR_OPERAND_ADDRESS:
5354 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
5355 is executed. */
5356 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
5357 break;
5358 case RELOAD_FOR_OUTADDR_ADDRESS:
5359 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
5360 break;
5361 case RELOAD_FOR_OUTPUT_ADDRESS:
5362 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
5363 break;
5364 default:
5365 time1 = MAX_RECOG_OPERANDS * 5 + 5;
5368 for (i = 0; i < n_reloads; i++)
5370 rtx reg = rld[i].reg_rtx;
5371 if (reg && GET_CODE (reg) == REG
5372 && ((unsigned) regno - true_regnum (reg)
5373 <= HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)) - (unsigned)1)
5374 && i != reloadnum)
5376 if (! rld[i].in || ! rtx_equal_p (rld[i].in, value)
5377 || rld[i].out || out)
5379 int time2;
5380 switch (rld[i].when_needed)
5382 case RELOAD_FOR_OTHER_ADDRESS:
5383 time2 = 0;
5384 break;
5385 case RELOAD_FOR_INPADDR_ADDRESS:
5386 /* find_reloads makes sure that a
5387 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
5388 by at most one - the first -
5389 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
5390 address reload is inherited, the address address reload
5391 goes away, so we can ignore this conflict. */
5392 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
5393 && ignore_address_reloads
5394 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
5395 Then the address address is still needed to store
5396 back the new address. */
5397 && ! rld[reloadnum].out)
5398 continue;
5399 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
5400 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
5401 reloads go away. */
5402 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5403 && ignore_address_reloads
5404 /* Unless we are reloading an auto_inc expression. */
5405 && ! rld[reloadnum].out)
5406 continue;
5407 time2 = rld[i].opnum * 4 + 2;
5408 break;
5409 case RELOAD_FOR_INPUT_ADDRESS:
5410 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5411 && ignore_address_reloads
5412 && ! rld[reloadnum].out)
5413 continue;
5414 time2 = rld[i].opnum * 4 + 3;
5415 break;
5416 case RELOAD_FOR_INPUT:
5417 time2 = rld[i].opnum * 4 + 4;
5418 check_earlyclobber = 1;
5419 break;
5420 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
5421 == MAX_RECOG_OPERAND * 4 */
5422 case RELOAD_FOR_OPADDR_ADDR:
5423 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
5424 && ignore_address_reloads
5425 && ! rld[reloadnum].out)
5426 continue;
5427 time2 = MAX_RECOG_OPERANDS * 4 + 1;
5428 break;
5429 case RELOAD_FOR_OPERAND_ADDRESS:
5430 time2 = MAX_RECOG_OPERANDS * 4 + 2;
5431 check_earlyclobber = 1;
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 if (earlyclobber_operand_p (rld[i].out))
5462 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5464 break;
5466 time2 = 1;
5467 /* RELOAD_OTHER might be live beyond instruction execution,
5468 but this is not obvious when we set time2 = 1. So check
5469 here if there might be a problem with the new reload
5470 clobbering the register used by the RELOAD_OTHER. */
5471 if (out)
5472 return 0;
5473 break;
5474 default:
5475 return 0;
5477 if ((time1 >= time2
5478 && (! rld[i].in || rld[i].out
5479 || ! rtx_equal_p (rld[i].in, value)))
5480 || (out && rld[reloadnum].out_reg
5481 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
5482 return 0;
5487 /* Earlyclobbered outputs must conflict with inputs. */
5488 if (check_earlyclobber && out && earlyclobber_operand_p (out))
5489 return 0;
5491 return 1;
5494 /* Give an error message saying we failed to find a reload for INSN,
5495 and clear out reload R. */
5496 static void
5497 failed_reload (insn, r)
5498 rtx insn;
5499 int r;
5501 if (asm_noperands (PATTERN (insn)) < 0)
5502 /* It's the compiler's fault. */
5503 fatal_insn ("Could not find a spill register", insn);
5505 /* It's the user's fault; the operand's mode and constraint
5506 don't match. Disable this reload so we don't crash in final. */
5507 error_for_asm (insn,
5508 "`asm' operand constraint incompatible with operand size");
5509 rld[r].in = 0;
5510 rld[r].out = 0;
5511 rld[r].reg_rtx = 0;
5512 rld[r].optional = 1;
5513 rld[r].secondary_p = 1;
5516 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5517 for reload R. If it's valid, get an rtx for it. Return nonzero if
5518 successful. */
5519 static int
5520 set_reload_reg (i, r)
5521 int i, r;
5523 int regno;
5524 rtx reg = spill_reg_rtx[i];
5526 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5527 spill_reg_rtx[i] = reg
5528 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5530 regno = true_regnum (reg);
5532 /* Detect when the reload reg can't hold the reload mode.
5533 This used to be one `if', but Sequent compiler can't handle that. */
5534 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5536 enum machine_mode test_mode = VOIDmode;
5537 if (rld[r].in)
5538 test_mode = GET_MODE (rld[r].in);
5539 /* If rld[r].in has VOIDmode, it means we will load it
5540 in whatever mode the reload reg has: to wit, rld[r].mode.
5541 We have already tested that for validity. */
5542 /* Aside from that, we need to test that the expressions
5543 to reload from or into have modes which are valid for this
5544 reload register. Otherwise the reload insns would be invalid. */
5545 if (! (rld[r].in != 0 && test_mode != VOIDmode
5546 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5547 if (! (rld[r].out != 0
5548 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5550 /* The reg is OK. */
5551 last_spill_reg = i;
5553 /* Mark as in use for this insn the reload regs we use
5554 for this. */
5555 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5556 rld[r].when_needed, rld[r].mode);
5558 rld[r].reg_rtx = reg;
5559 reload_spill_index[r] = spill_regs[i];
5560 return 1;
5563 return 0;
5566 /* Find a spill register to use as a reload register for reload R.
5567 LAST_RELOAD is non-zero if this is the last reload for the insn being
5568 processed.
5570 Set rld[R].reg_rtx to the register allocated.
5572 If NOERROR is nonzero, we return 1 if successful,
5573 or 0 if we couldn't find a spill reg and we didn't change anything. */
5575 static int
5576 allocate_reload_reg (chain, r, last_reload, noerror)
5577 struct insn_chain *chain;
5578 int r;
5579 int last_reload;
5580 int noerror;
5582 rtx insn = chain->insn;
5583 int i, pass, count;
5585 /* If we put this reload ahead, thinking it is a group,
5586 then insist on finding a group. Otherwise we can grab a
5587 reg that some other reload needs.
5588 (That can happen when we have a 68000 DATA_OR_FP_REG
5589 which is a group of data regs or one fp reg.)
5590 We need not be so restrictive if there are no more reloads
5591 for this insn.
5593 ??? Really it would be nicer to have smarter handling
5594 for that kind of reg class, where a problem like this is normal.
5595 Perhaps those classes should be avoided for reloading
5596 by use of more alternatives. */
5598 int force_group = rld[r].nregs > 1 && ! last_reload;
5600 /* If we want a single register and haven't yet found one,
5601 take any reg in the right class and not in use.
5602 If we want a consecutive group, here is where we look for it.
5604 We use two passes so we can first look for reload regs to
5605 reuse, which are already in use for other reloads in this insn,
5606 and only then use additional registers.
5607 I think that maximizing reuse is needed to make sure we don't
5608 run out of reload regs. Suppose we have three reloads, and
5609 reloads A and B can share regs. These need two regs.
5610 Suppose A and B are given different regs.
5611 That leaves none for C. */
5612 for (pass = 0; pass < 2; pass++)
5614 /* I is the index in spill_regs.
5615 We advance it round-robin between insns to use all spill regs
5616 equally, so that inherited reloads have a chance
5617 of leapfrogging each other. Don't do this, however, when we have
5618 group needs and failure would be fatal; if we only have a relatively
5619 small number of spill registers, and more than one of them has
5620 group needs, then by starting in the middle, we may end up
5621 allocating the first one in such a way that we are not left with
5622 sufficient groups to handle the rest. */
5624 if (noerror || ! force_group)
5625 i = last_spill_reg;
5626 else
5627 i = -1;
5629 for (count = 0; count < n_spills; count++)
5631 int class = (int) rld[r].class;
5632 int regnum;
5634 i++;
5635 if (i >= n_spills)
5636 i -= n_spills;
5637 regnum = spill_regs[i];
5639 if ((reload_reg_free_p (regnum, rld[r].opnum,
5640 rld[r].when_needed)
5641 || (rld[r].in
5642 /* We check reload_reg_used to make sure we
5643 don't clobber the return register. */
5644 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5645 && reload_reg_free_for_value_p (regnum,
5646 rld[r].opnum,
5647 rld[r].when_needed,
5648 rld[r].in,
5649 rld[r].out, r, 1)))
5650 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5651 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5652 /* Look first for regs to share, then for unshared. But
5653 don't share regs used for inherited reloads; they are
5654 the ones we want to preserve. */
5655 && (pass
5656 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5657 regnum)
5658 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5659 regnum))))
5661 int nr = HARD_REGNO_NREGS (regnum, rld[r].mode);
5662 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5663 (on 68000) got us two FP regs. If NR is 1,
5664 we would reject both of them. */
5665 if (force_group)
5666 nr = rld[r].nregs;
5667 /* If we need only one reg, we have already won. */
5668 if (nr == 1)
5670 /* But reject a single reg if we demand a group. */
5671 if (force_group)
5672 continue;
5673 break;
5675 /* Otherwise check that as many consecutive regs as we need
5676 are available here.
5677 Also, don't use for a group registers that are
5678 needed for nongroups. */
5679 if (! TEST_HARD_REG_BIT (chain->counted_for_nongroups, regnum))
5680 while (nr > 1)
5682 int regno = regnum + nr - 1;
5683 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5684 && spill_reg_order[regno] >= 0
5685 && reload_reg_free_p (regno, rld[r].opnum,
5686 rld[r].when_needed)
5687 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups,
5688 regno)))
5689 break;
5690 nr--;
5692 if (nr == 1)
5693 break;
5697 /* If we found something on pass 1, omit pass 2. */
5698 if (count < n_spills)
5699 break;
5702 /* We should have found a spill register by now. */
5703 if (count == n_spills)
5705 if (noerror)
5706 return 0;
5707 goto failure;
5710 if (set_reload_reg (i, r))
5711 return 1;
5713 /* The reg is not OK. */
5714 if (noerror)
5715 return 0;
5717 failure:
5718 failed_reload (insn, r);
5720 return 1;
5723 /* Initialize all the tables needed to allocate reload registers.
5724 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5725 is the array we use to restore the reg_rtx field for every reload. */
5726 static void
5727 choose_reload_regs_init (chain, save_reload_reg_rtx)
5728 struct insn_chain *chain;
5729 rtx *save_reload_reg_rtx;
5731 int i;
5733 for (i = 0; i < n_reloads; i++)
5734 rld[i].reg_rtx = save_reload_reg_rtx[i];
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 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5771 for (i = 0; i < n_reloads; i++)
5772 /* If we have already decided to use a certain register,
5773 don't use it in another way. */
5774 if (rld[i].reg_rtx)
5775 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5776 rld[i].when_needed, rld[i].mode);
5779 /* Assign hard reg targets for the pseudo-registers we must reload
5780 into hard regs for this insn.
5781 Also output the instructions to copy them in and out of the hard regs.
5783 For machines with register classes, we are responsible for
5784 finding a reload reg in the proper class. */
5786 static void
5787 choose_reload_regs (chain)
5788 struct insn_chain *chain;
5790 rtx insn = chain->insn;
5791 register int i, j;
5792 int max_group_size = 1;
5793 enum reg_class group_class = NO_REGS;
5794 int inheritance;
5795 int pass;
5797 rtx save_reload_reg_rtx[MAX_RELOADS];
5799 /* In order to be certain of getting the registers we need,
5800 we must sort the reloads into order of increasing register class.
5801 Then our grabbing of reload registers will parallel the process
5802 that provided the reload registers.
5804 Also note whether any of the reloads wants a consecutive group of regs.
5805 If so, record the maximum size of the group desired and what
5806 register class contains all the groups needed by this insn. */
5808 for (j = 0; j < n_reloads; j++)
5810 reload_order[j] = j;
5811 reload_spill_index[j] = -1;
5813 if (rld[j].nregs > 1)
5815 max_group_size = MAX (rld[j].nregs, max_group_size);
5816 group_class = reg_class_superunion[(int)rld[j].class][(int)group_class];
5819 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5822 if (n_reloads > 1)
5823 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5825 /* If -O, try first with inheritance, then turning it off.
5826 If not -O, don't do inheritance.
5827 Using inheritance when not optimizing leads to paradoxes
5828 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5829 because one side of the comparison might be inherited. */
5831 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5833 choose_reload_regs_init (chain, save_reload_reg_rtx);
5835 /* Process the reloads in order of preference just found.
5836 Beyond this point, subregs can be found in reload_reg_rtx.
5838 This used to look for an existing reloaded home for all
5839 of the reloads, and only then perform any new reloads.
5840 But that could lose if the reloads were done out of reg-class order
5841 because a later reload with a looser constraint might have an old
5842 home in a register needed by an earlier reload with a tighter constraint.
5844 To solve this, we make two passes over the reloads, in the order
5845 described above. In the first pass we try to inherit a reload
5846 from a previous insn. If there is a later reload that needs a
5847 class that is a proper subset of the class being processed, we must
5848 also allocate a spill register during the first pass.
5850 Then make a second pass over the reloads to allocate any reloads
5851 that haven't been given registers yet. */
5853 for (j = 0; j < n_reloads; j++)
5855 register int r = reload_order[j];
5856 rtx search_equiv = NULL_RTX;
5858 /* Ignore reloads that got marked inoperative. */
5859 if (rld[r].out == 0 && rld[r].in == 0
5860 && ! rld[r].secondary_p)
5861 continue;
5863 /* If find_reloads chose to use reload_in or reload_out as a reload
5864 register, we don't need to chose one. Otherwise, try even if it
5865 found one since we might save an insn if we find the value lying
5866 around.
5867 Try also when reload_in is a pseudo without a hard reg. */
5868 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5869 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5870 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5871 && GET_CODE (rld[r].in) != MEM
5872 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5873 continue;
5875 #if 0 /* No longer needed for correct operation.
5876 It might give better code, or might not; worth an experiment? */
5877 /* If this is an optional reload, we can't inherit from earlier insns
5878 until we are sure that any non-optional reloads have been allocated.
5879 The following code takes advantage of the fact that optional reloads
5880 are at the end of reload_order. */
5881 if (rld[r].optional != 0)
5882 for (i = 0; i < j; i++)
5883 if ((rld[reload_order[i]].out != 0
5884 || rld[reload_order[i]].in != 0
5885 || rld[reload_order[i]].secondary_p)
5886 && ! rld[reload_order[i]].optional
5887 && rld[reload_order[i]].reg_rtx == 0)
5888 allocate_reload_reg (chain, reload_order[i], 0, inheritance);
5889 #endif
5891 /* First see if this pseudo is already available as reloaded
5892 for a previous insn. We cannot try to inherit for reloads
5893 that are smaller than the maximum number of registers needed
5894 for groups unless the register we would allocate cannot be used
5895 for the groups.
5897 We could check here to see if this is a secondary reload for
5898 an object that is already in a register of the desired class.
5899 This would avoid the need for the secondary reload register.
5900 But this is complex because we can't easily determine what
5901 objects might want to be loaded via this reload. So let a
5902 register be allocated here. In `emit_reload_insns' we suppress
5903 one of the loads in the case described above. */
5905 if (inheritance)
5907 int word = 0;
5908 register int regno = -1;
5909 enum machine_mode mode = VOIDmode;
5911 if (rld[r].in == 0)
5913 else if (GET_CODE (rld[r].in) == REG)
5915 regno = REGNO (rld[r].in);
5916 mode = GET_MODE (rld[r].in);
5918 else if (GET_CODE (rld[r].in_reg) == REG)
5920 regno = REGNO (rld[r].in_reg);
5921 mode = GET_MODE (rld[r].in_reg);
5923 else if (GET_CODE (rld[r].in_reg) == SUBREG
5924 && GET_CODE (SUBREG_REG (rld[r].in_reg)) == REG)
5926 word = SUBREG_WORD (rld[r].in_reg);
5927 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5928 if (regno < FIRST_PSEUDO_REGISTER)
5929 regno += word;
5930 mode = GET_MODE (rld[r].in_reg);
5932 #ifdef AUTO_INC_DEC
5933 else if ((GET_CODE (rld[r].in_reg) == PRE_INC
5934 || GET_CODE (rld[r].in_reg) == PRE_DEC
5935 || GET_CODE (rld[r].in_reg) == POST_INC
5936 || GET_CODE (rld[r].in_reg) == POST_DEC)
5937 && GET_CODE (XEXP (rld[r].in_reg, 0)) == REG)
5939 regno = REGNO (XEXP (rld[r].in_reg, 0));
5940 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5941 rld[r].out = rld[r].in;
5943 #endif
5944 #if 0
5945 /* This won't work, since REGNO can be a pseudo reg number.
5946 Also, it takes much more hair to keep track of all the things
5947 that can invalidate an inherited reload of part of a pseudoreg. */
5948 else if (GET_CODE (rld[r].in) == SUBREG
5949 && GET_CODE (SUBREG_REG (rld[r].in)) == REG)
5950 regno = REGNO (SUBREG_REG (rld[r].in)) + SUBREG_WORD (rld[r].in);
5951 #endif
5953 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5955 enum reg_class class = rld[r].class, last_class;
5956 rtx last_reg = reg_last_reload_reg[regno];
5958 i = REGNO (last_reg) + word;
5959 last_class = REGNO_REG_CLASS (i);
5960 if ((GET_MODE_SIZE (GET_MODE (last_reg))
5961 >= GET_MODE_SIZE (mode) + word * UNITS_PER_WORD)
5962 && reg_reloaded_contents[i] == regno
5963 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
5964 && HARD_REGNO_MODE_OK (i, rld[r].mode)
5965 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5966 /* Even if we can't use this register as a reload
5967 register, we might use it for reload_override_in,
5968 if copying it to the desired class is cheap
5969 enough. */
5970 || ((REGISTER_MOVE_COST (last_class, class)
5971 < MEMORY_MOVE_COST (mode, class, 1))
5972 #ifdef SECONDARY_INPUT_RELOAD_CLASS
5973 && (SECONDARY_INPUT_RELOAD_CLASS (class, mode,
5974 last_reg)
5975 == NO_REGS)
5976 #endif
5977 #ifdef SECONDARY_MEMORY_NEEDED
5978 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5979 mode)
5980 #endif
5983 && (rld[r].nregs == max_group_size
5984 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5986 && reload_reg_free_for_value_p (i, rld[r].opnum,
5987 rld[r].when_needed,
5988 rld[r].in,
5989 const0_rtx, r, 1))
5991 /* If a group is needed, verify that all the subsequent
5992 registers still have their values intact. */
5993 int nr
5994 = HARD_REGNO_NREGS (i, rld[r].mode);
5995 int k;
5997 for (k = 1; k < nr; k++)
5998 if (reg_reloaded_contents[i + k] != regno
5999 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
6000 break;
6002 if (k == nr)
6004 int i1;
6006 last_reg = (GET_MODE (last_reg) == mode
6007 ? last_reg : gen_rtx_REG (mode, i));
6009 /* We found a register that contains the
6010 value we need. If this register is the
6011 same as an `earlyclobber' operand of the
6012 current insn, just mark it as a place to
6013 reload from since we can't use it as the
6014 reload register itself. */
6016 for (i1 = 0; i1 < n_earlyclobbers; i1++)
6017 if (reg_overlap_mentioned_for_reload_p
6018 (reg_last_reload_reg[regno],
6019 reload_earlyclobbers[i1]))
6020 break;
6022 if (i1 != n_earlyclobbers
6023 || ! (reload_reg_free_for_value_p
6024 (i, rld[r].opnum, rld[r].when_needed,
6025 rld[r].in, rld[r].out, r, 1))
6026 /* Don't use it if we'd clobber a pseudo reg. */
6027 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
6028 && rld[r].out
6029 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
6030 /* Don't clobber the frame pointer. */
6031 || (i == HARD_FRAME_POINTER_REGNUM && rld[r].out)
6032 /* Don't really use the inherited spill reg
6033 if we need it wider than we've got it. */
6034 || (GET_MODE_SIZE (rld[r].mode)
6035 > GET_MODE_SIZE (mode))
6036 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
6039 /* If find_reloads chose reload_out as reload
6040 register, stay with it - that leaves the
6041 inherited register for subsequent reloads. */
6042 || (rld[r].out && rld[r].reg_rtx
6043 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
6045 reload_override_in[r] = last_reg;
6046 reload_inheritance_insn[r]
6047 = reg_reloaded_insn[i];
6049 else
6051 int k;
6052 /* We can use this as a reload reg. */
6053 /* Mark the register as in use for this part of
6054 the insn. */
6055 mark_reload_reg_in_use (i,
6056 rld[r].opnum,
6057 rld[r].when_needed,
6058 rld[r].mode);
6059 rld[r].reg_rtx = last_reg;
6060 reload_inherited[r] = 1;
6061 reload_inheritance_insn[r]
6062 = reg_reloaded_insn[i];
6063 reload_spill_index[r] = i;
6064 for (k = 0; k < nr; k++)
6065 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6066 i + k);
6073 /* Here's another way to see if the value is already lying around. */
6074 if (inheritance
6075 && rld[r].in != 0
6076 && ! reload_inherited[r]
6077 && rld[r].out == 0
6078 && (CONSTANT_P (rld[r].in)
6079 || GET_CODE (rld[r].in) == PLUS
6080 || GET_CODE (rld[r].in) == REG
6081 || GET_CODE (rld[r].in) == MEM)
6082 && (rld[r].nregs == max_group_size
6083 || ! reg_classes_intersect_p (rld[r].class, group_class)))
6084 search_equiv = rld[r].in;
6085 /* If this is an output reload from a simple move insn, look
6086 if an equivalence for the input is available. */
6087 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
6089 rtx set = single_set (insn);
6091 if (set
6092 && rtx_equal_p (rld[r].out, SET_DEST (set))
6093 && CONSTANT_P (SET_SRC (set)))
6094 search_equiv = SET_SRC (set);
6097 if (search_equiv)
6099 register rtx equiv
6100 = find_equiv_reg (search_equiv, insn, rld[r].class,
6101 -1, NULL_PTR, 0, rld[r].mode);
6102 int regno = 0;
6104 if (equiv != 0)
6106 if (GET_CODE (equiv) == REG)
6107 regno = REGNO (equiv);
6108 else if (GET_CODE (equiv) == SUBREG)
6110 /* This must be a SUBREG of a hard register.
6111 Make a new REG since this might be used in an
6112 address and not all machines support SUBREGs
6113 there. */
6114 regno = REGNO (SUBREG_REG (equiv)) + SUBREG_WORD (equiv);
6115 equiv = gen_rtx_REG (rld[r].mode, regno);
6117 else
6118 abort ();
6121 /* If we found a spill reg, reject it unless it is free
6122 and of the desired class. */
6123 if (equiv != 0
6124 && ((TEST_HARD_REG_BIT (reload_reg_used_at_all, regno)
6125 && ! reload_reg_free_for_value_p (regno, rld[r].opnum,
6126 rld[r].when_needed,
6127 rld[r].in,
6128 rld[r].out, r, 1))
6129 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
6130 regno)))
6131 equiv = 0;
6133 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
6134 equiv = 0;
6136 /* We found a register that contains the value we need.
6137 If this register is the same as an `earlyclobber' operand
6138 of the current insn, just mark it as a place to reload from
6139 since we can't use it as the reload register itself. */
6141 if (equiv != 0)
6142 for (i = 0; i < n_earlyclobbers; i++)
6143 if (reg_overlap_mentioned_for_reload_p (equiv,
6144 reload_earlyclobbers[i]))
6146 reload_override_in[r] = equiv;
6147 equiv = 0;
6148 break;
6151 /* If the equiv register we have found is explicitly clobbered
6152 in the current insn, it depends on the reload type if we
6153 can use it, use it for reload_override_in, or not at all.
6154 In particular, we then can't use EQUIV for a
6155 RELOAD_FOR_OUTPUT_ADDRESS reload. */
6157 if (equiv != 0 && regno_clobbered_p (regno, insn))
6159 switch (rld[r].when_needed)
6161 case RELOAD_FOR_OTHER_ADDRESS:
6162 case RELOAD_FOR_INPADDR_ADDRESS:
6163 case RELOAD_FOR_INPUT_ADDRESS:
6164 case RELOAD_FOR_OPADDR_ADDR:
6165 break;
6166 case RELOAD_OTHER:
6167 case RELOAD_FOR_INPUT:
6168 case RELOAD_FOR_OPERAND_ADDRESS:
6169 reload_override_in[r] = equiv;
6170 /* Fall through. */
6171 default:
6172 equiv = 0;
6173 break;
6177 /* If we found an equivalent reg, say no code need be generated
6178 to load it, and use it as our reload reg. */
6179 if (equiv != 0 && regno != HARD_FRAME_POINTER_REGNUM)
6181 int nr = HARD_REGNO_NREGS (regno, rld[r].mode);
6182 int k;
6183 rld[r].reg_rtx = equiv;
6184 reload_inherited[r] = 1;
6186 /* If reg_reloaded_valid is not set for this register,
6187 there might be a stale spill_reg_store lying around.
6188 We must clear it, since otherwise emit_reload_insns
6189 might delete the store. */
6190 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
6191 spill_reg_store[regno] = NULL_RTX;
6192 /* If any of the hard registers in EQUIV are spill
6193 registers, mark them as in use for this insn. */
6194 for (k = 0; k < nr; k++)
6196 i = spill_reg_order[regno + k];
6197 if (i >= 0)
6199 mark_reload_reg_in_use (regno, rld[r].opnum,
6200 rld[r].when_needed,
6201 rld[r].mode);
6202 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6203 regno + k);
6209 /* If we found a register to use already, or if this is an optional
6210 reload, we are done. */
6211 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
6212 continue;
6214 #if 0 /* No longer needed for correct operation. Might or might not
6215 give better code on the average. Want to experiment? */
6217 /* See if there is a later reload that has a class different from our
6218 class that intersects our class or that requires less register
6219 than our reload. If so, we must allocate a register to this
6220 reload now, since that reload might inherit a previous reload
6221 and take the only available register in our class. Don't do this
6222 for optional reloads since they will force all previous reloads
6223 to be allocated. Also don't do this for reloads that have been
6224 turned off. */
6226 for (i = j + 1; i < n_reloads; i++)
6228 int s = reload_order[i];
6230 if ((rld[s].in == 0 && rld[s].out == 0
6231 && ! rld[s].secondary_p)
6232 || rld[s].optional)
6233 continue;
6235 if ((rld[s].class != rld[r].class
6236 && reg_classes_intersect_p (rld[r].class,
6237 rld[s].class))
6238 || rld[s].nregs < rld[r].nregs)
6239 break;
6242 if (i == n_reloads)
6243 continue;
6245 allocate_reload_reg (chain, r, j == n_reloads - 1, inheritance);
6246 #endif
6249 /* Now allocate reload registers for anything non-optional that
6250 didn't get one yet. */
6251 for (j = 0; j < n_reloads; j++)
6253 register int r = reload_order[j];
6255 /* Ignore reloads that got marked inoperative. */
6256 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
6257 continue;
6259 /* Skip reloads that already have a register allocated or are
6260 optional. */
6261 if (rld[r].reg_rtx != 0 || rld[r].optional)
6262 continue;
6264 if (! allocate_reload_reg (chain, r, j == n_reloads - 1, inheritance))
6265 break;
6268 /* If that loop got all the way, we have won. */
6269 if (j == n_reloads)
6270 break;
6272 /* Loop around and try without any inheritance. */
6275 /* If we thought we could inherit a reload, because it seemed that
6276 nothing else wanted the same reload register earlier in the insn,
6277 verify that assumption, now that all reloads have been assigned.
6278 Likewise for reloads where reload_override_in has been set. */
6280 /* If doing expensive optimizations, do one preliminary pass that doesn't
6281 cancel any inheritance, but removes reloads that have been needed only
6282 for reloads that we know can be inherited. */
6283 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
6285 for (j = 0; j < n_reloads; j++)
6287 register int r = reload_order[j];
6288 rtx check_reg;
6289 if (reload_inherited[r] && rld[r].reg_rtx)
6290 check_reg = rld[r].reg_rtx;
6291 else if (reload_override_in[r]
6292 && (GET_CODE (reload_override_in[r]) == REG
6293 || GET_CODE (reload_override_in[r]) == SUBREG))
6294 check_reg = reload_override_in[r];
6295 else
6296 continue;
6297 if (! reload_reg_free_for_value_p (true_regnum (check_reg),
6298 rld[r].opnum,
6299 rld[r].when_needed,
6300 rld[r].in,
6301 (reload_inherited[r]
6302 ? rld[r].out : const0_rtx),
6303 r, 1))
6305 if (pass)
6306 continue;
6307 reload_inherited[r] = 0;
6308 reload_override_in[r] = 0;
6310 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
6311 reload_override_in, then we do not need its related
6312 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
6313 likewise for other reload types.
6314 We handle this by removing a reload when its only replacement
6315 is mentioned in reload_in of the reload we are going to inherit.
6316 A special case are auto_inc expressions; even if the input is
6317 inherited, we still need the address for the output. We can
6318 recognize them because they have RELOAD_OUT set to RELOAD_IN.
6319 If we suceeded removing some reload and we are doing a preliminary
6320 pass just to remove such reloads, make another pass, since the
6321 removal of one reload might allow us to inherit another one. */
6322 else if (rld[r].in
6323 && rld[r].out != rld[r].in
6324 && remove_address_replacements (rld[r].in) && pass)
6325 pass = 2;
6329 /* Now that reload_override_in is known valid,
6330 actually override reload_in. */
6331 for (j = 0; j < n_reloads; j++)
6332 if (reload_override_in[j])
6333 rld[j].in = reload_override_in[j];
6335 /* If this reload won't be done because it has been cancelled or is
6336 optional and not inherited, clear reload_reg_rtx so other
6337 routines (such as subst_reloads) don't get confused. */
6338 for (j = 0; j < n_reloads; j++)
6339 if (rld[j].reg_rtx != 0
6340 && ((rld[j].optional && ! reload_inherited[j])
6341 || (rld[j].in == 0 && rld[j].out == 0
6342 && ! rld[j].secondary_p)))
6344 int regno = true_regnum (rld[j].reg_rtx);
6346 if (spill_reg_order[regno] >= 0)
6347 clear_reload_reg_in_use (regno, rld[j].opnum,
6348 rld[j].when_needed, rld[j].mode);
6349 rld[j].reg_rtx = 0;
6352 /* Record which pseudos and which spill regs have output reloads. */
6353 for (j = 0; j < n_reloads; j++)
6355 register int r = reload_order[j];
6357 i = reload_spill_index[r];
6359 /* I is nonneg if this reload uses a register.
6360 If rld[r].reg_rtx is 0, this is an optional reload
6361 that we opted to ignore. */
6362 if (rld[r].out_reg != 0 && GET_CODE (rld[r].out_reg) == REG
6363 && rld[r].reg_rtx != 0)
6365 register int nregno = REGNO (rld[r].out_reg);
6366 int nr = 1;
6368 if (nregno < FIRST_PSEUDO_REGISTER)
6369 nr = HARD_REGNO_NREGS (nregno, rld[r].mode);
6371 while (--nr >= 0)
6372 reg_has_output_reload[nregno + nr] = 1;
6374 if (i >= 0)
6376 nr = HARD_REGNO_NREGS (i, rld[r].mode);
6377 while (--nr >= 0)
6378 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
6381 if (rld[r].when_needed != RELOAD_OTHER
6382 && rld[r].when_needed != RELOAD_FOR_OUTPUT
6383 && rld[r].when_needed != RELOAD_FOR_INSN)
6384 abort ();
6389 /* Deallocate the reload register for reload R. This is called from
6390 remove_address_replacements. */
6391 void
6392 deallocate_reload_reg (r)
6393 int r;
6395 int regno;
6397 if (! rld[r].reg_rtx)
6398 return;
6399 regno = true_regnum (rld[r].reg_rtx);
6400 rld[r].reg_rtx = 0;
6401 if (spill_reg_order[regno] >= 0)
6402 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
6403 rld[r].mode);
6404 reload_spill_index[r] = -1;
6407 /* If SMALL_REGISTER_CLASSES is non-zero, we may not have merged two
6408 reloads of the same item for fear that we might not have enough reload
6409 registers. However, normally they will get the same reload register
6410 and hence actually need not be loaded twice.
6412 Here we check for the most common case of this phenomenon: when we have
6413 a number of reloads for the same object, each of which were allocated
6414 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6415 reload, and is not modified in the insn itself. If we find such,
6416 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6417 This will not increase the number of spill registers needed and will
6418 prevent redundant code. */
6420 static void
6421 merge_assigned_reloads (insn)
6422 rtx insn;
6424 int i, j;
6426 /* Scan all the reloads looking for ones that only load values and
6427 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6428 assigned and not modified by INSN. */
6430 for (i = 0; i < n_reloads; i++)
6432 int conflicting_input = 0;
6433 int max_input_address_opnum = -1;
6434 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6436 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6437 || rld[i].out != 0 || rld[i].reg_rtx == 0
6438 || reg_set_p (rld[i].reg_rtx, insn))
6439 continue;
6441 /* Look at all other reloads. Ensure that the only use of this
6442 reload_reg_rtx is in a reload that just loads the same value
6443 as we do. Note that any secondary reloads must be of the identical
6444 class since the values, modes, and result registers are the
6445 same, so we need not do anything with any secondary reloads. */
6447 for (j = 0; j < n_reloads; j++)
6449 if (i == j || rld[j].reg_rtx == 0
6450 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6451 rld[i].reg_rtx))
6452 continue;
6454 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6455 && rld[j].opnum > max_input_address_opnum)
6456 max_input_address_opnum = rld[j].opnum;
6458 /* If the reload regs aren't exactly the same (e.g, different modes)
6459 or if the values are different, we can't merge this reload.
6460 But if it is an input reload, we might still merge
6461 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6463 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6464 || rld[j].out != 0 || rld[j].in == 0
6465 || ! rtx_equal_p (rld[i].in, rld[j].in))
6467 if (rld[j].when_needed != RELOAD_FOR_INPUT
6468 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6469 || rld[i].opnum > rld[j].opnum)
6470 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6471 break;
6472 conflicting_input = 1;
6473 if (min_conflicting_input_opnum > rld[j].opnum)
6474 min_conflicting_input_opnum = rld[j].opnum;
6478 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6479 we, in fact, found any matching reloads. */
6481 if (j == n_reloads
6482 && max_input_address_opnum <= min_conflicting_input_opnum)
6484 for (j = 0; j < n_reloads; j++)
6485 if (i != j && rld[j].reg_rtx != 0
6486 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6487 && (! conflicting_input
6488 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6489 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6491 rld[i].when_needed = RELOAD_OTHER;
6492 rld[j].in = 0;
6493 reload_spill_index[j] = -1;
6494 transfer_replacements (i, j);
6497 /* If this is now RELOAD_OTHER, look for any reloads that load
6498 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6499 if they were for inputs, RELOAD_OTHER for outputs. Note that
6500 this test is equivalent to looking for reloads for this operand
6501 number. */
6503 if (rld[i].when_needed == RELOAD_OTHER)
6504 for (j = 0; j < n_reloads; j++)
6505 if (rld[j].in != 0
6506 && rld[i].when_needed != RELOAD_OTHER
6507 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6508 rld[i].in))
6509 rld[j].when_needed
6510 = ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
6511 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6512 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6518 /* Output insns to reload values in and out of the chosen reload regs. */
6520 static void
6521 emit_reload_insns (chain)
6522 struct insn_chain *chain;
6524 rtx insn = chain->insn;
6526 register int j;
6527 rtx input_reload_insns[MAX_RECOG_OPERANDS];
6528 rtx other_input_address_reload_insns = 0;
6529 rtx other_input_reload_insns = 0;
6530 rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6531 rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6532 rtx output_reload_insns[MAX_RECOG_OPERANDS];
6533 rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6534 rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6535 rtx operand_reload_insns = 0;
6536 rtx other_operand_reload_insns = 0;
6537 rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6538 rtx following_insn = NEXT_INSN (insn);
6539 rtx before_insn = PREV_INSN (insn);
6540 int special;
6541 /* Values to be put in spill_reg_store are put here first. */
6542 rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6543 HARD_REG_SET reg_reloaded_died;
6545 CLEAR_HARD_REG_SET (reg_reloaded_died);
6547 for (j = 0; j < reload_n_operands; j++)
6548 input_reload_insns[j] = input_address_reload_insns[j]
6549 = inpaddr_address_reload_insns[j]
6550 = output_reload_insns[j] = output_address_reload_insns[j]
6551 = outaddr_address_reload_insns[j]
6552 = other_output_reload_insns[j] = 0;
6554 /* Now output the instructions to copy the data into and out of the
6555 reload registers. Do these in the order that the reloads were reported,
6556 since reloads of base and index registers precede reloads of operands
6557 and the operands may need the base and index registers reloaded. */
6559 for (j = 0; j < n_reloads; j++)
6561 register rtx old;
6562 rtx oldequiv_reg = 0;
6563 rtx this_reload_insn = 0;
6564 int expect_occurrences = 1;
6566 if (rld[j].reg_rtx
6567 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
6568 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
6570 old = (rld[j].in && GET_CODE (rld[j].in) == MEM
6571 ? rld[j].in_reg : rld[j].in);
6573 if (old != 0
6574 /* AUTO_INC reloads need to be handled even if inherited. We got an
6575 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
6576 && (! reload_inherited[j] || (rld[j].out && ! rld[j].out_reg))
6577 && ! rtx_equal_p (rld[j].reg_rtx, old)
6578 && rld[j].reg_rtx != 0)
6580 register rtx reloadreg = rld[j].reg_rtx;
6581 rtx oldequiv = 0;
6582 enum machine_mode mode;
6583 rtx *where;
6585 /* Determine the mode to reload in.
6586 This is very tricky because we have three to choose from.
6587 There is the mode the insn operand wants (rld[J].inmode).
6588 There is the mode of the reload register RELOADREG.
6589 There is the intrinsic mode of the operand, which we could find
6590 by stripping some SUBREGs.
6591 It turns out that RELOADREG's mode is irrelevant:
6592 we can change that arbitrarily.
6594 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6595 then the reload reg may not support QImode moves, so use SImode.
6596 If foo is in memory due to spilling a pseudo reg, this is safe,
6597 because the QImode value is in the least significant part of a
6598 slot big enough for a SImode. If foo is some other sort of
6599 memory reference, then it is impossible to reload this case,
6600 so previous passes had better make sure this never happens.
6602 Then consider a one-word union which has SImode and one of its
6603 members is a float, being fetched as (SUBREG:SF union:SI).
6604 We must fetch that as SFmode because we could be loading into
6605 a float-only register. In this case OLD's mode is correct.
6607 Consider an immediate integer: it has VOIDmode. Here we need
6608 to get a mode from something else.
6610 In some cases, there is a fourth mode, the operand's
6611 containing mode. If the insn specifies a containing mode for
6612 this operand, it overrides all others.
6614 I am not sure whether the algorithm here is always right,
6615 but it does the right things in those cases. */
6617 mode = GET_MODE (old);
6618 if (mode == VOIDmode)
6619 mode = rld[j].inmode;
6621 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6622 /* If we need a secondary register for this operation, see if
6623 the value is already in a register in that class. Don't
6624 do this if the secondary register will be used as a scratch
6625 register. */
6627 if (rld[j].secondary_in_reload >= 0
6628 && rld[j].secondary_in_icode == CODE_FOR_nothing
6629 && optimize)
6630 oldequiv
6631 = find_equiv_reg (old, insn,
6632 rld[rld[j].secondary_in_reload].class,
6633 -1, NULL_PTR, 0, mode);
6634 #endif
6636 /* If reloading from memory, see if there is a register
6637 that already holds the same value. If so, reload from there.
6638 We can pass 0 as the reload_reg_p argument because
6639 any other reload has either already been emitted,
6640 in which case find_equiv_reg will see the reload-insn,
6641 or has yet to be emitted, in which case it doesn't matter
6642 because we will use this equiv reg right away. */
6644 if (oldequiv == 0 && optimize
6645 && (GET_CODE (old) == MEM
6646 || (GET_CODE (old) == REG
6647 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6648 && reg_renumber[REGNO (old)] < 0)))
6649 oldequiv = find_equiv_reg (old, insn, ALL_REGS,
6650 -1, NULL_PTR, 0, mode);
6652 if (oldequiv)
6654 int regno = true_regnum (oldequiv);
6656 /* Don't use OLDEQUIV if any other reload changes it at an
6657 earlier stage of this insn or at this stage. */
6658 if (! reload_reg_free_for_value_p (regno, rld[j].opnum,
6659 rld[j].when_needed,
6660 rld[j].in, const0_rtx, j,
6662 oldequiv = 0;
6664 /* If it is no cheaper to copy from OLDEQUIV into the
6665 reload register than it would be to move from memory,
6666 don't use it. Likewise, if we need a secondary register
6667 or memory. */
6669 if (oldequiv != 0
6670 && ((REGNO_REG_CLASS (regno) != rld[j].class
6671 && (REGISTER_MOVE_COST (REGNO_REG_CLASS (regno),
6672 rld[j].class)
6673 >= MEMORY_MOVE_COST (mode, rld[j].class, 1)))
6674 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6675 || (SECONDARY_INPUT_RELOAD_CLASS (rld[j].class,
6676 mode, oldequiv)
6677 != NO_REGS)
6678 #endif
6679 #ifdef SECONDARY_MEMORY_NEEDED
6680 || SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (regno),
6681 rld[j].class,
6682 mode)
6683 #endif
6685 oldequiv = 0;
6688 /* delete_output_reload is only invoked properly if old contains
6689 the original pseudo register. Since this is replaced with a
6690 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6691 find the pseudo in RELOAD_IN_REG. */
6692 if (oldequiv == 0
6693 && reload_override_in[j]
6694 && GET_CODE (rld[j].in_reg) == REG)
6696 oldequiv = old;
6697 old = rld[j].in_reg;
6699 if (oldequiv == 0)
6700 oldequiv = old;
6701 else if (GET_CODE (oldequiv) == REG)
6702 oldequiv_reg = oldequiv;
6703 else if (GET_CODE (oldequiv) == SUBREG)
6704 oldequiv_reg = SUBREG_REG (oldequiv);
6706 /* If we are reloading from a register that was recently stored in
6707 with an output-reload, see if we can prove there was
6708 actually no need to store the old value in it. */
6710 if (optimize && GET_CODE (oldequiv) == REG
6711 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6712 && spill_reg_store[REGNO (oldequiv)]
6713 && GET_CODE (old) == REG
6714 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6715 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6716 rld[j].out_reg)))
6717 delete_output_reload (insn, j, REGNO (oldequiv));
6719 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6720 then load RELOADREG from OLDEQUIV. Note that we cannot use
6721 gen_lowpart_common since it can do the wrong thing when
6722 RELOADREG has a multi-word mode. Note that RELOADREG
6723 must always be a REG here. */
6725 if (GET_MODE (reloadreg) != mode)
6726 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6727 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6728 oldequiv = SUBREG_REG (oldequiv);
6729 if (GET_MODE (oldequiv) != VOIDmode
6730 && mode != GET_MODE (oldequiv))
6731 oldequiv = gen_rtx_SUBREG (mode, oldequiv, 0);
6733 /* Switch to the right place to emit the reload insns. */
6734 switch (rld[j].when_needed)
6736 case RELOAD_OTHER:
6737 where = &other_input_reload_insns;
6738 break;
6739 case RELOAD_FOR_INPUT:
6740 where = &input_reload_insns[rld[j].opnum];
6741 break;
6742 case RELOAD_FOR_INPUT_ADDRESS:
6743 where = &input_address_reload_insns[rld[j].opnum];
6744 break;
6745 case RELOAD_FOR_INPADDR_ADDRESS:
6746 where = &inpaddr_address_reload_insns[rld[j].opnum];
6747 break;
6748 case RELOAD_FOR_OUTPUT_ADDRESS:
6749 where = &output_address_reload_insns[rld[j].opnum];
6750 break;
6751 case RELOAD_FOR_OUTADDR_ADDRESS:
6752 where = &outaddr_address_reload_insns[rld[j].opnum];
6753 break;
6754 case RELOAD_FOR_OPERAND_ADDRESS:
6755 where = &operand_reload_insns;
6756 break;
6757 case RELOAD_FOR_OPADDR_ADDR:
6758 where = &other_operand_reload_insns;
6759 break;
6760 case RELOAD_FOR_OTHER_ADDRESS:
6761 where = &other_input_address_reload_insns;
6762 break;
6763 default:
6764 abort ();
6767 push_to_sequence (*where);
6768 special = 0;
6770 /* Auto-increment addresses must be reloaded in a special way. */
6771 if (rld[j].out && ! rld[j].out_reg)
6773 /* We are not going to bother supporting the case where a
6774 incremented register can't be copied directly from
6775 OLDEQUIV since this seems highly unlikely. */
6776 if (rld[j].secondary_in_reload >= 0)
6777 abort ();
6779 if (reload_inherited[j])
6780 oldequiv = reloadreg;
6782 old = XEXP (rld[j].in_reg, 0);
6784 if (optimize && GET_CODE (oldequiv) == REG
6785 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6786 && spill_reg_store[REGNO (oldequiv)]
6787 && GET_CODE (old) == REG
6788 && (dead_or_set_p (insn,
6789 spill_reg_stored_to[REGNO (oldequiv)])
6790 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6791 old)))
6792 delete_output_reload (insn, j, REGNO (oldequiv));
6794 /* Prevent normal processing of this reload. */
6795 special = 1;
6796 /* Output a special code sequence for this case. */
6797 new_spill_reg_store[REGNO (reloadreg)]
6798 = inc_for_reload (reloadreg, oldequiv, rld[j].out,
6799 rld[j].inc);
6802 /* If we are reloading a pseudo-register that was set by the previous
6803 insn, see if we can get rid of that pseudo-register entirely
6804 by redirecting the previous insn into our reload register. */
6806 else if (optimize && GET_CODE (old) == REG
6807 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6808 && dead_or_set_p (insn, old)
6809 /* This is unsafe if some other reload
6810 uses the same reg first. */
6811 && reload_reg_free_for_value_p (REGNO (reloadreg),
6812 rld[j].opnum,
6813 rld[j].when_needed,
6814 old, rld[j].out,
6815 j, 0))
6817 rtx temp = PREV_INSN (insn);
6818 while (temp && GET_CODE (temp) == NOTE)
6819 temp = PREV_INSN (temp);
6820 if (temp
6821 && GET_CODE (temp) == INSN
6822 && GET_CODE (PATTERN (temp)) == SET
6823 && SET_DEST (PATTERN (temp)) == old
6824 /* Make sure we can access insn_operand_constraint. */
6825 && asm_noperands (PATTERN (temp)) < 0
6826 /* This is unsafe if prev insn rejects our reload reg. */
6827 && constraint_accepts_reg_p (insn_data[recog_memoized (temp)].operand[0].constraint,
6828 reloadreg)
6829 /* This is unsafe if operand occurs more than once in current
6830 insn. Perhaps some occurrences aren't reloaded. */
6831 && count_occurrences (PATTERN (insn), old) == 1
6832 /* Don't risk splitting a matching pair of operands. */
6833 && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
6835 /* Store into the reload register instead of the pseudo. */
6836 SET_DEST (PATTERN (temp)) = reloadreg;
6838 /* If the previous insn is an output reload, the source is
6839 a reload register, and its spill_reg_store entry will
6840 contain the previous destination. This is now
6841 invalid. */
6842 if (GET_CODE (SET_SRC (PATTERN (temp))) == REG
6843 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6845 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6846 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6849 /* If these are the only uses of the pseudo reg,
6850 pretend for GDB it lives in the reload reg we used. */
6851 if (REG_N_DEATHS (REGNO (old)) == 1
6852 && REG_N_SETS (REGNO (old)) == 1)
6854 reg_renumber[REGNO (old)] = REGNO (rld[j].reg_rtx);
6855 alter_reg (REGNO (old), -1);
6857 special = 1;
6861 /* We can't do that, so output an insn to load RELOADREG. */
6863 if (! special)
6865 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6866 rtx second_reload_reg = 0;
6867 enum insn_code icode;
6869 /* If we have a secondary reload, pick up the secondary register
6870 and icode, if any. If OLDEQUIV and OLD are different or
6871 if this is an in-out reload, recompute whether or not we
6872 still need a secondary register and what the icode should
6873 be. If we still need a secondary register and the class or
6874 icode is different, go back to reloading from OLD if using
6875 OLDEQUIV means that we got the wrong type of register. We
6876 cannot have different class or icode due to an in-out reload
6877 because we don't make such reloads when both the input and
6878 output need secondary reload registers. */
6880 if (rld[j].secondary_in_reload >= 0)
6882 int secondary_reload = rld[j].secondary_in_reload;
6883 rtx real_oldequiv = oldequiv;
6884 rtx real_old = old;
6885 rtx tmp;
6887 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6888 and similarly for OLD.
6889 See comments in get_secondary_reload in reload.c. */
6890 /* If it is a pseudo that cannot be replaced with its
6891 equivalent MEM, we must fall back to reload_in, which
6892 will have all the necessary substitutions registered.
6893 Likewise for a pseudo that can't be replaced with its
6894 equivalent constant.
6896 Take extra care for subregs of such pseudos. Note that
6897 we cannot use reg_equiv_mem in this case because it is
6898 not in the right mode. */
6900 tmp = oldequiv;
6901 if (GET_CODE (tmp) == SUBREG)
6902 tmp = SUBREG_REG (tmp);
6903 if (GET_CODE (tmp) == REG
6904 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6905 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6906 || reg_equiv_constant[REGNO (tmp)] != 0))
6908 if (! reg_equiv_mem[REGNO (tmp)]
6909 || num_not_at_initial_offset
6910 || GET_CODE (oldequiv) == SUBREG)
6911 real_oldequiv = rld[j].in;
6912 else
6913 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6916 tmp = old;
6917 if (GET_CODE (tmp) == SUBREG)
6918 tmp = SUBREG_REG (tmp);
6919 if (GET_CODE (tmp) == REG
6920 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6921 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6922 || reg_equiv_constant[REGNO (tmp)] != 0))
6924 if (! reg_equiv_mem[REGNO (tmp)]
6925 || num_not_at_initial_offset
6926 || GET_CODE (old) == SUBREG)
6927 real_old = rld[j].in;
6928 else
6929 real_old = reg_equiv_mem[REGNO (tmp)];
6932 second_reload_reg = rld[secondary_reload].reg_rtx;
6933 icode = rld[j].secondary_in_icode;
6935 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6936 || (rld[j].in != 0 && rld[j].out != 0))
6938 enum reg_class new_class
6939 = SECONDARY_INPUT_RELOAD_CLASS (rld[j].class,
6940 mode, real_oldequiv);
6942 if (new_class == NO_REGS)
6943 second_reload_reg = 0;
6944 else
6946 enum insn_code new_icode;
6947 enum machine_mode new_mode;
6949 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
6950 REGNO (second_reload_reg)))
6951 oldequiv = old, real_oldequiv = real_old;
6952 else
6954 new_icode = reload_in_optab[(int) mode];
6955 if (new_icode != CODE_FOR_nothing
6956 && ((insn_data[(int) new_icode].operand[0].predicate
6957 && ! ((*insn_data[(int) new_icode].operand[0].predicate)
6958 (reloadreg, mode)))
6959 || (insn_data[(int) new_icode].operand[1].predicate
6960 && ! ((*insn_data[(int) new_icode].operand[1].predicate)
6961 (real_oldequiv, mode)))))
6962 new_icode = CODE_FOR_nothing;
6964 if (new_icode == CODE_FOR_nothing)
6965 new_mode = mode;
6966 else
6967 new_mode = insn_data[(int) new_icode].operand[2].mode;
6969 if (GET_MODE (second_reload_reg) != new_mode)
6971 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6972 new_mode))
6973 oldequiv = old, real_oldequiv = real_old;
6974 else
6975 second_reload_reg
6976 = gen_rtx_REG (new_mode,
6977 REGNO (second_reload_reg));
6983 /* If we still need a secondary reload register, check
6984 to see if it is being used as a scratch or intermediate
6985 register and generate code appropriately. If we need
6986 a scratch register, use REAL_OLDEQUIV since the form of
6987 the insn may depend on the actual address if it is
6988 a MEM. */
6990 if (second_reload_reg)
6992 if (icode != CODE_FOR_nothing)
6994 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6995 second_reload_reg));
6996 special = 1;
6998 else
7000 /* See if we need a scratch register to load the
7001 intermediate register (a tertiary reload). */
7002 enum insn_code tertiary_icode
7003 = rld[secondary_reload].secondary_in_icode;
7005 if (tertiary_icode != CODE_FOR_nothing)
7007 rtx third_reload_reg
7008 = rld[rld[secondary_reload].secondary_in_reload].reg_rtx;
7010 emit_insn ((GEN_FCN (tertiary_icode)
7011 (second_reload_reg, real_oldequiv,
7012 third_reload_reg)));
7014 else
7015 gen_reload (second_reload_reg, real_oldequiv,
7016 rld[j].opnum,
7017 rld[j].when_needed);
7019 oldequiv = second_reload_reg;
7023 #endif
7025 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
7027 rtx real_oldequiv = oldequiv;
7029 if ((GET_CODE (oldequiv) == REG
7030 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
7031 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
7032 || reg_equiv_constant[REGNO (oldequiv)] != 0))
7033 || (GET_CODE (oldequiv) == SUBREG
7034 && GET_CODE (SUBREG_REG (oldequiv)) == REG
7035 && (REGNO (SUBREG_REG (oldequiv))
7036 >= FIRST_PSEUDO_REGISTER)
7037 && ((reg_equiv_memory_loc
7038 [REGNO (SUBREG_REG (oldequiv))] != 0)
7039 || (reg_equiv_constant
7040 [REGNO (SUBREG_REG (oldequiv))] != 0))))
7041 real_oldequiv = rld[j].in;
7042 gen_reload (reloadreg, real_oldequiv, rld[j].opnum,
7043 rld[j].when_needed);
7048 this_reload_insn = get_last_insn ();
7049 /* End this sequence. */
7050 *where = get_insns ();
7051 end_sequence ();
7053 /* Update reload_override_in so that delete_address_reloads_1
7054 can see the actual register usage. */
7055 if (oldequiv_reg)
7056 reload_override_in[j] = oldequiv;
7059 /* When inheriting a wider reload, we have a MEM in rld[j].in,
7060 e.g. inheriting a SImode output reload for
7061 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
7062 if (optimize && reload_inherited[j] && rld[j].in
7063 && GET_CODE (rld[j].in) == MEM
7064 && GET_CODE (rld[j].in_reg) == MEM
7065 && reload_spill_index[j] >= 0
7066 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
7068 expect_occurrences
7069 = count_occurrences (PATTERN (insn), rld[j].in) == 1 ? 0 : -1;
7070 rld[j].in
7071 = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
7074 /* If we are reloading a register that was recently stored in with an
7075 output-reload, see if we can prove there was
7076 actually no need to store the old value in it. */
7078 if (optimize
7079 && (reload_inherited[j] || reload_override_in[j])
7080 && rld[j].reg_rtx
7081 && GET_CODE (rld[j].reg_rtx) == REG
7082 && spill_reg_store[REGNO (rld[j].reg_rtx)] != 0
7083 #if 0
7084 /* There doesn't seem to be any reason to restrict this to pseudos
7085 and doing so loses in the case where we are copying from a
7086 register of the wrong class. */
7087 && (REGNO (spill_reg_stored_to[REGNO (rld[j].reg_rtx)])
7088 >= FIRST_PSEUDO_REGISTER)
7089 #endif
7090 /* The insn might have already some references to stackslots
7091 replaced by MEMs, while reload_out_reg still names the
7092 original pseudo. */
7093 && (dead_or_set_p (insn,
7094 spill_reg_stored_to[REGNO (rld[j].reg_rtx)])
7095 || rtx_equal_p (spill_reg_stored_to[REGNO (rld[j].reg_rtx)],
7096 rld[j].out_reg)))
7097 delete_output_reload (insn, j, REGNO (rld[j].reg_rtx));
7099 /* Input-reloading is done. Now do output-reloading,
7100 storing the value from the reload-register after the main insn
7101 if rld[j].out is nonzero.
7103 ??? At some point we need to support handling output reloads of
7104 JUMP_INSNs or insns that set cc0. */
7106 /* If this is an output reload that stores something that is
7107 not loaded in this same reload, see if we can eliminate a previous
7108 store. */
7110 rtx pseudo = rld[j].out_reg;
7112 if (pseudo
7113 && GET_CODE (pseudo) == REG
7114 && ! rtx_equal_p (rld[j].in_reg, pseudo)
7115 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
7116 && reg_last_reload_reg[REGNO (pseudo)])
7118 int pseudo_no = REGNO (pseudo);
7119 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
7121 /* We don't need to test full validity of last_regno for
7122 inherit here; we only want to know if the store actually
7123 matches the pseudo. */
7124 if (reg_reloaded_contents[last_regno] == pseudo_no
7125 && spill_reg_store[last_regno]
7126 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
7127 delete_output_reload (insn, j, last_regno);
7131 old = rld[j].out_reg;
7132 if (old != 0
7133 && rld[j].reg_rtx != old
7134 && rld[j].reg_rtx != 0)
7136 register rtx reloadreg = rld[j].reg_rtx;
7137 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
7138 register rtx second_reloadreg = 0;
7139 #endif
7140 rtx note, p;
7141 enum machine_mode mode;
7142 int special = 0;
7144 /* An output operand that dies right away does need a reload,
7145 but need not be copied from it. Show the new location in the
7146 REG_UNUSED note. */
7147 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
7148 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
7150 XEXP (note, 0) = rld[j].reg_rtx;
7151 continue;
7153 /* Likewise for a SUBREG of an operand that dies. */
7154 else if (GET_CODE (old) == SUBREG
7155 && GET_CODE (SUBREG_REG (old)) == REG
7156 && 0 != (note = find_reg_note (insn, REG_UNUSED,
7157 SUBREG_REG (old))))
7159 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
7160 rld[j].reg_rtx);
7161 continue;
7163 else if (GET_CODE (old) == SCRATCH)
7164 /* If we aren't optimizing, there won't be a REG_UNUSED note,
7165 but we don't want to make an output reload. */
7166 continue;
7168 #if 0
7169 /* Strip off of OLD any size-increasing SUBREGs such as
7170 (SUBREG:SI foo:QI 0). */
7172 while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
7173 && (GET_MODE_SIZE (GET_MODE (old))
7174 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
7175 old = SUBREG_REG (old);
7176 #endif
7178 /* If is a JUMP_INSN, we can't support output reloads yet. */
7179 if (GET_CODE (insn) == JUMP_INSN)
7180 abort ();
7182 if (rld[j].when_needed == RELOAD_OTHER)
7183 start_sequence ();
7184 else
7185 push_to_sequence (output_reload_insns[rld[j].opnum]);
7187 old = rld[j].out;
7189 /* Determine the mode to reload in.
7190 See comments above (for input reloading). */
7192 mode = GET_MODE (old);
7193 if (mode == VOIDmode)
7195 /* VOIDmode should never happen for an output. */
7196 if (asm_noperands (PATTERN (insn)) < 0)
7197 /* It's the compiler's fault. */
7198 fatal_insn ("VOIDmode on an output", insn);
7199 error_for_asm (insn, "output operand is constant in `asm'");
7200 /* Prevent crash--use something we know is valid. */
7201 mode = word_mode;
7202 old = gen_rtx_REG (mode, REGNO (reloadreg));
7205 if (GET_MODE (reloadreg) != mode)
7206 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
7208 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
7210 /* If we need two reload regs, set RELOADREG to the intermediate
7211 one, since it will be stored into OLD. We might need a secondary
7212 register only for an input reload, so check again here. */
7214 if (rld[j].secondary_out_reload >= 0)
7216 rtx real_old = old;
7218 if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
7219 && reg_equiv_mem[REGNO (old)] != 0)
7220 real_old = reg_equiv_mem[REGNO (old)];
7222 if((SECONDARY_OUTPUT_RELOAD_CLASS (rld[j].class,
7223 mode, real_old)
7224 != NO_REGS))
7226 second_reloadreg = reloadreg;
7227 reloadreg = rld[rld[j].secondary_out_reload].reg_rtx;
7229 /* See if RELOADREG is to be used as a scratch register
7230 or as an intermediate register. */
7231 if (rld[j].secondary_out_icode != CODE_FOR_nothing)
7233 emit_insn ((GEN_FCN (rld[j].secondary_out_icode)
7234 (real_old, second_reloadreg, reloadreg)));
7235 special = 1;
7237 else
7239 /* See if we need both a scratch and intermediate reload
7240 register. */
7242 int secondary_reload = rld[j].secondary_out_reload;
7243 enum insn_code tertiary_icode
7244 = rld[secondary_reload].secondary_out_icode;
7246 if (GET_MODE (reloadreg) != mode)
7247 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
7249 if (tertiary_icode != CODE_FOR_nothing)
7251 rtx third_reloadreg
7252 = rld[rld[secondary_reload].secondary_out_reload].reg_rtx;
7253 rtx tem;
7255 /* Copy primary reload reg to secondary reload reg.
7256 (Note that these have been swapped above, then
7257 secondary reload reg to OLD using our insn. */
7259 /* If REAL_OLD is a paradoxical SUBREG, remove it
7260 and try to put the opposite SUBREG on
7261 RELOADREG. */
7262 if (GET_CODE (real_old) == SUBREG
7263 && (GET_MODE_SIZE (GET_MODE (real_old))
7264 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
7265 && 0 != (tem = gen_lowpart_common
7266 (GET_MODE (SUBREG_REG (real_old)),
7267 reloadreg)))
7268 real_old = SUBREG_REG (real_old), reloadreg = tem;
7270 gen_reload (reloadreg, second_reloadreg,
7271 rld[j].opnum, rld[j].when_needed);
7272 emit_insn ((GEN_FCN (tertiary_icode)
7273 (real_old, reloadreg, third_reloadreg)));
7274 special = 1;
7277 else
7278 /* Copy between the reload regs here and then to
7279 OUT later. */
7281 gen_reload (reloadreg, second_reloadreg,
7282 rld[j].opnum, rld[j].when_needed);
7286 #endif
7288 /* Output the last reload insn. */
7289 if (! special)
7291 rtx set;
7293 /* Don't output the last reload if OLD is not the dest of
7294 INSN and is in the src and is clobbered by INSN. */
7295 if (! flag_expensive_optimizations
7296 || GET_CODE (old) != REG
7297 || !(set = single_set (insn))
7298 || rtx_equal_p (old, SET_DEST (set))
7299 || !reg_mentioned_p (old, SET_SRC (set))
7300 || !regno_clobbered_p (REGNO (old), insn))
7301 gen_reload (old, reloadreg, rld[j].opnum,
7302 rld[j].when_needed);
7305 /* Look at all insns we emitted, just to be safe. */
7306 for (p = get_insns (); p; p = NEXT_INSN (p))
7307 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
7309 rtx pat = PATTERN (p);
7311 /* If this output reload doesn't come from a spill reg,
7312 clear any memory of reloaded copies of the pseudo reg.
7313 If this output reload comes from a spill reg,
7314 reg_has_output_reload will make this do nothing. */
7315 note_stores (pat, forget_old_reloads_1);
7317 if (reg_mentioned_p (rld[j].reg_rtx, pat))
7319 rtx set = single_set (insn);
7320 if (reload_spill_index[j] < 0
7321 && set
7322 && SET_SRC (set) == rld[j].reg_rtx)
7324 int src = REGNO (SET_SRC (set));
7326 reload_spill_index[j] = src;
7327 SET_HARD_REG_BIT (reg_is_output_reload, src);
7328 if (find_regno_note (insn, REG_DEAD, src))
7329 SET_HARD_REG_BIT (reg_reloaded_died, src);
7331 if (REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
7333 int s = rld[j].secondary_out_reload;
7334 set = single_set (p);
7335 /* If this reload copies only to the secondary reload
7336 register, the secondary reload does the actual
7337 store. */
7338 if (s >= 0 && set == NULL_RTX)
7339 ; /* We can't tell what function the secondary reload
7340 has and where the actual store to the pseudo is
7341 made; leave new_spill_reg_store alone. */
7342 else if (s >= 0
7343 && SET_SRC (set) == rld[j].reg_rtx
7344 && SET_DEST (set) == rld[s].reg_rtx)
7346 /* Usually the next instruction will be the
7347 secondary reload insn; if we can confirm
7348 that it is, setting new_spill_reg_store to
7349 that insn will allow an extra optimization. */
7350 rtx s_reg = rld[s].reg_rtx;
7351 rtx next = NEXT_INSN (p);
7352 rld[s].out = rld[j].out;
7353 rld[s].out_reg = rld[j].out_reg;
7354 set = single_set (next);
7355 if (set && SET_SRC (set) == s_reg
7356 && ! new_spill_reg_store[REGNO (s_reg)])
7358 SET_HARD_REG_BIT (reg_is_output_reload,
7359 REGNO (s_reg));
7360 new_spill_reg_store[REGNO (s_reg)] = next;
7363 else
7364 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = p;
7369 if (rld[j].when_needed == RELOAD_OTHER)
7371 emit_insns (other_output_reload_insns[rld[j].opnum]);
7372 other_output_reload_insns[rld[j].opnum] = get_insns ();
7374 else
7375 output_reload_insns[rld[j].opnum] = get_insns ();
7377 end_sequence ();
7381 /* Now write all the insns we made for reloads in the order expected by
7382 the allocation functions. Prior to the insn being reloaded, we write
7383 the following reloads:
7385 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7387 RELOAD_OTHER reloads.
7389 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7390 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7391 RELOAD_FOR_INPUT reload for the operand.
7393 RELOAD_FOR_OPADDR_ADDRS reloads.
7395 RELOAD_FOR_OPERAND_ADDRESS reloads.
7397 After the insn being reloaded, we write the following:
7399 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7400 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7401 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7402 reloads for the operand. The RELOAD_OTHER output reloads are
7403 output in descending order by reload number. */
7405 emit_insns_before (other_input_address_reload_insns, insn);
7406 emit_insns_before (other_input_reload_insns, insn);
7408 for (j = 0; j < reload_n_operands; j++)
7410 emit_insns_before (inpaddr_address_reload_insns[j], insn);
7411 emit_insns_before (input_address_reload_insns[j], insn);
7412 emit_insns_before (input_reload_insns[j], insn);
7415 emit_insns_before (other_operand_reload_insns, insn);
7416 emit_insns_before (operand_reload_insns, insn);
7418 for (j = 0; j < reload_n_operands; j++)
7420 emit_insns_before (outaddr_address_reload_insns[j], following_insn);
7421 emit_insns_before (output_address_reload_insns[j], following_insn);
7422 emit_insns_before (output_reload_insns[j], following_insn);
7423 emit_insns_before (other_output_reload_insns[j], following_insn);
7426 /* Keep basic block info up to date. */
7427 if (n_basic_blocks)
7429 if (BLOCK_HEAD (chain->block) == insn)
7430 BLOCK_HEAD (chain->block) = NEXT_INSN (before_insn);
7431 if (BLOCK_END (chain->block) == insn)
7432 BLOCK_END (chain->block) = PREV_INSN (following_insn);
7435 /* For all the spill regs newly reloaded in this instruction,
7436 record what they were reloaded from, so subsequent instructions
7437 can inherit the reloads.
7439 Update spill_reg_store for the reloads of this insn.
7440 Copy the elements that were updated in the loop above. */
7442 for (j = 0; j < n_reloads; j++)
7444 register int r = reload_order[j];
7445 register int i = reload_spill_index[r];
7447 /* If this is a non-inherited input reload from a pseudo, we must
7448 clear any memory of a previous store to the same pseudo. Only do
7449 something if there will not be an output reload for the pseudo
7450 being reloaded. */
7451 if (rld[r].in_reg != 0
7452 && ! (reload_inherited[r] || reload_override_in[r]))
7454 rtx reg = rld[r].in_reg;
7456 if (GET_CODE (reg) == SUBREG)
7457 reg = SUBREG_REG (reg);
7459 if (GET_CODE (reg) == REG
7460 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7461 && ! reg_has_output_reload[REGNO (reg)])
7463 int nregno = REGNO (reg);
7465 if (reg_last_reload_reg[nregno])
7467 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7469 if (reg_reloaded_contents[last_regno] == nregno)
7470 spill_reg_store[last_regno] = 0;
7475 /* I is nonneg if this reload used a register.
7476 If rld[r].reg_rtx is 0, this is an optional reload
7477 that we opted to ignore. */
7479 if (i >= 0 && rld[r].reg_rtx != 0)
7481 int nr
7482 = HARD_REGNO_NREGS (i, GET_MODE (rld[r].reg_rtx));
7483 int k;
7484 int part_reaches_end = 0;
7485 int all_reaches_end = 1;
7487 /* For a multi register reload, we need to check if all or part
7488 of the value lives to the end. */
7489 for (k = 0; k < nr; k++)
7491 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7492 rld[r].when_needed))
7493 part_reaches_end = 1;
7494 else
7495 all_reaches_end = 0;
7498 /* Ignore reloads that don't reach the end of the insn in
7499 entirety. */
7500 if (all_reaches_end)
7502 /* First, clear out memory of what used to be in this spill reg.
7503 If consecutive registers are used, clear them all. */
7505 for (k = 0; k < nr; k++)
7506 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7508 /* Maybe the spill reg contains a copy of reload_out. */
7509 if (rld[r].out != 0
7510 && (GET_CODE (rld[r].out) == REG
7511 #ifdef AUTO_INC_DEC
7512 || ! rld[r].out_reg
7513 #endif
7514 || GET_CODE (rld[r].out_reg) == REG))
7516 rtx out = (GET_CODE (rld[r].out) == REG
7517 ? rld[r].out
7518 : rld[r].out_reg
7519 ? rld[r].out_reg
7520 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7521 register int nregno = REGNO (out);
7522 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7523 : HARD_REGNO_NREGS (nregno,
7524 GET_MODE (rld[r].reg_rtx)));
7526 spill_reg_store[i] = new_spill_reg_store[i];
7527 spill_reg_stored_to[i] = out;
7528 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7530 /* If NREGNO is a hard register, it may occupy more than
7531 one register. If it does, say what is in the
7532 rest of the registers assuming that both registers
7533 agree on how many words the object takes. If not,
7534 invalidate the subsequent registers. */
7536 if (nregno < FIRST_PSEUDO_REGISTER)
7537 for (k = 1; k < nnr; k++)
7538 reg_last_reload_reg[nregno + k]
7539 = (nr == nnr
7540 ? gen_rtx_REG (reg_raw_mode[REGNO (rld[r].reg_rtx) + k],
7541 REGNO (rld[r].reg_rtx) + k)
7542 : 0);
7544 /* Now do the inverse operation. */
7545 for (k = 0; k < nr; k++)
7547 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7548 reg_reloaded_contents[i + k]
7549 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7550 ? nregno
7551 : nregno + k);
7552 reg_reloaded_insn[i + k] = insn;
7553 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7557 /* Maybe the spill reg contains a copy of reload_in. Only do
7558 something if there will not be an output reload for
7559 the register being reloaded. */
7560 else if (rld[r].out_reg == 0
7561 && rld[r].in != 0
7562 && ((GET_CODE (rld[r].in) == REG
7563 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7564 && ! reg_has_output_reload[REGNO (rld[r].in)])
7565 || (GET_CODE (rld[r].in_reg) == REG
7566 && ! reg_has_output_reload[REGNO (rld[r].in_reg)]))
7567 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7569 register int nregno;
7570 int nnr;
7572 if (GET_CODE (rld[r].in) == REG
7573 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7574 nregno = REGNO (rld[r].in);
7575 else if (GET_CODE (rld[r].in_reg) == REG)
7576 nregno = REGNO (rld[r].in_reg);
7577 else
7578 nregno = REGNO (XEXP (rld[r].in_reg, 0));
7580 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7581 : HARD_REGNO_NREGS (nregno,
7582 GET_MODE (rld[r].reg_rtx)));
7584 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7586 if (nregno < FIRST_PSEUDO_REGISTER)
7587 for (k = 1; k < nnr; k++)
7588 reg_last_reload_reg[nregno + k]
7589 = (nr == nnr
7590 ? gen_rtx_REG (reg_raw_mode[REGNO (rld[r].reg_rtx) + k],
7591 REGNO (rld[r].reg_rtx) + k)
7592 : 0);
7594 /* Unless we inherited this reload, show we haven't
7595 recently done a store.
7596 Previous stores of inherited auto_inc expressions
7597 also have to be discarded. */
7598 if (! reload_inherited[r]
7599 || (rld[r].out && ! rld[r].out_reg))
7600 spill_reg_store[i] = 0;
7602 for (k = 0; k < nr; k++)
7604 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7605 reg_reloaded_contents[i + k]
7606 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7607 ? nregno
7608 : nregno + k);
7609 reg_reloaded_insn[i + k] = insn;
7610 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7615 /* However, if part of the reload reaches the end, then we must
7616 invalidate the old info for the part that survives to the end. */
7617 else if (part_reaches_end)
7619 for (k = 0; k < nr; k++)
7620 if (reload_reg_reaches_end_p (i + k,
7621 rld[r].opnum,
7622 rld[r].when_needed))
7623 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7627 /* The following if-statement was #if 0'd in 1.34 (or before...).
7628 It's reenabled in 1.35 because supposedly nothing else
7629 deals with this problem. */
7631 /* If a register gets output-reloaded from a non-spill register,
7632 that invalidates any previous reloaded copy of it.
7633 But forget_old_reloads_1 won't get to see it, because
7634 it thinks only about the original insn. So invalidate it here. */
7635 if (i < 0 && rld[r].out != 0
7636 && (GET_CODE (rld[r].out) == REG
7637 || (GET_CODE (rld[r].out) == MEM
7638 && GET_CODE (rld[r].out_reg) == REG)))
7640 rtx out = (GET_CODE (rld[r].out) == REG
7641 ? rld[r].out : rld[r].out_reg);
7642 register int nregno = REGNO (out);
7643 if (nregno >= FIRST_PSEUDO_REGISTER)
7645 rtx src_reg, store_insn = NULL_RTX;
7647 reg_last_reload_reg[nregno] = 0;
7649 /* If we can find a hard register that is stored, record
7650 the storing insn so that we may delete this insn with
7651 delete_output_reload. */
7652 src_reg = rld[r].reg_rtx;
7654 /* If this is an optional reload, try to find the source reg
7655 from an input reload. */
7656 if (! src_reg)
7658 rtx set = single_set (insn);
7659 if (set && SET_DEST (set) == rld[r].out)
7661 int k;
7663 src_reg = SET_SRC (set);
7664 store_insn = insn;
7665 for (k = 0; k < n_reloads; k++)
7667 if (rld[k].in == src_reg)
7669 src_reg = rld[k].reg_rtx;
7670 break;
7675 else
7676 store_insn = new_spill_reg_store[REGNO (src_reg)];
7677 if (src_reg && GET_CODE (src_reg) == REG
7678 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7680 int src_regno = REGNO (src_reg);
7681 int nr = HARD_REGNO_NREGS (src_regno, rld[r].mode);
7682 /* The place where to find a death note varies with
7683 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7684 necessarily checked exactly in the code that moves
7685 notes, so just check both locations. */
7686 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7687 if (! note)
7688 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7689 while (nr-- > 0)
7691 spill_reg_store[src_regno + nr] = store_insn;
7692 spill_reg_stored_to[src_regno + nr] = out;
7693 reg_reloaded_contents[src_regno + nr] = nregno;
7694 reg_reloaded_insn[src_regno + nr] = store_insn;
7695 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7696 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7697 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7698 if (note)
7699 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7700 else
7701 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7703 reg_last_reload_reg[nregno] = src_reg;
7706 else
7708 int num_regs = HARD_REGNO_NREGS (nregno,GET_MODE (rld[r].out));
7710 while (num_regs-- > 0)
7711 reg_last_reload_reg[nregno + num_regs] = 0;
7715 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7718 /* Emit code to perform a reload from IN (which may be a reload register) to
7719 OUT (which may also be a reload register). IN or OUT is from operand
7720 OPNUM with reload type TYPE.
7722 Returns first insn emitted. */
7725 gen_reload (out, in, opnum, type)
7726 rtx out;
7727 rtx in;
7728 int opnum;
7729 enum reload_type type;
7731 rtx last = get_last_insn ();
7732 rtx tem;
7734 /* If IN is a paradoxical SUBREG, remove it and try to put the
7735 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7736 if (GET_CODE (in) == SUBREG
7737 && (GET_MODE_SIZE (GET_MODE (in))
7738 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7739 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7740 in = SUBREG_REG (in), out = tem;
7741 else if (GET_CODE (out) == SUBREG
7742 && (GET_MODE_SIZE (GET_MODE (out))
7743 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7744 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7745 out = SUBREG_REG (out), in = tem;
7747 /* How to do this reload can get quite tricky. Normally, we are being
7748 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7749 register that didn't get a hard register. In that case we can just
7750 call emit_move_insn.
7752 We can also be asked to reload a PLUS that adds a register or a MEM to
7753 another register, constant or MEM. This can occur during frame pointer
7754 elimination and while reloading addresses. This case is handled by
7755 trying to emit a single insn to perform the add. If it is not valid,
7756 we use a two insn sequence.
7758 Finally, we could be called to handle an 'o' constraint by putting
7759 an address into a register. In that case, we first try to do this
7760 with a named pattern of "reload_load_address". If no such pattern
7761 exists, we just emit a SET insn and hope for the best (it will normally
7762 be valid on machines that use 'o').
7764 This entire process is made complex because reload will never
7765 process the insns we generate here and so we must ensure that
7766 they will fit their constraints and also by the fact that parts of
7767 IN might be being reloaded separately and replaced with spill registers.
7768 Because of this, we are, in some sense, just guessing the right approach
7769 here. The one listed above seems to work.
7771 ??? At some point, this whole thing needs to be rethought. */
7773 if (GET_CODE (in) == PLUS
7774 && (GET_CODE (XEXP (in, 0)) == REG
7775 || GET_CODE (XEXP (in, 0)) == SUBREG
7776 || GET_CODE (XEXP (in, 0)) == MEM)
7777 && (GET_CODE (XEXP (in, 1)) == REG
7778 || GET_CODE (XEXP (in, 1)) == SUBREG
7779 || CONSTANT_P (XEXP (in, 1))
7780 || GET_CODE (XEXP (in, 1)) == MEM))
7782 /* We need to compute the sum of a register or a MEM and another
7783 register, constant, or MEM, and put it into the reload
7784 register. The best possible way of doing this is if the machine
7785 has a three-operand ADD insn that accepts the required operands.
7787 The simplest approach is to try to generate such an insn and see if it
7788 is recognized and matches its constraints. If so, it can be used.
7790 It might be better not to actually emit the insn unless it is valid,
7791 but we need to pass the insn as an operand to `recog' and
7792 `extract_insn' and it is simpler to emit and then delete the insn if
7793 not valid than to dummy things up. */
7795 rtx op0, op1, tem, insn;
7796 int code;
7798 op0 = find_replacement (&XEXP (in, 0));
7799 op1 = find_replacement (&XEXP (in, 1));
7801 /* Since constraint checking is strict, commutativity won't be
7802 checked, so we need to do that here to avoid spurious failure
7803 if the add instruction is two-address and the second operand
7804 of the add is the same as the reload reg, which is frequently
7805 the case. If the insn would be A = B + A, rearrange it so
7806 it will be A = A + B as constrain_operands expects. */
7808 if (GET_CODE (XEXP (in, 1)) == REG
7809 && REGNO (out) == REGNO (XEXP (in, 1)))
7810 tem = op0, op0 = op1, op1 = tem;
7812 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7813 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7815 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
7816 code = recog_memoized (insn);
7818 if (code >= 0)
7820 extract_insn (insn);
7821 /* We want constrain operands to treat this insn strictly in
7822 its validity determination, i.e., the way it would after reload
7823 has completed. */
7824 if (constrain_operands (1))
7825 return insn;
7828 delete_insns_since (last);
7830 /* If that failed, we must use a conservative two-insn sequence.
7832 Use a move to copy one operand into the reload register. Prefer
7833 to reload a constant, MEM or pseudo since the move patterns can
7834 handle an arbitrary operand. If OP1 is not a constant, MEM or
7835 pseudo and OP1 is not a valid operand for an add instruction, then
7836 reload OP1.
7838 After reloading one of the operands into the reload register, add
7839 the reload register to the output register.
7841 If there is another way to do this for a specific machine, a
7842 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7843 we emit below. */
7845 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7847 if (CONSTANT_P (op1) || GET_CODE (op1) == MEM || GET_CODE (op1) == SUBREG
7848 || (GET_CODE (op1) == REG
7849 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7850 || (code != CODE_FOR_nothing
7851 && ! ((*insn_data[code].operand[2].predicate)
7852 (op1, insn_data[code].operand[2].mode))))
7853 tem = op0, op0 = op1, op1 = tem;
7855 gen_reload (out, op0, opnum, type);
7857 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7858 This fixes a problem on the 32K where the stack pointer cannot
7859 be used as an operand of an add insn. */
7861 if (rtx_equal_p (op0, op1))
7862 op1 = out;
7864 insn = emit_insn (gen_add2_insn (out, op1));
7866 /* If that failed, copy the address register to the reload register.
7867 Then add the constant to the reload register. */
7869 code = recog_memoized (insn);
7871 if (code >= 0)
7873 extract_insn (insn);
7874 /* We want constrain operands to treat this insn strictly in
7875 its validity determination, i.e., the way it would after reload
7876 has completed. */
7877 if (constrain_operands (1))
7879 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7880 REG_NOTES (insn)
7881 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7882 return insn;
7886 delete_insns_since (last);
7888 gen_reload (out, op1, opnum, type);
7889 insn = emit_insn (gen_add2_insn (out, op0));
7890 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7893 #ifdef SECONDARY_MEMORY_NEEDED
7894 /* If we need a memory location to do the move, do it that way. */
7895 else if (GET_CODE (in) == REG && REGNO (in) < FIRST_PSEUDO_REGISTER
7896 && GET_CODE (out) == REG && REGNO (out) < FIRST_PSEUDO_REGISTER
7897 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (in)),
7898 REGNO_REG_CLASS (REGNO (out)),
7899 GET_MODE (out)))
7901 /* Get the memory to use and rewrite both registers to its mode. */
7902 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7904 if (GET_MODE (loc) != GET_MODE (out))
7905 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7907 if (GET_MODE (loc) != GET_MODE (in))
7908 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7910 gen_reload (loc, in, opnum, type);
7911 gen_reload (out, loc, opnum, type);
7913 #endif
7915 /* If IN is a simple operand, use gen_move_insn. */
7916 else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
7917 emit_insn (gen_move_insn (out, in));
7919 #ifdef HAVE_reload_load_address
7920 else if (HAVE_reload_load_address)
7921 emit_insn (gen_reload_load_address (out, in));
7922 #endif
7924 /* Otherwise, just write (set OUT IN) and hope for the best. */
7925 else
7926 emit_insn (gen_rtx_SET (VOIDmode, out, in));
7928 /* Return the first insn emitted.
7929 We can not just return get_last_insn, because there may have
7930 been multiple instructions emitted. Also note that gen_move_insn may
7931 emit more than one insn itself, so we can not assume that there is one
7932 insn emitted per emit_insn_before call. */
7934 return last ? NEXT_INSN (last) : get_insns ();
7937 /* Delete a previously made output-reload
7938 whose result we now believe is not needed.
7939 First we double-check.
7941 INSN is the insn now being processed.
7942 LAST_RELOAD_REG is the hard register number for which we want to delete
7943 the last output reload.
7944 J is the reload-number that originally used REG. The caller has made
7945 certain that reload J doesn't use REG any longer for input. */
7947 static void
7948 delete_output_reload (insn, j, last_reload_reg)
7949 rtx insn;
7950 int j;
7951 int last_reload_reg;
7953 rtx output_reload_insn = spill_reg_store[last_reload_reg];
7954 rtx reg = spill_reg_stored_to[last_reload_reg];
7955 int k;
7956 int n_occurrences;
7957 int n_inherited = 0;
7958 register rtx i1;
7959 rtx substed;
7961 /* Get the raw pseudo-register referred to. */
7963 while (GET_CODE (reg) == SUBREG)
7964 reg = SUBREG_REG (reg);
7965 substed = reg_equiv_memory_loc[REGNO (reg)];
7967 /* This is unsafe if the operand occurs more often in the current
7968 insn than it is inherited. */
7969 for (k = n_reloads - 1; k >= 0; k--)
7971 rtx reg2 = rld[k].in;
7972 if (! reg2)
7973 continue;
7974 if (GET_CODE (reg2) == MEM || reload_override_in[k])
7975 reg2 = rld[k].in_reg;
7976 #ifdef AUTO_INC_DEC
7977 if (rld[k].out && ! rld[k].out_reg)
7978 reg2 = XEXP (rld[k].in_reg, 0);
7979 #endif
7980 while (GET_CODE (reg2) == SUBREG)
7981 reg2 = SUBREG_REG (reg2);
7982 if (rtx_equal_p (reg2, reg))
7984 if (reload_inherited[k] || reload_override_in[k] || k == j)
7986 n_inherited++;
7987 reg2 = rld[k].out_reg;
7988 if (! reg2)
7989 continue;
7990 while (GET_CODE (reg2) == SUBREG)
7991 reg2 = XEXP (reg2, 0);
7992 if (rtx_equal_p (reg2, reg))
7993 n_inherited++;
7995 else
7996 return;
7999 n_occurrences = count_occurrences (PATTERN (insn), reg);
8000 if (substed)
8001 n_occurrences += count_occurrences (PATTERN (insn), substed);
8002 if (n_occurrences > n_inherited)
8003 return;
8005 /* If the pseudo-reg we are reloading is no longer referenced
8006 anywhere between the store into it and here,
8007 and no jumps or labels intervene, then the value can get
8008 here through the reload reg alone.
8009 Otherwise, give up--return. */
8010 for (i1 = NEXT_INSN (output_reload_insn);
8011 i1 != insn; i1 = NEXT_INSN (i1))
8013 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
8014 return;
8015 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
8016 && reg_mentioned_p (reg, PATTERN (i1)))
8018 /* If this is USE in front of INSN, we only have to check that
8019 there are no more references than accounted for by inheritance. */
8020 while (GET_CODE (i1) == INSN && GET_CODE (PATTERN (i1)) == USE)
8022 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
8023 i1 = NEXT_INSN (i1);
8025 if (n_occurrences <= n_inherited && i1 == insn)
8026 break;
8027 return;
8031 /* The caller has already checked that REG dies or is set in INSN.
8032 It has also checked that we are optimizing, and thus some inaccurancies
8033 in the debugging information are acceptable.
8034 So we could just delete output_reload_insn.
8035 But in some cases we can improve the debugging information without
8036 sacrificing optimization - maybe even improving the code:
8037 See if the pseudo reg has been completely replaced
8038 with reload regs. If so, delete the store insn
8039 and forget we had a stack slot for the pseudo. */
8040 if (rld[j].out != rld[j].in
8041 && REG_N_DEATHS (REGNO (reg)) == 1
8042 && REG_N_SETS (REGNO (reg)) == 1
8043 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
8044 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
8046 rtx i2;
8048 /* We know that it was used only between here
8049 and the beginning of the current basic block.
8050 (We also know that the last use before INSN was
8051 the output reload we are thinking of deleting, but never mind that.)
8052 Search that range; see if any ref remains. */
8053 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8055 rtx set = single_set (i2);
8057 /* Uses which just store in the pseudo don't count,
8058 since if they are the only uses, they are dead. */
8059 if (set != 0 && SET_DEST (set) == reg)
8060 continue;
8061 if (GET_CODE (i2) == CODE_LABEL
8062 || GET_CODE (i2) == JUMP_INSN)
8063 break;
8064 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
8065 && reg_mentioned_p (reg, PATTERN (i2)))
8067 /* Some other ref remains; just delete the output reload we
8068 know to be dead. */
8069 delete_address_reloads (output_reload_insn, insn);
8070 PUT_CODE (output_reload_insn, NOTE);
8071 NOTE_SOURCE_FILE (output_reload_insn) = 0;
8072 NOTE_LINE_NUMBER (output_reload_insn) = NOTE_INSN_DELETED;
8073 return;
8077 /* Delete the now-dead stores into this pseudo. */
8078 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8080 rtx set = single_set (i2);
8082 if (set != 0 && SET_DEST (set) == reg)
8084 delete_address_reloads (i2, insn);
8085 /* This might be a basic block head,
8086 thus don't use delete_insn. */
8087 PUT_CODE (i2, NOTE);
8088 NOTE_SOURCE_FILE (i2) = 0;
8089 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
8091 if (GET_CODE (i2) == CODE_LABEL
8092 || GET_CODE (i2) == JUMP_INSN)
8093 break;
8096 /* For the debugging info,
8097 say the pseudo lives in this reload reg. */
8098 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
8099 alter_reg (REGNO (reg), -1);
8101 delete_address_reloads (output_reload_insn, insn);
8102 PUT_CODE (output_reload_insn, NOTE);
8103 NOTE_SOURCE_FILE (output_reload_insn) = 0;
8104 NOTE_LINE_NUMBER (output_reload_insn) = NOTE_INSN_DELETED;
8108 /* We are going to delete DEAD_INSN. Recursively delete loads of
8109 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
8110 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
8111 static void
8112 delete_address_reloads (dead_insn, current_insn)
8113 rtx dead_insn, current_insn;
8115 rtx set = single_set (dead_insn);
8116 rtx set2, dst, prev, next;
8117 if (set)
8119 rtx dst = SET_DEST (set);
8120 if (GET_CODE (dst) == MEM)
8121 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
8123 /* If we deleted the store from a reloaded post_{in,de}c expression,
8124 we can delete the matching adds. */
8125 prev = PREV_INSN (dead_insn);
8126 next = NEXT_INSN (dead_insn);
8127 if (! prev || ! next)
8128 return;
8129 set = single_set (next);
8130 set2 = single_set (prev);
8131 if (! set || ! set2
8132 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
8133 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
8134 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
8135 return;
8136 dst = SET_DEST (set);
8137 if (! rtx_equal_p (dst, SET_DEST (set2))
8138 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
8139 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
8140 || (INTVAL (XEXP (SET_SRC (set), 1))
8141 != - INTVAL (XEXP (SET_SRC (set2), 1))))
8142 return;
8143 delete_insn (prev);
8144 delete_insn (next);
8147 /* Subfunction of delete_address_reloads: process registers found in X. */
8148 static void
8149 delete_address_reloads_1 (dead_insn, x, current_insn)
8150 rtx dead_insn, x, current_insn;
8152 rtx prev, set, dst, i2;
8153 int i, j;
8154 enum rtx_code code = GET_CODE (x);
8156 if (code != REG)
8158 const char *fmt= GET_RTX_FORMAT (code);
8159 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8161 if (fmt[i] == 'e')
8162 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
8163 else if (fmt[i] == 'E')
8165 for (j = XVECLEN (x, i) - 1; j >=0; j--)
8166 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
8167 current_insn);
8170 return;
8173 if (spill_reg_order[REGNO (x)] < 0)
8174 return;
8176 /* Scan backwards for the insn that sets x. This might be a way back due
8177 to inheritance. */
8178 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
8180 code = GET_CODE (prev);
8181 if (code == CODE_LABEL || code == JUMP_INSN)
8182 return;
8183 if (GET_RTX_CLASS (code) != 'i')
8184 continue;
8185 if (reg_set_p (x, PATTERN (prev)))
8186 break;
8187 if (reg_referenced_p (x, PATTERN (prev)))
8188 return;
8190 if (! prev || INSN_UID (prev) < reload_first_uid)
8191 return;
8192 /* Check that PREV only sets the reload register. */
8193 set = single_set (prev);
8194 if (! set)
8195 return;
8196 dst = SET_DEST (set);
8197 if (GET_CODE (dst) != REG
8198 || ! rtx_equal_p (dst, x))
8199 return;
8200 if (! reg_set_p (dst, PATTERN (dead_insn)))
8202 /* Check if DST was used in a later insn -
8203 it might have been inherited. */
8204 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
8206 if (GET_CODE (i2) == CODE_LABEL)
8207 break;
8208 if (GET_RTX_CLASS (GET_CODE (i2)) != 'i')
8209 continue;
8210 if (reg_referenced_p (dst, PATTERN (i2)))
8212 /* If there is a reference to the register in the current insn,
8213 it might be loaded in a non-inherited reload. If no other
8214 reload uses it, that means the register is set before
8215 referenced. */
8216 if (i2 == current_insn)
8218 for (j = n_reloads - 1; j >= 0; j--)
8219 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8220 || reload_override_in[j] == dst)
8221 return;
8222 for (j = n_reloads - 1; j >= 0; j--)
8223 if (rld[j].in && rld[j].reg_rtx == dst)
8224 break;
8225 if (j >= 0)
8226 break;
8228 return;
8230 if (GET_CODE (i2) == JUMP_INSN)
8231 break;
8232 /* If DST is still live at CURRENT_INSN, check if it is used for
8233 any reload. Note that even if CURRENT_INSN sets DST, we still
8234 have to check the reloads. */
8235 if (i2 == current_insn)
8237 for (j = n_reloads - 1; j >= 0; j--)
8238 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8239 || reload_override_in[j] == dst)
8240 return;
8241 /* ??? We can't finish the loop here, because dst might be
8242 allocated to a pseudo in this block if no reload in this
8243 block needs any of the clsses containing DST - see
8244 spill_hard_reg. There is no easy way to tell this, so we
8245 have to scan till the end of the basic block. */
8247 if (reg_set_p (dst, PATTERN (i2)))
8248 break;
8251 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
8252 reg_reloaded_contents[REGNO (dst)] = -1;
8253 /* Can't use delete_insn here because PREV might be a basic block head. */
8254 PUT_CODE (prev, NOTE);
8255 NOTE_LINE_NUMBER (prev) = NOTE_INSN_DELETED;
8256 NOTE_SOURCE_FILE (prev) = 0;
8259 /* Output reload-insns to reload VALUE into RELOADREG.
8260 VALUE is an autoincrement or autodecrement RTX whose operand
8261 is a register or memory location;
8262 so reloading involves incrementing that location.
8263 IN is either identical to VALUE, or some cheaper place to reload from.
8265 INC_AMOUNT is the number to increment or decrement by (always positive).
8266 This cannot be deduced from VALUE.
8268 Return the instruction that stores into RELOADREG. */
8270 static rtx
8271 inc_for_reload (reloadreg, in, value, inc_amount)
8272 rtx reloadreg;
8273 rtx in, value;
8274 int inc_amount;
8276 /* REG or MEM to be copied and incremented. */
8277 rtx incloc = XEXP (value, 0);
8278 /* Nonzero if increment after copying. */
8279 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
8280 rtx last;
8281 rtx inc;
8282 rtx add_insn;
8283 int code;
8284 rtx store;
8285 rtx real_in = in == value ? XEXP (in, 0) : in;
8287 /* No hard register is equivalent to this register after
8288 inc/dec operation. If REG_LAST_RELOAD_REG were non-zero,
8289 we could inc/dec that register as well (maybe even using it for
8290 the source), but I'm not sure it's worth worrying about. */
8291 if (GET_CODE (incloc) == REG)
8292 reg_last_reload_reg[REGNO (incloc)] = 0;
8294 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
8295 inc_amount = - inc_amount;
8297 inc = GEN_INT (inc_amount);
8299 /* If this is post-increment, first copy the location to the reload reg. */
8300 if (post && real_in != reloadreg)
8301 emit_insn (gen_move_insn (reloadreg, real_in));
8303 if (in == value)
8305 /* See if we can directly increment INCLOC. Use a method similar to
8306 that in gen_reload. */
8308 last = get_last_insn ();
8309 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
8310 gen_rtx_PLUS (GET_MODE (incloc),
8311 incloc, inc)));
8313 code = recog_memoized (add_insn);
8314 if (code >= 0)
8316 extract_insn (add_insn);
8317 if (constrain_operands (1))
8319 /* If this is a pre-increment and we have incremented the value
8320 where it lives, copy the incremented value to RELOADREG to
8321 be used as an address. */
8323 if (! post)
8324 emit_insn (gen_move_insn (reloadreg, incloc));
8326 return add_insn;
8329 delete_insns_since (last);
8332 /* If couldn't do the increment directly, must increment in RELOADREG.
8333 The way we do this depends on whether this is pre- or post-increment.
8334 For pre-increment, copy INCLOC to the reload register, increment it
8335 there, then save back. */
8337 if (! post)
8339 if (in != reloadreg)
8340 emit_insn (gen_move_insn (reloadreg, real_in));
8341 emit_insn (gen_add2_insn (reloadreg, inc));
8342 store = emit_insn (gen_move_insn (incloc, reloadreg));
8344 else
8346 /* Postincrement.
8347 Because this might be a jump insn or a compare, and because RELOADREG
8348 may not be available after the insn in an input reload, we must do
8349 the incrementation before the insn being reloaded for.
8351 We have already copied IN to RELOADREG. Increment the copy in
8352 RELOADREG, save that back, then decrement RELOADREG so it has
8353 the original value. */
8355 emit_insn (gen_add2_insn (reloadreg, inc));
8356 store = emit_insn (gen_move_insn (incloc, reloadreg));
8357 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
8360 return store;
8363 /* Return 1 if we are certain that the constraint-string STRING allows
8364 the hard register REG. Return 0 if we can't be sure of this. */
8366 static int
8367 constraint_accepts_reg_p (string, reg)
8368 const char *string;
8369 rtx reg;
8371 int value = 0;
8372 int regno = true_regnum (reg);
8373 int c;
8375 /* Initialize for first alternative. */
8376 value = 0;
8377 /* Check that each alternative contains `g' or `r'. */
8378 while (1)
8379 switch (c = *string++)
8381 case 0:
8382 /* If an alternative lacks `g' or `r', we lose. */
8383 return value;
8384 case ',':
8385 /* If an alternative lacks `g' or `r', we lose. */
8386 if (value == 0)
8387 return 0;
8388 /* Initialize for next alternative. */
8389 value = 0;
8390 break;
8391 case 'g':
8392 case 'r':
8393 /* Any general reg wins for this alternative. */
8394 if (TEST_HARD_REG_BIT (reg_class_contents[(int) GENERAL_REGS], regno))
8395 value = 1;
8396 break;
8397 default:
8398 /* Any reg in specified class wins for this alternative. */
8400 enum reg_class class = REG_CLASS_FROM_LETTER (c);
8402 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno))
8403 value = 1;
8408 /* Return the number of places FIND appears within X, but don't count
8409 an occurrence if some SET_DEST is FIND. */
8412 count_occurrences (x, find)
8413 register rtx x, find;
8415 register int i, j;
8416 register enum rtx_code code;
8417 register const char *format_ptr;
8418 int count;
8420 if (x == find)
8421 return 1;
8422 if (x == 0)
8423 return 0;
8425 code = GET_CODE (x);
8427 switch (code)
8429 case REG:
8430 case QUEUED:
8431 case CONST_INT:
8432 case CONST_DOUBLE:
8433 case SYMBOL_REF:
8434 case CODE_LABEL:
8435 case PC:
8436 case CC0:
8437 return 0;
8439 case MEM:
8440 if (GET_CODE (find) == MEM && rtx_equal_p (x, find))
8441 return 1;
8442 break;
8443 case SET:
8444 if (SET_DEST (x) == find)
8445 return count_occurrences (SET_SRC (x), find);
8446 break;
8448 default:
8449 break;
8452 format_ptr = GET_RTX_FORMAT (code);
8453 count = 0;
8455 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8457 switch (*format_ptr++)
8459 case 'e':
8460 count += count_occurrences (XEXP (x, i), find);
8461 break;
8463 case 'E':
8464 if (XVEC (x, i) != NULL)
8466 for (j = 0; j < XVECLEN (x, i); j++)
8467 count += count_occurrences (XVECEXP (x, i, j), find);
8469 break;
8472 return count;
8475 /* This array holds values which are equivalent to a hard register
8476 during reload_cse_regs. Each array element is an EXPR_LIST of
8477 values. Each time a hard register is set, we set the corresponding
8478 array element to the value. Each time a hard register is copied
8479 into memory, we add the memory location to the corresponding array
8480 element. We don't store values or memory addresses with side
8481 effects in this array.
8483 If the value is a CONST_INT, then the mode of the containing
8484 EXPR_LIST is the mode in which that CONST_INT was referenced.
8486 We sometimes clobber a specific entry in a list. In that case, we
8487 just set XEXP (list-entry, 0) to 0. */
8489 static rtx *reg_values;
8491 /* This is a preallocated REG rtx which we use as a temporary in
8492 reload_cse_invalidate_regno, so that we don't need to allocate a
8493 new one each time through a loop in that function. */
8495 static rtx invalidate_regno_rtx;
8497 /* Invalidate any entries in reg_values which depend on REGNO,
8498 including those for REGNO itself. This is called if REGNO is
8499 changing. If CLOBBER is true, then always forget anything we
8500 currently know about REGNO. MODE is the mode of the assignment to
8501 REGNO, which is used to determine how many hard registers are being
8502 changed. If MODE is VOIDmode, then only REGNO is being changed;
8503 this is used when invalidating call clobbered registers across a
8504 call. */
8506 static void
8507 reload_cse_invalidate_regno (regno, mode, clobber)
8508 int regno;
8509 enum machine_mode mode;
8510 int clobber;
8512 int endregno;
8513 register int i;
8515 /* Our callers don't always go through true_regnum; we may see a
8516 pseudo-register here from a CLOBBER or the like. We probably
8517 won't ever see a pseudo-register that has a real register number,
8518 for we check anyhow for safety. */
8519 if (regno >= FIRST_PSEUDO_REGISTER)
8520 regno = reg_renumber[regno];
8521 if (regno < 0)
8522 return;
8524 if (mode == VOIDmode)
8525 endregno = regno + 1;
8526 else
8527 endregno = regno + HARD_REGNO_NREGS (regno, mode);
8529 if (clobber)
8530 for (i = regno; i < endregno; i++)
8531 reg_values[i] = 0;
8533 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8535 rtx x;
8537 for (x = reg_values[i]; x; x = XEXP (x, 1))
8539 if (XEXP (x, 0) != 0
8540 && refers_to_regno_p (regno, endregno, XEXP (x, 0), NULL_PTR))
8542 /* If this is the only entry on the list, clear
8543 reg_values[i]. Otherwise, just clear this entry on
8544 the list. */
8545 if (XEXP (x, 1) == 0 && x == reg_values[i])
8547 reg_values[i] = 0;
8548 break;
8550 XEXP (x, 0) = 0;
8555 /* We must look at earlier registers, in case REGNO is part of a
8556 multi word value but is not the first register. If an earlier
8557 register has a value in a mode which overlaps REGNO, then we must
8558 invalidate that earlier register. Note that we do not need to
8559 check REGNO or later registers (we must not check REGNO itself,
8560 because we would incorrectly conclude that there was a conflict). */
8562 for (i = 0; i < regno; i++)
8564 rtx x;
8566 for (x = reg_values[i]; x; x = XEXP (x, 1))
8568 if (XEXP (x, 0) != 0)
8570 PUT_MODE (invalidate_regno_rtx, GET_MODE (x));
8571 REGNO (invalidate_regno_rtx) = i;
8572 if (refers_to_regno_p (regno, endregno, invalidate_regno_rtx,
8573 NULL_PTR))
8575 reload_cse_invalidate_regno (i, VOIDmode, 1);
8576 break;
8583 /* The memory at address MEM_BASE is being changed.
8584 Return whether this change will invalidate VAL. */
8586 static int
8587 reload_cse_mem_conflict_p (mem_base, val)
8588 rtx mem_base;
8589 rtx val;
8591 enum rtx_code code;
8592 const char *fmt;
8593 int i;
8595 code = GET_CODE (val);
8596 switch (code)
8598 /* Get rid of a few simple cases quickly. */
8599 case REG:
8600 case PC:
8601 case CC0:
8602 case SCRATCH:
8603 case CONST:
8604 case CONST_INT:
8605 case CONST_DOUBLE:
8606 case SYMBOL_REF:
8607 case LABEL_REF:
8608 return 0;
8610 case MEM:
8611 if (GET_MODE (mem_base) == BLKmode
8612 || GET_MODE (val) == BLKmode)
8613 return 1;
8614 if (anti_dependence (val, mem_base))
8615 return 1;
8616 /* The address may contain nested MEMs. */
8617 break;
8619 default:
8620 break;
8623 fmt = GET_RTX_FORMAT (code);
8625 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8627 if (fmt[i] == 'e')
8629 if (reload_cse_mem_conflict_p (mem_base, XEXP (val, i)))
8630 return 1;
8632 else if (fmt[i] == 'E')
8634 int j;
8636 for (j = 0; j < XVECLEN (val, i); j++)
8637 if (reload_cse_mem_conflict_p (mem_base, XVECEXP (val, i, j)))
8638 return 1;
8642 return 0;
8645 /* Invalidate any entries in reg_values which are changed because of a
8646 store to MEM_RTX. If this is called because of a non-const call
8647 instruction, MEM_RTX is (mem:BLK const0_rtx). */
8649 static void
8650 reload_cse_invalidate_mem (mem_rtx)
8651 rtx mem_rtx;
8653 register int i;
8655 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8657 rtx x;
8659 for (x = reg_values[i]; x; x = XEXP (x, 1))
8661 if (XEXP (x, 0) != 0
8662 && reload_cse_mem_conflict_p (mem_rtx, XEXP (x, 0)))
8664 /* If this is the only entry on the list, clear
8665 reg_values[i]. Otherwise, just clear this entry on
8666 the list. */
8667 if (XEXP (x, 1) == 0 && x == reg_values[i])
8669 reg_values[i] = 0;
8670 break;
8672 XEXP (x, 0) = 0;
8678 /* Invalidate DEST, which is being assigned to or clobbered. The
8679 second parameter exists so that this function can be passed to
8680 note_stores; it is ignored. */
8682 static void
8683 reload_cse_invalidate_rtx (dest, ignore)
8684 rtx dest;
8685 rtx ignore ATTRIBUTE_UNUSED;
8687 while (GET_CODE (dest) == STRICT_LOW_PART
8688 || GET_CODE (dest) == SIGN_EXTRACT
8689 || GET_CODE (dest) == ZERO_EXTRACT
8690 || GET_CODE (dest) == SUBREG)
8691 dest = XEXP (dest, 0);
8693 if (GET_CODE (dest) == REG)
8694 reload_cse_invalidate_regno (REGNO (dest), GET_MODE (dest), 1);
8695 else if (GET_CODE (dest) == MEM)
8696 reload_cse_invalidate_mem (dest);
8699 /* Do a very simple CSE pass over the hard registers.
8701 This function detects no-op moves where we happened to assign two
8702 different pseudo-registers to the same hard register, and then
8703 copied one to the other. Reload will generate a useless
8704 instruction copying a register to itself.
8706 This function also detects cases where we load a value from memory
8707 into two different registers, and (if memory is more expensive than
8708 registers) changes it to simply copy the first register into the
8709 second register.
8711 Another optimization is performed that scans the operands of each
8712 instruction to see whether the value is already available in a
8713 hard register. It then replaces the operand with the hard register
8714 if possible, much like an optional reload would. */
8716 static void
8717 reload_cse_regs_1 (first)
8718 rtx first;
8720 char *firstobj;
8721 rtx callmem;
8722 register int i;
8723 rtx insn;
8725 init_alias_analysis ();
8727 reg_values = (rtx *) alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
8728 bzero ((char *)reg_values, FIRST_PSEUDO_REGISTER * sizeof (rtx));
8730 /* Create our EXPR_LIST structures on reload_obstack, so that we can
8731 free them when we are done. */
8732 push_obstacks (&reload_obstack, &reload_obstack);
8733 firstobj = (char *) obstack_alloc (&reload_obstack, 0);
8735 /* We pass this to reload_cse_invalidate_mem to invalidate all of
8736 memory for a non-const call instruction. */
8737 callmem = gen_rtx_MEM (BLKmode, const0_rtx);
8739 /* This is used in reload_cse_invalidate_regno to avoid consing a
8740 new REG in a loop in that function. */
8741 invalidate_regno_rtx = gen_rtx_REG (VOIDmode, 0);
8743 for (insn = first; insn; insn = NEXT_INSN (insn))
8745 rtx body;
8747 if (GET_CODE (insn) == CODE_LABEL)
8749 /* Forget all the register values at a code label. We don't
8750 try to do anything clever around jumps. */
8751 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8752 reg_values[i] = 0;
8754 continue;
8757 #ifdef NON_SAVING_SETJMP
8758 if (NON_SAVING_SETJMP && GET_CODE (insn) == NOTE
8759 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
8761 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8762 reg_values[i] = 0;
8764 continue;
8766 #endif
8768 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
8769 continue;
8771 /* If this is a call instruction, forget anything stored in a
8772 call clobbered register, or, if this is not a const call, in
8773 memory. */
8774 if (GET_CODE (insn) == CALL_INSN)
8776 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8777 if (call_used_regs[i])
8778 reload_cse_invalidate_regno (i, VOIDmode, 1);
8780 if (! CONST_CALL_P (insn))
8781 reload_cse_invalidate_mem (callmem);
8785 /* Forget all the register values at a volatile asm. */
8786 if (GET_CODE (insn) == INSN
8787 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
8788 && MEM_VOLATILE_P (PATTERN (insn)))
8789 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8790 reg_values[i] = 0;
8792 body = PATTERN (insn);
8793 if (GET_CODE (body) == SET)
8795 int count = 0;
8796 if (reload_cse_noop_set_p (body, insn))
8798 /* If this sets the return value of the function, we must keep
8799 a USE around, in case this is in a different basic block
8800 than the final USE. Otherwise, we could loose important
8801 register lifeness information on SMALL_REGISTER_CLASSES
8802 machines, where return registers might be used as spills:
8803 subsequent passes assume that spill registers are dead at
8804 the end of a basic block. */
8805 if (REG_FUNCTION_VALUE_P (SET_DEST (body)))
8807 pop_obstacks ();
8808 PATTERN (insn) = gen_rtx_USE (VOIDmode, SET_DEST (body));
8809 INSN_CODE (insn) = -1;
8810 REG_NOTES (insn) = NULL_RTX;
8811 push_obstacks (&reload_obstack, &reload_obstack);
8813 else
8815 PUT_CODE (insn, NOTE);
8816 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8817 NOTE_SOURCE_FILE (insn) = 0;
8820 /* We're done with this insn. */
8821 continue;
8824 /* It's not a no-op, but we can try to simplify it. */
8825 count += reload_cse_simplify_set (body, insn);
8827 if (count > 0)
8828 apply_change_group ();
8829 else
8830 reload_cse_simplify_operands (insn);
8832 reload_cse_record_set (body, body);
8834 else if (GET_CODE (body) == PARALLEL)
8836 int count = 0;
8837 rtx value = NULL_RTX;
8839 /* If every action in a PARALLEL is a noop, we can delete
8840 the entire PARALLEL. */
8841 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8843 rtx part = XVECEXP (body, 0, i);
8844 if (GET_CODE (part) == SET)
8846 if (! reload_cse_noop_set_p (part, insn))
8847 break;
8848 if (REG_FUNCTION_VALUE_P (SET_DEST (part)))
8850 if (value)
8851 break;
8852 value = SET_DEST (part);
8855 else if (GET_CODE (part) != CLOBBER)
8856 break;
8858 if (i < 0)
8860 if (value)
8862 pop_obstacks ();
8863 PATTERN (insn) = gen_rtx_USE (VOIDmode, value);
8864 INSN_CODE (insn) = -1;
8865 REG_NOTES (insn) = NULL_RTX;
8866 push_obstacks (&reload_obstack, &reload_obstack);
8868 else
8870 PUT_CODE (insn, NOTE);
8871 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8872 NOTE_SOURCE_FILE (insn) = 0;
8875 /* We're done with this insn. */
8876 continue;
8879 /* It's not a no-op, but we can try to simplify it. */
8880 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8881 if (GET_CODE (XVECEXP (body, 0, i)) == SET)
8882 count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
8884 if (count > 0)
8885 apply_change_group ();
8886 else
8887 reload_cse_simplify_operands (insn);
8889 /* Look through the PARALLEL and record the values being
8890 set, if possible. Also handle any CLOBBERs. */
8891 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8893 rtx x = XVECEXP (body, 0, i);
8895 if (GET_CODE (x) == SET)
8896 reload_cse_record_set (x, body);
8897 else
8898 note_stores (x, reload_cse_invalidate_rtx);
8901 else
8902 note_stores (body, reload_cse_invalidate_rtx);
8904 #ifdef AUTO_INC_DEC
8905 /* Clobber any registers which appear in REG_INC notes. We
8906 could keep track of the changes to their values, but it is
8907 unlikely to help. */
8909 rtx x;
8911 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
8912 if (REG_NOTE_KIND (x) == REG_INC)
8913 reload_cse_invalidate_rtx (XEXP (x, 0), NULL_RTX);
8915 #endif
8917 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
8918 after we have processed the insn. */
8919 if (GET_CODE (insn) == CALL_INSN)
8921 rtx x;
8923 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
8924 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
8925 reload_cse_invalidate_rtx (XEXP (XEXP (x, 0), 0), NULL_RTX);
8929 /* Clean up. */
8930 end_alias_analysis ();
8932 /* Free all the temporary structures we created, and go back to the
8933 regular obstacks. */
8934 obstack_free (&reload_obstack, firstobj);
8935 pop_obstacks ();
8938 /* Call cse / combine like post-reload optimization phases.
8939 FIRST is the first instruction. */
8940 void
8941 reload_cse_regs (first)
8942 rtx first;
8944 reload_cse_regs_1 (first);
8945 reload_combine ();
8946 reload_cse_move2add (first);
8947 if (flag_expensive_optimizations)
8948 reload_cse_regs_1 (first);
8951 /* Return whether the values known for REGNO are equal to VAL. MODE
8952 is the mode of the object that VAL is being copied to; this matters
8953 if VAL is a CONST_INT. */
8955 static int
8956 reload_cse_regno_equal_p (regno, val, mode)
8957 int regno;
8958 rtx val;
8959 enum machine_mode mode;
8961 rtx x;
8963 if (val == 0)
8964 return 0;
8966 for (x = reg_values[regno]; x; x = XEXP (x, 1))
8967 if (XEXP (x, 0) != 0
8968 && rtx_equal_p (XEXP (x, 0), val)
8969 && (! flag_float_store || GET_CODE (XEXP (x, 0)) != MEM
8970 || GET_MODE_CLASS (GET_MODE (x)) != MODE_FLOAT)
8971 && (GET_CODE (val) != CONST_INT
8972 || mode == GET_MODE (x)
8973 || (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8974 /* On a big endian machine if the value spans more than
8975 one register then this register holds the high part of
8976 it and we can't use it.
8978 ??? We should also compare with the high part of the
8979 value. */
8980 && !(WORDS_BIG_ENDIAN
8981 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
8982 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
8983 GET_MODE_BITSIZE (GET_MODE (x))))))
8984 return 1;
8986 return 0;
8989 /* See whether a single set is a noop. SET is the set instruction we
8990 are should check, and INSN is the instruction from which it came. */
8992 static int
8993 reload_cse_noop_set_p (set, insn)
8994 rtx set;
8995 rtx insn ATTRIBUTE_UNUSED;
8997 rtx src, dest;
8998 enum machine_mode dest_mode;
8999 int dreg, sreg;
9000 int ret;
9002 src = SET_SRC (set);
9003 dest = SET_DEST (set);
9004 dest_mode = GET_MODE (dest);
9006 if (side_effects_p (src))
9007 return 0;
9009 dreg = true_regnum (dest);
9010 sreg = true_regnum (src);
9012 /* Check for setting a register to itself. In this case, we don't
9013 have to worry about REG_DEAD notes. */
9014 if (dreg >= 0 && dreg == sreg)
9015 return 1;
9017 ret = 0;
9018 if (dreg >= 0)
9020 /* Check for setting a register to itself. */
9021 if (dreg == sreg)
9022 ret = 1;
9024 /* Check for setting a register to a value which we already know
9025 is in the register. */
9026 else if (reload_cse_regno_equal_p (dreg, src, dest_mode))
9027 ret = 1;
9029 /* Check for setting a register DREG to another register SREG
9030 where SREG is equal to a value which is already in DREG. */
9031 else if (sreg >= 0)
9033 rtx x;
9035 for (x = reg_values[sreg]; x; x = XEXP (x, 1))
9037 rtx tmp;
9039 if (XEXP (x, 0) == 0)
9040 continue;
9042 if (dest_mode == GET_MODE (x))
9043 tmp = XEXP (x, 0);
9044 else if (GET_MODE_BITSIZE (dest_mode)
9045 < GET_MODE_BITSIZE (GET_MODE (x)))
9046 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
9047 else
9048 continue;
9050 if (tmp
9051 && reload_cse_regno_equal_p (dreg, tmp, dest_mode))
9053 ret = 1;
9054 break;
9059 else if (GET_CODE (dest) == MEM)
9061 /* Check for storing a register to memory when we know that the
9062 register is equivalent to the memory location. */
9063 if (sreg >= 0
9064 && reload_cse_regno_equal_p (sreg, dest, dest_mode)
9065 && ! side_effects_p (dest))
9066 ret = 1;
9069 return ret;
9072 /* Try to simplify a single SET instruction. SET is the set pattern.
9073 INSN is the instruction it came from.
9074 This function only handles one case: if we set a register to a value
9075 which is not a register, we try to find that value in some other register
9076 and change the set into a register copy. */
9078 static int
9079 reload_cse_simplify_set (set, insn)
9080 rtx set;
9081 rtx insn;
9083 int dreg;
9084 rtx src;
9085 enum machine_mode dest_mode;
9086 enum reg_class dclass;
9087 register int i;
9089 dreg = true_regnum (SET_DEST (set));
9090 if (dreg < 0)
9091 return 0;
9093 src = SET_SRC (set);
9094 if (side_effects_p (src) || true_regnum (src) >= 0)
9095 return 0;
9097 dclass = REGNO_REG_CLASS (dreg);
9099 /* If memory loads are cheaper than register copies, don't change them. */
9100 if (GET_CODE (src) == MEM
9101 && MEMORY_MOVE_COST (GET_MODE (src), dclass, 1) < 2)
9102 return 0;
9104 /* If the constant is cheaper than a register, don't change it. */
9105 if (CONSTANT_P (src)
9106 && rtx_cost (src, SET) < 2)
9107 return 0;
9109 dest_mode = GET_MODE (SET_DEST (set));
9110 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
9112 if (i != dreg
9113 && REGISTER_MOVE_COST (REGNO_REG_CLASS (i), dclass) == 2
9114 && reload_cse_regno_equal_p (i, src, dest_mode))
9116 int validated;
9118 /* Pop back to the real obstacks while changing the insn. */
9119 pop_obstacks ();
9121 validated = validate_change (insn, &SET_SRC (set),
9122 gen_rtx_REG (dest_mode, i), 1);
9124 /* Go back to the obstack we are using for temporary
9125 storage. */
9126 push_obstacks (&reload_obstack, &reload_obstack);
9128 if (validated)
9129 return 1;
9132 return 0;
9135 /* Try to replace operands in INSN with equivalent values that are already
9136 in registers. This can be viewed as optional reloading.
9138 For each non-register operand in the insn, see if any hard regs are
9139 known to be equivalent to that operand. Record the alternatives which
9140 can accept these hard registers. Among all alternatives, select the
9141 ones which are better or equal to the one currently matching, where
9142 "better" is in terms of '?' and '!' constraints. Among the remaining
9143 alternatives, select the one which replaces most operands with
9144 hard registers. */
9146 static int
9147 reload_cse_simplify_operands (insn)
9148 rtx insn;
9150 int i,j;
9152 const char *constraints[MAX_RECOG_OPERANDS];
9154 /* Vector recording how bad an alternative is. */
9155 int *alternative_reject;
9156 /* Vector recording how many registers can be introduced by choosing
9157 this alternative. */
9158 int *alternative_nregs;
9159 /* Array of vectors recording, for each operand and each alternative,
9160 which hard register to substitute, or -1 if the operand should be
9161 left as it is. */
9162 int *op_alt_regno[MAX_RECOG_OPERANDS];
9163 /* Array of alternatives, sorted in order of decreasing desirability. */
9164 int *alternative_order;
9165 rtx reg = gen_rtx_REG (VOIDmode, -1);
9167 extract_insn (insn);
9169 if (recog_data.n_alternatives == 0 || recog_data.n_operands == 0)
9170 return 0;
9172 /* Figure out which alternative currently matches. */
9173 if (! constrain_operands (1))
9174 fatal_insn_not_found (insn);
9176 alternative_reject = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9177 alternative_nregs = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9178 alternative_order = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9179 bzero ((char *)alternative_reject, recog_data.n_alternatives * sizeof (int));
9180 bzero ((char *)alternative_nregs, recog_data.n_alternatives * sizeof (int));
9182 for (i = 0; i < recog_data.n_operands; i++)
9184 enum machine_mode mode;
9185 int regno;
9186 const char *p;
9188 op_alt_regno[i] = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9189 for (j = 0; j < recog_data.n_alternatives; j++)
9190 op_alt_regno[i][j] = -1;
9192 p = constraints[i] = recog_data.constraints[i];
9193 mode = recog_data.operand_mode[i];
9195 /* Add the reject values for each alternative given by the constraints
9196 for this operand. */
9197 j = 0;
9198 while (*p != '\0')
9200 char c = *p++;
9201 if (c == ',')
9202 j++;
9203 else if (c == '?')
9204 alternative_reject[j] += 3;
9205 else if (c == '!')
9206 alternative_reject[j] += 300;
9209 /* We won't change operands which are already registers. We
9210 also don't want to modify output operands. */
9211 regno = true_regnum (recog_data.operand[i]);
9212 if (regno >= 0
9213 || constraints[i][0] == '='
9214 || constraints[i][0] == '+')
9215 continue;
9217 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9219 int class = (int) NO_REGS;
9221 if (! reload_cse_regno_equal_p (regno, recog_data.operand[i], mode))
9222 continue;
9224 REGNO (reg) = regno;
9225 PUT_MODE (reg, mode);
9227 /* We found a register equal to this operand. Now look for all
9228 alternatives that can accept this register and have not been
9229 assigned a register they can use yet. */
9230 j = 0;
9231 p = constraints[i];
9232 for (;;)
9234 char c = *p++;
9236 switch (c)
9238 case '=': case '+': case '?':
9239 case '#': case '&': case '!':
9240 case '*': case '%':
9241 case '0': case '1': case '2': case '3': case '4':
9242 case '5': case '6': case '7': case '8': case '9':
9243 case 'm': case '<': case '>': case 'V': case 'o':
9244 case 'E': case 'F': case 'G': case 'H':
9245 case 's': case 'i': case 'n':
9246 case 'I': case 'J': case 'K': case 'L':
9247 case 'M': case 'N': case 'O': case 'P':
9248 #ifdef EXTRA_CONSTRAINT
9249 case 'Q': case 'R': case 'S': case 'T': case 'U':
9250 #endif
9251 case 'p': case 'X':
9252 /* These don't say anything we care about. */
9253 break;
9255 case 'g': case 'r':
9256 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
9257 break;
9259 default:
9260 class
9261 = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER ((unsigned char)c)];
9262 break;
9264 case ',': case '\0':
9265 /* See if REGNO fits this alternative, and set it up as the
9266 replacement register if we don't have one for this
9267 alternative yet and the operand being replaced is not
9268 a cheap CONST_INT. */
9269 if (op_alt_regno[i][j] == -1
9270 && reg_fits_class_p (reg, class, 0, mode)
9271 && (GET_CODE (recog_data.operand[i]) != CONST_INT
9272 || (rtx_cost (recog_data.operand[i], SET)
9273 > rtx_cost (reg, SET))))
9275 alternative_nregs[j]++;
9276 op_alt_regno[i][j] = regno;
9278 j++;
9279 break;
9282 if (c == '\0')
9283 break;
9288 /* Record all alternatives which are better or equal to the currently
9289 matching one in the alternative_order array. */
9290 for (i = j = 0; i < recog_data.n_alternatives; i++)
9291 if (alternative_reject[i] <= alternative_reject[which_alternative])
9292 alternative_order[j++] = i;
9293 recog_data.n_alternatives = j;
9295 /* Sort it. Given a small number of alternatives, a dumb algorithm
9296 won't hurt too much. */
9297 for (i = 0; i < recog_data.n_alternatives - 1; i++)
9299 int best = i;
9300 int best_reject = alternative_reject[alternative_order[i]];
9301 int best_nregs = alternative_nregs[alternative_order[i]];
9302 int tmp;
9304 for (j = i + 1; j < recog_data.n_alternatives; j++)
9306 int this_reject = alternative_reject[alternative_order[j]];
9307 int this_nregs = alternative_nregs[alternative_order[j]];
9309 if (this_reject < best_reject
9310 || (this_reject == best_reject && this_nregs < best_nregs))
9312 best = j;
9313 best_reject = this_reject;
9314 best_nregs = this_nregs;
9318 tmp = alternative_order[best];
9319 alternative_order[best] = alternative_order[i];
9320 alternative_order[i] = tmp;
9323 /* Substitute the operands as determined by op_alt_regno for the best
9324 alternative. */
9325 j = alternative_order[0];
9327 /* Pop back to the real obstacks while changing the insn. */
9328 pop_obstacks ();
9330 for (i = 0; i < recog_data.n_operands; i++)
9332 enum machine_mode mode = recog_data.operand_mode[i];
9333 if (op_alt_regno[i][j] == -1)
9334 continue;
9336 validate_change (insn, recog_data.operand_loc[i],
9337 gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
9340 for (i = recog_data.n_dups - 1; i >= 0; i--)
9342 int op = recog_data.dup_num[i];
9343 enum machine_mode mode = recog_data.operand_mode[op];
9345 if (op_alt_regno[op][j] == -1)
9346 continue;
9348 validate_change (insn, recog_data.dup_loc[i],
9349 gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
9352 /* Go back to the obstack we are using for temporary
9353 storage. */
9354 push_obstacks (&reload_obstack, &reload_obstack);
9356 return apply_change_group ();
9359 /* These two variables are used to pass information from
9360 reload_cse_record_set to reload_cse_check_clobber. */
9362 static int reload_cse_check_clobbered;
9363 static rtx reload_cse_check_src;
9365 /* See if DEST overlaps with RELOAD_CSE_CHECK_SRC. If it does, set
9366 RELOAD_CSE_CHECK_CLOBBERED. This is called via note_stores. The
9367 second argument, which is passed by note_stores, is ignored. */
9369 static void
9370 reload_cse_check_clobber (dest, ignore)
9371 rtx dest;
9372 rtx ignore ATTRIBUTE_UNUSED;
9374 if (reg_overlap_mentioned_p (dest, reload_cse_check_src))
9375 reload_cse_check_clobbered = 1;
9378 /* Record the result of a SET instruction. SET is the set pattern.
9379 BODY is the pattern of the insn that it came from. */
9381 static void
9382 reload_cse_record_set (set, body)
9383 rtx set;
9384 rtx body;
9386 rtx dest, src, x;
9387 int dreg, sreg;
9388 enum machine_mode dest_mode;
9390 dest = SET_DEST (set);
9391 src = SET_SRC (set);
9392 dreg = true_regnum (dest);
9393 sreg = true_regnum (src);
9394 dest_mode = GET_MODE (dest);
9396 /* Some machines don't define AUTO_INC_DEC, but they still use push
9397 instructions. We need to catch that case here in order to
9398 invalidate the stack pointer correctly. Note that invalidating
9399 the stack pointer is different from invalidating DEST. */
9400 x = dest;
9401 while (GET_CODE (x) == SUBREG
9402 || GET_CODE (x) == ZERO_EXTRACT
9403 || GET_CODE (x) == SIGN_EXTRACT
9404 || GET_CODE (x) == STRICT_LOW_PART)
9405 x = XEXP (x, 0);
9406 if (push_operand (x, GET_MODE (x)))
9408 reload_cse_invalidate_rtx (stack_pointer_rtx, NULL_RTX);
9409 reload_cse_invalidate_rtx (dest, NULL_RTX);
9410 return;
9413 /* We can only handle an assignment to a register, or a store of a
9414 register to a memory location. For other cases, we just clobber
9415 the destination. We also have to just clobber if there are side
9416 effects in SRC or DEST. */
9417 if ((dreg < 0 && GET_CODE (dest) != MEM)
9418 || side_effects_p (src)
9419 || side_effects_p (dest))
9421 reload_cse_invalidate_rtx (dest, NULL_RTX);
9422 return;
9425 #ifdef HAVE_cc0
9426 /* We don't try to handle values involving CC, because it's a pain
9427 to keep track of when they have to be invalidated. */
9428 if (reg_mentioned_p (cc0_rtx, src)
9429 || reg_mentioned_p (cc0_rtx, dest))
9431 reload_cse_invalidate_rtx (dest, NULL_RTX);
9432 return;
9434 #endif
9436 /* If BODY is a PARALLEL, then we need to see whether the source of
9437 SET is clobbered by some other instruction in the PARALLEL. */
9438 if (GET_CODE (body) == PARALLEL)
9440 int i;
9442 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
9444 rtx x;
9446 x = XVECEXP (body, 0, i);
9447 if (x == set)
9448 continue;
9450 reload_cse_check_clobbered = 0;
9451 reload_cse_check_src = src;
9452 note_stores (x, reload_cse_check_clobber);
9453 if (reload_cse_check_clobbered)
9455 reload_cse_invalidate_rtx (dest, NULL_RTX);
9456 return;
9461 if (dreg >= 0)
9463 int i;
9465 /* This is an assignment to a register. Update the value we
9466 have stored for the register. */
9467 if (sreg >= 0)
9469 rtx x;
9471 /* This is a copy from one register to another. Any values
9472 which were valid for SREG are now valid for DREG. If the
9473 mode changes, we use gen_lowpart_common to extract only
9474 the part of the value that is copied. */
9475 reg_values[dreg] = 0;
9476 for (x = reg_values[sreg]; x; x = XEXP (x, 1))
9478 rtx tmp;
9480 if (XEXP (x, 0) == 0)
9481 continue;
9482 if (dest_mode == GET_MODE (XEXP (x, 0)))
9483 tmp = XEXP (x, 0);
9484 else if (GET_MODE_BITSIZE (dest_mode)
9485 > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
9486 continue;
9487 else
9488 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
9489 if (tmp)
9490 reg_values[dreg] = gen_rtx_EXPR_LIST (dest_mode, tmp,
9491 reg_values[dreg]);
9494 else
9495 reg_values[dreg] = gen_rtx_EXPR_LIST (dest_mode, src, NULL_RTX);
9497 /* We've changed DREG, so invalidate any values held by other
9498 registers that depend upon it. */
9499 reload_cse_invalidate_regno (dreg, dest_mode, 0);
9501 /* If this assignment changes more than one hard register,
9502 forget anything we know about the others. */
9503 for (i = 1; i < HARD_REGNO_NREGS (dreg, dest_mode); i++)
9504 reg_values[dreg + i] = 0;
9506 else if (GET_CODE (dest) == MEM)
9508 /* Invalidate conflicting memory locations. */
9509 reload_cse_invalidate_mem (dest);
9511 /* If we're storing a register to memory, add DEST to the list
9512 in REG_VALUES. */
9513 if (sreg >= 0 && ! side_effects_p (dest))
9514 reg_values[sreg] = gen_rtx_EXPR_LIST (dest_mode, dest,
9515 reg_values[sreg]);
9517 else
9519 /* We should have bailed out earlier. */
9520 abort ();
9524 /* If reload couldn't use reg+reg+offset addressing, try to use reg+reg
9525 addressing now.
9526 This code might also be useful when reload gave up on reg+reg addresssing
9527 because of clashes between the return register and INDEX_REG_CLASS. */
9529 /* The maximum number of uses of a register we can keep track of to
9530 replace them with reg+reg addressing. */
9531 #define RELOAD_COMBINE_MAX_USES 6
9533 /* INSN is the insn where a register has ben used, and USEP points to the
9534 location of the register within the rtl. */
9535 struct reg_use { rtx insn, *usep; };
9537 /* If the register is used in some unknown fashion, USE_INDEX is negative.
9538 If it is dead, USE_INDEX is RELOAD_COMBINE_MAX_USES, and STORE_RUID
9539 indicates where it becomes live again.
9540 Otherwise, USE_INDEX is the index of the last encountered use of the
9541 register (which is first among these we have seen since we scan backwards),
9542 OFFSET contains the constant offset that is added to the register in
9543 all encountered uses, and USE_RUID indicates the first encountered, i.e.
9544 last, of these uses.
9545 STORE_RUID is always meaningful if we only want to use a value in a
9546 register in a different place: it denotes the next insn in the insn
9547 stream (i.e. the last ecountered) that sets or clobbers the register. */
9548 static struct
9550 struct reg_use reg_use[RELOAD_COMBINE_MAX_USES];
9551 int use_index;
9552 rtx offset;
9553 int store_ruid;
9554 int use_ruid;
9555 } reg_state[FIRST_PSEUDO_REGISTER];
9557 /* Reverse linear uid. This is increased in reload_combine while scanning
9558 the instructions from last to first. It is used to set last_label_ruid
9559 and the store_ruid / use_ruid fields in reg_state. */
9560 static int reload_combine_ruid;
9562 #define LABEL_LIVE(LABEL) \
9563 (label_live[CODE_LABEL_NUMBER (LABEL) - min_labelno])
9565 static void
9566 reload_combine ()
9568 rtx insn, set;
9569 int first_index_reg = 1, last_index_reg = 0;
9570 int i;
9571 int last_label_ruid;
9572 int min_labelno, n_labels;
9573 HARD_REG_SET ever_live_at_start, *label_live;
9575 /* If reg+reg can be used in offsetable memory adresses, the main chunk of
9576 reload has already used it where appropriate, so there is no use in
9577 trying to generate it now. */
9578 if (double_reg_address_ok && INDEX_REG_CLASS != NO_REGS)
9579 return;
9581 /* To avoid wasting too much time later searching for an index register,
9582 determine the minimum and maximum index register numbers. */
9583 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9585 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i))
9587 if (! last_index_reg)
9588 last_index_reg = i;
9589 first_index_reg = i;
9592 /* If no index register is available, we can quit now. */
9593 if (first_index_reg > last_index_reg)
9594 return;
9596 /* Set up LABEL_LIVE and EVER_LIVE_AT_START. The register lifetime
9597 information is a bit fuzzy immediately after reload, but it's
9598 still good enough to determine which registers are live at a jump
9599 destination. */
9600 min_labelno = get_first_label_num ();
9601 n_labels = max_label_num () - min_labelno;
9602 label_live = (HARD_REG_SET *) xmalloc (n_labels * sizeof (HARD_REG_SET));
9603 CLEAR_HARD_REG_SET (ever_live_at_start);
9604 for (i = n_basic_blocks - 1; i >= 0; i--)
9606 insn = BLOCK_HEAD (i);
9607 if (GET_CODE (insn) == CODE_LABEL)
9609 HARD_REG_SET live;
9611 REG_SET_TO_HARD_REG_SET (live, BASIC_BLOCK (i)->global_live_at_start);
9612 compute_use_by_pseudos (&live, BASIC_BLOCK (i)->global_live_at_start);
9613 COPY_HARD_REG_SET (LABEL_LIVE (insn), live);
9614 IOR_HARD_REG_SET (ever_live_at_start, live);
9618 /* Initialize last_label_ruid, reload_combine_ruid and reg_state. */
9619 last_label_ruid = reload_combine_ruid = 0;
9620 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9622 reg_state[i].store_ruid = reload_combine_ruid;
9623 if (fixed_regs[i])
9624 reg_state[i].use_index = -1;
9625 else
9626 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9629 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
9631 rtx note;
9633 /* We cannot do our optimization across labels. Invalidating all the use
9634 information we have would be costly, so we just note where the label
9635 is and then later disable any optimization that would cross it. */
9636 if (GET_CODE (insn) == CODE_LABEL)
9637 last_label_ruid = reload_combine_ruid;
9638 if (GET_CODE (insn) == BARRIER)
9640 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9641 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9643 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
9644 continue;
9645 reload_combine_ruid++;
9647 /* Look for (set (REGX) (CONST_INT))
9648 (set (REGX) (PLUS (REGX) (REGY)))
9650 ... (MEM (REGX)) ...
9651 and convert it to
9652 (set (REGZ) (CONST_INT))
9654 ... (MEM (PLUS (REGZ) (REGY)))... .
9656 First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
9657 and that we know all uses of REGX before it dies. */
9658 set = single_set (insn);
9659 if (set != NULL_RTX
9660 && GET_CODE (SET_DEST (set)) == REG
9661 && (HARD_REGNO_NREGS (REGNO (SET_DEST (set)),
9662 GET_MODE (SET_DEST (set)))
9663 == 1)
9664 && GET_CODE (SET_SRC (set)) == PLUS
9665 && GET_CODE (XEXP (SET_SRC (set), 1)) == REG
9666 && rtx_equal_p (XEXP (SET_SRC (set), 0), SET_DEST (set))
9667 && last_label_ruid < reg_state[REGNO (SET_DEST (set))].use_ruid)
9669 rtx reg = SET_DEST (set);
9670 rtx plus = SET_SRC (set);
9671 rtx base = XEXP (plus, 1);
9672 rtx prev = prev_nonnote_insn (insn);
9673 rtx prev_set = prev ? single_set (prev) : NULL_RTX;
9674 int regno = REGNO (reg);
9675 rtx const_reg = NULL_RTX;
9676 rtx reg_sum = NULL_RTX;
9678 /* Now, we need an index register.
9679 We'll set index_reg to this index register, const_reg to the
9680 register that is to be loaded with the constant
9681 (denoted as REGZ in the substitution illustration above),
9682 and reg_sum to the register-register that we want to use to
9683 substitute uses of REG (typically in MEMs) with.
9684 First check REG and BASE for being index registers;
9685 we can use them even if they are not dead. */
9686 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], regno)
9687 || TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
9688 REGNO (base)))
9690 const_reg = reg;
9691 reg_sum = plus;
9693 else
9695 /* Otherwise, look for a free index register. Since we have
9696 checked above that neiter REG nor BASE are index registers,
9697 if we find anything at all, it will be different from these
9698 two registers. */
9699 for (i = first_index_reg; i <= last_index_reg; i++)
9701 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i)
9702 && reg_state[i].use_index == RELOAD_COMBINE_MAX_USES
9703 && reg_state[i].store_ruid <= reg_state[regno].use_ruid
9704 && HARD_REGNO_NREGS (i, GET_MODE (reg)) == 1)
9706 rtx index_reg = gen_rtx_REG (GET_MODE (reg), i);
9707 const_reg = index_reg;
9708 reg_sum = gen_rtx_PLUS (GET_MODE (reg), index_reg, base);
9709 break;
9713 /* Check that PREV_SET is indeed (set (REGX) (CONST_INT)) and that
9714 (REGY), i.e. BASE, is not clobbered before the last use we'll
9715 create. */
9716 if (prev_set
9717 && GET_CODE (SET_SRC (prev_set)) == CONST_INT
9718 && rtx_equal_p (SET_DEST (prev_set), reg)
9719 && reg_state[regno].use_index >= 0
9720 && reg_state[REGNO (base)].store_ruid <= reg_state[regno].use_ruid
9721 && reg_sum)
9723 int i;
9725 /* Change destination register and - if necessary - the
9726 constant value in PREV, the constant loading instruction. */
9727 validate_change (prev, &SET_DEST (prev_set), const_reg, 1);
9728 if (reg_state[regno].offset != const0_rtx)
9729 validate_change (prev,
9730 &SET_SRC (prev_set),
9731 GEN_INT (INTVAL (SET_SRC (prev_set))
9732 + INTVAL (reg_state[regno].offset)),
9734 /* Now for every use of REG that we have recorded, replace REG
9735 with REG_SUM. */
9736 for (i = reg_state[regno].use_index;
9737 i < RELOAD_COMBINE_MAX_USES; i++)
9738 validate_change (reg_state[regno].reg_use[i].insn,
9739 reg_state[regno].reg_use[i].usep,
9740 reg_sum, 1);
9742 if (apply_change_group ())
9744 rtx *np;
9746 /* Delete the reg-reg addition. */
9747 PUT_CODE (insn, NOTE);
9748 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9749 NOTE_SOURCE_FILE (insn) = 0;
9751 if (reg_state[regno].offset != const0_rtx)
9753 /* Previous REG_EQUIV / REG_EQUAL notes for PREV
9754 are now invalid. */
9755 for (np = &REG_NOTES (prev); *np; )
9757 if (REG_NOTE_KIND (*np) == REG_EQUAL
9758 || REG_NOTE_KIND (*np) == REG_EQUIV)
9759 *np = XEXP (*np, 1);
9760 else
9761 np = &XEXP (*np, 1);
9764 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
9765 reg_state[REGNO (const_reg)].store_ruid = reload_combine_ruid;
9766 continue;
9770 note_stores (PATTERN (insn), reload_combine_note_store);
9771 if (GET_CODE (insn) == CALL_INSN)
9773 rtx link;
9775 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9777 if (call_used_regs[i])
9779 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9780 reg_state[i].store_ruid = reload_combine_ruid;
9783 for (link = CALL_INSN_FUNCTION_USAGE (insn); link;
9784 link = XEXP (link, 1))
9786 rtx use = XEXP (link, 0);
9787 int regno = REGNO (XEXP (use, 0));
9788 if (GET_CODE (use) == CLOBBER)
9790 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
9791 reg_state[regno].store_ruid = reload_combine_ruid;
9793 else
9794 reg_state[regno].use_index = -1;
9797 if (GET_CODE (insn) == JUMP_INSN && GET_CODE (PATTERN (insn)) != RETURN)
9799 /* Non-spill registers might be used at the call destination in
9800 some unknown fashion, so we have to mark the unknown use. */
9801 HARD_REG_SET *live;
9802 if ((condjump_p (insn) || condjump_in_parallel_p (insn))
9803 && JUMP_LABEL (insn))
9804 live = &LABEL_LIVE (JUMP_LABEL (insn));
9805 else
9806 live = &ever_live_at_start;
9807 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9809 if (TEST_HARD_REG_BIT (*live, i))
9810 reg_state[i].use_index = -1;
9813 reload_combine_note_use (&PATTERN (insn), insn);
9814 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
9816 if (REG_NOTE_KIND (note) == REG_INC
9817 && GET_CODE (XEXP (note, 0)) == REG)
9819 int regno = REGNO (XEXP (note, 0));
9821 reg_state[regno].store_ruid = reload_combine_ruid;
9822 reg_state[regno].use_index = -1;
9826 free (label_live);
9829 /* Check if DST is a register or a subreg of a register; if it is,
9830 update reg_state[regno].store_ruid and reg_state[regno].use_index
9831 accordingly. Called via note_stores from reload_combine. */
9832 static void
9833 reload_combine_note_store (dst, set)
9834 rtx dst, set;
9836 int regno = 0;
9837 int i;
9838 unsigned size = GET_MODE_SIZE (GET_MODE (dst));
9840 if (GET_CODE (dst) == SUBREG)
9842 regno = SUBREG_WORD (dst);
9843 dst = SUBREG_REG (dst);
9845 if (GET_CODE (dst) != REG)
9846 return;
9847 regno += REGNO (dst);
9849 /* note_stores might have stripped a STRICT_LOW_PART, so we have to be
9850 careful with registers / register parts that are not full words.
9852 Similarly for ZERO_EXTRACT and SIGN_EXTRACT. */
9853 if (GET_CODE (set) != SET
9854 || GET_CODE (SET_DEST (set)) == ZERO_EXTRACT
9855 || GET_CODE (SET_DEST (set)) == SIGN_EXTRACT
9856 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
9858 for (i = (size - 1) / UNITS_PER_WORD + regno; i >= regno; i--)
9860 reg_state[i].use_index = -1;
9861 reg_state[i].store_ruid = reload_combine_ruid;
9864 else
9866 for (i = (size - 1) / UNITS_PER_WORD + regno; i >= regno; i--)
9868 reg_state[i].store_ruid = reload_combine_ruid;
9869 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9874 /* XP points to a piece of rtl that has to be checked for any uses of
9875 registers.
9876 *XP is the pattern of INSN, or a part of it.
9877 Called from reload_combine, and recursively by itself. */
9878 static void
9879 reload_combine_note_use (xp, insn)
9880 rtx *xp, insn;
9882 rtx x = *xp;
9883 enum rtx_code code = x->code;
9884 const char *fmt;
9885 int i, j;
9886 rtx offset = const0_rtx; /* For the REG case below. */
9888 switch (code)
9890 case SET:
9891 if (GET_CODE (SET_DEST (x)) == REG)
9893 reload_combine_note_use (&SET_SRC (x), insn);
9894 return;
9896 break;
9898 case CLOBBER:
9899 if (GET_CODE (SET_DEST (x)) == REG)
9900 return;
9901 break;
9903 case PLUS:
9904 /* We are interested in (plus (reg) (const_int)) . */
9905 if (GET_CODE (XEXP (x, 0)) != REG || GET_CODE (XEXP (x, 1)) != CONST_INT)
9906 break;
9907 offset = XEXP (x, 1);
9908 x = XEXP (x, 0);
9909 /* Fall through. */
9910 case REG:
9912 int regno = REGNO (x);
9913 int use_index;
9915 /* Some spurious USEs of pseudo registers might remain.
9916 Just ignore them. */
9917 if (regno >= FIRST_PSEUDO_REGISTER)
9918 return;
9920 /* If this register is already used in some unknown fashion, we
9921 can't do anything.
9922 If we decrement the index from zero to -1, we can't store more
9923 uses, so this register becomes used in an unknown fashion. */
9924 use_index = --reg_state[regno].use_index;
9925 if (use_index < 0)
9926 return;
9928 if (use_index != RELOAD_COMBINE_MAX_USES - 1)
9930 /* We have found another use for a register that is already
9931 used later. Check if the offsets match; if not, mark the
9932 register as used in an unknown fashion. */
9933 if (! rtx_equal_p (offset, reg_state[regno].offset))
9935 reg_state[regno].use_index = -1;
9936 return;
9939 else
9941 /* This is the first use of this register we have seen since we
9942 marked it as dead. */
9943 reg_state[regno].offset = offset;
9944 reg_state[regno].use_ruid = reload_combine_ruid;
9946 reg_state[regno].reg_use[use_index].insn = insn;
9947 reg_state[regno].reg_use[use_index].usep = xp;
9948 return;
9951 default:
9952 break;
9955 /* Recursively process the components of X. */
9956 fmt = GET_RTX_FORMAT (code);
9957 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9959 if (fmt[i] == 'e')
9960 reload_combine_note_use (&XEXP (x, i), insn);
9961 else if (fmt[i] == 'E')
9963 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9964 reload_combine_note_use (&XVECEXP (x, i, j), insn);
9969 /* See if we can reduce the cost of a constant by replacing a move with
9970 an add. */
9971 /* We cannot do our optimization across labels. Invalidating all the
9972 information about register contents we have would be costly, so we
9973 use last_label_luid (local variable of reload_cse_move2add) to note
9974 where the label is and then later disable any optimization that would
9975 cross it.
9976 reg_offset[n] / reg_base_reg[n] / reg_mode[n] are only valid if
9977 reg_set_luid[n] is larger than last_label_luid[n] . */
9978 static int reg_set_luid[FIRST_PSEUDO_REGISTER];
9979 /* reg_offset[n] has to be CONST_INT for it and reg_base_reg[n] /
9980 reg_mode[n] to be valid.
9981 If reg_offset[n] is a CONST_INT and reg_base_reg[n] is negative, register n
9982 has been set to reg_offset[n] in mode reg_mode[n] .
9983 If reg_offset[n] is a CONST_INT and reg_base_reg[n] is non-negative,
9984 register n has been set to the sum of reg_offset[n] and register
9985 reg_base_reg[n], calculated in mode reg_mode[n] . */
9986 static rtx reg_offset[FIRST_PSEUDO_REGISTER];
9987 static int reg_base_reg[FIRST_PSEUDO_REGISTER];
9988 static enum machine_mode reg_mode[FIRST_PSEUDO_REGISTER];
9989 /* move2add_luid is linearily increased while scanning the instructions
9990 from first to last. It is used to set reg_set_luid in
9991 reload_cse_move2add and move2add_note_store. */
9992 static int move2add_luid;
9994 /* Generate a CONST_INT and force it in the range of MODE. */
9995 static rtx
9996 gen_mode_int (mode, value)
9997 enum machine_mode mode;
9998 HOST_WIDE_INT value;
10000 HOST_WIDE_INT cval = value & GET_MODE_MASK (mode);
10001 int width = GET_MODE_BITSIZE (mode);
10003 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative number,
10004 sign extend it. */
10005 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
10006 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
10007 cval |= (HOST_WIDE_INT) -1 << width;
10009 return GEN_INT (cval);
10012 static void
10013 reload_cse_move2add (first)
10014 rtx first;
10016 int i;
10017 rtx insn;
10018 int last_label_luid;
10020 for (i = FIRST_PSEUDO_REGISTER-1; i >= 0; i--)
10021 reg_set_luid[i] = 0;
10023 last_label_luid = 0;
10024 move2add_luid = 1;
10025 for (insn = first; insn; insn = NEXT_INSN (insn), move2add_luid++)
10027 rtx pat, note;
10029 if (GET_CODE (insn) == CODE_LABEL)
10030 last_label_luid = move2add_luid;
10031 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
10032 continue;
10033 pat = PATTERN (insn);
10034 /* For simplicity, we only perform this optimization on
10035 straightforward SETs. */
10036 if (GET_CODE (pat) == SET
10037 && GET_CODE (SET_DEST (pat)) == REG)
10039 rtx reg = SET_DEST (pat);
10040 int regno = REGNO (reg);
10041 rtx src = SET_SRC (pat);
10043 /* Check if we have valid information on the contents of this
10044 register in the mode of REG. */
10045 /* ??? We don't know how zero / sign extension is handled, hence
10046 we can't go from a narrower to a wider mode. */
10047 if (reg_set_luid[regno] > last_label_luid
10048 && (GET_MODE_SIZE (GET_MODE (reg))
10049 <= GET_MODE_SIZE (reg_mode[regno]))
10050 && GET_CODE (reg_offset[regno]) == CONST_INT)
10052 /* Try to transform (set (REGX) (CONST_INT A))
10054 (set (REGX) (CONST_INT B))
10056 (set (REGX) (CONST_INT A))
10058 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
10060 if (GET_CODE (src) == CONST_INT && reg_base_reg[regno] < 0)
10062 int success = 0;
10063 rtx new_src
10064 = gen_mode_int (GET_MODE (reg),
10065 INTVAL (src) - INTVAL (reg_offset[regno]));
10066 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
10067 use (set (reg) (reg)) instead.
10068 We don't delete this insn, nor do we convert it into a
10069 note, to avoid losing register notes or the return
10070 value flag. jump2 already knowns how to get rid of
10071 no-op moves. */
10072 if (new_src == const0_rtx)
10073 success = validate_change (insn, &SET_SRC (pat), reg, 0);
10074 else if (rtx_cost (new_src, PLUS) < rtx_cost (src, SET)
10075 && have_add2_insn (GET_MODE (reg)))
10076 success = validate_change (insn, &PATTERN (insn),
10077 gen_add2_insn (reg, new_src), 0);
10078 reg_set_luid[regno] = move2add_luid;
10079 reg_mode[regno] = GET_MODE (reg);
10080 reg_offset[regno] = src;
10081 continue;
10084 /* Try to transform (set (REGX) (REGY))
10085 (set (REGX) (PLUS (REGX) (CONST_INT A)))
10087 (set (REGX) (REGY))
10088 (set (REGX) (PLUS (REGX) (CONST_INT B)))
10090 (REGX) (REGY))
10091 (set (REGX) (PLUS (REGX) (CONST_INT A)))
10093 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
10094 else if (GET_CODE (src) == REG
10095 && reg_base_reg[regno] == REGNO (src)
10096 && reg_set_luid[regno] > reg_set_luid[REGNO (src)])
10098 rtx next = next_nonnote_insn (insn);
10099 rtx set = NULL_RTX;
10100 if (next)
10101 set = single_set (next);
10102 if (next
10103 && set
10104 && SET_DEST (set) == reg
10105 && GET_CODE (SET_SRC (set)) == PLUS
10106 && XEXP (SET_SRC (set), 0) == reg
10107 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
10109 rtx src3 = XEXP (SET_SRC (set), 1);
10110 rtx new_src
10111 = gen_mode_int (GET_MODE (reg),
10112 INTVAL (src3)
10113 - INTVAL (reg_offset[regno]));
10114 int success = 0;
10116 if (new_src == const0_rtx)
10117 /* See above why we create (set (reg) (reg)) here. */
10118 success
10119 = validate_change (next, &SET_SRC (set), reg, 0);
10120 else if ((rtx_cost (new_src, PLUS)
10121 < 2 + rtx_cost (src3, SET))
10122 && have_add2_insn (GET_MODE (reg)))
10123 success
10124 = validate_change (next, &PATTERN (next),
10125 gen_add2_insn (reg, new_src), 0);
10126 if (success)
10128 /* INSN might be the first insn in a basic block
10129 if the preceding insn is a conditional jump
10130 or a possible-throwing call. */
10131 PUT_CODE (insn, NOTE);
10132 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
10133 NOTE_SOURCE_FILE (insn) = 0;
10135 insn = next;
10136 reg_set_luid[regno] = move2add_luid;
10137 reg_mode[regno] = GET_MODE (reg);
10138 reg_offset[regno] = src3;
10139 continue;
10145 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
10147 if (REG_NOTE_KIND (note) == REG_INC
10148 && GET_CODE (XEXP (note, 0)) == REG)
10150 /* Indicate that this register has been recently written to,
10151 but the exact contents are not available. */
10152 int regno = REGNO (XEXP (note, 0));
10153 if (regno < FIRST_PSEUDO_REGISTER)
10155 reg_set_luid[regno] = move2add_luid;
10156 reg_offset[regno] = note;
10160 note_stores (PATTERN (insn), move2add_note_store);
10161 /* If this is a CALL_INSN, all call used registers are stored with
10162 unknown values. */
10163 if (GET_CODE (insn) == CALL_INSN)
10165 for (i = FIRST_PSEUDO_REGISTER-1; i >= 0; i--)
10167 if (call_used_regs[i])
10169 reg_set_luid[i] = move2add_luid;
10170 reg_offset[i] = insn; /* Invalidate contents. */
10177 /* SET is a SET or CLOBBER that sets DST.
10178 Update reg_set_luid, reg_offset and reg_base_reg accordingly.
10179 Called from reload_cse_move2add via note_stores. */
10180 static void
10181 move2add_note_store (dst, set)
10182 rtx dst, set;
10184 int regno = 0;
10185 int i;
10187 enum machine_mode mode = GET_MODE (dst);
10188 if (GET_CODE (dst) == SUBREG)
10190 regno = SUBREG_WORD (dst);
10191 dst = SUBREG_REG (dst);
10193 if (GET_CODE (dst) != REG)
10194 return;
10196 regno += REGNO (dst);
10198 if (HARD_REGNO_NREGS (regno, mode) == 1 && GET_CODE (set) == SET
10199 && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
10200 && GET_CODE (SET_DEST (set)) != SIGN_EXTRACT
10201 && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
10203 rtx src = SET_SRC (set);
10205 reg_mode[regno] = mode;
10206 switch (GET_CODE (src))
10208 case PLUS:
10210 rtx src0 = XEXP (src, 0);
10211 if (GET_CODE (src0) == REG)
10213 if (REGNO (src0) != regno
10214 || reg_offset[regno] != const0_rtx)
10216 reg_base_reg[regno] = REGNO (src0);
10217 reg_set_luid[regno] = move2add_luid;
10219 reg_offset[regno] = XEXP (src, 1);
10220 break;
10222 reg_set_luid[regno] = move2add_luid;
10223 reg_offset[regno] = set; /* Invalidate contents. */
10224 break;
10227 case REG:
10228 reg_base_reg[regno] = REGNO (SET_SRC (set));
10229 reg_offset[regno] = const0_rtx;
10230 reg_set_luid[regno] = move2add_luid;
10231 break;
10233 default:
10234 reg_base_reg[regno] = -1;
10235 reg_offset[regno] = SET_SRC (set);
10236 reg_set_luid[regno] = move2add_luid;
10237 break;
10240 else
10242 for (i = regno + HARD_REGNO_NREGS (regno, mode) - 1; i >= regno; i--)
10244 /* Indicate that this register has been recently written to,
10245 but the exact contents are not available. */
10246 reg_set_luid[i] = move2add_luid;
10247 reg_offset[i] = dst;
10252 #ifdef AUTO_INC_DEC
10253 static void
10254 add_auto_inc_notes (insn, x)
10255 rtx insn;
10256 rtx x;
10258 enum rtx_code code = GET_CODE (x);
10259 const char *fmt;
10260 int i, j;
10262 if (code == MEM && auto_inc_p (XEXP (x, 0)))
10264 REG_NOTES (insn)
10265 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
10266 return;
10269 /* Scan all the operand sub-expressions. */
10270 fmt = GET_RTX_FORMAT (code);
10271 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10273 if (fmt[i] == 'e')
10274 add_auto_inc_notes (insn, XEXP (x, i));
10275 else if (fmt[i] == 'E')
10276 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10277 add_auto_inc_notes (insn, XVECEXP (x, i, j));
10280 #endif