* friend.c (make_friend_class): Robustify.
[official-gcc.git] / gcc / reload1.c
blobcaa7c1048ec0e0ea625c01b1637e7c8cfc690b4f
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 int eliminate_regs_in_insn PROTO((rtx, int));
398 static void update_eliminable_offsets PROTO((void));
399 static void mark_not_eliminable PROTO((rtx, rtx));
400 static void set_initial_elim_offsets PROTO((void));
401 static void verify_initial_elim_offsets PROTO((void));
402 static void set_initial_label_offsets PROTO((void));
403 static void set_offsets_for_label PROTO((rtx));
404 static void init_elim_table PROTO((void));
405 static void update_eliminables PROTO((HARD_REG_SET *));
406 static void spill_hard_reg PROTO((int, FILE *, int));
407 static int finish_spills PROTO((int, FILE *));
408 static void ior_hard_reg_set PROTO((HARD_REG_SET *, HARD_REG_SET *));
409 static void scan_paradoxical_subregs PROTO((rtx));
410 static int hard_reg_use_compare PROTO((const PTR, const PTR));
411 static void count_pseudo PROTO((struct hard_reg_n_uses *, int));
412 static void order_regs_for_reload PROTO((struct insn_chain *));
413 static void reload_as_needed PROTO((int));
414 static void forget_old_reloads_1 PROTO((rtx, rtx));
415 static int reload_reg_class_lower PROTO((const PTR, const PTR));
416 static void mark_reload_reg_in_use PROTO((int, int, enum reload_type,
417 enum machine_mode));
418 static void clear_reload_reg_in_use PROTO((int, int, enum reload_type,
419 enum machine_mode));
420 static int reload_reg_free_p PROTO((int, int, enum reload_type));
421 static int reload_reg_free_for_value_p PROTO((int, int, enum reload_type, rtx, rtx, int, int));
422 static int reload_reg_reaches_end_p PROTO((int, int, enum reload_type));
423 static int allocate_reload_reg PROTO((struct insn_chain *, int, int,
424 int));
425 static void choose_reload_regs PROTO((struct insn_chain *));
426 static void merge_assigned_reloads PROTO((rtx));
427 static void emit_reload_insns PROTO((struct insn_chain *));
428 static void delete_output_reload PROTO((rtx, int, int));
429 static void delete_address_reloads PROTO((rtx, rtx));
430 static void delete_address_reloads_1 PROTO((rtx, rtx, rtx));
431 static rtx inc_for_reload PROTO((rtx, rtx, rtx, int));
432 static int constraint_accepts_reg_p PROTO((const char *, rtx));
433 static void reload_cse_regs_1 PROTO((rtx));
434 static void reload_cse_invalidate_regno PROTO((int, enum machine_mode, int));
435 static int reload_cse_mem_conflict_p PROTO((rtx, rtx));
436 static void reload_cse_invalidate_mem PROTO((rtx));
437 static void reload_cse_invalidate_rtx PROTO((rtx, rtx));
438 static int reload_cse_regno_equal_p PROTO((int, rtx, enum machine_mode));
439 static int reload_cse_noop_set_p PROTO((rtx, rtx));
440 static int reload_cse_simplify_set PROTO((rtx, rtx));
441 static int reload_cse_simplify_operands PROTO((rtx));
442 static void reload_cse_check_clobber PROTO((rtx, rtx));
443 static void reload_cse_record_set PROTO((rtx, rtx));
444 static void reload_combine PROTO((void));
445 static void reload_combine_note_use PROTO((rtx *, rtx));
446 static void reload_combine_note_store PROTO((rtx, rtx));
447 static void reload_cse_move2add PROTO((rtx));
448 static void move2add_note_store PROTO((rtx, rtx));
449 #ifdef AUTO_INC_DEC
450 static void add_auto_inc_notes PROTO((rtx, rtx));
451 #endif
452 static rtx gen_mode_int PROTO((enum machine_mode,
453 HOST_WIDE_INT));
454 extern void dump_needs PROTO((struct insn_chain *, FILE *));
456 /* Initialize the reload pass once per compilation. */
458 void
459 init_reload ()
461 register int i;
463 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
464 Set spill_indirect_levels to the number of levels such addressing is
465 permitted, zero if it is not permitted at all. */
467 register rtx tem
468 = gen_rtx_MEM (Pmode,
469 gen_rtx_PLUS (Pmode,
470 gen_rtx_REG (Pmode,
471 LAST_VIRTUAL_REGISTER + 1),
472 GEN_INT (4)));
473 spill_indirect_levels = 0;
475 while (memory_address_p (QImode, tem))
477 spill_indirect_levels++;
478 tem = gen_rtx_MEM (Pmode, tem);
481 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
483 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
484 indirect_symref_ok = memory_address_p (QImode, tem);
486 /* See if reg+reg is a valid (and offsettable) address. */
488 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
490 tem = gen_rtx_PLUS (Pmode,
491 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
492 gen_rtx_REG (Pmode, i));
494 /* This way, we make sure that reg+reg is an offsettable address. */
495 tem = plus_constant (tem, 4);
497 if (memory_address_p (QImode, tem))
499 double_reg_address_ok = 1;
500 break;
504 /* Initialize obstack for our rtl allocation. */
505 gcc_obstack_init (&reload_obstack);
506 reload_startobj = (char *) obstack_alloc (&reload_obstack, 0);
509 /* List of insn chains that are currently unused. */
510 static struct insn_chain *unused_insn_chains = 0;
512 /* Allocate an empty insn_chain structure. */
513 struct insn_chain *
514 new_insn_chain ()
516 struct insn_chain *c;
518 if (unused_insn_chains == 0)
520 c = (struct insn_chain *)
521 obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
522 c->live_before = OBSTACK_ALLOC_REG_SET (&reload_obstack);
523 c->live_after = OBSTACK_ALLOC_REG_SET (&reload_obstack);
525 else
527 c = unused_insn_chains;
528 unused_insn_chains = c->next;
530 c->is_caller_save_insn = 0;
531 c->need_operand_change = 0;
532 c->need_reload = 0;
533 c->need_elim = 0;
534 return c;
537 /* Small utility function to set all regs in hard reg set TO which are
538 allocated to pseudos in regset FROM. */
539 void
540 compute_use_by_pseudos (to, from)
541 HARD_REG_SET *to;
542 regset from;
544 int regno;
545 EXECUTE_IF_SET_IN_REG_SET
546 (from, FIRST_PSEUDO_REGISTER, regno,
548 int r = reg_renumber[regno];
549 int nregs;
550 if (r < 0)
552 /* reload_combine uses the information from
553 BASIC_BLOCK->global_live_at_start, which might still
554 contain registers that have not actually been allocated
555 since they have an equivalence. */
556 if (! reload_completed)
557 abort ();
559 else
561 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (regno));
562 while (nregs-- > 0)
563 SET_HARD_REG_BIT (*to, r + nregs);
568 /* Global variables used by reload and its subroutines. */
570 /* Set during calculate_needs if an insn needs register elimination. */
571 static int something_needs_elimination;
572 /* Set during calculate_needs if an insn needs an operand changed. */
573 int something_needs_operands_changed;
575 /* Nonzero means we couldn't get enough spill regs. */
576 static int failure;
578 /* Main entry point for the reload pass.
580 FIRST is the first insn of the function being compiled.
582 GLOBAL nonzero means we were called from global_alloc
583 and should attempt to reallocate any pseudoregs that we
584 displace from hard regs we will use for reloads.
585 If GLOBAL is zero, we do not have enough information to do that,
586 so any pseudo reg that is spilled must go to the stack.
588 DUMPFILE is the global-reg debugging dump file stream, or 0.
589 If it is nonzero, messages are written to it to describe
590 which registers are seized as reload regs, which pseudo regs
591 are spilled from them, and where the pseudo regs are reallocated to.
593 Return value is nonzero if reload failed
594 and we must not do any more for this function. */
597 reload (first, global, dumpfile)
598 rtx first;
599 int global;
600 FILE *dumpfile;
602 register int i;
603 register rtx insn;
604 register struct elim_table *ep;
606 /* The two pointers used to track the true location of the memory used
607 for label offsets. */
608 char *real_known_ptr = NULL_PTR;
609 int (*real_at_ptr)[NUM_ELIMINABLE_REGS];
611 /* Make sure even insns with volatile mem refs are recognizable. */
612 init_recog ();
614 failure = 0;
616 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
618 /* Make sure that the last insn in the chain
619 is not something that needs reloading. */
620 emit_note (NULL_PTR, NOTE_INSN_DELETED);
622 /* Enable find_equiv_reg to distinguish insns made by reload. */
623 reload_first_uid = get_max_uid ();
625 #ifdef SECONDARY_MEMORY_NEEDED
626 /* Initialize the secondary memory table. */
627 clear_secondary_mem ();
628 #endif
630 /* We don't have a stack slot for any spill reg yet. */
631 bzero ((char *) spill_stack_slot, sizeof spill_stack_slot);
632 bzero ((char *) spill_stack_slot_width, sizeof spill_stack_slot_width);
634 /* Initialize the save area information for caller-save, in case some
635 are needed. */
636 init_save_areas ();
638 /* Compute which hard registers are now in use
639 as homes for pseudo registers.
640 This is done here rather than (eg) in global_alloc
641 because this point is reached even if not optimizing. */
642 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
643 mark_home_live (i);
645 /* A function that receives a nonlocal goto must save all call-saved
646 registers. */
647 if (current_function_has_nonlocal_label)
648 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
650 if (! call_used_regs[i] && ! fixed_regs[i])
651 regs_ever_live[i] = 1;
654 /* Find all the pseudo registers that didn't get hard regs
655 but do have known equivalent constants or memory slots.
656 These include parameters (known equivalent to parameter slots)
657 and cse'd or loop-moved constant memory addresses.
659 Record constant equivalents in reg_equiv_constant
660 so they will be substituted by find_reloads.
661 Record memory equivalents in reg_mem_equiv so they can
662 be substituted eventually by altering the REG-rtx's. */
664 reg_equiv_constant = (rtx *) xcalloc (max_regno, sizeof (rtx));
665 reg_equiv_memory_loc = (rtx *) xcalloc (max_regno, sizeof (rtx));
666 reg_equiv_mem = (rtx *) xcalloc (max_regno, sizeof (rtx));
667 reg_equiv_init = (rtx *) xcalloc (max_regno, sizeof (rtx));
668 reg_equiv_address = (rtx *) xcalloc (max_regno, sizeof (rtx));
669 reg_max_ref_width = (int *) xcalloc (max_regno, sizeof (int));
670 reg_old_renumber = (short *) xcalloc (max_regno, sizeof (short));
671 bcopy ((PTR) reg_renumber, (PTR) reg_old_renumber, max_regno * sizeof (short));
672 pseudo_forbidden_regs
673 = (HARD_REG_SET *) xmalloc (max_regno * sizeof (HARD_REG_SET));
674 pseudo_previous_regs
675 = (HARD_REG_SET *) xcalloc (max_regno, sizeof (HARD_REG_SET));
677 CLEAR_HARD_REG_SET (bad_spill_regs_global);
679 /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
680 Also find all paradoxical subregs and find largest such for each pseudo.
681 On machines with small register classes, record hard registers that
682 are used for user variables. These can never be used for spills.
683 Also look for a "constant" NOTE_INSN_SETJMP. This means that all
684 caller-saved registers must be marked live. */
686 num_eliminable_invariants = 0;
687 for (insn = first; insn; insn = NEXT_INSN (insn))
689 rtx set = single_set (insn);
691 if (GET_CODE (insn) == NOTE && CONST_CALL_P (insn)
692 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
693 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
694 if (! call_used_regs[i])
695 regs_ever_live[i] = 1;
697 if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
699 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
700 if (note
701 #ifdef LEGITIMATE_PIC_OPERAND_P
702 && (! function_invariant_p (XEXP (note, 0))
703 || ! flag_pic
704 || LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))
705 #endif
708 rtx x = XEXP (note, 0);
709 i = REGNO (SET_DEST (set));
710 if (i > LAST_VIRTUAL_REGISTER)
712 if (GET_CODE (x) == MEM)
714 /* If the operand is a PLUS, the MEM may be shared,
715 so make sure we have an unshared copy here. */
716 if (GET_CODE (XEXP (x, 0)) == PLUS)
717 x = copy_rtx (x);
719 reg_equiv_memory_loc[i] = x;
721 else if (function_invariant_p (x))
723 if (GET_CODE (x) == PLUS)
725 /* This is PLUS of frame pointer and a constant,
726 and might be shared. Unshare it. */
727 reg_equiv_constant[i] = copy_rtx (x);
728 num_eliminable_invariants++;
730 else if (x == frame_pointer_rtx
731 || x == arg_pointer_rtx)
733 reg_equiv_constant[i] = x;
734 num_eliminable_invariants++;
736 else if (LEGITIMATE_CONSTANT_P (x))
737 reg_equiv_constant[i] = x;
738 else
739 reg_equiv_memory_loc[i]
740 = force_const_mem (GET_MODE (SET_DEST (set)), x);
742 else
743 continue;
745 /* If this register is being made equivalent to a MEM
746 and the MEM is not SET_SRC, the equivalencing insn
747 is one with the MEM as a SET_DEST and it occurs later.
748 So don't mark this insn now. */
749 if (GET_CODE (x) != MEM
750 || rtx_equal_p (SET_SRC (set), x))
751 reg_equiv_init[i]
752 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[i]);
757 /* If this insn is setting a MEM from a register equivalent to it,
758 this is the equivalencing insn. */
759 else if (set && GET_CODE (SET_DEST (set)) == MEM
760 && GET_CODE (SET_SRC (set)) == REG
761 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
762 && rtx_equal_p (SET_DEST (set),
763 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
764 reg_equiv_init[REGNO (SET_SRC (set))]
765 = gen_rtx_INSN_LIST (VOIDmode, insn,
766 reg_equiv_init[REGNO (SET_SRC (set))]);
768 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
769 scan_paradoxical_subregs (PATTERN (insn));
772 init_elim_table ();
774 num_labels = max_label_num () - get_first_label_num ();
776 /* Allocate the tables used to store offset information at labels. */
777 /* We used to use alloca here, but the size of what it would try to
778 allocate would occasionally cause it to exceed the stack limit and
779 cause a core dump. */
780 real_known_ptr = xmalloc (num_labels);
781 real_at_ptr
782 = (int (*)[NUM_ELIMINABLE_REGS])
783 xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
785 offsets_known_at = real_known_ptr - get_first_label_num ();
786 offsets_at
787 = (int (*)[NUM_ELIMINABLE_REGS]) (real_at_ptr - get_first_label_num ());
789 /* Alter each pseudo-reg rtx to contain its hard reg number.
790 Assign stack slots to the pseudos that lack hard regs or equivalents.
791 Do not touch virtual registers. */
793 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
794 alter_reg (i, -1);
796 /* If we have some registers we think can be eliminated, scan all insns to
797 see if there is an insn that sets one of these registers to something
798 other than itself plus a constant. If so, the register cannot be
799 eliminated. Doing this scan here eliminates an extra pass through the
800 main reload loop in the most common case where register elimination
801 cannot be done. */
802 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
803 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
804 || GET_CODE (insn) == CALL_INSN)
805 note_stores (PATTERN (insn), mark_not_eliminable);
807 maybe_fix_stack_asms ();
809 insns_need_reload = 0;
810 something_needs_elimination = 0;
812 /* Initialize to -1, which means take the first spill register. */
813 last_spill_reg = -1;
815 spilled_pseudos = ALLOCA_REG_SET ();
817 /* Spill any hard regs that we know we can't eliminate. */
818 CLEAR_HARD_REG_SET (used_spill_regs);
819 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
820 if (! ep->can_eliminate)
821 spill_hard_reg (ep->from, dumpfile, 1);
823 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
824 if (frame_pointer_needed)
825 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, dumpfile, 1);
826 #endif
827 finish_spills (global, dumpfile);
829 /* From now on, we may need to generate moves differently. We may also
830 allow modifications of insns which cause them to not be recognized.
831 Any such modifications will be cleaned up during reload itself. */
832 reload_in_progress = 1;
834 /* This loop scans the entire function each go-round
835 and repeats until one repetition spills no additional hard regs. */
836 for (;;)
838 int something_changed;
839 int did_spill;
840 struct insn_chain *chain;
842 HOST_WIDE_INT starting_frame_size;
844 /* Round size of stack frame to BIGGEST_ALIGNMENT. This must be done
845 here because the stack size may be a part of the offset computation
846 for register elimination, and there might have been new stack slots
847 created in the last iteration of this loop. */
848 assign_stack_local (BLKmode, 0, 0);
850 starting_frame_size = get_frame_size ();
852 set_initial_elim_offsets ();
853 set_initial_label_offsets ();
855 /* For each pseudo register that has an equivalent location defined,
856 try to eliminate any eliminable registers (such as the frame pointer)
857 assuming initial offsets for the replacement register, which
858 is the normal case.
860 If the resulting location is directly addressable, substitute
861 the MEM we just got directly for the old REG.
863 If it is not addressable but is a constant or the sum of a hard reg
864 and constant, it is probably not addressable because the constant is
865 out of range, in that case record the address; we will generate
866 hairy code to compute the address in a register each time it is
867 needed. Similarly if it is a hard register, but one that is not
868 valid as an address register.
870 If the location is not addressable, but does not have one of the
871 above forms, assign a stack slot. We have to do this to avoid the
872 potential of producing lots of reloads if, e.g., a location involves
873 a pseudo that didn't get a hard register and has an equivalent memory
874 location that also involves a pseudo that didn't get a hard register.
876 Perhaps at some point we will improve reload_when_needed handling
877 so this problem goes away. But that's very hairy. */
879 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
880 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
882 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
884 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
885 XEXP (x, 0)))
886 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
887 else if (CONSTANT_P (XEXP (x, 0))
888 || (GET_CODE (XEXP (x, 0)) == REG
889 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
890 || (GET_CODE (XEXP (x, 0)) == PLUS
891 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
892 && (REGNO (XEXP (XEXP (x, 0), 0))
893 < FIRST_PSEUDO_REGISTER)
894 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
895 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
896 else
898 /* Make a new stack slot. Then indicate that something
899 changed so we go back and recompute offsets for
900 eliminable registers because the allocation of memory
901 below might change some offset. reg_equiv_{mem,address}
902 will be set up for this pseudo on the next pass around
903 the loop. */
904 reg_equiv_memory_loc[i] = 0;
905 reg_equiv_init[i] = 0;
906 alter_reg (i, -1);
910 if (caller_save_needed)
911 setup_save_areas ();
913 /* If we allocated another stack slot, redo elimination bookkeeping. */
914 if (starting_frame_size != get_frame_size ())
915 continue;
917 if (caller_save_needed)
919 save_call_clobbered_regs ();
920 /* That might have allocated new insn_chain structures. */
921 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
924 calculate_needs_all_insns (global);
926 CLEAR_REG_SET (spilled_pseudos);
927 did_spill = 0;
929 something_changed = 0;
931 /* If we allocated any new memory locations, make another pass
932 since it might have changed elimination offsets. */
933 if (starting_frame_size != get_frame_size ())
934 something_changed = 1;
937 HARD_REG_SET to_spill;
938 CLEAR_HARD_REG_SET (to_spill);
939 update_eliminables (&to_spill);
940 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
941 if (TEST_HARD_REG_BIT (to_spill, i))
943 spill_hard_reg (i, dumpfile, 1);
944 did_spill = 1;
946 /* Regardless of the state of spills, if we previously had
947 a register that we thought we could eliminate, but no can
948 not eliminate, we must run another pass.
950 Consider pseudos which have an entry in reg_equiv_* which
951 reference an eliminable register. We must make another pass
952 to update reg_equiv_* so that we do not substitute in the
953 old value from when we thought the elimination could be
954 performed. */
955 something_changed = 1;
959 CLEAR_HARD_REG_SET (used_spill_regs);
960 /* Try to satisfy the needs for each insn. */
961 for (chain = insns_need_reload; chain != 0;
962 chain = chain->next_need_reload)
963 find_reload_regs (chain, dumpfile);
965 if (failure)
966 goto failed;
968 if (insns_need_reload != 0 || did_spill)
969 something_changed |= finish_spills (global, dumpfile);
971 if (! something_changed)
972 break;
974 if (caller_save_needed)
975 delete_caller_save_insns ();
978 /* If global-alloc was run, notify it of any register eliminations we have
979 done. */
980 if (global)
981 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
982 if (ep->can_eliminate)
983 mark_elimination (ep->from, ep->to);
985 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
986 If that insn didn't set the register (i.e., it copied the register to
987 memory), just delete that insn instead of the equivalencing insn plus
988 anything now dead. If we call delete_dead_insn on that insn, we may
989 delete the insn that actually sets the register if the register dies
990 there and that is incorrect. */
992 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
994 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
996 rtx list;
997 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
999 rtx equiv_insn = XEXP (list, 0);
1000 if (GET_CODE (equiv_insn) == NOTE)
1001 continue;
1002 if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1003 delete_dead_insn (equiv_insn);
1004 else
1006 PUT_CODE (equiv_insn, NOTE);
1007 NOTE_SOURCE_FILE (equiv_insn) = 0;
1008 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1014 /* Use the reload registers where necessary
1015 by generating move instructions to move the must-be-register
1016 values into or out of the reload registers. */
1018 if (insns_need_reload != 0 || something_needs_elimination
1019 || something_needs_operands_changed)
1021 int old_frame_size = get_frame_size ();
1023 reload_as_needed (global);
1025 if (old_frame_size != get_frame_size ())
1026 abort ();
1028 if (num_eliminable)
1029 verify_initial_elim_offsets ();
1032 /* If we were able to eliminate the frame pointer, show that it is no
1033 longer live at the start of any basic block. If it ls live by
1034 virtue of being in a pseudo, that pseudo will be marked live
1035 and hence the frame pointer will be known to be live via that
1036 pseudo. */
1038 if (! frame_pointer_needed)
1039 for (i = 0; i < n_basic_blocks; i++)
1040 CLEAR_REGNO_REG_SET (BASIC_BLOCK (i)->global_live_at_start,
1041 HARD_FRAME_POINTER_REGNUM);
1043 /* Come here (with failure set nonzero) if we can't get enough spill regs
1044 and we decide not to abort about it. */
1045 failed:
1047 reload_in_progress = 0;
1049 /* Now eliminate all pseudo regs by modifying them into
1050 their equivalent memory references.
1051 The REG-rtx's for the pseudos are modified in place,
1052 so all insns that used to refer to them now refer to memory.
1054 For a reg that has a reg_equiv_address, all those insns
1055 were changed by reloading so that no insns refer to it any longer;
1056 but the DECL_RTL of a variable decl may refer to it,
1057 and if so this causes the debugging info to mention the variable. */
1059 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1061 rtx addr = 0;
1062 int in_struct = 0;
1063 int is_scalar = 0;
1064 int is_readonly = 0;
1066 if (reg_equiv_memory_loc[i])
1068 in_struct = MEM_IN_STRUCT_P (reg_equiv_memory_loc[i]);
1069 is_scalar = MEM_SCALAR_P (reg_equiv_memory_loc[i]);
1070 is_readonly = RTX_UNCHANGING_P (reg_equiv_memory_loc[i]);
1073 if (reg_equiv_mem[i])
1074 addr = XEXP (reg_equiv_mem[i], 0);
1076 if (reg_equiv_address[i])
1077 addr = reg_equiv_address[i];
1079 if (addr)
1081 if (reg_renumber[i] < 0)
1083 rtx reg = regno_reg_rtx[i];
1084 PUT_CODE (reg, MEM);
1085 XEXP (reg, 0) = addr;
1086 REG_USERVAR_P (reg) = 0;
1087 RTX_UNCHANGING_P (reg) = is_readonly;
1088 MEM_IN_STRUCT_P (reg) = in_struct;
1089 MEM_SCALAR_P (reg) = is_scalar;
1090 /* We have no alias information about this newly created
1091 MEM. */
1092 MEM_ALIAS_SET (reg) = 0;
1094 else if (reg_equiv_mem[i])
1095 XEXP (reg_equiv_mem[i], 0) = addr;
1099 /* We must set reload_completed now since the cleanup_subreg_operands call
1100 below will re-recognize each insn and reload may have generated insns
1101 which are only valid during and after reload. */
1102 reload_completed = 1;
1104 /* Make a pass over all the insns and delete all USEs which we
1105 inserted only to tag a REG_EQUAL note on them. Remove all
1106 REG_DEAD and REG_UNUSED notes. Delete all CLOBBER insns and
1107 simplify (subreg (reg)) operands. Also remove all REG_RETVAL and
1108 REG_LIBCALL notes since they are no longer useful or accurate.
1109 Strip and regenerate REG_INC notes that may have been moved
1110 around. */
1112 for (insn = first; insn; insn = NEXT_INSN (insn))
1113 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1115 rtx *pnote;
1117 if ((GET_CODE (PATTERN (insn)) == USE
1118 && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1119 || GET_CODE (PATTERN (insn)) == CLOBBER)
1121 PUT_CODE (insn, NOTE);
1122 NOTE_SOURCE_FILE (insn) = 0;
1123 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1124 continue;
1127 pnote = &REG_NOTES (insn);
1128 while (*pnote != 0)
1130 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1131 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1132 || REG_NOTE_KIND (*pnote) == REG_INC
1133 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1134 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1135 *pnote = XEXP (*pnote, 1);
1136 else
1137 pnote = &XEXP (*pnote, 1);
1140 #ifdef AUTO_INC_DEC
1141 add_auto_inc_notes (insn, PATTERN (insn));
1142 #endif
1144 /* And simplify (subreg (reg)) if it appears as an operand. */
1145 cleanup_subreg_operands (insn);
1148 /* If we are doing stack checking, give a warning if this function's
1149 frame size is larger than we expect. */
1150 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1152 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1153 static int verbose_warned = 0;
1155 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1156 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1157 size += UNITS_PER_WORD;
1159 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1161 warning ("frame size too large for reliable stack checking");
1162 if (! verbose_warned)
1164 warning ("try reducing the number of local variables");
1165 verbose_warned = 1;
1170 /* Indicate that we no longer have known memory locations or constants. */
1171 if (reg_equiv_constant)
1172 free (reg_equiv_constant);
1173 reg_equiv_constant = 0;
1174 if (reg_equiv_memory_loc)
1175 free (reg_equiv_memory_loc);
1176 reg_equiv_memory_loc = 0;
1178 if (real_known_ptr)
1179 free (real_known_ptr);
1180 if (real_at_ptr)
1181 free (real_at_ptr);
1183 free (reg_equiv_mem);
1184 free (reg_equiv_init);
1185 free (reg_equiv_address);
1186 free (reg_max_ref_width);
1187 free (reg_old_renumber);
1188 free (pseudo_previous_regs);
1189 free (pseudo_forbidden_regs);
1191 FREE_REG_SET (spilled_pseudos);
1193 CLEAR_HARD_REG_SET (used_spill_regs);
1194 for (i = 0; i < n_spills; i++)
1195 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1197 /* Free all the insn_chain structures at once. */
1198 obstack_free (&reload_obstack, reload_startobj);
1199 unused_insn_chains = 0;
1201 return failure;
1204 /* Yet another special case. Unfortunately, reg-stack forces people to
1205 write incorrect clobbers in asm statements. These clobbers must not
1206 cause the register to appear in bad_spill_regs, otherwise we'll call
1207 fatal_insn later. We clear the corresponding regnos in the live
1208 register sets to avoid this.
1209 The whole thing is rather sick, I'm afraid. */
1210 static void
1211 maybe_fix_stack_asms ()
1213 #ifdef STACK_REGS
1214 const char *constraints[MAX_RECOG_OPERANDS];
1215 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1216 struct insn_chain *chain;
1218 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1220 int i, noperands;
1221 HARD_REG_SET clobbered, allowed;
1222 rtx pat;
1224 if (GET_RTX_CLASS (GET_CODE (chain->insn)) != 'i'
1225 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1226 continue;
1227 pat = PATTERN (chain->insn);
1228 if (GET_CODE (pat) != PARALLEL)
1229 continue;
1231 CLEAR_HARD_REG_SET (clobbered);
1232 CLEAR_HARD_REG_SET (allowed);
1234 /* First, make a mask of all stack regs that are clobbered. */
1235 for (i = 0; i < XVECLEN (pat, 0); i++)
1237 rtx t = XVECEXP (pat, 0, i);
1238 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1239 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1242 /* Get the operand values and constraints out of the insn. */
1243 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1244 constraints, operand_mode);
1246 /* For every operand, see what registers are allowed. */
1247 for (i = 0; i < noperands; i++)
1249 const char *p = constraints[i];
1250 /* For every alternative, we compute the class of registers allowed
1251 for reloading in CLS, and merge its contents into the reg set
1252 ALLOWED. */
1253 int cls = (int) NO_REGS;
1255 for (;;)
1257 char c = *p++;
1259 if (c == '\0' || c == ',' || c == '#')
1261 /* End of one alternative - mark the regs in the current
1262 class, and reset the class. */
1263 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1264 cls = NO_REGS;
1265 if (c == '#')
1266 do {
1267 c = *p++;
1268 } while (c != '\0' && c != ',');
1269 if (c == '\0')
1270 break;
1271 continue;
1274 switch (c)
1276 case '=': case '+': case '*': case '%': case '?': case '!':
1277 case '0': case '1': case '2': case '3': case '4': case 'm':
1278 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1279 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1280 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1281 case 'P':
1282 #ifdef EXTRA_CONSTRAINT
1283 case 'Q': case 'R': case 'S': case 'T': case 'U':
1284 #endif
1285 break;
1287 case 'p':
1288 cls = (int) reg_class_subunion[cls][(int) BASE_REG_CLASS];
1289 break;
1291 case 'g':
1292 case 'r':
1293 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1294 break;
1296 default:
1297 cls = (int) reg_class_subunion[cls][(int) REG_CLASS_FROM_LETTER (c)];
1302 /* Those of the registers which are clobbered, but allowed by the
1303 constraints, must be usable as reload registers. So clear them
1304 out of the life information. */
1305 AND_HARD_REG_SET (allowed, clobbered);
1306 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1307 if (TEST_HARD_REG_BIT (allowed, i))
1309 CLEAR_REGNO_REG_SET (chain->live_before, i);
1310 CLEAR_REGNO_REG_SET (chain->live_after, i);
1314 #endif
1318 /* Walk the chain of insns, and determine for each whether it needs reloads
1319 and/or eliminations. Build the corresponding insns_need_reload list, and
1320 set something_needs_elimination as appropriate. */
1321 static void
1322 calculate_needs_all_insns (global)
1323 int global;
1325 struct insn_chain **pprev_reload = &insns_need_reload;
1326 struct insn_chain **pchain;
1328 something_needs_elimination = 0;
1330 for (pchain = &reload_insn_chain; *pchain != 0; pchain = &(*pchain)->next)
1332 rtx insn;
1333 struct insn_chain *chain;
1335 chain = *pchain;
1336 insn = chain->insn;
1338 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1339 include REG_LABEL), we need to see what effects this has on the
1340 known offsets at labels. */
1342 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
1343 || (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1344 && REG_NOTES (insn) != 0))
1345 set_label_offsets (insn, insn, 0);
1347 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1349 rtx old_body = PATTERN (insn);
1350 int old_code = INSN_CODE (insn);
1351 rtx old_notes = REG_NOTES (insn);
1352 int did_elimination = 0;
1353 int operands_changed = 0;
1354 rtx set = single_set (insn);
1356 /* Skip insns that only set an equivalence. */
1357 if (set && GET_CODE (SET_DEST (set)) == REG
1358 && reg_renumber[REGNO (SET_DEST (set))] < 0
1359 && reg_equiv_constant[REGNO (SET_DEST (set))])
1361 /* Must clear out the shortcuts, in case they were set last
1362 time through. */
1363 chain->need_elim = 0;
1364 chain->need_reload = 0;
1365 chain->need_operand_change = 0;
1366 continue;
1369 /* If needed, eliminate any eliminable registers. */
1370 if (num_eliminable || num_eliminable_invariants)
1371 did_elimination = eliminate_regs_in_insn (insn, 0);
1373 /* Analyze the instruction. */
1374 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1375 global, spill_reg_order);
1377 /* If a no-op set needs more than one reload, this is likely
1378 to be something that needs input address reloads. We
1379 can't get rid of this cleanly later, and it is of no use
1380 anyway, so discard it now.
1381 We only do this when expensive_optimizations is enabled,
1382 since this complements reload inheritance / output
1383 reload deletion, and it can make debugging harder. */
1384 if (flag_expensive_optimizations && n_reloads > 1)
1386 rtx set = single_set (insn);
1387 if (set
1388 && SET_SRC (set) == SET_DEST (set)
1389 && GET_CODE (SET_SRC (set)) == REG
1390 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1392 PUT_CODE (insn, NOTE);
1393 NOTE_SOURCE_FILE (insn) = 0;
1394 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1395 continue;
1398 if (num_eliminable)
1399 update_eliminable_offsets ();
1401 /* Remember for later shortcuts which insns had any reloads or
1402 register eliminations. */
1403 chain->need_elim = did_elimination;
1404 chain->need_reload = n_reloads > 0;
1405 chain->need_operand_change = operands_changed;
1407 /* Discard any register replacements done. */
1408 if (did_elimination)
1410 obstack_free (&reload_obstack, reload_firstobj);
1411 PATTERN (insn) = old_body;
1412 INSN_CODE (insn) = old_code;
1413 REG_NOTES (insn) = old_notes;
1414 something_needs_elimination = 1;
1417 something_needs_operands_changed |= operands_changed;
1419 if (n_reloads != 0)
1421 *pprev_reload = chain;
1422 pprev_reload = &chain->next_need_reload;
1424 calculate_needs (chain);
1428 *pprev_reload = 0;
1431 /* Compute the most additional registers needed by one instruction,
1432 given by CHAIN. Collect information separately for each class of regs.
1434 To compute the number of reload registers of each class needed for an
1435 insn, we must simulate what choose_reload_regs can do. We do this by
1436 splitting an insn into an "input" and an "output" part. RELOAD_OTHER
1437 reloads are used in both. The input part uses those reloads,
1438 RELOAD_FOR_INPUT reloads, which must be live over the entire input section
1439 of reloads, and the maximum of all the RELOAD_FOR_INPUT_ADDRESS and
1440 RELOAD_FOR_OPERAND_ADDRESS reloads, which conflict with the inputs.
1442 The registers needed for output are RELOAD_OTHER and RELOAD_FOR_OUTPUT,
1443 which are live for the entire output portion, and the maximum of all the
1444 RELOAD_FOR_OUTPUT_ADDRESS reloads for each operand.
1446 The total number of registers needed is the maximum of the
1447 inputs and outputs. */
1449 static void
1450 calculate_needs (chain)
1451 struct insn_chain *chain;
1453 int i;
1455 /* Each `struct needs' corresponds to one RELOAD_... type. */
1456 struct {
1457 struct needs other;
1458 struct needs input;
1459 struct needs output;
1460 struct needs insn;
1461 struct needs other_addr;
1462 struct needs op_addr;
1463 struct needs op_addr_reload;
1464 struct needs in_addr[MAX_RECOG_OPERANDS];
1465 struct needs in_addr_addr[MAX_RECOG_OPERANDS];
1466 struct needs out_addr[MAX_RECOG_OPERANDS];
1467 struct needs out_addr_addr[MAX_RECOG_OPERANDS];
1468 } insn_needs;
1470 bzero ((char *) chain->group_size, sizeof chain->group_size);
1471 for (i = 0; i < N_REG_CLASSES; i++)
1472 chain->group_mode[i] = VOIDmode;
1473 bzero ((char *) &insn_needs, sizeof insn_needs);
1475 /* Count each reload once in every class
1476 containing the reload's own class. */
1478 for (i = 0; i < n_reloads; i++)
1480 register enum reg_class *p;
1481 enum reg_class class = rld[i].class;
1482 int size;
1483 enum machine_mode mode;
1484 struct needs *this_needs;
1486 /* Don't count the dummy reloads, for which one of the
1487 regs mentioned in the insn can be used for reloading.
1488 Don't count optional reloads.
1489 Don't count reloads that got combined with others. */
1490 if (rld[i].reg_rtx != 0
1491 || rld[i].optional != 0
1492 || (rld[i].out == 0 && rld[i].in == 0
1493 && ! rld[i].secondary_p))
1494 continue;
1496 mode = rld[i].inmode;
1497 if (GET_MODE_SIZE (rld[i].outmode) > GET_MODE_SIZE (mode))
1498 mode = rld[i].outmode;
1499 size = CLASS_MAX_NREGS (class, mode);
1501 /* Decide which time-of-use to count this reload for. */
1502 switch (rld[i].when_needed)
1504 case RELOAD_OTHER:
1505 this_needs = &insn_needs.other;
1506 break;
1507 case RELOAD_FOR_INPUT:
1508 this_needs = &insn_needs.input;
1509 break;
1510 case RELOAD_FOR_OUTPUT:
1511 this_needs = &insn_needs.output;
1512 break;
1513 case RELOAD_FOR_INSN:
1514 this_needs = &insn_needs.insn;
1515 break;
1516 case RELOAD_FOR_OTHER_ADDRESS:
1517 this_needs = &insn_needs.other_addr;
1518 break;
1519 case RELOAD_FOR_INPUT_ADDRESS:
1520 this_needs = &insn_needs.in_addr[rld[i].opnum];
1521 break;
1522 case RELOAD_FOR_INPADDR_ADDRESS:
1523 this_needs = &insn_needs.in_addr_addr[rld[i].opnum];
1524 break;
1525 case RELOAD_FOR_OUTPUT_ADDRESS:
1526 this_needs = &insn_needs.out_addr[rld[i].opnum];
1527 break;
1528 case RELOAD_FOR_OUTADDR_ADDRESS:
1529 this_needs = &insn_needs.out_addr_addr[rld[i].opnum];
1530 break;
1531 case RELOAD_FOR_OPERAND_ADDRESS:
1532 this_needs = &insn_needs.op_addr;
1533 break;
1534 case RELOAD_FOR_OPADDR_ADDR:
1535 this_needs = &insn_needs.op_addr_reload;
1536 break;
1537 default:
1538 abort();
1541 if (size > 1)
1543 enum machine_mode other_mode, allocate_mode;
1545 /* Count number of groups needed separately from
1546 number of individual regs needed. */
1547 this_needs->groups[(int) class]++;
1548 p = reg_class_superclasses[(int) class];
1549 while (*p != LIM_REG_CLASSES)
1550 this_needs->groups[(int) *p++]++;
1552 /* Record size and mode of a group of this class. */
1553 /* If more than one size group is needed,
1554 make all groups the largest needed size. */
1555 if (chain->group_size[(int) class] < size)
1557 other_mode = chain->group_mode[(int) class];
1558 allocate_mode = mode;
1560 chain->group_size[(int) class] = size;
1561 chain->group_mode[(int) class] = mode;
1563 else
1565 other_mode = mode;
1566 allocate_mode = chain->group_mode[(int) class];
1569 /* Crash if two dissimilar machine modes both need
1570 groups of consecutive regs of the same class. */
1572 if (other_mode != VOIDmode && other_mode != allocate_mode
1573 && ! modes_equiv_for_class_p (allocate_mode,
1574 other_mode, class))
1575 fatal_insn ("Two dissimilar machine modes both need groups of consecutive regs of the same class",
1576 chain->insn);
1578 else if (size == 1)
1580 this_needs->regs[(unsigned char)rld[i].nongroup][(int) class] += 1;
1581 p = reg_class_superclasses[(int) class];
1582 while (*p != LIM_REG_CLASSES)
1583 this_needs->regs[(unsigned char)rld[i].nongroup][(int) *p++] += 1;
1585 else
1586 abort ();
1589 /* All reloads have been counted for this insn;
1590 now merge the various times of use.
1591 This sets insn_needs, etc., to the maximum total number
1592 of registers needed at any point in this insn. */
1594 for (i = 0; i < N_REG_CLASSES; i++)
1596 int j, in_max, out_max;
1598 /* Compute normal and nongroup needs. */
1599 for (j = 0; j <= 1; j++)
1601 int k;
1602 for (in_max = 0, out_max = 0, k = 0; k < reload_n_operands; k++)
1604 in_max = MAX (in_max,
1605 (insn_needs.in_addr[k].regs[j][i]
1606 + insn_needs.in_addr_addr[k].regs[j][i]));
1607 out_max = MAX (out_max, insn_needs.out_addr[k].regs[j][i]);
1608 out_max = MAX (out_max,
1609 insn_needs.out_addr_addr[k].regs[j][i]);
1612 /* RELOAD_FOR_INSN reloads conflict with inputs, outputs,
1613 and operand addresses but not things used to reload
1614 them. Similarly, RELOAD_FOR_OPERAND_ADDRESS reloads
1615 don't conflict with things needed to reload inputs or
1616 outputs. */
1618 in_max = MAX (MAX (insn_needs.op_addr.regs[j][i],
1619 insn_needs.op_addr_reload.regs[j][i]),
1620 in_max);
1622 out_max = MAX (out_max, insn_needs.insn.regs[j][i]);
1624 insn_needs.input.regs[j][i]
1625 = MAX (insn_needs.input.regs[j][i]
1626 + insn_needs.op_addr.regs[j][i]
1627 + insn_needs.insn.regs[j][i],
1628 in_max + insn_needs.input.regs[j][i]);
1630 insn_needs.output.regs[j][i] += out_max;
1631 insn_needs.other.regs[j][i]
1632 += MAX (MAX (insn_needs.input.regs[j][i],
1633 insn_needs.output.regs[j][i]),
1634 insn_needs.other_addr.regs[j][i]);
1638 /* Now compute group needs. */
1639 for (in_max = 0, out_max = 0, j = 0; j < reload_n_operands; j++)
1641 in_max = MAX (in_max, insn_needs.in_addr[j].groups[i]);
1642 in_max = MAX (in_max, insn_needs.in_addr_addr[j].groups[i]);
1643 out_max = MAX (out_max, insn_needs.out_addr[j].groups[i]);
1644 out_max = MAX (out_max, insn_needs.out_addr_addr[j].groups[i]);
1647 in_max = MAX (MAX (insn_needs.op_addr.groups[i],
1648 insn_needs.op_addr_reload.groups[i]),
1649 in_max);
1650 out_max = MAX (out_max, insn_needs.insn.groups[i]);
1652 insn_needs.input.groups[i]
1653 = MAX (insn_needs.input.groups[i]
1654 + insn_needs.op_addr.groups[i]
1655 + insn_needs.insn.groups[i],
1656 in_max + insn_needs.input.groups[i]);
1658 insn_needs.output.groups[i] += out_max;
1659 insn_needs.other.groups[i]
1660 += MAX (MAX (insn_needs.input.groups[i],
1661 insn_needs.output.groups[i]),
1662 insn_needs.other_addr.groups[i]);
1665 /* Record the needs for later. */
1666 chain->need = insn_needs.other;
1669 /* Find a group of exactly 2 registers.
1671 First try to fill out the group by spilling a single register which
1672 would allow completion of the group.
1674 Then try to create a new group from a pair of registers, neither of
1675 which are explicitly used.
1677 Then try to create a group from any pair of registers. */
1679 static void
1680 find_tworeg_group (chain, class, dumpfile)
1681 struct insn_chain *chain;
1682 int class;
1683 FILE *dumpfile;
1685 int i;
1686 /* First, look for a register that will complete a group. */
1687 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1689 int j, other;
1691 j = potential_reload_regs[i];
1692 if (j >= 0 && ! TEST_HARD_REG_BIT (bad_spill_regs, j)
1693 && ((j > 0 && (other = j - 1, spill_reg_order[other] >= 0)
1694 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1695 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1696 && HARD_REGNO_MODE_OK (other, chain->group_mode[class])
1697 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, other)
1698 /* We don't want one part of another group.
1699 We could get "two groups" that overlap! */
1700 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, other))
1701 || (j < FIRST_PSEUDO_REGISTER - 1
1702 && (other = j + 1, spill_reg_order[other] >= 0)
1703 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1704 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1705 && HARD_REGNO_MODE_OK (j, chain->group_mode[class])
1706 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, other)
1707 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, other))))
1709 register enum reg_class *p;
1711 /* We have found one that will complete a group,
1712 so count off one group as provided. */
1713 chain->need.groups[class]--;
1714 p = reg_class_superclasses[class];
1715 while (*p != LIM_REG_CLASSES)
1717 if (chain->group_size [(int) *p] <= chain->group_size [class])
1718 chain->need.groups[(int) *p]--;
1719 p++;
1722 /* Indicate both these regs are part of a group. */
1723 SET_HARD_REG_BIT (chain->counted_for_groups, j);
1724 SET_HARD_REG_BIT (chain->counted_for_groups, other);
1725 break;
1728 /* We can't complete a group, so start one. */
1729 if (i == FIRST_PSEUDO_REGISTER)
1730 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1732 int j, k;
1733 j = potential_reload_regs[i];
1734 /* Verify that J+1 is a potential reload reg. */
1735 for (k = 0; k < FIRST_PSEUDO_REGISTER; k++)
1736 if (potential_reload_regs[k] == j + 1)
1737 break;
1738 if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1739 && k < FIRST_PSEUDO_REGISTER
1740 && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1741 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1742 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1743 && HARD_REGNO_MODE_OK (j, chain->group_mode[class])
1744 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, j + 1)
1745 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + 1))
1746 break;
1749 /* I should be the index in potential_reload_regs
1750 of the new reload reg we have found. */
1752 new_spill_reg (chain, i, class, 0, dumpfile);
1755 /* Find a group of more than 2 registers.
1756 Look for a sufficient sequence of unspilled registers, and spill them all
1757 at once. */
1759 static void
1760 find_group (chain, class, dumpfile)
1761 struct insn_chain *chain;
1762 int class;
1763 FILE *dumpfile;
1765 int i;
1767 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1769 int j = potential_reload_regs[i];
1771 if (j >= 0
1772 && j + chain->group_size[class] <= FIRST_PSEUDO_REGISTER
1773 && HARD_REGNO_MODE_OK (j, chain->group_mode[class]))
1775 int k;
1776 /* Check each reg in the sequence. */
1777 for (k = 0; k < chain->group_size[class]; k++)
1778 if (! (spill_reg_order[j + k] < 0
1779 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + k)
1780 && TEST_HARD_REG_BIT (reg_class_contents[class], j + k)))
1781 break;
1782 /* We got a full sequence, so spill them all. */
1783 if (k == chain->group_size[class])
1785 register enum reg_class *p;
1786 for (k = 0; k < chain->group_size[class]; k++)
1788 int idx;
1789 SET_HARD_REG_BIT (chain->counted_for_groups, j + k);
1790 for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
1791 if (potential_reload_regs[idx] == j + k)
1792 break;
1793 new_spill_reg (chain, idx, class, 0, dumpfile);
1796 /* We have found one that will complete a group,
1797 so count off one group as provided. */
1798 chain->need.groups[class]--;
1799 p = reg_class_superclasses[class];
1800 while (*p != LIM_REG_CLASSES)
1802 if (chain->group_size [(int) *p]
1803 <= chain->group_size [class])
1804 chain->need.groups[(int) *p]--;
1805 p++;
1807 return;
1811 /* There are no groups left. */
1812 spill_failure (chain->insn);
1813 failure = 1;
1816 /* If pseudo REG conflicts with one of our reload registers, mark it as
1817 spilled. */
1818 static void
1819 maybe_mark_pseudo_spilled (reg)
1820 int reg;
1822 int i;
1823 int r = reg_renumber[reg];
1824 int nregs;
1826 if (r < 0)
1827 abort ();
1828 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
1829 for (i = 0; i < n_spills; i++)
1830 if (r <= spill_regs[i] && r + nregs > spill_regs[i])
1832 SET_REGNO_REG_SET (spilled_pseudos, reg);
1833 return;
1837 /* Find more reload regs to satisfy the remaining need of an insn, which
1838 is given by CHAIN.
1839 Do it by ascending class number, since otherwise a reg
1840 might be spilled for a big class and might fail to count
1841 for a smaller class even though it belongs to that class.
1843 Count spilled regs in `spills', and add entries to
1844 `spill_regs' and `spill_reg_order'.
1846 ??? Note there is a problem here.
1847 When there is a need for a group in a high-numbered class,
1848 and also need for non-group regs that come from a lower class,
1849 the non-group regs are chosen first. If there aren't many regs,
1850 they might leave no room for a group.
1852 This was happening on the 386. To fix it, we added the code
1853 that calls possible_group_p, so that the lower class won't
1854 break up the last possible group.
1856 Really fixing the problem would require changes above
1857 in counting the regs already spilled, and in choose_reload_regs.
1858 It might be hard to avoid introducing bugs there. */
1860 static void
1861 find_reload_regs (chain, dumpfile)
1862 struct insn_chain *chain;
1863 FILE *dumpfile;
1865 int i, class;
1866 short *group_needs = chain->need.groups;
1867 short *simple_needs = chain->need.regs[0];
1868 short *nongroup_needs = chain->need.regs[1];
1870 if (dumpfile)
1871 fprintf (dumpfile, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1873 /* Compute the order of preference for hard registers to spill.
1874 Store them by decreasing preference in potential_reload_regs. */
1876 order_regs_for_reload (chain);
1878 /* So far, no hard regs have been spilled. */
1879 n_spills = 0;
1880 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1881 spill_reg_order[i] = -1;
1883 CLEAR_HARD_REG_SET (chain->used_spill_regs);
1884 CLEAR_HARD_REG_SET (chain->counted_for_groups);
1885 CLEAR_HARD_REG_SET (chain->counted_for_nongroups);
1887 for (class = 0; class < N_REG_CLASSES; class++)
1889 /* First get the groups of registers.
1890 If we got single registers first, we might fragment
1891 possible groups. */
1892 while (group_needs[class] > 0)
1894 /* If any single spilled regs happen to form groups,
1895 count them now. Maybe we don't really need
1896 to spill another group. */
1897 count_possible_groups (chain, class);
1899 if (group_needs[class] <= 0)
1900 break;
1902 /* Groups of size 2, the only groups used on most machines,
1903 are treated specially. */
1904 if (chain->group_size[class] == 2)
1905 find_tworeg_group (chain, class, dumpfile);
1906 else
1907 find_group (chain, class, dumpfile);
1908 if (failure)
1909 return;
1912 /* Now similarly satisfy all need for single registers. */
1914 while (simple_needs[class] > 0 || nongroup_needs[class] > 0)
1916 /* If we spilled enough regs, but they weren't counted
1917 against the non-group need, see if we can count them now.
1918 If so, we can avoid some actual spilling. */
1919 if (simple_needs[class] <= 0 && nongroup_needs[class] > 0)
1920 for (i = 0; i < n_spills; i++)
1922 int regno = spill_regs[i];
1923 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1924 && !TEST_HARD_REG_BIT (chain->counted_for_groups, regno)
1925 && !TEST_HARD_REG_BIT (chain->counted_for_nongroups, regno)
1926 && nongroup_needs[class] > 0)
1928 register enum reg_class *p;
1930 SET_HARD_REG_BIT (chain->counted_for_nongroups, regno);
1931 nongroup_needs[class]--;
1932 p = reg_class_superclasses[class];
1933 while (*p != LIM_REG_CLASSES)
1934 nongroup_needs[(int) *p++]--;
1938 if (simple_needs[class] <= 0 && nongroup_needs[class] <= 0)
1939 break;
1941 /* Consider the potential reload regs that aren't
1942 yet in use as reload regs, in order of preference.
1943 Find the most preferred one that's in this class. */
1945 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1947 int regno = potential_reload_regs[i];
1948 if (regno >= 0
1949 && TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1950 /* If this reg will not be available for groups,
1951 pick one that does not foreclose possible groups.
1952 This is a kludge, and not very general,
1953 but it should be sufficient to make the 386 work,
1954 and the problem should not occur on machines with
1955 more registers. */
1956 && (nongroup_needs[class] == 0
1957 || possible_group_p (chain, regno)))
1958 break;
1961 /* If we couldn't get a register, try to get one even if we
1962 might foreclose possible groups. This may cause problems
1963 later, but that's better than aborting now, since it is
1964 possible that we will, in fact, be able to form the needed
1965 group even with this allocation. */
1967 if (i >= FIRST_PSEUDO_REGISTER
1968 && asm_noperands (chain->insn) < 0)
1969 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1970 if (potential_reload_regs[i] >= 0
1971 && TEST_HARD_REG_BIT (reg_class_contents[class],
1972 potential_reload_regs[i]))
1973 break;
1975 /* I should be the index in potential_reload_regs
1976 of the new reload reg we have found. */
1978 new_spill_reg (chain, i, class, 1, dumpfile);
1979 if (failure)
1980 return;
1984 /* We know which hard regs to use, now mark the pseudos that live in them
1985 as needing to be kicked out. */
1986 EXECUTE_IF_SET_IN_REG_SET
1987 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
1989 maybe_mark_pseudo_spilled (i);
1991 EXECUTE_IF_SET_IN_REG_SET
1992 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
1994 maybe_mark_pseudo_spilled (i);
1997 IOR_HARD_REG_SET (used_spill_regs, chain->used_spill_regs);
2000 void
2001 dump_needs (chain, dumpfile)
2002 struct insn_chain *chain;
2003 FILE *dumpfile;
2005 static const char * const reg_class_names[] = REG_CLASS_NAMES;
2006 int i;
2007 struct needs *n = &chain->need;
2009 for (i = 0; i < N_REG_CLASSES; i++)
2011 if (n->regs[i][0] > 0)
2012 fprintf (dumpfile,
2013 ";; Need %d reg%s of class %s.\n",
2014 n->regs[i][0], n->regs[i][0] == 1 ? "" : "s",
2015 reg_class_names[i]);
2016 if (n->regs[i][1] > 0)
2017 fprintf (dumpfile,
2018 ";; Need %d nongroup reg%s of class %s.\n",
2019 n->regs[i][1], n->regs[i][1] == 1 ? "" : "s",
2020 reg_class_names[i]);
2021 if (n->groups[i] > 0)
2022 fprintf (dumpfile,
2023 ";; Need %d group%s (%smode) of class %s.\n",
2024 n->groups[i], n->groups[i] == 1 ? "" : "s",
2025 GET_MODE_NAME(chain->group_mode[i]),
2026 reg_class_names[i]);
2030 /* Delete all insns that were inserted by emit_caller_save_insns during
2031 this iteration. */
2032 static void
2033 delete_caller_save_insns ()
2035 struct insn_chain *c = reload_insn_chain;
2037 while (c != 0)
2039 while (c != 0 && c->is_caller_save_insn)
2041 struct insn_chain *next = c->next;
2042 rtx insn = c->insn;
2044 if (insn == BLOCK_HEAD (c->block))
2045 BLOCK_HEAD (c->block) = NEXT_INSN (insn);
2046 if (insn == BLOCK_END (c->block))
2047 BLOCK_END (c->block) = PREV_INSN (insn);
2048 if (c == reload_insn_chain)
2049 reload_insn_chain = next;
2051 if (NEXT_INSN (insn) != 0)
2052 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2053 if (PREV_INSN (insn) != 0)
2054 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2056 if (next)
2057 next->prev = c->prev;
2058 if (c->prev)
2059 c->prev->next = next;
2060 c->next = unused_insn_chains;
2061 unused_insn_chains = c;
2062 c = next;
2064 if (c != 0)
2065 c = c->next;
2069 /* Nonzero if, after spilling reg REGNO for non-groups,
2070 it will still be possible to find a group if we still need one. */
2072 static int
2073 possible_group_p (chain, regno)
2074 struct insn_chain *chain;
2075 int regno;
2077 int i;
2078 int class = (int) NO_REGS;
2080 for (i = 0; i < (int) N_REG_CLASSES; i++)
2081 if (chain->need.groups[i] > 0)
2083 class = i;
2084 break;
2087 if (class == (int) NO_REGS)
2088 return 1;
2090 /* Consider each pair of consecutive registers. */
2091 for (i = 0; i < FIRST_PSEUDO_REGISTER - 1; i++)
2093 /* Ignore pairs that include reg REGNO. */
2094 if (i == regno || i + 1 == regno)
2095 continue;
2097 /* Ignore pairs that are outside the class that needs the group.
2098 ??? Here we fail to handle the case where two different classes
2099 independently need groups. But this never happens with our
2100 current machine descriptions. */
2101 if (! (TEST_HARD_REG_BIT (reg_class_contents[class], i)
2102 && TEST_HARD_REG_BIT (reg_class_contents[class], i + 1)))
2103 continue;
2105 /* A pair of consecutive regs we can still spill does the trick. */
2106 if (spill_reg_order[i] < 0 && spill_reg_order[i + 1] < 0
2107 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2108 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1))
2109 return 1;
2111 /* A pair of one already spilled and one we can spill does it
2112 provided the one already spilled is not otherwise reserved. */
2113 if (spill_reg_order[i] < 0
2114 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2115 && spill_reg_order[i + 1] >= 0
2116 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, i + 1)
2117 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, i + 1))
2118 return 1;
2119 if (spill_reg_order[i + 1] < 0
2120 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1)
2121 && spill_reg_order[i] >= 0
2122 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, i)
2123 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, i))
2124 return 1;
2127 return 0;
2130 /* Count any groups of CLASS that can be formed from the registers recently
2131 spilled. */
2133 static void
2134 count_possible_groups (chain, class)
2135 struct insn_chain *chain;
2136 int class;
2138 HARD_REG_SET new;
2139 int i, j;
2141 /* Now find all consecutive groups of spilled registers
2142 and mark each group off against the need for such groups.
2143 But don't count them against ordinary need, yet. */
2145 if (chain->group_size[class] == 0)
2146 return;
2148 CLEAR_HARD_REG_SET (new);
2150 /* Make a mask of all the regs that are spill regs in class I. */
2151 for (i = 0; i < n_spills; i++)
2153 int regno = spill_regs[i];
2155 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
2156 && ! TEST_HARD_REG_BIT (chain->counted_for_groups, regno)
2157 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups, regno))
2158 SET_HARD_REG_BIT (new, regno);
2161 /* Find each consecutive group of them. */
2162 for (i = 0; i < FIRST_PSEUDO_REGISTER && chain->need.groups[class] > 0; i++)
2163 if (TEST_HARD_REG_BIT (new, i)
2164 && i + chain->group_size[class] <= FIRST_PSEUDO_REGISTER
2165 && HARD_REGNO_MODE_OK (i, chain->group_mode[class]))
2167 for (j = 1; j < chain->group_size[class]; j++)
2168 if (! TEST_HARD_REG_BIT (new, i + j))
2169 break;
2171 if (j == chain->group_size[class])
2173 /* We found a group. Mark it off against this class's need for
2174 groups, and against each superclass too. */
2175 register enum reg_class *p;
2177 chain->need.groups[class]--;
2178 p = reg_class_superclasses[class];
2179 while (*p != LIM_REG_CLASSES)
2181 if (chain->group_size [(int) *p] <= chain->group_size [class])
2182 chain->need.groups[(int) *p]--;
2183 p++;
2186 /* Don't count these registers again. */
2187 for (j = 0; j < chain->group_size[class]; j++)
2188 SET_HARD_REG_BIT (chain->counted_for_groups, i + j);
2191 /* Skip to the last reg in this group. When i is incremented above,
2192 it will then point to the first reg of the next possible group. */
2193 i += j - 1;
2197 /* ALLOCATE_MODE is a register mode that needs to be reloaded. OTHER_MODE is
2198 another mode that needs to be reloaded for the same register class CLASS.
2199 If any reg in CLASS allows ALLOCATE_MODE but not OTHER_MODE, fail.
2200 ALLOCATE_MODE will never be smaller than OTHER_MODE.
2202 This code used to also fail if any reg in CLASS allows OTHER_MODE but not
2203 ALLOCATE_MODE. This test is unnecessary, because we will never try to put
2204 something of mode ALLOCATE_MODE into an OTHER_MODE register. Testing this
2205 causes unnecessary failures on machines requiring alignment of register
2206 groups when the two modes are different sizes, because the larger mode has
2207 more strict alignment rules than the smaller mode. */
2209 static int
2210 modes_equiv_for_class_p (allocate_mode, other_mode, class)
2211 enum machine_mode allocate_mode, other_mode;
2212 enum reg_class class;
2214 register int regno;
2215 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2217 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno)
2218 && HARD_REGNO_MODE_OK (regno, allocate_mode)
2219 && ! HARD_REGNO_MODE_OK (regno, other_mode))
2220 return 0;
2222 return 1;
2225 /* Handle the failure to find a register to spill.
2226 INSN should be one of the insns which needed this particular spill reg. */
2228 static void
2229 spill_failure (insn)
2230 rtx insn;
2232 if (asm_noperands (PATTERN (insn)) >= 0)
2233 error_for_asm (insn, "`asm' needs too many reloads");
2234 else
2235 fatal_insn ("Unable to find a register to spill.", insn);
2238 /* Add a new register to the tables of available spill-registers.
2239 CHAIN is the insn for which the register will be used; we decrease the
2240 needs of that insn.
2241 I is the index of this register in potential_reload_regs.
2242 CLASS is the regclass whose need is being satisfied.
2243 NONGROUP is 0 if this register is part of a group.
2244 DUMPFILE is the same as the one that `reload' got. */
2246 static void
2247 new_spill_reg (chain, i, class, nongroup, dumpfile)
2248 struct insn_chain *chain;
2249 int i;
2250 int class;
2251 int nongroup;
2252 FILE *dumpfile;
2254 register enum reg_class *p;
2255 int regno = potential_reload_regs[i];
2257 if (i >= FIRST_PSEUDO_REGISTER)
2259 spill_failure (chain->insn);
2260 failure = 1;
2261 return;
2264 if (TEST_HARD_REG_BIT (bad_spill_regs, regno))
2266 static const char * const reg_class_names[] = REG_CLASS_NAMES;
2268 if (asm_noperands (PATTERN (chain->insn)) < 0)
2270 /* The error message is still correct - we know only that it wasn't
2271 an asm statement that caused the problem, but one of the global
2272 registers declared by the users might have screwed us. */
2273 error ("fixed or forbidden register %d (%s) was spilled for class %s.",
2274 regno, reg_names[regno], reg_class_names[class]);
2275 error ("This may be due to a compiler bug or to impossible asm");
2276 error ("statements or clauses.");
2277 fatal_insn ("This is the instruction:", chain->insn);
2279 error_for_asm (chain->insn, "Invalid `asm' statement:");
2280 error_for_asm (chain->insn,
2281 "fixed or forbidden register %d (%s) was spilled for class %s.",
2282 regno, reg_names[regno], reg_class_names[class]);
2283 failure = 1;
2284 return;
2287 /* Make reg REGNO an additional reload reg. */
2289 potential_reload_regs[i] = -1;
2290 spill_regs[n_spills] = regno;
2291 spill_reg_order[regno] = n_spills;
2292 if (dumpfile)
2293 fprintf (dumpfile, "Spilling reg %d.\n", regno);
2294 SET_HARD_REG_BIT (chain->used_spill_regs, regno);
2296 /* Clear off the needs we just satisfied. */
2298 chain->need.regs[0][class]--;
2299 p = reg_class_superclasses[class];
2300 while (*p != LIM_REG_CLASSES)
2301 chain->need.regs[0][(int) *p++]--;
2303 if (nongroup && chain->need.regs[1][class] > 0)
2305 SET_HARD_REG_BIT (chain->counted_for_nongroups, regno);
2306 chain->need.regs[1][class]--;
2307 p = reg_class_superclasses[class];
2308 while (*p != LIM_REG_CLASSES)
2309 chain->need.regs[1][(int) *p++]--;
2312 n_spills++;
2315 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2316 data that is dead in INSN. */
2318 static void
2319 delete_dead_insn (insn)
2320 rtx insn;
2322 rtx prev = prev_real_insn (insn);
2323 rtx prev_dest;
2325 /* If the previous insn sets a register that dies in our insn, delete it
2326 too. */
2327 if (prev && GET_CODE (PATTERN (prev)) == SET
2328 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
2329 && reg_mentioned_p (prev_dest, PATTERN (insn))
2330 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
2331 && ! side_effects_p (SET_SRC (PATTERN (prev))))
2332 delete_dead_insn (prev);
2334 PUT_CODE (insn, NOTE);
2335 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2336 NOTE_SOURCE_FILE (insn) = 0;
2339 /* Modify the home of pseudo-reg I.
2340 The new home is present in reg_renumber[I].
2342 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2343 or it may be -1, meaning there is none or it is not relevant.
2344 This is used so that all pseudos spilled from a given hard reg
2345 can share one stack slot. */
2347 static void
2348 alter_reg (i, from_reg)
2349 register int i;
2350 int from_reg;
2352 /* When outputting an inline function, this can happen
2353 for a reg that isn't actually used. */
2354 if (regno_reg_rtx[i] == 0)
2355 return;
2357 /* If the reg got changed to a MEM at rtl-generation time,
2358 ignore it. */
2359 if (GET_CODE (regno_reg_rtx[i]) != REG)
2360 return;
2362 /* Modify the reg-rtx to contain the new hard reg
2363 number or else to contain its pseudo reg number. */
2364 REGNO (regno_reg_rtx[i])
2365 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
2367 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2368 allocate a stack slot for it. */
2370 if (reg_renumber[i] < 0
2371 && REG_N_REFS (i) > 0
2372 && reg_equiv_constant[i] == 0
2373 && reg_equiv_memory_loc[i] == 0)
2375 register rtx x;
2376 int inherent_size = PSEUDO_REGNO_BYTES (i);
2377 int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2378 int adjust = 0;
2380 /* Each pseudo reg has an inherent size which comes from its own mode,
2381 and a total size which provides room for paradoxical subregs
2382 which refer to the pseudo reg in wider modes.
2384 We can use a slot already allocated if it provides both
2385 enough inherent space and enough total space.
2386 Otherwise, we allocate a new slot, making sure that it has no less
2387 inherent space, and no less total space, then the previous slot. */
2388 if (from_reg == -1)
2390 /* No known place to spill from => no slot to reuse. */
2391 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
2392 inherent_size == total_size ? 0 : -1);
2393 if (BYTES_BIG_ENDIAN)
2394 /* Cancel the big-endian correction done in assign_stack_local.
2395 Get the address of the beginning of the slot.
2396 This is so we can do a big-endian correction unconditionally
2397 below. */
2398 adjust = inherent_size - total_size;
2400 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2402 /* Reuse a stack slot if possible. */
2403 else if (spill_stack_slot[from_reg] != 0
2404 && spill_stack_slot_width[from_reg] >= total_size
2405 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2406 >= inherent_size))
2407 x = spill_stack_slot[from_reg];
2408 /* Allocate a bigger slot. */
2409 else
2411 /* Compute maximum size needed, both for inherent size
2412 and for total size. */
2413 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2414 rtx stack_slot;
2415 if (spill_stack_slot[from_reg])
2417 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2418 > inherent_size)
2419 mode = GET_MODE (spill_stack_slot[from_reg]);
2420 if (spill_stack_slot_width[from_reg] > total_size)
2421 total_size = spill_stack_slot_width[from_reg];
2423 /* Make a slot with that size. */
2424 x = assign_stack_local (mode, total_size,
2425 inherent_size == total_size ? 0 : -1);
2426 stack_slot = x;
2427 if (BYTES_BIG_ENDIAN)
2429 /* Cancel the big-endian correction done in assign_stack_local.
2430 Get the address of the beginning of the slot.
2431 This is so we can do a big-endian correction unconditionally
2432 below. */
2433 adjust = GET_MODE_SIZE (mode) - total_size;
2434 if (adjust)
2435 stack_slot = gen_rtx_MEM (mode_for_size (total_size
2436 * BITS_PER_UNIT,
2437 MODE_INT, 1),
2438 plus_constant (XEXP (x, 0), adjust));
2440 spill_stack_slot[from_reg] = stack_slot;
2441 spill_stack_slot_width[from_reg] = total_size;
2444 /* On a big endian machine, the "address" of the slot
2445 is the address of the low part that fits its inherent mode. */
2446 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2447 adjust += (total_size - inherent_size);
2449 /* If we have any adjustment to make, or if the stack slot is the
2450 wrong mode, make a new stack slot. */
2451 if (adjust != 0 || GET_MODE (x) != GET_MODE (regno_reg_rtx[i]))
2453 x = gen_rtx_MEM (GET_MODE (regno_reg_rtx[i]),
2454 plus_constant (XEXP (x, 0), adjust));
2456 /* If this was shared among registers, must ensure we never
2457 set it readonly since that can cause scheduling
2458 problems. Note we would only have in this adjustment
2459 case in any event, since the code above doesn't set it. */
2461 if (from_reg == -1)
2462 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2465 /* Save the stack slot for later. */
2466 reg_equiv_memory_loc[i] = x;
2470 /* Mark the slots in regs_ever_live for the hard regs
2471 used by pseudo-reg number REGNO. */
2473 void
2474 mark_home_live (regno)
2475 int regno;
2477 register int i, lim;
2478 i = reg_renumber[regno];
2479 if (i < 0)
2480 return;
2481 lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2482 while (i < lim)
2483 regs_ever_live[i++] = 1;
2486 /* This function handles the tracking of elimination offsets around branches.
2488 X is a piece of RTL being scanned.
2490 INSN is the insn that it came from, if any.
2492 INITIAL_P is non-zero if we are to set the offset to be the initial
2493 offset and zero if we are setting the offset of the label to be the
2494 current offset. */
2496 static void
2497 set_label_offsets (x, insn, initial_p)
2498 rtx x;
2499 rtx insn;
2500 int initial_p;
2502 enum rtx_code code = GET_CODE (x);
2503 rtx tem;
2504 unsigned int i;
2505 struct elim_table *p;
2507 switch (code)
2509 case LABEL_REF:
2510 if (LABEL_REF_NONLOCAL_P (x))
2511 return;
2513 x = XEXP (x, 0);
2515 /* ... fall through ... */
2517 case CODE_LABEL:
2518 /* If we know nothing about this label, set the desired offsets. Note
2519 that this sets the offset at a label to be the offset before a label
2520 if we don't know anything about the label. This is not correct for
2521 the label after a BARRIER, but is the best guess we can make. If
2522 we guessed wrong, we will suppress an elimination that might have
2523 been possible had we been able to guess correctly. */
2525 if (! offsets_known_at[CODE_LABEL_NUMBER (x)])
2527 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2528 offsets_at[CODE_LABEL_NUMBER (x)][i]
2529 = (initial_p ? reg_eliminate[i].initial_offset
2530 : reg_eliminate[i].offset);
2531 offsets_known_at[CODE_LABEL_NUMBER (x)] = 1;
2534 /* Otherwise, if this is the definition of a label and it is
2535 preceded by a BARRIER, set our offsets to the known offset of
2536 that label. */
2538 else if (x == insn
2539 && (tem = prev_nonnote_insn (insn)) != 0
2540 && GET_CODE (tem) == BARRIER)
2541 set_offsets_for_label (insn);
2542 else
2543 /* If neither of the above cases is true, compare each offset
2544 with those previously recorded and suppress any eliminations
2545 where the offsets disagree. */
2547 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2548 if (offsets_at[CODE_LABEL_NUMBER (x)][i]
2549 != (initial_p ? reg_eliminate[i].initial_offset
2550 : reg_eliminate[i].offset))
2551 reg_eliminate[i].can_eliminate = 0;
2553 return;
2555 case JUMP_INSN:
2556 set_label_offsets (PATTERN (insn), insn, initial_p);
2558 /* ... fall through ... */
2560 case INSN:
2561 case CALL_INSN:
2562 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2563 and hence must have all eliminations at their initial offsets. */
2564 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2565 if (REG_NOTE_KIND (tem) == REG_LABEL)
2566 set_label_offsets (XEXP (tem, 0), insn, 1);
2567 return;
2569 case ADDR_VEC:
2570 case ADDR_DIFF_VEC:
2571 /* Each of the labels in the address vector must be at their initial
2572 offsets. We want the first field for ADDR_VEC and the second
2573 field for ADDR_DIFF_VEC. */
2575 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2576 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2577 insn, initial_p);
2578 return;
2580 case SET:
2581 /* We only care about setting PC. If the source is not RETURN,
2582 IF_THEN_ELSE, or a label, disable any eliminations not at
2583 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2584 isn't one of those possibilities. For branches to a label,
2585 call ourselves recursively.
2587 Note that this can disable elimination unnecessarily when we have
2588 a non-local goto since it will look like a non-constant jump to
2589 someplace in the current function. This isn't a significant
2590 problem since such jumps will normally be when all elimination
2591 pairs are back to their initial offsets. */
2593 if (SET_DEST (x) != pc_rtx)
2594 return;
2596 switch (GET_CODE (SET_SRC (x)))
2598 case PC:
2599 case RETURN:
2600 return;
2602 case LABEL_REF:
2603 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2604 return;
2606 case IF_THEN_ELSE:
2607 tem = XEXP (SET_SRC (x), 1);
2608 if (GET_CODE (tem) == LABEL_REF)
2609 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2610 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2611 break;
2613 tem = XEXP (SET_SRC (x), 2);
2614 if (GET_CODE (tem) == LABEL_REF)
2615 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2616 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2617 break;
2618 return;
2620 default:
2621 break;
2624 /* If we reach here, all eliminations must be at their initial
2625 offset because we are doing a jump to a variable address. */
2626 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2627 if (p->offset != p->initial_offset)
2628 p->can_eliminate = 0;
2629 break;
2631 default:
2632 break;
2636 /* Used for communication between the next two function to properly share
2637 the vector for an ASM_OPERANDS. */
2639 static struct rtvec_def *old_asm_operands_vec, *new_asm_operands_vec;
2641 /* Scan X and replace any eliminable registers (such as fp) with a
2642 replacement (such as sp), plus an offset.
2644 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2645 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2646 MEM, we are allowed to replace a sum of a register and the constant zero
2647 with the register, which we cannot do outside a MEM. In addition, we need
2648 to record the fact that a register is referenced outside a MEM.
2650 If INSN is an insn, it is the insn containing X. If we replace a REG
2651 in a SET_DEST with an equivalent MEM and INSN is non-zero, write a
2652 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2653 the REG is being modified.
2655 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2656 That's used when we eliminate in expressions stored in notes.
2657 This means, do not set ref_outside_mem even if the reference
2658 is outside of MEMs.
2660 If we see a modification to a register we know about, take the
2661 appropriate action (see case SET, below).
2663 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2664 replacements done assuming all offsets are at their initial values. If
2665 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2666 encounter, return the actual location so that find_reloads will do
2667 the proper thing. */
2670 eliminate_regs (x, mem_mode, insn)
2671 rtx x;
2672 enum machine_mode mem_mode;
2673 rtx insn;
2675 enum rtx_code code = GET_CODE (x);
2676 struct elim_table *ep;
2677 int regno;
2678 rtx new;
2679 int i, j;
2680 const char *fmt;
2681 int copied = 0;
2683 if (! current_function_decl)
2684 return x;
2686 switch (code)
2688 case CONST_INT:
2689 case CONST_DOUBLE:
2690 case CONST:
2691 case SYMBOL_REF:
2692 case CODE_LABEL:
2693 case PC:
2694 case CC0:
2695 case ASM_INPUT:
2696 case ADDR_VEC:
2697 case ADDR_DIFF_VEC:
2698 case RETURN:
2699 return x;
2701 case ADDRESSOF:
2702 /* This is only for the benefit of the debugging backends, which call
2703 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2704 removed after CSE. */
2705 new = eliminate_regs (XEXP (x, 0), 0, insn);
2706 if (GET_CODE (new) == MEM)
2707 return XEXP (new, 0);
2708 return x;
2710 case REG:
2711 regno = REGNO (x);
2713 /* First handle the case where we encounter a bare register that
2714 is eliminable. Replace it with a PLUS. */
2715 if (regno < FIRST_PSEUDO_REGISTER)
2717 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2718 ep++)
2719 if (ep->from_rtx == x && ep->can_eliminate)
2721 if (! mem_mode
2722 /* Refs inside notes don't count for this purpose. */
2723 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2724 || GET_CODE (insn) == INSN_LIST)))
2725 ep->ref_outside_mem = 1;
2726 return plus_constant (ep->to_rtx, ep->previous_offset);
2730 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2731 && reg_equiv_constant[regno]
2732 && ! CONSTANT_P (reg_equiv_constant[regno]))
2733 return eliminate_regs (copy_rtx (reg_equiv_constant[regno]),
2734 mem_mode, insn);
2735 return x;
2737 /* You might think handling MINUS in a manner similar to PLUS is a
2738 good idea. It is not. It has been tried multiple times and every
2739 time the change has had to have been reverted.
2741 Other parts of reload know a PLUS is special (gen_reload for example)
2742 and require special code to handle code a reloaded PLUS operand.
2744 Also consider backends where the flags register is clobbered by a
2745 MINUS, but we can emit a PLUS that does not clobber flags (ia32,
2746 lea instruction comes to mind). If we try to reload a MINUS, we
2747 may kill the flags register that was holding a useful value.
2749 So, please before trying to handle MINUS, consider reload as a
2750 whole instead of this little section as well as the backend issues. */
2751 case PLUS:
2752 /* If this is the sum of an eliminable register and a constant, rework
2753 the sum. */
2754 if (GET_CODE (XEXP (x, 0)) == REG
2755 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2756 && CONSTANT_P (XEXP (x, 1)))
2758 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2759 ep++)
2760 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2762 if (! mem_mode
2763 /* Refs inside notes don't count for this purpose. */
2764 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2765 || GET_CODE (insn) == INSN_LIST)))
2766 ep->ref_outside_mem = 1;
2768 /* The only time we want to replace a PLUS with a REG (this
2769 occurs when the constant operand of the PLUS is the negative
2770 of the offset) is when we are inside a MEM. We won't want
2771 to do so at other times because that would change the
2772 structure of the insn in a way that reload can't handle.
2773 We special-case the commonest situation in
2774 eliminate_regs_in_insn, so just replace a PLUS with a
2775 PLUS here, unless inside a MEM. */
2776 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2777 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2778 return ep->to_rtx;
2779 else
2780 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2781 plus_constant (XEXP (x, 1),
2782 ep->previous_offset));
2785 /* If the register is not eliminable, we are done since the other
2786 operand is a constant. */
2787 return x;
2790 /* If this is part of an address, we want to bring any constant to the
2791 outermost PLUS. We will do this by doing register replacement in
2792 our operands and seeing if a constant shows up in one of them.
2794 We assume here this is part of an address (or a "load address" insn)
2795 since an eliminable register is not likely to appear in any other
2796 context.
2798 If we have (plus (eliminable) (reg)), we want to produce
2799 (plus (plus (replacement) (reg) (const))). If this was part of a
2800 normal add insn, (plus (replacement) (reg)) will be pushed as a
2801 reload. This is the desired action. */
2804 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2805 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2807 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2809 /* If one side is a PLUS and the other side is a pseudo that
2810 didn't get a hard register but has a reg_equiv_constant,
2811 we must replace the constant here since it may no longer
2812 be in the position of any operand. */
2813 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2814 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2815 && reg_renumber[REGNO (new1)] < 0
2816 && reg_equiv_constant != 0
2817 && reg_equiv_constant[REGNO (new1)] != 0)
2818 new1 = reg_equiv_constant[REGNO (new1)];
2819 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2820 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2821 && reg_renumber[REGNO (new0)] < 0
2822 && reg_equiv_constant[REGNO (new0)] != 0)
2823 new0 = reg_equiv_constant[REGNO (new0)];
2825 new = form_sum (new0, new1);
2827 /* As above, if we are not inside a MEM we do not want to
2828 turn a PLUS into something else. We might try to do so here
2829 for an addition of 0 if we aren't optimizing. */
2830 if (! mem_mode && GET_CODE (new) != PLUS)
2831 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2832 else
2833 return new;
2836 return x;
2838 case MULT:
2839 /* If this is the product of an eliminable register and a
2840 constant, apply the distribute law and move the constant out
2841 so that we have (plus (mult ..) ..). This is needed in order
2842 to keep load-address insns valid. This case is pathological.
2843 We ignore the possibility of overflow here. */
2844 if (GET_CODE (XEXP (x, 0)) == REG
2845 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2846 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2847 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2848 ep++)
2849 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2851 if (! mem_mode
2852 /* Refs inside notes don't count for this purpose. */
2853 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2854 || GET_CODE (insn) == INSN_LIST)))
2855 ep->ref_outside_mem = 1;
2857 return
2858 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2859 ep->previous_offset * INTVAL (XEXP (x, 1)));
2862 /* ... fall through ... */
2864 case CALL:
2865 case COMPARE:
2866 /* See comments before PLUS about handling MINUS. */
2867 case MINUS:
2868 case DIV: case UDIV:
2869 case MOD: case UMOD:
2870 case AND: case IOR: case XOR:
2871 case ROTATERT: case ROTATE:
2872 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2873 case NE: case EQ:
2874 case GE: case GT: case GEU: case GTU:
2875 case LE: case LT: case LEU: case LTU:
2877 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2878 rtx new1
2879 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
2881 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2882 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2884 return x;
2886 case EXPR_LIST:
2887 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2888 if (XEXP (x, 0))
2890 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2891 if (new != XEXP (x, 0))
2893 /* If this is a REG_DEAD note, it is not valid anymore.
2894 Using the eliminated version could result in creating a
2895 REG_DEAD note for the stack or frame pointer. */
2896 if (GET_MODE (x) == REG_DEAD)
2897 return (XEXP (x, 1)
2898 ? eliminate_regs (XEXP (x, 1), mem_mode, insn)
2899 : NULL_RTX);
2901 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2905 /* ... fall through ... */
2907 case INSN_LIST:
2908 /* Now do eliminations in the rest of the chain. If this was
2909 an EXPR_LIST, this might result in allocating more memory than is
2910 strictly needed, but it simplifies the code. */
2911 if (XEXP (x, 1))
2913 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2914 if (new != XEXP (x, 1))
2915 return gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2917 return x;
2919 case PRE_INC:
2920 case POST_INC:
2921 case PRE_DEC:
2922 case POST_DEC:
2923 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2924 if (ep->to_rtx == XEXP (x, 0))
2926 int size = GET_MODE_SIZE (mem_mode);
2928 /* If more bytes than MEM_MODE are pushed, account for them. */
2929 #ifdef PUSH_ROUNDING
2930 if (ep->to_rtx == stack_pointer_rtx)
2931 size = PUSH_ROUNDING (size);
2932 #endif
2933 if (code == PRE_DEC || code == POST_DEC)
2934 ep->offset += size;
2935 else
2936 ep->offset -= size;
2939 /* Fall through to generic unary operation case. */
2940 case STRICT_LOW_PART:
2941 case NEG: case NOT:
2942 case SIGN_EXTEND: case ZERO_EXTEND:
2943 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2944 case FLOAT: case FIX:
2945 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2946 case ABS:
2947 case SQRT:
2948 case FFS:
2949 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2950 if (new != XEXP (x, 0))
2951 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2952 return x;
2954 case SUBREG:
2955 /* Similar to above processing, but preserve SUBREG_WORD.
2956 Convert (subreg (mem)) to (mem) if not paradoxical.
2957 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2958 pseudo didn't get a hard reg, we must replace this with the
2959 eliminated version of the memory location because push_reloads
2960 may do the replacement in certain circumstances. */
2961 if (GET_CODE (SUBREG_REG (x)) == REG
2962 && (GET_MODE_SIZE (GET_MODE (x))
2963 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2964 && reg_equiv_memory_loc != 0
2965 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2967 #if 0
2968 new = eliminate_regs (reg_equiv_memory_loc[REGNO (SUBREG_REG (x))],
2969 mem_mode, insn);
2971 /* If we didn't change anything, we must retain the pseudo. */
2972 if (new == reg_equiv_memory_loc[REGNO (SUBREG_REG (x))])
2973 new = SUBREG_REG (x);
2974 else
2976 /* In this case, we must show that the pseudo is used in this
2977 insn so that delete_output_reload will do the right thing. */
2978 if (insn != 0 && GET_CODE (insn) != EXPR_LIST
2979 && GET_CODE (insn) != INSN_LIST)
2980 REG_NOTES (emit_insn_before (gen_rtx_USE (VOIDmode,
2981 SUBREG_REG (x)),
2982 insn))
2983 = gen_rtx_EXPR_LIST (REG_EQUAL, new, NULL_RTX);
2985 /* Ensure NEW isn't shared in case we have to reload it. */
2986 new = copy_rtx (new);
2988 #else
2989 new = SUBREG_REG (x);
2990 #endif
2992 else
2993 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
2995 if (new != XEXP (x, 0))
2997 int x_size = GET_MODE_SIZE (GET_MODE (x));
2998 int new_size = GET_MODE_SIZE (GET_MODE (new));
3000 if (GET_CODE (new) == MEM
3001 && ((x_size < new_size
3002 #ifdef WORD_REGISTER_OPERATIONS
3003 /* On these machines, combine can create rtl of the form
3004 (set (subreg:m1 (reg:m2 R) 0) ...)
3005 where m1 < m2, and expects something interesting to
3006 happen to the entire word. Moreover, it will use the
3007 (reg:m2 R) later, expecting all bits to be preserved.
3008 So if the number of words is the same, preserve the
3009 subreg so that push_reloads can see it. */
3010 && ! ((x_size-1)/UNITS_PER_WORD == (new_size-1)/UNITS_PER_WORD)
3011 #endif
3013 || (x_size == new_size))
3016 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
3017 enum machine_mode mode = GET_MODE (x);
3019 if (BYTES_BIG_ENDIAN)
3020 offset += (MIN (UNITS_PER_WORD,
3021 GET_MODE_SIZE (GET_MODE (new)))
3022 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
3024 PUT_MODE (new, mode);
3025 XEXP (new, 0) = plus_constant (XEXP (new, 0), offset);
3026 return new;
3028 else
3029 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_WORD (x));
3032 return x;
3034 case USE:
3035 /* If using a register that is the source of an eliminate we still
3036 think can be performed, note it cannot be performed since we don't
3037 know how this register is used. */
3038 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3039 if (ep->from_rtx == XEXP (x, 0))
3040 ep->can_eliminate = 0;
3042 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
3043 if (new != XEXP (x, 0))
3044 return gen_rtx_fmt_e (code, GET_MODE (x), new);
3045 return x;
3047 case CLOBBER:
3048 /* If clobbering a register that is the replacement register for an
3049 elimination we still think can be performed, note that it cannot
3050 be performed. Otherwise, we need not be concerned about it. */
3051 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3052 if (ep->to_rtx == XEXP (x, 0))
3053 ep->can_eliminate = 0;
3055 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
3056 if (new != XEXP (x, 0))
3057 return gen_rtx_fmt_e (code, GET_MODE (x), new);
3058 return x;
3060 case ASM_OPERANDS:
3062 rtx *temp_vec;
3063 /* Properly handle sharing input and constraint vectors. */
3064 if (ASM_OPERANDS_INPUT_VEC (x) != old_asm_operands_vec)
3066 /* When we come to a new vector not seen before,
3067 scan all its elements; keep the old vector if none
3068 of them changes; otherwise, make a copy. */
3069 old_asm_operands_vec = ASM_OPERANDS_INPUT_VEC (x);
3070 temp_vec = (rtx *) alloca (XVECLEN (x, 3) * sizeof (rtx));
3071 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
3072 temp_vec[i] = eliminate_regs (ASM_OPERANDS_INPUT (x, i),
3073 mem_mode, insn);
3075 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
3076 if (temp_vec[i] != ASM_OPERANDS_INPUT (x, i))
3077 break;
3079 if (i == ASM_OPERANDS_INPUT_LENGTH (x))
3080 new_asm_operands_vec = old_asm_operands_vec;
3081 else
3082 new_asm_operands_vec
3083 = gen_rtvec_v (ASM_OPERANDS_INPUT_LENGTH (x), temp_vec);
3086 /* If we had to copy the vector, copy the entire ASM_OPERANDS. */
3087 if (new_asm_operands_vec == old_asm_operands_vec)
3088 return x;
3090 new = gen_rtx_ASM_OPERANDS (VOIDmode, ASM_OPERANDS_TEMPLATE (x),
3091 ASM_OPERANDS_OUTPUT_CONSTRAINT (x),
3092 ASM_OPERANDS_OUTPUT_IDX (x),
3093 new_asm_operands_vec,
3094 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (x),
3095 ASM_OPERANDS_SOURCE_FILE (x),
3096 ASM_OPERANDS_SOURCE_LINE (x));
3097 new->volatil = x->volatil;
3098 return new;
3101 case SET:
3102 /* Check for setting a register that we know about. */
3103 if (GET_CODE (SET_DEST (x)) == REG)
3105 /* See if this is setting the replacement register for an
3106 elimination.
3108 If DEST is the hard frame pointer, we do nothing because we
3109 assume that all assignments to the frame pointer are for
3110 non-local gotos and are being done at a time when they are valid
3111 and do not disturb anything else. Some machines want to
3112 eliminate a fake argument pointer (or even a fake frame pointer)
3113 with either the real frame or the stack pointer. Assignments to
3114 the hard frame pointer must not prevent this elimination. */
3116 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3117 ep++)
3118 if (ep->to_rtx == SET_DEST (x)
3119 && SET_DEST (x) != hard_frame_pointer_rtx)
3121 /* If it is being incremented, adjust the offset. Otherwise,
3122 this elimination can't be done. */
3123 rtx src = SET_SRC (x);
3125 if (GET_CODE (src) == PLUS
3126 && XEXP (src, 0) == SET_DEST (x)
3127 && GET_CODE (XEXP (src, 1)) == CONST_INT)
3128 ep->offset -= INTVAL (XEXP (src, 1));
3129 else
3130 ep->can_eliminate = 0;
3133 /* Now check to see we are assigning to a register that can be
3134 eliminated. If so, it must be as part of a PARALLEL, since we
3135 will not have been called if this is a single SET. So indicate
3136 that we can no longer eliminate this reg. */
3137 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3138 ep++)
3139 if (ep->from_rtx == SET_DEST (x) && ep->can_eliminate)
3140 ep->can_eliminate = 0;
3143 /* Now avoid the loop below in this common case. */
3145 rtx new0 = eliminate_regs (SET_DEST (x), 0, insn);
3146 rtx new1 = eliminate_regs (SET_SRC (x), 0, insn);
3148 /* If SET_DEST changed from a REG to a MEM and INSN is an insn,
3149 write a CLOBBER insn. */
3150 if (GET_CODE (SET_DEST (x)) == REG && GET_CODE (new0) == MEM
3151 && insn != 0 && GET_CODE (insn) != EXPR_LIST
3152 && GET_CODE (insn) != INSN_LIST)
3153 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, SET_DEST (x)), insn);
3155 if (new0 != SET_DEST (x) || new1 != SET_SRC (x))
3156 return gen_rtx_SET (VOIDmode, new0, new1);
3159 return x;
3161 case MEM:
3162 /* This is only for the benefit of the debugging backends, which call
3163 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
3164 removed after CSE. */
3165 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
3166 return eliminate_regs (XEXP (XEXP (x, 0), 0), 0, insn);
3168 /* Our only special processing is to pass the mode of the MEM to our
3169 recursive call and copy the flags. While we are here, handle this
3170 case more efficiently. */
3171 new = eliminate_regs (XEXP (x, 0), GET_MODE (x), insn);
3172 if (new != XEXP (x, 0))
3174 new = gen_rtx_MEM (GET_MODE (x), new);
3175 new->volatil = x->volatil;
3176 new->unchanging = x->unchanging;
3177 new->in_struct = x->in_struct;
3178 return new;
3180 else
3181 return x;
3183 default:
3184 break;
3187 /* Process each of our operands recursively. If any have changed, make a
3188 copy of the rtx. */
3189 fmt = GET_RTX_FORMAT (code);
3190 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3192 if (*fmt == 'e')
3194 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
3195 if (new != XEXP (x, i) && ! copied)
3197 rtx new_x = rtx_alloc (code);
3198 bcopy ((char *) x, (char *) new_x,
3199 (sizeof (*new_x) - sizeof (new_x->fld)
3200 + sizeof (new_x->fld[0]) * GET_RTX_LENGTH (code)));
3201 x = new_x;
3202 copied = 1;
3204 XEXP (x, i) = new;
3206 else if (*fmt == 'E')
3208 int copied_vec = 0;
3209 for (j = 0; j < XVECLEN (x, i); j++)
3211 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
3212 if (new != XVECEXP (x, i, j) && ! copied_vec)
3214 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
3215 XVEC (x, i)->elem);
3216 if (! copied)
3218 rtx new_x = rtx_alloc (code);
3219 bcopy ((char *) x, (char *) new_x,
3220 (sizeof (*new_x) - sizeof (new_x->fld)
3221 + (sizeof (new_x->fld[0])
3222 * GET_RTX_LENGTH (code))));
3223 x = new_x;
3224 copied = 1;
3226 XVEC (x, i) = new_v;
3227 copied_vec = 1;
3229 XVECEXP (x, i, j) = new;
3234 return x;
3237 /* Scan INSN and eliminate all eliminable registers in it.
3239 If REPLACE is nonzero, do the replacement destructively. Also
3240 delete the insn as dead it if it is setting an eliminable register.
3242 If REPLACE is zero, do all our allocations in reload_obstack.
3244 If no eliminations were done and this insn doesn't require any elimination
3245 processing (these are not identical conditions: it might be updating sp,
3246 but not referencing fp; this needs to be seen during reload_as_needed so
3247 that the offset between fp and sp can be taken into consideration), zero
3248 is returned. Otherwise, 1 is returned. */
3250 static int
3251 eliminate_regs_in_insn (insn, replace)
3252 rtx insn;
3253 int replace;
3255 rtx old_body = PATTERN (insn);
3256 rtx old_set = single_set (insn);
3257 rtx new_body;
3258 int val = 0;
3259 struct elim_table *ep;
3261 if (! replace)
3262 push_obstacks (&reload_obstack, &reload_obstack);
3264 if (old_set != 0 && GET_CODE (SET_DEST (old_set)) == REG
3265 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3267 /* Check for setting an eliminable register. */
3268 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3269 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3271 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3272 /* If this is setting the frame pointer register to the
3273 hardware frame pointer register and this is an elimination
3274 that will be done (tested above), this insn is really
3275 adjusting the frame pointer downward to compensate for
3276 the adjustment done before a nonlocal goto. */
3277 if (ep->from == FRAME_POINTER_REGNUM
3278 && ep->to == HARD_FRAME_POINTER_REGNUM)
3280 rtx src = SET_SRC (old_set);
3281 int offset = 0, ok = 0;
3282 rtx prev_insn, prev_set;
3284 if (src == ep->to_rtx)
3285 offset = 0, ok = 1;
3286 else if (GET_CODE (src) == PLUS
3287 && GET_CODE (XEXP (src, 0)) == CONST_INT
3288 && XEXP (src, 1) == ep->to_rtx)
3289 offset = INTVAL (XEXP (src, 0)), ok = 1;
3290 else if (GET_CODE (src) == PLUS
3291 && GET_CODE (XEXP (src, 1)) == CONST_INT
3292 && XEXP (src, 0) == ep->to_rtx)
3293 offset = INTVAL (XEXP (src, 1)), ok = 1;
3294 else if ((prev_insn = prev_nonnote_insn (insn)) != 0
3295 && (prev_set = single_set (prev_insn)) != 0
3296 && rtx_equal_p (SET_DEST (prev_set), src))
3298 src = SET_SRC (prev_set);
3299 if (src == ep->to_rtx)
3300 offset = 0, ok = 1;
3301 else if (GET_CODE (src) == PLUS
3302 && GET_CODE (XEXP (src, 0)) == CONST_INT
3303 && XEXP (src, 1) == ep->to_rtx)
3304 offset = INTVAL (XEXP (src, 0)), ok = 1;
3305 else if (GET_CODE (src) == PLUS
3306 && GET_CODE (XEXP (src, 1)) == CONST_INT
3307 && XEXP (src, 0) == ep->to_rtx)
3308 offset = INTVAL (XEXP (src, 1)), ok = 1;
3311 if (ok)
3313 if (replace)
3315 rtx src
3316 = plus_constant (ep->to_rtx, offset - ep->offset);
3318 /* First see if this insn remains valid when we
3319 make the change. If not, keep the INSN_CODE
3320 the same and let reload fit it up. */
3321 validate_change (insn, &SET_SRC (old_set), src, 1);
3322 validate_change (insn, &SET_DEST (old_set),
3323 ep->to_rtx, 1);
3324 if (! apply_change_group ())
3326 SET_SRC (old_set) = src;
3327 SET_DEST (old_set) = ep->to_rtx;
3331 val = 1;
3332 goto done;
3335 #endif
3337 /* In this case this insn isn't serving a useful purpose. We
3338 will delete it in reload_as_needed once we know that this
3339 elimination is, in fact, being done.
3341 If REPLACE isn't set, we can't delete this insn, but needn't
3342 process it since it won't be used unless something changes. */
3343 if (replace)
3345 delete_dead_insn (insn);
3346 return 1;
3348 val = 1;
3349 goto done;
3352 /* Check for (set (reg) (plus (reg from) (offset))) where the offset
3353 in the insn is the negative of the offset in FROM. Substitute
3354 (set (reg) (reg to)) for the insn and change its code.
3356 We have to do this here, rather than in eliminate_regs, so that we can
3357 change the insn code. */
3359 if (GET_CODE (SET_SRC (old_set)) == PLUS
3360 && GET_CODE (XEXP (SET_SRC (old_set), 0)) == REG
3361 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT)
3362 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3363 ep++)
3364 if (ep->from_rtx == XEXP (SET_SRC (old_set), 0)
3365 && ep->can_eliminate)
3367 /* We must stop at the first elimination that will be used.
3368 If this one would replace the PLUS with a REG, do it
3369 now. Otherwise, quit the loop and let eliminate_regs
3370 do its normal replacement. */
3371 if (ep->offset == - INTVAL (XEXP (SET_SRC (old_set), 1)))
3373 /* We assume here that we don't need a PARALLEL of
3374 any CLOBBERs for this assignment. There's not
3375 much we can do if we do need it. */
3376 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3377 SET_DEST (old_set),
3378 ep->to_rtx);
3379 INSN_CODE (insn) = -1;
3380 val = 1;
3381 goto done;
3384 break;
3388 old_asm_operands_vec = 0;
3390 /* Replace the body of this insn with a substituted form. If we changed
3391 something, return non-zero.
3393 If we are replacing a body that was a (set X (plus Y Z)), try to
3394 re-recognize the insn. We do this in case we had a simple addition
3395 but now can do this as a load-address. This saves an insn in this
3396 common case. */
3398 new_body = eliminate_regs (old_body, 0, replace ? insn : NULL_RTX);
3399 if (new_body != old_body)
3401 /* If we aren't replacing things permanently and we changed something,
3402 make another copy to ensure that all the RTL is new. Otherwise
3403 things can go wrong if find_reload swaps commutative operands
3404 and one is inside RTL that has been copied while the other is not. */
3406 /* Don't copy an asm_operands because (1) there's no need and (2)
3407 copy_rtx can't do it properly when there are multiple outputs. */
3408 if (! replace && asm_noperands (old_body) < 0)
3409 new_body = copy_rtx (new_body);
3411 /* If we had a move insn but now we don't, rerecognize it. This will
3412 cause spurious re-recognition if the old move had a PARALLEL since
3413 the new one still will, but we can't call single_set without
3414 having put NEW_BODY into the insn and the re-recognition won't
3415 hurt in this rare case. */
3416 if (old_set != 0
3417 && ((GET_CODE (SET_SRC (old_set)) == REG
3418 && (GET_CODE (new_body) != SET
3419 || GET_CODE (SET_SRC (new_body)) != REG))
3420 /* If this was a load from or store to memory, compare
3421 the MEM in recog_data.operand to the one in the insn.
3422 If they are not equal, then rerecognize the insn. */
3423 || (old_set != 0
3424 && ((GET_CODE (SET_SRC (old_set)) == MEM
3425 && SET_SRC (old_set) != recog_data.operand[1])
3426 || (GET_CODE (SET_DEST (old_set)) == MEM
3427 && SET_DEST (old_set) != recog_data.operand[0])))
3428 /* If this was an add insn before, rerecognize. */
3429 || GET_CODE (SET_SRC (old_set)) == PLUS))
3431 if (! validate_change (insn, &PATTERN (insn), new_body, 0))
3432 /* If recognition fails, store the new body anyway.
3433 It's normal to have recognition failures here
3434 due to bizarre memory addresses; reloading will fix them. */
3435 PATTERN (insn) = new_body;
3437 else
3438 PATTERN (insn) = new_body;
3440 val = 1;
3443 /* Loop through all elimination pairs. See if any have changed.
3445 We also detect a cases where register elimination cannot be done,
3446 namely, if a register would be both changed and referenced outside a MEM
3447 in the resulting insn since such an insn is often undefined and, even if
3448 not, we cannot know what meaning will be given to it. Note that it is
3449 valid to have a register used in an address in an insn that changes it
3450 (presumably with a pre- or post-increment or decrement).
3452 If anything changes, return nonzero. */
3454 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3456 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3457 ep->can_eliminate = 0;
3459 ep->ref_outside_mem = 0;
3461 if (ep->previous_offset != ep->offset)
3462 val = 1;
3465 done:
3466 /* If we changed something, perform elimination in REG_NOTES. This is
3467 needed even when REPLACE is zero because a REG_DEAD note might refer
3468 to a register that we eliminate and could cause a different number
3469 of spill registers to be needed in the final reload pass than in
3470 the pre-passes. */
3471 if (val && REG_NOTES (insn) != 0)
3472 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
3474 if (! replace)
3475 pop_obstacks ();
3477 return val;
3480 /* Loop through all elimination pairs.
3481 Recalculate the number not at initial offset.
3483 Compute the maximum offset (minimum offset if the stack does not
3484 grow downward) for each elimination pair. */
3486 static void
3487 update_eliminable_offsets ()
3489 struct elim_table *ep;
3491 num_not_at_initial_offset = 0;
3492 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3494 ep->previous_offset = ep->offset;
3495 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3496 num_not_at_initial_offset++;
3500 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3501 replacement we currently believe is valid, mark it as not eliminable if X
3502 modifies DEST in any way other than by adding a constant integer to it.
3504 If DEST is the frame pointer, we do nothing because we assume that
3505 all assignments to the hard frame pointer are nonlocal gotos and are being
3506 done at a time when they are valid and do not disturb anything else.
3507 Some machines want to eliminate a fake argument pointer with either the
3508 frame or stack pointer. Assignments to the hard frame pointer must not
3509 prevent this elimination.
3511 Called via note_stores from reload before starting its passes to scan
3512 the insns of the function. */
3514 static void
3515 mark_not_eliminable (dest, x)
3516 rtx dest;
3517 rtx x;
3519 register unsigned int i;
3521 /* A SUBREG of a hard register here is just changing its mode. We should
3522 not see a SUBREG of an eliminable hard register, but check just in
3523 case. */
3524 if (GET_CODE (dest) == SUBREG)
3525 dest = SUBREG_REG (dest);
3527 if (dest == hard_frame_pointer_rtx)
3528 return;
3530 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3531 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3532 && (GET_CODE (x) != SET
3533 || GET_CODE (SET_SRC (x)) != PLUS
3534 || XEXP (SET_SRC (x), 0) != dest
3535 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3537 reg_eliminate[i].can_eliminate_previous
3538 = reg_eliminate[i].can_eliminate = 0;
3539 num_eliminable--;
3543 /* Verify that the initial elimination offsets did not change since the
3544 last call to set_initial_elim_offsets. This is used to catch cases
3545 where something illegal happened during reload_as_needed that could
3546 cause incorrect code to be generated if we did not check for it. */
3547 static void
3548 verify_initial_elim_offsets ()
3550 int t;
3552 #ifdef ELIMINABLE_REGS
3553 struct elim_table *ep;
3555 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3557 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3558 if (t != ep->initial_offset)
3559 abort ();
3561 #else
3562 INITIAL_FRAME_POINTER_OFFSET (t);
3563 if (t != reg_eliminate[0].initial_offset)
3564 abort ();
3565 #endif
3568 /* Reset all offsets on eliminable registers to their initial values. */
3569 static void
3570 set_initial_elim_offsets ()
3572 struct elim_table *ep = reg_eliminate;
3574 #ifdef ELIMINABLE_REGS
3575 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3577 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3578 ep->previous_offset = ep->offset = ep->initial_offset;
3580 #else
3581 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3582 ep->previous_offset = ep->offset = ep->initial_offset;
3583 #endif
3585 num_not_at_initial_offset = 0;
3588 /* Initialize the known label offsets.
3589 Set a known offset for each forced label to be at the initial offset
3590 of each elimination. We do this because we assume that all
3591 computed jumps occur from a location where each elimination is
3592 at its initial offset.
3593 For all other labels, show that we don't know the offsets. */
3595 static void
3596 set_initial_label_offsets ()
3598 rtx x;
3599 bzero ((char *) &offsets_known_at[get_first_label_num ()], num_labels);
3601 for (x = forced_labels; x; x = XEXP (x, 1))
3602 if (XEXP (x, 0))
3603 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3606 /* Set all elimination offsets to the known values for the code label given
3607 by INSN. */
3608 static void
3609 set_offsets_for_label (insn)
3610 rtx insn;
3612 unsigned int i;
3613 int label_nr = CODE_LABEL_NUMBER (insn);
3614 struct elim_table *ep;
3616 num_not_at_initial_offset = 0;
3617 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3619 ep->offset = ep->previous_offset = offsets_at[label_nr][i];
3620 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3621 num_not_at_initial_offset++;
3625 /* See if anything that happened changes which eliminations are valid.
3626 For example, on the Sparc, whether or not the frame pointer can
3627 be eliminated can depend on what registers have been used. We need
3628 not check some conditions again (such as flag_omit_frame_pointer)
3629 since they can't have changed. */
3631 static void
3632 update_eliminables (pset)
3633 HARD_REG_SET *pset;
3635 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3636 int previous_frame_pointer_needed = frame_pointer_needed;
3637 #endif
3638 struct elim_table *ep;
3640 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3641 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3642 #ifdef ELIMINABLE_REGS
3643 || ! CAN_ELIMINATE (ep->from, ep->to)
3644 #endif
3646 ep->can_eliminate = 0;
3648 /* Look for the case where we have discovered that we can't replace
3649 register A with register B and that means that we will now be
3650 trying to replace register A with register C. This means we can
3651 no longer replace register C with register B and we need to disable
3652 such an elimination, if it exists. This occurs often with A == ap,
3653 B == sp, and C == fp. */
3655 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3657 struct elim_table *op;
3658 register int new_to = -1;
3660 if (! ep->can_eliminate && ep->can_eliminate_previous)
3662 /* Find the current elimination for ep->from, if there is a
3663 new one. */
3664 for (op = reg_eliminate;
3665 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3666 if (op->from == ep->from && op->can_eliminate)
3668 new_to = op->to;
3669 break;
3672 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3673 disable it. */
3674 for (op = reg_eliminate;
3675 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3676 if (op->from == new_to && op->to == ep->to)
3677 op->can_eliminate = 0;
3681 /* See if any registers that we thought we could eliminate the previous
3682 time are no longer eliminable. If so, something has changed and we
3683 must spill the register. Also, recompute the number of eliminable
3684 registers and see if the frame pointer is needed; it is if there is
3685 no elimination of the frame pointer that we can perform. */
3687 frame_pointer_needed = 1;
3688 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3690 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3691 && ep->to != HARD_FRAME_POINTER_REGNUM)
3692 frame_pointer_needed = 0;
3694 if (! ep->can_eliminate && ep->can_eliminate_previous)
3696 ep->can_eliminate_previous = 0;
3697 SET_HARD_REG_BIT (*pset, ep->from);
3698 num_eliminable--;
3702 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3703 /* If we didn't need a frame pointer last time, but we do now, spill
3704 the hard frame pointer. */
3705 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3706 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3707 #endif
3710 /* Initialize the table of registers to eliminate. */
3711 static void
3712 init_elim_table ()
3714 struct elim_table *ep;
3715 #ifdef ELIMINABLE_REGS
3716 struct elim_table_1 *ep1;
3717 #endif
3719 if (!reg_eliminate)
3720 reg_eliminate = (struct elim_table *)
3721 xcalloc(sizeof(struct elim_table), NUM_ELIMINABLE_REGS);
3723 /* Does this function require a frame pointer? */
3725 frame_pointer_needed = (! flag_omit_frame_pointer
3726 #ifdef EXIT_IGNORE_STACK
3727 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3728 and restore sp for alloca. So we can't eliminate
3729 the frame pointer in that case. At some point,
3730 we should improve this by emitting the
3731 sp-adjusting insns for this case. */
3732 || (current_function_calls_alloca
3733 && EXIT_IGNORE_STACK)
3734 #endif
3735 || FRAME_POINTER_REQUIRED);
3737 num_eliminable = 0;
3739 #ifdef ELIMINABLE_REGS
3740 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3741 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3743 ep->from = ep1->from;
3744 ep->to = ep1->to;
3745 ep->can_eliminate = ep->can_eliminate_previous
3746 = (CAN_ELIMINATE (ep->from, ep->to)
3747 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3749 #else
3750 reg_eliminate[0].from = reg_eliminate_1[0].from;
3751 reg_eliminate[0].to = reg_eliminate_1[0].to;
3752 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3753 = ! frame_pointer_needed;
3754 #endif
3756 /* Count the number of eliminable registers and build the FROM and TO
3757 REG rtx's. Note that code in gen_rtx will cause, e.g.,
3758 gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3759 We depend on this. */
3760 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3762 num_eliminable += ep->can_eliminate;
3763 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3764 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3768 /* Kick all pseudos out of hard register REGNO.
3769 If DUMPFILE is nonzero, log actions taken on that file.
3771 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3772 because we found we can't eliminate some register. In the case, no pseudos
3773 are allowed to be in the register, even if they are only in a block that
3774 doesn't require spill registers, unlike the case when we are spilling this
3775 hard reg to produce another spill register.
3777 Return nonzero if any pseudos needed to be kicked out. */
3779 static void
3780 spill_hard_reg (regno, dumpfile, cant_eliminate)
3781 register int regno;
3782 FILE *dumpfile ATTRIBUTE_UNUSED;
3783 int cant_eliminate;
3785 register int i;
3787 if (cant_eliminate)
3789 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3790 regs_ever_live[regno] = 1;
3793 /* Spill every pseudo reg that was allocated to this reg
3794 or to something that overlaps this reg. */
3796 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3797 if (reg_renumber[i] >= 0
3798 && reg_renumber[i] <= regno
3799 && (reg_renumber[i]
3800 + HARD_REGNO_NREGS (reg_renumber[i],
3801 PSEUDO_REGNO_MODE (i))
3802 > regno))
3803 SET_REGNO_REG_SET (spilled_pseudos, i);
3806 /* I'm getting weird preprocessor errors if I use IOR_HARD_REG_SET
3807 from within EXECUTE_IF_SET_IN_REG_SET. Hence this awkwardness. */
3808 static void
3809 ior_hard_reg_set (set1, set2)
3810 HARD_REG_SET *set1, *set2;
3812 IOR_HARD_REG_SET (*set1, *set2);
3815 /* After find_reload_regs has been run for all insn that need reloads,
3816 and/or spill_hard_regs was called, this function is used to actually
3817 spill pseudo registers and try to reallocate them. It also sets up the
3818 spill_regs array for use by choose_reload_regs. */
3820 static int
3821 finish_spills (global, dumpfile)
3822 int global;
3823 FILE *dumpfile;
3825 struct insn_chain *chain;
3826 int something_changed = 0;
3827 int i;
3829 /* Build the spill_regs array for the function. */
3830 /* If there are some registers still to eliminate and one of the spill regs
3831 wasn't ever used before, additional stack space may have to be
3832 allocated to store this register. Thus, we may have changed the offset
3833 between the stack and frame pointers, so mark that something has changed.
3835 One might think that we need only set VAL to 1 if this is a call-used
3836 register. However, the set of registers that must be saved by the
3837 prologue is not identical to the call-used set. For example, the
3838 register used by the call insn for the return PC is a call-used register,
3839 but must be saved by the prologue. */
3841 n_spills = 0;
3842 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3843 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3845 spill_reg_order[i] = n_spills;
3846 spill_regs[n_spills++] = i;
3847 if (num_eliminable && ! regs_ever_live[i])
3848 something_changed = 1;
3849 regs_ever_live[i] = 1;
3851 else
3852 spill_reg_order[i] = -1;
3854 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3855 if (REGNO_REG_SET_P (spilled_pseudos, i))
3857 /* Record the current hard register the pseudo is allocated to in
3858 pseudo_previous_regs so we avoid reallocating it to the same
3859 hard reg in a later pass. */
3860 if (reg_renumber[i] < 0)
3861 abort ();
3862 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3863 /* Mark it as no longer having a hard register home. */
3864 reg_renumber[i] = -1;
3865 /* We will need to scan everything again. */
3866 something_changed = 1;
3869 /* Retry global register allocation if possible. */
3870 if (global)
3872 bzero ((char *) pseudo_forbidden_regs, max_regno * sizeof (HARD_REG_SET));
3873 /* For every insn that needs reloads, set the registers used as spill
3874 regs in pseudo_forbidden_regs for every pseudo live across the
3875 insn. */
3876 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
3878 EXECUTE_IF_SET_IN_REG_SET
3879 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
3881 ior_hard_reg_set (pseudo_forbidden_regs + i,
3882 &chain->used_spill_regs);
3884 EXECUTE_IF_SET_IN_REG_SET
3885 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
3887 ior_hard_reg_set (pseudo_forbidden_regs + i,
3888 &chain->used_spill_regs);
3892 /* Retry allocating the spilled pseudos. For each reg, merge the
3893 various reg sets that indicate which hard regs can't be used,
3894 and call retry_global_alloc.
3895 We change spill_pseudos here to only contain pseudos that did not
3896 get a new hard register. */
3897 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3898 if (reg_old_renumber[i] != reg_renumber[i])
3900 HARD_REG_SET forbidden;
3901 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
3902 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
3903 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
3904 retry_global_alloc (i, forbidden);
3905 if (reg_renumber[i] >= 0)
3906 CLEAR_REGNO_REG_SET (spilled_pseudos, i);
3910 /* Fix up the register information in the insn chain.
3911 This involves deleting those of the spilled pseudos which did not get
3912 a new hard register home from the live_{before,after} sets. */
3913 for (chain = reload_insn_chain; chain; chain = chain->next)
3915 HARD_REG_SET used_by_pseudos;
3916 HARD_REG_SET used_by_pseudos2;
3918 AND_COMPL_REG_SET (chain->live_before, spilled_pseudos);
3919 AND_COMPL_REG_SET (chain->live_after, spilled_pseudos);
3921 /* Mark any unallocated hard regs as available for spills. That
3922 makes inheritance work somewhat better. */
3923 if (chain->need_reload)
3925 REG_SET_TO_HARD_REG_SET (used_by_pseudos, chain->live_before);
3926 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, chain->live_after);
3927 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
3929 /* Save the old value for the sanity test below. */
3930 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
3932 compute_use_by_pseudos (&used_by_pseudos, chain->live_before);
3933 compute_use_by_pseudos (&used_by_pseudos, chain->live_after);
3934 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
3935 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
3937 /* Make sure we only enlarge the set. */
3938 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
3939 abort ();
3940 ok:;
3944 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
3945 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3947 int regno = reg_renumber[i];
3948 if (reg_old_renumber[i] == regno)
3949 continue;
3951 alter_reg (i, reg_old_renumber[i]);
3952 reg_old_renumber[i] = regno;
3953 if (dumpfile)
3955 if (regno == -1)
3956 fprintf (dumpfile, " Register %d now on stack.\n\n", i);
3957 else
3958 fprintf (dumpfile, " Register %d now in %d.\n\n",
3959 i, reg_renumber[i]);
3963 return something_changed;
3966 /* Find all paradoxical subregs within X and update reg_max_ref_width.
3967 Also mark any hard registers used to store user variables as
3968 forbidden from being used for spill registers. */
3970 static void
3971 scan_paradoxical_subregs (x)
3972 register rtx x;
3974 register int i;
3975 register const char *fmt;
3976 register enum rtx_code code = GET_CODE (x);
3978 switch (code)
3980 case REG:
3981 #if 0
3982 if (SMALL_REGISTER_CLASSES && REGNO (x) < FIRST_PSEUDO_REGISTER
3983 && REG_USERVAR_P (x))
3984 SET_HARD_REG_BIT (bad_spill_regs_global, REGNO (x));
3985 #endif
3986 return;
3988 case CONST_INT:
3989 case CONST:
3990 case SYMBOL_REF:
3991 case LABEL_REF:
3992 case CONST_DOUBLE:
3993 case CC0:
3994 case PC:
3995 case USE:
3996 case CLOBBER:
3997 return;
3999 case SUBREG:
4000 if (GET_CODE (SUBREG_REG (x)) == REG
4001 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
4002 reg_max_ref_width[REGNO (SUBREG_REG (x))]
4003 = GET_MODE_SIZE (GET_MODE (x));
4004 return;
4006 default:
4007 break;
4010 fmt = GET_RTX_FORMAT (code);
4011 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4013 if (fmt[i] == 'e')
4014 scan_paradoxical_subregs (XEXP (x, i));
4015 else if (fmt[i] == 'E')
4017 register int j;
4018 for (j = XVECLEN (x, i) - 1; j >=0; j--)
4019 scan_paradoxical_subregs (XVECEXP (x, i, j));
4024 static int
4025 hard_reg_use_compare (p1p, p2p)
4026 const PTR p1p;
4027 const PTR p2p;
4029 const struct hard_reg_n_uses *p1 = (const struct hard_reg_n_uses *)p1p;
4030 const struct hard_reg_n_uses *p2 = (const struct hard_reg_n_uses *)p2p;
4031 int bad1 = TEST_HARD_REG_BIT (bad_spill_regs, p1->regno);
4032 int bad2 = TEST_HARD_REG_BIT (bad_spill_regs, p2->regno);
4033 if (bad1 && bad2)
4034 return p1->regno - p2->regno;
4035 if (bad1)
4036 return 1;
4037 if (bad2)
4038 return -1;
4039 if (p1->uses > p2->uses)
4040 return 1;
4041 if (p1->uses < p2->uses)
4042 return -1;
4043 /* If regs are equally good, sort by regno,
4044 so that the results of qsort leave nothing to chance. */
4045 return p1->regno - p2->regno;
4048 /* Used for communication between order_regs_for_reload and count_pseudo.
4049 Used to avoid counting one pseudo twice. */
4050 static regset pseudos_counted;
4052 /* Update the costs in N_USES, considering that pseudo REG is live. */
4053 static void
4054 count_pseudo (n_uses, reg)
4055 struct hard_reg_n_uses *n_uses;
4056 int reg;
4058 int r = reg_renumber[reg];
4059 int nregs;
4061 if (REGNO_REG_SET_P (pseudos_counted, reg))
4062 return;
4063 SET_REGNO_REG_SET (pseudos_counted, reg);
4065 if (r < 0)
4066 abort ();
4068 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
4069 while (nregs-- > 0)
4070 n_uses[r++].uses += REG_N_REFS (reg);
4072 /* Choose the order to consider regs for use as reload registers
4073 based on how much trouble would be caused by spilling one.
4074 Store them in order of decreasing preference in potential_reload_regs. */
4076 static void
4077 order_regs_for_reload (chain)
4078 struct insn_chain *chain;
4080 register int i;
4081 register int o = 0;
4082 struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
4084 pseudos_counted = ALLOCA_REG_SET ();
4086 COPY_HARD_REG_SET (bad_spill_regs, bad_spill_regs_global);
4088 /* Count number of uses of each hard reg by pseudo regs allocated to it
4089 and then order them by decreasing use. */
4091 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4093 hard_reg_n_uses[i].regno = i;
4094 hard_reg_n_uses[i].uses = 0;
4096 /* Test the various reasons why we can't use a register for
4097 spilling in this insn. */
4098 if (fixed_regs[i]
4099 || REGNO_REG_SET_P (chain->live_before, i)
4100 || REGNO_REG_SET_P (chain->live_after, i))
4101 SET_HARD_REG_BIT (bad_spill_regs, i);
4104 /* Now compute hard_reg_n_uses. */
4105 CLEAR_REG_SET (pseudos_counted);
4107 EXECUTE_IF_SET_IN_REG_SET
4108 (chain->live_before, FIRST_PSEUDO_REGISTER, i,
4110 count_pseudo (hard_reg_n_uses, i);
4112 EXECUTE_IF_SET_IN_REG_SET
4113 (chain->live_after, FIRST_PSEUDO_REGISTER, i,
4115 count_pseudo (hard_reg_n_uses, i);
4118 FREE_REG_SET (pseudos_counted);
4120 /* Prefer registers not so far used, for use in temporary loading.
4121 Among them, if REG_ALLOC_ORDER is defined, use that order.
4122 Otherwise, prefer registers not preserved by calls. */
4124 #ifdef REG_ALLOC_ORDER
4125 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4127 int regno = reg_alloc_order[i];
4129 if (hard_reg_n_uses[regno].uses == 0
4130 && ! TEST_HARD_REG_BIT (bad_spill_regs, regno))
4131 potential_reload_regs[o++] = regno;
4133 #else
4134 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4136 if (hard_reg_n_uses[i].uses == 0 && call_used_regs[i]
4137 && ! TEST_HARD_REG_BIT (bad_spill_regs, i))
4138 potential_reload_regs[o++] = i;
4140 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4142 if (hard_reg_n_uses[i].uses == 0 && ! call_used_regs[i]
4143 && ! TEST_HARD_REG_BIT (bad_spill_regs, i))
4144 potential_reload_regs[o++] = i;
4146 #endif
4148 qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
4149 sizeof hard_reg_n_uses[0], hard_reg_use_compare);
4151 /* Now add the regs that are already used,
4152 preferring those used less often. The fixed and otherwise forbidden
4153 registers will be at the end of this list. */
4155 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4156 if (hard_reg_n_uses[i].uses != 0
4157 && ! TEST_HARD_REG_BIT (bad_spill_regs, hard_reg_n_uses[i].regno))
4158 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
4159 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4160 if (TEST_HARD_REG_BIT (bad_spill_regs, hard_reg_n_uses[i].regno))
4161 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
4164 /* Reload pseudo-registers into hard regs around each insn as needed.
4165 Additional register load insns are output before the insn that needs it
4166 and perhaps store insns after insns that modify the reloaded pseudo reg.
4168 reg_last_reload_reg and reg_reloaded_contents keep track of
4169 which registers are already available in reload registers.
4170 We update these for the reloads that we perform,
4171 as the insns are scanned. */
4173 static void
4174 reload_as_needed (live_known)
4175 int live_known;
4177 struct insn_chain *chain;
4178 #if defined (AUTO_INC_DEC) || defined (INSN_CLOBBERS_REGNO_P)
4179 register int i;
4180 #endif
4181 rtx x;
4183 bzero ((char *) spill_reg_rtx, sizeof spill_reg_rtx);
4184 bzero ((char *) spill_reg_store, sizeof spill_reg_store);
4185 reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
4186 bzero ((char *) reg_last_reload_reg, max_regno * sizeof (rtx));
4187 reg_has_output_reload = (char *) alloca (max_regno);
4188 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4190 set_initial_elim_offsets ();
4192 for (chain = reload_insn_chain; chain; chain = chain->next)
4194 rtx prev;
4195 rtx insn = chain->insn;
4196 rtx old_next = NEXT_INSN (insn);
4198 /* If we pass a label, copy the offsets from the label information
4199 into the current offsets of each elimination. */
4200 if (GET_CODE (insn) == CODE_LABEL)
4201 set_offsets_for_label (insn);
4203 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4205 rtx oldpat = PATTERN (insn);
4207 /* If this is a USE and CLOBBER of a MEM, ensure that any
4208 references to eliminable registers have been removed. */
4210 if ((GET_CODE (PATTERN (insn)) == USE
4211 || GET_CODE (PATTERN (insn)) == CLOBBER)
4212 && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
4213 XEXP (XEXP (PATTERN (insn), 0), 0)
4214 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
4215 GET_MODE (XEXP (PATTERN (insn), 0)),
4216 NULL_RTX);
4218 /* If we need to do register elimination processing, do so.
4219 This might delete the insn, in which case we are done. */
4220 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
4222 eliminate_regs_in_insn (insn, 1);
4223 if (GET_CODE (insn) == NOTE)
4225 update_eliminable_offsets ();
4226 continue;
4230 /* If need_elim is nonzero but need_reload is zero, one might think
4231 that we could simply set n_reloads to 0. However, find_reloads
4232 could have done some manipulation of the insn (such as swapping
4233 commutative operands), and these manipulations are lost during
4234 the first pass for every insn that needs register elimination.
4235 So the actions of find_reloads must be redone here. */
4237 if (! chain->need_elim && ! chain->need_reload
4238 && ! chain->need_operand_change)
4239 n_reloads = 0;
4240 /* First find the pseudo regs that must be reloaded for this insn.
4241 This info is returned in the tables reload_... (see reload.h).
4242 Also modify the body of INSN by substituting RELOAD
4243 rtx's for those pseudo regs. */
4244 else
4246 bzero (reg_has_output_reload, max_regno);
4247 CLEAR_HARD_REG_SET (reg_is_output_reload);
4249 find_reloads (insn, 1, spill_indirect_levels, live_known,
4250 spill_reg_order);
4253 if (num_eliminable && chain->need_elim)
4254 update_eliminable_offsets ();
4256 if (n_reloads > 0)
4258 rtx next = NEXT_INSN (insn);
4259 rtx p;
4261 prev = PREV_INSN (insn);
4263 /* Now compute which reload regs to reload them into. Perhaps
4264 reusing reload regs from previous insns, or else output
4265 load insns to reload them. Maybe output store insns too.
4266 Record the choices of reload reg in reload_reg_rtx. */
4267 choose_reload_regs (chain);
4269 /* Merge any reloads that we didn't combine for fear of
4270 increasing the number of spill registers needed but now
4271 discover can be safely merged. */
4272 if (SMALL_REGISTER_CLASSES)
4273 merge_assigned_reloads (insn);
4275 /* Generate the insns to reload operands into or out of
4276 their reload regs. */
4277 emit_reload_insns (chain);
4279 /* Substitute the chosen reload regs from reload_reg_rtx
4280 into the insn's body (or perhaps into the bodies of other
4281 load and store insn that we just made for reloading
4282 and that we moved the structure into). */
4283 subst_reloads ();
4285 /* If this was an ASM, make sure that all the reload insns
4286 we have generated are valid. If not, give an error
4287 and delete them. */
4289 if (asm_noperands (PATTERN (insn)) >= 0)
4290 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4291 if (p != insn && GET_RTX_CLASS (GET_CODE (p)) == 'i'
4292 && (recog_memoized (p) < 0
4293 || (extract_insn (p), ! constrain_operands (1))))
4295 error_for_asm (insn,
4296 "`asm' operand requires impossible reload");
4297 PUT_CODE (p, NOTE);
4298 NOTE_SOURCE_FILE (p) = 0;
4299 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
4302 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4303 is no longer validly lying around to save a future reload.
4304 Note that this does not detect pseudos that were reloaded
4305 for this insn in order to be stored in
4306 (obeying register constraints). That is correct; such reload
4307 registers ARE still valid. */
4308 note_stores (oldpat, forget_old_reloads_1);
4310 /* There may have been CLOBBER insns placed after INSN. So scan
4311 between INSN and NEXT and use them to forget old reloads. */
4312 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
4313 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
4314 note_stores (PATTERN (x), forget_old_reloads_1);
4316 #ifdef AUTO_INC_DEC
4317 /* Likewise for regs altered by auto-increment in this insn.
4318 REG_INC notes have been changed by reloading:
4319 find_reloads_address_1 records substitutions for them,
4320 which have been performed by subst_reloads above. */
4321 for (i = n_reloads - 1; i >= 0; i--)
4323 rtx in_reg = rld[i].in_reg;
4324 if (in_reg)
4326 enum rtx_code code = GET_CODE (in_reg);
4327 /* PRE_INC / PRE_DEC will have the reload register ending up
4328 with the same value as the stack slot, but that doesn't
4329 hold true for POST_INC / POST_DEC. Either we have to
4330 convert the memory access to a true POST_INC / POST_DEC,
4331 or we can't use the reload register for inheritance. */
4332 if ((code == POST_INC || code == POST_DEC)
4333 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4334 REGNO (rld[i].reg_rtx))
4335 /* Make sure it is the inc/dec pseudo, and not
4336 some other (e.g. output operand) pseudo. */
4337 && (reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4338 == REGNO (XEXP (in_reg, 0))))
4341 rtx reload_reg = rld[i].reg_rtx;
4342 enum machine_mode mode = GET_MODE (reload_reg);
4343 int n = 0;
4344 rtx p;
4346 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4348 /* We really want to ignore REG_INC notes here, so
4349 use PATTERN (p) as argument to reg_set_p . */
4350 if (reg_set_p (reload_reg, PATTERN (p)))
4351 break;
4352 n = count_occurrences (PATTERN (p), reload_reg);
4353 if (! n)
4354 continue;
4355 if (n == 1)
4357 n = validate_replace_rtx (reload_reg,
4358 gen_rtx (code, mode,
4359 reload_reg),
4362 /* We must also verify that the constraints
4363 are met after the replacement. */
4364 extract_insn (p);
4365 if (n)
4366 n = constrain_operands (1);
4367 else
4368 break;
4370 /* If the constraints were not met, then
4371 undo the replacement. */
4372 if (!n)
4374 validate_replace_rtx (gen_rtx (code, mode,
4375 reload_reg),
4376 reload_reg, p);
4377 break;
4381 break;
4383 if (n == 1)
4385 REG_NOTES (p)
4386 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
4387 REG_NOTES (p));
4388 /* Mark this as having an output reload so that the
4389 REG_INC processing code below won't invalidate
4390 the reload for inheritance. */
4391 SET_HARD_REG_BIT (reg_is_output_reload,
4392 REGNO (reload_reg));
4393 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4395 else
4396 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX);
4398 else if ((code == PRE_INC || code == PRE_DEC)
4399 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4400 REGNO (rld[i].reg_rtx))
4401 /* Make sure it is the inc/dec pseudo, and not
4402 some other (e.g. output operand) pseudo. */
4403 && (reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4404 == REGNO (XEXP (in_reg, 0))))
4406 SET_HARD_REG_BIT (reg_is_output_reload,
4407 REGNO (rld[i].reg_rtx));
4408 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4412 /* If a pseudo that got a hard register is auto-incremented,
4413 we must purge records of copying it into pseudos without
4414 hard registers. */
4415 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4416 if (REG_NOTE_KIND (x) == REG_INC)
4418 /* See if this pseudo reg was reloaded in this insn.
4419 If so, its last-reload info is still valid
4420 because it is based on this insn's reload. */
4421 for (i = 0; i < n_reloads; i++)
4422 if (rld[i].out == XEXP (x, 0))
4423 break;
4425 if (i == n_reloads)
4426 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX);
4428 #endif
4430 /* A reload reg's contents are unknown after a label. */
4431 if (GET_CODE (insn) == CODE_LABEL)
4432 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4434 /* Don't assume a reload reg is still good after a call insn
4435 if it is a call-used reg. */
4436 else if (GET_CODE (insn) == CALL_INSN)
4437 AND_COMPL_HARD_REG_SET(reg_reloaded_valid, call_used_reg_set);
4439 /* In case registers overlap, allow certain insns to invalidate
4440 particular hard registers. */
4442 #ifdef INSN_CLOBBERS_REGNO_P
4443 for (i = 0 ; i < FIRST_PSEUDO_REGISTER; i++)
4444 if (TEST_HARD_REG_BIT (reg_reloaded_valid, i)
4445 && INSN_CLOBBERS_REGNO_P (insn, i))
4446 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i);
4447 #endif
4449 #ifdef USE_C_ALLOCA
4450 alloca (0);
4451 #endif
4455 /* Discard all record of any value reloaded from X,
4456 or reloaded in X from someplace else;
4457 unless X is an output reload reg of the current insn.
4459 X may be a hard reg (the reload reg)
4460 or it may be a pseudo reg that was reloaded from. */
4462 static void
4463 forget_old_reloads_1 (x, ignored)
4464 rtx x;
4465 rtx ignored ATTRIBUTE_UNUSED;
4467 register int regno;
4468 int nr;
4469 int offset = 0;
4471 /* note_stores does give us subregs of hard regs. */
4472 while (GET_CODE (x) == SUBREG)
4474 offset += SUBREG_WORD (x);
4475 x = SUBREG_REG (x);
4478 if (GET_CODE (x) != REG)
4479 return;
4481 regno = REGNO (x) + offset;
4483 if (regno >= FIRST_PSEUDO_REGISTER)
4484 nr = 1;
4485 else
4487 int i;
4488 nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
4489 /* Storing into a spilled-reg invalidates its contents.
4490 This can happen if a block-local pseudo is allocated to that reg
4491 and it wasn't spilled because this block's total need is 0.
4492 Then some insn might have an optional reload and use this reg. */
4493 for (i = 0; i < nr; i++)
4494 /* But don't do this if the reg actually serves as an output
4495 reload reg in the current instruction. */
4496 if (n_reloads == 0
4497 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4498 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4501 /* Since value of X has changed,
4502 forget any value previously copied from it. */
4504 while (nr-- > 0)
4505 /* But don't forget a copy if this is the output reload
4506 that establishes the copy's validity. */
4507 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4508 reg_last_reload_reg[regno + nr] = 0;
4511 /* For each reload, the mode of the reload register. */
4512 static enum machine_mode reload_mode[MAX_RELOADS];
4514 /* For each reload, the largest number of registers it will require. */
4515 static int reload_nregs[MAX_RELOADS];
4517 /* Comparison function for qsort to decide which of two reloads
4518 should be handled first. *P1 and *P2 are the reload numbers. */
4520 static int
4521 reload_reg_class_lower (r1p, r2p)
4522 const PTR r1p;
4523 const PTR r2p;
4525 register int r1 = *(const short *)r1p, r2 = *(const short *)r2p;
4526 register int t;
4528 /* Consider required reloads before optional ones. */
4529 t = rld[r1].optional - rld[r2].optional;
4530 if (t != 0)
4531 return t;
4533 /* Count all solitary classes before non-solitary ones. */
4534 t = ((reg_class_size[(int) rld[r2].class] == 1)
4535 - (reg_class_size[(int) rld[r1].class] == 1));
4536 if (t != 0)
4537 return t;
4539 /* Aside from solitaires, consider all multi-reg groups first. */
4540 t = reload_nregs[r2] - reload_nregs[r1];
4541 if (t != 0)
4542 return t;
4544 /* Consider reloads in order of increasing reg-class number. */
4545 t = (int) rld[r1].class - (int) rld[r2].class;
4546 if (t != 0)
4547 return t;
4549 /* If reloads are equally urgent, sort by reload number,
4550 so that the results of qsort leave nothing to chance. */
4551 return r1 - r2;
4554 /* The following HARD_REG_SETs indicate when each hard register is
4555 used for a reload of various parts of the current insn. */
4557 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4558 static HARD_REG_SET reload_reg_used;
4559 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4560 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4561 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4562 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4563 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4564 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4565 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4566 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4567 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4568 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4569 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4570 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4571 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4572 static HARD_REG_SET reload_reg_used_in_op_addr;
4573 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4574 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4575 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4576 static HARD_REG_SET reload_reg_used_in_insn;
4577 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4578 static HARD_REG_SET reload_reg_used_in_other_addr;
4580 /* If reg is in use as a reload reg for any sort of reload. */
4581 static HARD_REG_SET reload_reg_used_at_all;
4583 /* If reg is use as an inherited reload. We just mark the first register
4584 in the group. */
4585 static HARD_REG_SET reload_reg_used_for_inherit;
4587 /* Records which hard regs are used in any way, either as explicit use or
4588 by being allocated to a pseudo during any point of the current insn. */
4589 static HARD_REG_SET reg_used_in_insn;
4591 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4592 TYPE. MODE is used to indicate how many consecutive regs are
4593 actually used. */
4595 static void
4596 mark_reload_reg_in_use (regno, opnum, type, mode)
4597 int regno;
4598 int opnum;
4599 enum reload_type type;
4600 enum machine_mode mode;
4602 int nregs = HARD_REGNO_NREGS (regno, mode);
4603 int i;
4605 for (i = regno; i < nregs + regno; i++)
4607 switch (type)
4609 case RELOAD_OTHER:
4610 SET_HARD_REG_BIT (reload_reg_used, i);
4611 break;
4613 case RELOAD_FOR_INPUT_ADDRESS:
4614 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4615 break;
4617 case RELOAD_FOR_INPADDR_ADDRESS:
4618 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4619 break;
4621 case RELOAD_FOR_OUTPUT_ADDRESS:
4622 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4623 break;
4625 case RELOAD_FOR_OUTADDR_ADDRESS:
4626 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4627 break;
4629 case RELOAD_FOR_OPERAND_ADDRESS:
4630 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4631 break;
4633 case RELOAD_FOR_OPADDR_ADDR:
4634 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4635 break;
4637 case RELOAD_FOR_OTHER_ADDRESS:
4638 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4639 break;
4641 case RELOAD_FOR_INPUT:
4642 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4643 break;
4645 case RELOAD_FOR_OUTPUT:
4646 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4647 break;
4649 case RELOAD_FOR_INSN:
4650 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4651 break;
4654 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4658 /* Similarly, but show REGNO is no longer in use for a reload. */
4660 static void
4661 clear_reload_reg_in_use (regno, opnum, type, mode)
4662 int regno;
4663 int opnum;
4664 enum reload_type type;
4665 enum machine_mode mode;
4667 int nregs = HARD_REGNO_NREGS (regno, mode);
4668 int start_regno, end_regno;
4669 int i;
4670 /* A complication is that for some reload types, inheritance might
4671 allow multiple reloads of the same types to share a reload register.
4672 We set check_opnum if we have to check only reloads with the same
4673 operand number, and check_any if we have to check all reloads. */
4674 int check_opnum = 0;
4675 int check_any = 0;
4676 HARD_REG_SET *used_in_set;
4678 switch (type)
4680 case RELOAD_OTHER:
4681 used_in_set = &reload_reg_used;
4682 break;
4684 case RELOAD_FOR_INPUT_ADDRESS:
4685 used_in_set = &reload_reg_used_in_input_addr[opnum];
4686 break;
4688 case RELOAD_FOR_INPADDR_ADDRESS:
4689 check_opnum = 1;
4690 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4691 break;
4693 case RELOAD_FOR_OUTPUT_ADDRESS:
4694 used_in_set = &reload_reg_used_in_output_addr[opnum];
4695 break;
4697 case RELOAD_FOR_OUTADDR_ADDRESS:
4698 check_opnum = 1;
4699 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4700 break;
4702 case RELOAD_FOR_OPERAND_ADDRESS:
4703 used_in_set = &reload_reg_used_in_op_addr;
4704 break;
4706 case RELOAD_FOR_OPADDR_ADDR:
4707 check_any = 1;
4708 used_in_set = &reload_reg_used_in_op_addr_reload;
4709 break;
4711 case RELOAD_FOR_OTHER_ADDRESS:
4712 used_in_set = &reload_reg_used_in_other_addr;
4713 check_any = 1;
4714 break;
4716 case RELOAD_FOR_INPUT:
4717 used_in_set = &reload_reg_used_in_input[opnum];
4718 break;
4720 case RELOAD_FOR_OUTPUT:
4721 used_in_set = &reload_reg_used_in_output[opnum];
4722 break;
4724 case RELOAD_FOR_INSN:
4725 used_in_set = &reload_reg_used_in_insn;
4726 break;
4727 default:
4728 abort ();
4730 /* We resolve conflicts with remaining reloads of the same type by
4731 excluding the intervals of of reload registers by them from the
4732 interval of freed reload registers. Since we only keep track of
4733 one set of interval bounds, we might have to exclude somewhat
4734 more then what would be necessary if we used a HARD_REG_SET here.
4735 But this should only happen very infrequently, so there should
4736 be no reason to worry about it. */
4738 start_regno = regno;
4739 end_regno = regno + nregs;
4740 if (check_opnum || check_any)
4742 for (i = n_reloads - 1; i >= 0; i--)
4744 if (rld[i].when_needed == type
4745 && (check_any || rld[i].opnum == opnum)
4746 && rld[i].reg_rtx)
4748 int conflict_start = true_regnum (rld[i].reg_rtx);
4749 int conflict_end
4750 = (conflict_start
4751 + HARD_REGNO_NREGS (conflict_start, reload_mode[i]));
4753 /* If there is an overlap with the first to-be-freed register,
4754 adjust the interval start. */
4755 if (conflict_start <= start_regno && conflict_end > start_regno)
4756 start_regno = conflict_end;
4757 /* Otherwise, if there is a conflict with one of the other
4758 to-be-freed registers, adjust the interval end. */
4759 if (conflict_start > start_regno && conflict_start < end_regno)
4760 end_regno = conflict_start;
4764 for (i = start_regno; i < end_regno; i++)
4765 CLEAR_HARD_REG_BIT (*used_in_set, i);
4768 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4769 specified by OPNUM and TYPE. */
4771 static int
4772 reload_reg_free_p (regno, opnum, type)
4773 int regno;
4774 int opnum;
4775 enum reload_type type;
4777 int i;
4779 /* In use for a RELOAD_OTHER means it's not available for anything. */
4780 if (TEST_HARD_REG_BIT (reload_reg_used, regno))
4781 return 0;
4783 switch (type)
4785 case RELOAD_OTHER:
4786 /* In use for anything means we can't use it for RELOAD_OTHER. */
4787 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4788 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4789 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4790 return 0;
4792 for (i = 0; i < reload_n_operands; i++)
4793 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4794 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4795 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4796 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4797 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4798 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4799 return 0;
4801 return 1;
4803 case RELOAD_FOR_INPUT:
4804 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4805 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4806 return 0;
4808 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4809 return 0;
4811 /* If it is used for some other input, can't use it. */
4812 for (i = 0; i < reload_n_operands; i++)
4813 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4814 return 0;
4816 /* If it is used in a later operand's address, can't use it. */
4817 for (i = opnum + 1; i < reload_n_operands; i++)
4818 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4819 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4820 return 0;
4822 return 1;
4824 case RELOAD_FOR_INPUT_ADDRESS:
4825 /* Can't use a register if it is used for an input address for this
4826 operand or used as an input in an earlier one. */
4827 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4828 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4829 return 0;
4831 for (i = 0; i < opnum; i++)
4832 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4833 return 0;
4835 return 1;
4837 case RELOAD_FOR_INPADDR_ADDRESS:
4838 /* Can't use a register if it is used for an input address
4839 for this operand or used as an input in an earlier
4840 one. */
4841 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4842 return 0;
4844 for (i = 0; i < opnum; i++)
4845 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4846 return 0;
4848 return 1;
4850 case RELOAD_FOR_OUTPUT_ADDRESS:
4851 /* Can't use a register if it is used for an output address for this
4852 operand or used as an output in this or a later operand. */
4853 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4854 return 0;
4856 for (i = opnum; i < reload_n_operands; i++)
4857 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4858 return 0;
4860 return 1;
4862 case RELOAD_FOR_OUTADDR_ADDRESS:
4863 /* Can't use a register if it is used for an output address
4864 for this operand or used as an output in this or a
4865 later operand. */
4866 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4867 return 0;
4869 for (i = opnum; i < reload_n_operands; i++)
4870 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4871 return 0;
4873 return 1;
4875 case RELOAD_FOR_OPERAND_ADDRESS:
4876 for (i = 0; i < reload_n_operands; i++)
4877 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4878 return 0;
4880 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4881 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4883 case RELOAD_FOR_OPADDR_ADDR:
4884 for (i = 0; i < reload_n_operands; i++)
4885 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4886 return 0;
4888 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4890 case RELOAD_FOR_OUTPUT:
4891 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4892 outputs, or an operand address for this or an earlier output. */
4893 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4894 return 0;
4896 for (i = 0; i < reload_n_operands; i++)
4897 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4898 return 0;
4900 for (i = 0; i <= opnum; i++)
4901 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4902 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4903 return 0;
4905 return 1;
4907 case RELOAD_FOR_INSN:
4908 for (i = 0; i < reload_n_operands; i++)
4909 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4910 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4911 return 0;
4913 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4914 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4916 case RELOAD_FOR_OTHER_ADDRESS:
4917 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4919 abort ();
4922 /* Return 1 if the value in reload reg REGNO, as used by a reload
4923 needed for the part of the insn specified by OPNUM and TYPE,
4924 is still available in REGNO at the end of the insn.
4926 We can assume that the reload reg was already tested for availability
4927 at the time it is needed, and we should not check this again,
4928 in case the reg has already been marked in use. */
4930 static int
4931 reload_reg_reaches_end_p (regno, opnum, type)
4932 int regno;
4933 int opnum;
4934 enum reload_type type;
4936 int i;
4938 switch (type)
4940 case RELOAD_OTHER:
4941 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4942 its value must reach the end. */
4943 return 1;
4945 /* If this use is for part of the insn,
4946 its value reaches if no subsequent part uses the same register.
4947 Just like the above function, don't try to do this with lots
4948 of fallthroughs. */
4950 case RELOAD_FOR_OTHER_ADDRESS:
4951 /* Here we check for everything else, since these don't conflict
4952 with anything else and everything comes later. */
4954 for (i = 0; i < reload_n_operands; i++)
4955 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4956 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4957 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4958 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4959 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4960 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4961 return 0;
4963 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4964 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4965 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4967 case RELOAD_FOR_INPUT_ADDRESS:
4968 case RELOAD_FOR_INPADDR_ADDRESS:
4969 /* Similar, except that we check only for this and subsequent inputs
4970 and the address of only subsequent inputs and we do not need
4971 to check for RELOAD_OTHER objects since they are known not to
4972 conflict. */
4974 for (i = opnum; i < reload_n_operands; i++)
4975 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4976 return 0;
4978 for (i = opnum + 1; i < reload_n_operands; i++)
4979 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4980 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4981 return 0;
4983 for (i = 0; i < reload_n_operands; i++)
4984 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4985 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4986 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4987 return 0;
4989 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4990 return 0;
4992 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4993 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
4995 case RELOAD_FOR_INPUT:
4996 /* Similar to input address, except we start at the next operand for
4997 both input and input address and we do not check for
4998 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4999 would conflict. */
5001 for (i = opnum + 1; i < reload_n_operands; i++)
5002 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5003 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5004 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5005 return 0;
5007 /* ... fall through ... */
5009 case RELOAD_FOR_OPERAND_ADDRESS:
5010 /* Check outputs and their addresses. */
5012 for (i = 0; i < reload_n_operands; i++)
5013 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5014 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5015 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5016 return 0;
5018 return 1;
5020 case RELOAD_FOR_OPADDR_ADDR:
5021 for (i = 0; i < reload_n_operands; i++)
5022 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5023 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5024 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5025 return 0;
5027 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5028 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
5030 case RELOAD_FOR_INSN:
5031 /* These conflict with other outputs with RELOAD_OTHER. So
5032 we need only check for output addresses. */
5034 opnum = -1;
5036 /* ... fall through ... */
5038 case RELOAD_FOR_OUTPUT:
5039 case RELOAD_FOR_OUTPUT_ADDRESS:
5040 case RELOAD_FOR_OUTADDR_ADDRESS:
5041 /* We already know these can't conflict with a later output. So the
5042 only thing to check are later output addresses. */
5043 for (i = opnum + 1; i < reload_n_operands; i++)
5044 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5045 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5046 return 0;
5048 return 1;
5051 abort ();
5054 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
5055 Return 0 otherwise.
5057 This function uses the same algorithm as reload_reg_free_p above. */
5060 reloads_conflict (r1, r2)
5061 int r1, r2;
5063 enum reload_type r1_type = rld[r1].when_needed;
5064 enum reload_type r2_type = rld[r2].when_needed;
5065 int r1_opnum = rld[r1].opnum;
5066 int r2_opnum = rld[r2].opnum;
5068 /* RELOAD_OTHER conflicts with everything. */
5069 if (r2_type == RELOAD_OTHER)
5070 return 1;
5072 /* Otherwise, check conflicts differently for each type. */
5074 switch (r1_type)
5076 case RELOAD_FOR_INPUT:
5077 return (r2_type == RELOAD_FOR_INSN
5078 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
5079 || r2_type == RELOAD_FOR_OPADDR_ADDR
5080 || r2_type == RELOAD_FOR_INPUT
5081 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
5082 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
5083 && r2_opnum > r1_opnum));
5085 case RELOAD_FOR_INPUT_ADDRESS:
5086 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
5087 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5089 case RELOAD_FOR_INPADDR_ADDRESS:
5090 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
5091 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5093 case RELOAD_FOR_OUTPUT_ADDRESS:
5094 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
5095 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
5097 case RELOAD_FOR_OUTADDR_ADDRESS:
5098 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
5099 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
5101 case RELOAD_FOR_OPERAND_ADDRESS:
5102 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
5103 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5105 case RELOAD_FOR_OPADDR_ADDR:
5106 return (r2_type == RELOAD_FOR_INPUT
5107 || r2_type == RELOAD_FOR_OPADDR_ADDR);
5109 case RELOAD_FOR_OUTPUT:
5110 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
5111 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
5112 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
5113 && r2_opnum >= r1_opnum));
5115 case RELOAD_FOR_INSN:
5116 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
5117 || r2_type == RELOAD_FOR_INSN
5118 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5120 case RELOAD_FOR_OTHER_ADDRESS:
5121 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
5123 case RELOAD_OTHER:
5124 return 1;
5126 default:
5127 abort ();
5131 /* Vector of reload-numbers showing the order in which the reloads should
5132 be processed. */
5133 short reload_order[MAX_RELOADS];
5135 /* Indexed by reload number, 1 if incoming value
5136 inherited from previous insns. */
5137 char reload_inherited[MAX_RELOADS];
5139 /* For an inherited reload, this is the insn the reload was inherited from,
5140 if we know it. Otherwise, this is 0. */
5141 rtx reload_inheritance_insn[MAX_RELOADS];
5143 /* If non-zero, this is a place to get the value of the reload,
5144 rather than using reload_in. */
5145 rtx reload_override_in[MAX_RELOADS];
5147 /* For each reload, the hard register number of the register used,
5148 or -1 if we did not need a register for this reload. */
5149 int reload_spill_index[MAX_RELOADS];
5151 /* Return 1 if the value in reload reg REGNO, as used by a reload
5152 needed for the part of the insn specified by OPNUM and TYPE,
5153 may be used to load VALUE into it.
5155 Other read-only reloads with the same value do not conflict
5156 unless OUT is non-zero and these other reloads have to live while
5157 output reloads live.
5158 If OUT is CONST0_RTX, this is a special case: it means that the
5159 test should not be for using register REGNO as reload register, but
5160 for copying from register REGNO into the reload register.
5162 RELOADNUM is the number of the reload we want to load this value for;
5163 a reload does not conflict with itself.
5165 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
5166 reloads that load an address for the very reload we are considering.
5168 The caller has to make sure that there is no conflict with the return
5169 register. */
5170 static int
5171 reload_reg_free_for_value_p (regno, opnum, type, value, out, reloadnum,
5172 ignore_address_reloads)
5173 int regno;
5174 int opnum;
5175 enum reload_type type;
5176 rtx value, out;
5177 int reloadnum;
5178 int ignore_address_reloads;
5180 int time1;
5181 int i;
5182 int copy = 0;
5184 if (out == const0_rtx)
5186 copy = 1;
5187 out = NULL_RTX;
5190 /* We use some pseudo 'time' value to check if the lifetimes of the
5191 new register use would overlap with the one of a previous reload
5192 that is not read-only or uses a different value.
5193 The 'time' used doesn't have to be linear in any shape or form, just
5194 monotonic.
5195 Some reload types use different 'buckets' for each operand.
5196 So there are MAX_RECOG_OPERANDS different time values for each
5197 such reload type.
5198 We compute TIME1 as the time when the register for the prospective
5199 new reload ceases to be live, and TIME2 for each existing
5200 reload as the time when that the reload register of that reload
5201 becomes live.
5202 Where there is little to be gained by exact lifetime calculations,
5203 we just make conservative assumptions, i.e. a longer lifetime;
5204 this is done in the 'default:' cases. */
5205 switch (type)
5207 case RELOAD_FOR_OTHER_ADDRESS:
5208 time1 = 0;
5209 break;
5210 case RELOAD_OTHER:
5211 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
5212 break;
5213 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
5214 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
5215 respectively, to the time values for these, we get distinct time
5216 values. To get distinct time values for each operand, we have to
5217 multiply opnum by at least three. We round that up to four because
5218 multiply by four is often cheaper. */
5219 case RELOAD_FOR_INPADDR_ADDRESS:
5220 time1 = opnum * 4 + 2;
5221 break;
5222 case RELOAD_FOR_INPUT_ADDRESS:
5223 time1 = opnum * 4 + 3;
5224 break;
5225 case RELOAD_FOR_INPUT:
5226 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
5227 executes (inclusive). */
5228 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
5229 break;
5230 case RELOAD_FOR_OPADDR_ADDR:
5231 /* opnum * 4 + 4
5232 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
5233 time1 = MAX_RECOG_OPERANDS * 4 + 1;
5234 break;
5235 case RELOAD_FOR_OPERAND_ADDRESS:
5236 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
5237 is executed. */
5238 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
5239 break;
5240 case RELOAD_FOR_OUTADDR_ADDRESS:
5241 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
5242 break;
5243 case RELOAD_FOR_OUTPUT_ADDRESS:
5244 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
5245 break;
5246 default:
5247 time1 = MAX_RECOG_OPERANDS * 5 + 5;
5250 for (i = 0; i < n_reloads; i++)
5252 rtx reg = rld[i].reg_rtx;
5253 if (reg && GET_CODE (reg) == REG
5254 && ((unsigned) regno - true_regnum (reg)
5255 <= HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)) - (unsigned)1)
5256 && i != reloadnum)
5258 if (! rld[i].in || ! rtx_equal_p (rld[i].in, value)
5259 || rld[i].out || out)
5261 int time2;
5262 switch (rld[i].when_needed)
5264 case RELOAD_FOR_OTHER_ADDRESS:
5265 time2 = 0;
5266 break;
5267 case RELOAD_FOR_INPADDR_ADDRESS:
5268 /* find_reloads makes sure that a
5269 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
5270 by at most one - the first -
5271 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
5272 address reload is inherited, the address address reload
5273 goes away, so we can ignore this conflict. */
5274 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
5275 && ignore_address_reloads
5276 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
5277 Then the address address is still needed to store
5278 back the new address. */
5279 && ! rld[reloadnum].out)
5280 continue;
5281 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
5282 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
5283 reloads go away. */
5284 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5285 && ignore_address_reloads
5286 /* Unless we are reloading an auto_inc expression. */
5287 && ! rld[reloadnum].out)
5288 continue;
5289 time2 = rld[i].opnum * 4 + 2;
5290 break;
5291 case RELOAD_FOR_INPUT_ADDRESS:
5292 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5293 && ignore_address_reloads
5294 && ! rld[reloadnum].out)
5295 continue;
5296 time2 = rld[i].opnum * 4 + 3;
5297 break;
5298 case RELOAD_FOR_INPUT:
5299 time2 = rld[i].opnum * 4 + 4;
5300 break;
5301 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
5302 == MAX_RECOG_OPERAND * 4 */
5303 case RELOAD_FOR_OPADDR_ADDR:
5304 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
5305 && ignore_address_reloads
5306 && ! rld[reloadnum].out)
5307 continue;
5308 time2 = MAX_RECOG_OPERANDS * 4 + 1;
5309 break;
5310 case RELOAD_FOR_OPERAND_ADDRESS:
5311 time2 = MAX_RECOG_OPERANDS * 4 + 2;
5312 break;
5313 case RELOAD_FOR_INSN:
5314 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5315 break;
5316 case RELOAD_FOR_OUTPUT:
5317 /* All RELOAD_FOR_OUTPUT reloads become live just after the
5318 instruction is executed. */
5319 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5320 break;
5321 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
5322 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
5323 value. */
5324 case RELOAD_FOR_OUTADDR_ADDRESS:
5325 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
5326 && ignore_address_reloads
5327 && ! rld[reloadnum].out)
5328 continue;
5329 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
5330 break;
5331 case RELOAD_FOR_OUTPUT_ADDRESS:
5332 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
5333 break;
5334 case RELOAD_OTHER:
5335 /* If there is no conflict in the input part, handle this
5336 like an output reload. */
5337 if (! rld[i].in || rtx_equal_p (rld[i].in, value))
5339 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5340 break;
5342 time2 = 1;
5343 /* RELOAD_OTHER might be live beyond instruction execution,
5344 but this is not obvious when we set time2 = 1. So check
5345 here if there might be a problem with the new reload
5346 clobbering the register used by the RELOAD_OTHER. */
5347 if (out)
5348 return 0;
5349 break;
5350 default:
5351 return 0;
5353 if ((time1 >= time2
5354 && (! rld[i].in || rld[i].out
5355 || ! rtx_equal_p (rld[i].in, value)))
5356 || (out && rld[reloadnum].out_reg
5357 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
5358 return 0;
5362 return 1;
5365 /* Find a spill register to use as a reload register for reload R.
5366 LAST_RELOAD is non-zero if this is the last reload for the insn being
5367 processed.
5369 Set rld[R].reg_rtx to the register allocated.
5371 If NOERROR is nonzero, we return 1 if successful,
5372 or 0 if we couldn't find a spill reg and we didn't change anything. */
5374 static int
5375 allocate_reload_reg (chain, r, last_reload, noerror)
5376 struct insn_chain *chain;
5377 int r;
5378 int last_reload;
5379 int noerror;
5381 rtx insn = chain->insn;
5382 int i, pass, count, regno;
5383 rtx new;
5385 /* If we put this reload ahead, thinking it is a group,
5386 then insist on finding a group. Otherwise we can grab a
5387 reg that some other reload needs.
5388 (That can happen when we have a 68000 DATA_OR_FP_REG
5389 which is a group of data regs or one fp reg.)
5390 We need not be so restrictive if there are no more reloads
5391 for this insn.
5393 ??? Really it would be nicer to have smarter handling
5394 for that kind of reg class, where a problem like this is normal.
5395 Perhaps those classes should be avoided for reloading
5396 by use of more alternatives. */
5398 int force_group = reload_nregs[r] > 1 && ! last_reload;
5400 /* If we want a single register and haven't yet found one,
5401 take any reg in the right class and not in use.
5402 If we want a consecutive group, here is where we look for it.
5404 We use two passes so we can first look for reload regs to
5405 reuse, which are already in use for other reloads in this insn,
5406 and only then use additional registers.
5407 I think that maximizing reuse is needed to make sure we don't
5408 run out of reload regs. Suppose we have three reloads, and
5409 reloads A and B can share regs. These need two regs.
5410 Suppose A and B are given different regs.
5411 That leaves none for C. */
5412 for (pass = 0; pass < 2; pass++)
5414 /* I is the index in spill_regs.
5415 We advance it round-robin between insns to use all spill regs
5416 equally, so that inherited reloads have a chance
5417 of leapfrogging each other. Don't do this, however, when we have
5418 group needs and failure would be fatal; if we only have a relatively
5419 small number of spill registers, and more than one of them has
5420 group needs, then by starting in the middle, we may end up
5421 allocating the first one in such a way that we are not left with
5422 sufficient groups to handle the rest. */
5424 if (noerror || ! force_group)
5425 i = last_spill_reg;
5426 else
5427 i = -1;
5429 for (count = 0; count < n_spills; count++)
5431 int class = (int) rld[r].class;
5432 int regnum;
5434 i++;
5435 if (i >= n_spills)
5436 i -= n_spills;
5437 regnum = spill_regs[i];
5439 if ((reload_reg_free_p (regnum, rld[r].opnum,
5440 rld[r].when_needed)
5441 || (rld[r].in
5442 /* We check reload_reg_used to make sure we
5443 don't clobber the return register. */
5444 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5445 && reload_reg_free_for_value_p (regnum,
5446 rld[r].opnum,
5447 rld[r].when_needed,
5448 rld[r].in,
5449 rld[r].out, r, 1)))
5450 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5451 && HARD_REGNO_MODE_OK (regnum, reload_mode[r])
5452 /* Look first for regs to share, then for unshared. But
5453 don't share regs used for inherited reloads; they are
5454 the ones we want to preserve. */
5455 && (pass
5456 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5457 regnum)
5458 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5459 regnum))))
5461 int nr = HARD_REGNO_NREGS (regnum, reload_mode[r]);
5462 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5463 (on 68000) got us two FP regs. If NR is 1,
5464 we would reject both of them. */
5465 if (force_group)
5466 nr = CLASS_MAX_NREGS (rld[r].class, reload_mode[r]);
5467 /* If we need only one reg, we have already won. */
5468 if (nr == 1)
5470 /* But reject a single reg if we demand a group. */
5471 if (force_group)
5472 continue;
5473 break;
5475 /* Otherwise check that as many consecutive regs as we need
5476 are available here.
5477 Also, don't use for a group registers that are
5478 needed for nongroups. */
5479 if (! TEST_HARD_REG_BIT (chain->counted_for_nongroups, regnum))
5480 while (nr > 1)
5482 regno = regnum + nr - 1;
5483 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5484 && spill_reg_order[regno] >= 0
5485 && reload_reg_free_p (regno, rld[r].opnum,
5486 rld[r].when_needed)
5487 && ! TEST_HARD_REG_BIT (chain->counted_for_nongroups,
5488 regno)))
5489 break;
5490 nr--;
5492 if (nr == 1)
5493 break;
5497 /* If we found something on pass 1, omit pass 2. */
5498 if (count < n_spills)
5499 break;
5502 /* We should have found a spill register by now. */
5503 if (count == n_spills)
5505 if (noerror)
5506 return 0;
5507 goto failure;
5510 /* I is the index in SPILL_REG_RTX of the reload register we are to
5511 allocate. Get an rtx for it and find its register number. */
5513 new = spill_reg_rtx[i];
5515 if (new == 0 || GET_MODE (new) != reload_mode[r])
5516 spill_reg_rtx[i] = new
5517 = gen_rtx_REG (reload_mode[r], spill_regs[i]);
5519 regno = true_regnum (new);
5521 /* Detect when the reload reg can't hold the reload mode.
5522 This used to be one `if', but Sequent compiler can't handle that. */
5523 if (HARD_REGNO_MODE_OK (regno, reload_mode[r]))
5525 enum machine_mode test_mode = VOIDmode;
5526 if (rld[r].in)
5527 test_mode = GET_MODE (rld[r].in);
5528 /* If rld[r].in has VOIDmode, it means we will load it
5529 in whatever mode the reload reg has: to wit, reload_mode[r].
5530 We have already tested that for validity. */
5531 /* Aside from that, we need to test that the expressions
5532 to reload from or into have modes which are valid for this
5533 reload register. Otherwise the reload insns would be invalid. */
5534 if (! (rld[r].in != 0 && test_mode != VOIDmode
5535 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5536 if (! (rld[r].out != 0
5537 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5539 /* The reg is OK. */
5540 last_spill_reg = i;
5542 /* Mark as in use for this insn the reload regs we use
5543 for this. */
5544 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5545 rld[r].when_needed, reload_mode[r]);
5547 rld[r].reg_rtx = new;
5548 reload_spill_index[r] = spill_regs[i];
5549 return 1;
5553 /* The reg is not OK. */
5554 if (noerror)
5555 return 0;
5557 failure:
5558 if (asm_noperands (PATTERN (insn)) < 0)
5559 /* It's the compiler's fault. */
5560 fatal_insn ("Could not find a spill register", insn);
5562 /* It's the user's fault; the operand's mode and constraint
5563 don't match. Disable this reload so we don't crash in final. */
5564 error_for_asm (insn,
5565 "`asm' operand constraint incompatible with operand size");
5566 rld[r].in = 0;
5567 rld[r].out = 0;
5568 rld[r].reg_rtx = 0;
5569 rld[r].optional = 1;
5570 rld[r].secondary_p = 1;
5572 return 1;
5575 /* Assign hard reg targets for the pseudo-registers we must reload
5576 into hard regs for this insn.
5577 Also output the instructions to copy them in and out of the hard regs.
5579 For machines with register classes, we are responsible for
5580 finding a reload reg in the proper class. */
5582 static void
5583 choose_reload_regs (chain)
5584 struct insn_chain *chain;
5586 rtx insn = chain->insn;
5587 register int i, j;
5588 int max_group_size = 1;
5589 enum reg_class group_class = NO_REGS;
5590 int inheritance;
5591 int pass;
5593 rtx save_reload_reg_rtx[MAX_RELOADS];
5594 char save_reload_inherited[MAX_RELOADS];
5595 rtx save_reload_inheritance_insn[MAX_RELOADS];
5596 rtx save_reload_override_in[MAX_RELOADS];
5597 int save_reload_spill_index[MAX_RELOADS];
5598 HARD_REG_SET save_reload_reg_used;
5599 HARD_REG_SET save_reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
5600 HARD_REG_SET save_reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
5601 HARD_REG_SET save_reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
5602 HARD_REG_SET save_reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
5603 HARD_REG_SET save_reload_reg_used_in_input[MAX_RECOG_OPERANDS];
5604 HARD_REG_SET save_reload_reg_used_in_output[MAX_RECOG_OPERANDS];
5605 HARD_REG_SET save_reload_reg_used_in_op_addr;
5606 HARD_REG_SET save_reload_reg_used_in_op_addr_reload;
5607 HARD_REG_SET save_reload_reg_used_in_insn;
5608 HARD_REG_SET save_reload_reg_used_in_other_addr;
5609 HARD_REG_SET save_reload_reg_used_at_all;
5611 bzero (reload_inherited, MAX_RELOADS);
5612 bzero ((char *) reload_inheritance_insn, MAX_RELOADS * sizeof (rtx));
5613 bzero ((char *) reload_override_in, MAX_RELOADS * sizeof (rtx));
5615 CLEAR_HARD_REG_SET (reload_reg_used);
5616 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5617 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5618 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5619 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5620 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5622 CLEAR_HARD_REG_SET (reg_used_in_insn);
5624 HARD_REG_SET tmp;
5625 REG_SET_TO_HARD_REG_SET (tmp, chain->live_before);
5626 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5627 REG_SET_TO_HARD_REG_SET (tmp, chain->live_after);
5628 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5629 compute_use_by_pseudos (&reg_used_in_insn, chain->live_before);
5630 compute_use_by_pseudos (&reg_used_in_insn, chain->live_after);
5632 for (i = 0; i < reload_n_operands; i++)
5634 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5635 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5636 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5637 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5638 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5639 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5642 IOR_COMPL_HARD_REG_SET (reload_reg_used, chain->used_spill_regs);
5644 #if 0 /* Not needed, now that we can always retry without inheritance. */
5645 /* See if we have more mandatory reloads than spill regs.
5646 If so, then we cannot risk optimizations that could prevent
5647 reloads from sharing one spill register.
5649 Since we will try finding a better register than reload_reg_rtx
5650 unless it is equal to reload_in or reload_out, count such reloads. */
5653 int tem = 0;
5654 for (j = 0; j < n_reloads; j++)
5655 if (! rld[j].optional
5656 && (rld[j].in != 0 || rld[j].out != 0 || rld[j].secondary_p)
5657 && (rld[j].reg_rtx == 0
5658 || (! rtx_equal_p (rld[j].reg_rtx, rld[j].in)
5659 && ! rtx_equal_p (rld[j].reg_rtx, rld[j].out))))
5660 tem++;
5661 if (tem > n_spills)
5662 must_reuse = 1;
5664 #endif
5666 /* In order to be certain of getting the registers we need,
5667 we must sort the reloads into order of increasing register class.
5668 Then our grabbing of reload registers will parallel the process
5669 that provided the reload registers.
5671 Also note whether any of the reloads wants a consecutive group of regs.
5672 If so, record the maximum size of the group desired and what
5673 register class contains all the groups needed by this insn. */
5675 for (j = 0; j < n_reloads; j++)
5677 reload_order[j] = j;
5678 reload_spill_index[j] = -1;
5680 reload_mode[j] = ((rld[j].inmode == VOIDmode
5681 || (GET_MODE_SIZE (rld[j].outmode)
5682 > GET_MODE_SIZE (rld[j].inmode)))
5683 ? rld[j].outmode : rld[j].inmode);
5685 reload_nregs[j] = CLASS_MAX_NREGS (rld[j].class, reload_mode[j]);
5687 if (reload_nregs[j] > 1)
5689 max_group_size = MAX (reload_nregs[j], max_group_size);
5690 group_class = reg_class_superunion[(int)rld[j].class][(int)group_class];
5693 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5694 /* If we have already decided to use a certain register,
5695 don't use it in another way. */
5696 if (rld[j].reg_rtx)
5697 mark_reload_reg_in_use (REGNO (rld[j].reg_rtx), rld[j].opnum,
5698 rld[j].when_needed, reload_mode[j]);
5701 if (n_reloads > 1)
5702 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5704 bcopy (reload_inherited, save_reload_inherited, sizeof reload_inherited);
5705 bcopy ((char *) reload_inheritance_insn,
5706 (char *) save_reload_inheritance_insn,
5707 sizeof reload_inheritance_insn);
5708 bcopy ((char *) reload_override_in, (char *) save_reload_override_in,
5709 sizeof reload_override_in);
5710 bcopy ((char *) reload_spill_index, (char *) save_reload_spill_index,
5711 sizeof reload_spill_index);
5712 COPY_HARD_REG_SET (save_reload_reg_used, reload_reg_used);
5713 COPY_HARD_REG_SET (save_reload_reg_used_at_all, reload_reg_used_at_all);
5714 COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr,
5715 reload_reg_used_in_op_addr);
5717 COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr_reload,
5718 reload_reg_used_in_op_addr_reload);
5720 COPY_HARD_REG_SET (save_reload_reg_used_in_insn,
5721 reload_reg_used_in_insn);
5722 COPY_HARD_REG_SET (save_reload_reg_used_in_other_addr,
5723 reload_reg_used_in_other_addr);
5725 for (i = 0; i < reload_n_operands; i++)
5727 COPY_HARD_REG_SET (save_reload_reg_used_in_output[i],
5728 reload_reg_used_in_output[i]);
5729 COPY_HARD_REG_SET (save_reload_reg_used_in_input[i],
5730 reload_reg_used_in_input[i]);
5731 COPY_HARD_REG_SET (save_reload_reg_used_in_input_addr[i],
5732 reload_reg_used_in_input_addr[i]);
5733 COPY_HARD_REG_SET (save_reload_reg_used_in_inpaddr_addr[i],
5734 reload_reg_used_in_inpaddr_addr[i]);
5735 COPY_HARD_REG_SET (save_reload_reg_used_in_output_addr[i],
5736 reload_reg_used_in_output_addr[i]);
5737 COPY_HARD_REG_SET (save_reload_reg_used_in_outaddr_addr[i],
5738 reload_reg_used_in_outaddr_addr[i]);
5741 /* If -O, try first with inheritance, then turning it off.
5742 If not -O, don't do inheritance.
5743 Using inheritance when not optimizing leads to paradoxes
5744 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5745 because one side of the comparison might be inherited. */
5747 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5749 /* Process the reloads in order of preference just found.
5750 Beyond this point, subregs can be found in reload_reg_rtx.
5752 This used to look for an existing reloaded home for all
5753 of the reloads, and only then perform any new reloads.
5754 But that could lose if the reloads were done out of reg-class order
5755 because a later reload with a looser constraint might have an old
5756 home in a register needed by an earlier reload with a tighter constraint.
5758 To solve this, we make two passes over the reloads, in the order
5759 described above. In the first pass we try to inherit a reload
5760 from a previous insn. If there is a later reload that needs a
5761 class that is a proper subset of the class being processed, we must
5762 also allocate a spill register during the first pass.
5764 Then make a second pass over the reloads to allocate any reloads
5765 that haven't been given registers yet. */
5767 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5769 for (j = 0; j < n_reloads; j++)
5771 register int r = reload_order[j];
5772 rtx search_equiv = NULL_RTX;
5774 /* Ignore reloads that got marked inoperative. */
5775 if (rld[r].out == 0 && rld[r].in == 0
5776 && ! rld[r].secondary_p)
5777 continue;
5779 /* If find_reloads chose to use reload_in or reload_out as a reload
5780 register, we don't need to chose one. Otherwise, try even if it
5781 found one since we might save an insn if we find the value lying
5782 around.
5783 Try also when reload_in is a pseudo without a hard reg. */
5784 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5785 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5786 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5787 && GET_CODE (rld[r].in) != MEM
5788 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5789 continue;
5791 #if 0 /* No longer needed for correct operation.
5792 It might give better code, or might not; worth an experiment? */
5793 /* If this is an optional reload, we can't inherit from earlier insns
5794 until we are sure that any non-optional reloads have been allocated.
5795 The following code takes advantage of the fact that optional reloads
5796 are at the end of reload_order. */
5797 if (rld[r].optional != 0)
5798 for (i = 0; i < j; i++)
5799 if ((rld[reload_order[i]].out != 0
5800 || rld[reload_order[i]].in != 0
5801 || rld[reload_order[i]].secondary_p)
5802 && ! rld[reload_order[i]].optional
5803 && rld[reload_order[i]].reg_rtx == 0)
5804 allocate_reload_reg (chain, reload_order[i], 0, inheritance);
5805 #endif
5807 /* First see if this pseudo is already available as reloaded
5808 for a previous insn. We cannot try to inherit for reloads
5809 that are smaller than the maximum number of registers needed
5810 for groups unless the register we would allocate cannot be used
5811 for the groups.
5813 We could check here to see if this is a secondary reload for
5814 an object that is already in a register of the desired class.
5815 This would avoid the need for the secondary reload register.
5816 But this is complex because we can't easily determine what
5817 objects might want to be loaded via this reload. So let a
5818 register be allocated here. In `emit_reload_insns' we suppress
5819 one of the loads in the case described above. */
5821 if (inheritance)
5823 int word = 0;
5824 register int regno = -1;
5825 enum machine_mode mode = VOIDmode;
5827 if (rld[r].in == 0)
5829 else if (GET_CODE (rld[r].in) == REG)
5831 regno = REGNO (rld[r].in);
5832 mode = GET_MODE (rld[r].in);
5834 else if (GET_CODE (rld[r].in_reg) == REG)
5836 regno = REGNO (rld[r].in_reg);
5837 mode = GET_MODE (rld[r].in_reg);
5839 else if (GET_CODE (rld[r].in_reg) == SUBREG
5840 && GET_CODE (SUBREG_REG (rld[r].in_reg)) == REG)
5842 word = SUBREG_WORD (rld[r].in_reg);
5843 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5844 if (regno < FIRST_PSEUDO_REGISTER)
5845 regno += word;
5846 mode = GET_MODE (rld[r].in_reg);
5848 #ifdef AUTO_INC_DEC
5849 else if ((GET_CODE (rld[r].in_reg) == PRE_INC
5850 || GET_CODE (rld[r].in_reg) == PRE_DEC
5851 || GET_CODE (rld[r].in_reg) == POST_INC
5852 || GET_CODE (rld[r].in_reg) == POST_DEC)
5853 && GET_CODE (XEXP (rld[r].in_reg, 0)) == REG)
5855 regno = REGNO (XEXP (rld[r].in_reg, 0));
5856 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5857 rld[r].out = rld[r].in;
5859 #endif
5860 #if 0
5861 /* This won't work, since REGNO can be a pseudo reg number.
5862 Also, it takes much more hair to keep track of all the things
5863 that can invalidate an inherited reload of part of a pseudoreg. */
5864 else if (GET_CODE (rld[r].in) == SUBREG
5865 && GET_CODE (SUBREG_REG (rld[r].in)) == REG)
5866 regno = REGNO (SUBREG_REG (rld[r].in)) + SUBREG_WORD (rld[r].in);
5867 #endif
5869 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5871 enum reg_class class = rld[r].class, last_class;
5872 rtx last_reg = reg_last_reload_reg[regno];
5874 i = REGNO (last_reg) + word;
5875 last_class = REGNO_REG_CLASS (i);
5876 if ((GET_MODE_SIZE (GET_MODE (last_reg))
5877 >= GET_MODE_SIZE (mode) + word * UNITS_PER_WORD)
5878 && reg_reloaded_contents[i] == regno
5879 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
5880 && HARD_REGNO_MODE_OK (i, reload_mode[r])
5881 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5882 /* Even if we can't use this register as a reload
5883 register, we might use it for reload_override_in,
5884 if copying it to the desired class is cheap
5885 enough. */
5886 || ((REGISTER_MOVE_COST (last_class, class)
5887 < MEMORY_MOVE_COST (mode, class, 1))
5888 #ifdef SECONDARY_INPUT_RELOAD_CLASS
5889 && (SECONDARY_INPUT_RELOAD_CLASS (class, mode,
5890 last_reg)
5891 == NO_REGS)
5892 #endif
5893 #ifdef SECONDARY_MEMORY_NEEDED
5894 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5895 mode)
5896 #endif
5899 && (reload_nregs[r] == max_group_size
5900 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5902 && reload_reg_free_for_value_p (i, rld[r].opnum,
5903 rld[r].when_needed,
5904 rld[r].in,
5905 const0_rtx, r, 1))
5907 /* If a group is needed, verify that all the subsequent
5908 registers still have their values intact. */
5909 int nr
5910 = HARD_REGNO_NREGS (i, reload_mode[r]);
5911 int k;
5913 for (k = 1; k < nr; k++)
5914 if (reg_reloaded_contents[i + k] != regno
5915 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
5916 break;
5918 if (k == nr)
5920 int i1;
5922 last_reg = (GET_MODE (last_reg) == mode
5923 ? last_reg : gen_rtx_REG (mode, i));
5925 /* We found a register that contains the
5926 value we need. If this register is the
5927 same as an `earlyclobber' operand of the
5928 current insn, just mark it as a place to
5929 reload from since we can't use it as the
5930 reload register itself. */
5932 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5933 if (reg_overlap_mentioned_for_reload_p
5934 (reg_last_reload_reg[regno],
5935 reload_earlyclobbers[i1]))
5936 break;
5938 if (i1 != n_earlyclobbers
5939 || ! (reload_reg_free_for_value_p
5940 (i, rld[r].opnum, rld[r].when_needed,
5941 rld[r].in, rld[r].out, r, 1))
5942 /* Don't use it if we'd clobber a pseudo reg. */
5943 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
5944 && rld[r].out
5945 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
5946 /* Don't clobber the frame pointer. */
5947 || (i == HARD_FRAME_POINTER_REGNUM
5948 && rld[r].out)
5949 /* Don't really use the inherited spill reg
5950 if we need it wider than we've got it. */
5951 || (GET_MODE_SIZE (reload_mode[r])
5952 > GET_MODE_SIZE (mode))
5953 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5956 /* If find_reloads chose reload_out as reload
5957 register, stay with it - that leaves the
5958 inherited register for subsequent reloads. */
5959 || (rld[r].out && rld[r].reg_rtx
5960 && rtx_equal_p (rld[r].out,
5961 rld[r].reg_rtx)))
5963 reload_override_in[r] = last_reg;
5964 reload_inheritance_insn[r]
5965 = reg_reloaded_insn[i];
5967 else
5969 int k;
5970 /* We can use this as a reload reg. */
5971 /* Mark the register as in use for this part of
5972 the insn. */
5973 mark_reload_reg_in_use (i,
5974 rld[r].opnum,
5975 rld[r].when_needed,
5976 reload_mode[r]);
5977 rld[r].reg_rtx = last_reg;
5978 reload_inherited[r] = 1;
5979 reload_inheritance_insn[r]
5980 = reg_reloaded_insn[i];
5981 reload_spill_index[r] = i;
5982 for (k = 0; k < nr; k++)
5983 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5984 i + k);
5991 /* Here's another way to see if the value is already lying around. */
5992 if (inheritance
5993 && rld[r].in != 0
5994 && ! reload_inherited[r]
5995 && rld[r].out == 0
5996 && (CONSTANT_P (rld[r].in)
5997 || GET_CODE (rld[r].in) == PLUS
5998 || GET_CODE (rld[r].in) == REG
5999 || GET_CODE (rld[r].in) == MEM)
6000 && (reload_nregs[r] == max_group_size
6001 || ! reg_classes_intersect_p (rld[r].class, group_class)))
6002 search_equiv = rld[r].in;
6003 /* If this is an output reload from a simple move insn, look
6004 if an equivalence for the input is available. */
6005 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
6007 rtx set = single_set (insn);
6009 if (set
6010 && rtx_equal_p (rld[r].out, SET_DEST (set))
6011 && CONSTANT_P (SET_SRC (set)))
6012 search_equiv = SET_SRC (set);
6015 if (search_equiv)
6017 register rtx equiv
6018 = find_equiv_reg (search_equiv, insn, rld[r].class,
6019 -1, NULL_PTR, 0, reload_mode[r]);
6020 int regno = 0;
6022 if (equiv != 0)
6024 if (GET_CODE (equiv) == REG)
6025 regno = REGNO (equiv);
6026 else if (GET_CODE (equiv) == SUBREG)
6028 /* This must be a SUBREG of a hard register.
6029 Make a new REG since this might be used in an
6030 address and not all machines support SUBREGs
6031 there. */
6032 regno = REGNO (SUBREG_REG (equiv)) + SUBREG_WORD (equiv);
6033 equiv = gen_rtx_REG (reload_mode[r], regno);
6035 else
6036 abort ();
6039 /* If we found a spill reg, reject it unless it is free
6040 and of the desired class. */
6041 if (equiv != 0
6042 && ((TEST_HARD_REG_BIT (reload_reg_used_at_all, regno)
6043 && ! reload_reg_free_for_value_p (regno, rld[r].opnum,
6044 rld[r].when_needed,
6045 rld[r].in,
6046 rld[r].out, r, 1))
6047 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
6048 regno)))
6049 equiv = 0;
6051 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, reload_mode[r]))
6052 equiv = 0;
6054 /* We found a register that contains the value we need.
6055 If this register is the same as an `earlyclobber' operand
6056 of the current insn, just mark it as a place to reload from
6057 since we can't use it as the reload register itself. */
6059 if (equiv != 0)
6060 for (i = 0; i < n_earlyclobbers; i++)
6061 if (reg_overlap_mentioned_for_reload_p (equiv,
6062 reload_earlyclobbers[i]))
6064 reload_override_in[r] = equiv;
6065 equiv = 0;
6066 break;
6069 /* If the equiv register we have found is explicitly clobbered
6070 in the current insn, it depends on the reload type if we
6071 can use it, use it for reload_override_in, or not at all.
6072 In particular, we then can't use EQUIV for a
6073 RELOAD_FOR_OUTPUT_ADDRESS reload. */
6075 if (equiv != 0 && regno_clobbered_p (regno, insn))
6077 switch (rld[r].when_needed)
6079 case RELOAD_FOR_OTHER_ADDRESS:
6080 case RELOAD_FOR_INPADDR_ADDRESS:
6081 case RELOAD_FOR_INPUT_ADDRESS:
6082 case RELOAD_FOR_OPADDR_ADDR:
6083 break;
6084 case RELOAD_OTHER:
6085 case RELOAD_FOR_INPUT:
6086 case RELOAD_FOR_OPERAND_ADDRESS:
6087 reload_override_in[r] = equiv;
6088 /* Fall through. */
6089 default:
6090 equiv = 0;
6091 break;
6095 /* If we found an equivalent reg, say no code need be generated
6096 to load it, and use it as our reload reg. */
6097 if (equiv != 0 && regno != HARD_FRAME_POINTER_REGNUM)
6099 int nr = HARD_REGNO_NREGS (regno, reload_mode[r]);
6100 int k;
6101 rld[r].reg_rtx = equiv;
6102 reload_inherited[r] = 1;
6104 /* If reg_reloaded_valid is not set for this register,
6105 there might be a stale spill_reg_store lying around.
6106 We must clear it, since otherwise emit_reload_insns
6107 might delete the store. */
6108 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
6109 spill_reg_store[regno] = NULL_RTX;
6110 /* If any of the hard registers in EQUIV are spill
6111 registers, mark them as in use for this insn. */
6112 for (k = 0; k < nr; k++)
6114 i = spill_reg_order[regno + k];
6115 if (i >= 0)
6117 mark_reload_reg_in_use (regno, rld[r].opnum,
6118 rld[r].when_needed,
6119 reload_mode[r]);
6120 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6121 regno + k);
6127 /* If we found a register to use already, or if this is an optional
6128 reload, we are done. */
6129 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
6130 continue;
6132 #if 0 /* No longer needed for correct operation. Might or might not
6133 give better code on the average. Want to experiment? */
6135 /* See if there is a later reload that has a class different from our
6136 class that intersects our class or that requires less register
6137 than our reload. If so, we must allocate a register to this
6138 reload now, since that reload might inherit a previous reload
6139 and take the only available register in our class. Don't do this
6140 for optional reloads since they will force all previous reloads
6141 to be allocated. Also don't do this for reloads that have been
6142 turned off. */
6144 for (i = j + 1; i < n_reloads; i++)
6146 int s = reload_order[i];
6148 if ((rld[s].in == 0 && rld[s].out == 0
6149 && ! rld[s].secondary_p)
6150 || rld[s].optional)
6151 continue;
6153 if ((rld[s].class != rld[r].class
6154 && reg_classes_intersect_p (rld[r].class,
6155 rld[s].class))
6156 || reload_nregs[s] < reload_nregs[r])
6157 break;
6160 if (i == n_reloads)
6161 continue;
6163 allocate_reload_reg (chain, r, j == n_reloads - 1, inheritance);
6164 #endif
6167 /* Now allocate reload registers for anything non-optional that
6168 didn't get one yet. */
6169 for (j = 0; j < n_reloads; j++)
6171 register int r = reload_order[j];
6173 /* Ignore reloads that got marked inoperative. */
6174 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
6175 continue;
6177 /* Skip reloads that already have a register allocated or are
6178 optional. */
6179 if (rld[r].reg_rtx != 0 || rld[r].optional)
6180 continue;
6182 if (! allocate_reload_reg (chain, r, j == n_reloads - 1, inheritance))
6183 break;
6186 /* If that loop got all the way, we have won. */
6187 if (j == n_reloads)
6188 break;
6190 /* Loop around and try without any inheritance. */
6191 /* First undo everything done by the failed attempt
6192 to allocate with inheritance. */
6193 for (i = 0; i < n_reloads; i++)
6194 rld[i].reg_rtx = save_reload_reg_rtx[i];
6195 bcopy ((char *) save_reload_inherited, (char *) reload_inherited,
6196 sizeof reload_inherited);
6197 bcopy ((char *) save_reload_inheritance_insn,
6198 (char *) reload_inheritance_insn,
6199 sizeof reload_inheritance_insn);
6200 bcopy ((char *) save_reload_override_in, (char *) reload_override_in,
6201 sizeof reload_override_in);
6202 bcopy ((char *) save_reload_spill_index, (char *) reload_spill_index,
6203 sizeof reload_spill_index);
6204 COPY_HARD_REG_SET (reload_reg_used, save_reload_reg_used);
6205 COPY_HARD_REG_SET (reload_reg_used_at_all, save_reload_reg_used_at_all);
6206 COPY_HARD_REG_SET (reload_reg_used_in_op_addr,
6207 save_reload_reg_used_in_op_addr);
6208 COPY_HARD_REG_SET (reload_reg_used_in_op_addr_reload,
6209 save_reload_reg_used_in_op_addr_reload);
6210 COPY_HARD_REG_SET (reload_reg_used_in_insn,
6211 save_reload_reg_used_in_insn);
6212 COPY_HARD_REG_SET (reload_reg_used_in_other_addr,
6213 save_reload_reg_used_in_other_addr);
6215 for (i = 0; i < reload_n_operands; i++)
6217 COPY_HARD_REG_SET (reload_reg_used_in_input[i],
6218 save_reload_reg_used_in_input[i]);
6219 COPY_HARD_REG_SET (reload_reg_used_in_output[i],
6220 save_reload_reg_used_in_output[i]);
6221 COPY_HARD_REG_SET (reload_reg_used_in_input_addr[i],
6222 save_reload_reg_used_in_input_addr[i]);
6223 COPY_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i],
6224 save_reload_reg_used_in_inpaddr_addr[i]);
6225 COPY_HARD_REG_SET (reload_reg_used_in_output_addr[i],
6226 save_reload_reg_used_in_output_addr[i]);
6227 COPY_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i],
6228 save_reload_reg_used_in_outaddr_addr[i]);
6232 /* If we thought we could inherit a reload, because it seemed that
6233 nothing else wanted the same reload register earlier in the insn,
6234 verify that assumption, now that all reloads have been assigned.
6235 Likewise for reloads where reload_override_in has been set. */
6237 /* If doing expensive optimizations, do one preliminary pass that doesn't
6238 cancel any inheritance, but removes reloads that have been needed only
6239 for reloads that we know can be inherited. */
6240 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
6242 for (j = 0; j < n_reloads; j++)
6244 register int r = reload_order[j];
6245 rtx check_reg;
6246 if (reload_inherited[r] && rld[r].reg_rtx)
6247 check_reg = rld[r].reg_rtx;
6248 else if (reload_override_in[r]
6249 && (GET_CODE (reload_override_in[r]) == REG
6250 || GET_CODE (reload_override_in[r]) == SUBREG))
6251 check_reg = reload_override_in[r];
6252 else
6253 continue;
6254 if (! reload_reg_free_for_value_p (true_regnum (check_reg),
6255 rld[r].opnum,
6256 rld[r].when_needed,
6257 rld[r].in,
6258 (reload_inherited[r]
6259 ? rld[r].out : const0_rtx),
6260 r, 1))
6262 if (pass)
6263 continue;
6264 reload_inherited[r] = 0;
6265 reload_override_in[r] = 0;
6267 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
6268 reload_override_in, then we do not need its related
6269 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
6270 likewise for other reload types.
6271 We handle this by removing a reload when its only replacement
6272 is mentioned in reload_in of the reload we are going to inherit.
6273 A special case are auto_inc expressions; even if the input is
6274 inherited, we still need the address for the output. We can
6275 recognize them because they have RELOAD_OUT set to RELOAD_IN.
6276 If we suceeded removing some reload and we are doing a preliminary
6277 pass just to remove such reloads, make another pass, since the
6278 removal of one reload might allow us to inherit another one. */
6279 else if (rld[r].in
6280 && rld[r].out != rld[r].in
6281 && remove_address_replacements (rld[r].in) && pass)
6282 pass = 2;
6286 /* Now that reload_override_in is known valid,
6287 actually override reload_in. */
6288 for (j = 0; j < n_reloads; j++)
6289 if (reload_override_in[j])
6290 rld[j].in = reload_override_in[j];
6292 /* If this reload won't be done because it has been cancelled or is
6293 optional and not inherited, clear reload_reg_rtx so other
6294 routines (such as subst_reloads) don't get confused. */
6295 for (j = 0; j < n_reloads; j++)
6296 if (rld[j].reg_rtx != 0
6297 && ((rld[j].optional && ! reload_inherited[j])
6298 || (rld[j].in == 0 && rld[j].out == 0
6299 && ! rld[j].secondary_p)))
6301 int regno = true_regnum (rld[j].reg_rtx);
6303 if (spill_reg_order[regno] >= 0)
6304 clear_reload_reg_in_use (regno, rld[j].opnum,
6305 rld[j].when_needed, reload_mode[j]);
6306 rld[j].reg_rtx = 0;
6309 /* Record which pseudos and which spill regs have output reloads. */
6310 for (j = 0; j < n_reloads; j++)
6312 register int r = reload_order[j];
6314 i = reload_spill_index[r];
6316 /* I is nonneg if this reload uses a register.
6317 If rld[r].reg_rtx is 0, this is an optional reload
6318 that we opted to ignore. */
6319 if (rld[r].out_reg != 0 && GET_CODE (rld[r].out_reg) == REG
6320 && rld[r].reg_rtx != 0)
6322 register int nregno = REGNO (rld[r].out_reg);
6323 int nr = 1;
6325 if (nregno < FIRST_PSEUDO_REGISTER)
6326 nr = HARD_REGNO_NREGS (nregno, reload_mode[r]);
6328 while (--nr >= 0)
6329 reg_has_output_reload[nregno + nr] = 1;
6331 if (i >= 0)
6333 nr = HARD_REGNO_NREGS (i, reload_mode[r]);
6334 while (--nr >= 0)
6335 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
6338 if (rld[r].when_needed != RELOAD_OTHER
6339 && rld[r].when_needed != RELOAD_FOR_OUTPUT
6340 && rld[r].when_needed != RELOAD_FOR_INSN)
6341 abort ();
6346 /* Deallocate the reload register for reload R. This is called from
6347 remove_address_replacements. */
6348 void
6349 deallocate_reload_reg (r)
6350 int r;
6352 int regno;
6354 if (! rld[r].reg_rtx)
6355 return;
6356 regno = true_regnum (rld[r].reg_rtx);
6357 rld[r].reg_rtx = 0;
6358 if (spill_reg_order[regno] >= 0)
6359 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
6360 reload_mode[r]);
6361 reload_spill_index[r] = -1;
6364 /* If SMALL_REGISTER_CLASSES is non-zero, we may not have merged two
6365 reloads of the same item for fear that we might not have enough reload
6366 registers. However, normally they will get the same reload register
6367 and hence actually need not be loaded twice.
6369 Here we check for the most common case of this phenomenon: when we have
6370 a number of reloads for the same object, each of which were allocated
6371 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6372 reload, and is not modified in the insn itself. If we find such,
6373 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6374 This will not increase the number of spill registers needed and will
6375 prevent redundant code. */
6377 static void
6378 merge_assigned_reloads (insn)
6379 rtx insn;
6381 int i, j;
6383 /* Scan all the reloads looking for ones that only load values and
6384 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6385 assigned and not modified by INSN. */
6387 for (i = 0; i < n_reloads; i++)
6389 int conflicting_input = 0;
6390 int max_input_address_opnum = -1;
6391 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6393 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6394 || rld[i].out != 0 || rld[i].reg_rtx == 0
6395 || reg_set_p (rld[i].reg_rtx, insn))
6396 continue;
6398 /* Look at all other reloads. Ensure that the only use of this
6399 reload_reg_rtx is in a reload that just loads the same value
6400 as we do. Note that any secondary reloads must be of the identical
6401 class since the values, modes, and result registers are the
6402 same, so we need not do anything with any secondary reloads. */
6404 for (j = 0; j < n_reloads; j++)
6406 if (i == j || rld[j].reg_rtx == 0
6407 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6408 rld[i].reg_rtx))
6409 continue;
6411 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6412 && rld[j].opnum > max_input_address_opnum)
6413 max_input_address_opnum = rld[j].opnum;
6415 /* If the reload regs aren't exactly the same (e.g, different modes)
6416 or if the values are different, we can't merge this reload.
6417 But if it is an input reload, we might still merge
6418 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6420 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6421 || rld[j].out != 0 || rld[j].in == 0
6422 || ! rtx_equal_p (rld[i].in, rld[j].in))
6424 if (rld[j].when_needed != RELOAD_FOR_INPUT
6425 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6426 || rld[i].opnum > rld[j].opnum)
6427 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6428 break;
6429 conflicting_input = 1;
6430 if (min_conflicting_input_opnum > rld[j].opnum)
6431 min_conflicting_input_opnum = rld[j].opnum;
6435 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6436 we, in fact, found any matching reloads. */
6438 if (j == n_reloads
6439 && max_input_address_opnum <= min_conflicting_input_opnum)
6441 for (j = 0; j < n_reloads; j++)
6442 if (i != j && rld[j].reg_rtx != 0
6443 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6444 && (! conflicting_input
6445 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6446 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6448 rld[i].when_needed = RELOAD_OTHER;
6449 rld[j].in = 0;
6450 reload_spill_index[j] = -1;
6451 transfer_replacements (i, j);
6454 /* If this is now RELOAD_OTHER, look for any reloads that load
6455 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6456 if they were for inputs, RELOAD_OTHER for outputs. Note that
6457 this test is equivalent to looking for reloads for this operand
6458 number. */
6460 if (rld[i].when_needed == RELOAD_OTHER)
6461 for (j = 0; j < n_reloads; j++)
6462 if (rld[j].in != 0
6463 && rld[i].when_needed != RELOAD_OTHER
6464 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6465 rld[i].in))
6466 rld[j].when_needed
6467 = ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
6468 || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6469 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6475 /* Output insns to reload values in and out of the chosen reload regs. */
6477 static void
6478 emit_reload_insns (chain)
6479 struct insn_chain *chain;
6481 rtx insn = chain->insn;
6483 register int j;
6484 rtx input_reload_insns[MAX_RECOG_OPERANDS];
6485 rtx other_input_address_reload_insns = 0;
6486 rtx other_input_reload_insns = 0;
6487 rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6488 rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6489 rtx output_reload_insns[MAX_RECOG_OPERANDS];
6490 rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6491 rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6492 rtx operand_reload_insns = 0;
6493 rtx other_operand_reload_insns = 0;
6494 rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6495 rtx following_insn = NEXT_INSN (insn);
6496 rtx before_insn = PREV_INSN (insn);
6497 int special;
6498 /* Values to be put in spill_reg_store are put here first. */
6499 rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6500 HARD_REG_SET reg_reloaded_died;
6502 CLEAR_HARD_REG_SET (reg_reloaded_died);
6504 for (j = 0; j < reload_n_operands; j++)
6505 input_reload_insns[j] = input_address_reload_insns[j]
6506 = inpaddr_address_reload_insns[j]
6507 = output_reload_insns[j] = output_address_reload_insns[j]
6508 = outaddr_address_reload_insns[j]
6509 = other_output_reload_insns[j] = 0;
6511 /* Now output the instructions to copy the data into and out of the
6512 reload registers. Do these in the order that the reloads were reported,
6513 since reloads of base and index registers precede reloads of operands
6514 and the operands may need the base and index registers reloaded. */
6516 for (j = 0; j < n_reloads; j++)
6518 register rtx old;
6519 rtx oldequiv_reg = 0;
6520 rtx this_reload_insn = 0;
6521 int expect_occurrences = 1;
6523 if (rld[j].reg_rtx
6524 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
6525 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
6527 old = (rld[j].in && GET_CODE (rld[j].in) == MEM
6528 ? rld[j].in_reg : rld[j].in);
6530 if (old != 0
6531 /* AUTO_INC reloads need to be handled even if inherited. We got an
6532 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
6533 && (! reload_inherited[j] || (rld[j].out && ! rld[j].out_reg))
6534 && ! rtx_equal_p (rld[j].reg_rtx, old)
6535 && rld[j].reg_rtx != 0)
6537 register rtx reloadreg = rld[j].reg_rtx;
6538 rtx oldequiv = 0;
6539 enum machine_mode mode;
6540 rtx *where;
6542 /* Determine the mode to reload in.
6543 This is very tricky because we have three to choose from.
6544 There is the mode the insn operand wants (rld[J].inmode).
6545 There is the mode of the reload register RELOADREG.
6546 There is the intrinsic mode of the operand, which we could find
6547 by stripping some SUBREGs.
6548 It turns out that RELOADREG's mode is irrelevant:
6549 we can change that arbitrarily.
6551 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6552 then the reload reg may not support QImode moves, so use SImode.
6553 If foo is in memory due to spilling a pseudo reg, this is safe,
6554 because the QImode value is in the least significant part of a
6555 slot big enough for a SImode. If foo is some other sort of
6556 memory reference, then it is impossible to reload this case,
6557 so previous passes had better make sure this never happens.
6559 Then consider a one-word union which has SImode and one of its
6560 members is a float, being fetched as (SUBREG:SF union:SI).
6561 We must fetch that as SFmode because we could be loading into
6562 a float-only register. In this case OLD's mode is correct.
6564 Consider an immediate integer: it has VOIDmode. Here we need
6565 to get a mode from something else.
6567 In some cases, there is a fourth mode, the operand's
6568 containing mode. If the insn specifies a containing mode for
6569 this operand, it overrides all others.
6571 I am not sure whether the algorithm here is always right,
6572 but it does the right things in those cases. */
6574 mode = GET_MODE (old);
6575 if (mode == VOIDmode)
6576 mode = rld[j].inmode;
6578 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6579 /* If we need a secondary register for this operation, see if
6580 the value is already in a register in that class. Don't
6581 do this if the secondary register will be used as a scratch
6582 register. */
6584 if (rld[j].secondary_in_reload >= 0
6585 && rld[j].secondary_in_icode == CODE_FOR_nothing
6586 && optimize)
6587 oldequiv
6588 = find_equiv_reg (old, insn,
6589 rld[rld[j].secondary_in_reload].class,
6590 -1, NULL_PTR, 0, mode);
6591 #endif
6593 /* If reloading from memory, see if there is a register
6594 that already holds the same value. If so, reload from there.
6595 We can pass 0 as the reload_reg_p argument because
6596 any other reload has either already been emitted,
6597 in which case find_equiv_reg will see the reload-insn,
6598 or has yet to be emitted, in which case it doesn't matter
6599 because we will use this equiv reg right away. */
6601 if (oldequiv == 0 && optimize
6602 && (GET_CODE (old) == MEM
6603 || (GET_CODE (old) == REG
6604 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6605 && reg_renumber[REGNO (old)] < 0)))
6606 oldequiv = find_equiv_reg (old, insn, ALL_REGS,
6607 -1, NULL_PTR, 0, mode);
6609 if (oldequiv)
6611 int regno = true_regnum (oldequiv);
6613 /* Don't use OLDEQUIV if any other reload changes it at an
6614 earlier stage of this insn or at this stage. */
6615 if (! reload_reg_free_for_value_p (regno, rld[j].opnum,
6616 rld[j].when_needed,
6617 rld[j].in, const0_rtx, j,
6619 oldequiv = 0;
6621 /* If it is no cheaper to copy from OLDEQUIV into the
6622 reload register than it would be to move from memory,
6623 don't use it. Likewise, if we need a secondary register
6624 or memory. */
6626 if (oldequiv != 0
6627 && ((REGNO_REG_CLASS (regno) != rld[j].class
6628 && (REGISTER_MOVE_COST (REGNO_REG_CLASS (regno),
6629 rld[j].class)
6630 >= MEMORY_MOVE_COST (mode, rld[j].class, 1)))
6631 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6632 || (SECONDARY_INPUT_RELOAD_CLASS (rld[j].class,
6633 mode, oldequiv)
6634 != NO_REGS)
6635 #endif
6636 #ifdef SECONDARY_MEMORY_NEEDED
6637 || SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (regno),
6638 rld[j].class,
6639 mode)
6640 #endif
6642 oldequiv = 0;
6645 /* delete_output_reload is only invoked properly if old contains
6646 the original pseudo register. Since this is replaced with a
6647 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6648 find the pseudo in RELOAD_IN_REG. */
6649 if (oldequiv == 0
6650 && reload_override_in[j]
6651 && GET_CODE (rld[j].in_reg) == REG)
6653 oldequiv = old;
6654 old = rld[j].in_reg;
6656 if (oldequiv == 0)
6657 oldequiv = old;
6658 else if (GET_CODE (oldequiv) == REG)
6659 oldequiv_reg = oldequiv;
6660 else if (GET_CODE (oldequiv) == SUBREG)
6661 oldequiv_reg = SUBREG_REG (oldequiv);
6663 /* If we are reloading from a register that was recently stored in
6664 with an output-reload, see if we can prove there was
6665 actually no need to store the old value in it. */
6667 if (optimize && GET_CODE (oldequiv) == REG
6668 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6669 && spill_reg_store[REGNO (oldequiv)]
6670 && GET_CODE (old) == REG
6671 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6672 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6673 rld[j].out_reg)))
6674 delete_output_reload (insn, j, REGNO (oldequiv));
6676 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6677 then load RELOADREG from OLDEQUIV. Note that we cannot use
6678 gen_lowpart_common since it can do the wrong thing when
6679 RELOADREG has a multi-word mode. Note that RELOADREG
6680 must always be a REG here. */
6682 if (GET_MODE (reloadreg) != mode)
6683 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6684 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6685 oldequiv = SUBREG_REG (oldequiv);
6686 if (GET_MODE (oldequiv) != VOIDmode
6687 && mode != GET_MODE (oldequiv))
6688 oldequiv = gen_rtx_SUBREG (mode, oldequiv, 0);
6690 /* Switch to the right place to emit the reload insns. */
6691 switch (rld[j].when_needed)
6693 case RELOAD_OTHER:
6694 where = &other_input_reload_insns;
6695 break;
6696 case RELOAD_FOR_INPUT:
6697 where = &input_reload_insns[rld[j].opnum];
6698 break;
6699 case RELOAD_FOR_INPUT_ADDRESS:
6700 where = &input_address_reload_insns[rld[j].opnum];
6701 break;
6702 case RELOAD_FOR_INPADDR_ADDRESS:
6703 where = &inpaddr_address_reload_insns[rld[j].opnum];
6704 break;
6705 case RELOAD_FOR_OUTPUT_ADDRESS:
6706 where = &output_address_reload_insns[rld[j].opnum];
6707 break;
6708 case RELOAD_FOR_OUTADDR_ADDRESS:
6709 where = &outaddr_address_reload_insns[rld[j].opnum];
6710 break;
6711 case RELOAD_FOR_OPERAND_ADDRESS:
6712 where = &operand_reload_insns;
6713 break;
6714 case RELOAD_FOR_OPADDR_ADDR:
6715 where = &other_operand_reload_insns;
6716 break;
6717 case RELOAD_FOR_OTHER_ADDRESS:
6718 where = &other_input_address_reload_insns;
6719 break;
6720 default:
6721 abort ();
6724 push_to_sequence (*where);
6725 special = 0;
6727 /* Auto-increment addresses must be reloaded in a special way. */
6728 if (rld[j].out && ! rld[j].out_reg)
6730 /* We are not going to bother supporting the case where a
6731 incremented register can't be copied directly from
6732 OLDEQUIV since this seems highly unlikely. */
6733 if (rld[j].secondary_in_reload >= 0)
6734 abort ();
6736 if (reload_inherited[j])
6737 oldequiv = reloadreg;
6739 old = XEXP (rld[j].in_reg, 0);
6741 if (optimize && GET_CODE (oldequiv) == REG
6742 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6743 && spill_reg_store[REGNO (oldequiv)]
6744 && GET_CODE (old) == REG
6745 && (dead_or_set_p (insn,
6746 spill_reg_stored_to[REGNO (oldequiv)])
6747 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6748 old)))
6749 delete_output_reload (insn, j, REGNO (oldequiv));
6751 /* Prevent normal processing of this reload. */
6752 special = 1;
6753 /* Output a special code sequence for this case. */
6754 new_spill_reg_store[REGNO (reloadreg)]
6755 = inc_for_reload (reloadreg, oldequiv, rld[j].out,
6756 rld[j].inc);
6759 /* If we are reloading a pseudo-register that was set by the previous
6760 insn, see if we can get rid of that pseudo-register entirely
6761 by redirecting the previous insn into our reload register. */
6763 else if (optimize && GET_CODE (old) == REG
6764 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6765 && dead_or_set_p (insn, old)
6766 /* This is unsafe if some other reload
6767 uses the same reg first. */
6768 && reload_reg_free_for_value_p (REGNO (reloadreg),
6769 rld[j].opnum,
6770 rld[j].when_needed,
6771 old, rld[j].out,
6772 j, 0))
6774 rtx temp = PREV_INSN (insn);
6775 while (temp && GET_CODE (temp) == NOTE)
6776 temp = PREV_INSN (temp);
6777 if (temp
6778 && GET_CODE (temp) == INSN
6779 && GET_CODE (PATTERN (temp)) == SET
6780 && SET_DEST (PATTERN (temp)) == old
6781 /* Make sure we can access insn_operand_constraint. */
6782 && asm_noperands (PATTERN (temp)) < 0
6783 /* This is unsafe if prev insn rejects our reload reg. */
6784 && constraint_accepts_reg_p (insn_data[recog_memoized (temp)].operand[0].constraint,
6785 reloadreg)
6786 /* This is unsafe if operand occurs more than once in current
6787 insn. Perhaps some occurrences aren't reloaded. */
6788 && count_occurrences (PATTERN (insn), old) == 1
6789 /* Don't risk splitting a matching pair of operands. */
6790 && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
6792 /* Store into the reload register instead of the pseudo. */
6793 SET_DEST (PATTERN (temp)) = reloadreg;
6795 /* If the previous insn is an output reload, the source is
6796 a reload register, and its spill_reg_store entry will
6797 contain the previous destination. This is now
6798 invalid. */
6799 if (GET_CODE (SET_SRC (PATTERN (temp))) == REG
6800 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6802 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6803 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6806 /* If these are the only uses of the pseudo reg,
6807 pretend for GDB it lives in the reload reg we used. */
6808 if (REG_N_DEATHS (REGNO (old)) == 1
6809 && REG_N_SETS (REGNO (old)) == 1)
6811 reg_renumber[REGNO (old)] = REGNO (rld[j].reg_rtx);
6812 alter_reg (REGNO (old), -1);
6814 special = 1;
6818 /* We can't do that, so output an insn to load RELOADREG. */
6820 if (! special)
6822 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6823 rtx second_reload_reg = 0;
6824 enum insn_code icode;
6826 /* If we have a secondary reload, pick up the secondary register
6827 and icode, if any. If OLDEQUIV and OLD are different or
6828 if this is an in-out reload, recompute whether or not we
6829 still need a secondary register and what the icode should
6830 be. If we still need a secondary register and the class or
6831 icode is different, go back to reloading from OLD if using
6832 OLDEQUIV means that we got the wrong type of register. We
6833 cannot have different class or icode due to an in-out reload
6834 because we don't make such reloads when both the input and
6835 output need secondary reload registers. */
6837 if (rld[j].secondary_in_reload >= 0)
6839 int secondary_reload = rld[j].secondary_in_reload;
6840 rtx real_oldequiv = oldequiv;
6841 rtx real_old = old;
6842 rtx tmp;
6844 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6845 and similarly for OLD.
6846 See comments in get_secondary_reload in reload.c. */
6847 /* If it is a pseudo that cannot be replaced with its
6848 equivalent MEM, we must fall back to reload_in, which
6849 will have all the necessary substitutions registered.
6850 Likewise for a pseudo that can't be replaced with its
6851 equivalent constant.
6853 Take extra care for subregs of such pseudos. Note that
6854 we cannot use reg_equiv_mem in this case because it is
6855 not in the right mode. */
6857 tmp = oldequiv;
6858 if (GET_CODE (tmp) == SUBREG)
6859 tmp = SUBREG_REG (tmp);
6860 if (GET_CODE (tmp) == REG
6861 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6862 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6863 || reg_equiv_constant[REGNO (tmp)] != 0))
6865 if (! reg_equiv_mem[REGNO (tmp)]
6866 || num_not_at_initial_offset
6867 || GET_CODE (oldequiv) == SUBREG)
6868 real_oldequiv = rld[j].in;
6869 else
6870 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6873 tmp = old;
6874 if (GET_CODE (tmp) == SUBREG)
6875 tmp = SUBREG_REG (tmp);
6876 if (GET_CODE (tmp) == REG
6877 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6878 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6879 || reg_equiv_constant[REGNO (tmp)] != 0))
6881 if (! reg_equiv_mem[REGNO (tmp)]
6882 || num_not_at_initial_offset
6883 || GET_CODE (old) == SUBREG)
6884 real_old = rld[j].in;
6885 else
6886 real_old = reg_equiv_mem[REGNO (tmp)];
6889 second_reload_reg = rld[secondary_reload].reg_rtx;
6890 icode = rld[j].secondary_in_icode;
6892 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6893 || (rld[j].in != 0 && rld[j].out != 0))
6895 enum reg_class new_class
6896 = SECONDARY_INPUT_RELOAD_CLASS (rld[j].class,
6897 mode, real_oldequiv);
6899 if (new_class == NO_REGS)
6900 second_reload_reg = 0;
6901 else
6903 enum insn_code new_icode;
6904 enum machine_mode new_mode;
6906 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
6907 REGNO (second_reload_reg)))
6908 oldequiv = old, real_oldequiv = real_old;
6909 else
6911 new_icode = reload_in_optab[(int) mode];
6912 if (new_icode != CODE_FOR_nothing
6913 && ((insn_data[(int) new_icode].operand[0].predicate
6914 && ! ((*insn_data[(int) new_icode].operand[0].predicate)
6915 (reloadreg, mode)))
6916 || (insn_data[(int) new_icode].operand[1].predicate
6917 && ! ((*insn_data[(int) new_icode].operand[1].predicate)
6918 (real_oldequiv, mode)))))
6919 new_icode = CODE_FOR_nothing;
6921 if (new_icode == CODE_FOR_nothing)
6922 new_mode = mode;
6923 else
6924 new_mode = insn_data[(int) new_icode].operand[2].mode;
6926 if (GET_MODE (second_reload_reg) != new_mode)
6928 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6929 new_mode))
6930 oldequiv = old, real_oldequiv = real_old;
6931 else
6932 second_reload_reg
6933 = gen_rtx_REG (new_mode,
6934 REGNO (second_reload_reg));
6940 /* If we still need a secondary reload register, check
6941 to see if it is being used as a scratch or intermediate
6942 register and generate code appropriately. If we need
6943 a scratch register, use REAL_OLDEQUIV since the form of
6944 the insn may depend on the actual address if it is
6945 a MEM. */
6947 if (second_reload_reg)
6949 if (icode != CODE_FOR_nothing)
6951 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6952 second_reload_reg));
6953 special = 1;
6955 else
6957 /* See if we need a scratch register to load the
6958 intermediate register (a tertiary reload). */
6959 enum insn_code tertiary_icode
6960 = rld[secondary_reload].secondary_in_icode;
6962 if (tertiary_icode != CODE_FOR_nothing)
6964 rtx third_reload_reg
6965 = rld[rld[secondary_reload].secondary_in_reload].reg_rtx;
6967 emit_insn ((GEN_FCN (tertiary_icode)
6968 (second_reload_reg, real_oldequiv,
6969 third_reload_reg)));
6971 else
6972 gen_reload (second_reload_reg, real_oldequiv,
6973 rld[j].opnum,
6974 rld[j].when_needed);
6976 oldequiv = second_reload_reg;
6980 #endif
6982 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
6984 rtx real_oldequiv = oldequiv;
6986 if ((GET_CODE (oldequiv) == REG
6987 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
6988 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
6989 || reg_equiv_constant[REGNO (oldequiv)] != 0))
6990 || (GET_CODE (oldequiv) == SUBREG
6991 && GET_CODE (SUBREG_REG (oldequiv)) == REG
6992 && (REGNO (SUBREG_REG (oldequiv))
6993 >= FIRST_PSEUDO_REGISTER)
6994 && ((reg_equiv_memory_loc
6995 [REGNO (SUBREG_REG (oldequiv))] != 0)
6996 || (reg_equiv_constant
6997 [REGNO (SUBREG_REG (oldequiv))] != 0))))
6998 real_oldequiv = rld[j].in;
6999 gen_reload (reloadreg, real_oldequiv, rld[j].opnum,
7000 rld[j].when_needed);
7005 this_reload_insn = get_last_insn ();
7006 /* End this sequence. */
7007 *where = get_insns ();
7008 end_sequence ();
7010 /* Update reload_override_in so that delete_address_reloads_1
7011 can see the actual register usage. */
7012 if (oldequiv_reg)
7013 reload_override_in[j] = oldequiv;
7016 /* When inheriting a wider reload, we have a MEM in rld[j].in,
7017 e.g. inheriting a SImode output reload for
7018 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
7019 if (optimize && reload_inherited[j] && rld[j].in
7020 && GET_CODE (rld[j].in) == MEM
7021 && GET_CODE (rld[j].in_reg) == MEM
7022 && reload_spill_index[j] >= 0
7023 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
7025 expect_occurrences
7026 = count_occurrences (PATTERN (insn), rld[j].in) == 1 ? 0 : -1;
7027 rld[j].in
7028 = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
7031 /* If we are reloading a register that was recently stored in with an
7032 output-reload, see if we can prove there was
7033 actually no need to store the old value in it. */
7035 if (optimize
7036 && (reload_inherited[j] || reload_override_in[j])
7037 && rld[j].reg_rtx
7038 && GET_CODE (rld[j].reg_rtx) == REG
7039 && spill_reg_store[REGNO (rld[j].reg_rtx)] != 0
7040 #if 0
7041 /* There doesn't seem to be any reason to restrict this to pseudos
7042 and doing so loses in the case where we are copying from a
7043 register of the wrong class. */
7044 && (REGNO (spill_reg_stored_to[REGNO (rld[j].reg_rtx)])
7045 >= FIRST_PSEUDO_REGISTER)
7046 #endif
7047 /* The insn might have already some references to stackslots
7048 replaced by MEMs, while reload_out_reg still names the
7049 original pseudo. */
7050 && (dead_or_set_p (insn,
7051 spill_reg_stored_to[REGNO (rld[j].reg_rtx)])
7052 || rtx_equal_p (spill_reg_stored_to[REGNO (rld[j].reg_rtx)],
7053 rld[j].out_reg)))
7054 delete_output_reload (insn, j, REGNO (rld[j].reg_rtx));
7056 /* Input-reloading is done. Now do output-reloading,
7057 storing the value from the reload-register after the main insn
7058 if rld[j].out is nonzero.
7060 ??? At some point we need to support handling output reloads of
7061 JUMP_INSNs or insns that set cc0. */
7063 /* If this is an output reload that stores something that is
7064 not loaded in this same reload, see if we can eliminate a previous
7065 store. */
7067 rtx pseudo = rld[j].out_reg;
7069 if (pseudo
7070 && GET_CODE (pseudo) == REG
7071 && ! rtx_equal_p (rld[j].in_reg, pseudo)
7072 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
7073 && reg_last_reload_reg[REGNO (pseudo)])
7075 int pseudo_no = REGNO (pseudo);
7076 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
7078 /* We don't need to test full validity of last_regno for
7079 inherit here; we only want to know if the store actually
7080 matches the pseudo. */
7081 if (reg_reloaded_contents[last_regno] == pseudo_no
7082 && spill_reg_store[last_regno]
7083 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
7084 delete_output_reload (insn, j, last_regno);
7088 old = rld[j].out_reg;
7089 if (old != 0
7090 && rld[j].reg_rtx != old
7091 && rld[j].reg_rtx != 0)
7093 register rtx reloadreg = rld[j].reg_rtx;
7094 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
7095 register rtx second_reloadreg = 0;
7096 #endif
7097 rtx note, p;
7098 enum machine_mode mode;
7099 int special = 0;
7101 /* An output operand that dies right away does need a reload,
7102 but need not be copied from it. Show the new location in the
7103 REG_UNUSED note. */
7104 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
7105 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
7107 XEXP (note, 0) = rld[j].reg_rtx;
7108 continue;
7110 /* Likewise for a SUBREG of an operand that dies. */
7111 else if (GET_CODE (old) == SUBREG
7112 && GET_CODE (SUBREG_REG (old)) == REG
7113 && 0 != (note = find_reg_note (insn, REG_UNUSED,
7114 SUBREG_REG (old))))
7116 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
7117 rld[j].reg_rtx);
7118 continue;
7120 else if (GET_CODE (old) == SCRATCH)
7121 /* If we aren't optimizing, there won't be a REG_UNUSED note,
7122 but we don't want to make an output reload. */
7123 continue;
7125 #if 0
7126 /* Strip off of OLD any size-increasing SUBREGs such as
7127 (SUBREG:SI foo:QI 0). */
7129 while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
7130 && (GET_MODE_SIZE (GET_MODE (old))
7131 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
7132 old = SUBREG_REG (old);
7133 #endif
7135 /* If is a JUMP_INSN, we can't support output reloads yet. */
7136 if (GET_CODE (insn) == JUMP_INSN)
7137 abort ();
7139 if (rld[j].when_needed == RELOAD_OTHER)
7140 start_sequence ();
7141 else
7142 push_to_sequence (output_reload_insns[rld[j].opnum]);
7144 old = rld[j].out;
7146 /* Determine the mode to reload in.
7147 See comments above (for input reloading). */
7149 mode = GET_MODE (old);
7150 if (mode == VOIDmode)
7152 /* VOIDmode should never happen for an output. */
7153 if (asm_noperands (PATTERN (insn)) < 0)
7154 /* It's the compiler's fault. */
7155 fatal_insn ("VOIDmode on an output", insn);
7156 error_for_asm (insn, "output operand is constant in `asm'");
7157 /* Prevent crash--use something we know is valid. */
7158 mode = word_mode;
7159 old = gen_rtx_REG (mode, REGNO (reloadreg));
7162 if (GET_MODE (reloadreg) != mode)
7163 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
7165 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
7167 /* If we need two reload regs, set RELOADREG to the intermediate
7168 one, since it will be stored into OLD. We might need a secondary
7169 register only for an input reload, so check again here. */
7171 if (rld[j].secondary_out_reload >= 0)
7173 rtx real_old = old;
7175 if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
7176 && reg_equiv_mem[REGNO (old)] != 0)
7177 real_old = reg_equiv_mem[REGNO (old)];
7179 if((SECONDARY_OUTPUT_RELOAD_CLASS (rld[j].class,
7180 mode, real_old)
7181 != NO_REGS))
7183 second_reloadreg = reloadreg;
7184 reloadreg = rld[rld[j].secondary_out_reload].reg_rtx;
7186 /* See if RELOADREG is to be used as a scratch register
7187 or as an intermediate register. */
7188 if (rld[j].secondary_out_icode != CODE_FOR_nothing)
7190 emit_insn ((GEN_FCN (rld[j].secondary_out_icode)
7191 (real_old, second_reloadreg, reloadreg)));
7192 special = 1;
7194 else
7196 /* See if we need both a scratch and intermediate reload
7197 register. */
7199 int secondary_reload = rld[j].secondary_out_reload;
7200 enum insn_code tertiary_icode
7201 = rld[secondary_reload].secondary_out_icode;
7203 if (GET_MODE (reloadreg) != mode)
7204 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
7206 if (tertiary_icode != CODE_FOR_nothing)
7208 rtx third_reloadreg
7209 = rld[rld[secondary_reload].secondary_out_reload].reg_rtx;
7210 rtx tem;
7212 /* Copy primary reload reg to secondary reload reg.
7213 (Note that these have been swapped above, then
7214 secondary reload reg to OLD using our insn. */
7216 /* If REAL_OLD is a paradoxical SUBREG, remove it
7217 and try to put the opposite SUBREG on
7218 RELOADREG. */
7219 if (GET_CODE (real_old) == SUBREG
7220 && (GET_MODE_SIZE (GET_MODE (real_old))
7221 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
7222 && 0 != (tem = gen_lowpart_common
7223 (GET_MODE (SUBREG_REG (real_old)),
7224 reloadreg)))
7225 real_old = SUBREG_REG (real_old), reloadreg = tem;
7227 gen_reload (reloadreg, second_reloadreg,
7228 rld[j].opnum, rld[j].when_needed);
7229 emit_insn ((GEN_FCN (tertiary_icode)
7230 (real_old, reloadreg, third_reloadreg)));
7231 special = 1;
7234 else
7235 /* Copy between the reload regs here and then to
7236 OUT later. */
7238 gen_reload (reloadreg, second_reloadreg,
7239 rld[j].opnum, rld[j].when_needed);
7243 #endif
7245 /* Output the last reload insn. */
7246 if (! special)
7248 rtx set;
7250 /* Don't output the last reload if OLD is not the dest of
7251 INSN and is in the src and is clobbered by INSN. */
7252 if (! flag_expensive_optimizations
7253 || GET_CODE (old) != REG
7254 || !(set = single_set (insn))
7255 || rtx_equal_p (old, SET_DEST (set))
7256 || !reg_mentioned_p (old, SET_SRC (set))
7257 || !regno_clobbered_p (REGNO (old), insn))
7258 gen_reload (old, reloadreg, rld[j].opnum,
7259 rld[j].when_needed);
7262 /* Look at all insns we emitted, just to be safe. */
7263 for (p = get_insns (); p; p = NEXT_INSN (p))
7264 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
7266 rtx pat = PATTERN (p);
7268 /* If this output reload doesn't come from a spill reg,
7269 clear any memory of reloaded copies of the pseudo reg.
7270 If this output reload comes from a spill reg,
7271 reg_has_output_reload will make this do nothing. */
7272 note_stores (pat, forget_old_reloads_1);
7274 if (reg_mentioned_p (rld[j].reg_rtx, pat))
7276 rtx set = single_set (insn);
7277 if (reload_spill_index[j] < 0
7278 && set
7279 && SET_SRC (set) == rld[j].reg_rtx)
7281 int src = REGNO (SET_SRC (set));
7283 reload_spill_index[j] = src;
7284 SET_HARD_REG_BIT (reg_is_output_reload, src);
7285 if (find_regno_note (insn, REG_DEAD, src))
7286 SET_HARD_REG_BIT (reg_reloaded_died, src);
7288 if (REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
7290 int s = rld[j].secondary_out_reload;
7291 set = single_set (p);
7292 /* If this reload copies only to the secondary reload
7293 register, the secondary reload does the actual
7294 store. */
7295 if (s >= 0 && set == NULL_RTX)
7296 ; /* We can't tell what function the secondary reload
7297 has and where the actual store to the pseudo is
7298 made; leave new_spill_reg_store alone. */
7299 else if (s >= 0
7300 && SET_SRC (set) == rld[j].reg_rtx
7301 && SET_DEST (set) == rld[s].reg_rtx)
7303 /* Usually the next instruction will be the
7304 secondary reload insn; if we can confirm
7305 that it is, setting new_spill_reg_store to
7306 that insn will allow an extra optimization. */
7307 rtx s_reg = rld[s].reg_rtx;
7308 rtx next = NEXT_INSN (p);
7309 rld[s].out = rld[j].out;
7310 rld[s].out_reg = rld[j].out_reg;
7311 set = single_set (next);
7312 if (set && SET_SRC (set) == s_reg
7313 && ! new_spill_reg_store[REGNO (s_reg)])
7315 SET_HARD_REG_BIT (reg_is_output_reload,
7316 REGNO (s_reg));
7317 new_spill_reg_store[REGNO (s_reg)] = next;
7320 else
7321 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = p;
7326 if (rld[j].when_needed == RELOAD_OTHER)
7328 emit_insns (other_output_reload_insns[rld[j].opnum]);
7329 other_output_reload_insns[rld[j].opnum] = get_insns ();
7331 else
7332 output_reload_insns[rld[j].opnum] = get_insns ();
7334 end_sequence ();
7338 /* Now write all the insns we made for reloads in the order expected by
7339 the allocation functions. Prior to the insn being reloaded, we write
7340 the following reloads:
7342 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7344 RELOAD_OTHER reloads.
7346 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7347 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7348 RELOAD_FOR_INPUT reload for the operand.
7350 RELOAD_FOR_OPADDR_ADDRS reloads.
7352 RELOAD_FOR_OPERAND_ADDRESS reloads.
7354 After the insn being reloaded, we write the following:
7356 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7357 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7358 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7359 reloads for the operand. The RELOAD_OTHER output reloads are
7360 output in descending order by reload number. */
7362 emit_insns_before (other_input_address_reload_insns, insn);
7363 emit_insns_before (other_input_reload_insns, insn);
7365 for (j = 0; j < reload_n_operands; j++)
7367 emit_insns_before (inpaddr_address_reload_insns[j], insn);
7368 emit_insns_before (input_address_reload_insns[j], insn);
7369 emit_insns_before (input_reload_insns[j], insn);
7372 emit_insns_before (other_operand_reload_insns, insn);
7373 emit_insns_before (operand_reload_insns, insn);
7375 for (j = 0; j < reload_n_operands; j++)
7377 emit_insns_before (outaddr_address_reload_insns[j], following_insn);
7378 emit_insns_before (output_address_reload_insns[j], following_insn);
7379 emit_insns_before (output_reload_insns[j], following_insn);
7380 emit_insns_before (other_output_reload_insns[j], following_insn);
7383 /* Keep basic block info up to date. */
7384 if (n_basic_blocks)
7386 if (BLOCK_HEAD (chain->block) == insn)
7387 BLOCK_HEAD (chain->block) = NEXT_INSN (before_insn);
7388 if (BLOCK_END (chain->block) == insn)
7389 BLOCK_END (chain->block) = PREV_INSN (following_insn);
7392 /* For all the spill regs newly reloaded in this instruction,
7393 record what they were reloaded from, so subsequent instructions
7394 can inherit the reloads.
7396 Update spill_reg_store for the reloads of this insn.
7397 Copy the elements that were updated in the loop above. */
7399 for (j = 0; j < n_reloads; j++)
7401 register int r = reload_order[j];
7402 register int i = reload_spill_index[r];
7404 /* If this is a non-inherited input reload from a pseudo, we must
7405 clear any memory of a previous store to the same pseudo. Only do
7406 something if there will not be an output reload for the pseudo
7407 being reloaded. */
7408 if (rld[r].in_reg != 0
7409 && ! (reload_inherited[r] || reload_override_in[r]))
7411 rtx reg = rld[r].in_reg;
7413 if (GET_CODE (reg) == SUBREG)
7414 reg = SUBREG_REG (reg);
7416 if (GET_CODE (reg) == REG
7417 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7418 && ! reg_has_output_reload[REGNO (reg)])
7420 int nregno = REGNO (reg);
7422 if (reg_last_reload_reg[nregno])
7424 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7426 if (reg_reloaded_contents[last_regno] == nregno)
7427 spill_reg_store[last_regno] = 0;
7432 /* I is nonneg if this reload used a register.
7433 If rld[r].reg_rtx is 0, this is an optional reload
7434 that we opted to ignore. */
7436 if (i >= 0 && rld[r].reg_rtx != 0)
7438 int nr
7439 = HARD_REGNO_NREGS (i, GET_MODE (rld[r].reg_rtx));
7440 int k;
7441 int part_reaches_end = 0;
7442 int all_reaches_end = 1;
7444 /* For a multi register reload, we need to check if all or part
7445 of the value lives to the end. */
7446 for (k = 0; k < nr; k++)
7448 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7449 rld[r].when_needed))
7450 part_reaches_end = 1;
7451 else
7452 all_reaches_end = 0;
7455 /* Ignore reloads that don't reach the end of the insn in
7456 entirety. */
7457 if (all_reaches_end)
7459 /* First, clear out memory of what used to be in this spill reg.
7460 If consecutive registers are used, clear them all. */
7462 for (k = 0; k < nr; k++)
7463 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7465 /* Maybe the spill reg contains a copy of reload_out. */
7466 if (rld[r].out != 0
7467 && (GET_CODE (rld[r].out) == REG
7468 #ifdef AUTO_INC_DEC
7469 || ! rld[r].out_reg
7470 #endif
7471 || GET_CODE (rld[r].out_reg) == REG))
7473 rtx out = (GET_CODE (rld[r].out) == REG
7474 ? rld[r].out
7475 : rld[r].out_reg
7476 ? rld[r].out_reg
7477 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7478 register int nregno = REGNO (out);
7479 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7480 : HARD_REGNO_NREGS (nregno,
7481 GET_MODE (rld[r].reg_rtx)));
7483 spill_reg_store[i] = new_spill_reg_store[i];
7484 spill_reg_stored_to[i] = out;
7485 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7487 /* If NREGNO is a hard register, it may occupy more than
7488 one register. If it does, say what is in the
7489 rest of the registers assuming that both registers
7490 agree on how many words the object takes. If not,
7491 invalidate the subsequent registers. */
7493 if (nregno < FIRST_PSEUDO_REGISTER)
7494 for (k = 1; k < nnr; k++)
7495 reg_last_reload_reg[nregno + k]
7496 = (nr == nnr
7497 ? gen_rtx_REG (reg_raw_mode[REGNO (rld[r].reg_rtx) + k],
7498 REGNO (rld[r].reg_rtx) + k)
7499 : 0);
7501 /* Now do the inverse operation. */
7502 for (k = 0; k < nr; k++)
7504 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7505 reg_reloaded_contents[i + k]
7506 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7507 ? nregno
7508 : nregno + k);
7509 reg_reloaded_insn[i + k] = insn;
7510 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7514 /* Maybe the spill reg contains a copy of reload_in. Only do
7515 something if there will not be an output reload for
7516 the register being reloaded. */
7517 else if (rld[r].out_reg == 0
7518 && rld[r].in != 0
7519 && ((GET_CODE (rld[r].in) == REG
7520 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7521 && ! reg_has_output_reload[REGNO (rld[r].in)])
7522 || (GET_CODE (rld[r].in_reg) == REG
7523 && ! reg_has_output_reload[REGNO (rld[r].in_reg)]))
7524 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7526 register int nregno;
7527 int nnr;
7529 if (GET_CODE (rld[r].in) == REG
7530 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7531 nregno = REGNO (rld[r].in);
7532 else if (GET_CODE (rld[r].in_reg) == REG)
7533 nregno = REGNO (rld[r].in_reg);
7534 else
7535 nregno = REGNO (XEXP (rld[r].in_reg, 0));
7537 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7538 : HARD_REGNO_NREGS (nregno,
7539 GET_MODE (rld[r].reg_rtx)));
7541 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7543 if (nregno < FIRST_PSEUDO_REGISTER)
7544 for (k = 1; k < nnr; k++)
7545 reg_last_reload_reg[nregno + k]
7546 = (nr == nnr
7547 ? gen_rtx_REG (reg_raw_mode[REGNO (rld[r].reg_rtx) + k],
7548 REGNO (rld[r].reg_rtx) + k)
7549 : 0);
7551 /* Unless we inherited this reload, show we haven't
7552 recently done a store.
7553 Previous stores of inherited auto_inc expressions
7554 also have to be discarded. */
7555 if (! reload_inherited[r]
7556 || (rld[r].out && ! rld[r].out_reg))
7557 spill_reg_store[i] = 0;
7559 for (k = 0; k < nr; k++)
7561 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7562 reg_reloaded_contents[i + k]
7563 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7564 ? nregno
7565 : nregno + k);
7566 reg_reloaded_insn[i + k] = insn;
7567 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7572 /* However, if part of the reload reaches the end, then we must
7573 invalidate the old info for the part that survives to the end. */
7574 else if (part_reaches_end)
7576 for (k = 0; k < nr; k++)
7577 if (reload_reg_reaches_end_p (i + k,
7578 rld[r].opnum,
7579 rld[r].when_needed))
7580 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7584 /* The following if-statement was #if 0'd in 1.34 (or before...).
7585 It's reenabled in 1.35 because supposedly nothing else
7586 deals with this problem. */
7588 /* If a register gets output-reloaded from a non-spill register,
7589 that invalidates any previous reloaded copy of it.
7590 But forget_old_reloads_1 won't get to see it, because
7591 it thinks only about the original insn. So invalidate it here. */
7592 if (i < 0 && rld[r].out != 0
7593 && (GET_CODE (rld[r].out) == REG
7594 || (GET_CODE (rld[r].out) == MEM
7595 && GET_CODE (rld[r].out_reg) == REG)))
7597 rtx out = (GET_CODE (rld[r].out) == REG
7598 ? rld[r].out : rld[r].out_reg);
7599 register int nregno = REGNO (out);
7600 if (nregno >= FIRST_PSEUDO_REGISTER)
7602 rtx src_reg, store_insn = NULL_RTX;
7604 reg_last_reload_reg[nregno] = 0;
7606 /* If we can find a hard register that is stored, record
7607 the storing insn so that we may delete this insn with
7608 delete_output_reload. */
7609 src_reg = rld[r].reg_rtx;
7611 /* If this is an optional reload, try to find the source reg
7612 from an input reload. */
7613 if (! src_reg)
7615 rtx set = single_set (insn);
7616 if (set && SET_DEST (set) == rld[r].out)
7618 int k;
7620 src_reg = SET_SRC (set);
7621 store_insn = insn;
7622 for (k = 0; k < n_reloads; k++)
7624 if (rld[k].in == src_reg)
7626 src_reg = rld[k].reg_rtx;
7627 break;
7632 else
7633 store_insn = new_spill_reg_store[REGNO (src_reg)];
7634 if (src_reg && GET_CODE (src_reg) == REG
7635 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7637 int src_regno = REGNO (src_reg);
7638 int nr = HARD_REGNO_NREGS (src_regno, reload_mode[r]);
7639 /* The place where to find a death note varies with
7640 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7641 necessarily checked exactly in the code that moves
7642 notes, so just check both locations. */
7643 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7644 if (! note)
7645 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7646 while (nr-- > 0)
7648 spill_reg_store[src_regno + nr] = store_insn;
7649 spill_reg_stored_to[src_regno + nr] = out;
7650 reg_reloaded_contents[src_regno + nr] = nregno;
7651 reg_reloaded_insn[src_regno + nr] = store_insn;
7652 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7653 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7654 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7655 if (note)
7656 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7657 else
7658 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7660 reg_last_reload_reg[nregno] = src_reg;
7663 else
7665 int num_regs = HARD_REGNO_NREGS (nregno,GET_MODE (rld[r].out));
7667 while (num_regs-- > 0)
7668 reg_last_reload_reg[nregno + num_regs] = 0;
7672 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7675 /* Emit code to perform a reload from IN (which may be a reload register) to
7676 OUT (which may also be a reload register). IN or OUT is from operand
7677 OPNUM with reload type TYPE.
7679 Returns first insn emitted. */
7682 gen_reload (out, in, opnum, type)
7683 rtx out;
7684 rtx in;
7685 int opnum;
7686 enum reload_type type;
7688 rtx last = get_last_insn ();
7689 rtx tem;
7691 /* If IN is a paradoxical SUBREG, remove it and try to put the
7692 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7693 if (GET_CODE (in) == SUBREG
7694 && (GET_MODE_SIZE (GET_MODE (in))
7695 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7696 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7697 in = SUBREG_REG (in), out = tem;
7698 else if (GET_CODE (out) == SUBREG
7699 && (GET_MODE_SIZE (GET_MODE (out))
7700 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7701 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7702 out = SUBREG_REG (out), in = tem;
7704 /* How to do this reload can get quite tricky. Normally, we are being
7705 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7706 register that didn't get a hard register. In that case we can just
7707 call emit_move_insn.
7709 We can also be asked to reload a PLUS that adds a register or a MEM to
7710 another register, constant or MEM. This can occur during frame pointer
7711 elimination and while reloading addresses. This case is handled by
7712 trying to emit a single insn to perform the add. If it is not valid,
7713 we use a two insn sequence.
7715 Finally, we could be called to handle an 'o' constraint by putting
7716 an address into a register. In that case, we first try to do this
7717 with a named pattern of "reload_load_address". If no such pattern
7718 exists, we just emit a SET insn and hope for the best (it will normally
7719 be valid on machines that use 'o').
7721 This entire process is made complex because reload will never
7722 process the insns we generate here and so we must ensure that
7723 they will fit their constraints and also by the fact that parts of
7724 IN might be being reloaded separately and replaced with spill registers.
7725 Because of this, we are, in some sense, just guessing the right approach
7726 here. The one listed above seems to work.
7728 ??? At some point, this whole thing needs to be rethought. */
7730 if (GET_CODE (in) == PLUS
7731 && (GET_CODE (XEXP (in, 0)) == REG
7732 || GET_CODE (XEXP (in, 0)) == SUBREG
7733 || GET_CODE (XEXP (in, 0)) == MEM)
7734 && (GET_CODE (XEXP (in, 1)) == REG
7735 || GET_CODE (XEXP (in, 1)) == SUBREG
7736 || CONSTANT_P (XEXP (in, 1))
7737 || GET_CODE (XEXP (in, 1)) == MEM))
7739 /* We need to compute the sum of a register or a MEM and another
7740 register, constant, or MEM, and put it into the reload
7741 register. The best possible way of doing this is if the machine
7742 has a three-operand ADD insn that accepts the required operands.
7744 The simplest approach is to try to generate such an insn and see if it
7745 is recognized and matches its constraints. If so, it can be used.
7747 It might be better not to actually emit the insn unless it is valid,
7748 but we need to pass the insn as an operand to `recog' and
7749 `extract_insn' and it is simpler to emit and then delete the insn if
7750 not valid than to dummy things up. */
7752 rtx op0, op1, tem, insn;
7753 int code;
7755 op0 = find_replacement (&XEXP (in, 0));
7756 op1 = find_replacement (&XEXP (in, 1));
7758 /* Since constraint checking is strict, commutativity won't be
7759 checked, so we need to do that here to avoid spurious failure
7760 if the add instruction is two-address and the second operand
7761 of the add is the same as the reload reg, which is frequently
7762 the case. If the insn would be A = B + A, rearrange it so
7763 it will be A = A + B as constrain_operands expects. */
7765 if (GET_CODE (XEXP (in, 1)) == REG
7766 && REGNO (out) == REGNO (XEXP (in, 1)))
7767 tem = op0, op0 = op1, op1 = tem;
7769 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7770 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7772 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
7773 code = recog_memoized (insn);
7775 if (code >= 0)
7777 extract_insn (insn);
7778 /* We want constrain operands to treat this insn strictly in
7779 its validity determination, i.e., the way it would after reload
7780 has completed. */
7781 if (constrain_operands (1))
7782 return insn;
7785 delete_insns_since (last);
7787 /* If that failed, we must use a conservative two-insn sequence.
7789 Use a move to copy one operand into the reload register. Prefer
7790 to reload a constant, MEM or pseudo since the move patterns can
7791 handle an arbitrary operand. If OP1 is not a constant, MEM or
7792 pseudo and OP1 is not a valid operand for an add instruction, then
7793 reload OP1.
7795 After reloading one of the operands into the reload register, add
7796 the reload register to the output register.
7798 If there is another way to do this for a specific machine, a
7799 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7800 we emit below. */
7802 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7804 if (CONSTANT_P (op1) || GET_CODE (op1) == MEM || GET_CODE (op1) == SUBREG
7805 || (GET_CODE (op1) == REG
7806 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7807 || (code != CODE_FOR_nothing
7808 && ! ((*insn_data[code].operand[2].predicate)
7809 (op1, insn_data[code].operand[2].mode))))
7810 tem = op0, op0 = op1, op1 = tem;
7812 gen_reload (out, op0, opnum, type);
7814 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7815 This fixes a problem on the 32K where the stack pointer cannot
7816 be used as an operand of an add insn. */
7818 if (rtx_equal_p (op0, op1))
7819 op1 = out;
7821 insn = emit_insn (gen_add2_insn (out, op1));
7823 /* If that failed, copy the address register to the reload register.
7824 Then add the constant to the reload register. */
7826 code = recog_memoized (insn);
7828 if (code >= 0)
7830 extract_insn (insn);
7831 /* We want constrain operands to treat this insn strictly in
7832 its validity determination, i.e., the way it would after reload
7833 has completed. */
7834 if (constrain_operands (1))
7836 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7837 REG_NOTES (insn)
7838 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7839 return insn;
7843 delete_insns_since (last);
7845 gen_reload (out, op1, opnum, type);
7846 insn = emit_insn (gen_add2_insn (out, op0));
7847 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7850 #ifdef SECONDARY_MEMORY_NEEDED
7851 /* If we need a memory location to do the move, do it that way. */
7852 else if (GET_CODE (in) == REG && REGNO (in) < FIRST_PSEUDO_REGISTER
7853 && GET_CODE (out) == REG && REGNO (out) < FIRST_PSEUDO_REGISTER
7854 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (in)),
7855 REGNO_REG_CLASS (REGNO (out)),
7856 GET_MODE (out)))
7858 /* Get the memory to use and rewrite both registers to its mode. */
7859 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7861 if (GET_MODE (loc) != GET_MODE (out))
7862 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7864 if (GET_MODE (loc) != GET_MODE (in))
7865 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7867 gen_reload (loc, in, opnum, type);
7868 gen_reload (out, loc, opnum, type);
7870 #endif
7872 /* If IN is a simple operand, use gen_move_insn. */
7873 else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
7874 emit_insn (gen_move_insn (out, in));
7876 #ifdef HAVE_reload_load_address
7877 else if (HAVE_reload_load_address)
7878 emit_insn (gen_reload_load_address (out, in));
7879 #endif
7881 /* Otherwise, just write (set OUT IN) and hope for the best. */
7882 else
7883 emit_insn (gen_rtx_SET (VOIDmode, out, in));
7885 /* Return the first insn emitted.
7886 We can not just return get_last_insn, because there may have
7887 been multiple instructions emitted. Also note that gen_move_insn may
7888 emit more than one insn itself, so we can not assume that there is one
7889 insn emitted per emit_insn_before call. */
7891 return last ? NEXT_INSN (last) : get_insns ();
7894 /* Delete a previously made output-reload
7895 whose result we now believe is not needed.
7896 First we double-check.
7898 INSN is the insn now being processed.
7899 LAST_RELOAD_REG is the hard register number for which we want to delete
7900 the last output reload.
7901 J is the reload-number that originally used REG. The caller has made
7902 certain that reload J doesn't use REG any longer for input. */
7904 static void
7905 delete_output_reload (insn, j, last_reload_reg)
7906 rtx insn;
7907 int j;
7908 int last_reload_reg;
7910 rtx output_reload_insn = spill_reg_store[last_reload_reg];
7911 rtx reg = spill_reg_stored_to[last_reload_reg];
7912 int k;
7913 int n_occurrences;
7914 int n_inherited = 0;
7915 register rtx i1;
7916 rtx substed;
7918 /* Get the raw pseudo-register referred to. */
7920 while (GET_CODE (reg) == SUBREG)
7921 reg = SUBREG_REG (reg);
7922 substed = reg_equiv_memory_loc[REGNO (reg)];
7924 /* This is unsafe if the operand occurs more often in the current
7925 insn than it is inherited. */
7926 for (k = n_reloads - 1; k >= 0; k--)
7928 rtx reg2 = rld[k].in;
7929 if (! reg2)
7930 continue;
7931 if (GET_CODE (reg2) == MEM || reload_override_in[k])
7932 reg2 = rld[k].in_reg;
7933 #ifdef AUTO_INC_DEC
7934 if (rld[k].out && ! rld[k].out_reg)
7935 reg2 = XEXP (rld[k].in_reg, 0);
7936 #endif
7937 while (GET_CODE (reg2) == SUBREG)
7938 reg2 = SUBREG_REG (reg2);
7939 if (rtx_equal_p (reg2, reg))
7941 if (reload_inherited[k] || reload_override_in[k] || k == j)
7943 n_inherited++;
7944 reg2 = rld[k].out_reg;
7945 if (! reg2)
7946 continue;
7947 while (GET_CODE (reg2) == SUBREG)
7948 reg2 = XEXP (reg2, 0);
7949 if (rtx_equal_p (reg2, reg))
7950 n_inherited++;
7952 else
7953 return;
7956 n_occurrences = count_occurrences (PATTERN (insn), reg);
7957 if (substed)
7958 n_occurrences += count_occurrences (PATTERN (insn), substed);
7959 if (n_occurrences > n_inherited)
7960 return;
7962 /* If the pseudo-reg we are reloading is no longer referenced
7963 anywhere between the store into it and here,
7964 and no jumps or labels intervene, then the value can get
7965 here through the reload reg alone.
7966 Otherwise, give up--return. */
7967 for (i1 = NEXT_INSN (output_reload_insn);
7968 i1 != insn; i1 = NEXT_INSN (i1))
7970 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
7971 return;
7972 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
7973 && reg_mentioned_p (reg, PATTERN (i1)))
7975 /* If this is USE in front of INSN, we only have to check that
7976 there are no more references than accounted for by inheritance. */
7977 while (GET_CODE (i1) == INSN && GET_CODE (PATTERN (i1)) == USE)
7979 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
7980 i1 = NEXT_INSN (i1);
7982 if (n_occurrences <= n_inherited && i1 == insn)
7983 break;
7984 return;
7988 /* The caller has already checked that REG dies or is set in INSN.
7989 It has also checked that we are optimizing, and thus some inaccurancies
7990 in the debugging information are acceptable.
7991 So we could just delete output_reload_insn.
7992 But in some cases we can improve the debugging information without
7993 sacrificing optimization - maybe even improving the code:
7994 See if the pseudo reg has been completely replaced
7995 with reload regs. If so, delete the store insn
7996 and forget we had a stack slot for the pseudo. */
7997 if (rld[j].out != rld[j].in
7998 && REG_N_DEATHS (REGNO (reg)) == 1
7999 && REG_N_SETS (REGNO (reg)) == 1
8000 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
8001 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
8003 rtx i2;
8005 /* We know that it was used only between here
8006 and the beginning of the current basic block.
8007 (We also know that the last use before INSN was
8008 the output reload we are thinking of deleting, but never mind that.)
8009 Search that range; see if any ref remains. */
8010 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8012 rtx set = single_set (i2);
8014 /* Uses which just store in the pseudo don't count,
8015 since if they are the only uses, they are dead. */
8016 if (set != 0 && SET_DEST (set) == reg)
8017 continue;
8018 if (GET_CODE (i2) == CODE_LABEL
8019 || GET_CODE (i2) == JUMP_INSN)
8020 break;
8021 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
8022 && reg_mentioned_p (reg, PATTERN (i2)))
8024 /* Some other ref remains; just delete the output reload we
8025 know to be dead. */
8026 delete_address_reloads (output_reload_insn, insn);
8027 PUT_CODE (output_reload_insn, NOTE);
8028 NOTE_SOURCE_FILE (output_reload_insn) = 0;
8029 NOTE_LINE_NUMBER (output_reload_insn) = NOTE_INSN_DELETED;
8030 return;
8034 /* Delete the now-dead stores into this pseudo. */
8035 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8037 rtx set = single_set (i2);
8039 if (set != 0 && SET_DEST (set) == reg)
8041 delete_address_reloads (i2, insn);
8042 /* This might be a basic block head,
8043 thus don't use delete_insn. */
8044 PUT_CODE (i2, NOTE);
8045 NOTE_SOURCE_FILE (i2) = 0;
8046 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
8048 if (GET_CODE (i2) == CODE_LABEL
8049 || GET_CODE (i2) == JUMP_INSN)
8050 break;
8053 /* For the debugging info,
8054 say the pseudo lives in this reload reg. */
8055 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
8056 alter_reg (REGNO (reg), -1);
8058 delete_address_reloads (output_reload_insn, insn);
8059 PUT_CODE (output_reload_insn, NOTE);
8060 NOTE_SOURCE_FILE (output_reload_insn) = 0;
8061 NOTE_LINE_NUMBER (output_reload_insn) = NOTE_INSN_DELETED;
8065 /* We are going to delete DEAD_INSN. Recursively delete loads of
8066 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
8067 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
8068 static void
8069 delete_address_reloads (dead_insn, current_insn)
8070 rtx dead_insn, current_insn;
8072 rtx set = single_set (dead_insn);
8073 rtx set2, dst, prev, next;
8074 if (set)
8076 rtx dst = SET_DEST (set);
8077 if (GET_CODE (dst) == MEM)
8078 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
8080 /* If we deleted the store from a reloaded post_{in,de}c expression,
8081 we can delete the matching adds. */
8082 prev = PREV_INSN (dead_insn);
8083 next = NEXT_INSN (dead_insn);
8084 if (! prev || ! next)
8085 return;
8086 set = single_set (next);
8087 set2 = single_set (prev);
8088 if (! set || ! set2
8089 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
8090 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
8091 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
8092 return;
8093 dst = SET_DEST (set);
8094 if (! rtx_equal_p (dst, SET_DEST (set2))
8095 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
8096 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
8097 || (INTVAL (XEXP (SET_SRC (set), 1))
8098 != - INTVAL (XEXP (SET_SRC (set2), 1))))
8099 return;
8100 delete_insn (prev);
8101 delete_insn (next);
8104 /* Subfunction of delete_address_reloads: process registers found in X. */
8105 static void
8106 delete_address_reloads_1 (dead_insn, x, current_insn)
8107 rtx dead_insn, x, current_insn;
8109 rtx prev, set, dst, i2;
8110 int i, j;
8111 enum rtx_code code = GET_CODE (x);
8113 if (code != REG)
8115 const char *fmt= GET_RTX_FORMAT (code);
8116 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8118 if (fmt[i] == 'e')
8119 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
8120 else if (fmt[i] == 'E')
8122 for (j = XVECLEN (x, i) - 1; j >=0; j--)
8123 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
8124 current_insn);
8127 return;
8130 if (spill_reg_order[REGNO (x)] < 0)
8131 return;
8133 /* Scan backwards for the insn that sets x. This might be a way back due
8134 to inheritance. */
8135 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
8137 code = GET_CODE (prev);
8138 if (code == CODE_LABEL || code == JUMP_INSN)
8139 return;
8140 if (GET_RTX_CLASS (code) != 'i')
8141 continue;
8142 if (reg_set_p (x, PATTERN (prev)))
8143 break;
8144 if (reg_referenced_p (x, PATTERN (prev)))
8145 return;
8147 if (! prev || INSN_UID (prev) < reload_first_uid)
8148 return;
8149 /* Check that PREV only sets the reload register. */
8150 set = single_set (prev);
8151 if (! set)
8152 return;
8153 dst = SET_DEST (set);
8154 if (GET_CODE (dst) != REG
8155 || ! rtx_equal_p (dst, x))
8156 return;
8157 if (! reg_set_p (dst, PATTERN (dead_insn)))
8159 /* Check if DST was used in a later insn -
8160 it might have been inherited. */
8161 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
8163 if (GET_CODE (i2) == CODE_LABEL)
8164 break;
8165 if (GET_RTX_CLASS (GET_CODE (i2)) != 'i')
8166 continue;
8167 if (reg_referenced_p (dst, PATTERN (i2)))
8169 /* If there is a reference to the register in the current insn,
8170 it might be loaded in a non-inherited reload. If no other
8171 reload uses it, that means the register is set before
8172 referenced. */
8173 if (i2 == current_insn)
8175 for (j = n_reloads - 1; j >= 0; j--)
8176 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8177 || reload_override_in[j] == dst)
8178 return;
8179 for (j = n_reloads - 1; j >= 0; j--)
8180 if (rld[j].in && rld[j].reg_rtx == dst)
8181 break;
8182 if (j >= 0)
8183 break;
8185 return;
8187 if (GET_CODE (i2) == JUMP_INSN)
8188 break;
8189 /* If DST is still live at CURRENT_INSN, check if it is used for
8190 any reload. Note that even if CURRENT_INSN sets DST, we still
8191 have to check the reloads. */
8192 if (i2 == current_insn)
8194 for (j = n_reloads - 1; j >= 0; j--)
8195 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8196 || reload_override_in[j] == dst)
8197 return;
8198 /* ??? We can't finish the loop here, because dst might be
8199 allocated to a pseudo in this block if no reload in this
8200 block needs any of the clsses containing DST - see
8201 spill_hard_reg. There is no easy way to tell this, so we
8202 have to scan till the end of the basic block. */
8204 if (reg_set_p (dst, PATTERN (i2)))
8205 break;
8208 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
8209 reg_reloaded_contents[REGNO (dst)] = -1;
8210 /* Can't use delete_insn here because PREV might be a basic block head. */
8211 PUT_CODE (prev, NOTE);
8212 NOTE_LINE_NUMBER (prev) = NOTE_INSN_DELETED;
8213 NOTE_SOURCE_FILE (prev) = 0;
8216 /* Output reload-insns to reload VALUE into RELOADREG.
8217 VALUE is an autoincrement or autodecrement RTX whose operand
8218 is a register or memory location;
8219 so reloading involves incrementing that location.
8220 IN is either identical to VALUE, or some cheaper place to reload from.
8222 INC_AMOUNT is the number to increment or decrement by (always positive).
8223 This cannot be deduced from VALUE.
8225 Return the instruction that stores into RELOADREG. */
8227 static rtx
8228 inc_for_reload (reloadreg, in, value, inc_amount)
8229 rtx reloadreg;
8230 rtx in, value;
8231 int inc_amount;
8233 /* REG or MEM to be copied and incremented. */
8234 rtx incloc = XEXP (value, 0);
8235 /* Nonzero if increment after copying. */
8236 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
8237 rtx last;
8238 rtx inc;
8239 rtx add_insn;
8240 int code;
8241 rtx store;
8242 rtx real_in = in == value ? XEXP (in, 0) : in;
8244 /* No hard register is equivalent to this register after
8245 inc/dec operation. If REG_LAST_RELOAD_REG were non-zero,
8246 we could inc/dec that register as well (maybe even using it for
8247 the source), but I'm not sure it's worth worrying about. */
8248 if (GET_CODE (incloc) == REG)
8249 reg_last_reload_reg[REGNO (incloc)] = 0;
8251 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
8252 inc_amount = - inc_amount;
8254 inc = GEN_INT (inc_amount);
8256 /* If this is post-increment, first copy the location to the reload reg. */
8257 if (post && real_in != reloadreg)
8258 emit_insn (gen_move_insn (reloadreg, real_in));
8260 if (in == value)
8262 /* See if we can directly increment INCLOC. Use a method similar to
8263 that in gen_reload. */
8265 last = get_last_insn ();
8266 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
8267 gen_rtx_PLUS (GET_MODE (incloc),
8268 incloc, inc)));
8270 code = recog_memoized (add_insn);
8271 if (code >= 0)
8273 extract_insn (add_insn);
8274 if (constrain_operands (1))
8276 /* If this is a pre-increment and we have incremented the value
8277 where it lives, copy the incremented value to RELOADREG to
8278 be used as an address. */
8280 if (! post)
8281 emit_insn (gen_move_insn (reloadreg, incloc));
8283 return add_insn;
8286 delete_insns_since (last);
8289 /* If couldn't do the increment directly, must increment in RELOADREG.
8290 The way we do this depends on whether this is pre- or post-increment.
8291 For pre-increment, copy INCLOC to the reload register, increment it
8292 there, then save back. */
8294 if (! post)
8296 if (in != reloadreg)
8297 emit_insn (gen_move_insn (reloadreg, real_in));
8298 emit_insn (gen_add2_insn (reloadreg, inc));
8299 store = emit_insn (gen_move_insn (incloc, reloadreg));
8301 else
8303 /* Postincrement.
8304 Because this might be a jump insn or a compare, and because RELOADREG
8305 may not be available after the insn in an input reload, we must do
8306 the incrementation before the insn being reloaded for.
8308 We have already copied IN to RELOADREG. Increment the copy in
8309 RELOADREG, save that back, then decrement RELOADREG so it has
8310 the original value. */
8312 emit_insn (gen_add2_insn (reloadreg, inc));
8313 store = emit_insn (gen_move_insn (incloc, reloadreg));
8314 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
8317 return store;
8320 /* Return 1 if we are certain that the constraint-string STRING allows
8321 the hard register REG. Return 0 if we can't be sure of this. */
8323 static int
8324 constraint_accepts_reg_p (string, reg)
8325 const char *string;
8326 rtx reg;
8328 int value = 0;
8329 int regno = true_regnum (reg);
8330 int c;
8332 /* Initialize for first alternative. */
8333 value = 0;
8334 /* Check that each alternative contains `g' or `r'. */
8335 while (1)
8336 switch (c = *string++)
8338 case 0:
8339 /* If an alternative lacks `g' or `r', we lose. */
8340 return value;
8341 case ',':
8342 /* If an alternative lacks `g' or `r', we lose. */
8343 if (value == 0)
8344 return 0;
8345 /* Initialize for next alternative. */
8346 value = 0;
8347 break;
8348 case 'g':
8349 case 'r':
8350 /* Any general reg wins for this alternative. */
8351 if (TEST_HARD_REG_BIT (reg_class_contents[(int) GENERAL_REGS], regno))
8352 value = 1;
8353 break;
8354 default:
8355 /* Any reg in specified class wins for this alternative. */
8357 enum reg_class class = REG_CLASS_FROM_LETTER (c);
8359 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno))
8360 value = 1;
8365 /* Return the number of places FIND appears within X, but don't count
8366 an occurrence if some SET_DEST is FIND. */
8369 count_occurrences (x, find)
8370 register rtx x, find;
8372 register int i, j;
8373 register enum rtx_code code;
8374 register const char *format_ptr;
8375 int count;
8377 if (x == find)
8378 return 1;
8379 if (x == 0)
8380 return 0;
8382 code = GET_CODE (x);
8384 switch (code)
8386 case REG:
8387 case QUEUED:
8388 case CONST_INT:
8389 case CONST_DOUBLE:
8390 case SYMBOL_REF:
8391 case CODE_LABEL:
8392 case PC:
8393 case CC0:
8394 return 0;
8396 case MEM:
8397 if (GET_CODE (find) == MEM && rtx_equal_p (x, find))
8398 return 1;
8399 break;
8400 case SET:
8401 if (SET_DEST (x) == find)
8402 return count_occurrences (SET_SRC (x), find);
8403 break;
8405 default:
8406 break;
8409 format_ptr = GET_RTX_FORMAT (code);
8410 count = 0;
8412 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8414 switch (*format_ptr++)
8416 case 'e':
8417 count += count_occurrences (XEXP (x, i), find);
8418 break;
8420 case 'E':
8421 if (XVEC (x, i) != NULL)
8423 for (j = 0; j < XVECLEN (x, i); j++)
8424 count += count_occurrences (XVECEXP (x, i, j), find);
8426 break;
8429 return count;
8432 /* This array holds values which are equivalent to a hard register
8433 during reload_cse_regs. Each array element is an EXPR_LIST of
8434 values. Each time a hard register is set, we set the corresponding
8435 array element to the value. Each time a hard register is copied
8436 into memory, we add the memory location to the corresponding array
8437 element. We don't store values or memory addresses with side
8438 effects in this array.
8440 If the value is a CONST_INT, then the mode of the containing
8441 EXPR_LIST is the mode in which that CONST_INT was referenced.
8443 We sometimes clobber a specific entry in a list. In that case, we
8444 just set XEXP (list-entry, 0) to 0. */
8446 static rtx *reg_values;
8448 /* This is a preallocated REG rtx which we use as a temporary in
8449 reload_cse_invalidate_regno, so that we don't need to allocate a
8450 new one each time through a loop in that function. */
8452 static rtx invalidate_regno_rtx;
8454 /* Invalidate any entries in reg_values which depend on REGNO,
8455 including those for REGNO itself. This is called if REGNO is
8456 changing. If CLOBBER is true, then always forget anything we
8457 currently know about REGNO. MODE is the mode of the assignment to
8458 REGNO, which is used to determine how many hard registers are being
8459 changed. If MODE is VOIDmode, then only REGNO is being changed;
8460 this is used when invalidating call clobbered registers across a
8461 call. */
8463 static void
8464 reload_cse_invalidate_regno (regno, mode, clobber)
8465 int regno;
8466 enum machine_mode mode;
8467 int clobber;
8469 int endregno;
8470 register int i;
8472 /* Our callers don't always go through true_regnum; we may see a
8473 pseudo-register here from a CLOBBER or the like. We probably
8474 won't ever see a pseudo-register that has a real register number,
8475 for we check anyhow for safety. */
8476 if (regno >= FIRST_PSEUDO_REGISTER)
8477 regno = reg_renumber[regno];
8478 if (regno < 0)
8479 return;
8481 if (mode == VOIDmode)
8482 endregno = regno + 1;
8483 else
8484 endregno = regno + HARD_REGNO_NREGS (regno, mode);
8486 if (clobber)
8487 for (i = regno; i < endregno; i++)
8488 reg_values[i] = 0;
8490 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8492 rtx x;
8494 for (x = reg_values[i]; x; x = XEXP (x, 1))
8496 if (XEXP (x, 0) != 0
8497 && refers_to_regno_p (regno, endregno, XEXP (x, 0), NULL_PTR))
8499 /* If this is the only entry on the list, clear
8500 reg_values[i]. Otherwise, just clear this entry on
8501 the list. */
8502 if (XEXP (x, 1) == 0 && x == reg_values[i])
8504 reg_values[i] = 0;
8505 break;
8507 XEXP (x, 0) = 0;
8512 /* We must look at earlier registers, in case REGNO is part of a
8513 multi word value but is not the first register. If an earlier
8514 register has a value in a mode which overlaps REGNO, then we must
8515 invalidate that earlier register. Note that we do not need to
8516 check REGNO or later registers (we must not check REGNO itself,
8517 because we would incorrectly conclude that there was a conflict). */
8519 for (i = 0; i < regno; i++)
8521 rtx x;
8523 for (x = reg_values[i]; x; x = XEXP (x, 1))
8525 if (XEXP (x, 0) != 0)
8527 PUT_MODE (invalidate_regno_rtx, GET_MODE (x));
8528 REGNO (invalidate_regno_rtx) = i;
8529 if (refers_to_regno_p (regno, endregno, invalidate_regno_rtx,
8530 NULL_PTR))
8532 reload_cse_invalidate_regno (i, VOIDmode, 1);
8533 break;
8540 /* The memory at address MEM_BASE is being changed.
8541 Return whether this change will invalidate VAL. */
8543 static int
8544 reload_cse_mem_conflict_p (mem_base, val)
8545 rtx mem_base;
8546 rtx val;
8548 enum rtx_code code;
8549 const char *fmt;
8550 int i;
8552 code = GET_CODE (val);
8553 switch (code)
8555 /* Get rid of a few simple cases quickly. */
8556 case REG:
8557 case PC:
8558 case CC0:
8559 case SCRATCH:
8560 case CONST:
8561 case CONST_INT:
8562 case CONST_DOUBLE:
8563 case SYMBOL_REF:
8564 case LABEL_REF:
8565 return 0;
8567 case MEM:
8568 if (GET_MODE (mem_base) == BLKmode
8569 || GET_MODE (val) == BLKmode)
8570 return 1;
8571 if (anti_dependence (val, mem_base))
8572 return 1;
8573 /* The address may contain nested MEMs. */
8574 break;
8576 default:
8577 break;
8580 fmt = GET_RTX_FORMAT (code);
8582 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8584 if (fmt[i] == 'e')
8586 if (reload_cse_mem_conflict_p (mem_base, XEXP (val, i)))
8587 return 1;
8589 else if (fmt[i] == 'E')
8591 int j;
8593 for (j = 0; j < XVECLEN (val, i); j++)
8594 if (reload_cse_mem_conflict_p (mem_base, XVECEXP (val, i, j)))
8595 return 1;
8599 return 0;
8602 /* Invalidate any entries in reg_values which are changed because of a
8603 store to MEM_RTX. If this is called because of a non-const call
8604 instruction, MEM_RTX is (mem:BLK const0_rtx). */
8606 static void
8607 reload_cse_invalidate_mem (mem_rtx)
8608 rtx mem_rtx;
8610 register int i;
8612 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8614 rtx x;
8616 for (x = reg_values[i]; x; x = XEXP (x, 1))
8618 if (XEXP (x, 0) != 0
8619 && reload_cse_mem_conflict_p (mem_rtx, XEXP (x, 0)))
8621 /* If this is the only entry on the list, clear
8622 reg_values[i]. Otherwise, just clear this entry on
8623 the list. */
8624 if (XEXP (x, 1) == 0 && x == reg_values[i])
8626 reg_values[i] = 0;
8627 break;
8629 XEXP (x, 0) = 0;
8635 /* Invalidate DEST, which is being assigned to or clobbered. The
8636 second parameter exists so that this function can be passed to
8637 note_stores; it is ignored. */
8639 static void
8640 reload_cse_invalidate_rtx (dest, ignore)
8641 rtx dest;
8642 rtx ignore ATTRIBUTE_UNUSED;
8644 while (GET_CODE (dest) == STRICT_LOW_PART
8645 || GET_CODE (dest) == SIGN_EXTRACT
8646 || GET_CODE (dest) == ZERO_EXTRACT
8647 || GET_CODE (dest) == SUBREG)
8648 dest = XEXP (dest, 0);
8650 if (GET_CODE (dest) == REG)
8651 reload_cse_invalidate_regno (REGNO (dest), GET_MODE (dest), 1);
8652 else if (GET_CODE (dest) == MEM)
8653 reload_cse_invalidate_mem (dest);
8656 /* Do a very simple CSE pass over the hard registers.
8658 This function detects no-op moves where we happened to assign two
8659 different pseudo-registers to the same hard register, and then
8660 copied one to the other. Reload will generate a useless
8661 instruction copying a register to itself.
8663 This function also detects cases where we load a value from memory
8664 into two different registers, and (if memory is more expensive than
8665 registers) changes it to simply copy the first register into the
8666 second register.
8668 Another optimization is performed that scans the operands of each
8669 instruction to see whether the value is already available in a
8670 hard register. It then replaces the operand with the hard register
8671 if possible, much like an optional reload would. */
8673 static void
8674 reload_cse_regs_1 (first)
8675 rtx first;
8677 char *firstobj;
8678 rtx callmem;
8679 register int i;
8680 rtx insn;
8682 init_alias_analysis ();
8684 reg_values = (rtx *) alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
8685 bzero ((char *)reg_values, FIRST_PSEUDO_REGISTER * sizeof (rtx));
8687 /* Create our EXPR_LIST structures on reload_obstack, so that we can
8688 free them when we are done. */
8689 push_obstacks (&reload_obstack, &reload_obstack);
8690 firstobj = (char *) obstack_alloc (&reload_obstack, 0);
8692 /* We pass this to reload_cse_invalidate_mem to invalidate all of
8693 memory for a non-const call instruction. */
8694 callmem = gen_rtx_MEM (BLKmode, const0_rtx);
8696 /* This is used in reload_cse_invalidate_regno to avoid consing a
8697 new REG in a loop in that function. */
8698 invalidate_regno_rtx = gen_rtx_REG (VOIDmode, 0);
8700 for (insn = first; insn; insn = NEXT_INSN (insn))
8702 rtx body;
8704 if (GET_CODE (insn) == CODE_LABEL)
8706 /* Forget all the register values at a code label. We don't
8707 try to do anything clever around jumps. */
8708 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8709 reg_values[i] = 0;
8711 continue;
8714 #ifdef NON_SAVING_SETJMP
8715 if (NON_SAVING_SETJMP && GET_CODE (insn) == NOTE
8716 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
8718 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8719 reg_values[i] = 0;
8721 continue;
8723 #endif
8725 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
8726 continue;
8728 /* If this is a call instruction, forget anything stored in a
8729 call clobbered register, or, if this is not a const call, in
8730 memory. */
8731 if (GET_CODE (insn) == CALL_INSN)
8733 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8734 if (call_used_regs[i])
8735 reload_cse_invalidate_regno (i, VOIDmode, 1);
8737 if (! CONST_CALL_P (insn))
8738 reload_cse_invalidate_mem (callmem);
8742 /* Forget all the register values at a volatile asm. */
8743 if (GET_CODE (insn) == INSN
8744 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
8745 && MEM_VOLATILE_P (PATTERN (insn)))
8746 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8747 reg_values[i] = 0;
8749 body = PATTERN (insn);
8750 if (GET_CODE (body) == SET)
8752 int count = 0;
8753 if (reload_cse_noop_set_p (body, insn))
8755 /* If this sets the return value of the function, we must keep
8756 a USE around, in case this is in a different basic block
8757 than the final USE. Otherwise, we could loose important
8758 register lifeness information on SMALL_REGISTER_CLASSES
8759 machines, where return registers might be used as spills:
8760 subsequent passes assume that spill registers are dead at
8761 the end of a basic block. */
8762 if (REG_FUNCTION_VALUE_P (SET_DEST (body)))
8764 pop_obstacks ();
8765 PATTERN (insn) = gen_rtx_USE (VOIDmode, SET_DEST (body));
8766 INSN_CODE (insn) = -1;
8767 REG_NOTES (insn) = NULL_RTX;
8768 push_obstacks (&reload_obstack, &reload_obstack);
8770 else
8772 PUT_CODE (insn, NOTE);
8773 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8774 NOTE_SOURCE_FILE (insn) = 0;
8777 /* We're done with this insn. */
8778 continue;
8781 /* It's not a no-op, but we can try to simplify it. */
8782 count += reload_cse_simplify_set (body, insn);
8784 if (count > 0)
8785 apply_change_group ();
8786 else
8787 reload_cse_simplify_operands (insn);
8789 reload_cse_record_set (body, body);
8791 else if (GET_CODE (body) == PARALLEL)
8793 int count = 0;
8794 rtx value = NULL_RTX;
8796 /* If every action in a PARALLEL is a noop, we can delete
8797 the entire PARALLEL. */
8798 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8800 rtx part = XVECEXP (body, 0, i);
8801 if (GET_CODE (part) == SET)
8803 if (! reload_cse_noop_set_p (part, insn))
8804 break;
8805 if (REG_FUNCTION_VALUE_P (SET_DEST (part)))
8807 if (value)
8808 break;
8809 value = SET_DEST (part);
8812 else if (GET_CODE (part) != CLOBBER)
8813 break;
8815 if (i < 0)
8817 if (value)
8819 pop_obstacks ();
8820 PATTERN (insn) = gen_rtx_USE (VOIDmode, value);
8821 INSN_CODE (insn) = -1;
8822 REG_NOTES (insn) = NULL_RTX;
8823 push_obstacks (&reload_obstack, &reload_obstack);
8825 else
8827 PUT_CODE (insn, NOTE);
8828 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8829 NOTE_SOURCE_FILE (insn) = 0;
8832 /* We're done with this insn. */
8833 continue;
8836 /* It's not a no-op, but we can try to simplify it. */
8837 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8838 if (GET_CODE (XVECEXP (body, 0, i)) == SET)
8839 count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
8841 if (count > 0)
8842 apply_change_group ();
8843 else
8844 reload_cse_simplify_operands (insn);
8846 /* Look through the PARALLEL and record the values being
8847 set, if possible. Also handle any CLOBBERs. */
8848 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8850 rtx x = XVECEXP (body, 0, i);
8852 if (GET_CODE (x) == SET)
8853 reload_cse_record_set (x, body);
8854 else
8855 note_stores (x, reload_cse_invalidate_rtx);
8858 else
8859 note_stores (body, reload_cse_invalidate_rtx);
8861 #ifdef AUTO_INC_DEC
8862 /* Clobber any registers which appear in REG_INC notes. We
8863 could keep track of the changes to their values, but it is
8864 unlikely to help. */
8866 rtx x;
8868 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
8869 if (REG_NOTE_KIND (x) == REG_INC)
8870 reload_cse_invalidate_rtx (XEXP (x, 0), NULL_RTX);
8872 #endif
8874 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
8875 after we have processed the insn. */
8876 if (GET_CODE (insn) == CALL_INSN)
8878 rtx x;
8880 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
8881 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
8882 reload_cse_invalidate_rtx (XEXP (XEXP (x, 0), 0), NULL_RTX);
8886 /* Free all the temporary structures we created, and go back to the
8887 regular obstacks. */
8888 obstack_free (&reload_obstack, firstobj);
8889 pop_obstacks ();
8892 /* Call cse / combine like post-reload optimization phases.
8893 FIRST is the first instruction. */
8894 void
8895 reload_cse_regs (first)
8896 rtx first;
8898 reload_cse_regs_1 (first);
8899 reload_combine ();
8900 reload_cse_move2add (first);
8901 if (flag_expensive_optimizations)
8902 reload_cse_regs_1 (first);
8905 /* Return whether the values known for REGNO are equal to VAL. MODE
8906 is the mode of the object that VAL is being copied to; this matters
8907 if VAL is a CONST_INT. */
8909 static int
8910 reload_cse_regno_equal_p (regno, val, mode)
8911 int regno;
8912 rtx val;
8913 enum machine_mode mode;
8915 rtx x;
8917 if (val == 0)
8918 return 0;
8920 for (x = reg_values[regno]; x; x = XEXP (x, 1))
8921 if (XEXP (x, 0) != 0
8922 && rtx_equal_p (XEXP (x, 0), val)
8923 && (! flag_float_store || GET_CODE (XEXP (x, 0)) != MEM
8924 || GET_MODE_CLASS (GET_MODE (x)) != MODE_FLOAT)
8925 && (GET_CODE (val) != CONST_INT
8926 || mode == GET_MODE (x)
8927 || (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8928 /* On a big endian machine if the value spans more than
8929 one register then this register holds the high part of
8930 it and we can't use it.
8932 ??? We should also compare with the high part of the
8933 value. */
8934 && !(WORDS_BIG_ENDIAN
8935 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
8936 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
8937 GET_MODE_BITSIZE (GET_MODE (x))))))
8938 return 1;
8940 return 0;
8943 /* See whether a single set is a noop. SET is the set instruction we
8944 are should check, and INSN is the instruction from which it came. */
8946 static int
8947 reload_cse_noop_set_p (set, insn)
8948 rtx set;
8949 rtx insn ATTRIBUTE_UNUSED;
8951 rtx src, dest;
8952 enum machine_mode dest_mode;
8953 int dreg, sreg;
8954 int ret;
8956 src = SET_SRC (set);
8957 dest = SET_DEST (set);
8958 dest_mode = GET_MODE (dest);
8960 if (side_effects_p (src))
8961 return 0;
8963 dreg = true_regnum (dest);
8964 sreg = true_regnum (src);
8966 /* Check for setting a register to itself. In this case, we don't
8967 have to worry about REG_DEAD notes. */
8968 if (dreg >= 0 && dreg == sreg)
8969 return 1;
8971 ret = 0;
8972 if (dreg >= 0)
8974 /* Check for setting a register to itself. */
8975 if (dreg == sreg)
8976 ret = 1;
8978 /* Check for setting a register to a value which we already know
8979 is in the register. */
8980 else if (reload_cse_regno_equal_p (dreg, src, dest_mode))
8981 ret = 1;
8983 /* Check for setting a register DREG to another register SREG
8984 where SREG is equal to a value which is already in DREG. */
8985 else if (sreg >= 0)
8987 rtx x;
8989 for (x = reg_values[sreg]; x; x = XEXP (x, 1))
8991 rtx tmp;
8993 if (XEXP (x, 0) == 0)
8994 continue;
8996 if (dest_mode == GET_MODE (x))
8997 tmp = XEXP (x, 0);
8998 else if (GET_MODE_BITSIZE (dest_mode)
8999 < GET_MODE_BITSIZE (GET_MODE (x)))
9000 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
9001 else
9002 continue;
9004 if (tmp
9005 && reload_cse_regno_equal_p (dreg, tmp, dest_mode))
9007 ret = 1;
9008 break;
9013 else if (GET_CODE (dest) == MEM)
9015 /* Check for storing a register to memory when we know that the
9016 register is equivalent to the memory location. */
9017 if (sreg >= 0
9018 && reload_cse_regno_equal_p (sreg, dest, dest_mode)
9019 && ! side_effects_p (dest))
9020 ret = 1;
9023 return ret;
9026 /* Try to simplify a single SET instruction. SET is the set pattern.
9027 INSN is the instruction it came from.
9028 This function only handles one case: if we set a register to a value
9029 which is not a register, we try to find that value in some other register
9030 and change the set into a register copy. */
9032 static int
9033 reload_cse_simplify_set (set, insn)
9034 rtx set;
9035 rtx insn;
9037 int dreg;
9038 rtx src;
9039 enum machine_mode dest_mode;
9040 enum reg_class dclass;
9041 register int i;
9043 dreg = true_regnum (SET_DEST (set));
9044 if (dreg < 0)
9045 return 0;
9047 src = SET_SRC (set);
9048 if (side_effects_p (src) || true_regnum (src) >= 0)
9049 return 0;
9051 dclass = REGNO_REG_CLASS (dreg);
9053 /* If memory loads are cheaper than register copies, don't change them. */
9054 if (GET_CODE (src) == MEM
9055 && MEMORY_MOVE_COST (GET_MODE (src), dclass, 1) < 2)
9056 return 0;
9058 /* If the constant is cheaper than a register, don't change it. */
9059 if (CONSTANT_P (src)
9060 && rtx_cost (src, SET) < 2)
9061 return 0;
9063 dest_mode = GET_MODE (SET_DEST (set));
9064 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
9066 if (i != dreg
9067 && REGISTER_MOVE_COST (REGNO_REG_CLASS (i), dclass) == 2
9068 && reload_cse_regno_equal_p (i, src, dest_mode))
9070 int validated;
9072 /* Pop back to the real obstacks while changing the insn. */
9073 pop_obstacks ();
9075 validated = validate_change (insn, &SET_SRC (set),
9076 gen_rtx_REG (dest_mode, i), 1);
9078 /* Go back to the obstack we are using for temporary
9079 storage. */
9080 push_obstacks (&reload_obstack, &reload_obstack);
9082 if (validated)
9083 return 1;
9086 return 0;
9089 /* Try to replace operands in INSN with equivalent values that are already
9090 in registers. This can be viewed as optional reloading.
9092 For each non-register operand in the insn, see if any hard regs are
9093 known to be equivalent to that operand. Record the alternatives which
9094 can accept these hard registers. Among all alternatives, select the
9095 ones which are better or equal to the one currently matching, where
9096 "better" is in terms of '?' and '!' constraints. Among the remaining
9097 alternatives, select the one which replaces most operands with
9098 hard registers. */
9100 static int
9101 reload_cse_simplify_operands (insn)
9102 rtx insn;
9104 int i,j;
9106 const char *constraints[MAX_RECOG_OPERANDS];
9108 /* Vector recording how bad an alternative is. */
9109 int *alternative_reject;
9110 /* Vector recording how many registers can be introduced by choosing
9111 this alternative. */
9112 int *alternative_nregs;
9113 /* Array of vectors recording, for each operand and each alternative,
9114 which hard register to substitute, or -1 if the operand should be
9115 left as it is. */
9116 int *op_alt_regno[MAX_RECOG_OPERANDS];
9117 /* Array of alternatives, sorted in order of decreasing desirability. */
9118 int *alternative_order;
9119 rtx reg = gen_rtx_REG (VOIDmode, -1);
9121 extract_insn (insn);
9123 if (recog_data.n_alternatives == 0 || recog_data.n_operands == 0)
9124 return 0;
9126 /* Figure out which alternative currently matches. */
9127 if (! constrain_operands (1))
9128 fatal_insn_not_found (insn);
9130 alternative_reject = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9131 alternative_nregs = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9132 alternative_order = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9133 bzero ((char *)alternative_reject, recog_data.n_alternatives * sizeof (int));
9134 bzero ((char *)alternative_nregs, recog_data.n_alternatives * sizeof (int));
9136 for (i = 0; i < recog_data.n_operands; i++)
9138 enum machine_mode mode;
9139 int regno;
9140 const char *p;
9142 op_alt_regno[i] = (int *) alloca (recog_data.n_alternatives * sizeof (int));
9143 for (j = 0; j < recog_data.n_alternatives; j++)
9144 op_alt_regno[i][j] = -1;
9146 p = constraints[i] = recog_data.constraints[i];
9147 mode = recog_data.operand_mode[i];
9149 /* Add the reject values for each alternative given by the constraints
9150 for this operand. */
9151 j = 0;
9152 while (*p != '\0')
9154 char c = *p++;
9155 if (c == ',')
9156 j++;
9157 else if (c == '?')
9158 alternative_reject[j] += 3;
9159 else if (c == '!')
9160 alternative_reject[j] += 300;
9163 /* We won't change operands which are already registers. We
9164 also don't want to modify output operands. */
9165 regno = true_regnum (recog_data.operand[i]);
9166 if (regno >= 0
9167 || constraints[i][0] == '='
9168 || constraints[i][0] == '+')
9169 continue;
9171 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9173 int class = (int) NO_REGS;
9175 if (! reload_cse_regno_equal_p (regno, recog_data.operand[i], mode))
9176 continue;
9178 REGNO (reg) = regno;
9179 PUT_MODE (reg, mode);
9181 /* We found a register equal to this operand. Now look for all
9182 alternatives that can accept this register and have not been
9183 assigned a register they can use yet. */
9184 j = 0;
9185 p = constraints[i];
9186 for (;;)
9188 char c = *p++;
9190 switch (c)
9192 case '=': case '+': case '?':
9193 case '#': case '&': case '!':
9194 case '*': case '%':
9195 case '0': case '1': case '2': case '3': case '4':
9196 case '5': case '6': case '7': case '8': case '9':
9197 case 'm': case '<': case '>': case 'V': case 'o':
9198 case 'E': case 'F': case 'G': case 'H':
9199 case 's': case 'i': case 'n':
9200 case 'I': case 'J': case 'K': case 'L':
9201 case 'M': case 'N': case 'O': case 'P':
9202 #ifdef EXTRA_CONSTRAINT
9203 case 'Q': case 'R': case 'S': case 'T': case 'U':
9204 #endif
9205 case 'p': case 'X':
9206 /* These don't say anything we care about. */
9207 break;
9209 case 'g': case 'r':
9210 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
9211 break;
9213 default:
9214 class
9215 = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER ((unsigned char)c)];
9216 break;
9218 case ',': case '\0':
9219 /* See if REGNO fits this alternative, and set it up as the
9220 replacement register if we don't have one for this
9221 alternative yet and the operand being replaced is not
9222 a cheap CONST_INT. */
9223 if (op_alt_regno[i][j] == -1
9224 && reg_fits_class_p (reg, class, 0, mode)
9225 && (GET_CODE (recog_data.operand[i]) != CONST_INT
9226 || (rtx_cost (recog_data.operand[i], SET)
9227 > rtx_cost (reg, SET))))
9229 alternative_nregs[j]++;
9230 op_alt_regno[i][j] = regno;
9232 j++;
9233 break;
9236 if (c == '\0')
9237 break;
9242 /* Record all alternatives which are better or equal to the currently
9243 matching one in the alternative_order array. */
9244 for (i = j = 0; i < recog_data.n_alternatives; i++)
9245 if (alternative_reject[i] <= alternative_reject[which_alternative])
9246 alternative_order[j++] = i;
9247 recog_data.n_alternatives = j;
9249 /* Sort it. Given a small number of alternatives, a dumb algorithm
9250 won't hurt too much. */
9251 for (i = 0; i < recog_data.n_alternatives - 1; i++)
9253 int best = i;
9254 int best_reject = alternative_reject[alternative_order[i]];
9255 int best_nregs = alternative_nregs[alternative_order[i]];
9256 int tmp;
9258 for (j = i + 1; j < recog_data.n_alternatives; j++)
9260 int this_reject = alternative_reject[alternative_order[j]];
9261 int this_nregs = alternative_nregs[alternative_order[j]];
9263 if (this_reject < best_reject
9264 || (this_reject == best_reject && this_nregs < best_nregs))
9266 best = j;
9267 best_reject = this_reject;
9268 best_nregs = this_nregs;
9272 tmp = alternative_order[best];
9273 alternative_order[best] = alternative_order[i];
9274 alternative_order[i] = tmp;
9277 /* Substitute the operands as determined by op_alt_regno for the best
9278 alternative. */
9279 j = alternative_order[0];
9281 /* Pop back to the real obstacks while changing the insn. */
9282 pop_obstacks ();
9284 for (i = 0; i < recog_data.n_operands; i++)
9286 enum machine_mode mode = recog_data.operand_mode[i];
9287 if (op_alt_regno[i][j] == -1)
9288 continue;
9290 validate_change (insn, recog_data.operand_loc[i],
9291 gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
9294 for (i = recog_data.n_dups - 1; i >= 0; i--)
9296 int op = recog_data.dup_num[i];
9297 enum machine_mode mode = recog_data.operand_mode[op];
9299 if (op_alt_regno[op][j] == -1)
9300 continue;
9302 validate_change (insn, recog_data.dup_loc[i],
9303 gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
9306 /* Go back to the obstack we are using for temporary
9307 storage. */
9308 push_obstacks (&reload_obstack, &reload_obstack);
9310 return apply_change_group ();
9313 /* These two variables are used to pass information from
9314 reload_cse_record_set to reload_cse_check_clobber. */
9316 static int reload_cse_check_clobbered;
9317 static rtx reload_cse_check_src;
9319 /* See if DEST overlaps with RELOAD_CSE_CHECK_SRC. If it does, set
9320 RELOAD_CSE_CHECK_CLOBBERED. This is called via note_stores. The
9321 second argument, which is passed by note_stores, is ignored. */
9323 static void
9324 reload_cse_check_clobber (dest, ignore)
9325 rtx dest;
9326 rtx ignore ATTRIBUTE_UNUSED;
9328 if (reg_overlap_mentioned_p (dest, reload_cse_check_src))
9329 reload_cse_check_clobbered = 1;
9332 /* Record the result of a SET instruction. SET is the set pattern.
9333 BODY is the pattern of the insn that it came from. */
9335 static void
9336 reload_cse_record_set (set, body)
9337 rtx set;
9338 rtx body;
9340 rtx dest, src, x;
9341 int dreg, sreg;
9342 enum machine_mode dest_mode;
9344 dest = SET_DEST (set);
9345 src = SET_SRC (set);
9346 dreg = true_regnum (dest);
9347 sreg = true_regnum (src);
9348 dest_mode = GET_MODE (dest);
9350 /* Some machines don't define AUTO_INC_DEC, but they still use push
9351 instructions. We need to catch that case here in order to
9352 invalidate the stack pointer correctly. Note that invalidating
9353 the stack pointer is different from invalidating DEST. */
9354 x = dest;
9355 while (GET_CODE (x) == SUBREG
9356 || GET_CODE (x) == ZERO_EXTRACT
9357 || GET_CODE (x) == SIGN_EXTRACT
9358 || GET_CODE (x) == STRICT_LOW_PART)
9359 x = XEXP (x, 0);
9360 if (push_operand (x, GET_MODE (x)))
9362 reload_cse_invalidate_rtx (stack_pointer_rtx, NULL_RTX);
9363 reload_cse_invalidate_rtx (dest, NULL_RTX);
9364 return;
9367 /* We can only handle an assignment to a register, or a store of a
9368 register to a memory location. For other cases, we just clobber
9369 the destination. We also have to just clobber if there are side
9370 effects in SRC or DEST. */
9371 if ((dreg < 0 && GET_CODE (dest) != MEM)
9372 || side_effects_p (src)
9373 || side_effects_p (dest))
9375 reload_cse_invalidate_rtx (dest, NULL_RTX);
9376 return;
9379 #ifdef HAVE_cc0
9380 /* We don't try to handle values involving CC, because it's a pain
9381 to keep track of when they have to be invalidated. */
9382 if (reg_mentioned_p (cc0_rtx, src)
9383 || reg_mentioned_p (cc0_rtx, dest))
9385 reload_cse_invalidate_rtx (dest, NULL_RTX);
9386 return;
9388 #endif
9390 /* If BODY is a PARALLEL, then we need to see whether the source of
9391 SET is clobbered by some other instruction in the PARALLEL. */
9392 if (GET_CODE (body) == PARALLEL)
9394 int i;
9396 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
9398 rtx x;
9400 x = XVECEXP (body, 0, i);
9401 if (x == set)
9402 continue;
9404 reload_cse_check_clobbered = 0;
9405 reload_cse_check_src = src;
9406 note_stores (x, reload_cse_check_clobber);
9407 if (reload_cse_check_clobbered)
9409 reload_cse_invalidate_rtx (dest, NULL_RTX);
9410 return;
9415 if (dreg >= 0)
9417 int i;
9419 /* This is an assignment to a register. Update the value we
9420 have stored for the register. */
9421 if (sreg >= 0)
9423 rtx x;
9425 /* This is a copy from one register to another. Any values
9426 which were valid for SREG are now valid for DREG. If the
9427 mode changes, we use gen_lowpart_common to extract only
9428 the part of the value that is copied. */
9429 reg_values[dreg] = 0;
9430 for (x = reg_values[sreg]; x; x = XEXP (x, 1))
9432 rtx tmp;
9434 if (XEXP (x, 0) == 0)
9435 continue;
9436 if (dest_mode == GET_MODE (XEXP (x, 0)))
9437 tmp = XEXP (x, 0);
9438 else if (GET_MODE_BITSIZE (dest_mode)
9439 > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
9440 continue;
9441 else
9442 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
9443 if (tmp)
9444 reg_values[dreg] = gen_rtx_EXPR_LIST (dest_mode, tmp,
9445 reg_values[dreg]);
9448 else
9449 reg_values[dreg] = gen_rtx_EXPR_LIST (dest_mode, src, NULL_RTX);
9451 /* We've changed DREG, so invalidate any values held by other
9452 registers that depend upon it. */
9453 reload_cse_invalidate_regno (dreg, dest_mode, 0);
9455 /* If this assignment changes more than one hard register,
9456 forget anything we know about the others. */
9457 for (i = 1; i < HARD_REGNO_NREGS (dreg, dest_mode); i++)
9458 reg_values[dreg + i] = 0;
9460 else if (GET_CODE (dest) == MEM)
9462 /* Invalidate conflicting memory locations. */
9463 reload_cse_invalidate_mem (dest);
9465 /* If we're storing a register to memory, add DEST to the list
9466 in REG_VALUES. */
9467 if (sreg >= 0 && ! side_effects_p (dest))
9468 reg_values[sreg] = gen_rtx_EXPR_LIST (dest_mode, dest,
9469 reg_values[sreg]);
9471 else
9473 /* We should have bailed out earlier. */
9474 abort ();
9478 /* If reload couldn't use reg+reg+offset addressing, try to use reg+reg
9479 addressing now.
9480 This code might also be useful when reload gave up on reg+reg addresssing
9481 because of clashes between the return register and INDEX_REG_CLASS. */
9483 /* The maximum number of uses of a register we can keep track of to
9484 replace them with reg+reg addressing. */
9485 #define RELOAD_COMBINE_MAX_USES 6
9487 /* INSN is the insn where a register has ben used, and USEP points to the
9488 location of the register within the rtl. */
9489 struct reg_use { rtx insn, *usep; };
9491 /* If the register is used in some unknown fashion, USE_INDEX is negative.
9492 If it is dead, USE_INDEX is RELOAD_COMBINE_MAX_USES, and STORE_RUID
9493 indicates where it becomes live again.
9494 Otherwise, USE_INDEX is the index of the last encountered use of the
9495 register (which is first among these we have seen since we scan backwards),
9496 OFFSET contains the constant offset that is added to the register in
9497 all encountered uses, and USE_RUID indicates the first encountered, i.e.
9498 last, of these uses.
9499 STORE_RUID is always meaningful if we only want to use a value in a
9500 register in a different place: it denotes the next insn in the insn
9501 stream (i.e. the last ecountered) that sets or clobbers the register. */
9502 static struct
9504 struct reg_use reg_use[RELOAD_COMBINE_MAX_USES];
9505 int use_index;
9506 rtx offset;
9507 int store_ruid;
9508 int use_ruid;
9509 } reg_state[FIRST_PSEUDO_REGISTER];
9511 /* Reverse linear uid. This is increased in reload_combine while scanning
9512 the instructions from last to first. It is used to set last_label_ruid
9513 and the store_ruid / use_ruid fields in reg_state. */
9514 static int reload_combine_ruid;
9516 #define LABEL_LIVE(LABEL) \
9517 (label_live[CODE_LABEL_NUMBER (LABEL) - min_labelno])
9519 static void
9520 reload_combine ()
9522 rtx insn, set;
9523 int first_index_reg = 1, last_index_reg = 0;
9524 int i;
9525 int last_label_ruid;
9526 int min_labelno, n_labels;
9527 HARD_REG_SET ever_live_at_start, *label_live;
9529 /* If reg+reg can be used in offsetable memory adresses, the main chunk of
9530 reload has already used it where appropriate, so there is no use in
9531 trying to generate it now. */
9532 if (double_reg_address_ok && INDEX_REG_CLASS != NO_REGS)
9533 return;
9535 /* To avoid wasting too much time later searching for an index register,
9536 determine the minimum and maximum index register numbers. */
9537 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9539 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i))
9541 if (! last_index_reg)
9542 last_index_reg = i;
9543 first_index_reg = i;
9546 /* If no index register is available, we can quit now. */
9547 if (first_index_reg > last_index_reg)
9548 return;
9550 /* Set up LABEL_LIVE and EVER_LIVE_AT_START. The register lifetime
9551 information is a bit fuzzy immediately after reload, but it's
9552 still good enough to determine which registers are live at a jump
9553 destination. */
9554 min_labelno = get_first_label_num ();
9555 n_labels = max_label_num () - min_labelno;
9556 label_live = (HARD_REG_SET *) xmalloc (n_labels * sizeof (HARD_REG_SET));
9557 CLEAR_HARD_REG_SET (ever_live_at_start);
9558 for (i = n_basic_blocks - 1; i >= 0; i--)
9560 insn = BLOCK_HEAD (i);
9561 if (GET_CODE (insn) == CODE_LABEL)
9563 HARD_REG_SET live;
9565 REG_SET_TO_HARD_REG_SET (live, BASIC_BLOCK (i)->global_live_at_start);
9566 compute_use_by_pseudos (&live, BASIC_BLOCK (i)->global_live_at_start);
9567 COPY_HARD_REG_SET (LABEL_LIVE (insn), live);
9568 IOR_HARD_REG_SET (ever_live_at_start, live);
9572 /* Initialize last_label_ruid, reload_combine_ruid and reg_state. */
9573 last_label_ruid = reload_combine_ruid = 0;
9574 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9576 reg_state[i].store_ruid = reload_combine_ruid;
9577 if (fixed_regs[i])
9578 reg_state[i].use_index = -1;
9579 else
9580 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9583 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
9585 rtx note;
9587 /* We cannot do our optimization across labels. Invalidating all the use
9588 information we have would be costly, so we just note where the label
9589 is and then later disable any optimization that would cross it. */
9590 if (GET_CODE (insn) == CODE_LABEL)
9591 last_label_ruid = reload_combine_ruid;
9592 if (GET_CODE (insn) == BARRIER)
9594 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9595 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9597 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
9598 continue;
9599 reload_combine_ruid++;
9601 /* Look for (set (REGX) (CONST_INT))
9602 (set (REGX) (PLUS (REGX) (REGY)))
9604 ... (MEM (REGX)) ...
9605 and convert it to
9606 (set (REGZ) (CONST_INT))
9608 ... (MEM (PLUS (REGZ) (REGY)))... .
9610 First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
9611 and that we know all uses of REGX before it dies. */
9612 set = single_set (insn);
9613 if (set != NULL_RTX
9614 && GET_CODE (SET_DEST (set)) == REG
9615 && (HARD_REGNO_NREGS (REGNO (SET_DEST (set)),
9616 GET_MODE (SET_DEST (set)))
9617 == 1)
9618 && GET_CODE (SET_SRC (set)) == PLUS
9619 && GET_CODE (XEXP (SET_SRC (set), 1)) == REG
9620 && rtx_equal_p (XEXP (SET_SRC (set), 0), SET_DEST (set))
9621 && last_label_ruid < reg_state[REGNO (SET_DEST (set))].use_ruid)
9623 rtx reg = SET_DEST (set);
9624 rtx plus = SET_SRC (set);
9625 rtx base = XEXP (plus, 1);
9626 rtx prev = prev_nonnote_insn (insn);
9627 rtx prev_set = prev ? single_set (prev) : NULL_RTX;
9628 int regno = REGNO (reg);
9629 rtx const_reg = NULL_RTX;
9630 rtx reg_sum = NULL_RTX;
9632 /* Now, we need an index register.
9633 We'll set index_reg to this index register, const_reg to the
9634 register that is to be loaded with the constant
9635 (denoted as REGZ in the substitution illustration above),
9636 and reg_sum to the register-register that we want to use to
9637 substitute uses of REG (typically in MEMs) with.
9638 First check REG and BASE for being index registers;
9639 we can use them even if they are not dead. */
9640 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], regno)
9641 || TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
9642 REGNO (base)))
9644 const_reg = reg;
9645 reg_sum = plus;
9647 else
9649 /* Otherwise, look for a free index register. Since we have
9650 checked above that neiter REG nor BASE are index registers,
9651 if we find anything at all, it will be different from these
9652 two registers. */
9653 for (i = first_index_reg; i <= last_index_reg; i++)
9655 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i)
9656 && reg_state[i].use_index == RELOAD_COMBINE_MAX_USES
9657 && reg_state[i].store_ruid <= reg_state[regno].use_ruid
9658 && HARD_REGNO_NREGS (i, GET_MODE (reg)) == 1)
9660 rtx index_reg = gen_rtx_REG (GET_MODE (reg), i);
9661 const_reg = index_reg;
9662 reg_sum = gen_rtx_PLUS (GET_MODE (reg), index_reg, base);
9663 break;
9667 /* Check that PREV_SET is indeed (set (REGX) (CONST_INT)) and that
9668 (REGY), i.e. BASE, is not clobbered before the last use we'll
9669 create. */
9670 if (prev_set
9671 && GET_CODE (SET_SRC (prev_set)) == CONST_INT
9672 && rtx_equal_p (SET_DEST (prev_set), reg)
9673 && reg_state[regno].use_index >= 0
9674 && reg_state[REGNO (base)].store_ruid <= reg_state[regno].use_ruid
9675 && reg_sum)
9677 int i;
9679 /* Change destination register and - if necessary - the
9680 constant value in PREV, the constant loading instruction. */
9681 validate_change (prev, &SET_DEST (prev_set), const_reg, 1);
9682 if (reg_state[regno].offset != const0_rtx)
9683 validate_change (prev,
9684 &SET_SRC (prev_set),
9685 GEN_INT (INTVAL (SET_SRC (prev_set))
9686 + INTVAL (reg_state[regno].offset)),
9688 /* Now for every use of REG that we have recorded, replace REG
9689 with REG_SUM. */
9690 for (i = reg_state[regno].use_index;
9691 i < RELOAD_COMBINE_MAX_USES; i++)
9692 validate_change (reg_state[regno].reg_use[i].insn,
9693 reg_state[regno].reg_use[i].usep,
9694 reg_sum, 1);
9696 if (apply_change_group ())
9698 rtx *np;
9700 /* Delete the reg-reg addition. */
9701 PUT_CODE (insn, NOTE);
9702 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9703 NOTE_SOURCE_FILE (insn) = 0;
9705 if (reg_state[regno].offset != const0_rtx)
9707 /* Previous REG_EQUIV / REG_EQUAL notes for PREV
9708 are now invalid. */
9709 for (np = &REG_NOTES (prev); *np; )
9711 if (REG_NOTE_KIND (*np) == REG_EQUAL
9712 || REG_NOTE_KIND (*np) == REG_EQUIV)
9713 *np = XEXP (*np, 1);
9714 else
9715 np = &XEXP (*np, 1);
9718 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
9719 reg_state[REGNO (const_reg)].store_ruid = reload_combine_ruid;
9720 continue;
9724 note_stores (PATTERN (insn), reload_combine_note_store);
9725 if (GET_CODE (insn) == CALL_INSN)
9727 rtx link;
9729 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9731 if (call_used_regs[i])
9733 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9734 reg_state[i].store_ruid = reload_combine_ruid;
9737 for (link = CALL_INSN_FUNCTION_USAGE (insn); link;
9738 link = XEXP (link, 1))
9740 rtx use = XEXP (link, 0);
9741 int regno = REGNO (XEXP (use, 0));
9742 if (GET_CODE (use) == CLOBBER)
9744 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
9745 reg_state[regno].store_ruid = reload_combine_ruid;
9747 else
9748 reg_state[regno].use_index = -1;
9751 if (GET_CODE (insn) == JUMP_INSN && GET_CODE (PATTERN (insn)) != RETURN)
9753 /* Non-spill registers might be used at the call destination in
9754 some unknown fashion, so we have to mark the unknown use. */
9755 HARD_REG_SET *live;
9756 if ((condjump_p (insn) || condjump_in_parallel_p (insn))
9757 && JUMP_LABEL (insn))
9758 live = &LABEL_LIVE (JUMP_LABEL (insn));
9759 else
9760 live = &ever_live_at_start;
9761 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
9763 if (TEST_HARD_REG_BIT (*live, i))
9764 reg_state[i].use_index = -1;
9767 reload_combine_note_use (&PATTERN (insn), insn);
9768 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
9770 if (REG_NOTE_KIND (note) == REG_INC
9771 && GET_CODE (XEXP (note, 0)) == REG)
9773 int regno = REGNO (XEXP (note, 0));
9775 reg_state[regno].store_ruid = reload_combine_ruid;
9776 reg_state[regno].use_index = -1;
9780 free (label_live);
9783 /* Check if DST is a register or a subreg of a register; if it is,
9784 update reg_state[regno].store_ruid and reg_state[regno].use_index
9785 accordingly. Called via note_stores from reload_combine. */
9786 static void
9787 reload_combine_note_store (dst, set)
9788 rtx dst, set;
9790 int regno = 0;
9791 int i;
9792 unsigned size = GET_MODE_SIZE (GET_MODE (dst));
9794 if (GET_CODE (dst) == SUBREG)
9796 regno = SUBREG_WORD (dst);
9797 dst = SUBREG_REG (dst);
9799 if (GET_CODE (dst) != REG)
9800 return;
9801 regno += REGNO (dst);
9803 /* note_stores might have stripped a STRICT_LOW_PART, so we have to be
9804 careful with registers / register parts that are not full words.
9806 Similarly for ZERO_EXTRACT and SIGN_EXTRACT. */
9807 if (GET_CODE (set) != SET
9808 || GET_CODE (SET_DEST (set)) == ZERO_EXTRACT
9809 || GET_CODE (SET_DEST (set)) == SIGN_EXTRACT
9810 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
9812 for (i = (size - 1) / UNITS_PER_WORD + regno; i >= regno; i--)
9814 reg_state[i].use_index = -1;
9815 reg_state[i].store_ruid = reload_combine_ruid;
9818 else
9820 for (i = (size - 1) / UNITS_PER_WORD + regno; i >= regno; i--)
9822 reg_state[i].store_ruid = reload_combine_ruid;
9823 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
9828 /* XP points to a piece of rtl that has to be checked for any uses of
9829 registers.
9830 *XP is the pattern of INSN, or a part of it.
9831 Called from reload_combine, and recursively by itself. */
9832 static void
9833 reload_combine_note_use (xp, insn)
9834 rtx *xp, insn;
9836 rtx x = *xp;
9837 enum rtx_code code = x->code;
9838 const char *fmt;
9839 int i, j;
9840 rtx offset = const0_rtx; /* For the REG case below. */
9842 switch (code)
9844 case SET:
9845 if (GET_CODE (SET_DEST (x)) == REG)
9847 reload_combine_note_use (&SET_SRC (x), insn);
9848 return;
9850 break;
9852 case CLOBBER:
9853 if (GET_CODE (SET_DEST (x)) == REG)
9854 return;
9855 break;
9857 case PLUS:
9858 /* We are interested in (plus (reg) (const_int)) . */
9859 if (GET_CODE (XEXP (x, 0)) != REG || GET_CODE (XEXP (x, 1)) != CONST_INT)
9860 break;
9861 offset = XEXP (x, 1);
9862 x = XEXP (x, 0);
9863 /* Fall through. */
9864 case REG:
9866 int regno = REGNO (x);
9867 int use_index;
9869 /* Some spurious USEs of pseudo registers might remain.
9870 Just ignore them. */
9871 if (regno >= FIRST_PSEUDO_REGISTER)
9872 return;
9874 /* If this register is already used in some unknown fashion, we
9875 can't do anything.
9876 If we decrement the index from zero to -1, we can't store more
9877 uses, so this register becomes used in an unknown fashion. */
9878 use_index = --reg_state[regno].use_index;
9879 if (use_index < 0)
9880 return;
9882 if (use_index != RELOAD_COMBINE_MAX_USES - 1)
9884 /* We have found another use for a register that is already
9885 used later. Check if the offsets match; if not, mark the
9886 register as used in an unknown fashion. */
9887 if (! rtx_equal_p (offset, reg_state[regno].offset))
9889 reg_state[regno].use_index = -1;
9890 return;
9893 else
9895 /* This is the first use of this register we have seen since we
9896 marked it as dead. */
9897 reg_state[regno].offset = offset;
9898 reg_state[regno].use_ruid = reload_combine_ruid;
9900 reg_state[regno].reg_use[use_index].insn = insn;
9901 reg_state[regno].reg_use[use_index].usep = xp;
9902 return;
9905 default:
9906 break;
9909 /* Recursively process the components of X. */
9910 fmt = GET_RTX_FORMAT (code);
9911 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9913 if (fmt[i] == 'e')
9914 reload_combine_note_use (&XEXP (x, i), insn);
9915 else if (fmt[i] == 'E')
9917 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9918 reload_combine_note_use (&XVECEXP (x, i, j), insn);
9923 /* See if we can reduce the cost of a constant by replacing a move with
9924 an add. */
9925 /* We cannot do our optimization across labels. Invalidating all the
9926 information about register contents we have would be costly, so we
9927 use last_label_luid (local variable of reload_cse_move2add) to note
9928 where the label is and then later disable any optimization that would
9929 cross it.
9930 reg_offset[n] / reg_base_reg[n] / reg_mode[n] are only valid if
9931 reg_set_luid[n] is larger than last_label_luid[n] . */
9932 static int reg_set_luid[FIRST_PSEUDO_REGISTER];
9933 /* reg_offset[n] has to be CONST_INT for it and reg_base_reg[n] /
9934 reg_mode[n] to be valid.
9935 If reg_offset[n] is a CONST_INT and reg_base_reg[n] is negative, register n
9936 has been set to reg_offset[n] in mode reg_mode[n] .
9937 If reg_offset[n] is a CONST_INT and reg_base_reg[n] is non-negative,
9938 register n has been set to the sum of reg_offset[n] and register
9939 reg_base_reg[n], calculated in mode reg_mode[n] . */
9940 static rtx reg_offset[FIRST_PSEUDO_REGISTER];
9941 static int reg_base_reg[FIRST_PSEUDO_REGISTER];
9942 static enum machine_mode reg_mode[FIRST_PSEUDO_REGISTER];
9943 /* move2add_luid is linearily increased while scanning the instructions
9944 from first to last. It is used to set reg_set_luid in
9945 reload_cse_move2add and move2add_note_store. */
9946 static int move2add_luid;
9948 /* Generate a CONST_INT and force it in the range of MODE. */
9949 static rtx
9950 gen_mode_int (mode, value)
9951 enum machine_mode mode;
9952 HOST_WIDE_INT value;
9954 HOST_WIDE_INT cval = value & GET_MODE_MASK (mode);
9955 int width = GET_MODE_BITSIZE (mode);
9957 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative number,
9958 sign extend it. */
9959 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
9960 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
9961 cval |= (HOST_WIDE_INT) -1 << width;
9963 return GEN_INT (cval);
9966 static void
9967 reload_cse_move2add (first)
9968 rtx first;
9970 int i;
9971 rtx insn;
9972 int last_label_luid;
9974 for (i = FIRST_PSEUDO_REGISTER-1; i >= 0; i--)
9975 reg_set_luid[i] = 0;
9977 last_label_luid = 0;
9978 move2add_luid = 1;
9979 for (insn = first; insn; insn = NEXT_INSN (insn), move2add_luid++)
9981 rtx pat, note;
9983 if (GET_CODE (insn) == CODE_LABEL)
9984 last_label_luid = move2add_luid;
9985 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
9986 continue;
9987 pat = PATTERN (insn);
9988 /* For simplicity, we only perform this optimization on
9989 straightforward SETs. */
9990 if (GET_CODE (pat) == SET
9991 && GET_CODE (SET_DEST (pat)) == REG)
9993 rtx reg = SET_DEST (pat);
9994 int regno = REGNO (reg);
9995 rtx src = SET_SRC (pat);
9997 /* Check if we have valid information on the contents of this
9998 register in the mode of REG. */
9999 /* ??? We don't know how zero / sign extension is handled, hence
10000 we can't go from a narrower to a wider mode. */
10001 if (reg_set_luid[regno] > last_label_luid
10002 && (GET_MODE_SIZE (GET_MODE (reg))
10003 <= GET_MODE_SIZE (reg_mode[regno]))
10004 && GET_CODE (reg_offset[regno]) == CONST_INT)
10006 /* Try to transform (set (REGX) (CONST_INT A))
10008 (set (REGX) (CONST_INT B))
10010 (set (REGX) (CONST_INT A))
10012 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
10014 if (GET_CODE (src) == CONST_INT && reg_base_reg[regno] < 0)
10016 int success = 0;
10017 rtx new_src
10018 = gen_mode_int (GET_MODE (reg),
10019 INTVAL (src) - INTVAL (reg_offset[regno]));
10020 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
10021 use (set (reg) (reg)) instead.
10022 We don't delete this insn, nor do we convert it into a
10023 note, to avoid losing register notes or the return
10024 value flag. jump2 already knowns how to get rid of
10025 no-op moves. */
10026 if (new_src == const0_rtx)
10027 success = validate_change (insn, &SET_SRC (pat), reg, 0);
10028 else if (rtx_cost (new_src, PLUS) < rtx_cost (src, SET)
10029 && have_add2_insn (GET_MODE (reg)))
10030 success = validate_change (insn, &PATTERN (insn),
10031 gen_add2_insn (reg, new_src), 0);
10032 reg_set_luid[regno] = move2add_luid;
10033 reg_mode[regno] = GET_MODE (reg);
10034 reg_offset[regno] = src;
10035 continue;
10038 /* Try to transform (set (REGX) (REGY))
10039 (set (REGX) (PLUS (REGX) (CONST_INT A)))
10041 (set (REGX) (REGY))
10042 (set (REGX) (PLUS (REGX) (CONST_INT B)))
10044 (REGX) (REGY))
10045 (set (REGX) (PLUS (REGX) (CONST_INT A)))
10047 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
10048 else if (GET_CODE (src) == REG
10049 && reg_base_reg[regno] == REGNO (src)
10050 && reg_set_luid[regno] > reg_set_luid[REGNO (src)])
10052 rtx next = next_nonnote_insn (insn);
10053 rtx set = NULL_RTX;
10054 if (next)
10055 set = single_set (next);
10056 if (next
10057 && set
10058 && SET_DEST (set) == reg
10059 && GET_CODE (SET_SRC (set)) == PLUS
10060 && XEXP (SET_SRC (set), 0) == reg
10061 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
10063 rtx src3 = XEXP (SET_SRC (set), 1);
10064 rtx new_src
10065 = gen_mode_int (GET_MODE (reg),
10066 INTVAL (src3)
10067 - INTVAL (reg_offset[regno]));
10068 int success = 0;
10070 if (new_src == const0_rtx)
10071 /* See above why we create (set (reg) (reg)) here. */
10072 success
10073 = validate_change (next, &SET_SRC (set), reg, 0);
10074 else if ((rtx_cost (new_src, PLUS)
10075 < 2 + rtx_cost (src3, SET))
10076 && have_add2_insn (GET_MODE (reg)))
10077 success
10078 = validate_change (next, &PATTERN (next),
10079 gen_add2_insn (reg, new_src), 0);
10080 if (success)
10082 /* INSN might be the first insn in a basic block
10083 if the preceding insn is a conditional jump
10084 or a possible-throwing call. */
10085 PUT_CODE (insn, NOTE);
10086 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
10087 NOTE_SOURCE_FILE (insn) = 0;
10089 insn = next;
10090 reg_set_luid[regno] = move2add_luid;
10091 reg_mode[regno] = GET_MODE (reg);
10092 reg_offset[regno] = src3;
10093 continue;
10099 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
10101 if (REG_NOTE_KIND (note) == REG_INC
10102 && GET_CODE (XEXP (note, 0)) == REG)
10104 /* Indicate that this register has been recently written to,
10105 but the exact contents are not available. */
10106 int regno = REGNO (XEXP (note, 0));
10107 if (regno < FIRST_PSEUDO_REGISTER)
10109 reg_set_luid[regno] = move2add_luid;
10110 reg_offset[regno] = note;
10114 note_stores (PATTERN (insn), move2add_note_store);
10115 /* If this is a CALL_INSN, all call used registers are stored with
10116 unknown values. */
10117 if (GET_CODE (insn) == CALL_INSN)
10119 for (i = FIRST_PSEUDO_REGISTER-1; i >= 0; i--)
10121 if (call_used_regs[i])
10123 reg_set_luid[i] = move2add_luid;
10124 reg_offset[i] = insn; /* Invalidate contents. */
10131 /* SET is a SET or CLOBBER that sets DST.
10132 Update reg_set_luid, reg_offset and reg_base_reg accordingly.
10133 Called from reload_cse_move2add via note_stores. */
10134 static void
10135 move2add_note_store (dst, set)
10136 rtx dst, set;
10138 int regno = 0;
10139 int i;
10141 enum machine_mode mode = GET_MODE (dst);
10142 if (GET_CODE (dst) == SUBREG)
10144 regno = SUBREG_WORD (dst);
10145 dst = SUBREG_REG (dst);
10147 if (GET_CODE (dst) != REG)
10148 return;
10150 regno += REGNO (dst);
10152 if (HARD_REGNO_NREGS (regno, mode) == 1 && GET_CODE (set) == SET
10153 && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
10154 && GET_CODE (SET_DEST (set)) != SIGN_EXTRACT
10155 && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
10157 rtx src = SET_SRC (set);
10159 reg_mode[regno] = mode;
10160 switch (GET_CODE (src))
10162 case PLUS:
10164 rtx src0 = XEXP (src, 0);
10165 if (GET_CODE (src0) == REG)
10167 if (REGNO (src0) != regno
10168 || reg_offset[regno] != const0_rtx)
10170 reg_base_reg[regno] = REGNO (src0);
10171 reg_set_luid[regno] = move2add_luid;
10173 reg_offset[regno] = XEXP (src, 1);
10174 break;
10176 reg_set_luid[regno] = move2add_luid;
10177 reg_offset[regno] = set; /* Invalidate contents. */
10178 break;
10181 case REG:
10182 reg_base_reg[regno] = REGNO (SET_SRC (set));
10183 reg_offset[regno] = const0_rtx;
10184 reg_set_luid[regno] = move2add_luid;
10185 break;
10187 default:
10188 reg_base_reg[regno] = -1;
10189 reg_offset[regno] = SET_SRC (set);
10190 reg_set_luid[regno] = move2add_luid;
10191 break;
10194 else
10196 for (i = regno + HARD_REGNO_NREGS (regno, mode) - 1; i >= regno; i--)
10198 /* Indicate that this register has been recently written to,
10199 but the exact contents are not available. */
10200 reg_set_luid[i] = move2add_luid;
10201 reg_offset[i] = dst;
10206 #ifdef AUTO_INC_DEC
10207 static void
10208 add_auto_inc_notes (insn, x)
10209 rtx insn;
10210 rtx x;
10212 enum rtx_code code = GET_CODE (x);
10213 const char *fmt;
10214 int i, j;
10216 if (code == MEM && auto_inc_p (XEXP (x, 0)))
10218 REG_NOTES (insn)
10219 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
10220 return;
10223 /* Scan all the operand sub-expressions. */
10224 fmt = GET_RTX_FORMAT (code);
10225 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10227 if (fmt[i] == 'e')
10228 add_auto_inc_notes (insn, XEXP (x, i));
10229 else if (fmt[i] == 'E')
10230 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10231 add_auto_inc_notes (insn, XVECEXP (x, i, j));
10234 #endif