]>
Commit | Line | Data |
---|---|---|
55a2c322 | 1 | /* Build live ranges for pseudos. |
5624e564 | 2 | Copyright (C) 2010-2015 Free Software Foundation, Inc. |
55a2c322 VM |
3 | Contributed by Vladimir Makarov <vmakarov@redhat.com>. |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it under | |
8 | the terms of the GNU General Public License as published by the Free | |
9 | Software Foundation; either version 3, or (at your option) any later | |
10 | version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GCC; see the file COPYING3. If not see | |
19 | <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | ||
22 | /* This file contains code to build pseudo live-ranges (analogous | |
23 | structures used in IRA, so read comments about the live-ranges | |
24 | there) and other info necessary for other passes to assign | |
25 | hard-registers to pseudos, coalesce the spilled pseudos, and assign | |
26 | stack memory slots to spilled pseudos. */ | |
27 | ||
28 | #include "config.h" | |
29 | #include "system.h" | |
30 | #include "coretypes.h" | |
31 | #include "tm.h" | |
32 | #include "hard-reg-set.h" | |
33 | #include "rtl.h" | |
34 | #include "tm_p.h" | |
35 | #include "insn-config.h" | |
36 | #include "recog.h" | |
37 | #include "output.h" | |
38 | #include "regs.h" | |
83685514 AM |
39 | #include "hashtab.h" |
40 | #include "hash-set.h" | |
41 | #include "vec.h" | |
42 | #include "machmode.h" | |
43 | #include "input.h" | |
55a2c322 | 44 | #include "function.h" |
40e23961 | 45 | #include "symtab.h" |
55a2c322 | 46 | #include "expr.h" |
60393bbc AM |
47 | #include "predict.h" |
48 | #include "dominance.h" | |
49 | #include "cfg.h" | |
50 | #include "cfganal.h" | |
55a2c322 VM |
51 | #include "basic-block.h" |
52 | #include "except.h" | |
53 | #include "df.h" | |
54 | #include "ira.h" | |
55 | #include "sparseset.h" | |
56 | #include "lra-int.h" | |
57 | ||
58 | /* Program points are enumerated by numbers from range | |
59 | 0..LRA_LIVE_MAX_POINT-1. There are approximately two times more | |
60 | program points than insns. Program points are places in the | |
61 | program where liveness info can be changed. In most general case | |
62 | (there are more complicated cases too) some program points | |
63 | correspond to places where input operand dies and other ones | |
64 | correspond to places where output operands are born. */ | |
65 | int lra_live_max_point; | |
66 | ||
67 | /* Accumulated execution frequency of all references for each hard | |
68 | register. */ | |
69 | int lra_hard_reg_usage[FIRST_PSEUDO_REGISTER]; | |
70 | ||
71 | /* A global flag whose true value says to build live ranges for all | |
72 | pseudos, otherwise the live ranges only for pseudos got memory is | |
73 | build. True value means also building copies and setting up hard | |
74 | register preferences. The complete info is necessary only for the | |
75 | assignment pass. The complete info is not needed for the | |
76 | coalescing and spill passes. */ | |
77 | static bool complete_info_p; | |
78 | ||
79 | /* Pseudos live at current point in the RTL scan. */ | |
80 | static sparseset pseudos_live; | |
81 | ||
82 | /* Pseudos probably living through calls and setjumps. As setjump is | |
83 | a call too, if a bit in PSEUDOS_LIVE_THROUGH_SETJUMPS is set up | |
84 | then the corresponding bit in PSEUDOS_LIVE_THROUGH_CALLS is set up | |
85 | too. These data are necessary for cases when only one subreg of a | |
86 | multi-reg pseudo is set up after a call. So we decide it is | |
87 | probably live when traversing bb backward. We are sure about | |
88 | living when we see its usage or definition of the pseudo. */ | |
89 | static sparseset pseudos_live_through_calls; | |
90 | static sparseset pseudos_live_through_setjumps; | |
91 | ||
92 | /* Set of hard regs (except eliminable ones) currently live. */ | |
93 | static HARD_REG_SET hard_regs_live; | |
94 | ||
95 | /* Set of pseudos and hard registers start living/dying in the current | |
96 | insn. These sets are used to update REG_DEAD and REG_UNUSED notes | |
97 | in the insn. */ | |
98 | static sparseset start_living, start_dying; | |
99 | ||
100 | /* Set of pseudos and hard regs dead and unused in the current | |
101 | insn. */ | |
102 | static sparseset unused_set, dead_set; | |
103 | ||
4ab74a01 VM |
104 | /* Bitmap used for holding intermediate bitmap operation results. */ |
105 | static bitmap_head temp_bitmap; | |
106 | ||
55a2c322 VM |
107 | /* Pool for pseudo live ranges. */ |
108 | static alloc_pool live_range_pool; | |
109 | ||
110 | /* Free live range LR. */ | |
111 | static void | |
112 | free_live_range (lra_live_range_t lr) | |
113 | { | |
114 | pool_free (live_range_pool, lr); | |
115 | } | |
116 | ||
117 | /* Free live range list LR. */ | |
118 | static void | |
119 | free_live_range_list (lra_live_range_t lr) | |
120 | { | |
121 | lra_live_range_t next; | |
122 | ||
123 | while (lr != NULL) | |
124 | { | |
125 | next = lr->next; | |
126 | free_live_range (lr); | |
127 | lr = next; | |
128 | } | |
129 | } | |
130 | ||
131 | /* Create and return pseudo live range with given attributes. */ | |
132 | static lra_live_range_t | |
133 | create_live_range (int regno, int start, int finish, lra_live_range_t next) | |
134 | { | |
135 | lra_live_range_t p; | |
136 | ||
137 | p = (lra_live_range_t) pool_alloc (live_range_pool); | |
138 | p->regno = regno; | |
139 | p->start = start; | |
140 | p->finish = finish; | |
141 | p->next = next; | |
142 | return p; | |
143 | } | |
144 | ||
145 | /* Copy live range R and return the result. */ | |
146 | static lra_live_range_t | |
147 | copy_live_range (lra_live_range_t r) | |
148 | { | |
149 | lra_live_range_t p; | |
150 | ||
151 | p = (lra_live_range_t) pool_alloc (live_range_pool); | |
152 | *p = *r; | |
153 | return p; | |
154 | } | |
155 | ||
156 | /* Copy live range list given by its head R and return the result. */ | |
157 | lra_live_range_t | |
158 | lra_copy_live_range_list (lra_live_range_t r) | |
159 | { | |
160 | lra_live_range_t p, first, *chain; | |
161 | ||
162 | first = NULL; | |
163 | for (chain = &first; r != NULL; r = r->next) | |
164 | { | |
165 | p = copy_live_range (r); | |
166 | *chain = p; | |
167 | chain = &p->next; | |
168 | } | |
169 | return first; | |
170 | } | |
171 | ||
172 | /* Merge *non-intersected* ranges R1 and R2 and returns the result. | |
173 | The function maintains the order of ranges and tries to minimize | |
174 | size of the result range list. Ranges R1 and R2 may not be used | |
175 | after the call. */ | |
176 | lra_live_range_t | |
177 | lra_merge_live_ranges (lra_live_range_t r1, lra_live_range_t r2) | |
178 | { | |
179 | lra_live_range_t first, last, temp; | |
180 | ||
181 | if (r1 == NULL) | |
182 | return r2; | |
183 | if (r2 == NULL) | |
184 | return r1; | |
185 | for (first = last = NULL; r1 != NULL && r2 != NULL;) | |
186 | { | |
187 | if (r1->start < r2->start) | |
188 | { | |
189 | temp = r1; | |
190 | r1 = r2; | |
191 | r2 = temp; | |
192 | } | |
193 | if (r1->start == r2->finish + 1) | |
194 | { | |
195 | /* Joint ranges: merge r1 and r2 into r1. */ | |
196 | r1->start = r2->start; | |
197 | temp = r2; | |
198 | r2 = r2->next; | |
199 | pool_free (live_range_pool, temp); | |
200 | } | |
201 | else | |
202 | { | |
203 | gcc_assert (r2->finish + 1 < r1->start); | |
204 | /* Add r1 to the result. */ | |
205 | if (first == NULL) | |
206 | first = last = r1; | |
207 | else | |
208 | { | |
209 | last->next = r1; | |
210 | last = r1; | |
211 | } | |
212 | r1 = r1->next; | |
213 | } | |
214 | } | |
215 | if (r1 != NULL) | |
216 | { | |
217 | if (first == NULL) | |
218 | first = r1; | |
219 | else | |
220 | last->next = r1; | |
221 | } | |
222 | else | |
223 | { | |
224 | lra_assert (r2 != NULL); | |
225 | if (first == NULL) | |
226 | first = r2; | |
227 | else | |
228 | last->next = r2; | |
229 | } | |
230 | return first; | |
231 | } | |
232 | ||
233 | /* Return TRUE if live ranges R1 and R2 intersect. */ | |
234 | bool | |
235 | lra_intersected_live_ranges_p (lra_live_range_t r1, lra_live_range_t r2) | |
236 | { | |
237 | /* Remember the live ranges are always kept ordered. */ | |
238 | while (r1 != NULL && r2 != NULL) | |
239 | { | |
240 | if (r1->start > r2->finish) | |
241 | r1 = r1->next; | |
242 | else if (r2->start > r1->finish) | |
243 | r2 = r2->next; | |
244 | else | |
245 | return true; | |
246 | } | |
247 | return false; | |
248 | } | |
249 | ||
250 | /* The function processing birth of hard register REGNO. It updates | |
251 | living hard regs, conflict hard regs for living pseudos, and | |
252 | START_LIVING. */ | |
253 | static void | |
254 | make_hard_regno_born (int regno) | |
255 | { | |
256 | unsigned int i; | |
257 | ||
258 | lra_assert (regno < FIRST_PSEUDO_REGISTER); | |
d9cf932c | 259 | if (TEST_HARD_REG_BIT (hard_regs_live, regno)) |
55a2c322 VM |
260 | return; |
261 | SET_HARD_REG_BIT (hard_regs_live, regno); | |
262 | sparseset_set_bit (start_living, regno); | |
263 | EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, i) | |
264 | SET_HARD_REG_BIT (lra_reg_info[i].conflict_hard_regs, regno); | |
265 | } | |
266 | ||
267 | /* Process the death of hard register REGNO. This updates | |
268 | hard_regs_live and START_DYING. */ | |
269 | static void | |
270 | make_hard_regno_dead (int regno) | |
271 | { | |
272 | lra_assert (regno < FIRST_PSEUDO_REGISTER); | |
d9cf932c | 273 | if (! TEST_HARD_REG_BIT (hard_regs_live, regno)) |
55a2c322 VM |
274 | return; |
275 | sparseset_set_bit (start_dying, regno); | |
276 | CLEAR_HARD_REG_BIT (hard_regs_live, regno); | |
277 | } | |
278 | ||
279 | /* Mark pseudo REGNO as living at program point POINT, update conflicting | |
280 | hard registers of the pseudo and START_LIVING, and start a new live | |
281 | range for the pseudo corresponding to REGNO if it is necessary. */ | |
282 | static void | |
283 | mark_pseudo_live (int regno, int point) | |
284 | { | |
285 | lra_live_range_t p; | |
286 | ||
287 | lra_assert (regno >= FIRST_PSEUDO_REGISTER); | |
288 | lra_assert (! sparseset_bit_p (pseudos_live, regno)); | |
289 | sparseset_set_bit (pseudos_live, regno); | |
290 | IOR_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs, hard_regs_live); | |
291 | ||
292 | if ((complete_info_p || lra_get_regno_hard_regno (regno) < 0) | |
293 | && ((p = lra_reg_info[regno].live_ranges) == NULL | |
294 | || (p->finish != point && p->finish + 1 != point))) | |
295 | lra_reg_info[regno].live_ranges | |
296 | = create_live_range (regno, point, -1, p); | |
297 | sparseset_set_bit (start_living, regno); | |
298 | } | |
299 | ||
300 | /* Mark pseudo REGNO as not living at program point POINT and update | |
301 | START_DYING. | |
302 | This finishes the current live range for the pseudo corresponding | |
303 | to REGNO. */ | |
304 | static void | |
305 | mark_pseudo_dead (int regno, int point) | |
306 | { | |
307 | lra_live_range_t p; | |
308 | ||
309 | lra_assert (regno >= FIRST_PSEUDO_REGISTER); | |
310 | lra_assert (sparseset_bit_p (pseudos_live, regno)); | |
311 | sparseset_clear_bit (pseudos_live, regno); | |
312 | sparseset_set_bit (start_dying, regno); | |
313 | if (complete_info_p || lra_get_regno_hard_regno (regno) < 0) | |
314 | { | |
315 | p = lra_reg_info[regno].live_ranges; | |
316 | lra_assert (p != NULL); | |
317 | p->finish = point; | |
318 | } | |
319 | } | |
320 | ||
8160cd3e VM |
321 | /* The corresponding bitmaps of BB currently being processed. */ |
322 | static bitmap bb_killed_pseudos, bb_gen_pseudos; | |
323 | ||
324 | /* Mark register REGNO (pseudo or hard register) in MODE as live at | |
18ea3d61 | 325 | program point POINT. Update BB_GEN_PSEUDOS. |
8160cd3e VM |
326 | Return TRUE if the liveness tracking sets were modified, or FALSE |
327 | if nothing changed. */ | |
55a2c322 | 328 | static bool |
18ea3d61 | 329 | mark_regno_live (int regno, machine_mode mode, int point) |
55a2c322 VM |
330 | { |
331 | int last; | |
332 | bool changed = false; | |
333 | ||
334 | if (regno < FIRST_PSEUDO_REGISTER) | |
335 | { | |
336 | for (last = regno + hard_regno_nregs[regno][mode]; | |
337 | regno < last; | |
338 | regno++) | |
339 | make_hard_regno_born (regno); | |
340 | } | |
8160cd3e | 341 | else |
55a2c322 | 342 | { |
8160cd3e VM |
343 | if (! sparseset_bit_p (pseudos_live, regno)) |
344 | { | |
345 | mark_pseudo_live (regno, point); | |
346 | changed = true; | |
347 | } | |
18ea3d61 | 348 | bitmap_set_bit (bb_gen_pseudos, regno); |
55a2c322 VM |
349 | } |
350 | return changed; | |
351 | } | |
352 | ||
353 | ||
8160cd3e | 354 | /* Mark register REGNO in MODE as dead at program point POINT. Update |
18ea3d61 VM |
355 | BB_GEN_PSEUDOS and BB_KILLED_PSEUDOS. Return TRUE if the liveness |
356 | tracking sets were modified, or FALSE if nothing changed. */ | |
55a2c322 | 357 | static bool |
18ea3d61 | 358 | mark_regno_dead (int regno, machine_mode mode, int point) |
55a2c322 VM |
359 | { |
360 | int last; | |
361 | bool changed = false; | |
362 | ||
363 | if (regno < FIRST_PSEUDO_REGISTER) | |
364 | { | |
365 | for (last = regno + hard_regno_nregs[regno][mode]; | |
366 | regno < last; | |
367 | regno++) | |
368 | make_hard_regno_dead (regno); | |
369 | } | |
8160cd3e | 370 | else |
55a2c322 | 371 | { |
8160cd3e VM |
372 | if (sparseset_bit_p (pseudos_live, regno)) |
373 | { | |
374 | mark_pseudo_dead (regno, point); | |
375 | changed = true; | |
376 | } | |
18ea3d61 VM |
377 | bitmap_clear_bit (bb_gen_pseudos, regno); |
378 | bitmap_set_bit (bb_killed_pseudos, regno); | |
55a2c322 VM |
379 | } |
380 | return changed; | |
381 | } | |
382 | ||
8160cd3e VM |
383 | \f |
384 | ||
385 | /* This page contains code for making global live analysis of pseudos. | |
386 | The code works only when pseudo live info is changed on a BB | |
387 | border. That might be a consequence of some global transformations | |
388 | in LRA, e.g. PIC pseudo reuse or rematerialization. */ | |
389 | ||
390 | /* Structure describing local BB data used for pseudo | |
391 | live-analysis. */ | |
152914cd | 392 | struct bb_data_pseudos |
8160cd3e VM |
393 | { |
394 | /* Basic block about which the below data are. */ | |
395 | basic_block bb; | |
396 | bitmap_head killed_pseudos; /* pseudos killed in the BB. */ | |
397 | bitmap_head gen_pseudos; /* pseudos generated in the BB. */ | |
398 | }; | |
399 | ||
400 | /* Array for all BB data. Indexed by the corresponding BB index. */ | |
152914cd | 401 | typedef struct bb_data_pseudos *bb_data_t; |
8160cd3e VM |
402 | |
403 | /* All basic block data are referred through the following array. */ | |
404 | static bb_data_t bb_data; | |
405 | ||
406 | /* Two small functions for access to the bb data. */ | |
407 | static inline bb_data_t | |
408 | get_bb_data (basic_block bb) | |
409 | { | |
410 | return &bb_data[(bb)->index]; | |
411 | } | |
412 | ||
413 | static inline bb_data_t | |
414 | get_bb_data_by_index (int index) | |
415 | { | |
416 | return &bb_data[index]; | |
417 | } | |
418 | ||
419 | /* Bitmap with all hard regs. */ | |
420 | static bitmap_head all_hard_regs_bitmap; | |
421 | ||
8160cd3e VM |
422 | /* The transfer function used by the DF equation solver to propagate |
423 | live info through block with BB_INDEX according to the following | |
424 | equation: | |
425 | ||
426 | bb.livein = (bb.liveout - bb.kill) OR bb.gen | |
427 | */ | |
428 | static bool | |
429 | live_trans_fun (int bb_index) | |
430 | { | |
431 | basic_block bb = get_bb_data_by_index (bb_index)->bb; | |
432 | bitmap bb_liveout = df_get_live_out (bb); | |
433 | bitmap bb_livein = df_get_live_in (bb); | |
434 | bb_data_t bb_info = get_bb_data (bb); | |
435 | ||
436 | bitmap_and_compl (&temp_bitmap, bb_liveout, &all_hard_regs_bitmap); | |
437 | return bitmap_ior_and_compl (bb_livein, &bb_info->gen_pseudos, | |
438 | &temp_bitmap, &bb_info->killed_pseudos); | |
439 | } | |
440 | ||
441 | /* The confluence function used by the DF equation solver to set up | |
442 | live info for a block BB without predecessor. */ | |
443 | static void | |
444 | live_con_fun_0 (basic_block bb) | |
445 | { | |
446 | bitmap_and_into (df_get_live_out (bb), &all_hard_regs_bitmap); | |
447 | } | |
448 | ||
449 | /* The confluence function used by the DF equation solver to propagate | |
450 | live info from successor to predecessor on edge E according to the | |
451 | following equation: | |
452 | ||
453 | bb.liveout = 0 for entry block | OR (livein of successors) | |
454 | */ | |
455 | static bool | |
456 | live_con_fun_n (edge e) | |
457 | { | |
458 | basic_block bb = e->src; | |
459 | basic_block dest = e->dest; | |
460 | bitmap bb_liveout = df_get_live_out (bb); | |
461 | bitmap dest_livein = df_get_live_in (dest); | |
462 | ||
463 | return bitmap_ior_and_compl_into (bb_liveout, | |
464 | dest_livein, &all_hard_regs_bitmap); | |
465 | } | |
466 | ||
467 | /* Indexes of all function blocks. */ | |
468 | static bitmap_head all_blocks; | |
469 | ||
470 | /* Allocate and initialize data needed for global pseudo live | |
471 | analysis. */ | |
472 | static void | |
473 | initiate_live_solver (void) | |
474 | { | |
8160cd3e VM |
475 | bitmap_initialize (&all_hard_regs_bitmap, ®_obstack); |
476 | bitmap_set_range (&all_hard_regs_bitmap, 0, FIRST_PSEUDO_REGISTER); | |
152914cd | 477 | bb_data = XNEWVEC (struct bb_data_pseudos, last_basic_block_for_fn (cfun)); |
8160cd3e VM |
478 | bitmap_initialize (&all_blocks, ®_obstack); |
479 | ||
480 | basic_block bb; | |
481 | FOR_ALL_BB_FN (bb, cfun) | |
482 | { | |
483 | bb_data_t bb_info = get_bb_data (bb); | |
484 | bb_info->bb = bb; | |
485 | bitmap_initialize (&bb_info->killed_pseudos, ®_obstack); | |
486 | bitmap_initialize (&bb_info->gen_pseudos, ®_obstack); | |
487 | bitmap_set_bit (&all_blocks, bb->index); | |
488 | } | |
489 | } | |
490 | ||
491 | /* Free all data needed for global pseudo live analysis. */ | |
492 | static void | |
493 | finish_live_solver (void) | |
494 | { | |
495 | basic_block bb; | |
496 | ||
497 | bitmap_clear (&all_blocks); | |
498 | FOR_ALL_BB_FN (bb, cfun) | |
499 | { | |
500 | bb_data_t bb_info = get_bb_data (bb); | |
501 | bitmap_clear (&bb_info->killed_pseudos); | |
502 | bitmap_clear (&bb_info->gen_pseudos); | |
503 | } | |
504 | free (bb_data); | |
505 | bitmap_clear (&all_hard_regs_bitmap); | |
8160cd3e VM |
506 | } |
507 | ||
508 | \f | |
509 | ||
55a2c322 | 510 | /* Insn currently scanned. */ |
cfa434f6 | 511 | static rtx_insn *curr_insn; |
55a2c322 VM |
512 | /* The insn data. */ |
513 | static lra_insn_recog_data_t curr_id; | |
514 | /* The insn static data. */ | |
515 | static struct lra_static_insn_data *curr_static_id; | |
516 | ||
517 | /* Return true when one of the predecessor edges of BB is marked with | |
518 | EDGE_ABNORMAL_CALL or EDGE_EH. */ | |
519 | static bool | |
520 | bb_has_abnormal_call_pred (basic_block bb) | |
521 | { | |
522 | edge e; | |
523 | edge_iterator ei; | |
524 | ||
525 | FOR_EACH_EDGE (e, ei, bb->preds) | |
526 | { | |
527 | if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)) | |
528 | return true; | |
529 | } | |
530 | return false; | |
531 | } | |
532 | ||
533 | /* Vec containing execution frequencies of program points. */ | |
9771b263 | 534 | static vec<int> point_freq_vec; |
55a2c322 VM |
535 | |
536 | /* The start of the above vector elements. */ | |
537 | int *lra_point_freq; | |
538 | ||
539 | /* Increment the current program point POINT to the next point which has | |
540 | execution frequency FREQ. */ | |
541 | static void | |
542 | next_program_point (int &point, int freq) | |
543 | { | |
9771b263 DN |
544 | point_freq_vec.safe_push (freq); |
545 | lra_point_freq = point_freq_vec.address (); | |
55a2c322 VM |
546 | point++; |
547 | } | |
548 | ||
549 | /* Update the preference of HARD_REGNO for pseudo REGNO by PROFIT. */ | |
550 | void | |
551 | lra_setup_reload_pseudo_preferenced_hard_reg (int regno, | |
552 | int hard_regno, int profit) | |
553 | { | |
554 | lra_assert (regno >= lra_constraint_new_regno_start); | |
555 | if (lra_reg_info[regno].preferred_hard_regno1 == hard_regno) | |
556 | lra_reg_info[regno].preferred_hard_regno_profit1 += profit; | |
557 | else if (lra_reg_info[regno].preferred_hard_regno2 == hard_regno) | |
558 | lra_reg_info[regno].preferred_hard_regno_profit2 += profit; | |
559 | else if (lra_reg_info[regno].preferred_hard_regno1 < 0) | |
560 | { | |
561 | lra_reg_info[regno].preferred_hard_regno1 = hard_regno; | |
562 | lra_reg_info[regno].preferred_hard_regno_profit1 = profit; | |
563 | } | |
564 | else if (lra_reg_info[regno].preferred_hard_regno2 < 0 | |
565 | || profit > lra_reg_info[regno].preferred_hard_regno_profit2) | |
566 | { | |
567 | lra_reg_info[regno].preferred_hard_regno2 = hard_regno; | |
568 | lra_reg_info[regno].preferred_hard_regno_profit2 = profit; | |
569 | } | |
570 | else | |
571 | return; | |
572 | /* Keep the 1st hard regno as more profitable. */ | |
573 | if (lra_reg_info[regno].preferred_hard_regno1 >= 0 | |
574 | && lra_reg_info[regno].preferred_hard_regno2 >= 0 | |
575 | && (lra_reg_info[regno].preferred_hard_regno_profit2 | |
576 | > lra_reg_info[regno].preferred_hard_regno_profit1)) | |
577 | { | |
578 | int temp; | |
579 | ||
580 | temp = lra_reg_info[regno].preferred_hard_regno1; | |
581 | lra_reg_info[regno].preferred_hard_regno1 | |
582 | = lra_reg_info[regno].preferred_hard_regno2; | |
583 | lra_reg_info[regno].preferred_hard_regno2 = temp; | |
584 | temp = lra_reg_info[regno].preferred_hard_regno_profit1; | |
585 | lra_reg_info[regno].preferred_hard_regno_profit1 | |
586 | = lra_reg_info[regno].preferred_hard_regno_profit2; | |
587 | lra_reg_info[regno].preferred_hard_regno_profit2 = temp; | |
588 | } | |
589 | if (lra_dump_file != NULL) | |
590 | { | |
591 | if ((hard_regno = lra_reg_info[regno].preferred_hard_regno1) >= 0) | |
592 | fprintf (lra_dump_file, | |
593 | " Hard reg %d is preferable by r%d with profit %d\n", | |
594 | hard_regno, regno, | |
595 | lra_reg_info[regno].preferred_hard_regno_profit1); | |
596 | if ((hard_regno = lra_reg_info[regno].preferred_hard_regno2) >= 0) | |
597 | fprintf (lra_dump_file, | |
598 | " Hard reg %d is preferable by r%d with profit %d\n", | |
599 | hard_regno, regno, | |
600 | lra_reg_info[regno].preferred_hard_regno_profit2); | |
601 | } | |
602 | } | |
603 | ||
604 | /* Check that REGNO living through calls and setjumps, set up conflict | |
605 | regs, and clear corresponding bits in PSEUDOS_LIVE_THROUGH_CALLS and | |
606 | PSEUDOS_LIVE_THROUGH_SETJUMPS. */ | |
607 | static inline void | |
608 | check_pseudos_live_through_calls (int regno) | |
609 | { | |
8a26ad39 VM |
610 | int hr; |
611 | ||
55a2c322 VM |
612 | if (! sparseset_bit_p (pseudos_live_through_calls, regno)) |
613 | return; | |
614 | sparseset_clear_bit (pseudos_live_through_calls, regno); | |
615 | IOR_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs, | |
616 | call_used_reg_set); | |
8a26ad39 VM |
617 | |
618 | for (hr = 0; hr < FIRST_PSEUDO_REGISTER; hr++) | |
619 | if (HARD_REGNO_CALL_PART_CLOBBERED (hr, PSEUDO_REGNO_MODE (regno))) | |
620 | SET_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hr); | |
55a2c322 VM |
621 | #ifdef ENABLE_CHECKING |
622 | lra_reg_info[regno].call_p = true; | |
623 | #endif | |
624 | if (! sparseset_bit_p (pseudos_live_through_setjumps, regno)) | |
625 | return; | |
626 | sparseset_clear_bit (pseudos_live_through_setjumps, regno); | |
627 | /* Don't allocate pseudos that cross setjmps or any call, if this | |
628 | function receives a nonlocal goto. */ | |
629 | SET_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs); | |
630 | } | |
631 | ||
632 | /* Process insns of the basic block BB to update pseudo live ranges, | |
633 | pseudo hard register conflicts, and insn notes. We do it on | |
634 | backward scan of BB insns. CURR_POINT is the program point where | |
635 | BB ends. The function updates this counter and returns in | |
8160cd3e | 636 | CURR_POINT the program point where BB starts. The function also |
4ab74a01 | 637 | does local live info updates and can delete the dead insns if |
18ea3d61 | 638 | DEAD_INSN_P. It returns true if pseudo live info was |
8160cd3e VM |
639 | changed at the BB start. */ |
640 | static bool | |
18ea3d61 | 641 | process_bb_lives (basic_block bb, int &curr_point, bool dead_insn_p) |
55a2c322 VM |
642 | { |
643 | int i, regno, freq; | |
644 | unsigned int j; | |
645 | bitmap_iterator bi; | |
646 | bitmap reg_live_out; | |
647 | unsigned int px; | |
8160cd3e | 648 | rtx_insn *next; |
55a2c322 VM |
649 | rtx link, *link_loc; |
650 | bool need_curr_point_incr; | |
651 | ||
652 | reg_live_out = df_get_live_out (bb); | |
653 | sparseset_clear (pseudos_live); | |
654 | sparseset_clear (pseudos_live_through_calls); | |
655 | sparseset_clear (pseudos_live_through_setjumps); | |
656 | REG_SET_TO_HARD_REG_SET (hard_regs_live, reg_live_out); | |
657 | AND_COMPL_HARD_REG_SET (hard_regs_live, eliminable_regset); | |
55a2c322 VM |
658 | EXECUTE_IF_SET_IN_BITMAP (reg_live_out, FIRST_PSEUDO_REGISTER, j, bi) |
659 | mark_pseudo_live (j, curr_point); | |
660 | ||
18ea3d61 VM |
661 | bb_gen_pseudos = &get_bb_data (bb)->gen_pseudos; |
662 | bb_killed_pseudos = &get_bb_data (bb)->killed_pseudos; | |
663 | bitmap_clear (bb_gen_pseudos); | |
664 | bitmap_clear (bb_killed_pseudos); | |
55a2c322 VM |
665 | freq = REG_FREQ_FROM_BB (bb); |
666 | ||
667 | if (lra_dump_file != NULL) | |
668 | fprintf (lra_dump_file, " BB %d\n", bb->index); | |
669 | ||
670 | /* Scan the code of this basic block, noting which pseudos and hard | |
671 | regs are born or die. | |
672 | ||
673 | Note that this loop treats uninitialized values as live until the | |
674 | beginning of the block. For example, if an instruction uses | |
675 | (reg:DI foo), and only (subreg:SI (reg:DI foo) 0) is ever set, | |
676 | FOO will remain live until the beginning of the block. Likewise | |
677 | if FOO is not set at all. This is unnecessarily pessimistic, but | |
678 | it probably doesn't matter much in practice. */ | |
8160cd3e | 679 | FOR_BB_INSNS_REVERSE_SAFE (bb, curr_insn, next) |
55a2c322 VM |
680 | { |
681 | bool call_p; | |
682 | int dst_regno, src_regno; | |
683 | rtx set; | |
684 | struct lra_insn_reg *reg; | |
685 | ||
686 | if (!NONDEBUG_INSN_P (curr_insn)) | |
687 | continue; | |
688 | ||
689 | curr_id = lra_get_insn_recog_data (curr_insn); | |
690 | curr_static_id = curr_id->insn_static_data; | |
691 | if (lra_dump_file != NULL) | |
692 | fprintf (lra_dump_file, " Insn %u: point = %d\n", | |
693 | INSN_UID (curr_insn), curr_point); | |
694 | ||
8160cd3e VM |
695 | set = single_set (curr_insn); |
696 | ||
18ea3d61 | 697 | if (dead_insn_p && set != NULL_RTX |
6750565c VM |
698 | && REG_P (SET_DEST (set)) && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER |
699 | && find_reg_note (curr_insn, REG_EH_REGION, NULL_RTX) == NULL_RTX | |
700 | && ! may_trap_p (PATTERN (curr_insn)) | |
701 | /* Don't do premature remove of pic offset pseudo as we can | |
702 | start to use it after some reload generation. */ | |
703 | && (pic_offset_table_rtx == NULL_RTX | |
704 | || pic_offset_table_rtx != SET_DEST (set))) | |
8160cd3e | 705 | { |
18ea3d61 | 706 | bool remove_p = true; |
8160cd3e VM |
707 | |
708 | for (reg = curr_id->regs; reg != NULL; reg = reg->next) | |
709 | if (reg->type != OP_IN && sparseset_bit_p (pseudos_live, reg->regno)) | |
710 | { | |
18ea3d61 | 711 | remove_p = false; |
8160cd3e VM |
712 | break; |
713 | } | |
714 | for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next) | |
715 | if (reg->type != OP_IN) | |
716 | { | |
18ea3d61 | 717 | remove_p = false; |
8160cd3e VM |
718 | break; |
719 | } | |
18ea3d61 | 720 | if (remove_p && ! volatile_refs_p (PATTERN (curr_insn))) |
8160cd3e VM |
721 | { |
722 | dst_regno = REGNO (SET_DEST (set)); | |
723 | if (lra_dump_file != NULL) | |
724 | fprintf (lra_dump_file, " Deleting dead insn %u\n", | |
725 | INSN_UID (curr_insn)); | |
726 | lra_set_insn_deleted (curr_insn); | |
727 | if (lra_reg_info[dst_regno].nrefs == 0) | |
728 | { | |
729 | /* There might be some debug insns with the pseudo. */ | |
730 | unsigned int uid; | |
731 | rtx_insn *insn; | |
732 | ||
4ab74a01 VM |
733 | bitmap_copy (&temp_bitmap, &lra_reg_info[dst_regno].insn_bitmap); |
734 | EXECUTE_IF_SET_IN_BITMAP (&temp_bitmap, 0, uid, bi) | |
8160cd3e VM |
735 | { |
736 | insn = lra_insn_recog_data[uid]->insn; | |
737 | lra_substitute_pseudo_within_insn (insn, dst_regno, | |
738 | SET_SRC (set)); | |
739 | lra_update_insn_regno_info (insn); | |
740 | } | |
741 | } | |
742 | continue; | |
743 | } | |
744 | } | |
745 | ||
55a2c322 VM |
746 | /* Update max ref width and hard reg usage. */ |
747 | for (reg = curr_id->regs; reg != NULL; reg = reg->next) | |
748 | if (reg->regno >= FIRST_PSEUDO_REGISTER | |
749 | && (GET_MODE_SIZE (reg->biggest_mode) | |
750 | > GET_MODE_SIZE (lra_reg_info[reg->regno].biggest_mode))) | |
751 | lra_reg_info[reg->regno].biggest_mode = reg->biggest_mode; | |
752 | else if (reg->regno < FIRST_PSEUDO_REGISTER) | |
753 | lra_hard_reg_usage[reg->regno] += freq; | |
754 | ||
755 | call_p = CALL_P (curr_insn); | |
756 | if (complete_info_p | |
8160cd3e | 757 | && set != NULL_RTX |
55a2c322 VM |
758 | && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)) |
759 | /* Check that source regno does not conflict with | |
760 | destination regno to exclude most impossible | |
761 | preferences. */ | |
762 | && ((((src_regno = REGNO (SET_SRC (set))) >= FIRST_PSEUDO_REGISTER | |
763 | && ! sparseset_bit_p (pseudos_live, src_regno)) | |
764 | || (src_regno < FIRST_PSEUDO_REGISTER | |
765 | && ! TEST_HARD_REG_BIT (hard_regs_live, src_regno))) | |
766 | /* It might be 'inheritance pseudo <- reload pseudo'. */ | |
767 | || (src_regno >= lra_constraint_new_regno_start | |
768 | && ((int) REGNO (SET_DEST (set)) | |
debd8f30 CLT |
769 | >= lra_constraint_new_regno_start) |
770 | /* Remember to skip special cases where src/dest regnos are | |
771 | the same, e.g. insn SET pattern has matching constraints | |
772 | like =r,0. */ | |
773 | && src_regno != (int) REGNO (SET_DEST (set))))) | |
55a2c322 VM |
774 | { |
775 | int hard_regno = -1, regno = -1; | |
776 | ||
777 | dst_regno = REGNO (SET_DEST (set)); | |
778 | if (dst_regno >= lra_constraint_new_regno_start | |
779 | && src_regno >= lra_constraint_new_regno_start) | |
780 | lra_create_copy (dst_regno, src_regno, freq); | |
781 | else if (dst_regno >= lra_constraint_new_regno_start) | |
782 | { | |
783 | if ((hard_regno = src_regno) >= FIRST_PSEUDO_REGISTER) | |
784 | hard_regno = reg_renumber[src_regno]; | |
785 | regno = dst_regno; | |
786 | } | |
787 | else if (src_regno >= lra_constraint_new_regno_start) | |
788 | { | |
789 | if ((hard_regno = dst_regno) >= FIRST_PSEUDO_REGISTER) | |
790 | hard_regno = reg_renumber[dst_regno]; | |
791 | regno = src_regno; | |
792 | } | |
793 | if (regno >= 0 && hard_regno >= 0) | |
794 | lra_setup_reload_pseudo_preferenced_hard_reg | |
795 | (regno, hard_regno, freq); | |
796 | } | |
797 | ||
798 | sparseset_clear (start_living); | |
799 | ||
800 | /* Try to avoid unnecessary program point increments, this saves | |
801 | a lot of time in remove_some_program_points_and_update_live_ranges. | |
802 | We only need an increment if something becomes live or dies at this | |
803 | program point. */ | |
804 | need_curr_point_incr = false; | |
805 | ||
806 | /* Mark each defined value as live. We need to do this for | |
807 | unused values because they still conflict with quantities | |
808 | that are live at the time of the definition. */ | |
809 | for (reg = curr_id->regs; reg != NULL; reg = reg->next) | |
810 | if (reg->type != OP_IN) | |
811 | { | |
4ab74a01 VM |
812 | need_curr_point_incr |
813 | |= mark_regno_live (reg->regno, reg->biggest_mode, | |
18ea3d61 | 814 | curr_point); |
55a2c322 VM |
815 | check_pseudos_live_through_calls (reg->regno); |
816 | } | |
817 | ||
818 | for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next) | |
819 | if (reg->type != OP_IN) | |
820 | make_hard_regno_born (reg->regno); | |
821 | ||
822 | sparseset_copy (unused_set, start_living); | |
823 | ||
824 | sparseset_clear (start_dying); | |
825 | ||
826 | /* See which defined values die here. */ | |
827 | for (reg = curr_id->regs; reg != NULL; reg = reg->next) | |
828 | if (reg->type == OP_OUT && ! reg->early_clobber && ! reg->subreg_p) | |
4ab74a01 VM |
829 | need_curr_point_incr |
830 | |= mark_regno_dead (reg->regno, reg->biggest_mode, | |
18ea3d61 | 831 | curr_point); |
55a2c322 VM |
832 | |
833 | for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next) | |
834 | if (reg->type == OP_OUT && ! reg->early_clobber && ! reg->subreg_p) | |
835 | make_hard_regno_dead (reg->regno); | |
836 | ||
837 | if (call_p) | |
838 | { | |
1e288103 | 839 | if (flag_ipa_ra) |
10e1bdb2 TV |
840 | { |
841 | HARD_REG_SET this_call_used_reg_set; | |
842 | get_call_reg_set_usage (curr_insn, &this_call_used_reg_set, | |
843 | call_used_reg_set); | |
844 | ||
845 | EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, j) | |
846 | IOR_HARD_REG_SET (lra_reg_info[j].actual_call_used_reg_set, | |
847 | this_call_used_reg_set); | |
848 | } | |
849 | ||
55a2c322 VM |
850 | sparseset_ior (pseudos_live_through_calls, |
851 | pseudos_live_through_calls, pseudos_live); | |
852 | if (cfun->has_nonlocal_label | |
853 | || find_reg_note (curr_insn, REG_SETJMP, | |
854 | NULL_RTX) != NULL_RTX) | |
855 | sparseset_ior (pseudos_live_through_setjumps, | |
856 | pseudos_live_through_setjumps, pseudos_live); | |
857 | } | |
858 | ||
859 | /* Increment the current program point if we must. */ | |
860 | if (need_curr_point_incr) | |
861 | next_program_point (curr_point, freq); | |
862 | ||
863 | sparseset_clear (start_living); | |
864 | ||
865 | need_curr_point_incr = false; | |
866 | ||
867 | /* Mark each used value as live. */ | |
868 | for (reg = curr_id->regs; reg != NULL; reg = reg->next) | |
869 | if (reg->type == OP_IN) | |
870 | { | |
4ab74a01 VM |
871 | need_curr_point_incr |
872 | |= mark_regno_live (reg->regno, reg->biggest_mode, | |
18ea3d61 | 873 | curr_point); |
55a2c322 VM |
874 | check_pseudos_live_through_calls (reg->regno); |
875 | } | |
876 | ||
877 | for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next) | |
878 | if (reg->type == OP_IN) | |
879 | make_hard_regno_born (reg->regno); | |
880 | ||
881 | if (curr_id->arg_hard_regs != NULL) | |
882 | /* Make argument hard registers live. */ | |
883 | for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++) | |
884 | make_hard_regno_born (regno); | |
885 | ||
886 | sparseset_and_compl (dead_set, start_living, start_dying); | |
887 | ||
888 | /* Mark early clobber outputs dead. */ | |
889 | for (reg = curr_id->regs; reg != NULL; reg = reg->next) | |
890 | if (reg->type == OP_OUT && reg->early_clobber && ! reg->subreg_p) | |
4ab74a01 VM |
891 | need_curr_point_incr |
892 | |= mark_regno_dead (reg->regno, reg->biggest_mode, | |
18ea3d61 | 893 | curr_point); |
55a2c322 VM |
894 | |
895 | for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next) | |
896 | if (reg->type == OP_OUT && reg->early_clobber && ! reg->subreg_p) | |
897 | make_hard_regno_dead (reg->regno); | |
898 | ||
899 | if (need_curr_point_incr) | |
900 | next_program_point (curr_point, freq); | |
901 | ||
902 | /* Update notes. */ | |
903 | for (link_loc = ®_NOTES (curr_insn); (link = *link_loc) != NULL_RTX;) | |
904 | { | |
905 | if (REG_NOTE_KIND (link) != REG_DEAD | |
906 | && REG_NOTE_KIND (link) != REG_UNUSED) | |
907 | ; | |
908 | else if (REG_P (XEXP (link, 0))) | |
909 | { | |
910 | regno = REGNO (XEXP (link, 0)); | |
911 | if ((REG_NOTE_KIND (link) == REG_DEAD | |
912 | && ! sparseset_bit_p (dead_set, regno)) | |
913 | || (REG_NOTE_KIND (link) == REG_UNUSED | |
914 | && ! sparseset_bit_p (unused_set, regno))) | |
915 | { | |
916 | *link_loc = XEXP (link, 1); | |
917 | continue; | |
918 | } | |
919 | if (REG_NOTE_KIND (link) == REG_DEAD) | |
920 | sparseset_clear_bit (dead_set, regno); | |
921 | else if (REG_NOTE_KIND (link) == REG_UNUSED) | |
922 | sparseset_clear_bit (unused_set, regno); | |
923 | } | |
924 | link_loc = &XEXP (link, 1); | |
925 | } | |
926 | EXECUTE_IF_SET_IN_SPARSESET (dead_set, j) | |
927 | add_reg_note (curr_insn, REG_DEAD, regno_reg_rtx[j]); | |
928 | EXECUTE_IF_SET_IN_SPARSESET (unused_set, j) | |
929 | add_reg_note (curr_insn, REG_UNUSED, regno_reg_rtx[j]); | |
930 | } | |
931 | ||
932 | #ifdef EH_RETURN_DATA_REGNO | |
933 | if (bb_has_eh_pred (bb)) | |
934 | for (j = 0; ; ++j) | |
935 | { | |
936 | unsigned int regno = EH_RETURN_DATA_REGNO (j); | |
937 | ||
938 | if (regno == INVALID_REGNUM) | |
939 | break; | |
940 | make_hard_regno_born (regno); | |
941 | } | |
942 | #endif | |
943 | ||
944 | /* Pseudos can't go in stack regs at the start of a basic block that | |
945 | is reached by an abnormal edge. Likewise for call clobbered regs, | |
946 | because caller-save, fixup_abnormal_edges and possibly the table | |
947 | driven EH machinery are not quite ready to handle such pseudos | |
948 | live across such edges. */ | |
949 | if (bb_has_abnormal_pred (bb)) | |
950 | { | |
951 | #ifdef STACK_REGS | |
952 | EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, px) | |
953 | lra_reg_info[px].no_stack_p = true; | |
954 | for (px = FIRST_STACK_REG; px <= LAST_STACK_REG; px++) | |
955 | make_hard_regno_born (px); | |
956 | #endif | |
957 | /* No need to record conflicts for call clobbered regs if we | |
958 | have nonlocal labels around, as we don't ever try to | |
959 | allocate such regs in this case. */ | |
960 | if (!cfun->has_nonlocal_label && bb_has_abnormal_call_pred (bb)) | |
961 | for (px = 0; px < FIRST_PSEUDO_REGISTER; px++) | |
962 | if (call_used_regs[px]) | |
963 | make_hard_regno_born (px); | |
964 | } | |
965 | ||
8160cd3e | 966 | bool live_change_p = false; |
18ea3d61 VM |
967 | /* Check if bb border live info was changed. */ |
968 | unsigned int live_pseudos_num = 0; | |
969 | EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb), | |
970 | FIRST_PSEUDO_REGISTER, j, bi) | |
8160cd3e | 971 | { |
18ea3d61 VM |
972 | live_pseudos_num++; |
973 | if (! sparseset_bit_p (pseudos_live, j)) | |
8160cd3e | 974 | { |
9503ade2 VM |
975 | live_change_p = true; |
976 | if (lra_dump_file != NULL) | |
977 | fprintf (lra_dump_file, | |
978 | " r%d is removed as live at bb%d start\n", j, bb->index); | |
18ea3d61 | 979 | break; |
8160cd3e VM |
980 | } |
981 | } | |
9503ade2 VM |
982 | if (! live_change_p |
983 | && sparseset_cardinality (pseudos_live) != live_pseudos_num) | |
984 | { | |
985 | live_change_p = true; | |
986 | if (lra_dump_file != NULL) | |
987 | EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, j) | |
988 | if (! bitmap_bit_p (df_get_live_in (bb), j)) | |
989 | fprintf (lra_dump_file, | |
990 | " r%d is added to live at bb%d start\n", j, bb->index); | |
991 | } | |
55a2c322 VM |
992 | /* See if we'll need an increment at the end of this basic block. |
993 | An increment is needed if the PSEUDOS_LIVE set is not empty, | |
994 | to make sure the finish points are set up correctly. */ | |
995 | need_curr_point_incr = (sparseset_cardinality (pseudos_live) > 0); | |
996 | ||
997 | EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, i) | |
998 | mark_pseudo_dead (i, curr_point); | |
999 | ||
1000 | EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb), FIRST_PSEUDO_REGISTER, j, bi) | |
1001 | { | |
1002 | if (sparseset_cardinality (pseudos_live_through_calls) == 0) | |
1003 | break; | |
1004 | if (sparseset_bit_p (pseudos_live_through_calls, j)) | |
1005 | check_pseudos_live_through_calls (j); | |
1006 | } | |
8160cd3e | 1007 | |
55a2c322 VM |
1008 | if (need_curr_point_incr) |
1009 | next_program_point (curr_point, freq); | |
8160cd3e VM |
1010 | |
1011 | return live_change_p; | |
55a2c322 VM |
1012 | } |
1013 | ||
1014 | /* Compress pseudo live ranges by removing program points where | |
1015 | nothing happens. Complexity of many algorithms in LRA is linear | |
1016 | function of program points number. To speed up the code we try to | |
1017 | minimize the number of the program points here. */ | |
1018 | static void | |
1019 | remove_some_program_points_and_update_live_ranges (void) | |
1020 | { | |
1021 | unsigned i; | |
1022 | int n, max_regno; | |
1023 | int *map; | |
1024 | lra_live_range_t r, prev_r, next_r; | |
1025 | sbitmap born_or_dead, born, dead; | |
1026 | sbitmap_iterator sbi; | |
1027 | bool born_p, dead_p, prev_born_p, prev_dead_p; | |
1028 | ||
1029 | born = sbitmap_alloc (lra_live_max_point); | |
1030 | dead = sbitmap_alloc (lra_live_max_point); | |
f61e445a LC |
1031 | bitmap_clear (born); |
1032 | bitmap_clear (dead); | |
55a2c322 VM |
1033 | max_regno = max_reg_num (); |
1034 | for (i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++) | |
1035 | { | |
1036 | for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next) | |
1037 | { | |
1038 | lra_assert (r->start <= r->finish); | |
d7c028c0 LC |
1039 | bitmap_set_bit (born, r->start); |
1040 | bitmap_set_bit (dead, r->finish); | |
55a2c322 VM |
1041 | } |
1042 | } | |
1043 | born_or_dead = sbitmap_alloc (lra_live_max_point); | |
f61e445a | 1044 | bitmap_ior (born_or_dead, born, dead); |
55a2c322 VM |
1045 | map = XCNEWVEC (int, lra_live_max_point); |
1046 | n = -1; | |
1047 | prev_born_p = prev_dead_p = false; | |
d4ac4ce2 | 1048 | EXECUTE_IF_SET_IN_BITMAP (born_or_dead, 0, i, sbi) |
55a2c322 | 1049 | { |
d7c028c0 LC |
1050 | born_p = bitmap_bit_p (born, i); |
1051 | dead_p = bitmap_bit_p (dead, i); | |
55a2c322 VM |
1052 | if ((prev_born_p && ! prev_dead_p && born_p && ! dead_p) |
1053 | || (prev_dead_p && ! prev_born_p && dead_p && ! born_p)) | |
1054 | { | |
1055 | map[i] = n; | |
1056 | lra_point_freq[n] = MAX (lra_point_freq[n], lra_point_freq[i]); | |
1057 | } | |
1058 | else | |
1059 | { | |
1060 | map[i] = ++n; | |
1061 | lra_point_freq[n] = lra_point_freq[i]; | |
1062 | } | |
1063 | prev_born_p = born_p; | |
1064 | prev_dead_p = dead_p; | |
1065 | } | |
1066 | sbitmap_free (born_or_dead); | |
1067 | sbitmap_free (born); | |
1068 | sbitmap_free (dead); | |
1069 | n++; | |
1070 | if (lra_dump_file != NULL) | |
1071 | fprintf (lra_dump_file, "Compressing live ranges: from %d to %d - %d%%\n", | |
1072 | lra_live_max_point, n, 100 * n / lra_live_max_point); | |
1073 | if (n < lra_live_max_point) | |
1074 | { | |
1075 | lra_live_max_point = n; | |
1076 | for (i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++) | |
1077 | { | |
1078 | for (prev_r = NULL, r = lra_reg_info[i].live_ranges; | |
1079 | r != NULL; | |
1080 | r = next_r) | |
1081 | { | |
1082 | next_r = r->next; | |
1083 | r->start = map[r->start]; | |
1084 | r->finish = map[r->finish]; | |
1085 | if (prev_r == NULL || prev_r->start > r->finish + 1) | |
1086 | { | |
1087 | prev_r = r; | |
1088 | continue; | |
1089 | } | |
1090 | prev_r->start = r->start; | |
1091 | prev_r->next = next_r; | |
1092 | free_live_range (r); | |
1093 | } | |
1094 | } | |
1095 | } | |
1096 | free (map); | |
1097 | } | |
1098 | ||
1099 | /* Print live ranges R to file F. */ | |
1100 | void | |
1101 | lra_print_live_range_list (FILE *f, lra_live_range_t r) | |
1102 | { | |
1103 | for (; r != NULL; r = r->next) | |
1104 | fprintf (f, " [%d..%d]", r->start, r->finish); | |
1105 | fprintf (f, "\n"); | |
1106 | } | |
1107 | ||
7b3b6ae4 LC |
1108 | DEBUG_FUNCTION void |
1109 | debug (lra_live_range &ref) | |
1110 | { | |
1111 | lra_print_live_range_list (stderr, &ref); | |
1112 | } | |
1113 | ||
1114 | DEBUG_FUNCTION void | |
1115 | debug (lra_live_range *ptr) | |
1116 | { | |
1117 | if (ptr) | |
1118 | debug (*ptr); | |
1119 | else | |
1120 | fprintf (stderr, "<nil>\n"); | |
1121 | } | |
1122 | ||
55a2c322 VM |
1123 | /* Print live ranges R to stderr. */ |
1124 | void | |
1125 | lra_debug_live_range_list (lra_live_range_t r) | |
1126 | { | |
1127 | lra_print_live_range_list (stderr, r); | |
1128 | } | |
1129 | ||
1130 | /* Print live ranges of pseudo REGNO to file F. */ | |
1131 | static void | |
1132 | print_pseudo_live_ranges (FILE *f, int regno) | |
1133 | { | |
1134 | if (lra_reg_info[regno].live_ranges == NULL) | |
1135 | return; | |
1136 | fprintf (f, " r%d:", regno); | |
1137 | lra_print_live_range_list (f, lra_reg_info[regno].live_ranges); | |
1138 | } | |
1139 | ||
1140 | /* Print live ranges of pseudo REGNO to stderr. */ | |
1141 | void | |
1142 | lra_debug_pseudo_live_ranges (int regno) | |
1143 | { | |
1144 | print_pseudo_live_ranges (stderr, regno); | |
1145 | } | |
1146 | ||
1147 | /* Print live ranges of all pseudos to file F. */ | |
1148 | static void | |
1149 | print_live_ranges (FILE *f) | |
1150 | { | |
1151 | int i, max_regno; | |
1152 | ||
1153 | max_regno = max_reg_num (); | |
1154 | for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++) | |
1155 | print_pseudo_live_ranges (f, i); | |
1156 | } | |
1157 | ||
1158 | /* Print live ranges of all pseudos to stderr. */ | |
1159 | void | |
1160 | lra_debug_live_ranges (void) | |
1161 | { | |
1162 | print_live_ranges (stderr); | |
1163 | } | |
1164 | ||
1165 | /* Compress pseudo live ranges. */ | |
1166 | static void | |
1167 | compress_live_ranges (void) | |
1168 | { | |
1169 | remove_some_program_points_and_update_live_ranges (); | |
1170 | if (lra_dump_file != NULL) | |
1171 | { | |
1172 | fprintf (lra_dump_file, "Ranges after the compression:\n"); | |
1173 | print_live_ranges (lra_dump_file); | |
1174 | } | |
1175 | } | |
1176 | ||
8160cd3e VM |
1177 | \f |
1178 | ||
55a2c322 VM |
1179 | /* The number of the current live range pass. */ |
1180 | int lra_live_range_iter; | |
1181 | ||
18ea3d61 VM |
1182 | /* The function creates live ranges only for memory pseudos (or for |
1183 | all ones if ALL_P), set up CONFLICT_HARD_REGS for the pseudos. It | |
1184 | also does dead insn elimination if DEAD_INSN_P and global live | |
1185 | analysis only for pseudos and only if the pseudo live info was | |
1186 | changed on a BB border. Return TRUE if the live info was | |
1187 | changed. */ | |
1188 | static bool | |
1189 | lra_create_live_ranges_1 (bool all_p, bool dead_insn_p) | |
55a2c322 VM |
1190 | { |
1191 | basic_block bb; | |
1192 | int i, hard_regno, max_regno = max_reg_num (); | |
1193 | int curr_point; | |
8160cd3e | 1194 | bool bb_live_change_p, have_referenced_pseudos = false; |
55a2c322 VM |
1195 | |
1196 | timevar_push (TV_LRA_CREATE_LIVE_RANGES); | |
1197 | ||
1198 | complete_info_p = all_p; | |
1199 | if (lra_dump_file != NULL) | |
1200 | fprintf (lra_dump_file, | |
1201 | "\n********** Pseudo live ranges #%d: **********\n\n", | |
1202 | ++lra_live_range_iter); | |
1203 | memset (lra_hard_reg_usage, 0, sizeof (lra_hard_reg_usage)); | |
1204 | for (i = 0; i < max_regno; i++) | |
1205 | { | |
1206 | lra_reg_info[i].live_ranges = NULL; | |
1207 | CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs); | |
1208 | lra_reg_info[i].preferred_hard_regno1 = -1; | |
1209 | lra_reg_info[i].preferred_hard_regno2 = -1; | |
1210 | lra_reg_info[i].preferred_hard_regno_profit1 = 0; | |
1211 | lra_reg_info[i].preferred_hard_regno_profit2 = 0; | |
1212 | #ifdef STACK_REGS | |
1213 | lra_reg_info[i].no_stack_p = false; | |
1214 | #endif | |
b28ece32 VM |
1215 | /* The biggest mode is already set but its value might be to |
1216 | conservative because of recent transformation. Here in this | |
1217 | file we recalculate it again as it costs practically | |
1218 | nothing. */ | |
55a2c322 VM |
1219 | if (regno_reg_rtx[i] != NULL_RTX) |
1220 | lra_reg_info[i].biggest_mode = GET_MODE (regno_reg_rtx[i]); | |
1221 | else | |
1222 | lra_reg_info[i].biggest_mode = VOIDmode; | |
1223 | #ifdef ENABLE_CHECKING | |
1224 | lra_reg_info[i].call_p = false; | |
1225 | #endif | |
1226 | if (i >= FIRST_PSEUDO_REGISTER | |
85f9ce67 SB |
1227 | && lra_reg_info[i].nrefs != 0) |
1228 | { | |
1229 | if ((hard_regno = reg_renumber[i]) >= 0) | |
1230 | lra_hard_reg_usage[hard_regno] += lra_reg_info[i].freq; | |
1231 | have_referenced_pseudos = true; | |
1232 | } | |
55a2c322 VM |
1233 | } |
1234 | lra_free_copies (); | |
85f9ce67 SB |
1235 | |
1236 | /* Under some circumstances, we can have functions without pseudo | |
1237 | registers. For such functions, lra_live_max_point will be 0, | |
1238 | see e.g. PR55604, and there's nothing more to do for us here. */ | |
1239 | if (! have_referenced_pseudos) | |
1240 | { | |
1241 | timevar_pop (TV_LRA_CREATE_LIVE_RANGES); | |
18ea3d61 | 1242 | return false; |
85f9ce67 SB |
1243 | } |
1244 | ||
55a2c322 VM |
1245 | pseudos_live = sparseset_alloc (max_regno); |
1246 | pseudos_live_through_calls = sparseset_alloc (max_regno); | |
1247 | pseudos_live_through_setjumps = sparseset_alloc (max_regno); | |
1248 | start_living = sparseset_alloc (max_regno); | |
1249 | start_dying = sparseset_alloc (max_regno); | |
1250 | dead_set = sparseset_alloc (max_regno); | |
1251 | unused_set = sparseset_alloc (max_regno); | |
1252 | curr_point = 0; | |
9771b263 DN |
1253 | point_freq_vec.create (get_max_uid () * 2); |
1254 | lra_point_freq = point_freq_vec.address (); | |
8b1c6fd7 | 1255 | int *post_order_rev_cfg = XNEWVEC (int, last_basic_block_for_fn (cfun)); |
55a2c322 | 1256 | int n_blocks_inverted = inverted_post_order_compute (post_order_rev_cfg); |
0cae8d31 | 1257 | lra_assert (n_blocks_inverted == n_basic_blocks_for_fn (cfun)); |
8160cd3e | 1258 | bb_live_change_p = false; |
55a2c322 VM |
1259 | for (i = n_blocks_inverted - 1; i >= 0; --i) |
1260 | { | |
06e28de2 | 1261 | bb = BASIC_BLOCK_FOR_FN (cfun, post_order_rev_cfg[i]); |
fefa31b5 DM |
1262 | if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb |
1263 | == ENTRY_BLOCK_PTR_FOR_FN (cfun)) | |
55a2c322 | 1264 | continue; |
18ea3d61 | 1265 | if (process_bb_lives (bb, curr_point, dead_insn_p)) |
8160cd3e VM |
1266 | bb_live_change_p = true; |
1267 | } | |
1268 | if (bb_live_change_p) | |
1269 | { | |
1270 | /* We need to clear pseudo live info as some pseudos can | |
1271 | disappear, e.g. pseudos with used equivalences. */ | |
1272 | FOR_EACH_BB_FN (bb, cfun) | |
1273 | { | |
1274 | bitmap_clear_range (df_get_live_in (bb), FIRST_PSEUDO_REGISTER, | |
1275 | max_regno - FIRST_PSEUDO_REGISTER); | |
1276 | bitmap_clear_range (df_get_live_out (bb), FIRST_PSEUDO_REGISTER, | |
1277 | max_regno - FIRST_PSEUDO_REGISTER); | |
1278 | } | |
1279 | /* As we did not change CFG since LRA start we can use | |
1280 | DF-infrastructure solver to solve live data flow problem. */ | |
1281 | df_simple_dataflow | |
1282 | (DF_BACKWARD, NULL, live_con_fun_0, live_con_fun_n, | |
1283 | live_trans_fun, &all_blocks, | |
1284 | df_get_postorder (DF_BACKWARD), df_get_n_blocks (DF_BACKWARD)); | |
1285 | if (lra_dump_file != NULL) | |
1286 | { | |
6750565c VM |
1287 | fprintf (lra_dump_file, |
1288 | "Global pseudo live data have been updated:\n"); | |
8160cd3e VM |
1289 | basic_block bb; |
1290 | FOR_EACH_BB_FN (bb, cfun) | |
1291 | { | |
1292 | bb_data_t bb_info = get_bb_data (bb); | |
1293 | bitmap bb_livein = df_get_live_in (bb); | |
1294 | bitmap bb_liveout = df_get_live_out (bb); | |
1295 | ||
1296 | fprintf (lra_dump_file, "\nBB %d:\n", bb->index); | |
1297 | lra_dump_bitmap_with_title (" gen:", | |
1298 | &bb_info->gen_pseudos, bb->index); | |
1299 | lra_dump_bitmap_with_title (" killed:", | |
1300 | &bb_info->killed_pseudos, bb->index); | |
1301 | lra_dump_bitmap_with_title (" livein:", bb_livein, bb->index); | |
1302 | lra_dump_bitmap_with_title (" liveout:", bb_liveout, bb->index); | |
1303 | } | |
1304 | } | |
55a2c322 VM |
1305 | } |
1306 | free (post_order_rev_cfg); | |
1307 | lra_live_max_point = curr_point; | |
1308 | if (lra_dump_file != NULL) | |
1309 | print_live_ranges (lra_dump_file); | |
1310 | /* Clean up. */ | |
1311 | sparseset_free (unused_set); | |
1312 | sparseset_free (dead_set); | |
1313 | sparseset_free (start_dying); | |
1314 | sparseset_free (start_living); | |
1315 | sparseset_free (pseudos_live_through_calls); | |
1316 | sparseset_free (pseudos_live_through_setjumps); | |
1317 | sparseset_free (pseudos_live); | |
1318 | compress_live_ranges (); | |
1319 | timevar_pop (TV_LRA_CREATE_LIVE_RANGES); | |
18ea3d61 VM |
1320 | return bb_live_change_p; |
1321 | } | |
1322 | ||
1323 | /* The main entry function creates live-ranges and other live info | |
1324 | necessary for the assignment sub-pass. It uses | |
1325 | lra_creates_live_ranges_1 -- so read comments for the | |
1326 | function. */ | |
1327 | void | |
1328 | lra_create_live_ranges (bool all_p, bool dead_insn_p) | |
1329 | { | |
1330 | if (! lra_create_live_ranges_1 (all_p, dead_insn_p)) | |
1331 | return; | |
1332 | if (lra_dump_file != NULL) | |
1333 | fprintf (lra_dump_file, "Live info was changed -- recalculate it\n"); | |
1334 | /* Live info was changed on a bb border. It means that some info, | |
9503ade2 VM |
1335 | e.g. about conflict regs, calls crossed, and live ranges may be |
1336 | wrong. We need this info for allocation. So recalculate it | |
1337 | again but without removing dead insns which can change live info | |
1338 | again. Repetitive live range calculations are expensive therefore | |
1339 | we stop here as we already have correct info although some | |
1340 | improvement in rare cases could be possible on this sub-pass if | |
1341 | we do dead insn elimination again (still the improvement may | |
1342 | happen later). */ | |
18ea3d61 | 1343 | lra_clear_live_ranges (); |
9503ade2 | 1344 | bool res = lra_create_live_ranges_1 (all_p, false); |
18ea3d61 | 1345 | lra_assert (! res); |
55a2c322 VM |
1346 | } |
1347 | ||
1348 | /* Finish all live ranges. */ | |
1349 | void | |
1350 | lra_clear_live_ranges (void) | |
1351 | { | |
1352 | int i; | |
1353 | ||
1354 | for (i = 0; i < max_reg_num (); i++) | |
1355 | free_live_range_list (lra_reg_info[i].live_ranges); | |
9771b263 | 1356 | point_freq_vec.release (); |
55a2c322 VM |
1357 | } |
1358 | ||
1359 | /* Initialize live ranges data once per function. */ | |
1360 | void | |
1361 | lra_live_ranges_init (void) | |
1362 | { | |
1363 | live_range_pool = create_alloc_pool ("live ranges", | |
1364 | sizeof (struct lra_live_range), 100); | |
4ab74a01 | 1365 | bitmap_initialize (&temp_bitmap, ®_obstack); |
8160cd3e | 1366 | initiate_live_solver (); |
55a2c322 VM |
1367 | } |
1368 | ||
1369 | /* Finish live ranges data once per function. */ | |
1370 | void | |
1371 | lra_live_ranges_finish (void) | |
1372 | { | |
8160cd3e | 1373 | finish_live_solver (); |
4ab74a01 | 1374 | bitmap_clear (&temp_bitmap); |
55a2c322 VM |
1375 | free_alloc_pool (live_range_pool); |
1376 | } |