* i386.c (has_dispatch): Disable for Ryzen.
[official-gcc.git] / gcc / lra-spills.c
blobab33dd4241229e34d1621e6510755fd756511866
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,
298 lra_reg_info[regno].biggest_mode);
299 nr++)
300 /* Just loop. */
301 df_set_regs_ever_live (hard_regno + nr, true);
303 free (reserved_hard_regs);
304 return res;
307 /* Add pseudo REGNO to slot SLOT_NUM. */
308 static void
309 add_pseudo_to_slot (int regno, int slot_num)
311 struct pseudo_slot *first;
313 /* Each pseudo has an inherent size which comes from its own mode,
314 and a total size which provides room for paradoxical subregs.
315 We need to make sure the size and alignment of the slot are
316 sufficient for both. */
317 machine_mode mode = (GET_MODE_SIZE (PSEUDO_REGNO_MODE (regno))
318 >= GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
319 ? PSEUDO_REGNO_MODE (regno)
320 : lra_reg_info[regno].biggest_mode);
321 unsigned int align = spill_slot_alignment (mode);
322 slots[slot_num].align = MAX (slots[slot_num].align, align);
323 slots[slot_num].size = MAX (slots[slot_num].size, GET_MODE_SIZE (mode));
325 if (slots[slot_num].regno < 0)
327 /* It is the first pseudo in the slot. */
328 slots[slot_num].regno = regno;
329 pseudo_slots[regno].first = &pseudo_slots[regno];
330 pseudo_slots[regno].next = NULL;
332 else
334 first = pseudo_slots[regno].first = &pseudo_slots[slots[slot_num].regno];
335 pseudo_slots[regno].next = first->next;
336 first->next = &pseudo_slots[regno];
338 pseudo_slots[regno].mem = NULL_RTX;
339 pseudo_slots[regno].slot_num = slot_num;
340 slots[slot_num].live_ranges
341 = lra_merge_live_ranges (slots[slot_num].live_ranges,
342 lra_copy_live_range_list
343 (lra_reg_info[regno].live_ranges));
346 /* Assign stack slot numbers to pseudos in array PSEUDO_REGNOS of
347 length N. Sort pseudos in PSEUDO_REGNOS for subsequent assigning
348 memory stack slots. */
349 static void
350 assign_stack_slot_num_and_sort_pseudos (int *pseudo_regnos, int n)
352 int i, j, regno;
354 slots_num = 0;
355 /* Assign stack slot numbers to spilled pseudos, use smaller numbers
356 for most frequently used pseudos. */
357 for (i = 0; i < n; i++)
359 regno = pseudo_regnos[i];
360 if (! flag_ira_share_spill_slots)
361 j = slots_num;
362 else
364 for (j = 0; j < slots_num; j++)
365 if (slots[j].hard_regno < 0
366 && ! (lra_intersected_live_ranges_p
367 (slots[j].live_ranges,
368 lra_reg_info[regno].live_ranges)))
369 break;
371 if (j >= slots_num)
373 /* New slot. */
374 slots[j].live_ranges = NULL;
375 slots[j].size = 0;
376 slots[j].align = BITS_PER_UNIT;
377 slots[j].regno = slots[j].hard_regno = -1;
378 slots[j].mem = NULL_RTX;
379 slots_num++;
381 add_pseudo_to_slot (regno, j);
383 /* Sort regnos according to their slot numbers. */
384 qsort (pseudo_regnos, n, sizeof (int), pseudo_reg_slot_compare);
387 /* Recursively process LOC in INSN and change spilled pseudos to the
388 corresponding memory or spilled hard reg. Ignore spilled pseudos
389 created from the scratches. Return true if the pseudo nrefs equal
390 to 0 (don't change the pseudo in this case). Otherwise return false. */
391 static bool
392 remove_pseudos (rtx *loc, rtx_insn *insn)
394 int i;
395 rtx hard_reg;
396 const char *fmt;
397 enum rtx_code code;
398 bool res = false;
400 if (*loc == NULL_RTX)
401 return res;
402 code = GET_CODE (*loc);
403 if (code == REG && (i = REGNO (*loc)) >= FIRST_PSEUDO_REGISTER
404 && lra_get_regno_hard_regno (i) < 0
405 /* We do not want to assign memory for former scratches because
406 it might result in an address reload for some targets. In
407 any case we transform such pseudos not getting hard registers
408 into scratches back. */
409 && ! lra_former_scratch_p (i))
411 if (lra_reg_info[i].nrefs == 0
412 && pseudo_slots[i].mem == NULL && spill_hard_reg[i] == NULL)
413 return true;
414 if ((hard_reg = spill_hard_reg[i]) != NULL_RTX)
415 *loc = copy_rtx (hard_reg);
416 else
418 rtx x = lra_eliminate_regs_1 (insn, pseudo_slots[i].mem,
419 GET_MODE (pseudo_slots[i].mem),
420 false, false, 0, true);
421 *loc = x != pseudo_slots[i].mem ? x : copy_rtx (x);
423 return res;
426 fmt = GET_RTX_FORMAT (code);
427 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
429 if (fmt[i] == 'e')
430 res = remove_pseudos (&XEXP (*loc, i), insn) || res;
431 else if (fmt[i] == 'E')
433 int j;
435 for (j = XVECLEN (*loc, i) - 1; j >= 0; j--)
436 res = remove_pseudos (&XVECEXP (*loc, i, j), insn) || res;
439 return res;
442 /* Convert spilled pseudos into their stack slots or spill hard regs,
443 put insns to process on the constraint stack (that is all insns in
444 which pseudos were changed to memory or spill hard regs). */
445 static void
446 spill_pseudos (void)
448 basic_block bb;
449 rtx_insn *insn, *curr;
450 int i;
452 auto_bitmap spilled_pseudos (&reg_obstack);
453 auto_bitmap changed_insns (&reg_obstack);
454 for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
456 if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
457 && ! lra_former_scratch_p (i))
459 bitmap_set_bit (spilled_pseudos, i);
460 bitmap_ior_into (changed_insns, &lra_reg_info[i].insn_bitmap);
463 FOR_EACH_BB_FN (bb, cfun)
465 FOR_BB_INSNS_SAFE (bb, insn, curr)
467 bool removed_pseudo_p = false;
469 if (bitmap_bit_p (changed_insns, INSN_UID (insn)))
471 rtx *link_loc, link;
473 removed_pseudo_p = remove_pseudos (&PATTERN (insn), insn);
474 if (CALL_P (insn)
475 && remove_pseudos (&CALL_INSN_FUNCTION_USAGE (insn), insn))
476 removed_pseudo_p = true;
477 for (link_loc = &REG_NOTES (insn);
478 (link = *link_loc) != NULL_RTX;
479 link_loc = &XEXP (link, 1))
481 switch (REG_NOTE_KIND (link))
483 case REG_FRAME_RELATED_EXPR:
484 case REG_CFA_DEF_CFA:
485 case REG_CFA_ADJUST_CFA:
486 case REG_CFA_OFFSET:
487 case REG_CFA_REGISTER:
488 case REG_CFA_EXPRESSION:
489 case REG_CFA_RESTORE:
490 case REG_CFA_SET_VDRAP:
491 if (remove_pseudos (&XEXP (link, 0), insn))
492 removed_pseudo_p = true;
493 break;
494 default:
495 break;
498 if (lra_dump_file != NULL)
499 fprintf (lra_dump_file,
500 "Changing spilled pseudos to memory in insn #%u\n",
501 INSN_UID (insn));
502 lra_push_insn (insn);
503 if (lra_reg_spill_p || targetm.different_addr_displacement_p ())
504 lra_set_used_insn_alternative (insn, -1);
506 else if (CALL_P (insn)
507 /* Presence of any pseudo in CALL_INSN_FUNCTION_USAGE
508 does not affect value of insn_bitmap of the
509 corresponding lra_reg_info. That is because we
510 don't need to reload pseudos in
511 CALL_INSN_FUNCTION_USAGEs. So if we process only
512 insns in the insn_bitmap of given pseudo here, we
513 can miss the pseudo in some
514 CALL_INSN_FUNCTION_USAGEs. */
515 && remove_pseudos (&CALL_INSN_FUNCTION_USAGE (insn), insn))
516 removed_pseudo_p = true;
517 if (removed_pseudo_p)
519 lra_assert (DEBUG_INSN_P (insn));
520 lra_invalidate_insn_data (insn);
521 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
522 if (lra_dump_file != NULL)
523 fprintf (lra_dump_file,
524 "Debug insn #%u is reset because it referenced "
525 "removed pseudo\n", INSN_UID (insn));
527 bitmap_and_compl_into (df_get_live_in (bb), spilled_pseudos);
528 bitmap_and_compl_into (df_get_live_out (bb), spilled_pseudos);
533 /* Return true if we need to change some pseudos into memory. */
534 bool
535 lra_need_for_spills_p (void)
537 int i; max_regno = max_reg_num ();
539 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
540 if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
541 && ! lra_former_scratch_p (i))
542 return true;
543 return false;
546 /* Change spilled pseudos into memory or spill hard regs. Put changed
547 insns on the constraint stack (these insns will be considered on
548 the next constraint pass). The changed insns are all insns in
549 which pseudos were changed. */
550 void
551 lra_spill (void)
553 int i, n, curr_regno;
554 int *pseudo_regnos;
556 regs_num = max_reg_num ();
557 spill_hard_reg = XNEWVEC (rtx, regs_num);
558 pseudo_regnos = XNEWVEC (int, regs_num);
559 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
560 if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
561 /* We do not want to assign memory for former scratches. */
562 && ! lra_former_scratch_p (i))
563 pseudo_regnos[n++] = i;
564 lra_assert (n > 0);
565 pseudo_slots = XNEWVEC (struct pseudo_slot, regs_num);
566 for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
568 spill_hard_reg[i] = NULL_RTX;
569 pseudo_slots[i].mem = NULL_RTX;
571 slots = XNEWVEC (struct slot, regs_num);
572 /* Sort regnos according their usage frequencies. */
573 qsort (pseudo_regnos, n, sizeof (int), regno_freq_compare);
574 n = assign_spill_hard_regs (pseudo_regnos, n);
575 assign_stack_slot_num_and_sort_pseudos (pseudo_regnos, n);
576 for (i = 0; i < n; i++)
577 if (pseudo_slots[pseudo_regnos[i]].mem == NULL_RTX)
578 assign_mem_slot (pseudo_regnos[i]);
579 if (n > 0 && crtl->stack_alignment_needed)
580 /* If we have a stack frame, we must align it now. The stack size
581 may be a part of the offset computation for register
582 elimination. */
583 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
584 if (lra_dump_file != NULL)
586 for (i = 0; i < slots_num; i++)
588 fprintf (lra_dump_file, " Slot %d regnos (width = %d):", i,
589 GET_MODE_SIZE (GET_MODE (slots[i].mem)));
590 for (curr_regno = slots[i].regno;;
591 curr_regno = pseudo_slots[curr_regno].next - pseudo_slots)
593 fprintf (lra_dump_file, " %d", curr_regno);
594 if (pseudo_slots[curr_regno].next == NULL)
595 break;
597 fprintf (lra_dump_file, "\n");
600 spill_pseudos ();
601 free (slots);
602 free (pseudo_slots);
603 free (pseudo_regnos);
604 free (spill_hard_reg);
607 /* Apply alter_subreg for subregs of regs in *LOC. Use FINAL_P for
608 alter_subreg calls. Return true if any subreg of reg is
609 processed. */
610 static bool
611 alter_subregs (rtx *loc, bool final_p)
613 int i;
614 rtx x = *loc;
615 bool res;
616 const char *fmt;
617 enum rtx_code code;
619 if (x == NULL_RTX)
620 return false;
621 code = GET_CODE (x);
622 if (code == SUBREG && REG_P (SUBREG_REG (x)))
624 lra_assert (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER);
625 alter_subreg (loc, final_p);
626 return true;
628 fmt = GET_RTX_FORMAT (code);
629 res = false;
630 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
632 if (fmt[i] == 'e')
634 if (alter_subregs (&XEXP (x, i), final_p))
635 res = true;
637 else if (fmt[i] == 'E')
639 int j;
641 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
642 if (alter_subregs (&XVECEXP (x, i, j), final_p))
643 res = true;
646 return res;
649 /* Return true if REGNO is used for return in the current
650 function. */
651 static bool
652 return_regno_p (unsigned int regno)
654 rtx outgoing = crtl->return_rtx;
656 if (! outgoing)
657 return false;
659 if (REG_P (outgoing))
660 return REGNO (outgoing) == regno;
661 else if (GET_CODE (outgoing) == PARALLEL)
663 int i;
665 for (i = 0; i < XVECLEN (outgoing, 0); i++)
667 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
669 if (REG_P (x) && REGNO (x) == regno)
670 return true;
673 return false;
676 /* Return true if REGNO is in one of subsequent USE after INSN in the
677 same BB. */
678 static bool
679 regno_in_use_p (rtx_insn *insn, unsigned int regno)
681 static lra_insn_recog_data_t id;
682 static struct lra_static_insn_data *static_id;
683 struct lra_insn_reg *reg;
684 int i, arg_regno;
685 basic_block bb = BLOCK_FOR_INSN (insn);
687 while ((insn = next_nondebug_insn (insn)) != NULL_RTX)
689 if (BARRIER_P (insn) || bb != BLOCK_FOR_INSN (insn))
690 return false;
691 if (! INSN_P (insn))
692 continue;
693 if (GET_CODE (PATTERN (insn)) == USE
694 && REG_P (XEXP (PATTERN (insn), 0))
695 && regno == REGNO (XEXP (PATTERN (insn), 0)))
696 return true;
697 /* Check that the regno is not modified. */
698 id = lra_get_insn_recog_data (insn);
699 for (reg = id->regs; reg != NULL; reg = reg->next)
700 if (reg->type != OP_IN && reg->regno == (int) regno)
701 return false;
702 static_id = id->insn_static_data;
703 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
704 if (reg->type != OP_IN && reg->regno == (int) regno)
705 return false;
706 if (id->arg_hard_regs != NULL)
707 for (i = 0; (arg_regno = id->arg_hard_regs[i]) >= 0; i++)
708 if ((int) regno == (arg_regno >= FIRST_PSEUDO_REGISTER
709 ? arg_regno : arg_regno - FIRST_PSEUDO_REGISTER))
710 return false;
712 return false;
715 /* Final change of pseudos got hard registers into the corresponding
716 hard registers and removing temporary clobbers. */
717 void
718 lra_final_code_change (void)
720 int i, hard_regno;
721 basic_block bb;
722 rtx_insn *insn, *curr;
723 int max_regno = max_reg_num ();
725 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
726 if (lra_reg_info[i].nrefs != 0
727 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
728 SET_REGNO (regno_reg_rtx[i], hard_regno);
729 FOR_EACH_BB_FN (bb, cfun)
730 FOR_BB_INSNS_SAFE (bb, insn, curr)
731 if (INSN_P (insn))
733 rtx pat = PATTERN (insn);
735 if (GET_CODE (pat) == CLOBBER && LRA_TEMP_CLOBBER_P (pat))
737 /* Remove clobbers temporarily created in LRA. We don't
738 need them anymore and don't want to waste compiler
739 time processing them in a few subsequent passes. */
740 lra_invalidate_insn_data (insn);
741 delete_insn (insn);
742 continue;
745 /* IRA can generate move insns involving pseudos. It is
746 better remove them earlier to speed up compiler a bit.
747 It is also better to do it here as they might not pass
748 final RTL check in LRA, (e.g. insn moving a control
749 register into itself). So remove an useless move insn
750 unless next insn is USE marking the return reg (we should
751 save this as some subsequent optimizations assume that
752 such original insns are saved). */
753 if (NONJUMP_INSN_P (insn) && GET_CODE (pat) == SET
754 && REG_P (SET_SRC (pat)) && REG_P (SET_DEST (pat))
755 && REGNO (SET_SRC (pat)) == REGNO (SET_DEST (pat))
756 && (! return_regno_p (REGNO (SET_SRC (pat)))
757 || ! regno_in_use_p (insn, REGNO (SET_SRC (pat)))))
759 lra_invalidate_insn_data (insn);
760 delete_insn (insn);
761 continue;
764 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
765 struct lra_insn_reg *reg;
767 for (reg = id->regs; reg != NULL; reg = reg->next)
768 if (reg->regno >= FIRST_PSEUDO_REGISTER
769 && lra_reg_info [reg->regno].nrefs == 0)
770 break;
772 if (reg != NULL)
774 /* Pseudos still can be in debug insns in some very rare
775 and complicated cases, e.g. the pseudo was removed by
776 inheritance and the debug insn is not EBBs where the
777 inheritance happened. It is difficult and time
778 consuming to find what hard register corresponds the
779 pseudo -- so just remove the debug insn. Another
780 solution could be assigning hard reg/memory but it
781 would be a misleading info. It is better not to have
782 info than have it wrong. */
783 lra_assert (DEBUG_INSN_P (insn));
784 lra_invalidate_insn_data (insn);
785 delete_insn (insn);
786 continue;
789 struct lra_static_insn_data *static_id = id->insn_static_data;
790 bool insn_change_p = false;
792 for (i = id->insn_static_data->n_operands - 1; i >= 0; i--)
793 if ((DEBUG_INSN_P (insn) || ! static_id->operand[i].is_operator)
794 && alter_subregs (id->operand_loc[i], ! DEBUG_INSN_P (insn)))
796 lra_update_dup (id, i);
797 insn_change_p = true;
799 if (insn_change_p)
800 lra_update_operator_dups (id);