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