1 /* Assign reload pseudos.
2 Copyright (C) 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file's main objective is to assign hard registers to reload
24 pseudos. It also tries to allocate hard registers to other
25 pseudos, but at a lower priority than the reload pseudos. The pass
26 does not transform the RTL.
28 We must allocate a hard register to every reload pseudo. We try to
29 increase the chances of finding a viable allocation by assigning
30 the pseudos in order of fewest available hard registers first. If
31 we still fail to find a hard register, we spill other (non-reload)
32 pseudos in order to make room.
34 find_hard_regno_for finds hard registers for allocation without
35 spilling. spill_for does the same with spilling. Both functions
36 use a cost model to determine the most profitable choice of hard
39 Once we have finished allocating reload pseudos, we also try to
40 assign registers to other (non-reload) pseudos. This is useful if
41 hard registers were freed up by the spilling just described.
43 We try to assign hard registers by collecting pseudos into threads.
44 These threads contain reload and inheritance pseudos that are
45 connected by copies (move insns). Doing this improves the chances
46 of pseudos in the thread getting the same hard register and, as a
47 result, of allowing some move insns to be deleted.
49 When we assign a hard register to a pseudo, we decrease the cost of
50 using the same hard register for pseudos that are connected by
53 If two hard registers have the same frequency-derived cost, we
54 prefer hard registers with higher priorities. The mapping of
55 registers to priorities is controlled by the register_priority
56 target hook. For example, x86-64 has a few register priorities:
57 hard registers with and without REX prefixes have different
58 priorities. This permits us to generate smaller code as insns
59 without REX prefixes are shorter.
61 If a few hard registers are still equally good for the assignment,
62 we choose the least used hard register. It is called leveling and
63 may be profitable for some targets.
65 Only insns with changed allocation pseudos are processed on the
68 The pseudo live-ranges are used to find conflicting pseudos.
70 For understanding the code, it is important to keep in mind that
71 inheritance, split, and reload pseudos created since last
72 constraint pass have regno >= lra_constraint_new_regno_start.
73 Inheritance and split pseudos created on any pass are in the
74 corresponding bitmaps. Inheritance and split pseudos since the
75 last constraint pass have also the corresponding non-negative
80 #include "coretypes.h"
82 #include "hard-reg-set.h"
84 #include "rtl-error.h"
87 #include "insn-config.h"
93 #include "basic-block.h"
97 #include "sparseset.h"
100 /* Array containing corresponding values of function
101 lra_get_allocno_class. It is used to speed up the code. */
102 static enum reg_class
*regno_allocno_class_array
;
104 /* Information about the thread to which a pseudo belongs. Threads are
105 a set of connected reload and inheritance pseudos with the same set of
106 available hard registers. Lone registers belong to their own threads. */
107 struct regno_assign_info
109 /* First/next pseudo of the same thread. */
111 /* Frequency of the thread (execution frequency of only reload
112 pseudos in the thread when the thread contains a reload pseudo).
113 Defined only for the first thread pseudo. */
117 /* Map regno to the corresponding regno assignment info. */
118 static struct regno_assign_info
*regno_assign_info
;
120 /* Process a pseudo copy with execution frequency COPY_FREQ connecting
121 REGNO1 and REGNO2 to form threads. */
123 process_copy_to_form_thread (int regno1
, int regno2
, int copy_freq
)
125 int last
, regno1_first
, regno2_first
;
127 lra_assert (regno1
>= lra_constraint_new_regno_start
128 && regno2
>= lra_constraint_new_regno_start
);
129 regno1_first
= regno_assign_info
[regno1
].first
;
130 regno2_first
= regno_assign_info
[regno2
].first
;
131 if (regno1_first
!= regno2_first
)
133 for (last
= regno2_first
;
134 regno_assign_info
[last
].next
>= 0;
135 last
= regno_assign_info
[last
].next
)
136 regno_assign_info
[last
].first
= regno1_first
;
137 regno_assign_info
[last
].first
= regno1_first
;
138 regno_assign_info
[last
].next
= regno_assign_info
[regno1_first
].next
;
139 regno_assign_info
[regno1_first
].next
= regno2_first
;
140 regno_assign_info
[regno1_first
].freq
141 += regno_assign_info
[regno2_first
].freq
;
143 regno_assign_info
[regno1_first
].freq
-= 2 * copy_freq
;
144 lra_assert (regno_assign_info
[regno1_first
].freq
>= 0);
147 /* Initialize REGNO_ASSIGN_INFO and form threads. */
149 init_regno_assign_info (void)
151 int i
, regno1
, regno2
, max_regno
= max_reg_num ();
154 regno_assign_info
= XNEWVEC (struct regno_assign_info
, max_regno
);
155 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
157 regno_assign_info
[i
].first
= i
;
158 regno_assign_info
[i
].next
= -1;
159 regno_assign_info
[i
].freq
= lra_reg_info
[i
].freq
;
161 /* Form the threads. */
162 for (i
= 0; (cp
= lra_get_copy (i
)) != NULL
; i
++)
163 if ((regno1
= cp
->regno1
) >= lra_constraint_new_regno_start
164 && (regno2
= cp
->regno2
) >= lra_constraint_new_regno_start
165 && reg_renumber
[regno1
] < 0 && lra_reg_info
[regno1
].nrefs
!= 0
166 && reg_renumber
[regno2
] < 0 && lra_reg_info
[regno2
].nrefs
!= 0
167 && (ira_class_hard_regs_num
[regno_allocno_class_array
[regno1
]]
168 == ira_class_hard_regs_num
[regno_allocno_class_array
[regno2
]]))
169 process_copy_to_form_thread (regno1
, regno2
, cp
->freq
);
172 /* Free REGNO_ASSIGN_INFO. */
174 finish_regno_assign_info (void)
176 free (regno_assign_info
);
179 /* The function is used to sort *reload* and *inheritance* pseudos to
180 try to assign them hard registers. We put pseudos from the same
181 thread always nearby. */
183 reload_pseudo_compare_func (const void *v1p
, const void *v2p
)
185 int r1
= *(const int *) v1p
, r2
= *(const int *) v2p
;
186 enum reg_class cl1
= regno_allocno_class_array
[r1
];
187 enum reg_class cl2
= regno_allocno_class_array
[r2
];
190 lra_assert (r1
>= lra_constraint_new_regno_start
191 && r2
>= lra_constraint_new_regno_start
);
193 /* Prefer to assign reload registers with smaller classes first to
194 guarantee assignment to all reload registers. */
195 if ((diff
= (ira_class_hard_regs_num
[cl1
]
196 - ira_class_hard_regs_num
[cl2
])) != 0)
198 if ((diff
= (regno_assign_info
[regno_assign_info
[r2
].first
].freq
199 - regno_assign_info
[regno_assign_info
[r1
].first
].freq
)) != 0)
201 /* Put pseudos from the thread nearby. */
202 if ((diff
= regno_assign_info
[r1
].first
- regno_assign_info
[r2
].first
) != 0)
204 /* If regs are equally good, sort by their numbers, so that the
205 results of qsort leave nothing to chance. */
209 /* The function is used to sort *non-reload* pseudos to try to assign
210 them hard registers. The order calculation is simpler than in the
211 previous function and based on the pseudo frequency usage. */
213 pseudo_compare_func (const void *v1p
, const void *v2p
)
215 int r1
= *(const int *) v1p
, r2
= *(const int *) v2p
;
218 /* Prefer to assign more frequently used registers first. */
219 if ((diff
= lra_reg_info
[r2
].freq
- lra_reg_info
[r1
].freq
) != 0)
222 /* If regs are equally good, sort by their numbers, so that the
223 results of qsort leave nothing to chance. */
227 /* Arrays of size LRA_LIVE_MAX_POINT mapping a program point to the
228 pseudo live ranges with given start point. We insert only live
229 ranges of pseudos interesting for assignment purposes. They are
230 reload pseudos and pseudos assigned to hard registers. */
231 static lra_live_range_t
*start_point_ranges
;
233 /* Used as a flag that a live range is not inserted in the start point
235 static struct lra_live_range not_in_chain_mark
;
237 /* Create and set up START_POINT_RANGES. */
239 create_live_range_start_chains (void)
244 start_point_ranges
= XCNEWVEC (lra_live_range_t
, lra_live_max_point
);
245 max_regno
= max_reg_num ();
246 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
247 if (i
>= lra_constraint_new_regno_start
|| reg_renumber
[i
] >= 0)
249 for (r
= lra_reg_info
[i
].live_ranges
; r
!= NULL
; r
= r
->next
)
251 r
->start_next
= start_point_ranges
[r
->start
];
252 start_point_ranges
[r
->start
] = r
;
257 for (r
= lra_reg_info
[i
].live_ranges
; r
!= NULL
; r
= r
->next
)
258 r
->start_next
= ¬_in_chain_mark
;
262 /* Insert live ranges of pseudo REGNO into start chains if they are
265 insert_in_live_range_start_chain (int regno
)
267 lra_live_range_t r
= lra_reg_info
[regno
].live_ranges
;
269 if (r
->start_next
!= ¬_in_chain_mark
)
271 for (; r
!= NULL
; r
= r
->next
)
273 r
->start_next
= start_point_ranges
[r
->start
];
274 start_point_ranges
[r
->start
] = r
;
278 /* Free START_POINT_RANGES. */
280 finish_live_range_start_chains (void)
282 gcc_assert (start_point_ranges
!= NULL
);
283 free (start_point_ranges
);
284 start_point_ranges
= NULL
;
287 /* Map: program point -> bitmap of all pseudos living at the point and
288 assigned to hard registers. */
289 static bitmap_head
*live_hard_reg_pseudos
;
290 static bitmap_obstack live_hard_reg_pseudos_bitmap_obstack
;
292 /* reg_renumber corresponding to pseudos marked in
293 live_hard_reg_pseudos. reg_renumber might be not matched to
294 live_hard_reg_pseudos but live_pseudos_reg_renumber always reflects
295 live_hard_reg_pseudos. */
296 static int *live_pseudos_reg_renumber
;
298 /* Sparseset used to calculate living hard reg pseudos for some program
300 static sparseset live_range_hard_reg_pseudos
;
302 /* Sparseset used to calculate living reload/inheritance pseudos for
303 some program point range. */
304 static sparseset live_range_reload_inheritance_pseudos
;
306 /* Allocate and initialize the data about living pseudos at program
311 int i
, max_regno
= max_reg_num ();
313 live_range_hard_reg_pseudos
= sparseset_alloc (max_regno
);
314 live_range_reload_inheritance_pseudos
= sparseset_alloc (max_regno
);
315 live_hard_reg_pseudos
= XNEWVEC (bitmap_head
, lra_live_max_point
);
316 bitmap_obstack_initialize (&live_hard_reg_pseudos_bitmap_obstack
);
317 for (i
= 0; i
< lra_live_max_point
; i
++)
318 bitmap_initialize (&live_hard_reg_pseudos
[i
],
319 &live_hard_reg_pseudos_bitmap_obstack
);
320 live_pseudos_reg_renumber
= XNEWVEC (int, max_regno
);
321 for (i
= 0; i
< max_regno
; i
++)
322 live_pseudos_reg_renumber
[i
] = -1;
325 /* Free the data about living pseudos at program points. */
329 sparseset_free (live_range_hard_reg_pseudos
);
330 sparseset_free (live_range_reload_inheritance_pseudos
);
331 free (live_hard_reg_pseudos
);
332 bitmap_obstack_release (&live_hard_reg_pseudos_bitmap_obstack
);
333 free (live_pseudos_reg_renumber
);
336 /* Update the LIVE_HARD_REG_PSEUDOS and LIVE_PSEUDOS_REG_RENUMBER
337 entries for pseudo REGNO. Assume that the register has been
338 spilled if FREE_P, otherwise assume that it has been assigned
339 reg_renumber[REGNO] (if >= 0). We also insert the pseudo live
340 ranges in the start chains when it is assumed to be assigned to a
341 hard register because we use the chains of pseudos assigned to hard
342 registers during allocation. */
344 update_lives (int regno
, bool free_p
)
349 if (reg_renumber
[regno
] < 0)
351 live_pseudos_reg_renumber
[regno
] = free_p
? -1 : reg_renumber
[regno
];
352 for (r
= lra_reg_info
[regno
].live_ranges
; r
!= NULL
; r
= r
->next
)
354 for (p
= r
->start
; p
<= r
->finish
; p
++)
356 bitmap_clear_bit (&live_hard_reg_pseudos
[p
], regno
);
359 bitmap_set_bit (&live_hard_reg_pseudos
[p
], regno
);
360 insert_in_live_range_start_chain (regno
);
365 /* Sparseset used to calculate reload pseudos conflicting with a given
366 pseudo when we are trying to find a hard register for the given
368 static sparseset conflict_reload_and_inheritance_pseudos
;
370 /* Map: program point -> bitmap of all reload and inheritance pseudos
371 living at the point. */
372 static bitmap_head
*live_reload_and_inheritance_pseudos
;
373 static bitmap_obstack live_reload_and_inheritance_pseudos_bitmap_obstack
;
375 /* Allocate and initialize data about living reload pseudos at any
376 given program point. */
378 init_live_reload_and_inheritance_pseudos (void)
380 int i
, p
, max_regno
= max_reg_num ();
383 conflict_reload_and_inheritance_pseudos
= sparseset_alloc (max_regno
);
384 live_reload_and_inheritance_pseudos
= XNEWVEC (bitmap_head
, lra_live_max_point
);
385 bitmap_obstack_initialize (&live_reload_and_inheritance_pseudos_bitmap_obstack
);
386 for (p
= 0; p
< lra_live_max_point
; p
++)
387 bitmap_initialize (&live_reload_and_inheritance_pseudos
[p
],
388 &live_reload_and_inheritance_pseudos_bitmap_obstack
);
389 for (i
= lra_constraint_new_regno_start
; i
< max_regno
; i
++)
391 for (r
= lra_reg_info
[i
].live_ranges
; r
!= NULL
; r
= r
->next
)
392 for (p
= r
->start
; p
<= r
->finish
; p
++)
393 bitmap_set_bit (&live_reload_and_inheritance_pseudos
[p
], i
);
397 /* Finalize data about living reload pseudos at any given program
400 finish_live_reload_and_inheritance_pseudos (void)
402 sparseset_free (conflict_reload_and_inheritance_pseudos
);
403 free (live_reload_and_inheritance_pseudos
);
404 bitmap_obstack_release (&live_reload_and_inheritance_pseudos_bitmap_obstack
);
407 /* The value used to check that cost of given hard reg is really
408 defined currently. */
409 static int curr_hard_regno_costs_check
= 0;
410 /* Array used to check that cost of the corresponding hard reg (the
411 array element index) is really defined currently. */
412 static int hard_regno_costs_check
[FIRST_PSEUDO_REGISTER
];
413 /* The current costs of allocation of hard regs. Defined only if the
414 value of the corresponding element of the previous array is equal to
415 CURR_HARD_REGNO_COSTS_CHECK. */
416 static int hard_regno_costs
[FIRST_PSEUDO_REGISTER
];
418 /* Adjust cost of HARD_REGNO by INCR. Reset the cost first if it is
421 adjust_hard_regno_cost (int hard_regno
, int incr
)
423 if (hard_regno_costs_check
[hard_regno
] != curr_hard_regno_costs_check
)
424 hard_regno_costs
[hard_regno
] = 0;
425 hard_regno_costs_check
[hard_regno
] = curr_hard_regno_costs_check
;
426 hard_regno_costs
[hard_regno
] += incr
;
429 /* Try to find a free hard register for pseudo REGNO. Return the
430 hard register on success and set *COST to the cost of using
431 that register. (If several registers have equal cost, the one with
432 the highest priority wins.) Return -1 on failure.
434 If TRY_ONLY_HARD_REGNO >= 0, consider only that hard register,
435 otherwise consider all hard registers in REGNO's class. */
437 find_hard_regno_for (int regno
, int *cost
, int try_only_hard_regno
)
439 HARD_REG_SET conflict_set
;
440 int best_cost
= INT_MAX
, best_priority
= INT_MIN
, best_usage
= INT_MAX
;
442 int p
, i
, j
, rclass_size
, best_hard_regno
, priority
, hard_regno
;
443 int hr
, conflict_hr
, nregs
;
444 enum machine_mode biggest_mode
;
445 unsigned int k
, conflict_regno
;
446 int val
, biggest_nregs
, nregs_diff
;
447 enum reg_class rclass
;
449 bool *rclass_intersect_p
;
450 HARD_REG_SET impossible_start_hard_regs
;
452 COPY_HARD_REG_SET (conflict_set
, lra_no_alloc_regs
);
453 rclass
= regno_allocno_class_array
[regno
];
454 rclass_intersect_p
= ira_reg_classes_intersect_p
[rclass
];
455 curr_hard_regno_costs_check
++;
456 sparseset_clear (conflict_reload_and_inheritance_pseudos
);
457 sparseset_clear (live_range_hard_reg_pseudos
);
458 IOR_HARD_REG_SET (conflict_set
, lra_reg_info
[regno
].conflict_hard_regs
);
459 biggest_mode
= lra_reg_info
[regno
].biggest_mode
;
460 for (r
= lra_reg_info
[regno
].live_ranges
; r
!= NULL
; r
= r
->next
)
462 EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos
[r
->start
], 0, k
, bi
)
463 if (rclass_intersect_p
[regno_allocno_class_array
[k
]])
464 sparseset_set_bit (live_range_hard_reg_pseudos
, k
);
465 EXECUTE_IF_SET_IN_BITMAP (&live_reload_and_inheritance_pseudos
[r
->start
],
467 if (lra_reg_info
[k
].preferred_hard_regno1
>= 0
468 && live_pseudos_reg_renumber
[k
] < 0
469 && rclass_intersect_p
[regno_allocno_class_array
[k
]])
470 sparseset_set_bit (conflict_reload_and_inheritance_pseudos
, k
);
471 for (p
= r
->start
+ 1; p
<= r
->finish
; p
++)
475 for (r2
= start_point_ranges
[p
];
479 if (r2
->regno
>= lra_constraint_new_regno_start
480 && lra_reg_info
[r2
->regno
].preferred_hard_regno1
>= 0
481 && live_pseudos_reg_renumber
[r2
->regno
] < 0
482 && rclass_intersect_p
[regno_allocno_class_array
[r2
->regno
]])
483 sparseset_set_bit (conflict_reload_and_inheritance_pseudos
,
485 if (live_pseudos_reg_renumber
[r2
->regno
] >= 0
486 && rclass_intersect_p
[regno_allocno_class_array
[r2
->regno
]])
487 sparseset_set_bit (live_range_hard_reg_pseudos
, r2
->regno
);
491 if ((hard_regno
= lra_reg_info
[regno
].preferred_hard_regno1
) >= 0)
493 adjust_hard_regno_cost
494 (hard_regno
, -lra_reg_info
[regno
].preferred_hard_regno_profit1
);
495 if ((hard_regno
= lra_reg_info
[regno
].preferred_hard_regno2
) >= 0)
496 adjust_hard_regno_cost
497 (hard_regno
, -lra_reg_info
[regno
].preferred_hard_regno_profit2
);
500 if (lra_reg_info
[regno
].no_stack_p
)
501 for (i
= FIRST_STACK_REG
; i
<= LAST_STACK_REG
; i
++)
502 SET_HARD_REG_BIT (conflict_set
, i
);
504 sparseset_clear_bit (conflict_reload_and_inheritance_pseudos
, regno
);
505 val
= lra_reg_info
[regno
].val
;
506 CLEAR_HARD_REG_SET (impossible_start_hard_regs
);
507 EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos
, conflict_regno
)
508 if (val
== lra_reg_info
[conflict_regno
].val
)
510 conflict_hr
= live_pseudos_reg_renumber
[conflict_regno
];
511 nregs
= (hard_regno_nregs
[conflict_hr
]
512 [lra_reg_info
[conflict_regno
].biggest_mode
]);
513 /* Remember about multi-register pseudos. For example, 2 hard
514 register pseudos can start on the same hard register but can
515 not start on HR and HR+1/HR-1. */
516 for (hr
= conflict_hr
+ 1;
517 hr
< FIRST_PSEUDO_REGISTER
&& hr
< conflict_hr
+ nregs
;
519 SET_HARD_REG_BIT (impossible_start_hard_regs
, hr
);
520 for (hr
= conflict_hr
- 1;
521 hr
>= 0 && hr
+ hard_regno_nregs
[hr
][biggest_mode
] > conflict_hr
;
523 SET_HARD_REG_BIT (impossible_start_hard_regs
, hr
);
527 add_to_hard_reg_set (&conflict_set
,
528 lra_reg_info
[conflict_regno
].biggest_mode
,
529 live_pseudos_reg_renumber
[conflict_regno
]);
530 if (hard_reg_set_subset_p (reg_class_contents
[rclass
],
534 EXECUTE_IF_SET_IN_SPARSESET (conflict_reload_and_inheritance_pseudos
,
536 if (val
!= lra_reg_info
[conflict_regno
].val
)
538 lra_assert (live_pseudos_reg_renumber
[conflict_regno
] < 0);
540 = lra_reg_info
[conflict_regno
].preferred_hard_regno1
) >= 0)
542 adjust_hard_regno_cost
544 lra_reg_info
[conflict_regno
].preferred_hard_regno_profit1
);
546 = lra_reg_info
[conflict_regno
].preferred_hard_regno2
) >= 0)
547 adjust_hard_regno_cost
549 lra_reg_info
[conflict_regno
].preferred_hard_regno_profit2
);
552 /* Make sure that all registers in a multi-word pseudo belong to the
554 IOR_COMPL_HARD_REG_SET (conflict_set
, reg_class_contents
[rclass
]);
555 lra_assert (rclass
!= NO_REGS
);
556 rclass_size
= ira_class_hard_regs_num
[rclass
];
557 best_hard_regno
= -1;
558 hard_regno
= ira_class_hard_regs
[rclass
][0];
559 biggest_nregs
= hard_regno_nregs
[hard_regno
][biggest_mode
];
560 nregs_diff
= (biggest_nregs
561 - hard_regno_nregs
[hard_regno
][PSEUDO_REGNO_MODE (regno
)]);
562 for (i
= 0; i
< rclass_size
; i
++)
564 if (try_only_hard_regno
>= 0)
565 hard_regno
= try_only_hard_regno
;
567 hard_regno
= ira_class_hard_regs
[rclass
][i
];
568 if (! overlaps_hard_reg_set_p (conflict_set
,
569 PSEUDO_REGNO_MODE (regno
), hard_regno
)
570 /* We can not use prohibited_class_mode_regs because it is
571 not defined for all classes. */
572 && HARD_REGNO_MODE_OK (hard_regno
, PSEUDO_REGNO_MODE (regno
))
573 && ! TEST_HARD_REG_BIT (impossible_start_hard_regs
, hard_regno
)
576 ? (hard_regno
- nregs_diff
>= 0
577 && TEST_HARD_REG_BIT (reg_class_contents
[rclass
],
578 hard_regno
- nregs_diff
))
579 : TEST_HARD_REG_BIT (reg_class_contents
[rclass
],
580 hard_regno
+ nregs_diff
))))
582 if (hard_regno_costs_check
[hard_regno
]
583 != curr_hard_regno_costs_check
)
585 hard_regno_costs_check
[hard_regno
] = curr_hard_regno_costs_check
;
586 hard_regno_costs
[hard_regno
] = 0;
589 j
< hard_regno_nregs
[hard_regno
][PSEUDO_REGNO_MODE (regno
)];
591 if (! TEST_HARD_REG_BIT (call_used_reg_set
, hard_regno
+ j
)
592 && ! df_regs_ever_live_p (hard_regno
+ j
))
593 /* It needs save restore. */
594 hard_regno_costs
[hard_regno
]
595 += 2 * ENTRY_BLOCK_PTR
->next_bb
->frequency
;
596 priority
= targetm
.register_priority (hard_regno
);
597 if (best_hard_regno
< 0 || hard_regno_costs
[hard_regno
] < best_cost
598 || (hard_regno_costs
[hard_regno
] == best_cost
599 && (priority
> best_priority
600 /* Hard register usage leveling actually results
601 in bigger code for targets with conditional
602 execution like ARM because it reduces chance
603 of if-conversion after LRA. */
604 || (! targetm
.have_conditional_execution ()
605 && priority
== best_priority
606 && best_usage
> lra_hard_reg_usage
[hard_regno
]))))
608 best_hard_regno
= hard_regno
;
609 best_cost
= hard_regno_costs
[hard_regno
];
610 best_priority
= priority
;
611 best_usage
= lra_hard_reg_usage
[hard_regno
];
614 if (try_only_hard_regno
>= 0)
617 if (best_hard_regno
>= 0)
618 *cost
= best_cost
- lra_reg_info
[regno
].freq
;
619 return best_hard_regno
;
622 /* Current value used for checking elements in
623 update_hard_regno_preference_check. */
624 static int curr_update_hard_regno_preference_check
;
625 /* If an element value is equal to the above variable value, then the
626 corresponding regno has been processed for preference
628 static int *update_hard_regno_preference_check
;
630 /* Update the preference for using HARD_REGNO for pseudos that are
631 connected directly or indirectly with REGNO. Apply divisor DIV
632 to any preference adjustments.
634 The more indirectly a pseudo is connected, the smaller its effect
635 should be. We therefore increase DIV on each "hop". */
637 update_hard_regno_preference (int regno
, int hard_regno
, int div
)
639 int another_regno
, cost
;
640 lra_copy_t cp
, next_cp
;
642 /* Search depth 5 seems to be enough. */
645 for (cp
= lra_reg_info
[regno
].copies
; cp
!= NULL
; cp
= next_cp
)
647 if (cp
->regno1
== regno
)
649 next_cp
= cp
->regno1_next
;
650 another_regno
= cp
->regno2
;
652 else if (cp
->regno2
== regno
)
654 next_cp
= cp
->regno2_next
;
655 another_regno
= cp
->regno1
;
659 if (reg_renumber
[another_regno
] < 0
660 && (update_hard_regno_preference_check
[another_regno
]
661 != curr_update_hard_regno_preference_check
))
663 update_hard_regno_preference_check
[another_regno
]
664 = curr_update_hard_regno_preference_check
;
665 cost
= cp
->freq
< div
? 1 : cp
->freq
/ div
;
666 lra_setup_reload_pseudo_preferenced_hard_reg
667 (another_regno
, hard_regno
, cost
);
668 update_hard_regno_preference (another_regno
, hard_regno
, div
* 2);
673 /* Update REG_RENUMBER and other pseudo preferences by assignment of
674 HARD_REGNO to pseudo REGNO and print about it if PRINT_P. */
676 lra_setup_reg_renumber (int regno
, int hard_regno
, bool print_p
)
680 /* We can not just reassign hard register. */
681 lra_assert (hard_regno
< 0 || reg_renumber
[regno
] < 0);
682 if ((hr
= hard_regno
) < 0)
683 hr
= reg_renumber
[regno
];
684 reg_renumber
[regno
] = hard_regno
;
685 lra_assert (hr
>= 0);
686 for (i
= 0; i
< hard_regno_nregs
[hr
][PSEUDO_REGNO_MODE (regno
)]; i
++)
688 lra_hard_reg_usage
[hr
+ i
] -= lra_reg_info
[regno
].freq
;
690 lra_hard_reg_usage
[hr
+ i
] += lra_reg_info
[regno
].freq
;
691 if (print_p
&& lra_dump_file
!= NULL
)
692 fprintf (lra_dump_file
, " Assign %d to %sr%d (freq=%d)\n",
694 regno
< lra_constraint_new_regno_start
696 : bitmap_bit_p (&lra_inheritance_pseudos
, regno
) ? "inheritance "
697 : bitmap_bit_p (&lra_split_regs
, regno
) ? "split "
698 : bitmap_bit_p (&lra_optional_reload_pseudos
, regno
)
699 ? "optional reload ": "reload ",
700 regno
, lra_reg_info
[regno
].freq
);
703 curr_update_hard_regno_preference_check
++;
704 update_hard_regno_preference (regno
, hard_regno
, 1);
708 /* Pseudos which occur in insns containing a particular pseudo. */
709 static bitmap_head insn_conflict_pseudos
;
711 /* Bitmaps used to contain spill pseudos for given pseudo hard regno
712 and best spill pseudos for given pseudo (and best hard regno). */
713 static bitmap_head spill_pseudos_bitmap
, best_spill_pseudos_bitmap
;
715 /* Current pseudo check for validity of elements in
716 TRY_HARD_REG_PSEUDOS. */
717 static int curr_pseudo_check
;
718 /* Array used for validity of elements in TRY_HARD_REG_PSEUDOS. */
719 static int try_hard_reg_pseudos_check
[FIRST_PSEUDO_REGISTER
];
720 /* Pseudos who hold given hard register at the considered points. */
721 static bitmap_head try_hard_reg_pseudos
[FIRST_PSEUDO_REGISTER
];
723 /* Set up try_hard_reg_pseudos for given program point P and class
724 RCLASS. Those are pseudos living at P and assigned to a hard
725 register of RCLASS. In other words, those are pseudos which can be
726 spilled to assign a hard register of RCLASS to a pseudo living at
729 setup_try_hard_regno_pseudos (int p
, enum reg_class rclass
)
732 enum machine_mode mode
;
733 unsigned int spill_regno
;
736 /* Find what pseudos could be spilled. */
737 EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos
[p
], 0, spill_regno
, bi
)
739 mode
= PSEUDO_REGNO_MODE (spill_regno
);
740 hard_regno
= live_pseudos_reg_renumber
[spill_regno
];
741 if (overlaps_hard_reg_set_p (reg_class_contents
[rclass
],
744 for (i
= hard_regno_nregs
[hard_regno
][mode
] - 1; i
>= 0; i
--)
746 if (try_hard_reg_pseudos_check
[hard_regno
+ i
]
747 != curr_pseudo_check
)
749 try_hard_reg_pseudos_check
[hard_regno
+ i
]
751 bitmap_clear (&try_hard_reg_pseudos
[hard_regno
+ i
]);
753 bitmap_set_bit (&try_hard_reg_pseudos
[hard_regno
+ i
],
760 /* Assign temporarily HARD_REGNO to pseudo REGNO. Temporary
761 assignment means that we might undo the data change. */
763 assign_temporarily (int regno
, int hard_regno
)
768 for (r
= lra_reg_info
[regno
].live_ranges
; r
!= NULL
; r
= r
->next
)
770 for (p
= r
->start
; p
<= r
->finish
; p
++)
772 bitmap_clear_bit (&live_hard_reg_pseudos
[p
], regno
);
775 bitmap_set_bit (&live_hard_reg_pseudos
[p
], regno
);
776 insert_in_live_range_start_chain (regno
);
779 live_pseudos_reg_renumber
[regno
] = hard_regno
;
782 /* Array used for sorting reload pseudos for subsequent allocation
783 after spilling some pseudo. */
784 static int *sorted_reload_pseudos
;
786 /* Spill some pseudos for a reload pseudo REGNO and return hard
787 register which should be used for pseudo after spilling. The
788 function adds spilled pseudos to SPILLED_PSEUDO_BITMAP. When we
789 choose hard register (and pseudos occupying the hard registers and
790 to be spilled), we take into account not only how REGNO will
791 benefit from the spills but also how other reload pseudos not yet
792 assigned to hard registers benefit from the spills too. In very
793 rare cases, the function can fail and return -1. */
795 spill_for (int regno
, bitmap spilled_pseudo_bitmap
)
797 int i
, j
, n
, p
, hard_regno
, best_hard_regno
, cost
, best_cost
, rclass_size
;
798 int reload_hard_regno
, reload_cost
;
799 enum machine_mode mode
;
800 enum reg_class rclass
;
801 unsigned int spill_regno
, reload_regno
, uid
;
802 int insn_pseudos_num
, best_insn_pseudos_num
;
806 rclass
= regno_allocno_class_array
[regno
];
807 lra_assert (reg_renumber
[regno
] < 0 && rclass
!= NO_REGS
);
808 bitmap_clear (&insn_conflict_pseudos
);
809 bitmap_clear (&best_spill_pseudos_bitmap
);
810 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info
[regno
].insn_bitmap
, 0, uid
, bi
)
812 struct lra_insn_reg
*ir
;
814 for (ir
= lra_get_insn_regs (uid
); ir
!= NULL
; ir
= ir
->next
)
815 if (ir
->regno
>= FIRST_PSEUDO_REGISTER
)
816 bitmap_set_bit (&insn_conflict_pseudos
, ir
->regno
);
818 best_hard_regno
= -1;
820 best_insn_pseudos_num
= INT_MAX
;
821 rclass_size
= ira_class_hard_regs_num
[rclass
];
822 mode
= PSEUDO_REGNO_MODE (regno
);
823 /* Invalidate try_hard_reg_pseudos elements. */
825 for (r
= lra_reg_info
[regno
].live_ranges
; r
!= NULL
; r
= r
->next
)
826 for (p
= r
->start
; p
<= r
->finish
; p
++)
827 setup_try_hard_regno_pseudos (p
, rclass
);
828 for (i
= 0; i
< rclass_size
; i
++)
830 hard_regno
= ira_class_hard_regs
[rclass
][i
];
831 bitmap_clear (&spill_pseudos_bitmap
);
832 for (j
= hard_regno_nregs
[hard_regno
][mode
] - 1; j
>= 0; j
--)
834 if (try_hard_reg_pseudos_check
[hard_regno
+ j
] != curr_pseudo_check
)
836 lra_assert (!bitmap_empty_p (&try_hard_reg_pseudos
[hard_regno
+ j
]));
837 bitmap_ior_into (&spill_pseudos_bitmap
,
838 &try_hard_reg_pseudos
[hard_regno
+ j
]);
841 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap
, 0, spill_regno
, bi
)
842 if ((int) spill_regno
>= lra_constraint_new_regno_start
843 && ! bitmap_bit_p (&lra_inheritance_pseudos
, spill_regno
)
844 && ! bitmap_bit_p (&lra_split_regs
, spill_regno
)
845 && ! bitmap_bit_p (&lra_optional_reload_pseudos
, spill_regno
))
847 insn_pseudos_num
= 0;
848 if (lra_dump_file
!= NULL
)
849 fprintf (lra_dump_file
, " Trying %d:", hard_regno
);
850 sparseset_clear (live_range_reload_inheritance_pseudos
);
851 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap
, 0, spill_regno
, bi
)
853 if (bitmap_bit_p (&insn_conflict_pseudos
, spill_regno
))
855 for (r
= lra_reg_info
[spill_regno
].live_ranges
;
859 for (p
= r
->start
; p
<= r
->finish
; p
++)
863 for (r2
= start_point_ranges
[p
];
866 if (r2
->regno
>= lra_constraint_new_regno_start
)
867 sparseset_set_bit (live_range_reload_inheritance_pseudos
,
873 EXECUTE_IF_SET_IN_SPARSESET (live_range_reload_inheritance_pseudos
,
875 if ((int) reload_regno
!= regno
876 && (ira_reg_classes_intersect_p
877 [rclass
][regno_allocno_class_array
[reload_regno
]])
878 && live_pseudos_reg_renumber
[reload_regno
] < 0
879 && find_hard_regno_for (reload_regno
, &cost
, -1) < 0)
880 sorted_reload_pseudos
[n
++] = reload_regno
;
881 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap
, 0, spill_regno
, bi
)
883 update_lives (spill_regno
, true);
884 if (lra_dump_file
!= NULL
)
885 fprintf (lra_dump_file
, " spill %d(freq=%d)",
886 spill_regno
, lra_reg_info
[spill_regno
].freq
);
888 hard_regno
= find_hard_regno_for (regno
, &cost
, -1);
891 assign_temporarily (regno
, hard_regno
);
892 qsort (sorted_reload_pseudos
, n
, sizeof (int),
893 reload_pseudo_compare_func
);
894 for (j
= 0; j
< n
; j
++)
896 reload_regno
= sorted_reload_pseudos
[j
];
897 lra_assert (live_pseudos_reg_renumber
[reload_regno
] < 0);
898 if ((reload_hard_regno
899 = find_hard_regno_for (reload_regno
,
900 &reload_cost
, -1)) >= 0)
902 if (lra_dump_file
!= NULL
)
903 fprintf (lra_dump_file
, " assign %d(cost=%d)",
904 reload_regno
, reload_cost
);
905 assign_temporarily (reload_regno
, reload_hard_regno
);
909 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap
, 0, spill_regno
, bi
)
913 cost
+= lra_reg_info
[spill_regno
].freq
;
914 if (ira_reg_equiv
[spill_regno
].memory
!= NULL
915 || ira_reg_equiv
[spill_regno
].constant
!= NULL
)
916 for (x
= ira_reg_equiv
[spill_regno
].init_insns
;
919 cost
-= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (XEXP (x
, 0)));
921 if (best_insn_pseudos_num
> insn_pseudos_num
922 || (best_insn_pseudos_num
== insn_pseudos_num
923 && best_cost
> cost
))
925 best_insn_pseudos_num
= insn_pseudos_num
;
927 best_hard_regno
= hard_regno
;
928 bitmap_copy (&best_spill_pseudos_bitmap
, &spill_pseudos_bitmap
);
929 if (lra_dump_file
!= NULL
)
930 fprintf (lra_dump_file
, " Now best %d(cost=%d)\n",
933 assign_temporarily (regno
, -1);
934 for (j
= 0; j
< n
; j
++)
936 reload_regno
= sorted_reload_pseudos
[j
];
937 if (live_pseudos_reg_renumber
[reload_regno
] >= 0)
938 assign_temporarily (reload_regno
, -1);
941 if (lra_dump_file
!= NULL
)
942 fprintf (lra_dump_file
, "\n");
943 /* Restore the live hard reg pseudo info for spilled pseudos. */
944 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap
, 0, spill_regno
, bi
)
945 update_lives (spill_regno
, false);
950 EXECUTE_IF_SET_IN_BITMAP (&best_spill_pseudos_bitmap
, 0, spill_regno
, bi
)
952 if (lra_dump_file
!= NULL
)
953 fprintf (lra_dump_file
, " Spill %sr%d(hr=%d, freq=%d) for r%d\n",
954 ((int) spill_regno
< lra_constraint_new_regno_start
956 : bitmap_bit_p (&lra_inheritance_pseudos
, spill_regno
)
958 : bitmap_bit_p (&lra_split_regs
, spill_regno
)
960 : bitmap_bit_p (&lra_optional_reload_pseudos
, spill_regno
)
961 ? "optional reload " : "reload "),
962 spill_regno
, reg_renumber
[spill_regno
],
963 lra_reg_info
[spill_regno
].freq
, regno
);
964 update_lives (spill_regno
, true);
965 lra_setup_reg_renumber (spill_regno
, -1, false);
967 bitmap_ior_into (spilled_pseudo_bitmap
, &best_spill_pseudos_bitmap
);
968 return best_hard_regno
;
971 /* Assign HARD_REGNO to REGNO. */
973 assign_hard_regno (int hard_regno
, int regno
)
977 lra_assert (hard_regno
>= 0);
978 lra_setup_reg_renumber (regno
, hard_regno
, true);
979 update_lives (regno
, false);
981 i
< hard_regno_nregs
[hard_regno
][lra_reg_info
[regno
].biggest_mode
];
983 df_set_regs_ever_live (hard_regno
+ i
, true);
986 /* Array used for sorting different pseudos. */
987 static int *sorted_pseudos
;
989 /* The constraints pass is allowed to create equivalences between
990 pseudos that make the current allocation "incorrect" (in the sense
991 that pseudos are assigned to hard registers from their own conflict
992 sets). The global variable lra_risky_transformations_p says
993 whether this might have happened.
995 Process pseudos assigned to hard registers (less frequently used
996 first), spill if a conflict is found, and mark the spilled pseudos
997 in SPILLED_PSEUDO_BITMAP. Set up LIVE_HARD_REG_PSEUDOS from
998 pseudos, assigned to hard registers. */
1000 setup_live_pseudos_and_spill_after_risky_transforms (bitmap
1001 spilled_pseudo_bitmap
)
1003 int p
, i
, j
, n
, regno
, hard_regno
;
1004 unsigned int k
, conflict_regno
;
1006 HARD_REG_SET conflict_set
;
1007 enum machine_mode mode
;
1010 int max_regno
= max_reg_num ();
1012 if (! lra_risky_transformations_p
)
1014 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
1015 if (reg_renumber
[i
] >= 0 && lra_reg_info
[i
].nrefs
> 0)
1016 update_lives (i
, false);
1019 for (n
= 0, i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
1020 if (reg_renumber
[i
] >= 0 && lra_reg_info
[i
].nrefs
> 0)
1021 sorted_pseudos
[n
++] = i
;
1022 qsort (sorted_pseudos
, n
, sizeof (int), pseudo_compare_func
);
1023 for (i
= n
- 1; i
>= 0; i
--)
1025 regno
= sorted_pseudos
[i
];
1026 hard_regno
= reg_renumber
[regno
];
1027 lra_assert (hard_regno
>= 0);
1028 mode
= lra_reg_info
[regno
].biggest_mode
;
1029 sparseset_clear (live_range_hard_reg_pseudos
);
1030 for (r
= lra_reg_info
[regno
].live_ranges
; r
!= NULL
; r
= r
->next
)
1032 EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos
[r
->start
], 0, k
, bi
)
1033 sparseset_set_bit (live_range_hard_reg_pseudos
, k
);
1034 for (p
= r
->start
+ 1; p
<= r
->finish
; p
++)
1036 lra_live_range_t r2
;
1038 for (r2
= start_point_ranges
[p
];
1040 r2
= r2
->start_next
)
1041 if (live_pseudos_reg_renumber
[r2
->regno
] >= 0)
1042 sparseset_set_bit (live_range_hard_reg_pseudos
, r2
->regno
);
1045 COPY_HARD_REG_SET (conflict_set
, lra_no_alloc_regs
);
1046 IOR_HARD_REG_SET (conflict_set
, lra_reg_info
[regno
].conflict_hard_regs
);
1047 val
= lra_reg_info
[regno
].val
;
1048 EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos
, conflict_regno
)
1049 if (val
!= lra_reg_info
[conflict_regno
].val
1050 /* If it is multi-register pseudos they should start on
1051 the same hard register. */
1052 || hard_regno
!= reg_renumber
[conflict_regno
])
1053 add_to_hard_reg_set (&conflict_set
,
1054 lra_reg_info
[conflict_regno
].biggest_mode
,
1055 reg_renumber
[conflict_regno
]);
1056 if (! overlaps_hard_reg_set_p (conflict_set
, mode
, hard_regno
))
1058 update_lives (regno
, false);
1061 bitmap_set_bit (spilled_pseudo_bitmap
, regno
);
1063 j
< hard_regno_nregs
[hard_regno
][PSEUDO_REGNO_MODE (regno
)];
1065 lra_hard_reg_usage
[hard_regno
+ j
] -= lra_reg_info
[regno
].freq
;
1066 reg_renumber
[regno
] = -1;
1067 if (lra_dump_file
!= NULL
)
1068 fprintf (lra_dump_file
, " Spill r%d after risky transformations\n",
1073 /* Improve allocation by assigning the same hard regno of inheritance
1074 pseudos to the connected pseudos. We need this because inheritance
1075 pseudos are allocated after reload pseudos in the thread and when
1076 we assign a hard register to a reload pseudo we don't know yet that
1077 the connected inheritance pseudos can get the same hard register.
1078 Add pseudos with changed allocation to bitmap CHANGED_PSEUDOS. */
1080 improve_inheritance (bitmap changed_pseudos
)
1083 int regno
, another_regno
, hard_regno
, another_hard_regno
, cost
, i
, n
;
1084 lra_copy_t cp
, next_cp
;
1087 if (lra_inheritance_iter
> LRA_MAX_INHERITANCE_PASSES
)
1090 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos
, 0, k
, bi
)
1091 if (reg_renumber
[k
] >= 0 && lra_reg_info
[k
].nrefs
!= 0)
1092 sorted_pseudos
[n
++] = k
;
1093 qsort (sorted_pseudos
, n
, sizeof (int), pseudo_compare_func
);
1094 for (i
= 0; i
< n
; i
++)
1096 regno
= sorted_pseudos
[i
];
1097 hard_regno
= reg_renumber
[regno
];
1098 lra_assert (hard_regno
>= 0);
1099 for (cp
= lra_reg_info
[regno
].copies
; cp
!= NULL
; cp
= next_cp
)
1101 if (cp
->regno1
== regno
)
1103 next_cp
= cp
->regno1_next
;
1104 another_regno
= cp
->regno2
;
1106 else if (cp
->regno2
== regno
)
1108 next_cp
= cp
->regno2_next
;
1109 another_regno
= cp
->regno1
;
1113 /* Don't change reload pseudo allocation. It might have
1114 this allocation for a purpose and changing it can result
1116 if ((another_regno
< lra_constraint_new_regno_start
1117 || bitmap_bit_p (&lra_inheritance_pseudos
, another_regno
))
1118 && (another_hard_regno
= reg_renumber
[another_regno
]) >= 0
1119 && another_hard_regno
!= hard_regno
)
1121 if (lra_dump_file
!= NULL
)
1124 " Improving inheritance for %d(%d) and %d(%d)...\n",
1125 regno
, hard_regno
, another_regno
, another_hard_regno
);
1126 update_lives (another_regno
, true);
1127 lra_setup_reg_renumber (another_regno
, -1, false);
1129 == find_hard_regno_for (another_regno
, &cost
, hard_regno
))
1130 assign_hard_regno (hard_regno
, another_regno
);
1132 assign_hard_regno (another_hard_regno
, another_regno
);
1133 bitmap_set_bit (changed_pseudos
, another_regno
);
1140 /* Bitmap finally containing all pseudos spilled on this assignment
1142 static bitmap_head all_spilled_pseudos
;
1143 /* All pseudos whose allocation was changed. */
1144 static bitmap_head changed_pseudo_bitmap
;
1146 /* Assign hard registers to reload pseudos and other pseudos. */
1148 assign_by_spills (void)
1150 int i
, n
, nfails
, iter
, regno
, hard_regno
, cost
, restore_regno
;
1153 bitmap_head changed_insns
, do_not_assign_nonreload_pseudos
;
1154 bitmap_head non_reload_pseudos
;
1158 int max_regno
= max_reg_num ();
1160 for (n
= 0, i
= lra_constraint_new_regno_start
; i
< max_regno
; i
++)
1161 if (reg_renumber
[i
] < 0 && lra_reg_info
[i
].nrefs
!= 0
1162 && regno_allocno_class_array
[i
] != NO_REGS
)
1163 sorted_pseudos
[n
++] = i
;
1164 bitmap_initialize (&insn_conflict_pseudos
, ®_obstack
);
1165 bitmap_initialize (&spill_pseudos_bitmap
, ®_obstack
);
1166 bitmap_initialize (&best_spill_pseudos_bitmap
, ®_obstack
);
1167 update_hard_regno_preference_check
= XCNEWVEC (int, max_regno
);
1168 curr_update_hard_regno_preference_check
= 0;
1169 memset (try_hard_reg_pseudos_check
, 0, sizeof (try_hard_reg_pseudos_check
));
1170 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1171 bitmap_initialize (&try_hard_reg_pseudos
[i
], ®_obstack
);
1172 curr_pseudo_check
= 0;
1173 bitmap_initialize (&changed_insns
, ®_obstack
);
1174 bitmap_initialize (&non_reload_pseudos
, ®_obstack
);
1175 bitmap_ior (&non_reload_pseudos
, &lra_inheritance_pseudos
, &lra_split_regs
);
1176 bitmap_ior_into (&non_reload_pseudos
, &lra_optional_reload_pseudos
);
1177 for (iter
= 0; iter
<= 1; iter
++)
1179 qsort (sorted_pseudos
, n
, sizeof (int), reload_pseudo_compare_func
);
1181 for (i
= 0; i
< n
; i
++)
1183 regno
= sorted_pseudos
[i
];
1184 if (lra_dump_file
!= NULL
)
1185 fprintf (lra_dump_file
, " Assigning to %d "
1186 "(cl=%s, orig=%d, freq=%d, tfirst=%d, tfreq=%d)...\n",
1187 regno
, reg_class_names
[regno_allocno_class_array
[regno
]],
1188 ORIGINAL_REGNO (regno_reg_rtx
[regno
]),
1189 lra_reg_info
[regno
].freq
, regno_assign_info
[regno
].first
,
1190 regno_assign_info
[regno_assign_info
[regno
].first
].freq
);
1191 hard_regno
= find_hard_regno_for (regno
, &cost
, -1);
1192 reload_p
= ! bitmap_bit_p (&non_reload_pseudos
, regno
);
1193 if (hard_regno
< 0 && reload_p
)
1194 hard_regno
= spill_for (regno
, &all_spilled_pseudos
);
1198 sorted_pseudos
[nfails
++] = regno
;
1202 /* This register might have been spilled by the previous
1203 pass. Indicate that it is no longer spilled. */
1204 bitmap_clear_bit (&all_spilled_pseudos
, regno
);
1205 assign_hard_regno (hard_regno
, regno
);
1207 /* As non-reload pseudo assignment is changed we
1208 should reconsider insns referring for the
1210 bitmap_set_bit (&changed_pseudo_bitmap
, regno
);
1217 /* We did not assign hard regs to reload pseudos after two
1218 iteration. It means something is wrong with asm insn
1219 constraints. Report it. */
1221 bitmap_head failed_reload_insns
;
1223 bitmap_initialize (&failed_reload_insns
, ®_obstack
);
1224 for (i
= 0; i
< nfails
; i
++)
1226 regno
= sorted_pseudos
[i
];
1227 bitmap_ior_into (&failed_reload_insns
,
1228 &lra_reg_info
[regno
].insn_bitmap
);
1229 /* Assign an arbitrary hard register of regno class to
1230 avoid further trouble with the asm insns. */
1231 bitmap_clear_bit (&all_spilled_pseudos
, regno
);
1233 (ira_class_hard_regs
[regno_allocno_class_array
[regno
]][0],
1236 EXECUTE_IF_SET_IN_BITMAP (&failed_reload_insns
, 0, u
, bi
)
1238 insn
= lra_insn_recog_data
[u
]->insn
;
1239 if (asm_noperands (PATTERN (insn
)) >= 0)
1242 error_for_asm (insn
,
1243 "%<asm%> operand has impossible constraints");
1249 /* This is a very rare event. We can not assign a hard
1250 register to reload pseudo because the hard register was
1251 assigned to another reload pseudo on a previous
1252 assignment pass. For x86 example, on the 1st pass we
1253 assigned CX (although another hard register could be used
1254 for this) to reload pseudo in an insn, on the 2nd pass we
1255 need CX (and only this) hard register for a new reload
1256 pseudo in the same insn. */
1257 if (lra_dump_file
!= NULL
)
1258 fprintf (lra_dump_file
, " 2nd iter for reload pseudo assignments:\n");
1259 for (i
= 0; i
< nfails
; i
++)
1261 if (lra_dump_file
!= NULL
)
1262 fprintf (lra_dump_file
, " Reload r%d assignment failure\n",
1264 bitmap_ior_into (&changed_insns
,
1265 &lra_reg_info
[sorted_pseudos
[i
]].insn_bitmap
);
1268 FOR_BB_INSNS (bb
, insn
)
1269 if (bitmap_bit_p (&changed_insns
, INSN_UID (insn
)))
1271 lra_insn_recog_data_t data
;
1272 struct lra_insn_reg
*r
;
1274 data
= lra_get_insn_recog_data (insn
);
1275 for (r
= data
->regs
; r
!= NULL
; r
= r
->next
)
1278 /* A reload pseudo did not get a hard register on the
1279 first iteration because of the conflict with
1280 another reload pseudos in the same insn. So we
1281 consider only reload pseudos assigned to hard
1282 registers. We shall exclude inheritance pseudos as
1283 they can occur in original insns (not reload ones).
1284 We can omit the check for split pseudos because
1285 they occur only in move insns containing non-reload
1287 if (regno
< lra_constraint_new_regno_start
1288 || bitmap_bit_p (&lra_inheritance_pseudos
, regno
)
1289 || reg_renumber
[regno
] < 0)
1291 sorted_pseudos
[nfails
++] = regno
;
1292 if (lra_dump_file
!= NULL
)
1293 fprintf (lra_dump_file
,
1294 " Spill reload r%d(hr=%d, freq=%d)\n",
1295 regno
, reg_renumber
[regno
],
1296 lra_reg_info
[regno
].freq
);
1297 update_lives (regno
, true);
1298 lra_setup_reg_renumber (regno
, -1, false);
1303 improve_inheritance (&changed_pseudo_bitmap
);
1304 bitmap_clear (&non_reload_pseudos
);
1305 bitmap_clear (&changed_insns
);
1308 /* We should not assign to original pseudos of inheritance
1309 pseudos or split pseudos if any its inheritance pseudo did
1310 not get hard register or any its split pseudo was not split
1311 because undo inheritance/split pass will extend live range of
1312 such inheritance or split pseudos. */
1313 bitmap_initialize (&do_not_assign_nonreload_pseudos
, ®_obstack
);
1314 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos
, 0, u
, bi
)
1315 if ((restore_regno
= lra_reg_info
[u
].restore_regno
) >= 0
1316 && reg_renumber
[u
] < 0
1317 && bitmap_bit_p (&lra_inheritance_pseudos
, u
))
1318 bitmap_set_bit (&do_not_assign_nonreload_pseudos
, restore_regno
);
1319 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs
, 0, u
, bi
)
1320 if ((restore_regno
= lra_reg_info
[u
].restore_regno
) >= 0
1321 && reg_renumber
[u
] >= 0)
1322 bitmap_set_bit (&do_not_assign_nonreload_pseudos
, restore_regno
);
1323 for (n
= 0, i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
1324 if (((i
< lra_constraint_new_regno_start
1325 && ! bitmap_bit_p (&do_not_assign_nonreload_pseudos
, i
))
1326 || (bitmap_bit_p (&lra_inheritance_pseudos
, i
)
1327 && lra_reg_info
[i
].restore_regno
>= 0)
1328 || (bitmap_bit_p (&lra_split_regs
, i
)
1329 && lra_reg_info
[i
].restore_regno
>= 0)
1330 || bitmap_bit_p (&lra_optional_reload_pseudos
, i
))
1331 && reg_renumber
[i
] < 0 && lra_reg_info
[i
].nrefs
!= 0
1332 && regno_allocno_class_array
[i
] != NO_REGS
)
1333 sorted_pseudos
[n
++] = i
;
1334 bitmap_clear (&do_not_assign_nonreload_pseudos
);
1335 if (n
!= 0 && lra_dump_file
!= NULL
)
1336 fprintf (lra_dump_file
, " Reassigning non-reload pseudos\n");
1337 qsort (sorted_pseudos
, n
, sizeof (int), pseudo_compare_func
);
1338 for (i
= 0; i
< n
; i
++)
1340 regno
= sorted_pseudos
[i
];
1341 hard_regno
= find_hard_regno_for (regno
, &cost
, -1);
1342 if (hard_regno
>= 0)
1344 assign_hard_regno (hard_regno
, regno
);
1345 /* We change allocation for non-reload pseudo on this
1346 iteration -- mark the pseudo for invalidation of used
1347 alternatives of insns containing the pseudo. */
1348 bitmap_set_bit (&changed_pseudo_bitmap
, regno
);
1352 free (update_hard_regno_preference_check
);
1353 bitmap_clear (&best_spill_pseudos_bitmap
);
1354 bitmap_clear (&spill_pseudos_bitmap
);
1355 bitmap_clear (&insn_conflict_pseudos
);
1359 /* Entry function to assign hard registers to new reload pseudos
1360 starting with LRA_CONSTRAINT_NEW_REGNO_START (by possible spilling
1361 of old pseudos) and possibly to the old pseudos. The function adds
1362 what insns to process for the next constraint pass. Those are all
1363 insns who contains non-reload and non-inheritance pseudos with
1366 Return true if we did not spill any non-reload and non-inheritance
1374 bitmap_head insns_to_process
;
1376 int max_regno
= max_reg_num ();
1378 timevar_push (TV_LRA_ASSIGN
);
1380 sorted_pseudos
= XNEWVEC (int, max_regno
);
1381 sorted_reload_pseudos
= XNEWVEC (int, max_regno
);
1382 regno_allocno_class_array
= XNEWVEC (enum reg_class
, max_regno
);
1383 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
1384 regno_allocno_class_array
[i
] = lra_get_allocno_class (i
);
1385 init_regno_assign_info ();
1386 bitmap_initialize (&all_spilled_pseudos
, ®_obstack
);
1387 create_live_range_start_chains ();
1388 setup_live_pseudos_and_spill_after_risky_transforms (&all_spilled_pseudos
);
1389 #ifdef ENABLE_CHECKING
1390 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
1391 if (lra_reg_info
[i
].nrefs
!= 0 && reg_renumber
[i
] >= 0
1392 && lra_reg_info
[i
].call_p
1393 && overlaps_hard_reg_set_p (call_used_reg_set
,
1394 PSEUDO_REGNO_MODE (i
), reg_renumber
[i
]))
1397 /* Setup insns to process on the next constraint pass. */
1398 bitmap_initialize (&changed_pseudo_bitmap
, ®_obstack
);
1399 init_live_reload_and_inheritance_pseudos ();
1400 assign_by_spills ();
1401 finish_live_reload_and_inheritance_pseudos ();
1402 bitmap_ior_into (&changed_pseudo_bitmap
, &all_spilled_pseudos
);
1404 EXECUTE_IF_SET_IN_BITMAP (&all_spilled_pseudos
, 0, u
, bi
)
1405 /* We ignore spilled pseudos created on last inheritance pass
1406 because they will be removed. */
1407 if (lra_reg_info
[u
].restore_regno
< 0)
1409 no_spills_p
= false;
1412 finish_live_range_start_chains ();
1413 bitmap_clear (&all_spilled_pseudos
);
1414 bitmap_initialize (&insns_to_process
, ®_obstack
);
1415 EXECUTE_IF_SET_IN_BITMAP (&changed_pseudo_bitmap
, 0, u
, bi
)
1416 bitmap_ior_into (&insns_to_process
, &lra_reg_info
[u
].insn_bitmap
);
1417 bitmap_clear (&changed_pseudo_bitmap
);
1418 EXECUTE_IF_SET_IN_BITMAP (&insns_to_process
, 0, u
, bi
)
1420 lra_push_insn_by_uid (u
);
1421 /* Invalidate alternatives for insn should be processed. */
1422 lra_set_used_insn_alternative_by_uid (u
, -1);
1424 bitmap_clear (&insns_to_process
);
1425 finish_regno_assign_info ();
1426 free (regno_allocno_class_array
);
1427 free (sorted_pseudos
);
1428 free (sorted_reload_pseudos
);
1430 timevar_pop (TV_LRA_ASSIGN
);