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