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