Fix date
[official-gcc.git] / gcc / lra-spills.c
blob3df6f6786a398a8532ee1f617b21d9865eb5c9f1
1 /* Change pseudos by memory.
2 Copyright (C) 2010-2017 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
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 3, or (at your option) any later
10 version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for a pass to change spilled pseudos into
23 memory.
25 The pass creates necessary stack slots and assigns spilled pseudos
26 to the stack slots in following way:
28 for all spilled pseudos P most frequently used first do
29 for all stack slots S do
30 if P doesn't conflict with pseudos assigned to S then
31 assign S to P and goto to the next pseudo process
32 end
33 end
34 create new stack slot S and assign P to S
35 end
37 The actual algorithm is bit more complicated because of different
38 pseudo sizes.
40 After that the code changes spilled pseudos (except ones created
41 from scratches) by corresponding stack slot memory in RTL.
43 If at least one stack slot was created, we need to run more passes
44 because we have new addresses which should be checked and because
45 the old address displacements might change and address constraints
46 (or insn memory constraints) might not be satisfied any more.
48 For some targets, the pass can spill some pseudos into hard
49 registers of different class (usually into vector registers)
50 instead of spilling them into memory if it is possible and
51 profitable. Spilling GENERAL_REGS pseudo into SSE registers for
52 Intel Corei7 is an example of such optimization. And this is
53 actually recommended by Intel optimization guide.
55 The file also contains code for final change of pseudos on hard
56 regs correspondingly assigned to them. */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "backend.h"
62 #include "target.h"
63 #include "rtl.h"
64 #include "df.h"
65 #include "insn-config.h"
66 #include "regs.h"
67 #include "memmodel.h"
68 #include "ira.h"
69 #include "recog.h"
70 #include "output.h"
71 #include "cfgrtl.h"
72 #include "lra.h"
73 #include "lra-int.h"
76 /* Max regno at the start of the pass. */
77 static int regs_num;
79 /* Map spilled regno -> hard regno used instead of memory for
80 spilling. */
81 static rtx *spill_hard_reg;
83 /* The structure describes stack slot of a spilled pseudo. */
84 struct pseudo_slot
86 /* Number (0, 1, ...) of the stack slot to which given pseudo
87 belongs. */
88 int slot_num;
89 /* First or next slot with the same slot number. */
90 struct pseudo_slot *next, *first;
91 /* Memory representing the spilled pseudo. */
92 rtx mem;
95 /* The stack slots for each spilled pseudo. Indexed by regnos. */
96 static struct pseudo_slot *pseudo_slots;
98 /* The structure describes a register or a stack slot which can be
99 used for several spilled pseudos. */
100 struct slot
102 /* First pseudo with given stack slot. */
103 int regno;
104 /* Hard reg into which the slot pseudos are spilled. The value is
105 negative for pseudos spilled into memory. */
106 int hard_regno;
107 /* Maximum alignment required by all users of the slot. */
108 unsigned int align;
109 /* Maximum size required by all users of the slot. */
110 HOST_WIDE_INT size;
111 /* Memory representing the all stack slot. It can be different from
112 memory representing a pseudo belonging to give stack slot because
113 pseudo can be placed in a part of the corresponding stack slot.
114 The value is NULL for pseudos spilled into a hard reg. */
115 rtx mem;
116 /* Combined live ranges of all pseudos belonging to given slot. It
117 is used to figure out that a new spilled pseudo can use given
118 stack slot. */
119 lra_live_range_t live_ranges;
122 /* Array containing info about the stack slots. The array element is
123 indexed by the stack slot number in the range [0..slots_num). */
124 static struct slot *slots;
125 /* The number of the stack slots currently existing. */
126 static int slots_num;
128 /* Set up memory of the spilled pseudo I. The function can allocate
129 the corresponding stack slot if it is not done yet. */
130 static void
131 assign_mem_slot (int i)
133 rtx x = NULL_RTX;
134 machine_mode mode = GET_MODE (regno_reg_rtx[i]);
135 HOST_WIDE_INT inherent_size = PSEUDO_REGNO_BYTES (i);
136 machine_mode wider_mode
137 = (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
138 ? mode : lra_reg_info[i].biggest_mode);
139 HOST_WIDE_INT total_size = GET_MODE_SIZE (wider_mode);
140 HOST_WIDE_INT adjust = 0;
142 lra_assert (regno_reg_rtx[i] != NULL_RTX && REG_P (regno_reg_rtx[i])
143 && lra_reg_info[i].nrefs != 0 && reg_renumber[i] < 0);
145 unsigned int slot_num = pseudo_slots[i].slot_num;
146 x = slots[slot_num].mem;
147 if (!x)
149 x = assign_stack_local (BLKmode, slots[slot_num].size,
150 slots[slot_num].align);
151 slots[slot_num].mem = x;
154 /* On a big endian machine, the "address" of the slot is the address
155 of the low part that fits its inherent mode. */
156 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
157 adjust += (total_size - inherent_size);
159 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
161 /* Set all of the memory attributes as appropriate for a spill. */
162 set_mem_attrs_for_spill (x);
163 pseudo_slots[i].mem = x;
166 /* Sort pseudos according their usage frequencies. */
167 static int
168 regno_freq_compare (const void *v1p, const void *v2p)
170 const int regno1 = *(const int *) v1p;
171 const int regno2 = *(const int *) v2p;
172 int diff;
174 if ((diff = lra_reg_info[regno2].freq - lra_reg_info[regno1].freq) != 0)
175 return diff;
176 return regno1 - regno2;
179 /* Sort pseudos according to their slots, putting the slots in the order
180 that they should be allocated. Slots with lower numbers have the highest
181 priority and should get the smallest displacement from the stack or
182 frame pointer (whichever is being used).
184 The first allocated slot is always closest to the frame pointer,
185 so prefer lower slot numbers when frame_pointer_needed. If the stack
186 and frame grow in the same direction, then the first allocated slot is
187 always closest to the initial stack pointer and furthest away from the
188 final stack pointer, so allocate higher numbers first when using the
189 stack pointer in that case. The reverse is true if the stack and
190 frame grow in opposite directions. */
191 static int
192 pseudo_reg_slot_compare (const void *v1p, const void *v2p)
194 const int regno1 = *(const int *) v1p;
195 const int regno2 = *(const int *) v2p;
196 int diff, slot_num1, slot_num2;
197 int total_size1, total_size2;
199 slot_num1 = pseudo_slots[regno1].slot_num;
200 slot_num2 = pseudo_slots[regno2].slot_num;
201 if ((diff = slot_num1 - slot_num2) != 0)
202 return (frame_pointer_needed
203 || (!FRAME_GROWS_DOWNWARD) == STACK_GROWS_DOWNWARD ? diff : -diff);
204 total_size1 = GET_MODE_SIZE (lra_reg_info[regno1].biggest_mode);
205 total_size2 = GET_MODE_SIZE (lra_reg_info[regno2].biggest_mode);
206 if ((diff = total_size2 - total_size1) != 0)
207 return diff;
208 return regno1 - regno2;
211 /* Assign spill hard registers to N pseudos in PSEUDO_REGNOS which is
212 sorted in order of highest frequency first. Put the pseudos which
213 did not get a spill hard register at the beginning of array
214 PSEUDO_REGNOS. Return the number of such pseudos. */
215 static int
216 assign_spill_hard_regs (int *pseudo_regnos, int n)
218 int i, k, p, regno, res, spill_class_size, hard_regno, nr;
219 enum reg_class rclass, spill_class;
220 machine_mode mode;
221 lra_live_range_t r;
222 rtx_insn *insn;
223 rtx set;
224 basic_block bb;
225 HARD_REG_SET conflict_hard_regs;
226 bitmap setjump_crosses = regstat_get_setjmp_crosses ();
227 /* Hard registers which can not be used for any purpose at given
228 program point because they are unallocatable or already allocated
229 for other pseudos. */
230 HARD_REG_SET *reserved_hard_regs;
232 if (! lra_reg_spill_p)
233 return n;
234 /* Set up reserved hard regs for every program point. */
235 reserved_hard_regs = XNEWVEC (HARD_REG_SET, lra_live_max_point);
236 for (p = 0; p < lra_live_max_point; p++)
237 COPY_HARD_REG_SET (reserved_hard_regs[p], lra_no_alloc_regs);
238 for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
239 if (lra_reg_info[i].nrefs != 0
240 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
241 for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
242 for (p = r->start; p <= r->finish; p++)
243 add_to_hard_reg_set (&reserved_hard_regs[p],
244 lra_reg_info[i].biggest_mode, hard_regno);
245 auto_bitmap ok_insn_bitmap (&reg_obstack);
246 FOR_EACH_BB_FN (bb, cfun)
247 FOR_BB_INSNS (bb, insn)
248 if (DEBUG_INSN_P (insn)
249 || ((set = single_set (insn)) != NULL_RTX
250 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))))
251 bitmap_set_bit (ok_insn_bitmap, INSN_UID (insn));
252 for (res = i = 0; i < n; i++)
254 regno = pseudo_regnos[i];
255 rclass = lra_get_allocno_class (regno);
256 if (bitmap_bit_p (setjump_crosses, regno)
257 || (spill_class
258 = ((enum reg_class)
259 targetm.spill_class ((reg_class_t) rclass,
260 PSEUDO_REGNO_MODE (regno)))) == NO_REGS
261 || bitmap_intersect_compl_p (&lra_reg_info[regno].insn_bitmap,
262 ok_insn_bitmap))
264 pseudo_regnos[res++] = regno;
265 continue;
267 lra_assert (spill_class != NO_REGS);
268 COPY_HARD_REG_SET (conflict_hard_regs,
269 lra_reg_info[regno].conflict_hard_regs);
270 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
271 for (p = r->start; p <= r->finish; p++)
272 IOR_HARD_REG_SET (conflict_hard_regs, reserved_hard_regs[p]);
273 spill_class_size = ira_class_hard_regs_num[spill_class];
274 mode = lra_reg_info[regno].biggest_mode;
275 for (k = 0; k < spill_class_size; k++)
277 hard_regno = ira_class_hard_regs[spill_class][k];
278 if (! overlaps_hard_reg_set_p (conflict_hard_regs, mode, hard_regno))
279 break;
281 if (k >= spill_class_size)
283 /* There is no available regs -- assign memory later. */
284 pseudo_regnos[res++] = regno;
285 continue;
287 if (lra_dump_file != NULL)
288 fprintf (lra_dump_file, " Spill r%d into hr%d\n", regno, hard_regno);
289 /* Update reserved_hard_regs. */
290 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
291 for (p = r->start; p <= r->finish; p++)
292 add_to_hard_reg_set (&reserved_hard_regs[p],
293 lra_reg_info[regno].biggest_mode, hard_regno);
294 spill_hard_reg[regno]
295 = gen_raw_REG (PSEUDO_REGNO_MODE (regno), hard_regno);
296 for (nr = 0;
297 nr < hard_regno_nregs[hard_regno][lra_reg_info[regno].biggest_mode];
298 nr++)
299 /* Just loop. */
300 df_set_regs_ever_live (hard_regno + nr, true);
302 free (reserved_hard_regs);
303 return res;
306 /* Add pseudo REGNO to slot SLOT_NUM. */
307 static void
308 add_pseudo_to_slot (int regno, int slot_num)
310 struct pseudo_slot *first;
312 /* Each pseudo has an inherent size which comes from its own mode,
313 and a total size which provides room for paradoxical subregs.
314 We need to make sure the size and alignment of the slot are
315 sufficient for both. */
316 machine_mode mode = (GET_MODE_SIZE (PSEUDO_REGNO_MODE (regno))
317 >= GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
318 ? PSEUDO_REGNO_MODE (regno)
319 : lra_reg_info[regno].biggest_mode);
320 unsigned int align = spill_slot_alignment (mode);
321 slots[slot_num].align = MAX (slots[slot_num].align, align);
322 slots[slot_num].size = MAX (slots[slot_num].size, GET_MODE_SIZE (mode));
324 if (slots[slot_num].regno < 0)
326 /* It is the first pseudo in the slot. */
327 slots[slot_num].regno = regno;
328 pseudo_slots[regno].first = &pseudo_slots[regno];
329 pseudo_slots[regno].next = NULL;
331 else
333 first = pseudo_slots[regno].first = &pseudo_slots[slots[slot_num].regno];
334 pseudo_slots[regno].next = first->next;
335 first->next = &pseudo_slots[regno];
337 pseudo_slots[regno].mem = NULL_RTX;
338 pseudo_slots[regno].slot_num = slot_num;
339 slots[slot_num].live_ranges
340 = lra_merge_live_ranges (slots[slot_num].live_ranges,
341 lra_copy_live_range_list
342 (lra_reg_info[regno].live_ranges));
345 /* Assign stack slot numbers to pseudos in array PSEUDO_REGNOS of
346 length N. Sort pseudos in PSEUDO_REGNOS for subsequent assigning
347 memory stack slots. */
348 static void
349 assign_stack_slot_num_and_sort_pseudos (int *pseudo_regnos, int n)
351 int i, j, regno;
353 slots_num = 0;
354 /* Assign stack slot numbers to spilled pseudos, use smaller numbers
355 for most frequently used pseudos. */
356 for (i = 0; i < n; i++)
358 regno = pseudo_regnos[i];
359 if (! flag_ira_share_spill_slots)
360 j = slots_num;
361 else
363 for (j = 0; j < slots_num; j++)
364 if (slots[j].hard_regno < 0
365 && ! (lra_intersected_live_ranges_p
366 (slots[j].live_ranges,
367 lra_reg_info[regno].live_ranges)))
368 break;
370 if (j >= slots_num)
372 /* New slot. */
373 slots[j].live_ranges = NULL;
374 slots[j].size = 0;
375 slots[j].align = BITS_PER_UNIT;
376 slots[j].regno = slots[j].hard_regno = -1;
377 slots[j].mem = NULL_RTX;
378 slots_num++;
380 add_pseudo_to_slot (regno, j);
382 /* Sort regnos according to their slot numbers. */
383 qsort (pseudo_regnos, n, sizeof (int), pseudo_reg_slot_compare);
386 /* Recursively process LOC in INSN and change spilled pseudos to the
387 corresponding memory or spilled hard reg. Ignore spilled pseudos
388 created from the scratches. Return true if the pseudo nrefs equal
389 to 0 (don't change the pseudo in this case). Otherwise return false. */
390 static bool
391 remove_pseudos (rtx *loc, rtx_insn *insn)
393 int i;
394 rtx hard_reg;
395 const char *fmt;
396 enum rtx_code code;
397 bool res = false;
399 if (*loc == NULL_RTX)
400 return res;
401 code = GET_CODE (*loc);
402 if (code == REG && (i = REGNO (*loc)) >= FIRST_PSEUDO_REGISTER
403 && lra_get_regno_hard_regno (i) < 0
404 /* We do not want to assign memory for former scratches because
405 it might result in an address reload for some targets. In
406 any case we transform such pseudos not getting hard registers
407 into scratches back. */
408 && ! lra_former_scratch_p (i))
410 if (lra_reg_info[i].nrefs == 0
411 && pseudo_slots[i].mem == NULL && spill_hard_reg[i] == NULL)
412 return true;
413 if ((hard_reg = spill_hard_reg[i]) != NULL_RTX)
414 *loc = copy_rtx (hard_reg);
415 else
417 rtx x = lra_eliminate_regs_1 (insn, pseudo_slots[i].mem,
418 GET_MODE (pseudo_slots[i].mem),
419 false, false, 0, true);
420 *loc = x != pseudo_slots[i].mem ? x : copy_rtx (x);
422 return res;
425 fmt = GET_RTX_FORMAT (code);
426 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
428 if (fmt[i] == 'e')
429 res = remove_pseudos (&XEXP (*loc, i), insn) || res;
430 else if (fmt[i] == 'E')
432 int j;
434 for (j = XVECLEN (*loc, i) - 1; j >= 0; j--)
435 res = remove_pseudos (&XVECEXP (*loc, i, j), insn) || res;
438 return res;
441 /* Convert spilled pseudos into their stack slots or spill hard regs,
442 put insns to process on the constraint stack (that is all insns in
443 which pseudos were changed to memory or spill hard regs). */
444 static void
445 spill_pseudos (void)
447 basic_block bb;
448 rtx_insn *insn, *curr;
449 int i;
451 auto_bitmap spilled_pseudos (&reg_obstack);
452 auto_bitmap changed_insns (&reg_obstack);
453 for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
455 if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
456 && ! lra_former_scratch_p (i))
458 bitmap_set_bit (spilled_pseudos, i);
459 bitmap_ior_into (changed_insns, &lra_reg_info[i].insn_bitmap);
462 FOR_EACH_BB_FN (bb, cfun)
464 FOR_BB_INSNS_SAFE (bb, insn, curr)
466 bool removed_pseudo_p = false;
468 if (bitmap_bit_p (changed_insns, INSN_UID (insn)))
470 rtx *link_loc, link;
472 removed_pseudo_p = remove_pseudos (&PATTERN (insn), insn);
473 if (CALL_P (insn)
474 && remove_pseudos (&CALL_INSN_FUNCTION_USAGE (insn), insn))
475 removed_pseudo_p = true;
476 for (link_loc = &REG_NOTES (insn);
477 (link = *link_loc) != NULL_RTX;
478 link_loc = &XEXP (link, 1))
480 switch (REG_NOTE_KIND (link))
482 case REG_FRAME_RELATED_EXPR:
483 case REG_CFA_DEF_CFA:
484 case REG_CFA_ADJUST_CFA:
485 case REG_CFA_OFFSET:
486 case REG_CFA_REGISTER:
487 case REG_CFA_EXPRESSION:
488 case REG_CFA_RESTORE:
489 case REG_CFA_SET_VDRAP:
490 if (remove_pseudos (&XEXP (link, 0), insn))
491 removed_pseudo_p = true;
492 break;
493 default:
494 break;
497 if (lra_dump_file != NULL)
498 fprintf (lra_dump_file,
499 "Changing spilled pseudos to memory in insn #%u\n",
500 INSN_UID (insn));
501 lra_push_insn (insn);
502 if (lra_reg_spill_p || targetm.different_addr_displacement_p ())
503 lra_set_used_insn_alternative (insn, -1);
505 else if (CALL_P (insn)
506 /* Presence of any pseudo in CALL_INSN_FUNCTION_USAGE
507 does not affect value of insn_bitmap of the
508 corresponding lra_reg_info. That is because we
509 don't need to reload pseudos in
510 CALL_INSN_FUNCTION_USAGEs. So if we process only
511 insns in the insn_bitmap of given pseudo here, we
512 can miss the pseudo in some
513 CALL_INSN_FUNCTION_USAGEs. */
514 && remove_pseudos (&CALL_INSN_FUNCTION_USAGE (insn), insn))
515 removed_pseudo_p = true;
516 if (removed_pseudo_p)
518 lra_assert (DEBUG_INSN_P (insn));
519 lra_invalidate_insn_data (insn);
520 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
521 if (lra_dump_file != NULL)
522 fprintf (lra_dump_file,
523 "Debug insn #%u is reset because it referenced "
524 "removed pseudo\n", INSN_UID (insn));
526 bitmap_and_compl_into (df_get_live_in (bb), spilled_pseudos);
527 bitmap_and_compl_into (df_get_live_out (bb), spilled_pseudos);
532 /* Return true if we need to change some pseudos into memory. */
533 bool
534 lra_need_for_spills_p (void)
536 int i; max_regno = max_reg_num ();
538 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
539 if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
540 && ! lra_former_scratch_p (i))
541 return true;
542 return false;
545 /* Change spilled pseudos into memory or spill hard regs. Put changed
546 insns on the constraint stack (these insns will be considered on
547 the next constraint pass). The changed insns are all insns in
548 which pseudos were changed. */
549 void
550 lra_spill (void)
552 int i, n, curr_regno;
553 int *pseudo_regnos;
555 regs_num = max_reg_num ();
556 spill_hard_reg = XNEWVEC (rtx, regs_num);
557 pseudo_regnos = XNEWVEC (int, regs_num);
558 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
559 if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
560 /* We do not want to assign memory for former scratches. */
561 && ! lra_former_scratch_p (i))
562 pseudo_regnos[n++] = i;
563 lra_assert (n > 0);
564 pseudo_slots = XNEWVEC (struct pseudo_slot, regs_num);
565 for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
567 spill_hard_reg[i] = NULL_RTX;
568 pseudo_slots[i].mem = NULL_RTX;
570 slots = XNEWVEC (struct slot, regs_num);
571 /* Sort regnos according their usage frequencies. */
572 qsort (pseudo_regnos, n, sizeof (int), regno_freq_compare);
573 n = assign_spill_hard_regs (pseudo_regnos, n);
574 assign_stack_slot_num_and_sort_pseudos (pseudo_regnos, n);
575 for (i = 0; i < n; i++)
576 if (pseudo_slots[pseudo_regnos[i]].mem == NULL_RTX)
577 assign_mem_slot (pseudo_regnos[i]);
578 if (n > 0 && crtl->stack_alignment_needed)
579 /* If we have a stack frame, we must align it now. The stack size
580 may be a part of the offset computation for register
581 elimination. */
582 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
583 if (lra_dump_file != NULL)
585 for (i = 0; i < slots_num; i++)
587 fprintf (lra_dump_file, " Slot %d regnos (width = %d):", i,
588 GET_MODE_SIZE (GET_MODE (slots[i].mem)));
589 for (curr_regno = slots[i].regno;;
590 curr_regno = pseudo_slots[curr_regno].next - pseudo_slots)
592 fprintf (lra_dump_file, " %d", curr_regno);
593 if (pseudo_slots[curr_regno].next == NULL)
594 break;
596 fprintf (lra_dump_file, "\n");
599 spill_pseudos ();
600 free (slots);
601 free (pseudo_slots);
602 free (pseudo_regnos);
603 free (spill_hard_reg);
606 /* Apply alter_subreg for subregs of regs in *LOC. Use FINAL_P for
607 alter_subreg calls. Return true if any subreg of reg is
608 processed. */
609 static bool
610 alter_subregs (rtx *loc, bool final_p)
612 int i;
613 rtx x = *loc;
614 bool res;
615 const char *fmt;
616 enum rtx_code code;
618 if (x == NULL_RTX)
619 return false;
620 code = GET_CODE (x);
621 if (code == SUBREG && REG_P (SUBREG_REG (x)))
623 lra_assert (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER);
624 alter_subreg (loc, final_p);
625 return true;
627 fmt = GET_RTX_FORMAT (code);
628 res = false;
629 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
631 if (fmt[i] == 'e')
633 if (alter_subregs (&XEXP (x, i), final_p))
634 res = true;
636 else if (fmt[i] == 'E')
638 int j;
640 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
641 if (alter_subregs (&XVECEXP (x, i, j), final_p))
642 res = true;
645 return res;
648 /* Return true if REGNO is used for return in the current
649 function. */
650 static bool
651 return_regno_p (unsigned int regno)
653 rtx outgoing = crtl->return_rtx;
655 if (! outgoing)
656 return false;
658 if (REG_P (outgoing))
659 return REGNO (outgoing) == regno;
660 else if (GET_CODE (outgoing) == PARALLEL)
662 int i;
664 for (i = 0; i < XVECLEN (outgoing, 0); i++)
666 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
668 if (REG_P (x) && REGNO (x) == regno)
669 return true;
672 return false;
675 /* Return true if REGNO is in one of subsequent USE after INSN in the
676 same BB. */
677 static bool
678 regno_in_use_p (rtx_insn *insn, unsigned int regno)
680 static lra_insn_recog_data_t id;
681 static struct lra_static_insn_data *static_id;
682 struct lra_insn_reg *reg;
683 int i, arg_regno;
684 basic_block bb = BLOCK_FOR_INSN (insn);
686 while ((insn = next_nondebug_insn (insn)) != NULL_RTX)
688 if (BARRIER_P (insn) || bb != BLOCK_FOR_INSN (insn))
689 return false;
690 if (! INSN_P (insn))
691 continue;
692 if (GET_CODE (PATTERN (insn)) == USE
693 && REG_P (XEXP (PATTERN (insn), 0))
694 && regno == REGNO (XEXP (PATTERN (insn), 0)))
695 return true;
696 /* Check that the regno is not modified. */
697 id = lra_get_insn_recog_data (insn);
698 for (reg = id->regs; reg != NULL; reg = reg->next)
699 if (reg->type != OP_IN && reg->regno == (int) regno)
700 return false;
701 static_id = id->insn_static_data;
702 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
703 if (reg->type != OP_IN && reg->regno == (int) regno)
704 return false;
705 if (id->arg_hard_regs != NULL)
706 for (i = 0; (arg_regno = id->arg_hard_regs[i]) >= 0; i++)
707 if ((int) regno == (arg_regno >= FIRST_PSEUDO_REGISTER
708 ? arg_regno : arg_regno - FIRST_PSEUDO_REGISTER))
709 return false;
711 return false;
714 /* Final change of pseudos got hard registers into the corresponding
715 hard registers and removing temporary clobbers. */
716 void
717 lra_final_code_change (void)
719 int i, hard_regno;
720 basic_block bb;
721 rtx_insn *insn, *curr;
722 int max_regno = max_reg_num ();
724 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
725 if (lra_reg_info[i].nrefs != 0
726 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
727 SET_REGNO (regno_reg_rtx[i], hard_regno);
728 FOR_EACH_BB_FN (bb, cfun)
729 FOR_BB_INSNS_SAFE (bb, insn, curr)
730 if (INSN_P (insn))
732 rtx pat = PATTERN (insn);
734 if (GET_CODE (pat) == CLOBBER && LRA_TEMP_CLOBBER_P (pat))
736 /* Remove clobbers temporarily created in LRA. We don't
737 need them anymore and don't want to waste compiler
738 time processing them in a few subsequent passes. */
739 lra_invalidate_insn_data (insn);
740 delete_insn (insn);
741 continue;
744 /* IRA can generate move insns involving pseudos. It is
745 better remove them earlier to speed up compiler a bit.
746 It is also better to do it here as they might not pass
747 final RTL check in LRA, (e.g. insn moving a control
748 register into itself). So remove an useless move insn
749 unless next insn is USE marking the return reg (we should
750 save this as some subsequent optimizations assume that
751 such original insns are saved). */
752 if (NONJUMP_INSN_P (insn) && GET_CODE (pat) == SET
753 && REG_P (SET_SRC (pat)) && REG_P (SET_DEST (pat))
754 && REGNO (SET_SRC (pat)) == REGNO (SET_DEST (pat))
755 && (! return_regno_p (REGNO (SET_SRC (pat)))
756 || ! regno_in_use_p (insn, REGNO (SET_SRC (pat)))))
758 lra_invalidate_insn_data (insn);
759 delete_insn (insn);
760 continue;
763 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
764 struct lra_insn_reg *reg;
766 for (reg = id->regs; reg != NULL; reg = reg->next)
767 if (reg->regno >= FIRST_PSEUDO_REGISTER
768 && lra_reg_info [reg->regno].nrefs == 0)
769 break;
771 if (reg != NULL)
773 /* Pseudos still can be in debug insns in some very rare
774 and complicated cases, e.g. the pseudo was removed by
775 inheritance and the debug insn is not EBBs where the
776 inheritance happened. It is difficult and time
777 consuming to find what hard register corresponds the
778 pseudo -- so just remove the debug insn. Another
779 solution could be assigning hard reg/memory but it
780 would be a misleading info. It is better not to have
781 info than have it wrong. */
782 lra_assert (DEBUG_INSN_P (insn));
783 lra_invalidate_insn_data (insn);
784 delete_insn (insn);
785 continue;
788 struct lra_static_insn_data *static_id = id->insn_static_data;
789 bool insn_change_p = false;
791 for (i = id->insn_static_data->n_operands - 1; i >= 0; i--)
792 if ((DEBUG_INSN_P (insn) || ! static_id->operand[i].is_operator)
793 && alter_subregs (id->operand_loc[i], ! DEBUG_INSN_P (insn)))
795 lra_update_dup (id, i);
796 insn_change_p = true;
798 if (insn_change_p)
799 lra_update_operator_dups (id);