]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lra-spills.c
[testsuite] Avoid duplicate test names in sizeless tests
[thirdparty/gcc.git] / gcc / lra-spills.c
CommitLineData
55a2c322 1/* Change pseudos by memory.
8d9254fc 2 Copyright (C) 2010-2020 Free Software Foundation, Inc.
55a2c322
VM
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21
22/* This file contains code for a pass to change spilled pseudos into
23 memory.
24
25 The pass creates necessary stack slots and assigns spilled pseudos
26 to the stack slots in following way:
27
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
f4eafc30 36
55a2c322
VM
37 The actual algorithm is bit more complicated because of different
38 pseudo sizes.
39
40 After that the code changes spilled pseudos (except ones created
41 from scratches) by corresponding stack slot memory in RTL.
42
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.
47
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.
54
55 The file also contains code for final change of pseudos on hard
56 regs correspondingly assigned to them. */
57
58#include "config.h"
59#include "system.h"
60#include "coretypes.h"
c7131fb2 61#include "backend.h"
957060b5 62#include "target.h"
55a2c322 63#include "rtl.h"
c7131fb2 64#include "df.h"
55a2c322 65#include "insn-config.h"
957060b5 66#include "regs.h"
4d0cdd0c 67#include "memmodel.h"
957060b5 68#include "ira.h"
55a2c322
VM
69#include "recog.h"
70#include "output.h"
60393bbc 71#include "cfgrtl.h"
c7131fb2 72#include "lra.h"
55a2c322 73#include "lra-int.h"
55a2c322
VM
74
75
76/* Max regno at the start of the pass. */
77static int regs_num;
78
79/* Map spilled regno -> hard regno used instead of memory for
80 spilling. */
81static rtx *spill_hard_reg;
82
83/* The structure describes stack slot of a spilled pseudo. */
84struct pseudo_slot
85{
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;
93};
94
95/* The stack slots for each spilled pseudo. Indexed by regnos. */
96static struct pseudo_slot *pseudo_slots;
97
98/* The structure describes a register or a stack slot which can be
99 used for several spilled pseudos. */
6c1dae73 100class slot
55a2c322 101{
6c1dae73 102public:
55a2c322
VM
103 /* First pseudo with given stack slot. */
104 int regno;
105 /* Hard reg into which the slot pseudos are spilled. The value is
106 negative for pseudos spilled into memory. */
107 int hard_regno;
83d0488b
RS
108 /* Maximum alignment required by all users of the slot. */
109 unsigned int align;
110 /* Maximum size required by all users of the slot. */
cf098191 111 poly_int64 size;
55a2c322
VM
112 /* Memory representing the all stack slot. It can be different from
113 memory representing a pseudo belonging to give stack slot because
114 pseudo can be placed in a part of the corresponding stack slot.
115 The value is NULL for pseudos spilled into a hard reg. */
116 rtx mem;
117 /* Combined live ranges of all pseudos belonging to given slot. It
118 is used to figure out that a new spilled pseudo can use given
119 stack slot. */
120 lra_live_range_t live_ranges;
121};
122
123/* Array containing info about the stack slots. The array element is
124 indexed by the stack slot number in the range [0..slots_num). */
99b1c316 125static class slot *slots;
55a2c322
VM
126/* The number of the stack slots currently existing. */
127static int slots_num;
128
129/* Set up memory of the spilled pseudo I. The function can allocate
130 the corresponding stack slot if it is not done yet. */
131static void
132assign_mem_slot (int i)
133{
134 rtx x = NULL_RTX;
ef4bddc2 135 machine_mode mode = GET_MODE (regno_reg_rtx[i]);
cf098191 136 poly_int64 inherent_size = PSEUDO_REGNO_BYTES (i);
83d0488b 137 machine_mode wider_mode
bd5a2c67 138 = wider_subreg_mode (mode, lra_reg_info[i].biggest_mode);
cf098191 139 poly_int64 total_size = GET_MODE_SIZE (wider_mode);
91914e56 140 poly_int64 adjust = 0;
55a2c322
VM
141
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);
f4eafc30 144
83d0488b
RS
145 unsigned int slot_num = pseudo_slots[i].slot_num;
146 x = slots[slot_num].mem;
147 if (!x)
55a2c322 148 {
83d0488b
RS
149 x = assign_stack_local (BLKmode, slots[slot_num].size,
150 slots[slot_num].align);
151 slots[slot_num].mem = x;
55a2c322 152 }
f4eafc30 153
55a2c322
VM
154 /* On a big endian machine, the "address" of the slot is the address
155 of the low part that fits its inherent mode. */
e10326ff 156 adjust += subreg_size_lowpart_offset (inherent_size, total_size);
55a2c322 157 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
f4eafc30 158
55a2c322
VM
159 /* Set all of the memory attributes as appropriate for a spill. */
160 set_mem_attrs_for_spill (x);
161 pseudo_slots[i].mem = x;
162}
163
164/* Sort pseudos according their usage frequencies. */
165static int
166regno_freq_compare (const void *v1p, const void *v2p)
167{
168 const int regno1 = *(const int *) v1p;
169 const int regno2 = *(const int *) v2p;
170 int diff;
171
172 if ((diff = lra_reg_info[regno2].freq - lra_reg_info[regno1].freq) != 0)
173 return diff;
174 return regno1 - regno2;
175}
176
55a2c322 177/* Sort pseudos according to their slots, putting the slots in the order
799d6b90
RS
178 that they should be allocated.
179
180 First prefer to group slots with variable sizes together and slots
181 with constant sizes together, since that usually makes them easier
182 to address from a common anchor point. E.g. loads of polynomial-sized
183 registers tend to take polynomial offsets while loads of constant-sized
184 registers tend to take constant (non-polynomial) offsets.
185
186 Next, slots with lower numbers have the highest priority and should
187 get the smallest displacement from the stack or frame pointer
188 (whichever is being used).
55a2c322
VM
189
190 The first allocated slot is always closest to the frame pointer,
191 so prefer lower slot numbers when frame_pointer_needed. If the stack
192 and frame grow in the same direction, then the first allocated slot is
193 always closest to the initial stack pointer and furthest away from the
194 final stack pointer, so allocate higher numbers first when using the
195 stack pointer in that case. The reverse is true if the stack and
196 frame grow in opposite directions. */
197static int
198pseudo_reg_slot_compare (const void *v1p, const void *v2p)
199{
200 const int regno1 = *(const int *) v1p;
201 const int regno2 = *(const int *) v2p;
202 int diff, slot_num1, slot_num2;
55a2c322
VM
203
204 slot_num1 = pseudo_slots[regno1].slot_num;
205 slot_num2 = pseudo_slots[regno2].slot_num;
799d6b90
RS
206 diff = (int (slots[slot_num1].size.is_constant ())
207 - int (slots[slot_num2].size.is_constant ()));
208 if (diff != 0)
209 return diff;
55a2c322
VM
210 if ((diff = slot_num1 - slot_num2) != 0)
211 return (frame_pointer_needed
e0bf0dc2 212 || (!FRAME_GROWS_DOWNWARD) == STACK_GROWS_DOWNWARD ? diff : -diff);
cf098191
RS
213 poly_int64 total_size1 = GET_MODE_SIZE (lra_reg_info[regno1].biggest_mode);
214 poly_int64 total_size2 = GET_MODE_SIZE (lra_reg_info[regno2].biggest_mode);
215 if ((diff = compare_sizes_for_sort (total_size2, total_size1)) != 0)
55a2c322
VM
216 return diff;
217 return regno1 - regno2;
218}
219
220/* Assign spill hard registers to N pseudos in PSEUDO_REGNOS which is
221 sorted in order of highest frequency first. Put the pseudos which
222 did not get a spill hard register at the beginning of array
223 PSEUDO_REGNOS. Return the number of such pseudos. */
224static int
225assign_spill_hard_regs (int *pseudo_regnos, int n)
226{
227 int i, k, p, regno, res, spill_class_size, hard_regno, nr;
228 enum reg_class rclass, spill_class;
ef4bddc2 229 machine_mode mode;
55a2c322 230 lra_live_range_t r;
cfa434f6
DM
231 rtx_insn *insn;
232 rtx set;
55a2c322
VM
233 basic_block bb;
234 HARD_REG_SET conflict_hard_regs;
55a2c322 235 bitmap setjump_crosses = regstat_get_setjmp_crosses ();
67914693 236 /* Hard registers which cannot be used for any purpose at given
55a2c322 237 program point because they are unallocatable or already allocated
f4eafc30 238 for other pseudos. */
55a2c322
VM
239 HARD_REG_SET *reserved_hard_regs;
240
241 if (! lra_reg_spill_p)
242 return n;
243 /* Set up reserved hard regs for every program point. */
244 reserved_hard_regs = XNEWVEC (HARD_REG_SET, lra_live_max_point);
245 for (p = 0; p < lra_live_max_point; p++)
6576d245 246 reserved_hard_regs[p] = lra_no_alloc_regs;
55a2c322
VM
247 for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
248 if (lra_reg_info[i].nrefs != 0
249 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
250 for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
251 for (p = r->start; p <= r->finish; p++)
252 add_to_hard_reg_set (&reserved_hard_regs[p],
253 lra_reg_info[i].biggest_mode, hard_regno);
d648b5ff 254 auto_bitmap ok_insn_bitmap (&reg_obstack);
11cd3bed 255 FOR_EACH_BB_FN (bb, cfun)
55a2c322
VM
256 FOR_BB_INSNS (bb, insn)
257 if (DEBUG_INSN_P (insn)
258 || ((set = single_set (insn)) != NULL_RTX
259 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))))
d648b5ff 260 bitmap_set_bit (ok_insn_bitmap, INSN_UID (insn));
55a2c322
VM
261 for (res = i = 0; i < n; i++)
262 {
263 regno = pseudo_regnos[i];
264 rclass = lra_get_allocno_class (regno);
265 if (bitmap_bit_p (setjump_crosses, regno)
266 || (spill_class
267 = ((enum reg_class)
268 targetm.spill_class ((reg_class_t) rclass,
269 PSEUDO_REGNO_MODE (regno)))) == NO_REGS
270 || bitmap_intersect_compl_p (&lra_reg_info[regno].insn_bitmap,
d648b5ff 271 ok_insn_bitmap))
55a2c322
VM
272 {
273 pseudo_regnos[res++] = regno;
274 continue;
275 }
276 lra_assert (spill_class != NO_REGS);
6576d245 277 conflict_hard_regs = lra_reg_info[regno].conflict_hard_regs;
55a2c322
VM
278 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
279 for (p = r->start; p <= r->finish; p++)
44942965 280 conflict_hard_regs |= reserved_hard_regs[p];
55a2c322
VM
281 spill_class_size = ira_class_hard_regs_num[spill_class];
282 mode = lra_reg_info[regno].biggest_mode;
283 for (k = 0; k < spill_class_size; k++)
284 {
285 hard_regno = ira_class_hard_regs[spill_class][k];
1a4ec325
KCY
286 if (TEST_HARD_REG_BIT (eliminable_regset, hard_regno)
287 || !targetm.hard_regno_mode_ok (hard_regno, mode))
4b77a380 288 continue;
55a2c322
VM
289 if (! overlaps_hard_reg_set_p (conflict_hard_regs, mode, hard_regno))
290 break;
291 }
292 if (k >= spill_class_size)
293 {
294 /* There is no available regs -- assign memory later. */
295 pseudo_regnos[res++] = regno;
296 continue;
297 }
298 if (lra_dump_file != NULL)
299 fprintf (lra_dump_file, " Spill r%d into hr%d\n", regno, hard_regno);
54178a01
TV
300 add_to_hard_reg_set (&hard_regs_spilled_into,
301 lra_reg_info[regno].biggest_mode, hard_regno);
55a2c322
VM
302 /* Update reserved_hard_regs. */
303 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
304 for (p = r->start; p <= r->finish; p++)
305 add_to_hard_reg_set (&reserved_hard_regs[p],
306 lra_reg_info[regno].biggest_mode, hard_regno);
307 spill_hard_reg[regno]
308 = gen_raw_REG (PSEUDO_REGNO_MODE (regno), hard_regno);
309 for (nr = 0;
ad474626
RS
310 nr < hard_regno_nregs (hard_regno,
311 lra_reg_info[regno].biggest_mode);
55a2c322 312 nr++)
7a59fa3a
RS
313 /* Just loop. */
314 df_set_regs_ever_live (hard_regno + nr, true);
55a2c322 315 }
55a2c322
VM
316 free (reserved_hard_regs);
317 return res;
318}
319
320/* Add pseudo REGNO to slot SLOT_NUM. */
321static void
322add_pseudo_to_slot (int regno, int slot_num)
323{
324 struct pseudo_slot *first;
325
83d0488b
RS
326 /* Each pseudo has an inherent size which comes from its own mode,
327 and a total size which provides room for paradoxical subregs.
328 We need to make sure the size and alignment of the slot are
329 sufficient for both. */
bd5a2c67
RS
330 machine_mode mode = wider_subreg_mode (PSEUDO_REGNO_MODE (regno),
331 lra_reg_info[regno].biggest_mode);
83d0488b
RS
332 unsigned int align = spill_slot_alignment (mode);
333 slots[slot_num].align = MAX (slots[slot_num].align, align);
cf098191
RS
334 slots[slot_num].size = upper_bound (slots[slot_num].size,
335 GET_MODE_SIZE (mode));
83d0488b 336
55a2c322
VM
337 if (slots[slot_num].regno < 0)
338 {
339 /* It is the first pseudo in the slot. */
340 slots[slot_num].regno = regno;
341 pseudo_slots[regno].first = &pseudo_slots[regno];
342 pseudo_slots[regno].next = NULL;
343 }
344 else
345 {
346 first = pseudo_slots[regno].first = &pseudo_slots[slots[slot_num].regno];
347 pseudo_slots[regno].next = first->next;
348 first->next = &pseudo_slots[regno];
349 }
350 pseudo_slots[regno].mem = NULL_RTX;
351 pseudo_slots[regno].slot_num = slot_num;
352 slots[slot_num].live_ranges
353 = lra_merge_live_ranges (slots[slot_num].live_ranges,
354 lra_copy_live_range_list
355 (lra_reg_info[regno].live_ranges));
356}
357
358/* Assign stack slot numbers to pseudos in array PSEUDO_REGNOS of
359 length N. Sort pseudos in PSEUDO_REGNOS for subsequent assigning
360 memory stack slots. */
361static void
362assign_stack_slot_num_and_sort_pseudos (int *pseudo_regnos, int n)
363{
364 int i, j, regno;
365
366 slots_num = 0;
367 /* Assign stack slot numbers to spilled pseudos, use smaller numbers
368 for most frequently used pseudos. */
369 for (i = 0; i < n; i++)
370 {
371 regno = pseudo_regnos[i];
372 if (! flag_ira_share_spill_slots)
373 j = slots_num;
374 else
375 {
799d6b90
RS
376 machine_mode mode
377 = wider_subreg_mode (PSEUDO_REGNO_MODE (regno),
378 lra_reg_info[regno].biggest_mode);
55a2c322
VM
379 for (j = 0; j < slots_num; j++)
380 if (slots[j].hard_regno < 0
799d6b90
RS
381 /* Although it's possible to share slots between modes
382 with constant and non-constant widths, we usually
383 get better spill code by keeping the constant and
384 non-constant areas separate. */
385 && (GET_MODE_SIZE (mode).is_constant ()
386 == slots[j].size.is_constant ())
55a2c322
VM
387 && ! (lra_intersected_live_ranges_p
388 (slots[j].live_ranges,
389 lra_reg_info[regno].live_ranges)))
390 break;
391 }
392 if (j >= slots_num)
393 {
394 /* New slot. */
395 slots[j].live_ranges = NULL;
83d0488b
RS
396 slots[j].size = 0;
397 slots[j].align = BITS_PER_UNIT;
55a2c322
VM
398 slots[j].regno = slots[j].hard_regno = -1;
399 slots[j].mem = NULL_RTX;
400 slots_num++;
401 }
402 add_pseudo_to_slot (regno, j);
403 }
404 /* Sort regnos according to their slot numbers. */
405 qsort (pseudo_regnos, n, sizeof (int), pseudo_reg_slot_compare);
406}
407
408/* Recursively process LOC in INSN and change spilled pseudos to the
409 corresponding memory or spilled hard reg. Ignore spilled pseudos
49abe076
VM
410 created from the scratches. Return true if the pseudo nrefs equal
411 to 0 (don't change the pseudo in this case). Otherwise return false. */
412static bool
cfa434f6 413remove_pseudos (rtx *loc, rtx_insn *insn)
55a2c322
VM
414{
415 int i;
416 rtx hard_reg;
417 const char *fmt;
418 enum rtx_code code;
49abe076
VM
419 bool res = false;
420
55a2c322 421 if (*loc == NULL_RTX)
49abe076 422 return res;
55a2c322 423 code = GET_CODE (*loc);
a4504f32
VM
424 if (code == SUBREG && REG_P (SUBREG_REG (*loc)))
425 {
426 /* Try to remove memory subregs to simplify LRA job
427 and avoid LRA cycling in case of subreg memory reload. */
428 res = remove_pseudos (&SUBREG_REG (*loc), insn);
429 if (GET_CODE (SUBREG_REG (*loc)) == MEM)
430 alter_subreg (loc, false);
431 return res;
432 }
433 else if (code == REG && (i = REGNO (*loc)) >= FIRST_PSEUDO_REGISTER
55a2c322
VM
434 && lra_get_regno_hard_regno (i) < 0
435 /* We do not want to assign memory for former scratches because
436 it might result in an address reload for some targets. In
437 any case we transform such pseudos not getting hard registers
438 into scratches back. */
439 && ! lra_former_scratch_p (i))
440 {
49abe076
VM
441 if (lra_reg_info[i].nrefs == 0
442 && pseudo_slots[i].mem == NULL && spill_hard_reg[i] == NULL)
443 return true;
8d49e7ef
VM
444 if ((hard_reg = spill_hard_reg[i]) != NULL_RTX)
445 *loc = copy_rtx (hard_reg);
446 else
447 {
448 rtx x = lra_eliminate_regs_1 (insn, pseudo_slots[i].mem,
449 GET_MODE (pseudo_slots[i].mem),
a6af1bf9 450 false, false, 0, true);
8d49e7ef
VM
451 *loc = x != pseudo_slots[i].mem ? x : copy_rtx (x);
452 }
49abe076 453 return res;
55a2c322
VM
454 }
455
456 fmt = GET_RTX_FORMAT (code);
457 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
458 {
459 if (fmt[i] == 'e')
49abe076 460 res = remove_pseudos (&XEXP (*loc, i), insn) || res;
55a2c322
VM
461 else if (fmt[i] == 'E')
462 {
463 int j;
464
465 for (j = XVECLEN (*loc, i) - 1; j >= 0; j--)
49abe076 466 res = remove_pseudos (&XVECEXP (*loc, i, j), insn) || res;
55a2c322
VM
467 }
468 }
49abe076 469 return res;
55a2c322
VM
470}
471
472/* Convert spilled pseudos into their stack slots or spill hard regs,
473 put insns to process on the constraint stack (that is all insns in
474 which pseudos were changed to memory or spill hard regs). */
475static void
476spill_pseudos (void)
477{
478 basic_block bb;
49abe076 479 rtx_insn *insn, *curr;
55a2c322 480 int i;
55a2c322 481
d648b5ff
TS
482 auto_bitmap spilled_pseudos (&reg_obstack);
483 auto_bitmap changed_insns (&reg_obstack);
55a2c322
VM
484 for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
485 {
486 if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
487 && ! lra_former_scratch_p (i))
488 {
d648b5ff
TS
489 bitmap_set_bit (spilled_pseudos, i);
490 bitmap_ior_into (changed_insns, &lra_reg_info[i].insn_bitmap);
55a2c322
VM
491 }
492 }
11cd3bed 493 FOR_EACH_BB_FN (bb, cfun)
55a2c322 494 {
49abe076
VM
495 FOR_BB_INSNS_SAFE (bb, insn, curr)
496 {
497 bool removed_pseudo_p = false;
498
d648b5ff 499 if (bitmap_bit_p (changed_insns, INSN_UID (insn)))
49abe076
VM
500 {
501 rtx *link_loc, link;
502
503 removed_pseudo_p = remove_pseudos (&PATTERN (insn), insn);
504 if (CALL_P (insn)
505 && remove_pseudos (&CALL_INSN_FUNCTION_USAGE (insn), insn))
506 removed_pseudo_p = true;
507 for (link_loc = &REG_NOTES (insn);
508 (link = *link_loc) != NULL_RTX;
509 link_loc = &XEXP (link, 1))
510 {
511 switch (REG_NOTE_KIND (link))
512 {
513 case REG_FRAME_RELATED_EXPR:
514 case REG_CFA_DEF_CFA:
515 case REG_CFA_ADJUST_CFA:
516 case REG_CFA_OFFSET:
517 case REG_CFA_REGISTER:
518 case REG_CFA_EXPRESSION:
519 case REG_CFA_RESTORE:
520 case REG_CFA_SET_VDRAP:
521 if (remove_pseudos (&XEXP (link, 0), insn))
522 removed_pseudo_p = true;
523 break;
524 default:
525 break;
526 }
527 }
528 if (lra_dump_file != NULL)
529 fprintf (lra_dump_file,
530 "Changing spilled pseudos to memory in insn #%u\n",
531 INSN_UID (insn));
532 lra_push_insn (insn);
533 if (lra_reg_spill_p || targetm.different_addr_displacement_p ())
7874b7c5 534 lra_set_used_insn_alternative (insn, LRA_UNKNOWN_ALT);
49abe076
VM
535 }
536 else if (CALL_P (insn)
537 /* Presence of any pseudo in CALL_INSN_FUNCTION_USAGE
538 does not affect value of insn_bitmap of the
539 corresponding lra_reg_info. That is because we
540 don't need to reload pseudos in
541 CALL_INSN_FUNCTION_USAGEs. So if we process only
542 insns in the insn_bitmap of given pseudo here, we
543 can miss the pseudo in some
544 CALL_INSN_FUNCTION_USAGEs. */
545 && remove_pseudos (&CALL_INSN_FUNCTION_USAGE (insn), insn))
546 removed_pseudo_p = true;
547 if (removed_pseudo_p)
548 {
549 lra_assert (DEBUG_INSN_P (insn));
5901e56a
JJ
550 lra_invalidate_insn_data (insn);
551 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
49abe076
VM
552 if (lra_dump_file != NULL)
553 fprintf (lra_dump_file,
5901e56a
JJ
554 "Debug insn #%u is reset because it referenced "
555 "removed pseudo\n", INSN_UID (insn));
49abe076 556 }
d648b5ff
TS
557 bitmap_and_compl_into (df_get_live_in (bb), spilled_pseudos);
558 bitmap_and_compl_into (df_get_live_out (bb), spilled_pseudos);
49abe076 559 }
55a2c322 560 }
55a2c322
VM
561}
562
23e0f4c3
BE
563/* Return true if we need scratch reg assignments. */
564bool
565lra_need_for_scratch_reg_p (void)
566{
567 int i; max_regno = max_reg_num ();
568
569 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
570 if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
571 && lra_former_scratch_p (i))
572 return true;
573 return false;
574}
575
55a2c322
VM
576/* Return true if we need to change some pseudos into memory. */
577bool
578lra_need_for_spills_p (void)
579{
580 int i; max_regno = max_reg_num ();
581
582 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
583 if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
584 && ! lra_former_scratch_p (i))
585 return true;
586 return false;
587}
588
589/* Change spilled pseudos into memory or spill hard regs. Put changed
590 insns on the constraint stack (these insns will be considered on
591 the next constraint pass). The changed insns are all insns in
592 which pseudos were changed. */
593void
594lra_spill (void)
595{
596 int i, n, curr_regno;
597 int *pseudo_regnos;
598
599 regs_num = max_reg_num ();
600 spill_hard_reg = XNEWVEC (rtx, regs_num);
601 pseudo_regnos = XNEWVEC (int, regs_num);
602 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
603 if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
604 /* We do not want to assign memory for former scratches. */
605 && ! lra_former_scratch_p (i))
49abe076 606 pseudo_regnos[n++] = i;
55a2c322
VM
607 lra_assert (n > 0);
608 pseudo_slots = XNEWVEC (struct pseudo_slot, regs_num);
49abe076
VM
609 for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
610 {
611 spill_hard_reg[i] = NULL_RTX;
612 pseudo_slots[i].mem = NULL_RTX;
613 }
99b1c316 614 slots = XNEWVEC (class slot, regs_num);
55a2c322
VM
615 /* Sort regnos according their usage frequencies. */
616 qsort (pseudo_regnos, n, sizeof (int), regno_freq_compare);
617 n = assign_spill_hard_regs (pseudo_regnos, n);
618 assign_stack_slot_num_and_sort_pseudos (pseudo_regnos, n);
619 for (i = 0; i < n; i++)
620 if (pseudo_slots[pseudo_regnos[i]].mem == NULL_RTX)
621 assign_mem_slot (pseudo_regnos[i]);
2c62cbaa
VM
622 if (n > 0 && crtl->stack_alignment_needed)
623 /* If we have a stack frame, we must align it now. The stack size
624 may be a part of the offset computation for register
625 elimination. */
626 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
55a2c322
VM
627 if (lra_dump_file != NULL)
628 {
629 for (i = 0; i < slots_num; i++)
630 {
cf098191
RS
631 fprintf (lra_dump_file, " Slot %d regnos (width = ", i);
632 print_dec (GET_MODE_SIZE (GET_MODE (slots[i].mem)),
633 lra_dump_file, SIGNED);
634 fprintf (lra_dump_file, "):");
55a2c322
VM
635 for (curr_regno = slots[i].regno;;
636 curr_regno = pseudo_slots[curr_regno].next - pseudo_slots)
637 {
638 fprintf (lra_dump_file, " %d", curr_regno);
639 if (pseudo_slots[curr_regno].next == NULL)
640 break;
641 }
642 fprintf (lra_dump_file, "\n");
643 }
644 }
645 spill_pseudos ();
646 free (slots);
647 free (pseudo_slots);
648 free (pseudo_regnos);
d0163673 649 free (spill_hard_reg);
55a2c322
VM
650}
651
6e5769ce
VM
652/* Apply alter_subreg for subregs of regs in *LOC. Use FINAL_P for
653 alter_subreg calls. Return true if any subreg of reg is
654 processed. */
655static bool
656alter_subregs (rtx *loc, bool final_p)
657{
658 int i;
659 rtx x = *loc;
660 bool res;
661 const char *fmt;
662 enum rtx_code code;
663
664 if (x == NULL_RTX)
665 return false;
666 code = GET_CODE (x);
667 if (code == SUBREG && REG_P (SUBREG_REG (x)))
668 {
669 lra_assert (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER);
670 alter_subreg (loc, final_p);
671 return true;
672 }
673 fmt = GET_RTX_FORMAT (code);
674 res = false;
675 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
676 {
677 if (fmt[i] == 'e')
678 {
679 if (alter_subregs (&XEXP (x, i), final_p))
680 res = true;
681 }
682 else if (fmt[i] == 'E')
683 {
684 int j;
f4eafc30 685
6e5769ce
VM
686 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
687 if (alter_subregs (&XVECEXP (x, i, j), final_p))
688 res = true;
689 }
690 }
691 return res;
692}
693
efaf512c
VM
694/* Return true if REGNO is used for return in the current
695 function. */
696static bool
697return_regno_p (unsigned int regno)
698{
699 rtx outgoing = crtl->return_rtx;
700
701 if (! outgoing)
702 return false;
703
704 if (REG_P (outgoing))
705 return REGNO (outgoing) == regno;
706 else if (GET_CODE (outgoing) == PARALLEL)
707 {
708 int i;
709
710 for (i = 0; i < XVECLEN (outgoing, 0); i++)
711 {
712 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
713
714 if (REG_P (x) && REGNO (x) == regno)
715 return true;
716 }
717 }
718 return false;
719}
720
73c77563
VM
721/* Return true if REGNO is in one of subsequent USE after INSN in the
722 same BB. */
00803109
VM
723static bool
724regno_in_use_p (rtx_insn *insn, unsigned int regno)
725{
73c77563
VM
726 static lra_insn_recog_data_t id;
727 static struct lra_static_insn_data *static_id;
728 struct lra_insn_reg *reg;
729 int i, arg_regno;
730 basic_block bb = BLOCK_FOR_INSN (insn);
731
5435398d 732 while ((insn = next_nondebug_insn (insn)) != NULL_RTX)
00803109 733 {
5435398d
JJ
734 if (BARRIER_P (insn) || bb != BLOCK_FOR_INSN (insn))
735 return false;
73c77563
VM
736 if (! INSN_P (insn))
737 continue;
738 if (GET_CODE (PATTERN (insn)) == USE
739 && REG_P (XEXP (PATTERN (insn), 0))
00803109 740 && regno == REGNO (XEXP (PATTERN (insn), 0)))
73c77563
VM
741 return true;
742 /* Check that the regno is not modified. */
743 id = lra_get_insn_recog_data (insn);
744 for (reg = id->regs; reg != NULL; reg = reg->next)
745 if (reg->type != OP_IN && reg->regno == (int) regno)
746 return false;
747 static_id = id->insn_static_data;
748 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
749 if (reg->type != OP_IN && reg->regno == (int) regno)
750 return false;
751 if (id->arg_hard_regs != NULL)
752 for (i = 0; (arg_regno = id->arg_hard_regs[i]) >= 0; i++)
753 if ((int) regno == (arg_regno >= FIRST_PSEUDO_REGISTER
754 ? arg_regno : arg_regno - FIRST_PSEUDO_REGISTER))
755 return false;
00803109
VM
756 }
757 return false;
758}
759
55a2c322 760/* Final change of pseudos got hard registers into the corresponding
c5cd5a7e 761 hard registers and removing temporary clobbers. */
55a2c322 762void
c5cd5a7e 763lra_final_code_change (void)
55a2c322
VM
764{
765 int i, hard_regno;
766 basic_block bb;
00803109 767 rtx_insn *insn, *curr;
1008df90 768 rtx set;
55a2c322
VM
769 int max_regno = max_reg_num ();
770
771 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
772 if (lra_reg_info[i].nrefs != 0
773 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
774 SET_REGNO (regno_reg_rtx[i], hard_regno);
11cd3bed 775 FOR_EACH_BB_FN (bb, cfun)
c5cd5a7e 776 FOR_BB_INSNS_SAFE (bb, insn, curr)
55a2c322
VM
777 if (INSN_P (insn))
778 {
c5cd5a7e
VM
779 rtx pat = PATTERN (insn);
780
781 if (GET_CODE (pat) == CLOBBER && LRA_TEMP_CLOBBER_P (pat))
782 {
783 /* Remove clobbers temporarily created in LRA. We don't
784 need them anymore and don't want to waste compiler
785 time processing them in a few subsequent passes. */
786 lra_invalidate_insn_data (insn);
1f397f45 787 delete_insn (insn);
c5cd5a7e
VM
788 continue;
789 }
790
efaf512c
VM
791 /* IRA can generate move insns involving pseudos. It is
792 better remove them earlier to speed up compiler a bit.
793 It is also better to do it here as they might not pass
794 final RTL check in LRA, (e.g. insn moving a control
795 register into itself). So remove an useless move insn
796 unless next insn is USE marking the return reg (we should
797 save this as some subsequent optimizations assume that
798 such original insns are saved). */
799 if (NONJUMP_INSN_P (insn) && GET_CODE (pat) == SET
800 && REG_P (SET_SRC (pat)) && REG_P (SET_DEST (pat))
801 && REGNO (SET_SRC (pat)) == REGNO (SET_DEST (pat))
8a8330b7 802 && (! return_regno_p (REGNO (SET_SRC (pat)))
00803109 803 || ! regno_in_use_p (insn, REGNO (SET_SRC (pat)))))
efaf512c
VM
804 {
805 lra_invalidate_insn_data (insn);
806 delete_insn (insn);
807 continue;
808 }
809
6e5769ce 810 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
684ffdc9
VM
811 struct lra_insn_reg *reg;
812
813 for (reg = id->regs; reg != NULL; reg = reg->next)
814 if (reg->regno >= FIRST_PSEUDO_REGISTER
815 && lra_reg_info [reg->regno].nrefs == 0)
816 break;
817
818 if (reg != NULL)
819 {
820 /* Pseudos still can be in debug insns in some very rare
821 and complicated cases, e.g. the pseudo was removed by
822 inheritance and the debug insn is not EBBs where the
823 inheritance happened. It is difficult and time
824 consuming to find what hard register corresponds the
825 pseudo -- so just remove the debug insn. Another
826 solution could be assigning hard reg/memory but it
827 would be a misleading info. It is better not to have
828 info than have it wrong. */
829 lra_assert (DEBUG_INSN_P (insn));
830 lra_invalidate_insn_data (insn);
831 delete_insn (insn);
832 continue;
833 }
834
2c62cbaa 835 struct lra_static_insn_data *static_id = id->insn_static_data;
55a2c322 836 bool insn_change_p = false;
6bbacdb5
L
837
838 for (i = id->insn_static_data->n_operands - 1; i >= 0; i--)
839 if ((DEBUG_INSN_P (insn) || ! static_id->operand[i].is_operator)
840 && alter_subregs (id->operand_loc[i], ! DEBUG_INSN_P (insn)))
841 {
842 lra_update_dup (id, i);
843 insn_change_p = true;
844 }
55a2c322
VM
845 if (insn_change_p)
846 lra_update_operator_dups (id);
1008df90
JJ
847
848 if ((set = single_set (insn)) != NULL
849 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))
850 && REGNO (SET_SRC (set)) == REGNO (SET_DEST (set)))
851 {
852 /* Remove an useless move insn. IRA can generate move
853 insns involving pseudos. It is better remove them
854 earlier to speed up compiler a bit. It is also
855 better to do it here as they might not pass final RTL
856 check in LRA, (e.g. insn moving a control register
857 into itself). */
858 lra_invalidate_insn_data (insn);
859 delete_insn (insn);
860 }
55a2c322
VM
861 }
862}