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