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