* class.c (check_bitfield_decl): New function, split out from
[official-gcc.git] / gcc / reload1.c
blob5291d9bf3e756c0a54d806900a0260eb971c503b
1 /* Reload pseudo regs into hard regs for insns that require hard regs.
2 Copyright (C) 1987, 88, 89, 92-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
25 #include "machmode.h"
26 #include "hard-reg-set.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "obstack.h"
30 #include "insn-config.h"
31 #include "insn-flags.h"
32 #include "insn-codes.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "regs.h"
37 #include "basic-block.h"
38 #include "reload.h"
39 #include "recog.h"
40 #include "output.h"
41 #include "real.h"
42 #include "toplev.h"
44 #if !defined PREFERRED_STACK_BOUNDARY && defined STACK_BOUNDARY
45 #define PREFERRED_STACK_BOUNDARY STACK_BOUNDARY
46 #endif
48 /* This file contains the reload pass of the compiler, which is
49 run after register allocation has been done. It checks that
50 each insn is valid (operands required to be in registers really
51 are in registers of the proper class) and fixes up invalid ones
52 by copying values temporarily into registers for the insns
53 that need them.
55 The results of register allocation are described by the vector
56 reg_renumber; the insns still contain pseudo regs, but reg_renumber
57 can be used to find which hard reg, if any, a pseudo reg is in.
59 The technique we always use is to free up a few hard regs that are
60 called ``reload regs'', and for each place where a pseudo reg
61 must be in a hard reg, copy it temporarily into one of the reload regs.
63 Reload regs are allocated locally for every instruction that needs
64 reloads. When there are pseudos which are allocated to a register that
65 has been chosen as a reload reg, such pseudos must be ``spilled''.
66 This means that they go to other hard regs, or to stack slots if no other
67 available hard regs can be found. Spilling can invalidate more
68 insns, requiring additional need for reloads, so we must keep checking
69 until the process stabilizes.
71 For machines with different classes of registers, we must keep track
72 of the register class needed for each reload, and make sure that
73 we allocate enough reload registers of each class.
75 The file reload.c contains the code that checks one insn for
76 validity and reports the reloads that it needs. This file
77 is in charge of scanning the entire rtl code, accumulating the
78 reload needs, spilling, assigning reload registers to use for
79 fixing up each insn, and generating the new insns to copy values
80 into the reload registers. */
83 #ifndef REGISTER_MOVE_COST
84 #define REGISTER_MOVE_COST(x, y) 2
85 #endif
87 /* During reload_as_needed, element N contains a REG rtx for the hard reg
88 into which reg N has been reloaded (perhaps for a previous insn). */
89 static rtx *reg_last_reload_reg;
91 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
92 for an output reload that stores into reg N. */
93 static char *reg_has_output_reload;
95 /* Indicates which hard regs are reload-registers for an output reload
96 in the current insn. */
97 static HARD_REG_SET reg_is_output_reload;
99 /* Element N is the constant value to which pseudo reg N is equivalent,
100 or zero if pseudo reg N is not equivalent to a constant.
101 find_reloads looks at this in order to replace pseudo reg N
102 with the constant it stands for. */
103 rtx *reg_equiv_constant;
105 /* Element N is a memory location to which pseudo reg N is equivalent,
106 prior to any register elimination (such as frame pointer to stack
107 pointer). Depending on whether or not it is a valid address, this value
108 is transferred to either reg_equiv_address or reg_equiv_mem. */
109 rtx *reg_equiv_memory_loc;
111 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
112 This is used when the address is not valid as a memory address
113 (because its displacement is too big for the machine.) */
114 rtx *reg_equiv_address;
116 /* Element N is the memory slot to which pseudo reg N is equivalent,
117 or zero if pseudo reg N is not equivalent to a memory slot. */
118 rtx *reg_equiv_mem;
120 /* Widest width in which each pseudo reg is referred to (via subreg). */
121 static int *reg_max_ref_width;
123 /* Element N is the list of insns that initialized reg N from its equivalent
124 constant or memory slot. */
125 static rtx *reg_equiv_init;
127 /* Vector to remember old contents of reg_renumber before spilling. */
128 static short *reg_old_renumber;
130 /* During reload_as_needed, element N contains the last pseudo regno reloaded
131 into hard register N. If that pseudo reg occupied more than one register,
132 reg_reloaded_contents points to that pseudo for each spill register in
133 use; all of these must remain set for an inheritance to occur. */
134 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
136 /* During reload_as_needed, element N contains the insn for which
137 hard register N was last used. Its contents are significant only
138 when reg_reloaded_valid is set for this register. */
139 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
141 /* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid */
142 static HARD_REG_SET reg_reloaded_valid;
143 /* Indicate if the register was dead at the end of the reload.
144 This is only valid if reg_reloaded_contents is set and valid. */
145 static HARD_REG_SET reg_reloaded_dead;
147 /* Number of spill-regs so far; number of valid elements of spill_regs. */
148 static int n_spills;
150 /* In parallel with spill_regs, contains REG rtx's for those regs.
151 Holds the last rtx used for any given reg, or 0 if it has never
152 been used for spilling yet. This rtx is reused, provided it has
153 the proper mode. */
154 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
156 /* In parallel with spill_regs, contains nonzero for a spill reg
157 that was stored after the last time it was used.
158 The precise value is the insn generated to do the store. */
159 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
161 /* This is the register that was stored with spill_reg_store. This is a
162 copy of reload_out / reload_out_reg when the value was stored; if
163 reload_out is a MEM, spill_reg_stored_to will be set to reload_out_reg. */
164 static rtx spill_reg_stored_to[FIRST_PSEUDO_REGISTER];
166 /* This table is the inverse mapping of spill_regs:
167 indexed by hard reg number,
168 it contains the position of that reg in spill_regs,
169 or -1 for something that is not in spill_regs.
171 ?!? This is no longer accurate. */
172 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
174 /* This reg set indicates registers that can't be used as spill registers for
175 the currently processed insn. These are the hard registers which are live
176 during the insn, but not allocated to pseudos, as well as fixed
177 registers. */
178 static HARD_REG_SET bad_spill_regs;
180 /* These are the hard registers that can't be used as spill register for any
181 insn. This includes registers used for user variables and registers that
182 we can't eliminate. A register that appears in this set also can't be used
183 to retry register allocation. */
184 static HARD_REG_SET bad_spill_regs_global;
186 /* Describes order of use of registers for reloading
187 of spilled pseudo-registers. `n_spills' is the number of
188 elements that are actually valid; new ones are added at the end.
190 Both spill_regs and spill_reg_order are used on two occasions:
191 once during find_reload_regs, where they keep track of the spill registers
192 for a single insn, but also during reload_as_needed where they show all
193 the registers ever used by reload. For the latter case, the information
194 is calculated during finish_spills. */
195 static short spill_regs[FIRST_PSEUDO_REGISTER];
197 /* This vector of reg sets indicates, for each pseudo, which hard registers
198 may not be used for retrying global allocation because the register was
199 formerly spilled from one of them. If we allowed reallocating a pseudo to
200 a register that it was already allocated to, reload might not
201 terminate. */
202 static HARD_REG_SET *pseudo_previous_regs;
204 /* This vector of reg sets indicates, for each pseudo, which hard
205 registers may not be used for retrying global allocation because they
206 are used as spill registers during one of the insns in which the
207 pseudo is live. */
208 static HARD_REG_SET *pseudo_forbidden_regs;
210 /* All hard regs that have been used as spill registers for any insn are
211 marked in this set. */
212 static HARD_REG_SET used_spill_regs;
214 /* Index of last register assigned as a spill register. We allocate in
215 a round-robin fashion. */
216 static int last_spill_reg;
218 /* Describes order of preference for putting regs into spill_regs.
219 Contains the numbers of all the hard regs, in order most preferred first.
220 This order is different for each function.
221 It is set up by order_regs_for_reload.
222 Empty elements at the end contain -1. */
223 static short potential_reload_regs[FIRST_PSEUDO_REGISTER];
225 /* Nonzero if indirect addressing is supported on the machine; this means
226 that spilling (REG n) does not require reloading it into a register in
227 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
228 value indicates the level of indirect addressing supported, e.g., two
229 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
230 a hard register. */
231 static char spill_indirect_levels;
233 /* Nonzero if indirect addressing is supported when the innermost MEM is
234 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
235 which these are valid is the same as spill_indirect_levels, above. */
236 char indirect_symref_ok;
238 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
239 char double_reg_address_ok;
241 /* Record the stack slot for each spilled hard register. */
242 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
244 /* Width allocated so far for that stack slot. */
245 static int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
247 /* Record which pseudos needed to be spilled. */
248 static regset spilled_pseudos;
250 /* First uid used by insns created by reload in this function.
251 Used in find_equiv_reg. */
252 int reload_first_uid;
254 /* Flag set by local-alloc or global-alloc if anything is live in
255 a call-clobbered reg across calls. */
256 int caller_save_needed;
258 /* Set to 1 while reload_as_needed is operating.
259 Required by some machines to handle any generated moves differently. */
260 int reload_in_progress = 0;
262 /* These arrays record the insn_code of insns that may be needed to
263 perform input and output reloads of special objects. They provide a
264 place to pass a scratch register. */
265 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
266 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
268 /* This obstack is used for allocation of rtl during register elimination.
269 The allocated storage can be freed once find_reloads has processed the
270 insn. */
271 struct obstack reload_obstack;
273 /* Points to the beginning of the reload_obstack. All insn_chain structures
274 are allocated first. */
275 char *reload_startobj;
277 /* The point after all insn_chain structures. Used to quickly deallocate
278 memory used while processing one insn. */
279 char *reload_firstobj;
281 #define obstack_chunk_alloc xmalloc
282 #define obstack_chunk_free free
284 /* List of insn_chain instructions, one for every insn that reload needs to
285 examine. */
286 struct insn_chain *reload_insn_chain;
288 #ifdef TREE_CODE
289 extern tree current_function_decl;
290 #else
291 extern union tree_node *current_function_decl;
292 #endif
294 /* List of all insns needing reloads. */
295 static struct insn_chain *insns_need_reload;
297 /* This structure is used to record information about register eliminations.
298 Each array entry describes one possible way of eliminating a register
299 in favor of another. If there is more than one way of eliminating a
300 particular register, the most preferred should be specified first. */
302 struct elim_table
304 int from; /* Register number to be eliminated. */
305 int to; /* Register number used as replacement. */
306 int initial_offset; /* Initial difference between values. */
307 int can_eliminate; /* Non-zero if this elimination can be done. */
308 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
309 insns made by reload. */
310 int offset; /* Current offset between the two regs. */
311 int previous_offset; /* Offset at end of previous insn. */
312 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
313 rtx from_rtx; /* REG rtx for the register to be eliminated.
314 We cannot simply compare the number since
315 we might then spuriously replace a hard
316 register corresponding to a pseudo
317 assigned to the reg to be eliminated. */
318 rtx to_rtx; /* REG rtx for the replacement. */
321 static struct elim_table * reg_eliminate = 0;
323 /* This is an intermediate structure to initialize the table. It has
324 exactly the members provided by ELIMINABLE_REGS. */
325 static struct elim_table_1
327 int from;
328 int to;
329 } reg_eliminate_1[] =
331 /* If a set of eliminable registers was specified, define the table from it.
332 Otherwise, default to the normal case of the frame pointer being
333 replaced by the stack pointer. */
335 #ifdef ELIMINABLE_REGS
336 ELIMINABLE_REGS;
337 #else
338 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
339 #endif
341 #define NUM_ELIMINABLE_REGS (sizeof reg_eliminate_1/sizeof reg_eliminate_1[0])
343 /* Record the number of pending eliminations that have an offset not equal
344 to their initial offset. If non-zero, we use a new copy of each
345 replacement result in any insns encountered. */
346 int num_not_at_initial_offset;
348 /* Count the number of registers that we may be able to eliminate. */
349 static int num_eliminable;
350 /* And the number of registers that are equivalent to a constant that
351 can be eliminated to frame_pointer / arg_pointer + constant. */
352 static int num_eliminable_invariants;
354 /* For each label, we record the offset of each elimination. If we reach
355 a label by more than one path and an offset differs, we cannot do the
356 elimination. This information is indexed by the number of the label.
357 The first table is an array of flags that records whether we have yet
358 encountered a label and the second table is an array of arrays, one
359 entry in the latter array for each elimination. */
361 static char *offsets_known_at;
362 static int (*offsets_at)[NUM_ELIMINABLE_REGS];
364 /* Number of labels in the current function. */
366 static int num_labels;
368 struct hard_reg_n_uses
370 int regno;
371 unsigned int uses;
374 static void maybe_fix_stack_asms PROTO((void));
375 static void calculate_needs_all_insns PROTO((int));
376 static void calculate_needs PROTO((struct insn_chain *));
377 static void find_reload_regs PROTO((struct insn_chain *chain,
378 FILE *));
379 static void find_tworeg_group PROTO((struct insn_chain *, int,
380 FILE *));
381 static void find_group PROTO((struct insn_chain *, int,
382 FILE *));
383 static int possible_group_p PROTO((struct insn_chain *, int));
384 static void count_possible_groups PROTO((struct insn_chain *, int));
385 static int modes_equiv_for_class_p PROTO((enum machine_mode,
386 enum machine_mode,
387 enum reg_class));
388 static void delete_caller_save_insns PROTO((void));
390 static void spill_failure PROTO((rtx));
391 static void new_spill_reg PROTO((struct insn_chain *, int, int,
392 int, FILE *));
393 static void maybe_mark_pseudo_spilled PROTO((int));
394 static void delete_dead_insn PROTO((rtx));
395 static void alter_reg PROTO((int, int));
396 static void set_label_offsets PROTO((rtx, rtx, int));
397 static void check_eliminable_occurrences PROTO((rtx));
398 static void elimination_effects PROTO((rtx, enum machine_mode));
399 static int eliminate_regs_in_insn PROTO((rtx, int));
400 static void update_eliminable_offsets PROTO((void));
401 static void mark_not_eliminable PROTO((rtx, rtx, void *));
402 static void set_initial_elim_offsets PROTO((void));
403 static void verify_initial_elim_offsets PROTO((void));
404 static void set_initial_label_offsets PROTO((void));
405 static void set_offsets_for_label PROTO((rtx));
406 static void init_elim_table PROTO((void));
407 static void update_eliminables PROTO((HARD_REG_SET *));
408 static void spill_hard_reg PROTO((int, FILE *, int));
409 static int finish_spills PROTO((int, FILE *));
410 static void ior_hard_reg_set PROTO((HARD_REG_SET *, HARD_REG_SET *));
411 static void scan_paradoxical_subregs PROTO((rtx));
412 static int hard_reg_use_compare PROTO((const PTR, const PTR));
413 static void count_pseudo PROTO((struct hard_reg_n_uses *, int));
414 static void order_regs_for_reload PROTO((struct insn_chain *));
415 static void reload_as_needed PROTO((int));
416 static void forget_old_reloads_1 PROTO((rtx, rtx, void *));
417 static int reload_reg_class_lower PROTO((const PTR, const PTR));
418 static void mark_reload_reg_in_use PROTO((int, int, enum reload_type,
419 enum machine_mode));
420 static void clear_reload_reg_in_use PROTO((int, int, enum reload_type,
421 enum machine_mode));
422 static int reload_reg_free_p PROTO((int, int, enum reload_type));
423 static int reload_reg_free_for_value_p PROTO((int, int, enum reload_type, rtx, rtx, int, int));
424 static int reload_reg_reaches_end_p PROTO((int, int, enum reload_type));
425 static int allocate_reload_reg PROTO((struct insn_chain *, int, int,
426 int));
427 static void choose_reload_regs_init PROTO((struct insn_chain *, rtx *));
428 static void choose_reload_regs PROTO((struct insn_chain *));
429 static void merge_assigned_reloads PROTO((rtx));
430 static void emit_reload_insns PROTO((struct insn_chain *));
431 static void delete_output_reload PROTO((rtx, int, int));
432 static void delete_address_reloads PROTO((rtx, rtx));
433 static void delete_address_reloads_1 PROTO((rtx, rtx, rtx));
434 static rtx inc_for_reload PROTO((rtx, rtx, rtx, int));
435 static int constraint_accepts_reg_p PROTO((const char *, rtx));
436 static void reload_cse_regs_1 PROTO((rtx));
437 static void reload_cse_invalidate_regno PROTO((int, enum machine_mode, int));
438 static int reload_cse_mem_conflict_p PROTO((rtx, rtx));
439 static void reload_cse_invalidate_mem PROTO((rtx));
440 static void reload_cse_invalidate_rtx PROTO((rtx, rtx, void *));
441 static int reload_cse_regno_equal_p PROTO((int, rtx, enum machine_mode));
442 static int reload_cse_noop_set_p PROTO((rtx, rtx));
443 static int reload_cse_simplify_set PROTO((rtx, rtx));
444 static int reload_cse_simplify_operands PROTO((rtx));
445 static void reload_cse_check_clobber PROTO((rtx, rtx, void *));
446 static void reload_cse_record_set PROTO((rtx, rtx));
447 static void reload_combine PROTO((void));
448 static void reload_combine_note_use PROTO((rtx *, rtx));
449 static void reload_combine_note_store PROTO((rtx, rtx, void *));
450 static void reload_cse_move2add PROTO((rtx));
451 static void move2add_note_store PROTO((rtx, rtx, void *));
452 #ifdef AUTO_INC_DEC
453 static void add_auto_inc_notes PROTO((rtx, rtx));
454 #endif
455 static rtx gen_mode_int PROTO((enum machine_mode,
456 HOST_WIDE_INT));
457 static void failed_reload PROTO((rtx, int));
458 static int set_reload_reg PROTO((int, int));
459 extern void dump_needs PROTO((struct insn_chain *, FILE *));
461 /* Initialize the reload pass once per compilation. */
463 void
464 init_reload ()
466 register int i;
468 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
469 Set spill_indirect_levels to the number of levels such addressing is
470 permitted, zero if it is not permitted at all. */
472 register rtx tem
473 = gen_rtx_MEM (Pmode,
474 gen_rtx_PLUS (Pmode,
475 gen_rtx_REG (Pmode,
476 LAST_VIRTUAL_REGISTER + 1),
477 GEN_INT (4)));
478 spill_indirect_levels = 0;
480 while (memory_address_p (QImode, tem))
482 spill_indirect_levels++;
483 tem = gen_rtx_MEM (Pmode, tem);
486 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
488 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
489 indirect_symref_ok = memory_address_p (QImode, tem);
491 /* See if reg+reg is a valid (and offsettable) address. */
493 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
495 tem = gen_rtx_PLUS (Pmode,
496 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
497 gen_rtx_REG (Pmode, i));
499 /* This way, we make sure that reg+reg is an offsettable address. */
500 tem = plus_constant (tem, 4);
502 if (memory_address_p (QImode, tem))
504 double_reg_address_ok = 1;
505 break;
509 /* Initialize obstack for our rtl allocation. */
510 gcc_obstack_init (&reload_obstack);
511 reload_startobj = (char *) obstack_alloc (&reload_obstack, 0);
514 /* List of insn chains that are currently unused. */
515 static struct insn_chain *unused_insn_chains = 0;
517 /* Allocate an empty insn_chain structure. */
518 struct insn_chain *
519 new_insn_chain ()
521 struct insn_chain *c;
523 if (unused_insn_chains == 0)
525 c = (struct insn_chain *)
526 obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
527 c->live_before = OBSTACK_ALLOC_REG_SET (&reload_obstack);
528 c->live_after = OBSTACK_ALLOC_REG_SET (&reload_obstack);
530 else
532 c = unused_insn_chains;
533 unused_insn_chains = c->next;
535 c->is_caller_save_insn = 0;
536 c->need_operand_change = 0;
537 c->need_reload = 0;
538 c->need_elim = 0;
539 return c;
542 /* Small utility function to set all regs in hard reg set TO which are
543 allocated to pseudos in regset FROM. */
544 void
545 compute_use_by_pseudos (to, from)
546 HARD_REG_SET *to;
547 regset from;
549 int regno;
550 EXECUTE_IF_SET_IN_REG_SET
551 (from, FIRST_PSEUDO_REGISTER, regno,
553 int r = reg_renumber[regno];
554 int nregs;
555 if (r < 0)
557 /* reload_combine uses the information from
558 BASIC_BLOCK->global_live_at_start, which might still
559 contain registers that have not actually been allocated
560 since they have an equivalence. */
561 if (! reload_completed)
562 abort ();
564 else
566 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (regno));
567 while (nregs-- > 0)
568 SET_HARD_REG_BIT (*to, r + nregs);
573 /* Global variables used by reload and its subroutines. */
575 /* Set during calculate_needs if an insn needs register elimination. */
576 static int something_needs_elimination;
577 /* Set during calculate_needs if an insn needs an operand changed. */
578 int something_needs_operands_changed;
580 /* Nonzero means we couldn't get enough spill regs. */
581 static int failure;
583 /* Main entry point for the reload pass.
585 FIRST is the first insn of the function being compiled.
587 GLOBAL nonzero means we were called from global_alloc
588 and should attempt to reallocate any pseudoregs that we
589 displace from hard regs we will use for reloads.
590 If GLOBAL is zero, we do not have enough information to do that,
591 so any pseudo reg that is spilled must go to the stack.
593 DUMPFILE is the global-reg debugging dump file stream, or 0.
594 If it is nonzero, messages are written to it to describe
595 which registers are seized as reload regs, which pseudo regs
596 are spilled from them, and where the pseudo regs are reallocated to.
598 Return value is nonzero if reload failed
599 and we must not do any more for this function. */
602 reload (first, global, dumpfile)
603 rtx first;
604 int global;
605 FILE *dumpfile;
607 register int i;
608 register rtx insn;
609 register struct elim_table *ep;
611 /* The two pointers used to track the true location of the memory used
612 for label offsets. */
613 char *real_known_ptr = NULL_PTR;
614 int (*real_at_ptr)[NUM_ELIMINABLE_REGS];
616 /* Make sure even insns with volatile mem refs are recognizable. */
617 init_recog ();
619 failure = 0;
621 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
623 /* Make sure that the last insn in the chain
624 is not something that needs reloading. */
625 emit_note (NULL_PTR, NOTE_INSN_DELETED);
627 /* Enable find_equiv_reg to distinguish insns made by reload. */
628 reload_first_uid = get_max_uid ();
630 #ifdef SECONDARY_MEMORY_NEEDED
631 /* Initialize the secondary memory table. */
632 clear_secondary_mem ();
633 #endif
635 /* We don't have a stack slot for any spill reg yet. */
636 bzero ((char *) spill_stack_slot, sizeof spill_stack_slot);
637 bzero ((char *) spill_stack_slot_width, sizeof spill_stack_slot_width);
639 /* Initialize the save area information for caller-save, in case some
640 are needed. */
641 init_save_areas ();
643 /* Compute which hard registers are now in use
644 as homes for pseudo registers.
645 This is done here rather than (eg) in global_alloc
646 because this point is reached even if not optimizing. */
647 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
648 mark_home_live (i);
650 /* A function that receives a nonlocal goto must save all call-saved
651 registers. */
652 if (current_function_has_nonlocal_label)
653 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
655 if (! call_used_regs[i] && ! fixed_regs[i])
656 regs_ever_live[i] = 1;
659 /* Find all the pseudo registers that didn't get hard regs
660 but do have known equivalent constants or memory slots.
661 These include parameters (known equivalent to parameter slots)
662 and cse'd or loop-moved constant memory addresses.
664 Record constant equivalents in reg_equiv_constant
665 so they will be substituted by find_reloads.
666 Record memory equivalents in reg_mem_equiv so they can
667 be substituted eventually by altering the REG-rtx's. */
669 reg_equiv_constant = (rtx *) xcalloc (max_regno, sizeof (rtx));
670 reg_equiv_memory_loc = (rtx *) xcalloc (max_regno, sizeof (rtx));
671 reg_equiv_mem = (rtx *) xcalloc (max_regno, sizeof (rtx));
672 reg_equiv_init = (rtx *) xcalloc (max_regno, sizeof (rtx));
673 reg_equiv_address = (rtx *) xcalloc (max_regno, sizeof (rtx));
674 reg_max_ref_width = (int *) xcalloc (max_regno, sizeof (int));
675 reg_old_renumber = (short *) xcalloc (max_regno, sizeof (short));
676 bcopy ((PTR) reg_renumber, (PTR) reg_old_renumber, max_regno * sizeof (short));
677 pseudo_forbidden_regs
678 = (HARD_REG_SET *) xmalloc (max_regno * sizeof (HARD_REG_SET));
679 pseudo_previous_regs
680 = (HARD_REG_SET *) xcalloc (max_regno, sizeof (HARD_REG_SET));
682 CLEAR_HARD_REG_SET (bad_spill_regs_global);
684 /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
685 Also find all paradoxical subregs and find largest such for each pseudo.
686 On machines with small register classes, record hard registers that
687 are used for user variables. These can never be used for spills.
688 Also look for a "constant" NOTE_INSN_SETJMP. This means that all
689 caller-saved registers must be marked live. */
691 num_eliminable_invariants = 0;
692 for (insn = first; insn; insn = NEXT_INSN (insn))
694 rtx set = single_set (insn);
696 if (GET_CODE (insn) == NOTE && CONST_CALL_P (insn)
697 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
698 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
699 if (! call_used_regs[i])
700 regs_ever_live[i] = 1;
702 if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
704 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
705 if (note
706 #ifdef LEGITIMATE_PIC_OPERAND_P
707 && (! function_invariant_p (XEXP (note, 0))
708 || ! flag_pic
709 || LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))
710 #endif
713 rtx x = XEXP (note, 0);
714 i = REGNO (SET_DEST (set));
715 if (i > LAST_VIRTUAL_REGISTER)
717 if (GET_CODE (x) == MEM)
719 /* If the operand is a PLUS, the MEM may be shared,
720 so make sure we have an unshared copy here. */
721 if (GET_CODE (XEXP (x, 0)) == PLUS)
722 x = copy_rtx (x);
724 reg_equiv_memory_loc[i] = x;
726 else if (function_invariant_p (x))
728 if (GET_CODE (x) == PLUS)
730 /* This is PLUS of frame pointer and a constant,
731 and might be shared. Unshare it. */
732 reg_equiv_constant[i] = copy_rtx (x);
733 num_eliminable_invariants++;
735 else if (x == frame_pointer_rtx
736 || x == arg_pointer_rtx)
738 reg_equiv_constant[i] = x;
739 num_eliminable_invariants++;
741 else if (LEGITIMATE_CONSTANT_P (x))
742 reg_equiv_constant[i] = x;
743 else
744 reg_equiv_memory_loc[i]
745 = force_const_mem (GET_MODE (SET_DEST (set)), x);
747 else
748 continue;
750 /* If this register is being made equivalent to a MEM
751 and the MEM is not SET_SRC, the equivalencing insn
752 is one with the MEM as a SET_DEST and it occurs later.
753 So don't mark this insn now. */
754 if (GET_CODE (x) != MEM
755 || rtx_equal_p (SET_SRC (set), x))
756 reg_equiv_init[i]
757 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[i]);
762 /* If this insn is setting a MEM from a register equivalent to it,
763 this is the equivalencing insn. */
764 else if (set && GET_CODE (SET_DEST (set)) == MEM
765 && GET_CODE (SET_SRC (set)) == REG
766 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
767 && rtx_equal_p (SET_DEST (set),
768 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
769 reg_equiv_init[REGNO (SET_SRC (set))]
770 = gen_rtx_INSN_LIST (VOIDmode, insn,
771 reg_equiv_init[REGNO (SET_SRC (set))]);
773 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
774 scan_paradoxical_subregs (PATTERN (insn));
777 init_elim_table ();
779 num_labels = max_label_num () - get_first_label_num ();
781 /* Allocate the tables used to store offset information at labels. */
782 /* We used to use alloca here, but the size of what it would try to
783 allocate would occasionally cause it to exceed the stack limit and
784 cause a core dump. */
785 real_known_ptr = xmalloc (num_labels);
786 real_at_ptr
787 = (int (*)[NUM_ELIMINABLE_REGS])
788 xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
790 offsets_known_at = real_known_ptr - get_first_label_num ();
791 offsets_at
792 = (int (*)[NUM_ELIMINABLE_REGS]) (real_at_ptr - get_first_label_num ());
794 /* Alter each pseudo-reg rtx to contain its hard reg number.
795 Assign stack slots to the pseudos that lack hard regs or equivalents.
796 Do not touch virtual registers. */
798 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
799 alter_reg (i, -1);
801 /* If we have some registers we think can be eliminated, scan all insns to
802 see if there is an insn that sets one of these registers to something
803 other than itself plus a constant. If so, the register cannot be
804 eliminated. Doing this scan here eliminates an extra pass through the
805 main reload loop in the most common case where register elimination
806 cannot be done. */
807 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
808 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
809 || GET_CODE (insn) == CALL_INSN)
810 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
812 maybe_fix_stack_asms ();
814 insns_need_reload = 0;
815 something_needs_elimination = 0;
817 /* Initialize to -1, which means take the first spill register. */
818 last_spill_reg = -1;
820 spilled_pseudos = ALLOCA_REG_SET ();
822 /* Spill any hard regs that we know we can't eliminate. */
823 CLEAR_HARD_REG_SET (used_spill_regs);
824 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
825 if (! ep->can_eliminate)
826 spill_hard_reg (ep->from, dumpfile, 1);
828 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
829 if (frame_pointer_needed)
830 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, dumpfile, 1);
831 #endif
832 finish_spills (global, dumpfile);
834 /* From now on, we may need to generate moves differently. We may also
835 allow modifications of insns which cause them to not be recognized.
836 Any such modifications will be cleaned up during reload itself. */
837 reload_in_progress = 1;
839 /* This loop scans the entire function each go-round
840 and repeats until one repetition spills no additional hard regs. */
841 for (;;)
843 int something_changed;
844 int did_spill;
845 struct insn_chain *chain;
847 HOST_WIDE_INT starting_frame_size;
849 /* Round size of stack frame to BIGGEST_ALIGNMENT. This must be done
850 here because the stack size may be a part of the offset computation
851 for register elimination, and there might have been new stack slots
852 created in the last iteration of this loop. */
853 assign_stack_local (BLKmode, 0, 0);
855 starting_frame_size = get_frame_size ();
857 set_initial_elim_offsets ();
858 set_initial_label_offsets ();
860 /* For each pseudo register that has an equivalent location defined,
861 try to eliminate any eliminable registers (such as the frame pointer)
862 assuming initial offsets for the replacement register, which
863 is the normal case.
865 If the resulting location is directly addressable, substitute
866 the MEM we just got directly for the old REG.
868 If it is not addressable but is a constant or the sum of a hard reg
869 and constant, it is probably not addressable because the constant is
870 out of range, in that case record the address; we will generate
871 hairy code to compute the address in a register each time it is
872 needed. Similarly if it is a hard register, but one that is not
873 valid as an address register.
875 If the location is not addressable, but does not have one of the
876 above forms, assign a stack slot. We have to do this to avoid the
877 potential of producing lots of reloads if, e.g., a location involves
878 a pseudo that didn't get a hard register and has an equivalent memory
879 location that also involves a pseudo that didn't get a hard register.
881 Perhaps at some point we will improve reload_when_needed handling
882 so this problem goes away. But that's very hairy. */
884 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
885 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
887 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
889 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
890 XEXP (x, 0)))
891 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
892 else if (CONSTANT_P (XEXP (x, 0))
893 || (GET_CODE (XEXP (x, 0)) == REG
894 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
895 || (GET_CODE (XEXP (x, 0)) == PLUS
896 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
897 && (REGNO (XEXP (XEXP (x, 0), 0))
898 < FIRST_PSEUDO_REGISTER)
899 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
900 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
901 else
903 /* Make a new stack slot. Then indicate that something
904 changed so we go back and recompute offsets for
905 eliminable registers because the allocation of memory
906 below might change some offset. reg_equiv_{mem,address}
907 will be set up for this pseudo on the next pass around
908 the loop. */
909 reg_equiv_memory_loc[i] = 0;
910 reg_equiv_init[i] = 0;
911 alter_reg (i, -1);
915 if (caller_save_needed)
916 setup_save_areas ();
918 /* If we allocated another stack slot, redo elimination bookkeeping. */
919 if (starting_frame_size != get_frame_size ())
920 continue;
922 if (caller_save_needed)
924 save_call_clobbered_regs ();
925 /* That might have allocated new insn_chain structures. */
926 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
929 calculate_needs_all_insns (global);
931 CLEAR_REG_SET (spilled_pseudos);
932 did_spill = 0;
934 something_changed = 0;
936 /* If we allocated any new memory locations, make another pass
937 since it might have changed elimination offsets. */
938 if (starting_frame_size != get_frame_size ())
939 something_changed = 1;
942 HARD_REG_SET to_spill;
943 CLEAR_HARD_REG_SET (to_spill);
944 update_eliminables (&to_spill);
945 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
946 if (TEST_HARD_REG_BIT (to_spill, i))
948 spill_hard_reg (i, dumpfile, 1);
949 did_spill = 1;
951 /* Regardless of the state of spills, if we previously had
952 a register that we thought we could eliminate, but no can
953 not eliminate, we must run another pass.
955 Consider pseudos which have an entry in reg_equiv_* which
956 reference an eliminable register. We must make another pass
957 to update reg_equiv_* so that we do not substitute in the
958 old value from when we thought the elimination could be
959 performed. */
960 something_changed = 1;
964 CLEAR_HARD_REG_SET (used_spill_regs);
965 /* Try to satisfy the needs for each insn. */
966 for (chain = insns_need_reload; chain != 0;
967 chain = chain->next_need_reload)
968 find_reload_regs (chain, dumpfile);
970 if (failure)
971 goto failed;
973 if (insns_need_reload != 0 || did_spill)
974 something_changed |= finish_spills (global, dumpfile);
976 if (! something_changed)
977 break;
979 if (caller_save_needed)
980 delete_caller_save_insns ();
983 /* If global-alloc was run, notify it of any register eliminations we have
984 done. */
985 if (global)
986 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
987 if (ep->can_eliminate)
988 mark_elimination (ep->from, ep->to);
990 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
991 If that insn didn't set the register (i.e., it copied the register to
992 memory), just delete that insn instead of the equivalencing insn plus
993 anything now dead. If we call delete_dead_insn on that insn, we may
994 delete the insn that actually sets the register if the register dies
995 there and that is incorrect. */
997 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
999 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1001 rtx list;
1002 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1004 rtx equiv_insn = XEXP (list, 0);
1005 if (GET_CODE (equiv_insn) == NOTE)
1006 continue;
1007 if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1008 delete_dead_insn (equiv_insn);
1009 else
1011 PUT_CODE (equiv_insn, NOTE);
1012 NOTE_SOURCE_FILE (equiv_insn) = 0;
1013 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1019 /* Use the reload registers where necessary
1020 by generating move instructions to move the must-be-register
1021 values into or out of the reload registers. */
1023 if (insns_need_reload != 0 || something_needs_elimination
1024 || something_needs_operands_changed)
1026 int old_frame_size = get_frame_size ();
1028 reload_as_needed (global);
1030 if (old_frame_size != get_frame_size ())
1031 abort ();
1033 if (num_eliminable)
1034 verify_initial_elim_offsets ();
1037 /* If we were able to eliminate the frame pointer, show that it is no
1038 longer live at the start of any basic block. If it ls live by
1039 virtue of being in a pseudo, that pseudo will be marked live
1040 and hence the frame pointer will be known to be live via that
1041 pseudo. */
1043 if (! frame_pointer_needed)
1044 for (i = 0; i < n_basic_blocks; i++)
1045 CLEAR_REGNO_REG_SET (BASIC_BLOCK (i)->global_live_at_start,
1046 HARD_FRAME_POINTER_REGNUM);
1048 /* Come here (with failure set nonzero) if we can't get enough spill regs
1049 and we decide not to abort about it. */
1050 failed:
1052 reload_in_progress = 0;
1054 /* Now eliminate all pseudo regs by modifying them into
1055 their equivalent memory references.
1056 The REG-rtx's for the pseudos are modified in place,
1057 so all insns that used to refer to them now refer to memory.
1059 For a reg that has a reg_equiv_address, all those insns
1060 were changed by reloading so that no insns refer to it any longer;
1061 but the DECL_RTL of a variable decl may refer to it,
1062 and if so this causes the debugging info to mention the variable. */
1064 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1066 rtx addr = 0;
1067 int in_struct = 0;
1068 int is_scalar = 0;
1069 int is_readonly = 0;
1071 if (reg_equiv_memory_loc[i])
1073 in_struct = MEM_IN_STRUCT_P (reg_equiv_memory_loc[i]);
1074 is_scalar = MEM_SCALAR_P (reg_equiv_memory_loc[i]);
1075 is_readonly = RTX_UNCHANGING_P (reg_equiv_memory_loc[i]);
1078 if (reg_equiv_mem[i])
1079 addr = XEXP (reg_equiv_mem[i], 0);
1081 if (reg_equiv_address[i])
1082 addr = reg_equiv_address[i];
1084 if (addr)
1086 if (reg_renumber[i] < 0)
1088 rtx reg = regno_reg_rtx[i];
1089 PUT_CODE (reg, MEM);
1090 XEXP (reg, 0) = addr;
1091 REG_USERVAR_P (reg) = 0;
1092 RTX_UNCHANGING_P (reg) = is_readonly;
1093 MEM_IN_STRUCT_P (reg) = in_struct;
1094 MEM_SCALAR_P (reg) = is_scalar;
1095 /* We have no alias information about this newly created
1096 MEM. */
1097 MEM_ALIAS_SET (reg) = 0;
1099 else if (reg_equiv_mem[i])
1100 XEXP (reg_equiv_mem[i], 0) = addr;
1104 /* We must set reload_completed now since the cleanup_subreg_operands call
1105 below will re-recognize each insn and reload may have generated insns
1106 which are only valid during and after reload. */
1107 reload_completed = 1;
1109 /* Make a pass over all the insns and delete all USEs which we
1110 inserted only to tag a REG_EQUAL note on them. Remove all
1111 REG_DEAD and REG_UNUSED notes. Delete all CLOBBER insns and
1112 simplify (subreg (reg)) operands. Also remove all REG_RETVAL and
1113 REG_LIBCALL notes since they are no longer useful or accurate.
1114 Strip and regenerate REG_INC notes that may have been moved
1115 around. */
1117 for (insn = first; insn; insn = NEXT_INSN (insn))
1118 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1120 rtx *pnote;
1122 if ((GET_CODE (PATTERN (insn)) == USE
1123 && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1124 || GET_CODE (PATTERN (insn)) == CLOBBER)
1126 PUT_CODE (insn, NOTE);
1127 NOTE_SOURCE_FILE (insn) = 0;
1128 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1129 continue;
1132 pnote = &REG_NOTES (insn);
1133 while (*pnote != 0)
1135 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1136 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1137 || REG_NOTE_KIND (*pnote) == REG_INC
1138 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1139 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1140 *pnote = XEXP (*pnote, 1);
1141 else
1142 pnote = &XEXP (*pnote, 1);
1145 #ifdef AUTO_INC_DEC
1146 add_auto_inc_notes (insn, PATTERN (insn));
1147 #endif
1149 /* And simplify (subreg (reg)) if it appears as an operand. */
1150 cleanup_subreg_operands (insn);
1153 /* If we are doing stack checking, give a warning if this function's
1154 frame size is larger than we expect. */
1155 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1157 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1158 static int verbose_warned = 0;
1160 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1161 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1162 size += UNITS_PER_WORD;
1164 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1166 warning ("frame size too large for reliable stack checking");
1167 if (! verbose_warned)
1169 warning ("try reducing the number of local variables");
1170 verbose_warned = 1;
1175 /* Indicate that we no longer have known memory locations or constants. */
1176 if (reg_equiv_constant)
1177 free (reg_equiv_constant);
1178 reg_equiv_constant = 0;
1179 if (reg_equiv_memory_loc)
1180 free (reg_equiv_memory_loc);
1181 reg_equiv_memory_loc = 0;
1183 if (real_known_ptr)
1184 free (real_known_ptr);
1185 if (real_at_ptr)
1186 free (real_at_ptr);
1188 free (reg_equiv_mem);
1189 free (reg_equiv_init);
1190 free (reg_equiv_address);
1191 free (reg_max_ref_width);
1192 free (reg_old_renumber);
1193 free (pseudo_previous_regs);
1194 free (pseudo_forbidden_regs);
1196 FREE_REG_SET (spilled_pseudos);
1198 CLEAR_HARD_REG_SET (used_spill_regs);
1199 for (i = 0; i < n_spills; i++)
1200 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1202 /* Free all the insn_chain structures at once. */
1203 obstack_free (&reload_obstack, reload_startobj);
1204 unused_insn_chains = 0;
1206 return failure;
1209 /* Yet another special case. Unfortunately, reg-stack forces people to
1210 write incorrect clobbers in asm statements. These clobbers must not
1211 cause the register to appear in bad_spill_regs, otherwise we'll call
1212 fatal_insn later. We clear the corresponding regnos in the live
1213 register sets to avoid this.
1214 The whole thing is rather sick, I'm afraid. */
1215 static void
1216 maybe_fix_stack_asms ()
1218 #ifdef STACK_REGS
1219 const char *constraints[MAX_RECOG_OPERANDS];
1220 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1221 struct insn_chain *chain;
1223 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1225 int i, noperands;
1226 HARD_REG_SET clobbered, allowed;
1227 rtx pat;
1229 if (GET_RTX_CLASS (GET_CODE (chain->insn)) != 'i'
1230 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1231 continue;
1232 pat = PATTERN (chain->insn);
1233 if (GET_CODE (pat) != PARALLEL)
1234 continue;
1236 CLEAR_HARD_REG_SET (clobbered);
1237 CLEAR_HARD_REG_SET (allowed);
1239 /* First, make a mask of all stack regs that are clobbered. */
1240 for (i = 0; i < XVECLEN (pat, 0); i++)
1242 rtx t = XVECEXP (pat, 0, i);
1243 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1244 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1247 /* Get the operand values and constraints out of the insn. */
1248 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1249 constraints, operand_mode);
1251 /* For every operand, see what registers are allowed. */
1252 for (i = 0; i < noperands; i++)
1254 const char *p = constraints[i];
1255 /* For every alternative, we compute the class of registers allowed
1256 for reloading in CLS, and merge its contents into the reg set
1257 ALLOWED. */
1258 int cls = (int) NO_REGS;
1260 for (;;)
1262 char c = *p++;
1264 if (c == '\0' || c == ',' || c == '#')
1266 /* End of one alternative - mark the regs in the current
1267 class, and reset the class. */
1268 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1269 cls = NO_REGS;
1270 if (c == '#')
1271 do {
1272 c = *p++;
1273 } while (c != '\0' && c != ',');
1274 if (c == '\0')
1275 break;
1276 continue;
1279 switch (c)
1281 case '=': case '+': case '*': case '%': case '?': case '!':
1282 case '0': case '1': case '2': case '3': case '4': case 'm':
1283 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1284 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1285 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1286 case 'P':
1287 #ifdef EXTRA_CONSTRAINT
1288 case 'Q': case 'R': case 'S': case 'T': case 'U':
1289 #endif
1290 break;
1292 case 'p':
1293 cls = (int) reg_class_subunion[cls][(int) BASE_REG_CLASS];
1294 break;
1296 case 'g':
1297 case 'r':
1298 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1299 break;
1301 default:
1302 cls = (int) reg_class_subunion[cls][(int) REG_CLASS_FROM_LETTER (c)];
1307 /* Those of the registers which are clobbered, but allowed by the
1308 constraints, must be usable as reload registers. So clear them
1309 out of the life information. */
1310 AND_HARD_REG_SET (allowed, clobbered);
1311 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1312 if (TEST_HARD_REG_BIT (allowed, i))
1314 CLEAR_REGNO_REG_SET (chain->live_before, i);
1315 CLEAR_REGNO_REG_SET (chain->live_after, i);
1319 #endif
1323 /* Walk the chain of insns, and determine for each whether it needs reloads
1324 and/or eliminations. Build the corresponding insns_need_reload list, and
1325 set something_needs_elimination as appropriate. */
1326 static void
1327 calculate_needs_all_insns (global)
1328 int global;
1330 struct insn_chain **pprev_reload = &insns_need_reload;
1331 struct insn_chain *chain;
1333 something_needs_elimination = 0;
1335 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1337 rtx insn = chain->insn;
1339 /* Clear out the shortcuts, in case they were set last time through. */
1340 chain->need_elim = 0;
1341 chain->need_reload = 0;
1342 chain->need_operand_change = 0;
1344 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1345 include REG_LABEL), we need to see what effects this has on the
1346 known offsets at labels. */
1348 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
1349 || (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1350 && REG_NOTES (insn) != 0))
1351 set_label_offsets (insn, insn, 0);
1353 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1355 rtx old_body = PATTERN (insn);
1356 int old_code = INSN_CODE (insn);
1357 rtx old_notes = REG_NOTES (insn);
1358 int did_elimination = 0;
1359 int operands_changed = 0;
1360 rtx set = single_set (insn);
1362 /* Skip insns that only set an equivalence. */
1363 if (set && GET_CODE (SET_DEST (set)) == REG
1364 && reg_renumber[REGNO (SET_DEST (set))] < 0
1365 && reg_equiv_constant[REGNO (SET_DEST (set))])
1366 continue;
1368 /* If needed, eliminate any eliminable registers. */
1369 if (num_eliminable || num_eliminable_invariants)
1370 did_elimination = eliminate_regs_in_insn (insn, 0);
1372 /* Analyze the instruction. */
1373 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1374 global, spill_reg_order);
1376 /* If a no-op set needs more than one reload, this is likely
1377 to be something that needs input address reloads. We
1378 can't get rid of this cleanly later, and it is of no use
1379 anyway, so discard it now.
1380 We only do this when expensive_optimizations is enabled,
1381 since this complements reload inheritance / output
1382 reload deletion, and it can make debugging harder. */
1383 if (flag_expensive_optimizations && n_reloads > 1)
1385 rtx set = single_set (insn);
1386 if (set
1387 && SET_SRC (set) == SET_DEST (set)
1388 && GET_CODE (SET_SRC (set)) == REG
1389 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1391 PUT_CODE (insn, NOTE);
1392 NOTE_SOURCE_FILE (insn) = 0;
1393 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1394 continue;
1397 if (num_eliminable)
1398 update_eliminable_offsets ();
1400 /* Remember for later shortcuts which insns had any reloads or
1401 register eliminations. */
1402 chain->need_elim = did_elimination;
1403 chain->need_reload = n_reloads > 0;
1404 chain->need_operand_change = operands_changed;
1406 /* Discard any register replacements done. */
1407 if (did_elimination)
1409 obstack_free (&reload_obstack, reload_firstobj);
1410 PATTERN (insn) = old_body;
1411 INSN_CODE (insn) = old_code;
1412 REG_NOTES (insn) = old_notes;
1413 something_needs_elimination = 1;
1416 something_needs_operands_changed |= operands_changed;
1418 if (n_reloads != 0)
1420 *pprev_reload = chain;
1421 pprev_reload = &chain->next_need_reload;
1423 calculate_needs (chain);
1427 *pprev_reload = 0;
1430 /* Compute the most additional registers needed by one instruction,
1431 given by CHAIN. Collect information separately for each class of regs.
1433 To compute the number of reload registers of each class needed for an
1434 insn, we must simulate what choose_reload_regs can do. We do this by
1435 splitting an insn into an "input" and an "output" part. RELOAD_OTHER
1436 reloads are used in both. The input part uses those reloads,
1437 RELOAD_FOR_INPUT reloads, which must be live over the entire input section
1438 of reloads, and the maximum of all the RELOAD_FOR_INPUT_ADDRESS and
1439 RELOAD_FOR_OPERAND_ADDRESS reloads, which conflict with the inputs.
1441 The registers needed for output are RELOAD_OTHER and RELOAD_FOR_OUTPUT,
1442 which are live for the entire output portion, and the maximum of all the
1443 RELOAD_FOR_OUTPUT_ADDRESS reloads for each operand.
1445 The total number of registers needed is the maximum of the
1446 inputs and outputs. */
1448 static void
1449 calculate_needs (chain)
1450 struct insn_chain *chain;
1452 int i;
1454 /* Each `struct needs' corresponds to one RELOAD_... type. */
1455 struct {
1456 struct needs other;
1457 struct needs input;
1458 struct needs output;
1459 struct needs insn;
1460 struct needs other_addr;
1461 struct needs op_addr;
1462 struct needs op_addr_reload;
1463 struct needs in_addr[MAX_RECOG_OPERANDS];
1464 struct needs in_addr_addr[MAX_RECOG_OPERANDS];
1465 struct needs out_addr[MAX_RECOG_OPERANDS];
1466 struct needs out_addr_addr[MAX_RECOG_OPERANDS];
1467 } insn_needs;
1469 bzero ((char *) chain->group_size, sizeof chain->group_size);
1470 for (i = 0; i < N_REG_CLASSES; i++)
1471 chain->group_mode[i] = VOIDmode;
1472 bzero ((char *) &insn_needs, sizeof insn_needs);
1474 /* Count each reload once in every class
1475 containing the reload's own class. */
1477 for (i = 0; i < n_reloads; i++)
1479 register enum reg_class *p;
1480 enum reg_class class = rld[i].class;
1481 int size;
1482 enum machine_mode mode;
1483 struct needs *this_needs;
1485 /* Don't count the dummy reloads, for which one of the
1486 regs mentioned in the insn can be used for reloading.
1487 Don't count optional reloads.
1488 Don't count reloads that got combined with others. */
1489 if (rld[i].reg_rtx != 0
1490 || rld[i].optional != 0
1491 || (rld[i].out == 0 && rld[i].in == 0
1492 && ! rld[i].secondary_p))
1493 continue;
1495 mode = rld[i].mode;
1496 size = rld[i].nregs;
1498 /* Decide which time-of-use to count this reload for. */
1499 switch (rld[i].when_needed)
1501 case RELOAD_OTHER:
1502 this_needs = &insn_needs.other;
1503 break;
1504 case RELOAD_FOR_INPUT:
1505 this_needs = &insn_needs.input;
1506 break;
1507 case RELOAD_FOR_OUTPUT:
1508 this_needs = &insn_needs.output;
1509 break;
1510 case RELOAD_FOR_INSN:
1511 this_needs = &insn_needs.insn;
1512 break;
1513 case RELOAD_FOR_OTHER_ADDRESS:
1514 this_needs = &insn_needs.other_addr;
1515 break;
1516 case RELOAD_FOR_INPUT_ADDRESS:
1517 this_needs = &insn_needs.in_addr[rld[i].opnum];
1518 break;
1519 case RELOAD_FOR_INPADDR_ADDRESS:
1520 this_needs = &insn_needs.in_addr_addr[rld[i].opnum];
1521 break;
1522 case RELOAD_FOR_OUTPUT_ADDRESS:
1523 this_needs = &insn_needs.out_addr[rld[i].opnum];
1524 break;
1525 case RELOAD_FOR_OUTADDR_ADDRESS:
1526 this_needs = &insn_needs.out_addr_addr[rld[i].opnum];
1527 break;
1528 case RELOAD_FOR_OPERAND_ADDRESS:
1529 this_needs = &insn_needs.op_addr;
1530 break;
1531 case RELOAD_FOR_OPADDR_ADDR:
1532 this_needs = &insn_needs.op_addr_reload;
1533 break;
1534 default:
1535 abort();
1538 if (size > 1)
1540 enum machine_mode other_mode, allocate_mode;
1542 /* Count number of groups needed separately from
1543 number of individual regs needed. */
1544 this_needs->groups[(int) class]++;
1545 p = reg_class_superclasses[(int) class];
1546 while (*p != LIM_REG_CLASSES)
1547 this_needs->groups[(int) *p++]++;
1549 /* Record size and mode of a group of this class. */
1550 /* If more than one size group is needed,
1551 make all groups the largest needed size. */
1552 if (chain->group_size[(int) class] < size)
1554 other_mode = chain->group_mode[(int) class];
1555 allocate_mode = mode;
1557 chain->group_size[(int) class] = size;
1558 chain->group_mode[(int) class] = mode;
1560 else
1562 other_mode = mode;
1563 allocate_mode = chain->group_mode[(int) class];
1566 /* Crash if two dissimilar machine modes both need
1567 groups of consecutive regs of the same class. */
1569 if (other_mode != VOIDmode && other_mode != allocate_mode
1570 && ! modes_equiv_for_class_p (allocate_mode,
1571 other_mode, class))
1572 fatal_insn ("Two dissimilar machine modes both need groups of consecutive regs of the same class",
1573 chain->insn);
1575 else if (size == 1)
1577 this_needs->regs[(unsigned char)rld[i].nongroup][(int) class] += 1;
1578 p = reg_class_superclasses[(int) class];
1579 while (*p != LIM_REG_CLASSES)
1580 this_needs->regs[(unsigned char)rld[i].nongroup][(int) *p++] += 1;
1582 else
1583 abort ();
1586 /* All reloads have been counted for this insn;
1587 now merge the various times of use.
1588 This sets insn_needs, etc., to the maximum total number
1589 of registers needed at any point in this insn. */
1591 for (i = 0; i < N_REG_CLASSES; i++)
1593 int j, in_max, out_max;
1595 /* Compute normal and nongroup needs. */
1596 for (j = 0; j <= 1; j++)
1598 int k;
1599 for (in_max = 0, out_max = 0, k = 0; k < reload_n_operands; k++)
1601 in_max = MAX (in_max,
1602 (insn_needs.in_addr[k].regs[j][i]
1603 + insn_needs.in_addr_addr[k].regs[j][i]));
1604 out_max = MAX (out_max, insn_needs.out_addr[k].regs[j][i]);
1605 out_max = MAX (out_max,
1606 insn_needs.out_addr_addr[k].regs[j][i]);
1609 /* RELOAD_FOR_INSN reloads conflict with inputs, outputs,
1610 and operand addresses but not things used to reload
1611 them. Similarly, RELOAD_FOR_OPERAND_ADDRESS reloads
1612 don't conflict with things needed to reload inputs or
1613 outputs. */
1615 in_max = MAX (MAX (insn_needs.op_addr.regs[j][i],
1616 insn_needs.op_addr_reload.regs[j][i]),
1617 in_max);
1619 out_max = MAX (out_max, insn_needs.insn.regs[j][i]);
1621 insn_needs.input.regs[j][i]
1622 = MAX (insn_needs.input.regs[j][i]
1623 + insn_needs.op_addr.regs[j][i]
1624 + insn_needs.insn.regs[j][i],
1625 in_max + insn_needs.input.regs[j][i]);
1627 insn_needs.output.regs[j][i] += out_max;
1628 insn_needs.other.regs[j][i]
1629 += MAX (MAX (insn_needs.input.regs[j][i],
1630 insn_needs.output.regs[j][i]),
1631 insn_needs.other_addr.regs[j][i]);
1635 /* Now compute group needs. */
1636 for (in_max = 0, out_max = 0, j = 0; j < reload_n_operands; j++)
1638 in_max = MAX (in_max, insn_needs.in_addr[j].groups[i]);
1639 in_max = MAX (in_max, insn_needs.in_addr_addr[j].groups[i]);
1640 out_max = MAX (out_max, insn_needs.out_addr[j].groups[i]);
1641 out_max = MAX (out_max, insn_needs.out_addr_addr[j].groups[i]);
1644 in_max = MAX (MAX (insn_needs.op_addr.groups[i],
1645 insn_needs.op_addr_reload.groups[i]),
1646 in_max);
1647 out_max = MAX (out_max, insn_needs.insn.groups[i]);
1649 insn_needs.input.groups[i]
1650 = MAX (insn_needs.input.groups[i]
1651 + insn_needs.op_addr.groups[i]
1652 + insn_needs.insn.groups[i],
1653 in_max + insn_needs.input.groups[i]);
1655 insn_needs.output.groups[i] += out_max;
1656 insn_needs.other.groups[i]
1657 += MAX (MAX (insn_needs.input.groups[i],
1658 insn_needs.output.groups[i]),
1659 insn_needs.other_addr.groups[i]);
1662 /* Record the needs for later. */
1663 chain->need = insn_needs.other;
1666 /* Find a group of exactly 2 registers.
1668 First try to fill out the group by spilling a single register which
1669 would allow completion of the group.
1671 Then try to create a new group from a pair of registers, neither of
1672 which are explicitly used.
1674 Then try to create a group from any pair of registers. */
1676 static void
1677 find_tworeg_group (chain, class, dumpfile)
1678 struct insn_chain *chain;
1679 int class;
1680 FILE *dumpfile;
1682 int i;
1683 /* First, look for a register that will complete a group. */
1684 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1686 int j, other;
1688 j = potential_reload_regs[i];
1689 if (j >= 0 && ! TEST_HARD_REG_BIT (bad_spill_regs, j)
1690 && ((j > 0 && (other = j - 1, spill_reg_order[other] >= 0)
1691 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1692 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1693 && HARD_REGNO_MODE_OK (other, chain->group_mode[class])
1694 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, other)
1695 /* We don't want one part of another group.
1696 We could get "two groups" that overlap! */
1697 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, other))
1698 || (j < FIRST_PSEUDO_REGISTER - 1
1699 && (other = j + 1, spill_reg_order[other] >= 0)
1700 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1701 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1702 && HARD_REGNO_MODE_OK (j, chain->group_mode[class])
1703 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, other)
1704 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, other))))
1706 register enum reg_class *p;
1708 /* We have found one that will complete a group,
1709 so count off one group as provided. */
1710 chain->need.groups[class]--;
1711 p = reg_class_superclasses[class];
1712 while (*p != LIM_REG_CLASSES)
1714 if (chain->group_size [(int) *p] <= chain->group_size [class])
1715 chain->need.groups[(int) *p]--;
1716 p++;
1719 /* Indicate both these regs are part of a group. */
1720 SET_HARD_REG_BIT (chain->counted_for_groups, j);
1721 SET_HARD_REG_BIT (chain->counted_for_groups, other);
1722 break;
1725 /* We can't complete a group, so start one. */
1726 if (i == FIRST_PSEUDO_REGISTER)
1727 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1729 int j, k;
1730 j = potential_reload_regs[i];
1731 /* Verify that J+1 is a potential reload reg. */
1732 for (k = 0; k < FIRST_PSEUDO_REGISTER; k++)
1733 if (potential_reload_regs[k] == j + 1)
1734 break;
1735 if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1736 && k < FIRST_PSEUDO_REGISTER
1737 && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1738 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1739 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1740 && HARD_REGNO_MODE_OK (j, chain->group_mode[class])
1741 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, j + 1)
1742 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + 1))
1743 break;
1746 /* I should be the index in potential_reload_regs
1747 of the new reload reg we have found. */
1749 new_spill_reg (chain, i, class, 0, dumpfile);
1752 /* Find a group of more than 2 registers.
1753 Look for a sufficient sequence of unspilled registers, and spill them all
1754 at once. */
1756 static void
1757 find_group (chain, class, dumpfile)
1758 struct insn_chain *chain;
1759 int class;
1760 FILE *dumpfile;
1762 int i;
1764 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1766 int j = potential_reload_regs[i];
1768 if (j >= 0
1769 && j + chain->group_size[class] <= FIRST_PSEUDO_REGISTER
1770 && HARD_REGNO_MODE_OK (j, chain->group_mode[class]))
1772 int k;
1773 /* Check each reg in the sequence. */
1774 for (k = 0; k < chain->group_size[class]; k++)
1775 if (! (spill_reg_order[j + k] < 0
1776 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + k)
1777 && TEST_HARD_REG_BIT (reg_class_contents[class], j + k)))
1778 break;
1779 /* We got a full sequence, so spill them all. */
1780 if (k == chain->group_size[class])
1782 register enum reg_class *p;
1783 for (k = 0; k < chain->group_size[class]; k++)
1785 int idx;
1786 SET_HARD_REG_BIT (chain->counted_for_groups, j + k);
1787 for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
1788 if (potential_reload_regs[idx] == j + k)
1789 break;
1790 new_spill_reg (chain, idx, class, 0, dumpfile);
1793 /* We have found one that will complete a group,
1794 so count off one group as provided. */
1795 chain->need.groups[class]--;
1796 p = reg_class_superclasses[class];
1797 while (*p != LIM_REG_CLASSES)
1799 if (chain->group_size [(int) *p]
1800 <= chain->group_size [class])
1801 chain->need.groups[(int) *p]--;
1802 p++;
1804 return;
1808 /* There are no groups left. */
1809 spill_failure (chain->insn);
1810 failure = 1;
1813 /* If pseudo REG conflicts with one of our reload registers, mark it as
1814 spilled. */
1815 static void
1816 maybe_mark_pseudo_spilled (reg)
1817 int reg;
1819 int i;
1820 int r = reg_renumber[reg];
1821 int nregs;
1823 if (r < 0)
1824 abort ();
1825 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
1826 for (i = 0; i < n_spills; i++)
1827 if (r <= spill_regs[i] && r + nregs > spill_regs[i])
1829 SET_REGNO_REG_SET (spilled_pseudos, reg);
1830 return;
1834 /* Find more reload regs to satisfy the remaining need of an insn, which
1835 is given by CHAIN.
1836 Do it by ascending class number, since otherwise a reg
1837 might be spilled for a big class and might fail to count
1838 for a smaller class even though it belongs to that class.
1840 Count spilled regs in `spills', and add entries to
1841 `spill_regs' and `spill_reg_order'.
1843 ??? Note there is a problem here.
1844 When there is a need for a group in a high-numbered class,
1845 and also need for non-group regs that come from a lower class,
1846 the non-group regs are chosen first. If there aren't many regs,
1847 they might leave no room for a group.
1849 This was happening on the 386. To fix it, we added the code
1850 that calls possible_group_p, so that the lower class won't
1851 break up the last possible group.
1853 Really fixing the problem would require changes above
1854 in counting the regs already spilled, and in choose_reload_regs.
1855 It might be hard to avoid introducing bugs there. */
1857 static void
1858 find_reload_regs (chain, dumpfile)
1859 struct insn_chain *chain;
1860 FILE *dumpfile;
1862 int i, class;
1863 short *group_needs = chain->need.groups;
1864 short *simple_needs = chain->need.regs[0];
1865 short *nongroup_needs = chain->need.regs[1];
1867 if (dumpfile)
1868 fprintf (dumpfile, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1870 /* Compute the order of preference for hard registers to spill.
1871 Store them by decreasing preference in potential_reload_regs. */
1873 order_regs_for_reload (chain);
1875 /* So far, no hard regs have been spilled. */
1876 n_spills = 0;
1877 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1878 spill_reg_order[i] = -1;
1880 CLEAR_HARD_REG_SET (chain->used_spill_regs);
1881 CLEAR_HARD_REG_SET (chain->counted_for_groups);
1882 CLEAR_HARD_REG_SET (chain->counted_for_nongroups);
1884 for (class = 0; class < N_REG_CLASSES; class++)
1886 /* First get the groups of registers.
1887 If we got single registers first, we might fragment
1888 possible groups. */
1889 while (group_needs[class] > 0)
1891 /* If any single spilled regs happen to form groups,
1892 count them now. Maybe we don't really need
1893 to spill another group. */
1894 count_possible_groups (chain, class);
1896 if (group_needs[class] <= 0)
1897 break;
1899 /* Groups of size 2, the only groups used on most machines,
1900 are treated specially. */
1901 if (chain->group_size[class] == 2)
1902 find_tworeg_group (chain, class, dumpfile);
1903 else
1904 find_group (chain, class, dumpfile);
1905 if (failure)
1906 return;
1909 /* Now similarly satisfy all need for single registers. */
1911 while (simple_needs[class] > 0 || nongroup_needs[class] > 0)
1913 /* If we spilled enough regs, but they weren't counted
1914 against the non-group need, see if we can count them now.
1915 If so, we can avoid some actual spilling. */
1916 if (simple_needs[class] <= 0 && nongroup_needs[class] > 0)
1917 for (i = 0; i < n_spills; i++)
1919 int regno = spill_regs[i];
1920 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1921 && !TEST_HARD_REG_BIT (chain->counted_for_groups, regno)
1922 && !TEST_HARD_REG_BIT (chain->counted_for_nongroups, regno)
1923 && nongroup_needs[class] > 0)
1925 register enum reg_class *p;
1927 SET_HARD_REG_BIT (chain->counted_for_nongroups, regno);
1928 nongroup_needs[class]--;
1929 p = reg_class_superclasses[class];
1930 while (*p != LIM_REG_CLASSES)
1931 nongroup_needs[(int) *p++]--;
1935 if (simple_needs[class] <= 0 && nongroup_needs[class] <= 0)
1936 break;
1938 /* Consider the potential reload regs that aren't
1939 yet in use as reload regs, in order of preference.
1940 Find the most preferred one that's in this class. */
1942 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1944 int regno = potential_reload_regs[i];
1945 if (regno >= 0
1946 && TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1947 /* If this reg will not be available for groups,
1948 pick one that does not foreclose possible groups.
1949 This is a kludge, and not very general,
1950 but it should be sufficient to make the 386 work,
1951 and the problem should not occur on machines with
1952 more registers. */
1953 && (nongroup_needs[class] == 0
1954 || possible_group_p (chain, regno)))
1955 break;
1958 /* If we couldn't get a register, try to get one even if we
1959 might foreclose possible groups. This may cause problems
1960 later, but that's better than aborting now, since it is
1961 possible that we will, in fact, be able to form the needed
1962 group even with this allocation. */
1964 if (i >= FIRST_PSEUDO_REGISTER
1965 && asm_noperands (chain->insn) < 0)
1966 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1967 if (potential_reload_regs[i] >= 0
1968 && TEST_HARD_REG_BIT (reg_class_contents[class],
1969 potential_reload_regs[i]))
1970 break;
1972 /* I should be the index in potential_reload_regs
1973 of the new reload reg we have found. */
1975 new_spill_reg (chain, i, class, 1, dumpfile);
1976 if (failure)
1977 return;
1981 /* We know which hard regs to use, now mark the pseudos that live in them
1982 as needing to be kicked out. */
1983 EXECUTE_IF_SET_IN_REG_SET
1984 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
1986 maybe_mark_pseudo_spilled (i);
1988 EXECUTE_IF_SET_IN_REG_SET
1989 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
1991 maybe_mark_pseudo_spilled (i);
1994 IOR_HARD_REG_SET (used_spill_regs, chain->used_spill_regs);
1997 void
1998 dump_needs (chain, dumpfile)
1999 struct insn_chain *chain;
2000 FILE *dumpfile;
2002 static const char * const reg_class_names[] = REG_CLASS_NAMES;
2003 int i;
2004 struct needs *n = &chain->need;
2006 for (i = 0; i < N_REG_CLASSES; i++)
2008 if (n->regs[i][0] > 0)
2009 fprintf (dumpfile,
2010 ";; Need %d reg%s of class %s.\n",
2011 n->regs[i][0], n->regs[i][0] == 1 ? "" : "s",
2012 reg_class_names[i]);
2013 if (n->regs[i][1] > 0)
2014 fprintf (dumpfile,
2015 ";; Need %d nongroup reg%s of class %s.\n",
2016 n->regs[i][1], n->regs[i][1] == 1 ? "" : "s",
2017 reg_class_names[i]);
2018 if (n->groups[i] > 0)
2019 fprintf (dumpfile,
2020 ";; Need %d group%s (%smode) of class %s.\n",
2021 n->groups[i], n->groups[i] == 1 ? "" : "s",
2022 GET_MODE_NAME(chain->group_mode[i]),
2023 reg_class_names[i]);
2027 /* Delete all insns that were inserted by emit_caller_save_insns during
2028 this iteration. */
2029 static void
2030 delete_caller_save_insns ()
2032 struct insn_chain *c = reload_insn_chain;
2034 while (c != 0)
2036 while (c != 0 && c->is_caller_save_insn)
2038 struct insn_chain *next = c->next;
2039 rtx insn = c->insn;
2041 if (insn == BLOCK_HEAD (c->block))
2042 BLOCK_HEAD (c->block) = NEXT_INSN (insn);
2043 if (insn == BLOCK_END (c->block))
2044 BLOCK_END (c->block) = PREV_INSN (insn);
2045 if (c == reload_insn_chain)
2046 reload_insn_chain = next;
2048 if (NEXT_INSN (insn) != 0)
2049 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2050 if (PREV_INSN (insn) != 0)
2051 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2053 if (next)
2054 next->prev = c->prev;
2055 if (c->prev)
2056 c->prev->next = next;
2057 c->next = unused_insn_chains;
2058 unused_insn_chains = c;
2059 c = next;
2061 if (c != 0)
2062 c = c->next;
2066 /* Nonzero if, after spilling reg REGNO for non-groups,
2067 it will still be possible to find a group if we still need one. */
2069 static int
2070 possible_group_p (chain, regno)
2071 struct insn_chain *chain;
2072 int regno;
2074 int i;
2075 int class = (int) NO_REGS;
2077 for (i = 0; i < (int) N_REG_CLASSES; i++)
2078 if (chain->need.groups[i] > 0)
2080 class = i;
2081 break;
2084 if (class == (int) NO_REGS)
2085 return 1;
2087 /* Consider each pair of consecutive registers. */
2088 for (i = 0; i < FIRST_PSEUDO_REGISTER - 1; i++)
2090 /* Ignore pairs that include reg REGNO. */
2091 if (i == regno || i + 1 == regno)
2092 continue;
2094 /* Ignore pairs that are outside the class that needs the group.
2095 ??? Here we fail to handle the case where two different classes
2096 independently need groups. But this never happens with our
2097 current machine descriptions. */
2098 if (! (TEST_HARD_REG_BIT (reg_class_contents[class], i)
2099 && TEST_HARD_REG_BIT (reg_class_contents[class], i + 1)))
2100 continue;
2102 /* A pair of consecutive regs we can still spill does the trick. */
2103 if (spill_reg_order[i] < 0 && spill_reg_order[i + 1] < 0
2104 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2105 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1))
2106 return 1;
2108 /* A pair of one already spilled and one we can spill does it
2109 provided the one already spilled is not otherwise reserved. */
2110 if (spill_reg_order[i] < 0
2111 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2112 && spill_reg_order[i + 1] >= 0
2113 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, i + 1)
2114 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, i + 1))
2115 return 1;
2116 if (spill_reg_order[i + 1] < 0
2117 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1)
2118 && spill_reg_order[i] >= 0
2119 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, i)
2120 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, i))
2121 return 1;
2124 return 0;
2127 /* Count any groups of CLASS that can be formed from the registers recently
2128 spilled. */
2130 static void
2131 count_possible_groups (chain, class)
2132 struct insn_chain *chain;
2133 int class;
2135 HARD_REG_SET new;
2136 int i, j;
2138 /* Now find all consecutive groups of spilled registers
2139 and mark each group off against the need for such groups.
2140 But don't count them against ordinary need, yet. */
2142 if (chain->group_size[class] == 0)
2143 return;
2145 CLEAR_HARD_REG_SET (new);
2147 /* Make a mask of all the regs that are spill regs in class I. */
2148 for (i = 0; i < n_spills; i++)
2150 int regno = spill_regs[i];
2152 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
2153 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, regno)
2154 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, regno))
2155 SET_HARD_REG_BIT (new, regno);
2158 /* Find each consecutive group of them. */
2159 for (i = 0; i < FIRST_PSEUDO_REGISTER && chain->need.groups[class] > 0; i++)
2160 if (TEST_HARD_REG_BIT (new, i)
2161 && i + chain->group_size[class] <= FIRST_PSEUDO_REGISTER
2162 && HARD_REGNO_MODE_OK (i, chain->group_mode[class]))
2164 for (j = 1; j < chain->group_size[class]; j++)
2165 if (! TEST_HARD_REG_BIT (new, i + j))
2166 break;
2168 if (j == chain->group_size[class])
2170 /* We found a group. Mark it off against this class's need for
2171 groups, and against each superclass too. */
2172 register enum reg_class *p;
2174 chain->need.groups[class]--;
2175 p = reg_class_superclasses[class];
2176 while (*p != LIM_REG_CLASSES)
2178 if (chain->group_size [(int) *p] <= chain->group_size [class])
2179 chain->need.groups[(int) *p]--;
2180 p++;
2183 /* Don't count these registers again. */
2184 for (j = 0; j < chain->group_size[class]; j++)
2185 SET_HARD_REG_BIT (chain->counted_for_groups, i + j);
2188 /* Skip to the last reg in this group. When i is incremented above,
2189 it will then point to the first reg of the next possible group. */
2190 i += j - 1;
2194 /* ALLOCATE_MODE is a register mode that needs to be reloaded. OTHER_MODE is
2195 another mode that needs to be reloaded for the same register class CLASS.
2196 If any reg in CLASS allows ALLOCATE_MODE but not OTHER_MODE, fail.
2197 ALLOCATE_MODE will never be smaller than OTHER_MODE.
2199 This code used to also fail if any reg in CLASS allows OTHER_MODE but not
2200 ALLOCATE_MODE. This test is unnecessary, because we will never try to put
2201 something of mode ALLOCATE_MODE into an OTHER_MODE register. Testing this
2202 causes unnecessary failures on machines requiring alignment of register
2203 groups when the two modes are different sizes, because the larger mode has
2204 more strict alignment rules than the smaller mode. */
2206 static int
2207 modes_equiv_for_class_p (allocate_mode, other_mode, class)
2208 enum machine_mode allocate_mode, other_mode;
2209 enum reg_class class;
2211 register int regno;
2212 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2214 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno)
2215 && HARD_REGNO_MODE_OK (regno, allocate_mode)
2216 && ! HARD_REGNO_MODE_OK (regno, other_mode))
2217 return 0;
2219 return 1;
2222 /* Handle the failure to find a register to spill.
2223 INSN should be one of the insns which needed this particular spill reg. */
2225 static void
2226 spill_failure (insn)
2227 rtx insn;
2229 if (asm_noperands (PATTERN (insn)) >= 0)
2230 error_for_asm (insn, "`asm' needs too many reloads");
2231 else
2232 fatal_insn ("Unable to find a register to spill.", insn);
2235 /* Add a new register to the tables of available spill-registers.
2236 CHAIN is the insn for which the register will be used; we decrease the
2237 needs of that insn.
2238 I is the index of this register in potential_reload_regs.
2239 CLASS is the regclass whose need is being satisfied.
2240 NONGROUP is 0 if this register is part of a group.
2241 DUMPFILE is the same as the one that `reload' got. */
2243 static void
2244 new_spill_reg (chain, i, class, nongroup, dumpfile)
2245 struct insn_chain *chain;
2246 int i;
2247 int class;
2248 int nongroup;
2249 FILE *dumpfile;
2251 register enum reg_class *p;
2252 int regno = potential_reload_regs[i];
2254 if (i >= FIRST_PSEUDO_REGISTER)
2256 spill_failure (chain->insn);
2257 failure = 1;
2258 return;
2261 if (TEST_HARD_REG_BIT (bad_spill_regs, regno))
2263 static const char * const reg_class_names[] = REG_CLASS_NAMES;
2265 if (asm_noperands (PATTERN (chain->insn)) < 0)
2267 /* The error message is still correct - we know only that it wasn't
2268 an asm statement that caused the problem, but one of the global
2269 registers declared by the users might have screwed us. */
2270 error ("fixed or forbidden register %d (%s) was spilled for class %s.",
2271 regno, reg_names[regno], reg_class_names[class]);
2272 error ("This may be due to a compiler bug or to impossible asm");
2273 error ("statements or clauses.");
2274 fatal_insn ("This is the instruction:", chain->insn);
2276 error_for_asm (chain->insn, "Invalid `asm' statement:");
2277 error_for_asm (chain->insn,
2278 "fixed or forbidden register %d (%s) was spilled for class %s.",
2279 regno, reg_names[regno], reg_class_names[class]);
2280 failure = 1;
2281 return;
2284 /* Make reg REGNO an additional reload reg. */
2286 potential_reload_regs[i] = -1;
2287 spill_regs[n_spills] = regno;
2288 spill_reg_order[regno] = n_spills;
2289 if (dumpfile)
2290 fprintf (dumpfile, "Spilling reg %d.\n", regno);
2291 SET_HARD_REG_BIT (chain->used_spill_regs, regno);
2293 /* Clear off the needs we just satisfied. */
2295 chain->need.regs[0][class]--;
2296 p = reg_class_superclasses[class];
2297 while (*p != LIM_REG_CLASSES)
2298 chain->need.regs[0][(int) *p++]--;
2300 if (nongroup && chain->need.regs[1][class] > 0)
2302 SET_HARD_REG_BIT (chain->counted_for_nongroups, regno);
2303 chain->need.regs[1][class]--;
2304 p = reg_class_superclasses[class];
2305 while (*p != LIM_REG_CLASSES)
2306 chain->need.regs[1][(int) *p++]--;
2309 n_spills++;
2312 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2313 data that is dead in INSN. */
2315 static void
2316 delete_dead_insn (insn)
2317 rtx insn;
2319 rtx prev = prev_real_insn (insn);
2320 rtx prev_dest;
2322 /* If the previous insn sets a register that dies in our insn, delete it
2323 too. */
2324 if (prev && GET_CODE (PATTERN (prev)) == SET
2325 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
2326 && reg_mentioned_p (prev_dest, PATTERN (insn))
2327 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
2328 && ! side_effects_p (SET_SRC (PATTERN (prev))))
2329 delete_dead_insn (prev);
2331 PUT_CODE (insn, NOTE);
2332 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2333 NOTE_SOURCE_FILE (insn) = 0;
2336 /* Modify the home of pseudo-reg I.
2337 The new home is present in reg_renumber[I].
2339 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2340 or it may be -1, meaning there is none or it is not relevant.
2341 This is used so that all pseudos spilled from a given hard reg
2342 can share one stack slot. */
2344 static void
2345 alter_reg (i, from_reg)
2346 register int i;
2347 int from_reg;
2349 /* When outputting an inline function, this can happen
2350 for a reg that isn't actually used. */
2351 if (regno_reg_rtx[i] == 0)
2352 return;
2354 /* If the reg got changed to a MEM at rtl-generation time,
2355 ignore it. */
2356 if (GET_CODE (regno_reg_rtx[i]) != REG)
2357 return;
2359 /* Modify the reg-rtx to contain the new hard reg
2360 number or else to contain its pseudo reg number. */
2361 REGNO (regno_reg_rtx[i])
2362 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
2364 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2365 allocate a stack slot for it. */
2367 if (reg_renumber[i] < 0
2368 && REG_N_REFS (i) > 0
2369 && reg_equiv_constant[i] == 0
2370 && reg_equiv_memory_loc[i] == 0)
2372 register rtx x;
2373 int inherent_size = PSEUDO_REGNO_BYTES (i);
2374 int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2375 int adjust = 0;
2377 /* Each pseudo reg has an inherent size which comes from its own mode,
2378 and a total size which provides room for paradoxical subregs
2379 which refer to the pseudo reg in wider modes.
2381 We can use a slot already allocated if it provides both
2382 enough inherent space and enough total space.
2383 Otherwise, we allocate a new slot, making sure that it has no less
2384 inherent space, and no less total space, then the previous slot. */
2385 if (from_reg == -1)
2387 /* No known place to spill from => no slot to reuse. */
2388 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
2389 inherent_size == total_size ? 0 : -1);
2390 if (BYTES_BIG_ENDIAN)
2391 /* Cancel the big-endian correction done in assign_stack_local.
2392 Get the address of the beginning of the slot.
2393 This is so we can do a big-endian correction unconditionally
2394 below. */
2395 adjust = inherent_size - total_size;
2397 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2399 /* Reuse a stack slot if possible. */
2400 else if (spill_stack_slot[from_reg] != 0
2401 && spill_stack_slot_width[from_reg] >= total_size
2402 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2403 >= inherent_size))
2404 x = spill_stack_slot[from_reg];
2405 /* Allocate a bigger slot. */
2406 else
2408 /* Compute maximum size needed, both for inherent size
2409 and for total size. */
2410 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2411 rtx stack_slot;
2412 if (spill_stack_slot[from_reg])
2414 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2415 > inherent_size)
2416 mode = GET_MODE (spill_stack_slot[from_reg]);
2417 if (spill_stack_slot_width[from_reg] > total_size)
2418 total_size = spill_stack_slot_width[from_reg];
2420 /* Make a slot with that size. */
2421 x = assign_stack_local (mode, total_size,
2422 inherent_size == total_size ? 0 : -1);
2423 stack_slot = x;
2424 if (BYTES_BIG_ENDIAN)
2426 /* Cancel the big-endian correction done in assign_stack_local.
2427 Get the address of the beginning of the slot.
2428 This is so we can do a big-endian correction unconditionally
2429 below. */
2430 adjust = GET_MODE_SIZE (mode) - total_size;
2431 if (adjust)
2432 stack_slot = gen_rtx_MEM (mode_for_size (total_size
2433 * BITS_PER_UNIT,
2434 MODE_INT, 1),
2435 plus_constant (XEXP (x, 0), adjust));
2437 spill_stack_slot[from_reg] = stack_slot;
2438 spill_stack_slot_width[from_reg] = total_size;
2441 /* On a big endian machine, the "address" of the slot
2442 is the address of the low part that fits its inherent mode. */
2443 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2444 adjust += (total_size - inherent_size);
2446 /* If we have any adjustment to make, or if the stack slot is the
2447 wrong mode, make a new stack slot. */
2448 if (adjust != 0 || GET_MODE (x) != GET_MODE (regno_reg_rtx[i]))
2450 x = gen_rtx_MEM (GET_MODE (regno_reg_rtx[i]),
2451 plus_constant (XEXP (x, 0), adjust));
2453 /* If this was shared among registers, must ensure we never
2454 set it readonly since that can cause scheduling
2455 problems. Note we would only have in this adjustment
2456 case in any event, since the code above doesn't set it. */
2458 if (from_reg == -1)
2459 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2462 /* Save the stack slot for later. */
2463 reg_equiv_memory_loc[i] = x;
2467 /* Mark the slots in regs_ever_live for the hard regs
2468 used by pseudo-reg number REGNO. */
2470 void
2471 mark_home_live (regno)
2472 int regno;
2474 register int i, lim;
2475 i = reg_renumber[regno];
2476 if (i < 0)
2477 return;
2478 lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2479 while (i < lim)
2480 regs_ever_live[i++] = 1;
2483 /* This function handles the tracking of elimination offsets around branches.
2485 X is a piece of RTL being scanned.
2487 INSN is the insn that it came from, if any.
2489 INITIAL_P is non-zero if we are to set the offset to be the initial
2490 offset and zero if we are setting the offset of the label to be the
2491 current offset. */
2493 static void
2494 set_label_offsets (x, insn, initial_p)
2495 rtx x;
2496 rtx insn;
2497 int initial_p;
2499 enum rtx_code code = GET_CODE (x);
2500 rtx tem;
2501 unsigned int i;
2502 struct elim_table *p;
2504 switch (code)
2506 case LABEL_REF:
2507 if (LABEL_REF_NONLOCAL_P (x))
2508 return;
2510 x = XEXP (x, 0);
2512 /* ... fall through ... */
2514 case CODE_LABEL:
2515 /* If we know nothing about this label, set the desired offsets. Note
2516 that this sets the offset at a label to be the offset before a label
2517 if we don't know anything about the label. This is not correct for
2518 the label after a BARRIER, but is the best guess we can make. If
2519 we guessed wrong, we will suppress an elimination that might have
2520 been possible had we been able to guess correctly. */
2522 if (! offsets_known_at[CODE_LABEL_NUMBER (x)])
2524 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2525 offsets_at[CODE_LABEL_NUMBER (x)][i]
2526 = (initial_p ? reg_eliminate[i].initial_offset
2527 : reg_eliminate[i].offset);
2528 offsets_known_at[CODE_LABEL_NUMBER (x)] = 1;
2531 /* Otherwise, if this is the definition of a label and it is
2532 preceded by a BARRIER, set our offsets to the known offset of
2533 that label. */
2535 else if (x == insn
2536 && (tem = prev_nonnote_insn (insn)) != 0
2537 && GET_CODE (tem) == BARRIER)
2538 set_offsets_for_label (insn);
2539 else
2540 /* If neither of the above cases is true, compare each offset
2541 with those previously recorded and suppress any eliminations
2542 where the offsets disagree. */
2544 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2545 if (offsets_at[CODE_LABEL_NUMBER (x)][i]
2546 != (initial_p ? reg_eliminate[i].initial_offset
2547 : reg_eliminate[i].offset))
2548 reg_eliminate[i].can_eliminate = 0;
2550 return;
2552 case JUMP_INSN:
2553 set_label_offsets (PATTERN (insn), insn, initial_p);
2555 /* ... fall through ... */
2557 case INSN:
2558 case CALL_INSN:
2559 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2560 and hence must have all eliminations at their initial offsets. */
2561 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2562 if (REG_NOTE_KIND (tem) == REG_LABEL)
2563 set_label_offsets (XEXP (tem, 0), insn, 1);
2564 return;
2566 case ADDR_VEC:
2567 case ADDR_DIFF_VEC:
2568 /* Each of the labels in the address vector must be at their initial
2569 offsets. We want the first field for ADDR_VEC and the second
2570 field for ADDR_DIFF_VEC. */
2572 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2573 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2574 insn, initial_p);
2575 return;
2577 case SET:
2578 /* We only care about setting PC. If the source is not RETURN,
2579 IF_THEN_ELSE, or a label, disable any eliminations not at
2580 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2581 isn't one of those possibilities. For branches to a label,
2582 call ourselves recursively.
2584 Note that this can disable elimination unnecessarily when we have
2585 a non-local goto since it will look like a non-constant jump to
2586 someplace in the current function. This isn't a significant
2587 problem since such jumps will normally be when all elimination
2588 pairs are back to their initial offsets. */
2590 if (SET_DEST (x) != pc_rtx)
2591 return;
2593 switch (GET_CODE (SET_SRC (x)))
2595 case PC:
2596 case RETURN:
2597 return;
2599 case LABEL_REF:
2600 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2601 return;
2603 case IF_THEN_ELSE:
2604 tem = XEXP (SET_SRC (x), 1);
2605 if (GET_CODE (tem) == LABEL_REF)
2606 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2607 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2608 break;
2610 tem = XEXP (SET_SRC (x), 2);
2611 if (GET_CODE (tem) == LABEL_REF)
2612 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2613 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2614 break;
2615 return;
2617 default:
2618 break;
2621 /* If we reach here, all eliminations must be at their initial
2622 offset because we are doing a jump to a variable address. */
2623 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2624 if (p->offset != p->initial_offset)
2625 p->can_eliminate = 0;
2626 break;
2628 default:
2629 break;
2633 /* Scan X and replace any eliminable registers (such as fp) with a
2634 replacement (such as sp), plus an offset.
2636 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2637 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2638 MEM, we are allowed to replace a sum of a register and the constant zero
2639 with the register, which we cannot do outside a MEM. In addition, we need
2640 to record the fact that a register is referenced outside a MEM.
2642 If INSN is an insn, it is the insn containing X. If we replace a REG
2643 in a SET_DEST with an equivalent MEM and INSN is non-zero, write a
2644 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2645 the REG is being modified.
2647 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2648 That's used when we eliminate in expressions stored in notes.
2649 This means, do not set ref_outside_mem even if the reference
2650 is outside of MEMs.
2652 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2653 replacements done assuming all offsets are at their initial values. If
2654 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2655 encounter, return the actual location so that find_reloads will do
2656 the proper thing. */
2659 eliminate_regs (x, mem_mode, insn)
2660 rtx x;
2661 enum machine_mode mem_mode;
2662 rtx insn;
2664 enum rtx_code code = GET_CODE (x);
2665 struct elim_table *ep;
2666 int regno;
2667 rtx new;
2668 int i, j;
2669 const char *fmt;
2670 int copied = 0;
2672 if (! current_function_decl)
2673 return x;
2675 switch (code)
2677 case CONST_INT:
2678 case CONST_DOUBLE:
2679 case CONST:
2680 case SYMBOL_REF:
2681 case CODE_LABEL:
2682 case PC:
2683 case CC0:
2684 case ASM_INPUT:
2685 case ADDR_VEC:
2686 case ADDR_DIFF_VEC:
2687 case RETURN:
2688 return x;
2690 case ADDRESSOF:
2691 /* This is only for the benefit of the debugging backends, which call
2692 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2693 removed after CSE. */
2694 new = eliminate_regs (XEXP (x, 0), 0, insn);
2695 if (GET_CODE (new) == MEM)
2696 return XEXP (new, 0);
2697 return x;
2699 case REG:
2700 regno = REGNO (x);
2702 /* First handle the case where we encounter a bare register that
2703 is eliminable. Replace it with a PLUS. */
2704 if (regno < FIRST_PSEUDO_REGISTER)
2706 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2707 ep++)
2708 if (ep->from_rtx == x && ep->can_eliminate)
2709 return plus_constant (ep->to_rtx, ep->previous_offset);
2712 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2713 && reg_equiv_constant[regno]
2714 && ! CONSTANT_P (reg_equiv_constant[regno]))
2715 return eliminate_regs (copy_rtx (reg_equiv_constant[regno]),
2716 mem_mode, insn);
2717 return x;
2719 /* You might think handling MINUS in a manner similar to PLUS is a
2720 good idea. It is not. It has been tried multiple times and every
2721 time the change has had to have been reverted.
2723 Other parts of reload know a PLUS is special (gen_reload for example)
2724 and require special code to handle code a reloaded PLUS operand.
2726 Also consider backends where the flags register is clobbered by a
2727 MINUS, but we can emit a PLUS that does not clobber flags (ia32,
2728 lea instruction comes to mind). If we try to reload a MINUS, we
2729 may kill the flags register that was holding a useful value.
2731 So, please before trying to handle MINUS, consider reload as a
2732 whole instead of this little section as well as the backend issues. */
2733 case PLUS:
2734 /* If this is the sum of an eliminable register and a constant, rework
2735 the sum. */
2736 if (GET_CODE (XEXP (x, 0)) == REG
2737 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2738 && CONSTANT_P (XEXP (x, 1)))
2740 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2741 ep++)
2742 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2744 /* The only time we want to replace a PLUS with a REG (this
2745 occurs when the constant operand of the PLUS is the negative
2746 of the offset) is when we are inside a MEM. We won't want
2747 to do so at other times because that would change the
2748 structure of the insn in a way that reload can't handle.
2749 We special-case the commonest situation in
2750 eliminate_regs_in_insn, so just replace a PLUS with a
2751 PLUS here, unless inside a MEM. */
2752 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2753 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2754 return ep->to_rtx;
2755 else
2756 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2757 plus_constant (XEXP (x, 1),
2758 ep->previous_offset));
2761 /* If the register is not eliminable, we are done since the other
2762 operand is a constant. */
2763 return x;
2766 /* If this is part of an address, we want to bring any constant to the
2767 outermost PLUS. We will do this by doing register replacement in
2768 our operands and seeing if a constant shows up in one of them.
2770 Note that there is no risk of modifying the structure of the insn,
2771 since we only get called for its operands, thus we are either
2772 modifying the address inside a MEM, or something like an address
2773 operand of a load-address insn. */
2776 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2777 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2779 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2781 /* If one side is a PLUS and the other side is a pseudo that
2782 didn't get a hard register but has a reg_equiv_constant,
2783 we must replace the constant here since it may no longer
2784 be in the position of any operand. */
2785 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2786 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2787 && reg_renumber[REGNO (new1)] < 0
2788 && reg_equiv_constant != 0
2789 && reg_equiv_constant[REGNO (new1)] != 0)
2790 new1 = reg_equiv_constant[REGNO (new1)];
2791 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2792 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2793 && reg_renumber[REGNO (new0)] < 0
2794 && reg_equiv_constant[REGNO (new0)] != 0)
2795 new0 = reg_equiv_constant[REGNO (new0)];
2797 new = form_sum (new0, new1);
2799 /* As above, if we are not inside a MEM we do not want to
2800 turn a PLUS into something else. We might try to do so here
2801 for an addition of 0 if we aren't optimizing. */
2802 if (! mem_mode && GET_CODE (new) != PLUS)
2803 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2804 else
2805 return new;
2808 return x;
2810 case MULT:
2811 /* If this is the product of an eliminable register and a
2812 constant, apply the distribute law and move the constant out
2813 so that we have (plus (mult ..) ..). This is needed in order
2814 to keep load-address insns valid. This case is pathological.
2815 We ignore the possibility of overflow here. */
2816 if (GET_CODE (XEXP (x, 0)) == REG
2817 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2818 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2819 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2820 ep++)
2821 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2823 if (! mem_mode
2824 /* Refs inside notes don't count for this purpose. */
2825 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2826 || GET_CODE (insn) == INSN_LIST)))
2827 ep->ref_outside_mem = 1;
2829 return
2830 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2831 ep->previous_offset * INTVAL (XEXP (x, 1)));
2834 /* ... fall through ... */
2836 case CALL:
2837 case COMPARE:
2838 /* See comments before PLUS about handling MINUS. */
2839 case MINUS:
2840 case DIV: case UDIV:
2841 case MOD: case UMOD:
2842 case AND: case IOR: case XOR:
2843 case ROTATERT: case ROTATE:
2844 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2845 case NE: case EQ:
2846 case GE: case GT: case GEU: case GTU:
2847 case LE: case LT: case LEU: case LTU:
2849 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2850 rtx new1
2851 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
2853 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2854 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2856 return x;
2858 case EXPR_LIST:
2859 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2860 if (XEXP (x, 0))
2862 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2863 if (new != XEXP (x, 0))
2865 /* If this is a REG_DEAD note, it is not valid anymore.
2866 Using the eliminated version could result in creating a
2867 REG_DEAD note for the stack or frame pointer. */
2868 if (GET_MODE (x) == REG_DEAD)
2869 return (XEXP (x, 1)
2870 ? eliminate_regs (XEXP (x, 1), mem_mode, insn)
2871 : NULL_RTX);
2873 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2877 /* ... fall through ... */
2879 case INSN_LIST:
2880 /* Now do eliminations in the rest of the chain. If this was
2881 an EXPR_LIST, this might result in allocating more memory than is
2882 strictly needed, but it simplifies the code. */
2883 if (XEXP (x, 1))
2885 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2886 if (new != XEXP (x, 1))
2887 return gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2889 return x;
2891 case PRE_INC:
2892 case POST_INC:
2893 case PRE_DEC:
2894 case POST_DEC:
2895 case STRICT_LOW_PART:
2896 case NEG: case NOT:
2897 case SIGN_EXTEND: case ZERO_EXTEND:
2898 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2899 case FLOAT: case FIX:
2900 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2901 case ABS:
2902 case SQRT:
2903 case FFS:
2904 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2905 if (new != XEXP (x, 0))
2906 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2907 return x;
2909 case SUBREG:
2910 /* Similar to above processing, but preserve SUBREG_WORD.
2911 Convert (subreg (mem)) to (mem) if not paradoxical.
2912 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2913 pseudo didn't get a hard reg, we must replace this with the
2914 eliminated version of the memory location because push_reloads
2915 may do the replacement in certain circumstances. */
2916 if (GET_CODE (SUBREG_REG (x)) == REG
2917 && (GET_MODE_SIZE (GET_MODE (x))
2918 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2919 && reg_equiv_memory_loc != 0
2920 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2922 new = SUBREG_REG (x);
2924 else
2925 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
2927 if (new != XEXP (x, 0))
2929 int x_size = GET_MODE_SIZE (GET_MODE (x));
2930 int new_size = GET_MODE_SIZE (GET_MODE (new));
2932 if (GET_CODE (new) == MEM
2933 && ((x_size < new_size
2934 #ifdef WORD_REGISTER_OPERATIONS
2935 /* On these machines, combine can create rtl of the form
2936 (set (subreg:m1 (reg:m2 R) 0) ...)
2937 where m1 < m2, and expects something interesting to
2938 happen to the entire word. Moreover, it will use the
2939 (reg:m2 R) later, expecting all bits to be preserved.
2940 So if the number of words is the same, preserve the
2941 subreg so that push_reloads can see it. */
2942 && ! ((x_size-1)/UNITS_PER_WORD == (new_size-1)/UNITS_PER_WORD)
2943 #endif
2945 || (x_size == new_size))
2948 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
2949 enum machine_mode mode = GET_MODE (x);
2951 if (BYTES_BIG_ENDIAN)
2952 offset += (MIN (UNITS_PER_WORD,
2953 GET_MODE_SIZE (GET_MODE (new)))
2954 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
2956 PUT_MODE (new, mode);
2957 XEXP (new, 0) = plus_constant (XEXP (new, 0), offset);
2958 return new;
2960 else
2961 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_WORD (x));
2964 return x;
2966 case MEM:
2967 /* This is only for the benefit of the debugging backends, which call
2968 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2969 removed after CSE. */
2970 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
2971 return eliminate_regs (XEXP (XEXP (x, 0), 0), 0, insn);
2973 /* Our only special processing is to pass the mode of the MEM to our
2974 recursive call and copy the flags. While we are here, handle this
2975 case more efficiently. */
2976 new = eliminate_regs (XEXP (x, 0), GET_MODE (x), insn);
2977 if (new != XEXP (x, 0))
2979 new = gen_rtx_MEM (GET_MODE (x), new);
2980 new->volatil = x->volatil;
2981 new->unchanging = x->unchanging;
2982 new->in_struct = x->in_struct;
2983 return new;
2985 else
2986 return x;
2988 case USE:
2989 case CLOBBER:
2990 case ASM_OPERANDS:
2991 case SET:
2992 abort ();
2994 default:
2995 break;
2998 /* Process each of our operands recursively. If any have changed, make a
2999 copy of the rtx. */
3000 fmt = GET_RTX_FORMAT (code);
3001 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3003 if (*fmt == 'e')
3005 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
3006 if (new != XEXP (x, i) && ! copied)
3008 rtx new_x = rtx_alloc (code);
3009 bcopy ((char *) x, (char *) new_x,
3010 (sizeof (*new_x) - sizeof (new_x->fld)
3011 + sizeof (new_x->fld[0]) * GET_RTX_LENGTH (code)));
3012 x = new_x;
3013 copied = 1;
3015 XEXP (x, i) = new;
3017 else if (*fmt == 'E')
3019 int copied_vec = 0;
3020 for (j = 0; j < XVECLEN (x, i); j++)
3022 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
3023 if (new != XVECEXP (x, i, j) && ! copied_vec)
3025 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
3026 XVEC (x, i)->elem);
3027 if (! copied)
3029 rtx new_x = rtx_alloc (code);
3030 bcopy ((char *) x, (char *) new_x,
3031 (sizeof (*new_x) - sizeof (new_x->fld)
3032 + (sizeof (new_x->fld[0])
3033 * GET_RTX_LENGTH (code))));
3034 x = new_x;
3035 copied = 1;
3037 XVEC (x, i) = new_v;
3038 copied_vec = 1;
3040 XVECEXP (x, i, j) = new;
3045 return x;
3048 /* Scan rtx X for modifications of elimination target registers. Update
3049 the table of eliminables to reflect the changed state. MEM_MODE is
3050 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
3052 static void
3053 elimination_effects (x, mem_mode)
3054 rtx x;
3055 enum machine_mode mem_mode;
3058 enum rtx_code code = GET_CODE (x);
3059 struct elim_table *ep;
3060 int regno;
3061 int i, j;
3062 const char *fmt;
3064 switch (code)
3066 case CONST_INT:
3067 case CONST_DOUBLE:
3068 case CONST:
3069 case SYMBOL_REF:
3070 case CODE_LABEL:
3071 case PC:
3072 case CC0:
3073 case ASM_INPUT:
3074 case ADDR_VEC:
3075 case ADDR_DIFF_VEC:
3076 case RETURN:
3077 return;
3079 case ADDRESSOF:
3080 abort ();
3082 case REG:
3083 regno = REGNO (x);
3085 /* First handle the case where we encounter a bare register that
3086 is eliminable. Replace it with a PLUS. */
3087 if (regno < FIRST_PSEUDO_REGISTER)
3089 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3090 ep++)
3091 if (ep->from_rtx == x && ep->can_eliminate)
3093 if (! mem_mode)
3094 ep->ref_outside_mem = 1;
3095 return;
3099 else if (reg_renumber[regno] < 0 && reg_equiv_constant
3100 && reg_equiv_constant[regno]
3101 && ! CONSTANT_P (reg_equiv_constant[regno]))
3102 elimination_effects (reg_equiv_constant[regno], mem_mode);
3103 return;
3105 case PRE_INC:
3106 case POST_INC:
3107 case PRE_DEC:
3108 case POST_DEC:
3109 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3110 if (ep->to_rtx == XEXP (x, 0))
3112 int size = GET_MODE_SIZE (mem_mode);
3114 /* If more bytes than MEM_MODE are pushed, account for them. */
3115 #ifdef PUSH_ROUNDING
3116 if (ep->to_rtx == stack_pointer_rtx)
3117 size = PUSH_ROUNDING (size);
3118 #endif
3119 if (code == PRE_DEC || code == POST_DEC)
3120 ep->offset += size;
3121 else
3122 ep->offset -= size;
3125 /* Fall through to generic unary operation case. */
3126 case STRICT_LOW_PART:
3127 case NEG: case NOT:
3128 case SIGN_EXTEND: case ZERO_EXTEND:
3129 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3130 case FLOAT: case FIX:
3131 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3132 case ABS:
3133 case SQRT:
3134 case FFS:
3135 elimination_effects (XEXP (x, 0), mem_mode);
3136 return;
3138 case SUBREG:
3139 if (GET_CODE (SUBREG_REG (x)) == REG
3140 && (GET_MODE_SIZE (GET_MODE (x))
3141 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3142 && reg_equiv_memory_loc != 0
3143 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
3144 return;
3146 elimination_effects (SUBREG_REG (x), mem_mode);
3147 return;
3149 case USE:
3150 /* If using a register that is the source of an eliminate we still
3151 think can be performed, note it cannot be performed since we don't
3152 know how this register is used. */
3153 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3154 if (ep->from_rtx == XEXP (x, 0))
3155 ep->can_eliminate = 0;
3157 elimination_effects (XEXP (x, 0), mem_mode);
3158 return;
3160 case CLOBBER:
3161 /* If clobbering a register that is the replacement register for an
3162 elimination we still think can be performed, note that it cannot
3163 be performed. Otherwise, we need not be concerned about it. */
3164 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3165 if (ep->to_rtx == XEXP (x, 0))
3166 ep->can_eliminate = 0;
3168 elimination_effects (XEXP (x, 0), mem_mode);
3169 return;
3171 case SET:
3172 /* Check for setting a register that we know about. */
3173 if (GET_CODE (SET_DEST (x)) == REG)
3175 /* See if this is setting the replacement register for an
3176 elimination.
3178 If DEST is the hard frame pointer, we do nothing because we
3179 assume that all assignments to the frame pointer are for
3180 non-local gotos and are being done at a time when they are valid
3181 and do not disturb anything else. Some machines want to
3182 eliminate a fake argument pointer (or even a fake frame pointer)
3183 with either the real frame or the stack pointer. Assignments to
3184 the hard frame pointer must not prevent this elimination. */
3186 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3187 ep++)
3188 if (ep->to_rtx == SET_DEST (x)
3189 && SET_DEST (x) != hard_frame_pointer_rtx)
3191 /* If it is being incremented, adjust the offset. Otherwise,
3192 this elimination can't be done. */
3193 rtx src = SET_SRC (x);
3195 if (GET_CODE (src) == PLUS
3196 && XEXP (src, 0) == SET_DEST (x)
3197 && GET_CODE (XEXP (src, 1)) == CONST_INT)
3198 ep->offset -= INTVAL (XEXP (src, 1));
3199 else
3200 ep->can_eliminate = 0;
3204 elimination_effects (SET_DEST (x), 0);
3205 elimination_effects (SET_SRC (x), 0);
3206 return;
3208 case MEM:
3209 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
3210 abort ();
3212 /* Our only special processing is to pass the mode of the MEM to our
3213 recursive call. */
3214 elimination_effects (XEXP (x, 0), GET_MODE (x));
3215 return;
3217 default:
3218 break;
3221 fmt = GET_RTX_FORMAT (code);
3222 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3224 if (*fmt == 'e')
3225 elimination_effects (XEXP (x, i), mem_mode);
3226 else if (*fmt == 'E')
3227 for (j = 0; j < XVECLEN (x, i); j++)
3228 elimination_effects (XVECEXP (x, i, j), mem_mode);
3232 /* Descend through rtx X and verify that no references to eliminable registers
3233 remain. If any do remain, mark the involved register as not
3234 eliminable. */
3235 static void
3236 check_eliminable_occurrences (x)
3237 rtx x;
3239 const char *fmt;
3240 int i;
3241 enum rtx_code code;
3243 if (x == 0)
3244 return;
3246 code = GET_CODE (x);
3248 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
3250 struct elim_table *ep;
3252 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3253 if (ep->from_rtx == x && ep->can_eliminate)
3254 ep->can_eliminate = 0;
3255 return;
3258 fmt = GET_RTX_FORMAT (code);
3259 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3261 if (*fmt == 'e')
3262 check_eliminable_occurrences (XEXP (x, i));
3263 else if (*fmt == 'E')
3265 int j;
3266 for (j = 0; j < XVECLEN (x, i); j++)
3267 check_eliminable_occurrences (XVECEXP (x, i, j));
3272 /* Scan INSN and eliminate all eliminable registers in it.
3274 If REPLACE is nonzero, do the replacement destructively. Also
3275 delete the insn as dead it if it is setting an eliminable register.
3277 If REPLACE is zero, do all our allocations in reload_obstack.
3279 If no eliminations were done and this insn doesn't require any elimination
3280 processing (these are not identical conditions: it might be updating sp,
3281 but not referencing fp; this needs to be seen during reload_as_needed so
3282 that the offset between fp and sp can be taken into consideration), zero
3283 is returned. Otherwise, 1 is returned. */
3285 static int
3286 eliminate_regs_in_insn (insn, replace)
3287 rtx insn;
3288 int replace;
3290 int icode = recog_memoized (insn);
3291 rtx old_body = PATTERN (insn);
3292 int insn_is_asm = asm_noperands (old_body) >= 0;
3293 rtx old_set = single_set (insn);
3294 rtx new_body;
3295 int val = 0;
3296 int i, any_changes;
3297 rtx substed_operand[MAX_RECOG_OPERANDS];
3298 rtx orig_operand[MAX_RECOG_OPERANDS];
3299 struct elim_table *ep;
3301 if (! insn_is_asm && icode < 0)
3303 if (GET_CODE (PATTERN (insn)) == USE
3304 || GET_CODE (PATTERN (insn)) == CLOBBER
3305 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3306 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3307 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
3308 return 0;
3309 abort ();
3312 if (! replace)
3313 push_obstacks (&reload_obstack, &reload_obstack);
3315 if (old_set != 0 && GET_CODE (SET_DEST (old_set)) == REG
3316 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3318 /* Check for setting an eliminable register. */
3319 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3320 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3322 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3323 /* If this is setting the frame pointer register to the
3324 hardware frame pointer register and this is an elimination
3325 that will be done (tested above), this insn is really
3326 adjusting the frame pointer downward to compensate for
3327 the adjustment done before a nonlocal goto. */
3328 if (ep->from == FRAME_POINTER_REGNUM
3329 && ep->to == HARD_FRAME_POINTER_REGNUM)
3331 rtx src = SET_SRC (old_set);
3332 int offset = 0, ok = 0;
3333 rtx prev_insn, prev_set;
3335 if (src == ep->to_rtx)
3336 offset = 0, ok = 1;
3337 else if (GET_CODE (src) == PLUS
3338 && GET_CODE (XEXP (src, 0)) == CONST_INT
3339 && XEXP (src, 1) == ep->to_rtx)
3340 offset = INTVAL (XEXP (src, 0)), ok = 1;
3341 else if (GET_CODE (src) == PLUS
3342 && GET_CODE (XEXP (src, 1)) == CONST_INT
3343 && XEXP (src, 0) == ep->to_rtx)
3344 offset = INTVAL (XEXP (src, 1)), ok = 1;
3345 else if ((prev_insn = prev_nonnote_insn (insn)) != 0
3346 && (prev_set = single_set (prev_insn)) != 0
3347 && rtx_equal_p (SET_DEST (prev_set), src))
3349 src = SET_SRC (prev_set);
3350 if (src == ep->to_rtx)
3351 offset = 0, ok = 1;
3352 else if (GET_CODE (src) == PLUS
3353 && GET_CODE (XEXP (src, 0)) == CONST_INT
3354 && XEXP (src, 1) == ep->to_rtx)
3355 offset = INTVAL (XEXP (src, 0)), ok = 1;
3356 else if (GET_CODE (src) == PLUS
3357 && GET_CODE (XEXP (src, 1)) == CONST_INT
3358 && XEXP (src, 0) == ep->to_rtx)
3359 offset = INTVAL (XEXP (src, 1)), ok = 1;
3362 if (ok)
3364 if (replace)
3366 rtx src
3367 = plus_constant (ep->to_rtx, offset - ep->offset);
3369 /* First see if this insn remains valid when we
3370 make the change. If not, keep the INSN_CODE
3371 the same and let reload fit it up. */
3372 validate_change (insn, &SET_SRC (old_set), src, 1);
3373 validate_change (insn, &SET_DEST (old_set),
3374 ep->to_rtx, 1);
3375 if (! apply_change_group ())
3377 SET_SRC (old_set) = src;
3378 SET_DEST (old_set) = ep->to_rtx;
3382 val = 1;
3383 goto done;
3386 #endif
3388 /* In this case this insn isn't serving a useful purpose. We
3389 will delete it in reload_as_needed once we know that this
3390 elimination is, in fact, being done.
3392 If REPLACE isn't set, we can't delete this insn, but needn't
3393 process it since it won't be used unless something changes. */
3394 if (replace)
3396 delete_dead_insn (insn);
3397 return 1;
3399 val = 1;
3400 goto done;
3403 /* Check for (set (reg) (plus (reg from) (offset))) where the offset
3404 in the insn is the negative of the offset in FROM. Substitute
3405 (set (reg) (reg to)) for the insn and change its code.
3407 We have to do this here, rather than in eliminate_regs, so that we can
3408 change the insn code. */
3410 if (GET_CODE (SET_SRC (old_set)) == PLUS
3411 && GET_CODE (XEXP (SET_SRC (old_set), 0)) == REG
3412 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT)
3413 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3414 ep++)
3415 if (ep->from_rtx == XEXP (SET_SRC (old_set), 0)
3416 && ep->can_eliminate)
3418 /* We must stop at the first elimination that will be used.
3419 If this one would replace the PLUS with a REG, do it
3420 now. Otherwise, quit the loop and let eliminate_regs
3421 do its normal replacement. */
3422 if (ep->offset == - INTVAL (XEXP (SET_SRC (old_set), 1)))
3424 /* We assume here that we don't need a PARALLEL of
3425 any CLOBBERs for this assignment. There's not
3426 much we can do if we do need it. */
3427 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3428 SET_DEST (old_set),
3429 ep->to_rtx);
3430 INSN_CODE (insn) = -1;
3431 val = 1;
3432 goto done;
3435 break;
3439 /* Determine the effects of this insn on elimination offsets. */
3440 elimination_effects (old_body, 0);
3442 /* Eliminate all eliminable registers occurring in operands that
3443 can be handled by reload. */
3444 extract_insn (insn);
3445 any_changes = 0;
3446 for (i = 0; i < recog_data.n_operands; i++)
3448 orig_operand[i] = recog_data.operand[i];
3449 substed_operand[i] = recog_data.operand[i];
3451 /* For an asm statement, every operand is eliminable. */
3452 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3454 /* Check for setting a register that we know about. */
3455 if (recog_data.operand_type[i] != OP_IN
3456 && GET_CODE (orig_operand[i]) == REG)
3458 /* If we are assigning to a register that can be eliminated, it
3459 must be as part of a PARALLEL, since the code above handles
3460 single SETs. We must indicate that we can no longer
3461 eliminate this reg. */
3462 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3463 ep++)
3464 if (ep->from_rtx == orig_operand[i] && ep->can_eliminate)
3465 ep->can_eliminate = 0;
3468 substed_operand[i] = eliminate_regs (recog_data.operand[i], 0,
3469 replace ? insn : NULL_RTX);
3470 if (substed_operand[i] != orig_operand[i])
3471 val = any_changes = 1;
3472 /* Terminate the search in check_eliminable_occurrences at
3473 this point. */
3474 *recog_data.operand_loc[i] = 0;
3476 /* If an output operand changed from a REG to a MEM and INSN is an
3477 insn, write a CLOBBER insn. */
3478 if (recog_data.operand_type[i] != OP_IN
3479 && GET_CODE (orig_operand[i]) == REG
3480 && GET_CODE (substed_operand[i]) == MEM
3481 && replace)
3482 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3483 insn);
3487 for (i = 0; i < recog_data.n_dups; i++)
3488 *recog_data.dup_loc[i]
3489 = *recog_data.operand_loc[(int)recog_data.dup_num[i]];
3491 /* If any eliminable remain, they aren't eliminable anymore. */
3492 check_eliminable_occurrences (old_body);
3494 /* Substitute the operands; the new values are in the substed_operand
3495 array. */
3496 for (i = 0; i < recog_data.n_operands; i++)
3497 *recog_data.operand_loc[i] = substed_operand[i];
3498 for (i = 0; i < recog_data.n_dups; i++)
3499 *recog_data.dup_loc[i] = substed_operand[(int)recog_data.dup_num[i]];
3501 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3502 re-recognize the insn. We do this in case we had a simple addition
3503 but now can do this as a load-address. This saves an insn in this
3504 common case.
3505 If re-recognition fails, the old insn code number will still be used,
3506 and some register operands may have changed into PLUS expressions.
3507 These will be handled by find_reloads by loading them into a register
3508 again.*/
3510 if (val)
3512 /* If we aren't replacing things permanently and we changed something,
3513 make another copy to ensure that all the RTL is new. Otherwise
3514 things can go wrong if find_reload swaps commutative operands
3515 and one is inside RTL that has been copied while the other is not. */
3516 new_body = old_body;
3517 if (! replace)
3519 new_body = copy_insn (old_body);
3520 if (REG_NOTES (insn))
3521 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3523 PATTERN (insn) = new_body;
3525 /* If we had a move insn but now we don't, rerecognize it. This will
3526 cause spurious re-recognition if the old move had a PARALLEL since
3527 the new one still will, but we can't call single_set without
3528 having put NEW_BODY into the insn and the re-recognition won't
3529 hurt in this rare case. */
3530 /* ??? Why this huge if statement - why don't we just rerecognize the
3531 thing always? */
3532 if (! insn_is_asm
3533 && old_set != 0
3534 && ((GET_CODE (SET_SRC (old_set)) == REG
3535 && (GET_CODE (new_body) != SET
3536 || GET_CODE (SET_SRC (new_body)) != REG))
3537 /* If this was a load from or store to memory, compare
3538 the MEM in recog_data.operand to the one in the insn.
3539 If they are not equal, then rerecognize the insn. */
3540 || (old_set != 0
3541 && ((GET_CODE (SET_SRC (old_set)) == MEM
3542 && SET_SRC (old_set) != recog_data.operand[1])
3543 || (GET_CODE (SET_DEST (old_set)) == MEM
3544 && SET_DEST (old_set) != recog_data.operand[0])))
3545 /* If this was an add insn before, rerecognize. */
3546 || GET_CODE (SET_SRC (old_set)) == PLUS))
3548 int new_icode = recog (PATTERN (insn), insn, 0);
3549 if (new_icode < 0)
3550 INSN_CODE (insn) = icode;
3554 /* Restore the old body. If there were any changes to it, we made a copy
3555 of it while the changes were still in place, so we'll correctly return
3556 a modified insn below. */
3557 if (! replace)
3559 /* Restore the old body. */
3560 for (i = 0; i < recog_data.n_operands; i++)
3561 *recog_data.operand_loc[i] = orig_operand[i];
3562 for (i = 0; i < recog_data.n_dups; i++)
3563 *recog_data.dup_loc[i] = orig_operand[(int)recog_data.dup_num[i]];
3566 /* Update all elimination pairs to reflect the status after the current
3567 insn. The changes we make were determined by the earlier call to
3568 elimination_effects.
3570 We also detect a cases where register elimination cannot be done,
3571 namely, if a register would be both changed and referenced outside a MEM
3572 in the resulting insn since such an insn is often undefined and, even if
3573 not, we cannot know what meaning will be given to it. Note that it is
3574 valid to have a register used in an address in an insn that changes it
3575 (presumably with a pre- or post-increment or decrement).
3577 If anything changes, return nonzero. */
3579 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3581 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3582 ep->can_eliminate = 0;
3584 ep->ref_outside_mem = 0;
3586 if (ep->previous_offset != ep->offset)
3587 val = 1;
3590 done:
3591 /* If we changed something, perform elimination in REG_NOTES. This is
3592 needed even when REPLACE is zero because a REG_DEAD note might refer
3593 to a register that we eliminate and could cause a different number
3594 of spill registers to be needed in the final reload pass than in
3595 the pre-passes. */
3596 if (val && REG_NOTES (insn) != 0)
3597 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
3599 if (! replace)
3600 pop_obstacks ();
3602 return val;
3605 /* Loop through all elimination pairs.
3606 Recalculate the number not at initial offset.
3608 Compute the maximum offset (minimum offset if the stack does not
3609 grow downward) for each elimination pair. */
3611 static void
3612 update_eliminable_offsets ()
3614 struct elim_table *ep;
3616 num_not_at_initial_offset = 0;
3617 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3619 ep->previous_offset = ep->offset;
3620 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3621 num_not_at_initial_offset++;
3625 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3626 replacement we currently believe is valid, mark it as not eliminable if X
3627 modifies DEST in any way other than by adding a constant integer to it.
3629 If DEST is the frame pointer, we do nothing because we assume that
3630 all assignments to the hard frame pointer are nonlocal gotos and are being
3631 done at a time when they are valid and do not disturb anything else.
3632 Some machines want to eliminate a fake argument pointer with either the
3633 frame or stack pointer. Assignments to the hard frame pointer must not
3634 prevent this elimination.
3636 Called via note_stores from reload before starting its passes to scan
3637 the insns of the function. */
3639 static void
3640 mark_not_eliminable (dest, x, data)
3641 rtx dest;
3642 rtx x;
3643 void *data ATTRIBUTE_UNUSED;
3645 register unsigned int i;
3647 /* A SUBREG of a hard register here is just changing its mode. We should
3648 not see a SUBREG of an eliminable hard register, but check just in
3649 case. */
3650 if (GET_CODE (dest) == SUBREG)
3651 dest = SUBREG_REG (dest);
3653 if (dest == hard_frame_pointer_rtx)
3654 return;
3656 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3657 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3658 && (GET_CODE (x) != SET
3659 || GET_CODE (SET_SRC (x)) != PLUS
3660 || XEXP (SET_SRC (x), 0) != dest
3661 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3663 reg_eliminate[i].can_eliminate_previous
3664 = reg_eliminate[i].can_eliminate = 0;
3665 num_eliminable--;
3669 /* Verify that the initial elimination offsets did not change since the
3670 last call to set_initial_elim_offsets. This is used to catch cases
3671 where something illegal happened during reload_as_needed that could
3672 cause incorrect code to be generated if we did not check for it. */
3673 static void
3674 verify_initial_elim_offsets ()
3676 int t;
3678 #ifdef ELIMINABLE_REGS
3679 struct elim_table *ep;
3681 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3683 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3684 if (t != ep->initial_offset)
3685 abort ();
3687 #else
3688 INITIAL_FRAME_POINTER_OFFSET (t);
3689 if (t != reg_eliminate[0].initial_offset)
3690 abort ();
3691 #endif
3694 /* Reset all offsets on eliminable registers to their initial values. */
3695 static void
3696 set_initial_elim_offsets ()
3698 struct elim_table *ep = reg_eliminate;
3700 #ifdef ELIMINABLE_REGS
3701 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3703 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3704 ep->previous_offset = ep->offset = ep->initial_offset;
3706 #else
3707 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3708 ep->previous_offset = ep->offset = ep->initial_offset;
3709 #endif
3711 num_not_at_initial_offset = 0;
3714 /* Initialize the known label offsets.
3715 Set a known offset for each forced label to be at the initial offset
3716 of each elimination. We do this because we assume that all
3717 computed jumps occur from a location where each elimination is
3718 at its initial offset.
3719 For all other labels, show that we don't know the offsets. */
3721 static void
3722 set_initial_label_offsets ()
3724 rtx x;
3725 bzero ((char *) &offsets_known_at[get_first_label_num ()], num_labels);
3727 for (x = forced_labels; x; x = XEXP (x, 1))
3728 if (XEXP (x, 0))
3729 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3732 /* Set all elimination offsets to the known values for the code label given
3733 by INSN. */
3734 static void
3735 set_offsets_for_label (insn)
3736 rtx insn;
3738 unsigned int i;
3739 int label_nr = CODE_LABEL_NUMBER (insn);
3740 struct elim_table *ep;
3742 num_not_at_initial_offset = 0;
3743 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3745 ep->offset = ep->previous_offset = offsets_at[label_nr][i];
3746 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3747 num_not_at_initial_offset++;
3751 /* See if anything that happened changes which eliminations are valid.
3752 For example, on the Sparc, whether or not the frame pointer can
3753 be eliminated can depend on what registers have been used. We need
3754 not check some conditions again (such as flag_omit_frame_pointer)
3755 since they can't have changed. */
3757 static void
3758 update_eliminables (pset)
3759 HARD_REG_SET *pset;
3761 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3762 int previous_frame_pointer_needed = frame_pointer_needed;
3763 #endif
3764 struct elim_table *ep;
3766 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3767 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3768 #ifdef ELIMINABLE_REGS
3769 || ! CAN_ELIMINATE (ep->from, ep->to)
3770 #endif
3772 ep->can_eliminate = 0;
3774 /* Look for the case where we have discovered that we can't replace
3775 register A with register B and that means that we will now be
3776 trying to replace register A with register C. This means we can
3777 no longer replace register C with register B and we need to disable
3778 such an elimination, if it exists. This occurs often with A == ap,
3779 B == sp, and C == fp. */
3781 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3783 struct elim_table *op;
3784 register int new_to = -1;
3786 if (! ep->can_eliminate && ep->can_eliminate_previous)
3788 /* Find the current elimination for ep->from, if there is a
3789 new one. */
3790 for (op = reg_eliminate;
3791 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3792 if (op->from == ep->from && op->can_eliminate)
3794 new_to = op->to;
3795 break;
3798 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3799 disable it. */
3800 for (op = reg_eliminate;
3801 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3802 if (op->from == new_to && op->to == ep->to)
3803 op->can_eliminate = 0;
3807 /* See if any registers that we thought we could eliminate the previous
3808 time are no longer eliminable. If so, something has changed and we
3809 must spill the register. Also, recompute the number of eliminable
3810 registers and see if the frame pointer is needed; it is if there is
3811 no elimination of the frame pointer that we can perform. */
3813 frame_pointer_needed = 1;
3814 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3816 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3817 && ep->to != HARD_FRAME_POINTER_REGNUM)
3818 frame_pointer_needed = 0;
3820 if (! ep->can_eliminate && ep->can_eliminate_previous)
3822 ep->can_eliminate_previous = 0;
3823 SET_HARD_REG_BIT (*pset, ep->from);
3824 num_eliminable--;
3828 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3829 /* If we didn't need a frame pointer last time, but we do now, spill
3830 the hard frame pointer. */
3831 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3832 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3833 #endif
3836 /* Initialize the table of registers to eliminate. */
3837 static void
3838 init_elim_table ()
3840 struct elim_table *ep;
3841 #ifdef ELIMINABLE_REGS
3842 struct elim_table_1 *ep1;
3843 #endif
3845 if (!reg_eliminate)
3846 reg_eliminate = (struct elim_table *)
3847 xcalloc(sizeof(struct elim_table), NUM_ELIMINABLE_REGS);
3849 /* Does this function require a frame pointer? */
3851 frame_pointer_needed = (! flag_omit_frame_pointer
3852 #ifdef EXIT_IGNORE_STACK
3853 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3854 and restore sp for alloca. So we can't eliminate
3855 the frame pointer in that case. At some point,
3856 we should improve this by emitting the
3857 sp-adjusting insns for this case. */
3858 || (current_function_calls_alloca
3859 && EXIT_IGNORE_STACK)
3860 #endif
3861 || FRAME_POINTER_REQUIRED);
3863 num_eliminable = 0;
3865 #ifdef ELIMINABLE_REGS
3866 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3867 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3869 ep->from = ep1->from;
3870 ep->to = ep1->to;
3871 ep->can_eliminate = ep->can_eliminate_previous
3872 = (CAN_ELIMINATE (ep->from, ep->to)
3873 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3875 #else
3876 reg_eliminate[0].from = reg_eliminate_1[0].from;
3877 reg_eliminate[0].to = reg_eliminate_1[0].to;
3878 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3879 = ! frame_pointer_needed;
3880 #endif
3882 /* Count the number of eliminable registers and build the FROM and TO
3883 REG rtx's. Note that code in gen_rtx will cause, e.g.,
3884 gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3885 We depend on this. */
3886 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3888 num_eliminable += ep->can_eliminate;
3889 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3890 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3894 /* Kick all pseudos out of hard register REGNO.
3895 If DUMPFILE is nonzero, log actions taken on that file.
3897 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3898 because we found we can't eliminate some register. In the case, no pseudos
3899 are allowed to be in the register, even if they are only in a block that
3900 doesn't require spill registers, unlike the case when we are spilling this
3901 hard reg to produce another spill register.
3903 Return nonzero if any pseudos needed to be kicked out. */
3905 static void
3906 spill_hard_reg (regno, dumpfile, cant_eliminate)
3907 register int regno;
3908 FILE *dumpfile ATTRIBUTE_UNUSED;
3909 int cant_eliminate;
3911 register int i;
3913 if (cant_eliminate)
3915 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3916 regs_ever_live[regno] = 1;
3919 /* Spill every pseudo reg that was allocated to this reg
3920 or to something that overlaps this reg. */
3922 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3923 if (reg_renumber[i] >= 0
3924 && reg_renumber[i] <= regno
3925 && (reg_renumber[i]
3926 + HARD_REGNO_NREGS (reg_renumber[i],
3927 PSEUDO_REGNO_MODE (i))
3928 > regno))
3929 SET_REGNO_REG_SET (spilled_pseudos, i);
3932 /* I'm getting weird preprocessor errors if I use IOR_HARD_REG_SET
3933 from within EXECUTE_IF_SET_IN_REG_SET. Hence this awkwardness. */
3934 static void
3935 ior_hard_reg_set (set1, set2)
3936 HARD_REG_SET *set1, *set2;
3938 IOR_HARD_REG_SET (*set1, *set2);
3941 /* After find_reload_regs has been run for all insn that need reloads,
3942 and/or spill_hard_regs was called, this function is used to actually
3943 spill pseudo registers and try to reallocate them. It also sets up the
3944 spill_regs array for use by choose_reload_regs. */
3946 static int
3947 finish_spills (global, dumpfile)
3948 int global;
3949 FILE *dumpfile;
3951 struct insn_chain *chain;
3952 int something_changed = 0;
3953 int i;
3955 /* Build the spill_regs array for the function. */
3956 /* If there are some registers still to eliminate and one of the spill regs
3957 wasn't ever used before, additional stack space may have to be
3958 allocated to store this register. Thus, we may have changed the offset
3959 between the stack and frame pointers, so mark that something has changed.
3961 One might think that we need only set VAL to 1 if this is a call-used
3962 register. However, the set of registers that must be saved by the
3963 prologue is not identical to the call-used set. For example, the
3964 register used by the call insn for the return PC is a call-used register,
3965 but must be saved by the prologue. */
3967 n_spills = 0;
3968 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3969 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3971 spill_reg_order[i] = n_spills;
3972 spill_regs[n_spills++] = i;
3973 if (num_eliminable && ! regs_ever_live[i])
3974 something_changed = 1;
3975 regs_ever_live[i] = 1;
3977 else
3978 spill_reg_order[i] = -1;
3980 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3981 if (REGNO_REG_SET_P (spilled_pseudos, i))
3983 /* Record the current hard register the pseudo is allocated to in
3984 pseudo_previous_regs so we avoid reallocating it to the same
3985 hard reg in a later pass. */
3986 if (reg_renumber[i] < 0)
3987 abort ();
3988 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3989 /* Mark it as no longer having a hard register home. */
3990 reg_renumber[i] = -1;
3991 /* We will need to scan everything again. */
3992 something_changed = 1;
3995 /* Retry global register allocation if possible. */
3996 if (global)
3998 bzero ((char *) pseudo_forbidden_regs, max_regno * sizeof (HARD_REG_SET));
3999 /* For every insn that needs reloads, set the registers used as spill
4000 regs in pseudo_forbidden_regs for every pseudo live across the
4001 insn. */
4002 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
4004 EXECUTE_IF_SET_IN_REG_SET
4005 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
4007 ior_hard_reg_set (pseudo_forbidden_regs + i,
4008 &chain->used_spill_regs);
4010 EXECUTE_IF_SET_IN_REG_SET
4011 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
4013 ior_hard_reg_set (pseudo_forbidden_regs + i,
4014 &chain->used_spill_regs);
4018 /* Retry allocating the spilled pseudos. For each reg, merge the
4019 various reg sets that indicate which hard regs can't be used,
4020 and call retry_global_alloc.
4021 We change spill_pseudos here to only contain pseudos that did not
4022 get a new hard register. */
4023 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4024 if (reg_old_renumber[i] != reg_renumber[i])
4026 HARD_REG_SET forbidden;
4027 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
4028 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
4029 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
4030 retry_global_alloc (i, forbidden);
4031 if (reg_renumber[i] >= 0)
4032 CLEAR_REGNO_REG_SET (spilled_pseudos, i);
4036 /* Fix up the register information in the insn chain.
4037 This involves deleting those of the spilled pseudos which did not get
4038 a new hard register home from the live_{before,after} sets. */
4039 for (chain = reload_insn_chain; chain; chain = chain->next)
4041 HARD_REG_SET used_by_pseudos;
4042 HARD_REG_SET used_by_pseudos2;
4044 AND_COMPL_REG_SET (chain->live_before, spilled_pseudos);
4045 AND_COMPL_REG_SET (chain->live_after, spilled_pseudos);
4047 /* Mark any unallocated hard regs as available for spills. That
4048 makes inheritance work somewhat better. */
4049 if (chain->need_reload)
4051 REG_SET_TO_HARD_REG_SET (used_by_pseudos, chain->live_before);
4052 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, chain->live_after);
4053 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
4055 /* Save the old value for the sanity test below. */
4056 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
4058 compute_use_by_pseudos (&used_by_pseudos, chain->live_before);
4059 compute_use_by_pseudos (&used_by_pseudos, chain->live_after);
4060 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
4061 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
4063 /* Make sure we only enlarge the set. */
4064 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
4065 abort ();
4066 ok:;
4070 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
4071 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4073 int regno = reg_renumber[i];
4074 if (reg_old_renumber[i] == regno)
4075 continue;
4077 alter_reg (i, reg_old_renumber[i]);
4078 reg_old_renumber[i] = regno;
4079 if (dumpfile)
4081 if (regno == -1)
4082 fprintf (dumpfile, " Register %d now on stack.\n\n", i);
4083 else
4084 fprintf (dumpfile, " Register %d now in %d.\n\n",
4085 i, reg_renumber[i]);
4089 return something_changed;
4092 /* Find all paradoxical subregs within X and update reg_max_ref_width.
4093 Also mark any hard registers used to store user variables as
4094 forbidden from being used for spill registers. */
4096 static void
4097 scan_paradoxical_subregs (x)
4098 register rtx x;
4100 register int i;
4101 register const char *fmt;
4102 register enum rtx_code code = GET_CODE (x);
4104 switch (code)
4106 case REG:
4107 #if 0
4108 if (SMALL_REGISTER_CLASSES && REGNO (x) < FIRST_PSEUDO_REGISTER
4109 && REG_USERVAR_P (x))
4110 SET_HARD_REG_BIT (bad_spill_regs_global, REGNO (x));
4111 #endif
4112 return;
4114 case CONST_INT:
4115 case CONST:
4116 case SYMBOL_REF:
4117 case LABEL_REF:
4118 case CONST_DOUBLE:
4119 case CC0:
4120 case PC:
4121 case USE:
4122 case CLOBBER:
4123 return;
4125 case SUBREG:
4126 if (GET_CODE (SUBREG_REG (x)) == REG
4127 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
4128 reg_max_ref_width[REGNO (SUBREG_REG (x))]
4129 = GET_MODE_SIZE (GET_MODE (x));
4130 return;
4132 default:
4133 break;
4136 fmt = GET_RTX_FORMAT (code);
4137 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4139 if (fmt[i] == 'e')
4140 scan_paradoxical_subregs (XEXP (x, i));
4141 else if (fmt[i] == 'E')
4143 register int j;
4144 for (j = XVECLEN (x, i) - 1; j >=0; j--)
4145 scan_paradoxical_subregs (XVECEXP (x, i, j));
4150 static int
4151 hard_reg_use_compare (p1p, p2p)
4152 const PTR p1p;
4153 const PTR p2p;
4155 const struct hard_reg_n_uses *p1 = (const struct hard_reg_n_uses *)p1p;
4156 const struct hard_reg_n_uses *p2 = (const struct hard_reg_n_uses *)p2p;
4157 int bad1 = TEST_HARD_REG_BIT (bad_spill_regs, p1->regno);
4158 int bad2 = TEST_HARD_REG_BIT (bad_spill_regs, p2->regno);
4159 if (bad1 && bad2)
4160 return p1->regno - p2->regno;
4161 if (bad1)
4162 return 1;
4163 if (bad2)
4164 return -1;
4165 if (p1->uses > p2->uses)
4166 return 1;
4167 if (p1->uses < p2->uses)
4168 return -1;
4169 /* If regs are equally good, sort by regno,
4170 so that the results of qsort leave nothing to chance. */
4171 return p1->regno - p2->regno;
4174 /* Used for communication between order_regs_for_reload and count_pseudo.
4175 Used to avoid counting one pseudo twice. */
4176 static regset pseudos_counted;
4178 /* Update the costs in N_USES, considering that pseudo REG is live. */
4179 static void
4180 count_pseudo (n_uses, reg)
4181 struct hard_reg_n_uses *n_uses;
4182 int reg;
4184 int r = reg_renumber[reg];
4185 int nregs;
4187 if (REGNO_REG_SET_P (pseudos_counted, reg))
4188 return;
4189 SET_REGNO_REG_SET (pseudos_counted, reg);
4191 if (r < 0)
4192 abort ();
4194 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
4195 while (nregs-- > 0)
4196 n_uses[r++].uses += REG_N_REFS (reg);
4198 /* Choose the order to consider regs for use as reload registers
4199 based on how much trouble would be caused by spilling one.
4200 Store them in order of decreasing preference in potential_reload_regs. */
4202 static void
4203 order_regs_for_reload (chain)
4204 struct insn_chain *chain;
4206 register int i;
4207 register int o = 0;
4208 struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
4210 pseudos_counted = ALLOCA_REG_SET ();
4212 COPY_HARD_REG_SET (bad_spill_regs, bad_spill_regs_global);
4214 /* Count number of uses of each hard reg by pseudo regs allocated to it
4215 and then order them by decreasing use. */
4217 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4219 hard_reg_n_uses[i].regno = i;
4220 hard_reg_n_uses[i].uses = 0;
4222 /* Test the various reasons why we can't use a register for
4223 spilling in this insn. */
4224 if (fixed_regs[i]
4225 || REGNO_REG_SET_P (chain->live_before, i)
4226 || REGNO_REG_SET_P (chain->live_after, i))
4227 SET_HARD_REG_BIT (bad_spill_regs, i);
4230 /* Now compute hard_reg_n_uses. */
4231 CLEAR_REG_SET (pseudos_counted);
4233 EXECUTE_IF_SET_IN_REG_SET
4234 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
4236 count_pseudo (hard_reg_n_uses, i);
4238 EXECUTE_IF_SET_IN_REG_SET
4239 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
4241 count_pseudo (hard_reg_n_uses, i);
4244 FREE_REG_SET (pseudos_counted);
4246 /* Prefer registers not so far used, for use in temporary loading.
4247 Among them, if REG_ALLOC_ORDER is defined, use that order.
4248 Otherwise, prefer registers not preserved by calls. */
4250 #ifdef REG_ALLOC_ORDER
4251 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4253 int regno = reg_alloc_order[i];
4255 if (hard_reg_n_uses[regno].uses == 0
4256 && ! TEST_HARD_REG_BIT (bad_spill_regs, regno))
4257 potential_reload_regs[o++] = regno;
4259 #else
4260 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4262 if (hard_reg_n_uses[i].uses == 0 && call_used_regs[i]
4263 && ! TEST_HARD_REG_BIT (bad_spill_regs, i))
4264 potential_reload_regs[o++] = i;
4266 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4268 if (hard_reg_n_uses[i].uses == 0 && ! call_used_regs[i]
4269 && ! TEST_HARD_REG_BIT (bad_spill_regs, i))
4270 potential_reload_regs[o++] = i;
4272 #endif
4274 qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
4275 sizeof hard_reg_n_uses[0], hard_reg_use_compare);
4277 /* Now add the regs that are already used,
4278 preferring those used less often. The fixed and otherwise forbidden
4279 registers will be at the end of this list. */
4281 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4282 if (hard_reg_n_uses[i].uses != 0
4283 && ! TEST_HARD_REG_BIT (bad_spill_regs, hard_reg_n_uses[i].regno))
4284 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
4285 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4286 if (TEST_HARD_REG_BIT (bad_spill_regs, hard_reg_n_uses[i].regno))
4287 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
4290 /* Reload pseudo-registers into hard regs around each insn as needed.
4291 Additional register load insns are output before the insn that needs it
4292 and perhaps store insns after insns that modify the reloaded pseudo reg.
4294 reg_last_reload_reg and reg_reloaded_contents keep track of
4295 which registers are already available in reload registers.
4296 We update these for the reloads that we perform,
4297 as the insns are scanned. */
4299 static void
4300 reload_as_needed (live_known)
4301 int live_known;
4303 struct insn_chain *chain;
4304 #if defined (AUTO_INC_DEC) || defined (INSN_CLOBBERS_REGNO_P)
4305 register int i;
4306 #endif
4307 rtx x;
4309 bzero ((char *) spill_reg_rtx, sizeof spill_reg_rtx);
4310 bzero ((char *) spill_reg_store, sizeof spill_reg_store);
4311 reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
4312 bzero ((char *) reg_last_reload_reg, max_regno * sizeof (rtx));
4313 reg_has_output_reload = (char *) alloca (max_regno);
4314 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4316 set_initial_elim_offsets ();
4318 for (chain = reload_insn_chain; chain; chain = chain->next)
4320 rtx prev;
4321 rtx insn = chain->insn;
4322 rtx old_next = NEXT_INSN (insn);
4324 /* If we pass a label, copy the offsets from the label information
4325 into the current offsets of each elimination. */
4326 if (GET_CODE (insn) == CODE_LABEL)
4327 set_offsets_for_label (insn);
4329 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4331 rtx oldpat = PATTERN (insn);
4333 /* If this is a USE and CLOBBER of a MEM, ensure that any
4334 references to eliminable registers have been removed. */
4336 if ((GET_CODE (PATTERN (insn)) == USE
4337 || GET_CODE (PATTERN (insn)) == CLOBBER)
4338 && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
4339 XEXP (XEXP (PATTERN (insn), 0), 0)
4340 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
4341 GET_MODE (XEXP (PATTERN (insn), 0)),
4342 NULL_RTX);
4344 /* If we need to do register elimination processing, do so.
4345 This might delete the insn, in which case we are done. */
4346 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
4348 eliminate_regs_in_insn (insn, 1);
4349 if (GET_CODE (insn) == NOTE)
4351 update_eliminable_offsets ();
4352 continue;
4356 /* If need_elim is nonzero but need_reload is zero, one might think
4357 that we could simply set n_reloads to 0. However, find_reloads
4358 could have done some manipulation of the insn (such as swapping
4359 commutative operands), and these manipulations are lost during
4360 the first pass for every insn that needs register elimination.
4361 So the actions of find_reloads must be redone here. */
4363 if (! chain->need_elim && ! chain->need_reload
4364 && ! chain->need_operand_change)
4365 n_reloads = 0;
4366 /* First find the pseudo regs that must be reloaded for this insn.
4367 This info is returned in the tables reload_... (see reload.h).
4368 Also modify the body of INSN by substituting RELOAD
4369 rtx's for those pseudo regs. */
4370 else
4372 bzero (reg_has_output_reload, max_regno);
4373 CLEAR_HARD_REG_SET (reg_is_output_reload);
4375 find_reloads (insn, 1, spill_indirect_levels, live_known,
4376 spill_reg_order);
4379 if (num_eliminable && chain->need_elim)
4380 update_eliminable_offsets ();
4382 if (n_reloads > 0)
4384 rtx next = NEXT_INSN (insn);
4385 rtx p;
4387 prev = PREV_INSN (insn);
4389 /* Now compute which reload regs to reload them into. Perhaps
4390 reusing reload regs from previous insns, or else output
4391 load insns to reload them. Maybe output store insns too.
4392 Record the choices of reload reg in reload_reg_rtx. */
4393 choose_reload_regs (chain);
4395 /* Merge any reloads that we didn't combine for fear of
4396 increasing the number of spill registers needed but now
4397 discover can be safely merged. */
4398 if (SMALL_REGISTER_CLASSES)
4399 merge_assigned_reloads (insn);
4401 /* Generate the insns to reload operands into or out of
4402 their reload regs. */
4403 emit_reload_insns (chain);
4405 /* Substitute the chosen reload regs from reload_reg_rtx
4406 into the insn's body (or perhaps into the bodies of other
4407 load and store insn that we just made for reloading
4408 and that we moved the structure into). */
4409 subst_reloads ();
4411 /* If this was an ASM, make sure that all the reload insns
4412 we have generated are valid. If not, give an error
4413 and delete them. */
4415 if (asm_noperands (PATTERN (insn)) >= 0)
4416 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4417 if (p != insn && GET_RTX_CLASS (GET_CODE (p)) == 'i'
4418 && (recog_memoized (p) < 0
4419 || (extract_insn (p), ! constrain_operands (1))))
4421 error_for_asm (insn,
4422 "`asm' operand requires impossible reload");
4423 PUT_CODE (p, NOTE);
4424 NOTE_SOURCE_FILE (p) = 0;
4425 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
4428 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4429 is no longer validly lying around to save a future reload.
4430 Note that this does not detect pseudos that were reloaded
4431 for this insn in order to be stored in
4432 (obeying register constraints). That is correct; such reload
4433 registers ARE still valid. */
4434 note_stores (oldpat, forget_old_reloads_1, NULL);
4436 /* There may have been CLOBBER insns placed after INSN. So scan
4437 between INSN and NEXT and use them to forget old reloads. */
4438 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
4439 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
4440 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
4442 #ifdef AUTO_INC_DEC
4443 /* Likewise for regs altered by auto-increment in this insn.
4444 REG_INC notes have been changed by reloading:
4445 find_reloads_address_1 records substitutions for them,
4446 which have been performed by subst_reloads above. */
4447 for (i = n_reloads - 1; i >= 0; i--)
4449 rtx in_reg = rld[i].in_reg;
4450 if (in_reg)
4452 enum rtx_code code = GET_CODE (in_reg);
4453 /* PRE_INC / PRE_DEC will have the reload register ending up
4454 with the same value as the stack slot, but that doesn't
4455 hold true for POST_INC / POST_DEC. Either we have to
4456 convert the memory access to a true POST_INC / POST_DEC,
4457 or we can't use the reload register for inheritance. */
4458 if ((code == POST_INC || code == POST_DEC)
4459 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4460 REGNO (rld[i].reg_rtx))
4461 /* Make sure it is the inc/dec pseudo, and not
4462 some other (e.g. output operand) pseudo. */
4463 && (reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4464 == REGNO (XEXP (in_reg, 0))))
4467 rtx reload_reg = rld[i].reg_rtx;
4468 enum machine_mode mode = GET_MODE (reload_reg);
4469 int n = 0;
4470 rtx p;
4472 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4474 /* We really want to ignore REG_INC notes here, so
4475 use PATTERN (p) as argument to reg_set_p . */
4476 if (reg_set_p (reload_reg, PATTERN (p)))
4477 break;
4478 n = count_occurrences (PATTERN (p), reload_reg);
4479 if (! n)
4480 continue;
4481 if (n == 1)
4483 n = validate_replace_rtx (reload_reg,
4484 gen_rtx (code, mode,
4485 reload_reg),
4488 /* We must also verify that the constraints
4489 are met after the replacement. */
4490 extract_insn (p);
4491 if (n)
4492 n = constrain_operands (1);
4493 else
4494 break;
4496 /* If the constraints were not met, then
4497 undo the replacement. */
4498 if (!n)
4500 validate_replace_rtx (gen_rtx (code, mode,
4501 reload_reg),
4502 reload_reg, p);
4503 break;
4507 break;
4509 if (n == 1)
4511 REG_NOTES (p)
4512 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
4513 REG_NOTES (p));
4514 /* Mark this as having an output reload so that the
4515 REG_INC processing code below won't invalidate
4516 the reload for inheritance. */
4517 SET_HARD_REG_BIT (reg_is_output_reload,
4518 REGNO (reload_reg));
4519 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4521 else
4522 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4523 NULL);
4525 else if ((code == PRE_INC || code == PRE_DEC)
4526 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4527 REGNO (rld[i].reg_rtx))
4528 /* Make sure it is the inc/dec pseudo, and not
4529 some other (e.g. output operand) pseudo. */
4530 && (reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4531 == REGNO (XEXP (in_reg, 0))))
4533 SET_HARD_REG_BIT (reg_is_output_reload,
4534 REGNO (rld[i].reg_rtx));
4535 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4539 /* If a pseudo that got a hard register is auto-incremented,
4540 we must purge records of copying it into pseudos without
4541 hard registers. */
4542 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4543 if (REG_NOTE_KIND (x) == REG_INC)
4545 /* See if this pseudo reg was reloaded in this insn.
4546 If so, its last-reload info is still valid
4547 because it is based on this insn's reload. */
4548 for (i = 0; i < n_reloads; i++)
4549 if (rld[i].out == XEXP (x, 0))
4550 break;
4552 if (i == n_reloads)
4553 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4555 #endif
4557 /* A reload reg's contents are unknown after a label. */
4558 if (GET_CODE (insn) == CODE_LABEL)
4559 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4561 /* Don't assume a reload reg is still good after a call insn
4562 if it is a call-used reg. */
4563 else if (GET_CODE (insn) == CALL_INSN)
4564 AND_COMPL_HARD_REG_SET(reg_reloaded_valid, call_used_reg_set);
4566 /* In case registers overlap, allow certain insns to invalidate
4567 particular hard registers. */
4569 #ifdef INSN_CLOBBERS_REGNO_P
4570 for (i = 0 ; i < FIRST_PSEUDO_REGISTER; i++)
4571 if (TEST_HARD_REG_BIT (reg_reloaded_valid, i)
4572 && INSN_CLOBBERS_REGNO_P (insn, i))
4573 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i);
4574 #endif
4576 #ifdef USE_C_ALLOCA
4577 alloca (0);
4578 #endif
4582 /* Discard all record of any value reloaded from X,
4583 or reloaded in X from someplace else;
4584 unless X is an output reload reg of the current insn.
4586 X may be a hard reg (the reload reg)
4587 or it may be a pseudo reg that was reloaded from. */
4589 static void
4590 forget_old_reloads_1 (x, ignored, data)
4591 rtx x;
4592 rtx ignored ATTRIBUTE_UNUSED;
4593 void *data ATTRIBUTE_UNUSED;
4595 register int regno;
4596 int nr;
4597 int offset = 0;
4599 /* note_stores does give us subregs of hard regs. */
4600 while (GET_CODE (x) == SUBREG)
4602 offset += SUBREG_WORD (x);
4603 x = SUBREG_REG (x);
4606 if (GET_CODE (x) != REG)
4607 return;
4609 regno = REGNO (x) + offset;
4611 if (regno >= FIRST_PSEUDO_REGISTER)
4612 nr = 1;
4613 else
4615 int i;
4616 nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
4617 /* Storing into a spilled-reg invalidates its contents.
4618 This can happen if a block-local pseudo is allocated to that reg
4619 and it wasn't spilled because this block's total need is 0.
4620 Then some insn might have an optional reload and use this reg. */
4621 for (i = 0; i < nr; i++)
4622 /* But don't do this if the reg actually serves as an output
4623 reload reg in the current instruction. */
4624 if (n_reloads == 0
4625 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4626 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4629 /* Since value of X has changed,
4630 forget any value previously copied from it. */
4632 while (nr-- > 0)
4633 /* But don't forget a copy if this is the output reload
4634 that establishes the copy's validity. */
4635 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4636 reg_last_reload_reg[regno + nr] = 0;
4639 /* Comparison function for qsort to decide which of two reloads
4640 should be handled first. *P1 and *P2 are the reload numbers. */
4642 static int
4643 reload_reg_class_lower (r1p, r2p)
4644 const PTR r1p;
4645 const PTR r2p;
4647 register int r1 = *(const short *)r1p, r2 = *(const short *)r2p;
4648 register int t;
4650 /* Consider required reloads before optional ones. */
4651 t = rld[r1].optional - rld[r2].optional;
4652 if (t != 0)
4653 return t;
4655 /* Count all solitary classes before non-solitary ones. */
4656 t = ((reg_class_size[(int) rld[r2].class] == 1)
4657 - (reg_class_size[(int) rld[r1].class] == 1));
4658 if (t != 0)
4659 return t;
4661 /* Aside from solitaires, consider all multi-reg groups first. */
4662 t = rld[r2].nregs - rld[r1].nregs;
4663 if (t != 0)
4664 return t;
4666 /* Consider reloads in order of increasing reg-class number. */
4667 t = (int) rld[r1].class - (int) rld[r2].class;
4668 if (t != 0)
4669 return t;
4671 /* If reloads are equally urgent, sort by reload number,
4672 so that the results of qsort leave nothing to chance. */
4673 return r1 - r2;
4676 /* The following HARD_REG_SETs indicate when each hard register is
4677 used for a reload of various parts of the current insn. */
4679 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4680 static HARD_REG_SET reload_reg_used;
4681 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4682 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4683 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4684 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4685 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4686 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4687 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4688 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4689 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4690 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4691 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4692 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4693 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4694 static HARD_REG_SET reload_reg_used_in_op_addr;
4695 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4696 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4697 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4698 static HARD_REG_SET reload_reg_used_in_insn;
4699 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4700 static HARD_REG_SET reload_reg_used_in_other_addr;
4702 /* If reg is in use as a reload reg for any sort of reload. */
4703 static HARD_REG_SET reload_reg_used_at_all;
4705 /* If reg is use as an inherited reload. We just mark the first register
4706 in the group. */
4707 static HARD_REG_SET reload_reg_used_for_inherit;
4709 /* Records which hard regs are used in any way, either as explicit use or
4710 by being allocated to a pseudo during any point of the current insn. */
4711 static HARD_REG_SET reg_used_in_insn;
4713 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4714 TYPE. MODE is used to indicate how many consecutive regs are
4715 actually used. */
4717 static void
4718 mark_reload_reg_in_use (regno, opnum, type, mode)
4719 int regno;
4720 int opnum;
4721 enum reload_type type;
4722 enum machine_mode mode;
4724 int nregs = HARD_REGNO_NREGS (regno, mode);
4725 int i;
4727 for (i = regno; i < nregs + regno; i++)
4729 switch (type)
4731 case RELOAD_OTHER:
4732 SET_HARD_REG_BIT (reload_reg_used, i);
4733 break;
4735 case RELOAD_FOR_INPUT_ADDRESS:
4736 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4737 break;
4739 case RELOAD_FOR_INPADDR_ADDRESS:
4740 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4741 break;
4743 case RELOAD_FOR_OUTPUT_ADDRESS:
4744 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4745 break;
4747 case RELOAD_FOR_OUTADDR_ADDRESS:
4748 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4749 break;
4751 case RELOAD_FOR_OPERAND_ADDRESS:
4752 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4753 break;
4755 case RELOAD_FOR_OPADDR_ADDR:
4756 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4757 break;
4759 case RELOAD_FOR_OTHER_ADDRESS:
4760 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4761 break;
4763 case RELOAD_FOR_INPUT:
4764 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4765 break;
4767 case RELOAD_FOR_OUTPUT:
4768 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4769 break;
4771 case RELOAD_FOR_INSN:
4772 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4773 break;
4776 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4780 /* Similarly, but show REGNO is no longer in use for a reload. */
4782 static void
4783 clear_reload_reg_in_use (regno, opnum, type, mode)
4784 int regno;
4785 int opnum;
4786 enum reload_type type;
4787 enum machine_mode mode;
4789 int nregs = HARD_REGNO_NREGS (regno, mode);
4790 int start_regno, end_regno;
4791 int i;
4792 /* A complication is that for some reload types, inheritance might
4793 allow multiple reloads of the same types to share a reload register.
4794 We set check_opnum if we have to check only reloads with the same
4795 operand number, and check_any if we have to check all reloads. */
4796 int check_opnum = 0;
4797 int check_any = 0;
4798 HARD_REG_SET *used_in_set;
4800 switch (type)
4802 case RELOAD_OTHER:
4803 used_in_set = &reload_reg_used;
4804 break;
4806 case RELOAD_FOR_INPUT_ADDRESS:
4807 used_in_set = &reload_reg_used_in_input_addr[opnum];
4808 break;
4810 case RELOAD_FOR_INPADDR_ADDRESS:
4811 check_opnum = 1;
4812 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4813 break;
4815 case RELOAD_FOR_OUTPUT_ADDRESS:
4816 used_in_set = &reload_reg_used_in_output_addr[opnum];
4817 break;
4819 case RELOAD_FOR_OUTADDR_ADDRESS:
4820 check_opnum = 1;
4821 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4822 break;
4824 case RELOAD_FOR_OPERAND_ADDRESS:
4825 used_in_set = &reload_reg_used_in_op_addr;
4826 break;
4828 case RELOAD_FOR_OPADDR_ADDR:
4829 check_any = 1;
4830 used_in_set = &reload_reg_used_in_op_addr_reload;
4831 break;
4833 case RELOAD_FOR_OTHER_ADDRESS:
4834 used_in_set = &reload_reg_used_in_other_addr;
4835 check_any = 1;
4836 break;
4838 case RELOAD_FOR_INPUT:
4839 used_in_set = &reload_reg_used_in_input[opnum];
4840 break;
4842 case RELOAD_FOR_OUTPUT:
4843 used_in_set = &reload_reg_used_in_output[opnum];
4844 break;
4846 case RELOAD_FOR_INSN:
4847 used_in_set = &reload_reg_used_in_insn;
4848 break;
4849 default:
4850 abort ();
4852 /* We resolve conflicts with remaining reloads of the same type by
4853 excluding the intervals of of reload registers by them from the
4854 interval of freed reload registers. Since we only keep track of
4855 one set of interval bounds, we might have to exclude somewhat
4856 more then what would be necessary if we used a HARD_REG_SET here.
4857 But this should only happen very infrequently, so there should
4858 be no reason to worry about it. */
4860 start_regno = regno;
4861 end_regno = regno + nregs;
4862 if (check_opnum || check_any)
4864 for (i = n_reloads - 1; i >= 0; i--)
4866 if (rld[i].when_needed == type
4867 && (check_any || rld[i].opnum == opnum)
4868 && rld[i].reg_rtx)
4870 int conflict_start = true_regnum (rld[i].reg_rtx);
4871 int conflict_end
4872 = (conflict_start
4873 + HARD_REGNO_NREGS (conflict_start, rld[i].mode));
4875 /* If there is an overlap with the first to-be-freed register,
4876 adjust the interval start. */
4877 if (conflict_start <= start_regno && conflict_end > start_regno)
4878 start_regno = conflict_end;
4879 /* Otherwise, if there is a conflict with one of the other
4880 to-be-freed registers, adjust the interval end. */
4881 if (conflict_start > start_regno && conflict_start < end_regno)
4882 end_regno = conflict_start;
4886 for (i = start_regno; i < end_regno; i++)
4887 CLEAR_HARD_REG_BIT (*used_in_set, i);
4890 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4891 specified by OPNUM and TYPE. */
4893 static int
4894 reload_reg_free_p (regno, opnum, type)
4895 int regno;
4896 int opnum;
4897 enum reload_type type;
4899 int i;
4901 /* In use for a RELOAD_OTHER means it's not available for anything. */
4902 if (TEST_HARD_REG_BIT (reload_reg_used, regno))
4903 return 0;
4905 switch (type)
4907 case RELOAD_OTHER:
4908 /* In use for anything means we can't use it for RELOAD_OTHER. */
4909 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4910 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4911 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4912 return 0;
4914 for (i = 0; i < reload_n_operands; i++)
4915 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4916 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4917 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4918 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4919 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4920 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4921 return 0;
4923 return 1;
4925 case RELOAD_FOR_INPUT:
4926 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4927 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4928 return 0;
4930 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4931 return 0;
4933 /* If it is used for some other input, can't use it. */
4934 for (i = 0; i < reload_n_operands; i++)
4935 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4936 return 0;
4938 /* If it is used in a later operand's address, can't use it. */
4939 for (i = opnum + 1; i < reload_n_operands; i++)
4940 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4941 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4942 return 0;
4944 return 1;
4946 case RELOAD_FOR_INPUT_ADDRESS:
4947 /* Can't use a register if it is used for an input address for this
4948 operand or used as an input in an earlier one. */
4949 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4950 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4951 return 0;
4953 for (i = 0; i < opnum; i++)
4954 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4955 return 0;
4957 return 1;
4959 case RELOAD_FOR_INPADDR_ADDRESS:
4960 /* Can't use a register if it is used for an input address
4961 for this operand or used as an input in an earlier
4962 one. */
4963 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4964 return 0;
4966 for (i = 0; i < opnum; i++)
4967 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4968 return 0;
4970 return 1;
4972 case RELOAD_FOR_OUTPUT_ADDRESS:
4973 /* Can't use a register if it is used for an output address for this
4974 operand or used as an output in this or a later operand. */
4975 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4976 return 0;
4978 for (i = opnum; i < reload_n_operands; i++)
4979 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4980 return 0;
4982 return 1;
4984 case RELOAD_FOR_OUTADDR_ADDRESS:
4985 /* Can't use a register if it is used for an output address
4986 for this operand or used as an output in this or a
4987 later operand. */
4988 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4989 return 0;
4991 for (i = opnum; i < reload_n_operands; i++)
4992 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4993 return 0;
4995 return 1;
4997 case RELOAD_FOR_OPERAND_ADDRESS:
4998 for (i = 0; i < reload_n_operands; i++)
4999 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5000 return 0;
5002 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5003 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
5005 case RELOAD_FOR_OPADDR_ADDR:
5006 for (i = 0; i < reload_n_operands; i++)
5007 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5008 return 0;
5010 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
5012 case RELOAD_FOR_OUTPUT:
5013 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
5014 outputs, or an operand address for this or an earlier output. */
5015 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
5016 return 0;
5018 for (i = 0; i < reload_n_operands; i++)
5019 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5020 return 0;
5022 for (i = 0; i <= opnum; i++)
5023 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5024 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5025 return 0;
5027 return 1;
5029 case RELOAD_FOR_INSN:
5030 for (i = 0; i < reload_n_operands; i++)
5031 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
5032 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5033 return 0;
5035 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5036 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
5038 case RELOAD_FOR_OTHER_ADDRESS:
5039 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
5041 abort ();
5044 /* Return 1 if the value in reload reg REGNO, as used by a reload
5045 needed for the part of the insn specified by OPNUM and TYPE,
5046 is still available in REGNO at the end of the insn.
5048 We can assume that the reload reg was already tested for availability
5049 at the time it is needed, and we should not check this again,
5050 in case the reg has already been marked in use. */
5052 static int
5053 reload_reg_reaches_end_p (regno, opnum, type)
5054 int regno;
5055 int opnum;
5056 enum reload_type type;
5058 int i;
5060 switch (type)
5062 case RELOAD_OTHER:
5063 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
5064 its value must reach the end. */
5065 return 1;
5067 /* If this use is for part of the insn,
5068 its value reaches if no subsequent part uses the same register.
5069 Just like the above function, don't try to do this with lots
5070 of fallthroughs. */
5072 case RELOAD_FOR_OTHER_ADDRESS:
5073 /* Here we check for everything else, since these don't conflict
5074 with anything else and everything comes later. */
5076 for (i = 0; i < reload_n_operands; i++)
5077 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5078 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5079 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
5080 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5081 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5082 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5083 return 0;
5085 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5086 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5087 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
5089 case RELOAD_FOR_INPUT_ADDRESS:
5090 case RELOAD_FOR_INPADDR_ADDRESS:
5091 /* Similar, except that we check only for this and subsequent inputs
5092 and the address of only subsequent inputs and we do not need
5093 to check for RELOAD_OTHER objects since they are known not to
5094 conflict. */
5096 for (i = opnum; i < reload_n_operands; i++)
5097 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5098 return 0;
5100 for (i = opnum + 1; i < reload_n_operands; i++)
5101 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5102 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
5103 return 0;
5105 for (i = 0; i < reload_n_operands; i++)
5106 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5107 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5108 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5109 return 0;
5111 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
5112 return 0;
5114 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5115 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
5117 case RELOAD_FOR_INPUT:
5118 /* Similar to input address, except we start at the next operand for
5119 both input and input address and we do not check for
5120 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
5121 would conflict. */
5123 for (i = opnum + 1; i < reload_n_operands; i++)
5124 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5125 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5126 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5127 return 0;
5129 /* ... fall through ... */
5131 case RELOAD_FOR_OPERAND_ADDRESS:
5132 /* Check outputs and their addresses. */
5134 for (i = 0; i < reload_n_operands; i++)
5135 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5136 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5137 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5138 return 0;
5140 return 1;
5142 case RELOAD_FOR_OPADDR_ADDR:
5143 for (i = 0; i < reload_n_operands; i++)
5144 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5145 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5146 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5147 return 0;
5149 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5150 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
5152 case RELOAD_FOR_INSN:
5153 /* These conflict with other outputs with RELOAD_OTHER. So
5154 we need only check for output addresses. */
5156 opnum = -1;
5158 /* ... fall through ... */
5160 case RELOAD_FOR_OUTPUT:
5161 case RELOAD_FOR_OUTPUT_ADDRESS:
5162 case RELOAD_FOR_OUTADDR_ADDRESS:
5163 /* We already know these can't conflict with a later output. So the
5164 only thing to check are later output addresses. */
5165 for (i = opnum + 1; i < reload_n_operands; i++)
5166 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5167 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5168 return 0;
5170 return 1;
5173 abort ();
5176 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
5177 Return 0 otherwise.
5179 This function uses the same algorithm as reload_reg_free_p above. */
5182 reloads_conflict (r1, r2)
5183 int r1, r2;
5185 enum reload_type r1_type = rld[r1].when_needed;
5186 enum reload_type r2_type = rld[r2].when_needed;
5187 int r1_opnum = rld[r1].opnum;
5188 int r2_opnum = rld[r2].opnum;
5190 /* RELOAD_OTHER conflicts with everything. */
5191 if (r2_type == RELOAD_OTHER)
5192 return 1;
5194 /* Otherwise, check conflicts differently for each type. */
5196 switch (r1_type)
5198 case RELOAD_FOR_INPUT:
5199 return (r2_type == RELOAD_FOR_INSN
5200 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
5201 || r2_type == RELOAD_FOR_OPADDR_ADDR
5202 || r2_type == RELOAD_FOR_INPUT
5203 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
5204 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
5205 && r2_opnum > r1_opnum));
5207 case RELOAD_FOR_INPUT_ADDRESS:
5208 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
5209 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5211 case RELOAD_FOR_INPADDR_ADDRESS:
5212 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
5213 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5215 case RELOAD_FOR_OUTPUT_ADDRESS:
5216 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
5217 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
5219 case RELOAD_FOR_OUTADDR_ADDRESS:
5220 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
5221 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
5223 case RELOAD_FOR_OPERAND_ADDRESS:
5224 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
5225 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5227 case RELOAD_FOR_OPADDR_ADDR:
5228 return (r2_type == RELOAD_FOR_INPUT
5229 || r2_type == RELOAD_FOR_OPADDR_ADDR);
5231 case RELOAD_FOR_OUTPUT:
5232 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
5233 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
5234 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
5235 && r2_opnum >= r1_opnum));
5237 case RELOAD_FOR_INSN:
5238 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
5239 || r2_type == RELOAD_FOR_INSN
5240 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5242 case RELOAD_FOR_OTHER_ADDRESS:
5243 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
5245 case RELOAD_OTHER:
5246 return 1;
5248 default:
5249 abort ();
5253 /* Vector of reload-numbers showing the order in which the reloads should
5254 be processed. */
5255 short reload_order[MAX_RELOADS];
5257 /* Indexed by reload number, 1 if incoming value
5258 inherited from previous insns. */
5259 char reload_inherited[MAX_RELOADS];
5261 /* For an inherited reload, this is the insn the reload was inherited from,
5262 if we know it. Otherwise, this is 0. */
5263 rtx reload_inheritance_insn[MAX_RELOADS];
5265 /* If non-zero, this is a place to get the value of the reload,
5266 rather than using reload_in. */
5267 rtx reload_override_in[MAX_RELOADS];
5269 /* For each reload, the hard register number of the register used,
5270 or -1 if we did not need a register for this reload. */
5271 int reload_spill_index[MAX_RELOADS];
5273 /* Return 1 if the value in reload reg REGNO, as used by a reload
5274 needed for the part of the insn specified by OPNUM and TYPE,
5275 may be used to load VALUE into it.
5277 Other read-only reloads with the same value do not conflict
5278 unless OUT is non-zero and these other reloads have to live while
5279 output reloads live.
5280 If OUT is CONST0_RTX, this is a special case: it means that the
5281 test should not be for using register REGNO as reload register, but
5282 for copying from register REGNO into the reload register.
5284 RELOADNUM is the number of the reload we want to load this value for;
5285 a reload does not conflict with itself.
5287 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
5288 reloads that load an address for the very reload we are considering.
5290 The caller has to make sure that there is no conflict with the return
5291 register. */
5292 static int
5293 reload_reg_free_for_value_p (regno, opnum, type, value, out, reloadnum,
5294 ignore_address_reloads)
5295 int regno;
5296 int opnum;
5297 enum reload_type type;
5298 rtx value, out;
5299 int reloadnum;
5300 int ignore_address_reloads;
5302 int time1;
5303 /* Set if we see an input reload that must not share its reload register
5304 with any new earlyclobber, but might otherwise share the reload
5305 register with an output or input-output reload. */
5306 int check_earlyclobber = 0;
5307 int i;
5308 int copy = 0;
5310 if (out == const0_rtx)
5312 copy = 1;
5313 out = NULL_RTX;
5316 /* We use some pseudo 'time' value to check if the lifetimes of the
5317 new register use would overlap with the one of a previous reload
5318 that is not read-only or uses a different value.
5319 The 'time' used doesn't have to be linear in any shape or form, just
5320 monotonic.
5321 Some reload types use different 'buckets' for each operand.
5322 So there are MAX_RECOG_OPERANDS different time values for each
5323 such reload type.
5324 We compute TIME1 as the time when the register for the prospective
5325 new reload ceases to be live, and TIME2 for each existing
5326 reload as the time when that the reload register of that reload
5327 becomes live.
5328 Where there is little to be gained by exact lifetime calculations,
5329 we just make conservative assumptions, i.e. a longer lifetime;
5330 this is done in the 'default:' cases. */
5331 switch (type)
5333 case RELOAD_FOR_OTHER_ADDRESS:
5334 /* RELOAD_FOR_OTHER_ADDRESS conflits with RELOAD_OTHER reloads. */
5335 time1 = copy ? 0 : 1;
5336 break;
5337 case RELOAD_OTHER:
5338 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
5339 break;
5340 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
5341 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
5342 respectively, to the time values for these, we get distinct time
5343 values. To get distinct time values for each operand, we have to
5344 multiply opnum by at least three. We round that up to four because
5345 multiply by four is often cheaper. */
5346 case RELOAD_FOR_INPADDR_ADDRESS:
5347 time1 = opnum * 4 + 2;
5348 break;
5349 case RELOAD_FOR_INPUT_ADDRESS:
5350 time1 = opnum * 4 + 3;
5351 break;
5352 case RELOAD_FOR_INPUT:
5353 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
5354 executes (inclusive). */
5355 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
5356 break;
5357 case RELOAD_FOR_OPADDR_ADDR:
5358 /* opnum * 4 + 4
5359 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
5360 time1 = MAX_RECOG_OPERANDS * 4 + 1;
5361 break;
5362 case RELOAD_FOR_OPERAND_ADDRESS:
5363 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
5364 is executed. */
5365 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
5366 break;
5367 case RELOAD_FOR_OUTADDR_ADDRESS:
5368 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
5369 break;
5370 case RELOAD_FOR_OUTPUT_ADDRESS:
5371 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
5372 break;
5373 default:
5374 time1 = MAX_RECOG_OPERANDS * 5 + 5;
5377 for (i = 0; i < n_reloads; i++)
5379 rtx reg = rld[i].reg_rtx;
5380 if (reg && GET_CODE (reg) == REG
5381 && ((unsigned) regno - true_regnum (reg)
5382 <= HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)) - (unsigned)1)
5383 && i != reloadnum)
5385 if (! rld[i].in || ! rtx_equal_p (rld[i].in, value)
5386 || rld[i].out || out)
5388 int time2;
5389 switch (rld[i].when_needed)
5391 case RELOAD_FOR_OTHER_ADDRESS:
5392 time2 = 0;
5393 break;
5394 case RELOAD_FOR_INPADDR_ADDRESS:
5395 /* find_reloads makes sure that a
5396 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
5397 by at most one - the first -
5398 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
5399 address reload is inherited, the address address reload
5400 goes away, so we can ignore this conflict. */
5401 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
5402 && ignore_address_reloads
5403 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
5404 Then the address address is still needed to store
5405 back the new address. */
5406 && ! rld[reloadnum].out)
5407 continue;
5408 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
5409 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
5410 reloads go away. */
5411 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5412 && ignore_address_reloads
5413 /* Unless we are reloading an auto_inc expression. */
5414 && ! rld[reloadnum].out)
5415 continue;
5416 time2 = rld[i].opnum * 4 + 2;
5417 break;
5418 case RELOAD_FOR_INPUT_ADDRESS:
5419 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5420 && ignore_address_reloads
5421 && ! rld[reloadnum].out)
5422 continue;
5423 time2 = rld[i].opnum * 4 + 3;
5424 break;
5425 case RELOAD_FOR_INPUT:
5426 time2 = rld[i].opnum * 4 + 4;
5427 check_earlyclobber = 1;
5428 break;
5429 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
5430 == MAX_RECOG_OPERAND * 4 */
5431 case RELOAD_FOR_OPADDR_ADDR:
5432 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
5433 && ignore_address_reloads
5434 && ! rld[reloadnum].out)
5435 continue;
5436 time2 = MAX_RECOG_OPERANDS * 4 + 1;
5437 break;
5438 case RELOAD_FOR_OPERAND_ADDRESS:
5439 time2 = MAX_RECOG_OPERANDS * 4 + 2;
5440 check_earlyclobber = 1;
5441 break;
5442 case RELOAD_FOR_INSN:
5443 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5444 break;
5445 case RELOAD_FOR_OUTPUT:
5446 /* All RELOAD_FOR_OUTPUT reloads become live just after the
5447 instruction is executed. */
5448 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5449 break;
5450 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
5451 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
5452 value. */
5453 case RELOAD_FOR_OUTADDR_ADDRESS:
5454 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
5455 && ignore_address_reloads
5456 && ! rld[reloadnum].out)
5457 continue;
5458 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
5459 break;
5460 case RELOAD_FOR_OUTPUT_ADDRESS:
5461 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
5462 break;
5463 case RELOAD_OTHER:
5464 /* If there is no conflict in the input part, handle this
5465 like an output reload. */
5466 if (! rld[i].in || rtx_equal_p (rld[i].in, value))
5468 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5469 /* Earlyclobbered outputs must conflict with inputs. */
5470 if (earlyclobber_operand_p (rld[i].out))
5471 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5473 break;
5475 time2 = 1;
5476 /* RELOAD_OTHER might be live beyond instruction execution,
5477 but this is not obvious when we set time2 = 1. So check
5478 here if there might be a problem with the new reload
5479 clobbering the register used by the RELOAD_OTHER. */
5480 if (out)
5481 return 0;
5482 break;
5483 default:
5484 return 0;
5486 if ((time1 >= time2
5487 && (! rld[i].in || rld[i].out
5488 || ! rtx_equal_p (rld[i].in, value)))
5489 || (out && rld[reloadnum].out_reg
5490 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
5491 return 0;
5496 /* Earlyclobbered outputs must conflict with inputs. */
5497 if (check_earlyclobber && out && earlyclobber_operand_p (out))
5498 return 0;
5500 return 1;
5503 /* Give an error message saying we failed to find a reload for INSN,
5504 and clear out reload R. */
5505 static void
5506 failed_reload (insn, r)
5507 rtx insn;
5508 int r;
5510 if (asm_noperands (PATTERN (insn)) < 0)
5511 /* It's the compiler's fault. */
5512 fatal_insn ("Could not find a spill register", insn);
5514 /* It's the user's fault; the operand's mode and constraint
5515 don't match. Disable this reload so we don't crash in final. */
5516 error_for_asm (insn,
5517 "`asm' operand constraint incompatible with operand size");
5518 rld[r].in = 0;
5519 rld[r].out = 0;
5520 rld[r].reg_rtx = 0;
5521 rld[r].optional = 1;
5522 rld[r].secondary_p = 1;
5525 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5526 for reload R. If it's valid, get an rtx for it. Return nonzero if
5527 successful. */
5528 static int
5529 set_reload_reg (i, r)
5530 int i, r;
5532 int regno;
5533 rtx reg = spill_reg_rtx[i];
5535 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5536 spill_reg_rtx[i] = reg
5537 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5539 regno = true_regnum (reg);
5541 /* Detect when the reload reg can't hold the reload mode.
5542 This used to be one `if', but Sequent compiler can't handle that. */
5543 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5545 enum machine_mode test_mode = VOIDmode;
5546 if (rld[r].in)
5547 test_mode = GET_MODE (rld[r].in);
5548 /* If rld[r].in has VOIDmode, it means we will load it
5549 in whatever mode the reload reg has: to wit, rld[r].mode.
5550 We have already tested that for validity. */
5551 /* Aside from that, we need to test that the expressions
5552 to reload from or into have modes which are valid for this
5553 reload register. Otherwise the reload insns would be invalid. */
5554 if (! (rld[r].in != 0 && test_mode != VOIDmode
5555 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5556 if (! (rld[r].out != 0
5557 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5559 /* The reg is OK. */
5560 last_spill_reg = i;
5562 /* Mark as in use for this insn the reload regs we use
5563 for this. */
5564 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5565 rld[r].when_needed, rld[r].mode);
5567 rld[r].reg_rtx = reg;
5568 reload_spill_index[r] = spill_regs[i];
5569 return 1;
5572 return 0;
5575 /* Find a spill register to use as a reload register for reload R.
5576 LAST_RELOAD is non-zero if this is the last reload for the insn being
5577 processed.
5579 Set rld[R].reg_rtx to the register allocated.
5581 If NOERROR is nonzero, we return 1 if successful,
5582 or 0 if we couldn't find a spill reg and we didn't change anything. */
5584 static int
5585 allocate_reload_reg (chain, r, last_reload, noerror)
5586 struct insn_chain *chain;
5587 int r;
5588 int last_reload;
5589 int noerror;
5591 rtx insn = chain->insn;
5592 int i, pass, count;
5594 /* If we put this reload ahead, thinking it is a group,
5595 then insist on finding a group. Otherwise we can grab a
5596 reg that some other reload needs.
5597 (That can happen when we have a 68000 DATA_OR_FP_REG
5598 which is a group of data regs or one fp reg.)
5599 We need not be so restrictive if there are no more reloads
5600 for this insn.
5602 ??? Really it would be nicer to have smarter handling
5603 for that kind of reg class, where a problem like this is normal.
5604 Perhaps those classes should be avoided for reloading
5605 by use of more alternatives. */
5607 int force_group = rld[r].nregs > 1 && ! last_reload;
5609 /* If we want a single register and haven't yet found one,
5610 take any reg in the right class and not in use.
5611 If we want a consecutive group, here is where we look for it.
5613 We use two passes so we can first look for reload regs to
5614 reuse, which are already in use for other reloads in this insn,
5615 and only then use additional registers.
5616 I think that maximizing reuse is needed to make sure we don't
5617 run out of reload regs. Suppose we have three reloads, and
5618 reloads A and B can share regs. These need two regs.
5619 Suppose A and B are given different regs.
5620 That leaves none for C. */
5621 for (pass = 0; pass < 2; pass++)
5623 /* I is the index in spill_regs.
5624 We advance it round-robin between insns to use all spill regs
5625 equally, so that inherited reloads have a chance
5626 of leapfrogging each other. Don't do this, however, when we have
5627 group needs and failure would be fatal; if we only have a relatively
5628 small number of spill registers, and more than one of them has
5629 group needs, then by starting in the middle, we may end up
5630 allocating the first one in such a way that we are not left with
5631 sufficient groups to handle the rest. */
5633 if (noerror || ! force_group)
5634 i = last_spill_reg;
5635 else
5636 i = -1;
5638 for (count = 0; count < n_spills; count++)
5640 int class = (int) rld[r].class;
5641 int regnum;
5643 i++;
5644 if (i >= n_spills)
5645 i -= n_spills;
5646 regnum = spill_regs[i];
5648 if ((reload_reg_free_p (regnum, rld[r].opnum,
5649 rld[r].when_needed)
5650 || (rld[r].in
5651 /* We check reload_reg_used to make sure we
5652 don't clobber the return register. */
5653 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5654 && reload_reg_free_for_value_p (regnum,
5655 rld[r].opnum,
5656 rld[r].when_needed,
5657 rld[r].in,
5658 rld[r].out, r, 1)))
5659 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5660 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5661 /* Look first for regs to share, then for unshared. But
5662 don't share regs used for inherited reloads; they are
5663 the ones we want to preserve. */
5664 && (pass
5665 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5666 regnum)
5667 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5668 regnum))))
5670 int nr = HARD_REGNO_NREGS (regnum, rld[r].mode);
5671 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5672 (on 68000) got us two FP regs. If NR is 1,
5673 we would reject both of them. */
5674 if (force_group)
5675 nr = rld[r].nregs;
5676 /* If we need only one reg, we have already won. */
5677 if (nr == 1)
5679 /* But reject a single reg if we demand a group. */
5680 if (force_group)
5681 continue;
5682 break;
5684 /* Otherwise check that as many consecutive regs as we need
5685 are available here.
5686 Also, don't use for a group registers that are
5687 needed for nongroups. */
5688 if (! TEST_HARD_REG_BIT (chain->counted_for_nongroups, regnum))
5689 while (nr > 1)
5691 int regno = regnum + nr - 1;
5692 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5693 && spill_reg_order[regno] >= 0
5694 && reload_reg_free_p (regno, rld[r].opnum,
5695 rld[r].when_needed)
5696 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups,
5697 regno)))
5698 break;
5699 nr--;
5701 if (nr == 1)
5702 break;
5706 /* If we found something on pass 1, omit pass 2. */
5707 if (count < n_spills)
5708 break;
5711 /* We should have found a spill register by now. */
5712 if (count == n_spills)
5714 if (noerror)
5715 return 0;
5716 goto failure;
5719 if (set_reload_reg (i, r))
5720 return 1;
5722 /* The reg is not OK. */
5723 if (noerror)
5724 return 0;
5726 failure:
5727 failed_reload (insn, r);
5729 return 1;
5732 /* Initialize all the tables needed to allocate reload registers.
5733 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5734 is the array we use to restore the reg_rtx field for every reload. */
5735 static void
5736 choose_reload_regs_init (chain, save_reload_reg_rtx)
5737 struct insn_chain *chain;
5738 rtx *save_reload_reg_rtx;
5740 int i;
5742 for (i = 0; i < n_reloads; i++)
5743 rld[i].reg_rtx = save_reload_reg_rtx[i];
5745 bzero (reload_inherited, MAX_RELOADS);
5746 bzero ((char *) reload_inheritance_insn, MAX_RELOADS * sizeof (rtx));
5747 bzero ((char *) reload_override_in, MAX_RELOADS * sizeof (rtx));
5749 CLEAR_HARD_REG_SET (reload_reg_used);
5750 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5751 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5752 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5753 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5754 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5756 CLEAR_HARD_REG_SET (reg_used_in_insn);
5758 HARD_REG_SET tmp;
5759 REG_SET_TO_HARD_REG_SET (tmp, chain->live_before);
5760 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5761 REG_SET_TO_HARD_REG_SET (tmp, chain->live_after);
5762 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5763 compute_use_by_pseudos (&reg_used_in_insn, chain->live_before);
5764 compute_use_by_pseudos (&reg_used_in_insn, chain->live_after);
5766 for (i = 0; i < reload_n_operands; i++)
5768 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5769 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5770 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5771 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5772 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5773 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5776 IOR_COMPL_HARD_REG_SET (reload_reg_used, chain->used_spill_regs);
5778 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5780 for (i = 0; i < n_reloads; i++)
5781 /* If we have already decided to use a certain register,
5782 don't use it in another way. */
5783 if (rld[i].reg_rtx)
5784 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5785 rld[i].when_needed, rld[i].mode);
5788 /* Assign hard reg targets for the pseudo-registers we must reload
5789 into hard regs for this insn.
5790 Also output the instructions to copy them in and out of the hard regs.
5792 For machines with register classes, we are responsible for
5793 finding a reload reg in the proper class. */
5795 static void
5796 choose_reload_regs (chain)
5797 struct insn_chain *chain;
5799 rtx insn = chain->insn;
5800 register int i, j;
5801 int max_group_size = 1;
5802 enum reg_class group_class = NO_REGS;
5803 int inheritance;
5804 int pass;
5806 rtx save_reload_reg_rtx[MAX_RELOADS];
5808 /* In order to be certain of getting the registers we need,
5809 we must sort the reloads into order of increasing register class.
5810 Then our grabbing of reload registers will parallel the process
5811 that provided the reload registers.
5813 Also note whether any of the reloads wants a consecutive group of regs.
5814 If so, record the maximum size of the group desired and what
5815 register class contains all the groups needed by this insn. */
5817 for (j = 0; j < n_reloads; j++)
5819 reload_order[j] = j;
5820 reload_spill_index[j] = -1;
5822 if (rld[j].nregs > 1)
5824 max_group_size = MAX (rld[j].nregs, max_group_size);
5825 group_class = reg_class_superunion[(int)rld[j].class][(int)group_class];
5828 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5831 if (n_reloads > 1)
5832 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5834 /* If -O, try first with inheritance, then turning it off.
5835 If not -O, don't do inheritance.
5836 Using inheritance when not optimizing leads to paradoxes
5837 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5838 because one side of the comparison might be inherited. */
5840 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5842 choose_reload_regs_init (chain, save_reload_reg_rtx);
5844 /* Process the reloads in order of preference just found.
5845 Beyond this point, subregs can be found in reload_reg_rtx.
5847 This used to look for an existing reloaded home for all
5848 of the reloads, and only then perform any new reloads.
5849 But that could lose if the reloads were done out of reg-class order
5850 because a later reload with a looser constraint might have an old
5851 home in a register needed by an earlier reload with a tighter constraint.
5853 To solve this, we make two passes over the reloads, in the order
5854 described above. In the first pass we try to inherit a reload
5855 from a previous insn. If there is a later reload that needs a
5856 class that is a proper subset of the class being processed, we must
5857 also allocate a spill register during the first pass.
5859 Then make a second pass over the reloads to allocate any reloads
5860 that haven't been given registers yet. */
5862 for (j = 0; j < n_reloads; j++)
5864 register int r = reload_order[j];
5865 rtx search_equiv = NULL_RTX;
5867 /* Ignore reloads that got marked inoperative. */
5868 if (rld[r].out == 0 && rld[r].in == 0
5869 && ! rld[r].secondary_p)
5870 continue;
5872 /* If find_reloads chose to use reload_in or reload_out as a reload
5873 register, we don't need to chose one. Otherwise, try even if it
5874 found one since we might save an insn if we find the value lying
5875 around.
5876 Try also when reload_in is a pseudo without a hard reg. */
5877 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5878 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5879 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5880 && GET_CODE (rld[r].in) != MEM
5881 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5882 continue;
5884 #if 0 /* No longer needed for correct operation.
5885 It might give better code, or might not; worth an experiment? */
5886 /* If this is an optional reload, we can't inherit from earlier insns
5887 until we are sure that any non-optional reloads have been allocated.
5888 The following code takes advantage of the fact that optional reloads
5889 are at the end of reload_order. */
5890 if (rld[r].optional != 0)
5891 for (i = 0; i < j; i++)
5892 if ((rld[reload_order[i]].out != 0
5893 || rld[reload_order[i]].in != 0
5894 || rld[reload_order[i]].secondary_p)
5895 && ! rld[reload_order[i]].optional
5896 && rld[reload_order[i]].reg_rtx == 0)
5897 allocate_reload_reg (chain, reload_order[i], 0, inheritance);
5898 #endif
5900 /* First see if this pseudo is already available as reloaded
5901 for a previous insn. We cannot try to inherit for reloads
5902 that are smaller than the maximum number of registers needed
5903 for groups unless the register we would allocate cannot be used
5904 for the groups.
5906 We could check here to see if this is a secondary reload for
5907 an object that is already in a register of the desired class.
5908 This would avoid the need for the secondary reload register.
5909 But this is complex because we can't easily determine what
5910 objects might want to be loaded via this reload. So let a
5911 register be allocated here. In `emit_reload_insns' we suppress
5912 one of the loads in the case described above. */
5914 if (inheritance)
5916 int word = 0;
5917 register int regno = -1;
5918 enum machine_mode mode = VOIDmode;
5920 if (rld[r].in == 0)
5922 else if (GET_CODE (rld[r].in) == REG)
5924 regno = REGNO (rld[r].in);
5925 mode = GET_MODE (rld[r].in);
5927 else if (GET_CODE (rld[r].in_reg) == REG)
5929 regno = REGNO (rld[r].in_reg);
5930 mode = GET_MODE (rld[r].in_reg);
5932 else if (GET_CODE (rld[r].in_reg) == SUBREG
5933 && GET_CODE (SUBREG_REG (rld[r].in_reg)) == REG)
5935 word = SUBREG_WORD (rld[r].in_reg);
5936 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5937 if (regno < FIRST_PSEUDO_REGISTER)
5938 regno += word;
5939 mode = GET_MODE (rld[r].in_reg);
5941 #ifdef AUTO_INC_DEC
5942 else if ((GET_CODE (rld[r].in_reg) == PRE_INC
5943 || GET_CODE (rld[r].in_reg) == PRE_DEC
5944 || GET_CODE (rld[r].in_reg) == POST_INC
5945 || GET_CODE (rld[r].in_reg) == POST_DEC)
5946 && GET_CODE (XEXP (rld[r].in_reg, 0)) == REG)
5948 regno = REGNO (XEXP (rld[r].in_reg, 0));
5949 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5950 rld[r].out = rld[r].in;
5952 #endif
5953 #if 0
5954 /* This won't work, since REGNO can be a pseudo reg number.
5955 Also, it takes much more hair to keep track of all the things
5956 that can invalidate an inherited reload of part of a pseudoreg. */
5957 else if (GET_CODE (rld[r].in) == SUBREG
5958 && GET_CODE (SUBREG_REG (rld[r].in)) == REG)
5959 regno = REGNO (SUBREG_REG (rld[r].in)) + SUBREG_WORD (rld[r].in);
5960 #endif
5962 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5964 enum reg_class class = rld[r].class, last_class;
5965 rtx last_reg = reg_last_reload_reg[regno];
5967 i = REGNO (last_reg) + word;
5968 last_class = REGNO_REG_CLASS (i);
5969 if ((GET_MODE_SIZE (GET_MODE (last_reg))
5970 >= GET_MODE_SIZE (mode) + word * UNITS_PER_WORD)
5971 && reg_reloaded_contents[i] == regno
5972 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
5973 && HARD_REGNO_MODE_OK (i, rld[r].mode)
5974 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5975 /* Even if we can't use this register as a reload
5976 register, we might use it for reload_override_in,
5977 if copying it to the desired class is cheap
5978 enough. */
5979 || ((REGISTER_MOVE_COST (last_class, class)
5980 < MEMORY_MOVE_COST (mode, class, 1))
5981 #ifdef SECONDARY_INPUT_RELOAD_CLASS
5982 && (SECONDARY_INPUT_RELOAD_CLASS (class, mode,
5983 last_reg)
5984 == NO_REGS)
5985 #endif
5986 #ifdef SECONDARY_MEMORY_NEEDED
5987 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5988 mode)
5989 #endif
5992 && (rld[r].nregs == max_group_size
5993 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5995 && reload_reg_free_for_value_p (i, rld[r].opnum,
5996 rld[r].when_needed,
5997 rld[r].in,
5998 const0_rtx, r, 1))
6000 /* If a group is needed, verify that all the subsequent
6001 registers still have their values intact. */
6002 int nr
6003 = HARD_REGNO_NREGS (i, rld[r].mode);
6004 int k;
6006 for (k = 1; k < nr; k++)
6007 if (reg_reloaded_contents[i + k] != regno
6008 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
6009 break;
6011 if (k == nr)
6013 int i1;
6015 last_reg = (GET_MODE (last_reg) == mode
6016 ? last_reg : gen_rtx_REG (mode, i));
6018 /* We found a register that contains the
6019 value we need. If this register is the
6020 same as an `earlyclobber' operand of the
6021 current insn, just mark it as a place to
6022 reload from since we can't use it as the
6023 reload register itself. */
6025 for (i1 = 0; i1 < n_earlyclobbers; i1++)
6026 if (reg_overlap_mentioned_for_reload_p
6027 (reg_last_reload_reg[regno],
6028 reload_earlyclobbers[i1]))
6029 break;
6031 if (i1 != n_earlyclobbers
6032 || ! (reload_reg_free_for_value_p
6033 (i, rld[r].opnum, rld[r].when_needed,
6034 rld[r].in, rld[r].out, r, 1))
6035 /* Don't use it if we'd clobber a pseudo reg. */
6036 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
6037 && rld[r].out
6038 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
6039 /* Don't clobber the frame pointer. */
6040 || (i == HARD_FRAME_POINTER_REGNUM && rld[r].out)
6041 /* Don't really use the inherited spill reg
6042 if we need it wider than we've got it. */
6043 || (GET_MODE_SIZE (rld[r].mode)
6044 > GET_MODE_SIZE (mode))
6045 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
6048 /* If find_reloads chose reload_out as reload
6049 register, stay with it - that leaves the
6050 inherited register for subsequent reloads. */
6051 || (rld[r].out && rld[r].reg_rtx
6052 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
6054 reload_override_in[r] = last_reg;
6055 reload_inheritance_insn[r]
6056 = reg_reloaded_insn[i];
6058 else
6060 int k;
6061 /* We can use this as a reload reg. */
6062 /* Mark the register as in use for this part of
6063 the insn. */
6064 mark_reload_reg_in_use (i,
6065 rld[r].opnum,
6066 rld[r].when_needed,
6067 rld[r].mode);
6068 rld[r].reg_rtx = last_reg;
6069 reload_inherited[r] = 1;
6070 reload_inheritance_insn[r]
6071 = reg_reloaded_insn[i];
6072 reload_spill_index[r] = i;
6073 for (k = 0; k < nr; k++)
6074 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6075 i + k);
6082 /* Here's another way to see if the value is already lying around. */
6083 if (inheritance
6084 && rld[r].in != 0
6085 && ! reload_inherited[r]
6086 && rld[r].out == 0
6087 && (CONSTANT_P (rld[r].in)
6088 || GET_CODE (rld[r].in) == PLUS
6089 || GET_CODE (rld[r].in) == REG
6090 || GET_CODE (rld[r].in) == MEM)
6091 && (rld[r].nregs == max_group_size
6092 || ! reg_classes_intersect_p (rld[r].class, group_class)))
6093 search_equiv = rld[r].in;
6094 /* If this is an output reload from a simple move insn, look
6095 if an equivalence for the input is available. */
6096 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
6098 rtx set = single_set (insn);
6100 if (set
6101 && rtx_equal_p (rld[r].out, SET_DEST (set))
6102 && CONSTANT_P (SET_SRC (set)))
6103 search_equiv = SET_SRC (set);
6106 if (search_equiv)
6108 register rtx equiv
6109 = find_equiv_reg (search_equiv, insn, rld[r].class,
6110 -1, NULL_PTR, 0, rld[r].mode);
6111 int regno = 0;
6113 if (equiv != 0)
6115 if (GET_CODE (equiv) == REG)
6116 regno = REGNO (equiv);
6117 else if (GET_CODE (equiv) == SUBREG)
6119 /* This must be a SUBREG of a hard register.
6120 Make a new REG since this might be used in an
6121 address and not all machines support SUBREGs
6122 there. */
6123 regno = REGNO (SUBREG_REG (equiv)) + SUBREG_WORD (equiv);
6124 equiv = gen_rtx_REG (rld[r].mode, regno);
6126 else
6127 abort ();
6130 /* If we found a spill reg, reject it unless it is free
6131 and of the desired class. */
6132 if (equiv != 0
6133 && ((TEST_HARD_REG_BIT (reload_reg_used_at_all, regno)
6134 && ! reload_reg_free_for_value_p (regno, rld[r].opnum,
6135 rld[r].when_needed,
6136 rld[r].in,
6137 rld[r].out, r, 1))
6138 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
6139 regno)))
6140 equiv = 0;
6142 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
6143 equiv = 0;
6145 /* We found a register that contains the value we need.
6146 If this register is the same as an `earlyclobber' operand
6147 of the current insn, just mark it as a place to reload from
6148 since we can't use it as the reload register itself. */
6150 if (equiv != 0)
6151 for (i = 0; i < n_earlyclobbers; i++)
6152 if (reg_overlap_mentioned_for_reload_p (equiv,
6153 reload_earlyclobbers[i]))
6155 reload_override_in[r] = equiv;
6156 equiv = 0;
6157 break;
6160 /* If the equiv register we have found is explicitly clobbered
6161 in the current insn, it depends on the reload type if we
6162 can use it, use it for reload_override_in, or not at all.
6163 In particular, we then can't use EQUIV for a
6164 RELOAD_FOR_OUTPUT_ADDRESS reload. */
6166 if (equiv != 0 && regno_clobbered_p (regno, insn))
6168 switch (rld[r].when_needed)
6170 case RELOAD_FOR_OTHER_ADDRESS:
6171 case RELOAD_FOR_INPADDR_ADDRESS:
6172 case RELOAD_FOR_INPUT_ADDRESS:
6173 case RELOAD_FOR_OPADDR_ADDR:
6174 break;
6175 case RELOAD_OTHER:
6176 case RELOAD_FOR_INPUT:
6177 case RELOAD_FOR_OPERAND_ADDRESS:
6178 reload_override_in[r] = equiv;
6179 /* Fall through. */
6180 default:
6181 equiv = 0;
6182 break;
6186 /* If we found an equivalent reg, say no code need be generated
6187 to load it, and use it as our reload reg. */
6188 if (equiv != 0 && regno != HARD_FRAME_POINTER_REGNUM)
6190 int nr = HARD_REGNO_NREGS (regno, rld[r].mode);
6191 int k;
6192 rld[r].reg_rtx = equiv;
6193 reload_inherited[r] = 1;
6195 /* If reg_reloaded_valid is not set for this register,
6196 there might be a stale spill_reg_store lying around.
6197 We must clear it, since otherwise emit_reload_insns
6198 might delete the store. */
6199 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
6200 spill_reg_store[regno] = NULL_RTX;
6201 /* If any of the hard registers in EQUIV are spill
6202 registers, mark them as in use for this insn. */
6203 for (k = 0; k < nr; k++)
6205 i = spill_reg_order[regno + k];
6206 if (i >= 0)
6208 mark_reload_reg_in_use (regno, rld[r].opnum,
6209 rld[r].when_needed,
6210 rld[r].mode);
6211 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6212 regno + k);
6218 /* If we found a register to use already, or if this is an optional
6219 reload, we are done. */
6220 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
6221 continue;
6223 #if 0 /* No longer needed for correct operation. Might or might not
6224 give better code on the average. Want to experiment? */
6226 /* See if there is a later reload that has a class different from our
6227 class that intersects our class or that requires less register
6228 than our reload. If so, we must allocate a register to this
6229 reload now, since that reload might inherit a previous reload
6230 and take the only available register in our class. Don't do this
6231 for optional reloads since they will force all previous reloads
6232 to be allocated. Also don't do this for reloads that have been
6233 turned off. */
6235 for (i = j + 1; i < n_reloads; i++)
6237 int s = reload_order[i];
6239 if ((rld[s].in == 0 && rld[s].out == 0
6240 && ! rld[s].secondary_p)
6241 || rld[s].optional)
6242 continue;
6244 if ((rld[s].class != rld[r].class
6245 && reg_classes_intersect_p (rld[r].class,
6246 rld[s].class))
6247 || rld[s].nregs < rld[r].nregs)
6248 break;
6251 if (i == n_reloads)
6252 continue;
6254 allocate_reload_reg (chain, r, j == n_reloads - 1, inheritance);
6255 #endif
6258 /* Now allocate reload registers for anything non-optional that
6259 didn't get one yet. */
6260 for (j = 0; j < n_reloads; j++)
6262 register int r = reload_order[j];
6264 /* Ignore reloads that got marked inoperative. */
6265 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
6266 continue;
6268 /* Skip reloads that already have a register allocated or are
6269 optional. */
6270 if (rld[r].reg_rtx != 0 || rld[r].optional)
6271 continue;
6273 if (! allocate_reload_reg (chain, r, j == n_reloads - 1, inheritance))
6274 break;
6277 /* If that loop got all the way, we have won. */
6278 if (j == n_reloads)
6279 break;
6281 /* Loop around and try without any inheritance. */
6284 /* If we thought we could inherit a reload, because it seemed that
6285 nothing else wanted the same reload register earlier in the insn,
6286 verify that assumption, now that all reloads have been assigned.
6287 Likewise for reloads where reload_override_in has been set. */
6289 /* If doing expensive optimizations, do one preliminary pass that doesn't
6290 cancel any inheritance, but removes reloads that have been needed only
6291 for reloads that we know can be inherited. */
6292 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
6294 for (j = 0; j < n_reloads; j++)
6296 register int r = reload_order[j];
6297 rtx check_reg;
6298 if (reload_inherited[r] && rld[r].reg_rtx)
6299 check_reg = rld[r].reg_rtx;
6300 else if (reload_override_in[r]
6301 && (GET_CODE (reload_override_in[r]) == REG
6302 || GET_CODE (reload_override_in[r]) == SUBREG))
6303 check_reg = reload_override_in[r];
6304 else
6305 continue;
6306 if (! reload_reg_free_for_value_p (true_regnum (check_reg),
6307 rld[r].opnum,
6308 rld[r].when_needed,
6309 rld[r].in,
6310 (reload_inherited[r]
6311 ? rld[r].out : const0_rtx),
6312 r, 1))
6314 if (pass)
6315 continue;
6316 reload_inherited[r] = 0;
6317 reload_override_in[r] = 0;
6319 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
6320 reload_override_in, then we do not need its related
6321 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
6322 likewise for other reload types.
6323 We handle this by removing a reload when its only replacement
6324 is mentioned in reload_in of the reload we are going to inherit.
6325 A special case are auto_inc expressions; even if the input is
6326 inherited, we still need the address for the output. We can
6327 recognize them because they have RELOAD_OUT set to RELOAD_IN.
6328 If we suceeded removing some reload and we are doing a preliminary
6329 pass just to remove such reloads, make another pass, since the
6330 removal of one reload might allow us to inherit another one. */
6331 else if (rld[r].in
6332 && rld[r].out != rld[r].in
6333 && remove_address_replacements (rld[r].in) && pass)
6334 pass = 2;
6338 /* Now that reload_override_in is known valid,
6339 actually override reload_in. */
6340 for (j = 0; j < n_reloads; j++)
6341 if (reload_override_in[j])
6342 rld[j].in = reload_override_in[j];
6344 /* If this reload won't be done because it has been cancelled or is
6345 optional and not inherited, clear reload_reg_rtx so other
6346 routines (such as subst_reloads) don't get confused. */
6347 for (j = 0; j < n_reloads; j++)
6348 if (rld[j].reg_rtx != 0
6349 && ((rld[j].optional && ! reload_inherited[j])
6350 || (rld[j].in == 0 && rld[j].out == 0
6351 && ! rld[j].secondary_p)))
6353 int regno = true_regnum (rld[j].reg_rtx);
6355 if (spill_reg_order[regno] >= 0)
6356 clear_reload_reg_in_use (regno, rld[j].opnum,
6357 rld[j].when_needed, rld[j].mode);
6358 rld[j].reg_rtx = 0;
6361 /* Record which pseudos and which spill regs have output reloads. */
6362 for (j = 0; j < n_reloads; j++)
6364 register int r = reload_order[j];
6366 i = reload_spill_index[r];
6368 /* I is nonneg if this reload uses a register.
6369 If rld[r].reg_rtx is 0, this is an optional reload
6370 that we opted to ignore. */
6371 if (rld[r].out_reg != 0 && GET_CODE (rld[r].out_reg) == REG
6372 && rld[r].reg_rtx != 0)
6374 register int nregno = REGNO (rld[r].out_reg);
6375 int nr = 1;
6377 if (nregno < FIRST_PSEUDO_REGISTER)
6378 nr = HARD_REGNO_NREGS (nregno, rld[r].mode);
6380 while (--nr >= 0)
6381 reg_has_output_reload[nregno + nr] = 1;
6383 if (i >= 0)
6385 nr = HARD_REGNO_NREGS (i, rld[r].mode);
6386 while (--nr >= 0)
6387 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
6390 if (rld[r].when_needed != RELOAD_OTHER
6391 && rld[r].when_needed != RELOAD_FOR_OUTPUT
6392 && rld[r].when_needed != RELOAD_FOR_INSN)
6393 abort ();
6398 /* Deallocate the reload register for reload R. This is called from
6399 remove_address_replacements. */
6400 void
6401 deallocate_reload_reg (r)
6402 int r;
6404 int regno;
6406 if (! rld[r].reg_rtx)
6407 return;
6408 regno = true_regnum (rld[r].reg_rtx);
6409 rld[r].reg_rtx = 0;
6410 if (spill_reg_order[regno] >= 0)
6411 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
6412 rld[r].mode);
6413 reload_spill_index[r] = -1;
6416 /* If SMALL_REGISTER_CLASSES is non-zero, we may not have merged two
6417 reloads of the same item for fear that we might not have enough reload
6418 registers. However, normally they will get the same reload register
6419 and hence actually need not be loaded twice.
6421 Here we check for the most common case of this phenomenon: when we have
6422 a number of reloads for the same object, each of which were allocated
6423 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6424 reload, and is not modified in the insn itself. If we find such,
6425 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6426 This will not increase the number of spill registers needed and will
6427 prevent redundant code. */
6429 static void
6430 merge_assigned_reloads (insn)
6431 rtx insn;
6433 int i, j;
6435 /* Scan all the reloads looking for ones that only load values and
6436 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6437 assigned and not modified by INSN. */
6439 for (i = 0; i < n_reloads; i++)
6441 int conflicting_input = 0;
6442 int max_input_address_opnum = -1;
6443 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6445 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6446 || rld[i].out != 0 || rld[i].reg_rtx == 0
6447 || reg_set_p (rld[i].reg_rtx, insn))
6448 continue;
6450 /* Look at all other reloads. Ensure that the only use of this
6451 reload_reg_rtx is in a reload that just loads the same value
6452 as we do. Note that any secondary reloads must be of the identical
6453 class since the values, modes, and result registers are the
6454 same, so we need not do anything with any secondary reloads. */
6456 for (j = 0; j < n_reloads; j++)
6458 if (i == j || rld[j].reg_rtx == 0
6459 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6460 rld[i].reg_rtx))
6461 continue;
6463 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6464 && rld[j].opnum > max_input_address_opnum)
6465 max_input_address_opnum = rld[j].opnum;
6467 /* If the reload regs aren't exactly the same (e.g, different modes)
6468 or if the values are different, we can't merge this reload.
6469 But if it is an input reload, we might still merge
6470 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6472 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6473 || rld[j].out != 0 || rld[j].in == 0
6474 || ! rtx_equal_p (rld[i].in, rld[j].in))
6476 if (rld[j].when_needed != RELOAD_FOR_INPUT
6477 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6478 || rld[i].opnum > rld[j].opnum)
6479 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6480 break;
6481 conflicting_input = 1;
6482 if (min_conflicting_input_opnum > rld[j].opnum)
6483 min_conflicting_input_opnum = rld[j].opnum;
6487 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6488 we, in fact, found any matching reloads. */
6490 if (j == n_reloads
6491 && max_input_address_opnum <= min_conflicting_input_opnum)
6493 for (j = 0; j < n_reloads; j++)
6494 if (i != j && rld[j].reg_rtx != 0
6495 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6496 && (! conflicting_input
6497 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6498 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6500 rld[i].when_needed = RELOAD_OTHER;
6501 rld[j].in = 0;
6502 reload_spill_index[j] = -1;
6503 transfer_replacements (i, j);
6506 /* If this is now RELOAD_OTHER, look for any reloads that load
6507 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6508 if they were for inputs, RELOAD_OTHER for outputs. Note that
6509 this test is equivalent to looking for reloads for this operand
6510 number. */
6512 if (rld[i].when_needed == RELOAD_OTHER)
6513 for (j = 0; j < n_reloads; j++)
6514 if (rld[j].in != 0
6515 && rld[i].when_needed != RELOAD_OTHER
6516 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6517 rld[i].in))
6518 rld[j].when_needed
6519 = ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
6520 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6521 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6527 /* Output insns to reload values in and out of the chosen reload regs. */
6529 static void
6530 emit_reload_insns (chain)
6531 struct insn_chain *chain;
6533 rtx insn = chain->insn;
6535 register int j;
6536 rtx input_reload_insns[MAX_RECOG_OPERANDS];
6537 rtx other_input_address_reload_insns = 0;
6538 rtx other_input_reload_insns = 0;
6539 rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6540 rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6541 rtx output_reload_insns[MAX_RECOG_OPERANDS];
6542 rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6543 rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6544 rtx operand_reload_insns = 0;
6545 rtx other_operand_reload_insns = 0;
6546 rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6547 rtx following_insn = NEXT_INSN (insn);
6548 rtx before_insn = PREV_INSN (insn);
6549 int special;
6550 /* Values to be put in spill_reg_store are put here first. */
6551 rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6552 HARD_REG_SET reg_reloaded_died;
6554 CLEAR_HARD_REG_SET (reg_reloaded_died);
6556 for (j = 0; j < reload_n_operands; j++)
6557 input_reload_insns[j] = input_address_reload_insns[j]
6558 = inpaddr_address_reload_insns[j]
6559 = output_reload_insns[j] = output_address_reload_insns[j]
6560 = outaddr_address_reload_insns[j]
6561 = other_output_reload_insns[j] = 0;
6563 /* Now output the instructions to copy the data into and out of the
6564 reload registers. Do these in the order that the reloads were reported,
6565 since reloads of base and index registers precede reloads of operands
6566 and the operands may need the base and index registers reloaded. */
6568 for (j = 0; j < n_reloads; j++)
6570 register rtx old;
6571 rtx oldequiv_reg = 0;
6572 rtx this_reload_insn = 0;
6573 int expect_occurrences = 1;
6575 if (rld[j].reg_rtx
6576 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
6577 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
6579 old = (rld[j].in && GET_CODE (rld[j].in) == MEM
6580 ? rld[j].in_reg : rld[j].in);
6582 if (old != 0
6583 /* AUTO_INC reloads need to be handled even if inherited. We got an
6584 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
6585 && (! reload_inherited[j] || (rld[j].out && ! rld[j].out_reg))
6586 && ! rtx_equal_p (rld[j].reg_rtx, old)
6587 && rld[j].reg_rtx != 0)
6589 register rtx reloadreg = rld[j].reg_rtx;
6590 rtx oldequiv = 0;
6591 enum machine_mode mode;
6592 rtx *where;
6594 /* Determine the mode to reload in.
6595 This is very tricky because we have three to choose from.
6596 There is the mode the insn operand wants (rld[J].inmode).
6597 There is the mode of the reload register RELOADREG.
6598 There is the intrinsic mode of the operand, which we could find
6599 by stripping some SUBREGs.
6600 It turns out that RELOADREG's mode is irrelevant:
6601 we can change that arbitrarily.
6603 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6604 then the reload reg may not support QImode moves, so use SImode.
6605 If foo is in memory due to spilling a pseudo reg, this is safe,
6606 because the QImode value is in the least significant part of a
6607 slot big enough for a SImode. If foo is some other sort of
6608 memory reference, then it is impossible to reload this case,
6609 so previous passes had better make sure this never happens.
6611 Then consider a one-word union which has SImode and one of its
6612 members is a float, being fetched as (SUBREG:SF union:SI).
6613 We must fetch that as SFmode because we could be loading into
6614 a float-only register. In this case OLD's mode is correct.
6616 Consider an immediate integer: it has VOIDmode. Here we need
6617 to get a mode from something else.
6619 In some cases, there is a fourth mode, the operand's
6620 containing mode. If the insn specifies a containing mode for
6621 this operand, it overrides all others.
6623 I am not sure whether the algorithm here is always right,
6624 but it does the right things in those cases. */
6626 mode = GET_MODE (old);
6627 if (mode == VOIDmode)
6628 mode = rld[j].inmode;
6630 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6631 /* If we need a secondary register for this operation, see if
6632 the value is already in a register in that class. Don't
6633 do this if the secondary register will be used as a scratch
6634 register. */
6636 if (rld[j].secondary_in_reload >= 0
6637 && rld[j].secondary_in_icode == CODE_FOR_nothing
6638 && optimize)
6639 oldequiv
6640 = find_equiv_reg (old, insn,
6641 rld[rld[j].secondary_in_reload].class,
6642 -1, NULL_PTR, 0, mode);
6643 #endif
6645 /* If reloading from memory, see if there is a register
6646 that already holds the same value. If so, reload from there.
6647 We can pass 0 as the reload_reg_p argument because
6648 any other reload has either already been emitted,
6649 in which case find_equiv_reg will see the reload-insn,
6650 or has yet to be emitted, in which case it doesn't matter
6651 because we will use this equiv reg right away. */
6653 if (oldequiv == 0 && optimize
6654 && (GET_CODE (old) == MEM
6655 || (GET_CODE (old) == REG
6656 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6657 && reg_renumber[REGNO (old)] < 0)))
6658 oldequiv = find_equiv_reg (old, insn, ALL_REGS,
6659 -1, NULL_PTR, 0, mode);
6661 if (oldequiv)
6663 int regno = true_regnum (oldequiv);
6665 /* Don't use OLDEQUIV if any other reload changes it at an
6666 earlier stage of this insn or at this stage. */
6667 if (! reload_reg_free_for_value_p (regno, rld[j].opnum,
6668 rld[j].when_needed,
6669 rld[j].in, const0_rtx, j,
6671 oldequiv = 0;
6673 /* If it is no cheaper to copy from OLDEQUIV into the
6674 reload register than it would be to move from memory,
6675 don't use it. Likewise, if we need a secondary register
6676 or memory. */
6678 if (oldequiv != 0
6679 && ((REGNO_REG_CLASS (regno) != rld[j].class
6680 && (REGISTER_MOVE_COST (REGNO_REG_CLASS (regno),
6681 rld[j].class)
6682 >= MEMORY_MOVE_COST (mode, rld[j].class, 1)))
6683 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6684 || (SECONDARY_INPUT_RELOAD_CLASS (rld[j].class,
6685 mode, oldequiv)
6686 != NO_REGS)
6687 #endif
6688 #ifdef SECONDARY_MEMORY_NEEDED
6689 || SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (regno),
6690 rld[j].class,
6691 mode)
6692 #endif
6694 oldequiv = 0;
6697 /* delete_output_reload is only invoked properly if old contains
6698 the original pseudo register. Since this is replaced with a
6699 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6700 find the pseudo in RELOAD_IN_REG. */
6701 if (oldequiv == 0
6702 && reload_override_in[j]
6703 && GET_CODE (rld[j].in_reg) == REG)
6705 oldequiv = old;
6706 old = rld[j].in_reg;
6708 if (oldequiv == 0)
6709 oldequiv = old;
6710 else if (GET_CODE (oldequiv) == REG)
6711 oldequiv_reg = oldequiv;
6712 else if (GET_CODE (oldequiv) == SUBREG)
6713 oldequiv_reg = SUBREG_REG (oldequiv);
6715 /* If we are reloading from a register that was recently stored in
6716 with an output-reload, see if we can prove there was
6717 actually no need to store the old value in it. */
6719 if (optimize && GET_CODE (oldequiv) == REG
6720 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6721 && spill_reg_store[REGNO (oldequiv)]
6722 && GET_CODE (old) == REG
6723 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6724 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6725 rld[j].out_reg)))
6726 delete_output_reload (insn, j, REGNO (oldequiv));
6728 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6729 then load RELOADREG from OLDEQUIV. Note that we cannot use
6730 gen_lowpart_common since it can do the wrong thing when
6731 RELOADREG has a multi-word mode. Note that RELOADREG
6732 must always be a REG here. */
6734 if (GET_MODE (reloadreg) != mode)
6735 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6736 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6737 oldequiv = SUBREG_REG (oldequiv);
6738 if (GET_MODE (oldequiv) != VOIDmode
6739 && mode != GET_MODE (oldequiv))
6740 oldequiv = gen_rtx_SUBREG (mode, oldequiv, 0);
6742 /* Switch to the right place to emit the reload insns. */
6743 switch (rld[j].when_needed)
6745 case RELOAD_OTHER:
6746 where = &other_input_reload_insns;
6747 break;
6748 case RELOAD_FOR_INPUT:
6749 where = &input_reload_insns[rld[j].opnum];
6750 break;
6751 case RELOAD_FOR_INPUT_ADDRESS:
6752 where = &input_address_reload_insns[rld[j].opnum];
6753 break;
6754 case RELOAD_FOR_INPADDR_ADDRESS:
6755 where = &inpaddr_address_reload_insns[rld[j].opnum];
6756 break;
6757 case RELOAD_FOR_OUTPUT_ADDRESS:
6758 where = &output_address_reload_insns[rld[j].opnum];
6759 break;
6760 case RELOAD_FOR_OUTADDR_ADDRESS:
6761 where = &outaddr_address_reload_insns[rld[j].opnum];
6762 break;
6763 case RELOAD_FOR_OPERAND_ADDRESS:
6764 where = &operand_reload_insns;
6765 break;
6766 case RELOAD_FOR_OPADDR_ADDR:
6767 where = &other_operand_reload_insns;
6768 break;
6769 case RELOAD_FOR_OTHER_ADDRESS:
6770 where = &other_input_address_reload_insns;
6771 break;
6772 default:
6773 abort ();
6776 push_to_sequence (*where);
6777 special = 0;
6779 /* Auto-increment addresses must be reloaded in a special way. */
6780 if (rld[j].out && ! rld[j].out_reg)
6782 /* We are not going to bother supporting the case where a
6783 incremented register can't be copied directly from
6784 OLDEQUIV since this seems highly unlikely. */
6785 if (rld[j].secondary_in_reload >= 0)
6786 abort ();
6788 if (reload_inherited[j])
6789 oldequiv = reloadreg;
6791 old = XEXP (rld[j].in_reg, 0);
6793 if (optimize && GET_CODE (oldequiv) == REG
6794 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6795 && spill_reg_store[REGNO (oldequiv)]
6796 && GET_CODE (old) == REG
6797 && (dead_or_set_p (insn,
6798 spill_reg_stored_to[REGNO (oldequiv)])
6799 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6800 old)))
6801 delete_output_reload (insn, j, REGNO (oldequiv));
6803 /* Prevent normal processing of this reload. */
6804 special = 1;
6805 /* Output a special code sequence for this case. */
6806 new_spill_reg_store[REGNO (reloadreg)]
6807 = inc_for_reload (reloadreg, oldequiv, rld[j].out,
6808 rld[j].inc);
6811 /* If we are reloading a pseudo-register that was set by the previous
6812 insn, see if we can get rid of that pseudo-register entirely
6813 by redirecting the previous insn into our reload register. */
6815 else if (optimize && GET_CODE (old) == REG
6816 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6817 && dead_or_set_p (insn, old)
6818 /* This is unsafe if some other reload
6819 uses the same reg first. */
6820 && reload_reg_free_for_value_p (REGNO (reloadreg),
6821 rld[j].opnum,
6822 rld[j].when_needed,
6823 old, rld[j].out,
6824 j, 0))
6826 rtx temp = PREV_INSN (insn);
6827 while (temp && GET_CODE (temp) == NOTE)
6828 temp = PREV_INSN (temp);
6829 if (temp
6830 && GET_CODE (temp) == INSN
6831 && GET_CODE (PATTERN (temp)) == SET
6832 && SET_DEST (PATTERN (temp)) == old
6833 /* Make sure we can access insn_operand_constraint. */
6834 && asm_noperands (PATTERN (temp)) < 0
6835 /* This is unsafe if prev insn rejects our reload reg. */
6836 && constraint_accepts_reg_p (insn_data[recog_memoized (temp)].operand[0].constraint,
6837 reloadreg)
6838 /* This is unsafe if operand occurs more than once in current
6839 insn. Perhaps some occurrences aren't reloaded. */
6840 && count_occurrences (PATTERN (insn), old) == 1
6841 /* Don't risk splitting a matching pair of operands. */
6842 && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
6844 /* Store into the reload register instead of the pseudo. */
6845 SET_DEST (PATTERN (temp)) = reloadreg;
6847 /* If the previous insn is an output reload, the source is
6848 a reload register, and its spill_reg_store entry will
6849 contain the previous destination. This is now
6850 invalid. */
6851 if (GET_CODE (SET_SRC (PATTERN (temp))) == REG
6852 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6854 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6855 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6858 /* If these are the only uses of the pseudo reg,
6859 pretend for GDB it lives in the reload reg we used. */
6860 if (REG_N_DEATHS (REGNO (old)) == 1
6861 && REG_N_SETS (REGNO (old)) == 1)
6863 reg_renumber[REGNO (old)] = REGNO (rld[j].reg_rtx);
6864 alter_reg (REGNO (old), -1);
6866 special = 1;
6870 /* We can't do that, so output an insn to load RELOADREG. */
6872 if (! special)
6874 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6875 rtx second_reload_reg = 0;
6876 enum insn_code icode;
6878 /* If we have a secondary reload, pick up the secondary register
6879 and icode, if any. If OLDEQUIV and OLD are different or
6880 if this is an in-out reload, recompute whether or not we
6881 still need a secondary register and what the icode should
6882 be. If we still need a secondary register and the class or
6883 icode is different, go back to reloading from OLD if using
6884 OLDEQUIV means that we got the wrong type of register. We
6885 cannot have different class or icode due to an in-out reload
6886 because we don't make such reloads when both the input and
6887 output need secondary reload registers. */
6889 if (rld[j].secondary_in_reload >= 0)
6891 int secondary_reload = rld[j].secondary_in_reload;
6892 rtx real_oldequiv = oldequiv;
6893 rtx real_old = old;
6894 rtx tmp;
6896 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6897 and similarly for OLD.
6898 See comments in get_secondary_reload in reload.c. */
6899 /* If it is a pseudo that cannot be replaced with its
6900 equivalent MEM, we must fall back to reload_in, which
6901 will have all the necessary substitutions registered.
6902 Likewise for a pseudo that can't be replaced with its
6903 equivalent constant.
6905 Take extra care for subregs of such pseudos. Note that
6906 we cannot use reg_equiv_mem in this case because it is
6907 not in the right mode. */
6909 tmp = oldequiv;
6910 if (GET_CODE (tmp) == SUBREG)
6911 tmp = SUBREG_REG (tmp);
6912 if (GET_CODE (tmp) == REG
6913 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6914 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6915 || reg_equiv_constant[REGNO (tmp)] != 0))
6917 if (! reg_equiv_mem[REGNO (tmp)]
6918 || num_not_at_initial_offset
6919 || GET_CODE (oldequiv) == SUBREG)
6920 real_oldequiv = rld[j].in;
6921 else
6922 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6925 tmp = old;
6926 if (GET_CODE (tmp) == SUBREG)
6927 tmp = SUBREG_REG (tmp);
6928 if (GET_CODE (tmp) == REG
6929 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6930 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6931 || reg_equiv_constant[REGNO (tmp)] != 0))
6933 if (! reg_equiv_mem[REGNO (tmp)]
6934 || num_not_at_initial_offset
6935 || GET_CODE (old) == SUBREG)
6936 real_old = rld[j].in;
6937 else
6938 real_old = reg_equiv_mem[REGNO (tmp)];
6941 second_reload_reg = rld[secondary_reload].reg_rtx;
6942 icode = rld[j].secondary_in_icode;
6944 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6945 || (rld[j].in != 0 && rld[j].out != 0))
6947 enum reg_class new_class
6948 = SECONDARY_INPUT_RELOAD_CLASS (rld[j].class,
6949 mode, real_oldequiv);
6951 if (new_class == NO_REGS)
6952 second_reload_reg = 0;
6953 else
6955 enum insn_code new_icode;
6956 enum machine_mode new_mode;
6958 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
6959 REGNO (second_reload_reg)))
6960 oldequiv = old, real_oldequiv = real_old;
6961 else
6963 new_icode = reload_in_optab[(int) mode];
6964 if (new_icode != CODE_FOR_nothing
6965 && ((insn_data[(int) new_icode].operand[0].predicate
6966 && ! ((*insn_data[(int) new_icode].operand[0].predicate)
6967 (reloadreg, mode)))
6968 || (insn_data[(int) new_icode].operand[1].predicate
6969 && ! ((*insn_data[(int) new_icode].operand[1].predicate)
6970 (real_oldequiv, mode)))))
6971 new_icode = CODE_FOR_nothing;
6973 if (new_icode == CODE_FOR_nothing)
6974 new_mode = mode;
6975 else
6976 new_mode = insn_data[(int) new_icode].operand[2].mode;
6978 if (GET_MODE (second_reload_reg) != new_mode)
6980 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6981 new_mode))
6982 oldequiv = old, real_oldequiv = real_old;
6983 else
6984 second_reload_reg
6985 = gen_rtx_REG (new_mode,
6986 REGNO (second_reload_reg));
6992 /* If we still need a secondary reload register, check
6993 to see if it is being used as a scratch or intermediate
6994 register and generate code appropriately. If we need
6995 a scratch register, use REAL_OLDEQUIV since the form of
6996 the insn may depend on the actual address if it is
6997 a MEM. */
6999 if (second_reload_reg)
7001 if (icode != CODE_FOR_nothing)
7003 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
7004 second_reload_reg));
7005 special = 1;
7007 else
7009 /* See if we need a scratch register to load the
7010 intermediate register (a tertiary reload). */
7011 enum insn_code tertiary_icode
7012 = rld[secondary_reload].secondary_in_icode;
7014 if (tertiary_icode != CODE_FOR_nothing)
7016 rtx third_reload_reg
7017 = rld[rld[secondary_reload].secondary_in_reload].reg_rtx;
7019 emit_insn ((GEN_FCN (tertiary_icode)
7020 (second_reload_reg, real_oldequiv,
7021 third_reload_reg)));
7023 else
7024 gen_reload (second_reload_reg, real_oldequiv,
7025 rld[j].opnum,
7026 rld[j].when_needed);
7028 oldequiv = second_reload_reg;
7032 #endif
7034 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
7036 rtx real_oldequiv = oldequiv;
7038 if ((GET_CODE (oldequiv) == REG
7039 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
7040 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
7041 || reg_equiv_constant[REGNO (oldequiv)] != 0))
7042 || (GET_CODE (oldequiv) == SUBREG
7043 && GET_CODE (SUBREG_REG (oldequiv)) == REG
7044 && (REGNO (SUBREG_REG (oldequiv))
7045 >= FIRST_PSEUDO_REGISTER)
7046 && ((reg_equiv_memory_loc
7047 [REGNO (SUBREG_REG (oldequiv))] != 0)
7048 || (reg_equiv_constant
7049 [REGNO (SUBREG_REG (oldequiv))] != 0))))
7050 real_oldequiv = rld[j].in;
7051 gen_reload (reloadreg, real_oldequiv, rld[j].opnum,
7052 rld[j].when_needed);
7057 this_reload_insn = get_last_insn ();
7058 /* End this sequence. */
7059 *where = get_insns ();
7060 end_sequence ();
7062 /* Update reload_override_in so that delete_address_reloads_1
7063 can see the actual register usage. */
7064 if (oldequiv_reg)
7065 reload_override_in[j] = oldequiv;
7068 /* When inheriting a wider reload, we have a MEM in rld[j].in,
7069 e.g. inheriting a SImode output reload for
7070 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
7071 if (optimize && reload_inherited[j] && rld[j].in
7072 && GET_CODE (rld[j].in) == MEM
7073 && GET_CODE (rld[j].in_reg) == MEM
7074 && reload_spill_index[j] >= 0
7075 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
7077 expect_occurrences
7078 = count_occurrences (PATTERN (insn), rld[j].in) == 1 ? 0 : -1;
7079 rld[j].in
7080 = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
7083 /* If we are reloading a register that was recently stored in with an
7084 output-reload, see if we can prove there was
7085 actually no need to store the old value in it. */
7087 if (optimize
7088 && (reload_inherited[j] || reload_override_in[j])
7089 && rld[j].reg_rtx
7090 && GET_CODE (rld[j].reg_rtx) == REG
7091 && spill_reg_store[REGNO (rld[j].reg_rtx)] != 0
7092 #if 0
7093 /* There doesn't seem to be any reason to restrict this to pseudos
7094 and doing so loses in the case where we are copying from a
7095 register of the wrong class. */
7096 && (REGNO (spill_reg_stored_to[REGNO (rld[j].reg_rtx)])
7097 >= FIRST_PSEUDO_REGISTER)
7098 #endif
7099 /* The insn might have already some references to stackslots
7100 replaced by MEMs, while reload_out_reg still names the
7101 original pseudo. */
7102 && (dead_or_set_p (insn,
7103 spill_reg_stored_to[REGNO (rld[j].reg_rtx)])
7104 || rtx_equal_p (spill_reg_stored_to[REGNO (rld[j].reg_rtx)],
7105 rld[j].out_reg)))
7106 delete_output_reload (insn, j, REGNO (rld[j].reg_rtx));
7108 /* Input-reloading is done. Now do output-reloading,
7109 storing the value from the reload-register after the main insn
7110 if rld[j].out is nonzero.
7112 ??? At some point we need to support handling output reloads of
7113 JUMP_INSNs or insns that set cc0. */
7115 /* If this is an output reload that stores something that is
7116 not loaded in this same reload, see if we can eliminate a previous
7117 store. */
7119 rtx pseudo = rld[j].out_reg;
7121 if (pseudo
7122 && GET_CODE (pseudo) == REG
7123 && ! rtx_equal_p (rld[j].in_reg, pseudo)
7124 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
7125 && reg_last_reload_reg[REGNO (pseudo)])
7127 int pseudo_no = REGNO (pseudo);
7128 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
7130 /* We don't need to test full validity of last_regno for
7131 inherit here; we only want to know if the store actually
7132 matches the pseudo. */
7133 if (reg_reloaded_contents[last_regno] == pseudo_no
7134 && spill_reg_store[last_regno]
7135 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
7136 delete_output_reload (insn, j, last_regno);
7140 old = rld[j].out_reg;
7141 if (old != 0
7142 && rld[j].reg_rtx != old
7143 && rld[j].reg_rtx != 0)
7145 register rtx reloadreg = rld[j].reg_rtx;
7146 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
7147 register rtx second_reloadreg = 0;
7148 #endif
7149 rtx note, p;
7150 enum machine_mode mode;
7151 int special = 0;
7153 /* An output operand that dies right away does need a reload,
7154 but need not be copied from it. Show the new location in the
7155 REG_UNUSED note. */
7156 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
7157 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
7159 XEXP (note, 0) = rld[j].reg_rtx;
7160 continue;
7162 /* Likewise for a SUBREG of an operand that dies. */
7163 else if (GET_CODE (old) == SUBREG
7164 && GET_CODE (SUBREG_REG (old)) == REG
7165 && 0 != (note = find_reg_note (insn, REG_UNUSED,
7166 SUBREG_REG (old))))
7168 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
7169 rld[j].reg_rtx);
7170 continue;
7172 else if (GET_CODE (old) == SCRATCH)
7173 /* If we aren't optimizing, there won't be a REG_UNUSED note,
7174 but we don't want to make an output reload. */
7175 continue;
7177 #if 0
7178 /* Strip off of OLD any size-increasing SUBREGs such as
7179 (SUBREG:SI foo:QI 0). */
7181 while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
7182 && (GET_MODE_SIZE (GET_MODE (old))
7183 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
7184 old = SUBREG_REG (old);
7185 #endif
7187 /* If is a JUMP_INSN, we can't support output reloads yet. */
7188 if (GET_CODE (insn) == JUMP_INSN)
7189 abort ();
7191 if (rld[j].when_needed == RELOAD_OTHER)
7192 start_sequence ();
7193 else
7194 push_to_sequence (output_reload_insns[rld[j].opnum]);
7196 old = rld[j].out;
7198 /* Determine the mode to reload in.
7199 See comments above (for input reloading). */
7201 mode = GET_MODE (old);
7202 if (mode == VOIDmode)
7204 /* VOIDmode should never happen for an output. */
7205 if (asm_noperands (PATTERN (insn)) < 0)
7206 /* It's the compiler's fault. */
7207 fatal_insn ("VOIDmode on an output", insn);
7208 error_for_asm (insn, "output operand is constant in `asm'");
7209 /* Prevent crash--use something we know is valid. */
7210 mode = word_mode;
7211 old = gen_rtx_REG (mode, REGNO (reloadreg));
7214 if (GET_MODE (reloadreg) != mode)
7215 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
7217 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
7219 /* If we need two reload regs, set RELOADREG to the intermediate
7220 one, since it will be stored into OLD. We might need a secondary
7221 register only for an input reload, so check again here. */
7223 if (rld[j].secondary_out_reload >= 0)
7225 rtx real_old = old;
7227 if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
7228 && reg_equiv_mem[REGNO (old)] != 0)
7229 real_old = reg_equiv_mem[REGNO (old)];
7231 if((SECONDARY_OUTPUT_RELOAD_CLASS (rld[j].class,
7232 mode, real_old)
7233 != NO_REGS))
7235 second_reloadreg = reloadreg;
7236 reloadreg = rld[rld[j].secondary_out_reload].reg_rtx;
7238 /* See if RELOADREG is to be used as a scratch register
7239 or as an intermediate register. */
7240 if (rld[j].secondary_out_icode != CODE_FOR_nothing)
7242 emit_insn ((GEN_FCN (rld[j].secondary_out_icode)
7243 (real_old, second_reloadreg, reloadreg)));
7244 special = 1;
7246 else
7248 /* See if we need both a scratch and intermediate reload
7249 register. */
7251 int secondary_reload = rld[j].secondary_out_reload;
7252 enum insn_code tertiary_icode
7253 = rld[secondary_reload].secondary_out_icode;
7255 if (GET_MODE (reloadreg) != mode)
7256 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
7258 if (tertiary_icode != CODE_FOR_nothing)
7260 rtx third_reloadreg
7261 = rld[rld[secondary_reload].secondary_out_reload].reg_rtx;
7262 rtx tem;
7264 /* Copy primary reload reg to secondary reload reg.
7265 (Note that these have been swapped above, then
7266 secondary reload reg to OLD using our insn. */
7268 /* If REAL_OLD is a paradoxical SUBREG, remove it
7269 and try to put the opposite SUBREG on
7270 RELOADREG. */
7271 if (GET_CODE (real_old) == SUBREG
7272 && (GET_MODE_SIZE (GET_MODE (real_old))
7273 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
7274 && 0 != (tem = gen_lowpart_common
7275 (GET_MODE (SUBREG_REG (real_old)),
7276 reloadreg)))
7277 real_old = SUBREG_REG (real_old), reloadreg = tem;
7279 gen_reload (reloadreg, second_reloadreg,
7280 rld[j].opnum, rld[j].when_needed);
7281 emit_insn ((GEN_FCN (tertiary_icode)
7282 (real_old, reloadreg, third_reloadreg)));
7283 special = 1;
7286 else
7287 /* Copy between the reload regs here and then to
7288 OUT later. */
7290 gen_reload (reloadreg, second_reloadreg,
7291 rld[j].opnum, rld[j].when_needed);
7295 #endif
7297 /* Output the last reload insn. */
7298 if (! special)
7300 rtx set;
7302 /* Don't output the last reload if OLD is not the dest of
7303 INSN and is in the src and is clobbered by INSN. */
7304 if (! flag_expensive_optimizations
7305 || GET_CODE (old) != REG
7306 || !(set = single_set (insn))
7307 || rtx_equal_p (old, SET_DEST (set))
7308 || !reg_mentioned_p (old, SET_SRC (set))
7309 || !regno_clobbered_p (REGNO (old), insn))
7310 gen_reload (old, reloadreg, rld[j].opnum,
7311 rld[j].when_needed);
7314 /* Look at all insns we emitted, just to be safe. */
7315 for (p = get_insns (); p; p = NEXT_INSN (p))
7316 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
7318 rtx pat = PATTERN (p);
7320 /* If this output reload doesn't come from a spill reg,
7321 clear any memory of reloaded copies of the pseudo reg.
7322 If this output reload comes from a spill reg,
7323 reg_has_output_reload will make this do nothing. */
7324 note_stores (pat, forget_old_reloads_1, NULL);
7326 if (reg_mentioned_p (rld[j].reg_rtx, pat))
7328 rtx set = single_set (insn);
7329 if (reload_spill_index[j] < 0
7330 && set
7331 && SET_SRC (set) == rld[j].reg_rtx)
7333 int src = REGNO (SET_SRC (set));
7335 reload_spill_index[j] = src;
7336 SET_HARD_REG_BIT (reg_is_output_reload, src);
7337 if (find_regno_note (insn, REG_DEAD, src))
7338 SET_HARD_REG_BIT (reg_reloaded_died, src);
7340 if (REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
7342 int s = rld[j].secondary_out_reload;
7343 set = single_set (p);
7344 /* If this reload copies only to the secondary reload
7345 register, the secondary reload does the actual
7346 store. */
7347 if (s >= 0 && set == NULL_RTX)
7348 ; /* We can't tell what function the secondary reload
7349 has and where the actual store to the pseudo is
7350 made; leave new_spill_reg_store alone. */
7351 else if (s >= 0
7352 && SET_SRC (set) == rld[j].reg_rtx
7353 && SET_DEST (set) == rld[s].reg_rtx)
7355 /* Usually the next instruction will be the
7356 secondary reload insn; if we can confirm
7357 that it is, setting new_spill_reg_store to
7358 that insn will allow an extra optimization. */
7359 rtx s_reg = rld[s].reg_rtx;
7360 rtx next = NEXT_INSN (p);
7361 rld[s].out = rld[j].out;
7362 rld[s].out_reg = rld[j].out_reg;
7363 set = single_set (next);
7364 if (set && SET_SRC (set) == s_reg
7365 && ! new_spill_reg_store[REGNO (s_reg)])
7367 SET_HARD_REG_BIT (reg_is_output_reload,
7368 REGNO (s_reg));
7369 new_spill_reg_store[REGNO (s_reg)] = next;
7372 else
7373 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = p;
7378 if (rld[j].when_needed == RELOAD_OTHER)
7380 emit_insns (other_output_reload_insns[rld[j].opnum]);
7381 other_output_reload_insns[rld[j].opnum] = get_insns ();
7383 else
7384 output_reload_insns[rld[j].opnum] = get_insns ();
7386 end_sequence ();
7390 /* Now write all the insns we made for reloads in the order expected by
7391 the allocation functions. Prior to the insn being reloaded, we write
7392 the following reloads:
7394 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7396 RELOAD_OTHER reloads.
7398 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7399 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7400 RELOAD_FOR_INPUT reload for the operand.
7402 RELOAD_FOR_OPADDR_ADDRS reloads.
7404 RELOAD_FOR_OPERAND_ADDRESS reloads.
7406 After the insn being reloaded, we write the following:
7408 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7409 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7410 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7411 reloads for the operand. The RELOAD_OTHER output reloads are
7412 output in descending order by reload number. */
7414 emit_insns_before (other_input_address_reload_insns, insn);
7415 emit_insns_before (other_input_reload_insns, insn);
7417 for (j = 0; j < reload_n_operands; j++)
7419 emit_insns_before (inpaddr_address_reload_insns[j], insn);
7420 emit_insns_before (input_address_reload_insns[j], insn);
7421 emit_insns_before (input_reload_insns[j], insn);
7424 emit_insns_before (other_operand_reload_insns, insn);
7425 emit_insns_before (operand_reload_insns, insn);
7427 for (j = 0; j < reload_n_operands; j++)
7429 emit_insns_before (outaddr_address_reload_insns[j], following_insn);
7430 emit_insns_before (output_address_reload_insns[j], following_insn);
7431 emit_insns_before (output_reload_insns[j], following_insn);
7432 emit_insns_before (other_output_reload_insns[j], following_insn);
7435 /* Keep basic block info up to date. */
7436 if (n_basic_blocks)
7438 if (BLOCK_HEAD (chain->block) == insn)
7439 BLOCK_HEAD (chain->block) = NEXT_INSN (before_insn);
7440 if (BLOCK_END (chain->block) == insn)
7441 BLOCK_END (chain->block) = PREV_INSN (following_insn);
7444 /* For all the spill regs newly reloaded in this instruction,
7445 record what they were reloaded from, so subsequent instructions
7446 can inherit the reloads.
7448 Update spill_reg_store for the reloads of this insn.
7449 Copy the elements that were updated in the loop above. */
7451 for (j = 0; j < n_reloads; j++)
7453 register int r = reload_order[j];
7454 register int i = reload_spill_index[r];
7456 /* If this is a non-inherited input reload from a pseudo, we must
7457 clear any memory of a previous store to the same pseudo. Only do
7458 something if there will not be an output reload for the pseudo
7459 being reloaded. */
7460 if (rld[r].in_reg != 0
7461 && ! (reload_inherited[r] || reload_override_in[r]))
7463 rtx reg = rld[r].in_reg;
7465 if (GET_CODE (reg) == SUBREG)
7466 reg = SUBREG_REG (reg);
7468 if (GET_CODE (reg) == REG
7469 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7470 && ! reg_has_output_reload[REGNO (reg)])
7472 int nregno = REGNO (reg);
7474 if (reg_last_reload_reg[nregno])
7476 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7478 if (reg_reloaded_contents[last_regno] == nregno)
7479 spill_reg_store[last_regno] = 0;
7484 /* I is nonneg if this reload used a register.
7485 If rld[r].reg_rtx is 0, this is an optional reload
7486 that we opted to ignore. */
7488 if (i >= 0 && rld[r].reg_rtx != 0)
7490 int nr
7491 = HARD_REGNO_NREGS (i, GET_MODE (rld[r].reg_rtx));
7492 int k;
7493 int part_reaches_end = 0;
7494 int all_reaches_end = 1;
7496 /* For a multi register reload, we need to check if all or part
7497 of the value lives to the end. */
7498 for (k = 0; k < nr; k++)
7500 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7501 rld[r].when_needed))
7502 part_reaches_end = 1;
7503 else
7504 all_reaches_end = 0;
7507 /* Ignore reloads that don't reach the end of the insn in
7508 entirety. */
7509 if (all_reaches_end)
7511 /* First, clear out memory of what used to be in this spill reg.
7512 If consecutive registers are used, clear them all. */
7514 for (k = 0; k < nr; k++)
7515 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7517 /* Maybe the spill reg contains a copy of reload_out. */
7518 if (rld[r].out != 0
7519 && (GET_CODE (rld[r].out) == REG
7520 #ifdef AUTO_INC_DEC
7521 || ! rld[r].out_reg
7522 #endif
7523 || GET_CODE (rld[r].out_reg) == REG))
7525 rtx out = (GET_CODE (rld[r].out) == REG
7526 ? rld[r].out
7527 : rld[r].out_reg
7528 ? rld[r].out_reg
7529 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7530 register int nregno = REGNO (out);
7531 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7532 : HARD_REGNO_NREGS (nregno,
7533 GET_MODE (rld[r].reg_rtx)));
7535 spill_reg_store[i] = new_spill_reg_store[i];
7536 spill_reg_stored_to[i] = out;
7537 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7539 /* If NREGNO is a hard register, it may occupy more than
7540 one register. If it does, say what is in the
7541 rest of the registers assuming that both registers
7542 agree on how many words the object takes. If not,
7543 invalidate the subsequent registers. */
7545 if (nregno < FIRST_PSEUDO_REGISTER)
7546 for (k = 1; k < nnr; k++)
7547 reg_last_reload_reg[nregno + k]
7548 = (nr == nnr
7549 ? gen_rtx_REG (reg_raw_mode[REGNO (rld[r].reg_rtx) + k],
7550 REGNO (rld[r].reg_rtx) + k)
7551 : 0);
7553 /* Now do the inverse operation. */
7554 for (k = 0; k < nr; k++)
7556 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7557 reg_reloaded_contents[i + k]
7558 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7559 ? nregno
7560 : nregno + k);
7561 reg_reloaded_insn[i + k] = insn;
7562 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7566 /* Maybe the spill reg contains a copy of reload_in. Only do
7567 something if there will not be an output reload for
7568 the register being reloaded. */
7569 else if (rld[r].out_reg == 0
7570 && rld[r].in != 0
7571 && ((GET_CODE (rld[r].in) == REG
7572 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7573 && ! reg_has_output_reload[REGNO (rld[r].in)])
7574 || (GET_CODE (rld[r].in_reg) == REG
7575 && ! reg_has_output_reload[REGNO (rld[r].in_reg)]))
7576 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7578 register int nregno;
7579 int nnr;
7581 if (GET_CODE (rld[r].in) == REG
7582 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7583 nregno = REGNO (rld[r].in);
7584 else if (GET_CODE (rld[r].in_reg) == REG)
7585 nregno = REGNO (rld[r].in_reg);
7586 else
7587 nregno = REGNO (XEXP (rld[r].in_reg, 0));
7589 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7590 : HARD_REGNO_NREGS (nregno,
7591 GET_MODE (rld[r].reg_rtx)));
7593 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7595 if (nregno < FIRST_PSEUDO_REGISTER)
7596 for (k = 1; k < nnr; k++)
7597 reg_last_reload_reg[nregno + k]
7598 = (nr == nnr
7599 ? gen_rtx_REG (reg_raw_mode[REGNO (rld[r].reg_rtx) + k],
7600 REGNO (rld[r].reg_rtx) + k)
7601 : 0);
7603 /* Unless we inherited this reload, show we haven't
7604 recently done a store.
7605 Previous stores of inherited auto_inc expressions
7606 also have to be discarded. */
7607 if (! reload_inherited[r]
7608 || (rld[r].out && ! rld[r].out_reg))
7609 spill_reg_store[i] = 0;
7611 for (k = 0; k < nr; k++)
7613 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7614 reg_reloaded_contents[i + k]
7615 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7616 ? nregno
7617 : nregno + k);
7618 reg_reloaded_insn[i + k] = insn;
7619 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7624 /* However, if part of the reload reaches the end, then we must
7625 invalidate the old info for the part that survives to the end. */
7626 else if (part_reaches_end)
7628 for (k = 0; k < nr; k++)
7629 if (reload_reg_reaches_end_p (i + k,
7630 rld[r].opnum,
7631 rld[r].when_needed))
7632 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7636 /* The following if-statement was #if 0'd in 1.34 (or before...).
7637 It's reenabled in 1.35 because supposedly nothing else
7638 deals with this problem. */
7640 /* If a register gets output-reloaded from a non-spill register,
7641 that invalidates any previous reloaded copy of it.
7642 But forget_old_reloads_1 won't get to see it, because
7643 it thinks only about the original insn. So invalidate it here. */
7644 if (i < 0 && rld[r].out != 0
7645 && (GET_CODE (rld[r].out) == REG
7646 || (GET_CODE (rld[r].out) == MEM
7647 && GET_CODE (rld[r].out_reg) == REG)))
7649 rtx out = (GET_CODE (rld[r].out) == REG
7650 ? rld[r].out : rld[r].out_reg);
7651 register int nregno = REGNO (out);
7652 if (nregno >= FIRST_PSEUDO_REGISTER)
7654 rtx src_reg, store_insn = NULL_RTX;
7656 reg_last_reload_reg[nregno] = 0;
7658 /* If we can find a hard register that is stored, record
7659 the storing insn so that we may delete this insn with
7660 delete_output_reload. */
7661 src_reg = rld[r].reg_rtx;
7663 /* If this is an optional reload, try to find the source reg
7664 from an input reload. */
7665 if (! src_reg)
7667 rtx set = single_set (insn);
7668 if (set && SET_DEST (set) == rld[r].out)
7670 int k;
7672 src_reg = SET_SRC (set);
7673 store_insn = insn;
7674 for (k = 0; k < n_reloads; k++)
7676 if (rld[k].in == src_reg)
7678 src_reg = rld[k].reg_rtx;
7679 break;
7684 else
7685 store_insn = new_spill_reg_store[REGNO (src_reg)];
7686 if (src_reg && GET_CODE (src_reg) == REG
7687 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7689 int src_regno = REGNO (src_reg);
7690 int nr = HARD_REGNO_NREGS (src_regno, rld[r].mode);
7691 /* The place where to find a death note varies with
7692 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7693 necessarily checked exactly in the code that moves
7694 notes, so just check both locations. */
7695 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7696 if (! note)
7697 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7698 while (nr-- > 0)
7700 spill_reg_store[src_regno + nr] = store_insn;
7701 spill_reg_stored_to[src_regno + nr] = out;
7702 reg_reloaded_contents[src_regno + nr] = nregno;
7703 reg_reloaded_insn[src_regno + nr] = store_insn;
7704 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7705 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7706 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7707 if (note)
7708 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7709 else
7710 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7712 reg_last_reload_reg[nregno] = src_reg;
7715 else
7717 int num_regs = HARD_REGNO_NREGS (nregno,GET_MODE (rld[r].out));
7719 while (num_regs-- > 0)
7720 reg_last_reload_reg[nregno + num_regs] = 0;
7724 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7727 /* Emit code to perform a reload from IN (which may be a reload register) to
7728 OUT (which may also be a reload register). IN or OUT is from operand
7729 OPNUM with reload type TYPE.
7731 Returns first insn emitted. */
7734 gen_reload (out, in, opnum, type)
7735 rtx out;
7736 rtx in;
7737 int opnum;
7738 enum reload_type type;
7740 rtx last = get_last_insn ();
7741 rtx tem;
7743 /* If IN is a paradoxical SUBREG, remove it and try to put the
7744 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7745 if (GET_CODE (in) == SUBREG
7746 && (GET_MODE_SIZE (GET_MODE (in))
7747 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7748 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7749 in = SUBREG_REG (in), out = tem;
7750 else if (GET_CODE (out) == SUBREG
7751 && (GET_MODE_SIZE (GET_MODE (out))
7752 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7753 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7754 out = SUBREG_REG (out), in = tem;
7756 /* How to do this reload can get quite tricky. Normally, we are being
7757 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7758 register that didn't get a hard register. In that case we can just
7759 call emit_move_insn.
7761 We can also be asked to reload a PLUS that adds a register or a MEM to
7762 another register, constant or MEM. This can occur during frame pointer
7763 elimination and while reloading addresses. This case is handled by
7764 trying to emit a single insn to perform the add. If it is not valid,
7765 we use a two insn sequence.
7767 Finally, we could be called to handle an 'o' constraint by putting
7768 an address into a register. In that case, we first try to do this
7769 with a named pattern of "reload_load_address". If no such pattern
7770 exists, we just emit a SET insn and hope for the best (it will normally
7771 be valid on machines that use 'o').
7773 This entire process is made complex because reload will never
7774 process the insns we generate here and so we must ensure that
7775 they will fit their constraints and also by the fact that parts of
7776 IN might be being reloaded separately and replaced with spill registers.
7777 Because of this, we are, in some sense, just guessing the right approach
7778 here. The one listed above seems to work.
7780 ??? At some point, this whole thing needs to be rethought. */
7782 if (GET_CODE (in) == PLUS
7783 && (GET_CODE (XEXP (in, 0)) == REG
7784 || GET_CODE (XEXP (in, 0)) == SUBREG
7785 || GET_CODE (XEXP (in, 0)) == MEM)
7786 && (GET_CODE (XEXP (in, 1)) == REG
7787 || GET_CODE (XEXP (in, 1)) == SUBREG
7788 || CONSTANT_P (XEXP (in, 1))
7789 || GET_CODE (XEXP (in, 1)) == MEM))
7791 /* We need to compute the sum of a register or a MEM and another
7792 register, constant, or MEM, and put it into the reload
7793 register. The best possible way of doing this is if the machine
7794 has a three-operand ADD insn that accepts the required operands.
7796 The simplest approach is to try to generate such an insn and see if it
7797 is recognized and matches its constraints. If so, it can be used.
7799 It might be better not to actually emit the insn unless it is valid,
7800 but we need to pass the insn as an operand to `recog' and
7801 `extract_insn' and it is simpler to emit and then delete the insn if
7802 not valid than to dummy things up. */
7804 rtx op0, op1, tem, insn;
7805 int code;
7807 op0 = find_replacement (&XEXP (in, 0));
7808 op1 = find_replacement (&XEXP (in, 1));
7810 /* Since constraint checking is strict, commutativity won't be
7811 checked, so we need to do that here to avoid spurious failure
7812 if the add instruction is two-address and the second operand
7813 of the add is the same as the reload reg, which is frequently
7814 the case. If the insn would be A = B + A, rearrange it so
7815 it will be A = A + B as constrain_operands expects. */
7817 if (GET_CODE (XEXP (in, 1)) == REG
7818 && REGNO (out) == REGNO (XEXP (in, 1)))
7819 tem = op0, op0 = op1, op1 = tem;
7821 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7822 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7824 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
7825 code = recog_memoized (insn);
7827 if (code >= 0)
7829 extract_insn (insn);
7830 /* We want constrain operands to treat this insn strictly in
7831 its validity determination, i.e., the way it would after reload
7832 has completed. */
7833 if (constrain_operands (1))
7834 return insn;
7837 delete_insns_since (last);
7839 /* If that failed, we must use a conservative two-insn sequence.
7841 Use a move to copy one operand into the reload register. Prefer
7842 to reload a constant, MEM or pseudo since the move patterns can
7843 handle an arbitrary operand. If OP1 is not a constant, MEM or
7844 pseudo and OP1 is not a valid operand for an add instruction, then
7845 reload OP1.
7847 After reloading one of the operands into the reload register, add
7848 the reload register to the output register.
7850 If there is another way to do this for a specific machine, a
7851 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7852 we emit below. */
7854 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7856 if (CONSTANT_P (op1) || GET_CODE (op1) == MEM || GET_CODE (op1) == SUBREG
7857 || (GET_CODE (op1) == REG
7858 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7859 || (code != CODE_FOR_nothing
7860 && ! ((*insn_data[code].operand[2].predicate)
7861 (op1, insn_data[code].operand[2].mode))))
7862 tem = op0, op0 = op1, op1 = tem;
7864 gen_reload (out, op0, opnum, type);
7866 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7867 This fixes a problem on the 32K where the stack pointer cannot
7868 be used as an operand of an add insn. */
7870 if (rtx_equal_p (op0, op1))
7871 op1 = out;
7873 insn = emit_insn (gen_add2_insn (out, op1));
7875 /* If that failed, copy the address register to the reload register.
7876 Then add the constant to the reload register. */
7878 code = recog_memoized (insn);
7880 if (code >= 0)
7882 extract_insn (insn);
7883 /* We want constrain operands to treat this insn strictly in
7884 its validity determination, i.e., the way it would after reload
7885 has completed. */
7886 if (constrain_operands (1))
7888 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7889 REG_NOTES (insn)
7890 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7891 return insn;
7895 delete_insns_since (last);
7897 gen_reload (out, op1, opnum, type);
7898 insn = emit_insn (gen_add2_insn (out, op0));
7899 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7902 #ifdef SECONDARY_MEMORY_NEEDED
7903 /* If we need a memory location to do the move, do it that way. */
7904 else if (GET_CODE (in) == REG && REGNO (in) < FIRST_PSEUDO_REGISTER
7905 && GET_CODE (out) == REG && REGNO (out) < FIRST_PSEUDO_REGISTER
7906 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (in)),
7907 REGNO_REG_CLASS (REGNO (out)),
7908 GET_MODE (out)))
7910 /* Get the memory to use and rewrite both registers to its mode. */
7911 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7913 if (GET_MODE (loc) != GET_MODE (out))
7914 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7916 if (GET_MODE (loc) != GET_MODE (in))
7917 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7919 gen_reload (loc, in, opnum, type);
7920 gen_reload (out, loc, opnum, type);
7922 #endif
7924 /* If IN is a simple operand, use gen_move_insn. */
7925 else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
7926 emit_insn (gen_move_insn (out, in));
7928 #ifdef HAVE_reload_load_address
7929 else if (HAVE_reload_load_address)
7930 emit_insn (gen_reload_load_address (out, in));
7931 #endif
7933 /* Otherwise, just write (set OUT IN) and hope for the best. */
7934 else
7935 emit_insn (gen_rtx_SET (VOIDmode, out, in));
7937 /* Return the first insn emitted.
7938 We can not just return get_last_insn, because there may have
7939 been multiple instructions emitted. Also note that gen_move_insn may
7940 emit more than one insn itself, so we can not assume that there is one
7941 insn emitted per emit_insn_before call. */
7943 return last ? NEXT_INSN (last) : get_insns ();
7946 /* Delete a previously made output-reload
7947 whose result we now believe is not needed.
7948 First we double-check.
7950 INSN is the insn now being processed.
7951 LAST_RELOAD_REG is the hard register number for which we want to delete
7952 the last output reload.
7953 J is the reload-number that originally used REG. The caller has made
7954 certain that reload J doesn't use REG any longer for input. */
7956 static void
7957 delete_output_reload (insn, j, last_reload_reg)
7958 rtx insn;
7959 int j;
7960 int last_reload_reg;
7962 rtx output_reload_insn = spill_reg_store[last_reload_reg];
7963 rtx reg = spill_reg_stored_to[last_reload_reg];
7964 int k;
7965 int n_occurrences;
7966 int n_inherited = 0;
7967 register rtx i1;
7968 rtx substed;
7970 /* Get the raw pseudo-register referred to. */
7972 while (GET_CODE (reg) == SUBREG)
7973 reg = SUBREG_REG (reg);
7974 substed = reg_equiv_memory_loc[REGNO (reg)];
7976 /* This is unsafe if the operand occurs more often in the current
7977 insn than it is inherited. */
7978 for (k = n_reloads - 1; k >= 0; k--)
7980 rtx reg2 = rld[k].in;
7981 if (! reg2)
7982 continue;
7983 if (GET_CODE (reg2) == MEM || reload_override_in[k])
7984 reg2 = rld[k].in_reg;
7985 #ifdef AUTO_INC_DEC
7986 if (rld[k].out && ! rld[k].out_reg)
7987 reg2 = XEXP (rld[k].in_reg, 0);
7988 #endif
7989 while (GET_CODE (reg2) == SUBREG)
7990 reg2 = SUBREG_REG (reg2);
7991 if (rtx_equal_p (reg2, reg))
7993 if (reload_inherited[k] || reload_override_in[k] || k == j)
7995 n_inherited++;
7996 reg2 = rld[k].out_reg;
7997 if (! reg2)
7998 continue;
7999 while (GET_CODE (reg2) == SUBREG)
8000 reg2 = XEXP (reg2, 0);
8001 if (rtx_equal_p (reg2, reg))
8002 n_inherited++;
8004 else
8005 return;
8008 n_occurrences = count_occurrences (PATTERN (insn), reg);
8009 if (substed)
8010 n_occurrences += count_occurrences (PATTERN (insn), substed);
8011 if (n_occurrences > n_inherited)
8012 return;
8014 /* If the pseudo-reg we are reloading is no longer referenced
8015 anywhere between the store into it and here,
8016 and no jumps or labels intervene, then the value can get
8017 here through the reload reg alone.
8018 Otherwise, give up--return. */
8019 for (i1 = NEXT_INSN (output_reload_insn);
8020 i1 != insn; i1 = NEXT_INSN (i1))
8022 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
8023 return;
8024 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
8025 && reg_mentioned_p (reg, PATTERN (i1)))
8027 /* If this is USE in front of INSN, we only have to check that
8028 there are no more references than accounted for by inheritance. */
8029 while (GET_CODE (i1) == INSN && GET_CODE (PATTERN (i1)) == USE)
8031 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
8032 i1 = NEXT_INSN (i1);
8034 if (n_occurrences <= n_inherited && i1 == insn)
8035 break;
8036 return;
8040 /* The caller has already checked that REG dies or is set in INSN.
8041 It has also checked that we are optimizing, and thus some inaccurancies
8042 in the debugging information are acceptable.
8043 So we could just delete output_reload_insn.
8044 But in some cases we can improve the debugging information without
8045 sacrificing optimization - maybe even improving the code:
8046 See if the pseudo reg has been completely replaced
8047 with reload regs. If so, delete the store insn
8048 and forget we had a stack slot for the pseudo. */
8049 if (rld[j].out != rld[j].in
8050 && REG_N_DEATHS (REGNO (reg)) == 1
8051 && REG_N_SETS (REGNO (reg)) == 1
8052 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
8053 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
8055 rtx i2;
8057 /* We know that it was used only between here
8058 and the beginning of the current basic block.
8059 (We also know that the last use before INSN was
8060 the output reload we are thinking of deleting, but never mind that.)
8061 Search that range; see if any ref remains. */
8062 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8064 rtx set = single_set (i2);
8066 /* Uses which just store in the pseudo don't count,
8067 since if they are the only uses, they are dead. */
8068 if (set != 0 && SET_DEST (set) == reg)
8069 continue;
8070 if (GET_CODE (i2) == CODE_LABEL
8071 || GET_CODE (i2) == JUMP_INSN)
8072 break;
8073 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
8074 && reg_mentioned_p (reg, PATTERN (i2)))
8076 /* Some other ref remains; just delete the output reload we
8077 know to be dead. */
8078 delete_address_reloads (output_reload_insn, insn);
8079 PUT_CODE (output_reload_insn, NOTE);
8080 NOTE_SOURCE_FILE (output_reload_insn) = 0;
8081 NOTE_LINE_NUMBER (output_reload_insn) = NOTE_INSN_DELETED;
8082 return;
8086 /* Delete the now-dead stores into this pseudo. */
8087 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8089 rtx set = single_set (i2);
8091 if (set != 0 && SET_DEST (set) == reg)
8093 delete_address_reloads (i2, insn);
8094 /* This might be a basic block head,
8095 thus don't use delete_insn. */
8096 PUT_CODE (i2, NOTE);
8097 NOTE_SOURCE_FILE (i2) = 0;
8098 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
8100 if (GET_CODE (i2) == CODE_LABEL
8101 || GET_CODE (i2) == JUMP_INSN)
8102 break;
8105 /* For the debugging info,
8106 say the pseudo lives in this reload reg. */
8107 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
8108 alter_reg (REGNO (reg), -1);
8110 delete_address_reloads (output_reload_insn, insn);
8111 PUT_CODE (output_reload_insn, NOTE);
8112 NOTE_SOURCE_FILE (output_reload_insn) = 0;
8113 NOTE_LINE_NUMBER (output_reload_insn) = NOTE_INSN_DELETED;
8117 /* We are going to delete DEAD_INSN. Recursively delete loads of
8118 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
8119 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
8120 static void
8121 delete_address_reloads (dead_insn, current_insn)
8122 rtx dead_insn, current_insn;
8124 rtx set = single_set (dead_insn);
8125 rtx set2, dst, prev, next;
8126 if (set)
8128 rtx dst = SET_DEST (set);
8129 if (GET_CODE (dst) == MEM)
8130 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
8132 /* If we deleted the store from a reloaded post_{in,de}c expression,
8133 we can delete the matching adds. */
8134 prev = PREV_INSN (dead_insn);
8135 next = NEXT_INSN (dead_insn);
8136 if (! prev || ! next)
8137 return;
8138 set = single_set (next);
8139 set2 = single_set (prev);
8140 if (! set || ! set2
8141 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
8142 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
8143 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
8144 return;
8145 dst = SET_DEST (set);
8146 if (! rtx_equal_p (dst, SET_DEST (set2))
8147 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
8148 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
8149 || (INTVAL (XEXP (SET_SRC (set), 1))
8150 != - INTVAL (XEXP (SET_SRC (set2), 1))))
8151 return;
8152 delete_insn (prev);
8153 delete_insn (next);
8156 /* Subfunction of delete_address_reloads: process registers found in X. */
8157 static void
8158 delete_address_reloads_1 (dead_insn, x, current_insn)
8159 rtx dead_insn, x, current_insn;
8161 rtx prev, set, dst, i2;
8162 int i, j;
8163 enum rtx_code code = GET_CODE (x);
8165 if (code != REG)
8167 const char *fmt= GET_RTX_FORMAT (code);
8168 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8170 if (fmt[i] == 'e')
8171 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
8172 else if (fmt[i] == 'E')
8174 for (j = XVECLEN (x, i) - 1; j >=0; j--)
8175 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
8176 current_insn);
8179 return;
8182 if (spill_reg_order[REGNO (x)] < 0)
8183 return;
8185 /* Scan backwards for the insn that sets x. This might be a way back due
8186 to inheritance. */
8187 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
8189 code = GET_CODE (prev);
8190 if (code == CODE_LABEL || code == JUMP_INSN)
8191 return;
8192 if (GET_RTX_CLASS (code) != 'i')
8193 continue;
8194 if (reg_set_p (x, PATTERN (prev)))
8195 break;
8196 if (reg_referenced_p (x, PATTERN (prev)))
8197 return;
8199 if (! prev || INSN_UID (prev) < reload_first_uid)
8200 return;
8201 /* Check that PREV only sets the reload register. */
8202 set = single_set (prev);
8203 if (! set)
8204 return;
8205 dst = SET_DEST (set);
8206 if (GET_CODE (dst) != REG
8207 || ! rtx_equal_p (dst, x))
8208 return;
8209 if (! reg_set_p (dst, PATTERN (dead_insn)))
8211 /* Check if DST was used in a later insn -
8212 it might have been inherited. */
8213 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
8215 if (GET_CODE (i2) == CODE_LABEL)
8216 break;
8217 if (GET_RTX_CLASS (GET_CODE (i2)) != 'i')
8218 continue;
8219 if (reg_referenced_p (dst, PATTERN (i2)))
8221 /* If there is a reference to the register in the current insn,
8222 it might be loaded in a non-inherited reload. If no other
8223 reload uses it, that means the register is set before
8224 referenced. */
8225 if (i2 == current_insn)
8227 for (j = n_reloads - 1; j >= 0; j--)
8228 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8229 || reload_override_in[j] == dst)
8230 return;
8231 for (j = n_reloads - 1; j >= 0; j--)
8232 if (rld[j].in && rld[j].reg_rtx == dst)
8233 break;
8234 if (j >= 0)
8235 break;
8237 return;
8239 if (GET_CODE (i2) == JUMP_INSN)
8240 break;
8241 /* If DST is still live at CURRENT_INSN, check if it is used for
8242 any reload. Note that even if CURRENT_INSN sets DST, we still
8243 have to check the reloads. */
8244 if (i2 == current_insn)
8246 for (j = n_reloads - 1; j >= 0; j--)
8247 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8248 || reload_override_in[j] == dst)
8249 return;
8250 /* ??? We can't finish the loop here, because dst might be
8251 allocated to a pseudo in this block if no reload in this
8252 block needs any of the clsses containing DST - see
8253 spill_hard_reg. There is no easy way to tell this, so we
8254 have to scan till the end of the basic block. */
8256 if (reg_set_p (dst, PATTERN (i2)))
8257 break;
8260 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
8261 reg_reloaded_contents[REGNO (dst)] = -1;
8262 /* Can't use delete_insn here because PREV might be a basic block head. */
8263 PUT_CODE (prev, NOTE);
8264 NOTE_LINE_NUMBER (prev) = NOTE_INSN_DELETED;
8265 NOTE_SOURCE_FILE (prev) = 0;
8268 /* Output reload-insns to reload VALUE into RELOADREG.
8269 VALUE is an autoincrement or autodecrement RTX whose operand
8270 is a register or memory location;
8271 so reloading involves incrementing that location.
8272 IN is either identical to VALUE, or some cheaper place to reload from.
8274 INC_AMOUNT is the number to increment or decrement by (always positive).
8275 This cannot be deduced from VALUE.
8277 Return the instruction that stores into RELOADREG. */
8279 static rtx
8280 inc_for_reload (reloadreg, in, value, inc_amount)
8281 rtx reloadreg;
8282 rtx in, value;
8283 int inc_amount;
8285 /* REG or MEM to be copied and incremented. */
8286 rtx incloc = XEXP (value, 0);
8287 /* Nonzero if increment after copying. */
8288 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
8289 rtx last;
8290 rtx inc;
8291 rtx add_insn;
8292 int code;
8293 rtx store;
8294 rtx real_in = in == value ? XEXP (in, 0) : in;
8296 /* No hard register is equivalent to this register after
8297 inc/dec operation. If REG_LAST_RELOAD_REG were non-zero,
8298 we could inc/dec that register as well (maybe even using it for
8299 the source), but I'm not sure it's worth worrying about. */
8300 if (GET_CODE (incloc) == REG)
8301 reg_last_reload_reg[REGNO (incloc)] = 0;
8303 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
8304 inc_amount = - inc_amount;
8306 inc = GEN_INT (inc_amount);
8308 /* If this is post-increment, first copy the location to the reload reg. */
8309 if (post && real_in != reloadreg)
8310 emit_insn (gen_move_insn (reloadreg, real_in));
8312 if (in == value)
8314 /* See if we can directly increment INCLOC. Use a method similar to
8315 that in gen_reload. */
8317 last = get_last_insn ();
8318 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
8319 gen_rtx_PLUS (GET_MODE (incloc),
8320 incloc, inc)));
8322 code = recog_memoized (add_insn);
8323 if (code >= 0)
8325 extract_insn (add_insn);
8326 if (constrain_operands (1))
8328 /* If this is a pre-increment and we have incremented the value
8329 where it lives, copy the incremented value to RELOADREG to
8330 be used as an address. */
8332 if (! post)
8333 emit_insn (gen_move_insn (reloadreg, incloc));
8335 return add_insn;
8338 delete_insns_since (last);
8341 /* If couldn't do the increment directly, must increment in RELOADREG.
8342 The way we do this depends on whether this is pre- or post-increment.
8343 For pre-increment, copy INCLOC to the reload register, increment it
8344 there, then save back. */
8346 if (! post)
8348 if (in != reloadreg)
8349 emit_insn (gen_move_insn (reloadreg, real_in));
8350 emit_insn (gen_add2_insn (reloadreg, inc));
8351 store = emit_insn (gen_move_insn (incloc, reloadreg));
8353 else
8355 /* Postincrement.
8356 Because this might be a jump insn or a compare, and because RELOADREG
8357 may not be available after the insn in an input reload, we must do
8358 the incrementation before the insn being reloaded for.
8360 We have already copied IN to RELOADREG. Increment the copy in
8361 RELOADREG, save that back, then decrement RELOADREG so it has
8362 the original value. */
8364 emit_insn (gen_add2_insn (reloadreg, inc));
8365 store = emit_insn (gen_move_insn (incloc, reloadreg));
8366 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
8369 return store;
8372 /* Return 1 if we are certain that the constraint-string STRING allows
8373 the hard register REG. Return 0 if we can't be sure of this. */
8375 static int
8376 constraint_accepts_reg_p (string, reg)
8377 const char *string;
8378 rtx reg;
8380 int value = 0;
8381 int regno = true_regnum (reg);
8382 int c;
8384 /* Initialize for first alternative. */
8385 value = 0;
8386 /* Check that each alternative contains `g' or `r'. */
8387 while (1)
8388 switch (c = *string++)
8390 case 0:
8391 /* If an alternative lacks `g' or `r', we lose. */
8392 return value;
8393 case ',':
8394 /* If an alternative lacks `g' or `r', we lose. */
8395 if (value == 0)
8396 return 0;
8397 /* Initialize for next alternative. */
8398 value = 0;
8399 break;
8400 case 'g':
8401 case 'r':
8402 /* Any general reg wins for this alternative. */
8403 if (TEST_HARD_REG_BIT (reg_class_contents[(int) GENERAL_REGS], regno))
8404 value = 1;
8405 break;
8406 default:
8407 /* Any reg in specified class wins for this alternative. */
8409 enum reg_class class = REG_CLASS_FROM_LETTER (c);
8411 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno))
8412 value = 1;
8417 /* Return the number of places FIND appears within X, but don't count
8418 an occurrence if some SET_DEST is FIND. */
8421 count_occurrences (x, find)
8422 register rtx x, find;
8424 register int i, j;
8425 register enum rtx_code code;
8426 register const char *format_ptr;
8427 int count;
8429 if (x == find)
8430 return 1;
8431 if (x == 0)
8432 return 0;
8434 code = GET_CODE (x);
8436 switch (code)
8438 case REG:
8439 case QUEUED:
8440 case CONST_INT:
8441 case CONST_DOUBLE:
8442 case SYMBOL_REF:
8443 case CODE_LABEL:
8444 case PC:
8445 case CC0:
8446 return 0;
8448 case MEM:
8449 if (GET_CODE (find) == MEM && rtx_equal_p (x, find))
8450 return 1;
8451 break;
8452 case SET:
8453 if (SET_DEST (x) == find)
8454 return count_occurrences (SET_SRC (x), find);
8455 break;
8457 default:
8458 break;
8461 format_ptr = GET_RTX_FORMAT (code);
8462 count = 0;
8464 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8466 switch (*format_ptr++)
8468 case 'e':
8469 count += count_occurrences (XEXP (x, i), find);
8470 break;
8472 case 'E':
8473 if (XVEC (x, i) != NULL)
8475 for (j = 0; j < XVECLEN (x, i); j++)
8476 count += count_occurrences (XVECEXP (x, i, j), find);
8478 break;
8481 return count;
8484 /* This array holds values which are equivalent to a hard register
8485 during reload_cse_regs. Each array element is an EXPR_LIST of
8486 values. Each time a hard register is set, we set the corresponding
8487 array element to the value. Each time a hard register is copied
8488 into memory, we add the memory location to the corresponding array
8489 element. We don't store values or memory addresses with side
8490 effects in this array.
8492 If the value is a CONST_INT, then the mode of the containing
8493 EXPR_LIST is the mode in which that CONST_INT was referenced.
8495 We sometimes clobber a specific entry in a list. In that case, we
8496 just set XEXP (list-entry, 0) to 0. */
8498 static rtx *reg_values;
8500 /* This is a preallocated REG rtx which we use as a temporary in
8501 reload_cse_invalidate_regno, so that we don't need to allocate a
8502 new one each time through a loop in that function. */
8504 static rtx invalidate_regno_rtx;
8506 /* Invalidate any entries in reg_values which depend on REGNO,
8507 including those for REGNO itself. This is called if REGNO is
8508 changing. If CLOBBER is true, then always forget anything we
8509 currently know about REGNO. MODE is the mode of the assignment to
8510 REGNO, which is used to determine how many hard registers are being
8511 changed. If MODE is VOIDmode, then only REGNO is being changed;
8512 this is used when invalidating call clobbered registers across a
8513 call. */
8515 static void
8516 reload_cse_invalidate_regno (regno, mode, clobber)
8517 int regno;
8518 enum machine_mode mode;
8519 int clobber;
8521 int endregno;
8522 register int i;
8524 /* Our callers don't always go through true_regnum; we may see a
8525 pseudo-register here from a CLOBBER or the like. We probably
8526 won't ever see a pseudo-register that has a real register number,
8527 for we check anyhow for safety. */
8528 if (regno >= FIRST_PSEUDO_REGISTER)
8529 regno = reg_renumber[regno];
8530 if (regno < 0)
8531 return;
8533 if (mode == VOIDmode)
8534 endregno = regno + 1;
8535 else
8536 endregno = regno + HARD_REGNO_NREGS (regno, mode);
8538 if (clobber)
8539 for (i = regno; i < endregno; i++)
8540 reg_values[i] = 0;
8542 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8544 rtx x;
8546 for (x = reg_values[i]; x; x = XEXP (x, 1))
8548 if (XEXP (x, 0) != 0
8549 && refers_to_regno_p (regno, endregno, XEXP (x, 0), NULL_PTR))
8551 /* If this is the only entry on the list, clear
8552 reg_values[i]. Otherwise, just clear this entry on
8553 the list. */
8554 if (XEXP (x, 1) == 0 && x == reg_values[i])
8556 reg_values[i] = 0;
8557 break;
8559 XEXP (x, 0) = 0;
8564 /* We must look at earlier registers, in case REGNO is part of a
8565 multi word value but is not the first register. If an earlier
8566 register has a value in a mode which overlaps REGNO, then we must
8567 invalidate that earlier register. Note that we do not need to
8568 check REGNO or later registers (we must not check REGNO itself,
8569 because we would incorrectly conclude that there was a conflict). */
8571 for (i = 0; i < regno; i++)
8573 rtx x;
8575 for (x = reg_values[i]; x; x = XEXP (x, 1))
8577 if (XEXP (x, 0) != 0)
8579 PUT_MODE (invalidate_regno_rtx, GET_MODE (x));
8580 REGNO (invalidate_regno_rtx) = i;
8581 if (refers_to_regno_p (regno, endregno, invalidate_regno_rtx,
8582 NULL_PTR))
8584 reload_cse_invalidate_regno (i, VOIDmode, 1);
8585 break;
8592 /* The memory at address MEM_BASE is being changed.
8593 Return whether this change will invalidate VAL. */
8595 static int
8596 reload_cse_mem_conflict_p (mem_base, val)
8597 rtx mem_base;
8598 rtx val;
8600 enum rtx_code code;
8601 const char *fmt;
8602 int i;
8604 code = GET_CODE (val);
8605 switch (code)
8607 /* Get rid of a few simple cases quickly. */
8608 case REG:
8609 case PC:
8610 case CC0:
8611 case SCRATCH:
8612 case CONST:
8613 case CONST_INT:
8614 case CONST_DOUBLE:
8615 case SYMBOL_REF:
8616 case LABEL_REF:
8617 return 0;
8619 case MEM:
8620 if (GET_MODE (mem_base) == BLKmode
8621 || GET_MODE (val) == BLKmode)
8622 return 1;
8623 if (anti_dependence (val, mem_base))
8624 return 1;
8625 /* The address may contain nested MEMs. */
8626 break;
8628 default:
8629 break;
8632 fmt = GET_RTX_FORMAT (code);
8634 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8636 if (fmt[i] == 'e')
8638 if (reload_cse_mem_conflict_p (mem_base, XEXP (val, i)))
8639 return 1;
8641 else if (fmt[i] == 'E')
8643 int j;
8645 for (j = 0; j < XVECLEN (val, i); j++)
8646 if (reload_cse_mem_conflict_p (mem_base, XVECEXP (val, i, j)))
8647 return 1;
8651 return 0;
8654 /* Invalidate any entries in reg_values which are changed because of a
8655 store to MEM_RTX. If this is called because of a non-const call
8656 instruction, MEM_RTX is (mem:BLK const0_rtx). */
8658 static void
8659 reload_cse_invalidate_mem (mem_rtx)
8660 rtx mem_rtx;
8662 register int i;
8664 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8666 rtx x;
8668 for (x = reg_values[i]; x; x = XEXP (x, 1))
8670 if (XEXP (x, 0) != 0
8671 && reload_cse_mem_conflict_p (mem_rtx, XEXP (x, 0)))
8673 /* If this is the only entry on the list, clear
8674 reg_values[i]. Otherwise, just clear this entry on
8675 the list. */
8676 if (XEXP (x, 1) == 0 && x == reg_values[i])
8678 reg_values[i] = 0;
8679 break;
8681 XEXP (x, 0) = 0;
8687 /* Invalidate DEST, which is being assigned to or clobbered. The
8688 second parameter exists so that this function can be passed to
8689 note_stores; it is ignored. */
8691 static void
8692 reload_cse_invalidate_rtx (dest, ignore, data)
8693 rtx dest;
8694 rtx ignore ATTRIBUTE_UNUSED;
8695 void *data ATTRIBUTE_UNUSED;
8697 while (GET_CODE (dest) == STRICT_LOW_PART
8698 || GET_CODE (dest) == SIGN_EXTRACT
8699 || GET_CODE (dest) == ZERO_EXTRACT
8700 || GET_CODE (dest) == SUBREG)
8701 dest = XEXP (dest, 0);
8703 if (GET_CODE (dest) == REG)
8704 reload_cse_invalidate_regno (REGNO (dest), GET_MODE (dest), 1);
8705 else if (GET_CODE (dest) == MEM)
8706 reload_cse_invalidate_mem (dest);
8709 /* Do a very simple CSE pass over the hard registers.
8711 This function detects no-op moves where we happened to assign two
8712 different pseudo-registers to the same hard register, and then
8713 copied one to the other. Reload will generate a useless
8714 instruction copying a register to itself.
8716 This function also detects cases where we load a value from memory
8717 into two different registers, and (if memory is more expensive than
8718 registers) changes it to simply copy the first register into the
8719 second register.
8721 Another optimization is performed that scans the operands of each
8722 instruction to see whether the value is already available in a
8723 hard register. It then replaces the operand with the hard register
8724 if possible, much like an optional reload would. */
8726 static void
8727 reload_cse_regs_1 (first)
8728 rtx first;
8730 char *firstobj;
8731 rtx callmem;
8732 register int i;
8733 rtx insn;
8735 init_alias_analysis ();
8737 reg_values = (rtx *) alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
8738 bzero ((char *)reg_values, FIRST_PSEUDO_REGISTER * sizeof (rtx));
8740 /* Create our EXPR_LIST structures on reload_obstack, so that we can
8741 free them when we are done. */
8742 push_obstacks (&reload_obstack, &reload_obstack);
8743 firstobj = (char *) obstack_alloc (&reload_obstack, 0);
8745 /* We pass this to reload_cse_invalidate_mem to invalidate all of
8746 memory for a non-const call instruction. */
8747 callmem = gen_rtx_MEM (BLKmode, const0_rtx);
8749 /* This is used in reload_cse_invalidate_regno to avoid consing a
8750 new REG in a loop in that function. */
8751 invalidate_regno_rtx = gen_rtx_REG (VOIDmode, 0);
8753 for (insn = first; insn; insn = NEXT_INSN (insn))
8755 rtx body;
8757 if (GET_CODE (insn) == CODE_LABEL)
8759 /* Forget all the register values at a code label. We don't
8760 try to do anything clever around jumps. */
8761 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8762 reg_values[i] = 0;
8764 continue;
8767 #ifdef NON_SAVING_SETJMP
8768 if (NON_SAVING_SETJMP && GET_CODE (insn) == NOTE
8769 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
8771 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8772 reg_values[i] = 0;
8774 continue;
8776 #endif
8778 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
8779 continue;
8781 /* If this is a call instruction, forget anything stored in a
8782 call clobbered register, or, if this is not a const call, in
8783 memory. */
8784 if (GET_CODE (insn) == CALL_INSN)
8786 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8787 if (call_used_regs[i])
8788 reload_cse_invalidate_regno (i, VOIDmode, 1);
8790 if (! CONST_CALL_P (insn))
8791 reload_cse_invalidate_mem (callmem);
8795 /* Forget all the register values at a volatile asm. */
8796 if (GET_CODE (insn) == INSN
8797 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
8798 && MEM_VOLATILE_P (PATTERN (insn)))
8799 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8800 reg_values[i] = 0;
8802 body = PATTERN (insn);
8803 if (GET_CODE (body) == SET)
8805 int count = 0;
8806 if (reload_cse_noop_set_p (body, insn))
8808 /* If this sets the return value of the function, we must keep
8809 a USE around, in case this is in a different basic block
8810 than the final USE. Otherwise, we could loose important
8811 register lifeness information on SMALL_REGISTER_CLASSES
8812 machines, where return registers might be used as spills:
8813 subsequent passes assume that spill registers are dead at
8814 the end of a basic block. */
8815 if (REG_FUNCTION_VALUE_P (SET_DEST (body)))
8817 pop_obstacks ();
8818 PATTERN (insn) = gen_rtx_USE (VOIDmode, SET_DEST (body));
8819 INSN_CODE (insn) = -1;
8820 REG_NOTES (insn) = NULL_RTX;
8821 push_obstacks (&reload_obstack, &reload_obstack);
8823 else
8825 PUT_CODE (insn, NOTE);
8826 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8827 NOTE_SOURCE_FILE (insn) = 0;
8830 /* We're done with this insn. */
8831 continue;
8834 /* It's not a no-op, but we can try to simplify it. */
8835 count += reload_cse_simplify_set (body, insn);
8837 if (count > 0)
8838 apply_change_group ();
8839 else
8840 reload_cse_simplify_operands (insn);
8842 reload_cse_record_set (body, body);
8844 else if (GET_CODE (body) == PARALLEL)
8846 int count = 0;
8847 rtx value = NULL_RTX;
8849 /* If every action in a PARALLEL is a noop, we can delete
8850 the entire PARALLEL. */
8851 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8853 rtx part = XVECEXP (body, 0, i);
8854 if (GET_CODE (part) == SET)
8856 if (! reload_cse_noop_set_p (part, insn))
8857 break;
8858 if (REG_FUNCTION_VALUE_P (SET_DEST (part)))
8860 if (value)
8861 break;
8862 value = SET_DEST (part);
8865 else if (GET_CODE (part) != CLOBBER)
8866 break;
8868 if (i < 0)
8870 if (value)
8872 pop_obstacks ();
8873 PATTERN (insn) = gen_rtx_USE (VOIDmode, value);
8874 INSN_CODE (insn) = -1;
8875 REG_NOTES (insn) = NULL_RTX;
8876 push_obstacks (&reload_obstack, &reload_obstack);
8878 else
8880 PUT_CODE (insn, NOTE);
8881 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8882 NOTE_SOURCE_FILE (insn) = 0;
8885 /* We're done with this insn. */
8886 continue;
8889 /* It's not a no-op, but we can try to simplify it. */
8890 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8891 if (GET_CODE (XVECEXP (body, 0, i)) == SET)
8892 count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
8894 if (count > 0)
8895 apply_change_group ();
8896 else
8897 reload_cse_simplify_operands (insn);
8899 /* Look through the PARALLEL and record the values being
8900 set, if possible. Also handle any CLOBBERs. */
8901 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8903 rtx x = XVECEXP (body, 0, i);
8905 if (GET_CODE (x) == SET)
8906 reload_cse_record_set (x, body);
8907 else
8908 note_stores (x, reload_cse_invalidate_rtx, NULL);
8911 else
8912 note_stores (body, reload_cse_invalidate_rtx, NULL);
8914 #ifdef AUTO_INC_DEC
8915 /* Clobber any registers which appear in REG_INC notes. We
8916 could keep track of the changes to their values, but it is
8917 unlikely to help. */
8919 rtx x;
8921 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
8922 if (REG_NOTE_KIND (x) == REG_INC)
8923 reload_cse_invalidate_rtx (XEXP (x, 0), NULL_RTX, NULL);
8925 #endif
8927 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
8928 after we have processed the insn. */
8929 if (GET_CODE (insn) == CALL_INSN)
8931 rtx x;
8933 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
8934 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
8935 reload_cse_invalidate_rtx (XEXP (XEXP (x, 0), 0), NULL_RTX,
8936 NULL);
8940 /* Clean up. */
8941 end_alias_analysis ();
8943 /* Free all the temporary structures we created, and go back to the
8944 regular obstacks. */
8945 obstack_free (&reload_obstack, firstobj);
8946 pop_obstacks ();
8949 /* Call cse / combine like post-reload optimization phases.
8950 FIRST is the first instruction. */
8951 void
8952 reload_cse_regs (first)
8953 rtx first;
8955 reload_cse_regs_1 (first);
8956 reload_combine ();
8957 reload_cse_move2add (first);
8958 if (flag_expensive_optimizations)
8959 reload_cse_regs_1 (first);
8962 /* Return whether the values known for REGNO are equal to VAL. MODE
8963 is the mode of the object that VAL is being copied to; this matters
8964 if VAL is a CONST_INT. */
8966 static int
8967 reload_cse_regno_equal_p (regno, val, mode)
8968 int regno;
8969 rtx val;
8970 enum machine_mode mode;
8972 rtx x;
8974 if (val == 0)
8975 return 0;
8977 for (x = reg_values[regno]; x; x = XEXP (x, 1))
8978 if (XEXP (x, 0) != 0
8979 && rtx_equal_p (XEXP (x, 0), val)
8980 && (! flag_float_store || GET_CODE (XEXP (x, 0)) != MEM
8981 || GET_MODE_CLASS (GET_MODE (x)) != MODE_FLOAT)
8982 && (GET_CODE (val) != CONST_INT
8983 || mode == GET_MODE (x)
8984 || (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8985 /* On a big endian machine if the value spans more than
8986 one register then this register holds the high part of
8987 it and we can't use it.
8989 ??? We should also compare with the high part of the
8990 value. */
8991 && !(WORDS_BIG_ENDIAN
8992 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
8993 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
8994 GET_MODE_BITSIZE (GET_MODE (x))))))
8995 return 1;
8997 return 0;
9000 /* See whether a single set is a noop. SET is the set instruction we
9001 are should check, and INSN is the instruction from which it came. */
9003 static int
9004 reload_cse_noop_set_p (set, insn)
9005 rtx set;
9006 rtx insn ATTRIBUTE_UNUSED;
9008 rtx src, dest;
9009 enum machine_mode dest_mode;
9010 int dreg, sreg;
9011 int ret;
9013 src = SET_SRC (set);
9014 dest = SET_DEST (set);
9015 dest_mode = GET_MODE (dest);
9017 if (side_effects_p (src))
9018 return 0;
9020 dreg = true_regnum (dest);
9021 sreg = true_regnum (src);
9023 /* Check for setting a register to itself. In this case, we don't
9024 have to worry about REG_DEAD notes. */
9025 if (dreg >= 0 && dreg == sreg)
9026 return 1;
9028 ret = 0;
9029 if (dreg >= 0)
9031 /* Check for setting a register to itself. */
9032 if (dreg == sreg)
9033 ret = 1;
9035 /* Check for setting a register to a value which we already know
9036 is in the register. */
9037 else if (reload_cse_regno_equal_p (dreg, src, dest_mode))
9038 ret = 1;
9040 /* Check for setting a register DREG to another register SREG
9041 where SREG is equal to a value which is already in DREG. */
9042 else if (sreg >= 0)
9044 rtx x;
9046 for (x = reg_values[sreg]; x; x = XEXP (x, 1))
9048 rtx tmp;
9050 if (XEXP (x, 0) == 0)
9051 continue;
9053 if (dest_mode == GET_MODE (x))
9054 tmp = XEXP (x, 0);
9055 else if (GET_MODE_BITSIZE (dest_mode)
9056 < GET_MODE_BITSIZE (GET_MODE (x)))
9057 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
9058 else
9059 continue;
9061 if (tmp
9062 && reload_cse_regno_equal_p (dreg, tmp, dest_mode))
9064 ret = 1;
9065 break;
9070 else if (GET_CODE (dest) == MEM)
9072 /* Check for storing a register to memory when we know that the
9073 register is equivalent to the memory location. */
9074 if (sreg >= 0
9075 && reload_cse_regno_equal_p (sreg, dest, dest_mode)
9076 && ! side_effects_p (dest))
9077 ret = 1;
9080 return ret;
9083 /* Try to simplify a single SET instruction. SET is the set pattern.
9084 INSN is the instruction it came from.
9085 This function only handles one case: if we set a register to a value
9086 which is not a register, we try to find that value in some other register
9087 and change the set into a register copy. */
9089 static int
9090 reload_cse_simplify_set (set, insn)
9091 rtx set;
9092 rtx insn;
9094 int dreg;
9095 rtx src;
9096 enum machine_mode dest_mode;
9097 enum reg_class dclass;
9098 register int i;
9100 dreg = true_regnum (SET_DEST (set));
9101 if (dreg < 0)
9102 return 0;
9104 src = SET_SRC (set);
9105 if (side_effects_p (src) || true_regnum (src) >= 0)
9106 return 0;
9108 dclass = REGNO_REG_CLASS (dreg);
9110 /* If memory loads are cheaper than register copies, don't change them. */
9111 if (GET_CODE (src) == MEM
9112 && MEMORY_MOVE_COST (GET_MODE (src), dclass, 1) < 2)
9113 return 0;
9115 /* If the constant is cheaper than a register, don't change it. */
9116 if (CONSTANT_P (src)
9117 && rtx_cost (src, SET) < 2)
9118 return 0;
9120 dest_mode = GET_MODE (SET_DEST (set));
9121 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
9123 if (i != dreg
9124 && REGISTER_MOVE_COST (REGNO_REG_CLASS (i), dclass) == 2
9125 && reload_cse_regno_equal_p (i, src, dest_mode))
9127 int validated;
9129 /* Pop back to the real obstacks while changing the insn. */
9130 pop_obstacks ();
9132 validated = validate_change (insn, &SET_SRC (set),
9133 gen_rtx_REG (dest_mode, i), 1);
9135 /* Go back to the obstack we are using for temporary
9136 storage. */
9137 push_obstacks (&reload_obstack, &reload_obstack);
9139 if (validated)
9140 return 1;
9143 return 0;
9146 /* Try to replace operands in INSN with equivalent values that are already
9147 in registers. This can be viewed as optional reloading.
9149 For each non-register operand in the insn, see if any hard regs are
9150 known to be equivalent to that operand. Record the alternatives which
9151 can accept these hard registers. Among all alternatives, select the
9152 ones which are better or equal to the one currently matching, where
9153 "better" is in terms of '?' and '!' constraints. Among the remaining
9154 alternatives, select the one which replaces most operands with
9155 hard registers. */
9157 static int
9158 reload_cse_simplify_operands (insn)
9159 rtx insn;
9161 int i,j;
9163 const char *constraints[MAX_RECOG_OPERANDS];
9165 /* Vector recording how bad an alternative is. */
9166 int *alternative_reject;
9167 /* Vector recording how many registers can be introduced by choosing
9168 this alternative. */
9169 int *alternative_nregs;
9170 /* Array of vectors recording, for each operand and each alternative,
9171 which hard register to substitute, or -1 if the operand should be
9172 left as it is. */
9173 int *op_alt_regno[MAX_RECOG_OPERANDS];
9174 /* Array of alternatives, sorted in order of decreasing desirability. */
9175 int *alternative_order;
9176 rtx reg = gen_rtx_REG (VOIDmode, -1);
9178 extract_insn (insn);
9180 if (recog_data.n_alternatives == 0 || recog_data.n_operands == 0)
9181 return 0;
9183 /* Figure out which alternative currently matches. */
9184 if (! constrain_operands (1))
9185 fatal_insn_not_found (insn);
9187 alternative_reject = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9188 alternative_nregs = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9189 alternative_order = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9190 bzero ((char *)alternative_reject, recog_data.n_alternatives * sizeof (int));
9191 bzero ((char *)alternative_nregs, recog_data.n_alternatives * sizeof (int));
9193 for (i = 0; i < recog_data.n_operands; i++)
9195 enum machine_mode mode;
9196 int regno;
9197 const char *p;
9199 op_alt_regno[i] = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9200 for (j = 0; j < recog_data.n_alternatives; j++)
9201 op_alt_regno[i][j] = -1;
9203 p = constraints[i] = recog_data.constraints[i];
9204 mode = recog_data.operand_mode[i];
9206 /* Add the reject values for each alternative given by the constraints
9207 for this operand. */
9208 j = 0;
9209 while (*p != '\0')
9211 char c = *p++;
9212 if (c == ',')
9213 j++;
9214 else if (c == '?')
9215 alternative_reject[j] += 3;
9216 else if (c == '!')
9217 alternative_reject[j] += 300;
9220 /* We won't change operands which are already registers. We
9221 also don't want to modify output operands. */
9222 regno = true_regnum (recog_data.operand[i]);
9223 if (regno >= 0
9224 || constraints[i][0] == '='
9225 || constraints[i][0] == '+')
9226 continue;
9228 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9230 int class = (int) NO_REGS;
9232 if (! reload_cse_regno_equal_p (regno, recog_data.operand[i], mode))
9233 continue;
9235 REGNO (reg) = regno;
9236 PUT_MODE (reg, mode);
9238 /* We found a register equal to this operand. Now look for all
9239 alternatives that can accept this register and have not been
9240 assigned a register they can use yet. */
9241 j = 0;
9242 p = constraints[i];
9243 for (;;)
9245 char c = *p++;
9247 switch (c)
9249 case '=': case '+': case '?':
9250 case '#': case '&': case '!':
9251 case '*': case '%':
9252 case '0': case '1': case '2': case '3': case '4':
9253 case '5': case '6': case '7': case '8': case '9':
9254 case 'm': case '<': case '>': case 'V': case 'o':
9255 case 'E': case 'F': case 'G': case 'H':
9256 case 's': case 'i': case 'n':
9257 case 'I': case 'J': case 'K': case 'L':
9258 case 'M': case 'N': case 'O': case 'P':
9259 #ifdef EXTRA_CONSTRAINT
9260 case 'Q': case 'R': case 'S': case 'T': case 'U':
9261 #endif
9262 case 'p': case 'X':
9263 /* These don't say anything we care about. */
9264 break;
9266 case 'g': case 'r':
9267 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
9268 break;
9270 default:
9271 class
9272 = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER ((unsigned char)c)];
9273 break;
9275 case ',': case '\0':
9276 /* See if REGNO fits this alternative, and set it up as the
9277 replacement register if we don't have one for this
9278 alternative yet and the operand being replaced is not
9279 a cheap CONST_INT. */
9280 if (op_alt_regno[i][j] == -1
9281 && reg_fits_class_p (reg, class, 0, mode)
9282 && (GET_CODE (recog_data.operand[i]) != CONST_INT
9283 || (rtx_cost (recog_data.operand[i], SET)
9284 > rtx_cost (reg, SET))))
9286 alternative_nregs[j]++;
9287 op_alt_regno[i][j] = regno;
9289 j++;
9290 break;
9293 if (c == '\0')
9294 break;
9299 /* Record all alternatives which are better or equal to the currently
9300 matching one in the alternative_order array. */
9301 for (i = j = 0; i < recog_data.n_alternatives; i++)
9302 if (alternative_reject[i] <= alternative_reject[which_alternative])
9303 alternative_order[j++] = i;
9304 recog_data.n_alternatives = j;
9306 /* Sort it. Given a small number of alternatives, a dumb algorithm
9307 won't hurt too much. */
9308 for (i = 0; i < recog_data.n_alternatives - 1; i++)
9310 int best = i;
9311 int best_reject = alternative_reject[alternative_order[i]];
9312 int best_nregs = alternative_nregs[alternative_order[i]];
9313 int tmp;
9315 for (j = i + 1; j < recog_data.n_alternatives; j++)
9317 int this_reject = alternative_reject[alternative_order[j]];
9318 int this_nregs = alternative_nregs[alternative_order[j]];
9320 if (this_reject < best_reject
9321 || (this_reject == best_reject && this_nregs < best_nregs))
9323 best = j;
9324 best_reject = this_reject;
9325 best_nregs = this_nregs;
9329 tmp = alternative_order[best];
9330 alternative_order[best] = alternative_order[i];
9331 alternative_order[i] = tmp;
9334 /* Substitute the operands as determined by op_alt_regno for the best
9335 alternative. */
9336 j = alternative_order[0];
9338 /* Pop back to the real obstacks while changing the insn. */
9339 pop_obstacks ();
9341 for (i = 0; i < recog_data.n_operands; i++)
9343 enum machine_mode mode = recog_data.operand_mode[i];
9344 if (op_alt_regno[i][j] == -1)
9345 continue;
9347 validate_change (insn, recog_data.operand_loc[i],
9348 gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
9351 for (i = recog_data.n_dups - 1; i >= 0; i--)
9353 int op = recog_data.dup_num[i];
9354 enum machine_mode mode = recog_data.operand_mode[op];
9356 if (op_alt_regno[op][j] == -1)
9357 continue;
9359 validate_change (insn, recog_data.dup_loc[i],
9360 gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
9363 /* Go back to the obstack we are using for temporary
9364 storage. */
9365 push_obstacks (&reload_obstack, &reload_obstack);
9367 return apply_change_group ();
9370 /* These two variables are used to pass information from
9371 reload_cse_record_set to reload_cse_check_clobber. */
9373 static int reload_cse_check_clobbered;
9374 static rtx reload_cse_check_src;
9376 /* See if DEST overlaps with RELOAD_CSE_CHECK_SRC. If it does, set
9377 RELOAD_CSE_CHECK_CLOBBERED. This is called via note_stores. The
9378 second argument, which is passed by note_stores, is ignored. */
9380 static void
9381 reload_cse_check_clobber (dest, ignore, data)
9382 rtx dest;
9383 rtx ignore ATTRIBUTE_UNUSED;
9384 void *data ATTRIBUTE_UNUSED;
9386 if (reg_overlap_mentioned_p (dest, reload_cse_check_src))
9387 reload_cse_check_clobbered = 1;
9390 /* Record the result of a SET instruction. SET is the set pattern.
9391 BODY is the pattern of the insn that it came from. */
9393 static void
9394 reload_cse_record_set (set, body)
9395 rtx set;
9396 rtx body;
9398 rtx dest, src, x;
9399 int dreg, sreg;
9400 enum machine_mode dest_mode;
9402 dest = SET_DEST (set);
9403 src = SET_SRC (set);
9404 dreg = true_regnum (dest);
9405 sreg = true_regnum (src);
9406 dest_mode = GET_MODE (dest);
9408 /* Some machines don't define AUTO_INC_DEC, but they still use push
9409 instructions. We need to catch that case here in order to
9410 invalidate the stack pointer correctly. Note that invalidating
9411 the stack pointer is different from invalidating DEST. */
9412 x = dest;
9413 while (GET_CODE (x) == SUBREG
9414 || GET_CODE (x) == ZERO_EXTRACT
9415 || GET_CODE (x) == SIGN_EXTRACT
9416 || GET_CODE (x) == STRICT_LOW_PART)
9417 x = XEXP (x, 0);
9418 if (push_operand (x, GET_MODE (x)))
9420 reload_cse_invalidate_rtx (stack_pointer_rtx, NULL_RTX, NULL);
9421 reload_cse_invalidate_rtx (dest, NULL_RTX, NULL);
9422 return;
9425 /* We can only handle an assignment to a register, or a store of a
9426 register to a memory location. For other cases, we just clobber
9427 the destination. We also have to just clobber if there are side
9428 effects in SRC or DEST. */
9429 if ((dreg < 0 && GET_CODE (dest) != MEM)
9430 || side_effects_p (src)
9431 || side_effects_p (dest))
9433 reload_cse_invalidate_rtx (dest, NULL_RTX, NULL);
9434 return;
9437 #ifdef HAVE_cc0
9438 /* We don't try to handle values involving CC, because it's a pain
9439 to keep track of when they have to be invalidated. */
9440 if (reg_mentioned_p (cc0_rtx, src)
9441 || reg_mentioned_p (cc0_rtx, dest))
9443 reload_cse_invalidate_rtx (dest, NULL_RTX, NULL);
9444 return;
9446 #endif
9448 /* If BODY is a PARALLEL, then we need to see whether the source of
9449 SET is clobbered by some other instruction in the PARALLEL. */
9450 if (GET_CODE (body) == PARALLEL)
9452 int i;
9454 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
9456 rtx x;
9458 x = XVECEXP (body, 0, i);
9459 if (x == set)
9460 continue;
9462 reload_cse_check_clobbered = 0;
9463 reload_cse_check_src = src;
9464 note_stores (x, reload_cse_check_clobber, NULL);
9465 if (reload_cse_check_clobbered)
9467 reload_cse_invalidate_rtx (dest, NULL_RTX, NULL);
9468 return;
9473 if (dreg >= 0)
9475 int i;
9477 /* This is an assignment to a register. Update the value we
9478 have stored for the register. */
9479 if (sreg >= 0)
9481 rtx x;
9483 /* This is a copy from one register to another. Any values
9484 which were valid for SREG are now valid for DREG. If the
9485 mode changes, we use gen_lowpart_common to extract only
9486 the part of the value that is copied. */
9487 reg_values[dreg] = 0;
9488 for (x = reg_values[sreg]; x; x = XEXP (x, 1))
9490 rtx tmp;
9492 if (XEXP (x, 0) == 0)
9493 continue;
9494 if (dest_mode == GET_MODE (XEXP (x, 0)))
9495 tmp = XEXP (x, 0);
9496 else if (GET_MODE_BITSIZE (dest_mode)
9497 > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
9498 continue;
9499 else
9500 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
9501 if (tmp)
9502 reg_values[dreg] = gen_rtx_EXPR_LIST (dest_mode, tmp,
9503 reg_values[dreg]);
9506 else
9507 reg_values[dreg] = gen_rtx_EXPR_LIST (dest_mode, src, NULL_RTX);
9509 /* We've changed DREG, so invalidate any values held by other
9510 registers that depend upon it. */
9511 reload_cse_invalidate_regno (dreg, dest_mode, 0);
9513 /* If this assignment changes more than one hard register,
9514 forget anything we know about the others. */
9515 for (i = 1; i < HARD_REGNO_NREGS (dreg, dest_mode); i++)
9516 reg_values[dreg + i] = 0;
9518 else if (GET_CODE (dest) == MEM)
9520 /* Invalidate conflicting memory locations. */
9521 reload_cse_invalidate_mem (dest);
9523 /* If we're storing a register to memory, add DEST to the list
9524 in REG_VALUES. */
9525 if (sreg >= 0 && ! side_effects_p (dest))
9526 reg_values[sreg] = gen_rtx_EXPR_LIST (dest_mode, dest,
9527 reg_values[sreg]);
9529 else
9531 /* We should have bailed out earlier. */
9532 abort ();
9536 /* If reload couldn't use reg+reg+offset addressing, try to use reg+reg
9537 addressing now.
9538 This code might also be useful when reload gave up on reg+reg addresssing
9539 because of clashes between the return register and INDEX_REG_CLASS. */
9541 /* The maximum number of uses of a register we can keep track of to
9542 replace them with reg+reg addressing. */
9543 #define RELOAD_COMBINE_MAX_USES 6
9545 /* INSN is the insn where a register has ben used, and USEP points to the
9546 location of the register within the rtl. */
9547 struct reg_use { rtx insn, *usep; };
9549 /* If the register is used in some unknown fashion, USE_INDEX is negative.
9550 If it is dead, USE_INDEX is RELOAD_COMBINE_MAX_USES, and STORE_RUID
9551 indicates where it becomes live again.
9552 Otherwise, USE_INDEX is the index of the last encountered use of the
9553 register (which is first among these we have seen since we scan backwards),
9554 OFFSET contains the constant offset that is added to the register in
9555 all encountered uses, and USE_RUID indicates the first encountered, i.e.
9556 last, of these uses.
9557 STORE_RUID is always meaningful if we only want to use a value in a
9558 register in a different place: it denotes the next insn in the insn
9559 stream (i.e. the last ecountered) that sets or clobbers the register. */
9560 static struct
9562 struct reg_use reg_use[RELOAD_COMBINE_MAX_USES];
9563 int use_index;
9564 rtx offset;
9565 int store_ruid;
9566 int use_ruid;
9567 } reg_state[FIRST_PSEUDO_REGISTER];
9569 /* Reverse linear uid. This is increased in reload_combine while scanning
9570 the instructions from last to first. It is used to set last_label_ruid
9571 and the store_ruid / use_ruid fields in reg_state. */
9572 static int reload_combine_ruid;
9574 #define LABEL_LIVE(LABEL) \
9575 (label_live[CODE_LABEL_NUMBER (LABEL) - min_labelno])
9577 static void
9578 reload_combine ()
9580 rtx insn, set;
9581 int first_index_reg = 1, last_index_reg = 0;
9582 int i;
9583 int last_label_ruid;
9584 int min_labelno, n_labels;
9585 HARD_REG_SET ever_live_at_start, *label_live;
9587 /* If reg+reg can be used in offsetable memory adresses, the main chunk of
9588 reload has already used it where appropriate, so there is no use in
9589 trying to generate it now. */
9590 if (double_reg_address_ok && INDEX_REG_CLASS != NO_REGS)
9591 return;
9593 /* To avoid wasting too much time later searching for an index register,
9594 determine the minimum and maximum index register numbers. */
9595 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9597 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i))
9599 if (! last_index_reg)
9600 last_index_reg = i;
9601 first_index_reg = i;
9604 /* If no index register is available, we can quit now. */
9605 if (first_index_reg > last_index_reg)
9606 return;
9608 /* Set up LABEL_LIVE and EVER_LIVE_AT_START. The register lifetime
9609 information is a bit fuzzy immediately after reload, but it's
9610 still good enough to determine which registers are live at a jump
9611 destination. */
9612 min_labelno = get_first_label_num ();
9613 n_labels = max_label_num () - min_labelno;
9614 label_live = (HARD_REG_SET *) xmalloc (n_labels * sizeof (HARD_REG_SET));
9615 CLEAR_HARD_REG_SET (ever_live_at_start);
9616 for (i = n_basic_blocks - 1; i >= 0; i--)
9618 insn = BLOCK_HEAD (i);
9619 if (GET_CODE (insn) == CODE_LABEL)
9621 HARD_REG_SET live;
9623 REG_SET_TO_HARD_REG_SET (live, BASIC_BLOCK (i)->global_live_at_start);
9624 compute_use_by_pseudos (&live, BASIC_BLOCK (i)->global_live_at_start);
9625 COPY_HARD_REG_SET (LABEL_LIVE (insn), live);
9626 IOR_HARD_REG_SET (ever_live_at_start, live);
9630 /* Initialize last_label_ruid, reload_combine_ruid and reg_state. */
9631 last_label_ruid = reload_combine_ruid = 0;
9632 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9634 reg_state[i].store_ruid = reload_combine_ruid;
9635 if (fixed_regs[i])
9636 reg_state[i].use_index = -1;
9637 else
9638 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9641 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
9643 rtx note;
9645 /* We cannot do our optimization across labels. Invalidating all the use
9646 information we have would be costly, so we just note where the label
9647 is and then later disable any optimization that would cross it. */
9648 if (GET_CODE (insn) == CODE_LABEL)
9649 last_label_ruid = reload_combine_ruid;
9650 if (GET_CODE (insn) == BARRIER)
9652 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9653 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9655 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
9656 continue;
9657 reload_combine_ruid++;
9659 /* Look for (set (REGX) (CONST_INT))
9660 (set (REGX) (PLUS (REGX) (REGY)))
9662 ... (MEM (REGX)) ...
9663 and convert it to
9664 (set (REGZ) (CONST_INT))
9666 ... (MEM (PLUS (REGZ) (REGY)))... .
9668 First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
9669 and that we know all uses of REGX before it dies. */
9670 set = single_set (insn);
9671 if (set != NULL_RTX
9672 && GET_CODE (SET_DEST (set)) == REG
9673 && (HARD_REGNO_NREGS (REGNO (SET_DEST (set)),
9674 GET_MODE (SET_DEST (set)))
9675 == 1)
9676 && GET_CODE (SET_SRC (set)) == PLUS
9677 && GET_CODE (XEXP (SET_SRC (set), 1)) == REG
9678 && rtx_equal_p (XEXP (SET_SRC (set), 0), SET_DEST (set))
9679 && last_label_ruid < reg_state[REGNO (SET_DEST (set))].use_ruid)
9681 rtx reg = SET_DEST (set);
9682 rtx plus = SET_SRC (set);
9683 rtx base = XEXP (plus, 1);
9684 rtx prev = prev_nonnote_insn (insn);
9685 rtx prev_set = prev ? single_set (prev) : NULL_RTX;
9686 int regno = REGNO (reg);
9687 rtx const_reg = NULL_RTX;
9688 rtx reg_sum = NULL_RTX;
9690 /* Now, we need an index register.
9691 We'll set index_reg to this index register, const_reg to the
9692 register that is to be loaded with the constant
9693 (denoted as REGZ in the substitution illustration above),
9694 and reg_sum to the register-register that we want to use to
9695 substitute uses of REG (typically in MEMs) with.
9696 First check REG and BASE for being index registers;
9697 we can use them even if they are not dead. */
9698 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], regno)
9699 || TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
9700 REGNO (base)))
9702 const_reg = reg;
9703 reg_sum = plus;
9705 else
9707 /* Otherwise, look for a free index register. Since we have
9708 checked above that neiter REG nor BASE are index registers,
9709 if we find anything at all, it will be different from these
9710 two registers. */
9711 for (i = first_index_reg; i <= last_index_reg; i++)
9713 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i)
9714 && reg_state[i].use_index == RELOAD_COMBINE_MAX_USES
9715 && reg_state[i].store_ruid <= reg_state[regno].use_ruid
9716 && HARD_REGNO_NREGS (i, GET_MODE (reg)) == 1)
9718 rtx index_reg = gen_rtx_REG (GET_MODE (reg), i);
9719 const_reg = index_reg;
9720 reg_sum = gen_rtx_PLUS (GET_MODE (reg), index_reg, base);
9721 break;
9725 /* Check that PREV_SET is indeed (set (REGX) (CONST_INT)) and that
9726 (REGY), i.e. BASE, is not clobbered before the last use we'll
9727 create. */
9728 if (prev_set
9729 && GET_CODE (SET_SRC (prev_set)) == CONST_INT
9730 && rtx_equal_p (SET_DEST (prev_set), reg)
9731 && reg_state[regno].use_index >= 0
9732 && reg_state[REGNO (base)].store_ruid <= reg_state[regno].use_ruid
9733 && reg_sum)
9735 int i;
9737 /* Change destination register and - if necessary - the
9738 constant value in PREV, the constant loading instruction. */
9739 validate_change (prev, &SET_DEST (prev_set), const_reg, 1);
9740 if (reg_state[regno].offset != const0_rtx)
9741 validate_change (prev,
9742 &SET_SRC (prev_set),
9743 GEN_INT (INTVAL (SET_SRC (prev_set))
9744 + INTVAL (reg_state[regno].offset)),
9746 /* Now for every use of REG that we have recorded, replace REG
9747 with REG_SUM. */
9748 for (i = reg_state[regno].use_index;
9749 i < RELOAD_COMBINE_MAX_USES; i++)
9750 validate_change (reg_state[regno].reg_use[i].insn,
9751 reg_state[regno].reg_use[i].usep,
9752 reg_sum, 1);
9754 if (apply_change_group ())
9756 rtx *np;
9758 /* Delete the reg-reg addition. */
9759 PUT_CODE (insn, NOTE);
9760 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9761 NOTE_SOURCE_FILE (insn) = 0;
9763 if (reg_state[regno].offset != const0_rtx)
9765 /* Previous REG_EQUIV / REG_EQUAL notes for PREV
9766 are now invalid. */
9767 for (np = &REG_NOTES (prev); *np; )
9769 if (REG_NOTE_KIND (*np) == REG_EQUAL
9770 || REG_NOTE_KIND (*np) == REG_EQUIV)
9771 *np = XEXP (*np, 1);
9772 else
9773 np = &XEXP (*np, 1);
9776 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
9777 reg_state[REGNO (const_reg)].store_ruid = reload_combine_ruid;
9778 continue;
9782 note_stores (PATTERN (insn), reload_combine_note_store, NULL);
9783 if (GET_CODE (insn) == CALL_INSN)
9785 rtx link;
9787 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9789 if (call_used_regs[i])
9791 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9792 reg_state[i].store_ruid = reload_combine_ruid;
9795 for (link = CALL_INSN_FUNCTION_USAGE (insn); link;
9796 link = XEXP (link, 1))
9798 rtx use = XEXP (link, 0);
9799 int regno = REGNO (XEXP (use, 0));
9800 if (GET_CODE (use) == CLOBBER)
9802 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
9803 reg_state[regno].store_ruid = reload_combine_ruid;
9805 else
9806 reg_state[regno].use_index = -1;
9809 if (GET_CODE (insn) == JUMP_INSN && GET_CODE (PATTERN (insn)) != RETURN)
9811 /* Non-spill registers might be used at the call destination in
9812 some unknown fashion, so we have to mark the unknown use. */
9813 HARD_REG_SET *live;
9814 if ((condjump_p (insn) || condjump_in_parallel_p (insn))
9815 && JUMP_LABEL (insn))
9816 live = &LABEL_LIVE (JUMP_LABEL (insn));
9817 else
9818 live = &ever_live_at_start;
9819 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9821 if (TEST_HARD_REG_BIT (*live, i))
9822 reg_state[i].use_index = -1;
9825 reload_combine_note_use (&PATTERN (insn), insn);
9826 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
9828 if (REG_NOTE_KIND (note) == REG_INC
9829 && GET_CODE (XEXP (note, 0)) == REG)
9831 int regno = REGNO (XEXP (note, 0));
9833 reg_state[regno].store_ruid = reload_combine_ruid;
9834 reg_state[regno].use_index = -1;
9838 free (label_live);
9841 /* Check if DST is a register or a subreg of a register; if it is,
9842 update reg_state[regno].store_ruid and reg_state[regno].use_index
9843 accordingly. Called via note_stores from reload_combine. */
9844 static void
9845 reload_combine_note_store (dst, set, data)
9846 rtx dst, set;
9847 void *data ATTRIBUTE_UNUSED;
9849 int regno = 0;
9850 int i;
9851 unsigned size = GET_MODE_SIZE (GET_MODE (dst));
9853 if (GET_CODE (dst) == SUBREG)
9855 regno = SUBREG_WORD (dst);
9856 dst = SUBREG_REG (dst);
9858 if (GET_CODE (dst) != REG)
9859 return;
9860 regno += REGNO (dst);
9862 /* note_stores might have stripped a STRICT_LOW_PART, so we have to be
9863 careful with registers / register parts that are not full words.
9865 Similarly for ZERO_EXTRACT and SIGN_EXTRACT. */
9866 if (GET_CODE (set) != SET
9867 || GET_CODE (SET_DEST (set)) == ZERO_EXTRACT
9868 || GET_CODE (SET_DEST (set)) == SIGN_EXTRACT
9869 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
9871 for (i = (size - 1) / UNITS_PER_WORD + regno; i >= regno; i--)
9873 reg_state[i].use_index = -1;
9874 reg_state[i].store_ruid = reload_combine_ruid;
9877 else
9879 for (i = (size - 1) / UNITS_PER_WORD + regno; i >= regno; i--)
9881 reg_state[i].store_ruid = reload_combine_ruid;
9882 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9887 /* XP points to a piece of rtl that has to be checked for any uses of
9888 registers.
9889 *XP is the pattern of INSN, or a part of it.
9890 Called from reload_combine, and recursively by itself. */
9891 static void
9892 reload_combine_note_use (xp, insn)
9893 rtx *xp, insn;
9895 rtx x = *xp;
9896 enum rtx_code code = x->code;
9897 const char *fmt;
9898 int i, j;
9899 rtx offset = const0_rtx; /* For the REG case below. */
9901 switch (code)
9903 case SET:
9904 if (GET_CODE (SET_DEST (x)) == REG)
9906 reload_combine_note_use (&SET_SRC (x), insn);
9907 return;
9909 break;
9911 case CLOBBER:
9912 if (GET_CODE (SET_DEST (x)) == REG)
9913 return;
9914 break;
9916 case PLUS:
9917 /* We are interested in (plus (reg) (const_int)) . */
9918 if (GET_CODE (XEXP (x, 0)) != REG || GET_CODE (XEXP (x, 1)) != CONST_INT)
9919 break;
9920 offset = XEXP (x, 1);
9921 x = XEXP (x, 0);
9922 /* Fall through. */
9923 case REG:
9925 int regno = REGNO (x);
9926 int use_index;
9928 /* Some spurious USEs of pseudo registers might remain.
9929 Just ignore them. */
9930 if (regno >= FIRST_PSEUDO_REGISTER)
9931 return;
9933 /* If this register is already used in some unknown fashion, we
9934 can't do anything.
9935 If we decrement the index from zero to -1, we can't store more
9936 uses, so this register becomes used in an unknown fashion. */
9937 use_index = --reg_state[regno].use_index;
9938 if (use_index < 0)
9939 return;
9941 if (use_index != RELOAD_COMBINE_MAX_USES - 1)
9943 /* We have found another use for a register that is already
9944 used later. Check if the offsets match; if not, mark the
9945 register as used in an unknown fashion. */
9946 if (! rtx_equal_p (offset, reg_state[regno].offset))
9948 reg_state[regno].use_index = -1;
9949 return;
9952 else
9954 /* This is the first use of this register we have seen since we
9955 marked it as dead. */
9956 reg_state[regno].offset = offset;
9957 reg_state[regno].use_ruid = reload_combine_ruid;
9959 reg_state[regno].reg_use[use_index].insn = insn;
9960 reg_state[regno].reg_use[use_index].usep = xp;
9961 return;
9964 default:
9965 break;
9968 /* Recursively process the components of X. */
9969 fmt = GET_RTX_FORMAT (code);
9970 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9972 if (fmt[i] == 'e')
9973 reload_combine_note_use (&XEXP (x, i), insn);
9974 else if (fmt[i] == 'E')
9976 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9977 reload_combine_note_use (&XVECEXP (x, i, j), insn);
9982 /* See if we can reduce the cost of a constant by replacing a move with
9983 an add. */
9984 /* We cannot do our optimization across labels. Invalidating all the
9985 information about register contents we have would be costly, so we
9986 use last_label_luid (local variable of reload_cse_move2add) to note
9987 where the label is and then later disable any optimization that would
9988 cross it.
9989 reg_offset[n] / reg_base_reg[n] / reg_mode[n] are only valid if
9990 reg_set_luid[n] is larger than last_label_luid[n] . */
9991 static int reg_set_luid[FIRST_PSEUDO_REGISTER];
9992 /* reg_offset[n] has to be CONST_INT for it and reg_base_reg[n] /
9993 reg_mode[n] to be valid.
9994 If reg_offset[n] is a CONST_INT and reg_base_reg[n] is negative, register n
9995 has been set to reg_offset[n] in mode reg_mode[n] .
9996 If reg_offset[n] is a CONST_INT and reg_base_reg[n] is non-negative,
9997 register n has been set to the sum of reg_offset[n] and register
9998 reg_base_reg[n], calculated in mode reg_mode[n] . */
9999 static rtx reg_offset[FIRST_PSEUDO_REGISTER];
10000 static int reg_base_reg[FIRST_PSEUDO_REGISTER];
10001 static enum machine_mode reg_mode[FIRST_PSEUDO_REGISTER];
10002 /* move2add_luid is linearily increased while scanning the instructions
10003 from first to last. It is used to set reg_set_luid in
10004 reload_cse_move2add and move2add_note_store. */
10005 static int move2add_luid;
10007 /* Generate a CONST_INT and force it in the range of MODE. */
10008 static rtx
10009 gen_mode_int (mode, value)
10010 enum machine_mode mode;
10011 HOST_WIDE_INT value;
10013 HOST_WIDE_INT cval = value & GET_MODE_MASK (mode);
10014 int width = GET_MODE_BITSIZE (mode);
10016 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative number,
10017 sign extend it. */
10018 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
10019 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
10020 cval |= (HOST_WIDE_INT) -1 << width;
10022 return GEN_INT (cval);
10025 static void
10026 reload_cse_move2add (first)
10027 rtx first;
10029 int i;
10030 rtx insn;
10031 int last_label_luid;
10033 for (i = FIRST_PSEUDO_REGISTER-1; i >= 0; i--)
10034 reg_set_luid[i] = 0;
10036 last_label_luid = 0;
10037 move2add_luid = 1;
10038 for (insn = first; insn; insn = NEXT_INSN (insn), move2add_luid++)
10040 rtx pat, note;
10042 if (GET_CODE (insn) == CODE_LABEL)
10043 last_label_luid = move2add_luid;
10044 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
10045 continue;
10046 pat = PATTERN (insn);
10047 /* For simplicity, we only perform this optimization on
10048 straightforward SETs. */
10049 if (GET_CODE (pat) == SET
10050 && GET_CODE (SET_DEST (pat)) == REG)
10052 rtx reg = SET_DEST (pat);
10053 int regno = REGNO (reg);
10054 rtx src = SET_SRC (pat);
10056 /* Check if we have valid information on the contents of this
10057 register in the mode of REG. */
10058 /* ??? We don't know how zero / sign extension is handled, hence
10059 we can't go from a narrower to a wider mode. */
10060 if (reg_set_luid[regno] > last_label_luid
10061 && (GET_MODE_SIZE (GET_MODE (reg))
10062 <= GET_MODE_SIZE (reg_mode[regno]))
10063 && GET_CODE (reg_offset[regno]) == CONST_INT)
10065 /* Try to transform (set (REGX) (CONST_INT A))
10067 (set (REGX) (CONST_INT B))
10069 (set (REGX) (CONST_INT A))
10071 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
10073 if (GET_CODE (src) == CONST_INT && reg_base_reg[regno] < 0)
10075 int success = 0;
10076 rtx new_src
10077 = gen_mode_int (GET_MODE (reg),
10078 INTVAL (src) - INTVAL (reg_offset[regno]));
10079 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
10080 use (set (reg) (reg)) instead.
10081 We don't delete this insn, nor do we convert it into a
10082 note, to avoid losing register notes or the return
10083 value flag. jump2 already knowns how to get rid of
10084 no-op moves. */
10085 if (new_src == const0_rtx)
10086 success = validate_change (insn, &SET_SRC (pat), reg, 0);
10087 else if (rtx_cost (new_src, PLUS) < rtx_cost (src, SET)
10088 && have_add2_insn (GET_MODE (reg)))
10089 success = validate_change (insn, &PATTERN (insn),
10090 gen_add2_insn (reg, new_src), 0);
10091 reg_set_luid[regno] = move2add_luid;
10092 reg_mode[regno] = GET_MODE (reg);
10093 reg_offset[regno] = src;
10094 continue;
10097 /* Try to transform (set (REGX) (REGY))
10098 (set (REGX) (PLUS (REGX) (CONST_INT A)))
10100 (set (REGX) (REGY))
10101 (set (REGX) (PLUS (REGX) (CONST_INT B)))
10103 (REGX) (REGY))
10104 (set (REGX) (PLUS (REGX) (CONST_INT A)))
10106 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
10107 else if (GET_CODE (src) == REG
10108 && reg_base_reg[regno] == REGNO (src)
10109 && reg_set_luid[regno] > reg_set_luid[REGNO (src)])
10111 rtx next = next_nonnote_insn (insn);
10112 rtx set = NULL_RTX;
10113 if (next)
10114 set = single_set (next);
10115 if (next
10116 && set
10117 && SET_DEST (set) == reg
10118 && GET_CODE (SET_SRC (set)) == PLUS
10119 && XEXP (SET_SRC (set), 0) == reg
10120 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
10122 rtx src3 = XEXP (SET_SRC (set), 1);
10123 rtx new_src
10124 = gen_mode_int (GET_MODE (reg),
10125 INTVAL (src3)
10126 - INTVAL (reg_offset[regno]));
10127 int success = 0;
10129 if (new_src == const0_rtx)
10130 /* See above why we create (set (reg) (reg)) here. */
10131 success
10132 = validate_change (next, &SET_SRC (set), reg, 0);
10133 else if ((rtx_cost (new_src, PLUS)
10134 < 2 + rtx_cost (src3, SET))
10135 && have_add2_insn (GET_MODE (reg)))
10136 success
10137 = validate_change (next, &PATTERN (next),
10138 gen_add2_insn (reg, new_src), 0);
10139 if (success)
10141 /* INSN might be the first insn in a basic block
10142 if the preceding insn is a conditional jump
10143 or a possible-throwing call. */
10144 PUT_CODE (insn, NOTE);
10145 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
10146 NOTE_SOURCE_FILE (insn) = 0;
10148 insn = next;
10149 reg_set_luid[regno] = move2add_luid;
10150 reg_mode[regno] = GET_MODE (reg);
10151 reg_offset[regno] = src3;
10152 continue;
10158 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
10160 if (REG_NOTE_KIND (note) == REG_INC
10161 && GET_CODE (XEXP (note, 0)) == REG)
10163 /* Indicate that this register has been recently written to,
10164 but the exact contents are not available. */
10165 int regno = REGNO (XEXP (note, 0));
10166 if (regno < FIRST_PSEUDO_REGISTER)
10168 reg_set_luid[regno] = move2add_luid;
10169 reg_offset[regno] = note;
10173 note_stores (PATTERN (insn), move2add_note_store, NULL);
10174 /* If this is a CALL_INSN, all call used registers are stored with
10175 unknown values. */
10176 if (GET_CODE (insn) == CALL_INSN)
10178 for (i = FIRST_PSEUDO_REGISTER-1; i >= 0; i--)
10180 if (call_used_regs[i])
10182 reg_set_luid[i] = move2add_luid;
10183 reg_offset[i] = insn; /* Invalidate contents. */
10190 /* SET is a SET or CLOBBER that sets DST.
10191 Update reg_set_luid, reg_offset and reg_base_reg accordingly.
10192 Called from reload_cse_move2add via note_stores. */
10193 static void
10194 move2add_note_store (dst, set, data)
10195 rtx dst, set;
10196 void *data ATTRIBUTE_UNUSED;
10198 int regno = 0;
10199 int i;
10201 enum machine_mode mode = GET_MODE (dst);
10202 if (GET_CODE (dst) == SUBREG)
10204 regno = SUBREG_WORD (dst);
10205 dst = SUBREG_REG (dst);
10207 if (GET_CODE (dst) != REG)
10208 return;
10210 regno += REGNO (dst);
10212 if (HARD_REGNO_NREGS (regno, mode) == 1 && GET_CODE (set) == SET
10213 && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
10214 && GET_CODE (SET_DEST (set)) != SIGN_EXTRACT
10215 && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
10217 rtx src = SET_SRC (set);
10219 reg_mode[regno] = mode;
10220 switch (GET_CODE (src))
10222 case PLUS:
10224 rtx src0 = XEXP (src, 0);
10225 if (GET_CODE (src0) == REG)
10227 if (REGNO (src0) != regno
10228 || reg_offset[regno] != const0_rtx)
10230 reg_base_reg[regno] = REGNO (src0);
10231 reg_set_luid[regno] = move2add_luid;
10233 reg_offset[regno] = XEXP (src, 1);
10234 break;
10236 reg_set_luid[regno] = move2add_luid;
10237 reg_offset[regno] = set; /* Invalidate contents. */
10238 break;
10241 case REG:
10242 reg_base_reg[regno] = REGNO (SET_SRC (set));
10243 reg_offset[regno] = const0_rtx;
10244 reg_set_luid[regno] = move2add_luid;
10245 break;
10247 default:
10248 reg_base_reg[regno] = -1;
10249 reg_offset[regno] = SET_SRC (set);
10250 reg_set_luid[regno] = move2add_luid;
10251 break;
10254 else
10256 for (i = regno + HARD_REGNO_NREGS (regno, mode) - 1; i >= regno; i--)
10258 /* Indicate that this register has been recently written to,
10259 but the exact contents are not available. */
10260 reg_set_luid[i] = move2add_luid;
10261 reg_offset[i] = dst;
10266 #ifdef AUTO_INC_DEC
10267 static void
10268 add_auto_inc_notes (insn, x)
10269 rtx insn;
10270 rtx x;
10272 enum rtx_code code = GET_CODE (x);
10273 const char *fmt;
10274 int i, j;
10276 if (code == MEM && auto_inc_p (XEXP (x, 0)))
10278 REG_NOTES (insn)
10279 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
10280 return;
10283 /* Scan all the operand sub-expressions. */
10284 fmt = GET_RTX_FORMAT (code);
10285 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10287 if (fmt[i] == 'e')
10288 add_auto_inc_notes (insn, XEXP (x, i));
10289 else if (fmt[i] == 'E')
10290 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10291 add_auto_inc_notes (insn, XVECEXP (x, i, j));
10294 #endif