]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lra-assigns.c
Remove IOR_HARD_REG_SET
[thirdparty/gcc.git] / gcc / lra-assigns.c
CommitLineData
55a2c322 1/* Assign reload pseudos.
a5544970 2 Copyright (C) 2010-2019 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's main objective is to assign hard registers to reload
23 pseudos. It also tries to allocate hard registers to other
24 pseudos, but at a lower priority than the reload pseudos. The pass
25 does not transform the RTL.
26
27 We must allocate a hard register to every reload pseudo. We try to
28 increase the chances of finding a viable allocation by assigning
29 the pseudos in order of fewest available hard registers first. If
30 we still fail to find a hard register, we spill other (non-reload)
31 pseudos in order to make room.
32
33 find_hard_regno_for finds hard registers for allocation without
34 spilling. spill_for does the same with spilling. Both functions
35 use a cost model to determine the most profitable choice of hard
36 and spill registers.
37
38 Once we have finished allocating reload pseudos, we also try to
39 assign registers to other (non-reload) pseudos. This is useful if
40 hard registers were freed up by the spilling just described.
41
42 We try to assign hard registers by collecting pseudos into threads.
43 These threads contain reload and inheritance pseudos that are
44 connected by copies (move insns). Doing this improves the chances
45 of pseudos in the thread getting the same hard register and, as a
46 result, of allowing some move insns to be deleted.
47
48 When we assign a hard register to a pseudo, we decrease the cost of
49 using the same hard register for pseudos that are connected by
50 copies.
51
52 If two hard registers have the same frequency-derived cost, we
53 prefer hard registers with higher priorities. The mapping of
54 registers to priorities is controlled by the register_priority
55 target hook. For example, x86-64 has a few register priorities:
56 hard registers with and without REX prefixes have different
57 priorities. This permits us to generate smaller code as insns
58 without REX prefixes are shorter.
59
60 If a few hard registers are still equally good for the assignment,
61 we choose the least used hard register. It is called leveling and
62 may be profitable for some targets.
63
64 Only insns with changed allocation pseudos are processed on the
65 next constraint pass.
66
67 The pseudo live-ranges are used to find conflicting pseudos.
68
69 For understanding the code, it is important to keep in mind that
70 inheritance, split, and reload pseudos created since last
71 constraint pass have regno >= lra_constraint_new_regno_start.
72 Inheritance and split pseudos created on any pass are in the
73 corresponding bitmaps. Inheritance and split pseudos since the
74 last constraint pass have also the corresponding non-negative
75 restore_regno. */
76
77#include "config.h"
78#include "system.h"
79#include "coretypes.h"
c7131fb2 80#include "backend.h"
957060b5 81#include "target.h"
55a2c322 82#include "rtl.h"
957060b5
AM
83#include "tree.h"
84#include "predict.h"
c7131fb2 85#include "df.h"
4d0cdd0c 86#include "memmodel.h"
55a2c322 87#include "tm_p.h"
55a2c322 88#include "insn-config.h"
957060b5
AM
89#include "regs.h"
90#include "ira.h"
55a2c322 91#include "recog.h"
957060b5 92#include "rtl-error.h"
55a2c322 93#include "sparseset.h"
88def637 94#include "params.h"
c7131fb2 95#include "lra.h"
55a2c322
VM
96#include "lra-int.h"
97
f54437d5
VM
98/* Current iteration number of the pass and current iteration number
99 of the pass after the latest spill pass when any former reload
100 pseudo was spilled. */
101int lra_assignment_iter;
102int lra_assignment_iter_after_spill;
103
104/* Flag of spilling former reload pseudos on this pass. */
105static bool former_reload_pseudo_spill_p;
106
55a2c322
VM
107/* Array containing corresponding values of function
108 lra_get_allocno_class. It is used to speed up the code. */
109static enum reg_class *regno_allocno_class_array;
110
8a8330b7
VM
111/* Array containing lengths of pseudo live ranges. It is used to
112 speed up the code. */
113static int *regno_live_length;
114
55a2c322
VM
115/* Information about the thread to which a pseudo belongs. Threads are
116 a set of connected reload and inheritance pseudos with the same set of
117 available hard registers. Lone registers belong to their own threads. */
118struct regno_assign_info
119{
120 /* First/next pseudo of the same thread. */
121 int first, next;
122 /* Frequency of the thread (execution frequency of only reload
123 pseudos in the thread when the thread contains a reload pseudo).
124 Defined only for the first thread pseudo. */
125 int freq;
126};
127
128/* Map regno to the corresponding regno assignment info. */
129static struct regno_assign_info *regno_assign_info;
130
bc404e1b
VM
131/* All inherited, subreg or optional pseudos created before last spill
132 sub-pass. Such pseudos are permitted to get memory instead of hard
133 regs. */
134static bitmap_head non_reload_pseudos;
135
55a2c322
VM
136/* Process a pseudo copy with execution frequency COPY_FREQ connecting
137 REGNO1 and REGNO2 to form threads. */
138static void
139process_copy_to_form_thread (int regno1, int regno2, int copy_freq)
140{
141 int last, regno1_first, regno2_first;
142
143 lra_assert (regno1 >= lra_constraint_new_regno_start
144 && regno2 >= lra_constraint_new_regno_start);
145 regno1_first = regno_assign_info[regno1].first;
146 regno2_first = regno_assign_info[regno2].first;
147 if (regno1_first != regno2_first)
148 {
149 for (last = regno2_first;
150 regno_assign_info[last].next >= 0;
151 last = regno_assign_info[last].next)
152 regno_assign_info[last].first = regno1_first;
153 regno_assign_info[last].first = regno1_first;
154 regno_assign_info[last].next = regno_assign_info[regno1_first].next;
155 regno_assign_info[regno1_first].next = regno2_first;
156 regno_assign_info[regno1_first].freq
157 += regno_assign_info[regno2_first].freq;
158 }
159 regno_assign_info[regno1_first].freq -= 2 * copy_freq;
160 lra_assert (regno_assign_info[regno1_first].freq >= 0);
161}
162
163/* Initialize REGNO_ASSIGN_INFO and form threads. */
164static void
165init_regno_assign_info (void)
166{
167 int i, regno1, regno2, max_regno = max_reg_num ();
168 lra_copy_t cp;
f4eafc30 169
55a2c322
VM
170 regno_assign_info = XNEWVEC (struct regno_assign_info, max_regno);
171 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
172 {
173 regno_assign_info[i].first = i;
174 regno_assign_info[i].next = -1;
175 regno_assign_info[i].freq = lra_reg_info[i].freq;
176 }
177 /* Form the threads. */
178 for (i = 0; (cp = lra_get_copy (i)) != NULL; i++)
179 if ((regno1 = cp->regno1) >= lra_constraint_new_regno_start
180 && (regno2 = cp->regno2) >= lra_constraint_new_regno_start
181 && reg_renumber[regno1] < 0 && lra_reg_info[regno1].nrefs != 0
182 && reg_renumber[regno2] < 0 && lra_reg_info[regno2].nrefs != 0
183 && (ira_class_hard_regs_num[regno_allocno_class_array[regno1]]
184 == ira_class_hard_regs_num[regno_allocno_class_array[regno2]]))
185 process_copy_to_form_thread (regno1, regno2, cp->freq);
186}
187
188/* Free REGNO_ASSIGN_INFO. */
189static void
190finish_regno_assign_info (void)
191{
192 free (regno_assign_info);
193}
194
195/* The function is used to sort *reload* and *inheritance* pseudos to
196 try to assign them hard registers. We put pseudos from the same
197 thread always nearby. */
198static int
199reload_pseudo_compare_func (const void *v1p, const void *v2p)
200{
201 int r1 = *(const int *) v1p, r2 = *(const int *) v2p;
202 enum reg_class cl1 = regno_allocno_class_array[r1];
203 enum reg_class cl2 = regno_allocno_class_array[r2];
204 int diff;
f4eafc30 205
55a2c322
VM
206 lra_assert (r1 >= lra_constraint_new_regno_start
207 && r2 >= lra_constraint_new_regno_start);
f4eafc30 208
55a2c322
VM
209 /* Prefer to assign reload registers with smaller classes first to
210 guarantee assignment to all reload registers. */
211 if ((diff = (ira_class_hard_regs_num[cl1]
212 - ira_class_hard_regs_num[cl2])) != 0)
213 return diff;
47918951
VM
214 /* Allocate bigger pseudos first to avoid register file
215 fragmentation. */
216 if ((diff
217 = (ira_reg_class_max_nregs[cl2][lra_reg_info[r2].biggest_mode]
218 - ira_reg_class_max_nregs[cl1][lra_reg_info[r1].biggest_mode])) != 0)
219 return diff;
ffaea117
AM
220 if ((diff = (regno_assign_info[regno_assign_info[r2].first].freq
221 - regno_assign_info[regno_assign_info[r1].first].freq)) != 0)
222 return diff;
55a2c322
VM
223 /* Put pseudos from the thread nearby. */
224 if ((diff = regno_assign_info[r1].first - regno_assign_info[r2].first) != 0)
225 return diff;
8a8330b7
VM
226 /* Prefer pseudos with longer live ranges. It sets up better
227 prefered hard registers for the thread pseudos and decreases
228 register-register moves between the thread pseudos. */
229 if ((diff = regno_live_length[r2] - regno_live_length[r1]) != 0)
230 return diff;
55a2c322
VM
231 /* If regs are equally good, sort by their numbers, so that the
232 results of qsort leave nothing to chance. */
233 return r1 - r2;
234}
235
236/* The function is used to sort *non-reload* pseudos to try to assign
237 them hard registers. The order calculation is simpler than in the
238 previous function and based on the pseudo frequency usage. */
239static int
240pseudo_compare_func (const void *v1p, const void *v2p)
241{
242 int r1 = *(const int *) v1p, r2 = *(const int *) v2p;
243 int diff;
244
b81a2f0d
VM
245 /* Assign hard reg to static chain pointer first pseudo when
246 non-local goto is used. */
f0a40456
AM
247 if ((diff = (non_spilled_static_chain_regno_p (r2)
248 - non_spilled_static_chain_regno_p (r1))) != 0)
249 return diff;
b81a2f0d 250
55a2c322
VM
251 /* Prefer to assign more frequently used registers first. */
252 if ((diff = lra_reg_info[r2].freq - lra_reg_info[r1].freq) != 0)
253 return diff;
f4eafc30 254
55a2c322
VM
255 /* If regs are equally good, sort by their numbers, so that the
256 results of qsort leave nothing to chance. */
257 return r1 - r2;
258}
259
260/* Arrays of size LRA_LIVE_MAX_POINT mapping a program point to the
261 pseudo live ranges with given start point. We insert only live
262 ranges of pseudos interesting for assignment purposes. They are
263 reload pseudos and pseudos assigned to hard registers. */
264static lra_live_range_t *start_point_ranges;
265
266/* Used as a flag that a live range is not inserted in the start point
267 chain. */
268static struct lra_live_range not_in_chain_mark;
269
270/* Create and set up START_POINT_RANGES. */
271static void
272create_live_range_start_chains (void)
273{
274 int i, max_regno;
275 lra_live_range_t r;
276
277 start_point_ranges = XCNEWVEC (lra_live_range_t, lra_live_max_point);
278 max_regno = max_reg_num ();
279 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
280 if (i >= lra_constraint_new_regno_start || reg_renumber[i] >= 0)
281 {
282 for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
283 {
284 r->start_next = start_point_ranges[r->start];
285 start_point_ranges[r->start] = r;
286 }
287 }
288 else
289 {
290 for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
291 r->start_next = &not_in_chain_mark;
292 }
293}
294
295/* Insert live ranges of pseudo REGNO into start chains if they are
296 not there yet. */
297static void
298insert_in_live_range_start_chain (int regno)
299{
300 lra_live_range_t r = lra_reg_info[regno].live_ranges;
301
302 if (r->start_next != &not_in_chain_mark)
303 return;
304 for (; r != NULL; r = r->next)
305 {
306 r->start_next = start_point_ranges[r->start];
307 start_point_ranges[r->start] = r;
308 }
309}
310
311/* Free START_POINT_RANGES. */
312static void
313finish_live_range_start_chains (void)
314{
315 gcc_assert (start_point_ranges != NULL);
316 free (start_point_ranges);
317 start_point_ranges = NULL;
318}
319
320/* Map: program point -> bitmap of all pseudos living at the point and
321 assigned to hard registers. */
322static bitmap_head *live_hard_reg_pseudos;
323static bitmap_obstack live_hard_reg_pseudos_bitmap_obstack;
324
325/* reg_renumber corresponding to pseudos marked in
326 live_hard_reg_pseudos. reg_renumber might be not matched to
327 live_hard_reg_pseudos but live_pseudos_reg_renumber always reflects
328 live_hard_reg_pseudos. */
329static int *live_pseudos_reg_renumber;
330
331/* Sparseset used to calculate living hard reg pseudos for some program
332 point range. */
333static sparseset live_range_hard_reg_pseudos;
334
335/* Sparseset used to calculate living reload/inheritance pseudos for
336 some program point range. */
337static sparseset live_range_reload_inheritance_pseudos;
338
339/* Allocate and initialize the data about living pseudos at program
340 points. */
341static void
342init_lives (void)
343{
344 int i, max_regno = max_reg_num ();
345
346 live_range_hard_reg_pseudos = sparseset_alloc (max_regno);
347 live_range_reload_inheritance_pseudos = sparseset_alloc (max_regno);
348 live_hard_reg_pseudos = XNEWVEC (bitmap_head, lra_live_max_point);
349 bitmap_obstack_initialize (&live_hard_reg_pseudos_bitmap_obstack);
350 for (i = 0; i < lra_live_max_point; i++)
351 bitmap_initialize (&live_hard_reg_pseudos[i],
352 &live_hard_reg_pseudos_bitmap_obstack);
353 live_pseudos_reg_renumber = XNEWVEC (int, max_regno);
354 for (i = 0; i < max_regno; i++)
355 live_pseudos_reg_renumber[i] = -1;
356}
357
358/* Free the data about living pseudos at program points. */
359static void
360finish_lives (void)
361{
362 sparseset_free (live_range_hard_reg_pseudos);
363 sparseset_free (live_range_reload_inheritance_pseudos);
364 free (live_hard_reg_pseudos);
365 bitmap_obstack_release (&live_hard_reg_pseudos_bitmap_obstack);
366 free (live_pseudos_reg_renumber);
367}
368
369/* Update the LIVE_HARD_REG_PSEUDOS and LIVE_PSEUDOS_REG_RENUMBER
370 entries for pseudo REGNO. Assume that the register has been
371 spilled if FREE_P, otherwise assume that it has been assigned
372 reg_renumber[REGNO] (if >= 0). We also insert the pseudo live
373 ranges in the start chains when it is assumed to be assigned to a
374 hard register because we use the chains of pseudos assigned to hard
375 registers during allocation. */
376static void
377update_lives (int regno, bool free_p)
378{
379 int p;
380 lra_live_range_t r;
381
382 if (reg_renumber[regno] < 0)
383 return;
384 live_pseudos_reg_renumber[regno] = free_p ? -1 : reg_renumber[regno];
385 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
386 {
387 for (p = r->start; p <= r->finish; p++)
388 if (free_p)
389 bitmap_clear_bit (&live_hard_reg_pseudos[p], regno);
390 else
391 {
392 bitmap_set_bit (&live_hard_reg_pseudos[p], regno);
393 insert_in_live_range_start_chain (regno);
394 }
395 }
396}
397
398/* Sparseset used to calculate reload pseudos conflicting with a given
399 pseudo when we are trying to find a hard register for the given
400 pseudo. */
401static sparseset conflict_reload_and_inheritance_pseudos;
402
403/* Map: program point -> bitmap of all reload and inheritance pseudos
404 living at the point. */
405static bitmap_head *live_reload_and_inheritance_pseudos;
406static bitmap_obstack live_reload_and_inheritance_pseudos_bitmap_obstack;
407
408/* Allocate and initialize data about living reload pseudos at any
409 given program point. */
410static void
411init_live_reload_and_inheritance_pseudos (void)
412{
413 int i, p, max_regno = max_reg_num ();
414 lra_live_range_t r;
f4eafc30 415
55a2c322
VM
416 conflict_reload_and_inheritance_pseudos = sparseset_alloc (max_regno);
417 live_reload_and_inheritance_pseudos = XNEWVEC (bitmap_head, lra_live_max_point);
418 bitmap_obstack_initialize (&live_reload_and_inheritance_pseudos_bitmap_obstack);
419 for (p = 0; p < lra_live_max_point; p++)
420 bitmap_initialize (&live_reload_and_inheritance_pseudos[p],
421 &live_reload_and_inheritance_pseudos_bitmap_obstack);
422 for (i = lra_constraint_new_regno_start; i < max_regno; i++)
423 {
424 for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
425 for (p = r->start; p <= r->finish; p++)
426 bitmap_set_bit (&live_reload_and_inheritance_pseudos[p], i);
427 }
428}
429
430/* Finalize data about living reload pseudos at any given program
431 point. */
432static void
433finish_live_reload_and_inheritance_pseudos (void)
434{
435 sparseset_free (conflict_reload_and_inheritance_pseudos);
436 free (live_reload_and_inheritance_pseudos);
437 bitmap_obstack_release (&live_reload_and_inheritance_pseudos_bitmap_obstack);
438}
439
440/* The value used to check that cost of given hard reg is really
441 defined currently. */
442static int curr_hard_regno_costs_check = 0;
443/* Array used to check that cost of the corresponding hard reg (the
444 array element index) is really defined currently. */
445static int hard_regno_costs_check[FIRST_PSEUDO_REGISTER];
446/* The current costs of allocation of hard regs. Defined only if the
447 value of the corresponding element of the previous array is equal to
448 CURR_HARD_REGNO_COSTS_CHECK. */
449static int hard_regno_costs[FIRST_PSEUDO_REGISTER];
450
451/* Adjust cost of HARD_REGNO by INCR. Reset the cost first if it is
452 not defined yet. */
453static inline void
454adjust_hard_regno_cost (int hard_regno, int incr)
455{
456 if (hard_regno_costs_check[hard_regno] != curr_hard_regno_costs_check)
457 hard_regno_costs[hard_regno] = 0;
458 hard_regno_costs_check[hard_regno] = curr_hard_regno_costs_check;
459 hard_regno_costs[hard_regno] += incr;
460}
461
462/* Try to find a free hard register for pseudo REGNO. Return the
463 hard register on success and set *COST to the cost of using
464 that register. (If several registers have equal cost, the one with
465 the highest priority wins.) Return -1 on failure.
466
9e038952
VM
467 If FIRST_P, return the first available hard reg ignoring other
468 criteria, e.g. allocation cost. This approach results in less hard
469 reg pool fragmentation and permit to allocate hard regs to reload
470 pseudos in complicated situations where pseudo sizes are different.
471
55a2c322 472 If TRY_ONLY_HARD_REGNO >= 0, consider only that hard register,
34349d55
VM
473 otherwise consider all hard registers in REGNO's class.
474
475 If REGNO_SET is not empty, only hard registers from the set are
476 considered. */
55a2c322 477static int
34349d55
VM
478find_hard_regno_for_1 (int regno, int *cost, int try_only_hard_regno,
479 bool first_p, HARD_REG_SET regno_set)
55a2c322
VM
480{
481 HARD_REG_SET conflict_set;
482 int best_cost = INT_MAX, best_priority = INT_MIN, best_usage = INT_MAX;
483 lra_live_range_t r;
484 int p, i, j, rclass_size, best_hard_regno, priority, hard_regno;
485 int hr, conflict_hr, nregs;
ef4bddc2 486 machine_mode biggest_mode;
55a2c322 487 unsigned int k, conflict_regno;
73ca989c
RS
488 poly_int64 offset;
489 int val, biggest_nregs, nregs_diff;
55a2c322
VM
490 enum reg_class rclass;
491 bitmap_iterator bi;
492 bool *rclass_intersect_p;
a4971e68 493 HARD_REG_SET impossible_start_hard_regs, available_regs;
55a2c322 494
34349d55 495 if (hard_reg_set_empty_p (regno_set))
6576d245 496 conflict_set = lra_no_alloc_regs;
34349d55 497 else
44942965 498 conflict_set = ~regno_set | lra_no_alloc_regs;
55a2c322
VM
499 rclass = regno_allocno_class_array[regno];
500 rclass_intersect_p = ira_reg_classes_intersect_p[rclass];
501 curr_hard_regno_costs_check++;
502 sparseset_clear (conflict_reload_and_inheritance_pseudos);
503 sparseset_clear (live_range_hard_reg_pseudos);
44942965 504 conflict_set |= lra_reg_info[regno].conflict_hard_regs;
55a2c322
VM
505 biggest_mode = lra_reg_info[regno].biggest_mode;
506 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
507 {
508 EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[r->start], 0, k, bi)
509 if (rclass_intersect_p[regno_allocno_class_array[k]])
510 sparseset_set_bit (live_range_hard_reg_pseudos, k);
511 EXECUTE_IF_SET_IN_BITMAP (&live_reload_and_inheritance_pseudos[r->start],
512 0, k, bi)
513 if (lra_reg_info[k].preferred_hard_regno1 >= 0
514 && live_pseudos_reg_renumber[k] < 0
515 && rclass_intersect_p[regno_allocno_class_array[k]])
516 sparseset_set_bit (conflict_reload_and_inheritance_pseudos, k);
517 for (p = r->start + 1; p <= r->finish; p++)
518 {
519 lra_live_range_t r2;
f4eafc30 520
55a2c322
VM
521 for (r2 = start_point_ranges[p];
522 r2 != NULL;
523 r2 = r2->start_next)
524 {
525 if (r2->regno >= lra_constraint_new_regno_start
526 && lra_reg_info[r2->regno].preferred_hard_regno1 >= 0
527 && live_pseudos_reg_renumber[r2->regno] < 0
528 && rclass_intersect_p[regno_allocno_class_array[r2->regno]])
529 sparseset_set_bit (conflict_reload_and_inheritance_pseudos,
530 r2->regno);
531 if (live_pseudos_reg_renumber[r2->regno] >= 0
532 && rclass_intersect_p[regno_allocno_class_array[r2->regno]])
533 sparseset_set_bit (live_range_hard_reg_pseudos, r2->regno);
534 }
535 }
536 }
537 if ((hard_regno = lra_reg_info[regno].preferred_hard_regno1) >= 0)
538 {
539 adjust_hard_regno_cost
540 (hard_regno, -lra_reg_info[regno].preferred_hard_regno_profit1);
541 if ((hard_regno = lra_reg_info[regno].preferred_hard_regno2) >= 0)
542 adjust_hard_regno_cost
543 (hard_regno, -lra_reg_info[regno].preferred_hard_regno_profit2);
544 }
545#ifdef STACK_REGS
546 if (lra_reg_info[regno].no_stack_p)
547 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
548 SET_HARD_REG_BIT (conflict_set, i);
549#endif
550 sparseset_clear_bit (conflict_reload_and_inheritance_pseudos, regno);
551 val = lra_reg_info[regno].val;
d70a81dd 552 offset = lra_reg_info[regno].offset;
55a2c322
VM
553 CLEAR_HARD_REG_SET (impossible_start_hard_regs);
554 EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos, conflict_regno)
9eb7045b
VM
555 {
556 conflict_hr = live_pseudos_reg_renumber[conflict_regno];
557 if (lra_reg_val_equal_p (conflict_regno, val, offset))
558 {
559 conflict_hr = live_pseudos_reg_renumber[conflict_regno];
ad474626
RS
560 nregs = hard_regno_nregs (conflict_hr,
561 lra_reg_info[conflict_regno].biggest_mode);
9eb7045b
VM
562 /* Remember about multi-register pseudos. For example, 2
563 hard register pseudos can start on the same hard register
67914693 564 but cannot start on HR and HR+1/HR-1. */
9eb7045b
VM
565 for (hr = conflict_hr + 1;
566 hr < FIRST_PSEUDO_REGISTER && hr < conflict_hr + nregs;
567 hr++)
568 SET_HARD_REG_BIT (impossible_start_hard_regs, hr);
569 for (hr = conflict_hr - 1;
4edd6298 570 hr >= 0 && (int) end_hard_regno (biggest_mode, hr) > conflict_hr;
9eb7045b
VM
571 hr--)
572 SET_HARD_REG_BIT (impossible_start_hard_regs, hr);
573 }
574 else
575 {
b8506a8a 576 machine_mode biggest_conflict_mode
9eb7045b
VM
577 = lra_reg_info[conflict_regno].biggest_mode;
578 int biggest_conflict_nregs
ad474626 579 = hard_regno_nregs (conflict_hr, biggest_conflict_mode);
9eb7045b 580
ad474626
RS
581 nregs_diff
582 = (biggest_conflict_nregs
583 - hard_regno_nregs (conflict_hr,
584 PSEUDO_REGNO_MODE (conflict_regno)));
9eb7045b
VM
585 add_to_hard_reg_set (&conflict_set,
586 biggest_conflict_mode,
587 conflict_hr
588 - (WORDS_BIG_ENDIAN ? nregs_diff : 0));
589 if (hard_reg_set_subset_p (reg_class_contents[rclass],
590 conflict_set))
591 return -1;
592 }
593 }
55a2c322
VM
594 EXECUTE_IF_SET_IN_SPARSESET (conflict_reload_and_inheritance_pseudos,
595 conflict_regno)
d70a81dd 596 if (!lra_reg_val_equal_p (conflict_regno, val, offset))
55a2c322
VM
597 {
598 lra_assert (live_pseudos_reg_renumber[conflict_regno] < 0);
599 if ((hard_regno
600 = lra_reg_info[conflict_regno].preferred_hard_regno1) >= 0)
601 {
602 adjust_hard_regno_cost
603 (hard_regno,
604 lra_reg_info[conflict_regno].preferred_hard_regno_profit1);
605 if ((hard_regno
606 = lra_reg_info[conflict_regno].preferred_hard_regno2) >= 0)
607 adjust_hard_regno_cost
608 (hard_regno,
609 lra_reg_info[conflict_regno].preferred_hard_regno_profit2);
610 }
611 }
612 /* Make sure that all registers in a multi-word pseudo belong to the
613 required class. */
614 IOR_COMPL_HARD_REG_SET (conflict_set, reg_class_contents[rclass]);
615 lra_assert (rclass != NO_REGS);
616 rclass_size = ira_class_hard_regs_num[rclass];
617 best_hard_regno = -1;
618 hard_regno = ira_class_hard_regs[rclass][0];
ad474626 619 biggest_nregs = hard_regno_nregs (hard_regno, biggest_mode);
55a2c322 620 nregs_diff = (biggest_nregs
ad474626 621 - hard_regno_nregs (hard_regno, PSEUDO_REGNO_MODE (regno)));
6576d245 622 available_regs = reg_class_contents[rclass];
a4971e68 623 AND_COMPL_HARD_REG_SET (available_regs, lra_no_alloc_regs);
55a2c322
VM
624 for (i = 0; i < rclass_size; i++)
625 {
626 if (try_only_hard_regno >= 0)
627 hard_regno = try_only_hard_regno;
628 else
629 hard_regno = ira_class_hard_regs[rclass][i];
630 if (! overlaps_hard_reg_set_p (conflict_set,
631 PSEUDO_REGNO_MODE (regno), hard_regno)
f939c3e6
RS
632 && targetm.hard_regno_mode_ok (hard_regno,
633 PSEUDO_REGNO_MODE (regno))
67914693 634 /* We cannot use prohibited_class_mode_regs for all classes
7e964f49
VM
635 because it is not defined for all classes. */
636 && (ira_allocno_class_translate[rclass] != rclass
637 || ! TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs
638 [rclass][PSEUDO_REGNO_MODE (regno)],
639 hard_regno))
55a2c322
VM
640 && ! TEST_HARD_REG_BIT (impossible_start_hard_regs, hard_regno)
641 && (nregs_diff == 0
a1b46e46
JR
642 || (WORDS_BIG_ENDIAN
643 ? (hard_regno - nregs_diff >= 0
a4971e68 644 && TEST_HARD_REG_BIT (available_regs,
a1b46e46 645 hard_regno - nregs_diff))
a4971e68 646 : TEST_HARD_REG_BIT (available_regs,
a1b46e46 647 hard_regno + nregs_diff))))
55a2c322
VM
648 {
649 if (hard_regno_costs_check[hard_regno]
650 != curr_hard_regno_costs_check)
651 {
652 hard_regno_costs_check[hard_regno] = curr_hard_regno_costs_check;
653 hard_regno_costs[hard_regno] = 0;
654 }
655 for (j = 0;
ad474626 656 j < hard_regno_nregs (hard_regno, PSEUDO_REGNO_MODE (regno));
55a2c322
VM
657 j++)
658 if (! TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + j)
659 && ! df_regs_ever_live_p (hard_regno + j))
660 /* It needs save restore. */
661 hard_regno_costs[hard_regno]
fef37404
VM
662 += (2
663 * REG_FREQ_FROM_BB (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
664 + 1);
55a2c322
VM
665 priority = targetm.register_priority (hard_regno);
666 if (best_hard_regno < 0 || hard_regno_costs[hard_regno] < best_cost
667 || (hard_regno_costs[hard_regno] == best_cost
668 && (priority > best_priority
3b9ceb4b 669 || (targetm.register_usage_leveling_p ()
55a2c322
VM
670 && priority == best_priority
671 && best_usage > lra_hard_reg_usage[hard_regno]))))
672 {
673 best_hard_regno = hard_regno;
674 best_cost = hard_regno_costs[hard_regno];
675 best_priority = priority;
676 best_usage = lra_hard_reg_usage[hard_regno];
677 }
678 }
9e038952 679 if (try_only_hard_regno >= 0 || (first_p && best_hard_regno >= 0))
55a2c322
VM
680 break;
681 }
682 if (best_hard_regno >= 0)
683 *cost = best_cost - lra_reg_info[regno].freq;
684 return best_hard_regno;
685}
686
34349d55
VM
687/* A wrapper for find_hard_regno_for_1 (see comments for that function
688 description). This function tries to find a hard register for
689 preferred class first if it is worth. */
690static int
691find_hard_regno_for (int regno, int *cost, int try_only_hard_regno, bool first_p)
692{
693 int hard_regno;
694 HARD_REG_SET regno_set;
695
696 /* Only original pseudos can have a different preferred class. */
697 if (try_only_hard_regno < 0 && regno < lra_new_regno_start)
698 {
699 enum reg_class pref_class = reg_preferred_class (regno);
700
701 if (regno_allocno_class_array[regno] != pref_class)
702 {
703 hard_regno = find_hard_regno_for_1 (regno, cost, -1, first_p,
704 reg_class_contents[pref_class]);
705 if (hard_regno >= 0)
706 return hard_regno;
707 }
708 }
709 CLEAR_HARD_REG_SET (regno_set);
710 return find_hard_regno_for_1 (regno, cost, try_only_hard_regno, first_p,
711 regno_set);
712}
713
55a2c322
VM
714/* Current value used for checking elements in
715 update_hard_regno_preference_check. */
716static int curr_update_hard_regno_preference_check;
717/* If an element value is equal to the above variable value, then the
718 corresponding regno has been processed for preference
719 propagation. */
720static int *update_hard_regno_preference_check;
721
722/* Update the preference for using HARD_REGNO for pseudos that are
723 connected directly or indirectly with REGNO. Apply divisor DIV
724 to any preference adjustments.
725
726 The more indirectly a pseudo is connected, the smaller its effect
727 should be. We therefore increase DIV on each "hop". */
728static void
729update_hard_regno_preference (int regno, int hard_regno, int div)
730{
731 int another_regno, cost;
732 lra_copy_t cp, next_cp;
733
734 /* Search depth 5 seems to be enough. */
735 if (div > (1 << 5))
736 return;
737 for (cp = lra_reg_info[regno].copies; cp != NULL; cp = next_cp)
738 {
739 if (cp->regno1 == regno)
740 {
741 next_cp = cp->regno1_next;
742 another_regno = cp->regno2;
743 }
744 else if (cp->regno2 == regno)
745 {
746 next_cp = cp->regno2_next;
747 another_regno = cp->regno1;
748 }
749 else
750 gcc_unreachable ();
751 if (reg_renumber[another_regno] < 0
752 && (update_hard_regno_preference_check[another_regno]
753 != curr_update_hard_regno_preference_check))
754 {
755 update_hard_regno_preference_check[another_regno]
756 = curr_update_hard_regno_preference_check;
757 cost = cp->freq < div ? 1 : cp->freq / div;
758 lra_setup_reload_pseudo_preferenced_hard_reg
759 (another_regno, hard_regno, cost);
760 update_hard_regno_preference (another_regno, hard_regno, div * 2);
761 }
762 }
763}
764
2b778c9d
VM
765/* Return prefix title for pseudo REGNO. */
766static const char *
767pseudo_prefix_title (int regno)
768{
769 return
770 (regno < lra_constraint_new_regno_start ? ""
771 : bitmap_bit_p (&lra_inheritance_pseudos, regno) ? "inheritance "
772 : bitmap_bit_p (&lra_split_regs, regno) ? "split "
773 : bitmap_bit_p (&lra_optional_reload_pseudos, regno) ? "optional reload "
774 : bitmap_bit_p (&lra_subreg_reload_pseudos, regno) ? "subreg reload "
775 : "reload ");
776}
777
55a2c322
VM
778/* Update REG_RENUMBER and other pseudo preferences by assignment of
779 HARD_REGNO to pseudo REGNO and print about it if PRINT_P. */
780void
781lra_setup_reg_renumber (int regno, int hard_regno, bool print_p)
782{
783 int i, hr;
784
67914693 785 /* We cannot just reassign hard register. */
55a2c322
VM
786 lra_assert (hard_regno < 0 || reg_renumber[regno] < 0);
787 if ((hr = hard_regno) < 0)
788 hr = reg_renumber[regno];
789 reg_renumber[regno] = hard_regno;
790 lra_assert (hr >= 0);
ad474626 791 for (i = 0; i < hard_regno_nregs (hr, PSEUDO_REGNO_MODE (regno)); i++)
55a2c322
VM
792 if (hard_regno < 0)
793 lra_hard_reg_usage[hr + i] -= lra_reg_info[regno].freq;
794 else
795 lra_hard_reg_usage[hr + i] += lra_reg_info[regno].freq;
796 if (print_p && lra_dump_file != NULL)
797 fprintf (lra_dump_file, " Assign %d to %sr%d (freq=%d)\n",
2b778c9d 798 reg_renumber[regno], pseudo_prefix_title (regno),
55a2c322
VM
799 regno, lra_reg_info[regno].freq);
800 if (hard_regno >= 0)
801 {
802 curr_update_hard_regno_preference_check++;
803 update_hard_regno_preference (regno, hard_regno, 1);
804 }
805}
806
807/* Pseudos which occur in insns containing a particular pseudo. */
808static bitmap_head insn_conflict_pseudos;
809
810/* Bitmaps used to contain spill pseudos for given pseudo hard regno
811 and best spill pseudos for given pseudo (and best hard regno). */
812static bitmap_head spill_pseudos_bitmap, best_spill_pseudos_bitmap;
813
814/* Current pseudo check for validity of elements in
815 TRY_HARD_REG_PSEUDOS. */
816static int curr_pseudo_check;
817/* Array used for validity of elements in TRY_HARD_REG_PSEUDOS. */
818static int try_hard_reg_pseudos_check[FIRST_PSEUDO_REGISTER];
819/* Pseudos who hold given hard register at the considered points. */
820static bitmap_head try_hard_reg_pseudos[FIRST_PSEUDO_REGISTER];
821
822/* Set up try_hard_reg_pseudos for given program point P and class
823 RCLASS. Those are pseudos living at P and assigned to a hard
824 register of RCLASS. In other words, those are pseudos which can be
825 spilled to assign a hard register of RCLASS to a pseudo living at
826 P. */
827static void
828setup_try_hard_regno_pseudos (int p, enum reg_class rclass)
829{
830 int i, hard_regno;
ef4bddc2 831 machine_mode mode;
55a2c322
VM
832 unsigned int spill_regno;
833 bitmap_iterator bi;
834
835 /* Find what pseudos could be spilled. */
836 EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[p], 0, spill_regno, bi)
837 {
838 mode = PSEUDO_REGNO_MODE (spill_regno);
839 hard_regno = live_pseudos_reg_renumber[spill_regno];
840 if (overlaps_hard_reg_set_p (reg_class_contents[rclass],
841 mode, hard_regno))
842 {
ad474626 843 for (i = hard_regno_nregs (hard_regno, mode) - 1; i >= 0; i--)
55a2c322
VM
844 {
845 if (try_hard_reg_pseudos_check[hard_regno + i]
846 != curr_pseudo_check)
847 {
848 try_hard_reg_pseudos_check[hard_regno + i]
849 = curr_pseudo_check;
850 bitmap_clear (&try_hard_reg_pseudos[hard_regno + i]);
851 }
852 bitmap_set_bit (&try_hard_reg_pseudos[hard_regno + i],
853 spill_regno);
854 }
855 }
856 }
857}
858
859/* Assign temporarily HARD_REGNO to pseudo REGNO. Temporary
860 assignment means that we might undo the data change. */
861static void
862assign_temporarily (int regno, int hard_regno)
863{
864 int p;
865 lra_live_range_t r;
866
867 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
868 {
869 for (p = r->start; p <= r->finish; p++)
870 if (hard_regno < 0)
871 bitmap_clear_bit (&live_hard_reg_pseudos[p], regno);
872 else
873 {
874 bitmap_set_bit (&live_hard_reg_pseudos[p], regno);
875 insert_in_live_range_start_chain (regno);
876 }
877 }
878 live_pseudos_reg_renumber[regno] = hard_regno;
879}
880
8f2f6381
BS
881/* Return true iff there is a reason why pseudo SPILL_REGNO should not
882 be spilled. */
883static bool
884must_not_spill_p (unsigned spill_regno)
885{
886 if ((pic_offset_table_rtx != NULL
887 && spill_regno == REGNO (pic_offset_table_rtx))
888 || ((int) spill_regno >= lra_constraint_new_regno_start
889 && ! bitmap_bit_p (&lra_inheritance_pseudos, spill_regno)
890 && ! bitmap_bit_p (&lra_split_regs, spill_regno)
891 && ! bitmap_bit_p (&lra_subreg_reload_pseudos, spill_regno)
892 && ! bitmap_bit_p (&lra_optional_reload_pseudos, spill_regno)))
893 return true;
894 /* A reload pseudo that requires a singleton register class should
895 not be spilled.
896 FIXME: this mitigates the issue on certain i386 patterns, but
897 does not solve the general case where existing reloads fully
898 cover a limited register class. */
899 if (!bitmap_bit_p (&non_reload_pseudos, spill_regno)
5da906ca
BS
900 && reg_class_size [reg_preferred_class (spill_regno)] == 1
901 && reg_alternate_class (spill_regno) == NO_REGS)
8f2f6381
BS
902 return true;
903 return false;
904}
905
55a2c322
VM
906/* Array used for sorting reload pseudos for subsequent allocation
907 after spilling some pseudo. */
908static int *sorted_reload_pseudos;
909
910/* Spill some pseudos for a reload pseudo REGNO and return hard
911 register which should be used for pseudo after spilling. The
912 function adds spilled pseudos to SPILLED_PSEUDO_BITMAP. When we
913 choose hard register (and pseudos occupying the hard registers and
914 to be spilled), we take into account not only how REGNO will
915 benefit from the spills but also how other reload pseudos not yet
916 assigned to hard registers benefit from the spills too. In very
9e038952
VM
917 rare cases, the function can fail and return -1.
918
919 If FIRST_P, return the first available hard reg ignoring other
920 criteria, e.g. allocation cost and cost of spilling non-reload
921 pseudos. This approach results in less hard reg pool fragmentation
922 and permit to allocate hard regs to reload pseudos in complicated
923 situations where pseudo sizes are different. */
55a2c322 924static int
9e038952 925spill_for (int regno, bitmap spilled_pseudo_bitmap, bool first_p)
55a2c322
VM
926{
927 int i, j, n, p, hard_regno, best_hard_regno, cost, best_cost, rclass_size;
928 int reload_hard_regno, reload_cost;
b81a2f0d 929 bool static_p, best_static_p;
ef4bddc2 930 machine_mode mode;
55a2c322 931 enum reg_class rclass;
55a2c322
VM
932 unsigned int spill_regno, reload_regno, uid;
933 int insn_pseudos_num, best_insn_pseudos_num;
8fd827b8 934 int bad_spills_num, smallest_bad_spills_num;
55a2c322
VM
935 lra_live_range_t r;
936 bitmap_iterator bi;
937
938 rclass = regno_allocno_class_array[regno];
939 lra_assert (reg_renumber[regno] < 0 && rclass != NO_REGS);
940 bitmap_clear (&insn_conflict_pseudos);
941 bitmap_clear (&best_spill_pseudos_bitmap);
942 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
943 {
944 struct lra_insn_reg *ir;
f4eafc30 945
55a2c322
VM
946 for (ir = lra_get_insn_regs (uid); ir != NULL; ir = ir->next)
947 if (ir->regno >= FIRST_PSEUDO_REGISTER)
948 bitmap_set_bit (&insn_conflict_pseudos, ir->regno);
949 }
950 best_hard_regno = -1;
951 best_cost = INT_MAX;
b81a2f0d 952 best_static_p = TRUE;
55a2c322 953 best_insn_pseudos_num = INT_MAX;
8fd827b8 954 smallest_bad_spills_num = INT_MAX;
55a2c322
VM
955 rclass_size = ira_class_hard_regs_num[rclass];
956 mode = PSEUDO_REGNO_MODE (regno);
957 /* Invalidate try_hard_reg_pseudos elements. */
958 curr_pseudo_check++;
959 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
960 for (p = r->start; p <= r->finish; p++)
961 setup_try_hard_regno_pseudos (p, rclass);
962 for (i = 0; i < rclass_size; i++)
963 {
964 hard_regno = ira_class_hard_regs[rclass][i];
965 bitmap_clear (&spill_pseudos_bitmap);
ad474626 966 for (j = hard_regno_nregs (hard_regno, mode) - 1; j >= 0; j--)
55a2c322
VM
967 {
968 if (try_hard_reg_pseudos_check[hard_regno + j] != curr_pseudo_check)
969 continue;
970 lra_assert (!bitmap_empty_p (&try_hard_reg_pseudos[hard_regno + j]));
971 bitmap_ior_into (&spill_pseudos_bitmap,
972 &try_hard_reg_pseudos[hard_regno + j]);
973 }
974 /* Spill pseudos. */
b81a2f0d 975 static_p = false;
55a2c322 976 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
8f2f6381 977 if (must_not_spill_p (spill_regno))
55a2c322 978 goto fail;
b81a2f0d
VM
979 else if (non_spilled_static_chain_regno_p (spill_regno))
980 static_p = true;
55a2c322 981 insn_pseudos_num = 0;
8fd827b8 982 bad_spills_num = 0;
55a2c322
VM
983 if (lra_dump_file != NULL)
984 fprintf (lra_dump_file, " Trying %d:", hard_regno);
985 sparseset_clear (live_range_reload_inheritance_pseudos);
986 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
987 {
988 if (bitmap_bit_p (&insn_conflict_pseudos, spill_regno))
989 insn_pseudos_num++;
8fd827b8
VM
990 if (spill_regno >= (unsigned int) lra_bad_spill_regno_start)
991 bad_spills_num++;
55a2c322
VM
992 for (r = lra_reg_info[spill_regno].live_ranges;
993 r != NULL;
994 r = r->next)
995 {
996 for (p = r->start; p <= r->finish; p++)
997 {
998 lra_live_range_t r2;
f4eafc30 999
55a2c322
VM
1000 for (r2 = start_point_ranges[p];
1001 r2 != NULL;
1002 r2 = r2->start_next)
1003 if (r2->regno >= lra_constraint_new_regno_start)
1004 sparseset_set_bit (live_range_reload_inheritance_pseudos,
1005 r2->regno);
1006 }
1007 }
1008 }
295d875c 1009 n = 0;
88def637 1010 if (sparseset_cardinality (live_range_reload_inheritance_pseudos)
bb750f4f 1011 <= (unsigned)LRA_MAX_CONSIDERED_RELOAD_PSEUDOS)
88def637
VM
1012 EXECUTE_IF_SET_IN_SPARSESET (live_range_reload_inheritance_pseudos,
1013 reload_regno)
1014 if ((int) reload_regno != regno
1015 && (ira_reg_classes_intersect_p
1016 [rclass][regno_allocno_class_array[reload_regno]])
1017 && live_pseudos_reg_renumber[reload_regno] < 0
9e038952 1018 && find_hard_regno_for (reload_regno, &cost, -1, first_p) < 0)
88def637 1019 sorted_reload_pseudos[n++] = reload_regno;
295d875c
VM
1020 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
1021 {
1022 update_lives (spill_regno, true);
1023 if (lra_dump_file != NULL)
1024 fprintf (lra_dump_file, " spill %d(freq=%d)",
1025 spill_regno, lra_reg_info[spill_regno].freq);
1026 }
9e038952 1027 hard_regno = find_hard_regno_for (regno, &cost, -1, first_p);
55a2c322
VM
1028 if (hard_regno >= 0)
1029 {
1030 assign_temporarily (regno, hard_regno);
55a2c322
VM
1031 qsort (sorted_reload_pseudos, n, sizeof (int),
1032 reload_pseudo_compare_func);
1033 for (j = 0; j < n; j++)
1034 {
1035 reload_regno = sorted_reload_pseudos[j];
1036 lra_assert (live_pseudos_reg_renumber[reload_regno] < 0);
1037 if ((reload_hard_regno
1038 = find_hard_regno_for (reload_regno,
9e038952 1039 &reload_cost, -1, first_p)) >= 0)
55a2c322
VM
1040 {
1041 if (lra_dump_file != NULL)
1042 fprintf (lra_dump_file, " assign %d(cost=%d)",
1043 reload_regno, reload_cost);
1044 assign_temporarily (reload_regno, reload_hard_regno);
1045 cost += reload_cost;
1046 }
1047 }
1048 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
1049 {
0cc97fc5 1050 rtx_insn_list *x;
f4eafc30 1051
55a2c322
VM
1052 cost += lra_reg_info[spill_regno].freq;
1053 if (ira_reg_equiv[spill_regno].memory != NULL
1054 || ira_reg_equiv[spill_regno].constant != NULL)
1055 for (x = ira_reg_equiv[spill_regno].init_insns;
1056 x != NULL;
0cc97fc5
DM
1057 x = x->next ())
1058 cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (x->insn ()));
55a2c322 1059 }
b81a2f0d
VM
1060 /* Avoid spilling static chain pointer pseudo when non-local
1061 goto is used. */
1062 if ((! static_p && best_static_p)
1063 || (static_p == best_static_p
1064 && (best_insn_pseudos_num > insn_pseudos_num
1065 || (best_insn_pseudos_num == insn_pseudos_num
1066 && (bad_spills_num < smallest_bad_spills_num
1067 || (bad_spills_num == smallest_bad_spills_num
1068 && best_cost > cost))))))
55a2c322
VM
1069 {
1070 best_insn_pseudos_num = insn_pseudos_num;
54e915b3 1071 smallest_bad_spills_num = bad_spills_num;
b81a2f0d 1072 best_static_p = static_p;
55a2c322
VM
1073 best_cost = cost;
1074 best_hard_regno = hard_regno;
1075 bitmap_copy (&best_spill_pseudos_bitmap, &spill_pseudos_bitmap);
1076 if (lra_dump_file != NULL)
54e915b3
VM
1077 fprintf (lra_dump_file,
1078 " Now best %d(cost=%d, bad_spills=%d, insn_pseudos=%d)\n",
1079 hard_regno, cost, bad_spills_num, insn_pseudos_num);
55a2c322
VM
1080 }
1081 assign_temporarily (regno, -1);
1082 for (j = 0; j < n; j++)
1083 {
1084 reload_regno = sorted_reload_pseudos[j];
1085 if (live_pseudos_reg_renumber[reload_regno] >= 0)
1086 assign_temporarily (reload_regno, -1);
1087 }
1088 }
1089 if (lra_dump_file != NULL)
1090 fprintf (lra_dump_file, "\n");
1091 /* Restore the live hard reg pseudo info for spilled pseudos. */
1092 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
1093 update_lives (spill_regno, false);
1094 fail:
1095 ;
1096 }
1097 /* Spill: */
1098 EXECUTE_IF_SET_IN_BITMAP (&best_spill_pseudos_bitmap, 0, spill_regno, bi)
1099 {
f54437d5
VM
1100 if ((int) spill_regno >= lra_constraint_new_regno_start)
1101 former_reload_pseudo_spill_p = true;
55a2c322
VM
1102 if (lra_dump_file != NULL)
1103 fprintf (lra_dump_file, " Spill %sr%d(hr=%d, freq=%d) for r%d\n",
2b778c9d 1104 pseudo_prefix_title (spill_regno),
55a2c322
VM
1105 spill_regno, reg_renumber[spill_regno],
1106 lra_reg_info[spill_regno].freq, regno);
1107 update_lives (spill_regno, true);
1108 lra_setup_reg_renumber (spill_regno, -1, false);
1109 }
1110 bitmap_ior_into (spilled_pseudo_bitmap, &best_spill_pseudos_bitmap);
1111 return best_hard_regno;
1112}
1113
1114/* Assign HARD_REGNO to REGNO. */
1115static void
1116assign_hard_regno (int hard_regno, int regno)
1117{
1118 int i;
1119
1120 lra_assert (hard_regno >= 0);
1121 lra_setup_reg_renumber (regno, hard_regno, true);
1122 update_lives (regno, false);
1123 for (i = 0;
ad474626 1124 i < hard_regno_nregs (hard_regno, lra_reg_info[regno].biggest_mode);
55a2c322
VM
1125 i++)
1126 df_set_regs_ever_live (hard_regno + i, true);
1127}
1128
1129/* Array used for sorting different pseudos. */
1130static int *sorted_pseudos;
1131
1132/* The constraints pass is allowed to create equivalences between
1133 pseudos that make the current allocation "incorrect" (in the sense
1134 that pseudos are assigned to hard registers from their own conflict
1135 sets). The global variable lra_risky_transformations_p says
1136 whether this might have happened.
1137
1138 Process pseudos assigned to hard registers (less frequently used
1139 first), spill if a conflict is found, and mark the spilled pseudos
1140 in SPILLED_PSEUDO_BITMAP. Set up LIVE_HARD_REG_PSEUDOS from
1141 pseudos, assigned to hard registers. */
1142static void
1143setup_live_pseudos_and_spill_after_risky_transforms (bitmap
1144 spilled_pseudo_bitmap)
1145{
7e4d17a8 1146 int p, i, j, n, regno, hard_regno, biggest_nregs, nregs_diff;
55a2c322 1147 unsigned int k, conflict_regno;
73ca989c
RS
1148 poly_int64 offset;
1149 int val;
55a2c322 1150 HARD_REG_SET conflict_set;
7e4d17a8 1151 machine_mode mode, biggest_mode;
55a2c322
VM
1152 lra_live_range_t r;
1153 bitmap_iterator bi;
1154 int max_regno = max_reg_num ();
1155
1156 if (! lra_risky_transformations_p)
1157 {
1158 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1159 if (reg_renumber[i] >= 0 && lra_reg_info[i].nrefs > 0)
1160 update_lives (i, false);
1161 return;
1162 }
1163 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
bcb21886
KY
1164 if ((pic_offset_table_rtx == NULL_RTX
1165 || i != (int) REGNO (pic_offset_table_rtx))
7e4d17a8
VM
1166 && (hard_regno = reg_renumber[i]) >= 0 && lra_reg_info[i].nrefs > 0)
1167 {
1168 biggest_mode = lra_reg_info[i].biggest_mode;
1169 biggest_nregs = hard_regno_nregs (hard_regno, biggest_mode);
1170 nregs_diff = (biggest_nregs
1171 - hard_regno_nregs (hard_regno, PSEUDO_REGNO_MODE (i)));
1172 enum reg_class rclass = lra_get_allocno_class (i);
1173
4321da7b
VM
1174 if ((WORDS_BIG_ENDIAN
1175 && (hard_regno - nregs_diff < 0
1176 || !TEST_HARD_REG_BIT (reg_class_contents[rclass],
1177 hard_regno - nregs_diff)))
1178 || (!WORDS_BIG_ENDIAN
1179 && (hard_regno + nregs_diff >= FIRST_PSEUDO_REGISTER
1180 || !TEST_HARD_REG_BIT (reg_class_contents[rclass],
1181 hard_regno + nregs_diff))))
7e4d17a8
VM
1182 {
1183 /* Hard registers of paradoxical sub-registers are out of
1184 range of pseudo register class. Spill the pseudo. */
1185 reg_renumber[i] = -1;
1186 continue;
1187 }
1188 sorted_pseudos[n++] = i;
1189 }
55a2c322 1190 qsort (sorted_pseudos, n, sizeof (int), pseudo_compare_func);
bcb21886
KY
1191 if (pic_offset_table_rtx != NULL_RTX
1192 && (regno = REGNO (pic_offset_table_rtx)) >= FIRST_PSEUDO_REGISTER
1193 && reg_renumber[regno] >= 0 && lra_reg_info[regno].nrefs > 0)
1194 sorted_pseudos[n++] = regno;
55a2c322
VM
1195 for (i = n - 1; i >= 0; i--)
1196 {
1197 regno = sorted_pseudos[i];
1198 hard_regno = reg_renumber[regno];
1199 lra_assert (hard_regno >= 0);
1200 mode = lra_reg_info[regno].biggest_mode;
1201 sparseset_clear (live_range_hard_reg_pseudos);
1202 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
1203 {
1204 EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[r->start], 0, k, bi)
1205 sparseset_set_bit (live_range_hard_reg_pseudos, k);
1206 for (p = r->start + 1; p <= r->finish; p++)
1207 {
1208 lra_live_range_t r2;
f4eafc30 1209
55a2c322
VM
1210 for (r2 = start_point_ranges[p];
1211 r2 != NULL;
1212 r2 = r2->start_next)
1213 if (live_pseudos_reg_renumber[r2->regno] >= 0)
1214 sparseset_set_bit (live_range_hard_reg_pseudos, r2->regno);
1215 }
1216 }
6576d245 1217 conflict_set = lra_no_alloc_regs;
44942965 1218 conflict_set |= lra_reg_info[regno].conflict_hard_regs;
55a2c322 1219 val = lra_reg_info[regno].val;
d70a81dd 1220 offset = lra_reg_info[regno].offset;
55a2c322 1221 EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos, conflict_regno)
d70a81dd 1222 if (!lra_reg_val_equal_p (conflict_regno, val, offset)
55a2c322
VM
1223 /* If it is multi-register pseudos they should start on
1224 the same hard register. */
1225 || hard_regno != reg_renumber[conflict_regno])
15961e4a
VM
1226 {
1227 int conflict_hard_regno = reg_renumber[conflict_regno];
7e4d17a8
VM
1228
1229 biggest_mode = lra_reg_info[conflict_regno].biggest_mode;
1230 biggest_nregs = hard_regno_nregs (conflict_hard_regno,
1231 biggest_mode);
1232 nregs_diff
ad474626
RS
1233 = (biggest_nregs
1234 - hard_regno_nregs (conflict_hard_regno,
1235 PSEUDO_REGNO_MODE (conflict_regno)));
15961e4a
VM
1236 add_to_hard_reg_set (&conflict_set,
1237 biggest_mode,
1238 conflict_hard_regno
1239 - (WORDS_BIG_ENDIAN ? nregs_diff : 0));
1240 }
55a2c322
VM
1241 if (! overlaps_hard_reg_set_p (conflict_set, mode, hard_regno))
1242 {
1243 update_lives (regno, false);
1244 continue;
1245 }
1246 bitmap_set_bit (spilled_pseudo_bitmap, regno);
1247 for (j = 0;
ad474626 1248 j < hard_regno_nregs (hard_regno, PSEUDO_REGNO_MODE (regno));
55a2c322
VM
1249 j++)
1250 lra_hard_reg_usage[hard_regno + j] -= lra_reg_info[regno].freq;
1251 reg_renumber[regno] = -1;
f54437d5
VM
1252 if (regno >= lra_constraint_new_regno_start)
1253 former_reload_pseudo_spill_p = true;
55a2c322
VM
1254 if (lra_dump_file != NULL)
1255 fprintf (lra_dump_file, " Spill r%d after risky transformations\n",
1256 regno);
1257 }
1258}
1259
1260/* Improve allocation by assigning the same hard regno of inheritance
1261 pseudos to the connected pseudos. We need this because inheritance
1262 pseudos are allocated after reload pseudos in the thread and when
1263 we assign a hard register to a reload pseudo we don't know yet that
1264 the connected inheritance pseudos can get the same hard register.
1265 Add pseudos with changed allocation to bitmap CHANGED_PSEUDOS. */
1266static void
1267improve_inheritance (bitmap changed_pseudos)
1268{
1269 unsigned int k;
1270 int regno, another_regno, hard_regno, another_hard_regno, cost, i, n;
1271 lra_copy_t cp, next_cp;
1272 bitmap_iterator bi;
1273
8e3a4869
VM
1274 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
1275 return;
55a2c322
VM
1276 n = 0;
1277 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, k, bi)
1278 if (reg_renumber[k] >= 0 && lra_reg_info[k].nrefs != 0)
1279 sorted_pseudos[n++] = k;
1280 qsort (sorted_pseudos, n, sizeof (int), pseudo_compare_func);
1281 for (i = 0; i < n; i++)
1282 {
1283 regno = sorted_pseudos[i];
1284 hard_regno = reg_renumber[regno];
1285 lra_assert (hard_regno >= 0);
1286 for (cp = lra_reg_info[regno].copies; cp != NULL; cp = next_cp)
1287 {
1288 if (cp->regno1 == regno)
1289 {
1290 next_cp = cp->regno1_next;
1291 another_regno = cp->regno2;
1292 }
1293 else if (cp->regno2 == regno)
1294 {
1295 next_cp = cp->regno2_next;
1296 another_regno = cp->regno1;
1297 }
1298 else
1299 gcc_unreachable ();
1300 /* Don't change reload pseudo allocation. It might have
1301 this allocation for a purpose and changing it can result
1302 in LRA cycling. */
1303 if ((another_regno < lra_constraint_new_regno_start
1304 || bitmap_bit_p (&lra_inheritance_pseudos, another_regno))
1305 && (another_hard_regno = reg_renumber[another_regno]) >= 0
1306 && another_hard_regno != hard_regno)
1307 {
1308 if (lra_dump_file != NULL)
1309 fprintf
1310 (lra_dump_file,
1311 " Improving inheritance for %d(%d) and %d(%d)...\n",
1312 regno, hard_regno, another_regno, another_hard_regno);
1313 update_lives (another_regno, true);
1314 lra_setup_reg_renumber (another_regno, -1, false);
9e038952
VM
1315 if (hard_regno == find_hard_regno_for (another_regno, &cost,
1316 hard_regno, false))
55a2c322
VM
1317 assign_hard_regno (hard_regno, another_regno);
1318 else
1319 assign_hard_regno (another_hard_regno, another_regno);
1320 bitmap_set_bit (changed_pseudos, another_regno);
1321 }
1322 }
1323 }
1324}
1325
1326
1327/* Bitmap finally containing all pseudos spilled on this assignment
1328 pass. */
1329static bitmap_head all_spilled_pseudos;
1330/* All pseudos whose allocation was changed. */
1331static bitmap_head changed_pseudo_bitmap;
1332
9e038952
VM
1333
1334/* Add to LIVE_RANGE_HARD_REG_PSEUDOS all pseudos conflicting with
1335 REGNO and whose hard regs can be assigned to REGNO. */
1336static void
1337find_all_spills_for (int regno)
1338{
1339 int p;
1340 lra_live_range_t r;
1341 unsigned int k;
1342 bitmap_iterator bi;
1343 enum reg_class rclass;
1344 bool *rclass_intersect_p;
1345
1346 rclass = regno_allocno_class_array[regno];
1347 rclass_intersect_p = ira_reg_classes_intersect_p[rclass];
1348 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
1349 {
1350 EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[r->start], 0, k, bi)
1351 if (rclass_intersect_p[regno_allocno_class_array[k]])
1352 sparseset_set_bit (live_range_hard_reg_pseudos, k);
1353 for (p = r->start + 1; p <= r->finish; p++)
1354 {
1355 lra_live_range_t r2;
1356
1357 for (r2 = start_point_ranges[p];
1358 r2 != NULL;
1359 r2 = r2->start_next)
1360 {
1361 if (live_pseudos_reg_renumber[r2->regno] >= 0
6027ea4c
VM
1362 && ! sparseset_bit_p (live_range_hard_reg_pseudos, r2->regno)
1363 && rclass_intersect_p[regno_allocno_class_array[r2->regno]]
1364 && ((int) r2->regno < lra_constraint_new_regno_start
1365 || bitmap_bit_p (&lra_inheritance_pseudos, r2->regno)
1366 || bitmap_bit_p (&lra_split_regs, r2->regno)
1367 || bitmap_bit_p (&lra_optional_reload_pseudos, r2->regno)
1368 /* There is no sense to consider another reload
1369 pseudo if it has the same class. */
1370 || regno_allocno_class_array[r2->regno] != rclass))
9e038952
VM
1371 sparseset_set_bit (live_range_hard_reg_pseudos, r2->regno);
1372 }
1373 }
1374 }
1375}
1376
6027ea4c
VM
1377/* Assign hard registers to reload pseudos and other pseudos. Return
1378 true if we was not able to assign hard registers to all reload
1379 pseudos. */
1380static bool
55a2c322
VM
1381assign_by_spills (void)
1382{
6027ea4c 1383 int i, n, nfails, iter, regno, regno2, hard_regno, cost;
8a8330b7 1384 rtx restore_rtx;
55a2c322 1385 bitmap_head changed_insns, do_not_assign_nonreload_pseudos;
9e038952 1386 unsigned int u, conflict_regno;
55a2c322 1387 bitmap_iterator bi;
6027ea4c 1388 bool reload_p, fails_p = false;
55a2c322
VM
1389 int max_regno = max_reg_num ();
1390
1391 for (n = 0, i = lra_constraint_new_regno_start; i < max_regno; i++)
1392 if (reg_renumber[i] < 0 && lra_reg_info[i].nrefs != 0
1393 && regno_allocno_class_array[i] != NO_REGS)
1394 sorted_pseudos[n++] = i;
1395 bitmap_initialize (&insn_conflict_pseudos, &reg_obstack);
1396 bitmap_initialize (&spill_pseudos_bitmap, &reg_obstack);
1397 bitmap_initialize (&best_spill_pseudos_bitmap, &reg_obstack);
1398 update_hard_regno_preference_check = XCNEWVEC (int, max_regno);
1399 curr_update_hard_regno_preference_check = 0;
1400 memset (try_hard_reg_pseudos_check, 0, sizeof (try_hard_reg_pseudos_check));
1401 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1402 bitmap_initialize (&try_hard_reg_pseudos[i], &reg_obstack);
1403 curr_pseudo_check = 0;
1404 bitmap_initialize (&changed_insns, &reg_obstack);
1405 bitmap_initialize (&non_reload_pseudos, &reg_obstack);
1406 bitmap_ior (&non_reload_pseudos, &lra_inheritance_pseudos, &lra_split_regs);
2b778c9d 1407 bitmap_ior_into (&non_reload_pseudos, &lra_subreg_reload_pseudos);
55a2c322
VM
1408 bitmap_ior_into (&non_reload_pseudos, &lra_optional_reload_pseudos);
1409 for (iter = 0; iter <= 1; iter++)
1410 {
1411 qsort (sorted_pseudos, n, sizeof (int), reload_pseudo_compare_func);
1412 nfails = 0;
1413 for (i = 0; i < n; i++)
1414 {
1415 regno = sorted_pseudos[i];
8a8330b7
VM
1416 if (reg_renumber[regno] >= 0)
1417 continue;
55a2c322
VM
1418 if (lra_dump_file != NULL)
1419 fprintf (lra_dump_file, " Assigning to %d "
1420 "(cl=%s, orig=%d, freq=%d, tfirst=%d, tfreq=%d)...\n",
1421 regno, reg_class_names[regno_allocno_class_array[regno]],
1422 ORIGINAL_REGNO (regno_reg_rtx[regno]),
1423 lra_reg_info[regno].freq, regno_assign_info[regno].first,
1424 regno_assign_info[regno_assign_info[regno].first].freq);
9e038952 1425 hard_regno = find_hard_regno_for (regno, &cost, -1, iter == 1);
992ca0f0
VM
1426 reload_p = ! bitmap_bit_p (&non_reload_pseudos, regno);
1427 if (hard_regno < 0 && reload_p)
9e038952 1428 hard_regno = spill_for (regno, &all_spilled_pseudos, iter == 1);
55a2c322
VM
1429 if (hard_regno < 0)
1430 {
6027ea4c
VM
1431 if (reload_p) {
1432 /* Put unassigned reload pseudo first in the
1433 array. */
1434 regno2 = sorted_pseudos[nfails];
55a2c322 1435 sorted_pseudos[nfails++] = regno;
6027ea4c
VM
1436 sorted_pseudos[i] = regno2;
1437 }
55a2c322
VM
1438 }
1439 else
1440 {
1441 /* This register might have been spilled by the previous
1442 pass. Indicate that it is no longer spilled. */
1443 bitmap_clear_bit (&all_spilled_pseudos, regno);
1444 assign_hard_regno (hard_regno, regno);
992ca0f0
VM
1445 if (! reload_p)
1446 /* As non-reload pseudo assignment is changed we
1447 should reconsider insns referring for the
1448 pseudo. */
1449 bitmap_set_bit (&changed_pseudo_bitmap, regno);
55a2c322
VM
1450 }
1451 }
6027ea4c 1452 if (nfails == 0 || iter > 0)
ce940020 1453 {
6027ea4c 1454 fails_p = nfails != 0;
ce940020
VM
1455 break;
1456 }
67914693 1457 /* This is a very rare event. We cannot assign a hard register
9e038952
VM
1458 to reload pseudo because the hard register was assigned to
1459 another reload pseudo on a previous assignment pass. For x86
1460 example, on the 1st pass we assigned CX (although another
1461 hard register could be used for this) to reload pseudo in an
1462 insn, on the 2nd pass we need CX (and only this) hard
1463 register for a new reload pseudo in the same insn. Another
1464 possible situation may occur in assigning to multi-regs
1465 reload pseudos when hard regs pool is too fragmented even
1466 after spilling non-reload pseudos.
1467
1468 We should do something radical here to succeed. Here we
1469 spill *all* conflicting pseudos and reassign them. */
55a2c322
VM
1470 if (lra_dump_file != NULL)
1471 fprintf (lra_dump_file, " 2nd iter for reload pseudo assignments:\n");
9e038952 1472 sparseset_clear (live_range_hard_reg_pseudos);
55a2c322
VM
1473 for (i = 0; i < nfails; i++)
1474 {
1475 if (lra_dump_file != NULL)
1476 fprintf (lra_dump_file, " Reload r%d assignment failure\n",
1477 sorted_pseudos[i]);
9e038952
VM
1478 find_all_spills_for (sorted_pseudos[i]);
1479 }
1480 EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos, conflict_regno)
1481 {
1482 if ((int) conflict_regno >= lra_constraint_new_regno_start)
f54437d5
VM
1483 {
1484 sorted_pseudos[nfails++] = conflict_regno;
1485 former_reload_pseudo_spill_p = true;
1486 }
fdcfea63
VM
1487 else
1488 /* It is better to do reloads before spilling as after the
1489 spill-subpass we will reload memory instead of pseudos
1490 and this will make reusing reload pseudos more
1491 complicated. Going directly to the spill pass in such
1492 case might result in worse code performance or even LRA
1493 cycling if we have few registers. */
1494 bitmap_set_bit (&all_spilled_pseudos, conflict_regno);
9e038952
VM
1495 if (lra_dump_file != NULL)
1496 fprintf (lra_dump_file, " Spill %s r%d(hr=%d, freq=%d)\n",
1497 pseudo_prefix_title (conflict_regno), conflict_regno,
1498 reg_renumber[conflict_regno],
1499 lra_reg_info[conflict_regno].freq);
1500 update_lives (conflict_regno, true);
1501 lra_setup_reg_renumber (conflict_regno, -1, false);
55a2c322 1502 }
6027ea4c
VM
1503 if (n < nfails)
1504 n = nfails;
55a2c322
VM
1505 }
1506 improve_inheritance (&changed_pseudo_bitmap);
1507 bitmap_clear (&non_reload_pseudos);
1508 bitmap_clear (&changed_insns);
1509 if (! lra_simple_p)
1510 {
1511 /* We should not assign to original pseudos of inheritance
1512 pseudos or split pseudos if any its inheritance pseudo did
1513 not get hard register or any its split pseudo was not split
1514 because undo inheritance/split pass will extend live range of
1515 such inheritance or split pseudos. */
1516 bitmap_initialize (&do_not_assign_nonreload_pseudos, &reg_obstack);
1517 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, u, bi)
8a8330b7
VM
1518 if ((restore_rtx = lra_reg_info[u].restore_rtx) != NULL_RTX
1519 && REG_P (restore_rtx)
55a2c322
VM
1520 && reg_renumber[u] < 0
1521 && bitmap_bit_p (&lra_inheritance_pseudos, u))
8a8330b7 1522 bitmap_set_bit (&do_not_assign_nonreload_pseudos, REGNO (restore_rtx));
55a2c322 1523 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, u, bi)
8a8330b7 1524 if ((restore_rtx = lra_reg_info[u].restore_rtx) != NULL_RTX
55a2c322 1525 && reg_renumber[u] >= 0)
8a8330b7
VM
1526 {
1527 lra_assert (REG_P (restore_rtx));
1528 bitmap_set_bit (&do_not_assign_nonreload_pseudos, REGNO (restore_rtx));
1529 }
55a2c322
VM
1530 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1531 if (((i < lra_constraint_new_regno_start
1532 && ! bitmap_bit_p (&do_not_assign_nonreload_pseudos, i))
1533 || (bitmap_bit_p (&lra_inheritance_pseudos, i)
8a8330b7 1534 && lra_reg_info[i].restore_rtx != NULL_RTX)
55a2c322 1535 || (bitmap_bit_p (&lra_split_regs, i)
8a8330b7 1536 && lra_reg_info[i].restore_rtx != NULL_RTX)
2b778c9d 1537 || bitmap_bit_p (&lra_subreg_reload_pseudos, i)
55a2c322
VM
1538 || bitmap_bit_p (&lra_optional_reload_pseudos, i))
1539 && reg_renumber[i] < 0 && lra_reg_info[i].nrefs != 0
1540 && regno_allocno_class_array[i] != NO_REGS)
1541 sorted_pseudos[n++] = i;
1542 bitmap_clear (&do_not_assign_nonreload_pseudos);
1543 if (n != 0 && lra_dump_file != NULL)
1544 fprintf (lra_dump_file, " Reassigning non-reload pseudos\n");
1545 qsort (sorted_pseudos, n, sizeof (int), pseudo_compare_func);
1546 for (i = 0; i < n; i++)
1547 {
1548 regno = sorted_pseudos[i];
9e038952 1549 hard_regno = find_hard_regno_for (regno, &cost, -1, false);
55a2c322
VM
1550 if (hard_regno >= 0)
1551 {
1552 assign_hard_regno (hard_regno, regno);
992ca0f0
VM
1553 /* We change allocation for non-reload pseudo on this
1554 iteration -- mark the pseudo for invalidation of used
1555 alternatives of insns containing the pseudo. */
55a2c322
VM
1556 bitmap_set_bit (&changed_pseudo_bitmap, regno);
1557 }
9afb455c
VM
1558 else
1559 {
1560 enum reg_class rclass = lra_get_allocno_class (regno);
1561 enum reg_class spill_class;
1562
1df2287f 1563 if (targetm.spill_class == NULL
8a8330b7 1564 || lra_reg_info[regno].restore_rtx == NULL_RTX
9afb455c
VM
1565 || ! bitmap_bit_p (&lra_inheritance_pseudos, regno)
1566 || (spill_class
1567 = ((enum reg_class)
1568 targetm.spill_class
1569 ((reg_class_t) rclass,
1570 PSEUDO_REGNO_MODE (regno)))) == NO_REGS)
1571 continue;
1572 regno_allocno_class_array[regno] = spill_class;
1573 hard_regno = find_hard_regno_for (regno, &cost, -1, false);
1574 if (hard_regno < 0)
1575 regno_allocno_class_array[regno] = rclass;
1576 else
1577 {
1578 setup_reg_classes
1579 (regno, spill_class, spill_class, spill_class);
1580 assign_hard_regno (hard_regno, regno);
1581 bitmap_set_bit (&changed_pseudo_bitmap, regno);
1582 }
1583 }
55a2c322
VM
1584 }
1585 }
1586 free (update_hard_regno_preference_check);
1587 bitmap_clear (&best_spill_pseudos_bitmap);
1588 bitmap_clear (&spill_pseudos_bitmap);
1589 bitmap_clear (&insn_conflict_pseudos);
6027ea4c 1590 return fails_p;
55a2c322
VM
1591}
1592
55a2c322
VM
1593/* Entry function to assign hard registers to new reload pseudos
1594 starting with LRA_CONSTRAINT_NEW_REGNO_START (by possible spilling
1595 of old pseudos) and possibly to the old pseudos. The function adds
1596 what insns to process for the next constraint pass. Those are all
1597 insns who contains non-reload and non-inheritance pseudos with
1598 changed allocation.
1599
1600 Return true if we did not spill any non-reload and non-inheritance
6027ea4c
VM
1601 pseudos. Set up FAILS_P if we failed to assign hard registers to
1602 all reload pseudos. */
55a2c322 1603bool
6027ea4c 1604lra_assign (bool &fails_p)
55a2c322
VM
1605{
1606 int i;
1607 unsigned int u;
1608 bitmap_iterator bi;
1609 bitmap_head insns_to_process;
1610 bool no_spills_p;
1611 int max_regno = max_reg_num ();
1612
1613 timevar_push (TV_LRA_ASSIGN);
f54437d5
VM
1614 lra_assignment_iter++;
1615 if (lra_dump_file != NULL)
1616 fprintf (lra_dump_file, "\n********** Assignment #%d: **********\n\n",
1617 lra_assignment_iter);
55a2c322
VM
1618 init_lives ();
1619 sorted_pseudos = XNEWVEC (int, max_regno);
1620 sorted_reload_pseudos = XNEWVEC (int, max_regno);
1621 regno_allocno_class_array = XNEWVEC (enum reg_class, max_regno);
8a8330b7 1622 regno_live_length = XNEWVEC (int, max_regno);
55a2c322 1623 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
8a8330b7
VM
1624 {
1625 int l;
1626 lra_live_range_t r;
1627
1628 regno_allocno_class_array[i] = lra_get_allocno_class (i);
1629 for (l = 0, r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
1630 l += r->finish - r->start + 1;
1631 regno_live_length[i] = l;
1632 }
f54437d5 1633 former_reload_pseudo_spill_p = false;
55a2c322
VM
1634 init_regno_assign_info ();
1635 bitmap_initialize (&all_spilled_pseudos, &reg_obstack);
1636 create_live_range_start_chains ();
1637 setup_live_pseudos_and_spill_after_risky_transforms (&all_spilled_pseudos);
11067dee
VM
1638 if (! lra_asm_error_p && flag_checking && !flag_ipa_ra)
1639 /* Check correctness of allocation for call-crossed pseudos but
1640 only when there are no asm errors as in the case of errors the
1641 asm is removed and it can result in incorrect allocation. */
10e1bdb2
TV
1642 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1643 if (lra_reg_info[i].nrefs != 0 && reg_renumber[i] >= 0
473574ee 1644 && lra_reg_info[i].call_insn
10e1bdb2
TV
1645 && overlaps_hard_reg_set_p (call_used_reg_set,
1646 PSEUDO_REGNO_MODE (i), reg_renumber[i]))
1647 gcc_unreachable ();
55a2c322
VM
1648 /* Setup insns to process on the next constraint pass. */
1649 bitmap_initialize (&changed_pseudo_bitmap, &reg_obstack);
1650 init_live_reload_and_inheritance_pseudos ();
6027ea4c 1651 fails_p = assign_by_spills ();
55a2c322
VM
1652 finish_live_reload_and_inheritance_pseudos ();
1653 bitmap_ior_into (&changed_pseudo_bitmap, &all_spilled_pseudos);
1654 no_spills_p = true;
1655 EXECUTE_IF_SET_IN_BITMAP (&all_spilled_pseudos, 0, u, bi)
1656 /* We ignore spilled pseudos created on last inheritance pass
1657 because they will be removed. */
8a8330b7 1658 if (lra_reg_info[u].restore_rtx == NULL_RTX)
55a2c322
VM
1659 {
1660 no_spills_p = false;
1661 break;
1662 }
1663 finish_live_range_start_chains ();
1664 bitmap_clear (&all_spilled_pseudos);
1665 bitmap_initialize (&insns_to_process, &reg_obstack);
1666 EXECUTE_IF_SET_IN_BITMAP (&changed_pseudo_bitmap, 0, u, bi)
1667 bitmap_ior_into (&insns_to_process, &lra_reg_info[u].insn_bitmap);
1668 bitmap_clear (&changed_pseudo_bitmap);
1669 EXECUTE_IF_SET_IN_BITMAP (&insns_to_process, 0, u, bi)
1670 {
1671 lra_push_insn_by_uid (u);
1672 /* Invalidate alternatives for insn should be processed. */
1673 lra_set_used_insn_alternative_by_uid (u, -1);
1674 }
1675 bitmap_clear (&insns_to_process);
1676 finish_regno_assign_info ();
8a8330b7 1677 free (regno_live_length);
55a2c322
VM
1678 free (regno_allocno_class_array);
1679 free (sorted_pseudos);
1680 free (sorted_reload_pseudos);
1681 finish_lives ();
1682 timevar_pop (TV_LRA_ASSIGN);
f54437d5
VM
1683 if (former_reload_pseudo_spill_p)
1684 lra_assignment_iter_after_spill++;
b6c38c69
BS
1685 /* This is conditional on flag_checking because valid code can take
1686 more than this maximum number of iteration, but at the same time
1687 the test can uncover errors in machine descriptions. */
1688 if (flag_checking
1689 && (lra_assignment_iter_after_spill
1690 > LRA_MAX_ASSIGNMENT_ITERATION_NUMBER))
f54437d5 1691 internal_error
a9c697b8 1692 ("maximum number of LRA assignment passes is achieved (%d)",
f54437d5 1693 LRA_MAX_ASSIGNMENT_ITERATION_NUMBER);
55a2c322
VM
1694 return no_spills_p;
1695}
8a8330b7 1696
6027ea4c
VM
1697/* Find start and finish insns for reload pseudo REGNO. Return true
1698 if we managed to find the expected insns. Return false,
1699 otherwise. */
1700static bool
1701find_reload_regno_insns (int regno, rtx_insn * &start, rtx_insn * &finish)
1702{
1703 unsigned int uid;
1704 bitmap_iterator bi;
1705 int n = 0;
1706 rtx_insn *prev_insn, *next_insn;
1707 rtx_insn *start_insn = NULL, *first_insn = NULL, *second_insn = NULL;
1708
1709 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
1710 {
1711 if (start_insn == NULL)
1712 start_insn = lra_insn_recog_data[uid]->insn;
1713 n++;
1714 }
1715 /* For reload pseudo we should have at most 3 insns referring for it:
1716 input/output reload insns and the original insn. */
1717 if (n > 3)
1718 return false;
1719 if (n > 1)
1720 {
1721 for (prev_insn = PREV_INSN (start_insn),
1722 next_insn = NEXT_INSN (start_insn);
1723 n != 1 && (prev_insn != NULL || next_insn != NULL); )
1724 {
1725 if (prev_insn != NULL && first_insn == NULL)
1726 {
1727 if (! bitmap_bit_p (&lra_reg_info[regno].insn_bitmap,
1728 INSN_UID (prev_insn)))
1729 prev_insn = PREV_INSN (prev_insn);
1730 else
1731 {
1732 first_insn = prev_insn;
1733 n--;
1734 }
1735 }
1736 if (next_insn != NULL && second_insn == NULL)
1737 {
1738 if (! bitmap_bit_p (&lra_reg_info[regno].insn_bitmap,
1739 INSN_UID (next_insn)))
1740 next_insn = NEXT_INSN (next_insn);
1741 else
1742 {
1743 second_insn = next_insn;
1744 n--;
1745 }
1746 }
1747 }
1748 if (n > 1)
1749 return false;
1750 }
1751 start = first_insn != NULL ? first_insn : start_insn;
1752 finish = second_insn != NULL ? second_insn : start_insn;
1753 return true;
1754}
1755
1756/* Process reload pseudos which did not get a hard reg, split a hard
1757 reg live range in live range of a reload pseudo, and then return
1758 TRUE. If we did not split a hard reg live range, report an error,
1759 and return FALSE. */
1760bool
1761lra_split_hard_reg_for (void)
1762{
7293e3f5 1763 int i, regno;
6027ea4c
VM
1764 rtx_insn *insn, *first, *last;
1765 unsigned int u;
1766 bitmap_iterator bi;
7293e3f5 1767 enum reg_class rclass;
6027ea4c
VM
1768 int max_regno = max_reg_num ();
1769 /* We did not assign hard regs to reload pseudos after two
1770 iterations. Either it's an asm and something is wrong with the
1771 constraints, or we have run out of spill registers; error out in
1772 either case. */
1773 bool asm_p = false;
7293e3f5 1774 bitmap_head failed_reload_insns, failed_reload_pseudos;
6027ea4c
VM
1775
1776 if (lra_dump_file != NULL)
1777 fprintf (lra_dump_file,
1778 "\n****** Splitting a hard reg after assignment #%d: ******\n\n",
1779 lra_assignment_iter);
7293e3f5 1780 bitmap_initialize (&failed_reload_pseudos, &reg_obstack);
003cd04c
VM
1781 bitmap_initialize (&non_reload_pseudos, &reg_obstack);
1782 bitmap_ior (&non_reload_pseudos, &lra_inheritance_pseudos, &lra_split_regs);
1783 bitmap_ior_into (&non_reload_pseudos, &lra_subreg_reload_pseudos);
1784 bitmap_ior_into (&non_reload_pseudos, &lra_optional_reload_pseudos);
7293e3f5 1785 for (i = lra_constraint_new_regno_start; i < max_regno; i++)
6027ea4c 1786 if (reg_renumber[i] < 0 && lra_reg_info[i].nrefs != 0
7293e3f5 1787 && (rclass = lra_get_allocno_class (i)) != NO_REGS
6027ea4c
VM
1788 && ! bitmap_bit_p (&non_reload_pseudos, i))
1789 {
6027ea4c
VM
1790 if (! find_reload_regno_insns (i, first, last))
1791 continue;
7293e3f5
VM
1792 if (spill_hard_reg_in_range (i, rclass, first, last))
1793 {
1794 bitmap_clear (&failed_reload_pseudos);
1795 return true;
1796 }
1797 bitmap_set_bit (&failed_reload_pseudos, i);
6027ea4c 1798 }
003cd04c 1799 bitmap_clear (&non_reload_pseudos);
6027ea4c 1800 bitmap_initialize (&failed_reload_insns, &reg_obstack);
7293e3f5 1801 EXECUTE_IF_SET_IN_BITMAP (&failed_reload_pseudos, 0, u, bi)
6027ea4c 1802 {
7293e3f5 1803 regno = u;
6027ea4c
VM
1804 bitmap_ior_into (&failed_reload_insns,
1805 &lra_reg_info[regno].insn_bitmap);
7293e3f5
VM
1806 lra_setup_reg_renumber
1807 (regno, ira_class_hard_regs[lra_get_allocno_class (regno)][0], false);
6027ea4c
VM
1808 }
1809 EXECUTE_IF_SET_IN_BITMAP (&failed_reload_insns, 0, u, bi)
1810 {
1811 insn = lra_insn_recog_data[u]->insn;
1812 if (asm_noperands (PATTERN (insn)) >= 0)
1813 {
11067dee 1814 lra_asm_error_p = asm_p = true;
6027ea4c
VM
1815 error_for_asm (insn,
1816 "%<asm%> operand has impossible constraints");
1817 /* Avoid further trouble with this insn.
1818 For asm goto, instead of fixing up all the edges
1819 just clear the template and clear input operands
1820 (asm goto doesn't have any output operands). */
1821 if (JUMP_P (insn))
1822 {
1823 rtx asm_op = extract_asm_operands (PATTERN (insn));
1824 ASM_OPERANDS_TEMPLATE (asm_op) = ggc_strdup ("");
1825 ASM_OPERANDS_INPUT_VEC (asm_op) = rtvec_alloc (0);
1826 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op) = rtvec_alloc (0);
1827 lra_update_insn_regno_info (insn);
1828 }
1829 else
1830 {
1831 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
1832 lra_set_insn_deleted (insn);
1833 }
1834 }
1835 else if (!asm_p)
1836 {
1837 error ("unable to find a register to spill");
1838 fatal_insn ("this is the insn:", insn);
1839 }
1840 }
7293e3f5
VM
1841 bitmap_clear (&failed_reload_pseudos);
1842 bitmap_clear (&failed_reload_insns);
6027ea4c
VM
1843 return false;
1844}