]> gcc.gnu.org Git - gcc.git/blame - gcc/reload1.c
Initial revision
[gcc.git] / gcc / reload1.c
CommitLineData
32131a9c 1/* Reload pseudo regs into hard regs for insns that require hard regs.
a8efe40d 2 Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
32131a9c
RK
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include "config.h"
22#include "rtl.h"
23#include "obstack.h"
24#include "insn-config.h"
25#include "insn-flags.h"
26#include "insn-codes.h"
27#include "flags.h"
28#include "expr.h"
29#include "regs.h"
30#include "hard-reg-set.h"
31#include "reload.h"
32#include "recog.h"
33#include "basic-block.h"
34#include "output.h"
35#include <stdio.h>
36
37/* This file contains the reload pass of the compiler, which is
38 run after register allocation has been done. It checks that
39 each insn is valid (operands required to be in registers really
40 are in registers of the proper class) and fixes up invalid ones
41 by copying values temporarily into registers for the insns
42 that need them.
43
44 The results of register allocation are described by the vector
45 reg_renumber; the insns still contain pseudo regs, but reg_renumber
46 can be used to find which hard reg, if any, a pseudo reg is in.
47
48 The technique we always use is to free up a few hard regs that are
49 called ``reload regs'', and for each place where a pseudo reg
50 must be in a hard reg, copy it temporarily into one of the reload regs.
51
52 All the pseudos that were formerly allocated to the hard regs that
53 are now in use as reload regs must be ``spilled''. This means
54 that they go to other hard regs, or to stack slots if no other
55 available hard regs can be found. Spilling can invalidate more
56 insns, requiring additional need for reloads, so we must keep checking
57 until the process stabilizes.
58
59 For machines with different classes of registers, we must keep track
60 of the register class needed for each reload, and make sure that
61 we allocate enough reload registers of each class.
62
63 The file reload.c contains the code that checks one insn for
64 validity and reports the reloads that it needs. This file
65 is in charge of scanning the entire rtl code, accumulating the
66 reload needs, spilling, assigning reload registers to use for
67 fixing up each insn, and generating the new insns to copy values
68 into the reload registers. */
69\f
70/* During reload_as_needed, element N contains a REG rtx for the hard reg
71 into which pseudo reg N has been reloaded (perhaps for a previous insn). */
72static rtx *reg_last_reload_reg;
73
74/* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
75 for an output reload that stores into reg N. */
76static char *reg_has_output_reload;
77
78/* Indicates which hard regs are reload-registers for an output reload
79 in the current insn. */
80static HARD_REG_SET reg_is_output_reload;
81
82/* Element N is the constant value to which pseudo reg N is equivalent,
83 or zero if pseudo reg N is not equivalent to a constant.
84 find_reloads looks at this in order to replace pseudo reg N
85 with the constant it stands for. */
86rtx *reg_equiv_constant;
87
88/* Element N is a memory location to which pseudo reg N is equivalent,
89 prior to any register elimination (such as frame pointer to stack
90 pointer). Depending on whether or not it is a valid address, this value
91 is transferred to either reg_equiv_address or reg_equiv_mem. */
92static rtx *reg_equiv_memory_loc;
93
94/* Element N is the address of stack slot to which pseudo reg N is equivalent.
95 This is used when the address is not valid as a memory address
96 (because its displacement is too big for the machine.) */
97rtx *reg_equiv_address;
98
99/* Element N is the memory slot to which pseudo reg N is equivalent,
100 or zero if pseudo reg N is not equivalent to a memory slot. */
101rtx *reg_equiv_mem;
102
103/* Widest width in which each pseudo reg is referred to (via subreg). */
104static int *reg_max_ref_width;
105
106/* Element N is the insn that initialized reg N from its equivalent
107 constant or memory slot. */
108static rtx *reg_equiv_init;
109
110/* During reload_as_needed, element N contains the last pseudo regno
111 reloaded into the Nth reload register. This vector is in parallel
112 with spill_regs. If that pseudo reg occupied more than one register,
113 reg_reloaded_contents points to that pseudo for each spill register in
114 use; all of these must remain set for an inheritance to occur. */
115static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
116
117/* During reload_as_needed, element N contains the insn for which
118 the Nth reload register was last used. This vector is in parallel
119 with spill_regs, and its contents are significant only when
120 reg_reloaded_contents is significant. */
121static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
122
123/* Number of spill-regs so far; number of valid elements of spill_regs. */
124static int n_spills;
125
126/* In parallel with spill_regs, contains REG rtx's for those regs.
127 Holds the last rtx used for any given reg, or 0 if it has never
128 been used for spilling yet. This rtx is reused, provided it has
129 the proper mode. */
130static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
131
132/* In parallel with spill_regs, contains nonzero for a spill reg
133 that was stored after the last time it was used.
134 The precise value is the insn generated to do the store. */
135static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
136
137/* This table is the inverse mapping of spill_regs:
138 indexed by hard reg number,
139 it contains the position of that reg in spill_regs,
140 or -1 for something that is not in spill_regs. */
141static short spill_reg_order[FIRST_PSEUDO_REGISTER];
142
143/* This reg set indicates registers that may not be used for retrying global
144 allocation. The registers that may not be used include all spill registers
145 and the frame pointer (if we are using one). */
146HARD_REG_SET forbidden_regs;
147
148/* This reg set indicates registers that are not good for spill registers.
149 They will not be used to complete groups of spill registers. This includes
150 all fixed registers, registers that may be eliminated, and registers
151 explicitly used in the rtl.
152
153 (spill_reg_order prevents these registers from being used to start a
154 group.) */
155static HARD_REG_SET bad_spill_regs;
156
157/* Describes order of use of registers for reloading
158 of spilled pseudo-registers. `spills' is the number of
159 elements that are actually valid; new ones are added at the end. */
160static short spill_regs[FIRST_PSEUDO_REGISTER];
161
162/* Describes order of preference for putting regs into spill_regs.
163 Contains the numbers of all the hard regs, in order most preferred first.
164 This order is different for each function.
165 It is set up by order_regs_for_reload.
166 Empty elements at the end contain -1. */
167static short potential_reload_regs[FIRST_PSEUDO_REGISTER];
168
169/* 1 for a hard register that appears explicitly in the rtl
170 (for example, function value registers, special registers
171 used by insns, structure value pointer registers). */
172static char regs_explicitly_used[FIRST_PSEUDO_REGISTER];
173
174/* Indicates if a register was counted against the need for
175 groups. 0 means it can count against max_nongroup instead. */
176static HARD_REG_SET counted_for_groups;
177
178/* Indicates if a register was counted against the need for
179 non-groups. 0 means it can become part of a new group.
180 During choose_reload_regs, 1 here means don't use this reg
181 as part of a group, even if it seems to be otherwise ok. */
182static HARD_REG_SET counted_for_nongroups;
183
184/* Nonzero if indirect addressing is supported on the machine; this means
185 that spilling (REG n) does not require reloading it into a register in
186 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
187 value indicates the level of indirect addressing supported, e.g., two
188 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
189 a hard register. */
190
191static char spill_indirect_levels;
192
193/* Nonzero if indirect addressing is supported when the innermost MEM is
194 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
195 which these are valid is the same as spill_indirect_levels, above. */
196
197char indirect_symref_ok;
198
199/* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
200
201char double_reg_address_ok;
202
203/* Record the stack slot for each spilled hard register. */
204
205static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
206
207/* Width allocated so far for that stack slot. */
208
209static int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
210
211/* Indexed by register class and basic block number, nonzero if there is
212 any need for a spill register of that class in that basic block.
213 The pointer is 0 if we did stupid allocation and don't know
214 the structure of basic blocks. */
215
216char *basic_block_needs[N_REG_CLASSES];
217
218/* First uid used by insns created by reload in this function.
219 Used in find_equiv_reg. */
220int reload_first_uid;
221
222/* Flag set by local-alloc or global-alloc if anything is live in
223 a call-clobbered reg across calls. */
224
225int caller_save_needed;
226
227/* Set to 1 while reload_as_needed is operating.
228 Required by some machines to handle any generated moves differently. */
229
230int reload_in_progress = 0;
231
232/* These arrays record the insn_code of insns that may be needed to
233 perform input and output reloads of special objects. They provide a
234 place to pass a scratch register. */
235
236enum insn_code reload_in_optab[NUM_MACHINE_MODES];
237enum insn_code reload_out_optab[NUM_MACHINE_MODES];
238
239/* This obstack is used for allocation of rtl during register elmination.
240 The allocated storage can be freed once find_reloads has processed the
241 insn. */
242
243struct obstack reload_obstack;
244char *reload_firstobj;
245
246#define obstack_chunk_alloc xmalloc
247#define obstack_chunk_free free
248
249extern int xmalloc ();
250extern void free ();
251
252/* List of labels that must never be deleted. */
253extern rtx forced_labels;
254\f
255/* This structure is used to record information about register eliminations.
256 Each array entry describes one possible way of eliminating a register
257 in favor of another. If there is more than one way of eliminating a
258 particular register, the most preferred should be specified first. */
259
260static struct elim_table
261{
262 int from; /* Register number to be eliminated. */
263 int to; /* Register number used as replacement. */
264 int initial_offset; /* Initial difference between values. */
265 int can_eliminate; /* Non-zero if this elimination can be done. */
266 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
267 insns made by reload. */
268 int offset; /* Current offset between the two regs. */
a8efe40d 269 int max_offset; /* Maximum offset between the two regs. */
32131a9c
RK
270 int previous_offset; /* Offset at end of previous insn. */
271 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
272 rtx from_rtx; /* REG rtx for the register to be eliminated.
273 We cannot simply compare the number since
274 we might then spuriously replace a hard
275 register corresponding to a pseudo
276 assigned to the reg to be eliminated. */
277 rtx to_rtx; /* REG rtx for the replacement. */
278} reg_eliminate[] =
279
280/* If a set of eliminable registers was specified, define the table from it.
281 Otherwise, default to the normal case of the frame pointer being
282 replaced by the stack pointer. */
283
284#ifdef ELIMINABLE_REGS
285 ELIMINABLE_REGS;
286#else
287 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
288#endif
289
290#define NUM_ELIMINABLE_REGS (sizeof reg_eliminate / sizeof reg_eliminate[0])
291
292/* Record the number of pending eliminations that have an offset not equal
293 to their initial offset. If non-zero, we use a new copy of each
294 replacement result in any insns encountered. */
295static int num_not_at_initial_offset;
296
297/* Count the number of registers that we may be able to eliminate. */
298static int num_eliminable;
299
300/* For each label, we record the offset of each elimination. If we reach
301 a label by more than one path and an offset differs, we cannot do the
302 elimination. This information is indexed by the number of the label.
303 The first table is an array of flags that records whether we have yet
304 encountered a label and the second table is an array of arrays, one
305 entry in the latter array for each elimination. */
306
307static char *offsets_known_at;
308static int (*offsets_at)[NUM_ELIMINABLE_REGS];
309
310/* Number of labels in the current function. */
311
312static int num_labels;
313\f
314void mark_home_live ();
315static void count_possible_groups ();
316static int possible_group_p ();
317static void scan_paradoxical_subregs ();
318static void reload_as_needed ();
319static int modes_equiv_for_class_p ();
320static void alter_reg ();
321static void delete_dead_insn ();
322static int new_spill_reg();
323static void set_label_offsets ();
324static int eliminate_regs_in_insn ();
325static void mark_not_eliminable ();
326static int spill_hard_reg ();
327static void choose_reload_regs ();
328static void emit_reload_insns ();
329static void delete_output_reload ();
330static void forget_old_reloads_1 ();
331static void order_regs_for_reload ();
332static rtx inc_for_reload ();
333static int constraint_accepts_reg_p ();
334static int count_occurrences ();
335
336extern void remove_death ();
337extern rtx adj_offsettable_operand ();
338extern rtx form_sum ();
339\f
340void
341init_reload ()
342{
343 register int i;
344
345 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
346 Set spill_indirect_levels to the number of levels such addressing is
347 permitted, zero if it is not permitted at all. */
348
349 register rtx tem
350 = gen_rtx (MEM, Pmode,
351 gen_rtx (PLUS, Pmode,
352 gen_rtx (REG, Pmode, LAST_VIRTUAL_REGISTER + 1),
353 gen_rtx (CONST_INT, VOIDmode, 4)));
354 spill_indirect_levels = 0;
355
356 while (memory_address_p (QImode, tem))
357 {
358 spill_indirect_levels++;
359 tem = gen_rtx (MEM, Pmode, tem);
360 }
361
362 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
363
364 tem = gen_rtx (MEM, Pmode, gen_rtx (SYMBOL_REF, Pmode, "foo"));
365 indirect_symref_ok = memory_address_p (QImode, tem);
366
367 /* See if reg+reg is a valid (and offsettable) address. */
368
369 tem = gen_rtx (PLUS, Pmode,
370 gen_rtx (REG, Pmode, FRAME_POINTER_REGNUM),
371 gen_rtx (REG, Pmode, FRAME_POINTER_REGNUM));
372 /* This way, we make sure that reg+reg is an offsettable address. */
373 tem = plus_constant (tem, 4);
374
375 double_reg_address_ok = memory_address_p (QImode, tem);
376
377 /* Initialize obstack for our rtl allocation. */
378 gcc_obstack_init (&reload_obstack);
379 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
380
381#ifdef HAVE_SECONDARY_RELOADS
382
383 /* Initialize the optabs for doing special input and output reloads. */
384
385 for (i = 0; i < NUM_MACHINE_MODES; i++)
386 reload_in_optab[i] = reload_out_optab[i] = CODE_FOR_nothing;
387
388#ifdef HAVE_reload_inqi
389 if (HAVE_reload_inqi)
390 reload_in_optab[(int) QImode] = CODE_FOR_reload_inqi;
391#endif
392#ifdef HAVE_reload_inhi
393 if (HAVE_reload_inhi)
394 reload_in_optab[(int) HImode] = CODE_FOR_reload_inhi;
395#endif
396#ifdef HAVE_reload_insi
397 if (HAVE_reload_insi)
398 reload_in_optab[(int) SImode] = CODE_FOR_reload_insi;
399#endif
400#ifdef HAVE_reload_indi
401 if (HAVE_reload_indi)
402 reload_in_optab[(int) DImode] = CODE_FOR_reload_indi;
403#endif
404#ifdef HAVE_reload_inti
405 if (HAVE_reload_inti)
406 reload_in_optab[(int) TImode] = CODE_FOR_reload_inti;
407#endif
408#ifdef HAVE_reload_insf
409 if (HAVE_reload_insf)
410 reload_in_optab[(int) SFmode] = CODE_FOR_reload_insf;
411#endif
412#ifdef HAVE_reload_indf
413 if (HAVE_reload_indf)
414 reload_in_optab[(int) DFmode] = CODE_FOR_reload_indf;
415#endif
416#ifdef HAVE_reload_inxf
417 if (HAVE_reload_inxf)
418 reload_in_optab[(int) XFmode] = CODE_FOR_reload_inxf;
419#endif
420#ifdef HAVE_reload_intf
421 if (HAVE_reload_intf)
422 reload_in_optab[(int) TFmode] = CODE_FOR_reload_intf;
423#endif
424
425#ifdef HAVE_reload_outqi
426 if (HAVE_reload_outqi)
427 reload_out_optab[(int) QImode] = CODE_FOR_reload_outqi;
428#endif
429#ifdef HAVE_reload_outhi
430 if (HAVE_reload_outhi)
431 reload_out_optab[(int) HImode] = CODE_FOR_reload_outhi;
432#endif
433#ifdef HAVE_reload_outsi
434 if (HAVE_reload_outsi)
435 reload_out_optab[(int) SImode] = CODE_FOR_reload_outsi;
436#endif
437#ifdef HAVE_reload_outdi
438 if (HAVE_reload_outdi)
439 reload_out_optab[(int) DImode] = CODE_FOR_reload_outdi;
440#endif
441#ifdef HAVE_reload_outti
442 if (HAVE_reload_outti)
443 reload_out_optab[(int) TImode] = CODE_FOR_reload_outti;
444#endif
445#ifdef HAVE_reload_outsf
446 if (HAVE_reload_outsf)
447 reload_out_optab[(int) SFmode] = CODE_FOR_reload_outsf;
448#endif
449#ifdef HAVE_reload_outdf
450 if (HAVE_reload_outdf)
451 reload_out_optab[(int) DFmode] = CODE_FOR_reload_outdf;
452#endif
453#ifdef HAVE_reload_outxf
454 if (HAVE_reload_outxf)
455 reload_out_optab[(int) XFmode] = CODE_FOR_reload_outxf;
456#endif
457#ifdef HAVE_reload_outtf
458 if (HAVE_reload_outtf)
459 reload_out_optab[(int) TFmode] = CODE_FOR_reload_outtf;
460#endif
461
462#endif /* HAVE_SECONDARY_RELOADS */
463
464}
465
466/* Main entry point for the reload pass, and only entry point
467 in this file.
468
469 FIRST is the first insn of the function being compiled.
470
471 GLOBAL nonzero means we were called from global_alloc
472 and should attempt to reallocate any pseudoregs that we
473 displace from hard regs we will use for reloads.
474 If GLOBAL is zero, we do not have enough information to do that,
475 so any pseudo reg that is spilled must go to the stack.
476
477 DUMPFILE is the global-reg debugging dump file stream, or 0.
478 If it is nonzero, messages are written to it to describe
479 which registers are seized as reload regs, which pseudo regs
480 are spilled from them, and where the pseudo regs are reallocated to. */
481
482void
483reload (first, global, dumpfile)
484 rtx first;
485 int global;
486 FILE *dumpfile;
487{
488 register int class;
489 register int i;
490 register rtx insn;
491 register struct elim_table *ep;
492
493 int something_changed;
494 int something_needs_reloads;
495 int something_needs_elimination;
496 int new_basic_block_needs;
a8efe40d
RK
497 enum reg_class caller_save_spill_class = NO_REGS;
498 int caller_save_group_size = 1;
32131a9c
RK
499
500 /* The basic block number currently being processed for INSN. */
501 int this_block;
502
503 /* Make sure even insns with volatile mem refs are recognizable. */
504 init_recog ();
505
506 /* Enable find_equiv_reg to distinguish insns made by reload. */
507 reload_first_uid = get_max_uid ();
508
509 for (i = 0; i < N_REG_CLASSES; i++)
510 basic_block_needs[i] = 0;
511
512 /* Remember which hard regs appear explicitly
513 before we merge into `regs_ever_live' the ones in which
514 pseudo regs have been allocated. */
515 bcopy (regs_ever_live, regs_explicitly_used, sizeof regs_ever_live);
516
517 /* We don't have a stack slot for any spill reg yet. */
518 bzero (spill_stack_slot, sizeof spill_stack_slot);
519 bzero (spill_stack_slot_width, sizeof spill_stack_slot_width);
520
a8efe40d
RK
521 /* Initialize the save area information for caller-save, in case some
522 are needed. */
523 init_save_areas ();
32131a9c
RK
524
525 /* Compute which hard registers are now in use
526 as homes for pseudo registers.
527 This is done here rather than (eg) in global_alloc
528 because this point is reached even if not optimizing. */
529
530 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
531 mark_home_live (i);
532
533 /* Make sure that the last insn in the chain
534 is not something that needs reloading. */
535 emit_note (0, NOTE_INSN_DELETED);
536
537 /* Find all the pseudo registers that didn't get hard regs
538 but do have known equivalent constants or memory slots.
539 These include parameters (known equivalent to parameter slots)
540 and cse'd or loop-moved constant memory addresses.
541
542 Record constant equivalents in reg_equiv_constant
543 so they will be substituted by find_reloads.
544 Record memory equivalents in reg_mem_equiv so they can
545 be substituted eventually by altering the REG-rtx's. */
546
547 reg_equiv_constant = (rtx *) alloca (max_regno * sizeof (rtx));
548 bzero (reg_equiv_constant, max_regno * sizeof (rtx));
549 reg_equiv_memory_loc = (rtx *) alloca (max_regno * sizeof (rtx));
550 bzero (reg_equiv_memory_loc, max_regno * sizeof (rtx));
551 reg_equiv_mem = (rtx *) alloca (max_regno * sizeof (rtx));
552 bzero (reg_equiv_mem, max_regno * sizeof (rtx));
553 reg_equiv_init = (rtx *) alloca (max_regno * sizeof (rtx));
554 bzero (reg_equiv_init, max_regno * sizeof (rtx));
555 reg_equiv_address = (rtx *) alloca (max_regno * sizeof (rtx));
556 bzero (reg_equiv_address, max_regno * sizeof (rtx));
557 reg_max_ref_width = (int *) alloca (max_regno * sizeof (int));
558 bzero (reg_max_ref_width, max_regno * sizeof (int));
559
560 /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
561 Also find all paradoxical subregs
562 and find largest such for each pseudo. */
563
564 for (insn = first; insn; insn = NEXT_INSN (insn))
565 {
566 rtx set = single_set (insn);
567
568 if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
569 {
570 rtx note = find_reg_note (insn, REG_EQUIV, 0);
a8efe40d
RK
571 if (note
572#ifdef LEGITIMATE_PIC_OPERAND_P
573 && (! CONSTANT_P (XEXP (note, 0)) || ! flag_pic
574 || LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))
575#endif
576 )
32131a9c
RK
577 {
578 rtx x = XEXP (note, 0);
579 i = REGNO (SET_DEST (set));
580 if (i > LAST_VIRTUAL_REGISTER)
581 {
582 if (GET_CODE (x) == MEM)
583 reg_equiv_memory_loc[i] = x;
584 else if (CONSTANT_P (x))
585 {
586 if (LEGITIMATE_CONSTANT_P (x))
587 reg_equiv_constant[i] = x;
588 else
589 reg_equiv_memory_loc[i]
d445b551 590 = force_const_mem (GET_MODE (SET_DEST (set)), x);
32131a9c
RK
591 }
592 else
593 continue;
594
595 /* If this register is being made equivalent to a MEM
596 and the MEM is not SET_SRC, the equivalencing insn
597 is one with the MEM as a SET_DEST and it occurs later.
598 So don't mark this insn now. */
599 if (GET_CODE (x) != MEM
600 || rtx_equal_p (SET_SRC (set), x))
601 reg_equiv_init[i] = insn;
602 }
603 }
604 }
605
606 /* If this insn is setting a MEM from a register equivalent to it,
607 this is the equivalencing insn. */
608 else if (set && GET_CODE (SET_DEST (set)) == MEM
609 && GET_CODE (SET_SRC (set)) == REG
610 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
611 && rtx_equal_p (SET_DEST (set),
612 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
613 reg_equiv_init[REGNO (SET_SRC (set))] = insn;
614
615 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
616 scan_paradoxical_subregs (PATTERN (insn));
617 }
618
619 /* Does this function require a frame pointer? */
620
621 frame_pointer_needed = (! flag_omit_frame_pointer
622#ifdef EXIT_IGNORE_STACK
623 /* ?? If EXIT_IGNORE_STACK is set, we will not save
624 and restore sp for alloca. So we can't eliminate
625 the frame pointer in that case. At some point,
626 we should improve this by emitting the
627 sp-adjusting insns for this case. */
628 || (current_function_calls_alloca
629 && EXIT_IGNORE_STACK)
630#endif
631 || FRAME_POINTER_REQUIRED);
632
633 num_eliminable = 0;
634
635 /* Initialize the table of registers to eliminate. The way we do this
636 depends on how the eliminable registers were defined. */
637#ifdef ELIMINABLE_REGS
638 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
639 {
640 ep->can_eliminate = ep->can_eliminate_previous
641 = (CAN_ELIMINATE (ep->from, ep->to)
642 && (ep->from != FRAME_POINTER_REGNUM || ! frame_pointer_needed));
643 }
644#else
645 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
646 = ! frame_pointer_needed;
647#endif
648
649 /* Count the number of eliminable registers and build the FROM and TO
650 REG rtx's. Note that code in gen_rtx will cause, e.g.,
651 gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
652 We depend on this. */
653 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
654 {
655 num_eliminable += ep->can_eliminate;
656 ep->from_rtx = gen_rtx (REG, Pmode, ep->from);
657 ep->to_rtx = gen_rtx (REG, Pmode, ep->to);
658 }
659
660 num_labels = max_label_num () - get_first_label_num ();
661
662 /* Allocate the tables used to store offset information at labels. */
663 offsets_known_at = (char *) alloca (num_labels);
664 offsets_at
665 = (int (*)[NUM_ELIMINABLE_REGS])
666 alloca (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
667
668 offsets_known_at -= get_first_label_num ();
669 offsets_at -= get_first_label_num ();
670
671 /* Alter each pseudo-reg rtx to contain its hard reg number.
672 Assign stack slots to the pseudos that lack hard regs or equivalents.
673 Do not touch virtual registers. */
674
675 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
676 alter_reg (i, -1);
677
678 /* Round size of stack frame to BIGGEST_ALIGNMENT. This must be done here
679 because the stack size may be a part of the offset computation for
680 register elimination. */
681 assign_stack_local (BLKmode, 0, 0);
682
683 /* If we have some registers we think can be eliminated, scan all insns to
684 see if there is an insn that sets one of these registers to something
685 other than itself plus a constant. If so, the register cannot be
686 eliminated. Doing this scan here eliminates an extra pass through the
687 main reload loop in the most common case where register elimination
688 cannot be done. */
689 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
690 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
691 || GET_CODE (insn) == CALL_INSN)
692 note_stores (PATTERN (insn), mark_not_eliminable);
693
694#ifndef REGISTER_CONSTRAINTS
695 /* If all the pseudo regs have hard regs,
696 except for those that are never referenced,
697 we know that no reloads are needed. */
698 /* But that is not true if there are register constraints, since
699 in that case some pseudos might be in the wrong kind of hard reg. */
700
701 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
702 if (reg_renumber[i] == -1 && reg_n_refs[i] != 0)
703 break;
704
705 if (i == max_regno && num_eliminable = 0 && ! caller_save_needed)
706 return;
707#endif
708
709 /* Compute the order of preference for hard registers to spill.
710 Store them by decreasing preference in potential_reload_regs. */
711
712 order_regs_for_reload ();
713
714 /* So far, no hard regs have been spilled. */
715 n_spills = 0;
716 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
717 spill_reg_order[i] = -1;
718
719 /* On most machines, we can't use any register explicitly used in the
720 rtl as a spill register. But on some, we have to. Those will have
721 taken care to keep the life of hard regs as short as possible. */
722
723#ifdef SMALL_REGISTER_CLASSES
724 CLEAR_HARD_REG_SET (forbidden_regs);
725#else
726 COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
727#endif
728
729 /* Spill any hard regs that we know we can't eliminate. */
730 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
731 if (! ep->can_eliminate)
732 {
733 spill_hard_reg (ep->from, global, dumpfile, 1);
734 regs_ever_live[ep->from] = 1;
735 }
736
737 if (global)
738 for (i = 0; i < N_REG_CLASSES; i++)
739 {
740 basic_block_needs[i] = (char *)alloca (n_basic_blocks);
741 bzero (basic_block_needs[i], n_basic_blocks);
742 }
743
744 /* This loop scans the entire function each go-round
745 and repeats until one repetition spills no additional hard regs. */
746
747 /* This flag is set when a psuedo reg is spilled,
748 to require another pass. Note that getting an additional reload
749 reg does not necessarily imply any pseudo reg was spilled;
750 sometimes we find a reload reg that no pseudo reg was allocated in. */
751 something_changed = 1;
752 /* This flag is set if there are any insns that require reloading. */
753 something_needs_reloads = 0;
754 /* This flag is set if there are any insns that require register
755 eliminations. */
756 something_needs_elimination = 0;
757 while (something_changed)
758 {
759 rtx after_call = 0;
760
761 /* For each class, number of reload regs needed in that class.
762 This is the maximum over all insns of the needs in that class
763 of the individual insn. */
764 int max_needs[N_REG_CLASSES];
765 /* For each class, size of group of consecutive regs
766 that is needed for the reloads of this class. */
767 int group_size[N_REG_CLASSES];
768 /* For each class, max number of consecutive groups needed.
769 (Each group contains group_size[CLASS] consecutive registers.) */
770 int max_groups[N_REG_CLASSES];
771 /* For each class, max number needed of regs that don't belong
772 to any of the groups. */
773 int max_nongroups[N_REG_CLASSES];
774 /* For each class, the machine mode which requires consecutive
775 groups of regs of that class.
776 If two different modes ever require groups of one class,
777 they must be the same size and equally restrictive for that class,
778 otherwise we can't handle the complexity. */
779 enum machine_mode group_mode[N_REG_CLASSES];
780 rtx x;
781
782 something_changed = 0;
783 bzero (max_needs, sizeof max_needs);
784 bzero (max_groups, sizeof max_groups);
785 bzero (max_nongroups, sizeof max_nongroups);
786 bzero (group_size, sizeof group_size);
787 for (i = 0; i < N_REG_CLASSES; i++)
788 group_mode[i] = VOIDmode;
789
790 /* Keep track of which basic blocks are needing the reloads. */
791 this_block = 0;
792
793 /* Remember whether any element of basic_block_needs
794 changes from 0 to 1 in this pass. */
795 new_basic_block_needs = 0;
796
797 /* Reset all offsets on eliminable registers to their initial values. */
798#ifdef ELIMINABLE_REGS
799 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
800 {
801 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
a8efe40d
RK
802 ep->previous_offset = ep->offset
803 = ep->max_offset = ep->initial_offset;
32131a9c
RK
804 }
805#else
806#ifdef INITIAL_FRAME_POINTER_OFFSET
807 INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
808#else
809 if (!FRAME_POINTER_REQUIRED)
810 abort ();
811 reg_eliminate[0].initial_offset = 0;
812#endif
a8efe40d 813 reg_eliminate[0].previous_offset = reg_eliminate[0].max_offset
32131a9c
RK
814 = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
815#endif
816
817 num_not_at_initial_offset = 0;
818
819 bzero (&offsets_known_at[get_first_label_num ()], num_labels);
820
821 /* Set a known offset for each forced label to be at the initial offset
822 of each elimination. We do this because we assume that all
823 computed jumps occur from a location where each elimination is
824 at its initial offset. */
825
826 for (x = forced_labels; x; x = XEXP (x, 1))
827 if (XEXP (x, 0))
828 set_label_offsets (XEXP (x, 0), 0, 1);
829
830 /* For each pseudo register that has an equivalent location defined,
831 try to eliminate any eliminable registers (such as the frame pointer)
832 assuming initial offsets for the replacement register, which
833 is the normal case.
834
835 If the resulting location is directly addressable, substitute
836 the MEM we just got directly for the old REG.
837
838 If it is not addressable but is a constant or the sum of a hard reg
839 and constant, it is probably not addressable because the constant is
840 out of range, in that case record the address; we will generate
841 hairy code to compute the address in a register each time it is
842 needed.
843
844 If the location is not addressable, but does not have one of the
845 above forms, assign a stack slot. We have to do this to avoid the
846 potential of producing lots of reloads if, e.g., a location involves
847 a pseudo that didn't get a hard register and has an equivalent memory
848 location that also involves a pseudo that didn't get a hard register.
849
850 Perhaps at some point we will improve reload_when_needed handling
851 so this problem goes away. But that's very hairy. */
852
853 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
854 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
855 {
856 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, 0);
857
858 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
859 XEXP (x, 0)))
860 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
861 else if (CONSTANT_P (XEXP (x, 0))
862 || (GET_CODE (XEXP (x, 0)) == PLUS
863 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
864 && (REGNO (XEXP (XEXP (x, 0), 0))
865 < FIRST_PSEUDO_REGISTER)
866 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
867 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
868 else
869 {
870 /* Make a new stack slot. Then indicate that something
871 changed so we go back and recompute offsets for
872 eliminable registers because the allocation of memory
873 below might change some offset. reg_equiv_{mem,address}
874 will be set up for this pseudo on the next pass around
875 the loop. */
876 reg_equiv_memory_loc[i] = 0;
877 reg_equiv_init[i] = 0;
878 alter_reg (i, -1);
879 something_changed = 1;
880 }
881 }
882
883 /* If we allocated another psuedo to the stack, redo elimination
884 bookkeeping. */
885 if (something_changed)
886 continue;
887
a8efe40d
RK
888 /* If caller-saves needs a group, initialize the group to include
889 the size and mode required for caller-saves. */
890
891 if (caller_save_group_size > 1)
892 {
893 group_mode[(int) caller_save_spill_class] = Pmode;
894 group_size[(int) caller_save_spill_class] = caller_save_group_size;
895 }
896
32131a9c
RK
897 /* Compute the most additional registers needed by any instruction.
898 Collect information separately for each class of regs. */
899
900 for (insn = first; insn; insn = NEXT_INSN (insn))
901 {
902 if (global && this_block + 1 < n_basic_blocks
903 && insn == basic_block_head[this_block+1])
904 ++this_block;
905
906 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which
907 might include REG_LABEL), we need to see what effects this
908 has on the known offsets at labels. */
909
910 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
911 || (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
912 && REG_NOTES (insn) != 0))
913 set_label_offsets (insn, insn, 0);
914
915 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
916 {
917 /* Nonzero means don't use a reload reg that overlaps
918 the place where a function value can be returned. */
919 rtx avoid_return_reg = 0;
920
921 rtx old_body = PATTERN (insn);
922 int old_code = INSN_CODE (insn);
923 rtx old_notes = REG_NOTES (insn);
924 int did_elimination = 0;
925
926 /* Initially, count RELOAD_OTHER reloads.
927 Later, merge in the other kinds. */
928 int insn_needs[N_REG_CLASSES];
929 int insn_groups[N_REG_CLASSES];
930 int insn_total_groups = 0;
931
932 /* Count RELOAD_FOR_INPUT_RELOAD_ADDRESS reloads. */
933 int insn_needs_for_inputs[N_REG_CLASSES];
934 int insn_groups_for_inputs[N_REG_CLASSES];
935 int insn_total_groups_for_inputs = 0;
936
937 /* Count RELOAD_FOR_OUTPUT_RELOAD_ADDRESS reloads. */
938 int insn_needs_for_outputs[N_REG_CLASSES];
939 int insn_groups_for_outputs[N_REG_CLASSES];
940 int insn_total_groups_for_outputs = 0;
941
942 /* Count RELOAD_FOR_OPERAND_ADDRESS reloads. */
943 int insn_needs_for_operands[N_REG_CLASSES];
944 int insn_groups_for_operands[N_REG_CLASSES];
945 int insn_total_groups_for_operands = 0;
946
32131a9c
RK
947#if 0 /* This wouldn't work nowadays, since optimize_bit_field
948 looks for non-strict memory addresses. */
949 /* Optimization: a bit-field instruction whose field
950 happens to be a byte or halfword in memory
951 can be changed to a move instruction. */
952
953 if (GET_CODE (PATTERN (insn)) == SET)
954 {
955 rtx dest = SET_DEST (PATTERN (insn));
956 rtx src = SET_SRC (PATTERN (insn));
957
958 if (GET_CODE (dest) == ZERO_EXTRACT
959 || GET_CODE (dest) == SIGN_EXTRACT)
960 optimize_bit_field (PATTERN (insn), insn, reg_equiv_mem);
961 if (GET_CODE (src) == ZERO_EXTRACT
962 || GET_CODE (src) == SIGN_EXTRACT)
963 optimize_bit_field (PATTERN (insn), insn, reg_equiv_mem);
964 }
965#endif
966
967 /* If needed, eliminate any eliminable registers. */
968 if (num_eliminable)
969 did_elimination = eliminate_regs_in_insn (insn, 0);
970
971#ifdef SMALL_REGISTER_CLASSES
972 /* Set avoid_return_reg if this is an insn
973 that might use the value of a function call. */
974 if (GET_CODE (insn) == CALL_INSN)
975 {
976 if (GET_CODE (PATTERN (insn)) == SET)
977 after_call = SET_DEST (PATTERN (insn));
978 else if (GET_CODE (PATTERN (insn)) == PARALLEL
979 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
980 after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
981 else
982 after_call = 0;
983 }
984 else if (after_call != 0
985 && !(GET_CODE (PATTERN (insn)) == SET
986 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx))
987 {
988 if (reg_mentioned_p (after_call, PATTERN (insn)))
989 avoid_return_reg = after_call;
990 after_call = 0;
991 }
992#endif /* SMALL_REGISTER_CLASSES */
993
994 /* Analyze the instruction. */
995 find_reloads (insn, 0, spill_indirect_levels, global,
996 spill_reg_order);
997
998 /* Remember for later shortcuts which insns had any reloads or
999 register eliminations.
1000
1001 One might think that it would be worthwhile to mark insns
1002 that need register replacements but not reloads, but this is
1003 not safe because find_reloads may do some manipulation of
1004 the insn (such as swapping commutative operands), which would
1005 be lost when we restore the old pattern after register
1006 replacement. So the actions of find_reloads must be redone in
1007 subsequent passes or in reload_as_needed.
1008
1009 However, it is safe to mark insns that need reloads
1010 but not register replacement. */
1011
1012 PUT_MODE (insn, (did_elimination ? QImode
1013 : n_reloads ? HImode
1014 : VOIDmode));
1015
1016 /* Discard any register replacements done. */
1017 if (did_elimination)
1018 {
1019 obstack_free (&reload_obstack, reload_firstobj);
1020 PATTERN (insn) = old_body;
1021 INSN_CODE (insn) = old_code;
1022 REG_NOTES (insn) = old_notes;
1023 something_needs_elimination = 1;
1024 }
1025
a8efe40d
RK
1026 /* If this insn has no reloads, we need not do anything except
1027 in the case of a CALL_INSN when we have caller-saves and
1028 caller-save needs reloads. */
1029
1030 if (n_reloads == 0
1031 && ! (GET_CODE (insn) == CALL_INSN
1032 && caller_save_spill_class != NO_REGS))
32131a9c
RK
1033 continue;
1034
1035 something_needs_reloads = 1;
1036
a8efe40d
RK
1037 for (i = 0; i < N_REG_CLASSES; i++)
1038 {
1039 insn_needs[i] = 0, insn_groups[i] = 0;
1040 insn_needs_for_inputs[i] = 0, insn_groups_for_inputs[i] = 0;
1041 insn_needs_for_outputs[i] = 0, insn_groups_for_outputs[i] = 0;
1042 insn_needs_for_operands[i] = 0, insn_groups_for_operands[i] = 0;
1043 }
1044
32131a9c
RK
1045 /* Count each reload once in every class
1046 containing the reload's own class. */
1047
1048 for (i = 0; i < n_reloads; i++)
1049 {
1050 register enum reg_class *p;
1051 int size;
1052 enum machine_mode mode;
1053 int *this_groups;
1054 int *this_needs;
1055 int *this_total_groups;
1056
1057 /* Don't count the dummy reloads, for which one of the
1058 regs mentioned in the insn can be used for reloading.
1059 Don't count optional reloads.
1060 Don't count reloads that got combined with others. */
1061 if (reload_reg_rtx[i] != 0
1062 || reload_optional[i] != 0
1063 || (reload_out[i] == 0 && reload_in[i] == 0
1064 && ! reload_secondary_p[i]))
1065 continue;
1066
1067 /* Decide which time-of-use to count this reload for. */
1068 switch (reload_when_needed[i])
1069 {
1070 case RELOAD_OTHER:
1071 case RELOAD_FOR_OUTPUT:
1072 case RELOAD_FOR_INPUT:
1073 this_needs = insn_needs;
1074 this_groups = insn_groups;
1075 this_total_groups = &insn_total_groups;
1076 break;
1077
1078 case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
1079 this_needs = insn_needs_for_inputs;
1080 this_groups = insn_groups_for_inputs;
1081 this_total_groups = &insn_total_groups_for_inputs;
1082 break;
1083
1084 case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
1085 this_needs = insn_needs_for_outputs;
1086 this_groups = insn_groups_for_outputs;
1087 this_total_groups = &insn_total_groups_for_outputs;
1088 break;
1089
1090 case RELOAD_FOR_OPERAND_ADDRESS:
1091 this_needs = insn_needs_for_operands;
1092 this_groups = insn_groups_for_operands;
1093 this_total_groups = &insn_total_groups_for_operands;
1094 break;
1095 }
1096
1097 mode = reload_inmode[i];
1098 if (GET_MODE_SIZE (reload_outmode[i]) > GET_MODE_SIZE (mode))
1099 mode = reload_outmode[i];
1100 size = CLASS_MAX_NREGS (reload_reg_class[i], mode);
1101 if (size > 1)
1102 {
1103 enum machine_mode other_mode, allocate_mode;
1104
1105 /* Count number of groups needed separately from
1106 number of individual regs needed. */
1107 this_groups[(int) reload_reg_class[i]]++;
1108 p = reg_class_superclasses[(int) reload_reg_class[i]];
1109 while (*p != LIM_REG_CLASSES)
1110 this_groups[(int) *p++]++;
1111 (*this_total_groups)++;
1112
1113 /* Record size and mode of a group of this class. */
1114 /* If more than one size group is needed,
1115 make all groups the largest needed size. */
1116 if (group_size[(int) reload_reg_class[i]] < size)
1117 {
1118 other_mode = group_mode[(int) reload_reg_class[i]];
1119 allocate_mode = mode;
1120
1121 group_size[(int) reload_reg_class[i]] = size;
1122 group_mode[(int) reload_reg_class[i]] = mode;
1123 }
1124 else
1125 {
1126 other_mode = mode;
1127 allocate_mode = group_mode[(int) reload_reg_class[i]];
1128 }
1129
1130 /* Crash if two dissimilar machine modes both need
1131 groups of consecutive regs of the same class. */
1132
1133 if (other_mode != VOIDmode
1134 && other_mode != allocate_mode
1135 && ! modes_equiv_for_class_p (allocate_mode,
1136 other_mode,
1137 reload_reg_class[i]))
1138 abort ();
1139 }
1140 else if (size == 1)
1141 {
1142 this_needs[(int) reload_reg_class[i]] += 1;
1143 p = reg_class_superclasses[(int) reload_reg_class[i]];
1144 while (*p != LIM_REG_CLASSES)
1145 this_needs[(int) *p++] += 1;
1146 }
1147 else
1148 abort ();
1149 }
1150
1151 /* All reloads have been counted for this insn;
1152 now merge the various times of use.
1153 This sets insn_needs, etc., to the maximum total number
1154 of registers needed at any point in this insn. */
1155
1156 for (i = 0; i < N_REG_CLASSES; i++)
1157 {
1158 int this_max;
1159 this_max = insn_needs_for_inputs[i];
1160 if (insn_needs_for_outputs[i] > this_max)
1161 this_max = insn_needs_for_outputs[i];
1162 if (insn_needs_for_operands[i] > this_max)
1163 this_max = insn_needs_for_operands[i];
1164 insn_needs[i] += this_max;
1165 this_max = insn_groups_for_inputs[i];
1166 if (insn_groups_for_outputs[i] > this_max)
1167 this_max = insn_groups_for_outputs[i];
1168 if (insn_groups_for_operands[i] > this_max)
1169 this_max = insn_groups_for_operands[i];
1170 insn_groups[i] += this_max;
32131a9c 1171 }
a8efe40d 1172
32131a9c
RK
1173 insn_total_groups += MAX (insn_total_groups_for_inputs,
1174 MAX (insn_total_groups_for_outputs,
1175 insn_total_groups_for_operands));
1176
a8efe40d
RK
1177 /* If this is a CALL_INSN and caller-saves will need
1178 a spill register, act as if the spill register is
1179 needed for this insn. However, the spill register
1180 can be used by any reload of this insn, so we only
1181 need do something if no need for that class has
1182 been recorded.
1183
1184 The assumption that every CALL_INSN will trigger a
1185 caller-save is highly conservative, however, the number
1186 of cases where caller-saves will need a spill register but
1187 a block containing a CALL_INSN won't need a spill register
1188 of that class should be quite rare.
1189
1190 If a group is needed, the size and mode of the group will
1191 have been set up at the begining of this loop. */
1192
1193 if (GET_CODE (insn) == CALL_INSN
1194 && caller_save_spill_class != NO_REGS)
1195 {
1196 int *caller_save_needs
1197 = (caller_save_group_size > 1 ? insn_groups : insn_needs);
1198
1199 if (caller_save_needs[(int) caller_save_spill_class] == 0)
1200 {
1201 register enum reg_class *p
1202 = reg_class_superclasses[(int) caller_save_spill_class];
1203
1204 caller_save_needs[(int) caller_save_spill_class]++;
1205
1206 while (*p != LIM_REG_CLASSES)
1207 caller_save_needs[*p++] += 1;
1208 }
1209
1210 if (caller_save_group_size > 1)
1211 insn_total_groups = MAX (insn_total_groups, 1);
1212 }
1213
1214 /* Update the basic block needs. */
1215
1216 for (i = 0; i < N_REG_CLASSES; i++)
1217 if (global && (insn_needs[i] || insn_groups[i])
1218 && ! basic_block_needs[i][this_block])
1219 {
1220 new_basic_block_needs = 1;
1221 basic_block_needs[i][this_block] = 1;
1222 }
1223
32131a9c
RK
1224#ifdef SMALL_REGISTER_CLASSES
1225 /* If this insn stores the value of a function call,
1226 and that value is in a register that has been spilled,
1227 and if the insn needs a reload in a class
1228 that might use that register as the reload register,
1229 then add add an extra need in that class.
1230 This makes sure we have a register available that does
1231 not overlap the return value. */
1232 if (avoid_return_reg)
1233 {
1234 int regno = REGNO (avoid_return_reg);
1235 int nregs
1236 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
1237 int r;
1238 int inc_groups = 0;
1239 for (r = regno; r < regno + nregs; r++)
1240 if (spill_reg_order[r] >= 0)
1241 for (i = 0; i < N_REG_CLASSES; i++)
1242 if (TEST_HARD_REG_BIT (reg_class_contents[i], r))
1243 {
1244 if (insn_needs[i] > 0)
1245 insn_needs[i]++;
1246 if (insn_groups[i] > 0
1247 && nregs > 1)
1248 inc_groups = 1;
1249 }
1250 if (inc_groups)
1251 insn_groups[i]++;
1252 }
1253#endif /* SMALL_REGISTER_CLASSES */
1254
1255 /* For each class, collect maximum need of any insn. */
1256
1257 for (i = 0; i < N_REG_CLASSES; i++)
1258 {
1259 if (max_needs[i] < insn_needs[i])
1260 max_needs[i] = insn_needs[i];
1261 if (max_groups[i] < insn_groups[i])
1262 max_groups[i] = insn_groups[i];
1263 if (insn_total_groups > 0)
1264 if (max_nongroups[i] < insn_needs[i])
1265 max_nongroups[i] = insn_needs[i];
1266 }
1267 }
1268 /* Note that there is a continue statement above. */
1269 }
1270
d445b551 1271 /* If we have caller-saves, set up the save areas and see if caller-save
a8efe40d 1272 will need a spill register. */
32131a9c 1273
d445b551 1274 if (caller_save_needed
a8efe40d
RK
1275 && ! setup_save_areas (&something_changed)
1276 && caller_save_spill_class == NO_REGS)
32131a9c 1277 {
a8efe40d
RK
1278 /* The class we will need depends on whether the machine
1279 supports the sum of two registers for an address; see
1280 find_address_reloads for details. */
1281
1282 caller_save_spill_class
1283 = double_reg_address_ok ? INDEX_REG_CLASS : BASE_REG_CLASS;
1284 caller_save_group_size
1285 = CLASS_MAX_NREGS (caller_save_spill_class, Pmode);
1286 something_changed = 1;
32131a9c
RK
1287 }
1288
1289 /* Now deduct from the needs for the registers already
1290 available (already spilled). */
1291
1292 CLEAR_HARD_REG_SET (counted_for_groups);
1293 CLEAR_HARD_REG_SET (counted_for_nongroups);
1294
1295 /* First find all regs alone in their class
1296 and count them (if desired) for non-groups.
1297 We would be screwed if a group took the only reg in a class
d445b551 1298 for which a non-group reload is needed.
32131a9c
RK
1299 (Note there is still a bug; if a class has 2 regs,
1300 both could be stolen by groups and we would lose the same way.
1301 With luck, no machine will need a nongroup in a 2-reg class.) */
1302
1303 for (i = 0; i < n_spills; i++)
1304 {
1305 register enum reg_class *p;
1306 class = (int) REGNO_REG_CLASS (spill_regs[i]);
1307
1308 if (reg_class_size[class] == 1 && max_nongroups[class] > 0)
1309 {
1310 max_needs[class]--;
1311 p = reg_class_superclasses[class];
1312 while (*p != LIM_REG_CLASSES)
1313 max_needs[(int) *p++]--;
1314
1315 SET_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]);
1316 max_nongroups[class]--;
1317 p = reg_class_superclasses[class];
1318 while (*p != LIM_REG_CLASSES)
1319 {
1320 if (max_nongroups[(int) *p] > 0)
1321 SET_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]);
1322 max_nongroups[(int) *p++]--;
1323 }
1324 }
1325 }
1326
1327 /* Now find all consecutive groups of spilled registers
1328 and mark each group off against the need for such groups.
1329 But don't count them against ordinary need, yet. */
1330
1331 count_possible_groups (group_size, group_mode, max_groups);
1332
1333 /* Now count all spill regs against the individual need,
1334 This includes those counted above for groups,
1335 but not those previously counted for nongroups.
1336
1337 Those that weren't counted_for_groups can also count against
1338 the not-in-group need. */
1339
1340 for (i = 0; i < n_spills; i++)
1341 {
1342 register enum reg_class *p;
1343 class = (int) REGNO_REG_CLASS (spill_regs[i]);
1344
1345 /* Those counted at the beginning shouldn't be counted twice. */
1346 if (! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
1347 {
1348 max_needs[class]--;
1349 p = reg_class_superclasses[class];
1350 while (*p != LIM_REG_CLASSES)
1351 max_needs[(int) *p++]--;
1352
1353 if (! TEST_HARD_REG_BIT (counted_for_groups, spill_regs[i]))
1354 {
1355 if (max_nongroups[class] > 0)
1356 SET_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]);
1357 max_nongroups[class]--;
1358 p = reg_class_superclasses[class];
1359 while (*p != LIM_REG_CLASSES)
1360 {
1361 if (max_nongroups[(int) *p] > 0)
1362 SET_HARD_REG_BIT (counted_for_nongroups,
1363 spill_regs[i]);
1364 max_nongroups[(int) *p++]--;
1365 }
1366 }
1367 }
1368 }
1369
1370 /* Look for the case where we have discovered that we can't replace
1371 register A with register B and that means that we will now be
1372 trying to replace register A with register C. This means we can
1373 no longer replace register C with register B and we need to disable
1374 such an elimination, if it exists. This occurs often with A == ap,
1375 B == sp, and C == fp. */
1376
1377 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1378 {
1379 struct elim_table *op;
1380 register int new_to = -1;
1381
1382 if (! ep->can_eliminate && ep->can_eliminate_previous)
1383 {
1384 /* Find the current elimination for ep->from, if there is a
1385 new one. */
1386 for (op = reg_eliminate;
1387 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
1388 if (op->from == ep->from && op->can_eliminate)
1389 {
1390 new_to = op->to;
1391 break;
1392 }
1393
1394 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
1395 disable it. */
1396 for (op = reg_eliminate;
1397 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
1398 if (op->from == new_to && op->to == ep->to)
1399 op->can_eliminate = 0;
1400 }
1401 }
1402
1403 /* See if any registers that we thought we could eliminate the previous
1404 time are no longer eliminable. If so, something has changed and we
1405 must spill the register. Also, recompute the number of eliminable
1406 registers and see if the frame pointer is needed; it is if there is
1407 no elimination of the frame pointer that we can perform. */
1408
1409 frame_pointer_needed = 1;
1410 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1411 {
1412 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM)
1413 frame_pointer_needed = 0;
1414
1415 if (! ep->can_eliminate && ep->can_eliminate_previous)
1416 {
1417 ep->can_eliminate_previous = 0;
1418 spill_hard_reg (ep->from, global, dumpfile, 1);
1419 regs_ever_live[ep->from] = 1;
1420 something_changed = 1;
1421 num_eliminable--;
1422 }
1423 }
1424
1425 /* If all needs are met, we win. */
1426
1427 for (i = 0; i < N_REG_CLASSES; i++)
1428 if (max_needs[i] > 0 || max_groups[i] > 0 || max_nongroups[i] > 0)
1429 break;
1430 if (i == N_REG_CLASSES && !new_basic_block_needs && ! something_changed)
1431 break;
1432
1433 /* Not all needs are met; must spill more hard regs. */
1434
1435 /* If any element of basic_block_needs changed from 0 to 1,
1436 re-spill all the regs already spilled. This may spill
1437 additional pseudos that didn't spill before. */
1438
1439 if (new_basic_block_needs)
1440 for (i = 0; i < n_spills; i++)
1441 something_changed
1442 |= spill_hard_reg (spill_regs[i], global, dumpfile, 0);
1443
1444 /* Now find more reload regs to satisfy the remaining need
1445 Do it by ascending class number, since otherwise a reg
1446 might be spilled for a big class and might fail to count
1447 for a smaller class even though it belongs to that class.
1448
1449 Count spilled regs in `spills', and add entries to
1450 `spill_regs' and `spill_reg_order'.
1451
1452 ??? Note there is a problem here.
1453 When there is a need for a group in a high-numbered class,
1454 and also need for non-group regs that come from a lower class,
1455 the non-group regs are chosen first. If there aren't many regs,
1456 they might leave no room for a group.
1457
1458 This was happening on the 386. To fix it, we added the code
1459 that calls possible_group_p, so that the lower class won't
1460 break up the last possible group.
1461
1462 Really fixing the problem would require changes above
1463 in counting the regs already spilled, and in choose_reload_regs.
1464 It might be hard to avoid introducing bugs there. */
1465
1466 for (class = 0; class < N_REG_CLASSES; class++)
1467 {
1468 /* First get the groups of registers.
1469 If we got single registers first, we might fragment
1470 possible groups. */
1471 while (max_groups[class] > 0)
1472 {
1473 /* If any single spilled regs happen to form groups,
1474 count them now. Maybe we don't really need
1475 to spill another group. */
1476 count_possible_groups (group_size, group_mode, max_groups);
1477
1478 /* Groups of size 2 (the only groups used on most machines)
1479 are treated specially. */
1480 if (group_size[class] == 2)
1481 {
1482 /* First, look for a register that will complete a group. */
1483 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1484 {
1485 int j = potential_reload_regs[i];
1486 int other;
1487 if (j >= 0 && ! TEST_HARD_REG_BIT (bad_spill_regs, j)
1488 &&
1489 ((j > 0 && (other = j - 1, spill_reg_order[other] >= 0)
1490 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1491 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1492 && HARD_REGNO_MODE_OK (other, group_mode[class])
1493 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1494 other)
1495 /* We don't want one part of another group.
1496 We could get "two groups" that overlap! */
1497 && ! TEST_HARD_REG_BIT (counted_for_groups, other))
1498 ||
1499 (j < FIRST_PSEUDO_REGISTER - 1
1500 && (other = j + 1, spill_reg_order[other] >= 0)
1501 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1502 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1503 && HARD_REGNO_MODE_OK (j, group_mode[class])
1504 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1505 other)
1506 && ! TEST_HARD_REG_BIT (counted_for_groups,
1507 other))))
1508 {
1509 register enum reg_class *p;
1510
1511 /* We have found one that will complete a group,
1512 so count off one group as provided. */
1513 max_groups[class]--;
1514 p = reg_class_superclasses[class];
1515 while (*p != LIM_REG_CLASSES)
1516 max_groups[(int) *p++]--;
1517
1518 /* Indicate both these regs are part of a group. */
1519 SET_HARD_REG_BIT (counted_for_groups, j);
1520 SET_HARD_REG_BIT (counted_for_groups, other);
1521 break;
1522 }
1523 }
1524 /* We can't complete a group, so start one. */
1525 if (i == FIRST_PSEUDO_REGISTER)
1526 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1527 {
1528 int j = potential_reload_regs[i];
1529 if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1530 && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1531 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1532 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1533 && HARD_REGNO_MODE_OK (j, group_mode[class])
1534 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1535 j + 1))
1536 break;
1537 }
1538
1539 /* I should be the index in potential_reload_regs
1540 of the new reload reg we have found. */
1541
1542 something_changed
1543 |= new_spill_reg (i, class, max_needs, 0,
1544 global, dumpfile);
1545 }
1546 else
1547 {
1548 /* For groups of more than 2 registers,
1549 look for a sufficient sequence of unspilled registers,
1550 and spill them all at once. */
1551 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1552 {
1553 int j = potential_reload_regs[i];
1554 int k;
1555 if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1556 && HARD_REGNO_MODE_OK (j, group_mode[class]))
1557 {
1558 /* Check each reg in the sequence. */
1559 for (k = 0; k < group_size[class]; k++)
1560 if (! (spill_reg_order[j + k] < 0
1561 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + k)
1562 && TEST_HARD_REG_BIT (reg_class_contents[class], j + k)))
1563 break;
1564 /* We got a full sequence, so spill them all. */
1565 if (k == group_size[class])
1566 {
1567 register enum reg_class *p;
1568 for (k = 0; k < group_size[class]; k++)
1569 {
1570 int idx;
1571 SET_HARD_REG_BIT (counted_for_groups, j + k);
1572 for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
1573 if (potential_reload_regs[idx] == j + k)
1574 break;
1575 something_changed
1576 |= new_spill_reg (idx, class, max_needs, 0,
1577 global, dumpfile);
1578 }
1579
1580 /* We have found one that will complete a group,
1581 so count off one group as provided. */
1582 max_groups[class]--;
1583 p = reg_class_superclasses[class];
1584 while (*p != LIM_REG_CLASSES)
1585 max_groups[(int) *p++]--;
1586
1587 break;
1588 }
1589 }
1590 }
1591 }
1592 }
1593
1594 /* Now similarly satisfy all need for single registers. */
1595
1596 while (max_needs[class] > 0 || max_nongroups[class] > 0)
1597 {
1598 /* Consider the potential reload regs that aren't
1599 yet in use as reload regs, in order of preference.
1600 Find the most preferred one that's in this class. */
1601
1602 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1603 if (potential_reload_regs[i] >= 0
1604 && TEST_HARD_REG_BIT (reg_class_contents[class],
1605 potential_reload_regs[i])
1606 /* If this reg will not be available for groups,
1607 pick one that does not foreclose possible groups.
1608 This is a kludge, and not very general,
1609 but it should be sufficient to make the 386 work,
1610 and the problem should not occur on machines with
1611 more registers. */
1612 && (max_nongroups[class] == 0
1613 || possible_group_p (potential_reload_regs[i], max_groups)))
1614 break;
1615
1616 /* I should be the index in potential_reload_regs
1617 of the new reload reg we have found. */
1618
1619 something_changed
1620 |= new_spill_reg (i, class, max_needs, max_nongroups,
1621 global, dumpfile);
1622 }
1623 }
1624 }
1625
1626 /* If global-alloc was run, notify it of any register eliminations we have
1627 done. */
1628 if (global)
1629 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1630 if (ep->can_eliminate)
1631 mark_elimination (ep->from, ep->to);
1632
1633 /* From now on, we need to emit any moves without making new pseudos. */
1634 reload_in_progress = 1;
1635
1636 /* Insert code to save and restore call-clobbered hard regs
a8efe40d
RK
1637 around calls. Tell if what mode to use so that we will process
1638 those insns in reload_as_needed if we have to. */
32131a9c
RK
1639
1640 if (caller_save_needed)
a8efe40d
RK
1641 save_call_clobbered_regs (num_eliminable ? QImode
1642 : caller_save_spill_class != NO_REGS ? HImode
1643 : VOIDmode);
32131a9c
RK
1644
1645 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1646 If that insn didn't set the register (i.e., it copied the register to
1647 memory), just delete that insn instead of the equivalencing insn plus
1648 anything now dead. If we call delete_dead_insn on that insn, we may
1649 delete the insn that actually sets the register if the register die
1650 there and that is incorrect. */
1651
1652 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1653 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0
1654 && GET_CODE (reg_equiv_init[i]) != NOTE)
1655 {
1656 if (reg_set_p (regno_reg_rtx[i], PATTERN (reg_equiv_init[i])))
1657 delete_dead_insn (reg_equiv_init[i]);
1658 else
1659 {
1660 PUT_CODE (reg_equiv_init[i], NOTE);
1661 NOTE_SOURCE_FILE (reg_equiv_init[i]) = 0;
1662 NOTE_LINE_NUMBER (reg_equiv_init[i]) = NOTE_INSN_DELETED;
1663 }
1664 }
1665
1666 /* Use the reload registers where necessary
1667 by generating move instructions to move the must-be-register
1668 values into or out of the reload registers. */
1669
a8efe40d
RK
1670 if (something_needs_reloads || something_needs_elimination
1671 || (caller_save_needed && num_eliminable)
1672 || caller_save_spill_class != NO_REGS)
32131a9c
RK
1673 reload_as_needed (first, global);
1674
1675 reload_in_progress = 0;
1676
1677 /* Now eliminate all pseudo regs by modifying them into
1678 their equivalent memory references.
1679 The REG-rtx's for the pseudos are modified in place,
1680 so all insns that used to refer to them now refer to memory.
1681
1682 For a reg that has a reg_equiv_address, all those insns
1683 were changed by reloading so that no insns refer to it any longer;
1684 but the DECL_RTL of a variable decl may refer to it,
1685 and if so this causes the debugging info to mention the variable. */
1686
1687 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1688 {
1689 rtx addr = 0;
1690 if (reg_equiv_mem[i])
1691 addr = XEXP (reg_equiv_mem[i], 0);
1692 if (reg_equiv_address[i])
1693 addr = reg_equiv_address[i];
1694 if (addr)
1695 {
1696 if (reg_renumber[i] < 0)
1697 {
1698 rtx reg = regno_reg_rtx[i];
1699 XEXP (reg, 0) = addr;
1700 REG_USERVAR_P (reg) = 0;
1701 PUT_CODE (reg, MEM);
1702 }
1703 else if (reg_equiv_mem[i])
1704 XEXP (reg_equiv_mem[i], 0) = addr;
1705 }
1706 }
1707
1708#ifdef PRESERVE_DEATH_INFO_REGNO_P
1709 /* Make a pass over all the insns and remove death notes for things that
1710 are no longer registers or no longer die in the insn (e.g., an input
1711 and output pseudo being tied). */
1712
1713 for (insn = first; insn; insn = NEXT_INSN (insn))
1714 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1715 {
1716 rtx note, next;
1717
1718 for (note = REG_NOTES (insn); note; note = next)
1719 {
1720 next = XEXP (note, 1);
1721 if (REG_NOTE_KIND (note) == REG_DEAD
1722 && (GET_CODE (XEXP (note, 0)) != REG
1723 || reg_set_p (XEXP (note, 0), PATTERN (insn))))
1724 remove_note (insn, note);
1725 }
1726 }
1727#endif
1728
1729 /* Indicate that we no longer have known memory locations or constants. */
1730 reg_equiv_constant = 0;
1731 reg_equiv_memory_loc = 0;
1732}
1733\f
1734/* Nonzero if, after spilling reg REGNO for non-groups,
1735 it will still be possible to find a group if we still need one. */
1736
1737static int
1738possible_group_p (regno, max_groups)
1739 int regno;
1740 int *max_groups;
1741{
1742 int i;
1743 int class = (int) NO_REGS;
1744
1745 for (i = 0; i < (int) N_REG_CLASSES; i++)
1746 if (max_groups[i] > 0)
1747 {
1748 class = i;
1749 break;
1750 }
1751
1752 if (class == (int) NO_REGS)
1753 return 1;
1754
1755 /* Consider each pair of consecutive registers. */
1756 for (i = 0; i < FIRST_PSEUDO_REGISTER - 1; i++)
1757 {
1758 /* Ignore pairs that include reg REGNO. */
1759 if (i == regno || i + 1 == regno)
1760 continue;
1761
1762 /* Ignore pairs that are outside the class that needs the group.
1763 ??? Here we fail to handle the case where two different classes
1764 independently need groups. But this never happens with our
1765 current machine descriptions. */
1766 if (! (TEST_HARD_REG_BIT (reg_class_contents[class], i)
1767 && TEST_HARD_REG_BIT (reg_class_contents[class], i + 1)))
1768 continue;
1769
1770 /* A pair of consecutive regs we can still spill does the trick. */
1771 if (spill_reg_order[i] < 0 && spill_reg_order[i + 1] < 0
1772 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
1773 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1))
1774 return 1;
1775
1776 /* A pair of one already spilled and one we can spill does it
1777 provided the one already spilled is not otherwise reserved. */
1778 if (spill_reg_order[i] < 0
1779 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
1780 && spill_reg_order[i + 1] >= 0
1781 && ! TEST_HARD_REG_BIT (counted_for_groups, i + 1)
1782 && ! TEST_HARD_REG_BIT (counted_for_nongroups, i + 1))
1783 return 1;
1784 if (spill_reg_order[i + 1] < 0
1785 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1)
1786 && spill_reg_order[i] >= 0
1787 && ! TEST_HARD_REG_BIT (counted_for_groups, i)
1788 && ! TEST_HARD_REG_BIT (counted_for_nongroups, i))
1789 return 1;
1790 }
1791
1792 return 0;
1793}
1794\f
1795/* Count any groups that can be formed from the registers recently spilled.
1796 This is done class by class, in order of ascending class number. */
1797
1798static void
1799count_possible_groups (group_size, group_mode, max_groups)
1800 int *group_size, *max_groups;
1801 enum machine_mode *group_mode;
1802{
1803 int i;
1804 /* Now find all consecutive groups of spilled registers
1805 and mark each group off against the need for such groups.
1806 But don't count them against ordinary need, yet. */
1807
1808 for (i = 0; i < N_REG_CLASSES; i++)
1809 if (group_size[i] > 1)
1810 {
1811 char regmask[FIRST_PSEUDO_REGISTER];
1812 int j;
1813
1814 bzero (regmask, sizeof regmask);
1815 /* Make a mask of all the regs that are spill regs in class I. */
1816 for (j = 0; j < n_spills; j++)
1817 if (TEST_HARD_REG_BIT (reg_class_contents[i], spill_regs[j])
1818 && ! TEST_HARD_REG_BIT (counted_for_groups, spill_regs[j])
1819 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1820 spill_regs[j]))
1821 regmask[spill_regs[j]] = 1;
1822 /* Find each consecutive group of them. */
1823 for (j = 0; j < FIRST_PSEUDO_REGISTER && max_groups[i] > 0; j++)
1824 if (regmask[j] && j + group_size[i] <= FIRST_PSEUDO_REGISTER
1825 /* Next line in case group-mode for this class
1826 demands an even-odd pair. */
1827 && HARD_REGNO_MODE_OK (j, group_mode[i]))
1828 {
1829 int k;
1830 for (k = 1; k < group_size[i]; k++)
1831 if (! regmask[j + k])
1832 break;
1833 if (k == group_size[i])
1834 {
1835 /* We found a group. Mark it off against this class's
1836 need for groups, and against each superclass too. */
1837 register enum reg_class *p;
1838 max_groups[i]--;
1839 p = reg_class_superclasses[i];
1840 while (*p != LIM_REG_CLASSES)
1841 max_groups[(int) *p++]--;
1842 /* Don't count these registers again. */
1843 for (k = 0; k < group_size[i]; k++)
1844 SET_HARD_REG_BIT (counted_for_groups, j + k);
1845 }
1846 j += k;
1847 }
1848 }
1849
1850}
1851\f
1852/* ALLOCATE_MODE is a register mode that needs to be reloaded. OTHER_MODE is
1853 another mode that needs to be reloaded for the same register class CLASS.
1854 If any reg in CLASS allows ALLOCATE_MODE but not OTHER_MODE, fail.
1855 ALLOCATE_MODE will never be smaller than OTHER_MODE.
1856
1857 This code used to also fail if any reg in CLASS allows OTHER_MODE but not
1858 ALLOCATE_MODE. This test is unnecessary, because we will never try to put
1859 something of mode ALLOCATE_MODE into an OTHER_MODE register. Testing this
1860 causes unnecessary failures on machines requiring alignment of register
1861 groups when the two modes are different sizes, because the larger mode has
1862 more strict alignment rules than the smaller mode. */
1863
1864static int
1865modes_equiv_for_class_p (allocate_mode, other_mode, class)
1866 enum machine_mode allocate_mode, other_mode;
1867 enum reg_class class;
1868{
1869 register int regno;
1870 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1871 {
1872 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno)
1873 && HARD_REGNO_MODE_OK (regno, allocate_mode)
1874 && ! HARD_REGNO_MODE_OK (regno, other_mode))
1875 return 0;
1876 }
1877 return 1;
1878}
1879
1880/* Add a new register to the tables of available spill-registers
1881 (as well as spilling all pseudos allocated to the register).
1882 I is the index of this register in potential_reload_regs.
1883 CLASS is the regclass whose need is being satisfied.
1884 MAX_NEEDS and MAX_NONGROUPS are the vectors of needs,
1885 so that this register can count off against them.
1886 MAX_NONGROUPS is 0 if this register is part of a group.
1887 GLOBAL and DUMPFILE are the same as the args that `reload' got. */
1888
1889static int
1890new_spill_reg (i, class, max_needs, max_nongroups, global, dumpfile)
1891 int i;
1892 int class;
1893 int *max_needs;
1894 int *max_nongroups;
1895 int global;
1896 FILE *dumpfile;
1897{
1898 register enum reg_class *p;
1899 int val;
1900 int regno = potential_reload_regs[i];
1901
1902 if (i >= FIRST_PSEUDO_REGISTER)
1903 abort (); /* Caller failed to find any register. */
1904
1905 if (fixed_regs[regno] || TEST_HARD_REG_BIT (forbidden_regs, regno))
1906 fatal ("fixed or forbidden register was spilled.\n\
1907This may be due to a compiler bug or to impossible asm statements.");
1908
1909 /* Make reg REGNO an additional reload reg. */
1910
1911 potential_reload_regs[i] = -1;
1912 spill_regs[n_spills] = regno;
1913 spill_reg_order[regno] = n_spills;
1914 if (dumpfile)
1915 fprintf (dumpfile, "Spilling reg %d.\n", spill_regs[n_spills]);
1916
1917 /* Clear off the needs we just satisfied. */
1918
1919 max_needs[class]--;
1920 p = reg_class_superclasses[class];
1921 while (*p != LIM_REG_CLASSES)
1922 max_needs[(int) *p++]--;
1923
1924 if (max_nongroups && max_nongroups[class] > 0)
1925 {
1926 SET_HARD_REG_BIT (counted_for_nongroups, regno);
1927 max_nongroups[class]--;
1928 p = reg_class_superclasses[class];
1929 while (*p != LIM_REG_CLASSES)
1930 max_nongroups[(int) *p++]--;
1931 }
1932
1933 /* Spill every pseudo reg that was allocated to this reg
1934 or to something that overlaps this reg. */
1935
1936 val = spill_hard_reg (spill_regs[n_spills], global, dumpfile, 0);
1937
1938 /* If there are some registers still to eliminate and this register
1939 wasn't ever used before, additional stack space may have to be
1940 allocated to store this register. Thus, we may have changed the offset
1941 between the stack and frame pointers, so mark that something has changed.
1942 (If new pseudos were spilled, thus requiring more space, VAL would have
1943 been set non-zero by the call to spill_hard_reg above since additional
1944 reloads may be needed in that case.
1945
1946 One might think that we need only set VAL to 1 if this is a call-used
1947 register. However, the set of registers that must be saved by the
1948 prologue is not identical to the call-used set. For example, the
1949 register used by the call insn for the return PC is a call-used register,
1950 but must be saved by the prologue. */
1951 if (num_eliminable && ! regs_ever_live[spill_regs[n_spills]])
1952 val = 1;
1953
1954 regs_ever_live[spill_regs[n_spills]] = 1;
1955 n_spills++;
1956
1957 return val;
1958}
1959\f
1960/* Delete an unneeded INSN and any previous insns who sole purpose is loading
1961 data that is dead in INSN. */
1962
1963static void
1964delete_dead_insn (insn)
1965 rtx insn;
1966{
1967 rtx prev = prev_real_insn (insn);
1968 rtx prev_dest;
1969
1970 /* If the previous insn sets a register that dies in our insn, delete it
1971 too. */
1972 if (prev && GET_CODE (PATTERN (prev)) == SET
1973 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
1974 && reg_mentioned_p (prev_dest, PATTERN (insn))
1975 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest)))
1976 delete_dead_insn (prev);
1977
1978 PUT_CODE (insn, NOTE);
1979 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1980 NOTE_SOURCE_FILE (insn) = 0;
1981}
1982
1983/* Modify the home of pseudo-reg I.
1984 The new home is present in reg_renumber[I].
1985
1986 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
1987 or it may be -1, meaning there is none or it is not relevant.
1988 This is used so that all pseudos spilled from a given hard reg
1989 can share one stack slot. */
1990
1991static void
1992alter_reg (i, from_reg)
1993 register int i;
1994 int from_reg;
1995{
1996 /* When outputting an inline function, this can happen
1997 for a reg that isn't actually used. */
1998 if (regno_reg_rtx[i] == 0)
1999 return;
2000
2001 /* If the reg got changed to a MEM at rtl-generation time,
2002 ignore it. */
2003 if (GET_CODE (regno_reg_rtx[i]) != REG)
2004 return;
2005
2006 /* Modify the reg-rtx to contain the new hard reg
2007 number or else to contain its pseudo reg number. */
2008 REGNO (regno_reg_rtx[i])
2009 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
2010
2011 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2012 allocate a stack slot for it. */
2013
2014 if (reg_renumber[i] < 0
2015 && reg_n_refs[i] > 0
2016 && reg_equiv_constant[i] == 0
2017 && reg_equiv_memory_loc[i] == 0)
2018 {
2019 register rtx x;
2020 int inherent_size = PSEUDO_REGNO_BYTES (i);
2021 int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2022 int adjust = 0;
2023
2024 /* Each pseudo reg has an inherent size which comes from its own mode,
2025 and a total size which provides room for paradoxical subregs
2026 which refer to the pseudo reg in wider modes.
2027
2028 We can use a slot already allocated if it provides both
2029 enough inherent space and enough total space.
2030 Otherwise, we allocate a new slot, making sure that it has no less
2031 inherent space, and no less total space, then the previous slot. */
2032 if (from_reg == -1)
2033 {
2034 /* No known place to spill from => no slot to reuse. */
2035 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size, -1);
2036#if BYTES_BIG_ENDIAN
2037 /* Cancel the big-endian correction done in assign_stack_local.
2038 Get the address of the beginning of the slot.
2039 This is so we can do a big-endian correction unconditionally
2040 below. */
2041 adjust = inherent_size - total_size;
2042#endif
2043 }
2044 /* Reuse a stack slot if possible. */
2045 else if (spill_stack_slot[from_reg] != 0
2046 && spill_stack_slot_width[from_reg] >= total_size
2047 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2048 >= inherent_size))
2049 x = spill_stack_slot[from_reg];
2050 /* Allocate a bigger slot. */
2051 else
2052 {
2053 /* Compute maximum size needed, both for inherent size
2054 and for total size. */
2055 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2056 if (spill_stack_slot[from_reg])
2057 {
2058 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2059 > inherent_size)
2060 mode = GET_MODE (spill_stack_slot[from_reg]);
2061 if (spill_stack_slot_width[from_reg] > total_size)
2062 total_size = spill_stack_slot_width[from_reg];
2063 }
2064 /* Make a slot with that size. */
2065 x = assign_stack_local (mode, total_size, -1);
2066#if BYTES_BIG_ENDIAN
2067 /* Cancel the big-endian correction done in assign_stack_local.
2068 Get the address of the beginning of the slot.
2069 This is so we can do a big-endian correction unconditionally
2070 below. */
2071 adjust = GET_MODE_SIZE (mode) - total_size;
2072#endif
2073 spill_stack_slot[from_reg] = x;
2074 spill_stack_slot_width[from_reg] = total_size;
2075 }
2076
2077#if BYTES_BIG_ENDIAN
2078 /* On a big endian machine, the "address" of the slot
2079 is the address of the low part that fits its inherent mode. */
2080 if (inherent_size < total_size)
2081 adjust += (total_size - inherent_size);
2082#endif /* BYTES_BIG_ENDIAN */
2083
2084 /* If we have any adjustment to make, or if the stack slot is the
2085 wrong mode, make a new stack slot. */
2086 if (adjust != 0 || GET_MODE (x) != GET_MODE (regno_reg_rtx[i]))
2087 {
2088 x = gen_rtx (MEM, GET_MODE (regno_reg_rtx[i]),
2089 plus_constant (XEXP (x, 0), adjust));
2090 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2091 }
2092
2093 /* Save the stack slot for later. */
2094 reg_equiv_memory_loc[i] = x;
2095 }
2096}
2097
2098/* Mark the slots in regs_ever_live for the hard regs
2099 used by pseudo-reg number REGNO. */
2100
2101void
2102mark_home_live (regno)
2103 int regno;
2104{
2105 register int i, lim;
2106 i = reg_renumber[regno];
2107 if (i < 0)
2108 return;
2109 lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2110 while (i < lim)
2111 regs_ever_live[i++] = 1;
2112}
2113\f
2114/* This function handles the tracking of elimination offsets around branches.
2115
2116 X is a piece of RTL being scanned.
2117
2118 INSN is the insn that it came from, if any.
2119
2120 INITIAL_P is non-zero if we are to set the offset to be the initial
2121 offset and zero if we are setting the offset of the label to be the
2122 current offset. */
2123
2124static void
2125set_label_offsets (x, insn, initial_p)
2126 rtx x;
2127 rtx insn;
2128 int initial_p;
2129{
2130 enum rtx_code code = GET_CODE (x);
2131 rtx tem;
2132 int i;
2133 struct elim_table *p;
2134
2135 switch (code)
2136 {
2137 case LABEL_REF:
2138 x = XEXP (x, 0);
2139
2140 /* ... fall through ... */
2141
2142 case CODE_LABEL:
2143 /* If we know nothing about this label, set the desired offsets. Note
2144 that this sets the offset at a label to be the offset before a label
2145 if we don't know anything about the label. This is not correct for
2146 the label after a BARRIER, but is the best guess we can make. If
2147 we guessed wrong, we will suppress an elimination that might have
2148 been possible had we been able to guess correctly. */
2149
2150 if (! offsets_known_at[CODE_LABEL_NUMBER (x)])
2151 {
2152 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2153 offsets_at[CODE_LABEL_NUMBER (x)][i]
2154 = (initial_p ? reg_eliminate[i].initial_offset
2155 : reg_eliminate[i].offset);
2156 offsets_known_at[CODE_LABEL_NUMBER (x)] = 1;
2157 }
2158
2159 /* Otherwise, if this is the definition of a label and it is
2160 preceeded by a BARRIER, set our offsets to the known offset of
2161 that label. */
2162
2163 else if (x == insn
2164 && (tem = prev_nonnote_insn (insn)) != 0
2165 && GET_CODE (tem) == BARRIER)
2166 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2167 reg_eliminate[i].offset = reg_eliminate[i].previous_offset
2168 = offsets_at[CODE_LABEL_NUMBER (x)][i];
2169
2170 else
2171 /* If neither of the above cases is true, compare each offset
2172 with those previously recorded and suppress any eliminations
2173 where the offsets disagree. */
2174
2175 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2176 if (offsets_at[CODE_LABEL_NUMBER (x)][i]
2177 != (initial_p ? reg_eliminate[i].initial_offset
2178 : reg_eliminate[i].offset))
2179 reg_eliminate[i].can_eliminate = 0;
2180
2181 return;
2182
2183 case JUMP_INSN:
2184 set_label_offsets (PATTERN (insn), insn, initial_p);
2185
2186 /* ... fall through ... */
2187
2188 case INSN:
2189 case CALL_INSN:
2190 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2191 and hence must have all eliminations at their initial offsets. */
2192 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2193 if (REG_NOTE_KIND (tem) == REG_LABEL)
2194 set_label_offsets (XEXP (tem, 0), insn, 1);
2195 return;
2196
2197 case ADDR_VEC:
2198 case ADDR_DIFF_VEC:
2199 /* Each of the labels in the address vector must be at their initial
2200 offsets. We want the first first for ADDR_VEC and the second
2201 field for ADDR_DIFF_VEC. */
2202
2203 for (i = 0; i < XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2204 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2205 insn, initial_p);
2206 return;
2207
2208 case SET:
2209 /* We only care about setting PC. If the source is not RETURN,
2210 IF_THEN_ELSE, or a label, disable any eliminations not at
2211 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2212 isn't one of those possibilities. For branches to a label,
2213 call ourselves recursively.
2214
2215 Note that this can disable elimination unnecessarily when we have
2216 a non-local goto since it will look like a non-constant jump to
2217 someplace in the current function. This isn't a significant
2218 problem since such jumps will normally be when all elimination
2219 pairs are back to their initial offsets. */
2220
2221 if (SET_DEST (x) != pc_rtx)
2222 return;
2223
2224 switch (GET_CODE (SET_SRC (x)))
2225 {
2226 case PC:
2227 case RETURN:
2228 return;
2229
2230 case LABEL_REF:
2231 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2232 return;
2233
2234 case IF_THEN_ELSE:
2235 tem = XEXP (SET_SRC (x), 1);
2236 if (GET_CODE (tem) == LABEL_REF)
2237 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2238 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2239 break;
2240
2241 tem = XEXP (SET_SRC (x), 2);
2242 if (GET_CODE (tem) == LABEL_REF)
2243 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2244 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2245 break;
2246 return;
2247 }
2248
2249 /* If we reach here, all eliminations must be at their initial
2250 offset because we are doing a jump to a variable address. */
2251 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2252 if (p->offset != p->initial_offset)
2253 p->can_eliminate = 0;
2254 }
2255}
2256\f
2257/* Used for communication between the next two function to properly share
2258 the vector for an ASM_OPERANDS. */
2259
2260static struct rtvec_def *old_asm_operands_vec, *new_asm_operands_vec;
2261
2262/* Scan X and replace any eliminable registers (such as fp) with a
2263 replacement (such as sp), plus an offset.
2264
2265 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2266 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2267 MEM, we are allowed to replace a sum of a register and the constant zero
2268 with the register, which we cannot do outside a MEM. In addition, we need
2269 to record the fact that a register is referenced outside a MEM.
2270
2271 If INSN is nonzero, it is the insn containing X. If we replace a REG
2272 in a SET_DEST with an equivalent MEM and INSN is non-zero, write a
2273 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2274 that the REG is being modified.
2275
2276 If we see a modification to a register we know about, take the
2277 appropriate action (see case SET, below).
2278
2279 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2280 replacements done assuming all offsets are at their initial values. If
2281 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2282 encounter, return the actual location so that find_reloads will do
2283 the proper thing. */
2284
2285rtx
2286eliminate_regs (x, mem_mode, insn)
2287 rtx x;
2288 enum machine_mode mem_mode;
2289 rtx insn;
2290{
2291 enum rtx_code code = GET_CODE (x);
2292 struct elim_table *ep;
2293 int regno;
2294 rtx new;
2295 int i, j;
2296 char *fmt;
2297 int copied = 0;
2298
2299 switch (code)
2300 {
2301 case CONST_INT:
2302 case CONST_DOUBLE:
2303 case CONST:
2304 case SYMBOL_REF:
2305 case CODE_LABEL:
2306 case PC:
2307 case CC0:
2308 case ASM_INPUT:
2309 case ADDR_VEC:
2310 case ADDR_DIFF_VEC:
2311 case RETURN:
2312 return x;
2313
2314 case REG:
2315 regno = REGNO (x);
2316
2317 /* First handle the case where we encounter a bare register that
2318 is eliminable. Replace it with a PLUS. */
2319 if (regno < FIRST_PSEUDO_REGISTER)
2320 {
2321 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2322 ep++)
2323 if (ep->from_rtx == x && ep->can_eliminate)
2324 {
2325 if (! mem_mode)
2326 ep->ref_outside_mem = 1;
2327 return plus_constant (ep->to_rtx, ep->previous_offset);
2328 }
2329
2330 }
2331 else if (reg_equiv_memory_loc && reg_equiv_memory_loc[regno]
2332 && (reg_equiv_address[regno] || num_not_at_initial_offset))
2333 {
2334 /* In this case, find_reloads would attempt to either use an
2335 incorrect address (if something is not at its initial offset)
2336 or substitute an replaced address into an insn (which loses
2337 if the offset is changed by some later action). So we simply
2338 return the replaced stack slot (assuming it is changed by
2339 elimination) and ignore the fact that this is actually a
2340 reference to the pseudo. Ensure we make a copy of the
2341 address in case it is shared. */
2342 new = eliminate_regs (reg_equiv_memory_loc[regno], mem_mode, 0);
2343 if (new != reg_equiv_memory_loc[regno])
2344 return copy_rtx (new);
2345 }
2346 return x;
2347
2348 case PLUS:
2349 /* If this is the sum of an eliminable register and a constant, rework
2350 the sum. */
2351 if (GET_CODE (XEXP (x, 0)) == REG
2352 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2353 && CONSTANT_P (XEXP (x, 1)))
2354 {
2355 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2356 ep++)
2357 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2358 {
2359 if (! mem_mode)
2360 ep->ref_outside_mem = 1;
2361
2362 /* The only time we want to replace a PLUS with a REG (this
2363 occurs when the constant operand of the PLUS is the negative
2364 of the offset) is when we are inside a MEM. We won't want
2365 to do so at other times because that would change the
2366 structure of the insn in a way that reload can't handle.
2367 We special-case the commonest situation in
2368 eliminate_regs_in_insn, so just replace a PLUS with a
2369 PLUS here, unless inside a MEM. */
2370 if (mem_mode && GET_CODE (XEXP (x, 1)) == CONST_INT
2371 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2372 return ep->to_rtx;
2373 else
2374 return gen_rtx (PLUS, Pmode, ep->to_rtx,
2375 plus_constant (XEXP (x, 1),
2376 ep->previous_offset));
2377 }
2378
2379 /* If the register is not eliminable, we are done since the other
2380 operand is a constant. */
2381 return x;
2382 }
2383
2384 /* If this is part of an address, we want to bring any constant to the
2385 outermost PLUS. We will do this by doing register replacement in
2386 our operands and seeing if a constant shows up in one of them.
2387
2388 We assume here this is part of an address (or a "load address" insn)
2389 since an eliminable register is not likely to appear in any other
2390 context.
2391
2392 If we have (plus (eliminable) (reg)), we want to produce
2393 (plus (plus (replacement) (reg) (const))). If this was part of a
2394 normal add insn, (plus (replacement) (reg)) will be pushed as a
2395 reload. This is the desired action. */
2396
2397 {
2398 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, 0);
2399 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, 0);
2400
2401 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2402 {
2403 /* If one side is a PLUS and the other side is a pseudo that
2404 didn't get a hard register but has a reg_equiv_constant,
2405 we must replace the constant here since it may no longer
2406 be in the position of any operand. */
2407 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2408 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2409 && reg_renumber[REGNO (new1)] < 0
2410 && reg_equiv_constant != 0
2411 && reg_equiv_constant[REGNO (new1)] != 0)
2412 new1 = reg_equiv_constant[REGNO (new1)];
2413 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2414 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2415 && reg_renumber[REGNO (new0)] < 0
2416 && reg_equiv_constant[REGNO (new0)] != 0)
2417 new0 = reg_equiv_constant[REGNO (new0)];
2418
2419 new = form_sum (new0, new1);
2420
2421 /* As above, if we are not inside a MEM we do not want to
2422 turn a PLUS into something else. We might try to do so here
2423 for an addition of 0 if we aren't optimizing. */
2424 if (! mem_mode && GET_CODE (new) != PLUS)
2425 return gen_rtx (PLUS, GET_MODE (x), new, const0_rtx);
2426 else
2427 return new;
2428 }
2429 }
2430 return x;
2431
2432 case EXPR_LIST:
2433 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2434 if (XEXP (x, 0))
2435 {
2436 new = eliminate_regs (XEXP (x, 0), mem_mode, 0);
2437 if (new != XEXP (x, 0))
2438 x = gen_rtx (EXPR_LIST, REG_NOTE_KIND (x), new, XEXP (x, 1));
2439 }
2440
2441 /* ... fall through ... */
2442
2443 case INSN_LIST:
2444 /* Now do eliminations in the rest of the chain. If this was
2445 an EXPR_LIST, this might result in allocating more memory than is
2446 strictly needed, but it simplifies the code. */
2447 if (XEXP (x, 1))
2448 {
2449 new = eliminate_regs (XEXP (x, 1), mem_mode, 0);
2450 if (new != XEXP (x, 1))
2451 return gen_rtx (INSN_LIST, GET_MODE (x), XEXP (x, 0), new);
2452 }
2453 return x;
2454
2455 case CALL:
2456 case COMPARE:
2457 case MINUS:
2458 case MULT:
2459 case DIV: case UDIV:
2460 case MOD: case UMOD:
2461 case AND: case IOR: case XOR:
2462 case LSHIFT: case ASHIFT: case ROTATE:
2463 case ASHIFTRT: case LSHIFTRT: case ROTATERT:
2464 case NE: case EQ:
2465 case GE: case GT: case GEU: case GTU:
2466 case LE: case LT: case LEU: case LTU:
2467 {
2468 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, 0);
2469 rtx new1 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, 0) : 0;
2470
2471 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2472 return gen_rtx (code, GET_MODE (x), new0, new1);
2473 }
2474 return x;
2475
2476 case PRE_INC:
2477 case POST_INC:
2478 case PRE_DEC:
2479 case POST_DEC:
2480 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2481 if (ep->to_rtx == XEXP (x, 0))
2482 {
2483 if (code == PRE_DEC || code == POST_DEC)
2484 ep->offset += GET_MODE_SIZE (mem_mode);
2485 else
2486 ep->offset -= GET_MODE_SIZE (mem_mode);
2487 }
2488
2489 /* Fall through to generic unary operation case. */
2490 case USE:
2491 case STRICT_LOW_PART:
2492 case NEG: case NOT:
2493 case SIGN_EXTEND: case ZERO_EXTEND:
2494 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2495 case FLOAT: case FIX:
2496 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2497 case ABS:
2498 case SQRT:
2499 case FFS:
2500 new = eliminate_regs (XEXP (x, 0), mem_mode, 0);
2501 if (new != XEXP (x, 0))
2502 return gen_rtx (code, GET_MODE (x), new);
2503 return x;
2504
2505 case SUBREG:
2506 /* Similar to above processing, but preserve SUBREG_WORD.
2507 Convert (subreg (mem)) to (mem) if not paradoxical.
2508 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2509 pseudo didn't get a hard reg, we must replace this with the
2510 eliminated version of the memory location because push_reloads
2511 may do the replacement in certain circumstances. */
2512 if (GET_CODE (SUBREG_REG (x)) == REG
2513 && (GET_MODE_SIZE (GET_MODE (x))
2514 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2515 && reg_equiv_memory_loc != 0
2516 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2517 {
2518 new = eliminate_regs (reg_equiv_memory_loc[REGNO (SUBREG_REG (x))],
2519 mem_mode, 0);
2520
2521 /* If we didn't change anything, we must retain the pseudo. */
2522 if (new == reg_equiv_memory_loc[REGNO (SUBREG_REG (x))])
2523 new = XEXP (x, 0);
2524 else
2525 /* Otherwise, ensure NEW isn't shared in case we have to reload
2526 it. */
2527 new = copy_rtx (new);
2528 }
2529 else
2530 new = eliminate_regs (SUBREG_REG (x), mem_mode, 0);
2531
2532 if (new != XEXP (x, 0))
2533 {
2534 if (GET_CODE (new) == MEM
2535 && (GET_MODE_SIZE (GET_MODE (x))
2536 <= GET_MODE_SIZE (GET_MODE (new))))
2537 {
2538 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
2539 enum machine_mode mode = GET_MODE (x);
2540
2541#if BYTES_BIG_ENDIAN
2542 offset += (MIN (UNITS_PER_WORD,
2543 GET_MODE_SIZE (GET_MODE (new)))
2544 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
2545#endif
2546
2547 PUT_MODE (new, mode);
2548 XEXP (new, 0) = plus_constant (XEXP (new, 0), offset);
2549 return new;
2550 }
2551 else
2552 return gen_rtx (SUBREG, GET_MODE (x), new, SUBREG_WORD (x));
2553 }
2554
2555 return x;
2556
2557 case CLOBBER:
2558 /* If clobbering a register that is the replacement register for an
2559 elimination we still think can be peformed, note that it cannot
2560 be performed. Otherwise, we need not be concerned about it. */
2561 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2562 if (ep->to_rtx == XEXP (x, 0))
2563 ep->can_eliminate = 0;
2564
2565 return x;
2566
2567 case ASM_OPERANDS:
2568 {
2569 rtx *temp_vec;
2570 /* Properly handle sharing input and constraint vectors. */
2571 if (ASM_OPERANDS_INPUT_VEC (x) != old_asm_operands_vec)
2572 {
2573 /* When we come to a new vector not seen before,
2574 scan all its elements; keep the old vector if none
2575 of them changes; otherwise, make a copy. */
2576 old_asm_operands_vec = ASM_OPERANDS_INPUT_VEC (x);
2577 temp_vec = (rtx *) alloca (XVECLEN (x, 3) * sizeof (rtx));
2578 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
2579 temp_vec[i] = eliminate_regs (ASM_OPERANDS_INPUT (x, i),
2580 mem_mode, 0);
2581
2582 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
2583 if (temp_vec[i] != ASM_OPERANDS_INPUT (x, i))
2584 break;
2585
2586 if (i == ASM_OPERANDS_INPUT_LENGTH (x))
2587 new_asm_operands_vec = old_asm_operands_vec;
2588 else
2589 new_asm_operands_vec
2590 = gen_rtvec_v (ASM_OPERANDS_INPUT_LENGTH (x), temp_vec);
2591 }
2592
2593 /* If we had to copy the vector, copy the entire ASM_OPERANDS. */
2594 if (new_asm_operands_vec == old_asm_operands_vec)
2595 return x;
2596
2597 new = gen_rtx (ASM_OPERANDS, VOIDmode, ASM_OPERANDS_TEMPLATE (x),
2598 ASM_OPERANDS_OUTPUT_CONSTRAINT (x),
2599 ASM_OPERANDS_OUTPUT_IDX (x), new_asm_operands_vec,
2600 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (x),
2601 ASM_OPERANDS_SOURCE_FILE (x),
2602 ASM_OPERANDS_SOURCE_LINE (x));
2603 new->volatil = x->volatil;
2604 return new;
2605 }
2606
2607 case SET:
2608 /* Check for setting a register that we know about. */
2609 if (GET_CODE (SET_DEST (x)) == REG)
2610 {
2611 /* See if this is setting the replacement register for an
2612 elimination.
2613
2614 If DEST is the frame pointer, we do nothing because we assume that
2615 all assignments to the frame pointer are for non-local gotos and
2616 are being done at a time when they are valid and do not disturb
2617 anything else. Some machines want to eliminate a fake argument
2618 pointer with either the frame or stack pointer. Assignments to
2619 the frame pointer must not prevent this elimination. */
2620
2621 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2622 ep++)
2623 if (ep->to_rtx == SET_DEST (x)
2624 && SET_DEST (x) != frame_pointer_rtx)
2625 {
2626 /* If it is being incrememented, adjust the offset. Otherwise,
2627 this elimination can't be done. */
2628 rtx src = SET_SRC (x);
2629
2630 if (GET_CODE (src) == PLUS
2631 && XEXP (src, 0) == SET_DEST (x)
2632 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2633 ep->offset -= INTVAL (XEXP (src, 1));
2634 else
2635 ep->can_eliminate = 0;
2636 }
2637
2638 /* Now check to see we are assigning to a register that can be
2639 eliminated. If so, it must be as part of a PARALLEL, since we
2640 will not have been called if this is a single SET. So indicate
2641 that we can no longer eliminate this reg. */
2642 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2643 ep++)
2644 if (ep->from_rtx == SET_DEST (x) && ep->can_eliminate)
2645 ep->can_eliminate = 0;
2646 }
2647
2648 /* Now avoid the loop below in this common case. */
2649 {
2650 rtx new0 = eliminate_regs (SET_DEST (x), 0, 0);
2651 rtx new1 = eliminate_regs (SET_SRC (x), 0, 0);
2652
2653 /* If SET_DEST changed from a REG to a MEM and INSN is non-zero,
2654 write a CLOBBER insn. */
2655 if (GET_CODE (SET_DEST (x)) == REG && GET_CODE (new0) == MEM
2656 && insn != 0)
2657 emit_insn_after (gen_rtx (CLOBBER, VOIDmode, SET_DEST (x)), insn);
2658
2659 if (new0 != SET_DEST (x) || new1 != SET_SRC (x))
2660 return gen_rtx (SET, VOIDmode, new0, new1);
2661 }
2662
2663 return x;
2664
2665 case MEM:
2666 /* Our only special processing is to pass the mode of the MEM to our
2667 recursive call and copy the flags. While we are here, handle this
2668 case more efficiently. */
2669 new = eliminate_regs (XEXP (x, 0), GET_MODE (x), 0);
2670 if (new != XEXP (x, 0))
2671 {
2672 new = gen_rtx (MEM, GET_MODE (x), new);
2673 new->volatil = x->volatil;
2674 new->unchanging = x->unchanging;
2675 new->in_struct = x->in_struct;
2676 return new;
2677 }
2678 else
2679 return x;
2680 }
2681
2682 /* Process each of our operands recursively. If any have changed, make a
2683 copy of the rtx. */
2684 fmt = GET_RTX_FORMAT (code);
2685 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2686 {
2687 if (*fmt == 'e')
2688 {
2689 new = eliminate_regs (XEXP (x, i), mem_mode, 0);
2690 if (new != XEXP (x, i) && ! copied)
2691 {
2692 rtx new_x = rtx_alloc (code);
2693 bcopy (x, new_x, (sizeof (*new_x) - sizeof (new_x->fld)
2694 + (sizeof (new_x->fld[0])
2695 * GET_RTX_LENGTH (code))));
2696 x = new_x;
2697 copied = 1;
2698 }
2699 XEXP (x, i) = new;
2700 }
2701 else if (*fmt == 'E')
2702 {
2703 int copied_vec = 0;
2704 for (j = 0; j < XVECLEN (x, i); j++)
2705 {
2706 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
2707 if (new != XVECEXP (x, i, j) && ! copied_vec)
2708 {
2709 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2710 &XVECEXP (x, i, 0));
2711 if (! copied)
2712 {
2713 rtx new_x = rtx_alloc (code);
2714 bcopy (x, new_x, (sizeof (*new_x) - sizeof (new_x->fld)
2715 + (sizeof (new_x->fld[0])
2716 * GET_RTX_LENGTH (code))));
2717 x = new_x;
2718 copied = 1;
2719 }
2720 XVEC (x, i) = new_v;
2721 copied_vec = 1;
2722 }
2723 XVECEXP (x, i, j) = new;
2724 }
2725 }
2726 }
2727
2728 return x;
2729}
2730\f
2731/* Scan INSN and eliminate all eliminable registers in it.
2732
2733 If REPLACE is nonzero, do the replacement destructively. Also
2734 delete the insn as dead it if it is setting an eliminable register.
2735
2736 If REPLACE is zero, do all our allocations in reload_obstack.
2737
2738 If no eliminations were done and this insn doesn't require any elimination
2739 processing (these are not identical conditions: it might be updating sp,
2740 but not referencing fp; this needs to be seen during reload_as_needed so
2741 that the offset between fp and sp can be taken into consideration), zero
2742 is returned. Otherwise, 1 is returned. */
2743
2744static int
2745eliminate_regs_in_insn (insn, replace)
2746 rtx insn;
2747 int replace;
2748{
2749 rtx old_body = PATTERN (insn);
2750 rtx new_body;
2751 int val = 0;
2752 struct elim_table *ep;
2753
2754 if (! replace)
2755 push_obstacks (&reload_obstack, &reload_obstack);
2756
2757 if (GET_CODE (old_body) == SET && GET_CODE (SET_DEST (old_body)) == REG
2758 && REGNO (SET_DEST (old_body)) < FIRST_PSEUDO_REGISTER)
2759 {
2760 /* Check for setting an eliminable register. */
2761 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2762 if (ep->from_rtx == SET_DEST (old_body) && ep->can_eliminate)
2763 {
2764 /* In this case this insn isn't serving a useful purpose. We
2765 will delete it in reload_as_needed once we know that this
2766 elimination is, in fact, being done.
2767
2768 If REPLACE isn't set, we can't delete this insn, but neededn't
2769 process it since it won't be used unless something changes. */
2770 if (replace)
2771 delete_dead_insn (insn);
2772 val = 1;
2773 goto done;
2774 }
2775
2776 /* Check for (set (reg) (plus (reg from) (offset))) where the offset
2777 in the insn is the negative of the offset in FROM. Substitute
2778 (set (reg) (reg to)) for the insn and change its code.
2779
2780 We have to do this here, rather than in eliminate_regs, do that we can
2781 change the insn code. */
2782
2783 if (GET_CODE (SET_SRC (old_body)) == PLUS
2784 && GET_CODE (XEXP (SET_SRC (old_body), 0)) == REG
2785 && GET_CODE (XEXP (SET_SRC (old_body), 1)) == CONST_INT)
2786 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2787 ep++)
2788 if (ep->from_rtx == XEXP (SET_SRC (old_body), 0)
2789 && ep->can_eliminate
2790 && ep->offset == - INTVAL (XEXP (SET_SRC (old_body), 1)))
2791 {
2792 PATTERN (insn) = gen_rtx (SET, VOIDmode,
2793 SET_DEST (old_body), ep->to_rtx);
2794 INSN_CODE (insn) = -1;
2795 val = 1;
2796 goto done;
2797 }
2798 }
2799
2800 old_asm_operands_vec = 0;
2801
2802 /* Replace the body of this insn with a substituted form. If we changed
2803 something, return non-zero. If this is the final call for this
2804 insn (REPLACE is non-zero), do the elimination in REG_NOTES as well.
2805
2806 If we are replacing a body that was a (set X (plus Y Z)), try to
2807 re-recognize the insn. We do this in case we had a simple addition
2808 but now can do this as a load-address. This saves an insn in this
2809 common case. */
2810
2811 new_body = eliminate_regs (old_body, 0, replace ? insn : 0);
2812 if (new_body != old_body)
2813 {
2814 if (GET_CODE (old_body) != SET || GET_CODE (SET_SRC (old_body)) != PLUS
2815 || ! validate_change (insn, &PATTERN (insn), new_body, 0))
2816 PATTERN (insn) = new_body;
2817
2818 if (replace && REG_NOTES (insn))
2819 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, 0);
2820 val = 1;
2821 }
2822
2823 /* Loop through all elimination pairs. See if any have changed and
2824 recalculate the number not at initial offset.
2825
a8efe40d
RK
2826 Compute the maximum offset (minimum offset if the stack does not
2827 grow downward) for each elimination pair.
2828
32131a9c
RK
2829 We also detect a cases where register elimination cannot be done,
2830 namely, if a register would be both changed and referenced outside a MEM
2831 in the resulting insn since such an insn is often undefined and, even if
2832 not, we cannot know what meaning will be given to it. Note that it is
2833 valid to have a register used in an address in an insn that changes it
2834 (presumably with a pre- or post-increment or decrement).
2835
2836 If anything changes, return nonzero. */
2837
2838 num_not_at_initial_offset = 0;
2839 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2840 {
2841 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
2842 ep->can_eliminate = 0;
2843
2844 ep->ref_outside_mem = 0;
2845
2846 if (ep->previous_offset != ep->offset)
2847 val = 1;
2848
2849 ep->previous_offset = ep->offset;
2850 if (ep->can_eliminate && ep->offset != ep->initial_offset)
2851 num_not_at_initial_offset++;
a8efe40d
RK
2852
2853#ifdef STACK_GROWS_DOWNWARD
2854 ep->max_offset = MAX (ep->max_offset, ep->offset);
2855#else
2856 ep->max_offset = MIN (ep->max_offset, ep->offset);
2857#endif
32131a9c
RK
2858 }
2859
2860 done:
2861 if (! replace)
2862 pop_obstacks ();
2863
2864 return val;
2865}
2866
2867/* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
2868 replacement we currently believe is valid, mark it as not eliminable if X
2869 modifies DEST in any way other than by adding a constant integer to it.
2870
2871 If DEST is the frame pointer, we do nothing because we assume that
2872 all assignments to the frame pointer are nonlocal gotos and are being done
2873 at a time when they are valid and do not disturb anything else.
2874 Some machines want to eliminate a fake argument pointer with either the
2875 frame or stack pointer. Assignments to the frame pointer must not prevent
2876 this elimination.
2877
2878 Called via note_stores from reload before starting its passes to scan
2879 the insns of the function. */
2880
2881static void
2882mark_not_eliminable (dest, x)
2883 rtx dest;
2884 rtx x;
2885{
2886 register int i;
2887
2888 /* A SUBREG of a hard register here is just changing its mode. We should
2889 not see a SUBREG of an eliminable hard register, but check just in
2890 case. */
2891 if (GET_CODE (dest) == SUBREG)
2892 dest = SUBREG_REG (dest);
2893
2894 if (dest == frame_pointer_rtx)
2895 return;
2896
2897 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2898 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
2899 && (GET_CODE (x) != SET
2900 || GET_CODE (SET_SRC (x)) != PLUS
2901 || XEXP (SET_SRC (x), 0) != dest
2902 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
2903 {
2904 reg_eliminate[i].can_eliminate_previous
2905 = reg_eliminate[i].can_eliminate = 0;
2906 num_eliminable--;
2907 }
2908}
2909\f
2910/* Kick all pseudos out of hard register REGNO.
2911 If GLOBAL is nonzero, try to find someplace else to put them.
2912 If DUMPFILE is nonzero, log actions taken on that file.
2913
2914 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
2915 because we found we can't eliminate some register. In the case, no pseudos
2916 are allowed to be in the register, even if they are only in a block that
2917 doesn't require spill registers, unlike the case when we are spilling this
2918 hard reg to produce another spill register.
2919
2920 Return nonzero if any pseudos needed to be kicked out. */
2921
2922static int
2923spill_hard_reg (regno, global, dumpfile, cant_eliminate)
2924 register int regno;
2925 int global;
2926 FILE *dumpfile;
2927 int cant_eliminate;
2928{
2929 int something_changed = 0;
2930 register int i;
2931
2932 SET_HARD_REG_BIT (forbidden_regs, regno);
2933
2934 /* Spill every pseudo reg that was allocated to this reg
2935 or to something that overlaps this reg. */
2936
2937 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2938 if (reg_renumber[i] >= 0
2939 && reg_renumber[i] <= regno
2940 && (reg_renumber[i]
2941 + HARD_REGNO_NREGS (reg_renumber[i],
2942 PSEUDO_REGNO_MODE (i))
2943 > regno))
2944 {
2945 enum reg_class class = REGNO_REG_CLASS (regno);
2946
2947 /* If this register belongs solely to a basic block which needed no
2948 spilling of any class that this register is contained in,
2949 leave it be, unless we are spilling this register because
2950 it was a hard register that can't be eliminated. */
2951
2952 if (! cant_eliminate
2953 && basic_block_needs[0]
2954 && reg_basic_block[i] >= 0
2955 && basic_block_needs[(int) class][reg_basic_block[i]] == 0)
2956 {
2957 enum reg_class *p;
2958
2959 for (p = reg_class_superclasses[(int) class];
2960 *p != LIM_REG_CLASSES; p++)
2961 if (basic_block_needs[(int) *p][reg_basic_block[i]] > 0)
2962 break;
2963
2964 if (*p == LIM_REG_CLASSES)
2965 continue;
2966 }
2967
2968 /* Mark it as no longer having a hard register home. */
2969 reg_renumber[i] = -1;
2970 /* We will need to scan everything again. */
2971 something_changed = 1;
2972 if (global)
2973 retry_global_alloc (i, forbidden_regs);
2974
2975 alter_reg (i, regno);
2976 if (dumpfile)
2977 {
2978 if (reg_renumber[i] == -1)
2979 fprintf (dumpfile, " Register %d now on stack.\n\n", i);
2980 else
2981 fprintf (dumpfile, " Register %d now in %d.\n\n",
2982 i, reg_renumber[i]);
2983 }
2984 }
2985
2986 return something_changed;
2987}
2988\f
2989/* Find all paradoxical subregs within X and update reg_max_ref_width. */
2990
2991static void
2992scan_paradoxical_subregs (x)
2993 register rtx x;
2994{
2995 register int i;
2996 register char *fmt;
2997 register enum rtx_code code = GET_CODE (x);
2998
2999 switch (code)
3000 {
3001 case CONST_INT:
3002 case CONST:
3003 case SYMBOL_REF:
3004 case LABEL_REF:
3005 case CONST_DOUBLE:
3006 case CC0:
3007 case PC:
3008 case REG:
3009 case USE:
3010 case CLOBBER:
3011 return;
3012
3013 case SUBREG:
3014 if (GET_CODE (SUBREG_REG (x)) == REG
3015 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3016 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3017 = GET_MODE_SIZE (GET_MODE (x));
3018 return;
3019 }
3020
3021 fmt = GET_RTX_FORMAT (code);
3022 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3023 {
3024 if (fmt[i] == 'e')
3025 scan_paradoxical_subregs (XEXP (x, i));
3026 else if (fmt[i] == 'E')
3027 {
3028 register int j;
3029 for (j = XVECLEN (x, i) - 1; j >=0; j--)
3030 scan_paradoxical_subregs (XVECEXP (x, i, j));
3031 }
3032 }
3033}
3034\f
3035struct hard_reg_n_uses { int regno; int uses; };
3036
3037static int
3038hard_reg_use_compare (p1, p2)
3039 struct hard_reg_n_uses *p1, *p2;
3040{
3041 int tem = p1->uses - p2->uses;
3042 if (tem != 0) return tem;
3043 /* If regs are equally good, sort by regno,
3044 so that the results of qsort leave nothing to chance. */
3045 return p1->regno - p2->regno;
3046}
3047
3048/* Choose the order to consider regs for use as reload registers
3049 based on how much trouble would be caused by spilling one.
3050 Store them in order of decreasing preference in potential_reload_regs. */
3051
3052static void
3053order_regs_for_reload ()
3054{
3055 register int i;
3056 register int o = 0;
3057 int large = 0;
3058
3059 struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
3060
3061 CLEAR_HARD_REG_SET (bad_spill_regs);
3062
3063 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3064 potential_reload_regs[i] = -1;
3065
3066 /* Count number of uses of each hard reg by pseudo regs allocated to it
3067 and then order them by decreasing use. */
3068
3069 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3070 {
3071 hard_reg_n_uses[i].uses = 0;
3072 hard_reg_n_uses[i].regno = i;
3073 }
3074
3075 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3076 {
3077 int regno = reg_renumber[i];
3078 if (regno >= 0)
3079 {
3080 int lim = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
3081 while (regno < lim)
3082 hard_reg_n_uses[regno++].uses += reg_n_refs[i];
3083 }
3084 large += reg_n_refs[i];
3085 }
3086
3087 /* Now fixed registers (which cannot safely be used for reloading)
3088 get a very high use count so they will be considered least desirable.
3089 Registers used explicitly in the rtl code are almost as bad. */
3090
3091 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3092 {
3093 if (fixed_regs[i])
3094 {
3095 hard_reg_n_uses[i].uses += 2 * large + 2;
3096 SET_HARD_REG_BIT (bad_spill_regs, i);
3097 }
3098 else if (regs_explicitly_used[i])
3099 {
3100 hard_reg_n_uses[i].uses += large + 1;
3101 /* ??? We are doing this here because of the potential that
3102 bad code may be generated if a register explicitly used in
3103 an insn was used as a spill register for that insn. But
3104 not using these are spill registers may lose on some machine.
3105 We'll have to see how this works out. */
3106 SET_HARD_REG_BIT (bad_spill_regs, i);
3107 }
3108 }
3109 hard_reg_n_uses[FRAME_POINTER_REGNUM].uses += 2 * large + 2;
3110 SET_HARD_REG_BIT (bad_spill_regs, FRAME_POINTER_REGNUM);
3111
3112#ifdef ELIMINABLE_REGS
3113 /* If registers other than the frame pointer are eliminable, mark them as
3114 poor choices. */
3115 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3116 {
3117 hard_reg_n_uses[reg_eliminate[i].from].uses += 2 * large + 2;
3118 SET_HARD_REG_BIT (bad_spill_regs, reg_eliminate[i].from);
3119 }
3120#endif
3121
3122 /* Prefer registers not so far used, for use in temporary loading.
3123 Among them, if REG_ALLOC_ORDER is defined, use that order.
3124 Otherwise, prefer registers not preserved by calls. */
3125
3126#ifdef REG_ALLOC_ORDER
3127 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3128 {
3129 int regno = reg_alloc_order[i];
3130
3131 if (hard_reg_n_uses[regno].uses == 0)
3132 potential_reload_regs[o++] = regno;
3133 }
3134#else
3135 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3136 {
3137 if (hard_reg_n_uses[i].uses == 0 && call_used_regs[i])
3138 potential_reload_regs[o++] = i;
3139 }
3140 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3141 {
3142 if (hard_reg_n_uses[i].uses == 0 && ! call_used_regs[i])
3143 potential_reload_regs[o++] = i;
3144 }
3145#endif
3146
3147 qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
3148 sizeof hard_reg_n_uses[0], hard_reg_use_compare);
3149
3150 /* Now add the regs that are already used,
3151 preferring those used less often. The fixed and otherwise forbidden
3152 registers will be at the end of this list. */
3153
3154 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3155 if (hard_reg_n_uses[i].uses != 0)
3156 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
3157}
3158\f
3159/* Reload pseudo-registers into hard regs around each insn as needed.
3160 Additional register load insns are output before the insn that needs it
3161 and perhaps store insns after insns that modify the reloaded pseudo reg.
3162
3163 reg_last_reload_reg and reg_reloaded_contents keep track of
3164 which pseudo-registers are already available in reload registers.
3165 We update these for the reloads that we perform,
3166 as the insns are scanned. */
3167
3168static void
3169reload_as_needed (first, live_known)
3170 rtx first;
3171 int live_known;
3172{
3173 register rtx insn;
3174 register int i;
3175 int this_block = 0;
3176 rtx x;
3177 rtx after_call = 0;
3178
3179 bzero (spill_reg_rtx, sizeof spill_reg_rtx);
3180 reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
3181 bzero (reg_last_reload_reg, max_regno * sizeof (rtx));
3182 reg_has_output_reload = (char *) alloca (max_regno);
3183 for (i = 0; i < n_spills; i++)
3184 {
3185 reg_reloaded_contents[i] = -1;
3186 reg_reloaded_insn[i] = 0;
3187 }
3188
3189 /* Reset all offsets on eliminable registers to their initial values. */
3190#ifdef ELIMINABLE_REGS
3191 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3192 {
3193 INITIAL_ELIMINATION_OFFSET (reg_eliminate[i].from, reg_eliminate[i].to,
3194 reg_eliminate[i].initial_offset)
3195 reg_eliminate[i].previous_offset
3196 = reg_eliminate[i].offset = reg_eliminate[i].initial_offset;
3197 }
3198#else
3199 INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
3200 reg_eliminate[0].previous_offset
3201 = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
3202#endif
3203
3204 num_not_at_initial_offset = 0;
3205
3206 for (insn = first; insn;)
3207 {
3208 register rtx next = NEXT_INSN (insn);
3209
3210 /* Notice when we move to a new basic block. */
3211 if (live_known && basic_block_needs && this_block + 1 < n_basic_blocks
3212 && insn == basic_block_head[this_block+1])
3213 ++this_block;
3214
3215 /* If we pass a label, copy the offsets from the label information
3216 into the current offsets of each elimination. */
3217 if (GET_CODE (insn) == CODE_LABEL)
3218 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3219 reg_eliminate[i].offset = reg_eliminate[i].previous_offset
3220 = offsets_at[CODE_LABEL_NUMBER (insn)][i];
3221
3222 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3223 {
3224 rtx avoid_return_reg = 0;
3225
3226#ifdef SMALL_REGISTER_CLASSES
3227 /* Set avoid_return_reg if this is an insn
3228 that might use the value of a function call. */
3229 if (GET_CODE (insn) == CALL_INSN)
3230 {
3231 if (GET_CODE (PATTERN (insn)) == SET)
3232 after_call = SET_DEST (PATTERN (insn));
3233 else if (GET_CODE (PATTERN (insn)) == PARALLEL
3234 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
3235 after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
3236 else
3237 after_call = 0;
3238 }
3239 else if (after_call != 0
3240 && !(GET_CODE (PATTERN (insn)) == SET
3241 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx))
3242 {
3243 if (reg_mentioned_p (after_call, PATTERN (insn)))
3244 avoid_return_reg = after_call;
3245 after_call = 0;
3246 }
3247#endif /* SMALL_REGISTER_CLASSES */
3248
3249 /* If we need to do register elimination processing, do so.
3250 This might delete the insn, in which case we are done. */
3251 if (num_eliminable && GET_MODE (insn) == QImode)
3252 {
3253 eliminate_regs_in_insn (insn, 1);
3254 if (GET_CODE (insn) == NOTE)
3255 {
3256 insn = next;
3257 continue;
3258 }
3259 }
3260
3261 if (GET_MODE (insn) == VOIDmode)
3262 n_reloads = 0;
3263 /* First find the pseudo regs that must be reloaded for this insn.
3264 This info is returned in the tables reload_... (see reload.h).
3265 Also modify the body of INSN by substituting RELOAD
3266 rtx's for those pseudo regs. */
3267 else
3268 {
3269 bzero (reg_has_output_reload, max_regno);
3270 CLEAR_HARD_REG_SET (reg_is_output_reload);
3271
3272 find_reloads (insn, 1, spill_indirect_levels, live_known,
3273 spill_reg_order);
3274 }
3275
3276 if (n_reloads > 0)
3277 {
3278 int class;
3279
3280 /* If this block has not had spilling done for a
3281 particular class, deactivate any optional reloads
3282 of that class lest they try to use a spill-reg which isn't
3283 available here. If we have any non-optionals that need a
3284 spill reg, abort. */
3285
3286 for (class = 0; class < N_REG_CLASSES; class++)
3287 if (basic_block_needs[class] != 0
3288 && basic_block_needs[class][this_block] == 0)
3289 for (i = 0; i < n_reloads; i++)
3290 if (class == (int) reload_reg_class[i])
3291 {
3292 if (reload_optional[i])
3293 reload_in[i] = reload_out[i] = reload_reg_rtx[i] = 0;
3294 else if (reload_reg_rtx[i] == 0)
3295 abort ();
3296 }
3297
3298 /* Now compute which reload regs to reload them into. Perhaps
3299 reusing reload regs from previous insns, or else output
3300 load insns to reload them. Maybe output store insns too.
3301 Record the choices of reload reg in reload_reg_rtx. */
3302 choose_reload_regs (insn, avoid_return_reg);
3303
3304 /* Generate the insns to reload operands into or out of
3305 their reload regs. */
3306 emit_reload_insns (insn);
3307
3308 /* Substitute the chosen reload regs from reload_reg_rtx
3309 into the insn's body (or perhaps into the bodies of other
3310 load and store insn that we just made for reloading
3311 and that we moved the structure into). */
3312 subst_reloads ();
3313 }
3314 /* Any previously reloaded spilled pseudo reg, stored in this insn,
3315 is no longer validly lying around to save a future reload.
3316 Note that this does not detect pseudos that were reloaded
3317 for this insn in order to be stored in
3318 (obeying register constraints). That is correct; such reload
3319 registers ARE still valid. */
3320 note_stores (PATTERN (insn), forget_old_reloads_1);
3321
3322 /* There may have been CLOBBER insns placed after INSN. So scan
3323 between INSN and NEXT and use them to forget old reloads. */
3324 for (x = NEXT_INSN (insn); x != next; x = NEXT_INSN (x))
3325 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
3326 note_stores (PATTERN (x), forget_old_reloads_1);
3327
3328#ifdef AUTO_INC_DEC
3329 /* Likewise for regs altered by auto-increment in this insn.
3330 But note that the reg-notes are not changed by reloading:
3331 they still contain the pseudo-regs, not the spill regs. */
3332 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
3333 if (REG_NOTE_KIND (x) == REG_INC)
3334 {
3335 /* See if this pseudo reg was reloaded in this insn.
3336 If so, its last-reload info is still valid
3337 because it is based on this insn's reload. */
3338 for (i = 0; i < n_reloads; i++)
3339 if (reload_out[i] == XEXP (x, 0))
3340 break;
3341
3342 if (i != n_reloads)
3343 forget_old_reloads_1 (XEXP (x, 0));
3344 }
3345#endif
3346 }
3347 /* A reload reg's contents are unknown after a label. */
3348 if (GET_CODE (insn) == CODE_LABEL)
3349 for (i = 0; i < n_spills; i++)
3350 {
3351 reg_reloaded_contents[i] = -1;
3352 reg_reloaded_insn[i] = 0;
3353 }
3354
3355 /* Don't assume a reload reg is still good after a call insn
3356 if it is a call-used reg. */
3357 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == CALL_INSN)
3358 for (i = 0; i < n_spills; i++)
3359 if (call_used_regs[spill_regs[i]])
3360 {
3361 reg_reloaded_contents[i] = -1;
3362 reg_reloaded_insn[i] = 0;
3363 }
3364
3365 /* In case registers overlap, allow certain insns to invalidate
3366 particular hard registers. */
3367
3368#ifdef INSN_CLOBBERS_REGNO_P
3369 for (i = 0 ; i < n_spills ; i++)
3370 if (INSN_CLOBBERS_REGNO_P (insn, spill_regs[i]))
3371 {
3372 reg_reloaded_contents[i] = -1;
3373 reg_reloaded_insn[i] = 0;
3374 }
3375#endif
3376
3377 insn = next;
3378
3379#ifdef USE_C_ALLOCA
3380 alloca (0);
3381#endif
3382 }
3383}
3384
3385/* Discard all record of any value reloaded from X,
3386 or reloaded in X from someplace else;
3387 unless X is an output reload reg of the current insn.
3388
3389 X may be a hard reg (the reload reg)
3390 or it may be a pseudo reg that was reloaded from. */
3391
3392static void
3393forget_old_reloads_1 (x)
3394 rtx x;
3395{
3396 register int regno;
3397 int nr;
3398
3399 if (GET_CODE (x) != REG)
3400 return;
3401
3402 regno = REGNO (x);
3403
3404 if (regno >= FIRST_PSEUDO_REGISTER)
3405 nr = 1;
3406 else
3407 {
3408 int i;
3409 nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
3410 /* Storing into a spilled-reg invalidates its contents.
3411 This can happen if a block-local pseudo is allocated to that reg
3412 and it wasn't spilled because this block's total need is 0.
3413 Then some insn might have an optional reload and use this reg. */
3414 for (i = 0; i < nr; i++)
3415 if (spill_reg_order[regno + i] >= 0
3416 /* But don't do this if the reg actually serves as an output
3417 reload reg in the current instruction. */
3418 && (n_reloads == 0
3419 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i)))
3420 {
3421 reg_reloaded_contents[spill_reg_order[regno + i]] = -1;
3422 reg_reloaded_insn[spill_reg_order[regno + i]] = 0;
3423 }
3424 }
3425
3426 /* Since value of X has changed,
3427 forget any value previously copied from it. */
3428
3429 while (nr-- > 0)
3430 /* But don't forget a copy if this is the output reload
3431 that establishes the copy's validity. */
3432 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
3433 reg_last_reload_reg[regno + nr] = 0;
3434}
3435\f
3436/* For each reload, the mode of the reload register. */
3437static enum machine_mode reload_mode[MAX_RELOADS];
3438
3439/* For each reload, the largest number of registers it will require. */
3440static int reload_nregs[MAX_RELOADS];
3441
3442/* Comparison function for qsort to decide which of two reloads
3443 should be handled first. *P1 and *P2 are the reload numbers. */
3444
3445static int
3446reload_reg_class_lower (p1, p2)
3447 short *p1, *p2;
3448{
3449 register int r1 = *p1, r2 = *p2;
3450 register int t;
3451
3452 /* Consider required reloads before optional ones. */
3453 t = reload_optional[r1] - reload_optional[r2];
3454 if (t != 0)
3455 return t;
3456
3457 /* Count all solitary classes before non-solitary ones. */
3458 t = ((reg_class_size[(int) reload_reg_class[r2]] == 1)
3459 - (reg_class_size[(int) reload_reg_class[r1]] == 1));
3460 if (t != 0)
3461 return t;
3462
3463 /* Aside from solitaires, consider all multi-reg groups first. */
3464 t = reload_nregs[r2] - reload_nregs[r1];
3465 if (t != 0)
3466 return t;
3467
3468 /* Consider reloads in order of increasing reg-class number. */
3469 t = (int) reload_reg_class[r1] - (int) reload_reg_class[r2];
3470 if (t != 0)
3471 return t;
3472
3473 /* If reloads are equally urgent, sort by reload number,
3474 so that the results of qsort leave nothing to chance. */
3475 return r1 - r2;
3476}
3477\f
3478/* The following HARD_REG_SETs indicate when each hard register is
3479 used for a reload of various parts of the current insn. */
3480
3481/* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
3482static HARD_REG_SET reload_reg_used;
3483/* If reg is in use for a RELOAD_FOR_INPUT_RELOAD_ADDRESS reload. */
3484static HARD_REG_SET reload_reg_used_in_input_addr;
3485/* If reg is in use for a RELOAD_FOR_OUTPUT_RELOAD_ADDRESS reload. */
3486static HARD_REG_SET reload_reg_used_in_output_addr;
3487/* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
3488static HARD_REG_SET reload_reg_used_in_op_addr;
3489/* If reg is in use for a RELOAD_FOR_INPUT reload. */
3490static HARD_REG_SET reload_reg_used_in_input;
3491/* If reg is in use for a RELOAD_FOR_OUTPUT reload. */
3492static HARD_REG_SET reload_reg_used_in_output;
3493
3494/* If reg is in use as a reload reg for any sort of reload. */
3495static HARD_REG_SET reload_reg_used_at_all;
3496
3497/* Mark reg REGNO as in use for a reload of the sort spec'd by WHEN_NEEDED.
3498 MODE is used to indicate how many consecutive regs are actually used. */
3499
3500static void
3501mark_reload_reg_in_use (regno, when_needed, mode)
3502 int regno;
3503 enum reload_when_needed when_needed;
3504 enum machine_mode mode;
3505{
3506 int nregs = HARD_REGNO_NREGS (regno, mode);
3507 int i;
3508
3509 for (i = regno; i < nregs + regno; i++)
3510 {
3511 switch (when_needed)
3512 {
3513 case RELOAD_OTHER:
3514 SET_HARD_REG_BIT (reload_reg_used, i);
3515 break;
3516
3517 case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3518 SET_HARD_REG_BIT (reload_reg_used_in_input_addr, i);
3519 break;
3520
3521 case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3522 SET_HARD_REG_BIT (reload_reg_used_in_output_addr, i);
3523 break;
3524
3525 case RELOAD_FOR_OPERAND_ADDRESS:
3526 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
3527 break;
3528
3529 case RELOAD_FOR_INPUT:
3530 SET_HARD_REG_BIT (reload_reg_used_in_input, i);
3531 break;
3532
3533 case RELOAD_FOR_OUTPUT:
3534 SET_HARD_REG_BIT (reload_reg_used_in_output, i);
3535 break;
3536 }
3537
3538 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
3539 }
3540}
3541
3542/* 1 if reg REGNO is free as a reload reg for a reload of the sort
3543 specified by WHEN_NEEDED. */
3544
3545static int
3546reload_reg_free_p (regno, when_needed)
3547 int regno;
3548 enum reload_when_needed when_needed;
3549{
3550 /* In use for a RELOAD_OTHER means it's not available for anything. */
3551 if (TEST_HARD_REG_BIT (reload_reg_used, regno))
3552 return 0;
3553 switch (when_needed)
3554 {
3555 case RELOAD_OTHER:
3556 /* In use for anything means not available for a RELOAD_OTHER. */
3557 return ! TEST_HARD_REG_BIT (reload_reg_used_at_all, regno);
3558
3559 /* The other kinds of use can sometimes share a register. */
3560 case RELOAD_FOR_INPUT:
3561 return (! TEST_HARD_REG_BIT (reload_reg_used_in_input, regno)
3562 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3563 && ! TEST_HARD_REG_BIT (reload_reg_used_in_input_addr, regno));
3564 case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3565 return (! TEST_HARD_REG_BIT (reload_reg_used_in_input_addr, regno)
3566 && ! TEST_HARD_REG_BIT (reload_reg_used_in_input, regno));
3567 case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3568 return (! TEST_HARD_REG_BIT (reload_reg_used_in_output_addr, regno)
3569 && ! TEST_HARD_REG_BIT (reload_reg_used_in_output, regno));
3570 case RELOAD_FOR_OPERAND_ADDRESS:
3571 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3572 && ! TEST_HARD_REG_BIT (reload_reg_used_in_input, regno)
3573 && ! TEST_HARD_REG_BIT (reload_reg_used_in_output, regno));
3574 case RELOAD_FOR_OUTPUT:
3575 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3576 && ! TEST_HARD_REG_BIT (reload_reg_used_in_output_addr, regno)
3577 && ! TEST_HARD_REG_BIT (reload_reg_used_in_output, regno));
3578 }
3579 abort ();
3580}
3581
3582/* Return 1 if the value in reload reg REGNO, as used by a reload
3583 needed for the part of the insn specified by WHEN_NEEDED,
3584 is not in use for a reload in any prior part of the insn.
3585
3586 We can assume that the reload reg was already tested for availability
3587 at the time it is needed, and we should not check this again,
3588 in case the reg has already been marked in use. */
3589
3590static int
3591reload_reg_free_before_p (regno, when_needed)
3592 int regno;
3593 enum reload_when_needed when_needed;
3594{
3595 switch (when_needed)
3596 {
3597 case RELOAD_OTHER:
3598 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
3599 its use starts from the beginning, so nothing can use it earlier. */
3600 return 1;
3601
3602 /* If this use is for part of the insn,
3603 check the reg is not in use for any prior part. */
3604 case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3605 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
3606 return 0;
3607 case RELOAD_FOR_OUTPUT:
3608 if (TEST_HARD_REG_BIT (reload_reg_used_in_input, regno))
3609 return 0;
3610 case RELOAD_FOR_OPERAND_ADDRESS:
3611 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr, regno))
3612 return 0;
3613 case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3614 case RELOAD_FOR_INPUT:
3615 return 1;
3616 }
3617 abort ();
3618}
3619
3620/* Return 1 if the value in reload reg REGNO, as used by a reload
3621 needed for the part of the insn specified by WHEN_NEEDED,
3622 is still available in REGNO at the end of the insn.
3623
3624 We can assume that the reload reg was already tested for availability
3625 at the time it is needed, and we should not check this again,
3626 in case the reg has already been marked in use. */
3627
3628static int
3629reload_reg_reaches_end_p (regno, when_needed)
3630 int regno;
3631 enum reload_when_needed when_needed;
3632{
3633 switch (when_needed)
3634 {
3635 case RELOAD_OTHER:
3636 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
3637 its value must reach the end. */
3638 return 1;
3639
3640 /* If this use is for part of the insn,
3641 its value reaches if no subsequent part uses the same register. */
3642 case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3643 case RELOAD_FOR_INPUT:
3644 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3645 || TEST_HARD_REG_BIT (reload_reg_used_in_output, regno))
3646 return 0;
3647 case RELOAD_FOR_OPERAND_ADDRESS:
3648 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr, regno))
3649 return 0;
3650 case RELOAD_FOR_OUTPUT:
3651 case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3652 return 1;
3653 }
3654 abort ();
3655}
3656\f
3657/* Vector of reload-numbers showing the order in which the reloads should
3658 be processed. */
3659short reload_order[MAX_RELOADS];
3660
3661/* Indexed by reload number, 1 if incoming value
3662 inherited from previous insns. */
3663char reload_inherited[MAX_RELOADS];
3664
3665/* For an inherited reload, this is the insn the reload was inherited from,
3666 if we know it. Otherwise, this is 0. */
3667rtx reload_inheritance_insn[MAX_RELOADS];
3668
3669/* If non-zero, this is a place to get the value of the reload,
3670 rather than using reload_in. */
3671rtx reload_override_in[MAX_RELOADS];
3672
3673/* For each reload, the index in spill_regs of the spill register used,
3674 or -1 if we did not need one of the spill registers for this reload. */
3675int reload_spill_index[MAX_RELOADS];
3676
3677/* Index of last register assigned as a spill register. We allocate in
3678 a round-robin fashio. */
3679
3680static last_spill_reg = 0;
3681
3682/* Find a spill register to use as a reload register for reload R.
3683 LAST_RELOAD is non-zero if this is the last reload for the insn being
3684 processed.
3685
3686 Set reload_reg_rtx[R] to the register allocated.
3687
3688 If NOERROR is nonzero, we return 1 if successful,
3689 or 0 if we couldn't find a spill reg and we didn't change anything. */
3690
3691static int
3692allocate_reload_reg (r, insn, last_reload, noerror)
3693 int r;
3694 rtx insn;
3695 int last_reload;
3696 int noerror;
3697{
3698 int i;
3699 int pass;
3700 int count;
3701 rtx new;
3702 int regno;
3703
3704 /* If we put this reload ahead, thinking it is a group,
3705 then insist on finding a group. Otherwise we can grab a
3706 reg that some other reload needs.
3707 (That can happen when we have a 68000 DATA_OR_FP_REG
3708 which is a group of data regs or one fp reg.)
3709 We need not be so restrictive if there are no more reloads
3710 for this insn.
3711
3712 ??? Really it would be nicer to have smarter handling
3713 for that kind of reg class, where a problem like this is normal.
3714 Perhaps those classes should be avoided for reloading
3715 by use of more alternatives. */
3716
3717 int force_group = reload_nregs[r] > 1 && ! last_reload;
3718
3719 /* If we want a single register and haven't yet found one,
3720 take any reg in the right class and not in use.
3721 If we want a consecutive group, here is where we look for it.
3722
3723 We use two passes so we can first look for reload regs to
3724 reuse, which are already in use for other reloads in this insn,
3725 and only then use additional registers.
3726 I think that maximizing reuse is needed to make sure we don't
3727 run out of reload regs. Suppose we have three reloads, and
3728 reloads A and B can share regs. These need two regs.
3729 Suppose A and B are given different regs.
3730 That leaves none for C. */
3731 for (pass = 0; pass < 2; pass++)
3732 {
3733 /* I is the index in spill_regs.
3734 We advance it round-robin between insns to use all spill regs
3735 equally, so that inherited reloads have a chance
3736 of leapfrogging each other. */
3737
3738 for (count = 0, i = last_spill_reg; count < n_spills; count++)
3739 {
3740 int class = (int) reload_reg_class[r];
3741
3742 i = (i + 1) % n_spills;
3743
3744 if (reload_reg_free_p (spill_regs[i], reload_when_needed[r])
3745 && TEST_HARD_REG_BIT (reg_class_contents[class], spill_regs[i])
3746 && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
3747 /* Look first for regs to share, then for unshared. */
3748 && (pass || TEST_HARD_REG_BIT (reload_reg_used_at_all,
3749 spill_regs[i])))
3750 {
3751 int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
3752 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
3753 (on 68000) got us two FP regs. If NR is 1,
3754 we would reject both of them. */
3755 if (force_group)
3756 nr = CLASS_MAX_NREGS (reload_reg_class[r], reload_mode[r]);
3757 /* If we need only one reg, we have already won. */
3758 if (nr == 1)
3759 {
3760 /* But reject a single reg if we demand a group. */
3761 if (force_group)
3762 continue;
3763 break;
3764 }
3765 /* Otherwise check that as many consecutive regs as we need
3766 are available here.
3767 Also, don't use for a group registers that are
3768 needed for nongroups. */
3769 if (! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
3770 while (nr > 1)
3771 {
3772 regno = spill_regs[i] + nr - 1;
3773 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
3774 && spill_reg_order[regno] >= 0
3775 && reload_reg_free_p (regno, reload_when_needed[r])
3776 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
3777 regno)))
3778 break;
3779 nr--;
3780 }
3781 if (nr == 1)
3782 break;
3783 }
3784 }
3785
3786 /* If we found something on pass 1, omit pass 2. */
3787 if (count < n_spills)
3788 break;
3789 }
3790
3791 /* We should have found a spill register by now. */
3792 if (count == n_spills)
3793 {
3794 if (noerror)
3795 return 0;
3796 abort ();
3797 }
3798
3799 last_spill_reg = i;
3800
3801 /* Mark as in use for this insn the reload regs we use for this. */
3802 mark_reload_reg_in_use (spill_regs[i], reload_when_needed[r],
3803 reload_mode[r]);
3804
3805 new = spill_reg_rtx[i];
3806
3807 if (new == 0 || GET_MODE (new) != reload_mode[r])
3808 spill_reg_rtx[i] = new = gen_rtx (REG, reload_mode[r], spill_regs[i]);
3809
3810 reload_reg_rtx[r] = new;
3811 reload_spill_index[r] = i;
3812 regno = true_regnum (new);
3813
3814 /* Detect when the reload reg can't hold the reload mode.
3815 This used to be one `if', but Sequent compiler can't handle that. */
3816 if (HARD_REGNO_MODE_OK (regno, reload_mode[r]))
3817 {
3818 enum machine_mode test_mode = VOIDmode;
3819 if (reload_in[r])
3820 test_mode = GET_MODE (reload_in[r]);
3821 /* If reload_in[r] has VOIDmode, it means we will load it
3822 in whatever mode the reload reg has: to wit, reload_mode[r].
3823 We have already tested that for validity. */
3824 /* Aside from that, we need to test that the expressions
3825 to reload from or into have modes which are valid for this
3826 reload register. Otherwise the reload insns would be invalid. */
3827 if (! (reload_in[r] != 0 && test_mode != VOIDmode
3828 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
3829 if (! (reload_out[r] != 0
3830 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (reload_out[r]))))
3831 /* The reg is OK. */
3832 return 1;
3833 }
3834
3835 /* The reg is not OK. */
3836 if (noerror)
3837 return 0;
3838
3839 if (asm_noperands (PATTERN (insn)) < 0)
3840 /* It's the compiler's fault. */
3841 abort ();
3842
3843 /* It's the user's fault; the operand's mode and constraint
3844 don't match. Disable this reload so we don't crash in final. */
3845 error_for_asm (insn,
3846 "`asm' operand constraint incompatible with operand size");
3847 reload_in[r] = 0;
3848 reload_out[r] = 0;
3849 reload_reg_rtx[r] = 0;
3850 reload_optional[r] = 1;
3851 reload_secondary_p[r] = 1;
3852
3853 return 1;
3854}
3855\f
3856/* Assign hard reg targets for the pseudo-registers we must reload
3857 into hard regs for this insn.
3858 Also output the instructions to copy them in and out of the hard regs.
3859
3860 For machines with register classes, we are responsible for
3861 finding a reload reg in the proper class. */
3862
3863static void
3864choose_reload_regs (insn, avoid_return_reg)
3865 rtx insn;
3866 /* This argument is currently ignored. */
3867 rtx avoid_return_reg;
3868{
3869 register int i, j;
3870 int max_group_size = 1;
3871 enum reg_class group_class = NO_REGS;
3872 int inheritance;
3873
3874 rtx save_reload_reg_rtx[MAX_RELOADS];
3875 char save_reload_inherited[MAX_RELOADS];
3876 rtx save_reload_inheritance_insn[MAX_RELOADS];
3877 rtx save_reload_override_in[MAX_RELOADS];
3878 int save_reload_spill_index[MAX_RELOADS];
3879 HARD_REG_SET save_reload_reg_used;
3880 HARD_REG_SET save_reload_reg_used_in_input_addr;
3881 HARD_REG_SET save_reload_reg_used_in_output_addr;
3882 HARD_REG_SET save_reload_reg_used_in_op_addr;
3883 HARD_REG_SET save_reload_reg_used_in_input;
3884 HARD_REG_SET save_reload_reg_used_in_output;
3885 HARD_REG_SET save_reload_reg_used_at_all;
3886
3887 bzero (reload_inherited, MAX_RELOADS);
3888 bzero (reload_inheritance_insn, MAX_RELOADS * sizeof (rtx));
3889 bzero (reload_override_in, MAX_RELOADS * sizeof (rtx));
3890
3891 CLEAR_HARD_REG_SET (reload_reg_used);
3892 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
3893 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr);
3894 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr);
3895 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
3896 CLEAR_HARD_REG_SET (reload_reg_used_in_output);
3897 CLEAR_HARD_REG_SET (reload_reg_used_in_input);
3898
3899 /* Distinguish output-only and input-only reloads
3900 because they can overlap with other things. */
3901 for (j = 0; j < n_reloads; j++)
3902 if (reload_when_needed[j] == RELOAD_OTHER
3903 && ! reload_needed_for_multiple[j])
3904 {
3905 if (reload_in[j] == 0)
3906 {
3907 /* But earlyclobber operands must stay as RELOAD_OTHER. */
3908 for (i = 0; i < n_earlyclobbers; i++)
3909 if (rtx_equal_p (reload_out[j], reload_earlyclobbers[i]))
3910 break;
3911 if (i == n_earlyclobbers)
3912 reload_when_needed[j] = RELOAD_FOR_OUTPUT;
3913 }
3914 if (reload_out[j] == 0)
3915 reload_when_needed[j] = RELOAD_FOR_INPUT;
3916
3917 if (reload_secondary_reload[j] >= 0
3918 && ! reload_needed_for_multiple[reload_secondary_reload[j]])
3919 reload_when_needed[reload_secondary_reload[j]]
3920 = reload_when_needed[j];
3921 }
3922
3923#ifdef SMALL_REGISTER_CLASSES
3924 /* Don't bother with avoiding the return reg
3925 if we have no mandatory reload that could use it. */
3926 if (avoid_return_reg)
3927 {
3928 int do_avoid = 0;
3929 int regno = REGNO (avoid_return_reg);
3930 int nregs
3931 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
3932 int r;
3933
3934 for (r = regno; r < regno + nregs; r++)
3935 if (spill_reg_order[r] >= 0)
3936 for (j = 0; j < n_reloads; j++)
3937 if (!reload_optional[j] && reload_reg_rtx[j] == 0
3938 && (reload_in[j] != 0 || reload_out[j] != 0
3939 || reload_secondary_p[j])
3940 &&
3941 TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[j]], r))
3942 do_avoid = 1;
3943 if (!do_avoid)
3944 avoid_return_reg = 0;
3945 }
3946#endif /* SMALL_REGISTER_CLASSES */
3947
3948#if 0 /* Not needed, now that we can always retry without inheritance. */
3949 /* See if we have more mandatory reloads than spill regs.
3950 If so, then we cannot risk optimizations that could prevent
3951 reloads from sharing one spill register.
3952
3953 Since we will try finding a better register than reload_reg_rtx
3954 unless it is equal to reload_in or reload_out, count such reloads. */
3955
3956 {
3957 int tem = 0;
3958#ifdef SMALL_REGISTER_CLASSES
3959 int tem = (avoid_return_reg != 0);
3960#endif
3961 for (j = 0; j < n_reloads; j++)
3962 if (! reload_optional[j]
3963 && (reload_in[j] != 0 || reload_out[j] != 0 || reload_secondary_p[j])
3964 && (reload_reg_rtx[j] == 0
3965 || (! rtx_equal_p (reload_reg_rtx[j], reload_in[j])
3966 && ! rtx_equal_p (reload_reg_rtx[j], reload_out[j]))))
3967 tem++;
3968 if (tem > n_spills)
3969 must_reuse = 1;
3970 }
3971#endif
3972
3973#ifdef SMALL_REGISTER_CLASSES
3974 /* Don't use the subroutine call return reg for a reload
3975 if we are supposed to avoid it. */
3976 if (avoid_return_reg)
3977 {
3978 int regno = REGNO (avoid_return_reg);
3979 int nregs
3980 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
3981 int r;
3982
3983 for (r = regno; r < regno + nregs; r++)
3984 if (spill_reg_order[r] >= 0)
3985 SET_HARD_REG_BIT (reload_reg_used, r);
3986 }
3987#endif /* SMALL_REGISTER_CLASSES */
3988
3989 /* In order to be certain of getting the registers we need,
3990 we must sort the reloads into order of increasing register class.
3991 Then our grabbing of reload registers will parallel the process
3992 that provided the reload registers.
3993
3994 Also note whether any of the reloads wants a consecutive group of regs.
3995 If so, record the maximum size of the group desired and what
3996 register class contains all the groups needed by this insn. */
3997
3998 for (j = 0; j < n_reloads; j++)
3999 {
4000 reload_order[j] = j;
4001 reload_spill_index[j] = -1;
4002
4003 reload_mode[j]
4004 = (reload_strict_low[j] && reload_out[j]
4005 ? GET_MODE (SUBREG_REG (reload_out[j]))
4006 : (reload_inmode[j] == VOIDmode
4007 || (GET_MODE_SIZE (reload_outmode[j])
4008 > GET_MODE_SIZE (reload_inmode[j])))
4009 ? reload_outmode[j] : reload_inmode[j]);
4010
4011 reload_nregs[j] = CLASS_MAX_NREGS (reload_reg_class[j], reload_mode[j]);
4012
4013 if (reload_nregs[j] > 1)
4014 {
4015 max_group_size = MAX (reload_nregs[j], max_group_size);
4016 group_class = reg_class_superunion[(int)reload_reg_class[j]][(int)group_class];
4017 }
4018
4019 /* If we have already decided to use a certain register,
4020 don't use it in another way. */
4021 if (reload_reg_rtx[j])
4022 mark_reload_reg_in_use (REGNO (reload_reg_rtx[j]),
4023 reload_when_needed[j], reload_mode[j]);
4024 }
4025
4026 if (n_reloads > 1)
4027 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
4028
4029 bcopy (reload_reg_rtx, save_reload_reg_rtx, sizeof reload_reg_rtx);
4030 bcopy (reload_inherited, save_reload_inherited, sizeof reload_inherited);
4031 bcopy (reload_inheritance_insn, save_reload_inheritance_insn,
4032 sizeof reload_inheritance_insn);
4033 bcopy (reload_override_in, save_reload_override_in,
4034 sizeof reload_override_in);
4035 bcopy (reload_spill_index, save_reload_spill_index,
4036 sizeof reload_spill_index);
4037 COPY_HARD_REG_SET (save_reload_reg_used, reload_reg_used);
4038 COPY_HARD_REG_SET (save_reload_reg_used_at_all, reload_reg_used_at_all);
4039 COPY_HARD_REG_SET (save_reload_reg_used_in_output,
4040 reload_reg_used_in_output);
4041 COPY_HARD_REG_SET (save_reload_reg_used_in_input,
4042 reload_reg_used_in_input);
4043 COPY_HARD_REG_SET (save_reload_reg_used_in_input_addr,
4044 reload_reg_used_in_input_addr);
4045 COPY_HARD_REG_SET (save_reload_reg_used_in_output_addr,
4046 reload_reg_used_in_output_addr);
4047 COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr,
4048 reload_reg_used_in_op_addr);
4049
4050 /* Try first with inheritance, then turning it off. */
4051
4052 for (inheritance = 1; inheritance >= 0; inheritance--)
4053 {
4054 /* Process the reloads in order of preference just found.
4055 Beyond this point, subregs can be found in reload_reg_rtx.
4056
4057 This used to look for an existing reloaded home for all
4058 of the reloads, and only then perform any new reloads.
4059 But that could lose if the reloads were done out of reg-class order
4060 because a later reload with a looser constraint might have an old
4061 home in a register needed by an earlier reload with a tighter constraint.
4062
4063 To solve this, we make two passes over the reloads, in the order
4064 described above. In the first pass we try to inherit a reload
4065 from a previous insn. If there is a later reload that needs a
4066 class that is a proper subset of the class being processed, we must
4067 also allocate a spill register during the first pass.
4068
4069 Then make a second pass over the reloads to allocate any reloads
4070 that haven't been given registers yet. */
4071
4072 for (j = 0; j < n_reloads; j++)
4073 {
4074 register int r = reload_order[j];
4075
4076 /* Ignore reloads that got marked inoperative. */
4077 if (reload_out[r] == 0 && reload_in[r] == 0 && ! reload_secondary_p[r])
4078 continue;
4079
4080 /* If find_reloads chose a to use reload_in or reload_out as a reload
4081 register, we don't need to chose one. Otherwise, try even if it found
4082 one since we might save an insn if we find the value lying around. */
4083 if (reload_in[r] != 0 && reload_reg_rtx[r] != 0
4084 && (rtx_equal_p (reload_in[r], reload_reg_rtx[r])
4085 || rtx_equal_p (reload_out[r], reload_reg_rtx[r])))
4086 continue;
4087
4088#if 0 /* No longer needed for correct operation.
4089 It might give better code, or might not; worth an experiment? */
4090 /* If this is an optional reload, we can't inherit from earlier insns
4091 until we are sure that any non-optional reloads have been allocated.
4092 The following code takes advantage of the fact that optional reloads
4093 are at the end of reload_order. */
4094 if (reload_optional[r] != 0)
4095 for (i = 0; i < j; i++)
4096 if ((reload_out[reload_order[i]] != 0
4097 || reload_in[reload_order[i]] != 0
4098 || reload_secondary_p[reload_order[i]])
4099 && ! reload_optional[reload_order[i]]
4100 && reload_reg_rtx[reload_order[i]] == 0)
4101 allocate_reload_reg (reload_order[i], insn, 0, inheritance);
4102#endif
4103
4104 /* First see if this pseudo is already available as reloaded
4105 for a previous insn. We cannot try to inherit for reloads
4106 that are smaller than the maximum number of registers needed
4107 for groups unless the register we would allocate cannot be used
4108 for the groups.
4109
4110 We could check here to see if this is a secondary reload for
4111 an object that is already in a register of the desired class.
4112 This would avoid the need for the secondary reload register.
4113 But this is complex because we can't easily determine what
4114 objects might want to be loaded via this reload. So let a register
4115 be allocated here. In `emit_reload_insns' we suppress one of the
4116 loads in the case described above. */
4117
4118 if (inheritance)
4119 {
4120 register int regno = -1;
4121
4122 if (reload_in[r] == 0)
4123 ;
4124 else if (GET_CODE (reload_in[r]) == REG)
4125 regno = REGNO (reload_in[r]);
4126 else if (GET_CODE (reload_in_reg[r]) == REG)
4127 regno = REGNO (reload_in_reg[r]);
4128#if 0
4129 /* This won't work, since REGNO can be a pseudo reg number.
4130 Also, it takes much more hair to keep track of all the things
4131 that can invalidate an inherited reload of part of a pseudoreg. */
4132 else if (GET_CODE (reload_in[r]) == SUBREG
4133 && GET_CODE (SUBREG_REG (reload_in[r])) == REG)
4134 regno = REGNO (SUBREG_REG (reload_in[r])) + SUBREG_WORD (reload_in[r]);
4135#endif
4136
4137 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
4138 {
4139 i = spill_reg_order[REGNO (reg_last_reload_reg[regno])];
4140
4141 if (reg_reloaded_contents[i] == regno
4142 && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
4143 && TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
4144 spill_regs[i])
4145 && (reload_nregs[r] == max_group_size
4146 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
4147 spill_regs[i]))
4148 && reload_reg_free_p (spill_regs[i], reload_when_needed[r])
4149 && reload_reg_free_before_p (spill_regs[i],
4150 reload_when_needed[r]))
4151 {
4152 /* If a group is needed, verify that all the subsequent
4153 registers still have their values intact. */
4154 int nr
4155 = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
4156 int k;
4157
4158 for (k = 1; k < nr; k++)
4159 if (reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
4160 != regno)
4161 break;
4162
4163 if (k == nr)
4164 {
4165 /* Mark the register as in use for this part of
4166 the insn. */
4167 mark_reload_reg_in_use (spill_regs[i],
4168 reload_when_needed[r],
4169 reload_mode[r]);
4170 reload_reg_rtx[r] = reg_last_reload_reg[regno];
4171 reload_inherited[r] = 1;
4172 reload_inheritance_insn[r] = reg_reloaded_insn[i];
4173 reload_spill_index[r] = i;
4174 }
4175 }
4176 }
4177 }
4178
4179 /* Here's another way to see if the value is already lying around. */
4180 if (inheritance
4181 && reload_in[r] != 0
4182 && ! reload_inherited[r]
4183 && reload_out[r] == 0
4184 && (CONSTANT_P (reload_in[r])
4185 || GET_CODE (reload_in[r]) == PLUS
4186 || GET_CODE (reload_in[r]) == REG
4187 || GET_CODE (reload_in[r]) == MEM)
4188 && (reload_nregs[r] == max_group_size
4189 || ! reg_classes_intersect_p (reload_reg_class[r], group_class)))
4190 {
4191 register rtx equiv
4192 = find_equiv_reg (reload_in[r], insn, reload_reg_class[r],
4193 -1, 0, 0, reload_mode[r]);
4194 int regno;
4195
4196 if (equiv != 0)
4197 {
4198 if (GET_CODE (equiv) == REG)
4199 regno = REGNO (equiv);
4200 else if (GET_CODE (equiv) == SUBREG)
4201 {
4202 regno = REGNO (SUBREG_REG (equiv));
4203 if (regno < FIRST_PSEUDO_REGISTER)
4204 regno += SUBREG_WORD (equiv);
4205 }
4206 else
4207 abort ();
4208 }
4209
4210 /* If we found a spill reg, reject it unless it is free
4211 and of the desired class. */
4212 if (equiv != 0
4213 && ((spill_reg_order[regno] >= 0
4214 && ! reload_reg_free_before_p (regno,
4215 reload_when_needed[r]))
4216 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
4217 regno)))
4218 equiv = 0;
4219
4220 if (equiv != 0 && TEST_HARD_REG_BIT (reload_reg_used_at_all, regno))
4221 equiv = 0;
4222
4223 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, reload_mode[r]))
4224 equiv = 0;
4225
4226 /* We found a register that contains the value we need.
4227 If this register is the same as an `earlyclobber' operand
4228 of the current insn, just mark it as a place to reload from
4229 since we can't use it as the reload register itself. */
4230
4231 if (equiv != 0)
4232 for (i = 0; i < n_earlyclobbers; i++)
4233 if (reg_overlap_mentioned_p (equiv, reload_earlyclobbers[i]))
4234 {
4235 reload_override_in[r] = equiv;
4236 equiv = 0;
4237 break;
4238 }
4239
4240 /* JRV: If the equiv register we have found is explicitly
4241 clobbered in the current insn, mark but don't use, as above. */
4242
4243 if (equiv != 0 && regno_clobbered_p (regno, insn))
4244 {
4245 reload_override_in[r] = equiv;
4246 equiv = 0;
4247 }
4248
4249 /* If we found an equivalent reg, say no code need be generated
4250 to load it, and use it as our reload reg. */
4251 if (equiv != 0 && regno != FRAME_POINTER_REGNUM)
4252 {
4253 reload_reg_rtx[r] = equiv;
4254 reload_inherited[r] = 1;
4255 /* If it is a spill reg,
4256 mark the spill reg as in use for this insn. */
4257 i = spill_reg_order[regno];
4258 if (i >= 0)
4259 mark_reload_reg_in_use (regno, reload_when_needed[r],
4260 reload_mode[r]);
4261 }
4262 }
4263
4264 /* If we found a register to use already, or if this is an optional
4265 reload, we are done. */
4266 if (reload_reg_rtx[r] != 0 || reload_optional[r] != 0)
4267 continue;
4268
4269#if 0 /* No longer needed for correct operation. Might or might not
4270 give better code on the average. Want to experiment? */
4271
4272 /* See if there is a later reload that has a class different from our
4273 class that intersects our class or that requires less register
4274 than our reload. If so, we must allocate a register to this
4275 reload now, since that reload might inherit a previous reload
4276 and take the only available register in our class. Don't do this
4277 for optional reloads since they will force all previous reloads
4278 to be allocated. Also don't do this for reloads that have been
4279 turned off. */
4280
4281 for (i = j + 1; i < n_reloads; i++)
4282 {
4283 int s = reload_order[i];
4284
4285 if ((reload_in[s] == 0 && reload_out[s] == 0 &&
4286 ! reload_secondary_p[s])
4287 || reload_optional[s])
4288 continue;
4289
4290 if ((reload_reg_class[s] != reload_reg_class[r]
4291 && reg_classes_intersect_p (reload_reg_class[r],
4292 reload_reg_class[s]))
4293 || reload_nregs[s] < reload_nregs[r])
4294 break;
4295 }
4296
4297 if (i == n_reloads)
4298 continue;
4299
4300 allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance);
4301#endif
4302 }
4303
4304 /* Now allocate reload registers for anything non-optional that
4305 didn't get one yet. */
4306 for (j = 0; j < n_reloads; j++)
4307 {
4308 register int r = reload_order[j];
4309
4310 /* Ignore reloads that got marked inoperative. */
4311 if (reload_out[r] == 0 && reload_in[r] == 0 && ! reload_secondary_p[r])
4312 continue;
4313
4314 /* Skip reloads that already have a register allocated or are
4315 optional. */
4316 if (reload_reg_rtx[r] != 0 || reload_optional[r])
4317 continue;
4318
4319 if (! allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance))
4320 break;
4321 }
4322
4323 /* If that loop got all the way, we have won. */
4324 if (j == n_reloads)
4325 break;
4326
4327 fail:
4328 /* Loop around and try without any inheritance. */
4329 /* First undo everything done by the failed attempt
4330 to allocate with inheritance. */
4331 bcopy (save_reload_reg_rtx, reload_reg_rtx, sizeof reload_reg_rtx);
4332 bcopy (save_reload_inherited, reload_inherited, sizeof reload_inherited);
4333 bcopy (save_reload_inheritance_insn, reload_inheritance_insn,
4334 sizeof reload_inheritance_insn);
4335 bcopy (save_reload_override_in, reload_override_in,
4336 sizeof reload_override_in);
4337 bcopy (save_reload_spill_index, reload_spill_index,
4338 sizeof reload_spill_index);
4339 COPY_HARD_REG_SET (reload_reg_used, save_reload_reg_used);
4340 COPY_HARD_REG_SET (reload_reg_used_at_all, save_reload_reg_used_at_all);
4341 COPY_HARD_REG_SET (reload_reg_used_in_input,
4342 save_reload_reg_used_in_input);
4343 COPY_HARD_REG_SET (reload_reg_used_in_output,
4344 save_reload_reg_used_in_output);
4345 COPY_HARD_REG_SET (reload_reg_used_in_input_addr,
4346 save_reload_reg_used_in_input_addr);
4347 COPY_HARD_REG_SET (reload_reg_used_in_output_addr,
4348 save_reload_reg_used_in_output_addr);
4349 COPY_HARD_REG_SET (reload_reg_used_in_op_addr,
4350 save_reload_reg_used_in_op_addr);
4351 }
4352
4353 /* If we thought we could inherit a reload, because it seemed that
4354 nothing else wanted the same reload register earlier in the insn,
4355 verify that assumption, now that all reloads have been assigned. */
4356
4357 for (j = 0; j < n_reloads; j++)
4358 {
4359 register int r = reload_order[j];
4360
4361 if (reload_inherited[r] && reload_reg_rtx[r] != 0
4362 && ! reload_reg_free_before_p (true_regnum (reload_reg_rtx[r]),
4363 reload_when_needed[r]))
4364 reload_inherited[r] = 0;
4365
4366 /* If we found a better place to reload from,
4367 validate it in the same fashion, if it is a reload reg. */
4368 if (reload_override_in[r]
4369 && (GET_CODE (reload_override_in[r]) == REG
4370 || GET_CODE (reload_override_in[r]) == SUBREG))
4371 {
4372 int regno = true_regnum (reload_override_in[r]);
4373 if (spill_reg_order[regno] >= 0
4374 && ! reload_reg_free_before_p (regno, reload_when_needed[r]))
4375 reload_override_in[r] = 0;
4376 }
4377 }
4378
4379 /* Now that reload_override_in is known valid,
4380 actually override reload_in. */
4381 for (j = 0; j < n_reloads; j++)
4382 if (reload_override_in[j])
4383 reload_in[j] = reload_override_in[j];
4384
4385 /* If this reload won't be done because it has been cancelled or is
4386 optional and not inherited, clear reload_reg_rtx so other
4387 routines (such as subst_reloads) don't get confused. */
4388 for (j = 0; j < n_reloads; j++)
4389 if ((reload_optional[j] && ! reload_inherited[j])
4390 || (reload_in[j] == 0 && reload_out[j] == 0
4391 && ! reload_secondary_p[j]))
4392 reload_reg_rtx[j] = 0;
4393
4394 /* Record which pseudos and which spill regs have output reloads. */
4395 for (j = 0; j < n_reloads; j++)
4396 {
4397 register int r = reload_order[j];
4398
4399 i = reload_spill_index[r];
4400
4401 /* I is nonneg if this reload used one of the spill regs.
4402 If reload_reg_rtx[r] is 0, this is an optional reload
4403 that we opted to ignore. */
4404 if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG
4405 && reload_reg_rtx[r] != 0)
4406 {
4407 register int nregno = REGNO (reload_out[r]);
4408 int nr = HARD_REGNO_NREGS (nregno, reload_mode[r]);
4409
4410 while (--nr >= 0)
4411 {
4412 reg_has_output_reload[nregno + nr] = 1;
4413 if (i >= 0)
4414 SET_HARD_REG_BIT (reg_is_output_reload, spill_regs[i] + nr);
4415 }
4416
4417 if (reload_when_needed[r] != RELOAD_OTHER
4418 && reload_when_needed[r] != RELOAD_FOR_OUTPUT)
4419 abort ();
4420 }
4421 }
4422}
4423\f
4424/* Output insns to reload values in and out of the chosen reload regs. */
4425
4426static void
4427emit_reload_insns (insn)
4428 rtx insn;
4429{
4430 register int j;
4431 rtx following_insn = NEXT_INSN (insn);
a8efe40d 4432 rtx before_insn = insn;
32131a9c
RK
4433 rtx first_output_reload_insn = NEXT_INSN (insn);
4434 rtx first_other_reload_insn = insn;
4435 rtx first_operand_address_reload_insn = insn;
4436 int special;
4437 /* Values to be put in spill_reg_store are put here first. */
4438 rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
4439
a8efe40d
RK
4440 /* If this is a CALL_INSN preceeded by USE insns, any reload insns
4441 must go in front of the first USE insn, not in front of INSN. */
4442
4443 if (GET_CODE (insn) == CALL_INSN && GET_CODE (PREV_INSN (insn)) == INSN
4444 && GET_CODE (PATTERN (PREV_INSN (insn))) == USE)
4445 while (GET_CODE (PREV_INSN (before_insn)) == INSN
4446 && GET_CODE (PATTERN (PREV_INSN (before_insn))) == USE)
4447 first_other_reload_insn = first_operand_address_reload_insn
4448 = before_insn = PREV_INSN (before_insn);
4449
32131a9c
RK
4450 /* Now output the instructions to copy the data into and out of the
4451 reload registers. Do these in the order that the reloads were reported,
4452 since reloads of base and index registers precede reloads of operands
4453 and the operands may need the base and index registers reloaded. */
4454
4455 for (j = 0; j < n_reloads; j++)
4456 {
4457 register rtx old;
4458 rtx oldequiv_reg = 0;
4459 rtx this_reload_insn = 0;
4460 rtx store_insn = 0;
4461
4462 old = reload_in[j];
4463 if (old != 0 && ! reload_inherited[j]
4464 && ! rtx_equal_p (reload_reg_rtx[j], old)
4465 && reload_reg_rtx[j] != 0)
4466 {
4467 register rtx reloadreg = reload_reg_rtx[j];
4468 rtx oldequiv = 0;
4469 enum machine_mode mode;
4470 rtx where;
d445b551 4471 rtx reload_insn;
32131a9c
RK
4472
4473 /* Determine the mode to reload in.
4474 This is very tricky because we have three to choose from.
4475 There is the mode the insn operand wants (reload_inmode[J]).
4476 There is the mode of the reload register RELOADREG.
4477 There is the intrinsic mode of the operand, which we could find
4478 by stripping some SUBREGs.
4479 It turns out that RELOADREG's mode is irrelevant:
4480 we can change that arbitrarily.
4481
4482 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
4483 then the reload reg may not support QImode moves, so use SImode.
4484 If foo is in memory due to spilling a pseudo reg, this is safe,
4485 because the QImode value is in the least significant part of a
4486 slot big enough for a SImode. If foo is some other sort of
4487 memory reference, then it is impossible to reload this case,
4488 so previous passes had better make sure this never happens.
4489
4490 Then consider a one-word union which has SImode and one of its
4491 members is a float, being fetched as (SUBREG:SF union:SI).
4492 We must fetch that as SFmode because we could be loading into
4493 a float-only register. In this case OLD's mode is correct.
4494
4495 Consider an immediate integer: it has VOIDmode. Here we need
4496 to get a mode from something else.
4497
4498 In some cases, there is a fourth mode, the operand's
4499 containing mode. If the insn specifies a containing mode for
4500 this operand, it overrides all others.
4501
4502 I am not sure whether the algorithm here is always right,
4503 but it does the right things in those cases. */
4504
4505 mode = GET_MODE (old);
4506 if (mode == VOIDmode)
4507 mode = reload_inmode[j];
4508 if (reload_strict_low[j])
4509 mode = GET_MODE (SUBREG_REG (reload_in[j]));
4510
4511#ifdef SECONDARY_INPUT_RELOAD_CLASS
4512 /* If we need a secondary register for this operation, see if
4513 the value is already in a register in that class. Don't
4514 do this if the secondary register will be used as a scratch
4515 register. */
4516
4517 if (reload_secondary_reload[j] >= 0
4518 && reload_secondary_icode[j] == CODE_FOR_nothing)
4519 oldequiv
4520 = find_equiv_reg (old, insn,
4521 reload_reg_class[reload_secondary_reload[j]],
4522 -1, 0, 0, mode);
4523#endif
4524
4525 /* If reloading from memory, see if there is a register
4526 that already holds the same value. If so, reload from there.
4527 We can pass 0 as the reload_reg_p argument because
4528 any other reload has either already been emitted,
4529 in which case find_equiv_reg will see the reload-insn,
4530 or has yet to be emitted, in which case it doesn't matter
4531 because we will use this equiv reg right away. */
4532
4533 if (oldequiv == 0
4534 && (GET_CODE (old) == MEM
4535 || (GET_CODE (old) == REG
4536 && REGNO (old) >= FIRST_PSEUDO_REGISTER
4537 && reg_renumber[REGNO (old)] < 0)))
4538 oldequiv = find_equiv_reg (old, insn, GENERAL_REGS,
4539 -1, 0, 0, mode);
4540
4541 if (oldequiv)
4542 {
4543 int regno = true_regnum (oldequiv);
4544
4545 /* If OLDEQUIV is a spill register, don't use it for this
4546 if any other reload needs it at an earlier stage of this insn
4547 or at this stage. */
4548 if (spill_reg_order[regno] >= 0
4549 && (! reload_reg_free_p (regno, reload_when_needed[j])
4550 || ! reload_reg_free_before_p (regno,
4551 reload_when_needed[j])))
4552 oldequiv = 0;
4553
4554 /* If OLDEQUIV is not a spill register,
4555 don't use it if any other reload wants it. */
4556 if (spill_reg_order[regno] < 0)
4557 {
4558 int k;
4559 for (k = 0; k < n_reloads; k++)
4560 if (reload_reg_rtx[k] != 0 && k != j
4561 && reg_overlap_mentioned_p (reload_reg_rtx[k], oldequiv))
4562 {
4563 oldequiv = 0;
4564 break;
4565 }
4566 }
4567 }
4568
4569 if (oldequiv == 0)
4570 oldequiv = old;
4571 else if (GET_CODE (oldequiv) == REG)
4572 oldequiv_reg = oldequiv;
4573 else if (GET_CODE (oldequiv) == SUBREG)
4574 oldequiv_reg = SUBREG_REG (oldequiv);
4575
4576 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
4577 then load RELOADREG from OLDEQUIV. */
4578
4579 if (GET_MODE (reloadreg) != mode)
4580 reloadreg = gen_rtx (REG, mode, REGNO (reloadreg));
4581 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
4582 oldequiv = SUBREG_REG (oldequiv);
4583 if (GET_MODE (oldequiv) != VOIDmode
4584 && mode != GET_MODE (oldequiv))
4585 oldequiv = gen_rtx (SUBREG, mode, oldequiv, 0);
4586
4587 /* Decide where to put reload insn for this reload. */
4588 switch (reload_when_needed[j])
4589 {
4590 case RELOAD_FOR_INPUT:
4591 case RELOAD_OTHER:
4592 where = first_operand_address_reload_insn;
4593 break;
4594 case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
4595 where = first_other_reload_insn;
4596 break;
4597 case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
4598 where = first_output_reload_insn;
4599 break;
4600 case RELOAD_FOR_OPERAND_ADDRESS:
a8efe40d 4601 where = before_insn;
32131a9c
RK
4602 }
4603
4604 special = 0;
4605
4606 /* Auto-increment addresses must be reloaded in a special way. */
4607 if (GET_CODE (oldequiv) == POST_INC
4608 || GET_CODE (oldequiv) == POST_DEC
4609 || GET_CODE (oldequiv) == PRE_INC
4610 || GET_CODE (oldequiv) == PRE_DEC)
4611 {
4612 /* We are not going to bother supporting the case where a
4613 incremented register can't be copied directly from
4614 OLDEQUIV since this seems highly unlikely. */
4615 if (reload_secondary_reload[j] >= 0)
4616 abort ();
4617 /* Prevent normal processing of this reload. */
4618 special = 1;
4619 /* Output a special code sequence for this case. */
4620 this_reload_insn
4621 = inc_for_reload (reloadreg, oldequiv, reload_inc[j], where);
4622 }
4623
4624 /* If we are reloading a pseudo-register that was set by the previous
4625 insn, see if we can get rid of that pseudo-register entirely
4626 by redirecting the previous insn into our reload register. */
4627
4628 else if (optimize && GET_CODE (old) == REG
4629 && REGNO (old) >= FIRST_PSEUDO_REGISTER
4630 && dead_or_set_p (insn, old)
4631 /* This is unsafe if some other reload
4632 uses the same reg first. */
4633 && (reload_when_needed[j] == RELOAD_OTHER
4634 || reload_when_needed[j] == RELOAD_FOR_INPUT
4635 || reload_when_needed[j] == RELOAD_FOR_INPUT_RELOAD_ADDRESS))
4636 {
4637 rtx temp = PREV_INSN (insn);
4638 while (temp && GET_CODE (temp) == NOTE)
4639 temp = PREV_INSN (temp);
4640 if (temp
4641 && GET_CODE (temp) == INSN
4642 && GET_CODE (PATTERN (temp)) == SET
4643 && SET_DEST (PATTERN (temp)) == old
4644 /* Make sure we can access insn_operand_constraint. */
4645 && asm_noperands (PATTERN (temp)) < 0
4646 /* This is unsafe if prev insn rejects our reload reg. */
4647 && constraint_accepts_reg_p (insn_operand_constraint[recog_memoized (temp)][0],
4648 reloadreg)
4649 /* This is unsafe if operand occurs more than once in current
4650 insn. Perhaps some occurrences aren't reloaded. */
4651 && count_occurrences (PATTERN (insn), old) == 1
4652 /* Don't risk splitting a matching pair of operands. */
4653 && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
4654 {
4655 /* Store into the reload register instead of the pseudo. */
4656 SET_DEST (PATTERN (temp)) = reloadreg;
4657 /* If these are the only uses of the pseudo reg,
4658 pretend for GDB it lives in the reload reg we used. */
4659 if (reg_n_deaths[REGNO (old)] == 1
4660 && reg_n_sets[REGNO (old)] == 1)
4661 {
4662 reg_renumber[REGNO (old)] = REGNO (reload_reg_rtx[j]);
4663 alter_reg (REGNO (old), -1);
4664 }
4665 special = 1;
4666 }
4667 }
4668
4669 /* We can't do that, so output an insn to load RELOADREG.
4670 Keep them in the following order:
4671 all reloads for input reload addresses,
4672 all reloads for ordinary input operands,
4673 all reloads for addresses of non-reloaded operands,
4674 the insn being reloaded,
4675 all reloads for addresses of output reloads,
4676 the output reloads. */
4677 if (! special)
4678 {
4679#ifdef SECONDARY_INPUT_RELOAD_CLASS
4680 rtx second_reload_reg = 0;
4681 enum insn_code icode;
4682
4683 /* If we have a secondary reload, pick up the secondary register
d445b551
RK
4684 and icode, if any. If OLDEQUIV and OLD are different or
4685 if this is an in-out reload, recompute whether or not we
4686 still need a secondary register and what the icode should
4687 be. If we still need a secondary register and the class or
4688 icode is different, go back to reloading from OLD if using
4689 OLDEQUIV means that we got the wrong type of register. We
4690 cannot have different class or icode due to an in-out reload
4691 because we don't make such reloads when both the input and
4692 output need secondary reload registers. */
32131a9c
RK
4693
4694 if (reload_secondary_reload[j] >= 0)
4695 {
4696 int secondary_reload = reload_secondary_reload[j];
4697 second_reload_reg = reload_reg_rtx[secondary_reload];
4698 icode = reload_secondary_icode[j];
4699
d445b551
RK
4700 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
4701 || (reload_in[j] != 0 && reload_out[j] != 0))
32131a9c
RK
4702 {
4703 enum reg_class new_class
4704 = SECONDARY_INPUT_RELOAD_CLASS (reload_reg_class[j],
4705 mode, oldequiv);
4706
4707 if (new_class == NO_REGS)
4708 second_reload_reg = 0;
4709 else
4710 {
4711 enum insn_code new_icode;
4712 enum machine_mode new_mode;
4713
4714 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
4715 REGNO (second_reload_reg)))
4716 oldequiv = old;
4717 else
4718 {
4719 new_icode = reload_in_optab[(int) mode];
4720 if (new_icode != CODE_FOR_nothing
4721 && ((insn_operand_predicate[(int) new_icode][0]
4722 && ! (insn_operand_predicate[(int) new_icode][0]
4723 (reloadreg, mode)))
4724 || (insn_operand_predicate[(int) new_icode]
4725 && ! (insn_operand_predicate[(int) new_icode][1]
4726 (oldequiv, mode)))))
4727 new_icode = CODE_FOR_nothing;
4728
4729 if (new_icode == CODE_FOR_nothing)
4730 new_mode = mode;
4731 else
4732 new_mode = insn_operand_mode[new_icode][2];
4733
4734 if (GET_MODE (second_reload_reg) != new_mode)
4735 {
4736 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
4737 new_mode))
4738 oldequiv = old;
4739 else
4740 second_reload_reg
4741 = gen_reg_rtx (REG, new_mode,
4742 REGNO (second_reload_reg));
4743 }
4744 }
4745 }
4746 }
4747
4748 /* If we still need a secondary reload register, check
4749 to see if it is being used as a scratch or intermediate
4750 register and generate code appropriately. */
4751
4752 if (second_reload_reg)
4753 {
4754 if (icode != CODE_FOR_nothing)
4755 {
d445b551
RK
4756 reload_insn = emit_insn_before (GEN_FCN (icode)
4757 (reloadreg, oldequiv,
4758 second_reload_reg),
4759 where);
4760 if (this_reload_insn == 0)
4761 this_reload_insn = reload_insn;
32131a9c
RK
4762 special = 1;
4763 }
4764 else
4765 {
4766 /* See if we need a scratch register to load the
4767 intermediate register (a tertiary reload). */
4768 enum insn_code tertiary_icode
4769 = reload_secondary_icode[secondary_reload];
4770
4771 if (tertiary_icode != CODE_FOR_nothing)
4772 {
4773 rtx third_reload_reg
4774 = reload_reg_rtx[reload_secondary_reload[secondary_reload]];
4775
d445b551
RK
4776 reload_insn
4777 = emit_insn_before ((GEN_FCN (tertiary_icode)
4778 (second_reload_reg,
4779 oldequiv,
4780 third_reload_reg)),
4781 where);
4782 if (this_reload_insn == 0)
4783 this_reload_insn = reload_insn;
32131a9c
RK
4784 }
4785 else
4786 {
d445b551
RK
4787 reload_insn
4788 = gen_input_reload (second_reload_reg,
4789 oldequiv, where);
4790 if (this_reload_insn == 0)
4791 this_reload_insn = reload_insn;
32131a9c
RK
4792 oldequiv = second_reload_reg;
4793 }
4794 }
4795 }
4796 }
4797#endif
4798
4799 if (! special)
d445b551
RK
4800 {
4801 reload_insn = gen_input_reload (reloadreg,
4802 oldequiv, where);
4803 if (this_reload_insn == 0)
4804 this_reload_insn = reload_insn;
4805 }
32131a9c
RK
4806
4807#if defined(SECONDARY_INPUT_RELOAD_CLASS) && defined(PRESERVE_DEATH_INFO_REGNO_P)
4808 /* We may have to make a REG_DEAD note for the secondary reload
4809 register in the insns we just made. Find the last insn that
4810 mentioned the register. */
4811 if (! special && second_reload_reg
4812 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reload_reg)))
4813 {
4814 rtx prev;
4815
4816 for (prev = where;
4817 prev != PREV_INSN (this_reload_insn);
4818 prev = PREV_INSN (prev))
4819 if (GET_RTX_CLASS (GET_CODE (prev) == 'i')
4820 && reg_overlap_mentioned_p (second_reload_reg,
4821 PATTERN (prev)))
4822 {
4823 REG_NOTES (prev) = gen_rtx (EXPR_LIST, REG_DEAD,
4824 second_reload_reg,
4825 REG_NOTES (prev));
4826 break;
4827 }
4828 }
4829#endif
4830 }
4831
4832 /* Update where to put other reload insns. */
4833 if (this_reload_insn)
4834 switch (reload_when_needed[j])
4835 {
4836 case RELOAD_FOR_INPUT:
4837 case RELOAD_OTHER:
4838 if (first_other_reload_insn == first_operand_address_reload_insn)
4839 first_other_reload_insn = this_reload_insn;
4840 break;
4841 case RELOAD_FOR_OPERAND_ADDRESS:
a8efe40d 4842 if (first_operand_address_reload_insn == before_insn)
32131a9c 4843 first_operand_address_reload_insn = this_reload_insn;
a8efe40d 4844 if (first_other_reload_insn == before_insn)
32131a9c
RK
4845 first_other_reload_insn = this_reload_insn;
4846 }
4847
4848 /* reload_inc[j] was formerly processed here. */
4849 }
4850
4851 /* Add a note saying the input reload reg
4852 dies in this insn, if anyone cares. */
4853#ifdef PRESERVE_DEATH_INFO_REGNO_P
4854 if (old != 0
4855 && reload_reg_rtx[j] != old
4856 && reload_reg_rtx[j] != 0
4857 && reload_out[j] == 0
4858 && ! reload_inherited[j]
4859 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j])))
4860 {
4861 register rtx reloadreg = reload_reg_rtx[j];
4862
4863#if 0
4864 /* We can't abort here because we need to support this for sched.c.
4865 It's not terrible to miss a REG_DEAD note, but we should try
4866 to figure out how to do this correctly. */
4867 /* The code below is incorrect for address-only reloads. */
4868 if (reload_when_needed[j] != RELOAD_OTHER
4869 && reload_when_needed[j] != RELOAD_FOR_INPUT)
4870 abort ();
4871#endif
4872
4873 /* Add a death note to this insn, for an input reload. */
4874
4875 if ((reload_when_needed[j] == RELOAD_OTHER
4876 || reload_when_needed[j] == RELOAD_FOR_INPUT)
4877 && ! dead_or_set_p (insn, reloadreg))
4878 REG_NOTES (insn)
4879 = gen_rtx (EXPR_LIST, REG_DEAD,
4880 reloadreg, REG_NOTES (insn));
4881 }
4882
4883 /* When we inherit a reload, the last marked death of the reload reg
4884 may no longer really be a death. */
4885 if (reload_reg_rtx[j] != 0
4886 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j]))
4887 && reload_inherited[j])
4888 {
4889 /* Handle inheriting an output reload.
4890 Remove the death note from the output reload insn. */
4891 if (reload_spill_index[j] >= 0
4892 && GET_CODE (reload_in[j]) == REG
4893 && spill_reg_store[reload_spill_index[j]] != 0
4894 && find_regno_note (spill_reg_store[reload_spill_index[j]],
4895 REG_DEAD, REGNO (reload_reg_rtx[j])))
4896 remove_death (REGNO (reload_reg_rtx[j]),
4897 spill_reg_store[reload_spill_index[j]]);
4898 /* Likewise for input reloads that were inherited. */
4899 else if (reload_spill_index[j] >= 0
4900 && GET_CODE (reload_in[j]) == REG
4901 && spill_reg_store[reload_spill_index[j]] == 0
4902 && reload_inheritance_insn[j] != 0
4903 && find_regno_note (reload_inheritance_insn[j], REG_DEAD,
4904 REGNO (reload_reg_rtx[j])))
4905 remove_death (REGNO (reload_reg_rtx[j]),
4906 reload_inheritance_insn[j]);
4907 else
4908 {
4909 rtx prev;
4910
4911 /* We got this register from find_equiv_reg.
4912 Search back for its last death note and get rid of it.
4913 But don't search back too far.
4914 Don't go past a place where this reg is set,
4915 since a death note before that remains valid. */
4916 for (prev = PREV_INSN (insn);
4917 prev && GET_CODE (prev) != CODE_LABEL;
4918 prev = PREV_INSN (prev))
4919 if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
4920 && dead_or_set_p (prev, reload_reg_rtx[j]))
4921 {
4922 if (find_regno_note (prev, REG_DEAD,
4923 REGNO (reload_reg_rtx[j])))
4924 remove_death (REGNO (reload_reg_rtx[j]), prev);
4925 break;
4926 }
4927 }
4928 }
4929
4930 /* We might have used find_equiv_reg above to choose an alternate
4931 place from which to reload. If so, and it died, we need to remove
4932 that death and move it to one of the insns we just made. */
4933
4934 if (oldequiv_reg != 0
4935 && PRESERVE_DEATH_INFO_REGNO_P (true_regnum (oldequiv_reg)))
4936 {
4937 rtx prev, prev1;
4938
4939 for (prev = PREV_INSN (insn); prev && GET_CODE (prev) != CODE_LABEL;
4940 prev = PREV_INSN (prev))
4941 if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
4942 && dead_or_set_p (prev, oldequiv_reg))
4943 {
4944 if (find_regno_note (prev, REG_DEAD, REGNO (oldequiv_reg)))
4945 {
4946 for (prev1 = this_reload_insn;
4947 prev1; prev1 = PREV_INSN (prev1))
4948 if (GET_RTX_CLASS (GET_CODE (prev1) == 'i')
4949 && reg_overlap_mentioned_p (oldequiv_reg,
4950 PATTERN (prev1)))
4951 {
4952 REG_NOTES (prev1) = gen_rtx (EXPR_LIST, REG_DEAD,
4953 oldequiv_reg,
4954 REG_NOTES (prev1));
4955 break;
4956 }
4957 remove_death (REGNO (oldequiv_reg), prev);
4958 }
4959 break;
4960 }
4961 }
4962#endif
4963
4964 /* If we are reloading a register that was recently stored in with an
4965 output-reload, see if we can prove there was
4966 actually no need to store the old value in it. */
4967
4968 if (optimize && reload_inherited[j] && reload_spill_index[j] >= 0
4969 /* This is unsafe if some other reload uses the same reg first. */
4970 && (reload_when_needed[j] == RELOAD_OTHER
4971 || reload_when_needed[j] == RELOAD_FOR_INPUT
4972 || reload_when_needed[j] == RELOAD_FOR_INPUT_RELOAD_ADDRESS)
4973 && GET_CODE (reload_in[j]) == REG
4974#if 0
4975 /* There doesn't seem to be any reason to restrict this to pseudos
4976 and doing so loses in the case where we are copying from a
4977 register of the wrong class. */
4978 && REGNO (reload_in[j]) >= FIRST_PSEUDO_REGISTER
4979#endif
4980 && spill_reg_store[reload_spill_index[j]] != 0
4981 && dead_or_set_p (insn, reload_in[j])
4982 /* This is unsafe if operand occurs more than once in current
4983 insn. Perhaps some occurrences weren't reloaded. */
4984 && count_occurrences (PATTERN (insn), reload_in[j]) == 1)
4985 delete_output_reload (insn, j,
4986 spill_reg_store[reload_spill_index[j]]);
4987
4988 /* Input-reloading is done. Now do output-reloading,
4989 storing the value from the reload-register after the main insn
4990 if reload_out[j] is nonzero.
4991
4992 ??? At some point we need to support handling output reloads of
4993 JUMP_INSNs or insns that set cc0. */
4994 old = reload_out[j];
4995 if (old != 0
4996 && reload_reg_rtx[j] != old
4997 && reload_reg_rtx[j] != 0)
4998 {
4999 register rtx reloadreg = reload_reg_rtx[j];
5000 register rtx second_reloadreg = 0;
5001 rtx prev_insn = PREV_INSN (first_output_reload_insn);
5002 rtx note, p;
5003 enum machine_mode mode;
5004 int special = 0;
5005
5006 /* An output operand that dies right away does need a reload,
5007 but need not be copied from it. Show the new location in the
5008 REG_UNUSED note. */
5009 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
5010 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
5011 {
5012 XEXP (note, 0) = reload_reg_rtx[j];
5013 continue;
5014 }
5015 else if (GET_CODE (old) == SCRATCH)
5016 /* If we aren't optimizing, there won't be a REG_UNUSED note,
5017 but we don't want to make an output reload. */
5018 continue;
5019
5020#if 0
5021 /* Strip off of OLD any size-increasing SUBREGs such as
5022 (SUBREG:SI foo:QI 0). */
5023
5024 while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
5025 && (GET_MODE_SIZE (GET_MODE (old))
5026 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
5027 old = SUBREG_REG (old);
5028#endif
5029
5030 /* If is a JUMP_INSN, we can't support output reloads yet. */
5031 if (GET_CODE (insn) == JUMP_INSN)
5032 abort ();
5033
5034 /* Determine the mode to reload in.
5035 See comments above (for input reloading). */
5036
5037 mode = GET_MODE (old);
5038 if (mode == VOIDmode)
5039 abort (); /* Should never happen for an output. */
5040
5041 /* A strict-low-part output operand needs to be reloaded
5042 in the mode of the entire value. */
5043 if (reload_strict_low[j])
5044 {
5045 mode = GET_MODE (SUBREG_REG (reload_out[j]));
5046 /* Encapsulate OLD into that mode. */
5047 /* If OLD is a subreg, then strip it, since the subreg will
5048 be altered by this very reload. */
5049 while (GET_CODE (old) == SUBREG && GET_MODE (old) != mode)
5050 old = SUBREG_REG (old);
5051 if (GET_MODE (old) != VOIDmode
5052 && mode != GET_MODE (old))
5053 old = gen_rtx (SUBREG, mode, old, 0);
5054 }
5055
5056 if (GET_MODE (reloadreg) != mode)
5057 reloadreg = gen_rtx (REG, mode, REGNO (reloadreg));
5058
5059#ifdef SECONDARY_OUTPUT_RELOAD_CLASS
5060
5061 /* If we need two reload regs, set RELOADREG to the intermediate
5062 one, since it will be stored into OUT. We might need a secondary
5063 register only for an input reload, so check again here. */
5064
5065 if (reload_secondary_reload[j] >= 0
5066 && (SECONDARY_OUTPUT_RELOAD_CLASS (reload_reg_class[j],
5067 mode, old)
5068 != NO_REGS))
5069 {
5070 second_reloadreg = reloadreg;
5071 reloadreg = reload_reg_rtx[reload_secondary_reload[j]];
5072
5073 /* See if RELOADREG is to be used as a scratch register
5074 or as an intermediate register. */
5075 if (reload_secondary_icode[j] != CODE_FOR_nothing)
5076 {
5077 emit_insn_before ((GEN_FCN (reload_secondary_icode[j])
5078 (old, second_reloadreg, reloadreg)),
5079 first_output_reload_insn);
5080 special = 1;
5081 }
5082 else
5083 {
5084 /* See if we need both a scratch and intermediate reload
5085 register. */
5086 int secondary_reload = reload_secondary_reload[j];
5087 enum insn_code tertiary_icode
5088 = reload_secondary_icode[secondary_reload];
5089 rtx pat;
5090
5091 if (GET_MODE (reloadreg) != mode)
5092 reloadreg = gen_rtx (REG, mode, REGNO (reloadreg));
5093
5094 if (tertiary_icode != CODE_FOR_nothing)
5095 {
5096 rtx third_reloadreg
5097 = reload_reg_rtx[reload_secondary_reload[secondary_reload]];
5098 pat = (GEN_FCN (tertiary_icode)
5099 (reloadreg, second_reloadreg, third_reloadreg));
5100 }
5101 else
5102 pat = gen_move_insn (reloadreg, second_reloadreg);
5103
5104 emit_insn_before (pat, first_output_reload_insn);
5105 }
5106 }
5107#endif
5108
5109 /* Output the last reload insn. */
5110 if (! special)
5111 emit_insn_before (gen_move_insn (old, reloadreg),
5112 first_output_reload_insn);
5113
5114#ifdef PRESERVE_DEATH_INFO_REGNO_P
5115 /* If final will look at death notes for this reg,
5116 put one on the last output-reload insn to use it. Similarly
5117 for any secondary register. */
5118 if (PRESERVE_DEATH_INFO_REGNO_P (REGNO (reloadreg)))
5119 for (p = PREV_INSN (first_output_reload_insn);
5120 p != prev_insn; p = PREV_INSN (p))
5121 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
5122 && reg_overlap_mentioned_p (reloadreg, PATTERN (p)))
5123 REG_NOTES (p) = gen_rtx (EXPR_LIST, REG_DEAD,
5124 reloadreg, REG_NOTES (p));
5125
5126#ifdef SECONDARY_OUTPUT_RELOAD_CLASS
5127 if (! special
5128 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reloadreg)))
5129 for (p = PREV_INSN (first_output_reload_insn);
5130 p != prev_insn; p = PREV_INSN (p))
5131 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
5132 && reg_overlap_mentioned_p (second_reloadreg, PATTERN (p)))
5133 REG_NOTES (p) = gen_rtx (EXPR_LIST, REG_DEAD,
5134 second_reloadreg, REG_NOTES (p));
5135#endif
5136#endif
5137 /* Look at all insns we emitted, just to be safe. */
5138 for (p = NEXT_INSN (prev_insn); p != first_output_reload_insn;
5139 p = NEXT_INSN (p))
5140 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
5141 {
5142 /* If this output reload doesn't come from a spill reg,
5143 clear any memory of reloaded copies of the pseudo reg.
5144 If this output reload comes from a spill reg,
5145 reg_has_output_reload will make this do nothing. */
5146 note_stores (PATTERN (p), forget_old_reloads_1);
5147
5148 if (reg_mentioned_p (reload_reg_rtx[j], PATTERN (p)))
5149 store_insn = p;
5150 }
5151
5152 first_output_reload_insn = NEXT_INSN (prev_insn);
5153 }
5154
5155 if (reload_spill_index[j] >= 0)
5156 new_spill_reg_store[reload_spill_index[j]] = store_insn;
5157 }
5158
32131a9c
RK
5159 /* Move death notes from INSN
5160 to output-operand-address and output reload insns. */
5161#ifdef PRESERVE_DEATH_INFO_REGNO_P
5162 {
5163 rtx insn1;
5164 /* Loop over those insns, last ones first. */
5165 for (insn1 = PREV_INSN (following_insn); insn1 != insn;
5166 insn1 = PREV_INSN (insn1))
5167 if (GET_CODE (insn1) == INSN && GET_CODE (PATTERN (insn1)) == SET)
5168 {
5169 rtx source = SET_SRC (PATTERN (insn1));
5170 rtx dest = SET_DEST (PATTERN (insn1));
5171
5172 /* The note we will examine next. */
5173 rtx reg_notes = REG_NOTES (insn);
5174 /* The place that pointed to this note. */
5175 rtx *prev_reg_note = &REG_NOTES (insn);
5176
5177 /* If the note is for something used in the source of this
5178 reload insn, or in the output address, move the note. */
5179 while (reg_notes)
5180 {
5181 rtx next_reg_notes = XEXP (reg_notes, 1);
5182 if (REG_NOTE_KIND (reg_notes) == REG_DEAD
5183 && GET_CODE (XEXP (reg_notes, 0)) == REG
5184 && ((GET_CODE (dest) != REG
5185 && reg_overlap_mentioned_p (XEXP (reg_notes, 0), dest))
5186 || reg_overlap_mentioned_p (XEXP (reg_notes, 0), source)))
5187 {
5188 *prev_reg_note = next_reg_notes;
5189 XEXP (reg_notes, 1) = REG_NOTES (insn1);
5190 REG_NOTES (insn1) = reg_notes;
5191 }
5192 else
5193 prev_reg_note = &XEXP (reg_notes, 1);
5194
5195 reg_notes = next_reg_notes;
5196 }
5197 }
5198 }
5199#endif
5200
5201 /* For all the spill regs newly reloaded in this instruction,
5202 record what they were reloaded from, so subsequent instructions
d445b551
RK
5203 can inherit the reloads.
5204
5205 Update spill_reg_store for the reloads of this insn.
5206 Copy the elements that were updated in the loop above. */
32131a9c
RK
5207
5208 for (j = 0; j < n_reloads; j++)
5209 {
5210 register int r = reload_order[j];
5211 register int i = reload_spill_index[r];
5212
5213 /* I is nonneg if this reload used one of the spill regs.
5214 If reload_reg_rtx[r] is 0, this is an optional reload
5215 that we opted to ignore. */
d445b551 5216
32131a9c
RK
5217 if (i >= 0 && reload_reg_rtx[r] != 0)
5218 {
5219 /* First, clear out memory of what used to be in this spill reg.
5220 If consecutive registers are used, clear them all. */
5221 int nr
5222 = HARD_REGNO_NREGS (spill_regs[i], GET_MODE (reload_reg_rtx[r]));
5223 int k;
5224
5225 for (k = 0; k < nr; k++)
5226 {
5227 reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]] = -1;
5228 reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = 0;
5229 }
5230
5231 /* Maybe the spill reg contains a copy of reload_out. */
5232 if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
5233 {
5234 register int nregno = REGNO (reload_out[r]);
d445b551
RK
5235
5236 spill_reg_store[i] = new_spill_reg_store[i];
32131a9c 5237 reg_last_reload_reg[nregno] = reload_reg_rtx[r];
d445b551 5238
32131a9c
RK
5239 for (k = 0; k < nr; k++)
5240 {
5241 reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
5242 = nregno;
5243 reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = insn;
5244 }
5245 }
d445b551 5246
32131a9c
RK
5247 /* Maybe the spill reg contains a copy of reload_in. */
5248 else if (reload_out[r] == 0
5249 && reload_in[r] != 0
5250 && (GET_CODE (reload_in[r]) == REG
5251 || GET_CODE (reload_in_reg[r]) == REG))
5252 {
5253 register int nregno;
5254 if (GET_CODE (reload_in[r]) == REG)
5255 nregno = REGNO (reload_in[r]);
5256 else
5257 nregno = REGNO (reload_in_reg[r]);
5258
5259 /* If there are two separate reloads (one in and one out)
5260 for the same (hard or pseudo) reg,
5261 leave reg_last_reload_reg set
5262 based on the output reload.
5263 Otherwise, set it from this input reload. */
5264 if (!reg_has_output_reload[nregno]
5265 /* But don't do so if another input reload
5266 will clobber this one's value. */
5267 && reload_reg_reaches_end_p (spill_regs[i],
5268 reload_when_needed[r]))
5269 {
5270 reg_last_reload_reg[nregno] = reload_reg_rtx[r];
d445b551
RK
5271
5272 /* Unless we inherited this reload, show we haven't
5273 recently done a store. */
5274 if (! reload_inherited[r])
5275 spill_reg_store[i] = 0;
5276
32131a9c
RK
5277 for (k = 0; k < nr; k++)
5278 {
5279 reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
5280 = nregno;
5281 reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]]
5282 = insn;
5283 }
5284 }
5285 }
5286 }
5287
5288 /* The following if-statement was #if 0'd in 1.34 (or before...).
5289 It's reenabled in 1.35 because supposedly nothing else
5290 deals with this problem. */
5291
5292 /* If a register gets output-reloaded from a non-spill register,
5293 that invalidates any previous reloaded copy of it.
5294 But forget_old_reloads_1 won't get to see it, because
5295 it thinks only about the original insn. So invalidate it here. */
5296 if (i < 0 && reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
5297 {
5298 register int nregno = REGNO (reload_out[r]);
5299 reg_last_reload_reg[nregno] = 0;
5300 }
5301 }
5302}
5303\f
5304/* Emit code before BEFORE_INSN to perform an input reload of IN to RELOADREG.
d445b551 5305 Returns first insn emitted. */
32131a9c
RK
5306
5307rtx
5308gen_input_reload (reloadreg, in, before_insn)
5309 rtx reloadreg;
5310 rtx in;
5311 rtx before_insn;
5312{
5313 register rtx prev_insn = PREV_INSN (before_insn);
5314
5315 /* How to do this reload can get quite tricky. Normally, we are being
5316 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
5317 register that didn't get a hard register. In that case we can just
5318 call emit_move_insn.
5319
5320 We can also be asked to reload a PLUS that adds either two registers or
5321 a register and a constant or MEM. This can occur during frame pointer
5322 elimination. That case if handled by trying to emit a single insn
5323 to perform the add. If it is not valid, we use a two insn sequence.
5324
5325 Finally, we could be called to handle an 'o' constraint by putting
5326 an address into a register. In that case, we first try to do this
5327 with a named pattern of "reload_load_address". If no such pattern
5328 exists, we just emit a SET insn and hope for the best (it will normally
5329 be valid on machines that use 'o').
5330
5331 This entire process is made complex because reload will never
5332 process the insns we generate here and so we must ensure that
5333 they will fit their constraints and also by the fact that parts of
5334 IN might be being reloaded separately and replaced with spill registers.
5335 Because of this, we are, in some sense, just guessing the right approach
5336 here. The one listed above seems to work.
5337
5338 ??? At some point, this whole thing needs to be rethought. */
5339
5340 if (GET_CODE (in) == PLUS
5341 && GET_CODE (XEXP (in, 0)) == REG
5342 && (GET_CODE (XEXP (in, 1)) == REG
5343 || CONSTANT_P (XEXP (in, 1))
5344 || GET_CODE (XEXP (in, 1)) == MEM))
5345 {
5346 /* We need to compute the sum of what is either a register and a
5347 constant, a register and memory, or a hard register and a pseudo
5348 register and put it into the reload register. The best possible way
5349 of doing this is if the machine has a three-operand ADD insn that
5350 accepts the required operands.
5351
5352 The simplest approach is to try to generate such an insn and see if it
5353 is recognized and matches its constraints. If so, it can be used.
5354
5355 It might be better not to actually emit the insn unless it is valid,
5356 but we need to pass the insn as an operand to `recog' and it is
5357 simpler to emit and then delete the insn if not valid than to
5358 dummy things up. */
5359
5360 rtx move_operand, other_operand, insn;
5361 int code;
5362
5363 /* Since constraint checking is strict, commutativity won't be
5364 checked, so we need to do that here to avoid spurious failure
5365 if the add instruction is two-address and the second operand
5366 of the add is the same as the reload reg, which is frequently
5367 the case. If the insn would be A = B + A, rearrange it so
5368 it will be A = A + B as constrain_operands expects. */
5369
5370 if (GET_CODE (XEXP (in, 1)) == REG
5371 && REGNO (reloadreg) == REGNO (XEXP (in, 1)))
5372 in = gen_rtx (PLUS, GET_MODE (in), XEXP (in, 1), XEXP (in, 0));
5373
5374 insn = emit_insn_before (gen_rtx (SET, VOIDmode, reloadreg, in),
5375 before_insn);
5376 code = recog_memoized (insn);
5377
5378 if (code >= 0)
5379 {
5380 insn_extract (insn);
5381 /* We want constrain operands to treat this insn strictly in
5382 its validity determination, i.e., the way it would after reload
5383 has completed. */
5384 if (constrain_operands (code, 1))
5385 return insn;
5386 }
5387
5388 if (PREV_INSN (insn))
5389 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
5390 if (NEXT_INSN (insn))
5391 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
5392
5393 /* If that failed, we must use a conservative two-insn sequence.
5394 use move to copy constant, MEM, or pseudo register to the reload
5395 register since "move" will be able to handle arbitrary operand, unlike
5396 add which can't, in general. Then add the registers.
5397
5398 If there is another way to do this for a specific machine, a
5399 DEFINE_PEEPHOLE should be specified that recognizes the sequence
5400 we emit below. */
5401
5402 if (CONSTANT_P (XEXP (in, 1))
5403 || (GET_CODE (XEXP (in, 1)) == REG
5404 && REGNO (XEXP (in, 1)) >= FIRST_PSEUDO_REGISTER))
5405 move_operand = XEXP (in, 1), other_operand = XEXP (in, 0);
5406 else
5407 move_operand = XEXP (in, 0), other_operand = XEXP (in, 1);
5408
5409 emit_insn_before (gen_move_insn (reloadreg, move_operand), before_insn);
5410 emit_insn_before (gen_add2_insn (reloadreg, other_operand), before_insn);
5411 }
5412
5413 /* If IN is a simple operand, use gen_move_insn. */
5414 else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
5415 emit_insn_before (gen_move_insn (reloadreg, in), before_insn);
5416
5417#ifdef HAVE_reload_load_address
5418 else if (HAVE_reload_load_address)
5419 emit_insn_before (gen_reload_load_address (reloadreg, in), before_insn);
5420#endif
5421
5422 /* Otherwise, just write (set REGLOADREG IN) and hope for the best. */
5423 else
5424 emit_insn_before (gen_rtx (SET, VOIDmode, reloadreg, in), before_insn);
5425
5426 /* Return the first insn emitted.
5427 We can not just return PREV_INSN (before_insn), because there may have
5428 been multiple instructions emitted. Also note that gen_move_insn may
5429 emit more than one insn itself, so we can not assume that there is one
5430 insn emitted per emit_insn_before call. */
5431
5432 return NEXT_INSN (prev_insn);
5433}
5434\f
5435/* Delete a previously made output-reload
5436 whose result we now believe is not needed.
5437 First we double-check.
5438
5439 INSN is the insn now being processed.
5440 OUTPUT_RELOAD_INSN is the insn of the output reload.
5441 J is the reload-number for this insn. */
5442
5443static void
5444delete_output_reload (insn, j, output_reload_insn)
5445 rtx insn;
5446 int j;
5447 rtx output_reload_insn;
5448{
5449 register rtx i1;
5450
5451 /* Get the raw pseudo-register referred to. */
5452
5453 rtx reg = reload_in[j];
5454 while (GET_CODE (reg) == SUBREG)
5455 reg = SUBREG_REG (reg);
5456
5457 /* If the pseudo-reg we are reloading is no longer referenced
5458 anywhere between the store into it and here,
5459 and no jumps or labels intervene, then the value can get
5460 here through the reload reg alone.
5461 Otherwise, give up--return. */
5462 for (i1 = NEXT_INSN (output_reload_insn);
5463 i1 != insn; i1 = NEXT_INSN (i1))
5464 {
5465 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
5466 return;
5467 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
5468 && reg_mentioned_p (reg, PATTERN (i1)))
5469 return;
5470 }
5471
5472 /* If this insn will store in the pseudo again,
5473 the previous store can be removed. */
5474 if (reload_out[j] == reload_in[j])
5475 delete_insn (output_reload_insn);
5476
5477 /* See if the pseudo reg has been completely replaced
5478 with reload regs. If so, delete the store insn
5479 and forget we had a stack slot for the pseudo. */
5480 else if (reg_n_deaths[REGNO (reg)] == 1
5481 && reg_basic_block[REGNO (reg)] >= 0
5482 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
5483 {
5484 rtx i2;
5485
5486 /* We know that it was used only between here
5487 and the beginning of the current basic block.
5488 (We also know that the last use before INSN was
5489 the output reload we are thinking of deleting, but never mind that.)
5490 Search that range; see if any ref remains. */
5491 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
5492 {
d445b551
RK
5493 rtx set = single_set (i2);
5494
32131a9c
RK
5495 /* Uses which just store in the pseudo don't count,
5496 since if they are the only uses, they are dead. */
d445b551 5497 if (set != 0 && SET_DEST (set) == reg)
32131a9c
RK
5498 continue;
5499 if (GET_CODE (i2) == CODE_LABEL
5500 || GET_CODE (i2) == JUMP_INSN)
5501 break;
5502 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
5503 && reg_mentioned_p (reg, PATTERN (i2)))
5504 /* Some other ref remains;
5505 we can't do anything. */
5506 return;
5507 }
5508
5509 /* Delete the now-dead stores into this pseudo. */
5510 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
5511 {
d445b551
RK
5512 rtx set = single_set (i2);
5513
5514 if (set != 0 && SET_DEST (set) == reg)
32131a9c
RK
5515 delete_insn (i2);
5516 if (GET_CODE (i2) == CODE_LABEL
5517 || GET_CODE (i2) == JUMP_INSN)
5518 break;
5519 }
5520
5521 /* For the debugging info,
5522 say the pseudo lives in this reload reg. */
5523 reg_renumber[REGNO (reg)] = REGNO (reload_reg_rtx[j]);
5524 alter_reg (REGNO (reg), -1);
5525 }
5526}
5527
5528\f
5529/* Output reload-insns to reload VALUE into RELOADREG.
5530 VALUE is a autoincrement or autodecrement RTX whose operand
5531 is a register or memory location;
5532 so reloading involves incrementing that location.
5533
5534 INC_AMOUNT is the number to increment or decrement by (always positive).
5535 This cannot be deduced from VALUE.
5536
5537 INSN is the insn before which the new insns should be emitted.
5538
5539 The return value is the first of the insns emitted. */
5540
5541static rtx
5542inc_for_reload (reloadreg, value, inc_amount, insn)
5543 rtx reloadreg;
5544 rtx value;
5545 int inc_amount;
5546 rtx insn;
5547{
5548 /* REG or MEM to be copied and incremented. */
5549 rtx incloc = XEXP (value, 0);
5550 /* Nonzero if increment after copying. */
5551 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
5552
5553 /* No hard register is equivalent to this register after
5554 inc/dec operation. If REG_LAST_RELOAD_REG were non-zero,
5555 we could inc/dec that register as well (maybe even using it for
5556 the source), but I'm not sure it's worth worrying about. */
5557 if (GET_CODE (incloc) == REG)
5558 reg_last_reload_reg[REGNO (incloc)] = 0;
5559
5560 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
5561 inc_amount = - inc_amount;
5562
5563 /* First handle preincrement, which is simpler. */
5564 if (! post)
5565 {
5566 /* If incrementing a register, assume we can
5567 output an insn to increment it directly. */
5568 if (GET_CODE (incloc) == REG &&
5569 (REGNO (incloc) < FIRST_PSEUDO_REGISTER
5570 || reg_renumber[REGNO (incloc)] >= 0))
5571 {
5572 rtx first_new
5573 = emit_insn_before (gen_add2_insn (incloc,
5574 gen_rtx (CONST_INT, VOIDmode,
5575 inc_amount)),
5576 insn);
5577 emit_insn_before (gen_move_insn (reloadreg, incloc), insn);
5578 return first_new;
5579 }
5580 else
5581 /* Else we must not assume we can increment the location directly
5582 (even though on many target machines we can);
5583 copy it to the reload register, increment there, then save back. */
5584 {
5585 rtx first_new
5586 = emit_insn_before (gen_move_insn (reloadreg, incloc), insn);
5587 emit_insn_before (gen_add2_insn (reloadreg,
5588 gen_rtx (CONST_INT, VOIDmode,
5589 inc_amount)),
5590 insn);
5591 emit_insn_before (gen_move_insn (incloc, reloadreg), insn);
5592 return first_new;
5593 }
5594 }
5595 /* Postincrement.
5596 Because this might be a jump insn or a compare, and because RELOADREG
5597 may not be available after the insn in an input reload,
5598 we must do the incrementation before the insn being reloaded for. */
5599 else
5600 {
5601 /* Copy the value, then increment it. */
5602 rtx first_new
5603 = emit_insn_before (gen_move_insn (reloadreg, incloc), insn);
5604
5605 /* If incrementing a register, assume we can
5606 output an insn to increment it directly. */
5607 if (GET_CODE (incloc) == REG &&
5608 (REGNO (incloc) < FIRST_PSEUDO_REGISTER
5609 || reg_renumber[REGNO (incloc)] >= 0))
5610 {
5611 emit_insn_before (gen_add2_insn (incloc,
5612 gen_rtx (CONST_INT, VOIDmode,
5613 inc_amount)),
5614 insn);
5615 }
5616 else
5617 /* Else we must not assume we can increment INCLOC
5618 (even though on many target machines we can);
5619 increment the copy in the reload register,
5620 save that back, then decrement the reload register
5621 so it has the original value. */
5622 {
5623 emit_insn_before (gen_add2_insn (reloadreg,
5624 gen_rtx (CONST_INT, VOIDmode,
5625 inc_amount)),
5626 insn);
5627 emit_insn_before (gen_move_insn (incloc, reloadreg), insn);
5628 emit_insn_before (gen_sub2_insn (reloadreg,
5629 gen_rtx (CONST_INT, VOIDmode,
5630 inc_amount)),
5631 insn);
5632 }
5633 return first_new;
5634 }
5635}
5636\f
5637/* Return 1 if we are certain that the constraint-string STRING allows
5638 the hard register REG. Return 0 if we can't be sure of this. */
5639
5640static int
5641constraint_accepts_reg_p (string, reg)
5642 char *string;
5643 rtx reg;
5644{
5645 int value = 0;
5646 int regno = true_regnum (reg);
5647 int c;
5648
5649 /* Initialize for first alternative. */
5650 value = 0;
5651 /* Check that each alternative contains `g' or `r'. */
5652 while (1)
5653 switch (c = *string++)
5654 {
5655 case 0:
5656 /* If an alternative lacks `g' or `r', we lose. */
5657 return value;
5658 case ',':
5659 /* If an alternative lacks `g' or `r', we lose. */
5660 if (value == 0)
5661 return 0;
5662 /* Initialize for next alternative. */
5663 value = 0;
5664 break;
5665 case 'g':
5666 case 'r':
5667 /* Any general reg wins for this alternative. */
5668 if (TEST_HARD_REG_BIT (reg_class_contents[(int) GENERAL_REGS], regno))
5669 value = 1;
5670 break;
5671 default:
5672 /* Any reg in specified class wins for this alternative. */
5673 {
5674 int class = REG_CLASS_FROM_LETTER (c);
5675
5676 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno))
5677 value = 1;
5678 }
5679 }
5680}
5681\f
d445b551
RK
5682/* Return the number of places FIND appears within X, but don't count
5683 an occurrence if some SET_DEST is FIND. */
32131a9c
RK
5684
5685static int
5686count_occurrences (x, find)
5687 register rtx x, find;
5688{
5689 register int i, j;
5690 register enum rtx_code code;
5691 register char *format_ptr;
5692 int count;
5693
5694 if (x == find)
5695 return 1;
5696 if (x == 0)
5697 return 0;
5698
5699 code = GET_CODE (x);
5700
5701 switch (code)
5702 {
5703 case REG:
5704 case QUEUED:
5705 case CONST_INT:
5706 case CONST_DOUBLE:
5707 case SYMBOL_REF:
5708 case CODE_LABEL:
5709 case PC:
5710 case CC0:
5711 return 0;
d445b551
RK
5712
5713 case SET:
5714 if (SET_DEST (x) == find)
5715 return count_occurrences (SET_SRC (x), find);
5716 break;
32131a9c
RK
5717 }
5718
5719 format_ptr = GET_RTX_FORMAT (code);
5720 count = 0;
5721
5722 for (i = 0; i < GET_RTX_LENGTH (code); i++)
5723 {
5724 switch (*format_ptr++)
5725 {
5726 case 'e':
5727 count += count_occurrences (XEXP (x, i), find);
5728 break;
5729
5730 case 'E':
5731 if (XVEC (x, i) != NULL)
5732 {
5733 for (j = 0; j < XVECLEN (x, i); j++)
5734 count += count_occurrences (XVECEXP (x, i, j), find);
5735 }
5736 break;
5737 }
5738 }
5739 return count;
5740}
This page took 0.593473 seconds and 5 git commands to generate.