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