Line data Source code
1 : /* Change pseudos by memory.
2 : Copyright (C) 2010-2026 Free Software Foundation, Inc.
3 : Contributed by Vladimir Makarov <vmakarov@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
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.
11 :
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.
16 :
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/>. */
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
36 :
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"
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"
74 :
75 :
76 : /* Max regno at the start of the pass. */
77 : static int regs_num;
78 :
79 : /* Map spilled regno -> hard regno used instead of memory for
80 : spilling. */
81 : static rtx *spill_hard_reg;
82 :
83 : /* The structure describes stack slot of a spilled pseudo. */
84 : struct 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. */
96 : static 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. */
100 : class slot
101 : {
102 : public:
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;
108 : /* Maximum alignment required by all users of the slot. */
109 : unsigned int align;
110 : /* Maximum size required by all users of the slot. */
111 : poly_int64 size;
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). */
125 : static class slot *slots;
126 : /* The number of the stack slots currently existing. */
127 : static 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. */
131 : static void
132 1464911 : assign_mem_slot (int i)
133 : {
134 1464911 : rtx x = NULL_RTX;
135 1464911 : machine_mode mode = GET_MODE (regno_reg_rtx[i]);
136 2929822 : poly_int64 inherent_size = PSEUDO_REGNO_BYTES (i);
137 1464911 : machine_mode wider_mode
138 1464911 : = wider_subreg_mode (mode, lra_reg_info[i].biggest_mode);
139 2929822 : poly_int64 total_size = GET_MODE_SIZE (wider_mode);
140 1464911 : poly_int64 adjust = 0;
141 :
142 1464911 : 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);
144 :
145 1464911 : unsigned int slot_num = pseudo_slots[i].slot_num;
146 1464911 : x = slots[slot_num].mem;
147 1464911 : if (!x)
148 : {
149 1565394 : x = assign_stack_local_1 (BLKmode, slots[slot_num].size,
150 782697 : slots[slot_num].align,
151 : ASLK_RECORD_PAD | ASLK_REDUCE_ALIGN);
152 782697 : slots[slot_num].mem = x;
153 : }
154 :
155 : /* On a big endian machine, the "address" of the slot is the address
156 : of the low part that fits its inherent mode. */
157 2929822 : adjust += subreg_size_lowpart_offset (inherent_size, total_size);
158 1464911 : x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
159 :
160 : /* Set all of the memory attributes as appropriate for a spill. */
161 1464911 : set_mem_attrs_for_spill (x);
162 1464911 : pseudo_slots[i].mem = x;
163 1464911 : }
164 :
165 : /* Sort pseudos according their usage frequencies. */
166 : static int
167 31661826 : regno_freq_compare (const void *v1p, const void *v2p)
168 : {
169 31661826 : const int regno1 = *(const int *) v1p;
170 31661826 : const int regno2 = *(const int *) v2p;
171 31661826 : int diff;
172 :
173 31661826 : if ((diff = lra_reg_info[regno2].freq - lra_reg_info[regno1].freq) != 0)
174 : return diff;
175 12888447 : return regno1 - regno2;
176 : }
177 :
178 : /* Sort pseudos according to their slots, putting the slots in the order
179 : that they should be allocated.
180 :
181 : First prefer to group slots with variable sizes together and slots
182 : with constant sizes together, since that usually makes them easier
183 : to address from a common anchor point. E.g. loads of polynomial-sized
184 : registers tend to take polynomial offsets while loads of constant-sized
185 : registers tend to take constant (non-polynomial) offsets.
186 :
187 : Next, slots with lower numbers have the highest priority and should
188 : get the smallest displacement from the stack or frame pointer
189 : (whichever is being used).
190 :
191 : The first allocated slot is always closest to the frame pointer,
192 : so prefer lower slot numbers when frame_pointer_needed. If the stack
193 : and frame grow in the same direction, then the first allocated slot is
194 : always closest to the initial stack pointer and furthest away from the
195 : final stack pointer, so allocate higher numbers first when using the
196 : stack pointer in that case. The reverse is true if the stack and
197 : frame grow in opposite directions. */
198 : static int
199 32177941 : pseudo_reg_slot_compare (const void *v1p, const void *v2p)
200 : {
201 32177941 : const int regno1 = *(const int *) v1p;
202 32177941 : const int regno2 = *(const int *) v2p;
203 32177941 : int diff, slot_num1, slot_num2;
204 :
205 32177941 : slot_num1 = pseudo_slots[regno1].slot_num;
206 32177941 : slot_num2 = pseudo_slots[regno2].slot_num;
207 32177941 : diff = (int (slots[slot_num1].size.is_constant ())
208 32177941 : - int (slots[slot_num2].size.is_constant ()));
209 32177941 : if (diff != 0)
210 : return diff;
211 32177941 : if ((diff = slot_num1 - slot_num2) != 0)
212 23908071 : return (frame_pointer_needed
213 23908071 : || (!FRAME_GROWS_DOWNWARD) == STACK_GROWS_DOWNWARD ? diff : -diff);
214 16539740 : poly_int64 total_size1 = GET_MODE_SIZE (lra_reg_info[regno1].biggest_mode);
215 16539740 : poly_int64 total_size2 = GET_MODE_SIZE (lra_reg_info[regno2].biggest_mode);
216 9100592 : if ((diff = compare_sizes_for_sort (total_size2, total_size1)) != 0)
217 : return diff;
218 6728579 : return regno1 - regno2;
219 : }
220 :
221 : /* Assign spill hard registers to N pseudos in PSEUDO_REGNOS which is
222 : sorted in order of highest frequency first. Put the pseudos which
223 : did not get a spill hard register at the beginning of array
224 : PSEUDO_REGNOS. Return the number of such pseudos. */
225 : static int
226 201201 : assign_spill_hard_regs (int *pseudo_regnos, int n)
227 : {
228 201201 : int i, k, p, regno, res, spill_class_size, hard_regno, nr;
229 201201 : enum reg_class rclass, spill_class;
230 201201 : machine_mode mode;
231 201201 : lra_live_range_t r;
232 201201 : rtx_insn *insn;
233 201201 : rtx set;
234 201201 : basic_block bb;
235 201201 : HARD_REG_SET conflict_hard_regs;
236 201201 : bitmap setjump_crosses = regstat_get_setjmp_crosses ();
237 : /* Hard registers which cannot be used for any purpose at given
238 : program point because they are unallocatable or already allocated
239 : for other pseudos. */
240 201201 : HARD_REG_SET *reserved_hard_regs;
241 :
242 201201 : if (! lra_reg_spill_p)
243 : return n;
244 : /* Set up reserved hard regs for every program point. */
245 0 : reserved_hard_regs = XNEWVEC (HARD_REG_SET, lra_live_max_point);
246 0 : for (p = 0; p < lra_live_max_point; p++)
247 0 : reserved_hard_regs[p] = lra_no_alloc_regs;
248 0 : for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
249 0 : if (lra_reg_info[i].nrefs != 0
250 0 : && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
251 0 : for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
252 0 : for (p = r->start; p <= r->finish; p++)
253 0 : add_to_hard_reg_set (&reserved_hard_regs[p],
254 : lra_reg_info[i].biggest_mode, hard_regno);
255 0 : auto_bitmap ok_insn_bitmap (®_obstack);
256 0 : FOR_EACH_BB_FN (bb, cfun)
257 0 : FOR_BB_INSNS (bb, insn)
258 0 : if (DEBUG_INSN_P (insn)
259 0 : || ((set = single_set (insn)) != NULL_RTX
260 0 : && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))))
261 0 : bitmap_set_bit (ok_insn_bitmap, INSN_UID (insn));
262 0 : for (res = i = 0; i < n; i++)
263 : {
264 0 : regno = pseudo_regnos[i];
265 0 : gcc_assert (lra_reg_info[regno].live_ranges);
266 0 : rclass = lra_get_allocno_class (regno);
267 0 : if (bitmap_bit_p (setjump_crosses, regno)
268 0 : || (spill_class
269 0 : = ((enum reg_class)
270 0 : targetm.spill_class ((reg_class_t) rclass,
271 0 : PSEUDO_REGNO_MODE (regno)))) == NO_REGS
272 0 : || bitmap_intersect_compl_p (&lra_reg_info[regno].insn_bitmap,
273 0 : ok_insn_bitmap))
274 : {
275 0 : pseudo_regnos[res++] = regno;
276 0 : continue;
277 : }
278 0 : lra_assert (spill_class != NO_REGS);
279 0 : conflict_hard_regs = lra_reg_info[regno].conflict_hard_regs;
280 0 : for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
281 0 : for (p = r->start; p <= r->finish; p++)
282 0 : conflict_hard_regs |= reserved_hard_regs[p];
283 0 : spill_class_size = ira_class_hard_regs_num[spill_class];
284 0 : mode = lra_reg_info[regno].biggest_mode;
285 0 : for (k = 0; k < spill_class_size; k++)
286 : {
287 0 : hard_regno = ira_class_hard_regs[spill_class][k];
288 0 : if (TEST_HARD_REG_BIT (eliminable_regset, hard_regno)
289 0 : || !targetm.hard_regno_mode_ok (hard_regno, mode))
290 0 : continue;
291 0 : if (! overlaps_hard_reg_set_p (conflict_hard_regs, mode, hard_regno))
292 : break;
293 : }
294 0 : if (k >= spill_class_size)
295 : {
296 : /* There is no available regs -- assign memory later. */
297 0 : pseudo_regnos[res++] = regno;
298 0 : continue;
299 : }
300 0 : if (lra_dump_file != NULL)
301 0 : fprintf (lra_dump_file, " Spill r%d into hr%d\n", regno, hard_regno);
302 0 : add_to_hard_reg_set (&hard_regs_spilled_into,
303 0 : lra_reg_info[regno].biggest_mode, hard_regno);
304 : /* Update reserved_hard_regs. */
305 0 : for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
306 0 : for (p = r->start; p <= r->finish; p++)
307 0 : add_to_hard_reg_set (&reserved_hard_regs[p],
308 : lra_reg_info[regno].biggest_mode, hard_regno);
309 0 : spill_hard_reg[regno]
310 0 : = gen_raw_REG (PSEUDO_REGNO_MODE (regno), hard_regno);
311 0 : for (nr = 0;
312 0 : nr < hard_regno_nregs (hard_regno,
313 0 : lra_reg_info[regno].biggest_mode);
314 : nr++)
315 : /* Just loop. */
316 0 : df_set_regs_ever_live (hard_regno + nr, true);
317 : }
318 0 : free (reserved_hard_regs);
319 0 : return res;
320 0 : }
321 :
322 : /* Add pseudo REGNO to slot SLOT_NUM. */
323 : static void
324 1464911 : add_pseudo_to_slot (int regno, int slot_num)
325 : {
326 1464911 : struct pseudo_slot *first;
327 :
328 : /* Each pseudo has an inherent size which comes from its own mode,
329 : and a total size which provides room for paradoxical subregs.
330 : We need to make sure the size and alignment of the slot are
331 : sufficient for both. */
332 2929822 : machine_mode mode = wider_subreg_mode (PSEUDO_REGNO_MODE (regno),
333 1464911 : lra_reg_info[regno].biggest_mode);
334 1464911 : unsigned int align = spill_slot_alignment (mode);
335 1464911 : slots[slot_num].align = MAX (slots[slot_num].align, align);
336 1464911 : slots[slot_num].size = upper_bound (slots[slot_num].size,
337 1464911 : GET_MODE_SIZE (mode));
338 :
339 1464911 : if (slots[slot_num].regno < 0)
340 : {
341 : /* It is the first pseudo in the slot. */
342 782697 : slots[slot_num].regno = regno;
343 782697 : pseudo_slots[regno].first = &pseudo_slots[regno];
344 782697 : pseudo_slots[regno].next = NULL;
345 : }
346 : else
347 : {
348 682214 : first = pseudo_slots[regno].first = &pseudo_slots[slots[slot_num].regno];
349 682214 : pseudo_slots[regno].next = first->next;
350 682214 : first->next = &pseudo_slots[regno];
351 682214 : lra_assert (slots[slot_num].live_ranges);
352 : }
353 1464911 : pseudo_slots[regno].mem = NULL_RTX;
354 1464911 : pseudo_slots[regno].slot_num = slot_num;
355 :
356 : /* Pseudos with empty ranges shouldn't need to be spilled; if we get
357 : an empty range added to a slot, something fishy is going on, such
358 : as missing live range information, and without that information
359 : we may generate wrong code. We can probably relax this to an
360 : lra_assert at some point. Likewise the analogous one in
361 : assign_spill_hard_regs. */
362 1464911 : gcc_assert (lra_reg_info[regno].live_ranges);
363 :
364 1464911 : slots[slot_num].live_ranges
365 1464911 : = lra_merge_live_ranges (slots[slot_num].live_ranges,
366 : lra_copy_live_range_list
367 : (lra_reg_info[regno].live_ranges));
368 1464911 : }
369 :
370 : /* Recompute the combined live ranges of pseudos assigned to stack
371 : slots. This brings the live ranges of slots back in sync with
372 : those of pseudos, after recomputing live ranges for pseudos. */
373 : void
374 0 : lra_recompute_slots_live_ranges (void)
375 : {
376 0 : for (int i = 0; i < slots_num; i++)
377 : {
378 0 : if (slots[i].regno < 0)
379 0 : continue;
380 0 : lra_reset_live_range_list (slots[i].live_ranges);
381 0 : for (struct pseudo_slot *next = pseudo_slots[slots[i].regno].first;
382 0 : next; next = next->next)
383 : {
384 0 : int regno = next - pseudo_slots;
385 0 : lra_assert (!(lra_intersected_live_ranges_p
386 : (lra_reg_info[regno].live_ranges,
387 : slots[i].live_ranges)));
388 0 : slots[i].live_ranges
389 0 : = lra_merge_live_ranges (slots[i].live_ranges,
390 : lra_copy_live_range_list
391 0 : (lra_reg_info[regno].live_ranges));
392 : }
393 : }
394 0 : }
395 :
396 : /* Assign stack slot numbers to pseudos in array PSEUDO_REGNOS of
397 : length N. Sort pseudos in PSEUDO_REGNOS for subsequent assigning
398 : memory stack slots. */
399 : static void
400 201201 : assign_stack_slot_num_and_sort_pseudos (int *pseudo_regnos, int n)
401 : {
402 201201 : int i, j, regno;
403 :
404 : /* Assign stack slot numbers to spilled pseudos, use smaller numbers
405 : for most frequently used pseudos. */
406 1666112 : for (i = 0; i < n; i++)
407 : {
408 1464911 : regno = pseudo_regnos[i];
409 1464911 : if (! flag_ira_share_spill_slots)
410 27795 : j = slots_num;
411 : else
412 : {
413 1437116 : machine_mode mode
414 2874232 : = wider_subreg_mode (PSEUDO_REGNO_MODE (regno),
415 1437116 : lra_reg_info[regno].biggest_mode);
416 27871363 : for (j = 0; j < slots_num; j++)
417 27116461 : if (slots[j].hard_regno < 0
418 : /* Although it's possible to share slots between modes
419 : with constant and non-constant widths, we usually
420 : get better spill code by keeping the constant and
421 : non-constant areas separate. */
422 27116461 : && (GET_MODE_SIZE (mode).is_constant ()
423 : == slots[j].size.is_constant ())
424 54232922 : && ! (lra_intersected_live_ranges_p
425 27116461 : (slots[j].live_ranges,
426 27116461 : lra_reg_info[regno].live_ranges)))
427 : {
428 : /* A slot without allocated memory can be shared. */
429 682214 : if (slots[j].mem == NULL_RTX)
430 : break;
431 :
432 : /* A slot with allocated memory can be shared only with equal
433 : or smaller register with equal or smaller alignment. */
434 0 : if (slots[j].align >= spill_slot_alignment (mode)
435 0 : && known_ge (slots[j].size, GET_MODE_SIZE (mode)))
436 : break;
437 : }
438 : }
439 1464911 : if (j >= slots_num)
440 : {
441 : /* New slot. */
442 782697 : slots[j].live_ranges = NULL;
443 782697 : slots[j].size = 0;
444 782697 : slots[j].align = BITS_PER_UNIT;
445 782697 : slots[j].regno = slots[j].hard_regno = -1;
446 782697 : slots[j].mem = NULL_RTX;
447 782697 : slots_num++;
448 : }
449 1464911 : add_pseudo_to_slot (regno, j);
450 : }
451 : /* Sort regnos according to their slot numbers. */
452 201201 : qsort (pseudo_regnos, n, sizeof (int), pseudo_reg_slot_compare);
453 201201 : }
454 :
455 : /* Recursively process LOC in INSN and change spilled pseudos to the
456 : corresponding memory or spilled hard reg. Ignore spilled pseudos
457 : created from the scratches. Return true if the pseudo nrefs equal
458 : to 0 (don't change the pseudo in this case). Otherwise return false. */
459 : static bool
460 37652193 : remove_pseudos (rtx *loc, rtx_insn *insn)
461 : {
462 37652193 : int i;
463 37652193 : rtx hard_reg;
464 37652193 : const char *fmt;
465 37652193 : enum rtx_code code;
466 37652193 : bool res = false;
467 :
468 37652193 : if (*loc == NULL_RTX)
469 : return res;
470 34731143 : code = GET_CODE (*loc);
471 34731143 : if (code == SUBREG && REG_P (SUBREG_REG (*loc)))
472 : {
473 : /* Try to remove memory subregs to simplify LRA job
474 : and avoid LRA cycling in case of subreg memory reload. */
475 287017 : res = remove_pseudos (&SUBREG_REG (*loc), insn);
476 287017 : if (GET_CODE (SUBREG_REG (*loc)) == MEM)
477 : {
478 224402 : alter_subreg (loc, false);
479 224402 : if (GET_CODE (*loc) == MEM)
480 : {
481 224402 : lra_update_insn_recog_data (insn);
482 224402 : if (lra_dump_file != NULL)
483 1 : fprintf (lra_dump_file,
484 : "Memory subreg was simplified in insn #%u\n",
485 1 : INSN_UID (insn));
486 : }
487 : }
488 : return res;
489 : }
490 15483190 : else if (code == REG && (i = REGNO (*loc)) >= FIRST_PSEUDO_REGISTER
491 8971742 : && lra_get_regno_hard_regno (i) < 0
492 : /* We do not want to assign memory for former scratches because
493 : it might result in an address reload for some targets. In
494 : any case we transform such pseudos not getting hard registers
495 : into scratches back. */
496 39537673 : && ! ira_former_scratch_p (i))
497 : {
498 5093547 : if (lra_reg_info[i].nrefs == 0
499 21985 : && pseudo_slots[i].mem == NULL && spill_hard_reg[i] == NULL)
500 : return true;
501 5093547 : if ((hard_reg = spill_hard_reg[i]) != NULL_RTX)
502 0 : *loc = copy_rtx (hard_reg);
503 5093547 : else if (pseudo_slots[i].mem != NULL_RTX)
504 : /* There might be no memory slot or hard reg for a pseudo when we spill
505 : the frame pointer after elimination of frame pointer to stack
506 : pointer became impossible. */
507 : {
508 10187094 : rtx x = lra_eliminate_regs_1 (insn, pseudo_slots[i].mem,
509 5093547 : GET_MODE (pseudo_slots[i].mem),
510 : false, false, 0, true);
511 5093547 : *loc = x != pseudo_slots[i].mem ? x : copy_rtx (x);
512 : }
513 : return res;
514 : }
515 :
516 29350579 : fmt = GET_RTX_FORMAT (code);
517 71020959 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
518 : {
519 41670380 : if (fmt[i] == 'e')
520 29195428 : res = remove_pseudos (&XEXP (*loc, i), insn) || res;
521 12474952 : else if (fmt[i] == 'E')
522 : {
523 104368 : int j;
524 :
525 329334 : for (j = XVECLEN (*loc, i) - 1; j >= 0; j--)
526 224966 : res = remove_pseudos (&XVECEXP (*loc, i, j), insn) || res;
527 : }
528 : }
529 : return res;
530 : }
531 :
532 : /* Convert spilled pseudos into their stack slots or spill hard regs,
533 : put insns to process on the constraint stack (that is all insns in
534 : which pseudos were changed to memory or spill hard regs). */
535 : static void
536 201201 : spill_pseudos (void)
537 : {
538 201201 : basic_block bb;
539 201201 : rtx_insn *insn, *curr;
540 201201 : int i;
541 :
542 201201 : auto_bitmap spilled_pseudos (®_obstack);
543 201201 : auto_bitmap changed_insns (®_obstack);
544 37062519 : for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
545 : {
546 18916943 : if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
547 38328821 : && ! ira_former_scratch_p (i))
548 : {
549 1464911 : bitmap_set_bit (spilled_pseudos, i);
550 1464911 : bitmap_ior_into (changed_insns, &lra_reg_info[i].insn_bitmap);
551 : }
552 : }
553 7351283 : FOR_EACH_BB_FN (bb, cfun)
554 : {
555 213480966 : FOR_BB_INSNS_SAFE (bb, insn, curr)
556 : {
557 103165442 : bool removed_pseudo_p = false;
558 :
559 103165442 : rtx_call_insn *call;
560 103165442 : if (bitmap_bit_p (changed_insns, INSN_UID (insn)))
561 : {
562 5023732 : rtx *link_loc, link;
563 :
564 5023732 : removed_pseudo_p = remove_pseudos (&PATTERN (insn), insn);
565 5023732 : auto call = dyn_cast<rtx_call_insn *> (insn);
566 0 : if (call
567 0 : && remove_pseudos (&CALL_INSN_FUNCTION_USAGE (call), call))
568 : removed_pseudo_p = true;
569 5023732 : for (link_loc = ®_NOTES (insn);
570 8444329 : (link = *link_loc) != NULL_RTX;
571 3420597 : link_loc = &XEXP (link, 1))
572 : {
573 3420597 : switch (REG_NOTE_KIND (link))
574 : {
575 0 : case REG_FRAME_RELATED_EXPR:
576 0 : case REG_CFA_DEF_CFA:
577 0 : case REG_CFA_ADJUST_CFA:
578 0 : case REG_CFA_OFFSET:
579 0 : case REG_CFA_REGISTER:
580 0 : case REG_CFA_EXPRESSION:
581 0 : case REG_CFA_RESTORE:
582 0 : case REG_CFA_SET_VDRAP:
583 0 : if (remove_pseudos (&XEXP (link, 0), insn))
584 3420597 : removed_pseudo_p = true;
585 : break;
586 : default:
587 : break;
588 : }
589 : }
590 5023732 : if (GET_CODE (PATTERN (insn)) == CLOBBER)
591 : /* This is a CLOBBER insn with pseudo spilled to memory.
592 : Mark it for removing it later together with LRA temporary
593 : CLOBBER insns. */
594 9055 : LRA_TEMP_CLOBBER_P (PATTERN (insn)) = 1;
595 5023732 : if (lra_dump_file != NULL)
596 2 : fprintf (lra_dump_file,
597 : "Changing spilled pseudos to memory in insn #%u\n",
598 2 : INSN_UID (insn));
599 5023732 : lra_push_insn_and_update_insn_regno_info (insn);
600 5023732 : if (lra_reg_spill_p || targetm.different_addr_displacement_p ())
601 0 : lra_set_used_insn_alternative (insn, LRA_UNKNOWN_ALT);
602 : }
603 98141710 : else if ((call = dyn_cast<rtx_call_insn *> (insn))
604 : /* Presence of any pseudo in CALL_INSN_FUNCTION_USAGE
605 : does not affect value of insn_bitmap of the
606 : corresponding lra_reg_info. That is because we
607 : don't need to reload pseudos in
608 : CALL_INSN_FUNCTION_USAGEs. So if we process only
609 : insns in the insn_bitmap of given pseudo here, we
610 : can miss the pseudo in some
611 : CALL_INSN_FUNCTION_USAGEs. */
612 2921050 : && remove_pseudos (&CALL_INSN_FUNCTION_USAGE (call), call))
613 : removed_pseudo_p = true;
614 5023732 : if (removed_pseudo_p)
615 : {
616 0 : lra_assert (DEBUG_INSN_P (insn));
617 0 : lra_invalidate_insn_data (insn);
618 0 : INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
619 0 : if (lra_dump_file != NULL)
620 0 : fprintf (lra_dump_file,
621 : "Debug insn #%u is reset because it referenced "
622 0 : "removed pseudo\n", INSN_UID (insn));
623 : }
624 103165442 : bitmap_and_compl_into (df_get_live_in (bb), spilled_pseudos);
625 103165442 : bitmap_and_compl_into (df_get_live_out (bb), spilled_pseudos);
626 : }
627 : }
628 201201 : }
629 :
630 : /* Return true if we need scratch reg assignments. */
631 : bool
632 302 : lra_need_for_scratch_reg_p (void)
633 : {
634 302 : int i; max_regno = max_reg_num ();
635 :
636 14888 : for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
637 4846 : if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
638 14586 : && ira_former_scratch_p (i))
639 : return true;
640 : return false;
641 : }
642 :
643 : /* Return true if we need to change some pseudos into memory. */
644 : bool
645 1734000 : lra_need_for_spills_p (void)
646 : {
647 1734000 : int i;
648 :
649 1734000 : max_regno = max_reg_num ();
650 93220851 : for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
651 41383959 : if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
652 91897456 : && ! ira_former_scratch_p (i))
653 : return true;
654 : return false;
655 : }
656 :
657 : /* Change spilled pseudos into memory or spill hard regs. Put changed
658 : insns on the constraint stack (these insns will be considered on
659 : the next constraint pass). The changed insns are all insns in
660 : which pseudos were changed. */
661 : void
662 201201 : lra_spill (void)
663 : {
664 201201 : int i, n, n2, curr_regno;
665 201201 : int *pseudo_regnos;
666 :
667 201201 : regs_num = max_reg_num ();
668 201201 : spill_hard_reg = XNEWVEC (rtx, regs_num);
669 201201 : pseudo_regnos = XNEWVEC (int, regs_num);
670 37263720 : for (n = 0, i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
671 18916943 : if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
672 : /* We do not want to assign memory for former scratches. */
673 38328821 : && ! ira_former_scratch_p (i))
674 1464911 : pseudo_regnos[n++] = i;
675 201201 : lra_assert (n > 0);
676 201201 : pseudo_slots = XNEWVEC (struct pseudo_slot, regs_num);
677 37062519 : for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
678 : {
679 36861318 : spill_hard_reg[i] = NULL_RTX;
680 36861318 : pseudo_slots[i].mem = NULL_RTX;
681 : }
682 201201 : slots = XNEWVEC (class slot, regs_num);
683 : /* Sort regnos according their usage frequencies. */
684 201201 : qsort (pseudo_regnos, n, sizeof (int), regno_freq_compare);
685 201201 : n = assign_spill_hard_regs (pseudo_regnos, n);
686 201201 : slots_num = 0;
687 201201 : assign_stack_slot_num_and_sort_pseudos (pseudo_regnos, n);
688 1867313 : for (i = 0; i < n; i++)
689 1464911 : if (pseudo_slots[pseudo_regnos[i]].mem == NULL_RTX)
690 1464911 : assign_mem_slot (pseudo_regnos[i]);
691 201201 : if ((n2 = lra_update_fp2sp_elimination (pseudo_regnos)) > 0)
692 : {
693 : /* Assign stack slots to spilled pseudos assigned to fp. */
694 0 : assign_stack_slot_num_and_sort_pseudos (pseudo_regnos, n2);
695 0 : for (i = 0; i < n2; i++)
696 0 : if (pseudo_slots[pseudo_regnos[i]].mem == NULL_RTX)
697 0 : assign_mem_slot (pseudo_regnos[i]);
698 : }
699 201201 : if (n + n2 > 0 && crtl->stack_alignment_needed)
700 : /* If we have a stack frame, we must align it now. The stack size
701 : may be a part of the offset computation for register
702 : elimination. */
703 201201 : assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
704 201201 : if (lra_dump_file != NULL)
705 : {
706 2 : for (i = 0; i < slots_num; i++)
707 : {
708 1 : fprintf (lra_dump_file, " Slot %d regnos (width = ", i);
709 1 : print_dec (slots[i].size, lra_dump_file, SIGNED);
710 1 : fprintf (lra_dump_file, "):");
711 1 : for (curr_regno = slots[i].regno;;
712 0 : curr_regno = pseudo_slots[curr_regno].next - pseudo_slots)
713 : {
714 1 : fprintf (lra_dump_file, " %d", curr_regno);
715 1 : if (pseudo_slots[curr_regno].next == NULL)
716 : break;
717 : }
718 1 : fprintf (lra_dump_file, "\n");
719 : }
720 : }
721 201201 : spill_pseudos ();
722 201201 : free (slots);
723 201201 : free (pseudo_slots);
724 201201 : free (pseudo_regnos);
725 201201 : free (spill_hard_reg);
726 201201 : }
727 :
728 : /* Apply alter_subreg for subregs of regs in *LOC. Use FINAL_P for
729 : alter_subreg calls. Return true if any subreg of reg is
730 : processed. */
731 : static bool
732 366647452 : alter_subregs (rtx *loc, bool final_p)
733 : {
734 366647452 : int i;
735 366647452 : rtx x = *loc;
736 366647452 : bool res;
737 366647452 : const char *fmt;
738 366647452 : enum rtx_code code;
739 :
740 366647452 : if (x == NULL_RTX)
741 : return false;
742 366647452 : code = GET_CODE (x);
743 366647452 : if (code == SUBREG && REG_P (SUBREG_REG (x)))
744 : {
745 3133169 : lra_assert (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER);
746 3133169 : alter_subreg (loc, final_p);
747 3133169 : return true;
748 : }
749 363514283 : fmt = GET_RTX_FORMAT (code);
750 363514283 : res = false;
751 867876994 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
752 : {
753 504362711 : if (fmt[i] == 'e')
754 : {
755 125380814 : if (alter_subregs (&XEXP (x, i), final_p))
756 504362711 : res = true;
757 : }
758 378981897 : else if (fmt[i] == 'E')
759 : {
760 573067 : int j;
761 :
762 2904602 : for (j = XVECLEN (x, i) - 1; j >= 0; j--)
763 2331535 : if (alter_subregs (&XVECEXP (x, i, j), final_p))
764 812 : res = true;
765 : }
766 : }
767 : return res;
768 : }
769 :
770 : /* Final change of pseudos got hard registers into the corresponding
771 : hard registers and removing temporary clobbers. */
772 : void
773 1531123 : lra_final_code_change (void)
774 : {
775 1531123 : int i, hard_regno;
776 1531123 : basic_block bb;
777 1531123 : rtx_insn *insn, *curr;
778 1531123 : rtx set;
779 1531123 : int max_regno = max_reg_num ();
780 :
781 81994766 : for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
782 78932520 : if (lra_reg_info[i].nrefs != 0
783 115314852 : && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
784 36377705 : SET_REGNO (regno_reg_rtx[i], hard_regno);
785 16461561 : FOR_EACH_BB_FN (bb, cfun)
786 371686950 : FOR_BB_INSNS_SAFE (bb, insn, curr)
787 178378256 : if (INSN_P (insn))
788 : {
789 148996894 : rtx pat = PATTERN (insn);
790 :
791 148996894 : if (GET_CODE (pat) == USE && XEXP (pat, 0) == const1_rtx)
792 : {
793 : /* Remove markers to eliminate critical edges for jump insn
794 : output reloads (see code in ira.cc::ira). */
795 11 : lra_invalidate_insn_data (insn);
796 11 : delete_insn (insn);
797 11 : continue;
798 : }
799 148996883 : if (GET_CODE (pat) == CLOBBER && LRA_TEMP_CLOBBER_P (pat))
800 : {
801 : /* Remove clobbers temporarily created in LRA. We don't
802 : need them anymore and don't want to waste compiler
803 : time processing them in a few subsequent passes. */
804 120698 : lra_invalidate_insn_data (insn);
805 120698 : delete_insn (insn);
806 120698 : continue;
807 : }
808 :
809 : /* IRA can generate move insns involving pseudos. It is
810 : better remove them earlier to speed up compiler a bit.
811 : It is also better to do it here as they might not pass
812 : final RTL check in LRA, (e.g. insn moving a control
813 : register into itself). So remove an useless move insn
814 : unless next insn is USE marking the return reg (we should
815 : save this as some subsequent optimizations assume that
816 : such original insns are saved). */
817 81488729 : if (NONJUMP_INSN_P (insn) && GET_CODE (pat) == SET
818 69022936 : && REG_P (SET_SRC (pat)) && REG_P (SET_DEST (pat))
819 17904316 : && REGNO (SET_SRC (pat)) == REGNO (SET_DEST (pat))
820 159973722 : && REGNO (SET_SRC (pat)) >= FIRST_PSEUDO_REGISTER)
821 : {
822 0 : lra_invalidate_insn_data (insn);
823 0 : delete_insn (insn);
824 0 : continue;
825 : }
826 :
827 148876185 : lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
828 148876185 : struct lra_insn_reg *reg;
829 :
830 309231216 : for (reg = id->regs; reg != NULL; reg = reg->next)
831 160362163 : if (reg->regno >= FIRST_PSEUDO_REGISTER
832 105019998 : && lra_reg_info [reg->regno].nrefs == 0)
833 : break;
834 :
835 148876185 : if (reg != NULL)
836 : {
837 : /* Pseudos still can be in debug insns in some very rare
838 : and complicated cases, e.g. the pseudo was removed by
839 : inheritance and the debug insn is not EBBs where the
840 : inheritance happened. It is difficult and time
841 : consuming to find what hard register corresponds the
842 : pseudo -- so just remove the debug insn. Another
843 : solution could be assigning hard reg/memory but it
844 : would be a misleading info. It is better not to have
845 : info than have it wrong. */
846 7132 : lra_assert (DEBUG_INSN_P (insn));
847 7132 : lra_invalidate_insn_data (insn);
848 7132 : delete_insn (insn);
849 7132 : continue;
850 : }
851 :
852 148869053 : struct lra_static_insn_data *static_id = id->insn_static_data;
853 148869053 : bool insn_change_p = false;
854 :
855 394742590 : for (i = id->insn_static_data->n_operands - 1; i >= 0; i--)
856 205541411 : if ((DEBUG_INSN_P (insn) || ! static_id->operand[i].is_operator)
857 443651538 : && alter_subregs (id->operand_loc[i], ! DEBUG_INSN_P (insn)))
858 : {
859 3123547 : lra_update_dup (id, i);
860 3123547 : insn_change_p = true;
861 : }
862 148869053 : if ((GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
863 148869053 : && alter_subregs (&XEXP (pat, 0), false))
864 : insn_change_p = true;
865 148869053 : if (insn_change_p)
866 2785853 : lra_update_operator_dups (id);
867 :
868 148869053 : if ((set = single_set (insn)) != NULL
869 91335749 : && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))
870 167846785 : && REGNO (SET_SRC (set)) == REGNO (SET_DEST (set)))
871 : {
872 : /* Remove an useless move insn. IRA can generate move
873 : insns involving pseudos. It is better remove them
874 : earlier to speed up compiler a bit. It is also
875 : better to do it here as they might not pass final RTL
876 : check in LRA, (e.g. insn moving a control register
877 : into itself). */
878 11454022 : lra_invalidate_insn_data (insn);
879 11454022 : delete_insn (insn);
880 : }
881 : }
882 1531123 : }
|