* optimize.c (initialize_inlined_parameters): Take FN to which the
[official-gcc.git] / gcc / reload1.c
blob214b41355cd76a9806ba2a52730eab40a8e7cc04
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, void *));
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, void *));
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, void *));
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, void *));
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, void *));
450 static void reload_cse_move2add PROTO((rtx));
451 static void move2add_note_store PROTO((rtx, rtx, void *));
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 static void failed_reload PROTO((rtx, int));
458 static int set_reload_reg PROTO((int, int));
459 extern void dump_needs PROTO((struct insn_chain *, FILE *));
461 /* Initialize the reload pass once per compilation. */
463 void
464 init_reload ()
466 register int i;
468 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
469 Set spill_indirect_levels to the number of levels such addressing is
470 permitted, zero if it is not permitted at all. */
472 register rtx tem
473 = gen_rtx_MEM (Pmode,
474 gen_rtx_PLUS (Pmode,
475 gen_rtx_REG (Pmode,
476 LAST_VIRTUAL_REGISTER + 1),
477 GEN_INT (4)));
478 spill_indirect_levels = 0;
480 while (memory_address_p (QImode, tem))
482 spill_indirect_levels++;
483 tem = gen_rtx_MEM (Pmode, tem);
486 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
488 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
489 indirect_symref_ok = memory_address_p (QImode, tem);
491 /* See if reg+reg is a valid (and offsettable) address. */
493 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
495 tem = gen_rtx_PLUS (Pmode,
496 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
497 gen_rtx_REG (Pmode, i));
499 /* This way, we make sure that reg+reg is an offsettable address. */
500 tem = plus_constant (tem, 4);
502 if (memory_address_p (QImode, tem))
504 double_reg_address_ok = 1;
505 break;
509 /* Initialize obstack for our rtl allocation. */
510 gcc_obstack_init (&reload_obstack);
511 reload_startobj = (char *) obstack_alloc (&reload_obstack, 0);
514 /* List of insn chains that are currently unused. */
515 static struct insn_chain *unused_insn_chains = 0;
517 /* Allocate an empty insn_chain structure. */
518 struct insn_chain *
519 new_insn_chain ()
521 struct insn_chain *c;
523 if (unused_insn_chains == 0)
525 c = (struct insn_chain *)
526 obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
527 c->live_before = OBSTACK_ALLOC_REG_SET (&reload_obstack);
528 c->live_after = OBSTACK_ALLOC_REG_SET (&reload_obstack);
530 else
532 c = unused_insn_chains;
533 unused_insn_chains = c->next;
535 c->is_caller_save_insn = 0;
536 c->need_operand_change = 0;
537 c->need_reload = 0;
538 c->need_elim = 0;
539 return c;
542 /* Small utility function to set all regs in hard reg set TO which are
543 allocated to pseudos in regset FROM. */
544 void
545 compute_use_by_pseudos (to, from)
546 HARD_REG_SET *to;
547 regset from;
549 int regno;
550 EXECUTE_IF_SET_IN_REG_SET
551 (from, FIRST_PSEUDO_REGISTER, regno,
553 int r = reg_renumber[regno];
554 int nregs;
555 if (r < 0)
557 /* reload_combine uses the information from
558 BASIC_BLOCK->global_live_at_start, which might still
559 contain registers that have not actually been allocated
560 since they have an equivalence. */
561 if (! reload_completed)
562 abort ();
564 else
566 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (regno));
567 while (nregs-- > 0)
568 SET_HARD_REG_BIT (*to, r + nregs);
573 /* Global variables used by reload and its subroutines. */
575 /* Set during calculate_needs if an insn needs register elimination. */
576 static int something_needs_elimination;
577 /* Set during calculate_needs if an insn needs an operand changed. */
578 int something_needs_operands_changed;
580 /* Nonzero means we couldn't get enough spill regs. */
581 static int failure;
583 /* Main entry point for the reload pass.
585 FIRST is the first insn of the function being compiled.
587 GLOBAL nonzero means we were called from global_alloc
588 and should attempt to reallocate any pseudoregs that we
589 displace from hard regs we will use for reloads.
590 If GLOBAL is zero, we do not have enough information to do that,
591 so any pseudo reg that is spilled must go to the stack.
593 DUMPFILE is the global-reg debugging dump file stream, or 0.
594 If it is nonzero, messages are written to it to describe
595 which registers are seized as reload regs, which pseudo regs
596 are spilled from them, and where the pseudo regs are reallocated to.
598 Return value is nonzero if reload failed
599 and we must not do any more for this function. */
602 reload (first, global, dumpfile)
603 rtx first;
604 int global;
605 FILE *dumpfile;
607 register int i;
608 register rtx insn;
609 register struct elim_table *ep;
611 /* The two pointers used to track the true location of the memory used
612 for label offsets. */
613 char *real_known_ptr = NULL_PTR;
614 int (*real_at_ptr)[NUM_ELIMINABLE_REGS];
616 /* Make sure even insns with volatile mem refs are recognizable. */
617 init_recog ();
619 failure = 0;
621 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
623 /* Make sure that the last insn in the chain
624 is not something that needs reloading. */
625 emit_note (NULL_PTR, NOTE_INSN_DELETED);
627 /* Enable find_equiv_reg to distinguish insns made by reload. */
628 reload_first_uid = get_max_uid ();
630 #ifdef SECONDARY_MEMORY_NEEDED
631 /* Initialize the secondary memory table. */
632 clear_secondary_mem ();
633 #endif
635 /* We don't have a stack slot for any spill reg yet. */
636 bzero ((char *) spill_stack_slot, sizeof spill_stack_slot);
637 bzero ((char *) spill_stack_slot_width, sizeof spill_stack_slot_width);
639 /* Initialize the save area information for caller-save, in case some
640 are needed. */
641 init_save_areas ();
643 /* Compute which hard registers are now in use
644 as homes for pseudo registers.
645 This is done here rather than (eg) in global_alloc
646 because this point is reached even if not optimizing. */
647 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
648 mark_home_live (i);
650 /* A function that receives a nonlocal goto must save all call-saved
651 registers. */
652 if (current_function_has_nonlocal_label)
653 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
655 if (! call_used_regs[i] && ! fixed_regs[i])
656 regs_ever_live[i] = 1;
659 /* Find all the pseudo registers that didn't get hard regs
660 but do have known equivalent constants or memory slots.
661 These include parameters (known equivalent to parameter slots)
662 and cse'd or loop-moved constant memory addresses.
664 Record constant equivalents in reg_equiv_constant
665 so they will be substituted by find_reloads.
666 Record memory equivalents in reg_mem_equiv so they can
667 be substituted eventually by altering the REG-rtx's. */
669 reg_equiv_constant = (rtx *) xcalloc (max_regno, sizeof (rtx));
670 reg_equiv_memory_loc = (rtx *) xcalloc (max_regno, sizeof (rtx));
671 reg_equiv_mem = (rtx *) xcalloc (max_regno, sizeof (rtx));
672 reg_equiv_init = (rtx *) xcalloc (max_regno, sizeof (rtx));
673 reg_equiv_address = (rtx *) xcalloc (max_regno, sizeof (rtx));
674 reg_max_ref_width = (int *) xcalloc (max_regno, sizeof (int));
675 reg_old_renumber = (short *) xcalloc (max_regno, sizeof (short));
676 bcopy ((PTR) reg_renumber, (PTR) reg_old_renumber, max_regno * sizeof (short));
677 pseudo_forbidden_regs
678 = (HARD_REG_SET *) xmalloc (max_regno * sizeof (HARD_REG_SET));
679 pseudo_previous_regs
680 = (HARD_REG_SET *) xcalloc (max_regno, sizeof (HARD_REG_SET));
682 CLEAR_HARD_REG_SET (bad_spill_regs_global);
684 /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
685 Also find all paradoxical subregs and find largest such for each pseudo.
686 On machines with small register classes, record hard registers that
687 are used for user variables. These can never be used for spills.
688 Also look for a "constant" NOTE_INSN_SETJMP. This means that all
689 caller-saved registers must be marked live. */
691 num_eliminable_invariants = 0;
692 for (insn = first; insn; insn = NEXT_INSN (insn))
694 rtx set = single_set (insn);
696 if (GET_CODE (insn) == NOTE && CONST_CALL_P (insn)
697 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
698 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
699 if (! call_used_regs[i])
700 regs_ever_live[i] = 1;
702 if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
704 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
705 if (note
706 #ifdef LEGITIMATE_PIC_OPERAND_P
707 && (! function_invariant_p (XEXP (note, 0))
708 || ! flag_pic
709 || LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))
710 #endif
713 rtx x = XEXP (note, 0);
714 i = REGNO (SET_DEST (set));
715 if (i > LAST_VIRTUAL_REGISTER)
717 if (GET_CODE (x) == MEM)
719 /* If the operand is a PLUS, the MEM may be shared,
720 so make sure we have an unshared copy here. */
721 if (GET_CODE (XEXP (x, 0)) == PLUS)
722 x = copy_rtx (x);
724 reg_equiv_memory_loc[i] = x;
726 else if (function_invariant_p (x))
728 if (GET_CODE (x) == PLUS)
730 /* This is PLUS of frame pointer and a constant,
731 and might be shared. Unshare it. */
732 reg_equiv_constant[i] = copy_rtx (x);
733 num_eliminable_invariants++;
735 else if (x == frame_pointer_rtx
736 || x == arg_pointer_rtx)
738 reg_equiv_constant[i] = x;
739 num_eliminable_invariants++;
741 else if (LEGITIMATE_CONSTANT_P (x))
742 reg_equiv_constant[i] = x;
743 else
744 reg_equiv_memory_loc[i]
745 = force_const_mem (GET_MODE (SET_DEST (set)), x);
747 else
748 continue;
750 /* If this register is being made equivalent to a MEM
751 and the MEM is not SET_SRC, the equivalencing insn
752 is one with the MEM as a SET_DEST and it occurs later.
753 So don't mark this insn now. */
754 if (GET_CODE (x) != MEM
755 || rtx_equal_p (SET_SRC (set), x))
756 reg_equiv_init[i]
757 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[i]);
762 /* If this insn is setting a MEM from a register equivalent to it,
763 this is the equivalencing insn. */
764 else if (set && GET_CODE (SET_DEST (set)) == MEM
765 && GET_CODE (SET_SRC (set)) == REG
766 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
767 && rtx_equal_p (SET_DEST (set),
768 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
769 reg_equiv_init[REGNO (SET_SRC (set))]
770 = gen_rtx_INSN_LIST (VOIDmode, insn,
771 reg_equiv_init[REGNO (SET_SRC (set))]);
773 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
774 scan_paradoxical_subregs (PATTERN (insn));
777 init_elim_table ();
779 num_labels = max_label_num () - get_first_label_num ();
781 /* Allocate the tables used to store offset information at labels. */
782 /* We used to use alloca here, but the size of what it would try to
783 allocate would occasionally cause it to exceed the stack limit and
784 cause a core dump. */
785 real_known_ptr = xmalloc (num_labels);
786 real_at_ptr
787 = (int (*)[NUM_ELIMINABLE_REGS])
788 xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
790 offsets_known_at = real_known_ptr - get_first_label_num ();
791 offsets_at
792 = (int (*)[NUM_ELIMINABLE_REGS]) (real_at_ptr - get_first_label_num ());
794 /* Alter each pseudo-reg rtx to contain its hard reg number.
795 Assign stack slots to the pseudos that lack hard regs or equivalents.
796 Do not touch virtual registers. */
798 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
799 alter_reg (i, -1);
801 /* If we have some registers we think can be eliminated, scan all insns to
802 see if there is an insn that sets one of these registers to something
803 other than itself plus a constant. If so, the register cannot be
804 eliminated. Doing this scan here eliminates an extra pass through the
805 main reload loop in the most common case where register elimination
806 cannot be done. */
807 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
808 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
809 || GET_CODE (insn) == CALL_INSN)
810 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
812 maybe_fix_stack_asms ();
814 insns_need_reload = 0;
815 something_needs_elimination = 0;
817 /* Initialize to -1, which means take the first spill register. */
818 last_spill_reg = -1;
820 spilled_pseudos = ALLOCA_REG_SET ();
822 /* Spill any hard regs that we know we can't eliminate. */
823 CLEAR_HARD_REG_SET (used_spill_regs);
824 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
825 if (! ep->can_eliminate)
826 spill_hard_reg (ep->from, dumpfile, 1);
828 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
829 if (frame_pointer_needed)
830 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, dumpfile, 1);
831 #endif
832 finish_spills (global, dumpfile);
834 /* From now on, we may need to generate moves differently. We may also
835 allow modifications of insns which cause them to not be recognized.
836 Any such modifications will be cleaned up during reload itself. */
837 reload_in_progress = 1;
839 /* This loop scans the entire function each go-round
840 and repeats until one repetition spills no additional hard regs. */
841 for (;;)
843 int something_changed;
844 int did_spill;
845 struct insn_chain *chain;
847 HOST_WIDE_INT starting_frame_size;
849 /* Round size of stack frame to BIGGEST_ALIGNMENT. This must be done
850 here because the stack size may be a part of the offset computation
851 for register elimination, and there might have been new stack slots
852 created in the last iteration of this loop. */
853 assign_stack_local (BLKmode, 0, 0);
855 starting_frame_size = get_frame_size ();
857 set_initial_elim_offsets ();
858 set_initial_label_offsets ();
860 /* For each pseudo register that has an equivalent location defined,
861 try to eliminate any eliminable registers (such as the frame pointer)
862 assuming initial offsets for the replacement register, which
863 is the normal case.
865 If the resulting location is directly addressable, substitute
866 the MEM we just got directly for the old REG.
868 If it is not addressable but is a constant or the sum of a hard reg
869 and constant, it is probably not addressable because the constant is
870 out of range, in that case record the address; we will generate
871 hairy code to compute the address in a register each time it is
872 needed. Similarly if it is a hard register, but one that is not
873 valid as an address register.
875 If the location is not addressable, but does not have one of the
876 above forms, assign a stack slot. We have to do this to avoid the
877 potential of producing lots of reloads if, e.g., a location involves
878 a pseudo that didn't get a hard register and has an equivalent memory
879 location that also involves a pseudo that didn't get a hard register.
881 Perhaps at some point we will improve reload_when_needed handling
882 so this problem goes away. But that's very hairy. */
884 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
885 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
887 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
889 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
890 XEXP (x, 0)))
891 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
892 else if (CONSTANT_P (XEXP (x, 0))
893 || (GET_CODE (XEXP (x, 0)) == REG
894 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
895 || (GET_CODE (XEXP (x, 0)) == PLUS
896 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
897 && (REGNO (XEXP (XEXP (x, 0), 0))
898 < FIRST_PSEUDO_REGISTER)
899 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
900 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
901 else
903 /* Make a new stack slot. Then indicate that something
904 changed so we go back and recompute offsets for
905 eliminable registers because the allocation of memory
906 below might change some offset. reg_equiv_{mem,address}
907 will be set up for this pseudo on the next pass around
908 the loop. */
909 reg_equiv_memory_loc[i] = 0;
910 reg_equiv_init[i] = 0;
911 alter_reg (i, -1);
915 if (caller_save_needed)
916 setup_save_areas ();
918 /* If we allocated another stack slot, redo elimination bookkeeping. */
919 if (starting_frame_size != get_frame_size ())
920 continue;
922 if (caller_save_needed)
924 save_call_clobbered_regs ();
925 /* That might have allocated new insn_chain structures. */
926 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
929 calculate_needs_all_insns (global);
931 CLEAR_REG_SET (spilled_pseudos);
932 did_spill = 0;
934 something_changed = 0;
936 /* If we allocated any new memory locations, make another pass
937 since it might have changed elimination offsets. */
938 if (starting_frame_size != get_frame_size ())
939 something_changed = 1;
942 HARD_REG_SET to_spill;
943 CLEAR_HARD_REG_SET (to_spill);
944 update_eliminables (&to_spill);
945 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
946 if (TEST_HARD_REG_BIT (to_spill, i))
948 spill_hard_reg (i, dumpfile, 1);
949 did_spill = 1;
951 /* Regardless of the state of spills, if we previously had
952 a register that we thought we could eliminate, but no can
953 not eliminate, we must run another pass.
955 Consider pseudos which have an entry in reg_equiv_* which
956 reference an eliminable register. We must make another pass
957 to update reg_equiv_* so that we do not substitute in the
958 old value from when we thought the elimination could be
959 performed. */
960 something_changed = 1;
964 CLEAR_HARD_REG_SET (used_spill_regs);
965 /* Try to satisfy the needs for each insn. */
966 for (chain = insns_need_reload; chain != 0;
967 chain = chain->next_need_reload)
968 find_reload_regs (chain, dumpfile);
970 if (failure)
971 goto failed;
973 if (insns_need_reload != 0 || did_spill)
974 something_changed |= finish_spills (global, dumpfile);
976 if (! something_changed)
977 break;
979 if (caller_save_needed)
980 delete_caller_save_insns ();
983 /* If global-alloc was run, notify it of any register eliminations we have
984 done. */
985 if (global)
986 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
987 if (ep->can_eliminate)
988 mark_elimination (ep->from, ep->to);
990 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
991 If that insn didn't set the register (i.e., it copied the register to
992 memory), just delete that insn instead of the equivalencing insn plus
993 anything now dead. If we call delete_dead_insn on that insn, we may
994 delete the insn that actually sets the register if the register dies
995 there and that is incorrect. */
997 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
999 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1001 rtx list;
1002 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1004 rtx equiv_insn = XEXP (list, 0);
1005 if (GET_CODE (equiv_insn) == NOTE)
1006 continue;
1007 if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1008 delete_dead_insn (equiv_insn);
1009 else
1011 PUT_CODE (equiv_insn, NOTE);
1012 NOTE_SOURCE_FILE (equiv_insn) = 0;
1013 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1019 /* Use the reload registers where necessary
1020 by generating move instructions to move the must-be-register
1021 values into or out of the reload registers. */
1023 if (insns_need_reload != 0 || something_needs_elimination
1024 || something_needs_operands_changed)
1026 int old_frame_size = get_frame_size ();
1028 reload_as_needed (global);
1030 if (old_frame_size != get_frame_size ())
1031 abort ();
1033 if (num_eliminable)
1034 verify_initial_elim_offsets ();
1037 /* If we were able to eliminate the frame pointer, show that it is no
1038 longer live at the start of any basic block. If it ls live by
1039 virtue of being in a pseudo, that pseudo will be marked live
1040 and hence the frame pointer will be known to be live via that
1041 pseudo. */
1043 if (! frame_pointer_needed)
1044 for (i = 0; i < n_basic_blocks; i++)
1045 CLEAR_REGNO_REG_SET (BASIC_BLOCK (i)->global_live_at_start,
1046 HARD_FRAME_POINTER_REGNUM);
1048 /* Come here (with failure set nonzero) if we can't get enough spill regs
1049 and we decide not to abort about it. */
1050 failed:
1052 reload_in_progress = 0;
1054 /* Now eliminate all pseudo regs by modifying them into
1055 their equivalent memory references.
1056 The REG-rtx's for the pseudos are modified in place,
1057 so all insns that used to refer to them now refer to memory.
1059 For a reg that has a reg_equiv_address, all those insns
1060 were changed by reloading so that no insns refer to it any longer;
1061 but the DECL_RTL of a variable decl may refer to it,
1062 and if so this causes the debugging info to mention the variable. */
1064 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1066 rtx addr = 0;
1067 int in_struct = 0;
1068 int is_scalar = 0;
1069 int is_readonly = 0;
1071 if (reg_equiv_memory_loc[i])
1073 in_struct = MEM_IN_STRUCT_P (reg_equiv_memory_loc[i]);
1074 is_scalar = MEM_SCALAR_P (reg_equiv_memory_loc[i]);
1075 is_readonly = RTX_UNCHANGING_P (reg_equiv_memory_loc[i]);
1078 if (reg_equiv_mem[i])
1079 addr = XEXP (reg_equiv_mem[i], 0);
1081 if (reg_equiv_address[i])
1082 addr = reg_equiv_address[i];
1084 if (addr)
1086 if (reg_renumber[i] < 0)
1088 rtx reg = regno_reg_rtx[i];
1089 PUT_CODE (reg, MEM);
1090 XEXP (reg, 0) = addr;
1091 REG_USERVAR_P (reg) = 0;
1092 RTX_UNCHANGING_P (reg) = is_readonly;
1093 MEM_IN_STRUCT_P (reg) = in_struct;
1094 MEM_SCALAR_P (reg) = is_scalar;
1095 /* We have no alias information about this newly created
1096 MEM. */
1097 MEM_ALIAS_SET (reg) = 0;
1099 else if (reg_equiv_mem[i])
1100 XEXP (reg_equiv_mem[i], 0) = addr;
1104 /* We must set reload_completed now since the cleanup_subreg_operands call
1105 below will re-recognize each insn and reload may have generated insns
1106 which are only valid during and after reload. */
1107 reload_completed = 1;
1109 /* Make a pass over all the insns and delete all USEs which we inserted
1110 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1111 notes. Delete all CLOBBER insns that don't refer to the return value
1112 and simplify (subreg (reg)) operands. Also remove all REG_RETVAL and
1113 REG_LIBCALL notes since they are no longer useful or accurate. Strip
1114 and regenerate REG_INC notes that may have been moved around. */
1116 for (insn = first; insn; insn = NEXT_INSN (insn))
1117 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1119 rtx *pnote;
1121 if ((GET_CODE (PATTERN (insn)) == USE
1122 && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1123 || (GET_CODE (PATTERN (insn)) == CLOBBER
1124 && (GET_CODE (XEXP (PATTERN (insn), 0)) != REG
1125 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1127 PUT_CODE (insn, NOTE);
1128 NOTE_SOURCE_FILE (insn) = 0;
1129 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1130 continue;
1133 pnote = &REG_NOTES (insn);
1134 while (*pnote != 0)
1136 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1137 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1138 || REG_NOTE_KIND (*pnote) == REG_INC
1139 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1140 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1141 *pnote = XEXP (*pnote, 1);
1142 else
1143 pnote = &XEXP (*pnote, 1);
1146 #ifdef AUTO_INC_DEC
1147 add_auto_inc_notes (insn, PATTERN (insn));
1148 #endif
1150 /* And simplify (subreg (reg)) if it appears as an operand. */
1151 cleanup_subreg_operands (insn);
1154 /* If we are doing stack checking, give a warning if this function's
1155 frame size is larger than we expect. */
1156 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1158 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1159 static int verbose_warned = 0;
1161 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1162 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1163 size += UNITS_PER_WORD;
1165 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1167 warning ("frame size too large for reliable stack checking");
1168 if (! verbose_warned)
1170 warning ("try reducing the number of local variables");
1171 verbose_warned = 1;
1176 /* Indicate that we no longer have known memory locations or constants. */
1177 if (reg_equiv_constant)
1178 free (reg_equiv_constant);
1179 reg_equiv_constant = 0;
1180 if (reg_equiv_memory_loc)
1181 free (reg_equiv_memory_loc);
1182 reg_equiv_memory_loc = 0;
1184 if (real_known_ptr)
1185 free (real_known_ptr);
1186 if (real_at_ptr)
1187 free (real_at_ptr);
1189 free (reg_equiv_mem);
1190 free (reg_equiv_init);
1191 free (reg_equiv_address);
1192 free (reg_max_ref_width);
1193 free (reg_old_renumber);
1194 free (pseudo_previous_regs);
1195 free (pseudo_forbidden_regs);
1197 FREE_REG_SET (spilled_pseudos);
1199 CLEAR_HARD_REG_SET (used_spill_regs);
1200 for (i = 0; i < n_spills; i++)
1201 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1203 /* Free all the insn_chain structures at once. */
1204 obstack_free (&reload_obstack, reload_startobj);
1205 unused_insn_chains = 0;
1207 return failure;
1210 /* Yet another special case. Unfortunately, reg-stack forces people to
1211 write incorrect clobbers in asm statements. These clobbers must not
1212 cause the register to appear in bad_spill_regs, otherwise we'll call
1213 fatal_insn later. We clear the corresponding regnos in the live
1214 register sets to avoid this.
1215 The whole thing is rather sick, I'm afraid. */
1216 static void
1217 maybe_fix_stack_asms ()
1219 #ifdef STACK_REGS
1220 const char *constraints[MAX_RECOG_OPERANDS];
1221 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1222 struct insn_chain *chain;
1224 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1226 int i, noperands;
1227 HARD_REG_SET clobbered, allowed;
1228 rtx pat;
1230 if (GET_RTX_CLASS (GET_CODE (chain->insn)) != 'i'
1231 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1232 continue;
1233 pat = PATTERN (chain->insn);
1234 if (GET_CODE (pat) != PARALLEL)
1235 continue;
1237 CLEAR_HARD_REG_SET (clobbered);
1238 CLEAR_HARD_REG_SET (allowed);
1240 /* First, make a mask of all stack regs that are clobbered. */
1241 for (i = 0; i < XVECLEN (pat, 0); i++)
1243 rtx t = XVECEXP (pat, 0, i);
1244 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1245 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1248 /* Get the operand values and constraints out of the insn. */
1249 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1250 constraints, operand_mode);
1252 /* For every operand, see what registers are allowed. */
1253 for (i = 0; i < noperands; i++)
1255 const char *p = constraints[i];
1256 /* For every alternative, we compute the class of registers allowed
1257 for reloading in CLS, and merge its contents into the reg set
1258 ALLOWED. */
1259 int cls = (int) NO_REGS;
1261 for (;;)
1263 char c = *p++;
1265 if (c == '\0' || c == ',' || c == '#')
1267 /* End of one alternative - mark the regs in the current
1268 class, and reset the class. */
1269 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1270 cls = NO_REGS;
1271 if (c == '#')
1272 do {
1273 c = *p++;
1274 } while (c != '\0' && c != ',');
1275 if (c == '\0')
1276 break;
1277 continue;
1280 switch (c)
1282 case '=': case '+': case '*': case '%': case '?': case '!':
1283 case '0': case '1': case '2': case '3': case '4': case 'm':
1284 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1285 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1286 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1287 case 'P':
1288 #ifdef EXTRA_CONSTRAINT
1289 case 'Q': case 'R': case 'S': case 'T': case 'U':
1290 #endif
1291 break;
1293 case 'p':
1294 cls = (int) reg_class_subunion[cls][(int) BASE_REG_CLASS];
1295 break;
1297 case 'g':
1298 case 'r':
1299 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1300 break;
1302 default:
1303 cls = (int) reg_class_subunion[cls][(int) REG_CLASS_FROM_LETTER (c)];
1308 /* Those of the registers which are clobbered, but allowed by the
1309 constraints, must be usable as reload registers. So clear them
1310 out of the life information. */
1311 AND_HARD_REG_SET (allowed, clobbered);
1312 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1313 if (TEST_HARD_REG_BIT (allowed, i))
1315 CLEAR_REGNO_REG_SET (chain->live_before, i);
1316 CLEAR_REGNO_REG_SET (chain->live_after, i);
1320 #endif
1324 /* Walk the chain of insns, and determine for each whether it needs reloads
1325 and/or eliminations. Build the corresponding insns_need_reload list, and
1326 set something_needs_elimination as appropriate. */
1327 static void
1328 calculate_needs_all_insns (global)
1329 int global;
1331 struct insn_chain **pprev_reload = &insns_need_reload;
1332 struct insn_chain *chain;
1334 something_needs_elimination = 0;
1336 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1338 rtx insn = chain->insn;
1340 /* Clear out the shortcuts, in case they were set last time through. */
1341 chain->need_elim = 0;
1342 chain->need_reload = 0;
1343 chain->need_operand_change = 0;
1345 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1346 include REG_LABEL), we need to see what effects this has on the
1347 known offsets at labels. */
1349 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
1350 || (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1351 && REG_NOTES (insn) != 0))
1352 set_label_offsets (insn, insn, 0);
1354 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1356 rtx old_body = PATTERN (insn);
1357 int old_code = INSN_CODE (insn);
1358 rtx old_notes = REG_NOTES (insn);
1359 int did_elimination = 0;
1360 int operands_changed = 0;
1361 rtx set = single_set (insn);
1363 /* Skip insns that only set an equivalence. */
1364 if (set && GET_CODE (SET_DEST (set)) == REG
1365 && reg_renumber[REGNO (SET_DEST (set))] < 0
1366 && reg_equiv_constant[REGNO (SET_DEST (set))])
1367 continue;
1369 /* If needed, eliminate any eliminable registers. */
1370 if (num_eliminable || num_eliminable_invariants)
1371 did_elimination = eliminate_regs_in_insn (insn, 0);
1373 /* Analyze the instruction. */
1374 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1375 global, spill_reg_order);
1377 /* If a no-op set needs more than one reload, this is likely
1378 to be something that needs input address reloads. We
1379 can't get rid of this cleanly later, and it is of no use
1380 anyway, so discard it now.
1381 We only do this when expensive_optimizations is enabled,
1382 since this complements reload inheritance / output
1383 reload deletion, and it can make debugging harder. */
1384 if (flag_expensive_optimizations && n_reloads > 1)
1386 rtx set = single_set (insn);
1387 if (set
1388 && SET_SRC (set) == SET_DEST (set)
1389 && GET_CODE (SET_SRC (set)) == REG
1390 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1392 PUT_CODE (insn, NOTE);
1393 NOTE_SOURCE_FILE (insn) = 0;
1394 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1395 continue;
1398 if (num_eliminable)
1399 update_eliminable_offsets ();
1401 /* Remember for later shortcuts which insns had any reloads or
1402 register eliminations. */
1403 chain->need_elim = did_elimination;
1404 chain->need_reload = n_reloads > 0;
1405 chain->need_operand_change = operands_changed;
1407 /* Discard any register replacements done. */
1408 if (did_elimination)
1410 obstack_free (&reload_obstack, reload_firstobj);
1411 PATTERN (insn) = old_body;
1412 INSN_CODE (insn) = old_code;
1413 REG_NOTES (insn) = old_notes;
1414 something_needs_elimination = 1;
1417 something_needs_operands_changed |= operands_changed;
1419 if (n_reloads != 0)
1421 *pprev_reload = chain;
1422 pprev_reload = &chain->next_need_reload;
1424 calculate_needs (chain);
1428 *pprev_reload = 0;
1431 /* Compute the most additional registers needed by one instruction,
1432 given by CHAIN. Collect information separately for each class of regs.
1434 To compute the number of reload registers of each class needed for an
1435 insn, we must simulate what choose_reload_regs can do. We do this by
1436 splitting an insn into an "input" and an "output" part. RELOAD_OTHER
1437 reloads are used in both. The input part uses those reloads,
1438 RELOAD_FOR_INPUT reloads, which must be live over the entire input section
1439 of reloads, and the maximum of all the RELOAD_FOR_INPUT_ADDRESS and
1440 RELOAD_FOR_OPERAND_ADDRESS reloads, which conflict with the inputs.
1442 The registers needed for output are RELOAD_OTHER and RELOAD_FOR_OUTPUT,
1443 which are live for the entire output portion, and the maximum of all the
1444 RELOAD_FOR_OUTPUT_ADDRESS reloads for each operand.
1446 The total number of registers needed is the maximum of the
1447 inputs and outputs. */
1449 static void
1450 calculate_needs (chain)
1451 struct insn_chain *chain;
1453 int i;
1455 /* Each `struct needs' corresponds to one RELOAD_... type. */
1456 struct {
1457 struct needs other;
1458 struct needs input;
1459 struct needs output;
1460 struct needs insn;
1461 struct needs other_addr;
1462 struct needs op_addr;
1463 struct needs op_addr_reload;
1464 struct needs in_addr[MAX_RECOG_OPERANDS];
1465 struct needs in_addr_addr[MAX_RECOG_OPERANDS];
1466 struct needs out_addr[MAX_RECOG_OPERANDS];
1467 struct needs out_addr_addr[MAX_RECOG_OPERANDS];
1468 } insn_needs;
1470 bzero ((char *) chain->group_size, sizeof chain->group_size);
1471 for (i = 0; i < N_REG_CLASSES; i++)
1472 chain->group_mode[i] = VOIDmode;
1473 bzero ((char *) &insn_needs, sizeof insn_needs);
1475 /* Count each reload once in every class
1476 containing the reload's own class. */
1478 for (i = 0; i < n_reloads; i++)
1480 register enum reg_class *p;
1481 enum reg_class class = rld[i].class;
1482 int size;
1483 enum machine_mode mode;
1484 struct needs *this_needs;
1486 /* Don't count the dummy reloads, for which one of the
1487 regs mentioned in the insn can be used for reloading.
1488 Don't count optional reloads.
1489 Don't count reloads that got combined with others. */
1490 if (rld[i].reg_rtx != 0
1491 || rld[i].optional != 0
1492 || (rld[i].out == 0 && rld[i].in == 0
1493 && ! rld[i].secondary_p))
1494 continue;
1496 mode = rld[i].mode;
1497 size = rld[i].nregs;
1499 /* Decide which time-of-use to count this reload for. */
1500 switch (rld[i].when_needed)
1502 case RELOAD_OTHER:
1503 this_needs = &insn_needs.other;
1504 break;
1505 case RELOAD_FOR_INPUT:
1506 this_needs = &insn_needs.input;
1507 break;
1508 case RELOAD_FOR_OUTPUT:
1509 this_needs = &insn_needs.output;
1510 break;
1511 case RELOAD_FOR_INSN:
1512 this_needs = &insn_needs.insn;
1513 break;
1514 case RELOAD_FOR_OTHER_ADDRESS:
1515 this_needs = &insn_needs.other_addr;
1516 break;
1517 case RELOAD_FOR_INPUT_ADDRESS:
1518 this_needs = &insn_needs.in_addr[rld[i].opnum];
1519 break;
1520 case RELOAD_FOR_INPADDR_ADDRESS:
1521 this_needs = &insn_needs.in_addr_addr[rld[i].opnum];
1522 break;
1523 case RELOAD_FOR_OUTPUT_ADDRESS:
1524 this_needs = &insn_needs.out_addr[rld[i].opnum];
1525 break;
1526 case RELOAD_FOR_OUTADDR_ADDRESS:
1527 this_needs = &insn_needs.out_addr_addr[rld[i].opnum];
1528 break;
1529 case RELOAD_FOR_OPERAND_ADDRESS:
1530 this_needs = &insn_needs.op_addr;
1531 break;
1532 case RELOAD_FOR_OPADDR_ADDR:
1533 this_needs = &insn_needs.op_addr_reload;
1534 break;
1535 default:
1536 abort();
1539 if (size > 1)
1541 enum machine_mode other_mode, allocate_mode;
1543 /* Count number of groups needed separately from
1544 number of individual regs needed. */
1545 this_needs->groups[(int) class]++;
1546 p = reg_class_superclasses[(int) class];
1547 while (*p != LIM_REG_CLASSES)
1548 this_needs->groups[(int) *p++]++;
1550 /* Record size and mode of a group of this class. */
1551 /* If more than one size group is needed,
1552 make all groups the largest needed size. */
1553 if (chain->group_size[(int) class] < size)
1555 other_mode = chain->group_mode[(int) class];
1556 allocate_mode = mode;
1558 chain->group_size[(int) class] = size;
1559 chain->group_mode[(int) class] = mode;
1561 else
1563 other_mode = mode;
1564 allocate_mode = chain->group_mode[(int) class];
1567 /* Crash if two dissimilar machine modes both need
1568 groups of consecutive regs of the same class. */
1570 if (other_mode != VOIDmode && other_mode != allocate_mode
1571 && ! modes_equiv_for_class_p (allocate_mode,
1572 other_mode, class))
1573 fatal_insn ("Two dissimilar machine modes both need groups of consecutive regs of the same class",
1574 chain->insn);
1576 else if (size == 1)
1578 this_needs->regs[(unsigned char)rld[i].nongroup][(int) class] += 1;
1579 p = reg_class_superclasses[(int) class];
1580 while (*p != LIM_REG_CLASSES)
1581 this_needs->regs[(unsigned char)rld[i].nongroup][(int) *p++] += 1;
1583 else
1584 abort ();
1587 /* All reloads have been counted for this insn;
1588 now merge the various times of use.
1589 This sets insn_needs, etc., to the maximum total number
1590 of registers needed at any point in this insn. */
1592 for (i = 0; i < N_REG_CLASSES; i++)
1594 int j, in_max, out_max;
1596 /* Compute normal and nongroup needs. */
1597 for (j = 0; j <= 1; j++)
1599 int k;
1600 for (in_max = 0, out_max = 0, k = 0; k < reload_n_operands; k++)
1602 in_max = MAX (in_max,
1603 (insn_needs.in_addr[k].regs[j][i]
1604 + insn_needs.in_addr_addr[k].regs[j][i]));
1605 out_max = MAX (out_max, insn_needs.out_addr[k].regs[j][i]);
1606 out_max = MAX (out_max,
1607 insn_needs.out_addr_addr[k].regs[j][i]);
1610 /* RELOAD_FOR_INSN reloads conflict with inputs, outputs,
1611 and operand addresses but not things used to reload
1612 them. Similarly, RELOAD_FOR_OPERAND_ADDRESS reloads
1613 don't conflict with things needed to reload inputs or
1614 outputs. */
1616 in_max = MAX (MAX (insn_needs.op_addr.regs[j][i],
1617 insn_needs.op_addr_reload.regs[j][i]),
1618 in_max);
1620 out_max = MAX (out_max, insn_needs.insn.regs[j][i]);
1622 insn_needs.input.regs[j][i]
1623 = MAX (insn_needs.input.regs[j][i]
1624 + insn_needs.op_addr.regs[j][i]
1625 + insn_needs.insn.regs[j][i],
1626 in_max + insn_needs.input.regs[j][i]);
1628 insn_needs.output.regs[j][i] += out_max;
1629 insn_needs.other.regs[j][i]
1630 += MAX (MAX (insn_needs.input.regs[j][i],
1631 insn_needs.output.regs[j][i]),
1632 insn_needs.other_addr.regs[j][i]);
1636 /* Now compute group needs. */
1637 for (in_max = 0, out_max = 0, j = 0; j < reload_n_operands; j++)
1639 in_max = MAX (in_max, insn_needs.in_addr[j].groups[i]);
1640 in_max = MAX (in_max, insn_needs.in_addr_addr[j].groups[i]);
1641 out_max = MAX (out_max, insn_needs.out_addr[j].groups[i]);
1642 out_max = MAX (out_max, insn_needs.out_addr_addr[j].groups[i]);
1645 in_max = MAX (MAX (insn_needs.op_addr.groups[i],
1646 insn_needs.op_addr_reload.groups[i]),
1647 in_max);
1648 out_max = MAX (out_max, insn_needs.insn.groups[i]);
1650 insn_needs.input.groups[i]
1651 = MAX (insn_needs.input.groups[i]
1652 + insn_needs.op_addr.groups[i]
1653 + insn_needs.insn.groups[i],
1654 in_max + insn_needs.input.groups[i]);
1656 insn_needs.output.groups[i] += out_max;
1657 insn_needs.other.groups[i]
1658 += MAX (MAX (insn_needs.input.groups[i],
1659 insn_needs.output.groups[i]),
1660 insn_needs.other_addr.groups[i]);
1663 /* Record the needs for later. */
1664 chain->need = insn_needs.other;
1667 /* Find a group of exactly 2 registers.
1669 First try to fill out the group by spilling a single register which
1670 would allow completion of the group.
1672 Then try to create a new group from a pair of registers, neither of
1673 which are explicitly used.
1675 Then try to create a group from any pair of registers. */
1677 static void
1678 find_tworeg_group (chain, class, dumpfile)
1679 struct insn_chain *chain;
1680 int class;
1681 FILE *dumpfile;
1683 int i;
1684 /* First, look for a register that will complete a group. */
1685 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1687 int j, other;
1689 j = potential_reload_regs[i];
1690 if (j >= 0 && ! TEST_HARD_REG_BIT (bad_spill_regs, j)
1691 && ((j > 0 && (other = j - 1, spill_reg_order[other] >= 0)
1692 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1693 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1694 && HARD_REGNO_MODE_OK (other, chain->group_mode[class])
1695 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, other)
1696 /* We don't want one part of another group.
1697 We could get "two groups" that overlap! */
1698 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, other))
1699 || (j < FIRST_PSEUDO_REGISTER - 1
1700 && (other = j + 1, spill_reg_order[other] >= 0)
1701 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1702 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1703 && HARD_REGNO_MODE_OK (j, chain->group_mode[class])
1704 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, other)
1705 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, other))))
1707 register enum reg_class *p;
1709 /* We have found one that will complete a group,
1710 so count off one group as provided. */
1711 chain->need.groups[class]--;
1712 p = reg_class_superclasses[class];
1713 while (*p != LIM_REG_CLASSES)
1715 if (chain->group_size [(int) *p] <= chain->group_size [class])
1716 chain->need.groups[(int) *p]--;
1717 p++;
1720 /* Indicate both these regs are part of a group. */
1721 SET_HARD_REG_BIT (chain->counted_for_groups, j);
1722 SET_HARD_REG_BIT (chain->counted_for_groups, other);
1723 break;
1726 /* We can't complete a group, so start one. */
1727 if (i == FIRST_PSEUDO_REGISTER)
1728 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1730 int j, k;
1731 j = potential_reload_regs[i];
1732 /* Verify that J+1 is a potential reload reg. */
1733 for (k = 0; k < FIRST_PSEUDO_REGISTER; k++)
1734 if (potential_reload_regs[k] == j + 1)
1735 break;
1736 if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1737 && k < FIRST_PSEUDO_REGISTER
1738 && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1739 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1740 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1741 && HARD_REGNO_MODE_OK (j, chain->group_mode[class])
1742 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, j + 1)
1743 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + 1))
1744 break;
1747 /* I should be the index in potential_reload_regs
1748 of the new reload reg we have found. */
1750 new_spill_reg (chain, i, class, 0, dumpfile);
1753 /* Find a group of more than 2 registers.
1754 Look for a sufficient sequence of unspilled registers, and spill them all
1755 at once. */
1757 static void
1758 find_group (chain, class, dumpfile)
1759 struct insn_chain *chain;
1760 int class;
1761 FILE *dumpfile;
1763 int i;
1765 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1767 int j = potential_reload_regs[i];
1769 if (j >= 0
1770 && j + chain->group_size[class] <= FIRST_PSEUDO_REGISTER
1771 && HARD_REGNO_MODE_OK (j, chain->group_mode[class]))
1773 int k;
1774 /* Check each reg in the sequence. */
1775 for (k = 0; k < chain->group_size[class]; k++)
1776 if (! (spill_reg_order[j + k] < 0
1777 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + k)
1778 && TEST_HARD_REG_BIT (reg_class_contents[class], j + k)))
1779 break;
1780 /* We got a full sequence, so spill them all. */
1781 if (k == chain->group_size[class])
1783 register enum reg_class *p;
1784 for (k = 0; k < chain->group_size[class]; k++)
1786 int idx;
1787 SET_HARD_REG_BIT (chain->counted_for_groups, j + k);
1788 for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
1789 if (potential_reload_regs[idx] == j + k)
1790 break;
1791 new_spill_reg (chain, idx, class, 0, dumpfile);
1794 /* We have found one that will complete a group,
1795 so count off one group as provided. */
1796 chain->need.groups[class]--;
1797 p = reg_class_superclasses[class];
1798 while (*p != LIM_REG_CLASSES)
1800 if (chain->group_size [(int) *p]
1801 <= chain->group_size [class])
1802 chain->need.groups[(int) *p]--;
1803 p++;
1805 return;
1809 /* There are no groups left. */
1810 spill_failure (chain->insn);
1811 failure = 1;
1814 /* If pseudo REG conflicts with one of our reload registers, mark it as
1815 spilled. */
1816 static void
1817 maybe_mark_pseudo_spilled (reg)
1818 int reg;
1820 int i;
1821 int r = reg_renumber[reg];
1822 int nregs;
1824 if (r < 0)
1825 abort ();
1826 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
1827 for (i = 0; i < n_spills; i++)
1828 if (r <= spill_regs[i] && r + nregs > spill_regs[i])
1830 SET_REGNO_REG_SET (spilled_pseudos, reg);
1831 return;
1835 /* Find more reload regs to satisfy the remaining need of an insn, which
1836 is given by CHAIN.
1837 Do it by ascending class number, since otherwise a reg
1838 might be spilled for a big class and might fail to count
1839 for a smaller class even though it belongs to that class.
1841 Count spilled regs in `spills', and add entries to
1842 `spill_regs' and `spill_reg_order'.
1844 ??? Note there is a problem here.
1845 When there is a need for a group in a high-numbered class,
1846 and also need for non-group regs that come from a lower class,
1847 the non-group regs are chosen first. If there aren't many regs,
1848 they might leave no room for a group.
1850 This was happening on the 386. To fix it, we added the code
1851 that calls possible_group_p, so that the lower class won't
1852 break up the last possible group.
1854 Really fixing the problem would require changes above
1855 in counting the regs already spilled, and in choose_reload_regs.
1856 It might be hard to avoid introducing bugs there. */
1858 static void
1859 find_reload_regs (chain, dumpfile)
1860 struct insn_chain *chain;
1861 FILE *dumpfile;
1863 int i, class;
1864 short *group_needs = chain->need.groups;
1865 short *simple_needs = chain->need.regs[0];
1866 short *nongroup_needs = chain->need.regs[1];
1868 if (dumpfile)
1869 fprintf (dumpfile, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1871 /* Compute the order of preference for hard registers to spill.
1872 Store them by decreasing preference in potential_reload_regs. */
1874 order_regs_for_reload (chain);
1876 /* So far, no hard regs have been spilled. */
1877 n_spills = 0;
1878 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1879 spill_reg_order[i] = -1;
1881 CLEAR_HARD_REG_SET (chain->used_spill_regs);
1882 CLEAR_HARD_REG_SET (chain->counted_for_groups);
1883 CLEAR_HARD_REG_SET (chain->counted_for_nongroups);
1885 for (class = 0; class < N_REG_CLASSES; class++)
1887 /* First get the groups of registers.
1888 If we got single registers first, we might fragment
1889 possible groups. */
1890 while (group_needs[class] > 0)
1892 /* If any single spilled regs happen to form groups,
1893 count them now. Maybe we don't really need
1894 to spill another group. */
1895 count_possible_groups (chain, class);
1897 if (group_needs[class] <= 0)
1898 break;
1900 /* Groups of size 2, the only groups used on most machines,
1901 are treated specially. */
1902 if (chain->group_size[class] == 2)
1903 find_tworeg_group (chain, class, dumpfile);
1904 else
1905 find_group (chain, class, dumpfile);
1906 if (failure)
1907 return;
1910 /* Now similarly satisfy all need for single registers. */
1912 while (simple_needs[class] > 0 || nongroup_needs[class] > 0)
1914 /* If we spilled enough regs, but they weren't counted
1915 against the non-group need, see if we can count them now.
1916 If so, we can avoid some actual spilling. */
1917 if (simple_needs[class] <= 0 && nongroup_needs[class] > 0)
1918 for (i = 0; i < n_spills; i++)
1920 int regno = spill_regs[i];
1921 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1922 && !TEST_HARD_REG_BIT (chain->counted_for_groups, regno)
1923 && !TEST_HARD_REG_BIT (chain->counted_for_nongroups, regno)
1924 && nongroup_needs[class] > 0)
1926 register enum reg_class *p;
1928 SET_HARD_REG_BIT (chain->counted_for_nongroups, regno);
1929 nongroup_needs[class]--;
1930 p = reg_class_superclasses[class];
1931 while (*p != LIM_REG_CLASSES)
1932 nongroup_needs[(int) *p++]--;
1936 if (simple_needs[class] <= 0 && nongroup_needs[class] <= 0)
1937 break;
1939 /* Consider the potential reload regs that aren't
1940 yet in use as reload regs, in order of preference.
1941 Find the most preferred one that's in this class. */
1943 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1945 int regno = potential_reload_regs[i];
1946 if (regno >= 0
1947 && TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1948 /* If this reg will not be available for groups,
1949 pick one that does not foreclose possible groups.
1950 This is a kludge, and not very general,
1951 but it should be sufficient to make the 386 work,
1952 and the problem should not occur on machines with
1953 more registers. */
1954 && (nongroup_needs[class] == 0
1955 || possible_group_p (chain, regno)))
1956 break;
1959 /* If we couldn't get a register, try to get one even if we
1960 might foreclose possible groups. This may cause problems
1961 later, but that's better than aborting now, since it is
1962 possible that we will, in fact, be able to form the needed
1963 group even with this allocation. */
1965 if (i >= FIRST_PSEUDO_REGISTER
1966 && asm_noperands (chain->insn) < 0)
1967 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1968 if (potential_reload_regs[i] >= 0
1969 && TEST_HARD_REG_BIT (reg_class_contents[class],
1970 potential_reload_regs[i]))
1971 break;
1973 /* I should be the index in potential_reload_regs
1974 of the new reload reg we have found. */
1976 new_spill_reg (chain, i, class, 1, dumpfile);
1977 if (failure)
1978 return;
1982 /* We know which hard regs to use, now mark the pseudos that live in them
1983 as needing to be kicked out. */
1984 EXECUTE_IF_SET_IN_REG_SET
1985 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
1987 maybe_mark_pseudo_spilled (i);
1989 EXECUTE_IF_SET_IN_REG_SET
1990 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
1992 maybe_mark_pseudo_spilled (i);
1995 IOR_HARD_REG_SET (used_spill_regs, chain->used_spill_regs);
1998 void
1999 dump_needs (chain, dumpfile)
2000 struct insn_chain *chain;
2001 FILE *dumpfile;
2003 static const char * const reg_class_names[] = REG_CLASS_NAMES;
2004 int i;
2005 struct needs *n = &chain->need;
2007 for (i = 0; i < N_REG_CLASSES; i++)
2009 if (n->regs[i][0] > 0)
2010 fprintf (dumpfile,
2011 ";; Need %d reg%s of class %s.\n",
2012 n->regs[i][0], n->regs[i][0] == 1 ? "" : "s",
2013 reg_class_names[i]);
2014 if (n->regs[i][1] > 0)
2015 fprintf (dumpfile,
2016 ";; Need %d nongroup reg%s of class %s.\n",
2017 n->regs[i][1], n->regs[i][1] == 1 ? "" : "s",
2018 reg_class_names[i]);
2019 if (n->groups[i] > 0)
2020 fprintf (dumpfile,
2021 ";; Need %d group%s (%smode) of class %s.\n",
2022 n->groups[i], n->groups[i] == 1 ? "" : "s",
2023 GET_MODE_NAME(chain->group_mode[i]),
2024 reg_class_names[i]);
2028 /* Delete all insns that were inserted by emit_caller_save_insns during
2029 this iteration. */
2030 static void
2031 delete_caller_save_insns ()
2033 struct insn_chain *c = reload_insn_chain;
2035 while (c != 0)
2037 while (c != 0 && c->is_caller_save_insn)
2039 struct insn_chain *next = c->next;
2040 rtx insn = c->insn;
2042 if (insn == BLOCK_HEAD (c->block))
2043 BLOCK_HEAD (c->block) = NEXT_INSN (insn);
2044 if (insn == BLOCK_END (c->block))
2045 BLOCK_END (c->block) = PREV_INSN (insn);
2046 if (c == reload_insn_chain)
2047 reload_insn_chain = next;
2049 if (NEXT_INSN (insn) != 0)
2050 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2051 if (PREV_INSN (insn) != 0)
2052 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2054 if (next)
2055 next->prev = c->prev;
2056 if (c->prev)
2057 c->prev->next = next;
2058 c->next = unused_insn_chains;
2059 unused_insn_chains = c;
2060 c = next;
2062 if (c != 0)
2063 c = c->next;
2067 /* Nonzero if, after spilling reg REGNO for non-groups,
2068 it will still be possible to find a group if we still need one. */
2070 static int
2071 possible_group_p (chain, regno)
2072 struct insn_chain *chain;
2073 int regno;
2075 int i;
2076 int class = (int) NO_REGS;
2078 for (i = 0; i < (int) N_REG_CLASSES; i++)
2079 if (chain->need.groups[i] > 0)
2081 class = i;
2082 break;
2085 if (class == (int) NO_REGS)
2086 return 1;
2088 /* Consider each pair of consecutive registers. */
2089 for (i = 0; i < FIRST_PSEUDO_REGISTER - 1; i++)
2091 /* Ignore pairs that include reg REGNO. */
2092 if (i == regno || i + 1 == regno)
2093 continue;
2095 /* Ignore pairs that are outside the class that needs the group.
2096 ??? Here we fail to handle the case where two different classes
2097 independently need groups. But this never happens with our
2098 current machine descriptions. */
2099 if (! (TEST_HARD_REG_BIT (reg_class_contents[class], i)
2100 && TEST_HARD_REG_BIT (reg_class_contents[class], i + 1)))
2101 continue;
2103 /* A pair of consecutive regs we can still spill does the trick. */
2104 if (spill_reg_order[i] < 0 && spill_reg_order[i + 1] < 0
2105 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2106 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1))
2107 return 1;
2109 /* A pair of one already spilled and one we can spill does it
2110 provided the one already spilled is not otherwise reserved. */
2111 if (spill_reg_order[i] < 0
2112 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2113 && spill_reg_order[i + 1] >= 0
2114 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, i + 1)
2115 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, i + 1))
2116 return 1;
2117 if (spill_reg_order[i + 1] < 0
2118 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1)
2119 && spill_reg_order[i] >= 0
2120 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, i)
2121 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, i))
2122 return 1;
2125 return 0;
2128 /* Count any groups of CLASS that can be formed from the registers recently
2129 spilled. */
2131 static void
2132 count_possible_groups (chain, class)
2133 struct insn_chain *chain;
2134 int class;
2136 HARD_REG_SET new;
2137 int i, j;
2139 /* Now find all consecutive groups of spilled registers
2140 and mark each group off against the need for such groups.
2141 But don't count them against ordinary need, yet. */
2143 if (chain->group_size[class] == 0)
2144 return;
2146 CLEAR_HARD_REG_SET (new);
2148 /* Make a mask of all the regs that are spill regs in class I. */
2149 for (i = 0; i < n_spills; i++)
2151 int regno = spill_regs[i];
2153 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
2154 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, regno)
2155 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, regno))
2156 SET_HARD_REG_BIT (new, regno);
2159 /* Find each consecutive group of them. */
2160 for (i = 0; i < FIRST_PSEUDO_REGISTER && chain->need.groups[class] > 0; i++)
2161 if (TEST_HARD_REG_BIT (new, i)
2162 && i + chain->group_size[class] <= FIRST_PSEUDO_REGISTER
2163 && HARD_REGNO_MODE_OK (i, chain->group_mode[class]))
2165 for (j = 1; j < chain->group_size[class]; j++)
2166 if (! TEST_HARD_REG_BIT (new, i + j))
2167 break;
2169 if (j == chain->group_size[class])
2171 /* We found a group. Mark it off against this class's need for
2172 groups, and against each superclass too. */
2173 register enum reg_class *p;
2175 chain->need.groups[class]--;
2176 p = reg_class_superclasses[class];
2177 while (*p != LIM_REG_CLASSES)
2179 if (chain->group_size [(int) *p] <= chain->group_size [class])
2180 chain->need.groups[(int) *p]--;
2181 p++;
2184 /* Don't count these registers again. */
2185 for (j = 0; j < chain->group_size[class]; j++)
2186 SET_HARD_REG_BIT (chain->counted_for_groups, i + j);
2189 /* Skip to the last reg in this group. When i is incremented above,
2190 it will then point to the first reg of the next possible group. */
2191 i += j - 1;
2195 /* ALLOCATE_MODE is a register mode that needs to be reloaded. OTHER_MODE is
2196 another mode that needs to be reloaded for the same register class CLASS.
2197 If any reg in CLASS allows ALLOCATE_MODE but not OTHER_MODE, fail.
2198 ALLOCATE_MODE will never be smaller than OTHER_MODE.
2200 This code used to also fail if any reg in CLASS allows OTHER_MODE but not
2201 ALLOCATE_MODE. This test is unnecessary, because we will never try to put
2202 something of mode ALLOCATE_MODE into an OTHER_MODE register. Testing this
2203 causes unnecessary failures on machines requiring alignment of register
2204 groups when the two modes are different sizes, because the larger mode has
2205 more strict alignment rules than the smaller mode. */
2207 static int
2208 modes_equiv_for_class_p (allocate_mode, other_mode, class)
2209 enum machine_mode allocate_mode, other_mode;
2210 enum reg_class class;
2212 register int regno;
2213 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2215 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno)
2216 && HARD_REGNO_MODE_OK (regno, allocate_mode)
2217 && ! HARD_REGNO_MODE_OK (regno, other_mode))
2218 return 0;
2220 return 1;
2223 /* Handle the failure to find a register to spill.
2224 INSN should be one of the insns which needed this particular spill reg. */
2226 static void
2227 spill_failure (insn)
2228 rtx insn;
2230 if (asm_noperands (PATTERN (insn)) >= 0)
2231 error_for_asm (insn, "`asm' needs too many reloads");
2232 else
2233 fatal_insn ("Unable to find a register to spill.", insn);
2236 /* Add a new register to the tables of available spill-registers.
2237 CHAIN is the insn for which the register will be used; we decrease the
2238 needs of that insn.
2239 I is the index of this register in potential_reload_regs.
2240 CLASS is the regclass whose need is being satisfied.
2241 NONGROUP is 0 if this register is part of a group.
2242 DUMPFILE is the same as the one that `reload' got. */
2244 static void
2245 new_spill_reg (chain, i, class, nongroup, dumpfile)
2246 struct insn_chain *chain;
2247 int i;
2248 int class;
2249 int nongroup;
2250 FILE *dumpfile;
2252 register enum reg_class *p;
2253 int regno = potential_reload_regs[i];
2255 if (i >= FIRST_PSEUDO_REGISTER)
2257 spill_failure (chain->insn);
2258 failure = 1;
2259 return;
2262 if (TEST_HARD_REG_BIT (bad_spill_regs, regno))
2264 static const char * const reg_class_names[] = REG_CLASS_NAMES;
2266 if (asm_noperands (PATTERN (chain->insn)) < 0)
2268 /* The error message is still correct - we know only that it wasn't
2269 an asm statement that caused the problem, but one of the global
2270 registers declared by the users might have screwed us. */
2271 error ("fixed or forbidden register %d (%s) was spilled for class %s.",
2272 regno, reg_names[regno], reg_class_names[class]);
2273 error ("This may be due to a compiler bug or to impossible asm");
2274 error ("statements or clauses.");
2275 fatal_insn ("This is the instruction:", chain->insn);
2277 error_for_asm (chain->insn, "Invalid `asm' statement:");
2278 error_for_asm (chain->insn,
2279 "fixed or forbidden register %d (%s) was spilled for class %s.",
2280 regno, reg_names[regno], reg_class_names[class]);
2281 failure = 1;
2282 return;
2285 /* Make reg REGNO an additional reload reg. */
2287 potential_reload_regs[i] = -1;
2288 spill_regs[n_spills] = regno;
2289 spill_reg_order[regno] = n_spills;
2290 if (dumpfile)
2291 fprintf (dumpfile, "Spilling reg %d.\n", regno);
2292 SET_HARD_REG_BIT (chain->used_spill_regs, regno);
2294 /* Clear off the needs we just satisfied. */
2296 chain->need.regs[0][class]--;
2297 p = reg_class_superclasses[class];
2298 while (*p != LIM_REG_CLASSES)
2299 chain->need.regs[0][(int) *p++]--;
2301 if (nongroup && chain->need.regs[1][class] > 0)
2303 SET_HARD_REG_BIT (chain->counted_for_nongroups, regno);
2304 chain->need.regs[1][class]--;
2305 p = reg_class_superclasses[class];
2306 while (*p != LIM_REG_CLASSES)
2307 chain->need.regs[1][(int) *p++]--;
2310 n_spills++;
2313 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2314 data that is dead in INSN. */
2316 static void
2317 delete_dead_insn (insn)
2318 rtx insn;
2320 rtx prev = prev_real_insn (insn);
2321 rtx prev_dest;
2323 /* If the previous insn sets a register that dies in our insn, delete it
2324 too. */
2325 if (prev && GET_CODE (PATTERN (prev)) == SET
2326 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
2327 && reg_mentioned_p (prev_dest, PATTERN (insn))
2328 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
2329 && ! side_effects_p (SET_SRC (PATTERN (prev))))
2330 delete_dead_insn (prev);
2332 PUT_CODE (insn, NOTE);
2333 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2334 NOTE_SOURCE_FILE (insn) = 0;
2337 /* Modify the home of pseudo-reg I.
2338 The new home is present in reg_renumber[I].
2340 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2341 or it may be -1, meaning there is none or it is not relevant.
2342 This is used so that all pseudos spilled from a given hard reg
2343 can share one stack slot. */
2345 static void
2346 alter_reg (i, from_reg)
2347 register int i;
2348 int from_reg;
2350 /* When outputting an inline function, this can happen
2351 for a reg that isn't actually used. */
2352 if (regno_reg_rtx[i] == 0)
2353 return;
2355 /* If the reg got changed to a MEM at rtl-generation time,
2356 ignore it. */
2357 if (GET_CODE (regno_reg_rtx[i]) != REG)
2358 return;
2360 /* Modify the reg-rtx to contain the new hard reg
2361 number or else to contain its pseudo reg number. */
2362 REGNO (regno_reg_rtx[i])
2363 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
2365 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2366 allocate a stack slot for it. */
2368 if (reg_renumber[i] < 0
2369 && REG_N_REFS (i) > 0
2370 && reg_equiv_constant[i] == 0
2371 && reg_equiv_memory_loc[i] == 0)
2373 register rtx x;
2374 int inherent_size = PSEUDO_REGNO_BYTES (i);
2375 int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2376 int adjust = 0;
2378 /* Each pseudo reg has an inherent size which comes from its own mode,
2379 and a total size which provides room for paradoxical subregs
2380 which refer to the pseudo reg in wider modes.
2382 We can use a slot already allocated if it provides both
2383 enough inherent space and enough total space.
2384 Otherwise, we allocate a new slot, making sure that it has no less
2385 inherent space, and no less total space, then the previous slot. */
2386 if (from_reg == -1)
2388 /* No known place to spill from => no slot to reuse. */
2389 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
2390 inherent_size == total_size ? 0 : -1);
2391 if (BYTES_BIG_ENDIAN)
2392 /* Cancel the big-endian correction done in assign_stack_local.
2393 Get the address of the beginning of the slot.
2394 This is so we can do a big-endian correction unconditionally
2395 below. */
2396 adjust = inherent_size - total_size;
2398 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2400 /* Reuse a stack slot if possible. */
2401 else if (spill_stack_slot[from_reg] != 0
2402 && spill_stack_slot_width[from_reg] >= total_size
2403 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2404 >= inherent_size))
2405 x = spill_stack_slot[from_reg];
2406 /* Allocate a bigger slot. */
2407 else
2409 /* Compute maximum size needed, both for inherent size
2410 and for total size. */
2411 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2412 rtx stack_slot;
2413 if (spill_stack_slot[from_reg])
2415 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2416 > inherent_size)
2417 mode = GET_MODE (spill_stack_slot[from_reg]);
2418 if (spill_stack_slot_width[from_reg] > total_size)
2419 total_size = spill_stack_slot_width[from_reg];
2421 /* Make a slot with that size. */
2422 x = assign_stack_local (mode, total_size,
2423 inherent_size == total_size ? 0 : -1);
2424 stack_slot = x;
2425 if (BYTES_BIG_ENDIAN)
2427 /* Cancel the big-endian correction done in assign_stack_local.
2428 Get the address of the beginning of the slot.
2429 This is so we can do a big-endian correction unconditionally
2430 below. */
2431 adjust = GET_MODE_SIZE (mode) - total_size;
2432 if (adjust)
2433 stack_slot = gen_rtx_MEM (mode_for_size (total_size
2434 * BITS_PER_UNIT,
2435 MODE_INT, 1),
2436 plus_constant (XEXP (x, 0), adjust));
2438 spill_stack_slot[from_reg] = stack_slot;
2439 spill_stack_slot_width[from_reg] = total_size;
2442 /* On a big endian machine, the "address" of the slot
2443 is the address of the low part that fits its inherent mode. */
2444 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2445 adjust += (total_size - inherent_size);
2447 /* If we have any adjustment to make, or if the stack slot is the
2448 wrong mode, make a new stack slot. */
2449 if (adjust != 0 || GET_MODE (x) != GET_MODE (regno_reg_rtx[i]))
2451 x = gen_rtx_MEM (GET_MODE (regno_reg_rtx[i]),
2452 plus_constant (XEXP (x, 0), adjust));
2454 /* If this was shared among registers, must ensure we never
2455 set it readonly since that can cause scheduling
2456 problems. Note we would only have in this adjustment
2457 case in any event, since the code above doesn't set it. */
2459 if (from_reg == -1)
2460 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2463 /* Save the stack slot for later. */
2464 reg_equiv_memory_loc[i] = x;
2468 /* Mark the slots in regs_ever_live for the hard regs
2469 used by pseudo-reg number REGNO. */
2471 void
2472 mark_home_live (regno)
2473 int regno;
2475 register int i, lim;
2476 i = reg_renumber[regno];
2477 if (i < 0)
2478 return;
2479 lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2480 while (i < lim)
2481 regs_ever_live[i++] = 1;
2484 /* This function handles the tracking of elimination offsets around branches.
2486 X is a piece of RTL being scanned.
2488 INSN is the insn that it came from, if any.
2490 INITIAL_P is non-zero if we are to set the offset to be the initial
2491 offset and zero if we are setting the offset of the label to be the
2492 current offset. */
2494 static void
2495 set_label_offsets (x, insn, initial_p)
2496 rtx x;
2497 rtx insn;
2498 int initial_p;
2500 enum rtx_code code = GET_CODE (x);
2501 rtx tem;
2502 unsigned int i;
2503 struct elim_table *p;
2505 switch (code)
2507 case LABEL_REF:
2508 if (LABEL_REF_NONLOCAL_P (x))
2509 return;
2511 x = XEXP (x, 0);
2513 /* ... fall through ... */
2515 case CODE_LABEL:
2516 /* If we know nothing about this label, set the desired offsets. Note
2517 that this sets the offset at a label to be the offset before a label
2518 if we don't know anything about the label. This is not correct for
2519 the label after a BARRIER, but is the best guess we can make. If
2520 we guessed wrong, we will suppress an elimination that might have
2521 been possible had we been able to guess correctly. */
2523 if (! offsets_known_at[CODE_LABEL_NUMBER (x)])
2525 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2526 offsets_at[CODE_LABEL_NUMBER (x)][i]
2527 = (initial_p ? reg_eliminate[i].initial_offset
2528 : reg_eliminate[i].offset);
2529 offsets_known_at[CODE_LABEL_NUMBER (x)] = 1;
2532 /* Otherwise, if this is the definition of a label and it is
2533 preceded by a BARRIER, set our offsets to the known offset of
2534 that label. */
2536 else if (x == insn
2537 && (tem = prev_nonnote_insn (insn)) != 0
2538 && GET_CODE (tem) == BARRIER)
2539 set_offsets_for_label (insn);
2540 else
2541 /* If neither of the above cases is true, compare each offset
2542 with those previously recorded and suppress any eliminations
2543 where the offsets disagree. */
2545 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2546 if (offsets_at[CODE_LABEL_NUMBER (x)][i]
2547 != (initial_p ? reg_eliminate[i].initial_offset
2548 : reg_eliminate[i].offset))
2549 reg_eliminate[i].can_eliminate = 0;
2551 return;
2553 case JUMP_INSN:
2554 set_label_offsets (PATTERN (insn), insn, initial_p);
2556 /* ... fall through ... */
2558 case INSN:
2559 case CALL_INSN:
2560 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2561 and hence must have all eliminations at their initial offsets. */
2562 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2563 if (REG_NOTE_KIND (tem) == REG_LABEL)
2564 set_label_offsets (XEXP (tem, 0), insn, 1);
2565 return;
2567 case ADDR_VEC:
2568 case ADDR_DIFF_VEC:
2569 /* Each of the labels in the address vector must be at their initial
2570 offsets. We want the first field for ADDR_VEC and the second
2571 field for ADDR_DIFF_VEC. */
2573 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2574 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2575 insn, initial_p);
2576 return;
2578 case SET:
2579 /* We only care about setting PC. If the source is not RETURN,
2580 IF_THEN_ELSE, or a label, disable any eliminations not at
2581 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2582 isn't one of those possibilities. For branches to a label,
2583 call ourselves recursively.
2585 Note that this can disable elimination unnecessarily when we have
2586 a non-local goto since it will look like a non-constant jump to
2587 someplace in the current function. This isn't a significant
2588 problem since such jumps will normally be when all elimination
2589 pairs are back to their initial offsets. */
2591 if (SET_DEST (x) != pc_rtx)
2592 return;
2594 switch (GET_CODE (SET_SRC (x)))
2596 case PC:
2597 case RETURN:
2598 return;
2600 case LABEL_REF:
2601 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2602 return;
2604 case IF_THEN_ELSE:
2605 tem = XEXP (SET_SRC (x), 1);
2606 if (GET_CODE (tem) == LABEL_REF)
2607 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2608 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2609 break;
2611 tem = XEXP (SET_SRC (x), 2);
2612 if (GET_CODE (tem) == LABEL_REF)
2613 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2614 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2615 break;
2616 return;
2618 default:
2619 break;
2622 /* If we reach here, all eliminations must be at their initial
2623 offset because we are doing a jump to a variable address. */
2624 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2625 if (p->offset != p->initial_offset)
2626 p->can_eliminate = 0;
2627 break;
2629 default:
2630 break;
2634 /* Scan X and replace any eliminable registers (such as fp) with a
2635 replacement (such as sp), plus an offset.
2637 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2638 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2639 MEM, we are allowed to replace a sum of a register and the constant zero
2640 with the register, which we cannot do outside a MEM. In addition, we need
2641 to record the fact that a register is referenced outside a MEM.
2643 If INSN is an insn, it is the insn containing X. If we replace a REG
2644 in a SET_DEST with an equivalent MEM and INSN is non-zero, write a
2645 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2646 the REG is being modified.
2648 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2649 That's used when we eliminate in expressions stored in notes.
2650 This means, do not set ref_outside_mem even if the reference
2651 is outside of MEMs.
2653 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2654 replacements done assuming all offsets are at their initial values. If
2655 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2656 encounter, return the actual location so that find_reloads will do
2657 the proper thing. */
2660 eliminate_regs (x, mem_mode, insn)
2661 rtx x;
2662 enum machine_mode mem_mode;
2663 rtx insn;
2665 enum rtx_code code = GET_CODE (x);
2666 struct elim_table *ep;
2667 int regno;
2668 rtx new;
2669 int i, j;
2670 const char *fmt;
2671 int copied = 0;
2673 if (! current_function_decl)
2674 return x;
2676 switch (code)
2678 case CONST_INT:
2679 case CONST_DOUBLE:
2680 case CONST:
2681 case SYMBOL_REF:
2682 case CODE_LABEL:
2683 case PC:
2684 case CC0:
2685 case ASM_INPUT:
2686 case ADDR_VEC:
2687 case ADDR_DIFF_VEC:
2688 case RETURN:
2689 return x;
2691 case ADDRESSOF:
2692 /* This is only for the benefit of the debugging backends, which call
2693 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2694 removed after CSE. */
2695 new = eliminate_regs (XEXP (x, 0), 0, insn);
2696 if (GET_CODE (new) == MEM)
2697 return XEXP (new, 0);
2698 return x;
2700 case REG:
2701 regno = REGNO (x);
2703 /* First handle the case where we encounter a bare register that
2704 is eliminable. Replace it with a PLUS. */
2705 if (regno < FIRST_PSEUDO_REGISTER)
2707 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2708 ep++)
2709 if (ep->from_rtx == x && ep->can_eliminate)
2710 return plus_constant (ep->to_rtx, ep->previous_offset);
2713 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2714 && reg_equiv_constant[regno]
2715 && ! CONSTANT_P (reg_equiv_constant[regno]))
2716 return eliminate_regs (copy_rtx (reg_equiv_constant[regno]),
2717 mem_mode, insn);
2718 return x;
2720 /* You might think handling MINUS in a manner similar to PLUS is a
2721 good idea. It is not. It has been tried multiple times and every
2722 time the change has had to have been reverted.
2724 Other parts of reload know a PLUS is special (gen_reload for example)
2725 and require special code to handle code a reloaded PLUS operand.
2727 Also consider backends where the flags register is clobbered by a
2728 MINUS, but we can emit a PLUS that does not clobber flags (ia32,
2729 lea instruction comes to mind). If we try to reload a MINUS, we
2730 may kill the flags register that was holding a useful value.
2732 So, please before trying to handle MINUS, consider reload as a
2733 whole instead of this little section as well as the backend issues. */
2734 case PLUS:
2735 /* If this is the sum of an eliminable register and a constant, rework
2736 the sum. */
2737 if (GET_CODE (XEXP (x, 0)) == REG
2738 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2739 && CONSTANT_P (XEXP (x, 1)))
2741 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2742 ep++)
2743 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2745 /* The only time we want to replace a PLUS with a REG (this
2746 occurs when the constant operand of the PLUS is the negative
2747 of the offset) is when we are inside a MEM. We won't want
2748 to do so at other times because that would change the
2749 structure of the insn in a way that reload can't handle.
2750 We special-case the commonest situation in
2751 eliminate_regs_in_insn, so just replace a PLUS with a
2752 PLUS here, unless inside a MEM. */
2753 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2754 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2755 return ep->to_rtx;
2756 else
2757 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2758 plus_constant (XEXP (x, 1),
2759 ep->previous_offset));
2762 /* If the register is not eliminable, we are done since the other
2763 operand is a constant. */
2764 return x;
2767 /* If this is part of an address, we want to bring any constant to the
2768 outermost PLUS. We will do this by doing register replacement in
2769 our operands and seeing if a constant shows up in one of them.
2771 Note that there is no risk of modifying the structure of the insn,
2772 since we only get called for its operands, thus we are either
2773 modifying the address inside a MEM, or something like an address
2774 operand of a load-address insn. */
2777 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2778 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2780 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2782 /* If one side is a PLUS and the other side is a pseudo that
2783 didn't get a hard register but has a reg_equiv_constant,
2784 we must replace the constant here since it may no longer
2785 be in the position of any operand. */
2786 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2787 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2788 && reg_renumber[REGNO (new1)] < 0
2789 && reg_equiv_constant != 0
2790 && reg_equiv_constant[REGNO (new1)] != 0)
2791 new1 = reg_equiv_constant[REGNO (new1)];
2792 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2793 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2794 && reg_renumber[REGNO (new0)] < 0
2795 && reg_equiv_constant[REGNO (new0)] != 0)
2796 new0 = reg_equiv_constant[REGNO (new0)];
2798 new = form_sum (new0, new1);
2800 /* As above, if we are not inside a MEM we do not want to
2801 turn a PLUS into something else. We might try to do so here
2802 for an addition of 0 if we aren't optimizing. */
2803 if (! mem_mode && GET_CODE (new) != PLUS)
2804 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2805 else
2806 return new;
2809 return x;
2811 case MULT:
2812 /* If this is the product of an eliminable register and a
2813 constant, apply the distribute law and move the constant out
2814 so that we have (plus (mult ..) ..). This is needed in order
2815 to keep load-address insns valid. This case is pathological.
2816 We ignore the possibility of overflow here. */
2817 if (GET_CODE (XEXP (x, 0)) == REG
2818 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2819 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2820 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2821 ep++)
2822 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2824 if (! mem_mode
2825 /* Refs inside notes don't count for this purpose. */
2826 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2827 || GET_CODE (insn) == INSN_LIST)))
2828 ep->ref_outside_mem = 1;
2830 return
2831 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2832 ep->previous_offset * INTVAL (XEXP (x, 1)));
2835 /* ... fall through ... */
2837 case CALL:
2838 case COMPARE:
2839 /* See comments before PLUS about handling MINUS. */
2840 case MINUS:
2841 case DIV: case UDIV:
2842 case MOD: case UMOD:
2843 case AND: case IOR: case XOR:
2844 case ROTATERT: case ROTATE:
2845 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2846 case NE: case EQ:
2847 case GE: case GT: case GEU: case GTU:
2848 case LE: case LT: case LEU: case LTU:
2850 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2851 rtx new1
2852 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
2854 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2855 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2857 return x;
2859 case EXPR_LIST:
2860 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2861 if (XEXP (x, 0))
2863 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2864 if (new != XEXP (x, 0))
2866 /* If this is a REG_DEAD note, it is not valid anymore.
2867 Using the eliminated version could result in creating a
2868 REG_DEAD note for the stack or frame pointer. */
2869 if (GET_MODE (x) == REG_DEAD)
2870 return (XEXP (x, 1)
2871 ? eliminate_regs (XEXP (x, 1), mem_mode, insn)
2872 : NULL_RTX);
2874 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2878 /* ... fall through ... */
2880 case INSN_LIST:
2881 /* Now do eliminations in the rest of the chain. If this was
2882 an EXPR_LIST, this might result in allocating more memory than is
2883 strictly needed, but it simplifies the code. */
2884 if (XEXP (x, 1))
2886 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2887 if (new != XEXP (x, 1))
2888 return gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2890 return x;
2892 case PRE_INC:
2893 case POST_INC:
2894 case PRE_DEC:
2895 case POST_DEC:
2896 case STRICT_LOW_PART:
2897 case NEG: case NOT:
2898 case SIGN_EXTEND: case ZERO_EXTEND:
2899 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2900 case FLOAT: case FIX:
2901 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2902 case ABS:
2903 case SQRT:
2904 case FFS:
2905 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2906 if (new != XEXP (x, 0))
2907 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2908 return x;
2910 case SUBREG:
2911 /* Similar to above processing, but preserve SUBREG_WORD.
2912 Convert (subreg (mem)) to (mem) if not paradoxical.
2913 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2914 pseudo didn't get a hard reg, we must replace this with the
2915 eliminated version of the memory location because push_reloads
2916 may do the replacement in certain circumstances. */
2917 if (GET_CODE (SUBREG_REG (x)) == REG
2918 && (GET_MODE_SIZE (GET_MODE (x))
2919 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2920 && reg_equiv_memory_loc != 0
2921 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2923 new = SUBREG_REG (x);
2925 else
2926 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
2928 if (new != XEXP (x, 0))
2930 int x_size = GET_MODE_SIZE (GET_MODE (x));
2931 int new_size = GET_MODE_SIZE (GET_MODE (new));
2933 if (GET_CODE (new) == MEM
2934 && ((x_size < new_size
2935 #ifdef WORD_REGISTER_OPERATIONS
2936 /* On these machines, combine can create rtl of the form
2937 (set (subreg:m1 (reg:m2 R) 0) ...)
2938 where m1 < m2, and expects something interesting to
2939 happen to the entire word. Moreover, it will use the
2940 (reg:m2 R) later, expecting all bits to be preserved.
2941 So if the number of words is the same, preserve the
2942 subreg so that push_reloads can see it. */
2943 && ! ((x_size-1)/UNITS_PER_WORD == (new_size-1)/UNITS_PER_WORD)
2944 #endif
2946 || (x_size == new_size))
2949 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
2950 enum machine_mode mode = GET_MODE (x);
2952 if (BYTES_BIG_ENDIAN)
2953 offset += (MIN (UNITS_PER_WORD,
2954 GET_MODE_SIZE (GET_MODE (new)))
2955 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
2957 PUT_MODE (new, mode);
2958 XEXP (new, 0) = plus_constant (XEXP (new, 0), offset);
2959 return new;
2961 else
2962 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_WORD (x));
2965 return x;
2967 case MEM:
2968 /* This is only for the benefit of the debugging backends, which call
2969 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2970 removed after CSE. */
2971 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
2972 return eliminate_regs (XEXP (XEXP (x, 0), 0), 0, insn);
2974 /* Our only special processing is to pass the mode of the MEM to our
2975 recursive call and copy the flags. While we are here, handle this
2976 case more efficiently. */
2977 new = eliminate_regs (XEXP (x, 0), GET_MODE (x), insn);
2978 if (new != XEXP (x, 0))
2980 new = gen_rtx_MEM (GET_MODE (x), new);
2981 new->volatil = x->volatil;
2982 new->unchanging = x->unchanging;
2983 new->in_struct = x->in_struct;
2984 return new;
2986 else
2987 return x;
2989 case USE:
2990 case CLOBBER:
2991 case ASM_OPERANDS:
2992 case SET:
2993 abort ();
2995 default:
2996 break;
2999 /* Process each of our operands recursively. If any have changed, make a
3000 copy of the rtx. */
3001 fmt = GET_RTX_FORMAT (code);
3002 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3004 if (*fmt == 'e')
3006 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
3007 if (new != XEXP (x, i) && ! copied)
3009 rtx new_x = rtx_alloc (code);
3010 bcopy ((char *) x, (char *) new_x,
3011 (sizeof (*new_x) - sizeof (new_x->fld)
3012 + sizeof (new_x->fld[0]) * GET_RTX_LENGTH (code)));
3013 x = new_x;
3014 copied = 1;
3016 XEXP (x, i) = new;
3018 else if (*fmt == 'E')
3020 int copied_vec = 0;
3021 for (j = 0; j < XVECLEN (x, i); j++)
3023 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
3024 if (new != XVECEXP (x, i, j) && ! copied_vec)
3026 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
3027 XVEC (x, i)->elem);
3028 if (! copied)
3030 rtx new_x = rtx_alloc (code);
3031 bcopy ((char *) x, (char *) new_x,
3032 (sizeof (*new_x) - sizeof (new_x->fld)
3033 + (sizeof (new_x->fld[0])
3034 * GET_RTX_LENGTH (code))));
3035 x = new_x;
3036 copied = 1;
3038 XVEC (x, i) = new_v;
3039 copied_vec = 1;
3041 XVECEXP (x, i, j) = new;
3046 return x;
3049 /* Scan rtx X for modifications of elimination target registers. Update
3050 the table of eliminables to reflect the changed state. MEM_MODE is
3051 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
3053 static void
3054 elimination_effects (x, mem_mode)
3055 rtx x;
3056 enum machine_mode mem_mode;
3059 enum rtx_code code = GET_CODE (x);
3060 struct elim_table *ep;
3061 int regno;
3062 int i, j;
3063 const char *fmt;
3065 switch (code)
3067 case CONST_INT:
3068 case CONST_DOUBLE:
3069 case CONST:
3070 case SYMBOL_REF:
3071 case CODE_LABEL:
3072 case PC:
3073 case CC0:
3074 case ASM_INPUT:
3075 case ADDR_VEC:
3076 case ADDR_DIFF_VEC:
3077 case RETURN:
3078 return;
3080 case ADDRESSOF:
3081 abort ();
3083 case REG:
3084 regno = REGNO (x);
3086 /* First handle the case where we encounter a bare register that
3087 is eliminable. Replace it with a PLUS. */
3088 if (regno < FIRST_PSEUDO_REGISTER)
3090 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3091 ep++)
3092 if (ep->from_rtx == x && ep->can_eliminate)
3094 if (! mem_mode)
3095 ep->ref_outside_mem = 1;
3096 return;
3100 else if (reg_renumber[regno] < 0 && reg_equiv_constant
3101 && reg_equiv_constant[regno]
3102 && ! CONSTANT_P (reg_equiv_constant[regno]))
3103 elimination_effects (reg_equiv_constant[regno], mem_mode);
3104 return;
3106 case PRE_INC:
3107 case POST_INC:
3108 case PRE_DEC:
3109 case POST_DEC:
3110 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3111 if (ep->to_rtx == XEXP (x, 0))
3113 int size = GET_MODE_SIZE (mem_mode);
3115 /* If more bytes than MEM_MODE are pushed, account for them. */
3116 #ifdef PUSH_ROUNDING
3117 if (ep->to_rtx == stack_pointer_rtx)
3118 size = PUSH_ROUNDING (size);
3119 #endif
3120 if (code == PRE_DEC || code == POST_DEC)
3121 ep->offset += size;
3122 else
3123 ep->offset -= size;
3126 /* Fall through to generic unary operation case. */
3127 case STRICT_LOW_PART:
3128 case NEG: case NOT:
3129 case SIGN_EXTEND: case ZERO_EXTEND:
3130 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3131 case FLOAT: case FIX:
3132 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3133 case ABS:
3134 case SQRT:
3135 case FFS:
3136 elimination_effects (XEXP (x, 0), mem_mode);
3137 return;
3139 case SUBREG:
3140 if (GET_CODE (SUBREG_REG (x)) == REG
3141 && (GET_MODE_SIZE (GET_MODE (x))
3142 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3143 && reg_equiv_memory_loc != 0
3144 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
3145 return;
3147 elimination_effects (SUBREG_REG (x), mem_mode);
3148 return;
3150 case USE:
3151 /* If using a register that is the source of an eliminate we still
3152 think can be performed, note it cannot be performed since we don't
3153 know how this register is used. */
3154 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3155 if (ep->from_rtx == XEXP (x, 0))
3156 ep->can_eliminate = 0;
3158 elimination_effects (XEXP (x, 0), mem_mode);
3159 return;
3161 case CLOBBER:
3162 /* If clobbering a register that is the replacement register for an
3163 elimination we still think can be performed, note that it cannot
3164 be performed. Otherwise, we need not be concerned about it. */
3165 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3166 if (ep->to_rtx == XEXP (x, 0))
3167 ep->can_eliminate = 0;
3169 elimination_effects (XEXP (x, 0), mem_mode);
3170 return;
3172 case SET:
3173 /* Check for setting a register that we know about. */
3174 if (GET_CODE (SET_DEST (x)) == REG)
3176 /* See if this is setting the replacement register for an
3177 elimination.
3179 If DEST is the hard frame pointer, we do nothing because we
3180 assume that all assignments to the frame pointer are for
3181 non-local gotos and are being done at a time when they are valid
3182 and do not disturb anything else. Some machines want to
3183 eliminate a fake argument pointer (or even a fake frame pointer)
3184 with either the real frame or the stack pointer. Assignments to
3185 the hard frame pointer must not prevent this elimination. */
3187 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3188 ep++)
3189 if (ep->to_rtx == SET_DEST (x)
3190 && SET_DEST (x) != hard_frame_pointer_rtx)
3192 /* If it is being incremented, adjust the offset. Otherwise,
3193 this elimination can't be done. */
3194 rtx src = SET_SRC (x);
3196 if (GET_CODE (src) == PLUS
3197 && XEXP (src, 0) == SET_DEST (x)
3198 && GET_CODE (XEXP (src, 1)) == CONST_INT)
3199 ep->offset -= INTVAL (XEXP (src, 1));
3200 else
3201 ep->can_eliminate = 0;
3205 elimination_effects (SET_DEST (x), 0);
3206 elimination_effects (SET_SRC (x), 0);
3207 return;
3209 case MEM:
3210 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
3211 abort ();
3213 /* Our only special processing is to pass the mode of the MEM to our
3214 recursive call. */
3215 elimination_effects (XEXP (x, 0), GET_MODE (x));
3216 return;
3218 default:
3219 break;
3222 fmt = GET_RTX_FORMAT (code);
3223 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3225 if (*fmt == 'e')
3226 elimination_effects (XEXP (x, i), mem_mode);
3227 else if (*fmt == 'E')
3228 for (j = 0; j < XVECLEN (x, i); j++)
3229 elimination_effects (XVECEXP (x, i, j), mem_mode);
3233 /* Descend through rtx X and verify that no references to eliminable registers
3234 remain. If any do remain, mark the involved register as not
3235 eliminable. */
3236 static void
3237 check_eliminable_occurrences (x)
3238 rtx x;
3240 const char *fmt;
3241 int i;
3242 enum rtx_code code;
3244 if (x == 0)
3245 return;
3247 code = GET_CODE (x);
3249 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
3251 struct elim_table *ep;
3253 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3254 if (ep->from_rtx == x && ep->can_eliminate)
3255 ep->can_eliminate = 0;
3256 return;
3259 fmt = GET_RTX_FORMAT (code);
3260 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3262 if (*fmt == 'e')
3263 check_eliminable_occurrences (XEXP (x, i));
3264 else if (*fmt == 'E')
3266 int j;
3267 for (j = 0; j < XVECLEN (x, i); j++)
3268 check_eliminable_occurrences (XVECEXP (x, i, j));
3273 /* Scan INSN and eliminate all eliminable registers in it.
3275 If REPLACE is nonzero, do the replacement destructively. Also
3276 delete the insn as dead it if it is setting an eliminable register.
3278 If REPLACE is zero, do all our allocations in reload_obstack.
3280 If no eliminations were done and this insn doesn't require any elimination
3281 processing (these are not identical conditions: it might be updating sp,
3282 but not referencing fp; this needs to be seen during reload_as_needed so
3283 that the offset between fp and sp can be taken into consideration), zero
3284 is returned. Otherwise, 1 is returned. */
3286 static int
3287 eliminate_regs_in_insn (insn, replace)
3288 rtx insn;
3289 int replace;
3291 int icode = recog_memoized (insn);
3292 rtx old_body = PATTERN (insn);
3293 int insn_is_asm = asm_noperands (old_body) >= 0;
3294 rtx old_set = single_set (insn);
3295 rtx new_body;
3296 int val = 0;
3297 int i, any_changes;
3298 rtx substed_operand[MAX_RECOG_OPERANDS];
3299 rtx orig_operand[MAX_RECOG_OPERANDS];
3300 struct elim_table *ep;
3302 if (! insn_is_asm && icode < 0)
3304 if (GET_CODE (PATTERN (insn)) == USE
3305 || GET_CODE (PATTERN (insn)) == CLOBBER
3306 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3307 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3308 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
3309 return 0;
3310 abort ();
3313 if (! replace)
3314 push_obstacks (&reload_obstack, &reload_obstack);
3316 if (old_set != 0 && GET_CODE (SET_DEST (old_set)) == REG
3317 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3319 /* Check for setting an eliminable register. */
3320 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3321 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3323 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3324 /* If this is setting the frame pointer register to the
3325 hardware frame pointer register and this is an elimination
3326 that will be done (tested above), this insn is really
3327 adjusting the frame pointer downward to compensate for
3328 the adjustment done before a nonlocal goto. */
3329 if (ep->from == FRAME_POINTER_REGNUM
3330 && ep->to == HARD_FRAME_POINTER_REGNUM)
3332 rtx src = SET_SRC (old_set);
3333 int offset = 0, ok = 0;
3334 rtx prev_insn, prev_set;
3336 if (src == ep->to_rtx)
3337 offset = 0, ok = 1;
3338 else if (GET_CODE (src) == PLUS
3339 && GET_CODE (XEXP (src, 0)) == CONST_INT
3340 && XEXP (src, 1) == ep->to_rtx)
3341 offset = INTVAL (XEXP (src, 0)), ok = 1;
3342 else if (GET_CODE (src) == PLUS
3343 && GET_CODE (XEXP (src, 1)) == CONST_INT
3344 && XEXP (src, 0) == ep->to_rtx)
3345 offset = INTVAL (XEXP (src, 1)), ok = 1;
3346 else if ((prev_insn = prev_nonnote_insn (insn)) != 0
3347 && (prev_set = single_set (prev_insn)) != 0
3348 && rtx_equal_p (SET_DEST (prev_set), src))
3350 src = SET_SRC (prev_set);
3351 if (src == ep->to_rtx)
3352 offset = 0, ok = 1;
3353 else if (GET_CODE (src) == PLUS
3354 && GET_CODE (XEXP (src, 0)) == CONST_INT
3355 && XEXP (src, 1) == ep->to_rtx)
3356 offset = INTVAL (XEXP (src, 0)), ok = 1;
3357 else if (GET_CODE (src) == PLUS
3358 && GET_CODE (XEXP (src, 1)) == CONST_INT
3359 && XEXP (src, 0) == ep->to_rtx)
3360 offset = INTVAL (XEXP (src, 1)), ok = 1;
3363 if (ok)
3365 if (replace)
3367 rtx src
3368 = plus_constant (ep->to_rtx, offset - ep->offset);
3370 /* First see if this insn remains valid when we
3371 make the change. If not, keep the INSN_CODE
3372 the same and let reload fit it up. */
3373 validate_change (insn, &SET_SRC (old_set), src, 1);
3374 validate_change (insn, &SET_DEST (old_set),
3375 ep->to_rtx, 1);
3376 if (! apply_change_group ())
3378 SET_SRC (old_set) = src;
3379 SET_DEST (old_set) = ep->to_rtx;
3383 val = 1;
3384 goto done;
3387 #endif
3389 /* In this case this insn isn't serving a useful purpose. We
3390 will delete it in reload_as_needed once we know that this
3391 elimination is, in fact, being done.
3393 If REPLACE isn't set, we can't delete this insn, but needn't
3394 process it since it won't be used unless something changes. */
3395 if (replace)
3397 delete_dead_insn (insn);
3398 return 1;
3400 val = 1;
3401 goto done;
3404 /* Check for (set (reg) (plus (reg from) (offset))) where the offset
3405 in the insn is the negative of the offset in FROM. Substitute
3406 (set (reg) (reg to)) for the insn and change its code.
3408 We have to do this here, rather than in eliminate_regs, so that we can
3409 change the insn code. */
3411 if (GET_CODE (SET_SRC (old_set)) == PLUS
3412 && GET_CODE (XEXP (SET_SRC (old_set), 0)) == REG
3413 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT)
3414 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3415 ep++)
3416 if (ep->from_rtx == XEXP (SET_SRC (old_set), 0)
3417 && ep->can_eliminate)
3419 /* We must stop at the first elimination that will be used.
3420 If this one would replace the PLUS with a REG, do it
3421 now. Otherwise, quit the loop and let eliminate_regs
3422 do its normal replacement. */
3423 if (ep->offset == - INTVAL (XEXP (SET_SRC (old_set), 1)))
3425 /* We assume here that we don't need a PARALLEL of
3426 any CLOBBERs for this assignment. There's not
3427 much we can do if we do need it. */
3428 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3429 SET_DEST (old_set),
3430 ep->to_rtx);
3431 INSN_CODE (insn) = -1;
3432 val = 1;
3433 goto done;
3436 break;
3440 /* Determine the effects of this insn on elimination offsets. */
3441 elimination_effects (old_body, 0);
3443 /* Eliminate all eliminable registers occurring in operands that
3444 can be handled by reload. */
3445 extract_insn (insn);
3446 any_changes = 0;
3447 for (i = 0; i < recog_data.n_operands; i++)
3449 orig_operand[i] = recog_data.operand[i];
3450 substed_operand[i] = recog_data.operand[i];
3452 /* For an asm statement, every operand is eliminable. */
3453 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3455 /* Check for setting a register that we know about. */
3456 if (recog_data.operand_type[i] != OP_IN
3457 && GET_CODE (orig_operand[i]) == REG)
3459 /* If we are assigning to a register that can be eliminated, it
3460 must be as part of a PARALLEL, since the code above handles
3461 single SETs. We must indicate that we can no longer
3462 eliminate this reg. */
3463 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3464 ep++)
3465 if (ep->from_rtx == orig_operand[i] && ep->can_eliminate)
3466 ep->can_eliminate = 0;
3469 substed_operand[i] = eliminate_regs (recog_data.operand[i], 0,
3470 replace ? insn : NULL_RTX);
3471 if (substed_operand[i] != orig_operand[i])
3472 val = any_changes = 1;
3473 /* Terminate the search in check_eliminable_occurrences at
3474 this point. */
3475 *recog_data.operand_loc[i] = 0;
3477 /* If an output operand changed from a REG to a MEM and INSN is an
3478 insn, write a CLOBBER insn. */
3479 if (recog_data.operand_type[i] != OP_IN
3480 && GET_CODE (orig_operand[i]) == REG
3481 && GET_CODE (substed_operand[i]) == MEM
3482 && replace)
3483 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3484 insn);
3488 for (i = 0; i < recog_data.n_dups; i++)
3489 *recog_data.dup_loc[i]
3490 = *recog_data.operand_loc[(int)recog_data.dup_num[i]];
3492 /* If any eliminable remain, they aren't eliminable anymore. */
3493 check_eliminable_occurrences (old_body);
3495 /* Substitute the operands; the new values are in the substed_operand
3496 array. */
3497 for (i = 0; i < recog_data.n_operands; i++)
3498 *recog_data.operand_loc[i] = substed_operand[i];
3499 for (i = 0; i < recog_data.n_dups; i++)
3500 *recog_data.dup_loc[i] = substed_operand[(int)recog_data.dup_num[i]];
3502 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3503 re-recognize the insn. We do this in case we had a simple addition
3504 but now can do this as a load-address. This saves an insn in this
3505 common case.
3506 If re-recognition fails, the old insn code number will still be used,
3507 and some register operands may have changed into PLUS expressions.
3508 These will be handled by find_reloads by loading them into a register
3509 again.*/
3511 if (val)
3513 /* If we aren't replacing things permanently and we changed something,
3514 make another copy to ensure that all the RTL is new. Otherwise
3515 things can go wrong if find_reload swaps commutative operands
3516 and one is inside RTL that has been copied while the other is not. */
3517 new_body = old_body;
3518 if (! replace)
3520 new_body = copy_insn (old_body);
3521 if (REG_NOTES (insn))
3522 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3524 PATTERN (insn) = new_body;
3526 /* If we had a move insn but now we don't, rerecognize it. This will
3527 cause spurious re-recognition if the old move had a PARALLEL since
3528 the new one still will, but we can't call single_set without
3529 having put NEW_BODY into the insn and the re-recognition won't
3530 hurt in this rare case. */
3531 /* ??? Why this huge if statement - why don't we just rerecognize the
3532 thing always? */
3533 if (! insn_is_asm
3534 && old_set != 0
3535 && ((GET_CODE (SET_SRC (old_set)) == REG
3536 && (GET_CODE (new_body) != SET
3537 || GET_CODE (SET_SRC (new_body)) != REG))
3538 /* If this was a load from or store to memory, compare
3539 the MEM in recog_data.operand to the one in the insn.
3540 If they are not equal, then rerecognize the insn. */
3541 || (old_set != 0
3542 && ((GET_CODE (SET_SRC (old_set)) == MEM
3543 && SET_SRC (old_set) != recog_data.operand[1])
3544 || (GET_CODE (SET_DEST (old_set)) == MEM
3545 && SET_DEST (old_set) != recog_data.operand[0])))
3546 /* If this was an add insn before, rerecognize. */
3547 || GET_CODE (SET_SRC (old_set)) == PLUS))
3549 int new_icode = recog (PATTERN (insn), insn, 0);
3550 if (new_icode < 0)
3551 INSN_CODE (insn) = icode;
3555 /* Restore the old body. If there were any changes to it, we made a copy
3556 of it while the changes were still in place, so we'll correctly return
3557 a modified insn below. */
3558 if (! replace)
3560 /* Restore the old body. */
3561 for (i = 0; i < recog_data.n_operands; i++)
3562 *recog_data.operand_loc[i] = orig_operand[i];
3563 for (i = 0; i < recog_data.n_dups; i++)
3564 *recog_data.dup_loc[i] = orig_operand[(int)recog_data.dup_num[i]];
3567 /* Update all elimination pairs to reflect the status after the current
3568 insn. The changes we make were determined by the earlier call to
3569 elimination_effects.
3571 We also detect a cases where register elimination cannot be done,
3572 namely, if a register would be both changed and referenced outside a MEM
3573 in the resulting insn since such an insn is often undefined and, even if
3574 not, we cannot know what meaning will be given to it. Note that it is
3575 valid to have a register used in an address in an insn that changes it
3576 (presumably with a pre- or post-increment or decrement).
3578 If anything changes, return nonzero. */
3580 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3582 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3583 ep->can_eliminate = 0;
3585 ep->ref_outside_mem = 0;
3587 if (ep->previous_offset != ep->offset)
3588 val = 1;
3591 done:
3592 /* If we changed something, perform elimination in REG_NOTES. This is
3593 needed even when REPLACE is zero because a REG_DEAD note might refer
3594 to a register that we eliminate and could cause a different number
3595 of spill registers to be needed in the final reload pass than in
3596 the pre-passes. */
3597 if (val && REG_NOTES (insn) != 0)
3598 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
3600 if (! replace)
3601 pop_obstacks ();
3603 return val;
3606 /* Loop through all elimination pairs.
3607 Recalculate the number not at initial offset.
3609 Compute the maximum offset (minimum offset if the stack does not
3610 grow downward) for each elimination pair. */
3612 static void
3613 update_eliminable_offsets ()
3615 struct elim_table *ep;
3617 num_not_at_initial_offset = 0;
3618 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3620 ep->previous_offset = ep->offset;
3621 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3622 num_not_at_initial_offset++;
3626 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3627 replacement we currently believe is valid, mark it as not eliminable if X
3628 modifies DEST in any way other than by adding a constant integer to it.
3630 If DEST is the frame pointer, we do nothing because we assume that
3631 all assignments to the hard frame pointer are nonlocal gotos and are being
3632 done at a time when they are valid and do not disturb anything else.
3633 Some machines want to eliminate a fake argument pointer with either the
3634 frame or stack pointer. Assignments to the hard frame pointer must not
3635 prevent this elimination.
3637 Called via note_stores from reload before starting its passes to scan
3638 the insns of the function. */
3640 static void
3641 mark_not_eliminable (dest, x, data)
3642 rtx dest;
3643 rtx x;
3644 void *data ATTRIBUTE_UNUSED;
3646 register unsigned int i;
3648 /* A SUBREG of a hard register here is just changing its mode. We should
3649 not see a SUBREG of an eliminable hard register, but check just in
3650 case. */
3651 if (GET_CODE (dest) == SUBREG)
3652 dest = SUBREG_REG (dest);
3654 if (dest == hard_frame_pointer_rtx)
3655 return;
3657 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3658 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3659 && (GET_CODE (x) != SET
3660 || GET_CODE (SET_SRC (x)) != PLUS
3661 || XEXP (SET_SRC (x), 0) != dest
3662 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3664 reg_eliminate[i].can_eliminate_previous
3665 = reg_eliminate[i].can_eliminate = 0;
3666 num_eliminable--;
3670 /* Verify that the initial elimination offsets did not change since the
3671 last call to set_initial_elim_offsets. This is used to catch cases
3672 where something illegal happened during reload_as_needed that could
3673 cause incorrect code to be generated if we did not check for it. */
3674 static void
3675 verify_initial_elim_offsets ()
3677 int t;
3679 #ifdef ELIMINABLE_REGS
3680 struct elim_table *ep;
3682 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3684 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3685 if (t != ep->initial_offset)
3686 abort ();
3688 #else
3689 INITIAL_FRAME_POINTER_OFFSET (t);
3690 if (t != reg_eliminate[0].initial_offset)
3691 abort ();
3692 #endif
3695 /* Reset all offsets on eliminable registers to their initial values. */
3696 static void
3697 set_initial_elim_offsets ()
3699 struct elim_table *ep = reg_eliminate;
3701 #ifdef ELIMINABLE_REGS
3702 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3704 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3705 ep->previous_offset = ep->offset = ep->initial_offset;
3707 #else
3708 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3709 ep->previous_offset = ep->offset = ep->initial_offset;
3710 #endif
3712 num_not_at_initial_offset = 0;
3715 /* Initialize the known label offsets.
3716 Set a known offset for each forced label to be at the initial offset
3717 of each elimination. We do this because we assume that all
3718 computed jumps occur from a location where each elimination is
3719 at its initial offset.
3720 For all other labels, show that we don't know the offsets. */
3722 static void
3723 set_initial_label_offsets ()
3725 rtx x;
3726 bzero ((char *) &offsets_known_at[get_first_label_num ()], num_labels);
3728 for (x = forced_labels; x; x = XEXP (x, 1))
3729 if (XEXP (x, 0))
3730 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3733 /* Set all elimination offsets to the known values for the code label given
3734 by INSN. */
3735 static void
3736 set_offsets_for_label (insn)
3737 rtx insn;
3739 unsigned int i;
3740 int label_nr = CODE_LABEL_NUMBER (insn);
3741 struct elim_table *ep;
3743 num_not_at_initial_offset = 0;
3744 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3746 ep->offset = ep->previous_offset = offsets_at[label_nr][i];
3747 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3748 num_not_at_initial_offset++;
3752 /* See if anything that happened changes which eliminations are valid.
3753 For example, on the Sparc, whether or not the frame pointer can
3754 be eliminated can depend on what registers have been used. We need
3755 not check some conditions again (such as flag_omit_frame_pointer)
3756 since they can't have changed. */
3758 static void
3759 update_eliminables (pset)
3760 HARD_REG_SET *pset;
3762 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3763 int previous_frame_pointer_needed = frame_pointer_needed;
3764 #endif
3765 struct elim_table *ep;
3767 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3768 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3769 #ifdef ELIMINABLE_REGS
3770 || ! CAN_ELIMINATE (ep->from, ep->to)
3771 #endif
3773 ep->can_eliminate = 0;
3775 /* Look for the case where we have discovered that we can't replace
3776 register A with register B and that means that we will now be
3777 trying to replace register A with register C. This means we can
3778 no longer replace register C with register B and we need to disable
3779 such an elimination, if it exists. This occurs often with A == ap,
3780 B == sp, and C == fp. */
3782 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3784 struct elim_table *op;
3785 register int new_to = -1;
3787 if (! ep->can_eliminate && ep->can_eliminate_previous)
3789 /* Find the current elimination for ep->from, if there is a
3790 new one. */
3791 for (op = reg_eliminate;
3792 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3793 if (op->from == ep->from && op->can_eliminate)
3795 new_to = op->to;
3796 break;
3799 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3800 disable it. */
3801 for (op = reg_eliminate;
3802 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3803 if (op->from == new_to && op->to == ep->to)
3804 op->can_eliminate = 0;
3808 /* See if any registers that we thought we could eliminate the previous
3809 time are no longer eliminable. If so, something has changed and we
3810 must spill the register. Also, recompute the number of eliminable
3811 registers and see if the frame pointer is needed; it is if there is
3812 no elimination of the frame pointer that we can perform. */
3814 frame_pointer_needed = 1;
3815 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3817 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3818 && ep->to != HARD_FRAME_POINTER_REGNUM)
3819 frame_pointer_needed = 0;
3821 if (! ep->can_eliminate && ep->can_eliminate_previous)
3823 ep->can_eliminate_previous = 0;
3824 SET_HARD_REG_BIT (*pset, ep->from);
3825 num_eliminable--;
3829 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3830 /* If we didn't need a frame pointer last time, but we do now, spill
3831 the hard frame pointer. */
3832 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3833 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3834 #endif
3837 /* Initialize the table of registers to eliminate. */
3838 static void
3839 init_elim_table ()
3841 struct elim_table *ep;
3842 #ifdef ELIMINABLE_REGS
3843 struct elim_table_1 *ep1;
3844 #endif
3846 if (!reg_eliminate)
3847 reg_eliminate = (struct elim_table *)
3848 xcalloc(sizeof(struct elim_table), NUM_ELIMINABLE_REGS);
3850 /* Does this function require a frame pointer? */
3852 frame_pointer_needed = (! flag_omit_frame_pointer
3853 #ifdef EXIT_IGNORE_STACK
3854 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3855 and restore sp for alloca. So we can't eliminate
3856 the frame pointer in that case. At some point,
3857 we should improve this by emitting the
3858 sp-adjusting insns for this case. */
3859 || (current_function_calls_alloca
3860 && EXIT_IGNORE_STACK)
3861 #endif
3862 || FRAME_POINTER_REQUIRED);
3864 num_eliminable = 0;
3866 #ifdef ELIMINABLE_REGS
3867 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3868 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3870 ep->from = ep1->from;
3871 ep->to = ep1->to;
3872 ep->can_eliminate = ep->can_eliminate_previous
3873 = (CAN_ELIMINATE (ep->from, ep->to)
3874 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3876 #else
3877 reg_eliminate[0].from = reg_eliminate_1[0].from;
3878 reg_eliminate[0].to = reg_eliminate_1[0].to;
3879 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3880 = ! frame_pointer_needed;
3881 #endif
3883 /* Count the number of eliminable registers and build the FROM and TO
3884 REG rtx's. Note that code in gen_rtx will cause, e.g.,
3885 gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3886 We depend on this. */
3887 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3889 num_eliminable += ep->can_eliminate;
3890 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3891 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3895 /* Kick all pseudos out of hard register REGNO.
3896 If DUMPFILE is nonzero, log actions taken on that file.
3898 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3899 because we found we can't eliminate some register. In the case, no pseudos
3900 are allowed to be in the register, even if they are only in a block that
3901 doesn't require spill registers, unlike the case when we are spilling this
3902 hard reg to produce another spill register.
3904 Return nonzero if any pseudos needed to be kicked out. */
3906 static void
3907 spill_hard_reg (regno, dumpfile, cant_eliminate)
3908 register int regno;
3909 FILE *dumpfile ATTRIBUTE_UNUSED;
3910 int cant_eliminate;
3912 register int i;
3914 if (cant_eliminate)
3916 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3917 regs_ever_live[regno] = 1;
3920 /* Spill every pseudo reg that was allocated to this reg
3921 or to something that overlaps this reg. */
3923 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3924 if (reg_renumber[i] >= 0
3925 && reg_renumber[i] <= regno
3926 && (reg_renumber[i]
3927 + HARD_REGNO_NREGS (reg_renumber[i],
3928 PSEUDO_REGNO_MODE (i))
3929 > regno))
3930 SET_REGNO_REG_SET (spilled_pseudos, i);
3933 /* I'm getting weird preprocessor errors if I use IOR_HARD_REG_SET
3934 from within EXECUTE_IF_SET_IN_REG_SET. Hence this awkwardness. */
3935 static void
3936 ior_hard_reg_set (set1, set2)
3937 HARD_REG_SET *set1, *set2;
3939 IOR_HARD_REG_SET (*set1, *set2);
3942 /* After find_reload_regs has been run for all insn that need reloads,
3943 and/or spill_hard_regs was called, this function is used to actually
3944 spill pseudo registers and try to reallocate them. It also sets up the
3945 spill_regs array for use by choose_reload_regs. */
3947 static int
3948 finish_spills (global, dumpfile)
3949 int global;
3950 FILE *dumpfile;
3952 struct insn_chain *chain;
3953 int something_changed = 0;
3954 int i;
3956 /* Build the spill_regs array for the function. */
3957 /* If there are some registers still to eliminate and one of the spill regs
3958 wasn't ever used before, additional stack space may have to be
3959 allocated to store this register. Thus, we may have changed the offset
3960 between the stack and frame pointers, so mark that something has changed.
3962 One might think that we need only set VAL to 1 if this is a call-used
3963 register. However, the set of registers that must be saved by the
3964 prologue is not identical to the call-used set. For example, the
3965 register used by the call insn for the return PC is a call-used register,
3966 but must be saved by the prologue. */
3968 n_spills = 0;
3969 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3970 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3972 spill_reg_order[i] = n_spills;
3973 spill_regs[n_spills++] = i;
3974 if (num_eliminable && ! regs_ever_live[i])
3975 something_changed = 1;
3976 regs_ever_live[i] = 1;
3978 else
3979 spill_reg_order[i] = -1;
3981 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3982 if (REGNO_REG_SET_P (spilled_pseudos, i))
3984 /* Record the current hard register the pseudo is allocated to in
3985 pseudo_previous_regs so we avoid reallocating it to the same
3986 hard reg in a later pass. */
3987 if (reg_renumber[i] < 0)
3988 abort ();
3989 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3990 /* Mark it as no longer having a hard register home. */
3991 reg_renumber[i] = -1;
3992 /* We will need to scan everything again. */
3993 something_changed = 1;
3996 /* Retry global register allocation if possible. */
3997 if (global)
3999 bzero ((char *) pseudo_forbidden_regs, max_regno * sizeof (HARD_REG_SET));
4000 /* For every insn that needs reloads, set the registers used as spill
4001 regs in pseudo_forbidden_regs for every pseudo live across the
4002 insn. */
4003 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
4005 EXECUTE_IF_SET_IN_REG_SET
4006 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
4008 ior_hard_reg_set (pseudo_forbidden_regs + i,
4009 &chain->used_spill_regs);
4011 EXECUTE_IF_SET_IN_REG_SET
4012 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
4014 ior_hard_reg_set (pseudo_forbidden_regs + i,
4015 &chain->used_spill_regs);
4019 /* Retry allocating the spilled pseudos. For each reg, merge the
4020 various reg sets that indicate which hard regs can't be used,
4021 and call retry_global_alloc.
4022 We change spill_pseudos here to only contain pseudos that did not
4023 get a new hard register. */
4024 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4025 if (reg_old_renumber[i] != reg_renumber[i])
4027 HARD_REG_SET forbidden;
4028 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
4029 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
4030 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
4031 retry_global_alloc (i, forbidden);
4032 if (reg_renumber[i] >= 0)
4033 CLEAR_REGNO_REG_SET (spilled_pseudos, i);
4037 /* Fix up the register information in the insn chain.
4038 This involves deleting those of the spilled pseudos which did not get
4039 a new hard register home from the live_{before,after} sets. */
4040 for (chain = reload_insn_chain; chain; chain = chain->next)
4042 HARD_REG_SET used_by_pseudos;
4043 HARD_REG_SET used_by_pseudos2;
4045 AND_COMPL_REG_SET (chain->live_before, spilled_pseudos);
4046 AND_COMPL_REG_SET (chain->live_after, spilled_pseudos);
4048 /* Mark any unallocated hard regs as available for spills. That
4049 makes inheritance work somewhat better. */
4050 if (chain->need_reload)
4052 REG_SET_TO_HARD_REG_SET (used_by_pseudos, chain->live_before);
4053 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, chain->live_after);
4054 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
4056 /* Save the old value for the sanity test below. */
4057 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
4059 compute_use_by_pseudos (&used_by_pseudos, chain->live_before);
4060 compute_use_by_pseudos (&used_by_pseudos, chain->live_after);
4061 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
4062 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
4064 /* Make sure we only enlarge the set. */
4065 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
4066 abort ();
4067 ok:;
4071 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
4072 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4074 int regno = reg_renumber[i];
4075 if (reg_old_renumber[i] == regno)
4076 continue;
4078 alter_reg (i, reg_old_renumber[i]);
4079 reg_old_renumber[i] = regno;
4080 if (dumpfile)
4082 if (regno == -1)
4083 fprintf (dumpfile, " Register %d now on stack.\n\n", i);
4084 else
4085 fprintf (dumpfile, " Register %d now in %d.\n\n",
4086 i, reg_renumber[i]);
4090 return something_changed;
4093 /* Find all paradoxical subregs within X and update reg_max_ref_width.
4094 Also mark any hard registers used to store user variables as
4095 forbidden from being used for spill registers. */
4097 static void
4098 scan_paradoxical_subregs (x)
4099 register rtx x;
4101 register int i;
4102 register const char *fmt;
4103 register enum rtx_code code = GET_CODE (x);
4105 switch (code)
4107 case REG:
4108 #if 0
4109 if (SMALL_REGISTER_CLASSES && REGNO (x) < FIRST_PSEUDO_REGISTER
4110 && REG_USERVAR_P (x))
4111 SET_HARD_REG_BIT (bad_spill_regs_global, REGNO (x));
4112 #endif
4113 return;
4115 case CONST_INT:
4116 case CONST:
4117 case SYMBOL_REF:
4118 case LABEL_REF:
4119 case CONST_DOUBLE:
4120 case CC0:
4121 case PC:
4122 case USE:
4123 case CLOBBER:
4124 return;
4126 case SUBREG:
4127 if (GET_CODE (SUBREG_REG (x)) == REG
4128 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
4129 reg_max_ref_width[REGNO (SUBREG_REG (x))]
4130 = GET_MODE_SIZE (GET_MODE (x));
4131 return;
4133 default:
4134 break;
4137 fmt = GET_RTX_FORMAT (code);
4138 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4140 if (fmt[i] == 'e')
4141 scan_paradoxical_subregs (XEXP (x, i));
4142 else if (fmt[i] == 'E')
4144 register int j;
4145 for (j = XVECLEN (x, i) - 1; j >=0; j--)
4146 scan_paradoxical_subregs (XVECEXP (x, i, j));
4151 static int
4152 hard_reg_use_compare (p1p, p2p)
4153 const PTR p1p;
4154 const PTR p2p;
4156 const struct hard_reg_n_uses *p1 = (const struct hard_reg_n_uses *)p1p;
4157 const struct hard_reg_n_uses *p2 = (const struct hard_reg_n_uses *)p2p;
4158 int bad1 = TEST_HARD_REG_BIT (bad_spill_regs, p1->regno);
4159 int bad2 = TEST_HARD_REG_BIT (bad_spill_regs, p2->regno);
4160 if (bad1 && bad2)
4161 return p1->regno - p2->regno;
4162 if (bad1)
4163 return 1;
4164 if (bad2)
4165 return -1;
4166 if (p1->uses > p2->uses)
4167 return 1;
4168 if (p1->uses < p2->uses)
4169 return -1;
4170 /* If regs are equally good, sort by regno,
4171 so that the results of qsort leave nothing to chance. */
4172 return p1->regno - p2->regno;
4175 /* Used for communication between order_regs_for_reload and count_pseudo.
4176 Used to avoid counting one pseudo twice. */
4177 static regset pseudos_counted;
4179 /* Update the costs in N_USES, considering that pseudo REG is live. */
4180 static void
4181 count_pseudo (n_uses, reg)
4182 struct hard_reg_n_uses *n_uses;
4183 int reg;
4185 int r = reg_renumber[reg];
4186 int nregs;
4188 if (REGNO_REG_SET_P (pseudos_counted, reg))
4189 return;
4190 SET_REGNO_REG_SET (pseudos_counted, reg);
4192 if (r < 0)
4193 abort ();
4195 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
4196 while (nregs-- > 0)
4197 n_uses[r++].uses += REG_N_REFS (reg);
4199 /* Choose the order to consider regs for use as reload registers
4200 based on how much trouble would be caused by spilling one.
4201 Store them in order of decreasing preference in potential_reload_regs. */
4203 static void
4204 order_regs_for_reload (chain)
4205 struct insn_chain *chain;
4207 register int i;
4208 register int o = 0;
4209 struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
4211 pseudos_counted = ALLOCA_REG_SET ();
4213 COPY_HARD_REG_SET (bad_spill_regs, bad_spill_regs_global);
4215 /* Count number of uses of each hard reg by pseudo regs allocated to it
4216 and then order them by decreasing use. */
4218 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4220 hard_reg_n_uses[i].regno = i;
4221 hard_reg_n_uses[i].uses = 0;
4223 /* Test the various reasons why we can't use a register for
4224 spilling in this insn. */
4225 if (fixed_regs[i]
4226 || REGNO_REG_SET_P (chain->live_before, i)
4227 || REGNO_REG_SET_P (chain->live_after, i))
4228 SET_HARD_REG_BIT (bad_spill_regs, i);
4231 /* Now compute hard_reg_n_uses. */
4232 CLEAR_REG_SET (pseudos_counted);
4234 EXECUTE_IF_SET_IN_REG_SET
4235 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
4237 count_pseudo (hard_reg_n_uses, i);
4239 EXECUTE_IF_SET_IN_REG_SET
4240 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
4242 count_pseudo (hard_reg_n_uses, i);
4245 FREE_REG_SET (pseudos_counted);
4247 /* Prefer registers not so far used, for use in temporary loading.
4248 Among them, if REG_ALLOC_ORDER is defined, use that order.
4249 Otherwise, prefer registers not preserved by calls. */
4251 #ifdef REG_ALLOC_ORDER
4252 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4254 int regno = reg_alloc_order[i];
4256 if (hard_reg_n_uses[regno].uses == 0
4257 && ! TEST_HARD_REG_BIT (bad_spill_regs, regno))
4258 potential_reload_regs[o++] = regno;
4260 #else
4261 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4263 if (hard_reg_n_uses[i].uses == 0 && call_used_regs[i]
4264 && ! TEST_HARD_REG_BIT (bad_spill_regs, i))
4265 potential_reload_regs[o++] = i;
4267 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4269 if (hard_reg_n_uses[i].uses == 0 && ! call_used_regs[i]
4270 && ! TEST_HARD_REG_BIT (bad_spill_regs, i))
4271 potential_reload_regs[o++] = i;
4273 #endif
4275 qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
4276 sizeof hard_reg_n_uses[0], hard_reg_use_compare);
4278 /* Now add the regs that are already used,
4279 preferring those used less often. The fixed and otherwise forbidden
4280 registers will be at the end of this list. */
4282 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4283 if (hard_reg_n_uses[i].uses != 0
4284 && ! TEST_HARD_REG_BIT (bad_spill_regs, hard_reg_n_uses[i].regno))
4285 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
4286 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4287 if (TEST_HARD_REG_BIT (bad_spill_regs, hard_reg_n_uses[i].regno))
4288 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
4291 /* Reload pseudo-registers into hard regs around each insn as needed.
4292 Additional register load insns are output before the insn that needs it
4293 and perhaps store insns after insns that modify the reloaded pseudo reg.
4295 reg_last_reload_reg and reg_reloaded_contents keep track of
4296 which registers are already available in reload registers.
4297 We update these for the reloads that we perform,
4298 as the insns are scanned. */
4300 static void
4301 reload_as_needed (live_known)
4302 int live_known;
4304 struct insn_chain *chain;
4305 #if defined (AUTO_INC_DEC)
4306 register int i;
4307 #endif
4308 rtx x;
4310 bzero ((char *) spill_reg_rtx, sizeof spill_reg_rtx);
4311 bzero ((char *) spill_reg_store, sizeof spill_reg_store);
4312 reg_last_reload_reg = (rtx *) xcalloc (max_regno, sizeof (rtx));
4313 reg_has_output_reload = (char *) xmalloc (max_regno);
4314 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4316 set_initial_elim_offsets ();
4318 for (chain = reload_insn_chain; chain; chain = chain->next)
4320 rtx prev;
4321 rtx insn = chain->insn;
4322 rtx old_next = NEXT_INSN (insn);
4324 /* If we pass a label, copy the offsets from the label information
4325 into the current offsets of each elimination. */
4326 if (GET_CODE (insn) == CODE_LABEL)
4327 set_offsets_for_label (insn);
4329 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4331 rtx oldpat = PATTERN (insn);
4333 /* If this is a USE and CLOBBER of a MEM, ensure that any
4334 references to eliminable registers have been removed. */
4336 if ((GET_CODE (PATTERN (insn)) == USE
4337 || GET_CODE (PATTERN (insn)) == CLOBBER)
4338 && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
4339 XEXP (XEXP (PATTERN (insn), 0), 0)
4340 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
4341 GET_MODE (XEXP (PATTERN (insn), 0)),
4342 NULL_RTX);
4344 /* If we need to do register elimination processing, do so.
4345 This might delete the insn, in which case we are done. */
4346 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
4348 eliminate_regs_in_insn (insn, 1);
4349 if (GET_CODE (insn) == NOTE)
4351 update_eliminable_offsets ();
4352 continue;
4356 /* If need_elim is nonzero but need_reload is zero, one might think
4357 that we could simply set n_reloads to 0. However, find_reloads
4358 could have done some manipulation of the insn (such as swapping
4359 commutative operands), and these manipulations are lost during
4360 the first pass for every insn that needs register elimination.
4361 So the actions of find_reloads must be redone here. */
4363 if (! chain->need_elim && ! chain->need_reload
4364 && ! chain->need_operand_change)
4365 n_reloads = 0;
4366 /* First find the pseudo regs that must be reloaded for this insn.
4367 This info is returned in the tables reload_... (see reload.h).
4368 Also modify the body of INSN by substituting RELOAD
4369 rtx's for those pseudo regs. */
4370 else
4372 bzero (reg_has_output_reload, max_regno);
4373 CLEAR_HARD_REG_SET (reg_is_output_reload);
4375 find_reloads (insn, 1, spill_indirect_levels, live_known,
4376 spill_reg_order);
4379 if (num_eliminable && chain->need_elim)
4380 update_eliminable_offsets ();
4382 if (n_reloads > 0)
4384 rtx next = NEXT_INSN (insn);
4385 rtx p;
4387 prev = PREV_INSN (insn);
4389 /* Now compute which reload regs to reload them into. Perhaps
4390 reusing reload regs from previous insns, or else output
4391 load insns to reload them. Maybe output store insns too.
4392 Record the choices of reload reg in reload_reg_rtx. */
4393 choose_reload_regs (chain);
4395 /* Merge any reloads that we didn't combine for fear of
4396 increasing the number of spill registers needed but now
4397 discover can be safely merged. */
4398 if (SMALL_REGISTER_CLASSES)
4399 merge_assigned_reloads (insn);
4401 /* Generate the insns to reload operands into or out of
4402 their reload regs. */
4403 emit_reload_insns (chain);
4405 /* Substitute the chosen reload regs from reload_reg_rtx
4406 into the insn's body (or perhaps into the bodies of other
4407 load and store insn that we just made for reloading
4408 and that we moved the structure into). */
4409 subst_reloads ();
4411 /* If this was an ASM, make sure that all the reload insns
4412 we have generated are valid. If not, give an error
4413 and delete them. */
4415 if (asm_noperands (PATTERN (insn)) >= 0)
4416 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4417 if (p != insn && GET_RTX_CLASS (GET_CODE (p)) == 'i'
4418 && (recog_memoized (p) < 0
4419 || (extract_insn (p), ! constrain_operands (1))))
4421 error_for_asm (insn,
4422 "`asm' operand requires impossible reload");
4423 PUT_CODE (p, NOTE);
4424 NOTE_SOURCE_FILE (p) = 0;
4425 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
4428 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4429 is no longer validly lying around to save a future reload.
4430 Note that this does not detect pseudos that were reloaded
4431 for this insn in order to be stored in
4432 (obeying register constraints). That is correct; such reload
4433 registers ARE still valid. */
4434 note_stores (oldpat, forget_old_reloads_1, NULL);
4436 /* There may have been CLOBBER insns placed after INSN. So scan
4437 between INSN and NEXT and use them to forget old reloads. */
4438 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
4439 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
4440 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
4442 #ifdef AUTO_INC_DEC
4443 /* Likewise for regs altered by auto-increment in this insn.
4444 REG_INC notes have been changed by reloading:
4445 find_reloads_address_1 records substitutions for them,
4446 which have been performed by subst_reloads above. */
4447 for (i = n_reloads - 1; i >= 0; i--)
4449 rtx in_reg = rld[i].in_reg;
4450 if (in_reg)
4452 enum rtx_code code = GET_CODE (in_reg);
4453 /* PRE_INC / PRE_DEC will have the reload register ending up
4454 with the same value as the stack slot, but that doesn't
4455 hold true for POST_INC / POST_DEC. Either we have to
4456 convert the memory access to a true POST_INC / POST_DEC,
4457 or we can't use the reload register for inheritance. */
4458 if ((code == POST_INC || code == POST_DEC)
4459 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4460 REGNO (rld[i].reg_rtx))
4461 /* Make sure it is the inc/dec pseudo, and not
4462 some other (e.g. output operand) pseudo. */
4463 && (reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4464 == REGNO (XEXP (in_reg, 0))))
4467 rtx reload_reg = rld[i].reg_rtx;
4468 enum machine_mode mode = GET_MODE (reload_reg);
4469 int n = 0;
4470 rtx p;
4472 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4474 /* We really want to ignore REG_INC notes here, so
4475 use PATTERN (p) as argument to reg_set_p . */
4476 if (reg_set_p (reload_reg, PATTERN (p)))
4477 break;
4478 n = count_occurrences (PATTERN (p), reload_reg);
4479 if (! n)
4480 continue;
4481 if (n == 1)
4483 n = validate_replace_rtx (reload_reg,
4484 gen_rtx (code, mode,
4485 reload_reg),
4488 /* We must also verify that the constraints
4489 are met after the replacement. */
4490 extract_insn (p);
4491 if (n)
4492 n = constrain_operands (1);
4493 else
4494 break;
4496 /* If the constraints were not met, then
4497 undo the replacement. */
4498 if (!n)
4500 validate_replace_rtx (gen_rtx (code, mode,
4501 reload_reg),
4502 reload_reg, p);
4503 break;
4507 break;
4509 if (n == 1)
4511 REG_NOTES (p)
4512 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
4513 REG_NOTES (p));
4514 /* Mark this as having an output reload so that the
4515 REG_INC processing code below won't invalidate
4516 the reload for inheritance. */
4517 SET_HARD_REG_BIT (reg_is_output_reload,
4518 REGNO (reload_reg));
4519 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4521 else
4522 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4523 NULL);
4525 else if ((code == PRE_INC || code == PRE_DEC)
4526 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4527 REGNO (rld[i].reg_rtx))
4528 /* Make sure it is the inc/dec pseudo, and not
4529 some other (e.g. output operand) pseudo. */
4530 && (reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4531 == REGNO (XEXP (in_reg, 0))))
4533 SET_HARD_REG_BIT (reg_is_output_reload,
4534 REGNO (rld[i].reg_rtx));
4535 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4539 /* If a pseudo that got a hard register is auto-incremented,
4540 we must purge records of copying it into pseudos without
4541 hard registers. */
4542 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4543 if (REG_NOTE_KIND (x) == REG_INC)
4545 /* See if this pseudo reg was reloaded in this insn.
4546 If so, its last-reload info is still valid
4547 because it is based on this insn's reload. */
4548 for (i = 0; i < n_reloads; i++)
4549 if (rld[i].out == XEXP (x, 0))
4550 break;
4552 if (i == n_reloads)
4553 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4555 #endif
4557 /* A reload reg's contents are unknown after a label. */
4558 if (GET_CODE (insn) == CODE_LABEL)
4559 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4561 /* Don't assume a reload reg is still good after a call insn
4562 if it is a call-used reg. */
4563 else if (GET_CODE (insn) == CALL_INSN)
4564 AND_COMPL_HARD_REG_SET(reg_reloaded_valid, call_used_reg_set);
4567 /* Clean up. */
4568 free (reg_last_reload_reg);
4569 free (reg_has_output_reload);
4572 /* Discard all record of any value reloaded from X,
4573 or reloaded in X from someplace else;
4574 unless X is an output reload reg of the current insn.
4576 X may be a hard reg (the reload reg)
4577 or it may be a pseudo reg that was reloaded from. */
4579 static void
4580 forget_old_reloads_1 (x, ignored, data)
4581 rtx x;
4582 rtx ignored ATTRIBUTE_UNUSED;
4583 void *data ATTRIBUTE_UNUSED;
4585 register int regno;
4586 int nr;
4587 int offset = 0;
4589 /* note_stores does give us subregs of hard regs. */
4590 while (GET_CODE (x) == SUBREG)
4592 offset += SUBREG_WORD (x);
4593 x = SUBREG_REG (x);
4596 if (GET_CODE (x) != REG)
4597 return;
4599 regno = REGNO (x) + offset;
4601 if (regno >= FIRST_PSEUDO_REGISTER)
4602 nr = 1;
4603 else
4605 int i;
4606 nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
4607 /* Storing into a spilled-reg invalidates its contents.
4608 This can happen if a block-local pseudo is allocated to that reg
4609 and it wasn't spilled because this block's total need is 0.
4610 Then some insn might have an optional reload and use this reg. */
4611 for (i = 0; i < nr; i++)
4612 /* But don't do this if the reg actually serves as an output
4613 reload reg in the current instruction. */
4614 if (n_reloads == 0
4615 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4616 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4619 /* Since value of X has changed,
4620 forget any value previously copied from it. */
4622 while (nr-- > 0)
4623 /* But don't forget a copy if this is the output reload
4624 that establishes the copy's validity. */
4625 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4626 reg_last_reload_reg[regno + nr] = 0;
4629 /* Comparison function for qsort to decide which of two reloads
4630 should be handled first. *P1 and *P2 are the reload numbers. */
4632 static int
4633 reload_reg_class_lower (r1p, r2p)
4634 const PTR r1p;
4635 const PTR r2p;
4637 register int r1 = *(const short *)r1p, r2 = *(const short *)r2p;
4638 register int t;
4640 /* Consider required reloads before optional ones. */
4641 t = rld[r1].optional - rld[r2].optional;
4642 if (t != 0)
4643 return t;
4645 /* Count all solitary classes before non-solitary ones. */
4646 t = ((reg_class_size[(int) rld[r2].class] == 1)
4647 - (reg_class_size[(int) rld[r1].class] == 1));
4648 if (t != 0)
4649 return t;
4651 /* Aside from solitaires, consider all multi-reg groups first. */
4652 t = rld[r2].nregs - rld[r1].nregs;
4653 if (t != 0)
4654 return t;
4656 /* Consider reloads in order of increasing reg-class number. */
4657 t = (int) rld[r1].class - (int) rld[r2].class;
4658 if (t != 0)
4659 return t;
4661 /* If reloads are equally urgent, sort by reload number,
4662 so that the results of qsort leave nothing to chance. */
4663 return r1 - r2;
4666 /* The following HARD_REG_SETs indicate when each hard register is
4667 used for a reload of various parts of the current insn. */
4669 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4670 static HARD_REG_SET reload_reg_used;
4671 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4672 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4673 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4674 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4675 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4676 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4677 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4678 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4679 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4680 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4681 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4682 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4683 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4684 static HARD_REG_SET reload_reg_used_in_op_addr;
4685 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4686 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4687 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4688 static HARD_REG_SET reload_reg_used_in_insn;
4689 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4690 static HARD_REG_SET reload_reg_used_in_other_addr;
4692 /* If reg is in use as a reload reg for any sort of reload. */
4693 static HARD_REG_SET reload_reg_used_at_all;
4695 /* If reg is use as an inherited reload. We just mark the first register
4696 in the group. */
4697 static HARD_REG_SET reload_reg_used_for_inherit;
4699 /* Records which hard regs are used in any way, either as explicit use or
4700 by being allocated to a pseudo during any point of the current insn. */
4701 static HARD_REG_SET reg_used_in_insn;
4703 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4704 TYPE. MODE is used to indicate how many consecutive regs are
4705 actually used. */
4707 static void
4708 mark_reload_reg_in_use (regno, opnum, type, mode)
4709 int regno;
4710 int opnum;
4711 enum reload_type type;
4712 enum machine_mode mode;
4714 int nregs = HARD_REGNO_NREGS (regno, mode);
4715 int i;
4717 for (i = regno; i < nregs + regno; i++)
4719 switch (type)
4721 case RELOAD_OTHER:
4722 SET_HARD_REG_BIT (reload_reg_used, i);
4723 break;
4725 case RELOAD_FOR_INPUT_ADDRESS:
4726 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4727 break;
4729 case RELOAD_FOR_INPADDR_ADDRESS:
4730 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4731 break;
4733 case RELOAD_FOR_OUTPUT_ADDRESS:
4734 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4735 break;
4737 case RELOAD_FOR_OUTADDR_ADDRESS:
4738 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4739 break;
4741 case RELOAD_FOR_OPERAND_ADDRESS:
4742 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4743 break;
4745 case RELOAD_FOR_OPADDR_ADDR:
4746 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4747 break;
4749 case RELOAD_FOR_OTHER_ADDRESS:
4750 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4751 break;
4753 case RELOAD_FOR_INPUT:
4754 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4755 break;
4757 case RELOAD_FOR_OUTPUT:
4758 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4759 break;
4761 case RELOAD_FOR_INSN:
4762 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4763 break;
4766 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4770 /* Similarly, but show REGNO is no longer in use for a reload. */
4772 static void
4773 clear_reload_reg_in_use (regno, opnum, type, mode)
4774 int regno;
4775 int opnum;
4776 enum reload_type type;
4777 enum machine_mode mode;
4779 int nregs = HARD_REGNO_NREGS (regno, mode);
4780 int start_regno, end_regno;
4781 int i;
4782 /* A complication is that for some reload types, inheritance might
4783 allow multiple reloads of the same types to share a reload register.
4784 We set check_opnum if we have to check only reloads with the same
4785 operand number, and check_any if we have to check all reloads. */
4786 int check_opnum = 0;
4787 int check_any = 0;
4788 HARD_REG_SET *used_in_set;
4790 switch (type)
4792 case RELOAD_OTHER:
4793 used_in_set = &reload_reg_used;
4794 break;
4796 case RELOAD_FOR_INPUT_ADDRESS:
4797 used_in_set = &reload_reg_used_in_input_addr[opnum];
4798 break;
4800 case RELOAD_FOR_INPADDR_ADDRESS:
4801 check_opnum = 1;
4802 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4803 break;
4805 case RELOAD_FOR_OUTPUT_ADDRESS:
4806 used_in_set = &reload_reg_used_in_output_addr[opnum];
4807 break;
4809 case RELOAD_FOR_OUTADDR_ADDRESS:
4810 check_opnum = 1;
4811 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4812 break;
4814 case RELOAD_FOR_OPERAND_ADDRESS:
4815 used_in_set = &reload_reg_used_in_op_addr;
4816 break;
4818 case RELOAD_FOR_OPADDR_ADDR:
4819 check_any = 1;
4820 used_in_set = &reload_reg_used_in_op_addr_reload;
4821 break;
4823 case RELOAD_FOR_OTHER_ADDRESS:
4824 used_in_set = &reload_reg_used_in_other_addr;
4825 check_any = 1;
4826 break;
4828 case RELOAD_FOR_INPUT:
4829 used_in_set = &reload_reg_used_in_input[opnum];
4830 break;
4832 case RELOAD_FOR_OUTPUT:
4833 used_in_set = &reload_reg_used_in_output[opnum];
4834 break;
4836 case RELOAD_FOR_INSN:
4837 used_in_set = &reload_reg_used_in_insn;
4838 break;
4839 default:
4840 abort ();
4842 /* We resolve conflicts with remaining reloads of the same type by
4843 excluding the intervals of of reload registers by them from the
4844 interval of freed reload registers. Since we only keep track of
4845 one set of interval bounds, we might have to exclude somewhat
4846 more then what would be necessary if we used a HARD_REG_SET here.
4847 But this should only happen very infrequently, so there should
4848 be no reason to worry about it. */
4850 start_regno = regno;
4851 end_regno = regno + nregs;
4852 if (check_opnum || check_any)
4854 for (i = n_reloads - 1; i >= 0; i--)
4856 if (rld[i].when_needed == type
4857 && (check_any || rld[i].opnum == opnum)
4858 && rld[i].reg_rtx)
4860 int conflict_start = true_regnum (rld[i].reg_rtx);
4861 int conflict_end
4862 = (conflict_start
4863 + HARD_REGNO_NREGS (conflict_start, rld[i].mode));
4865 /* If there is an overlap with the first to-be-freed register,
4866 adjust the interval start. */
4867 if (conflict_start <= start_regno && conflict_end > start_regno)
4868 start_regno = conflict_end;
4869 /* Otherwise, if there is a conflict with one of the other
4870 to-be-freed registers, adjust the interval end. */
4871 if (conflict_start > start_regno && conflict_start < end_regno)
4872 end_regno = conflict_start;
4876 for (i = start_regno; i < end_regno; i++)
4877 CLEAR_HARD_REG_BIT (*used_in_set, i);
4880 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4881 specified by OPNUM and TYPE. */
4883 static int
4884 reload_reg_free_p (regno, opnum, type)
4885 int regno;
4886 int opnum;
4887 enum reload_type type;
4889 int i;
4891 /* In use for a RELOAD_OTHER means it's not available for anything. */
4892 if (TEST_HARD_REG_BIT (reload_reg_used, regno))
4893 return 0;
4895 switch (type)
4897 case RELOAD_OTHER:
4898 /* In use for anything means we can't use it for RELOAD_OTHER. */
4899 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4900 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4901 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4902 return 0;
4904 for (i = 0; i < reload_n_operands; i++)
4905 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4906 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4907 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4908 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4909 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4910 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4911 return 0;
4913 return 1;
4915 case RELOAD_FOR_INPUT:
4916 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4917 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4918 return 0;
4920 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4921 return 0;
4923 /* If it is used for some other input, can't use it. */
4924 for (i = 0; i < reload_n_operands; i++)
4925 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4926 return 0;
4928 /* If it is used in a later operand's address, can't use it. */
4929 for (i = opnum + 1; i < reload_n_operands; i++)
4930 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4931 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4932 return 0;
4934 return 1;
4936 case RELOAD_FOR_INPUT_ADDRESS:
4937 /* Can't use a register if it is used for an input address for this
4938 operand or used as an input in an earlier one. */
4939 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4940 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4941 return 0;
4943 for (i = 0; i < opnum; i++)
4944 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4945 return 0;
4947 return 1;
4949 case RELOAD_FOR_INPADDR_ADDRESS:
4950 /* Can't use a register if it is used for an input address
4951 for this operand or used as an input in an earlier
4952 one. */
4953 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4954 return 0;
4956 for (i = 0; i < opnum; i++)
4957 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4958 return 0;
4960 return 1;
4962 case RELOAD_FOR_OUTPUT_ADDRESS:
4963 /* Can't use a register if it is used for an output address for this
4964 operand or used as an output in this or a later operand. */
4965 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4966 return 0;
4968 for (i = opnum; i < reload_n_operands; i++)
4969 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4970 return 0;
4972 return 1;
4974 case RELOAD_FOR_OUTADDR_ADDRESS:
4975 /* Can't use a register if it is used for an output address
4976 for this operand or used as an output in this or a
4977 later operand. */
4978 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4979 return 0;
4981 for (i = opnum; i < reload_n_operands; i++)
4982 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4983 return 0;
4985 return 1;
4987 case RELOAD_FOR_OPERAND_ADDRESS:
4988 for (i = 0; i < reload_n_operands; i++)
4989 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4990 return 0;
4992 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4993 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4995 case RELOAD_FOR_OPADDR_ADDR:
4996 for (i = 0; i < reload_n_operands; i++)
4997 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4998 return 0;
5000 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
5002 case RELOAD_FOR_OUTPUT:
5003 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
5004 outputs, or an operand address for this or an earlier output. */
5005 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
5006 return 0;
5008 for (i = 0; i < reload_n_operands; i++)
5009 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5010 return 0;
5012 for (i = 0; i <= opnum; i++)
5013 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5014 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5015 return 0;
5017 return 1;
5019 case RELOAD_FOR_INSN:
5020 for (i = 0; i < reload_n_operands; i++)
5021 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
5022 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5023 return 0;
5025 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5026 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
5028 case RELOAD_FOR_OTHER_ADDRESS:
5029 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
5031 abort ();
5034 /* Return 1 if the value in reload reg REGNO, as used by a reload
5035 needed for the part of the insn specified by OPNUM and TYPE,
5036 is still available in REGNO at the end of the insn.
5038 We can assume that the reload reg was already tested for availability
5039 at the time it is needed, and we should not check this again,
5040 in case the reg has already been marked in use. */
5042 static int
5043 reload_reg_reaches_end_p (regno, opnum, type)
5044 int regno;
5045 int opnum;
5046 enum reload_type type;
5048 int i;
5050 switch (type)
5052 case RELOAD_OTHER:
5053 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
5054 its value must reach the end. */
5055 return 1;
5057 /* If this use is for part of the insn,
5058 its value reaches if no subsequent part uses the same register.
5059 Just like the above function, don't try to do this with lots
5060 of fallthroughs. */
5062 case RELOAD_FOR_OTHER_ADDRESS:
5063 /* Here we check for everything else, since these don't conflict
5064 with anything else and everything comes later. */
5066 for (i = 0; i < reload_n_operands; i++)
5067 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5068 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5069 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
5070 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5071 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5072 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5073 return 0;
5075 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5076 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5077 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
5079 case RELOAD_FOR_INPUT_ADDRESS:
5080 case RELOAD_FOR_INPADDR_ADDRESS:
5081 /* Similar, except that we check only for this and subsequent inputs
5082 and the address of only subsequent inputs and we do not need
5083 to check for RELOAD_OTHER objects since they are known not to
5084 conflict. */
5086 for (i = opnum; i < reload_n_operands; i++)
5087 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5088 return 0;
5090 for (i = opnum + 1; i < reload_n_operands; i++)
5091 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5092 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
5093 return 0;
5095 for (i = 0; i < reload_n_operands; i++)
5096 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5097 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5098 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5099 return 0;
5101 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
5102 return 0;
5104 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5105 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
5107 case RELOAD_FOR_INPUT:
5108 /* Similar to input address, except we start at the next operand for
5109 both input and input address and we do not check for
5110 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
5111 would conflict. */
5113 for (i = opnum + 1; i < reload_n_operands; i++)
5114 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5115 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5116 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5117 return 0;
5119 /* ... fall through ... */
5121 case RELOAD_FOR_OPERAND_ADDRESS:
5122 /* Check outputs and their addresses. */
5124 for (i = 0; i < reload_n_operands; i++)
5125 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5126 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5127 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5128 return 0;
5130 return 1;
5132 case RELOAD_FOR_OPADDR_ADDR:
5133 for (i = 0; i < reload_n_operands; i++)
5134 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5135 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5136 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5137 return 0;
5139 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5140 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
5142 case RELOAD_FOR_INSN:
5143 /* These conflict with other outputs with RELOAD_OTHER. So
5144 we need only check for output addresses. */
5146 opnum = -1;
5148 /* ... fall through ... */
5150 case RELOAD_FOR_OUTPUT:
5151 case RELOAD_FOR_OUTPUT_ADDRESS:
5152 case RELOAD_FOR_OUTADDR_ADDRESS:
5153 /* We already know these can't conflict with a later output. So the
5154 only thing to check are later output addresses. */
5155 for (i = opnum + 1; i < reload_n_operands; i++)
5156 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5157 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5158 return 0;
5160 return 1;
5163 abort ();
5166 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
5167 Return 0 otherwise.
5169 This function uses the same algorithm as reload_reg_free_p above. */
5172 reloads_conflict (r1, r2)
5173 int r1, r2;
5175 enum reload_type r1_type = rld[r1].when_needed;
5176 enum reload_type r2_type = rld[r2].when_needed;
5177 int r1_opnum = rld[r1].opnum;
5178 int r2_opnum = rld[r2].opnum;
5180 /* RELOAD_OTHER conflicts with everything. */
5181 if (r2_type == RELOAD_OTHER)
5182 return 1;
5184 /* Otherwise, check conflicts differently for each type. */
5186 switch (r1_type)
5188 case RELOAD_FOR_INPUT:
5189 return (r2_type == RELOAD_FOR_INSN
5190 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
5191 || r2_type == RELOAD_FOR_OPADDR_ADDR
5192 || r2_type == RELOAD_FOR_INPUT
5193 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
5194 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
5195 && r2_opnum > r1_opnum));
5197 case RELOAD_FOR_INPUT_ADDRESS:
5198 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
5199 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5201 case RELOAD_FOR_INPADDR_ADDRESS:
5202 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
5203 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5205 case RELOAD_FOR_OUTPUT_ADDRESS:
5206 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
5207 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
5209 case RELOAD_FOR_OUTADDR_ADDRESS:
5210 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
5211 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
5213 case RELOAD_FOR_OPERAND_ADDRESS:
5214 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
5215 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5217 case RELOAD_FOR_OPADDR_ADDR:
5218 return (r2_type == RELOAD_FOR_INPUT
5219 || r2_type == RELOAD_FOR_OPADDR_ADDR);
5221 case RELOAD_FOR_OUTPUT:
5222 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
5223 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
5224 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
5225 && r2_opnum <= r1_opnum));
5227 case RELOAD_FOR_INSN:
5228 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
5229 || r2_type == RELOAD_FOR_INSN
5230 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5232 case RELOAD_FOR_OTHER_ADDRESS:
5233 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
5235 case RELOAD_OTHER:
5236 return 1;
5238 default:
5239 abort ();
5243 /* Vector of reload-numbers showing the order in which the reloads should
5244 be processed. */
5245 short reload_order[MAX_RELOADS];
5247 /* Indexed by reload number, 1 if incoming value
5248 inherited from previous insns. */
5249 char reload_inherited[MAX_RELOADS];
5251 /* For an inherited reload, this is the insn the reload was inherited from,
5252 if we know it. Otherwise, this is 0. */
5253 rtx reload_inheritance_insn[MAX_RELOADS];
5255 /* If non-zero, this is a place to get the value of the reload,
5256 rather than using reload_in. */
5257 rtx reload_override_in[MAX_RELOADS];
5259 /* For each reload, the hard register number of the register used,
5260 or -1 if we did not need a register for this reload. */
5261 int reload_spill_index[MAX_RELOADS];
5263 /* Return 1 if the value in reload reg REGNO, as used by a reload
5264 needed for the part of the insn specified by OPNUM and TYPE,
5265 may be used to load VALUE into it.
5267 Other read-only reloads with the same value do not conflict
5268 unless OUT is non-zero and these other reloads have to live while
5269 output reloads live.
5270 If OUT is CONST0_RTX, this is a special case: it means that the
5271 test should not be for using register REGNO as reload register, but
5272 for copying from register REGNO into the reload register.
5274 RELOADNUM is the number of the reload we want to load this value for;
5275 a reload does not conflict with itself.
5277 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
5278 reloads that load an address for the very reload we are considering.
5280 The caller has to make sure that there is no conflict with the return
5281 register. */
5282 static int
5283 reload_reg_free_for_value_p (regno, opnum, type, value, out, reloadnum,
5284 ignore_address_reloads)
5285 int regno;
5286 int opnum;
5287 enum reload_type type;
5288 rtx value, out;
5289 int reloadnum;
5290 int ignore_address_reloads;
5292 int time1;
5293 /* Set if we see an input reload that must not share its reload register
5294 with any new earlyclobber, but might otherwise share the reload
5295 register with an output or input-output reload. */
5296 int check_earlyclobber = 0;
5297 int i;
5298 int copy = 0;
5300 /* ??? reload_reg_used is abused to hold the registers that are not
5301 available as spill registers, including hard registers that are
5302 earlyclobbered in asms. As a temporary measure, reject anything
5303 in reload_reg_used. */
5304 if (TEST_HARD_REG_BIT (reload_reg_used, regno))
5305 return 0;
5307 if (out == const0_rtx)
5309 copy = 1;
5310 out = NULL_RTX;
5313 /* We use some pseudo 'time' value to check if the lifetimes of the
5314 new register use would overlap with the one of a previous reload
5315 that is not read-only or uses a different value.
5316 The 'time' used doesn't have to be linear in any shape or form, just
5317 monotonic.
5318 Some reload types use different 'buckets' for each operand.
5319 So there are MAX_RECOG_OPERANDS different time values for each
5320 such reload type.
5321 We compute TIME1 as the time when the register for the prospective
5322 new reload ceases to be live, and TIME2 for each existing
5323 reload as the time when that the reload register of that reload
5324 becomes live.
5325 Where there is little to be gained by exact lifetime calculations,
5326 we just make conservative assumptions, i.e. a longer lifetime;
5327 this is done in the 'default:' cases. */
5328 switch (type)
5330 case RELOAD_FOR_OTHER_ADDRESS:
5331 /* RELOAD_FOR_OTHER_ADDRESS conflits with RELOAD_OTHER reloads. */
5332 time1 = copy ? 0 : 1;
5333 break;
5334 case RELOAD_OTHER:
5335 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
5336 break;
5337 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
5338 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
5339 respectively, to the time values for these, we get distinct time
5340 values. To get distinct time values for each operand, we have to
5341 multiply opnum by at least three. We round that up to four because
5342 multiply by four is often cheaper. */
5343 case RELOAD_FOR_INPADDR_ADDRESS:
5344 time1 = opnum * 4 + 2;
5345 break;
5346 case RELOAD_FOR_INPUT_ADDRESS:
5347 time1 = opnum * 4 + 3;
5348 break;
5349 case RELOAD_FOR_INPUT:
5350 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
5351 executes (inclusive). */
5352 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
5353 break;
5354 case RELOAD_FOR_OPADDR_ADDR:
5355 /* opnum * 4 + 4
5356 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
5357 time1 = MAX_RECOG_OPERANDS * 4 + 1;
5358 break;
5359 case RELOAD_FOR_OPERAND_ADDRESS:
5360 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
5361 is executed. */
5362 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
5363 break;
5364 case RELOAD_FOR_OUTADDR_ADDRESS:
5365 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
5366 break;
5367 case RELOAD_FOR_OUTPUT_ADDRESS:
5368 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
5369 break;
5370 default:
5371 time1 = MAX_RECOG_OPERANDS * 5 + 5;
5374 for (i = 0; i < n_reloads; i++)
5376 rtx reg = rld[i].reg_rtx;
5377 if (reg && GET_CODE (reg) == REG
5378 && ((unsigned) regno - true_regnum (reg)
5379 <= HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)) - (unsigned)1)
5380 && i != reloadnum)
5382 if (! rld[i].in || ! rtx_equal_p (rld[i].in, value)
5383 || rld[i].out || out)
5385 int time2;
5386 switch (rld[i].when_needed)
5388 case RELOAD_FOR_OTHER_ADDRESS:
5389 time2 = 0;
5390 break;
5391 case RELOAD_FOR_INPADDR_ADDRESS:
5392 /* find_reloads makes sure that a
5393 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
5394 by at most one - the first -
5395 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
5396 address reload is inherited, the address address reload
5397 goes away, so we can ignore this conflict. */
5398 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
5399 && ignore_address_reloads
5400 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
5401 Then the address address is still needed to store
5402 back the new address. */
5403 && ! rld[reloadnum].out)
5404 continue;
5405 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
5406 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
5407 reloads go away. */
5408 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5409 && ignore_address_reloads
5410 /* Unless we are reloading an auto_inc expression. */
5411 && ! rld[reloadnum].out)
5412 continue;
5413 time2 = rld[i].opnum * 4 + 2;
5414 break;
5415 case RELOAD_FOR_INPUT_ADDRESS:
5416 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5417 && ignore_address_reloads
5418 && ! rld[reloadnum].out)
5419 continue;
5420 time2 = rld[i].opnum * 4 + 3;
5421 break;
5422 case RELOAD_FOR_INPUT:
5423 time2 = rld[i].opnum * 4 + 4;
5424 check_earlyclobber = 1;
5425 break;
5426 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
5427 == MAX_RECOG_OPERAND * 4 */
5428 case RELOAD_FOR_OPADDR_ADDR:
5429 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
5430 && ignore_address_reloads
5431 && ! rld[reloadnum].out)
5432 continue;
5433 time2 = MAX_RECOG_OPERANDS * 4 + 1;
5434 break;
5435 case RELOAD_FOR_OPERAND_ADDRESS:
5436 time2 = MAX_RECOG_OPERANDS * 4 + 2;
5437 check_earlyclobber = 1;
5438 break;
5439 case RELOAD_FOR_INSN:
5440 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5441 break;
5442 case RELOAD_FOR_OUTPUT:
5443 /* All RELOAD_FOR_OUTPUT reloads become live just after the
5444 instruction is executed. */
5445 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5446 break;
5447 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
5448 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
5449 value. */
5450 case RELOAD_FOR_OUTADDR_ADDRESS:
5451 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
5452 && ignore_address_reloads
5453 && ! rld[reloadnum].out)
5454 continue;
5455 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
5456 break;
5457 case RELOAD_FOR_OUTPUT_ADDRESS:
5458 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
5459 break;
5460 case RELOAD_OTHER:
5461 /* If there is no conflict in the input part, handle this
5462 like an output reload. */
5463 if (! rld[i].in || rtx_equal_p (rld[i].in, value))
5465 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5466 /* Earlyclobbered outputs must conflict with inputs. */
5467 if (earlyclobber_operand_p (rld[i].out))
5468 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5470 break;
5472 time2 = 1;
5473 /* RELOAD_OTHER might be live beyond instruction execution,
5474 but this is not obvious when we set time2 = 1. So check
5475 here if there might be a problem with the new reload
5476 clobbering the register used by the RELOAD_OTHER. */
5477 if (out)
5478 return 0;
5479 break;
5480 default:
5481 return 0;
5483 if ((time1 >= time2
5484 && (! rld[i].in || rld[i].out
5485 || ! rtx_equal_p (rld[i].in, value)))
5486 || (out && rld[reloadnum].out_reg
5487 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
5488 return 0;
5493 /* Earlyclobbered outputs must conflict with inputs. */
5494 if (check_earlyclobber && out && earlyclobber_operand_p (out))
5495 return 0;
5497 return 1;
5500 /* Give an error message saying we failed to find a reload for INSN,
5501 and clear out reload R. */
5502 static void
5503 failed_reload (insn, r)
5504 rtx insn;
5505 int r;
5507 if (asm_noperands (PATTERN (insn)) < 0)
5508 /* It's the compiler's fault. */
5509 fatal_insn ("Could not find a spill register", insn);
5511 /* It's the user's fault; the operand's mode and constraint
5512 don't match. Disable this reload so we don't crash in final. */
5513 error_for_asm (insn,
5514 "`asm' operand constraint incompatible with operand size");
5515 rld[r].in = 0;
5516 rld[r].out = 0;
5517 rld[r].reg_rtx = 0;
5518 rld[r].optional = 1;
5519 rld[r].secondary_p = 1;
5522 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5523 for reload R. If it's valid, get an rtx for it. Return nonzero if
5524 successful. */
5525 static int
5526 set_reload_reg (i, r)
5527 int i, r;
5529 int regno;
5530 rtx reg = spill_reg_rtx[i];
5532 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5533 spill_reg_rtx[i] = reg
5534 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5536 regno = true_regnum (reg);
5538 /* Detect when the reload reg can't hold the reload mode.
5539 This used to be one `if', but Sequent compiler can't handle that. */
5540 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5542 enum machine_mode test_mode = VOIDmode;
5543 if (rld[r].in)
5544 test_mode = GET_MODE (rld[r].in);
5545 /* If rld[r].in has VOIDmode, it means we will load it
5546 in whatever mode the reload reg has: to wit, rld[r].mode.
5547 We have already tested that for validity. */
5548 /* Aside from that, we need to test that the expressions
5549 to reload from or into have modes which are valid for this
5550 reload register. Otherwise the reload insns would be invalid. */
5551 if (! (rld[r].in != 0 && test_mode != VOIDmode
5552 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5553 if (! (rld[r].out != 0
5554 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5556 /* The reg is OK. */
5557 last_spill_reg = i;
5559 /* Mark as in use for this insn the reload regs we use
5560 for this. */
5561 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5562 rld[r].when_needed, rld[r].mode);
5564 rld[r].reg_rtx = reg;
5565 reload_spill_index[r] = spill_regs[i];
5566 return 1;
5569 return 0;
5572 /* Find a spill register to use as a reload register for reload R.
5573 LAST_RELOAD is non-zero if this is the last reload for the insn being
5574 processed.
5576 Set rld[R].reg_rtx to the register allocated.
5578 If NOERROR is nonzero, we return 1 if successful,
5579 or 0 if we couldn't find a spill reg and we didn't change anything. */
5581 static int
5582 allocate_reload_reg (chain, r, last_reload, noerror)
5583 struct insn_chain *chain;
5584 int r;
5585 int last_reload;
5586 int noerror;
5588 rtx insn = chain->insn;
5589 int i, pass, count;
5591 /* If we put this reload ahead, thinking it is a group,
5592 then insist on finding a group. Otherwise we can grab a
5593 reg that some other reload needs.
5594 (That can happen when we have a 68000 DATA_OR_FP_REG
5595 which is a group of data regs or one fp reg.)
5596 We need not be so restrictive if there are no more reloads
5597 for this insn.
5599 ??? Really it would be nicer to have smarter handling
5600 for that kind of reg class, where a problem like this is normal.
5601 Perhaps those classes should be avoided for reloading
5602 by use of more alternatives. */
5604 int force_group = rld[r].nregs > 1 && ! last_reload;
5606 /* If we want a single register and haven't yet found one,
5607 take any reg in the right class and not in use.
5608 If we want a consecutive group, here is where we look for it.
5610 We use two passes so we can first look for reload regs to
5611 reuse, which are already in use for other reloads in this insn,
5612 and only then use additional registers.
5613 I think that maximizing reuse is needed to make sure we don't
5614 run out of reload regs. Suppose we have three reloads, and
5615 reloads A and B can share regs. These need two regs.
5616 Suppose A and B are given different regs.
5617 That leaves none for C. */
5618 for (pass = 0; pass < 2; pass++)
5620 /* I is the index in spill_regs.
5621 We advance it round-robin between insns to use all spill regs
5622 equally, so that inherited reloads have a chance
5623 of leapfrogging each other. Don't do this, however, when we have
5624 group needs and failure would be fatal; if we only have a relatively
5625 small number of spill registers, and more than one of them has
5626 group needs, then by starting in the middle, we may end up
5627 allocating the first one in such a way that we are not left with
5628 sufficient groups to handle the rest. */
5630 if (noerror || ! force_group)
5631 i = last_spill_reg;
5632 else
5633 i = -1;
5635 for (count = 0; count < n_spills; count++)
5637 int class = (int) rld[r].class;
5638 int regnum;
5640 i++;
5641 if (i >= n_spills)
5642 i -= n_spills;
5643 regnum = spill_regs[i];
5645 if ((reload_reg_free_p (regnum, rld[r].opnum,
5646 rld[r].when_needed)
5647 || (rld[r].in
5648 /* We check reload_reg_used to make sure we
5649 don't clobber the return register. */
5650 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5651 && reload_reg_free_for_value_p (regnum,
5652 rld[r].opnum,
5653 rld[r].when_needed,
5654 rld[r].in,
5655 rld[r].out, r, 1)))
5656 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5657 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5658 /* Look first for regs to share, then for unshared. But
5659 don't share regs used for inherited reloads; they are
5660 the ones we want to preserve. */
5661 && (pass
5662 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5663 regnum)
5664 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5665 regnum))))
5667 int nr = HARD_REGNO_NREGS (regnum, rld[r].mode);
5668 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5669 (on 68000) got us two FP regs. If NR is 1,
5670 we would reject both of them. */
5671 if (force_group)
5672 nr = rld[r].nregs;
5673 /* If we need only one reg, we have already won. */
5674 if (nr == 1)
5676 /* But reject a single reg if we demand a group. */
5677 if (force_group)
5678 continue;
5679 break;
5681 /* Otherwise check that as many consecutive regs as we need
5682 are available here.
5683 Also, don't use for a group registers that are
5684 needed for nongroups. */
5685 if (! TEST_HARD_REG_BIT (chain->counted_for_nongroups, regnum))
5686 while (nr > 1)
5688 int regno = regnum + nr - 1;
5689 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5690 && spill_reg_order[regno] >= 0
5691 && reload_reg_free_p (regno, rld[r].opnum,
5692 rld[r].when_needed)
5693 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups,
5694 regno)))
5695 break;
5696 nr--;
5698 if (nr == 1)
5699 break;
5703 /* If we found something on pass 1, omit pass 2. */
5704 if (count < n_spills)
5705 break;
5708 /* We should have found a spill register by now. */
5709 if (count == n_spills)
5711 if (noerror)
5712 return 0;
5713 goto failure;
5716 if (set_reload_reg (i, r))
5717 return 1;
5719 /* The reg is not OK. */
5720 if (noerror)
5721 return 0;
5723 failure:
5724 failed_reload (insn, r);
5726 return 1;
5729 /* Initialize all the tables needed to allocate reload registers.
5730 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5731 is the array we use to restore the reg_rtx field for every reload. */
5732 static void
5733 choose_reload_regs_init (chain, save_reload_reg_rtx)
5734 struct insn_chain *chain;
5735 rtx *save_reload_reg_rtx;
5737 int i;
5739 for (i = 0; i < n_reloads; i++)
5740 rld[i].reg_rtx = save_reload_reg_rtx[i];
5742 bzero (reload_inherited, MAX_RELOADS);
5743 bzero ((char *) reload_inheritance_insn, MAX_RELOADS * sizeof (rtx));
5744 bzero ((char *) reload_override_in, MAX_RELOADS * sizeof (rtx));
5746 CLEAR_HARD_REG_SET (reload_reg_used);
5747 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5748 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5749 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5750 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5751 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5753 CLEAR_HARD_REG_SET (reg_used_in_insn);
5755 HARD_REG_SET tmp;
5756 REG_SET_TO_HARD_REG_SET (tmp, chain->live_before);
5757 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5758 REG_SET_TO_HARD_REG_SET (tmp, chain->live_after);
5759 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5760 compute_use_by_pseudos (&reg_used_in_insn, chain->live_before);
5761 compute_use_by_pseudos (&reg_used_in_insn, chain->live_after);
5763 for (i = 0; i < reload_n_operands; i++)
5765 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5766 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5767 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5768 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5769 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5770 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5773 IOR_COMPL_HARD_REG_SET (reload_reg_used, chain->used_spill_regs);
5775 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5777 for (i = 0; i < n_reloads; i++)
5778 /* If we have already decided to use a certain register,
5779 don't use it in another way. */
5780 if (rld[i].reg_rtx)
5781 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5782 rld[i].when_needed, rld[i].mode);
5785 /* Assign hard reg targets for the pseudo-registers we must reload
5786 into hard regs for this insn.
5787 Also output the instructions to copy them in and out of the hard regs.
5789 For machines with register classes, we are responsible for
5790 finding a reload reg in the proper class. */
5792 static void
5793 choose_reload_regs (chain)
5794 struct insn_chain *chain;
5796 rtx insn = chain->insn;
5797 register int i, j;
5798 int max_group_size = 1;
5799 enum reg_class group_class = NO_REGS;
5800 int inheritance;
5801 int pass;
5803 rtx save_reload_reg_rtx[MAX_RELOADS];
5805 /* In order to be certain of getting the registers we need,
5806 we must sort the reloads into order of increasing register class.
5807 Then our grabbing of reload registers will parallel the process
5808 that provided the reload registers.
5810 Also note whether any of the reloads wants a consecutive group of regs.
5811 If so, record the maximum size of the group desired and what
5812 register class contains all the groups needed by this insn. */
5814 for (j = 0; j < n_reloads; j++)
5816 reload_order[j] = j;
5817 reload_spill_index[j] = -1;
5819 if (rld[j].nregs > 1)
5821 max_group_size = MAX (rld[j].nregs, max_group_size);
5822 group_class = reg_class_superunion[(int)rld[j].class][(int)group_class];
5825 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5828 if (n_reloads > 1)
5829 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5831 /* If -O, try first with inheritance, then turning it off.
5832 If not -O, don't do inheritance.
5833 Using inheritance when not optimizing leads to paradoxes
5834 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5835 because one side of the comparison might be inherited. */
5837 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5839 choose_reload_regs_init (chain, save_reload_reg_rtx);
5841 /* Process the reloads in order of preference just found.
5842 Beyond this point, subregs can be found in reload_reg_rtx.
5844 This used to look for an existing reloaded home for all
5845 of the reloads, and only then perform any new reloads.
5846 But that could lose if the reloads were done out of reg-class order
5847 because a later reload with a looser constraint might have an old
5848 home in a register needed by an earlier reload with a tighter constraint.
5850 To solve this, we make two passes over the reloads, in the order
5851 described above. In the first pass we try to inherit a reload
5852 from a previous insn. If there is a later reload that needs a
5853 class that is a proper subset of the class being processed, we must
5854 also allocate a spill register during the first pass.
5856 Then make a second pass over the reloads to allocate any reloads
5857 that haven't been given registers yet. */
5859 for (j = 0; j < n_reloads; j++)
5861 register int r = reload_order[j];
5862 rtx search_equiv = NULL_RTX;
5864 /* Ignore reloads that got marked inoperative. */
5865 if (rld[r].out == 0 && rld[r].in == 0
5866 && ! rld[r].secondary_p)
5867 continue;
5869 /* If find_reloads chose to use reload_in or reload_out as a reload
5870 register, we don't need to chose one. Otherwise, try even if it
5871 found one since we might save an insn if we find the value lying
5872 around.
5873 Try also when reload_in is a pseudo without a hard reg. */
5874 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5875 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5876 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5877 && GET_CODE (rld[r].in) != MEM
5878 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5879 continue;
5881 #if 0 /* No longer needed for correct operation.
5882 It might give better code, or might not; worth an experiment? */
5883 /* If this is an optional reload, we can't inherit from earlier insns
5884 until we are sure that any non-optional reloads have been allocated.
5885 The following code takes advantage of the fact that optional reloads
5886 are at the end of reload_order. */
5887 if (rld[r].optional != 0)
5888 for (i = 0; i < j; i++)
5889 if ((rld[reload_order[i]].out != 0
5890 || rld[reload_order[i]].in != 0
5891 || rld[reload_order[i]].secondary_p)
5892 && ! rld[reload_order[i]].optional
5893 && rld[reload_order[i]].reg_rtx == 0)
5894 allocate_reload_reg (chain, reload_order[i], 0, inheritance);
5895 #endif
5897 /* First see if this pseudo is already available as reloaded
5898 for a previous insn. We cannot try to inherit for reloads
5899 that are smaller than the maximum number of registers needed
5900 for groups unless the register we would allocate cannot be used
5901 for the groups.
5903 We could check here to see if this is a secondary reload for
5904 an object that is already in a register of the desired class.
5905 This would avoid the need for the secondary reload register.
5906 But this is complex because we can't easily determine what
5907 objects might want to be loaded via this reload. So let a
5908 register be allocated here. In `emit_reload_insns' we suppress
5909 one of the loads in the case described above. */
5911 if (inheritance)
5913 int word = 0;
5914 register int regno = -1;
5915 enum machine_mode mode = VOIDmode;
5917 if (rld[r].in == 0)
5919 else if (GET_CODE (rld[r].in) == REG)
5921 regno = REGNO (rld[r].in);
5922 mode = GET_MODE (rld[r].in);
5924 else if (GET_CODE (rld[r].in_reg) == REG)
5926 regno = REGNO (rld[r].in_reg);
5927 mode = GET_MODE (rld[r].in_reg);
5929 else if (GET_CODE (rld[r].in_reg) == SUBREG
5930 && GET_CODE (SUBREG_REG (rld[r].in_reg)) == REG)
5932 word = SUBREG_WORD (rld[r].in_reg);
5933 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5934 if (regno < FIRST_PSEUDO_REGISTER)
5935 regno += word;
5936 mode = GET_MODE (rld[r].in_reg);
5938 #ifdef AUTO_INC_DEC
5939 else if ((GET_CODE (rld[r].in_reg) == PRE_INC
5940 || GET_CODE (rld[r].in_reg) == PRE_DEC
5941 || GET_CODE (rld[r].in_reg) == POST_INC
5942 || GET_CODE (rld[r].in_reg) == POST_DEC)
5943 && GET_CODE (XEXP (rld[r].in_reg, 0)) == REG)
5945 regno = REGNO (XEXP (rld[r].in_reg, 0));
5946 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5947 rld[r].out = rld[r].in;
5949 #endif
5950 #if 0
5951 /* This won't work, since REGNO can be a pseudo reg number.
5952 Also, it takes much more hair to keep track of all the things
5953 that can invalidate an inherited reload of part of a pseudoreg. */
5954 else if (GET_CODE (rld[r].in) == SUBREG
5955 && GET_CODE (SUBREG_REG (rld[r].in)) == REG)
5956 regno = REGNO (SUBREG_REG (rld[r].in)) + SUBREG_WORD (rld[r].in);
5957 #endif
5959 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5961 enum reg_class class = rld[r].class, last_class;
5962 rtx last_reg = reg_last_reload_reg[regno];
5964 i = REGNO (last_reg) + word;
5965 last_class = REGNO_REG_CLASS (i);
5966 if ((GET_MODE_SIZE (GET_MODE (last_reg))
5967 >= GET_MODE_SIZE (mode) + word * UNITS_PER_WORD)
5968 && reg_reloaded_contents[i] == regno
5969 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
5970 && HARD_REGNO_MODE_OK (i, rld[r].mode)
5971 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5972 /* Even if we can't use this register as a reload
5973 register, we might use it for reload_override_in,
5974 if copying it to the desired class is cheap
5975 enough. */
5976 || ((REGISTER_MOVE_COST (last_class, class)
5977 < MEMORY_MOVE_COST (mode, class, 1))
5978 #ifdef SECONDARY_INPUT_RELOAD_CLASS
5979 && (SECONDARY_INPUT_RELOAD_CLASS (class, mode,
5980 last_reg)
5981 == NO_REGS)
5982 #endif
5983 #ifdef SECONDARY_MEMORY_NEEDED
5984 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5985 mode)
5986 #endif
5989 && (rld[r].nregs == max_group_size
5990 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5992 && reload_reg_free_for_value_p (i, rld[r].opnum,
5993 rld[r].when_needed,
5994 rld[r].in,
5995 const0_rtx, r, 1))
5997 /* If a group is needed, verify that all the subsequent
5998 registers still have their values intact. */
5999 int nr
6000 = HARD_REGNO_NREGS (i, rld[r].mode);
6001 int k;
6003 for (k = 1; k < nr; k++)
6004 if (reg_reloaded_contents[i + k] != regno
6005 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
6006 break;
6008 if (k == nr)
6010 int i1;
6012 last_reg = (GET_MODE (last_reg) == mode
6013 ? last_reg : gen_rtx_REG (mode, i));
6015 /* We found a register that contains the
6016 value we need. If this register is the
6017 same as an `earlyclobber' operand of the
6018 current insn, just mark it as a place to
6019 reload from since we can't use it as the
6020 reload register itself. */
6022 for (i1 = 0; i1 < n_earlyclobbers; i1++)
6023 if (reg_overlap_mentioned_for_reload_p
6024 (reg_last_reload_reg[regno],
6025 reload_earlyclobbers[i1]))
6026 break;
6028 if (i1 != n_earlyclobbers
6029 || ! (reload_reg_free_for_value_p
6030 (i, rld[r].opnum, rld[r].when_needed,
6031 rld[r].in, rld[r].out, r, 1))
6032 /* Don't use it if we'd clobber a pseudo reg. */
6033 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
6034 && rld[r].out
6035 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
6036 /* Don't clobber the frame pointer. */
6037 || (i == HARD_FRAME_POINTER_REGNUM && rld[r].out)
6038 /* Don't really use the inherited spill reg
6039 if we need it wider than we've got it. */
6040 || (GET_MODE_SIZE (rld[r].mode)
6041 > GET_MODE_SIZE (mode))
6042 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
6045 /* If find_reloads chose reload_out as reload
6046 register, stay with it - that leaves the
6047 inherited register for subsequent reloads. */
6048 || (rld[r].out && rld[r].reg_rtx
6049 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
6051 reload_override_in[r] = last_reg;
6052 reload_inheritance_insn[r]
6053 = reg_reloaded_insn[i];
6055 else
6057 int k;
6058 /* We can use this as a reload reg. */
6059 /* Mark the register as in use for this part of
6060 the insn. */
6061 mark_reload_reg_in_use (i,
6062 rld[r].opnum,
6063 rld[r].when_needed,
6064 rld[r].mode);
6065 rld[r].reg_rtx = last_reg;
6066 reload_inherited[r] = 1;
6067 reload_inheritance_insn[r]
6068 = reg_reloaded_insn[i];
6069 reload_spill_index[r] = i;
6070 for (k = 0; k < nr; k++)
6071 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6072 i + k);
6079 /* Here's another way to see if the value is already lying around. */
6080 if (inheritance
6081 && rld[r].in != 0
6082 && ! reload_inherited[r]
6083 && rld[r].out == 0
6084 && (CONSTANT_P (rld[r].in)
6085 || GET_CODE (rld[r].in) == PLUS
6086 || GET_CODE (rld[r].in) == REG
6087 || GET_CODE (rld[r].in) == MEM)
6088 && (rld[r].nregs == max_group_size
6089 || ! reg_classes_intersect_p (rld[r].class, group_class)))
6090 search_equiv = rld[r].in;
6091 /* If this is an output reload from a simple move insn, look
6092 if an equivalence for the input is available. */
6093 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
6095 rtx set = single_set (insn);
6097 if (set
6098 && rtx_equal_p (rld[r].out, SET_DEST (set))
6099 && CONSTANT_P (SET_SRC (set)))
6100 search_equiv = SET_SRC (set);
6103 if (search_equiv)
6105 register rtx equiv
6106 = find_equiv_reg (search_equiv, insn, rld[r].class,
6107 -1, NULL_PTR, 0, rld[r].mode);
6108 int regno = 0;
6110 if (equiv != 0)
6112 if (GET_CODE (equiv) == REG)
6113 regno = REGNO (equiv);
6114 else if (GET_CODE (equiv) == SUBREG)
6116 /* This must be a SUBREG of a hard register.
6117 Make a new REG since this might be used in an
6118 address and not all machines support SUBREGs
6119 there. */
6120 regno = REGNO (SUBREG_REG (equiv)) + SUBREG_WORD (equiv);
6121 equiv = gen_rtx_REG (rld[r].mode, regno);
6123 else
6124 abort ();
6127 /* If we found a spill reg, reject it unless it is free
6128 and of the desired class. */
6129 if (equiv != 0
6130 && ((TEST_HARD_REG_BIT (reload_reg_used_at_all, regno)
6131 && ! reload_reg_free_for_value_p (regno, rld[r].opnum,
6132 rld[r].when_needed,
6133 rld[r].in,
6134 rld[r].out, r, 1))
6135 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
6136 regno)))
6137 equiv = 0;
6139 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
6140 equiv = 0;
6142 /* We found a register that contains the value we need.
6143 If this register is the same as an `earlyclobber' operand
6144 of the current insn, just mark it as a place to reload from
6145 since we can't use it as the reload register itself. */
6147 if (equiv != 0)
6148 for (i = 0; i < n_earlyclobbers; i++)
6149 if (reg_overlap_mentioned_for_reload_p (equiv,
6150 reload_earlyclobbers[i]))
6152 reload_override_in[r] = equiv;
6153 equiv = 0;
6154 break;
6157 /* If the equiv register we have found is explicitly clobbered
6158 in the current insn, it depends on the reload type if we
6159 can use it, use it for reload_override_in, or not at all.
6160 In particular, we then can't use EQUIV for a
6161 RELOAD_FOR_OUTPUT_ADDRESS reload. */
6163 if (equiv != 0 && regno_clobbered_p (regno, insn))
6165 switch (rld[r].when_needed)
6167 case RELOAD_FOR_OTHER_ADDRESS:
6168 case RELOAD_FOR_INPADDR_ADDRESS:
6169 case RELOAD_FOR_INPUT_ADDRESS:
6170 case RELOAD_FOR_OPADDR_ADDR:
6171 break;
6172 case RELOAD_OTHER:
6173 case RELOAD_FOR_INPUT:
6174 case RELOAD_FOR_OPERAND_ADDRESS:
6175 reload_override_in[r] = equiv;
6176 /* Fall through. */
6177 default:
6178 equiv = 0;
6179 break;
6183 /* If we found an equivalent reg, say no code need be generated
6184 to load it, and use it as our reload reg. */
6185 if (equiv != 0 && regno != HARD_FRAME_POINTER_REGNUM)
6187 int nr = HARD_REGNO_NREGS (regno, rld[r].mode);
6188 int k;
6189 rld[r].reg_rtx = equiv;
6190 reload_inherited[r] = 1;
6192 /* If reg_reloaded_valid is not set for this register,
6193 there might be a stale spill_reg_store lying around.
6194 We must clear it, since otherwise emit_reload_insns
6195 might delete the store. */
6196 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
6197 spill_reg_store[regno] = NULL_RTX;
6198 /* If any of the hard registers in EQUIV are spill
6199 registers, mark them as in use for this insn. */
6200 for (k = 0; k < nr; k++)
6202 i = spill_reg_order[regno + k];
6203 if (i >= 0)
6205 mark_reload_reg_in_use (regno, rld[r].opnum,
6206 rld[r].when_needed,
6207 rld[r].mode);
6208 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6209 regno + k);
6215 /* If we found a register to use already, or if this is an optional
6216 reload, we are done. */
6217 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
6218 continue;
6220 #if 0 /* No longer needed for correct operation. Might or might not
6221 give better code on the average. Want to experiment? */
6223 /* See if there is a later reload that has a class different from our
6224 class that intersects our class or that requires less register
6225 than our reload. If so, we must allocate a register to this
6226 reload now, since that reload might inherit a previous reload
6227 and take the only available register in our class. Don't do this
6228 for optional reloads since they will force all previous reloads
6229 to be allocated. Also don't do this for reloads that have been
6230 turned off. */
6232 for (i = j + 1; i < n_reloads; i++)
6234 int s = reload_order[i];
6236 if ((rld[s].in == 0 && rld[s].out == 0
6237 && ! rld[s].secondary_p)
6238 || rld[s].optional)
6239 continue;
6241 if ((rld[s].class != rld[r].class
6242 && reg_classes_intersect_p (rld[r].class,
6243 rld[s].class))
6244 || rld[s].nregs < rld[r].nregs)
6245 break;
6248 if (i == n_reloads)
6249 continue;
6251 allocate_reload_reg (chain, r, j == n_reloads - 1, inheritance);
6252 #endif
6255 /* Now allocate reload registers for anything non-optional that
6256 didn't get one yet. */
6257 for (j = 0; j < n_reloads; j++)
6259 register int r = reload_order[j];
6261 /* Ignore reloads that got marked inoperative. */
6262 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
6263 continue;
6265 /* Skip reloads that already have a register allocated or are
6266 optional. */
6267 if (rld[r].reg_rtx != 0 || rld[r].optional)
6268 continue;
6270 if (! allocate_reload_reg (chain, r, j == n_reloads - 1, inheritance))
6271 break;
6274 /* If that loop got all the way, we have won. */
6275 if (j == n_reloads)
6276 break;
6278 /* Loop around and try without any inheritance. */
6281 /* If we thought we could inherit a reload, because it seemed that
6282 nothing else wanted the same reload register earlier in the insn,
6283 verify that assumption, now that all reloads have been assigned.
6284 Likewise for reloads where reload_override_in has been set. */
6286 /* If doing expensive optimizations, do one preliminary pass that doesn't
6287 cancel any inheritance, but removes reloads that have been needed only
6288 for reloads that we know can be inherited. */
6289 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
6291 for (j = 0; j < n_reloads; j++)
6293 register int r = reload_order[j];
6294 rtx check_reg;
6295 if (reload_inherited[r] && rld[r].reg_rtx)
6296 check_reg = rld[r].reg_rtx;
6297 else if (reload_override_in[r]
6298 && (GET_CODE (reload_override_in[r]) == REG
6299 || GET_CODE (reload_override_in[r]) == SUBREG))
6300 check_reg = reload_override_in[r];
6301 else
6302 continue;
6303 if (! reload_reg_free_for_value_p (true_regnum (check_reg),
6304 rld[r].opnum,
6305 rld[r].when_needed,
6306 rld[r].in,
6307 (reload_inherited[r]
6308 ? rld[r].out : const0_rtx),
6309 r, 1))
6311 if (pass)
6312 continue;
6313 reload_inherited[r] = 0;
6314 reload_override_in[r] = 0;
6316 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
6317 reload_override_in, then we do not need its related
6318 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
6319 likewise for other reload types.
6320 We handle this by removing a reload when its only replacement
6321 is mentioned in reload_in of the reload we are going to inherit.
6322 A special case are auto_inc expressions; even if the input is
6323 inherited, we still need the address for the output. We can
6324 recognize them because they have RELOAD_OUT set to RELOAD_IN.
6325 If we suceeded removing some reload and we are doing a preliminary
6326 pass just to remove such reloads, make another pass, since the
6327 removal of one reload might allow us to inherit another one. */
6328 else if (rld[r].in
6329 && rld[r].out != rld[r].in
6330 && remove_address_replacements (rld[r].in) && pass)
6331 pass = 2;
6335 /* Now that reload_override_in is known valid,
6336 actually override reload_in. */
6337 for (j = 0; j < n_reloads; j++)
6338 if (reload_override_in[j])
6339 rld[j].in = reload_override_in[j];
6341 /* If this reload won't be done because it has been cancelled or is
6342 optional and not inherited, clear reload_reg_rtx so other
6343 routines (such as subst_reloads) don't get confused. */
6344 for (j = 0; j < n_reloads; j++)
6345 if (rld[j].reg_rtx != 0
6346 && ((rld[j].optional && ! reload_inherited[j])
6347 || (rld[j].in == 0 && rld[j].out == 0
6348 && ! rld[j].secondary_p)))
6350 int regno = true_regnum (rld[j].reg_rtx);
6352 if (spill_reg_order[regno] >= 0)
6353 clear_reload_reg_in_use (regno, rld[j].opnum,
6354 rld[j].when_needed, rld[j].mode);
6355 rld[j].reg_rtx = 0;
6358 /* Record which pseudos and which spill regs have output reloads. */
6359 for (j = 0; j < n_reloads; j++)
6361 register int r = reload_order[j];
6363 i = reload_spill_index[r];
6365 /* I is nonneg if this reload uses a register.
6366 If rld[r].reg_rtx is 0, this is an optional reload
6367 that we opted to ignore. */
6368 if (rld[r].out_reg != 0 && GET_CODE (rld[r].out_reg) == REG
6369 && rld[r].reg_rtx != 0)
6371 register int nregno = REGNO (rld[r].out_reg);
6372 int nr = 1;
6374 if (nregno < FIRST_PSEUDO_REGISTER)
6375 nr = HARD_REGNO_NREGS (nregno, rld[r].mode);
6377 while (--nr >= 0)
6378 reg_has_output_reload[nregno + nr] = 1;
6380 if (i >= 0)
6382 nr = HARD_REGNO_NREGS (i, rld[r].mode);
6383 while (--nr >= 0)
6384 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
6387 if (rld[r].when_needed != RELOAD_OTHER
6388 && rld[r].when_needed != RELOAD_FOR_OUTPUT
6389 && rld[r].when_needed != RELOAD_FOR_INSN)
6390 abort ();
6395 /* Deallocate the reload register for reload R. This is called from
6396 remove_address_replacements. */
6397 void
6398 deallocate_reload_reg (r)
6399 int r;
6401 int regno;
6403 if (! rld[r].reg_rtx)
6404 return;
6405 regno = true_regnum (rld[r].reg_rtx);
6406 rld[r].reg_rtx = 0;
6407 if (spill_reg_order[regno] >= 0)
6408 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
6409 rld[r].mode);
6410 reload_spill_index[r] = -1;
6413 /* If SMALL_REGISTER_CLASSES is non-zero, we may not have merged two
6414 reloads of the same item for fear that we might not have enough reload
6415 registers. However, normally they will get the same reload register
6416 and hence actually need not be loaded twice.
6418 Here we check for the most common case of this phenomenon: when we have
6419 a number of reloads for the same object, each of which were allocated
6420 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6421 reload, and is not modified in the insn itself. If we find such,
6422 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6423 This will not increase the number of spill registers needed and will
6424 prevent redundant code. */
6426 static void
6427 merge_assigned_reloads (insn)
6428 rtx insn;
6430 int i, j;
6432 /* Scan all the reloads looking for ones that only load values and
6433 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6434 assigned and not modified by INSN. */
6436 for (i = 0; i < n_reloads; i++)
6438 int conflicting_input = 0;
6439 int max_input_address_opnum = -1;
6440 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6442 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6443 || rld[i].out != 0 || rld[i].reg_rtx == 0
6444 || reg_set_p (rld[i].reg_rtx, insn))
6445 continue;
6447 /* Look at all other reloads. Ensure that the only use of this
6448 reload_reg_rtx is in a reload that just loads the same value
6449 as we do. Note that any secondary reloads must be of the identical
6450 class since the values, modes, and result registers are the
6451 same, so we need not do anything with any secondary reloads. */
6453 for (j = 0; j < n_reloads; j++)
6455 if (i == j || rld[j].reg_rtx == 0
6456 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6457 rld[i].reg_rtx))
6458 continue;
6460 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6461 && rld[j].opnum > max_input_address_opnum)
6462 max_input_address_opnum = rld[j].opnum;
6464 /* If the reload regs aren't exactly the same (e.g, different modes)
6465 or if the values are different, we can't merge this reload.
6466 But if it is an input reload, we might still merge
6467 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6469 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6470 || rld[j].out != 0 || rld[j].in == 0
6471 || ! rtx_equal_p (rld[i].in, rld[j].in))
6473 if (rld[j].when_needed != RELOAD_FOR_INPUT
6474 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6475 || rld[i].opnum > rld[j].opnum)
6476 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6477 break;
6478 conflicting_input = 1;
6479 if (min_conflicting_input_opnum > rld[j].opnum)
6480 min_conflicting_input_opnum = rld[j].opnum;
6484 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6485 we, in fact, found any matching reloads. */
6487 if (j == n_reloads
6488 && max_input_address_opnum <= min_conflicting_input_opnum)
6490 for (j = 0; j < n_reloads; j++)
6491 if (i != j && rld[j].reg_rtx != 0
6492 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6493 && (! conflicting_input
6494 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6495 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6497 rld[i].when_needed = RELOAD_OTHER;
6498 rld[j].in = 0;
6499 reload_spill_index[j] = -1;
6500 transfer_replacements (i, j);
6503 /* If this is now RELOAD_OTHER, look for any reloads that load
6504 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6505 if they were for inputs, RELOAD_OTHER for outputs. Note that
6506 this test is equivalent to looking for reloads for this operand
6507 number. */
6509 if (rld[i].when_needed == RELOAD_OTHER)
6510 for (j = 0; j < n_reloads; j++)
6511 if (rld[j].in != 0
6512 && rld[i].when_needed != RELOAD_OTHER
6513 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6514 rld[i].in))
6515 rld[j].when_needed
6516 = ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
6517 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6518 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6524 /* Output insns to reload values in and out of the chosen reload regs. */
6526 static void
6527 emit_reload_insns (chain)
6528 struct insn_chain *chain;
6530 rtx insn = chain->insn;
6532 register int j;
6533 rtx input_reload_insns[MAX_RECOG_OPERANDS];
6534 rtx other_input_address_reload_insns = 0;
6535 rtx other_input_reload_insns = 0;
6536 rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6537 rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6538 rtx output_reload_insns[MAX_RECOG_OPERANDS];
6539 rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6540 rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6541 rtx operand_reload_insns = 0;
6542 rtx other_operand_reload_insns = 0;
6543 rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6544 rtx following_insn = NEXT_INSN (insn);
6545 rtx before_insn = PREV_INSN (insn);
6546 int special;
6547 /* Values to be put in spill_reg_store are put here first. */
6548 rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6549 HARD_REG_SET reg_reloaded_died;
6551 CLEAR_HARD_REG_SET (reg_reloaded_died);
6553 for (j = 0; j < reload_n_operands; j++)
6554 input_reload_insns[j] = input_address_reload_insns[j]
6555 = inpaddr_address_reload_insns[j]
6556 = output_reload_insns[j] = output_address_reload_insns[j]
6557 = outaddr_address_reload_insns[j]
6558 = other_output_reload_insns[j] = 0;
6560 /* Now output the instructions to copy the data into and out of the
6561 reload registers. Do these in the order that the reloads were reported,
6562 since reloads of base and index registers precede reloads of operands
6563 and the operands may need the base and index registers reloaded. */
6565 for (j = 0; j < n_reloads; j++)
6567 register rtx old;
6568 rtx oldequiv_reg = 0;
6569 rtx this_reload_insn = 0;
6570 int expect_occurrences = 1;
6572 if (rld[j].reg_rtx
6573 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
6574 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
6576 old = (rld[j].in && GET_CODE (rld[j].in) == MEM
6577 ? rld[j].in_reg : rld[j].in);
6579 if (old != 0
6580 /* AUTO_INC reloads need to be handled even if inherited. We got an
6581 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
6582 && (! reload_inherited[j] || (rld[j].out && ! rld[j].out_reg))
6583 && ! rtx_equal_p (rld[j].reg_rtx, old)
6584 && rld[j].reg_rtx != 0)
6586 register rtx reloadreg = rld[j].reg_rtx;
6587 rtx oldequiv = 0;
6588 enum machine_mode mode;
6589 rtx *where;
6591 /* Determine the mode to reload in.
6592 This is very tricky because we have three to choose from.
6593 There is the mode the insn operand wants (rld[J].inmode).
6594 There is the mode of the reload register RELOADREG.
6595 There is the intrinsic mode of the operand, which we could find
6596 by stripping some SUBREGs.
6597 It turns out that RELOADREG's mode is irrelevant:
6598 we can change that arbitrarily.
6600 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6601 then the reload reg may not support QImode moves, so use SImode.
6602 If foo is in memory due to spilling a pseudo reg, this is safe,
6603 because the QImode value is in the least significant part of a
6604 slot big enough for a SImode. If foo is some other sort of
6605 memory reference, then it is impossible to reload this case,
6606 so previous passes had better make sure this never happens.
6608 Then consider a one-word union which has SImode and one of its
6609 members is a float, being fetched as (SUBREG:SF union:SI).
6610 We must fetch that as SFmode because we could be loading into
6611 a float-only register. In this case OLD's mode is correct.
6613 Consider an immediate integer: it has VOIDmode. Here we need
6614 to get a mode from something else.
6616 In some cases, there is a fourth mode, the operand's
6617 containing mode. If the insn specifies a containing mode for
6618 this operand, it overrides all others.
6620 I am not sure whether the algorithm here is always right,
6621 but it does the right things in those cases. */
6623 mode = GET_MODE (old);
6624 if (mode == VOIDmode)
6625 mode = rld[j].inmode;
6627 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6628 /* If we need a secondary register for this operation, see if
6629 the value is already in a register in that class. Don't
6630 do this if the secondary register will be used as a scratch
6631 register. */
6633 if (rld[j].secondary_in_reload >= 0
6634 && rld[j].secondary_in_icode == CODE_FOR_nothing
6635 && optimize)
6636 oldequiv
6637 = find_equiv_reg (old, insn,
6638 rld[rld[j].secondary_in_reload].class,
6639 -1, NULL_PTR, 0, mode);
6640 #endif
6642 /* If reloading from memory, see if there is a register
6643 that already holds the same value. If so, reload from there.
6644 We can pass 0 as the reload_reg_p argument because
6645 any other reload has either already been emitted,
6646 in which case find_equiv_reg will see the reload-insn,
6647 or has yet to be emitted, in which case it doesn't matter
6648 because we will use this equiv reg right away. */
6650 if (oldequiv == 0 && optimize
6651 && (GET_CODE (old) == MEM
6652 || (GET_CODE (old) == REG
6653 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6654 && reg_renumber[REGNO (old)] < 0)))
6655 oldequiv = find_equiv_reg (old, insn, ALL_REGS,
6656 -1, NULL_PTR, 0, mode);
6658 if (oldequiv)
6660 int regno = true_regnum (oldequiv);
6662 /* Don't use OLDEQUIV if any other reload changes it at an
6663 earlier stage of this insn or at this stage. */
6664 if (! reload_reg_free_for_value_p (regno, rld[j].opnum,
6665 rld[j].when_needed,
6666 rld[j].in, const0_rtx, j,
6668 oldequiv = 0;
6670 /* If it is no cheaper to copy from OLDEQUIV into the
6671 reload register than it would be to move from memory,
6672 don't use it. Likewise, if we need a secondary register
6673 or memory. */
6675 if (oldequiv != 0
6676 && ((REGNO_REG_CLASS (regno) != rld[j].class
6677 && (REGISTER_MOVE_COST (REGNO_REG_CLASS (regno),
6678 rld[j].class)
6679 >= MEMORY_MOVE_COST (mode, rld[j].class, 1)))
6680 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6681 || (SECONDARY_INPUT_RELOAD_CLASS (rld[j].class,
6682 mode, oldequiv)
6683 != NO_REGS)
6684 #endif
6685 #ifdef SECONDARY_MEMORY_NEEDED
6686 || SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (regno),
6687 rld[j].class,
6688 mode)
6689 #endif
6691 oldequiv = 0;
6694 /* delete_output_reload is only invoked properly if old contains
6695 the original pseudo register. Since this is replaced with a
6696 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6697 find the pseudo in RELOAD_IN_REG. */
6698 if (oldequiv == 0
6699 && reload_override_in[j]
6700 && GET_CODE (rld[j].in_reg) == REG)
6702 oldequiv = old;
6703 old = rld[j].in_reg;
6705 if (oldequiv == 0)
6706 oldequiv = old;
6707 else if (GET_CODE (oldequiv) == REG)
6708 oldequiv_reg = oldequiv;
6709 else if (GET_CODE (oldequiv) == SUBREG)
6710 oldequiv_reg = SUBREG_REG (oldequiv);
6712 /* If we are reloading from a register that was recently stored in
6713 with an output-reload, see if we can prove there was
6714 actually no need to store the old value in it. */
6716 if (optimize && GET_CODE (oldequiv) == REG
6717 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6718 && spill_reg_store[REGNO (oldequiv)]
6719 && GET_CODE (old) == REG
6720 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6721 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6722 rld[j].out_reg)))
6723 delete_output_reload (insn, j, REGNO (oldequiv));
6725 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6726 then load RELOADREG from OLDEQUIV. Note that we cannot use
6727 gen_lowpart_common since it can do the wrong thing when
6728 RELOADREG has a multi-word mode. Note that RELOADREG
6729 must always be a REG here. */
6731 if (GET_MODE (reloadreg) != mode)
6732 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6733 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6734 oldequiv = SUBREG_REG (oldequiv);
6735 if (GET_MODE (oldequiv) != VOIDmode
6736 && mode != GET_MODE (oldequiv))
6737 oldequiv = gen_rtx_SUBREG (mode, oldequiv, 0);
6739 /* Switch to the right place to emit the reload insns. */
6740 switch (rld[j].when_needed)
6742 case RELOAD_OTHER:
6743 where = &other_input_reload_insns;
6744 break;
6745 case RELOAD_FOR_INPUT:
6746 where = &input_reload_insns[rld[j].opnum];
6747 break;
6748 case RELOAD_FOR_INPUT_ADDRESS:
6749 where = &input_address_reload_insns[rld[j].opnum];
6750 break;
6751 case RELOAD_FOR_INPADDR_ADDRESS:
6752 where = &inpaddr_address_reload_insns[rld[j].opnum];
6753 break;
6754 case RELOAD_FOR_OUTPUT_ADDRESS:
6755 where = &output_address_reload_insns[rld[j].opnum];
6756 break;
6757 case RELOAD_FOR_OUTADDR_ADDRESS:
6758 where = &outaddr_address_reload_insns[rld[j].opnum];
6759 break;
6760 case RELOAD_FOR_OPERAND_ADDRESS:
6761 where = &operand_reload_insns;
6762 break;
6763 case RELOAD_FOR_OPADDR_ADDR:
6764 where = &other_operand_reload_insns;
6765 break;
6766 case RELOAD_FOR_OTHER_ADDRESS:
6767 where = &other_input_address_reload_insns;
6768 break;
6769 default:
6770 abort ();
6773 push_to_sequence (*where);
6774 special = 0;
6776 /* Auto-increment addresses must be reloaded in a special way. */
6777 if (rld[j].out && ! rld[j].out_reg)
6779 /* We are not going to bother supporting the case where a
6780 incremented register can't be copied directly from
6781 OLDEQUIV since this seems highly unlikely. */
6782 if (rld[j].secondary_in_reload >= 0)
6783 abort ();
6785 if (reload_inherited[j])
6786 oldequiv = reloadreg;
6788 old = XEXP (rld[j].in_reg, 0);
6790 if (optimize && GET_CODE (oldequiv) == REG
6791 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6792 && spill_reg_store[REGNO (oldequiv)]
6793 && GET_CODE (old) == REG
6794 && (dead_or_set_p (insn,
6795 spill_reg_stored_to[REGNO (oldequiv)])
6796 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6797 old)))
6798 delete_output_reload (insn, j, REGNO (oldequiv));
6800 /* Prevent normal processing of this reload. */
6801 special = 1;
6802 /* Output a special code sequence for this case. */
6803 new_spill_reg_store[REGNO (reloadreg)]
6804 = inc_for_reload (reloadreg, oldequiv, rld[j].out,
6805 rld[j].inc);
6808 /* If we are reloading a pseudo-register that was set by the previous
6809 insn, see if we can get rid of that pseudo-register entirely
6810 by redirecting the previous insn into our reload register. */
6812 else if (optimize && GET_CODE (old) == REG
6813 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6814 && dead_or_set_p (insn, old)
6815 /* This is unsafe if some other reload
6816 uses the same reg first. */
6817 && reload_reg_free_for_value_p (REGNO (reloadreg),
6818 rld[j].opnum,
6819 rld[j].when_needed,
6820 old, rld[j].out,
6821 j, 0))
6823 rtx temp = PREV_INSN (insn);
6824 while (temp && GET_CODE (temp) == NOTE)
6825 temp = PREV_INSN (temp);
6826 if (temp
6827 && GET_CODE (temp) == INSN
6828 && GET_CODE (PATTERN (temp)) == SET
6829 && SET_DEST (PATTERN (temp)) == old
6830 /* Make sure we can access insn_operand_constraint. */
6831 && asm_noperands (PATTERN (temp)) < 0
6832 /* This is unsafe if prev insn rejects our reload reg. */
6833 && constraint_accepts_reg_p (insn_data[recog_memoized (temp)].operand[0].constraint,
6834 reloadreg)
6835 /* This is unsafe if operand occurs more than once in current
6836 insn. Perhaps some occurrences aren't reloaded. */
6837 && count_occurrences (PATTERN (insn), old) == 1
6838 /* Don't risk splitting a matching pair of operands. */
6839 && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
6841 /* Store into the reload register instead of the pseudo. */
6842 SET_DEST (PATTERN (temp)) = reloadreg;
6844 /* If the previous insn is an output reload, the source is
6845 a reload register, and its spill_reg_store entry will
6846 contain the previous destination. This is now
6847 invalid. */
6848 if (GET_CODE (SET_SRC (PATTERN (temp))) == REG
6849 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6851 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6852 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6855 /* If these are the only uses of the pseudo reg,
6856 pretend for GDB it lives in the reload reg we used. */
6857 if (REG_N_DEATHS (REGNO (old)) == 1
6858 && REG_N_SETS (REGNO (old)) == 1)
6860 reg_renumber[REGNO (old)] = REGNO (rld[j].reg_rtx);
6861 alter_reg (REGNO (old), -1);
6863 special = 1;
6867 /* We can't do that, so output an insn to load RELOADREG. */
6869 if (! special)
6871 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6872 rtx second_reload_reg = 0;
6873 enum insn_code icode;
6875 /* If we have a secondary reload, pick up the secondary register
6876 and icode, if any. If OLDEQUIV and OLD are different or
6877 if this is an in-out reload, recompute whether or not we
6878 still need a secondary register and what the icode should
6879 be. If we still need a secondary register and the class or
6880 icode is different, go back to reloading from OLD if using
6881 OLDEQUIV means that we got the wrong type of register. We
6882 cannot have different class or icode due to an in-out reload
6883 because we don't make such reloads when both the input and
6884 output need secondary reload registers. */
6886 if (rld[j].secondary_in_reload >= 0)
6888 int secondary_reload = rld[j].secondary_in_reload;
6889 rtx real_oldequiv = oldequiv;
6890 rtx real_old = old;
6891 rtx tmp;
6893 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6894 and similarly for OLD.
6895 See comments in get_secondary_reload in reload.c. */
6896 /* If it is a pseudo that cannot be replaced with its
6897 equivalent MEM, we must fall back to reload_in, which
6898 will have all the necessary substitutions registered.
6899 Likewise for a pseudo that can't be replaced with its
6900 equivalent constant.
6902 Take extra care for subregs of such pseudos. Note that
6903 we cannot use reg_equiv_mem in this case because it is
6904 not in the right mode. */
6906 tmp = oldequiv;
6907 if (GET_CODE (tmp) == SUBREG)
6908 tmp = SUBREG_REG (tmp);
6909 if (GET_CODE (tmp) == REG
6910 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6911 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6912 || reg_equiv_constant[REGNO (tmp)] != 0))
6914 if (! reg_equiv_mem[REGNO (tmp)]
6915 || num_not_at_initial_offset
6916 || GET_CODE (oldequiv) == SUBREG)
6917 real_oldequiv = rld[j].in;
6918 else
6919 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6922 tmp = old;
6923 if (GET_CODE (tmp) == SUBREG)
6924 tmp = SUBREG_REG (tmp);
6925 if (GET_CODE (tmp) == REG
6926 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6927 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6928 || reg_equiv_constant[REGNO (tmp)] != 0))
6930 if (! reg_equiv_mem[REGNO (tmp)]
6931 || num_not_at_initial_offset
6932 || GET_CODE (old) == SUBREG)
6933 real_old = rld[j].in;
6934 else
6935 real_old = reg_equiv_mem[REGNO (tmp)];
6938 second_reload_reg = rld[secondary_reload].reg_rtx;
6939 icode = rld[j].secondary_in_icode;
6941 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6942 || (rld[j].in != 0 && rld[j].out != 0))
6944 enum reg_class new_class
6945 = SECONDARY_INPUT_RELOAD_CLASS (rld[j].class,
6946 mode, real_oldequiv);
6948 if (new_class == NO_REGS)
6949 second_reload_reg = 0;
6950 else
6952 enum insn_code new_icode;
6953 enum machine_mode new_mode;
6955 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
6956 REGNO (second_reload_reg)))
6957 oldequiv = old, real_oldequiv = real_old;
6958 else
6960 new_icode = reload_in_optab[(int) mode];
6961 if (new_icode != CODE_FOR_nothing
6962 && ((insn_data[(int) new_icode].operand[0].predicate
6963 && ! ((*insn_data[(int) new_icode].operand[0].predicate)
6964 (reloadreg, mode)))
6965 || (insn_data[(int) new_icode].operand[1].predicate
6966 && ! ((*insn_data[(int) new_icode].operand[1].predicate)
6967 (real_oldequiv, mode)))))
6968 new_icode = CODE_FOR_nothing;
6970 if (new_icode == CODE_FOR_nothing)
6971 new_mode = mode;
6972 else
6973 new_mode = insn_data[(int) new_icode].operand[2].mode;
6975 if (GET_MODE (second_reload_reg) != new_mode)
6977 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6978 new_mode))
6979 oldequiv = old, real_oldequiv = real_old;
6980 else
6981 second_reload_reg
6982 = gen_rtx_REG (new_mode,
6983 REGNO (second_reload_reg));
6989 /* If we still need a secondary reload register, check
6990 to see if it is being used as a scratch or intermediate
6991 register and generate code appropriately. If we need
6992 a scratch register, use REAL_OLDEQUIV since the form of
6993 the insn may depend on the actual address if it is
6994 a MEM. */
6996 if (second_reload_reg)
6998 if (icode != CODE_FOR_nothing)
7000 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
7001 second_reload_reg));
7002 special = 1;
7004 else
7006 /* See if we need a scratch register to load the
7007 intermediate register (a tertiary reload). */
7008 enum insn_code tertiary_icode
7009 = rld[secondary_reload].secondary_in_icode;
7011 if (tertiary_icode != CODE_FOR_nothing)
7013 rtx third_reload_reg
7014 = rld[rld[secondary_reload].secondary_in_reload].reg_rtx;
7016 emit_insn ((GEN_FCN (tertiary_icode)
7017 (second_reload_reg, real_oldequiv,
7018 third_reload_reg)));
7020 else
7021 gen_reload (second_reload_reg, real_oldequiv,
7022 rld[j].opnum,
7023 rld[j].when_needed);
7025 oldequiv = second_reload_reg;
7029 #endif
7031 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
7033 rtx real_oldequiv = oldequiv;
7035 if ((GET_CODE (oldequiv) == REG
7036 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
7037 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
7038 || reg_equiv_constant[REGNO (oldequiv)] != 0))
7039 || (GET_CODE (oldequiv) == SUBREG
7040 && GET_CODE (SUBREG_REG (oldequiv)) == REG
7041 && (REGNO (SUBREG_REG (oldequiv))
7042 >= FIRST_PSEUDO_REGISTER)
7043 && ((reg_equiv_memory_loc
7044 [REGNO (SUBREG_REG (oldequiv))] != 0)
7045 || (reg_equiv_constant
7046 [REGNO (SUBREG_REG (oldequiv))] != 0))))
7047 real_oldequiv = rld[j].in;
7048 gen_reload (reloadreg, real_oldequiv, rld[j].opnum,
7049 rld[j].when_needed);
7054 this_reload_insn = get_last_insn ();
7055 /* End this sequence. */
7056 *where = get_insns ();
7057 end_sequence ();
7059 /* Update reload_override_in so that delete_address_reloads_1
7060 can see the actual register usage. */
7061 if (oldequiv_reg)
7062 reload_override_in[j] = oldequiv;
7065 /* When inheriting a wider reload, we have a MEM in rld[j].in,
7066 e.g. inheriting a SImode output reload for
7067 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
7068 if (optimize && reload_inherited[j] && rld[j].in
7069 && GET_CODE (rld[j].in) == MEM
7070 && GET_CODE (rld[j].in_reg) == MEM
7071 && reload_spill_index[j] >= 0
7072 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
7074 expect_occurrences
7075 = count_occurrences (PATTERN (insn), rld[j].in) == 1 ? 0 : -1;
7076 rld[j].in
7077 = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
7080 /* If we are reloading a register that was recently stored in with an
7081 output-reload, see if we can prove there was
7082 actually no need to store the old value in it. */
7084 if (optimize
7085 && (reload_inherited[j] || reload_override_in[j])
7086 && rld[j].reg_rtx
7087 && GET_CODE (rld[j].reg_rtx) == REG
7088 && spill_reg_store[REGNO (rld[j].reg_rtx)] != 0
7089 #if 0
7090 /* There doesn't seem to be any reason to restrict this to pseudos
7091 and doing so loses in the case where we are copying from a
7092 register of the wrong class. */
7093 && (REGNO (spill_reg_stored_to[REGNO (rld[j].reg_rtx)])
7094 >= FIRST_PSEUDO_REGISTER)
7095 #endif
7096 /* The insn might have already some references to stackslots
7097 replaced by MEMs, while reload_out_reg still names the
7098 original pseudo. */
7099 && (dead_or_set_p (insn,
7100 spill_reg_stored_to[REGNO (rld[j].reg_rtx)])
7101 || rtx_equal_p (spill_reg_stored_to[REGNO (rld[j].reg_rtx)],
7102 rld[j].out_reg)))
7103 delete_output_reload (insn, j, REGNO (rld[j].reg_rtx));
7105 /* Input-reloading is done. Now do output-reloading,
7106 storing the value from the reload-register after the main insn
7107 if rld[j].out is nonzero.
7109 ??? At some point we need to support handling output reloads of
7110 JUMP_INSNs or insns that set cc0. */
7112 /* If this is an output reload that stores something that is
7113 not loaded in this same reload, see if we can eliminate a previous
7114 store. */
7116 rtx pseudo = rld[j].out_reg;
7118 if (pseudo
7119 && GET_CODE (pseudo) == REG
7120 && ! rtx_equal_p (rld[j].in_reg, pseudo)
7121 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
7122 && reg_last_reload_reg[REGNO (pseudo)])
7124 int pseudo_no = REGNO (pseudo);
7125 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
7127 /* We don't need to test full validity of last_regno for
7128 inherit here; we only want to know if the store actually
7129 matches the pseudo. */
7130 if (reg_reloaded_contents[last_regno] == pseudo_no
7131 && spill_reg_store[last_regno]
7132 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
7133 delete_output_reload (insn, j, last_regno);
7137 old = rld[j].out_reg;
7138 if (old != 0
7139 && rld[j].reg_rtx != old
7140 && rld[j].reg_rtx != 0)
7142 register rtx reloadreg = rld[j].reg_rtx;
7143 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
7144 register rtx second_reloadreg = 0;
7145 #endif
7146 rtx note, p;
7147 enum machine_mode mode;
7148 int special = 0;
7150 /* An output operand that dies right away does need a reload,
7151 but need not be copied from it. Show the new location in the
7152 REG_UNUSED note. */
7153 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
7154 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
7156 XEXP (note, 0) = rld[j].reg_rtx;
7157 continue;
7159 /* Likewise for a SUBREG of an operand that dies. */
7160 else if (GET_CODE (old) == SUBREG
7161 && GET_CODE (SUBREG_REG (old)) == REG
7162 && 0 != (note = find_reg_note (insn, REG_UNUSED,
7163 SUBREG_REG (old))))
7165 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
7166 rld[j].reg_rtx);
7167 continue;
7169 else if (GET_CODE (old) == SCRATCH)
7170 /* If we aren't optimizing, there won't be a REG_UNUSED note,
7171 but we don't want to make an output reload. */
7172 continue;
7174 #if 0
7175 /* Strip off of OLD any size-increasing SUBREGs such as
7176 (SUBREG:SI foo:QI 0). */
7178 while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
7179 && (GET_MODE_SIZE (GET_MODE (old))
7180 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
7181 old = SUBREG_REG (old);
7182 #endif
7184 /* If is a JUMP_INSN, we can't support output reloads yet. */
7185 if (GET_CODE (insn) == JUMP_INSN)
7186 abort ();
7188 if (rld[j].when_needed == RELOAD_OTHER)
7189 start_sequence ();
7190 else
7191 push_to_sequence (output_reload_insns[rld[j].opnum]);
7193 old = rld[j].out;
7195 /* Determine the mode to reload in.
7196 See comments above (for input reloading). */
7198 mode = GET_MODE (old);
7199 if (mode == VOIDmode)
7201 /* VOIDmode should never happen for an output. */
7202 if (asm_noperands (PATTERN (insn)) < 0)
7203 /* It's the compiler's fault. */
7204 fatal_insn ("VOIDmode on an output", insn);
7205 error_for_asm (insn, "output operand is constant in `asm'");
7206 /* Prevent crash--use something we know is valid. */
7207 mode = word_mode;
7208 old = gen_rtx_REG (mode, REGNO (reloadreg));
7211 if (GET_MODE (reloadreg) != mode)
7212 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
7214 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
7216 /* If we need two reload regs, set RELOADREG to the intermediate
7217 one, since it will be stored into OLD. We might need a secondary
7218 register only for an input reload, so check again here. */
7220 if (rld[j].secondary_out_reload >= 0)
7222 rtx real_old = old;
7224 if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
7225 && reg_equiv_mem[REGNO (old)] != 0)
7226 real_old = reg_equiv_mem[REGNO (old)];
7228 if((SECONDARY_OUTPUT_RELOAD_CLASS (rld[j].class,
7229 mode, real_old)
7230 != NO_REGS))
7232 second_reloadreg = reloadreg;
7233 reloadreg = rld[rld[j].secondary_out_reload].reg_rtx;
7235 /* See if RELOADREG is to be used as a scratch register
7236 or as an intermediate register. */
7237 if (rld[j].secondary_out_icode != CODE_FOR_nothing)
7239 emit_insn ((GEN_FCN (rld[j].secondary_out_icode)
7240 (real_old, second_reloadreg, reloadreg)));
7241 special = 1;
7243 else
7245 /* See if we need both a scratch and intermediate reload
7246 register. */
7248 int secondary_reload = rld[j].secondary_out_reload;
7249 enum insn_code tertiary_icode
7250 = rld[secondary_reload].secondary_out_icode;
7252 if (GET_MODE (reloadreg) != mode)
7253 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
7255 if (tertiary_icode != CODE_FOR_nothing)
7257 rtx third_reloadreg
7258 = rld[rld[secondary_reload].secondary_out_reload].reg_rtx;
7259 rtx tem;
7261 /* Copy primary reload reg to secondary reload reg.
7262 (Note that these have been swapped above, then
7263 secondary reload reg to OLD using our insn. */
7265 /* If REAL_OLD is a paradoxical SUBREG, remove it
7266 and try to put the opposite SUBREG on
7267 RELOADREG. */
7268 if (GET_CODE (real_old) == SUBREG
7269 && (GET_MODE_SIZE (GET_MODE (real_old))
7270 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
7271 && 0 != (tem = gen_lowpart_common
7272 (GET_MODE (SUBREG_REG (real_old)),
7273 reloadreg)))
7274 real_old = SUBREG_REG (real_old), reloadreg = tem;
7276 gen_reload (reloadreg, second_reloadreg,
7277 rld[j].opnum, rld[j].when_needed);
7278 emit_insn ((GEN_FCN (tertiary_icode)
7279 (real_old, reloadreg, third_reloadreg)));
7280 special = 1;
7283 else
7284 /* Copy between the reload regs here and then to
7285 OUT later. */
7287 gen_reload (reloadreg, second_reloadreg,
7288 rld[j].opnum, rld[j].when_needed);
7292 #endif
7294 /* Output the last reload insn. */
7295 if (! special)
7297 rtx set;
7299 /* Don't output the last reload if OLD is not the dest of
7300 INSN and is in the src and is clobbered by INSN. */
7301 if (! flag_expensive_optimizations
7302 || GET_CODE (old) != REG
7303 || !(set = single_set (insn))
7304 || rtx_equal_p (old, SET_DEST (set))
7305 || !reg_mentioned_p (old, SET_SRC (set))
7306 || !regno_clobbered_p (REGNO (old), insn))
7307 gen_reload (old, reloadreg, rld[j].opnum,
7308 rld[j].when_needed);
7311 /* Look at all insns we emitted, just to be safe. */
7312 for (p = get_insns (); p; p = NEXT_INSN (p))
7313 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
7315 rtx pat = PATTERN (p);
7317 /* If this output reload doesn't come from a spill reg,
7318 clear any memory of reloaded copies of the pseudo reg.
7319 If this output reload comes from a spill reg,
7320 reg_has_output_reload will make this do nothing. */
7321 note_stores (pat, forget_old_reloads_1, NULL);
7323 if (reg_mentioned_p (rld[j].reg_rtx, pat))
7325 rtx set = single_set (insn);
7326 if (reload_spill_index[j] < 0
7327 && set
7328 && SET_SRC (set) == rld[j].reg_rtx)
7330 int src = REGNO (SET_SRC (set));
7332 reload_spill_index[j] = src;
7333 SET_HARD_REG_BIT (reg_is_output_reload, src);
7334 if (find_regno_note (insn, REG_DEAD, src))
7335 SET_HARD_REG_BIT (reg_reloaded_died, src);
7337 if (REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
7339 int s = rld[j].secondary_out_reload;
7340 set = single_set (p);
7341 /* If this reload copies only to the secondary reload
7342 register, the secondary reload does the actual
7343 store. */
7344 if (s >= 0 && set == NULL_RTX)
7345 ; /* We can't tell what function the secondary reload
7346 has and where the actual store to the pseudo is
7347 made; leave new_spill_reg_store alone. */
7348 else if (s >= 0
7349 && SET_SRC (set) == rld[j].reg_rtx
7350 && SET_DEST (set) == rld[s].reg_rtx)
7352 /* Usually the next instruction will be the
7353 secondary reload insn; if we can confirm
7354 that it is, setting new_spill_reg_store to
7355 that insn will allow an extra optimization. */
7356 rtx s_reg = rld[s].reg_rtx;
7357 rtx next = NEXT_INSN (p);
7358 rld[s].out = rld[j].out;
7359 rld[s].out_reg = rld[j].out_reg;
7360 set = single_set (next);
7361 if (set && SET_SRC (set) == s_reg
7362 && ! new_spill_reg_store[REGNO (s_reg)])
7364 SET_HARD_REG_BIT (reg_is_output_reload,
7365 REGNO (s_reg));
7366 new_spill_reg_store[REGNO (s_reg)] = next;
7369 else
7370 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = p;
7375 if (rld[j].when_needed == RELOAD_OTHER)
7377 emit_insns (other_output_reload_insns[rld[j].opnum]);
7378 other_output_reload_insns[rld[j].opnum] = get_insns ();
7380 else
7381 output_reload_insns[rld[j].opnum] = get_insns ();
7383 end_sequence ();
7387 /* Now write all the insns we made for reloads in the order expected by
7388 the allocation functions. Prior to the insn being reloaded, we write
7389 the following reloads:
7391 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7393 RELOAD_OTHER reloads.
7395 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7396 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7397 RELOAD_FOR_INPUT reload for the operand.
7399 RELOAD_FOR_OPADDR_ADDRS reloads.
7401 RELOAD_FOR_OPERAND_ADDRESS reloads.
7403 After the insn being reloaded, we write the following:
7405 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7406 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7407 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7408 reloads for the operand. The RELOAD_OTHER output reloads are
7409 output in descending order by reload number. */
7411 emit_insns_before (other_input_address_reload_insns, insn);
7412 emit_insns_before (other_input_reload_insns, insn);
7414 for (j = 0; j < reload_n_operands; j++)
7416 emit_insns_before (inpaddr_address_reload_insns[j], insn);
7417 emit_insns_before (input_address_reload_insns[j], insn);
7418 emit_insns_before (input_reload_insns[j], insn);
7421 emit_insns_before (other_operand_reload_insns, insn);
7422 emit_insns_before (operand_reload_insns, insn);
7424 for (j = 0; j < reload_n_operands; j++)
7426 emit_insns_before (outaddr_address_reload_insns[j], following_insn);
7427 emit_insns_before (output_address_reload_insns[j], following_insn);
7428 emit_insns_before (output_reload_insns[j], following_insn);
7429 emit_insns_before (other_output_reload_insns[j], following_insn);
7432 /* Keep basic block info up to date. */
7433 if (n_basic_blocks)
7435 if (BLOCK_HEAD (chain->block) == insn)
7436 BLOCK_HEAD (chain->block) = NEXT_INSN (before_insn);
7437 if (BLOCK_END (chain->block) == insn)
7438 BLOCK_END (chain->block) = PREV_INSN (following_insn);
7441 /* For all the spill regs newly reloaded in this instruction,
7442 record what they were reloaded from, so subsequent instructions
7443 can inherit the reloads.
7445 Update spill_reg_store for the reloads of this insn.
7446 Copy the elements that were updated in the loop above. */
7448 for (j = 0; j < n_reloads; j++)
7450 register int r = reload_order[j];
7451 register int i = reload_spill_index[r];
7453 /* If this is a non-inherited input reload from a pseudo, we must
7454 clear any memory of a previous store to the same pseudo. Only do
7455 something if there will not be an output reload for the pseudo
7456 being reloaded. */
7457 if (rld[r].in_reg != 0
7458 && ! (reload_inherited[r] || reload_override_in[r]))
7460 rtx reg = rld[r].in_reg;
7462 if (GET_CODE (reg) == SUBREG)
7463 reg = SUBREG_REG (reg);
7465 if (GET_CODE (reg) == REG
7466 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7467 && ! reg_has_output_reload[REGNO (reg)])
7469 int nregno = REGNO (reg);
7471 if (reg_last_reload_reg[nregno])
7473 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7475 if (reg_reloaded_contents[last_regno] == nregno)
7476 spill_reg_store[last_regno] = 0;
7481 /* I is nonneg if this reload used a register.
7482 If rld[r].reg_rtx is 0, this is an optional reload
7483 that we opted to ignore. */
7485 if (i >= 0 && rld[r].reg_rtx != 0)
7487 int nr
7488 = HARD_REGNO_NREGS (i, GET_MODE (rld[r].reg_rtx));
7489 int k;
7490 int part_reaches_end = 0;
7491 int all_reaches_end = 1;
7493 /* For a multi register reload, we need to check if all or part
7494 of the value lives to the end. */
7495 for (k = 0; k < nr; k++)
7497 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7498 rld[r].when_needed))
7499 part_reaches_end = 1;
7500 else
7501 all_reaches_end = 0;
7504 /* Ignore reloads that don't reach the end of the insn in
7505 entirety. */
7506 if (all_reaches_end)
7508 /* First, clear out memory of what used to be in this spill reg.
7509 If consecutive registers are used, clear them all. */
7511 for (k = 0; k < nr; k++)
7512 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7514 /* Maybe the spill reg contains a copy of reload_out. */
7515 if (rld[r].out != 0
7516 && (GET_CODE (rld[r].out) == REG
7517 #ifdef AUTO_INC_DEC
7518 || ! rld[r].out_reg
7519 #endif
7520 || GET_CODE (rld[r].out_reg) == REG))
7522 rtx out = (GET_CODE (rld[r].out) == REG
7523 ? rld[r].out
7524 : rld[r].out_reg
7525 ? rld[r].out_reg
7526 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7527 register int nregno = REGNO (out);
7528 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7529 : HARD_REGNO_NREGS (nregno,
7530 GET_MODE (rld[r].reg_rtx)));
7532 spill_reg_store[i] = new_spill_reg_store[i];
7533 spill_reg_stored_to[i] = out;
7534 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7536 /* If NREGNO is a hard register, it may occupy more than
7537 one register. If it does, say what is in the
7538 rest of the registers assuming that both registers
7539 agree on how many words the object takes. If not,
7540 invalidate the subsequent registers. */
7542 if (nregno < FIRST_PSEUDO_REGISTER)
7543 for (k = 1; k < nnr; k++)
7544 reg_last_reload_reg[nregno + k]
7545 = (nr == nnr
7546 ? gen_rtx_REG (reg_raw_mode[REGNO (rld[r].reg_rtx) + k],
7547 REGNO (rld[r].reg_rtx) + k)
7548 : 0);
7550 /* Now do the inverse operation. */
7551 for (k = 0; k < nr; k++)
7553 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7554 reg_reloaded_contents[i + k]
7555 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7556 ? nregno
7557 : nregno + k);
7558 reg_reloaded_insn[i + k] = insn;
7559 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7563 /* Maybe the spill reg contains a copy of reload_in. Only do
7564 something if there will not be an output reload for
7565 the register being reloaded. */
7566 else if (rld[r].out_reg == 0
7567 && rld[r].in != 0
7568 && ((GET_CODE (rld[r].in) == REG
7569 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7570 && ! reg_has_output_reload[REGNO (rld[r].in)])
7571 || (GET_CODE (rld[r].in_reg) == REG
7572 && ! reg_has_output_reload[REGNO (rld[r].in_reg)]))
7573 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7575 register int nregno;
7576 int nnr;
7578 if (GET_CODE (rld[r].in) == REG
7579 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7580 nregno = REGNO (rld[r].in);
7581 else if (GET_CODE (rld[r].in_reg) == REG)
7582 nregno = REGNO (rld[r].in_reg);
7583 else
7584 nregno = REGNO (XEXP (rld[r].in_reg, 0));
7586 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7587 : HARD_REGNO_NREGS (nregno,
7588 GET_MODE (rld[r].reg_rtx)));
7590 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7592 if (nregno < FIRST_PSEUDO_REGISTER)
7593 for (k = 1; k < nnr; k++)
7594 reg_last_reload_reg[nregno + k]
7595 = (nr == nnr
7596 ? gen_rtx_REG (reg_raw_mode[REGNO (rld[r].reg_rtx) + k],
7597 REGNO (rld[r].reg_rtx) + k)
7598 : 0);
7600 /* Unless we inherited this reload, show we haven't
7601 recently done a store.
7602 Previous stores of inherited auto_inc expressions
7603 also have to be discarded. */
7604 if (! reload_inherited[r]
7605 || (rld[r].out && ! rld[r].out_reg))
7606 spill_reg_store[i] = 0;
7608 for (k = 0; k < nr; k++)
7610 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7611 reg_reloaded_contents[i + k]
7612 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7613 ? nregno
7614 : nregno + k);
7615 reg_reloaded_insn[i + k] = insn;
7616 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7621 /* However, if part of the reload reaches the end, then we must
7622 invalidate the old info for the part that survives to the end. */
7623 else if (part_reaches_end)
7625 for (k = 0; k < nr; k++)
7626 if (reload_reg_reaches_end_p (i + k,
7627 rld[r].opnum,
7628 rld[r].when_needed))
7629 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7633 /* The following if-statement was #if 0'd in 1.34 (or before...).
7634 It's reenabled in 1.35 because supposedly nothing else
7635 deals with this problem. */
7637 /* If a register gets output-reloaded from a non-spill register,
7638 that invalidates any previous reloaded copy of it.
7639 But forget_old_reloads_1 won't get to see it, because
7640 it thinks only about the original insn. So invalidate it here. */
7641 if (i < 0 && rld[r].out != 0
7642 && (GET_CODE (rld[r].out) == REG
7643 || (GET_CODE (rld[r].out) == MEM
7644 && GET_CODE (rld[r].out_reg) == REG)))
7646 rtx out = (GET_CODE (rld[r].out) == REG
7647 ? rld[r].out : rld[r].out_reg);
7648 register int nregno = REGNO (out);
7649 if (nregno >= FIRST_PSEUDO_REGISTER)
7651 rtx src_reg, store_insn = NULL_RTX;
7653 reg_last_reload_reg[nregno] = 0;
7655 /* If we can find a hard register that is stored, record
7656 the storing insn so that we may delete this insn with
7657 delete_output_reload. */
7658 src_reg = rld[r].reg_rtx;
7660 /* If this is an optional reload, try to find the source reg
7661 from an input reload. */
7662 if (! src_reg)
7664 rtx set = single_set (insn);
7665 if (set && SET_DEST (set) == rld[r].out)
7667 int k;
7669 src_reg = SET_SRC (set);
7670 store_insn = insn;
7671 for (k = 0; k < n_reloads; k++)
7673 if (rld[k].in == src_reg)
7675 src_reg = rld[k].reg_rtx;
7676 break;
7681 else
7682 store_insn = new_spill_reg_store[REGNO (src_reg)];
7683 if (src_reg && GET_CODE (src_reg) == REG
7684 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7686 int src_regno = REGNO (src_reg);
7687 int nr = HARD_REGNO_NREGS (src_regno, rld[r].mode);
7688 /* The place where to find a death note varies with
7689 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7690 necessarily checked exactly in the code that moves
7691 notes, so just check both locations. */
7692 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7693 if (! note)
7694 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7695 while (nr-- > 0)
7697 spill_reg_store[src_regno + nr] = store_insn;
7698 spill_reg_stored_to[src_regno + nr] = out;
7699 reg_reloaded_contents[src_regno + nr] = nregno;
7700 reg_reloaded_insn[src_regno + nr] = store_insn;
7701 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7702 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7703 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7704 if (note)
7705 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7706 else
7707 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7709 reg_last_reload_reg[nregno] = src_reg;
7712 else
7714 int num_regs = HARD_REGNO_NREGS (nregno,GET_MODE (rld[r].out));
7716 while (num_regs-- > 0)
7717 reg_last_reload_reg[nregno + num_regs] = 0;
7721 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7724 /* Emit code to perform a reload from IN (which may be a reload register) to
7725 OUT (which may also be a reload register). IN or OUT is from operand
7726 OPNUM with reload type TYPE.
7728 Returns first insn emitted. */
7731 gen_reload (out, in, opnum, type)
7732 rtx out;
7733 rtx in;
7734 int opnum;
7735 enum reload_type type;
7737 rtx last = get_last_insn ();
7738 rtx tem;
7740 /* If IN is a paradoxical SUBREG, remove it and try to put the
7741 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7742 if (GET_CODE (in) == SUBREG
7743 && (GET_MODE_SIZE (GET_MODE (in))
7744 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7745 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7746 in = SUBREG_REG (in), out = tem;
7747 else if (GET_CODE (out) == SUBREG
7748 && (GET_MODE_SIZE (GET_MODE (out))
7749 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7750 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7751 out = SUBREG_REG (out), in = tem;
7753 /* How to do this reload can get quite tricky. Normally, we are being
7754 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7755 register that didn't get a hard register. In that case we can just
7756 call emit_move_insn.
7758 We can also be asked to reload a PLUS that adds a register or a MEM to
7759 another register, constant or MEM. This can occur during frame pointer
7760 elimination and while reloading addresses. This case is handled by
7761 trying to emit a single insn to perform the add. If it is not valid,
7762 we use a two insn sequence.
7764 Finally, we could be called to handle an 'o' constraint by putting
7765 an address into a register. In that case, we first try to do this
7766 with a named pattern of "reload_load_address". If no such pattern
7767 exists, we just emit a SET insn and hope for the best (it will normally
7768 be valid on machines that use 'o').
7770 This entire process is made complex because reload will never
7771 process the insns we generate here and so we must ensure that
7772 they will fit their constraints and also by the fact that parts of
7773 IN might be being reloaded separately and replaced with spill registers.
7774 Because of this, we are, in some sense, just guessing the right approach
7775 here. The one listed above seems to work.
7777 ??? At some point, this whole thing needs to be rethought. */
7779 if (GET_CODE (in) == PLUS
7780 && (GET_CODE (XEXP (in, 0)) == REG
7781 || GET_CODE (XEXP (in, 0)) == SUBREG
7782 || GET_CODE (XEXP (in, 0)) == MEM)
7783 && (GET_CODE (XEXP (in, 1)) == REG
7784 || GET_CODE (XEXP (in, 1)) == SUBREG
7785 || CONSTANT_P (XEXP (in, 1))
7786 || GET_CODE (XEXP (in, 1)) == MEM))
7788 /* We need to compute the sum of a register or a MEM and another
7789 register, constant, or MEM, and put it into the reload
7790 register. The best possible way of doing this is if the machine
7791 has a three-operand ADD insn that accepts the required operands.
7793 The simplest approach is to try to generate such an insn and see if it
7794 is recognized and matches its constraints. If so, it can be used.
7796 It might be better not to actually emit the insn unless it is valid,
7797 but we need to pass the insn as an operand to `recog' and
7798 `extract_insn' and it is simpler to emit and then delete the insn if
7799 not valid than to dummy things up. */
7801 rtx op0, op1, tem, insn;
7802 int code;
7804 op0 = find_replacement (&XEXP (in, 0));
7805 op1 = find_replacement (&XEXP (in, 1));
7807 /* Since constraint checking is strict, commutativity won't be
7808 checked, so we need to do that here to avoid spurious failure
7809 if the add instruction is two-address and the second operand
7810 of the add is the same as the reload reg, which is frequently
7811 the case. If the insn would be A = B + A, rearrange it so
7812 it will be A = A + B as constrain_operands expects. */
7814 if (GET_CODE (XEXP (in, 1)) == REG
7815 && REGNO (out) == REGNO (XEXP (in, 1)))
7816 tem = op0, op0 = op1, op1 = tem;
7818 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7819 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7821 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
7822 code = recog_memoized (insn);
7824 if (code >= 0)
7826 extract_insn (insn);
7827 /* We want constrain operands to treat this insn strictly in
7828 its validity determination, i.e., the way it would after reload
7829 has completed. */
7830 if (constrain_operands (1))
7831 return insn;
7834 delete_insns_since (last);
7836 /* If that failed, we must use a conservative two-insn sequence.
7838 Use a move to copy one operand into the reload register. Prefer
7839 to reload a constant, MEM or pseudo since the move patterns can
7840 handle an arbitrary operand. If OP1 is not a constant, MEM or
7841 pseudo and OP1 is not a valid operand for an add instruction, then
7842 reload OP1.
7844 After reloading one of the operands into the reload register, add
7845 the reload register to the output register.
7847 If there is another way to do this for a specific machine, a
7848 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7849 we emit below. */
7851 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7853 if (CONSTANT_P (op1) || GET_CODE (op1) == MEM || GET_CODE (op1) == SUBREG
7854 || (GET_CODE (op1) == REG
7855 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7856 || (code != CODE_FOR_nothing
7857 && ! ((*insn_data[code].operand[2].predicate)
7858 (op1, insn_data[code].operand[2].mode))))
7859 tem = op0, op0 = op1, op1 = tem;
7861 gen_reload (out, op0, opnum, type);
7863 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7864 This fixes a problem on the 32K where the stack pointer cannot
7865 be used as an operand of an add insn. */
7867 if (rtx_equal_p (op0, op1))
7868 op1 = out;
7870 insn = emit_insn (gen_add2_insn (out, op1));
7872 /* If that failed, copy the address register to the reload register.
7873 Then add the constant to the reload register. */
7875 code = recog_memoized (insn);
7877 if (code >= 0)
7879 extract_insn (insn);
7880 /* We want constrain operands to treat this insn strictly in
7881 its validity determination, i.e., the way it would after reload
7882 has completed. */
7883 if (constrain_operands (1))
7885 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7886 REG_NOTES (insn)
7887 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7888 return insn;
7892 delete_insns_since (last);
7894 gen_reload (out, op1, opnum, type);
7895 insn = emit_insn (gen_add2_insn (out, op0));
7896 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7899 #ifdef SECONDARY_MEMORY_NEEDED
7900 /* If we need a memory location to do the move, do it that way. */
7901 else if (GET_CODE (in) == REG && REGNO (in) < FIRST_PSEUDO_REGISTER
7902 && GET_CODE (out) == REG && REGNO (out) < FIRST_PSEUDO_REGISTER
7903 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (in)),
7904 REGNO_REG_CLASS (REGNO (out)),
7905 GET_MODE (out)))
7907 /* Get the memory to use and rewrite both registers to its mode. */
7908 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7910 if (GET_MODE (loc) != GET_MODE (out))
7911 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7913 if (GET_MODE (loc) != GET_MODE (in))
7914 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7916 gen_reload (loc, in, opnum, type);
7917 gen_reload (out, loc, opnum, type);
7919 #endif
7921 /* If IN is a simple operand, use gen_move_insn. */
7922 else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
7923 emit_insn (gen_move_insn (out, in));
7925 #ifdef HAVE_reload_load_address
7926 else if (HAVE_reload_load_address)
7927 emit_insn (gen_reload_load_address (out, in));
7928 #endif
7930 /* Otherwise, just write (set OUT IN) and hope for the best. */
7931 else
7932 emit_insn (gen_rtx_SET (VOIDmode, out, in));
7934 /* Return the first insn emitted.
7935 We can not just return get_last_insn, because there may have
7936 been multiple instructions emitted. Also note that gen_move_insn may
7937 emit more than one insn itself, so we can not assume that there is one
7938 insn emitted per emit_insn_before call. */
7940 return last ? NEXT_INSN (last) : get_insns ();
7943 /* Delete a previously made output-reload
7944 whose result we now believe is not needed.
7945 First we double-check.
7947 INSN is the insn now being processed.
7948 LAST_RELOAD_REG is the hard register number for which we want to delete
7949 the last output reload.
7950 J is the reload-number that originally used REG. The caller has made
7951 certain that reload J doesn't use REG any longer for input. */
7953 static void
7954 delete_output_reload (insn, j, last_reload_reg)
7955 rtx insn;
7956 int j;
7957 int last_reload_reg;
7959 rtx output_reload_insn = spill_reg_store[last_reload_reg];
7960 rtx reg = spill_reg_stored_to[last_reload_reg];
7961 int k;
7962 int n_occurrences;
7963 int n_inherited = 0;
7964 register rtx i1;
7965 rtx substed;
7967 /* Get the raw pseudo-register referred to. */
7969 while (GET_CODE (reg) == SUBREG)
7970 reg = SUBREG_REG (reg);
7971 substed = reg_equiv_memory_loc[REGNO (reg)];
7973 /* This is unsafe if the operand occurs more often in the current
7974 insn than it is inherited. */
7975 for (k = n_reloads - 1; k >= 0; k--)
7977 rtx reg2 = rld[k].in;
7978 if (! reg2)
7979 continue;
7980 if (GET_CODE (reg2) == MEM || reload_override_in[k])
7981 reg2 = rld[k].in_reg;
7982 #ifdef AUTO_INC_DEC
7983 if (rld[k].out && ! rld[k].out_reg)
7984 reg2 = XEXP (rld[k].in_reg, 0);
7985 #endif
7986 while (GET_CODE (reg2) == SUBREG)
7987 reg2 = SUBREG_REG (reg2);
7988 if (rtx_equal_p (reg2, reg))
7990 if (reload_inherited[k] || reload_override_in[k] || k == j)
7992 n_inherited++;
7993 reg2 = rld[k].out_reg;
7994 if (! reg2)
7995 continue;
7996 while (GET_CODE (reg2) == SUBREG)
7997 reg2 = XEXP (reg2, 0);
7998 if (rtx_equal_p (reg2, reg))
7999 n_inherited++;
8001 else
8002 return;
8005 n_occurrences = count_occurrences (PATTERN (insn), reg);
8006 if (substed)
8007 n_occurrences += count_occurrences (PATTERN (insn), substed);
8008 if (n_occurrences > n_inherited)
8009 return;
8011 /* If the pseudo-reg we are reloading is no longer referenced
8012 anywhere between the store into it and here,
8013 and no jumps or labels intervene, then the value can get
8014 here through the reload reg alone.
8015 Otherwise, give up--return. */
8016 for (i1 = NEXT_INSN (output_reload_insn);
8017 i1 != insn; i1 = NEXT_INSN (i1))
8019 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
8020 return;
8021 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
8022 && reg_mentioned_p (reg, PATTERN (i1)))
8024 /* If this is USE in front of INSN, we only have to check that
8025 there are no more references than accounted for by inheritance. */
8026 while (GET_CODE (i1) == INSN && GET_CODE (PATTERN (i1)) == USE)
8028 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
8029 i1 = NEXT_INSN (i1);
8031 if (n_occurrences <= n_inherited && i1 == insn)
8032 break;
8033 return;
8037 /* The caller has already checked that REG dies or is set in INSN.
8038 It has also checked that we are optimizing, and thus some inaccurancies
8039 in the debugging information are acceptable.
8040 So we could just delete output_reload_insn.
8041 But in some cases we can improve the debugging information without
8042 sacrificing optimization - maybe even improving the code:
8043 See if the pseudo reg has been completely replaced
8044 with reload regs. If so, delete the store insn
8045 and forget we had a stack slot for the pseudo. */
8046 if (rld[j].out != rld[j].in
8047 && REG_N_DEATHS (REGNO (reg)) == 1
8048 && REG_N_SETS (REGNO (reg)) == 1
8049 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
8050 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
8052 rtx i2;
8054 /* We know that it was used only between here
8055 and the beginning of the current basic block.
8056 (We also know that the last use before INSN was
8057 the output reload we are thinking of deleting, but never mind that.)
8058 Search that range; see if any ref remains. */
8059 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8061 rtx set = single_set (i2);
8063 /* Uses which just store in the pseudo don't count,
8064 since if they are the only uses, they are dead. */
8065 if (set != 0 && SET_DEST (set) == reg)
8066 continue;
8067 if (GET_CODE (i2) == CODE_LABEL
8068 || GET_CODE (i2) == JUMP_INSN)
8069 break;
8070 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
8071 && reg_mentioned_p (reg, PATTERN (i2)))
8073 /* Some other ref remains; just delete the output reload we
8074 know to be dead. */
8075 delete_address_reloads (output_reload_insn, insn);
8076 PUT_CODE (output_reload_insn, NOTE);
8077 NOTE_SOURCE_FILE (output_reload_insn) = 0;
8078 NOTE_LINE_NUMBER (output_reload_insn) = NOTE_INSN_DELETED;
8079 return;
8083 /* Delete the now-dead stores into this pseudo. */
8084 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8086 rtx set = single_set (i2);
8088 if (set != 0 && SET_DEST (set) == reg)
8090 delete_address_reloads (i2, insn);
8091 /* This might be a basic block head,
8092 thus don't use delete_insn. */
8093 PUT_CODE (i2, NOTE);
8094 NOTE_SOURCE_FILE (i2) = 0;
8095 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
8097 if (GET_CODE (i2) == CODE_LABEL
8098 || GET_CODE (i2) == JUMP_INSN)
8099 break;
8102 /* For the debugging info,
8103 say the pseudo lives in this reload reg. */
8104 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
8105 alter_reg (REGNO (reg), -1);
8107 delete_address_reloads (output_reload_insn, insn);
8108 PUT_CODE (output_reload_insn, NOTE);
8109 NOTE_SOURCE_FILE (output_reload_insn) = 0;
8110 NOTE_LINE_NUMBER (output_reload_insn) = NOTE_INSN_DELETED;
8114 /* We are going to delete DEAD_INSN. Recursively delete loads of
8115 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
8116 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
8117 static void
8118 delete_address_reloads (dead_insn, current_insn)
8119 rtx dead_insn, current_insn;
8121 rtx set = single_set (dead_insn);
8122 rtx set2, dst, prev, next;
8123 if (set)
8125 rtx dst = SET_DEST (set);
8126 if (GET_CODE (dst) == MEM)
8127 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
8129 /* If we deleted the store from a reloaded post_{in,de}c expression,
8130 we can delete the matching adds. */
8131 prev = PREV_INSN (dead_insn);
8132 next = NEXT_INSN (dead_insn);
8133 if (! prev || ! next)
8134 return;
8135 set = single_set (next);
8136 set2 = single_set (prev);
8137 if (! set || ! set2
8138 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
8139 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
8140 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
8141 return;
8142 dst = SET_DEST (set);
8143 if (! rtx_equal_p (dst, SET_DEST (set2))
8144 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
8145 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
8146 || (INTVAL (XEXP (SET_SRC (set), 1))
8147 != - INTVAL (XEXP (SET_SRC (set2), 1))))
8148 return;
8149 delete_insn (prev);
8150 delete_insn (next);
8153 /* Subfunction of delete_address_reloads: process registers found in X. */
8154 static void
8155 delete_address_reloads_1 (dead_insn, x, current_insn)
8156 rtx dead_insn, x, current_insn;
8158 rtx prev, set, dst, i2;
8159 int i, j;
8160 enum rtx_code code = GET_CODE (x);
8162 if (code != REG)
8164 const char *fmt= GET_RTX_FORMAT (code);
8165 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8167 if (fmt[i] == 'e')
8168 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
8169 else if (fmt[i] == 'E')
8171 for (j = XVECLEN (x, i) - 1; j >=0; j--)
8172 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
8173 current_insn);
8176 return;
8179 if (spill_reg_order[REGNO (x)] < 0)
8180 return;
8182 /* Scan backwards for the insn that sets x. This might be a way back due
8183 to inheritance. */
8184 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
8186 code = GET_CODE (prev);
8187 if (code == CODE_LABEL || code == JUMP_INSN)
8188 return;
8189 if (GET_RTX_CLASS (code) != 'i')
8190 continue;
8191 if (reg_set_p (x, PATTERN (prev)))
8192 break;
8193 if (reg_referenced_p (x, PATTERN (prev)))
8194 return;
8196 if (! prev || INSN_UID (prev) < reload_first_uid)
8197 return;
8198 /* Check that PREV only sets the reload register. */
8199 set = single_set (prev);
8200 if (! set)
8201 return;
8202 dst = SET_DEST (set);
8203 if (GET_CODE (dst) != REG
8204 || ! rtx_equal_p (dst, x))
8205 return;
8206 if (! reg_set_p (dst, PATTERN (dead_insn)))
8208 /* Check if DST was used in a later insn -
8209 it might have been inherited. */
8210 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
8212 if (GET_CODE (i2) == CODE_LABEL)
8213 break;
8214 if (GET_RTX_CLASS (GET_CODE (i2)) != 'i')
8215 continue;
8216 if (reg_referenced_p (dst, PATTERN (i2)))
8218 /* If there is a reference to the register in the current insn,
8219 it might be loaded in a non-inherited reload. If no other
8220 reload uses it, that means the register is set before
8221 referenced. */
8222 if (i2 == current_insn)
8224 for (j = n_reloads - 1; j >= 0; j--)
8225 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8226 || reload_override_in[j] == dst)
8227 return;
8228 for (j = n_reloads - 1; j >= 0; j--)
8229 if (rld[j].in && rld[j].reg_rtx == dst)
8230 break;
8231 if (j >= 0)
8232 break;
8234 return;
8236 if (GET_CODE (i2) == JUMP_INSN)
8237 break;
8238 /* If DST is still live at CURRENT_INSN, check if it is used for
8239 any reload. Note that even if CURRENT_INSN sets DST, we still
8240 have to check the reloads. */
8241 if (i2 == current_insn)
8243 for (j = n_reloads - 1; j >= 0; j--)
8244 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8245 || reload_override_in[j] == dst)
8246 return;
8247 /* ??? We can't finish the loop here, because dst might be
8248 allocated to a pseudo in this block if no reload in this
8249 block needs any of the clsses containing DST - see
8250 spill_hard_reg. There is no easy way to tell this, so we
8251 have to scan till the end of the basic block. */
8253 if (reg_set_p (dst, PATTERN (i2)))
8254 break;
8257 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
8258 reg_reloaded_contents[REGNO (dst)] = -1;
8259 /* Can't use delete_insn here because PREV might be a basic block head. */
8260 PUT_CODE (prev, NOTE);
8261 NOTE_LINE_NUMBER (prev) = NOTE_INSN_DELETED;
8262 NOTE_SOURCE_FILE (prev) = 0;
8265 /* Output reload-insns to reload VALUE into RELOADREG.
8266 VALUE is an autoincrement or autodecrement RTX whose operand
8267 is a register or memory location;
8268 so reloading involves incrementing that location.
8269 IN is either identical to VALUE, or some cheaper place to reload from.
8271 INC_AMOUNT is the number to increment or decrement by (always positive).
8272 This cannot be deduced from VALUE.
8274 Return the instruction that stores into RELOADREG. */
8276 static rtx
8277 inc_for_reload (reloadreg, in, value, inc_amount)
8278 rtx reloadreg;
8279 rtx in, value;
8280 int inc_amount;
8282 /* REG or MEM to be copied and incremented. */
8283 rtx incloc = XEXP (value, 0);
8284 /* Nonzero if increment after copying. */
8285 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
8286 rtx last;
8287 rtx inc;
8288 rtx add_insn;
8289 int code;
8290 rtx store;
8291 rtx real_in = in == value ? XEXP (in, 0) : in;
8293 /* No hard register is equivalent to this register after
8294 inc/dec operation. If REG_LAST_RELOAD_REG were non-zero,
8295 we could inc/dec that register as well (maybe even using it for
8296 the source), but I'm not sure it's worth worrying about. */
8297 if (GET_CODE (incloc) == REG)
8298 reg_last_reload_reg[REGNO (incloc)] = 0;
8300 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
8301 inc_amount = - inc_amount;
8303 inc = GEN_INT (inc_amount);
8305 /* If this is post-increment, first copy the location to the reload reg. */
8306 if (post && real_in != reloadreg)
8307 emit_insn (gen_move_insn (reloadreg, real_in));
8309 if (in == value)
8311 /* See if we can directly increment INCLOC. Use a method similar to
8312 that in gen_reload. */
8314 last = get_last_insn ();
8315 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
8316 gen_rtx_PLUS (GET_MODE (incloc),
8317 incloc, inc)));
8319 code = recog_memoized (add_insn);
8320 if (code >= 0)
8322 extract_insn (add_insn);
8323 if (constrain_operands (1))
8325 /* If this is a pre-increment and we have incremented the value
8326 where it lives, copy the incremented value to RELOADREG to
8327 be used as an address. */
8329 if (! post)
8330 emit_insn (gen_move_insn (reloadreg, incloc));
8332 return add_insn;
8335 delete_insns_since (last);
8338 /* If couldn't do the increment directly, must increment in RELOADREG.
8339 The way we do this depends on whether this is pre- or post-increment.
8340 For pre-increment, copy INCLOC to the reload register, increment it
8341 there, then save back. */
8343 if (! post)
8345 if (in != reloadreg)
8346 emit_insn (gen_move_insn (reloadreg, real_in));
8347 emit_insn (gen_add2_insn (reloadreg, inc));
8348 store = emit_insn (gen_move_insn (incloc, reloadreg));
8350 else
8352 /* Postincrement.
8353 Because this might be a jump insn or a compare, and because RELOADREG
8354 may not be available after the insn in an input reload, we must do
8355 the incrementation before the insn being reloaded for.
8357 We have already copied IN to RELOADREG. Increment the copy in
8358 RELOADREG, save that back, then decrement RELOADREG so it has
8359 the original value. */
8361 emit_insn (gen_add2_insn (reloadreg, inc));
8362 store = emit_insn (gen_move_insn (incloc, reloadreg));
8363 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
8366 return store;
8369 /* Return 1 if we are certain that the constraint-string STRING allows
8370 the hard register REG. Return 0 if we can't be sure of this. */
8372 static int
8373 constraint_accepts_reg_p (string, reg)
8374 const char *string;
8375 rtx reg;
8377 int value = 0;
8378 int regno = true_regnum (reg);
8379 int c;
8381 /* Initialize for first alternative. */
8382 value = 0;
8383 /* Check that each alternative contains `g' or `r'. */
8384 while (1)
8385 switch (c = *string++)
8387 case 0:
8388 /* If an alternative lacks `g' or `r', we lose. */
8389 return value;
8390 case ',':
8391 /* If an alternative lacks `g' or `r', we lose. */
8392 if (value == 0)
8393 return 0;
8394 /* Initialize for next alternative. */
8395 value = 0;
8396 break;
8397 case 'g':
8398 case 'r':
8399 /* Any general reg wins for this alternative. */
8400 if (TEST_HARD_REG_BIT (reg_class_contents[(int) GENERAL_REGS], regno))
8401 value = 1;
8402 break;
8403 default:
8404 /* Any reg in specified class wins for this alternative. */
8406 enum reg_class class = REG_CLASS_FROM_LETTER (c);
8408 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno))
8409 value = 1;
8414 /* Return the number of places FIND appears within X, but don't count
8415 an occurrence if some SET_DEST is FIND. */
8418 count_occurrences (x, find)
8419 register rtx x, find;
8421 register int i, j;
8422 register enum rtx_code code;
8423 register const char *format_ptr;
8424 int count;
8426 if (x == find)
8427 return 1;
8428 if (x == 0)
8429 return 0;
8431 code = GET_CODE (x);
8433 switch (code)
8435 case REG:
8436 case QUEUED:
8437 case CONST_INT:
8438 case CONST_DOUBLE:
8439 case SYMBOL_REF:
8440 case CODE_LABEL:
8441 case PC:
8442 case CC0:
8443 return 0;
8445 case MEM:
8446 if (GET_CODE (find) == MEM && rtx_equal_p (x, find))
8447 return 1;
8448 break;
8449 case SET:
8450 if (SET_DEST (x) == find)
8451 return count_occurrences (SET_SRC (x), find);
8452 break;
8454 default:
8455 break;
8458 format_ptr = GET_RTX_FORMAT (code);
8459 count = 0;
8461 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8463 switch (*format_ptr++)
8465 case 'e':
8466 count += count_occurrences (XEXP (x, i), find);
8467 break;
8469 case 'E':
8470 if (XVEC (x, i) != NULL)
8472 for (j = 0; j < XVECLEN (x, i); j++)
8473 count += count_occurrences (XVECEXP (x, i, j), find);
8475 break;
8478 return count;
8481 /* This array holds values which are equivalent to a hard register
8482 during reload_cse_regs. Each array element is an EXPR_LIST of
8483 values. Each time a hard register is set, we set the corresponding
8484 array element to the value. Each time a hard register is copied
8485 into memory, we add the memory location to the corresponding array
8486 element. We don't store values or memory addresses with side
8487 effects in this array.
8489 If the value is a CONST_INT, then the mode of the containing
8490 EXPR_LIST is the mode in which that CONST_INT was referenced.
8492 We sometimes clobber a specific entry in a list. In that case, we
8493 just set XEXP (list-entry, 0) to 0. */
8495 static rtx *reg_values;
8497 /* This is a preallocated REG rtx which we use as a temporary in
8498 reload_cse_invalidate_regno, so that we don't need to allocate a
8499 new one each time through a loop in that function. */
8501 static rtx invalidate_regno_rtx;
8503 /* Invalidate any entries in reg_values which depend on REGNO,
8504 including those for REGNO itself. This is called if REGNO is
8505 changing. If CLOBBER is true, then always forget anything we
8506 currently know about REGNO. MODE is the mode of the assignment to
8507 REGNO, which is used to determine how many hard registers are being
8508 changed. If MODE is VOIDmode, then only REGNO is being changed;
8509 this is used when invalidating call clobbered registers across a
8510 call. */
8512 static void
8513 reload_cse_invalidate_regno (regno, mode, clobber)
8514 int regno;
8515 enum machine_mode mode;
8516 int clobber;
8518 int endregno;
8519 register int i;
8521 /* Our callers don't always go through true_regnum; we may see a
8522 pseudo-register here from a CLOBBER or the like. We probably
8523 won't ever see a pseudo-register that has a real register number,
8524 for we check anyhow for safety. */
8525 if (regno >= FIRST_PSEUDO_REGISTER)
8526 regno = reg_renumber[regno];
8527 if (regno < 0)
8528 return;
8530 if (mode == VOIDmode)
8531 endregno = regno + 1;
8532 else
8533 endregno = regno + HARD_REGNO_NREGS (regno, mode);
8535 if (clobber)
8536 for (i = regno; i < endregno; i++)
8537 reg_values[i] = 0;
8539 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8541 rtx x;
8543 for (x = reg_values[i]; x; x = XEXP (x, 1))
8545 if (XEXP (x, 0) != 0
8546 && refers_to_regno_p (regno, endregno, XEXP (x, 0), NULL_PTR))
8548 /* If this is the only entry on the list, clear
8549 reg_values[i]. Otherwise, just clear this entry on
8550 the list. */
8551 if (XEXP (x, 1) == 0 && x == reg_values[i])
8553 reg_values[i] = 0;
8554 break;
8556 XEXP (x, 0) = 0;
8561 /* We must look at earlier registers, in case REGNO is part of a
8562 multi word value but is not the first register. If an earlier
8563 register has a value in a mode which overlaps REGNO, then we must
8564 invalidate that earlier register. Note that we do not need to
8565 check REGNO or later registers (we must not check REGNO itself,
8566 because we would incorrectly conclude that there was a conflict). */
8568 for (i = 0; i < regno; i++)
8570 rtx x;
8572 for (x = reg_values[i]; x; x = XEXP (x, 1))
8574 if (XEXP (x, 0) != 0)
8576 PUT_MODE (invalidate_regno_rtx, GET_MODE (x));
8577 REGNO (invalidate_regno_rtx) = i;
8578 if (refers_to_regno_p (regno, endregno, invalidate_regno_rtx,
8579 NULL_PTR))
8581 reload_cse_invalidate_regno (i, VOIDmode, 1);
8582 break;
8589 /* The memory at address MEM_BASE is being changed.
8590 Return whether this change will invalidate VAL. */
8592 static int
8593 reload_cse_mem_conflict_p (mem_base, val)
8594 rtx mem_base;
8595 rtx val;
8597 enum rtx_code code;
8598 const char *fmt;
8599 int i;
8601 code = GET_CODE (val);
8602 switch (code)
8604 /* Get rid of a few simple cases quickly. */
8605 case REG:
8606 case PC:
8607 case CC0:
8608 case SCRATCH:
8609 case CONST:
8610 case CONST_INT:
8611 case CONST_DOUBLE:
8612 case SYMBOL_REF:
8613 case LABEL_REF:
8614 return 0;
8616 case MEM:
8617 if (GET_MODE (mem_base) == BLKmode
8618 || GET_MODE (val) == BLKmode)
8619 return 1;
8620 if (anti_dependence (val, mem_base))
8621 return 1;
8622 /* The address may contain nested MEMs. */
8623 break;
8625 default:
8626 break;
8629 fmt = GET_RTX_FORMAT (code);
8631 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8633 if (fmt[i] == 'e')
8635 if (reload_cse_mem_conflict_p (mem_base, XEXP (val, i)))
8636 return 1;
8638 else if (fmt[i] == 'E')
8640 int j;
8642 for (j = 0; j < XVECLEN (val, i); j++)
8643 if (reload_cse_mem_conflict_p (mem_base, XVECEXP (val, i, j)))
8644 return 1;
8648 return 0;
8651 /* Invalidate any entries in reg_values which are changed because of a
8652 store to MEM_RTX. If this is called because of a non-const call
8653 instruction, MEM_RTX is (mem:BLK const0_rtx). */
8655 static void
8656 reload_cse_invalidate_mem (mem_rtx)
8657 rtx mem_rtx;
8659 register int i;
8661 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8663 rtx x;
8665 for (x = reg_values[i]; x; x = XEXP (x, 1))
8667 if (XEXP (x, 0) != 0
8668 && reload_cse_mem_conflict_p (mem_rtx, XEXP (x, 0)))
8670 /* If this is the only entry on the list, clear
8671 reg_values[i]. Otherwise, just clear this entry on
8672 the list. */
8673 if (XEXP (x, 1) == 0 && x == reg_values[i])
8675 reg_values[i] = 0;
8676 break;
8678 XEXP (x, 0) = 0;
8684 /* Invalidate DEST, which is being assigned to or clobbered. The
8685 second parameter exists so that this function can be passed to
8686 note_stores; it is ignored. */
8688 static void
8689 reload_cse_invalidate_rtx (dest, ignore, data)
8690 rtx dest;
8691 rtx ignore ATTRIBUTE_UNUSED;
8692 void *data ATTRIBUTE_UNUSED;
8694 while (GET_CODE (dest) == STRICT_LOW_PART
8695 || GET_CODE (dest) == SIGN_EXTRACT
8696 || GET_CODE (dest) == ZERO_EXTRACT
8697 || GET_CODE (dest) == SUBREG)
8698 dest = XEXP (dest, 0);
8700 if (GET_CODE (dest) == REG)
8701 reload_cse_invalidate_regno (REGNO (dest), GET_MODE (dest), 1);
8702 else if (GET_CODE (dest) == MEM)
8703 reload_cse_invalidate_mem (dest);
8706 /* Do a very simple CSE pass over the hard registers.
8708 This function detects no-op moves where we happened to assign two
8709 different pseudo-registers to the same hard register, and then
8710 copied one to the other. Reload will generate a useless
8711 instruction copying a register to itself.
8713 This function also detects cases where we load a value from memory
8714 into two different registers, and (if memory is more expensive than
8715 registers) changes it to simply copy the first register into the
8716 second register.
8718 Another optimization is performed that scans the operands of each
8719 instruction to see whether the value is already available in a
8720 hard register. It then replaces the operand with the hard register
8721 if possible, much like an optional reload would. */
8723 static void
8724 reload_cse_regs_1 (first)
8725 rtx first;
8727 char *firstobj;
8728 rtx callmem;
8729 register int i;
8730 rtx insn;
8732 init_alias_analysis ();
8734 reg_values = (rtx *) alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
8735 bzero ((char *)reg_values, FIRST_PSEUDO_REGISTER * sizeof (rtx));
8737 /* Create our EXPR_LIST structures on reload_obstack, so that we can
8738 free them when we are done. */
8739 push_obstacks (&reload_obstack, &reload_obstack);
8740 firstobj = (char *) obstack_alloc (&reload_obstack, 0);
8742 /* We pass this to reload_cse_invalidate_mem to invalidate all of
8743 memory for a non-const call instruction. */
8744 callmem = gen_rtx_MEM (BLKmode, const0_rtx);
8746 /* This is used in reload_cse_invalidate_regno to avoid consing a
8747 new REG in a loop in that function. */
8748 invalidate_regno_rtx = gen_rtx_REG (VOIDmode, 0);
8750 for (insn = first; insn; insn = NEXT_INSN (insn))
8752 rtx body;
8754 if (GET_CODE (insn) == CODE_LABEL)
8756 /* Forget all the register values at a code label. We don't
8757 try to do anything clever around jumps. */
8758 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8759 reg_values[i] = 0;
8761 continue;
8764 #ifdef NON_SAVING_SETJMP
8765 if (NON_SAVING_SETJMP && GET_CODE (insn) == NOTE
8766 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
8768 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8769 reg_values[i] = 0;
8771 continue;
8773 #endif
8775 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
8776 continue;
8778 /* If this is a call instruction, forget anything stored in a
8779 call clobbered register, or, if this is not a const call, in
8780 memory. */
8781 if (GET_CODE (insn) == CALL_INSN)
8783 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8784 if (call_used_regs[i])
8785 reload_cse_invalidate_regno (i, VOIDmode, 1);
8787 if (! CONST_CALL_P (insn))
8788 reload_cse_invalidate_mem (callmem);
8792 /* Forget all the register values at a volatile asm. */
8793 if (GET_CODE (insn) == INSN
8794 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
8795 && MEM_VOLATILE_P (PATTERN (insn)))
8796 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8797 reg_values[i] = 0;
8799 body = PATTERN (insn);
8800 if (GET_CODE (body) == SET)
8802 int count = 0;
8803 if (reload_cse_noop_set_p (body, insn))
8805 /* If this sets the return value of the function, we must keep
8806 a USE around, in case this is in a different basic block
8807 than the final USE. Otherwise, we could loose important
8808 register lifeness information on SMALL_REGISTER_CLASSES
8809 machines, where return registers might be used as spills:
8810 subsequent passes assume that spill registers are dead at
8811 the end of a basic block. */
8812 if (REG_FUNCTION_VALUE_P (SET_DEST (body)))
8814 pop_obstacks ();
8815 PATTERN (insn) = gen_rtx_USE (VOIDmode, SET_DEST (body));
8816 INSN_CODE (insn) = -1;
8817 REG_NOTES (insn) = NULL_RTX;
8818 push_obstacks (&reload_obstack, &reload_obstack);
8820 else
8822 PUT_CODE (insn, NOTE);
8823 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8824 NOTE_SOURCE_FILE (insn) = 0;
8827 /* We're done with this insn. */
8828 continue;
8831 /* It's not a no-op, but we can try to simplify it. */
8832 count += reload_cse_simplify_set (body, insn);
8834 if (count > 0)
8835 apply_change_group ();
8836 else
8837 reload_cse_simplify_operands (insn);
8839 reload_cse_record_set (body, body);
8841 else if (GET_CODE (body) == PARALLEL)
8843 int count = 0;
8844 rtx value = NULL_RTX;
8846 /* If every action in a PARALLEL is a noop, we can delete
8847 the entire PARALLEL. */
8848 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8850 rtx part = XVECEXP (body, 0, i);
8851 if (GET_CODE (part) == SET)
8853 if (! reload_cse_noop_set_p (part, insn))
8854 break;
8855 if (REG_FUNCTION_VALUE_P (SET_DEST (part)))
8857 if (value)
8858 break;
8859 value = SET_DEST (part);
8862 else if (GET_CODE (part) != CLOBBER)
8863 break;
8865 if (i < 0)
8867 if (value)
8869 pop_obstacks ();
8870 PATTERN (insn) = gen_rtx_USE (VOIDmode, value);
8871 INSN_CODE (insn) = -1;
8872 REG_NOTES (insn) = NULL_RTX;
8873 push_obstacks (&reload_obstack, &reload_obstack);
8875 else
8877 PUT_CODE (insn, NOTE);
8878 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8879 NOTE_SOURCE_FILE (insn) = 0;
8882 /* We're done with this insn. */
8883 continue;
8886 /* It's not a no-op, but we can try to simplify it. */
8887 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8888 if (GET_CODE (XVECEXP (body, 0, i)) == SET)
8889 count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
8891 if (count > 0)
8892 apply_change_group ();
8893 else
8894 reload_cse_simplify_operands (insn);
8896 /* Look through the PARALLEL and record the values being
8897 set, if possible. Also handle any CLOBBERs. */
8898 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8900 rtx x = XVECEXP (body, 0, i);
8902 if (GET_CODE (x) == SET)
8903 reload_cse_record_set (x, body);
8904 else
8905 note_stores (x, reload_cse_invalidate_rtx, NULL);
8908 else
8909 note_stores (body, reload_cse_invalidate_rtx, NULL);
8911 #ifdef AUTO_INC_DEC
8912 /* Clobber any registers which appear in REG_INC notes. We
8913 could keep track of the changes to their values, but it is
8914 unlikely to help. */
8916 rtx x;
8918 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
8919 if (REG_NOTE_KIND (x) == REG_INC)
8920 reload_cse_invalidate_rtx (XEXP (x, 0), NULL_RTX, NULL);
8922 #endif
8924 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
8925 after we have processed the insn. */
8926 if (GET_CODE (insn) == CALL_INSN)
8928 rtx x;
8930 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
8931 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
8932 reload_cse_invalidate_rtx (XEXP (XEXP (x, 0), 0), NULL_RTX,
8933 NULL);
8937 /* Clean up. */
8938 end_alias_analysis ();
8940 /* Free all the temporary structures we created, and go back to the
8941 regular obstacks. */
8942 obstack_free (&reload_obstack, firstobj);
8943 pop_obstacks ();
8946 /* Call cse / combine like post-reload optimization phases.
8947 FIRST is the first instruction. */
8948 void
8949 reload_cse_regs (first)
8950 rtx first;
8952 reload_cse_regs_1 (first);
8953 reload_combine ();
8954 reload_cse_move2add (first);
8955 if (flag_expensive_optimizations)
8956 reload_cse_regs_1 (first);
8959 /* Return whether the values known for REGNO are equal to VAL. MODE
8960 is the mode of the object that VAL is being copied to; this matters
8961 if VAL is a CONST_INT. */
8963 static int
8964 reload_cse_regno_equal_p (regno, val, mode)
8965 int regno;
8966 rtx val;
8967 enum machine_mode mode;
8969 rtx x;
8971 if (val == 0)
8972 return 0;
8974 for (x = reg_values[regno]; x; x = XEXP (x, 1))
8975 if (XEXP (x, 0) != 0
8976 && rtx_equal_p (XEXP (x, 0), val)
8977 && (! flag_float_store || GET_CODE (XEXP (x, 0)) != MEM
8978 || GET_MODE_CLASS (GET_MODE (x)) != MODE_FLOAT)
8979 && (GET_CODE (val) != CONST_INT
8980 || mode == GET_MODE (x)
8981 || (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8982 /* On a big endian machine if the value spans more than
8983 one register then this register holds the high part of
8984 it and we can't use it.
8986 ??? We should also compare with the high part of the
8987 value. */
8988 && !(WORDS_BIG_ENDIAN
8989 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
8990 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
8991 GET_MODE_BITSIZE (GET_MODE (x))))))
8992 return 1;
8994 return 0;
8997 /* See whether a single set is a noop. SET is the set instruction we
8998 are should check, and INSN is the instruction from which it came. */
9000 static int
9001 reload_cse_noop_set_p (set, insn)
9002 rtx set;
9003 rtx insn ATTRIBUTE_UNUSED;
9005 rtx src, dest;
9006 enum machine_mode dest_mode;
9007 int dreg, sreg;
9008 int ret;
9010 src = SET_SRC (set);
9011 dest = SET_DEST (set);
9012 dest_mode = GET_MODE (dest);
9014 if (side_effects_p (src))
9015 return 0;
9017 dreg = true_regnum (dest);
9018 sreg = true_regnum (src);
9020 /* Check for setting a register to itself. In this case, we don't
9021 have to worry about REG_DEAD notes. */
9022 if (dreg >= 0 && dreg == sreg)
9023 return 1;
9025 ret = 0;
9026 if (dreg >= 0)
9028 /* Check for setting a register to itself. */
9029 if (dreg == sreg)
9030 ret = 1;
9032 /* Check for setting a register to a value which we already know
9033 is in the register. */
9034 else if (reload_cse_regno_equal_p (dreg, src, dest_mode))
9035 ret = 1;
9037 /* Check for setting a register DREG to another register SREG
9038 where SREG is equal to a value which is already in DREG. */
9039 else if (sreg >= 0)
9041 rtx x;
9043 for (x = reg_values[sreg]; x; x = XEXP (x, 1))
9045 rtx tmp;
9047 if (XEXP (x, 0) == 0)
9048 continue;
9050 if (dest_mode == GET_MODE (x))
9051 tmp = XEXP (x, 0);
9052 else if (GET_MODE_BITSIZE (dest_mode)
9053 < GET_MODE_BITSIZE (GET_MODE (x)))
9054 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
9055 else
9056 continue;
9058 if (tmp
9059 && reload_cse_regno_equal_p (dreg, tmp, dest_mode))
9061 ret = 1;
9062 break;
9067 else if (GET_CODE (dest) == MEM)
9069 /* Check for storing a register to memory when we know that the
9070 register is equivalent to the memory location. */
9071 if (sreg >= 0
9072 && reload_cse_regno_equal_p (sreg, dest, dest_mode)
9073 && ! side_effects_p (dest))
9074 ret = 1;
9077 return ret;
9080 /* Try to simplify a single SET instruction. SET is the set pattern.
9081 INSN is the instruction it came from.
9082 This function only handles one case: if we set a register to a value
9083 which is not a register, we try to find that value in some other register
9084 and change the set into a register copy. */
9086 static int
9087 reload_cse_simplify_set (set, insn)
9088 rtx set;
9089 rtx insn;
9091 int dreg;
9092 rtx src;
9093 enum machine_mode dest_mode;
9094 enum reg_class dclass;
9095 register int i;
9097 dreg = true_regnum (SET_DEST (set));
9098 if (dreg < 0)
9099 return 0;
9101 src = SET_SRC (set);
9102 if (side_effects_p (src) || true_regnum (src) >= 0)
9103 return 0;
9105 dclass = REGNO_REG_CLASS (dreg);
9107 /* If memory loads are cheaper than register copies, don't change them. */
9108 if (GET_CODE (src) == MEM
9109 && MEMORY_MOVE_COST (GET_MODE (src), dclass, 1) < 2)
9110 return 0;
9112 /* If the constant is cheaper than a register, don't change it. */
9113 if (CONSTANT_P (src)
9114 && rtx_cost (src, SET) < 2)
9115 return 0;
9117 dest_mode = GET_MODE (SET_DEST (set));
9118 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
9120 if (i != dreg
9121 && REGISTER_MOVE_COST (REGNO_REG_CLASS (i), dclass) == 2
9122 && reload_cse_regno_equal_p (i, src, dest_mode))
9124 int validated;
9126 /* Pop back to the real obstacks while changing the insn. */
9127 pop_obstacks ();
9129 validated = validate_change (insn, &SET_SRC (set),
9130 gen_rtx_REG (dest_mode, i), 1);
9132 /* Go back to the obstack we are using for temporary
9133 storage. */
9134 push_obstacks (&reload_obstack, &reload_obstack);
9136 if (validated)
9137 return 1;
9140 return 0;
9143 /* Try to replace operands in INSN with equivalent values that are already
9144 in registers. This can be viewed as optional reloading.
9146 For each non-register operand in the insn, see if any hard regs are
9147 known to be equivalent to that operand. Record the alternatives which
9148 can accept these hard registers. Among all alternatives, select the
9149 ones which are better or equal to the one currently matching, where
9150 "better" is in terms of '?' and '!' constraints. Among the remaining
9151 alternatives, select the one which replaces most operands with
9152 hard registers. */
9154 static int
9155 reload_cse_simplify_operands (insn)
9156 rtx insn;
9158 int i,j;
9160 const char *constraints[MAX_RECOG_OPERANDS];
9162 /* Vector recording how bad an alternative is. */
9163 int *alternative_reject;
9164 /* Vector recording how many registers can be introduced by choosing
9165 this alternative. */
9166 int *alternative_nregs;
9167 /* Array of vectors recording, for each operand and each alternative,
9168 which hard register to substitute, or -1 if the operand should be
9169 left as it is. */
9170 int *op_alt_regno[MAX_RECOG_OPERANDS];
9171 /* Array of alternatives, sorted in order of decreasing desirability. */
9172 int *alternative_order;
9173 rtx reg = gen_rtx_REG (VOIDmode, -1);
9175 extract_insn (insn);
9177 if (recog_data.n_alternatives == 0 || recog_data.n_operands == 0)
9178 return 0;
9180 /* Figure out which alternative currently matches. */
9181 if (! constrain_operands (1))
9182 fatal_insn_not_found (insn);
9184 alternative_reject = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9185 alternative_nregs = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9186 alternative_order = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9187 bzero ((char *)alternative_reject, recog_data.n_alternatives * sizeof (int));
9188 bzero ((char *)alternative_nregs, recog_data.n_alternatives * sizeof (int));
9190 for (i = 0; i < recog_data.n_operands; i++)
9192 enum machine_mode mode;
9193 int regno;
9194 const char *p;
9196 op_alt_regno[i] = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9197 for (j = 0; j < recog_data.n_alternatives; j++)
9198 op_alt_regno[i][j] = -1;
9200 p = constraints[i] = recog_data.constraints[i];
9201 mode = recog_data.operand_mode[i];
9203 /* Add the reject values for each alternative given by the constraints
9204 for this operand. */
9205 j = 0;
9206 while (*p != '\0')
9208 char c = *p++;
9209 if (c == ',')
9210 j++;
9211 else if (c == '?')
9212 alternative_reject[j] += 3;
9213 else if (c == '!')
9214 alternative_reject[j] += 300;
9217 /* We won't change operands which are already registers. We
9218 also don't want to modify output operands. */
9219 regno = true_regnum (recog_data.operand[i]);
9220 if (regno >= 0
9221 || constraints[i][0] == '='
9222 || constraints[i][0] == '+')
9223 continue;
9225 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9227 int class = (int) NO_REGS;
9229 if (! reload_cse_regno_equal_p (regno, recog_data.operand[i], mode))
9230 continue;
9232 REGNO (reg) = regno;
9233 PUT_MODE (reg, mode);
9235 /* We found a register equal to this operand. Now look for all
9236 alternatives that can accept this register and have not been
9237 assigned a register they can use yet. */
9238 j = 0;
9239 p = constraints[i];
9240 for (;;)
9242 char c = *p++;
9244 switch (c)
9246 case '=': case '+': case '?':
9247 case '#': case '&': case '!':
9248 case '*': case '%':
9249 case '0': case '1': case '2': case '3': case '4':
9250 case '5': case '6': case '7': case '8': case '9':
9251 case 'm': case '<': case '>': case 'V': case 'o':
9252 case 'E': case 'F': case 'G': case 'H':
9253 case 's': case 'i': case 'n':
9254 case 'I': case 'J': case 'K': case 'L':
9255 case 'M': case 'N': case 'O': case 'P':
9256 #ifdef EXTRA_CONSTRAINT
9257 case 'Q': case 'R': case 'S': case 'T': case 'U':
9258 #endif
9259 case 'p': case 'X':
9260 /* These don't say anything we care about. */
9261 break;
9263 case 'g': case 'r':
9264 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
9265 break;
9267 default:
9268 class
9269 = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER ((unsigned char)c)];
9270 break;
9272 case ',': case '\0':
9273 /* See if REGNO fits this alternative, and set it up as the
9274 replacement register if we don't have one for this
9275 alternative yet and the operand being replaced is not
9276 a cheap CONST_INT. */
9277 if (op_alt_regno[i][j] == -1
9278 && reg_fits_class_p (reg, class, 0, mode)
9279 && (GET_CODE (recog_data.operand[i]) != CONST_INT
9280 || (rtx_cost (recog_data.operand[i], SET)
9281 > rtx_cost (reg, SET))))
9283 alternative_nregs[j]++;
9284 op_alt_regno[i][j] = regno;
9286 j++;
9287 break;
9290 if (c == '\0')
9291 break;
9296 /* Record all alternatives which are better or equal to the currently
9297 matching one in the alternative_order array. */
9298 for (i = j = 0; i < recog_data.n_alternatives; i++)
9299 if (alternative_reject[i] <= alternative_reject[which_alternative])
9300 alternative_order[j++] = i;
9301 recog_data.n_alternatives = j;
9303 /* Sort it. Given a small number of alternatives, a dumb algorithm
9304 won't hurt too much. */
9305 for (i = 0; i < recog_data.n_alternatives - 1; i++)
9307 int best = i;
9308 int best_reject = alternative_reject[alternative_order[i]];
9309 int best_nregs = alternative_nregs[alternative_order[i]];
9310 int tmp;
9312 for (j = i + 1; j < recog_data.n_alternatives; j++)
9314 int this_reject = alternative_reject[alternative_order[j]];
9315 int this_nregs = alternative_nregs[alternative_order[j]];
9317 if (this_reject < best_reject
9318 || (this_reject == best_reject && this_nregs < best_nregs))
9320 best = j;
9321 best_reject = this_reject;
9322 best_nregs = this_nregs;
9326 tmp = alternative_order[best];
9327 alternative_order[best] = alternative_order[i];
9328 alternative_order[i] = tmp;
9331 /* Substitute the operands as determined by op_alt_regno for the best
9332 alternative. */
9333 j = alternative_order[0];
9335 /* Pop back to the real obstacks while changing the insn. */
9336 pop_obstacks ();
9338 for (i = 0; i < recog_data.n_operands; i++)
9340 enum machine_mode mode = recog_data.operand_mode[i];
9341 if (op_alt_regno[i][j] == -1)
9342 continue;
9344 validate_change (insn, recog_data.operand_loc[i],
9345 gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
9348 for (i = recog_data.n_dups - 1; i >= 0; i--)
9350 int op = recog_data.dup_num[i];
9351 enum machine_mode mode = recog_data.operand_mode[op];
9353 if (op_alt_regno[op][j] == -1)
9354 continue;
9356 validate_change (insn, recog_data.dup_loc[i],
9357 gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
9360 /* Go back to the obstack we are using for temporary
9361 storage. */
9362 push_obstacks (&reload_obstack, &reload_obstack);
9364 return apply_change_group ();
9367 /* These two variables are used to pass information from
9368 reload_cse_record_set to reload_cse_check_clobber. */
9370 static int reload_cse_check_clobbered;
9371 static rtx reload_cse_check_src;
9373 /* See if DEST overlaps with RELOAD_CSE_CHECK_SRC. If it does, set
9374 RELOAD_CSE_CHECK_CLOBBERED. This is called via note_stores. The
9375 second argument, which is passed by note_stores, is ignored. */
9377 static void
9378 reload_cse_check_clobber (dest, ignore, data)
9379 rtx dest;
9380 rtx ignore ATTRIBUTE_UNUSED;
9381 void *data ATTRIBUTE_UNUSED;
9383 if (reg_overlap_mentioned_p (dest, reload_cse_check_src))
9384 reload_cse_check_clobbered = 1;
9387 /* Record the result of a SET instruction. SET is the set pattern.
9388 BODY is the pattern of the insn that it came from. */
9390 static void
9391 reload_cse_record_set (set, body)
9392 rtx set;
9393 rtx body;
9395 rtx dest, src, x;
9396 int dreg, sreg;
9397 enum machine_mode dest_mode;
9399 dest = SET_DEST (set);
9400 src = SET_SRC (set);
9401 dreg = true_regnum (dest);
9402 sreg = true_regnum (src);
9403 dest_mode = GET_MODE (dest);
9405 /* Some machines don't define AUTO_INC_DEC, but they still use push
9406 instructions. We need to catch that case here in order to
9407 invalidate the stack pointer correctly. Note that invalidating
9408 the stack pointer is different from invalidating DEST. */
9409 x = dest;
9410 while (GET_CODE (x) == SUBREG
9411 || GET_CODE (x) == ZERO_EXTRACT
9412 || GET_CODE (x) == SIGN_EXTRACT
9413 || GET_CODE (x) == STRICT_LOW_PART)
9414 x = XEXP (x, 0);
9415 if (push_operand (x, GET_MODE (x)))
9417 reload_cse_invalidate_rtx (stack_pointer_rtx, NULL_RTX, NULL);
9418 reload_cse_invalidate_rtx (dest, NULL_RTX, NULL);
9419 return;
9422 /* We can only handle an assignment to a register, or a store of a
9423 register to a memory location. For other cases, we just clobber
9424 the destination. We also have to just clobber if there are side
9425 effects in SRC or DEST. */
9426 if ((dreg < 0 && GET_CODE (dest) != MEM)
9427 || side_effects_p (src)
9428 || side_effects_p (dest))
9430 reload_cse_invalidate_rtx (dest, NULL_RTX, NULL);
9431 return;
9434 #ifdef HAVE_cc0
9435 /* We don't try to handle values involving CC, because it's a pain
9436 to keep track of when they have to be invalidated. */
9437 if (reg_mentioned_p (cc0_rtx, src)
9438 || reg_mentioned_p (cc0_rtx, dest))
9440 reload_cse_invalidate_rtx (dest, NULL_RTX, NULL);
9441 return;
9443 #endif
9445 /* If BODY is a PARALLEL, then we need to see whether the source of
9446 SET is clobbered by some other instruction in the PARALLEL. */
9447 if (GET_CODE (body) == PARALLEL)
9449 int i;
9451 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
9453 rtx x;
9455 x = XVECEXP (body, 0, i);
9456 if (x == set)
9457 continue;
9459 reload_cse_check_clobbered = 0;
9460 reload_cse_check_src = src;
9461 note_stores (x, reload_cse_check_clobber, NULL);
9462 if (reload_cse_check_clobbered)
9464 reload_cse_invalidate_rtx (dest, NULL_RTX, NULL);
9465 return;
9470 if (dreg >= 0)
9472 int i;
9474 /* This is an assignment to a register. Update the value we
9475 have stored for the register. */
9476 if (sreg >= 0)
9478 rtx x;
9480 /* This is a copy from one register to another. Any values
9481 which were valid for SREG are now valid for DREG. If the
9482 mode changes, we use gen_lowpart_common to extract only
9483 the part of the value that is copied. */
9484 reg_values[dreg] = 0;
9485 for (x = reg_values[sreg]; x; x = XEXP (x, 1))
9487 rtx tmp;
9489 if (XEXP (x, 0) == 0)
9490 continue;
9491 if (dest_mode == GET_MODE (XEXP (x, 0)))
9492 tmp = XEXP (x, 0);
9493 else if (GET_MODE_BITSIZE (dest_mode)
9494 > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
9495 continue;
9496 else
9497 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
9498 if (tmp)
9499 reg_values[dreg] = gen_rtx_EXPR_LIST (dest_mode, tmp,
9500 reg_values[dreg]);
9503 else
9504 reg_values[dreg] = gen_rtx_EXPR_LIST (dest_mode, src, NULL_RTX);
9506 /* We've changed DREG, so invalidate any values held by other
9507 registers that depend upon it. */
9508 reload_cse_invalidate_regno (dreg, dest_mode, 0);
9510 /* If this assignment changes more than one hard register,
9511 forget anything we know about the others. */
9512 for (i = 1; i < HARD_REGNO_NREGS (dreg, dest_mode); i++)
9513 reg_values[dreg + i] = 0;
9515 else if (GET_CODE (dest) == MEM)
9517 /* Invalidate conflicting memory locations. */
9518 reload_cse_invalidate_mem (dest);
9520 /* If we're storing a register to memory, add DEST to the list
9521 in REG_VALUES. */
9522 if (sreg >= 0 && ! side_effects_p (dest))
9523 reg_values[sreg] = gen_rtx_EXPR_LIST (dest_mode, dest,
9524 reg_values[sreg]);
9526 else
9528 /* We should have bailed out earlier. */
9529 abort ();
9533 /* If reload couldn't use reg+reg+offset addressing, try to use reg+reg
9534 addressing now.
9535 This code might also be useful when reload gave up on reg+reg addresssing
9536 because of clashes between the return register and INDEX_REG_CLASS. */
9538 /* The maximum number of uses of a register we can keep track of to
9539 replace them with reg+reg addressing. */
9540 #define RELOAD_COMBINE_MAX_USES 6
9542 /* INSN is the insn where a register has ben used, and USEP points to the
9543 location of the register within the rtl. */
9544 struct reg_use { rtx insn, *usep; };
9546 /* If the register is used in some unknown fashion, USE_INDEX is negative.
9547 If it is dead, USE_INDEX is RELOAD_COMBINE_MAX_USES, and STORE_RUID
9548 indicates where it becomes live again.
9549 Otherwise, USE_INDEX is the index of the last encountered use of the
9550 register (which is first among these we have seen since we scan backwards),
9551 OFFSET contains the constant offset that is added to the register in
9552 all encountered uses, and USE_RUID indicates the first encountered, i.e.
9553 last, of these uses.
9554 STORE_RUID is always meaningful if we only want to use a value in a
9555 register in a different place: it denotes the next insn in the insn
9556 stream (i.e. the last ecountered) that sets or clobbers the register. */
9557 static struct
9559 struct reg_use reg_use[RELOAD_COMBINE_MAX_USES];
9560 int use_index;
9561 rtx offset;
9562 int store_ruid;
9563 int use_ruid;
9564 } reg_state[FIRST_PSEUDO_REGISTER];
9566 /* Reverse linear uid. This is increased in reload_combine while scanning
9567 the instructions from last to first. It is used to set last_label_ruid
9568 and the store_ruid / use_ruid fields in reg_state. */
9569 static int reload_combine_ruid;
9571 #define LABEL_LIVE(LABEL) \
9572 (label_live[CODE_LABEL_NUMBER (LABEL) - min_labelno])
9574 static void
9575 reload_combine ()
9577 rtx insn, set;
9578 int first_index_reg = 1, last_index_reg = 0;
9579 int i;
9580 int last_label_ruid;
9581 int min_labelno, n_labels;
9582 HARD_REG_SET ever_live_at_start, *label_live;
9584 /* If reg+reg can be used in offsetable memory adresses, the main chunk of
9585 reload has already used it where appropriate, so there is no use in
9586 trying to generate it now. */
9587 if (double_reg_address_ok && INDEX_REG_CLASS != NO_REGS)
9588 return;
9590 /* To avoid wasting too much time later searching for an index register,
9591 determine the minimum and maximum index register numbers. */
9592 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9594 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i))
9596 if (! last_index_reg)
9597 last_index_reg = i;
9598 first_index_reg = i;
9601 /* If no index register is available, we can quit now. */
9602 if (first_index_reg > last_index_reg)
9603 return;
9605 /* Set up LABEL_LIVE and EVER_LIVE_AT_START. The register lifetime
9606 information is a bit fuzzy immediately after reload, but it's
9607 still good enough to determine which registers are live at a jump
9608 destination. */
9609 min_labelno = get_first_label_num ();
9610 n_labels = max_label_num () - min_labelno;
9611 label_live = (HARD_REG_SET *) xmalloc (n_labels * sizeof (HARD_REG_SET));
9612 CLEAR_HARD_REG_SET (ever_live_at_start);
9613 for (i = n_basic_blocks - 1; i >= 0; i--)
9615 insn = BLOCK_HEAD (i);
9616 if (GET_CODE (insn) == CODE_LABEL)
9618 HARD_REG_SET live;
9620 REG_SET_TO_HARD_REG_SET (live, BASIC_BLOCK (i)->global_live_at_start);
9621 compute_use_by_pseudos (&live, BASIC_BLOCK (i)->global_live_at_start);
9622 COPY_HARD_REG_SET (LABEL_LIVE (insn), live);
9623 IOR_HARD_REG_SET (ever_live_at_start, live);
9627 /* Initialize last_label_ruid, reload_combine_ruid and reg_state. */
9628 last_label_ruid = reload_combine_ruid = 0;
9629 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9631 reg_state[i].store_ruid = reload_combine_ruid;
9632 if (fixed_regs[i])
9633 reg_state[i].use_index = -1;
9634 else
9635 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9638 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
9640 rtx note;
9642 /* We cannot do our optimization across labels. Invalidating all the use
9643 information we have would be costly, so we just note where the label
9644 is and then later disable any optimization that would cross it. */
9645 if (GET_CODE (insn) == CODE_LABEL)
9646 last_label_ruid = reload_combine_ruid;
9647 if (GET_CODE (insn) == BARRIER)
9649 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9650 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9652 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
9653 continue;
9654 reload_combine_ruid++;
9656 /* Look for (set (REGX) (CONST_INT))
9657 (set (REGX) (PLUS (REGX) (REGY)))
9659 ... (MEM (REGX)) ...
9660 and convert it to
9661 (set (REGZ) (CONST_INT))
9663 ... (MEM (PLUS (REGZ) (REGY)))... .
9665 First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
9666 and that we know all uses of REGX before it dies. */
9667 set = single_set (insn);
9668 if (set != NULL_RTX
9669 && GET_CODE (SET_DEST (set)) == REG
9670 && (HARD_REGNO_NREGS (REGNO (SET_DEST (set)),
9671 GET_MODE (SET_DEST (set)))
9672 == 1)
9673 && GET_CODE (SET_SRC (set)) == PLUS
9674 && GET_CODE (XEXP (SET_SRC (set), 1)) == REG
9675 && rtx_equal_p (XEXP (SET_SRC (set), 0), SET_DEST (set))
9676 && last_label_ruid < reg_state[REGNO (SET_DEST (set))].use_ruid)
9678 rtx reg = SET_DEST (set);
9679 rtx plus = SET_SRC (set);
9680 rtx base = XEXP (plus, 1);
9681 rtx prev = prev_nonnote_insn (insn);
9682 rtx prev_set = prev ? single_set (prev) : NULL_RTX;
9683 int regno = REGNO (reg);
9684 rtx const_reg = NULL_RTX;
9685 rtx reg_sum = NULL_RTX;
9687 /* Now, we need an index register.
9688 We'll set index_reg to this index register, const_reg to the
9689 register that is to be loaded with the constant
9690 (denoted as REGZ in the substitution illustration above),
9691 and reg_sum to the register-register that we want to use to
9692 substitute uses of REG (typically in MEMs) with.
9693 First check REG and BASE for being index registers;
9694 we can use them even if they are not dead. */
9695 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], regno)
9696 || TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
9697 REGNO (base)))
9699 const_reg = reg;
9700 reg_sum = plus;
9702 else
9704 /* Otherwise, look for a free index register. Since we have
9705 checked above that neiter REG nor BASE are index registers,
9706 if we find anything at all, it will be different from these
9707 two registers. */
9708 for (i = first_index_reg; i <= last_index_reg; i++)
9710 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i)
9711 && reg_state[i].use_index == RELOAD_COMBINE_MAX_USES
9712 && reg_state[i].store_ruid <= reg_state[regno].use_ruid
9713 && HARD_REGNO_NREGS (i, GET_MODE (reg)) == 1)
9715 rtx index_reg = gen_rtx_REG (GET_MODE (reg), i);
9716 const_reg = index_reg;
9717 reg_sum = gen_rtx_PLUS (GET_MODE (reg), index_reg, base);
9718 break;
9722 /* Check that PREV_SET is indeed (set (REGX) (CONST_INT)) and that
9723 (REGY), i.e. BASE, is not clobbered before the last use we'll
9724 create. */
9725 if (prev_set
9726 && GET_CODE (SET_SRC (prev_set)) == CONST_INT
9727 && rtx_equal_p (SET_DEST (prev_set), reg)
9728 && reg_state[regno].use_index >= 0
9729 && reg_state[REGNO (base)].store_ruid <= reg_state[regno].use_ruid
9730 && reg_sum)
9732 int i;
9734 /* Change destination register and - if necessary - the
9735 constant value in PREV, the constant loading instruction. */
9736 validate_change (prev, &SET_DEST (prev_set), const_reg, 1);
9737 if (reg_state[regno].offset != const0_rtx)
9738 validate_change (prev,
9739 &SET_SRC (prev_set),
9740 GEN_INT (INTVAL (SET_SRC (prev_set))
9741 + INTVAL (reg_state[regno].offset)),
9743 /* Now for every use of REG that we have recorded, replace REG
9744 with REG_SUM. */
9745 for (i = reg_state[regno].use_index;
9746 i < RELOAD_COMBINE_MAX_USES; i++)
9747 validate_change (reg_state[regno].reg_use[i].insn,
9748 reg_state[regno].reg_use[i].usep,
9749 reg_sum, 1);
9751 if (apply_change_group ())
9753 rtx *np;
9755 /* Delete the reg-reg addition. */
9756 PUT_CODE (insn, NOTE);
9757 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9758 NOTE_SOURCE_FILE (insn) = 0;
9760 if (reg_state[regno].offset != const0_rtx)
9762 /* Previous REG_EQUIV / REG_EQUAL notes for PREV
9763 are now invalid. */
9764 for (np = &REG_NOTES (prev); *np; )
9766 if (REG_NOTE_KIND (*np) == REG_EQUAL
9767 || REG_NOTE_KIND (*np) == REG_EQUIV)
9768 *np = XEXP (*np, 1);
9769 else
9770 np = &XEXP (*np, 1);
9773 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
9774 reg_state[REGNO (const_reg)].store_ruid = reload_combine_ruid;
9775 continue;
9779 note_stores (PATTERN (insn), reload_combine_note_store, NULL);
9780 if (GET_CODE (insn) == CALL_INSN)
9782 rtx link;
9784 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9786 if (call_used_regs[i])
9788 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9789 reg_state[i].store_ruid = reload_combine_ruid;
9792 for (link = CALL_INSN_FUNCTION_USAGE (insn); link;
9793 link = XEXP (link, 1))
9795 rtx use = XEXP (link, 0);
9796 int regno = REGNO (XEXP (use, 0));
9797 if (GET_CODE (use) == CLOBBER)
9799 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
9800 reg_state[regno].store_ruid = reload_combine_ruid;
9802 else
9803 reg_state[regno].use_index = -1;
9806 if (GET_CODE (insn) == JUMP_INSN && GET_CODE (PATTERN (insn)) != RETURN)
9808 /* Non-spill registers might be used at the call destination in
9809 some unknown fashion, so we have to mark the unknown use. */
9810 HARD_REG_SET *live;
9811 if ((condjump_p (insn) || condjump_in_parallel_p (insn))
9812 && JUMP_LABEL (insn))
9813 live = &LABEL_LIVE (JUMP_LABEL (insn));
9814 else
9815 live = &ever_live_at_start;
9816 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9818 if (TEST_HARD_REG_BIT (*live, i))
9819 reg_state[i].use_index = -1;
9822 reload_combine_note_use (&PATTERN (insn), insn);
9823 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
9825 if (REG_NOTE_KIND (note) == REG_INC
9826 && GET_CODE (XEXP (note, 0)) == REG)
9828 int regno = REGNO (XEXP (note, 0));
9830 reg_state[regno].store_ruid = reload_combine_ruid;
9831 reg_state[regno].use_index = -1;
9835 free (label_live);
9838 /* Check if DST is a register or a subreg of a register; if it is,
9839 update reg_state[regno].store_ruid and reg_state[regno].use_index
9840 accordingly. Called via note_stores from reload_combine. */
9841 static void
9842 reload_combine_note_store (dst, set, data)
9843 rtx dst, set;
9844 void *data ATTRIBUTE_UNUSED;
9846 int regno = 0;
9847 int i;
9848 unsigned size = GET_MODE_SIZE (GET_MODE (dst));
9850 if (GET_CODE (dst) == SUBREG)
9852 regno = SUBREG_WORD (dst);
9853 dst = SUBREG_REG (dst);
9855 if (GET_CODE (dst) != REG)
9856 return;
9857 regno += REGNO (dst);
9859 /* note_stores might have stripped a STRICT_LOW_PART, so we have to be
9860 careful with registers / register parts that are not full words.
9862 Similarly for ZERO_EXTRACT and SIGN_EXTRACT. */
9863 if (GET_CODE (set) != SET
9864 || GET_CODE (SET_DEST (set)) == ZERO_EXTRACT
9865 || GET_CODE (SET_DEST (set)) == SIGN_EXTRACT
9866 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
9868 for (i = (size - 1) / UNITS_PER_WORD + regno; i >= regno; i--)
9870 reg_state[i].use_index = -1;
9871 reg_state[i].store_ruid = reload_combine_ruid;
9874 else
9876 for (i = (size - 1) / UNITS_PER_WORD + regno; i >= regno; i--)
9878 reg_state[i].store_ruid = reload_combine_ruid;
9879 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9884 /* XP points to a piece of rtl that has to be checked for any uses of
9885 registers.
9886 *XP is the pattern of INSN, or a part of it.
9887 Called from reload_combine, and recursively by itself. */
9888 static void
9889 reload_combine_note_use (xp, insn)
9890 rtx *xp, insn;
9892 rtx x = *xp;
9893 enum rtx_code code = x->code;
9894 const char *fmt;
9895 int i, j;
9896 rtx offset = const0_rtx; /* For the REG case below. */
9898 switch (code)
9900 case SET:
9901 if (GET_CODE (SET_DEST (x)) == REG)
9903 reload_combine_note_use (&SET_SRC (x), insn);
9904 return;
9906 break;
9908 case CLOBBER:
9909 if (GET_CODE (SET_DEST (x)) == REG)
9910 return;
9911 break;
9913 case PLUS:
9914 /* We are interested in (plus (reg) (const_int)) . */
9915 if (GET_CODE (XEXP (x, 0)) != REG || GET_CODE (XEXP (x, 1)) != CONST_INT)
9916 break;
9917 offset = XEXP (x, 1);
9918 x = XEXP (x, 0);
9919 /* Fall through. */
9920 case REG:
9922 int regno = REGNO (x);
9923 int use_index;
9925 /* Some spurious USEs of pseudo registers might remain.
9926 Just ignore them. */
9927 if (regno >= FIRST_PSEUDO_REGISTER)
9928 return;
9930 /* If this register is already used in some unknown fashion, we
9931 can't do anything.
9932 If we decrement the index from zero to -1, we can't store more
9933 uses, so this register becomes used in an unknown fashion. */
9934 use_index = --reg_state[regno].use_index;
9935 if (use_index < 0)
9936 return;
9938 if (use_index != RELOAD_COMBINE_MAX_USES - 1)
9940 /* We have found another use for a register that is already
9941 used later. Check if the offsets match; if not, mark the
9942 register as used in an unknown fashion. */
9943 if (! rtx_equal_p (offset, reg_state[regno].offset))
9945 reg_state[regno].use_index = -1;
9946 return;
9949 else
9951 /* This is the first use of this register we have seen since we
9952 marked it as dead. */
9953 reg_state[regno].offset = offset;
9954 reg_state[regno].use_ruid = reload_combine_ruid;
9956 reg_state[regno].reg_use[use_index].insn = insn;
9957 reg_state[regno].reg_use[use_index].usep = xp;
9958 return;
9961 default:
9962 break;
9965 /* Recursively process the components of X. */
9966 fmt = GET_RTX_FORMAT (code);
9967 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9969 if (fmt[i] == 'e')
9970 reload_combine_note_use (&XEXP (x, i), insn);
9971 else if (fmt[i] == 'E')
9973 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9974 reload_combine_note_use (&XVECEXP (x, i, j), insn);
9979 /* See if we can reduce the cost of a constant by replacing a move with
9980 an add. */
9981 /* We cannot do our optimization across labels. Invalidating all the
9982 information about register contents we have would be costly, so we
9983 use last_label_luid (local variable of reload_cse_move2add) to note
9984 where the label is and then later disable any optimization that would
9985 cross it.
9986 reg_offset[n] / reg_base_reg[n] / reg_mode[n] are only valid if
9987 reg_set_luid[n] is larger than last_label_luid[n] . */
9988 static int reg_set_luid[FIRST_PSEUDO_REGISTER];
9989 /* reg_offset[n] has to be CONST_INT for it and reg_base_reg[n] /
9990 reg_mode[n] to be valid.
9991 If reg_offset[n] is a CONST_INT and reg_base_reg[n] is negative, register n
9992 has been set to reg_offset[n] in mode reg_mode[n] .
9993 If reg_offset[n] is a CONST_INT and reg_base_reg[n] is non-negative,
9994 register n has been set to the sum of reg_offset[n] and register
9995 reg_base_reg[n], calculated in mode reg_mode[n] . */
9996 static rtx reg_offset[FIRST_PSEUDO_REGISTER];
9997 static int reg_base_reg[FIRST_PSEUDO_REGISTER];
9998 static enum machine_mode reg_mode[FIRST_PSEUDO_REGISTER];
9999 /* move2add_luid is linearily increased while scanning the instructions
10000 from first to last. It is used to set reg_set_luid in
10001 reload_cse_move2add and move2add_note_store. */
10002 static int move2add_luid;
10004 /* Generate a CONST_INT and force it in the range of MODE. */
10005 static rtx
10006 gen_mode_int (mode, value)
10007 enum machine_mode mode;
10008 HOST_WIDE_INT value;
10010 HOST_WIDE_INT cval = value & GET_MODE_MASK (mode);
10011 int width = GET_MODE_BITSIZE (mode);
10013 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative number,
10014 sign extend it. */
10015 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
10016 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
10017 cval |= (HOST_WIDE_INT) -1 << width;
10019 return GEN_INT (cval);
10022 static void
10023 reload_cse_move2add (first)
10024 rtx first;
10026 int i;
10027 rtx insn;
10028 int last_label_luid;
10030 for (i = FIRST_PSEUDO_REGISTER-1; i >= 0; i--)
10031 reg_set_luid[i] = 0;
10033 last_label_luid = 0;
10034 move2add_luid = 1;
10035 for (insn = first; insn; insn = NEXT_INSN (insn), move2add_luid++)
10037 rtx pat, note;
10039 if (GET_CODE (insn) == CODE_LABEL)
10040 last_label_luid = move2add_luid;
10041 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
10042 continue;
10043 pat = PATTERN (insn);
10044 /* For simplicity, we only perform this optimization on
10045 straightforward SETs. */
10046 if (GET_CODE (pat) == SET
10047 && GET_CODE (SET_DEST (pat)) == REG)
10049 rtx reg = SET_DEST (pat);
10050 int regno = REGNO (reg);
10051 rtx src = SET_SRC (pat);
10053 /* Check if we have valid information on the contents of this
10054 register in the mode of REG. */
10055 /* ??? We don't know how zero / sign extension is handled, hence
10056 we can't go from a narrower to a wider mode. */
10057 if (reg_set_luid[regno] > last_label_luid
10058 && (GET_MODE_SIZE (GET_MODE (reg))
10059 <= GET_MODE_SIZE (reg_mode[regno]))
10060 && GET_CODE (reg_offset[regno]) == CONST_INT)
10062 /* Try to transform (set (REGX) (CONST_INT A))
10064 (set (REGX) (CONST_INT B))
10066 (set (REGX) (CONST_INT A))
10068 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
10070 if (GET_CODE (src) == CONST_INT && reg_base_reg[regno] < 0)
10072 int success = 0;
10073 rtx new_src
10074 = gen_mode_int (GET_MODE (reg),
10075 INTVAL (src) - INTVAL (reg_offset[regno]));
10076 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
10077 use (set (reg) (reg)) instead.
10078 We don't delete this insn, nor do we convert it into a
10079 note, to avoid losing register notes or the return
10080 value flag. jump2 already knowns how to get rid of
10081 no-op moves. */
10082 if (new_src == const0_rtx)
10083 success = validate_change (insn, &SET_SRC (pat), reg, 0);
10084 else if (rtx_cost (new_src, PLUS) < rtx_cost (src, SET)
10085 && have_add2_insn (GET_MODE (reg)))
10086 success = validate_change (insn, &PATTERN (insn),
10087 gen_add2_insn (reg, new_src), 0);
10088 reg_set_luid[regno] = move2add_luid;
10089 reg_mode[regno] = GET_MODE (reg);
10090 reg_offset[regno] = src;
10091 continue;
10094 /* Try to transform (set (REGX) (REGY))
10095 (set (REGX) (PLUS (REGX) (CONST_INT A)))
10097 (set (REGX) (REGY))
10098 (set (REGX) (PLUS (REGX) (CONST_INT B)))
10100 (REGX) (REGY))
10101 (set (REGX) (PLUS (REGX) (CONST_INT A)))
10103 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
10104 else if (GET_CODE (src) == REG
10105 && reg_base_reg[regno] == REGNO (src)
10106 && reg_set_luid[regno] > reg_set_luid[REGNO (src)])
10108 rtx next = next_nonnote_insn (insn);
10109 rtx set = NULL_RTX;
10110 if (next)
10111 set = single_set (next);
10112 if (next
10113 && set
10114 && SET_DEST (set) == reg
10115 && GET_CODE (SET_SRC (set)) == PLUS
10116 && XEXP (SET_SRC (set), 0) == reg
10117 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
10119 rtx src3 = XEXP (SET_SRC (set), 1);
10120 rtx new_src
10121 = gen_mode_int (GET_MODE (reg),
10122 INTVAL (src3)
10123 - INTVAL (reg_offset[regno]));
10124 int success = 0;
10126 if (new_src == const0_rtx)
10127 /* See above why we create (set (reg) (reg)) here. */
10128 success
10129 = validate_change (next, &SET_SRC (set), reg, 0);
10130 else if ((rtx_cost (new_src, PLUS)
10131 < 2 + rtx_cost (src3, SET))
10132 && have_add2_insn (GET_MODE (reg)))
10133 success
10134 = validate_change (next, &PATTERN (next),
10135 gen_add2_insn (reg, new_src), 0);
10136 if (success)
10138 /* INSN might be the first insn in a basic block
10139 if the preceding insn is a conditional jump
10140 or a possible-throwing call. */
10141 PUT_CODE (insn, NOTE);
10142 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
10143 NOTE_SOURCE_FILE (insn) = 0;
10145 insn = next;
10146 reg_set_luid[regno] = move2add_luid;
10147 reg_mode[regno] = GET_MODE (reg);
10148 reg_offset[regno] = src3;
10149 continue;
10155 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
10157 if (REG_NOTE_KIND (note) == REG_INC
10158 && GET_CODE (XEXP (note, 0)) == REG)
10160 /* Indicate that this register has been recently written to,
10161 but the exact contents are not available. */
10162 int regno = REGNO (XEXP (note, 0));
10163 if (regno < FIRST_PSEUDO_REGISTER)
10165 reg_set_luid[regno] = move2add_luid;
10166 reg_offset[regno] = note;
10170 note_stores (PATTERN (insn), move2add_note_store, NULL);
10171 /* If this is a CALL_INSN, all call used registers are stored with
10172 unknown values. */
10173 if (GET_CODE (insn) == CALL_INSN)
10175 for (i = FIRST_PSEUDO_REGISTER-1; i >= 0; i--)
10177 if (call_used_regs[i])
10179 reg_set_luid[i] = move2add_luid;
10180 reg_offset[i] = insn; /* Invalidate contents. */
10187 /* SET is a SET or CLOBBER that sets DST.
10188 Update reg_set_luid, reg_offset and reg_base_reg accordingly.
10189 Called from reload_cse_move2add via note_stores. */
10190 static void
10191 move2add_note_store (dst, set, data)
10192 rtx dst, set;
10193 void *data ATTRIBUTE_UNUSED;
10195 int regno = 0;
10196 int i;
10198 enum machine_mode mode = GET_MODE (dst);
10199 if (GET_CODE (dst) == SUBREG)
10201 regno = SUBREG_WORD (dst);
10202 dst = SUBREG_REG (dst);
10204 if (GET_CODE (dst) != REG)
10205 return;
10207 regno += REGNO (dst);
10209 if (HARD_REGNO_NREGS (regno, mode) == 1 && GET_CODE (set) == SET
10210 && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
10211 && GET_CODE (SET_DEST (set)) != SIGN_EXTRACT
10212 && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
10214 rtx src = SET_SRC (set);
10216 reg_mode[regno] = mode;
10217 switch (GET_CODE (src))
10219 case PLUS:
10221 rtx src0 = XEXP (src, 0);
10222 if (GET_CODE (src0) == REG)
10224 if (REGNO (src0) != regno
10225 || reg_offset[regno] != const0_rtx)
10227 reg_base_reg[regno] = REGNO (src0);
10228 reg_set_luid[regno] = move2add_luid;
10230 reg_offset[regno] = XEXP (src, 1);
10231 break;
10233 reg_set_luid[regno] = move2add_luid;
10234 reg_offset[regno] = set; /* Invalidate contents. */
10235 break;
10238 case REG:
10239 reg_base_reg[regno] = REGNO (SET_SRC (set));
10240 reg_offset[regno] = const0_rtx;
10241 reg_set_luid[regno] = move2add_luid;
10242 break;
10244 default:
10245 reg_base_reg[regno] = -1;
10246 reg_offset[regno] = SET_SRC (set);
10247 reg_set_luid[regno] = move2add_luid;
10248 break;
10251 else
10253 for (i = regno + HARD_REGNO_NREGS (regno, mode) - 1; i >= regno; i--)
10255 /* Indicate that this register has been recently written to,
10256 but the exact contents are not available. */
10257 reg_set_luid[i] = move2add_luid;
10258 reg_offset[i] = dst;
10263 #ifdef AUTO_INC_DEC
10264 static void
10265 add_auto_inc_notes (insn, x)
10266 rtx insn;
10267 rtx x;
10269 enum rtx_code code = GET_CODE (x);
10270 const char *fmt;
10271 int i, j;
10273 if (code == MEM && auto_inc_p (XEXP (x, 0)))
10275 REG_NOTES (insn)
10276 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
10277 return;
10280 /* Scan all the operand sub-expressions. */
10281 fmt = GET_RTX_FORMAT (code);
10282 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10284 if (fmt[i] == 'e')
10285 add_auto_inc_notes (insn, XEXP (x, i));
10286 else if (fmt[i] == 'E')
10287 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10288 add_auto_inc_notes (insn, XVECEXP (x, i, j));
10291 #endif