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