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