]> gcc.gnu.org Git - gcc.git/blame - gcc/reload1.c
[multiple changes]
[gcc.git] / gcc / reload1.c
CommitLineData
32131a9c 1/* Reload pseudo regs into hard regs for insns that require hard regs.
f5963e61 2 Copyright (C) 1987, 88, 89, 92-97, 1998 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
e99215a3
RK
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
32131a9c
RK
20
21
22#include "config.h"
670ee920 23#include "system.h"
cab634f2
KG
24
25#include "machmode.h"
26#include "hard-reg-set.h"
32131a9c
RK
27#include "rtl.h"
28#include "obstack.h"
29#include "insn-config.h"
30#include "insn-flags.h"
31#include "insn-codes.h"
32#include "flags.h"
33#include "expr.h"
34#include "regs.h"
32131a9c
RK
35#include "reload.h"
36#include "recog.h"
37#include "basic-block.h"
38#include "output.h"
a9c366bf 39#include "real.h"
10f0ad3d 40#include "toplev.h"
32131a9c
RK
41
42/* This file contains the reload pass of the compiler, which is
43 run after register allocation has been done. It checks that
44 each insn is valid (operands required to be in registers really
45 are in registers of the proper class) and fixes up invalid ones
46 by copying values temporarily into registers for the insns
47 that need them.
48
49 The results of register allocation are described by the vector
50 reg_renumber; the insns still contain pseudo regs, but reg_renumber
51 can be used to find which hard reg, if any, a pseudo reg is in.
52
53 The technique we always use is to free up a few hard regs that are
54 called ``reload regs'', and for each place where a pseudo reg
55 must be in a hard reg, copy it temporarily into one of the reload regs.
56
57 All the pseudos that were formerly allocated to the hard regs that
58 are now in use as reload regs must be ``spilled''. This means
59 that they go to other hard regs, or to stack slots if no other
60 available hard regs can be found. Spilling can invalidate more
61 insns, requiring additional need for reloads, so we must keep checking
62 until the process stabilizes.
63
64 For machines with different classes of registers, we must keep track
65 of the register class needed for each reload, and make sure that
66 we allocate enough reload registers of each class.
67
68 The file reload.c contains the code that checks one insn for
69 validity and reports the reloads that it needs. This file
70 is in charge of scanning the entire rtl code, accumulating the
71 reload needs, spilling, assigning reload registers to use for
72 fixing up each insn, and generating the new insns to copy values
73 into the reload registers. */
546b63fb
RK
74
75
76#ifndef REGISTER_MOVE_COST
77#define REGISTER_MOVE_COST(x, y) 2
78#endif
32131a9c
RK
79\f
80/* During reload_as_needed, element N contains a REG rtx for the hard reg
0f41302f 81 into which reg N has been reloaded (perhaps for a previous insn). */
32131a9c
RK
82static rtx *reg_last_reload_reg;
83
84/* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
85 for an output reload that stores into reg N. */
86static char *reg_has_output_reload;
87
88/* Indicates which hard regs are reload-registers for an output reload
89 in the current insn. */
90static HARD_REG_SET reg_is_output_reload;
91
92/* Element N is the constant value to which pseudo reg N is equivalent,
93 or zero if pseudo reg N is not equivalent to a constant.
94 find_reloads looks at this in order to replace pseudo reg N
95 with the constant it stands for. */
96rtx *reg_equiv_constant;
97
98/* Element N is a memory location to which pseudo reg N is equivalent,
99 prior to any register elimination (such as frame pointer to stack
100 pointer). Depending on whether or not it is a valid address, this value
101 is transferred to either reg_equiv_address or reg_equiv_mem. */
4803a34a 102rtx *reg_equiv_memory_loc;
32131a9c
RK
103
104/* Element N is the address of stack slot to which pseudo reg N is equivalent.
105 This is used when the address is not valid as a memory address
106 (because its displacement is too big for the machine.) */
107rtx *reg_equiv_address;
108
109/* Element N is the memory slot to which pseudo reg N is equivalent,
110 or zero if pseudo reg N is not equivalent to a memory slot. */
111rtx *reg_equiv_mem;
112
113/* Widest width in which each pseudo reg is referred to (via subreg). */
114static int *reg_max_ref_width;
115
116/* Element N is the insn that initialized reg N from its equivalent
117 constant or memory slot. */
118static rtx *reg_equiv_init;
119
e6e52be0
R
120/* During reload_as_needed, element N contains the last pseudo regno reloaded
121 into hard register N. If that pseudo reg occupied more than one register,
32131a9c
RK
122 reg_reloaded_contents points to that pseudo for each spill register in
123 use; all of these must remain set for an inheritance to occur. */
124static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
125
126/* During reload_as_needed, element N contains the insn for which
e6e52be0
R
127 hard register N was last used. Its contents are significant only
128 when reg_reloaded_valid is set for this register. */
32131a9c
RK
129static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
130
e6e52be0
R
131/* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid */
132static HARD_REG_SET reg_reloaded_valid;
133/* Indicate if the register was dead at the end of the reload.
134 This is only valid if reg_reloaded_contents is set and valid. */
135static HARD_REG_SET reg_reloaded_dead;
136
32131a9c
RK
137/* Number of spill-regs so far; number of valid elements of spill_regs. */
138static int n_spills;
139
140/* In parallel with spill_regs, contains REG rtx's for those regs.
141 Holds the last rtx used for any given reg, or 0 if it has never
142 been used for spilling yet. This rtx is reused, provided it has
143 the proper mode. */
144static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
145
146/* In parallel with spill_regs, contains nonzero for a spill reg
147 that was stored after the last time it was used.
148 The precise value is the insn generated to do the store. */
149static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
150
151/* This table is the inverse mapping of spill_regs:
152 indexed by hard reg number,
153 it contains the position of that reg in spill_regs,
154 or -1 for something that is not in spill_regs. */
155static short spill_reg_order[FIRST_PSEUDO_REGISTER];
156
157/* This reg set indicates registers that may not be used for retrying global
158 allocation. The registers that may not be used include all spill registers
159 and the frame pointer (if we are using one). */
160HARD_REG_SET forbidden_regs;
161
162/* This reg set indicates registers that are not good for spill registers.
163 They will not be used to complete groups of spill registers. This includes
546b63fb 164 all fixed registers, registers that may be eliminated, and, if
e9a25f70 165 SMALL_REGISTER_CLASSES is zero, registers explicitly used in the rtl.
32131a9c
RK
166
167 (spill_reg_order prevents these registers from being used to start a
168 group.) */
169static HARD_REG_SET bad_spill_regs;
170
171/* Describes order of use of registers for reloading
172 of spilled pseudo-registers. `spills' is the number of
173 elements that are actually valid; new ones are added at the end. */
174static short spill_regs[FIRST_PSEUDO_REGISTER];
175
8b4f9969
JW
176/* This reg set indicates those registers that have been used a spill
177 registers. This information is used in reorg.c, to help figure out
178 what registers are live at any point. It is assumed that all spill_regs
179 are dead at every CODE_LABEL. */
180
181HARD_REG_SET used_spill_regs;
182
4079cd63
JW
183/* Index of last register assigned as a spill register. We allocate in
184 a round-robin fashion. */
185
186static int last_spill_reg;
187
32131a9c
RK
188/* Describes order of preference for putting regs into spill_regs.
189 Contains the numbers of all the hard regs, in order most preferred first.
190 This order is different for each function.
191 It is set up by order_regs_for_reload.
192 Empty elements at the end contain -1. */
193static short potential_reload_regs[FIRST_PSEUDO_REGISTER];
194
195/* 1 for a hard register that appears explicitly in the rtl
196 (for example, function value registers, special registers
197 used by insns, structure value pointer registers). */
198static char regs_explicitly_used[FIRST_PSEUDO_REGISTER];
199
200/* Indicates if a register was counted against the need for
201 groups. 0 means it can count against max_nongroup instead. */
202static HARD_REG_SET counted_for_groups;
203
204/* Indicates if a register was counted against the need for
205 non-groups. 0 means it can become part of a new group.
206 During choose_reload_regs, 1 here means don't use this reg
207 as part of a group, even if it seems to be otherwise ok. */
208static HARD_REG_SET counted_for_nongroups;
209
210/* Nonzero if indirect addressing is supported on the machine; this means
211 that spilling (REG n) does not require reloading it into a register in
212 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
213 value indicates the level of indirect addressing supported, e.g., two
214 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
215 a hard register. */
216
217static char spill_indirect_levels;
218
219/* Nonzero if indirect addressing is supported when the innermost MEM is
220 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
221 which these are valid is the same as spill_indirect_levels, above. */
222
223char indirect_symref_ok;
224
225/* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
226
227char double_reg_address_ok;
228
229/* Record the stack slot for each spilled hard register. */
230
231static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
232
233/* Width allocated so far for that stack slot. */
234
235static int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
236
237/* Indexed by register class and basic block number, nonzero if there is
238 any need for a spill register of that class in that basic block.
239 The pointer is 0 if we did stupid allocation and don't know
240 the structure of basic blocks. */
241
242char *basic_block_needs[N_REG_CLASSES];
243
244/* First uid used by insns created by reload in this function.
245 Used in find_equiv_reg. */
246int reload_first_uid;
247
248/* Flag set by local-alloc or global-alloc if anything is live in
249 a call-clobbered reg across calls. */
250
251int caller_save_needed;
252
7402683f
ILT
253/* The register class to use for a base register when reloading an
254 address. This is normally BASE_REG_CLASS, but it may be different
255 when using SMALL_REGISTER_CLASSES and passing parameters in
256 registers. */
257enum reg_class reload_address_base_reg_class;
258
259/* The register class to use for an index register when reloading an
260 address. This is normally INDEX_REG_CLASS, but it may be different
261 when using SMALL_REGISTER_CLASSES and passing parameters in
262 registers. */
263enum reg_class reload_address_index_reg_class;
264
32131a9c
RK
265/* Set to 1 while reload_as_needed is operating.
266 Required by some machines to handle any generated moves differently. */
267
268int reload_in_progress = 0;
269
270/* These arrays record the insn_code of insns that may be needed to
271 perform input and output reloads of special objects. They provide a
272 place to pass a scratch register. */
273
274enum insn_code reload_in_optab[NUM_MACHINE_MODES];
275enum insn_code reload_out_optab[NUM_MACHINE_MODES];
276
d45cf215 277/* This obstack is used for allocation of rtl during register elimination.
32131a9c
RK
278 The allocated storage can be freed once find_reloads has processed the
279 insn. */
280
281struct obstack reload_obstack;
282char *reload_firstobj;
283
284#define obstack_chunk_alloc xmalloc
285#define obstack_chunk_free free
286
32131a9c
RK
287/* List of labels that must never be deleted. */
288extern rtx forced_labels;
2c5d9e37
RK
289
290/* Allocation number table from global register allocation. */
291extern int *reg_allocno;
32131a9c
RK
292\f
293/* This structure is used to record information about register eliminations.
294 Each array entry describes one possible way of eliminating a register
295 in favor of another. If there is more than one way of eliminating a
296 particular register, the most preferred should be specified first. */
297
298static struct elim_table
299{
0f41302f
MS
300 int from; /* Register number to be eliminated. */
301 int to; /* Register number used as replacement. */
302 int initial_offset; /* Initial difference between values. */
303 int can_eliminate; /* Non-zero if this elimination can be done. */
32131a9c 304 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
0f41302f
MS
305 insns made by reload. */
306 int offset; /* Current offset between the two regs. */
307 int max_offset; /* Maximum offset between the two regs. */
308 int previous_offset; /* Offset at end of previous insn. */
309 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
32131a9c
RK
310 rtx from_rtx; /* REG rtx for the register to be eliminated.
311 We cannot simply compare the number since
312 we might then spuriously replace a hard
313 register corresponding to a pseudo
0f41302f
MS
314 assigned to the reg to be eliminated. */
315 rtx to_rtx; /* REG rtx for the replacement. */
32131a9c
RK
316} reg_eliminate[] =
317
318/* If a set of eliminable registers was specified, define the table from it.
319 Otherwise, default to the normal case of the frame pointer being
320 replaced by the stack pointer. */
321
322#ifdef ELIMINABLE_REGS
323 ELIMINABLE_REGS;
324#else
325 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
326#endif
327
328#define NUM_ELIMINABLE_REGS (sizeof reg_eliminate / sizeof reg_eliminate[0])
329
330/* Record the number of pending eliminations that have an offset not equal
331 to their initial offset. If non-zero, we use a new copy of each
332 replacement result in any insns encountered. */
333static int num_not_at_initial_offset;
334
335/* Count the number of registers that we may be able to eliminate. */
336static int num_eliminable;
337
338/* For each label, we record the offset of each elimination. If we reach
339 a label by more than one path and an offset differs, we cannot do the
340 elimination. This information is indexed by the number of the label.
341 The first table is an array of flags that records whether we have yet
342 encountered a label and the second table is an array of arrays, one
343 entry in the latter array for each elimination. */
344
345static char *offsets_known_at;
346static int (*offsets_at)[NUM_ELIMINABLE_REGS];
347
348/* Number of labels in the current function. */
349
350static int num_labels;
546b63fb
RK
351
352struct hard_reg_n_uses { int regno; int uses; };
32131a9c 353\f
546b63fb
RK
354static int possible_group_p PROTO((int, int *));
355static void count_possible_groups PROTO((int *, enum machine_mode *,
066aca28 356 int *, int));
546b63fb
RK
357static int modes_equiv_for_class_p PROTO((enum machine_mode,
358 enum machine_mode,
359 enum reg_class));
360static void spill_failure PROTO((rtx));
361static int new_spill_reg PROTO((int, int, int *, int *, int,
362 FILE *));
363static void delete_dead_insn PROTO((rtx));
364static void alter_reg PROTO((int, int));
c307c237 365static void mark_scratch_live PROTO((rtx));
546b63fb
RK
366static void set_label_offsets PROTO((rtx, rtx, int));
367static int eliminate_regs_in_insn PROTO((rtx, int));
368static void mark_not_eliminable PROTO((rtx, rtx));
369static int spill_hard_reg PROTO((int, int, FILE *, int));
370static void scan_paradoxical_subregs PROTO((rtx));
788a0818 371static int hard_reg_use_compare PROTO((const GENERIC_PTR, const GENERIC_PTR));
2c5d9e37 372static void order_regs_for_reload PROTO((int));
788a0818 373static int compare_spill_regs PROTO((const GENERIC_PTR, const GENERIC_PTR));
546b63fb 374static void reload_as_needed PROTO((rtx, int));
9a881562 375static void forget_old_reloads_1 PROTO((rtx, rtx));
788a0818 376static int reload_reg_class_lower PROTO((const GENERIC_PTR, const GENERIC_PTR));
546b63fb
RK
377static void mark_reload_reg_in_use PROTO((int, int, enum reload_type,
378 enum machine_mode));
be7ae2a4
RK
379static void clear_reload_reg_in_use PROTO((int, int, enum reload_type,
380 enum machine_mode));
546b63fb
RK
381static int reload_reg_free_p PROTO((int, int, enum reload_type));
382static int reload_reg_free_before_p PROTO((int, int, enum reload_type));
f5470689 383static int reload_reg_free_for_value_p PROTO((int, int, enum reload_type, rtx, rtx, int));
546b63fb
RK
384static int reload_reg_reaches_end_p PROTO((int, int, enum reload_type));
385static int allocate_reload_reg PROTO((int, rtx, int, int));
386static void choose_reload_regs PROTO((rtx, rtx));
387static void merge_assigned_reloads PROTO((rtx));
388static void emit_reload_insns PROTO((rtx));
389static void delete_output_reload PROTO((rtx, int, rtx));
390static void inc_for_reload PROTO((rtx, rtx, int));
391static int constraint_accepts_reg_p PROTO((char *, rtx));
2a9fb548 392static void reload_cse_invalidate_regno PROTO((int, enum machine_mode, int));
cbfc3ad3 393static int reload_cse_mem_conflict_p PROTO((rtx, rtx));
2a9fb548
ILT
394static void reload_cse_invalidate_mem PROTO((rtx));
395static void reload_cse_invalidate_rtx PROTO((rtx, rtx));
2a9fb548 396static int reload_cse_regno_equal_p PROTO((int, rtx, enum machine_mode));
31418d35 397static int reload_cse_noop_set_p PROTO((rtx, rtx));
e9a25f70
JL
398static int reload_cse_simplify_set PROTO((rtx, rtx));
399static int reload_cse_simplify_operands PROTO((rtx));
2a9fb548
ILT
400static void reload_cse_check_clobber PROTO((rtx, rtx));
401static void reload_cse_record_set PROTO((rtx, rtx));
e9a25f70
JL
402static void reload_cse_delete_death_notes PROTO((rtx));
403static void reload_cse_no_longer_dead PROTO((int, enum machine_mode));
32131a9c 404\f
546b63fb
RK
405/* Initialize the reload pass once per compilation. */
406
32131a9c
RK
407void
408init_reload ()
409{
410 register int i;
411
412 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
413 Set spill_indirect_levels to the number of levels such addressing is
414 permitted, zero if it is not permitted at all. */
415
416 register rtx tem
38a448ca
RH
417 = gen_rtx_MEM (Pmode,
418 gen_rtx_PLUS (Pmode,
419 gen_rtx_REG (Pmode, LAST_VIRTUAL_REGISTER + 1),
420 GEN_INT (4)));
32131a9c
RK
421 spill_indirect_levels = 0;
422
423 while (memory_address_p (QImode, tem))
424 {
425 spill_indirect_levels++;
38a448ca 426 tem = gen_rtx_MEM (Pmode, tem);
32131a9c
RK
427 }
428
429 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
430
38a448ca 431 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
32131a9c
RK
432 indirect_symref_ok = memory_address_p (QImode, tem);
433
434 /* See if reg+reg is a valid (and offsettable) address. */
435
65701fd2 436 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
57caa638 437 {
38a448ca
RH
438 tem = gen_rtx_PLUS (Pmode,
439 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
440 gen_rtx_REG (Pmode, i));
57caa638
RS
441 /* This way, we make sure that reg+reg is an offsettable address. */
442 tem = plus_constant (tem, 4);
443
444 if (memory_address_p (QImode, tem))
445 {
446 double_reg_address_ok = 1;
447 break;
448 }
449 }
32131a9c 450
0f41302f 451 /* Initialize obstack for our rtl allocation. */
32131a9c
RK
452 gcc_obstack_init (&reload_obstack);
453 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
7402683f
ILT
454
455 /* Decide which register class should be used when reloading
456 addresses. If we are using SMALL_REGISTER_CLASSES, and any
457 parameters are passed in registers, then we do not want to use
458 those registers when reloading an address. Otherwise, if a
459 function argument needs a reload, we may wind up clobbering
460 another argument to the function which was already computed. If
461 we find a subset class which simply avoids those registers, we
462 use it instead. ??? It would be better to only use the
463 restricted class when we actually are loading function arguments,
464 but that is hard to determine. */
465 reload_address_base_reg_class = BASE_REG_CLASS;
466 reload_address_index_reg_class = INDEX_REG_CLASS;
7402683f
ILT
467 if (SMALL_REGISTER_CLASSES)
468 {
469 int regno;
470 HARD_REG_SET base, index;
471 enum reg_class *p;
472
473 COPY_HARD_REG_SET (base, reg_class_contents[BASE_REG_CLASS]);
474 COPY_HARD_REG_SET (index, reg_class_contents[INDEX_REG_CLASS]);
475 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
476 {
477 if (FUNCTION_ARG_REGNO_P (regno))
478 {
479 CLEAR_HARD_REG_BIT (base, regno);
480 CLEAR_HARD_REG_BIT (index, regno);
481 }
482 }
483
484 GO_IF_HARD_REG_EQUAL (base, reg_class_contents[BASE_REG_CLASS],
485 baseok);
486 for (p = reg_class_subclasses[BASE_REG_CLASS];
487 *p != LIM_REG_CLASSES;
488 p++)
489 {
490 GO_IF_HARD_REG_EQUAL (base, reg_class_contents[*p], usebase);
491 continue;
492 usebase:
493 reload_address_base_reg_class = *p;
494 break;
495 }
496 baseok:;
497
498 GO_IF_HARD_REG_EQUAL (index, reg_class_contents[INDEX_REG_CLASS],
499 indexok);
500 for (p = reg_class_subclasses[INDEX_REG_CLASS];
501 *p != LIM_REG_CLASSES;
502 p++)
503 {
504 GO_IF_HARD_REG_EQUAL (index, reg_class_contents[*p], useindex);
505 continue;
506 useindex:
507 reload_address_index_reg_class = *p;
508 break;
509 }
510 indexok:;
511 }
32131a9c
RK
512}
513
546b63fb 514/* Main entry point for the reload pass.
32131a9c
RK
515
516 FIRST is the first insn of the function being compiled.
517
518 GLOBAL nonzero means we were called from global_alloc
519 and should attempt to reallocate any pseudoregs that we
520 displace from hard regs we will use for reloads.
521 If GLOBAL is zero, we do not have enough information to do that,
522 so any pseudo reg that is spilled must go to the stack.
523
524 DUMPFILE is the global-reg debugging dump file stream, or 0.
525 If it is nonzero, messages are written to it to describe
526 which registers are seized as reload regs, which pseudo regs
5352b11a 527 are spilled from them, and where the pseudo regs are reallocated to.
32131a9c 528
5352b11a
RS
529 Return value is nonzero if reload failed
530 and we must not do any more for this function. */
531
532int
32131a9c
RK
533reload (first, global, dumpfile)
534 rtx first;
535 int global;
536 FILE *dumpfile;
537{
538 register int class;
8b3e912b 539 register int i, j, k;
32131a9c
RK
540 register rtx insn;
541 register struct elim_table *ep;
542
a68d4b75
BK
543 /* The two pointers used to track the true location of the memory used
544 for label offsets. */
545 char *real_known_ptr = NULL_PTR;
546 int (*real_at_ptr)[NUM_ELIMINABLE_REGS];
547
32131a9c
RK
548 int something_changed;
549 int something_needs_reloads;
550 int something_needs_elimination;
551 int new_basic_block_needs;
a8efe40d
RK
552 enum reg_class caller_save_spill_class = NO_REGS;
553 int caller_save_group_size = 1;
32131a9c 554
5352b11a
RS
555 /* Nonzero means we couldn't get enough spill regs. */
556 int failure = 0;
557
32131a9c
RK
558 /* The basic block number currently being processed for INSN. */
559 int this_block;
560
561 /* Make sure even insns with volatile mem refs are recognizable. */
562 init_recog ();
563
564 /* Enable find_equiv_reg to distinguish insns made by reload. */
565 reload_first_uid = get_max_uid ();
566
567 for (i = 0; i < N_REG_CLASSES; i++)
568 basic_block_needs[i] = 0;
569
0dadecf6
RK
570#ifdef SECONDARY_MEMORY_NEEDED
571 /* Initialize the secondary memory table. */
572 clear_secondary_mem ();
573#endif
574
32131a9c
RK
575 /* Remember which hard regs appear explicitly
576 before we merge into `regs_ever_live' the ones in which
577 pseudo regs have been allocated. */
578 bcopy (regs_ever_live, regs_explicitly_used, sizeof regs_ever_live);
579
580 /* We don't have a stack slot for any spill reg yet. */
4c9a05bc
RK
581 bzero ((char *) spill_stack_slot, sizeof spill_stack_slot);
582 bzero ((char *) spill_stack_slot_width, sizeof spill_stack_slot_width);
32131a9c 583
a8efe40d
RK
584 /* Initialize the save area information for caller-save, in case some
585 are needed. */
586 init_save_areas ();
a8fdc208 587
32131a9c
RK
588 /* Compute which hard registers are now in use
589 as homes for pseudo registers.
590 This is done here rather than (eg) in global_alloc
591 because this point is reached even if not optimizing. */
32131a9c
RK
592 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
593 mark_home_live (i);
594
8dddd002
RK
595 /* A function that receives a nonlocal goto must save all call-saved
596 registers. */
597 if (current_function_has_nonlocal_label)
598 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
599 {
600 if (! call_used_regs[i] && ! fixed_regs[i])
601 regs_ever_live[i] = 1;
602 }
603
c307c237
RK
604 for (i = 0; i < scratch_list_length; i++)
605 if (scratch_list[i])
606 mark_scratch_live (scratch_list[i]);
607
32131a9c
RK
608 /* Make sure that the last insn in the chain
609 is not something that needs reloading. */
fb3821f7 610 emit_note (NULL_PTR, NOTE_INSN_DELETED);
32131a9c
RK
611
612 /* Find all the pseudo registers that didn't get hard regs
613 but do have known equivalent constants or memory slots.
614 These include parameters (known equivalent to parameter slots)
615 and cse'd or loop-moved constant memory addresses.
616
617 Record constant equivalents in reg_equiv_constant
618 so they will be substituted by find_reloads.
619 Record memory equivalents in reg_mem_equiv so they can
620 be substituted eventually by altering the REG-rtx's. */
621
622 reg_equiv_constant = (rtx *) alloca (max_regno * sizeof (rtx));
4c9a05bc 623 bzero ((char *) reg_equiv_constant, max_regno * sizeof (rtx));
32131a9c 624 reg_equiv_memory_loc = (rtx *) alloca (max_regno * sizeof (rtx));
4c9a05bc 625 bzero ((char *) reg_equiv_memory_loc, max_regno * sizeof (rtx));
32131a9c 626 reg_equiv_mem = (rtx *) alloca (max_regno * sizeof (rtx));
4c9a05bc 627 bzero ((char *) reg_equiv_mem, max_regno * sizeof (rtx));
32131a9c 628 reg_equiv_init = (rtx *) alloca (max_regno * sizeof (rtx));
4c9a05bc 629 bzero ((char *) reg_equiv_init, max_regno * sizeof (rtx));
32131a9c 630 reg_equiv_address = (rtx *) alloca (max_regno * sizeof (rtx));
4c9a05bc 631 bzero ((char *) reg_equiv_address, max_regno * sizeof (rtx));
32131a9c 632 reg_max_ref_width = (int *) alloca (max_regno * sizeof (int));
4c9a05bc 633 bzero ((char *) reg_max_ref_width, max_regno * sizeof (int));
32131a9c 634
f95182a4
ILT
635 if (SMALL_REGISTER_CLASSES)
636 CLEAR_HARD_REG_SET (forbidden_regs);
56f58d3a 637
32131a9c 638 /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
56f58d3a
RK
639 Also find all paradoxical subregs and find largest such for each pseudo.
640 On machines with small register classes, record hard registers that
b453cb0b
RK
641 are used for user variables. These can never be used for spills.
642 Also look for a "constant" NOTE_INSN_SETJMP. This means that all
643 caller-saved registers must be marked live. */
32131a9c
RK
644
645 for (insn = first; insn; insn = NEXT_INSN (insn))
646 {
647 rtx set = single_set (insn);
648
b453cb0b
RK
649 if (GET_CODE (insn) == NOTE && CONST_CALL_P (insn)
650 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
651 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
652 if (! call_used_regs[i])
653 regs_ever_live[i] = 1;
654
32131a9c
RK
655 if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
656 {
fb3821f7 657 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
a8efe40d
RK
658 if (note
659#ifdef LEGITIMATE_PIC_OPERAND_P
a8fdc208 660 && (! CONSTANT_P (XEXP (note, 0)) || ! flag_pic
a8efe40d
RK
661 || LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))
662#endif
663 )
32131a9c
RK
664 {
665 rtx x = XEXP (note, 0);
666 i = REGNO (SET_DEST (set));
667 if (i > LAST_VIRTUAL_REGISTER)
668 {
669 if (GET_CODE (x) == MEM)
956d6950
JL
670 {
671 /* If the operand is a PLUS, the MEM may be shared,
672 so make sure we have an unshared copy here. */
673 if (GET_CODE (XEXP (x, 0)) == PLUS)
674 x = copy_rtx (x);
675
676 reg_equiv_memory_loc[i] = x;
677 }
32131a9c
RK
678 else if (CONSTANT_P (x))
679 {
680 if (LEGITIMATE_CONSTANT_P (x))
681 reg_equiv_constant[i] = x;
682 else
683 reg_equiv_memory_loc[i]
d445b551 684 = force_const_mem (GET_MODE (SET_DEST (set)), x);
32131a9c
RK
685 }
686 else
687 continue;
688
689 /* If this register is being made equivalent to a MEM
690 and the MEM is not SET_SRC, the equivalencing insn
691 is one with the MEM as a SET_DEST and it occurs later.
692 So don't mark this insn now. */
693 if (GET_CODE (x) != MEM
694 || rtx_equal_p (SET_SRC (set), x))
695 reg_equiv_init[i] = insn;
696 }
697 }
698 }
699
700 /* If this insn is setting a MEM from a register equivalent to it,
701 this is the equivalencing insn. */
702 else if (set && GET_CODE (SET_DEST (set)) == MEM
703 && GET_CODE (SET_SRC (set)) == REG
704 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
705 && rtx_equal_p (SET_DEST (set),
706 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
707 reg_equiv_init[REGNO (SET_SRC (set))] = insn;
708
709 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
710 scan_paradoxical_subregs (PATTERN (insn));
711 }
712
713 /* Does this function require a frame pointer? */
714
715 frame_pointer_needed = (! flag_omit_frame_pointer
716#ifdef EXIT_IGNORE_STACK
717 /* ?? If EXIT_IGNORE_STACK is set, we will not save
718 and restore sp for alloca. So we can't eliminate
719 the frame pointer in that case. At some point,
720 we should improve this by emitting the
721 sp-adjusting insns for this case. */
722 || (current_function_calls_alloca
723 && EXIT_IGNORE_STACK)
724#endif
725 || FRAME_POINTER_REQUIRED);
726
727 num_eliminable = 0;
728
729 /* Initialize the table of registers to eliminate. The way we do this
730 depends on how the eliminable registers were defined. */
731#ifdef ELIMINABLE_REGS
732 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
733 {
734 ep->can_eliminate = ep->can_eliminate_previous
735 = (CAN_ELIMINATE (ep->from, ep->to)
9ff3516a 736 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
32131a9c
RK
737 }
738#else
739 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
740 = ! frame_pointer_needed;
741#endif
742
743 /* Count the number of eliminable registers and build the FROM and TO
a8fdc208 744 REG rtx's. Note that code in gen_rtx will cause, e.g.,
32131a9c
RK
745 gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
746 We depend on this. */
747 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
748 {
749 num_eliminable += ep->can_eliminate;
38a448ca
RH
750 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
751 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
32131a9c
RK
752 }
753
754 num_labels = max_label_num () - get_first_label_num ();
755
756 /* Allocate the tables used to store offset information at labels. */
a68d4b75
BK
757 /* We used to use alloca here, but the size of what it would try to
758 allocate would occasionally cause it to exceed the stack limit and
759 cause a core dump. */
760 real_known_ptr = xmalloc (num_labels);
761 real_at_ptr
32131a9c 762 = (int (*)[NUM_ELIMINABLE_REGS])
a68d4b75 763 xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
32131a9c 764
a68d4b75
BK
765 offsets_known_at = real_known_ptr - get_first_label_num ();
766 offsets_at
767 = (int (*)[NUM_ELIMINABLE_REGS]) (real_at_ptr - get_first_label_num ());
32131a9c
RK
768
769 /* Alter each pseudo-reg rtx to contain its hard reg number.
770 Assign stack slots to the pseudos that lack hard regs or equivalents.
771 Do not touch virtual registers. */
772
773 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
774 alter_reg (i, -1);
775
32131a9c
RK
776 /* If we have some registers we think can be eliminated, scan all insns to
777 see if there is an insn that sets one of these registers to something
778 other than itself plus a constant. If so, the register cannot be
779 eliminated. Doing this scan here eliminates an extra pass through the
780 main reload loop in the most common case where register elimination
781 cannot be done. */
782 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
783 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
784 || GET_CODE (insn) == CALL_INSN)
785 note_stores (PATTERN (insn), mark_not_eliminable);
786
787#ifndef REGISTER_CONSTRAINTS
788 /* If all the pseudo regs have hard regs,
789 except for those that are never referenced,
790 we know that no reloads are needed. */
791 /* But that is not true if there are register constraints, since
792 in that case some pseudos might be in the wrong kind of hard reg. */
793
794 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
b1f21e0a 795 if (reg_renumber[i] == -1 && REG_N_REFS (i) != 0)
32131a9c
RK
796 break;
797
b8093d02 798 if (i == max_regno && num_eliminable == 0 && ! caller_save_needed)
a68d4b75
BK
799 {
800 free (real_known_ptr);
801 free (real_at_ptr);
802 return;
803 }
32131a9c
RK
804#endif
805
806 /* Compute the order of preference for hard registers to spill.
807 Store them by decreasing preference in potential_reload_regs. */
808
2c5d9e37 809 order_regs_for_reload (global);
32131a9c
RK
810
811 /* So far, no hard regs have been spilled. */
812 n_spills = 0;
813 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
814 spill_reg_order[i] = -1;
815
4079cd63
JW
816 /* Initialize to -1, which means take the first spill register. */
817 last_spill_reg = -1;
818
32131a9c
RK
819 /* On most machines, we can't use any register explicitly used in the
820 rtl as a spill register. But on some, we have to. Those will have
821 taken care to keep the life of hard regs as short as possible. */
822
f95182a4 823 if (! SMALL_REGISTER_CLASSES)
f95182a4 824 COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
32131a9c
RK
825
826 /* Spill any hard regs that we know we can't eliminate. */
827 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
828 if (! ep->can_eliminate)
9ff3516a
RK
829 spill_hard_reg (ep->from, global, dumpfile, 1);
830
831#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
832 if (frame_pointer_needed)
833 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, global, dumpfile, 1);
834#endif
32131a9c
RK
835
836 if (global)
837 for (i = 0; i < N_REG_CLASSES; i++)
838 {
4c9a05bc 839 basic_block_needs[i] = (char *) alloca (n_basic_blocks);
32131a9c
RK
840 bzero (basic_block_needs[i], n_basic_blocks);
841 }
842
b2f15f94
RK
843 /* From now on, we need to emit any moves without making new pseudos. */
844 reload_in_progress = 1;
845
32131a9c
RK
846 /* This loop scans the entire function each go-round
847 and repeats until one repetition spills no additional hard regs. */
848
d45cf215 849 /* This flag is set when a pseudo reg is spilled,
32131a9c
RK
850 to require another pass. Note that getting an additional reload
851 reg does not necessarily imply any pseudo reg was spilled;
852 sometimes we find a reload reg that no pseudo reg was allocated in. */
853 something_changed = 1;
854 /* This flag is set if there are any insns that require reloading. */
855 something_needs_reloads = 0;
856 /* This flag is set if there are any insns that require register
857 eliminations. */
858 something_needs_elimination = 0;
859 while (something_changed)
860 {
861 rtx after_call = 0;
862
863 /* For each class, number of reload regs needed in that class.
864 This is the maximum over all insns of the needs in that class
865 of the individual insn. */
866 int max_needs[N_REG_CLASSES];
867 /* For each class, size of group of consecutive regs
868 that is needed for the reloads of this class. */
869 int group_size[N_REG_CLASSES];
870 /* For each class, max number of consecutive groups needed.
871 (Each group contains group_size[CLASS] consecutive registers.) */
872 int max_groups[N_REG_CLASSES];
873 /* For each class, max number needed of regs that don't belong
874 to any of the groups. */
875 int max_nongroups[N_REG_CLASSES];
876 /* For each class, the machine mode which requires consecutive
877 groups of regs of that class.
878 If two different modes ever require groups of one class,
879 they must be the same size and equally restrictive for that class,
880 otherwise we can't handle the complexity. */
881 enum machine_mode group_mode[N_REG_CLASSES];
5352b11a
RS
882 /* Record the insn where each maximum need is first found. */
883 rtx max_needs_insn[N_REG_CLASSES];
884 rtx max_groups_insn[N_REG_CLASSES];
885 rtx max_nongroups_insn[N_REG_CLASSES];
32131a9c 886 rtx x;
7657bf2f 887 HOST_WIDE_INT starting_frame_size;
29a82058 888#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
9ff3516a 889 int previous_frame_pointer_needed = frame_pointer_needed;
29a82058 890#endif
e404a39a 891 static char *reg_class_names[] = REG_CLASS_NAMES;
32131a9c
RK
892
893 something_changed = 0;
4c9a05bc
RK
894 bzero ((char *) max_needs, sizeof max_needs);
895 bzero ((char *) max_groups, sizeof max_groups);
896 bzero ((char *) max_nongroups, sizeof max_nongroups);
897 bzero ((char *) max_needs_insn, sizeof max_needs_insn);
898 bzero ((char *) max_groups_insn, sizeof max_groups_insn);
899 bzero ((char *) max_nongroups_insn, sizeof max_nongroups_insn);
900 bzero ((char *) group_size, sizeof group_size);
32131a9c
RK
901 for (i = 0; i < N_REG_CLASSES; i++)
902 group_mode[i] = VOIDmode;
903
904 /* Keep track of which basic blocks are needing the reloads. */
905 this_block = 0;
906
907 /* Remember whether any element of basic_block_needs
908 changes from 0 to 1 in this pass. */
909 new_basic_block_needs = 0;
910
7657bf2f
JW
911 /* Round size of stack frame to BIGGEST_ALIGNMENT. This must be done
912 here because the stack size may be a part of the offset computation
913 for register elimination, and there might have been new stack slots
914 created in the last iteration of this loop. */
915 assign_stack_local (BLKmode, 0, 0);
916
917 starting_frame_size = get_frame_size ();
918
32131a9c
RK
919 /* Reset all offsets on eliminable registers to their initial values. */
920#ifdef ELIMINABLE_REGS
921 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
922 {
923 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
a8efe40d
RK
924 ep->previous_offset = ep->offset
925 = ep->max_offset = ep->initial_offset;
32131a9c
RK
926 }
927#else
928#ifdef INITIAL_FRAME_POINTER_OFFSET
929 INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
930#else
931 if (!FRAME_POINTER_REQUIRED)
932 abort ();
933 reg_eliminate[0].initial_offset = 0;
934#endif
a8efe40d 935 reg_eliminate[0].previous_offset = reg_eliminate[0].max_offset
32131a9c
RK
936 = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
937#endif
938
939 num_not_at_initial_offset = 0;
940
4c9a05bc 941 bzero ((char *) &offsets_known_at[get_first_label_num ()], num_labels);
32131a9c
RK
942
943 /* Set a known offset for each forced label to be at the initial offset
944 of each elimination. We do this because we assume that all
945 computed jumps occur from a location where each elimination is
946 at its initial offset. */
947
948 for (x = forced_labels; x; x = XEXP (x, 1))
949 if (XEXP (x, 0))
fb3821f7 950 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
32131a9c
RK
951
952 /* For each pseudo register that has an equivalent location defined,
953 try to eliminate any eliminable registers (such as the frame pointer)
954 assuming initial offsets for the replacement register, which
955 is the normal case.
956
957 If the resulting location is directly addressable, substitute
958 the MEM we just got directly for the old REG.
959
960 If it is not addressable but is a constant or the sum of a hard reg
961 and constant, it is probably not addressable because the constant is
962 out of range, in that case record the address; we will generate
963 hairy code to compute the address in a register each time it is
6491dbbb
RK
964 needed. Similarly if it is a hard register, but one that is not
965 valid as an address register.
32131a9c
RK
966
967 If the location is not addressable, but does not have one of the
968 above forms, assign a stack slot. We have to do this to avoid the
969 potential of producing lots of reloads if, e.g., a location involves
970 a pseudo that didn't get a hard register and has an equivalent memory
971 location that also involves a pseudo that didn't get a hard register.
972
973 Perhaps at some point we will improve reload_when_needed handling
974 so this problem goes away. But that's very hairy. */
975
976 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
977 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
978 {
1914f5da 979 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
32131a9c
RK
980
981 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
982 XEXP (x, 0)))
983 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
984 else if (CONSTANT_P (XEXP (x, 0))
6491dbbb
RK
985 || (GET_CODE (XEXP (x, 0)) == REG
986 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
32131a9c
RK
987 || (GET_CODE (XEXP (x, 0)) == PLUS
988 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
989 && (REGNO (XEXP (XEXP (x, 0), 0))
990 < FIRST_PSEUDO_REGISTER)
991 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
992 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
993 else
994 {
995 /* Make a new stack slot. Then indicate that something
a8fdc208 996 changed so we go back and recompute offsets for
32131a9c
RK
997 eliminable registers because the allocation of memory
998 below might change some offset. reg_equiv_{mem,address}
999 will be set up for this pseudo on the next pass around
1000 the loop. */
1001 reg_equiv_memory_loc[i] = 0;
1002 reg_equiv_init[i] = 0;
1003 alter_reg (i, -1);
1004 something_changed = 1;
1005 }
1006 }
a8fdc208 1007
d45cf215 1008 /* If we allocated another pseudo to the stack, redo elimination
32131a9c
RK
1009 bookkeeping. */
1010 if (something_changed)
1011 continue;
1012
a8efe40d
RK
1013 /* If caller-saves needs a group, initialize the group to include
1014 the size and mode required for caller-saves. */
1015
1016 if (caller_save_group_size > 1)
1017 {
1018 group_mode[(int) caller_save_spill_class] = Pmode;
1019 group_size[(int) caller_save_spill_class] = caller_save_group_size;
1020 }
1021
32131a9c
RK
1022 /* Compute the most additional registers needed by any instruction.
1023 Collect information separately for each class of regs. */
1024
1025 for (insn = first; insn; insn = NEXT_INSN (insn))
1026 {
1027 if (global && this_block + 1 < n_basic_blocks
1028 && insn == basic_block_head[this_block+1])
1029 ++this_block;
1030
1031 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which
1032 might include REG_LABEL), we need to see what effects this
1033 has on the known offsets at labels. */
1034
1035 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
1036 || (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1037 && REG_NOTES (insn) != 0))
1038 set_label_offsets (insn, insn, 0);
1039
1040 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1041 {
1042 /* Nonzero means don't use a reload reg that overlaps
1043 the place where a function value can be returned. */
1044 rtx avoid_return_reg = 0;
1045
1046 rtx old_body = PATTERN (insn);
1047 int old_code = INSN_CODE (insn);
1048 rtx old_notes = REG_NOTES (insn);
1049 int did_elimination = 0;
546b63fb
RK
1050
1051 /* To compute the number of reload registers of each class
9faa82d8 1052 needed for an insn, we must simulate what choose_reload_regs
546b63fb
RK
1053 can do. We do this by splitting an insn into an "input" and
1054 an "output" part. RELOAD_OTHER reloads are used in both.
1055 The input part uses those reloads, RELOAD_FOR_INPUT reloads,
1056 which must be live over the entire input section of reloads,
1057 and the maximum of all the RELOAD_FOR_INPUT_ADDRESS and
1058 RELOAD_FOR_OPERAND_ADDRESS reloads, which conflict with the
1059 inputs.
1060
1061 The registers needed for output are RELOAD_OTHER and
1062 RELOAD_FOR_OUTPUT, which are live for the entire output
1063 portion, and the maximum of all the RELOAD_FOR_OUTPUT_ADDRESS
1064 reloads for each operand.
1065
1066 The total number of registers needed is the maximum of the
1067 inputs and outputs. */
1068
8b3e912b 1069 struct needs
32131a9c 1070 {
8b3e912b
RK
1071 /* [0] is normal, [1] is nongroup. */
1072 int regs[2][N_REG_CLASSES];
1073 int groups[N_REG_CLASSES];
1074 };
1075
1076 /* Each `struct needs' corresponds to one RELOAD_... type. */
1077 struct {
1078 struct needs other;
1079 struct needs input;
1080 struct needs output;
1081 struct needs insn;
1082 struct needs other_addr;
1083 struct needs op_addr;
893bc853 1084 struct needs op_addr_reload;
8b3e912b 1085 struct needs in_addr[MAX_RECOG_OPERANDS];
47c8cf91 1086 struct needs in_addr_addr[MAX_RECOG_OPERANDS];
8b3e912b 1087 struct needs out_addr[MAX_RECOG_OPERANDS];
47c8cf91 1088 struct needs out_addr_addr[MAX_RECOG_OPERANDS];
8b3e912b 1089 } insn_needs;
32131a9c
RK
1090
1091 /* If needed, eliminate any eliminable registers. */
1092 if (num_eliminable)
1093 did_elimination = eliminate_regs_in_insn (insn, 0);
1094
32131a9c
RK
1095 /* Set avoid_return_reg if this is an insn
1096 that might use the value of a function call. */
f95182a4 1097 if (SMALL_REGISTER_CLASSES && GET_CODE (insn) == CALL_INSN)
32131a9c
RK
1098 {
1099 if (GET_CODE (PATTERN (insn)) == SET)
1100 after_call = SET_DEST (PATTERN (insn));
1101 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1102 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1103 after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1104 else
1105 after_call = 0;
1106 }
e9a25f70 1107 else if (SMALL_REGISTER_CLASSES && after_call != 0
32131a9c 1108 && !(GET_CODE (PATTERN (insn)) == SET
b60a8416
R
1109 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx)
1110 && GET_CODE (PATTERN (insn)) != USE)
32131a9c 1111 {
2b979c57 1112 if (reg_referenced_p (after_call, PATTERN (insn)))
32131a9c
RK
1113 avoid_return_reg = after_call;
1114 after_call = 0;
1115 }
32131a9c
RK
1116
1117 /* Analyze the instruction. */
1118 find_reloads (insn, 0, spill_indirect_levels, global,
1119 spill_reg_order);
1120
1121 /* Remember for later shortcuts which insns had any reloads or
1122 register eliminations.
1123
1124 One might think that it would be worthwhile to mark insns
1125 that need register replacements but not reloads, but this is
1126 not safe because find_reloads may do some manipulation of
1127 the insn (such as swapping commutative operands), which would
1128 be lost when we restore the old pattern after register
1129 replacement. So the actions of find_reloads must be redone in
1130 subsequent passes or in reload_as_needed.
1131
1132 However, it is safe to mark insns that need reloads
1133 but not register replacement. */
1134
1135 PUT_MODE (insn, (did_elimination ? QImode
1136 : n_reloads ? HImode
546b63fb 1137 : GET_MODE (insn) == DImode ? DImode
32131a9c
RK
1138 : VOIDmode));
1139
1140 /* Discard any register replacements done. */
1141 if (did_elimination)
1142 {
1143 obstack_free (&reload_obstack, reload_firstobj);
1144 PATTERN (insn) = old_body;
1145 INSN_CODE (insn) = old_code;
1146 REG_NOTES (insn) = old_notes;
1147 something_needs_elimination = 1;
1148 }
1149
a8efe40d 1150 /* If this insn has no reloads, we need not do anything except
a8fdc208 1151 in the case of a CALL_INSN when we have caller-saves and
a8efe40d
RK
1152 caller-save needs reloads. */
1153
1154 if (n_reloads == 0
1155 && ! (GET_CODE (insn) == CALL_INSN
1156 && caller_save_spill_class != NO_REGS))
32131a9c
RK
1157 continue;
1158
1159 something_needs_reloads = 1;
4c9a05bc 1160 bzero ((char *) &insn_needs, sizeof insn_needs);
32131a9c
RK
1161
1162 /* Count each reload once in every class
1163 containing the reload's own class. */
1164
1165 for (i = 0; i < n_reloads; i++)
1166 {
1167 register enum reg_class *p;
e85ddd99 1168 enum reg_class class = reload_reg_class[i];
32131a9c
RK
1169 int size;
1170 enum machine_mode mode;
8b3e912b 1171 struct needs *this_needs;
32131a9c
RK
1172
1173 /* Don't count the dummy reloads, for which one of the
1174 regs mentioned in the insn can be used for reloading.
1175 Don't count optional reloads.
1176 Don't count reloads that got combined with others. */
1177 if (reload_reg_rtx[i] != 0
1178 || reload_optional[i] != 0
1179 || (reload_out[i] == 0 && reload_in[i] == 0
1180 && ! reload_secondary_p[i]))
1181 continue;
1182
e85ddd99
RK
1183 /* Show that a reload register of this class is needed
1184 in this basic block. We do not use insn_needs and
1185 insn_groups because they are overly conservative for
1186 this purpose. */
1187 if (global && ! basic_block_needs[(int) class][this_block])
1188 {
1189 basic_block_needs[(int) class][this_block] = 1;
1190 new_basic_block_needs = 1;
1191 }
1192
ee249c09
RK
1193 mode = reload_inmode[i];
1194 if (GET_MODE_SIZE (reload_outmode[i]) > GET_MODE_SIZE (mode))
1195 mode = reload_outmode[i];
1196 size = CLASS_MAX_NREGS (class, mode);
1197
32131a9c
RK
1198 /* Decide which time-of-use to count this reload for. */
1199 switch (reload_when_needed[i])
1200 {
1201 case RELOAD_OTHER:
8b3e912b 1202 this_needs = &insn_needs.other;
32131a9c 1203 break;
546b63fb 1204 case RELOAD_FOR_INPUT:
8b3e912b 1205 this_needs = &insn_needs.input;
32131a9c 1206 break;
546b63fb 1207 case RELOAD_FOR_OUTPUT:
8b3e912b 1208 this_needs = &insn_needs.output;
32131a9c 1209 break;
546b63fb 1210 case RELOAD_FOR_INSN:
8b3e912b 1211 this_needs = &insn_needs.insn;
546b63fb 1212 break;
546b63fb 1213 case RELOAD_FOR_OTHER_ADDRESS:
8b3e912b 1214 this_needs = &insn_needs.other_addr;
546b63fb 1215 break;
546b63fb 1216 case RELOAD_FOR_INPUT_ADDRESS:
8b3e912b 1217 this_needs = &insn_needs.in_addr[reload_opnum[i]];
546b63fb 1218 break;
47c8cf91
ILT
1219 case RELOAD_FOR_INPADDR_ADDRESS:
1220 this_needs = &insn_needs.in_addr_addr[reload_opnum[i]];
1221 break;
546b63fb 1222 case RELOAD_FOR_OUTPUT_ADDRESS:
8b3e912b 1223 this_needs = &insn_needs.out_addr[reload_opnum[i]];
546b63fb 1224 break;
47c8cf91
ILT
1225 case RELOAD_FOR_OUTADDR_ADDRESS:
1226 this_needs = &insn_needs.out_addr_addr[reload_opnum[i]];
1227 break;
32131a9c 1228 case RELOAD_FOR_OPERAND_ADDRESS:
8b3e912b 1229 this_needs = &insn_needs.op_addr;
32131a9c 1230 break;
893bc853
RK
1231 case RELOAD_FOR_OPADDR_ADDR:
1232 this_needs = &insn_needs.op_addr_reload;
1233 break;
32131a9c
RK
1234 }
1235
32131a9c
RK
1236 if (size > 1)
1237 {
1238 enum machine_mode other_mode, allocate_mode;
1239
1240 /* Count number of groups needed separately from
1241 number of individual regs needed. */
8b3e912b 1242 this_needs->groups[(int) class]++;
e85ddd99 1243 p = reg_class_superclasses[(int) class];
32131a9c 1244 while (*p != LIM_REG_CLASSES)
8b3e912b 1245 this_needs->groups[(int) *p++]++;
32131a9c
RK
1246
1247 /* Record size and mode of a group of this class. */
1248 /* If more than one size group is needed,
1249 make all groups the largest needed size. */
e85ddd99 1250 if (group_size[(int) class] < size)
32131a9c 1251 {
e85ddd99 1252 other_mode = group_mode[(int) class];
32131a9c
RK
1253 allocate_mode = mode;
1254
e85ddd99
RK
1255 group_size[(int) class] = size;
1256 group_mode[(int) class] = mode;
32131a9c
RK
1257 }
1258 else
1259 {
1260 other_mode = mode;
e85ddd99 1261 allocate_mode = group_mode[(int) class];
32131a9c
RK
1262 }
1263
1264 /* Crash if two dissimilar machine modes both need
1265 groups of consecutive regs of the same class. */
1266
8b3e912b 1267 if (other_mode != VOIDmode && other_mode != allocate_mode
32131a9c 1268 && ! modes_equiv_for_class_p (allocate_mode,
8b3e912b 1269 other_mode, class))
a89b2cc4
MM
1270 fatal_insn ("Two dissimilar machine modes both need groups of consecutive regs of the same class",
1271 insn);
32131a9c
RK
1272 }
1273 else if (size == 1)
1274 {
f5963e61 1275 this_needs->regs[reload_nongroup[i]][(int) class] += 1;
e85ddd99 1276 p = reg_class_superclasses[(int) class];
32131a9c 1277 while (*p != LIM_REG_CLASSES)
f5963e61 1278 this_needs->regs[reload_nongroup[i]][(int) *p++] += 1;
32131a9c
RK
1279 }
1280 else
1281 abort ();
1282 }
1283
1284 /* All reloads have been counted for this insn;
1285 now merge the various times of use.
1286 This sets insn_needs, etc., to the maximum total number
1287 of registers needed at any point in this insn. */
1288
1289 for (i = 0; i < N_REG_CLASSES; i++)
1290 {
546b63fb
RK
1291 int in_max, out_max;
1292
8b3e912b
RK
1293 /* Compute normal and nongroup needs. */
1294 for (j = 0; j <= 1; j++)
546b63fb 1295 {
8b3e912b
RK
1296 for (in_max = 0, out_max = 0, k = 0;
1297 k < reload_n_operands; k++)
1298 {
47c8cf91
ILT
1299 in_max
1300 = MAX (in_max,
b080c137
RK
1301 (insn_needs.in_addr[k].regs[j][i]
1302 + insn_needs.in_addr_addr[k].regs[j][i]));
8b3e912b
RK
1303 out_max
1304 = MAX (out_max, insn_needs.out_addr[k].regs[j][i]);
47c8cf91
ILT
1305 out_max
1306 = MAX (out_max,
1307 insn_needs.out_addr_addr[k].regs[j][i]);
8b3e912b 1308 }
546b63fb 1309
8b3e912b
RK
1310 /* RELOAD_FOR_INSN reloads conflict with inputs, outputs,
1311 and operand addresses but not things used to reload
1312 them. Similarly, RELOAD_FOR_OPERAND_ADDRESS reloads
1313 don't conflict with things needed to reload inputs or
0f41302f 1314 outputs. */
546b63fb 1315
a94ce333
JW
1316 in_max = MAX (MAX (insn_needs.op_addr.regs[j][i],
1317 insn_needs.op_addr_reload.regs[j][i]),
893bc853
RK
1318 in_max);
1319
8b3e912b 1320 out_max = MAX (out_max, insn_needs.insn.regs[j][i]);
546b63fb 1321
8b3e912b
RK
1322 insn_needs.input.regs[j][i]
1323 = MAX (insn_needs.input.regs[j][i]
1324 + insn_needs.op_addr.regs[j][i]
1325 + insn_needs.insn.regs[j][i],
1326 in_max + insn_needs.input.regs[j][i]);
546b63fb 1327
8b3e912b
RK
1328 insn_needs.output.regs[j][i] += out_max;
1329 insn_needs.other.regs[j][i]
1330 += MAX (MAX (insn_needs.input.regs[j][i],
1331 insn_needs.output.regs[j][i]),
1332 insn_needs.other_addr.regs[j][i]);
546b63fb 1333
ce0e109b
RK
1334 }
1335
8b3e912b 1336 /* Now compute group needs. */
546b63fb
RK
1337 for (in_max = 0, out_max = 0, j = 0;
1338 j < reload_n_operands; j++)
1339 {
8b3e912b 1340 in_max = MAX (in_max, insn_needs.in_addr[j].groups[i]);
47c8cf91
ILT
1341 in_max = MAX (in_max,
1342 insn_needs.in_addr_addr[j].groups[i]);
8b3e912b
RK
1343 out_max
1344 = MAX (out_max, insn_needs.out_addr[j].groups[i]);
47c8cf91
ILT
1345 out_max
1346 = MAX (out_max, insn_needs.out_addr_addr[j].groups[i]);
546b63fb
RK
1347 }
1348
a94ce333
JW
1349 in_max = MAX (MAX (insn_needs.op_addr.groups[i],
1350 insn_needs.op_addr_reload.groups[i]),
893bc853 1351 in_max);
8b3e912b 1352 out_max = MAX (out_max, insn_needs.insn.groups[i]);
546b63fb 1353
8b3e912b
RK
1354 insn_needs.input.groups[i]
1355 = MAX (insn_needs.input.groups[i]
1356 + insn_needs.op_addr.groups[i]
1357 + insn_needs.insn.groups[i],
1358 in_max + insn_needs.input.groups[i]);
546b63fb 1359
8b3e912b
RK
1360 insn_needs.output.groups[i] += out_max;
1361 insn_needs.other.groups[i]
1362 += MAX (MAX (insn_needs.input.groups[i],
1363 insn_needs.output.groups[i]),
1364 insn_needs.other_addr.groups[i]);
546b63fb
RK
1365 }
1366
a8efe40d
RK
1367 /* If this is a CALL_INSN and caller-saves will need
1368 a spill register, act as if the spill register is
1369 needed for this insn. However, the spill register
1370 can be used by any reload of this insn, so we only
1371 need do something if no need for that class has
a8fdc208 1372 been recorded.
a8efe40d
RK
1373
1374 The assumption that every CALL_INSN will trigger a
1375 caller-save is highly conservative, however, the number
1376 of cases where caller-saves will need a spill register but
1377 a block containing a CALL_INSN won't need a spill register
1378 of that class should be quite rare.
1379
1380 If a group is needed, the size and mode of the group will
d45cf215 1381 have been set up at the beginning of this loop. */
a8efe40d
RK
1382
1383 if (GET_CODE (insn) == CALL_INSN
1384 && caller_save_spill_class != NO_REGS)
1385 {
f5963e61
JL
1386 /* See if this register would conflict with any reload that
1387 needs a group or any reload that needs a nongroup. */
8b3e912b
RK
1388 int nongroup_need = 0;
1389 int *caller_save_needs;
1390
1391 for (j = 0; j < n_reloads; j++)
f5963e61
JL
1392 if (reg_classes_intersect_p (caller_save_spill_class,
1393 reload_reg_class[j])
1394 && ((CLASS_MAX_NREGS
1395 (reload_reg_class[j],
1396 (GET_MODE_SIZE (reload_outmode[j])
1397 > GET_MODE_SIZE (reload_inmode[j]))
1398 ? reload_outmode[j] : reload_inmode[j])
1399 > 1)
1400 || reload_nongroup[j]))
8b3e912b
RK
1401 {
1402 nongroup_need = 1;
1403 break;
1404 }
1405
1406 caller_save_needs
1407 = (caller_save_group_size > 1
1408 ? insn_needs.other.groups
1409 : insn_needs.other.regs[nongroup_need]);
a8efe40d
RK
1410
1411 if (caller_save_needs[(int) caller_save_spill_class] == 0)
1412 {
1413 register enum reg_class *p
1414 = reg_class_superclasses[(int) caller_save_spill_class];
1415
1416 caller_save_needs[(int) caller_save_spill_class]++;
1417
1418 while (*p != LIM_REG_CLASSES)
0aaa6af8 1419 caller_save_needs[(int) *p++] += 1;
a8efe40d
RK
1420 }
1421
8b3e912b 1422 /* Show that this basic block will need a register of
d1c1397e
RS
1423 this class. */
1424
8b3e912b
RK
1425 if (global
1426 && ! (basic_block_needs[(int) caller_save_spill_class]
1427 [this_block]))
1428 {
1429 basic_block_needs[(int) caller_save_spill_class]
1430 [this_block] = 1;
1431 new_basic_block_needs = 1;
1432 }
a8efe40d
RK
1433 }
1434
32131a9c
RK
1435 /* If this insn stores the value of a function call,
1436 and that value is in a register that has been spilled,
1437 and if the insn needs a reload in a class
1438 that might use that register as the reload register,
38e01259 1439 then add an extra need in that class.
32131a9c
RK
1440 This makes sure we have a register available that does
1441 not overlap the return value. */
8b3e912b 1442
f95182a4 1443 if (SMALL_REGISTER_CLASSES && avoid_return_reg)
32131a9c
RK
1444 {
1445 int regno = REGNO (avoid_return_reg);
1446 int nregs
1447 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
1448 int r;
546b63fb
RK
1449 int basic_needs[N_REG_CLASSES], basic_groups[N_REG_CLASSES];
1450
1451 /* First compute the "basic needs", which counts a
1452 need only in the smallest class in which it
1453 is required. */
1454
9b232232
RK
1455 bcopy ((char *) insn_needs.other.regs[0],
1456 (char *) basic_needs, sizeof basic_needs);
1457 bcopy ((char *) insn_needs.other.groups,
1458 (char *) basic_groups, sizeof basic_groups);
546b63fb
RK
1459
1460 for (i = 0; i < N_REG_CLASSES; i++)
1461 {
1462 enum reg_class *p;
1463
1464 if (basic_needs[i] >= 0)
1465 for (p = reg_class_superclasses[i];
1466 *p != LIM_REG_CLASSES; p++)
1467 basic_needs[(int) *p] -= basic_needs[i];
1468
1469 if (basic_groups[i] >= 0)
1470 for (p = reg_class_superclasses[i];
1471 *p != LIM_REG_CLASSES; p++)
1472 basic_groups[(int) *p] -= basic_groups[i];
1473 }
1474
1475 /* Now count extra regs if there might be a conflict with
0f41302f 1476 the return value register. */
546b63fb 1477
32131a9c
RK
1478 for (r = regno; r < regno + nregs; r++)
1479 if (spill_reg_order[r] >= 0)
1480 for (i = 0; i < N_REG_CLASSES; i++)
1481 if (TEST_HARD_REG_BIT (reg_class_contents[i], r))
1482 {
af432130 1483 if (basic_needs[i] > 0)
546b63fb
RK
1484 {
1485 enum reg_class *p;
1486
8b3e912b 1487 insn_needs.other.regs[0][i]++;
546b63fb
RK
1488 p = reg_class_superclasses[i];
1489 while (*p != LIM_REG_CLASSES)
8b3e912b 1490 insn_needs.other.regs[0][(int) *p++]++;
546b63fb 1491 }
af432130
RK
1492 if (basic_groups[i] > 0)
1493 {
1494 enum reg_class *p;
1495
1496 insn_needs.other.groups[i]++;
1497 p = reg_class_superclasses[i];
1498 while (*p != LIM_REG_CLASSES)
1499 insn_needs.other.groups[(int) *p++]++;
1500 }
32131a9c 1501 }
32131a9c 1502 }
32131a9c
RK
1503
1504 /* For each class, collect maximum need of any insn. */
1505
1506 for (i = 0; i < N_REG_CLASSES; i++)
1507 {
8b3e912b 1508 if (max_needs[i] < insn_needs.other.regs[0][i])
5352b11a 1509 {
8b3e912b 1510 max_needs[i] = insn_needs.other.regs[0][i];
5352b11a
RS
1511 max_needs_insn[i] = insn;
1512 }
8b3e912b 1513 if (max_groups[i] < insn_needs.other.groups[i])
5352b11a 1514 {
8b3e912b 1515 max_groups[i] = insn_needs.other.groups[i];
5352b11a
RS
1516 max_groups_insn[i] = insn;
1517 }
8b3e912b 1518 if (max_nongroups[i] < insn_needs.other.regs[1][i])
ce0e109b 1519 {
8b3e912b 1520 max_nongroups[i] = insn_needs.other.regs[1][i];
ce0e109b
RK
1521 max_nongroups_insn[i] = insn;
1522 }
32131a9c
RK
1523 }
1524 }
1525 /* Note that there is a continue statement above. */
1526 }
1527
0dadecf6
RK
1528 /* If we allocated any new memory locations, make another pass
1529 since it might have changed elimination offsets. */
1530 if (starting_frame_size != get_frame_size ())
1531 something_changed = 1;
1532
e404a39a
RK
1533 if (dumpfile)
1534 for (i = 0; i < N_REG_CLASSES; i++)
1535 {
1536 if (max_needs[i] > 0)
1537 fprintf (dumpfile,
1538 ";; Need %d reg%s of class %s (for insn %d).\n",
1539 max_needs[i], max_needs[i] == 1 ? "" : "s",
1540 reg_class_names[i], INSN_UID (max_needs_insn[i]));
1541 if (max_nongroups[i] > 0)
1542 fprintf (dumpfile,
1543 ";; Need %d nongroup reg%s of class %s (for insn %d).\n",
1544 max_nongroups[i], max_nongroups[i] == 1 ? "" : "s",
1545 reg_class_names[i], INSN_UID (max_nongroups_insn[i]));
1546 if (max_groups[i] > 0)
1547 fprintf (dumpfile,
1548 ";; Need %d group%s (%smode) of class %s (for insn %d).\n",
1549 max_groups[i], max_groups[i] == 1 ? "" : "s",
1550 mode_name[(int) group_mode[i]],
1551 reg_class_names[i], INSN_UID (max_groups_insn[i]));
1552 }
1553
d445b551 1554 /* If we have caller-saves, set up the save areas and see if caller-save
a8efe40d 1555 will need a spill register. */
32131a9c 1556
37c0e55f 1557 if (caller_save_needed)
32131a9c 1558 {
37c0e55f
RK
1559 /* Set the offsets for setup_save_areas. */
1560 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
1561 ep++)
1562 ep->previous_offset = ep->max_offset;
1563
1564 if ( ! setup_save_areas (&something_changed)
1565 && caller_save_spill_class == NO_REGS)
1566 {
1567 /* The class we will need depends on whether the machine
1568 supports the sum of two registers for an address; see
1569 find_address_reloads for details. */
1570
1571 caller_save_spill_class
1572 = double_reg_address_ok ? INDEX_REG_CLASS : BASE_REG_CLASS;
1573 caller_save_group_size
1574 = CLASS_MAX_NREGS (caller_save_spill_class, Pmode);
1575 something_changed = 1;
1576 }
32131a9c
RK
1577 }
1578
5c23c401
RK
1579 /* See if anything that happened changes which eliminations are valid.
1580 For example, on the Sparc, whether or not the frame pointer can
1581 be eliminated can depend on what registers have been used. We need
1582 not check some conditions again (such as flag_omit_frame_pointer)
1583 since they can't have changed. */
1584
1585 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3ec2ea3e 1586 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
5c23c401
RK
1587#ifdef ELIMINABLE_REGS
1588 || ! CAN_ELIMINATE (ep->from, ep->to)
1589#endif
1590 )
1591 ep->can_eliminate = 0;
1592
32131a9c
RK
1593 /* Look for the case where we have discovered that we can't replace
1594 register A with register B and that means that we will now be
1595 trying to replace register A with register C. This means we can
1596 no longer replace register C with register B and we need to disable
1597 such an elimination, if it exists. This occurs often with A == ap,
1598 B == sp, and C == fp. */
a8fdc208 1599
32131a9c
RK
1600 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1601 {
1602 struct elim_table *op;
1603 register int new_to = -1;
1604
1605 if (! ep->can_eliminate && ep->can_eliminate_previous)
1606 {
1607 /* Find the current elimination for ep->from, if there is a
1608 new one. */
1609 for (op = reg_eliminate;
1610 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
1611 if (op->from == ep->from && op->can_eliminate)
1612 {
1613 new_to = op->to;
1614 break;
1615 }
1616
1617 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
1618 disable it. */
1619 for (op = reg_eliminate;
1620 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
1621 if (op->from == new_to && op->to == ep->to)
1622 op->can_eliminate = 0;
1623 }
1624 }
1625
1626 /* See if any registers that we thought we could eliminate the previous
1627 time are no longer eliminable. If so, something has changed and we
1628 must spill the register. Also, recompute the number of eliminable
1629 registers and see if the frame pointer is needed; it is if there is
1630 no elimination of the frame pointer that we can perform. */
1631
1632 frame_pointer_needed = 1;
1633 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1634 {
3ec2ea3e
DE
1635 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
1636 && ep->to != HARD_FRAME_POINTER_REGNUM)
32131a9c
RK
1637 frame_pointer_needed = 0;
1638
1639 if (! ep->can_eliminate && ep->can_eliminate_previous)
1640 {
1641 ep->can_eliminate_previous = 0;
1642 spill_hard_reg (ep->from, global, dumpfile, 1);
32131a9c
RK
1643 something_changed = 1;
1644 num_eliminable--;
1645 }
1646 }
1647
9ff3516a
RK
1648#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1649 /* If we didn't need a frame pointer last time, but we do now, spill
1650 the hard frame pointer. */
1651 if (frame_pointer_needed && ! previous_frame_pointer_needed)
1652 {
1653 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, global, dumpfile, 1);
1654 something_changed = 1;
1655 }
1656#endif
1657
32131a9c
RK
1658 /* If all needs are met, we win. */
1659
1660 for (i = 0; i < N_REG_CLASSES; i++)
1661 if (max_needs[i] > 0 || max_groups[i] > 0 || max_nongroups[i] > 0)
1662 break;
1663 if (i == N_REG_CLASSES && !new_basic_block_needs && ! something_changed)
1664 break;
1665
546b63fb
RK
1666 /* Not all needs are met; must spill some hard regs. */
1667
1668 /* Put all registers spilled so far back in potential_reload_regs, but
1669 put them at the front, since we've already spilled most of the
9faa82d8 1670 pseudos in them (we might have left some pseudos unspilled if they
546b63fb
RK
1671 were in a block that didn't need any spill registers of a conflicting
1672 class. We used to try to mark off the need for those registers,
1673 but doing so properly is very complex and reallocating them is the
1674 simpler approach. First, "pack" potential_reload_regs by pushing
1675 any nonnegative entries towards the end. That will leave room
1676 for the registers we already spilled.
1677
1678 Also, undo the marking of the spill registers from the last time
1679 around in FORBIDDEN_REGS since we will be probably be allocating
1680 them again below.
1681
1682 ??? It is theoretically possible that we might end up not using one
1683 of our previously-spilled registers in this allocation, even though
1684 they are at the head of the list. It's not clear what to do about
1685 this, but it was no better before, when we marked off the needs met
1686 by the previously-spilled registers. With the current code, globals
1687 can be allocated into these registers, but locals cannot. */
1688
1689 if (n_spills)
1690 {
1691 for (i = j = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1692 if (potential_reload_regs[i] != -1)
1693 potential_reload_regs[j--] = potential_reload_regs[i];
32131a9c 1694
546b63fb
RK
1695 for (i = 0; i < n_spills; i++)
1696 {
1697 potential_reload_regs[i] = spill_regs[i];
1698 spill_reg_order[spill_regs[i]] = -1;
1699 CLEAR_HARD_REG_BIT (forbidden_regs, spill_regs[i]);
1700 }
32131a9c 1701
546b63fb
RK
1702 n_spills = 0;
1703 }
32131a9c
RK
1704
1705 /* Now find more reload regs to satisfy the remaining need
1706 Do it by ascending class number, since otherwise a reg
1707 might be spilled for a big class and might fail to count
1708 for a smaller class even though it belongs to that class.
1709
1710 Count spilled regs in `spills', and add entries to
1711 `spill_regs' and `spill_reg_order'.
1712
1713 ??? Note there is a problem here.
1714 When there is a need for a group in a high-numbered class,
1715 and also need for non-group regs that come from a lower class,
1716 the non-group regs are chosen first. If there aren't many regs,
1717 they might leave no room for a group.
1718
1719 This was happening on the 386. To fix it, we added the code
1720 that calls possible_group_p, so that the lower class won't
1721 break up the last possible group.
1722
1723 Really fixing the problem would require changes above
1724 in counting the regs already spilled, and in choose_reload_regs.
1725 It might be hard to avoid introducing bugs there. */
1726
546b63fb
RK
1727 CLEAR_HARD_REG_SET (counted_for_groups);
1728 CLEAR_HARD_REG_SET (counted_for_nongroups);
1729
32131a9c
RK
1730 for (class = 0; class < N_REG_CLASSES; class++)
1731 {
1732 /* First get the groups of registers.
1733 If we got single registers first, we might fragment
1734 possible groups. */
1735 while (max_groups[class] > 0)
1736 {
1737 /* If any single spilled regs happen to form groups,
1738 count them now. Maybe we don't really need
1739 to spill another group. */
066aca28
RK
1740 count_possible_groups (group_size, group_mode, max_groups,
1741 class);
32131a9c 1742
93193ab5
RK
1743 if (max_groups[class] <= 0)
1744 break;
1745
32131a9c
RK
1746 /* Groups of size 2 (the only groups used on most machines)
1747 are treated specially. */
1748 if (group_size[class] == 2)
1749 {
1750 /* First, look for a register that will complete a group. */
1751 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1752 {
32131a9c 1753 int other;
546b63fb
RK
1754
1755 j = potential_reload_regs[i];
32131a9c
RK
1756 if (j >= 0 && ! TEST_HARD_REG_BIT (bad_spill_regs, j)
1757 &&
1758 ((j > 0 && (other = j - 1, spill_reg_order[other] >= 0)
1759 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1760 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1761 && HARD_REGNO_MODE_OK (other, group_mode[class])
1762 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1763 other)
1764 /* We don't want one part of another group.
1765 We could get "two groups" that overlap! */
1766 && ! TEST_HARD_REG_BIT (counted_for_groups, other))
1767 ||
1768 (j < FIRST_PSEUDO_REGISTER - 1
1769 && (other = j + 1, spill_reg_order[other] >= 0)
1770 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1771 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1772 && HARD_REGNO_MODE_OK (j, group_mode[class])
1773 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1774 other)
1775 && ! TEST_HARD_REG_BIT (counted_for_groups,
1776 other))))
1777 {
1778 register enum reg_class *p;
1779
1780 /* We have found one that will complete a group,
1781 so count off one group as provided. */
1782 max_groups[class]--;
1783 p = reg_class_superclasses[class];
1784 while (*p != LIM_REG_CLASSES)
d601d5da
JW
1785 {
1786 if (group_size [(int) *p] <= group_size [class])
1787 max_groups[(int) *p]--;
1788 p++;
1789 }
32131a9c
RK
1790
1791 /* Indicate both these regs are part of a group. */
1792 SET_HARD_REG_BIT (counted_for_groups, j);
1793 SET_HARD_REG_BIT (counted_for_groups, other);
1794 break;
1795 }
1796 }
1797 /* We can't complete a group, so start one. */
92b0556d 1798 /* Look for a pair neither of which is explicitly used. */
f95182a4 1799 if (SMALL_REGISTER_CLASSES && i == FIRST_PSEUDO_REGISTER)
92b0556d
RS
1800 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1801 {
1802 int k;
1803 j = potential_reload_regs[i];
1804 /* Verify that J+1 is a potential reload reg. */
1805 for (k = 0; k < FIRST_PSEUDO_REGISTER; k++)
1806 if (potential_reload_regs[k] == j + 1)
1807 break;
1808 if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1809 && k < FIRST_PSEUDO_REGISTER
1810 && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1811 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1812 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1813 && HARD_REGNO_MODE_OK (j, group_mode[class])
1814 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1815 j + 1)
1816 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + 1)
1817 /* Reject J at this stage
1818 if J+1 was explicitly used. */
1819 && ! regs_explicitly_used[j + 1])
1820 break;
1821 }
92b0556d
RS
1822 /* Now try any group at all
1823 whose registers are not in bad_spill_regs. */
32131a9c
RK
1824 if (i == FIRST_PSEUDO_REGISTER)
1825 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1826 {
57697575 1827 int k;
546b63fb 1828 j = potential_reload_regs[i];
57697575
RS
1829 /* Verify that J+1 is a potential reload reg. */
1830 for (k = 0; k < FIRST_PSEUDO_REGISTER; k++)
1831 if (potential_reload_regs[k] == j + 1)
1832 break;
32131a9c 1833 if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
57697575 1834 && k < FIRST_PSEUDO_REGISTER
32131a9c
RK
1835 && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1836 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1837 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1838 && HARD_REGNO_MODE_OK (j, group_mode[class])
1839 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
ee9f63c6
RS
1840 j + 1)
1841 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + 1))
32131a9c
RK
1842 break;
1843 }
1844
1845 /* I should be the index in potential_reload_regs
1846 of the new reload reg we have found. */
1847
5352b11a
RS
1848 if (i >= FIRST_PSEUDO_REGISTER)
1849 {
1850 /* There are no groups left to spill. */
1851 spill_failure (max_groups_insn[class]);
1852 failure = 1;
1853 goto failed;
1854 }
1855 else
1856 something_changed
fb3821f7 1857 |= new_spill_reg (i, class, max_needs, NULL_PTR,
5352b11a 1858 global, dumpfile);
32131a9c
RK
1859 }
1860 else
1861 {
1862 /* For groups of more than 2 registers,
1863 look for a sufficient sequence of unspilled registers,
1864 and spill them all at once. */
1865 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1866 {
32131a9c 1867 int k;
546b63fb
RK
1868
1869 j = potential_reload_regs[i];
9d1a4667
RS
1870 if (j >= 0
1871 && j + group_size[class] <= FIRST_PSEUDO_REGISTER
32131a9c
RK
1872 && HARD_REGNO_MODE_OK (j, group_mode[class]))
1873 {
1874 /* Check each reg in the sequence. */
1875 for (k = 0; k < group_size[class]; k++)
1876 if (! (spill_reg_order[j + k] < 0
1877 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + k)
1878 && TEST_HARD_REG_BIT (reg_class_contents[class], j + k)))
1879 break;
1880 /* We got a full sequence, so spill them all. */
1881 if (k == group_size[class])
1882 {
1883 register enum reg_class *p;
1884 for (k = 0; k < group_size[class]; k++)
1885 {
1886 int idx;
1887 SET_HARD_REG_BIT (counted_for_groups, j + k);
1888 for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
1889 if (potential_reload_regs[idx] == j + k)
1890 break;
9d1a4667
RS
1891 something_changed
1892 |= new_spill_reg (idx, class,
1893 max_needs, NULL_PTR,
1894 global, dumpfile);
32131a9c
RK
1895 }
1896
1897 /* We have found one that will complete a group,
1898 so count off one group as provided. */
1899 max_groups[class]--;
1900 p = reg_class_superclasses[class];
1901 while (*p != LIM_REG_CLASSES)
d601d5da
JW
1902 {
1903 if (group_size [(int) *p]
1904 <= group_size [class])
1905 max_groups[(int) *p]--;
1906 p++;
1907 }
32131a9c
RK
1908 break;
1909 }
1910 }
1911 }
fa52261e 1912 /* We couldn't find any registers for this reload.
9d1a4667
RS
1913 Avoid going into an infinite loop. */
1914 if (i >= FIRST_PSEUDO_REGISTER)
1915 {
1916 /* There are no groups left. */
1917 spill_failure (max_groups_insn[class]);
1918 failure = 1;
1919 goto failed;
1920 }
32131a9c
RK
1921 }
1922 }
1923
1924 /* Now similarly satisfy all need for single registers. */
1925
1926 while (max_needs[class] > 0 || max_nongroups[class] > 0)
1927 {
9a6cde3a
RS
1928 /* If we spilled enough regs, but they weren't counted
1929 against the non-group need, see if we can count them now.
1930 If so, we can avoid some actual spilling. */
1931 if (max_needs[class] <= 0 && max_nongroups[class] > 0)
1932 for (i = 0; i < n_spills; i++)
1933 if (TEST_HARD_REG_BIT (reg_class_contents[class],
1934 spill_regs[i])
1935 && !TEST_HARD_REG_BIT (counted_for_groups,
1936 spill_regs[i])
1937 && !TEST_HARD_REG_BIT (counted_for_nongroups,
1938 spill_regs[i])
1939 && max_nongroups[class] > 0)
1940 {
1941 register enum reg_class *p;
1942
1943 SET_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]);
1944 max_nongroups[class]--;
1945 p = reg_class_superclasses[class];
1946 while (*p != LIM_REG_CLASSES)
1947 max_nongroups[(int) *p++]--;
1948 }
1949 if (max_needs[class] <= 0 && max_nongroups[class] <= 0)
1950 break;
9a6cde3a 1951
32131a9c
RK
1952 /* Consider the potential reload regs that aren't
1953 yet in use as reload regs, in order of preference.
1954 Find the most preferred one that's in this class. */
1955
1956 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1957 if (potential_reload_regs[i] >= 0
1958 && TEST_HARD_REG_BIT (reg_class_contents[class],
1959 potential_reload_regs[i])
1960 /* If this reg will not be available for groups,
1961 pick one that does not foreclose possible groups.
1962 This is a kludge, and not very general,
1963 but it should be sufficient to make the 386 work,
1964 and the problem should not occur on machines with
1965 more registers. */
1966 && (max_nongroups[class] == 0
1967 || possible_group_p (potential_reload_regs[i], max_groups)))
1968 break;
1969
e404a39a
RK
1970 /* If we couldn't get a register, try to get one even if we
1971 might foreclose possible groups. This may cause problems
1972 later, but that's better than aborting now, since it is
1973 possible that we will, in fact, be able to form the needed
1974 group even with this allocation. */
1975
1976 if (i >= FIRST_PSEUDO_REGISTER
1977 && (asm_noperands (max_needs[class] > 0
1978 ? max_needs_insn[class]
1979 : max_nongroups_insn[class])
1980 < 0))
1981 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1982 if (potential_reload_regs[i] >= 0
1983 && TEST_HARD_REG_BIT (reg_class_contents[class],
1984 potential_reload_regs[i]))
1985 break;
1986
32131a9c
RK
1987 /* I should be the index in potential_reload_regs
1988 of the new reload reg we have found. */
1989
5352b11a
RS
1990 if (i >= FIRST_PSEUDO_REGISTER)
1991 {
1992 /* There are no possible registers left to spill. */
1993 spill_failure (max_needs[class] > 0 ? max_needs_insn[class]
1994 : max_nongroups_insn[class]);
1995 failure = 1;
1996 goto failed;
1997 }
1998 else
1999 something_changed
2000 |= new_spill_reg (i, class, max_needs, max_nongroups,
2001 global, dumpfile);
32131a9c
RK
2002 }
2003 }
2004 }
2005
2006 /* If global-alloc was run, notify it of any register eliminations we have
2007 done. */
2008 if (global)
2009 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2010 if (ep->can_eliminate)
2011 mark_elimination (ep->from, ep->to);
2012
32131a9c 2013 /* Insert code to save and restore call-clobbered hard regs
a8efe40d
RK
2014 around calls. Tell if what mode to use so that we will process
2015 those insns in reload_as_needed if we have to. */
32131a9c
RK
2016
2017 if (caller_save_needed)
a8efe40d
RK
2018 save_call_clobbered_regs (num_eliminable ? QImode
2019 : caller_save_spill_class != NO_REGS ? HImode
2020 : VOIDmode);
32131a9c
RK
2021
2022 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
2023 If that insn didn't set the register (i.e., it copied the register to
2024 memory), just delete that insn instead of the equivalencing insn plus
2025 anything now dead. If we call delete_dead_insn on that insn, we may
2026 delete the insn that actually sets the register if the register die
2027 there and that is incorrect. */
2028
2029 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2030 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0
2031 && GET_CODE (reg_equiv_init[i]) != NOTE)
2032 {
2033 if (reg_set_p (regno_reg_rtx[i], PATTERN (reg_equiv_init[i])))
2034 delete_dead_insn (reg_equiv_init[i]);
2035 else
2036 {
2037 PUT_CODE (reg_equiv_init[i], NOTE);
2038 NOTE_SOURCE_FILE (reg_equiv_init[i]) = 0;
2039 NOTE_LINE_NUMBER (reg_equiv_init[i]) = NOTE_INSN_DELETED;
2040 }
2041 }
2042
2043 /* Use the reload registers where necessary
2044 by generating move instructions to move the must-be-register
2045 values into or out of the reload registers. */
2046
a8efe40d
RK
2047 if (something_needs_reloads || something_needs_elimination
2048 || (caller_save_needed && num_eliminable)
2049 || caller_save_spill_class != NO_REGS)
32131a9c
RK
2050 reload_as_needed (first, global);
2051
2a1f8b6b 2052 /* If we were able to eliminate the frame pointer, show that it is no
546b63fb 2053 longer live at the start of any basic block. If it ls live by
2a1f8b6b
RK
2054 virtue of being in a pseudo, that pseudo will be marked live
2055 and hence the frame pointer will be known to be live via that
2056 pseudo. */
2057
2058 if (! frame_pointer_needed)
2059 for (i = 0; i < n_basic_blocks; i++)
8e08106d
MM
2060 CLEAR_REGNO_REG_SET (basic_block_live_at_start[i],
2061 HARD_FRAME_POINTER_REGNUM);
2a1f8b6b 2062
5352b11a
RS
2063 /* Come here (with failure set nonzero) if we can't get enough spill regs
2064 and we decide not to abort about it. */
2065 failed:
2066
a3ec87a8
RS
2067 reload_in_progress = 0;
2068
32131a9c
RK
2069 /* Now eliminate all pseudo regs by modifying them into
2070 their equivalent memory references.
2071 The REG-rtx's for the pseudos are modified in place,
2072 so all insns that used to refer to them now refer to memory.
2073
2074 For a reg that has a reg_equiv_address, all those insns
2075 were changed by reloading so that no insns refer to it any longer;
2076 but the DECL_RTL of a variable decl may refer to it,
2077 and if so this causes the debugging info to mention the variable. */
2078
2079 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2080 {
2081 rtx addr = 0;
ab1fd483 2082 int in_struct = 0;
32131a9c 2083 if (reg_equiv_mem[i])
ab1fd483
RS
2084 {
2085 addr = XEXP (reg_equiv_mem[i], 0);
2086 in_struct = MEM_IN_STRUCT_P (reg_equiv_mem[i]);
2087 }
32131a9c
RK
2088 if (reg_equiv_address[i])
2089 addr = reg_equiv_address[i];
2090 if (addr)
2091 {
2092 if (reg_renumber[i] < 0)
2093 {
2094 rtx reg = regno_reg_rtx[i];
2095 XEXP (reg, 0) = addr;
2096 REG_USERVAR_P (reg) = 0;
ab1fd483 2097 MEM_IN_STRUCT_P (reg) = in_struct;
32131a9c
RK
2098 PUT_CODE (reg, MEM);
2099 }
2100 else if (reg_equiv_mem[i])
2101 XEXP (reg_equiv_mem[i], 0) = addr;
2102 }
2103 }
2104
b60a8416
R
2105 /* Make a pass over all the insns and delete all USEs which we inserted
2106 only to tag a REG_EQUAL note on them; if PRESERVE_DEATH_INFO_REGNO_P
2107 is defined, also remove death notes for things that are no longer
2108 registers or no longer die in the insn (e.g., an input and output
2109 pseudo being tied). */
32131a9c
RK
2110
2111 for (insn = first; insn; insn = NEXT_INSN (insn))
2112 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2113 {
487a6e06 2114#ifdef PRESERVE_DEATH_INFO_REGNO_P
32131a9c 2115 rtx note, next;
487a6e06 2116#endif
32131a9c 2117
4d3eb414 2118 if (GET_CODE (PATTERN (insn)) == USE
b60a8416
R
2119 && find_reg_note (insn, REG_EQUAL, NULL_RTX))
2120 {
2121 PUT_CODE (insn, NOTE);
2122 NOTE_SOURCE_FILE (insn) = 0;
2123 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2124 continue;
2125 }
2126#ifdef PRESERVE_DEATH_INFO_REGNO_P
32131a9c
RK
2127 for (note = REG_NOTES (insn); note; note = next)
2128 {
2129 next = XEXP (note, 1);
2130 if (REG_NOTE_KIND (note) == REG_DEAD
2131 && (GET_CODE (XEXP (note, 0)) != REG
2132 || reg_set_p (XEXP (note, 0), PATTERN (insn))))
2133 remove_note (insn, note);
2134 }
32131a9c 2135#endif
b60a8416 2136 }
32131a9c 2137
76e0d211
RK
2138 /* If we are doing stack checking, give a warning if this function's
2139 frame size is larger than we expect. */
2140 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
2141 {
2142 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
2143
2144 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2145 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
2146 size += UNITS_PER_WORD;
2147
2148 if (size > STACK_CHECK_MAX_FRAME_SIZE)
2149 warning ("frame size too large for reliable stack checking");
2150 }
2151
32131a9c
RK
2152 /* Indicate that we no longer have known memory locations or constants. */
2153 reg_equiv_constant = 0;
2154 reg_equiv_memory_loc = 0;
5352b11a 2155
a68d4b75
BK
2156 if (real_known_ptr)
2157 free (real_known_ptr);
2158 if (real_at_ptr)
2159 free (real_at_ptr);
2160
c8ab4464
RS
2161 if (scratch_list)
2162 free (scratch_list);
c307c237 2163 scratch_list = 0;
c8ab4464
RS
2164 if (scratch_block)
2165 free (scratch_block);
c307c237
RK
2166 scratch_block = 0;
2167
8b4f9969
JW
2168 CLEAR_HARD_REG_SET (used_spill_regs);
2169 for (i = 0; i < n_spills; i++)
2170 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
2171
5352b11a 2172 return failure;
32131a9c
RK
2173}
2174\f
2175/* Nonzero if, after spilling reg REGNO for non-groups,
2176 it will still be possible to find a group if we still need one. */
2177
2178static int
2179possible_group_p (regno, max_groups)
2180 int regno;
2181 int *max_groups;
2182{
2183 int i;
2184 int class = (int) NO_REGS;
2185
2186 for (i = 0; i < (int) N_REG_CLASSES; i++)
2187 if (max_groups[i] > 0)
2188 {
2189 class = i;
2190 break;
2191 }
2192
2193 if (class == (int) NO_REGS)
2194 return 1;
2195
2196 /* Consider each pair of consecutive registers. */
2197 for (i = 0; i < FIRST_PSEUDO_REGISTER - 1; i++)
2198 {
2199 /* Ignore pairs that include reg REGNO. */
2200 if (i == regno || i + 1 == regno)
2201 continue;
2202
2203 /* Ignore pairs that are outside the class that needs the group.
2204 ??? Here we fail to handle the case where two different classes
2205 independently need groups. But this never happens with our
2206 current machine descriptions. */
2207 if (! (TEST_HARD_REG_BIT (reg_class_contents[class], i)
2208 && TEST_HARD_REG_BIT (reg_class_contents[class], i + 1)))
2209 continue;
2210
2211 /* A pair of consecutive regs we can still spill does the trick. */
2212 if (spill_reg_order[i] < 0 && spill_reg_order[i + 1] < 0
2213 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2214 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1))
2215 return 1;
2216
2217 /* A pair of one already spilled and one we can spill does it
2218 provided the one already spilled is not otherwise reserved. */
2219 if (spill_reg_order[i] < 0
2220 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2221 && spill_reg_order[i + 1] >= 0
2222 && ! TEST_HARD_REG_BIT (counted_for_groups, i + 1)
2223 && ! TEST_HARD_REG_BIT (counted_for_nongroups, i + 1))
2224 return 1;
2225 if (spill_reg_order[i + 1] < 0
2226 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1)
2227 && spill_reg_order[i] >= 0
2228 && ! TEST_HARD_REG_BIT (counted_for_groups, i)
2229 && ! TEST_HARD_REG_BIT (counted_for_nongroups, i))
2230 return 1;
2231 }
2232
2233 return 0;
2234}
2235\f
066aca28
RK
2236/* Count any groups of CLASS that can be formed from the registers recently
2237 spilled. */
32131a9c
RK
2238
2239static void
066aca28 2240count_possible_groups (group_size, group_mode, max_groups, class)
546b63fb 2241 int *group_size;
32131a9c 2242 enum machine_mode *group_mode;
546b63fb 2243 int *max_groups;
066aca28 2244 int class;
32131a9c 2245{
066aca28
RK
2246 HARD_REG_SET new;
2247 int i, j;
2248
32131a9c
RK
2249 /* Now find all consecutive groups of spilled registers
2250 and mark each group off against the need for such groups.
2251 But don't count them against ordinary need, yet. */
2252
066aca28
RK
2253 if (group_size[class] == 0)
2254 return;
2255
2256 CLEAR_HARD_REG_SET (new);
2257
2258 /* Make a mask of all the regs that are spill regs in class I. */
2259 for (i = 0; i < n_spills; i++)
2260 if (TEST_HARD_REG_BIT (reg_class_contents[class], spill_regs[i])
2261 && ! TEST_HARD_REG_BIT (counted_for_groups, spill_regs[i])
2262 && ! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
2263 SET_HARD_REG_BIT (new, spill_regs[i]);
2264
2265 /* Find each consecutive group of them. */
2266 for (i = 0; i < FIRST_PSEUDO_REGISTER && max_groups[class] > 0; i++)
2267 if (TEST_HARD_REG_BIT (new, i)
2268 && i + group_size[class] <= FIRST_PSEUDO_REGISTER
2269 && HARD_REGNO_MODE_OK (i, group_mode[class]))
32131a9c 2270 {
066aca28
RK
2271 for (j = 1; j < group_size[class]; j++)
2272 if (! TEST_HARD_REG_BIT (new, i + j))
2273 break;
32131a9c 2274
066aca28
RK
2275 if (j == group_size[class])
2276 {
2277 /* We found a group. Mark it off against this class's need for
2278 groups, and against each superclass too. */
2279 register enum reg_class *p;
2280
2281 max_groups[class]--;
2282 p = reg_class_superclasses[class];
2283 while (*p != LIM_REG_CLASSES)
d601d5da
JW
2284 {
2285 if (group_size [(int) *p] <= group_size [class])
2286 max_groups[(int) *p]--;
2287 p++;
2288 }
066aca28
RK
2289
2290 /* Don't count these registers again. */
46a70e45 2291 for (j = 0; j < group_size[class]; j++)
066aca28
RK
2292 SET_HARD_REG_BIT (counted_for_groups, i + j);
2293 }
2294
2295 /* Skip to the last reg in this group. When i is incremented above,
2296 it will then point to the first reg of the next possible group. */
2297 i += j - 1;
2298 }
32131a9c
RK
2299}
2300\f
2301/* ALLOCATE_MODE is a register mode that needs to be reloaded. OTHER_MODE is
2302 another mode that needs to be reloaded for the same register class CLASS.
2303 If any reg in CLASS allows ALLOCATE_MODE but not OTHER_MODE, fail.
2304 ALLOCATE_MODE will never be smaller than OTHER_MODE.
2305
2306 This code used to also fail if any reg in CLASS allows OTHER_MODE but not
2307 ALLOCATE_MODE. This test is unnecessary, because we will never try to put
2308 something of mode ALLOCATE_MODE into an OTHER_MODE register. Testing this
2309 causes unnecessary failures on machines requiring alignment of register
2310 groups when the two modes are different sizes, because the larger mode has
2311 more strict alignment rules than the smaller mode. */
2312
2313static int
2314modes_equiv_for_class_p (allocate_mode, other_mode, class)
2315 enum machine_mode allocate_mode, other_mode;
2316 enum reg_class class;
2317{
2318 register int regno;
2319 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2320 {
2321 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno)
2322 && HARD_REGNO_MODE_OK (regno, allocate_mode)
2323 && ! HARD_REGNO_MODE_OK (regno, other_mode))
2324 return 0;
2325 }
2326 return 1;
2327}
2328
5352b11a
RS
2329/* Handle the failure to find a register to spill.
2330 INSN should be one of the insns which needed this particular spill reg. */
2331
2332static void
2333spill_failure (insn)
2334 rtx insn;
2335{
2336 if (asm_noperands (PATTERN (insn)) >= 0)
2337 error_for_asm (insn, "`asm' needs too many reloads");
2338 else
a89b2cc4 2339 fatal_insn ("Unable to find a register to spill.", insn);
5352b11a
RS
2340}
2341
32131a9c
RK
2342/* Add a new register to the tables of available spill-registers
2343 (as well as spilling all pseudos allocated to the register).
2344 I is the index of this register in potential_reload_regs.
2345 CLASS is the regclass whose need is being satisfied.
2346 MAX_NEEDS and MAX_NONGROUPS are the vectors of needs,
2347 so that this register can count off against them.
2348 MAX_NONGROUPS is 0 if this register is part of a group.
2349 GLOBAL and DUMPFILE are the same as the args that `reload' got. */
2350
2351static int
2352new_spill_reg (i, class, max_needs, max_nongroups, global, dumpfile)
2353 int i;
2354 int class;
2355 int *max_needs;
2356 int *max_nongroups;
2357 int global;
2358 FILE *dumpfile;
2359{
2360 register enum reg_class *p;
2361 int val;
2362 int regno = potential_reload_regs[i];
2363
2364 if (i >= FIRST_PSEUDO_REGISTER)
2365 abort (); /* Caller failed to find any register. */
2366
2367 if (fixed_regs[regno] || TEST_HARD_REG_BIT (forbidden_regs, regno))
da275344
MM
2368 {
2369 static char *reg_class_names[] = REG_CLASS_NAMES;
2370 fatal ("fixed or forbidden register %d (%s) was spilled for class %s.\n\
56f58d3a 2371This may be due to a compiler bug or to impossible asm\n\
da275344
MM
2372statements or clauses.", regno, reg_names[regno], reg_class_names[class]);
2373 }
32131a9c
RK
2374
2375 /* Make reg REGNO an additional reload reg. */
2376
2377 potential_reload_regs[i] = -1;
2378 spill_regs[n_spills] = regno;
2379 spill_reg_order[regno] = n_spills;
2380 if (dumpfile)
2381 fprintf (dumpfile, "Spilling reg %d.\n", spill_regs[n_spills]);
2382
2383 /* Clear off the needs we just satisfied. */
2384
2385 max_needs[class]--;
2386 p = reg_class_superclasses[class];
2387 while (*p != LIM_REG_CLASSES)
2388 max_needs[(int) *p++]--;
2389
2390 if (max_nongroups && max_nongroups[class] > 0)
2391 {
2392 SET_HARD_REG_BIT (counted_for_nongroups, regno);
2393 max_nongroups[class]--;
2394 p = reg_class_superclasses[class];
2395 while (*p != LIM_REG_CLASSES)
2396 max_nongroups[(int) *p++]--;
2397 }
2398
2399 /* Spill every pseudo reg that was allocated to this reg
2400 or to something that overlaps this reg. */
2401
2402 val = spill_hard_reg (spill_regs[n_spills], global, dumpfile, 0);
2403
2404 /* If there are some registers still to eliminate and this register
2405 wasn't ever used before, additional stack space may have to be
2406 allocated to store this register. Thus, we may have changed the offset
2407 between the stack and frame pointers, so mark that something has changed.
2408 (If new pseudos were spilled, thus requiring more space, VAL would have
2409 been set non-zero by the call to spill_hard_reg above since additional
2410 reloads may be needed in that case.
2411
2412 One might think that we need only set VAL to 1 if this is a call-used
2413 register. However, the set of registers that must be saved by the
2414 prologue is not identical to the call-used set. For example, the
2415 register used by the call insn for the return PC is a call-used register,
2416 but must be saved by the prologue. */
2417 if (num_eliminable && ! regs_ever_live[spill_regs[n_spills]])
2418 val = 1;
2419
2420 regs_ever_live[spill_regs[n_spills]] = 1;
2421 n_spills++;
2422
2423 return val;
2424}
2425\f
2426/* Delete an unneeded INSN and any previous insns who sole purpose is loading
2427 data that is dead in INSN. */
2428
2429static void
2430delete_dead_insn (insn)
2431 rtx insn;
2432{
2433 rtx prev = prev_real_insn (insn);
2434 rtx prev_dest;
2435
2436 /* If the previous insn sets a register that dies in our insn, delete it
2437 too. */
2438 if (prev && GET_CODE (PATTERN (prev)) == SET
2439 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
2440 && reg_mentioned_p (prev_dest, PATTERN (insn))
2441 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest)))
2442 delete_dead_insn (prev);
2443
2444 PUT_CODE (insn, NOTE);
2445 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2446 NOTE_SOURCE_FILE (insn) = 0;
2447}
2448
2449/* Modify the home of pseudo-reg I.
2450 The new home is present in reg_renumber[I].
2451
2452 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2453 or it may be -1, meaning there is none or it is not relevant.
2454 This is used so that all pseudos spilled from a given hard reg
2455 can share one stack slot. */
2456
2457static void
2458alter_reg (i, from_reg)
2459 register int i;
2460 int from_reg;
2461{
2462 /* When outputting an inline function, this can happen
2463 for a reg that isn't actually used. */
2464 if (regno_reg_rtx[i] == 0)
2465 return;
2466
2467 /* If the reg got changed to a MEM at rtl-generation time,
2468 ignore it. */
2469 if (GET_CODE (regno_reg_rtx[i]) != REG)
2470 return;
2471
2472 /* Modify the reg-rtx to contain the new hard reg
2473 number or else to contain its pseudo reg number. */
2474 REGNO (regno_reg_rtx[i])
2475 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
2476
2477 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2478 allocate a stack slot for it. */
2479
2480 if (reg_renumber[i] < 0
b1f21e0a 2481 && REG_N_REFS (i) > 0
32131a9c
RK
2482 && reg_equiv_constant[i] == 0
2483 && reg_equiv_memory_loc[i] == 0)
2484 {
2485 register rtx x;
2486 int inherent_size = PSEUDO_REGNO_BYTES (i);
2487 int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2488 int adjust = 0;
2489
2490 /* Each pseudo reg has an inherent size which comes from its own mode,
2491 and a total size which provides room for paradoxical subregs
2492 which refer to the pseudo reg in wider modes.
2493
2494 We can use a slot already allocated if it provides both
2495 enough inherent space and enough total space.
2496 Otherwise, we allocate a new slot, making sure that it has no less
2497 inherent space, and no less total space, then the previous slot. */
2498 if (from_reg == -1)
2499 {
2500 /* No known place to spill from => no slot to reuse. */
cabcf079
ILT
2501 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
2502 inherent_size == total_size ? 0 : -1);
f76b9db2 2503 if (BYTES_BIG_ENDIAN)
02db8dd0
RK
2504 /* Cancel the big-endian correction done in assign_stack_local.
2505 Get the address of the beginning of the slot.
2506 This is so we can do a big-endian correction unconditionally
2507 below. */
2508 adjust = inherent_size - total_size;
2509
2510 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
32131a9c
RK
2511 }
2512 /* Reuse a stack slot if possible. */
2513 else if (spill_stack_slot[from_reg] != 0
2514 && spill_stack_slot_width[from_reg] >= total_size
2515 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2516 >= inherent_size))
2517 x = spill_stack_slot[from_reg];
2518 /* Allocate a bigger slot. */
2519 else
2520 {
2521 /* Compute maximum size needed, both for inherent size
2522 and for total size. */
2523 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
4f2d3674 2524 rtx stack_slot;
32131a9c
RK
2525 if (spill_stack_slot[from_reg])
2526 {
2527 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2528 > inherent_size)
2529 mode = GET_MODE (spill_stack_slot[from_reg]);
2530 if (spill_stack_slot_width[from_reg] > total_size)
2531 total_size = spill_stack_slot_width[from_reg];
2532 }
2533 /* Make a slot with that size. */
cabcf079
ILT
2534 x = assign_stack_local (mode, total_size,
2535 inherent_size == total_size ? 0 : -1);
4f2d3674 2536 stack_slot = x;
f76b9db2
ILT
2537 if (BYTES_BIG_ENDIAN)
2538 {
2539 /* Cancel the big-endian correction done in assign_stack_local.
2540 Get the address of the beginning of the slot.
2541 This is so we can do a big-endian correction unconditionally
2542 below. */
2543 adjust = GET_MODE_SIZE (mode) - total_size;
4f2d3674 2544 if (adjust)
38a448ca
RH
2545 stack_slot = gen_rtx_MEM (mode_for_size (total_size
2546 * BITS_PER_UNIT,
2547 MODE_INT, 1),
02db8dd0 2548 plus_constant (XEXP (x, 0), adjust));
f76b9db2 2549 }
4f2d3674 2550 spill_stack_slot[from_reg] = stack_slot;
32131a9c
RK
2551 spill_stack_slot_width[from_reg] = total_size;
2552 }
2553
32131a9c
RK
2554 /* On a big endian machine, the "address" of the slot
2555 is the address of the low part that fits its inherent mode. */
f76b9db2 2556 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
32131a9c 2557 adjust += (total_size - inherent_size);
32131a9c
RK
2558
2559 /* If we have any adjustment to make, or if the stack slot is the
2560 wrong mode, make a new stack slot. */
2561 if (adjust != 0 || GET_MODE (x) != GET_MODE (regno_reg_rtx[i]))
2562 {
38a448ca 2563 x = gen_rtx_MEM (GET_MODE (regno_reg_rtx[i]),
32131a9c
RK
2564 plus_constant (XEXP (x, 0), adjust));
2565 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2566 }
2567
2568 /* Save the stack slot for later. */
2569 reg_equiv_memory_loc[i] = x;
2570 }
2571}
2572
2573/* Mark the slots in regs_ever_live for the hard regs
2574 used by pseudo-reg number REGNO. */
2575
2576void
2577mark_home_live (regno)
2578 int regno;
2579{
2580 register int i, lim;
2581 i = reg_renumber[regno];
2582 if (i < 0)
2583 return;
2584 lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2585 while (i < lim)
2586 regs_ever_live[i++] = 1;
2587}
c307c237
RK
2588
2589/* Mark the registers used in SCRATCH as being live. */
2590
2591static void
2592mark_scratch_live (scratch)
2593 rtx scratch;
2594{
2595 register int i;
2596 int regno = REGNO (scratch);
2597 int lim = regno + HARD_REGNO_NREGS (regno, GET_MODE (scratch));
2598
2599 for (i = regno; i < lim; i++)
2600 regs_ever_live[i] = 1;
2601}
32131a9c
RK
2602\f
2603/* This function handles the tracking of elimination offsets around branches.
2604
2605 X is a piece of RTL being scanned.
2606
2607 INSN is the insn that it came from, if any.
2608
2609 INITIAL_P is non-zero if we are to set the offset to be the initial
2610 offset and zero if we are setting the offset of the label to be the
2611 current offset. */
2612
2613static void
2614set_label_offsets (x, insn, initial_p)
2615 rtx x;
2616 rtx insn;
2617 int initial_p;
2618{
2619 enum rtx_code code = GET_CODE (x);
2620 rtx tem;
2621 int i;
2622 struct elim_table *p;
2623
2624 switch (code)
2625 {
2626 case LABEL_REF:
8be386d9
RS
2627 if (LABEL_REF_NONLOCAL_P (x))
2628 return;
2629
32131a9c
RK
2630 x = XEXP (x, 0);
2631
0f41302f 2632 /* ... fall through ... */
32131a9c
RK
2633
2634 case CODE_LABEL:
2635 /* If we know nothing about this label, set the desired offsets. Note
2636 that this sets the offset at a label to be the offset before a label
2637 if we don't know anything about the label. This is not correct for
2638 the label after a BARRIER, but is the best guess we can make. If
2639 we guessed wrong, we will suppress an elimination that might have
2640 been possible had we been able to guess correctly. */
2641
2642 if (! offsets_known_at[CODE_LABEL_NUMBER (x)])
2643 {
2644 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2645 offsets_at[CODE_LABEL_NUMBER (x)][i]
2646 = (initial_p ? reg_eliminate[i].initial_offset
2647 : reg_eliminate[i].offset);
2648 offsets_known_at[CODE_LABEL_NUMBER (x)] = 1;
2649 }
2650
2651 /* Otherwise, if this is the definition of a label and it is
d45cf215 2652 preceded by a BARRIER, set our offsets to the known offset of
32131a9c
RK
2653 that label. */
2654
2655 else if (x == insn
2656 && (tem = prev_nonnote_insn (insn)) != 0
2657 && GET_CODE (tem) == BARRIER)
2a4b5f3b
RK
2658 {
2659 num_not_at_initial_offset = 0;
2660 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2661 {
2662 reg_eliminate[i].offset = reg_eliminate[i].previous_offset
2663 = offsets_at[CODE_LABEL_NUMBER (x)][i];
1d0d98f3
RK
2664 if (reg_eliminate[i].can_eliminate
2665 && (reg_eliminate[i].offset
2666 != reg_eliminate[i].initial_offset))
2a4b5f3b
RK
2667 num_not_at_initial_offset++;
2668 }
2669 }
32131a9c
RK
2670
2671 else
2672 /* If neither of the above cases is true, compare each offset
2673 with those previously recorded and suppress any eliminations
2674 where the offsets disagree. */
a8fdc208 2675
32131a9c
RK
2676 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2677 if (offsets_at[CODE_LABEL_NUMBER (x)][i]
2678 != (initial_p ? reg_eliminate[i].initial_offset
2679 : reg_eliminate[i].offset))
2680 reg_eliminate[i].can_eliminate = 0;
2681
2682 return;
2683
2684 case JUMP_INSN:
2685 set_label_offsets (PATTERN (insn), insn, initial_p);
2686
0f41302f 2687 /* ... fall through ... */
32131a9c
RK
2688
2689 case INSN:
2690 case CALL_INSN:
2691 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2692 and hence must have all eliminations at their initial offsets. */
2693 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2694 if (REG_NOTE_KIND (tem) == REG_LABEL)
2695 set_label_offsets (XEXP (tem, 0), insn, 1);
2696 return;
2697
2698 case ADDR_VEC:
2699 case ADDR_DIFF_VEC:
2700 /* Each of the labels in the address vector must be at their initial
38e01259 2701 offsets. We want the first field for ADDR_VEC and the second
32131a9c
RK
2702 field for ADDR_DIFF_VEC. */
2703
2704 for (i = 0; i < XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2705 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2706 insn, initial_p);
2707 return;
2708
2709 case SET:
2710 /* We only care about setting PC. If the source is not RETURN,
2711 IF_THEN_ELSE, or a label, disable any eliminations not at
2712 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2713 isn't one of those possibilities. For branches to a label,
2714 call ourselves recursively.
2715
2716 Note that this can disable elimination unnecessarily when we have
2717 a non-local goto since it will look like a non-constant jump to
2718 someplace in the current function. This isn't a significant
2719 problem since such jumps will normally be when all elimination
2720 pairs are back to their initial offsets. */
2721
2722 if (SET_DEST (x) != pc_rtx)
2723 return;
2724
2725 switch (GET_CODE (SET_SRC (x)))
2726 {
2727 case PC:
2728 case RETURN:
2729 return;
2730
2731 case LABEL_REF:
2732 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2733 return;
2734
2735 case IF_THEN_ELSE:
2736 tem = XEXP (SET_SRC (x), 1);
2737 if (GET_CODE (tem) == LABEL_REF)
2738 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2739 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2740 break;
2741
2742 tem = XEXP (SET_SRC (x), 2);
2743 if (GET_CODE (tem) == LABEL_REF)
2744 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2745 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2746 break;
2747 return;
e9a25f70
JL
2748
2749 default:
2750 break;
32131a9c
RK
2751 }
2752
2753 /* If we reach here, all eliminations must be at their initial
2754 offset because we are doing a jump to a variable address. */
2755 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2756 if (p->offset != p->initial_offset)
2757 p->can_eliminate = 0;
e9a25f70
JL
2758 break;
2759
2760 default:
2761 break;
32131a9c
RK
2762 }
2763}
2764\f
2765/* Used for communication between the next two function to properly share
2766 the vector for an ASM_OPERANDS. */
2767
2768static struct rtvec_def *old_asm_operands_vec, *new_asm_operands_vec;
2769
a8fdc208 2770/* Scan X and replace any eliminable registers (such as fp) with a
32131a9c
RK
2771 replacement (such as sp), plus an offset.
2772
2773 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2774 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2775 MEM, we are allowed to replace a sum of a register and the constant zero
2776 with the register, which we cannot do outside a MEM. In addition, we need
2777 to record the fact that a register is referenced outside a MEM.
2778
ff32812a 2779 If INSN is an insn, it is the insn containing X. If we replace a REG
32131a9c
RK
2780 in a SET_DEST with an equivalent MEM and INSN is non-zero, write a
2781 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
38e01259 2782 the REG is being modified.
32131a9c 2783
ff32812a
RS
2784 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2785 That's used when we eliminate in expressions stored in notes.
2786 This means, do not set ref_outside_mem even if the reference
2787 is outside of MEMs.
2788
32131a9c
RK
2789 If we see a modification to a register we know about, take the
2790 appropriate action (see case SET, below).
2791
2792 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2793 replacements done assuming all offsets are at their initial values. If
2794 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2795 encounter, return the actual location so that find_reloads will do
2796 the proper thing. */
2797
2798rtx
1914f5da 2799eliminate_regs (x, mem_mode, insn)
32131a9c
RK
2800 rtx x;
2801 enum machine_mode mem_mode;
2802 rtx insn;
2803{
2804 enum rtx_code code = GET_CODE (x);
2805 struct elim_table *ep;
2806 int regno;
2807 rtx new;
2808 int i, j;
2809 char *fmt;
2810 int copied = 0;
2811
2812 switch (code)
2813 {
2814 case CONST_INT:
2815 case CONST_DOUBLE:
2816 case CONST:
2817 case SYMBOL_REF:
2818 case CODE_LABEL:
2819 case PC:
2820 case CC0:
2821 case ASM_INPUT:
2822 case ADDR_VEC:
2823 case ADDR_DIFF_VEC:
2824 case RETURN:
2825 return x;
2826
e9a25f70
JL
2827 case ADDRESSOF:
2828 /* This is only for the benefit of the debugging backends, which call
2829 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2830 removed after CSE. */
1914f5da 2831 new = eliminate_regs (XEXP (x, 0), 0, insn);
e9a25f70
JL
2832 if (GET_CODE (new) == MEM)
2833 return XEXP (new, 0);
2834 return x;
2835
32131a9c
RK
2836 case REG:
2837 regno = REGNO (x);
2838
2839 /* First handle the case where we encounter a bare register that
2840 is eliminable. Replace it with a PLUS. */
2841 if (regno < FIRST_PSEUDO_REGISTER)
2842 {
2843 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2844 ep++)
2845 if (ep->from_rtx == x && ep->can_eliminate)
2846 {
ff32812a
RS
2847 if (! mem_mode
2848 /* Refs inside notes don't count for this purpose. */
fe089a90 2849 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
ff32812a 2850 || GET_CODE (insn) == INSN_LIST)))
32131a9c
RK
2851 ep->ref_outside_mem = 1;
2852 return plus_constant (ep->to_rtx, ep->previous_offset);
2853 }
2854
2855 }
2856 else if (reg_equiv_memory_loc && reg_equiv_memory_loc[regno]
2857 && (reg_equiv_address[regno] || num_not_at_initial_offset))
2858 {
2859 /* In this case, find_reloads would attempt to either use an
2860 incorrect address (if something is not at its initial offset)
2861 or substitute an replaced address into an insn (which loses
2862 if the offset is changed by some later action). So we simply
2863 return the replaced stack slot (assuming it is changed by
2864 elimination) and ignore the fact that this is actually a
2865 reference to the pseudo. Ensure we make a copy of the
2866 address in case it is shared. */
1914f5da 2867 new = eliminate_regs (reg_equiv_memory_loc[regno], mem_mode, insn);
32131a9c 2868 if (new != reg_equiv_memory_loc[regno])
208dffa5 2869 {
b60a8416
R
2870 if (insn != 0 && GET_CODE (insn) != EXPR_LIST
2871 && GET_CODE (insn) != INSN_LIST)
2872 REG_NOTES (emit_insn_before (gen_rtx_USE (VOIDmode, x), insn))
2873 = gen_rtx_EXPR_LIST (REG_EQUAL, new, NULL_RTX);
208dffa5
RS
2874 return copy_rtx (new);
2875 }
32131a9c
RK
2876 }
2877 return x;
2878
2879 case PLUS:
2880 /* If this is the sum of an eliminable register and a constant, rework
2881 the sum. */
2882 if (GET_CODE (XEXP (x, 0)) == REG
2883 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2884 && CONSTANT_P (XEXP (x, 1)))
2885 {
2886 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2887 ep++)
2888 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2889 {
e5687447
JW
2890 if (! mem_mode
2891 /* Refs inside notes don't count for this purpose. */
2892 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2893 || GET_CODE (insn) == INSN_LIST)))
32131a9c
RK
2894 ep->ref_outside_mem = 1;
2895
2896 /* The only time we want to replace a PLUS with a REG (this
2897 occurs when the constant operand of the PLUS is the negative
2898 of the offset) is when we are inside a MEM. We won't want
2899 to do so at other times because that would change the
2900 structure of the insn in a way that reload can't handle.
2901 We special-case the commonest situation in
2902 eliminate_regs_in_insn, so just replace a PLUS with a
2903 PLUS here, unless inside a MEM. */
a23b64d5 2904 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
32131a9c
RK
2905 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2906 return ep->to_rtx;
2907 else
38a448ca
RH
2908 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2909 plus_constant (XEXP (x, 1),
2910 ep->previous_offset));
32131a9c
RK
2911 }
2912
2913 /* If the register is not eliminable, we are done since the other
2914 operand is a constant. */
2915 return x;
2916 }
2917
2918 /* If this is part of an address, we want to bring any constant to the
2919 outermost PLUS. We will do this by doing register replacement in
2920 our operands and seeing if a constant shows up in one of them.
2921
2922 We assume here this is part of an address (or a "load address" insn)
2923 since an eliminable register is not likely to appear in any other
2924 context.
2925
2926 If we have (plus (eliminable) (reg)), we want to produce
930aeef3 2927 (plus (plus (replacement) (reg) (const))). If this was part of a
32131a9c
RK
2928 normal add insn, (plus (replacement) (reg)) will be pushed as a
2929 reload. This is the desired action. */
2930
2931 {
1914f5da
RH
2932 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2933 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
32131a9c
RK
2934
2935 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2936 {
2937 /* If one side is a PLUS and the other side is a pseudo that
a8fdc208 2938 didn't get a hard register but has a reg_equiv_constant,
32131a9c
RK
2939 we must replace the constant here since it may no longer
2940 be in the position of any operand. */
2941 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2942 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2943 && reg_renumber[REGNO (new1)] < 0
2944 && reg_equiv_constant != 0
2945 && reg_equiv_constant[REGNO (new1)] != 0)
2946 new1 = reg_equiv_constant[REGNO (new1)];
2947 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2948 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2949 && reg_renumber[REGNO (new0)] < 0
2950 && reg_equiv_constant[REGNO (new0)] != 0)
2951 new0 = reg_equiv_constant[REGNO (new0)];
2952
2953 new = form_sum (new0, new1);
2954
2955 /* As above, if we are not inside a MEM we do not want to
2956 turn a PLUS into something else. We might try to do so here
2957 for an addition of 0 if we aren't optimizing. */
2958 if (! mem_mode && GET_CODE (new) != PLUS)
38a448ca 2959 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
32131a9c
RK
2960 else
2961 return new;
2962 }
2963 }
2964 return x;
2965
981c7390
RK
2966 case MULT:
2967 /* If this is the product of an eliminable register and a
2968 constant, apply the distribute law and move the constant out
2969 so that we have (plus (mult ..) ..). This is needed in order
9faa82d8 2970 to keep load-address insns valid. This case is pathological.
981c7390
RK
2971 We ignore the possibility of overflow here. */
2972 if (GET_CODE (XEXP (x, 0)) == REG
2973 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2974 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2975 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2976 ep++)
2977 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2978 {
2979 if (! mem_mode
2980 /* Refs inside notes don't count for this purpose. */
2981 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2982 || GET_CODE (insn) == INSN_LIST)))
2983 ep->ref_outside_mem = 1;
2984
2985 return
38a448ca 2986 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
981c7390
RK
2987 ep->previous_offset * INTVAL (XEXP (x, 1)));
2988 }
32131a9c 2989
0f41302f 2990 /* ... fall through ... */
32131a9c 2991
32131a9c
RK
2992 case CALL:
2993 case COMPARE:
930aeef3 2994 case MINUS:
32131a9c
RK
2995 case DIV: case UDIV:
2996 case MOD: case UMOD:
2997 case AND: case IOR: case XOR:
45620ed4
RK
2998 case ROTATERT: case ROTATE:
2999 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
32131a9c
RK
3000 case NE: case EQ:
3001 case GE: case GT: case GEU: case GTU:
3002 case LE: case LT: case LEU: case LTU:
3003 {
1914f5da 3004 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
fb3821f7 3005 rtx new1
1914f5da 3006 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
32131a9c
RK
3007
3008 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
38a448ca 3009 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
32131a9c
RK
3010 }
3011 return x;
3012
981c7390
RK
3013 case EXPR_LIST:
3014 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
3015 if (XEXP (x, 0))
3016 {
1914f5da 3017 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
981c7390 3018 if (new != XEXP (x, 0))
38a448ca 3019 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
981c7390
RK
3020 }
3021
0f41302f 3022 /* ... fall through ... */
981c7390
RK
3023
3024 case INSN_LIST:
3025 /* Now do eliminations in the rest of the chain. If this was
3026 an EXPR_LIST, this might result in allocating more memory than is
3027 strictly needed, but it simplifies the code. */
3028 if (XEXP (x, 1))
3029 {
1914f5da 3030 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
981c7390 3031 if (new != XEXP (x, 1))
38a448ca 3032 return gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
981c7390
RK
3033 }
3034 return x;
3035
32131a9c
RK
3036 case PRE_INC:
3037 case POST_INC:
3038 case PRE_DEC:
3039 case POST_DEC:
3040 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3041 if (ep->to_rtx == XEXP (x, 0))
3042 {
4c05b187
RK
3043 int size = GET_MODE_SIZE (mem_mode);
3044
3045 /* If more bytes than MEM_MODE are pushed, account for them. */
3046#ifdef PUSH_ROUNDING
3047 if (ep->to_rtx == stack_pointer_rtx)
3048 size = PUSH_ROUNDING (size);
3049#endif
32131a9c 3050 if (code == PRE_DEC || code == POST_DEC)
4c05b187 3051 ep->offset += size;
32131a9c 3052 else
4c05b187 3053 ep->offset -= size;
32131a9c
RK
3054 }
3055
3056 /* Fall through to generic unary operation case. */
32131a9c
RK
3057 case STRICT_LOW_PART:
3058 case NEG: case NOT:
3059 case SIGN_EXTEND: case ZERO_EXTEND:
3060 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3061 case FLOAT: case FIX:
3062 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3063 case ABS:
3064 case SQRT:
3065 case FFS:
1914f5da 3066 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
32131a9c 3067 if (new != XEXP (x, 0))
38a448ca 3068 return gen_rtx_fmt_e (code, GET_MODE (x), new);
32131a9c
RK
3069 return x;
3070
3071 case SUBREG:
3072 /* Similar to above processing, but preserve SUBREG_WORD.
3073 Convert (subreg (mem)) to (mem) if not paradoxical.
3074 Also, if we have a non-paradoxical (subreg (pseudo)) and the
3075 pseudo didn't get a hard reg, we must replace this with the
3076 eliminated version of the memory location because push_reloads
3077 may do the replacement in certain circumstances. */
3078 if (GET_CODE (SUBREG_REG (x)) == REG
3079 && (GET_MODE_SIZE (GET_MODE (x))
3080 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3081 && reg_equiv_memory_loc != 0
3082 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
3083 {
3084 new = eliminate_regs (reg_equiv_memory_loc[REGNO (SUBREG_REG (x))],
1914f5da 3085 mem_mode, insn);
32131a9c
RK
3086
3087 /* If we didn't change anything, we must retain the pseudo. */
3088 if (new == reg_equiv_memory_loc[REGNO (SUBREG_REG (x))])
59e2c378 3089 new = SUBREG_REG (x);
32131a9c 3090 else
59e2c378 3091 {
59e2c378
RK
3092 /* In this case, we must show that the pseudo is used in this
3093 insn so that delete_output_reload will do the right thing. */
3094 if (insn != 0 && GET_CODE (insn) != EXPR_LIST
3095 && GET_CODE (insn) != INSN_LIST)
b60a8416
R
3096 REG_NOTES (emit_insn_before (gen_rtx_USE (VOIDmode,
3097 SUBREG_REG (x)),
3098 insn))
3099 = gen_rtx_EXPR_LIST (REG_EQUAL, new, NULL_RTX);
3100
3101 /* Ensure NEW isn't shared in case we have to reload it. */
3102 new = copy_rtx (new);
59e2c378 3103 }
32131a9c
RK
3104 }
3105 else
1914f5da 3106 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
32131a9c
RK
3107
3108 if (new != XEXP (x, 0))
3109 {
29ae5012
RK
3110 int x_size = GET_MODE_SIZE (GET_MODE (x));
3111 int new_size = GET_MODE_SIZE (GET_MODE (new));
3112
1914f5da 3113 if (GET_CODE (new) == MEM
6d49a073 3114 && ((x_size < new_size
1914f5da 3115#ifdef WORD_REGISTER_OPERATIONS
6d49a073
JW
3116 /* On these machines, combine can create rtl of the form
3117 (set (subreg:m1 (reg:m2 R) 0) ...)
3118 where m1 < m2, and expects something interesting to
3119 happen to the entire word. Moreover, it will use the
3120 (reg:m2 R) later, expecting all bits to be preserved.
3121 So if the number of words is the same, preserve the
3122 subreg so that push_reloads can see it. */
3123 && ! ((x_size-1)/UNITS_PER_WORD == (new_size-1)/UNITS_PER_WORD)
1914f5da 3124#endif
6d49a073
JW
3125 )
3126 || (x_size == new_size))
1914f5da 3127 )
32131a9c
RK
3128 {
3129 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
3130 enum machine_mode mode = GET_MODE (x);
3131
f76b9db2
ILT
3132 if (BYTES_BIG_ENDIAN)
3133 offset += (MIN (UNITS_PER_WORD,
3134 GET_MODE_SIZE (GET_MODE (new)))
3135 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
32131a9c
RK
3136
3137 PUT_MODE (new, mode);
3138 XEXP (new, 0) = plus_constant (XEXP (new, 0), offset);
3139 return new;
3140 }
3141 else
38a448ca 3142 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_WORD (x));
32131a9c
RK
3143 }
3144
3145 return x;
3146
94714ecc
RK
3147 case USE:
3148 /* If using a register that is the source of an eliminate we still
3149 think can be performed, note it cannot be performed since we don't
3150 know how this register is used. */
3151 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3152 if (ep->from_rtx == XEXP (x, 0))
3153 ep->can_eliminate = 0;
3154
1914f5da 3155 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
94714ecc 3156 if (new != XEXP (x, 0))
38a448ca 3157 return gen_rtx_fmt_e (code, GET_MODE (x), new);
94714ecc
RK
3158 return x;
3159
32131a9c
RK
3160 case CLOBBER:
3161 /* If clobbering a register that is the replacement register for an
d45cf215 3162 elimination we still think can be performed, note that it cannot
32131a9c
RK
3163 be performed. Otherwise, we need not be concerned about it. */
3164 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3165 if (ep->to_rtx == XEXP (x, 0))
3166 ep->can_eliminate = 0;
3167
1914f5da 3168 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2045084c 3169 if (new != XEXP (x, 0))
38a448ca 3170 return gen_rtx_fmt_e (code, GET_MODE (x), new);
32131a9c
RK
3171 return x;
3172
3173 case ASM_OPERANDS:
3174 {
3175 rtx *temp_vec;
3176 /* Properly handle sharing input and constraint vectors. */
3177 if (ASM_OPERANDS_INPUT_VEC (x) != old_asm_operands_vec)
3178 {
3179 /* When we come to a new vector not seen before,
3180 scan all its elements; keep the old vector if none
3181 of them changes; otherwise, make a copy. */
3182 old_asm_operands_vec = ASM_OPERANDS_INPUT_VEC (x);
3183 temp_vec = (rtx *) alloca (XVECLEN (x, 3) * sizeof (rtx));
3184 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
3185 temp_vec[i] = eliminate_regs (ASM_OPERANDS_INPUT (x, i),
1914f5da 3186 mem_mode, insn);
32131a9c
RK
3187
3188 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
3189 if (temp_vec[i] != ASM_OPERANDS_INPUT (x, i))
3190 break;
3191
3192 if (i == ASM_OPERANDS_INPUT_LENGTH (x))
3193 new_asm_operands_vec = old_asm_operands_vec;
3194 else
3195 new_asm_operands_vec
3196 = gen_rtvec_v (ASM_OPERANDS_INPUT_LENGTH (x), temp_vec);
3197 }
3198
3199 /* If we had to copy the vector, copy the entire ASM_OPERANDS. */
3200 if (new_asm_operands_vec == old_asm_operands_vec)
3201 return x;
3202
38a448ca
RH
3203 new = gen_rtx_ASM_OPERANDS (VOIDmode, ASM_OPERANDS_TEMPLATE (x),
3204 ASM_OPERANDS_OUTPUT_CONSTRAINT (x),
3205 ASM_OPERANDS_OUTPUT_IDX (x),
3206 new_asm_operands_vec,
3207 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (x),
3208 ASM_OPERANDS_SOURCE_FILE (x),
3209 ASM_OPERANDS_SOURCE_LINE (x));
32131a9c
RK
3210 new->volatil = x->volatil;
3211 return new;
3212 }
3213
3214 case SET:
3215 /* Check for setting a register that we know about. */
3216 if (GET_CODE (SET_DEST (x)) == REG)
3217 {
3218 /* See if this is setting the replacement register for an
a8fdc208 3219 elimination.
32131a9c 3220
3ec2ea3e
DE
3221 If DEST is the hard frame pointer, we do nothing because we
3222 assume that all assignments to the frame pointer are for
3223 non-local gotos and are being done at a time when they are valid
3224 and do not disturb anything else. Some machines want to
3225 eliminate a fake argument pointer (or even a fake frame pointer)
3226 with either the real frame or the stack pointer. Assignments to
3227 the hard frame pointer must not prevent this elimination. */
32131a9c
RK
3228
3229 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3230 ep++)
3231 if (ep->to_rtx == SET_DEST (x)
3ec2ea3e 3232 && SET_DEST (x) != hard_frame_pointer_rtx)
32131a9c 3233 {
6dc42e49 3234 /* If it is being incremented, adjust the offset. Otherwise,
32131a9c
RK
3235 this elimination can't be done. */
3236 rtx src = SET_SRC (x);
3237
3238 if (GET_CODE (src) == PLUS
3239 && XEXP (src, 0) == SET_DEST (x)
3240 && GET_CODE (XEXP (src, 1)) == CONST_INT)
3241 ep->offset -= INTVAL (XEXP (src, 1));
3242 else
3243 ep->can_eliminate = 0;
3244 }
3245
3246 /* Now check to see we are assigning to a register that can be
3247 eliminated. If so, it must be as part of a PARALLEL, since we
3248 will not have been called if this is a single SET. So indicate
3249 that we can no longer eliminate this reg. */
3250 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3251 ep++)
3252 if (ep->from_rtx == SET_DEST (x) && ep->can_eliminate)
3253 ep->can_eliminate = 0;
3254 }
3255
3256 /* Now avoid the loop below in this common case. */
3257 {
1914f5da
RH
3258 rtx new0 = eliminate_regs (SET_DEST (x), 0, insn);
3259 rtx new1 = eliminate_regs (SET_SRC (x), 0, insn);
32131a9c 3260
ff32812a 3261 /* If SET_DEST changed from a REG to a MEM and INSN is an insn,
32131a9c
RK
3262 write a CLOBBER insn. */
3263 if (GET_CODE (SET_DEST (x)) == REG && GET_CODE (new0) == MEM
572ca60a
RS
3264 && insn != 0 && GET_CODE (insn) != EXPR_LIST
3265 && GET_CODE (insn) != INSN_LIST)
38a448ca 3266 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, SET_DEST (x)), insn);
32131a9c
RK
3267
3268 if (new0 != SET_DEST (x) || new1 != SET_SRC (x))
38a448ca 3269 return gen_rtx_SET (VOIDmode, new0, new1);
32131a9c
RK
3270 }
3271
3272 return x;
3273
3274 case MEM:
e9a25f70
JL
3275 /* This is only for the benefit of the debugging backends, which call
3276 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
3277 removed after CSE. */
3278 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
1914f5da 3279 return eliminate_regs (XEXP (XEXP (x, 0), 0), 0, insn);
e9a25f70 3280
32131a9c
RK
3281 /* Our only special processing is to pass the mode of the MEM to our
3282 recursive call and copy the flags. While we are here, handle this
3283 case more efficiently. */
1914f5da 3284 new = eliminate_regs (XEXP (x, 0), GET_MODE (x), insn);
32131a9c
RK
3285 if (new != XEXP (x, 0))
3286 {
38a448ca 3287 new = gen_rtx_MEM (GET_MODE (x), new);
32131a9c
RK
3288 new->volatil = x->volatil;
3289 new->unchanging = x->unchanging;
3290 new->in_struct = x->in_struct;
3291 return new;
3292 }
3293 else
3294 return x;
e9a25f70
JL
3295
3296 default:
3297 break;
32131a9c
RK
3298 }
3299
3300 /* Process each of our operands recursively. If any have changed, make a
3301 copy of the rtx. */
3302 fmt = GET_RTX_FORMAT (code);
3303 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3304 {
3305 if (*fmt == 'e')
3306 {
1914f5da 3307 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
32131a9c
RK
3308 if (new != XEXP (x, i) && ! copied)
3309 {
3310 rtx new_x = rtx_alloc (code);
4c9a05bc
RK
3311 bcopy ((char *) x, (char *) new_x,
3312 (sizeof (*new_x) - sizeof (new_x->fld)
3313 + sizeof (new_x->fld[0]) * GET_RTX_LENGTH (code)));
32131a9c
RK
3314 x = new_x;
3315 copied = 1;
3316 }
3317 XEXP (x, i) = new;
3318 }
3319 else if (*fmt == 'E')
3320 {
3321 int copied_vec = 0;
3322 for (j = 0; j < XVECLEN (x, i); j++)
3323 {
1914f5da 3324 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
32131a9c
RK
3325 if (new != XVECEXP (x, i, j) && ! copied_vec)
3326 {
27108369
RK
3327 rtvec new_v = gen_rtvec_vv (XVECLEN (x, i),
3328 XVEC (x, i)->elem);
32131a9c
RK
3329 if (! copied)
3330 {
3331 rtx new_x = rtx_alloc (code);
4c9a05bc
RK
3332 bcopy ((char *) x, (char *) new_x,
3333 (sizeof (*new_x) - sizeof (new_x->fld)
3334 + (sizeof (new_x->fld[0])
3335 * GET_RTX_LENGTH (code))));
32131a9c
RK
3336 x = new_x;
3337 copied = 1;
3338 }
3339 XVEC (x, i) = new_v;
3340 copied_vec = 1;
3341 }
3342 XVECEXP (x, i, j) = new;
3343 }
3344 }
3345 }
3346
3347 return x;
3348}
3349\f
3350/* Scan INSN and eliminate all eliminable registers in it.
3351
3352 If REPLACE is nonzero, do the replacement destructively. Also
3353 delete the insn as dead it if it is setting an eliminable register.
3354
3355 If REPLACE is zero, do all our allocations in reload_obstack.
3356
3357 If no eliminations were done and this insn doesn't require any elimination
3358 processing (these are not identical conditions: it might be updating sp,
3359 but not referencing fp; this needs to be seen during reload_as_needed so
3360 that the offset between fp and sp can be taken into consideration), zero
3361 is returned. Otherwise, 1 is returned. */
3362
3363static int
3364eliminate_regs_in_insn (insn, replace)
3365 rtx insn;
3366 int replace;
3367{
3368 rtx old_body = PATTERN (insn);
774672d2 3369 rtx old_set = single_set (insn);
32131a9c
RK
3370 rtx new_body;
3371 int val = 0;
3372 struct elim_table *ep;
3373
3374 if (! replace)
3375 push_obstacks (&reload_obstack, &reload_obstack);
3376
774672d2
RK
3377 if (old_set != 0 && GET_CODE (SET_DEST (old_set)) == REG
3378 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
32131a9c
RK
3379 {
3380 /* Check for setting an eliminable register. */
3381 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
774672d2 3382 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
32131a9c 3383 {
dd1eab0a
RK
3384#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3385 /* If this is setting the frame pointer register to the
3386 hardware frame pointer register and this is an elimination
3387 that will be done (tested above), this insn is really
3388 adjusting the frame pointer downward to compensate for
3389 the adjustment done before a nonlocal goto. */
3390 if (ep->from == FRAME_POINTER_REGNUM
3391 && ep->to == HARD_FRAME_POINTER_REGNUM)
3392 {
3393 rtx src = SET_SRC (old_set);
3394 int offset, ok = 0;
8026ebba 3395 rtx prev_insn, prev_set;
dd1eab0a
RK
3396
3397 if (src == ep->to_rtx)
3398 offset = 0, ok = 1;
3399 else if (GET_CODE (src) == PLUS
3400 && GET_CODE (XEXP (src, 0)) == CONST_INT)
3401 offset = INTVAL (XEXP (src, 0)), ok = 1;
8026ebba
ILT
3402 else if ((prev_insn = prev_nonnote_insn (insn)) != 0
3403 && (prev_set = single_set (prev_insn)) != 0
3404 && rtx_equal_p (SET_DEST (prev_set), src))
3405 {
3406 src = SET_SRC (prev_set);
3407 if (src == ep->to_rtx)
3408 offset = 0, ok = 1;
3409 else if (GET_CODE (src) == PLUS
3410 && GET_CODE (XEXP (src, 0)) == CONST_INT
3411 && XEXP (src, 1) == ep->to_rtx)
3412 offset = INTVAL (XEXP (src, 0)), ok = 1;
3413 else if (GET_CODE (src) == PLUS
3414 && GET_CODE (XEXP (src, 1)) == CONST_INT
3415 && XEXP (src, 0) == ep->to_rtx)
3416 offset = INTVAL (XEXP (src, 1)), ok = 1;
3417 }
dd1eab0a
RK
3418
3419 if (ok)
3420 {
3421 if (replace)
3422 {
3423 rtx src
3424 = plus_constant (ep->to_rtx, offset - ep->offset);
3425
3426 /* First see if this insn remains valid when we
3427 make the change. If not, keep the INSN_CODE
3428 the same and let reload fit it up. */
3429 validate_change (insn, &SET_SRC (old_set), src, 1);
3430 validate_change (insn, &SET_DEST (old_set),
3431 ep->to_rtx, 1);
3432 if (! apply_change_group ())
3433 {
3434 SET_SRC (old_set) = src;
3435 SET_DEST (old_set) = ep->to_rtx;
3436 }
3437 }
3438
3439 val = 1;
3440 goto done;
3441 }
3442 }
3443#endif
3444
32131a9c
RK
3445 /* In this case this insn isn't serving a useful purpose. We
3446 will delete it in reload_as_needed once we know that this
3447 elimination is, in fact, being done.
3448
abc95ed3 3449 If REPLACE isn't set, we can't delete this insn, but needn't
32131a9c
RK
3450 process it since it won't be used unless something changes. */
3451 if (replace)
3452 delete_dead_insn (insn);
3453 val = 1;
3454 goto done;
3455 }
3456
3457 /* Check for (set (reg) (plus (reg from) (offset))) where the offset
3458 in the insn is the negative of the offset in FROM. Substitute
3459 (set (reg) (reg to)) for the insn and change its code.
3460
3461 We have to do this here, rather than in eliminate_regs, do that we can
3462 change the insn code. */
3463
774672d2
RK
3464 if (GET_CODE (SET_SRC (old_set)) == PLUS
3465 && GET_CODE (XEXP (SET_SRC (old_set), 0)) == REG
3466 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT)
32131a9c
RK
3467 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3468 ep++)
774672d2 3469 if (ep->from_rtx == XEXP (SET_SRC (old_set), 0)
922d9d40 3470 && ep->can_eliminate)
32131a9c 3471 {
922d9d40
RK
3472 /* We must stop at the first elimination that will be used.
3473 If this one would replace the PLUS with a REG, do it
3474 now. Otherwise, quit the loop and let eliminate_regs
3475 do its normal replacement. */
774672d2 3476 if (ep->offset == - INTVAL (XEXP (SET_SRC (old_set), 1)))
922d9d40 3477 {
774672d2
RK
3478 /* We assume here that we don't need a PARALLEL of
3479 any CLOBBERs for this assignment. There's not
3480 much we can do if we do need it. */
38a448ca
RH
3481 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3482 SET_DEST (old_set),
3483 ep->to_rtx);
922d9d40
RK
3484 INSN_CODE (insn) = -1;
3485 val = 1;
3486 goto done;
3487 }
3488
3489 break;
32131a9c
RK
3490 }
3491 }
3492
3493 old_asm_operands_vec = 0;
3494
3495 /* Replace the body of this insn with a substituted form. If we changed
05b4c365 3496 something, return non-zero.
32131a9c
RK
3497
3498 If we are replacing a body that was a (set X (plus Y Z)), try to
3499 re-recognize the insn. We do this in case we had a simple addition
3500 but now can do this as a load-address. This saves an insn in this
0f41302f 3501 common case. */
32131a9c 3502
1914f5da 3503 new_body = eliminate_regs (old_body, 0, replace ? insn : NULL_RTX);
32131a9c
RK
3504 if (new_body != old_body)
3505 {
7c791b13
RK
3506 /* If we aren't replacing things permanently and we changed something,
3507 make another copy to ensure that all the RTL is new. Otherwise
3508 things can go wrong if find_reload swaps commutative operands
0f41302f 3509 and one is inside RTL that has been copied while the other is not. */
7c791b13 3510
4d411872
RS
3511 /* Don't copy an asm_operands because (1) there's no need and (2)
3512 copy_rtx can't do it properly when there are multiple outputs. */
b84f9d9c 3513 if (! replace && asm_noperands (old_body) < 0)
7c791b13
RK
3514 new_body = copy_rtx (new_body);
3515
774672d2
RK
3516 /* If we had a move insn but now we don't, rerecognize it. This will
3517 cause spurious re-recognition if the old move had a PARALLEL since
3518 the new one still will, but we can't call single_set without
3519 having put NEW_BODY into the insn and the re-recognition won't
3520 hurt in this rare case. */
3521 if (old_set != 0
3522 && ((GET_CODE (SET_SRC (old_set)) == REG
3523 && (GET_CODE (new_body) != SET
3524 || GET_CODE (SET_SRC (new_body)) != REG))
3525 /* If this was a load from or store to memory, compare
3526 the MEM in recog_operand to the one in the insn. If they
3527 are not equal, then rerecognize the insn. */
3528 || (old_set != 0
3529 && ((GET_CODE (SET_SRC (old_set)) == MEM
3530 && SET_SRC (old_set) != recog_operand[1])
3531 || (GET_CODE (SET_DEST (old_set)) == MEM
3532 && SET_DEST (old_set) != recog_operand[0])))
3533 /* If this was an add insn before, rerecognize. */
3534 || GET_CODE (SET_SRC (old_set)) == PLUS))
4a5d0fb5
RS
3535 {
3536 if (! validate_change (insn, &PATTERN (insn), new_body, 0))
0ba846c7
RS
3537 /* If recognition fails, store the new body anyway.
3538 It's normal to have recognition failures here
3539 due to bizarre memory addresses; reloading will fix them. */
3540 PATTERN (insn) = new_body;
4a5d0fb5 3541 }
0ba846c7 3542 else
32131a9c
RK
3543 PATTERN (insn) = new_body;
3544
32131a9c
RK
3545 val = 1;
3546 }
a8fdc208 3547
32131a9c
RK
3548 /* Loop through all elimination pairs. See if any have changed and
3549 recalculate the number not at initial offset.
3550
a8efe40d
RK
3551 Compute the maximum offset (minimum offset if the stack does not
3552 grow downward) for each elimination pair.
3553
32131a9c
RK
3554 We also detect a cases where register elimination cannot be done,
3555 namely, if a register would be both changed and referenced outside a MEM
3556 in the resulting insn since such an insn is often undefined and, even if
3557 not, we cannot know what meaning will be given to it. Note that it is
3558 valid to have a register used in an address in an insn that changes it
3559 (presumably with a pre- or post-increment or decrement).
3560
3561 If anything changes, return nonzero. */
3562
3563 num_not_at_initial_offset = 0;
3564 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3565 {
3566 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3567 ep->can_eliminate = 0;
3568
3569 ep->ref_outside_mem = 0;
3570
3571 if (ep->previous_offset != ep->offset)
3572 val = 1;
3573
3574 ep->previous_offset = ep->offset;
3575 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3576 num_not_at_initial_offset++;
a8efe40d
RK
3577
3578#ifdef STACK_GROWS_DOWNWARD
3579 ep->max_offset = MAX (ep->max_offset, ep->offset);
3580#else
3581 ep->max_offset = MIN (ep->max_offset, ep->offset);
3582#endif
32131a9c
RK
3583 }
3584
3585 done:
9faa82d8 3586 /* If we changed something, perform elimination in REG_NOTES. This is
05b4c365
RK
3587 needed even when REPLACE is zero because a REG_DEAD note might refer
3588 to a register that we eliminate and could cause a different number
3589 of spill registers to be needed in the final reload pass than in
3590 the pre-passes. */
20748cab 3591 if (val && REG_NOTES (insn) != 0)
1914f5da 3592 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
05b4c365 3593
32131a9c
RK
3594 if (! replace)
3595 pop_obstacks ();
3596
3597 return val;
3598}
3599
3600/* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3601 replacement we currently believe is valid, mark it as not eliminable if X
3602 modifies DEST in any way other than by adding a constant integer to it.
3603
3604 If DEST is the frame pointer, we do nothing because we assume that
3ec2ea3e
DE
3605 all assignments to the hard frame pointer are nonlocal gotos and are being
3606 done at a time when they are valid and do not disturb anything else.
32131a9c 3607 Some machines want to eliminate a fake argument pointer with either the
3ec2ea3e
DE
3608 frame or stack pointer. Assignments to the hard frame pointer must not
3609 prevent this elimination.
32131a9c
RK
3610
3611 Called via note_stores from reload before starting its passes to scan
3612 the insns of the function. */
3613
3614static void
3615mark_not_eliminable (dest, x)
3616 rtx dest;
3617 rtx x;
3618{
3619 register int i;
3620
3621 /* A SUBREG of a hard register here is just changing its mode. We should
3622 not see a SUBREG of an eliminable hard register, but check just in
3623 case. */
3624 if (GET_CODE (dest) == SUBREG)
3625 dest = SUBREG_REG (dest);
3626
3ec2ea3e 3627 if (dest == hard_frame_pointer_rtx)
32131a9c
RK
3628 return;
3629
3630 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3631 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3632 && (GET_CODE (x) != SET
3633 || GET_CODE (SET_SRC (x)) != PLUS
3634 || XEXP (SET_SRC (x), 0) != dest
3635 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3636 {
3637 reg_eliminate[i].can_eliminate_previous
3638 = reg_eliminate[i].can_eliminate = 0;
3639 num_eliminable--;
3640 }
3641}
3642\f
3643/* Kick all pseudos out of hard register REGNO.
3644 If GLOBAL is nonzero, try to find someplace else to put them.
3645 If DUMPFILE is nonzero, log actions taken on that file.
3646
3647 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3648 because we found we can't eliminate some register. In the case, no pseudos
3649 are allowed to be in the register, even if they are only in a block that
3650 doesn't require spill registers, unlike the case when we are spilling this
3651 hard reg to produce another spill register.
3652
3653 Return nonzero if any pseudos needed to be kicked out. */
3654
3655static int
3656spill_hard_reg (regno, global, dumpfile, cant_eliminate)
3657 register int regno;
3658 int global;
3659 FILE *dumpfile;
3660 int cant_eliminate;
3661{
c307c237 3662 enum reg_class class = REGNO_REG_CLASS (regno);
32131a9c
RK
3663 int something_changed = 0;
3664 register int i;
3665
3666 SET_HARD_REG_BIT (forbidden_regs, regno);
3667
9ff3516a
RK
3668 if (cant_eliminate)
3669 regs_ever_live[regno] = 1;
3670
32131a9c
RK
3671 /* Spill every pseudo reg that was allocated to this reg
3672 or to something that overlaps this reg. */
3673
3674 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3675 if (reg_renumber[i] >= 0
3676 && reg_renumber[i] <= regno
a8fdc208 3677 && (reg_renumber[i]
32131a9c
RK
3678 + HARD_REGNO_NREGS (reg_renumber[i],
3679 PSEUDO_REGNO_MODE (i))
3680 > regno))
3681 {
32131a9c
RK
3682 /* If this register belongs solely to a basic block which needed no
3683 spilling of any class that this register is contained in,
3684 leave it be, unless we are spilling this register because
3685 it was a hard register that can't be eliminated. */
3686
3687 if (! cant_eliminate
3688 && basic_block_needs[0]
b1f21e0a
MM
3689 && REG_BASIC_BLOCK (i) >= 0
3690 && basic_block_needs[(int) class][REG_BASIC_BLOCK (i)] == 0)
32131a9c
RK
3691 {
3692 enum reg_class *p;
3693
3694 for (p = reg_class_superclasses[(int) class];
3695 *p != LIM_REG_CLASSES; p++)
b1f21e0a 3696 if (basic_block_needs[(int) *p][REG_BASIC_BLOCK (i)] > 0)
32131a9c 3697 break;
a8fdc208 3698
32131a9c
RK
3699 if (*p == LIM_REG_CLASSES)
3700 continue;
3701 }
3702
3703 /* Mark it as no longer having a hard register home. */
3704 reg_renumber[i] = -1;
3705 /* We will need to scan everything again. */
3706 something_changed = 1;
3707 if (global)
2c5d9e37 3708 retry_global_alloc (i, forbidden_regs);
32131a9c
RK
3709
3710 alter_reg (i, regno);
3711 if (dumpfile)
3712 {
3713 if (reg_renumber[i] == -1)
3714 fprintf (dumpfile, " Register %d now on stack.\n\n", i);
3715 else
3716 fprintf (dumpfile, " Register %d now in %d.\n\n",
3717 i, reg_renumber[i]);
3718 }
3719 }
c307c237
RK
3720 for (i = 0; i < scratch_list_length; i++)
3721 {
4fdf79cb
CM
3722 if (scratch_list[i]
3723 && regno >= REGNO (scratch_list[i])
3724 && regno < REGNO (scratch_list[i])
3725 + HARD_REGNO_NREGS (REGNO (scratch_list[i]),
3726 GET_MODE (scratch_list[i])))
c307c237
RK
3727 {
3728 if (! cant_eliminate && basic_block_needs[0]
3729 && ! basic_block_needs[(int) class][scratch_block[i]])
3730 {
3731 enum reg_class *p;
3732
3733 for (p = reg_class_superclasses[(int) class];
3734 *p != LIM_REG_CLASSES; p++)
3735 if (basic_block_needs[(int) *p][scratch_block[i]] > 0)
3736 break;
3737
3738 if (*p == LIM_REG_CLASSES)
3739 continue;
3740 }
3741 PUT_CODE (scratch_list[i], SCRATCH);
3742 scratch_list[i] = 0;
3743 something_changed = 1;
3744 continue;
3745 }
3746 }
32131a9c
RK
3747
3748 return something_changed;
3749}
3750\f
56f58d3a
RK
3751/* Find all paradoxical subregs within X and update reg_max_ref_width.
3752 Also mark any hard registers used to store user variables as
3753 forbidden from being used for spill registers. */
32131a9c
RK
3754
3755static void
3756scan_paradoxical_subregs (x)
3757 register rtx x;
3758{
3759 register int i;
3760 register char *fmt;
3761 register enum rtx_code code = GET_CODE (x);
3762
3763 switch (code)
3764 {
56f58d3a 3765 case REG:
e9a25f70 3766 if (SMALL_REGISTER_CLASSES && REGNO (x) < FIRST_PSEUDO_REGISTER
f95182a4 3767 && REG_USERVAR_P (x))
56f58d3a 3768 SET_HARD_REG_BIT (forbidden_regs, REGNO (x));
56f58d3a
RK
3769 return;
3770
32131a9c
RK
3771 case CONST_INT:
3772 case CONST:
3773 case SYMBOL_REF:
3774 case LABEL_REF:
3775 case CONST_DOUBLE:
3776 case CC0:
3777 case PC:
32131a9c
RK
3778 case USE:
3779 case CLOBBER:
3780 return;
3781
3782 case SUBREG:
3783 if (GET_CODE (SUBREG_REG (x)) == REG
3784 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3785 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3786 = GET_MODE_SIZE (GET_MODE (x));
3787 return;
e9a25f70
JL
3788
3789 default:
3790 break;
32131a9c
RK
3791 }
3792
3793 fmt = GET_RTX_FORMAT (code);
3794 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3795 {
3796 if (fmt[i] == 'e')
3797 scan_paradoxical_subregs (XEXP (x, i));
3798 else if (fmt[i] == 'E')
3799 {
3800 register int j;
3801 for (j = XVECLEN (x, i) - 1; j >=0; j--)
3802 scan_paradoxical_subregs (XVECEXP (x, i, j));
3803 }
3804 }
3805}
3806\f
32131a9c 3807static int
788a0818
RK
3808hard_reg_use_compare (p1p, p2p)
3809 const GENERIC_PTR p1p;
3810 const GENERIC_PTR p2p;
32131a9c 3811{
788a0818
RK
3812 struct hard_reg_n_uses *p1 = (struct hard_reg_n_uses *)p1p,
3813 *p2 = (struct hard_reg_n_uses *)p2p;
32131a9c
RK
3814 int tem = p1->uses - p2->uses;
3815 if (tem != 0) return tem;
3816 /* If regs are equally good, sort by regno,
3817 so that the results of qsort leave nothing to chance. */
3818 return p1->regno - p2->regno;
3819}
3820
3821/* Choose the order to consider regs for use as reload registers
3822 based on how much trouble would be caused by spilling one.
3823 Store them in order of decreasing preference in potential_reload_regs. */
3824
3825static void
2c5d9e37
RK
3826order_regs_for_reload (global)
3827 int global;
32131a9c
RK
3828{
3829 register int i;
3830 register int o = 0;
3831 int large = 0;
3832
3833 struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
3834
3835 CLEAR_HARD_REG_SET (bad_spill_regs);
3836
3837 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3838 potential_reload_regs[i] = -1;
3839
3840 /* Count number of uses of each hard reg by pseudo regs allocated to it
3841 and then order them by decreasing use. */
3842
3843 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3844 {
3845 hard_reg_n_uses[i].uses = 0;
3846 hard_reg_n_uses[i].regno = i;
3847 }
3848
3849 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3850 {
3851 int regno = reg_renumber[i];
3852 if (regno >= 0)
3853 {
3854 int lim = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
3855 while (regno < lim)
2c5d9e37
RK
3856 {
3857 /* If allocated by local-alloc, show more uses since
3858 we're not going to be able to reallocate it, but
3859 we might if allocated by global alloc. */
3860 if (global && reg_allocno[i] < 0)
b1f21e0a 3861 hard_reg_n_uses[regno].uses += (REG_N_REFS (i) + 1) / 2;
2c5d9e37 3862
b1f21e0a 3863 hard_reg_n_uses[regno++].uses += REG_N_REFS (i);
2c5d9e37 3864 }
32131a9c 3865 }
b1f21e0a 3866 large += REG_N_REFS (i);
32131a9c
RK
3867 }
3868
3869 /* Now fixed registers (which cannot safely be used for reloading)
3870 get a very high use count so they will be considered least desirable.
3871 Registers used explicitly in the rtl code are almost as bad. */
3872
3873 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3874 {
3875 if (fixed_regs[i])
3876 {
3877 hard_reg_n_uses[i].uses += 2 * large + 2;
3878 SET_HARD_REG_BIT (bad_spill_regs, i);
3879 }
3880 else if (regs_explicitly_used[i])
3881 {
3882 hard_reg_n_uses[i].uses += large + 1;
f95182a4 3883 if (! SMALL_REGISTER_CLASSES)
e9a25f70
JL
3884 /* ??? We are doing this here because of the potential
3885 that bad code may be generated if a register explicitly
3886 used in an insn was used as a spill register for that
3887 insn. But not using these are spill registers may lose
3888 on some machine. We'll have to see how this works out. */
f95182a4 3889 SET_HARD_REG_BIT (bad_spill_regs, i);
32131a9c
RK
3890 }
3891 }
3ec2ea3e
DE
3892 hard_reg_n_uses[HARD_FRAME_POINTER_REGNUM].uses += 2 * large + 2;
3893 SET_HARD_REG_BIT (bad_spill_regs, HARD_FRAME_POINTER_REGNUM);
32131a9c
RK
3894
3895#ifdef ELIMINABLE_REGS
3896 /* If registers other than the frame pointer are eliminable, mark them as
3897 poor choices. */
3898 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3899 {
3900 hard_reg_n_uses[reg_eliminate[i].from].uses += 2 * large + 2;
3901 SET_HARD_REG_BIT (bad_spill_regs, reg_eliminate[i].from);
3902 }
3903#endif
3904
3905 /* Prefer registers not so far used, for use in temporary loading.
3906 Among them, if REG_ALLOC_ORDER is defined, use that order.
3907 Otherwise, prefer registers not preserved by calls. */
3908
3909#ifdef REG_ALLOC_ORDER
3910 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3911 {
3912 int regno = reg_alloc_order[i];
3913
3914 if (hard_reg_n_uses[regno].uses == 0)
3915 potential_reload_regs[o++] = regno;
3916 }
3917#else
3918 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3919 {
3920 if (hard_reg_n_uses[i].uses == 0 && call_used_regs[i])
3921 potential_reload_regs[o++] = i;
3922 }
3923 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3924 {
3925 if (hard_reg_n_uses[i].uses == 0 && ! call_used_regs[i])
3926 potential_reload_regs[o++] = i;
3927 }
3928#endif
3929
3930 qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
3931 sizeof hard_reg_n_uses[0], hard_reg_use_compare);
3932
3933 /* Now add the regs that are already used,
3934 preferring those used less often. The fixed and otherwise forbidden
3935 registers will be at the end of this list. */
3936
3937 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3938 if (hard_reg_n_uses[i].uses != 0)
3939 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
3940}
3941\f
a5339699 3942/* Used in reload_as_needed to sort the spilled regs. */
2f23a46d 3943
a5339699 3944static int
788a0818
RK
3945compare_spill_regs (r1p, r2p)
3946 const GENERIC_PTR r1p;
3947 const GENERIC_PTR r2p;
a5339699 3948{
788a0818
RK
3949 short r1 = *(short *)r1p, r2 = *(short *)r2p;
3950 return r1 - r2;
a5339699
RK
3951}
3952
32131a9c
RK
3953/* Reload pseudo-registers into hard regs around each insn as needed.
3954 Additional register load insns are output before the insn that needs it
3955 and perhaps store insns after insns that modify the reloaded pseudo reg.
3956
3957 reg_last_reload_reg and reg_reloaded_contents keep track of
d08ea79f 3958 which registers are already available in reload registers.
32131a9c
RK
3959 We update these for the reloads that we perform,
3960 as the insns are scanned. */
3961
3962static void
3963reload_as_needed (first, live_known)
3964 rtx first;
3965 int live_known;
3966{
3967 register rtx insn;
3968 register int i;
3969 int this_block = 0;
3970 rtx x;
3971 rtx after_call = 0;
3972
4c9a05bc
RK
3973 bzero ((char *) spill_reg_rtx, sizeof spill_reg_rtx);
3974 bzero ((char *) spill_reg_store, sizeof spill_reg_store);
32131a9c 3975 reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
4c9a05bc 3976 bzero ((char *) reg_last_reload_reg, max_regno * sizeof (rtx));
32131a9c 3977 reg_has_output_reload = (char *) alloca (max_regno);
e6e52be0 3978 CLEAR_HARD_REG_SET (reg_reloaded_valid);
32131a9c
RK
3979
3980 /* Reset all offsets on eliminable registers to their initial values. */
3981#ifdef ELIMINABLE_REGS
3982 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3983 {
3984 INITIAL_ELIMINATION_OFFSET (reg_eliminate[i].from, reg_eliminate[i].to,
510dd77e 3985 reg_eliminate[i].initial_offset);
32131a9c
RK
3986 reg_eliminate[i].previous_offset
3987 = reg_eliminate[i].offset = reg_eliminate[i].initial_offset;
3988 }
3989#else
3990 INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
3991 reg_eliminate[0].previous_offset
3992 = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
3993#endif
3994
3995 num_not_at_initial_offset = 0;
3996
a5339699
RK
3997 /* Order the spilled regs, so that allocate_reload_regs can guarantee to
3998 pack registers with group needs. */
3999 if (n_spills > 1)
5f40cc2d
RK
4000 {
4001 qsort (spill_regs, n_spills, sizeof (short), compare_spill_regs);
4002 for (i = 0; i < n_spills; i++)
4003 spill_reg_order[spill_regs[i]] = i;
4004 }
a5339699 4005
32131a9c
RK
4006 for (insn = first; insn;)
4007 {
4008 register rtx next = NEXT_INSN (insn);
4009
4010 /* Notice when we move to a new basic block. */
aa2c50d6 4011 if (live_known && this_block + 1 < n_basic_blocks
32131a9c
RK
4012 && insn == basic_block_head[this_block+1])
4013 ++this_block;
4014
4015 /* If we pass a label, copy the offsets from the label information
4016 into the current offsets of each elimination. */
4017 if (GET_CODE (insn) == CODE_LABEL)
2a4b5f3b
RK
4018 {
4019 num_not_at_initial_offset = 0;
4020 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
4021 {
4022 reg_eliminate[i].offset = reg_eliminate[i].previous_offset
4023 = offsets_at[CODE_LABEL_NUMBER (insn)][i];
1d0d98f3
RK
4024 if (reg_eliminate[i].can_eliminate
4025 && (reg_eliminate[i].offset
4026 != reg_eliminate[i].initial_offset))
2a4b5f3b
RK
4027 num_not_at_initial_offset++;
4028 }
4029 }
32131a9c
RK
4030
4031 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4032 {
4033 rtx avoid_return_reg = 0;
0639444f 4034 rtx oldpat = PATTERN (insn);
32131a9c 4035
32131a9c
RK
4036 /* Set avoid_return_reg if this is an insn
4037 that might use the value of a function call. */
f95182a4 4038 if (SMALL_REGISTER_CLASSES && GET_CODE (insn) == CALL_INSN)
32131a9c
RK
4039 {
4040 if (GET_CODE (PATTERN (insn)) == SET)
4041 after_call = SET_DEST (PATTERN (insn));
4042 else if (GET_CODE (PATTERN (insn)) == PARALLEL
4043 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
4044 after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
4045 else
4046 after_call = 0;
4047 }
e9a25f70 4048 else if (SMALL_REGISTER_CLASSES && after_call != 0
32131a9c 4049 && !(GET_CODE (PATTERN (insn)) == SET
b60a8416
R
4050 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx)
4051 && GET_CODE (PATTERN (insn)) != USE)
32131a9c 4052 {
2b979c57 4053 if (reg_referenced_p (after_call, PATTERN (insn)))
32131a9c
RK
4054 avoid_return_reg = after_call;
4055 after_call = 0;
4056 }
32131a9c 4057
2758481d
RS
4058 /* If this is a USE and CLOBBER of a MEM, ensure that any
4059 references to eliminable registers have been removed. */
4060
4061 if ((GET_CODE (PATTERN (insn)) == USE
4062 || GET_CODE (PATTERN (insn)) == CLOBBER)
4063 && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
4064 XEXP (XEXP (PATTERN (insn), 0), 0)
4065 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
29ae5012 4066 GET_MODE (XEXP (PATTERN (insn), 0)),
1914f5da 4067 NULL_RTX);
2758481d 4068
32131a9c
RK
4069 /* If we need to do register elimination processing, do so.
4070 This might delete the insn, in which case we are done. */
4071 if (num_eliminable && GET_MODE (insn) == QImode)
4072 {
4073 eliminate_regs_in_insn (insn, 1);
4074 if (GET_CODE (insn) == NOTE)
4075 {
4076 insn = next;
4077 continue;
4078 }
4079 }
4080
4081 if (GET_MODE (insn) == VOIDmode)
4082 n_reloads = 0;
4083 /* First find the pseudo regs that must be reloaded for this insn.
4084 This info is returned in the tables reload_... (see reload.h).
4085 Also modify the body of INSN by substituting RELOAD
4086 rtx's for those pseudo regs. */
4087 else
4088 {
4089 bzero (reg_has_output_reload, max_regno);
4090 CLEAR_HARD_REG_SET (reg_is_output_reload);
4091
4092 find_reloads (insn, 1, spill_indirect_levels, live_known,
4093 spill_reg_order);
4094 }
4095
4096 if (n_reloads > 0)
4097 {
3c3eeea6
RK
4098 rtx prev = PREV_INSN (insn), next = NEXT_INSN (insn);
4099 rtx p;
32131a9c
RK
4100 int class;
4101
4102 /* If this block has not had spilling done for a
546b63fb
RK
4103 particular clas and we have any non-optionals that need a
4104 spill reg in that class, abort. */
32131a9c
RK
4105
4106 for (class = 0; class < N_REG_CLASSES; class++)
4107 if (basic_block_needs[class] != 0
4108 && basic_block_needs[class][this_block] == 0)
4109 for (i = 0; i < n_reloads; i++)
546b63fb
RK
4110 if (class == (int) reload_reg_class[i]
4111 && reload_reg_rtx[i] == 0
4112 && ! reload_optional[i]
4113 && (reload_in[i] != 0 || reload_out[i] != 0
4114 || reload_secondary_p[i] != 0))
a89b2cc4 4115 fatal_insn ("Non-optional registers need a spill register", insn);
32131a9c
RK
4116
4117 /* Now compute which reload regs to reload them into. Perhaps
4118 reusing reload regs from previous insns, or else output
4119 load insns to reload them. Maybe output store insns too.
4120 Record the choices of reload reg in reload_reg_rtx. */
4121 choose_reload_regs (insn, avoid_return_reg);
4122
546b63fb
RK
4123 /* Merge any reloads that we didn't combine for fear of
4124 increasing the number of spill registers needed but now
4125 discover can be safely merged. */
f95182a4
ILT
4126 if (SMALL_REGISTER_CLASSES)
4127 merge_assigned_reloads (insn);
546b63fb 4128
32131a9c
RK
4129 /* Generate the insns to reload operands into or out of
4130 their reload regs. */
4131 emit_reload_insns (insn);
4132
4133 /* Substitute the chosen reload regs from reload_reg_rtx
4134 into the insn's body (or perhaps into the bodies of other
4135 load and store insn that we just made for reloading
4136 and that we moved the structure into). */
4137 subst_reloads ();
3c3eeea6
RK
4138
4139 /* If this was an ASM, make sure that all the reload insns
4140 we have generated are valid. If not, give an error
4141 and delete them. */
4142
4143 if (asm_noperands (PATTERN (insn)) >= 0)
4144 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4145 if (p != insn && GET_RTX_CLASS (GET_CODE (p)) == 'i'
4146 && (recog_memoized (p) < 0
4147 || (insn_extract (p),
4148 ! constrain_operands (INSN_CODE (p), 1))))
4149 {
4150 error_for_asm (insn,
4151 "`asm' operand requires impossible reload");
4152 PUT_CODE (p, NOTE);
4153 NOTE_SOURCE_FILE (p) = 0;
4154 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
4155 }
32131a9c
RK
4156 }
4157 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4158 is no longer validly lying around to save a future reload.
4159 Note that this does not detect pseudos that were reloaded
4160 for this insn in order to be stored in
4161 (obeying register constraints). That is correct; such reload
4162 registers ARE still valid. */
0639444f 4163 note_stores (oldpat, forget_old_reloads_1);
32131a9c
RK
4164
4165 /* There may have been CLOBBER insns placed after INSN. So scan
4166 between INSN and NEXT and use them to forget old reloads. */
4167 for (x = NEXT_INSN (insn); x != next; x = NEXT_INSN (x))
4168 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
4169 note_stores (PATTERN (x), forget_old_reloads_1);
4170
4171#ifdef AUTO_INC_DEC
4172 /* Likewise for regs altered by auto-increment in this insn.
4173 But note that the reg-notes are not changed by reloading:
4174 they still contain the pseudo-regs, not the spill regs. */
4175 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4176 if (REG_NOTE_KIND (x) == REG_INC)
4177 {
4178 /* See if this pseudo reg was reloaded in this insn.
4179 If so, its last-reload info is still valid
4180 because it is based on this insn's reload. */
4181 for (i = 0; i < n_reloads; i++)
4182 if (reload_out[i] == XEXP (x, 0))
4183 break;
4184
08fb99fa 4185 if (i == n_reloads)
9a881562 4186 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX);
32131a9c
RK
4187 }
4188#endif
4189 }
4190 /* A reload reg's contents are unknown after a label. */
4191 if (GET_CODE (insn) == CODE_LABEL)
e6e52be0 4192 CLEAR_HARD_REG_SET (reg_reloaded_valid);
32131a9c
RK
4193
4194 /* Don't assume a reload reg is still good after a call insn
4195 if it is a call-used reg. */
546b63fb 4196 else if (GET_CODE (insn) == CALL_INSN)
e6e52be0 4197 AND_COMPL_HARD_REG_SET(reg_reloaded_valid, call_used_reg_set);
32131a9c
RK
4198
4199 /* In case registers overlap, allow certain insns to invalidate
4200 particular hard registers. */
4201
4202#ifdef INSN_CLOBBERS_REGNO_P
e6e52be0
R
4203 for (i = 0 ; i < FIRST_PSEUDO_REGISTER; i++)
4204 if (TEST_HARD_REG_BIT (reg_reloaded_valid, i)
4205 && INSN_CLOBBERS_REGNO_P (insn, i))
4206 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i);
32131a9c
RK
4207#endif
4208
4209 insn = next;
4210
4211#ifdef USE_C_ALLOCA
4212 alloca (0);
4213#endif
4214 }
4215}
4216
4217/* Discard all record of any value reloaded from X,
4218 or reloaded in X from someplace else;
4219 unless X is an output reload reg of the current insn.
4220
4221 X may be a hard reg (the reload reg)
4222 or it may be a pseudo reg that was reloaded from. */
4223
4224static void
9a881562 4225forget_old_reloads_1 (x, ignored)
32131a9c 4226 rtx x;
487a6e06 4227 rtx ignored ATTRIBUTE_UNUSED;
32131a9c
RK
4228{
4229 register int regno;
4230 int nr;
0a2e51a9
RS
4231 int offset = 0;
4232
4233 /* note_stores does give us subregs of hard regs. */
4234 while (GET_CODE (x) == SUBREG)
4235 {
4236 offset += SUBREG_WORD (x);
4237 x = SUBREG_REG (x);
4238 }
32131a9c
RK
4239
4240 if (GET_CODE (x) != REG)
4241 return;
4242
0a2e51a9 4243 regno = REGNO (x) + offset;
32131a9c
RK
4244
4245 if (regno >= FIRST_PSEUDO_REGISTER)
4246 nr = 1;
4247 else
4248 {
4249 int i;
4250 nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
4251 /* Storing into a spilled-reg invalidates its contents.
4252 This can happen if a block-local pseudo is allocated to that reg
4253 and it wasn't spilled because this block's total need is 0.
4254 Then some insn might have an optional reload and use this reg. */
4255 for (i = 0; i < nr; i++)
e6e52be0
R
4256 /* But don't do this if the reg actually serves as an output
4257 reload reg in the current instruction. */
4258 if (n_reloads == 0
4259 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4260 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
32131a9c
RK
4261 }
4262
4263 /* Since value of X has changed,
4264 forget any value previously copied from it. */
4265
4266 while (nr-- > 0)
4267 /* But don't forget a copy if this is the output reload
4268 that establishes the copy's validity. */
4269 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4270 reg_last_reload_reg[regno + nr] = 0;
4271}
4272\f
4273/* For each reload, the mode of the reload register. */
4274static enum machine_mode reload_mode[MAX_RELOADS];
4275
4276/* For each reload, the largest number of registers it will require. */
4277static int reload_nregs[MAX_RELOADS];
4278
4279/* Comparison function for qsort to decide which of two reloads
4280 should be handled first. *P1 and *P2 are the reload numbers. */
4281
4282static int
788a0818
RK
4283reload_reg_class_lower (r1p, r2p)
4284 const GENERIC_PTR r1p;
4285 const GENERIC_PTR r2p;
32131a9c 4286{
788a0818 4287 register int r1 = *(short *)r1p, r2 = *(short *)r2p;
32131a9c 4288 register int t;
a8fdc208 4289
32131a9c
RK
4290 /* Consider required reloads before optional ones. */
4291 t = reload_optional[r1] - reload_optional[r2];
4292 if (t != 0)
4293 return t;
4294
4295 /* Count all solitary classes before non-solitary ones. */
4296 t = ((reg_class_size[(int) reload_reg_class[r2]] == 1)
4297 - (reg_class_size[(int) reload_reg_class[r1]] == 1));
4298 if (t != 0)
4299 return t;
4300
4301 /* Aside from solitaires, consider all multi-reg groups first. */
4302 t = reload_nregs[r2] - reload_nregs[r1];
4303 if (t != 0)
4304 return t;
4305
4306 /* Consider reloads in order of increasing reg-class number. */
4307 t = (int) reload_reg_class[r1] - (int) reload_reg_class[r2];
4308 if (t != 0)
4309 return t;
4310
4311 /* If reloads are equally urgent, sort by reload number,
4312 so that the results of qsort leave nothing to chance. */
4313 return r1 - r2;
4314}
4315\f
4316/* The following HARD_REG_SETs indicate when each hard register is
4317 used for a reload of various parts of the current insn. */
4318
4319/* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4320static HARD_REG_SET reload_reg_used;
546b63fb
RK
4321/* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4322static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
47c8cf91
ILT
4323/* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4324static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
546b63fb
RK
4325/* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4326static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
47c8cf91
ILT
4327/* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4328static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
546b63fb
RK
4329/* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4330static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4331/* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4332static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
32131a9c
RK
4333/* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4334static HARD_REG_SET reload_reg_used_in_op_addr;
893bc853
RK
4335/* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4336static HARD_REG_SET reload_reg_used_in_op_addr_reload;
546b63fb
RK
4337/* If reg is in use for a RELOAD_FOR_INSN reload. */
4338static HARD_REG_SET reload_reg_used_in_insn;
4339/* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4340static HARD_REG_SET reload_reg_used_in_other_addr;
32131a9c
RK
4341
4342/* If reg is in use as a reload reg for any sort of reload. */
4343static HARD_REG_SET reload_reg_used_at_all;
4344
be7ae2a4
RK
4345/* If reg is use as an inherited reload. We just mark the first register
4346 in the group. */
4347static HARD_REG_SET reload_reg_used_for_inherit;
4348
546b63fb
RK
4349/* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4350 TYPE. MODE is used to indicate how many consecutive regs are
4351 actually used. */
32131a9c
RK
4352
4353static void
546b63fb 4354mark_reload_reg_in_use (regno, opnum, type, mode)
32131a9c 4355 int regno;
546b63fb
RK
4356 int opnum;
4357 enum reload_type type;
32131a9c
RK
4358 enum machine_mode mode;
4359{
4360 int nregs = HARD_REGNO_NREGS (regno, mode);
4361 int i;
4362
4363 for (i = regno; i < nregs + regno; i++)
4364 {
546b63fb 4365 switch (type)
32131a9c
RK
4366 {
4367 case RELOAD_OTHER:
4368 SET_HARD_REG_BIT (reload_reg_used, i);
4369 break;
4370
546b63fb
RK
4371 case RELOAD_FOR_INPUT_ADDRESS:
4372 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
32131a9c
RK
4373 break;
4374
47c8cf91
ILT
4375 case RELOAD_FOR_INPADDR_ADDRESS:
4376 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4377 break;
4378
546b63fb
RK
4379 case RELOAD_FOR_OUTPUT_ADDRESS:
4380 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
32131a9c
RK
4381 break;
4382
47c8cf91
ILT
4383 case RELOAD_FOR_OUTADDR_ADDRESS:
4384 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4385 break;
4386
32131a9c
RK
4387 case RELOAD_FOR_OPERAND_ADDRESS:
4388 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4389 break;
4390
893bc853
RK
4391 case RELOAD_FOR_OPADDR_ADDR:
4392 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4393 break;
4394
546b63fb
RK
4395 case RELOAD_FOR_OTHER_ADDRESS:
4396 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4397 break;
4398
32131a9c 4399 case RELOAD_FOR_INPUT:
546b63fb 4400 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
32131a9c
RK
4401 break;
4402
4403 case RELOAD_FOR_OUTPUT:
546b63fb
RK
4404 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4405 break;
4406
4407 case RELOAD_FOR_INSN:
4408 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
32131a9c
RK
4409 break;
4410 }
4411
4412 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4413 }
4414}
4415
be7ae2a4
RK
4416/* Similarly, but show REGNO is no longer in use for a reload. */
4417
4418static void
4419clear_reload_reg_in_use (regno, opnum, type, mode)
4420 int regno;
4421 int opnum;
4422 enum reload_type type;
4423 enum machine_mode mode;
4424{
4425 int nregs = HARD_REGNO_NREGS (regno, mode);
4426 int i;
4427
4428 for (i = regno; i < nregs + regno; i++)
4429 {
4430 switch (type)
4431 {
4432 case RELOAD_OTHER:
4433 CLEAR_HARD_REG_BIT (reload_reg_used, i);
4434 break;
4435
4436 case RELOAD_FOR_INPUT_ADDRESS:
4437 CLEAR_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4438 break;
4439
47c8cf91
ILT
4440 case RELOAD_FOR_INPADDR_ADDRESS:
4441 CLEAR_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4442 break;
4443
be7ae2a4
RK
4444 case RELOAD_FOR_OUTPUT_ADDRESS:
4445 CLEAR_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4446 break;
4447
47c8cf91
ILT
4448 case RELOAD_FOR_OUTADDR_ADDRESS:
4449 CLEAR_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4450 break;
4451
be7ae2a4
RK
4452 case RELOAD_FOR_OPERAND_ADDRESS:
4453 CLEAR_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4454 break;
4455
893bc853
RK
4456 case RELOAD_FOR_OPADDR_ADDR:
4457 CLEAR_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4458 break;
4459
be7ae2a4
RK
4460 case RELOAD_FOR_OTHER_ADDRESS:
4461 CLEAR_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4462 break;
4463
4464 case RELOAD_FOR_INPUT:
4465 CLEAR_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4466 break;
4467
4468 case RELOAD_FOR_OUTPUT:
4469 CLEAR_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4470 break;
4471
4472 case RELOAD_FOR_INSN:
4473 CLEAR_HARD_REG_BIT (reload_reg_used_in_insn, i);
4474 break;
4475 }
4476 }
4477}
4478
32131a9c 4479/* 1 if reg REGNO is free as a reload reg for a reload of the sort
546b63fb 4480 specified by OPNUM and TYPE. */
32131a9c
RK
4481
4482static int
546b63fb 4483reload_reg_free_p (regno, opnum, type)
32131a9c 4484 int regno;
546b63fb
RK
4485 int opnum;
4486 enum reload_type type;
32131a9c 4487{
546b63fb
RK
4488 int i;
4489
2edc8d65
RK
4490 /* In use for a RELOAD_OTHER means it's not available for anything. */
4491 if (TEST_HARD_REG_BIT (reload_reg_used, regno))
32131a9c 4492 return 0;
546b63fb
RK
4493
4494 switch (type)
32131a9c
RK
4495 {
4496 case RELOAD_OTHER:
2edc8d65
RK
4497 /* In use for anything means we can't use it for RELOAD_OTHER. */
4498 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
224f1d71
RK
4499 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4500 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4501 return 0;
4502
4503 for (i = 0; i < reload_n_operands; i++)
4504 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
47c8cf91 4505 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
224f1d71 4506 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
47c8cf91 4507 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
224f1d71
RK
4508 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4509 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4510 return 0;
4511
4512 return 1;
32131a9c 4513
32131a9c 4514 case RELOAD_FOR_INPUT:
546b63fb
RK
4515 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4516 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4517 return 0;
4518
893bc853
RK
4519 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4520 return 0;
4521
546b63fb
RK
4522 /* If it is used for some other input, can't use it. */
4523 for (i = 0; i < reload_n_operands; i++)
4524 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4525 return 0;
4526
4527 /* If it is used in a later operand's address, can't use it. */
4528 for (i = opnum + 1; i < reload_n_operands; i++)
47c8cf91
ILT
4529 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4530 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
546b63fb
RK
4531 return 0;
4532
4533 return 1;
4534
4535 case RELOAD_FOR_INPUT_ADDRESS:
4536 /* Can't use a register if it is used for an input address for this
4537 operand or used as an input in an earlier one. */
47c8cf91
ILT
4538 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4539 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4540 return 0;
4541
4542 for (i = 0; i < opnum; i++)
4543 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4544 return 0;
4545
4546 return 1;
4547
4548 case RELOAD_FOR_INPADDR_ADDRESS:
4549 /* Can't use a register if it is used for an input address
38e01259 4550 for this operand or used as an input in an earlier
47c8cf91
ILT
4551 one. */
4552 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
546b63fb
RK
4553 return 0;
4554
4555 for (i = 0; i < opnum; i++)
4556 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4557 return 0;
4558
4559 return 1;
4560
4561 case RELOAD_FOR_OUTPUT_ADDRESS:
4562 /* Can't use a register if it is used for an output address for this
4563 operand or used as an output in this or a later operand. */
4564 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4565 return 0;
4566
4567 for (i = opnum; i < reload_n_operands; i++)
4568 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4569 return 0;
4570
4571 return 1;
4572
47c8cf91
ILT
4573 case RELOAD_FOR_OUTADDR_ADDRESS:
4574 /* Can't use a register if it is used for an output address
38e01259 4575 for this operand or used as an output in this or a
47c8cf91
ILT
4576 later operand. */
4577 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4578 return 0;
4579
4580 for (i = opnum; i < reload_n_operands; i++)
4581 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4582 return 0;
4583
4584 return 1;
4585
32131a9c 4586 case RELOAD_FOR_OPERAND_ADDRESS:
546b63fb
RK
4587 for (i = 0; i < reload_n_operands; i++)
4588 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4589 return 0;
4590
4591 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4592 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4593
893bc853
RK
4594 case RELOAD_FOR_OPADDR_ADDR:
4595 for (i = 0; i < reload_n_operands; i++)
4596 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4597 return 0;
4598
a94ce333 4599 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
893bc853 4600
32131a9c 4601 case RELOAD_FOR_OUTPUT:
546b63fb
RK
4602 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4603 outputs, or an operand address for this or an earlier output. */
4604 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4605 return 0;
4606
4607 for (i = 0; i < reload_n_operands; i++)
4608 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4609 return 0;
4610
4611 for (i = 0; i <= opnum; i++)
47c8cf91
ILT
4612 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4613 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
546b63fb
RK
4614 return 0;
4615
4616 return 1;
4617
4618 case RELOAD_FOR_INSN:
4619 for (i = 0; i < reload_n_operands; i++)
4620 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4621 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4622 return 0;
4623
4624 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4625 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4626
4627 case RELOAD_FOR_OTHER_ADDRESS:
4628 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
32131a9c
RK
4629 }
4630 abort ();
4631}
4632
4633/* Return 1 if the value in reload reg REGNO, as used by a reload
546b63fb 4634 needed for the part of the insn specified by OPNUM and TYPE,
32131a9c
RK
4635 is not in use for a reload in any prior part of the insn.
4636
4637 We can assume that the reload reg was already tested for availability
4638 at the time it is needed, and we should not check this again,
4639 in case the reg has already been marked in use. */
4640
4641static int
546b63fb 4642reload_reg_free_before_p (regno, opnum, type)
32131a9c 4643 int regno;
546b63fb
RK
4644 int opnum;
4645 enum reload_type type;
32131a9c 4646{
546b63fb
RK
4647 int i;
4648
4649 switch (type)
32131a9c 4650 {
546b63fb
RK
4651 case RELOAD_FOR_OTHER_ADDRESS:
4652 /* These always come first. */
32131a9c
RK
4653 return 1;
4654
546b63fb
RK
4655 case RELOAD_OTHER:
4656 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4657
32131a9c 4658 /* If this use is for part of the insn,
546b63fb
RK
4659 check the reg is not in use for any prior part. It is tempting
4660 to try to do this by falling through from objecs that occur
4661 later in the insn to ones that occur earlier, but that will not
4662 correctly take into account the fact that here we MUST ignore
4663 things that would prevent the register from being allocated in
4664 the first place, since we know that it was allocated. */
4665
4666 case RELOAD_FOR_OUTPUT_ADDRESS:
75528b80
R
4667 /* Earlier reloads include RELOAD_FOR_INPADDR_ADDRESS reloads. */
4668 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4669 return 0;
4670 /* ... fall through ... */
47c8cf91 4671 case RELOAD_FOR_OUTADDR_ADDRESS:
546b63fb
RK
4672 /* Earlier reloads are for earlier outputs or their addresses,
4673 any RELOAD_FOR_INSN reloads, any inputs or their addresses, or any
4674 RELOAD_FOR_OTHER_ADDRESS reloads (we know it can't conflict with
4675 RELOAD_OTHER).. */
4676 for (i = 0; i < opnum; i++)
4677 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
47c8cf91 4678 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
546b63fb
RK
4679 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4680 return 0;
4681
4682 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
32131a9c 4683 return 0;
546b63fb
RK
4684
4685 for (i = 0; i < reload_n_operands; i++)
4686 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
47c8cf91 4687 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
546b63fb
RK
4688 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4689 return 0;
4690
4691 return (! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4692 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4693 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4694
32131a9c 4695 case RELOAD_FOR_OUTPUT:
546b63fb
RK
4696 /* This can't be used in the output address for this operand and
4697 anything that can't be used for it, except that we've already
4698 tested for RELOAD_FOR_INSN objects. */
4699
47c8cf91
ILT
4700 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno)
4701 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
32131a9c 4702 return 0;
546b63fb
RK
4703
4704 for (i = 0; i < opnum; i++)
4705 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
47c8cf91 4706 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
546b63fb
RK
4707 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4708 return 0;
4709
4710 for (i = 0; i < reload_n_operands; i++)
4711 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
47c8cf91 4712 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
546b63fb
RK
4713 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4714 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4715 return 0;
4716
4717 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4718
32131a9c 4719 case RELOAD_FOR_OPERAND_ADDRESS:
a94ce333
JW
4720 /* Earlier reloads include RELOAD_FOR_OPADDR_ADDR reloads. */
4721 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4722 return 0;
4723
4724 /* ... fall through ... */
4725
893bc853 4726 case RELOAD_FOR_OPADDR_ADDR:
546b63fb
RK
4727 case RELOAD_FOR_INSN:
4728 /* These can't conflict with inputs, or each other, so all we have to
4729 test is input addresses and the addresses of OTHER items. */
4730
4731 for (i = 0; i < reload_n_operands; i++)
47c8cf91
ILT
4732 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4733 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
546b63fb
RK
4734 return 0;
4735
4736 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4737
32131a9c 4738 case RELOAD_FOR_INPUT:
5bc80b30
JL
4739 /* The only things earlier are the address for this and
4740 earlier inputs, other inputs (which we know we don't conflict
4741 with), and addresses of RELOAD_OTHER objects. */
546b63fb 4742
5bc80b30 4743 for (i = 0; i <= opnum; i++)
47c8cf91 4744 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5bc80b30 4745 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
546b63fb
RK
4746 return 0;
4747
4748 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4749
4750 case RELOAD_FOR_INPUT_ADDRESS:
75528b80
R
4751 /* Earlier reloads include RELOAD_FOR_INPADDR_ADDRESS reloads. */
4752 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4753 return 0;
4754 /* ... fall through ... */
47c8cf91 4755 case RELOAD_FOR_INPADDR_ADDRESS:
546b63fb
RK
4756 /* Similarly, all we have to check is for use in earlier inputs'
4757 addresses. */
4758 for (i = 0; i < opnum; i++)
47c8cf91
ILT
4759 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4760 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
546b63fb
RK
4761 return 0;
4762
4763 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
32131a9c
RK
4764 }
4765 abort ();
4766}
4767
4768/* Return 1 if the value in reload reg REGNO, as used by a reload
546b63fb 4769 needed for the part of the insn specified by OPNUM and TYPE,
32131a9c
RK
4770 is still available in REGNO at the end of the insn.
4771
4772 We can assume that the reload reg was already tested for availability
4773 at the time it is needed, and we should not check this again,
4774 in case the reg has already been marked in use. */
4775
4776static int
546b63fb 4777reload_reg_reaches_end_p (regno, opnum, type)
32131a9c 4778 int regno;
546b63fb
RK
4779 int opnum;
4780 enum reload_type type;
32131a9c 4781{
546b63fb
RK
4782 int i;
4783
4784 switch (type)
32131a9c
RK
4785 {
4786 case RELOAD_OTHER:
4787 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4788 its value must reach the end. */
4789 return 1;
4790
4791 /* If this use is for part of the insn,
546b63fb
RK
4792 its value reaches if no subsequent part uses the same register.
4793 Just like the above function, don't try to do this with lots
4794 of fallthroughs. */
4795
4796 case RELOAD_FOR_OTHER_ADDRESS:
4797 /* Here we check for everything else, since these don't conflict
4798 with anything else and everything comes later. */
4799
4800 for (i = 0; i < reload_n_operands; i++)
4801 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
47c8cf91 4802 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
546b63fb
RK
4803 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4804 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
47c8cf91 4805 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
546b63fb
RK
4806 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4807 return 0;
4808
4809 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4810 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4811 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4812
4813 case RELOAD_FOR_INPUT_ADDRESS:
47c8cf91 4814 case RELOAD_FOR_INPADDR_ADDRESS:
546b63fb
RK
4815 /* Similar, except that we check only for this and subsequent inputs
4816 and the address of only subsequent inputs and we do not need
4817 to check for RELOAD_OTHER objects since they are known not to
4818 conflict. */
4819
4820 for (i = opnum; i < reload_n_operands; i++)
4821 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4822 return 0;
4823
4824 for (i = opnum + 1; i < reload_n_operands; i++)
47c8cf91
ILT
4825 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4826 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
546b63fb
RK
4827 return 0;
4828
4829 for (i = 0; i < reload_n_operands; i++)
4830 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
47c8cf91 4831 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
546b63fb
RK
4832 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4833 return 0;
4834
893bc853
RK
4835 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4836 return 0;
4837
546b63fb
RK
4838 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4839 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
4840
32131a9c 4841 case RELOAD_FOR_INPUT:
546b63fb
RK
4842 /* Similar to input address, except we start at the next operand for
4843 both input and input address and we do not check for
4844 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4845 would conflict. */
4846
4847 for (i = opnum + 1; i < reload_n_operands; i++)
4848 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
47c8cf91 4849 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
546b63fb
RK
4850 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4851 return 0;
4852
0f41302f 4853 /* ... fall through ... */
546b63fb 4854
32131a9c 4855 case RELOAD_FOR_OPERAND_ADDRESS:
546b63fb
RK
4856 /* Check outputs and their addresses. */
4857
4858 for (i = 0; i < reload_n_operands; i++)
4859 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
47c8cf91 4860 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
546b63fb
RK
4861 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4862 return 0;
4863
4864 return 1;
4865
893bc853
RK
4866 case RELOAD_FOR_OPADDR_ADDR:
4867 for (i = 0; i < reload_n_operands; i++)
4868 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
47c8cf91 4869 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
893bc853
RK
4870 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4871 return 0;
4872
a94ce333
JW
4873 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4874 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
893bc853 4875
546b63fb 4876 case RELOAD_FOR_INSN:
893bc853 4877 /* These conflict with other outputs with RELOAD_OTHER. So
546b63fb
RK
4878 we need only check for output addresses. */
4879
4880 opnum = -1;
4881
0f41302f 4882 /* ... fall through ... */
546b63fb 4883
32131a9c 4884 case RELOAD_FOR_OUTPUT:
546b63fb 4885 case RELOAD_FOR_OUTPUT_ADDRESS:
47c8cf91 4886 case RELOAD_FOR_OUTADDR_ADDRESS:
546b63fb
RK
4887 /* We already know these can't conflict with a later output. So the
4888 only thing to check are later output addresses. */
4889 for (i = opnum + 1; i < reload_n_operands; i++)
47c8cf91
ILT
4890 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4891 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
546b63fb
RK
4892 return 0;
4893
32131a9c
RK
4894 return 1;
4895 }
546b63fb 4896
32131a9c
RK
4897 abort ();
4898}
4899\f
351aa1c1
RK
4900/* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
4901 Return 0 otherwise.
4902
4903 This function uses the same algorithm as reload_reg_free_p above. */
4904
f5963e61 4905int
351aa1c1
RK
4906reloads_conflict (r1, r2)
4907 int r1, r2;
4908{
4909 enum reload_type r1_type = reload_when_needed[r1];
4910 enum reload_type r2_type = reload_when_needed[r2];
4911 int r1_opnum = reload_opnum[r1];
4912 int r2_opnum = reload_opnum[r2];
4913
2edc8d65
RK
4914 /* RELOAD_OTHER conflicts with everything. */
4915 if (r2_type == RELOAD_OTHER)
351aa1c1
RK
4916 return 1;
4917
4918 /* Otherwise, check conflicts differently for each type. */
4919
4920 switch (r1_type)
4921 {
4922 case RELOAD_FOR_INPUT:
4923 return (r2_type == RELOAD_FOR_INSN
4924 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
893bc853 4925 || r2_type == RELOAD_FOR_OPADDR_ADDR
351aa1c1 4926 || r2_type == RELOAD_FOR_INPUT
47c8cf91
ILT
4927 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
4928 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
4929 && r2_opnum > r1_opnum));
351aa1c1
RK
4930
4931 case RELOAD_FOR_INPUT_ADDRESS:
4932 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
4933 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4934
47c8cf91
ILT
4935 case RELOAD_FOR_INPADDR_ADDRESS:
4936 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
4937 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4938
351aa1c1
RK
4939 case RELOAD_FOR_OUTPUT_ADDRESS:
4940 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
4941 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
4942
47c8cf91
ILT
4943 case RELOAD_FOR_OUTADDR_ADDRESS:
4944 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
4945 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
4946
351aa1c1
RK
4947 case RELOAD_FOR_OPERAND_ADDRESS:
4948 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
a94ce333 4949 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
351aa1c1 4950
893bc853
RK
4951 case RELOAD_FOR_OPADDR_ADDR:
4952 return (r2_type == RELOAD_FOR_INPUT
a94ce333 4953 || r2_type == RELOAD_FOR_OPADDR_ADDR);
893bc853 4954
351aa1c1
RK
4955 case RELOAD_FOR_OUTPUT:
4956 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
47c8cf91
ILT
4957 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
4958 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
351aa1c1
RK
4959 && r2_opnum >= r1_opnum));
4960
4961 case RELOAD_FOR_INSN:
4962 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
4963 || r2_type == RELOAD_FOR_INSN
4964 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4965
4966 case RELOAD_FOR_OTHER_ADDRESS:
4967 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
4968
adab4fc5 4969 case RELOAD_OTHER:
2edc8d65 4970 return 1;
adab4fc5 4971
351aa1c1
RK
4972 default:
4973 abort ();
4974 }
4975}
4976\f
32131a9c
RK
4977/* Vector of reload-numbers showing the order in which the reloads should
4978 be processed. */
4979short reload_order[MAX_RELOADS];
4980
4981/* Indexed by reload number, 1 if incoming value
4982 inherited from previous insns. */
4983char reload_inherited[MAX_RELOADS];
4984
4985/* For an inherited reload, this is the insn the reload was inherited from,
4986 if we know it. Otherwise, this is 0. */
4987rtx reload_inheritance_insn[MAX_RELOADS];
4988
4989/* If non-zero, this is a place to get the value of the reload,
4990 rather than using reload_in. */
4991rtx reload_override_in[MAX_RELOADS];
4992
e6e52be0
R
4993/* For each reload, the hard register number of the register used,
4994 or -1 if we did not need a register for this reload. */
32131a9c
RK
4995int reload_spill_index[MAX_RELOADS];
4996
6e684430
R
4997/* Return 1 if the value in reload reg REGNO, as used by a reload
4998 needed for the part of the insn specified by OPNUM and TYPE,
4999 may be used to load VALUE into it.
f5470689
R
5000
5001 Other read-only reloads with the same value do not conflict
5002 unless OUT is non-zero and these other reloads have to live while
5003 output reloads live.
5004
5005 RELOADNUM is the number of the reload we want to load this value for;
5006 a reload does not conflict with itself.
5007
6e684430
R
5008 The caller has to make sure that there is no conflict with the return
5009 register. */
5010static int
f5470689 5011reload_reg_free_for_value_p (regno, opnum, type, value, out, reloadnum)
6e684430
R
5012 int regno;
5013 int opnum;
5014 enum reload_type type;
f5470689
R
5015 rtx value, out;
5016 int reloadnum;
6e684430
R
5017{
5018 int time1;
5019 int i;
5020
5021 /* We use some pseudo 'time' value to check if the lifetimes of the
5022 new register use would overlap with the one of a previous reload
5023 that is not read-only or uses a different value.
5024 The 'time' used doesn't have to be linear in any shape or form, just
5025 monotonic.
5026 Some reload types use different 'buckets' for each operand.
5027 So there are MAX_RECOG_OPERANDS different time values for each
cecbf6e2
R
5028 such reload type.
5029 We compute TIME1 as the time when the register for the prospective
5030 new reload ceases to be live, and TIME2 for each existing
5031 reload as the time when that the reload register of that reload
5032 becomes live.
5033 Where there is little to be gained by exact lifetime calculations,
5034 we just make conservative assumptions, i.e. a longer lifetime;
5035 this is done in the 'default:' cases. */
6e684430
R
5036 switch (type)
5037 {
5038 case RELOAD_FOR_OTHER_ADDRESS:
5039 time1 = 0;
5040 break;
5041 /* For each input, we might have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
5042 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
5043 respectively, to the time values for these, we get distinct time
5044 values. To get distinct time values for each operand, we have to
5045 multiply opnum by at least three. We round that up to four because
5046 multiply by four is often cheaper. */
5047 case RELOAD_FOR_INPADDR_ADDRESS:
5048 time1 = opnum * 4 + 1;
5049 break;
5050 case RELOAD_FOR_INPUT_ADDRESS:
5051 time1 = opnum * 4 + 2;
5052 break;
5053 case RELOAD_FOR_INPUT:
cecbf6e2
R
5054 /* All RELOAD_FOR_INPUT reloads remain live till just before the
5055 instruction is executed. */
5056 time1 = (MAX_RECOG_OPERANDS - 1) * 4 + 3;
6e684430
R
5057 break;
5058 /* opnum * 4 + 3 < opnum * 4 + 4
cecbf6e2 5059 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
6e684430
R
5060 case RELOAD_FOR_OUTPUT_ADDRESS:
5061 time1 = MAX_RECOG_OPERANDS * 4 + opnum;
5062 break;
5063 default:
5064 time1 = MAX_RECOG_OPERANDS * 5;
5065 }
5066
5067 for (i = 0; i < n_reloads; i++)
5068 {
5069 rtx reg = reload_reg_rtx[i];
5070 if (reg && GET_CODE (reg) == REG
5071 && ((unsigned) regno - true_regnum (reg)
83e0821b 5072 <= HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)) - (unsigned)1)
f5470689 5073 && i != reloadnum)
6e684430 5074 {
f5470689
R
5075 if (out
5076 && reload_when_needed[i] != RELOAD_FOR_INPUT
5077 && reload_when_needed[i] != RELOAD_FOR_INPUT_ADDRESS
5078 && reload_when_needed[i] != RELOAD_FOR_INPADDR_ADDRESS)
5079 return 0;
5080 if (! reload_in[i] || ! rtx_equal_p (reload_in[i], value)
5081 || reload_out[i])
6e684430 5082 {
f5470689
R
5083 int time2;
5084 switch (reload_when_needed[i])
5085 {
5086 case RELOAD_FOR_OTHER_ADDRESS:
5087 time2 = 0;
5088 break;
5089 case RELOAD_FOR_INPADDR_ADDRESS:
5090 time2 = reload_opnum[i] * 4 + 1;
5091 break;
5092 case RELOAD_FOR_INPUT_ADDRESS:
5093 time2 = reload_opnum[i] * 4 + 2;
5094 break;
5095 case RELOAD_FOR_INPUT:
5096 time2 = reload_opnum[i] * 4 + 3;
5097 break;
5098 case RELOAD_FOR_OUTPUT:
5099 /* All RELOAD_FOR_OUTPUT reloads become live just after the
5100 instruction is executed. */
5101 time2 = MAX_RECOG_OPERANDS * 4;
5102 break;
5103 /* The first RELOAD_FOR_OUTPUT_ADDRESS reload conflicts with the
5104 RELOAD_FOR_OUTPUT reloads, so assign it the same time value. */
5105 case RELOAD_FOR_OUTPUT_ADDRESS:
5106 time2 = MAX_RECOG_OPERANDS * 4 + reload_opnum[i];
5107 break;
5108 case RELOAD_OTHER:
5109 if (! reload_in[i] || rtx_equal_p (reload_in[i], value))
5110 {
5111 time2 = MAX_RECOG_OPERANDS * 4;
5112 break;
5113 }
5114 default:
5115 time2 = 0;
5116 }
5117 if (time1 >= time2)
5118 return 0;
6e684430 5119 }
6e684430
R
5120 }
5121 }
5122 return 1;
5123}
5124
32131a9c
RK
5125/* Find a spill register to use as a reload register for reload R.
5126 LAST_RELOAD is non-zero if this is the last reload for the insn being
5127 processed.
5128
5129 Set reload_reg_rtx[R] to the register allocated.
5130
5131 If NOERROR is nonzero, we return 1 if successful,
5132 or 0 if we couldn't find a spill reg and we didn't change anything. */
5133
5134static int
5135allocate_reload_reg (r, insn, last_reload, noerror)
5136 int r;
5137 rtx insn;
5138 int last_reload;
5139 int noerror;
5140{
5141 int i;
5142 int pass;
5143 int count;
5144 rtx new;
5145 int regno;
5146
5147 /* If we put this reload ahead, thinking it is a group,
5148 then insist on finding a group. Otherwise we can grab a
a8fdc208 5149 reg that some other reload needs.
32131a9c
RK
5150 (That can happen when we have a 68000 DATA_OR_FP_REG
5151 which is a group of data regs or one fp reg.)
5152 We need not be so restrictive if there are no more reloads
5153 for this insn.
5154
5155 ??? Really it would be nicer to have smarter handling
5156 for that kind of reg class, where a problem like this is normal.
5157 Perhaps those classes should be avoided for reloading
5158 by use of more alternatives. */
5159
5160 int force_group = reload_nregs[r] > 1 && ! last_reload;
5161
5162 /* If we want a single register and haven't yet found one,
5163 take any reg in the right class and not in use.
5164 If we want a consecutive group, here is where we look for it.
5165
5166 We use two passes so we can first look for reload regs to
5167 reuse, which are already in use for other reloads in this insn,
5168 and only then use additional registers.
5169 I think that maximizing reuse is needed to make sure we don't
5170 run out of reload regs. Suppose we have three reloads, and
5171 reloads A and B can share regs. These need two regs.
5172 Suppose A and B are given different regs.
5173 That leaves none for C. */
5174 for (pass = 0; pass < 2; pass++)
5175 {
5176 /* I is the index in spill_regs.
5177 We advance it round-robin between insns to use all spill regs
5178 equally, so that inherited reloads have a chance
a5339699
RK
5179 of leapfrogging each other. Don't do this, however, when we have
5180 group needs and failure would be fatal; if we only have a relatively
5181 small number of spill registers, and more than one of them has
5182 group needs, then by starting in the middle, we may end up
5183 allocating the first one in such a way that we are not left with
5184 sufficient groups to handle the rest. */
5185
5186 if (noerror || ! force_group)
5187 i = last_spill_reg;
5188 else
5189 i = -1;
5190
5191 for (count = 0; count < n_spills; count++)
32131a9c
RK
5192 {
5193 int class = (int) reload_reg_class[r];
5194
5195 i = (i + 1) % n_spills;
5196
6e684430
R
5197 if ((reload_reg_free_p (spill_regs[i], reload_opnum[r],
5198 reload_when_needed[r])
f5470689 5199 || (reload_in[r]
6e684430
R
5200 /* We check reload_reg_used to make sure we
5201 don't clobber the return register. */
5202 && ! TEST_HARD_REG_BIT (reload_reg_used, spill_regs[i])
5203 && reload_reg_free_for_value_p (spill_regs[i],
5204 reload_opnum[r],
5205 reload_when_needed[r],
f5470689
R
5206 reload_in[r],
5207 reload_out[r], r)))
32131a9c
RK
5208 && TEST_HARD_REG_BIT (reg_class_contents[class], spill_regs[i])
5209 && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
be7ae2a4
RK
5210 /* Look first for regs to share, then for unshared. But
5211 don't share regs used for inherited reloads; they are
5212 the ones we want to preserve. */
5213 && (pass
5214 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5215 spill_regs[i])
5216 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5217 spill_regs[i]))))
32131a9c
RK
5218 {
5219 int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
5220 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5221 (on 68000) got us two FP regs. If NR is 1,
5222 we would reject both of them. */
5223 if (force_group)
5224 nr = CLASS_MAX_NREGS (reload_reg_class[r], reload_mode[r]);
5225 /* If we need only one reg, we have already won. */
5226 if (nr == 1)
5227 {
5228 /* But reject a single reg if we demand a group. */
5229 if (force_group)
5230 continue;
5231 break;
5232 }
5233 /* Otherwise check that as many consecutive regs as we need
5234 are available here.
5235 Also, don't use for a group registers that are
5236 needed for nongroups. */
5237 if (! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
5238 while (nr > 1)
5239 {
5240 regno = spill_regs[i] + nr - 1;
5241 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5242 && spill_reg_order[regno] >= 0
546b63fb
RK
5243 && reload_reg_free_p (regno, reload_opnum[r],
5244 reload_when_needed[r])
32131a9c
RK
5245 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
5246 regno)))
5247 break;
5248 nr--;
5249 }
5250 if (nr == 1)
5251 break;
5252 }
5253 }
5254
5255 /* If we found something on pass 1, omit pass 2. */
5256 if (count < n_spills)
5257 break;
5258 }
5259
5260 /* We should have found a spill register by now. */
5261 if (count == n_spills)
5262 {
5263 if (noerror)
5264 return 0;
139fc12e 5265 goto failure;
32131a9c
RK
5266 }
5267
be7ae2a4
RK
5268 /* I is the index in SPILL_REG_RTX of the reload register we are to
5269 allocate. Get an rtx for it and find its register number. */
32131a9c
RK
5270
5271 new = spill_reg_rtx[i];
5272
5273 if (new == 0 || GET_MODE (new) != reload_mode[r])
be7ae2a4 5274 spill_reg_rtx[i] = new
38a448ca 5275 = gen_rtx_REG (reload_mode[r], spill_regs[i]);
be7ae2a4 5276
32131a9c
RK
5277 regno = true_regnum (new);
5278
5279 /* Detect when the reload reg can't hold the reload mode.
5280 This used to be one `if', but Sequent compiler can't handle that. */
5281 if (HARD_REGNO_MODE_OK (regno, reload_mode[r]))
5282 {
5283 enum machine_mode test_mode = VOIDmode;
5284 if (reload_in[r])
5285 test_mode = GET_MODE (reload_in[r]);
5286 /* If reload_in[r] has VOIDmode, it means we will load it
5287 in whatever mode the reload reg has: to wit, reload_mode[r].
5288 We have already tested that for validity. */
5289 /* Aside from that, we need to test that the expressions
5290 to reload from or into have modes which are valid for this
5291 reload register. Otherwise the reload insns would be invalid. */
5292 if (! (reload_in[r] != 0 && test_mode != VOIDmode
5293 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5294 if (! (reload_out[r] != 0
5295 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (reload_out[r]))))
be7ae2a4
RK
5296 {
5297 /* The reg is OK. */
5298 last_spill_reg = i;
5299
5300 /* Mark as in use for this insn the reload regs we use
5301 for this. */
5302 mark_reload_reg_in_use (spill_regs[i], reload_opnum[r],
5303 reload_when_needed[r], reload_mode[r]);
5304
5305 reload_reg_rtx[r] = new;
e6e52be0 5306 reload_spill_index[r] = spill_regs[i];
be7ae2a4
RK
5307 return 1;
5308 }
32131a9c
RK
5309 }
5310
5311 /* The reg is not OK. */
5312 if (noerror)
5313 return 0;
5314
139fc12e 5315 failure:
32131a9c
RK
5316 if (asm_noperands (PATTERN (insn)) < 0)
5317 /* It's the compiler's fault. */
a89b2cc4 5318 fatal_insn ("Could not find a spill register", insn);
32131a9c
RK
5319
5320 /* It's the user's fault; the operand's mode and constraint
5321 don't match. Disable this reload so we don't crash in final. */
5322 error_for_asm (insn,
5323 "`asm' operand constraint incompatible with operand size");
5324 reload_in[r] = 0;
5325 reload_out[r] = 0;
5326 reload_reg_rtx[r] = 0;
5327 reload_optional[r] = 1;
5328 reload_secondary_p[r] = 1;
5329
5330 return 1;
5331}
5332\f
5333/* Assign hard reg targets for the pseudo-registers we must reload
5334 into hard regs for this insn.
5335 Also output the instructions to copy them in and out of the hard regs.
5336
5337 For machines with register classes, we are responsible for
5338 finding a reload reg in the proper class. */
5339
5340static void
5341choose_reload_regs (insn, avoid_return_reg)
5342 rtx insn;
32131a9c
RK
5343 rtx avoid_return_reg;
5344{
5345 register int i, j;
5346 int max_group_size = 1;
5347 enum reg_class group_class = NO_REGS;
5348 int inheritance;
5349
5350 rtx save_reload_reg_rtx[MAX_RELOADS];
5351 char save_reload_inherited[MAX_RELOADS];
5352 rtx save_reload_inheritance_insn[MAX_RELOADS];
5353 rtx save_reload_override_in[MAX_RELOADS];
5354 int save_reload_spill_index[MAX_RELOADS];
5355 HARD_REG_SET save_reload_reg_used;
546b63fb 5356 HARD_REG_SET save_reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
47c8cf91 5357 HARD_REG_SET save_reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
546b63fb 5358 HARD_REG_SET save_reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
47c8cf91 5359 HARD_REG_SET save_reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
546b63fb
RK
5360 HARD_REG_SET save_reload_reg_used_in_input[MAX_RECOG_OPERANDS];
5361 HARD_REG_SET save_reload_reg_used_in_output[MAX_RECOG_OPERANDS];
32131a9c 5362 HARD_REG_SET save_reload_reg_used_in_op_addr;
893bc853 5363 HARD_REG_SET save_reload_reg_used_in_op_addr_reload;
546b63fb
RK
5364 HARD_REG_SET save_reload_reg_used_in_insn;
5365 HARD_REG_SET save_reload_reg_used_in_other_addr;
32131a9c
RK
5366 HARD_REG_SET save_reload_reg_used_at_all;
5367
5368 bzero (reload_inherited, MAX_RELOADS);
4c9a05bc
RK
5369 bzero ((char *) reload_inheritance_insn, MAX_RELOADS * sizeof (rtx));
5370 bzero ((char *) reload_override_in, MAX_RELOADS * sizeof (rtx));
32131a9c
RK
5371
5372 CLEAR_HARD_REG_SET (reload_reg_used);
5373 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
32131a9c 5374 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
893bc853 5375 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
546b63fb
RK
5376 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5377 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
32131a9c 5378
546b63fb
RK
5379 for (i = 0; i < reload_n_operands; i++)
5380 {
5381 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5382 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5383 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
47c8cf91 5384 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
546b63fb 5385 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
47c8cf91 5386 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
546b63fb 5387 }
32131a9c 5388
32131a9c
RK
5389 /* Don't bother with avoiding the return reg
5390 if we have no mandatory reload that could use it. */
f95182a4 5391 if (SMALL_REGISTER_CLASSES && avoid_return_reg)
32131a9c
RK
5392 {
5393 int do_avoid = 0;
5394 int regno = REGNO (avoid_return_reg);
5395 int nregs
5396 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
5397 int r;
5398
5399 for (r = regno; r < regno + nregs; r++)
5400 if (spill_reg_order[r] >= 0)
5401 for (j = 0; j < n_reloads; j++)
5402 if (!reload_optional[j] && reload_reg_rtx[j] == 0
5403 && (reload_in[j] != 0 || reload_out[j] != 0
5404 || reload_secondary_p[j])
5405 &&
5406 TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[j]], r))
5407 do_avoid = 1;
5408 if (!do_avoid)
5409 avoid_return_reg = 0;
5410 }
32131a9c
RK
5411
5412#if 0 /* Not needed, now that we can always retry without inheritance. */
5413 /* See if we have more mandatory reloads than spill regs.
5414 If so, then we cannot risk optimizations that could prevent
a8fdc208 5415 reloads from sharing one spill register.
32131a9c
RK
5416
5417 Since we will try finding a better register than reload_reg_rtx
5418 unless it is equal to reload_in or reload_out, count such reloads. */
5419
5420 {
e9a25f70 5421 int tem = SMALL_REGISTER_CLASSES? (avoid_return_reg != 0): 0;
32131a9c
RK
5422 for (j = 0; j < n_reloads; j++)
5423 if (! reload_optional[j]
5424 && (reload_in[j] != 0 || reload_out[j] != 0 || reload_secondary_p[j])
5425 && (reload_reg_rtx[j] == 0
5426 || (! rtx_equal_p (reload_reg_rtx[j], reload_in[j])
5427 && ! rtx_equal_p (reload_reg_rtx[j], reload_out[j]))))
5428 tem++;
5429 if (tem > n_spills)
5430 must_reuse = 1;
5431 }
5432#endif
5433
32131a9c
RK
5434 /* Don't use the subroutine call return reg for a reload
5435 if we are supposed to avoid it. */
f95182a4 5436 if (SMALL_REGISTER_CLASSES && avoid_return_reg)
32131a9c
RK
5437 {
5438 int regno = REGNO (avoid_return_reg);
5439 int nregs
5440 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
5441 int r;
5442
5443 for (r = regno; r < regno + nregs; r++)
5444 if (spill_reg_order[r] >= 0)
5445 SET_HARD_REG_BIT (reload_reg_used, r);
5446 }
32131a9c
RK
5447
5448 /* In order to be certain of getting the registers we need,
5449 we must sort the reloads into order of increasing register class.
5450 Then our grabbing of reload registers will parallel the process
a8fdc208 5451 that provided the reload registers.
32131a9c
RK
5452
5453 Also note whether any of the reloads wants a consecutive group of regs.
5454 If so, record the maximum size of the group desired and what
5455 register class contains all the groups needed by this insn. */
5456
5457 for (j = 0; j < n_reloads; j++)
5458 {
5459 reload_order[j] = j;
5460 reload_spill_index[j] = -1;
5461
5462 reload_mode[j]
546b63fb
RK
5463 = (reload_inmode[j] == VOIDmode
5464 || (GET_MODE_SIZE (reload_outmode[j])
5465 > GET_MODE_SIZE (reload_inmode[j])))
5466 ? reload_outmode[j] : reload_inmode[j];
32131a9c
RK
5467
5468 reload_nregs[j] = CLASS_MAX_NREGS (reload_reg_class[j], reload_mode[j]);
5469
5470 if (reload_nregs[j] > 1)
5471 {
5472 max_group_size = MAX (reload_nregs[j], max_group_size);
5473 group_class = reg_class_superunion[(int)reload_reg_class[j]][(int)group_class];
5474 }
5475
5476 /* If we have already decided to use a certain register,
5477 don't use it in another way. */
5478 if (reload_reg_rtx[j])
546b63fb 5479 mark_reload_reg_in_use (REGNO (reload_reg_rtx[j]), reload_opnum[j],
32131a9c
RK
5480 reload_when_needed[j], reload_mode[j]);
5481 }
5482
5483 if (n_reloads > 1)
5484 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5485
4c9a05bc
RK
5486 bcopy ((char *) reload_reg_rtx, (char *) save_reload_reg_rtx,
5487 sizeof reload_reg_rtx);
32131a9c 5488 bcopy (reload_inherited, save_reload_inherited, sizeof reload_inherited);
4c9a05bc
RK
5489 bcopy ((char *) reload_inheritance_insn,
5490 (char *) save_reload_inheritance_insn,
32131a9c 5491 sizeof reload_inheritance_insn);
4c9a05bc 5492 bcopy ((char *) reload_override_in, (char *) save_reload_override_in,
32131a9c 5493 sizeof reload_override_in);
4c9a05bc 5494 bcopy ((char *) reload_spill_index, (char *) save_reload_spill_index,
32131a9c
RK
5495 sizeof reload_spill_index);
5496 COPY_HARD_REG_SET (save_reload_reg_used, reload_reg_used);
5497 COPY_HARD_REG_SET (save_reload_reg_used_at_all, reload_reg_used_at_all);
32131a9c
RK
5498 COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr,
5499 reload_reg_used_in_op_addr);
893bc853
RK
5500
5501 COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr_reload,
5502 reload_reg_used_in_op_addr_reload);
5503
546b63fb
RK
5504 COPY_HARD_REG_SET (save_reload_reg_used_in_insn,
5505 reload_reg_used_in_insn);
5506 COPY_HARD_REG_SET (save_reload_reg_used_in_other_addr,
5507 reload_reg_used_in_other_addr);
5508
5509 for (i = 0; i < reload_n_operands; i++)
5510 {
5511 COPY_HARD_REG_SET (save_reload_reg_used_in_output[i],
5512 reload_reg_used_in_output[i]);
5513 COPY_HARD_REG_SET (save_reload_reg_used_in_input[i],
5514 reload_reg_used_in_input[i]);
5515 COPY_HARD_REG_SET (save_reload_reg_used_in_input_addr[i],
5516 reload_reg_used_in_input_addr[i]);
47c8cf91
ILT
5517 COPY_HARD_REG_SET (save_reload_reg_used_in_inpaddr_addr[i],
5518 reload_reg_used_in_inpaddr_addr[i]);
546b63fb
RK
5519 COPY_HARD_REG_SET (save_reload_reg_used_in_output_addr[i],
5520 reload_reg_used_in_output_addr[i]);
47c8cf91
ILT
5521 COPY_HARD_REG_SET (save_reload_reg_used_in_outaddr_addr[i],
5522 reload_reg_used_in_outaddr_addr[i]);
546b63fb 5523 }
32131a9c 5524
58b1581b
RS
5525 /* If -O, try first with inheritance, then turning it off.
5526 If not -O, don't do inheritance.
5527 Using inheritance when not optimizing leads to paradoxes
5528 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5529 because one side of the comparison might be inherited. */
32131a9c 5530
58b1581b 5531 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
32131a9c
RK
5532 {
5533 /* Process the reloads in order of preference just found.
5534 Beyond this point, subregs can be found in reload_reg_rtx.
5535
5536 This used to look for an existing reloaded home for all
5537 of the reloads, and only then perform any new reloads.
5538 But that could lose if the reloads were done out of reg-class order
5539 because a later reload with a looser constraint might have an old
5540 home in a register needed by an earlier reload with a tighter constraint.
5541
5542 To solve this, we make two passes over the reloads, in the order
5543 described above. In the first pass we try to inherit a reload
5544 from a previous insn. If there is a later reload that needs a
5545 class that is a proper subset of the class being processed, we must
5546 also allocate a spill register during the first pass.
5547
5548 Then make a second pass over the reloads to allocate any reloads
5549 that haven't been given registers yet. */
5550
be7ae2a4
RK
5551 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5552
32131a9c
RK
5553 for (j = 0; j < n_reloads; j++)
5554 {
5555 register int r = reload_order[j];
5556
5557 /* Ignore reloads that got marked inoperative. */
b080c137
RK
5558 if (reload_out[r] == 0 && reload_in[r] == 0
5559 && ! reload_secondary_p[r])
32131a9c
RK
5560 continue;
5561
5562 /* If find_reloads chose a to use reload_in or reload_out as a reload
b080c137
RK
5563 register, we don't need to chose one. Otherwise, try even if it
5564 found one since we might save an insn if we find the value lying
5565 around. */
32131a9c
RK
5566 if (reload_in[r] != 0 && reload_reg_rtx[r] != 0
5567 && (rtx_equal_p (reload_in[r], reload_reg_rtx[r])
5568 || rtx_equal_p (reload_out[r], reload_reg_rtx[r])))
5569 continue;
5570
5571#if 0 /* No longer needed for correct operation.
5572 It might give better code, or might not; worth an experiment? */
5573 /* If this is an optional reload, we can't inherit from earlier insns
5574 until we are sure that any non-optional reloads have been allocated.
5575 The following code takes advantage of the fact that optional reloads
5576 are at the end of reload_order. */
5577 if (reload_optional[r] != 0)
5578 for (i = 0; i < j; i++)
5579 if ((reload_out[reload_order[i]] != 0
5580 || reload_in[reload_order[i]] != 0
5581 || reload_secondary_p[reload_order[i]])
5582 && ! reload_optional[reload_order[i]]
5583 && reload_reg_rtx[reload_order[i]] == 0)
5584 allocate_reload_reg (reload_order[i], insn, 0, inheritance);
5585#endif
5586
5587 /* First see if this pseudo is already available as reloaded
5588 for a previous insn. We cannot try to inherit for reloads
5589 that are smaller than the maximum number of registers needed
5590 for groups unless the register we would allocate cannot be used
5591 for the groups.
5592
5593 We could check here to see if this is a secondary reload for
5594 an object that is already in a register of the desired class.
5595 This would avoid the need for the secondary reload register.
5596 But this is complex because we can't easily determine what
b080c137
RK
5597 objects might want to be loaded via this reload. So let a
5598 register be allocated here. In `emit_reload_insns' we suppress
5599 one of the loads in the case described above. */
32131a9c
RK
5600
5601 if (inheritance)
5602 {
5603 register int regno = -1;
db660765 5604 enum machine_mode mode;
32131a9c
RK
5605
5606 if (reload_in[r] == 0)
5607 ;
5608 else if (GET_CODE (reload_in[r]) == REG)
db660765
TW
5609 {
5610 regno = REGNO (reload_in[r]);
5611 mode = GET_MODE (reload_in[r]);
5612 }
32131a9c 5613 else if (GET_CODE (reload_in_reg[r]) == REG)
db660765
TW
5614 {
5615 regno = REGNO (reload_in_reg[r]);
5616 mode = GET_MODE (reload_in_reg[r]);
5617 }
b60a8416
R
5618 else if (GET_CODE (reload_in[r]) == MEM)
5619 {
5620 rtx prev = prev_nonnote_insn (insn), note;
5621
5622 if (prev && GET_CODE (prev) == INSN
5623 && GET_CODE (PATTERN (prev)) == USE
5624 && GET_CODE (XEXP (PATTERN (prev), 0)) == REG
5625 && (REGNO (XEXP (PATTERN (prev), 0))
5626 >= FIRST_PSEUDO_REGISTER)
5627 && (note = find_reg_note (prev, REG_EQUAL, NULL_RTX))
5628 && GET_CODE (XEXP (note, 0)) == MEM)
5629 {
5630 rtx addr = XEXP (XEXP (note, 0), 0);
5631 int size_diff
5632 = (GET_MODE_SIZE (GET_MODE (addr))
5633 - GET_MODE_SIZE (GET_MODE (reload_in[r])));
5634 if (size_diff >= 0
5635 && rtx_equal_p ((BYTES_BIG_ENDIAN
5636 ? plus_constant (addr, size_diff)
5637 : addr),
5638 XEXP (reload_in[r], 0)))
5639 {
5640 regno = REGNO (XEXP (PATTERN (prev), 0));
5641 mode = GET_MODE (reload_in[r]);
b60a8416
R
5642 }
5643 }
5644 }
32131a9c
RK
5645#if 0
5646 /* This won't work, since REGNO can be a pseudo reg number.
5647 Also, it takes much more hair to keep track of all the things
5648 that can invalidate an inherited reload of part of a pseudoreg. */
5649 else if (GET_CODE (reload_in[r]) == SUBREG
5650 && GET_CODE (SUBREG_REG (reload_in[r])) == REG)
5651 regno = REGNO (SUBREG_REG (reload_in[r])) + SUBREG_WORD (reload_in[r]);
5652#endif
5653
5654 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5655 {
e6e52be0 5656 i = REGNO (reg_last_reload_reg[regno]);
32131a9c
RK
5657
5658 if (reg_reloaded_contents[i] == regno
e6e52be0 5659 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
db660765
TW
5660 && (GET_MODE_SIZE (GET_MODE (reg_last_reload_reg[regno]))
5661 >= GET_MODE_SIZE (mode))
e6e52be0 5662 && HARD_REGNO_MODE_OK (i, reload_mode[r])
32131a9c 5663 && TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
e6e52be0 5664 i)
32131a9c
RK
5665 && (reload_nregs[r] == max_group_size
5666 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
e6e52be0 5667 i))
6e684430
R
5668 && ((reload_reg_free_p (i, reload_opnum[r],
5669 reload_when_needed[r])
5670 && reload_reg_free_before_p (i, reload_opnum[r],
5671 reload_when_needed[r]))
5672 || reload_reg_free_for_value_p (i, reload_opnum[r],
5673 reload_when_needed[r],
f5470689
R
5674 reload_in[r],
5675 reload_out[r], r)))
32131a9c
RK
5676 {
5677 /* If a group is needed, verify that all the subsequent
0f41302f 5678 registers still have their values intact. */
32131a9c 5679 int nr
e6e52be0 5680 = HARD_REGNO_NREGS (i, reload_mode[r]);
32131a9c
RK
5681 int k;
5682
5683 for (k = 1; k < nr; k++)
e6e52be0
R
5684 if (reg_reloaded_contents[i + k] != regno
5685 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
32131a9c
RK
5686 break;
5687
5688 if (k == nr)
5689 {
c74fa651
RS
5690 int i1;
5691
5692 /* We found a register that contains the
5693 value we need. If this register is the
5694 same as an `earlyclobber' operand of the
5695 current insn, just mark it as a place to
5696 reload from since we can't use it as the
5697 reload register itself. */
5698
5699 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5700 if (reg_overlap_mentioned_for_reload_p
5701 (reg_last_reload_reg[regno],
5702 reload_earlyclobbers[i1]))
5703 break;
5704
8908158d 5705 if (i1 != n_earlyclobbers
e6e52be0
R
5706 /* Don't use it if we'd clobber a pseudo reg. */
5707 || (spill_reg_order[i] < 0
5708 && reload_out[r]
5709 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
8908158d
RS
5710 /* Don't really use the inherited spill reg
5711 if we need it wider than we've got it. */
5712 || (GET_MODE_SIZE (reload_mode[r])
5713 > GET_MODE_SIZE (mode)))
c74fa651
RS
5714 reload_override_in[r] = reg_last_reload_reg[regno];
5715 else
5716 {
54c40e68 5717 int k;
c74fa651
RS
5718 /* We can use this as a reload reg. */
5719 /* Mark the register as in use for this part of
5720 the insn. */
e6e52be0 5721 mark_reload_reg_in_use (i,
c74fa651
RS
5722 reload_opnum[r],
5723 reload_when_needed[r],
5724 reload_mode[r]);
5725 reload_reg_rtx[r] = reg_last_reload_reg[regno];
5726 reload_inherited[r] = 1;
5727 reload_inheritance_insn[r]
5728 = reg_reloaded_insn[i];
5729 reload_spill_index[r] = i;
54c40e68
RS
5730 for (k = 0; k < nr; k++)
5731 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
e6e52be0 5732 i + k);
c74fa651 5733 }
32131a9c
RK
5734 }
5735 }
5736 }
5737 }
5738
5739 /* Here's another way to see if the value is already lying around. */
5740 if (inheritance
5741 && reload_in[r] != 0
5742 && ! reload_inherited[r]
5743 && reload_out[r] == 0
5744 && (CONSTANT_P (reload_in[r])
5745 || GET_CODE (reload_in[r]) == PLUS
5746 || GET_CODE (reload_in[r]) == REG
5747 || GET_CODE (reload_in[r]) == MEM)
5748 && (reload_nregs[r] == max_group_size
5749 || ! reg_classes_intersect_p (reload_reg_class[r], group_class)))
5750 {
5751 register rtx equiv
5752 = find_equiv_reg (reload_in[r], insn, reload_reg_class[r],
fb3821f7 5753 -1, NULL_PTR, 0, reload_mode[r]);
32131a9c
RK
5754 int regno;
5755
5756 if (equiv != 0)
5757 {
5758 if (GET_CODE (equiv) == REG)
5759 regno = REGNO (equiv);
5760 else if (GET_CODE (equiv) == SUBREG)
5761 {
f8a9e02b
RK
5762 /* This must be a SUBREG of a hard register.
5763 Make a new REG since this might be used in an
5764 address and not all machines support SUBREGs
5765 there. */
5766 regno = REGNO (SUBREG_REG (equiv)) + SUBREG_WORD (equiv);
38a448ca 5767 equiv = gen_rtx_REG (reload_mode[r], regno);
32131a9c
RK
5768 }
5769 else
5770 abort ();
5771 }
5772
5773 /* If we found a spill reg, reject it unless it is free
5774 and of the desired class. */
5775 if (equiv != 0
5776 && ((spill_reg_order[regno] >= 0
6e684430
R
5777 && ! (reload_reg_free_before_p (regno, reload_opnum[r],
5778 reload_when_needed[r])
5779 || reload_reg_free_for_value_p (regno,
5780 reload_opnum[r],
5781 reload_when_needed[r],
f5470689
R
5782 reload_in[r],
5783 reload_out[r], r)))
32131a9c
RK
5784 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
5785 regno)))
5786 equiv = 0;
5787
5788 if (equiv != 0 && TEST_HARD_REG_BIT (reload_reg_used_at_all, regno))
5789 equiv = 0;
5790
5791 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, reload_mode[r]))
5792 equiv = 0;
5793
5794 /* We found a register that contains the value we need.
5795 If this register is the same as an `earlyclobber' operand
5796 of the current insn, just mark it as a place to reload from
5797 since we can't use it as the reload register itself. */
5798
5799 if (equiv != 0)
5800 for (i = 0; i < n_earlyclobbers; i++)
bfa30b22
RK
5801 if (reg_overlap_mentioned_for_reload_p (equiv,
5802 reload_earlyclobbers[i]))
32131a9c
RK
5803 {
5804 reload_override_in[r] = equiv;
5805 equiv = 0;
5806 break;
5807 }
5808
0f41302f
MS
5809 /* JRV: If the equiv register we have found is
5810 explicitly clobbered in the current insn, mark but
5811 don't use, as above. */
32131a9c
RK
5812
5813 if (equiv != 0 && regno_clobbered_p (regno, insn))
5814 {
5815 reload_override_in[r] = equiv;
5816 equiv = 0;
5817 }
5818
5819 /* If we found an equivalent reg, say no code need be generated
5820 to load it, and use it as our reload reg. */
3ec2ea3e 5821 if (equiv != 0 && regno != HARD_FRAME_POINTER_REGNUM)
32131a9c 5822 {
100338df
JL
5823 int nr = HARD_REGNO_NREGS (regno, reload_mode[r]);
5824 int k;
32131a9c
RK
5825 reload_reg_rtx[r] = equiv;
5826 reload_inherited[r] = 1;
100338df
JL
5827
5828 /* If any of the hard registers in EQUIV are spill
5829 registers, mark them as in use for this insn. */
5830 for (k = 0; k < nr; k++)
be7ae2a4 5831 {
100338df
JL
5832 i = spill_reg_order[regno + k];
5833 if (i >= 0)
5834 {
5835 mark_reload_reg_in_use (regno, reload_opnum[r],
5836 reload_when_needed[r],
5837 reload_mode[r]);
5838 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5839 regno + k);
5840 }
be7ae2a4 5841 }
32131a9c
RK
5842 }
5843 }
5844
5845 /* If we found a register to use already, or if this is an optional
5846 reload, we are done. */
5847 if (reload_reg_rtx[r] != 0 || reload_optional[r] != 0)
5848 continue;
5849
5850#if 0 /* No longer needed for correct operation. Might or might not
5851 give better code on the average. Want to experiment? */
5852
5853 /* See if there is a later reload that has a class different from our
5854 class that intersects our class or that requires less register
5855 than our reload. If so, we must allocate a register to this
5856 reload now, since that reload might inherit a previous reload
5857 and take the only available register in our class. Don't do this
5858 for optional reloads since they will force all previous reloads
5859 to be allocated. Also don't do this for reloads that have been
5860 turned off. */
5861
5862 for (i = j + 1; i < n_reloads; i++)
5863 {
5864 int s = reload_order[i];
5865
d45cf215
RS
5866 if ((reload_in[s] == 0 && reload_out[s] == 0
5867 && ! reload_secondary_p[s])
32131a9c
RK
5868 || reload_optional[s])
5869 continue;
5870
5871 if ((reload_reg_class[s] != reload_reg_class[r]
5872 && reg_classes_intersect_p (reload_reg_class[r],
5873 reload_reg_class[s]))
5874 || reload_nregs[s] < reload_nregs[r])
5875 break;
5876 }
5877
5878 if (i == n_reloads)
5879 continue;
5880
5881 allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance);
5882#endif
5883 }
5884
5885 /* Now allocate reload registers for anything non-optional that
5886 didn't get one yet. */
5887 for (j = 0; j < n_reloads; j++)
5888 {
5889 register int r = reload_order[j];
5890
5891 /* Ignore reloads that got marked inoperative. */
5892 if (reload_out[r] == 0 && reload_in[r] == 0 && ! reload_secondary_p[r])
5893 continue;
5894
5895 /* Skip reloads that already have a register allocated or are
0f41302f 5896 optional. */
32131a9c
RK
5897 if (reload_reg_rtx[r] != 0 || reload_optional[r])
5898 continue;
5899
5900 if (! allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance))
5901 break;
5902 }
5903
5904 /* If that loop got all the way, we have won. */
5905 if (j == n_reloads)
5906 break;
5907
5908 fail:
5909 /* Loop around and try without any inheritance. */
5910 /* First undo everything done by the failed attempt
5911 to allocate with inheritance. */
4c9a05bc
RK
5912 bcopy ((char *) save_reload_reg_rtx, (char *) reload_reg_rtx,
5913 sizeof reload_reg_rtx);
5914 bcopy ((char *) save_reload_inherited, (char *) reload_inherited,
5915 sizeof reload_inherited);
5916 bcopy ((char *) save_reload_inheritance_insn,
5917 (char *) reload_inheritance_insn,
32131a9c 5918 sizeof reload_inheritance_insn);
4c9a05bc 5919 bcopy ((char *) save_reload_override_in, (char *) reload_override_in,
32131a9c 5920 sizeof reload_override_in);
4c9a05bc 5921 bcopy ((char *) save_reload_spill_index, (char *) reload_spill_index,
32131a9c
RK
5922 sizeof reload_spill_index);
5923 COPY_HARD_REG_SET (reload_reg_used, save_reload_reg_used);
5924 COPY_HARD_REG_SET (reload_reg_used_at_all, save_reload_reg_used_at_all);
32131a9c
RK
5925 COPY_HARD_REG_SET (reload_reg_used_in_op_addr,
5926 save_reload_reg_used_in_op_addr);
893bc853
RK
5927 COPY_HARD_REG_SET (reload_reg_used_in_op_addr_reload,
5928 save_reload_reg_used_in_op_addr_reload);
546b63fb
RK
5929 COPY_HARD_REG_SET (reload_reg_used_in_insn,
5930 save_reload_reg_used_in_insn);
5931 COPY_HARD_REG_SET (reload_reg_used_in_other_addr,
5932 save_reload_reg_used_in_other_addr);
5933
5934 for (i = 0; i < reload_n_operands; i++)
5935 {
5936 COPY_HARD_REG_SET (reload_reg_used_in_input[i],
5937 save_reload_reg_used_in_input[i]);
5938 COPY_HARD_REG_SET (reload_reg_used_in_output[i],
5939 save_reload_reg_used_in_output[i]);
5940 COPY_HARD_REG_SET (reload_reg_used_in_input_addr[i],
5941 save_reload_reg_used_in_input_addr[i]);
47c8cf91
ILT
5942 COPY_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i],
5943 save_reload_reg_used_in_inpaddr_addr[i]);
546b63fb
RK
5944 COPY_HARD_REG_SET (reload_reg_used_in_output_addr[i],
5945 save_reload_reg_used_in_output_addr[i]);
47c8cf91
ILT
5946 COPY_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i],
5947 save_reload_reg_used_in_outaddr_addr[i]);
546b63fb 5948 }
32131a9c
RK
5949 }
5950
5951 /* If we thought we could inherit a reload, because it seemed that
5952 nothing else wanted the same reload register earlier in the insn,
5953 verify that assumption, now that all reloads have been assigned. */
5954
5955 for (j = 0; j < n_reloads; j++)
5956 {
5957 register int r = reload_order[j];
5958
5959 if (reload_inherited[r] && reload_reg_rtx[r] != 0
6e684430
R
5960 && ! (reload_reg_free_before_p (true_regnum (reload_reg_rtx[r]),
5961 reload_opnum[r],
5962 reload_when_needed[r])
5963 || reload_reg_free_for_value_p (true_regnum (reload_reg_rtx[r]),
5964 reload_opnum[r],
5965 reload_when_needed[r],
f5470689
R
5966 reload_in[r],
5967 reload_out[r], r)))
32131a9c 5968 reload_inherited[r] = 0;
029b38ff
R
5969 /* If we can inherit a RELOAD_FOR_INPUT, then we do not need its related
5970 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads.
5971 ??? This could be extended to other reload types, but these are
5972 more tricky to handle:
5973 RELOAD_FOR_OTHER_ADDRESS reloads might have been merged, so we
5974 can't eliminate them without a check that *all* references are
5975 now unused due to inheritance.
5976 While RELOAD_FOR_INPADDR_ADDRESS and RELOAD_FOR_OUTADDR_ADDRESS are
5977 not merged, we can't be sure that we have eliminated the use of
5978 that particular reload if we have seen just one
5979 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_OUTPUT_ADDRESS being inherited,
5980 since there might be multiple of the latter two reloads for a single
5981 operand.
5982 RELOAD_FOR_OPADDR_ADDR reloads for different operands are not
5983 merged, but might share the same register by courtesy of
5984 reload_reg_free_for_value_p. reload_reg_used_in_op_addr_reload
5985 does not differentiate by opnum, thus calling clear_reload_reg_in_use
5986 for one of these reloads would mark the register as free even though
5987 another RELOAD_FOR_OPADDR_ADDR reload might still use it. */
5988 else if (reload_inherited[r] && reload_when_needed[r] == RELOAD_FOR_INPUT)
5989 {
5990 for (i = 0; i < n_reloads; i++)
5991 {
5992 if ((reload_when_needed[i] == RELOAD_FOR_INPUT_ADDRESS
5993 || reload_when_needed[i] == RELOAD_FOR_INPADDR_ADDRESS)
5994 && reload_opnum[i] == reload_opnum[r]
5995 && reload_in[i] && reload_reg_rtx[i])
5996 {
5997 int regno = true_regnum (reload_reg_rtx[i]);
5998
5999 reload_in[i] = 0;
6000 if (spill_reg_order[regno] >= 0)
6001 clear_reload_reg_in_use (regno, reload_opnum[i],
6002 reload_when_needed[i],
6003 reload_mode[i]);
6004 reload_reg_rtx[i] = 0;
6005 reload_spill_index[i] = -1;
6006 remove_replacements (i);
6007 }
6008 }
6009 }
32131a9c
RK
6010
6011 /* If we found a better place to reload from,
6012 validate it in the same fashion, if it is a reload reg. */
6013 if (reload_override_in[r]
6014 && (GET_CODE (reload_override_in[r]) == REG
6015 || GET_CODE (reload_override_in[r]) == SUBREG))
6016 {
6017 int regno = true_regnum (reload_override_in[r]);
6018 if (spill_reg_order[regno] >= 0
546b63fb
RK
6019 && ! reload_reg_free_before_p (regno, reload_opnum[r],
6020 reload_when_needed[r]))
32131a9c
RK
6021 reload_override_in[r] = 0;
6022 }
6023 }
6024
6025 /* Now that reload_override_in is known valid,
6026 actually override reload_in. */
6027 for (j = 0; j < n_reloads; j++)
6028 if (reload_override_in[j])
6029 reload_in[j] = reload_override_in[j];
6030
6031 /* If this reload won't be done because it has been cancelled or is
6032 optional and not inherited, clear reload_reg_rtx so other
6033 routines (such as subst_reloads) don't get confused. */
6034 for (j = 0; j < n_reloads; j++)
be7ae2a4
RK
6035 if (reload_reg_rtx[j] != 0
6036 && ((reload_optional[j] && ! reload_inherited[j])
6037 || (reload_in[j] == 0 && reload_out[j] == 0
6038 && ! reload_secondary_p[j])))
6039 {
6040 int regno = true_regnum (reload_reg_rtx[j]);
6041
6042 if (spill_reg_order[regno] >= 0)
6043 clear_reload_reg_in_use (regno, reload_opnum[j],
6044 reload_when_needed[j], reload_mode[j]);
6045 reload_reg_rtx[j] = 0;
6046 }
32131a9c
RK
6047
6048 /* Record which pseudos and which spill regs have output reloads. */
6049 for (j = 0; j < n_reloads; j++)
6050 {
6051 register int r = reload_order[j];
6052
6053 i = reload_spill_index[r];
6054
e6e52be0 6055 /* I is nonneg if this reload uses a register.
32131a9c
RK
6056 If reload_reg_rtx[r] is 0, this is an optional reload
6057 that we opted to ignore. */
6058 if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG
6059 && reload_reg_rtx[r] != 0)
6060 {
6061 register int nregno = REGNO (reload_out[r]);
372e033b
RS
6062 int nr = 1;
6063
6064 if (nregno < FIRST_PSEUDO_REGISTER)
6065 nr = HARD_REGNO_NREGS (nregno, reload_mode[r]);
32131a9c
RK
6066
6067 while (--nr >= 0)
372e033b
RS
6068 reg_has_output_reload[nregno + nr] = 1;
6069
6070 if (i >= 0)
32131a9c 6071 {
e6e52be0 6072 nr = HARD_REGNO_NREGS (i, reload_mode[r]);
372e033b 6073 while (--nr >= 0)
e6e52be0 6074 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
32131a9c
RK
6075 }
6076
6077 if (reload_when_needed[r] != RELOAD_OTHER
546b63fb
RK
6078 && reload_when_needed[r] != RELOAD_FOR_OUTPUT
6079 && reload_when_needed[r] != RELOAD_FOR_INSN)
32131a9c
RK
6080 abort ();
6081 }
6082 }
6083}
6084\f
e9a25f70 6085/* If SMALL_REGISTER_CLASSES is non-zero, we may not have merged two
546b63fb
RK
6086 reloads of the same item for fear that we might not have enough reload
6087 registers. However, normally they will get the same reload register
6088 and hence actually need not be loaded twice.
6089
6090 Here we check for the most common case of this phenomenon: when we have
6091 a number of reloads for the same object, each of which were allocated
6092 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6093 reload, and is not modified in the insn itself. If we find such,
6094 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6095 This will not increase the number of spill registers needed and will
6096 prevent redundant code. */
6097
546b63fb
RK
6098static void
6099merge_assigned_reloads (insn)
6100 rtx insn;
6101{
6102 int i, j;
6103
6104 /* Scan all the reloads looking for ones that only load values and
6105 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6106 assigned and not modified by INSN. */
6107
6108 for (i = 0; i < n_reloads; i++)
6109 {
d668e863
R
6110 int conflicting_input = 0;
6111 int max_input_address_opnum = -1;
6112 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6113
546b63fb
RK
6114 if (reload_in[i] == 0 || reload_when_needed[i] == RELOAD_OTHER
6115 || reload_out[i] != 0 || reload_reg_rtx[i] == 0
6116 || reg_set_p (reload_reg_rtx[i], insn))
6117 continue;
6118
6119 /* Look at all other reloads. Ensure that the only use of this
6120 reload_reg_rtx is in a reload that just loads the same value
6121 as we do. Note that any secondary reloads must be of the identical
6122 class since the values, modes, and result registers are the
6123 same, so we need not do anything with any secondary reloads. */
6124
6125 for (j = 0; j < n_reloads; j++)
6126 {
6127 if (i == j || reload_reg_rtx[j] == 0
6128 || ! reg_overlap_mentioned_p (reload_reg_rtx[j],
6129 reload_reg_rtx[i]))
6130 continue;
6131
d668e863
R
6132 if (reload_when_needed[j] == RELOAD_FOR_INPUT_ADDRESS
6133 && reload_opnum[j] > max_input_address_opnum)
6134 max_input_address_opnum = reload_opnum[j];
6135
546b63fb 6136 /* If the reload regs aren't exactly the same (e.g, different modes)
d668e863
R
6137 or if the values are different, we can't merge this reload.
6138 But if it is an input reload, we might still merge
6139 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
546b63fb
RK
6140
6141 if (! rtx_equal_p (reload_reg_rtx[i], reload_reg_rtx[j])
6142 || reload_out[j] != 0 || reload_in[j] == 0
6143 || ! rtx_equal_p (reload_in[i], reload_in[j]))
d668e863
R
6144 {
6145 if (reload_when_needed[j] != RELOAD_FOR_INPUT
6146 || ((reload_when_needed[i] != RELOAD_FOR_INPUT_ADDRESS
6147 || reload_opnum[i] > reload_opnum[j])
6148 && reload_when_needed[i] != RELOAD_FOR_OTHER_ADDRESS))
6149 break;
6150 conflicting_input = 1;
6151 if (min_conflicting_input_opnum > reload_opnum[j])
6152 min_conflicting_input_opnum = reload_opnum[j];
6153 }
546b63fb
RK
6154 }
6155
6156 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6157 we, in fact, found any matching reloads. */
6158
d668e863
R
6159 if (j == n_reloads
6160 && max_input_address_opnum <= min_conflicting_input_opnum)
546b63fb
RK
6161 {
6162 for (j = 0; j < n_reloads; j++)
6163 if (i != j && reload_reg_rtx[j] != 0
d668e863
R
6164 && rtx_equal_p (reload_reg_rtx[i], reload_reg_rtx[j])
6165 && (! conflicting_input
6166 || reload_when_needed[j] == RELOAD_FOR_INPUT_ADDRESS
6167 || reload_when_needed[j] == RELOAD_FOR_OTHER_ADDRESS))
546b63fb
RK
6168 {
6169 reload_when_needed[i] = RELOAD_OTHER;
6170 reload_in[j] = 0;
efdb3590 6171 reload_spill_index[j] = -1;
546b63fb
RK
6172 transfer_replacements (i, j);
6173 }
6174
6175 /* If this is now RELOAD_OTHER, look for any reloads that load
6176 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6177 if they were for inputs, RELOAD_OTHER for outputs. Note that
6178 this test is equivalent to looking for reloads for this operand
6179 number. */
6180
6181 if (reload_when_needed[i] == RELOAD_OTHER)
6182 for (j = 0; j < n_reloads; j++)
6183 if (reload_in[j] != 0
6184 && reload_when_needed[i] != RELOAD_OTHER
6185 && reg_overlap_mentioned_for_reload_p (reload_in[j],
6186 reload_in[i]))
6187 reload_when_needed[j]
47c8cf91
ILT
6188 = ((reload_when_needed[i] == RELOAD_FOR_INPUT_ADDRESS
6189 || reload_when_needed[i] == RELOAD_FOR_INPADDR_ADDRESS)
6190 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
546b63fb
RK
6191 }
6192 }
6193}
e9a25f70 6194
546b63fb 6195\f
32131a9c
RK
6196/* Output insns to reload values in and out of the chosen reload regs. */
6197
6198static void
6199emit_reload_insns (insn)
6200 rtx insn;
6201{
6202 register int j;
546b63fb
RK
6203 rtx input_reload_insns[MAX_RECOG_OPERANDS];
6204 rtx other_input_address_reload_insns = 0;
6205 rtx other_input_reload_insns = 0;
6206 rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
47c8cf91 6207 rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
546b63fb
RK
6208 rtx output_reload_insns[MAX_RECOG_OPERANDS];
6209 rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
47c8cf91 6210 rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
546b63fb 6211 rtx operand_reload_insns = 0;
893bc853 6212 rtx other_operand_reload_insns = 0;
befa01b9 6213 rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
32131a9c 6214 rtx following_insn = NEXT_INSN (insn);
a8efe40d 6215 rtx before_insn = insn;
32131a9c
RK
6216 int special;
6217 /* Values to be put in spill_reg_store are put here first. */
6218 rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
e6e52be0
R
6219 HARD_REG_SET reg_reloaded_died;
6220
6221 CLEAR_HARD_REG_SET (reg_reloaded_died);
32131a9c 6222
546b63fb
RK
6223 for (j = 0; j < reload_n_operands; j++)
6224 input_reload_insns[j] = input_address_reload_insns[j]
47c8cf91 6225 = inpaddr_address_reload_insns[j]
befa01b9 6226 = output_reload_insns[j] = output_address_reload_insns[j]
47c8cf91 6227 = outaddr_address_reload_insns[j]
befa01b9 6228 = other_output_reload_insns[j] = 0;
546b63fb 6229
32131a9c
RK
6230 /* Now output the instructions to copy the data into and out of the
6231 reload registers. Do these in the order that the reloads were reported,
6232 since reloads of base and index registers precede reloads of operands
6233 and the operands may need the base and index registers reloaded. */
6234
6235 for (j = 0; j < n_reloads; j++)
6236 {
6237 register rtx old;
6238 rtx oldequiv_reg = 0;
80d92002 6239 rtx this_reload_insn = 0;
b60a8416 6240 int expect_occurrences = 1;
73b2ad9e
RK
6241
6242 if (reload_spill_index[j] >= 0)
6243 new_spill_reg_store[reload_spill_index[j]] = 0;
32131a9c
RK
6244
6245 old = reload_in[j];
6246 if (old != 0 && ! reload_inherited[j]
6247 && ! rtx_equal_p (reload_reg_rtx[j], old)
6248 && reload_reg_rtx[j] != 0)
6249 {
6250 register rtx reloadreg = reload_reg_rtx[j];
6251 rtx oldequiv = 0;
6252 enum machine_mode mode;
546b63fb 6253 rtx *where;
32131a9c
RK
6254
6255 /* Determine the mode to reload in.
6256 This is very tricky because we have three to choose from.
6257 There is the mode the insn operand wants (reload_inmode[J]).
6258 There is the mode of the reload register RELOADREG.
6259 There is the intrinsic mode of the operand, which we could find
6260 by stripping some SUBREGs.
6261 It turns out that RELOADREG's mode is irrelevant:
6262 we can change that arbitrarily.
6263
6264 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6265 then the reload reg may not support QImode moves, so use SImode.
6266 If foo is in memory due to spilling a pseudo reg, this is safe,
6267 because the QImode value is in the least significant part of a
6268 slot big enough for a SImode. If foo is some other sort of
6269 memory reference, then it is impossible to reload this case,
6270 so previous passes had better make sure this never happens.
6271
6272 Then consider a one-word union which has SImode and one of its
6273 members is a float, being fetched as (SUBREG:SF union:SI).
6274 We must fetch that as SFmode because we could be loading into
6275 a float-only register. In this case OLD's mode is correct.
6276
6277 Consider an immediate integer: it has VOIDmode. Here we need
6278 to get a mode from something else.
6279
6280 In some cases, there is a fourth mode, the operand's
6281 containing mode. If the insn specifies a containing mode for
6282 this operand, it overrides all others.
6283
6284 I am not sure whether the algorithm here is always right,
6285 but it does the right things in those cases. */
6286
6287 mode = GET_MODE (old);
6288 if (mode == VOIDmode)
6289 mode = reload_inmode[j];
32131a9c
RK
6290
6291#ifdef SECONDARY_INPUT_RELOAD_CLASS
6292 /* If we need a secondary register for this operation, see if
6293 the value is already in a register in that class. Don't
6294 do this if the secondary register will be used as a scratch
6295 register. */
6296
b80bba27
RK
6297 if (reload_secondary_in_reload[j] >= 0
6298 && reload_secondary_in_icode[j] == CODE_FOR_nothing
58b1581b 6299 && optimize)
32131a9c
RK
6300 oldequiv
6301 = find_equiv_reg (old, insn,
b80bba27 6302 reload_reg_class[reload_secondary_in_reload[j]],
fb3821f7 6303 -1, NULL_PTR, 0, mode);
32131a9c
RK
6304#endif
6305
6306 /* If reloading from memory, see if there is a register
6307 that already holds the same value. If so, reload from there.
6308 We can pass 0 as the reload_reg_p argument because
6309 any other reload has either already been emitted,
6310 in which case find_equiv_reg will see the reload-insn,
6311 or has yet to be emitted, in which case it doesn't matter
6312 because we will use this equiv reg right away. */
6313
58b1581b 6314 if (oldequiv == 0 && optimize
32131a9c
RK
6315 && (GET_CODE (old) == MEM
6316 || (GET_CODE (old) == REG
6317 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6318 && reg_renumber[REGNO (old)] < 0)))
546b63fb 6319 oldequiv = find_equiv_reg (old, insn, ALL_REGS,
fb3821f7 6320 -1, NULL_PTR, 0, mode);
32131a9c
RK
6321
6322 if (oldequiv)
6323 {
6324 int regno = true_regnum (oldequiv);
6325
6326 /* If OLDEQUIV is a spill register, don't use it for this
6327 if any other reload needs it at an earlier stage of this insn
a8fdc208 6328 or at this stage. */
32131a9c 6329 if (spill_reg_order[regno] >= 0
546b63fb
RK
6330 && (! reload_reg_free_p (regno, reload_opnum[j],
6331 reload_when_needed[j])
6332 || ! reload_reg_free_before_p (regno, reload_opnum[j],
32131a9c
RK
6333 reload_when_needed[j])))
6334 oldequiv = 0;
6335
6336 /* If OLDEQUIV is not a spill register,
6337 don't use it if any other reload wants it. */
6338 if (spill_reg_order[regno] < 0)
6339 {
6340 int k;
6341 for (k = 0; k < n_reloads; k++)
6342 if (reload_reg_rtx[k] != 0 && k != j
bfa30b22
RK
6343 && reg_overlap_mentioned_for_reload_p (reload_reg_rtx[k],
6344 oldequiv))
32131a9c
RK
6345 {
6346 oldequiv = 0;
6347 break;
6348 }
6349 }
546b63fb
RK
6350
6351 /* If it is no cheaper to copy from OLDEQUIV into the
6352 reload register than it would be to move from memory,
6353 don't use it. Likewise, if we need a secondary register
6354 or memory. */
6355
6356 if (oldequiv != 0
6357 && ((REGNO_REG_CLASS (regno) != reload_reg_class[j]
6358 && (REGISTER_MOVE_COST (REGNO_REG_CLASS (regno),
6359 reload_reg_class[j])
cbd5b9a2
KR
6360 >= MEMORY_MOVE_COST (mode, REGNO_REG_CLASS (regno),
6361 1)))
546b63fb
RK
6362#ifdef SECONDARY_INPUT_RELOAD_CLASS
6363 || (SECONDARY_INPUT_RELOAD_CLASS (reload_reg_class[j],
6364 mode, oldequiv)
6365 != NO_REGS)
6366#endif
6367#ifdef SECONDARY_MEMORY_NEEDED
6368 || SECONDARY_MEMORY_NEEDED (reload_reg_class[j],
6369 REGNO_REG_CLASS (regno),
6370 mode)
6371#endif
6372 ))
6373 oldequiv = 0;
32131a9c
RK
6374 }
6375
6376 if (oldequiv == 0)
6377 oldequiv = old;
6378 else if (GET_CODE (oldequiv) == REG)
6379 oldequiv_reg = oldequiv;
6380 else if (GET_CODE (oldequiv) == SUBREG)
6381 oldequiv_reg = SUBREG_REG (oldequiv);
6382
76182796
RK
6383 /* If we are reloading from a register that was recently stored in
6384 with an output-reload, see if we can prove there was
6385 actually no need to store the old value in it. */
6386
6387 if (optimize && GET_CODE (oldequiv) == REG
6388 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
e6e52be0
R
6389 && spill_reg_store[REGNO (oldequiv)]
6390 && GET_CODE (old) == REG && dead_or_set_p (insn, old)
76182796 6391 /* This is unsafe if operand occurs more than once in current
b87b7ecd 6392 insn. Perhaps some occurrences weren't reloaded. */
e6e52be0
R
6393 && count_occurrences (PATTERN (insn), old) == 1)
6394 delete_output_reload (insn, j, spill_reg_store[REGNO (oldequiv)]);
76182796 6395
32131a9c 6396 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
3abe6f90
RK
6397 then load RELOADREG from OLDEQUIV. Note that we cannot use
6398 gen_lowpart_common since it can do the wrong thing when
6399 RELOADREG has a multi-word mode. Note that RELOADREG
6400 must always be a REG here. */
32131a9c
RK
6401
6402 if (GET_MODE (reloadreg) != mode)
38a448ca 6403 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
32131a9c
RK
6404 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6405 oldequiv = SUBREG_REG (oldequiv);
6406 if (GET_MODE (oldequiv) != VOIDmode
6407 && mode != GET_MODE (oldequiv))
38a448ca 6408 oldequiv = gen_rtx_SUBREG (mode, oldequiv, 0);
32131a9c 6409
546b63fb 6410 /* Switch to the right place to emit the reload insns. */
32131a9c
RK
6411 switch (reload_when_needed[j])
6412 {
32131a9c 6413 case RELOAD_OTHER:
546b63fb
RK
6414 where = &other_input_reload_insns;
6415 break;
6416 case RELOAD_FOR_INPUT:
6417 where = &input_reload_insns[reload_opnum[j]];
32131a9c 6418 break;
546b63fb
RK
6419 case RELOAD_FOR_INPUT_ADDRESS:
6420 where = &input_address_reload_insns[reload_opnum[j]];
32131a9c 6421 break;
47c8cf91
ILT
6422 case RELOAD_FOR_INPADDR_ADDRESS:
6423 where = &inpaddr_address_reload_insns[reload_opnum[j]];
6424 break;
546b63fb
RK
6425 case RELOAD_FOR_OUTPUT_ADDRESS:
6426 where = &output_address_reload_insns[reload_opnum[j]];
32131a9c 6427 break;
47c8cf91
ILT
6428 case RELOAD_FOR_OUTADDR_ADDRESS:
6429 where = &outaddr_address_reload_insns[reload_opnum[j]];
6430 break;
32131a9c 6431 case RELOAD_FOR_OPERAND_ADDRESS:
546b63fb
RK
6432 where = &operand_reload_insns;
6433 break;
893bc853
RK
6434 case RELOAD_FOR_OPADDR_ADDR:
6435 where = &other_operand_reload_insns;
6436 break;
546b63fb
RK
6437 case RELOAD_FOR_OTHER_ADDRESS:
6438 where = &other_input_address_reload_insns;
6439 break;
6440 default:
6441 abort ();
32131a9c
RK
6442 }
6443
546b63fb 6444 push_to_sequence (*where);
32131a9c
RK
6445 special = 0;
6446
6447 /* Auto-increment addresses must be reloaded in a special way. */
6448 if (GET_CODE (oldequiv) == POST_INC
6449 || GET_CODE (oldequiv) == POST_DEC
6450 || GET_CODE (oldequiv) == PRE_INC
6451 || GET_CODE (oldequiv) == PRE_DEC)
6452 {
6453 /* We are not going to bother supporting the case where a
6454 incremented register can't be copied directly from
6455 OLDEQUIV since this seems highly unlikely. */
b80bba27 6456 if (reload_secondary_in_reload[j] >= 0)
32131a9c
RK
6457 abort ();
6458 /* Prevent normal processing of this reload. */
6459 special = 1;
6460 /* Output a special code sequence for this case. */
546b63fb 6461 inc_for_reload (reloadreg, oldequiv, reload_inc[j]);
32131a9c
RK
6462 }
6463
6464 /* If we are reloading a pseudo-register that was set by the previous
6465 insn, see if we can get rid of that pseudo-register entirely
6466 by redirecting the previous insn into our reload register. */
6467
6468 else if (optimize && GET_CODE (old) == REG
6469 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6470 && dead_or_set_p (insn, old)
6471 /* This is unsafe if some other reload
6472 uses the same reg first. */
546b63fb
RK
6473 && reload_reg_free_before_p (REGNO (reloadreg),
6474 reload_opnum[j],
6475 reload_when_needed[j]))
32131a9c
RK
6476 {
6477 rtx temp = PREV_INSN (insn);
6478 while (temp && GET_CODE (temp) == NOTE)
6479 temp = PREV_INSN (temp);
6480 if (temp
6481 && GET_CODE (temp) == INSN
6482 && GET_CODE (PATTERN (temp)) == SET
6483 && SET_DEST (PATTERN (temp)) == old
6484 /* Make sure we can access insn_operand_constraint. */
6485 && asm_noperands (PATTERN (temp)) < 0
6486 /* This is unsafe if prev insn rejects our reload reg. */
6487 && constraint_accepts_reg_p (insn_operand_constraint[recog_memoized (temp)][0],
6488 reloadreg)
6489 /* This is unsafe if operand occurs more than once in current
6490 insn. Perhaps some occurrences aren't reloaded. */
6491 && count_occurrences (PATTERN (insn), old) == 1
6492 /* Don't risk splitting a matching pair of operands. */
6493 && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
6494 {
6495 /* Store into the reload register instead of the pseudo. */
6496 SET_DEST (PATTERN (temp)) = reloadreg;
6497 /* If these are the only uses of the pseudo reg,
6498 pretend for GDB it lives in the reload reg we used. */
b1f21e0a
MM
6499 if (REG_N_DEATHS (REGNO (old)) == 1
6500 && REG_N_SETS (REGNO (old)) == 1)
32131a9c
RK
6501 {
6502 reg_renumber[REGNO (old)] = REGNO (reload_reg_rtx[j]);
6503 alter_reg (REGNO (old), -1);
6504 }
6505 special = 1;
6506 }
6507 }
6508
546b63fb
RK
6509 /* We can't do that, so output an insn to load RELOADREG. */
6510
32131a9c
RK
6511 if (! special)
6512 {
6513#ifdef SECONDARY_INPUT_RELOAD_CLASS
6514 rtx second_reload_reg = 0;
6515 enum insn_code icode;
6516
6517 /* If we have a secondary reload, pick up the secondary register
d445b551
RK
6518 and icode, if any. If OLDEQUIV and OLD are different or
6519 if this is an in-out reload, recompute whether or not we
6520 still need a secondary register and what the icode should
6521 be. If we still need a secondary register and the class or
6522 icode is different, go back to reloading from OLD if using
6523 OLDEQUIV means that we got the wrong type of register. We
6524 cannot have different class or icode due to an in-out reload
6525 because we don't make such reloads when both the input and
6526 output need secondary reload registers. */
32131a9c 6527
b80bba27 6528 if (reload_secondary_in_reload[j] >= 0)
32131a9c 6529 {
b80bba27 6530 int secondary_reload = reload_secondary_in_reload[j];
1554c2c6
RK
6531 rtx real_oldequiv = oldequiv;
6532 rtx real_old = old;
6533
6534 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6535 and similarly for OLD.
b80bba27 6536 See comments in get_secondary_reload in reload.c. */
1554c2c6
RK
6537 if (GET_CODE (oldequiv) == REG
6538 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
6539 && reg_equiv_mem[REGNO (oldequiv)] != 0)
6540 real_oldequiv = reg_equiv_mem[REGNO (oldequiv)];
6541
6542 if (GET_CODE (old) == REG
6543 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6544 && reg_equiv_mem[REGNO (old)] != 0)
6545 real_old = reg_equiv_mem[REGNO (old)];
6546
32131a9c 6547 second_reload_reg = reload_reg_rtx[secondary_reload];
b80bba27 6548 icode = reload_secondary_in_icode[j];
32131a9c 6549
d445b551
RK
6550 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6551 || (reload_in[j] != 0 && reload_out[j] != 0))
32131a9c
RK
6552 {
6553 enum reg_class new_class
6554 = SECONDARY_INPUT_RELOAD_CLASS (reload_reg_class[j],
1554c2c6 6555 mode, real_oldequiv);
32131a9c
RK
6556
6557 if (new_class == NO_REGS)
6558 second_reload_reg = 0;
6559 else
6560 {
6561 enum insn_code new_icode;
6562 enum machine_mode new_mode;
6563
6564 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
6565 REGNO (second_reload_reg)))
1554c2c6 6566 oldequiv = old, real_oldequiv = real_old;
32131a9c
RK
6567 else
6568 {
6569 new_icode = reload_in_optab[(int) mode];
6570 if (new_icode != CODE_FOR_nothing
6571 && ((insn_operand_predicate[(int) new_icode][0]
a8fdc208 6572 && ! ((*insn_operand_predicate[(int) new_icode][0])
32131a9c 6573 (reloadreg, mode)))
a8fdc208
RS
6574 || (insn_operand_predicate[(int) new_icode][1]
6575 && ! ((*insn_operand_predicate[(int) new_icode][1])
1554c2c6 6576 (real_oldequiv, mode)))))
32131a9c
RK
6577 new_icode = CODE_FOR_nothing;
6578
6579 if (new_icode == CODE_FOR_nothing)
6580 new_mode = mode;
6581 else
196ddf8a 6582 new_mode = insn_operand_mode[(int) new_icode][2];
32131a9c
RK
6583
6584 if (GET_MODE (second_reload_reg) != new_mode)
6585 {
6586 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6587 new_mode))
1554c2c6 6588 oldequiv = old, real_oldequiv = real_old;
32131a9c
RK
6589 else
6590 second_reload_reg
38a448ca
RH
6591 = gen_rtx_REG (new_mode,
6592 REGNO (second_reload_reg));
32131a9c
RK
6593 }
6594 }
6595 }
6596 }
6597
6598 /* If we still need a secondary reload register, check
6599 to see if it is being used as a scratch or intermediate
1554c2c6
RK
6600 register and generate code appropriately. If we need
6601 a scratch register, use REAL_OLDEQUIV since the form of
6602 the insn may depend on the actual address if it is
6603 a MEM. */
32131a9c
RK
6604
6605 if (second_reload_reg)
6606 {
6607 if (icode != CODE_FOR_nothing)
6608 {
5e03c156
RK
6609 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6610 second_reload_reg));
32131a9c
RK
6611 special = 1;
6612 }
6613 else
6614 {
6615 /* See if we need a scratch register to load the
6616 intermediate register (a tertiary reload). */
6617 enum insn_code tertiary_icode
b80bba27 6618 = reload_secondary_in_icode[secondary_reload];
32131a9c
RK
6619
6620 if (tertiary_icode != CODE_FOR_nothing)
6621 {
6622 rtx third_reload_reg
b80bba27 6623 = reload_reg_rtx[reload_secondary_in_reload[secondary_reload]];
32131a9c 6624
546b63fb
RK
6625 emit_insn ((GEN_FCN (tertiary_icode)
6626 (second_reload_reg, real_oldequiv,
6627 third_reload_reg)));
32131a9c
RK
6628 }
6629 else
5e03c156
RK
6630 gen_reload (second_reload_reg, oldequiv,
6631 reload_opnum[j],
6632 reload_when_needed[j]);
546b63fb
RK
6633
6634 oldequiv = second_reload_reg;
32131a9c
RK
6635 }
6636 }
6637 }
6638#endif
6639
2d182c6f 6640 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
5e03c156
RK
6641 gen_reload (reloadreg, oldequiv, reload_opnum[j],
6642 reload_when_needed[j]);
32131a9c
RK
6643
6644#if defined(SECONDARY_INPUT_RELOAD_CLASS) && defined(PRESERVE_DEATH_INFO_REGNO_P)
6645 /* We may have to make a REG_DEAD note for the secondary reload
6646 register in the insns we just made. Find the last insn that
6647 mentioned the register. */
6648 if (! special && second_reload_reg
6649 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reload_reg)))
6650 {
6651 rtx prev;
6652
546b63fb 6653 for (prev = get_last_insn (); prev;
32131a9c
RK
6654 prev = PREV_INSN (prev))
6655 if (GET_RTX_CLASS (GET_CODE (prev) == 'i')
bfa30b22
RK
6656 && reg_overlap_mentioned_for_reload_p (second_reload_reg,
6657 PATTERN (prev)))
32131a9c 6658 {
38a448ca
RH
6659 REG_NOTES (prev) = gen_rtx_EXPR_LIST (REG_DEAD,
6660 second_reload_reg,
6661 REG_NOTES (prev));
32131a9c
RK
6662 break;
6663 }
6664 }
6665#endif
6666 }
6667
80d92002 6668 this_reload_insn = get_last_insn ();
546b63fb
RK
6669 /* End this sequence. */
6670 *where = get_insns ();
6671 end_sequence ();
32131a9c
RK
6672 }
6673
b60a8416
R
6674 /* When inheriting a wider reload, we have a MEM in reload_in[j],
6675 e.g. inheriting a SImode output reload for
6676 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
6677 if (optimize && reload_inherited[j] && reload_in[j]
6678 && GET_CODE (reload_in[j]) == MEM
6679 && reload_spill_index[j] >= 0
6680 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
6681 {
6682 expect_occurrences
6683 = count_occurrences (PATTERN (insn), reload_in[j]) == 1 ? 0 : -1;
6684 reload_in[j]
6685 = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
6686 }
32131a9c
RK
6687 /* Add a note saying the input reload reg
6688 dies in this insn, if anyone cares. */
6689#ifdef PRESERVE_DEATH_INFO_REGNO_P
6690 if (old != 0
6691 && reload_reg_rtx[j] != old
6692 && reload_reg_rtx[j] != 0
6693 && reload_out[j] == 0
6694 && ! reload_inherited[j]
6695 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j])))
6696 {
6697 register rtx reloadreg = reload_reg_rtx[j];
6698
a8fdc208 6699#if 0
32131a9c
RK
6700 /* We can't abort here because we need to support this for sched.c.
6701 It's not terrible to miss a REG_DEAD note, but we should try
6702 to figure out how to do this correctly. */
6703 /* The code below is incorrect for address-only reloads. */
6704 if (reload_when_needed[j] != RELOAD_OTHER
6705 && reload_when_needed[j] != RELOAD_FOR_INPUT)
6706 abort ();
6707#endif
6708
6709 /* Add a death note to this insn, for an input reload. */
6710
6711 if ((reload_when_needed[j] == RELOAD_OTHER
6712 || reload_when_needed[j] == RELOAD_FOR_INPUT)
6713 && ! dead_or_set_p (insn, reloadreg))
6714 REG_NOTES (insn)
38a448ca
RH
6715 = gen_rtx_EXPR_LIST (REG_DEAD,
6716 reloadreg, REG_NOTES (insn));
32131a9c
RK
6717 }
6718
6719 /* When we inherit a reload, the last marked death of the reload reg
6720 may no longer really be a death. */
6721 if (reload_reg_rtx[j] != 0
6722 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j]))
6723 && reload_inherited[j])
6724 {
6725 /* Handle inheriting an output reload.
6726 Remove the death note from the output reload insn. */
6727 if (reload_spill_index[j] >= 0
6728 && GET_CODE (reload_in[j]) == REG
6729 && spill_reg_store[reload_spill_index[j]] != 0
6730 && find_regno_note (spill_reg_store[reload_spill_index[j]],
6731 REG_DEAD, REGNO (reload_reg_rtx[j])))
6732 remove_death (REGNO (reload_reg_rtx[j]),
6733 spill_reg_store[reload_spill_index[j]]);
6734 /* Likewise for input reloads that were inherited. */
6735 else if (reload_spill_index[j] >= 0
6736 && GET_CODE (reload_in[j]) == REG
6737 && spill_reg_store[reload_spill_index[j]] == 0
6738 && reload_inheritance_insn[j] != 0
a8fdc208 6739 && find_regno_note (reload_inheritance_insn[j], REG_DEAD,
32131a9c
RK
6740 REGNO (reload_reg_rtx[j])))
6741 remove_death (REGNO (reload_reg_rtx[j]),
6742 reload_inheritance_insn[j]);
6743 else
6744 {
6745 rtx prev;
6746
6747 /* We got this register from find_equiv_reg.
6748 Search back for its last death note and get rid of it.
6749 But don't search back too far.
6750 Don't go past a place where this reg is set,
6751 since a death note before that remains valid. */
6752 for (prev = PREV_INSN (insn);
6753 prev && GET_CODE (prev) != CODE_LABEL;
6754 prev = PREV_INSN (prev))
6755 if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
6756 && dead_or_set_p (prev, reload_reg_rtx[j]))
6757 {
6758 if (find_regno_note (prev, REG_DEAD,
6759 REGNO (reload_reg_rtx[j])))
6760 remove_death (REGNO (reload_reg_rtx[j]), prev);
6761 break;
6762 }
6763 }
6764 }
6765
6766 /* We might have used find_equiv_reg above to choose an alternate
6767 place from which to reload. If so, and it died, we need to remove
6768 that death and move it to one of the insns we just made. */
6769
6770 if (oldequiv_reg != 0
6771 && PRESERVE_DEATH_INFO_REGNO_P (true_regnum (oldequiv_reg)))
6772 {
6773 rtx prev, prev1;
6774
6775 for (prev = PREV_INSN (insn); prev && GET_CODE (prev) != CODE_LABEL;
6776 prev = PREV_INSN (prev))
6777 if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
6778 && dead_or_set_p (prev, oldequiv_reg))
6779 {
6780 if (find_regno_note (prev, REG_DEAD, REGNO (oldequiv_reg)))
6781 {
6782 for (prev1 = this_reload_insn;
6783 prev1; prev1 = PREV_INSN (prev1))
6784 if (GET_RTX_CLASS (GET_CODE (prev1) == 'i')
bfa30b22
RK
6785 && reg_overlap_mentioned_for_reload_p (oldequiv_reg,
6786 PATTERN (prev1)))
32131a9c 6787 {
38a448ca
RH
6788 REG_NOTES (prev1) = gen_rtx_EXPR_LIST (REG_DEAD,
6789 oldequiv_reg,
6790 REG_NOTES (prev1));
32131a9c
RK
6791 break;
6792 }
6793 remove_death (REGNO (oldequiv_reg), prev);
6794 }
6795 break;
6796 }
6797 }
6798#endif
6799
6800 /* If we are reloading a register that was recently stored in with an
6801 output-reload, see if we can prove there was
6802 actually no need to store the old value in it. */
6803
6804 if (optimize && reload_inherited[j] && reload_spill_index[j] >= 0
546b63fb 6805 && reload_in[j] != 0
32131a9c
RK
6806 && GET_CODE (reload_in[j]) == REG
6807#if 0
6808 /* There doesn't seem to be any reason to restrict this to pseudos
6809 and doing so loses in the case where we are copying from a
6810 register of the wrong class. */
6811 && REGNO (reload_in[j]) >= FIRST_PSEUDO_REGISTER
6812#endif
6813 && spill_reg_store[reload_spill_index[j]] != 0
546b63fb 6814 /* This is unsafe if some other reload uses the same reg first. */
e6e52be0 6815 && reload_reg_free_before_p (reload_spill_index[j],
546b63fb 6816 reload_opnum[j], reload_when_needed[j])
32131a9c
RK
6817 && dead_or_set_p (insn, reload_in[j])
6818 /* This is unsafe if operand occurs more than once in current
6819 insn. Perhaps some occurrences weren't reloaded. */
b60a8416
R
6820 && (count_occurrences (PATTERN (insn), reload_in[j])
6821 == expect_occurrences))
32131a9c
RK
6822 delete_output_reload (insn, j,
6823 spill_reg_store[reload_spill_index[j]]);
6824
6825 /* Input-reloading is done. Now do output-reloading,
6826 storing the value from the reload-register after the main insn
6827 if reload_out[j] is nonzero.
6828
6829 ??? At some point we need to support handling output reloads of
6830 JUMP_INSNs or insns that set cc0. */
6831 old = reload_out[j];
6832 if (old != 0
6833 && reload_reg_rtx[j] != old
6834 && reload_reg_rtx[j] != 0)
6835 {
6836 register rtx reloadreg = reload_reg_rtx[j];
29a82058 6837#ifdef SECONDARY_OUTPUT_RELOAD_CLASS
32131a9c 6838 register rtx second_reloadreg = 0;
29a82058 6839#endif
32131a9c
RK
6840 rtx note, p;
6841 enum machine_mode mode;
6842 int special = 0;
6843
6844 /* An output operand that dies right away does need a reload,
6845 but need not be copied from it. Show the new location in the
6846 REG_UNUSED note. */
6847 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
6848 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
6849 {
6850 XEXP (note, 0) = reload_reg_rtx[j];
6851 continue;
6852 }
a7911cd2
RK
6853 /* Likewise for a SUBREG of an operand that dies. */
6854 else if (GET_CODE (old) == SUBREG
6855 && GET_CODE (SUBREG_REG (old)) == REG
6856 && 0 != (note = find_reg_note (insn, REG_UNUSED,
6857 SUBREG_REG (old))))
6858 {
6859 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
6860 reload_reg_rtx[j]);
6861 continue;
6862 }
32131a9c
RK
6863 else if (GET_CODE (old) == SCRATCH)
6864 /* If we aren't optimizing, there won't be a REG_UNUSED note,
6865 but we don't want to make an output reload. */
6866 continue;
6867
6868#if 0
6869 /* Strip off of OLD any size-increasing SUBREGs such as
6870 (SUBREG:SI foo:QI 0). */
6871
6872 while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
6873 && (GET_MODE_SIZE (GET_MODE (old))
6874 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
6875 old = SUBREG_REG (old);
6876#endif
6877
6878 /* If is a JUMP_INSN, we can't support output reloads yet. */
6879 if (GET_CODE (insn) == JUMP_INSN)
6880 abort ();
6881
d7e0324f 6882 if (reload_when_needed[j] == RELOAD_OTHER)
5ca582cf 6883 start_sequence ();
d7e0324f
RK
6884 else
6885 push_to_sequence (output_reload_insns[reload_opnum[j]]);
546b63fb 6886
32131a9c
RK
6887 /* Determine the mode to reload in.
6888 See comments above (for input reloading). */
6889
6890 mode = GET_MODE (old);
6891 if (mode == VOIDmode)
79a365a7
RS
6892 {
6893 /* VOIDmode should never happen for an output. */
6894 if (asm_noperands (PATTERN (insn)) < 0)
6895 /* It's the compiler's fault. */
a89b2cc4 6896 fatal_insn ("VOIDmode on an output", insn);
79a365a7
RS
6897 error_for_asm (insn, "output operand is constant in `asm'");
6898 /* Prevent crash--use something we know is valid. */
6899 mode = word_mode;
38a448ca 6900 old = gen_rtx_REG (mode, REGNO (reloadreg));
79a365a7 6901 }
32131a9c 6902
32131a9c 6903 if (GET_MODE (reloadreg) != mode)
38a448ca 6904 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
32131a9c
RK
6905
6906#ifdef SECONDARY_OUTPUT_RELOAD_CLASS
6907
6908 /* If we need two reload regs, set RELOADREG to the intermediate
5e03c156 6909 one, since it will be stored into OLD. We might need a secondary
32131a9c
RK
6910 register only for an input reload, so check again here. */
6911
b80bba27 6912 if (reload_secondary_out_reload[j] >= 0)
32131a9c 6913 {
1554c2c6 6914 rtx real_old = old;
32131a9c 6915
1554c2c6
RK
6916 if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
6917 && reg_equiv_mem[REGNO (old)] != 0)
6918 real_old = reg_equiv_mem[REGNO (old)];
32131a9c 6919
1554c2c6
RK
6920 if((SECONDARY_OUTPUT_RELOAD_CLASS (reload_reg_class[j],
6921 mode, real_old)
6922 != NO_REGS))
6923 {
6924 second_reloadreg = reloadreg;
b80bba27 6925 reloadreg = reload_reg_rtx[reload_secondary_out_reload[j]];
32131a9c 6926
1554c2c6
RK
6927 /* See if RELOADREG is to be used as a scratch register
6928 or as an intermediate register. */
b80bba27 6929 if (reload_secondary_out_icode[j] != CODE_FOR_nothing)
32131a9c 6930 {
b80bba27 6931 emit_insn ((GEN_FCN (reload_secondary_out_icode[j])
546b63fb 6932 (real_old, second_reloadreg, reloadreg)));
1554c2c6 6933 special = 1;
32131a9c
RK
6934 }
6935 else
1554c2c6
RK
6936 {
6937 /* See if we need both a scratch and intermediate reload
6938 register. */
5e03c156 6939
b80bba27 6940 int secondary_reload = reload_secondary_out_reload[j];
1554c2c6 6941 enum insn_code tertiary_icode
b80bba27 6942 = reload_secondary_out_icode[secondary_reload];
32131a9c 6943
1554c2c6 6944 if (GET_MODE (reloadreg) != mode)
38a448ca 6945 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
1554c2c6
RK
6946
6947 if (tertiary_icode != CODE_FOR_nothing)
6948 {
6949 rtx third_reloadreg
b80bba27 6950 = reload_reg_rtx[reload_secondary_out_reload[secondary_reload]];
a7911cd2 6951 rtx tem;
5e03c156
RK
6952
6953 /* Copy primary reload reg to secondary reload reg.
6954 (Note that these have been swapped above, then
6955 secondary reload reg to OLD using our insn. */
6956
a7911cd2
RK
6957 /* If REAL_OLD is a paradoxical SUBREG, remove it
6958 and try to put the opposite SUBREG on
6959 RELOADREG. */
6960 if (GET_CODE (real_old) == SUBREG
6961 && (GET_MODE_SIZE (GET_MODE (real_old))
6962 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
6963 && 0 != (tem = gen_lowpart_common
6964 (GET_MODE (SUBREG_REG (real_old)),
6965 reloadreg)))
6966 real_old = SUBREG_REG (real_old), reloadreg = tem;
6967
5e03c156
RK
6968 gen_reload (reloadreg, second_reloadreg,
6969 reload_opnum[j], reload_when_needed[j]);
6970 emit_insn ((GEN_FCN (tertiary_icode)
6971 (real_old, reloadreg, third_reloadreg)));
6972 special = 1;
9ad5f9f6 6973 }
5e03c156 6974
1554c2c6 6975 else
5e03c156
RK
6976 /* Copy between the reload regs here and then to
6977 OUT later. */
1554c2c6 6978
5e03c156
RK
6979 gen_reload (reloadreg, second_reloadreg,
6980 reload_opnum[j], reload_when_needed[j]);
1554c2c6 6981 }
32131a9c
RK
6982 }
6983 }
6984#endif
6985
6986 /* Output the last reload insn. */
6987 if (! special)
d7c2e385
L
6988 {
6989 rtx set;
6990
6991 /* Don't output the last reload if OLD is not the dest of
6992 INSN and is in the src and is clobbered by INSN. */
6993 if (! flag_expensive_optimizations
6994 || GET_CODE (old) != REG
6995 || !(set = single_set (insn))
6996 || rtx_equal_p (old, SET_DEST (set))
6997 || !reg_mentioned_p (old, SET_SRC (set))
6998 || !regno_clobbered_p (REGNO (old), insn))
6999 gen_reload (old, reloadreg, reload_opnum[j],
7000 reload_when_needed[j]);
7001 }
32131a9c
RK
7002
7003#ifdef PRESERVE_DEATH_INFO_REGNO_P
7004 /* If final will look at death notes for this reg,
7005 put one on the last output-reload insn to use it. Similarly
7006 for any secondary register. */
7007 if (PRESERVE_DEATH_INFO_REGNO_P (REGNO (reloadreg)))
546b63fb 7008 for (p = get_last_insn (); p; p = PREV_INSN (p))
32131a9c 7009 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
bfa30b22
RK
7010 && reg_overlap_mentioned_for_reload_p (reloadreg,
7011 PATTERN (p)))
38a448ca
RH
7012 REG_NOTES (p) = gen_rtx_EXPR_LIST (REG_DEAD,
7013 reloadreg, REG_NOTES (p));
32131a9c
RK
7014
7015#ifdef SECONDARY_OUTPUT_RELOAD_CLASS
32051ff5 7016 if (! special && second_reloadreg
32131a9c 7017 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reloadreg)))
546b63fb 7018 for (p = get_last_insn (); p; p = PREV_INSN (p))
32131a9c 7019 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
bfa30b22
RK
7020 && reg_overlap_mentioned_for_reload_p (second_reloadreg,
7021 PATTERN (p)))
38a448ca
RH
7022 REG_NOTES (p) = gen_rtx_EXPR_LIST (REG_DEAD,
7023 second_reloadreg,
7024 REG_NOTES (p));
32131a9c
RK
7025#endif
7026#endif
7027 /* Look at all insns we emitted, just to be safe. */
546b63fb 7028 for (p = get_insns (); p; p = NEXT_INSN (p))
32131a9c
RK
7029 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
7030 {
e6e52be0
R
7031 rtx pat = PATTERN (p);
7032
32131a9c
RK
7033 /* If this output reload doesn't come from a spill reg,
7034 clear any memory of reloaded copies of the pseudo reg.
7035 If this output reload comes from a spill reg,
7036 reg_has_output_reload will make this do nothing. */
e6e52be0
R
7037 note_stores (pat, forget_old_reloads_1);
7038
7039 if (reg_mentioned_p (reload_reg_rtx[j], pat))
7040 {
7041 if (reload_spill_index[j] < 0
7042 && GET_CODE (pat) == SET
7043 && SET_SRC (pat) == reload_reg_rtx[j])
7044 {
7045 int src = REGNO (SET_SRC (pat));
32131a9c 7046
e6e52be0
R
7047 reload_spill_index[j] = src;
7048 SET_HARD_REG_BIT (reg_is_output_reload, src);
7049 if (find_regno_note (insn, REG_DEAD, src))
7050 SET_HARD_REG_BIT (reg_reloaded_died, src);
7051 }
7052 if (reload_spill_index[j] >= 0)
7053 new_spill_reg_store[reload_spill_index[j]] = p;
7054 }
32131a9c
RK
7055 }
7056
d7e0324f 7057 if (reload_when_needed[j] == RELOAD_OTHER)
befa01b9
JW
7058 {
7059 emit_insns (other_output_reload_insns[reload_opnum[j]]);
7060 other_output_reload_insns[reload_opnum[j]] = get_insns ();
7061 }
7062 else
7063 output_reload_insns[reload_opnum[j]] = get_insns ();
d7e0324f 7064
546b63fb 7065 end_sequence ();
32131a9c 7066 }
32131a9c
RK
7067 }
7068
546b63fb
RK
7069 /* Now write all the insns we made for reloads in the order expected by
7070 the allocation functions. Prior to the insn being reloaded, we write
7071 the following reloads:
7072
7073 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7074
2edc8d65 7075 RELOAD_OTHER reloads.
546b63fb 7076
47c8cf91
ILT
7077 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7078 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7079 RELOAD_FOR_INPUT reload for the operand.
546b63fb 7080
893bc853
RK
7081 RELOAD_FOR_OPADDR_ADDRS reloads.
7082
546b63fb
RK
7083 RELOAD_FOR_OPERAND_ADDRESS reloads.
7084
7085 After the insn being reloaded, we write the following:
7086
47c8cf91
ILT
7087 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7088 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7089 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7090 reloads for the operand. The RELOAD_OTHER output reloads are
7091 output in descending order by reload number. */
546b63fb
RK
7092
7093 emit_insns_before (other_input_address_reload_insns, before_insn);
7094 emit_insns_before (other_input_reload_insns, before_insn);
7095
7096 for (j = 0; j < reload_n_operands; j++)
7097 {
47c8cf91 7098 emit_insns_before (inpaddr_address_reload_insns[j], before_insn);
546b63fb
RK
7099 emit_insns_before (input_address_reload_insns[j], before_insn);
7100 emit_insns_before (input_reload_insns[j], before_insn);
7101 }
7102
893bc853 7103 emit_insns_before (other_operand_reload_insns, before_insn);
546b63fb
RK
7104 emit_insns_before (operand_reload_insns, before_insn);
7105
7106 for (j = 0; j < reload_n_operands; j++)
7107 {
47c8cf91 7108 emit_insns_before (outaddr_address_reload_insns[j], following_insn);
546b63fb
RK
7109 emit_insns_before (output_address_reload_insns[j], following_insn);
7110 emit_insns_before (output_reload_insns[j], following_insn);
befa01b9 7111 emit_insns_before (other_output_reload_insns[j], following_insn);
546b63fb
RK
7112 }
7113
32131a9c
RK
7114 /* Move death notes from INSN
7115 to output-operand-address and output reload insns. */
7116#ifdef PRESERVE_DEATH_INFO_REGNO_P
7117 {
7118 rtx insn1;
7119 /* Loop over those insns, last ones first. */
7120 for (insn1 = PREV_INSN (following_insn); insn1 != insn;
7121 insn1 = PREV_INSN (insn1))
7122 if (GET_CODE (insn1) == INSN && GET_CODE (PATTERN (insn1)) == SET)
7123 {
7124 rtx source = SET_SRC (PATTERN (insn1));
7125 rtx dest = SET_DEST (PATTERN (insn1));
7126
7127 /* The note we will examine next. */
7128 rtx reg_notes = REG_NOTES (insn);
7129 /* The place that pointed to this note. */
7130 rtx *prev_reg_note = &REG_NOTES (insn);
7131
7132 /* If the note is for something used in the source of this
7133 reload insn, or in the output address, move the note. */
7134 while (reg_notes)
7135 {
7136 rtx next_reg_notes = XEXP (reg_notes, 1);
7137 if (REG_NOTE_KIND (reg_notes) == REG_DEAD
7138 && GET_CODE (XEXP (reg_notes, 0)) == REG
7139 && ((GET_CODE (dest) != REG
bfa30b22
RK
7140 && reg_overlap_mentioned_for_reload_p (XEXP (reg_notes, 0),
7141 dest))
7142 || reg_overlap_mentioned_for_reload_p (XEXP (reg_notes, 0),
7143 source)))
32131a9c
RK
7144 {
7145 *prev_reg_note = next_reg_notes;
7146 XEXP (reg_notes, 1) = REG_NOTES (insn1);
7147 REG_NOTES (insn1) = reg_notes;
7148 }
7149 else
7150 prev_reg_note = &XEXP (reg_notes, 1);
7151
7152 reg_notes = next_reg_notes;
7153 }
7154 }
7155 }
7156#endif
7157
7158 /* For all the spill regs newly reloaded in this instruction,
7159 record what they were reloaded from, so subsequent instructions
d445b551
RK
7160 can inherit the reloads.
7161
7162 Update spill_reg_store for the reloads of this insn.
e9e79d69 7163 Copy the elements that were updated in the loop above. */
32131a9c
RK
7164
7165 for (j = 0; j < n_reloads; j++)
7166 {
7167 register int r = reload_order[j];
7168 register int i = reload_spill_index[r];
7169
e6e52be0 7170 /* I is nonneg if this reload used a register.
32131a9c 7171 If reload_reg_rtx[r] is 0, this is an optional reload
51f0c3b7 7172 that we opted to ignore. */
d445b551 7173
51f0c3b7 7174 if (i >= 0 && reload_reg_rtx[r] != 0)
32131a9c 7175 {
32131a9c 7176 int nr
e6e52be0 7177 = HARD_REGNO_NREGS (i, GET_MODE (reload_reg_rtx[r]));
32131a9c 7178 int k;
51f0c3b7
JW
7179 int part_reaches_end = 0;
7180 int all_reaches_end = 1;
32131a9c 7181
51f0c3b7
JW
7182 /* For a multi register reload, we need to check if all or part
7183 of the value lives to the end. */
32131a9c
RK
7184 for (k = 0; k < nr; k++)
7185 {
e6e52be0 7186 if (reload_reg_reaches_end_p (i + k, reload_opnum[r],
51f0c3b7
JW
7187 reload_when_needed[r]))
7188 part_reaches_end = 1;
7189 else
7190 all_reaches_end = 0;
32131a9c
RK
7191 }
7192
51f0c3b7
JW
7193 /* Ignore reloads that don't reach the end of the insn in
7194 entirety. */
7195 if (all_reaches_end)
32131a9c 7196 {
51f0c3b7
JW
7197 /* First, clear out memory of what used to be in this spill reg.
7198 If consecutive registers are used, clear them all. */
d08ea79f 7199
32131a9c 7200 for (k = 0; k < nr; k++)
e6e52be0 7201 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
d08ea79f 7202
51f0c3b7
JW
7203 /* Maybe the spill reg contains a copy of reload_out. */
7204 if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
7205 {
7206 register int nregno = REGNO (reload_out[r]);
7207 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7208 : HARD_REGNO_NREGS (nregno,
7209 GET_MODE (reload_reg_rtx[r])));
7210
7211 spill_reg_store[i] = new_spill_reg_store[i];
7212 reg_last_reload_reg[nregno] = reload_reg_rtx[r];
7213
7214 /* If NREGNO is a hard register, it may occupy more than
7215 one register. If it does, say what is in the
7216 rest of the registers assuming that both registers
7217 agree on how many words the object takes. If not,
7218 invalidate the subsequent registers. */
7219
7220 if (nregno < FIRST_PSEUDO_REGISTER)
7221 for (k = 1; k < nnr; k++)
7222 reg_last_reload_reg[nregno + k]
7223 = (nr == nnr
38a448ca
RH
7224 ? gen_rtx_REG (reg_raw_mode[REGNO (reload_reg_rtx[r]) + k],
7225 REGNO (reload_reg_rtx[r]) + k)
51f0c3b7
JW
7226 : 0);
7227
7228 /* Now do the inverse operation. */
7229 for (k = 0; k < nr; k++)
7230 {
e6e52be0
R
7231 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7232 reg_reloaded_contents[i + k]
51f0c3b7
JW
7233 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7234 ? nregno
7235 : nregno + k);
e6e52be0
R
7236 reg_reloaded_insn[i + k] = insn;
7237 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
51f0c3b7
JW
7238 }
7239 }
d08ea79f 7240
51f0c3b7
JW
7241 /* Maybe the spill reg contains a copy of reload_in. Only do
7242 something if there will not be an output reload for
7243 the register being reloaded. */
7244 else if (reload_out[r] == 0
7245 && reload_in[r] != 0
e6e52be0 7246 && spill_reg_order[i] >= 0
51f0c3b7
JW
7247 && ((GET_CODE (reload_in[r]) == REG
7248 && ! reg_has_output_reload[REGNO (reload_in[r])])
7249 || (GET_CODE (reload_in_reg[r]) == REG
7250 && ! reg_has_output_reload[REGNO (reload_in_reg[r])])))
7251 {
7252 register int nregno;
7253 int nnr;
d445b551 7254
51f0c3b7
JW
7255 if (GET_CODE (reload_in[r]) == REG)
7256 nregno = REGNO (reload_in[r]);
7257 else
7258 nregno = REGNO (reload_in_reg[r]);
d08ea79f 7259
51f0c3b7
JW
7260 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7261 : HARD_REGNO_NREGS (nregno,
7262 GET_MODE (reload_reg_rtx[r])));
7263
7264 reg_last_reload_reg[nregno] = reload_reg_rtx[r];
7265
7266 if (nregno < FIRST_PSEUDO_REGISTER)
7267 for (k = 1; k < nnr; k++)
7268 reg_last_reload_reg[nregno + k]
7269 = (nr == nnr
38a448ca
RH
7270 ? gen_rtx_REG (reg_raw_mode[REGNO (reload_reg_rtx[r]) + k],
7271 REGNO (reload_reg_rtx[r]) + k)
51f0c3b7
JW
7272 : 0);
7273
7274 /* Unless we inherited this reload, show we haven't
7275 recently done a store. */
7276 if (! reload_inherited[r])
7277 spill_reg_store[i] = 0;
7278
7279 for (k = 0; k < nr; k++)
7280 {
e6e52be0
R
7281 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7282 reg_reloaded_contents[i + k]
51f0c3b7
JW
7283 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7284 ? nregno
7285 : nregno + k);
e6e52be0
R
7286 reg_reloaded_insn[i + k] = insn;
7287 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
51f0c3b7
JW
7288 }
7289 }
7290 }
d445b551 7291
51f0c3b7
JW
7292 /* However, if part of the reload reaches the end, then we must
7293 invalidate the old info for the part that survives to the end. */
7294 else if (part_reaches_end)
7295 {
546b63fb 7296 for (k = 0; k < nr; k++)
e6e52be0 7297 if (reload_reg_reaches_end_p (i + k,
51f0c3b7
JW
7298 reload_opnum[r],
7299 reload_when_needed[r]))
e6e52be0 7300 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
32131a9c
RK
7301 }
7302 }
7303
7304 /* The following if-statement was #if 0'd in 1.34 (or before...).
7305 It's reenabled in 1.35 because supposedly nothing else
7306 deals with this problem. */
7307
7308 /* If a register gets output-reloaded from a non-spill register,
7309 that invalidates any previous reloaded copy of it.
7310 But forget_old_reloads_1 won't get to see it, because
7311 it thinks only about the original insn. So invalidate it here. */
7312 if (i < 0 && reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
7313 {
7314 register int nregno = REGNO (reload_out[r]);
c7093272
RK
7315 if (nregno >= FIRST_PSEUDO_REGISTER)
7316 reg_last_reload_reg[nregno] = 0;
7317 else
7318 {
7319 int num_regs = HARD_REGNO_NREGS (nregno,GET_MODE (reload_out[r]));
36281332 7320
c7093272
RK
7321 while (num_regs-- > 0)
7322 reg_last_reload_reg[nregno + num_regs] = 0;
7323 }
32131a9c
RK
7324 }
7325 }
e6e52be0 7326 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
32131a9c
RK
7327}
7328\f
5e03c156
RK
7329/* Emit code to perform a reload from IN (which may be a reload register) to
7330 OUT (which may also be a reload register). IN or OUT is from operand
7331 OPNUM with reload type TYPE.
546b63fb 7332
3c3eeea6 7333 Returns first insn emitted. */
32131a9c
RK
7334
7335rtx
5e03c156
RK
7336gen_reload (out, in, opnum, type)
7337 rtx out;
32131a9c 7338 rtx in;
546b63fb
RK
7339 int opnum;
7340 enum reload_type type;
32131a9c 7341{
546b63fb 7342 rtx last = get_last_insn ();
7a5b18b0
RK
7343 rtx tem;
7344
7345 /* If IN is a paradoxical SUBREG, remove it and try to put the
7346 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7347 if (GET_CODE (in) == SUBREG
7348 && (GET_MODE_SIZE (GET_MODE (in))
7349 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7350 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7351 in = SUBREG_REG (in), out = tem;
7352 else if (GET_CODE (out) == SUBREG
7353 && (GET_MODE_SIZE (GET_MODE (out))
7354 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7355 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7356 out = SUBREG_REG (out), in = tem;
32131a9c 7357
a8fdc208 7358 /* How to do this reload can get quite tricky. Normally, we are being
32131a9c
RK
7359 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7360 register that didn't get a hard register. In that case we can just
7361 call emit_move_insn.
7362
a7fd196c
JW
7363 We can also be asked to reload a PLUS that adds a register or a MEM to
7364 another register, constant or MEM. This can occur during frame pointer
7365 elimination and while reloading addresses. This case is handled by
7366 trying to emit a single insn to perform the add. If it is not valid,
7367 we use a two insn sequence.
32131a9c
RK
7368
7369 Finally, we could be called to handle an 'o' constraint by putting
7370 an address into a register. In that case, we first try to do this
7371 with a named pattern of "reload_load_address". If no such pattern
7372 exists, we just emit a SET insn and hope for the best (it will normally
7373 be valid on machines that use 'o').
7374
7375 This entire process is made complex because reload will never
7376 process the insns we generate here and so we must ensure that
7377 they will fit their constraints and also by the fact that parts of
7378 IN might be being reloaded separately and replaced with spill registers.
7379 Because of this, we are, in some sense, just guessing the right approach
7380 here. The one listed above seems to work.
7381
7382 ??? At some point, this whole thing needs to be rethought. */
7383
7384 if (GET_CODE (in) == PLUS
a7fd196c 7385 && (GET_CODE (XEXP (in, 0)) == REG
5c6b1bd2 7386 || GET_CODE (XEXP (in, 0)) == SUBREG
a7fd196c
JW
7387 || GET_CODE (XEXP (in, 0)) == MEM)
7388 && (GET_CODE (XEXP (in, 1)) == REG
5c6b1bd2 7389 || GET_CODE (XEXP (in, 1)) == SUBREG
a7fd196c
JW
7390 || CONSTANT_P (XEXP (in, 1))
7391 || GET_CODE (XEXP (in, 1)) == MEM))
32131a9c 7392 {
a7fd196c
JW
7393 /* We need to compute the sum of a register or a MEM and another
7394 register, constant, or MEM, and put it into the reload
3002e160
JW
7395 register. The best possible way of doing this is if the machine
7396 has a three-operand ADD insn that accepts the required operands.
32131a9c
RK
7397
7398 The simplest approach is to try to generate such an insn and see if it
7399 is recognized and matches its constraints. If so, it can be used.
7400
7401 It might be better not to actually emit the insn unless it is valid,
0009eff2 7402 but we need to pass the insn as an operand to `recog' and
b36d7dd7 7403 `insn_extract' and it is simpler to emit and then delete the insn if
0009eff2 7404 not valid than to dummy things up. */
a8fdc208 7405
af929c62 7406 rtx op0, op1, tem, insn;
32131a9c 7407 int code;
a8fdc208 7408
af929c62
RK
7409 op0 = find_replacement (&XEXP (in, 0));
7410 op1 = find_replacement (&XEXP (in, 1));
7411
32131a9c
RK
7412 /* Since constraint checking is strict, commutativity won't be
7413 checked, so we need to do that here to avoid spurious failure
7414 if the add instruction is two-address and the second operand
7415 of the add is the same as the reload reg, which is frequently
7416 the case. If the insn would be A = B + A, rearrange it so
0f41302f 7417 it will be A = A + B as constrain_operands expects. */
a8fdc208 7418
32131a9c 7419 if (GET_CODE (XEXP (in, 1)) == REG
5e03c156 7420 && REGNO (out) == REGNO (XEXP (in, 1)))
af929c62
RK
7421 tem = op0, op0 = op1, op1 = tem;
7422
7423 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
38a448ca 7424 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
32131a9c 7425
38a448ca 7426 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
32131a9c
RK
7427 code = recog_memoized (insn);
7428
7429 if (code >= 0)
7430 {
7431 insn_extract (insn);
7432 /* We want constrain operands to treat this insn strictly in
7433 its validity determination, i.e., the way it would after reload
7434 has completed. */
7435 if (constrain_operands (code, 1))
7436 return insn;
7437 }
7438
546b63fb 7439 delete_insns_since (last);
32131a9c
RK
7440
7441 /* If that failed, we must use a conservative two-insn sequence.
7442 use move to copy constant, MEM, or pseudo register to the reload
af929c62
RK
7443 register since "move" will be able to handle an arbitrary operand,
7444 unlike add which can't, in general. Then add the registers.
32131a9c
RK
7445
7446 If there is another way to do this for a specific machine, a
7447 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7448 we emit below. */
7449
5c6b1bd2 7450 if (CONSTANT_P (op1) || GET_CODE (op1) == MEM || GET_CODE (op1) == SUBREG
af929c62
RK
7451 || (GET_CODE (op1) == REG
7452 && REGNO (op1) >= FIRST_PSEUDO_REGISTER))
7453 tem = op0, op0 = op1, op1 = tem;
32131a9c 7454
5c6b1bd2 7455 gen_reload (out, op0, opnum, type);
39b56c2a 7456
5e03c156 7457 /* If OP0 and OP1 are the same, we can use OUT for OP1.
39b56c2a
RK
7458 This fixes a problem on the 32K where the stack pointer cannot
7459 be used as an operand of an add insn. */
7460
7461 if (rtx_equal_p (op0, op1))
5e03c156 7462 op1 = out;
39b56c2a 7463
5e03c156 7464 insn = emit_insn (gen_add2_insn (out, op1));
c77c9766
RK
7465
7466 /* If that failed, copy the address register to the reload register.
0f41302f 7467 Then add the constant to the reload register. */
c77c9766
RK
7468
7469 code = recog_memoized (insn);
7470
7471 if (code >= 0)
7472 {
7473 insn_extract (insn);
7474 /* We want constrain operands to treat this insn strictly in
7475 its validity determination, i.e., the way it would after reload
7476 has completed. */
7477 if (constrain_operands (code, 1))
4117a96b
R
7478 {
7479 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7480 REG_NOTES (insn)
9e6a5703 7481 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
4117a96b
R
7482 return insn;
7483 }
c77c9766
RK
7484 }
7485
7486 delete_insns_since (last);
7487
5c6b1bd2 7488 gen_reload (out, op1, opnum, type);
4117a96b 7489 insn = emit_insn (gen_add2_insn (out, op0));
9e6a5703 7490 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
32131a9c
RK
7491 }
7492
0dadecf6
RK
7493#ifdef SECONDARY_MEMORY_NEEDED
7494 /* If we need a memory location to do the move, do it that way. */
7495 else if (GET_CODE (in) == REG && REGNO (in) < FIRST_PSEUDO_REGISTER
5e03c156 7496 && GET_CODE (out) == REG && REGNO (out) < FIRST_PSEUDO_REGISTER
0dadecf6 7497 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (in)),
5e03c156
RK
7498 REGNO_REG_CLASS (REGNO (out)),
7499 GET_MODE (out)))
0dadecf6
RK
7500 {
7501 /* Get the memory to use and rewrite both registers to its mode. */
5e03c156 7502 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
0dadecf6 7503
5e03c156 7504 if (GET_MODE (loc) != GET_MODE (out))
38a448ca 7505 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
0dadecf6
RK
7506
7507 if (GET_MODE (loc) != GET_MODE (in))
38a448ca 7508 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
0dadecf6 7509
5c6b1bd2
RK
7510 gen_reload (loc, in, opnum, type);
7511 gen_reload (out, loc, opnum, type);
0dadecf6
RK
7512 }
7513#endif
7514
32131a9c
RK
7515 /* If IN is a simple operand, use gen_move_insn. */
7516 else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
5e03c156 7517 emit_insn (gen_move_insn (out, in));
32131a9c
RK
7518
7519#ifdef HAVE_reload_load_address
7520 else if (HAVE_reload_load_address)
5e03c156 7521 emit_insn (gen_reload_load_address (out, in));
32131a9c
RK
7522#endif
7523
5e03c156 7524 /* Otherwise, just write (set OUT IN) and hope for the best. */
32131a9c 7525 else
38a448ca 7526 emit_insn (gen_rtx_SET (VOIDmode, out, in));
32131a9c
RK
7527
7528 /* Return the first insn emitted.
546b63fb 7529 We can not just return get_last_insn, because there may have
32131a9c
RK
7530 been multiple instructions emitted. Also note that gen_move_insn may
7531 emit more than one insn itself, so we can not assume that there is one
7532 insn emitted per emit_insn_before call. */
7533
546b63fb 7534 return last ? NEXT_INSN (last) : get_insns ();
32131a9c
RK
7535}
7536\f
7537/* Delete a previously made output-reload
7538 whose result we now believe is not needed.
7539 First we double-check.
7540
7541 INSN is the insn now being processed.
7542 OUTPUT_RELOAD_INSN is the insn of the output reload.
7543 J is the reload-number for this insn. */
7544
7545static void
7546delete_output_reload (insn, j, output_reload_insn)
7547 rtx insn;
7548 int j;
7549 rtx output_reload_insn;
7550{
7551 register rtx i1;
7552
7553 /* Get the raw pseudo-register referred to. */
7554
7555 rtx reg = reload_in[j];
7556 while (GET_CODE (reg) == SUBREG)
7557 reg = SUBREG_REG (reg);
7558
7559 /* If the pseudo-reg we are reloading is no longer referenced
7560 anywhere between the store into it and here,
7561 and no jumps or labels intervene, then the value can get
7562 here through the reload reg alone.
7563 Otherwise, give up--return. */
7564 for (i1 = NEXT_INSN (output_reload_insn);
7565 i1 != insn; i1 = NEXT_INSN (i1))
7566 {
7567 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
7568 return;
7569 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
7570 && reg_mentioned_p (reg, PATTERN (i1)))
aa6498c2
R
7571 {
7572 /* If this is just a single USE with an REG_EQUAL note in front
7573 of INSN, this is no problem, because this mentions just the
7574 address that we are using here.
7575 But if there is more than one such USE, the insn might use
7576 the operand directly, or another reload might do that.
7577 This is analogous to the count_occurences check in the callers. */
7578 int num_occurences = 0;
7579
7580 while (GET_CODE (i1) == INSN && GET_CODE (PATTERN (i1)) == USE
7581 && find_reg_note (i1, REG_EQUAL, NULL_RTX))
7582 {
7583 num_occurences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
7584 i1 = NEXT_INSN (i1);
7585 }
7586 if (num_occurences == 1 && i1 == insn)
7587 break;
7588 return;
7589 }
32131a9c
RK
7590 }
7591
aa6498c2
R
7592 /* The caller has already checked that REG dies or is set in INSN.
7593 It has also checked that we are optimizing, and thus some inaccurancies
7594 in the debugging information are acceptable.
7595 So we could just delete output_reload_insn.
7596 But in some cases we can improve the debugging information without
7597 sacrificing optimization - maybe even improving the code:
7598 See if the pseudo reg has been completely replaced
32131a9c
RK
7599 with reload regs. If so, delete the store insn
7600 and forget we had a stack slot for the pseudo. */
aa6498c2
R
7601 if (reload_out[j] != reload_in[j]
7602 && REG_N_DEATHS (REGNO (reg)) == 1
7603 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
7604 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
32131a9c
RK
7605 {
7606 rtx i2;
7607
7608 /* We know that it was used only between here
7609 and the beginning of the current basic block.
7610 (We also know that the last use before INSN was
7611 the output reload we are thinking of deleting, but never mind that.)
7612 Search that range; see if any ref remains. */
7613 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7614 {
d445b551
RK
7615 rtx set = single_set (i2);
7616
32131a9c
RK
7617 /* Uses which just store in the pseudo don't count,
7618 since if they are the only uses, they are dead. */
d445b551 7619 if (set != 0 && SET_DEST (set) == reg)
32131a9c
RK
7620 continue;
7621 if (GET_CODE (i2) == CODE_LABEL
7622 || GET_CODE (i2) == JUMP_INSN)
7623 break;
7624 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
7625 && reg_mentioned_p (reg, PATTERN (i2)))
aa6498c2
R
7626 {
7627 /* Some other ref remains; just delete the output reload we
7628 know to be dead. */
7629 delete_insn (output_reload_insn);
7630 return;
7631 }
32131a9c
RK
7632 }
7633
7634 /* Delete the now-dead stores into this pseudo. */
7635 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7636 {
d445b551
RK
7637 rtx set = single_set (i2);
7638
7639 if (set != 0 && SET_DEST (set) == reg)
5507b94b
RK
7640 {
7641 /* This might be a basic block head,
7642 thus don't use delete_insn. */
7643 PUT_CODE (i2, NOTE);
7644 NOTE_SOURCE_FILE (i2) = 0;
7645 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
7646 }
32131a9c
RK
7647 if (GET_CODE (i2) == CODE_LABEL
7648 || GET_CODE (i2) == JUMP_INSN)
7649 break;
7650 }
7651
7652 /* For the debugging info,
7653 say the pseudo lives in this reload reg. */
7654 reg_renumber[REGNO (reg)] = REGNO (reload_reg_rtx[j]);
7655 alter_reg (REGNO (reg), -1);
7656 }
aa6498c2
R
7657 delete_insn (output_reload_insn);
7658
32131a9c 7659}
32131a9c 7660\f
a8fdc208 7661/* Output reload-insns to reload VALUE into RELOADREG.
858a47b1 7662 VALUE is an autoincrement or autodecrement RTX whose operand
32131a9c
RK
7663 is a register or memory location;
7664 so reloading involves incrementing that location.
7665
7666 INC_AMOUNT is the number to increment or decrement by (always positive).
546b63fb 7667 This cannot be deduced from VALUE. */
32131a9c 7668
546b63fb
RK
7669static void
7670inc_for_reload (reloadreg, value, inc_amount)
32131a9c
RK
7671 rtx reloadreg;
7672 rtx value;
7673 int inc_amount;
32131a9c
RK
7674{
7675 /* REG or MEM to be copied and incremented. */
7676 rtx incloc = XEXP (value, 0);
7677 /* Nonzero if increment after copying. */
7678 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
546b63fb 7679 rtx last;
0009eff2
RK
7680 rtx inc;
7681 rtx add_insn;
7682 int code;
32131a9c
RK
7683
7684 /* No hard register is equivalent to this register after
7685 inc/dec operation. If REG_LAST_RELOAD_REG were non-zero,
7686 we could inc/dec that register as well (maybe even using it for
7687 the source), but I'm not sure it's worth worrying about. */
7688 if (GET_CODE (incloc) == REG)
7689 reg_last_reload_reg[REGNO (incloc)] = 0;
7690
7691 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
7692 inc_amount = - inc_amount;
7693
fb3821f7 7694 inc = GEN_INT (inc_amount);
0009eff2
RK
7695
7696 /* If this is post-increment, first copy the location to the reload reg. */
7697 if (post)
546b63fb 7698 emit_insn (gen_move_insn (reloadreg, incloc));
0009eff2
RK
7699
7700 /* See if we can directly increment INCLOC. Use a method similar to that
5e03c156 7701 in gen_reload. */
0009eff2 7702
546b63fb 7703 last = get_last_insn ();
38a448ca
RH
7704 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
7705 gen_rtx_PLUS (GET_MODE (incloc),
7706 incloc, inc)));
0009eff2
RK
7707
7708 code = recog_memoized (add_insn);
7709 if (code >= 0)
32131a9c 7710 {
0009eff2
RK
7711 insn_extract (add_insn);
7712 if (constrain_operands (code, 1))
32131a9c 7713 {
0009eff2
RK
7714 /* If this is a pre-increment and we have incremented the value
7715 where it lives, copy the incremented value to RELOADREG to
7716 be used as an address. */
7717
7718 if (! post)
546b63fb
RK
7719 emit_insn (gen_move_insn (reloadreg, incloc));
7720
7721 return;
32131a9c
RK
7722 }
7723 }
0009eff2 7724
546b63fb 7725 delete_insns_since (last);
0009eff2
RK
7726
7727 /* If couldn't do the increment directly, must increment in RELOADREG.
7728 The way we do this depends on whether this is pre- or post-increment.
7729 For pre-increment, copy INCLOC to the reload register, increment it
7730 there, then save back. */
7731
7732 if (! post)
7733 {
546b63fb
RK
7734 emit_insn (gen_move_insn (reloadreg, incloc));
7735 emit_insn (gen_add2_insn (reloadreg, inc));
7736 emit_insn (gen_move_insn (incloc, reloadreg));
0009eff2 7737 }
32131a9c
RK
7738 else
7739 {
0009eff2
RK
7740 /* Postincrement.
7741 Because this might be a jump insn or a compare, and because RELOADREG
7742 may not be available after the insn in an input reload, we must do
7743 the incrementation before the insn being reloaded for.
7744
7745 We have already copied INCLOC to RELOADREG. Increment the copy in
7746 RELOADREG, save that back, then decrement RELOADREG so it has
7747 the original value. */
7748
546b63fb
RK
7749 emit_insn (gen_add2_insn (reloadreg, inc));
7750 emit_insn (gen_move_insn (incloc, reloadreg));
7751 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
32131a9c 7752 }
0009eff2 7753
546b63fb 7754 return;
32131a9c
RK
7755}
7756\f
7757/* Return 1 if we are certain that the constraint-string STRING allows
7758 the hard register REG. Return 0 if we can't be sure of this. */
7759
7760static int
7761constraint_accepts_reg_p (string, reg)
7762 char *string;
7763 rtx reg;
7764{
7765 int value = 0;
7766 int regno = true_regnum (reg);
7767 int c;
7768
7769 /* Initialize for first alternative. */
7770 value = 0;
7771 /* Check that each alternative contains `g' or `r'. */
7772 while (1)
7773 switch (c = *string++)
7774 {
7775 case 0:
7776 /* If an alternative lacks `g' or `r', we lose. */
7777 return value;
7778 case ',':
7779 /* If an alternative lacks `g' or `r', we lose. */
7780 if (value == 0)
7781 return 0;
7782 /* Initialize for next alternative. */
7783 value = 0;
7784 break;
7785 case 'g':
7786 case 'r':
7787 /* Any general reg wins for this alternative. */
7788 if (TEST_HARD_REG_BIT (reg_class_contents[(int) GENERAL_REGS], regno))
7789 value = 1;
7790 break;
7791 default:
7792 /* Any reg in specified class wins for this alternative. */
7793 {
0009eff2 7794 enum reg_class class = REG_CLASS_FROM_LETTER (c);
32131a9c 7795
0009eff2 7796 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno))
32131a9c
RK
7797 value = 1;
7798 }
7799 }
7800}
7801\f
d445b551
RK
7802/* Return the number of places FIND appears within X, but don't count
7803 an occurrence if some SET_DEST is FIND. */
32131a9c 7804
184bb750 7805int
32131a9c
RK
7806count_occurrences (x, find)
7807 register rtx x, find;
7808{
7809 register int i, j;
7810 register enum rtx_code code;
7811 register char *format_ptr;
7812 int count;
7813
7814 if (x == find)
7815 return 1;
7816 if (x == 0)
7817 return 0;
7818
7819 code = GET_CODE (x);
7820
7821 switch (code)
7822 {
7823 case REG:
7824 case QUEUED:
7825 case CONST_INT:
7826 case CONST_DOUBLE:
7827 case SYMBOL_REF:
7828 case CODE_LABEL:
7829 case PC:
7830 case CC0:
7831 return 0;
d445b551
RK
7832
7833 case SET:
7834 if (SET_DEST (x) == find)
7835 return count_occurrences (SET_SRC (x), find);
7836 break;
e9a25f70
JL
7837
7838 default:
7839 break;
32131a9c
RK
7840 }
7841
7842 format_ptr = GET_RTX_FORMAT (code);
7843 count = 0;
7844
7845 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7846 {
7847 switch (*format_ptr++)
7848 {
7849 case 'e':
7850 count += count_occurrences (XEXP (x, i), find);
7851 break;
7852
7853 case 'E':
7854 if (XVEC (x, i) != NULL)
7855 {
7856 for (j = 0; j < XVECLEN (x, i); j++)
7857 count += count_occurrences (XVECEXP (x, i, j), find);
7858 }
7859 break;
7860 }
7861 }
7862 return count;
7863}
2a9fb548
ILT
7864\f
7865/* This array holds values which are equivalent to a hard register
7866 during reload_cse_regs. Each array element is an EXPR_LIST of
7867 values. Each time a hard register is set, we set the corresponding
7868 array element to the value. Each time a hard register is copied
7869 into memory, we add the memory location to the corresponding array
7870 element. We don't store values or memory addresses with side
7871 effects in this array.
7872
7873 If the value is a CONST_INT, then the mode of the containing
7874 EXPR_LIST is the mode in which that CONST_INT was referenced.
7875
7876 We sometimes clobber a specific entry in a list. In that case, we
7877 just set XEXP (list-entry, 0) to 0. */
7878
7879static rtx *reg_values;
7880
ba325eba
ILT
7881/* This is a preallocated REG rtx which we use as a temporary in
7882 reload_cse_invalidate_regno, so that we don't need to allocate a
7883 new one each time through a loop in that function. */
7884
7885static rtx invalidate_regno_rtx;
7886
e9a25f70
JL
7887/* This is a set of registers for which we must remove REG_DEAD notes in
7888 previous insns, because our modifications made them invalid. That can
7889 happen if we introduced the register into the current insn, or we deleted
7890 the current insn which used to set the register. */
7891
7892static HARD_REG_SET no_longer_dead_regs;
7893
2a9fb548
ILT
7894/* Invalidate any entries in reg_values which depend on REGNO,
7895 including those for REGNO itself. This is called if REGNO is
7896 changing. If CLOBBER is true, then always forget anything we
7897 currently know about REGNO. MODE is the mode of the assignment to
7898 REGNO, which is used to determine how many hard registers are being
7899 changed. If MODE is VOIDmode, then only REGNO is being changed;
7900 this is used when invalidating call clobbered registers across a
7901 call. */
7902
7903static void
7904reload_cse_invalidate_regno (regno, mode, clobber)
7905 int regno;
7906 enum machine_mode mode;
7907 int clobber;
7908{
7909 int endregno;
7910 register int i;
7911
7912 /* Our callers don't always go through true_regnum; we may see a
7913 pseudo-register here from a CLOBBER or the like. We probably
7914 won't ever see a pseudo-register that has a real register number,
7915 for we check anyhow for safety. */
7916 if (regno >= FIRST_PSEUDO_REGISTER)
7917 regno = reg_renumber[regno];
7918 if (regno < 0)
7919 return;
7920
7921 if (mode == VOIDmode)
7922 endregno = regno + 1;
7923 else
7924 endregno = regno + HARD_REGNO_NREGS (regno, mode);
7925
7926 if (clobber)
7927 for (i = regno; i < endregno; i++)
7928 reg_values[i] = 0;
7929
7930 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7931 {
7932 rtx x;
7933
7934 for (x = reg_values[i]; x; x = XEXP (x, 1))
7935 {
7936 if (XEXP (x, 0) != 0
9e148ceb 7937 && refers_to_regno_p (regno, endregno, XEXP (x, 0), NULL_PTR))
2a9fb548
ILT
7938 {
7939 /* If this is the only entry on the list, clear
7940 reg_values[i]. Otherwise, just clear this entry on
7941 the list. */
7942 if (XEXP (x, 1) == 0 && x == reg_values[i])
7943 {
7944 reg_values[i] = 0;
7945 break;
7946 }
7947 XEXP (x, 0) = 0;
7948 }
7949 }
7950 }
ba325eba
ILT
7951
7952 /* We must look at earlier registers, in case REGNO is part of a
7953 multi word value but is not the first register. If an earlier
7954 register has a value in a mode which overlaps REGNO, then we must
7955 invalidate that earlier register. Note that we do not need to
7956 check REGNO or later registers (we must not check REGNO itself,
7957 because we would incorrectly conclude that there was a conflict). */
7958
7959 for (i = 0; i < regno; i++)
7960 {
7961 rtx x;
7962
7963 for (x = reg_values[i]; x; x = XEXP (x, 1))
7964 {
7965 if (XEXP (x, 0) != 0)
7966 {
dbd7556e 7967 PUT_MODE (invalidate_regno_rtx, GET_MODE (x));
ba325eba
ILT
7968 REGNO (invalidate_regno_rtx) = i;
7969 if (refers_to_regno_p (regno, endregno, invalidate_regno_rtx,
7970 NULL_PTR))
7971 {
7972 reload_cse_invalidate_regno (i, VOIDmode, 1);
7973 break;
7974 }
7975 }
7976 }
7977 }
2a9fb548
ILT
7978}
7979
866aa3b6
DE
7980/* The memory at address MEM_BASE is being changed.
7981 Return whether this change will invalidate VAL. */
2a9fb548
ILT
7982
7983static int
cbfc3ad3 7984reload_cse_mem_conflict_p (mem_base, val)
2a9fb548 7985 rtx mem_base;
2a9fb548
ILT
7986 rtx val;
7987{
7988 enum rtx_code code;
7989 char *fmt;
7990 int i;
7991
7992 code = GET_CODE (val);
7993 switch (code)
7994 {
7995 /* Get rid of a few simple cases quickly. */
7996 case REG:
2a9fb548
ILT
7997 case PC:
7998 case CC0:
7999 case SCRATCH:
8000 case CONST:
8001 case CONST_INT:
8002 case CONST_DOUBLE:
8003 case SYMBOL_REF:
8004 case LABEL_REF:
8005 return 0;
8006
8007 case MEM:
866aa3b6
DE
8008 if (GET_MODE (mem_base) == BLKmode
8009 || GET_MODE (val) == BLKmode)
8010 return 1;
e9a25f70
JL
8011 if (anti_dependence (val, mem_base))
8012 return 1;
8013 /* The address may contain nested MEMs. */
8014 break;
2a9fb548
ILT
8015
8016 default:
8017 break;
8018 }
8019
8020 fmt = GET_RTX_FORMAT (code);
8021
8022 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8023 {
8024 if (fmt[i] == 'e')
8025 {
cbfc3ad3 8026 if (reload_cse_mem_conflict_p (mem_base, XEXP (val, i)))
2a9fb548
ILT
8027 return 1;
8028 }
8029 else if (fmt[i] == 'E')
8030 {
8031 int j;
8032
8033 for (j = 0; j < XVECLEN (val, i); j++)
cbfc3ad3 8034 if (reload_cse_mem_conflict_p (mem_base, XVECEXP (val, i, j)))
2a9fb548
ILT
8035 return 1;
8036 }
8037 }
8038
8039 return 0;
8040}
8041
8042/* Invalidate any entries in reg_values which are changed because of a
8043 store to MEM_RTX. If this is called because of a non-const call
8044 instruction, MEM_RTX is (mem:BLK const0_rtx). */
8045
8046static void
8047reload_cse_invalidate_mem (mem_rtx)
8048 rtx mem_rtx;
8049{
8050 register int i;
2a9fb548
ILT
8051
8052 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8053 {
8054 rtx x;
8055
8056 for (x = reg_values[i]; x; x = XEXP (x, 1))
8057 {
8058 if (XEXP (x, 0) != 0
cbfc3ad3 8059 && reload_cse_mem_conflict_p (mem_rtx, XEXP (x, 0)))
2a9fb548
ILT
8060 {
8061 /* If this is the only entry on the list, clear
8062 reg_values[i]. Otherwise, just clear this entry on
8063 the list. */
8064 if (XEXP (x, 1) == 0 && x == reg_values[i])
8065 {
8066 reg_values[i] = 0;
8067 break;
8068 }
8069 XEXP (x, 0) = 0;
8070 }
8071 }
8072 }
8073}
8074
8075/* Invalidate DEST, which is being assigned to or clobbered. The
8076 second parameter exists so that this function can be passed to
8077 note_stores; it is ignored. */
8078
8079static void
8080reload_cse_invalidate_rtx (dest, ignore)
8081 rtx dest;
487a6e06 8082 rtx ignore ATTRIBUTE_UNUSED;
2a9fb548
ILT
8083{
8084 while (GET_CODE (dest) == STRICT_LOW_PART
8085 || GET_CODE (dest) == SIGN_EXTRACT
8086 || GET_CODE (dest) == ZERO_EXTRACT
8087 || GET_CODE (dest) == SUBREG)
8088 dest = XEXP (dest, 0);
8089
8090 if (GET_CODE (dest) == REG)
8091 reload_cse_invalidate_regno (REGNO (dest), GET_MODE (dest), 1);
8092 else if (GET_CODE (dest) == MEM)
8093 reload_cse_invalidate_mem (dest);
8094}
8095
e9a25f70
JL
8096/* Possibly delete death notes on the insns before INSN if modifying INSN
8097 extended the lifespan of the registers. */
8098
8099static void
8100reload_cse_delete_death_notes (insn)
8101 rtx insn;
8102{
8103 int dreg;
8104
8105 for (dreg = 0; dreg < FIRST_PSEUDO_REGISTER; dreg++)
8106 {
8107 rtx trial;
8108
8109 if (! TEST_HARD_REG_BIT (no_longer_dead_regs, dreg))
8110 continue;
8111
8112 for (trial = prev_nonnote_insn (insn);
8113 (trial
8114 && GET_CODE (trial) != CODE_LABEL
8115 && GET_CODE (trial) != BARRIER);
8116 trial = prev_nonnote_insn (trial))
8117 {
8118 if (find_regno_note (trial, REG_DEAD, dreg))
8119 {
8120 remove_death (dreg, trial);
8121 break;
8122 }
8123 }
8124 }
8125}
8126
8127/* Record that the current insn uses hard reg REGNO in mode MODE. This
8128 will be used in reload_cse_delete_death_notes to delete prior REG_DEAD
8129 notes for this register. */
8130
8131static void
8132reload_cse_no_longer_dead (regno, mode)
8133 int regno;
8134 enum machine_mode mode;
8135{
8136 int nregs = HARD_REGNO_NREGS (regno, mode);
8137 while (nregs-- > 0)
8138 {
8139 SET_HARD_REG_BIT (no_longer_dead_regs, regno);
8140 regno++;
8141 }
8142}
8143
8144
2a9fb548
ILT
8145/* Do a very simple CSE pass over the hard registers.
8146
8147 This function detects no-op moves where we happened to assign two
8148 different pseudo-registers to the same hard register, and then
8149 copied one to the other. Reload will generate a useless
8150 instruction copying a register to itself.
8151
8152 This function also detects cases where we load a value from memory
8153 into two different registers, and (if memory is more expensive than
8154 registers) changes it to simply copy the first register into the
e9a25f70
JL
8155 second register.
8156
8157 Another optimization is performed that scans the operands of each
8158 instruction to see whether the value is already available in a
8159 hard register. It then replaces the operand with the hard register
8160 if possible, much like an optional reload would. */
2a9fb548 8161
cbfc3ad3 8162void
2a9fb548
ILT
8163reload_cse_regs (first)
8164 rtx first;
8165{
8166 char *firstobj;
8167 rtx callmem;
8168 register int i;
8169 rtx insn;
8170
cbfc3ad3
RK
8171 init_alias_analysis ();
8172
2a9fb548 8173 reg_values = (rtx *) alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
e016950d 8174 bzero ((char *)reg_values, FIRST_PSEUDO_REGISTER * sizeof (rtx));
2a9fb548
ILT
8175
8176 /* Create our EXPR_LIST structures on reload_obstack, so that we can
8177 free them when we are done. */
8178 push_obstacks (&reload_obstack, &reload_obstack);
8179 firstobj = (char *) obstack_alloc (&reload_obstack, 0);
8180
8181 /* We pass this to reload_cse_invalidate_mem to invalidate all of
8182 memory for a non-const call instruction. */
38a448ca 8183 callmem = gen_rtx_MEM (BLKmode, const0_rtx);
2a9fb548 8184
ba325eba
ILT
8185 /* This is used in reload_cse_invalidate_regno to avoid consing a
8186 new REG in a loop in that function. */
38a448ca 8187 invalidate_regno_rtx = gen_rtx_REG (VOIDmode, 0);
ba325eba 8188
2a9fb548
ILT
8189 for (insn = first; insn; insn = NEXT_INSN (insn))
8190 {
8191 rtx body;
8192
8193 if (GET_CODE (insn) == CODE_LABEL)
8194 {
8195 /* Forget all the register values at a code label. We don't
8196 try to do anything clever around jumps. */
8197 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8198 reg_values[i] = 0;
8199
8200 continue;
8201 }
8202
8203#ifdef NON_SAVING_SETJMP
8204 if (NON_SAVING_SETJMP && GET_CODE (insn) == NOTE
8205 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
8206 {
8207 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8208 reg_values[i] = 0;
8209
8210 continue;
8211 }
8212#endif
8213
8214 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
8215 continue;
8216
e9a25f70
JL
8217 CLEAR_HARD_REG_SET (no_longer_dead_regs);
8218
2a9fb548
ILT
8219 /* If this is a call instruction, forget anything stored in a
8220 call clobbered register, or, if this is not a const call, in
8221 memory. */
8222 if (GET_CODE (insn) == CALL_INSN)
8223 {
8224 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8225 if (call_used_regs[i])
8226 reload_cse_invalidate_regno (i, VOIDmode, 1);
8227
8228 if (! CONST_CALL_P (insn))
8229 reload_cse_invalidate_mem (callmem);
8230 }
8231
8232 body = PATTERN (insn);
8233 if (GET_CODE (body) == SET)
8234 {
e9a25f70 8235 int count = 0;
31418d35 8236 if (reload_cse_noop_set_p (body, insn))
2a9fb548 8237 {
2a9fb548
ILT
8238 PUT_CODE (insn, NOTE);
8239 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8240 NOTE_SOURCE_FILE (insn) = 0;
e9a25f70 8241 reload_cse_delete_death_notes (insn);
2a9fb548
ILT
8242
8243 /* We're done with this insn. */
8244 continue;
8245 }
8246
e9a25f70
JL
8247 /* It's not a no-op, but we can try to simplify it. */
8248 CLEAR_HARD_REG_SET (no_longer_dead_regs);
8249 count += reload_cse_simplify_set (body, insn);
8250
8251 if (count > 0 && apply_change_group ())
8252 reload_cse_delete_death_notes (insn);
8253 else if (reload_cse_simplify_operands (insn))
8254 reload_cse_delete_death_notes (insn);
8255
2a9fb548
ILT
8256 reload_cse_record_set (body, body);
8257 }
8258 else if (GET_CODE (body) == PARALLEL)
8259 {
e9a25f70 8260 int count = 0;
2a9fb548
ILT
8261
8262 /* If every action in a PARALLEL is a noop, we can delete
8263 the entire PARALLEL. */
8264 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
cbfc3ad3
RK
8265 if ((GET_CODE (XVECEXP (body, 0, i)) != SET
8266 || ! reload_cse_noop_set_p (XVECEXP (body, 0, i), insn))
8267 && GET_CODE (XVECEXP (body, 0, i)) != CLOBBER)
2a9fb548
ILT
8268 break;
8269 if (i < 0)
8270 {
2a9fb548
ILT
8271 PUT_CODE (insn, NOTE);
8272 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8273 NOTE_SOURCE_FILE (insn) = 0;
e9a25f70 8274 reload_cse_delete_death_notes (insn);
2a9fb548
ILT
8275
8276 /* We're done with this insn. */
8277 continue;
8278 }
e9a25f70
JL
8279
8280 /* It's not a no-op, but we can try to simplify it. */
8281 CLEAR_HARD_REG_SET (no_longer_dead_regs);
8282 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8283 if (GET_CODE (XVECEXP (body, 0, i)) == SET)
8284 count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
8285
8286 if (count > 0 && apply_change_group ())
8287 reload_cse_delete_death_notes (insn);
8288 else if (reload_cse_simplify_operands (insn))
8289 reload_cse_delete_death_notes (insn);
2a9fb548
ILT
8290
8291 /* Look through the PARALLEL and record the values being
8292 set, if possible. Also handle any CLOBBERs. */
8293 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8294 {
8295 rtx x = XVECEXP (body, 0, i);
8296
8297 if (GET_CODE (x) == SET)
8298 reload_cse_record_set (x, body);
8299 else
8300 note_stores (x, reload_cse_invalidate_rtx);
8301 }
8302 }
8303 else
8304 note_stores (body, reload_cse_invalidate_rtx);
8305
8306#ifdef AUTO_INC_DEC
8307 /* Clobber any registers which appear in REG_INC notes. We
8308 could keep track of the changes to their values, but it is
8309 unlikely to help. */
8310 {
8311 rtx x;
8312
8313 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
8314 if (REG_NOTE_KIND (x) == REG_INC)
8315 reload_cse_invalidate_rtx (XEXP (x, 0), NULL_RTX);
8316 }
8317#endif
8318
8319 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
8320 after we have processed the insn. */
8321 if (GET_CODE (insn) == CALL_INSN)
8322 {
8323 rtx x;
8324
8325 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
8326 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
8327 reload_cse_invalidate_rtx (XEXP (XEXP (x, 0), 0), NULL_RTX);
8328 }
8329 }
8330
8331 /* Free all the temporary structures we created, and go back to the
8332 regular obstacks. */
8333 obstack_free (&reload_obstack, firstobj);
8334 pop_obstacks ();
8335}
8336
8337/* Return whether the values known for REGNO are equal to VAL. MODE
8338 is the mode of the object that VAL is being copied to; this matters
8339 if VAL is a CONST_INT. */
8340
8341static int
8342reload_cse_regno_equal_p (regno, val, mode)
8343 int regno;
8344 rtx val;
8345 enum machine_mode mode;
8346{
8347 rtx x;
8348
8349 if (val == 0)
8350 return 0;
8351
8352 for (x = reg_values[regno]; x; x = XEXP (x, 1))
8353 if (XEXP (x, 0) != 0
8354 && rtx_equal_p (XEXP (x, 0), val)
bb173ade
RK
8355 && (! flag_float_store || GET_CODE (XEXP (x, 0)) != MEM
8356 || GET_MODE_CLASS (GET_MODE (x)) != MODE_FLOAT)
2a9fb548
ILT
8357 && (GET_CODE (val) != CONST_INT
8358 || mode == GET_MODE (x)
8359 || (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
6e848450
RK
8360 /* On a big endian machine if the value spans more than
8361 one register then this register holds the high part of
8362 it and we can't use it.
8363
8364 ??? We should also compare with the high part of the
8365 value. */
8366 && !(WORDS_BIG_ENDIAN
8367 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
2a9fb548
ILT
8368 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
8369 GET_MODE_BITSIZE (GET_MODE (x))))))
8370 return 1;
8371
8372 return 0;
8373}
8374
31418d35
ILT
8375/* See whether a single set is a noop. SET is the set instruction we
8376 are should check, and INSN is the instruction from which it came. */
2a9fb548
ILT
8377
8378static int
31418d35 8379reload_cse_noop_set_p (set, insn)
2a9fb548 8380 rtx set;
31418d35 8381 rtx insn;
2a9fb548
ILT
8382{
8383 rtx src, dest;
8384 enum machine_mode dest_mode;
8385 int dreg, sreg;
31418d35 8386 int ret;
2a9fb548
ILT
8387
8388 src = SET_SRC (set);
8389 dest = SET_DEST (set);
8390 dest_mode = GET_MODE (dest);
8391
8392 if (side_effects_p (src))
8393 return 0;
8394
8395 dreg = true_regnum (dest);
8396 sreg = true_regnum (src);
8397
31418d35
ILT
8398 /* Check for setting a register to itself. In this case, we don't
8399 have to worry about REG_DEAD notes. */
8400 if (dreg >= 0 && dreg == sreg)
8401 return 1;
8402
8403 ret = 0;
2a9fb548
ILT
8404 if (dreg >= 0)
8405 {
8406 /* Check for setting a register to itself. */
8407 if (dreg == sreg)
31418d35 8408 ret = 1;
2a9fb548
ILT
8409
8410 /* Check for setting a register to a value which we already know
8411 is in the register. */
31418d35
ILT
8412 else if (reload_cse_regno_equal_p (dreg, src, dest_mode))
8413 ret = 1;
2a9fb548
ILT
8414
8415 /* Check for setting a register DREG to another register SREG
8416 where SREG is equal to a value which is already in DREG. */
31418d35 8417 else if (sreg >= 0)
2a9fb548
ILT
8418 {
8419 rtx x;
8420
8421 for (x = reg_values[sreg]; x; x = XEXP (x, 1))
31418d35 8422 {
99c2b71f
ILT
8423 rtx tmp;
8424
8425 if (XEXP (x, 0) == 0)
8426 continue;
8427
8428 if (dest_mode == GET_MODE (x))
8429 tmp = XEXP (x, 0);
8430 else if (GET_MODE_BITSIZE (dest_mode)
8431 < GET_MODE_BITSIZE (GET_MODE (x)))
8432 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
8433 else
8434 continue;
8435
8436 if (tmp
8437 && reload_cse_regno_equal_p (dreg, tmp, dest_mode))
31418d35
ILT
8438 {
8439 ret = 1;
8440 break;
8441 }
8442 }
2a9fb548
ILT
8443 }
8444 }
8445 else if (GET_CODE (dest) == MEM)
8446 {
8447 /* Check for storing a register to memory when we know that the
8448 register is equivalent to the memory location. */
8449 if (sreg >= 0
8450 && reload_cse_regno_equal_p (sreg, dest, dest_mode)
8451 && ! side_effects_p (dest))
31418d35 8452 ret = 1;
2a9fb548
ILT
8453 }
8454
31418d35
ILT
8455 /* If we can delete this SET, then we need to look for an earlier
8456 REG_DEAD note on DREG, and remove it if it exists. */
e9a25f70 8457 if (ret && dreg >= 0)
31418d35
ILT
8458 {
8459 if (! find_regno_note (insn, REG_UNUSED, dreg))
e9a25f70 8460 reload_cse_no_longer_dead (dreg, dest_mode);
31418d35
ILT
8461 }
8462
8463 return ret;
2a9fb548
ILT
8464}
8465
8466/* Try to simplify a single SET instruction. SET is the set pattern.
e9a25f70
JL
8467 INSN is the instruction it came from.
8468 This function only handles one case: if we set a register to a value
8469 which is not a register, we try to find that value in some other register
8470 and change the set into a register copy. */
2a9fb548 8471
e9a25f70 8472static int
2a9fb548
ILT
8473reload_cse_simplify_set (set, insn)
8474 rtx set;
8475 rtx insn;
8476{
8477 int dreg;
8478 rtx src;
8479 enum machine_mode dest_mode;
8480 enum reg_class dclass;
8481 register int i;
8482
2a9fb548
ILT
8483 dreg = true_regnum (SET_DEST (set));
8484 if (dreg < 0)
e9a25f70 8485 return 0;
2a9fb548
ILT
8486
8487 src = SET_SRC (set);
8488 if (side_effects_p (src) || true_regnum (src) >= 0)
e9a25f70 8489 return 0;
2a9fb548 8490
cbd5b9a2
KR
8491 dclass = REGNO_REG_CLASS (dreg);
8492
33ab8de0 8493 /* If memory loads are cheaper than register copies, don't change them. */
cbd5b9a2
KR
8494 if (GET_CODE (src) == MEM
8495 && MEMORY_MOVE_COST (GET_MODE (src), dclass, 1) < 2)
e9a25f70 8496 return 0;
2a9fb548 8497
0254c561
JC
8498 /* If the constant is cheaper than a register, don't change it. */
8499 if (CONSTANT_P (src)
8500 && rtx_cost (src, SET) < 2)
8501 return 0;
8502
2a9fb548 8503 dest_mode = GET_MODE (SET_DEST (set));
2a9fb548
ILT
8504 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8505 {
8506 if (i != dreg
8507 && REGISTER_MOVE_COST (REGNO_REG_CLASS (i), dclass) == 2
8508 && reload_cse_regno_equal_p (i, src, dest_mode))
8509 {
8510 int validated;
8511
8512 /* Pop back to the real obstacks while changing the insn. */
8513 pop_obstacks ();
8514
8515 validated = validate_change (insn, &SET_SRC (set),
38a448ca 8516 gen_rtx_REG (dest_mode, i), 1);
2a9fb548
ILT
8517
8518 /* Go back to the obstack we are using for temporary
8519 storage. */
8520 push_obstacks (&reload_obstack, &reload_obstack);
8521
e9a25f70
JL
8522 if (validated && ! find_regno_note (insn, REG_UNUSED, i))
8523 {
8524 reload_cse_no_longer_dead (i, dest_mode);
8525 return 1;
8526 }
8527 }
8528 }
8529 return 0;
8530}
8531
8532/* Try to replace operands in INSN with equivalent values that are already
8533 in registers. This can be viewed as optional reloading.
8534
8535 For each non-register operand in the insn, see if any hard regs are
8536 known to be equivalent to that operand. Record the alternatives which
8537 can accept these hard registers. Among all alternatives, select the
8538 ones which are better or equal to the one currently matching, where
8539 "better" is in terms of '?' and '!' constraints. Among the remaining
8540 alternatives, select the one which replaces most operands with
8541 hard registers. */
8542
8543static int
8544reload_cse_simplify_operands (insn)
8545 rtx insn;
8546{
8547#ifdef REGISTER_CONSTRAINTS
8548 int insn_code_number, n_operands, n_alternatives;
8549 int i,j;
8550
8551 char *constraints[MAX_RECOG_OPERANDS];
8552
8553 /* Vector recording how bad an alternative is. */
8554 int *alternative_reject;
8555 /* Vector recording how many registers can be introduced by choosing
8556 this alternative. */
8557 int *alternative_nregs;
8558 /* Array of vectors recording, for each operand and each alternative,
8559 which hard register to substitute, or -1 if the operand should be
8560 left as it is. */
8561 int *op_alt_regno[MAX_RECOG_OPERANDS];
8562 /* Array of alternatives, sorted in order of decreasing desirability. */
8563 int *alternative_order;
0254c561 8564 rtx reg = gen_rtx_REG (VOIDmode, -1);
e9a25f70
JL
8565
8566 /* Find out some information about this insn. */
8567 insn_code_number = recog_memoized (insn);
8568 /* We don't modify asm instructions. */
8569 if (insn_code_number < 0)
8570 return 0;
8571
8572 n_operands = insn_n_operands[insn_code_number];
8573 n_alternatives = insn_n_alternatives[insn_code_number];
8574
8575 if (n_alternatives == 0 || n_operands == 0)
1d300e19 8576 return 0;
e9a25f70
JL
8577 insn_extract (insn);
8578
8579 /* Figure out which alternative currently matches. */
8580 if (! constrain_operands (insn_code_number, 1))
8581 abort ();
8582
8583 alternative_reject = (int *) alloca (n_alternatives * sizeof (int));
8584 alternative_nregs = (int *) alloca (n_alternatives * sizeof (int));
8585 alternative_order = (int *) alloca (n_alternatives * sizeof (int));
8586 bzero ((char *)alternative_reject, n_alternatives * sizeof (int));
8587 bzero ((char *)alternative_nregs, n_alternatives * sizeof (int));
8588
8589 for (i = 0; i < n_operands; i++)
8590 {
8591 enum machine_mode mode;
8592 int regno;
8593 char *p;
8594
8595 op_alt_regno[i] = (int *) alloca (n_alternatives * sizeof (int));
8596 for (j = 0; j < n_alternatives; j++)
8597 op_alt_regno[i][j] = -1;
8598
8599 p = constraints[i] = insn_operand_constraint[insn_code_number][i];
8600 mode = insn_operand_mode[insn_code_number][i];
8601
8602 /* Add the reject values for each alternative given by the constraints
8603 for this operand. */
8604 j = 0;
8605 while (*p != '\0')
8606 {
8607 char c = *p++;
8608 if (c == ',')
8609 j++;
8610 else if (c == '?')
8611 alternative_reject[j] += 3;
8612 else if (c == '!')
8613 alternative_reject[j] += 300;
8614 }
8615
8616 /* We won't change operands which are already registers. We
8617 also don't want to modify output operands. */
8618 regno = true_regnum (recog_operand[i]);
8619 if (regno >= 0
8620 || constraints[i][0] == '='
8621 || constraints[i][0] == '+')
8622 continue;
8623
8624 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8625 {
8626 int class = (int) NO_REGS;
8627
8628 if (! reload_cse_regno_equal_p (regno, recog_operand[i], mode))
8629 continue;
8630
0254c561
JC
8631 REGNO (reg) = regno;
8632 PUT_MODE (reg, mode);
8633
e9a25f70
JL
8634 /* We found a register equal to this operand. Now look for all
8635 alternatives that can accept this register and have not been
8636 assigned a register they can use yet. */
8637 j = 0;
8638 p = constraints[i];
8639 for (;;)
31418d35 8640 {
e9a25f70
JL
8641 char c = *p++;
8642
8643 switch (c)
31418d35 8644 {
e9a25f70
JL
8645 case '=': case '+': case '?':
8646 case '#': case '&': case '!':
8647 case '*': case '%':
8648 case '0': case '1': case '2': case '3': case '4':
8649 case 'm': case '<': case '>': case 'V': case 'o':
8650 case 'E': case 'F': case 'G': case 'H':
8651 case 's': case 'i': case 'n':
8652 case 'I': case 'J': case 'K': case 'L':
8653 case 'M': case 'N': case 'O': case 'P':
8654#ifdef EXTRA_CONSTRAINT
8655 case 'Q': case 'R': case 'S': case 'T': case 'U':
8656#endif
8657 case 'p': case 'X':
8658 /* These don't say anything we care about. */
8659 break;
8660
8661 case 'g': case 'r':
8662 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
8663 break;
8664
8665 default:
8666 class
8667 = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER (c)];
8668 break;
31418d35 8669
e9a25f70
JL
8670 case ',': case '\0':
8671 /* See if REGNO fits this alternative, and set it up as the
8672 replacement register if we don't have one for this
0254c561
JC
8673 alternative yet and the operand being replaced is not
8674 a cheap CONST_INT. */
e9a25f70 8675 if (op_alt_regno[i][j] == -1
0254c561
JC
8676 && reg_fits_class_p (reg, class, 0, mode)
8677 && (GET_CODE (recog_operand[i]) != CONST_INT
8678 || rtx_cost (recog_operand[i], SET) > rtx_cost (reg, SET)))
31418d35 8679 {
e9a25f70
JL
8680 alternative_nregs[j]++;
8681 op_alt_regno[i][j] = regno;
31418d35 8682 }
e9a25f70
JL
8683 j++;
8684 break;
31418d35
ILT
8685 }
8686
e9a25f70
JL
8687 if (c == '\0')
8688 break;
8689 }
8690 }
8691 }
8692
8693 /* Record all alternatives which are better or equal to the currently
8694 matching one in the alternative_order array. */
8695 for (i = j = 0; i < n_alternatives; i++)
8696 if (alternative_reject[i] <= alternative_reject[which_alternative])
8697 alternative_order[j++] = i;
8698 n_alternatives = j;
8699
8700 /* Sort it. Given a small number of alternatives, a dumb algorithm
8701 won't hurt too much. */
8702 for (i = 0; i < n_alternatives - 1; i++)
8703 {
8704 int best = i;
8705 int best_reject = alternative_reject[alternative_order[i]];
8706 int best_nregs = alternative_nregs[alternative_order[i]];
8707 int tmp;
8708
8709 for (j = i + 1; j < n_alternatives; j++)
8710 {
8711 int this_reject = alternative_reject[alternative_order[j]];
8712 int this_nregs = alternative_nregs[alternative_order[j]];
8713
8714 if (this_reject < best_reject
8715 || (this_reject == best_reject && this_nregs < best_nregs))
8716 {
8717 best = j;
8718 best_reject = this_reject;
8719 best_nregs = this_nregs;
31418d35 8720 }
2a9fb548 8721 }
e9a25f70
JL
8722
8723 tmp = alternative_order[best];
8724 alternative_order[best] = alternative_order[i];
8725 alternative_order[i] = tmp;
8726 }
8727
8728 /* Substitute the operands as determined by op_alt_regno for the best
8729 alternative. */
8730 j = alternative_order[0];
8731 CLEAR_HARD_REG_SET (no_longer_dead_regs);
8732
8733 /* Pop back to the real obstacks while changing the insn. */
8734 pop_obstacks ();
8735
8736 for (i = 0; i < n_operands; i++)
8737 {
8738 enum machine_mode mode = insn_operand_mode[insn_code_number][i];
8739 if (op_alt_regno[i][j] == -1)
8740 continue;
8741
8742 reload_cse_no_longer_dead (op_alt_regno[i][j], mode);
8743 validate_change (insn, recog_operand_loc[i],
38a448ca 8744 gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
e9a25f70
JL
8745 }
8746
8747 for (i = insn_n_dups[insn_code_number] - 1; i >= 0; i--)
8748 {
8749 int op = recog_dup_num[i];
8750 enum machine_mode mode = insn_operand_mode[insn_code_number][op];
8751
8752 if (op_alt_regno[op][j] == -1)
8753 continue;
8754
8755 reload_cse_no_longer_dead (op_alt_regno[op][j], mode);
8756 validate_change (insn, recog_dup_loc[i],
38a448ca 8757 gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
2a9fb548 8758 }
e9a25f70
JL
8759
8760 /* Go back to the obstack we are using for temporary
8761 storage. */
8762 push_obstacks (&reload_obstack, &reload_obstack);
8763
8764 return apply_change_group ();
8765#else
8766 return 0;
8767#endif
2a9fb548
ILT
8768}
8769
8770/* These two variables are used to pass information from
8771 reload_cse_record_set to reload_cse_check_clobber. */
8772
8773static int reload_cse_check_clobbered;
8774static rtx reload_cse_check_src;
8775
8776/* See if DEST overlaps with RELOAD_CSE_CHECK_SRC. If it does, set
8777 RELOAD_CSE_CHECK_CLOBBERED. This is called via note_stores. The
8778 second argument, which is passed by note_stores, is ignored. */
8779
8780static void
8781reload_cse_check_clobber (dest, ignore)
8782 rtx dest;
487a6e06 8783 rtx ignore ATTRIBUTE_UNUSED;
2a9fb548
ILT
8784{
8785 if (reg_overlap_mentioned_p (dest, reload_cse_check_src))
8786 reload_cse_check_clobbered = 1;
8787}
8788
8789/* Record the result of a SET instruction. SET is the set pattern.
8790 BODY is the pattern of the insn that it came from. */
8791
8792static void
8793reload_cse_record_set (set, body)
8794 rtx set;
8795 rtx body;
8796{
9e148ceb 8797 rtx dest, src, x;
2a9fb548
ILT
8798 int dreg, sreg;
8799 enum machine_mode dest_mode;
8800
8801 dest = SET_DEST (set);
8802 src = SET_SRC (set);
8803 dreg = true_regnum (dest);
8804 sreg = true_regnum (src);
8805 dest_mode = GET_MODE (dest);
8806
9e148ceb
ILT
8807 /* Some machines don't define AUTO_INC_DEC, but they still use push
8808 instructions. We need to catch that case here in order to
8809 invalidate the stack pointer correctly. Note that invalidating
8810 the stack pointer is different from invalidating DEST. */
8811 x = dest;
8812 while (GET_CODE (x) == SUBREG
8813 || GET_CODE (x) == ZERO_EXTRACT
8814 || GET_CODE (x) == SIGN_EXTRACT
8815 || GET_CODE (x) == STRICT_LOW_PART)
8816 x = XEXP (x, 0);
8817 if (push_operand (x, GET_MODE (x)))
8818 {
8819 reload_cse_invalidate_rtx (stack_pointer_rtx, NULL_RTX);
8820 reload_cse_invalidate_rtx (dest, NULL_RTX);
8821 return;
8822 }
8823
2a9fb548
ILT
8824 /* We can only handle an assignment to a register, or a store of a
8825 register to a memory location. For other cases, we just clobber
8826 the destination. We also have to just clobber if there are side
8827 effects in SRC or DEST. */
8828 if ((dreg < 0 && GET_CODE (dest) != MEM)
8829 || side_effects_p (src)
8830 || side_effects_p (dest))
8831 {
8832 reload_cse_invalidate_rtx (dest, NULL_RTX);
8833 return;
8834 }
8835
8836#ifdef HAVE_cc0
8837 /* We don't try to handle values involving CC, because it's a pain
8838 to keep track of when they have to be invalidated. */
8839 if (reg_mentioned_p (cc0_rtx, src)
8840 || reg_mentioned_p (cc0_rtx, dest))
8841 {
8842 reload_cse_invalidate_rtx (dest, NULL_RTX);
8843 return;
8844 }
8845#endif
8846
8847 /* If BODY is a PARALLEL, then we need to see whether the source of
8848 SET is clobbered by some other instruction in the PARALLEL. */
8849 if (GET_CODE (body) == PARALLEL)
8850 {
8851 int i;
8852
8853 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8854 {
8855 rtx x;
8856
8857 x = XVECEXP (body, 0, i);
8858 if (x == set)
8859 continue;
8860
8861 reload_cse_check_clobbered = 0;
8862 reload_cse_check_src = src;
8863 note_stores (x, reload_cse_check_clobber);
8864 if (reload_cse_check_clobbered)
8865 {
8866 reload_cse_invalidate_rtx (dest, NULL_RTX);
8867 return;
8868 }
8869 }
8870 }
8871
8872 if (dreg >= 0)
8873 {
8874 int i;
8875
8876 /* This is an assignment to a register. Update the value we
8877 have stored for the register. */
8878 if (sreg >= 0)
ad578014
ILT
8879 {
8880 rtx x;
8881
8882 /* This is a copy from one register to another. Any values
8883 which were valid for SREG are now valid for DREG. If the
8884 mode changes, we use gen_lowpart_common to extract only
8885 the part of the value that is copied. */
8886 reg_values[dreg] = 0;
8887 for (x = reg_values[sreg]; x; x = XEXP (x, 1))
8888 {
8889 rtx tmp;
8890
8891 if (XEXP (x, 0) == 0)
8892 continue;
8893 if (dest_mode == GET_MODE (XEXP (x, 0)))
8894 tmp = XEXP (x, 0);
23e7786b
JL
8895 else if (GET_MODE_BITSIZE (dest_mode)
8896 > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
8897 continue;
ad578014
ILT
8898 else
8899 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
8900 if (tmp)
38a448ca
RH
8901 reg_values[dreg] = gen_rtx_EXPR_LIST (dest_mode, tmp,
8902 reg_values[dreg]);
ad578014
ILT
8903 }
8904 }
2a9fb548 8905 else
38a448ca 8906 reg_values[dreg] = gen_rtx_EXPR_LIST (dest_mode, src, NULL_RTX);
2a9fb548
ILT
8907
8908 /* We've changed DREG, so invalidate any values held by other
8909 registers that depend upon it. */
8910 reload_cse_invalidate_regno (dreg, dest_mode, 0);
8911
8912 /* If this assignment changes more than one hard register,
8913 forget anything we know about the others. */
8914 for (i = 1; i < HARD_REGNO_NREGS (dreg, dest_mode); i++)
8915 reg_values[dreg + i] = 0;
8916 }
8917 else if (GET_CODE (dest) == MEM)
8918 {
8919 /* Invalidate conflicting memory locations. */
8920 reload_cse_invalidate_mem (dest);
8921
8922 /* If we're storing a register to memory, add DEST to the list
8923 in REG_VALUES. */
8924 if (sreg >= 0 && ! side_effects_p (dest))
38a448ca 8925 reg_values[sreg] = gen_rtx_EXPR_LIST (dest_mode, dest,
2a9fb548
ILT
8926 reg_values[sreg]);
8927 }
8928 else
8929 {
8930 /* We should have bailed out earlier. */
8931 abort ();
8932 }
8933}
This page took 1.794163 seconds and 5 git commands to generate.