i386.c (legitimize_tls_address): Generate tls_initial_exec_64_sun only when !TARGET_X32.
[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 (Pmode, 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 (Pmode, 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 (Pmode, 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 (Pmode,
2727 gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2728 ep->previous_offset * INTVAL (XEXP (x, 1)));
2729 }
2730
2731 /* ... fall through ... */
2732
2733 case CALL:
2734 case COMPARE:
2735 /* See comments before PLUS about handling MINUS. */
2736 case MINUS:
2737 case DIV: case UDIV:
2738 case MOD: case UMOD:
2739 case AND: case IOR: case XOR:
2740 case ROTATERT: case ROTATE:
2741 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2742 case NE: case EQ:
2743 case GE: case GT: case GEU: case GTU:
2744 case LE: case LT: case LEU: case LTU:
2745 {
2746 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false,
2747 for_costs);
2748 rtx new1 = XEXP (x, 1)
2749 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, false,
2750 for_costs) : 0;
2751
2752 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2753 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2754 }
2755 return x;
2756
2757 case EXPR_LIST:
2758 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2759 if (XEXP (x, 0))
2760 {
2761 new_rtx = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true,
2762 for_costs);
2763 if (new_rtx != XEXP (x, 0))
2764 {
2765 /* If this is a REG_DEAD note, it is not valid anymore.
2766 Using the eliminated version could result in creating a
2767 REG_DEAD note for the stack or frame pointer. */
2768 if (REG_NOTE_KIND (x) == REG_DEAD)
2769 return (XEXP (x, 1)
2770 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true,
2771 for_costs)
2772 : NULL_RTX);
2773
2774 x = alloc_reg_note (REG_NOTE_KIND (x), new_rtx, XEXP (x, 1));
2775 }
2776 }
2777
2778 /* ... fall through ... */
2779
2780 case INSN_LIST:
2781 /* Now do eliminations in the rest of the chain. If this was
2782 an EXPR_LIST, this might result in allocating more memory than is
2783 strictly needed, but it simplifies the code. */
2784 if (XEXP (x, 1))
2785 {
2786 new_rtx = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true,
2787 for_costs);
2788 if (new_rtx != XEXP (x, 1))
2789 return
2790 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new_rtx);
2791 }
2792 return x;
2793
2794 case PRE_INC:
2795 case POST_INC:
2796 case PRE_DEC:
2797 case POST_DEC:
2798 /* We do not support elimination of a register that is modified.
2799 elimination_effects has already make sure that this does not
2800 happen. */
2801 return x;
2802
2803 case PRE_MODIFY:
2804 case POST_MODIFY:
2805 /* We do not support elimination of a register that is modified.
2806 elimination_effects has already make sure that this does not
2807 happen. The only remaining case we need to consider here is
2808 that the increment value may be an eliminable register. */
2809 if (GET_CODE (XEXP (x, 1)) == PLUS
2810 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
2811 {
2812 rtx new_rtx = eliminate_regs_1 (XEXP (XEXP (x, 1), 1), mem_mode,
2813 insn, true, for_costs);
2814
2815 if (new_rtx != XEXP (XEXP (x, 1), 1))
2816 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
2817 gen_rtx_PLUS (GET_MODE (x),
2818 XEXP (x, 0), new_rtx));
2819 }
2820 return x;
2821
2822 case STRICT_LOW_PART:
2823 case NEG: case NOT:
2824 case SIGN_EXTEND: case ZERO_EXTEND:
2825 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2826 case FLOAT: case FIX:
2827 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2828 case ABS:
2829 case SQRT:
2830 case FFS:
2831 case CLZ:
2832 case CTZ:
2833 case POPCOUNT:
2834 case PARITY:
2835 case BSWAP:
2836 new_rtx = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false,
2837 for_costs);
2838 if (new_rtx != XEXP (x, 0))
2839 return gen_rtx_fmt_e (code, GET_MODE (x), new_rtx);
2840 return x;
2841
2842 case SUBREG:
2843 /* Similar to above processing, but preserve SUBREG_BYTE.
2844 Convert (subreg (mem)) to (mem) if not paradoxical.
2845 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2846 pseudo didn't get a hard reg, we must replace this with the
2847 eliminated version of the memory location because push_reload
2848 may do the replacement in certain circumstances. */
2849 if (REG_P (SUBREG_REG (x))
2850 && !paradoxical_subreg_p (x)
2851 && reg_equivs
2852 && reg_equiv_memory_loc (REGNO (SUBREG_REG (x))) != 0)
2853 {
2854 new_rtx = SUBREG_REG (x);
2855 }
2856 else
2857 new_rtx = eliminate_regs_1 (SUBREG_REG (x), mem_mode, insn, false, for_costs);
2858
2859 if (new_rtx != SUBREG_REG (x))
2860 {
2861 int x_size = GET_MODE_SIZE (GET_MODE (x));
2862 int new_size = GET_MODE_SIZE (GET_MODE (new_rtx));
2863
2864 if (MEM_P (new_rtx)
2865 && ((x_size < new_size
2866 #ifdef WORD_REGISTER_OPERATIONS
2867 /* On these machines, combine can create rtl of the form
2868 (set (subreg:m1 (reg:m2 R) 0) ...)
2869 where m1 < m2, and expects something interesting to
2870 happen to the entire word. Moreover, it will use the
2871 (reg:m2 R) later, expecting all bits to be preserved.
2872 So if the number of words is the same, preserve the
2873 subreg so that push_reload can see it. */
2874 && ! ((x_size - 1) / UNITS_PER_WORD
2875 == (new_size -1 ) / UNITS_PER_WORD)
2876 #endif
2877 )
2878 || x_size == new_size)
2879 )
2880 return adjust_address_nv (new_rtx, GET_MODE (x), SUBREG_BYTE (x));
2881 else
2882 return gen_rtx_SUBREG (GET_MODE (x), new_rtx, SUBREG_BYTE (x));
2883 }
2884
2885 return x;
2886
2887 case MEM:
2888 /* Our only special processing is to pass the mode of the MEM to our
2889 recursive call and copy the flags. While we are here, handle this
2890 case more efficiently. */
2891
2892 new_rtx = eliminate_regs_1 (XEXP (x, 0), GET_MODE (x), insn, true,
2893 for_costs);
2894 if (for_costs
2895 && memory_address_p (GET_MODE (x), XEXP (x, 0))
2896 && !memory_address_p (GET_MODE (x), new_rtx))
2897 for_each_rtx (&XEXP (x, 0), note_reg_elim_costly, insn);
2898
2899 return replace_equiv_address_nv (x, new_rtx);
2900
2901 case USE:
2902 /* Handle insn_list USE that a call to a pure function may generate. */
2903 new_rtx = eliminate_regs_1 (XEXP (x, 0), VOIDmode, insn, false,
2904 for_costs);
2905 if (new_rtx != XEXP (x, 0))
2906 return gen_rtx_USE (GET_MODE (x), new_rtx);
2907 return x;
2908
2909 case CLOBBER:
2910 case ASM_OPERANDS:
2911 gcc_assert (insn && DEBUG_INSN_P (insn));
2912 break;
2913
2914 case SET:
2915 gcc_unreachable ();
2916
2917 default:
2918 break;
2919 }
2920
2921 /* Process each of our operands recursively. If any have changed, make a
2922 copy of the rtx. */
2923 fmt = GET_RTX_FORMAT (code);
2924 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2925 {
2926 if (*fmt == 'e')
2927 {
2928 new_rtx = eliminate_regs_1 (XEXP (x, i), mem_mode, insn, false,
2929 for_costs);
2930 if (new_rtx != XEXP (x, i) && ! copied)
2931 {
2932 x = shallow_copy_rtx (x);
2933 copied = 1;
2934 }
2935 XEXP (x, i) = new_rtx;
2936 }
2937 else if (*fmt == 'E')
2938 {
2939 int copied_vec = 0;
2940 for (j = 0; j < XVECLEN (x, i); j++)
2941 {
2942 new_rtx = eliminate_regs_1 (XVECEXP (x, i, j), mem_mode, insn, false,
2943 for_costs);
2944 if (new_rtx != XVECEXP (x, i, j) && ! copied_vec)
2945 {
2946 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2947 XVEC (x, i)->elem);
2948 if (! copied)
2949 {
2950 x = shallow_copy_rtx (x);
2951 copied = 1;
2952 }
2953 XVEC (x, i) = new_v;
2954 copied_vec = 1;
2955 }
2956 XVECEXP (x, i, j) = new_rtx;
2957 }
2958 }
2959 }
2960
2961 return x;
2962 }
2963
2964 rtx
2965 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2966 {
2967 return eliminate_regs_1 (x, mem_mode, insn, false, false);
2968 }
2969
2970 /* Scan rtx X for modifications of elimination target registers. Update
2971 the table of eliminables to reflect the changed state. MEM_MODE is
2972 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2973
2974 static void
2975 elimination_effects (rtx x, enum machine_mode mem_mode)
2976 {
2977 enum rtx_code code = GET_CODE (x);
2978 struct elim_table *ep;
2979 int regno;
2980 int i, j;
2981 const char *fmt;
2982
2983 switch (code)
2984 {
2985 case CONST_INT:
2986 case CONST_DOUBLE:
2987 case CONST_FIXED:
2988 case CONST_VECTOR:
2989 case CONST:
2990 case SYMBOL_REF:
2991 case CODE_LABEL:
2992 case PC:
2993 case CC0:
2994 case ASM_INPUT:
2995 case ADDR_VEC:
2996 case ADDR_DIFF_VEC:
2997 case RETURN:
2998 return;
2999
3000 case REG:
3001 regno = REGNO (x);
3002
3003 /* First handle the case where we encounter a bare register that
3004 is eliminable. Replace it with a PLUS. */
3005 if (regno < FIRST_PSEUDO_REGISTER)
3006 {
3007 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3008 ep++)
3009 if (ep->from_rtx == x && ep->can_eliminate)
3010 {
3011 if (! mem_mode)
3012 ep->ref_outside_mem = 1;
3013 return;
3014 }
3015
3016 }
3017 else if (reg_renumber[regno] < 0
3018 && reg_equivs != 0
3019 && reg_equiv_constant (regno)
3020 && ! function_invariant_p (reg_equiv_constant (regno)))
3021 elimination_effects (reg_equiv_constant (regno), mem_mode);
3022 return;
3023
3024 case PRE_INC:
3025 case POST_INC:
3026 case PRE_DEC:
3027 case POST_DEC:
3028 case POST_MODIFY:
3029 case PRE_MODIFY:
3030 /* If we modify the source of an elimination rule, disable it. */
3031 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3032 if (ep->from_rtx == XEXP (x, 0))
3033 ep->can_eliminate = 0;
3034
3035 /* If we modify the target of an elimination rule by adding a constant,
3036 update its offset. If we modify the target in any other way, we'll
3037 have to disable the rule as well. */
3038 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3039 if (ep->to_rtx == XEXP (x, 0))
3040 {
3041 int size = GET_MODE_SIZE (mem_mode);
3042
3043 /* If more bytes than MEM_MODE are pushed, account for them. */
3044 #ifdef PUSH_ROUNDING
3045 if (ep->to_rtx == stack_pointer_rtx)
3046 size = PUSH_ROUNDING (size);
3047 #endif
3048 if (code == PRE_DEC || code == POST_DEC)
3049 ep->offset += size;
3050 else if (code == PRE_INC || code == POST_INC)
3051 ep->offset -= size;
3052 else if (code == PRE_MODIFY || code == POST_MODIFY)
3053 {
3054 if (GET_CODE (XEXP (x, 1)) == PLUS
3055 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
3056 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))
3057 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
3058 else
3059 ep->can_eliminate = 0;
3060 }
3061 }
3062
3063 /* These two aren't unary operators. */
3064 if (code == POST_MODIFY || code == PRE_MODIFY)
3065 break;
3066
3067 /* Fall through to generic unary operation case. */
3068 case STRICT_LOW_PART:
3069 case NEG: case NOT:
3070 case SIGN_EXTEND: case ZERO_EXTEND:
3071 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3072 case FLOAT: case FIX:
3073 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3074 case ABS:
3075 case SQRT:
3076 case FFS:
3077 case CLZ:
3078 case CTZ:
3079 case POPCOUNT:
3080 case PARITY:
3081 case BSWAP:
3082 elimination_effects (XEXP (x, 0), mem_mode);
3083 return;
3084
3085 case SUBREG:
3086 if (REG_P (SUBREG_REG (x))
3087 && (GET_MODE_SIZE (GET_MODE (x))
3088 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3089 && reg_equivs != 0
3090 && reg_equiv_memory_loc (REGNO (SUBREG_REG (x))) != 0)
3091 return;
3092
3093 elimination_effects (SUBREG_REG (x), mem_mode);
3094 return;
3095
3096 case USE:
3097 /* If using a register that is the source of an eliminate we still
3098 think can be performed, note it cannot be performed since we don't
3099 know how this register is used. */
3100 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3101 if (ep->from_rtx == XEXP (x, 0))
3102 ep->can_eliminate = 0;
3103
3104 elimination_effects (XEXP (x, 0), mem_mode);
3105 return;
3106
3107 case CLOBBER:
3108 /* If clobbering a register that is the replacement register for an
3109 elimination we still think can be performed, note that it cannot
3110 be performed. Otherwise, we need not be concerned about it. */
3111 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3112 if (ep->to_rtx == XEXP (x, 0))
3113 ep->can_eliminate = 0;
3114
3115 elimination_effects (XEXP (x, 0), mem_mode);
3116 return;
3117
3118 case SET:
3119 /* Check for setting a register that we know about. */
3120 if (REG_P (SET_DEST (x)))
3121 {
3122 /* See if this is setting the replacement register for an
3123 elimination.
3124
3125 If DEST is the hard frame pointer, we do nothing because we
3126 assume that all assignments to the frame pointer are for
3127 non-local gotos and are being done at a time when they are valid
3128 and do not disturb anything else. Some machines want to
3129 eliminate a fake argument pointer (or even a fake frame pointer)
3130 with either the real frame or the stack pointer. Assignments to
3131 the hard frame pointer must not prevent this elimination. */
3132
3133 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3134 ep++)
3135 if (ep->to_rtx == SET_DEST (x)
3136 && SET_DEST (x) != hard_frame_pointer_rtx)
3137 {
3138 /* If it is being incremented, adjust the offset. Otherwise,
3139 this elimination can't be done. */
3140 rtx src = SET_SRC (x);
3141
3142 if (GET_CODE (src) == PLUS
3143 && XEXP (src, 0) == SET_DEST (x)
3144 && CONST_INT_P (XEXP (src, 1)))
3145 ep->offset -= INTVAL (XEXP (src, 1));
3146 else
3147 ep->can_eliminate = 0;
3148 }
3149 }
3150
3151 elimination_effects (SET_DEST (x), VOIDmode);
3152 elimination_effects (SET_SRC (x), VOIDmode);
3153 return;
3154
3155 case MEM:
3156 /* Our only special processing is to pass the mode of the MEM to our
3157 recursive call. */
3158 elimination_effects (XEXP (x, 0), GET_MODE (x));
3159 return;
3160
3161 default:
3162 break;
3163 }
3164
3165 fmt = GET_RTX_FORMAT (code);
3166 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3167 {
3168 if (*fmt == 'e')
3169 elimination_effects (XEXP (x, i), mem_mode);
3170 else if (*fmt == 'E')
3171 for (j = 0; j < XVECLEN (x, i); j++)
3172 elimination_effects (XVECEXP (x, i, j), mem_mode);
3173 }
3174 }
3175
3176 /* Descend through rtx X and verify that no references to eliminable registers
3177 remain. If any do remain, mark the involved register as not
3178 eliminable. */
3179
3180 static void
3181 check_eliminable_occurrences (rtx x)
3182 {
3183 const char *fmt;
3184 int i;
3185 enum rtx_code code;
3186
3187 if (x == 0)
3188 return;
3189
3190 code = GET_CODE (x);
3191
3192 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
3193 {
3194 struct elim_table *ep;
3195
3196 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3197 if (ep->from_rtx == x)
3198 ep->can_eliminate = 0;
3199 return;
3200 }
3201
3202 fmt = GET_RTX_FORMAT (code);
3203 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3204 {
3205 if (*fmt == 'e')
3206 check_eliminable_occurrences (XEXP (x, i));
3207 else if (*fmt == 'E')
3208 {
3209 int j;
3210 for (j = 0; j < XVECLEN (x, i); j++)
3211 check_eliminable_occurrences (XVECEXP (x, i, j));
3212 }
3213 }
3214 }
3215 \f
3216 /* Scan INSN and eliminate all eliminable registers in it.
3217
3218 If REPLACE is nonzero, do the replacement destructively. Also
3219 delete the insn as dead it if it is setting an eliminable register.
3220
3221 If REPLACE is zero, do all our allocations in reload_obstack.
3222
3223 If no eliminations were done and this insn doesn't require any elimination
3224 processing (these are not identical conditions: it might be updating sp,
3225 but not referencing fp; this needs to be seen during reload_as_needed so
3226 that the offset between fp and sp can be taken into consideration), zero
3227 is returned. Otherwise, 1 is returned. */
3228
3229 static int
3230 eliminate_regs_in_insn (rtx insn, int replace)
3231 {
3232 int icode = recog_memoized (insn);
3233 rtx old_body = PATTERN (insn);
3234 int insn_is_asm = asm_noperands (old_body) >= 0;
3235 rtx old_set = single_set (insn);
3236 rtx new_body;
3237 int val = 0;
3238 int i;
3239 rtx substed_operand[MAX_RECOG_OPERANDS];
3240 rtx orig_operand[MAX_RECOG_OPERANDS];
3241 struct elim_table *ep;
3242 rtx plus_src, plus_cst_src;
3243
3244 if (! insn_is_asm && icode < 0)
3245 {
3246 gcc_assert (GET_CODE (PATTERN (insn)) == USE
3247 || GET_CODE (PATTERN (insn)) == CLOBBER
3248 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3249 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3250 || GET_CODE (PATTERN (insn)) == ASM_INPUT
3251 || DEBUG_INSN_P (insn));
3252 if (DEBUG_INSN_P (insn))
3253 INSN_VAR_LOCATION_LOC (insn)
3254 = eliminate_regs (INSN_VAR_LOCATION_LOC (insn), VOIDmode, insn);
3255 return 0;
3256 }
3257
3258 if (old_set != 0 && REG_P (SET_DEST (old_set))
3259 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3260 {
3261 /* Check for setting an eliminable register. */
3262 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3263 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3264 {
3265 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3266 /* If this is setting the frame pointer register to the
3267 hardware frame pointer register and this is an elimination
3268 that will be done (tested above), this insn is really
3269 adjusting the frame pointer downward to compensate for
3270 the adjustment done before a nonlocal goto. */
3271 if (ep->from == FRAME_POINTER_REGNUM
3272 && ep->to == HARD_FRAME_POINTER_REGNUM)
3273 {
3274 rtx base = SET_SRC (old_set);
3275 rtx base_insn = insn;
3276 HOST_WIDE_INT offset = 0;
3277
3278 while (base != ep->to_rtx)
3279 {
3280 rtx prev_insn, prev_set;
3281
3282 if (GET_CODE (base) == PLUS
3283 && CONST_INT_P (XEXP (base, 1)))
3284 {
3285 offset += INTVAL (XEXP (base, 1));
3286 base = XEXP (base, 0);
3287 }
3288 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
3289 && (prev_set = single_set (prev_insn)) != 0
3290 && rtx_equal_p (SET_DEST (prev_set), base))
3291 {
3292 base = SET_SRC (prev_set);
3293 base_insn = prev_insn;
3294 }
3295 else
3296 break;
3297 }
3298
3299 if (base == ep->to_rtx)
3300 {
3301 rtx src = plus_constant (Pmode, ep->to_rtx,
3302 offset - ep->offset);
3303
3304 new_body = old_body;
3305 if (! replace)
3306 {
3307 new_body = copy_insn (old_body);
3308 if (REG_NOTES (insn))
3309 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3310 }
3311 PATTERN (insn) = new_body;
3312 old_set = single_set (insn);
3313
3314 /* First see if this insn remains valid when we
3315 make the change. If not, keep the INSN_CODE
3316 the same and let reload fit it up. */
3317 validate_change (insn, &SET_SRC (old_set), src, 1);
3318 validate_change (insn, &SET_DEST (old_set),
3319 ep->to_rtx, 1);
3320 if (! apply_change_group ())
3321 {
3322 SET_SRC (old_set) = src;
3323 SET_DEST (old_set) = ep->to_rtx;
3324 }
3325
3326 val = 1;
3327 goto done;
3328 }
3329 }
3330 #endif
3331
3332 /* In this case this insn isn't serving a useful purpose. We
3333 will delete it in reload_as_needed once we know that this
3334 elimination is, in fact, being done.
3335
3336 If REPLACE isn't set, we can't delete this insn, but needn't
3337 process it since it won't be used unless something changes. */
3338 if (replace)
3339 {
3340 delete_dead_insn (insn);
3341 return 1;
3342 }
3343 val = 1;
3344 goto done;
3345 }
3346 }
3347
3348 /* We allow one special case which happens to work on all machines we
3349 currently support: a single set with the source or a REG_EQUAL
3350 note being a PLUS of an eliminable register and a constant. */
3351 plus_src = plus_cst_src = 0;
3352 if (old_set && REG_P (SET_DEST (old_set)))
3353 {
3354 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3355 plus_src = SET_SRC (old_set);
3356 /* First see if the source is of the form (plus (...) CST). */
3357 if (plus_src
3358 && CONST_INT_P (XEXP (plus_src, 1)))
3359 plus_cst_src = plus_src;
3360 else if (REG_P (SET_SRC (old_set))
3361 || plus_src)
3362 {
3363 /* Otherwise, see if we have a REG_EQUAL note of the form
3364 (plus (...) CST). */
3365 rtx links;
3366 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3367 {
3368 if ((REG_NOTE_KIND (links) == REG_EQUAL
3369 || REG_NOTE_KIND (links) == REG_EQUIV)
3370 && GET_CODE (XEXP (links, 0)) == PLUS
3371 && CONST_INT_P (XEXP (XEXP (links, 0), 1)))
3372 {
3373 plus_cst_src = XEXP (links, 0);
3374 break;
3375 }
3376 }
3377 }
3378
3379 /* Check that the first operand of the PLUS is a hard reg or
3380 the lowpart subreg of one. */
3381 if (plus_cst_src)
3382 {
3383 rtx reg = XEXP (plus_cst_src, 0);
3384 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
3385 reg = SUBREG_REG (reg);
3386
3387 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
3388 plus_cst_src = 0;
3389 }
3390 }
3391 if (plus_cst_src)
3392 {
3393 rtx reg = XEXP (plus_cst_src, 0);
3394 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
3395
3396 if (GET_CODE (reg) == SUBREG)
3397 reg = SUBREG_REG (reg);
3398
3399 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3400 if (ep->from_rtx == reg && ep->can_eliminate)
3401 {
3402 rtx to_rtx = ep->to_rtx;
3403 offset += ep->offset;
3404 offset = trunc_int_for_mode (offset, GET_MODE (plus_cst_src));
3405
3406 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
3407 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)),
3408 to_rtx);
3409 /* If we have a nonzero offset, and the source is already
3410 a simple REG, the following transformation would
3411 increase the cost of the insn by replacing a simple REG
3412 with (plus (reg sp) CST). So try only when we already
3413 had a PLUS before. */
3414 if (offset == 0 || plus_src)
3415 {
3416 rtx new_src = plus_constant (GET_MODE (to_rtx),
3417 to_rtx, offset);
3418
3419 new_body = old_body;
3420 if (! replace)
3421 {
3422 new_body = copy_insn (old_body);
3423 if (REG_NOTES (insn))
3424 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3425 }
3426 PATTERN (insn) = new_body;
3427 old_set = single_set (insn);
3428
3429 /* First see if this insn remains valid when we make the
3430 change. If not, try to replace the whole pattern with
3431 a simple set (this may help if the original insn was a
3432 PARALLEL that was only recognized as single_set due to
3433 REG_UNUSED notes). If this isn't valid either, keep
3434 the INSN_CODE the same and let reload fix it up. */
3435 if (!validate_change (insn, &SET_SRC (old_set), new_src, 0))
3436 {
3437 rtx new_pat = gen_rtx_SET (VOIDmode,
3438 SET_DEST (old_set), new_src);
3439
3440 if (!validate_change (insn, &PATTERN (insn), new_pat, 0))
3441 SET_SRC (old_set) = new_src;
3442 }
3443 }
3444 else
3445 break;
3446
3447 val = 1;
3448 /* This can't have an effect on elimination offsets, so skip right
3449 to the end. */
3450 goto done;
3451 }
3452 }
3453
3454 /* Determine the effects of this insn on elimination offsets. */
3455 elimination_effects (old_body, VOIDmode);
3456
3457 /* Eliminate all eliminable registers occurring in operands that
3458 can be handled by reload. */
3459 extract_insn (insn);
3460 for (i = 0; i < recog_data.n_operands; i++)
3461 {
3462 orig_operand[i] = recog_data.operand[i];
3463 substed_operand[i] = recog_data.operand[i];
3464
3465 /* For an asm statement, every operand is eliminable. */
3466 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3467 {
3468 bool is_set_src, in_plus;
3469
3470 /* Check for setting a register that we know about. */
3471 if (recog_data.operand_type[i] != OP_IN
3472 && REG_P (orig_operand[i]))
3473 {
3474 /* If we are assigning to a register that can be eliminated, it
3475 must be as part of a PARALLEL, since the code above handles
3476 single SETs. We must indicate that we can no longer
3477 eliminate this reg. */
3478 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3479 ep++)
3480 if (ep->from_rtx == orig_operand[i])
3481 ep->can_eliminate = 0;
3482 }
3483
3484 /* Companion to the above plus substitution, we can allow
3485 invariants as the source of a plain move. */
3486 is_set_src = false;
3487 if (old_set
3488 && recog_data.operand_loc[i] == &SET_SRC (old_set))
3489 is_set_src = true;
3490 in_plus = false;
3491 if (plus_src
3492 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3493 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3494 in_plus = true;
3495
3496 substed_operand[i]
3497 = eliminate_regs_1 (recog_data.operand[i], VOIDmode,
3498 replace ? insn : NULL_RTX,
3499 is_set_src || in_plus, false);
3500 if (substed_operand[i] != orig_operand[i])
3501 val = 1;
3502 /* Terminate the search in check_eliminable_occurrences at
3503 this point. */
3504 *recog_data.operand_loc[i] = 0;
3505
3506 /* If an output operand changed from a REG to a MEM and INSN is an
3507 insn, write a CLOBBER insn. */
3508 if (recog_data.operand_type[i] != OP_IN
3509 && REG_P (orig_operand[i])
3510 && MEM_P (substed_operand[i])
3511 && replace)
3512 emit_insn_after (gen_clobber (orig_operand[i]), insn);
3513 }
3514 }
3515
3516 for (i = 0; i < recog_data.n_dups; i++)
3517 *recog_data.dup_loc[i]
3518 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3519
3520 /* If any eliminable remain, they aren't eliminable anymore. */
3521 check_eliminable_occurrences (old_body);
3522
3523 /* Substitute the operands; the new values are in the substed_operand
3524 array. */
3525 for (i = 0; i < recog_data.n_operands; i++)
3526 *recog_data.operand_loc[i] = substed_operand[i];
3527 for (i = 0; i < recog_data.n_dups; i++)
3528 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3529
3530 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3531 re-recognize the insn. We do this in case we had a simple addition
3532 but now can do this as a load-address. This saves an insn in this
3533 common case.
3534 If re-recognition fails, the old insn code number will still be used,
3535 and some register operands may have changed into PLUS expressions.
3536 These will be handled by find_reloads by loading them into a register
3537 again. */
3538
3539 if (val)
3540 {
3541 /* If we aren't replacing things permanently and we changed something,
3542 make another copy to ensure that all the RTL is new. Otherwise
3543 things can go wrong if find_reload swaps commutative operands
3544 and one is inside RTL that has been copied while the other is not. */
3545 new_body = old_body;
3546 if (! replace)
3547 {
3548 new_body = copy_insn (old_body);
3549 if (REG_NOTES (insn))
3550 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3551 }
3552 PATTERN (insn) = new_body;
3553
3554 /* If we had a move insn but now we don't, rerecognize it. This will
3555 cause spurious re-recognition if the old move had a PARALLEL since
3556 the new one still will, but we can't call single_set without
3557 having put NEW_BODY into the insn and the re-recognition won't
3558 hurt in this rare case. */
3559 /* ??? Why this huge if statement - why don't we just rerecognize the
3560 thing always? */
3561 if (! insn_is_asm
3562 && old_set != 0
3563 && ((REG_P (SET_SRC (old_set))
3564 && (GET_CODE (new_body) != SET
3565 || !REG_P (SET_SRC (new_body))))
3566 /* If this was a load from or store to memory, compare
3567 the MEM in recog_data.operand to the one in the insn.
3568 If they are not equal, then rerecognize the insn. */
3569 || (old_set != 0
3570 && ((MEM_P (SET_SRC (old_set))
3571 && SET_SRC (old_set) != recog_data.operand[1])
3572 || (MEM_P (SET_DEST (old_set))
3573 && SET_DEST (old_set) != recog_data.operand[0])))
3574 /* If this was an add insn before, rerecognize. */
3575 || GET_CODE (SET_SRC (old_set)) == PLUS))
3576 {
3577 int new_icode = recog (PATTERN (insn), insn, 0);
3578 if (new_icode >= 0)
3579 INSN_CODE (insn) = new_icode;
3580 }
3581 }
3582
3583 /* Restore the old body. If there were any changes to it, we made a copy
3584 of it while the changes were still in place, so we'll correctly return
3585 a modified insn below. */
3586 if (! replace)
3587 {
3588 /* Restore the old body. */
3589 for (i = 0; i < recog_data.n_operands; i++)
3590 /* Restoring a top-level match_parallel would clobber the new_body
3591 we installed in the insn. */
3592 if (recog_data.operand_loc[i] != &PATTERN (insn))
3593 *recog_data.operand_loc[i] = orig_operand[i];
3594 for (i = 0; i < recog_data.n_dups; i++)
3595 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3596 }
3597
3598 /* Update all elimination pairs to reflect the status after the current
3599 insn. The changes we make were determined by the earlier call to
3600 elimination_effects.
3601
3602 We also detect cases where register elimination cannot be done,
3603 namely, if a register would be both changed and referenced outside a MEM
3604 in the resulting insn since such an insn is often undefined and, even if
3605 not, we cannot know what meaning will be given to it. Note that it is
3606 valid to have a register used in an address in an insn that changes it
3607 (presumably with a pre- or post-increment or decrement).
3608
3609 If anything changes, return nonzero. */
3610
3611 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3612 {
3613 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3614 ep->can_eliminate = 0;
3615
3616 ep->ref_outside_mem = 0;
3617
3618 if (ep->previous_offset != ep->offset)
3619 val = 1;
3620 }
3621
3622 done:
3623 /* If we changed something, perform elimination in REG_NOTES. This is
3624 needed even when REPLACE is zero because a REG_DEAD note might refer
3625 to a register that we eliminate and could cause a different number
3626 of spill registers to be needed in the final reload pass than in
3627 the pre-passes. */
3628 if (val && REG_NOTES (insn) != 0)
3629 REG_NOTES (insn)
3630 = eliminate_regs_1 (REG_NOTES (insn), VOIDmode, REG_NOTES (insn), true,
3631 false);
3632
3633 return val;
3634 }
3635
3636 /* Like eliminate_regs_in_insn, but only estimate costs for the use of the
3637 register allocator. INSN is the instruction we need to examine, we perform
3638 eliminations in its operands and record cases where eliminating a reg with
3639 an invariant equivalence would add extra cost. */
3640
3641 static void
3642 elimination_costs_in_insn (rtx insn)
3643 {
3644 int icode = recog_memoized (insn);
3645 rtx old_body = PATTERN (insn);
3646 int insn_is_asm = asm_noperands (old_body) >= 0;
3647 rtx old_set = single_set (insn);
3648 int i;
3649 rtx orig_operand[MAX_RECOG_OPERANDS];
3650 rtx orig_dup[MAX_RECOG_OPERANDS];
3651 struct elim_table *ep;
3652 rtx plus_src, plus_cst_src;
3653 bool sets_reg_p;
3654
3655 if (! insn_is_asm && icode < 0)
3656 {
3657 gcc_assert (GET_CODE (PATTERN (insn)) == USE
3658 || GET_CODE (PATTERN (insn)) == CLOBBER
3659 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3660 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3661 || GET_CODE (PATTERN (insn)) == ASM_INPUT
3662 || DEBUG_INSN_P (insn));
3663 return;
3664 }
3665
3666 if (old_set != 0 && REG_P (SET_DEST (old_set))
3667 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3668 {
3669 /* Check for setting an eliminable register. */
3670 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3671 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3672 return;
3673 }
3674
3675 /* We allow one special case which happens to work on all machines we
3676 currently support: a single set with the source or a REG_EQUAL
3677 note being a PLUS of an eliminable register and a constant. */
3678 plus_src = plus_cst_src = 0;
3679 sets_reg_p = false;
3680 if (old_set && REG_P (SET_DEST (old_set)))
3681 {
3682 sets_reg_p = true;
3683 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3684 plus_src = SET_SRC (old_set);
3685 /* First see if the source is of the form (plus (...) CST). */
3686 if (plus_src
3687 && CONST_INT_P (XEXP (plus_src, 1)))
3688 plus_cst_src = plus_src;
3689 else if (REG_P (SET_SRC (old_set))
3690 || plus_src)
3691 {
3692 /* Otherwise, see if we have a REG_EQUAL note of the form
3693 (plus (...) CST). */
3694 rtx links;
3695 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3696 {
3697 if ((REG_NOTE_KIND (links) == REG_EQUAL
3698 || REG_NOTE_KIND (links) == REG_EQUIV)
3699 && GET_CODE (XEXP (links, 0)) == PLUS
3700 && CONST_INT_P (XEXP (XEXP (links, 0), 1)))
3701 {
3702 plus_cst_src = XEXP (links, 0);
3703 break;
3704 }
3705 }
3706 }
3707 }
3708
3709 /* Determine the effects of this insn on elimination offsets. */
3710 elimination_effects (old_body, VOIDmode);
3711
3712 /* Eliminate all eliminable registers occurring in operands that
3713 can be handled by reload. */
3714 extract_insn (insn);
3715 for (i = 0; i < recog_data.n_dups; i++)
3716 orig_dup[i] = *recog_data.dup_loc[i];
3717
3718 for (i = 0; i < recog_data.n_operands; i++)
3719 {
3720 orig_operand[i] = recog_data.operand[i];
3721
3722 /* For an asm statement, every operand is eliminable. */
3723 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3724 {
3725 bool is_set_src, in_plus;
3726
3727 /* Check for setting a register that we know about. */
3728 if (recog_data.operand_type[i] != OP_IN
3729 && REG_P (orig_operand[i]))
3730 {
3731 /* If we are assigning to a register that can be eliminated, it
3732 must be as part of a PARALLEL, since the code above handles
3733 single SETs. We must indicate that we can no longer
3734 eliminate this reg. */
3735 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3736 ep++)
3737 if (ep->from_rtx == orig_operand[i])
3738 ep->can_eliminate = 0;
3739 }
3740
3741 /* Companion to the above plus substitution, we can allow
3742 invariants as the source of a plain move. */
3743 is_set_src = false;
3744 if (old_set && recog_data.operand_loc[i] == &SET_SRC (old_set))
3745 is_set_src = true;
3746 if (is_set_src && !sets_reg_p)
3747 note_reg_elim_costly (&SET_SRC (old_set), insn);
3748 in_plus = false;
3749 if (plus_src && sets_reg_p
3750 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3751 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3752 in_plus = true;
3753
3754 eliminate_regs_1 (recog_data.operand[i], VOIDmode,
3755 NULL_RTX,
3756 is_set_src || in_plus, true);
3757 /* Terminate the search in check_eliminable_occurrences at
3758 this point. */
3759 *recog_data.operand_loc[i] = 0;
3760 }
3761 }
3762
3763 for (i = 0; i < recog_data.n_dups; i++)
3764 *recog_data.dup_loc[i]
3765 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3766
3767 /* If any eliminable remain, they aren't eliminable anymore. */
3768 check_eliminable_occurrences (old_body);
3769
3770 /* Restore the old body. */
3771 for (i = 0; i < recog_data.n_operands; i++)
3772 *recog_data.operand_loc[i] = orig_operand[i];
3773 for (i = 0; i < recog_data.n_dups; i++)
3774 *recog_data.dup_loc[i] = orig_dup[i];
3775
3776 /* Update all elimination pairs to reflect the status after the current
3777 insn. The changes we make were determined by the earlier call to
3778 elimination_effects. */
3779
3780 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3781 {
3782 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3783 ep->can_eliminate = 0;
3784
3785 ep->ref_outside_mem = 0;
3786 }
3787
3788 return;
3789 }
3790
3791 /* Loop through all elimination pairs.
3792 Recalculate the number not at initial offset.
3793
3794 Compute the maximum offset (minimum offset if the stack does not
3795 grow downward) for each elimination pair. */
3796
3797 static void
3798 update_eliminable_offsets (void)
3799 {
3800 struct elim_table *ep;
3801
3802 num_not_at_initial_offset = 0;
3803 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3804 {
3805 ep->previous_offset = ep->offset;
3806 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3807 num_not_at_initial_offset++;
3808 }
3809 }
3810
3811 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3812 replacement we currently believe is valid, mark it as not eliminable if X
3813 modifies DEST in any way other than by adding a constant integer to it.
3814
3815 If DEST is the frame pointer, we do nothing because we assume that
3816 all assignments to the hard frame pointer are nonlocal gotos and are being
3817 done at a time when they are valid and do not disturb anything else.
3818 Some machines want to eliminate a fake argument pointer with either the
3819 frame or stack pointer. Assignments to the hard frame pointer must not
3820 prevent this elimination.
3821
3822 Called via note_stores from reload before starting its passes to scan
3823 the insns of the function. */
3824
3825 static void
3826 mark_not_eliminable (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
3827 {
3828 unsigned int i;
3829
3830 /* A SUBREG of a hard register here is just changing its mode. We should
3831 not see a SUBREG of an eliminable hard register, but check just in
3832 case. */
3833 if (GET_CODE (dest) == SUBREG)
3834 dest = SUBREG_REG (dest);
3835
3836 if (dest == hard_frame_pointer_rtx)
3837 return;
3838
3839 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3840 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3841 && (GET_CODE (x) != SET
3842 || GET_CODE (SET_SRC (x)) != PLUS
3843 || XEXP (SET_SRC (x), 0) != dest
3844 || !CONST_INT_P (XEXP (SET_SRC (x), 1))))
3845 {
3846 reg_eliminate[i].can_eliminate_previous
3847 = reg_eliminate[i].can_eliminate = 0;
3848 num_eliminable--;
3849 }
3850 }
3851
3852 /* Verify that the initial elimination offsets did not change since the
3853 last call to set_initial_elim_offsets. This is used to catch cases
3854 where something illegal happened during reload_as_needed that could
3855 cause incorrect code to be generated if we did not check for it. */
3856
3857 static bool
3858 verify_initial_elim_offsets (void)
3859 {
3860 HOST_WIDE_INT t;
3861
3862 if (!num_eliminable)
3863 return true;
3864
3865 #ifdef ELIMINABLE_REGS
3866 {
3867 struct elim_table *ep;
3868
3869 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3870 {
3871 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3872 if (t != ep->initial_offset)
3873 return false;
3874 }
3875 }
3876 #else
3877 INITIAL_FRAME_POINTER_OFFSET (t);
3878 if (t != reg_eliminate[0].initial_offset)
3879 return false;
3880 #endif
3881
3882 return true;
3883 }
3884
3885 /* Reset all offsets on eliminable registers to their initial values. */
3886
3887 static void
3888 set_initial_elim_offsets (void)
3889 {
3890 struct elim_table *ep = reg_eliminate;
3891
3892 #ifdef ELIMINABLE_REGS
3893 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3894 {
3895 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3896 ep->previous_offset = ep->offset = ep->initial_offset;
3897 }
3898 #else
3899 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3900 ep->previous_offset = ep->offset = ep->initial_offset;
3901 #endif
3902
3903 num_not_at_initial_offset = 0;
3904 }
3905
3906 /* Subroutine of set_initial_label_offsets called via for_each_eh_label. */
3907
3908 static void
3909 set_initial_eh_label_offset (rtx label)
3910 {
3911 set_label_offsets (label, NULL_RTX, 1);
3912 }
3913
3914 /* Initialize the known label offsets.
3915 Set a known offset for each forced label to be at the initial offset
3916 of each elimination. We do this because we assume that all
3917 computed jumps occur from a location where each elimination is
3918 at its initial offset.
3919 For all other labels, show that we don't know the offsets. */
3920
3921 static void
3922 set_initial_label_offsets (void)
3923 {
3924 rtx x;
3925 memset (offsets_known_at, 0, num_labels);
3926
3927 for (x = forced_labels; x; x = XEXP (x, 1))
3928 if (XEXP (x, 0))
3929 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3930
3931 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
3932 if (XEXP (x, 0))
3933 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3934
3935 for_each_eh_label (set_initial_eh_label_offset);
3936 }
3937
3938 /* Set all elimination offsets to the known values for the code label given
3939 by INSN. */
3940
3941 static void
3942 set_offsets_for_label (rtx insn)
3943 {
3944 unsigned int i;
3945 int label_nr = CODE_LABEL_NUMBER (insn);
3946 struct elim_table *ep;
3947
3948 num_not_at_initial_offset = 0;
3949 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3950 {
3951 ep->offset = ep->previous_offset
3952 = offsets_at[label_nr - first_label_num][i];
3953 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3954 num_not_at_initial_offset++;
3955 }
3956 }
3957
3958 /* See if anything that happened changes which eliminations are valid.
3959 For example, on the SPARC, whether or not the frame pointer can
3960 be eliminated can depend on what registers have been used. We need
3961 not check some conditions again (such as flag_omit_frame_pointer)
3962 since they can't have changed. */
3963
3964 static void
3965 update_eliminables (HARD_REG_SET *pset)
3966 {
3967 int previous_frame_pointer_needed = frame_pointer_needed;
3968 struct elim_table *ep;
3969
3970 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3971 if ((ep->from == HARD_FRAME_POINTER_REGNUM
3972 && targetm.frame_pointer_required ())
3973 #ifdef ELIMINABLE_REGS
3974 || ! targetm.can_eliminate (ep->from, ep->to)
3975 #endif
3976 )
3977 ep->can_eliminate = 0;
3978
3979 /* Look for the case where we have discovered that we can't replace
3980 register A with register B and that means that we will now be
3981 trying to replace register A with register C. This means we can
3982 no longer replace register C with register B and we need to disable
3983 such an elimination, if it exists. This occurs often with A == ap,
3984 B == sp, and C == fp. */
3985
3986 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3987 {
3988 struct elim_table *op;
3989 int new_to = -1;
3990
3991 if (! ep->can_eliminate && ep->can_eliminate_previous)
3992 {
3993 /* Find the current elimination for ep->from, if there is a
3994 new one. */
3995 for (op = reg_eliminate;
3996 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3997 if (op->from == ep->from && op->can_eliminate)
3998 {
3999 new_to = op->to;
4000 break;
4001 }
4002
4003 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
4004 disable it. */
4005 for (op = reg_eliminate;
4006 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
4007 if (op->from == new_to && op->to == ep->to)
4008 op->can_eliminate = 0;
4009 }
4010 }
4011
4012 /* See if any registers that we thought we could eliminate the previous
4013 time are no longer eliminable. If so, something has changed and we
4014 must spill the register. Also, recompute the number of eliminable
4015 registers and see if the frame pointer is needed; it is if there is
4016 no elimination of the frame pointer that we can perform. */
4017
4018 frame_pointer_needed = 1;
4019 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
4020 {
4021 if (ep->can_eliminate
4022 && ep->from == FRAME_POINTER_REGNUM
4023 && ep->to != HARD_FRAME_POINTER_REGNUM
4024 && (! SUPPORTS_STACK_ALIGNMENT
4025 || ! crtl->stack_realign_needed))
4026 frame_pointer_needed = 0;
4027
4028 if (! ep->can_eliminate && ep->can_eliminate_previous)
4029 {
4030 ep->can_eliminate_previous = 0;
4031 SET_HARD_REG_BIT (*pset, ep->from);
4032 num_eliminable--;
4033 }
4034 }
4035
4036 /* If we didn't need a frame pointer last time, but we do now, spill
4037 the hard frame pointer. */
4038 if (frame_pointer_needed && ! previous_frame_pointer_needed)
4039 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
4040 }
4041
4042 /* Return true if X is used as the target register of an elimination. */
4043
4044 bool
4045 elimination_target_reg_p (rtx x)
4046 {
4047 struct elim_table *ep;
4048
4049 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
4050 if (ep->to_rtx == x && ep->can_eliminate)
4051 return true;
4052
4053 return false;
4054 }
4055
4056 /* Initialize the table of registers to eliminate.
4057 Pre-condition: global flag frame_pointer_needed has been set before
4058 calling this function. */
4059
4060 static void
4061 init_elim_table (void)
4062 {
4063 struct elim_table *ep;
4064 #ifdef ELIMINABLE_REGS
4065 const struct elim_table_1 *ep1;
4066 #endif
4067
4068 if (!reg_eliminate)
4069 reg_eliminate = XCNEWVEC (struct elim_table, NUM_ELIMINABLE_REGS);
4070
4071 num_eliminable = 0;
4072
4073 #ifdef ELIMINABLE_REGS
4074 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
4075 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
4076 {
4077 ep->from = ep1->from;
4078 ep->to = ep1->to;
4079 ep->can_eliminate = ep->can_eliminate_previous
4080 = (targetm.can_eliminate (ep->from, ep->to)
4081 && ! (ep->to == STACK_POINTER_REGNUM
4082 && frame_pointer_needed
4083 && (! SUPPORTS_STACK_ALIGNMENT
4084 || ! stack_realign_fp)));
4085 }
4086 #else
4087 reg_eliminate[0].from = reg_eliminate_1[0].from;
4088 reg_eliminate[0].to = reg_eliminate_1[0].to;
4089 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
4090 = ! frame_pointer_needed;
4091 #endif
4092
4093 /* Count the number of eliminable registers and build the FROM and TO
4094 REG rtx's. Note that code in gen_rtx_REG will cause, e.g.,
4095 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
4096 We depend on this. */
4097 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
4098 {
4099 num_eliminable += ep->can_eliminate;
4100 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
4101 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
4102 }
4103 }
4104
4105 /* Find all the pseudo registers that didn't get hard regs
4106 but do have known equivalent constants or memory slots.
4107 These include parameters (known equivalent to parameter slots)
4108 and cse'd or loop-moved constant memory addresses.
4109
4110 Record constant equivalents in reg_equiv_constant
4111 so they will be substituted by find_reloads.
4112 Record memory equivalents in reg_mem_equiv so they can
4113 be substituted eventually by altering the REG-rtx's. */
4114
4115 static void
4116 init_eliminable_invariants (rtx first, bool do_subregs)
4117 {
4118 int i;
4119 rtx insn;
4120
4121 grow_reg_equivs ();
4122 if (do_subregs)
4123 reg_max_ref_width = XCNEWVEC (unsigned int, max_regno);
4124 else
4125 reg_max_ref_width = NULL;
4126
4127 num_eliminable_invariants = 0;
4128
4129 first_label_num = get_first_label_num ();
4130 num_labels = max_label_num () - first_label_num;
4131
4132 /* Allocate the tables used to store offset information at labels. */
4133 offsets_known_at = XNEWVEC (char, num_labels);
4134 offsets_at = (HOST_WIDE_INT (*)[NUM_ELIMINABLE_REGS]) xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (HOST_WIDE_INT));
4135
4136 /* Look for REG_EQUIV notes; record what each pseudo is equivalent
4137 to. If DO_SUBREGS is true, also find all paradoxical subregs and
4138 find largest such for each pseudo. FIRST is the head of the insn
4139 list. */
4140
4141 for (insn = first; insn; insn = NEXT_INSN (insn))
4142 {
4143 rtx set = single_set (insn);
4144
4145 /* We may introduce USEs that we want to remove at the end, so
4146 we'll mark them with QImode. Make sure there are no
4147 previously-marked insns left by say regmove. */
4148 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
4149 && GET_MODE (insn) != VOIDmode)
4150 PUT_MODE (insn, VOIDmode);
4151
4152 if (do_subregs && NONDEBUG_INSN_P (insn))
4153 scan_paradoxical_subregs (PATTERN (insn));
4154
4155 if (set != 0 && REG_P (SET_DEST (set)))
4156 {
4157 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
4158 rtx x;
4159
4160 if (! note)
4161 continue;
4162
4163 i = REGNO (SET_DEST (set));
4164 x = XEXP (note, 0);
4165
4166 if (i <= LAST_VIRTUAL_REGISTER)
4167 continue;
4168
4169 /* If flag_pic and we have constant, verify it's legitimate. */
4170 if (!CONSTANT_P (x)
4171 || !flag_pic || LEGITIMATE_PIC_OPERAND_P (x))
4172 {
4173 /* It can happen that a REG_EQUIV note contains a MEM
4174 that is not a legitimate memory operand. As later
4175 stages of reload assume that all addresses found
4176 in the reg_equiv_* arrays were originally legitimate,
4177 we ignore such REG_EQUIV notes. */
4178 if (memory_operand (x, VOIDmode))
4179 {
4180 /* Always unshare the equivalence, so we can
4181 substitute into this insn without touching the
4182 equivalence. */
4183 reg_equiv_memory_loc (i) = copy_rtx (x);
4184 }
4185 else if (function_invariant_p (x))
4186 {
4187 enum machine_mode mode;
4188
4189 mode = GET_MODE (SET_DEST (set));
4190 if (GET_CODE (x) == PLUS)
4191 {
4192 /* This is PLUS of frame pointer and a constant,
4193 and might be shared. Unshare it. */
4194 reg_equiv_invariant (i) = copy_rtx (x);
4195 num_eliminable_invariants++;
4196 }
4197 else if (x == frame_pointer_rtx || x == arg_pointer_rtx)
4198 {
4199 reg_equiv_invariant (i) = x;
4200 num_eliminable_invariants++;
4201 }
4202 else if (targetm.legitimate_constant_p (mode, x))
4203 reg_equiv_constant (i) = x;
4204 else
4205 {
4206 reg_equiv_memory_loc (i) = force_const_mem (mode, x);
4207 if (! reg_equiv_memory_loc (i))
4208 reg_equiv_init (i) = NULL_RTX;
4209 }
4210 }
4211 else
4212 {
4213 reg_equiv_init (i) = NULL_RTX;
4214 continue;
4215 }
4216 }
4217 else
4218 reg_equiv_init (i) = NULL_RTX;
4219 }
4220 }
4221
4222 if (dump_file)
4223 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4224 if (reg_equiv_init (i))
4225 {
4226 fprintf (dump_file, "init_insns for %u: ", i);
4227 print_inline_rtx (dump_file, reg_equiv_init (i), 20);
4228 fprintf (dump_file, "\n");
4229 }
4230 }
4231
4232 /* Indicate that we no longer have known memory locations or constants.
4233 Free all data involved in tracking these. */
4234
4235 static void
4236 free_reg_equiv (void)
4237 {
4238 int i;
4239
4240
4241 free (offsets_known_at);
4242 free (offsets_at);
4243 offsets_at = 0;
4244 offsets_known_at = 0;
4245
4246 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4247 if (reg_equiv_alt_mem_list (i))
4248 free_EXPR_LIST_list (&reg_equiv_alt_mem_list (i));
4249 VEC_free (reg_equivs_t, gc, reg_equivs);
4250 reg_equivs = NULL;
4251
4252 }
4253 \f
4254 /* Kick all pseudos out of hard register REGNO.
4255
4256 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
4257 because we found we can't eliminate some register. In the case, no pseudos
4258 are allowed to be in the register, even if they are only in a block that
4259 doesn't require spill registers, unlike the case when we are spilling this
4260 hard reg to produce another spill register.
4261
4262 Return nonzero if any pseudos needed to be kicked out. */
4263
4264 static void
4265 spill_hard_reg (unsigned int regno, int cant_eliminate)
4266 {
4267 int i;
4268
4269 if (cant_eliminate)
4270 {
4271 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
4272 df_set_regs_ever_live (regno, true);
4273 }
4274
4275 /* Spill every pseudo reg that was allocated to this reg
4276 or to something that overlaps this reg. */
4277
4278 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4279 if (reg_renumber[i] >= 0
4280 && (unsigned int) reg_renumber[i] <= regno
4281 && end_hard_regno (PSEUDO_REGNO_MODE (i), reg_renumber[i]) > regno)
4282 SET_REGNO_REG_SET (&spilled_pseudos, i);
4283 }
4284
4285 /* After find_reload_regs has been run for all insn that need reloads,
4286 and/or spill_hard_regs was called, this function is used to actually
4287 spill pseudo registers and try to reallocate them. It also sets up the
4288 spill_regs array for use by choose_reload_regs. */
4289
4290 static int
4291 finish_spills (int global)
4292 {
4293 struct insn_chain *chain;
4294 int something_changed = 0;
4295 unsigned i;
4296 reg_set_iterator rsi;
4297
4298 /* Build the spill_regs array for the function. */
4299 /* If there are some registers still to eliminate and one of the spill regs
4300 wasn't ever used before, additional stack space may have to be
4301 allocated to store this register. Thus, we may have changed the offset
4302 between the stack and frame pointers, so mark that something has changed.
4303
4304 One might think that we need only set VAL to 1 if this is a call-used
4305 register. However, the set of registers that must be saved by the
4306 prologue is not identical to the call-used set. For example, the
4307 register used by the call insn for the return PC is a call-used register,
4308 but must be saved by the prologue. */
4309
4310 n_spills = 0;
4311 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4312 if (TEST_HARD_REG_BIT (used_spill_regs, i))
4313 {
4314 spill_reg_order[i] = n_spills;
4315 spill_regs[n_spills++] = i;
4316 if (num_eliminable && ! df_regs_ever_live_p (i))
4317 something_changed = 1;
4318 df_set_regs_ever_live (i, true);
4319 }
4320 else
4321 spill_reg_order[i] = -1;
4322
4323 EXECUTE_IF_SET_IN_REG_SET (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i, rsi)
4324 if (! ira_conflicts_p || reg_renumber[i] >= 0)
4325 {
4326 /* Record the current hard register the pseudo is allocated to
4327 in pseudo_previous_regs so we avoid reallocating it to the
4328 same hard reg in a later pass. */
4329 gcc_assert (reg_renumber[i] >= 0);
4330
4331 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
4332 /* Mark it as no longer having a hard register home. */
4333 reg_renumber[i] = -1;
4334 if (ira_conflicts_p)
4335 /* Inform IRA about the change. */
4336 ira_mark_allocation_change (i);
4337 /* We will need to scan everything again. */
4338 something_changed = 1;
4339 }
4340
4341 /* Retry global register allocation if possible. */
4342 if (global && ira_conflicts_p)
4343 {
4344 unsigned int n;
4345
4346 memset (pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
4347 /* For every insn that needs reloads, set the registers used as spill
4348 regs in pseudo_forbidden_regs for every pseudo live across the
4349 insn. */
4350 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
4351 {
4352 EXECUTE_IF_SET_IN_REG_SET
4353 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
4354 {
4355 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
4356 chain->used_spill_regs);
4357 }
4358 EXECUTE_IF_SET_IN_REG_SET
4359 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
4360 {
4361 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
4362 chain->used_spill_regs);
4363 }
4364 }
4365
4366 /* Retry allocating the pseudos spilled in IRA and the
4367 reload. For each reg, merge the various reg sets that
4368 indicate which hard regs can't be used, and call
4369 ira_reassign_pseudos. */
4370 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
4371 if (reg_old_renumber[i] != reg_renumber[i])
4372 {
4373 if (reg_renumber[i] < 0)
4374 temp_pseudo_reg_arr[n++] = i;
4375 else
4376 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
4377 }
4378 if (ira_reassign_pseudos (temp_pseudo_reg_arr, n,
4379 bad_spill_regs_global,
4380 pseudo_forbidden_regs, pseudo_previous_regs,
4381 &spilled_pseudos))
4382 something_changed = 1;
4383 }
4384 /* Fix up the register information in the insn chain.
4385 This involves deleting those of the spilled pseudos which did not get
4386 a new hard register home from the live_{before,after} sets. */
4387 for (chain = reload_insn_chain; chain; chain = chain->next)
4388 {
4389 HARD_REG_SET used_by_pseudos;
4390 HARD_REG_SET used_by_pseudos2;
4391
4392 if (! ira_conflicts_p)
4393 {
4394 /* Don't do it for IRA because IRA and the reload still can
4395 assign hard registers to the spilled pseudos on next
4396 reload iterations. */
4397 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
4398 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
4399 }
4400 /* Mark any unallocated hard regs as available for spills. That
4401 makes inheritance work somewhat better. */
4402 if (chain->need_reload)
4403 {
4404 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
4405 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
4406 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
4407
4408 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
4409 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
4410 /* Value of chain->used_spill_regs from previous iteration
4411 may be not included in the value calculated here because
4412 of possible removing caller-saves insns (see function
4413 delete_caller_save_insns. */
4414 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
4415 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
4416 }
4417 }
4418
4419 CLEAR_REG_SET (&changed_allocation_pseudos);
4420 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
4421 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
4422 {
4423 int regno = reg_renumber[i];
4424 if (reg_old_renumber[i] == regno)
4425 continue;
4426
4427 SET_REGNO_REG_SET (&changed_allocation_pseudos, i);
4428
4429 alter_reg (i, reg_old_renumber[i], false);
4430 reg_old_renumber[i] = regno;
4431 if (dump_file)
4432 {
4433 if (regno == -1)
4434 fprintf (dump_file, " Register %d now on stack.\n\n", i);
4435 else
4436 fprintf (dump_file, " Register %d now in %d.\n\n",
4437 i, reg_renumber[i]);
4438 }
4439 }
4440
4441 return something_changed;
4442 }
4443 \f
4444 /* Find all paradoxical subregs within X and update reg_max_ref_width. */
4445
4446 static void
4447 scan_paradoxical_subregs (rtx x)
4448 {
4449 int i;
4450 const char *fmt;
4451 enum rtx_code code = GET_CODE (x);
4452
4453 switch (code)
4454 {
4455 case REG:
4456 case CONST_INT:
4457 case CONST:
4458 case SYMBOL_REF:
4459 case LABEL_REF:
4460 case CONST_DOUBLE:
4461 case CONST_FIXED:
4462 case CONST_VECTOR: /* shouldn't happen, but just in case. */
4463 case CC0:
4464 case PC:
4465 case USE:
4466 case CLOBBER:
4467 return;
4468
4469 case SUBREG:
4470 if (REG_P (SUBREG_REG (x))
4471 && (GET_MODE_SIZE (GET_MODE (x))
4472 > reg_max_ref_width[REGNO (SUBREG_REG (x))]))
4473 {
4474 reg_max_ref_width[REGNO (SUBREG_REG (x))]
4475 = GET_MODE_SIZE (GET_MODE (x));
4476 mark_home_live_1 (REGNO (SUBREG_REG (x)), GET_MODE (x));
4477 }
4478 return;
4479
4480 default:
4481 break;
4482 }
4483
4484 fmt = GET_RTX_FORMAT (code);
4485 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4486 {
4487 if (fmt[i] == 'e')
4488 scan_paradoxical_subregs (XEXP (x, i));
4489 else if (fmt[i] == 'E')
4490 {
4491 int j;
4492 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4493 scan_paradoxical_subregs (XVECEXP (x, i, j));
4494 }
4495 }
4496 }
4497
4498 /* *OP_PTR and *OTHER_PTR are two operands to a conceptual reload.
4499 If *OP_PTR is a paradoxical subreg, try to remove that subreg
4500 and apply the corresponding narrowing subreg to *OTHER_PTR.
4501 Return true if the operands were changed, false otherwise. */
4502
4503 static bool
4504 strip_paradoxical_subreg (rtx *op_ptr, rtx *other_ptr)
4505 {
4506 rtx op, inner, other, tem;
4507
4508 op = *op_ptr;
4509 if (!paradoxical_subreg_p (op))
4510 return false;
4511 inner = SUBREG_REG (op);
4512
4513 other = *other_ptr;
4514 tem = gen_lowpart_common (GET_MODE (inner), other);
4515 if (!tem)
4516 return false;
4517
4518 /* If the lowpart operation turned a hard register into a subreg,
4519 rather than simplifying it to another hard register, then the
4520 mode change cannot be properly represented. For example, OTHER
4521 might be valid in its current mode, but not in the new one. */
4522 if (GET_CODE (tem) == SUBREG
4523 && REG_P (other)
4524 && HARD_REGISTER_P (other))
4525 return false;
4526
4527 *op_ptr = inner;
4528 *other_ptr = tem;
4529 return true;
4530 }
4531 \f
4532 /* A subroutine of reload_as_needed. If INSN has a REG_EH_REGION note,
4533 examine all of the reload insns between PREV and NEXT exclusive, and
4534 annotate all that may trap. */
4535
4536 static void
4537 fixup_eh_region_note (rtx insn, rtx prev, rtx next)
4538 {
4539 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
4540 if (note == NULL)
4541 return;
4542 if (!insn_could_throw_p (insn))
4543 remove_note (insn, note);
4544 copy_reg_eh_region_note_forward (note, NEXT_INSN (prev), next);
4545 }
4546
4547 /* Reload pseudo-registers into hard regs around each insn as needed.
4548 Additional register load insns are output before the insn that needs it
4549 and perhaps store insns after insns that modify the reloaded pseudo reg.
4550
4551 reg_last_reload_reg and reg_reloaded_contents keep track of
4552 which registers are already available in reload registers.
4553 We update these for the reloads that we perform,
4554 as the insns are scanned. */
4555
4556 static void
4557 reload_as_needed (int live_known)
4558 {
4559 struct insn_chain *chain;
4560 #if defined (AUTO_INC_DEC)
4561 int i;
4562 #endif
4563 rtx x, marker;
4564
4565 memset (spill_reg_rtx, 0, sizeof spill_reg_rtx);
4566 memset (spill_reg_store, 0, sizeof spill_reg_store);
4567 reg_last_reload_reg = XCNEWVEC (rtx, max_regno);
4568 INIT_REG_SET (&reg_has_output_reload);
4569 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4570 CLEAR_HARD_REG_SET (reg_reloaded_call_part_clobbered);
4571
4572 set_initial_elim_offsets ();
4573
4574 /* Generate a marker insn that we will move around. */
4575 marker = emit_note (NOTE_INSN_DELETED);
4576 unlink_insn_chain (marker, marker);
4577
4578 for (chain = reload_insn_chain; chain; chain = chain->next)
4579 {
4580 rtx prev = 0;
4581 rtx insn = chain->insn;
4582 rtx old_next = NEXT_INSN (insn);
4583 #ifdef AUTO_INC_DEC
4584 rtx old_prev = PREV_INSN (insn);
4585 #endif
4586
4587 /* If we pass a label, copy the offsets from the label information
4588 into the current offsets of each elimination. */
4589 if (LABEL_P (insn))
4590 set_offsets_for_label (insn);
4591
4592 else if (INSN_P (insn))
4593 {
4594 regset_head regs_to_forget;
4595 INIT_REG_SET (&regs_to_forget);
4596 note_stores (PATTERN (insn), forget_old_reloads_1, &regs_to_forget);
4597
4598 /* If this is a USE and CLOBBER of a MEM, ensure that any
4599 references to eliminable registers have been removed. */
4600
4601 if ((GET_CODE (PATTERN (insn)) == USE
4602 || GET_CODE (PATTERN (insn)) == CLOBBER)
4603 && MEM_P (XEXP (PATTERN (insn), 0)))
4604 XEXP (XEXP (PATTERN (insn), 0), 0)
4605 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
4606 GET_MODE (XEXP (PATTERN (insn), 0)),
4607 NULL_RTX);
4608
4609 /* If we need to do register elimination processing, do so.
4610 This might delete the insn, in which case we are done. */
4611 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
4612 {
4613 eliminate_regs_in_insn (insn, 1);
4614 if (NOTE_P (insn))
4615 {
4616 update_eliminable_offsets ();
4617 CLEAR_REG_SET (&regs_to_forget);
4618 continue;
4619 }
4620 }
4621
4622 /* If need_elim is nonzero but need_reload is zero, one might think
4623 that we could simply set n_reloads to 0. However, find_reloads
4624 could have done some manipulation of the insn (such as swapping
4625 commutative operands), and these manipulations are lost during
4626 the first pass for every insn that needs register elimination.
4627 So the actions of find_reloads must be redone here. */
4628
4629 if (! chain->need_elim && ! chain->need_reload
4630 && ! chain->need_operand_change)
4631 n_reloads = 0;
4632 /* First find the pseudo regs that must be reloaded for this insn.
4633 This info is returned in the tables reload_... (see reload.h).
4634 Also modify the body of INSN by substituting RELOAD
4635 rtx's for those pseudo regs. */
4636 else
4637 {
4638 CLEAR_REG_SET (&reg_has_output_reload);
4639 CLEAR_HARD_REG_SET (reg_is_output_reload);
4640
4641 find_reloads (insn, 1, spill_indirect_levels, live_known,
4642 spill_reg_order);
4643 }
4644
4645 if (n_reloads > 0)
4646 {
4647 rtx next = NEXT_INSN (insn);
4648 rtx p;
4649
4650 /* ??? PREV can get deleted by reload inheritance.
4651 Work around this by emitting a marker note. */
4652 prev = PREV_INSN (insn);
4653 reorder_insns_nobb (marker, marker, prev);
4654
4655 /* Now compute which reload regs to reload them into. Perhaps
4656 reusing reload regs from previous insns, or else output
4657 load insns to reload them. Maybe output store insns too.
4658 Record the choices of reload reg in reload_reg_rtx. */
4659 choose_reload_regs (chain);
4660
4661 /* Generate the insns to reload operands into or out of
4662 their reload regs. */
4663 emit_reload_insns (chain);
4664
4665 /* Substitute the chosen reload regs from reload_reg_rtx
4666 into the insn's body (or perhaps into the bodies of other
4667 load and store insn that we just made for reloading
4668 and that we moved the structure into). */
4669 subst_reloads (insn);
4670
4671 prev = PREV_INSN (marker);
4672 unlink_insn_chain (marker, marker);
4673
4674 /* Adjust the exception region notes for loads and stores. */
4675 if (cfun->can_throw_non_call_exceptions && !CALL_P (insn))
4676 fixup_eh_region_note (insn, prev, next);
4677
4678 /* Adjust the location of REG_ARGS_SIZE. */
4679 p = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4680 if (p)
4681 {
4682 remove_note (insn, p);
4683 fixup_args_size_notes (prev, PREV_INSN (next),
4684 INTVAL (XEXP (p, 0)));
4685 }
4686
4687 /* If this was an ASM, make sure that all the reload insns
4688 we have generated are valid. If not, give an error
4689 and delete them. */
4690 if (asm_noperands (PATTERN (insn)) >= 0)
4691 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4692 if (p != insn && INSN_P (p)
4693 && GET_CODE (PATTERN (p)) != USE
4694 && (recog_memoized (p) < 0
4695 || (extract_insn (p), ! constrain_operands (1))))
4696 {
4697 error_for_asm (insn,
4698 "%<asm%> operand requires "
4699 "impossible reload");
4700 delete_insn (p);
4701 }
4702 }
4703
4704 if (num_eliminable && chain->need_elim)
4705 update_eliminable_offsets ();
4706
4707 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4708 is no longer validly lying around to save a future reload.
4709 Note that this does not detect pseudos that were reloaded
4710 for this insn in order to be stored in
4711 (obeying register constraints). That is correct; such reload
4712 registers ARE still valid. */
4713 forget_marked_reloads (&regs_to_forget);
4714 CLEAR_REG_SET (&regs_to_forget);
4715
4716 /* There may have been CLOBBER insns placed after INSN. So scan
4717 between INSN and NEXT and use them to forget old reloads. */
4718 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
4719 if (NONJUMP_INSN_P (x) && GET_CODE (PATTERN (x)) == CLOBBER)
4720 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
4721
4722 #ifdef AUTO_INC_DEC
4723 /* Likewise for regs altered by auto-increment in this insn.
4724 REG_INC notes have been changed by reloading:
4725 find_reloads_address_1 records substitutions for them,
4726 which have been performed by subst_reloads above. */
4727 for (i = n_reloads - 1; i >= 0; i--)
4728 {
4729 rtx in_reg = rld[i].in_reg;
4730 if (in_reg)
4731 {
4732 enum rtx_code code = GET_CODE (in_reg);
4733 /* PRE_INC / PRE_DEC will have the reload register ending up
4734 with the same value as the stack slot, but that doesn't
4735 hold true for POST_INC / POST_DEC. Either we have to
4736 convert the memory access to a true POST_INC / POST_DEC,
4737 or we can't use the reload register for inheritance. */
4738 if ((code == POST_INC || code == POST_DEC)
4739 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4740 REGNO (rld[i].reg_rtx))
4741 /* Make sure it is the inc/dec pseudo, and not
4742 some other (e.g. output operand) pseudo. */
4743 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4744 == REGNO (XEXP (in_reg, 0))))
4745
4746 {
4747 rtx reload_reg = rld[i].reg_rtx;
4748 enum machine_mode mode = GET_MODE (reload_reg);
4749 int n = 0;
4750 rtx p;
4751
4752 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4753 {
4754 /* We really want to ignore REG_INC notes here, so
4755 use PATTERN (p) as argument to reg_set_p . */
4756 if (reg_set_p (reload_reg, PATTERN (p)))
4757 break;
4758 n = count_occurrences (PATTERN (p), reload_reg, 0);
4759 if (! n)
4760 continue;
4761 if (n == 1)
4762 {
4763 rtx replace_reg
4764 = gen_rtx_fmt_e (code, mode, reload_reg);
4765
4766 validate_replace_rtx_group (reload_reg,
4767 replace_reg, p);
4768 n = verify_changes (0);
4769
4770 /* We must also verify that the constraints
4771 are met after the replacement. Make sure
4772 extract_insn is only called for an insn
4773 where the replacements were found to be
4774 valid so far. */
4775 if (n)
4776 {
4777 extract_insn (p);
4778 n = constrain_operands (1);
4779 }
4780
4781 /* If the constraints were not met, then
4782 undo the replacement, else confirm it. */
4783 if (!n)
4784 cancel_changes (0);
4785 else
4786 confirm_change_group ();
4787 }
4788 break;
4789 }
4790 if (n == 1)
4791 {
4792 add_reg_note (p, REG_INC, reload_reg);
4793 /* Mark this as having an output reload so that the
4794 REG_INC processing code below won't invalidate
4795 the reload for inheritance. */
4796 SET_HARD_REG_BIT (reg_is_output_reload,
4797 REGNO (reload_reg));
4798 SET_REGNO_REG_SET (&reg_has_output_reload,
4799 REGNO (XEXP (in_reg, 0)));
4800 }
4801 else
4802 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4803 NULL);
4804 }
4805 else if ((code == PRE_INC || code == PRE_DEC)
4806 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4807 REGNO (rld[i].reg_rtx))
4808 /* Make sure it is the inc/dec pseudo, and not
4809 some other (e.g. output operand) pseudo. */
4810 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4811 == REGNO (XEXP (in_reg, 0))))
4812 {
4813 SET_HARD_REG_BIT (reg_is_output_reload,
4814 REGNO (rld[i].reg_rtx));
4815 SET_REGNO_REG_SET (&reg_has_output_reload,
4816 REGNO (XEXP (in_reg, 0)));
4817 }
4818 else if (code == PRE_INC || code == PRE_DEC
4819 || code == POST_INC || code == POST_DEC)
4820 {
4821 int in_regno = REGNO (XEXP (in_reg, 0));
4822
4823 if (reg_last_reload_reg[in_regno] != NULL_RTX)
4824 {
4825 int in_hard_regno;
4826 bool forget_p = true;
4827
4828 in_hard_regno = REGNO (reg_last_reload_reg[in_regno]);
4829 if (TEST_HARD_REG_BIT (reg_reloaded_valid,
4830 in_hard_regno))
4831 {
4832 for (x = old_prev ? NEXT_INSN (old_prev) : insn;
4833 x != old_next;
4834 x = NEXT_INSN (x))
4835 if (x == reg_reloaded_insn[in_hard_regno])
4836 {
4837 forget_p = false;
4838 break;
4839 }
4840 }
4841 /* If for some reasons, we didn't set up
4842 reg_last_reload_reg in this insn,
4843 invalidate inheritance from previous
4844 insns for the incremented/decremented
4845 register. Such registers will be not in
4846 reg_has_output_reload. Invalidate it
4847 also if the corresponding element in
4848 reg_reloaded_insn is also
4849 invalidated. */
4850 if (forget_p)
4851 forget_old_reloads_1 (XEXP (in_reg, 0),
4852 NULL_RTX, NULL);
4853 }
4854 }
4855 }
4856 }
4857 /* If a pseudo that got a hard register is auto-incremented,
4858 we must purge records of copying it into pseudos without
4859 hard registers. */
4860 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4861 if (REG_NOTE_KIND (x) == REG_INC)
4862 {
4863 /* See if this pseudo reg was reloaded in this insn.
4864 If so, its last-reload info is still valid
4865 because it is based on this insn's reload. */
4866 for (i = 0; i < n_reloads; i++)
4867 if (rld[i].out == XEXP (x, 0))
4868 break;
4869
4870 if (i == n_reloads)
4871 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4872 }
4873 #endif
4874 }
4875 /* A reload reg's contents are unknown after a label. */
4876 if (LABEL_P (insn))
4877 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4878
4879 /* Don't assume a reload reg is still good after a call insn
4880 if it is a call-used reg, or if it contains a value that will
4881 be partially clobbered by the call. */
4882 else if (CALL_P (insn))
4883 {
4884 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
4885 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, reg_reloaded_call_part_clobbered);
4886
4887 /* If this is a call to a setjmp-type function, we must not
4888 reuse any reload reg contents across the call; that will
4889 just be clobbered by other uses of the register in later
4890 code, before the longjmp. */
4891 if (find_reg_note (insn, REG_SETJMP, NULL_RTX))
4892 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4893 }
4894 }
4895
4896 /* Clean up. */
4897 free (reg_last_reload_reg);
4898 CLEAR_REG_SET (&reg_has_output_reload);
4899 }
4900
4901 /* Discard all record of any value reloaded from X,
4902 or reloaded in X from someplace else;
4903 unless X is an output reload reg of the current insn.
4904
4905 X may be a hard reg (the reload reg)
4906 or it may be a pseudo reg that was reloaded from.
4907
4908 When DATA is non-NULL just mark the registers in regset
4909 to be forgotten later. */
4910
4911 static void
4912 forget_old_reloads_1 (rtx x, const_rtx ignored ATTRIBUTE_UNUSED,
4913 void *data)
4914 {
4915 unsigned int regno;
4916 unsigned int nr;
4917 regset regs = (regset) data;
4918
4919 /* note_stores does give us subregs of hard regs,
4920 subreg_regno_offset requires a hard reg. */
4921 while (GET_CODE (x) == SUBREG)
4922 {
4923 /* We ignore the subreg offset when calculating the regno,
4924 because we are using the entire underlying hard register
4925 below. */
4926 x = SUBREG_REG (x);
4927 }
4928
4929 if (!REG_P (x))
4930 return;
4931
4932 regno = REGNO (x);
4933
4934 if (regno >= FIRST_PSEUDO_REGISTER)
4935 nr = 1;
4936 else
4937 {
4938 unsigned int i;
4939
4940 nr = hard_regno_nregs[regno][GET_MODE (x)];
4941 /* Storing into a spilled-reg invalidates its contents.
4942 This can happen if a block-local pseudo is allocated to that reg
4943 and it wasn't spilled because this block's total need is 0.
4944 Then some insn might have an optional reload and use this reg. */
4945 if (!regs)
4946 for (i = 0; i < nr; i++)
4947 /* But don't do this if the reg actually serves as an output
4948 reload reg in the current instruction. */
4949 if (n_reloads == 0
4950 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4951 {
4952 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4953 spill_reg_store[regno + i] = 0;
4954 }
4955 }
4956
4957 if (regs)
4958 while (nr-- > 0)
4959 SET_REGNO_REG_SET (regs, regno + nr);
4960 else
4961 {
4962 /* Since value of X has changed,
4963 forget any value previously copied from it. */
4964
4965 while (nr-- > 0)
4966 /* But don't forget a copy if this is the output reload
4967 that establishes the copy's validity. */
4968 if (n_reloads == 0
4969 || !REGNO_REG_SET_P (&reg_has_output_reload, regno + nr))
4970 reg_last_reload_reg[regno + nr] = 0;
4971 }
4972 }
4973
4974 /* Forget the reloads marked in regset by previous function. */
4975 static void
4976 forget_marked_reloads (regset regs)
4977 {
4978 unsigned int reg;
4979 reg_set_iterator rsi;
4980 EXECUTE_IF_SET_IN_REG_SET (regs, 0, reg, rsi)
4981 {
4982 if (reg < FIRST_PSEUDO_REGISTER
4983 /* But don't do this if the reg actually serves as an output
4984 reload reg in the current instruction. */
4985 && (n_reloads == 0
4986 || ! TEST_HARD_REG_BIT (reg_is_output_reload, reg)))
4987 {
4988 CLEAR_HARD_REG_BIT (reg_reloaded_valid, reg);
4989 spill_reg_store[reg] = 0;
4990 }
4991 if (n_reloads == 0
4992 || !REGNO_REG_SET_P (&reg_has_output_reload, reg))
4993 reg_last_reload_reg[reg] = 0;
4994 }
4995 }
4996 \f
4997 /* The following HARD_REG_SETs indicate when each hard register is
4998 used for a reload of various parts of the current insn. */
4999
5000 /* If reg is unavailable for all reloads. */
5001 static HARD_REG_SET reload_reg_unavailable;
5002 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
5003 static HARD_REG_SET reload_reg_used;
5004 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
5005 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
5006 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
5007 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
5008 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
5009 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
5010 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
5011 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
5012 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
5013 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
5014 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
5015 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
5016 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
5017 static HARD_REG_SET reload_reg_used_in_op_addr;
5018 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
5019 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
5020 /* If reg is in use for a RELOAD_FOR_INSN reload. */
5021 static HARD_REG_SET reload_reg_used_in_insn;
5022 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
5023 static HARD_REG_SET reload_reg_used_in_other_addr;
5024
5025 /* If reg is in use as a reload reg for any sort of reload. */
5026 static HARD_REG_SET reload_reg_used_at_all;
5027
5028 /* If reg is use as an inherited reload. We just mark the first register
5029 in the group. */
5030 static HARD_REG_SET reload_reg_used_for_inherit;
5031
5032 /* Records which hard regs are used in any way, either as explicit use or
5033 by being allocated to a pseudo during any point of the current insn. */
5034 static HARD_REG_SET reg_used_in_insn;
5035
5036 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
5037 TYPE. MODE is used to indicate how many consecutive regs are
5038 actually used. */
5039
5040 static void
5041 mark_reload_reg_in_use (unsigned int regno, int opnum, enum reload_type type,
5042 enum machine_mode mode)
5043 {
5044 switch (type)
5045 {
5046 case RELOAD_OTHER:
5047 add_to_hard_reg_set (&reload_reg_used, mode, regno);
5048 break;
5049
5050 case RELOAD_FOR_INPUT_ADDRESS:
5051 add_to_hard_reg_set (&reload_reg_used_in_input_addr[opnum], mode, regno);
5052 break;
5053
5054 case RELOAD_FOR_INPADDR_ADDRESS:
5055 add_to_hard_reg_set (&reload_reg_used_in_inpaddr_addr[opnum], mode, regno);
5056 break;
5057
5058 case RELOAD_FOR_OUTPUT_ADDRESS:
5059 add_to_hard_reg_set (&reload_reg_used_in_output_addr[opnum], mode, regno);
5060 break;
5061
5062 case RELOAD_FOR_OUTADDR_ADDRESS:
5063 add_to_hard_reg_set (&reload_reg_used_in_outaddr_addr[opnum], mode, regno);
5064 break;
5065
5066 case RELOAD_FOR_OPERAND_ADDRESS:
5067 add_to_hard_reg_set (&reload_reg_used_in_op_addr, mode, regno);
5068 break;
5069
5070 case RELOAD_FOR_OPADDR_ADDR:
5071 add_to_hard_reg_set (&reload_reg_used_in_op_addr_reload, mode, regno);
5072 break;
5073
5074 case RELOAD_FOR_OTHER_ADDRESS:
5075 add_to_hard_reg_set (&reload_reg_used_in_other_addr, mode, regno);
5076 break;
5077
5078 case RELOAD_FOR_INPUT:
5079 add_to_hard_reg_set (&reload_reg_used_in_input[opnum], mode, regno);
5080 break;
5081
5082 case RELOAD_FOR_OUTPUT:
5083 add_to_hard_reg_set (&reload_reg_used_in_output[opnum], mode, regno);
5084 break;
5085
5086 case RELOAD_FOR_INSN:
5087 add_to_hard_reg_set (&reload_reg_used_in_insn, mode, regno);
5088 break;
5089 }
5090
5091 add_to_hard_reg_set (&reload_reg_used_at_all, mode, regno);
5092 }
5093
5094 /* Similarly, but show REGNO is no longer in use for a reload. */
5095
5096 static void
5097 clear_reload_reg_in_use (unsigned int regno, int opnum,
5098 enum reload_type type, enum machine_mode mode)
5099 {
5100 unsigned int nregs = hard_regno_nregs[regno][mode];
5101 unsigned int start_regno, end_regno, r;
5102 int i;
5103 /* A complication is that for some reload types, inheritance might
5104 allow multiple reloads of the same types to share a reload register.
5105 We set check_opnum if we have to check only reloads with the same
5106 operand number, and check_any if we have to check all reloads. */
5107 int check_opnum = 0;
5108 int check_any = 0;
5109 HARD_REG_SET *used_in_set;
5110
5111 switch (type)
5112 {
5113 case RELOAD_OTHER:
5114 used_in_set = &reload_reg_used;
5115 break;
5116
5117 case RELOAD_FOR_INPUT_ADDRESS:
5118 used_in_set = &reload_reg_used_in_input_addr[opnum];
5119 break;
5120
5121 case RELOAD_FOR_INPADDR_ADDRESS:
5122 check_opnum = 1;
5123 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
5124 break;
5125
5126 case RELOAD_FOR_OUTPUT_ADDRESS:
5127 used_in_set = &reload_reg_used_in_output_addr[opnum];
5128 break;
5129
5130 case RELOAD_FOR_OUTADDR_ADDRESS:
5131 check_opnum = 1;
5132 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
5133 break;
5134
5135 case RELOAD_FOR_OPERAND_ADDRESS:
5136 used_in_set = &reload_reg_used_in_op_addr;
5137 break;
5138
5139 case RELOAD_FOR_OPADDR_ADDR:
5140 check_any = 1;
5141 used_in_set = &reload_reg_used_in_op_addr_reload;
5142 break;
5143
5144 case RELOAD_FOR_OTHER_ADDRESS:
5145 used_in_set = &reload_reg_used_in_other_addr;
5146 check_any = 1;
5147 break;
5148
5149 case RELOAD_FOR_INPUT:
5150 used_in_set = &reload_reg_used_in_input[opnum];
5151 break;
5152
5153 case RELOAD_FOR_OUTPUT:
5154 used_in_set = &reload_reg_used_in_output[opnum];
5155 break;
5156
5157 case RELOAD_FOR_INSN:
5158 used_in_set = &reload_reg_used_in_insn;
5159 break;
5160 default:
5161 gcc_unreachable ();
5162 }
5163 /* We resolve conflicts with remaining reloads of the same type by
5164 excluding the intervals of reload registers by them from the
5165 interval of freed reload registers. Since we only keep track of
5166 one set of interval bounds, we might have to exclude somewhat
5167 more than what would be necessary if we used a HARD_REG_SET here.
5168 But this should only happen very infrequently, so there should
5169 be no reason to worry about it. */
5170
5171 start_regno = regno;
5172 end_regno = regno + nregs;
5173 if (check_opnum || check_any)
5174 {
5175 for (i = n_reloads - 1; i >= 0; i--)
5176 {
5177 if (rld[i].when_needed == type
5178 && (check_any || rld[i].opnum == opnum)
5179 && rld[i].reg_rtx)
5180 {
5181 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
5182 unsigned int conflict_end
5183 = end_hard_regno (rld[i].mode, conflict_start);
5184
5185 /* If there is an overlap with the first to-be-freed register,
5186 adjust the interval start. */
5187 if (conflict_start <= start_regno && conflict_end > start_regno)
5188 start_regno = conflict_end;
5189 /* Otherwise, if there is a conflict with one of the other
5190 to-be-freed registers, adjust the interval end. */
5191 if (conflict_start > start_regno && conflict_start < end_regno)
5192 end_regno = conflict_start;
5193 }
5194 }
5195 }
5196
5197 for (r = start_regno; r < end_regno; r++)
5198 CLEAR_HARD_REG_BIT (*used_in_set, r);
5199 }
5200
5201 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
5202 specified by OPNUM and TYPE. */
5203
5204 static int
5205 reload_reg_free_p (unsigned int regno, int opnum, enum reload_type type)
5206 {
5207 int i;
5208
5209 /* In use for a RELOAD_OTHER means it's not available for anything. */
5210 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
5211 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
5212 return 0;
5213
5214 switch (type)
5215 {
5216 case RELOAD_OTHER:
5217 /* In use for anything means we can't use it for RELOAD_OTHER. */
5218 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
5219 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5220 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
5221 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
5222 return 0;
5223
5224 for (i = 0; i < reload_n_operands; i++)
5225 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5226 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5227 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5228 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5229 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
5230 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5231 return 0;
5232
5233 return 1;
5234
5235 case RELOAD_FOR_INPUT:
5236 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5237 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
5238 return 0;
5239
5240 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
5241 return 0;
5242
5243 /* If it is used for some other input, can't use it. */
5244 for (i = 0; i < reload_n_operands; i++)
5245 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5246 return 0;
5247
5248 /* If it is used in a later operand's address, can't use it. */
5249 for (i = opnum + 1; i < reload_n_operands; i++)
5250 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5251 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
5252 return 0;
5253
5254 return 1;
5255
5256 case RELOAD_FOR_INPUT_ADDRESS:
5257 /* Can't use a register if it is used for an input address for this
5258 operand or used as an input in an earlier one. */
5259 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
5260 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
5261 return 0;
5262
5263 for (i = 0; i < opnum; i++)
5264 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5265 return 0;
5266
5267 return 1;
5268
5269 case RELOAD_FOR_INPADDR_ADDRESS:
5270 /* Can't use a register if it is used for an input address
5271 for this operand or used as an input in an earlier
5272 one. */
5273 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
5274 return 0;
5275
5276 for (i = 0; i < opnum; i++)
5277 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5278 return 0;
5279
5280 return 1;
5281
5282 case RELOAD_FOR_OUTPUT_ADDRESS:
5283 /* Can't use a register if it is used for an output address for this
5284 operand or used as an output in this or a later operand. Note
5285 that multiple output operands are emitted in reverse order, so
5286 the conflicting ones are those with lower indices. */
5287 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
5288 return 0;
5289
5290 for (i = 0; i <= opnum; i++)
5291 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5292 return 0;
5293
5294 return 1;
5295
5296 case RELOAD_FOR_OUTADDR_ADDRESS:
5297 /* Can't use a register if it is used for an output address
5298 for this operand or used as an output in this or a
5299 later operand. Note that multiple output operands are
5300 emitted in reverse order, so the conflicting ones are
5301 those with lower indices. */
5302 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
5303 return 0;
5304
5305 for (i = 0; i <= opnum; i++)
5306 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5307 return 0;
5308
5309 return 1;
5310
5311 case RELOAD_FOR_OPERAND_ADDRESS:
5312 for (i = 0; i < reload_n_operands; i++)
5313 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5314 return 0;
5315
5316 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5317 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
5318
5319 case RELOAD_FOR_OPADDR_ADDR:
5320 for (i = 0; i < reload_n_operands; i++)
5321 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5322 return 0;
5323
5324 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
5325
5326 case RELOAD_FOR_OUTPUT:
5327 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
5328 outputs, or an operand address for this or an earlier output.
5329 Note that multiple output operands are emitted in reverse order,
5330 so the conflicting ones are those with higher indices. */
5331 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
5332 return 0;
5333
5334 for (i = 0; i < reload_n_operands; i++)
5335 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5336 return 0;
5337
5338 for (i = opnum; i < reload_n_operands; i++)
5339 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5340 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5341 return 0;
5342
5343 return 1;
5344
5345 case RELOAD_FOR_INSN:
5346 for (i = 0; i < reload_n_operands; i++)
5347 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
5348 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5349 return 0;
5350
5351 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5352 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
5353
5354 case RELOAD_FOR_OTHER_ADDRESS:
5355 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
5356
5357 default:
5358 gcc_unreachable ();
5359 }
5360 }
5361
5362 /* Return 1 if the value in reload reg REGNO, as used by the reload with
5363 the number RELOADNUM, is still available in REGNO at the end of the insn.
5364
5365 We can assume that the reload reg was already tested for availability
5366 at the time it is needed, and we should not check this again,
5367 in case the reg has already been marked in use. */
5368
5369 static int
5370 reload_reg_reaches_end_p (unsigned int regno, int reloadnum)
5371 {
5372 int opnum = rld[reloadnum].opnum;
5373 enum reload_type type = rld[reloadnum].when_needed;
5374 int i;
5375
5376 /* See if there is a reload with the same type for this operand, using
5377 the same register. This case is not handled by the code below. */
5378 for (i = reloadnum + 1; i < n_reloads; i++)
5379 {
5380 rtx reg;
5381 int nregs;
5382
5383 if (rld[i].opnum != opnum || rld[i].when_needed != type)
5384 continue;
5385 reg = rld[i].reg_rtx;
5386 if (reg == NULL_RTX)
5387 continue;
5388 nregs = hard_regno_nregs[REGNO (reg)][GET_MODE (reg)];
5389 if (regno >= REGNO (reg) && regno < REGNO (reg) + nregs)
5390 return 0;
5391 }
5392
5393 switch (type)
5394 {
5395 case RELOAD_OTHER:
5396 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
5397 its value must reach the end. */
5398 return 1;
5399
5400 /* If this use is for part of the insn,
5401 its value reaches if no subsequent part uses the same register.
5402 Just like the above function, don't try to do this with lots
5403 of fallthroughs. */
5404
5405 case RELOAD_FOR_OTHER_ADDRESS:
5406 /* Here we check for everything else, since these don't conflict
5407 with anything else and everything comes later. */
5408
5409 for (i = 0; i < reload_n_operands; i++)
5410 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5411 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5412 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
5413 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5414 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5415 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5416 return 0;
5417
5418 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5419 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
5420 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5421 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
5422
5423 case RELOAD_FOR_INPUT_ADDRESS:
5424 case RELOAD_FOR_INPADDR_ADDRESS:
5425 /* Similar, except that we check only for this and subsequent inputs
5426 and the address of only subsequent inputs and we do not need
5427 to check for RELOAD_OTHER objects since they are known not to
5428 conflict. */
5429
5430 for (i = opnum; i < reload_n_operands; i++)
5431 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5432 return 0;
5433
5434 /* Reload register of reload with type RELOAD_FOR_INPADDR_ADDRESS
5435 could be killed if the register is also used by reload with type
5436 RELOAD_FOR_INPUT_ADDRESS, so check it. */
5437 if (type == RELOAD_FOR_INPADDR_ADDRESS
5438 && TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno))
5439 return 0;
5440
5441 for (i = opnum + 1; i < reload_n_operands; i++)
5442 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5443 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
5444 return 0;
5445
5446 for (i = 0; i < reload_n_operands; i++)
5447 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5448 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5449 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5450 return 0;
5451
5452 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
5453 return 0;
5454
5455 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5456 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5457 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
5458
5459 case RELOAD_FOR_INPUT:
5460 /* Similar to input address, except we start at the next operand for
5461 both input and input address and we do not check for
5462 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
5463 would conflict. */
5464
5465 for (i = opnum + 1; i < reload_n_operands; i++)
5466 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5467 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5468 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5469 return 0;
5470
5471 /* ... fall through ... */
5472
5473 case RELOAD_FOR_OPERAND_ADDRESS:
5474 /* Check outputs and their addresses. */
5475
5476 for (i = 0; i < reload_n_operands; i++)
5477 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5478 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5479 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5480 return 0;
5481
5482 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
5483
5484 case RELOAD_FOR_OPADDR_ADDR:
5485 for (i = 0; i < reload_n_operands; i++)
5486 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5487 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5488 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5489 return 0;
5490
5491 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5492 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5493 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
5494
5495 case RELOAD_FOR_INSN:
5496 /* These conflict with other outputs with RELOAD_OTHER. So
5497 we need only check for output addresses. */
5498
5499 opnum = reload_n_operands;
5500
5501 /* ... fall through ... */
5502
5503 case RELOAD_FOR_OUTPUT:
5504 case RELOAD_FOR_OUTPUT_ADDRESS:
5505 case RELOAD_FOR_OUTADDR_ADDRESS:
5506 /* We already know these can't conflict with a later output. So the
5507 only thing to check are later output addresses.
5508 Note that multiple output operands are emitted in reverse order,
5509 so the conflicting ones are those with lower indices. */
5510 for (i = 0; i < opnum; i++)
5511 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5512 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5513 return 0;
5514
5515 /* Reload register of reload with type RELOAD_FOR_OUTADDR_ADDRESS
5516 could be killed if the register is also used by reload with type
5517 RELOAD_FOR_OUTPUT_ADDRESS, so check it. */
5518 if (type == RELOAD_FOR_OUTADDR_ADDRESS
5519 && TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
5520 return 0;
5521
5522 return 1;
5523
5524 default:
5525 gcc_unreachable ();
5526 }
5527 }
5528
5529 /* Like reload_reg_reaches_end_p, but check that the condition holds for
5530 every register in REG. */
5531
5532 static bool
5533 reload_reg_rtx_reaches_end_p (rtx reg, int reloadnum)
5534 {
5535 unsigned int i;
5536
5537 for (i = REGNO (reg); i < END_REGNO (reg); i++)
5538 if (!reload_reg_reaches_end_p (i, reloadnum))
5539 return false;
5540 return true;
5541 }
5542 \f
5543
5544 /* Returns whether R1 and R2 are uniquely chained: the value of one
5545 is used by the other, and that value is not used by any other
5546 reload for this insn. This is used to partially undo the decision
5547 made in find_reloads when in the case of multiple
5548 RELOAD_FOR_OPERAND_ADDRESS reloads it converts all
5549 RELOAD_FOR_OPADDR_ADDR reloads into RELOAD_FOR_OPERAND_ADDRESS
5550 reloads. This code tries to avoid the conflict created by that
5551 change. It might be cleaner to explicitly keep track of which
5552 RELOAD_FOR_OPADDR_ADDR reload is associated with which
5553 RELOAD_FOR_OPERAND_ADDRESS reload, rather than to try to detect
5554 this after the fact. */
5555 static bool
5556 reloads_unique_chain_p (int r1, int r2)
5557 {
5558 int i;
5559
5560 /* We only check input reloads. */
5561 if (! rld[r1].in || ! rld[r2].in)
5562 return false;
5563
5564 /* Avoid anything with output reloads. */
5565 if (rld[r1].out || rld[r2].out)
5566 return false;
5567
5568 /* "chained" means one reload is a component of the other reload,
5569 not the same as the other reload. */
5570 if (rld[r1].opnum != rld[r2].opnum
5571 || rtx_equal_p (rld[r1].in, rld[r2].in)
5572 || rld[r1].optional || rld[r2].optional
5573 || ! (reg_mentioned_p (rld[r1].in, rld[r2].in)
5574 || reg_mentioned_p (rld[r2].in, rld[r1].in)))
5575 return false;
5576
5577 for (i = 0; i < n_reloads; i ++)
5578 /* Look for input reloads that aren't our two */
5579 if (i != r1 && i != r2 && rld[i].in)
5580 {
5581 /* If our reload is mentioned at all, it isn't a simple chain. */
5582 if (reg_mentioned_p (rld[r1].in, rld[i].in))
5583 return false;
5584 }
5585 return true;
5586 }
5587
5588 /* The recursive function change all occurrences of WHAT in *WHERE
5589 to REPL. */
5590 static void
5591 substitute (rtx *where, const_rtx what, rtx repl)
5592 {
5593 const char *fmt;
5594 int i;
5595 enum rtx_code code;
5596
5597 if (*where == 0)
5598 return;
5599
5600 if (*where == what || rtx_equal_p (*where, what))
5601 {
5602 /* Record the location of the changed rtx. */
5603 VEC_safe_push (rtx_p, heap, substitute_stack, where);
5604 *where = repl;
5605 return;
5606 }
5607
5608 code = GET_CODE (*where);
5609 fmt = GET_RTX_FORMAT (code);
5610 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5611 {
5612 if (fmt[i] == 'E')
5613 {
5614 int j;
5615
5616 for (j = XVECLEN (*where, i) - 1; j >= 0; j--)
5617 substitute (&XVECEXP (*where, i, j), what, repl);
5618 }
5619 else if (fmt[i] == 'e')
5620 substitute (&XEXP (*where, i), what, repl);
5621 }
5622 }
5623
5624 /* The function returns TRUE if chain of reload R1 and R2 (in any
5625 order) can be evaluated without usage of intermediate register for
5626 the reload containing another reload. It is important to see
5627 gen_reload to understand what the function is trying to do. As an
5628 example, let us have reload chain
5629
5630 r2: const
5631 r1: <something> + const
5632
5633 and reload R2 got reload reg HR. The function returns true if
5634 there is a correct insn HR = HR + <something>. Otherwise,
5635 gen_reload will use intermediate register (and this is the reload
5636 reg for R1) to reload <something>.
5637
5638 We need this function to find a conflict for chain reloads. In our
5639 example, if HR = HR + <something> is incorrect insn, then we cannot
5640 use HR as a reload register for R2. If we do use it then we get a
5641 wrong code:
5642
5643 HR = const
5644 HR = <something>
5645 HR = HR + HR
5646
5647 */
5648 static bool
5649 gen_reload_chain_without_interm_reg_p (int r1, int r2)
5650 {
5651 /* Assume other cases in gen_reload are not possible for
5652 chain reloads or do need an intermediate hard registers. */
5653 bool result = true;
5654 int regno, n, code;
5655 rtx out, in, insn;
5656 rtx last = get_last_insn ();
5657
5658 /* Make r2 a component of r1. */
5659 if (reg_mentioned_p (rld[r1].in, rld[r2].in))
5660 {
5661 n = r1;
5662 r1 = r2;
5663 r2 = n;
5664 }
5665 gcc_assert (reg_mentioned_p (rld[r2].in, rld[r1].in));
5666 regno = rld[r1].regno >= 0 ? rld[r1].regno : rld[r2].regno;
5667 gcc_assert (regno >= 0);
5668 out = gen_rtx_REG (rld[r1].mode, regno);
5669 in = rld[r1].in;
5670 substitute (&in, rld[r2].in, gen_rtx_REG (rld[r2].mode, regno));
5671
5672 /* If IN is a paradoxical SUBREG, remove it and try to put the
5673 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
5674 strip_paradoxical_subreg (&in, &out);
5675
5676 if (GET_CODE (in) == PLUS
5677 && (REG_P (XEXP (in, 0))
5678 || GET_CODE (XEXP (in, 0)) == SUBREG
5679 || MEM_P (XEXP (in, 0)))
5680 && (REG_P (XEXP (in, 1))
5681 || GET_CODE (XEXP (in, 1)) == SUBREG
5682 || CONSTANT_P (XEXP (in, 1))
5683 || MEM_P (XEXP (in, 1))))
5684 {
5685 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
5686 code = recog_memoized (insn);
5687 result = false;
5688
5689 if (code >= 0)
5690 {
5691 extract_insn (insn);
5692 /* We want constrain operands to treat this insn strictly in
5693 its validity determination, i.e., the way it would after
5694 reload has completed. */
5695 result = constrain_operands (1);
5696 }
5697
5698 delete_insns_since (last);
5699 }
5700
5701 /* Restore the original value at each changed address within R1. */
5702 while (!VEC_empty (rtx_p, substitute_stack))
5703 {
5704 rtx *where = VEC_pop (rtx_p, substitute_stack);
5705 *where = rld[r2].in;
5706 }
5707
5708 return result;
5709 }
5710
5711 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
5712 Return 0 otherwise.
5713
5714 This function uses the same algorithm as reload_reg_free_p above. */
5715
5716 static int
5717 reloads_conflict (int r1, int r2)
5718 {
5719 enum reload_type r1_type = rld[r1].when_needed;
5720 enum reload_type r2_type = rld[r2].when_needed;
5721 int r1_opnum = rld[r1].opnum;
5722 int r2_opnum = rld[r2].opnum;
5723
5724 /* RELOAD_OTHER conflicts with everything. */
5725 if (r2_type == RELOAD_OTHER)
5726 return 1;
5727
5728 /* Otherwise, check conflicts differently for each type. */
5729
5730 switch (r1_type)
5731 {
5732 case RELOAD_FOR_INPUT:
5733 return (r2_type == RELOAD_FOR_INSN
5734 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
5735 || r2_type == RELOAD_FOR_OPADDR_ADDR
5736 || r2_type == RELOAD_FOR_INPUT
5737 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
5738 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
5739 && r2_opnum > r1_opnum));
5740
5741 case RELOAD_FOR_INPUT_ADDRESS:
5742 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
5743 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5744
5745 case RELOAD_FOR_INPADDR_ADDRESS:
5746 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
5747 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5748
5749 case RELOAD_FOR_OUTPUT_ADDRESS:
5750 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
5751 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
5752
5753 case RELOAD_FOR_OUTADDR_ADDRESS:
5754 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
5755 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
5756
5757 case RELOAD_FOR_OPERAND_ADDRESS:
5758 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
5759 || (r2_type == RELOAD_FOR_OPERAND_ADDRESS
5760 && (!reloads_unique_chain_p (r1, r2)
5761 || !gen_reload_chain_without_interm_reg_p (r1, r2))));
5762
5763 case RELOAD_FOR_OPADDR_ADDR:
5764 return (r2_type == RELOAD_FOR_INPUT
5765 || r2_type == RELOAD_FOR_OPADDR_ADDR);
5766
5767 case RELOAD_FOR_OUTPUT:
5768 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
5769 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
5770 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
5771 && r2_opnum >= r1_opnum));
5772
5773 case RELOAD_FOR_INSN:
5774 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
5775 || r2_type == RELOAD_FOR_INSN
5776 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5777
5778 case RELOAD_FOR_OTHER_ADDRESS:
5779 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
5780
5781 case RELOAD_OTHER:
5782 return 1;
5783
5784 default:
5785 gcc_unreachable ();
5786 }
5787 }
5788 \f
5789 /* Indexed by reload number, 1 if incoming value
5790 inherited from previous insns. */
5791 static char reload_inherited[MAX_RELOADS];
5792
5793 /* For an inherited reload, this is the insn the reload was inherited from,
5794 if we know it. Otherwise, this is 0. */
5795 static rtx reload_inheritance_insn[MAX_RELOADS];
5796
5797 /* If nonzero, this is a place to get the value of the reload,
5798 rather than using reload_in. */
5799 static rtx reload_override_in[MAX_RELOADS];
5800
5801 /* For each reload, the hard register number of the register used,
5802 or -1 if we did not need a register for this reload. */
5803 static int reload_spill_index[MAX_RELOADS];
5804
5805 /* Index X is the value of rld[X].reg_rtx, adjusted for the input mode. */
5806 static rtx reload_reg_rtx_for_input[MAX_RELOADS];
5807
5808 /* Index X is the value of rld[X].reg_rtx, adjusted for the output mode. */
5809 static rtx reload_reg_rtx_for_output[MAX_RELOADS];
5810
5811 /* Subroutine of free_for_value_p, used to check a single register.
5812 START_REGNO is the starting regno of the full reload register
5813 (possibly comprising multiple hard registers) that we are considering. */
5814
5815 static int
5816 reload_reg_free_for_value_p (int start_regno, int regno, int opnum,
5817 enum reload_type type, rtx value, rtx out,
5818 int reloadnum, int ignore_address_reloads)
5819 {
5820 int time1;
5821 /* Set if we see an input reload that must not share its reload register
5822 with any new earlyclobber, but might otherwise share the reload
5823 register with an output or input-output reload. */
5824 int check_earlyclobber = 0;
5825 int i;
5826 int copy = 0;
5827
5828 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
5829 return 0;
5830
5831 if (out == const0_rtx)
5832 {
5833 copy = 1;
5834 out = NULL_RTX;
5835 }
5836
5837 /* We use some pseudo 'time' value to check if the lifetimes of the
5838 new register use would overlap with the one of a previous reload
5839 that is not read-only or uses a different value.
5840 The 'time' used doesn't have to be linear in any shape or form, just
5841 monotonic.
5842 Some reload types use different 'buckets' for each operand.
5843 So there are MAX_RECOG_OPERANDS different time values for each
5844 such reload type.
5845 We compute TIME1 as the time when the register for the prospective
5846 new reload ceases to be live, and TIME2 for each existing
5847 reload as the time when that the reload register of that reload
5848 becomes live.
5849 Where there is little to be gained by exact lifetime calculations,
5850 we just make conservative assumptions, i.e. a longer lifetime;
5851 this is done in the 'default:' cases. */
5852 switch (type)
5853 {
5854 case RELOAD_FOR_OTHER_ADDRESS:
5855 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
5856 time1 = copy ? 0 : 1;
5857 break;
5858 case RELOAD_OTHER:
5859 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
5860 break;
5861 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
5862 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
5863 respectively, to the time values for these, we get distinct time
5864 values. To get distinct time values for each operand, we have to
5865 multiply opnum by at least three. We round that up to four because
5866 multiply by four is often cheaper. */
5867 case RELOAD_FOR_INPADDR_ADDRESS:
5868 time1 = opnum * 4 + 2;
5869 break;
5870 case RELOAD_FOR_INPUT_ADDRESS:
5871 time1 = opnum * 4 + 3;
5872 break;
5873 case RELOAD_FOR_INPUT:
5874 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
5875 executes (inclusive). */
5876 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
5877 break;
5878 case RELOAD_FOR_OPADDR_ADDR:
5879 /* opnum * 4 + 4
5880 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
5881 time1 = MAX_RECOG_OPERANDS * 4 + 1;
5882 break;
5883 case RELOAD_FOR_OPERAND_ADDRESS:
5884 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
5885 is executed. */
5886 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
5887 break;
5888 case RELOAD_FOR_OUTADDR_ADDRESS:
5889 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
5890 break;
5891 case RELOAD_FOR_OUTPUT_ADDRESS:
5892 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
5893 break;
5894 default:
5895 time1 = MAX_RECOG_OPERANDS * 5 + 5;
5896 }
5897
5898 for (i = 0; i < n_reloads; i++)
5899 {
5900 rtx reg = rld[i].reg_rtx;
5901 if (reg && REG_P (reg)
5902 && ((unsigned) regno - true_regnum (reg)
5903 <= hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] - (unsigned) 1)
5904 && i != reloadnum)
5905 {
5906 rtx other_input = rld[i].in;
5907
5908 /* If the other reload loads the same input value, that
5909 will not cause a conflict only if it's loading it into
5910 the same register. */
5911 if (true_regnum (reg) != start_regno)
5912 other_input = NULL_RTX;
5913 if (! other_input || ! rtx_equal_p (other_input, value)
5914 || rld[i].out || out)
5915 {
5916 int time2;
5917 switch (rld[i].when_needed)
5918 {
5919 case RELOAD_FOR_OTHER_ADDRESS:
5920 time2 = 0;
5921 break;
5922 case RELOAD_FOR_INPADDR_ADDRESS:
5923 /* find_reloads makes sure that a
5924 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
5925 by at most one - the first -
5926 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
5927 address reload is inherited, the address address reload
5928 goes away, so we can ignore this conflict. */
5929 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
5930 && ignore_address_reloads
5931 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
5932 Then the address address is still needed to store
5933 back the new address. */
5934 && ! rld[reloadnum].out)
5935 continue;
5936 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
5937 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
5938 reloads go away. */
5939 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5940 && ignore_address_reloads
5941 /* Unless we are reloading an auto_inc expression. */
5942 && ! rld[reloadnum].out)
5943 continue;
5944 time2 = rld[i].opnum * 4 + 2;
5945 break;
5946 case RELOAD_FOR_INPUT_ADDRESS:
5947 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5948 && ignore_address_reloads
5949 && ! rld[reloadnum].out)
5950 continue;
5951 time2 = rld[i].opnum * 4 + 3;
5952 break;
5953 case RELOAD_FOR_INPUT:
5954 time2 = rld[i].opnum * 4 + 4;
5955 check_earlyclobber = 1;
5956 break;
5957 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
5958 == MAX_RECOG_OPERAND * 4 */
5959 case RELOAD_FOR_OPADDR_ADDR:
5960 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
5961 && ignore_address_reloads
5962 && ! rld[reloadnum].out)
5963 continue;
5964 time2 = MAX_RECOG_OPERANDS * 4 + 1;
5965 break;
5966 case RELOAD_FOR_OPERAND_ADDRESS:
5967 time2 = MAX_RECOG_OPERANDS * 4 + 2;
5968 check_earlyclobber = 1;
5969 break;
5970 case RELOAD_FOR_INSN:
5971 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5972 break;
5973 case RELOAD_FOR_OUTPUT:
5974 /* All RELOAD_FOR_OUTPUT reloads become live just after the
5975 instruction is executed. */
5976 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5977 break;
5978 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
5979 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
5980 value. */
5981 case RELOAD_FOR_OUTADDR_ADDRESS:
5982 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
5983 && ignore_address_reloads
5984 && ! rld[reloadnum].out)
5985 continue;
5986 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
5987 break;
5988 case RELOAD_FOR_OUTPUT_ADDRESS:
5989 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
5990 break;
5991 case RELOAD_OTHER:
5992 /* If there is no conflict in the input part, handle this
5993 like an output reload. */
5994 if (! rld[i].in || rtx_equal_p (other_input, value))
5995 {
5996 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5997 /* Earlyclobbered outputs must conflict with inputs. */
5998 if (earlyclobber_operand_p (rld[i].out))
5999 time2 = MAX_RECOG_OPERANDS * 4 + 3;
6000
6001 break;
6002 }
6003 time2 = 1;
6004 /* RELOAD_OTHER might be live beyond instruction execution,
6005 but this is not obvious when we set time2 = 1. So check
6006 here if there might be a problem with the new reload
6007 clobbering the register used by the RELOAD_OTHER. */
6008 if (out)
6009 return 0;
6010 break;
6011 default:
6012 return 0;
6013 }
6014 if ((time1 >= time2
6015 && (! rld[i].in || rld[i].out
6016 || ! rtx_equal_p (other_input, value)))
6017 || (out && rld[reloadnum].out_reg
6018 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
6019 return 0;
6020 }
6021 }
6022 }
6023
6024 /* Earlyclobbered outputs must conflict with inputs. */
6025 if (check_earlyclobber && out && earlyclobber_operand_p (out))
6026 return 0;
6027
6028 return 1;
6029 }
6030
6031 /* Return 1 if the value in reload reg REGNO, as used by a reload
6032 needed for the part of the insn specified by OPNUM and TYPE,
6033 may be used to load VALUE into it.
6034
6035 MODE is the mode in which the register is used, this is needed to
6036 determine how many hard regs to test.
6037
6038 Other read-only reloads with the same value do not conflict
6039 unless OUT is nonzero and these other reloads have to live while
6040 output reloads live.
6041 If OUT is CONST0_RTX, this is a special case: it means that the
6042 test should not be for using register REGNO as reload register, but
6043 for copying from register REGNO into the reload register.
6044
6045 RELOADNUM is the number of the reload we want to load this value for;
6046 a reload does not conflict with itself.
6047
6048 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
6049 reloads that load an address for the very reload we are considering.
6050
6051 The caller has to make sure that there is no conflict with the return
6052 register. */
6053
6054 static int
6055 free_for_value_p (int regno, enum machine_mode mode, int opnum,
6056 enum reload_type type, rtx value, rtx out, int reloadnum,
6057 int ignore_address_reloads)
6058 {
6059 int nregs = hard_regno_nregs[regno][mode];
6060 while (nregs-- > 0)
6061 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
6062 value, out, reloadnum,
6063 ignore_address_reloads))
6064 return 0;
6065 return 1;
6066 }
6067
6068 /* Return nonzero if the rtx X is invariant over the current function. */
6069 /* ??? Actually, the places where we use this expect exactly what is
6070 tested here, and not everything that is function invariant. In
6071 particular, the frame pointer and arg pointer are special cased;
6072 pic_offset_table_rtx is not, and we must not spill these things to
6073 memory. */
6074
6075 int
6076 function_invariant_p (const_rtx x)
6077 {
6078 if (CONSTANT_P (x))
6079 return 1;
6080 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
6081 return 1;
6082 if (GET_CODE (x) == PLUS
6083 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
6084 && GET_CODE (XEXP (x, 1)) == CONST_INT)
6085 return 1;
6086 return 0;
6087 }
6088
6089 /* Determine whether the reload reg X overlaps any rtx'es used for
6090 overriding inheritance. Return nonzero if so. */
6091
6092 static int
6093 conflicts_with_override (rtx x)
6094 {
6095 int i;
6096 for (i = 0; i < n_reloads; i++)
6097 if (reload_override_in[i]
6098 && reg_overlap_mentioned_p (x, reload_override_in[i]))
6099 return 1;
6100 return 0;
6101 }
6102 \f
6103 /* Give an error message saying we failed to find a reload for INSN,
6104 and clear out reload R. */
6105 static void
6106 failed_reload (rtx insn, int r)
6107 {
6108 if (asm_noperands (PATTERN (insn)) < 0)
6109 /* It's the compiler's fault. */
6110 fatal_insn ("could not find a spill register", insn);
6111
6112 /* It's the user's fault; the operand's mode and constraint
6113 don't match. Disable this reload so we don't crash in final. */
6114 error_for_asm (insn,
6115 "%<asm%> operand constraint incompatible with operand size");
6116 rld[r].in = 0;
6117 rld[r].out = 0;
6118 rld[r].reg_rtx = 0;
6119 rld[r].optional = 1;
6120 rld[r].secondary_p = 1;
6121 }
6122
6123 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
6124 for reload R. If it's valid, get an rtx for it. Return nonzero if
6125 successful. */
6126 static int
6127 set_reload_reg (int i, int r)
6128 {
6129 /* regno is 'set but not used' if HARD_REGNO_MODE_OK doesn't use its first
6130 parameter. */
6131 int regno ATTRIBUTE_UNUSED;
6132 rtx reg = spill_reg_rtx[i];
6133
6134 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
6135 spill_reg_rtx[i] = reg
6136 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
6137
6138 regno = true_regnum (reg);
6139
6140 /* Detect when the reload reg can't hold the reload mode.
6141 This used to be one `if', but Sequent compiler can't handle that. */
6142 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
6143 {
6144 enum machine_mode test_mode = VOIDmode;
6145 if (rld[r].in)
6146 test_mode = GET_MODE (rld[r].in);
6147 /* If rld[r].in has VOIDmode, it means we will load it
6148 in whatever mode the reload reg has: to wit, rld[r].mode.
6149 We have already tested that for validity. */
6150 /* Aside from that, we need to test that the expressions
6151 to reload from or into have modes which are valid for this
6152 reload register. Otherwise the reload insns would be invalid. */
6153 if (! (rld[r].in != 0 && test_mode != VOIDmode
6154 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
6155 if (! (rld[r].out != 0
6156 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
6157 {
6158 /* The reg is OK. */
6159 last_spill_reg = i;
6160
6161 /* Mark as in use for this insn the reload regs we use
6162 for this. */
6163 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
6164 rld[r].when_needed, rld[r].mode);
6165
6166 rld[r].reg_rtx = reg;
6167 reload_spill_index[r] = spill_regs[i];
6168 return 1;
6169 }
6170 }
6171 return 0;
6172 }
6173
6174 /* Find a spill register to use as a reload register for reload R.
6175 LAST_RELOAD is nonzero if this is the last reload for the insn being
6176 processed.
6177
6178 Set rld[R].reg_rtx to the register allocated.
6179
6180 We return 1 if successful, or 0 if we couldn't find a spill reg and
6181 we didn't change anything. */
6182
6183 static int
6184 allocate_reload_reg (struct insn_chain *chain ATTRIBUTE_UNUSED, int r,
6185 int last_reload)
6186 {
6187 int i, pass, count;
6188
6189 /* If we put this reload ahead, thinking it is a group,
6190 then insist on finding a group. Otherwise we can grab a
6191 reg that some other reload needs.
6192 (That can happen when we have a 68000 DATA_OR_FP_REG
6193 which is a group of data regs or one fp reg.)
6194 We need not be so restrictive if there are no more reloads
6195 for this insn.
6196
6197 ??? Really it would be nicer to have smarter handling
6198 for that kind of reg class, where a problem like this is normal.
6199 Perhaps those classes should be avoided for reloading
6200 by use of more alternatives. */
6201
6202 int force_group = rld[r].nregs > 1 && ! last_reload;
6203
6204 /* If we want a single register and haven't yet found one,
6205 take any reg in the right class and not in use.
6206 If we want a consecutive group, here is where we look for it.
6207
6208 We use three passes so we can first look for reload regs to
6209 reuse, which are already in use for other reloads in this insn,
6210 and only then use additional registers which are not "bad", then
6211 finally any register.
6212
6213 I think that maximizing reuse is needed to make sure we don't
6214 run out of reload regs. Suppose we have three reloads, and
6215 reloads A and B can share regs. These need two regs.
6216 Suppose A and B are given different regs.
6217 That leaves none for C. */
6218 for (pass = 0; pass < 3; pass++)
6219 {
6220 /* I is the index in spill_regs.
6221 We advance it round-robin between insns to use all spill regs
6222 equally, so that inherited reloads have a chance
6223 of leapfrogging each other. */
6224
6225 i = last_spill_reg;
6226
6227 for (count = 0; count < n_spills; count++)
6228 {
6229 int rclass = (int) rld[r].rclass;
6230 int regnum;
6231
6232 i++;
6233 if (i >= n_spills)
6234 i -= n_spills;
6235 regnum = spill_regs[i];
6236
6237 if ((reload_reg_free_p (regnum, rld[r].opnum,
6238 rld[r].when_needed)
6239 || (rld[r].in
6240 /* We check reload_reg_used to make sure we
6241 don't clobber the return register. */
6242 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
6243 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
6244 rld[r].when_needed, rld[r].in,
6245 rld[r].out, r, 1)))
6246 && TEST_HARD_REG_BIT (reg_class_contents[rclass], regnum)
6247 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
6248 /* Look first for regs to share, then for unshared. But
6249 don't share regs used for inherited reloads; they are
6250 the ones we want to preserve. */
6251 && (pass
6252 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
6253 regnum)
6254 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
6255 regnum))))
6256 {
6257 int nr = hard_regno_nregs[regnum][rld[r].mode];
6258
6259 /* During the second pass we want to avoid reload registers
6260 which are "bad" for this reload. */
6261 if (pass == 1
6262 && ira_bad_reload_regno (regnum, rld[r].in, rld[r].out))
6263 continue;
6264
6265 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
6266 (on 68000) got us two FP regs. If NR is 1,
6267 we would reject both of them. */
6268 if (force_group)
6269 nr = rld[r].nregs;
6270 /* If we need only one reg, we have already won. */
6271 if (nr == 1)
6272 {
6273 /* But reject a single reg if we demand a group. */
6274 if (force_group)
6275 continue;
6276 break;
6277 }
6278 /* Otherwise check that as many consecutive regs as we need
6279 are available here. */
6280 while (nr > 1)
6281 {
6282 int regno = regnum + nr - 1;
6283 if (!(TEST_HARD_REG_BIT (reg_class_contents[rclass], regno)
6284 && spill_reg_order[regno] >= 0
6285 && reload_reg_free_p (regno, rld[r].opnum,
6286 rld[r].when_needed)))
6287 break;
6288 nr--;
6289 }
6290 if (nr == 1)
6291 break;
6292 }
6293 }
6294
6295 /* If we found something on the current pass, omit later passes. */
6296 if (count < n_spills)
6297 break;
6298 }
6299
6300 /* We should have found a spill register by now. */
6301 if (count >= n_spills)
6302 return 0;
6303
6304 /* I is the index in SPILL_REG_RTX of the reload register we are to
6305 allocate. Get an rtx for it and find its register number. */
6306
6307 return set_reload_reg (i, r);
6308 }
6309 \f
6310 /* Initialize all the tables needed to allocate reload registers.
6311 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
6312 is the array we use to restore the reg_rtx field for every reload. */
6313
6314 static void
6315 choose_reload_regs_init (struct insn_chain *chain, rtx *save_reload_reg_rtx)
6316 {
6317 int i;
6318
6319 for (i = 0; i < n_reloads; i++)
6320 rld[i].reg_rtx = save_reload_reg_rtx[i];
6321
6322 memset (reload_inherited, 0, MAX_RELOADS);
6323 memset (reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
6324 memset (reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
6325
6326 CLEAR_HARD_REG_SET (reload_reg_used);
6327 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
6328 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
6329 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
6330 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
6331 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
6332
6333 CLEAR_HARD_REG_SET (reg_used_in_insn);
6334 {
6335 HARD_REG_SET tmp;
6336 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
6337 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
6338 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
6339 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
6340 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
6341 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
6342 }
6343
6344 for (i = 0; i < reload_n_operands; i++)
6345 {
6346 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
6347 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
6348 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
6349 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
6350 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
6351 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
6352 }
6353
6354 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
6355
6356 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
6357
6358 for (i = 0; i < n_reloads; i++)
6359 /* If we have already decided to use a certain register,
6360 don't use it in another way. */
6361 if (rld[i].reg_rtx)
6362 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
6363 rld[i].when_needed, rld[i].mode);
6364 }
6365
6366 /* Assign hard reg targets for the pseudo-registers we must reload
6367 into hard regs for this insn.
6368 Also output the instructions to copy them in and out of the hard regs.
6369
6370 For machines with register classes, we are responsible for
6371 finding a reload reg in the proper class. */
6372
6373 static void
6374 choose_reload_regs (struct insn_chain *chain)
6375 {
6376 rtx insn = chain->insn;
6377 int i, j;
6378 unsigned int max_group_size = 1;
6379 enum reg_class group_class = NO_REGS;
6380 int pass, win, inheritance;
6381
6382 rtx save_reload_reg_rtx[MAX_RELOADS];
6383
6384 /* In order to be certain of getting the registers we need,
6385 we must sort the reloads into order of increasing register class.
6386 Then our grabbing of reload registers will parallel the process
6387 that provided the reload registers.
6388
6389 Also note whether any of the reloads wants a consecutive group of regs.
6390 If so, record the maximum size of the group desired and what
6391 register class contains all the groups needed by this insn. */
6392
6393 for (j = 0; j < n_reloads; j++)
6394 {
6395 reload_order[j] = j;
6396 if (rld[j].reg_rtx != NULL_RTX)
6397 {
6398 gcc_assert (REG_P (rld[j].reg_rtx)
6399 && HARD_REGISTER_P (rld[j].reg_rtx));
6400 reload_spill_index[j] = REGNO (rld[j].reg_rtx);
6401 }
6402 else
6403 reload_spill_index[j] = -1;
6404
6405 if (rld[j].nregs > 1)
6406 {
6407 max_group_size = MAX (rld[j].nregs, max_group_size);
6408 group_class
6409 = reg_class_superunion[(int) rld[j].rclass][(int) group_class];
6410 }
6411
6412 save_reload_reg_rtx[j] = rld[j].reg_rtx;
6413 }
6414
6415 if (n_reloads > 1)
6416 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
6417
6418 /* If -O, try first with inheritance, then turning it off.
6419 If not -O, don't do inheritance.
6420 Using inheritance when not optimizing leads to paradoxes
6421 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
6422 because one side of the comparison might be inherited. */
6423 win = 0;
6424 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
6425 {
6426 choose_reload_regs_init (chain, save_reload_reg_rtx);
6427
6428 /* Process the reloads in order of preference just found.
6429 Beyond this point, subregs can be found in reload_reg_rtx.
6430
6431 This used to look for an existing reloaded home for all of the
6432 reloads, and only then perform any new reloads. But that could lose
6433 if the reloads were done out of reg-class order because a later
6434 reload with a looser constraint might have an old home in a register
6435 needed by an earlier reload with a tighter constraint.
6436
6437 To solve this, we make two passes over the reloads, in the order
6438 described above. In the first pass we try to inherit a reload
6439 from a previous insn. If there is a later reload that needs a
6440 class that is a proper subset of the class being processed, we must
6441 also allocate a spill register during the first pass.
6442
6443 Then make a second pass over the reloads to allocate any reloads
6444 that haven't been given registers yet. */
6445
6446 for (j = 0; j < n_reloads; j++)
6447 {
6448 int r = reload_order[j];
6449 rtx search_equiv = NULL_RTX;
6450
6451 /* Ignore reloads that got marked inoperative. */
6452 if (rld[r].out == 0 && rld[r].in == 0
6453 && ! rld[r].secondary_p)
6454 continue;
6455
6456 /* If find_reloads chose to use reload_in or reload_out as a reload
6457 register, we don't need to chose one. Otherwise, try even if it
6458 found one since we might save an insn if we find the value lying
6459 around.
6460 Try also when reload_in is a pseudo without a hard reg. */
6461 if (rld[r].in != 0 && rld[r].reg_rtx != 0
6462 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
6463 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
6464 && !MEM_P (rld[r].in)
6465 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
6466 continue;
6467
6468 #if 0 /* No longer needed for correct operation.
6469 It might give better code, or might not; worth an experiment? */
6470 /* If this is an optional reload, we can't inherit from earlier insns
6471 until we are sure that any non-optional reloads have been allocated.
6472 The following code takes advantage of the fact that optional reloads
6473 are at the end of reload_order. */
6474 if (rld[r].optional != 0)
6475 for (i = 0; i < j; i++)
6476 if ((rld[reload_order[i]].out != 0
6477 || rld[reload_order[i]].in != 0
6478 || rld[reload_order[i]].secondary_p)
6479 && ! rld[reload_order[i]].optional
6480 && rld[reload_order[i]].reg_rtx == 0)
6481 allocate_reload_reg (chain, reload_order[i], 0);
6482 #endif
6483
6484 /* First see if this pseudo is already available as reloaded
6485 for a previous insn. We cannot try to inherit for reloads
6486 that are smaller than the maximum number of registers needed
6487 for groups unless the register we would allocate cannot be used
6488 for the groups.
6489
6490 We could check here to see if this is a secondary reload for
6491 an object that is already in a register of the desired class.
6492 This would avoid the need for the secondary reload register.
6493 But this is complex because we can't easily determine what
6494 objects might want to be loaded via this reload. So let a
6495 register be allocated here. In `emit_reload_insns' we suppress
6496 one of the loads in the case described above. */
6497
6498 if (inheritance)
6499 {
6500 int byte = 0;
6501 int regno = -1;
6502 enum machine_mode mode = VOIDmode;
6503
6504 if (rld[r].in == 0)
6505 ;
6506 else if (REG_P (rld[r].in))
6507 {
6508 regno = REGNO (rld[r].in);
6509 mode = GET_MODE (rld[r].in);
6510 }
6511 else if (REG_P (rld[r].in_reg))
6512 {
6513 regno = REGNO (rld[r].in_reg);
6514 mode = GET_MODE (rld[r].in_reg);
6515 }
6516 else if (GET_CODE (rld[r].in_reg) == SUBREG
6517 && REG_P (SUBREG_REG (rld[r].in_reg)))
6518 {
6519 regno = REGNO (SUBREG_REG (rld[r].in_reg));
6520 if (regno < FIRST_PSEUDO_REGISTER)
6521 regno = subreg_regno (rld[r].in_reg);
6522 else
6523 byte = SUBREG_BYTE (rld[r].in_reg);
6524 mode = GET_MODE (rld[r].in_reg);
6525 }
6526 #ifdef AUTO_INC_DEC
6527 else if (GET_RTX_CLASS (GET_CODE (rld[r].in_reg)) == RTX_AUTOINC
6528 && REG_P (XEXP (rld[r].in_reg, 0)))
6529 {
6530 regno = REGNO (XEXP (rld[r].in_reg, 0));
6531 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
6532 rld[r].out = rld[r].in;
6533 }
6534 #endif
6535 #if 0
6536 /* This won't work, since REGNO can be a pseudo reg number.
6537 Also, it takes much more hair to keep track of all the things
6538 that can invalidate an inherited reload of part of a pseudoreg. */
6539 else if (GET_CODE (rld[r].in) == SUBREG
6540 && REG_P (SUBREG_REG (rld[r].in)))
6541 regno = subreg_regno (rld[r].in);
6542 #endif
6543
6544 if (regno >= 0
6545 && reg_last_reload_reg[regno] != 0
6546 && (GET_MODE_SIZE (GET_MODE (reg_last_reload_reg[regno]))
6547 >= GET_MODE_SIZE (mode) + byte)
6548 #ifdef CANNOT_CHANGE_MODE_CLASS
6549 /* Verify that the register it's in can be used in
6550 mode MODE. */
6551 && !REG_CANNOT_CHANGE_MODE_P (REGNO (reg_last_reload_reg[regno]),
6552 GET_MODE (reg_last_reload_reg[regno]),
6553 mode)
6554 #endif
6555 )
6556 {
6557 enum reg_class rclass = rld[r].rclass, last_class;
6558 rtx last_reg = reg_last_reload_reg[regno];
6559
6560 i = REGNO (last_reg);
6561 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
6562 last_class = REGNO_REG_CLASS (i);
6563
6564 if (reg_reloaded_contents[i] == regno
6565 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
6566 && HARD_REGNO_MODE_OK (i, rld[r].mode)
6567 && (TEST_HARD_REG_BIT (reg_class_contents[(int) rclass], i)
6568 /* Even if we can't use this register as a reload
6569 register, we might use it for reload_override_in,
6570 if copying it to the desired class is cheap
6571 enough. */
6572 || ((register_move_cost (mode, last_class, rclass)
6573 < memory_move_cost (mode, rclass, true))
6574 && (secondary_reload_class (1, rclass, mode,
6575 last_reg)
6576 == NO_REGS)
6577 #ifdef SECONDARY_MEMORY_NEEDED
6578 && ! SECONDARY_MEMORY_NEEDED (last_class, rclass,
6579 mode)
6580 #endif
6581 ))
6582
6583 && (rld[r].nregs == max_group_size
6584 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
6585 i))
6586 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
6587 rld[r].when_needed, rld[r].in,
6588 const0_rtx, r, 1))
6589 {
6590 /* If a group is needed, verify that all the subsequent
6591 registers still have their values intact. */
6592 int nr = hard_regno_nregs[i][rld[r].mode];
6593 int k;
6594
6595 for (k = 1; k < nr; k++)
6596 if (reg_reloaded_contents[i + k] != regno
6597 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
6598 break;
6599
6600 if (k == nr)
6601 {
6602 int i1;
6603 int bad_for_class;
6604
6605 last_reg = (GET_MODE (last_reg) == mode
6606 ? last_reg : gen_rtx_REG (mode, i));
6607
6608 bad_for_class = 0;
6609 for (k = 0; k < nr; k++)
6610 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].rclass],
6611 i+k);
6612
6613 /* We found a register that contains the
6614 value we need. If this register is the
6615 same as an `earlyclobber' operand of the
6616 current insn, just mark it as a place to
6617 reload from since we can't use it as the
6618 reload register itself. */
6619
6620 for (i1 = 0; i1 < n_earlyclobbers; i1++)
6621 if (reg_overlap_mentioned_for_reload_p
6622 (reg_last_reload_reg[regno],
6623 reload_earlyclobbers[i1]))
6624 break;
6625
6626 if (i1 != n_earlyclobbers
6627 || ! (free_for_value_p (i, rld[r].mode,
6628 rld[r].opnum,
6629 rld[r].when_needed, rld[r].in,
6630 rld[r].out, r, 1))
6631 /* Don't use it if we'd clobber a pseudo reg. */
6632 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
6633 && rld[r].out
6634 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
6635 /* Don't clobber the frame pointer. */
6636 || (i == HARD_FRAME_POINTER_REGNUM
6637 && frame_pointer_needed
6638 && rld[r].out)
6639 /* Don't really use the inherited spill reg
6640 if we need it wider than we've got it. */
6641 || (GET_MODE_SIZE (rld[r].mode)
6642 > GET_MODE_SIZE (mode))
6643 || bad_for_class
6644
6645 /* If find_reloads chose reload_out as reload
6646 register, stay with it - that leaves the
6647 inherited register for subsequent reloads. */
6648 || (rld[r].out && rld[r].reg_rtx
6649 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
6650 {
6651 if (! rld[r].optional)
6652 {
6653 reload_override_in[r] = last_reg;
6654 reload_inheritance_insn[r]
6655 = reg_reloaded_insn[i];
6656 }
6657 }
6658 else
6659 {
6660 int k;
6661 /* We can use this as a reload reg. */
6662 /* Mark the register as in use for this part of
6663 the insn. */
6664 mark_reload_reg_in_use (i,
6665 rld[r].opnum,
6666 rld[r].when_needed,
6667 rld[r].mode);
6668 rld[r].reg_rtx = last_reg;
6669 reload_inherited[r] = 1;
6670 reload_inheritance_insn[r]
6671 = reg_reloaded_insn[i];
6672 reload_spill_index[r] = i;
6673 for (k = 0; k < nr; k++)
6674 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6675 i + k);
6676 }
6677 }
6678 }
6679 }
6680 }
6681
6682 /* Here's another way to see if the value is already lying around. */
6683 if (inheritance
6684 && rld[r].in != 0
6685 && ! reload_inherited[r]
6686 && rld[r].out == 0
6687 && (CONSTANT_P (rld[r].in)
6688 || GET_CODE (rld[r].in) == PLUS
6689 || REG_P (rld[r].in)
6690 || MEM_P (rld[r].in))
6691 && (rld[r].nregs == max_group_size
6692 || ! reg_classes_intersect_p (rld[r].rclass, group_class)))
6693 search_equiv = rld[r].in;
6694
6695 if (search_equiv)
6696 {
6697 rtx equiv
6698 = find_equiv_reg (search_equiv, insn, rld[r].rclass,
6699 -1, NULL, 0, rld[r].mode);
6700 int regno = 0;
6701
6702 if (equiv != 0)
6703 {
6704 if (REG_P (equiv))
6705 regno = REGNO (equiv);
6706 else
6707 {
6708 /* This must be a SUBREG of a hard register.
6709 Make a new REG since this might be used in an
6710 address and not all machines support SUBREGs
6711 there. */
6712 gcc_assert (GET_CODE (equiv) == SUBREG);
6713 regno = subreg_regno (equiv);
6714 equiv = gen_rtx_REG (rld[r].mode, regno);
6715 /* If we choose EQUIV as the reload register, but the
6716 loop below decides to cancel the inheritance, we'll
6717 end up reloading EQUIV in rld[r].mode, not the mode
6718 it had originally. That isn't safe when EQUIV isn't
6719 available as a spill register since its value might
6720 still be live at this point. */
6721 for (i = regno; i < regno + (int) rld[r].nregs; i++)
6722 if (TEST_HARD_REG_BIT (reload_reg_unavailable, i))
6723 equiv = 0;
6724 }
6725 }
6726
6727 /* If we found a spill reg, reject it unless it is free
6728 and of the desired class. */
6729 if (equiv != 0)
6730 {
6731 int regs_used = 0;
6732 int bad_for_class = 0;
6733 int max_regno = regno + rld[r].nregs;
6734
6735 for (i = regno; i < max_regno; i++)
6736 {
6737 regs_used |= TEST_HARD_REG_BIT (reload_reg_used_at_all,
6738 i);
6739 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].rclass],
6740 i);
6741 }
6742
6743 if ((regs_used
6744 && ! free_for_value_p (regno, rld[r].mode,
6745 rld[r].opnum, rld[r].when_needed,
6746 rld[r].in, rld[r].out, r, 1))
6747 || bad_for_class)
6748 equiv = 0;
6749 }
6750
6751 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
6752 equiv = 0;
6753
6754 /* We found a register that contains the value we need.
6755 If this register is the same as an `earlyclobber' operand
6756 of the current insn, just mark it as a place to reload from
6757 since we can't use it as the reload register itself. */
6758
6759 if (equiv != 0)
6760 for (i = 0; i < n_earlyclobbers; i++)
6761 if (reg_overlap_mentioned_for_reload_p (equiv,
6762 reload_earlyclobbers[i]))
6763 {
6764 if (! rld[r].optional)
6765 reload_override_in[r] = equiv;
6766 equiv = 0;
6767 break;
6768 }
6769
6770 /* If the equiv register we have found is explicitly clobbered
6771 in the current insn, it depends on the reload type if we
6772 can use it, use it for reload_override_in, or not at all.
6773 In particular, we then can't use EQUIV for a
6774 RELOAD_FOR_OUTPUT_ADDRESS reload. */
6775
6776 if (equiv != 0)
6777 {
6778 if (regno_clobbered_p (regno, insn, rld[r].mode, 2))
6779 switch (rld[r].when_needed)
6780 {
6781 case RELOAD_FOR_OTHER_ADDRESS:
6782 case RELOAD_FOR_INPADDR_ADDRESS:
6783 case RELOAD_FOR_INPUT_ADDRESS:
6784 case RELOAD_FOR_OPADDR_ADDR:
6785 break;
6786 case RELOAD_OTHER:
6787 case RELOAD_FOR_INPUT:
6788 case RELOAD_FOR_OPERAND_ADDRESS:
6789 if (! rld[r].optional)
6790 reload_override_in[r] = equiv;
6791 /* Fall through. */
6792 default:
6793 equiv = 0;
6794 break;
6795 }
6796 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
6797 switch (rld[r].when_needed)
6798 {
6799 case RELOAD_FOR_OTHER_ADDRESS:
6800 case RELOAD_FOR_INPADDR_ADDRESS:
6801 case RELOAD_FOR_INPUT_ADDRESS:
6802 case RELOAD_FOR_OPADDR_ADDR:
6803 case RELOAD_FOR_OPERAND_ADDRESS:
6804 case RELOAD_FOR_INPUT:
6805 break;
6806 case RELOAD_OTHER:
6807 if (! rld[r].optional)
6808 reload_override_in[r] = equiv;
6809 /* Fall through. */
6810 default:
6811 equiv = 0;
6812 break;
6813 }
6814 }
6815
6816 /* If we found an equivalent reg, say no code need be generated
6817 to load it, and use it as our reload reg. */
6818 if (equiv != 0
6819 && (regno != HARD_FRAME_POINTER_REGNUM
6820 || !frame_pointer_needed))
6821 {
6822 int nr = hard_regno_nregs[regno][rld[r].mode];
6823 int k;
6824 rld[r].reg_rtx = equiv;
6825 reload_spill_index[r] = regno;
6826 reload_inherited[r] = 1;
6827
6828 /* If reg_reloaded_valid is not set for this register,
6829 there might be a stale spill_reg_store lying around.
6830 We must clear it, since otherwise emit_reload_insns
6831 might delete the store. */
6832 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
6833 spill_reg_store[regno] = NULL_RTX;
6834 /* If any of the hard registers in EQUIV are spill
6835 registers, mark them as in use for this insn. */
6836 for (k = 0; k < nr; k++)
6837 {
6838 i = spill_reg_order[regno + k];
6839 if (i >= 0)
6840 {
6841 mark_reload_reg_in_use (regno, rld[r].opnum,
6842 rld[r].when_needed,
6843 rld[r].mode);
6844 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6845 regno + k);
6846 }
6847 }
6848 }
6849 }
6850
6851 /* If we found a register to use already, or if this is an optional
6852 reload, we are done. */
6853 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
6854 continue;
6855
6856 #if 0
6857 /* No longer needed for correct operation. Might or might
6858 not give better code on the average. Want to experiment? */
6859
6860 /* See if there is a later reload that has a class different from our
6861 class that intersects our class or that requires less register
6862 than our reload. If so, we must allocate a register to this
6863 reload now, since that reload might inherit a previous reload
6864 and take the only available register in our class. Don't do this
6865 for optional reloads since they will force all previous reloads
6866 to be allocated. Also don't do this for reloads that have been
6867 turned off. */
6868
6869 for (i = j + 1; i < n_reloads; i++)
6870 {
6871 int s = reload_order[i];
6872
6873 if ((rld[s].in == 0 && rld[s].out == 0
6874 && ! rld[s].secondary_p)
6875 || rld[s].optional)
6876 continue;
6877
6878 if ((rld[s].rclass != rld[r].rclass
6879 && reg_classes_intersect_p (rld[r].rclass,
6880 rld[s].rclass))
6881 || rld[s].nregs < rld[r].nregs)
6882 break;
6883 }
6884
6885 if (i == n_reloads)
6886 continue;
6887
6888 allocate_reload_reg (chain, r, j == n_reloads - 1);
6889 #endif
6890 }
6891
6892 /* Now allocate reload registers for anything non-optional that
6893 didn't get one yet. */
6894 for (j = 0; j < n_reloads; j++)
6895 {
6896 int r = reload_order[j];
6897
6898 /* Ignore reloads that got marked inoperative. */
6899 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
6900 continue;
6901
6902 /* Skip reloads that already have a register allocated or are
6903 optional. */
6904 if (rld[r].reg_rtx != 0 || rld[r].optional)
6905 continue;
6906
6907 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
6908 break;
6909 }
6910
6911 /* If that loop got all the way, we have won. */
6912 if (j == n_reloads)
6913 {
6914 win = 1;
6915 break;
6916 }
6917
6918 /* Loop around and try without any inheritance. */
6919 }
6920
6921 if (! win)
6922 {
6923 /* First undo everything done by the failed attempt
6924 to allocate with inheritance. */
6925 choose_reload_regs_init (chain, save_reload_reg_rtx);
6926
6927 /* Some sanity tests to verify that the reloads found in the first
6928 pass are identical to the ones we have now. */
6929 gcc_assert (chain->n_reloads == n_reloads);
6930
6931 for (i = 0; i < n_reloads; i++)
6932 {
6933 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
6934 continue;
6935 gcc_assert (chain->rld[i].when_needed == rld[i].when_needed);
6936 for (j = 0; j < n_spills; j++)
6937 if (spill_regs[j] == chain->rld[i].regno)
6938 if (! set_reload_reg (j, i))
6939 failed_reload (chain->insn, i);
6940 }
6941 }
6942
6943 /* If we thought we could inherit a reload, because it seemed that
6944 nothing else wanted the same reload register earlier in the insn,
6945 verify that assumption, now that all reloads have been assigned.
6946 Likewise for reloads where reload_override_in has been set. */
6947
6948 /* If doing expensive optimizations, do one preliminary pass that doesn't
6949 cancel any inheritance, but removes reloads that have been needed only
6950 for reloads that we know can be inherited. */
6951 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
6952 {
6953 for (j = 0; j < n_reloads; j++)
6954 {
6955 int r = reload_order[j];
6956 rtx check_reg;
6957 if (reload_inherited[r] && rld[r].reg_rtx)
6958 check_reg = rld[r].reg_rtx;
6959 else if (reload_override_in[r]
6960 && (REG_P (reload_override_in[r])
6961 || GET_CODE (reload_override_in[r]) == SUBREG))
6962 check_reg = reload_override_in[r];
6963 else
6964 continue;
6965 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
6966 rld[r].opnum, rld[r].when_needed, rld[r].in,
6967 (reload_inherited[r]
6968 ? rld[r].out : const0_rtx),
6969 r, 1))
6970 {
6971 if (pass)
6972 continue;
6973 reload_inherited[r] = 0;
6974 reload_override_in[r] = 0;
6975 }
6976 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
6977 reload_override_in, then we do not need its related
6978 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
6979 likewise for other reload types.
6980 We handle this by removing a reload when its only replacement
6981 is mentioned in reload_in of the reload we are going to inherit.
6982 A special case are auto_inc expressions; even if the input is
6983 inherited, we still need the address for the output. We can
6984 recognize them because they have RELOAD_OUT set to RELOAD_IN.
6985 If we succeeded removing some reload and we are doing a preliminary
6986 pass just to remove such reloads, make another pass, since the
6987 removal of one reload might allow us to inherit another one. */
6988 else if (rld[r].in
6989 && rld[r].out != rld[r].in
6990 && remove_address_replacements (rld[r].in) && pass)
6991 pass = 2;
6992 }
6993 }
6994
6995 /* Now that reload_override_in is known valid,
6996 actually override reload_in. */
6997 for (j = 0; j < n_reloads; j++)
6998 if (reload_override_in[j])
6999 rld[j].in = reload_override_in[j];
7000
7001 /* If this reload won't be done because it has been canceled or is
7002 optional and not inherited, clear reload_reg_rtx so other
7003 routines (such as subst_reloads) don't get confused. */
7004 for (j = 0; j < n_reloads; j++)
7005 if (rld[j].reg_rtx != 0
7006 && ((rld[j].optional && ! reload_inherited[j])
7007 || (rld[j].in == 0 && rld[j].out == 0
7008 && ! rld[j].secondary_p)))
7009 {
7010 int regno = true_regnum (rld[j].reg_rtx);
7011
7012 if (spill_reg_order[regno] >= 0)
7013 clear_reload_reg_in_use (regno, rld[j].opnum,
7014 rld[j].when_needed, rld[j].mode);
7015 rld[j].reg_rtx = 0;
7016 reload_spill_index[j] = -1;
7017 }
7018
7019 /* Record which pseudos and which spill regs have output reloads. */
7020 for (j = 0; j < n_reloads; j++)
7021 {
7022 int r = reload_order[j];
7023
7024 i = reload_spill_index[r];
7025
7026 /* I is nonneg if this reload uses a register.
7027 If rld[r].reg_rtx is 0, this is an optional reload
7028 that we opted to ignore. */
7029 if (rld[r].out_reg != 0 && REG_P (rld[r].out_reg)
7030 && rld[r].reg_rtx != 0)
7031 {
7032 int nregno = REGNO (rld[r].out_reg);
7033 int nr = 1;
7034
7035 if (nregno < FIRST_PSEUDO_REGISTER)
7036 nr = hard_regno_nregs[nregno][rld[r].mode];
7037
7038 while (--nr >= 0)
7039 SET_REGNO_REG_SET (&reg_has_output_reload,
7040 nregno + nr);
7041
7042 if (i >= 0)
7043 add_to_hard_reg_set (&reg_is_output_reload, rld[r].mode, i);
7044
7045 gcc_assert (rld[r].when_needed == RELOAD_OTHER
7046 || rld[r].when_needed == RELOAD_FOR_OUTPUT
7047 || rld[r].when_needed == RELOAD_FOR_INSN);
7048 }
7049 }
7050 }
7051
7052 /* Deallocate the reload register for reload R. This is called from
7053 remove_address_replacements. */
7054
7055 void
7056 deallocate_reload_reg (int r)
7057 {
7058 int regno;
7059
7060 if (! rld[r].reg_rtx)
7061 return;
7062 regno = true_regnum (rld[r].reg_rtx);
7063 rld[r].reg_rtx = 0;
7064 if (spill_reg_order[regno] >= 0)
7065 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
7066 rld[r].mode);
7067 reload_spill_index[r] = -1;
7068 }
7069 \f
7070 /* These arrays are filled by emit_reload_insns and its subroutines. */
7071 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
7072 static rtx other_input_address_reload_insns = 0;
7073 static rtx other_input_reload_insns = 0;
7074 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
7075 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
7076 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
7077 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
7078 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
7079 static rtx operand_reload_insns = 0;
7080 static rtx other_operand_reload_insns = 0;
7081 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
7082
7083 /* Values to be put in spill_reg_store are put here first. Instructions
7084 must only be placed here if the associated reload register reaches
7085 the end of the instruction's reload sequence. */
7086 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
7087 static HARD_REG_SET reg_reloaded_died;
7088
7089 /* Check if *RELOAD_REG is suitable as an intermediate or scratch register
7090 of class NEW_CLASS with mode NEW_MODE. Or alternatively, if alt_reload_reg
7091 is nonzero, if that is suitable. On success, change *RELOAD_REG to the
7092 adjusted register, and return true. Otherwise, return false. */
7093 static bool
7094 reload_adjust_reg_for_temp (rtx *reload_reg, rtx alt_reload_reg,
7095 enum reg_class new_class,
7096 enum machine_mode new_mode)
7097
7098 {
7099 rtx reg;
7100
7101 for (reg = *reload_reg; reg; reg = alt_reload_reg, alt_reload_reg = 0)
7102 {
7103 unsigned regno = REGNO (reg);
7104
7105 if (!TEST_HARD_REG_BIT (reg_class_contents[(int) new_class], regno))
7106 continue;
7107 if (GET_MODE (reg) != new_mode)
7108 {
7109 if (!HARD_REGNO_MODE_OK (regno, new_mode))
7110 continue;
7111 if (hard_regno_nregs[regno][new_mode]
7112 > hard_regno_nregs[regno][GET_MODE (reg)])
7113 continue;
7114 reg = reload_adjust_reg_for_mode (reg, new_mode);
7115 }
7116 *reload_reg = reg;
7117 return true;
7118 }
7119 return false;
7120 }
7121
7122 /* Check if *RELOAD_REG is suitable as a scratch register for the reload
7123 pattern with insn_code ICODE, or alternatively, if alt_reload_reg is
7124 nonzero, if that is suitable. On success, change *RELOAD_REG to the
7125 adjusted register, and return true. Otherwise, return false. */
7126 static bool
7127 reload_adjust_reg_for_icode (rtx *reload_reg, rtx alt_reload_reg,
7128 enum insn_code icode)
7129
7130 {
7131 enum reg_class new_class = scratch_reload_class (icode);
7132 enum machine_mode new_mode = insn_data[(int) icode].operand[2].mode;
7133
7134 return reload_adjust_reg_for_temp (reload_reg, alt_reload_reg,
7135 new_class, new_mode);
7136 }
7137
7138 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
7139 has the number J. OLD contains the value to be used as input. */
7140
7141 static void
7142 emit_input_reload_insns (struct insn_chain *chain, struct reload *rl,
7143 rtx old, int j)
7144 {
7145 rtx insn = chain->insn;
7146 rtx reloadreg;
7147 rtx oldequiv_reg = 0;
7148 rtx oldequiv = 0;
7149 int special = 0;
7150 enum machine_mode mode;
7151 rtx *where;
7152
7153 /* delete_output_reload is only invoked properly if old contains
7154 the original pseudo register. Since this is replaced with a
7155 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
7156 find the pseudo in RELOAD_IN_REG. */
7157 if (reload_override_in[j]
7158 && REG_P (rl->in_reg))
7159 {
7160 oldequiv = old;
7161 old = rl->in_reg;
7162 }
7163 if (oldequiv == 0)
7164 oldequiv = old;
7165 else if (REG_P (oldequiv))
7166 oldequiv_reg = oldequiv;
7167 else if (GET_CODE (oldequiv) == SUBREG)
7168 oldequiv_reg = SUBREG_REG (oldequiv);
7169
7170 reloadreg = reload_reg_rtx_for_input[j];
7171 mode = GET_MODE (reloadreg);
7172
7173 /* If we are reloading from a register that was recently stored in
7174 with an output-reload, see if we can prove there was
7175 actually no need to store the old value in it. */
7176
7177 if (optimize && REG_P (oldequiv)
7178 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
7179 && spill_reg_store[REGNO (oldequiv)]
7180 && REG_P (old)
7181 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
7182 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
7183 rl->out_reg)))
7184 delete_output_reload (insn, j, REGNO (oldequiv), reloadreg);
7185
7186 /* Encapsulate OLDEQUIV into the reload mode, then load RELOADREG from
7187 OLDEQUIV. */
7188
7189 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
7190 oldequiv = SUBREG_REG (oldequiv);
7191 if (GET_MODE (oldequiv) != VOIDmode
7192 && mode != GET_MODE (oldequiv))
7193 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
7194
7195 /* Switch to the right place to emit the reload insns. */
7196 switch (rl->when_needed)
7197 {
7198 case RELOAD_OTHER:
7199 where = &other_input_reload_insns;
7200 break;
7201 case RELOAD_FOR_INPUT:
7202 where = &input_reload_insns[rl->opnum];
7203 break;
7204 case RELOAD_FOR_INPUT_ADDRESS:
7205 where = &input_address_reload_insns[rl->opnum];
7206 break;
7207 case RELOAD_FOR_INPADDR_ADDRESS:
7208 where = &inpaddr_address_reload_insns[rl->opnum];
7209 break;
7210 case RELOAD_FOR_OUTPUT_ADDRESS:
7211 where = &output_address_reload_insns[rl->opnum];
7212 break;
7213 case RELOAD_FOR_OUTADDR_ADDRESS:
7214 where = &outaddr_address_reload_insns[rl->opnum];
7215 break;
7216 case RELOAD_FOR_OPERAND_ADDRESS:
7217 where = &operand_reload_insns;
7218 break;
7219 case RELOAD_FOR_OPADDR_ADDR:
7220 where = &other_operand_reload_insns;
7221 break;
7222 case RELOAD_FOR_OTHER_ADDRESS:
7223 where = &other_input_address_reload_insns;
7224 break;
7225 default:
7226 gcc_unreachable ();
7227 }
7228
7229 push_to_sequence (*where);
7230
7231 /* Auto-increment addresses must be reloaded in a special way. */
7232 if (rl->out && ! rl->out_reg)
7233 {
7234 /* We are not going to bother supporting the case where a
7235 incremented register can't be copied directly from
7236 OLDEQUIV since this seems highly unlikely. */
7237 gcc_assert (rl->secondary_in_reload < 0);
7238
7239 if (reload_inherited[j])
7240 oldequiv = reloadreg;
7241
7242 old = XEXP (rl->in_reg, 0);
7243
7244 /* Prevent normal processing of this reload. */
7245 special = 1;
7246 /* Output a special code sequence for this case. */
7247 inc_for_reload (reloadreg, oldequiv, rl->out, rl->inc);
7248 }
7249
7250 /* If we are reloading a pseudo-register that was set by the previous
7251 insn, see if we can get rid of that pseudo-register entirely
7252 by redirecting the previous insn into our reload register. */
7253
7254 else if (optimize && REG_P (old)
7255 && REGNO (old) >= FIRST_PSEUDO_REGISTER
7256 && dead_or_set_p (insn, old)
7257 /* This is unsafe if some other reload
7258 uses the same reg first. */
7259 && ! conflicts_with_override (reloadreg)
7260 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
7261 rl->when_needed, old, rl->out, j, 0))
7262 {
7263 rtx temp = PREV_INSN (insn);
7264 while (temp && (NOTE_P (temp) || DEBUG_INSN_P (temp)))
7265 temp = PREV_INSN (temp);
7266 if (temp
7267 && NONJUMP_INSN_P (temp)
7268 && GET_CODE (PATTERN (temp)) == SET
7269 && SET_DEST (PATTERN (temp)) == old
7270 /* Make sure we can access insn_operand_constraint. */
7271 && asm_noperands (PATTERN (temp)) < 0
7272 /* This is unsafe if operand occurs more than once in current
7273 insn. Perhaps some occurrences aren't reloaded. */
7274 && count_occurrences (PATTERN (insn), old, 0) == 1)
7275 {
7276 rtx old = SET_DEST (PATTERN (temp));
7277 /* Store into the reload register instead of the pseudo. */
7278 SET_DEST (PATTERN (temp)) = reloadreg;
7279
7280 /* Verify that resulting insn is valid. */
7281 extract_insn (temp);
7282 if (constrain_operands (1))
7283 {
7284 /* If the previous insn is an output reload, the source is
7285 a reload register, and its spill_reg_store entry will
7286 contain the previous destination. This is now
7287 invalid. */
7288 if (REG_P (SET_SRC (PATTERN (temp)))
7289 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
7290 {
7291 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
7292 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
7293 }
7294
7295 /* If these are the only uses of the pseudo reg,
7296 pretend for GDB it lives in the reload reg we used. */
7297 if (REG_N_DEATHS (REGNO (old)) == 1
7298 && REG_N_SETS (REGNO (old)) == 1)
7299 {
7300 reg_renumber[REGNO (old)] = REGNO (reloadreg);
7301 if (ira_conflicts_p)
7302 /* Inform IRA about the change. */
7303 ira_mark_allocation_change (REGNO (old));
7304 alter_reg (REGNO (old), -1, false);
7305 }
7306 special = 1;
7307
7308 /* Adjust any debug insns between temp and insn. */
7309 while ((temp = NEXT_INSN (temp)) != insn)
7310 if (DEBUG_INSN_P (temp))
7311 replace_rtx (PATTERN (temp), old, reloadreg);
7312 else
7313 gcc_assert (NOTE_P (temp));
7314 }
7315 else
7316 {
7317 SET_DEST (PATTERN (temp)) = old;
7318 }
7319 }
7320 }
7321
7322 /* We can't do that, so output an insn to load RELOADREG. */
7323
7324 /* If we have a secondary reload, pick up the secondary register
7325 and icode, if any. If OLDEQUIV and OLD are different or
7326 if this is an in-out reload, recompute whether or not we
7327 still need a secondary register and what the icode should
7328 be. If we still need a secondary register and the class or
7329 icode is different, go back to reloading from OLD if using
7330 OLDEQUIV means that we got the wrong type of register. We
7331 cannot have different class or icode due to an in-out reload
7332 because we don't make such reloads when both the input and
7333 output need secondary reload registers. */
7334
7335 if (! special && rl->secondary_in_reload >= 0)
7336 {
7337 rtx second_reload_reg = 0;
7338 rtx third_reload_reg = 0;
7339 int secondary_reload = rl->secondary_in_reload;
7340 rtx real_oldequiv = oldequiv;
7341 rtx real_old = old;
7342 rtx tmp;
7343 enum insn_code icode;
7344 enum insn_code tertiary_icode = CODE_FOR_nothing;
7345
7346 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
7347 and similarly for OLD.
7348 See comments in get_secondary_reload in reload.c. */
7349 /* If it is a pseudo that cannot be replaced with its
7350 equivalent MEM, we must fall back to reload_in, which
7351 will have all the necessary substitutions registered.
7352 Likewise for a pseudo that can't be replaced with its
7353 equivalent constant.
7354
7355 Take extra care for subregs of such pseudos. Note that
7356 we cannot use reg_equiv_mem in this case because it is
7357 not in the right mode. */
7358
7359 tmp = oldequiv;
7360 if (GET_CODE (tmp) == SUBREG)
7361 tmp = SUBREG_REG (tmp);
7362 if (REG_P (tmp)
7363 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
7364 && (reg_equiv_memory_loc (REGNO (tmp)) != 0
7365 || reg_equiv_constant (REGNO (tmp)) != 0))
7366 {
7367 if (! reg_equiv_mem (REGNO (tmp))
7368 || num_not_at_initial_offset
7369 || GET_CODE (oldequiv) == SUBREG)
7370 real_oldequiv = rl->in;
7371 else
7372 real_oldequiv = reg_equiv_mem (REGNO (tmp));
7373 }
7374
7375 tmp = old;
7376 if (GET_CODE (tmp) == SUBREG)
7377 tmp = SUBREG_REG (tmp);
7378 if (REG_P (tmp)
7379 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
7380 && (reg_equiv_memory_loc (REGNO (tmp)) != 0
7381 || reg_equiv_constant (REGNO (tmp)) != 0))
7382 {
7383 if (! reg_equiv_mem (REGNO (tmp))
7384 || num_not_at_initial_offset
7385 || GET_CODE (old) == SUBREG)
7386 real_old = rl->in;
7387 else
7388 real_old = reg_equiv_mem (REGNO (tmp));
7389 }
7390
7391 second_reload_reg = rld[secondary_reload].reg_rtx;
7392 if (rld[secondary_reload].secondary_in_reload >= 0)
7393 {
7394 int tertiary_reload = rld[secondary_reload].secondary_in_reload;
7395
7396 third_reload_reg = rld[tertiary_reload].reg_rtx;
7397 tertiary_icode = rld[secondary_reload].secondary_in_icode;
7398 /* We'd have to add more code for quartary reloads. */
7399 gcc_assert (rld[tertiary_reload].secondary_in_reload < 0);
7400 }
7401 icode = rl->secondary_in_icode;
7402
7403 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
7404 || (rl->in != 0 && rl->out != 0))
7405 {
7406 secondary_reload_info sri, sri2;
7407 enum reg_class new_class, new_t_class;
7408
7409 sri.icode = CODE_FOR_nothing;
7410 sri.prev_sri = NULL;
7411 new_class
7412 = (enum reg_class) targetm.secondary_reload (1, real_oldequiv,
7413 rl->rclass, mode,
7414 &sri);
7415
7416 if (new_class == NO_REGS && sri.icode == CODE_FOR_nothing)
7417 second_reload_reg = 0;
7418 else if (new_class == NO_REGS)
7419 {
7420 if (reload_adjust_reg_for_icode (&second_reload_reg,
7421 third_reload_reg,
7422 (enum insn_code) sri.icode))
7423 {
7424 icode = (enum insn_code) sri.icode;
7425 third_reload_reg = 0;
7426 }
7427 else
7428 {
7429 oldequiv = old;
7430 real_oldequiv = real_old;
7431 }
7432 }
7433 else if (sri.icode != CODE_FOR_nothing)
7434 /* We currently lack a way to express this in reloads. */
7435 gcc_unreachable ();
7436 else
7437 {
7438 sri2.icode = CODE_FOR_nothing;
7439 sri2.prev_sri = &sri;
7440 new_t_class
7441 = (enum reg_class) targetm.secondary_reload (1, real_oldequiv,
7442 new_class, mode,
7443 &sri);
7444 if (new_t_class == NO_REGS && sri2.icode == CODE_FOR_nothing)
7445 {
7446 if (reload_adjust_reg_for_temp (&second_reload_reg,
7447 third_reload_reg,
7448 new_class, mode))
7449 {
7450 third_reload_reg = 0;
7451 tertiary_icode = (enum insn_code) sri2.icode;
7452 }
7453 else
7454 {
7455 oldequiv = old;
7456 real_oldequiv = real_old;
7457 }
7458 }
7459 else if (new_t_class == NO_REGS && sri2.icode != CODE_FOR_nothing)
7460 {
7461 rtx intermediate = second_reload_reg;
7462
7463 if (reload_adjust_reg_for_temp (&intermediate, NULL,
7464 new_class, mode)
7465 && reload_adjust_reg_for_icode (&third_reload_reg, NULL,
7466 ((enum insn_code)
7467 sri2.icode)))
7468 {
7469 second_reload_reg = intermediate;
7470 tertiary_icode = (enum insn_code) sri2.icode;
7471 }
7472 else
7473 {
7474 oldequiv = old;
7475 real_oldequiv = real_old;
7476 }
7477 }
7478 else if (new_t_class != NO_REGS && sri2.icode == CODE_FOR_nothing)
7479 {
7480 rtx intermediate = second_reload_reg;
7481
7482 if (reload_adjust_reg_for_temp (&intermediate, NULL,
7483 new_class, mode)
7484 && reload_adjust_reg_for_temp (&third_reload_reg, NULL,
7485 new_t_class, mode))
7486 {
7487 second_reload_reg = intermediate;
7488 tertiary_icode = (enum insn_code) sri2.icode;
7489 }
7490 else
7491 {
7492 oldequiv = old;
7493 real_oldequiv = real_old;
7494 }
7495 }
7496 else
7497 {
7498 /* This could be handled more intelligently too. */
7499 oldequiv = old;
7500 real_oldequiv = real_old;
7501 }
7502 }
7503 }
7504
7505 /* If we still need a secondary reload register, check
7506 to see if it is being used as a scratch or intermediate
7507 register and generate code appropriately. If we need
7508 a scratch register, use REAL_OLDEQUIV since the form of
7509 the insn may depend on the actual address if it is
7510 a MEM. */
7511
7512 if (second_reload_reg)
7513 {
7514 if (icode != CODE_FOR_nothing)
7515 {
7516 /* We'd have to add extra code to handle this case. */
7517 gcc_assert (!third_reload_reg);
7518
7519 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
7520 second_reload_reg));
7521 special = 1;
7522 }
7523 else
7524 {
7525 /* See if we need a scratch register to load the
7526 intermediate register (a tertiary reload). */
7527 if (tertiary_icode != CODE_FOR_nothing)
7528 {
7529 emit_insn ((GEN_FCN (tertiary_icode)
7530 (second_reload_reg, real_oldequiv,
7531 third_reload_reg)));
7532 }
7533 else if (third_reload_reg)
7534 {
7535 gen_reload (third_reload_reg, real_oldequiv,
7536 rl->opnum,
7537 rl->when_needed);
7538 gen_reload (second_reload_reg, third_reload_reg,
7539 rl->opnum,
7540 rl->when_needed);
7541 }
7542 else
7543 gen_reload (second_reload_reg, real_oldequiv,
7544 rl->opnum,
7545 rl->when_needed);
7546
7547 oldequiv = second_reload_reg;
7548 }
7549 }
7550 }
7551
7552 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
7553 {
7554 rtx real_oldequiv = oldequiv;
7555
7556 if ((REG_P (oldequiv)
7557 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
7558 && (reg_equiv_memory_loc (REGNO (oldequiv)) != 0
7559 || reg_equiv_constant (REGNO (oldequiv)) != 0))
7560 || (GET_CODE (oldequiv) == SUBREG
7561 && REG_P (SUBREG_REG (oldequiv))
7562 && (REGNO (SUBREG_REG (oldequiv))
7563 >= FIRST_PSEUDO_REGISTER)
7564 && ((reg_equiv_memory_loc (REGNO (SUBREG_REG (oldequiv))) != 0)
7565 || (reg_equiv_constant (REGNO (SUBREG_REG (oldequiv))) != 0)))
7566 || (CONSTANT_P (oldequiv)
7567 && (targetm.preferred_reload_class (oldequiv,
7568 REGNO_REG_CLASS (REGNO (reloadreg)))
7569 == NO_REGS)))
7570 real_oldequiv = rl->in;
7571 gen_reload (reloadreg, real_oldequiv, rl->opnum,
7572 rl->when_needed);
7573 }
7574
7575 if (cfun->can_throw_non_call_exceptions)
7576 copy_reg_eh_region_note_forward (insn, get_insns (), NULL);
7577
7578 /* End this sequence. */
7579 *where = get_insns ();
7580 end_sequence ();
7581
7582 /* Update reload_override_in so that delete_address_reloads_1
7583 can see the actual register usage. */
7584 if (oldequiv_reg)
7585 reload_override_in[j] = oldequiv;
7586 }
7587
7588 /* Generate insns to for the output reload RL, which is for the insn described
7589 by CHAIN and has the number J. */
7590 static void
7591 emit_output_reload_insns (struct insn_chain *chain, struct reload *rl,
7592 int j)
7593 {
7594 rtx reloadreg;
7595 rtx insn = chain->insn;
7596 int special = 0;
7597 rtx old = rl->out;
7598 enum machine_mode mode;
7599 rtx p;
7600 rtx rl_reg_rtx;
7601
7602 if (rl->when_needed == RELOAD_OTHER)
7603 start_sequence ();
7604 else
7605 push_to_sequence (output_reload_insns[rl->opnum]);
7606
7607 rl_reg_rtx = reload_reg_rtx_for_output[j];
7608 mode = GET_MODE (rl_reg_rtx);
7609
7610 reloadreg = rl_reg_rtx;
7611
7612 /* If we need two reload regs, set RELOADREG to the intermediate
7613 one, since it will be stored into OLD. We might need a secondary
7614 register only for an input reload, so check again here. */
7615
7616 if (rl->secondary_out_reload >= 0)
7617 {
7618 rtx real_old = old;
7619 int secondary_reload = rl->secondary_out_reload;
7620 int tertiary_reload = rld[secondary_reload].secondary_out_reload;
7621
7622 if (REG_P (old) && REGNO (old) >= FIRST_PSEUDO_REGISTER
7623 && reg_equiv_mem (REGNO (old)) != 0)
7624 real_old = reg_equiv_mem (REGNO (old));
7625
7626 if (secondary_reload_class (0, rl->rclass, mode, real_old) != NO_REGS)
7627 {
7628 rtx second_reloadreg = reloadreg;
7629 reloadreg = rld[secondary_reload].reg_rtx;
7630
7631 /* See if RELOADREG is to be used as a scratch register
7632 or as an intermediate register. */
7633 if (rl->secondary_out_icode != CODE_FOR_nothing)
7634 {
7635 /* We'd have to add extra code to handle this case. */
7636 gcc_assert (tertiary_reload < 0);
7637
7638 emit_insn ((GEN_FCN (rl->secondary_out_icode)
7639 (real_old, second_reloadreg, reloadreg)));
7640 special = 1;
7641 }
7642 else
7643 {
7644 /* See if we need both a scratch and intermediate reload
7645 register. */
7646
7647 enum insn_code tertiary_icode
7648 = rld[secondary_reload].secondary_out_icode;
7649
7650 /* We'd have to add more code for quartary reloads. */
7651 gcc_assert (tertiary_reload < 0
7652 || rld[tertiary_reload].secondary_out_reload < 0);
7653
7654 if (GET_MODE (reloadreg) != mode)
7655 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
7656
7657 if (tertiary_icode != CODE_FOR_nothing)
7658 {
7659 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
7660
7661 /* Copy primary reload reg to secondary reload reg.
7662 (Note that these have been swapped above, then
7663 secondary reload reg to OLD using our insn.) */
7664
7665 /* If REAL_OLD is a paradoxical SUBREG, remove it
7666 and try to put the opposite SUBREG on
7667 RELOADREG. */
7668 strip_paradoxical_subreg (&real_old, &reloadreg);
7669
7670 gen_reload (reloadreg, second_reloadreg,
7671 rl->opnum, rl->when_needed);
7672 emit_insn ((GEN_FCN (tertiary_icode)
7673 (real_old, reloadreg, third_reloadreg)));
7674 special = 1;
7675 }
7676
7677 else
7678 {
7679 /* Copy between the reload regs here and then to
7680 OUT later. */
7681
7682 gen_reload (reloadreg, second_reloadreg,
7683 rl->opnum, rl->when_needed);
7684 if (tertiary_reload >= 0)
7685 {
7686 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
7687
7688 gen_reload (third_reloadreg, reloadreg,
7689 rl->opnum, rl->when_needed);
7690 reloadreg = third_reloadreg;
7691 }
7692 }
7693 }
7694 }
7695 }
7696
7697 /* Output the last reload insn. */
7698 if (! special)
7699 {
7700 rtx set;
7701
7702 /* Don't output the last reload if OLD is not the dest of
7703 INSN and is in the src and is clobbered by INSN. */
7704 if (! flag_expensive_optimizations
7705 || !REG_P (old)
7706 || !(set = single_set (insn))
7707 || rtx_equal_p (old, SET_DEST (set))
7708 || !reg_mentioned_p (old, SET_SRC (set))
7709 || !((REGNO (old) < FIRST_PSEUDO_REGISTER)
7710 && regno_clobbered_p (REGNO (old), insn, rl->mode, 0)))
7711 gen_reload (old, reloadreg, rl->opnum,
7712 rl->when_needed);
7713 }
7714
7715 /* Look at all insns we emitted, just to be safe. */
7716 for (p = get_insns (); p; p = NEXT_INSN (p))
7717 if (INSN_P (p))
7718 {
7719 rtx pat = PATTERN (p);
7720
7721 /* If this output reload doesn't come from a spill reg,
7722 clear any memory of reloaded copies of the pseudo reg.
7723 If this output reload comes from a spill reg,
7724 reg_has_output_reload will make this do nothing. */
7725 note_stores (pat, forget_old_reloads_1, NULL);
7726
7727 if (reg_mentioned_p (rl_reg_rtx, pat))
7728 {
7729 rtx set = single_set (insn);
7730 if (reload_spill_index[j] < 0
7731 && set
7732 && SET_SRC (set) == rl_reg_rtx)
7733 {
7734 int src = REGNO (SET_SRC (set));
7735
7736 reload_spill_index[j] = src;
7737 SET_HARD_REG_BIT (reg_is_output_reload, src);
7738 if (find_regno_note (insn, REG_DEAD, src))
7739 SET_HARD_REG_BIT (reg_reloaded_died, src);
7740 }
7741 if (HARD_REGISTER_P (rl_reg_rtx))
7742 {
7743 int s = rl->secondary_out_reload;
7744 set = single_set (p);
7745 /* If this reload copies only to the secondary reload
7746 register, the secondary reload does the actual
7747 store. */
7748 if (s >= 0 && set == NULL_RTX)
7749 /* We can't tell what function the secondary reload
7750 has and where the actual store to the pseudo is
7751 made; leave new_spill_reg_store alone. */
7752 ;
7753 else if (s >= 0
7754 && SET_SRC (set) == rl_reg_rtx
7755 && SET_DEST (set) == rld[s].reg_rtx)
7756 {
7757 /* Usually the next instruction will be the
7758 secondary reload insn; if we can confirm
7759 that it is, setting new_spill_reg_store to
7760 that insn will allow an extra optimization. */
7761 rtx s_reg = rld[s].reg_rtx;
7762 rtx next = NEXT_INSN (p);
7763 rld[s].out = rl->out;
7764 rld[s].out_reg = rl->out_reg;
7765 set = single_set (next);
7766 if (set && SET_SRC (set) == s_reg
7767 && reload_reg_rtx_reaches_end_p (s_reg, s))
7768 {
7769 SET_HARD_REG_BIT (reg_is_output_reload,
7770 REGNO (s_reg));
7771 new_spill_reg_store[REGNO (s_reg)] = next;
7772 }
7773 }
7774 else if (reload_reg_rtx_reaches_end_p (rl_reg_rtx, j))
7775 new_spill_reg_store[REGNO (rl_reg_rtx)] = p;
7776 }
7777 }
7778 }
7779
7780 if (rl->when_needed == RELOAD_OTHER)
7781 {
7782 emit_insn (other_output_reload_insns[rl->opnum]);
7783 other_output_reload_insns[rl->opnum] = get_insns ();
7784 }
7785 else
7786 output_reload_insns[rl->opnum] = get_insns ();
7787
7788 if (cfun->can_throw_non_call_exceptions)
7789 copy_reg_eh_region_note_forward (insn, get_insns (), NULL);
7790
7791 end_sequence ();
7792 }
7793
7794 /* Do input reloading for reload RL, which is for the insn described by CHAIN
7795 and has the number J. */
7796 static void
7797 do_input_reload (struct insn_chain *chain, struct reload *rl, int j)
7798 {
7799 rtx insn = chain->insn;
7800 rtx old = (rl->in && MEM_P (rl->in)
7801 ? rl->in_reg : rl->in);
7802 rtx reg_rtx = rl->reg_rtx;
7803
7804 if (old && reg_rtx)
7805 {
7806 enum machine_mode mode;
7807
7808 /* Determine the mode to reload in.
7809 This is very tricky because we have three to choose from.
7810 There is the mode the insn operand wants (rl->inmode).
7811 There is the mode of the reload register RELOADREG.
7812 There is the intrinsic mode of the operand, which we could find
7813 by stripping some SUBREGs.
7814 It turns out that RELOADREG's mode is irrelevant:
7815 we can change that arbitrarily.
7816
7817 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
7818 then the reload reg may not support QImode moves, so use SImode.
7819 If foo is in memory due to spilling a pseudo reg, this is safe,
7820 because the QImode value is in the least significant part of a
7821 slot big enough for a SImode. If foo is some other sort of
7822 memory reference, then it is impossible to reload this case,
7823 so previous passes had better make sure this never happens.
7824
7825 Then consider a one-word union which has SImode and one of its
7826 members is a float, being fetched as (SUBREG:SF union:SI).
7827 We must fetch that as SFmode because we could be loading into
7828 a float-only register. In this case OLD's mode is correct.
7829
7830 Consider an immediate integer: it has VOIDmode. Here we need
7831 to get a mode from something else.
7832
7833 In some cases, there is a fourth mode, the operand's
7834 containing mode. If the insn specifies a containing mode for
7835 this operand, it overrides all others.
7836
7837 I am not sure whether the algorithm here is always right,
7838 but it does the right things in those cases. */
7839
7840 mode = GET_MODE (old);
7841 if (mode == VOIDmode)
7842 mode = rl->inmode;
7843
7844 /* We cannot use gen_lowpart_common since it can do the wrong thing
7845 when REG_RTX has a multi-word mode. Note that REG_RTX must
7846 always be a REG here. */
7847 if (GET_MODE (reg_rtx) != mode)
7848 reg_rtx = reload_adjust_reg_for_mode (reg_rtx, mode);
7849 }
7850 reload_reg_rtx_for_input[j] = reg_rtx;
7851
7852 if (old != 0
7853 /* AUTO_INC reloads need to be handled even if inherited. We got an
7854 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
7855 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
7856 && ! rtx_equal_p (reg_rtx, old)
7857 && reg_rtx != 0)
7858 emit_input_reload_insns (chain, rld + j, old, j);
7859
7860 /* When inheriting a wider reload, we have a MEM in rl->in,
7861 e.g. inheriting a SImode output reload for
7862 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
7863 if (optimize && reload_inherited[j] && rl->in
7864 && MEM_P (rl->in)
7865 && MEM_P (rl->in_reg)
7866 && reload_spill_index[j] >= 0
7867 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
7868 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
7869
7870 /* If we are reloading a register that was recently stored in with an
7871 output-reload, see if we can prove there was
7872 actually no need to store the old value in it. */
7873
7874 if (optimize
7875 && (reload_inherited[j] || reload_override_in[j])
7876 && reg_rtx
7877 && REG_P (reg_rtx)
7878 && spill_reg_store[REGNO (reg_rtx)] != 0
7879 #if 0
7880 /* There doesn't seem to be any reason to restrict this to pseudos
7881 and doing so loses in the case where we are copying from a
7882 register of the wrong class. */
7883 && !HARD_REGISTER_P (spill_reg_stored_to[REGNO (reg_rtx)])
7884 #endif
7885 /* The insn might have already some references to stackslots
7886 replaced by MEMs, while reload_out_reg still names the
7887 original pseudo. */
7888 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (reg_rtx)])
7889 || rtx_equal_p (spill_reg_stored_to[REGNO (reg_rtx)], rl->out_reg)))
7890 delete_output_reload (insn, j, REGNO (reg_rtx), reg_rtx);
7891 }
7892
7893 /* Do output reloading for reload RL, which is for the insn described by
7894 CHAIN and has the number J.
7895 ??? At some point we need to support handling output reloads of
7896 JUMP_INSNs or insns that set cc0. */
7897 static void
7898 do_output_reload (struct insn_chain *chain, struct reload *rl, int j)
7899 {
7900 rtx note, old;
7901 rtx insn = chain->insn;
7902 /* If this is an output reload that stores something that is
7903 not loaded in this same reload, see if we can eliminate a previous
7904 store. */
7905 rtx pseudo = rl->out_reg;
7906 rtx reg_rtx = rl->reg_rtx;
7907
7908 if (rl->out && reg_rtx)
7909 {
7910 enum machine_mode mode;
7911
7912 /* Determine the mode to reload in.
7913 See comments above (for input reloading). */
7914 mode = GET_MODE (rl->out);
7915 if (mode == VOIDmode)
7916 {
7917 /* VOIDmode should never happen for an output. */
7918 if (asm_noperands (PATTERN (insn)) < 0)
7919 /* It's the compiler's fault. */
7920 fatal_insn ("VOIDmode on an output", insn);
7921 error_for_asm (insn, "output operand is constant in %<asm%>");
7922 /* Prevent crash--use something we know is valid. */
7923 mode = word_mode;
7924 rl->out = gen_rtx_REG (mode, REGNO (reg_rtx));
7925 }
7926 if (GET_MODE (reg_rtx) != mode)
7927 reg_rtx = reload_adjust_reg_for_mode (reg_rtx, mode);
7928 }
7929 reload_reg_rtx_for_output[j] = reg_rtx;
7930
7931 if (pseudo
7932 && optimize
7933 && REG_P (pseudo)
7934 && ! rtx_equal_p (rl->in_reg, pseudo)
7935 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
7936 && reg_last_reload_reg[REGNO (pseudo)])
7937 {
7938 int pseudo_no = REGNO (pseudo);
7939 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
7940
7941 /* We don't need to test full validity of last_regno for
7942 inherit here; we only want to know if the store actually
7943 matches the pseudo. */
7944 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
7945 && reg_reloaded_contents[last_regno] == pseudo_no
7946 && spill_reg_store[last_regno]
7947 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
7948 delete_output_reload (insn, j, last_regno, reg_rtx);
7949 }
7950
7951 old = rl->out_reg;
7952 if (old == 0
7953 || reg_rtx == 0
7954 || rtx_equal_p (old, reg_rtx))
7955 return;
7956
7957 /* An output operand that dies right away does need a reload,
7958 but need not be copied from it. Show the new location in the
7959 REG_UNUSED note. */
7960 if ((REG_P (old) || GET_CODE (old) == SCRATCH)
7961 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
7962 {
7963 XEXP (note, 0) = reg_rtx;
7964 return;
7965 }
7966 /* Likewise for a SUBREG of an operand that dies. */
7967 else if (GET_CODE (old) == SUBREG
7968 && REG_P (SUBREG_REG (old))
7969 && 0 != (note = find_reg_note (insn, REG_UNUSED,
7970 SUBREG_REG (old))))
7971 {
7972 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old), reg_rtx);
7973 return;
7974 }
7975 else if (GET_CODE (old) == SCRATCH)
7976 /* If we aren't optimizing, there won't be a REG_UNUSED note,
7977 but we don't want to make an output reload. */
7978 return;
7979
7980 /* If is a JUMP_INSN, we can't support output reloads yet. */
7981 gcc_assert (NONJUMP_INSN_P (insn));
7982
7983 emit_output_reload_insns (chain, rld + j, j);
7984 }
7985
7986 /* A reload copies values of MODE from register SRC to register DEST.
7987 Return true if it can be treated for inheritance purposes like a
7988 group of reloads, each one reloading a single hard register. The
7989 caller has already checked that (reg:MODE SRC) and (reg:MODE DEST)
7990 occupy the same number of hard registers. */
7991
7992 static bool
7993 inherit_piecemeal_p (int dest ATTRIBUTE_UNUSED,
7994 int src ATTRIBUTE_UNUSED,
7995 enum machine_mode mode ATTRIBUTE_UNUSED)
7996 {
7997 #ifdef CANNOT_CHANGE_MODE_CLASS
7998 return (!REG_CANNOT_CHANGE_MODE_P (dest, mode, reg_raw_mode[dest])
7999 && !REG_CANNOT_CHANGE_MODE_P (src, mode, reg_raw_mode[src]));
8000 #else
8001 return true;
8002 #endif
8003 }
8004
8005 /* Output insns to reload values in and out of the chosen reload regs. */
8006
8007 static void
8008 emit_reload_insns (struct insn_chain *chain)
8009 {
8010 rtx insn = chain->insn;
8011
8012 int j;
8013
8014 CLEAR_HARD_REG_SET (reg_reloaded_died);
8015
8016 for (j = 0; j < reload_n_operands; j++)
8017 input_reload_insns[j] = input_address_reload_insns[j]
8018 = inpaddr_address_reload_insns[j]
8019 = output_reload_insns[j] = output_address_reload_insns[j]
8020 = outaddr_address_reload_insns[j]
8021 = other_output_reload_insns[j] = 0;
8022 other_input_address_reload_insns = 0;
8023 other_input_reload_insns = 0;
8024 operand_reload_insns = 0;
8025 other_operand_reload_insns = 0;
8026
8027 /* Dump reloads into the dump file. */
8028 if (dump_file)
8029 {
8030 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
8031 debug_reload_to_stream (dump_file);
8032 }
8033
8034 for (j = 0; j < n_reloads; j++)
8035 if (rld[j].reg_rtx && HARD_REGISTER_P (rld[j].reg_rtx))
8036 {
8037 unsigned int i;
8038
8039 for (i = REGNO (rld[j].reg_rtx); i < END_REGNO (rld[j].reg_rtx); i++)
8040 new_spill_reg_store[i] = 0;
8041 }
8042
8043 /* Now output the instructions to copy the data into and out of the
8044 reload registers. Do these in the order that the reloads were reported,
8045 since reloads of base and index registers precede reloads of operands
8046 and the operands may need the base and index registers reloaded. */
8047
8048 for (j = 0; j < n_reloads; j++)
8049 {
8050 do_input_reload (chain, rld + j, j);
8051 do_output_reload (chain, rld + j, j);
8052 }
8053
8054 /* Now write all the insns we made for reloads in the order expected by
8055 the allocation functions. Prior to the insn being reloaded, we write
8056 the following reloads:
8057
8058 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
8059
8060 RELOAD_OTHER reloads.
8061
8062 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
8063 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
8064 RELOAD_FOR_INPUT reload for the operand.
8065
8066 RELOAD_FOR_OPADDR_ADDRS reloads.
8067
8068 RELOAD_FOR_OPERAND_ADDRESS reloads.
8069
8070 After the insn being reloaded, we write the following:
8071
8072 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
8073 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
8074 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
8075 reloads for the operand. The RELOAD_OTHER output reloads are
8076 output in descending order by reload number. */
8077
8078 emit_insn_before (other_input_address_reload_insns, insn);
8079 emit_insn_before (other_input_reload_insns, insn);
8080
8081 for (j = 0; j < reload_n_operands; j++)
8082 {
8083 emit_insn_before (inpaddr_address_reload_insns[j], insn);
8084 emit_insn_before (input_address_reload_insns[j], insn);
8085 emit_insn_before (input_reload_insns[j], insn);
8086 }
8087
8088 emit_insn_before (other_operand_reload_insns, insn);
8089 emit_insn_before (operand_reload_insns, insn);
8090
8091 for (j = 0; j < reload_n_operands; j++)
8092 {
8093 rtx x = emit_insn_after (outaddr_address_reload_insns[j], insn);
8094 x = emit_insn_after (output_address_reload_insns[j], x);
8095 x = emit_insn_after (output_reload_insns[j], x);
8096 emit_insn_after (other_output_reload_insns[j], x);
8097 }
8098
8099 /* For all the spill regs newly reloaded in this instruction,
8100 record what they were reloaded from, so subsequent instructions
8101 can inherit the reloads.
8102
8103 Update spill_reg_store for the reloads of this insn.
8104 Copy the elements that were updated in the loop above. */
8105
8106 for (j = 0; j < n_reloads; j++)
8107 {
8108 int r = reload_order[j];
8109 int i = reload_spill_index[r];
8110
8111 /* If this is a non-inherited input reload from a pseudo, we must
8112 clear any memory of a previous store to the same pseudo. Only do
8113 something if there will not be an output reload for the pseudo
8114 being reloaded. */
8115 if (rld[r].in_reg != 0
8116 && ! (reload_inherited[r] || reload_override_in[r]))
8117 {
8118 rtx reg = rld[r].in_reg;
8119
8120 if (GET_CODE (reg) == SUBREG)
8121 reg = SUBREG_REG (reg);
8122
8123 if (REG_P (reg)
8124 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
8125 && !REGNO_REG_SET_P (&reg_has_output_reload, REGNO (reg)))
8126 {
8127 int nregno = REGNO (reg);
8128
8129 if (reg_last_reload_reg[nregno])
8130 {
8131 int last_regno = REGNO (reg_last_reload_reg[nregno]);
8132
8133 if (reg_reloaded_contents[last_regno] == nregno)
8134 spill_reg_store[last_regno] = 0;
8135 }
8136 }
8137 }
8138
8139 /* I is nonneg if this reload used a register.
8140 If rld[r].reg_rtx is 0, this is an optional reload
8141 that we opted to ignore. */
8142
8143 if (i >= 0 && rld[r].reg_rtx != 0)
8144 {
8145 int nr = hard_regno_nregs[i][GET_MODE (rld[r].reg_rtx)];
8146 int k;
8147
8148 /* For a multi register reload, we need to check if all or part
8149 of the value lives to the end. */
8150 for (k = 0; k < nr; k++)
8151 if (reload_reg_reaches_end_p (i + k, r))
8152 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
8153
8154 /* Maybe the spill reg contains a copy of reload_out. */
8155 if (rld[r].out != 0
8156 && (REG_P (rld[r].out)
8157 || (rld[r].out_reg
8158 ? REG_P (rld[r].out_reg)
8159 /* The reload value is an auto-modification of
8160 some kind. For PRE_INC, POST_INC, PRE_DEC
8161 and POST_DEC, we record an equivalence
8162 between the reload register and the operand
8163 on the optimistic assumption that we can make
8164 the equivalence hold. reload_as_needed must
8165 then either make it hold or invalidate the
8166 equivalence.
8167
8168 PRE_MODIFY and POST_MODIFY addresses are reloaded
8169 somewhat differently, and allowing them here leads
8170 to problems. */
8171 : (GET_CODE (rld[r].out) != POST_MODIFY
8172 && GET_CODE (rld[r].out) != PRE_MODIFY))))
8173 {
8174 rtx reg;
8175
8176 reg = reload_reg_rtx_for_output[r];
8177 if (reload_reg_rtx_reaches_end_p (reg, r))
8178 {
8179 enum machine_mode mode = GET_MODE (reg);
8180 int regno = REGNO (reg);
8181 int nregs = hard_regno_nregs[regno][mode];
8182 rtx out = (REG_P (rld[r].out)
8183 ? rld[r].out
8184 : rld[r].out_reg
8185 ? rld[r].out_reg
8186 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
8187 int out_regno = REGNO (out);
8188 int out_nregs = (!HARD_REGISTER_NUM_P (out_regno) ? 1
8189 : hard_regno_nregs[out_regno][mode]);
8190 bool piecemeal;
8191
8192 spill_reg_store[regno] = new_spill_reg_store[regno];
8193 spill_reg_stored_to[regno] = out;
8194 reg_last_reload_reg[out_regno] = reg;
8195
8196 piecemeal = (HARD_REGISTER_NUM_P (out_regno)
8197 && nregs == out_nregs
8198 && inherit_piecemeal_p (out_regno, regno, mode));
8199
8200 /* If OUT_REGNO is a hard register, it may occupy more than
8201 one register. If it does, say what is in the
8202 rest of the registers assuming that both registers
8203 agree on how many words the object takes. If not,
8204 invalidate the subsequent registers. */
8205
8206 if (HARD_REGISTER_NUM_P (out_regno))
8207 for (k = 1; k < out_nregs; k++)
8208 reg_last_reload_reg[out_regno + k]
8209 = (piecemeal ? regno_reg_rtx[regno + k] : 0);
8210
8211 /* Now do the inverse operation. */
8212 for (k = 0; k < nregs; k++)
8213 {
8214 CLEAR_HARD_REG_BIT (reg_reloaded_dead, regno + k);
8215 reg_reloaded_contents[regno + k]
8216 = (!HARD_REGISTER_NUM_P (out_regno) || !piecemeal
8217 ? out_regno
8218 : out_regno + k);
8219 reg_reloaded_insn[regno + k] = insn;
8220 SET_HARD_REG_BIT (reg_reloaded_valid, regno + k);
8221 if (HARD_REGNO_CALL_PART_CLOBBERED (regno + k, mode))
8222 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8223 regno + k);
8224 else
8225 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8226 regno + k);
8227 }
8228 }
8229 }
8230 /* Maybe the spill reg contains a copy of reload_in. Only do
8231 something if there will not be an output reload for
8232 the register being reloaded. */
8233 else if (rld[r].out_reg == 0
8234 && rld[r].in != 0
8235 && ((REG_P (rld[r].in)
8236 && !HARD_REGISTER_P (rld[r].in)
8237 && !REGNO_REG_SET_P (&reg_has_output_reload,
8238 REGNO (rld[r].in)))
8239 || (REG_P (rld[r].in_reg)
8240 && !REGNO_REG_SET_P (&reg_has_output_reload,
8241 REGNO (rld[r].in_reg))))
8242 && !reg_set_p (reload_reg_rtx_for_input[r], PATTERN (insn)))
8243 {
8244 rtx reg;
8245
8246 reg = reload_reg_rtx_for_input[r];
8247 if (reload_reg_rtx_reaches_end_p (reg, r))
8248 {
8249 enum machine_mode mode;
8250 int regno;
8251 int nregs;
8252 int in_regno;
8253 int in_nregs;
8254 rtx in;
8255 bool piecemeal;
8256
8257 mode = GET_MODE (reg);
8258 regno = REGNO (reg);
8259 nregs = hard_regno_nregs[regno][mode];
8260 if (REG_P (rld[r].in)
8261 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
8262 in = rld[r].in;
8263 else if (REG_P (rld[r].in_reg))
8264 in = rld[r].in_reg;
8265 else
8266 in = XEXP (rld[r].in_reg, 0);
8267 in_regno = REGNO (in);
8268
8269 in_nregs = (!HARD_REGISTER_NUM_P (in_regno) ? 1
8270 : hard_regno_nregs[in_regno][mode]);
8271
8272 reg_last_reload_reg[in_regno] = reg;
8273
8274 piecemeal = (HARD_REGISTER_NUM_P (in_regno)
8275 && nregs == in_nregs
8276 && inherit_piecemeal_p (regno, in_regno, mode));
8277
8278 if (HARD_REGISTER_NUM_P (in_regno))
8279 for (k = 1; k < in_nregs; k++)
8280 reg_last_reload_reg[in_regno + k]
8281 = (piecemeal ? regno_reg_rtx[regno + k] : 0);
8282
8283 /* Unless we inherited this reload, show we haven't
8284 recently done a store.
8285 Previous stores of inherited auto_inc expressions
8286 also have to be discarded. */
8287 if (! reload_inherited[r]
8288 || (rld[r].out && ! rld[r].out_reg))
8289 spill_reg_store[regno] = 0;
8290
8291 for (k = 0; k < nregs; k++)
8292 {
8293 CLEAR_HARD_REG_BIT (reg_reloaded_dead, regno + k);
8294 reg_reloaded_contents[regno + k]
8295 = (!HARD_REGISTER_NUM_P (in_regno) || !piecemeal
8296 ? in_regno
8297 : in_regno + k);
8298 reg_reloaded_insn[regno + k] = insn;
8299 SET_HARD_REG_BIT (reg_reloaded_valid, regno + k);
8300 if (HARD_REGNO_CALL_PART_CLOBBERED (regno + k, mode))
8301 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8302 regno + k);
8303 else
8304 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8305 regno + k);
8306 }
8307 }
8308 }
8309 }
8310
8311 /* The following if-statement was #if 0'd in 1.34 (or before...).
8312 It's reenabled in 1.35 because supposedly nothing else
8313 deals with this problem. */
8314
8315 /* If a register gets output-reloaded from a non-spill register,
8316 that invalidates any previous reloaded copy of it.
8317 But forget_old_reloads_1 won't get to see it, because
8318 it thinks only about the original insn. So invalidate it here.
8319 Also do the same thing for RELOAD_OTHER constraints where the
8320 output is discarded. */
8321 if (i < 0
8322 && ((rld[r].out != 0
8323 && (REG_P (rld[r].out)
8324 || (MEM_P (rld[r].out)
8325 && REG_P (rld[r].out_reg))))
8326 || (rld[r].out == 0 && rld[r].out_reg
8327 && REG_P (rld[r].out_reg))))
8328 {
8329 rtx out = ((rld[r].out && REG_P (rld[r].out))
8330 ? rld[r].out : rld[r].out_reg);
8331 int out_regno = REGNO (out);
8332 enum machine_mode mode = GET_MODE (out);
8333
8334 /* REG_RTX is now set or clobbered by the main instruction.
8335 As the comment above explains, forget_old_reloads_1 only
8336 sees the original instruction, and there is no guarantee
8337 that the original instruction also clobbered REG_RTX.
8338 For example, if find_reloads sees that the input side of
8339 a matched operand pair dies in this instruction, it may
8340 use the input register as the reload register.
8341
8342 Calling forget_old_reloads_1 is a waste of effort if
8343 REG_RTX is also the output register.
8344
8345 If we know that REG_RTX holds the value of a pseudo
8346 register, the code after the call will record that fact. */
8347 if (rld[r].reg_rtx && rld[r].reg_rtx != out)
8348 forget_old_reloads_1 (rld[r].reg_rtx, NULL_RTX, NULL);
8349
8350 if (!HARD_REGISTER_NUM_P (out_regno))
8351 {
8352 rtx src_reg, store_insn = NULL_RTX;
8353
8354 reg_last_reload_reg[out_regno] = 0;
8355
8356 /* If we can find a hard register that is stored, record
8357 the storing insn so that we may delete this insn with
8358 delete_output_reload. */
8359 src_reg = reload_reg_rtx_for_output[r];
8360
8361 if (src_reg)
8362 {
8363 if (reload_reg_rtx_reaches_end_p (src_reg, r))
8364 store_insn = new_spill_reg_store[REGNO (src_reg)];
8365 else
8366 src_reg = NULL_RTX;
8367 }
8368 else
8369 {
8370 /* If this is an optional reload, try to find the
8371 source reg from an input reload. */
8372 rtx set = single_set (insn);
8373 if (set && SET_DEST (set) == rld[r].out)
8374 {
8375 int k;
8376
8377 src_reg = SET_SRC (set);
8378 store_insn = insn;
8379 for (k = 0; k < n_reloads; k++)
8380 {
8381 if (rld[k].in == src_reg)
8382 {
8383 src_reg = reload_reg_rtx_for_input[k];
8384 break;
8385 }
8386 }
8387 }
8388 }
8389 if (src_reg && REG_P (src_reg)
8390 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
8391 {
8392 int src_regno, src_nregs, k;
8393 rtx note;
8394
8395 gcc_assert (GET_MODE (src_reg) == mode);
8396 src_regno = REGNO (src_reg);
8397 src_nregs = hard_regno_nregs[src_regno][mode];
8398 /* The place where to find a death note varies with
8399 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
8400 necessarily checked exactly in the code that moves
8401 notes, so just check both locations. */
8402 note = find_regno_note (insn, REG_DEAD, src_regno);
8403 if (! note && store_insn)
8404 note = find_regno_note (store_insn, REG_DEAD, src_regno);
8405 for (k = 0; k < src_nregs; k++)
8406 {
8407 spill_reg_store[src_regno + k] = store_insn;
8408 spill_reg_stored_to[src_regno + k] = out;
8409 reg_reloaded_contents[src_regno + k] = out_regno;
8410 reg_reloaded_insn[src_regno + k] = store_insn;
8411 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + k);
8412 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + k);
8413 if (HARD_REGNO_CALL_PART_CLOBBERED (src_regno + k,
8414 mode))
8415 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8416 src_regno + k);
8417 else
8418 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8419 src_regno + k);
8420 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + k);
8421 if (note)
8422 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
8423 else
8424 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
8425 }
8426 reg_last_reload_reg[out_regno] = src_reg;
8427 /* We have to set reg_has_output_reload here, or else
8428 forget_old_reloads_1 will clear reg_last_reload_reg
8429 right away. */
8430 SET_REGNO_REG_SET (&reg_has_output_reload,
8431 out_regno);
8432 }
8433 }
8434 else
8435 {
8436 int k, out_nregs = hard_regno_nregs[out_regno][mode];
8437
8438 for (k = 0; k < out_nregs; k++)
8439 reg_last_reload_reg[out_regno + k] = 0;
8440 }
8441 }
8442 }
8443 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
8444 }
8445 \f
8446 /* Go through the motions to emit INSN and test if it is strictly valid.
8447 Return the emitted insn if valid, else return NULL. */
8448
8449 static rtx
8450 emit_insn_if_valid_for_reload (rtx insn)
8451 {
8452 rtx last = get_last_insn ();
8453 int code;
8454
8455 insn = emit_insn (insn);
8456 code = recog_memoized (insn);
8457
8458 if (code >= 0)
8459 {
8460 extract_insn (insn);
8461 /* We want constrain operands to treat this insn strictly in its
8462 validity determination, i.e., the way it would after reload has
8463 completed. */
8464 if (constrain_operands (1))
8465 return insn;
8466 }
8467
8468 delete_insns_since (last);
8469 return NULL;
8470 }
8471
8472 /* Emit code to perform a reload from IN (which may be a reload register) to
8473 OUT (which may also be a reload register). IN or OUT is from operand
8474 OPNUM with reload type TYPE.
8475
8476 Returns first insn emitted. */
8477
8478 static rtx
8479 gen_reload (rtx out, rtx in, int opnum, enum reload_type type)
8480 {
8481 rtx last = get_last_insn ();
8482 rtx tem;
8483
8484 /* If IN is a paradoxical SUBREG, remove it and try to put the
8485 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
8486 if (!strip_paradoxical_subreg (&in, &out))
8487 strip_paradoxical_subreg (&out, &in);
8488
8489 /* How to do this reload can get quite tricky. Normally, we are being
8490 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
8491 register that didn't get a hard register. In that case we can just
8492 call emit_move_insn.
8493
8494 We can also be asked to reload a PLUS that adds a register or a MEM to
8495 another register, constant or MEM. This can occur during frame pointer
8496 elimination and while reloading addresses. This case is handled by
8497 trying to emit a single insn to perform the add. If it is not valid,
8498 we use a two insn sequence.
8499
8500 Or we can be asked to reload an unary operand that was a fragment of
8501 an addressing mode, into a register. If it isn't recognized as-is,
8502 we try making the unop operand and the reload-register the same:
8503 (set reg:X (unop:X expr:Y))
8504 -> (set reg:Y expr:Y) (set reg:X (unop:X reg:Y)).
8505
8506 Finally, we could be called to handle an 'o' constraint by putting
8507 an address into a register. In that case, we first try to do this
8508 with a named pattern of "reload_load_address". If no such pattern
8509 exists, we just emit a SET insn and hope for the best (it will normally
8510 be valid on machines that use 'o').
8511
8512 This entire process is made complex because reload will never
8513 process the insns we generate here and so we must ensure that
8514 they will fit their constraints and also by the fact that parts of
8515 IN might be being reloaded separately and replaced with spill registers.
8516 Because of this, we are, in some sense, just guessing the right approach
8517 here. The one listed above seems to work.
8518
8519 ??? At some point, this whole thing needs to be rethought. */
8520
8521 if (GET_CODE (in) == PLUS
8522 && (REG_P (XEXP (in, 0))
8523 || GET_CODE (XEXP (in, 0)) == SUBREG
8524 || MEM_P (XEXP (in, 0)))
8525 && (REG_P (XEXP (in, 1))
8526 || GET_CODE (XEXP (in, 1)) == SUBREG
8527 || CONSTANT_P (XEXP (in, 1))
8528 || MEM_P (XEXP (in, 1))))
8529 {
8530 /* We need to compute the sum of a register or a MEM and another
8531 register, constant, or MEM, and put it into the reload
8532 register. The best possible way of doing this is if the machine
8533 has a three-operand ADD insn that accepts the required operands.
8534
8535 The simplest approach is to try to generate such an insn and see if it
8536 is recognized and matches its constraints. If so, it can be used.
8537
8538 It might be better not to actually emit the insn unless it is valid,
8539 but we need to pass the insn as an operand to `recog' and
8540 `extract_insn' and it is simpler to emit and then delete the insn if
8541 not valid than to dummy things up. */
8542
8543 rtx op0, op1, tem, insn;
8544 enum insn_code code;
8545
8546 op0 = find_replacement (&XEXP (in, 0));
8547 op1 = find_replacement (&XEXP (in, 1));
8548
8549 /* Since constraint checking is strict, commutativity won't be
8550 checked, so we need to do that here to avoid spurious failure
8551 if the add instruction is two-address and the second operand
8552 of the add is the same as the reload reg, which is frequently
8553 the case. If the insn would be A = B + A, rearrange it so
8554 it will be A = A + B as constrain_operands expects. */
8555
8556 if (REG_P (XEXP (in, 1))
8557 && REGNO (out) == REGNO (XEXP (in, 1)))
8558 tem = op0, op0 = op1, op1 = tem;
8559
8560 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
8561 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
8562
8563 insn = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
8564 if (insn)
8565 return insn;
8566
8567 /* If that failed, we must use a conservative two-insn sequence.
8568
8569 Use a move to copy one operand into the reload register. Prefer
8570 to reload a constant, MEM or pseudo since the move patterns can
8571 handle an arbitrary operand. If OP1 is not a constant, MEM or
8572 pseudo and OP1 is not a valid operand for an add instruction, then
8573 reload OP1.
8574
8575 After reloading one of the operands into the reload register, add
8576 the reload register to the output register.
8577
8578 If there is another way to do this for a specific machine, a
8579 DEFINE_PEEPHOLE should be specified that recognizes the sequence
8580 we emit below. */
8581
8582 code = optab_handler (add_optab, GET_MODE (out));
8583
8584 if (CONSTANT_P (op1) || MEM_P (op1) || GET_CODE (op1) == SUBREG
8585 || (REG_P (op1)
8586 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
8587 || (code != CODE_FOR_nothing
8588 && !insn_operand_matches (code, 2, op1)))
8589 tem = op0, op0 = op1, op1 = tem;
8590
8591 gen_reload (out, op0, opnum, type);
8592
8593 /* If OP0 and OP1 are the same, we can use OUT for OP1.
8594 This fixes a problem on the 32K where the stack pointer cannot
8595 be used as an operand of an add insn. */
8596
8597 if (rtx_equal_p (op0, op1))
8598 op1 = out;
8599
8600 insn = emit_insn_if_valid_for_reload (gen_add2_insn (out, op1));
8601 if (insn)
8602 {
8603 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
8604 set_dst_reg_note (insn, REG_EQUIV, in, out);
8605 return insn;
8606 }
8607
8608 /* If that failed, copy the address register to the reload register.
8609 Then add the constant to the reload register. */
8610
8611 gcc_assert (!reg_overlap_mentioned_p (out, op0));
8612 gen_reload (out, op1, opnum, type);
8613 insn = emit_insn (gen_add2_insn (out, op0));
8614 set_dst_reg_note (insn, REG_EQUIV, in, out);
8615 }
8616
8617 #ifdef SECONDARY_MEMORY_NEEDED
8618 /* If we need a memory location to do the move, do it that way. */
8619 else if ((REG_P (in)
8620 || (GET_CODE (in) == SUBREG && REG_P (SUBREG_REG (in))))
8621 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
8622 && (REG_P (out)
8623 || (GET_CODE (out) == SUBREG && REG_P (SUBREG_REG (out))))
8624 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
8625 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
8626 REGNO_REG_CLASS (reg_or_subregno (out)),
8627 GET_MODE (out)))
8628 {
8629 /* Get the memory to use and rewrite both registers to its mode. */
8630 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
8631
8632 if (GET_MODE (loc) != GET_MODE (out))
8633 out = gen_rtx_REG (GET_MODE (loc), reg_or_subregno (out));
8634
8635 if (GET_MODE (loc) != GET_MODE (in))
8636 in = gen_rtx_REG (GET_MODE (loc), reg_or_subregno (in));
8637
8638 gen_reload (loc, in, opnum, type);
8639 gen_reload (out, loc, opnum, type);
8640 }
8641 #endif
8642 else if (REG_P (out) && UNARY_P (in))
8643 {
8644 rtx insn;
8645 rtx op1;
8646 rtx out_moded;
8647 rtx set;
8648
8649 op1 = find_replacement (&XEXP (in, 0));
8650 if (op1 != XEXP (in, 0))
8651 in = gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in), op1);
8652
8653 /* First, try a plain SET. */
8654 set = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
8655 if (set)
8656 return set;
8657
8658 /* If that failed, move the inner operand to the reload
8659 register, and try the same unop with the inner expression
8660 replaced with the reload register. */
8661
8662 if (GET_MODE (op1) != GET_MODE (out))
8663 out_moded = gen_rtx_REG (GET_MODE (op1), REGNO (out));
8664 else
8665 out_moded = out;
8666
8667 gen_reload (out_moded, op1, opnum, type);
8668
8669 insn
8670 = gen_rtx_SET (VOIDmode, out,
8671 gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in),
8672 out_moded));
8673 insn = emit_insn_if_valid_for_reload (insn);
8674 if (insn)
8675 {
8676 set_unique_reg_note (insn, REG_EQUIV, in);
8677 return insn;
8678 }
8679
8680 fatal_insn ("failure trying to reload:", set);
8681 }
8682 /* If IN is a simple operand, use gen_move_insn. */
8683 else if (OBJECT_P (in) || GET_CODE (in) == SUBREG)
8684 {
8685 tem = emit_insn (gen_move_insn (out, in));
8686 /* IN may contain a LABEL_REF, if so add a REG_LABEL_OPERAND note. */
8687 mark_jump_label (in, tem, 0);
8688 }
8689
8690 #ifdef HAVE_reload_load_address
8691 else if (HAVE_reload_load_address)
8692 emit_insn (gen_reload_load_address (out, in));
8693 #endif
8694
8695 /* Otherwise, just write (set OUT IN) and hope for the best. */
8696 else
8697 emit_insn (gen_rtx_SET (VOIDmode, out, in));
8698
8699 /* Return the first insn emitted.
8700 We can not just return get_last_insn, because there may have
8701 been multiple instructions emitted. Also note that gen_move_insn may
8702 emit more than one insn itself, so we can not assume that there is one
8703 insn emitted per emit_insn_before call. */
8704
8705 return last ? NEXT_INSN (last) : get_insns ();
8706 }
8707 \f
8708 /* Delete a previously made output-reload whose result we now believe
8709 is not needed. First we double-check.
8710
8711 INSN is the insn now being processed.
8712 LAST_RELOAD_REG is the hard register number for which we want to delete
8713 the last output reload.
8714 J is the reload-number that originally used REG. The caller has made
8715 certain that reload J doesn't use REG any longer for input.
8716 NEW_RELOAD_REG is reload register that reload J is using for REG. */
8717
8718 static void
8719 delete_output_reload (rtx insn, int j, int last_reload_reg, rtx new_reload_reg)
8720 {
8721 rtx output_reload_insn = spill_reg_store[last_reload_reg];
8722 rtx reg = spill_reg_stored_to[last_reload_reg];
8723 int k;
8724 int n_occurrences;
8725 int n_inherited = 0;
8726 rtx i1;
8727 rtx substed;
8728 unsigned regno;
8729 int nregs;
8730
8731 /* It is possible that this reload has been only used to set another reload
8732 we eliminated earlier and thus deleted this instruction too. */
8733 if (INSN_DELETED_P (output_reload_insn))
8734 return;
8735
8736 /* Get the raw pseudo-register referred to. */
8737
8738 while (GET_CODE (reg) == SUBREG)
8739 reg = SUBREG_REG (reg);
8740 substed = reg_equiv_memory_loc (REGNO (reg));
8741
8742 /* This is unsafe if the operand occurs more often in the current
8743 insn than it is inherited. */
8744 for (k = n_reloads - 1; k >= 0; k--)
8745 {
8746 rtx reg2 = rld[k].in;
8747 if (! reg2)
8748 continue;
8749 if (MEM_P (reg2) || reload_override_in[k])
8750 reg2 = rld[k].in_reg;
8751 #ifdef AUTO_INC_DEC
8752 if (rld[k].out && ! rld[k].out_reg)
8753 reg2 = XEXP (rld[k].in_reg, 0);
8754 #endif
8755 while (GET_CODE (reg2) == SUBREG)
8756 reg2 = SUBREG_REG (reg2);
8757 if (rtx_equal_p (reg2, reg))
8758 {
8759 if (reload_inherited[k] || reload_override_in[k] || k == j)
8760 n_inherited++;
8761 else
8762 return;
8763 }
8764 }
8765 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
8766 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
8767 n_occurrences += count_occurrences (CALL_INSN_FUNCTION_USAGE (insn),
8768 reg, 0);
8769 if (substed)
8770 n_occurrences += count_occurrences (PATTERN (insn),
8771 eliminate_regs (substed, VOIDmode,
8772 NULL_RTX), 0);
8773 for (i1 = reg_equiv_alt_mem_list (REGNO (reg)); i1; i1 = XEXP (i1, 1))
8774 {
8775 gcc_assert (!rtx_equal_p (XEXP (i1, 0), substed));
8776 n_occurrences += count_occurrences (PATTERN (insn), XEXP (i1, 0), 0);
8777 }
8778 if (n_occurrences > n_inherited)
8779 return;
8780
8781 regno = REGNO (reg);
8782 if (regno >= FIRST_PSEUDO_REGISTER)
8783 nregs = 1;
8784 else
8785 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
8786
8787 /* If the pseudo-reg we are reloading is no longer referenced
8788 anywhere between the store into it and here,
8789 and we're within the same basic block, then the value can only
8790 pass through the reload reg and end up here.
8791 Otherwise, give up--return. */
8792 for (i1 = NEXT_INSN (output_reload_insn);
8793 i1 != insn; i1 = NEXT_INSN (i1))
8794 {
8795 if (NOTE_INSN_BASIC_BLOCK_P (i1))
8796 return;
8797 if ((NONJUMP_INSN_P (i1) || CALL_P (i1))
8798 && refers_to_regno_p (regno, regno + nregs, PATTERN (i1), NULL))
8799 {
8800 /* If this is USE in front of INSN, we only have to check that
8801 there are no more references than accounted for by inheritance. */
8802 while (NONJUMP_INSN_P (i1) && GET_CODE (PATTERN (i1)) == USE)
8803 {
8804 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
8805 i1 = NEXT_INSN (i1);
8806 }
8807 if (n_occurrences <= n_inherited && i1 == insn)
8808 break;
8809 return;
8810 }
8811 }
8812
8813 /* We will be deleting the insn. Remove the spill reg information. */
8814 for (k = hard_regno_nregs[last_reload_reg][GET_MODE (reg)]; k-- > 0; )
8815 {
8816 spill_reg_store[last_reload_reg + k] = 0;
8817 spill_reg_stored_to[last_reload_reg + k] = 0;
8818 }
8819
8820 /* The caller has already checked that REG dies or is set in INSN.
8821 It has also checked that we are optimizing, and thus some
8822 inaccuracies in the debugging information are acceptable.
8823 So we could just delete output_reload_insn. But in some cases
8824 we can improve the debugging information without sacrificing
8825 optimization - maybe even improving the code: See if the pseudo
8826 reg has been completely replaced with reload regs. If so, delete
8827 the store insn and forget we had a stack slot for the pseudo. */
8828 if (rld[j].out != rld[j].in
8829 && REG_N_DEATHS (REGNO (reg)) == 1
8830 && REG_N_SETS (REGNO (reg)) == 1
8831 && REG_BASIC_BLOCK (REGNO (reg)) >= NUM_FIXED_BLOCKS
8832 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
8833 {
8834 rtx i2;
8835
8836 /* We know that it was used only between here and the beginning of
8837 the current basic block. (We also know that the last use before
8838 INSN was the output reload we are thinking of deleting, but never
8839 mind that.) Search that range; see if any ref remains. */
8840 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8841 {
8842 rtx set = single_set (i2);
8843
8844 /* Uses which just store in the pseudo don't count,
8845 since if they are the only uses, they are dead. */
8846 if (set != 0 && SET_DEST (set) == reg)
8847 continue;
8848 if (LABEL_P (i2)
8849 || JUMP_P (i2))
8850 break;
8851 if ((NONJUMP_INSN_P (i2) || CALL_P (i2))
8852 && reg_mentioned_p (reg, PATTERN (i2)))
8853 {
8854 /* Some other ref remains; just delete the output reload we
8855 know to be dead. */
8856 delete_address_reloads (output_reload_insn, insn);
8857 delete_insn (output_reload_insn);
8858 return;
8859 }
8860 }
8861
8862 /* Delete the now-dead stores into this pseudo. Note that this
8863 loop also takes care of deleting output_reload_insn. */
8864 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8865 {
8866 rtx set = single_set (i2);
8867
8868 if (set != 0 && SET_DEST (set) == reg)
8869 {
8870 delete_address_reloads (i2, insn);
8871 delete_insn (i2);
8872 }
8873 if (LABEL_P (i2)
8874 || JUMP_P (i2))
8875 break;
8876 }
8877
8878 /* For the debugging info, say the pseudo lives in this reload reg. */
8879 reg_renumber[REGNO (reg)] = REGNO (new_reload_reg);
8880 if (ira_conflicts_p)
8881 /* Inform IRA about the change. */
8882 ira_mark_allocation_change (REGNO (reg));
8883 alter_reg (REGNO (reg), -1, false);
8884 }
8885 else
8886 {
8887 delete_address_reloads (output_reload_insn, insn);
8888 delete_insn (output_reload_insn);
8889 }
8890 }
8891
8892 /* We are going to delete DEAD_INSN. Recursively delete loads of
8893 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
8894 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
8895 static void
8896 delete_address_reloads (rtx dead_insn, rtx current_insn)
8897 {
8898 rtx set = single_set (dead_insn);
8899 rtx set2, dst, prev, next;
8900 if (set)
8901 {
8902 rtx dst = SET_DEST (set);
8903 if (MEM_P (dst))
8904 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
8905 }
8906 /* If we deleted the store from a reloaded post_{in,de}c expression,
8907 we can delete the matching adds. */
8908 prev = PREV_INSN (dead_insn);
8909 next = NEXT_INSN (dead_insn);
8910 if (! prev || ! next)
8911 return;
8912 set = single_set (next);
8913 set2 = single_set (prev);
8914 if (! set || ! set2
8915 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
8916 || !CONST_INT_P (XEXP (SET_SRC (set), 1))
8917 || !CONST_INT_P (XEXP (SET_SRC (set2), 1)))
8918 return;
8919 dst = SET_DEST (set);
8920 if (! rtx_equal_p (dst, SET_DEST (set2))
8921 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
8922 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
8923 || (INTVAL (XEXP (SET_SRC (set), 1))
8924 != -INTVAL (XEXP (SET_SRC (set2), 1))))
8925 return;
8926 delete_related_insns (prev);
8927 delete_related_insns (next);
8928 }
8929
8930 /* Subfunction of delete_address_reloads: process registers found in X. */
8931 static void
8932 delete_address_reloads_1 (rtx dead_insn, rtx x, rtx current_insn)
8933 {
8934 rtx prev, set, dst, i2;
8935 int i, j;
8936 enum rtx_code code = GET_CODE (x);
8937
8938 if (code != REG)
8939 {
8940 const char *fmt = GET_RTX_FORMAT (code);
8941 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8942 {
8943 if (fmt[i] == 'e')
8944 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
8945 else if (fmt[i] == 'E')
8946 {
8947 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8948 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
8949 current_insn);
8950 }
8951 }
8952 return;
8953 }
8954
8955 if (spill_reg_order[REGNO (x)] < 0)
8956 return;
8957
8958 /* Scan backwards for the insn that sets x. This might be a way back due
8959 to inheritance. */
8960 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
8961 {
8962 code = GET_CODE (prev);
8963 if (code == CODE_LABEL || code == JUMP_INSN)
8964 return;
8965 if (!INSN_P (prev))
8966 continue;
8967 if (reg_set_p (x, PATTERN (prev)))
8968 break;
8969 if (reg_referenced_p (x, PATTERN (prev)))
8970 return;
8971 }
8972 if (! prev || INSN_UID (prev) < reload_first_uid)
8973 return;
8974 /* Check that PREV only sets the reload register. */
8975 set = single_set (prev);
8976 if (! set)
8977 return;
8978 dst = SET_DEST (set);
8979 if (!REG_P (dst)
8980 || ! rtx_equal_p (dst, x))
8981 return;
8982 if (! reg_set_p (dst, PATTERN (dead_insn)))
8983 {
8984 /* Check if DST was used in a later insn -
8985 it might have been inherited. */
8986 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
8987 {
8988 if (LABEL_P (i2))
8989 break;
8990 if (! INSN_P (i2))
8991 continue;
8992 if (reg_referenced_p (dst, PATTERN (i2)))
8993 {
8994 /* If there is a reference to the register in the current insn,
8995 it might be loaded in a non-inherited reload. If no other
8996 reload uses it, that means the register is set before
8997 referenced. */
8998 if (i2 == current_insn)
8999 {
9000 for (j = n_reloads - 1; j >= 0; j--)
9001 if ((rld[j].reg_rtx == dst && reload_inherited[j])
9002 || reload_override_in[j] == dst)
9003 return;
9004 for (j = n_reloads - 1; j >= 0; j--)
9005 if (rld[j].in && rld[j].reg_rtx == dst)
9006 break;
9007 if (j >= 0)
9008 break;
9009 }
9010 return;
9011 }
9012 if (JUMP_P (i2))
9013 break;
9014 /* If DST is still live at CURRENT_INSN, check if it is used for
9015 any reload. Note that even if CURRENT_INSN sets DST, we still
9016 have to check the reloads. */
9017 if (i2 == current_insn)
9018 {
9019 for (j = n_reloads - 1; j >= 0; j--)
9020 if ((rld[j].reg_rtx == dst && reload_inherited[j])
9021 || reload_override_in[j] == dst)
9022 return;
9023 /* ??? We can't finish the loop here, because dst might be
9024 allocated to a pseudo in this block if no reload in this
9025 block needs any of the classes containing DST - see
9026 spill_hard_reg. There is no easy way to tell this, so we
9027 have to scan till the end of the basic block. */
9028 }
9029 if (reg_set_p (dst, PATTERN (i2)))
9030 break;
9031 }
9032 }
9033 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
9034 reg_reloaded_contents[REGNO (dst)] = -1;
9035 delete_insn (prev);
9036 }
9037 \f
9038 /* Output reload-insns to reload VALUE into RELOADREG.
9039 VALUE is an autoincrement or autodecrement RTX whose operand
9040 is a register or memory location;
9041 so reloading involves incrementing that location.
9042 IN is either identical to VALUE, or some cheaper place to reload from.
9043
9044 INC_AMOUNT is the number to increment or decrement by (always positive).
9045 This cannot be deduced from VALUE. */
9046
9047 static void
9048 inc_for_reload (rtx reloadreg, rtx in, rtx value, int inc_amount)
9049 {
9050 /* REG or MEM to be copied and incremented. */
9051 rtx incloc = find_replacement (&XEXP (value, 0));
9052 /* Nonzero if increment after copying. */
9053 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
9054 || GET_CODE (value) == POST_MODIFY);
9055 rtx last;
9056 rtx inc;
9057 rtx add_insn;
9058 int code;
9059 rtx real_in = in == value ? incloc : in;
9060
9061 /* No hard register is equivalent to this register after
9062 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
9063 we could inc/dec that register as well (maybe even using it for
9064 the source), but I'm not sure it's worth worrying about. */
9065 if (REG_P (incloc))
9066 reg_last_reload_reg[REGNO (incloc)] = 0;
9067
9068 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
9069 {
9070 gcc_assert (GET_CODE (XEXP (value, 1)) == PLUS);
9071 inc = find_replacement (&XEXP (XEXP (value, 1), 1));
9072 }
9073 else
9074 {
9075 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
9076 inc_amount = -inc_amount;
9077
9078 inc = GEN_INT (inc_amount);
9079 }
9080
9081 /* If this is post-increment, first copy the location to the reload reg. */
9082 if (post && real_in != reloadreg)
9083 emit_insn (gen_move_insn (reloadreg, real_in));
9084
9085 if (in == value)
9086 {
9087 /* See if we can directly increment INCLOC. Use a method similar to
9088 that in gen_reload. */
9089
9090 last = get_last_insn ();
9091 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
9092 gen_rtx_PLUS (GET_MODE (incloc),
9093 incloc, inc)));
9094
9095 code = recog_memoized (add_insn);
9096 if (code >= 0)
9097 {
9098 extract_insn (add_insn);
9099 if (constrain_operands (1))
9100 {
9101 /* If this is a pre-increment and we have incremented the value
9102 where it lives, copy the incremented value to RELOADREG to
9103 be used as an address. */
9104
9105 if (! post)
9106 emit_insn (gen_move_insn (reloadreg, incloc));
9107 return;
9108 }
9109 }
9110 delete_insns_since (last);
9111 }
9112
9113 /* If couldn't do the increment directly, must increment in RELOADREG.
9114 The way we do this depends on whether this is pre- or post-increment.
9115 For pre-increment, copy INCLOC to the reload register, increment it
9116 there, then save back. */
9117
9118 if (! post)
9119 {
9120 if (in != reloadreg)
9121 emit_insn (gen_move_insn (reloadreg, real_in));
9122 emit_insn (gen_add2_insn (reloadreg, inc));
9123 emit_insn (gen_move_insn (incloc, reloadreg));
9124 }
9125 else
9126 {
9127 /* Postincrement.
9128 Because this might be a jump insn or a compare, and because RELOADREG
9129 may not be available after the insn in an input reload, we must do
9130 the incrementation before the insn being reloaded for.
9131
9132 We have already copied IN to RELOADREG. Increment the copy in
9133 RELOADREG, save that back, then decrement RELOADREG so it has
9134 the original value. */
9135
9136 emit_insn (gen_add2_insn (reloadreg, inc));
9137 emit_insn (gen_move_insn (incloc, reloadreg));
9138 if (CONST_INT_P (inc))
9139 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-INTVAL (inc))));
9140 else
9141 emit_insn (gen_sub2_insn (reloadreg, inc));
9142 }
9143 }
9144 \f
9145 #ifdef AUTO_INC_DEC
9146 static void
9147 add_auto_inc_notes (rtx insn, rtx x)
9148 {
9149 enum rtx_code code = GET_CODE (x);
9150 const char *fmt;
9151 int i, j;
9152
9153 if (code == MEM && auto_inc_p (XEXP (x, 0)))
9154 {
9155 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
9156 return;
9157 }
9158
9159 /* Scan all the operand sub-expressions. */
9160 fmt = GET_RTX_FORMAT (code);
9161 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9162 {
9163 if (fmt[i] == 'e')
9164 add_auto_inc_notes (insn, XEXP (x, i));
9165 else if (fmt[i] == 'E')
9166 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9167 add_auto_inc_notes (insn, XVECEXP (x, i, j));
9168 }
9169 }
9170 #endif