* predict.c (estimate_bb_frequencies): Correctly set
[official-gcc.git] / gcc / reload1.c
blob06b53f73597a41e4c197620f2982d5b22af0f216
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, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
27 #include "machmode.h"
28 #include "hard-reg-set.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "obstack.h"
32 #include "insn-config.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "optabs.h"
37 #include "regs.h"
38 #include "basic-block.h"
39 #include "reload.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "cselib.h"
43 #include "real.h"
44 #include "toplev.h"
45 #include "except.h"
46 #include "tree.h"
48 /* This file contains the reload pass of the compiler, which is
49 run after register allocation has been done. It checks that
50 each insn is valid (operands required to be in registers really
51 are in registers of the proper class) and fixes up invalid ones
52 by copying values temporarily into registers for the insns
53 that need them.
55 The results of register allocation are described by the vector
56 reg_renumber; the insns still contain pseudo regs, but reg_renumber
57 can be used to find which hard reg, if any, a pseudo reg is in.
59 The technique we always use is to free up a few hard regs that are
60 called ``reload regs'', and for each place where a pseudo reg
61 must be in a hard reg, copy it temporarily into one of the reload regs.
63 Reload regs are allocated locally for every instruction that needs
64 reloads. When there are pseudos which are allocated to a register that
65 has been chosen as a reload reg, such pseudos must be ``spilled''.
66 This means that they go to other hard regs, or to stack slots if no other
67 available hard regs can be found. Spilling can invalidate more
68 insns, requiring additional need for reloads, so we must keep checking
69 until the process stabilizes.
71 For machines with different classes of registers, we must keep track
72 of the register class needed for each reload, and make sure that
73 we allocate enough reload registers of each class.
75 The file reload.c contains the code that checks one insn for
76 validity and reports the reloads that it needs. This file
77 is in charge of scanning the entire rtl code, accumulating the
78 reload needs, spilling, assigning reload registers to use for
79 fixing up each insn, and generating the new insns to copy values
80 into the reload registers. */
82 #ifndef REGISTER_MOVE_COST
83 #define REGISTER_MOVE_COST(m, x, y) 2
84 #endif
86 #ifndef LOCAL_REGNO
87 #define LOCAL_REGNO(REGNO) 0
88 #endif
90 /* During reload_as_needed, element N contains a REG rtx for the hard reg
91 into which reg N has been reloaded (perhaps for a previous insn). */
92 static rtx *reg_last_reload_reg;
94 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
95 for an output reload that stores into reg N. */
96 static char *reg_has_output_reload;
98 /* Indicates which hard regs are reload-registers for an output reload
99 in the current insn. */
100 static HARD_REG_SET reg_is_output_reload;
102 /* Element N is the constant value to which pseudo reg N is equivalent,
103 or zero if pseudo reg N is not equivalent to a constant.
104 find_reloads looks at this in order to replace pseudo reg N
105 with the constant it stands for. */
106 rtx *reg_equiv_constant;
108 /* Element N is a memory location to which pseudo reg N is equivalent,
109 prior to any register elimination (such as frame pointer to stack
110 pointer). Depending on whether or not it is a valid address, this value
111 is transferred to either reg_equiv_address or reg_equiv_mem. */
112 rtx *reg_equiv_memory_loc;
114 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
115 This is used when the address is not valid as a memory address
116 (because its displacement is too big for the machine.) */
117 rtx *reg_equiv_address;
119 /* Element N is the memory slot to which pseudo reg N is equivalent,
120 or zero if pseudo reg N is not equivalent to a memory slot. */
121 rtx *reg_equiv_mem;
123 /* Widest width in which each pseudo reg is referred to (via subreg). */
124 static unsigned int *reg_max_ref_width;
126 /* Element N is the list of insns that initialized reg N from its equivalent
127 constant or memory slot. */
128 static rtx *reg_equiv_init;
130 /* Vector to remember old contents of reg_renumber before spilling. */
131 static short *reg_old_renumber;
133 /* During reload_as_needed, element N contains the last pseudo regno reloaded
134 into hard register N. If that pseudo reg occupied more than one register,
135 reg_reloaded_contents points to that pseudo for each spill register in
136 use; all of these must remain set for an inheritance to occur. */
137 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
139 /* During reload_as_needed, element N contains the insn for which
140 hard register N was last used. Its contents are significant only
141 when reg_reloaded_valid is set for this register. */
142 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
144 /* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid. */
145 static HARD_REG_SET reg_reloaded_valid;
146 /* Indicate if the register was dead at the end of the reload.
147 This is only valid if reg_reloaded_contents is set and valid. */
148 static HARD_REG_SET reg_reloaded_dead;
150 /* Number of spill-regs so far; number of valid elements of spill_regs. */
151 static int n_spills;
153 /* In parallel with spill_regs, contains REG rtx's for those regs.
154 Holds the last rtx used for any given reg, or 0 if it has never
155 been used for spilling yet. This rtx is reused, provided it has
156 the proper mode. */
157 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
159 /* In parallel with spill_regs, contains nonzero for a spill reg
160 that was stored after the last time it was used.
161 The precise value is the insn generated to do the store. */
162 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
164 /* This is the register that was stored with spill_reg_store. This is a
165 copy of reload_out / reload_out_reg when the value was stored; if
166 reload_out is a MEM, spill_reg_stored_to will be set to reload_out_reg. */
167 static rtx spill_reg_stored_to[FIRST_PSEUDO_REGISTER];
169 /* This table is the inverse mapping of spill_regs:
170 indexed by hard reg number,
171 it contains the position of that reg in spill_regs,
172 or -1 for something that is not in spill_regs.
174 ?!? This is no longer accurate. */
175 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
177 /* This reg set indicates registers that can't be used as spill registers for
178 the currently processed insn. These are the hard registers which are live
179 during the insn, but not allocated to pseudos, as well as fixed
180 registers. */
181 static HARD_REG_SET bad_spill_regs;
183 /* These are the hard registers that can't be used as spill register for any
184 insn. This includes registers used for user variables and registers that
185 we can't eliminate. A register that appears in this set also can't be used
186 to retry register allocation. */
187 static HARD_REG_SET bad_spill_regs_global;
189 /* Describes order of use of registers for reloading
190 of spilled pseudo-registers. `n_spills' is the number of
191 elements that are actually valid; new ones are added at the end.
193 Both spill_regs and spill_reg_order are used on two occasions:
194 once during find_reload_regs, where they keep track of the spill registers
195 for a single insn, but also during reload_as_needed where they show all
196 the registers ever used by reload. For the latter case, the information
197 is calculated during finish_spills. */
198 static short spill_regs[FIRST_PSEUDO_REGISTER];
200 /* This vector of reg sets indicates, for each pseudo, which hard registers
201 may not be used for retrying global allocation because the register was
202 formerly spilled from one of them. If we allowed reallocating a pseudo to
203 a register that it was already allocated to, reload might not
204 terminate. */
205 static HARD_REG_SET *pseudo_previous_regs;
207 /* This vector of reg sets indicates, for each pseudo, which hard
208 registers may not be used for retrying global allocation because they
209 are used as spill registers during one of the insns in which the
210 pseudo is live. */
211 static HARD_REG_SET *pseudo_forbidden_regs;
213 /* All hard regs that have been used as spill registers for any insn are
214 marked in this set. */
215 static HARD_REG_SET used_spill_regs;
217 /* Index of last register assigned as a spill register. We allocate in
218 a round-robin fashion. */
219 static int last_spill_reg;
221 /* Nonzero if indirect addressing is supported on the machine; this means
222 that spilling (REG n) does not require reloading it into a register in
223 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
224 value indicates the level of indirect addressing supported, e.g., two
225 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
226 a hard register. */
227 static char spill_indirect_levels;
229 /* Nonzero if indirect addressing is supported when the innermost MEM is
230 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
231 which these are valid is the same as spill_indirect_levels, above. */
232 char indirect_symref_ok;
234 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
235 char double_reg_address_ok;
237 /* Record the stack slot for each spilled hard register. */
238 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
240 /* Width allocated so far for that stack slot. */
241 static unsigned int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
243 /* Record which pseudos needed to be spilled. */
244 static regset_head spilled_pseudos;
246 /* Used for communication between order_regs_for_reload and count_pseudo.
247 Used to avoid counting one pseudo twice. */
248 static regset_head pseudos_counted;
250 /* First uid used by insns created by reload in this function.
251 Used in find_equiv_reg. */
252 int reload_first_uid;
254 /* Flag set by local-alloc or global-alloc if anything is live in
255 a call-clobbered reg across calls. */
256 int caller_save_needed;
258 /* Set to 1 while reload_as_needed is operating.
259 Required by some machines to handle any generated moves differently. */
260 int reload_in_progress = 0;
262 /* These arrays record the insn_code of insns that may be needed to
263 perform input and output reloads of special objects. They provide a
264 place to pass a scratch register. */
265 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
266 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
268 /* This obstack is used for allocation of rtl during register elimination.
269 The allocated storage can be freed once find_reloads has processed the
270 insn. */
271 struct obstack reload_obstack;
273 /* Points to the beginning of the reload_obstack. All insn_chain structures
274 are allocated first. */
275 char *reload_startobj;
277 /* The point after all insn_chain structures. Used to quickly deallocate
278 memory allocated in copy_reloads during calculate_needs_all_insns. */
279 char *reload_firstobj;
281 /* This points before all local rtl generated by register elimination.
282 Used to quickly free all memory after processing one insn. */
283 static char *reload_insn_firstobj;
285 /* List of insn_chain instructions, one for every insn that reload needs to
286 examine. */
287 struct insn_chain *reload_insn_chain;
289 #ifdef TREE_CODE
290 extern tree current_function_decl;
291 #else
292 extern union tree_node *current_function_decl;
293 #endif
295 /* List of all insns needing reloads. */
296 static struct insn_chain *insns_need_reload;
298 /* This structure is used to record information about register eliminations.
299 Each array entry describes one possible way of eliminating a register
300 in favor of another. If there is more than one way of eliminating a
301 particular register, the most preferred should be specified first. */
303 struct elim_table
305 int from; /* Register number to be eliminated. */
306 int to; /* Register number used as replacement. */
307 int initial_offset; /* Initial difference between values. */
308 int can_eliminate; /* Nonzero if this elimination can be done. */
309 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
310 insns made by reload. */
311 int offset; /* Current offset between the two regs. */
312 int previous_offset; /* Offset at end of previous insn. */
313 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
314 rtx from_rtx; /* REG rtx for the register to be eliminated.
315 We cannot simply compare the number since
316 we might then spuriously replace a hard
317 register corresponding to a pseudo
318 assigned to the reg to be eliminated. */
319 rtx to_rtx; /* REG rtx for the replacement. */
322 static struct elim_table *reg_eliminate = 0;
324 /* This is an intermediate structure to initialize the table. It has
325 exactly the members provided by ELIMINABLE_REGS. */
326 static const struct elim_table_1
328 const int from;
329 const int to;
330 } reg_eliminate_1[] =
332 /* If a set of eliminable registers was specified, define the table from it.
333 Otherwise, default to the normal case of the frame pointer being
334 replaced by the stack pointer. */
336 #ifdef ELIMINABLE_REGS
337 ELIMINABLE_REGS;
338 #else
339 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
340 #endif
342 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
344 /* Record the number of pending eliminations that have an offset not equal
345 to their initial offset. If nonzero, we use a new copy of each
346 replacement result in any insns encountered. */
347 int num_not_at_initial_offset;
349 /* Count the number of registers that we may be able to eliminate. */
350 static int num_eliminable;
351 /* And the number of registers that are equivalent to a constant that
352 can be eliminated to frame_pointer / arg_pointer + constant. */
353 static int num_eliminable_invariants;
355 /* For each label, we record the offset of each elimination. If we reach
356 a label by more than one path and an offset differs, we cannot do the
357 elimination. This information is indexed by the difference of the
358 number of the label and the first label number. We can't offset the
359 pointer itself as this can cause problems on machines with segmented
360 memory. The first table is an array of flags that records whether we
361 have yet encountered a label and the second table is an array of arrays,
362 one entry in the latter array for each elimination. */
364 static int first_label_num;
365 static char *offsets_known_at;
366 static int (*offsets_at)[NUM_ELIMINABLE_REGS];
368 /* Number of labels in the current function. */
370 static int num_labels;
372 static void replace_pseudos_in_call_usage PARAMS ((rtx *,
373 enum machine_mode,
374 rtx));
375 static void maybe_fix_stack_asms PARAMS ((void));
376 static void copy_reloads PARAMS ((struct insn_chain *));
377 static void calculate_needs_all_insns PARAMS ((int));
378 static int find_reg PARAMS ((struct insn_chain *, int));
379 static void find_reload_regs PARAMS ((struct insn_chain *));
380 static void select_reload_regs PARAMS ((void));
381 static void delete_caller_save_insns PARAMS ((void));
383 static void spill_failure PARAMS ((rtx, enum reg_class));
384 static void count_spilled_pseudo PARAMS ((int, int, int));
385 static void delete_dead_insn PARAMS ((rtx));
386 static void alter_reg PARAMS ((int, int));
387 static void set_label_offsets PARAMS ((rtx, rtx, int));
388 static void check_eliminable_occurrences PARAMS ((rtx));
389 static void elimination_effects PARAMS ((rtx, enum machine_mode));
390 static int eliminate_regs_in_insn PARAMS ((rtx, int));
391 static void update_eliminable_offsets PARAMS ((void));
392 static void mark_not_eliminable PARAMS ((rtx, rtx, void *));
393 static void set_initial_elim_offsets PARAMS ((void));
394 static void verify_initial_elim_offsets PARAMS ((void));
395 static void set_initial_label_offsets PARAMS ((void));
396 static void set_offsets_for_label PARAMS ((rtx));
397 static void init_elim_table PARAMS ((void));
398 static void update_eliminables PARAMS ((HARD_REG_SET *));
399 static void spill_hard_reg PARAMS ((unsigned int, int));
400 static int finish_spills PARAMS ((int));
401 static void ior_hard_reg_set PARAMS ((HARD_REG_SET *, HARD_REG_SET *));
402 static void scan_paradoxical_subregs PARAMS ((rtx));
403 static void count_pseudo PARAMS ((int));
404 static void order_regs_for_reload PARAMS ((struct insn_chain *));
405 static void reload_as_needed PARAMS ((int));
406 static void forget_old_reloads_1 PARAMS ((rtx, rtx, void *));
407 static int reload_reg_class_lower PARAMS ((const PTR, const PTR));
408 static void mark_reload_reg_in_use PARAMS ((unsigned int, int,
409 enum reload_type,
410 enum machine_mode));
411 static void clear_reload_reg_in_use PARAMS ((unsigned int, int,
412 enum reload_type,
413 enum machine_mode));
414 static int reload_reg_free_p PARAMS ((unsigned int, int,
415 enum reload_type));
416 static int reload_reg_free_for_value_p PARAMS ((int, int, int,
417 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 void reload_cse_regs_1 PARAMS ((rtx));
446 static int reload_cse_noop_set_p PARAMS ((rtx));
447 static int reload_cse_simplify_set PARAMS ((rtx, rtx));
448 static int reload_cse_simplify_operands PARAMS ((rtx, rtx));
449 static void reload_combine PARAMS ((void));
450 static void reload_combine_note_use PARAMS ((rtx *, rtx));
451 static void reload_combine_note_store PARAMS ((rtx, rtx, void *));
452 static void reload_cse_move2add PARAMS ((rtx));
453 static void move2add_note_store PARAMS ((rtx, rtx, void *));
454 #ifdef AUTO_INC_DEC
455 static void add_auto_inc_notes PARAMS ((rtx, rtx));
456 #endif
457 static void copy_eh_notes PARAMS ((rtx, rtx));
458 static void failed_reload PARAMS ((rtx, int));
459 static int set_reload_reg PARAMS ((int, int));
460 static void reload_cse_simplify PARAMS ((rtx, rtx));
461 void fixup_abnormal_edges PARAMS ((void));
462 extern void dump_needs PARAMS ((struct insn_chain *));
464 /* Initialize the reload pass once per compilation. */
466 void
467 init_reload ()
469 int i;
471 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
472 Set spill_indirect_levels to the number of levels such addressing is
473 permitted, zero if it is not permitted at all. */
475 rtx tem
476 = gen_rtx_MEM (Pmode,
477 gen_rtx_PLUS (Pmode,
478 gen_rtx_REG (Pmode,
479 LAST_VIRTUAL_REGISTER + 1),
480 GEN_INT (4)));
481 spill_indirect_levels = 0;
483 while (memory_address_p (QImode, tem))
485 spill_indirect_levels++;
486 tem = gen_rtx_MEM (Pmode, tem);
489 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
491 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
492 indirect_symref_ok = memory_address_p (QImode, tem);
494 /* See if reg+reg is a valid (and offsettable) address. */
496 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
498 tem = gen_rtx_PLUS (Pmode,
499 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
500 gen_rtx_REG (Pmode, i));
502 /* This way, we make sure that reg+reg is an offsettable address. */
503 tem = plus_constant (tem, 4);
505 if (memory_address_p (QImode, tem))
507 double_reg_address_ok = 1;
508 break;
512 /* Initialize obstack for our rtl allocation. */
513 gcc_obstack_init (&reload_obstack);
514 reload_startobj = (char *) obstack_alloc (&reload_obstack, 0);
516 INIT_REG_SET (&spilled_pseudos);
517 INIT_REG_SET (&pseudos_counted);
520 /* List of insn chains that are currently unused. */
521 static struct insn_chain *unused_insn_chains = 0;
523 /* Allocate an empty insn_chain structure. */
524 struct insn_chain *
525 new_insn_chain ()
527 struct insn_chain *c;
529 if (unused_insn_chains == 0)
531 c = (struct insn_chain *)
532 obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
533 INIT_REG_SET (&c->live_throughout);
534 INIT_REG_SET (&c->dead_or_set);
536 else
538 c = unused_insn_chains;
539 unused_insn_chains = c->next;
541 c->is_caller_save_insn = 0;
542 c->need_operand_change = 0;
543 c->need_reload = 0;
544 c->need_elim = 0;
545 return c;
548 /* Small utility function to set all regs in hard reg set TO which are
549 allocated to pseudos in regset FROM. */
551 void
552 compute_use_by_pseudos (to, from)
553 HARD_REG_SET *to;
554 regset from;
556 unsigned int regno;
558 EXECUTE_IF_SET_IN_REG_SET
559 (from, FIRST_PSEUDO_REGISTER, regno,
561 int r = reg_renumber[regno];
562 int nregs;
564 if (r < 0)
566 /* reload_combine uses the information from
567 BASIC_BLOCK->global_live_at_start, which might still
568 contain registers that have not actually been allocated
569 since they have an equivalence. */
570 if (! reload_completed)
571 abort ();
573 else
575 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (regno));
576 while (nregs-- > 0)
577 SET_HARD_REG_BIT (*to, r + nregs);
582 /* Replace all pseudos found in LOC with their corresponding
583 equivalences. */
585 static void
586 replace_pseudos_in_call_usage (loc, mem_mode, usage)
587 rtx *loc;
588 enum machine_mode mem_mode;
589 rtx usage;
591 rtx x = *loc;
592 enum rtx_code code;
593 const char *fmt;
594 int i, j;
596 if (! x)
597 return;
599 code = GET_CODE (x);
600 if (code == REG)
602 unsigned int regno = REGNO (x);
604 if (regno < FIRST_PSEUDO_REGISTER)
605 return;
607 x = eliminate_regs (x, mem_mode, usage);
608 if (x != *loc)
610 *loc = x;
611 replace_pseudos_in_call_usage (loc, mem_mode, usage);
612 return;
615 if (reg_equiv_constant[regno])
616 *loc = reg_equiv_constant[regno];
617 else if (reg_equiv_mem[regno])
618 *loc = reg_equiv_mem[regno];
619 else if (reg_equiv_address[regno])
620 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address[regno]);
621 else if (GET_CODE (regno_reg_rtx[regno]) != REG
622 || REGNO (regno_reg_rtx[regno]) != regno)
623 *loc = regno_reg_rtx[regno];
624 else
625 abort ();
627 return;
629 else if (code == MEM)
631 replace_pseudos_in_call_usage (& XEXP (x, 0), GET_MODE (x), usage);
632 return;
635 /* Process each of our operands recursively. */
636 fmt = GET_RTX_FORMAT (code);
637 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
638 if (*fmt == 'e')
639 replace_pseudos_in_call_usage (&XEXP (x, i), mem_mode, usage);
640 else if (*fmt == 'E')
641 for (j = 0; j < XVECLEN (x, i); j++)
642 replace_pseudos_in_call_usage (& XVECEXP (x, i, j), mem_mode, usage);
646 /* Global variables used by reload and its subroutines. */
648 /* Set during calculate_needs if an insn needs register elimination. */
649 static int something_needs_elimination;
650 /* Set during calculate_needs if an insn needs an operand changed. */
651 int something_needs_operands_changed;
653 /* Nonzero means we couldn't get enough spill regs. */
654 static int failure;
656 /* Main entry point for the reload pass.
658 FIRST is the first insn of the function being compiled.
660 GLOBAL nonzero means we were called from global_alloc
661 and should attempt to reallocate any pseudoregs that we
662 displace from hard regs we will use for reloads.
663 If GLOBAL is zero, we do not have enough information to do that,
664 so any pseudo reg that is spilled must go to the stack.
666 Return value is nonzero if reload failed
667 and we must not do any more for this function. */
670 reload (first, global)
671 rtx first;
672 int global;
674 int i;
675 rtx insn;
676 struct elim_table *ep;
677 basic_block bb;
679 /* Make sure even insns with volatile mem refs are recognizable. */
680 init_recog ();
682 failure = 0;
684 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
686 /* Make sure that the last insn in the chain
687 is not something that needs reloading. */
688 emit_note (NULL, NOTE_INSN_DELETED);
690 /* Enable find_equiv_reg to distinguish insns made by reload. */
691 reload_first_uid = get_max_uid ();
693 #ifdef SECONDARY_MEMORY_NEEDED
694 /* Initialize the secondary memory table. */
695 clear_secondary_mem ();
696 #endif
698 /* We don't have a stack slot for any spill reg yet. */
699 memset ((char *) spill_stack_slot, 0, sizeof spill_stack_slot);
700 memset ((char *) spill_stack_slot_width, 0, sizeof spill_stack_slot_width);
702 /* Initialize the save area information for caller-save, in case some
703 are needed. */
704 init_save_areas ();
706 /* Compute which hard registers are now in use
707 as homes for pseudo registers.
708 This is done here rather than (eg) in global_alloc
709 because this point is reached even if not optimizing. */
710 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
711 mark_home_live (i);
713 /* A function that receives a nonlocal goto must save all call-saved
714 registers. */
715 if (current_function_has_nonlocal_label)
716 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
717 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
718 regs_ever_live[i] = 1;
720 /* Find all the pseudo registers that didn't get hard regs
721 but do have known equivalent constants or memory slots.
722 These include parameters (known equivalent to parameter slots)
723 and cse'd or loop-moved constant memory addresses.
725 Record constant equivalents in reg_equiv_constant
726 so they will be substituted by find_reloads.
727 Record memory equivalents in reg_mem_equiv so they can
728 be substituted eventually by altering the REG-rtx's. */
730 reg_equiv_constant = (rtx *) xcalloc (max_regno, sizeof (rtx));
731 reg_equiv_mem = (rtx *) xcalloc (max_regno, sizeof (rtx));
732 reg_equiv_init = (rtx *) xcalloc (max_regno, sizeof (rtx));
733 reg_equiv_address = (rtx *) xcalloc (max_regno, sizeof (rtx));
734 reg_max_ref_width = (unsigned int *) xcalloc (max_regno, sizeof (int));
735 reg_old_renumber = (short *) xcalloc (max_regno, sizeof (short));
736 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
737 pseudo_forbidden_regs
738 = (HARD_REG_SET *) xmalloc (max_regno * sizeof (HARD_REG_SET));
739 pseudo_previous_regs
740 = (HARD_REG_SET *) xcalloc (max_regno, sizeof (HARD_REG_SET));
742 CLEAR_HARD_REG_SET (bad_spill_regs_global);
744 /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
745 Also find all paradoxical subregs and find largest such for each pseudo.
746 On machines with small register classes, record hard registers that
747 are used for user variables. These can never be used for spills.
748 Also look for a "constant" REG_SETJMP. This means that all
749 caller-saved registers must be marked live. */
751 num_eliminable_invariants = 0;
752 for (insn = first; insn; insn = NEXT_INSN (insn))
754 rtx set = single_set (insn);
756 /* We may introduce USEs that we want to remove at the end, so
757 we'll mark them with QImode. Make sure there are no
758 previously-marked insns left by say regmove. */
759 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
760 && GET_MODE (insn) != VOIDmode)
761 PUT_MODE (insn, VOIDmode);
763 if (GET_CODE (insn) == CALL_INSN
764 && find_reg_note (insn, REG_SETJMP, NULL))
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 /* A function invariant is often CONSTANT_P but may
777 include a register. We promise to only pass
778 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
779 || (CONSTANT_P (XEXP (note, 0))
780 && LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0))))
781 #endif
784 rtx x = XEXP (note, 0);
785 i = REGNO (SET_DEST (set));
786 if (i > LAST_VIRTUAL_REGISTER)
788 /* It can happen that a REG_EQUIV note contains a MEM
789 that is not a legitimate memory operand. As later
790 stages of reload assume that all addresses found
791 in the reg_equiv_* arrays were originally legitimate,
792 we ignore such REG_EQUIV notes. */
793 if (memory_operand (x, VOIDmode))
795 /* Always unshare the equivalence, so we can
796 substitute into this insn without touching the
797 equivalence. */
798 reg_equiv_memory_loc[i] = copy_rtx (x);
800 else if (function_invariant_p (x))
802 if (GET_CODE (x) == PLUS)
804 /* This is PLUS of frame pointer and a constant,
805 and might be shared. Unshare it. */
806 reg_equiv_constant[i] = copy_rtx (x);
807 num_eliminable_invariants++;
809 else if (x == frame_pointer_rtx
810 || x == arg_pointer_rtx)
812 reg_equiv_constant[i] = x;
813 num_eliminable_invariants++;
815 else if (LEGITIMATE_CONSTANT_P (x))
816 reg_equiv_constant[i] = x;
817 else
819 reg_equiv_memory_loc[i]
820 = force_const_mem (GET_MODE (SET_DEST (set)), x);
821 if (!reg_equiv_memory_loc[i])
822 continue;
825 else
826 continue;
828 /* If this register is being made equivalent to a MEM
829 and the MEM is not SET_SRC, the equivalencing insn
830 is one with the MEM as a SET_DEST and it occurs later.
831 So don't mark this insn now. */
832 if (GET_CODE (x) != MEM
833 || rtx_equal_p (SET_SRC (set), x))
834 reg_equiv_init[i]
835 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[i]);
840 /* If this insn is setting a MEM from a register equivalent to it,
841 this is the equivalencing insn. */
842 else if (set && GET_CODE (SET_DEST (set)) == MEM
843 && GET_CODE (SET_SRC (set)) == REG
844 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
845 && rtx_equal_p (SET_DEST (set),
846 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
847 reg_equiv_init[REGNO (SET_SRC (set))]
848 = gen_rtx_INSN_LIST (VOIDmode, insn,
849 reg_equiv_init[REGNO (SET_SRC (set))]);
851 if (INSN_P (insn))
852 scan_paradoxical_subregs (PATTERN (insn));
855 init_elim_table ();
857 first_label_num = get_first_label_num ();
858 num_labels = max_label_num () - first_label_num;
860 /* Allocate the tables used to store offset information at labels. */
861 /* We used to use alloca here, but the size of what it would try to
862 allocate would occasionally cause it to exceed the stack limit and
863 cause a core dump. */
864 offsets_known_at = xmalloc (num_labels);
865 offsets_at
866 = (int (*)[NUM_ELIMINABLE_REGS])
867 xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
869 /* Alter each pseudo-reg rtx to contain its hard reg number.
870 Assign stack slots to the pseudos that lack hard regs or equivalents.
871 Do not touch virtual registers. */
873 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
874 alter_reg (i, -1);
876 /* If we have some registers we think can be eliminated, scan all insns to
877 see if there is an insn that sets one of these registers to something
878 other than itself plus a constant. If so, the register cannot be
879 eliminated. Doing this scan here eliminates an extra pass through the
880 main reload loop in the most common case where register elimination
881 cannot be done. */
882 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
883 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
884 || GET_CODE (insn) == CALL_INSN)
885 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
887 maybe_fix_stack_asms ();
889 insns_need_reload = 0;
890 something_needs_elimination = 0;
892 /* Initialize to -1, which means take the first spill register. */
893 last_spill_reg = -1;
895 /* Spill any hard regs that we know we can't eliminate. */
896 CLEAR_HARD_REG_SET (used_spill_regs);
897 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
898 if (! ep->can_eliminate)
899 spill_hard_reg (ep->from, 1);
901 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
902 if (frame_pointer_needed)
903 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
904 #endif
905 finish_spills (global);
907 /* From now on, we may need to generate moves differently. We may also
908 allow modifications of insns which cause them to not be recognized.
909 Any such modifications will be cleaned up during reload itself. */
910 reload_in_progress = 1;
912 /* This loop scans the entire function each go-round
913 and repeats until one repetition spills no additional hard regs. */
914 for (;;)
916 int something_changed;
917 int did_spill;
919 HOST_WIDE_INT starting_frame_size;
921 /* Round size of stack frame to stack_alignment_needed. This must be done
922 here because the stack size may be a part of the offset computation
923 for register elimination, and there might have been new stack slots
924 created in the last iteration of this loop. */
925 if (cfun->stack_alignment_needed)
926 assign_stack_local (BLKmode, 0, cfun->stack_alignment_needed);
928 starting_frame_size = get_frame_size ();
930 set_initial_elim_offsets ();
931 set_initial_label_offsets ();
933 /* For each pseudo register that has an equivalent location defined,
934 try to eliminate any eliminable registers (such as the frame pointer)
935 assuming initial offsets for the replacement register, which
936 is the normal case.
938 If the resulting location is directly addressable, substitute
939 the MEM we just got directly for the old REG.
941 If it is not addressable but is a constant or the sum of a hard reg
942 and constant, it is probably not addressable because the constant is
943 out of range, in that case record the address; we will generate
944 hairy code to compute the address in a register each time it is
945 needed. Similarly if it is a hard register, but one that is not
946 valid as an address register.
948 If the location is not addressable, but does not have one of the
949 above forms, assign a stack slot. We have to do this to avoid the
950 potential of producing lots of reloads if, e.g., a location involves
951 a pseudo that didn't get a hard register and has an equivalent memory
952 location that also involves a pseudo that didn't get a hard register.
954 Perhaps at some point we will improve reload_when_needed handling
955 so this problem goes away. But that's very hairy. */
957 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
958 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
960 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
962 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
963 XEXP (x, 0)))
964 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
965 else if (CONSTANT_P (XEXP (x, 0))
966 || (GET_CODE (XEXP (x, 0)) == REG
967 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
968 || (GET_CODE (XEXP (x, 0)) == PLUS
969 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
970 && (REGNO (XEXP (XEXP (x, 0), 0))
971 < FIRST_PSEUDO_REGISTER)
972 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
973 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
974 else
976 /* Make a new stack slot. Then indicate that something
977 changed so we go back and recompute offsets for
978 eliminable registers because the allocation of memory
979 below might change some offset. reg_equiv_{mem,address}
980 will be set up for this pseudo on the next pass around
981 the loop. */
982 reg_equiv_memory_loc[i] = 0;
983 reg_equiv_init[i] = 0;
984 alter_reg (i, -1);
988 if (caller_save_needed)
989 setup_save_areas ();
991 /* If we allocated another stack slot, redo elimination bookkeeping. */
992 if (starting_frame_size != get_frame_size ())
993 continue;
995 if (caller_save_needed)
997 save_call_clobbered_regs ();
998 /* That might have allocated new insn_chain structures. */
999 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
1002 calculate_needs_all_insns (global);
1004 CLEAR_REG_SET (&spilled_pseudos);
1005 did_spill = 0;
1007 something_changed = 0;
1009 /* If we allocated any new memory locations, make another pass
1010 since it might have changed elimination offsets. */
1011 if (starting_frame_size != get_frame_size ())
1012 something_changed = 1;
1015 HARD_REG_SET to_spill;
1016 CLEAR_HARD_REG_SET (to_spill);
1017 update_eliminables (&to_spill);
1018 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1019 if (TEST_HARD_REG_BIT (to_spill, i))
1021 spill_hard_reg (i, 1);
1022 did_spill = 1;
1024 /* Regardless of the state of spills, if we previously had
1025 a register that we thought we could eliminate, but now can
1026 not eliminate, we must run another pass.
1028 Consider pseudos which have an entry in reg_equiv_* which
1029 reference an eliminable register. We must make another pass
1030 to update reg_equiv_* so that we do not substitute in the
1031 old value from when we thought the elimination could be
1032 performed. */
1033 something_changed = 1;
1037 select_reload_regs ();
1038 if (failure)
1039 goto failed;
1041 if (insns_need_reload != 0 || did_spill)
1042 something_changed |= finish_spills (global);
1044 if (! something_changed)
1045 break;
1047 if (caller_save_needed)
1048 delete_caller_save_insns ();
1050 obstack_free (&reload_obstack, reload_firstobj);
1053 /* If global-alloc was run, notify it of any register eliminations we have
1054 done. */
1055 if (global)
1056 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1057 if (ep->can_eliminate)
1058 mark_elimination (ep->from, ep->to);
1060 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1061 If that insn didn't set the register (i.e., it copied the register to
1062 memory), just delete that insn instead of the equivalencing insn plus
1063 anything now dead. If we call delete_dead_insn on that insn, we may
1064 delete the insn that actually sets the register if the register dies
1065 there and that is incorrect. */
1067 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1069 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1071 rtx list;
1072 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1074 rtx equiv_insn = XEXP (list, 0);
1076 /* If we already deleted the insn or if it may trap, we can't
1077 delete it. The latter case shouldn't happen, but can
1078 if an insn has a variable address, gets a REG_EH_REGION
1079 note added to it, and then gets converted into an load
1080 from a constant address. */
1081 if (GET_CODE (equiv_insn) == NOTE
1082 || can_throw_internal (equiv_insn))
1084 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1085 delete_dead_insn (equiv_insn);
1086 else
1088 PUT_CODE (equiv_insn, NOTE);
1089 NOTE_SOURCE_FILE (equiv_insn) = 0;
1090 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1096 /* Use the reload registers where necessary
1097 by generating move instructions to move the must-be-register
1098 values into or out of the reload registers. */
1100 if (insns_need_reload != 0 || something_needs_elimination
1101 || something_needs_operands_changed)
1103 HOST_WIDE_INT old_frame_size = get_frame_size ();
1105 reload_as_needed (global);
1107 if (old_frame_size != get_frame_size ())
1108 abort ();
1110 if (num_eliminable)
1111 verify_initial_elim_offsets ();
1114 /* If we were able to eliminate the frame pointer, show that it is no
1115 longer live at the start of any basic block. If it ls live by
1116 virtue of being in a pseudo, that pseudo will be marked live
1117 and hence the frame pointer will be known to be live via that
1118 pseudo. */
1120 if (! frame_pointer_needed)
1121 FOR_EACH_BB (bb)
1122 CLEAR_REGNO_REG_SET (bb->global_live_at_start,
1123 HARD_FRAME_POINTER_REGNUM);
1125 /* Come here (with failure set nonzero) if we can't get enough spill regs
1126 and we decide not to abort about it. */
1127 failed:
1129 CLEAR_REG_SET (&spilled_pseudos);
1130 reload_in_progress = 0;
1132 /* Now eliminate all pseudo regs by modifying them into
1133 their equivalent memory references.
1134 The REG-rtx's for the pseudos are modified in place,
1135 so all insns that used to refer to them now refer to memory.
1137 For a reg that has a reg_equiv_address, all those insns
1138 were changed by reloading so that no insns refer to it any longer;
1139 but the DECL_RTL of a variable decl may refer to it,
1140 and if so this causes the debugging info to mention the variable. */
1142 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1144 rtx addr = 0;
1146 if (reg_equiv_mem[i])
1147 addr = XEXP (reg_equiv_mem[i], 0);
1149 if (reg_equiv_address[i])
1150 addr = reg_equiv_address[i];
1152 if (addr)
1154 if (reg_renumber[i] < 0)
1156 rtx reg = regno_reg_rtx[i];
1158 REG_USERVAR_P (reg) = 0;
1159 PUT_CODE (reg, MEM);
1160 XEXP (reg, 0) = addr;
1161 if (reg_equiv_memory_loc[i])
1162 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc[i]);
1163 else
1165 RTX_UNCHANGING_P (reg) = MEM_IN_STRUCT_P (reg)
1166 = MEM_SCALAR_P (reg) = 0;
1167 MEM_ATTRS (reg) = 0;
1170 else if (reg_equiv_mem[i])
1171 XEXP (reg_equiv_mem[i], 0) = addr;
1175 /* We must set reload_completed now since the cleanup_subreg_operands call
1176 below will re-recognize each insn and reload may have generated insns
1177 which are only valid during and after reload. */
1178 reload_completed = 1;
1180 /* Make a pass over all the insns and delete all USEs which we inserted
1181 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1182 notes. Delete all CLOBBER insns, except those that refer to the return
1183 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1184 from misarranging variable-array code, and simplify (subreg (reg))
1185 operands. Also remove all REG_RETVAL and REG_LIBCALL notes since they
1186 are no longer useful or accurate. Strip and regenerate REG_INC notes
1187 that may have been moved around. */
1189 for (insn = first; insn; insn = NEXT_INSN (insn))
1190 if (INSN_P (insn))
1192 rtx *pnote;
1194 if (GET_CODE (insn) == CALL_INSN)
1195 replace_pseudos_in_call_usage (& CALL_INSN_FUNCTION_USAGE (insn),
1196 VOIDmode,
1197 CALL_INSN_FUNCTION_USAGE (insn));
1199 if ((GET_CODE (PATTERN (insn)) == USE
1200 /* We mark with QImode USEs introduced by reload itself. */
1201 && (GET_MODE (insn) == QImode
1202 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
1203 || (GET_CODE (PATTERN (insn)) == CLOBBER
1204 && (GET_CODE (XEXP (PATTERN (insn), 0)) != MEM
1205 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1206 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1207 && XEXP (XEXP (PATTERN (insn), 0), 0)
1208 != stack_pointer_rtx))
1209 && (GET_CODE (XEXP (PATTERN (insn), 0)) != REG
1210 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1212 delete_insn (insn);
1213 continue;
1216 pnote = &REG_NOTES (insn);
1217 while (*pnote != 0)
1219 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1220 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1221 || REG_NOTE_KIND (*pnote) == REG_INC
1222 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1223 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1224 *pnote = XEXP (*pnote, 1);
1225 else
1226 pnote = &XEXP (*pnote, 1);
1229 #ifdef AUTO_INC_DEC
1230 add_auto_inc_notes (insn, PATTERN (insn));
1231 #endif
1233 /* And simplify (subreg (reg)) if it appears as an operand. */
1234 cleanup_subreg_operands (insn);
1237 /* If we are doing stack checking, give a warning if this function's
1238 frame size is larger than we expect. */
1239 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1241 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1242 static int verbose_warned = 0;
1244 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1245 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1246 size += UNITS_PER_WORD;
1248 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1250 warning ("frame size too large for reliable stack checking");
1251 if (! verbose_warned)
1253 warning ("try reducing the number of local variables");
1254 verbose_warned = 1;
1259 /* Indicate that we no longer have known memory locations or constants. */
1260 if (reg_equiv_constant)
1261 free (reg_equiv_constant);
1262 reg_equiv_constant = 0;
1263 if (reg_equiv_memory_loc)
1264 free (reg_equiv_memory_loc);
1265 reg_equiv_memory_loc = 0;
1267 if (offsets_known_at)
1268 free (offsets_known_at);
1269 if (offsets_at)
1270 free (offsets_at);
1272 free (reg_equiv_mem);
1273 free (reg_equiv_init);
1274 free (reg_equiv_address);
1275 free (reg_max_ref_width);
1276 free (reg_old_renumber);
1277 free (pseudo_previous_regs);
1278 free (pseudo_forbidden_regs);
1280 CLEAR_HARD_REG_SET (used_spill_regs);
1281 for (i = 0; i < n_spills; i++)
1282 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1284 /* Free all the insn_chain structures at once. */
1285 obstack_free (&reload_obstack, reload_startobj);
1286 unused_insn_chains = 0;
1287 fixup_abnormal_edges ();
1289 /* Replacing pseudos with their memory equivalents might have
1290 created shared rtx. Subsequent passes would get confused
1291 by this, so unshare everything here. */
1292 unshare_all_rtl_again (first);
1294 return failure;
1297 /* Yet another special case. Unfortunately, reg-stack forces people to
1298 write incorrect clobbers in asm statements. These clobbers must not
1299 cause the register to appear in bad_spill_regs, otherwise we'll call
1300 fatal_insn later. We clear the corresponding regnos in the live
1301 register sets to avoid this.
1302 The whole thing is rather sick, I'm afraid. */
1304 static void
1305 maybe_fix_stack_asms ()
1307 #ifdef STACK_REGS
1308 const char *constraints[MAX_RECOG_OPERANDS];
1309 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1310 struct insn_chain *chain;
1312 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1314 int i, noperands;
1315 HARD_REG_SET clobbered, allowed;
1316 rtx pat;
1318 if (! INSN_P (chain->insn)
1319 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1320 continue;
1321 pat = PATTERN (chain->insn);
1322 if (GET_CODE (pat) != PARALLEL)
1323 continue;
1325 CLEAR_HARD_REG_SET (clobbered);
1326 CLEAR_HARD_REG_SET (allowed);
1328 /* First, make a mask of all stack regs that are clobbered. */
1329 for (i = 0; i < XVECLEN (pat, 0); i++)
1331 rtx t = XVECEXP (pat, 0, i);
1332 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1333 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1336 /* Get the operand values and constraints out of the insn. */
1337 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1338 constraints, operand_mode);
1340 /* For every operand, see what registers are allowed. */
1341 for (i = 0; i < noperands; i++)
1343 const char *p = constraints[i];
1344 /* For every alternative, we compute the class of registers allowed
1345 for reloading in CLS, and merge its contents into the reg set
1346 ALLOWED. */
1347 int cls = (int) NO_REGS;
1349 for (;;)
1351 char c = *p;
1353 if (c == '\0' || c == ',' || c == '#')
1355 /* End of one alternative - mark the regs in the current
1356 class, and reset the class. */
1357 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1358 cls = NO_REGS;
1359 p++;
1360 if (c == '#')
1361 do {
1362 c = *p++;
1363 } while (c != '\0' && c != ',');
1364 if (c == '\0')
1365 break;
1366 continue;
1369 switch (c)
1371 case '=': case '+': case '*': case '%': case '?': case '!':
1372 case '0': case '1': case '2': case '3': case '4': case 'm':
1373 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1374 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1375 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1376 case 'P':
1377 break;
1379 case 'p':
1380 cls = (int) reg_class_subunion[cls]
1381 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1382 break;
1384 case 'g':
1385 case 'r':
1386 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1387 break;
1389 default:
1390 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1391 cls = (int) reg_class_subunion[cls]
1392 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1393 else
1394 cls = (int) reg_class_subunion[cls]
1395 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1397 p += CONSTRAINT_LEN (c, p);
1400 /* Those of the registers which are clobbered, but allowed by the
1401 constraints, must be usable as reload registers. So clear them
1402 out of the life information. */
1403 AND_HARD_REG_SET (allowed, clobbered);
1404 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1405 if (TEST_HARD_REG_BIT (allowed, i))
1407 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1408 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1412 #endif
1415 /* Copy the global variables n_reloads and rld into the corresponding elts
1416 of CHAIN. */
1417 static void
1418 copy_reloads (chain)
1419 struct insn_chain *chain;
1421 chain->n_reloads = n_reloads;
1422 chain->rld
1423 = (struct reload *) obstack_alloc (&reload_obstack,
1424 n_reloads * sizeof (struct reload));
1425 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1426 reload_insn_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
1429 /* Walk the chain of insns, and determine for each whether it needs reloads
1430 and/or eliminations. Build the corresponding insns_need_reload list, and
1431 set something_needs_elimination as appropriate. */
1432 static void
1433 calculate_needs_all_insns (global)
1434 int global;
1436 struct insn_chain **pprev_reload = &insns_need_reload;
1437 struct insn_chain *chain, *next = 0;
1439 something_needs_elimination = 0;
1441 reload_insn_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
1442 for (chain = reload_insn_chain; chain != 0; chain = next)
1444 rtx insn = chain->insn;
1446 next = chain->next;
1448 /* Clear out the shortcuts. */
1449 chain->n_reloads = 0;
1450 chain->need_elim = 0;
1451 chain->need_reload = 0;
1452 chain->need_operand_change = 0;
1454 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1455 include REG_LABEL), we need to see what effects this has on the
1456 known offsets at labels. */
1458 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
1459 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1460 set_label_offsets (insn, insn, 0);
1462 if (INSN_P (insn))
1464 rtx old_body = PATTERN (insn);
1465 int old_code = INSN_CODE (insn);
1466 rtx old_notes = REG_NOTES (insn);
1467 int did_elimination = 0;
1468 int operands_changed = 0;
1469 rtx set = single_set (insn);
1471 /* Skip insns that only set an equivalence. */
1472 if (set && GET_CODE (SET_DEST (set)) == REG
1473 && reg_renumber[REGNO (SET_DEST (set))] < 0
1474 && reg_equiv_constant[REGNO (SET_DEST (set))])
1475 continue;
1477 /* If needed, eliminate any eliminable registers. */
1478 if (num_eliminable || num_eliminable_invariants)
1479 did_elimination = eliminate_regs_in_insn (insn, 0);
1481 /* Analyze the instruction. */
1482 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1483 global, spill_reg_order);
1485 /* If a no-op set needs more than one reload, this is likely
1486 to be something that needs input address reloads. We
1487 can't get rid of this cleanly later, and it is of no use
1488 anyway, so discard it now.
1489 We only do this when expensive_optimizations is enabled,
1490 since this complements reload inheritance / output
1491 reload deletion, and it can make debugging harder. */
1492 if (flag_expensive_optimizations && n_reloads > 1)
1494 rtx set = single_set (insn);
1495 if (set
1496 && SET_SRC (set) == SET_DEST (set)
1497 && GET_CODE (SET_SRC (set)) == REG
1498 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1500 delete_insn (insn);
1501 /* Delete it from the reload chain. */
1502 if (chain->prev)
1503 chain->prev->next = next;
1504 else
1505 reload_insn_chain = next;
1506 if (next)
1507 next->prev = chain->prev;
1508 chain->next = unused_insn_chains;
1509 unused_insn_chains = chain;
1510 continue;
1513 if (num_eliminable)
1514 update_eliminable_offsets ();
1516 /* Remember for later shortcuts which insns had any reloads or
1517 register eliminations. */
1518 chain->need_elim = did_elimination;
1519 chain->need_reload = n_reloads > 0;
1520 chain->need_operand_change = operands_changed;
1522 /* Discard any register replacements done. */
1523 if (did_elimination)
1525 obstack_free (&reload_obstack, reload_insn_firstobj);
1526 PATTERN (insn) = old_body;
1527 INSN_CODE (insn) = old_code;
1528 REG_NOTES (insn) = old_notes;
1529 something_needs_elimination = 1;
1532 something_needs_operands_changed |= operands_changed;
1534 if (n_reloads != 0)
1536 copy_reloads (chain);
1537 *pprev_reload = chain;
1538 pprev_reload = &chain->next_need_reload;
1542 *pprev_reload = 0;
1545 /* Comparison function for qsort to decide which of two reloads
1546 should be handled first. *P1 and *P2 are the reload numbers. */
1548 static int
1549 reload_reg_class_lower (r1p, r2p)
1550 const PTR r1p;
1551 const PTR r2p;
1553 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1554 int t;
1556 /* Consider required reloads before optional ones. */
1557 t = rld[r1].optional - rld[r2].optional;
1558 if (t != 0)
1559 return t;
1561 /* Count all solitary classes before non-solitary ones. */
1562 t = ((reg_class_size[(int) rld[r2].class] == 1)
1563 - (reg_class_size[(int) rld[r1].class] == 1));
1564 if (t != 0)
1565 return t;
1567 /* Aside from solitaires, consider all multi-reg groups first. */
1568 t = rld[r2].nregs - rld[r1].nregs;
1569 if (t != 0)
1570 return t;
1572 /* Consider reloads in order of increasing reg-class number. */
1573 t = (int) rld[r1].class - (int) rld[r2].class;
1574 if (t != 0)
1575 return t;
1577 /* If reloads are equally urgent, sort by reload number,
1578 so that the results of qsort leave nothing to chance. */
1579 return r1 - r2;
1582 /* The cost of spilling each hard reg. */
1583 static int spill_cost[FIRST_PSEUDO_REGISTER];
1585 /* When spilling multiple hard registers, we use SPILL_COST for the first
1586 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1587 only the first hard reg for a multi-reg pseudo. */
1588 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1590 /* Update the spill cost arrays, considering that pseudo REG is live. */
1592 static void
1593 count_pseudo (reg)
1594 int reg;
1596 int freq = REG_FREQ (reg);
1597 int r = reg_renumber[reg];
1598 int nregs;
1600 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1601 || REGNO_REG_SET_P (&spilled_pseudos, reg))
1602 return;
1604 SET_REGNO_REG_SET (&pseudos_counted, reg);
1606 if (r < 0)
1607 abort ();
1609 spill_add_cost[r] += freq;
1611 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
1612 while (nregs-- > 0)
1613 spill_cost[r + nregs] += freq;
1616 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1617 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1619 static void
1620 order_regs_for_reload (chain)
1621 struct insn_chain *chain;
1623 int i;
1624 HARD_REG_SET used_by_pseudos;
1625 HARD_REG_SET used_by_pseudos2;
1627 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1629 memset (spill_cost, 0, sizeof spill_cost);
1630 memset (spill_add_cost, 0, sizeof spill_add_cost);
1632 /* Count number of uses of each hard reg by pseudo regs allocated to it
1633 and then order them by decreasing use. First exclude hard registers
1634 that are live in or across this insn. */
1636 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1637 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1638 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1639 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1641 /* Now find out which pseudos are allocated to it, and update
1642 hard_reg_n_uses. */
1643 CLEAR_REG_SET (&pseudos_counted);
1645 EXECUTE_IF_SET_IN_REG_SET
1646 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i,
1648 count_pseudo (i);
1650 EXECUTE_IF_SET_IN_REG_SET
1651 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i,
1653 count_pseudo (i);
1655 CLEAR_REG_SET (&pseudos_counted);
1658 /* Vector of reload-numbers showing the order in which the reloads should
1659 be processed. */
1660 static short reload_order[MAX_RELOADS];
1662 /* This is used to keep track of the spill regs used in one insn. */
1663 static HARD_REG_SET used_spill_regs_local;
1665 /* We decided to spill hard register SPILLED, which has a size of
1666 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1667 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1668 update SPILL_COST/SPILL_ADD_COST. */
1670 static void
1671 count_spilled_pseudo (spilled, spilled_nregs, reg)
1672 int spilled, spilled_nregs, reg;
1674 int r = reg_renumber[reg];
1675 int nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
1677 if (REGNO_REG_SET_P (&spilled_pseudos, reg)
1678 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1679 return;
1681 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1683 spill_add_cost[r] -= REG_FREQ (reg);
1684 while (nregs-- > 0)
1685 spill_cost[r + nregs] -= REG_FREQ (reg);
1688 /* Find reload register to use for reload number ORDER. */
1690 static int
1691 find_reg (chain, order)
1692 struct insn_chain *chain;
1693 int order;
1695 int rnum = reload_order[order];
1696 struct reload *rl = rld + rnum;
1697 int best_cost = INT_MAX;
1698 int best_reg = -1;
1699 unsigned int i, j;
1700 int k;
1701 HARD_REG_SET not_usable;
1702 HARD_REG_SET used_by_other_reload;
1704 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1705 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1706 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->class]);
1708 CLEAR_HARD_REG_SET (used_by_other_reload);
1709 for (k = 0; k < order; k++)
1711 int other = reload_order[k];
1713 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1714 for (j = 0; j < rld[other].nregs; j++)
1715 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1718 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1720 unsigned int regno = i;
1722 if (! TEST_HARD_REG_BIT (not_usable, regno)
1723 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1724 && HARD_REGNO_MODE_OK (regno, rl->mode))
1726 int this_cost = spill_cost[regno];
1727 int ok = 1;
1728 unsigned int this_nregs = HARD_REGNO_NREGS (regno, rl->mode);
1730 for (j = 1; j < this_nregs; j++)
1732 this_cost += spill_add_cost[regno + j];
1733 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1734 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1735 ok = 0;
1737 if (! ok)
1738 continue;
1739 if (rl->in && GET_CODE (rl->in) == REG && REGNO (rl->in) == regno)
1740 this_cost--;
1741 if (rl->out && GET_CODE (rl->out) == REG && REGNO (rl->out) == regno)
1742 this_cost--;
1743 if (this_cost < best_cost
1744 /* Among registers with equal cost, prefer caller-saved ones, or
1745 use REG_ALLOC_ORDER if it is defined. */
1746 || (this_cost == best_cost
1747 #ifdef REG_ALLOC_ORDER
1748 && (inv_reg_alloc_order[regno]
1749 < inv_reg_alloc_order[best_reg])
1750 #else
1751 && call_used_regs[regno]
1752 && ! call_used_regs[best_reg]
1753 #endif
1756 best_reg = regno;
1757 best_cost = this_cost;
1761 if (best_reg == -1)
1762 return 0;
1764 if (rtl_dump_file)
1765 fprintf (rtl_dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
1767 rl->nregs = HARD_REGNO_NREGS (best_reg, rl->mode);
1768 rl->regno = best_reg;
1770 EXECUTE_IF_SET_IN_REG_SET
1771 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j,
1773 count_spilled_pseudo (best_reg, rl->nregs, j);
1776 EXECUTE_IF_SET_IN_REG_SET
1777 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j,
1779 count_spilled_pseudo (best_reg, rl->nregs, j);
1782 for (i = 0; i < rl->nregs; i++)
1784 if (spill_cost[best_reg + i] != 0
1785 || spill_add_cost[best_reg + i] != 0)
1786 abort ();
1787 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1789 return 1;
1792 /* Find more reload regs to satisfy the remaining need of an insn, which
1793 is given by CHAIN.
1794 Do it by ascending class number, since otherwise a reg
1795 might be spilled for a big class and might fail to count
1796 for a smaller class even though it belongs to that class. */
1798 static void
1799 find_reload_regs (chain)
1800 struct insn_chain *chain;
1802 int i;
1804 /* In order to be certain of getting the registers we need,
1805 we must sort the reloads into order of increasing register class.
1806 Then our grabbing of reload registers will parallel the process
1807 that provided the reload registers. */
1808 for (i = 0; i < chain->n_reloads; i++)
1810 /* Show whether this reload already has a hard reg. */
1811 if (chain->rld[i].reg_rtx)
1813 int regno = REGNO (chain->rld[i].reg_rtx);
1814 chain->rld[i].regno = regno;
1815 chain->rld[i].nregs
1816 = HARD_REGNO_NREGS (regno, GET_MODE (chain->rld[i].reg_rtx));
1818 else
1819 chain->rld[i].regno = -1;
1820 reload_order[i] = i;
1823 n_reloads = chain->n_reloads;
1824 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
1826 CLEAR_HARD_REG_SET (used_spill_regs_local);
1828 if (rtl_dump_file)
1829 fprintf (rtl_dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1831 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
1833 /* Compute the order of preference for hard registers to spill. */
1835 order_regs_for_reload (chain);
1837 for (i = 0; i < n_reloads; i++)
1839 int r = reload_order[i];
1841 /* Ignore reloads that got marked inoperative. */
1842 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
1843 && ! rld[r].optional
1844 && rld[r].regno == -1)
1845 if (! find_reg (chain, i))
1847 spill_failure (chain->insn, rld[r].class);
1848 failure = 1;
1849 return;
1853 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
1854 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
1856 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1859 static void
1860 select_reload_regs ()
1862 struct insn_chain *chain;
1864 /* Try to satisfy the needs for each insn. */
1865 for (chain = insns_need_reload; chain != 0;
1866 chain = chain->next_need_reload)
1867 find_reload_regs (chain);
1870 /* Delete all insns that were inserted by emit_caller_save_insns during
1871 this iteration. */
1872 static void
1873 delete_caller_save_insns ()
1875 struct insn_chain *c = reload_insn_chain;
1877 while (c != 0)
1879 while (c != 0 && c->is_caller_save_insn)
1881 struct insn_chain *next = c->next;
1882 rtx insn = c->insn;
1884 if (c == reload_insn_chain)
1885 reload_insn_chain = next;
1886 delete_insn (insn);
1888 if (next)
1889 next->prev = c->prev;
1890 if (c->prev)
1891 c->prev->next = next;
1892 c->next = unused_insn_chains;
1893 unused_insn_chains = c;
1894 c = next;
1896 if (c != 0)
1897 c = c->next;
1901 /* Handle the failure to find a register to spill.
1902 INSN should be one of the insns which needed this particular spill reg. */
1904 static void
1905 spill_failure (insn, class)
1906 rtx insn;
1907 enum reg_class class;
1909 static const char *const reg_class_names[] = REG_CLASS_NAMES;
1910 if (asm_noperands (PATTERN (insn)) >= 0)
1911 error_for_asm (insn, "can't find a register in class `%s' while reloading `asm'",
1912 reg_class_names[class]);
1913 else
1915 error ("unable to find a register to spill in class `%s'",
1916 reg_class_names[class]);
1917 fatal_insn ("this is the insn:", insn);
1921 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
1922 data that is dead in INSN. */
1924 static void
1925 delete_dead_insn (insn)
1926 rtx insn;
1928 rtx prev = prev_real_insn (insn);
1929 rtx prev_dest;
1931 /* If the previous insn sets a register that dies in our insn, delete it
1932 too. */
1933 if (prev && GET_CODE (PATTERN (prev)) == SET
1934 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
1935 && reg_mentioned_p (prev_dest, PATTERN (insn))
1936 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
1937 && ! side_effects_p (SET_SRC (PATTERN (prev))))
1938 delete_dead_insn (prev);
1940 PUT_CODE (insn, NOTE);
1941 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1942 NOTE_SOURCE_FILE (insn) = 0;
1945 /* Modify the home of pseudo-reg I.
1946 The new home is present in reg_renumber[I].
1948 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
1949 or it may be -1, meaning there is none or it is not relevant.
1950 This is used so that all pseudos spilled from a given hard reg
1951 can share one stack slot. */
1953 static void
1954 alter_reg (i, from_reg)
1955 int i;
1956 int from_reg;
1958 /* When outputting an inline function, this can happen
1959 for a reg that isn't actually used. */
1960 if (regno_reg_rtx[i] == 0)
1961 return;
1963 /* If the reg got changed to a MEM at rtl-generation time,
1964 ignore it. */
1965 if (GET_CODE (regno_reg_rtx[i]) != REG)
1966 return;
1968 /* Modify the reg-rtx to contain the new hard reg
1969 number or else to contain its pseudo reg number. */
1970 REGNO (regno_reg_rtx[i])
1971 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
1973 /* If we have a pseudo that is needed but has no hard reg or equivalent,
1974 allocate a stack slot for it. */
1976 if (reg_renumber[i] < 0
1977 && REG_N_REFS (i) > 0
1978 && reg_equiv_constant[i] == 0
1979 && reg_equiv_memory_loc[i] == 0)
1981 rtx x;
1982 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
1983 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
1984 int adjust = 0;
1986 /* Each pseudo reg has an inherent size which comes from its own mode,
1987 and a total size which provides room for paradoxical subregs
1988 which refer to the pseudo reg in wider modes.
1990 We can use a slot already allocated if it provides both
1991 enough inherent space and enough total space.
1992 Otherwise, we allocate a new slot, making sure that it has no less
1993 inherent space, and no less total space, then the previous slot. */
1994 if (from_reg == -1)
1996 /* No known place to spill from => no slot to reuse. */
1997 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
1998 inherent_size == total_size ? 0 : -1);
1999 if (BYTES_BIG_ENDIAN)
2000 /* Cancel the big-endian correction done in assign_stack_local.
2001 Get the address of the beginning of the slot.
2002 This is so we can do a big-endian correction unconditionally
2003 below. */
2004 adjust = inherent_size - total_size;
2006 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2008 /* Nothing can alias this slot except this pseudo. */
2009 set_mem_alias_set (x, new_alias_set ());
2012 /* Reuse a stack slot if possible. */
2013 else if (spill_stack_slot[from_reg] != 0
2014 && spill_stack_slot_width[from_reg] >= total_size
2015 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2016 >= inherent_size))
2017 x = spill_stack_slot[from_reg];
2019 /* Allocate a bigger slot. */
2020 else
2022 /* Compute maximum size needed, both for inherent size
2023 and for total size. */
2024 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2025 rtx stack_slot;
2027 if (spill_stack_slot[from_reg])
2029 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2030 > inherent_size)
2031 mode = GET_MODE (spill_stack_slot[from_reg]);
2032 if (spill_stack_slot_width[from_reg] > total_size)
2033 total_size = spill_stack_slot_width[from_reg];
2036 /* Make a slot with that size. */
2037 x = assign_stack_local (mode, total_size,
2038 inherent_size == total_size ? 0 : -1);
2039 stack_slot = x;
2041 /* All pseudos mapped to this slot can alias each other. */
2042 if (spill_stack_slot[from_reg])
2043 set_mem_alias_set (x, MEM_ALIAS_SET (spill_stack_slot[from_reg]));
2044 else
2045 set_mem_alias_set (x, new_alias_set ());
2047 if (BYTES_BIG_ENDIAN)
2049 /* Cancel the big-endian correction done in assign_stack_local.
2050 Get the address of the beginning of the slot.
2051 This is so we can do a big-endian correction unconditionally
2052 below. */
2053 adjust = GET_MODE_SIZE (mode) - total_size;
2054 if (adjust)
2055 stack_slot
2056 = adjust_address_nv (x, mode_for_size (total_size
2057 * BITS_PER_UNIT,
2058 MODE_INT, 1),
2059 adjust);
2062 spill_stack_slot[from_reg] = stack_slot;
2063 spill_stack_slot_width[from_reg] = total_size;
2066 /* On a big endian machine, the "address" of the slot
2067 is the address of the low part that fits its inherent mode. */
2068 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2069 adjust += (total_size - inherent_size);
2071 /* If we have any adjustment to make, or if the stack slot is the
2072 wrong mode, make a new stack slot. */
2073 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2075 /* If we have a decl for the original register, set it for the
2076 memory. If this is a shared MEM, make a copy. */
2077 if (REG_EXPR (regno_reg_rtx[i])
2078 && TREE_CODE_CLASS (TREE_CODE (REG_EXPR (regno_reg_rtx[i]))) == 'd')
2080 rtx decl = DECL_RTL_IF_SET (REG_EXPR (regno_reg_rtx[i]));
2082 /* We can do this only for the DECLs home pseudo, not for
2083 any copies of it, since otherwise when the stack slot
2084 is reused, nonoverlapping_memrefs_p might think they
2085 cannot overlap. */
2086 if (decl && GET_CODE (decl) == REG && REGNO (decl) == (unsigned) i)
2088 if (from_reg != -1 && spill_stack_slot[from_reg] == x)
2089 x = copy_rtx (x);
2091 set_mem_attrs_from_reg (x, regno_reg_rtx[i]);
2095 /* Save the stack slot for later. */
2096 reg_equiv_memory_loc[i] = x;
2100 /* Mark the slots in regs_ever_live for the hard regs
2101 used by pseudo-reg number REGNO. */
2103 void
2104 mark_home_live (regno)
2105 int regno;
2107 int i, lim;
2109 i = reg_renumber[regno];
2110 if (i < 0)
2111 return;
2112 lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2113 while (i < lim)
2114 regs_ever_live[i++] = 1;
2117 /* This function handles the tracking of elimination offsets around branches.
2119 X is a piece of RTL being scanned.
2121 INSN is the insn that it came from, if any.
2123 INITIAL_P is nonzero if we are to set the offset to be the initial
2124 offset and zero if we are setting the offset of the label to be the
2125 current offset. */
2127 static void
2128 set_label_offsets (x, insn, initial_p)
2129 rtx x;
2130 rtx insn;
2131 int initial_p;
2133 enum rtx_code code = GET_CODE (x);
2134 rtx tem;
2135 unsigned int i;
2136 struct elim_table *p;
2138 switch (code)
2140 case LABEL_REF:
2141 if (LABEL_REF_NONLOCAL_P (x))
2142 return;
2144 x = XEXP (x, 0);
2146 /* ... fall through ... */
2148 case CODE_LABEL:
2149 /* If we know nothing about this label, set the desired offsets. Note
2150 that this sets the offset at a label to be the offset before a label
2151 if we don't know anything about the label. This is not correct for
2152 the label after a BARRIER, but is the best guess we can make. If
2153 we guessed wrong, we will suppress an elimination that might have
2154 been possible had we been able to guess correctly. */
2156 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2158 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2159 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2160 = (initial_p ? reg_eliminate[i].initial_offset
2161 : reg_eliminate[i].offset);
2162 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2165 /* Otherwise, if this is the definition of a label and it is
2166 preceded by a BARRIER, set our offsets to the known offset of
2167 that label. */
2169 else if (x == insn
2170 && (tem = prev_nonnote_insn (insn)) != 0
2171 && GET_CODE (tem) == BARRIER)
2172 set_offsets_for_label (insn);
2173 else
2174 /* If neither of the above cases is true, compare each offset
2175 with those previously recorded and suppress any eliminations
2176 where the offsets disagree. */
2178 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2179 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2180 != (initial_p ? reg_eliminate[i].initial_offset
2181 : reg_eliminate[i].offset))
2182 reg_eliminate[i].can_eliminate = 0;
2184 return;
2186 case JUMP_INSN:
2187 set_label_offsets (PATTERN (insn), insn, initial_p);
2189 /* ... fall through ... */
2191 case INSN:
2192 case CALL_INSN:
2193 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2194 and hence must have all eliminations at their initial offsets. */
2195 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2196 if (REG_NOTE_KIND (tem) == REG_LABEL)
2197 set_label_offsets (XEXP (tem, 0), insn, 1);
2198 return;
2200 case PARALLEL:
2201 case ADDR_VEC:
2202 case ADDR_DIFF_VEC:
2203 /* Each of the labels in the parallel or address vector must be
2204 at their initial offsets. We want the first field for PARALLEL
2205 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2207 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2208 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2209 insn, initial_p);
2210 return;
2212 case SET:
2213 /* We only care about setting PC. If the source is not RETURN,
2214 IF_THEN_ELSE, or a label, disable any eliminations not at
2215 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2216 isn't one of those possibilities. For branches to a label,
2217 call ourselves recursively.
2219 Note that this can disable elimination unnecessarily when we have
2220 a non-local goto since it will look like a non-constant jump to
2221 someplace in the current function. This isn't a significant
2222 problem since such jumps will normally be when all elimination
2223 pairs are back to their initial offsets. */
2225 if (SET_DEST (x) != pc_rtx)
2226 return;
2228 switch (GET_CODE (SET_SRC (x)))
2230 case PC:
2231 case RETURN:
2232 return;
2234 case LABEL_REF:
2235 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2236 return;
2238 case IF_THEN_ELSE:
2239 tem = XEXP (SET_SRC (x), 1);
2240 if (GET_CODE (tem) == LABEL_REF)
2241 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2242 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2243 break;
2245 tem = XEXP (SET_SRC (x), 2);
2246 if (GET_CODE (tem) == LABEL_REF)
2247 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2248 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2249 break;
2250 return;
2252 default:
2253 break;
2256 /* If we reach here, all eliminations must be at their initial
2257 offset because we are doing a jump to a variable address. */
2258 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2259 if (p->offset != p->initial_offset)
2260 p->can_eliminate = 0;
2261 break;
2263 default:
2264 break;
2268 /* Scan X and replace any eliminable registers (such as fp) with a
2269 replacement (such as sp), plus an offset.
2271 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2272 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2273 MEM, we are allowed to replace a sum of a register and the constant zero
2274 with the register, which we cannot do outside a MEM. In addition, we need
2275 to record the fact that a register is referenced outside a MEM.
2277 If INSN is an insn, it is the insn containing X. If we replace a REG
2278 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2279 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2280 the REG is being modified.
2282 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2283 That's used when we eliminate in expressions stored in notes.
2284 This means, do not set ref_outside_mem even if the reference
2285 is outside of MEMs.
2287 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2288 replacements done assuming all offsets are at their initial values. If
2289 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2290 encounter, return the actual location so that find_reloads will do
2291 the proper thing. */
2294 eliminate_regs (x, mem_mode, insn)
2295 rtx x;
2296 enum machine_mode mem_mode;
2297 rtx insn;
2299 enum rtx_code code = GET_CODE (x);
2300 struct elim_table *ep;
2301 int regno;
2302 rtx new;
2303 int i, j;
2304 const char *fmt;
2305 int copied = 0;
2307 if (! current_function_decl)
2308 return x;
2310 switch (code)
2312 case CONST_INT:
2313 case CONST_DOUBLE:
2314 case CONST_VECTOR:
2315 case CONST:
2316 case SYMBOL_REF:
2317 case CODE_LABEL:
2318 case PC:
2319 case CC0:
2320 case ASM_INPUT:
2321 case ADDR_VEC:
2322 case ADDR_DIFF_VEC:
2323 case RETURN:
2324 return x;
2326 case ADDRESSOF:
2327 /* This is only for the benefit of the debugging backends, which call
2328 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2329 removed after CSE. */
2330 new = eliminate_regs (XEXP (x, 0), 0, insn);
2331 if (GET_CODE (new) == MEM)
2332 return XEXP (new, 0);
2333 return x;
2335 case REG:
2336 regno = REGNO (x);
2338 /* First handle the case where we encounter a bare register that
2339 is eliminable. Replace it with a PLUS. */
2340 if (regno < FIRST_PSEUDO_REGISTER)
2342 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2343 ep++)
2344 if (ep->from_rtx == x && ep->can_eliminate)
2345 return plus_constant (ep->to_rtx, ep->previous_offset);
2348 else if (reg_renumber && reg_renumber[regno] < 0
2349 && reg_equiv_constant && reg_equiv_constant[regno]
2350 && ! CONSTANT_P (reg_equiv_constant[regno]))
2351 return eliminate_regs (copy_rtx (reg_equiv_constant[regno]),
2352 mem_mode, insn);
2353 return x;
2355 /* You might think handling MINUS in a manner similar to PLUS is a
2356 good idea. It is not. It has been tried multiple times and every
2357 time the change has had to have been reverted.
2359 Other parts of reload know a PLUS is special (gen_reload for example)
2360 and require special code to handle code a reloaded PLUS operand.
2362 Also consider backends where the flags register is clobbered by a
2363 MINUS, but we can emit a PLUS that does not clobber flags (ia32,
2364 lea instruction comes to mind). If we try to reload a MINUS, we
2365 may kill the flags register that was holding a useful value.
2367 So, please before trying to handle MINUS, consider reload as a
2368 whole instead of this little section as well as the backend issues. */
2369 case PLUS:
2370 /* If this is the sum of an eliminable register and a constant, rework
2371 the sum. */
2372 if (GET_CODE (XEXP (x, 0)) == REG
2373 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2374 && CONSTANT_P (XEXP (x, 1)))
2376 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2377 ep++)
2378 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2380 /* The only time we want to replace a PLUS with a REG (this
2381 occurs when the constant operand of the PLUS is the negative
2382 of the offset) is when we are inside a MEM. We won't want
2383 to do so at other times because that would change the
2384 structure of the insn in a way that reload can't handle.
2385 We special-case the commonest situation in
2386 eliminate_regs_in_insn, so just replace a PLUS with a
2387 PLUS here, unless inside a MEM. */
2388 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2389 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2390 return ep->to_rtx;
2391 else
2392 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2393 plus_constant (XEXP (x, 1),
2394 ep->previous_offset));
2397 /* If the register is not eliminable, we are done since the other
2398 operand is a constant. */
2399 return x;
2402 /* If this is part of an address, we want to bring any constant to the
2403 outermost PLUS. We will do this by doing register replacement in
2404 our operands and seeing if a constant shows up in one of them.
2406 Note that there is no risk of modifying the structure of the insn,
2407 since we only get called for its operands, thus we are either
2408 modifying the address inside a MEM, or something like an address
2409 operand of a load-address insn. */
2412 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2413 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2415 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2417 /* If one side is a PLUS and the other side is a pseudo that
2418 didn't get a hard register but has a reg_equiv_constant,
2419 we must replace the constant here since it may no longer
2420 be in the position of any operand. */
2421 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2422 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2423 && reg_renumber[REGNO (new1)] < 0
2424 && reg_equiv_constant != 0
2425 && reg_equiv_constant[REGNO (new1)] != 0)
2426 new1 = reg_equiv_constant[REGNO (new1)];
2427 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2428 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2429 && reg_renumber[REGNO (new0)] < 0
2430 && reg_equiv_constant[REGNO (new0)] != 0)
2431 new0 = reg_equiv_constant[REGNO (new0)];
2433 new = form_sum (new0, new1);
2435 /* As above, if we are not inside a MEM we do not want to
2436 turn a PLUS into something else. We might try to do so here
2437 for an addition of 0 if we aren't optimizing. */
2438 if (! mem_mode && GET_CODE (new) != PLUS)
2439 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2440 else
2441 return new;
2444 return x;
2446 case MULT:
2447 /* If this is the product of an eliminable register and a
2448 constant, apply the distribute law and move the constant out
2449 so that we have (plus (mult ..) ..). This is needed in order
2450 to keep load-address insns valid. This case is pathological.
2451 We ignore the possibility of overflow here. */
2452 if (GET_CODE (XEXP (x, 0)) == REG
2453 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2454 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2455 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2456 ep++)
2457 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2459 if (! mem_mode
2460 /* Refs inside notes don't count for this purpose. */
2461 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2462 || GET_CODE (insn) == INSN_LIST)))
2463 ep->ref_outside_mem = 1;
2465 return
2466 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2467 ep->previous_offset * INTVAL (XEXP (x, 1)));
2470 /* ... fall through ... */
2472 case CALL:
2473 case COMPARE:
2474 /* See comments before PLUS about handling MINUS. */
2475 case MINUS:
2476 case DIV: case UDIV:
2477 case MOD: case UMOD:
2478 case AND: case IOR: case XOR:
2479 case ROTATERT: case ROTATE:
2480 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2481 case NE: case EQ:
2482 case GE: case GT: case GEU: case GTU:
2483 case LE: case LT: case LEU: case LTU:
2485 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2486 rtx new1
2487 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
2489 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2490 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2492 return x;
2494 case EXPR_LIST:
2495 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2496 if (XEXP (x, 0))
2498 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2499 if (new != XEXP (x, 0))
2501 /* If this is a REG_DEAD note, it is not valid anymore.
2502 Using the eliminated version could result in creating a
2503 REG_DEAD note for the stack or frame pointer. */
2504 if (GET_MODE (x) == REG_DEAD)
2505 return (XEXP (x, 1)
2506 ? eliminate_regs (XEXP (x, 1), mem_mode, insn)
2507 : NULL_RTX);
2509 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2513 /* ... fall through ... */
2515 case INSN_LIST:
2516 /* Now do eliminations in the rest of the chain. If this was
2517 an EXPR_LIST, this might result in allocating more memory than is
2518 strictly needed, but it simplifies the code. */
2519 if (XEXP (x, 1))
2521 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2522 if (new != XEXP (x, 1))
2523 return
2524 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2526 return x;
2528 case PRE_INC:
2529 case POST_INC:
2530 case PRE_DEC:
2531 case POST_DEC:
2532 case STRICT_LOW_PART:
2533 case NEG: case NOT:
2534 case SIGN_EXTEND: case ZERO_EXTEND:
2535 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2536 case FLOAT: case FIX:
2537 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2538 case ABS:
2539 case SQRT:
2540 case FFS:
2541 case CLZ:
2542 case CTZ:
2543 case POPCOUNT:
2544 case PARITY:
2545 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2546 if (new != XEXP (x, 0))
2547 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2548 return x;
2550 case SUBREG:
2551 /* Similar to above processing, but preserve SUBREG_BYTE.
2552 Convert (subreg (mem)) to (mem) if not paradoxical.
2553 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2554 pseudo didn't get a hard reg, we must replace this with the
2555 eliminated version of the memory location because push_reloads
2556 may do the replacement in certain circumstances. */
2557 if (GET_CODE (SUBREG_REG (x)) == REG
2558 && (GET_MODE_SIZE (GET_MODE (x))
2559 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2560 && reg_equiv_memory_loc != 0
2561 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2563 new = SUBREG_REG (x);
2565 else
2566 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
2568 if (new != SUBREG_REG (x))
2570 int x_size = GET_MODE_SIZE (GET_MODE (x));
2571 int new_size = GET_MODE_SIZE (GET_MODE (new));
2573 if (GET_CODE (new) == MEM
2574 && ((x_size < new_size
2575 #ifdef WORD_REGISTER_OPERATIONS
2576 /* On these machines, combine can create rtl of the form
2577 (set (subreg:m1 (reg:m2 R) 0) ...)
2578 where m1 < m2, and expects something interesting to
2579 happen to the entire word. Moreover, it will use the
2580 (reg:m2 R) later, expecting all bits to be preserved.
2581 So if the number of words is the same, preserve the
2582 subreg so that push_reloads can see it. */
2583 && ! ((x_size - 1) / UNITS_PER_WORD
2584 == (new_size -1 ) / UNITS_PER_WORD)
2585 #endif
2587 || x_size == new_size)
2589 return adjust_address_nv (new, GET_MODE (x), SUBREG_BYTE (x));
2590 else
2591 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_BYTE (x));
2594 return x;
2596 case MEM:
2597 /* This is only for the benefit of the debugging backends, which call
2598 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2599 removed after CSE. */
2600 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
2601 return eliminate_regs (XEXP (XEXP (x, 0), 0), 0, insn);
2603 /* Our only special processing is to pass the mode of the MEM to our
2604 recursive call and copy the flags. While we are here, handle this
2605 case more efficiently. */
2606 return
2607 replace_equiv_address_nv (x,
2608 eliminate_regs (XEXP (x, 0),
2609 GET_MODE (x), insn));
2611 case USE:
2612 /* Handle insn_list USE that a call to a pure function may generate. */
2613 new = eliminate_regs (XEXP (x, 0), 0, insn);
2614 if (new != XEXP (x, 0))
2615 return gen_rtx_USE (GET_MODE (x), new);
2616 return x;
2618 case CLOBBER:
2619 case ASM_OPERANDS:
2620 case SET:
2621 abort ();
2623 default:
2624 break;
2627 /* Process each of our operands recursively. If any have changed, make a
2628 copy of the rtx. */
2629 fmt = GET_RTX_FORMAT (code);
2630 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2632 if (*fmt == 'e')
2634 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
2635 if (new != XEXP (x, i) && ! copied)
2637 rtx new_x = rtx_alloc (code);
2638 memcpy (new_x, x,
2639 (sizeof (*new_x) - sizeof (new_x->fld)
2640 + sizeof (new_x->fld[0]) * GET_RTX_LENGTH (code)));
2641 x = new_x;
2642 copied = 1;
2644 XEXP (x, i) = new;
2646 else if (*fmt == 'E')
2648 int copied_vec = 0;
2649 for (j = 0; j < XVECLEN (x, i); j++)
2651 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
2652 if (new != XVECEXP (x, i, j) && ! copied_vec)
2654 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2655 XVEC (x, i)->elem);
2656 if (! copied)
2658 rtx new_x = rtx_alloc (code);
2659 memcpy (new_x, x,
2660 (sizeof (*new_x) - sizeof (new_x->fld)
2661 + (sizeof (new_x->fld[0])
2662 * GET_RTX_LENGTH (code))));
2663 x = new_x;
2664 copied = 1;
2666 XVEC (x, i) = new_v;
2667 copied_vec = 1;
2669 XVECEXP (x, i, j) = new;
2674 return x;
2677 /* Scan rtx X for modifications of elimination target registers. Update
2678 the table of eliminables to reflect the changed state. MEM_MODE is
2679 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2681 static void
2682 elimination_effects (x, mem_mode)
2683 rtx x;
2684 enum machine_mode mem_mode;
2687 enum rtx_code code = GET_CODE (x);
2688 struct elim_table *ep;
2689 int regno;
2690 int i, j;
2691 const char *fmt;
2693 switch (code)
2695 case CONST_INT:
2696 case CONST_DOUBLE:
2697 case CONST_VECTOR:
2698 case CONST:
2699 case SYMBOL_REF:
2700 case CODE_LABEL:
2701 case PC:
2702 case CC0:
2703 case ASM_INPUT:
2704 case ADDR_VEC:
2705 case ADDR_DIFF_VEC:
2706 case RETURN:
2707 return;
2709 case ADDRESSOF:
2710 abort ();
2712 case REG:
2713 regno = REGNO (x);
2715 /* First handle the case where we encounter a bare register that
2716 is eliminable. Replace it with a PLUS. */
2717 if (regno < FIRST_PSEUDO_REGISTER)
2719 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2720 ep++)
2721 if (ep->from_rtx == x && ep->can_eliminate)
2723 if (! mem_mode)
2724 ep->ref_outside_mem = 1;
2725 return;
2729 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2730 && reg_equiv_constant[regno]
2731 && ! function_invariant_p (reg_equiv_constant[regno]))
2732 elimination_effects (reg_equiv_constant[regno], mem_mode);
2733 return;
2735 case PRE_INC:
2736 case POST_INC:
2737 case PRE_DEC:
2738 case POST_DEC:
2739 case POST_MODIFY:
2740 case PRE_MODIFY:
2741 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2742 if (ep->to_rtx == XEXP (x, 0))
2744 int size = GET_MODE_SIZE (mem_mode);
2746 /* If more bytes than MEM_MODE are pushed, account for them. */
2747 #ifdef PUSH_ROUNDING
2748 if (ep->to_rtx == stack_pointer_rtx)
2749 size = PUSH_ROUNDING (size);
2750 #endif
2751 if (code == PRE_DEC || code == POST_DEC)
2752 ep->offset += size;
2753 else if (code == PRE_INC || code == POST_INC)
2754 ep->offset -= size;
2755 else if ((code == PRE_MODIFY || code == POST_MODIFY)
2756 && GET_CODE (XEXP (x, 1)) == PLUS
2757 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
2758 && CONSTANT_P (XEXP (XEXP (x, 1), 1)))
2759 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
2762 /* These two aren't unary operators. */
2763 if (code == POST_MODIFY || code == PRE_MODIFY)
2764 break;
2766 /* Fall through to generic unary operation case. */
2767 case STRICT_LOW_PART:
2768 case NEG: case NOT:
2769 case SIGN_EXTEND: case ZERO_EXTEND:
2770 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2771 case FLOAT: case FIX:
2772 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2773 case ABS:
2774 case SQRT:
2775 case FFS:
2776 case CLZ:
2777 case CTZ:
2778 case POPCOUNT:
2779 case PARITY:
2780 elimination_effects (XEXP (x, 0), mem_mode);
2781 return;
2783 case SUBREG:
2784 if (GET_CODE (SUBREG_REG (x)) == REG
2785 && (GET_MODE_SIZE (GET_MODE (x))
2786 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2787 && reg_equiv_memory_loc != 0
2788 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2789 return;
2791 elimination_effects (SUBREG_REG (x), mem_mode);
2792 return;
2794 case USE:
2795 /* If using a register that is the source of an eliminate we still
2796 think can be performed, note it cannot be performed since we don't
2797 know how this register is used. */
2798 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2799 if (ep->from_rtx == XEXP (x, 0))
2800 ep->can_eliminate = 0;
2802 elimination_effects (XEXP (x, 0), mem_mode);
2803 return;
2805 case CLOBBER:
2806 /* If clobbering a register that is the replacement register for an
2807 elimination we still think can be performed, note that it cannot
2808 be performed. Otherwise, we need not be concerned about it. */
2809 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2810 if (ep->to_rtx == XEXP (x, 0))
2811 ep->can_eliminate = 0;
2813 elimination_effects (XEXP (x, 0), mem_mode);
2814 return;
2816 case SET:
2817 /* Check for setting a register that we know about. */
2818 if (GET_CODE (SET_DEST (x)) == REG)
2820 /* See if this is setting the replacement register for an
2821 elimination.
2823 If DEST is the hard frame pointer, we do nothing because we
2824 assume that all assignments to the frame pointer are for
2825 non-local gotos and are being done at a time when they are valid
2826 and do not disturb anything else. Some machines want to
2827 eliminate a fake argument pointer (or even a fake frame pointer)
2828 with either the real frame or the stack pointer. Assignments to
2829 the hard frame pointer must not prevent this elimination. */
2831 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2832 ep++)
2833 if (ep->to_rtx == SET_DEST (x)
2834 && SET_DEST (x) != hard_frame_pointer_rtx)
2836 /* If it is being incremented, adjust the offset. Otherwise,
2837 this elimination can't be done. */
2838 rtx src = SET_SRC (x);
2840 if (GET_CODE (src) == PLUS
2841 && XEXP (src, 0) == SET_DEST (x)
2842 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2843 ep->offset -= INTVAL (XEXP (src, 1));
2844 else
2845 ep->can_eliminate = 0;
2849 elimination_effects (SET_DEST (x), 0);
2850 elimination_effects (SET_SRC (x), 0);
2851 return;
2853 case MEM:
2854 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
2855 abort ();
2857 /* Our only special processing is to pass the mode of the MEM to our
2858 recursive call. */
2859 elimination_effects (XEXP (x, 0), GET_MODE (x));
2860 return;
2862 default:
2863 break;
2866 fmt = GET_RTX_FORMAT (code);
2867 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2869 if (*fmt == 'e')
2870 elimination_effects (XEXP (x, i), mem_mode);
2871 else if (*fmt == 'E')
2872 for (j = 0; j < XVECLEN (x, i); j++)
2873 elimination_effects (XVECEXP (x, i, j), mem_mode);
2877 /* Descend through rtx X and verify that no references to eliminable registers
2878 remain. If any do remain, mark the involved register as not
2879 eliminable. */
2881 static void
2882 check_eliminable_occurrences (x)
2883 rtx x;
2885 const char *fmt;
2886 int i;
2887 enum rtx_code code;
2889 if (x == 0)
2890 return;
2892 code = GET_CODE (x);
2894 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
2896 struct elim_table *ep;
2898 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2899 if (ep->from_rtx == x && ep->can_eliminate)
2900 ep->can_eliminate = 0;
2901 return;
2904 fmt = GET_RTX_FORMAT (code);
2905 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2907 if (*fmt == 'e')
2908 check_eliminable_occurrences (XEXP (x, i));
2909 else if (*fmt == 'E')
2911 int j;
2912 for (j = 0; j < XVECLEN (x, i); j++)
2913 check_eliminable_occurrences (XVECEXP (x, i, j));
2918 /* Scan INSN and eliminate all eliminable registers in it.
2920 If REPLACE is nonzero, do the replacement destructively. Also
2921 delete the insn as dead it if it is setting an eliminable register.
2923 If REPLACE is zero, do all our allocations in reload_obstack.
2925 If no eliminations were done and this insn doesn't require any elimination
2926 processing (these are not identical conditions: it might be updating sp,
2927 but not referencing fp; this needs to be seen during reload_as_needed so
2928 that the offset between fp and sp can be taken into consideration), zero
2929 is returned. Otherwise, 1 is returned. */
2931 static int
2932 eliminate_regs_in_insn (insn, replace)
2933 rtx insn;
2934 int replace;
2936 int icode = recog_memoized (insn);
2937 rtx old_body = PATTERN (insn);
2938 int insn_is_asm = asm_noperands (old_body) >= 0;
2939 rtx old_set = single_set (insn);
2940 rtx new_body;
2941 int val = 0;
2942 int i;
2943 rtx substed_operand[MAX_RECOG_OPERANDS];
2944 rtx orig_operand[MAX_RECOG_OPERANDS];
2945 struct elim_table *ep;
2947 if (! insn_is_asm && icode < 0)
2949 if (GET_CODE (PATTERN (insn)) == USE
2950 || GET_CODE (PATTERN (insn)) == CLOBBER
2951 || GET_CODE (PATTERN (insn)) == ADDR_VEC
2952 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2953 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
2954 return 0;
2955 abort ();
2958 if (old_set != 0 && GET_CODE (SET_DEST (old_set)) == REG
2959 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
2961 /* Check for setting an eliminable register. */
2962 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2963 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
2965 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2966 /* If this is setting the frame pointer register to the
2967 hardware frame pointer register and this is an elimination
2968 that will be done (tested above), this insn is really
2969 adjusting the frame pointer downward to compensate for
2970 the adjustment done before a nonlocal goto. */
2971 if (ep->from == FRAME_POINTER_REGNUM
2972 && ep->to == HARD_FRAME_POINTER_REGNUM)
2974 rtx base = SET_SRC (old_set);
2975 rtx base_insn = insn;
2976 int offset = 0;
2978 while (base != ep->to_rtx)
2980 rtx prev_insn, prev_set;
2982 if (GET_CODE (base) == PLUS
2983 && GET_CODE (XEXP (base, 1)) == CONST_INT)
2985 offset += INTVAL (XEXP (base, 1));
2986 base = XEXP (base, 0);
2988 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
2989 && (prev_set = single_set (prev_insn)) != 0
2990 && rtx_equal_p (SET_DEST (prev_set), base))
2992 base = SET_SRC (prev_set);
2993 base_insn = prev_insn;
2995 else
2996 break;
2999 if (base == ep->to_rtx)
3001 rtx src
3002 = plus_constant (ep->to_rtx, offset - ep->offset);
3004 new_body = old_body;
3005 if (! replace)
3007 new_body = copy_insn (old_body);
3008 if (REG_NOTES (insn))
3009 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3011 PATTERN (insn) = new_body;
3012 old_set = single_set (insn);
3014 /* First see if this insn remains valid when we
3015 make the change. If not, keep the INSN_CODE
3016 the same and let reload fit it up. */
3017 validate_change (insn, &SET_SRC (old_set), src, 1);
3018 validate_change (insn, &SET_DEST (old_set),
3019 ep->to_rtx, 1);
3020 if (! apply_change_group ())
3022 SET_SRC (old_set) = src;
3023 SET_DEST (old_set) = ep->to_rtx;
3026 val = 1;
3027 goto done;
3030 #endif
3032 /* In this case this insn isn't serving a useful purpose. We
3033 will delete it in reload_as_needed once we know that this
3034 elimination is, in fact, being done.
3036 If REPLACE isn't set, we can't delete this insn, but needn't
3037 process it since it won't be used unless something changes. */
3038 if (replace)
3040 delete_dead_insn (insn);
3041 return 1;
3043 val = 1;
3044 goto done;
3048 /* We allow one special case which happens to work on all machines we
3049 currently support: a single set with the source being a PLUS of an
3050 eliminable register and a constant. */
3051 if (old_set
3052 && GET_CODE (SET_DEST (old_set)) == REG
3053 && GET_CODE (SET_SRC (old_set)) == PLUS
3054 && GET_CODE (XEXP (SET_SRC (old_set), 0)) == REG
3055 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT
3056 && REGNO (XEXP (SET_SRC (old_set), 0)) < FIRST_PSEUDO_REGISTER)
3058 rtx reg = XEXP (SET_SRC (old_set), 0);
3059 int offset = INTVAL (XEXP (SET_SRC (old_set), 1));
3061 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3062 if (ep->from_rtx == reg && ep->can_eliminate)
3064 offset += ep->offset;
3066 if (offset == 0)
3068 int num_clobbers;
3069 /* We assume here that if we need a PARALLEL with
3070 CLOBBERs for this assignment, we can do with the
3071 MATCH_SCRATCHes that add_clobbers allocates.
3072 There's not much we can do if that doesn't work. */
3073 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3074 SET_DEST (old_set),
3075 ep->to_rtx);
3076 num_clobbers = 0;
3077 INSN_CODE (insn) = recog (PATTERN (insn), insn, &num_clobbers);
3078 if (num_clobbers)
3080 rtvec vec = rtvec_alloc (num_clobbers + 1);
3082 vec->elem[0] = PATTERN (insn);
3083 PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
3084 add_clobbers (PATTERN (insn), INSN_CODE (insn));
3086 if (INSN_CODE (insn) < 0)
3087 abort ();
3089 else
3091 new_body = old_body;
3092 if (! replace)
3094 new_body = copy_insn (old_body);
3095 if (REG_NOTES (insn))
3096 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3098 PATTERN (insn) = new_body;
3099 old_set = single_set (insn);
3101 XEXP (SET_SRC (old_set), 0) = ep->to_rtx;
3102 XEXP (SET_SRC (old_set), 1) = GEN_INT (offset);
3104 val = 1;
3105 /* This can't have an effect on elimination offsets, so skip right
3106 to the end. */
3107 goto done;
3111 /* Determine the effects of this insn on elimination offsets. */
3112 elimination_effects (old_body, 0);
3114 /* Eliminate all eliminable registers occurring in operands that
3115 can be handled by reload. */
3116 extract_insn (insn);
3117 for (i = 0; i < recog_data.n_operands; i++)
3119 orig_operand[i] = recog_data.operand[i];
3120 substed_operand[i] = recog_data.operand[i];
3122 /* For an asm statement, every operand is eliminable. */
3123 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3125 /* Check for setting a register that we know about. */
3126 if (recog_data.operand_type[i] != OP_IN
3127 && GET_CODE (orig_operand[i]) == REG)
3129 /* If we are assigning to a register that can be eliminated, it
3130 must be as part of a PARALLEL, since the code above handles
3131 single SETs. We must indicate that we can no longer
3132 eliminate this reg. */
3133 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3134 ep++)
3135 if (ep->from_rtx == orig_operand[i] && ep->can_eliminate)
3136 ep->can_eliminate = 0;
3139 substed_operand[i] = eliminate_regs (recog_data.operand[i], 0,
3140 replace ? insn : NULL_RTX);
3141 if (substed_operand[i] != orig_operand[i])
3142 val = 1;
3143 /* Terminate the search in check_eliminable_occurrences at
3144 this point. */
3145 *recog_data.operand_loc[i] = 0;
3147 /* If an output operand changed from a REG to a MEM and INSN is an
3148 insn, write a CLOBBER insn. */
3149 if (recog_data.operand_type[i] != OP_IN
3150 && GET_CODE (orig_operand[i]) == REG
3151 && GET_CODE (substed_operand[i]) == MEM
3152 && replace)
3153 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3154 insn);
3158 for (i = 0; i < recog_data.n_dups; i++)
3159 *recog_data.dup_loc[i]
3160 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3162 /* If any eliminable remain, they aren't eliminable anymore. */
3163 check_eliminable_occurrences (old_body);
3165 /* Substitute the operands; the new values are in the substed_operand
3166 array. */
3167 for (i = 0; i < recog_data.n_operands; i++)
3168 *recog_data.operand_loc[i] = substed_operand[i];
3169 for (i = 0; i < recog_data.n_dups; i++)
3170 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3172 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3173 re-recognize the insn. We do this in case we had a simple addition
3174 but now can do this as a load-address. This saves an insn in this
3175 common case.
3176 If re-recognition fails, the old insn code number will still be used,
3177 and some register operands may have changed into PLUS expressions.
3178 These will be handled by find_reloads by loading them into a register
3179 again. */
3181 if (val)
3183 /* If we aren't replacing things permanently and we changed something,
3184 make another copy to ensure that all the RTL is new. Otherwise
3185 things can go wrong if find_reload swaps commutative operands
3186 and one is inside RTL that has been copied while the other is not. */
3187 new_body = old_body;
3188 if (! replace)
3190 new_body = copy_insn (old_body);
3191 if (REG_NOTES (insn))
3192 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3194 PATTERN (insn) = new_body;
3196 /* If we had a move insn but now we don't, rerecognize it. This will
3197 cause spurious re-recognition if the old move had a PARALLEL since
3198 the new one still will, but we can't call single_set without
3199 having put NEW_BODY into the insn and the re-recognition won't
3200 hurt in this rare case. */
3201 /* ??? Why this huge if statement - why don't we just rerecognize the
3202 thing always? */
3203 if (! insn_is_asm
3204 && old_set != 0
3205 && ((GET_CODE (SET_SRC (old_set)) == REG
3206 && (GET_CODE (new_body) != SET
3207 || GET_CODE (SET_SRC (new_body)) != REG))
3208 /* If this was a load from or store to memory, compare
3209 the MEM in recog_data.operand to the one in the insn.
3210 If they are not equal, then rerecognize the insn. */
3211 || (old_set != 0
3212 && ((GET_CODE (SET_SRC (old_set)) == MEM
3213 && SET_SRC (old_set) != recog_data.operand[1])
3214 || (GET_CODE (SET_DEST (old_set)) == MEM
3215 && SET_DEST (old_set) != recog_data.operand[0])))
3216 /* If this was an add insn before, rerecognize. */
3217 || GET_CODE (SET_SRC (old_set)) == PLUS))
3219 int new_icode = recog (PATTERN (insn), insn, 0);
3220 if (new_icode < 0)
3221 INSN_CODE (insn) = icode;
3225 /* Restore the old body. If there were any changes to it, we made a copy
3226 of it while the changes were still in place, so we'll correctly return
3227 a modified insn below. */
3228 if (! replace)
3230 /* Restore the old body. */
3231 for (i = 0; i < recog_data.n_operands; i++)
3232 *recog_data.operand_loc[i] = orig_operand[i];
3233 for (i = 0; i < recog_data.n_dups; i++)
3234 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3237 /* Update all elimination pairs to reflect the status after the current
3238 insn. The changes we make were determined by the earlier call to
3239 elimination_effects.
3241 We also detect cases where register elimination cannot be done,
3242 namely, if a register would be both changed and referenced outside a MEM
3243 in the resulting insn since such an insn is often undefined and, even if
3244 not, we cannot know what meaning will be given to it. Note that it is
3245 valid to have a register used in an address in an insn that changes it
3246 (presumably with a pre- or post-increment or decrement).
3248 If anything changes, return nonzero. */
3250 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3252 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3253 ep->can_eliminate = 0;
3255 ep->ref_outside_mem = 0;
3257 if (ep->previous_offset != ep->offset)
3258 val = 1;
3261 done:
3262 /* If we changed something, perform elimination in REG_NOTES. This is
3263 needed even when REPLACE is zero because a REG_DEAD note might refer
3264 to a register that we eliminate and could cause a different number
3265 of spill registers to be needed in the final reload pass than in
3266 the pre-passes. */
3267 if (val && REG_NOTES (insn) != 0)
3268 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
3270 return val;
3273 /* Loop through all elimination pairs.
3274 Recalculate the number not at initial offset.
3276 Compute the maximum offset (minimum offset if the stack does not
3277 grow downward) for each elimination pair. */
3279 static void
3280 update_eliminable_offsets ()
3282 struct elim_table *ep;
3284 num_not_at_initial_offset = 0;
3285 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3287 ep->previous_offset = ep->offset;
3288 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3289 num_not_at_initial_offset++;
3293 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3294 replacement we currently believe is valid, mark it as not eliminable if X
3295 modifies DEST in any way other than by adding a constant integer to it.
3297 If DEST is the frame pointer, we do nothing because we assume that
3298 all assignments to the hard frame pointer are nonlocal gotos and are being
3299 done at a time when they are valid and do not disturb anything else.
3300 Some machines want to eliminate a fake argument pointer with either the
3301 frame or stack pointer. Assignments to the hard frame pointer must not
3302 prevent this elimination.
3304 Called via note_stores from reload before starting its passes to scan
3305 the insns of the function. */
3307 static void
3308 mark_not_eliminable (dest, x, data)
3309 rtx dest;
3310 rtx x;
3311 void *data ATTRIBUTE_UNUSED;
3313 unsigned int i;
3315 /* A SUBREG of a hard register here is just changing its mode. We should
3316 not see a SUBREG of an eliminable hard register, but check just in
3317 case. */
3318 if (GET_CODE (dest) == SUBREG)
3319 dest = SUBREG_REG (dest);
3321 if (dest == hard_frame_pointer_rtx)
3322 return;
3324 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3325 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3326 && (GET_CODE (x) != SET
3327 || GET_CODE (SET_SRC (x)) != PLUS
3328 || XEXP (SET_SRC (x), 0) != dest
3329 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3331 reg_eliminate[i].can_eliminate_previous
3332 = reg_eliminate[i].can_eliminate = 0;
3333 num_eliminable--;
3337 /* Verify that the initial elimination offsets did not change since the
3338 last call to set_initial_elim_offsets. This is used to catch cases
3339 where something illegal happened during reload_as_needed that could
3340 cause incorrect code to be generated if we did not check for it. */
3342 static void
3343 verify_initial_elim_offsets ()
3345 int t;
3347 #ifdef ELIMINABLE_REGS
3348 struct elim_table *ep;
3350 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3352 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3353 if (t != ep->initial_offset)
3354 abort ();
3356 #else
3357 INITIAL_FRAME_POINTER_OFFSET (t);
3358 if (t != reg_eliminate[0].initial_offset)
3359 abort ();
3360 #endif
3363 /* Reset all offsets on eliminable registers to their initial values. */
3365 static void
3366 set_initial_elim_offsets ()
3368 struct elim_table *ep = reg_eliminate;
3370 #ifdef ELIMINABLE_REGS
3371 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3373 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3374 ep->previous_offset = ep->offset = ep->initial_offset;
3376 #else
3377 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3378 ep->previous_offset = ep->offset = ep->initial_offset;
3379 #endif
3381 num_not_at_initial_offset = 0;
3384 /* Initialize the known label offsets.
3385 Set a known offset for each forced label to be at the initial offset
3386 of each elimination. We do this because we assume that all
3387 computed jumps occur from a location where each elimination is
3388 at its initial offset.
3389 For all other labels, show that we don't know the offsets. */
3391 static void
3392 set_initial_label_offsets ()
3394 rtx x;
3395 memset (offsets_known_at, 0, num_labels);
3397 for (x = forced_labels; x; x = XEXP (x, 1))
3398 if (XEXP (x, 0))
3399 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3402 /* Set all elimination offsets to the known values for the code label given
3403 by INSN. */
3405 static void
3406 set_offsets_for_label (insn)
3407 rtx insn;
3409 unsigned int i;
3410 int label_nr = CODE_LABEL_NUMBER (insn);
3411 struct elim_table *ep;
3413 num_not_at_initial_offset = 0;
3414 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3416 ep->offset = ep->previous_offset
3417 = offsets_at[label_nr - first_label_num][i];
3418 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3419 num_not_at_initial_offset++;
3423 /* See if anything that happened changes which eliminations are valid.
3424 For example, on the SPARC, whether or not the frame pointer can
3425 be eliminated can depend on what registers have been used. We need
3426 not check some conditions again (such as flag_omit_frame_pointer)
3427 since they can't have changed. */
3429 static void
3430 update_eliminables (pset)
3431 HARD_REG_SET *pset;
3433 int previous_frame_pointer_needed = frame_pointer_needed;
3434 struct elim_table *ep;
3436 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3437 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3438 #ifdef ELIMINABLE_REGS
3439 || ! CAN_ELIMINATE (ep->from, ep->to)
3440 #endif
3442 ep->can_eliminate = 0;
3444 /* Look for the case where we have discovered that we can't replace
3445 register A with register B and that means that we will now be
3446 trying to replace register A with register C. This means we can
3447 no longer replace register C with register B and we need to disable
3448 such an elimination, if it exists. This occurs often with A == ap,
3449 B == sp, and C == fp. */
3451 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3453 struct elim_table *op;
3454 int new_to = -1;
3456 if (! ep->can_eliminate && ep->can_eliminate_previous)
3458 /* Find the current elimination for ep->from, if there is a
3459 new one. */
3460 for (op = reg_eliminate;
3461 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3462 if (op->from == ep->from && op->can_eliminate)
3464 new_to = op->to;
3465 break;
3468 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3469 disable it. */
3470 for (op = reg_eliminate;
3471 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3472 if (op->from == new_to && op->to == ep->to)
3473 op->can_eliminate = 0;
3477 /* See if any registers that we thought we could eliminate the previous
3478 time are no longer eliminable. If so, something has changed and we
3479 must spill the register. Also, recompute the number of eliminable
3480 registers and see if the frame pointer is needed; it is if there is
3481 no elimination of the frame pointer that we can perform. */
3483 frame_pointer_needed = 1;
3484 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3486 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3487 && ep->to != HARD_FRAME_POINTER_REGNUM)
3488 frame_pointer_needed = 0;
3490 if (! ep->can_eliminate && ep->can_eliminate_previous)
3492 ep->can_eliminate_previous = 0;
3493 SET_HARD_REG_BIT (*pset, ep->from);
3494 num_eliminable--;
3498 /* If we didn't need a frame pointer last time, but we do now, spill
3499 the hard frame pointer. */
3500 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3501 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3504 /* Initialize the table of registers to eliminate. */
3506 static void
3507 init_elim_table ()
3509 struct elim_table *ep;
3510 #ifdef ELIMINABLE_REGS
3511 const struct elim_table_1 *ep1;
3512 #endif
3514 if (!reg_eliminate)
3515 reg_eliminate = (struct elim_table *)
3516 xcalloc (sizeof (struct elim_table), NUM_ELIMINABLE_REGS);
3518 /* Does this function require a frame pointer? */
3520 frame_pointer_needed = (! flag_omit_frame_pointer
3521 #ifdef EXIT_IGNORE_STACK
3522 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3523 and restore sp for alloca. So we can't eliminate
3524 the frame pointer in that case. At some point,
3525 we should improve this by emitting the
3526 sp-adjusting insns for this case. */
3527 || (current_function_calls_alloca
3528 && EXIT_IGNORE_STACK)
3529 #endif
3530 || FRAME_POINTER_REQUIRED);
3532 num_eliminable = 0;
3534 #ifdef ELIMINABLE_REGS
3535 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3536 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3538 ep->from = ep1->from;
3539 ep->to = ep1->to;
3540 ep->can_eliminate = ep->can_eliminate_previous
3541 = (CAN_ELIMINATE (ep->from, ep->to)
3542 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3544 #else
3545 reg_eliminate[0].from = reg_eliminate_1[0].from;
3546 reg_eliminate[0].to = reg_eliminate_1[0].to;
3547 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3548 = ! frame_pointer_needed;
3549 #endif
3551 /* Count the number of eliminable registers and build the FROM and TO
3552 REG rtx's. Note that code in gen_rtx will cause, e.g.,
3553 gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3554 We depend on this. */
3555 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3557 num_eliminable += ep->can_eliminate;
3558 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3559 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3563 /* Kick all pseudos out of hard register REGNO.
3565 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3566 because we found we can't eliminate some register. In the case, no pseudos
3567 are allowed to be in the register, even if they are only in a block that
3568 doesn't require spill registers, unlike the case when we are spilling this
3569 hard reg to produce another spill register.
3571 Return nonzero if any pseudos needed to be kicked out. */
3573 static void
3574 spill_hard_reg (regno, cant_eliminate)
3575 unsigned int regno;
3576 int cant_eliminate;
3578 int i;
3580 if (cant_eliminate)
3582 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3583 regs_ever_live[regno] = 1;
3586 /* Spill every pseudo reg that was allocated to this reg
3587 or to something that overlaps this reg. */
3589 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3590 if (reg_renumber[i] >= 0
3591 && (unsigned int) reg_renumber[i] <= regno
3592 && ((unsigned int) reg_renumber[i]
3593 + HARD_REGNO_NREGS ((unsigned int) reg_renumber[i],
3594 PSEUDO_REGNO_MODE (i))
3595 > regno))
3596 SET_REGNO_REG_SET (&spilled_pseudos, i);
3599 /* I'm getting weird preprocessor errors if I use IOR_HARD_REG_SET
3600 from within EXECUTE_IF_SET_IN_REG_SET. Hence this awkwardness. */
3602 static void
3603 ior_hard_reg_set (set1, set2)
3604 HARD_REG_SET *set1, *set2;
3606 IOR_HARD_REG_SET (*set1, *set2);
3609 /* After find_reload_regs has been run for all insn that need reloads,
3610 and/or spill_hard_regs was called, this function is used to actually
3611 spill pseudo registers and try to reallocate them. It also sets up the
3612 spill_regs array for use by choose_reload_regs. */
3614 static int
3615 finish_spills (global)
3616 int global;
3618 struct insn_chain *chain;
3619 int something_changed = 0;
3620 int i;
3622 /* Build the spill_regs array for the function. */
3623 /* If there are some registers still to eliminate and one of the spill regs
3624 wasn't ever used before, additional stack space may have to be
3625 allocated to store this register. Thus, we may have changed the offset
3626 between the stack and frame pointers, so mark that something has changed.
3628 One might think that we need only set VAL to 1 if this is a call-used
3629 register. However, the set of registers that must be saved by the
3630 prologue is not identical to the call-used set. For example, the
3631 register used by the call insn for the return PC is a call-used register,
3632 but must be saved by the prologue. */
3634 n_spills = 0;
3635 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3636 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3638 spill_reg_order[i] = n_spills;
3639 spill_regs[n_spills++] = i;
3640 if (num_eliminable && ! regs_ever_live[i])
3641 something_changed = 1;
3642 regs_ever_live[i] = 1;
3644 else
3645 spill_reg_order[i] = -1;
3647 EXECUTE_IF_SET_IN_REG_SET
3648 (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i,
3650 /* Record the current hard register the pseudo is allocated to in
3651 pseudo_previous_regs so we avoid reallocating it to the same
3652 hard reg in a later pass. */
3653 if (reg_renumber[i] < 0)
3654 abort ();
3656 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3657 /* Mark it as no longer having a hard register home. */
3658 reg_renumber[i] = -1;
3659 /* We will need to scan everything again. */
3660 something_changed = 1;
3663 /* Retry global register allocation if possible. */
3664 if (global)
3666 memset ((char *) pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
3667 /* For every insn that needs reloads, set the registers used as spill
3668 regs in pseudo_forbidden_regs for every pseudo live across the
3669 insn. */
3670 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
3672 EXECUTE_IF_SET_IN_REG_SET
3673 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i,
3675 ior_hard_reg_set (pseudo_forbidden_regs + i,
3676 &chain->used_spill_regs);
3678 EXECUTE_IF_SET_IN_REG_SET
3679 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i,
3681 ior_hard_reg_set (pseudo_forbidden_regs + i,
3682 &chain->used_spill_regs);
3686 /* Retry allocating the spilled pseudos. For each reg, merge the
3687 various reg sets that indicate which hard regs can't be used,
3688 and call retry_global_alloc.
3689 We change spill_pseudos here to only contain pseudos that did not
3690 get a new hard register. */
3691 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3692 if (reg_old_renumber[i] != reg_renumber[i])
3694 HARD_REG_SET forbidden;
3695 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
3696 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
3697 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
3698 retry_global_alloc (i, forbidden);
3699 if (reg_renumber[i] >= 0)
3700 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
3704 /* Fix up the register information in the insn chain.
3705 This involves deleting those of the spilled pseudos which did not get
3706 a new hard register home from the live_{before,after} sets. */
3707 for (chain = reload_insn_chain; chain; chain = chain->next)
3709 HARD_REG_SET used_by_pseudos;
3710 HARD_REG_SET used_by_pseudos2;
3712 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
3713 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
3715 /* Mark any unallocated hard regs as available for spills. That
3716 makes inheritance work somewhat better. */
3717 if (chain->need_reload)
3719 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
3720 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
3721 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
3723 /* Save the old value for the sanity test below. */
3724 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
3726 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
3727 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
3728 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
3729 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
3731 /* Make sure we only enlarge the set. */
3732 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
3733 abort ();
3734 ok:;
3738 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
3739 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3741 int regno = reg_renumber[i];
3742 if (reg_old_renumber[i] == regno)
3743 continue;
3745 alter_reg (i, reg_old_renumber[i]);
3746 reg_old_renumber[i] = regno;
3747 if (rtl_dump_file)
3749 if (regno == -1)
3750 fprintf (rtl_dump_file, " Register %d now on stack.\n\n", i);
3751 else
3752 fprintf (rtl_dump_file, " Register %d now in %d.\n\n",
3753 i, reg_renumber[i]);
3757 return something_changed;
3760 /* Find all paradoxical subregs within X and update reg_max_ref_width.
3761 Also mark any hard registers used to store user variables as
3762 forbidden from being used for spill registers. */
3764 static void
3765 scan_paradoxical_subregs (x)
3766 rtx x;
3768 int i;
3769 const char *fmt;
3770 enum rtx_code code = GET_CODE (x);
3772 switch (code)
3774 case REG:
3775 #if 0
3776 if (SMALL_REGISTER_CLASSES && REGNO (x) < FIRST_PSEUDO_REGISTER
3777 && REG_USERVAR_P (x))
3778 SET_HARD_REG_BIT (bad_spill_regs_global, REGNO (x));
3779 #endif
3780 return;
3782 case CONST_INT:
3783 case CONST:
3784 case SYMBOL_REF:
3785 case LABEL_REF:
3786 case CONST_DOUBLE:
3787 case CONST_VECTOR: /* shouldn't happen, but just in case. */
3788 case CC0:
3789 case PC:
3790 case USE:
3791 case CLOBBER:
3792 return;
3794 case SUBREG:
3795 if (GET_CODE (SUBREG_REG (x)) == REG
3796 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3797 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3798 = GET_MODE_SIZE (GET_MODE (x));
3799 return;
3801 default:
3802 break;
3805 fmt = GET_RTX_FORMAT (code);
3806 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3808 if (fmt[i] == 'e')
3809 scan_paradoxical_subregs (XEXP (x, i));
3810 else if (fmt[i] == 'E')
3812 int j;
3813 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3814 scan_paradoxical_subregs (XVECEXP (x, i, j));
3819 /* Reload pseudo-registers into hard regs around each insn as needed.
3820 Additional register load insns are output before the insn that needs it
3821 and perhaps store insns after insns that modify the reloaded pseudo reg.
3823 reg_last_reload_reg and reg_reloaded_contents keep track of
3824 which registers are already available in reload registers.
3825 We update these for the reloads that we perform,
3826 as the insns are scanned. */
3828 static void
3829 reload_as_needed (live_known)
3830 int live_known;
3832 struct insn_chain *chain;
3833 #if defined (AUTO_INC_DEC)
3834 int i;
3835 #endif
3836 rtx x;
3838 memset ((char *) spill_reg_rtx, 0, sizeof spill_reg_rtx);
3839 memset ((char *) spill_reg_store, 0, sizeof spill_reg_store);
3840 reg_last_reload_reg = (rtx *) xcalloc (max_regno, sizeof (rtx));
3841 reg_has_output_reload = (char *) xmalloc (max_regno);
3842 CLEAR_HARD_REG_SET (reg_reloaded_valid);
3844 set_initial_elim_offsets ();
3846 for (chain = reload_insn_chain; chain; chain = chain->next)
3848 rtx prev = 0;
3849 rtx insn = chain->insn;
3850 rtx old_next = NEXT_INSN (insn);
3852 /* If we pass a label, copy the offsets from the label information
3853 into the current offsets of each elimination. */
3854 if (GET_CODE (insn) == CODE_LABEL)
3855 set_offsets_for_label (insn);
3857 else if (INSN_P (insn))
3859 rtx oldpat = copy_rtx (PATTERN (insn));
3861 /* If this is a USE and CLOBBER of a MEM, ensure that any
3862 references to eliminable registers have been removed. */
3864 if ((GET_CODE (PATTERN (insn)) == USE
3865 || GET_CODE (PATTERN (insn)) == CLOBBER)
3866 && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
3867 XEXP (XEXP (PATTERN (insn), 0), 0)
3868 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
3869 GET_MODE (XEXP (PATTERN (insn), 0)),
3870 NULL_RTX);
3872 /* If we need to do register elimination processing, do so.
3873 This might delete the insn, in which case we are done. */
3874 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
3876 eliminate_regs_in_insn (insn, 1);
3877 if (GET_CODE (insn) == NOTE)
3879 update_eliminable_offsets ();
3880 continue;
3884 /* If need_elim is nonzero but need_reload is zero, one might think
3885 that we could simply set n_reloads to 0. However, find_reloads
3886 could have done some manipulation of the insn (such as swapping
3887 commutative operands), and these manipulations are lost during
3888 the first pass for every insn that needs register elimination.
3889 So the actions of find_reloads must be redone here. */
3891 if (! chain->need_elim && ! chain->need_reload
3892 && ! chain->need_operand_change)
3893 n_reloads = 0;
3894 /* First find the pseudo regs that must be reloaded for this insn.
3895 This info is returned in the tables reload_... (see reload.h).
3896 Also modify the body of INSN by substituting RELOAD
3897 rtx's for those pseudo regs. */
3898 else
3900 memset (reg_has_output_reload, 0, max_regno);
3901 CLEAR_HARD_REG_SET (reg_is_output_reload);
3903 find_reloads (insn, 1, spill_indirect_levels, live_known,
3904 spill_reg_order);
3907 if (n_reloads > 0)
3909 rtx next = NEXT_INSN (insn);
3910 rtx p;
3912 prev = PREV_INSN (insn);
3914 /* Now compute which reload regs to reload them into. Perhaps
3915 reusing reload regs from previous insns, or else output
3916 load insns to reload them. Maybe output store insns too.
3917 Record the choices of reload reg in reload_reg_rtx. */
3918 choose_reload_regs (chain);
3920 /* Merge any reloads that we didn't combine for fear of
3921 increasing the number of spill registers needed but now
3922 discover can be safely merged. */
3923 if (SMALL_REGISTER_CLASSES)
3924 merge_assigned_reloads (insn);
3926 /* Generate the insns to reload operands into or out of
3927 their reload regs. */
3928 emit_reload_insns (chain);
3930 /* Substitute the chosen reload regs from reload_reg_rtx
3931 into the insn's body (or perhaps into the bodies of other
3932 load and store insn that we just made for reloading
3933 and that we moved the structure into). */
3934 subst_reloads (insn);
3936 /* If this was an ASM, make sure that all the reload insns
3937 we have generated are valid. If not, give an error
3938 and delete them. */
3940 if (asm_noperands (PATTERN (insn)) >= 0)
3941 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
3942 if (p != insn && INSN_P (p)
3943 && (recog_memoized (p) < 0
3944 || (extract_insn (p), ! constrain_operands (1))))
3946 error_for_asm (insn,
3947 "`asm' operand requires impossible reload");
3948 delete_insn (p);
3952 if (num_eliminable && chain->need_elim)
3953 update_eliminable_offsets ();
3955 /* Any previously reloaded spilled pseudo reg, stored in this insn,
3956 is no longer validly lying around to save a future reload.
3957 Note that this does not detect pseudos that were reloaded
3958 for this insn in order to be stored in
3959 (obeying register constraints). That is correct; such reload
3960 registers ARE still valid. */
3961 note_stores (oldpat, forget_old_reloads_1, NULL);
3963 /* There may have been CLOBBER insns placed after INSN. So scan
3964 between INSN and NEXT and use them to forget old reloads. */
3965 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
3966 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
3967 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
3969 #ifdef AUTO_INC_DEC
3970 /* Likewise for regs altered by auto-increment in this insn.
3971 REG_INC notes have been changed by reloading:
3972 find_reloads_address_1 records substitutions for them,
3973 which have been performed by subst_reloads above. */
3974 for (i = n_reloads - 1; i >= 0; i--)
3976 rtx in_reg = rld[i].in_reg;
3977 if (in_reg)
3979 enum rtx_code code = GET_CODE (in_reg);
3980 /* PRE_INC / PRE_DEC will have the reload register ending up
3981 with the same value as the stack slot, but that doesn't
3982 hold true for POST_INC / POST_DEC. Either we have to
3983 convert the memory access to a true POST_INC / POST_DEC,
3984 or we can't use the reload register for inheritance. */
3985 if ((code == POST_INC || code == POST_DEC)
3986 && TEST_HARD_REG_BIT (reg_reloaded_valid,
3987 REGNO (rld[i].reg_rtx))
3988 /* Make sure it is the inc/dec pseudo, and not
3989 some other (e.g. output operand) pseudo. */
3990 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
3991 == REGNO (XEXP (in_reg, 0))))
3994 rtx reload_reg = rld[i].reg_rtx;
3995 enum machine_mode mode = GET_MODE (reload_reg);
3996 int n = 0;
3997 rtx p;
3999 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4001 /* We really want to ignore REG_INC notes here, so
4002 use PATTERN (p) as argument to reg_set_p . */
4003 if (reg_set_p (reload_reg, PATTERN (p)))
4004 break;
4005 n = count_occurrences (PATTERN (p), reload_reg, 0);
4006 if (! n)
4007 continue;
4008 if (n == 1)
4010 n = validate_replace_rtx (reload_reg,
4011 gen_rtx (code, mode,
4012 reload_reg),
4015 /* We must also verify that the constraints
4016 are met after the replacement. */
4017 extract_insn (p);
4018 if (n)
4019 n = constrain_operands (1);
4020 else
4021 break;
4023 /* If the constraints were not met, then
4024 undo the replacement. */
4025 if (!n)
4027 validate_replace_rtx (gen_rtx (code, mode,
4028 reload_reg),
4029 reload_reg, p);
4030 break;
4034 break;
4036 if (n == 1)
4038 REG_NOTES (p)
4039 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
4040 REG_NOTES (p));
4041 /* Mark this as having an output reload so that the
4042 REG_INC processing code below won't invalidate
4043 the reload for inheritance. */
4044 SET_HARD_REG_BIT (reg_is_output_reload,
4045 REGNO (reload_reg));
4046 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4048 else
4049 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4050 NULL);
4052 else if ((code == PRE_INC || code == PRE_DEC)
4053 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4054 REGNO (rld[i].reg_rtx))
4055 /* Make sure it is the inc/dec pseudo, and not
4056 some other (e.g. output operand) pseudo. */
4057 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4058 == REGNO (XEXP (in_reg, 0))))
4060 SET_HARD_REG_BIT (reg_is_output_reload,
4061 REGNO (rld[i].reg_rtx));
4062 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4066 /* If a pseudo that got a hard register is auto-incremented,
4067 we must purge records of copying it into pseudos without
4068 hard registers. */
4069 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4070 if (REG_NOTE_KIND (x) == REG_INC)
4072 /* See if this pseudo reg was reloaded in this insn.
4073 If so, its last-reload info is still valid
4074 because it is based on this insn's reload. */
4075 for (i = 0; i < n_reloads; i++)
4076 if (rld[i].out == XEXP (x, 0))
4077 break;
4079 if (i == n_reloads)
4080 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4082 #endif
4084 /* A reload reg's contents are unknown after a label. */
4085 if (GET_CODE (insn) == CODE_LABEL)
4086 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4088 /* Don't assume a reload reg is still good after a call insn
4089 if it is a call-used reg. */
4090 else if (GET_CODE (insn) == CALL_INSN)
4091 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
4094 /* Clean up. */
4095 free (reg_last_reload_reg);
4096 free (reg_has_output_reload);
4099 /* Discard all record of any value reloaded from X,
4100 or reloaded in X from someplace else;
4101 unless X is an output reload reg of the current insn.
4103 X may be a hard reg (the reload reg)
4104 or it may be a pseudo reg that was reloaded from. */
4106 static void
4107 forget_old_reloads_1 (x, ignored, data)
4108 rtx x;
4109 rtx ignored ATTRIBUTE_UNUSED;
4110 void *data ATTRIBUTE_UNUSED;
4112 unsigned int regno;
4113 unsigned int nr;
4115 /* note_stores does give us subregs of hard regs,
4116 subreg_regno_offset will abort if it is not a hard reg. */
4117 while (GET_CODE (x) == SUBREG)
4119 /* We ignore the subreg offset when calculating the regno,
4120 because we are using the entire underlying hard register
4121 below. */
4122 x = SUBREG_REG (x);
4125 if (GET_CODE (x) != REG)
4126 return;
4128 regno = REGNO (x);
4130 if (regno >= FIRST_PSEUDO_REGISTER)
4131 nr = 1;
4132 else
4134 unsigned int i;
4136 nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
4137 /* Storing into a spilled-reg invalidates its contents.
4138 This can happen if a block-local pseudo is allocated to that reg
4139 and it wasn't spilled because this block's total need is 0.
4140 Then some insn might have an optional reload and use this reg. */
4141 for (i = 0; i < nr; i++)
4142 /* But don't do this if the reg actually serves as an output
4143 reload reg in the current instruction. */
4144 if (n_reloads == 0
4145 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4147 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4148 spill_reg_store[regno + i] = 0;
4152 /* Since value of X has changed,
4153 forget any value previously copied from it. */
4155 while (nr-- > 0)
4156 /* But don't forget a copy if this is the output reload
4157 that establishes the copy's validity. */
4158 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4159 reg_last_reload_reg[regno + nr] = 0;
4162 /* The following HARD_REG_SETs indicate when each hard register is
4163 used for a reload of various parts of the current insn. */
4165 /* If reg is unavailable for all reloads. */
4166 static HARD_REG_SET reload_reg_unavailable;
4167 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4168 static HARD_REG_SET reload_reg_used;
4169 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4170 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4171 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4172 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4173 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4174 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4175 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4176 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4177 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4178 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4179 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4180 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4181 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4182 static HARD_REG_SET reload_reg_used_in_op_addr;
4183 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4184 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4185 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4186 static HARD_REG_SET reload_reg_used_in_insn;
4187 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4188 static HARD_REG_SET reload_reg_used_in_other_addr;
4190 /* If reg is in use as a reload reg for any sort of reload. */
4191 static HARD_REG_SET reload_reg_used_at_all;
4193 /* If reg is use as an inherited reload. We just mark the first register
4194 in the group. */
4195 static HARD_REG_SET reload_reg_used_for_inherit;
4197 /* Records which hard regs are used in any way, either as explicit use or
4198 by being allocated to a pseudo during any point of the current insn. */
4199 static HARD_REG_SET reg_used_in_insn;
4201 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4202 TYPE. MODE is used to indicate how many consecutive regs are
4203 actually used. */
4205 static void
4206 mark_reload_reg_in_use (regno, opnum, type, mode)
4207 unsigned int regno;
4208 int opnum;
4209 enum reload_type type;
4210 enum machine_mode mode;
4212 unsigned int nregs = HARD_REGNO_NREGS (regno, mode);
4213 unsigned int i;
4215 for (i = regno; i < nregs + regno; i++)
4217 switch (type)
4219 case RELOAD_OTHER:
4220 SET_HARD_REG_BIT (reload_reg_used, i);
4221 break;
4223 case RELOAD_FOR_INPUT_ADDRESS:
4224 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4225 break;
4227 case RELOAD_FOR_INPADDR_ADDRESS:
4228 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4229 break;
4231 case RELOAD_FOR_OUTPUT_ADDRESS:
4232 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4233 break;
4235 case RELOAD_FOR_OUTADDR_ADDRESS:
4236 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4237 break;
4239 case RELOAD_FOR_OPERAND_ADDRESS:
4240 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4241 break;
4243 case RELOAD_FOR_OPADDR_ADDR:
4244 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4245 break;
4247 case RELOAD_FOR_OTHER_ADDRESS:
4248 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4249 break;
4251 case RELOAD_FOR_INPUT:
4252 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4253 break;
4255 case RELOAD_FOR_OUTPUT:
4256 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4257 break;
4259 case RELOAD_FOR_INSN:
4260 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4261 break;
4264 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4268 /* Similarly, but show REGNO is no longer in use for a reload. */
4270 static void
4271 clear_reload_reg_in_use (regno, opnum, type, mode)
4272 unsigned int regno;
4273 int opnum;
4274 enum reload_type type;
4275 enum machine_mode mode;
4277 unsigned int nregs = HARD_REGNO_NREGS (regno, mode);
4278 unsigned int start_regno, end_regno, r;
4279 int i;
4280 /* A complication is that for some reload types, inheritance might
4281 allow multiple reloads of the same types to share a reload register.
4282 We set check_opnum if we have to check only reloads with the same
4283 operand number, and check_any if we have to check all reloads. */
4284 int check_opnum = 0;
4285 int check_any = 0;
4286 HARD_REG_SET *used_in_set;
4288 switch (type)
4290 case RELOAD_OTHER:
4291 used_in_set = &reload_reg_used;
4292 break;
4294 case RELOAD_FOR_INPUT_ADDRESS:
4295 used_in_set = &reload_reg_used_in_input_addr[opnum];
4296 break;
4298 case RELOAD_FOR_INPADDR_ADDRESS:
4299 check_opnum = 1;
4300 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4301 break;
4303 case RELOAD_FOR_OUTPUT_ADDRESS:
4304 used_in_set = &reload_reg_used_in_output_addr[opnum];
4305 break;
4307 case RELOAD_FOR_OUTADDR_ADDRESS:
4308 check_opnum = 1;
4309 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4310 break;
4312 case RELOAD_FOR_OPERAND_ADDRESS:
4313 used_in_set = &reload_reg_used_in_op_addr;
4314 break;
4316 case RELOAD_FOR_OPADDR_ADDR:
4317 check_any = 1;
4318 used_in_set = &reload_reg_used_in_op_addr_reload;
4319 break;
4321 case RELOAD_FOR_OTHER_ADDRESS:
4322 used_in_set = &reload_reg_used_in_other_addr;
4323 check_any = 1;
4324 break;
4326 case RELOAD_FOR_INPUT:
4327 used_in_set = &reload_reg_used_in_input[opnum];
4328 break;
4330 case RELOAD_FOR_OUTPUT:
4331 used_in_set = &reload_reg_used_in_output[opnum];
4332 break;
4334 case RELOAD_FOR_INSN:
4335 used_in_set = &reload_reg_used_in_insn;
4336 break;
4337 default:
4338 abort ();
4340 /* We resolve conflicts with remaining reloads of the same type by
4341 excluding the intervals of reload registers by them from the
4342 interval of freed reload registers. Since we only keep track of
4343 one set of interval bounds, we might have to exclude somewhat
4344 more than what would be necessary if we used a HARD_REG_SET here.
4345 But this should only happen very infrequently, so there should
4346 be no reason to worry about it. */
4348 start_regno = regno;
4349 end_regno = regno + nregs;
4350 if (check_opnum || check_any)
4352 for (i = n_reloads - 1; i >= 0; i--)
4354 if (rld[i].when_needed == type
4355 && (check_any || rld[i].opnum == opnum)
4356 && rld[i].reg_rtx)
4358 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
4359 unsigned int conflict_end
4360 = (conflict_start
4361 + HARD_REGNO_NREGS (conflict_start, rld[i].mode));
4363 /* If there is an overlap with the first to-be-freed register,
4364 adjust the interval start. */
4365 if (conflict_start <= start_regno && conflict_end > start_regno)
4366 start_regno = conflict_end;
4367 /* Otherwise, if there is a conflict with one of the other
4368 to-be-freed registers, adjust the interval end. */
4369 if (conflict_start > start_regno && conflict_start < end_regno)
4370 end_regno = conflict_start;
4375 for (r = start_regno; r < end_regno; r++)
4376 CLEAR_HARD_REG_BIT (*used_in_set, r);
4379 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4380 specified by OPNUM and TYPE. */
4382 static int
4383 reload_reg_free_p (regno, opnum, type)
4384 unsigned int regno;
4385 int opnum;
4386 enum reload_type type;
4388 int i;
4390 /* In use for a RELOAD_OTHER means it's not available for anything. */
4391 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
4392 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4393 return 0;
4395 switch (type)
4397 case RELOAD_OTHER:
4398 /* In use for anything means we can't use it for RELOAD_OTHER. */
4399 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4400 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4401 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4402 return 0;
4404 for (i = 0; i < reload_n_operands; i++)
4405 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4406 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4407 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4408 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4409 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4410 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4411 return 0;
4413 return 1;
4415 case RELOAD_FOR_INPUT:
4416 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4417 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4418 return 0;
4420 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4421 return 0;
4423 /* If it is used for some other input, can't use it. */
4424 for (i = 0; i < reload_n_operands; i++)
4425 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4426 return 0;
4428 /* If it is used in a later operand's address, can't use it. */
4429 for (i = opnum + 1; i < reload_n_operands; i++)
4430 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4431 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4432 return 0;
4434 return 1;
4436 case RELOAD_FOR_INPUT_ADDRESS:
4437 /* Can't use a register if it is used for an input address for this
4438 operand or used as an input in an earlier one. */
4439 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4440 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4441 return 0;
4443 for (i = 0; i < opnum; i++)
4444 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4445 return 0;
4447 return 1;
4449 case RELOAD_FOR_INPADDR_ADDRESS:
4450 /* Can't use a register if it is used for an input address
4451 for this operand or used as an input in an earlier
4452 one. */
4453 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4454 return 0;
4456 for (i = 0; i < opnum; i++)
4457 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4458 return 0;
4460 return 1;
4462 case RELOAD_FOR_OUTPUT_ADDRESS:
4463 /* Can't use a register if it is used for an output address for this
4464 operand or used as an output in this or a later operand. Note
4465 that multiple output operands are emitted in reverse order, so
4466 the conflicting ones are those with lower indices. */
4467 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4468 return 0;
4470 for (i = 0; i <= opnum; i++)
4471 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4472 return 0;
4474 return 1;
4476 case RELOAD_FOR_OUTADDR_ADDRESS:
4477 /* Can't use a register if it is used for an output address
4478 for this operand or used as an output in this or a
4479 later operand. Note that multiple output operands are
4480 emitted in reverse order, so the conflicting ones are
4481 those with lower indices. */
4482 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4483 return 0;
4485 for (i = 0; i <= opnum; i++)
4486 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4487 return 0;
4489 return 1;
4491 case RELOAD_FOR_OPERAND_ADDRESS:
4492 for (i = 0; i < reload_n_operands; i++)
4493 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4494 return 0;
4496 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4497 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4499 case RELOAD_FOR_OPADDR_ADDR:
4500 for (i = 0; i < reload_n_operands; i++)
4501 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4502 return 0;
4504 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4506 case RELOAD_FOR_OUTPUT:
4507 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4508 outputs, or an operand address for this or an earlier output.
4509 Note that multiple output operands are emitted in reverse order,
4510 so the conflicting ones are those with higher indices. */
4511 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4512 return 0;
4514 for (i = 0; i < reload_n_operands; i++)
4515 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4516 return 0;
4518 for (i = opnum; i < reload_n_operands; i++)
4519 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4520 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4521 return 0;
4523 return 1;
4525 case RELOAD_FOR_INSN:
4526 for (i = 0; i < reload_n_operands; i++)
4527 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4528 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4529 return 0;
4531 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4532 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4534 case RELOAD_FOR_OTHER_ADDRESS:
4535 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4537 abort ();
4540 /* Return 1 if the value in reload reg REGNO, as used by a reload
4541 needed for the part of the insn specified by OPNUM and TYPE,
4542 is still available in REGNO at the end of the insn.
4544 We can assume that the reload reg was already tested for availability
4545 at the time it is needed, and we should not check this again,
4546 in case the reg has already been marked in use. */
4548 static int
4549 reload_reg_reaches_end_p (regno, opnum, type)
4550 unsigned int regno;
4551 int opnum;
4552 enum reload_type type;
4554 int i;
4556 switch (type)
4558 case RELOAD_OTHER:
4559 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4560 its value must reach the end. */
4561 return 1;
4563 /* If this use is for part of the insn,
4564 its value reaches if no subsequent part uses the same register.
4565 Just like the above function, don't try to do this with lots
4566 of fallthroughs. */
4568 case RELOAD_FOR_OTHER_ADDRESS:
4569 /* Here we check for everything else, since these don't conflict
4570 with anything else and everything comes later. */
4572 for (i = 0; i < reload_n_operands; i++)
4573 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4574 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4575 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4576 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4577 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4578 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4579 return 0;
4581 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4582 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4583 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4585 case RELOAD_FOR_INPUT_ADDRESS:
4586 case RELOAD_FOR_INPADDR_ADDRESS:
4587 /* Similar, except that we check only for this and subsequent inputs
4588 and the address of only subsequent inputs and we do not need
4589 to check for RELOAD_OTHER objects since they are known not to
4590 conflict. */
4592 for (i = opnum; i < reload_n_operands; i++)
4593 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4594 return 0;
4596 for (i = opnum + 1; i < reload_n_operands; i++)
4597 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4598 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4599 return 0;
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 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4608 return 0;
4610 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4611 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4612 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4614 case RELOAD_FOR_INPUT:
4615 /* Similar to input address, except we start at the next operand for
4616 both input and input address and we do not check for
4617 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4618 would conflict. */
4620 for (i = opnum + 1; i < reload_n_operands; i++)
4621 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4622 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4623 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4624 return 0;
4626 /* ... fall through ... */
4628 case RELOAD_FOR_OPERAND_ADDRESS:
4629 /* Check outputs and their addresses. */
4631 for (i = 0; i < reload_n_operands; i++)
4632 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4633 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4634 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4635 return 0;
4637 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
4639 case RELOAD_FOR_OPADDR_ADDR:
4640 for (i = 0; i < reload_n_operands; i++)
4641 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4642 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4643 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4644 return 0;
4646 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4647 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4648 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4650 case RELOAD_FOR_INSN:
4651 /* These conflict with other outputs with RELOAD_OTHER. So
4652 we need only check for output addresses. */
4654 opnum = reload_n_operands;
4656 /* ... fall through ... */
4658 case RELOAD_FOR_OUTPUT:
4659 case RELOAD_FOR_OUTPUT_ADDRESS:
4660 case RELOAD_FOR_OUTADDR_ADDRESS:
4661 /* We already know these can't conflict with a later output. So the
4662 only thing to check are later output addresses.
4663 Note that multiple output operands are emitted in reverse order,
4664 so the conflicting ones are those with lower indices. */
4665 for (i = 0; i < opnum; i++)
4666 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4667 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4668 return 0;
4670 return 1;
4673 abort ();
4676 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
4677 Return 0 otherwise.
4679 This function uses the same algorithm as reload_reg_free_p above. */
4682 reloads_conflict (r1, r2)
4683 int r1, r2;
4685 enum reload_type r1_type = rld[r1].when_needed;
4686 enum reload_type r2_type = rld[r2].when_needed;
4687 int r1_opnum = rld[r1].opnum;
4688 int r2_opnum = rld[r2].opnum;
4690 /* RELOAD_OTHER conflicts with everything. */
4691 if (r2_type == RELOAD_OTHER)
4692 return 1;
4694 /* Otherwise, check conflicts differently for each type. */
4696 switch (r1_type)
4698 case RELOAD_FOR_INPUT:
4699 return (r2_type == RELOAD_FOR_INSN
4700 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
4701 || r2_type == RELOAD_FOR_OPADDR_ADDR
4702 || r2_type == RELOAD_FOR_INPUT
4703 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
4704 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
4705 && r2_opnum > r1_opnum));
4707 case RELOAD_FOR_INPUT_ADDRESS:
4708 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
4709 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4711 case RELOAD_FOR_INPADDR_ADDRESS:
4712 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
4713 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4715 case RELOAD_FOR_OUTPUT_ADDRESS:
4716 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
4717 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4719 case RELOAD_FOR_OUTADDR_ADDRESS:
4720 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
4721 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4723 case RELOAD_FOR_OPERAND_ADDRESS:
4724 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
4725 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4727 case RELOAD_FOR_OPADDR_ADDR:
4728 return (r2_type == RELOAD_FOR_INPUT
4729 || r2_type == RELOAD_FOR_OPADDR_ADDR);
4731 case RELOAD_FOR_OUTPUT:
4732 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
4733 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
4734 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
4735 && r2_opnum >= r1_opnum));
4737 case RELOAD_FOR_INSN:
4738 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
4739 || r2_type == RELOAD_FOR_INSN
4740 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4742 case RELOAD_FOR_OTHER_ADDRESS:
4743 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
4745 case RELOAD_OTHER:
4746 return 1;
4748 default:
4749 abort ();
4753 /* Indexed by reload number, 1 if incoming value
4754 inherited from previous insns. */
4755 char reload_inherited[MAX_RELOADS];
4757 /* For an inherited reload, this is the insn the reload was inherited from,
4758 if we know it. Otherwise, this is 0. */
4759 rtx reload_inheritance_insn[MAX_RELOADS];
4761 /* If nonzero, this is a place to get the value of the reload,
4762 rather than using reload_in. */
4763 rtx reload_override_in[MAX_RELOADS];
4765 /* For each reload, the hard register number of the register used,
4766 or -1 if we did not need a register for this reload. */
4767 int reload_spill_index[MAX_RELOADS];
4769 /* Subroutine of free_for_value_p, used to check a single register.
4770 START_REGNO is the starting regno of the full reload register
4771 (possibly comprising multiple hard registers) that we are considering. */
4773 static int
4774 reload_reg_free_for_value_p (start_regno, regno, opnum, type, value, out,
4775 reloadnum, ignore_address_reloads)
4776 int start_regno, regno;
4777 int opnum;
4778 enum reload_type type;
4779 rtx value, out;
4780 int reloadnum;
4781 int ignore_address_reloads;
4783 int time1;
4784 /* Set if we see an input reload that must not share its reload register
4785 with any new earlyclobber, but might otherwise share the reload
4786 register with an output or input-output reload. */
4787 int check_earlyclobber = 0;
4788 int i;
4789 int copy = 0;
4791 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4792 return 0;
4794 if (out == const0_rtx)
4796 copy = 1;
4797 out = NULL_RTX;
4800 /* We use some pseudo 'time' value to check if the lifetimes of the
4801 new register use would overlap with the one of a previous reload
4802 that is not read-only or uses a different value.
4803 The 'time' used doesn't have to be linear in any shape or form, just
4804 monotonic.
4805 Some reload types use different 'buckets' for each operand.
4806 So there are MAX_RECOG_OPERANDS different time values for each
4807 such reload type.
4808 We compute TIME1 as the time when the register for the prospective
4809 new reload ceases to be live, and TIME2 for each existing
4810 reload as the time when that the reload register of that reload
4811 becomes live.
4812 Where there is little to be gained by exact lifetime calculations,
4813 we just make conservative assumptions, i.e. a longer lifetime;
4814 this is done in the 'default:' cases. */
4815 switch (type)
4817 case RELOAD_FOR_OTHER_ADDRESS:
4818 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
4819 time1 = copy ? 0 : 1;
4820 break;
4821 case RELOAD_OTHER:
4822 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
4823 break;
4824 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
4825 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
4826 respectively, to the time values for these, we get distinct time
4827 values. To get distinct time values for each operand, we have to
4828 multiply opnum by at least three. We round that up to four because
4829 multiply by four is often cheaper. */
4830 case RELOAD_FOR_INPADDR_ADDRESS:
4831 time1 = opnum * 4 + 2;
4832 break;
4833 case RELOAD_FOR_INPUT_ADDRESS:
4834 time1 = opnum * 4 + 3;
4835 break;
4836 case RELOAD_FOR_INPUT:
4837 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
4838 executes (inclusive). */
4839 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
4840 break;
4841 case RELOAD_FOR_OPADDR_ADDR:
4842 /* opnum * 4 + 4
4843 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
4844 time1 = MAX_RECOG_OPERANDS * 4 + 1;
4845 break;
4846 case RELOAD_FOR_OPERAND_ADDRESS:
4847 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
4848 is executed. */
4849 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
4850 break;
4851 case RELOAD_FOR_OUTADDR_ADDRESS:
4852 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
4853 break;
4854 case RELOAD_FOR_OUTPUT_ADDRESS:
4855 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
4856 break;
4857 default:
4858 time1 = MAX_RECOG_OPERANDS * 5 + 5;
4861 for (i = 0; i < n_reloads; i++)
4863 rtx reg = rld[i].reg_rtx;
4864 if (reg && GET_CODE (reg) == REG
4865 && ((unsigned) regno - true_regnum (reg)
4866 <= HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)) - (unsigned) 1)
4867 && i != reloadnum)
4869 rtx other_input = rld[i].in;
4871 /* If the other reload loads the same input value, that
4872 will not cause a conflict only if it's loading it into
4873 the same register. */
4874 if (true_regnum (reg) != start_regno)
4875 other_input = NULL_RTX;
4876 if (! other_input || ! rtx_equal_p (other_input, value)
4877 || rld[i].out || out)
4879 int time2;
4880 switch (rld[i].when_needed)
4882 case RELOAD_FOR_OTHER_ADDRESS:
4883 time2 = 0;
4884 break;
4885 case RELOAD_FOR_INPADDR_ADDRESS:
4886 /* find_reloads makes sure that a
4887 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
4888 by at most one - the first -
4889 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
4890 address reload is inherited, the address address reload
4891 goes away, so we can ignore this conflict. */
4892 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
4893 && ignore_address_reloads
4894 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
4895 Then the address address is still needed to store
4896 back the new address. */
4897 && ! rld[reloadnum].out)
4898 continue;
4899 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
4900 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
4901 reloads go away. */
4902 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
4903 && ignore_address_reloads
4904 /* Unless we are reloading an auto_inc expression. */
4905 && ! rld[reloadnum].out)
4906 continue;
4907 time2 = rld[i].opnum * 4 + 2;
4908 break;
4909 case RELOAD_FOR_INPUT_ADDRESS:
4910 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
4911 && ignore_address_reloads
4912 && ! rld[reloadnum].out)
4913 continue;
4914 time2 = rld[i].opnum * 4 + 3;
4915 break;
4916 case RELOAD_FOR_INPUT:
4917 time2 = rld[i].opnum * 4 + 4;
4918 check_earlyclobber = 1;
4919 break;
4920 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
4921 == MAX_RECOG_OPERAND * 4 */
4922 case RELOAD_FOR_OPADDR_ADDR:
4923 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
4924 && ignore_address_reloads
4925 && ! rld[reloadnum].out)
4926 continue;
4927 time2 = MAX_RECOG_OPERANDS * 4 + 1;
4928 break;
4929 case RELOAD_FOR_OPERAND_ADDRESS:
4930 time2 = MAX_RECOG_OPERANDS * 4 + 2;
4931 check_earlyclobber = 1;
4932 break;
4933 case RELOAD_FOR_INSN:
4934 time2 = MAX_RECOG_OPERANDS * 4 + 3;
4935 break;
4936 case RELOAD_FOR_OUTPUT:
4937 /* All RELOAD_FOR_OUTPUT reloads become live just after the
4938 instruction is executed. */
4939 time2 = MAX_RECOG_OPERANDS * 4 + 4;
4940 break;
4941 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
4942 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
4943 value. */
4944 case RELOAD_FOR_OUTADDR_ADDRESS:
4945 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
4946 && ignore_address_reloads
4947 && ! rld[reloadnum].out)
4948 continue;
4949 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
4950 break;
4951 case RELOAD_FOR_OUTPUT_ADDRESS:
4952 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
4953 break;
4954 case RELOAD_OTHER:
4955 /* If there is no conflict in the input part, handle this
4956 like an output reload. */
4957 if (! rld[i].in || rtx_equal_p (other_input, value))
4959 time2 = MAX_RECOG_OPERANDS * 4 + 4;
4960 /* Earlyclobbered outputs must conflict with inputs. */
4961 if (earlyclobber_operand_p (rld[i].out))
4962 time2 = MAX_RECOG_OPERANDS * 4 + 3;
4964 break;
4966 time2 = 1;
4967 /* RELOAD_OTHER might be live beyond instruction execution,
4968 but this is not obvious when we set time2 = 1. So check
4969 here if there might be a problem with the new reload
4970 clobbering the register used by the RELOAD_OTHER. */
4971 if (out)
4972 return 0;
4973 break;
4974 default:
4975 return 0;
4977 if ((time1 >= time2
4978 && (! rld[i].in || rld[i].out
4979 || ! rtx_equal_p (other_input, value)))
4980 || (out && rld[reloadnum].out_reg
4981 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
4982 return 0;
4987 /* Earlyclobbered outputs must conflict with inputs. */
4988 if (check_earlyclobber && out && earlyclobber_operand_p (out))
4989 return 0;
4991 return 1;
4994 /* Return 1 if the value in reload reg REGNO, as used by a reload
4995 needed for the part of the insn specified by OPNUM and TYPE,
4996 may be used to load VALUE into it.
4998 MODE is the mode in which the register is used, this is needed to
4999 determine how many hard regs to test.
5001 Other read-only reloads with the same value do not conflict
5002 unless OUT is nonzero and these other reloads have to live while
5003 output reloads live.
5004 If OUT is CONST0_RTX, this is a special case: it means that the
5005 test should not be for using register REGNO as reload register, but
5006 for copying from register REGNO into the reload register.
5008 RELOADNUM is the number of the reload we want to load this value for;
5009 a reload does not conflict with itself.
5011 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
5012 reloads that load an address for the very reload we are considering.
5014 The caller has to make sure that there is no conflict with the return
5015 register. */
5017 static int
5018 free_for_value_p (regno, mode, opnum, type, value, out, reloadnum,
5019 ignore_address_reloads)
5020 int regno;
5021 enum machine_mode mode;
5022 int opnum;
5023 enum reload_type type;
5024 rtx value, out;
5025 int reloadnum;
5026 int ignore_address_reloads;
5028 int nregs = HARD_REGNO_NREGS (regno, mode);
5029 while (nregs-- > 0)
5030 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
5031 value, out, reloadnum,
5032 ignore_address_reloads))
5033 return 0;
5034 return 1;
5037 /* Determine whether the reload reg X overlaps any rtx'es used for
5038 overriding inheritance. Return nonzero if so. */
5040 static int
5041 conflicts_with_override (x)
5042 rtx x;
5044 int i;
5045 for (i = 0; i < n_reloads; i++)
5046 if (reload_override_in[i]
5047 && reg_overlap_mentioned_p (x, reload_override_in[i]))
5048 return 1;
5049 return 0;
5052 /* Give an error message saying we failed to find a reload for INSN,
5053 and clear out reload R. */
5054 static void
5055 failed_reload (insn, r)
5056 rtx insn;
5057 int r;
5059 if (asm_noperands (PATTERN (insn)) < 0)
5060 /* It's the compiler's fault. */
5061 fatal_insn ("could not find a spill register", insn);
5063 /* It's the user's fault; the operand's mode and constraint
5064 don't match. Disable this reload so we don't crash in final. */
5065 error_for_asm (insn,
5066 "`asm' operand constraint incompatible with operand size");
5067 rld[r].in = 0;
5068 rld[r].out = 0;
5069 rld[r].reg_rtx = 0;
5070 rld[r].optional = 1;
5071 rld[r].secondary_p = 1;
5074 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5075 for reload R. If it's valid, get an rtx for it. Return nonzero if
5076 successful. */
5077 static int
5078 set_reload_reg (i, r)
5079 int i, r;
5081 int regno;
5082 rtx reg = spill_reg_rtx[i];
5084 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5085 spill_reg_rtx[i] = reg
5086 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5088 regno = true_regnum (reg);
5090 /* Detect when the reload reg can't hold the reload mode.
5091 This used to be one `if', but Sequent compiler can't handle that. */
5092 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5094 enum machine_mode test_mode = VOIDmode;
5095 if (rld[r].in)
5096 test_mode = GET_MODE (rld[r].in);
5097 /* If rld[r].in has VOIDmode, it means we will load it
5098 in whatever mode the reload reg has: to wit, rld[r].mode.
5099 We have already tested that for validity. */
5100 /* Aside from that, we need to test that the expressions
5101 to reload from or into have modes which are valid for this
5102 reload register. Otherwise the reload insns would be invalid. */
5103 if (! (rld[r].in != 0 && test_mode != VOIDmode
5104 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5105 if (! (rld[r].out != 0
5106 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5108 /* The reg is OK. */
5109 last_spill_reg = i;
5111 /* Mark as in use for this insn the reload regs we use
5112 for this. */
5113 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5114 rld[r].when_needed, rld[r].mode);
5116 rld[r].reg_rtx = reg;
5117 reload_spill_index[r] = spill_regs[i];
5118 return 1;
5121 return 0;
5124 /* Find a spill register to use as a reload register for reload R.
5125 LAST_RELOAD is nonzero if this is the last reload for the insn being
5126 processed.
5128 Set rld[R].reg_rtx to the register allocated.
5130 We return 1 if successful, or 0 if we couldn't find a spill reg and
5131 we didn't change anything. */
5133 static int
5134 allocate_reload_reg (chain, r, last_reload)
5135 struct insn_chain *chain ATTRIBUTE_UNUSED;
5136 int r;
5137 int last_reload;
5139 int i, pass, count;
5141 /* If we put this reload ahead, thinking it is a group,
5142 then insist on finding a group. Otherwise we can grab a
5143 reg that some other reload needs.
5144 (That can happen when we have a 68000 DATA_OR_FP_REG
5145 which is a group of data regs or one fp reg.)
5146 We need not be so restrictive if there are no more reloads
5147 for this insn.
5149 ??? Really it would be nicer to have smarter handling
5150 for that kind of reg class, where a problem like this is normal.
5151 Perhaps those classes should be avoided for reloading
5152 by use of more alternatives. */
5154 int force_group = rld[r].nregs > 1 && ! last_reload;
5156 /* If we want a single register and haven't yet found one,
5157 take any reg in the right class and not in use.
5158 If we want a consecutive group, here is where we look for it.
5160 We use two passes so we can first look for reload regs to
5161 reuse, which are already in use for other reloads in this insn,
5162 and only then use additional registers.
5163 I think that maximizing reuse is needed to make sure we don't
5164 run out of reload regs. Suppose we have three reloads, and
5165 reloads A and B can share regs. These need two regs.
5166 Suppose A and B are given different regs.
5167 That leaves none for C. */
5168 for (pass = 0; pass < 2; pass++)
5170 /* I is the index in spill_regs.
5171 We advance it round-robin between insns to use all spill regs
5172 equally, so that inherited reloads have a chance
5173 of leapfrogging each other. */
5175 i = last_spill_reg;
5177 for (count = 0; count < n_spills; count++)
5179 int class = (int) rld[r].class;
5180 int regnum;
5182 i++;
5183 if (i >= n_spills)
5184 i -= n_spills;
5185 regnum = spill_regs[i];
5187 if ((reload_reg_free_p (regnum, rld[r].opnum,
5188 rld[r].when_needed)
5189 || (rld[r].in
5190 /* We check reload_reg_used to make sure we
5191 don't clobber the return register. */
5192 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5193 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
5194 rld[r].when_needed, rld[r].in,
5195 rld[r].out, r, 1)))
5196 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5197 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5198 /* Look first for regs to share, then for unshared. But
5199 don't share regs used for inherited reloads; they are
5200 the ones we want to preserve. */
5201 && (pass
5202 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5203 regnum)
5204 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5205 regnum))))
5207 int nr = HARD_REGNO_NREGS (regnum, rld[r].mode);
5208 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5209 (on 68000) got us two FP regs. If NR is 1,
5210 we would reject both of them. */
5211 if (force_group)
5212 nr = rld[r].nregs;
5213 /* If we need only one reg, we have already won. */
5214 if (nr == 1)
5216 /* But reject a single reg if we demand a group. */
5217 if (force_group)
5218 continue;
5219 break;
5221 /* Otherwise check that as many consecutive regs as we need
5222 are available here. */
5223 while (nr > 1)
5225 int regno = regnum + nr - 1;
5226 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5227 && spill_reg_order[regno] >= 0
5228 && reload_reg_free_p (regno, rld[r].opnum,
5229 rld[r].when_needed)))
5230 break;
5231 nr--;
5233 if (nr == 1)
5234 break;
5238 /* If we found something on pass 1, omit pass 2. */
5239 if (count < n_spills)
5240 break;
5243 /* We should have found a spill register by now. */
5244 if (count >= n_spills)
5245 return 0;
5247 /* I is the index in SPILL_REG_RTX of the reload register we are to
5248 allocate. Get an rtx for it and find its register number. */
5250 return set_reload_reg (i, r);
5253 /* Initialize all the tables needed to allocate reload registers.
5254 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5255 is the array we use to restore the reg_rtx field for every reload. */
5257 static void
5258 choose_reload_regs_init (chain, save_reload_reg_rtx)
5259 struct insn_chain *chain;
5260 rtx *save_reload_reg_rtx;
5262 int i;
5264 for (i = 0; i < n_reloads; i++)
5265 rld[i].reg_rtx = save_reload_reg_rtx[i];
5267 memset (reload_inherited, 0, MAX_RELOADS);
5268 memset ((char *) reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
5269 memset ((char *) reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
5271 CLEAR_HARD_REG_SET (reload_reg_used);
5272 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5273 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5274 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5275 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5276 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5278 CLEAR_HARD_REG_SET (reg_used_in_insn);
5280 HARD_REG_SET tmp;
5281 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
5282 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5283 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
5284 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5285 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
5286 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
5289 for (i = 0; i < reload_n_operands; i++)
5291 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5292 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5293 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5294 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5295 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5296 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5299 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
5301 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5303 for (i = 0; i < n_reloads; i++)
5304 /* If we have already decided to use a certain register,
5305 don't use it in another way. */
5306 if (rld[i].reg_rtx)
5307 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5308 rld[i].when_needed, rld[i].mode);
5311 /* Assign hard reg targets for the pseudo-registers we must reload
5312 into hard regs for this insn.
5313 Also output the instructions to copy them in and out of the hard regs.
5315 For machines with register classes, we are responsible for
5316 finding a reload reg in the proper class. */
5318 static void
5319 choose_reload_regs (chain)
5320 struct insn_chain *chain;
5322 rtx insn = chain->insn;
5323 int i, j;
5324 unsigned int max_group_size = 1;
5325 enum reg_class group_class = NO_REGS;
5326 int pass, win, inheritance;
5328 rtx save_reload_reg_rtx[MAX_RELOADS];
5330 /* In order to be certain of getting the registers we need,
5331 we must sort the reloads into order of increasing register class.
5332 Then our grabbing of reload registers will parallel the process
5333 that provided the reload registers.
5335 Also note whether any of the reloads wants a consecutive group of regs.
5336 If so, record the maximum size of the group desired and what
5337 register class contains all the groups needed by this insn. */
5339 for (j = 0; j < n_reloads; j++)
5341 reload_order[j] = j;
5342 reload_spill_index[j] = -1;
5344 if (rld[j].nregs > 1)
5346 max_group_size = MAX (rld[j].nregs, max_group_size);
5347 group_class
5348 = reg_class_superunion[(int) rld[j].class][(int) group_class];
5351 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5354 if (n_reloads > 1)
5355 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5357 /* If -O, try first with inheritance, then turning it off.
5358 If not -O, don't do inheritance.
5359 Using inheritance when not optimizing leads to paradoxes
5360 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5361 because one side of the comparison might be inherited. */
5362 win = 0;
5363 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5365 choose_reload_regs_init (chain, save_reload_reg_rtx);
5367 /* Process the reloads in order of preference just found.
5368 Beyond this point, subregs can be found in reload_reg_rtx.
5370 This used to look for an existing reloaded home for all of the
5371 reloads, and only then perform any new reloads. But that could lose
5372 if the reloads were done out of reg-class order because a later
5373 reload with a looser constraint might have an old home in a register
5374 needed by an earlier reload with a tighter constraint.
5376 To solve this, we make two passes over the reloads, in the order
5377 described above. In the first pass we try to inherit a reload
5378 from a previous insn. If there is a later reload that needs a
5379 class that is a proper subset of the class being processed, we must
5380 also allocate a spill register during the first pass.
5382 Then make a second pass over the reloads to allocate any reloads
5383 that haven't been given registers yet. */
5385 for (j = 0; j < n_reloads; j++)
5387 int r = reload_order[j];
5388 rtx search_equiv = NULL_RTX;
5390 /* Ignore reloads that got marked inoperative. */
5391 if (rld[r].out == 0 && rld[r].in == 0
5392 && ! rld[r].secondary_p)
5393 continue;
5395 /* If find_reloads chose to use reload_in or reload_out as a reload
5396 register, we don't need to chose one. Otherwise, try even if it
5397 found one since we might save an insn if we find the value lying
5398 around.
5399 Try also when reload_in is a pseudo without a hard reg. */
5400 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5401 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5402 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5403 && GET_CODE (rld[r].in) != MEM
5404 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5405 continue;
5407 #if 0 /* No longer needed for correct operation.
5408 It might give better code, or might not; worth an experiment? */
5409 /* If this is an optional reload, we can't inherit from earlier insns
5410 until we are sure that any non-optional reloads have been allocated.
5411 The following code takes advantage of the fact that optional reloads
5412 are at the end of reload_order. */
5413 if (rld[r].optional != 0)
5414 for (i = 0; i < j; i++)
5415 if ((rld[reload_order[i]].out != 0
5416 || rld[reload_order[i]].in != 0
5417 || rld[reload_order[i]].secondary_p)
5418 && ! rld[reload_order[i]].optional
5419 && rld[reload_order[i]].reg_rtx == 0)
5420 allocate_reload_reg (chain, reload_order[i], 0);
5421 #endif
5423 /* First see if this pseudo is already available as reloaded
5424 for a previous insn. We cannot try to inherit for reloads
5425 that are smaller than the maximum number of registers needed
5426 for groups unless the register we would allocate cannot be used
5427 for the groups.
5429 We could check here to see if this is a secondary reload for
5430 an object that is already in a register of the desired class.
5431 This would avoid the need for the secondary reload register.
5432 But this is complex because we can't easily determine what
5433 objects might want to be loaded via this reload. So let a
5434 register be allocated here. In `emit_reload_insns' we suppress
5435 one of the loads in the case described above. */
5437 if (inheritance)
5439 int byte = 0;
5440 int regno = -1;
5441 enum machine_mode mode = VOIDmode;
5443 if (rld[r].in == 0)
5445 else if (GET_CODE (rld[r].in) == REG)
5447 regno = REGNO (rld[r].in);
5448 mode = GET_MODE (rld[r].in);
5450 else if (GET_CODE (rld[r].in_reg) == REG)
5452 regno = REGNO (rld[r].in_reg);
5453 mode = GET_MODE (rld[r].in_reg);
5455 else if (GET_CODE (rld[r].in_reg) == SUBREG
5456 && GET_CODE (SUBREG_REG (rld[r].in_reg)) == REG)
5458 byte = SUBREG_BYTE (rld[r].in_reg);
5459 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5460 if (regno < FIRST_PSEUDO_REGISTER)
5461 regno = subreg_regno (rld[r].in_reg);
5462 mode = GET_MODE (rld[r].in_reg);
5464 #ifdef AUTO_INC_DEC
5465 else if ((GET_CODE (rld[r].in_reg) == PRE_INC
5466 || GET_CODE (rld[r].in_reg) == PRE_DEC
5467 || GET_CODE (rld[r].in_reg) == POST_INC
5468 || GET_CODE (rld[r].in_reg) == POST_DEC)
5469 && GET_CODE (XEXP (rld[r].in_reg, 0)) == REG)
5471 regno = REGNO (XEXP (rld[r].in_reg, 0));
5472 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5473 rld[r].out = rld[r].in;
5475 #endif
5476 #if 0
5477 /* This won't work, since REGNO can be a pseudo reg number.
5478 Also, it takes much more hair to keep track of all the things
5479 that can invalidate an inherited reload of part of a pseudoreg. */
5480 else if (GET_CODE (rld[r].in) == SUBREG
5481 && GET_CODE (SUBREG_REG (rld[r].in)) == REG)
5482 regno = subreg_regno (rld[r].in);
5483 #endif
5485 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5487 enum reg_class class = rld[r].class, last_class;
5488 rtx last_reg = reg_last_reload_reg[regno];
5489 enum machine_mode need_mode;
5491 i = REGNO (last_reg);
5492 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
5493 last_class = REGNO_REG_CLASS (i);
5495 if (byte == 0)
5496 need_mode = mode;
5497 else
5498 need_mode
5499 = smallest_mode_for_size (GET_MODE_SIZE (mode) + byte,
5500 GET_MODE_CLASS (mode));
5502 if (
5503 #ifdef CANNOT_CHANGE_MODE_CLASS
5504 (!REG_CANNOT_CHANGE_MODE_P (i, GET_MODE (last_reg),
5505 need_mode)
5507 #endif
5508 (GET_MODE_SIZE (GET_MODE (last_reg))
5509 >= GET_MODE_SIZE (need_mode))
5510 #ifdef CANNOT_CHANGE_MODE_CLASS
5512 #endif
5513 && reg_reloaded_contents[i] == regno
5514 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
5515 && HARD_REGNO_MODE_OK (i, rld[r].mode)
5516 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5517 /* Even if we can't use this register as a reload
5518 register, we might use it for reload_override_in,
5519 if copying it to the desired class is cheap
5520 enough. */
5521 || ((REGISTER_MOVE_COST (mode, last_class, class)
5522 < MEMORY_MOVE_COST (mode, class, 1))
5523 #ifdef SECONDARY_INPUT_RELOAD_CLASS
5524 && (SECONDARY_INPUT_RELOAD_CLASS (class, mode,
5525 last_reg)
5526 == NO_REGS)
5527 #endif
5528 #ifdef SECONDARY_MEMORY_NEEDED
5529 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5530 mode)
5531 #endif
5534 && (rld[r].nregs == max_group_size
5535 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5537 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
5538 rld[r].when_needed, rld[r].in,
5539 const0_rtx, r, 1))
5541 /* If a group is needed, verify that all the subsequent
5542 registers still have their values intact. */
5543 int nr = HARD_REGNO_NREGS (i, rld[r].mode);
5544 int k;
5546 for (k = 1; k < nr; k++)
5547 if (reg_reloaded_contents[i + k] != regno
5548 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
5549 break;
5551 if (k == nr)
5553 int i1;
5554 int bad_for_class;
5556 last_reg = (GET_MODE (last_reg) == mode
5557 ? last_reg : gen_rtx_REG (mode, i));
5559 bad_for_class = 0;
5560 for (k = 0; k < nr; k++)
5561 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5562 i+k);
5564 /* We found a register that contains the
5565 value we need. If this register is the
5566 same as an `earlyclobber' operand of the
5567 current insn, just mark it as a place to
5568 reload from since we can't use it as the
5569 reload register itself. */
5571 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5572 if (reg_overlap_mentioned_for_reload_p
5573 (reg_last_reload_reg[regno],
5574 reload_earlyclobbers[i1]))
5575 break;
5577 if (i1 != n_earlyclobbers
5578 || ! (free_for_value_p (i, rld[r].mode,
5579 rld[r].opnum,
5580 rld[r].when_needed, rld[r].in,
5581 rld[r].out, r, 1))
5582 /* Don't use it if we'd clobber a pseudo reg. */
5583 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
5584 && rld[r].out
5585 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
5586 /* Don't clobber the frame pointer. */
5587 || (i == HARD_FRAME_POINTER_REGNUM
5588 && frame_pointer_needed
5589 && rld[r].out)
5590 /* Don't really use the inherited spill reg
5591 if we need it wider than we've got it. */
5592 || (GET_MODE_SIZE (rld[r].mode)
5593 > GET_MODE_SIZE (mode))
5594 || bad_for_class
5596 /* If find_reloads chose reload_out as reload
5597 register, stay with it - that leaves the
5598 inherited register for subsequent reloads. */
5599 || (rld[r].out && rld[r].reg_rtx
5600 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
5602 if (! rld[r].optional)
5604 reload_override_in[r] = last_reg;
5605 reload_inheritance_insn[r]
5606 = reg_reloaded_insn[i];
5609 else
5611 int k;
5612 /* We can use this as a reload reg. */
5613 /* Mark the register as in use for this part of
5614 the insn. */
5615 mark_reload_reg_in_use (i,
5616 rld[r].opnum,
5617 rld[r].when_needed,
5618 rld[r].mode);
5619 rld[r].reg_rtx = last_reg;
5620 reload_inherited[r] = 1;
5621 reload_inheritance_insn[r]
5622 = reg_reloaded_insn[i];
5623 reload_spill_index[r] = i;
5624 for (k = 0; k < nr; k++)
5625 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5626 i + k);
5633 /* Here's another way to see if the value is already lying around. */
5634 if (inheritance
5635 && rld[r].in != 0
5636 && ! reload_inherited[r]
5637 && rld[r].out == 0
5638 && (CONSTANT_P (rld[r].in)
5639 || GET_CODE (rld[r].in) == PLUS
5640 || GET_CODE (rld[r].in) == REG
5641 || GET_CODE (rld[r].in) == MEM)
5642 && (rld[r].nregs == max_group_size
5643 || ! reg_classes_intersect_p (rld[r].class, group_class)))
5644 search_equiv = rld[r].in;
5645 /* If this is an output reload from a simple move insn, look
5646 if an equivalence for the input is available. */
5647 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
5649 rtx set = single_set (insn);
5651 if (set
5652 && rtx_equal_p (rld[r].out, SET_DEST (set))
5653 && CONSTANT_P (SET_SRC (set)))
5654 search_equiv = SET_SRC (set);
5657 if (search_equiv)
5659 rtx equiv
5660 = find_equiv_reg (search_equiv, insn, rld[r].class,
5661 -1, NULL, 0, rld[r].mode);
5662 int regno = 0;
5664 if (equiv != 0)
5666 if (GET_CODE (equiv) == REG)
5667 regno = REGNO (equiv);
5668 else if (GET_CODE (equiv) == SUBREG)
5670 /* This must be a SUBREG of a hard register.
5671 Make a new REG since this might be used in an
5672 address and not all machines support SUBREGs
5673 there. */
5674 regno = subreg_regno (equiv);
5675 equiv = gen_rtx_REG (rld[r].mode, regno);
5677 else
5678 abort ();
5681 /* If we found a spill reg, reject it unless it is free
5682 and of the desired class. */
5683 if (equiv != 0
5684 && ((TEST_HARD_REG_BIT (reload_reg_used_at_all, regno)
5685 && ! free_for_value_p (regno, rld[r].mode,
5686 rld[r].opnum, rld[r].when_needed,
5687 rld[r].in, rld[r].out, r, 1))
5688 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5689 regno)))
5690 equiv = 0;
5692 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
5693 equiv = 0;
5695 /* We found a register that contains the value we need.
5696 If this register is the same as an `earlyclobber' operand
5697 of the current insn, just mark it as a place to reload from
5698 since we can't use it as the reload register itself. */
5700 if (equiv != 0)
5701 for (i = 0; i < n_earlyclobbers; i++)
5702 if (reg_overlap_mentioned_for_reload_p (equiv,
5703 reload_earlyclobbers[i]))
5705 if (! rld[r].optional)
5706 reload_override_in[r] = equiv;
5707 equiv = 0;
5708 break;
5711 /* If the equiv register we have found is explicitly clobbered
5712 in the current insn, it depends on the reload type if we
5713 can use it, use it for reload_override_in, or not at all.
5714 In particular, we then can't use EQUIV for a
5715 RELOAD_FOR_OUTPUT_ADDRESS reload. */
5717 if (equiv != 0)
5719 if (regno_clobbered_p (regno, insn, rld[r].mode, 0))
5720 switch (rld[r].when_needed)
5722 case RELOAD_FOR_OTHER_ADDRESS:
5723 case RELOAD_FOR_INPADDR_ADDRESS:
5724 case RELOAD_FOR_INPUT_ADDRESS:
5725 case RELOAD_FOR_OPADDR_ADDR:
5726 break;
5727 case RELOAD_OTHER:
5728 case RELOAD_FOR_INPUT:
5729 case RELOAD_FOR_OPERAND_ADDRESS:
5730 if (! rld[r].optional)
5731 reload_override_in[r] = equiv;
5732 /* Fall through. */
5733 default:
5734 equiv = 0;
5735 break;
5737 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
5738 switch (rld[r].when_needed)
5740 case RELOAD_FOR_OTHER_ADDRESS:
5741 case RELOAD_FOR_INPADDR_ADDRESS:
5742 case RELOAD_FOR_INPUT_ADDRESS:
5743 case RELOAD_FOR_OPADDR_ADDR:
5744 case RELOAD_FOR_OPERAND_ADDRESS:
5745 case RELOAD_FOR_INPUT:
5746 break;
5747 case RELOAD_OTHER:
5748 if (! rld[r].optional)
5749 reload_override_in[r] = equiv;
5750 /* Fall through. */
5751 default:
5752 equiv = 0;
5753 break;
5757 /* If we found an equivalent reg, say no code need be generated
5758 to load it, and use it as our reload reg. */
5759 if (equiv != 0
5760 && (regno != HARD_FRAME_POINTER_REGNUM
5761 || !frame_pointer_needed))
5763 int nr = HARD_REGNO_NREGS (regno, rld[r].mode);
5764 int k;
5765 rld[r].reg_rtx = equiv;
5766 reload_inherited[r] = 1;
5768 /* If reg_reloaded_valid is not set for this register,
5769 there might be a stale spill_reg_store lying around.
5770 We must clear it, since otherwise emit_reload_insns
5771 might delete the store. */
5772 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
5773 spill_reg_store[regno] = NULL_RTX;
5774 /* If any of the hard registers in EQUIV are spill
5775 registers, mark them as in use for this insn. */
5776 for (k = 0; k < nr; k++)
5778 i = spill_reg_order[regno + k];
5779 if (i >= 0)
5781 mark_reload_reg_in_use (regno, rld[r].opnum,
5782 rld[r].when_needed,
5783 rld[r].mode);
5784 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5785 regno + k);
5791 /* If we found a register to use already, or if this is an optional
5792 reload, we are done. */
5793 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
5794 continue;
5796 #if 0
5797 /* No longer needed for correct operation. Might or might
5798 not give better code on the average. Want to experiment? */
5800 /* See if there is a later reload that has a class different from our
5801 class that intersects our class or that requires less register
5802 than our reload. If so, we must allocate a register to this
5803 reload now, since that reload might inherit a previous reload
5804 and take the only available register in our class. Don't do this
5805 for optional reloads since they will force all previous reloads
5806 to be allocated. Also don't do this for reloads that have been
5807 turned off. */
5809 for (i = j + 1; i < n_reloads; i++)
5811 int s = reload_order[i];
5813 if ((rld[s].in == 0 && rld[s].out == 0
5814 && ! rld[s].secondary_p)
5815 || rld[s].optional)
5816 continue;
5818 if ((rld[s].class != rld[r].class
5819 && reg_classes_intersect_p (rld[r].class,
5820 rld[s].class))
5821 || rld[s].nregs < rld[r].nregs)
5822 break;
5825 if (i == n_reloads)
5826 continue;
5828 allocate_reload_reg (chain, r, j == n_reloads - 1);
5829 #endif
5832 /* Now allocate reload registers for anything non-optional that
5833 didn't get one yet. */
5834 for (j = 0; j < n_reloads; j++)
5836 int r = reload_order[j];
5838 /* Ignore reloads that got marked inoperative. */
5839 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
5840 continue;
5842 /* Skip reloads that already have a register allocated or are
5843 optional. */
5844 if (rld[r].reg_rtx != 0 || rld[r].optional)
5845 continue;
5847 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
5848 break;
5851 /* If that loop got all the way, we have won. */
5852 if (j == n_reloads)
5854 win = 1;
5855 break;
5858 /* Loop around and try without any inheritance. */
5861 if (! win)
5863 /* First undo everything done by the failed attempt
5864 to allocate with inheritance. */
5865 choose_reload_regs_init (chain, save_reload_reg_rtx);
5867 /* Some sanity tests to verify that the reloads found in the first
5868 pass are identical to the ones we have now. */
5869 if (chain->n_reloads != n_reloads)
5870 abort ();
5872 for (i = 0; i < n_reloads; i++)
5874 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
5875 continue;
5876 if (chain->rld[i].when_needed != rld[i].when_needed)
5877 abort ();
5878 for (j = 0; j < n_spills; j++)
5879 if (spill_regs[j] == chain->rld[i].regno)
5880 if (! set_reload_reg (j, i))
5881 failed_reload (chain->insn, i);
5885 /* If we thought we could inherit a reload, because it seemed that
5886 nothing else wanted the same reload register earlier in the insn,
5887 verify that assumption, now that all reloads have been assigned.
5888 Likewise for reloads where reload_override_in has been set. */
5890 /* If doing expensive optimizations, do one preliminary pass that doesn't
5891 cancel any inheritance, but removes reloads that have been needed only
5892 for reloads that we know can be inherited. */
5893 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
5895 for (j = 0; j < n_reloads; j++)
5897 int r = reload_order[j];
5898 rtx check_reg;
5899 if (reload_inherited[r] && rld[r].reg_rtx)
5900 check_reg = rld[r].reg_rtx;
5901 else if (reload_override_in[r]
5902 && (GET_CODE (reload_override_in[r]) == REG
5903 || GET_CODE (reload_override_in[r]) == SUBREG))
5904 check_reg = reload_override_in[r];
5905 else
5906 continue;
5907 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
5908 rld[r].opnum, rld[r].when_needed, rld[r].in,
5909 (reload_inherited[r]
5910 ? rld[r].out : const0_rtx),
5911 r, 1))
5913 if (pass)
5914 continue;
5915 reload_inherited[r] = 0;
5916 reload_override_in[r] = 0;
5918 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
5919 reload_override_in, then we do not need its related
5920 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
5921 likewise for other reload types.
5922 We handle this by removing a reload when its only replacement
5923 is mentioned in reload_in of the reload we are going to inherit.
5924 A special case are auto_inc expressions; even if the input is
5925 inherited, we still need the address for the output. We can
5926 recognize them because they have RELOAD_OUT set to RELOAD_IN.
5927 If we succeeded removing some reload and we are doing a preliminary
5928 pass just to remove such reloads, make another pass, since the
5929 removal of one reload might allow us to inherit another one. */
5930 else if (rld[r].in
5931 && rld[r].out != rld[r].in
5932 && remove_address_replacements (rld[r].in) && pass)
5933 pass = 2;
5937 /* Now that reload_override_in is known valid,
5938 actually override reload_in. */
5939 for (j = 0; j < n_reloads; j++)
5940 if (reload_override_in[j])
5941 rld[j].in = reload_override_in[j];
5943 /* If this reload won't be done because it has been canceled or is
5944 optional and not inherited, clear reload_reg_rtx so other
5945 routines (such as subst_reloads) don't get confused. */
5946 for (j = 0; j < n_reloads; j++)
5947 if (rld[j].reg_rtx != 0
5948 && ((rld[j].optional && ! reload_inherited[j])
5949 || (rld[j].in == 0 && rld[j].out == 0
5950 && ! rld[j].secondary_p)))
5952 int regno = true_regnum (rld[j].reg_rtx);
5954 if (spill_reg_order[regno] >= 0)
5955 clear_reload_reg_in_use (regno, rld[j].opnum,
5956 rld[j].when_needed, rld[j].mode);
5957 rld[j].reg_rtx = 0;
5958 reload_spill_index[j] = -1;
5961 /* Record which pseudos and which spill regs have output reloads. */
5962 for (j = 0; j < n_reloads; j++)
5964 int r = reload_order[j];
5966 i = reload_spill_index[r];
5968 /* I is nonneg if this reload uses a register.
5969 If rld[r].reg_rtx is 0, this is an optional reload
5970 that we opted to ignore. */
5971 if (rld[r].out_reg != 0 && GET_CODE (rld[r].out_reg) == REG
5972 && rld[r].reg_rtx != 0)
5974 int nregno = REGNO (rld[r].out_reg);
5975 int nr = 1;
5977 if (nregno < FIRST_PSEUDO_REGISTER)
5978 nr = HARD_REGNO_NREGS (nregno, rld[r].mode);
5980 while (--nr >= 0)
5981 reg_has_output_reload[nregno + nr] = 1;
5983 if (i >= 0)
5985 nr = HARD_REGNO_NREGS (i, rld[r].mode);
5986 while (--nr >= 0)
5987 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
5990 if (rld[r].when_needed != RELOAD_OTHER
5991 && rld[r].when_needed != RELOAD_FOR_OUTPUT
5992 && rld[r].when_needed != RELOAD_FOR_INSN)
5993 abort ();
5998 /* Deallocate the reload register for reload R. This is called from
5999 remove_address_replacements. */
6001 void
6002 deallocate_reload_reg (r)
6003 int r;
6005 int regno;
6007 if (! rld[r].reg_rtx)
6008 return;
6009 regno = true_regnum (rld[r].reg_rtx);
6010 rld[r].reg_rtx = 0;
6011 if (spill_reg_order[regno] >= 0)
6012 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
6013 rld[r].mode);
6014 reload_spill_index[r] = -1;
6017 /* If SMALL_REGISTER_CLASSES is nonzero, we may not have merged two
6018 reloads of the same item for fear that we might not have enough reload
6019 registers. However, normally they will get the same reload register
6020 and hence actually need not be loaded twice.
6022 Here we check for the most common case of this phenomenon: when we have
6023 a number of reloads for the same object, each of which were allocated
6024 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6025 reload, and is not modified in the insn itself. If we find such,
6026 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6027 This will not increase the number of spill registers needed and will
6028 prevent redundant code. */
6030 static void
6031 merge_assigned_reloads (insn)
6032 rtx insn;
6034 int i, j;
6036 /* Scan all the reloads looking for ones that only load values and
6037 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6038 assigned and not modified by INSN. */
6040 for (i = 0; i < n_reloads; i++)
6042 int conflicting_input = 0;
6043 int max_input_address_opnum = -1;
6044 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6046 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6047 || rld[i].out != 0 || rld[i].reg_rtx == 0
6048 || reg_set_p (rld[i].reg_rtx, insn))
6049 continue;
6051 /* Look at all other reloads. Ensure that the only use of this
6052 reload_reg_rtx is in a reload that just loads the same value
6053 as we do. Note that any secondary reloads must be of the identical
6054 class since the values, modes, and result registers are the
6055 same, so we need not do anything with any secondary reloads. */
6057 for (j = 0; j < n_reloads; j++)
6059 if (i == j || rld[j].reg_rtx == 0
6060 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6061 rld[i].reg_rtx))
6062 continue;
6064 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6065 && rld[j].opnum > max_input_address_opnum)
6066 max_input_address_opnum = rld[j].opnum;
6068 /* If the reload regs aren't exactly the same (e.g, different modes)
6069 or if the values are different, we can't merge this reload.
6070 But if it is an input reload, we might still merge
6071 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6073 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6074 || rld[j].out != 0 || rld[j].in == 0
6075 || ! rtx_equal_p (rld[i].in, rld[j].in))
6077 if (rld[j].when_needed != RELOAD_FOR_INPUT
6078 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6079 || rld[i].opnum > rld[j].opnum)
6080 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6081 break;
6082 conflicting_input = 1;
6083 if (min_conflicting_input_opnum > rld[j].opnum)
6084 min_conflicting_input_opnum = rld[j].opnum;
6088 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6089 we, in fact, found any matching reloads. */
6091 if (j == n_reloads
6092 && max_input_address_opnum <= min_conflicting_input_opnum)
6094 for (j = 0; j < n_reloads; j++)
6095 if (i != j && rld[j].reg_rtx != 0
6096 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6097 && (! conflicting_input
6098 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6099 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6101 rld[i].when_needed = RELOAD_OTHER;
6102 rld[j].in = 0;
6103 reload_spill_index[j] = -1;
6104 transfer_replacements (i, j);
6107 /* If this is now RELOAD_OTHER, look for any reloads that load
6108 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6109 if they were for inputs, RELOAD_OTHER for outputs. Note that
6110 this test is equivalent to looking for reloads for this operand
6111 number. */
6112 /* We must take special care when there are two or more reloads to
6113 be merged and a RELOAD_FOR_OUTPUT_ADDRESS reload that loads the
6114 same value or a part of it; we must not change its type if there
6115 is a conflicting input. */
6117 if (rld[i].when_needed == RELOAD_OTHER)
6118 for (j = 0; j < n_reloads; j++)
6119 if (rld[j].in != 0
6120 && rld[j].when_needed != RELOAD_OTHER
6121 && rld[j].when_needed != RELOAD_FOR_OTHER_ADDRESS
6122 && (! conflicting_input
6123 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6124 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6125 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6126 rld[i].in))
6128 int k;
6130 rld[j].when_needed
6131 = ((rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6132 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6133 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6135 /* Check to see if we accidentally converted two reloads
6136 that use the same reload register to the same type.
6137 If so, the resulting code won't work, so abort. */
6138 if (rld[j].reg_rtx)
6139 for (k = 0; k < j; k++)
6140 if (rld[k].in != 0 && rld[k].reg_rtx != 0
6141 && rld[k].when_needed == rld[j].when_needed
6142 && rtx_equal_p (rld[k].reg_rtx, rld[j].reg_rtx))
6143 abort ();
6149 /* These arrays are filled by emit_reload_insns and its subroutines. */
6150 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
6151 static rtx other_input_address_reload_insns = 0;
6152 static rtx other_input_reload_insns = 0;
6153 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6154 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6155 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
6156 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6157 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6158 static rtx operand_reload_insns = 0;
6159 static rtx other_operand_reload_insns = 0;
6160 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6162 /* Values to be put in spill_reg_store are put here first. */
6163 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6164 static HARD_REG_SET reg_reloaded_died;
6166 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
6167 has the number J. OLD contains the value to be used as input. */
6169 static void
6170 emit_input_reload_insns (chain, rl, old, j)
6171 struct insn_chain *chain;
6172 struct reload *rl;
6173 rtx old;
6174 int j;
6176 rtx insn = chain->insn;
6177 rtx reloadreg = rl->reg_rtx;
6178 rtx oldequiv_reg = 0;
6179 rtx oldequiv = 0;
6180 int special = 0;
6181 enum machine_mode mode;
6182 rtx *where;
6184 /* Determine the mode to reload in.
6185 This is very tricky because we have three to choose from.
6186 There is the mode the insn operand wants (rl->inmode).
6187 There is the mode of the reload register RELOADREG.
6188 There is the intrinsic mode of the operand, which we could find
6189 by stripping some SUBREGs.
6190 It turns out that RELOADREG's mode is irrelevant:
6191 we can change that arbitrarily.
6193 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6194 then the reload reg may not support QImode moves, so use SImode.
6195 If foo is in memory due to spilling a pseudo reg, this is safe,
6196 because the QImode value is in the least significant part of a
6197 slot big enough for a SImode. If foo is some other sort of
6198 memory reference, then it is impossible to reload this case,
6199 so previous passes had better make sure this never happens.
6201 Then consider a one-word union which has SImode and one of its
6202 members is a float, being fetched as (SUBREG:SF union:SI).
6203 We must fetch that as SFmode because we could be loading into
6204 a float-only register. In this case OLD's mode is correct.
6206 Consider an immediate integer: it has VOIDmode. Here we need
6207 to get a mode from something else.
6209 In some cases, there is a fourth mode, the operand's
6210 containing mode. If the insn specifies a containing mode for
6211 this operand, it overrides all others.
6213 I am not sure whether the algorithm here is always right,
6214 but it does the right things in those cases. */
6216 mode = GET_MODE (old);
6217 if (mode == VOIDmode)
6218 mode = rl->inmode;
6220 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6221 /* If we need a secondary register for this operation, see if
6222 the value is already in a register in that class. Don't
6223 do this if the secondary register will be used as a scratch
6224 register. */
6226 if (rl->secondary_in_reload >= 0
6227 && rl->secondary_in_icode == CODE_FOR_nothing
6228 && optimize)
6229 oldequiv
6230 = find_equiv_reg (old, insn,
6231 rld[rl->secondary_in_reload].class,
6232 -1, NULL, 0, mode);
6233 #endif
6235 /* If reloading from memory, see if there is a register
6236 that already holds the same value. If so, reload from there.
6237 We can pass 0 as the reload_reg_p argument because
6238 any other reload has either already been emitted,
6239 in which case find_equiv_reg will see the reload-insn,
6240 or has yet to be emitted, in which case it doesn't matter
6241 because we will use this equiv reg right away. */
6243 if (oldequiv == 0 && optimize
6244 && (GET_CODE (old) == MEM
6245 || (GET_CODE (old) == REG
6246 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6247 && reg_renumber[REGNO (old)] < 0)))
6248 oldequiv = find_equiv_reg (old, insn, ALL_REGS, -1, NULL, 0, mode);
6250 if (oldequiv)
6252 unsigned int regno = true_regnum (oldequiv);
6254 /* Don't use OLDEQUIV if any other reload changes it at an
6255 earlier stage of this insn or at this stage. */
6256 if (! free_for_value_p (regno, rl->mode, rl->opnum, rl->when_needed,
6257 rl->in, const0_rtx, j, 0))
6258 oldequiv = 0;
6260 /* If it is no cheaper to copy from OLDEQUIV into the
6261 reload register than it would be to move from memory,
6262 don't use it. Likewise, if we need a secondary register
6263 or memory. */
6265 if (oldequiv != 0
6266 && (((enum reg_class) REGNO_REG_CLASS (regno) != rl->class
6267 && (REGISTER_MOVE_COST (mode, REGNO_REG_CLASS (regno),
6268 rl->class)
6269 >= MEMORY_MOVE_COST (mode, rl->class, 1)))
6270 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6271 || (SECONDARY_INPUT_RELOAD_CLASS (rl->class,
6272 mode, oldequiv)
6273 != NO_REGS)
6274 #endif
6275 #ifdef SECONDARY_MEMORY_NEEDED
6276 || SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (regno),
6277 rl->class,
6278 mode)
6279 #endif
6281 oldequiv = 0;
6284 /* delete_output_reload is only invoked properly if old contains
6285 the original pseudo register. Since this is replaced with a
6286 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6287 find the pseudo in RELOAD_IN_REG. */
6288 if (oldequiv == 0
6289 && reload_override_in[j]
6290 && GET_CODE (rl->in_reg) == REG)
6292 oldequiv = old;
6293 old = rl->in_reg;
6295 if (oldequiv == 0)
6296 oldequiv = old;
6297 else if (GET_CODE (oldequiv) == REG)
6298 oldequiv_reg = oldequiv;
6299 else if (GET_CODE (oldequiv) == SUBREG)
6300 oldequiv_reg = SUBREG_REG (oldequiv);
6302 /* If we are reloading from a register that was recently stored in
6303 with an output-reload, see if we can prove there was
6304 actually no need to store the old value in it. */
6306 if (optimize && GET_CODE (oldequiv) == REG
6307 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6308 && spill_reg_store[REGNO (oldequiv)]
6309 && GET_CODE (old) == REG
6310 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6311 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6312 rl->out_reg)))
6313 delete_output_reload (insn, j, REGNO (oldequiv));
6315 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6316 then load RELOADREG from OLDEQUIV. Note that we cannot use
6317 gen_lowpart_common since it can do the wrong thing when
6318 RELOADREG has a multi-word mode. Note that RELOADREG
6319 must always be a REG here. */
6321 if (GET_MODE (reloadreg) != mode)
6322 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6323 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6324 oldequiv = SUBREG_REG (oldequiv);
6325 if (GET_MODE (oldequiv) != VOIDmode
6326 && mode != GET_MODE (oldequiv))
6327 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
6329 /* Switch to the right place to emit the reload insns. */
6330 switch (rl->when_needed)
6332 case RELOAD_OTHER:
6333 where = &other_input_reload_insns;
6334 break;
6335 case RELOAD_FOR_INPUT:
6336 where = &input_reload_insns[rl->opnum];
6337 break;
6338 case RELOAD_FOR_INPUT_ADDRESS:
6339 where = &input_address_reload_insns[rl->opnum];
6340 break;
6341 case RELOAD_FOR_INPADDR_ADDRESS:
6342 where = &inpaddr_address_reload_insns[rl->opnum];
6343 break;
6344 case RELOAD_FOR_OUTPUT_ADDRESS:
6345 where = &output_address_reload_insns[rl->opnum];
6346 break;
6347 case RELOAD_FOR_OUTADDR_ADDRESS:
6348 where = &outaddr_address_reload_insns[rl->opnum];
6349 break;
6350 case RELOAD_FOR_OPERAND_ADDRESS:
6351 where = &operand_reload_insns;
6352 break;
6353 case RELOAD_FOR_OPADDR_ADDR:
6354 where = &other_operand_reload_insns;
6355 break;
6356 case RELOAD_FOR_OTHER_ADDRESS:
6357 where = &other_input_address_reload_insns;
6358 break;
6359 default:
6360 abort ();
6363 push_to_sequence (*where);
6365 /* Auto-increment addresses must be reloaded in a special way. */
6366 if (rl->out && ! rl->out_reg)
6368 /* We are not going to bother supporting the case where a
6369 incremented register can't be copied directly from
6370 OLDEQUIV since this seems highly unlikely. */
6371 if (rl->secondary_in_reload >= 0)
6372 abort ();
6374 if (reload_inherited[j])
6375 oldequiv = reloadreg;
6377 old = XEXP (rl->in_reg, 0);
6379 if (optimize && GET_CODE (oldequiv) == REG
6380 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6381 && spill_reg_store[REGNO (oldequiv)]
6382 && GET_CODE (old) == REG
6383 && (dead_or_set_p (insn,
6384 spill_reg_stored_to[REGNO (oldequiv)])
6385 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6386 old)))
6387 delete_output_reload (insn, j, REGNO (oldequiv));
6389 /* Prevent normal processing of this reload. */
6390 special = 1;
6391 /* Output a special code sequence for this case. */
6392 new_spill_reg_store[REGNO (reloadreg)]
6393 = inc_for_reload (reloadreg, oldequiv, rl->out,
6394 rl->inc);
6397 /* If we are reloading a pseudo-register that was set by the previous
6398 insn, see if we can get rid of that pseudo-register entirely
6399 by redirecting the previous insn into our reload register. */
6401 else if (optimize && GET_CODE (old) == REG
6402 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6403 && dead_or_set_p (insn, old)
6404 /* This is unsafe if some other reload
6405 uses the same reg first. */
6406 && ! conflicts_with_override (reloadreg)
6407 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
6408 rl->when_needed, old, rl->out, j, 0))
6410 rtx temp = PREV_INSN (insn);
6411 while (temp && GET_CODE (temp) == NOTE)
6412 temp = PREV_INSN (temp);
6413 if (temp
6414 && GET_CODE (temp) == INSN
6415 && GET_CODE (PATTERN (temp)) == SET
6416 && SET_DEST (PATTERN (temp)) == old
6417 /* Make sure we can access insn_operand_constraint. */
6418 && asm_noperands (PATTERN (temp)) < 0
6419 /* This is unsafe if operand occurs more than once in current
6420 insn. Perhaps some occurrences aren't reloaded. */
6421 && count_occurrences (PATTERN (insn), old, 0) == 1)
6423 rtx old = SET_DEST (PATTERN (temp));
6424 /* Store into the reload register instead of the pseudo. */
6425 SET_DEST (PATTERN (temp)) = reloadreg;
6427 /* Verify that resulting insn is valid. */
6428 extract_insn (temp);
6429 if (constrain_operands (1))
6431 /* If the previous insn is an output reload, the source is
6432 a reload register, and its spill_reg_store entry will
6433 contain the previous destination. This is now
6434 invalid. */
6435 if (GET_CODE (SET_SRC (PATTERN (temp))) == REG
6436 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6438 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6439 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6442 /* If these are the only uses of the pseudo reg,
6443 pretend for GDB it lives in the reload reg we used. */
6444 if (REG_N_DEATHS (REGNO (old)) == 1
6445 && REG_N_SETS (REGNO (old)) == 1)
6447 reg_renumber[REGNO (old)] = REGNO (rl->reg_rtx);
6448 alter_reg (REGNO (old), -1);
6450 special = 1;
6452 else
6454 SET_DEST (PATTERN (temp)) = old;
6459 /* We can't do that, so output an insn to load RELOADREG. */
6461 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6462 /* If we have a secondary reload, pick up the secondary register
6463 and icode, if any. If OLDEQUIV and OLD are different or
6464 if this is an in-out reload, recompute whether or not we
6465 still need a secondary register and what the icode should
6466 be. If we still need a secondary register and the class or
6467 icode is different, go back to reloading from OLD if using
6468 OLDEQUIV means that we got the wrong type of register. We
6469 cannot have different class or icode due to an in-out reload
6470 because we don't make such reloads when both the input and
6471 output need secondary reload registers. */
6473 if (! special && rl->secondary_in_reload >= 0)
6475 rtx second_reload_reg = 0;
6476 int secondary_reload = rl->secondary_in_reload;
6477 rtx real_oldequiv = oldequiv;
6478 rtx real_old = old;
6479 rtx tmp;
6480 enum insn_code icode;
6482 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6483 and similarly for OLD.
6484 See comments in get_secondary_reload in reload.c. */
6485 /* If it is a pseudo that cannot be replaced with its
6486 equivalent MEM, we must fall back to reload_in, which
6487 will have all the necessary substitutions registered.
6488 Likewise for a pseudo that can't be replaced with its
6489 equivalent constant.
6491 Take extra care for subregs of such pseudos. Note that
6492 we cannot use reg_equiv_mem in this case because it is
6493 not in the right mode. */
6495 tmp = oldequiv;
6496 if (GET_CODE (tmp) == SUBREG)
6497 tmp = SUBREG_REG (tmp);
6498 if (GET_CODE (tmp) == REG
6499 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6500 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6501 || reg_equiv_constant[REGNO (tmp)] != 0))
6503 if (! reg_equiv_mem[REGNO (tmp)]
6504 || num_not_at_initial_offset
6505 || GET_CODE (oldequiv) == SUBREG)
6506 real_oldequiv = rl->in;
6507 else
6508 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6511 tmp = old;
6512 if (GET_CODE (tmp) == SUBREG)
6513 tmp = SUBREG_REG (tmp);
6514 if (GET_CODE (tmp) == REG
6515 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6516 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6517 || reg_equiv_constant[REGNO (tmp)] != 0))
6519 if (! reg_equiv_mem[REGNO (tmp)]
6520 || num_not_at_initial_offset
6521 || GET_CODE (old) == SUBREG)
6522 real_old = rl->in;
6523 else
6524 real_old = reg_equiv_mem[REGNO (tmp)];
6527 second_reload_reg = rld[secondary_reload].reg_rtx;
6528 icode = rl->secondary_in_icode;
6530 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6531 || (rl->in != 0 && rl->out != 0))
6533 enum reg_class new_class
6534 = SECONDARY_INPUT_RELOAD_CLASS (rl->class,
6535 mode, real_oldequiv);
6537 if (new_class == NO_REGS)
6538 second_reload_reg = 0;
6539 else
6541 enum insn_code new_icode;
6542 enum machine_mode new_mode;
6544 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
6545 REGNO (second_reload_reg)))
6546 oldequiv = old, real_oldequiv = real_old;
6547 else
6549 new_icode = reload_in_optab[(int) mode];
6550 if (new_icode != CODE_FOR_nothing
6551 && ((insn_data[(int) new_icode].operand[0].predicate
6552 && ! ((*insn_data[(int) new_icode].operand[0].predicate)
6553 (reloadreg, mode)))
6554 || (insn_data[(int) new_icode].operand[1].predicate
6555 && ! ((*insn_data[(int) new_icode].operand[1].predicate)
6556 (real_oldequiv, mode)))))
6557 new_icode = CODE_FOR_nothing;
6559 if (new_icode == CODE_FOR_nothing)
6560 new_mode = mode;
6561 else
6562 new_mode = insn_data[(int) new_icode].operand[2].mode;
6564 if (GET_MODE (second_reload_reg) != new_mode)
6566 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6567 new_mode))
6568 oldequiv = old, real_oldequiv = real_old;
6569 else
6570 second_reload_reg
6571 = gen_rtx_REG (new_mode,
6572 REGNO (second_reload_reg));
6578 /* If we still need a secondary reload register, check
6579 to see if it is being used as a scratch or intermediate
6580 register and generate code appropriately. If we need
6581 a scratch register, use REAL_OLDEQUIV since the form of
6582 the insn may depend on the actual address if it is
6583 a MEM. */
6585 if (second_reload_reg)
6587 if (icode != CODE_FOR_nothing)
6589 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6590 second_reload_reg));
6591 special = 1;
6593 else
6595 /* See if we need a scratch register to load the
6596 intermediate register (a tertiary reload). */
6597 enum insn_code tertiary_icode
6598 = rld[secondary_reload].secondary_in_icode;
6600 if (tertiary_icode != CODE_FOR_nothing)
6602 rtx third_reload_reg
6603 = rld[rld[secondary_reload].secondary_in_reload].reg_rtx;
6605 emit_insn ((GEN_FCN (tertiary_icode)
6606 (second_reload_reg, real_oldequiv,
6607 third_reload_reg)));
6609 else
6610 gen_reload (second_reload_reg, real_oldequiv,
6611 rl->opnum,
6612 rl->when_needed);
6614 oldequiv = second_reload_reg;
6618 #endif
6620 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
6622 rtx real_oldequiv = oldequiv;
6624 if ((GET_CODE (oldequiv) == REG
6625 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
6626 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
6627 || reg_equiv_constant[REGNO (oldequiv)] != 0))
6628 || (GET_CODE (oldequiv) == SUBREG
6629 && GET_CODE (SUBREG_REG (oldequiv)) == REG
6630 && (REGNO (SUBREG_REG (oldequiv))
6631 >= FIRST_PSEUDO_REGISTER)
6632 && ((reg_equiv_memory_loc
6633 [REGNO (SUBREG_REG (oldequiv))] != 0)
6634 || (reg_equiv_constant
6635 [REGNO (SUBREG_REG (oldequiv))] != 0)))
6636 || (CONSTANT_P (oldequiv)
6637 && (PREFERRED_RELOAD_CLASS (oldequiv,
6638 REGNO_REG_CLASS (REGNO (reloadreg)))
6639 == NO_REGS)))
6640 real_oldequiv = rl->in;
6641 gen_reload (reloadreg, real_oldequiv, rl->opnum,
6642 rl->when_needed);
6645 if (flag_non_call_exceptions)
6646 copy_eh_notes (insn, get_insns ());
6648 /* End this sequence. */
6649 *where = get_insns ();
6650 end_sequence ();
6652 /* Update reload_override_in so that delete_address_reloads_1
6653 can see the actual register usage. */
6654 if (oldequiv_reg)
6655 reload_override_in[j] = oldequiv;
6658 /* Generate insns to for the output reload RL, which is for the insn described
6659 by CHAIN and has the number J. */
6660 static void
6661 emit_output_reload_insns (chain, rl, j)
6662 struct insn_chain *chain;
6663 struct reload *rl;
6664 int j;
6666 rtx reloadreg = rl->reg_rtx;
6667 rtx insn = chain->insn;
6668 int special = 0;
6669 rtx old = rl->out;
6670 enum machine_mode mode = GET_MODE (old);
6671 rtx p;
6673 if (rl->when_needed == RELOAD_OTHER)
6674 start_sequence ();
6675 else
6676 push_to_sequence (output_reload_insns[rl->opnum]);
6678 /* Determine the mode to reload in.
6679 See comments above (for input reloading). */
6681 if (mode == VOIDmode)
6683 /* VOIDmode should never happen for an output. */
6684 if (asm_noperands (PATTERN (insn)) < 0)
6685 /* It's the compiler's fault. */
6686 fatal_insn ("VOIDmode on an output", insn);
6687 error_for_asm (insn, "output operand is constant in `asm'");
6688 /* Prevent crash--use something we know is valid. */
6689 mode = word_mode;
6690 old = gen_rtx_REG (mode, REGNO (reloadreg));
6693 if (GET_MODE (reloadreg) != mode)
6694 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6696 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
6698 /* If we need two reload regs, set RELOADREG to the intermediate
6699 one, since it will be stored into OLD. We might need a secondary
6700 register only for an input reload, so check again here. */
6702 if (rl->secondary_out_reload >= 0)
6704 rtx real_old = old;
6706 if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
6707 && reg_equiv_mem[REGNO (old)] != 0)
6708 real_old = reg_equiv_mem[REGNO (old)];
6710 if ((SECONDARY_OUTPUT_RELOAD_CLASS (rl->class,
6711 mode, real_old)
6712 != NO_REGS))
6714 rtx second_reloadreg = reloadreg;
6715 reloadreg = rld[rl->secondary_out_reload].reg_rtx;
6717 /* See if RELOADREG is to be used as a scratch register
6718 or as an intermediate register. */
6719 if (rl->secondary_out_icode != CODE_FOR_nothing)
6721 emit_insn ((GEN_FCN (rl->secondary_out_icode)
6722 (real_old, second_reloadreg, reloadreg)));
6723 special = 1;
6725 else
6727 /* See if we need both a scratch and intermediate reload
6728 register. */
6730 int secondary_reload = rl->secondary_out_reload;
6731 enum insn_code tertiary_icode
6732 = rld[secondary_reload].secondary_out_icode;
6734 if (GET_MODE (reloadreg) != mode)
6735 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6737 if (tertiary_icode != CODE_FOR_nothing)
6739 rtx third_reloadreg
6740 = rld[rld[secondary_reload].secondary_out_reload].reg_rtx;
6741 rtx tem;
6743 /* Copy primary reload reg to secondary reload reg.
6744 (Note that these have been swapped above, then
6745 secondary reload reg to OLD using our insn.) */
6747 /* If REAL_OLD is a paradoxical SUBREG, remove it
6748 and try to put the opposite SUBREG on
6749 RELOADREG. */
6750 if (GET_CODE (real_old) == SUBREG
6751 && (GET_MODE_SIZE (GET_MODE (real_old))
6752 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
6753 && 0 != (tem = gen_lowpart_common
6754 (GET_MODE (SUBREG_REG (real_old)),
6755 reloadreg)))
6756 real_old = SUBREG_REG (real_old), reloadreg = tem;
6758 gen_reload (reloadreg, second_reloadreg,
6759 rl->opnum, rl->when_needed);
6760 emit_insn ((GEN_FCN (tertiary_icode)
6761 (real_old, reloadreg, third_reloadreg)));
6762 special = 1;
6765 else
6766 /* Copy between the reload regs here and then to
6767 OUT later. */
6769 gen_reload (reloadreg, second_reloadreg,
6770 rl->opnum, rl->when_needed);
6774 #endif
6776 /* Output the last reload insn. */
6777 if (! special)
6779 rtx set;
6781 /* Don't output the last reload if OLD is not the dest of
6782 INSN and is in the src and is clobbered by INSN. */
6783 if (! flag_expensive_optimizations
6784 || GET_CODE (old) != REG
6785 || !(set = single_set (insn))
6786 || rtx_equal_p (old, SET_DEST (set))
6787 || !reg_mentioned_p (old, SET_SRC (set))
6788 || !regno_clobbered_p (REGNO (old), insn, rl->mode, 0))
6789 gen_reload (old, reloadreg, rl->opnum,
6790 rl->when_needed);
6793 /* Look at all insns we emitted, just to be safe. */
6794 for (p = get_insns (); p; p = NEXT_INSN (p))
6795 if (INSN_P (p))
6797 rtx pat = PATTERN (p);
6799 /* If this output reload doesn't come from a spill reg,
6800 clear any memory of reloaded copies of the pseudo reg.
6801 If this output reload comes from a spill reg,
6802 reg_has_output_reload will make this do nothing. */
6803 note_stores (pat, forget_old_reloads_1, NULL);
6805 if (reg_mentioned_p (rl->reg_rtx, pat))
6807 rtx set = single_set (insn);
6808 if (reload_spill_index[j] < 0
6809 && set
6810 && SET_SRC (set) == rl->reg_rtx)
6812 int src = REGNO (SET_SRC (set));
6814 reload_spill_index[j] = src;
6815 SET_HARD_REG_BIT (reg_is_output_reload, src);
6816 if (find_regno_note (insn, REG_DEAD, src))
6817 SET_HARD_REG_BIT (reg_reloaded_died, src);
6819 if (REGNO (rl->reg_rtx) < FIRST_PSEUDO_REGISTER)
6821 int s = rl->secondary_out_reload;
6822 set = single_set (p);
6823 /* If this reload copies only to the secondary reload
6824 register, the secondary reload does the actual
6825 store. */
6826 if (s >= 0 && set == NULL_RTX)
6827 /* We can't tell what function the secondary reload
6828 has and where the actual store to the pseudo is
6829 made; leave new_spill_reg_store alone. */
6831 else if (s >= 0
6832 && SET_SRC (set) == rl->reg_rtx
6833 && SET_DEST (set) == rld[s].reg_rtx)
6835 /* Usually the next instruction will be the
6836 secondary reload insn; if we can confirm
6837 that it is, setting new_spill_reg_store to
6838 that insn will allow an extra optimization. */
6839 rtx s_reg = rld[s].reg_rtx;
6840 rtx next = NEXT_INSN (p);
6841 rld[s].out = rl->out;
6842 rld[s].out_reg = rl->out_reg;
6843 set = single_set (next);
6844 if (set && SET_SRC (set) == s_reg
6845 && ! new_spill_reg_store[REGNO (s_reg)])
6847 SET_HARD_REG_BIT (reg_is_output_reload,
6848 REGNO (s_reg));
6849 new_spill_reg_store[REGNO (s_reg)] = next;
6852 else
6853 new_spill_reg_store[REGNO (rl->reg_rtx)] = p;
6858 if (rl->when_needed == RELOAD_OTHER)
6860 emit_insn (other_output_reload_insns[rl->opnum]);
6861 other_output_reload_insns[rl->opnum] = get_insns ();
6863 else
6864 output_reload_insns[rl->opnum] = get_insns ();
6866 if (flag_non_call_exceptions)
6867 copy_eh_notes (insn, get_insns ());
6869 end_sequence ();
6872 /* Do input reloading for reload RL, which is for the insn described by CHAIN
6873 and has the number J. */
6874 static void
6875 do_input_reload (chain, rl, j)
6876 struct insn_chain *chain;
6877 struct reload *rl;
6878 int j;
6880 rtx insn = chain->insn;
6881 rtx old = (rl->in && GET_CODE (rl->in) == MEM
6882 ? rl->in_reg : rl->in);
6884 if (old != 0
6885 /* AUTO_INC reloads need to be handled even if inherited. We got an
6886 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
6887 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
6888 && ! rtx_equal_p (rl->reg_rtx, old)
6889 && rl->reg_rtx != 0)
6890 emit_input_reload_insns (chain, rld + j, old, j);
6892 /* When inheriting a wider reload, we have a MEM in rl->in,
6893 e.g. inheriting a SImode output reload for
6894 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
6895 if (optimize && reload_inherited[j] && rl->in
6896 && GET_CODE (rl->in) == MEM
6897 && GET_CODE (rl->in_reg) == MEM
6898 && reload_spill_index[j] >= 0
6899 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
6900 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
6902 /* If we are reloading a register that was recently stored in with an
6903 output-reload, see if we can prove there was
6904 actually no need to store the old value in it. */
6906 if (optimize
6907 && (reload_inherited[j] || reload_override_in[j])
6908 && rl->reg_rtx
6909 && GET_CODE (rl->reg_rtx) == REG
6910 && spill_reg_store[REGNO (rl->reg_rtx)] != 0
6911 #if 0
6912 /* There doesn't seem to be any reason to restrict this to pseudos
6913 and doing so loses in the case where we are copying from a
6914 register of the wrong class. */
6915 && (REGNO (spill_reg_stored_to[REGNO (rl->reg_rtx)])
6916 >= FIRST_PSEUDO_REGISTER)
6917 #endif
6918 /* The insn might have already some references to stackslots
6919 replaced by MEMs, while reload_out_reg still names the
6920 original pseudo. */
6921 && (dead_or_set_p (insn,
6922 spill_reg_stored_to[REGNO (rl->reg_rtx)])
6923 || rtx_equal_p (spill_reg_stored_to[REGNO (rl->reg_rtx)],
6924 rl->out_reg)))
6925 delete_output_reload (insn, j, REGNO (rl->reg_rtx));
6928 /* Do output reloading for reload RL, which is for the insn described by
6929 CHAIN and has the number J.
6930 ??? At some point we need to support handling output reloads of
6931 JUMP_INSNs or insns that set cc0. */
6932 static void
6933 do_output_reload (chain, rl, j)
6934 struct insn_chain *chain;
6935 struct reload *rl;
6936 int j;
6938 rtx note, old;
6939 rtx insn = chain->insn;
6940 /* If this is an output reload that stores something that is
6941 not loaded in this same reload, see if we can eliminate a previous
6942 store. */
6943 rtx pseudo = rl->out_reg;
6945 if (pseudo
6946 && optimize
6947 && GET_CODE (pseudo) == REG
6948 && ! rtx_equal_p (rl->in_reg, pseudo)
6949 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
6950 && reg_last_reload_reg[REGNO (pseudo)])
6952 int pseudo_no = REGNO (pseudo);
6953 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
6955 /* We don't need to test full validity of last_regno for
6956 inherit here; we only want to know if the store actually
6957 matches the pseudo. */
6958 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
6959 && reg_reloaded_contents[last_regno] == pseudo_no
6960 && spill_reg_store[last_regno]
6961 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
6962 delete_output_reload (insn, j, last_regno);
6965 old = rl->out_reg;
6966 if (old == 0
6967 || rl->reg_rtx == old
6968 || rl->reg_rtx == 0)
6969 return;
6971 /* An output operand that dies right away does need a reload,
6972 but need not be copied from it. Show the new location in the
6973 REG_UNUSED note. */
6974 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
6975 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
6977 XEXP (note, 0) = rl->reg_rtx;
6978 return;
6980 /* Likewise for a SUBREG of an operand that dies. */
6981 else if (GET_CODE (old) == SUBREG
6982 && GET_CODE (SUBREG_REG (old)) == REG
6983 && 0 != (note = find_reg_note (insn, REG_UNUSED,
6984 SUBREG_REG (old))))
6986 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
6987 rl->reg_rtx);
6988 return;
6990 else if (GET_CODE (old) == SCRATCH)
6991 /* If we aren't optimizing, there won't be a REG_UNUSED note,
6992 but we don't want to make an output reload. */
6993 return;
6995 /* If is a JUMP_INSN, we can't support output reloads yet. */
6996 if (GET_CODE (insn) == JUMP_INSN)
6997 abort ();
6999 emit_output_reload_insns (chain, rld + j, j);
7002 /* Output insns to reload values in and out of the chosen reload regs. */
7004 static void
7005 emit_reload_insns (chain)
7006 struct insn_chain *chain;
7008 rtx insn = chain->insn;
7010 int j;
7012 CLEAR_HARD_REG_SET (reg_reloaded_died);
7014 for (j = 0; j < reload_n_operands; j++)
7015 input_reload_insns[j] = input_address_reload_insns[j]
7016 = inpaddr_address_reload_insns[j]
7017 = output_reload_insns[j] = output_address_reload_insns[j]
7018 = outaddr_address_reload_insns[j]
7019 = other_output_reload_insns[j] = 0;
7020 other_input_address_reload_insns = 0;
7021 other_input_reload_insns = 0;
7022 operand_reload_insns = 0;
7023 other_operand_reload_insns = 0;
7025 /* Dump reloads into the dump file. */
7026 if (rtl_dump_file)
7028 fprintf (rtl_dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
7029 debug_reload_to_stream (rtl_dump_file);
7032 /* Now output the instructions to copy the data into and out of the
7033 reload registers. Do these in the order that the reloads were reported,
7034 since reloads of base and index registers precede reloads of operands
7035 and the operands may need the base and index registers reloaded. */
7037 for (j = 0; j < n_reloads; j++)
7039 if (rld[j].reg_rtx
7040 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
7041 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
7043 do_input_reload (chain, rld + j, j);
7044 do_output_reload (chain, rld + j, j);
7047 /* Now write all the insns we made for reloads in the order expected by
7048 the allocation functions. Prior to the insn being reloaded, we write
7049 the following reloads:
7051 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7053 RELOAD_OTHER reloads.
7055 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7056 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7057 RELOAD_FOR_INPUT reload for the operand.
7059 RELOAD_FOR_OPADDR_ADDRS reloads.
7061 RELOAD_FOR_OPERAND_ADDRESS reloads.
7063 After the insn being reloaded, we write the following:
7065 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7066 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7067 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7068 reloads for the operand. The RELOAD_OTHER output reloads are
7069 output in descending order by reload number. */
7071 emit_insn_before (other_input_address_reload_insns, insn);
7072 emit_insn_before (other_input_reload_insns, insn);
7074 for (j = 0; j < reload_n_operands; j++)
7076 emit_insn_before (inpaddr_address_reload_insns[j], insn);
7077 emit_insn_before (input_address_reload_insns[j], insn);
7078 emit_insn_before (input_reload_insns[j], insn);
7081 emit_insn_before (other_operand_reload_insns, insn);
7082 emit_insn_before (operand_reload_insns, insn);
7084 for (j = 0; j < reload_n_operands; j++)
7086 rtx x = emit_insn_after (outaddr_address_reload_insns[j], insn);
7087 x = emit_insn_after (output_address_reload_insns[j], x);
7088 x = emit_insn_after (output_reload_insns[j], x);
7089 emit_insn_after (other_output_reload_insns[j], x);
7092 /* For all the spill regs newly reloaded in this instruction,
7093 record what they were reloaded from, so subsequent instructions
7094 can inherit the reloads.
7096 Update spill_reg_store for the reloads of this insn.
7097 Copy the elements that were updated in the loop above. */
7099 for (j = 0; j < n_reloads; j++)
7101 int r = reload_order[j];
7102 int i = reload_spill_index[r];
7104 /* If this is a non-inherited input reload from a pseudo, we must
7105 clear any memory of a previous store to the same pseudo. Only do
7106 something if there will not be an output reload for the pseudo
7107 being reloaded. */
7108 if (rld[r].in_reg != 0
7109 && ! (reload_inherited[r] || reload_override_in[r]))
7111 rtx reg = rld[r].in_reg;
7113 if (GET_CODE (reg) == SUBREG)
7114 reg = SUBREG_REG (reg);
7116 if (GET_CODE (reg) == REG
7117 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7118 && ! reg_has_output_reload[REGNO (reg)])
7120 int nregno = REGNO (reg);
7122 if (reg_last_reload_reg[nregno])
7124 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7126 if (reg_reloaded_contents[last_regno] == nregno)
7127 spill_reg_store[last_regno] = 0;
7132 /* I is nonneg if this reload used a register.
7133 If rld[r].reg_rtx is 0, this is an optional reload
7134 that we opted to ignore. */
7136 if (i >= 0 && rld[r].reg_rtx != 0)
7138 int nr = HARD_REGNO_NREGS (i, GET_MODE (rld[r].reg_rtx));
7139 int k;
7140 int part_reaches_end = 0;
7141 int all_reaches_end = 1;
7143 /* For a multi register reload, we need to check if all or part
7144 of the value lives to the end. */
7145 for (k = 0; k < nr; k++)
7147 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7148 rld[r].when_needed))
7149 part_reaches_end = 1;
7150 else
7151 all_reaches_end = 0;
7154 /* Ignore reloads that don't reach the end of the insn in
7155 entirety. */
7156 if (all_reaches_end)
7158 /* First, clear out memory of what used to be in this spill reg.
7159 If consecutive registers are used, clear them all. */
7161 for (k = 0; k < nr; k++)
7162 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7164 /* Maybe the spill reg contains a copy of reload_out. */
7165 if (rld[r].out != 0
7166 && (GET_CODE (rld[r].out) == REG
7167 #ifdef AUTO_INC_DEC
7168 || ! rld[r].out_reg
7169 #endif
7170 || GET_CODE (rld[r].out_reg) == REG))
7172 rtx out = (GET_CODE (rld[r].out) == REG
7173 ? rld[r].out
7174 : rld[r].out_reg
7175 ? rld[r].out_reg
7176 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7177 int nregno = REGNO (out);
7178 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7179 : HARD_REGNO_NREGS (nregno,
7180 GET_MODE (rld[r].reg_rtx)));
7182 spill_reg_store[i] = new_spill_reg_store[i];
7183 spill_reg_stored_to[i] = out;
7184 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7186 /* If NREGNO is a hard register, it may occupy more than
7187 one register. If it does, say what is in the
7188 rest of the registers assuming that both registers
7189 agree on how many words the object takes. If not,
7190 invalidate the subsequent registers. */
7192 if (nregno < FIRST_PSEUDO_REGISTER)
7193 for (k = 1; k < nnr; k++)
7194 reg_last_reload_reg[nregno + k]
7195 = (nr == nnr
7196 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7197 : 0);
7199 /* Now do the inverse operation. */
7200 for (k = 0; k < nr; k++)
7202 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7203 reg_reloaded_contents[i + k]
7204 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7205 ? nregno
7206 : nregno + k);
7207 reg_reloaded_insn[i + k] = insn;
7208 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7212 /* Maybe the spill reg contains a copy of reload_in. Only do
7213 something if there will not be an output reload for
7214 the register being reloaded. */
7215 else if (rld[r].out_reg == 0
7216 && rld[r].in != 0
7217 && ((GET_CODE (rld[r].in) == REG
7218 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7219 && ! reg_has_output_reload[REGNO (rld[r].in)])
7220 || (GET_CODE (rld[r].in_reg) == REG
7221 && ! reg_has_output_reload[REGNO (rld[r].in_reg)]))
7222 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7224 int nregno;
7225 int nnr;
7227 if (GET_CODE (rld[r].in) == REG
7228 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7229 nregno = REGNO (rld[r].in);
7230 else if (GET_CODE (rld[r].in_reg) == REG)
7231 nregno = REGNO (rld[r].in_reg);
7232 else
7233 nregno = REGNO (XEXP (rld[r].in_reg, 0));
7235 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7236 : HARD_REGNO_NREGS (nregno,
7237 GET_MODE (rld[r].reg_rtx)));
7239 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7241 if (nregno < FIRST_PSEUDO_REGISTER)
7242 for (k = 1; k < nnr; k++)
7243 reg_last_reload_reg[nregno + k]
7244 = (nr == nnr
7245 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7246 : 0);
7248 /* Unless we inherited this reload, show we haven't
7249 recently done a store.
7250 Previous stores of inherited auto_inc expressions
7251 also have to be discarded. */
7252 if (! reload_inherited[r]
7253 || (rld[r].out && ! rld[r].out_reg))
7254 spill_reg_store[i] = 0;
7256 for (k = 0; k < nr; k++)
7258 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7259 reg_reloaded_contents[i + k]
7260 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7261 ? nregno
7262 : nregno + k);
7263 reg_reloaded_insn[i + k] = insn;
7264 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7269 /* However, if part of the reload reaches the end, then we must
7270 invalidate the old info for the part that survives to the end. */
7271 else if (part_reaches_end)
7273 for (k = 0; k < nr; k++)
7274 if (reload_reg_reaches_end_p (i + k,
7275 rld[r].opnum,
7276 rld[r].when_needed))
7277 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7281 /* The following if-statement was #if 0'd in 1.34 (or before...).
7282 It's reenabled in 1.35 because supposedly nothing else
7283 deals with this problem. */
7285 /* If a register gets output-reloaded from a non-spill register,
7286 that invalidates any previous reloaded copy of it.
7287 But forget_old_reloads_1 won't get to see it, because
7288 it thinks only about the original insn. So invalidate it here. */
7289 if (i < 0 && rld[r].out != 0
7290 && (GET_CODE (rld[r].out) == REG
7291 || (GET_CODE (rld[r].out) == MEM
7292 && GET_CODE (rld[r].out_reg) == REG)))
7294 rtx out = (GET_CODE (rld[r].out) == REG
7295 ? rld[r].out : rld[r].out_reg);
7296 int nregno = REGNO (out);
7297 if (nregno >= FIRST_PSEUDO_REGISTER)
7299 rtx src_reg, store_insn = NULL_RTX;
7301 reg_last_reload_reg[nregno] = 0;
7303 /* If we can find a hard register that is stored, record
7304 the storing insn so that we may delete this insn with
7305 delete_output_reload. */
7306 src_reg = rld[r].reg_rtx;
7308 /* If this is an optional reload, try to find the source reg
7309 from an input reload. */
7310 if (! src_reg)
7312 rtx set = single_set (insn);
7313 if (set && SET_DEST (set) == rld[r].out)
7315 int k;
7317 src_reg = SET_SRC (set);
7318 store_insn = insn;
7319 for (k = 0; k < n_reloads; k++)
7321 if (rld[k].in == src_reg)
7323 src_reg = rld[k].reg_rtx;
7324 break;
7329 else
7330 store_insn = new_spill_reg_store[REGNO (src_reg)];
7331 if (src_reg && GET_CODE (src_reg) == REG
7332 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7334 int src_regno = REGNO (src_reg);
7335 int nr = HARD_REGNO_NREGS (src_regno, rld[r].mode);
7336 /* The place where to find a death note varies with
7337 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7338 necessarily checked exactly in the code that moves
7339 notes, so just check both locations. */
7340 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7341 if (! note && store_insn)
7342 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7343 while (nr-- > 0)
7345 spill_reg_store[src_regno + nr] = store_insn;
7346 spill_reg_stored_to[src_regno + nr] = out;
7347 reg_reloaded_contents[src_regno + nr] = nregno;
7348 reg_reloaded_insn[src_regno + nr] = store_insn;
7349 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7350 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7351 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7352 if (note)
7353 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7354 else
7355 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7357 reg_last_reload_reg[nregno] = src_reg;
7360 else
7362 int num_regs = HARD_REGNO_NREGS (nregno, GET_MODE (rld[r].out));
7364 while (num_regs-- > 0)
7365 reg_last_reload_reg[nregno + num_regs] = 0;
7369 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7372 /* Emit code to perform a reload from IN (which may be a reload register) to
7373 OUT (which may also be a reload register). IN or OUT is from operand
7374 OPNUM with reload type TYPE.
7376 Returns first insn emitted. */
7379 gen_reload (out, in, opnum, type)
7380 rtx out;
7381 rtx in;
7382 int opnum;
7383 enum reload_type type;
7385 rtx last = get_last_insn ();
7386 rtx tem;
7388 /* If IN is a paradoxical SUBREG, remove it and try to put the
7389 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7390 if (GET_CODE (in) == SUBREG
7391 && (GET_MODE_SIZE (GET_MODE (in))
7392 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7393 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7394 in = SUBREG_REG (in), out = tem;
7395 else if (GET_CODE (out) == SUBREG
7396 && (GET_MODE_SIZE (GET_MODE (out))
7397 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7398 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7399 out = SUBREG_REG (out), in = tem;
7401 /* How to do this reload can get quite tricky. Normally, we are being
7402 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7403 register that didn't get a hard register. In that case we can just
7404 call emit_move_insn.
7406 We can also be asked to reload a PLUS that adds a register or a MEM to
7407 another register, constant or MEM. This can occur during frame pointer
7408 elimination and while reloading addresses. This case is handled by
7409 trying to emit a single insn to perform the add. If it is not valid,
7410 we use a two insn sequence.
7412 Finally, we could be called to handle an 'o' constraint by putting
7413 an address into a register. In that case, we first try to do this
7414 with a named pattern of "reload_load_address". If no such pattern
7415 exists, we just emit a SET insn and hope for the best (it will normally
7416 be valid on machines that use 'o').
7418 This entire process is made complex because reload will never
7419 process the insns we generate here and so we must ensure that
7420 they will fit their constraints and also by the fact that parts of
7421 IN might be being reloaded separately and replaced with spill registers.
7422 Because of this, we are, in some sense, just guessing the right approach
7423 here. The one listed above seems to work.
7425 ??? At some point, this whole thing needs to be rethought. */
7427 if (GET_CODE (in) == PLUS
7428 && (GET_CODE (XEXP (in, 0)) == REG
7429 || GET_CODE (XEXP (in, 0)) == SUBREG
7430 || GET_CODE (XEXP (in, 0)) == MEM)
7431 && (GET_CODE (XEXP (in, 1)) == REG
7432 || GET_CODE (XEXP (in, 1)) == SUBREG
7433 || CONSTANT_P (XEXP (in, 1))
7434 || GET_CODE (XEXP (in, 1)) == MEM))
7436 /* We need to compute the sum of a register or a MEM and another
7437 register, constant, or MEM, and put it into the reload
7438 register. The best possible way of doing this is if the machine
7439 has a three-operand ADD insn that accepts the required operands.
7441 The simplest approach is to try to generate such an insn and see if it
7442 is recognized and matches its constraints. If so, it can be used.
7444 It might be better not to actually emit the insn unless it is valid,
7445 but we need to pass the insn as an operand to `recog' and
7446 `extract_insn' and it is simpler to emit and then delete the insn if
7447 not valid than to dummy things up. */
7449 rtx op0, op1, tem, insn;
7450 int code;
7452 op0 = find_replacement (&XEXP (in, 0));
7453 op1 = find_replacement (&XEXP (in, 1));
7455 /* Since constraint checking is strict, commutativity won't be
7456 checked, so we need to do that here to avoid spurious failure
7457 if the add instruction is two-address and the second operand
7458 of the add is the same as the reload reg, which is frequently
7459 the case. If the insn would be A = B + A, rearrange it so
7460 it will be A = A + B as constrain_operands expects. */
7462 if (GET_CODE (XEXP (in, 1)) == REG
7463 && REGNO (out) == REGNO (XEXP (in, 1)))
7464 tem = op0, op0 = op1, op1 = tem;
7466 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7467 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7469 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
7470 code = recog_memoized (insn);
7472 if (code >= 0)
7474 extract_insn (insn);
7475 /* We want constrain operands to treat this insn strictly in
7476 its validity determination, i.e., the way it would after reload
7477 has completed. */
7478 if (constrain_operands (1))
7479 return insn;
7482 delete_insns_since (last);
7484 /* If that failed, we must use a conservative two-insn sequence.
7486 Use a move to copy one operand into the reload register. Prefer
7487 to reload a constant, MEM or pseudo since the move patterns can
7488 handle an arbitrary operand. If OP1 is not a constant, MEM or
7489 pseudo and OP1 is not a valid operand for an add instruction, then
7490 reload OP1.
7492 After reloading one of the operands into the reload register, add
7493 the reload register to the output register.
7495 If there is another way to do this for a specific machine, a
7496 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7497 we emit below. */
7499 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7501 if (CONSTANT_P (op1) || GET_CODE (op1) == MEM || GET_CODE (op1) == SUBREG
7502 || (GET_CODE (op1) == REG
7503 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7504 || (code != CODE_FOR_nothing
7505 && ! ((*insn_data[code].operand[2].predicate)
7506 (op1, insn_data[code].operand[2].mode))))
7507 tem = op0, op0 = op1, op1 = tem;
7509 gen_reload (out, op0, opnum, type);
7511 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7512 This fixes a problem on the 32K where the stack pointer cannot
7513 be used as an operand of an add insn. */
7515 if (rtx_equal_p (op0, op1))
7516 op1 = out;
7518 insn = emit_insn (gen_add2_insn (out, op1));
7520 /* If that failed, copy the address register to the reload register.
7521 Then add the constant to the reload register. */
7523 code = recog_memoized (insn);
7525 if (code >= 0)
7527 extract_insn (insn);
7528 /* We want constrain operands to treat this insn strictly in
7529 its validity determination, i.e., the way it would after reload
7530 has completed. */
7531 if (constrain_operands (1))
7533 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7534 REG_NOTES (insn)
7535 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7536 return insn;
7540 delete_insns_since (last);
7542 gen_reload (out, op1, opnum, type);
7543 insn = emit_insn (gen_add2_insn (out, op0));
7544 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7547 #ifdef SECONDARY_MEMORY_NEEDED
7548 /* If we need a memory location to do the move, do it that way. */
7549 else if ((GET_CODE (in) == REG || GET_CODE (in) == SUBREG)
7550 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
7551 && (GET_CODE (out) == REG || GET_CODE (out) == SUBREG)
7552 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
7553 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
7554 REGNO_REG_CLASS (reg_or_subregno (out)),
7555 GET_MODE (out)))
7557 /* Get the memory to use and rewrite both registers to its mode. */
7558 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7560 if (GET_MODE (loc) != GET_MODE (out))
7561 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7563 if (GET_MODE (loc) != GET_MODE (in))
7564 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7566 gen_reload (loc, in, opnum, type);
7567 gen_reload (out, loc, opnum, type);
7569 #endif
7571 /* If IN is a simple operand, use gen_move_insn. */
7572 else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
7573 emit_insn (gen_move_insn (out, in));
7575 #ifdef HAVE_reload_load_address
7576 else if (HAVE_reload_load_address)
7577 emit_insn (gen_reload_load_address (out, in));
7578 #endif
7580 /* Otherwise, just write (set OUT IN) and hope for the best. */
7581 else
7582 emit_insn (gen_rtx_SET (VOIDmode, out, in));
7584 /* Return the first insn emitted.
7585 We can not just return get_last_insn, because there may have
7586 been multiple instructions emitted. Also note that gen_move_insn may
7587 emit more than one insn itself, so we can not assume that there is one
7588 insn emitted per emit_insn_before call. */
7590 return last ? NEXT_INSN (last) : get_insns ();
7593 /* Delete a previously made output-reload whose result we now believe
7594 is not needed. First we double-check.
7596 INSN is the insn now being processed.
7597 LAST_RELOAD_REG is the hard register number for which we want to delete
7598 the last output reload.
7599 J is the reload-number that originally used REG. The caller has made
7600 certain that reload J doesn't use REG any longer for input. */
7602 static void
7603 delete_output_reload (insn, j, last_reload_reg)
7604 rtx insn;
7605 int j;
7606 int last_reload_reg;
7608 rtx output_reload_insn = spill_reg_store[last_reload_reg];
7609 rtx reg = spill_reg_stored_to[last_reload_reg];
7610 int k;
7611 int n_occurrences;
7612 int n_inherited = 0;
7613 rtx i1;
7614 rtx substed;
7616 /* It is possible that this reload has been only used to set another reload
7617 we eliminated earlier and thus deleted this instruction too. */
7618 if (INSN_DELETED_P (output_reload_insn))
7619 return;
7621 /* Get the raw pseudo-register referred to. */
7623 while (GET_CODE (reg) == SUBREG)
7624 reg = SUBREG_REG (reg);
7625 substed = reg_equiv_memory_loc[REGNO (reg)];
7627 /* This is unsafe if the operand occurs more often in the current
7628 insn than it is inherited. */
7629 for (k = n_reloads - 1; k >= 0; k--)
7631 rtx reg2 = rld[k].in;
7632 if (! reg2)
7633 continue;
7634 if (GET_CODE (reg2) == MEM || reload_override_in[k])
7635 reg2 = rld[k].in_reg;
7636 #ifdef AUTO_INC_DEC
7637 if (rld[k].out && ! rld[k].out_reg)
7638 reg2 = XEXP (rld[k].in_reg, 0);
7639 #endif
7640 while (GET_CODE (reg2) == SUBREG)
7641 reg2 = SUBREG_REG (reg2);
7642 if (rtx_equal_p (reg2, reg))
7644 if (reload_inherited[k] || reload_override_in[k] || k == j)
7646 n_inherited++;
7647 reg2 = rld[k].out_reg;
7648 if (! reg2)
7649 continue;
7650 while (GET_CODE (reg2) == SUBREG)
7651 reg2 = XEXP (reg2, 0);
7652 if (rtx_equal_p (reg2, reg))
7653 n_inherited++;
7655 else
7656 return;
7659 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
7660 if (substed)
7661 n_occurrences += count_occurrences (PATTERN (insn),
7662 eliminate_regs (substed, 0,
7663 NULL_RTX), 0);
7664 if (n_occurrences > n_inherited)
7665 return;
7667 /* If the pseudo-reg we are reloading is no longer referenced
7668 anywhere between the store into it and here,
7669 and no jumps or labels intervene, then the value can get
7670 here through the reload reg alone.
7671 Otherwise, give up--return. */
7672 for (i1 = NEXT_INSN (output_reload_insn);
7673 i1 != insn; i1 = NEXT_INSN (i1))
7675 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
7676 return;
7677 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
7678 && reg_mentioned_p (reg, PATTERN (i1)))
7680 /* If this is USE in front of INSN, we only have to check that
7681 there are no more references than accounted for by inheritance. */
7682 while (GET_CODE (i1) == INSN && GET_CODE (PATTERN (i1)) == USE)
7684 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
7685 i1 = NEXT_INSN (i1);
7687 if (n_occurrences <= n_inherited && i1 == insn)
7688 break;
7689 return;
7693 /* We will be deleting the insn. Remove the spill reg information. */
7694 for (k = HARD_REGNO_NREGS (last_reload_reg, GET_MODE (reg)); k-- > 0; )
7696 spill_reg_store[last_reload_reg + k] = 0;
7697 spill_reg_stored_to[last_reload_reg + k] = 0;
7700 /* The caller has already checked that REG dies or is set in INSN.
7701 It has also checked that we are optimizing, and thus some
7702 inaccuracies in the debugging information are acceptable.
7703 So we could just delete output_reload_insn. But in some cases
7704 we can improve the debugging information without sacrificing
7705 optimization - maybe even improving the code: See if the pseudo
7706 reg has been completely replaced with reload regs. If so, delete
7707 the store insn and forget we had a stack slot for the pseudo. */
7708 if (rld[j].out != rld[j].in
7709 && REG_N_DEATHS (REGNO (reg)) == 1
7710 && REG_N_SETS (REGNO (reg)) == 1
7711 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
7712 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
7714 rtx i2;
7716 /* We know that it was used only between here and the beginning of
7717 the current basic block. (We also know that the last use before
7718 INSN was the output reload we are thinking of deleting, but never
7719 mind that.) Search that range; see if any ref remains. */
7720 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7722 rtx set = single_set (i2);
7724 /* Uses which just store in the pseudo don't count,
7725 since if they are the only uses, they are dead. */
7726 if (set != 0 && SET_DEST (set) == reg)
7727 continue;
7728 if (GET_CODE (i2) == CODE_LABEL
7729 || GET_CODE (i2) == JUMP_INSN)
7730 break;
7731 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
7732 && reg_mentioned_p (reg, PATTERN (i2)))
7734 /* Some other ref remains; just delete the output reload we
7735 know to be dead. */
7736 delete_address_reloads (output_reload_insn, insn);
7737 delete_insn (output_reload_insn);
7738 return;
7742 /* Delete the now-dead stores into this pseudo. Note that this
7743 loop also takes care of deleting output_reload_insn. */
7744 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7746 rtx set = single_set (i2);
7748 if (set != 0 && SET_DEST (set) == reg)
7750 delete_address_reloads (i2, insn);
7751 delete_insn (i2);
7753 if (GET_CODE (i2) == CODE_LABEL
7754 || GET_CODE (i2) == JUMP_INSN)
7755 break;
7758 /* For the debugging info, say the pseudo lives in this reload reg. */
7759 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
7760 alter_reg (REGNO (reg), -1);
7762 else
7764 delete_address_reloads (output_reload_insn, insn);
7765 delete_insn (output_reload_insn);
7769 /* We are going to delete DEAD_INSN. Recursively delete loads of
7770 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
7771 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
7772 static void
7773 delete_address_reloads (dead_insn, current_insn)
7774 rtx dead_insn, current_insn;
7776 rtx set = single_set (dead_insn);
7777 rtx set2, dst, prev, next;
7778 if (set)
7780 rtx dst = SET_DEST (set);
7781 if (GET_CODE (dst) == MEM)
7782 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
7784 /* If we deleted the store from a reloaded post_{in,de}c expression,
7785 we can delete the matching adds. */
7786 prev = PREV_INSN (dead_insn);
7787 next = NEXT_INSN (dead_insn);
7788 if (! prev || ! next)
7789 return;
7790 set = single_set (next);
7791 set2 = single_set (prev);
7792 if (! set || ! set2
7793 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
7794 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
7795 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
7796 return;
7797 dst = SET_DEST (set);
7798 if (! rtx_equal_p (dst, SET_DEST (set2))
7799 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
7800 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
7801 || (INTVAL (XEXP (SET_SRC (set), 1))
7802 != -INTVAL (XEXP (SET_SRC (set2), 1))))
7803 return;
7804 delete_related_insns (prev);
7805 delete_related_insns (next);
7808 /* Subfunction of delete_address_reloads: process registers found in X. */
7809 static void
7810 delete_address_reloads_1 (dead_insn, x, current_insn)
7811 rtx dead_insn, x, current_insn;
7813 rtx prev, set, dst, i2;
7814 int i, j;
7815 enum rtx_code code = GET_CODE (x);
7817 if (code != REG)
7819 const char *fmt = GET_RTX_FORMAT (code);
7820 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7822 if (fmt[i] == 'e')
7823 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
7824 else if (fmt[i] == 'E')
7826 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7827 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
7828 current_insn);
7831 return;
7834 if (spill_reg_order[REGNO (x)] < 0)
7835 return;
7837 /* Scan backwards for the insn that sets x. This might be a way back due
7838 to inheritance. */
7839 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
7841 code = GET_CODE (prev);
7842 if (code == CODE_LABEL || code == JUMP_INSN)
7843 return;
7844 if (GET_RTX_CLASS (code) != 'i')
7845 continue;
7846 if (reg_set_p (x, PATTERN (prev)))
7847 break;
7848 if (reg_referenced_p (x, PATTERN (prev)))
7849 return;
7851 if (! prev || INSN_UID (prev) < reload_first_uid)
7852 return;
7853 /* Check that PREV only sets the reload register. */
7854 set = single_set (prev);
7855 if (! set)
7856 return;
7857 dst = SET_DEST (set);
7858 if (GET_CODE (dst) != REG
7859 || ! rtx_equal_p (dst, x))
7860 return;
7861 if (! reg_set_p (dst, PATTERN (dead_insn)))
7863 /* Check if DST was used in a later insn -
7864 it might have been inherited. */
7865 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
7867 if (GET_CODE (i2) == CODE_LABEL)
7868 break;
7869 if (! INSN_P (i2))
7870 continue;
7871 if (reg_referenced_p (dst, PATTERN (i2)))
7873 /* If there is a reference to the register in the current insn,
7874 it might be loaded in a non-inherited reload. If no other
7875 reload uses it, that means the register is set before
7876 referenced. */
7877 if (i2 == current_insn)
7879 for (j = n_reloads - 1; j >= 0; j--)
7880 if ((rld[j].reg_rtx == dst && reload_inherited[j])
7881 || reload_override_in[j] == dst)
7882 return;
7883 for (j = n_reloads - 1; j >= 0; j--)
7884 if (rld[j].in && rld[j].reg_rtx == dst)
7885 break;
7886 if (j >= 0)
7887 break;
7889 return;
7891 if (GET_CODE (i2) == JUMP_INSN)
7892 break;
7893 /* If DST is still live at CURRENT_INSN, check if it is used for
7894 any reload. Note that even if CURRENT_INSN sets DST, we still
7895 have to check the reloads. */
7896 if (i2 == current_insn)
7898 for (j = n_reloads - 1; j >= 0; j--)
7899 if ((rld[j].reg_rtx == dst && reload_inherited[j])
7900 || reload_override_in[j] == dst)
7901 return;
7902 /* ??? We can't finish the loop here, because dst might be
7903 allocated to a pseudo in this block if no reload in this
7904 block needs any of the classes containing DST - see
7905 spill_hard_reg. There is no easy way to tell this, so we
7906 have to scan till the end of the basic block. */
7908 if (reg_set_p (dst, PATTERN (i2)))
7909 break;
7912 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
7913 reg_reloaded_contents[REGNO (dst)] = -1;
7914 delete_insn (prev);
7917 /* Output reload-insns to reload VALUE into RELOADREG.
7918 VALUE is an autoincrement or autodecrement RTX whose operand
7919 is a register or memory location;
7920 so reloading involves incrementing that location.
7921 IN is either identical to VALUE, or some cheaper place to reload from.
7923 INC_AMOUNT is the number to increment or decrement by (always positive).
7924 This cannot be deduced from VALUE.
7926 Return the instruction that stores into RELOADREG. */
7928 static rtx
7929 inc_for_reload (reloadreg, in, value, inc_amount)
7930 rtx reloadreg;
7931 rtx in, value;
7932 int inc_amount;
7934 /* REG or MEM to be copied and incremented. */
7935 rtx incloc = XEXP (value, 0);
7936 /* Nonzero if increment after copying. */
7937 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
7938 rtx last;
7939 rtx inc;
7940 rtx add_insn;
7941 int code;
7942 rtx store;
7943 rtx real_in = in == value ? XEXP (in, 0) : in;
7945 /* No hard register is equivalent to this register after
7946 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
7947 we could inc/dec that register as well (maybe even using it for
7948 the source), but I'm not sure it's worth worrying about. */
7949 if (GET_CODE (incloc) == REG)
7950 reg_last_reload_reg[REGNO (incloc)] = 0;
7952 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
7953 inc_amount = -inc_amount;
7955 inc = GEN_INT (inc_amount);
7957 /* If this is post-increment, first copy the location to the reload reg. */
7958 if (post && real_in != reloadreg)
7959 emit_insn (gen_move_insn (reloadreg, real_in));
7961 if (in == value)
7963 /* See if we can directly increment INCLOC. Use a method similar to
7964 that in gen_reload. */
7966 last = get_last_insn ();
7967 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
7968 gen_rtx_PLUS (GET_MODE (incloc),
7969 incloc, inc)));
7971 code = recog_memoized (add_insn);
7972 if (code >= 0)
7974 extract_insn (add_insn);
7975 if (constrain_operands (1))
7977 /* If this is a pre-increment and we have incremented the value
7978 where it lives, copy the incremented value to RELOADREG to
7979 be used as an address. */
7981 if (! post)
7982 emit_insn (gen_move_insn (reloadreg, incloc));
7984 return add_insn;
7987 delete_insns_since (last);
7990 /* If couldn't do the increment directly, must increment in RELOADREG.
7991 The way we do this depends on whether this is pre- or post-increment.
7992 For pre-increment, copy INCLOC to the reload register, increment it
7993 there, then save back. */
7995 if (! post)
7997 if (in != reloadreg)
7998 emit_insn (gen_move_insn (reloadreg, real_in));
7999 emit_insn (gen_add2_insn (reloadreg, inc));
8000 store = emit_insn (gen_move_insn (incloc, reloadreg));
8002 else
8004 /* Postincrement.
8005 Because this might be a jump insn or a compare, and because RELOADREG
8006 may not be available after the insn in an input reload, we must do
8007 the incrementation before the insn being reloaded for.
8009 We have already copied IN to RELOADREG. Increment the copy in
8010 RELOADREG, save that back, then decrement RELOADREG so it has
8011 the original value. */
8013 emit_insn (gen_add2_insn (reloadreg, inc));
8014 store = emit_insn (gen_move_insn (incloc, reloadreg));
8015 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
8018 return store;
8022 /* See whether a single set SET is a noop. */
8023 static int
8024 reload_cse_noop_set_p (set)
8025 rtx set;
8027 return rtx_equal_for_cselib_p (SET_DEST (set), SET_SRC (set));
8030 /* Try to simplify INSN. */
8031 static void
8032 reload_cse_simplify (insn, testreg)
8033 rtx insn;
8034 rtx testreg;
8036 rtx body = PATTERN (insn);
8038 if (GET_CODE (body) == SET)
8040 int count = 0;
8042 /* Simplify even if we may think it is a no-op.
8043 We may think a memory load of a value smaller than WORD_SIZE
8044 is redundant because we haven't taken into account possible
8045 implicit extension. reload_cse_simplify_set() will bring
8046 this out, so it's safer to simplify before we delete. */
8047 count += reload_cse_simplify_set (body, insn);
8049 if (!count && reload_cse_noop_set_p (body))
8051 rtx value = SET_DEST (body);
8052 if (REG_P (value)
8053 && ! REG_FUNCTION_VALUE_P (value))
8054 value = 0;
8055 delete_insn_and_edges (insn);
8056 return;
8059 if (count > 0)
8060 apply_change_group ();
8061 else
8062 reload_cse_simplify_operands (insn, testreg);
8064 else if (GET_CODE (body) == PARALLEL)
8066 int i;
8067 int count = 0;
8068 rtx value = NULL_RTX;
8070 /* If every action in a PARALLEL is a noop, we can delete
8071 the entire PARALLEL. */
8072 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8074 rtx part = XVECEXP (body, 0, i);
8075 if (GET_CODE (part) == SET)
8077 if (! reload_cse_noop_set_p (part))
8078 break;
8079 if (REG_P (SET_DEST (part))
8080 && REG_FUNCTION_VALUE_P (SET_DEST (part)))
8082 if (value)
8083 break;
8084 value = SET_DEST (part);
8087 else if (GET_CODE (part) != CLOBBER)
8088 break;
8091 if (i < 0)
8093 delete_insn_and_edges (insn);
8094 /* We're done with this insn. */
8095 return;
8098 /* It's not a no-op, but we can try to simplify it. */
8099 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8100 if (GET_CODE (XVECEXP (body, 0, i)) == SET)
8101 count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
8103 if (count > 0)
8104 apply_change_group ();
8105 else
8106 reload_cse_simplify_operands (insn, testreg);
8110 /* Do a very simple CSE pass over the hard registers.
8112 This function detects no-op moves where we happened to assign two
8113 different pseudo-registers to the same hard register, and then
8114 copied one to the other. Reload will generate a useless
8115 instruction copying a register to itself.
8117 This function also detects cases where we load a value from memory
8118 into two different registers, and (if memory is more expensive than
8119 registers) changes it to simply copy the first register into the
8120 second register.
8122 Another optimization is performed that scans the operands of each
8123 instruction to see whether the value is already available in a
8124 hard register. It then replaces the operand with the hard register
8125 if possible, much like an optional reload would. */
8127 static void
8128 reload_cse_regs_1 (first)
8129 rtx first;
8131 rtx insn;
8132 rtx testreg = gen_rtx_REG (VOIDmode, -1);
8134 cselib_init ();
8135 init_alias_analysis ();
8137 for (insn = first; insn; insn = NEXT_INSN (insn))
8139 if (INSN_P (insn))
8140 reload_cse_simplify (insn, testreg);
8142 cselib_process_insn (insn);
8145 /* Clean up. */
8146 end_alias_analysis ();
8147 cselib_finish ();
8150 /* Call cse / combine like post-reload optimization phases.
8151 FIRST is the first instruction. */
8152 void
8153 reload_cse_regs (first)
8154 rtx first;
8156 reload_cse_regs_1 (first);
8157 reload_combine ();
8158 reload_cse_move2add (first);
8159 if (flag_expensive_optimizations)
8160 reload_cse_regs_1 (first);
8163 /* Try to simplify a single SET instruction. SET is the set pattern.
8164 INSN is the instruction it came from.
8165 This function only handles one case: if we set a register to a value
8166 which is not a register, we try to find that value in some other register
8167 and change the set into a register copy. */
8169 static int
8170 reload_cse_simplify_set (set, insn)
8171 rtx set;
8172 rtx insn;
8174 int did_change = 0;
8175 int dreg;
8176 rtx src;
8177 enum reg_class dclass;
8178 int old_cost;
8179 cselib_val *val;
8180 struct elt_loc_list *l;
8181 #ifdef LOAD_EXTEND_OP
8182 enum rtx_code extend_op = NIL;
8183 #endif
8185 dreg = true_regnum (SET_DEST (set));
8186 if (dreg < 0)
8187 return 0;
8189 src = SET_SRC (set);
8190 if (side_effects_p (src) || true_regnum (src) >= 0)
8191 return 0;
8193 dclass = REGNO_REG_CLASS (dreg);
8195 #ifdef LOAD_EXTEND_OP
8196 /* When replacing a memory with a register, we need to honor assumptions
8197 that combine made wrt the contents of sign bits. We'll do this by
8198 generating an extend instruction instead of a reg->reg copy. Thus
8199 the destination must be a register that we can widen. */
8200 if (GET_CODE (src) == MEM
8201 && GET_MODE_BITSIZE (GET_MODE (src)) < BITS_PER_WORD
8202 && (extend_op = LOAD_EXTEND_OP (GET_MODE (src))) != NIL
8203 && GET_CODE (SET_DEST (set)) != REG)
8204 return 0;
8205 #endif
8207 /* If memory loads are cheaper than register copies, don't change them. */
8208 if (GET_CODE (src) == MEM)
8209 old_cost = MEMORY_MOVE_COST (GET_MODE (src), dclass, 1);
8210 else if (CONSTANT_P (src))
8211 old_cost = rtx_cost (src, SET);
8212 else if (GET_CODE (src) == REG)
8213 old_cost = REGISTER_MOVE_COST (GET_MODE (src),
8214 REGNO_REG_CLASS (REGNO (src)), dclass);
8215 else
8216 /* ??? */
8217 old_cost = rtx_cost (src, SET);
8219 val = cselib_lookup (src, GET_MODE (SET_DEST (set)), 0);
8220 if (! val)
8221 return 0;
8222 for (l = val->locs; l; l = l->next)
8224 rtx this_rtx = l->loc;
8225 int this_cost;
8227 if (CONSTANT_P (this_rtx) && ! references_value_p (this_rtx, 0))
8229 #ifdef LOAD_EXTEND_OP
8230 if (extend_op != NIL)
8232 HOST_WIDE_INT this_val;
8234 /* ??? I'm lazy and don't wish to handle CONST_DOUBLE. Other
8235 constants, such as SYMBOL_REF, cannot be extended. */
8236 if (GET_CODE (this_rtx) != CONST_INT)
8237 continue;
8239 this_val = INTVAL (this_rtx);
8240 switch (extend_op)
8242 case ZERO_EXTEND:
8243 this_val &= GET_MODE_MASK (GET_MODE (src));
8244 break;
8245 case SIGN_EXTEND:
8246 /* ??? In theory we're already extended. */
8247 if (this_val == trunc_int_for_mode (this_val, GET_MODE (src)))
8248 break;
8249 default:
8250 abort ();
8252 this_rtx = GEN_INT (this_val);
8254 #endif
8255 this_cost = rtx_cost (this_rtx, SET);
8257 else if (GET_CODE (this_rtx) == REG)
8259 #ifdef LOAD_EXTEND_OP
8260 if (extend_op != NIL)
8262 this_rtx = gen_rtx_fmt_e (extend_op, word_mode, this_rtx);
8263 this_cost = rtx_cost (this_rtx, SET);
8265 else
8266 #endif
8267 this_cost = REGISTER_MOVE_COST (GET_MODE (this_rtx),
8268 REGNO_REG_CLASS (REGNO (this_rtx)),
8269 dclass);
8271 else
8272 continue;
8274 /* If equal costs, prefer registers over anything else. That
8275 tends to lead to smaller instructions on some machines. */
8276 if (this_cost < old_cost
8277 || (this_cost == old_cost
8278 && GET_CODE (this_rtx) == REG
8279 && GET_CODE (SET_SRC (set)) != REG))
8281 #ifdef LOAD_EXTEND_OP
8282 if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (set))) < BITS_PER_WORD
8283 && extend_op != NIL
8284 #ifdef CANNOT_CHANGE_MODE_CLASS
8285 && !CANNOT_CHANGE_MODE_CLASS (GET_MODE (SET_DEST (set)),
8286 word_mode,
8287 REGNO_REG_CLASS (REGNO (SET_DEST (set))))
8288 #endif
8291 rtx wide_dest = gen_rtx_REG (word_mode, REGNO (SET_DEST (set)));
8292 ORIGINAL_REGNO (wide_dest) = ORIGINAL_REGNO (SET_DEST (set));
8293 validate_change (insn, &SET_DEST (set), wide_dest, 1);
8295 #endif
8297 validate_change (insn, &SET_SRC (set), copy_rtx (this_rtx), 1);
8298 old_cost = this_cost, did_change = 1;
8302 return did_change;
8305 /* Try to replace operands in INSN with equivalent values that are already
8306 in registers. This can be viewed as optional reloading.
8308 For each non-register operand in the insn, see if any hard regs are
8309 known to be equivalent to that operand. Record the alternatives which
8310 can accept these hard registers. Among all alternatives, select the
8311 ones which are better or equal to the one currently matching, where
8312 "better" is in terms of '?' and '!' constraints. Among the remaining
8313 alternatives, select the one which replaces most operands with
8314 hard registers. */
8316 static int
8317 reload_cse_simplify_operands (insn, testreg)
8318 rtx insn;
8319 rtx testreg;
8321 int i, j;
8323 /* For each operand, all registers that are equivalent to it. */
8324 HARD_REG_SET equiv_regs[MAX_RECOG_OPERANDS];
8326 const char *constraints[MAX_RECOG_OPERANDS];
8328 /* Vector recording how bad an alternative is. */
8329 int *alternative_reject;
8330 /* Vector recording how many registers can be introduced by choosing
8331 this alternative. */
8332 int *alternative_nregs;
8333 /* Array of vectors recording, for each operand and each alternative,
8334 which hard register to substitute, or -1 if the operand should be
8335 left as it is. */
8336 int *op_alt_regno[MAX_RECOG_OPERANDS];
8337 /* Array of alternatives, sorted in order of decreasing desirability. */
8338 int *alternative_order;
8340 extract_insn (insn);
8342 if (recog_data.n_alternatives == 0 || recog_data.n_operands == 0)
8343 return 0;
8345 /* Figure out which alternative currently matches. */
8346 if (! constrain_operands (1))
8347 fatal_insn_not_found (insn);
8349 alternative_reject = (int *) alloca (recog_data.n_alternatives * sizeof (int));
8350 alternative_nregs = (int *) alloca (recog_data.n_alternatives * sizeof (int));
8351 alternative_order = (int *) alloca (recog_data.n_alternatives * sizeof (int));
8352 memset ((char *) alternative_reject, 0, recog_data.n_alternatives * sizeof (int));
8353 memset ((char *) alternative_nregs, 0, recog_data.n_alternatives * sizeof (int));
8355 /* For each operand, find out which regs are equivalent. */
8356 for (i = 0; i < recog_data.n_operands; i++)
8358 cselib_val *v;
8359 struct elt_loc_list *l;
8361 CLEAR_HARD_REG_SET (equiv_regs[i]);
8363 /* cselib blows up on CODE_LABELs. Trying to fix that doesn't seem
8364 right, so avoid the problem here. Likewise if we have a constant
8365 and the insn pattern doesn't tell us the mode we need. */
8366 if (GET_CODE (recog_data.operand[i]) == CODE_LABEL
8367 || (CONSTANT_P (recog_data.operand[i])
8368 && recog_data.operand_mode[i] == VOIDmode))
8369 continue;
8371 v = cselib_lookup (recog_data.operand[i], recog_data.operand_mode[i], 0);
8372 if (! v)
8373 continue;
8375 for (l = v->locs; l; l = l->next)
8376 if (GET_CODE (l->loc) == REG)
8377 SET_HARD_REG_BIT (equiv_regs[i], REGNO (l->loc));
8380 for (i = 0; i < recog_data.n_operands; i++)
8382 enum machine_mode mode;
8383 int regno;
8384 const char *p;
8386 op_alt_regno[i] = (int *) alloca (recog_data.n_alternatives * sizeof (int));
8387 for (j = 0; j < recog_data.n_alternatives; j++)
8388 op_alt_regno[i][j] = -1;
8390 p = constraints[i] = recog_data.constraints[i];
8391 mode = recog_data.operand_mode[i];
8393 /* Add the reject values for each alternative given by the constraints
8394 for this operand. */
8395 j = 0;
8396 while (*p != '\0')
8398 char c = *p++;
8399 if (c == ',')
8400 j++;
8401 else if (c == '?')
8402 alternative_reject[j] += 3;
8403 else if (c == '!')
8404 alternative_reject[j] += 300;
8407 /* We won't change operands which are already registers. We
8408 also don't want to modify output operands. */
8409 regno = true_regnum (recog_data.operand[i]);
8410 if (regno >= 0
8411 || constraints[i][0] == '='
8412 || constraints[i][0] == '+')
8413 continue;
8415 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8417 int class = (int) NO_REGS;
8419 if (! TEST_HARD_REG_BIT (equiv_regs[i], regno))
8420 continue;
8422 REGNO (testreg) = regno;
8423 PUT_MODE (testreg, mode);
8425 /* We found a register equal to this operand. Now look for all
8426 alternatives that can accept this register and have not been
8427 assigned a register they can use yet. */
8428 j = 0;
8429 p = constraints[i];
8430 for (;;)
8432 char c = *p;
8434 switch (c)
8436 case '=': case '+': case '?':
8437 case '#': case '&': case '!':
8438 case '*': case '%':
8439 case '0': case '1': case '2': case '3': case '4':
8440 case '5': case '6': case '7': case '8': case '9':
8441 case 'm': case '<': case '>': case 'V': case 'o':
8442 case 'E': case 'F': case 'G': case 'H':
8443 case 's': case 'i': case 'n':
8444 case 'I': case 'J': case 'K': case 'L':
8445 case 'M': case 'N': case 'O': case 'P':
8446 case 'p': case 'X':
8447 /* These don't say anything we care about. */
8448 break;
8450 case 'g': case 'r':
8451 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
8452 break;
8454 default:
8455 class
8456 = (reg_class_subunion
8457 [(int) class]
8458 [(int) REG_CLASS_FROM_CONSTRAINT ((unsigned char) c, p)]);
8459 break;
8461 case ',': case '\0':
8462 /* See if REGNO fits this alternative, and set it up as the
8463 replacement register if we don't have one for this
8464 alternative yet and the operand being replaced is not
8465 a cheap CONST_INT. */
8466 if (op_alt_regno[i][j] == -1
8467 && reg_fits_class_p (testreg, class, 0, mode)
8468 && (GET_CODE (recog_data.operand[i]) != CONST_INT
8469 || (rtx_cost (recog_data.operand[i], SET)
8470 > rtx_cost (testreg, SET))))
8472 alternative_nregs[j]++;
8473 op_alt_regno[i][j] = regno;
8475 j++;
8476 break;
8478 p += CONSTRAINT_LEN (c, p);
8480 if (c == '\0')
8481 break;
8486 /* Record all alternatives which are better or equal to the currently
8487 matching one in the alternative_order array. */
8488 for (i = j = 0; i < recog_data.n_alternatives; i++)
8489 if (alternative_reject[i] <= alternative_reject[which_alternative])
8490 alternative_order[j++] = i;
8491 recog_data.n_alternatives = j;
8493 /* Sort it. Given a small number of alternatives, a dumb algorithm
8494 won't hurt too much. */
8495 for (i = 0; i < recog_data.n_alternatives - 1; i++)
8497 int best = i;
8498 int best_reject = alternative_reject[alternative_order[i]];
8499 int best_nregs = alternative_nregs[alternative_order[i]];
8500 int tmp;
8502 for (j = i + 1; j < recog_data.n_alternatives; j++)
8504 int this_reject = alternative_reject[alternative_order[j]];
8505 int this_nregs = alternative_nregs[alternative_order[j]];
8507 if (this_reject < best_reject
8508 || (this_reject == best_reject && this_nregs < best_nregs))
8510 best = j;
8511 best_reject = this_reject;
8512 best_nregs = this_nregs;
8516 tmp = alternative_order[best];
8517 alternative_order[best] = alternative_order[i];
8518 alternative_order[i] = tmp;
8521 /* Substitute the operands as determined by op_alt_regno for the best
8522 alternative. */
8523 j = alternative_order[0];
8525 for (i = 0; i < recog_data.n_operands; i++)
8527 enum machine_mode mode = recog_data.operand_mode[i];
8528 if (op_alt_regno[i][j] == -1)
8529 continue;
8531 validate_change (insn, recog_data.operand_loc[i],
8532 gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
8535 for (i = recog_data.n_dups - 1; i >= 0; i--)
8537 int op = recog_data.dup_num[i];
8538 enum machine_mode mode = recog_data.operand_mode[op];
8540 if (op_alt_regno[op][j] == -1)
8541 continue;
8543 validate_change (insn, recog_data.dup_loc[i],
8544 gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
8547 return apply_change_group ();
8550 /* If reload couldn't use reg+reg+offset addressing, try to use reg+reg
8551 addressing now.
8552 This code might also be useful when reload gave up on reg+reg addressing
8553 because of clashes between the return register and INDEX_REG_CLASS. */
8555 /* The maximum number of uses of a register we can keep track of to
8556 replace them with reg+reg addressing. */
8557 #define RELOAD_COMBINE_MAX_USES 6
8559 /* INSN is the insn where a register has ben used, and USEP points to the
8560 location of the register within the rtl. */
8561 struct reg_use { rtx insn, *usep; };
8563 /* If the register is used in some unknown fashion, USE_INDEX is negative.
8564 If it is dead, USE_INDEX is RELOAD_COMBINE_MAX_USES, and STORE_RUID
8565 indicates where it becomes live again.
8566 Otherwise, USE_INDEX is the index of the last encountered use of the
8567 register (which is first among these we have seen since we scan backwards),
8568 OFFSET contains the constant offset that is added to the register in
8569 all encountered uses, and USE_RUID indicates the first encountered, i.e.
8570 last, of these uses.
8571 STORE_RUID is always meaningful if we only want to use a value in a
8572 register in a different place: it denotes the next insn in the insn
8573 stream (i.e. the last encountered) that sets or clobbers the register. */
8574 static struct
8576 struct reg_use reg_use[RELOAD_COMBINE_MAX_USES];
8577 int use_index;
8578 rtx offset;
8579 int store_ruid;
8580 int use_ruid;
8581 } reg_state[FIRST_PSEUDO_REGISTER];
8583 /* Reverse linear uid. This is increased in reload_combine while scanning
8584 the instructions from last to first. It is used to set last_label_ruid
8585 and the store_ruid / use_ruid fields in reg_state. */
8586 static int reload_combine_ruid;
8588 #define LABEL_LIVE(LABEL) \
8589 (label_live[CODE_LABEL_NUMBER (LABEL) - min_labelno])
8591 static void
8592 reload_combine ()
8594 rtx insn, set;
8595 int first_index_reg = -1;
8596 int last_index_reg = 0;
8597 int i;
8598 basic_block bb;
8599 unsigned int r;
8600 int last_label_ruid;
8601 int min_labelno, n_labels;
8602 HARD_REG_SET ever_live_at_start, *label_live;
8604 /* If reg+reg can be used in offsetable memory addresses, the main chunk of
8605 reload has already used it where appropriate, so there is no use in
8606 trying to generate it now. */
8607 if (double_reg_address_ok && INDEX_REG_CLASS != NO_REGS)
8608 return;
8610 /* To avoid wasting too much time later searching for an index register,
8611 determine the minimum and maximum index register numbers. */
8612 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
8613 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], r))
8615 if (first_index_reg == -1)
8616 first_index_reg = r;
8618 last_index_reg = r;
8621 /* If no index register is available, we can quit now. */
8622 if (first_index_reg == -1)
8623 return;
8625 /* Set up LABEL_LIVE and EVER_LIVE_AT_START. The register lifetime
8626 information is a bit fuzzy immediately after reload, but it's
8627 still good enough to determine which registers are live at a jump
8628 destination. */
8629 min_labelno = get_first_label_num ();
8630 n_labels = max_label_num () - min_labelno;
8631 label_live = (HARD_REG_SET *) xmalloc (n_labels * sizeof (HARD_REG_SET));
8632 CLEAR_HARD_REG_SET (ever_live_at_start);
8634 FOR_EACH_BB_REVERSE (bb)
8636 insn = bb->head;
8637 if (GET_CODE (insn) == CODE_LABEL)
8639 HARD_REG_SET live;
8641 REG_SET_TO_HARD_REG_SET (live,
8642 bb->global_live_at_start);
8643 compute_use_by_pseudos (&live,
8644 bb->global_live_at_start);
8645 COPY_HARD_REG_SET (LABEL_LIVE (insn), live);
8646 IOR_HARD_REG_SET (ever_live_at_start, live);
8650 /* Initialize last_label_ruid, reload_combine_ruid and reg_state. */
8651 last_label_ruid = reload_combine_ruid = 0;
8652 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
8654 reg_state[r].store_ruid = reload_combine_ruid;
8655 if (fixed_regs[r])
8656 reg_state[r].use_index = -1;
8657 else
8658 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
8661 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
8663 rtx note;
8665 /* We cannot do our optimization across labels. Invalidating all the use
8666 information we have would be costly, so we just note where the label
8667 is and then later disable any optimization that would cross it. */
8668 if (GET_CODE (insn) == CODE_LABEL)
8669 last_label_ruid = reload_combine_ruid;
8670 else if (GET_CODE (insn) == BARRIER)
8671 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
8672 if (! fixed_regs[r])
8673 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
8675 if (! INSN_P (insn))
8676 continue;
8678 reload_combine_ruid++;
8680 /* Look for (set (REGX) (CONST_INT))
8681 (set (REGX) (PLUS (REGX) (REGY)))
8683 ... (MEM (REGX)) ...
8684 and convert it to
8685 (set (REGZ) (CONST_INT))
8687 ... (MEM (PLUS (REGZ) (REGY)))... .
8689 First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
8690 and that we know all uses of REGX before it dies. */
8691 set = single_set (insn);
8692 if (set != NULL_RTX
8693 && GET_CODE (SET_DEST (set)) == REG
8694 && (HARD_REGNO_NREGS (REGNO (SET_DEST (set)),
8695 GET_MODE (SET_DEST (set)))
8696 == 1)
8697 && GET_CODE (SET_SRC (set)) == PLUS
8698 && GET_CODE (XEXP (SET_SRC (set), 1)) == REG
8699 && rtx_equal_p (XEXP (SET_SRC (set), 0), SET_DEST (set))
8700 && last_label_ruid < reg_state[REGNO (SET_DEST (set))].use_ruid)
8702 rtx reg = SET_DEST (set);
8703 rtx plus = SET_SRC (set);
8704 rtx base = XEXP (plus, 1);
8705 rtx prev = prev_nonnote_insn (insn);
8706 rtx prev_set = prev ? single_set (prev) : NULL_RTX;
8707 unsigned int regno = REGNO (reg);
8708 rtx const_reg = NULL_RTX;
8709 rtx reg_sum = NULL_RTX;
8711 /* Now, we need an index register.
8712 We'll set index_reg to this index register, const_reg to the
8713 register that is to be loaded with the constant
8714 (denoted as REGZ in the substitution illustration above),
8715 and reg_sum to the register-register that we want to use to
8716 substitute uses of REG (typically in MEMs) with.
8717 First check REG and BASE for being index registers;
8718 we can use them even if they are not dead. */
8719 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], regno)
8720 || TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
8721 REGNO (base)))
8723 const_reg = reg;
8724 reg_sum = plus;
8726 else
8728 /* Otherwise, look for a free index register. Since we have
8729 checked above that neiter REG nor BASE are index registers,
8730 if we find anything at all, it will be different from these
8731 two registers. */
8732 for (i = first_index_reg; i <= last_index_reg; i++)
8734 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
8736 && reg_state[i].use_index == RELOAD_COMBINE_MAX_USES
8737 && reg_state[i].store_ruid <= reg_state[regno].use_ruid
8738 && HARD_REGNO_NREGS (i, GET_MODE (reg)) == 1)
8740 rtx index_reg = gen_rtx_REG (GET_MODE (reg), i);
8742 const_reg = index_reg;
8743 reg_sum = gen_rtx_PLUS (GET_MODE (reg), index_reg, base);
8744 break;
8749 /* Check that PREV_SET is indeed (set (REGX) (CONST_INT)) and that
8750 (REGY), i.e. BASE, is not clobbered before the last use we'll
8751 create. */
8752 if (prev_set != 0
8753 && GET_CODE (SET_SRC (prev_set)) == CONST_INT
8754 && rtx_equal_p (SET_DEST (prev_set), reg)
8755 && reg_state[regno].use_index >= 0
8756 && (reg_state[REGNO (base)].store_ruid
8757 <= reg_state[regno].use_ruid)
8758 && reg_sum != 0)
8760 int i;
8762 /* Change destination register and, if necessary, the
8763 constant value in PREV, the constant loading instruction. */
8764 validate_change (prev, &SET_DEST (prev_set), const_reg, 1);
8765 if (reg_state[regno].offset != const0_rtx)
8766 validate_change (prev,
8767 &SET_SRC (prev_set),
8768 GEN_INT (INTVAL (SET_SRC (prev_set))
8769 + INTVAL (reg_state[regno].offset)),
8772 /* Now for every use of REG that we have recorded, replace REG
8773 with REG_SUM. */
8774 for (i = reg_state[regno].use_index;
8775 i < RELOAD_COMBINE_MAX_USES; i++)
8776 validate_change (reg_state[regno].reg_use[i].insn,
8777 reg_state[regno].reg_use[i].usep,
8778 /* Each change must have its own
8779 replacement. */
8780 copy_rtx (reg_sum), 1);
8782 if (apply_change_group ())
8784 rtx *np;
8786 /* Delete the reg-reg addition. */
8787 delete_insn (insn);
8789 if (reg_state[regno].offset != const0_rtx)
8790 /* Previous REG_EQUIV / REG_EQUAL notes for PREV
8791 are now invalid. */
8792 for (np = &REG_NOTES (prev); *np;)
8794 if (REG_NOTE_KIND (*np) == REG_EQUAL
8795 || REG_NOTE_KIND (*np) == REG_EQUIV)
8796 *np = XEXP (*np, 1);
8797 else
8798 np = &XEXP (*np, 1);
8801 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
8802 reg_state[REGNO (const_reg)].store_ruid
8803 = reload_combine_ruid;
8804 continue;
8809 note_stores (PATTERN (insn), reload_combine_note_store, NULL);
8811 if (GET_CODE (insn) == CALL_INSN)
8813 rtx link;
8815 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
8816 if (call_used_regs[r])
8818 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
8819 reg_state[r].store_ruid = reload_combine_ruid;
8822 for (link = CALL_INSN_FUNCTION_USAGE (insn); link;
8823 link = XEXP (link, 1))
8825 rtx usage_rtx = XEXP (XEXP (link, 0), 0);
8826 if (GET_CODE (usage_rtx) == REG)
8828 unsigned int i;
8829 unsigned int start_reg = REGNO (usage_rtx);
8830 unsigned int num_regs =
8831 HARD_REGNO_NREGS (start_reg, GET_MODE (usage_rtx));
8832 unsigned int end_reg = start_reg + num_regs - 1;
8833 for (i = start_reg; i <= end_reg; i++)
8834 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
8836 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
8837 reg_state[i].store_ruid = reload_combine_ruid;
8839 else
8840 reg_state[i].use_index = -1;
8845 else if (GET_CODE (insn) == JUMP_INSN
8846 && GET_CODE (PATTERN (insn)) != RETURN)
8848 /* Non-spill registers might be used at the call destination in
8849 some unknown fashion, so we have to mark the unknown use. */
8850 HARD_REG_SET *live;
8852 if ((condjump_p (insn) || condjump_in_parallel_p (insn))
8853 && JUMP_LABEL (insn))
8854 live = &LABEL_LIVE (JUMP_LABEL (insn));
8855 else
8856 live = &ever_live_at_start;
8858 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
8859 if (TEST_HARD_REG_BIT (*live, i))
8860 reg_state[i].use_index = -1;
8863 reload_combine_note_use (&PATTERN (insn), insn);
8864 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
8866 if (REG_NOTE_KIND (note) == REG_INC
8867 && GET_CODE (XEXP (note, 0)) == REG)
8869 int regno = REGNO (XEXP (note, 0));
8871 reg_state[regno].store_ruid = reload_combine_ruid;
8872 reg_state[regno].use_index = -1;
8877 free (label_live);
8880 /* Check if DST is a register or a subreg of a register; if it is,
8881 update reg_state[regno].store_ruid and reg_state[regno].use_index
8882 accordingly. Called via note_stores from reload_combine. */
8884 static void
8885 reload_combine_note_store (dst, set, data)
8886 rtx dst, set;
8887 void *data ATTRIBUTE_UNUSED;
8889 int regno = 0;
8890 int i;
8891 enum machine_mode mode = GET_MODE (dst);
8893 if (GET_CODE (dst) == SUBREG)
8895 regno = subreg_regno_offset (REGNO (SUBREG_REG (dst)),
8896 GET_MODE (SUBREG_REG (dst)),
8897 SUBREG_BYTE (dst),
8898 GET_MODE (dst));
8899 dst = SUBREG_REG (dst);
8901 if (GET_CODE (dst) != REG)
8902 return;
8903 regno += REGNO (dst);
8905 /* note_stores might have stripped a STRICT_LOW_PART, so we have to be
8906 careful with registers / register parts that are not full words.
8908 Similarly for ZERO_EXTRACT and SIGN_EXTRACT. */
8909 if (GET_CODE (set) != SET
8910 || GET_CODE (SET_DEST (set)) == ZERO_EXTRACT
8911 || GET_CODE (SET_DEST (set)) == SIGN_EXTRACT
8912 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
8914 for (i = HARD_REGNO_NREGS (regno, mode) - 1 + regno; i >= regno; i--)
8916 reg_state[i].use_index = -1;
8917 reg_state[i].store_ruid = reload_combine_ruid;
8920 else
8922 for (i = HARD_REGNO_NREGS (regno, mode) - 1 + regno; i >= regno; i--)
8924 reg_state[i].store_ruid = reload_combine_ruid;
8925 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
8930 /* XP points to a piece of rtl that has to be checked for any uses of
8931 registers.
8932 *XP is the pattern of INSN, or a part of it.
8933 Called from reload_combine, and recursively by itself. */
8934 static void
8935 reload_combine_note_use (xp, insn)
8936 rtx *xp, insn;
8938 rtx x = *xp;
8939 enum rtx_code code = x->code;
8940 const char *fmt;
8941 int i, j;
8942 rtx offset = const0_rtx; /* For the REG case below. */
8944 switch (code)
8946 case SET:
8947 if (GET_CODE (SET_DEST (x)) == REG)
8949 reload_combine_note_use (&SET_SRC (x), insn);
8950 return;
8952 break;
8954 case USE:
8955 /* If this is the USE of a return value, we can't change it. */
8956 if (GET_CODE (XEXP (x, 0)) == REG && REG_FUNCTION_VALUE_P (XEXP (x, 0)))
8958 /* Mark the return register as used in an unknown fashion. */
8959 rtx reg = XEXP (x, 0);
8960 int regno = REGNO (reg);
8961 int nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg));
8963 while (--nregs >= 0)
8964 reg_state[regno + nregs].use_index = -1;
8965 return;
8967 break;
8969 case CLOBBER:
8970 if (GET_CODE (SET_DEST (x)) == REG)
8972 /* No spurious CLOBBERs of pseudo registers may remain. */
8973 if (REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER)
8974 abort ();
8975 return;
8977 break;
8979 case PLUS:
8980 /* We are interested in (plus (reg) (const_int)) . */
8981 if (GET_CODE (XEXP (x, 0)) != REG
8982 || GET_CODE (XEXP (x, 1)) != CONST_INT)
8983 break;
8984 offset = XEXP (x, 1);
8985 x = XEXP (x, 0);
8986 /* Fall through. */
8987 case REG:
8989 int regno = REGNO (x);
8990 int use_index;
8991 int nregs;
8993 /* No spurious USEs of pseudo registers may remain. */
8994 if (regno >= FIRST_PSEUDO_REGISTER)
8995 abort ();
8997 nregs = HARD_REGNO_NREGS (regno, GET_MODE (x));
8999 /* We can't substitute into multi-hard-reg uses. */
9000 if (nregs > 1)
9002 while (--nregs >= 0)
9003 reg_state[regno + nregs].use_index = -1;
9004 return;
9007 /* If this register is already used in some unknown fashion, we
9008 can't do anything.
9009 If we decrement the index from zero to -1, we can't store more
9010 uses, so this register becomes used in an unknown fashion. */
9011 use_index = --reg_state[regno].use_index;
9012 if (use_index < 0)
9013 return;
9015 if (use_index != RELOAD_COMBINE_MAX_USES - 1)
9017 /* We have found another use for a register that is already
9018 used later. Check if the offsets match; if not, mark the
9019 register as used in an unknown fashion. */
9020 if (! rtx_equal_p (offset, reg_state[regno].offset))
9022 reg_state[regno].use_index = -1;
9023 return;
9026 else
9028 /* This is the first use of this register we have seen since we
9029 marked it as dead. */
9030 reg_state[regno].offset = offset;
9031 reg_state[regno].use_ruid = reload_combine_ruid;
9033 reg_state[regno].reg_use[use_index].insn = insn;
9034 reg_state[regno].reg_use[use_index].usep = xp;
9035 return;
9038 default:
9039 break;
9042 /* Recursively process the components of X. */
9043 fmt = GET_RTX_FORMAT (code);
9044 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9046 if (fmt[i] == 'e')
9047 reload_combine_note_use (&XEXP (x, i), insn);
9048 else if (fmt[i] == 'E')
9050 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9051 reload_combine_note_use (&XVECEXP (x, i, j), insn);
9056 /* See if we can reduce the cost of a constant by replacing a move
9057 with an add. We track situations in which a register is set to a
9058 constant or to a register plus a constant. */
9059 /* We cannot do our optimization across labels. Invalidating all the
9060 information about register contents we have would be costly, so we
9061 use move2add_last_label_luid to note where the label is and then
9062 later disable any optimization that would cross it.
9063 reg_offset[n] / reg_base_reg[n] / reg_mode[n] are only valid if
9064 reg_set_luid[n] is greater than last_label_luid[n] . */
9065 static int reg_set_luid[FIRST_PSEUDO_REGISTER];
9067 /* If reg_base_reg[n] is negative, register n has been set to
9068 reg_offset[n] in mode reg_mode[n] .
9069 If reg_base_reg[n] is non-negative, register n has been set to the
9070 sum of reg_offset[n] and the value of register reg_base_reg[n]
9071 before reg_set_luid[n], calculated in mode reg_mode[n] . */
9072 static HOST_WIDE_INT reg_offset[FIRST_PSEUDO_REGISTER];
9073 static int reg_base_reg[FIRST_PSEUDO_REGISTER];
9074 static enum machine_mode reg_mode[FIRST_PSEUDO_REGISTER];
9076 /* move2add_luid is linearly increased while scanning the instructions
9077 from first to last. It is used to set reg_set_luid in
9078 reload_cse_move2add and move2add_note_store. */
9079 static int move2add_luid;
9081 /* move2add_last_label_luid is set whenever a label is found. Labels
9082 invalidate all previously collected reg_offset data. */
9083 static int move2add_last_label_luid;
9085 /* ??? We don't know how zero / sign extension is handled, hence we
9086 can't go from a narrower to a wider mode. */
9087 #define MODES_OK_FOR_MOVE2ADD(OUTMODE, INMODE) \
9088 (GET_MODE_SIZE (OUTMODE) == GET_MODE_SIZE (INMODE) \
9089 || (GET_MODE_SIZE (OUTMODE) <= GET_MODE_SIZE (INMODE) \
9090 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (OUTMODE), \
9091 GET_MODE_BITSIZE (INMODE))))
9093 static void
9094 reload_cse_move2add (first)
9095 rtx first;
9097 int i;
9098 rtx insn;
9100 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
9101 reg_set_luid[i] = 0;
9103 move2add_last_label_luid = 0;
9104 move2add_luid = 2;
9105 for (insn = first; insn; insn = NEXT_INSN (insn), move2add_luid++)
9107 rtx pat, note;
9109 if (GET_CODE (insn) == CODE_LABEL)
9111 move2add_last_label_luid = move2add_luid;
9112 /* We're going to increment move2add_luid twice after a
9113 label, so that we can use move2add_last_label_luid + 1 as
9114 the luid for constants. */
9115 move2add_luid++;
9116 continue;
9118 if (! INSN_P (insn))
9119 continue;
9120 pat = PATTERN (insn);
9121 /* For simplicity, we only perform this optimization on
9122 straightforward SETs. */
9123 if (GET_CODE (pat) == SET
9124 && GET_CODE (SET_DEST (pat)) == REG)
9126 rtx reg = SET_DEST (pat);
9127 int regno = REGNO (reg);
9128 rtx src = SET_SRC (pat);
9130 /* Check if we have valid information on the contents of this
9131 register in the mode of REG. */
9132 if (reg_set_luid[regno] > move2add_last_label_luid
9133 && MODES_OK_FOR_MOVE2ADD (GET_MODE (reg), reg_mode[regno]))
9135 /* Try to transform (set (REGX) (CONST_INT A))
9137 (set (REGX) (CONST_INT B))
9139 (set (REGX) (CONST_INT A))
9141 (set (REGX) (plus (REGX) (CONST_INT B-A)))
9143 (set (REGX) (CONST_INT A))
9145 (set (STRICT_LOW_PART (REGX)) (CONST_INT B))
9148 if (GET_CODE (src) == CONST_INT && reg_base_reg[regno] < 0)
9150 int success = 0;
9151 rtx new_src =
9152 GEN_INT (trunc_int_for_mode (INTVAL (src)
9153 - reg_offset[regno],
9154 GET_MODE (reg)));
9155 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
9156 use (set (reg) (reg)) instead.
9157 We don't delete this insn, nor do we convert it into a
9158 note, to avoid losing register notes or the return
9159 value flag. jump2 already knows how to get rid of
9160 no-op moves. */
9161 if (new_src == const0_rtx)
9162 success = validate_change (insn, &SET_SRC (pat), reg, 0);
9163 else if (rtx_cost (new_src, PLUS) < rtx_cost (src, SET)
9164 && have_add2_insn (reg, new_src))
9165 success = validate_change (insn, &PATTERN (insn),
9166 gen_add2_insn (reg, new_src), 0);
9167 else
9169 enum machine_mode narrow_mode;
9170 for (narrow_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9171 narrow_mode != GET_MODE (reg);
9172 narrow_mode = GET_MODE_WIDER_MODE (narrow_mode))
9174 if (have_insn_for (STRICT_LOW_PART, narrow_mode)
9175 && ((reg_offset[regno]
9176 & ~GET_MODE_MASK (narrow_mode))
9177 == (INTVAL (src)
9178 & ~GET_MODE_MASK (narrow_mode))))
9180 rtx narrow_reg = gen_rtx_REG (narrow_mode,
9181 REGNO (reg));
9182 rtx narrow_src =
9183 GEN_INT (trunc_int_for_mode (INTVAL (src),
9184 narrow_mode));
9185 rtx new_set =
9186 gen_rtx_SET (VOIDmode,
9187 gen_rtx_STRICT_LOW_PART (VOIDmode,
9188 narrow_reg),
9189 narrow_src);
9190 success = validate_change (insn, &PATTERN (insn),
9191 new_set, 0);
9192 if (success)
9193 break;
9197 reg_set_luid[regno] = move2add_luid;
9198 reg_mode[regno] = GET_MODE (reg);
9199 reg_offset[regno] = INTVAL (src);
9200 continue;
9203 /* Try to transform (set (REGX) (REGY))
9204 (set (REGX) (PLUS (REGX) (CONST_INT A)))
9206 (set (REGX) (REGY))
9207 (set (REGX) (PLUS (REGX) (CONST_INT B)))
9209 (REGX) (REGY))
9210 (set (REGX) (PLUS (REGX) (CONST_INT A)))
9212 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
9213 else if (GET_CODE (src) == REG
9214 && reg_set_luid[regno] == reg_set_luid[REGNO (src)]
9215 && reg_base_reg[regno] == reg_base_reg[REGNO (src)]
9216 && MODES_OK_FOR_MOVE2ADD (GET_MODE (reg),
9217 reg_mode[REGNO (src)]))
9219 rtx next = next_nonnote_insn (insn);
9220 rtx set = NULL_RTX;
9221 if (next)
9222 set = single_set (next);
9223 if (set
9224 && SET_DEST (set) == reg
9225 && GET_CODE (SET_SRC (set)) == PLUS
9226 && XEXP (SET_SRC (set), 0) == reg
9227 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
9229 rtx src3 = XEXP (SET_SRC (set), 1);
9230 HOST_WIDE_INT added_offset = INTVAL (src3);
9231 HOST_WIDE_INT base_offset = reg_offset[REGNO (src)];
9232 HOST_WIDE_INT regno_offset = reg_offset[regno];
9233 rtx new_src =
9234 GEN_INT (trunc_int_for_mode (added_offset
9235 + base_offset
9236 - regno_offset,
9237 GET_MODE (reg)));
9238 int success = 0;
9240 if (new_src == const0_rtx)
9241 /* See above why we create (set (reg) (reg)) here. */
9242 success
9243 = validate_change (next, &SET_SRC (set), reg, 0);
9244 else if ((rtx_cost (new_src, PLUS)
9245 < COSTS_N_INSNS (1) + rtx_cost (src3, SET))
9246 && have_add2_insn (reg, new_src))
9247 success
9248 = validate_change (next, &PATTERN (next),
9249 gen_add2_insn (reg, new_src), 0);
9250 if (success)
9251 delete_insn (insn);
9252 insn = next;
9253 reg_mode[regno] = GET_MODE (reg);
9254 reg_offset[regno] =
9255 trunc_int_for_mode (added_offset + base_offset,
9256 GET_MODE (reg));
9257 continue;
9263 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
9265 if (REG_NOTE_KIND (note) == REG_INC
9266 && GET_CODE (XEXP (note, 0)) == REG)
9268 /* Reset the information about this register. */
9269 int regno = REGNO (XEXP (note, 0));
9270 if (regno < FIRST_PSEUDO_REGISTER)
9271 reg_set_luid[regno] = 0;
9274 note_stores (PATTERN (insn), move2add_note_store, NULL);
9275 /* If this is a CALL_INSN, all call used registers are stored with
9276 unknown values. */
9277 if (GET_CODE (insn) == CALL_INSN)
9279 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
9281 if (call_used_regs[i])
9282 /* Reset the information about this register. */
9283 reg_set_luid[i] = 0;
9289 /* SET is a SET or CLOBBER that sets DST.
9290 Update reg_set_luid, reg_offset and reg_base_reg accordingly.
9291 Called from reload_cse_move2add via note_stores. */
9293 static void
9294 move2add_note_store (dst, set, data)
9295 rtx dst, set;
9296 void *data ATTRIBUTE_UNUSED;
9298 unsigned int regno = 0;
9299 unsigned int i;
9300 enum machine_mode mode = GET_MODE (dst);
9302 if (GET_CODE (dst) == SUBREG)
9304 regno = subreg_regno_offset (REGNO (SUBREG_REG (dst)),
9305 GET_MODE (SUBREG_REG (dst)),
9306 SUBREG_BYTE (dst),
9307 GET_MODE (dst));
9308 dst = SUBREG_REG (dst);
9311 /* Some targets do argument pushes without adding REG_INC notes. */
9313 if (GET_CODE (dst) == MEM)
9315 dst = XEXP (dst, 0);
9316 if (GET_CODE (dst) == PRE_INC || GET_CODE (dst) == POST_INC
9317 || GET_CODE (dst) == PRE_DEC || GET_CODE (dst) == POST_DEC)
9318 reg_set_luid[REGNO (XEXP (dst, 0))] = 0;
9319 return;
9321 if (GET_CODE (dst) != REG)
9322 return;
9324 regno += REGNO (dst);
9326 if (SCALAR_INT_MODE_P (mode)
9327 && HARD_REGNO_NREGS (regno, mode) == 1 && GET_CODE (set) == SET
9328 && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
9329 && GET_CODE (SET_DEST (set)) != SIGN_EXTRACT
9330 && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
9332 rtx src = SET_SRC (set);
9333 rtx base_reg;
9334 HOST_WIDE_INT offset;
9335 int base_regno;
9336 /* This may be different from mode, if SET_DEST (set) is a
9337 SUBREG. */
9338 enum machine_mode dst_mode = GET_MODE (dst);
9340 switch (GET_CODE (src))
9342 case PLUS:
9343 if (GET_CODE (XEXP (src, 0)) == REG)
9345 base_reg = XEXP (src, 0);
9347 if (GET_CODE (XEXP (src, 1)) == CONST_INT)
9348 offset = INTVAL (XEXP (src, 1));
9349 else if (GET_CODE (XEXP (src, 1)) == REG
9350 && (reg_set_luid[REGNO (XEXP (src, 1))]
9351 > move2add_last_label_luid)
9352 && (MODES_OK_FOR_MOVE2ADD
9353 (dst_mode, reg_mode[REGNO (XEXP (src, 1))])))
9355 if (reg_base_reg[REGNO (XEXP (src, 1))] < 0)
9356 offset = reg_offset[REGNO (XEXP (src, 1))];
9357 /* Maybe the first register is known to be a
9358 constant. */
9359 else if (reg_set_luid[REGNO (base_reg)]
9360 > move2add_last_label_luid
9361 && (MODES_OK_FOR_MOVE2ADD
9362 (dst_mode, reg_mode[REGNO (XEXP (src, 1))]))
9363 && reg_base_reg[REGNO (base_reg)] < 0)
9365 offset = reg_offset[REGNO (base_reg)];
9366 base_reg = XEXP (src, 1);
9368 else
9369 goto invalidate;
9371 else
9372 goto invalidate;
9374 break;
9377 goto invalidate;
9379 case REG:
9380 base_reg = src;
9381 offset = 0;
9382 break;
9384 case CONST_INT:
9385 /* Start tracking the register as a constant. */
9386 reg_base_reg[regno] = -1;
9387 reg_offset[regno] = INTVAL (SET_SRC (set));
9388 /* We assign the same luid to all registers set to constants. */
9389 reg_set_luid[regno] = move2add_last_label_luid + 1;
9390 reg_mode[regno] = mode;
9391 return;
9393 default:
9394 invalidate:
9395 /* Invalidate the contents of the register. */
9396 reg_set_luid[regno] = 0;
9397 return;
9400 base_regno = REGNO (base_reg);
9401 /* If information about the base register is not valid, set it
9402 up as a new base register, pretending its value is known
9403 starting from the current insn. */
9404 if (reg_set_luid[base_regno] <= move2add_last_label_luid)
9406 reg_base_reg[base_regno] = base_regno;
9407 reg_offset[base_regno] = 0;
9408 reg_set_luid[base_regno] = move2add_luid;
9409 reg_mode[base_regno] = mode;
9411 else if (! MODES_OK_FOR_MOVE2ADD (dst_mode,
9412 reg_mode[base_regno]))
9413 goto invalidate;
9415 reg_mode[regno] = mode;
9417 /* Copy base information from our base register. */
9418 reg_set_luid[regno] = reg_set_luid[base_regno];
9419 reg_base_reg[regno] = reg_base_reg[base_regno];
9421 /* Compute the sum of the offsets or constants. */
9422 reg_offset[regno] = trunc_int_for_mode (offset
9423 + reg_offset[base_regno],
9424 dst_mode);
9426 else
9428 unsigned int endregno = regno + HARD_REGNO_NREGS (regno, mode);
9430 for (i = regno; i < endregno; i++)
9431 /* Reset the information about this register. */
9432 reg_set_luid[i] = 0;
9436 #ifdef AUTO_INC_DEC
9437 static void
9438 add_auto_inc_notes (insn, x)
9439 rtx insn;
9440 rtx x;
9442 enum rtx_code code = GET_CODE (x);
9443 const char *fmt;
9444 int i, j;
9446 if (code == MEM && auto_inc_p (XEXP (x, 0)))
9448 REG_NOTES (insn)
9449 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
9450 return;
9453 /* Scan all the operand sub-expressions. */
9454 fmt = GET_RTX_FORMAT (code);
9455 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9457 if (fmt[i] == 'e')
9458 add_auto_inc_notes (insn, XEXP (x, i));
9459 else if (fmt[i] == 'E')
9460 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9461 add_auto_inc_notes (insn, XVECEXP (x, i, j));
9464 #endif
9466 /* Copy EH notes from an insn to its reloads. */
9467 static void
9468 copy_eh_notes (insn, x)
9469 rtx insn;
9470 rtx x;
9472 rtx eh_note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
9473 if (eh_note)
9475 for (; x != 0; x = NEXT_INSN (x))
9477 if (may_trap_p (PATTERN (x)))
9478 REG_NOTES (x)
9479 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (eh_note, 0),
9480 REG_NOTES (x));
9485 /* This is used by reload pass, that does emit some instructions after
9486 abnormal calls moving basic block end, but in fact it wants to emit
9487 them on the edge. Looks for abnormal call edges, find backward the
9488 proper call and fix the damage.
9490 Similar handle instructions throwing exceptions internally. */
9491 void
9492 fixup_abnormal_edges ()
9494 bool inserted = false;
9495 basic_block bb;
9497 FOR_EACH_BB (bb)
9499 edge e;
9501 /* Look for cases we are interested in - calls or instructions causing
9502 exceptions. */
9503 for (e = bb->succ; e; e = e->succ_next)
9505 if (e->flags & EDGE_ABNORMAL_CALL)
9506 break;
9507 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
9508 == (EDGE_ABNORMAL | EDGE_EH))
9509 break;
9511 if (e && GET_CODE (bb->end) != CALL_INSN && !can_throw_internal (bb->end))
9513 rtx insn = bb->end, stop = NEXT_INSN (bb->end);
9514 rtx next;
9515 for (e = bb->succ; e; e = e->succ_next)
9516 if (e->flags & EDGE_FALLTHRU)
9517 break;
9518 /* Get past the new insns generated. Allow notes, as the insns may
9519 be already deleted. */
9520 while ((GET_CODE (insn) == INSN || GET_CODE (insn) == NOTE)
9521 && !can_throw_internal (insn)
9522 && insn != bb->head)
9523 insn = PREV_INSN (insn);
9524 if (GET_CODE (insn) != CALL_INSN && !can_throw_internal (insn))
9525 abort ();
9526 bb->end = insn;
9527 inserted = true;
9528 insn = NEXT_INSN (insn);
9529 while (insn && insn != stop)
9531 next = NEXT_INSN (insn);
9532 if (INSN_P (insn))
9534 delete_insn (insn);
9536 /* Sometimes there's still the return value USE.
9537 If it's placed after a trapping call (i.e. that
9538 call is the last insn anyway), we have no fallthru
9539 edge. Simply delete this use and don't try to insert
9540 on the non-existent edge. */
9541 if (GET_CODE (PATTERN (insn)) != USE)
9543 /* We're not deleting it, we're moving it. */
9544 INSN_DELETED_P (insn) = 0;
9545 PREV_INSN (insn) = NULL_RTX;
9546 NEXT_INSN (insn) = NULL_RTX;
9548 insert_insn_on_edge (insn, e);
9551 insn = next;
9555 /* We've possibly turned single trapping insn into multiple ones. */
9556 if (flag_non_call_exceptions)
9558 sbitmap blocks;
9559 blocks = sbitmap_alloc (last_basic_block);
9560 sbitmap_ones (blocks);
9561 find_many_sub_basic_blocks (blocks);
9563 if (inserted)
9564 commit_edge_insertions ();