1 /* Assign reload pseudos.
2 Copyright (C) 2010-2013 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file's main objective is to assign hard registers to reload
23 pseudos. It also tries to allocate hard registers to other
24 pseudos, but at a lower priority than the reload pseudos. The pass
25 does not transform the RTL.
27 We must allocate a hard register to every reload pseudo. We try to
28 increase the chances of finding a viable allocation by assigning
29 the pseudos in order of fewest available hard registers first. If
30 we still fail to find a hard register, we spill other (non-reload)
31 pseudos in order to make room.
33 find_hard_regno_for finds hard registers for allocation without
34 spilling. spill_for does the same with spilling. Both functions
35 use a cost model to determine the most profitable choice of hard
38 Once we have finished allocating reload pseudos, we also try to
39 assign registers to other (non-reload) pseudos. This is useful if
40 hard registers were freed up by the spilling just described.
42 We try to assign hard registers by collecting pseudos into threads.
43 These threads contain reload and inheritance pseudos that are
44 connected by copies (move insns). Doing this improves the chances
45 of pseudos in the thread getting the same hard register and, as a
46 result, of allowing some move insns to be deleted.
48 When we assign a hard register to a pseudo, we decrease the cost of
49 using the same hard register for pseudos that are connected by
52 If two hard registers have the same frequency-derived cost, we
53 prefer hard registers with higher priorities. The mapping of
54 registers to priorities is controlled by the register_priority
55 target hook. For example, x86-64 has a few register priorities:
56 hard registers with and without REX prefixes have different
57 priorities. This permits us to generate smaller code as insns
58 without REX prefixes are shorter.
60 If a few hard registers are still equally good for the assignment,
61 we choose the least used hard register. It is called leveling and
62 may be profitable for some targets.
64 Only insns with changed allocation pseudos are processed on the
67 The pseudo live-ranges are used to find conflicting pseudos.
69 For understanding the code, it is important to keep in mind that
70 inheritance, split, and reload pseudos created since last
71 constraint pass have regno >= lra_constraint_new_regno_start.
72 Inheritance and split pseudos created on any pass are in the
73 corresponding bitmaps. Inheritance and split pseudos since the
74 last constraint pass have also the corresponding non-negative
79 #include "coretypes.h"
81 #include "hard-reg-set.h"
83 #include "rtl-error.h"
86 #include "insn-config.h"
92 #include "basic-block.h"
96 #include "sparseset.h"
99 /* Array containing corresponding values of function
100 lra_get_allocno_class. It is used to speed up the code. */
101 static enum reg_class
*regno_allocno_class_array
;
103 /* Information about the thread to which a pseudo belongs. Threads are
104 a set of connected reload and inheritance pseudos with the same set of
105 available hard registers. Lone registers belong to their own threads. */
106 struct regno_assign_info
108 /* First/next pseudo of the same thread. */
110 /* Frequency of the thread (execution frequency of only reload
111 pseudos in the thread when the thread contains a reload pseudo).
112 Defined only for the first thread pseudo. */
116 /* Map regno to the corresponding regno assignment info. */
117 static struct regno_assign_info
*regno_assign_info
;
119 /* Process a pseudo copy with execution frequency COPY_FREQ connecting
120 REGNO1 and REGNO2 to form threads. */
122 process_copy_to_form_thread (int regno1
, int regno2
, int copy_freq
)
124 int last
, regno1_first
, regno2_first
;
126 lra_assert (regno1
>= lra_constraint_new_regno_start
127 && regno2
>= lra_constraint_new_regno_start
);
128 regno1_first
= regno_assign_info
[regno1
].first
;
129 regno2_first
= regno_assign_info
[regno2
].first
;
130 if (regno1_first
!= regno2_first
)
132 for (last
= regno2_first
;
133 regno_assign_info
[last
].next
>= 0;
134 last
= regno_assign_info
[last
].next
)
135 regno_assign_info
[last
].first
= regno1_first
;
136 regno_assign_info
[last
].first
= regno1_first
;
137 regno_assign_info
[last
].next
= regno_assign_info
[regno1_first
].next
;
138 regno_assign_info
[regno1_first
].next
= regno2_first
;
139 regno_assign_info
[regno1_first
].freq
140 += regno_assign_info
[regno2_first
].freq
;
142 regno_assign_info
[regno1_first
].freq
-= 2 * copy_freq
;
143 lra_assert (regno_assign_info
[regno1_first
].freq
>= 0);
146 /* Initialize REGNO_ASSIGN_INFO and form threads. */
148 init_regno_assign_info (void)
150 int i
, regno1
, regno2
, max_regno
= max_reg_num ();
153 regno_assign_info
= XNEWVEC (struct regno_assign_info
, max_regno
);
154 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
156 regno_assign_info
[i
].first
= i
;
157 regno_assign_info
[i
].next
= -1;
158 regno_assign_info
[i
].freq
= lra_reg_info
[i
].freq
;
160 /* Form the threads. */
161 for (i
= 0; (cp
= lra_get_copy (i
)) != NULL
; i
++)
162 if ((regno1
= cp
->regno1
) >= lra_constraint_new_regno_start
163 && (regno2
= cp
->regno2
) >= lra_constraint_new_regno_start
164 && reg_renumber
[regno1
] < 0 && lra_reg_info
[regno1
].nrefs
!= 0
165 && reg_renumber
[regno2
] < 0 && lra_reg_info
[regno2
].nrefs
!= 0
166 && (ira_class_hard_regs_num
[regno_allocno_class_array
[regno1
]]
167 == ira_class_hard_regs_num
[regno_allocno_class_array
[regno2
]]))
168 process_copy_to_form_thread (regno1
, regno2
, cp
->freq
);
171 /* Free REGNO_ASSIGN_INFO. */
173 finish_regno_assign_info (void)
175 free (regno_assign_info
);
178 /* The function is used to sort *reload* and *inheritance* pseudos to
179 try to assign them hard registers. We put pseudos from the same
180 thread always nearby. */
182 reload_pseudo_compare_func (const void *v1p
, const void *v2p
)
184 int r1
= *(const int *) v1p
, r2
= *(const int *) v2p
;
185 enum reg_class cl1
= regno_allocno_class_array
[r1
];
186 enum reg_class cl2
= regno_allocno_class_array
[r2
];
189 lra_assert (r1
>= lra_constraint_new_regno_start
190 && r2
>= lra_constraint_new_regno_start
);
192 /* Prefer to assign reload registers with smaller classes first to
193 guarantee assignment to all reload registers. */
194 if ((diff
= (ira_class_hard_regs_num
[cl1
]
195 - ira_class_hard_regs_num
[cl2
])) != 0)
197 if ((diff
= (regno_assign_info
[regno_assign_info
[r2
].first
].freq
198 - regno_assign_info
[regno_assign_info
[r1
].first
].freq
)) != 0)
200 /* Allocate bigger pseudos first to avoid register file
203 = (ira_reg_class_max_nregs
[cl2
][lra_reg_info
[r2
].biggest_mode
]
204 - ira_reg_class_max_nregs
[cl1
][lra_reg_info
[r1
].biggest_mode
])) != 0)
206 /* Put pseudos from the thread nearby. */
207 if ((diff
= regno_assign_info
[r1
].first
- regno_assign_info
[r2
].first
) != 0)
209 /* If regs are equally good, sort by their numbers, so that the
210 results of qsort leave nothing to chance. */
214 /* The function is used to sort *non-reload* pseudos to try to assign
215 them hard registers. The order calculation is simpler than in the
216 previous function and based on the pseudo frequency usage. */
218 pseudo_compare_func (const void *v1p
, const void *v2p
)
220 int r1
= *(const int *) v1p
, r2
= *(const int *) v2p
;
223 /* Prefer to assign more frequently used registers first. */
224 if ((diff
= lra_reg_info
[r2
].freq
- lra_reg_info
[r1
].freq
) != 0)
227 /* If regs are equally good, sort by their numbers, so that the
228 results of qsort leave nothing to chance. */
232 /* Arrays of size LRA_LIVE_MAX_POINT mapping a program point to the
233 pseudo live ranges with given start point. We insert only live
234 ranges of pseudos interesting for assignment purposes. They are
235 reload pseudos and pseudos assigned to hard registers. */
236 static lra_live_range_t
*start_point_ranges
;
238 /* Used as a flag that a live range is not inserted in the start point
240 static struct lra_live_range not_in_chain_mark
;
242 /* Create and set up START_POINT_RANGES. */
244 create_live_range_start_chains (void)
249 start_point_ranges
= XCNEWVEC (lra_live_range_t
, lra_live_max_point
);
250 max_regno
= max_reg_num ();
251 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
252 if (i
>= lra_constraint_new_regno_start
|| reg_renumber
[i
] >= 0)
254 for (r
= lra_reg_info
[i
].live_ranges
; r
!= NULL
; r
= r
->next
)
256 r
->start_next
= start_point_ranges
[r
->start
];
257 start_point_ranges
[r
->start
] = r
;
262 for (r
= lra_reg_info
[i
].live_ranges
; r
!= NULL
; r
= r
->next
)
263 r
->start_next
= ¬_in_chain_mark
;
267 /* Insert live ranges of pseudo REGNO into start chains if they are
270 insert_in_live_range_start_chain (int regno
)
272 lra_live_range_t r
= lra_reg_info
[regno
].live_ranges
;
274 if (r
->start_next
!= ¬_in_chain_mark
)
276 for (; r
!= NULL
; r
= r
->next
)
278 r
->start_next
= start_point_ranges
[r
->start
];
279 start_point_ranges
[r
->start
] = r
;
283 /* Free START_POINT_RANGES. */
285 finish_live_range_start_chains (void)
287 gcc_assert (start_point_ranges
!= NULL
);
288 free (start_point_ranges
);
289 start_point_ranges
= NULL
;
292 /* Map: program point -> bitmap of all pseudos living at the point and
293 assigned to hard registers. */
294 static bitmap_head
*live_hard_reg_pseudos
;
295 static bitmap_obstack live_hard_reg_pseudos_bitmap_obstack
;
297 /* reg_renumber corresponding to pseudos marked in
298 live_hard_reg_pseudos. reg_renumber might be not matched to
299 live_hard_reg_pseudos but live_pseudos_reg_renumber always reflects
300 live_hard_reg_pseudos. */
301 static int *live_pseudos_reg_renumber
;
303 /* Sparseset used to calculate living hard reg pseudos for some program
305 static sparseset live_range_hard_reg_pseudos
;
307 /* Sparseset used to calculate living reload/inheritance pseudos for
308 some program point range. */
309 static sparseset live_range_reload_inheritance_pseudos
;
311 /* Allocate and initialize the data about living pseudos at program
316 int i
, max_regno
= max_reg_num ();
318 live_range_hard_reg_pseudos
= sparseset_alloc (max_regno
);
319 live_range_reload_inheritance_pseudos
= sparseset_alloc (max_regno
);
320 live_hard_reg_pseudos
= XNEWVEC (bitmap_head
, lra_live_max_point
);
321 bitmap_obstack_initialize (&live_hard_reg_pseudos_bitmap_obstack
);
322 for (i
= 0; i
< lra_live_max_point
; i
++)
323 bitmap_initialize (&live_hard_reg_pseudos
[i
],
324 &live_hard_reg_pseudos_bitmap_obstack
);
325 live_pseudos_reg_renumber
= XNEWVEC (int, max_regno
);
326 for (i
= 0; i
< max_regno
; i
++)
327 live_pseudos_reg_renumber
[i
] = -1;
330 /* Free the data about living pseudos at program points. */
334 sparseset_free (live_range_hard_reg_pseudos
);
335 sparseset_free (live_range_reload_inheritance_pseudos
);
336 free (live_hard_reg_pseudos
);
337 bitmap_obstack_release (&live_hard_reg_pseudos_bitmap_obstack
);
338 free (live_pseudos_reg_renumber
);
341 /* Update the LIVE_HARD_REG_PSEUDOS and LIVE_PSEUDOS_REG_RENUMBER
342 entries for pseudo REGNO. Assume that the register has been
343 spilled if FREE_P, otherwise assume that it has been assigned
344 reg_renumber[REGNO] (if >= 0). We also insert the pseudo live
345 ranges in the start chains when it is assumed to be assigned to a
346 hard register because we use the chains of pseudos assigned to hard
347 registers during allocation. */
349 update_lives (int regno
, bool free_p
)
354 if (reg_renumber
[regno
] < 0)
356 live_pseudos_reg_renumber
[regno
] = free_p
? -1 : reg_renumber
[regno
];
357 for (r
= lra_reg_info
[regno
].live_ranges
; r
!= NULL
; r
= r
->next
)
359 for (p
= r
->start
; p
<= r
->finish
; p
++)
361 bitmap_clear_bit (&live_hard_reg_pseudos
[p
], regno
);
364 bitmap_set_bit (&live_hard_reg_pseudos
[p
], regno
);
365 insert_in_live_range_start_chain (regno
);
370 /* Sparseset used to calculate reload pseudos conflicting with a given
371 pseudo when we are trying to find a hard register for the given
373 static sparseset conflict_reload_and_inheritance_pseudos
;
375 /* Map: program point -> bitmap of all reload and inheritance pseudos
376 living at the point. */
377 static bitmap_head
*live_reload_and_inheritance_pseudos
;
378 static bitmap_obstack live_reload_and_inheritance_pseudos_bitmap_obstack
;
380 /* Allocate and initialize data about living reload pseudos at any
381 given program point. */
383 init_live_reload_and_inheritance_pseudos (void)
385 int i
, p
, max_regno
= max_reg_num ();
388 conflict_reload_and_inheritance_pseudos
= sparseset_alloc (max_regno
);
389 live_reload_and_inheritance_pseudos
= XNEWVEC (bitmap_head
, lra_live_max_point
);
390 bitmap_obstack_initialize (&live_reload_and_inheritance_pseudos_bitmap_obstack
);
391 for (p
= 0; p
< lra_live_max_point
; p
++)
392 bitmap_initialize (&live_reload_and_inheritance_pseudos
[p
],
393 &live_reload_and_inheritance_pseudos_bitmap_obstack
);
394 for (i
= lra_constraint_new_regno_start
; i
< max_regno
; i
++)
396 for (r
= lra_reg_info
[i
].live_ranges
; r
!= NULL
; r
= r
->next
)
397 for (p
= r
->start
; p
<= r
->finish
; p
++)
398 bitmap_set_bit (&live_reload_and_inheritance_pseudos
[p
], i
);
402 /* Finalize data about living reload pseudos at any given program
405 finish_live_reload_and_inheritance_pseudos (void)
407 sparseset_free (conflict_reload_and_inheritance_pseudos
);
408 free (live_reload_and_inheritance_pseudos
);
409 bitmap_obstack_release (&live_reload_and_inheritance_pseudos_bitmap_obstack
);
412 /* The value used to check that cost of given hard reg is really
413 defined currently. */
414 static int curr_hard_regno_costs_check
= 0;
415 /* Array used to check that cost of the corresponding hard reg (the
416 array element index) is really defined currently. */
417 static int hard_regno_costs_check
[FIRST_PSEUDO_REGISTER
];
418 /* The current costs of allocation of hard regs. Defined only if the
419 value of the corresponding element of the previous array is equal to
420 CURR_HARD_REGNO_COSTS_CHECK. */
421 static int hard_regno_costs
[FIRST_PSEUDO_REGISTER
];
423 /* Adjust cost of HARD_REGNO by INCR. Reset the cost first if it is
426 adjust_hard_regno_cost (int hard_regno
, int incr
)
428 if (hard_regno_costs_check
[hard_regno
] != curr_hard_regno_costs_check
)
429 hard_regno_costs
[hard_regno
] = 0;
430 hard_regno_costs_check
[hard_regno
] = curr_hard_regno_costs_check
;
431 hard_regno_costs
[hard_regno
] += incr
;
434 /* Try to find a free hard register for pseudo REGNO. Return the
435 hard register on success and set *COST to the cost of using
436 that register. (If several registers have equal cost, the one with
437 the highest priority wins.) Return -1 on failure.
439 If TRY_ONLY_HARD_REGNO >= 0, consider only that hard register,
440 otherwise consider all hard registers in REGNO's class. */
442 find_hard_regno_for (int regno
, int *cost
, int try_only_hard_regno
)
444 HARD_REG_SET conflict_set
;
445 int best_cost
= INT_MAX
, best_priority
= INT_MIN
, best_usage
= INT_MAX
;
447 int p
, i
, j
, rclass_size
, best_hard_regno
, priority
, hard_regno
;
448 int hr
, conflict_hr
, nregs
;
449 enum machine_mode biggest_mode
;
450 unsigned int k
, conflict_regno
;
451 int offset
, val
, biggest_nregs
, nregs_diff
;
452 enum reg_class rclass
;
454 bool *rclass_intersect_p
;
455 HARD_REG_SET impossible_start_hard_regs
;
457 COPY_HARD_REG_SET (conflict_set
, lra_no_alloc_regs
);
458 rclass
= regno_allocno_class_array
[regno
];
459 rclass_intersect_p
= ira_reg_classes_intersect_p
[rclass
];
460 curr_hard_regno_costs_check
++;
461 sparseset_clear (conflict_reload_and_inheritance_pseudos
);
462 sparseset_clear (live_range_hard_reg_pseudos
);
463 IOR_HARD_REG_SET (conflict_set
, lra_reg_info
[regno
].conflict_hard_regs
);
464 biggest_mode
= lra_reg_info
[regno
].biggest_mode
;
465 for (r
= lra_reg_info
[regno
].live_ranges
; r
!= NULL
; r
= r
->next
)
467 EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos
[r
->start
], 0, k
, bi
)
468 if (rclass_intersect_p
[regno_allocno_class_array
[k
]])
469 sparseset_set_bit (live_range_hard_reg_pseudos
, k
);
470 EXECUTE_IF_SET_IN_BITMAP (&live_reload_and_inheritance_pseudos
[r
->start
],
472 if (lra_reg_info
[k
].preferred_hard_regno1
>= 0
473 && live_pseudos_reg_renumber
[k
] < 0
474 && rclass_intersect_p
[regno_allocno_class_array
[k
]])
475 sparseset_set_bit (conflict_reload_and_inheritance_pseudos
, k
);
476 for (p
= r
->start
+ 1; p
<= r
->finish
; p
++)
480 for (r2
= start_point_ranges
[p
];
484 if (r2
->regno
>= lra_constraint_new_regno_start
485 && lra_reg_info
[r2
->regno
].preferred_hard_regno1
>= 0
486 && live_pseudos_reg_renumber
[r2
->regno
] < 0
487 && rclass_intersect_p
[regno_allocno_class_array
[r2
->regno
]])
488 sparseset_set_bit (conflict_reload_and_inheritance_pseudos
,
490 if (live_pseudos_reg_renumber
[r2
->regno
] >= 0
491 && rclass_intersect_p
[regno_allocno_class_array
[r2
->regno
]])
492 sparseset_set_bit (live_range_hard_reg_pseudos
, r2
->regno
);
496 if ((hard_regno
= lra_reg_info
[regno
].preferred_hard_regno1
) >= 0)
498 adjust_hard_regno_cost
499 (hard_regno
, -lra_reg_info
[regno
].preferred_hard_regno_profit1
);
500 if ((hard_regno
= lra_reg_info
[regno
].preferred_hard_regno2
) >= 0)
501 adjust_hard_regno_cost
502 (hard_regno
, -lra_reg_info
[regno
].preferred_hard_regno_profit2
);
505 if (lra_reg_info
[regno
].no_stack_p
)
506 for (i
= FIRST_STACK_REG
; i
<= LAST_STACK_REG
; i
++)
507 SET_HARD_REG_BIT (conflict_set
, i
);
509 sparseset_clear_bit (conflict_reload_and_inheritance_pseudos
, regno
);
510 val
= lra_reg_info
[regno
].val
;
511 offset
= lra_reg_info
[regno
].offset
;
512 CLEAR_HARD_REG_SET (impossible_start_hard_regs
);
513 EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos
, conflict_regno
)
514 if (lra_reg_val_equal_p (conflict_regno
, val
, offset
))
516 conflict_hr
= live_pseudos_reg_renumber
[conflict_regno
];
517 nregs
= (hard_regno_nregs
[conflict_hr
]
518 [lra_reg_info
[conflict_regno
].biggest_mode
]);
519 /* Remember about multi-register pseudos. For example, 2 hard
520 register pseudos can start on the same hard register but can
521 not start on HR and HR+1/HR-1. */
522 for (hr
= conflict_hr
+ 1;
523 hr
< FIRST_PSEUDO_REGISTER
&& hr
< conflict_hr
+ nregs
;
525 SET_HARD_REG_BIT (impossible_start_hard_regs
, hr
);
526 for (hr
= conflict_hr
- 1;
527 hr
>= 0 && hr
+ hard_regno_nregs
[hr
][biggest_mode
] > conflict_hr
;
529 SET_HARD_REG_BIT (impossible_start_hard_regs
, hr
);
533 add_to_hard_reg_set (&conflict_set
,
534 lra_reg_info
[conflict_regno
].biggest_mode
,
535 live_pseudos_reg_renumber
[conflict_regno
]);
536 if (hard_reg_set_subset_p (reg_class_contents
[rclass
],
540 EXECUTE_IF_SET_IN_SPARSESET (conflict_reload_and_inheritance_pseudos
,
542 if (!lra_reg_val_equal_p (conflict_regno
, val
, offset
))
544 lra_assert (live_pseudos_reg_renumber
[conflict_regno
] < 0);
546 = lra_reg_info
[conflict_regno
].preferred_hard_regno1
) >= 0)
548 adjust_hard_regno_cost
550 lra_reg_info
[conflict_regno
].preferred_hard_regno_profit1
);
552 = lra_reg_info
[conflict_regno
].preferred_hard_regno2
) >= 0)
553 adjust_hard_regno_cost
555 lra_reg_info
[conflict_regno
].preferred_hard_regno_profit2
);
558 /* Make sure that all registers in a multi-word pseudo belong to the
560 IOR_COMPL_HARD_REG_SET (conflict_set
, reg_class_contents
[rclass
]);
561 lra_assert (rclass
!= NO_REGS
);
562 rclass_size
= ira_class_hard_regs_num
[rclass
];
563 best_hard_regno
= -1;
564 hard_regno
= ira_class_hard_regs
[rclass
][0];
565 biggest_nregs
= hard_regno_nregs
[hard_regno
][biggest_mode
];
566 nregs_diff
= (biggest_nregs
567 - hard_regno_nregs
[hard_regno
][PSEUDO_REGNO_MODE (regno
)]);
568 for (i
= 0; i
< rclass_size
; i
++)
570 if (try_only_hard_regno
>= 0)
571 hard_regno
= try_only_hard_regno
;
573 hard_regno
= ira_class_hard_regs
[rclass
][i
];
574 if (! overlaps_hard_reg_set_p (conflict_set
,
575 PSEUDO_REGNO_MODE (regno
), hard_regno
)
576 /* We can not use prohibited_class_mode_regs because it is
577 not defined for all classes. */
578 && HARD_REGNO_MODE_OK (hard_regno
, PSEUDO_REGNO_MODE (regno
))
579 && ! TEST_HARD_REG_BIT (impossible_start_hard_regs
, hard_regno
)
582 ? (hard_regno
- nregs_diff
>= 0
583 && TEST_HARD_REG_BIT (reg_class_contents
[rclass
],
584 hard_regno
- nregs_diff
))
585 : TEST_HARD_REG_BIT (reg_class_contents
[rclass
],
586 hard_regno
+ nregs_diff
))))
588 if (hard_regno_costs_check
[hard_regno
]
589 != curr_hard_regno_costs_check
)
591 hard_regno_costs_check
[hard_regno
] = curr_hard_regno_costs_check
;
592 hard_regno_costs
[hard_regno
] = 0;
595 j
< hard_regno_nregs
[hard_regno
][PSEUDO_REGNO_MODE (regno
)];
597 if (! TEST_HARD_REG_BIT (call_used_reg_set
, hard_regno
+ j
)
598 && ! df_regs_ever_live_p (hard_regno
+ j
))
599 /* It needs save restore. */
600 hard_regno_costs
[hard_regno
]
601 += 2 * ENTRY_BLOCK_PTR
->next_bb
->frequency
;
602 priority
= targetm
.register_priority (hard_regno
);
603 if (best_hard_regno
< 0 || hard_regno_costs
[hard_regno
] < best_cost
604 || (hard_regno_costs
[hard_regno
] == best_cost
605 && (priority
> best_priority
606 /* Hard register usage leveling actually results
607 in bigger code for targets with conditional
608 execution like ARM because it reduces chance
609 of if-conversion after LRA. */
610 || (! targetm
.have_conditional_execution ()
611 && priority
== best_priority
612 && best_usage
> lra_hard_reg_usage
[hard_regno
]))))
614 best_hard_regno
= hard_regno
;
615 best_cost
= hard_regno_costs
[hard_regno
];
616 best_priority
= priority
;
617 best_usage
= lra_hard_reg_usage
[hard_regno
];
620 if (try_only_hard_regno
>= 0)
623 if (best_hard_regno
>= 0)
624 *cost
= best_cost
- lra_reg_info
[regno
].freq
;
625 return best_hard_regno
;
628 /* Current value used for checking elements in
629 update_hard_regno_preference_check. */
630 static int curr_update_hard_regno_preference_check
;
631 /* If an element value is equal to the above variable value, then the
632 corresponding regno has been processed for preference
634 static int *update_hard_regno_preference_check
;
636 /* Update the preference for using HARD_REGNO for pseudos that are
637 connected directly or indirectly with REGNO. Apply divisor DIV
638 to any preference adjustments.
640 The more indirectly a pseudo is connected, the smaller its effect
641 should be. We therefore increase DIV on each "hop". */
643 update_hard_regno_preference (int regno
, int hard_regno
, int div
)
645 int another_regno
, cost
;
646 lra_copy_t cp
, next_cp
;
648 /* Search depth 5 seems to be enough. */
651 for (cp
= lra_reg_info
[regno
].copies
; cp
!= NULL
; cp
= next_cp
)
653 if (cp
->regno1
== regno
)
655 next_cp
= cp
->regno1_next
;
656 another_regno
= cp
->regno2
;
658 else if (cp
->regno2
== regno
)
660 next_cp
= cp
->regno2_next
;
661 another_regno
= cp
->regno1
;
665 if (reg_renumber
[another_regno
] < 0
666 && (update_hard_regno_preference_check
[another_regno
]
667 != curr_update_hard_regno_preference_check
))
669 update_hard_regno_preference_check
[another_regno
]
670 = curr_update_hard_regno_preference_check
;
671 cost
= cp
->freq
< div
? 1 : cp
->freq
/ div
;
672 lra_setup_reload_pseudo_preferenced_hard_reg
673 (another_regno
, hard_regno
, cost
);
674 update_hard_regno_preference (another_regno
, hard_regno
, div
* 2);
679 /* Update REG_RENUMBER and other pseudo preferences by assignment of
680 HARD_REGNO to pseudo REGNO and print about it if PRINT_P. */
682 lra_setup_reg_renumber (int regno
, int hard_regno
, bool print_p
)
686 /* We can not just reassign hard register. */
687 lra_assert (hard_regno
< 0 || reg_renumber
[regno
] < 0);
688 if ((hr
= hard_regno
) < 0)
689 hr
= reg_renumber
[regno
];
690 reg_renumber
[regno
] = hard_regno
;
691 lra_assert (hr
>= 0);
692 for (i
= 0; i
< hard_regno_nregs
[hr
][PSEUDO_REGNO_MODE (regno
)]; i
++)
694 lra_hard_reg_usage
[hr
+ i
] -= lra_reg_info
[regno
].freq
;
696 lra_hard_reg_usage
[hr
+ i
] += lra_reg_info
[regno
].freq
;
697 if (print_p
&& lra_dump_file
!= NULL
)
698 fprintf (lra_dump_file
, " Assign %d to %sr%d (freq=%d)\n",
700 regno
< lra_constraint_new_regno_start
702 : bitmap_bit_p (&lra_inheritance_pseudos
, regno
) ? "inheritance "
703 : bitmap_bit_p (&lra_split_regs
, regno
) ? "split "
704 : bitmap_bit_p (&lra_optional_reload_pseudos
, regno
)
705 ? "optional reload ": "reload ",
706 regno
, lra_reg_info
[regno
].freq
);
709 curr_update_hard_regno_preference_check
++;
710 update_hard_regno_preference (regno
, hard_regno
, 1);
714 /* Pseudos which occur in insns containing a particular pseudo. */
715 static bitmap_head insn_conflict_pseudos
;
717 /* Bitmaps used to contain spill pseudos for given pseudo hard regno
718 and best spill pseudos for given pseudo (and best hard regno). */
719 static bitmap_head spill_pseudos_bitmap
, best_spill_pseudos_bitmap
;
721 /* Current pseudo check for validity of elements in
722 TRY_HARD_REG_PSEUDOS. */
723 static int curr_pseudo_check
;
724 /* Array used for validity of elements in TRY_HARD_REG_PSEUDOS. */
725 static int try_hard_reg_pseudos_check
[FIRST_PSEUDO_REGISTER
];
726 /* Pseudos who hold given hard register at the considered points. */
727 static bitmap_head try_hard_reg_pseudos
[FIRST_PSEUDO_REGISTER
];
729 /* Set up try_hard_reg_pseudos for given program point P and class
730 RCLASS. Those are pseudos living at P and assigned to a hard
731 register of RCLASS. In other words, those are pseudos which can be
732 spilled to assign a hard register of RCLASS to a pseudo living at
735 setup_try_hard_regno_pseudos (int p
, enum reg_class rclass
)
738 enum machine_mode mode
;
739 unsigned int spill_regno
;
742 /* Find what pseudos could be spilled. */
743 EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos
[p
], 0, spill_regno
, bi
)
745 mode
= PSEUDO_REGNO_MODE (spill_regno
);
746 hard_regno
= live_pseudos_reg_renumber
[spill_regno
];
747 if (overlaps_hard_reg_set_p (reg_class_contents
[rclass
],
750 for (i
= hard_regno_nregs
[hard_regno
][mode
] - 1; i
>= 0; i
--)
752 if (try_hard_reg_pseudos_check
[hard_regno
+ i
]
753 != curr_pseudo_check
)
755 try_hard_reg_pseudos_check
[hard_regno
+ i
]
757 bitmap_clear (&try_hard_reg_pseudos
[hard_regno
+ i
]);
759 bitmap_set_bit (&try_hard_reg_pseudos
[hard_regno
+ i
],
766 /* Assign temporarily HARD_REGNO to pseudo REGNO. Temporary
767 assignment means that we might undo the data change. */
769 assign_temporarily (int regno
, int hard_regno
)
774 for (r
= lra_reg_info
[regno
].live_ranges
; r
!= NULL
; r
= r
->next
)
776 for (p
= r
->start
; p
<= r
->finish
; p
++)
778 bitmap_clear_bit (&live_hard_reg_pseudos
[p
], regno
);
781 bitmap_set_bit (&live_hard_reg_pseudos
[p
], regno
);
782 insert_in_live_range_start_chain (regno
);
785 live_pseudos_reg_renumber
[regno
] = hard_regno
;
788 /* Array used for sorting reload pseudos for subsequent allocation
789 after spilling some pseudo. */
790 static int *sorted_reload_pseudos
;
792 /* Spill some pseudos for a reload pseudo REGNO and return hard
793 register which should be used for pseudo after spilling. The
794 function adds spilled pseudos to SPILLED_PSEUDO_BITMAP. When we
795 choose hard register (and pseudos occupying the hard registers and
796 to be spilled), we take into account not only how REGNO will
797 benefit from the spills but also how other reload pseudos not yet
798 assigned to hard registers benefit from the spills too. In very
799 rare cases, the function can fail and return -1. */
801 spill_for (int regno
, bitmap spilled_pseudo_bitmap
)
803 int i
, j
, n
, p
, hard_regno
, best_hard_regno
, cost
, best_cost
, rclass_size
;
804 int reload_hard_regno
, reload_cost
;
805 enum machine_mode mode
;
806 enum reg_class rclass
;
807 unsigned int spill_regno
, reload_regno
, uid
;
808 int insn_pseudos_num
, best_insn_pseudos_num
;
812 rclass
= regno_allocno_class_array
[regno
];
813 lra_assert (reg_renumber
[regno
] < 0 && rclass
!= NO_REGS
);
814 bitmap_clear (&insn_conflict_pseudos
);
815 bitmap_clear (&best_spill_pseudos_bitmap
);
816 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info
[regno
].insn_bitmap
, 0, uid
, bi
)
818 struct lra_insn_reg
*ir
;
820 for (ir
= lra_get_insn_regs (uid
); ir
!= NULL
; ir
= ir
->next
)
821 if (ir
->regno
>= FIRST_PSEUDO_REGISTER
)
822 bitmap_set_bit (&insn_conflict_pseudos
, ir
->regno
);
824 best_hard_regno
= -1;
826 best_insn_pseudos_num
= INT_MAX
;
827 rclass_size
= ira_class_hard_regs_num
[rclass
];
828 mode
= PSEUDO_REGNO_MODE (regno
);
829 /* Invalidate try_hard_reg_pseudos elements. */
831 for (r
= lra_reg_info
[regno
].live_ranges
; r
!= NULL
; r
= r
->next
)
832 for (p
= r
->start
; p
<= r
->finish
; p
++)
833 setup_try_hard_regno_pseudos (p
, rclass
);
834 for (i
= 0; i
< rclass_size
; i
++)
836 hard_regno
= ira_class_hard_regs
[rclass
][i
];
837 bitmap_clear (&spill_pseudos_bitmap
);
838 for (j
= hard_regno_nregs
[hard_regno
][mode
] - 1; j
>= 0; j
--)
840 if (try_hard_reg_pseudos_check
[hard_regno
+ j
] != curr_pseudo_check
)
842 lra_assert (!bitmap_empty_p (&try_hard_reg_pseudos
[hard_regno
+ j
]));
843 bitmap_ior_into (&spill_pseudos_bitmap
,
844 &try_hard_reg_pseudos
[hard_regno
+ j
]);
847 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap
, 0, spill_regno
, bi
)
848 if ((int) spill_regno
>= lra_constraint_new_regno_start
849 && ! bitmap_bit_p (&lra_inheritance_pseudos
, spill_regno
)
850 && ! bitmap_bit_p (&lra_split_regs
, spill_regno
)
851 && ! bitmap_bit_p (&lra_optional_reload_pseudos
, spill_regno
))
853 insn_pseudos_num
= 0;
854 if (lra_dump_file
!= NULL
)
855 fprintf (lra_dump_file
, " Trying %d:", hard_regno
);
856 sparseset_clear (live_range_reload_inheritance_pseudos
);
857 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap
, 0, spill_regno
, bi
)
859 if (bitmap_bit_p (&insn_conflict_pseudos
, spill_regno
))
861 for (r
= lra_reg_info
[spill_regno
].live_ranges
;
865 for (p
= r
->start
; p
<= r
->finish
; p
++)
869 for (r2
= start_point_ranges
[p
];
872 if (r2
->regno
>= lra_constraint_new_regno_start
)
873 sparseset_set_bit (live_range_reload_inheritance_pseudos
,
879 EXECUTE_IF_SET_IN_SPARSESET (live_range_reload_inheritance_pseudos
,
881 if ((int) reload_regno
!= regno
882 && (ira_reg_classes_intersect_p
883 [rclass
][regno_allocno_class_array
[reload_regno
]])
884 && live_pseudos_reg_renumber
[reload_regno
] < 0
885 && find_hard_regno_for (reload_regno
, &cost
, -1) < 0)
886 sorted_reload_pseudos
[n
++] = reload_regno
;
887 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap
, 0, spill_regno
, bi
)
889 update_lives (spill_regno
, true);
890 if (lra_dump_file
!= NULL
)
891 fprintf (lra_dump_file
, " spill %d(freq=%d)",
892 spill_regno
, lra_reg_info
[spill_regno
].freq
);
894 hard_regno
= find_hard_regno_for (regno
, &cost
, -1);
897 assign_temporarily (regno
, hard_regno
);
898 qsort (sorted_reload_pseudos
, n
, sizeof (int),
899 reload_pseudo_compare_func
);
900 for (j
= 0; j
< n
; j
++)
902 reload_regno
= sorted_reload_pseudos
[j
];
903 lra_assert (live_pseudos_reg_renumber
[reload_regno
] < 0);
904 if ((reload_hard_regno
905 = find_hard_regno_for (reload_regno
,
906 &reload_cost
, -1)) >= 0)
908 if (lra_dump_file
!= NULL
)
909 fprintf (lra_dump_file
, " assign %d(cost=%d)",
910 reload_regno
, reload_cost
);
911 assign_temporarily (reload_regno
, reload_hard_regno
);
915 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap
, 0, spill_regno
, bi
)
919 cost
+= lra_reg_info
[spill_regno
].freq
;
920 if (ira_reg_equiv
[spill_regno
].memory
!= NULL
921 || ira_reg_equiv
[spill_regno
].constant
!= NULL
)
922 for (x
= ira_reg_equiv
[spill_regno
].init_insns
;
925 cost
-= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (XEXP (x
, 0)));
927 if (best_insn_pseudos_num
> insn_pseudos_num
928 || (best_insn_pseudos_num
== insn_pseudos_num
929 && best_cost
> cost
))
931 best_insn_pseudos_num
= insn_pseudos_num
;
933 best_hard_regno
= hard_regno
;
934 bitmap_copy (&best_spill_pseudos_bitmap
, &spill_pseudos_bitmap
);
935 if (lra_dump_file
!= NULL
)
936 fprintf (lra_dump_file
, " Now best %d(cost=%d)\n",
939 assign_temporarily (regno
, -1);
940 for (j
= 0; j
< n
; j
++)
942 reload_regno
= sorted_reload_pseudos
[j
];
943 if (live_pseudos_reg_renumber
[reload_regno
] >= 0)
944 assign_temporarily (reload_regno
, -1);
947 if (lra_dump_file
!= NULL
)
948 fprintf (lra_dump_file
, "\n");
949 /* Restore the live hard reg pseudo info for spilled pseudos. */
950 EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap
, 0, spill_regno
, bi
)
951 update_lives (spill_regno
, false);
956 EXECUTE_IF_SET_IN_BITMAP (&best_spill_pseudos_bitmap
, 0, spill_regno
, bi
)
958 if (lra_dump_file
!= NULL
)
959 fprintf (lra_dump_file
, " Spill %sr%d(hr=%d, freq=%d) for r%d\n",
960 ((int) spill_regno
< lra_constraint_new_regno_start
962 : bitmap_bit_p (&lra_inheritance_pseudos
, spill_regno
)
964 : bitmap_bit_p (&lra_split_regs
, spill_regno
)
966 : bitmap_bit_p (&lra_optional_reload_pseudos
, spill_regno
)
967 ? "optional reload " : "reload "),
968 spill_regno
, reg_renumber
[spill_regno
],
969 lra_reg_info
[spill_regno
].freq
, regno
);
970 update_lives (spill_regno
, true);
971 lra_setup_reg_renumber (spill_regno
, -1, false);
973 bitmap_ior_into (spilled_pseudo_bitmap
, &best_spill_pseudos_bitmap
);
974 return best_hard_regno
;
977 /* Assign HARD_REGNO to REGNO. */
979 assign_hard_regno (int hard_regno
, int regno
)
983 lra_assert (hard_regno
>= 0);
984 lra_setup_reg_renumber (regno
, hard_regno
, true);
985 update_lives (regno
, false);
987 i
< hard_regno_nregs
[hard_regno
][lra_reg_info
[regno
].biggest_mode
];
989 df_set_regs_ever_live (hard_regno
+ i
, true);
992 /* Array used for sorting different pseudos. */
993 static int *sorted_pseudos
;
995 /* The constraints pass is allowed to create equivalences between
996 pseudos that make the current allocation "incorrect" (in the sense
997 that pseudos are assigned to hard registers from their own conflict
998 sets). The global variable lra_risky_transformations_p says
999 whether this might have happened.
1001 Process pseudos assigned to hard registers (less frequently used
1002 first), spill if a conflict is found, and mark the spilled pseudos
1003 in SPILLED_PSEUDO_BITMAP. Set up LIVE_HARD_REG_PSEUDOS from
1004 pseudos, assigned to hard registers. */
1006 setup_live_pseudos_and_spill_after_risky_transforms (bitmap
1007 spilled_pseudo_bitmap
)
1009 int p
, i
, j
, n
, regno
, hard_regno
;
1010 unsigned int k
, conflict_regno
;
1012 HARD_REG_SET conflict_set
;
1013 enum machine_mode mode
;
1016 int max_regno
= max_reg_num ();
1018 if (! lra_risky_transformations_p
)
1020 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
1021 if (reg_renumber
[i
] >= 0 && lra_reg_info
[i
].nrefs
> 0)
1022 update_lives (i
, false);
1025 for (n
= 0, i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
1026 if (reg_renumber
[i
] >= 0 && lra_reg_info
[i
].nrefs
> 0)
1027 sorted_pseudos
[n
++] = i
;
1028 qsort (sorted_pseudos
, n
, sizeof (int), pseudo_compare_func
);
1029 for (i
= n
- 1; i
>= 0; i
--)
1031 regno
= sorted_pseudos
[i
];
1032 hard_regno
= reg_renumber
[regno
];
1033 lra_assert (hard_regno
>= 0);
1034 mode
= lra_reg_info
[regno
].biggest_mode
;
1035 sparseset_clear (live_range_hard_reg_pseudos
);
1036 for (r
= lra_reg_info
[regno
].live_ranges
; r
!= NULL
; r
= r
->next
)
1038 EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos
[r
->start
], 0, k
, bi
)
1039 sparseset_set_bit (live_range_hard_reg_pseudos
, k
);
1040 for (p
= r
->start
+ 1; p
<= r
->finish
; p
++)
1042 lra_live_range_t r2
;
1044 for (r2
= start_point_ranges
[p
];
1046 r2
= r2
->start_next
)
1047 if (live_pseudos_reg_renumber
[r2
->regno
] >= 0)
1048 sparseset_set_bit (live_range_hard_reg_pseudos
, r2
->regno
);
1051 COPY_HARD_REG_SET (conflict_set
, lra_no_alloc_regs
);
1052 IOR_HARD_REG_SET (conflict_set
, lra_reg_info
[regno
].conflict_hard_regs
);
1053 val
= lra_reg_info
[regno
].val
;
1054 offset
= lra_reg_info
[regno
].offset
;
1055 EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos
, conflict_regno
)
1056 if (!lra_reg_val_equal_p (conflict_regno
, val
, offset
)
1057 /* If it is multi-register pseudos they should start on
1058 the same hard register. */
1059 || hard_regno
!= reg_renumber
[conflict_regno
])
1060 add_to_hard_reg_set (&conflict_set
,
1061 lra_reg_info
[conflict_regno
].biggest_mode
,
1062 reg_renumber
[conflict_regno
]);
1063 if (! overlaps_hard_reg_set_p (conflict_set
, mode
, hard_regno
))
1065 update_lives (regno
, false);
1068 bitmap_set_bit (spilled_pseudo_bitmap
, regno
);
1070 j
< hard_regno_nregs
[hard_regno
][PSEUDO_REGNO_MODE (regno
)];
1072 lra_hard_reg_usage
[hard_regno
+ j
] -= lra_reg_info
[regno
].freq
;
1073 reg_renumber
[regno
] = -1;
1074 if (lra_dump_file
!= NULL
)
1075 fprintf (lra_dump_file
, " Spill r%d after risky transformations\n",
1080 /* Improve allocation by assigning the same hard regno of inheritance
1081 pseudos to the connected pseudos. We need this because inheritance
1082 pseudos are allocated after reload pseudos in the thread and when
1083 we assign a hard register to a reload pseudo we don't know yet that
1084 the connected inheritance pseudos can get the same hard register.
1085 Add pseudos with changed allocation to bitmap CHANGED_PSEUDOS. */
1087 improve_inheritance (bitmap changed_pseudos
)
1090 int regno
, another_regno
, hard_regno
, another_hard_regno
, cost
, i
, n
;
1091 lra_copy_t cp
, next_cp
;
1094 if (lra_inheritance_iter
> LRA_MAX_INHERITANCE_PASSES
)
1097 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos
, 0, k
, bi
)
1098 if (reg_renumber
[k
] >= 0 && lra_reg_info
[k
].nrefs
!= 0)
1099 sorted_pseudos
[n
++] = k
;
1100 qsort (sorted_pseudos
, n
, sizeof (int), pseudo_compare_func
);
1101 for (i
= 0; i
< n
; i
++)
1103 regno
= sorted_pseudos
[i
];
1104 hard_regno
= reg_renumber
[regno
];
1105 lra_assert (hard_regno
>= 0);
1106 for (cp
= lra_reg_info
[regno
].copies
; cp
!= NULL
; cp
= next_cp
)
1108 if (cp
->regno1
== regno
)
1110 next_cp
= cp
->regno1_next
;
1111 another_regno
= cp
->regno2
;
1113 else if (cp
->regno2
== regno
)
1115 next_cp
= cp
->regno2_next
;
1116 another_regno
= cp
->regno1
;
1120 /* Don't change reload pseudo allocation. It might have
1121 this allocation for a purpose and changing it can result
1123 if ((another_regno
< lra_constraint_new_regno_start
1124 || bitmap_bit_p (&lra_inheritance_pseudos
, another_regno
))
1125 && (another_hard_regno
= reg_renumber
[another_regno
]) >= 0
1126 && another_hard_regno
!= hard_regno
)
1128 if (lra_dump_file
!= NULL
)
1131 " Improving inheritance for %d(%d) and %d(%d)...\n",
1132 regno
, hard_regno
, another_regno
, another_hard_regno
);
1133 update_lives (another_regno
, true);
1134 lra_setup_reg_renumber (another_regno
, -1, false);
1136 == find_hard_regno_for (another_regno
, &cost
, hard_regno
))
1137 assign_hard_regno (hard_regno
, another_regno
);
1139 assign_hard_regno (another_hard_regno
, another_regno
);
1140 bitmap_set_bit (changed_pseudos
, another_regno
);
1147 /* Bitmap finally containing all pseudos spilled on this assignment
1149 static bitmap_head all_spilled_pseudos
;
1150 /* All pseudos whose allocation was changed. */
1151 static bitmap_head changed_pseudo_bitmap
;
1153 /* Assign hard registers to reload pseudos and other pseudos. */
1155 assign_by_spills (void)
1157 int i
, n
, nfails
, iter
, regno
, hard_regno
, cost
, restore_regno
;
1160 bitmap_head changed_insns
, do_not_assign_nonreload_pseudos
;
1161 bitmap_head non_reload_pseudos
;
1165 int max_regno
= max_reg_num ();
1167 for (n
= 0, i
= lra_constraint_new_regno_start
; i
< max_regno
; i
++)
1168 if (reg_renumber
[i
] < 0 && lra_reg_info
[i
].nrefs
!= 0
1169 && regno_allocno_class_array
[i
] != NO_REGS
)
1170 sorted_pseudos
[n
++] = i
;
1171 bitmap_initialize (&insn_conflict_pseudos
, ®_obstack
);
1172 bitmap_initialize (&spill_pseudos_bitmap
, ®_obstack
);
1173 bitmap_initialize (&best_spill_pseudos_bitmap
, ®_obstack
);
1174 update_hard_regno_preference_check
= XCNEWVEC (int, max_regno
);
1175 curr_update_hard_regno_preference_check
= 0;
1176 memset (try_hard_reg_pseudos_check
, 0, sizeof (try_hard_reg_pseudos_check
));
1177 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1178 bitmap_initialize (&try_hard_reg_pseudos
[i
], ®_obstack
);
1179 curr_pseudo_check
= 0;
1180 bitmap_initialize (&changed_insns
, ®_obstack
);
1181 bitmap_initialize (&non_reload_pseudos
, ®_obstack
);
1182 bitmap_ior (&non_reload_pseudos
, &lra_inheritance_pseudos
, &lra_split_regs
);
1183 bitmap_ior_into (&non_reload_pseudos
, &lra_optional_reload_pseudos
);
1184 for (iter
= 0; iter
<= 1; iter
++)
1186 qsort (sorted_pseudos
, n
, sizeof (int), reload_pseudo_compare_func
);
1188 for (i
= 0; i
< n
; i
++)
1190 regno
= sorted_pseudos
[i
];
1191 if (lra_dump_file
!= NULL
)
1192 fprintf (lra_dump_file
, " Assigning to %d "
1193 "(cl=%s, orig=%d, freq=%d, tfirst=%d, tfreq=%d)...\n",
1194 regno
, reg_class_names
[regno_allocno_class_array
[regno
]],
1195 ORIGINAL_REGNO (regno_reg_rtx
[regno
]),
1196 lra_reg_info
[regno
].freq
, regno_assign_info
[regno
].first
,
1197 regno_assign_info
[regno_assign_info
[regno
].first
].freq
);
1198 hard_regno
= find_hard_regno_for (regno
, &cost
, -1);
1199 reload_p
= ! bitmap_bit_p (&non_reload_pseudos
, regno
);
1200 if (hard_regno
< 0 && reload_p
)
1201 hard_regno
= spill_for (regno
, &all_spilled_pseudos
);
1205 sorted_pseudos
[nfails
++] = regno
;
1209 /* This register might have been spilled by the previous
1210 pass. Indicate that it is no longer spilled. */
1211 bitmap_clear_bit (&all_spilled_pseudos
, regno
);
1212 assign_hard_regno (hard_regno
, regno
);
1214 /* As non-reload pseudo assignment is changed we
1215 should reconsider insns referring for the
1217 bitmap_set_bit (&changed_pseudo_bitmap
, regno
);
1224 /* We did not assign hard regs to reload pseudos after two
1225 iteration. It means something is wrong with asm insn
1226 constraints. Report it. */
1228 bitmap_head failed_reload_insns
;
1230 bitmap_initialize (&failed_reload_insns
, ®_obstack
);
1231 for (i
= 0; i
< nfails
; i
++)
1233 regno
= sorted_pseudos
[i
];
1234 bitmap_ior_into (&failed_reload_insns
,
1235 &lra_reg_info
[regno
].insn_bitmap
);
1236 /* Assign an arbitrary hard register of regno class to
1237 avoid further trouble with the asm insns. */
1238 bitmap_clear_bit (&all_spilled_pseudos
, regno
);
1240 (ira_class_hard_regs
[regno_allocno_class_array
[regno
]][0],
1243 EXECUTE_IF_SET_IN_BITMAP (&failed_reload_insns
, 0, u
, bi
)
1245 insn
= lra_insn_recog_data
[u
]->insn
;
1246 if (asm_noperands (PATTERN (insn
)) >= 0)
1249 error_for_asm (insn
,
1250 "%<asm%> operand has impossible constraints");
1251 /* Avoid further trouble with this insn.
1252 For asm goto, instead of fixing up all the edges
1253 just clear the template and clear input operands
1254 (asm goto doesn't have any output operands). */
1257 rtx asm_op
= extract_asm_operands (PATTERN (insn
));
1258 ASM_OPERANDS_TEMPLATE (asm_op
) = ggc_strdup ("");
1259 ASM_OPERANDS_INPUT_VEC (asm_op
) = rtvec_alloc (0);
1260 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op
) = rtvec_alloc (0);
1261 lra_update_insn_regno_info (insn
);
1265 PATTERN (insn
) = gen_rtx_USE (VOIDmode
, const0_rtx
);
1266 lra_set_insn_deleted (insn
);
1273 /* This is a very rare event. We can not assign a hard
1274 register to reload pseudo because the hard register was
1275 assigned to another reload pseudo on a previous
1276 assignment pass. For x86 example, on the 1st pass we
1277 assigned CX (although another hard register could be used
1278 for this) to reload pseudo in an insn, on the 2nd pass we
1279 need CX (and only this) hard register for a new reload
1280 pseudo in the same insn. */
1281 if (lra_dump_file
!= NULL
)
1282 fprintf (lra_dump_file
, " 2nd iter for reload pseudo assignments:\n");
1283 for (i
= 0; i
< nfails
; i
++)
1285 if (lra_dump_file
!= NULL
)
1286 fprintf (lra_dump_file
, " Reload r%d assignment failure\n",
1288 bitmap_ior_into (&changed_insns
,
1289 &lra_reg_info
[sorted_pseudos
[i
]].insn_bitmap
);
1292 /* FIXME: Look up the changed insns in the cached LRA insn data using
1293 an EXECUTE_IF_SET_IN_BITMAP over changed_insns. */
1295 FOR_BB_INSNS (bb
, insn
)
1296 if (bitmap_bit_p (&changed_insns
, INSN_UID (insn
)))
1298 lra_insn_recog_data_t data
;
1299 struct lra_insn_reg
*r
;
1301 data
= lra_get_insn_recog_data (insn
);
1302 for (r
= data
->regs
; r
!= NULL
; r
= r
->next
)
1305 /* A reload pseudo did not get a hard register on the
1306 first iteration because of the conflict with
1307 another reload pseudos in the same insn. So we
1308 consider only reload pseudos assigned to hard
1309 registers. We shall exclude inheritance pseudos as
1310 they can occur in original insns (not reload ones).
1311 We can omit the check for split pseudos because
1312 they occur only in move insns containing non-reload
1314 if (regno
< lra_constraint_new_regno_start
1315 || bitmap_bit_p (&lra_inheritance_pseudos
, regno
)
1316 || reg_renumber
[regno
] < 0)
1318 sorted_pseudos
[nfails
++] = regno
;
1319 if (lra_dump_file
!= NULL
)
1320 fprintf (lra_dump_file
,
1321 " Spill reload r%d(hr=%d, freq=%d)\n",
1322 regno
, reg_renumber
[regno
],
1323 lra_reg_info
[regno
].freq
);
1324 update_lives (regno
, true);
1325 lra_setup_reg_renumber (regno
, -1, false);
1330 improve_inheritance (&changed_pseudo_bitmap
);
1331 bitmap_clear (&non_reload_pseudos
);
1332 bitmap_clear (&changed_insns
);
1335 /* We should not assign to original pseudos of inheritance
1336 pseudos or split pseudos if any its inheritance pseudo did
1337 not get hard register or any its split pseudo was not split
1338 because undo inheritance/split pass will extend live range of
1339 such inheritance or split pseudos. */
1340 bitmap_initialize (&do_not_assign_nonreload_pseudos
, ®_obstack
);
1341 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos
, 0, u
, bi
)
1342 if ((restore_regno
= lra_reg_info
[u
].restore_regno
) >= 0
1343 && reg_renumber
[u
] < 0
1344 && bitmap_bit_p (&lra_inheritance_pseudos
, u
))
1345 bitmap_set_bit (&do_not_assign_nonreload_pseudos
, restore_regno
);
1346 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs
, 0, u
, bi
)
1347 if ((restore_regno
= lra_reg_info
[u
].restore_regno
) >= 0
1348 && reg_renumber
[u
] >= 0)
1349 bitmap_set_bit (&do_not_assign_nonreload_pseudos
, restore_regno
);
1350 for (n
= 0, i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
1351 if (((i
< lra_constraint_new_regno_start
1352 && ! bitmap_bit_p (&do_not_assign_nonreload_pseudos
, i
))
1353 || (bitmap_bit_p (&lra_inheritance_pseudos
, i
)
1354 && lra_reg_info
[i
].restore_regno
>= 0)
1355 || (bitmap_bit_p (&lra_split_regs
, i
)
1356 && lra_reg_info
[i
].restore_regno
>= 0)
1357 || bitmap_bit_p (&lra_optional_reload_pseudos
, i
))
1358 && reg_renumber
[i
] < 0 && lra_reg_info
[i
].nrefs
!= 0
1359 && regno_allocno_class_array
[i
] != NO_REGS
)
1360 sorted_pseudos
[n
++] = i
;
1361 bitmap_clear (&do_not_assign_nonreload_pseudos
);
1362 if (n
!= 0 && lra_dump_file
!= NULL
)
1363 fprintf (lra_dump_file
, " Reassigning non-reload pseudos\n");
1364 qsort (sorted_pseudos
, n
, sizeof (int), pseudo_compare_func
);
1365 for (i
= 0; i
< n
; i
++)
1367 regno
= sorted_pseudos
[i
];
1368 hard_regno
= find_hard_regno_for (regno
, &cost
, -1);
1369 if (hard_regno
>= 0)
1371 assign_hard_regno (hard_regno
, regno
);
1372 /* We change allocation for non-reload pseudo on this
1373 iteration -- mark the pseudo for invalidation of used
1374 alternatives of insns containing the pseudo. */
1375 bitmap_set_bit (&changed_pseudo_bitmap
, regno
);
1379 free (update_hard_regno_preference_check
);
1380 bitmap_clear (&best_spill_pseudos_bitmap
);
1381 bitmap_clear (&spill_pseudos_bitmap
);
1382 bitmap_clear (&insn_conflict_pseudos
);
1386 /* Entry function to assign hard registers to new reload pseudos
1387 starting with LRA_CONSTRAINT_NEW_REGNO_START (by possible spilling
1388 of old pseudos) and possibly to the old pseudos. The function adds
1389 what insns to process for the next constraint pass. Those are all
1390 insns who contains non-reload and non-inheritance pseudos with
1393 Return true if we did not spill any non-reload and non-inheritance
1401 bitmap_head insns_to_process
;
1403 int max_regno
= max_reg_num ();
1405 timevar_push (TV_LRA_ASSIGN
);
1407 sorted_pseudos
= XNEWVEC (int, max_regno
);
1408 sorted_reload_pseudos
= XNEWVEC (int, max_regno
);
1409 regno_allocno_class_array
= XNEWVEC (enum reg_class
, max_regno
);
1410 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
1411 regno_allocno_class_array
[i
] = lra_get_allocno_class (i
);
1412 init_regno_assign_info ();
1413 bitmap_initialize (&all_spilled_pseudos
, ®_obstack
);
1414 create_live_range_start_chains ();
1415 setup_live_pseudos_and_spill_after_risky_transforms (&all_spilled_pseudos
);
1416 #ifdef ENABLE_CHECKING
1417 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
1418 if (lra_reg_info
[i
].nrefs
!= 0 && reg_renumber
[i
] >= 0
1419 && lra_reg_info
[i
].call_p
1420 && overlaps_hard_reg_set_p (call_used_reg_set
,
1421 PSEUDO_REGNO_MODE (i
), reg_renumber
[i
]))
1424 /* Setup insns to process on the next constraint pass. */
1425 bitmap_initialize (&changed_pseudo_bitmap
, ®_obstack
);
1426 init_live_reload_and_inheritance_pseudos ();
1427 assign_by_spills ();
1428 finish_live_reload_and_inheritance_pseudos ();
1429 bitmap_ior_into (&changed_pseudo_bitmap
, &all_spilled_pseudos
);
1431 EXECUTE_IF_SET_IN_BITMAP (&all_spilled_pseudos
, 0, u
, bi
)
1432 /* We ignore spilled pseudos created on last inheritance pass
1433 because they will be removed. */
1434 if (lra_reg_info
[u
].restore_regno
< 0)
1436 no_spills_p
= false;
1439 finish_live_range_start_chains ();
1440 bitmap_clear (&all_spilled_pseudos
);
1441 bitmap_initialize (&insns_to_process
, ®_obstack
);
1442 EXECUTE_IF_SET_IN_BITMAP (&changed_pseudo_bitmap
, 0, u
, bi
)
1443 bitmap_ior_into (&insns_to_process
, &lra_reg_info
[u
].insn_bitmap
);
1444 bitmap_clear (&changed_pseudo_bitmap
);
1445 EXECUTE_IF_SET_IN_BITMAP (&insns_to_process
, 0, u
, bi
)
1447 lra_push_insn_by_uid (u
);
1448 /* Invalidate alternatives for insn should be processed. */
1449 lra_set_used_insn_alternative_by_uid (u
, -1);
1451 bitmap_clear (&insns_to_process
);
1452 finish_regno_assign_info ();
1453 free (regno_allocno_class_array
);
1454 free (sorted_pseudos
);
1455 free (sorted_reload_pseudos
);
1457 timevar_pop (TV_LRA_ASSIGN
);