Branch data Line data Source code
1 : : /* Assign reload pseudos.
2 : : Copyright (C) 2010-2025 Free Software Foundation, Inc.
3 : : Contributed by Vladimir Makarov <vmakarov@redhat.com>.
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : :
22 : : /* This file'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 "backend.h"
81 : : #include "target.h"
82 : : #include "rtl.h"
83 : : #include "tree.h"
84 : : #include "predict.h"
85 : : #include "df.h"
86 : : #include "memmodel.h"
87 : : #include "tm_p.h"
88 : : #include "insn-config.h"
89 : : #include "regs.h"
90 : : #include "ira.h"
91 : : #include "recog.h"
92 : : #include "rtl-error.h"
93 : : #include "sparseset.h"
94 : : #include "lra.h"
95 : : #include "lra-int.h"
96 : : #include "function-abi.h"
97 : :
98 : : /* Current iteration number of the pass and current iteration number
99 : : of the pass after the latest spill pass when any former reload
100 : : pseudo was spilled. */
101 : : int lra_assignment_iter;
102 : : int lra_assignment_iter_after_spill;
103 : :
104 : : /* Flag of spilling former reload pseudos on this pass. */
105 : : static bool former_reload_pseudo_spill_p;
106 : :
107 : : /* Array containing corresponding values of function
108 : : lra_get_allocno_class. It is used to speed up the code. */
109 : : static enum reg_class *regno_allocno_class_array;
110 : :
111 : : /* Array containing lengths of pseudo live ranges. It is used to
112 : : speed up the code. */
113 : : static int *regno_live_length;
114 : :
115 : : /* Information about the thread to which a pseudo belongs. Threads are
116 : : a set of connected reload and inheritance pseudos with the same set of
117 : : available hard registers. Lone registers belong to their own threads. */
118 : : struct regno_assign_info
119 : : {
120 : : /* First/next pseudo of the same thread. */
121 : : int first, next;
122 : : /* Frequency of the thread (execution frequency of only reload
123 : : pseudos in the thread when the thread contains a reload pseudo).
124 : : Defined only for the first thread pseudo. */
125 : : int freq;
126 : : };
127 : :
128 : : /* Map regno to the corresponding regno assignment info. */
129 : : static struct regno_assign_info *regno_assign_info;
130 : :
131 : : /* All inherited, subreg or optional pseudos created before last spill
132 : : sub-pass. Such pseudos are permitted to get memory instead of hard
133 : : regs. */
134 : : static bitmap_head non_reload_pseudos;
135 : :
136 : : /* Process a pseudo copy with execution frequency COPY_FREQ connecting
137 : : REGNO1 and REGNO2 to form threads. */
138 : : static void
139 : 1591441 : process_copy_to_form_thread (int regno1, int regno2, int copy_freq)
140 : : {
141 : 1591441 : int last, regno1_first, regno2_first;
142 : :
143 : 1591441 : lra_assert (regno1 >= lra_constraint_new_regno_start
144 : : && regno2 >= lra_constraint_new_regno_start);
145 : 1591441 : regno1_first = regno_assign_info[regno1].first;
146 : 1591441 : regno2_first = regno_assign_info[regno2].first;
147 : 1591441 : if (regno1_first != regno2_first)
148 : : {
149 : 2490866 : for (last = regno2_first;
150 : 4082307 : regno_assign_info[last].next >= 0;
151 : 2490866 : last = regno_assign_info[last].next)
152 : 2490866 : regno_assign_info[last].first = regno1_first;
153 : 1591441 : regno_assign_info[last].first = regno1_first;
154 : 1591441 : regno_assign_info[last].next = regno_assign_info[regno1_first].next;
155 : 1591441 : regno_assign_info[regno1_first].next = regno2_first;
156 : 1591441 : regno_assign_info[regno1_first].freq
157 : 1591441 : += regno_assign_info[regno2_first].freq;
158 : : }
159 : 1591441 : regno_assign_info[regno1_first].freq -= 2 * copy_freq;
160 : 1591441 : lra_assert (regno_assign_info[regno1_first].freq >= 0);
161 : 1591441 : }
162 : :
163 : : /* Initialize REGNO_ASSIGN_INFO and form threads. */
164 : : static void
165 : 1547247 : init_regno_assign_info (void)
166 : : {
167 : 1547247 : int i, regno1, regno2, max_regno = max_reg_num ();
168 : 1547247 : lra_copy_t cp;
169 : :
170 : 1547247 : regno_assign_info = XNEWVEC (struct regno_assign_info, max_regno);
171 : 102338771 : for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
172 : : {
173 : 100791524 : regno_assign_info[i].first = i;
174 : 100791524 : regno_assign_info[i].next = -1;
175 : 100791524 : regno_assign_info[i].freq = lra_reg_info[i].freq;
176 : : }
177 : : /* Form the threads. */
178 : 4375787 : for (i = 0; (cp = lra_get_copy (i)) != NULL; i++)
179 : 2828540 : if ((regno1 = cp->regno1) >= lra_constraint_new_regno_start
180 : 2828540 : && (regno2 = cp->regno2) >= lra_constraint_new_regno_start
181 : 2828540 : && reg_renumber[regno1] < 0 && lra_reg_info[regno1].nrefs != 0
182 : 1624261 : && reg_renumber[regno2] < 0 && lra_reg_info[regno2].nrefs != 0
183 : 2828540 : && (ira_class_hard_regs_num[regno_allocno_class_array[regno1]]
184 : 1622826 : == ira_class_hard_regs_num[regno_allocno_class_array[regno2]]))
185 : 1591441 : process_copy_to_form_thread (regno1, regno2, cp->freq);
186 : 1547247 : }
187 : :
188 : : /* Free REGNO_ASSIGN_INFO. */
189 : : static void
190 : 1547247 : finish_regno_assign_info (void)
191 : : {
192 : 1547247 : free (regno_assign_info);
193 : 0 : }
194 : :
195 : : /* The function is used to sort *reload* and *inheritance* pseudos to
196 : : try to assign them hard registers. We put pseudos from the same
197 : : thread always nearby. */
198 : : static int
199 : 249105052 : reload_pseudo_compare_func (const void *v1p, const void *v2p)
200 : : {
201 : 249105052 : int r1 = *(const int *) v1p, r2 = *(const int *) v2p;
202 : 249105052 : enum reg_class cl1 = regno_allocno_class_array[r1];
203 : 249105052 : enum reg_class cl2 = regno_allocno_class_array[r2];
204 : 249105052 : int diff;
205 : :
206 : 249105052 : lra_assert (r1 >= lra_constraint_new_regno_start
207 : : && r2 >= lra_constraint_new_regno_start);
208 : :
209 : : /* Prefer to assign reload registers with smaller classes first to
210 : : guarantee assignment to all reload registers. */
211 : 249105052 : if ((diff = (ira_class_hard_regs_num[cl1]
212 : 249105052 : - ira_class_hard_regs_num[cl2])) != 0)
213 : : return diff;
214 : : /* Allocate bigger pseudos first to avoid register file
215 : : fragmentation. */
216 : 233911844 : if ((diff
217 : 233911844 : = (ira_reg_class_max_nregs[cl2][lra_reg_info[r2].biggest_mode]
218 : 233911844 : - ira_reg_class_max_nregs[cl1][lra_reg_info[r1].biggest_mode])) != 0)
219 : : return diff;
220 : 231814349 : if ((diff = (regno_assign_info[regno_assign_info[r2].first].freq
221 : 231814349 : - regno_assign_info[regno_assign_info[r1].first].freq)) != 0)
222 : : return diff;
223 : : /* Put pseudos from the thread nearby. */
224 : 129287284 : if ((diff = regno_assign_info[r1].first - regno_assign_info[r2].first) != 0)
225 : : return diff;
226 : : /* Prefer pseudos with longer live ranges. It sets up better
227 : : prefered hard registers for the thread pseudos and decreases
228 : : register-register moves between the thread pseudos. */
229 : 17603624 : if ((diff = regno_live_length[r2] - regno_live_length[r1]) != 0)
230 : : return diff;
231 : : /* If regs are equally good, sort by their numbers, so that the
232 : : results of qsort leave nothing to chance. */
233 : 8130620 : return r1 - r2;
234 : : }
235 : :
236 : : /* The function is used to sort *non-reload* pseudos to try to assign
237 : : them hard registers. The order calculation is simpler than in the
238 : : previous function and based on the pseudo frequency usage. */
239 : : static int
240 : 1119731366 : pseudo_compare_func (const void *v1p, const void *v2p)
241 : : {
242 : 1119731366 : int r1 = *(const int *) v1p, r2 = *(const int *) v2p;
243 : 1119731366 : int diff;
244 : :
245 : : /* Assign hard reg to static chain pointer first pseudo when
246 : : non-local goto is used. */
247 : 1119731366 : if ((diff = (non_spilled_static_chain_regno_p (r2)
248 : 1119731366 : - non_spilled_static_chain_regno_p (r1))) != 0)
249 : : return diff;
250 : :
251 : : /* Prefer to assign more frequently used registers first. */
252 : 1119728098 : if ((diff = lra_reg_info[r2].freq - lra_reg_info[r1].freq) != 0)
253 : : return diff;
254 : :
255 : : /* If regs are equally good, sort by their numbers, so that the
256 : : results of qsort leave nothing to chance. */
257 : 656120016 : return r1 - r2;
258 : : }
259 : :
260 : : /* Arrays of size LRA_LIVE_MAX_POINT mapping a program point to the
261 : : pseudo live ranges with given start point. We insert only live
262 : : ranges of pseudos interesting for assignment purposes. They are
263 : : reload pseudos and pseudos assigned to hard registers. */
264 : : static lra_live_range_t *start_point_ranges;
265 : :
266 : : /* Used as a flag that a live range is not inserted in the start point
267 : : chain. */
268 : : static struct lra_live_range not_in_chain_mark;
269 : :
270 : : /* Create and set up START_POINT_RANGES. */
271 : : static void
272 : 1547247 : create_live_range_start_chains (void)
273 : : {
274 : 1547247 : int i, max_regno;
275 : 1547247 : lra_live_range_t r;
276 : :
277 : 1547247 : start_point_ranges = XCNEWVEC (lra_live_range_t, lra_live_max_point);
278 : 1547247 : max_regno = max_reg_num ();
279 : 103886018 : for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
280 : 100791524 : if (i >= lra_constraint_new_regno_start || reg_renumber[i] >= 0)
281 : : {
282 : 108446713 : for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
283 : : {
284 : 58274348 : r->start_next = start_point_ranges[r->start];
285 : 58274348 : start_point_ranges[r->start] = r;
286 : : }
287 : : }
288 : : else
289 : : {
290 : 53489234 : for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
291 : 2870075 : r->start_next = ¬_in_chain_mark;
292 : : }
293 : 1547247 : }
294 : :
295 : : /* Insert live ranges of pseudo REGNO into start chains if they are
296 : : not there yet. */
297 : : static void
298 : 494864573 : insert_in_live_range_start_chain (int regno)
299 : : {
300 : 494864573 : lra_live_range_t r = lra_reg_info[regno].live_ranges;
301 : :
302 : 494864573 : if (r->start_next != ¬_in_chain_mark)
303 : : return;
304 : 225247 : for (; r != NULL; r = r->next)
305 : : {
306 : 149212 : r->start_next = start_point_ranges[r->start];
307 : 149212 : start_point_ranges[r->start] = r;
308 : : }
309 : : }
310 : :
311 : : /* Free START_POINT_RANGES. */
312 : : static void
313 : 1547247 : finish_live_range_start_chains (void)
314 : : {
315 : 1547247 : gcc_assert (start_point_ranges != NULL);
316 : 1547247 : free (start_point_ranges);
317 : 1547247 : start_point_ranges = NULL;
318 : 1547247 : }
319 : :
320 : : /* Map: program point -> bitmap of all pseudos living at the point and
321 : : assigned to hard registers. */
322 : : static bitmap_head *live_hard_reg_pseudos;
323 : : static bitmap_obstack live_hard_reg_pseudos_bitmap_obstack;
324 : :
325 : : /* reg_renumber corresponding to pseudos marked in
326 : : live_hard_reg_pseudos. reg_renumber might be not matched to
327 : : live_hard_reg_pseudos but live_pseudos_reg_renumber always reflects
328 : : live_hard_reg_pseudos. */
329 : : static int *live_pseudos_reg_renumber;
330 : :
331 : : /* Sparseset used to calculate living hard reg pseudos for some program
332 : : point range. */
333 : : static sparseset live_range_hard_reg_pseudos;
334 : :
335 : : /* Sparseset used to calculate living reload/inheritance pseudos for
336 : : some program point range. */
337 : : static sparseset live_range_reload_inheritance_pseudos;
338 : :
339 : : /* Allocate and initialize the data about living pseudos at program
340 : : points. */
341 : : static void
342 : 1547247 : init_lives (void)
343 : : {
344 : 1547247 : int i, max_regno = max_reg_num ();
345 : :
346 : 1547247 : live_range_hard_reg_pseudos = sparseset_alloc (max_regno);
347 : 1547247 : live_range_reload_inheritance_pseudos = sparseset_alloc (max_regno);
348 : 1547247 : live_hard_reg_pseudos = XNEWVEC (bitmap_head, lra_live_max_point);
349 : 1547247 : bitmap_obstack_initialize (&live_hard_reg_pseudos_bitmap_obstack);
350 : 91681272 : for (i = 0; i < lra_live_max_point; i++)
351 : 88586778 : bitmap_initialize (&live_hard_reg_pseudos[i],
352 : : &live_hard_reg_pseudos_bitmap_obstack);
353 : 1547247 : live_pseudos_reg_renumber = XNEWVEC (int, max_regno);
354 : 244685495 : for (i = 0; i < max_regno; i++)
355 : 243138248 : live_pseudos_reg_renumber[i] = -1;
356 : 1547247 : }
357 : :
358 : : /* Free the data about living pseudos at program points. */
359 : : static void
360 : 1547247 : finish_lives (void)
361 : : {
362 : 1547247 : sparseset_free (live_range_hard_reg_pseudos);
363 : 1547247 : sparseset_free (live_range_reload_inheritance_pseudos);
364 : 1547247 : free (live_hard_reg_pseudos);
365 : 1547247 : bitmap_obstack_release (&live_hard_reg_pseudos_bitmap_obstack);
366 : 1547247 : free (live_pseudos_reg_renumber);
367 : 1547247 : }
368 : :
369 : : /* Update the LIVE_HARD_REG_PSEUDOS and LIVE_PSEUDOS_REG_RENUMBER
370 : : entries for pseudo REGNO. Assume that the register has been
371 : : spilled if FREE_P, otherwise assume that it has been assigned
372 : : reg_renumber[REGNO] (if >= 0). We also insert the pseudo live
373 : : ranges in the start chains when it is assumed to be assigned to a
374 : : hard register because we use the chains of pseudos assigned to hard
375 : : registers during allocation. */
376 : : static void
377 : 48729186 : update_lives (int regno, bool free_p)
378 : : {
379 : 48729186 : int p;
380 : 48729186 : lra_live_range_t r;
381 : :
382 : 48729186 : if (reg_renumber[regno] < 0)
383 : : return;
384 : 48729186 : live_pseudos_reg_renumber[regno] = free_p ? -1 : reg_renumber[regno];
385 : 107313563 : for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
386 : : {
387 : 604764457 : for (p = r->start; p <= r->finish; p++)
388 : 546180080 : if (free_p)
389 : 56773654 : bitmap_clear_bit (&live_hard_reg_pseudos[p], regno);
390 : : else
391 : : {
392 : 489406426 : bitmap_set_bit (&live_hard_reg_pseudos[p], regno);
393 : 489406426 : insert_in_live_range_start_chain (regno);
394 : : }
395 : : }
396 : : }
397 : :
398 : : /* Sparseset used to calculate reload pseudos conflicting with a given
399 : : pseudo when we are trying to find a hard register for the given
400 : : pseudo. */
401 : : static sparseset conflict_reload_and_inheritance_pseudos;
402 : :
403 : : /* Map: program point -> bitmap of all reload and inheritance pseudos
404 : : living at the point. */
405 : : static bitmap_head *live_reload_and_inheritance_pseudos;
406 : : static bitmap_obstack live_reload_and_inheritance_pseudos_bitmap_obstack;
407 : :
408 : : /* Allocate and initialize data about living reload pseudos at any
409 : : given program point. */
410 : : static void
411 : 1547247 : init_live_reload_and_inheritance_pseudos (void)
412 : : {
413 : 1547247 : int i, p, max_regno = max_reg_num ();
414 : 1547247 : lra_live_range_t r;
415 : :
416 : 1547247 : conflict_reload_and_inheritance_pseudos = sparseset_alloc (max_regno);
417 : 1547247 : live_reload_and_inheritance_pseudos = XNEWVEC (bitmap_head, lra_live_max_point);
418 : 1547247 : bitmap_obstack_initialize (&live_reload_and_inheritance_pseudos_bitmap_obstack);
419 : 91681272 : for (p = 0; p < lra_live_max_point; p++)
420 : 88586778 : bitmap_initialize (&live_reload_and_inheritance_pseudos[p],
421 : : &live_reload_and_inheritance_pseudos_bitmap_obstack);
422 : 1547247 : if ((unsigned) (max_regno - lra_constraint_new_regno_start)
423 : 1547247 : >= (1U << lra_max_pseudos_points_log2_considered_for_preferences)
424 : 1547247 : / (lra_live_max_point + 1))
425 : 16 : return;
426 : 1547231 : bitmap_head start_points;
427 : 1547231 : bitmap_initialize (&start_points,
428 : : &live_hard_reg_pseudos_bitmap_obstack);
429 : 13367436 : for (i = lra_constraint_new_regno_start; i < max_regno; i++)
430 : 23084513 : for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
431 : 11264308 : bitmap_set_bit (&start_points, r->start);
432 : 13367436 : for (i = lra_constraint_new_regno_start; i < max_regno; i++)
433 : : {
434 : 23084513 : for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
435 : : {
436 : 11264308 : bitmap_iterator bi;
437 : 11264308 : unsigned p;
438 : 45487724 : EXECUTE_IF_SET_IN_BITMAP (&start_points, r->start, p, bi)
439 : : {
440 : 44817523 : if (p > (unsigned) r->finish)
441 : : break;
442 : 34223416 : bitmap_set_bit (&live_reload_and_inheritance_pseudos[p], i);
443 : : }
444 : : }
445 : : }
446 : 1547231 : bitmap_clear (&start_points);
447 : : }
448 : :
449 : : /* Finalize data about living reload pseudos at any given program
450 : : point. */
451 : : static void
452 : 1547247 : finish_live_reload_and_inheritance_pseudos (void)
453 : : {
454 : 1547247 : sparseset_free (conflict_reload_and_inheritance_pseudos);
455 : 1547247 : free (live_reload_and_inheritance_pseudos);
456 : 1547247 : bitmap_obstack_release (&live_reload_and_inheritance_pseudos_bitmap_obstack);
457 : 1547247 : }
458 : :
459 : : /* The value used to check that cost of given hard reg is really
460 : : defined currently. */
461 : : static int curr_hard_regno_costs_check = 0;
462 : : /* Array used to check that cost of the corresponding hard reg (the
463 : : array element index) is really defined currently. */
464 : : static int hard_regno_costs_check[FIRST_PSEUDO_REGISTER];
465 : : /* The current costs of allocation of hard regs. Defined only if the
466 : : value of the corresponding element of the previous array is equal to
467 : : CURR_HARD_REGNO_COSTS_CHECK. */
468 : : static int hard_regno_costs[FIRST_PSEUDO_REGISTER];
469 : :
470 : : /* Adjust cost of HARD_REGNO by INCR. Reset the cost first if it is
471 : : not defined yet. */
472 : : static inline void
473 : 22296656 : adjust_hard_regno_cost (int hard_regno, int incr)
474 : : {
475 : 22296656 : if (hard_regno_costs_check[hard_regno] != curr_hard_regno_costs_check)
476 : 11816362 : hard_regno_costs[hard_regno] = 0;
477 : 22296656 : hard_regno_costs_check[hard_regno] = curr_hard_regno_costs_check;
478 : 22296656 : hard_regno_costs[hard_regno] += incr;
479 : 22296656 : }
480 : :
481 : : /* Try to find a free hard register for pseudo REGNO. Return the
482 : : hard register on success and set *COST to the cost of using
483 : : that register. (If several registers have equal cost, the one with
484 : : the highest priority wins.) Return -1 on failure.
485 : :
486 : : If FIRST_P, return the first available hard reg ignoring other
487 : : criteria, e.g. allocation cost. This approach results in less hard
488 : : reg pool fragmentation and permit to allocate hard regs to reload
489 : : pseudos in complicated situations where pseudo sizes are different.
490 : :
491 : : If TRY_ONLY_HARD_REGNO >= 0, consider only that hard register,
492 : : otherwise consider all hard registers in REGNO's class.
493 : :
494 : : If REGNO_SET is not empty, only hard registers from the set are
495 : : considered. */
496 : : static int
497 : 13816722 : find_hard_regno_for_1 (int regno, int *cost, int try_only_hard_regno,
498 : : bool first_p, HARD_REG_SET regno_set)
499 : : {
500 : 13816722 : HARD_REG_SET conflict_set;
501 : 13816722 : int best_cost = INT_MAX, best_priority = INT_MIN, best_usage = INT_MAX;
502 : 13816722 : lra_live_range_t r;
503 : 13816722 : int p, i, j, rclass_size, best_hard_regno, priority, hard_regno;
504 : 13816722 : int hr, conflict_hr, nregs;
505 : 13816722 : machine_mode biggest_mode;
506 : 13816722 : unsigned int k, conflict_regno;
507 : 13816722 : poly_int64 offset;
508 : 13816722 : int val, biggest_nregs, nregs_diff;
509 : 13816722 : enum reg_class rclass;
510 : 13816722 : bitmap_iterator bi;
511 : 13816722 : bool *rclass_intersect_p;
512 : 13816722 : HARD_REG_SET impossible_start_hard_regs, available_regs;
513 : :
514 : 27633444 : if (hard_reg_set_empty_p (regno_set))
515 : 13597479 : conflict_set = lra_no_alloc_regs;
516 : : else
517 : 219243 : conflict_set = ~regno_set | lra_no_alloc_regs;
518 : 13816722 : rclass = regno_allocno_class_array[regno];
519 : 13816722 : rclass_intersect_p = ira_reg_classes_intersect_p[rclass];
520 : 13816722 : curr_hard_regno_costs_check++;
521 : 13816722 : sparseset_clear (conflict_reload_and_inheritance_pseudos);
522 : 13816722 : sparseset_clear (live_range_hard_reg_pseudos);
523 : 13816722 : conflict_set |= lra_reg_info[regno].conflict_hard_regs;
524 : 13816722 : biggest_mode = lra_reg_info[regno].biggest_mode;
525 : 29612686 : for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
526 : : {
527 : 129031247 : EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[r->start], 0, k, bi)
528 : 113235283 : if (rclass_intersect_p[regno_allocno_class_array[k]])
529 : 99914823 : sparseset_set_bit (live_range_hard_reg_pseudos, k);
530 : 134168363 : EXECUTE_IF_SET_IN_BITMAP (&live_reload_and_inheritance_pseudos[r->start],
531 : : 0, k, bi)
532 : 118372399 : if (lra_reg_info[k].preferred_hard_regno1 >= 0
533 : 32863214 : && live_pseudos_reg_renumber[k] < 0
534 : 30348605 : && rclass_intersect_p[regno_allocno_class_array[k]])
535 : 28057935 : sparseset_set_bit (conflict_reload_and_inheritance_pseudos, k);
536 : 2835894772 : for (p = r->start + 1; p <= r->finish; p++)
537 : : {
538 : 2820098808 : lra_live_range_t r2;
539 : :
540 : 2820098808 : for (r2 = start_point_ranges[p];
541 : 4807581233 : r2 != NULL;
542 : 1987482425 : r2 = r2->start_next)
543 : : {
544 : 1987482425 : if (live_pseudos_reg_renumber[r2->regno] < 0
545 : 633595386 : && r2->regno >= lra_constraint_new_regno_start
546 : 632670244 : && lra_reg_info[r2->regno].preferred_hard_regno1 >= 0
547 : 363355218 : && rclass_intersect_p[regno_allocno_class_array[r2->regno]])
548 : 355386192 : sparseset_set_bit (conflict_reload_and_inheritance_pseudos,
549 : : r2->regno);
550 : 1632096233 : else if (live_pseudos_reg_renumber[r2->regno] >= 0
551 : 1353887039 : && rclass_intersect_p
552 : 1353887039 : [regno_allocno_class_array[r2->regno]])
553 : 1247134244 : sparseset_set_bit (live_range_hard_reg_pseudos, r2->regno);
554 : : }
555 : : }
556 : : }
557 : 13816722 : if ((hard_regno = lra_reg_info[regno].preferred_hard_regno1) >= 0)
558 : : {
559 : 5702552 : adjust_hard_regno_cost
560 : 5702552 : (hard_regno, -lra_reg_info[regno].preferred_hard_regno_profit1);
561 : 5702552 : if ((hard_regno = lra_reg_info[regno].preferred_hard_regno2) >= 0)
562 : 1791128 : adjust_hard_regno_cost
563 : 1791128 : (hard_regno, -lra_reg_info[regno].preferred_hard_regno_profit2);
564 : : }
565 : : #ifdef STACK_REGS
566 : 13816722 : if (lra_reg_info[regno].no_stack_p)
567 : 476289 : for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
568 : 423368 : SET_HARD_REG_BIT (conflict_set, i);
569 : : #endif
570 : 13816722 : sparseset_clear_bit (conflict_reload_and_inheritance_pseudos, regno);
571 : 13816722 : val = lra_reg_info[regno].val;
572 : 13816722 : offset = lra_reg_info[regno].offset;
573 : 13816722 : impossible_start_hard_regs = lra_reg_info[regno].exclude_start_hard_regs;
574 : 196755453 : EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos, conflict_regno)
575 : : {
576 : 187412802 : conflict_hr = live_pseudos_reg_renumber[conflict_regno];
577 : 187412802 : if (lra_reg_val_equal_p (conflict_regno, val, offset))
578 : : {
579 : 859665 : conflict_hr = live_pseudos_reg_renumber[conflict_regno];
580 : 859665 : nregs = hard_regno_nregs (conflict_hr,
581 : 859665 : lra_reg_info[conflict_regno].biggest_mode);
582 : : /* Remember about multi-register pseudos. For example, 2
583 : : hard register pseudos can start on the same hard register
584 : : but cannot start on HR and HR+1/HR-1. */
585 : 866220 : for (hr = conflict_hr + 1;
586 : 866220 : hr < FIRST_PSEUDO_REGISTER && hr < conflict_hr + nregs;
587 : : hr++)
588 : 6555 : SET_HARD_REG_BIT (impossible_start_hard_regs, hr);
589 : 864848 : for (hr = conflict_hr - 1;
590 : 864848 : hr >= 0 && (int) end_hard_regno (biggest_mode, hr) > conflict_hr;
591 : : hr--)
592 : 5183 : SET_HARD_REG_BIT (impossible_start_hard_regs, hr);
593 : : }
594 : : else
595 : : {
596 : 186553137 : machine_mode biggest_conflict_mode
597 : 186553137 : = lra_reg_info[conflict_regno].biggest_mode;
598 : 186553137 : int biggest_conflict_nregs
599 : 186553137 : = hard_regno_nregs (conflict_hr, biggest_conflict_mode);
600 : :
601 : 186553137 : nregs_diff
602 : 186553137 : = (biggest_conflict_nregs
603 : 186553137 : - hard_regno_nregs (conflict_hr,
604 : 186553137 : PSEUDO_REGNO_MODE (conflict_regno)));
605 : 186553137 : add_to_hard_reg_set (&conflict_set,
606 : : biggest_conflict_mode,
607 : : conflict_hr
608 : : - (WORDS_BIG_ENDIAN ? nregs_diff : 0));
609 : 373106274 : if (hard_reg_set_subset_p (reg_class_contents[rclass],
610 : : conflict_set))
611 : : return -1;
612 : : }
613 : : }
614 : 19330550 : EXECUTE_IF_SET_IN_SPARSESET (conflict_reload_and_inheritance_pseudos,
615 : : conflict_regno)
616 : 19975798 : if (!lra_reg_val_equal_p (conflict_regno, val, offset))
617 : : {
618 : 9740778 : lra_assert (live_pseudos_reg_renumber[conflict_regno] < 0);
619 : 9740778 : if ((hard_regno
620 : 9740778 : = lra_reg_info[conflict_regno].preferred_hard_regno1) >= 0)
621 : : {
622 : 9740778 : adjust_hard_regno_cost
623 : 9740778 : (hard_regno,
624 : : lra_reg_info[conflict_regno].preferred_hard_regno_profit1);
625 : 9740778 : if ((hard_regno
626 : 9740778 : = lra_reg_info[conflict_regno].preferred_hard_regno2) >= 0)
627 : 5062198 : adjust_hard_regno_cost
628 : 5062198 : (hard_regno,
629 : : lra_reg_info[conflict_regno].preferred_hard_regno_profit2);
630 : : }
631 : : }
632 : : /* Make sure that all registers in a multi-word pseudo belong to the
633 : : required class. */
634 : 9342651 : conflict_set |= ~reg_class_contents[rclass];
635 : 9342651 : lra_assert (rclass != NO_REGS);
636 : 9342651 : rclass_size = ira_class_hard_regs_num[rclass];
637 : 9342651 : best_hard_regno = -1;
638 : 9342651 : hard_regno = ira_class_hard_regs[rclass][0];
639 : 9342651 : biggest_nregs = hard_regno_nregs (hard_regno, biggest_mode);
640 : 9342651 : nregs_diff = (biggest_nregs
641 : 9342651 : - hard_regno_nregs (hard_regno, PSEUDO_REGNO_MODE (regno)));
642 : 9342651 : available_regs = reg_class_contents[rclass] & ~lra_no_alloc_regs;
643 : 121473196 : for (i = 0; i < rclass_size; i++)
644 : : {
645 : 112204721 : if (try_only_hard_regno >= 0)
646 : : hard_regno = try_only_hard_regno;
647 : : else
648 : 112134015 : hard_regno = ira_class_hard_regs[rclass][i];
649 : 112204721 : if (! overlaps_hard_reg_set_p (conflict_set,
650 : 112204721 : PSEUDO_REGNO_MODE (regno), hard_regno)
651 : 59168024 : && targetm.hard_regno_mode_ok (hard_regno, PSEUDO_REGNO_MODE (regno))
652 : : /* We cannot use prohibited_class_mode_regs for all classes
653 : : because it is not defined for all classes. */
654 : 59019841 : && (ira_allocno_class_translate[rclass] != rclass
655 : 22492904 : || ! TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs
656 : 22492904 : [rclass][biggest_mode],
657 : : hard_regno))
658 : 59019484 : && ! TEST_HARD_REG_BIT (impossible_start_hard_regs, hard_regno)
659 : 171222395 : && (nregs_diff == 0
660 : 4550 : || (WORDS_BIG_ENDIAN
661 : : ? (hard_regno - nregs_diff >= 0
662 : : && TEST_HARD_REG_BIT (available_regs,
663 : : hard_regno - nregs_diff))
664 : 4550 : : TEST_HARD_REG_BIT (available_regs,
665 : 4550 : hard_regno + nregs_diff))))
666 : : {
667 : 59017293 : if (hard_regno_costs_check[hard_regno]
668 : 59017293 : != curr_hard_regno_costs_check)
669 : : {
670 : 53799067 : hard_regno_costs_check[hard_regno] = curr_hard_regno_costs_check;
671 : 53799067 : hard_regno_costs[hard_regno] = 0;
672 : : }
673 : 60180508 : for (j = 0;
674 : 119197801 : j < hard_regno_nregs (hard_regno, PSEUDO_REGNO_MODE (regno));
675 : : j++)
676 : 60180508 : if (! crtl->abi->clobbers_full_reg_p (hard_regno + j)
677 : 60180508 : && ! df_regs_ever_live_p (hard_regno + j))
678 : : /* It needs save restore. */
679 : 12087586 : hard_regno_costs[hard_regno]
680 : 6043793 : += (2
681 : 16656140 : * REG_FREQ_FROM_BB (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
682 : 15658700 : + 1);
683 : 59017293 : priority = targetm.register_priority (hard_regno);
684 : 49888246 : if (best_hard_regno < 0 || hard_regno_costs[hard_regno] < best_cost
685 : 107094960 : || (hard_regno_costs[hard_regno] == best_cost
686 : 25161601 : && (priority > best_priority
687 : 25066120 : || (targetm.register_usage_leveling_p ()
688 : 25066120 : && priority == best_priority
689 : 6378081 : && best_usage > lra_hard_reg_usage[hard_regno]))))
690 : : {
691 : 13656089 : best_hard_regno = hard_regno;
692 : 13656089 : best_cost = hard_regno_costs[hard_regno];
693 : 13656089 : best_priority = priority;
694 : 13656089 : best_usage = lra_hard_reg_usage[hard_regno];
695 : : }
696 : : }
697 : 112204721 : if (try_only_hard_regno >= 0 || (first_p && best_hard_regno >= 0))
698 : : break;
699 : : }
700 : 9342651 : if (best_hard_regno >= 0)
701 : 9129047 : *cost = best_cost - lra_reg_info[regno].freq;
702 : : return best_hard_regno;
703 : : }
704 : :
705 : : /* A wrapper for find_hard_regno_for_1 (see comments for that function
706 : : description). This function tries to find a hard register for
707 : : preferred class first if it is worth. */
708 : : static int
709 : 13600307 : find_hard_regno_for (int regno, int *cost, int try_only_hard_regno, bool first_p)
710 : : {
711 : 13600307 : int hard_regno;
712 : 13600307 : HARD_REG_SET regno_set;
713 : :
714 : : /* Only original pseudos can have a different preferred class. */
715 : 13600307 : if (try_only_hard_regno < 0 && regno < lra_new_regno_start)
716 : : {
717 : 1207202 : enum reg_class pref_class = reg_preferred_class (regno);
718 : :
719 : 1207202 : if (regno_allocno_class_array[regno] != pref_class)
720 : : {
721 : 438486 : hard_regno = find_hard_regno_for_1 (regno, cost, -1, first_p,
722 : 219243 : reg_class_contents[pref_class]);
723 : 219243 : if (hard_regno >= 0)
724 : : return hard_regno;
725 : : }
726 : : }
727 : 13597479 : CLEAR_HARD_REG_SET (regno_set);
728 : 13597479 : return find_hard_regno_for_1 (regno, cost, try_only_hard_regno, first_p,
729 : 13597479 : regno_set);
730 : : }
731 : :
732 : : /* Current value used for checking elements in
733 : : update_hard_regno_preference_check. */
734 : : static int curr_update_hard_regno_preference_check;
735 : : /* If an element value is equal to the above variable value, then the
736 : : corresponding regno has been processed for preference
737 : : propagation. */
738 : : static int *update_hard_regno_preference_check;
739 : :
740 : : /* Update the preference for using HARD_REGNO for pseudos that are
741 : : connected directly or indirectly with REGNO. Apply divisor DIV
742 : : to any preference adjustments.
743 : :
744 : : The more indirectly a pseudo is connected, the smaller its effect
745 : : should be. We therefore increase DIV on each "hop". */
746 : : static void
747 : 9399592 : update_hard_regno_preference (int regno, int hard_regno, int div)
748 : : {
749 : 9399592 : int another_regno, cost;
750 : 9399592 : lra_copy_t cp, next_cp;
751 : :
752 : : /* Search depth 5 seems to be enough. */
753 : 9399592 : if (div > (1 << 5))
754 : : return;
755 : 17087821 : for (cp = lra_reg_info[regno].copies; cp != NULL; cp = next_cp)
756 : : {
757 : 7769859 : if (cp->regno1 == regno)
758 : : {
759 : 3535529 : next_cp = cp->regno1_next;
760 : 3535529 : another_regno = cp->regno2;
761 : : }
762 : 4234330 : else if (cp->regno2 == regno)
763 : : {
764 : 4234330 : next_cp = cp->regno2_next;
765 : 4234330 : another_regno = cp->regno1;
766 : : }
767 : : else
768 : 0 : gcc_unreachable ();
769 : 7769859 : if (reg_renumber[another_regno] < 0
770 : 4206101 : && (update_hard_regno_preference_check[another_regno]
771 : 4206101 : != curr_update_hard_regno_preference_check))
772 : : {
773 : 2911941 : update_hard_regno_preference_check[another_regno]
774 : 2911941 : = curr_update_hard_regno_preference_check;
775 : 2911941 : cost = cp->freq < div ? 1 : cp->freq / div;
776 : 2911941 : lra_setup_reload_pseudo_preferenced_hard_reg
777 : 2911941 : (another_regno, hard_regno, cost);
778 : 2911941 : update_hard_regno_preference (another_regno, hard_regno, div * 2);
779 : : }
780 : : }
781 : : }
782 : :
783 : : /* Return prefix title for pseudo REGNO. */
784 : : static const char *
785 : 98 : pseudo_prefix_title (int regno)
786 : : {
787 : 98 : return
788 : 98 : (regno < lra_constraint_new_regno_start ? ""
789 : 98 : : bitmap_bit_p (&lra_inheritance_pseudos, regno) ? "inheritance "
790 : 95 : : bitmap_bit_p (&lra_split_regs, regno) ? "split "
791 : 95 : : bitmap_bit_p (&lra_optional_reload_pseudos, regno) ? "optional reload "
792 : 92 : : bitmap_bit_p (&lra_subreg_reload_pseudos, regno) ? "subreg reload "
793 : 98 : : "reload ");
794 : : }
795 : :
796 : : /* Update REG_RENUMBER and other pseudo preferences by assignment of
797 : : HARD_REGNO to pseudo REGNO and print about it if PRINT_P. */
798 : : void
799 : 6614374 : lra_setup_reg_renumber (int regno, int hard_regno, bool print_p)
800 : : {
801 : 6614374 : int i, hr;
802 : :
803 : : /* We cannot just reassign hard register. */
804 : 6614374 : lra_assert (hard_regno < 0 || reg_renumber[regno] < 0);
805 : 126723 : if ((hr = hard_regno) < 0)
806 : 126723 : hr = reg_renumber[regno];
807 : 6614374 : reg_renumber[regno] = hard_regno;
808 : 6614374 : lra_assert (hr >= 0);
809 : 13430898 : for (i = 0; i < hard_regno_nregs (hr, PSEUDO_REGNO_MODE (regno)); i++)
810 : 6816524 : if (hard_regno < 0)
811 : 135866 : lra_hard_reg_usage[hr + i] -= lra_reg_info[regno].freq;
812 : : else
813 : 6680658 : lra_hard_reg_usage[hr + i] += lra_reg_info[regno].freq;
814 : 6614374 : if (print_p && lra_dump_file != NULL)
815 : 196 : fprintf (lra_dump_file, " Assign %d to %sr%d (freq=%d)\n",
816 : 98 : reg_renumber[regno], pseudo_prefix_title (regno),
817 : 98 : regno, lra_reg_info[regno].freq);
818 : 6614374 : if (hard_regno >= 0)
819 : : {
820 : 6487651 : curr_update_hard_regno_preference_check++;
821 : 6487651 : update_hard_regno_preference (regno, hard_regno, 1);
822 : : }
823 : 6614374 : }
824 : :
825 : : /* Pseudos which occur in insns containing a particular pseudo. */
826 : : static bitmap_head insn_conflict_pseudos;
827 : :
828 : : /* Bitmaps used to contain spill pseudos for given pseudo hard regno
829 : : and best spill pseudos for given pseudo (and best hard regno). */
830 : : static bitmap_head spill_pseudos_bitmap, best_spill_pseudos_bitmap;
831 : :
832 : : /* Current pseudo check for validity of elements in
833 : : TRY_HARD_REG_PSEUDOS. */
834 : : static int curr_pseudo_check;
835 : : /* Array used for validity of elements in TRY_HARD_REG_PSEUDOS. */
836 : : static int try_hard_reg_pseudos_check[FIRST_PSEUDO_REGISTER];
837 : : /* Pseudos who hold given hard register at the considered points. */
838 : : static bitmap_head try_hard_reg_pseudos[FIRST_PSEUDO_REGISTER];
839 : :
840 : : /* Set up try_hard_reg_pseudos for given program point P and class
841 : : RCLASS. Those are pseudos living at P and assigned to a hard
842 : : register of RCLASS. In other words, those are pseudos which can be
843 : : spilled to assign a hard register of RCLASS to a pseudo living at
844 : : P. */
845 : : static void
846 : 122911 : setup_try_hard_regno_pseudos (int p, enum reg_class rclass)
847 : : {
848 : 122911 : int i, hard_regno;
849 : 122911 : machine_mode mode;
850 : 122911 : unsigned int spill_regno;
851 : 122911 : bitmap_iterator bi;
852 : :
853 : : /* Find what pseudos could be spilled. */
854 : 930857 : EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[p], 0, spill_regno, bi)
855 : : {
856 : 807946 : mode = PSEUDO_REGNO_MODE (spill_regno);
857 : 807946 : hard_regno = live_pseudos_reg_renumber[spill_regno];
858 : 807946 : if (overlaps_hard_reg_set_p (reg_class_contents[rclass],
859 : : mode, hard_regno))
860 : : {
861 : 1186276 : for (i = hard_regno_nregs (hard_regno, mode) - 1; i >= 0; i--)
862 : : {
863 : 601610 : if (try_hard_reg_pseudos_check[hard_regno + i]
864 : 601610 : != curr_pseudo_check)
865 : : {
866 : 283251 : try_hard_reg_pseudos_check[hard_regno + i]
867 : 283251 : = curr_pseudo_check;
868 : 283251 : bitmap_clear (&try_hard_reg_pseudos[hard_regno + i]);
869 : : }
870 : 601610 : bitmap_set_bit (&try_hard_reg_pseudos[hard_regno + i],
871 : : spill_regno);
872 : : }
873 : : }
874 : : }
875 : 122911 : }
876 : :
877 : : /* Assign temporarily HARD_REGNO to pseudo REGNO. Temporary
878 : : assignment means that we might undo the data change. */
879 : : static void
880 : 1928670 : assign_temporarily (int regno, int hard_regno)
881 : : {
882 : 1928670 : int p;
883 : 1928670 : lra_live_range_t r;
884 : :
885 : 3885810 : for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
886 : : {
887 : 12873434 : for (p = r->start; p <= r->finish; p++)
888 : 10916294 : if (hard_regno < 0)
889 : 5458147 : bitmap_clear_bit (&live_hard_reg_pseudos[p], regno);
890 : : else
891 : : {
892 : 5458147 : bitmap_set_bit (&live_hard_reg_pseudos[p], regno);
893 : 5458147 : insert_in_live_range_start_chain (regno);
894 : : }
895 : : }
896 : 1928670 : live_pseudos_reg_renumber[regno] = hard_regno;
897 : 1928670 : }
898 : :
899 : : /* Return true iff there is a reason why pseudo SPILL_REGNO should not
900 : : be spilled. */
901 : : static bool
902 : 287154 : must_not_spill_p (unsigned spill_regno)
903 : : {
904 : 287154 : if ((pic_offset_table_rtx != NULL
905 : 85163 : && spill_regno == REGNO (pic_offset_table_rtx))
906 : 366909 : || ((int) spill_regno >= lra_constraint_new_regno_start
907 : 29101 : && ! bitmap_bit_p (&lra_inheritance_pseudos, spill_regno)
908 : 14645 : && ! bitmap_bit_p (&lra_split_regs, spill_regno)
909 : 13003 : && ! bitmap_bit_p (&lra_subreg_reload_pseudos, spill_regno)
910 : 13003 : && ! bitmap_bit_p (&lra_optional_reload_pseudos, spill_regno)))
911 : 16567 : return true;
912 : : /* A reload pseudo that requires a singleton register class should
913 : : not be spilled.
914 : : FIXME: this mitigates the issue on certain i386 patterns, but
915 : : does not solve the general case where existing reloads fully
916 : : cover a limited register class. */
917 : 270587 : if (!bitmap_bit_p (&non_reload_pseudos, spill_regno)
918 : 252645 : && reg_class_size [reg_preferred_class (spill_regno)] == 1
919 : 285720 : && reg_alternate_class (spill_regno) == NO_REGS)
920 : : return true;
921 : : return false;
922 : : }
923 : :
924 : : /* Array used for sorting reload pseudos for subsequent allocation
925 : : after spilling some pseudo. */
926 : : static int *sorted_reload_pseudos;
927 : :
928 : : /* Spill some pseudos for a reload pseudo REGNO and return hard
929 : : register which should be used for pseudo after spilling. The
930 : : function adds spilled pseudos to SPILLED_PSEUDO_BITMAP. When we
931 : : choose hard register (and pseudos occupying the hard registers and
932 : : to be spilled), we take into account not only how REGNO will
933 : : benefit from the spills but also how other reload pseudos not yet
934 : : assigned to hard registers benefit from the spills too. In very
935 : : rare cases, the function can fail and return -1.
936 : :
937 : : If FIRST_P, return the first available hard reg ignoring other
938 : : criteria, e.g. allocation cost and cost of spilling non-reload
939 : : pseudos. This approach results in less hard reg pool fragmentation
940 : : and permit to allocate hard regs to reload pseudos in complicated
941 : : situations where pseudo sizes are different. */
942 : : static int
943 : 53307 : spill_for (int regno, bitmap spilled_pseudo_bitmap, bool first_p)
944 : : {
945 : 53307 : int i, j, n, p, hard_regno, best_hard_regno, cost, best_cost, rclass_size;
946 : 53307 : int reload_hard_regno, reload_cost;
947 : 53307 : bool static_p, best_static_p;
948 : 53307 : machine_mode mode;
949 : 53307 : enum reg_class rclass;
950 : 53307 : unsigned int spill_regno, reload_regno, uid;
951 : 53307 : int insn_pseudos_num, best_insn_pseudos_num;
952 : 53307 : int bad_spills_num, smallest_bad_spills_num;
953 : 53307 : lra_live_range_t r;
954 : 53307 : bitmap_iterator bi;
955 : :
956 : 53307 : rclass = regno_allocno_class_array[regno];
957 : 53307 : lra_assert (reg_renumber[regno] < 0 && rclass != NO_REGS);
958 : 53307 : bitmap_clear (&insn_conflict_pseudos);
959 : 53307 : bitmap_clear (&best_spill_pseudos_bitmap);
960 : 154759 : EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
961 : : {
962 : 101452 : struct lra_insn_reg *ir;
963 : :
964 : 345693 : for (ir = lra_get_insn_regs (uid); ir != NULL; ir = ir->next)
965 : 244241 : if (ir->regno >= FIRST_PSEUDO_REGISTER)
966 : 233004 : bitmap_set_bit (&insn_conflict_pseudos, ir->regno);
967 : : }
968 : 53307 : best_hard_regno = -1;
969 : 53307 : best_cost = INT_MAX;
970 : 53307 : best_static_p = true;
971 : 53307 : best_insn_pseudos_num = INT_MAX;
972 : 53307 : smallest_bad_spills_num = INT_MAX;
973 : 53307 : rclass_size = ira_class_hard_regs_num[rclass];
974 : 53307 : mode = PSEUDO_REGNO_MODE (regno);
975 : : /* Invalidate try_hard_reg_pseudos elements. */
976 : 53307 : curr_pseudo_check++;
977 : 109032 : for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
978 : 178636 : for (p = r->start; p <= r->finish; p++)
979 : 122911 : setup_try_hard_regno_pseudos (p, rclass);
980 : 380431 : for (i = 0; i < rclass_size; i++)
981 : : {
982 : 327124 : hard_regno = ira_class_hard_regs[rclass][i];
983 : 327124 : bitmap_clear (&spill_pseudos_bitmap);
984 : 668014 : for (j = hard_regno_nregs (hard_regno, mode) - 1; j >= 0; j--)
985 : : {
986 : 340890 : if (hard_regno + j >= FIRST_PSEUDO_REGISTER)
987 : : break;
988 : 340890 : if (try_hard_reg_pseudos_check[hard_regno + j] != curr_pseudo_check)
989 : 51105 : continue;
990 : 289785 : lra_assert (!bitmap_empty_p (&try_hard_reg_pseudos[hard_regno + j]));
991 : 289785 : bitmap_ior_into (&spill_pseudos_bitmap,
992 : 289785 : &try_hard_reg_pseudos[hard_regno + j]);
993 : : }
994 : : /* Spill pseudos. */
995 : 327124 : static_p = false;
996 : 596326 : EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
997 : 287154 : if (must_not_spill_p (spill_regno))
998 : 17952 : goto fail;
999 : 269202 : else if (non_spilled_static_chain_regno_p (spill_regno))
1000 : 0 : static_p = true;
1001 : 309172 : insn_pseudos_num = 0;
1002 : 309172 : bad_spills_num = 0;
1003 : 309172 : if (lra_dump_file != NULL)
1004 : 0 : fprintf (lra_dump_file, " Trying %d:", hard_regno);
1005 : 309172 : sparseset_clear (live_range_reload_inheritance_pseudos);
1006 : 578121 : EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
1007 : : {
1008 : 268949 : if (bitmap_bit_p (&insn_conflict_pseudos, spill_regno))
1009 : 44671 : insn_pseudos_num++;
1010 : 268949 : if (spill_regno >= (unsigned int) lra_bad_spill_regno_start)
1011 : 0 : bad_spills_num++;
1012 : 268949 : for (r = lra_reg_info[spill_regno].live_ranges;
1013 : 922825 : r != NULL;
1014 : 653876 : r = r->next)
1015 : : {
1016 : 53220444 : for (p = r->start; p <= r->finish; p++)
1017 : : {
1018 : 52566568 : lra_live_range_t r2;
1019 : :
1020 : 52566568 : for (r2 = start_point_ranges[p];
1021 : 90732845 : r2 != NULL;
1022 : 38166277 : r2 = r2->start_next)
1023 : 38166277 : if (r2->regno >= lra_constraint_new_regno_start)
1024 : 22202958 : sparseset_set_bit (live_range_reload_inheritance_pseudos,
1025 : : r2->regno);
1026 : : }
1027 : : }
1028 : : }
1029 : 309172 : n = 0;
1030 : 309172 : if (sparseset_cardinality (live_range_reload_inheritance_pseudos)
1031 : 309172 : <= (unsigned)param_lra_max_considered_reload_pseudos)
1032 : 13723180 : EXECUTE_IF_SET_IN_SPARSESET (live_range_reload_inheritance_pseudos,
1033 : : reload_regno)
1034 : 6557431 : if ((int) reload_regno != regno
1035 : 6313540 : && (ira_reg_classes_intersect_p
1036 : 6313540 : [rclass][regno_allocno_class_array[reload_regno]])
1037 : 5546278 : && live_pseudos_reg_renumber[reload_regno] < 0
1038 : 9830241 : && find_hard_regno_for (reload_regno, &cost, -1, first_p) < 0)
1039 : 1518266 : sorted_reload_pseudos[n++] = reload_regno;
1040 : 578121 : EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
1041 : : {
1042 : 268949 : update_lives (spill_regno, true);
1043 : 268949 : if (lra_dump_file != NULL)
1044 : 0 : fprintf (lra_dump_file, " spill %d(freq=%d)",
1045 : 0 : spill_regno, lra_reg_info[spill_regno].freq);
1046 : : }
1047 : 309172 : hard_regno = find_hard_regno_for (regno, &cost, -1, first_p);
1048 : 309172 : if (hard_regno >= 0)
1049 : : {
1050 : 260291 : assign_temporarily (regno, hard_regno);
1051 : 260291 : qsort (sorted_reload_pseudos, n, sizeof (int),
1052 : : reload_pseudo_compare_func);
1053 : 2031826 : for (j = 0; j < n; j++)
1054 : : {
1055 : 1511244 : reload_regno = sorted_reload_pseudos[j];
1056 : 1511244 : lra_assert (live_pseudos_reg_renumber[reload_regno] < 0);
1057 : 3022488 : if ((reload_hard_regno
1058 : 1511244 : = find_hard_regno_for (reload_regno,
1059 : : &reload_cost, -1, first_p)) >= 0)
1060 : : {
1061 : 704044 : if (lra_dump_file != NULL)
1062 : 0 : fprintf (lra_dump_file, " assign %d(cost=%d)",
1063 : : reload_regno, reload_cost);
1064 : 704044 : assign_temporarily (reload_regno, reload_hard_regno);
1065 : 704044 : cost += reload_cost;
1066 : : }
1067 : : }
1068 : 523911 : EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
1069 : : {
1070 : 263620 : rtx_insn_list *x;
1071 : :
1072 : 263620 : cost += lra_reg_info[spill_regno].freq;
1073 : 263620 : if (ira_reg_equiv[spill_regno].memory != NULL
1074 : 254983 : || ira_reg_equiv[spill_regno].constant != NULL)
1075 : 10113 : for (x = ira_reg_equiv[spill_regno].init_insns;
1076 : 18748 : x != NULL;
1077 : 8635 : x = x->next ())
1078 : 25517 : cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (x->insn ()));
1079 : : }
1080 : : /* Avoid spilling static chain pointer pseudo when non-local
1081 : : goto is used. */
1082 : 260291 : if ((! static_p && best_static_p)
1083 : 213707 : || (static_p == best_static_p
1084 : 213707 : && (best_insn_pseudos_num > insn_pseudos_num
1085 : 207038 : || (best_insn_pseudos_num == insn_pseudos_num
1086 : 188171 : && (bad_spills_num < smallest_bad_spills_num
1087 : 188171 : || (bad_spills_num == smallest_bad_spills_num
1088 : 188171 : && best_cost > cost))))))
1089 : : {
1090 : 90629 : best_insn_pseudos_num = insn_pseudos_num;
1091 : 90629 : smallest_bad_spills_num = bad_spills_num;
1092 : 90629 : best_static_p = static_p;
1093 : 90629 : best_cost = cost;
1094 : 90629 : best_hard_regno = hard_regno;
1095 : 90629 : bitmap_copy (&best_spill_pseudos_bitmap, &spill_pseudos_bitmap);
1096 : 90629 : if (lra_dump_file != NULL)
1097 : 0 : fprintf (lra_dump_file,
1098 : : " Now best %d(cost=%d, bad_spills=%d, insn_pseudos=%d)\n",
1099 : : hard_regno, cost, bad_spills_num, insn_pseudos_num);
1100 : : }
1101 : 260291 : assign_temporarily (regno, -1);
1102 : 2031826 : for (j = 0; j < n; j++)
1103 : : {
1104 : 1511244 : reload_regno = sorted_reload_pseudos[j];
1105 : 1511244 : if (live_pseudos_reg_renumber[reload_regno] >= 0)
1106 : 704044 : assign_temporarily (reload_regno, -1);
1107 : : }
1108 : : }
1109 : 309172 : if (lra_dump_file != NULL)
1110 : 0 : fprintf (lra_dump_file, "\n");
1111 : : /* Restore the live hard reg pseudo info for spilled pseudos. */
1112 : 578121 : EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
1113 : 268949 : update_lives (spill_regno, false);
1114 : 309172 : fail:
1115 : 327124 : ;
1116 : : }
1117 : : /* Spill: */
1118 : 100274 : EXECUTE_IF_SET_IN_BITMAP (&best_spill_pseudos_bitmap, 0, spill_regno, bi)
1119 : : {
1120 : 46967 : if ((int) spill_regno >= lra_constraint_new_regno_start)
1121 : 4230 : former_reload_pseudo_spill_p = true;
1122 : 46967 : if (lra_dump_file != NULL)
1123 : 0 : fprintf (lra_dump_file, " Spill %sr%d(hr=%d, freq=%d) for r%d\n",
1124 : : pseudo_prefix_title (spill_regno),
1125 : 0 : spill_regno, reg_renumber[spill_regno],
1126 : 0 : lra_reg_info[spill_regno].freq, regno);
1127 : 46967 : update_lives (spill_regno, true);
1128 : 46967 : lra_setup_reg_renumber (spill_regno, -1, false);
1129 : : }
1130 : 53307 : bitmap_ior_into (spilled_pseudo_bitmap, &best_spill_pseudos_bitmap);
1131 : 53307 : return best_hard_regno;
1132 : : }
1133 : :
1134 : : /* Assign HARD_REGNO to REGNO. */
1135 : : static void
1136 : 6487617 : assign_hard_regno (int hard_regno, int regno)
1137 : : {
1138 : 6487617 : int i;
1139 : :
1140 : 6487617 : lra_assert (hard_regno >= 0);
1141 : 6487617 : lra_setup_reg_renumber (regno, hard_regno, true);
1142 : 6487617 : update_lives (regno, false);
1143 : 13170507 : for (i = 0;
1144 : 13170507 : i < hard_regno_nregs (hard_regno, lra_reg_info[regno].biggest_mode);
1145 : : i++)
1146 : 6682890 : df_set_regs_ever_live (hard_regno + i, true);
1147 : 6487617 : }
1148 : :
1149 : : /* Array used for sorting different pseudos. */
1150 : : static int *sorted_pseudos;
1151 : :
1152 : : /* The constraints pass is allowed to create equivalences between
1153 : : pseudos that make the current allocation "incorrect" (in the sense
1154 : : that pseudos are assigned to hard registers from their own conflict
1155 : : sets). The global variable check_and_force_assignment_correctness_p says
1156 : : whether this might have happened.
1157 : :
1158 : : Process pseudos assigned to hard registers (less frequently used
1159 : : first), spill if a conflict is found, and mark the spilled pseudos
1160 : : in SPILLED_PSEUDO_BITMAP. Set up LIVE_HARD_REG_PSEUDOS from
1161 : : pseudos, assigned to hard registers. */
1162 : : static void
1163 : 1547247 : setup_live_pseudos_and_spill_after_risky_transforms (bitmap
1164 : : spilled_pseudo_bitmap)
1165 : : {
1166 : 1547247 : int p, i, j, n, regno, hard_regno, biggest_nregs, nregs_diff;
1167 : 1547247 : unsigned int k, conflict_regno;
1168 : 1547247 : poly_int64 offset;
1169 : 1547247 : int val;
1170 : 1547247 : HARD_REG_SET conflict_set;
1171 : 1547247 : machine_mode mode, biggest_mode;
1172 : 1547247 : lra_live_range_t r;
1173 : 1547247 : bitmap_iterator bi;
1174 : 1547247 : int max_regno = max_reg_num ();
1175 : :
1176 : 1547247 : if (! check_and_force_assignment_correctness_p)
1177 : : {
1178 : 20989558 : for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1179 : 20930462 : if (reg_renumber[i] >= 0 && lra_reg_info[i].nrefs > 0)
1180 : 10098265 : update_lives (i, false);
1181 : 59096 : return;
1182 : : }
1183 : 81349213 : for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1184 : 79861062 : if ((pic_offset_table_rtx == NULL_RTX
1185 : 7196991 : || i != (int) REGNO (pic_offset_table_rtx))
1186 : 87009134 : && (hard_regno = reg_renumber[i]) >= 0 && lra_reg_info[i].nrefs > 0)
1187 : : {
1188 : 31449964 : biggest_mode = lra_reg_info[i].biggest_mode;
1189 : 31449964 : biggest_nregs = hard_regno_nregs (hard_regno, biggest_mode);
1190 : 31449964 : nregs_diff = (biggest_nregs
1191 : 31449964 : - hard_regno_nregs (hard_regno, PSEUDO_REGNO_MODE (i)));
1192 : 31449964 : enum reg_class rclass = lra_get_allocno_class (i);
1193 : :
1194 : 31449964 : if ((WORDS_BIG_ENDIAN
1195 : : && (hard_regno - nregs_diff < 0
1196 : : || !TEST_HARD_REG_BIT (reg_class_contents[rclass],
1197 : : hard_regno - nregs_diff)))
1198 : : || (!WORDS_BIG_ENDIAN
1199 : 31449964 : && (hard_regno + nregs_diff >= FIRST_PSEUDO_REGISTER
1200 : 31449964 : || !TEST_HARD_REG_BIT (reg_class_contents[rclass],
1201 : : hard_regno + nregs_diff))))
1202 : : {
1203 : : /* Hard registers of paradoxical sub-registers are out of
1204 : : range of pseudo register class. Spill the pseudo. */
1205 : 0 : reg_renumber[i] = -1;
1206 : 0 : continue;
1207 : : }
1208 : 31449964 : sorted_pseudos[n++] = i;
1209 : : }
1210 : 1488151 : qsort (sorted_pseudos, n, sizeof (int), pseudo_compare_func);
1211 : 1488151 : if (pic_offset_table_rtx != NULL_RTX
1212 : 48919 : && (regno = REGNO (pic_offset_table_rtx)) >= FIRST_PSEUDO_REGISTER
1213 : 1537070 : && reg_renumber[regno] >= 0 && lra_reg_info[regno].nrefs > 0)
1214 : 42893 : sorted_pseudos[n++] = regno;
1215 : 32981008 : for (i = n - 1; i >= 0; i--)
1216 : : {
1217 : 31492857 : regno = sorted_pseudos[i];
1218 : 31492857 : hard_regno = reg_renumber[regno];
1219 : 31492857 : lra_assert (hard_regno >= 0);
1220 : 31492857 : mode = lra_reg_info[regno].biggest_mode;
1221 : 31492857 : sparseset_clear (live_range_hard_reg_pseudos);
1222 : 68906895 : for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
1223 : : {
1224 : 86199720 : EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[r->start], 0, k, bi)
1225 : 48785682 : sparseset_set_bit (live_range_hard_reg_pseudos, k);
1226 : 274418664 : for (p = r->start + 1; p <= r->finish; p++)
1227 : : {
1228 : 237004626 : lra_live_range_t r2;
1229 : :
1230 : 237004626 : for (r2 = start_point_ranges[p];
1231 : 364835921 : r2 != NULL;
1232 : 127831295 : r2 = r2->start_next)
1233 : 127831295 : if (live_pseudos_reg_renumber[r2->regno] >= 0)
1234 : 68727791 : sparseset_set_bit (live_range_hard_reg_pseudos, r2->regno);
1235 : : }
1236 : : }
1237 : 31492857 : conflict_set = lra_no_alloc_regs;
1238 : 31492857 : conflict_set |= lra_reg_info[regno].conflict_hard_regs;
1239 : 31492857 : val = lra_reg_info[regno].val;
1240 : 31492857 : offset = lra_reg_info[regno].offset;
1241 : 127762088 : EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos, conflict_regno)
1242 : 96269231 : if (!lra_reg_val_equal_p (conflict_regno, val, offset)
1243 : : /* If it is multi-register pseudos they should start on
1244 : : the same hard register. */
1245 : 82818 : || hard_regno != reg_renumber[conflict_regno])
1246 : : {
1247 : 96196957 : int conflict_hard_regno = reg_renumber[conflict_regno];
1248 : :
1249 : 96196957 : biggest_mode = lra_reg_info[conflict_regno].biggest_mode;
1250 : 96196957 : biggest_nregs = hard_regno_nregs (conflict_hard_regno,
1251 : : biggest_mode);
1252 : 96196957 : nregs_diff
1253 : 96196957 : = (biggest_nregs
1254 : 96196957 : - hard_regno_nregs (conflict_hard_regno,
1255 : 96196957 : PSEUDO_REGNO_MODE (conflict_regno)));
1256 : 96196957 : add_to_hard_reg_set (&conflict_set,
1257 : : biggest_mode,
1258 : : conflict_hard_regno
1259 : : - (WORDS_BIG_ENDIAN ? nregs_diff : 0));
1260 : : }
1261 : 31492857 : if (! overlaps_hard_reg_set_p (conflict_set, mode, hard_regno))
1262 : : {
1263 : 31478683 : update_lives (regno, false);
1264 : 31478683 : continue;
1265 : : }
1266 : 14174 : bitmap_set_bit (spilled_pseudo_bitmap, regno);
1267 : 14174 : for (j = 0;
1268 : 38337 : j < hard_regno_nregs (hard_regno, PSEUDO_REGNO_MODE (regno));
1269 : : j++)
1270 : 24163 : lra_hard_reg_usage[hard_regno + j] -= lra_reg_info[regno].freq;
1271 : 14174 : reg_renumber[regno] = -1;
1272 : 14174 : if (regno >= lra_constraint_new_regno_start)
1273 : 0 : former_reload_pseudo_spill_p = true;
1274 : 14174 : if (lra_dump_file != NULL)
1275 : 0 : fprintf (lra_dump_file, " Spill r%d after risky transformations\n",
1276 : : regno);
1277 : : }
1278 : : }
1279 : :
1280 : : /* Improve allocation by assigning the same hard regno of inheritance
1281 : : pseudos to the connected pseudos. We need this because inheritance
1282 : : pseudos are allocated after reload pseudos in the thread and when
1283 : : we assign a hard register to a reload pseudo we don't know yet that
1284 : : the connected inheritance pseudos can get the same hard register.
1285 : : Add pseudos with changed allocation to bitmap CHANGED_PSEUDOS. */
1286 : : static void
1287 : 1547247 : improve_inheritance (bitmap changed_pseudos)
1288 : : {
1289 : 1547247 : unsigned int k;
1290 : 1547247 : int regno, another_regno, hard_regno, another_hard_regno, cost, i, n;
1291 : 1547247 : lra_copy_t cp, next_cp;
1292 : 1547247 : bitmap_iterator bi;
1293 : :
1294 : 1547247 : if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
1295 : 4130 : return;
1296 : 1543117 : n = 0;
1297 : 3421015 : EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, k, bi)
1298 : 1877898 : if (reg_renumber[k] >= 0 && lra_reg_info[k].nrefs != 0)
1299 : 1267330 : sorted_pseudos[n++] = k;
1300 : 1543117 : qsort (sorted_pseudos, n, sizeof (int), pseudo_compare_func);
1301 : 4353564 : for (i = 0; i < n; i++)
1302 : : {
1303 : 1267330 : regno = sorted_pseudos[i];
1304 : 1267330 : hard_regno = reg_renumber[regno];
1305 : 1267330 : lra_assert (hard_regno >= 0);
1306 : 3670013 : for (cp = lra_reg_info[regno].copies; cp != NULL; cp = next_cp)
1307 : : {
1308 : 2402683 : if (cp->regno1 == regno)
1309 : : {
1310 : 417907 : next_cp = cp->regno1_next;
1311 : 417907 : another_regno = cp->regno2;
1312 : : }
1313 : 1984776 : else if (cp->regno2 == regno)
1314 : : {
1315 : 1984776 : next_cp = cp->regno2_next;
1316 : 1984776 : another_regno = cp->regno1;
1317 : : }
1318 : : else
1319 : 0 : gcc_unreachable ();
1320 : : /* Don't change reload pseudo allocation. It might have
1321 : : this allocation for a purpose and changing it can result
1322 : : in LRA cycling. */
1323 : 2402683 : if ((another_regno < lra_constraint_new_regno_start
1324 : 2402683 : || bitmap_bit_p (&lra_inheritance_pseudos, another_regno))
1325 : 858532 : && (another_hard_regno = reg_renumber[another_regno]) >= 0
1326 : 3149809 : && another_hard_regno != hard_regno)
1327 : : {
1328 : 70706 : if (lra_dump_file != NULL)
1329 : 1 : fprintf
1330 : 1 : (lra_dump_file,
1331 : : " Improving inheritance for %d(%d) and %d(%d)...\n",
1332 : : regno, hard_regno, another_regno, another_hard_regno);
1333 : 70706 : update_lives (another_regno, true);
1334 : 70706 : lra_setup_reg_renumber (another_regno, -1, false);
1335 : 70706 : if (hard_regno == find_hard_regno_for (another_regno, &cost,
1336 : : hard_regno, false))
1337 : 39841 : assign_hard_regno (hard_regno, another_regno);
1338 : : else
1339 : 30865 : assign_hard_regno (another_hard_regno, another_regno);
1340 : 70706 : bitmap_set_bit (changed_pseudos, another_regno);
1341 : : }
1342 : : }
1343 : : }
1344 : : }
1345 : :
1346 : :
1347 : : /* Bitmap finally containing all pseudos spilled on this assignment
1348 : : pass. */
1349 : : static bitmap_head all_spilled_pseudos;
1350 : : /* All pseudos whose allocation was changed. */
1351 : : static bitmap_head changed_pseudo_bitmap;
1352 : :
1353 : :
1354 : : /* Add to LIVE_RANGE_HARD_REG_PSEUDOS all pseudos conflicting with
1355 : : REGNO and whose hard regs can be assigned to REGNO. */
1356 : : static void
1357 : 3882 : find_all_spills_for (int regno)
1358 : : {
1359 : 3882 : int p;
1360 : 3882 : lra_live_range_t r;
1361 : 3882 : unsigned int k;
1362 : 3882 : bitmap_iterator bi;
1363 : 3882 : enum reg_class rclass;
1364 : 3882 : bool *rclass_intersect_p;
1365 : :
1366 : 3882 : rclass = regno_allocno_class_array[regno];
1367 : 3882 : rclass_intersect_p = ira_reg_classes_intersect_p[rclass];
1368 : 8962 : for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
1369 : : {
1370 : 20363 : EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[r->start], 0, k, bi)
1371 : 15283 : if (rclass_intersect_p[regno_allocno_class_array[k]])
1372 : 8648 : sparseset_set_bit (live_range_hard_reg_pseudos, k);
1373 : 9896 : for (p = r->start + 1; p <= r->finish; p++)
1374 : : {
1375 : 4816 : lra_live_range_t r2;
1376 : :
1377 : 4816 : for (r2 = start_point_ranges[p];
1378 : 10307 : r2 != NULL;
1379 : 5491 : r2 = r2->start_next)
1380 : : {
1381 : 5491 : if (live_pseudos_reg_renumber[r2->regno] >= 0
1382 : 5424 : && ! sparseset_bit_p (live_range_hard_reg_pseudos, r2->regno)
1383 : 10900 : && rclass_intersect_p[regno_allocno_class_array[r2->regno]])
1384 : 5406 : sparseset_set_bit (live_range_hard_reg_pseudos, r2->regno);
1385 : : }
1386 : : }
1387 : : }
1388 : 3882 : }
1389 : :
1390 : : /* Assign hard registers to reload pseudos and other pseudos. Return
1391 : : true if we was not able to assign hard registers to all reload
1392 : : pseudos. */
1393 : : static bool
1394 : 1547247 : assign_by_spills (void)
1395 : : {
1396 : 1547247 : int i, n, nfails, iter, regno, regno2, hard_regno, cost;
1397 : 1547247 : rtx restore_rtx;
1398 : 1547247 : bitmap_head changed_insns, do_not_assign_nonreload_pseudos;
1399 : 1547247 : unsigned int u, conflict_regno;
1400 : 1547247 : bitmap_iterator bi;
1401 : 1547247 : bool reload_p, fails_p = false;
1402 : 1547247 : int max_regno = max_reg_num ();
1403 : :
1404 : 13532010 : for (n = 0, i = lra_constraint_new_regno_start; i < max_regno; i++)
1405 : 11984763 : if (reg_renumber[i] < 0 && lra_reg_info[i].nrefs != 0
1406 : 7687093 : && regno_allocno_class_array[i] != NO_REGS)
1407 : 6751659 : sorted_pseudos[n++] = i;
1408 : 1547247 : bitmap_initialize (&insn_conflict_pseudos, ®_obstack);
1409 : 1547247 : bitmap_initialize (&spill_pseudos_bitmap, ®_obstack);
1410 : 1547247 : bitmap_initialize (&best_spill_pseudos_bitmap, ®_obstack);
1411 : 1547247 : update_hard_regno_preference_check = XCNEWVEC (int, max_regno);
1412 : 1547247 : curr_update_hard_regno_preference_check = 0;
1413 : 1547247 : memset (try_hard_reg_pseudos_check, 0, sizeof (try_hard_reg_pseudos_check));
1414 : 143893971 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1415 : 142346724 : bitmap_initialize (&try_hard_reg_pseudos[i], ®_obstack);
1416 : 1547247 : curr_pseudo_check = 0;
1417 : 1547247 : bitmap_initialize (&changed_insns, ®_obstack);
1418 : 1547247 : bitmap_initialize (&non_reload_pseudos, ®_obstack);
1419 : 1547247 : bitmap_ior (&non_reload_pseudos, &lra_inheritance_pseudos, &lra_split_regs);
1420 : 1547247 : bitmap_ior_into (&non_reload_pseudos, &lra_subreg_reload_pseudos);
1421 : 1547247 : bitmap_ior_into (&non_reload_pseudos, &lra_optional_reload_pseudos);
1422 : 1547247 : for (iter = 0; iter <= 1; iter++)
1423 : : {
1424 : 1548745 : qsort (sorted_pseudos, n, sizeof (int), reload_pseudo_compare_func);
1425 : 1548745 : nfails = 0;
1426 : 8309835 : for (i = 0; i < n; i++)
1427 : : {
1428 : 6761090 : regno = sorted_pseudos[i];
1429 : 6761090 : if (reg_renumber[regno] >= 0)
1430 : 1252 : continue;
1431 : 6759838 : if (lra_dump_file != NULL)
1432 : 194 : fprintf (lra_dump_file, " Assigning to %d "
1433 : : "(cl=%s, orig=%d, freq=%d, tfirst=%d, tfreq=%d)...\n",
1434 : 97 : regno, reg_class_names[regno_allocno_class_array[regno]],
1435 : 97 : ORIGINAL_REGNO (regno_reg_rtx[regno]),
1436 : 97 : lra_reg_info[regno].freq, regno_assign_info[regno].first,
1437 : 97 : regno_assign_info[regno_assign_info[regno].first].freq);
1438 : 6759838 : hard_regno = find_hard_regno_for (regno, &cost, -1, iter == 1);
1439 : 6759838 : reload_p = ! bitmap_bit_p (&non_reload_pseudos, regno);
1440 : 6759838 : if (hard_regno < 0 && reload_p)
1441 : 53307 : hard_regno = spill_for (regno, &all_spilled_pseudos, iter == 1);
1442 : 6759838 : if (hard_regno < 0)
1443 : : {
1444 : 468849 : if (reload_p)
1445 : : {
1446 : : /* Put unassigned reload pseudo first in the array. */
1447 : 6723 : regno2 = sorted_pseudos[nfails];
1448 : 6723 : sorted_pseudos[nfails++] = regno;
1449 : 6723 : sorted_pseudos[i] = regno2;
1450 : : }
1451 : : else
1452 : : {
1453 : : /* Consider all alternatives on the next constraint
1454 : : subpass. */
1455 : 462126 : bitmap_set_bit (&all_spilled_pseudos, regno);
1456 : : }
1457 : : }
1458 : : else
1459 : : {
1460 : : /* This register might have been spilled by the previous
1461 : : pass. Indicate that it is no longer spilled. */
1462 : 6290989 : bitmap_clear_bit (&all_spilled_pseudos, regno);
1463 : 6290989 : assign_hard_regno (hard_regno, regno);
1464 : 6290989 : if (! reload_p || regno_allocno_class_array[regno] == ALL_REGS)
1465 : : /* As non-reload pseudo assignment is changed we should
1466 : : reconsider insns referring for the pseudo. Do the same if a
1467 : : reload pseudo did not refine its class which can happens
1468 : : when the pseudo occurs only in reload insns. */
1469 : 1392872 : bitmap_set_bit (&changed_pseudo_bitmap, regno);
1470 : : }
1471 : : }
1472 : 1548745 : if (nfails == 0 || iter > 0)
1473 : : {
1474 : 1547247 : fails_p = nfails != 0;
1475 : 1547247 : break;
1476 : : }
1477 : : /* This is a very rare event. We cannot assign a hard register
1478 : : to reload pseudo because the hard register was assigned to
1479 : : another reload pseudo on a previous assignment pass. For x86
1480 : : example, on the 1st pass we assigned CX (although another
1481 : : hard register could be used for this) to reload pseudo in an
1482 : : insn, on the 2nd pass we need CX (and only this) hard
1483 : : register for a new reload pseudo in the same insn. Another
1484 : : possible situation may occur in assigning to multi-regs
1485 : : reload pseudos when hard regs pool is too fragmented even
1486 : : after spilling non-reload pseudos.
1487 : :
1488 : : We should do something radical here to succeed. Here we
1489 : : spill *all* conflicting pseudos and reassign them. */
1490 : 1498 : if (lra_dump_file != NULL)
1491 : 0 : fprintf (lra_dump_file, " 2nd iter for reload pseudo assignments:\n");
1492 : 1498 : sparseset_clear (live_range_hard_reg_pseudos);
1493 : 5380 : for (i = 0; i < nfails; i++)
1494 : : {
1495 : 3882 : if (lra_dump_file != NULL)
1496 : 0 : fprintf (lra_dump_file, " Reload r%d assignment failure\n",
1497 : 0 : sorted_pseudos[i]);
1498 : 3882 : find_all_spills_for (sorted_pseudos[i]);
1499 : : }
1500 : 19598 : EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos, conflict_regno)
1501 : : {
1502 : 9050 : if ((int) conflict_regno >= lra_constraint_new_regno_start)
1503 : : {
1504 : 2384 : sorted_pseudos[nfails++] = conflict_regno;
1505 : 2384 : former_reload_pseudo_spill_p = true;
1506 : : }
1507 : : else
1508 : : /* It is better to do reloads before spilling as after the
1509 : : spill-subpass we will reload memory instead of pseudos
1510 : : and this will make reusing reload pseudos more
1511 : : complicated. Going directly to the spill pass in such
1512 : : case might result in worse code performance or even LRA
1513 : : cycling if we have few registers. */
1514 : 6666 : bitmap_set_bit (&all_spilled_pseudos, conflict_regno);
1515 : 9050 : if (lra_dump_file != NULL)
1516 : 0 : fprintf (lra_dump_file, " Spill %s r%d(hr=%d, freq=%d)\n",
1517 : : pseudo_prefix_title (conflict_regno), conflict_regno,
1518 : 0 : reg_renumber[conflict_regno],
1519 : 0 : lra_reg_info[conflict_regno].freq);
1520 : 9050 : update_lives (conflict_regno, true);
1521 : 9050 : lra_setup_reg_renumber (conflict_regno, -1, false);
1522 : : }
1523 : 1498 : if (n < nfails)
1524 : : n = nfails;
1525 : : }
1526 : 1547247 : improve_inheritance (&changed_pseudo_bitmap);
1527 : 1547247 : bitmap_clear (&non_reload_pseudos);
1528 : 1547247 : bitmap_clear (&changed_insns);
1529 : 1547247 : if (! lra_simple_p)
1530 : : {
1531 : : /* We should not assign to original pseudos of inheritance
1532 : : pseudos or split pseudos if any its inheritance pseudo did
1533 : : not get hard register or any its split pseudo was not split
1534 : : because undo inheritance/split pass will extend live range of
1535 : : such inheritance or split pseudos. */
1536 : 1547241 : bitmap_initialize (&do_not_assign_nonreload_pseudos, ®_obstack);
1537 : 3607149 : EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, u, bi)
1538 : 2059908 : if ((restore_rtx = lra_reg_info[u].restore_rtx) != NULL_RTX
1539 : 1180906 : && REG_P (restore_rtx)
1540 : 1159005 : && reg_renumber[u] < 0
1541 : 2492346 : && bitmap_bit_p (&lra_inheritance_pseudos, u))
1542 : 432438 : bitmap_set_bit (&do_not_assign_nonreload_pseudos, REGNO (restore_rtx));
1543 : 2493925 : EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, u, bi)
1544 : 946684 : if ((restore_rtx = lra_reg_info[u].restore_rtx) != NULL_RTX
1545 : 946684 : && reg_renumber[u] >= 0)
1546 : : {
1547 : 800 : lra_assert (REG_P (restore_rtx));
1548 : 800 : bitmap_set_bit (&do_not_assign_nonreload_pseudos, REGNO (restore_rtx));
1549 : : }
1550 : 102108502 : for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1551 : 100561261 : if (((i < lra_constraint_new_regno_start
1552 : 88580192 : && ! bitmap_bit_p (&do_not_assign_nonreload_pseudos, i))
1553 : 12276758 : || (bitmap_bit_p (&lra_inheritance_pseudos, i)
1554 : 2059908 : && lra_reg_info[i].restore_rtx != NULL_RTX)
1555 : 11095852 : || (bitmap_bit_p (&lra_split_regs, i)
1556 : 946684 : && lra_reg_info[i].restore_rtx != NULL_RTX)
1557 : 10454972 : || bitmap_bit_p (&lra_subreg_reload_pseudos, i)
1558 : 10454512 : || bitmap_bit_p (&lra_optional_reload_pseudos, i))
1559 : 91256092 : && reg_renumber[i] < 0 && lra_reg_info[i].nrefs != 0
1560 : 102916315 : && regno_allocno_class_array[i] != NO_REGS)
1561 : 1676537 : sorted_pseudos[n++] = i;
1562 : 1547241 : bitmap_clear (&do_not_assign_nonreload_pseudos);
1563 : 1547241 : if (n != 0 && lra_dump_file != NULL)
1564 : 2 : fprintf (lra_dump_file, " Reassigning non-reload pseudos\n");
1565 : 1547241 : qsort (sorted_pseudos, n, sizeof (int), pseudo_compare_func);
1566 : 3223778 : for (i = 0; i < n; i++)
1567 : : {
1568 : 1676537 : regno = sorted_pseudos[i];
1569 : 1676537 : hard_regno = find_hard_regno_for (regno, &cost, -1, false);
1570 : 1676537 : if (hard_regno >= 0)
1571 : : {
1572 : 125922 : assign_hard_regno (hard_regno, regno);
1573 : : /* We change allocation for non-reload pseudo on this
1574 : : iteration -- mark the pseudo for invalidation of used
1575 : : alternatives of insns containing the pseudo. */
1576 : 125922 : bitmap_set_bit (&changed_pseudo_bitmap, regno);
1577 : : }
1578 : : else
1579 : : {
1580 : 1550615 : enum reg_class rclass = lra_get_allocno_class (regno);
1581 : 1550615 : enum reg_class spill_class;
1582 : :
1583 : 3101230 : if (targetm.spill_class == NULL
1584 : 1550615 : || lra_reg_info[regno].restore_rtx == NULL_RTX
1585 : 446362 : || ! bitmap_bit_p (&lra_inheritance_pseudos, regno)
1586 : 1550615 : || (spill_class
1587 : 426762 : = ((enum reg_class)
1588 : 426762 : targetm.spill_class
1589 : 426762 : ((reg_class_t) rclass,
1590 : 426762 : PSEUDO_REGNO_MODE (regno)))) == NO_REGS)
1591 : 1550615 : continue;
1592 : 0 : regno_allocno_class_array[regno] = spill_class;
1593 : 0 : hard_regno = find_hard_regno_for (regno, &cost, -1, false);
1594 : 0 : if (hard_regno < 0)
1595 : 0 : regno_allocno_class_array[regno] = rclass;
1596 : : else
1597 : : {
1598 : 0 : setup_reg_classes
1599 : 0 : (regno, spill_class, spill_class, spill_class);
1600 : 0 : assign_hard_regno (hard_regno, regno);
1601 : 0 : bitmap_set_bit (&changed_pseudo_bitmap, regno);
1602 : : }
1603 : : }
1604 : : }
1605 : : }
1606 : 1547247 : free (update_hard_regno_preference_check);
1607 : 1547247 : bitmap_clear (&best_spill_pseudos_bitmap);
1608 : 1547247 : bitmap_clear (&spill_pseudos_bitmap);
1609 : 1547247 : bitmap_clear (&insn_conflict_pseudos);
1610 : 1547247 : return fails_p;
1611 : : }
1612 : :
1613 : : /* Entry function to assign hard registers to new reload pseudos
1614 : : starting with LRA_CONSTRAINT_NEW_REGNO_START (by possible spilling
1615 : : of old pseudos) and possibly to the old pseudos. The function adds
1616 : : what insns to process for the next constraint pass. Those are all
1617 : : insns who contains non-reload and non-inheritance pseudos with
1618 : : changed allocation.
1619 : :
1620 : : Return true if we did not spill any non-reload and non-inheritance
1621 : : pseudos. Set up FAILS_P if we failed to assign hard registers to
1622 : : all reload pseudos. */
1623 : : bool
1624 : 1547247 : lra_assign (bool &fails_p)
1625 : : {
1626 : 1547247 : int i;
1627 : 1547247 : unsigned int u;
1628 : 1547247 : bitmap_iterator bi;
1629 : 1547247 : bitmap_head insns_to_process;
1630 : 1547247 : bool no_spills_p;
1631 : 1547247 : int max_regno = max_reg_num ();
1632 : :
1633 : 1547247 : timevar_push (TV_LRA_ASSIGN);
1634 : 1547247 : lra_assignment_iter++;
1635 : 1547247 : if (lra_dump_file != NULL)
1636 : 97 : fprintf (lra_dump_file, "\n********** Assignment #%d: **********\n\n",
1637 : : lra_assignment_iter);
1638 : 1547247 : init_lives ();
1639 : 1547247 : sorted_pseudos = XNEWVEC (int, max_regno);
1640 : 1547247 : sorted_reload_pseudos = XNEWVEC (int, max_regno);
1641 : 1547247 : regno_allocno_class_array = XNEWVEC (enum reg_class, max_regno);
1642 : 1547247 : regno_live_length = XNEWVEC (int, max_regno);
1643 : 102338771 : for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1644 : : {
1645 : 100791524 : int l;
1646 : 100791524 : lra_live_range_t r;
1647 : :
1648 : 100791524 : regno_allocno_class_array[i] = lra_get_allocno_class (i);
1649 : 161935947 : for (l = 0, r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
1650 : 61144423 : l += r->finish - r->start + 1;
1651 : 100791524 : regno_live_length[i] = l;
1652 : : }
1653 : 1547247 : former_reload_pseudo_spill_p = false;
1654 : 1547247 : init_regno_assign_info ();
1655 : 1547247 : bitmap_initialize (&all_spilled_pseudos, ®_obstack);
1656 : 1547247 : create_live_range_start_chains ();
1657 : 1547247 : setup_live_pseudos_and_spill_after_risky_transforms (&all_spilled_pseudos);
1658 : 1547247 : if (! lra_hard_reg_split_p && ! lra_asm_error_p && flag_checking)
1659 : : /* Check correctness of allocation but only when there are no hard reg
1660 : : splits and asm errors as in the case of errors explicit insns involving
1661 : : hard regs are added or the asm is removed and this can result in
1662 : : incorrect allocation. */
1663 : 101611864 : for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1664 : 100066042 : if (lra_reg_info[i].nrefs != 0
1665 : 50208324 : && reg_renumber[i] >= 0
1666 : 100066042 : && overlaps_hard_reg_set_p (lra_reg_info[i].conflict_hard_regs,
1667 : 41227741 : PSEUDO_REGNO_MODE (i), reg_renumber[i]))
1668 : 0 : gcc_unreachable ();
1669 : : /* Setup insns to process on the next constraint pass. */
1670 : 1547247 : bitmap_initialize (&changed_pseudo_bitmap, ®_obstack);
1671 : 1547247 : init_live_reload_and_inheritance_pseudos ();
1672 : 1547247 : fails_p = assign_by_spills ();
1673 : 1547247 : finish_live_reload_and_inheritance_pseudos ();
1674 : 1547247 : bitmap_ior_into (&changed_pseudo_bitmap, &all_spilled_pseudos);
1675 : 1547247 : no_spills_p = true;
1676 : 1822857 : EXECUTE_IF_SET_IN_BITMAP (&all_spilled_pseudos, 0, u, bi)
1677 : : /* We ignore spilled pseudos created on last inheritance pass
1678 : : because they will be removed. */
1679 : 304222 : if (lra_reg_info[u].restore_rtx == NULL_RTX)
1680 : : {
1681 : : no_spills_p = false;
1682 : : break;
1683 : : }
1684 : 1547247 : finish_live_range_start_chains ();
1685 : 1547247 : bitmap_clear (&all_spilled_pseudos);
1686 : 1547247 : bitmap_initialize (&insns_to_process, ®_obstack);
1687 : 3558454 : EXECUTE_IF_SET_IN_BITMAP (&changed_pseudo_bitmap, 0, u, bi)
1688 : 2011207 : bitmap_ior_into (&insns_to_process, &lra_reg_info[u].insn_bitmap);
1689 : 1547247 : bitmap_clear (&changed_pseudo_bitmap);
1690 : 6365807 : EXECUTE_IF_SET_IN_BITMAP (&insns_to_process, 0, u, bi)
1691 : : {
1692 : 4818560 : lra_push_insn_by_uid (u);
1693 : : /* Invalidate alternatives for insn should be processed. */
1694 : 4818560 : lra_set_used_insn_alternative_by_uid (u, -1);
1695 : : }
1696 : 1547247 : bitmap_clear (&insns_to_process);
1697 : 1547247 : finish_regno_assign_info ();
1698 : 1547247 : free (regno_live_length);
1699 : 1547247 : free (regno_allocno_class_array);
1700 : 1547247 : free (sorted_pseudos);
1701 : 1547247 : free (sorted_reload_pseudos);
1702 : 1547247 : finish_lives ();
1703 : 1547247 : timevar_pop (TV_LRA_ASSIGN);
1704 : 1547247 : if (former_reload_pseudo_spill_p)
1705 : 1949 : lra_assignment_iter_after_spill++;
1706 : : /* This is conditional on flag_checking because valid code can take
1707 : : more than this maximum number of iteration, but at the same time
1708 : : the test can uncover errors in machine descriptions. */
1709 : 1547247 : if (flag_checking
1710 : 1547227 : && (lra_assignment_iter_after_spill
1711 : 1547227 : > LRA_MAX_ASSIGNMENT_ITERATION_NUMBER))
1712 : 0 : internal_error
1713 : 0 : ("maximum number of LRA assignment passes is achieved (%d)",
1714 : : LRA_MAX_ASSIGNMENT_ITERATION_NUMBER);
1715 : : /* Reset the assignment correctness flag: */
1716 : 1547247 : check_and_force_assignment_correctness_p = false;
1717 : 1547247 : return no_spills_p;
1718 : : }
1719 : :
1720 : : /* Find start and finish insns for reload pseudo REGNO. Return true
1721 : : if we managed to find the expected insns. Return false,
1722 : : otherwise. */
1723 : : static bool
1724 : 2841 : find_reload_regno_insns (int regno, rtx_insn * &start, rtx_insn * &finish)
1725 : : {
1726 : 2841 : unsigned int uid;
1727 : 2841 : bitmap_iterator bi;
1728 : 2841 : int insns_num = 0;
1729 : 2841 : bool clobber_p = false;
1730 : 2841 : rtx_insn *prev_insn, *next_insn;
1731 : 2841 : rtx_insn *start_insn = NULL, *first_insn = NULL, *second_insn = NULL;
1732 : :
1733 : 9581 : EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
1734 : : {
1735 : 6740 : if (start_insn == NULL)
1736 : 2841 : start_insn = lra_insn_recog_data[uid]->insn;
1737 : 6740 : if (GET_CODE (PATTERN (lra_insn_recog_data[uid]->insn)) == CLOBBER)
1738 : : clobber_p = true;
1739 : : else
1740 : 6740 : insns_num++;
1741 : : }
1742 : : /* For reload pseudo we should have at most 3 insns besides clobber referring for
1743 : : it: input/output reload insns and the original insn. */
1744 : 2841 : if (insns_num > 3)
1745 : : return false;
1746 : 2841 : if (clobber_p)
1747 : 0 : insns_num++;
1748 : 2841 : if (insns_num > 1)
1749 : : {
1750 : 2683 : for (prev_insn = PREV_INSN (start_insn),
1751 : 2683 : next_insn = NEXT_INSN (start_insn);
1752 : 116516 : insns_num != 1 && (prev_insn != NULL
1753 : 1197 : || (next_insn != NULL && second_insn == NULL)); )
1754 : : {
1755 : : if (prev_insn != NULL)
1756 : : {
1757 : 113833 : if (bitmap_bit_p (&lra_reg_info[regno].insn_bitmap,
1758 : 113833 : INSN_UID (prev_insn)))
1759 : : {
1760 : 906 : first_insn = prev_insn;
1761 : 906 : insns_num--;
1762 : : }
1763 : 113833 : prev_insn = PREV_INSN (prev_insn);
1764 : : }
1765 : 113833 : if (next_insn != NULL && second_insn == NULL)
1766 : : {
1767 : 4053 : if (! bitmap_bit_p (&lra_reg_info[regno].insn_bitmap,
1768 : 4053 : INSN_UID (next_insn)))
1769 : 2257 : next_insn = NEXT_INSN (next_insn);
1770 : : else
1771 : : {
1772 : 1796 : second_insn = next_insn;
1773 : 1796 : insns_num--;
1774 : : }
1775 : : }
1776 : : }
1777 : 2683 : if (insns_num > 1)
1778 : : return false;
1779 : : }
1780 : 1486 : start = first_insn != NULL ? first_insn : start_insn;
1781 : 1644 : finish = second_insn != NULL ? second_insn : start_insn;
1782 : 1644 : return true;
1783 : : }
1784 : :
1785 : : /* Process reload pseudos which did not get a hard reg, split a hard reg live
1786 : : range in live range of a reload pseudo, and then return TRUE. Otherwise,
1787 : : return FALSE. When FAIL_P is TRUE and if we did not split a hard reg live
1788 : : range for failed reload pseudos, report an error and modify related asm
1789 : : insns. */
1790 : : bool
1791 : 1403 : lra_split_hard_reg_for (bool fail_p)
1792 : : {
1793 : 1403 : int i, regno;
1794 : 1403 : rtx_insn *insn, *first, *last;
1795 : 1403 : unsigned int u;
1796 : 1403 : bitmap_iterator bi;
1797 : 1403 : enum reg_class rclass;
1798 : 1403 : int max_regno = max_reg_num ();
1799 : : /* We did not assign hard regs to reload pseudos after two
1800 : : iterations. Either it's an asm and something is wrong with the
1801 : : constraints, or we have run out of spill registers; error out in
1802 : : either case. */
1803 : 1403 : bool asm_p = false, spill_p = false;
1804 : 1403 : bitmap_head failed_reload_insns, failed_reload_pseudos, over_split_insns;
1805 : :
1806 : 1403 : if (lra_dump_file != NULL)
1807 : 0 : fprintf (lra_dump_file,
1808 : : "\n****** Splitting a hard reg after assignment #%d: ******\n\n",
1809 : : lra_assignment_iter);
1810 : 1403 : bitmap_initialize (&failed_reload_pseudos, ®_obstack);
1811 : 1403 : bitmap_initialize (&non_reload_pseudos, ®_obstack);
1812 : 1403 : bitmap_ior (&non_reload_pseudos, &lra_inheritance_pseudos, &lra_split_regs);
1813 : 1403 : bitmap_ior_into (&non_reload_pseudos, &lra_subreg_reload_pseudos);
1814 : 1403 : bitmap_ior_into (&non_reload_pseudos, &lra_optional_reload_pseudos);
1815 : 1403 : bitmap_initialize (&over_split_insns, ®_obstack);
1816 : 17862 : for (i = lra_constraint_new_regno_start; i < max_regno; i++)
1817 : 5744 : if (reg_renumber[i] < 0 && lra_reg_info[i].nrefs != 0
1818 : 4917 : && (rclass = lra_get_allocno_class (i)) != NO_REGS
1819 : 21353 : && ! bitmap_bit_p (&non_reload_pseudos, i))
1820 : : {
1821 : 2841 : if (! find_reload_regno_insns (i, first, last))
1822 : 1197 : continue;
1823 : 1644 : if (BLOCK_FOR_INSN (first) == BLOCK_FOR_INSN (last))
1824 : : {
1825 : : /* Check that we are not trying to split over the same insn
1826 : : requiring reloads to avoid splitting the same hard reg twice or
1827 : : more. If we need several hard regs splitting over the same insn
1828 : : it can be finished on the next iterations.
1829 : :
1830 : : The following loop iteration number is small as we split hard
1831 : : reg in a very small range. */
1832 : 3308 : for (insn = first;
1833 : 4952 : insn != NEXT_INSN (last);
1834 : 3308 : insn = NEXT_INSN (insn))
1835 : 3317 : if (bitmap_bit_p (&over_split_insns, INSN_UID (insn)))
1836 : : break;
1837 : 1644 : if (insn != NEXT_INSN (last)
1838 : 1644 : || !spill_hard_reg_in_range (i, rclass, first, last))
1839 : : {
1840 : 1485 : bitmap_set_bit (&failed_reload_pseudos, i);
1841 : : }
1842 : : else
1843 : : {
1844 : 302 : for (insn = first;
1845 : 461 : insn != NEXT_INSN (last);
1846 : 302 : insn = NEXT_INSN (insn))
1847 : 302 : bitmap_set_bit (&over_split_insns, INSN_UID (insn));
1848 : : spill_p = true;
1849 : : }
1850 : : }
1851 : : }
1852 : 1403 : bitmap_clear (&over_split_insns);
1853 : 1403 : if (spill_p)
1854 : : {
1855 : 98 : bitmap_clear (&failed_reload_pseudos);
1856 : 98 : lra_dump_insns_if_possible ("changed func after splitting hard regs");
1857 : 98 : return true;
1858 : : }
1859 : 1305 : bitmap_clear (&non_reload_pseudos);
1860 : 1305 : bitmap_initialize (&failed_reload_insns, ®_obstack);
1861 : 2781 : EXECUTE_IF_SET_IN_BITMAP (&failed_reload_pseudos, 0, u, bi)
1862 : : {
1863 : 1476 : regno = u;
1864 : 1476 : bitmap_ior_into (&failed_reload_insns,
1865 : 1476 : &lra_reg_info[regno].insn_bitmap);
1866 : 1476 : if (fail_p)
1867 : 34 : lra_setup_reg_renumber
1868 : 68 : (regno, ira_class_hard_regs[lra_get_allocno_class (regno)][0], false);
1869 : : }
1870 : 1305 : if (fail_p)
1871 : 161 : EXECUTE_IF_SET_IN_BITMAP (&failed_reload_insns, 0, u, bi)
1872 : : {
1873 : 60 : insn = lra_insn_recog_data[u]->insn;
1874 : 60 : if (asm_noperands (PATTERN (insn)) >= 0)
1875 : : {
1876 : 34 : asm_p = true;
1877 : 34 : lra_asm_insn_error (insn);
1878 : : }
1879 : 26 : else if (!asm_p)
1880 : : {
1881 : 0 : error ("unable to find a register to spill");
1882 : 0 : fatal_insn ("this is the insn:", insn);
1883 : : }
1884 : : }
1885 : 1305 : bitmap_clear (&failed_reload_pseudos);
1886 : 1305 : bitmap_clear (&failed_reload_insns);
1887 : 1305 : return false;
1888 : : }
|