* gcc.dg/cpp/assembl2.S: New test case.
[official-gcc.git] / gcc / reload1.c
blob47e1eb4f53286c3590b92e6671a6e76a387bd98f
1 /* Reload pseudo regs into hard regs for insns that require hard regs.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 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 "cselib.h"
42 #include "real.h"
43 #include "toplev.h"
45 #if !defined PREFERRED_STACK_BOUNDARY && defined STACK_BOUNDARY
46 #define PREFERRED_STACK_BOUNDARY STACK_BOUNDARY
47 #endif
49 /* This file contains the reload pass of the compiler, which is
50 run after register allocation has been done. It checks that
51 each insn is valid (operands required to be in registers really
52 are in registers of the proper class) and fixes up invalid ones
53 by copying values temporarily into registers for the insns
54 that need them.
56 The results of register allocation are described by the vector
57 reg_renumber; the insns still contain pseudo regs, but reg_renumber
58 can be used to find which hard reg, if any, a pseudo reg is in.
60 The technique we always use is to free up a few hard regs that are
61 called ``reload regs'', and for each place where a pseudo reg
62 must be in a hard reg, copy it temporarily into one of the reload regs.
64 Reload regs are allocated locally for every instruction that needs
65 reloads. When there are pseudos which are allocated to a register that
66 has been chosen as a reload reg, such pseudos must be ``spilled''.
67 This means that they go to other hard regs, or to stack slots if no other
68 available hard regs can be found. Spilling can invalidate more
69 insns, requiring additional need for reloads, so we must keep checking
70 until the process stabilizes.
72 For machines with different classes of registers, we must keep track
73 of the register class needed for each reload, and make sure that
74 we allocate enough reload registers of each class.
76 The file reload.c contains the code that checks one insn for
77 validity and reports the reloads that it needs. This file
78 is in charge of scanning the entire rtl code, accumulating the
79 reload needs, spilling, assigning reload registers to use for
80 fixing up each insn, and generating the new insns to copy values
81 into the reload registers. */
83 #ifndef REGISTER_MOVE_COST
84 #define REGISTER_MOVE_COST(m, x, y) 2
85 #endif
87 #ifndef LOCAL_REGNO
88 #define LOCAL_REGNO(REGNO) 0
89 #endif
91 /* During reload_as_needed, element N contains a REG rtx for the hard reg
92 into which reg N has been reloaded (perhaps for a previous insn). */
93 static rtx *reg_last_reload_reg;
95 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
96 for an output reload that stores into reg N. */
97 static char *reg_has_output_reload;
99 /* Indicates which hard regs are reload-registers for an output reload
100 in the current insn. */
101 static HARD_REG_SET reg_is_output_reload;
103 /* Element N is the constant value to which pseudo reg N is equivalent,
104 or zero if pseudo reg N is not equivalent to a constant.
105 find_reloads looks at this in order to replace pseudo reg N
106 with the constant it stands for. */
107 rtx *reg_equiv_constant;
109 /* Element N is a memory location to which pseudo reg N is equivalent,
110 prior to any register elimination (such as frame pointer to stack
111 pointer). Depending on whether or not it is a valid address, this value
112 is transferred to either reg_equiv_address or reg_equiv_mem. */
113 rtx *reg_equiv_memory_loc;
115 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
116 This is used when the address is not valid as a memory address
117 (because its displacement is too big for the machine.) */
118 rtx *reg_equiv_address;
120 /* Element N is the memory slot to which pseudo reg N is equivalent,
121 or zero if pseudo reg N is not equivalent to a memory slot. */
122 rtx *reg_equiv_mem;
124 /* Widest width in which each pseudo reg is referred to (via subreg). */
125 static unsigned int *reg_max_ref_width;
127 /* Element N is the list of insns that initialized reg N from its equivalent
128 constant or memory slot. */
129 static rtx *reg_equiv_init;
131 /* Vector to remember old contents of reg_renumber before spilling. */
132 static short *reg_old_renumber;
134 /* During reload_as_needed, element N contains the last pseudo regno reloaded
135 into hard register N. If that pseudo reg occupied more than one register,
136 reg_reloaded_contents points to that pseudo for each spill register in
137 use; all of these must remain set for an inheritance to occur. */
138 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
140 /* During reload_as_needed, element N contains the insn for which
141 hard register N was last used. Its contents are significant only
142 when reg_reloaded_valid is set for this register. */
143 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
145 /* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid */
146 static HARD_REG_SET reg_reloaded_valid;
147 /* Indicate if the register was dead at the end of the reload.
148 This is only valid if reg_reloaded_contents is set and valid. */
149 static HARD_REG_SET reg_reloaded_dead;
151 /* Number of spill-regs so far; number of valid elements of spill_regs. */
152 static int n_spills;
154 /* In parallel with spill_regs, contains REG rtx's for those regs.
155 Holds the last rtx used for any given reg, or 0 if it has never
156 been used for spilling yet. This rtx is reused, provided it has
157 the proper mode. */
158 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
160 /* In parallel with spill_regs, contains nonzero for a spill reg
161 that was stored after the last time it was used.
162 The precise value is the insn generated to do the store. */
163 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
165 /* This is the register that was stored with spill_reg_store. This is a
166 copy of reload_out / reload_out_reg when the value was stored; if
167 reload_out is a MEM, spill_reg_stored_to will be set to reload_out_reg. */
168 static rtx spill_reg_stored_to[FIRST_PSEUDO_REGISTER];
170 /* This table is the inverse mapping of spill_regs:
171 indexed by hard reg number,
172 it contains the position of that reg in spill_regs,
173 or -1 for something that is not in spill_regs.
175 ?!? This is no longer accurate. */
176 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
178 /* This reg set indicates registers that can't be used as spill registers for
179 the currently processed insn. These are the hard registers which are live
180 during the insn, but not allocated to pseudos, as well as fixed
181 registers. */
182 static HARD_REG_SET bad_spill_regs;
184 /* These are the hard registers that can't be used as spill register for any
185 insn. This includes registers used for user variables and registers that
186 we can't eliminate. A register that appears in this set also can't be used
187 to retry register allocation. */
188 static HARD_REG_SET bad_spill_regs_global;
190 /* Describes order of use of registers for reloading
191 of spilled pseudo-registers. `n_spills' is the number of
192 elements that are actually valid; new ones are added at the end.
194 Both spill_regs and spill_reg_order are used on two occasions:
195 once during find_reload_regs, where they keep track of the spill registers
196 for a single insn, but also during reload_as_needed where they show all
197 the registers ever used by reload. For the latter case, the information
198 is calculated during finish_spills. */
199 static short spill_regs[FIRST_PSEUDO_REGISTER];
201 /* This vector of reg sets indicates, for each pseudo, which hard registers
202 may not be used for retrying global allocation because the register was
203 formerly spilled from one of them. If we allowed reallocating a pseudo to
204 a register that it was already allocated to, reload might not
205 terminate. */
206 static HARD_REG_SET *pseudo_previous_regs;
208 /* This vector of reg sets indicates, for each pseudo, which hard
209 registers may not be used for retrying global allocation because they
210 are used as spill registers during one of the insns in which the
211 pseudo is live. */
212 static HARD_REG_SET *pseudo_forbidden_regs;
214 /* All hard regs that have been used as spill registers for any insn are
215 marked in this set. */
216 static HARD_REG_SET used_spill_regs;
218 /* Index of last register assigned as a spill register. We allocate in
219 a round-robin fashion. */
220 static int last_spill_reg;
222 /* Nonzero if indirect addressing is supported on the machine; this means
223 that spilling (REG n) does not require reloading it into a register in
224 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
225 value indicates the level of indirect addressing supported, e.g., two
226 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
227 a hard register. */
228 static char spill_indirect_levels;
230 /* Nonzero if indirect addressing is supported when the innermost MEM is
231 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
232 which these are valid is the same as spill_indirect_levels, above. */
233 char indirect_symref_ok;
235 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
236 char double_reg_address_ok;
238 /* Record the stack slot for each spilled hard register. */
239 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
241 /* Width allocated so far for that stack slot. */
242 static unsigned int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
244 /* Record which pseudos needed to be spilled. */
245 static regset_head spilled_pseudos;
247 /* Used for communication between order_regs_for_reload and count_pseudo.
248 Used to avoid counting one pseudo twice. */
249 static regset_head pseudos_counted;
251 /* First uid used by insns created by reload in this function.
252 Used in find_equiv_reg. */
253 int reload_first_uid;
255 /* Flag set by local-alloc or global-alloc if anything is live in
256 a call-clobbered reg across calls. */
257 int caller_save_needed;
259 /* Set to 1 while reload_as_needed is operating.
260 Required by some machines to handle any generated moves differently. */
261 int reload_in_progress = 0;
263 /* These arrays record the insn_code of insns that may be needed to
264 perform input and output reloads of special objects. They provide a
265 place to pass a scratch register. */
266 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
267 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
269 /* This obstack is used for allocation of rtl during register elimination.
270 The allocated storage can be freed once find_reloads has processed the
271 insn. */
272 struct obstack reload_obstack;
274 /* Points to the beginning of the reload_obstack. All insn_chain structures
275 are allocated first. */
276 char *reload_startobj;
278 /* The point after all insn_chain structures. Used to quickly deallocate
279 memory allocated in copy_reloads during calculate_needs_all_insns. */
280 char *reload_firstobj;
282 /* This points before all local rtl generated by register elimination.
283 Used to quickly free all memory after processing one insn. */
284 static char *reload_insn_firstobj;
286 #define obstack_chunk_alloc xmalloc
287 #define obstack_chunk_free free
289 /* List of insn_chain instructions, one for every insn that reload needs to
290 examine. */
291 struct insn_chain *reload_insn_chain;
293 #ifdef TREE_CODE
294 extern tree current_function_decl;
295 #else
296 extern union tree_node *current_function_decl;
297 #endif
299 /* List of all insns needing reloads. */
300 static struct insn_chain *insns_need_reload;
302 /* This structure is used to record information about register eliminations.
303 Each array entry describes one possible way of eliminating a register
304 in favor of another. If there is more than one way of eliminating a
305 particular register, the most preferred should be specified first. */
307 struct elim_table
309 int from; /* Register number to be eliminated. */
310 int to; /* Register number used as replacement. */
311 int initial_offset; /* Initial difference between values. */
312 int can_eliminate; /* Non-zero if this elimination can be done. */
313 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
314 insns made by reload. */
315 int offset; /* Current offset between the two regs. */
316 int previous_offset; /* Offset at end of previous insn. */
317 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
318 rtx from_rtx; /* REG rtx for the register to be eliminated.
319 We cannot simply compare the number since
320 we might then spuriously replace a hard
321 register corresponding to a pseudo
322 assigned to the reg to be eliminated. */
323 rtx to_rtx; /* REG rtx for the replacement. */
326 static struct elim_table *reg_eliminate = 0;
328 /* This is an intermediate structure to initialize the table. It has
329 exactly the members provided by ELIMINABLE_REGS. */
330 static struct elim_table_1
332 int from;
333 int to;
334 } reg_eliminate_1[] =
336 /* If a set of eliminable registers was specified, define the table from it.
337 Otherwise, default to the normal case of the frame pointer being
338 replaced by the stack pointer. */
340 #ifdef ELIMINABLE_REGS
341 ELIMINABLE_REGS;
342 #else
343 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
344 #endif
346 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
348 /* Record the number of pending eliminations that have an offset not equal
349 to their initial offset. If non-zero, we use a new copy of each
350 replacement result in any insns encountered. */
351 int num_not_at_initial_offset;
353 /* Count the number of registers that we may be able to eliminate. */
354 static int num_eliminable;
355 /* And the number of registers that are equivalent to a constant that
356 can be eliminated to frame_pointer / arg_pointer + constant. */
357 static int num_eliminable_invariants;
359 /* For each label, we record the offset of each elimination. If we reach
360 a label by more than one path and an offset differs, we cannot do the
361 elimination. This information is indexed by the number of the label.
362 The first table is an array of flags that records whether we have yet
363 encountered a label and the second table is an array of arrays, one
364 entry in the latter array for each elimination. */
366 static char *offsets_known_at;
367 static int (*offsets_at)[NUM_ELIMINABLE_REGS];
369 /* Number of labels in the current function. */
371 static int num_labels;
373 static void replace_pseudos_in_call_usage PARAMS((rtx *,
374 enum machine_mode,
375 rtx));
376 static void maybe_fix_stack_asms PARAMS ((void));
377 static void copy_reloads PARAMS ((struct insn_chain *));
378 static void calculate_needs_all_insns PARAMS ((int));
379 static int find_reg PARAMS ((struct insn_chain *, int));
380 static void find_reload_regs PARAMS ((struct insn_chain *));
381 static void select_reload_regs PARAMS ((void));
382 static void delete_caller_save_insns PARAMS ((void));
384 static void spill_failure PARAMS ((rtx, enum reg_class));
385 static void count_spilled_pseudo PARAMS ((int, int, int));
386 static void delete_dead_insn PARAMS ((rtx));
387 static void alter_reg PARAMS ((int, int));
388 static void set_label_offsets PARAMS ((rtx, rtx, int));
389 static void check_eliminable_occurrences PARAMS ((rtx));
390 static void elimination_effects PARAMS ((rtx, enum machine_mode));
391 static int eliminate_regs_in_insn PARAMS ((rtx, int));
392 static void update_eliminable_offsets PARAMS ((void));
393 static void mark_not_eliminable PARAMS ((rtx, rtx, void *));
394 static void set_initial_elim_offsets PARAMS ((void));
395 static void verify_initial_elim_offsets PARAMS ((void));
396 static void set_initial_label_offsets PARAMS ((void));
397 static void set_offsets_for_label PARAMS ((rtx));
398 static void init_elim_table PARAMS ((void));
399 static void update_eliminables PARAMS ((HARD_REG_SET *));
400 static void spill_hard_reg PARAMS ((unsigned int, int));
401 static int finish_spills PARAMS ((int));
402 static void ior_hard_reg_set PARAMS ((HARD_REG_SET *, HARD_REG_SET *));
403 static void scan_paradoxical_subregs PARAMS ((rtx));
404 static void count_pseudo PARAMS ((int));
405 static void order_regs_for_reload PARAMS ((struct insn_chain *));
406 static void reload_as_needed PARAMS ((int));
407 static void forget_old_reloads_1 PARAMS ((rtx, rtx, void *));
408 static int reload_reg_class_lower PARAMS ((const PTR, const PTR));
409 static void mark_reload_reg_in_use PARAMS ((unsigned int, int,
410 enum reload_type,
411 enum machine_mode));
412 static void clear_reload_reg_in_use PARAMS ((unsigned int, int,
413 enum reload_type,
414 enum machine_mode));
415 static int reload_reg_free_p PARAMS ((unsigned int, int,
416 enum reload_type));
417 static int reload_reg_free_for_value_p PARAMS ((int, int, enum reload_type,
418 rtx, rtx, int, int));
419 static int free_for_value_p PARAMS ((int, enum machine_mode, int,
420 enum reload_type, rtx, rtx,
421 int, int));
422 static int reload_reg_reaches_end_p PARAMS ((unsigned int, int,
423 enum reload_type));
424 static int allocate_reload_reg PARAMS ((struct insn_chain *, int,
425 int));
426 static int conflicts_with_override PARAMS ((rtx));
427 static void failed_reload PARAMS ((rtx, int));
428 static int set_reload_reg PARAMS ((int, int));
429 static void choose_reload_regs_init PARAMS ((struct insn_chain *, rtx *));
430 static void choose_reload_regs PARAMS ((struct insn_chain *));
431 static void merge_assigned_reloads PARAMS ((rtx));
432 static void emit_input_reload_insns PARAMS ((struct insn_chain *,
433 struct reload *, rtx, int));
434 static void emit_output_reload_insns PARAMS ((struct insn_chain *,
435 struct reload *, int));
436 static void do_input_reload PARAMS ((struct insn_chain *,
437 struct reload *, int));
438 static void do_output_reload PARAMS ((struct insn_chain *,
439 struct reload *, int));
440 static void emit_reload_insns PARAMS ((struct insn_chain *));
441 static void delete_output_reload PARAMS ((rtx, int, int));
442 static void delete_address_reloads PARAMS ((rtx, rtx));
443 static void delete_address_reloads_1 PARAMS ((rtx, rtx, rtx));
444 static rtx inc_for_reload PARAMS ((rtx, rtx, rtx, int));
445 static int constraint_accepts_reg_p PARAMS ((const char *, rtx));
446 static void reload_cse_regs_1 PARAMS ((rtx));
447 static int reload_cse_noop_set_p PARAMS ((rtx));
448 static int reload_cse_simplify_set PARAMS ((rtx, rtx));
449 static int reload_cse_simplify_operands PARAMS ((rtx));
450 static void reload_combine PARAMS ((void));
451 static void reload_combine_note_use PARAMS ((rtx *, rtx));
452 static void reload_combine_note_store PARAMS ((rtx, rtx, void *));
453 static void reload_cse_move2add PARAMS ((rtx));
454 static void move2add_note_store PARAMS ((rtx, rtx, void *));
455 #ifdef AUTO_INC_DEC
456 static void add_auto_inc_notes PARAMS ((rtx, rtx));
457 #endif
458 static rtx gen_mode_int PARAMS ((enum machine_mode,
459 HOST_WIDE_INT));
460 static void failed_reload PARAMS ((rtx, int));
461 static int set_reload_reg PARAMS ((int, int));
462 static void reload_cse_delete_noop_set PARAMS ((rtx, rtx));
463 static void reload_cse_simplify PARAMS ((rtx));
464 extern void dump_needs PARAMS ((struct insn_chain *));
466 /* Initialize the reload pass once per compilation. */
468 void
469 init_reload ()
471 register int i;
473 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
474 Set spill_indirect_levels to the number of levels such addressing is
475 permitted, zero if it is not permitted at all. */
477 register rtx tem
478 = gen_rtx_MEM (Pmode,
479 gen_rtx_PLUS (Pmode,
480 gen_rtx_REG (Pmode,
481 LAST_VIRTUAL_REGISTER + 1),
482 GEN_INT (4)));
483 spill_indirect_levels = 0;
485 while (memory_address_p (QImode, tem))
487 spill_indirect_levels++;
488 tem = gen_rtx_MEM (Pmode, tem);
491 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
493 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
494 indirect_symref_ok = memory_address_p (QImode, tem);
496 /* See if reg+reg is a valid (and offsettable) address. */
498 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
500 tem = gen_rtx_PLUS (Pmode,
501 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
502 gen_rtx_REG (Pmode, i));
504 /* This way, we make sure that reg+reg is an offsettable address. */
505 tem = plus_constant (tem, 4);
507 if (memory_address_p (QImode, tem))
509 double_reg_address_ok = 1;
510 break;
514 /* Initialize obstack for our rtl allocation. */
515 gcc_obstack_init (&reload_obstack);
516 reload_startobj = (char *) obstack_alloc (&reload_obstack, 0);
518 INIT_REG_SET (&spilled_pseudos);
519 INIT_REG_SET (&pseudos_counted);
522 /* List of insn chains that are currently unused. */
523 static struct insn_chain *unused_insn_chains = 0;
525 /* Allocate an empty insn_chain structure. */
526 struct insn_chain *
527 new_insn_chain ()
529 struct insn_chain *c;
531 if (unused_insn_chains == 0)
533 c = (struct insn_chain *)
534 obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
535 INIT_REG_SET (&c->live_throughout);
536 INIT_REG_SET (&c->dead_or_set);
538 else
540 c = unused_insn_chains;
541 unused_insn_chains = c->next;
543 c->is_caller_save_insn = 0;
544 c->need_operand_change = 0;
545 c->need_reload = 0;
546 c->need_elim = 0;
547 return c;
550 /* Small utility function to set all regs in hard reg set TO which are
551 allocated to pseudos in regset FROM. */
553 void
554 compute_use_by_pseudos (to, from)
555 HARD_REG_SET *to;
556 regset from;
558 unsigned int regno;
560 EXECUTE_IF_SET_IN_REG_SET
561 (from, FIRST_PSEUDO_REGISTER, regno,
563 int r = reg_renumber[regno];
564 int nregs;
566 if (r < 0)
568 /* reload_combine uses the information from
569 BASIC_BLOCK->global_live_at_start, which might still
570 contain registers that have not actually been allocated
571 since they have an equivalence. */
572 if (! reload_completed)
573 abort ();
575 else
577 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (regno));
578 while (nregs-- > 0)
579 SET_HARD_REG_BIT (*to, r + nregs);
584 /* Replace all pseudos found in LOC with their corresponding
585 equivalences. */
587 static void
588 replace_pseudos_in_call_usage (loc, mem_mode, usage)
589 rtx *loc;
590 enum machine_mode mem_mode;
591 rtx usage;
593 rtx x = *loc;
594 enum rtx_code code;
595 const char *fmt;
596 int i, j;
598 if (! x)
599 return;
601 code = GET_CODE (x);
602 if (code == REG)
604 int regno = REGNO (x);
606 if (regno < FIRST_PSEUDO_REGISTER)
607 return;
609 x = eliminate_regs (x, mem_mode, usage);
610 if (x != *loc)
612 *loc = x;
613 replace_pseudos_in_call_usage (loc, mem_mode, usage);
614 return;
617 if (reg_equiv_constant[regno])
618 *loc = reg_equiv_constant[regno];
619 else if (reg_equiv_mem[regno])
620 *loc = reg_equiv_mem[regno];
621 else if (reg_equiv_address[regno])
622 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address[regno]);
623 else if (GET_CODE (regno_reg_rtx[regno]) != REG
624 || REGNO (regno_reg_rtx[regno]) != regno)
625 *loc = regno_reg_rtx[regno];
626 else
627 abort ();
629 return;
631 else if (code == MEM)
633 replace_pseudos_in_call_usage (& XEXP (x, 0), GET_MODE (x), usage);
634 return;
637 /* Process each of our operands recursively. */
638 fmt = GET_RTX_FORMAT (code);
639 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
640 if (*fmt == 'e')
641 replace_pseudos_in_call_usage (&XEXP (x, i), mem_mode, usage);
642 else if (*fmt == 'E')
643 for (j = 0; j < XVECLEN (x, i); j++)
644 replace_pseudos_in_call_usage (& XVECEXP (x, i, j), mem_mode, usage);
648 /* Global variables used by reload and its subroutines. */
650 /* Set during calculate_needs if an insn needs register elimination. */
651 static int something_needs_elimination;
652 /* Set during calculate_needs if an insn needs an operand changed. */
653 int something_needs_operands_changed;
655 /* Nonzero means we couldn't get enough spill regs. */
656 static int failure;
658 /* Main entry point for the reload pass.
660 FIRST is the first insn of the function being compiled.
662 GLOBAL nonzero means we were called from global_alloc
663 and should attempt to reallocate any pseudoregs that we
664 displace from hard regs we will use for reloads.
665 If GLOBAL is zero, we do not have enough information to do that,
666 so any pseudo reg that is spilled must go to the stack.
668 Return value is nonzero if reload failed
669 and we must not do any more for this function. */
672 reload (first, global)
673 rtx first;
674 int global;
676 register int i;
677 register rtx insn;
678 register struct elim_table *ep;
680 /* The two pointers used to track the true location of the memory used
681 for label offsets. */
682 char *real_known_ptr = NULL_PTR;
683 int (*real_at_ptr)[NUM_ELIMINABLE_REGS];
685 /* Make sure even insns with volatile mem refs are recognizable. */
686 init_recog ();
688 failure = 0;
690 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
692 /* Make sure that the last insn in the chain
693 is not something that needs reloading. */
694 emit_note (NULL_PTR, NOTE_INSN_DELETED);
696 /* Enable find_equiv_reg to distinguish insns made by reload. */
697 reload_first_uid = get_max_uid ();
699 #ifdef SECONDARY_MEMORY_NEEDED
700 /* Initialize the secondary memory table. */
701 clear_secondary_mem ();
702 #endif
704 /* We don't have a stack slot for any spill reg yet. */
705 memset ((char *) spill_stack_slot, 0, sizeof spill_stack_slot);
706 memset ((char *) spill_stack_slot_width, 0, sizeof spill_stack_slot_width);
708 /* Initialize the save area information for caller-save, in case some
709 are needed. */
710 init_save_areas ();
712 /* Compute which hard registers are now in use
713 as homes for pseudo registers.
714 This is done here rather than (eg) in global_alloc
715 because this point is reached even if not optimizing. */
716 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
717 mark_home_live (i);
719 /* A function that receives a nonlocal goto must save all call-saved
720 registers. */
721 if (current_function_has_nonlocal_label)
722 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
723 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
724 regs_ever_live[i] = 1;
726 /* Find all the pseudo registers that didn't get hard regs
727 but do have known equivalent constants or memory slots.
728 These include parameters (known equivalent to parameter slots)
729 and cse'd or loop-moved constant memory addresses.
731 Record constant equivalents in reg_equiv_constant
732 so they will be substituted by find_reloads.
733 Record memory equivalents in reg_mem_equiv so they can
734 be substituted eventually by altering the REG-rtx's. */
736 reg_equiv_constant = (rtx *) xcalloc (max_regno, sizeof (rtx));
737 reg_equiv_memory_loc = (rtx *) xcalloc (max_regno, sizeof (rtx));
738 reg_equiv_mem = (rtx *) xcalloc (max_regno, sizeof (rtx));
739 reg_equiv_init = (rtx *) xcalloc (max_regno, sizeof (rtx));
740 reg_equiv_address = (rtx *) xcalloc (max_regno, sizeof (rtx));
741 reg_max_ref_width = (unsigned int *) xcalloc (max_regno, sizeof (int));
742 reg_old_renumber = (short *) xcalloc (max_regno, sizeof (short));
743 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
744 pseudo_forbidden_regs
745 = (HARD_REG_SET *) xmalloc (max_regno * sizeof (HARD_REG_SET));
746 pseudo_previous_regs
747 = (HARD_REG_SET *) xcalloc (max_regno, sizeof (HARD_REG_SET));
749 CLEAR_HARD_REG_SET (bad_spill_regs_global);
751 /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
752 Also find all paradoxical subregs and find largest such for each pseudo.
753 On machines with small register classes, record hard registers that
754 are used for user variables. These can never be used for spills.
755 Also look for a "constant" NOTE_INSN_SETJMP. This means that all
756 caller-saved registers must be marked live. */
758 num_eliminable_invariants = 0;
759 for (insn = first; insn; insn = NEXT_INSN (insn))
761 rtx set = single_set (insn);
763 if (GET_CODE (insn) == NOTE && CONST_CALL_P (insn)
764 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
765 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
766 if (! call_used_regs[i])
767 regs_ever_live[i] = 1;
769 if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
771 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
772 if (note
773 #ifdef LEGITIMATE_PIC_OPERAND_P
774 && (! function_invariant_p (XEXP (note, 0))
775 || ! flag_pic
776 || LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))
777 #endif
780 rtx x = XEXP (note, 0);
781 i = REGNO (SET_DEST (set));
782 if (i > LAST_VIRTUAL_REGISTER)
784 if (GET_CODE (x) == MEM)
786 /* If the operand is a PLUS, the MEM may be shared,
787 so make sure we have an unshared copy here. */
788 if (GET_CODE (XEXP (x, 0)) == PLUS)
789 x = copy_rtx (x);
791 reg_equiv_memory_loc[i] = x;
793 else if (function_invariant_p (x))
795 if (GET_CODE (x) == PLUS)
797 /* This is PLUS of frame pointer and a constant,
798 and might be shared. Unshare it. */
799 reg_equiv_constant[i] = copy_rtx (x);
800 num_eliminable_invariants++;
802 else if (x == frame_pointer_rtx
803 || x == arg_pointer_rtx)
805 reg_equiv_constant[i] = x;
806 num_eliminable_invariants++;
808 else if (LEGITIMATE_CONSTANT_P (x))
809 reg_equiv_constant[i] = x;
810 else
811 reg_equiv_memory_loc[i]
812 = force_const_mem (GET_MODE (SET_DEST (set)), x);
814 else
815 continue;
817 /* If this register is being made equivalent to a MEM
818 and the MEM is not SET_SRC, the equivalencing insn
819 is one with the MEM as a SET_DEST and it occurs later.
820 So don't mark this insn now. */
821 if (GET_CODE (x) != MEM
822 || rtx_equal_p (SET_SRC (set), x))
823 reg_equiv_init[i]
824 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[i]);
829 /* If this insn is setting a MEM from a register equivalent to it,
830 this is the equivalencing insn. */
831 else if (set && GET_CODE (SET_DEST (set)) == MEM
832 && GET_CODE (SET_SRC (set)) == REG
833 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
834 && rtx_equal_p (SET_DEST (set),
835 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
836 reg_equiv_init[REGNO (SET_SRC (set))]
837 = gen_rtx_INSN_LIST (VOIDmode, insn,
838 reg_equiv_init[REGNO (SET_SRC (set))]);
840 if (INSN_P (insn))
841 scan_paradoxical_subregs (PATTERN (insn));
844 init_elim_table ();
846 num_labels = max_label_num () - get_first_label_num ();
848 /* Allocate the tables used to store offset information at labels. */
849 /* We used to use alloca here, but the size of what it would try to
850 allocate would occasionally cause it to exceed the stack limit and
851 cause a core dump. */
852 real_known_ptr = xmalloc (num_labels);
853 real_at_ptr
854 = (int (*)[NUM_ELIMINABLE_REGS])
855 xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
857 offsets_known_at = real_known_ptr - get_first_label_num ();
858 offsets_at
859 = (int (*)[NUM_ELIMINABLE_REGS]) (real_at_ptr - get_first_label_num ());
861 /* Alter each pseudo-reg rtx to contain its hard reg number.
862 Assign stack slots to the pseudos that lack hard regs or equivalents.
863 Do not touch virtual registers. */
865 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
866 alter_reg (i, -1);
868 /* If we have some registers we think can be eliminated, scan all insns to
869 see if there is an insn that sets one of these registers to something
870 other than itself plus a constant. If so, the register cannot be
871 eliminated. Doing this scan here eliminates an extra pass through the
872 main reload loop in the most common case where register elimination
873 cannot be done. */
874 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
875 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
876 || GET_CODE (insn) == CALL_INSN)
877 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
879 maybe_fix_stack_asms ();
881 insns_need_reload = 0;
882 something_needs_elimination = 0;
884 /* Initialize to -1, which means take the first spill register. */
885 last_spill_reg = -1;
887 /* Spill any hard regs that we know we can't eliminate. */
888 CLEAR_HARD_REG_SET (used_spill_regs);
889 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
890 if (! ep->can_eliminate)
891 spill_hard_reg (ep->from, 1);
893 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
894 if (frame_pointer_needed)
895 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
896 #endif
897 finish_spills (global);
899 /* From now on, we may need to generate moves differently. We may also
900 allow modifications of insns which cause them to not be recognized.
901 Any such modifications will be cleaned up during reload itself. */
902 reload_in_progress = 1;
904 /* This loop scans the entire function each go-round
905 and repeats until one repetition spills no additional hard regs. */
906 for (;;)
908 int something_changed;
909 int did_spill;
911 HOST_WIDE_INT starting_frame_size;
913 /* Round size of stack frame to stack_alignment_needed. This must be done
914 here because the stack size may be a part of the offset computation
915 for register elimination, and there might have been new stack slots
916 created in the last iteration of this loop. */
917 if (cfun->stack_alignment_needed)
918 assign_stack_local (BLKmode, 0, cfun->stack_alignment_needed);
920 starting_frame_size = get_frame_size ();
922 set_initial_elim_offsets ();
923 set_initial_label_offsets ();
925 /* For each pseudo register that has an equivalent location defined,
926 try to eliminate any eliminable registers (such as the frame pointer)
927 assuming initial offsets for the replacement register, which
928 is the normal case.
930 If the resulting location is directly addressable, substitute
931 the MEM we just got directly for the old REG.
933 If it is not addressable but is a constant or the sum of a hard reg
934 and constant, it is probably not addressable because the constant is
935 out of range, in that case record the address; we will generate
936 hairy code to compute the address in a register each time it is
937 needed. Similarly if it is a hard register, but one that is not
938 valid as an address register.
940 If the location is not addressable, but does not have one of the
941 above forms, assign a stack slot. We have to do this to avoid the
942 potential of producing lots of reloads if, e.g., a location involves
943 a pseudo that didn't get a hard register and has an equivalent memory
944 location that also involves a pseudo that didn't get a hard register.
946 Perhaps at some point we will improve reload_when_needed handling
947 so this problem goes away. But that's very hairy. */
949 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
950 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
952 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
954 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
955 XEXP (x, 0)))
956 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
957 else if (CONSTANT_P (XEXP (x, 0))
958 || (GET_CODE (XEXP (x, 0)) == REG
959 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
960 || (GET_CODE (XEXP (x, 0)) == PLUS
961 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
962 && (REGNO (XEXP (XEXP (x, 0), 0))
963 < FIRST_PSEUDO_REGISTER)
964 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
965 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
966 else
968 /* Make a new stack slot. Then indicate that something
969 changed so we go back and recompute offsets for
970 eliminable registers because the allocation of memory
971 below might change some offset. reg_equiv_{mem,address}
972 will be set up for this pseudo on the next pass around
973 the loop. */
974 reg_equiv_memory_loc[i] = 0;
975 reg_equiv_init[i] = 0;
976 alter_reg (i, -1);
980 if (caller_save_needed)
981 setup_save_areas ();
983 /* If we allocated another stack slot, redo elimination bookkeeping. */
984 if (starting_frame_size != get_frame_size ())
985 continue;
987 if (caller_save_needed)
989 save_call_clobbered_regs ();
990 /* That might have allocated new insn_chain structures. */
991 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
994 calculate_needs_all_insns (global);
996 CLEAR_REG_SET (&spilled_pseudos);
997 did_spill = 0;
999 something_changed = 0;
1001 /* If we allocated any new memory locations, make another pass
1002 since it might have changed elimination offsets. */
1003 if (starting_frame_size != get_frame_size ())
1004 something_changed = 1;
1007 HARD_REG_SET to_spill;
1008 CLEAR_HARD_REG_SET (to_spill);
1009 update_eliminables (&to_spill);
1010 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1011 if (TEST_HARD_REG_BIT (to_spill, i))
1013 spill_hard_reg (i, 1);
1014 did_spill = 1;
1016 /* Regardless of the state of spills, if we previously had
1017 a register that we thought we could eliminate, but no can
1018 not eliminate, we must run another pass.
1020 Consider pseudos which have an entry in reg_equiv_* which
1021 reference an eliminable register. We must make another pass
1022 to update reg_equiv_* so that we do not substitute in the
1023 old value from when we thought the elimination could be
1024 performed. */
1025 something_changed = 1;
1029 select_reload_regs ();
1030 if (failure)
1031 goto failed;
1033 if (insns_need_reload != 0 || did_spill)
1034 something_changed |= finish_spills (global);
1036 if (! something_changed)
1037 break;
1039 if (caller_save_needed)
1040 delete_caller_save_insns ();
1042 obstack_free (&reload_obstack, reload_firstobj);
1045 /* If global-alloc was run, notify it of any register eliminations we have
1046 done. */
1047 if (global)
1048 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1049 if (ep->can_eliminate)
1050 mark_elimination (ep->from, ep->to);
1052 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1053 If that insn didn't set the register (i.e., it copied the register to
1054 memory), just delete that insn instead of the equivalencing insn plus
1055 anything now dead. If we call delete_dead_insn on that insn, we may
1056 delete the insn that actually sets the register if the register dies
1057 there and that is incorrect. */
1059 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1061 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1063 rtx list;
1064 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1066 rtx equiv_insn = XEXP (list, 0);
1067 if (GET_CODE (equiv_insn) == NOTE)
1068 continue;
1069 if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1070 delete_dead_insn (equiv_insn);
1071 else
1073 PUT_CODE (equiv_insn, NOTE);
1074 NOTE_SOURCE_FILE (equiv_insn) = 0;
1075 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1081 /* Use the reload registers where necessary
1082 by generating move instructions to move the must-be-register
1083 values into or out of the reload registers. */
1085 if (insns_need_reload != 0 || something_needs_elimination
1086 || something_needs_operands_changed)
1088 HOST_WIDE_INT old_frame_size = get_frame_size ();
1090 reload_as_needed (global);
1092 if (old_frame_size != get_frame_size ())
1093 abort ();
1095 if (num_eliminable)
1096 verify_initial_elim_offsets ();
1099 /* If we were able to eliminate the frame pointer, show that it is no
1100 longer live at the start of any basic block. If it ls live by
1101 virtue of being in a pseudo, that pseudo will be marked live
1102 and hence the frame pointer will be known to be live via that
1103 pseudo. */
1105 if (! frame_pointer_needed)
1106 for (i = 0; i < n_basic_blocks; i++)
1107 CLEAR_REGNO_REG_SET (BASIC_BLOCK (i)->global_live_at_start,
1108 HARD_FRAME_POINTER_REGNUM);
1110 /* Come here (with failure set nonzero) if we can't get enough spill regs
1111 and we decide not to abort about it. */
1112 failed:
1114 CLEAR_REG_SET (&spilled_pseudos);
1115 reload_in_progress = 0;
1117 /* Now eliminate all pseudo regs by modifying them into
1118 their equivalent memory references.
1119 The REG-rtx's for the pseudos are modified in place,
1120 so all insns that used to refer to them now refer to memory.
1122 For a reg that has a reg_equiv_address, all those insns
1123 were changed by reloading so that no insns refer to it any longer;
1124 but the DECL_RTL of a variable decl may refer to it,
1125 and if so this causes the debugging info to mention the variable. */
1127 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1129 rtx addr = 0;
1130 int in_struct = 0;
1131 int is_scalar = 0;
1132 int is_readonly = 0;
1134 if (reg_equiv_memory_loc[i])
1136 in_struct = MEM_IN_STRUCT_P (reg_equiv_memory_loc[i]);
1137 is_scalar = MEM_SCALAR_P (reg_equiv_memory_loc[i]);
1138 is_readonly = RTX_UNCHANGING_P (reg_equiv_memory_loc[i]);
1141 if (reg_equiv_mem[i])
1142 addr = XEXP (reg_equiv_mem[i], 0);
1144 if (reg_equiv_address[i])
1145 addr = reg_equiv_address[i];
1147 if (addr)
1149 if (reg_renumber[i] < 0)
1151 rtx reg = regno_reg_rtx[i];
1152 PUT_CODE (reg, MEM);
1153 XEXP (reg, 0) = addr;
1154 REG_USERVAR_P (reg) = 0;
1155 RTX_UNCHANGING_P (reg) = is_readonly;
1156 MEM_IN_STRUCT_P (reg) = in_struct;
1157 MEM_SCALAR_P (reg) = is_scalar;
1158 /* We have no alias information about this newly created
1159 MEM. */
1160 MEM_ALIAS_SET (reg) = 0;
1162 else if (reg_equiv_mem[i])
1163 XEXP (reg_equiv_mem[i], 0) = addr;
1167 /* We must set reload_completed now since the cleanup_subreg_operands call
1168 below will re-recognize each insn and reload may have generated insns
1169 which are only valid during and after reload. */
1170 reload_completed = 1;
1172 /* Make a pass over all the insns and delete all USEs which we inserted
1173 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1174 notes. Delete all CLOBBER insns that don't refer to the return value
1175 and simplify (subreg (reg)) operands. Also remove all REG_RETVAL and
1176 REG_LIBCALL notes since they are no longer useful or accurate. Strip
1177 and regenerate REG_INC notes that may have been moved around. */
1179 for (insn = first; insn; insn = NEXT_INSN (insn))
1180 if (INSN_P (insn))
1182 rtx *pnote;
1184 if (GET_CODE (insn) == CALL_INSN)
1185 replace_pseudos_in_call_usage (& CALL_INSN_FUNCTION_USAGE (insn),
1186 VOIDmode,
1187 CALL_INSN_FUNCTION_USAGE (insn));
1189 if ((GET_CODE (PATTERN (insn)) == USE
1190 && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1191 || (GET_CODE (PATTERN (insn)) == CLOBBER
1192 && (GET_CODE (XEXP (PATTERN (insn), 0)) != REG
1193 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1195 PUT_CODE (insn, NOTE);
1196 NOTE_SOURCE_FILE (insn) = 0;
1197 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1198 continue;
1201 pnote = &REG_NOTES (insn);
1202 while (*pnote != 0)
1204 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1205 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1206 || REG_NOTE_KIND (*pnote) == REG_INC
1207 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1208 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1209 *pnote = XEXP (*pnote, 1);
1210 else
1211 pnote = &XEXP (*pnote, 1);
1214 #ifdef AUTO_INC_DEC
1215 add_auto_inc_notes (insn, PATTERN (insn));
1216 #endif
1218 /* And simplify (subreg (reg)) if it appears as an operand. */
1219 cleanup_subreg_operands (insn);
1222 /* If we are doing stack checking, give a warning if this function's
1223 frame size is larger than we expect. */
1224 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1226 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1227 static int verbose_warned = 0;
1229 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1230 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1231 size += UNITS_PER_WORD;
1233 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1235 warning ("frame size too large for reliable stack checking");
1236 if (! verbose_warned)
1238 warning ("try reducing the number of local variables");
1239 verbose_warned = 1;
1244 /* Indicate that we no longer have known memory locations or constants. */
1245 if (reg_equiv_constant)
1246 free (reg_equiv_constant);
1247 reg_equiv_constant = 0;
1248 if (reg_equiv_memory_loc)
1249 free (reg_equiv_memory_loc);
1250 reg_equiv_memory_loc = 0;
1252 if (real_known_ptr)
1253 free (real_known_ptr);
1254 if (real_at_ptr)
1255 free (real_at_ptr);
1257 free (reg_equiv_mem);
1258 free (reg_equiv_init);
1259 free (reg_equiv_address);
1260 free (reg_max_ref_width);
1261 free (reg_old_renumber);
1262 free (pseudo_previous_regs);
1263 free (pseudo_forbidden_regs);
1265 CLEAR_HARD_REG_SET (used_spill_regs);
1266 for (i = 0; i < n_spills; i++)
1267 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1269 /* Free all the insn_chain structures at once. */
1270 obstack_free (&reload_obstack, reload_startobj);
1271 unused_insn_chains = 0;
1273 return failure;
1276 /* Yet another special case. Unfortunately, reg-stack forces people to
1277 write incorrect clobbers in asm statements. These clobbers must not
1278 cause the register to appear in bad_spill_regs, otherwise we'll call
1279 fatal_insn later. We clear the corresponding regnos in the live
1280 register sets to avoid this.
1281 The whole thing is rather sick, I'm afraid. */
1283 static void
1284 maybe_fix_stack_asms ()
1286 #ifdef STACK_REGS
1287 const char *constraints[MAX_RECOG_OPERANDS];
1288 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1289 struct insn_chain *chain;
1291 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1293 int i, noperands;
1294 HARD_REG_SET clobbered, allowed;
1295 rtx pat;
1297 if (! INSN_P (chain->insn)
1298 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1299 continue;
1300 pat = PATTERN (chain->insn);
1301 if (GET_CODE (pat) != PARALLEL)
1302 continue;
1304 CLEAR_HARD_REG_SET (clobbered);
1305 CLEAR_HARD_REG_SET (allowed);
1307 /* First, make a mask of all stack regs that are clobbered. */
1308 for (i = 0; i < XVECLEN (pat, 0); i++)
1310 rtx t = XVECEXP (pat, 0, i);
1311 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1312 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1315 /* Get the operand values and constraints out of the insn. */
1316 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1317 constraints, operand_mode);
1319 /* For every operand, see what registers are allowed. */
1320 for (i = 0; i < noperands; i++)
1322 const char *p = constraints[i];
1323 /* For every alternative, we compute the class of registers allowed
1324 for reloading in CLS, and merge its contents into the reg set
1325 ALLOWED. */
1326 int cls = (int) NO_REGS;
1328 for (;;)
1330 char c = *p++;
1332 if (c == '\0' || c == ',' || c == '#')
1334 /* End of one alternative - mark the regs in the current
1335 class, and reset the class. */
1336 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1337 cls = NO_REGS;
1338 if (c == '#')
1339 do {
1340 c = *p++;
1341 } while (c != '\0' && c != ',');
1342 if (c == '\0')
1343 break;
1344 continue;
1347 switch (c)
1349 case '=': case '+': case '*': case '%': case '?': case '!':
1350 case '0': case '1': case '2': case '3': case '4': case 'm':
1351 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1352 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1353 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1354 case 'P':
1355 break;
1357 case 'p':
1358 cls = (int) reg_class_subunion[cls][(int) BASE_REG_CLASS];
1359 break;
1361 case 'g':
1362 case 'r':
1363 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1364 break;
1366 default:
1367 cls = (int) reg_class_subunion[cls][(int) REG_CLASS_FROM_LETTER (c)];
1372 /* Those of the registers which are clobbered, but allowed by the
1373 constraints, must be usable as reload registers. So clear them
1374 out of the life information. */
1375 AND_HARD_REG_SET (allowed, clobbered);
1376 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1377 if (TEST_HARD_REG_BIT (allowed, i))
1379 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1380 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1384 #endif
1387 /* Copy the global variables n_reloads and rld into the corresponding elts
1388 of CHAIN. */
1389 static void
1390 copy_reloads (chain)
1391 struct insn_chain *chain;
1393 chain->n_reloads = n_reloads;
1394 chain->rld
1395 = (struct reload *) obstack_alloc (&reload_obstack,
1396 n_reloads * sizeof (struct reload));
1397 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1398 reload_insn_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
1401 /* Walk the chain of insns, and determine for each whether it needs reloads
1402 and/or eliminations. Build the corresponding insns_need_reload list, and
1403 set something_needs_elimination as appropriate. */
1404 static void
1405 calculate_needs_all_insns (global)
1406 int global;
1408 struct insn_chain **pprev_reload = &insns_need_reload;
1409 struct insn_chain *chain, *next = 0;
1411 something_needs_elimination = 0;
1413 reload_insn_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
1414 for (chain = reload_insn_chain; chain != 0; chain = next)
1416 rtx insn = chain->insn;
1418 next = chain->next;
1420 /* Clear out the shortcuts. */
1421 chain->n_reloads = 0;
1422 chain->need_elim = 0;
1423 chain->need_reload = 0;
1424 chain->need_operand_change = 0;
1426 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1427 include REG_LABEL), we need to see what effects this has on the
1428 known offsets at labels. */
1430 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
1431 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1432 set_label_offsets (insn, insn, 0);
1434 if (INSN_P (insn))
1436 rtx old_body = PATTERN (insn);
1437 int old_code = INSN_CODE (insn);
1438 rtx old_notes = REG_NOTES (insn);
1439 int did_elimination = 0;
1440 int operands_changed = 0;
1441 rtx set = single_set (insn);
1443 /* Skip insns that only set an equivalence. */
1444 if (set && GET_CODE (SET_DEST (set)) == REG
1445 && reg_renumber[REGNO (SET_DEST (set))] < 0
1446 && reg_equiv_constant[REGNO (SET_DEST (set))])
1447 continue;
1449 /* If needed, eliminate any eliminable registers. */
1450 if (num_eliminable || num_eliminable_invariants)
1451 did_elimination = eliminate_regs_in_insn (insn, 0);
1453 /* Analyze the instruction. */
1454 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1455 global, spill_reg_order);
1457 /* If a no-op set needs more than one reload, this is likely
1458 to be something that needs input address reloads. We
1459 can't get rid of this cleanly later, and it is of no use
1460 anyway, so discard it now.
1461 We only do this when expensive_optimizations is enabled,
1462 since this complements reload inheritance / output
1463 reload deletion, and it can make debugging harder. */
1464 if (flag_expensive_optimizations && n_reloads > 1)
1466 rtx set = single_set (insn);
1467 if (set
1468 && SET_SRC (set) == SET_DEST (set)
1469 && GET_CODE (SET_SRC (set)) == REG
1470 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1472 PUT_CODE (insn, NOTE);
1473 NOTE_SOURCE_FILE (insn) = 0;
1474 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1475 /* Delete it from the reload chain */
1476 if (chain->prev)
1477 chain->prev->next = next;
1478 else
1479 reload_insn_chain = next;
1480 if (next)
1481 next->prev = chain->prev;
1482 chain->next = unused_insn_chains;
1483 unused_insn_chains = chain;
1484 continue;
1487 if (num_eliminable)
1488 update_eliminable_offsets ();
1490 /* Remember for later shortcuts which insns had any reloads or
1491 register eliminations. */
1492 chain->need_elim = did_elimination;
1493 chain->need_reload = n_reloads > 0;
1494 chain->need_operand_change = operands_changed;
1496 /* Discard any register replacements done. */
1497 if (did_elimination)
1499 obstack_free (&reload_obstack, reload_insn_firstobj);
1500 PATTERN (insn) = old_body;
1501 INSN_CODE (insn) = old_code;
1502 REG_NOTES (insn) = old_notes;
1503 something_needs_elimination = 1;
1506 something_needs_operands_changed |= operands_changed;
1508 if (n_reloads != 0)
1510 copy_reloads (chain);
1511 *pprev_reload = chain;
1512 pprev_reload = &chain->next_need_reload;
1516 *pprev_reload = 0;
1519 /* Comparison function for qsort to decide which of two reloads
1520 should be handled first. *P1 and *P2 are the reload numbers. */
1522 static int
1523 reload_reg_class_lower (r1p, r2p)
1524 const PTR r1p;
1525 const PTR r2p;
1527 register int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1528 register int t;
1530 /* Consider required reloads before optional ones. */
1531 t = rld[r1].optional - rld[r2].optional;
1532 if (t != 0)
1533 return t;
1535 /* Count all solitary classes before non-solitary ones. */
1536 t = ((reg_class_size[(int) rld[r2].class] == 1)
1537 - (reg_class_size[(int) rld[r1].class] == 1));
1538 if (t != 0)
1539 return t;
1541 /* Aside from solitaires, consider all multi-reg groups first. */
1542 t = rld[r2].nregs - rld[r1].nregs;
1543 if (t != 0)
1544 return t;
1546 /* Consider reloads in order of increasing reg-class number. */
1547 t = (int) rld[r1].class - (int) rld[r2].class;
1548 if (t != 0)
1549 return t;
1551 /* If reloads are equally urgent, sort by reload number,
1552 so that the results of qsort leave nothing to chance. */
1553 return r1 - r2;
1556 /* The cost of spilling each hard reg. */
1557 static int spill_cost[FIRST_PSEUDO_REGISTER];
1559 /* When spilling multiple hard registers, we use SPILL_COST for the first
1560 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1561 only the first hard reg for a multi-reg pseudo. */
1562 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1564 /* Update the spill cost arrays, considering that pseudo REG is live. */
1566 static void
1567 count_pseudo (reg)
1568 int reg;
1570 int n_refs = REG_N_REFS (reg);
1571 int r = reg_renumber[reg];
1572 int nregs;
1574 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1575 || REGNO_REG_SET_P (&spilled_pseudos, reg))
1576 return;
1578 SET_REGNO_REG_SET (&pseudos_counted, reg);
1580 if (r < 0)
1581 abort ();
1583 spill_add_cost[r] += n_refs;
1585 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
1586 while (nregs-- > 0)
1587 spill_cost[r + nregs] += n_refs;
1590 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1591 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1593 static void
1594 order_regs_for_reload (chain)
1595 struct insn_chain *chain;
1597 int i;
1598 HARD_REG_SET used_by_pseudos;
1599 HARD_REG_SET used_by_pseudos2;
1601 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1603 memset (spill_cost, 0, sizeof spill_cost);
1604 memset (spill_add_cost, 0, sizeof spill_add_cost);
1606 /* Count number of uses of each hard reg by pseudo regs allocated to it
1607 and then order them by decreasing use. First exclude hard registers
1608 that are live in or across this insn. */
1610 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1611 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1612 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1613 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1615 /* Now find out which pseudos are allocated to it, and update
1616 hard_reg_n_uses. */
1617 CLEAR_REG_SET (&pseudos_counted);
1619 EXECUTE_IF_SET_IN_REG_SET
1620 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i,
1622 count_pseudo (i);
1624 EXECUTE_IF_SET_IN_REG_SET
1625 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i,
1627 count_pseudo (i);
1629 CLEAR_REG_SET (&pseudos_counted);
1632 /* Vector of reload-numbers showing the order in which the reloads should
1633 be processed. */
1634 static short reload_order[MAX_RELOADS];
1636 /* This is used to keep track of the spill regs used in one insn. */
1637 static HARD_REG_SET used_spill_regs_local;
1639 /* We decided to spill hard register SPILLED, which has a size of
1640 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1641 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1642 update SPILL_COST/SPILL_ADD_COST. */
1644 static void
1645 count_spilled_pseudo (spilled, spilled_nregs, reg)
1646 int spilled, spilled_nregs, reg;
1648 int r = reg_renumber[reg];
1649 int nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
1651 if (REGNO_REG_SET_P (&spilled_pseudos, reg)
1652 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1653 return;
1655 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1657 spill_add_cost[r] -= REG_N_REFS (reg);
1658 while (nregs-- > 0)
1659 spill_cost[r + nregs] -= REG_N_REFS (reg);
1662 /* Find reload register to use for reload number ORDER. */
1664 static int
1665 find_reg (chain, order)
1666 struct insn_chain *chain;
1667 int order;
1669 int rnum = reload_order[order];
1670 struct reload *rl = rld + rnum;
1671 int best_cost = INT_MAX;
1672 int best_reg = -1;
1673 unsigned int i, j;
1674 int k;
1675 HARD_REG_SET not_usable;
1676 HARD_REG_SET used_by_other_reload;
1678 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1679 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1680 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->class]);
1682 CLEAR_HARD_REG_SET (used_by_other_reload);
1683 for (k = 0; k < order; k++)
1685 int other = reload_order[k];
1687 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1688 for (j = 0; j < rld[other].nregs; j++)
1689 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1692 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1694 unsigned int regno = i;
1696 if (! TEST_HARD_REG_BIT (not_usable, regno)
1697 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1698 && HARD_REGNO_MODE_OK (regno, rl->mode))
1700 int this_cost = spill_cost[regno];
1701 int ok = 1;
1702 unsigned int this_nregs = HARD_REGNO_NREGS (regno, rl->mode);
1704 for (j = 1; j < this_nregs; j++)
1706 this_cost += spill_add_cost[regno + j];
1707 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1708 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1709 ok = 0;
1711 if (! ok)
1712 continue;
1713 if (rl->in && GET_CODE (rl->in) == REG && REGNO (rl->in) == regno)
1714 this_cost--;
1715 if (rl->out && GET_CODE (rl->out) == REG && REGNO (rl->out) == regno)
1716 this_cost--;
1717 if (this_cost < best_cost
1718 /* Among registers with equal cost, prefer caller-saved ones, or
1719 use REG_ALLOC_ORDER if it is defined. */
1720 || (this_cost == best_cost
1721 #ifdef REG_ALLOC_ORDER
1722 && (inv_reg_alloc_order[regno]
1723 < inv_reg_alloc_order[best_reg])
1724 #else
1725 && call_used_regs[regno]
1726 && ! call_used_regs[best_reg]
1727 #endif
1730 best_reg = regno;
1731 best_cost = this_cost;
1735 if (best_reg == -1)
1736 return 0;
1738 if (rtl_dump_file)
1739 fprintf (rtl_dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
1741 rl->nregs = HARD_REGNO_NREGS (best_reg, rl->mode);
1742 rl->regno = best_reg;
1744 EXECUTE_IF_SET_IN_REG_SET
1745 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j,
1747 count_spilled_pseudo (best_reg, rl->nregs, j);
1750 EXECUTE_IF_SET_IN_REG_SET
1751 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j,
1753 count_spilled_pseudo (best_reg, rl->nregs, j);
1756 for (i = 0; i < rl->nregs; i++)
1758 if (spill_cost[best_reg + i] != 0
1759 || spill_add_cost[best_reg + i] != 0)
1760 abort ();
1761 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1763 return 1;
1766 /* Find more reload regs to satisfy the remaining need of an insn, which
1767 is given by CHAIN.
1768 Do it by ascending class number, since otherwise a reg
1769 might be spilled for a big class and might fail to count
1770 for a smaller class even though it belongs to that class. */
1772 static void
1773 find_reload_regs (chain)
1774 struct insn_chain *chain;
1776 int i;
1778 /* In order to be certain of getting the registers we need,
1779 we must sort the reloads into order of increasing register class.
1780 Then our grabbing of reload registers will parallel the process
1781 that provided the reload registers. */
1782 for (i = 0; i < chain->n_reloads; i++)
1784 /* Show whether this reload already has a hard reg. */
1785 if (chain->rld[i].reg_rtx)
1787 int regno = REGNO (chain->rld[i].reg_rtx);
1788 chain->rld[i].regno = regno;
1789 chain->rld[i].nregs
1790 = HARD_REGNO_NREGS (regno, GET_MODE (chain->rld[i].reg_rtx));
1792 else
1793 chain->rld[i].regno = -1;
1794 reload_order[i] = i;
1797 n_reloads = chain->n_reloads;
1798 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
1800 CLEAR_HARD_REG_SET (used_spill_regs_local);
1802 if (rtl_dump_file)
1803 fprintf (rtl_dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1805 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
1807 /* Compute the order of preference for hard registers to spill. */
1809 order_regs_for_reload (chain);
1811 for (i = 0; i < n_reloads; i++)
1813 int r = reload_order[i];
1815 /* Ignore reloads that got marked inoperative. */
1816 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
1817 && ! rld[r].optional
1818 && rld[r].regno == -1)
1819 if (! find_reg (chain, i))
1821 spill_failure (chain->insn, rld[r].class);
1822 failure = 1;
1823 return;
1827 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
1828 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
1830 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1833 static void
1834 select_reload_regs ()
1836 struct insn_chain *chain;
1838 /* Try to satisfy the needs for each insn. */
1839 for (chain = insns_need_reload; chain != 0;
1840 chain = chain->next_need_reload)
1841 find_reload_regs (chain);
1844 /* Delete all insns that were inserted by emit_caller_save_insns during
1845 this iteration. */
1846 static void
1847 delete_caller_save_insns ()
1849 struct insn_chain *c = reload_insn_chain;
1851 while (c != 0)
1853 while (c != 0 && c->is_caller_save_insn)
1855 struct insn_chain *next = c->next;
1856 rtx insn = c->insn;
1858 if (insn == BLOCK_HEAD (c->block))
1859 BLOCK_HEAD (c->block) = NEXT_INSN (insn);
1860 if (insn == BLOCK_END (c->block))
1861 BLOCK_END (c->block) = PREV_INSN (insn);
1862 if (c == reload_insn_chain)
1863 reload_insn_chain = next;
1865 if (NEXT_INSN (insn) != 0)
1866 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
1867 if (PREV_INSN (insn) != 0)
1868 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
1870 if (next)
1871 next->prev = c->prev;
1872 if (c->prev)
1873 c->prev->next = next;
1874 c->next = unused_insn_chains;
1875 unused_insn_chains = c;
1876 c = next;
1878 if (c != 0)
1879 c = c->next;
1883 /* Handle the failure to find a register to spill.
1884 INSN should be one of the insns which needed this particular spill reg. */
1886 static void
1887 spill_failure (insn, class)
1888 rtx insn;
1889 enum reg_class class;
1891 static const char *const reg_class_names[] = REG_CLASS_NAMES;
1892 if (asm_noperands (PATTERN (insn)) >= 0)
1893 error_for_asm (insn, "Can't find a register in class `%s' while reloading `asm'.",
1894 reg_class_names[class]);
1895 else
1897 error ("Unable to find a register to spill in class `%s'.",
1898 reg_class_names[class]);
1899 fatal_insn ("This is the insn:", insn);
1903 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
1904 data that is dead in INSN. */
1906 static void
1907 delete_dead_insn (insn)
1908 rtx insn;
1910 rtx prev = prev_real_insn (insn);
1911 rtx prev_dest;
1913 /* If the previous insn sets a register that dies in our insn, delete it
1914 too. */
1915 if (prev && GET_CODE (PATTERN (prev)) == SET
1916 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
1917 && reg_mentioned_p (prev_dest, PATTERN (insn))
1918 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
1919 && ! side_effects_p (SET_SRC (PATTERN (prev))))
1920 delete_dead_insn (prev);
1922 PUT_CODE (insn, NOTE);
1923 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1924 NOTE_SOURCE_FILE (insn) = 0;
1927 /* Modify the home of pseudo-reg I.
1928 The new home is present in reg_renumber[I].
1930 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
1931 or it may be -1, meaning there is none or it is not relevant.
1932 This is used so that all pseudos spilled from a given hard reg
1933 can share one stack slot. */
1935 static void
1936 alter_reg (i, from_reg)
1937 register int i;
1938 int from_reg;
1940 /* When outputting an inline function, this can happen
1941 for a reg that isn't actually used. */
1942 if (regno_reg_rtx[i] == 0)
1943 return;
1945 /* If the reg got changed to a MEM at rtl-generation time,
1946 ignore it. */
1947 if (GET_CODE (regno_reg_rtx[i]) != REG)
1948 return;
1950 /* Modify the reg-rtx to contain the new hard reg
1951 number or else to contain its pseudo reg number. */
1952 REGNO (regno_reg_rtx[i])
1953 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
1955 /* If we have a pseudo that is needed but has no hard reg or equivalent,
1956 allocate a stack slot for it. */
1958 if (reg_renumber[i] < 0
1959 && REG_N_REFS (i) > 0
1960 && reg_equiv_constant[i] == 0
1961 && reg_equiv_memory_loc[i] == 0)
1963 register rtx x;
1964 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
1965 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
1966 int adjust = 0;
1968 /* Each pseudo reg has an inherent size which comes from its own mode,
1969 and a total size which provides room for paradoxical subregs
1970 which refer to the pseudo reg in wider modes.
1972 We can use a slot already allocated if it provides both
1973 enough inherent space and enough total space.
1974 Otherwise, we allocate a new slot, making sure that it has no less
1975 inherent space, and no less total space, then the previous slot. */
1976 if (from_reg == -1)
1978 /* No known place to spill from => no slot to reuse. */
1979 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
1980 inherent_size == total_size ? 0 : -1);
1981 if (BYTES_BIG_ENDIAN)
1982 /* Cancel the big-endian correction done in assign_stack_local.
1983 Get the address of the beginning of the slot.
1984 This is so we can do a big-endian correction unconditionally
1985 below. */
1986 adjust = inherent_size - total_size;
1988 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
1990 /* Nothing can alias this slot except this pseudo. */
1991 MEM_ALIAS_SET (x) = new_alias_set ();
1994 /* Reuse a stack slot if possible. */
1995 else if (spill_stack_slot[from_reg] != 0
1996 && spill_stack_slot_width[from_reg] >= total_size
1997 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
1998 >= inherent_size))
1999 x = spill_stack_slot[from_reg];
2001 /* Allocate a bigger slot. */
2002 else
2004 /* Compute maximum size needed, both for inherent size
2005 and for total size. */
2006 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2007 rtx stack_slot;
2009 if (spill_stack_slot[from_reg])
2011 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2012 > inherent_size)
2013 mode = GET_MODE (spill_stack_slot[from_reg]);
2014 if (spill_stack_slot_width[from_reg] > total_size)
2015 total_size = spill_stack_slot_width[from_reg];
2018 /* Make a slot with that size. */
2019 x = assign_stack_local (mode, total_size,
2020 inherent_size == total_size ? 0 : -1);
2021 stack_slot = x;
2023 /* All pseudos mapped to this slot can alias each other. */
2024 if (spill_stack_slot[from_reg])
2025 MEM_ALIAS_SET (x) = MEM_ALIAS_SET (spill_stack_slot[from_reg]);
2026 else
2027 MEM_ALIAS_SET (x) = new_alias_set ();
2029 if (BYTES_BIG_ENDIAN)
2031 /* Cancel the big-endian correction done in assign_stack_local.
2032 Get the address of the beginning of the slot.
2033 This is so we can do a big-endian correction unconditionally
2034 below. */
2035 adjust = GET_MODE_SIZE (mode) - total_size;
2036 if (adjust)
2037 stack_slot = gen_rtx_MEM (mode_for_size (total_size
2038 * BITS_PER_UNIT,
2039 MODE_INT, 1),
2040 plus_constant (XEXP (x, 0), adjust));
2043 spill_stack_slot[from_reg] = stack_slot;
2044 spill_stack_slot_width[from_reg] = total_size;
2047 /* On a big endian machine, the "address" of the slot
2048 is the address of the low part that fits its inherent mode. */
2049 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2050 adjust += (total_size - inherent_size);
2052 /* If we have any adjustment to make, or if the stack slot is the
2053 wrong mode, make a new stack slot. */
2054 if (adjust != 0 || GET_MODE (x) != GET_MODE (regno_reg_rtx[i]))
2056 rtx new = gen_rtx_MEM (GET_MODE (regno_reg_rtx[i]),
2057 plus_constant (XEXP (x, 0), adjust));
2059 MEM_COPY_ATTRIBUTES (new, x);
2060 x = new;
2063 /* Save the stack slot for later. */
2064 reg_equiv_memory_loc[i] = x;
2068 /* Mark the slots in regs_ever_live for the hard regs
2069 used by pseudo-reg number REGNO. */
2071 void
2072 mark_home_live (regno)
2073 int regno;
2075 register int i, lim;
2077 i = reg_renumber[regno];
2078 if (i < 0)
2079 return;
2080 lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2081 while (i < lim)
2082 regs_ever_live[i++] = 1;
2085 /* This function handles the tracking of elimination offsets around branches.
2087 X is a piece of RTL being scanned.
2089 INSN is the insn that it came from, if any.
2091 INITIAL_P is non-zero if we are to set the offset to be the initial
2092 offset and zero if we are setting the offset of the label to be the
2093 current offset. */
2095 static void
2096 set_label_offsets (x, insn, initial_p)
2097 rtx x;
2098 rtx insn;
2099 int initial_p;
2101 enum rtx_code code = GET_CODE (x);
2102 rtx tem;
2103 unsigned int i;
2104 struct elim_table *p;
2106 switch (code)
2108 case LABEL_REF:
2109 if (LABEL_REF_NONLOCAL_P (x))
2110 return;
2112 x = XEXP (x, 0);
2114 /* ... fall through ... */
2116 case CODE_LABEL:
2117 /* If we know nothing about this label, set the desired offsets. Note
2118 that this sets the offset at a label to be the offset before a label
2119 if we don't know anything about the label. This is not correct for
2120 the label after a BARRIER, but is the best guess we can make. If
2121 we guessed wrong, we will suppress an elimination that might have
2122 been possible had we been able to guess correctly. */
2124 if (! offsets_known_at[CODE_LABEL_NUMBER (x)])
2126 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2127 offsets_at[CODE_LABEL_NUMBER (x)][i]
2128 = (initial_p ? reg_eliminate[i].initial_offset
2129 : reg_eliminate[i].offset);
2130 offsets_known_at[CODE_LABEL_NUMBER (x)] = 1;
2133 /* Otherwise, if this is the definition of a label and it is
2134 preceded by a BARRIER, set our offsets to the known offset of
2135 that label. */
2137 else if (x == insn
2138 && (tem = prev_nonnote_insn (insn)) != 0
2139 && GET_CODE (tem) == BARRIER)
2140 set_offsets_for_label (insn);
2141 else
2142 /* If neither of the above cases is true, compare each offset
2143 with those previously recorded and suppress any eliminations
2144 where the offsets disagree. */
2146 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2147 if (offsets_at[CODE_LABEL_NUMBER (x)][i]
2148 != (initial_p ? reg_eliminate[i].initial_offset
2149 : reg_eliminate[i].offset))
2150 reg_eliminate[i].can_eliminate = 0;
2152 return;
2154 case JUMP_INSN:
2155 set_label_offsets (PATTERN (insn), insn, initial_p);
2157 /* ... fall through ... */
2159 case INSN:
2160 case CALL_INSN:
2161 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2162 and hence must have all eliminations at their initial offsets. */
2163 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2164 if (REG_NOTE_KIND (tem) == REG_LABEL)
2165 set_label_offsets (XEXP (tem, 0), insn, 1);
2166 return;
2168 case PARALLEL:
2169 case ADDR_VEC:
2170 case ADDR_DIFF_VEC:
2171 /* Each of the labels in the parallel or address vector must be
2172 at their initial offsets. We want the first field for PARALLEL
2173 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2175 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2176 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2177 insn, initial_p);
2178 return;
2180 case SET:
2181 /* We only care about setting PC. If the source is not RETURN,
2182 IF_THEN_ELSE, or a label, disable any eliminations not at
2183 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2184 isn't one of those possibilities. For branches to a label,
2185 call ourselves recursively.
2187 Note that this can disable elimination unnecessarily when we have
2188 a non-local goto since it will look like a non-constant jump to
2189 someplace in the current function. This isn't a significant
2190 problem since such jumps will normally be when all elimination
2191 pairs are back to their initial offsets. */
2193 if (SET_DEST (x) != pc_rtx)
2194 return;
2196 switch (GET_CODE (SET_SRC (x)))
2198 case PC:
2199 case RETURN:
2200 return;
2202 case LABEL_REF:
2203 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2204 return;
2206 case IF_THEN_ELSE:
2207 tem = XEXP (SET_SRC (x), 1);
2208 if (GET_CODE (tem) == LABEL_REF)
2209 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2210 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2211 break;
2213 tem = XEXP (SET_SRC (x), 2);
2214 if (GET_CODE (tem) == LABEL_REF)
2215 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2216 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2217 break;
2218 return;
2220 default:
2221 break;
2224 /* If we reach here, all eliminations must be at their initial
2225 offset because we are doing a jump to a variable address. */
2226 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2227 if (p->offset != p->initial_offset)
2228 p->can_eliminate = 0;
2229 break;
2231 default:
2232 break;
2236 /* Scan X and replace any eliminable registers (such as fp) with a
2237 replacement (such as sp), plus an offset.
2239 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2240 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2241 MEM, we are allowed to replace a sum of a register and the constant zero
2242 with the register, which we cannot do outside a MEM. In addition, we need
2243 to record the fact that a register is referenced outside a MEM.
2245 If INSN is an insn, it is the insn containing X. If we replace a REG
2246 in a SET_DEST with an equivalent MEM and INSN is non-zero, write a
2247 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2248 the REG is being modified.
2250 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2251 That's used when we eliminate in expressions stored in notes.
2252 This means, do not set ref_outside_mem even if the reference
2253 is outside of MEMs.
2255 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2256 replacements done assuming all offsets are at their initial values. If
2257 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2258 encounter, return the actual location so that find_reloads will do
2259 the proper thing. */
2262 eliminate_regs (x, mem_mode, insn)
2263 rtx x;
2264 enum machine_mode mem_mode;
2265 rtx insn;
2267 enum rtx_code code = GET_CODE (x);
2268 struct elim_table *ep;
2269 int regno;
2270 rtx new;
2271 int i, j;
2272 const char *fmt;
2273 int copied = 0;
2275 if (! current_function_decl)
2276 return x;
2278 switch (code)
2280 case CONST_INT:
2281 case CONST_DOUBLE:
2282 case CONST:
2283 case SYMBOL_REF:
2284 case CODE_LABEL:
2285 case PC:
2286 case CC0:
2287 case ASM_INPUT:
2288 case ADDR_VEC:
2289 case ADDR_DIFF_VEC:
2290 case RETURN:
2291 return x;
2293 case ADDRESSOF:
2294 /* This is only for the benefit of the debugging backends, which call
2295 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2296 removed after CSE. */
2297 new = eliminate_regs (XEXP (x, 0), 0, insn);
2298 if (GET_CODE (new) == MEM)
2299 return XEXP (new, 0);
2300 return x;
2302 case REG:
2303 regno = REGNO (x);
2305 /* First handle the case where we encounter a bare register that
2306 is eliminable. Replace it with a PLUS. */
2307 if (regno < FIRST_PSEUDO_REGISTER)
2309 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2310 ep++)
2311 if (ep->from_rtx == x && ep->can_eliminate)
2312 return plus_constant (ep->to_rtx, ep->previous_offset);
2315 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2316 && reg_equiv_constant[regno]
2317 && ! CONSTANT_P (reg_equiv_constant[regno]))
2318 return eliminate_regs (copy_rtx (reg_equiv_constant[regno]),
2319 mem_mode, insn);
2320 return x;
2322 /* You might think handling MINUS in a manner similar to PLUS is a
2323 good idea. It is not. It has been tried multiple times and every
2324 time the change has had to have been reverted.
2326 Other parts of reload know a PLUS is special (gen_reload for example)
2327 and require special code to handle code a reloaded PLUS operand.
2329 Also consider backends where the flags register is clobbered by a
2330 MINUS, but we can emit a PLUS that does not clobber flags (ia32,
2331 lea instruction comes to mind). If we try to reload a MINUS, we
2332 may kill the flags register that was holding a useful value.
2334 So, please before trying to handle MINUS, consider reload as a
2335 whole instead of this little section as well as the backend issues. */
2336 case PLUS:
2337 /* If this is the sum of an eliminable register and a constant, rework
2338 the sum. */
2339 if (GET_CODE (XEXP (x, 0)) == REG
2340 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2341 && CONSTANT_P (XEXP (x, 1)))
2343 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2344 ep++)
2345 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2347 /* The only time we want to replace a PLUS with a REG (this
2348 occurs when the constant operand of the PLUS is the negative
2349 of the offset) is when we are inside a MEM. We won't want
2350 to do so at other times because that would change the
2351 structure of the insn in a way that reload can't handle.
2352 We special-case the commonest situation in
2353 eliminate_regs_in_insn, so just replace a PLUS with a
2354 PLUS here, unless inside a MEM. */
2355 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2356 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2357 return ep->to_rtx;
2358 else
2359 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2360 plus_constant (XEXP (x, 1),
2361 ep->previous_offset));
2364 /* If the register is not eliminable, we are done since the other
2365 operand is a constant. */
2366 return x;
2369 /* If this is part of an address, we want to bring any constant to the
2370 outermost PLUS. We will do this by doing register replacement in
2371 our operands and seeing if a constant shows up in one of them.
2373 Note that there is no risk of modifying the structure of the insn,
2374 since we only get called for its operands, thus we are either
2375 modifying the address inside a MEM, or something like an address
2376 operand of a load-address insn. */
2379 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2380 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2382 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2384 /* If one side is a PLUS and the other side is a pseudo that
2385 didn't get a hard register but has a reg_equiv_constant,
2386 we must replace the constant here since it may no longer
2387 be in the position of any operand. */
2388 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2389 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2390 && reg_renumber[REGNO (new1)] < 0
2391 && reg_equiv_constant != 0
2392 && reg_equiv_constant[REGNO (new1)] != 0)
2393 new1 = reg_equiv_constant[REGNO (new1)];
2394 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2395 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2396 && reg_renumber[REGNO (new0)] < 0
2397 && reg_equiv_constant[REGNO (new0)] != 0)
2398 new0 = reg_equiv_constant[REGNO (new0)];
2400 new = form_sum (new0, new1);
2402 /* As above, if we are not inside a MEM we do not want to
2403 turn a PLUS into something else. We might try to do so here
2404 for an addition of 0 if we aren't optimizing. */
2405 if (! mem_mode && GET_CODE (new) != PLUS)
2406 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2407 else
2408 return new;
2411 return x;
2413 case MULT:
2414 /* If this is the product of an eliminable register and a
2415 constant, apply the distribute law and move the constant out
2416 so that we have (plus (mult ..) ..). This is needed in order
2417 to keep load-address insns valid. This case is pathological.
2418 We ignore the possibility of overflow here. */
2419 if (GET_CODE (XEXP (x, 0)) == REG
2420 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2421 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2422 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2423 ep++)
2424 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2426 if (! mem_mode
2427 /* Refs inside notes don't count for this purpose. */
2428 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2429 || GET_CODE (insn) == INSN_LIST)))
2430 ep->ref_outside_mem = 1;
2432 return
2433 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2434 ep->previous_offset * INTVAL (XEXP (x, 1)));
2437 /* ... fall through ... */
2439 case CALL:
2440 case COMPARE:
2441 /* See comments before PLUS about handling MINUS. */
2442 case MINUS:
2443 case DIV: case UDIV:
2444 case MOD: case UMOD:
2445 case AND: case IOR: case XOR:
2446 case ROTATERT: case ROTATE:
2447 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2448 case NE: case EQ:
2449 case GE: case GT: case GEU: case GTU:
2450 case LE: case LT: case LEU: case LTU:
2452 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2453 rtx new1
2454 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
2456 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2457 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2459 return x;
2461 case EXPR_LIST:
2462 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2463 if (XEXP (x, 0))
2465 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2466 if (new != XEXP (x, 0))
2468 /* If this is a REG_DEAD note, it is not valid anymore.
2469 Using the eliminated version could result in creating a
2470 REG_DEAD note for the stack or frame pointer. */
2471 if (GET_MODE (x) == REG_DEAD)
2472 return (XEXP (x, 1)
2473 ? eliminate_regs (XEXP (x, 1), mem_mode, insn)
2474 : NULL_RTX);
2476 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2480 /* ... fall through ... */
2482 case INSN_LIST:
2483 /* Now do eliminations in the rest of the chain. If this was
2484 an EXPR_LIST, this might result in allocating more memory than is
2485 strictly needed, but it simplifies the code. */
2486 if (XEXP (x, 1))
2488 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2489 if (new != XEXP (x, 1))
2490 return gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2492 return x;
2494 case PRE_INC:
2495 case POST_INC:
2496 case PRE_DEC:
2497 case POST_DEC:
2498 case STRICT_LOW_PART:
2499 case NEG: case NOT:
2500 case SIGN_EXTEND: case ZERO_EXTEND:
2501 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2502 case FLOAT: case FIX:
2503 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2504 case ABS:
2505 case SQRT:
2506 case FFS:
2507 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2508 if (new != XEXP (x, 0))
2509 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2510 return x;
2512 case SUBREG:
2513 /* Similar to above processing, but preserve SUBREG_WORD.
2514 Convert (subreg (mem)) to (mem) if not paradoxical.
2515 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2516 pseudo didn't get a hard reg, we must replace this with the
2517 eliminated version of the memory location because push_reloads
2518 may do the replacement in certain circumstances. */
2519 if (GET_CODE (SUBREG_REG (x)) == REG
2520 && (GET_MODE_SIZE (GET_MODE (x))
2521 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2522 && reg_equiv_memory_loc != 0
2523 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2525 new = SUBREG_REG (x);
2527 else
2528 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
2530 if (new != XEXP (x, 0))
2532 int x_size = GET_MODE_SIZE (GET_MODE (x));
2533 int new_size = GET_MODE_SIZE (GET_MODE (new));
2535 if (GET_CODE (new) == MEM
2536 && ((x_size < new_size
2537 #ifdef WORD_REGISTER_OPERATIONS
2538 /* On these machines, combine can create rtl of the form
2539 (set (subreg:m1 (reg:m2 R) 0) ...)
2540 where m1 < m2, and expects something interesting to
2541 happen to the entire word. Moreover, it will use the
2542 (reg:m2 R) later, expecting all bits to be preserved.
2543 So if the number of words is the same, preserve the
2544 subreg so that push_reloads can see it. */
2545 && ! ((x_size-1)/UNITS_PER_WORD == (new_size-1)/UNITS_PER_WORD)
2546 #endif
2548 || (x_size == new_size))
2551 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
2552 enum machine_mode mode = GET_MODE (x);
2554 if (BYTES_BIG_ENDIAN)
2555 offset += (MIN (UNITS_PER_WORD,
2556 GET_MODE_SIZE (GET_MODE (new)))
2557 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
2559 PUT_MODE (new, mode);
2560 XEXP (new, 0) = plus_constant (XEXP (new, 0), offset);
2561 return new;
2563 else
2564 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_WORD (x));
2567 return x;
2569 case MEM:
2570 /* This is only for the benefit of the debugging backends, which call
2571 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2572 removed after CSE. */
2573 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
2574 return eliminate_regs (XEXP (XEXP (x, 0), 0), 0, insn);
2576 /* Our only special processing is to pass the mode of the MEM to our
2577 recursive call and copy the flags. While we are here, handle this
2578 case more efficiently. */
2579 new = eliminate_regs (XEXP (x, 0), GET_MODE (x), insn);
2580 if (new != XEXP (x, 0))
2582 new = gen_rtx_MEM (GET_MODE (x), new);
2583 MEM_COPY_ATTRIBUTES (new, x);
2584 return new;
2586 else
2587 return x;
2589 case USE:
2590 case CLOBBER:
2591 case ASM_OPERANDS:
2592 case SET:
2593 abort ();
2595 default:
2596 break;
2599 /* Process each of our operands recursively. If any have changed, make a
2600 copy of the rtx. */
2601 fmt = GET_RTX_FORMAT (code);
2602 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2604 if (*fmt == 'e')
2606 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
2607 if (new != XEXP (x, i) && ! copied)
2609 rtx new_x = rtx_alloc (code);
2610 memcpy (new_x, x,
2611 (sizeof (*new_x) - sizeof (new_x->fld)
2612 + sizeof (new_x->fld[0]) * GET_RTX_LENGTH (code)));
2613 x = new_x;
2614 copied = 1;
2616 XEXP (x, i) = new;
2618 else if (*fmt == 'E')
2620 int copied_vec = 0;
2621 for (j = 0; j < XVECLEN (x, i); j++)
2623 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
2624 if (new != XVECEXP (x, i, j) && ! copied_vec)
2626 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2627 XVEC (x, i)->elem);
2628 if (! copied)
2630 rtx new_x = rtx_alloc (code);
2631 memcpy (new_x, x,
2632 (sizeof (*new_x) - sizeof (new_x->fld)
2633 + (sizeof (new_x->fld[0])
2634 * GET_RTX_LENGTH (code))));
2635 x = new_x;
2636 copied = 1;
2638 XVEC (x, i) = new_v;
2639 copied_vec = 1;
2641 XVECEXP (x, i, j) = new;
2646 return x;
2649 /* Scan rtx X for modifications of elimination target registers. Update
2650 the table of eliminables to reflect the changed state. MEM_MODE is
2651 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2653 static void
2654 elimination_effects (x, mem_mode)
2655 rtx x;
2656 enum machine_mode mem_mode;
2659 enum rtx_code code = GET_CODE (x);
2660 struct elim_table *ep;
2661 int regno;
2662 int i, j;
2663 const char *fmt;
2665 switch (code)
2667 case CONST_INT:
2668 case CONST_DOUBLE:
2669 case CONST:
2670 case SYMBOL_REF:
2671 case CODE_LABEL:
2672 case PC:
2673 case CC0:
2674 case ASM_INPUT:
2675 case ADDR_VEC:
2676 case ADDR_DIFF_VEC:
2677 case RETURN:
2678 return;
2680 case ADDRESSOF:
2681 abort ();
2683 case REG:
2684 regno = REGNO (x);
2686 /* First handle the case where we encounter a bare register that
2687 is eliminable. Replace it with a PLUS. */
2688 if (regno < FIRST_PSEUDO_REGISTER)
2690 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2691 ep++)
2692 if (ep->from_rtx == x && ep->can_eliminate)
2694 if (! mem_mode)
2695 ep->ref_outside_mem = 1;
2696 return;
2700 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2701 && reg_equiv_constant[regno]
2702 && ! CONSTANT_P (reg_equiv_constant[regno]))
2703 elimination_effects (reg_equiv_constant[regno], mem_mode);
2704 return;
2706 case PRE_INC:
2707 case POST_INC:
2708 case PRE_DEC:
2709 case POST_DEC:
2710 case POST_MODIFY:
2711 case PRE_MODIFY:
2712 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2713 if (ep->to_rtx == XEXP (x, 0))
2715 int size = GET_MODE_SIZE (mem_mode);
2717 /* If more bytes than MEM_MODE are pushed, account for them. */
2718 #ifdef PUSH_ROUNDING
2719 if (ep->to_rtx == stack_pointer_rtx)
2720 size = PUSH_ROUNDING (size);
2721 #endif
2722 if (code == PRE_DEC || code == POST_DEC)
2723 ep->offset += size;
2724 else if (code == PRE_INC || code == POST_INC)
2725 ep->offset -= size;
2726 else if ((code == PRE_MODIFY || code == POST_MODIFY)
2727 && GET_CODE (XEXP (x, 1)) == PLUS
2728 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
2729 && CONSTANT_P (XEXP (XEXP (x, 1), 1)))
2730 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
2733 /* These two aren't unary operators. */
2734 if (code == POST_MODIFY || code == PRE_MODIFY)
2735 break;
2737 /* Fall through to generic unary operation case. */
2738 case STRICT_LOW_PART:
2739 case NEG: case NOT:
2740 case SIGN_EXTEND: case ZERO_EXTEND:
2741 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2742 case FLOAT: case FIX:
2743 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2744 case ABS:
2745 case SQRT:
2746 case FFS:
2747 elimination_effects (XEXP (x, 0), mem_mode);
2748 return;
2750 case SUBREG:
2751 if (GET_CODE (SUBREG_REG (x)) == REG
2752 && (GET_MODE_SIZE (GET_MODE (x))
2753 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2754 && reg_equiv_memory_loc != 0
2755 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2756 return;
2758 elimination_effects (SUBREG_REG (x), mem_mode);
2759 return;
2761 case USE:
2762 /* If using a register that is the source of an eliminate we still
2763 think can be performed, note it cannot be performed since we don't
2764 know how this register is used. */
2765 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2766 if (ep->from_rtx == XEXP (x, 0))
2767 ep->can_eliminate = 0;
2769 elimination_effects (XEXP (x, 0), mem_mode);
2770 return;
2772 case CLOBBER:
2773 /* If clobbering a register that is the replacement register for an
2774 elimination we still think can be performed, note that it cannot
2775 be performed. Otherwise, we need not be concerned about it. */
2776 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2777 if (ep->to_rtx == XEXP (x, 0))
2778 ep->can_eliminate = 0;
2780 elimination_effects (XEXP (x, 0), mem_mode);
2781 return;
2783 case SET:
2784 /* Check for setting a register that we know about. */
2785 if (GET_CODE (SET_DEST (x)) == REG)
2787 /* See if this is setting the replacement register for an
2788 elimination.
2790 If DEST is the hard frame pointer, we do nothing because we
2791 assume that all assignments to the frame pointer are for
2792 non-local gotos and are being done at a time when they are valid
2793 and do not disturb anything else. Some machines want to
2794 eliminate a fake argument pointer (or even a fake frame pointer)
2795 with either the real frame or the stack pointer. Assignments to
2796 the hard frame pointer must not prevent this elimination. */
2798 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2799 ep++)
2800 if (ep->to_rtx == SET_DEST (x)
2801 && SET_DEST (x) != hard_frame_pointer_rtx)
2803 /* If it is being incremented, adjust the offset. Otherwise,
2804 this elimination can't be done. */
2805 rtx src = SET_SRC (x);
2807 if (GET_CODE (src) == PLUS
2808 && XEXP (src, 0) == SET_DEST (x)
2809 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2810 ep->offset -= INTVAL (XEXP (src, 1));
2811 else
2812 ep->can_eliminate = 0;
2816 elimination_effects (SET_DEST (x), 0);
2817 elimination_effects (SET_SRC (x), 0);
2818 return;
2820 case MEM:
2821 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
2822 abort ();
2824 /* Our only special processing is to pass the mode of the MEM to our
2825 recursive call. */
2826 elimination_effects (XEXP (x, 0), GET_MODE (x));
2827 return;
2829 default:
2830 break;
2833 fmt = GET_RTX_FORMAT (code);
2834 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2836 if (*fmt == 'e')
2837 elimination_effects (XEXP (x, i), mem_mode);
2838 else if (*fmt == 'E')
2839 for (j = 0; j < XVECLEN (x, i); j++)
2840 elimination_effects (XVECEXP (x, i, j), mem_mode);
2844 /* Descend through rtx X and verify that no references to eliminable registers
2845 remain. If any do remain, mark the involved register as not
2846 eliminable. */
2848 static void
2849 check_eliminable_occurrences (x)
2850 rtx x;
2852 const char *fmt;
2853 int i;
2854 enum rtx_code code;
2856 if (x == 0)
2857 return;
2859 code = GET_CODE (x);
2861 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
2863 struct elim_table *ep;
2865 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2866 if (ep->from_rtx == x && ep->can_eliminate)
2867 ep->can_eliminate = 0;
2868 return;
2871 fmt = GET_RTX_FORMAT (code);
2872 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2874 if (*fmt == 'e')
2875 check_eliminable_occurrences (XEXP (x, i));
2876 else if (*fmt == 'E')
2878 int j;
2879 for (j = 0; j < XVECLEN (x, i); j++)
2880 check_eliminable_occurrences (XVECEXP (x, i, j));
2885 /* Scan INSN and eliminate all eliminable registers in it.
2887 If REPLACE is nonzero, do the replacement destructively. Also
2888 delete the insn as dead it if it is setting an eliminable register.
2890 If REPLACE is zero, do all our allocations in reload_obstack.
2892 If no eliminations were done and this insn doesn't require any elimination
2893 processing (these are not identical conditions: it might be updating sp,
2894 but not referencing fp; this needs to be seen during reload_as_needed so
2895 that the offset between fp and sp can be taken into consideration), zero
2896 is returned. Otherwise, 1 is returned. */
2898 static int
2899 eliminate_regs_in_insn (insn, replace)
2900 rtx insn;
2901 int replace;
2903 int icode = recog_memoized (insn);
2904 rtx old_body = PATTERN (insn);
2905 int insn_is_asm = asm_noperands (old_body) >= 0;
2906 rtx old_set = single_set (insn);
2907 rtx new_body;
2908 int val = 0;
2909 int i, any_changes;
2910 rtx substed_operand[MAX_RECOG_OPERANDS];
2911 rtx orig_operand[MAX_RECOG_OPERANDS];
2912 struct elim_table *ep;
2914 if (! insn_is_asm && icode < 0)
2916 if (GET_CODE (PATTERN (insn)) == USE
2917 || GET_CODE (PATTERN (insn)) == CLOBBER
2918 || GET_CODE (PATTERN (insn)) == ADDR_VEC
2919 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2920 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
2921 return 0;
2922 abort ();
2925 if (old_set != 0 && GET_CODE (SET_DEST (old_set)) == REG
2926 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
2928 /* Check for setting an eliminable register. */
2929 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2930 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
2932 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2933 /* If this is setting the frame pointer register to the
2934 hardware frame pointer register and this is an elimination
2935 that will be done (tested above), this insn is really
2936 adjusting the frame pointer downward to compensate for
2937 the adjustment done before a nonlocal goto. */
2938 if (ep->from == FRAME_POINTER_REGNUM
2939 && ep->to == HARD_FRAME_POINTER_REGNUM)
2941 rtx src = SET_SRC (old_set);
2942 int offset = 0, ok = 0;
2943 rtx prev_insn, prev_set;
2945 if (src == ep->to_rtx)
2946 offset = 0, ok = 1;
2947 else if (GET_CODE (src) == PLUS
2948 && GET_CODE (XEXP (src, 0)) == CONST_INT
2949 && XEXP (src, 1) == ep->to_rtx)
2950 offset = INTVAL (XEXP (src, 0)), ok = 1;
2951 else if (GET_CODE (src) == PLUS
2952 && GET_CODE (XEXP (src, 1)) == CONST_INT
2953 && XEXP (src, 0) == ep->to_rtx)
2954 offset = INTVAL (XEXP (src, 1)), ok = 1;
2955 else if ((prev_insn = prev_nonnote_insn (insn)) != 0
2956 && (prev_set = single_set (prev_insn)) != 0
2957 && rtx_equal_p (SET_DEST (prev_set), src))
2959 src = SET_SRC (prev_set);
2960 if (src == ep->to_rtx)
2961 offset = 0, ok = 1;
2962 else if (GET_CODE (src) == PLUS
2963 && GET_CODE (XEXP (src, 0)) == CONST_INT
2964 && XEXP (src, 1) == ep->to_rtx)
2965 offset = INTVAL (XEXP (src, 0)), ok = 1;
2966 else if (GET_CODE (src) == PLUS
2967 && GET_CODE (XEXP (src, 1)) == CONST_INT
2968 && XEXP (src, 0) == ep->to_rtx)
2969 offset = INTVAL (XEXP (src, 1)), ok = 1;
2972 if (ok)
2974 if (replace)
2976 rtx src
2977 = plus_constant (ep->to_rtx, offset - ep->offset);
2979 /* First see if this insn remains valid when we
2980 make the change. If not, keep the INSN_CODE
2981 the same and let reload fit it up. */
2982 validate_change (insn, &SET_SRC (old_set), src, 1);
2983 validate_change (insn, &SET_DEST (old_set),
2984 ep->to_rtx, 1);
2985 if (! apply_change_group ())
2987 SET_SRC (old_set) = src;
2988 SET_DEST (old_set) = ep->to_rtx;
2992 val = 1;
2993 goto done;
2996 #endif
2998 /* In this case this insn isn't serving a useful purpose. We
2999 will delete it in reload_as_needed once we know that this
3000 elimination is, in fact, being done.
3002 If REPLACE isn't set, we can't delete this insn, but needn't
3003 process it since it won't be used unless something changes. */
3004 if (replace)
3006 delete_dead_insn (insn);
3007 return 1;
3009 val = 1;
3010 goto done;
3014 /* We allow one special case which happens to work on all machines we
3015 currently support: a single set with the source being a PLUS of an
3016 eliminable register and a constant. */
3017 if (old_set
3018 && GET_CODE (SET_SRC (old_set)) == PLUS
3019 && GET_CODE (XEXP (SET_SRC (old_set), 0)) == REG
3020 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT
3021 && REGNO (XEXP (SET_SRC (old_set), 0)) < FIRST_PSEUDO_REGISTER)
3023 rtx reg = XEXP (SET_SRC (old_set), 0);
3024 int offset = INTVAL (XEXP (SET_SRC (old_set), 1));
3026 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3027 if (ep->from_rtx == reg && ep->can_eliminate)
3029 offset += ep->offset;
3031 if (offset == 0)
3033 int num_clobbers;
3034 /* We assume here that if we need a PARALLEL with
3035 CLOBBERs for this assignment, we can do with the
3036 MATCH_SCRATCHes that add_clobbers allocates.
3037 There's not much we can do if that doesn't work. */
3038 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3039 SET_DEST (old_set),
3040 ep->to_rtx);
3041 num_clobbers = 0;
3042 INSN_CODE (insn) = recog (PATTERN (insn), insn, &num_clobbers);
3043 if (num_clobbers)
3045 rtvec vec = rtvec_alloc (num_clobbers + 1);
3047 vec->elem[0] = PATTERN (insn);
3048 PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
3049 add_clobbers (PATTERN (insn), INSN_CODE (insn));
3051 if (INSN_CODE (insn) < 0)
3052 abort ();
3054 else
3056 new_body = old_body;
3057 if (! replace)
3059 new_body = copy_insn (old_body);
3060 if (REG_NOTES (insn))
3061 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3063 PATTERN (insn) = new_body;
3064 old_set = single_set (insn);
3066 XEXP (SET_SRC (old_set), 0) = ep->to_rtx;
3067 XEXP (SET_SRC (old_set), 1) = GEN_INT (offset);
3069 val = 1;
3070 /* This can't have an effect on elimination offsets, so skip right
3071 to the end. */
3072 goto done;
3076 /* Determine the effects of this insn on elimination offsets. */
3077 elimination_effects (old_body, 0);
3079 /* Eliminate all eliminable registers occurring in operands that
3080 can be handled by reload. */
3081 extract_insn (insn);
3082 any_changes = 0;
3083 for (i = 0; i < recog_data.n_operands; i++)
3085 orig_operand[i] = recog_data.operand[i];
3086 substed_operand[i] = recog_data.operand[i];
3088 /* For an asm statement, every operand is eliminable. */
3089 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3091 /* Check for setting a register that we know about. */
3092 if (recog_data.operand_type[i] != OP_IN
3093 && GET_CODE (orig_operand[i]) == REG)
3095 /* If we are assigning to a register that can be eliminated, it
3096 must be as part of a PARALLEL, since the code above handles
3097 single SETs. We must indicate that we can no longer
3098 eliminate this reg. */
3099 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3100 ep++)
3101 if (ep->from_rtx == orig_operand[i] && ep->can_eliminate)
3102 ep->can_eliminate = 0;
3105 substed_operand[i] = eliminate_regs (recog_data.operand[i], 0,
3106 replace ? insn : NULL_RTX);
3107 if (substed_operand[i] != orig_operand[i])
3108 val = any_changes = 1;
3109 /* Terminate the search in check_eliminable_occurrences at
3110 this point. */
3111 *recog_data.operand_loc[i] = 0;
3113 /* If an output operand changed from a REG to a MEM and INSN is an
3114 insn, write a CLOBBER insn. */
3115 if (recog_data.operand_type[i] != OP_IN
3116 && GET_CODE (orig_operand[i]) == REG
3117 && GET_CODE (substed_operand[i]) == MEM
3118 && replace)
3119 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3120 insn);
3124 for (i = 0; i < recog_data.n_dups; i++)
3125 *recog_data.dup_loc[i]
3126 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3128 /* If any eliminable remain, they aren't eliminable anymore. */
3129 check_eliminable_occurrences (old_body);
3131 /* Substitute the operands; the new values are in the substed_operand
3132 array. */
3133 for (i = 0; i < recog_data.n_operands; i++)
3134 *recog_data.operand_loc[i] = substed_operand[i];
3135 for (i = 0; i < recog_data.n_dups; i++)
3136 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3138 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3139 re-recognize the insn. We do this in case we had a simple addition
3140 but now can do this as a load-address. This saves an insn in this
3141 common case.
3142 If re-recognition fails, the old insn code number will still be used,
3143 and some register operands may have changed into PLUS expressions.
3144 These will be handled by find_reloads by loading them into a register
3145 again. */
3147 if (val)
3149 /* If we aren't replacing things permanently and we changed something,
3150 make another copy to ensure that all the RTL is new. Otherwise
3151 things can go wrong if find_reload swaps commutative operands
3152 and one is inside RTL that has been copied while the other is not. */
3153 new_body = old_body;
3154 if (! replace)
3156 new_body = copy_insn (old_body);
3157 if (REG_NOTES (insn))
3158 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3160 PATTERN (insn) = new_body;
3162 /* If we had a move insn but now we don't, rerecognize it. This will
3163 cause spurious re-recognition if the old move had a PARALLEL since
3164 the new one still will, but we can't call single_set without
3165 having put NEW_BODY into the insn and the re-recognition won't
3166 hurt in this rare case. */
3167 /* ??? Why this huge if statement - why don't we just rerecognize the
3168 thing always? */
3169 if (! insn_is_asm
3170 && old_set != 0
3171 && ((GET_CODE (SET_SRC (old_set)) == REG
3172 && (GET_CODE (new_body) != SET
3173 || GET_CODE (SET_SRC (new_body)) != REG))
3174 /* If this was a load from or store to memory, compare
3175 the MEM in recog_data.operand to the one in the insn.
3176 If they are not equal, then rerecognize the insn. */
3177 || (old_set != 0
3178 && ((GET_CODE (SET_SRC (old_set)) == MEM
3179 && SET_SRC (old_set) != recog_data.operand[1])
3180 || (GET_CODE (SET_DEST (old_set)) == MEM
3181 && SET_DEST (old_set) != recog_data.operand[0])))
3182 /* If this was an add insn before, rerecognize. */
3183 || GET_CODE (SET_SRC (old_set)) == PLUS))
3185 int new_icode = recog (PATTERN (insn), insn, 0);
3186 if (new_icode < 0)
3187 INSN_CODE (insn) = icode;
3191 /* Restore the old body. If there were any changes to it, we made a copy
3192 of it while the changes were still in place, so we'll correctly return
3193 a modified insn below. */
3194 if (! replace)
3196 /* Restore the old body. */
3197 for (i = 0; i < recog_data.n_operands; i++)
3198 *recog_data.operand_loc[i] = orig_operand[i];
3199 for (i = 0; i < recog_data.n_dups; i++)
3200 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3203 /* Update all elimination pairs to reflect the status after the current
3204 insn. The changes we make were determined by the earlier call to
3205 elimination_effects.
3207 We also detect a cases where register elimination cannot be done,
3208 namely, if a register would be both changed and referenced outside a MEM
3209 in the resulting insn since such an insn is often undefined and, even if
3210 not, we cannot know what meaning will be given to it. Note that it is
3211 valid to have a register used in an address in an insn that changes it
3212 (presumably with a pre- or post-increment or decrement).
3214 If anything changes, return nonzero. */
3216 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3218 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3219 ep->can_eliminate = 0;
3221 ep->ref_outside_mem = 0;
3223 if (ep->previous_offset != ep->offset)
3224 val = 1;
3227 done:
3228 /* If we changed something, perform elimination in REG_NOTES. This is
3229 needed even when REPLACE is zero because a REG_DEAD note might refer
3230 to a register that we eliminate and could cause a different number
3231 of spill registers to be needed in the final reload pass than in
3232 the pre-passes. */
3233 if (val && REG_NOTES (insn) != 0)
3234 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
3236 return val;
3239 /* Loop through all elimination pairs.
3240 Recalculate the number not at initial offset.
3242 Compute the maximum offset (minimum offset if the stack does not
3243 grow downward) for each elimination pair. */
3245 static void
3246 update_eliminable_offsets ()
3248 struct elim_table *ep;
3250 num_not_at_initial_offset = 0;
3251 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3253 ep->previous_offset = ep->offset;
3254 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3255 num_not_at_initial_offset++;
3259 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3260 replacement we currently believe is valid, mark it as not eliminable if X
3261 modifies DEST in any way other than by adding a constant integer to it.
3263 If DEST is the frame pointer, we do nothing because we assume that
3264 all assignments to the hard frame pointer are nonlocal gotos and are being
3265 done at a time when they are valid and do not disturb anything else.
3266 Some machines want to eliminate a fake argument pointer with either the
3267 frame or stack pointer. Assignments to the hard frame pointer must not
3268 prevent this elimination.
3270 Called via note_stores from reload before starting its passes to scan
3271 the insns of the function. */
3273 static void
3274 mark_not_eliminable (dest, x, data)
3275 rtx dest;
3276 rtx x;
3277 void *data ATTRIBUTE_UNUSED;
3279 register unsigned int i;
3281 /* A SUBREG of a hard register here is just changing its mode. We should
3282 not see a SUBREG of an eliminable hard register, but check just in
3283 case. */
3284 if (GET_CODE (dest) == SUBREG)
3285 dest = SUBREG_REG (dest);
3287 if (dest == hard_frame_pointer_rtx)
3288 return;
3290 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3291 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3292 && (GET_CODE (x) != SET
3293 || GET_CODE (SET_SRC (x)) != PLUS
3294 || XEXP (SET_SRC (x), 0) != dest
3295 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3297 reg_eliminate[i].can_eliminate_previous
3298 = reg_eliminate[i].can_eliminate = 0;
3299 num_eliminable--;
3303 /* Verify that the initial elimination offsets did not change since the
3304 last call to set_initial_elim_offsets. This is used to catch cases
3305 where something illegal happened during reload_as_needed that could
3306 cause incorrect code to be generated if we did not check for it. */
3308 static void
3309 verify_initial_elim_offsets ()
3311 int t;
3313 #ifdef ELIMINABLE_REGS
3314 struct elim_table *ep;
3316 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3318 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3319 if (t != ep->initial_offset)
3320 abort ();
3322 #else
3323 INITIAL_FRAME_POINTER_OFFSET (t);
3324 if (t != reg_eliminate[0].initial_offset)
3325 abort ();
3326 #endif
3329 /* Reset all offsets on eliminable registers to their initial values. */
3331 static void
3332 set_initial_elim_offsets ()
3334 struct elim_table *ep = reg_eliminate;
3336 #ifdef ELIMINABLE_REGS
3337 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3339 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3340 ep->previous_offset = ep->offset = ep->initial_offset;
3342 #else
3343 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3344 ep->previous_offset = ep->offset = ep->initial_offset;
3345 #endif
3347 num_not_at_initial_offset = 0;
3350 /* Initialize the known label offsets.
3351 Set a known offset for each forced label to be at the initial offset
3352 of each elimination. We do this because we assume that all
3353 computed jumps occur from a location where each elimination is
3354 at its initial offset.
3355 For all other labels, show that we don't know the offsets. */
3357 static void
3358 set_initial_label_offsets ()
3360 rtx x;
3361 memset ((char *) &offsets_known_at[get_first_label_num ()], 0, num_labels);
3363 for (x = forced_labels; x; x = XEXP (x, 1))
3364 if (XEXP (x, 0))
3365 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3368 /* Set all elimination offsets to the known values for the code label given
3369 by INSN. */
3371 static void
3372 set_offsets_for_label (insn)
3373 rtx insn;
3375 unsigned int i;
3376 int label_nr = CODE_LABEL_NUMBER (insn);
3377 struct elim_table *ep;
3379 num_not_at_initial_offset = 0;
3380 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3382 ep->offset = ep->previous_offset = offsets_at[label_nr][i];
3383 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3384 num_not_at_initial_offset++;
3388 /* See if anything that happened changes which eliminations are valid.
3389 For example, on the Sparc, whether or not the frame pointer can
3390 be eliminated can depend on what registers have been used. We need
3391 not check some conditions again (such as flag_omit_frame_pointer)
3392 since they can't have changed. */
3394 static void
3395 update_eliminables (pset)
3396 HARD_REG_SET *pset;
3398 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3399 int previous_frame_pointer_needed = frame_pointer_needed;
3400 #endif
3401 struct elim_table *ep;
3403 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3404 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3405 #ifdef ELIMINABLE_REGS
3406 || ! CAN_ELIMINATE (ep->from, ep->to)
3407 #endif
3409 ep->can_eliminate = 0;
3411 /* Look for the case where we have discovered that we can't replace
3412 register A with register B and that means that we will now be
3413 trying to replace register A with register C. This means we can
3414 no longer replace register C with register B and we need to disable
3415 such an elimination, if it exists. This occurs often with A == ap,
3416 B == sp, and C == fp. */
3418 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3420 struct elim_table *op;
3421 register int new_to = -1;
3423 if (! ep->can_eliminate && ep->can_eliminate_previous)
3425 /* Find the current elimination for ep->from, if there is a
3426 new one. */
3427 for (op = reg_eliminate;
3428 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3429 if (op->from == ep->from && op->can_eliminate)
3431 new_to = op->to;
3432 break;
3435 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3436 disable it. */
3437 for (op = reg_eliminate;
3438 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3439 if (op->from == new_to && op->to == ep->to)
3440 op->can_eliminate = 0;
3444 /* See if any registers that we thought we could eliminate the previous
3445 time are no longer eliminable. If so, something has changed and we
3446 must spill the register. Also, recompute the number of eliminable
3447 registers and see if the frame pointer is needed; it is if there is
3448 no elimination of the frame pointer that we can perform. */
3450 frame_pointer_needed = 1;
3451 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3453 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3454 && ep->to != HARD_FRAME_POINTER_REGNUM)
3455 frame_pointer_needed = 0;
3457 if (! ep->can_eliminate && ep->can_eliminate_previous)
3459 ep->can_eliminate_previous = 0;
3460 SET_HARD_REG_BIT (*pset, ep->from);
3461 num_eliminable--;
3465 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3466 /* If we didn't need a frame pointer last time, but we do now, spill
3467 the hard frame pointer. */
3468 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3469 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3470 #endif
3473 /* Initialize the table of registers to eliminate. */
3475 static void
3476 init_elim_table ()
3478 struct elim_table *ep;
3479 #ifdef ELIMINABLE_REGS
3480 struct elim_table_1 *ep1;
3481 #endif
3483 if (!reg_eliminate)
3484 reg_eliminate = (struct elim_table *)
3485 xcalloc (sizeof (struct elim_table), NUM_ELIMINABLE_REGS);
3487 /* Does this function require a frame pointer? */
3489 frame_pointer_needed = (! flag_omit_frame_pointer
3490 #ifdef EXIT_IGNORE_STACK
3491 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3492 and restore sp for alloca. So we can't eliminate
3493 the frame pointer in that case. At some point,
3494 we should improve this by emitting the
3495 sp-adjusting insns for this case. */
3496 || (current_function_calls_alloca
3497 && EXIT_IGNORE_STACK)
3498 #endif
3499 || FRAME_POINTER_REQUIRED);
3501 num_eliminable = 0;
3503 #ifdef ELIMINABLE_REGS
3504 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3505 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3507 ep->from = ep1->from;
3508 ep->to = ep1->to;
3509 ep->can_eliminate = ep->can_eliminate_previous
3510 = (CAN_ELIMINATE (ep->from, ep->to)
3511 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3513 #else
3514 reg_eliminate[0].from = reg_eliminate_1[0].from;
3515 reg_eliminate[0].to = reg_eliminate_1[0].to;
3516 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3517 = ! frame_pointer_needed;
3518 #endif
3520 /* Count the number of eliminable registers and build the FROM and TO
3521 REG rtx's. Note that code in gen_rtx will cause, e.g.,
3522 gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3523 We depend on this. */
3524 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3526 num_eliminable += ep->can_eliminate;
3527 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3528 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3532 /* Kick all pseudos out of hard register REGNO.
3534 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3535 because we found we can't eliminate some register. In the case, no pseudos
3536 are allowed to be in the register, even if they are only in a block that
3537 doesn't require spill registers, unlike the case when we are spilling this
3538 hard reg to produce another spill register.
3540 Return nonzero if any pseudos needed to be kicked out. */
3542 static void
3543 spill_hard_reg (regno, cant_eliminate)
3544 unsigned int regno;
3545 int cant_eliminate;
3547 register int i;
3549 if (cant_eliminate)
3551 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3552 regs_ever_live[regno] = 1;
3555 /* Spill every pseudo reg that was allocated to this reg
3556 or to something that overlaps this reg. */
3558 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3559 if (reg_renumber[i] >= 0
3560 && (unsigned int) reg_renumber[i] <= regno
3561 && ((unsigned int) reg_renumber[i]
3562 + HARD_REGNO_NREGS ((unsigned int) reg_renumber[i],
3563 PSEUDO_REGNO_MODE (i))
3564 > regno))
3565 SET_REGNO_REG_SET (&spilled_pseudos, i);
3568 /* I'm getting weird preprocessor errors if I use IOR_HARD_REG_SET
3569 from within EXECUTE_IF_SET_IN_REG_SET. Hence this awkwardness. */
3571 static void
3572 ior_hard_reg_set (set1, set2)
3573 HARD_REG_SET *set1, *set2;
3575 IOR_HARD_REG_SET (*set1, *set2);
3578 /* After find_reload_regs has been run for all insn that need reloads,
3579 and/or spill_hard_regs was called, this function is used to actually
3580 spill pseudo registers and try to reallocate them. It also sets up the
3581 spill_regs array for use by choose_reload_regs. */
3583 static int
3584 finish_spills (global)
3585 int global;
3587 struct insn_chain *chain;
3588 int something_changed = 0;
3589 int i;
3591 /* Build the spill_regs array for the function. */
3592 /* If there are some registers still to eliminate and one of the spill regs
3593 wasn't ever used before, additional stack space may have to be
3594 allocated to store this register. Thus, we may have changed the offset
3595 between the stack and frame pointers, so mark that something has changed.
3597 One might think that we need only set VAL to 1 if this is a call-used
3598 register. However, the set of registers that must be saved by the
3599 prologue is not identical to the call-used set. For example, the
3600 register used by the call insn for the return PC is a call-used register,
3601 but must be saved by the prologue. */
3603 n_spills = 0;
3604 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3605 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3607 spill_reg_order[i] = n_spills;
3608 spill_regs[n_spills++] = i;
3609 if (num_eliminable && ! regs_ever_live[i])
3610 something_changed = 1;
3611 regs_ever_live[i] = 1;
3613 else
3614 spill_reg_order[i] = -1;
3616 EXECUTE_IF_SET_IN_REG_SET
3617 (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i,
3619 /* Record the current hard register the pseudo is allocated to in
3620 pseudo_previous_regs so we avoid reallocating it to the same
3621 hard reg in a later pass. */
3622 if (reg_renumber[i] < 0)
3623 abort ();
3625 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3626 /* Mark it as no longer having a hard register home. */
3627 reg_renumber[i] = -1;
3628 /* We will need to scan everything again. */
3629 something_changed = 1;
3632 /* Retry global register allocation if possible. */
3633 if (global)
3635 memset ((char *) pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
3636 /* For every insn that needs reloads, set the registers used as spill
3637 regs in pseudo_forbidden_regs for every pseudo live across the
3638 insn. */
3639 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
3641 EXECUTE_IF_SET_IN_REG_SET
3642 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i,
3644 ior_hard_reg_set (pseudo_forbidden_regs + i,
3645 &chain->used_spill_regs);
3647 EXECUTE_IF_SET_IN_REG_SET
3648 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i,
3650 ior_hard_reg_set (pseudo_forbidden_regs + i,
3651 &chain->used_spill_regs);
3655 /* Retry allocating the spilled pseudos. For each reg, merge the
3656 various reg sets that indicate which hard regs can't be used,
3657 and call retry_global_alloc.
3658 We change spill_pseudos here to only contain pseudos that did not
3659 get a new hard register. */
3660 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3661 if (reg_old_renumber[i] != reg_renumber[i])
3663 HARD_REG_SET forbidden;
3664 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
3665 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
3666 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
3667 retry_global_alloc (i, forbidden);
3668 if (reg_renumber[i] >= 0)
3669 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
3673 /* Fix up the register information in the insn chain.
3674 This involves deleting those of the spilled pseudos which did not get
3675 a new hard register home from the live_{before,after} sets. */
3676 for (chain = reload_insn_chain; chain; chain = chain->next)
3678 HARD_REG_SET used_by_pseudos;
3679 HARD_REG_SET used_by_pseudos2;
3681 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
3682 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
3684 /* Mark any unallocated hard regs as available for spills. That
3685 makes inheritance work somewhat better. */
3686 if (chain->need_reload)
3688 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
3689 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
3690 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
3692 /* Save the old value for the sanity test below. */
3693 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
3695 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
3696 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
3697 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
3698 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
3700 /* Make sure we only enlarge the set. */
3701 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
3702 abort ();
3703 ok:;
3707 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
3708 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3710 int regno = reg_renumber[i];
3711 if (reg_old_renumber[i] == regno)
3712 continue;
3714 alter_reg (i, reg_old_renumber[i]);
3715 reg_old_renumber[i] = regno;
3716 if (rtl_dump_file)
3718 if (regno == -1)
3719 fprintf (rtl_dump_file, " Register %d now on stack.\n\n", i);
3720 else
3721 fprintf (rtl_dump_file, " Register %d now in %d.\n\n",
3722 i, reg_renumber[i]);
3726 return something_changed;
3729 /* Find all paradoxical subregs within X and update reg_max_ref_width.
3730 Also mark any hard registers used to store user variables as
3731 forbidden from being used for spill registers. */
3733 static void
3734 scan_paradoxical_subregs (x)
3735 register rtx x;
3737 register int i;
3738 register const char *fmt;
3739 register enum rtx_code code = GET_CODE (x);
3741 switch (code)
3743 case REG:
3744 #if 0
3745 if (SMALL_REGISTER_CLASSES && REGNO (x) < FIRST_PSEUDO_REGISTER
3746 && REG_USERVAR_P (x))
3747 SET_HARD_REG_BIT (bad_spill_regs_global, REGNO (x));
3748 #endif
3749 return;
3751 case CONST_INT:
3752 case CONST:
3753 case SYMBOL_REF:
3754 case LABEL_REF:
3755 case CONST_DOUBLE:
3756 case CC0:
3757 case PC:
3758 case USE:
3759 case CLOBBER:
3760 return;
3762 case SUBREG:
3763 if (GET_CODE (SUBREG_REG (x)) == REG
3764 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3765 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3766 = GET_MODE_SIZE (GET_MODE (x));
3767 return;
3769 default:
3770 break;
3773 fmt = GET_RTX_FORMAT (code);
3774 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3776 if (fmt[i] == 'e')
3777 scan_paradoxical_subregs (XEXP (x, i));
3778 else if (fmt[i] == 'E')
3780 register int j;
3781 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3782 scan_paradoxical_subregs (XVECEXP (x, i, j));
3787 /* Reload pseudo-registers into hard regs around each insn as needed.
3788 Additional register load insns are output before the insn that needs it
3789 and perhaps store insns after insns that modify the reloaded pseudo reg.
3791 reg_last_reload_reg and reg_reloaded_contents keep track of
3792 which registers are already available in reload registers.
3793 We update these for the reloads that we perform,
3794 as the insns are scanned. */
3796 static void
3797 reload_as_needed (live_known)
3798 int live_known;
3800 struct insn_chain *chain;
3801 #if defined (AUTO_INC_DEC)
3802 register int i;
3803 #endif
3804 rtx x;
3806 memset ((char *) spill_reg_rtx, 0, sizeof spill_reg_rtx);
3807 memset ((char *) spill_reg_store, 0, sizeof spill_reg_store);
3808 reg_last_reload_reg = (rtx *) xcalloc (max_regno, sizeof (rtx));
3809 reg_has_output_reload = (char *) xmalloc (max_regno);
3810 CLEAR_HARD_REG_SET (reg_reloaded_valid);
3812 set_initial_elim_offsets ();
3814 for (chain = reload_insn_chain; chain; chain = chain->next)
3816 rtx prev;
3817 rtx insn = chain->insn;
3818 rtx old_next = NEXT_INSN (insn);
3820 /* If we pass a label, copy the offsets from the label information
3821 into the current offsets of each elimination. */
3822 if (GET_CODE (insn) == CODE_LABEL)
3823 set_offsets_for_label (insn);
3825 else if (INSN_P (insn))
3827 rtx oldpat = PATTERN (insn);
3829 /* If this is a USE and CLOBBER of a MEM, ensure that any
3830 references to eliminable registers have been removed. */
3832 if ((GET_CODE (PATTERN (insn)) == USE
3833 || GET_CODE (PATTERN (insn)) == CLOBBER)
3834 && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
3835 XEXP (XEXP (PATTERN (insn), 0), 0)
3836 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
3837 GET_MODE (XEXP (PATTERN (insn), 0)),
3838 NULL_RTX);
3840 /* If we need to do register elimination processing, do so.
3841 This might delete the insn, in which case we are done. */
3842 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
3844 eliminate_regs_in_insn (insn, 1);
3845 if (GET_CODE (insn) == NOTE)
3847 update_eliminable_offsets ();
3848 continue;
3852 /* If need_elim is nonzero but need_reload is zero, one might think
3853 that we could simply set n_reloads to 0. However, find_reloads
3854 could have done some manipulation of the insn (such as swapping
3855 commutative operands), and these manipulations are lost during
3856 the first pass for every insn that needs register elimination.
3857 So the actions of find_reloads must be redone here. */
3859 if (! chain->need_elim && ! chain->need_reload
3860 && ! chain->need_operand_change)
3861 n_reloads = 0;
3862 /* First find the pseudo regs that must be reloaded for this insn.
3863 This info is returned in the tables reload_... (see reload.h).
3864 Also modify the body of INSN by substituting RELOAD
3865 rtx's for those pseudo regs. */
3866 else
3868 memset (reg_has_output_reload, 0, max_regno);
3869 CLEAR_HARD_REG_SET (reg_is_output_reload);
3871 find_reloads (insn, 1, spill_indirect_levels, live_known,
3872 spill_reg_order);
3875 if (num_eliminable && chain->need_elim)
3876 update_eliminable_offsets ();
3878 if (n_reloads > 0)
3880 rtx next = NEXT_INSN (insn);
3881 rtx p;
3883 prev = PREV_INSN (insn);
3885 /* Now compute which reload regs to reload them into. Perhaps
3886 reusing reload regs from previous insns, or else output
3887 load insns to reload them. Maybe output store insns too.
3888 Record the choices of reload reg in reload_reg_rtx. */
3889 choose_reload_regs (chain);
3891 /* Merge any reloads that we didn't combine for fear of
3892 increasing the number of spill registers needed but now
3893 discover can be safely merged. */
3894 if (SMALL_REGISTER_CLASSES)
3895 merge_assigned_reloads (insn);
3897 /* Generate the insns to reload operands into or out of
3898 their reload regs. */
3899 emit_reload_insns (chain);
3901 /* Substitute the chosen reload regs from reload_reg_rtx
3902 into the insn's body (or perhaps into the bodies of other
3903 load and store insn that we just made for reloading
3904 and that we moved the structure into). */
3905 subst_reloads (insn);
3907 /* If this was an ASM, make sure that all the reload insns
3908 we have generated are valid. If not, give an error
3909 and delete them. */
3911 if (asm_noperands (PATTERN (insn)) >= 0)
3912 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
3913 if (p != insn && INSN_P (p)
3914 && (recog_memoized (p) < 0
3915 || (extract_insn (p), ! constrain_operands (1))))
3917 error_for_asm (insn,
3918 "`asm' operand requires impossible reload");
3919 PUT_CODE (p, NOTE);
3920 NOTE_SOURCE_FILE (p) = 0;
3921 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
3924 /* Any previously reloaded spilled pseudo reg, stored in this insn,
3925 is no longer validly lying around to save a future reload.
3926 Note that this does not detect pseudos that were reloaded
3927 for this insn in order to be stored in
3928 (obeying register constraints). That is correct; such reload
3929 registers ARE still valid. */
3930 note_stores (oldpat, forget_old_reloads_1, NULL);
3932 /* There may have been CLOBBER insns placed after INSN. So scan
3933 between INSN and NEXT and use them to forget old reloads. */
3934 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
3935 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
3936 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
3938 #ifdef AUTO_INC_DEC
3939 /* Likewise for regs altered by auto-increment in this insn.
3940 REG_INC notes have been changed by reloading:
3941 find_reloads_address_1 records substitutions for them,
3942 which have been performed by subst_reloads above. */
3943 for (i = n_reloads - 1; i >= 0; i--)
3945 rtx in_reg = rld[i].in_reg;
3946 if (in_reg)
3948 enum rtx_code code = GET_CODE (in_reg);
3949 /* PRE_INC / PRE_DEC will have the reload register ending up
3950 with the same value as the stack slot, but that doesn't
3951 hold true for POST_INC / POST_DEC. Either we have to
3952 convert the memory access to a true POST_INC / POST_DEC,
3953 or we can't use the reload register for inheritance. */
3954 if ((code == POST_INC || code == POST_DEC)
3955 && TEST_HARD_REG_BIT (reg_reloaded_valid,
3956 REGNO (rld[i].reg_rtx))
3957 /* Make sure it is the inc/dec pseudo, and not
3958 some other (e.g. output operand) pseudo. */
3959 && (reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
3960 == REGNO (XEXP (in_reg, 0))))
3963 rtx reload_reg = rld[i].reg_rtx;
3964 enum machine_mode mode = GET_MODE (reload_reg);
3965 int n = 0;
3966 rtx p;
3968 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
3970 /* We really want to ignore REG_INC notes here, so
3971 use PATTERN (p) as argument to reg_set_p . */
3972 if (reg_set_p (reload_reg, PATTERN (p)))
3973 break;
3974 n = count_occurrences (PATTERN (p), reload_reg, 0);
3975 if (! n)
3976 continue;
3977 if (n == 1)
3979 n = validate_replace_rtx (reload_reg,
3980 gen_rtx (code, mode,
3981 reload_reg),
3984 /* We must also verify that the constraints
3985 are met after the replacement. */
3986 extract_insn (p);
3987 if (n)
3988 n = constrain_operands (1);
3989 else
3990 break;
3992 /* If the constraints were not met, then
3993 undo the replacement. */
3994 if (!n)
3996 validate_replace_rtx (gen_rtx (code, mode,
3997 reload_reg),
3998 reload_reg, p);
3999 break;
4003 break;
4005 if (n == 1)
4007 REG_NOTES (p)
4008 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
4009 REG_NOTES (p));
4010 /* Mark this as having an output reload so that the
4011 REG_INC processing code below won't invalidate
4012 the reload for inheritance. */
4013 SET_HARD_REG_BIT (reg_is_output_reload,
4014 REGNO (reload_reg));
4015 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4017 else
4018 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4019 NULL);
4021 else if ((code == PRE_INC || code == PRE_DEC)
4022 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4023 REGNO (rld[i].reg_rtx))
4024 /* Make sure it is the inc/dec pseudo, and not
4025 some other (e.g. output operand) pseudo. */
4026 && (reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4027 == REGNO (XEXP (in_reg, 0))))
4029 SET_HARD_REG_BIT (reg_is_output_reload,
4030 REGNO (rld[i].reg_rtx));
4031 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4035 /* If a pseudo that got a hard register is auto-incremented,
4036 we must purge records of copying it into pseudos without
4037 hard registers. */
4038 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4039 if (REG_NOTE_KIND (x) == REG_INC)
4041 /* See if this pseudo reg was reloaded in this insn.
4042 If so, its last-reload info is still valid
4043 because it is based on this insn's reload. */
4044 for (i = 0; i < n_reloads; i++)
4045 if (rld[i].out == XEXP (x, 0))
4046 break;
4048 if (i == n_reloads)
4049 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4051 #endif
4053 /* A reload reg's contents are unknown after a label. */
4054 if (GET_CODE (insn) == CODE_LABEL)
4055 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4057 /* Don't assume a reload reg is still good after a call insn
4058 if it is a call-used reg. */
4059 else if (GET_CODE (insn) == CALL_INSN)
4060 AND_COMPL_HARD_REG_SET(reg_reloaded_valid, call_used_reg_set);
4063 /* Clean up. */
4064 free (reg_last_reload_reg);
4065 free (reg_has_output_reload);
4068 /* Discard all record of any value reloaded from X,
4069 or reloaded in X from someplace else;
4070 unless X is an output reload reg of the current insn.
4072 X may be a hard reg (the reload reg)
4073 or it may be a pseudo reg that was reloaded from. */
4075 static void
4076 forget_old_reloads_1 (x, ignored, data)
4077 rtx x;
4078 rtx ignored ATTRIBUTE_UNUSED;
4079 void *data ATTRIBUTE_UNUSED;
4081 unsigned int regno;
4082 unsigned int nr;
4083 int offset = 0;
4085 /* note_stores does give us subregs of hard regs. */
4086 while (GET_CODE (x) == SUBREG)
4088 offset += SUBREG_WORD (x);
4089 x = SUBREG_REG (x);
4092 if (GET_CODE (x) != REG)
4093 return;
4095 regno = REGNO (x) + offset;
4097 if (regno >= FIRST_PSEUDO_REGISTER)
4098 nr = 1;
4099 else
4101 unsigned int i;
4103 nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
4104 /* Storing into a spilled-reg invalidates its contents.
4105 This can happen if a block-local pseudo is allocated to that reg
4106 and it wasn't spilled because this block's total need is 0.
4107 Then some insn might have an optional reload and use this reg. */
4108 for (i = 0; i < nr; i++)
4109 /* But don't do this if the reg actually serves as an output
4110 reload reg in the current instruction. */
4111 if (n_reloads == 0
4112 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4114 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4115 spill_reg_store[regno + i] = 0;
4119 /* Since value of X has changed,
4120 forget any value previously copied from it. */
4122 while (nr-- > 0)
4123 /* But don't forget a copy if this is the output reload
4124 that establishes the copy's validity. */
4125 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4126 reg_last_reload_reg[regno + nr] = 0;
4129 /* The following HARD_REG_SETs indicate when each hard register is
4130 used for a reload of various parts of the current insn. */
4132 /* If reg is unavailable for all reloads. */
4133 static HARD_REG_SET reload_reg_unavailable;
4134 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4135 static HARD_REG_SET reload_reg_used;
4136 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4137 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4138 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4139 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4140 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4141 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4142 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4143 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4144 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4145 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4146 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4147 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4148 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4149 static HARD_REG_SET reload_reg_used_in_op_addr;
4150 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4151 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4152 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4153 static HARD_REG_SET reload_reg_used_in_insn;
4154 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4155 static HARD_REG_SET reload_reg_used_in_other_addr;
4157 /* If reg is in use as a reload reg for any sort of reload. */
4158 static HARD_REG_SET reload_reg_used_at_all;
4160 /* If reg is use as an inherited reload. We just mark the first register
4161 in the group. */
4162 static HARD_REG_SET reload_reg_used_for_inherit;
4164 /* Records which hard regs are used in any way, either as explicit use or
4165 by being allocated to a pseudo during any point of the current insn. */
4166 static HARD_REG_SET reg_used_in_insn;
4168 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4169 TYPE. MODE is used to indicate how many consecutive regs are
4170 actually used. */
4172 static void
4173 mark_reload_reg_in_use (regno, opnum, type, mode)
4174 unsigned int regno;
4175 int opnum;
4176 enum reload_type type;
4177 enum machine_mode mode;
4179 unsigned int nregs = HARD_REGNO_NREGS (regno, mode);
4180 unsigned int i;
4182 for (i = regno; i < nregs + regno; i++)
4184 switch (type)
4186 case RELOAD_OTHER:
4187 SET_HARD_REG_BIT (reload_reg_used, i);
4188 break;
4190 case RELOAD_FOR_INPUT_ADDRESS:
4191 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4192 break;
4194 case RELOAD_FOR_INPADDR_ADDRESS:
4195 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4196 break;
4198 case RELOAD_FOR_OUTPUT_ADDRESS:
4199 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4200 break;
4202 case RELOAD_FOR_OUTADDR_ADDRESS:
4203 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4204 break;
4206 case RELOAD_FOR_OPERAND_ADDRESS:
4207 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4208 break;
4210 case RELOAD_FOR_OPADDR_ADDR:
4211 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4212 break;
4214 case RELOAD_FOR_OTHER_ADDRESS:
4215 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4216 break;
4218 case RELOAD_FOR_INPUT:
4219 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4220 break;
4222 case RELOAD_FOR_OUTPUT:
4223 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4224 break;
4226 case RELOAD_FOR_INSN:
4227 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4228 break;
4231 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4235 /* Similarly, but show REGNO is no longer in use for a reload. */
4237 static void
4238 clear_reload_reg_in_use (regno, opnum, type, mode)
4239 unsigned int regno;
4240 int opnum;
4241 enum reload_type type;
4242 enum machine_mode mode;
4244 unsigned int nregs = HARD_REGNO_NREGS (regno, mode);
4245 unsigned int start_regno, end_regno, r;
4246 int i;
4247 /* A complication is that for some reload types, inheritance might
4248 allow multiple reloads of the same types to share a reload register.
4249 We set check_opnum if we have to check only reloads with the same
4250 operand number, and check_any if we have to check all reloads. */
4251 int check_opnum = 0;
4252 int check_any = 0;
4253 HARD_REG_SET *used_in_set;
4255 switch (type)
4257 case RELOAD_OTHER:
4258 used_in_set = &reload_reg_used;
4259 break;
4261 case RELOAD_FOR_INPUT_ADDRESS:
4262 used_in_set = &reload_reg_used_in_input_addr[opnum];
4263 break;
4265 case RELOAD_FOR_INPADDR_ADDRESS:
4266 check_opnum = 1;
4267 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4268 break;
4270 case RELOAD_FOR_OUTPUT_ADDRESS:
4271 used_in_set = &reload_reg_used_in_output_addr[opnum];
4272 break;
4274 case RELOAD_FOR_OUTADDR_ADDRESS:
4275 check_opnum = 1;
4276 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4277 break;
4279 case RELOAD_FOR_OPERAND_ADDRESS:
4280 used_in_set = &reload_reg_used_in_op_addr;
4281 break;
4283 case RELOAD_FOR_OPADDR_ADDR:
4284 check_any = 1;
4285 used_in_set = &reload_reg_used_in_op_addr_reload;
4286 break;
4288 case RELOAD_FOR_OTHER_ADDRESS:
4289 used_in_set = &reload_reg_used_in_other_addr;
4290 check_any = 1;
4291 break;
4293 case RELOAD_FOR_INPUT:
4294 used_in_set = &reload_reg_used_in_input[opnum];
4295 break;
4297 case RELOAD_FOR_OUTPUT:
4298 used_in_set = &reload_reg_used_in_output[opnum];
4299 break;
4301 case RELOAD_FOR_INSN:
4302 used_in_set = &reload_reg_used_in_insn;
4303 break;
4304 default:
4305 abort ();
4307 /* We resolve conflicts with remaining reloads of the same type by
4308 excluding the intervals of of reload registers by them from the
4309 interval of freed reload registers. Since we only keep track of
4310 one set of interval bounds, we might have to exclude somewhat
4311 more then what would be necessary if we used a HARD_REG_SET here.
4312 But this should only happen very infrequently, so there should
4313 be no reason to worry about it. */
4315 start_regno = regno;
4316 end_regno = regno + nregs;
4317 if (check_opnum || check_any)
4319 for (i = n_reloads - 1; i >= 0; i--)
4321 if (rld[i].when_needed == type
4322 && (check_any || rld[i].opnum == opnum)
4323 && rld[i].reg_rtx)
4325 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
4326 unsigned int conflict_end
4327 = (conflict_start
4328 + HARD_REGNO_NREGS (conflict_start, rld[i].mode));
4330 /* If there is an overlap with the first to-be-freed register,
4331 adjust the interval start. */
4332 if (conflict_start <= start_regno && conflict_end > start_regno)
4333 start_regno = conflict_end;
4334 /* Otherwise, if there is a conflict with one of the other
4335 to-be-freed registers, adjust the interval end. */
4336 if (conflict_start > start_regno && conflict_start < end_regno)
4337 end_regno = conflict_start;
4342 for (r = start_regno; r < end_regno; r++)
4343 CLEAR_HARD_REG_BIT (*used_in_set, r);
4346 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4347 specified by OPNUM and TYPE. */
4349 static int
4350 reload_reg_free_p (regno, opnum, type)
4351 unsigned int regno;
4352 int opnum;
4353 enum reload_type type;
4355 int i;
4357 /* In use for a RELOAD_OTHER means it's not available for anything. */
4358 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
4359 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4360 return 0;
4362 switch (type)
4364 case RELOAD_OTHER:
4365 /* In use for anything means we can't use it for RELOAD_OTHER. */
4366 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4367 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4368 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4369 return 0;
4371 for (i = 0; i < reload_n_operands; i++)
4372 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4373 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4374 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4375 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4376 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4377 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4378 return 0;
4380 return 1;
4382 case RELOAD_FOR_INPUT:
4383 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4384 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4385 return 0;
4387 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4388 return 0;
4390 /* If it is used for some other input, can't use it. */
4391 for (i = 0; i < reload_n_operands; i++)
4392 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4393 return 0;
4395 /* If it is used in a later operand's address, can't use it. */
4396 for (i = opnum + 1; i < reload_n_operands; i++)
4397 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4398 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4399 return 0;
4401 return 1;
4403 case RELOAD_FOR_INPUT_ADDRESS:
4404 /* Can't use a register if it is used for an input address for this
4405 operand or used as an input in an earlier one. */
4406 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4407 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4408 return 0;
4410 for (i = 0; i < opnum; i++)
4411 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4412 return 0;
4414 return 1;
4416 case RELOAD_FOR_INPADDR_ADDRESS:
4417 /* Can't use a register if it is used for an input address
4418 for this operand or used as an input in an earlier
4419 one. */
4420 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4421 return 0;
4423 for (i = 0; i < opnum; i++)
4424 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4425 return 0;
4427 return 1;
4429 case RELOAD_FOR_OUTPUT_ADDRESS:
4430 /* Can't use a register if it is used for an output address for this
4431 operand or used as an output in this or a later operand. */
4432 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4433 return 0;
4435 for (i = opnum; i < reload_n_operands; i++)
4436 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4437 return 0;
4439 return 1;
4441 case RELOAD_FOR_OUTADDR_ADDRESS:
4442 /* Can't use a register if it is used for an output address
4443 for this operand or used as an output in this or a
4444 later operand. */
4445 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4446 return 0;
4448 for (i = opnum; i < reload_n_operands; i++)
4449 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4450 return 0;
4452 return 1;
4454 case RELOAD_FOR_OPERAND_ADDRESS:
4455 for (i = 0; i < reload_n_operands; i++)
4456 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4457 return 0;
4459 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4460 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4462 case RELOAD_FOR_OPADDR_ADDR:
4463 for (i = 0; i < reload_n_operands; i++)
4464 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4465 return 0;
4467 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4469 case RELOAD_FOR_OUTPUT:
4470 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4471 outputs, or an operand address for this or an earlier output. */
4472 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4473 return 0;
4475 for (i = 0; i < reload_n_operands; i++)
4476 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4477 return 0;
4479 for (i = 0; i <= opnum; i++)
4480 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4481 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4482 return 0;
4484 return 1;
4486 case RELOAD_FOR_INSN:
4487 for (i = 0; i < reload_n_operands; i++)
4488 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4489 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4490 return 0;
4492 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4493 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4495 case RELOAD_FOR_OTHER_ADDRESS:
4496 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4498 abort ();
4501 /* Return 1 if the value in reload reg REGNO, as used by a reload
4502 needed for the part of the insn specified by OPNUM and TYPE,
4503 is still available in REGNO at the end of the insn.
4505 We can assume that the reload reg was already tested for availability
4506 at the time it is needed, and we should not check this again,
4507 in case the reg has already been marked in use. */
4509 static int
4510 reload_reg_reaches_end_p (regno, opnum, type)
4511 unsigned int regno;
4512 int opnum;
4513 enum reload_type type;
4515 int i;
4517 switch (type)
4519 case RELOAD_OTHER:
4520 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4521 its value must reach the end. */
4522 return 1;
4524 /* If this use is for part of the insn,
4525 its value reaches if no subsequent part uses the same register.
4526 Just like the above function, don't try to do this with lots
4527 of fallthroughs. */
4529 case RELOAD_FOR_OTHER_ADDRESS:
4530 /* Here we check for everything else, since these don't conflict
4531 with anything else and everything comes later. */
4533 for (i = 0; i < reload_n_operands; i++)
4534 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4535 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4536 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4537 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4538 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4539 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4540 return 0;
4542 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4543 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4544 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4546 case RELOAD_FOR_INPUT_ADDRESS:
4547 case RELOAD_FOR_INPADDR_ADDRESS:
4548 /* Similar, except that we check only for this and subsequent inputs
4549 and the address of only subsequent inputs and we do not need
4550 to check for RELOAD_OTHER objects since they are known not to
4551 conflict. */
4553 for (i = opnum; i < reload_n_operands; i++)
4554 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4555 return 0;
4557 for (i = opnum + 1; i < reload_n_operands; i++)
4558 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4559 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4560 return 0;
4562 for (i = 0; i < reload_n_operands; i++)
4563 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4564 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4565 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4566 return 0;
4568 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4569 return 0;
4571 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4572 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4573 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4575 case RELOAD_FOR_INPUT:
4576 /* Similar to input address, except we start at the next operand for
4577 both input and input address and we do not check for
4578 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4579 would conflict. */
4581 for (i = opnum + 1; i < reload_n_operands; i++)
4582 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4583 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4584 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4585 return 0;
4587 /* ... fall through ... */
4589 case RELOAD_FOR_OPERAND_ADDRESS:
4590 /* Check outputs and their addresses. */
4592 for (i = 0; i < reload_n_operands; i++)
4593 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4594 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4595 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4596 return 0;
4598 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
4600 case RELOAD_FOR_OPADDR_ADDR:
4601 for (i = 0; i < reload_n_operands; i++)
4602 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4603 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4604 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4605 return 0;
4607 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4608 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4609 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4611 case RELOAD_FOR_INSN:
4612 /* These conflict with other outputs with RELOAD_OTHER. So
4613 we need only check for output addresses. */
4615 opnum = -1;
4617 /* ... fall through ... */
4619 case RELOAD_FOR_OUTPUT:
4620 case RELOAD_FOR_OUTPUT_ADDRESS:
4621 case RELOAD_FOR_OUTADDR_ADDRESS:
4622 /* We already know these can't conflict with a later output. So the
4623 only thing to check are later output addresses. */
4624 for (i = opnum + 1; i < reload_n_operands; i++)
4625 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4626 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4627 return 0;
4629 return 1;
4632 abort ();
4635 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
4636 Return 0 otherwise.
4638 This function uses the same algorithm as reload_reg_free_p above. */
4641 reloads_conflict (r1, r2)
4642 int r1, r2;
4644 enum reload_type r1_type = rld[r1].when_needed;
4645 enum reload_type r2_type = rld[r2].when_needed;
4646 int r1_opnum = rld[r1].opnum;
4647 int r2_opnum = rld[r2].opnum;
4649 /* RELOAD_OTHER conflicts with everything. */
4650 if (r2_type == RELOAD_OTHER)
4651 return 1;
4653 /* Otherwise, check conflicts differently for each type. */
4655 switch (r1_type)
4657 case RELOAD_FOR_INPUT:
4658 return (r2_type == RELOAD_FOR_INSN
4659 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
4660 || r2_type == RELOAD_FOR_OPADDR_ADDR
4661 || r2_type == RELOAD_FOR_INPUT
4662 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
4663 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
4664 && r2_opnum > r1_opnum));
4666 case RELOAD_FOR_INPUT_ADDRESS:
4667 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
4668 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4670 case RELOAD_FOR_INPADDR_ADDRESS:
4671 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
4672 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4674 case RELOAD_FOR_OUTPUT_ADDRESS:
4675 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
4676 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
4678 case RELOAD_FOR_OUTADDR_ADDRESS:
4679 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
4680 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
4682 case RELOAD_FOR_OPERAND_ADDRESS:
4683 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
4684 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4686 case RELOAD_FOR_OPADDR_ADDR:
4687 return (r2_type == RELOAD_FOR_INPUT
4688 || r2_type == RELOAD_FOR_OPADDR_ADDR);
4690 case RELOAD_FOR_OUTPUT:
4691 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
4692 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
4693 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
4694 && r2_opnum <= r1_opnum));
4696 case RELOAD_FOR_INSN:
4697 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
4698 || r2_type == RELOAD_FOR_INSN
4699 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4701 case RELOAD_FOR_OTHER_ADDRESS:
4702 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
4704 case RELOAD_OTHER:
4705 return 1;
4707 default:
4708 abort ();
4712 /* Indexed by reload number, 1 if incoming value
4713 inherited from previous insns. */
4714 char reload_inherited[MAX_RELOADS];
4716 /* For an inherited reload, this is the insn the reload was inherited from,
4717 if we know it. Otherwise, this is 0. */
4718 rtx reload_inheritance_insn[MAX_RELOADS];
4720 /* If non-zero, this is a place to get the value of the reload,
4721 rather than using reload_in. */
4722 rtx reload_override_in[MAX_RELOADS];
4724 /* For each reload, the hard register number of the register used,
4725 or -1 if we did not need a register for this reload. */
4726 int reload_spill_index[MAX_RELOADS];
4728 /* Subroutine of free_for_value_p, used to check a single register. */
4730 static int
4731 reload_reg_free_for_value_p (regno, opnum, type, value, out, reloadnum,
4732 ignore_address_reloads)
4733 int regno;
4734 int opnum;
4735 enum reload_type type;
4736 rtx value, out;
4737 int reloadnum;
4738 int ignore_address_reloads;
4740 int time1;
4741 /* Set if we see an input reload that must not share its reload register
4742 with any new earlyclobber, but might otherwise share the reload
4743 register with an output or input-output reload. */
4744 int check_earlyclobber = 0;
4745 int i;
4746 int copy = 0;
4748 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4749 return 0;
4751 if (out == const0_rtx)
4753 copy = 1;
4754 out = NULL_RTX;
4757 /* We use some pseudo 'time' value to check if the lifetimes of the
4758 new register use would overlap with the one of a previous reload
4759 that is not read-only or uses a different value.
4760 The 'time' used doesn't have to be linear in any shape or form, just
4761 monotonic.
4762 Some reload types use different 'buckets' for each operand.
4763 So there are MAX_RECOG_OPERANDS different time values for each
4764 such reload type.
4765 We compute TIME1 as the time when the register for the prospective
4766 new reload ceases to be live, and TIME2 for each existing
4767 reload as the time when that the reload register of that reload
4768 becomes live.
4769 Where there is little to be gained by exact lifetime calculations,
4770 we just make conservative assumptions, i.e. a longer lifetime;
4771 this is done in the 'default:' cases. */
4772 switch (type)
4774 case RELOAD_FOR_OTHER_ADDRESS:
4775 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
4776 time1 = copy ? 0 : 1;
4777 break;
4778 case RELOAD_OTHER:
4779 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
4780 break;
4781 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
4782 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
4783 respectively, to the time values for these, we get distinct time
4784 values. To get distinct time values for each operand, we have to
4785 multiply opnum by at least three. We round that up to four because
4786 multiply by four is often cheaper. */
4787 case RELOAD_FOR_INPADDR_ADDRESS:
4788 time1 = opnum * 4 + 2;
4789 break;
4790 case RELOAD_FOR_INPUT_ADDRESS:
4791 time1 = opnum * 4 + 3;
4792 break;
4793 case RELOAD_FOR_INPUT:
4794 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
4795 executes (inclusive). */
4796 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
4797 break;
4798 case RELOAD_FOR_OPADDR_ADDR:
4799 /* opnum * 4 + 4
4800 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
4801 time1 = MAX_RECOG_OPERANDS * 4 + 1;
4802 break;
4803 case RELOAD_FOR_OPERAND_ADDRESS:
4804 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
4805 is executed. */
4806 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
4807 break;
4808 case RELOAD_FOR_OUTADDR_ADDRESS:
4809 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
4810 break;
4811 case RELOAD_FOR_OUTPUT_ADDRESS:
4812 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
4813 break;
4814 default:
4815 time1 = MAX_RECOG_OPERANDS * 5 + 5;
4818 for (i = 0; i < n_reloads; i++)
4820 rtx reg = rld[i].reg_rtx;
4821 if (reg && GET_CODE (reg) == REG
4822 && ((unsigned) regno - true_regnum (reg)
4823 <= HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)) - (unsigned)1)
4824 && i != reloadnum)
4826 if (! rld[i].in || ! rtx_equal_p (rld[i].in, value)
4827 || rld[i].out || out)
4829 int time2;
4830 switch (rld[i].when_needed)
4832 case RELOAD_FOR_OTHER_ADDRESS:
4833 time2 = 0;
4834 break;
4835 case RELOAD_FOR_INPADDR_ADDRESS:
4836 /* find_reloads makes sure that a
4837 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
4838 by at most one - the first -
4839 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
4840 address reload is inherited, the address address reload
4841 goes away, so we can ignore this conflict. */
4842 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
4843 && ignore_address_reloads
4844 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
4845 Then the address address is still needed to store
4846 back the new address. */
4847 && ! rld[reloadnum].out)
4848 continue;
4849 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
4850 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
4851 reloads go away. */
4852 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
4853 && ignore_address_reloads
4854 /* Unless we are reloading an auto_inc expression. */
4855 && ! rld[reloadnum].out)
4856 continue;
4857 time2 = rld[i].opnum * 4 + 2;
4858 break;
4859 case RELOAD_FOR_INPUT_ADDRESS:
4860 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
4861 && ignore_address_reloads
4862 && ! rld[reloadnum].out)
4863 continue;
4864 time2 = rld[i].opnum * 4 + 3;
4865 break;
4866 case RELOAD_FOR_INPUT:
4867 time2 = rld[i].opnum * 4 + 4;
4868 check_earlyclobber = 1;
4869 break;
4870 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
4871 == MAX_RECOG_OPERAND * 4 */
4872 case RELOAD_FOR_OPADDR_ADDR:
4873 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
4874 && ignore_address_reloads
4875 && ! rld[reloadnum].out)
4876 continue;
4877 time2 = MAX_RECOG_OPERANDS * 4 + 1;
4878 break;
4879 case RELOAD_FOR_OPERAND_ADDRESS:
4880 time2 = MAX_RECOG_OPERANDS * 4 + 2;
4881 check_earlyclobber = 1;
4882 break;
4883 case RELOAD_FOR_INSN:
4884 time2 = MAX_RECOG_OPERANDS * 4 + 3;
4885 break;
4886 case RELOAD_FOR_OUTPUT:
4887 /* All RELOAD_FOR_OUTPUT reloads become live just after the
4888 instruction is executed. */
4889 time2 = MAX_RECOG_OPERANDS * 4 + 4;
4890 break;
4891 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
4892 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
4893 value. */
4894 case RELOAD_FOR_OUTADDR_ADDRESS:
4895 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
4896 && ignore_address_reloads
4897 && ! rld[reloadnum].out)
4898 continue;
4899 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
4900 break;
4901 case RELOAD_FOR_OUTPUT_ADDRESS:
4902 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
4903 break;
4904 case RELOAD_OTHER:
4905 /* If there is no conflict in the input part, handle this
4906 like an output reload. */
4907 if (! rld[i].in || rtx_equal_p (rld[i].in, value))
4909 time2 = MAX_RECOG_OPERANDS * 4 + 4;
4910 /* Earlyclobbered outputs must conflict with inputs. */
4911 if (earlyclobber_operand_p (rld[i].out))
4912 time2 = MAX_RECOG_OPERANDS * 4 + 3;
4914 break;
4916 time2 = 1;
4917 /* RELOAD_OTHER might be live beyond instruction execution,
4918 but this is not obvious when we set time2 = 1. So check
4919 here if there might be a problem with the new reload
4920 clobbering the register used by the RELOAD_OTHER. */
4921 if (out)
4922 return 0;
4923 break;
4924 default:
4925 return 0;
4927 if ((time1 >= time2
4928 && (! rld[i].in || rld[i].out
4929 || ! rtx_equal_p (rld[i].in, value)))
4930 || (out && rld[reloadnum].out_reg
4931 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
4932 return 0;
4937 /* Earlyclobbered outputs must conflict with inputs. */
4938 if (check_earlyclobber && out && earlyclobber_operand_p (out))
4939 return 0;
4941 return 1;
4944 /* Return 1 if the value in reload reg REGNO, as used by a reload
4945 needed for the part of the insn specified by OPNUM and TYPE,
4946 may be used to load VALUE into it.
4948 MODE is the mode in which the register is used, this is needed to
4949 determine how many hard regs to test.
4951 Other read-only reloads with the same value do not conflict
4952 unless OUT is non-zero and these other reloads have to live while
4953 output reloads live.
4954 If OUT is CONST0_RTX, this is a special case: it means that the
4955 test should not be for using register REGNO as reload register, but
4956 for copying from register REGNO into the reload register.
4958 RELOADNUM is the number of the reload we want to load this value for;
4959 a reload does not conflict with itself.
4961 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
4962 reloads that load an address for the very reload we are considering.
4964 The caller has to make sure that there is no conflict with the return
4965 register. */
4967 static int
4968 free_for_value_p (regno, mode, opnum, type, value, out, reloadnum,
4969 ignore_address_reloads)
4970 int regno;
4971 enum machine_mode mode;
4972 int opnum;
4973 enum reload_type type;
4974 rtx value, out;
4975 int reloadnum;
4976 int ignore_address_reloads;
4978 int nregs = HARD_REGNO_NREGS (regno, mode);
4979 while (nregs-- > 0)
4980 if (! reload_reg_free_for_value_p (regno + nregs, opnum, type, value, out,
4981 reloadnum, ignore_address_reloads))
4982 return 0;
4983 return 1;
4986 /* Determine whether the reload reg X overlaps any rtx'es used for
4987 overriding inheritance. Return nonzero if so. */
4989 static int
4990 conflicts_with_override (x)
4991 rtx x;
4993 int i;
4994 for (i = 0; i < n_reloads; i++)
4995 if (reload_override_in[i]
4996 && reg_overlap_mentioned_p (x, reload_override_in[i]))
4997 return 1;
4998 return 0;
5001 /* Give an error message saying we failed to find a reload for INSN,
5002 and clear out reload R. */
5003 static void
5004 failed_reload (insn, r)
5005 rtx insn;
5006 int r;
5008 if (asm_noperands (PATTERN (insn)) < 0)
5009 /* It's the compiler's fault. */
5010 fatal_insn ("Could not find a spill register", insn);
5012 /* It's the user's fault; the operand's mode and constraint
5013 don't match. Disable this reload so we don't crash in final. */
5014 error_for_asm (insn,
5015 "`asm' operand constraint incompatible with operand size");
5016 rld[r].in = 0;
5017 rld[r].out = 0;
5018 rld[r].reg_rtx = 0;
5019 rld[r].optional = 1;
5020 rld[r].secondary_p = 1;
5023 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5024 for reload R. If it's valid, get an rtx for it. Return nonzero if
5025 successful. */
5026 static int
5027 set_reload_reg (i, r)
5028 int i, r;
5030 int regno;
5031 rtx reg = spill_reg_rtx[i];
5033 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5034 spill_reg_rtx[i] = reg
5035 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5037 regno = true_regnum (reg);
5039 /* Detect when the reload reg can't hold the reload mode.
5040 This used to be one `if', but Sequent compiler can't handle that. */
5041 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5043 enum machine_mode test_mode = VOIDmode;
5044 if (rld[r].in)
5045 test_mode = GET_MODE (rld[r].in);
5046 /* If rld[r].in has VOIDmode, it means we will load it
5047 in whatever mode the reload reg has: to wit, rld[r].mode.
5048 We have already tested that for validity. */
5049 /* Aside from that, we need to test that the expressions
5050 to reload from or into have modes which are valid for this
5051 reload register. Otherwise the reload insns would be invalid. */
5052 if (! (rld[r].in != 0 && test_mode != VOIDmode
5053 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5054 if (! (rld[r].out != 0
5055 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5057 /* The reg is OK. */
5058 last_spill_reg = i;
5060 /* Mark as in use for this insn the reload regs we use
5061 for this. */
5062 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5063 rld[r].when_needed, rld[r].mode);
5065 rld[r].reg_rtx = reg;
5066 reload_spill_index[r] = spill_regs[i];
5067 return 1;
5070 return 0;
5073 /* Find a spill register to use as a reload register for reload R.
5074 LAST_RELOAD is non-zero if this is the last reload for the insn being
5075 processed.
5077 Set rld[R].reg_rtx to the register allocated.
5079 We return 1 if successful, or 0 if we couldn't find a spill reg and
5080 we didn't change anything. */
5082 static int
5083 allocate_reload_reg (chain, r, last_reload)
5084 struct insn_chain *chain ATTRIBUTE_UNUSED;
5085 int r;
5086 int last_reload;
5088 int i, pass, count;
5090 /* If we put this reload ahead, thinking it is a group,
5091 then insist on finding a group. Otherwise we can grab a
5092 reg that some other reload needs.
5093 (That can happen when we have a 68000 DATA_OR_FP_REG
5094 which is a group of data regs or one fp reg.)
5095 We need not be so restrictive if there are no more reloads
5096 for this insn.
5098 ??? Really it would be nicer to have smarter handling
5099 for that kind of reg class, where a problem like this is normal.
5100 Perhaps those classes should be avoided for reloading
5101 by use of more alternatives. */
5103 int force_group = rld[r].nregs > 1 && ! last_reload;
5105 /* If we want a single register and haven't yet found one,
5106 take any reg in the right class and not in use.
5107 If we want a consecutive group, here is where we look for it.
5109 We use two passes so we can first look for reload regs to
5110 reuse, which are already in use for other reloads in this insn,
5111 and only then use additional registers.
5112 I think that maximizing reuse is needed to make sure we don't
5113 run out of reload regs. Suppose we have three reloads, and
5114 reloads A and B can share regs. These need two regs.
5115 Suppose A and B are given different regs.
5116 That leaves none for C. */
5117 for (pass = 0; pass < 2; pass++)
5119 /* I is the index in spill_regs.
5120 We advance it round-robin between insns to use all spill regs
5121 equally, so that inherited reloads have a chance
5122 of leapfrogging each other. */
5124 i = last_spill_reg;
5126 for (count = 0; count < n_spills; count++)
5128 int class = (int) rld[r].class;
5129 int regnum;
5131 i++;
5132 if (i >= n_spills)
5133 i -= n_spills;
5134 regnum = spill_regs[i];
5136 if ((reload_reg_free_p (regnum, rld[r].opnum,
5137 rld[r].when_needed)
5138 || (rld[r].in
5139 /* We check reload_reg_used to make sure we
5140 don't clobber the return register. */
5141 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5142 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
5143 rld[r].when_needed, rld[r].in,
5144 rld[r].out, r, 1)))
5145 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5146 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5147 /* Look first for regs to share, then for unshared. But
5148 don't share regs used for inherited reloads; they are
5149 the ones we want to preserve. */
5150 && (pass
5151 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5152 regnum)
5153 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5154 regnum))))
5156 int nr = HARD_REGNO_NREGS (regnum, rld[r].mode);
5157 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5158 (on 68000) got us two FP regs. If NR is 1,
5159 we would reject both of them. */
5160 if (force_group)
5161 nr = rld[r].nregs;
5162 /* If we need only one reg, we have already won. */
5163 if (nr == 1)
5165 /* But reject a single reg if we demand a group. */
5166 if (force_group)
5167 continue;
5168 break;
5170 /* Otherwise check that as many consecutive regs as we need
5171 are available here. */
5172 while (nr > 1)
5174 int regno = regnum + nr - 1;
5175 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5176 && spill_reg_order[regno] >= 0
5177 && reload_reg_free_p (regno, rld[r].opnum,
5178 rld[r].when_needed)))
5179 break;
5180 nr--;
5182 if (nr == 1)
5183 break;
5187 /* If we found something on pass 1, omit pass 2. */
5188 if (count < n_spills)
5189 break;
5192 /* We should have found a spill register by now. */
5193 if (count >= n_spills)
5194 return 0;
5196 /* I is the index in SPILL_REG_RTX of the reload register we are to
5197 allocate. Get an rtx for it and find its register number. */
5199 return set_reload_reg (i, r);
5202 /* Initialize all the tables needed to allocate reload registers.
5203 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5204 is the array we use to restore the reg_rtx field for every reload. */
5206 static void
5207 choose_reload_regs_init (chain, save_reload_reg_rtx)
5208 struct insn_chain *chain;
5209 rtx *save_reload_reg_rtx;
5211 int i;
5213 for (i = 0; i < n_reloads; i++)
5214 rld[i].reg_rtx = save_reload_reg_rtx[i];
5216 memset (reload_inherited, 0, MAX_RELOADS);
5217 memset ((char *) reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
5218 memset ((char *) reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
5220 CLEAR_HARD_REG_SET (reload_reg_used);
5221 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5222 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5223 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5224 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5225 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5227 CLEAR_HARD_REG_SET (reg_used_in_insn);
5229 HARD_REG_SET tmp;
5230 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
5231 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5232 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
5233 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5234 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
5235 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
5238 for (i = 0; i < reload_n_operands; i++)
5240 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5241 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5242 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5243 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5244 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5245 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5248 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
5250 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5252 for (i = 0; i < n_reloads; i++)
5253 /* If we have already decided to use a certain register,
5254 don't use it in another way. */
5255 if (rld[i].reg_rtx)
5256 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5257 rld[i].when_needed, rld[i].mode);
5260 /* Assign hard reg targets for the pseudo-registers we must reload
5261 into hard regs for this insn.
5262 Also output the instructions to copy them in and out of the hard regs.
5264 For machines with register classes, we are responsible for
5265 finding a reload reg in the proper class. */
5267 static void
5268 choose_reload_regs (chain)
5269 struct insn_chain *chain;
5271 rtx insn = chain->insn;
5272 register int i, j;
5273 unsigned int max_group_size = 1;
5274 enum reg_class group_class = NO_REGS;
5275 int pass, win, inheritance;
5277 rtx save_reload_reg_rtx[MAX_RELOADS];
5279 /* In order to be certain of getting the registers we need,
5280 we must sort the reloads into order of increasing register class.
5281 Then our grabbing of reload registers will parallel the process
5282 that provided the reload registers.
5284 Also note whether any of the reloads wants a consecutive group of regs.
5285 If so, record the maximum size of the group desired and what
5286 register class contains all the groups needed by this insn. */
5288 for (j = 0; j < n_reloads; j++)
5290 reload_order[j] = j;
5291 reload_spill_index[j] = -1;
5293 if (rld[j].nregs > 1)
5295 max_group_size = MAX (rld[j].nregs, max_group_size);
5296 group_class
5297 = reg_class_superunion[(int) rld[j].class][(int)group_class];
5300 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5303 if (n_reloads > 1)
5304 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5306 /* If -O, try first with inheritance, then turning it off.
5307 If not -O, don't do inheritance.
5308 Using inheritance when not optimizing leads to paradoxes
5309 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5310 because one side of the comparison might be inherited. */
5311 win = 0;
5312 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5314 choose_reload_regs_init (chain, save_reload_reg_rtx);
5316 /* Process the reloads in order of preference just found.
5317 Beyond this point, subregs can be found in reload_reg_rtx.
5319 This used to look for an existing reloaded home for all of the
5320 reloads, and only then perform any new reloads. But that could lose
5321 if the reloads were done out of reg-class order because a later
5322 reload with a looser constraint might have an old home in a register
5323 needed by an earlier reload with a tighter constraint.
5325 To solve this, we make two passes over the reloads, in the order
5326 described above. In the first pass we try to inherit a reload
5327 from a previous insn. If there is a later reload that needs a
5328 class that is a proper subset of the class being processed, we must
5329 also allocate a spill register during the first pass.
5331 Then make a second pass over the reloads to allocate any reloads
5332 that haven't been given registers yet. */
5334 for (j = 0; j < n_reloads; j++)
5336 register int r = reload_order[j];
5337 rtx search_equiv = NULL_RTX;
5339 /* Ignore reloads that got marked inoperative. */
5340 if (rld[r].out == 0 && rld[r].in == 0
5341 && ! rld[r].secondary_p)
5342 continue;
5344 /* If find_reloads chose to use reload_in or reload_out as a reload
5345 register, we don't need to chose one. Otherwise, try even if it
5346 found one since we might save an insn if we find the value lying
5347 around.
5348 Try also when reload_in is a pseudo without a hard reg. */
5349 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5350 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5351 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5352 && GET_CODE (rld[r].in) != MEM
5353 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5354 continue;
5356 #if 0 /* No longer needed for correct operation.
5357 It might give better code, or might not; worth an experiment? */
5358 /* If this is an optional reload, we can't inherit from earlier insns
5359 until we are sure that any non-optional reloads have been allocated.
5360 The following code takes advantage of the fact that optional reloads
5361 are at the end of reload_order. */
5362 if (rld[r].optional != 0)
5363 for (i = 0; i < j; i++)
5364 if ((rld[reload_order[i]].out != 0
5365 || rld[reload_order[i]].in != 0
5366 || rld[reload_order[i]].secondary_p)
5367 && ! rld[reload_order[i]].optional
5368 && rld[reload_order[i]].reg_rtx == 0)
5369 allocate_reload_reg (chain, reload_order[i], 0);
5370 #endif
5372 /* First see if this pseudo is already available as reloaded
5373 for a previous insn. We cannot try to inherit for reloads
5374 that are smaller than the maximum number of registers needed
5375 for groups unless the register we would allocate cannot be used
5376 for the groups.
5378 We could check here to see if this is a secondary reload for
5379 an object that is already in a register of the desired class.
5380 This would avoid the need for the secondary reload register.
5381 But this is complex because we can't easily determine what
5382 objects might want to be loaded via this reload. So let a
5383 register be allocated here. In `emit_reload_insns' we suppress
5384 one of the loads in the case described above. */
5386 if (inheritance)
5388 int word = 0;
5389 register int regno = -1;
5390 enum machine_mode mode = VOIDmode;
5392 if (rld[r].in == 0)
5394 else if (GET_CODE (rld[r].in) == REG)
5396 regno = REGNO (rld[r].in);
5397 mode = GET_MODE (rld[r].in);
5399 else if (GET_CODE (rld[r].in_reg) == REG)
5401 regno = REGNO (rld[r].in_reg);
5402 mode = GET_MODE (rld[r].in_reg);
5404 else if (GET_CODE (rld[r].in_reg) == SUBREG
5405 && GET_CODE (SUBREG_REG (rld[r].in_reg)) == REG)
5407 word = SUBREG_WORD (rld[r].in_reg);
5408 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5409 if (regno < FIRST_PSEUDO_REGISTER)
5410 regno += word;
5411 mode = GET_MODE (rld[r].in_reg);
5413 #ifdef AUTO_INC_DEC
5414 else if ((GET_CODE (rld[r].in_reg) == PRE_INC
5415 || GET_CODE (rld[r].in_reg) == PRE_DEC
5416 || GET_CODE (rld[r].in_reg) == POST_INC
5417 || GET_CODE (rld[r].in_reg) == POST_DEC)
5418 && GET_CODE (XEXP (rld[r].in_reg, 0)) == REG)
5420 regno = REGNO (XEXP (rld[r].in_reg, 0));
5421 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5422 rld[r].out = rld[r].in;
5424 #endif
5425 #if 0
5426 /* This won't work, since REGNO can be a pseudo reg number.
5427 Also, it takes much more hair to keep track of all the things
5428 that can invalidate an inherited reload of part of a pseudoreg. */
5429 else if (GET_CODE (rld[r].in) == SUBREG
5430 && GET_CODE (SUBREG_REG (rld[r].in)) == REG)
5431 regno = REGNO (SUBREG_REG (rld[r].in)) + SUBREG_WORD (rld[r].in);
5432 #endif
5434 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5436 enum reg_class class = rld[r].class, last_class;
5437 rtx last_reg = reg_last_reload_reg[regno];
5438 enum machine_mode need_mode;
5440 i = REGNO (last_reg) + word;
5441 last_class = REGNO_REG_CLASS (i);
5443 if (word == 0)
5444 need_mode = mode;
5445 else
5446 need_mode
5447 = smallest_mode_for_size (GET_MODE_SIZE (mode)
5448 + word * UNITS_PER_WORD,
5449 GET_MODE_CLASS (mode));
5451 if (
5452 #ifdef CLASS_CANNOT_CHANGE_MODE
5453 (TEST_HARD_REG_BIT
5454 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_MODE], i)
5455 ? ! CLASS_CANNOT_CHANGE_MODE_P (GET_MODE (last_reg),
5456 need_mode)
5457 : (GET_MODE_SIZE (GET_MODE (last_reg))
5458 >= GET_MODE_SIZE (need_mode)))
5459 #else
5460 (GET_MODE_SIZE (GET_MODE (last_reg))
5461 >= GET_MODE_SIZE (need_mode))
5462 #endif
5463 && reg_reloaded_contents[i] == regno
5464 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
5465 && HARD_REGNO_MODE_OK (i, rld[r].mode)
5466 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5467 /* Even if we can't use this register as a reload
5468 register, we might use it for reload_override_in,
5469 if copying it to the desired class is cheap
5470 enough. */
5471 || ((REGISTER_MOVE_COST (mode, last_class, class)
5472 < MEMORY_MOVE_COST (mode, class, 1))
5473 #ifdef SECONDARY_INPUT_RELOAD_CLASS
5474 && (SECONDARY_INPUT_RELOAD_CLASS (class, mode,
5475 last_reg)
5476 == NO_REGS)
5477 #endif
5478 #ifdef SECONDARY_MEMORY_NEEDED
5479 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5480 mode)
5481 #endif
5484 && (rld[r].nregs == max_group_size
5485 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5487 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
5488 rld[r].when_needed, rld[r].in,
5489 const0_rtx, r, 1))
5491 /* If a group is needed, verify that all the subsequent
5492 registers still have their values intact. */
5493 int nr = HARD_REGNO_NREGS (i, rld[r].mode);
5494 int k;
5496 for (k = 1; k < nr; k++)
5497 if (reg_reloaded_contents[i + k] != regno
5498 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
5499 break;
5501 if (k == nr)
5503 int i1;
5505 last_reg = (GET_MODE (last_reg) == mode
5506 ? last_reg : gen_rtx_REG (mode, i));
5508 /* We found a register that contains the
5509 value we need. If this register is the
5510 same as an `earlyclobber' operand of the
5511 current insn, just mark it as a place to
5512 reload from since we can't use it as the
5513 reload register itself. */
5515 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5516 if (reg_overlap_mentioned_for_reload_p
5517 (reg_last_reload_reg[regno],
5518 reload_earlyclobbers[i1]))
5519 break;
5521 if (i1 != n_earlyclobbers
5522 || ! (free_for_value_p (i, rld[r].mode,
5523 rld[r].opnum,
5524 rld[r].when_needed, rld[r].in,
5525 rld[r].out, r, 1))
5526 /* Don't use it if we'd clobber a pseudo reg. */
5527 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
5528 && rld[r].out
5529 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
5530 /* Don't clobber the frame pointer. */
5531 || (i == HARD_FRAME_POINTER_REGNUM
5532 && rld[r].out)
5533 /* Don't really use the inherited spill reg
5534 if we need it wider than we've got it. */
5535 || (GET_MODE_SIZE (rld[r].mode)
5536 > GET_MODE_SIZE (mode))
5537 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5540 /* If find_reloads chose reload_out as reload
5541 register, stay with it - that leaves the
5542 inherited register for subsequent reloads. */
5543 || (rld[r].out && rld[r].reg_rtx
5544 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
5546 if (! rld[r].optional)
5548 reload_override_in[r] = last_reg;
5549 reload_inheritance_insn[r]
5550 = reg_reloaded_insn[i];
5553 else
5555 int k;
5556 /* We can use this as a reload reg. */
5557 /* Mark the register as in use for this part of
5558 the insn. */
5559 mark_reload_reg_in_use (i,
5560 rld[r].opnum,
5561 rld[r].when_needed,
5562 rld[r].mode);
5563 rld[r].reg_rtx = last_reg;
5564 reload_inherited[r] = 1;
5565 reload_inheritance_insn[r]
5566 = reg_reloaded_insn[i];
5567 reload_spill_index[r] = i;
5568 for (k = 0; k < nr; k++)
5569 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5570 i + k);
5577 /* Here's another way to see if the value is already lying around. */
5578 if (inheritance
5579 && rld[r].in != 0
5580 && ! reload_inherited[r]
5581 && rld[r].out == 0
5582 && (CONSTANT_P (rld[r].in)
5583 || GET_CODE (rld[r].in) == PLUS
5584 || GET_CODE (rld[r].in) == REG
5585 || GET_CODE (rld[r].in) == MEM)
5586 && (rld[r].nregs == max_group_size
5587 || ! reg_classes_intersect_p (rld[r].class, group_class)))
5588 search_equiv = rld[r].in;
5589 /* If this is an output reload from a simple move insn, look
5590 if an equivalence for the input is available. */
5591 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
5593 rtx set = single_set (insn);
5595 if (set
5596 && rtx_equal_p (rld[r].out, SET_DEST (set))
5597 && CONSTANT_P (SET_SRC (set)))
5598 search_equiv = SET_SRC (set);
5601 if (search_equiv)
5603 register rtx equiv
5604 = find_equiv_reg (search_equiv, insn, rld[r].class,
5605 -1, NULL_PTR, 0, rld[r].mode);
5606 int regno = 0;
5608 if (equiv != 0)
5610 if (GET_CODE (equiv) == REG)
5611 regno = REGNO (equiv);
5612 else if (GET_CODE (equiv) == SUBREG)
5614 /* This must be a SUBREG of a hard register.
5615 Make a new REG since this might be used in an
5616 address and not all machines support SUBREGs
5617 there. */
5618 regno = REGNO (SUBREG_REG (equiv)) + SUBREG_WORD (equiv);
5619 equiv = gen_rtx_REG (rld[r].mode, regno);
5621 else
5622 abort ();
5625 /* If we found a spill reg, reject it unless it is free
5626 and of the desired class. */
5627 if (equiv != 0
5628 && ((TEST_HARD_REG_BIT (reload_reg_used_at_all, regno)
5629 && ! free_for_value_p (regno, rld[r].mode,
5630 rld[r].opnum, rld[r].when_needed,
5631 rld[r].in, rld[r].out, r, 1))
5632 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5633 regno)))
5634 equiv = 0;
5636 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
5637 equiv = 0;
5639 /* We found a register that contains the value we need.
5640 If this register is the same as an `earlyclobber' operand
5641 of the current insn, just mark it as a place to reload from
5642 since we can't use it as the reload register itself. */
5644 if (equiv != 0)
5645 for (i = 0; i < n_earlyclobbers; i++)
5646 if (reg_overlap_mentioned_for_reload_p (equiv,
5647 reload_earlyclobbers[i]))
5649 if (! rld[r].optional)
5650 reload_override_in[r] = equiv;
5651 equiv = 0;
5652 break;
5655 /* If the equiv register we have found is explicitly clobbered
5656 in the current insn, it depends on the reload type if we
5657 can use it, use it for reload_override_in, or not at all.
5658 In particular, we then can't use EQUIV for a
5659 RELOAD_FOR_OUTPUT_ADDRESS reload. */
5661 if (equiv != 0)
5663 if (regno_clobbered_p (regno, insn, rld[r].mode, 0))
5664 switch (rld[r].when_needed)
5666 case RELOAD_FOR_OTHER_ADDRESS:
5667 case RELOAD_FOR_INPADDR_ADDRESS:
5668 case RELOAD_FOR_INPUT_ADDRESS:
5669 case RELOAD_FOR_OPADDR_ADDR:
5670 break;
5671 case RELOAD_OTHER:
5672 case RELOAD_FOR_INPUT:
5673 case RELOAD_FOR_OPERAND_ADDRESS:
5674 if (! rld[r].optional)
5675 reload_override_in[r] = equiv;
5676 /* Fall through. */
5677 default:
5678 equiv = 0;
5679 break;
5681 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
5682 switch (rld[r].when_needed)
5684 case RELOAD_FOR_OTHER_ADDRESS:
5685 case RELOAD_FOR_INPADDR_ADDRESS:
5686 case RELOAD_FOR_INPUT_ADDRESS:
5687 case RELOAD_FOR_OPADDR_ADDR:
5688 case RELOAD_FOR_OPERAND_ADDRESS:
5689 case RELOAD_FOR_INPUT:
5690 break;
5691 case RELOAD_OTHER:
5692 if (! rld[r].optional)
5693 reload_override_in[r] = equiv;
5694 /* Fall through. */
5695 default:
5696 equiv = 0;
5697 break;
5701 /* If we found an equivalent reg, say no code need be generated
5702 to load it, and use it as our reload reg. */
5703 if (equiv != 0 && regno != HARD_FRAME_POINTER_REGNUM)
5705 int nr = HARD_REGNO_NREGS (regno, rld[r].mode);
5706 int k;
5707 rld[r].reg_rtx = equiv;
5708 reload_inherited[r] = 1;
5710 /* If reg_reloaded_valid is not set for this register,
5711 there might be a stale spill_reg_store lying around.
5712 We must clear it, since otherwise emit_reload_insns
5713 might delete the store. */
5714 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
5715 spill_reg_store[regno] = NULL_RTX;
5716 /* If any of the hard registers in EQUIV are spill
5717 registers, mark them as in use for this insn. */
5718 for (k = 0; k < nr; k++)
5720 i = spill_reg_order[regno + k];
5721 if (i >= 0)
5723 mark_reload_reg_in_use (regno, rld[r].opnum,
5724 rld[r].when_needed,
5725 rld[r].mode);
5726 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5727 regno + k);
5733 /* If we found a register to use already, or if this is an optional
5734 reload, we are done. */
5735 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
5736 continue;
5738 #if 0
5739 /* No longer needed for correct operation. Might or might
5740 not give better code on the average. Want to experiment? */
5742 /* See if there is a later reload that has a class different from our
5743 class that intersects our class or that requires less register
5744 than our reload. If so, we must allocate a register to this
5745 reload now, since that reload might inherit a previous reload
5746 and take the only available register in our class. Don't do this
5747 for optional reloads since they will force all previous reloads
5748 to be allocated. Also don't do this for reloads that have been
5749 turned off. */
5751 for (i = j + 1; i < n_reloads; i++)
5753 int s = reload_order[i];
5755 if ((rld[s].in == 0 && rld[s].out == 0
5756 && ! rld[s].secondary_p)
5757 || rld[s].optional)
5758 continue;
5760 if ((rld[s].class != rld[r].class
5761 && reg_classes_intersect_p (rld[r].class,
5762 rld[s].class))
5763 || rld[s].nregs < rld[r].nregs)
5764 break;
5767 if (i == n_reloads)
5768 continue;
5770 allocate_reload_reg (chain, r, j == n_reloads - 1);
5771 #endif
5774 /* Now allocate reload registers for anything non-optional that
5775 didn't get one yet. */
5776 for (j = 0; j < n_reloads; j++)
5778 register int r = reload_order[j];
5780 /* Ignore reloads that got marked inoperative. */
5781 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
5782 continue;
5784 /* Skip reloads that already have a register allocated or are
5785 optional. */
5786 if (rld[r].reg_rtx != 0 || rld[r].optional)
5787 continue;
5789 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
5790 break;
5793 /* If that loop got all the way, we have won. */
5794 if (j == n_reloads)
5796 win = 1;
5797 break;
5800 /* Loop around and try without any inheritance. */
5803 if (! win)
5805 /* First undo everything done by the failed attempt
5806 to allocate with inheritance. */
5807 choose_reload_regs_init (chain, save_reload_reg_rtx);
5809 /* Some sanity tests to verify that the reloads found in the first
5810 pass are identical to the ones we have now. */
5811 if (chain->n_reloads != n_reloads)
5812 abort ();
5814 for (i = 0; i < n_reloads; i++)
5816 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
5817 continue;
5818 if (chain->rld[i].when_needed != rld[i].when_needed)
5819 abort ();
5820 for (j = 0; j < n_spills; j++)
5821 if (spill_regs[j] == chain->rld[i].regno)
5822 if (! set_reload_reg (j, i))
5823 failed_reload (chain->insn, i);
5827 /* If we thought we could inherit a reload, because it seemed that
5828 nothing else wanted the same reload register earlier in the insn,
5829 verify that assumption, now that all reloads have been assigned.
5830 Likewise for reloads where reload_override_in has been set. */
5832 /* If doing expensive optimizations, do one preliminary pass that doesn't
5833 cancel any inheritance, but removes reloads that have been needed only
5834 for reloads that we know can be inherited. */
5835 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
5837 for (j = 0; j < n_reloads; j++)
5839 register int r = reload_order[j];
5840 rtx check_reg;
5841 if (reload_inherited[r] && rld[r].reg_rtx)
5842 check_reg = rld[r].reg_rtx;
5843 else if (reload_override_in[r]
5844 && (GET_CODE (reload_override_in[r]) == REG
5845 || GET_CODE (reload_override_in[r]) == SUBREG))
5846 check_reg = reload_override_in[r];
5847 else
5848 continue;
5849 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
5850 rld[r].opnum, rld[r].when_needed, rld[r].in,
5851 (reload_inherited[r]
5852 ? rld[r].out : const0_rtx),
5853 r, 1))
5855 if (pass)
5856 continue;
5857 reload_inherited[r] = 0;
5858 reload_override_in[r] = 0;
5860 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
5861 reload_override_in, then we do not need its related
5862 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
5863 likewise for other reload types.
5864 We handle this by removing a reload when its only replacement
5865 is mentioned in reload_in of the reload we are going to inherit.
5866 A special case are auto_inc expressions; even if the input is
5867 inherited, we still need the address for the output. We can
5868 recognize them because they have RELOAD_OUT set to RELOAD_IN.
5869 If we suceeded removing some reload and we are doing a preliminary
5870 pass just to remove such reloads, make another pass, since the
5871 removal of one reload might allow us to inherit another one. */
5872 else if (rld[r].in
5873 && rld[r].out != rld[r].in
5874 && remove_address_replacements (rld[r].in) && pass)
5875 pass = 2;
5879 /* Now that reload_override_in is known valid,
5880 actually override reload_in. */
5881 for (j = 0; j < n_reloads; j++)
5882 if (reload_override_in[j])
5883 rld[j].in = reload_override_in[j];
5885 /* If this reload won't be done because it has been cancelled or is
5886 optional and not inherited, clear reload_reg_rtx so other
5887 routines (such as subst_reloads) don't get confused. */
5888 for (j = 0; j < n_reloads; j++)
5889 if (rld[j].reg_rtx != 0
5890 && ((rld[j].optional && ! reload_inherited[j])
5891 || (rld[j].in == 0 && rld[j].out == 0
5892 && ! rld[j].secondary_p)))
5894 int regno = true_regnum (rld[j].reg_rtx);
5896 if (spill_reg_order[regno] >= 0)
5897 clear_reload_reg_in_use (regno, rld[j].opnum,
5898 rld[j].when_needed, rld[j].mode);
5899 rld[j].reg_rtx = 0;
5900 reload_spill_index[j] = -1;
5903 /* Record which pseudos and which spill regs have output reloads. */
5904 for (j = 0; j < n_reloads; j++)
5906 register int r = reload_order[j];
5908 i = reload_spill_index[r];
5910 /* I is nonneg if this reload uses a register.
5911 If rld[r].reg_rtx is 0, this is an optional reload
5912 that we opted to ignore. */
5913 if (rld[r].out_reg != 0 && GET_CODE (rld[r].out_reg) == REG
5914 && rld[r].reg_rtx != 0)
5916 register int nregno = REGNO (rld[r].out_reg);
5917 int nr = 1;
5919 if (nregno < FIRST_PSEUDO_REGISTER)
5920 nr = HARD_REGNO_NREGS (nregno, rld[r].mode);
5922 while (--nr >= 0)
5923 reg_has_output_reload[nregno + nr] = 1;
5925 if (i >= 0)
5927 nr = HARD_REGNO_NREGS (i, rld[r].mode);
5928 while (--nr >= 0)
5929 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
5932 if (rld[r].when_needed != RELOAD_OTHER
5933 && rld[r].when_needed != RELOAD_FOR_OUTPUT
5934 && rld[r].when_needed != RELOAD_FOR_INSN)
5935 abort ();
5940 /* Deallocate the reload register for reload R. This is called from
5941 remove_address_replacements. */
5943 void
5944 deallocate_reload_reg (r)
5945 int r;
5947 int regno;
5949 if (! rld[r].reg_rtx)
5950 return;
5951 regno = true_regnum (rld[r].reg_rtx);
5952 rld[r].reg_rtx = 0;
5953 if (spill_reg_order[regno] >= 0)
5954 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
5955 rld[r].mode);
5956 reload_spill_index[r] = -1;
5959 /* If SMALL_REGISTER_CLASSES is non-zero, we may not have merged two
5960 reloads of the same item for fear that we might not have enough reload
5961 registers. However, normally they will get the same reload register
5962 and hence actually need not be loaded twice.
5964 Here we check for the most common case of this phenomenon: when we have
5965 a number of reloads for the same object, each of which were allocated
5966 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
5967 reload, and is not modified in the insn itself. If we find such,
5968 merge all the reloads and set the resulting reload to RELOAD_OTHER.
5969 This will not increase the number of spill registers needed and will
5970 prevent redundant code. */
5972 static void
5973 merge_assigned_reloads (insn)
5974 rtx insn;
5976 int i, j;
5978 /* Scan all the reloads looking for ones that only load values and
5979 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
5980 assigned and not modified by INSN. */
5982 for (i = 0; i < n_reloads; i++)
5984 int conflicting_input = 0;
5985 int max_input_address_opnum = -1;
5986 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
5988 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
5989 || rld[i].out != 0 || rld[i].reg_rtx == 0
5990 || reg_set_p (rld[i].reg_rtx, insn))
5991 continue;
5993 /* Look at all other reloads. Ensure that the only use of this
5994 reload_reg_rtx is in a reload that just loads the same value
5995 as we do. Note that any secondary reloads must be of the identical
5996 class since the values, modes, and result registers are the
5997 same, so we need not do anything with any secondary reloads. */
5999 for (j = 0; j < n_reloads; j++)
6001 if (i == j || rld[j].reg_rtx == 0
6002 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6003 rld[i].reg_rtx))
6004 continue;
6006 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6007 && rld[j].opnum > max_input_address_opnum)
6008 max_input_address_opnum = rld[j].opnum;
6010 /* If the reload regs aren't exactly the same (e.g, different modes)
6011 or if the values are different, we can't merge this reload.
6012 But if it is an input reload, we might still merge
6013 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6015 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6016 || rld[j].out != 0 || rld[j].in == 0
6017 || ! rtx_equal_p (rld[i].in, rld[j].in))
6019 if (rld[j].when_needed != RELOAD_FOR_INPUT
6020 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6021 || rld[i].opnum > rld[j].opnum)
6022 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6023 break;
6024 conflicting_input = 1;
6025 if (min_conflicting_input_opnum > rld[j].opnum)
6026 min_conflicting_input_opnum = rld[j].opnum;
6030 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6031 we, in fact, found any matching reloads. */
6033 if (j == n_reloads
6034 && max_input_address_opnum <= min_conflicting_input_opnum)
6036 for (j = 0; j < n_reloads; j++)
6037 if (i != j && rld[j].reg_rtx != 0
6038 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6039 && (! conflicting_input
6040 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6041 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6043 rld[i].when_needed = RELOAD_OTHER;
6044 rld[j].in = 0;
6045 reload_spill_index[j] = -1;
6046 transfer_replacements (i, j);
6049 /* If this is now RELOAD_OTHER, look for any reloads that load
6050 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6051 if they were for inputs, RELOAD_OTHER for outputs. Note that
6052 this test is equivalent to looking for reloads for this operand
6053 number. */
6055 if (rld[i].when_needed == RELOAD_OTHER)
6056 for (j = 0; j < n_reloads; j++)
6057 if (rld[j].in != 0
6058 && rld[i].when_needed != RELOAD_OTHER
6059 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6060 rld[i].in))
6061 rld[j].when_needed
6062 = ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
6063 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6064 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6069 /* These arrays are filled by emit_reload_insns and its subroutines. */
6070 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
6071 static rtx other_input_address_reload_insns = 0;
6072 static rtx other_input_reload_insns = 0;
6073 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6074 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6075 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
6076 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6077 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6078 static rtx operand_reload_insns = 0;
6079 static rtx other_operand_reload_insns = 0;
6080 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6082 /* Values to be put in spill_reg_store are put here first. */
6083 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6084 static HARD_REG_SET reg_reloaded_died;
6086 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
6087 has the number J. OLD contains the value to be used as input. */
6089 static void
6090 emit_input_reload_insns (chain, rl, old, j)
6091 struct insn_chain *chain;
6092 struct reload *rl;
6093 rtx old;
6094 int j;
6096 rtx insn = chain->insn;
6097 register rtx reloadreg = rl->reg_rtx;
6098 rtx oldequiv_reg = 0;
6099 rtx oldequiv = 0;
6100 int special = 0;
6101 enum machine_mode mode;
6102 rtx *where;
6104 /* Determine the mode to reload in.
6105 This is very tricky because we have three to choose from.
6106 There is the mode the insn operand wants (rl->inmode).
6107 There is the mode of the reload register RELOADREG.
6108 There is the intrinsic mode of the operand, which we could find
6109 by stripping some SUBREGs.
6110 It turns out that RELOADREG's mode is irrelevant:
6111 we can change that arbitrarily.
6113 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6114 then the reload reg may not support QImode moves, so use SImode.
6115 If foo is in memory due to spilling a pseudo reg, this is safe,
6116 because the QImode value is in the least significant part of a
6117 slot big enough for a SImode. If foo is some other sort of
6118 memory reference, then it is impossible to reload this case,
6119 so previous passes had better make sure this never happens.
6121 Then consider a one-word union which has SImode and one of its
6122 members is a float, being fetched as (SUBREG:SF union:SI).
6123 We must fetch that as SFmode because we could be loading into
6124 a float-only register. In this case OLD's mode is correct.
6126 Consider an immediate integer: it has VOIDmode. Here we need
6127 to get a mode from something else.
6129 In some cases, there is a fourth mode, the operand's
6130 containing mode. If the insn specifies a containing mode for
6131 this operand, it overrides all others.
6133 I am not sure whether the algorithm here is always right,
6134 but it does the right things in those cases. */
6136 mode = GET_MODE (old);
6137 if (mode == VOIDmode)
6138 mode = rl->inmode;
6140 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6141 /* If we need a secondary register for this operation, see if
6142 the value is already in a register in that class. Don't
6143 do this if the secondary register will be used as a scratch
6144 register. */
6146 if (rl->secondary_in_reload >= 0
6147 && rl->secondary_in_icode == CODE_FOR_nothing
6148 && optimize)
6149 oldequiv
6150 = find_equiv_reg (old, insn,
6151 rld[rl->secondary_in_reload].class,
6152 -1, NULL_PTR, 0, mode);
6153 #endif
6155 /* If reloading from memory, see if there is a register
6156 that already holds the same value. If so, reload from there.
6157 We can pass 0 as the reload_reg_p argument because
6158 any other reload has either already been emitted,
6159 in which case find_equiv_reg will see the reload-insn,
6160 or has yet to be emitted, in which case it doesn't matter
6161 because we will use this equiv reg right away. */
6163 if (oldequiv == 0 && optimize
6164 && (GET_CODE (old) == MEM
6165 || (GET_CODE (old) == REG
6166 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6167 && reg_renumber[REGNO (old)] < 0)))
6168 oldequiv = find_equiv_reg (old, insn, ALL_REGS,
6169 -1, NULL_PTR, 0, mode);
6171 if (oldequiv)
6173 unsigned int regno = true_regnum (oldequiv);
6175 /* Don't use OLDEQUIV if any other reload changes it at an
6176 earlier stage of this insn or at this stage. */
6177 if (! free_for_value_p (regno, rl->mode, rl->opnum, rl->when_needed,
6178 rl->in, const0_rtx, j, 0))
6179 oldequiv = 0;
6181 /* If it is no cheaper to copy from OLDEQUIV into the
6182 reload register than it would be to move from memory,
6183 don't use it. Likewise, if we need a secondary register
6184 or memory. */
6186 if (oldequiv != 0
6187 && ((REGNO_REG_CLASS (regno) != rl->class
6188 && (REGISTER_MOVE_COST (mode, REGNO_REG_CLASS (regno),
6189 rl->class)
6190 >= MEMORY_MOVE_COST (mode, rl->class, 1)))
6191 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6192 || (SECONDARY_INPUT_RELOAD_CLASS (rl->class,
6193 mode, oldequiv)
6194 != NO_REGS)
6195 #endif
6196 #ifdef SECONDARY_MEMORY_NEEDED
6197 || SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (regno),
6198 rl->class,
6199 mode)
6200 #endif
6202 oldequiv = 0;
6205 /* delete_output_reload is only invoked properly if old contains
6206 the original pseudo register. Since this is replaced with a
6207 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6208 find the pseudo in RELOAD_IN_REG. */
6209 if (oldequiv == 0
6210 && reload_override_in[j]
6211 && GET_CODE (rl->in_reg) == REG)
6213 oldequiv = old;
6214 old = rl->in_reg;
6216 if (oldequiv == 0)
6217 oldequiv = old;
6218 else if (GET_CODE (oldequiv) == REG)
6219 oldequiv_reg = oldequiv;
6220 else if (GET_CODE (oldequiv) == SUBREG)
6221 oldequiv_reg = SUBREG_REG (oldequiv);
6223 /* If we are reloading from a register that was recently stored in
6224 with an output-reload, see if we can prove there was
6225 actually no need to store the old value in it. */
6227 if (optimize && GET_CODE (oldequiv) == REG
6228 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6229 && spill_reg_store[REGNO (oldequiv)]
6230 && GET_CODE (old) == REG
6231 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6232 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6233 rl->out_reg)))
6234 delete_output_reload (insn, j, REGNO (oldequiv));
6236 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6237 then load RELOADREG from OLDEQUIV. Note that we cannot use
6238 gen_lowpart_common since it can do the wrong thing when
6239 RELOADREG has a multi-word mode. Note that RELOADREG
6240 must always be a REG here. */
6242 if (GET_MODE (reloadreg) != mode)
6243 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6244 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6245 oldequiv = SUBREG_REG (oldequiv);
6246 if (GET_MODE (oldequiv) != VOIDmode
6247 && mode != GET_MODE (oldequiv))
6248 oldequiv = gen_rtx_SUBREG (mode, oldequiv, 0);
6250 /* Switch to the right place to emit the reload insns. */
6251 switch (rl->when_needed)
6253 case RELOAD_OTHER:
6254 where = &other_input_reload_insns;
6255 break;
6256 case RELOAD_FOR_INPUT:
6257 where = &input_reload_insns[rl->opnum];
6258 break;
6259 case RELOAD_FOR_INPUT_ADDRESS:
6260 where = &input_address_reload_insns[rl->opnum];
6261 break;
6262 case RELOAD_FOR_INPADDR_ADDRESS:
6263 where = &inpaddr_address_reload_insns[rl->opnum];
6264 break;
6265 case RELOAD_FOR_OUTPUT_ADDRESS:
6266 where = &output_address_reload_insns[rl->opnum];
6267 break;
6268 case RELOAD_FOR_OUTADDR_ADDRESS:
6269 where = &outaddr_address_reload_insns[rl->opnum];
6270 break;
6271 case RELOAD_FOR_OPERAND_ADDRESS:
6272 where = &operand_reload_insns;
6273 break;
6274 case RELOAD_FOR_OPADDR_ADDR:
6275 where = &other_operand_reload_insns;
6276 break;
6277 case RELOAD_FOR_OTHER_ADDRESS:
6278 where = &other_input_address_reload_insns;
6279 break;
6280 default:
6281 abort ();
6284 push_to_sequence (*where);
6286 /* Auto-increment addresses must be reloaded in a special way. */
6287 if (rl->out && ! rl->out_reg)
6289 /* We are not going to bother supporting the case where a
6290 incremented register can't be copied directly from
6291 OLDEQUIV since this seems highly unlikely. */
6292 if (rl->secondary_in_reload >= 0)
6293 abort ();
6295 if (reload_inherited[j])
6296 oldequiv = reloadreg;
6298 old = XEXP (rl->in_reg, 0);
6300 if (optimize && GET_CODE (oldequiv) == REG
6301 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6302 && spill_reg_store[REGNO (oldequiv)]
6303 && GET_CODE (old) == REG
6304 && (dead_or_set_p (insn,
6305 spill_reg_stored_to[REGNO (oldequiv)])
6306 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6307 old)))
6308 delete_output_reload (insn, j, REGNO (oldequiv));
6310 /* Prevent normal processing of this reload. */
6311 special = 1;
6312 /* Output a special code sequence for this case. */
6313 new_spill_reg_store[REGNO (reloadreg)]
6314 = inc_for_reload (reloadreg, oldequiv, rl->out,
6315 rl->inc);
6318 /* If we are reloading a pseudo-register that was set by the previous
6319 insn, see if we can get rid of that pseudo-register entirely
6320 by redirecting the previous insn into our reload register. */
6322 else if (optimize && GET_CODE (old) == REG
6323 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6324 && dead_or_set_p (insn, old)
6325 /* This is unsafe if some other reload
6326 uses the same reg first. */
6327 && ! conflicts_with_override (reloadreg)
6328 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
6329 rl->when_needed, old, rl->out, j, 0))
6331 rtx temp = PREV_INSN (insn);
6332 while (temp && GET_CODE (temp) == NOTE)
6333 temp = PREV_INSN (temp);
6334 if (temp
6335 && GET_CODE (temp) == INSN
6336 && GET_CODE (PATTERN (temp)) == SET
6337 && SET_DEST (PATTERN (temp)) == old
6338 /* Make sure we can access insn_operand_constraint. */
6339 && asm_noperands (PATTERN (temp)) < 0
6340 /* This is unsafe if prev insn rejects our reload reg. */
6341 && constraint_accepts_reg_p (insn_data[recog_memoized (temp)].operand[0].constraint,
6342 reloadreg)
6343 /* This is unsafe if operand occurs more than once in current
6344 insn. Perhaps some occurrences aren't reloaded. */
6345 && count_occurrences (PATTERN (insn), old, 0) == 1
6346 /* Don't risk splitting a matching pair of operands. */
6347 && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
6349 /* Store into the reload register instead of the pseudo. */
6350 SET_DEST (PATTERN (temp)) = reloadreg;
6352 /* If the previous insn is an output reload, the source is
6353 a reload register, and its spill_reg_store entry will
6354 contain the previous destination. This is now
6355 invalid. */
6356 if (GET_CODE (SET_SRC (PATTERN (temp))) == REG
6357 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6359 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6360 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6363 /* If these are the only uses of the pseudo reg,
6364 pretend for GDB it lives in the reload reg we used. */
6365 if (REG_N_DEATHS (REGNO (old)) == 1
6366 && REG_N_SETS (REGNO (old)) == 1)
6368 reg_renumber[REGNO (old)] = REGNO (rl->reg_rtx);
6369 alter_reg (REGNO (old), -1);
6371 special = 1;
6375 /* We can't do that, so output an insn to load RELOADREG. */
6377 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6378 /* If we have a secondary reload, pick up the secondary register
6379 and icode, if any. If OLDEQUIV and OLD are different or
6380 if this is an in-out reload, recompute whether or not we
6381 still need a secondary register and what the icode should
6382 be. If we still need a secondary register and the class or
6383 icode is different, go back to reloading from OLD if using
6384 OLDEQUIV means that we got the wrong type of register. We
6385 cannot have different class or icode due to an in-out reload
6386 because we don't make such reloads when both the input and
6387 output need secondary reload registers. */
6389 if (! special && rl->secondary_in_reload >= 0)
6391 rtx second_reload_reg = 0;
6392 int secondary_reload = rl->secondary_in_reload;
6393 rtx real_oldequiv = oldequiv;
6394 rtx real_old = old;
6395 rtx tmp;
6396 enum insn_code icode;
6398 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6399 and similarly for OLD.
6400 See comments in get_secondary_reload in reload.c. */
6401 /* If it is a pseudo that cannot be replaced with its
6402 equivalent MEM, we must fall back to reload_in, which
6403 will have all the necessary substitutions registered.
6404 Likewise for a pseudo that can't be replaced with its
6405 equivalent constant.
6407 Take extra care for subregs of such pseudos. Note that
6408 we cannot use reg_equiv_mem in this case because it is
6409 not in the right mode. */
6411 tmp = oldequiv;
6412 if (GET_CODE (tmp) == SUBREG)
6413 tmp = SUBREG_REG (tmp);
6414 if (GET_CODE (tmp) == REG
6415 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6416 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6417 || reg_equiv_constant[REGNO (tmp)] != 0))
6419 if (! reg_equiv_mem[REGNO (tmp)]
6420 || num_not_at_initial_offset
6421 || GET_CODE (oldequiv) == SUBREG)
6422 real_oldequiv = rl->in;
6423 else
6424 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6427 tmp = old;
6428 if (GET_CODE (tmp) == SUBREG)
6429 tmp = SUBREG_REG (tmp);
6430 if (GET_CODE (tmp) == REG
6431 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6432 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6433 || reg_equiv_constant[REGNO (tmp)] != 0))
6435 if (! reg_equiv_mem[REGNO (tmp)]
6436 || num_not_at_initial_offset
6437 || GET_CODE (old) == SUBREG)
6438 real_old = rl->in;
6439 else
6440 real_old = reg_equiv_mem[REGNO (tmp)];
6443 second_reload_reg = rld[secondary_reload].reg_rtx;
6444 icode = rl->secondary_in_icode;
6446 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6447 || (rl->in != 0 && rl->out != 0))
6449 enum reg_class new_class
6450 = SECONDARY_INPUT_RELOAD_CLASS (rl->class,
6451 mode, real_oldequiv);
6453 if (new_class == NO_REGS)
6454 second_reload_reg = 0;
6455 else
6457 enum insn_code new_icode;
6458 enum machine_mode new_mode;
6460 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
6461 REGNO (second_reload_reg)))
6462 oldequiv = old, real_oldequiv = real_old;
6463 else
6465 new_icode = reload_in_optab[(int) mode];
6466 if (new_icode != CODE_FOR_nothing
6467 && ((insn_data[(int) new_icode].operand[0].predicate
6468 && ! ((*insn_data[(int) new_icode].operand[0].predicate)
6469 (reloadreg, mode)))
6470 || (insn_data[(int) new_icode].operand[1].predicate
6471 && ! ((*insn_data[(int) new_icode].operand[1].predicate)
6472 (real_oldequiv, mode)))))
6473 new_icode = CODE_FOR_nothing;
6475 if (new_icode == CODE_FOR_nothing)
6476 new_mode = mode;
6477 else
6478 new_mode = insn_data[(int) new_icode].operand[2].mode;
6480 if (GET_MODE (second_reload_reg) != new_mode)
6482 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6483 new_mode))
6484 oldequiv = old, real_oldequiv = real_old;
6485 else
6486 second_reload_reg
6487 = gen_rtx_REG (new_mode,
6488 REGNO (second_reload_reg));
6494 /* If we still need a secondary reload register, check
6495 to see if it is being used as a scratch or intermediate
6496 register and generate code appropriately. If we need
6497 a scratch register, use REAL_OLDEQUIV since the form of
6498 the insn may depend on the actual address if it is
6499 a MEM. */
6501 if (second_reload_reg)
6503 if (icode != CODE_FOR_nothing)
6505 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6506 second_reload_reg));
6507 special = 1;
6509 else
6511 /* See if we need a scratch register to load the
6512 intermediate register (a tertiary reload). */
6513 enum insn_code tertiary_icode
6514 = rld[secondary_reload].secondary_in_icode;
6516 if (tertiary_icode != CODE_FOR_nothing)
6518 rtx third_reload_reg
6519 = rld[rld[secondary_reload].secondary_in_reload].reg_rtx;
6521 emit_insn ((GEN_FCN (tertiary_icode)
6522 (second_reload_reg, real_oldequiv,
6523 third_reload_reg)));
6525 else
6526 gen_reload (second_reload_reg, real_oldequiv,
6527 rl->opnum,
6528 rl->when_needed);
6530 oldequiv = second_reload_reg;
6534 #endif
6536 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
6538 rtx real_oldequiv = oldequiv;
6540 if ((GET_CODE (oldequiv) == REG
6541 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
6542 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
6543 || reg_equiv_constant[REGNO (oldequiv)] != 0))
6544 || (GET_CODE (oldequiv) == SUBREG
6545 && GET_CODE (SUBREG_REG (oldequiv)) == REG
6546 && (REGNO (SUBREG_REG (oldequiv))
6547 >= FIRST_PSEUDO_REGISTER)
6548 && ((reg_equiv_memory_loc
6549 [REGNO (SUBREG_REG (oldequiv))] != 0)
6550 || (reg_equiv_constant
6551 [REGNO (SUBREG_REG (oldequiv))] != 0)))
6552 || (CONSTANT_P (oldequiv)
6553 && PREFERRED_RELOAD_CLASS (oldequiv,
6554 REGNO_REG_CLASS (REGNO (reloadreg))) == NO_REGS))
6555 real_oldequiv = rl->in;
6556 gen_reload (reloadreg, real_oldequiv, rl->opnum,
6557 rl->when_needed);
6560 /* End this sequence. */
6561 *where = get_insns ();
6562 end_sequence ();
6564 /* Update reload_override_in so that delete_address_reloads_1
6565 can see the actual register usage. */
6566 if (oldequiv_reg)
6567 reload_override_in[j] = oldequiv;
6570 /* Generate insns to for the output reload RL, which is for the insn described
6571 by CHAIN and has the number J. */
6572 static void
6573 emit_output_reload_insns (chain, rl, j)
6574 struct insn_chain *chain;
6575 struct reload *rl;
6576 int j;
6578 rtx reloadreg = rl->reg_rtx;
6579 rtx insn = chain->insn;
6580 int special = 0;
6581 rtx old = rl->out;
6582 enum machine_mode mode = GET_MODE (old);
6583 rtx p;
6585 if (rl->when_needed == RELOAD_OTHER)
6586 start_sequence ();
6587 else
6588 push_to_sequence (output_reload_insns[rl->opnum]);
6590 /* Determine the mode to reload in.
6591 See comments above (for input reloading). */
6593 if (mode == VOIDmode)
6595 /* VOIDmode should never happen for an output. */
6596 if (asm_noperands (PATTERN (insn)) < 0)
6597 /* It's the compiler's fault. */
6598 fatal_insn ("VOIDmode on an output", insn);
6599 error_for_asm (insn, "output operand is constant in `asm'");
6600 /* Prevent crash--use something we know is valid. */
6601 mode = word_mode;
6602 old = gen_rtx_REG (mode, REGNO (reloadreg));
6605 if (GET_MODE (reloadreg) != mode)
6606 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6608 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
6610 /* If we need two reload regs, set RELOADREG to the intermediate
6611 one, since it will be stored into OLD. We might need a secondary
6612 register only for an input reload, so check again here. */
6614 if (rl->secondary_out_reload >= 0)
6616 rtx real_old = old;
6618 if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
6619 && reg_equiv_mem[REGNO (old)] != 0)
6620 real_old = reg_equiv_mem[REGNO (old)];
6622 if ((SECONDARY_OUTPUT_RELOAD_CLASS (rl->class,
6623 mode, real_old)
6624 != NO_REGS))
6626 rtx second_reloadreg = reloadreg;
6627 reloadreg = rld[rl->secondary_out_reload].reg_rtx;
6629 /* See if RELOADREG is to be used as a scratch register
6630 or as an intermediate register. */
6631 if (rl->secondary_out_icode != CODE_FOR_nothing)
6633 emit_insn ((GEN_FCN (rl->secondary_out_icode)
6634 (real_old, second_reloadreg, reloadreg)));
6635 special = 1;
6637 else
6639 /* See if we need both a scratch and intermediate reload
6640 register. */
6642 int secondary_reload = rl->secondary_out_reload;
6643 enum insn_code tertiary_icode
6644 = rld[secondary_reload].secondary_out_icode;
6646 if (GET_MODE (reloadreg) != mode)
6647 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6649 if (tertiary_icode != CODE_FOR_nothing)
6651 rtx third_reloadreg
6652 = rld[rld[secondary_reload].secondary_out_reload].reg_rtx;
6653 rtx tem;
6655 /* Copy primary reload reg to secondary reload reg.
6656 (Note that these have been swapped above, then
6657 secondary reload reg to OLD using our insn. */
6659 /* If REAL_OLD is a paradoxical SUBREG, remove it
6660 and try to put the opposite SUBREG on
6661 RELOADREG. */
6662 if (GET_CODE (real_old) == SUBREG
6663 && (GET_MODE_SIZE (GET_MODE (real_old))
6664 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
6665 && 0 != (tem = gen_lowpart_common
6666 (GET_MODE (SUBREG_REG (real_old)),
6667 reloadreg)))
6668 real_old = SUBREG_REG (real_old), reloadreg = tem;
6670 gen_reload (reloadreg, second_reloadreg,
6671 rl->opnum, rl->when_needed);
6672 emit_insn ((GEN_FCN (tertiary_icode)
6673 (real_old, reloadreg, third_reloadreg)));
6674 special = 1;
6677 else
6678 /* Copy between the reload regs here and then to
6679 OUT later. */
6681 gen_reload (reloadreg, second_reloadreg,
6682 rl->opnum, rl->when_needed);
6686 #endif
6688 /* Output the last reload insn. */
6689 if (! special)
6691 rtx set;
6693 /* Don't output the last reload if OLD is not the dest of
6694 INSN and is in the src and is clobbered by INSN. */
6695 if (! flag_expensive_optimizations
6696 || GET_CODE (old) != REG
6697 || !(set = single_set (insn))
6698 || rtx_equal_p (old, SET_DEST (set))
6699 || !reg_mentioned_p (old, SET_SRC (set))
6700 || !regno_clobbered_p (REGNO (old), insn, rl->mode, 0))
6701 gen_reload (old, reloadreg, rl->opnum,
6702 rl->when_needed);
6705 /* Look at all insns we emitted, just to be safe. */
6706 for (p = get_insns (); p; p = NEXT_INSN (p))
6707 if (INSN_P (p))
6709 rtx pat = PATTERN (p);
6711 /* If this output reload doesn't come from a spill reg,
6712 clear any memory of reloaded copies of the pseudo reg.
6713 If this output reload comes from a spill reg,
6714 reg_has_output_reload will make this do nothing. */
6715 note_stores (pat, forget_old_reloads_1, NULL);
6717 if (reg_mentioned_p (rl->reg_rtx, pat))
6719 rtx set = single_set (insn);
6720 if (reload_spill_index[j] < 0
6721 && set
6722 && SET_SRC (set) == rl->reg_rtx)
6724 int src = REGNO (SET_SRC (set));
6726 reload_spill_index[j] = src;
6727 SET_HARD_REG_BIT (reg_is_output_reload, src);
6728 if (find_regno_note (insn, REG_DEAD, src))
6729 SET_HARD_REG_BIT (reg_reloaded_died, src);
6731 if (REGNO (rl->reg_rtx) < FIRST_PSEUDO_REGISTER)
6733 int s = rl->secondary_out_reload;
6734 set = single_set (p);
6735 /* If this reload copies only to the secondary reload
6736 register, the secondary reload does the actual
6737 store. */
6738 if (s >= 0 && set == NULL_RTX)
6739 /* We can't tell what function the secondary reload
6740 has and where the actual store to the pseudo is
6741 made; leave new_spill_reg_store alone. */
6743 else if (s >= 0
6744 && SET_SRC (set) == rl->reg_rtx
6745 && SET_DEST (set) == rld[s].reg_rtx)
6747 /* Usually the next instruction will be the
6748 secondary reload insn; if we can confirm
6749 that it is, setting new_spill_reg_store to
6750 that insn will allow an extra optimization. */
6751 rtx s_reg = rld[s].reg_rtx;
6752 rtx next = NEXT_INSN (p);
6753 rld[s].out = rl->out;
6754 rld[s].out_reg = rl->out_reg;
6755 set = single_set (next);
6756 if (set && SET_SRC (set) == s_reg
6757 && ! new_spill_reg_store[REGNO (s_reg)])
6759 SET_HARD_REG_BIT (reg_is_output_reload,
6760 REGNO (s_reg));
6761 new_spill_reg_store[REGNO (s_reg)] = next;
6764 else
6765 new_spill_reg_store[REGNO (rl->reg_rtx)] = p;
6770 if (rl->when_needed == RELOAD_OTHER)
6772 emit_insns (other_output_reload_insns[rl->opnum]);
6773 other_output_reload_insns[rl->opnum] = get_insns ();
6775 else
6776 output_reload_insns[rl->opnum] = get_insns ();
6778 end_sequence ();
6781 /* Do input reloading for reload RL, which is for the insn described by CHAIN
6782 and has the number J. */
6783 static void
6784 do_input_reload (chain, rl, j)
6785 struct insn_chain *chain;
6786 struct reload *rl;
6787 int j;
6789 int expect_occurrences = 1;
6790 rtx insn = chain->insn;
6791 rtx old = (rl->in && GET_CODE (rl->in) == MEM
6792 ? rl->in_reg : rl->in);
6794 if (old != 0
6795 /* AUTO_INC reloads need to be handled even if inherited. We got an
6796 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
6797 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
6798 && ! rtx_equal_p (rl->reg_rtx, old)
6799 && rl->reg_rtx != 0)
6800 emit_input_reload_insns (chain, rld + j, old, j);
6802 /* When inheriting a wider reload, we have a MEM in rl->in,
6803 e.g. inheriting a SImode output reload for
6804 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
6805 if (optimize && reload_inherited[j] && rl->in
6806 && GET_CODE (rl->in) == MEM
6807 && GET_CODE (rl->in_reg) == MEM
6808 && reload_spill_index[j] >= 0
6809 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
6811 expect_occurrences
6812 = count_occurrences (PATTERN (insn), rl->in, 0) == 1 ? 0 : -1;
6813 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
6816 /* If we are reloading a register that was recently stored in with an
6817 output-reload, see if we can prove there was
6818 actually no need to store the old value in it. */
6820 if (optimize
6821 && (reload_inherited[j] || reload_override_in[j])
6822 && rl->reg_rtx
6823 && GET_CODE (rl->reg_rtx) == REG
6824 && spill_reg_store[REGNO (rl->reg_rtx)] != 0
6825 #if 0
6826 /* There doesn't seem to be any reason to restrict this to pseudos
6827 and doing so loses in the case where we are copying from a
6828 register of the wrong class. */
6829 && (REGNO (spill_reg_stored_to[REGNO (rl->reg_rtx)])
6830 >= FIRST_PSEUDO_REGISTER)
6831 #endif
6832 /* The insn might have already some references to stackslots
6833 replaced by MEMs, while reload_out_reg still names the
6834 original pseudo. */
6835 && (dead_or_set_p (insn,
6836 spill_reg_stored_to[REGNO (rl->reg_rtx)])
6837 || rtx_equal_p (spill_reg_stored_to[REGNO (rl->reg_rtx)],
6838 rl->out_reg)))
6839 delete_output_reload (insn, j, REGNO (rl->reg_rtx));
6842 /* Do output reloading for reload RL, which is for the insn described by
6843 CHAIN and has the number J.
6844 ??? At some point we need to support handling output reloads of
6845 JUMP_INSNs or insns that set cc0. */
6846 static void
6847 do_output_reload (chain, rl, j)
6848 struct insn_chain *chain;
6849 struct reload *rl;
6850 int j;
6852 rtx note, old;
6853 rtx insn = chain->insn;
6854 /* If this is an output reload that stores something that is
6855 not loaded in this same reload, see if we can eliminate a previous
6856 store. */
6857 rtx pseudo = rl->out_reg;
6859 if (pseudo
6860 && GET_CODE (pseudo) == REG
6861 && ! rtx_equal_p (rl->in_reg, pseudo)
6862 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
6863 && reg_last_reload_reg[REGNO (pseudo)])
6865 int pseudo_no = REGNO (pseudo);
6866 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
6868 /* We don't need to test full validity of last_regno for
6869 inherit here; we only want to know if the store actually
6870 matches the pseudo. */
6871 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
6872 && reg_reloaded_contents[last_regno] == pseudo_no
6873 && spill_reg_store[last_regno]
6874 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
6875 delete_output_reload (insn, j, last_regno);
6878 old = rl->out_reg;
6879 if (old == 0
6880 || rl->reg_rtx == old
6881 || rl->reg_rtx == 0)
6882 return;
6884 /* An output operand that dies right away does need a reload,
6885 but need not be copied from it. Show the new location in the
6886 REG_UNUSED note. */
6887 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
6888 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
6890 XEXP (note, 0) = rl->reg_rtx;
6891 return;
6893 /* Likewise for a SUBREG of an operand that dies. */
6894 else if (GET_CODE (old) == SUBREG
6895 && GET_CODE (SUBREG_REG (old)) == REG
6896 && 0 != (note = find_reg_note (insn, REG_UNUSED,
6897 SUBREG_REG (old))))
6899 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
6900 rl->reg_rtx);
6901 return;
6903 else if (GET_CODE (old) == SCRATCH)
6904 /* If we aren't optimizing, there won't be a REG_UNUSED note,
6905 but we don't want to make an output reload. */
6906 return;
6908 /* If is a JUMP_INSN, we can't support output reloads yet. */
6909 if (GET_CODE (insn) == JUMP_INSN)
6910 abort ();
6912 emit_output_reload_insns (chain, rld + j, j);
6915 /* Output insns to reload values in and out of the chosen reload regs. */
6917 static void
6918 emit_reload_insns (chain)
6919 struct insn_chain *chain;
6921 rtx insn = chain->insn;
6923 register int j;
6924 rtx following_insn = NEXT_INSN (insn);
6925 rtx before_insn = PREV_INSN (insn);
6927 CLEAR_HARD_REG_SET (reg_reloaded_died);
6929 for (j = 0; j < reload_n_operands; j++)
6930 input_reload_insns[j] = input_address_reload_insns[j]
6931 = inpaddr_address_reload_insns[j]
6932 = output_reload_insns[j] = output_address_reload_insns[j]
6933 = outaddr_address_reload_insns[j]
6934 = other_output_reload_insns[j] = 0;
6935 other_input_address_reload_insns = 0;
6936 other_input_reload_insns = 0;
6937 operand_reload_insns = 0;
6938 other_operand_reload_insns = 0;
6940 /* Dump reloads into the dump file. */
6941 if (rtl_dump_file)
6943 fprintf (rtl_dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
6944 debug_reload_to_stream (rtl_dump_file);
6947 /* Now output the instructions to copy the data into and out of the
6948 reload registers. Do these in the order that the reloads were reported,
6949 since reloads of base and index registers precede reloads of operands
6950 and the operands may need the base and index registers reloaded. */
6952 for (j = 0; j < n_reloads; j++)
6954 if (rld[j].reg_rtx
6955 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
6956 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
6958 do_input_reload (chain, rld + j, j);
6959 do_output_reload (chain, rld + j, j);
6962 /* Now write all the insns we made for reloads in the order expected by
6963 the allocation functions. Prior to the insn being reloaded, we write
6964 the following reloads:
6966 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
6968 RELOAD_OTHER reloads.
6970 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
6971 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
6972 RELOAD_FOR_INPUT reload for the operand.
6974 RELOAD_FOR_OPADDR_ADDRS reloads.
6976 RELOAD_FOR_OPERAND_ADDRESS reloads.
6978 After the insn being reloaded, we write the following:
6980 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
6981 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
6982 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
6983 reloads for the operand. The RELOAD_OTHER output reloads are
6984 output in descending order by reload number. */
6986 emit_insns_before (other_input_address_reload_insns, insn);
6987 emit_insns_before (other_input_reload_insns, insn);
6989 for (j = 0; j < reload_n_operands; j++)
6991 emit_insns_before (inpaddr_address_reload_insns[j], insn);
6992 emit_insns_before (input_address_reload_insns[j], insn);
6993 emit_insns_before (input_reload_insns[j], insn);
6996 emit_insns_before (other_operand_reload_insns, insn);
6997 emit_insns_before (operand_reload_insns, insn);
6999 for (j = 0; j < reload_n_operands; j++)
7001 emit_insns_before (outaddr_address_reload_insns[j], following_insn);
7002 emit_insns_before (output_address_reload_insns[j], following_insn);
7003 emit_insns_before (output_reload_insns[j], following_insn);
7004 emit_insns_before (other_output_reload_insns[j], following_insn);
7007 /* Keep basic block info up to date. */
7008 if (n_basic_blocks)
7010 if (BLOCK_HEAD (chain->block) == insn)
7011 BLOCK_HEAD (chain->block) = NEXT_INSN (before_insn);
7012 if (BLOCK_END (chain->block) == insn)
7013 BLOCK_END (chain->block) = PREV_INSN (following_insn);
7016 /* For all the spill regs newly reloaded in this instruction,
7017 record what they were reloaded from, so subsequent instructions
7018 can inherit the reloads.
7020 Update spill_reg_store for the reloads of this insn.
7021 Copy the elements that were updated in the loop above. */
7023 for (j = 0; j < n_reloads; j++)
7025 register int r = reload_order[j];
7026 register int i = reload_spill_index[r];
7028 /* If this is a non-inherited input reload from a pseudo, we must
7029 clear any memory of a previous store to the same pseudo. Only do
7030 something if there will not be an output reload for the pseudo
7031 being reloaded. */
7032 if (rld[r].in_reg != 0
7033 && ! (reload_inherited[r] || reload_override_in[r]))
7035 rtx reg = rld[r].in_reg;
7037 if (GET_CODE (reg) == SUBREG)
7038 reg = SUBREG_REG (reg);
7040 if (GET_CODE (reg) == REG
7041 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7042 && ! reg_has_output_reload[REGNO (reg)])
7044 int nregno = REGNO (reg);
7046 if (reg_last_reload_reg[nregno])
7048 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7050 if (reg_reloaded_contents[last_regno] == nregno)
7051 spill_reg_store[last_regno] = 0;
7056 /* I is nonneg if this reload used a register.
7057 If rld[r].reg_rtx is 0, this is an optional reload
7058 that we opted to ignore. */
7060 if (i >= 0 && rld[r].reg_rtx != 0)
7062 int nr = HARD_REGNO_NREGS (i, GET_MODE (rld[r].reg_rtx));
7063 int k;
7064 int part_reaches_end = 0;
7065 int all_reaches_end = 1;
7067 /* For a multi register reload, we need to check if all or part
7068 of the value lives to the end. */
7069 for (k = 0; k < nr; k++)
7071 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7072 rld[r].when_needed))
7073 part_reaches_end = 1;
7074 else
7075 all_reaches_end = 0;
7078 /* Ignore reloads that don't reach the end of the insn in
7079 entirety. */
7080 if (all_reaches_end)
7082 /* First, clear out memory of what used to be in this spill reg.
7083 If consecutive registers are used, clear them all. */
7085 for (k = 0; k < nr; k++)
7086 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7088 /* Maybe the spill reg contains a copy of reload_out. */
7089 if (rld[r].out != 0
7090 && (GET_CODE (rld[r].out) == REG
7091 #ifdef AUTO_INC_DEC
7092 || ! rld[r].out_reg
7093 #endif
7094 || GET_CODE (rld[r].out_reg) == REG))
7096 rtx out = (GET_CODE (rld[r].out) == REG
7097 ? rld[r].out
7098 : rld[r].out_reg
7099 ? rld[r].out_reg
7100 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7101 register int nregno = REGNO (out);
7102 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7103 : HARD_REGNO_NREGS (nregno,
7104 GET_MODE (rld[r].reg_rtx)));
7106 spill_reg_store[i] = new_spill_reg_store[i];
7107 spill_reg_stored_to[i] = out;
7108 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7110 /* If NREGNO is a hard register, it may occupy more than
7111 one register. If it does, say what is in the
7112 rest of the registers assuming that both registers
7113 agree on how many words the object takes. If not,
7114 invalidate the subsequent registers. */
7116 if (nregno < FIRST_PSEUDO_REGISTER)
7117 for (k = 1; k < nnr; k++)
7118 reg_last_reload_reg[nregno + k]
7119 = (nr == nnr
7120 ? gen_rtx_REG (reg_raw_mode[REGNO (rld[r].reg_rtx) + k],
7121 REGNO (rld[r].reg_rtx) + k)
7122 : 0);
7124 /* Now do the inverse operation. */
7125 for (k = 0; k < nr; k++)
7127 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7128 reg_reloaded_contents[i + k]
7129 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7130 ? nregno
7131 : nregno + k);
7132 reg_reloaded_insn[i + k] = insn;
7133 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7137 /* Maybe the spill reg contains a copy of reload_in. Only do
7138 something if there will not be an output reload for
7139 the register being reloaded. */
7140 else if (rld[r].out_reg == 0
7141 && rld[r].in != 0
7142 && ((GET_CODE (rld[r].in) == REG
7143 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7144 && ! reg_has_output_reload[REGNO (rld[r].in)])
7145 || (GET_CODE (rld[r].in_reg) == REG
7146 && ! reg_has_output_reload[REGNO (rld[r].in_reg)]))
7147 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7149 register int nregno;
7150 int nnr;
7152 if (GET_CODE (rld[r].in) == REG
7153 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7154 nregno = REGNO (rld[r].in);
7155 else if (GET_CODE (rld[r].in_reg) == REG)
7156 nregno = REGNO (rld[r].in_reg);
7157 else
7158 nregno = REGNO (XEXP (rld[r].in_reg, 0));
7160 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7161 : HARD_REGNO_NREGS (nregno,
7162 GET_MODE (rld[r].reg_rtx)));
7164 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7166 if (nregno < FIRST_PSEUDO_REGISTER)
7167 for (k = 1; k < nnr; k++)
7168 reg_last_reload_reg[nregno + k]
7169 = (nr == nnr
7170 ? gen_rtx_REG (reg_raw_mode[REGNO (rld[r].reg_rtx) + k],
7171 REGNO (rld[r].reg_rtx) + k)
7172 : 0);
7174 /* Unless we inherited this reload, show we haven't
7175 recently done a store.
7176 Previous stores of inherited auto_inc expressions
7177 also have to be discarded. */
7178 if (! reload_inherited[r]
7179 || (rld[r].out && ! rld[r].out_reg))
7180 spill_reg_store[i] = 0;
7182 for (k = 0; k < nr; k++)
7184 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7185 reg_reloaded_contents[i + k]
7186 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7187 ? nregno
7188 : nregno + k);
7189 reg_reloaded_insn[i + k] = insn;
7190 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7195 /* However, if part of the reload reaches the end, then we must
7196 invalidate the old info for the part that survives to the end. */
7197 else if (part_reaches_end)
7199 for (k = 0; k < nr; k++)
7200 if (reload_reg_reaches_end_p (i + k,
7201 rld[r].opnum,
7202 rld[r].when_needed))
7203 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7207 /* The following if-statement was #if 0'd in 1.34 (or before...).
7208 It's reenabled in 1.35 because supposedly nothing else
7209 deals with this problem. */
7211 /* If a register gets output-reloaded from a non-spill register,
7212 that invalidates any previous reloaded copy of it.
7213 But forget_old_reloads_1 won't get to see it, because
7214 it thinks only about the original insn. So invalidate it here. */
7215 if (i < 0 && rld[r].out != 0
7216 && (GET_CODE (rld[r].out) == REG
7217 || (GET_CODE (rld[r].out) == MEM
7218 && GET_CODE (rld[r].out_reg) == REG)))
7220 rtx out = (GET_CODE (rld[r].out) == REG
7221 ? rld[r].out : rld[r].out_reg);
7222 register int nregno = REGNO (out);
7223 if (nregno >= FIRST_PSEUDO_REGISTER)
7225 rtx src_reg, store_insn = NULL_RTX;
7227 reg_last_reload_reg[nregno] = 0;
7229 /* If we can find a hard register that is stored, record
7230 the storing insn so that we may delete this insn with
7231 delete_output_reload. */
7232 src_reg = rld[r].reg_rtx;
7234 /* If this is an optional reload, try to find the source reg
7235 from an input reload. */
7236 if (! src_reg)
7238 rtx set = single_set (insn);
7239 if (set && SET_DEST (set) == rld[r].out)
7241 int k;
7243 src_reg = SET_SRC (set);
7244 store_insn = insn;
7245 for (k = 0; k < n_reloads; k++)
7247 if (rld[k].in == src_reg)
7249 src_reg = rld[k].reg_rtx;
7250 break;
7255 else
7256 store_insn = new_spill_reg_store[REGNO (src_reg)];
7257 if (src_reg && GET_CODE (src_reg) == REG
7258 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7260 int src_regno = REGNO (src_reg);
7261 int nr = HARD_REGNO_NREGS (src_regno, rld[r].mode);
7262 /* The place where to find a death note varies with
7263 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7264 necessarily checked exactly in the code that moves
7265 notes, so just check both locations. */
7266 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7267 if (! note)
7268 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7269 while (nr-- > 0)
7271 spill_reg_store[src_regno + nr] = store_insn;
7272 spill_reg_stored_to[src_regno + nr] = out;
7273 reg_reloaded_contents[src_regno + nr] = nregno;
7274 reg_reloaded_insn[src_regno + nr] = store_insn;
7275 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7276 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7277 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7278 if (note)
7279 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7280 else
7281 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7283 reg_last_reload_reg[nregno] = src_reg;
7286 else
7288 int num_regs = HARD_REGNO_NREGS (nregno, GET_MODE (rld[r].out));
7290 while (num_regs-- > 0)
7291 reg_last_reload_reg[nregno + num_regs] = 0;
7295 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7298 /* Emit code to perform a reload from IN (which may be a reload register) to
7299 OUT (which may also be a reload register). IN or OUT is from operand
7300 OPNUM with reload type TYPE.
7302 Returns first insn emitted. */
7305 gen_reload (out, in, opnum, type)
7306 rtx out;
7307 rtx in;
7308 int opnum;
7309 enum reload_type type;
7311 rtx last = get_last_insn ();
7312 rtx tem;
7314 /* If IN is a paradoxical SUBREG, remove it and try to put the
7315 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7316 if (GET_CODE (in) == SUBREG
7317 && (GET_MODE_SIZE (GET_MODE (in))
7318 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7319 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7320 in = SUBREG_REG (in), out = tem;
7321 else if (GET_CODE (out) == SUBREG
7322 && (GET_MODE_SIZE (GET_MODE (out))
7323 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7324 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7325 out = SUBREG_REG (out), in = tem;
7327 /* How to do this reload can get quite tricky. Normally, we are being
7328 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7329 register that didn't get a hard register. In that case we can just
7330 call emit_move_insn.
7332 We can also be asked to reload a PLUS that adds a register or a MEM to
7333 another register, constant or MEM. This can occur during frame pointer
7334 elimination and while reloading addresses. This case is handled by
7335 trying to emit a single insn to perform the add. If it is not valid,
7336 we use a two insn sequence.
7338 Finally, we could be called to handle an 'o' constraint by putting
7339 an address into a register. In that case, we first try to do this
7340 with a named pattern of "reload_load_address". If no such pattern
7341 exists, we just emit a SET insn and hope for the best (it will normally
7342 be valid on machines that use 'o').
7344 This entire process is made complex because reload will never
7345 process the insns we generate here and so we must ensure that
7346 they will fit their constraints and also by the fact that parts of
7347 IN might be being reloaded separately and replaced with spill registers.
7348 Because of this, we are, in some sense, just guessing the right approach
7349 here. The one listed above seems to work.
7351 ??? At some point, this whole thing needs to be rethought. */
7353 if (GET_CODE (in) == PLUS
7354 && (GET_CODE (XEXP (in, 0)) == REG
7355 || GET_CODE (XEXP (in, 0)) == SUBREG
7356 || GET_CODE (XEXP (in, 0)) == MEM)
7357 && (GET_CODE (XEXP (in, 1)) == REG
7358 || GET_CODE (XEXP (in, 1)) == SUBREG
7359 || CONSTANT_P (XEXP (in, 1))
7360 || GET_CODE (XEXP (in, 1)) == MEM))
7362 /* We need to compute the sum of a register or a MEM and another
7363 register, constant, or MEM, and put it into the reload
7364 register. The best possible way of doing this is if the machine
7365 has a three-operand ADD insn that accepts the required operands.
7367 The simplest approach is to try to generate such an insn and see if it
7368 is recognized and matches its constraints. If so, it can be used.
7370 It might be better not to actually emit the insn unless it is valid,
7371 but we need to pass the insn as an operand to `recog' and
7372 `extract_insn' and it is simpler to emit and then delete the insn if
7373 not valid than to dummy things up. */
7375 rtx op0, op1, tem, insn;
7376 int code;
7378 op0 = find_replacement (&XEXP (in, 0));
7379 op1 = find_replacement (&XEXP (in, 1));
7381 /* Since constraint checking is strict, commutativity won't be
7382 checked, so we need to do that here to avoid spurious failure
7383 if the add instruction is two-address and the second operand
7384 of the add is the same as the reload reg, which is frequently
7385 the case. If the insn would be A = B + A, rearrange it so
7386 it will be A = A + B as constrain_operands expects. */
7388 if (GET_CODE (XEXP (in, 1)) == REG
7389 && REGNO (out) == REGNO (XEXP (in, 1)))
7390 tem = op0, op0 = op1, op1 = tem;
7392 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7393 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7395 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
7396 code = recog_memoized (insn);
7398 if (code >= 0)
7400 extract_insn (insn);
7401 /* We want constrain operands to treat this insn strictly in
7402 its validity determination, i.e., the way it would after reload
7403 has completed. */
7404 if (constrain_operands (1))
7405 return insn;
7408 delete_insns_since (last);
7410 /* If that failed, we must use a conservative two-insn sequence.
7412 Use a move to copy one operand into the reload register. Prefer
7413 to reload a constant, MEM or pseudo since the move patterns can
7414 handle an arbitrary operand. If OP1 is not a constant, MEM or
7415 pseudo and OP1 is not a valid operand for an add instruction, then
7416 reload OP1.
7418 After reloading one of the operands into the reload register, add
7419 the reload register to the output register.
7421 If there is another way to do this for a specific machine, a
7422 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7423 we emit below. */
7425 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7427 if (CONSTANT_P (op1) || GET_CODE (op1) == MEM || GET_CODE (op1) == SUBREG
7428 || (GET_CODE (op1) == REG
7429 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7430 || (code != CODE_FOR_nothing
7431 && ! ((*insn_data[code].operand[2].predicate)
7432 (op1, insn_data[code].operand[2].mode))))
7433 tem = op0, op0 = op1, op1 = tem;
7435 gen_reload (out, op0, opnum, type);
7437 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7438 This fixes a problem on the 32K where the stack pointer cannot
7439 be used as an operand of an add insn. */
7441 if (rtx_equal_p (op0, op1))
7442 op1 = out;
7444 insn = emit_insn (gen_add2_insn (out, op1));
7446 /* If that failed, copy the address register to the reload register.
7447 Then add the constant to the reload register. */
7449 code = recog_memoized (insn);
7451 if (code >= 0)
7453 extract_insn (insn);
7454 /* We want constrain operands to treat this insn strictly in
7455 its validity determination, i.e., the way it would after reload
7456 has completed. */
7457 if (constrain_operands (1))
7459 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7460 REG_NOTES (insn)
7461 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7462 return insn;
7466 delete_insns_since (last);
7468 gen_reload (out, op1, opnum, type);
7469 insn = emit_insn (gen_add2_insn (out, op0));
7470 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7473 #ifdef SECONDARY_MEMORY_NEEDED
7474 /* If we need a memory location to do the move, do it that way. */
7475 else if (GET_CODE (in) == REG && REGNO (in) < FIRST_PSEUDO_REGISTER
7476 && GET_CODE (out) == REG && REGNO (out) < FIRST_PSEUDO_REGISTER
7477 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (in)),
7478 REGNO_REG_CLASS (REGNO (out)),
7479 GET_MODE (out)))
7481 /* Get the memory to use and rewrite both registers to its mode. */
7482 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7484 if (GET_MODE (loc) != GET_MODE (out))
7485 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7487 if (GET_MODE (loc) != GET_MODE (in))
7488 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7490 gen_reload (loc, in, opnum, type);
7491 gen_reload (out, loc, opnum, type);
7493 #endif
7495 /* If IN is a simple operand, use gen_move_insn. */
7496 else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
7497 emit_insn (gen_move_insn (out, in));
7499 #ifdef HAVE_reload_load_address
7500 else if (HAVE_reload_load_address)
7501 emit_insn (gen_reload_load_address (out, in));
7502 #endif
7504 /* Otherwise, just write (set OUT IN) and hope for the best. */
7505 else
7506 emit_insn (gen_rtx_SET (VOIDmode, out, in));
7508 /* Return the first insn emitted.
7509 We can not just return get_last_insn, because there may have
7510 been multiple instructions emitted. Also note that gen_move_insn may
7511 emit more than one insn itself, so we can not assume that there is one
7512 insn emitted per emit_insn_before call. */
7514 return last ? NEXT_INSN (last) : get_insns ();
7517 /* Delete a previously made output-reload
7518 whose result we now believe is not needed.
7519 First we double-check.
7521 INSN is the insn now being processed.
7522 LAST_RELOAD_REG is the hard register number for which we want to delete
7523 the last output reload.
7524 J is the reload-number that originally used REG. The caller has made
7525 certain that reload J doesn't use REG any longer for input. */
7527 static void
7528 delete_output_reload (insn, j, last_reload_reg)
7529 rtx insn;
7530 int j;
7531 int last_reload_reg;
7533 rtx output_reload_insn = spill_reg_store[last_reload_reg];
7534 rtx reg = spill_reg_stored_to[last_reload_reg];
7535 int k;
7536 int n_occurrences;
7537 int n_inherited = 0;
7538 register rtx i1;
7539 rtx substed;
7541 /* Get the raw pseudo-register referred to. */
7543 while (GET_CODE (reg) == SUBREG)
7544 reg = SUBREG_REG (reg);
7545 substed = reg_equiv_memory_loc[REGNO (reg)];
7547 /* This is unsafe if the operand occurs more often in the current
7548 insn than it is inherited. */
7549 for (k = n_reloads - 1; k >= 0; k--)
7551 rtx reg2 = rld[k].in;
7552 if (! reg2)
7553 continue;
7554 if (GET_CODE (reg2) == MEM || reload_override_in[k])
7555 reg2 = rld[k].in_reg;
7556 #ifdef AUTO_INC_DEC
7557 if (rld[k].out && ! rld[k].out_reg)
7558 reg2 = XEXP (rld[k].in_reg, 0);
7559 #endif
7560 while (GET_CODE (reg2) == SUBREG)
7561 reg2 = SUBREG_REG (reg2);
7562 if (rtx_equal_p (reg2, reg))
7564 if (reload_inherited[k] || reload_override_in[k] || k == j)
7566 n_inherited++;
7567 reg2 = rld[k].out_reg;
7568 if (! reg2)
7569 continue;
7570 while (GET_CODE (reg2) == SUBREG)
7571 reg2 = XEXP (reg2, 0);
7572 if (rtx_equal_p (reg2, reg))
7573 n_inherited++;
7575 else
7576 return;
7579 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
7580 if (substed)
7581 n_occurrences += count_occurrences (PATTERN (insn), substed, 0);
7582 if (n_occurrences > n_inherited)
7583 return;
7585 /* If the pseudo-reg we are reloading is no longer referenced
7586 anywhere between the store into it and here,
7587 and no jumps or labels intervene, then the value can get
7588 here through the reload reg alone.
7589 Otherwise, give up--return. */
7590 for (i1 = NEXT_INSN (output_reload_insn);
7591 i1 != insn; i1 = NEXT_INSN (i1))
7593 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
7594 return;
7595 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
7596 && reg_mentioned_p (reg, PATTERN (i1)))
7598 /* If this is USE in front of INSN, we only have to check that
7599 there are no more references than accounted for by inheritance. */
7600 while (GET_CODE (i1) == INSN && GET_CODE (PATTERN (i1)) == USE)
7602 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
7603 i1 = NEXT_INSN (i1);
7605 if (n_occurrences <= n_inherited && i1 == insn)
7606 break;
7607 return;
7611 /* The caller has already checked that REG dies or is set in INSN.
7612 It has also checked that we are optimizing, and thus some inaccurancies
7613 in the debugging information are acceptable.
7614 So we could just delete output_reload_insn.
7615 But in some cases we can improve the debugging information without
7616 sacrificing optimization - maybe even improving the code:
7617 See if the pseudo reg has been completely replaced
7618 with reload regs. If so, delete the store insn
7619 and forget we had a stack slot for the pseudo. */
7620 if (rld[j].out != rld[j].in
7621 && REG_N_DEATHS (REGNO (reg)) == 1
7622 && REG_N_SETS (REGNO (reg)) == 1
7623 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
7624 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
7626 rtx i2;
7628 /* We know that it was used only between here
7629 and the beginning of the current basic block.
7630 (We also know that the last use before INSN was
7631 the output reload we are thinking of deleting, but never mind that.)
7632 Search that range; see if any ref remains. */
7633 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7635 rtx set = single_set (i2);
7637 /* Uses which just store in the pseudo don't count,
7638 since if they are the only uses, they are dead. */
7639 if (set != 0 && SET_DEST (set) == reg)
7640 continue;
7641 if (GET_CODE (i2) == CODE_LABEL
7642 || GET_CODE (i2) == JUMP_INSN)
7643 break;
7644 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
7645 && reg_mentioned_p (reg, PATTERN (i2)))
7647 /* Some other ref remains; just delete the output reload we
7648 know to be dead. */
7649 delete_address_reloads (output_reload_insn, insn);
7650 PUT_CODE (output_reload_insn, NOTE);
7651 NOTE_SOURCE_FILE (output_reload_insn) = 0;
7652 NOTE_LINE_NUMBER (output_reload_insn) = NOTE_INSN_DELETED;
7653 return;
7657 /* Delete the now-dead stores into this pseudo. */
7658 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7660 rtx set = single_set (i2);
7662 if (set != 0 && SET_DEST (set) == reg)
7664 delete_address_reloads (i2, insn);
7665 /* This might be a basic block head,
7666 thus don't use delete_insn. */
7667 PUT_CODE (i2, NOTE);
7668 NOTE_SOURCE_FILE (i2) = 0;
7669 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
7671 if (GET_CODE (i2) == CODE_LABEL
7672 || GET_CODE (i2) == JUMP_INSN)
7673 break;
7676 /* For the debugging info,
7677 say the pseudo lives in this reload reg. */
7678 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
7679 alter_reg (REGNO (reg), -1);
7681 delete_address_reloads (output_reload_insn, insn);
7682 PUT_CODE (output_reload_insn, NOTE);
7683 NOTE_SOURCE_FILE (output_reload_insn) = 0;
7684 NOTE_LINE_NUMBER (output_reload_insn) = NOTE_INSN_DELETED;
7688 /* We are going to delete DEAD_INSN. Recursively delete loads of
7689 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
7690 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
7691 static void
7692 delete_address_reloads (dead_insn, current_insn)
7693 rtx dead_insn, current_insn;
7695 rtx set = single_set (dead_insn);
7696 rtx set2, dst, prev, next;
7697 if (set)
7699 rtx dst = SET_DEST (set);
7700 if (GET_CODE (dst) == MEM)
7701 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
7703 /* If we deleted the store from a reloaded post_{in,de}c expression,
7704 we can delete the matching adds. */
7705 prev = PREV_INSN (dead_insn);
7706 next = NEXT_INSN (dead_insn);
7707 if (! prev || ! next)
7708 return;
7709 set = single_set (next);
7710 set2 = single_set (prev);
7711 if (! set || ! set2
7712 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
7713 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
7714 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
7715 return;
7716 dst = SET_DEST (set);
7717 if (! rtx_equal_p (dst, SET_DEST (set2))
7718 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
7719 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
7720 || (INTVAL (XEXP (SET_SRC (set), 1))
7721 != -INTVAL (XEXP (SET_SRC (set2), 1))))
7722 return;
7723 delete_insn (prev);
7724 delete_insn (next);
7727 /* Subfunction of delete_address_reloads: process registers found in X. */
7728 static void
7729 delete_address_reloads_1 (dead_insn, x, current_insn)
7730 rtx dead_insn, x, current_insn;
7732 rtx prev, set, dst, i2;
7733 int i, j;
7734 enum rtx_code code = GET_CODE (x);
7736 if (code != REG)
7738 const char *fmt = GET_RTX_FORMAT (code);
7739 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7741 if (fmt[i] == 'e')
7742 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
7743 else if (fmt[i] == 'E')
7745 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7746 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
7747 current_insn);
7750 return;
7753 if (spill_reg_order[REGNO (x)] < 0)
7754 return;
7756 /* Scan backwards for the insn that sets x. This might be a way back due
7757 to inheritance. */
7758 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
7760 code = GET_CODE (prev);
7761 if (code == CODE_LABEL || code == JUMP_INSN)
7762 return;
7763 if (GET_RTX_CLASS (code) != 'i')
7764 continue;
7765 if (reg_set_p (x, PATTERN (prev)))
7766 break;
7767 if (reg_referenced_p (x, PATTERN (prev)))
7768 return;
7770 if (! prev || INSN_UID (prev) < reload_first_uid)
7771 return;
7772 /* Check that PREV only sets the reload register. */
7773 set = single_set (prev);
7774 if (! set)
7775 return;
7776 dst = SET_DEST (set);
7777 if (GET_CODE (dst) != REG
7778 || ! rtx_equal_p (dst, x))
7779 return;
7780 if (! reg_set_p (dst, PATTERN (dead_insn)))
7782 /* Check if DST was used in a later insn -
7783 it might have been inherited. */
7784 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
7786 if (GET_CODE (i2) == CODE_LABEL)
7787 break;
7788 if (! INSN_P (i2))
7789 continue;
7790 if (reg_referenced_p (dst, PATTERN (i2)))
7792 /* If there is a reference to the register in the current insn,
7793 it might be loaded in a non-inherited reload. If no other
7794 reload uses it, that means the register is set before
7795 referenced. */
7796 if (i2 == current_insn)
7798 for (j = n_reloads - 1; j >= 0; j--)
7799 if ((rld[j].reg_rtx == dst && reload_inherited[j])
7800 || reload_override_in[j] == dst)
7801 return;
7802 for (j = n_reloads - 1; j >= 0; j--)
7803 if (rld[j].in && rld[j].reg_rtx == dst)
7804 break;
7805 if (j >= 0)
7806 break;
7808 return;
7810 if (GET_CODE (i2) == JUMP_INSN)
7811 break;
7812 /* If DST is still live at CURRENT_INSN, check if it is used for
7813 any reload. Note that even if CURRENT_INSN sets DST, we still
7814 have to check the reloads. */
7815 if (i2 == current_insn)
7817 for (j = n_reloads - 1; j >= 0; j--)
7818 if ((rld[j].reg_rtx == dst && reload_inherited[j])
7819 || reload_override_in[j] == dst)
7820 return;
7821 /* ??? We can't finish the loop here, because dst might be
7822 allocated to a pseudo in this block if no reload in this
7823 block needs any of the clsses containing DST - see
7824 spill_hard_reg. There is no easy way to tell this, so we
7825 have to scan till the end of the basic block. */
7827 if (reg_set_p (dst, PATTERN (i2)))
7828 break;
7831 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
7832 reg_reloaded_contents[REGNO (dst)] = -1;
7833 /* Can't use delete_insn here because PREV might be a basic block head. */
7834 PUT_CODE (prev, NOTE);
7835 NOTE_LINE_NUMBER (prev) = NOTE_INSN_DELETED;
7836 NOTE_SOURCE_FILE (prev) = 0;
7839 /* Output reload-insns to reload VALUE into RELOADREG.
7840 VALUE is an autoincrement or autodecrement RTX whose operand
7841 is a register or memory location;
7842 so reloading involves incrementing that location.
7843 IN is either identical to VALUE, or some cheaper place to reload from.
7845 INC_AMOUNT is the number to increment or decrement by (always positive).
7846 This cannot be deduced from VALUE.
7848 Return the instruction that stores into RELOADREG. */
7850 static rtx
7851 inc_for_reload (reloadreg, in, value, inc_amount)
7852 rtx reloadreg;
7853 rtx in, value;
7854 int inc_amount;
7856 /* REG or MEM to be copied and incremented. */
7857 rtx incloc = XEXP (value, 0);
7858 /* Nonzero if increment after copying. */
7859 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
7860 rtx last;
7861 rtx inc;
7862 rtx add_insn;
7863 int code;
7864 rtx store;
7865 rtx real_in = in == value ? XEXP (in, 0) : in;
7867 /* No hard register is equivalent to this register after
7868 inc/dec operation. If REG_LAST_RELOAD_REG were non-zero,
7869 we could inc/dec that register as well (maybe even using it for
7870 the source), but I'm not sure it's worth worrying about. */
7871 if (GET_CODE (incloc) == REG)
7872 reg_last_reload_reg[REGNO (incloc)] = 0;
7874 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
7875 inc_amount = -inc_amount;
7877 inc = GEN_INT (inc_amount);
7879 /* If this is post-increment, first copy the location to the reload reg. */
7880 if (post && real_in != reloadreg)
7881 emit_insn (gen_move_insn (reloadreg, real_in));
7883 if (in == value)
7885 /* See if we can directly increment INCLOC. Use a method similar to
7886 that in gen_reload. */
7888 last = get_last_insn ();
7889 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
7890 gen_rtx_PLUS (GET_MODE (incloc),
7891 incloc, inc)));
7893 code = recog_memoized (add_insn);
7894 if (code >= 0)
7896 extract_insn (add_insn);
7897 if (constrain_operands (1))
7899 /* If this is a pre-increment and we have incremented the value
7900 where it lives, copy the incremented value to RELOADREG to
7901 be used as an address. */
7903 if (! post)
7904 emit_insn (gen_move_insn (reloadreg, incloc));
7906 return add_insn;
7909 delete_insns_since (last);
7912 /* If couldn't do the increment directly, must increment in RELOADREG.
7913 The way we do this depends on whether this is pre- or post-increment.
7914 For pre-increment, copy INCLOC to the reload register, increment it
7915 there, then save back. */
7917 if (! post)
7919 if (in != reloadreg)
7920 emit_insn (gen_move_insn (reloadreg, real_in));
7921 emit_insn (gen_add2_insn (reloadreg, inc));
7922 store = emit_insn (gen_move_insn (incloc, reloadreg));
7924 else
7926 /* Postincrement.
7927 Because this might be a jump insn or a compare, and because RELOADREG
7928 may not be available after the insn in an input reload, we must do
7929 the incrementation before the insn being reloaded for.
7931 We have already copied IN to RELOADREG. Increment the copy in
7932 RELOADREG, save that back, then decrement RELOADREG so it has
7933 the original value. */
7935 emit_insn (gen_add2_insn (reloadreg, inc));
7936 store = emit_insn (gen_move_insn (incloc, reloadreg));
7937 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
7940 return store;
7943 /* Return 1 if we are certain that the constraint-string STRING allows
7944 the hard register REG. Return 0 if we can't be sure of this. */
7946 static int
7947 constraint_accepts_reg_p (string, reg)
7948 const char *string;
7949 rtx reg;
7951 int value = 0;
7952 int regno = true_regnum (reg);
7953 int c;
7955 /* Initialize for first alternative. */
7956 value = 0;
7957 /* Check that each alternative contains `g' or `r'. */
7958 while (1)
7959 switch (c = *string++)
7961 case 0:
7962 /* If an alternative lacks `g' or `r', we lose. */
7963 return value;
7964 case ',':
7965 /* If an alternative lacks `g' or `r', we lose. */
7966 if (value == 0)
7967 return 0;
7968 /* Initialize for next alternative. */
7969 value = 0;
7970 break;
7971 case 'g':
7972 case 'r':
7973 /* Any general reg wins for this alternative. */
7974 if (TEST_HARD_REG_BIT (reg_class_contents[(int) GENERAL_REGS], regno))
7975 value = 1;
7976 break;
7977 default:
7978 /* Any reg in specified class wins for this alternative. */
7980 enum reg_class class = REG_CLASS_FROM_LETTER (c);
7982 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno))
7983 value = 1;
7988 /* INSN is a no-op; delete it.
7989 If this sets the return value of the function, we must keep a USE around,
7990 in case this is in a different basic block than the final USE. Otherwise,
7991 we could loose important register lifeness information on
7992 SMALL_REGISTER_CLASSES machines, where return registers might be used as
7993 spills: subsequent passes assume that spill registers are dead at the end
7994 of a basic block.
7995 VALUE must be the return value in such a case, NULL otherwise. */
7996 static void
7997 reload_cse_delete_noop_set (insn, value)
7998 rtx insn, value;
8000 if (value)
8002 PATTERN (insn) = gen_rtx_USE (VOIDmode, value);
8003 INSN_CODE (insn) = -1;
8004 REG_NOTES (insn) = NULL_RTX;
8006 else
8008 PUT_CODE (insn, NOTE);
8009 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8010 NOTE_SOURCE_FILE (insn) = 0;
8014 /* See whether a single set SET is a noop. */
8015 static int
8016 reload_cse_noop_set_p (set)
8017 rtx set;
8019 return rtx_equal_for_cselib_p (SET_DEST (set), SET_SRC (set));
8022 /* Try to simplify INSN. */
8023 static void
8024 reload_cse_simplify (insn)
8025 rtx insn;
8027 rtx body = PATTERN (insn);
8029 if (GET_CODE (body) == SET)
8031 int count = 0;
8032 if (reload_cse_noop_set_p (body))
8034 rtx value = SET_DEST (body);
8035 if (! REG_FUNCTION_VALUE_P (SET_DEST (body)))
8036 value = 0;
8037 reload_cse_delete_noop_set (insn, value);
8038 return;
8041 /* It's not a no-op, but we can try to simplify it. */
8042 count += reload_cse_simplify_set (body, insn);
8044 if (count > 0)
8045 apply_change_group ();
8046 else
8047 reload_cse_simplify_operands (insn);
8049 else if (GET_CODE (body) == PARALLEL)
8051 int i;
8052 int count = 0;
8053 rtx value = NULL_RTX;
8055 /* If every action in a PARALLEL is a noop, we can delete
8056 the entire PARALLEL. */
8057 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8059 rtx part = XVECEXP (body, 0, i);
8060 if (GET_CODE (part) == SET)
8062 if (! reload_cse_noop_set_p (part))
8063 break;
8064 if (REG_FUNCTION_VALUE_P (SET_DEST (part)))
8066 if (value)
8067 break;
8068 value = SET_DEST (part);
8071 else if (GET_CODE (part) != CLOBBER)
8072 break;
8075 if (i < 0)
8077 reload_cse_delete_noop_set (insn, value);
8078 /* We're done with this insn. */
8079 return;
8082 /* It's not a no-op, but we can try to simplify it. */
8083 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8084 if (GET_CODE (XVECEXP (body, 0, i)) == SET)
8085 count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
8087 if (count > 0)
8088 apply_change_group ();
8089 else
8090 reload_cse_simplify_operands (insn);
8094 /* Do a very simple CSE pass over the hard registers.
8096 This function detects no-op moves where we happened to assign two
8097 different pseudo-registers to the same hard register, and then
8098 copied one to the other. Reload will generate a useless
8099 instruction copying a register to itself.
8101 This function also detects cases where we load a value from memory
8102 into two different registers, and (if memory is more expensive than
8103 registers) changes it to simply copy the first register into the
8104 second register.
8106 Another optimization is performed that scans the operands of each
8107 instruction to see whether the value is already available in a
8108 hard register. It then replaces the operand with the hard register
8109 if possible, much like an optional reload would. */
8111 static void
8112 reload_cse_regs_1 (first)
8113 rtx first;
8115 rtx insn;
8117 cselib_init ();
8118 init_alias_analysis ();
8120 for (insn = first; insn; insn = NEXT_INSN (insn))
8122 if (INSN_P (insn))
8123 reload_cse_simplify (insn);
8125 cselib_process_insn (insn);
8128 /* Clean up. */
8129 end_alias_analysis ();
8130 cselib_finish ();
8133 /* Call cse / combine like post-reload optimization phases.
8134 FIRST is the first instruction. */
8135 void
8136 reload_cse_regs (first)
8137 rtx first;
8139 reload_cse_regs_1 (first);
8140 reload_combine ();
8141 reload_cse_move2add (first);
8142 if (flag_expensive_optimizations)
8143 reload_cse_regs_1 (first);
8146 /* Try to simplify a single SET instruction. SET is the set pattern.
8147 INSN is the instruction it came from.
8148 This function only handles one case: if we set a register to a value
8149 which is not a register, we try to find that value in some other register
8150 and change the set into a register copy. */
8152 static int
8153 reload_cse_simplify_set (set, insn)
8154 rtx set;
8155 rtx insn;
8157 int did_change = 0;
8158 int dreg;
8159 rtx src;
8160 enum reg_class dclass;
8161 int old_cost;
8162 cselib_val *val;
8163 struct elt_loc_list *l;
8165 dreg = true_regnum (SET_DEST (set));
8166 if (dreg < 0)
8167 return 0;
8169 src = SET_SRC (set);
8170 if (side_effects_p (src) || true_regnum (src) >= 0)
8171 return 0;
8173 dclass = REGNO_REG_CLASS (dreg);
8175 /* If memory loads are cheaper than register copies, don't change them. */
8176 if (GET_CODE (src) == MEM)
8177 old_cost = MEMORY_MOVE_COST (GET_MODE (src), dclass, 1);
8178 else if (CONSTANT_P (src))
8179 old_cost = rtx_cost (src, SET);
8180 else if (GET_CODE (src) == REG)
8181 old_cost = REGISTER_MOVE_COST (GET_MODE (src),
8182 REGNO_REG_CLASS (REGNO (src)), dclass);
8183 else
8184 /* ??? */
8185 old_cost = rtx_cost (src, SET);
8187 val = cselib_lookup (src, GET_MODE (SET_DEST (set)), 0);
8188 if (! val)
8189 return 0;
8190 for (l = val->locs; l; l = l->next)
8192 int this_cost;
8193 if (CONSTANT_P (l->loc) && ! references_value_p (l->loc, 0))
8194 this_cost = rtx_cost (l->loc, SET);
8195 else if (GET_CODE (l->loc) == REG)
8196 this_cost = REGISTER_MOVE_COST (GET_MODE (l->loc),
8197 REGNO_REG_CLASS (REGNO (l->loc)),
8198 dclass);
8199 else
8200 continue;
8201 /* If equal costs, prefer registers over anything else. That tends to
8202 lead to smaller instructions on some machines. */
8203 if ((this_cost < old_cost
8204 || (this_cost == old_cost
8205 && GET_CODE (l->loc) == REG
8206 && GET_CODE (SET_SRC (set)) != REG))
8207 && validate_change (insn, &SET_SRC (set), copy_rtx (l->loc), 1))
8208 old_cost = this_cost, did_change = 1;
8211 return did_change;
8214 /* Try to replace operands in INSN with equivalent values that are already
8215 in registers. This can be viewed as optional reloading.
8217 For each non-register operand in the insn, see if any hard regs are
8218 known to be equivalent to that operand. Record the alternatives which
8219 can accept these hard registers. Among all alternatives, select the
8220 ones which are better or equal to the one currently matching, where
8221 "better" is in terms of '?' and '!' constraints. Among the remaining
8222 alternatives, select the one which replaces most operands with
8223 hard registers. */
8225 static int
8226 reload_cse_simplify_operands (insn)
8227 rtx insn;
8229 int i, j;
8231 /* For each operand, all registers that are equivalent to it. */
8232 HARD_REG_SET equiv_regs[MAX_RECOG_OPERANDS];
8234 const char *constraints[MAX_RECOG_OPERANDS];
8236 /* Vector recording how bad an alternative is. */
8237 int *alternative_reject;
8238 /* Vector recording how many registers can be introduced by choosing
8239 this alternative. */
8240 int *alternative_nregs;
8241 /* Array of vectors recording, for each operand and each alternative,
8242 which hard register to substitute, or -1 if the operand should be
8243 left as it is. */
8244 int *op_alt_regno[MAX_RECOG_OPERANDS];
8245 /* Array of alternatives, sorted in order of decreasing desirability. */
8246 int *alternative_order;
8247 rtx reg = gen_rtx_REG (VOIDmode, -1);
8249 extract_insn (insn);
8251 if (recog_data.n_alternatives == 0 || recog_data.n_operands == 0)
8252 return 0;
8254 /* Figure out which alternative currently matches. */
8255 if (! constrain_operands (1))
8256 fatal_insn_not_found (insn);
8258 alternative_reject = (int *) alloca (recog_data.n_alternatives * sizeof (int));
8259 alternative_nregs = (int *) alloca (recog_data.n_alternatives * sizeof (int));
8260 alternative_order = (int *) alloca (recog_data.n_alternatives * sizeof (int));
8261 memset ((char *)alternative_reject, 0, recog_data.n_alternatives * sizeof (int));
8262 memset ((char *)alternative_nregs, 0, recog_data.n_alternatives * sizeof (int));
8264 /* For each operand, find out which regs are equivalent. */
8265 for (i = 0; i < recog_data.n_operands; i++)
8267 cselib_val *v;
8268 struct elt_loc_list *l;
8270 CLEAR_HARD_REG_SET (equiv_regs[i]);
8272 /* cselib blows up on CODE_LABELs. Trying to fix that doesn't seem
8273 right, so avoid the problem here. Likewise if we have a constant
8274 and the insn pattern doesn't tell us the mode we need. */
8275 if (GET_CODE (recog_data.operand[i]) == CODE_LABEL
8276 || (CONSTANT_P (recog_data.operand[i])
8277 && recog_data.operand_mode[i] == VOIDmode))
8278 continue;
8280 v = cselib_lookup (recog_data.operand[i], recog_data.operand_mode[i], 0);
8281 if (! v)
8282 continue;
8284 for (l = v->locs; l; l = l->next)
8285 if (GET_CODE (l->loc) == REG)
8286 SET_HARD_REG_BIT (equiv_regs[i], REGNO (l->loc));
8289 for (i = 0; i < recog_data.n_operands; i++)
8291 enum machine_mode mode;
8292 int regno;
8293 const char *p;
8295 op_alt_regno[i] = (int *) alloca (recog_data.n_alternatives * sizeof (int));
8296 for (j = 0; j < recog_data.n_alternatives; j++)
8297 op_alt_regno[i][j] = -1;
8299 p = constraints[i] = recog_data.constraints[i];
8300 mode = recog_data.operand_mode[i];
8302 /* Add the reject values for each alternative given by the constraints
8303 for this operand. */
8304 j = 0;
8305 while (*p != '\0')
8307 char c = *p++;
8308 if (c == ',')
8309 j++;
8310 else if (c == '?')
8311 alternative_reject[j] += 3;
8312 else if (c == '!')
8313 alternative_reject[j] += 300;
8316 /* We won't change operands which are already registers. We
8317 also don't want to modify output operands. */
8318 regno = true_regnum (recog_data.operand[i]);
8319 if (regno >= 0
8320 || constraints[i][0] == '='
8321 || constraints[i][0] == '+')
8322 continue;
8324 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8326 int class = (int) NO_REGS;
8328 if (! TEST_HARD_REG_BIT (equiv_regs[i], regno))
8329 continue;
8331 REGNO (reg) = regno;
8332 PUT_MODE (reg, mode);
8334 /* We found a register equal to this operand. Now look for all
8335 alternatives that can accept this register and have not been
8336 assigned a register they can use yet. */
8337 j = 0;
8338 p = constraints[i];
8339 for (;;)
8341 char c = *p++;
8343 switch (c)
8345 case '=': case '+': case '?':
8346 case '#': case '&': case '!':
8347 case '*': case '%':
8348 case '0': case '1': case '2': case '3': case '4':
8349 case '5': case '6': case '7': case '8': case '9':
8350 case 'm': case '<': case '>': case 'V': case 'o':
8351 case 'E': case 'F': case 'G': case 'H':
8352 case 's': case 'i': case 'n':
8353 case 'I': case 'J': case 'K': case 'L':
8354 case 'M': case 'N': case 'O': case 'P':
8355 case 'p': case 'X':
8356 /* These don't say anything we care about. */
8357 break;
8359 case 'g': case 'r':
8360 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
8361 break;
8363 default:
8364 class
8365 = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER ((unsigned char)c)];
8366 break;
8368 case ',': case '\0':
8369 /* See if REGNO fits this alternative, and set it up as the
8370 replacement register if we don't have one for this
8371 alternative yet and the operand being replaced is not
8372 a cheap CONST_INT. */
8373 if (op_alt_regno[i][j] == -1
8374 && reg_fits_class_p (reg, class, 0, mode)
8375 && (GET_CODE (recog_data.operand[i]) != CONST_INT
8376 || (rtx_cost (recog_data.operand[i], SET)
8377 > rtx_cost (reg, SET))))
8379 alternative_nregs[j]++;
8380 op_alt_regno[i][j] = regno;
8382 j++;
8383 break;
8386 if (c == '\0')
8387 break;
8392 /* Record all alternatives which are better or equal to the currently
8393 matching one in the alternative_order array. */
8394 for (i = j = 0; i < recog_data.n_alternatives; i++)
8395 if (alternative_reject[i] <= alternative_reject[which_alternative])
8396 alternative_order[j++] = i;
8397 recog_data.n_alternatives = j;
8399 /* Sort it. Given a small number of alternatives, a dumb algorithm
8400 won't hurt too much. */
8401 for (i = 0; i < recog_data.n_alternatives - 1; i++)
8403 int best = i;
8404 int best_reject = alternative_reject[alternative_order[i]];
8405 int best_nregs = alternative_nregs[alternative_order[i]];
8406 int tmp;
8408 for (j = i + 1; j < recog_data.n_alternatives; j++)
8410 int this_reject = alternative_reject[alternative_order[j]];
8411 int this_nregs = alternative_nregs[alternative_order[j]];
8413 if (this_reject < best_reject
8414 || (this_reject == best_reject && this_nregs < best_nregs))
8416 best = j;
8417 best_reject = this_reject;
8418 best_nregs = this_nregs;
8422 tmp = alternative_order[best];
8423 alternative_order[best] = alternative_order[i];
8424 alternative_order[i] = tmp;
8427 /* Substitute the operands as determined by op_alt_regno for the best
8428 alternative. */
8429 j = alternative_order[0];
8431 for (i = 0; i < recog_data.n_operands; i++)
8433 enum machine_mode mode = recog_data.operand_mode[i];
8434 if (op_alt_regno[i][j] == -1)
8435 continue;
8437 validate_change (insn, recog_data.operand_loc[i],
8438 gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
8441 for (i = recog_data.n_dups - 1; i >= 0; i--)
8443 int op = recog_data.dup_num[i];
8444 enum machine_mode mode = recog_data.operand_mode[op];
8446 if (op_alt_regno[op][j] == -1)
8447 continue;
8449 validate_change (insn, recog_data.dup_loc[i],
8450 gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
8453 return apply_change_group ();
8456 /* If reload couldn't use reg+reg+offset addressing, try to use reg+reg
8457 addressing now.
8458 This code might also be useful when reload gave up on reg+reg addresssing
8459 because of clashes between the return register and INDEX_REG_CLASS. */
8461 /* The maximum number of uses of a register we can keep track of to
8462 replace them with reg+reg addressing. */
8463 #define RELOAD_COMBINE_MAX_USES 6
8465 /* INSN is the insn where a register has ben used, and USEP points to the
8466 location of the register within the rtl. */
8467 struct reg_use { rtx insn, *usep; };
8469 /* If the register is used in some unknown fashion, USE_INDEX is negative.
8470 If it is dead, USE_INDEX is RELOAD_COMBINE_MAX_USES, and STORE_RUID
8471 indicates where it becomes live again.
8472 Otherwise, USE_INDEX is the index of the last encountered use of the
8473 register (which is first among these we have seen since we scan backwards),
8474 OFFSET contains the constant offset that is added to the register in
8475 all encountered uses, and USE_RUID indicates the first encountered, i.e.
8476 last, of these uses.
8477 STORE_RUID is always meaningful if we only want to use a value in a
8478 register in a different place: it denotes the next insn in the insn
8479 stream (i.e. the last ecountered) that sets or clobbers the register. */
8480 static struct
8482 struct reg_use reg_use[RELOAD_COMBINE_MAX_USES];
8483 int use_index;
8484 rtx offset;
8485 int store_ruid;
8486 int use_ruid;
8487 } reg_state[FIRST_PSEUDO_REGISTER];
8489 /* Reverse linear uid. This is increased in reload_combine while scanning
8490 the instructions from last to first. It is used to set last_label_ruid
8491 and the store_ruid / use_ruid fields in reg_state. */
8492 static int reload_combine_ruid;
8494 #define LABEL_LIVE(LABEL) \
8495 (label_live[CODE_LABEL_NUMBER (LABEL) - min_labelno])
8497 static void
8498 reload_combine ()
8500 rtx insn, set;
8501 int first_index_reg = -1, last_index_reg;
8502 int i;
8503 unsigned int r;
8504 int last_label_ruid;
8505 int min_labelno, n_labels;
8506 HARD_REG_SET ever_live_at_start, *label_live;
8508 /* If reg+reg can be used in offsetable memory adresses, the main chunk of
8509 reload has already used it where appropriate, so there is no use in
8510 trying to generate it now. */
8511 if (double_reg_address_ok && INDEX_REG_CLASS != NO_REGS)
8512 return;
8514 /* To avoid wasting too much time later searching for an index register,
8515 determine the minimum and maximum index register numbers. */
8516 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
8517 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], r))
8519 if (first_index_reg == -1)
8520 first_index_reg = r;
8522 last_index_reg = r;
8525 /* If no index register is available, we can quit now. */
8526 if (first_index_reg == -1)
8527 return;
8529 /* Set up LABEL_LIVE and EVER_LIVE_AT_START. The register lifetime
8530 information is a bit fuzzy immediately after reload, but it's
8531 still good enough to determine which registers are live at a jump
8532 destination. */
8533 min_labelno = get_first_label_num ();
8534 n_labels = max_label_num () - min_labelno;
8535 label_live = (HARD_REG_SET *) xmalloc (n_labels * sizeof (HARD_REG_SET));
8536 CLEAR_HARD_REG_SET (ever_live_at_start);
8538 for (i = n_basic_blocks - 1; i >= 0; i--)
8540 insn = BLOCK_HEAD (i);
8541 if (GET_CODE (insn) == CODE_LABEL)
8543 HARD_REG_SET live;
8545 REG_SET_TO_HARD_REG_SET (live,
8546 BASIC_BLOCK (i)->global_live_at_start);
8547 compute_use_by_pseudos (&live,
8548 BASIC_BLOCK (i)->global_live_at_start);
8549 COPY_HARD_REG_SET (LABEL_LIVE (insn), live);
8550 IOR_HARD_REG_SET (ever_live_at_start, live);
8554 /* Initialize last_label_ruid, reload_combine_ruid and reg_state. */
8555 last_label_ruid = reload_combine_ruid = 0;
8556 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
8558 reg_state[r].store_ruid = reload_combine_ruid;
8559 if (fixed_regs[r])
8560 reg_state[r].use_index = -1;
8561 else
8562 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
8565 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
8567 rtx note;
8569 /* We cannot do our optimization across labels. Invalidating all the use
8570 information we have would be costly, so we just note where the label
8571 is and then later disable any optimization that would cross it. */
8572 if (GET_CODE (insn) == CODE_LABEL)
8573 last_label_ruid = reload_combine_ruid;
8574 else if (GET_CODE (insn) == BARRIER)
8575 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
8576 if (! fixed_regs[r])
8577 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
8579 if (! INSN_P (insn))
8580 continue;
8582 reload_combine_ruid++;
8584 /* Look for (set (REGX) (CONST_INT))
8585 (set (REGX) (PLUS (REGX) (REGY)))
8587 ... (MEM (REGX)) ...
8588 and convert it to
8589 (set (REGZ) (CONST_INT))
8591 ... (MEM (PLUS (REGZ) (REGY)))... .
8593 First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
8594 and that we know all uses of REGX before it dies. */
8595 set = single_set (insn);
8596 if (set != NULL_RTX
8597 && GET_CODE (SET_DEST (set)) == REG
8598 && (HARD_REGNO_NREGS (REGNO (SET_DEST (set)),
8599 GET_MODE (SET_DEST (set)))
8600 == 1)
8601 && GET_CODE (SET_SRC (set)) == PLUS
8602 && GET_CODE (XEXP (SET_SRC (set), 1)) == REG
8603 && rtx_equal_p (XEXP (SET_SRC (set), 0), SET_DEST (set))
8604 && last_label_ruid < reg_state[REGNO (SET_DEST (set))].use_ruid)
8606 rtx reg = SET_DEST (set);
8607 rtx plus = SET_SRC (set);
8608 rtx base = XEXP (plus, 1);
8609 rtx prev = prev_nonnote_insn (insn);
8610 rtx prev_set = prev ? single_set (prev) : NULL_RTX;
8611 unsigned int regno = REGNO (reg);
8612 rtx const_reg = NULL_RTX;
8613 rtx reg_sum = NULL_RTX;
8615 /* Now, we need an index register.
8616 We'll set index_reg to this index register, const_reg to the
8617 register that is to be loaded with the constant
8618 (denoted as REGZ in the substitution illustration above),
8619 and reg_sum to the register-register that we want to use to
8620 substitute uses of REG (typically in MEMs) with.
8621 First check REG and BASE for being index registers;
8622 we can use them even if they are not dead. */
8623 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], regno)
8624 || TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
8625 REGNO (base)))
8627 const_reg = reg;
8628 reg_sum = plus;
8630 else
8632 /* Otherwise, look for a free index register. Since we have
8633 checked above that neiter REG nor BASE are index registers,
8634 if we find anything at all, it will be different from these
8635 two registers. */
8636 for (i = first_index_reg; i <= last_index_reg; i++)
8638 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
8640 && reg_state[i].use_index == RELOAD_COMBINE_MAX_USES
8641 && reg_state[i].store_ruid <= reg_state[regno].use_ruid
8642 && HARD_REGNO_NREGS (i, GET_MODE (reg)) == 1)
8644 rtx index_reg = gen_rtx_REG (GET_MODE (reg), i);
8646 const_reg = index_reg;
8647 reg_sum = gen_rtx_PLUS (GET_MODE (reg), index_reg, base);
8648 break;
8653 /* Check that PREV_SET is indeed (set (REGX) (CONST_INT)) and that
8654 (REGY), i.e. BASE, is not clobbered before the last use we'll
8655 create. */
8656 if (prev_set != 0
8657 && GET_CODE (SET_SRC (prev_set)) == CONST_INT
8658 && rtx_equal_p (SET_DEST (prev_set), reg)
8659 && reg_state[regno].use_index >= 0
8660 && (reg_state[REGNO (base)].store_ruid
8661 <= reg_state[regno].use_ruid)
8662 && reg_sum != 0)
8664 int i;
8666 /* Change destination register and, if necessary, the
8667 constant value in PREV, the constant loading instruction. */
8668 validate_change (prev, &SET_DEST (prev_set), const_reg, 1);
8669 if (reg_state[regno].offset != const0_rtx)
8670 validate_change (prev,
8671 &SET_SRC (prev_set),
8672 GEN_INT (INTVAL (SET_SRC (prev_set))
8673 + INTVAL (reg_state[regno].offset)),
8676 /* Now for every use of REG that we have recorded, replace REG
8677 with REG_SUM. */
8678 for (i = reg_state[regno].use_index;
8679 i < RELOAD_COMBINE_MAX_USES; i++)
8680 validate_change (reg_state[regno].reg_use[i].insn,
8681 reg_state[regno].reg_use[i].usep,
8682 reg_sum, 1);
8684 if (apply_change_group ())
8686 rtx *np;
8688 /* Delete the reg-reg addition. */
8689 PUT_CODE (insn, NOTE);
8690 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8691 NOTE_SOURCE_FILE (insn) = 0;
8693 if (reg_state[regno].offset != const0_rtx)
8694 /* Previous REG_EQUIV / REG_EQUAL notes for PREV
8695 are now invalid. */
8696 for (np = &REG_NOTES (prev); *np;)
8698 if (REG_NOTE_KIND (*np) == REG_EQUAL
8699 || REG_NOTE_KIND (*np) == REG_EQUIV)
8700 *np = XEXP (*np, 1);
8701 else
8702 np = &XEXP (*np, 1);
8705 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
8706 reg_state[REGNO (const_reg)].store_ruid
8707 = reload_combine_ruid;
8708 continue;
8713 note_stores (PATTERN (insn), reload_combine_note_store, NULL);
8715 if (GET_CODE (insn) == CALL_INSN)
8717 rtx link;
8719 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
8720 if (call_used_regs[r])
8722 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
8723 reg_state[r].store_ruid = reload_combine_ruid;
8726 for (link = CALL_INSN_FUNCTION_USAGE (insn); link;
8727 link = XEXP (link, 1))
8729 rtx usage_rtx = XEXP (XEXP (link, 0), 0);
8730 if (GET_CODE (usage_rtx) == REG)
8732 int i;
8733 unsigned int start_reg = REGNO (usage_rtx);
8734 unsigned int num_regs =
8735 HARD_REGNO_NREGS (start_reg, GET_MODE (usage_rtx));
8736 unsigned int end_reg = start_reg + num_regs - 1;
8737 for (i = start_reg; i <= end_reg; i++)
8738 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
8740 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
8741 reg_state[i].store_ruid = reload_combine_ruid;
8743 else
8744 reg_state[i].use_index = -1;
8749 else if (GET_CODE (insn) == JUMP_INSN
8750 && GET_CODE (PATTERN (insn)) != RETURN)
8752 /* Non-spill registers might be used at the call destination in
8753 some unknown fashion, so we have to mark the unknown use. */
8754 HARD_REG_SET *live;
8756 if ((condjump_p (insn) || condjump_in_parallel_p (insn))
8757 && JUMP_LABEL (insn))
8758 live = &LABEL_LIVE (JUMP_LABEL (insn));
8759 else
8760 live = &ever_live_at_start;
8762 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
8763 if (TEST_HARD_REG_BIT (*live, i))
8764 reg_state[i].use_index = -1;
8767 reload_combine_note_use (&PATTERN (insn), insn);
8768 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
8770 if (REG_NOTE_KIND (note) == REG_INC
8771 && GET_CODE (XEXP (note, 0)) == REG)
8773 int regno = REGNO (XEXP (note, 0));
8775 reg_state[regno].store_ruid = reload_combine_ruid;
8776 reg_state[regno].use_index = -1;
8781 free (label_live);
8784 /* Check if DST is a register or a subreg of a register; if it is,
8785 update reg_state[regno].store_ruid and reg_state[regno].use_index
8786 accordingly. Called via note_stores from reload_combine. */
8788 static void
8789 reload_combine_note_store (dst, set, data)
8790 rtx dst, set;
8791 void *data ATTRIBUTE_UNUSED;
8793 int regno = 0;
8794 int i;
8795 enum machine_mode mode = GET_MODE (dst);
8797 if (GET_CODE (dst) == SUBREG)
8799 regno = SUBREG_WORD (dst);
8800 dst = SUBREG_REG (dst);
8802 if (GET_CODE (dst) != REG)
8803 return;
8804 regno += REGNO (dst);
8806 /* note_stores might have stripped a STRICT_LOW_PART, so we have to be
8807 careful with registers / register parts that are not full words.
8809 Similarly for ZERO_EXTRACT and SIGN_EXTRACT. */
8810 if (GET_CODE (set) != SET
8811 || GET_CODE (SET_DEST (set)) == ZERO_EXTRACT
8812 || GET_CODE (SET_DEST (set)) == SIGN_EXTRACT
8813 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
8815 for (i = HARD_REGNO_NREGS (regno, mode) - 1 + regno; i >= regno; i--)
8817 reg_state[i].use_index = -1;
8818 reg_state[i].store_ruid = reload_combine_ruid;
8821 else
8823 for (i = HARD_REGNO_NREGS (regno, mode) - 1 + regno; i >= regno; i--)
8825 reg_state[i].store_ruid = reload_combine_ruid;
8826 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
8831 /* XP points to a piece of rtl that has to be checked for any uses of
8832 registers.
8833 *XP is the pattern of INSN, or a part of it.
8834 Called from reload_combine, and recursively by itself. */
8835 static void
8836 reload_combine_note_use (xp, insn)
8837 rtx *xp, insn;
8839 rtx x = *xp;
8840 enum rtx_code code = x->code;
8841 const char *fmt;
8842 int i, j;
8843 rtx offset = const0_rtx; /* For the REG case below. */
8845 switch (code)
8847 case SET:
8848 if (GET_CODE (SET_DEST (x)) == REG)
8850 reload_combine_note_use (&SET_SRC (x), insn);
8851 return;
8853 break;
8855 case USE:
8856 /* If this is the USE of a return value, we can't change it. */
8857 if (GET_CODE (XEXP (x, 0)) == REG && REG_FUNCTION_VALUE_P (XEXP (x, 0)))
8859 /* Mark the return register as used in an unknown fashion. */
8860 rtx reg = XEXP (x, 0);
8861 int regno = REGNO (reg);
8862 int nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg));
8864 while (--nregs >= 0)
8865 reg_state[regno + nregs].use_index = -1;
8866 return;
8868 break;
8870 case CLOBBER:
8871 if (GET_CODE (SET_DEST (x)) == REG)
8872 return;
8873 break;
8875 case PLUS:
8876 /* We are interested in (plus (reg) (const_int)) . */
8877 if (GET_CODE (XEXP (x, 0)) != REG
8878 || GET_CODE (XEXP (x, 1)) != CONST_INT)
8879 break;
8880 offset = XEXP (x, 1);
8881 x = XEXP (x, 0);
8882 /* Fall through. */
8883 case REG:
8885 int regno = REGNO (x);
8886 int use_index;
8887 int nregs;
8889 /* Some spurious USEs of pseudo registers might remain.
8890 Just ignore them. */
8891 if (regno >= FIRST_PSEUDO_REGISTER)
8892 return;
8894 nregs = HARD_REGNO_NREGS (regno, GET_MODE (x));
8896 /* We can't substitute into multi-hard-reg uses. */
8897 if (nregs > 1)
8899 while (--nregs >= 0)
8900 reg_state[regno + nregs].use_index = -1;
8901 return;
8904 /* If this register is already used in some unknown fashion, we
8905 can't do anything.
8906 If we decrement the index from zero to -1, we can't store more
8907 uses, so this register becomes used in an unknown fashion. */
8908 use_index = --reg_state[regno].use_index;
8909 if (use_index < 0)
8910 return;
8912 if (use_index != RELOAD_COMBINE_MAX_USES - 1)
8914 /* We have found another use for a register that is already
8915 used later. Check if the offsets match; if not, mark the
8916 register as used in an unknown fashion. */
8917 if (! rtx_equal_p (offset, reg_state[regno].offset))
8919 reg_state[regno].use_index = -1;
8920 return;
8923 else
8925 /* This is the first use of this register we have seen since we
8926 marked it as dead. */
8927 reg_state[regno].offset = offset;
8928 reg_state[regno].use_ruid = reload_combine_ruid;
8930 reg_state[regno].reg_use[use_index].insn = insn;
8931 reg_state[regno].reg_use[use_index].usep = xp;
8932 return;
8935 default:
8936 break;
8939 /* Recursively process the components of X. */
8940 fmt = GET_RTX_FORMAT (code);
8941 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8943 if (fmt[i] == 'e')
8944 reload_combine_note_use (&XEXP (x, i), insn);
8945 else if (fmt[i] == 'E')
8947 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8948 reload_combine_note_use (&XVECEXP (x, i, j), insn);
8953 /* See if we can reduce the cost of a constant by replacing a move with
8954 an add. */
8955 /* We cannot do our optimization across labels. Invalidating all the
8956 information about register contents we have would be costly, so we
8957 use last_label_luid (local variable of reload_cse_move2add) to note
8958 where the label is and then later disable any optimization that would
8959 cross it.
8960 reg_offset[n] / reg_base_reg[n] / reg_mode[n] are only valid if
8961 reg_set_luid[n] is larger than last_label_luid[n] . */
8962 static int reg_set_luid[FIRST_PSEUDO_REGISTER];
8964 /* reg_offset[n] has to be CONST_INT for it and reg_base_reg[n] /
8965 reg_mode[n] to be valid.
8966 If reg_offset[n] is a CONST_INT and reg_base_reg[n] is negative, register n
8967 has been set to reg_offset[n] in mode reg_mode[n] .
8968 If reg_offset[n] is a CONST_INT and reg_base_reg[n] is non-negative,
8969 register n has been set to the sum of reg_offset[n] and register
8970 reg_base_reg[n], calculated in mode reg_mode[n] . */
8971 static rtx reg_offset[FIRST_PSEUDO_REGISTER];
8972 static int reg_base_reg[FIRST_PSEUDO_REGISTER];
8973 static enum machine_mode reg_mode[FIRST_PSEUDO_REGISTER];
8975 /* move2add_luid is linearily increased while scanning the instructions
8976 from first to last. It is used to set reg_set_luid in
8977 reload_cse_move2add and move2add_note_store. */
8978 static int move2add_luid;
8980 /* Generate a CONST_INT and force it in the range of MODE. */
8982 static rtx
8983 gen_mode_int (mode, value)
8984 enum machine_mode mode;
8985 HOST_WIDE_INT value;
8987 HOST_WIDE_INT cval = value & GET_MODE_MASK (mode);
8988 int width = GET_MODE_BITSIZE (mode);
8990 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative number,
8991 sign extend it. */
8992 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
8993 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
8994 cval |= (HOST_WIDE_INT) -1 << width;
8996 return GEN_INT (cval);
8999 static void
9000 reload_cse_move2add (first)
9001 rtx first;
9003 int i;
9004 rtx insn;
9005 int last_label_luid;
9007 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
9008 reg_set_luid[i] = 0;
9010 last_label_luid = 0;
9011 move2add_luid = 1;
9012 for (insn = first; insn; insn = NEXT_INSN (insn), move2add_luid++)
9014 rtx pat, note;
9016 if (GET_CODE (insn) == CODE_LABEL)
9017 last_label_luid = move2add_luid;
9018 if (! INSN_P (insn))
9019 continue;
9020 pat = PATTERN (insn);
9021 /* For simplicity, we only perform this optimization on
9022 straightforward SETs. */
9023 if (GET_CODE (pat) == SET
9024 && GET_CODE (SET_DEST (pat)) == REG)
9026 rtx reg = SET_DEST (pat);
9027 int regno = REGNO (reg);
9028 rtx src = SET_SRC (pat);
9030 /* Check if we have valid information on the contents of this
9031 register in the mode of REG. */
9032 /* ??? We don't know how zero / sign extension is handled, hence
9033 we can't go from a narrower to a wider mode. */
9034 if (reg_set_luid[regno] > last_label_luid
9035 && ((GET_MODE_SIZE (GET_MODE (reg))
9036 == GET_MODE_SIZE (reg_mode[regno]))
9037 || ((GET_MODE_SIZE (GET_MODE (reg))
9038 <= GET_MODE_SIZE (reg_mode[regno]))
9039 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (reg)),
9040 GET_MODE_BITSIZE (reg_mode[regno]))))
9041 && GET_CODE (reg_offset[regno]) == CONST_INT)
9043 /* Try to transform (set (REGX) (CONST_INT A))
9045 (set (REGX) (CONST_INT B))
9047 (set (REGX) (CONST_INT A))
9049 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
9051 if (GET_CODE (src) == CONST_INT && reg_base_reg[regno] < 0)
9053 int success = 0;
9054 rtx new_src
9055 = gen_mode_int (GET_MODE (reg),
9056 INTVAL (src) - INTVAL (reg_offset[regno]));
9057 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
9058 use (set (reg) (reg)) instead.
9059 We don't delete this insn, nor do we convert it into a
9060 note, to avoid losing register notes or the return
9061 value flag. jump2 already knowns how to get rid of
9062 no-op moves. */
9063 if (new_src == const0_rtx)
9064 success = validate_change (insn, &SET_SRC (pat), reg, 0);
9065 else if (rtx_cost (new_src, PLUS) < rtx_cost (src, SET)
9066 && have_add2_insn (GET_MODE (reg)))
9067 success = validate_change (insn, &PATTERN (insn),
9068 gen_add2_insn (reg, new_src), 0);
9069 reg_set_luid[regno] = move2add_luid;
9070 reg_mode[regno] = GET_MODE (reg);
9071 reg_offset[regno] = src;
9072 continue;
9075 /* Try to transform (set (REGX) (REGY))
9076 (set (REGX) (PLUS (REGX) (CONST_INT A)))
9078 (set (REGX) (REGY))
9079 (set (REGX) (PLUS (REGX) (CONST_INT B)))
9081 (REGX) (REGY))
9082 (set (REGX) (PLUS (REGX) (CONST_INT A)))
9084 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
9085 else if (GET_CODE (src) == REG
9086 && reg_base_reg[regno] == (int) REGNO (src)
9087 && reg_set_luid[regno] > reg_set_luid[REGNO (src)])
9089 rtx next = next_nonnote_insn (insn);
9090 rtx set = NULL_RTX;
9091 if (next)
9092 set = single_set (next);
9093 if (next
9094 && set
9095 && SET_DEST (set) == reg
9096 && GET_CODE (SET_SRC (set)) == PLUS
9097 && XEXP (SET_SRC (set), 0) == reg
9098 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
9100 rtx src3 = XEXP (SET_SRC (set), 1);
9101 rtx new_src
9102 = gen_mode_int (GET_MODE (reg),
9103 INTVAL (src3)
9104 - INTVAL (reg_offset[regno]));
9105 int success = 0;
9107 if (new_src == const0_rtx)
9108 /* See above why we create (set (reg) (reg)) here. */
9109 success
9110 = validate_change (next, &SET_SRC (set), reg, 0);
9111 else if ((rtx_cost (new_src, PLUS)
9112 < COSTS_N_INSNS (1) + rtx_cost (src3, SET))
9113 && have_add2_insn (GET_MODE (reg)))
9114 success
9115 = validate_change (next, &PATTERN (next),
9116 gen_add2_insn (reg, new_src), 0);
9117 if (success)
9119 /* INSN might be the first insn in a basic block
9120 if the preceding insn is a conditional jump
9121 or a possible-throwing call. */
9122 PUT_CODE (insn, NOTE);
9123 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9124 NOTE_SOURCE_FILE (insn) = 0;
9126 insn = next;
9127 reg_set_luid[regno] = move2add_luid;
9128 reg_mode[regno] = GET_MODE (reg);
9129 reg_offset[regno] = src3;
9130 continue;
9136 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
9138 if (REG_NOTE_KIND (note) == REG_INC
9139 && GET_CODE (XEXP (note, 0)) == REG)
9141 /* Indicate that this register has been recently written to,
9142 but the exact contents are not available. */
9143 int regno = REGNO (XEXP (note, 0));
9144 if (regno < FIRST_PSEUDO_REGISTER)
9146 reg_set_luid[regno] = move2add_luid;
9147 reg_offset[regno] = note;
9151 note_stores (PATTERN (insn), move2add_note_store, NULL);
9152 /* If this is a CALL_INSN, all call used registers are stored with
9153 unknown values. */
9154 if (GET_CODE (insn) == CALL_INSN)
9156 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
9158 if (call_used_regs[i])
9160 reg_set_luid[i] = move2add_luid;
9161 reg_offset[i] = insn; /* Invalidate contents. */
9168 /* SET is a SET or CLOBBER that sets DST.
9169 Update reg_set_luid, reg_offset and reg_base_reg accordingly.
9170 Called from reload_cse_move2add via note_stores. */
9172 static void
9173 move2add_note_store (dst, set, data)
9174 rtx dst, set;
9175 void *data ATTRIBUTE_UNUSED;
9177 unsigned int regno = 0;
9178 unsigned int i;
9179 enum machine_mode mode = GET_MODE (dst);
9181 if (GET_CODE (dst) == SUBREG)
9183 regno = SUBREG_WORD (dst);
9184 dst = SUBREG_REG (dst);
9187 /* Some targets do argument pushes without adding REG_INC notes. */
9189 if (GET_CODE (dst) == MEM)
9191 dst = XEXP (dst, 0);
9192 if (GET_CODE (dst) == PRE_INC || GET_CODE (dst) == POST_DEC
9193 || GET_CODE (dst) == PRE_DEC || GET_CODE (dst) == POST_DEC)
9195 regno = REGNO (XEXP (dst, 0));
9196 reg_set_luid[regno] = move2add_luid;
9197 reg_offset[regno] = dst;
9199 return;
9201 if (GET_CODE (dst) != REG)
9202 return;
9204 regno += REGNO (dst);
9206 if (HARD_REGNO_NREGS (regno, mode) == 1 && GET_CODE (set) == SET
9207 && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
9208 && GET_CODE (SET_DEST (set)) != SIGN_EXTRACT
9209 && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
9211 rtx src = SET_SRC (set);
9213 reg_mode[regno] = mode;
9214 switch (GET_CODE (src))
9216 case PLUS:
9218 rtx src0 = XEXP (src, 0);
9220 if (GET_CODE (src0) == REG)
9222 if (REGNO (src0) != regno
9223 || reg_offset[regno] != const0_rtx)
9224 reg_base_reg[regno] = REGNO (src0);
9226 reg_set_luid[regno] = move2add_luid;
9227 reg_offset[regno] = XEXP (src, 1);
9228 break;
9231 reg_set_luid[regno] = move2add_luid;
9232 reg_offset[regno] = set; /* Invalidate contents. */
9233 break;
9236 case REG:
9237 reg_base_reg[regno] = REGNO (SET_SRC (set));
9238 reg_offset[regno] = const0_rtx;
9239 reg_set_luid[regno] = move2add_luid;
9240 break;
9242 default:
9243 reg_base_reg[regno] = -1;
9244 reg_offset[regno] = SET_SRC (set);
9245 reg_set_luid[regno] = move2add_luid;
9246 break;
9249 else
9251 unsigned int endregno = regno + HARD_REGNO_NREGS (regno, mode);
9253 for (i = regno; i < endregno; i++)
9255 /* Indicate that this register has been recently written to,
9256 but the exact contents are not available. */
9257 reg_set_luid[i] = move2add_luid;
9258 reg_offset[i] = dst;
9263 #ifdef AUTO_INC_DEC
9264 static void
9265 add_auto_inc_notes (insn, x)
9266 rtx insn;
9267 rtx x;
9269 enum rtx_code code = GET_CODE (x);
9270 const char *fmt;
9271 int i, j;
9273 if (code == MEM && auto_inc_p (XEXP (x, 0)))
9275 REG_NOTES (insn)
9276 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
9277 return;
9280 /* Scan all the operand sub-expressions. */
9281 fmt = GET_RTX_FORMAT (code);
9282 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9284 if (fmt[i] == 'e')
9285 add_auto_inc_notes (insn, XEXP (x, i));
9286 else if (fmt[i] == 'E')
9287 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9288 add_auto_inc_notes (insn, XVECEXP (x, i, j));
9291 #endif