1 /* Build live ranges for pseudos.
2 Copyright (C) 2010-2015 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 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. */
30 #include "coretypes.h"
32 #include "hard-reg-set.h"
35 #include "insn-config.h"
47 #include "statistics.h"
48 #include "double-int.h"
50 #include "fixed-value.h"
64 #include "dominance.h"
67 #include "basic-block.h"
71 #include "sparseset.h"
74 /* Program points are enumerated by numbers from range
75 0..LRA_LIVE_MAX_POINT-1. There are approximately two times more
76 program points than insns. Program points are places in the
77 program where liveness info can be changed. In most general case
78 (there are more complicated cases too) some program points
79 correspond to places where input operand dies and other ones
80 correspond to places where output operands are born. */
81 int lra_live_max_point
;
83 /* Accumulated execution frequency of all references for each hard
85 int lra_hard_reg_usage
[FIRST_PSEUDO_REGISTER
];
87 /* A global flag whose true value says to build live ranges for all
88 pseudos, otherwise the live ranges only for pseudos got memory is
89 build. True value means also building copies and setting up hard
90 register preferences. The complete info is necessary only for the
91 assignment pass. The complete info is not needed for the
92 coalescing and spill passes. */
93 static bool complete_info_p
;
95 /* Pseudos live at current point in the RTL scan. */
96 static sparseset pseudos_live
;
98 /* Pseudos probably living through calls and setjumps. As setjump is
99 a call too, if a bit in PSEUDOS_LIVE_THROUGH_SETJUMPS is set up
100 then the corresponding bit in PSEUDOS_LIVE_THROUGH_CALLS is set up
101 too. These data are necessary for cases when only one subreg of a
102 multi-reg pseudo is set up after a call. So we decide it is
103 probably live when traversing bb backward. We are sure about
104 living when we see its usage or definition of the pseudo. */
105 static sparseset pseudos_live_through_calls
;
106 static sparseset pseudos_live_through_setjumps
;
108 /* Set of hard regs (except eliminable ones) currently live. */
109 static HARD_REG_SET hard_regs_live
;
111 /* Set of pseudos and hard registers start living/dying in the current
112 insn. These sets are used to update REG_DEAD and REG_UNUSED notes
114 static sparseset start_living
, start_dying
;
116 /* Set of pseudos and hard regs dead and unused in the current
118 static sparseset unused_set
, dead_set
;
120 /* Bitmap used for holding intermediate bitmap operation results. */
121 static bitmap_head temp_bitmap
;
123 /* Pool for pseudo live ranges. */
124 static alloc_pool live_range_pool
;
126 /* Free live range LR. */
128 free_live_range (lra_live_range_t lr
)
130 pool_free (live_range_pool
, lr
);
133 /* Free live range list LR. */
135 free_live_range_list (lra_live_range_t lr
)
137 lra_live_range_t next
;
142 free_live_range (lr
);
147 /* Create and return pseudo live range with given attributes. */
148 static lra_live_range_t
149 create_live_range (int regno
, int start
, int finish
, lra_live_range_t next
)
153 p
= (lra_live_range_t
) pool_alloc (live_range_pool
);
161 /* Copy live range R and return the result. */
162 static lra_live_range_t
163 copy_live_range (lra_live_range_t r
)
167 p
= (lra_live_range_t
) pool_alloc (live_range_pool
);
172 /* Copy live range list given by its head R and return the result. */
174 lra_copy_live_range_list (lra_live_range_t r
)
176 lra_live_range_t p
, first
, *chain
;
179 for (chain
= &first
; r
!= NULL
; r
= r
->next
)
181 p
= copy_live_range (r
);
188 /* Merge *non-intersected* ranges R1 and R2 and returns the result.
189 The function maintains the order of ranges and tries to minimize
190 size of the result range list. Ranges R1 and R2 may not be used
193 lra_merge_live_ranges (lra_live_range_t r1
, lra_live_range_t r2
)
195 lra_live_range_t first
, last
;
201 for (first
= last
= NULL
; r1
!= NULL
&& r2
!= NULL
;)
203 if (r1
->start
< r2
->start
)
206 if (r1
->start
== r2
->finish
+ 1)
208 /* Joint ranges: merge r1 and r2 into r1. */
209 r1
->start
= r2
->start
;
210 lra_live_range_t temp
= r2
;
212 pool_free (live_range_pool
, temp
);
216 gcc_assert (r2
->finish
+ 1 < r1
->start
);
217 /* Add r1 to the result. */
237 lra_assert (r2
!= NULL
);
246 /* Return TRUE if live ranges R1 and R2 intersect. */
248 lra_intersected_live_ranges_p (lra_live_range_t r1
, lra_live_range_t r2
)
250 /* Remember the live ranges are always kept ordered. */
251 while (r1
!= NULL
&& r2
!= NULL
)
253 if (r1
->start
> r2
->finish
)
255 else if (r2
->start
> r1
->finish
)
263 /* The function processing birth of hard register REGNO. It updates
264 living hard regs, START_LIVING, and conflict hard regs for living
265 pseudos. Conflict hard regs for the pic pseudo is not updated if
266 REGNO is REAL_PIC_OFFSET_TABLE_REGNUM and CHECK_PIC_PSEUDO_P is
269 make_hard_regno_born (int regno
, bool check_pic_pseudo_p ATTRIBUTE_UNUSED
)
273 lra_assert (regno
< FIRST_PSEUDO_REGISTER
);
274 if (TEST_HARD_REG_BIT (hard_regs_live
, regno
))
276 SET_HARD_REG_BIT (hard_regs_live
, regno
);
277 sparseset_set_bit (start_living
, regno
);
278 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live
, i
)
279 #ifdef REAL_PIC_OFFSET_TABLE_REGNUM
280 if (! check_pic_pseudo_p
281 || regno
!= REAL_PIC_OFFSET_TABLE_REGNUM
282 || pic_offset_table_rtx
== NULL
283 || i
!= REGNO (pic_offset_table_rtx
))
285 SET_HARD_REG_BIT (lra_reg_info
[i
].conflict_hard_regs
, regno
);
288 /* Process the death of hard register REGNO. This updates
289 hard_regs_live and START_DYING. */
291 make_hard_regno_dead (int regno
)
293 lra_assert (regno
< FIRST_PSEUDO_REGISTER
);
294 if (! TEST_HARD_REG_BIT (hard_regs_live
, regno
))
296 sparseset_set_bit (start_dying
, regno
);
297 CLEAR_HARD_REG_BIT (hard_regs_live
, regno
);
300 /* Mark pseudo REGNO as living at program point POINT, update conflicting
301 hard registers of the pseudo and START_LIVING, and start a new live
302 range for the pseudo corresponding to REGNO if it is necessary. */
304 mark_pseudo_live (int regno
, int point
)
308 lra_assert (regno
>= FIRST_PSEUDO_REGISTER
);
309 lra_assert (! sparseset_bit_p (pseudos_live
, regno
));
310 sparseset_set_bit (pseudos_live
, regno
);
311 IOR_HARD_REG_SET (lra_reg_info
[regno
].conflict_hard_regs
, hard_regs_live
);
313 if ((complete_info_p
|| lra_get_regno_hard_regno (regno
) < 0)
314 && ((p
= lra_reg_info
[regno
].live_ranges
) == NULL
315 || (p
->finish
!= point
&& p
->finish
+ 1 != point
)))
316 lra_reg_info
[regno
].live_ranges
317 = create_live_range (regno
, point
, -1, p
);
318 sparseset_set_bit (start_living
, regno
);
321 /* Mark pseudo REGNO as not living at program point POINT and update
323 This finishes the current live range for the pseudo corresponding
326 mark_pseudo_dead (int regno
, int point
)
330 lra_assert (regno
>= FIRST_PSEUDO_REGISTER
);
331 lra_assert (sparseset_bit_p (pseudos_live
, regno
));
332 sparseset_clear_bit (pseudos_live
, regno
);
333 sparseset_set_bit (start_dying
, regno
);
334 if (complete_info_p
|| lra_get_regno_hard_regno (regno
) < 0)
336 p
= lra_reg_info
[regno
].live_ranges
;
337 lra_assert (p
!= NULL
);
342 /* The corresponding bitmaps of BB currently being processed. */
343 static bitmap bb_killed_pseudos
, bb_gen_pseudos
;
345 /* Mark register REGNO (pseudo or hard register) in MODE as live at
346 program point POINT. Update BB_GEN_PSEUDOS.
347 Return TRUE if the liveness tracking sets were modified, or FALSE
348 if nothing changed. */
350 mark_regno_live (int regno
, machine_mode mode
, int point
)
353 bool changed
= false;
355 if (regno
< FIRST_PSEUDO_REGISTER
)
357 for (last
= regno
+ hard_regno_nregs
[regno
][mode
];
360 make_hard_regno_born (regno
, false);
364 if (! sparseset_bit_p (pseudos_live
, regno
))
366 mark_pseudo_live (regno
, point
);
369 bitmap_set_bit (bb_gen_pseudos
, regno
);
375 /* Mark register REGNO in MODE as dead at program point POINT. Update
376 BB_GEN_PSEUDOS and BB_KILLED_PSEUDOS. Return TRUE if the liveness
377 tracking sets were modified, or FALSE if nothing changed. */
379 mark_regno_dead (int regno
, machine_mode mode
, int point
)
382 bool changed
= false;
384 if (regno
< FIRST_PSEUDO_REGISTER
)
386 for (last
= regno
+ hard_regno_nregs
[regno
][mode
];
389 make_hard_regno_dead (regno
);
393 if (sparseset_bit_p (pseudos_live
, regno
))
395 mark_pseudo_dead (regno
, point
);
398 bitmap_clear_bit (bb_gen_pseudos
, regno
);
399 bitmap_set_bit (bb_killed_pseudos
, regno
);
406 /* This page contains code for making global live analysis of pseudos.
407 The code works only when pseudo live info is changed on a BB
408 border. That might be a consequence of some global transformations
409 in LRA, e.g. PIC pseudo reuse or rematerialization. */
411 /* Structure describing local BB data used for pseudo
413 struct bb_data_pseudos
415 /* Basic block about which the below data are. */
417 bitmap_head killed_pseudos
; /* pseudos killed in the BB. */
418 bitmap_head gen_pseudos
; /* pseudos generated in the BB. */
421 /* Array for all BB data. Indexed by the corresponding BB index. */
422 typedef struct bb_data_pseudos
*bb_data_t
;
424 /* All basic block data are referred through the following array. */
425 static bb_data_t bb_data
;
427 /* Two small functions for access to the bb data. */
428 static inline bb_data_t
429 get_bb_data (basic_block bb
)
431 return &bb_data
[(bb
)->index
];
434 static inline bb_data_t
435 get_bb_data_by_index (int index
)
437 return &bb_data
[index
];
440 /* Bitmap with all hard regs. */
441 static bitmap_head all_hard_regs_bitmap
;
443 /* The transfer function used by the DF equation solver to propagate
444 live info through block with BB_INDEX according to the following
447 bb.livein = (bb.liveout - bb.kill) OR bb.gen
450 live_trans_fun (int bb_index
)
452 basic_block bb
= get_bb_data_by_index (bb_index
)->bb
;
453 bitmap bb_liveout
= df_get_live_out (bb
);
454 bitmap bb_livein
= df_get_live_in (bb
);
455 bb_data_t bb_info
= get_bb_data (bb
);
457 bitmap_and_compl (&temp_bitmap
, bb_liveout
, &all_hard_regs_bitmap
);
458 return bitmap_ior_and_compl (bb_livein
, &bb_info
->gen_pseudos
,
459 &temp_bitmap
, &bb_info
->killed_pseudos
);
462 /* The confluence function used by the DF equation solver to set up
463 live info for a block BB without predecessor. */
465 live_con_fun_0 (basic_block bb
)
467 bitmap_and_into (df_get_live_out (bb
), &all_hard_regs_bitmap
);
470 /* The confluence function used by the DF equation solver to propagate
471 live info from successor to predecessor on edge E according to the
474 bb.liveout = 0 for entry block | OR (livein of successors)
477 live_con_fun_n (edge e
)
479 basic_block bb
= e
->src
;
480 basic_block dest
= e
->dest
;
481 bitmap bb_liveout
= df_get_live_out (bb
);
482 bitmap dest_livein
= df_get_live_in (dest
);
484 return bitmap_ior_and_compl_into (bb_liveout
,
485 dest_livein
, &all_hard_regs_bitmap
);
488 /* Indexes of all function blocks. */
489 static bitmap_head all_blocks
;
491 /* Allocate and initialize data needed for global pseudo live
494 initiate_live_solver (void)
496 bitmap_initialize (&all_hard_regs_bitmap
, ®_obstack
);
497 bitmap_set_range (&all_hard_regs_bitmap
, 0, FIRST_PSEUDO_REGISTER
);
498 bb_data
= XNEWVEC (struct bb_data_pseudos
, last_basic_block_for_fn (cfun
));
499 bitmap_initialize (&all_blocks
, ®_obstack
);
502 FOR_ALL_BB_FN (bb
, cfun
)
504 bb_data_t bb_info
= get_bb_data (bb
);
506 bitmap_initialize (&bb_info
->killed_pseudos
, ®_obstack
);
507 bitmap_initialize (&bb_info
->gen_pseudos
, ®_obstack
);
508 bitmap_set_bit (&all_blocks
, bb
->index
);
512 /* Free all data needed for global pseudo live analysis. */
514 finish_live_solver (void)
518 bitmap_clear (&all_blocks
);
519 FOR_ALL_BB_FN (bb
, cfun
)
521 bb_data_t bb_info
= get_bb_data (bb
);
522 bitmap_clear (&bb_info
->killed_pseudos
);
523 bitmap_clear (&bb_info
->gen_pseudos
);
526 bitmap_clear (&all_hard_regs_bitmap
);
531 /* Insn currently scanned. */
532 static rtx_insn
*curr_insn
;
534 static lra_insn_recog_data_t curr_id
;
535 /* The insn static data. */
536 static struct lra_static_insn_data
*curr_static_id
;
538 /* Return true when one of the predecessor edges of BB is marked with
539 EDGE_ABNORMAL_CALL or EDGE_EH. */
541 bb_has_abnormal_call_pred (basic_block bb
)
546 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
548 if (e
->flags
& (EDGE_ABNORMAL_CALL
| EDGE_EH
))
554 /* Vec containing execution frequencies of program points. */
555 static vec
<int> point_freq_vec
;
557 /* The start of the above vector elements. */
560 /* Increment the current program point POINT to the next point which has
561 execution frequency FREQ. */
563 next_program_point (int &point
, int freq
)
565 point_freq_vec
.safe_push (freq
);
566 lra_point_freq
= point_freq_vec
.address ();
570 /* Update the preference of HARD_REGNO for pseudo REGNO by PROFIT. */
572 lra_setup_reload_pseudo_preferenced_hard_reg (int regno
,
573 int hard_regno
, int profit
)
575 lra_assert (regno
>= lra_constraint_new_regno_start
);
576 if (lra_reg_info
[regno
].preferred_hard_regno1
== hard_regno
)
577 lra_reg_info
[regno
].preferred_hard_regno_profit1
+= profit
;
578 else if (lra_reg_info
[regno
].preferred_hard_regno2
== hard_regno
)
579 lra_reg_info
[regno
].preferred_hard_regno_profit2
+= profit
;
580 else if (lra_reg_info
[regno
].preferred_hard_regno1
< 0)
582 lra_reg_info
[regno
].preferred_hard_regno1
= hard_regno
;
583 lra_reg_info
[regno
].preferred_hard_regno_profit1
= profit
;
585 else if (lra_reg_info
[regno
].preferred_hard_regno2
< 0
586 || profit
> lra_reg_info
[regno
].preferred_hard_regno_profit2
)
588 lra_reg_info
[regno
].preferred_hard_regno2
= hard_regno
;
589 lra_reg_info
[regno
].preferred_hard_regno_profit2
= profit
;
593 /* Keep the 1st hard regno as more profitable. */
594 if (lra_reg_info
[regno
].preferred_hard_regno1
>= 0
595 && lra_reg_info
[regno
].preferred_hard_regno2
>= 0
596 && (lra_reg_info
[regno
].preferred_hard_regno_profit2
597 > lra_reg_info
[regno
].preferred_hard_regno_profit1
))
601 temp
= lra_reg_info
[regno
].preferred_hard_regno1
;
602 lra_reg_info
[regno
].preferred_hard_regno1
603 = lra_reg_info
[regno
].preferred_hard_regno2
;
604 lra_reg_info
[regno
].preferred_hard_regno2
= temp
;
605 temp
= lra_reg_info
[regno
].preferred_hard_regno_profit1
;
606 lra_reg_info
[regno
].preferred_hard_regno_profit1
607 = lra_reg_info
[regno
].preferred_hard_regno_profit2
;
608 lra_reg_info
[regno
].preferred_hard_regno_profit2
= temp
;
610 if (lra_dump_file
!= NULL
)
612 if ((hard_regno
= lra_reg_info
[regno
].preferred_hard_regno1
) >= 0)
613 fprintf (lra_dump_file
,
614 " Hard reg %d is preferable by r%d with profit %d\n",
616 lra_reg_info
[regno
].preferred_hard_regno_profit1
);
617 if ((hard_regno
= lra_reg_info
[regno
].preferred_hard_regno2
) >= 0)
618 fprintf (lra_dump_file
,
619 " Hard reg %d is preferable by r%d with profit %d\n",
621 lra_reg_info
[regno
].preferred_hard_regno_profit2
);
625 /* Check that REGNO living through calls and setjumps, set up conflict
626 regs, and clear corresponding bits in PSEUDOS_LIVE_THROUGH_CALLS and
627 PSEUDOS_LIVE_THROUGH_SETJUMPS. */
629 check_pseudos_live_through_calls (int regno
)
633 if (! sparseset_bit_p (pseudos_live_through_calls
, regno
))
635 sparseset_clear_bit (pseudos_live_through_calls
, regno
);
636 IOR_HARD_REG_SET (lra_reg_info
[regno
].conflict_hard_regs
,
639 for (hr
= 0; hr
< FIRST_PSEUDO_REGISTER
; hr
++)
640 if (HARD_REGNO_CALL_PART_CLOBBERED (hr
, PSEUDO_REGNO_MODE (regno
)))
641 SET_HARD_REG_BIT (lra_reg_info
[regno
].conflict_hard_regs
, hr
);
642 #ifdef ENABLE_CHECKING
643 lra_reg_info
[regno
].call_p
= true;
645 if (! sparseset_bit_p (pseudos_live_through_setjumps
, regno
))
647 sparseset_clear_bit (pseudos_live_through_setjumps
, regno
);
648 /* Don't allocate pseudos that cross setjmps or any call, if this
649 function receives a nonlocal goto. */
650 SET_HARD_REG_SET (lra_reg_info
[regno
].conflict_hard_regs
);
653 /* Process insns of the basic block BB to update pseudo live ranges,
654 pseudo hard register conflicts, and insn notes. We do it on
655 backward scan of BB insns. CURR_POINT is the program point where
656 BB ends. The function updates this counter and returns in
657 CURR_POINT the program point where BB starts. The function also
658 does local live info updates and can delete the dead insns if
659 DEAD_INSN_P. It returns true if pseudo live info was
660 changed at the BB start. */
662 process_bb_lives (basic_block bb
, int &curr_point
, bool dead_insn_p
)
671 bool need_curr_point_incr
;
673 reg_live_out
= df_get_live_out (bb
);
674 sparseset_clear (pseudos_live
);
675 sparseset_clear (pseudos_live_through_calls
);
676 sparseset_clear (pseudos_live_through_setjumps
);
677 REG_SET_TO_HARD_REG_SET (hard_regs_live
, reg_live_out
);
678 AND_COMPL_HARD_REG_SET (hard_regs_live
, eliminable_regset
);
679 EXECUTE_IF_SET_IN_BITMAP (reg_live_out
, FIRST_PSEUDO_REGISTER
, j
, bi
)
680 mark_pseudo_live (j
, curr_point
);
682 bb_gen_pseudos
= &get_bb_data (bb
)->gen_pseudos
;
683 bb_killed_pseudos
= &get_bb_data (bb
)->killed_pseudos
;
684 bitmap_clear (bb_gen_pseudos
);
685 bitmap_clear (bb_killed_pseudos
);
686 freq
= REG_FREQ_FROM_BB (bb
);
688 if (lra_dump_file
!= NULL
)
689 fprintf (lra_dump_file
, " BB %d\n", bb
->index
);
691 /* Scan the code of this basic block, noting which pseudos and hard
692 regs are born or die.
694 Note that this loop treats uninitialized values as live until the
695 beginning of the block. For example, if an instruction uses
696 (reg:DI foo), and only (subreg:SI (reg:DI foo) 0) is ever set,
697 FOO will remain live until the beginning of the block. Likewise
698 if FOO is not set at all. This is unnecessarily pessimistic, but
699 it probably doesn't matter much in practice. */
700 FOR_BB_INSNS_REVERSE_SAFE (bb
, curr_insn
, next
)
703 int dst_regno
, src_regno
;
705 struct lra_insn_reg
*reg
;
707 if (!NONDEBUG_INSN_P (curr_insn
))
710 curr_id
= lra_get_insn_recog_data (curr_insn
);
711 curr_static_id
= curr_id
->insn_static_data
;
712 if (lra_dump_file
!= NULL
)
713 fprintf (lra_dump_file
, " Insn %u: point = %d\n",
714 INSN_UID (curr_insn
), curr_point
);
716 set
= single_set (curr_insn
);
718 if (dead_insn_p
&& set
!= NULL_RTX
719 && REG_P (SET_DEST (set
)) && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
720 && find_reg_note (curr_insn
, REG_EH_REGION
, NULL_RTX
) == NULL_RTX
721 && ! may_trap_p (PATTERN (curr_insn
))
722 /* Don't do premature remove of pic offset pseudo as we can
723 start to use it after some reload generation. */
724 && (pic_offset_table_rtx
== NULL_RTX
725 || pic_offset_table_rtx
!= SET_DEST (set
)))
727 bool remove_p
= true;
729 for (reg
= curr_id
->regs
; reg
!= NULL
; reg
= reg
->next
)
730 if (reg
->type
!= OP_IN
&& sparseset_bit_p (pseudos_live
, reg
->regno
))
735 for (reg
= curr_static_id
->hard_regs
; reg
!= NULL
; reg
= reg
->next
)
736 if (reg
->type
!= OP_IN
)
741 if (remove_p
&& ! volatile_refs_p (PATTERN (curr_insn
)))
743 dst_regno
= REGNO (SET_DEST (set
));
744 if (lra_dump_file
!= NULL
)
745 fprintf (lra_dump_file
, " Deleting dead insn %u\n",
746 INSN_UID (curr_insn
));
747 lra_set_insn_deleted (curr_insn
);
748 if (lra_reg_info
[dst_regno
].nrefs
== 0)
750 /* There might be some debug insns with the pseudo. */
754 bitmap_copy (&temp_bitmap
, &lra_reg_info
[dst_regno
].insn_bitmap
);
755 EXECUTE_IF_SET_IN_BITMAP (&temp_bitmap
, 0, uid
, bi
)
757 insn
= lra_insn_recog_data
[uid
]->insn
;
758 lra_substitute_pseudo_within_insn (insn
, dst_regno
,
760 lra_update_insn_regno_info (insn
);
767 /* Update max ref width and hard reg usage. */
768 for (reg
= curr_id
->regs
; reg
!= NULL
; reg
= reg
->next
)
769 if (reg
->regno
>= FIRST_PSEUDO_REGISTER
770 && (GET_MODE_SIZE (reg
->biggest_mode
)
771 > GET_MODE_SIZE (lra_reg_info
[reg
->regno
].biggest_mode
)))
772 lra_reg_info
[reg
->regno
].biggest_mode
= reg
->biggest_mode
;
773 else if (reg
->regno
< FIRST_PSEUDO_REGISTER
)
774 lra_hard_reg_usage
[reg
->regno
] += freq
;
776 call_p
= CALL_P (curr_insn
);
779 && REG_P (SET_DEST (set
)) && REG_P (SET_SRC (set
))
780 /* Check that source regno does not conflict with
781 destination regno to exclude most impossible
783 && ((((src_regno
= REGNO (SET_SRC (set
))) >= FIRST_PSEUDO_REGISTER
784 && ! sparseset_bit_p (pseudos_live
, src_regno
))
785 || (src_regno
< FIRST_PSEUDO_REGISTER
786 && ! TEST_HARD_REG_BIT (hard_regs_live
, src_regno
)))
787 /* It might be 'inheritance pseudo <- reload pseudo'. */
788 || (src_regno
>= lra_constraint_new_regno_start
789 && ((int) REGNO (SET_DEST (set
))
790 >= lra_constraint_new_regno_start
)
791 /* Remember to skip special cases where src/dest regnos are
792 the same, e.g. insn SET pattern has matching constraints
794 && src_regno
!= (int) REGNO (SET_DEST (set
)))))
796 int hard_regno
= -1, regno
= -1;
798 dst_regno
= REGNO (SET_DEST (set
));
799 if (dst_regno
>= lra_constraint_new_regno_start
800 && src_regno
>= lra_constraint_new_regno_start
)
801 lra_create_copy (dst_regno
, src_regno
, freq
);
802 else if (dst_regno
>= lra_constraint_new_regno_start
)
804 if ((hard_regno
= src_regno
) >= FIRST_PSEUDO_REGISTER
)
805 hard_regno
= reg_renumber
[src_regno
];
808 else if (src_regno
>= lra_constraint_new_regno_start
)
810 if ((hard_regno
= dst_regno
) >= FIRST_PSEUDO_REGISTER
)
811 hard_regno
= reg_renumber
[dst_regno
];
814 if (regno
>= 0 && hard_regno
>= 0)
815 lra_setup_reload_pseudo_preferenced_hard_reg
816 (regno
, hard_regno
, freq
);
819 sparseset_clear (start_living
);
821 /* Try to avoid unnecessary program point increments, this saves
822 a lot of time in remove_some_program_points_and_update_live_ranges.
823 We only need an increment if something becomes live or dies at this
825 need_curr_point_incr
= false;
827 /* Mark each defined value as live. We need to do this for
828 unused values because they still conflict with quantities
829 that are live at the time of the definition. */
830 for (reg
= curr_id
->regs
; reg
!= NULL
; reg
= reg
->next
)
831 if (reg
->type
!= OP_IN
)
834 |= mark_regno_live (reg
->regno
, reg
->biggest_mode
,
836 check_pseudos_live_through_calls (reg
->regno
);
839 for (reg
= curr_static_id
->hard_regs
; reg
!= NULL
; reg
= reg
->next
)
840 if (reg
->type
!= OP_IN
)
841 make_hard_regno_born (reg
->regno
, false);
843 sparseset_copy (unused_set
, start_living
);
845 sparseset_clear (start_dying
);
847 /* See which defined values die here. */
848 for (reg
= curr_id
->regs
; reg
!= NULL
; reg
= reg
->next
)
849 if (reg
->type
== OP_OUT
&& ! reg
->early_clobber
&& ! reg
->subreg_p
)
851 |= mark_regno_dead (reg
->regno
, reg
->biggest_mode
,
854 for (reg
= curr_static_id
->hard_regs
; reg
!= NULL
; reg
= reg
->next
)
855 if (reg
->type
== OP_OUT
&& ! reg
->early_clobber
&& ! reg
->subreg_p
)
856 make_hard_regno_dead (reg
->regno
);
862 HARD_REG_SET this_call_used_reg_set
;
863 get_call_reg_set_usage (curr_insn
, &this_call_used_reg_set
,
866 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live
, j
)
867 IOR_HARD_REG_SET (lra_reg_info
[j
].actual_call_used_reg_set
,
868 this_call_used_reg_set
);
871 sparseset_ior (pseudos_live_through_calls
,
872 pseudos_live_through_calls
, pseudos_live
);
873 if (cfun
->has_nonlocal_label
874 || find_reg_note (curr_insn
, REG_SETJMP
,
875 NULL_RTX
) != NULL_RTX
)
876 sparseset_ior (pseudos_live_through_setjumps
,
877 pseudos_live_through_setjumps
, pseudos_live
);
880 /* Increment the current program point if we must. */
881 if (need_curr_point_incr
)
882 next_program_point (curr_point
, freq
);
884 sparseset_clear (start_living
);
886 need_curr_point_incr
= false;
888 /* Mark each used value as live. */
889 for (reg
= curr_id
->regs
; reg
!= NULL
; reg
= reg
->next
)
890 if (reg
->type
== OP_IN
)
893 |= mark_regno_live (reg
->regno
, reg
->biggest_mode
,
895 check_pseudos_live_through_calls (reg
->regno
);
898 for (reg
= curr_static_id
->hard_regs
; reg
!= NULL
; reg
= reg
->next
)
899 if (reg
->type
== OP_IN
)
900 make_hard_regno_born (reg
->regno
, false);
902 if (curr_id
->arg_hard_regs
!= NULL
)
903 /* Make argument hard registers live. Don't create conflict
904 of used REAL_PIC_OFFSET_TABLE_REGNUM and the pic pseudo. */
905 for (i
= 0; (regno
= curr_id
->arg_hard_regs
[i
]) >= 0; i
++)
906 make_hard_regno_born (regno
, true);
908 sparseset_and_compl (dead_set
, start_living
, start_dying
);
910 /* Mark early clobber outputs dead. */
911 for (reg
= curr_id
->regs
; reg
!= NULL
; reg
= reg
->next
)
912 if (reg
->type
== OP_OUT
&& reg
->early_clobber
&& ! reg
->subreg_p
)
914 |= mark_regno_dead (reg
->regno
, reg
->biggest_mode
,
917 for (reg
= curr_static_id
->hard_regs
; reg
!= NULL
; reg
= reg
->next
)
918 if (reg
->type
== OP_OUT
&& reg
->early_clobber
&& ! reg
->subreg_p
)
919 make_hard_regno_dead (reg
->regno
);
921 if (need_curr_point_incr
)
922 next_program_point (curr_point
, freq
);
925 for (link_loc
= ®_NOTES (curr_insn
); (link
= *link_loc
) != NULL_RTX
;)
927 if (REG_NOTE_KIND (link
) != REG_DEAD
928 && REG_NOTE_KIND (link
) != REG_UNUSED
)
930 else if (REG_P (XEXP (link
, 0)))
932 regno
= REGNO (XEXP (link
, 0));
933 if ((REG_NOTE_KIND (link
) == REG_DEAD
934 && ! sparseset_bit_p (dead_set
, regno
))
935 || (REG_NOTE_KIND (link
) == REG_UNUSED
936 && ! sparseset_bit_p (unused_set
, regno
)))
938 *link_loc
= XEXP (link
, 1);
941 if (REG_NOTE_KIND (link
) == REG_DEAD
)
942 sparseset_clear_bit (dead_set
, regno
);
943 else if (REG_NOTE_KIND (link
) == REG_UNUSED
)
944 sparseset_clear_bit (unused_set
, regno
);
946 link_loc
= &XEXP (link
, 1);
948 EXECUTE_IF_SET_IN_SPARSESET (dead_set
, j
)
949 add_reg_note (curr_insn
, REG_DEAD
, regno_reg_rtx
[j
]);
950 EXECUTE_IF_SET_IN_SPARSESET (unused_set
, j
)
951 add_reg_note (curr_insn
, REG_UNUSED
, regno_reg_rtx
[j
]);
954 if (bb_has_eh_pred (bb
))
957 unsigned int regno
= EH_RETURN_DATA_REGNO (j
);
959 if (regno
== INVALID_REGNUM
)
961 make_hard_regno_born (regno
, false);
964 /* Pseudos can't go in stack regs at the start of a basic block that
965 is reached by an abnormal edge. Likewise for call clobbered regs,
966 because caller-save, fixup_abnormal_edges and possibly the table
967 driven EH machinery are not quite ready to handle such pseudos
968 live across such edges. */
969 if (bb_has_abnormal_pred (bb
))
972 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live
, px
)
973 lra_reg_info
[px
].no_stack_p
= true;
974 for (px
= FIRST_STACK_REG
; px
<= LAST_STACK_REG
; px
++)
975 make_hard_regno_born (px
, false);
977 /* No need to record conflicts for call clobbered regs if we
978 have nonlocal labels around, as we don't ever try to
979 allocate such regs in this case. */
980 if (!cfun
->has_nonlocal_label
&& bb_has_abnormal_call_pred (bb
))
981 for (px
= 0; px
< FIRST_PSEUDO_REGISTER
; px
++)
982 if (call_used_regs
[px
])
983 make_hard_regno_born (px
, false);
986 bool live_change_p
= false;
987 /* Check if bb border live info was changed. */
988 unsigned int live_pseudos_num
= 0;
989 EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb
),
990 FIRST_PSEUDO_REGISTER
, j
, bi
)
993 if (! sparseset_bit_p (pseudos_live
, j
))
995 live_change_p
= true;
996 if (lra_dump_file
!= NULL
)
997 fprintf (lra_dump_file
,
998 " r%d is removed as live at bb%d start\n", j
, bb
->index
);
1003 && sparseset_cardinality (pseudos_live
) != live_pseudos_num
)
1005 live_change_p
= true;
1006 if (lra_dump_file
!= NULL
)
1007 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live
, j
)
1008 if (! bitmap_bit_p (df_get_live_in (bb
), j
))
1009 fprintf (lra_dump_file
,
1010 " r%d is added to live at bb%d start\n", j
, bb
->index
);
1012 /* See if we'll need an increment at the end of this basic block.
1013 An increment is needed if the PSEUDOS_LIVE set is not empty,
1014 to make sure the finish points are set up correctly. */
1015 need_curr_point_incr
= (sparseset_cardinality (pseudos_live
) > 0);
1017 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live
, i
)
1018 mark_pseudo_dead (i
, curr_point
);
1020 EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb
), FIRST_PSEUDO_REGISTER
, j
, bi
)
1022 if (sparseset_cardinality (pseudos_live_through_calls
) == 0)
1024 if (sparseset_bit_p (pseudos_live_through_calls
, j
))
1025 check_pseudos_live_through_calls (j
);
1028 if (need_curr_point_incr
)
1029 next_program_point (curr_point
, freq
);
1031 return live_change_p
;
1034 /* Compress pseudo live ranges by removing program points where
1035 nothing happens. Complexity of many algorithms in LRA is linear
1036 function of program points number. To speed up the code we try to
1037 minimize the number of the program points here. */
1039 remove_some_program_points_and_update_live_ranges (void)
1044 lra_live_range_t r
, prev_r
, next_r
;
1045 sbitmap born_or_dead
, born
, dead
;
1046 sbitmap_iterator sbi
;
1047 bool born_p
, dead_p
, prev_born_p
, prev_dead_p
;
1049 born
= sbitmap_alloc (lra_live_max_point
);
1050 dead
= sbitmap_alloc (lra_live_max_point
);
1051 bitmap_clear (born
);
1052 bitmap_clear (dead
);
1053 max_regno
= max_reg_num ();
1054 for (i
= FIRST_PSEUDO_REGISTER
; i
< (unsigned) max_regno
; i
++)
1056 for (r
= lra_reg_info
[i
].live_ranges
; r
!= NULL
; r
= r
->next
)
1058 lra_assert (r
->start
<= r
->finish
);
1059 bitmap_set_bit (born
, r
->start
);
1060 bitmap_set_bit (dead
, r
->finish
);
1063 born_or_dead
= sbitmap_alloc (lra_live_max_point
);
1064 bitmap_ior (born_or_dead
, born
, dead
);
1065 map
= XCNEWVEC (int, lra_live_max_point
);
1067 prev_born_p
= prev_dead_p
= false;
1068 EXECUTE_IF_SET_IN_BITMAP (born_or_dead
, 0, i
, sbi
)
1070 born_p
= bitmap_bit_p (born
, i
);
1071 dead_p
= bitmap_bit_p (dead
, i
);
1072 if ((prev_born_p
&& ! prev_dead_p
&& born_p
&& ! dead_p
)
1073 || (prev_dead_p
&& ! prev_born_p
&& dead_p
&& ! born_p
))
1076 lra_point_freq
[n
] = MAX (lra_point_freq
[n
], lra_point_freq
[i
]);
1081 lra_point_freq
[n
] = lra_point_freq
[i
];
1083 prev_born_p
= born_p
;
1084 prev_dead_p
= dead_p
;
1086 sbitmap_free (born_or_dead
);
1087 sbitmap_free (born
);
1088 sbitmap_free (dead
);
1090 if (lra_dump_file
!= NULL
)
1091 fprintf (lra_dump_file
, "Compressing live ranges: from %d to %d - %d%%\n",
1092 lra_live_max_point
, n
, 100 * n
/ lra_live_max_point
);
1093 if (n
< lra_live_max_point
)
1095 lra_live_max_point
= n
;
1096 for (i
= FIRST_PSEUDO_REGISTER
; i
< (unsigned) max_regno
; i
++)
1098 for (prev_r
= NULL
, r
= lra_reg_info
[i
].live_ranges
;
1103 r
->start
= map
[r
->start
];
1104 r
->finish
= map
[r
->finish
];
1105 if (prev_r
== NULL
|| prev_r
->start
> r
->finish
+ 1)
1110 prev_r
->start
= r
->start
;
1111 prev_r
->next
= next_r
;
1112 free_live_range (r
);
1119 /* Print live ranges R to file F. */
1121 lra_print_live_range_list (FILE *f
, lra_live_range_t r
)
1123 for (; r
!= NULL
; r
= r
->next
)
1124 fprintf (f
, " [%d..%d]", r
->start
, r
->finish
);
1129 debug (lra_live_range
&ref
)
1131 lra_print_live_range_list (stderr
, &ref
);
1135 debug (lra_live_range
*ptr
)
1140 fprintf (stderr
, "<nil>\n");
1143 /* Print live ranges R to stderr. */
1145 lra_debug_live_range_list (lra_live_range_t r
)
1147 lra_print_live_range_list (stderr
, r
);
1150 /* Print live ranges of pseudo REGNO to file F. */
1152 print_pseudo_live_ranges (FILE *f
, int regno
)
1154 if (lra_reg_info
[regno
].live_ranges
== NULL
)
1156 fprintf (f
, " r%d:", regno
);
1157 lra_print_live_range_list (f
, lra_reg_info
[regno
].live_ranges
);
1160 /* Print live ranges of pseudo REGNO to stderr. */
1162 lra_debug_pseudo_live_ranges (int regno
)
1164 print_pseudo_live_ranges (stderr
, regno
);
1167 /* Print live ranges of all pseudos to file F. */
1169 print_live_ranges (FILE *f
)
1173 max_regno
= max_reg_num ();
1174 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
1175 print_pseudo_live_ranges (f
, i
);
1178 /* Print live ranges of all pseudos to stderr. */
1180 lra_debug_live_ranges (void)
1182 print_live_ranges (stderr
);
1185 /* Compress pseudo live ranges. */
1187 compress_live_ranges (void)
1189 remove_some_program_points_and_update_live_ranges ();
1190 if (lra_dump_file
!= NULL
)
1192 fprintf (lra_dump_file
, "Ranges after the compression:\n");
1193 print_live_ranges (lra_dump_file
);
1199 /* The number of the current live range pass. */
1200 int lra_live_range_iter
;
1202 /* The function creates live ranges only for memory pseudos (or for
1203 all ones if ALL_P), set up CONFLICT_HARD_REGS for the pseudos. It
1204 also does dead insn elimination if DEAD_INSN_P and global live
1205 analysis only for pseudos and only if the pseudo live info was
1206 changed on a BB border. Return TRUE if the live info was
1209 lra_create_live_ranges_1 (bool all_p
, bool dead_insn_p
)
1212 int i
, hard_regno
, max_regno
= max_reg_num ();
1214 bool bb_live_change_p
, have_referenced_pseudos
= false;
1216 timevar_push (TV_LRA_CREATE_LIVE_RANGES
);
1218 complete_info_p
= all_p
;
1219 if (lra_dump_file
!= NULL
)
1220 fprintf (lra_dump_file
,
1221 "\n********** Pseudo live ranges #%d: **********\n\n",
1222 ++lra_live_range_iter
);
1223 memset (lra_hard_reg_usage
, 0, sizeof (lra_hard_reg_usage
));
1224 for (i
= 0; i
< max_regno
; i
++)
1226 lra_reg_info
[i
].live_ranges
= NULL
;
1227 CLEAR_HARD_REG_SET (lra_reg_info
[i
].conflict_hard_regs
);
1228 lra_reg_info
[i
].preferred_hard_regno1
= -1;
1229 lra_reg_info
[i
].preferred_hard_regno2
= -1;
1230 lra_reg_info
[i
].preferred_hard_regno_profit1
= 0;
1231 lra_reg_info
[i
].preferred_hard_regno_profit2
= 0;
1233 lra_reg_info
[i
].no_stack_p
= false;
1235 /* The biggest mode is already set but its value might be to
1236 conservative because of recent transformation. Here in this
1237 file we recalculate it again as it costs practically
1239 if (regno_reg_rtx
[i
] != NULL_RTX
)
1240 lra_reg_info
[i
].biggest_mode
= GET_MODE (regno_reg_rtx
[i
]);
1242 lra_reg_info
[i
].biggest_mode
= VOIDmode
;
1243 #ifdef ENABLE_CHECKING
1244 lra_reg_info
[i
].call_p
= false;
1246 if (i
>= FIRST_PSEUDO_REGISTER
1247 && lra_reg_info
[i
].nrefs
!= 0)
1249 if ((hard_regno
= reg_renumber
[i
]) >= 0)
1250 lra_hard_reg_usage
[hard_regno
] += lra_reg_info
[i
].freq
;
1251 have_referenced_pseudos
= true;
1256 /* Under some circumstances, we can have functions without pseudo
1257 registers. For such functions, lra_live_max_point will be 0,
1258 see e.g. PR55604, and there's nothing more to do for us here. */
1259 if (! have_referenced_pseudos
)
1261 timevar_pop (TV_LRA_CREATE_LIVE_RANGES
);
1265 pseudos_live
= sparseset_alloc (max_regno
);
1266 pseudos_live_through_calls
= sparseset_alloc (max_regno
);
1267 pseudos_live_through_setjumps
= sparseset_alloc (max_regno
);
1268 start_living
= sparseset_alloc (max_regno
);
1269 start_dying
= sparseset_alloc (max_regno
);
1270 dead_set
= sparseset_alloc (max_regno
);
1271 unused_set
= sparseset_alloc (max_regno
);
1273 point_freq_vec
.create (get_max_uid () * 2);
1274 lra_point_freq
= point_freq_vec
.address ();
1275 int *post_order_rev_cfg
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
1276 int n_blocks_inverted
= inverted_post_order_compute (post_order_rev_cfg
);
1277 lra_assert (n_blocks_inverted
== n_basic_blocks_for_fn (cfun
));
1278 bb_live_change_p
= false;
1279 for (i
= n_blocks_inverted
- 1; i
>= 0; --i
)
1281 bb
= BASIC_BLOCK_FOR_FN (cfun
, post_order_rev_cfg
[i
]);
1282 if (bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
) || bb
1283 == ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1285 if (process_bb_lives (bb
, curr_point
, dead_insn_p
))
1286 bb_live_change_p
= true;
1288 if (bb_live_change_p
)
1290 /* We need to clear pseudo live info as some pseudos can
1291 disappear, e.g. pseudos with used equivalences. */
1292 FOR_EACH_BB_FN (bb
, cfun
)
1294 bitmap_clear_range (df_get_live_in (bb
), FIRST_PSEUDO_REGISTER
,
1295 max_regno
- FIRST_PSEUDO_REGISTER
);
1296 bitmap_clear_range (df_get_live_out (bb
), FIRST_PSEUDO_REGISTER
,
1297 max_regno
- FIRST_PSEUDO_REGISTER
);
1299 /* As we did not change CFG since LRA start we can use
1300 DF-infrastructure solver to solve live data flow problem. */
1302 (DF_BACKWARD
, NULL
, live_con_fun_0
, live_con_fun_n
,
1303 live_trans_fun
, &all_blocks
,
1304 df_get_postorder (DF_BACKWARD
), df_get_n_blocks (DF_BACKWARD
));
1305 if (lra_dump_file
!= NULL
)
1307 fprintf (lra_dump_file
,
1308 "Global pseudo live data have been updated:\n");
1310 FOR_EACH_BB_FN (bb
, cfun
)
1312 bb_data_t bb_info
= get_bb_data (bb
);
1313 bitmap bb_livein
= df_get_live_in (bb
);
1314 bitmap bb_liveout
= df_get_live_out (bb
);
1316 fprintf (lra_dump_file
, "\nBB %d:\n", bb
->index
);
1317 lra_dump_bitmap_with_title (" gen:",
1318 &bb_info
->gen_pseudos
, bb
->index
);
1319 lra_dump_bitmap_with_title (" killed:",
1320 &bb_info
->killed_pseudos
, bb
->index
);
1321 lra_dump_bitmap_with_title (" livein:", bb_livein
, bb
->index
);
1322 lra_dump_bitmap_with_title (" liveout:", bb_liveout
, bb
->index
);
1326 free (post_order_rev_cfg
);
1327 lra_live_max_point
= curr_point
;
1328 if (lra_dump_file
!= NULL
)
1329 print_live_ranges (lra_dump_file
);
1331 sparseset_free (unused_set
);
1332 sparseset_free (dead_set
);
1333 sparseset_free (start_dying
);
1334 sparseset_free (start_living
);
1335 sparseset_free (pseudos_live_through_calls
);
1336 sparseset_free (pseudos_live_through_setjumps
);
1337 sparseset_free (pseudos_live
);
1338 compress_live_ranges ();
1339 timevar_pop (TV_LRA_CREATE_LIVE_RANGES
);
1340 return bb_live_change_p
;
1343 /* The main entry function creates live-ranges and other live info
1344 necessary for the assignment sub-pass. It uses
1345 lra_creates_live_ranges_1 -- so read comments for the
1348 lra_create_live_ranges (bool all_p
, bool dead_insn_p
)
1350 if (! lra_create_live_ranges_1 (all_p
, dead_insn_p
))
1352 if (lra_dump_file
!= NULL
)
1353 fprintf (lra_dump_file
, "Live info was changed -- recalculate it\n");
1354 /* Live info was changed on a bb border. It means that some info,
1355 e.g. about conflict regs, calls crossed, and live ranges may be
1356 wrong. We need this info for allocation. So recalculate it
1357 again but without removing dead insns which can change live info
1358 again. Repetitive live range calculations are expensive therefore
1359 we stop here as we already have correct info although some
1360 improvement in rare cases could be possible on this sub-pass if
1361 we do dead insn elimination again (still the improvement may
1363 lra_clear_live_ranges ();
1364 bool res
= lra_create_live_ranges_1 (all_p
, false);
1368 /* Finish all live ranges. */
1370 lra_clear_live_ranges (void)
1374 for (i
= 0; i
< max_reg_num (); i
++)
1375 free_live_range_list (lra_reg_info
[i
].live_ranges
);
1376 point_freq_vec
.release ();
1379 /* Initialize live ranges data once per function. */
1381 lra_live_ranges_init (void)
1383 live_range_pool
= create_alloc_pool ("live ranges",
1384 sizeof (struct lra_live_range
), 100);
1385 bitmap_initialize (&temp_bitmap
, ®_obstack
);
1386 initiate_live_solver ();
1389 /* Finish live ranges data once per function. */
1391 lra_live_ranges_finish (void)
1393 finish_live_solver ();
1394 bitmap_clear (&temp_bitmap
);
1395 free_alloc_pool (live_range_pool
);