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