Merge aosp-toolchain/gcc/gcc-4_9 changes.
[official-gcc.git] / gcc-4_9-mobile / gcc / lra-assigns.c
blobb980c05f2dda7b81c50d455c759083c38fe0207d
1 /* Assign reload pseudos.
2 Copyright (C) 2010-2014 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
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.
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.
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.
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.
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.
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.
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.
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.
64 Only insns with changed allocation pseudos are processed on the
65 next constraint pass.
67 The pseudo live-ranges are used to find conflicting pseudos.
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. */
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"
83 #include "rtl-error.h"
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"
97 #include "params.h"
98 #include "lra-int.h"
100 /* Current iteration number of the pass and current iteration number
101 of the pass after the latest spill pass when any former reload
102 pseudo was spilled. */
103 int lra_assignment_iter;
104 int lra_assignment_iter_after_spill;
106 /* Flag of spilling former reload pseudos on this pass. */
107 static bool former_reload_pseudo_spill_p;
109 /* Array containing corresponding values of function
110 lra_get_allocno_class. It is used to speed up the code. */
111 static enum reg_class *regno_allocno_class_array;
113 /* Information about the thread to which a pseudo belongs. Threads are
114 a set of connected reload and inheritance pseudos with the same set of
115 available hard registers. Lone registers belong to their own threads. */
116 struct regno_assign_info
118 /* First/next pseudo of the same thread. */
119 int first, next;
120 /* Frequency of the thread (execution frequency of only reload
121 pseudos in the thread when the thread contains a reload pseudo).
122 Defined only for the first thread pseudo. */
123 int freq;
126 /* Map regno to the corresponding regno assignment info. */
127 static struct regno_assign_info *regno_assign_info;
129 /* All inherited, subreg or optional pseudos created before last spill
130 sub-pass. Such pseudos are permitted to get memory instead of hard
131 regs. */
132 static bitmap_head non_reload_pseudos;
134 /* Process a pseudo copy with execution frequency COPY_FREQ connecting
135 REGNO1 and REGNO2 to form threads. */
136 static void
137 process_copy_to_form_thread (int regno1, int regno2, int copy_freq)
139 int last, regno1_first, regno2_first;
141 lra_assert (regno1 >= lra_constraint_new_regno_start
142 && regno2 >= lra_constraint_new_regno_start);
143 regno1_first = regno_assign_info[regno1].first;
144 regno2_first = regno_assign_info[regno2].first;
145 if (regno1_first != regno2_first)
147 for (last = regno2_first;
148 regno_assign_info[last].next >= 0;
149 last = regno_assign_info[last].next)
150 regno_assign_info[last].first = regno1_first;
151 regno_assign_info[last].first = regno1_first;
152 regno_assign_info[last].next = regno_assign_info[regno1_first].next;
153 regno_assign_info[regno1_first].next = regno2_first;
154 regno_assign_info[regno1_first].freq
155 += regno_assign_info[regno2_first].freq;
157 regno_assign_info[regno1_first].freq -= 2 * copy_freq;
158 lra_assert (regno_assign_info[regno1_first].freq >= 0);
161 /* Initialize REGNO_ASSIGN_INFO and form threads. */
162 static void
163 init_regno_assign_info (void)
165 int i, regno1, regno2, max_regno = max_reg_num ();
166 lra_copy_t cp;
168 regno_assign_info = XNEWVEC (struct regno_assign_info, max_regno);
169 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
171 regno_assign_info[i].first = i;
172 regno_assign_info[i].next = -1;
173 regno_assign_info[i].freq = lra_reg_info[i].freq;
175 /* Form the threads. */
176 for (i = 0; (cp = lra_get_copy (i)) != NULL; i++)
177 if ((regno1 = cp->regno1) >= lra_constraint_new_regno_start
178 && (regno2 = cp->regno2) >= lra_constraint_new_regno_start
179 && reg_renumber[regno1] < 0 && lra_reg_info[regno1].nrefs != 0
180 && reg_renumber[regno2] < 0 && lra_reg_info[regno2].nrefs != 0
181 && (ira_class_hard_regs_num[regno_allocno_class_array[regno1]]
182 == ira_class_hard_regs_num[regno_allocno_class_array[regno2]]))
183 process_copy_to_form_thread (regno1, regno2, cp->freq);
186 /* Free REGNO_ASSIGN_INFO. */
187 static void
188 finish_regno_assign_info (void)
190 free (regno_assign_info);
193 /* The function is used to sort *reload* and *inheritance* pseudos to
194 try to assign them hard registers. We put pseudos from the same
195 thread always nearby. */
196 static int
197 reload_pseudo_compare_func (const void *v1p, const void *v2p)
199 int r1 = *(const int *) v1p, r2 = *(const int *) v2p;
200 enum reg_class cl1 = regno_allocno_class_array[r1];
201 enum reg_class cl2 = regno_allocno_class_array[r2];
202 int diff;
204 lra_assert (r1 >= lra_constraint_new_regno_start
205 && r2 >= lra_constraint_new_regno_start);
207 /* Prefer to assign reload registers with smaller classes first to
208 guarantee assignment to all reload registers. */
209 if ((diff = (ira_class_hard_regs_num[cl1]
210 - ira_class_hard_regs_num[cl2])) != 0)
211 return diff;
212 if ((diff
213 = (ira_reg_class_max_nregs[cl2][lra_reg_info[r2].biggest_mode]
214 - ira_reg_class_max_nregs[cl1][lra_reg_info[r1].biggest_mode])) != 0
215 /* The code below executes rarely as nregs == 1 in most cases.
216 So we should not worry about using faster data structures to
217 check reload pseudos. */
218 && ! bitmap_bit_p (&non_reload_pseudos, r1)
219 && ! bitmap_bit_p (&non_reload_pseudos, r2))
220 return diff;
221 if ((diff = (regno_assign_info[regno_assign_info[r2].first].freq
222 - regno_assign_info[regno_assign_info[r1].first].freq)) != 0)
223 return diff;
224 /* Allocate bigger pseudos first to avoid register file
225 fragmentation. */
226 if ((diff
227 = (ira_reg_class_max_nregs[cl2][lra_reg_info[r2].biggest_mode]
228 - ira_reg_class_max_nregs[cl1][lra_reg_info[r1].biggest_mode])) != 0)
229 return diff;
230 /* Put pseudos from the thread nearby. */
231 if ((diff = regno_assign_info[r1].first - regno_assign_info[r2].first) != 0)
232 return diff;
233 /* If regs are equally good, sort by their numbers, so that the
234 results of qsort leave nothing to chance. */
235 return r1 - r2;
238 /* The function is used to sort *non-reload* pseudos to try to assign
239 them hard registers. The order calculation is simpler than in the
240 previous function and based on the pseudo frequency usage. */
241 static int
242 pseudo_compare_func (const void *v1p, const void *v2p)
244 int r1 = *(const int *) v1p, r2 = *(const int *) v2p;
245 int diff;
247 /* Prefer to assign more frequently used registers first. */
248 if ((diff = lra_reg_info[r2].freq - lra_reg_info[r1].freq) != 0)
249 return diff;
251 /* If regs are equally good, sort by their numbers, so that the
252 results of qsort leave nothing to chance. */
253 return r1 - r2;
256 /* Arrays of size LRA_LIVE_MAX_POINT mapping a program point to the
257 pseudo live ranges with given start point. We insert only live
258 ranges of pseudos interesting for assignment purposes. They are
259 reload pseudos and pseudos assigned to hard registers. */
260 static lra_live_range_t *start_point_ranges;
262 /* Used as a flag that a live range is not inserted in the start point
263 chain. */
264 static struct lra_live_range not_in_chain_mark;
266 /* Create and set up START_POINT_RANGES. */
267 static void
268 create_live_range_start_chains (void)
270 int i, max_regno;
271 lra_live_range_t r;
273 start_point_ranges = XCNEWVEC (lra_live_range_t, lra_live_max_point);
274 max_regno = max_reg_num ();
275 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
276 if (i >= lra_constraint_new_regno_start || reg_renumber[i] >= 0)
278 for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
280 r->start_next = start_point_ranges[r->start];
281 start_point_ranges[r->start] = r;
284 else
286 for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
287 r->start_next = &not_in_chain_mark;
291 /* Insert live ranges of pseudo REGNO into start chains if they are
292 not there yet. */
293 static void
294 insert_in_live_range_start_chain (int regno)
296 lra_live_range_t r = lra_reg_info[regno].live_ranges;
298 if (r->start_next != &not_in_chain_mark)
299 return;
300 for (; r != NULL; r = r->next)
302 r->start_next = start_point_ranges[r->start];
303 start_point_ranges[r->start] = r;
307 /* Free START_POINT_RANGES. */
308 static void
309 finish_live_range_start_chains (void)
311 gcc_assert (start_point_ranges != NULL);
312 free (start_point_ranges);
313 start_point_ranges = NULL;
316 /* Map: program point -> bitmap of all pseudos living at the point and
317 assigned to hard registers. */
318 static bitmap_head *live_hard_reg_pseudos;
319 static bitmap_obstack live_hard_reg_pseudos_bitmap_obstack;
321 /* reg_renumber corresponding to pseudos marked in
322 live_hard_reg_pseudos. reg_renumber might be not matched to
323 live_hard_reg_pseudos but live_pseudos_reg_renumber always reflects
324 live_hard_reg_pseudos. */
325 static int *live_pseudos_reg_renumber;
327 /* Sparseset used to calculate living hard reg pseudos for some program
328 point range. */
329 static sparseset live_range_hard_reg_pseudos;
331 /* Sparseset used to calculate living reload/inheritance pseudos for
332 some program point range. */
333 static sparseset live_range_reload_inheritance_pseudos;
335 /* Allocate and initialize the data about living pseudos at program
336 points. */
337 static void
338 init_lives (void)
340 int i, max_regno = max_reg_num ();
342 live_range_hard_reg_pseudos = sparseset_alloc (max_regno);
343 live_range_reload_inheritance_pseudos = sparseset_alloc (max_regno);
344 live_hard_reg_pseudos = XNEWVEC (bitmap_head, lra_live_max_point);
345 bitmap_obstack_initialize (&live_hard_reg_pseudos_bitmap_obstack);
346 for (i = 0; i < lra_live_max_point; i++)
347 bitmap_initialize (&live_hard_reg_pseudos[i],
348 &live_hard_reg_pseudos_bitmap_obstack);
349 live_pseudos_reg_renumber = XNEWVEC (int, max_regno);
350 for (i = 0; i < max_regno; i++)
351 live_pseudos_reg_renumber[i] = -1;
354 /* Free the data about living pseudos at program points. */
355 static void
356 finish_lives (void)
358 sparseset_free (live_range_hard_reg_pseudos);
359 sparseset_free (live_range_reload_inheritance_pseudos);
360 free (live_hard_reg_pseudos);
361 bitmap_obstack_release (&live_hard_reg_pseudos_bitmap_obstack);
362 free (live_pseudos_reg_renumber);
365 /* Update the LIVE_HARD_REG_PSEUDOS and LIVE_PSEUDOS_REG_RENUMBER
366 entries for pseudo REGNO. Assume that the register has been
367 spilled if FREE_P, otherwise assume that it has been assigned
368 reg_renumber[REGNO] (if >= 0). We also insert the pseudo live
369 ranges in the start chains when it is assumed to be assigned to a
370 hard register because we use the chains of pseudos assigned to hard
371 registers during allocation. */
372 static void
373 update_lives (int regno, bool free_p)
375 int p;
376 lra_live_range_t r;
378 if (reg_renumber[regno] < 0)
379 return;
380 live_pseudos_reg_renumber[regno] = free_p ? -1 : reg_renumber[regno];
381 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
383 for (p = r->start; p <= r->finish; p++)
384 if (free_p)
385 bitmap_clear_bit (&live_hard_reg_pseudos[p], regno);
386 else
388 bitmap_set_bit (&live_hard_reg_pseudos[p], regno);
389 insert_in_live_range_start_chain (regno);
394 /* Sparseset used to calculate reload pseudos conflicting with a given
395 pseudo when we are trying to find a hard register for the given
396 pseudo. */
397 static sparseset conflict_reload_and_inheritance_pseudos;
399 /* Map: program point -> bitmap of all reload and inheritance pseudos
400 living at the point. */
401 static bitmap_head *live_reload_and_inheritance_pseudos;
402 static bitmap_obstack live_reload_and_inheritance_pseudos_bitmap_obstack;
404 /* Allocate and initialize data about living reload pseudos at any
405 given program point. */
406 static void
407 init_live_reload_and_inheritance_pseudos (void)
409 int i, p, max_regno = max_reg_num ();
410 lra_live_range_t r;
412 conflict_reload_and_inheritance_pseudos = sparseset_alloc (max_regno);
413 live_reload_and_inheritance_pseudos = XNEWVEC (bitmap_head, lra_live_max_point);
414 bitmap_obstack_initialize (&live_reload_and_inheritance_pseudos_bitmap_obstack);
415 for (p = 0; p < lra_live_max_point; p++)
416 bitmap_initialize (&live_reload_and_inheritance_pseudos[p],
417 &live_reload_and_inheritance_pseudos_bitmap_obstack);
418 for (i = lra_constraint_new_regno_start; i < max_regno; i++)
420 for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
421 for (p = r->start; p <= r->finish; p++)
422 bitmap_set_bit (&live_reload_and_inheritance_pseudos[p], i);
426 /* Finalize data about living reload pseudos at any given program
427 point. */
428 static void
429 finish_live_reload_and_inheritance_pseudos (void)
431 sparseset_free (conflict_reload_and_inheritance_pseudos);
432 free (live_reload_and_inheritance_pseudos);
433 bitmap_obstack_release (&live_reload_and_inheritance_pseudos_bitmap_obstack);
436 /* The value used to check that cost of given hard reg is really
437 defined currently. */
438 static int curr_hard_regno_costs_check = 0;
439 /* Array used to check that cost of the corresponding hard reg (the
440 array element index) is really defined currently. */
441 static int hard_regno_costs_check[FIRST_PSEUDO_REGISTER];
442 /* The current costs of allocation of hard regs. Defined only if the
443 value of the corresponding element of the previous array is equal to
444 CURR_HARD_REGNO_COSTS_CHECK. */
445 static int hard_regno_costs[FIRST_PSEUDO_REGISTER];
447 /* Adjust cost of HARD_REGNO by INCR. Reset the cost first if it is
448 not defined yet. */
449 static inline void
450 adjust_hard_regno_cost (int hard_regno, int incr)
452 if (hard_regno_costs_check[hard_regno] != curr_hard_regno_costs_check)
453 hard_regno_costs[hard_regno] = 0;
454 hard_regno_costs_check[hard_regno] = curr_hard_regno_costs_check;
455 hard_regno_costs[hard_regno] += incr;
458 /* If any insn referencing the REGNO is used in a BB marked as
459 BB_FP_IS_FREE, LRA could allocate fp to this REGNO.
461 If REGNO is used in call insn, it is not allowed to assign
462 fp to the regno -- this is to avoid call (*fp). Or else after
463 framepointer shrinkwrapping transformation, the value of call
464 target register will be wrongly changed by fp setting of
465 frame address. */
467 static bool
468 fp_is_allowed_p (int regno)
470 unsigned int uid;
471 bitmap_iterator bi;
472 basic_block bb;
473 bool allowed = false;
475 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
477 rtx insn;
478 insn = lra_insn_recog_data[uid]->insn;
479 if (insn && !DEBUG_INSN_P (insn))
481 if (CALL_P (insn))
482 return false;
483 bb = BLOCK_FOR_INSN (insn);
484 if (bb->flags & BB_FP_IS_FREE)
485 allowed = true;
488 return allowed;
491 /* Try to find a free hard register for pseudo REGNO. Return the
492 hard register on success and set *COST to the cost of using
493 that register. (If several registers have equal cost, the one with
494 the highest priority wins.) Return -1 on failure.
496 If FIRST_P, return the first available hard reg ignoring other
497 criteria, e.g. allocation cost. This approach results in less hard
498 reg pool fragmentation and permit to allocate hard regs to reload
499 pseudos in complicated situations where pseudo sizes are different.
501 If TRY_ONLY_HARD_REGNO >= 0, consider only that hard register,
502 otherwise consider all hard registers in REGNO's class. */
503 static int
504 find_hard_regno_for (int regno, int *cost, int try_only_hard_regno,
505 bool first_p)
507 HARD_REG_SET conflict_set;
508 int best_cost = INT_MAX, best_priority = INT_MIN, best_usage = INT_MAX;
509 lra_live_range_t r;
510 int p, i, j, rclass_size, best_hard_regno, priority, hard_regno;
511 int hr, conflict_hr, nregs;
512 enum machine_mode biggest_mode;
513 unsigned int k, conflict_regno;
514 int offset, val, biggest_nregs, nregs_diff;
515 enum reg_class rclass;
516 bitmap_iterator bi;
517 bool *rclass_intersect_p;
518 HARD_REG_SET impossible_start_hard_regs, available_regs;
520 COPY_HARD_REG_SET (conflict_set, lra_no_alloc_regs);
521 /* Check whether fp is allowed for this regno. */
522 if (frame_pointer_partially_needed && !fp_is_allowed_p (regno))
523 SET_HARD_REG_BIT (conflict_set, HARD_FRAME_POINTER_REGNUM);
524 rclass = regno_allocno_class_array[regno];
525 rclass_intersect_p = ira_reg_classes_intersect_p[rclass];
526 curr_hard_regno_costs_check++;
527 sparseset_clear (conflict_reload_and_inheritance_pseudos);
528 sparseset_clear (live_range_hard_reg_pseudos);
529 IOR_HARD_REG_SET (conflict_set, lra_reg_info[regno].conflict_hard_regs);
530 biggest_mode = lra_reg_info[regno].biggest_mode;
531 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
533 EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[r->start], 0, k, bi)
534 if (rclass_intersect_p[regno_allocno_class_array[k]])
535 sparseset_set_bit (live_range_hard_reg_pseudos, k);
536 EXECUTE_IF_SET_IN_BITMAP (&live_reload_and_inheritance_pseudos[r->start],
537 0, k, bi)
538 if (lra_reg_info[k].preferred_hard_regno1 >= 0
539 && live_pseudos_reg_renumber[k] < 0
540 && rclass_intersect_p[regno_allocno_class_array[k]])
541 sparseset_set_bit (conflict_reload_and_inheritance_pseudos, k);
542 for (p = r->start + 1; p <= r->finish; p++)
544 lra_live_range_t r2;
546 for (r2 = start_point_ranges[p];
547 r2 != NULL;
548 r2 = r2->start_next)
550 if (r2->regno >= lra_constraint_new_regno_start
551 && lra_reg_info[r2->regno].preferred_hard_regno1 >= 0
552 && live_pseudos_reg_renumber[r2->regno] < 0
553 && rclass_intersect_p[regno_allocno_class_array[r2->regno]])
554 sparseset_set_bit (conflict_reload_and_inheritance_pseudos,
555 r2->regno);
556 if (live_pseudos_reg_renumber[r2->regno] >= 0
557 && rclass_intersect_p[regno_allocno_class_array[r2->regno]])
558 sparseset_set_bit (live_range_hard_reg_pseudos, r2->regno);
562 if ((hard_regno = lra_reg_info[regno].preferred_hard_regno1) >= 0)
564 adjust_hard_regno_cost
565 (hard_regno, -lra_reg_info[regno].preferred_hard_regno_profit1);
566 if ((hard_regno = lra_reg_info[regno].preferred_hard_regno2) >= 0)
567 adjust_hard_regno_cost
568 (hard_regno, -lra_reg_info[regno].preferred_hard_regno_profit2);
570 #ifdef STACK_REGS
571 if (lra_reg_info[regno].no_stack_p)
572 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
573 SET_HARD_REG_BIT (conflict_set, i);
574 #endif
575 sparseset_clear_bit (conflict_reload_and_inheritance_pseudos, regno);
576 val = lra_reg_info[regno].val;
577 offset = lra_reg_info[regno].offset;
578 CLEAR_HARD_REG_SET (impossible_start_hard_regs);
579 EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos, conflict_regno)
580 if (lra_reg_val_equal_p (conflict_regno, val, offset))
582 conflict_hr = live_pseudos_reg_renumber[conflict_regno];
583 nregs = (hard_regno_nregs[conflict_hr]
584 [lra_reg_info[conflict_regno].biggest_mode]);
585 /* Remember about multi-register pseudos. For example, 2 hard
586 register pseudos can start on the same hard register but can
587 not start on HR and HR+1/HR-1. */
588 for (hr = conflict_hr + 1;
589 hr < FIRST_PSEUDO_REGISTER && hr < conflict_hr + nregs;
590 hr++)
591 SET_HARD_REG_BIT (impossible_start_hard_regs, hr);
592 for (hr = conflict_hr - 1;
593 hr >= 0 && hr + hard_regno_nregs[hr][biggest_mode] > conflict_hr;
594 hr--)
595 SET_HARD_REG_BIT (impossible_start_hard_regs, hr);
597 else
599 add_to_hard_reg_set (&conflict_set,
600 lra_reg_info[conflict_regno].biggest_mode,
601 live_pseudos_reg_renumber[conflict_regno]);
602 if (hard_reg_set_subset_p (reg_class_contents[rclass],
603 conflict_set))
604 return -1;
606 EXECUTE_IF_SET_IN_SPARSESET (conflict_reload_and_inheritance_pseudos,
607 conflict_regno)
608 if (!lra_reg_val_equal_p (conflict_regno, val, offset))
610 lra_assert (live_pseudos_reg_renumber[conflict_regno] < 0);
611 if ((hard_regno
612 = lra_reg_info[conflict_regno].preferred_hard_regno1) >= 0)
614 adjust_hard_regno_cost
615 (hard_regno,
616 lra_reg_info[conflict_regno].preferred_hard_regno_profit1);
617 if ((hard_regno
618 = lra_reg_info[conflict_regno].preferred_hard_regno2) >= 0)
619 adjust_hard_regno_cost
620 (hard_regno,
621 lra_reg_info[conflict_regno].preferred_hard_regno_profit2);
624 /* Make sure that all registers in a multi-word pseudo belong to the
625 required class. */
626 IOR_COMPL_HARD_REG_SET (conflict_set, reg_class_contents[rclass]);
627 lra_assert (rclass != NO_REGS);
628 rclass_size = ira_class_hard_regs_num[rclass];
629 best_hard_regno = -1;
630 hard_regno = ira_class_hard_regs[rclass][0];
631 biggest_nregs = hard_regno_nregs[hard_regno][biggest_mode];
632 nregs_diff = (biggest_nregs
633 - hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (regno)]);
634 COPY_HARD_REG_SET (available_regs, reg_class_contents[rclass]);
635 AND_COMPL_HARD_REG_SET (available_regs, lra_no_alloc_regs);
636 for (i = 0; i < rclass_size; i++)
638 if (try_only_hard_regno >= 0)
639 hard_regno = try_only_hard_regno;
640 else
641 hard_regno = ira_class_hard_regs[rclass][i];
642 if (! overlaps_hard_reg_set_p (conflict_set,
643 PSEUDO_REGNO_MODE (regno), hard_regno)
644 /* We can not use prohibited_class_mode_regs because it is
645 not defined for all classes. */
646 && HARD_REGNO_MODE_OK (hard_regno, PSEUDO_REGNO_MODE (regno))
647 && ! TEST_HARD_REG_BIT (impossible_start_hard_regs, hard_regno)
648 && (nregs_diff == 0
649 || (WORDS_BIG_ENDIAN
650 ? (hard_regno - nregs_diff >= 0
651 && TEST_HARD_REG_BIT (available_regs,
652 hard_regno - nregs_diff))
653 : TEST_HARD_REG_BIT (available_regs,
654 hard_regno + nregs_diff))))
656 if (hard_regno_costs_check[hard_regno]
657 != curr_hard_regno_costs_check)
659 hard_regno_costs_check[hard_regno] = curr_hard_regno_costs_check;
660 hard_regno_costs[hard_regno] = 0;
662 for (j = 0;
663 j < hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (regno)];
664 j++)
665 if ((! TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + j)
666 /* fp has no less cost than other callee saved reg if
667 frame_pointer_partially_needed is true. */
668 || (frame_pointer_partially_needed
669 && (hard_regno + j == HARD_FRAME_POINTER_REGNUM)))
670 && ! df_regs_ever_live_p (hard_regno + j))
671 /* It needs save restore. */
672 hard_regno_costs[hard_regno]
673 += (2
674 * REG_FREQ_FROM_BB (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
675 + 1);
676 priority = targetm.register_priority (hard_regno);
677 if (best_hard_regno < 0 || hard_regno_costs[hard_regno] < best_cost
678 || (hard_regno_costs[hard_regno] == best_cost
679 && (priority > best_priority
680 || (targetm.register_usage_leveling_p ()
681 && priority == best_priority
682 && best_usage > lra_hard_reg_usage[hard_regno]))))
684 best_hard_regno = hard_regno;
685 best_cost = hard_regno_costs[hard_regno];
686 best_priority = priority;
687 best_usage = lra_hard_reg_usage[hard_regno];
690 if (try_only_hard_regno >= 0 || (first_p && best_hard_regno >= 0))
691 break;
693 if (best_hard_regno >= 0)
694 *cost = best_cost - lra_reg_info[regno].freq;
695 return best_hard_regno;
698 /* Current value used for checking elements in
699 update_hard_regno_preference_check. */
700 static int curr_update_hard_regno_preference_check;
701 /* If an element value is equal to the above variable value, then the
702 corresponding regno has been processed for preference
703 propagation. */
704 static int *update_hard_regno_preference_check;
706 /* Update the preference for using HARD_REGNO for pseudos that are
707 connected directly or indirectly with REGNO. Apply divisor DIV
708 to any preference adjustments.
710 The more indirectly a pseudo is connected, the smaller its effect
711 should be. We therefore increase DIV on each "hop". */
712 static void
713 update_hard_regno_preference (int regno, int hard_regno, int div)
715 int another_regno, cost;
716 lra_copy_t cp, next_cp;
718 /* Search depth 5 seems to be enough. */
719 if (div > (1 << 5))
720 return;
721 for (cp = lra_reg_info[regno].copies; cp != NULL; cp = next_cp)
723 if (cp->regno1 == regno)
725 next_cp = cp->regno1_next;
726 another_regno = cp->regno2;
728 else if (cp->regno2 == regno)
730 next_cp = cp->regno2_next;
731 another_regno = cp->regno1;
733 else
734 gcc_unreachable ();
735 if (reg_renumber[another_regno] < 0
736 && (update_hard_regno_preference_check[another_regno]
737 != curr_update_hard_regno_preference_check))
739 update_hard_regno_preference_check[another_regno]
740 = curr_update_hard_regno_preference_check;
741 cost = cp->freq < div ? 1 : cp->freq / div;
742 lra_setup_reload_pseudo_preferenced_hard_reg
743 (another_regno, hard_regno, cost);
744 update_hard_regno_preference (another_regno, hard_regno, div * 2);
749 /* Return prefix title for pseudo REGNO. */
750 static const char *
751 pseudo_prefix_title (int regno)
753 return
754 (regno < lra_constraint_new_regno_start ? ""
755 : bitmap_bit_p (&lra_inheritance_pseudos, regno) ? "inheritance "
756 : bitmap_bit_p (&lra_split_regs, regno) ? "split "
757 : bitmap_bit_p (&lra_optional_reload_pseudos, regno) ? "optional reload "
758 : bitmap_bit_p (&lra_subreg_reload_pseudos, regno) ? "subreg reload "
759 : "reload ");
762 /* Update REG_RENUMBER and other pseudo preferences by assignment of
763 HARD_REGNO to pseudo REGNO and print about it if PRINT_P. */
764 void
765 lra_setup_reg_renumber (int regno, int hard_regno, bool print_p)
767 int i, hr;
769 /* We can not just reassign hard register. */
770 lra_assert (hard_regno < 0 || reg_renumber[regno] < 0);
771 if ((hr = hard_regno) < 0)
772 hr = reg_renumber[regno];
773 reg_renumber[regno] = hard_regno;
774 lra_assert (hr >= 0);
775 for (i = 0; i < hard_regno_nregs[hr][PSEUDO_REGNO_MODE (regno)]; i++)
776 if (hard_regno < 0)
777 lra_hard_reg_usage[hr + i] -= lra_reg_info[regno].freq;
778 else
779 lra_hard_reg_usage[hr + i] += lra_reg_info[regno].freq;
780 if (print_p && lra_dump_file != NULL)
781 fprintf (lra_dump_file, " Assign %d to %sr%d (freq=%d)\n",
782 reg_renumber[regno], pseudo_prefix_title (regno),
783 regno, lra_reg_info[regno].freq);
784 if (hard_regno >= 0)
786 curr_update_hard_regno_preference_check++;
787 update_hard_regno_preference (regno, hard_regno, 1);
791 /* Pseudos which occur in insns containing a particular pseudo. */
792 static bitmap_head insn_conflict_pseudos;
794 /* Bitmaps used to contain spill pseudos for given pseudo hard regno
795 and best spill pseudos for given pseudo (and best hard regno). */
796 static bitmap_head spill_pseudos_bitmap, best_spill_pseudos_bitmap;
798 /* Current pseudo check for validity of elements in
799 TRY_HARD_REG_PSEUDOS. */
800 static int curr_pseudo_check;
801 /* Array used for validity of elements in TRY_HARD_REG_PSEUDOS. */
802 static int try_hard_reg_pseudos_check[FIRST_PSEUDO_REGISTER];
803 /* Pseudos who hold given hard register at the considered points. */
804 static bitmap_head try_hard_reg_pseudos[FIRST_PSEUDO_REGISTER];
806 /* Set up try_hard_reg_pseudos for given program point P and class
807 RCLASS. Those are pseudos living at P and assigned to a hard
808 register of RCLASS. In other words, those are pseudos which can be
809 spilled to assign a hard register of RCLASS to a pseudo living at
810 P. */
811 static void
812 setup_try_hard_regno_pseudos (int p, enum reg_class rclass)
814 int i, hard_regno;
815 enum machine_mode mode;
816 unsigned int spill_regno;
817 bitmap_iterator bi;
819 /* Find what pseudos could be spilled. */
820 EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[p], 0, spill_regno, bi)
822 mode = PSEUDO_REGNO_MODE (spill_regno);
823 hard_regno = live_pseudos_reg_renumber[spill_regno];
824 if (overlaps_hard_reg_set_p (reg_class_contents[rclass],
825 mode, hard_regno))
827 for (i = hard_regno_nregs[hard_regno][mode] - 1; i >= 0; i--)
829 if (try_hard_reg_pseudos_check[hard_regno + i]
830 != curr_pseudo_check)
832 try_hard_reg_pseudos_check[hard_regno + i]
833 = curr_pseudo_check;
834 bitmap_clear (&try_hard_reg_pseudos[hard_regno + i]);
836 bitmap_set_bit (&try_hard_reg_pseudos[hard_regno + i],
837 spill_regno);
843 /* Assign temporarily HARD_REGNO to pseudo REGNO. Temporary
844 assignment means that we might undo the data change. */
845 static void
846 assign_temporarily (int regno, int hard_regno)
848 int p;
849 lra_live_range_t r;
851 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
853 for (p = r->start; p <= r->finish; p++)
854 if (hard_regno < 0)
855 bitmap_clear_bit (&live_hard_reg_pseudos[p], regno);
856 else
858 bitmap_set_bit (&live_hard_reg_pseudos[p], regno);
859 insert_in_live_range_start_chain (regno);
862 live_pseudos_reg_renumber[regno] = hard_regno;
865 /* Array used for sorting reload pseudos for subsequent allocation
866 after spilling some pseudo. */
867 static int *sorted_reload_pseudos;
869 /* Spill some pseudos for a reload pseudo REGNO and return hard
870 register which should be used for pseudo after spilling. The
871 function adds spilled pseudos to SPILLED_PSEUDO_BITMAP. When we
872 choose hard register (and pseudos occupying the hard registers and
873 to be spilled), we take into account not only how REGNO will
874 benefit from the spills but also how other reload pseudos not yet
875 assigned to hard registers benefit from the spills too. In very
876 rare cases, the function can fail and return -1.
878 If FIRST_P, return the first available hard reg ignoring other
879 criteria, e.g. allocation cost and cost of spilling non-reload
880 pseudos. This approach results in less hard reg pool fragmentation
881 and permit to allocate hard regs to reload pseudos in complicated
882 situations where pseudo sizes are different. */
883 static int
884 spill_for (int regno, bitmap spilled_pseudo_bitmap, bool first_p)
886 int i, j, n, p, hard_regno, best_hard_regno, cost, best_cost, rclass_size;
887 int reload_hard_regno, reload_cost;
888 enum machine_mode mode;
889 enum reg_class rclass;
890 unsigned int spill_regno, reload_regno, uid;
891 int insn_pseudos_num, best_insn_pseudos_num;
892 lra_live_range_t r;
893 bitmap_iterator bi;
895 rclass = regno_allocno_class_array[regno];
896 lra_assert (reg_renumber[regno] < 0 && rclass != NO_REGS);
897 bitmap_clear (&insn_conflict_pseudos);
898 bitmap_clear (&best_spill_pseudos_bitmap);
899 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
901 struct lra_insn_reg *ir;
903 for (ir = lra_get_insn_regs (uid); ir != NULL; ir = ir->next)
904 if (ir->regno >= FIRST_PSEUDO_REGISTER)
905 bitmap_set_bit (&insn_conflict_pseudos, ir->regno);
907 best_hard_regno = -1;
908 best_cost = INT_MAX;
909 best_insn_pseudos_num = INT_MAX;
910 rclass_size = ira_class_hard_regs_num[rclass];
911 mode = PSEUDO_REGNO_MODE (regno);
912 /* Invalidate try_hard_reg_pseudos elements. */
913 curr_pseudo_check++;
914 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
915 for (p = r->start; p <= r->finish; p++)
916 setup_try_hard_regno_pseudos (p, rclass);
917 for (i = 0; i < rclass_size; i++)
919 hard_regno = ira_class_hard_regs[rclass][i];
920 bitmap_clear (&spill_pseudos_bitmap);
921 for (j = hard_regno_nregs[hard_regno][mode] - 1; j >= 0; j--)
923 if (try_hard_reg_pseudos_check[hard_regno + j] != curr_pseudo_check)
924 continue;
925 lra_assert (!bitmap_empty_p (&try_hard_reg_pseudos[hard_regno + j]));
926 bitmap_ior_into (&spill_pseudos_bitmap,
927 &try_hard_reg_pseudos[hard_regno + j]);
929 /* Spill pseudos. */
930 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
931 if ((int) spill_regno >= lra_constraint_new_regno_start
932 && ! bitmap_bit_p (&lra_inheritance_pseudos, spill_regno)
933 && ! bitmap_bit_p (&lra_split_regs, spill_regno)
934 && ! bitmap_bit_p (&lra_subreg_reload_pseudos, spill_regno)
935 && ! bitmap_bit_p (&lra_optional_reload_pseudos, spill_regno))
936 goto fail;
937 insn_pseudos_num = 0;
938 if (lra_dump_file != NULL)
939 fprintf (lra_dump_file, " Trying %d:", hard_regno);
940 sparseset_clear (live_range_reload_inheritance_pseudos);
941 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
943 if (bitmap_bit_p (&insn_conflict_pseudos, spill_regno))
944 insn_pseudos_num++;
945 for (r = lra_reg_info[spill_regno].live_ranges;
946 r != NULL;
947 r = r->next)
949 for (p = r->start; p <= r->finish; p++)
951 lra_live_range_t r2;
953 for (r2 = start_point_ranges[p];
954 r2 != NULL;
955 r2 = r2->start_next)
956 if (r2->regno >= lra_constraint_new_regno_start)
957 sparseset_set_bit (live_range_reload_inheritance_pseudos,
958 r2->regno);
962 n = 0;
963 if (sparseset_cardinality (live_range_reload_inheritance_pseudos)
964 <= (unsigned)LRA_MAX_CONSIDERED_RELOAD_PSEUDOS)
965 EXECUTE_IF_SET_IN_SPARSESET (live_range_reload_inheritance_pseudos,
966 reload_regno)
967 if ((int) reload_regno != regno
968 && (ira_reg_classes_intersect_p
969 [rclass][regno_allocno_class_array[reload_regno]])
970 && live_pseudos_reg_renumber[reload_regno] < 0
971 && find_hard_regno_for (reload_regno, &cost, -1, first_p) < 0)
972 sorted_reload_pseudos[n++] = reload_regno;
973 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
975 update_lives (spill_regno, true);
976 if (lra_dump_file != NULL)
977 fprintf (lra_dump_file, " spill %d(freq=%d)",
978 spill_regno, lra_reg_info[spill_regno].freq);
980 hard_regno = find_hard_regno_for (regno, &cost, -1, first_p);
981 if (hard_regno >= 0)
983 assign_temporarily (regno, hard_regno);
984 qsort (sorted_reload_pseudos, n, sizeof (int),
985 reload_pseudo_compare_func);
986 for (j = 0; j < n; j++)
988 reload_regno = sorted_reload_pseudos[j];
989 lra_assert (live_pseudos_reg_renumber[reload_regno] < 0);
990 if ((reload_hard_regno
991 = find_hard_regno_for (reload_regno,
992 &reload_cost, -1, first_p)) >= 0)
994 if (lra_dump_file != NULL)
995 fprintf (lra_dump_file, " assign %d(cost=%d)",
996 reload_regno, reload_cost);
997 assign_temporarily (reload_regno, reload_hard_regno);
998 cost += reload_cost;
1001 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
1003 rtx x;
1005 cost += lra_reg_info[spill_regno].freq;
1006 if (ira_reg_equiv[spill_regno].memory != NULL
1007 || ira_reg_equiv[spill_regno].constant != NULL)
1008 for (x = ira_reg_equiv[spill_regno].init_insns;
1009 x != NULL;
1010 x = XEXP (x, 1))
1011 cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (XEXP (x, 0)));
1013 if (best_insn_pseudos_num > insn_pseudos_num
1014 || (best_insn_pseudos_num == insn_pseudos_num
1015 && best_cost > cost))
1017 best_insn_pseudos_num = insn_pseudos_num;
1018 best_cost = cost;
1019 best_hard_regno = hard_regno;
1020 bitmap_copy (&best_spill_pseudos_bitmap, &spill_pseudos_bitmap);
1021 if (lra_dump_file != NULL)
1022 fprintf (lra_dump_file, " Now best %d(cost=%d)\n",
1023 hard_regno, cost);
1025 assign_temporarily (regno, -1);
1026 for (j = 0; j < n; j++)
1028 reload_regno = sorted_reload_pseudos[j];
1029 if (live_pseudos_reg_renumber[reload_regno] >= 0)
1030 assign_temporarily (reload_regno, -1);
1033 if (lra_dump_file != NULL)
1034 fprintf (lra_dump_file, "\n");
1035 /* Restore the live hard reg pseudo info for spilled pseudos. */
1036 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
1037 update_lives (spill_regno, false);
1038 fail:
1041 /* Spill: */
1042 EXECUTE_IF_SET_IN_BITMAP (&best_spill_pseudos_bitmap, 0, spill_regno, bi)
1044 if ((int) spill_regno >= lra_constraint_new_regno_start)
1045 former_reload_pseudo_spill_p = true;
1046 if (lra_dump_file != NULL)
1047 fprintf (lra_dump_file, " Spill %sr%d(hr=%d, freq=%d) for r%d\n",
1048 pseudo_prefix_title (spill_regno),
1049 spill_regno, reg_renumber[spill_regno],
1050 lra_reg_info[spill_regno].freq, regno);
1051 update_lives (spill_regno, true);
1052 lra_setup_reg_renumber (spill_regno, -1, false);
1054 bitmap_ior_into (spilled_pseudo_bitmap, &best_spill_pseudos_bitmap);
1055 return best_hard_regno;
1058 /* Assign HARD_REGNO to REGNO. */
1059 static void
1060 assign_hard_regno (int hard_regno, int regno)
1062 int i;
1064 lra_assert (hard_regno >= 0);
1065 lra_setup_reg_renumber (regno, hard_regno, true);
1066 update_lives (regno, false);
1067 for (i = 0;
1068 i < hard_regno_nregs[hard_regno][lra_reg_info[regno].biggest_mode];
1069 i++)
1070 df_set_regs_ever_live (hard_regno + i, true);
1073 /* Array used for sorting different pseudos. */
1074 static int *sorted_pseudos;
1076 /* The constraints pass is allowed to create equivalences between
1077 pseudos that make the current allocation "incorrect" (in the sense
1078 that pseudos are assigned to hard registers from their own conflict
1079 sets). The global variable lra_risky_transformations_p says
1080 whether this might have happened.
1082 Process pseudos assigned to hard registers (less frequently used
1083 first), spill if a conflict is found, and mark the spilled pseudos
1084 in SPILLED_PSEUDO_BITMAP. Set up LIVE_HARD_REG_PSEUDOS from
1085 pseudos, assigned to hard registers. */
1086 static void
1087 setup_live_pseudos_and_spill_after_risky_transforms (bitmap
1088 spilled_pseudo_bitmap)
1090 int p, i, j, n, regno, hard_regno;
1091 unsigned int k, conflict_regno;
1092 int val, offset;
1093 HARD_REG_SET conflict_set;
1094 enum machine_mode mode;
1095 lra_live_range_t r;
1096 bitmap_iterator bi;
1097 int max_regno = max_reg_num ();
1099 if (! lra_risky_transformations_p)
1101 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1102 if (reg_renumber[i] >= 0 && lra_reg_info[i].nrefs > 0)
1103 update_lives (i, false);
1104 return;
1106 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1107 if (reg_renumber[i] >= 0 && lra_reg_info[i].nrefs > 0)
1108 sorted_pseudos[n++] = i;
1109 qsort (sorted_pseudos, n, sizeof (int), pseudo_compare_func);
1110 for (i = n - 1; i >= 0; i--)
1112 regno = sorted_pseudos[i];
1113 hard_regno = reg_renumber[regno];
1114 lra_assert (hard_regno >= 0);
1115 mode = lra_reg_info[regno].biggest_mode;
1116 sparseset_clear (live_range_hard_reg_pseudos);
1117 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
1119 EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[r->start], 0, k, bi)
1120 sparseset_set_bit (live_range_hard_reg_pseudos, k);
1121 for (p = r->start + 1; p <= r->finish; p++)
1123 lra_live_range_t r2;
1125 for (r2 = start_point_ranges[p];
1126 r2 != NULL;
1127 r2 = r2->start_next)
1128 if (live_pseudos_reg_renumber[r2->regno] >= 0)
1129 sparseset_set_bit (live_range_hard_reg_pseudos, r2->regno);
1132 COPY_HARD_REG_SET (conflict_set, lra_no_alloc_regs);
1133 IOR_HARD_REG_SET (conflict_set, lra_reg_info[regno].conflict_hard_regs);
1134 val = lra_reg_info[regno].val;
1135 offset = lra_reg_info[regno].offset;
1136 EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos, conflict_regno)
1137 if (!lra_reg_val_equal_p (conflict_regno, val, offset)
1138 /* If it is multi-register pseudos they should start on
1139 the same hard register. */
1140 || hard_regno != reg_renumber[conflict_regno])
1141 add_to_hard_reg_set (&conflict_set,
1142 lra_reg_info[conflict_regno].biggest_mode,
1143 reg_renumber[conflict_regno]);
1144 if (! overlaps_hard_reg_set_p (conflict_set, mode, hard_regno))
1146 update_lives (regno, false);
1147 continue;
1149 bitmap_set_bit (spilled_pseudo_bitmap, regno);
1150 for (j = 0;
1151 j < hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (regno)];
1152 j++)
1153 lra_hard_reg_usage[hard_regno + j] -= lra_reg_info[regno].freq;
1154 reg_renumber[regno] = -1;
1155 if (regno >= lra_constraint_new_regno_start)
1156 former_reload_pseudo_spill_p = true;
1157 if (lra_dump_file != NULL)
1158 fprintf (lra_dump_file, " Spill r%d after risky transformations\n",
1159 regno);
1163 /* Improve allocation by assigning the same hard regno of inheritance
1164 pseudos to the connected pseudos. We need this because inheritance
1165 pseudos are allocated after reload pseudos in the thread and when
1166 we assign a hard register to a reload pseudo we don't know yet that
1167 the connected inheritance pseudos can get the same hard register.
1168 Add pseudos with changed allocation to bitmap CHANGED_PSEUDOS. */
1169 static void
1170 improve_inheritance (bitmap changed_pseudos)
1172 unsigned int k;
1173 int regno, another_regno, hard_regno, another_hard_regno, cost, i, n;
1174 lra_copy_t cp, next_cp;
1175 bitmap_iterator bi;
1177 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
1178 return;
1179 n = 0;
1180 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, k, bi)
1181 if (reg_renumber[k] >= 0 && lra_reg_info[k].nrefs != 0)
1182 sorted_pseudos[n++] = k;
1183 qsort (sorted_pseudos, n, sizeof (int), pseudo_compare_func);
1184 for (i = 0; i < n; i++)
1186 regno = sorted_pseudos[i];
1187 hard_regno = reg_renumber[regno];
1188 lra_assert (hard_regno >= 0);
1189 for (cp = lra_reg_info[regno].copies; cp != NULL; cp = next_cp)
1191 if (cp->regno1 == regno)
1193 next_cp = cp->regno1_next;
1194 another_regno = cp->regno2;
1196 else if (cp->regno2 == regno)
1198 next_cp = cp->regno2_next;
1199 another_regno = cp->regno1;
1201 else
1202 gcc_unreachable ();
1203 /* Don't change reload pseudo allocation. It might have
1204 this allocation for a purpose and changing it can result
1205 in LRA cycling. */
1206 if ((another_regno < lra_constraint_new_regno_start
1207 || bitmap_bit_p (&lra_inheritance_pseudos, another_regno))
1208 && (another_hard_regno = reg_renumber[another_regno]) >= 0
1209 && another_hard_regno != hard_regno)
1211 if (lra_dump_file != NULL)
1212 fprintf
1213 (lra_dump_file,
1214 " Improving inheritance for %d(%d) and %d(%d)...\n",
1215 regno, hard_regno, another_regno, another_hard_regno);
1216 update_lives (another_regno, true);
1217 lra_setup_reg_renumber (another_regno, -1, false);
1218 if (hard_regno == find_hard_regno_for (another_regno, &cost,
1219 hard_regno, false))
1220 assign_hard_regno (hard_regno, another_regno);
1221 else
1222 assign_hard_regno (another_hard_regno, another_regno);
1223 bitmap_set_bit (changed_pseudos, another_regno);
1230 /* Bitmap finally containing all pseudos spilled on this assignment
1231 pass. */
1232 static bitmap_head all_spilled_pseudos;
1233 /* All pseudos whose allocation was changed. */
1234 static bitmap_head changed_pseudo_bitmap;
1237 /* Add to LIVE_RANGE_HARD_REG_PSEUDOS all pseudos conflicting with
1238 REGNO and whose hard regs can be assigned to REGNO. */
1239 static void
1240 find_all_spills_for (int regno)
1242 int p;
1243 lra_live_range_t r;
1244 unsigned int k;
1245 bitmap_iterator bi;
1246 enum reg_class rclass;
1247 bool *rclass_intersect_p;
1249 rclass = regno_allocno_class_array[regno];
1250 rclass_intersect_p = ira_reg_classes_intersect_p[rclass];
1251 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
1253 EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[r->start], 0, k, bi)
1254 if (rclass_intersect_p[regno_allocno_class_array[k]])
1255 sparseset_set_bit (live_range_hard_reg_pseudos, k);
1256 for (p = r->start + 1; p <= r->finish; p++)
1258 lra_live_range_t r2;
1260 for (r2 = start_point_ranges[p];
1261 r2 != NULL;
1262 r2 = r2->start_next)
1264 if (live_pseudos_reg_renumber[r2->regno] >= 0
1265 && rclass_intersect_p[regno_allocno_class_array[r2->regno]])
1266 sparseset_set_bit (live_range_hard_reg_pseudos, r2->regno);
1272 /* Assign hard registers to reload pseudos and other pseudos. */
1273 static void
1274 assign_by_spills (void)
1276 int i, n, nfails, iter, regno, hard_regno, cost, restore_regno;
1277 rtx insn;
1278 bitmap_head changed_insns, do_not_assign_nonreload_pseudos;
1279 unsigned int u, conflict_regno;
1280 bitmap_iterator bi;
1281 bool reload_p;
1282 int max_regno = max_reg_num ();
1284 for (n = 0, i = lra_constraint_new_regno_start; i < max_regno; i++)
1285 if (reg_renumber[i] < 0 && lra_reg_info[i].nrefs != 0
1286 && regno_allocno_class_array[i] != NO_REGS)
1287 sorted_pseudos[n++] = i;
1288 bitmap_initialize (&insn_conflict_pseudos, &reg_obstack);
1289 bitmap_initialize (&spill_pseudos_bitmap, &reg_obstack);
1290 bitmap_initialize (&best_spill_pseudos_bitmap, &reg_obstack);
1291 update_hard_regno_preference_check = XCNEWVEC (int, max_regno);
1292 curr_update_hard_regno_preference_check = 0;
1293 memset (try_hard_reg_pseudos_check, 0, sizeof (try_hard_reg_pseudos_check));
1294 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1295 bitmap_initialize (&try_hard_reg_pseudos[i], &reg_obstack);
1296 curr_pseudo_check = 0;
1297 bitmap_initialize (&changed_insns, &reg_obstack);
1298 bitmap_initialize (&non_reload_pseudos, &reg_obstack);
1299 bitmap_ior (&non_reload_pseudos, &lra_inheritance_pseudos, &lra_split_regs);
1300 bitmap_ior_into (&non_reload_pseudos, &lra_subreg_reload_pseudos);
1301 bitmap_ior_into (&non_reload_pseudos, &lra_optional_reload_pseudos);
1302 for (iter = 0; iter <= 1; iter++)
1304 qsort (sorted_pseudos, n, sizeof (int), reload_pseudo_compare_func);
1305 nfails = 0;
1306 for (i = 0; i < n; i++)
1308 regno = sorted_pseudos[i];
1309 if (lra_dump_file != NULL)
1310 fprintf (lra_dump_file, " Assigning to %d "
1311 "(cl=%s, orig=%d, freq=%d, tfirst=%d, tfreq=%d)...\n",
1312 regno, reg_class_names[regno_allocno_class_array[regno]],
1313 ORIGINAL_REGNO (regno_reg_rtx[regno]),
1314 lra_reg_info[regno].freq, regno_assign_info[regno].first,
1315 regno_assign_info[regno_assign_info[regno].first].freq);
1316 hard_regno = find_hard_regno_for (regno, &cost, -1, iter == 1);
1317 reload_p = ! bitmap_bit_p (&non_reload_pseudos, regno);
1318 if (hard_regno < 0 && reload_p)
1319 hard_regno = spill_for (regno, &all_spilled_pseudos, iter == 1);
1320 if (hard_regno < 0)
1322 if (reload_p)
1323 sorted_pseudos[nfails++] = regno;
1325 else
1327 /* This register might have been spilled by the previous
1328 pass. Indicate that it is no longer spilled. */
1329 bitmap_clear_bit (&all_spilled_pseudos, regno);
1330 assign_hard_regno (hard_regno, regno);
1331 if (! reload_p)
1332 /* As non-reload pseudo assignment is changed we
1333 should reconsider insns referring for the
1334 pseudo. */
1335 bitmap_set_bit (&changed_pseudo_bitmap, regno);
1338 if (nfails == 0)
1339 break;
1340 if (iter > 0)
1342 /* We did not assign hard regs to reload pseudos after two
1343 iteration. It means something is wrong with asm insn
1344 constraints. Report it. */
1345 bool asm_p = false;
1346 bitmap_head failed_reload_insns;
1348 bitmap_initialize (&failed_reload_insns, &reg_obstack);
1349 for (i = 0; i < nfails; i++)
1351 regno = sorted_pseudos[i];
1352 bitmap_ior_into (&failed_reload_insns,
1353 &lra_reg_info[regno].insn_bitmap);
1354 /* Assign an arbitrary hard register of regno class to
1355 avoid further trouble with the asm insns. */
1356 bitmap_clear_bit (&all_spilled_pseudos, regno);
1357 assign_hard_regno
1358 (ira_class_hard_regs[regno_allocno_class_array[regno]][0],
1359 regno);
1361 EXECUTE_IF_SET_IN_BITMAP (&failed_reload_insns, 0, u, bi)
1363 insn = lra_insn_recog_data[u]->insn;
1364 if (asm_noperands (PATTERN (insn)) >= 0)
1366 asm_p = true;
1367 error_for_asm (insn,
1368 "%<asm%> operand has impossible constraints");
1369 /* Avoid further trouble with this insn.
1370 For asm goto, instead of fixing up all the edges
1371 just clear the template and clear input operands
1372 (asm goto doesn't have any output operands). */
1373 if (JUMP_P (insn))
1375 rtx asm_op = extract_asm_operands (PATTERN (insn));
1376 ASM_OPERANDS_TEMPLATE (asm_op) = ggc_strdup ("");
1377 ASM_OPERANDS_INPUT_VEC (asm_op) = rtvec_alloc (0);
1378 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op) = rtvec_alloc (0);
1379 lra_update_insn_regno_info (insn);
1381 else
1383 PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
1384 lra_set_insn_deleted (insn);
1388 lra_assert (asm_p);
1389 break;
1391 /* This is a very rare event. We can not assign a hard register
1392 to reload pseudo because the hard register was assigned to
1393 another reload pseudo on a previous assignment pass. For x86
1394 example, on the 1st pass we assigned CX (although another
1395 hard register could be used for this) to reload pseudo in an
1396 insn, on the 2nd pass we need CX (and only this) hard
1397 register for a new reload pseudo in the same insn. Another
1398 possible situation may occur in assigning to multi-regs
1399 reload pseudos when hard regs pool is too fragmented even
1400 after spilling non-reload pseudos.
1402 We should do something radical here to succeed. Here we
1403 spill *all* conflicting pseudos and reassign them. */
1404 if (lra_dump_file != NULL)
1405 fprintf (lra_dump_file, " 2nd iter for reload pseudo assignments:\n");
1406 sparseset_clear (live_range_hard_reg_pseudos);
1407 for (i = 0; i < nfails; i++)
1409 if (lra_dump_file != NULL)
1410 fprintf (lra_dump_file, " Reload r%d assignment failure\n",
1411 sorted_pseudos[i]);
1412 find_all_spills_for (sorted_pseudos[i]);
1414 EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos, conflict_regno)
1416 if ((int) conflict_regno >= lra_constraint_new_regno_start)
1418 sorted_pseudos[nfails++] = conflict_regno;
1419 former_reload_pseudo_spill_p = true;
1421 if (lra_dump_file != NULL)
1422 fprintf (lra_dump_file, " Spill %s r%d(hr=%d, freq=%d)\n",
1423 pseudo_prefix_title (conflict_regno), conflict_regno,
1424 reg_renumber[conflict_regno],
1425 lra_reg_info[conflict_regno].freq);
1426 update_lives (conflict_regno, true);
1427 lra_setup_reg_renumber (conflict_regno, -1, false);
1429 n = nfails;
1431 improve_inheritance (&changed_pseudo_bitmap);
1432 bitmap_clear (&non_reload_pseudos);
1433 bitmap_clear (&changed_insns);
1434 if (! lra_simple_p)
1436 /* We should not assign to original pseudos of inheritance
1437 pseudos or split pseudos if any its inheritance pseudo did
1438 not get hard register or any its split pseudo was not split
1439 because undo inheritance/split pass will extend live range of
1440 such inheritance or split pseudos. */
1441 bitmap_initialize (&do_not_assign_nonreload_pseudos, &reg_obstack);
1442 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, u, bi)
1443 if ((restore_regno = lra_reg_info[u].restore_regno) >= 0
1444 && reg_renumber[u] < 0
1445 && bitmap_bit_p (&lra_inheritance_pseudos, u))
1446 bitmap_set_bit (&do_not_assign_nonreload_pseudos, restore_regno);
1447 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, u, bi)
1448 if ((restore_regno = lra_reg_info[u].restore_regno) >= 0
1449 && reg_renumber[u] >= 0)
1450 bitmap_set_bit (&do_not_assign_nonreload_pseudos, restore_regno);
1451 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1452 if (((i < lra_constraint_new_regno_start
1453 && ! bitmap_bit_p (&do_not_assign_nonreload_pseudos, i))
1454 || (bitmap_bit_p (&lra_inheritance_pseudos, i)
1455 && lra_reg_info[i].restore_regno >= 0)
1456 || (bitmap_bit_p (&lra_split_regs, i)
1457 && lra_reg_info[i].restore_regno >= 0)
1458 || bitmap_bit_p (&lra_subreg_reload_pseudos, i)
1459 || bitmap_bit_p (&lra_optional_reload_pseudos, i))
1460 && reg_renumber[i] < 0 && lra_reg_info[i].nrefs != 0
1461 && regno_allocno_class_array[i] != NO_REGS)
1462 sorted_pseudos[n++] = i;
1463 bitmap_clear (&do_not_assign_nonreload_pseudos);
1464 if (n != 0 && lra_dump_file != NULL)
1465 fprintf (lra_dump_file, " Reassigning non-reload pseudos\n");
1466 qsort (sorted_pseudos, n, sizeof (int), pseudo_compare_func);
1467 for (i = 0; i < n; i++)
1469 regno = sorted_pseudos[i];
1470 hard_regno = find_hard_regno_for (regno, &cost, -1, false);
1471 if (hard_regno >= 0)
1473 assign_hard_regno (hard_regno, regno);
1474 /* We change allocation for non-reload pseudo on this
1475 iteration -- mark the pseudo for invalidation of used
1476 alternatives of insns containing the pseudo. */
1477 bitmap_set_bit (&changed_pseudo_bitmap, regno);
1481 free (update_hard_regno_preference_check);
1482 bitmap_clear (&best_spill_pseudos_bitmap);
1483 bitmap_clear (&spill_pseudos_bitmap);
1484 bitmap_clear (&insn_conflict_pseudos);
1488 /* Entry function to assign hard registers to new reload pseudos
1489 starting with LRA_CONSTRAINT_NEW_REGNO_START (by possible spilling
1490 of old pseudos) and possibly to the old pseudos. The function adds
1491 what insns to process for the next constraint pass. Those are all
1492 insns who contains non-reload and non-inheritance pseudos with
1493 changed allocation.
1495 Return true if we did not spill any non-reload and non-inheritance
1496 pseudos. */
1497 bool
1498 lra_assign (void)
1500 int i;
1501 unsigned int u;
1502 bitmap_iterator bi;
1503 bitmap_head insns_to_process;
1504 bool no_spills_p;
1505 int max_regno = max_reg_num ();
1507 timevar_push (TV_LRA_ASSIGN);
1508 lra_assignment_iter++;
1509 if (lra_dump_file != NULL)
1510 fprintf (lra_dump_file, "\n********** Assignment #%d: **********\n\n",
1511 lra_assignment_iter);
1512 init_lives ();
1513 sorted_pseudos = XNEWVEC (int, max_regno);
1514 sorted_reload_pseudos = XNEWVEC (int, max_regno);
1515 regno_allocno_class_array = XNEWVEC (enum reg_class, max_regno);
1516 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1517 regno_allocno_class_array[i] = lra_get_allocno_class (i);
1518 former_reload_pseudo_spill_p = false;
1519 init_regno_assign_info ();
1520 bitmap_initialize (&all_spilled_pseudos, &reg_obstack);
1521 create_live_range_start_chains ();
1522 setup_live_pseudos_and_spill_after_risky_transforms (&all_spilled_pseudos);
1523 #ifdef ENABLE_CHECKING
1524 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1525 if (lra_reg_info[i].nrefs != 0 && reg_renumber[i] >= 0
1526 && lra_reg_info[i].call_p
1527 && overlaps_hard_reg_set_p (call_used_reg_set,
1528 PSEUDO_REGNO_MODE (i), reg_renumber[i]))
1529 gcc_unreachable ();
1530 #endif
1531 /* Setup insns to process on the next constraint pass. */
1532 bitmap_initialize (&changed_pseudo_bitmap, &reg_obstack);
1533 init_live_reload_and_inheritance_pseudos ();
1534 assign_by_spills ();
1535 finish_live_reload_and_inheritance_pseudos ();
1536 bitmap_ior_into (&changed_pseudo_bitmap, &all_spilled_pseudos);
1537 no_spills_p = true;
1538 EXECUTE_IF_SET_IN_BITMAP (&all_spilled_pseudos, 0, u, bi)
1539 /* We ignore spilled pseudos created on last inheritance pass
1540 because they will be removed. */
1541 if (lra_reg_info[u].restore_regno < 0)
1543 no_spills_p = false;
1544 break;
1546 finish_live_range_start_chains ();
1547 bitmap_clear (&all_spilled_pseudos);
1548 bitmap_initialize (&insns_to_process, &reg_obstack);
1549 EXECUTE_IF_SET_IN_BITMAP (&changed_pseudo_bitmap, 0, u, bi)
1550 bitmap_ior_into (&insns_to_process, &lra_reg_info[u].insn_bitmap);
1551 bitmap_clear (&changed_pseudo_bitmap);
1552 EXECUTE_IF_SET_IN_BITMAP (&insns_to_process, 0, u, bi)
1554 lra_push_insn_by_uid (u);
1555 /* Invalidate alternatives for insn should be processed. */
1556 lra_set_used_insn_alternative_by_uid (u, -1);
1558 bitmap_clear (&insns_to_process);
1559 finish_regno_assign_info ();
1560 free (regno_allocno_class_array);
1561 free (sorted_pseudos);
1562 free (sorted_reload_pseudos);
1563 finish_lives ();
1564 timevar_pop (TV_LRA_ASSIGN);
1565 if (former_reload_pseudo_spill_p)
1566 lra_assignment_iter_after_spill++;
1567 if (lra_assignment_iter_after_spill > LRA_MAX_ASSIGNMENT_ITERATION_NUMBER)
1568 internal_error
1569 ("Maximum number of LRA assignment passes is achieved (%d)\n",
1570 LRA_MAX_ASSIGNMENT_ITERATION_NUMBER);
1571 return no_spills_p;