1 /* Build live ranges for pseudos.
2 Copyright (C) 2010-2014 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 "dominance.h"
50 #include "basic-block.h"
54 #include "sparseset.h"
57 /* Program points are enumerated by numbers from range
58 0..LRA_LIVE_MAX_POINT-1. There are approximately two times more
59 program points than insns. Program points are places in the
60 program where liveness info can be changed. In most general case
61 (there are more complicated cases too) some program points
62 correspond to places where input operand dies and other ones
63 correspond to places where output operands are born. */
64 int lra_live_max_point
;
66 /* Accumulated execution frequency of all references for each hard
68 int lra_hard_reg_usage
[FIRST_PSEUDO_REGISTER
];
70 /* A global flag whose true value says to build live ranges for all
71 pseudos, otherwise the live ranges only for pseudos got memory is
72 build. True value means also building copies and setting up hard
73 register preferences. The complete info is necessary only for the
74 assignment pass. The complete info is not needed for the
75 coalescing and spill passes. */
76 static bool complete_info_p
;
78 /* Pseudos live at current point in the RTL scan. */
79 static sparseset pseudos_live
;
81 /* Pseudos probably living through calls and setjumps. As setjump is
82 a call too, if a bit in PSEUDOS_LIVE_THROUGH_SETJUMPS is set up
83 then the corresponding bit in PSEUDOS_LIVE_THROUGH_CALLS is set up
84 too. These data are necessary for cases when only one subreg of a
85 multi-reg pseudo is set up after a call. So we decide it is
86 probably live when traversing bb backward. We are sure about
87 living when we see its usage or definition of the pseudo. */
88 static sparseset pseudos_live_through_calls
;
89 static sparseset pseudos_live_through_setjumps
;
91 /* Set of hard regs (except eliminable ones) currently live. */
92 static HARD_REG_SET hard_regs_live
;
94 /* Set of pseudos and hard registers start living/dying in the current
95 insn. These sets are used to update REG_DEAD and REG_UNUSED notes
97 static sparseset start_living
, start_dying
;
99 /* Set of pseudos and hard regs dead and unused in the current
101 static sparseset unused_set
, dead_set
;
103 /* Bitmap used for holding intermediate bitmap operation results. */
104 static bitmap_head temp_bitmap
;
106 /* Pool for pseudo live ranges. */
107 static alloc_pool live_range_pool
;
109 /* Free live range LR. */
111 free_live_range (lra_live_range_t lr
)
113 pool_free (live_range_pool
, lr
);
116 /* Free live range list LR. */
118 free_live_range_list (lra_live_range_t lr
)
120 lra_live_range_t next
;
125 free_live_range (lr
);
130 /* Create and return pseudo live range with given attributes. */
131 static lra_live_range_t
132 create_live_range (int regno
, int start
, int finish
, lra_live_range_t next
)
136 p
= (lra_live_range_t
) pool_alloc (live_range_pool
);
144 /* Copy live range R and return the result. */
145 static lra_live_range_t
146 copy_live_range (lra_live_range_t r
)
150 p
= (lra_live_range_t
) pool_alloc (live_range_pool
);
155 /* Copy live range list given by its head R and return the result. */
157 lra_copy_live_range_list (lra_live_range_t r
)
159 lra_live_range_t p
, first
, *chain
;
162 for (chain
= &first
; r
!= NULL
; r
= r
->next
)
164 p
= copy_live_range (r
);
171 /* Merge *non-intersected* ranges R1 and R2 and returns the result.
172 The function maintains the order of ranges and tries to minimize
173 size of the result range list. Ranges R1 and R2 may not be used
176 lra_merge_live_ranges (lra_live_range_t r1
, lra_live_range_t r2
)
178 lra_live_range_t first
, last
, temp
;
184 for (first
= last
= NULL
; r1
!= NULL
&& r2
!= NULL
;)
186 if (r1
->start
< r2
->start
)
192 if (r1
->start
== r2
->finish
+ 1)
194 /* Joint ranges: merge r1 and r2 into r1. */
195 r1
->start
= r2
->start
;
198 pool_free (live_range_pool
, temp
);
202 gcc_assert (r2
->finish
+ 1 < r1
->start
);
203 /* Add r1 to the result. */
223 lra_assert (r2
!= NULL
);
232 /* Return TRUE if live ranges R1 and R2 intersect. */
234 lra_intersected_live_ranges_p (lra_live_range_t r1
, lra_live_range_t r2
)
236 /* Remember the live ranges are always kept ordered. */
237 while (r1
!= NULL
&& r2
!= NULL
)
239 if (r1
->start
> r2
->finish
)
241 else if (r2
->start
> r1
->finish
)
249 /* The function processing birth of hard register REGNO. It updates
250 living hard regs, conflict hard regs for living pseudos, and
253 make_hard_regno_born (int regno
)
257 lra_assert (regno
< FIRST_PSEUDO_REGISTER
);
258 if (TEST_HARD_REG_BIT (hard_regs_live
, regno
))
260 SET_HARD_REG_BIT (hard_regs_live
, regno
);
261 sparseset_set_bit (start_living
, regno
);
262 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live
, i
)
263 SET_HARD_REG_BIT (lra_reg_info
[i
].conflict_hard_regs
, regno
);
266 /* Process the death of hard register REGNO. This updates
267 hard_regs_live and START_DYING. */
269 make_hard_regno_dead (int regno
)
271 lra_assert (regno
< FIRST_PSEUDO_REGISTER
);
272 if (! TEST_HARD_REG_BIT (hard_regs_live
, regno
))
274 sparseset_set_bit (start_dying
, regno
);
275 CLEAR_HARD_REG_BIT (hard_regs_live
, regno
);
278 /* Mark pseudo REGNO as living at program point POINT, update conflicting
279 hard registers of the pseudo and START_LIVING, and start a new live
280 range for the pseudo corresponding to REGNO if it is necessary. */
282 mark_pseudo_live (int regno
, int point
)
286 lra_assert (regno
>= FIRST_PSEUDO_REGISTER
);
287 lra_assert (! sparseset_bit_p (pseudos_live
, regno
));
288 sparseset_set_bit (pseudos_live
, regno
);
289 IOR_HARD_REG_SET (lra_reg_info
[regno
].conflict_hard_regs
, hard_regs_live
);
291 if ((complete_info_p
|| lra_get_regno_hard_regno (regno
) < 0)
292 && ((p
= lra_reg_info
[regno
].live_ranges
) == NULL
293 || (p
->finish
!= point
&& p
->finish
+ 1 != point
)))
294 lra_reg_info
[regno
].live_ranges
295 = create_live_range (regno
, point
, -1, p
);
296 sparseset_set_bit (start_living
, regno
);
299 /* Mark pseudo REGNO as not living at program point POINT and update
301 This finishes the current live range for the pseudo corresponding
304 mark_pseudo_dead (int regno
, int point
)
308 lra_assert (regno
>= FIRST_PSEUDO_REGISTER
);
309 lra_assert (sparseset_bit_p (pseudos_live
, regno
));
310 sparseset_clear_bit (pseudos_live
, regno
);
311 sparseset_set_bit (start_dying
, regno
);
312 if (complete_info_p
|| lra_get_regno_hard_regno (regno
) < 0)
314 p
= lra_reg_info
[regno
].live_ranges
;
315 lra_assert (p
!= NULL
);
320 /* The corresponding bitmaps of BB currently being processed. */
321 static bitmap bb_killed_pseudos
, bb_gen_pseudos
;
323 /* Mark register REGNO (pseudo or hard register) in MODE as live at
324 program point POINT. Update BB_GEN_PSEUDOS.
325 Return TRUE if the liveness tracking sets were modified, or FALSE
326 if nothing changed. */
328 mark_regno_live (int regno
, machine_mode mode
, int point
)
331 bool changed
= false;
333 if (regno
< FIRST_PSEUDO_REGISTER
)
335 for (last
= regno
+ hard_regno_nregs
[regno
][mode
];
338 make_hard_regno_born (regno
);
342 if (! sparseset_bit_p (pseudos_live
, regno
))
344 mark_pseudo_live (regno
, point
);
347 bitmap_set_bit (bb_gen_pseudos
, regno
);
353 /* Mark register REGNO in MODE as dead at program point POINT. Update
354 BB_GEN_PSEUDOS and BB_KILLED_PSEUDOS. Return TRUE if the liveness
355 tracking sets were modified, or FALSE if nothing changed. */
357 mark_regno_dead (int regno
, machine_mode mode
, int point
)
360 bool changed
= false;
362 if (regno
< FIRST_PSEUDO_REGISTER
)
364 for (last
= regno
+ hard_regno_nregs
[regno
][mode
];
367 make_hard_regno_dead (regno
);
371 if (sparseset_bit_p (pseudos_live
, regno
))
373 mark_pseudo_dead (regno
, point
);
376 bitmap_clear_bit (bb_gen_pseudos
, regno
);
377 bitmap_set_bit (bb_killed_pseudos
, regno
);
384 /* This page contains code for making global live analysis of pseudos.
385 The code works only when pseudo live info is changed on a BB
386 border. That might be a consequence of some global transformations
387 in LRA, e.g. PIC pseudo reuse or rematerialization. */
389 /* Structure describing local BB data used for pseudo
391 struct bb_data_pseudos
393 /* Basic block about which the below data are. */
395 bitmap_head killed_pseudos
; /* pseudos killed in the BB. */
396 bitmap_head gen_pseudos
; /* pseudos generated in the BB. */
399 /* Array for all BB data. Indexed by the corresponding BB index. */
400 typedef struct bb_data_pseudos
*bb_data_t
;
402 /* All basic block data are referred through the following array. */
403 static bb_data_t bb_data
;
405 /* Two small functions for access to the bb data. */
406 static inline bb_data_t
407 get_bb_data (basic_block bb
)
409 return &bb_data
[(bb
)->index
];
412 static inline bb_data_t
413 get_bb_data_by_index (int index
)
415 return &bb_data
[index
];
418 /* Bitmap with all hard regs. */
419 static bitmap_head all_hard_regs_bitmap
;
421 /* The transfer function used by the DF equation solver to propagate
422 live info through block with BB_INDEX according to the following
425 bb.livein = (bb.liveout - bb.kill) OR bb.gen
428 live_trans_fun (int bb_index
)
430 basic_block bb
= get_bb_data_by_index (bb_index
)->bb
;
431 bitmap bb_liveout
= df_get_live_out (bb
);
432 bitmap bb_livein
= df_get_live_in (bb
);
433 bb_data_t bb_info
= get_bb_data (bb
);
435 bitmap_and_compl (&temp_bitmap
, bb_liveout
, &all_hard_regs_bitmap
);
436 return bitmap_ior_and_compl (bb_livein
, &bb_info
->gen_pseudos
,
437 &temp_bitmap
, &bb_info
->killed_pseudos
);
440 /* The confluence function used by the DF equation solver to set up
441 live info for a block BB without predecessor. */
443 live_con_fun_0 (basic_block bb
)
445 bitmap_and_into (df_get_live_out (bb
), &all_hard_regs_bitmap
);
448 /* The confluence function used by the DF equation solver to propagate
449 live info from successor to predecessor on edge E according to the
452 bb.liveout = 0 for entry block | OR (livein of successors)
455 live_con_fun_n (edge e
)
457 basic_block bb
= e
->src
;
458 basic_block dest
= e
->dest
;
459 bitmap bb_liveout
= df_get_live_out (bb
);
460 bitmap dest_livein
= df_get_live_in (dest
);
462 return bitmap_ior_and_compl_into (bb_liveout
,
463 dest_livein
, &all_hard_regs_bitmap
);
466 /* Indexes of all function blocks. */
467 static bitmap_head all_blocks
;
469 /* Allocate and initialize data needed for global pseudo live
472 initiate_live_solver (void)
474 bitmap_initialize (&all_hard_regs_bitmap
, ®_obstack
);
475 bitmap_set_range (&all_hard_regs_bitmap
, 0, FIRST_PSEUDO_REGISTER
);
476 bb_data
= XNEWVEC (struct bb_data_pseudos
, last_basic_block_for_fn (cfun
));
477 bitmap_initialize (&all_blocks
, ®_obstack
);
480 FOR_ALL_BB_FN (bb
, cfun
)
482 bb_data_t bb_info
= get_bb_data (bb
);
484 bitmap_initialize (&bb_info
->killed_pseudos
, ®_obstack
);
485 bitmap_initialize (&bb_info
->gen_pseudos
, ®_obstack
);
486 bitmap_set_bit (&all_blocks
, bb
->index
);
490 /* Free all data needed for global pseudo live analysis. */
492 finish_live_solver (void)
496 bitmap_clear (&all_blocks
);
497 FOR_ALL_BB_FN (bb
, cfun
)
499 bb_data_t bb_info
= get_bb_data (bb
);
500 bitmap_clear (&bb_info
->killed_pseudos
);
501 bitmap_clear (&bb_info
->gen_pseudos
);
504 bitmap_clear (&all_hard_regs_bitmap
);
509 /* Insn currently scanned. */
510 static rtx_insn
*curr_insn
;
512 static lra_insn_recog_data_t curr_id
;
513 /* The insn static data. */
514 static struct lra_static_insn_data
*curr_static_id
;
516 /* Return true when one of the predecessor edges of BB is marked with
517 EDGE_ABNORMAL_CALL or EDGE_EH. */
519 bb_has_abnormal_call_pred (basic_block bb
)
524 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
526 if (e
->flags
& (EDGE_ABNORMAL_CALL
| EDGE_EH
))
532 /* Vec containing execution frequencies of program points. */
533 static vec
<int> point_freq_vec
;
535 /* The start of the above vector elements. */
538 /* Increment the current program point POINT to the next point which has
539 execution frequency FREQ. */
541 next_program_point (int &point
, int freq
)
543 point_freq_vec
.safe_push (freq
);
544 lra_point_freq
= point_freq_vec
.address ();
548 /* Update the preference of HARD_REGNO for pseudo REGNO by PROFIT. */
550 lra_setup_reload_pseudo_preferenced_hard_reg (int regno
,
551 int hard_regno
, int profit
)
553 lra_assert (regno
>= lra_constraint_new_regno_start
);
554 if (lra_reg_info
[regno
].preferred_hard_regno1
== hard_regno
)
555 lra_reg_info
[regno
].preferred_hard_regno_profit1
+= profit
;
556 else if (lra_reg_info
[regno
].preferred_hard_regno2
== hard_regno
)
557 lra_reg_info
[regno
].preferred_hard_regno_profit2
+= profit
;
558 else if (lra_reg_info
[regno
].preferred_hard_regno1
< 0)
560 lra_reg_info
[regno
].preferred_hard_regno1
= hard_regno
;
561 lra_reg_info
[regno
].preferred_hard_regno_profit1
= profit
;
563 else if (lra_reg_info
[regno
].preferred_hard_regno2
< 0
564 || profit
> lra_reg_info
[regno
].preferred_hard_regno_profit2
)
566 lra_reg_info
[regno
].preferred_hard_regno2
= hard_regno
;
567 lra_reg_info
[regno
].preferred_hard_regno_profit2
= profit
;
571 /* Keep the 1st hard regno as more profitable. */
572 if (lra_reg_info
[regno
].preferred_hard_regno1
>= 0
573 && lra_reg_info
[regno
].preferred_hard_regno2
>= 0
574 && (lra_reg_info
[regno
].preferred_hard_regno_profit2
575 > lra_reg_info
[regno
].preferred_hard_regno_profit1
))
579 temp
= lra_reg_info
[regno
].preferred_hard_regno1
;
580 lra_reg_info
[regno
].preferred_hard_regno1
581 = lra_reg_info
[regno
].preferred_hard_regno2
;
582 lra_reg_info
[regno
].preferred_hard_regno2
= temp
;
583 temp
= lra_reg_info
[regno
].preferred_hard_regno_profit1
;
584 lra_reg_info
[regno
].preferred_hard_regno_profit1
585 = lra_reg_info
[regno
].preferred_hard_regno_profit2
;
586 lra_reg_info
[regno
].preferred_hard_regno_profit2
= temp
;
588 if (lra_dump_file
!= NULL
)
590 if ((hard_regno
= lra_reg_info
[regno
].preferred_hard_regno1
) >= 0)
591 fprintf (lra_dump_file
,
592 " Hard reg %d is preferable by r%d with profit %d\n",
594 lra_reg_info
[regno
].preferred_hard_regno_profit1
);
595 if ((hard_regno
= lra_reg_info
[regno
].preferred_hard_regno2
) >= 0)
596 fprintf (lra_dump_file
,
597 " Hard reg %d is preferable by r%d with profit %d\n",
599 lra_reg_info
[regno
].preferred_hard_regno_profit2
);
603 /* Check that REGNO living through calls and setjumps, set up conflict
604 regs, and clear corresponding bits in PSEUDOS_LIVE_THROUGH_CALLS and
605 PSEUDOS_LIVE_THROUGH_SETJUMPS. */
607 check_pseudos_live_through_calls (int regno
)
611 if (! sparseset_bit_p (pseudos_live_through_calls
, regno
))
613 sparseset_clear_bit (pseudos_live_through_calls
, regno
);
614 IOR_HARD_REG_SET (lra_reg_info
[regno
].conflict_hard_regs
,
617 for (hr
= 0; hr
< FIRST_PSEUDO_REGISTER
; hr
++)
618 if (HARD_REGNO_CALL_PART_CLOBBERED (hr
, PSEUDO_REGNO_MODE (regno
)))
619 SET_HARD_REG_BIT (lra_reg_info
[regno
].conflict_hard_regs
, hr
);
620 #ifdef ENABLE_CHECKING
621 lra_reg_info
[regno
].call_p
= true;
623 if (! sparseset_bit_p (pseudos_live_through_setjumps
, regno
))
625 sparseset_clear_bit (pseudos_live_through_setjumps
, regno
);
626 /* Don't allocate pseudos that cross setjmps or any call, if this
627 function receives a nonlocal goto. */
628 SET_HARD_REG_SET (lra_reg_info
[regno
].conflict_hard_regs
);
631 /* Process insns of the basic block BB to update pseudo live ranges,
632 pseudo hard register conflicts, and insn notes. We do it on
633 backward scan of BB insns. CURR_POINT is the program point where
634 BB ends. The function updates this counter and returns in
635 CURR_POINT the program point where BB starts. The function also
636 does local live info updates and can delete the dead insns if
637 DEAD_INSN_P. It returns true if pseudo live info was
638 changed at the BB start. */
640 process_bb_lives (basic_block bb
, int &curr_point
, bool dead_insn_p
)
649 bool need_curr_point_incr
;
651 reg_live_out
= df_get_live_out (bb
);
652 sparseset_clear (pseudos_live
);
653 sparseset_clear (pseudos_live_through_calls
);
654 sparseset_clear (pseudos_live_through_setjumps
);
655 REG_SET_TO_HARD_REG_SET (hard_regs_live
, reg_live_out
);
656 AND_COMPL_HARD_REG_SET (hard_regs_live
, eliminable_regset
);
657 EXECUTE_IF_SET_IN_BITMAP (reg_live_out
, FIRST_PSEUDO_REGISTER
, j
, bi
)
658 mark_pseudo_live (j
, curr_point
);
660 bb_gen_pseudos
= &get_bb_data (bb
)->gen_pseudos
;
661 bb_killed_pseudos
= &get_bb_data (bb
)->killed_pseudos
;
662 bitmap_clear (bb_gen_pseudos
);
663 bitmap_clear (bb_killed_pseudos
);
664 freq
= REG_FREQ_FROM_BB (bb
);
666 if (lra_dump_file
!= NULL
)
667 fprintf (lra_dump_file
, " BB %d\n", bb
->index
);
669 /* Scan the code of this basic block, noting which pseudos and hard
670 regs are born or die.
672 Note that this loop treats uninitialized values as live until the
673 beginning of the block. For example, if an instruction uses
674 (reg:DI foo), and only (subreg:SI (reg:DI foo) 0) is ever set,
675 FOO will remain live until the beginning of the block. Likewise
676 if FOO is not set at all. This is unnecessarily pessimistic, but
677 it probably doesn't matter much in practice. */
678 FOR_BB_INSNS_REVERSE_SAFE (bb
, curr_insn
, next
)
681 int dst_regno
, src_regno
;
683 struct lra_insn_reg
*reg
;
685 if (!NONDEBUG_INSN_P (curr_insn
))
688 curr_id
= lra_get_insn_recog_data (curr_insn
);
689 curr_static_id
= curr_id
->insn_static_data
;
690 if (lra_dump_file
!= NULL
)
691 fprintf (lra_dump_file
, " Insn %u: point = %d\n",
692 INSN_UID (curr_insn
), curr_point
);
694 set
= single_set (curr_insn
);
696 if (dead_insn_p
&& set
!= NULL_RTX
697 && REG_P (SET_DEST (set
)) && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
698 && find_reg_note (curr_insn
, REG_EH_REGION
, NULL_RTX
) == NULL_RTX
699 && ! may_trap_p (PATTERN (curr_insn
))
700 /* Don't do premature remove of pic offset pseudo as we can
701 start to use it after some reload generation. */
702 && (pic_offset_table_rtx
== NULL_RTX
703 || pic_offset_table_rtx
!= SET_DEST (set
)))
705 bool remove_p
= true;
707 for (reg
= curr_id
->regs
; reg
!= NULL
; reg
= reg
->next
)
708 if (reg
->type
!= OP_IN
&& sparseset_bit_p (pseudos_live
, reg
->regno
))
713 for (reg
= curr_static_id
->hard_regs
; reg
!= NULL
; reg
= reg
->next
)
714 if (reg
->type
!= OP_IN
)
719 if (remove_p
&& ! volatile_refs_p (PATTERN (curr_insn
)))
721 dst_regno
= REGNO (SET_DEST (set
));
722 if (lra_dump_file
!= NULL
)
723 fprintf (lra_dump_file
, " Deleting dead insn %u\n",
724 INSN_UID (curr_insn
));
725 lra_set_insn_deleted (curr_insn
);
726 if (lra_reg_info
[dst_regno
].nrefs
== 0)
728 /* There might be some debug insns with the pseudo. */
732 bitmap_copy (&temp_bitmap
, &lra_reg_info
[dst_regno
].insn_bitmap
);
733 EXECUTE_IF_SET_IN_BITMAP (&temp_bitmap
, 0, uid
, bi
)
735 insn
= lra_insn_recog_data
[uid
]->insn
;
736 lra_substitute_pseudo_within_insn (insn
, dst_regno
,
738 lra_update_insn_regno_info (insn
);
745 /* Update max ref width and hard reg usage. */
746 for (reg
= curr_id
->regs
; reg
!= NULL
; reg
= reg
->next
)
747 if (reg
->regno
>= FIRST_PSEUDO_REGISTER
748 && (GET_MODE_SIZE (reg
->biggest_mode
)
749 > GET_MODE_SIZE (lra_reg_info
[reg
->regno
].biggest_mode
)))
750 lra_reg_info
[reg
->regno
].biggest_mode
= reg
->biggest_mode
;
751 else if (reg
->regno
< FIRST_PSEUDO_REGISTER
)
752 lra_hard_reg_usage
[reg
->regno
] += freq
;
754 call_p
= CALL_P (curr_insn
);
757 && REG_P (SET_DEST (set
)) && REG_P (SET_SRC (set
))
758 /* Check that source regno does not conflict with
759 destination regno to exclude most impossible
761 && ((((src_regno
= REGNO (SET_SRC (set
))) >= FIRST_PSEUDO_REGISTER
762 && ! sparseset_bit_p (pseudos_live
, src_regno
))
763 || (src_regno
< FIRST_PSEUDO_REGISTER
764 && ! TEST_HARD_REG_BIT (hard_regs_live
, src_regno
)))
765 /* It might be 'inheritance pseudo <- reload pseudo'. */
766 || (src_regno
>= lra_constraint_new_regno_start
767 && ((int) REGNO (SET_DEST (set
))
768 >= lra_constraint_new_regno_start
)
769 /* Remember to skip special cases where src/dest regnos are
770 the same, e.g. insn SET pattern has matching constraints
772 && src_regno
!= (int) REGNO (SET_DEST (set
)))))
774 int hard_regno
= -1, regno
= -1;
776 dst_regno
= REGNO (SET_DEST (set
));
777 if (dst_regno
>= lra_constraint_new_regno_start
778 && src_regno
>= lra_constraint_new_regno_start
)
779 lra_create_copy (dst_regno
, src_regno
, freq
);
780 else if (dst_regno
>= lra_constraint_new_regno_start
)
782 if ((hard_regno
= src_regno
) >= FIRST_PSEUDO_REGISTER
)
783 hard_regno
= reg_renumber
[src_regno
];
786 else if (src_regno
>= lra_constraint_new_regno_start
)
788 if ((hard_regno
= dst_regno
) >= FIRST_PSEUDO_REGISTER
)
789 hard_regno
= reg_renumber
[dst_regno
];
792 if (regno
>= 0 && hard_regno
>= 0)
793 lra_setup_reload_pseudo_preferenced_hard_reg
794 (regno
, hard_regno
, freq
);
797 sparseset_clear (start_living
);
799 /* Try to avoid unnecessary program point increments, this saves
800 a lot of time in remove_some_program_points_and_update_live_ranges.
801 We only need an increment if something becomes live or dies at this
803 need_curr_point_incr
= false;
805 /* Mark each defined value as live. We need to do this for
806 unused values because they still conflict with quantities
807 that are live at the time of the definition. */
808 for (reg
= curr_id
->regs
; reg
!= NULL
; reg
= reg
->next
)
809 if (reg
->type
!= OP_IN
)
812 |= mark_regno_live (reg
->regno
, reg
->biggest_mode
,
814 check_pseudos_live_through_calls (reg
->regno
);
817 for (reg
= curr_static_id
->hard_regs
; reg
!= NULL
; reg
= reg
->next
)
818 if (reg
->type
!= OP_IN
)
819 make_hard_regno_born (reg
->regno
);
821 sparseset_copy (unused_set
, start_living
);
823 sparseset_clear (start_dying
);
825 /* See which defined values die here. */
826 for (reg
= curr_id
->regs
; reg
!= NULL
; reg
= reg
->next
)
827 if (reg
->type
== OP_OUT
&& ! reg
->early_clobber
&& ! reg
->subreg_p
)
829 |= mark_regno_dead (reg
->regno
, reg
->biggest_mode
,
832 for (reg
= curr_static_id
->hard_regs
; reg
!= NULL
; reg
= reg
->next
)
833 if (reg
->type
== OP_OUT
&& ! reg
->early_clobber
&& ! reg
->subreg_p
)
834 make_hard_regno_dead (reg
->regno
);
840 HARD_REG_SET this_call_used_reg_set
;
841 get_call_reg_set_usage (curr_insn
, &this_call_used_reg_set
,
844 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live
, j
)
845 IOR_HARD_REG_SET (lra_reg_info
[j
].actual_call_used_reg_set
,
846 this_call_used_reg_set
);
849 sparseset_ior (pseudos_live_through_calls
,
850 pseudos_live_through_calls
, pseudos_live
);
851 if (cfun
->has_nonlocal_label
852 || find_reg_note (curr_insn
, REG_SETJMP
,
853 NULL_RTX
) != NULL_RTX
)
854 sparseset_ior (pseudos_live_through_setjumps
,
855 pseudos_live_through_setjumps
, pseudos_live
);
858 /* Increment the current program point if we must. */
859 if (need_curr_point_incr
)
860 next_program_point (curr_point
, freq
);
862 sparseset_clear (start_living
);
864 need_curr_point_incr
= false;
866 /* Mark each used value as live. */
867 for (reg
= curr_id
->regs
; reg
!= NULL
; reg
= reg
->next
)
868 if (reg
->type
== OP_IN
)
871 |= mark_regno_live (reg
->regno
, reg
->biggest_mode
,
873 check_pseudos_live_through_calls (reg
->regno
);
876 for (reg
= curr_static_id
->hard_regs
; reg
!= NULL
; reg
= reg
->next
)
877 if (reg
->type
== OP_IN
)
878 make_hard_regno_born (reg
->regno
);
880 if (curr_id
->arg_hard_regs
!= NULL
)
881 /* Make argument hard registers live. */
882 for (i
= 0; (regno
= curr_id
->arg_hard_regs
[i
]) >= 0; i
++)
883 make_hard_regno_born (regno
);
885 sparseset_and_compl (dead_set
, start_living
, start_dying
);
887 /* Mark early clobber outputs dead. */
888 for (reg
= curr_id
->regs
; reg
!= NULL
; reg
= reg
->next
)
889 if (reg
->type
== OP_OUT
&& reg
->early_clobber
&& ! reg
->subreg_p
)
891 |= mark_regno_dead (reg
->regno
, reg
->biggest_mode
,
894 for (reg
= curr_static_id
->hard_regs
; reg
!= NULL
; reg
= reg
->next
)
895 if (reg
->type
== OP_OUT
&& reg
->early_clobber
&& ! reg
->subreg_p
)
896 make_hard_regno_dead (reg
->regno
);
898 if (need_curr_point_incr
)
899 next_program_point (curr_point
, freq
);
902 for (link_loc
= ®_NOTES (curr_insn
); (link
= *link_loc
) != NULL_RTX
;)
904 if (REG_NOTE_KIND (link
) != REG_DEAD
905 && REG_NOTE_KIND (link
) != REG_UNUSED
)
907 else if (REG_P (XEXP (link
, 0)))
909 regno
= REGNO (XEXP (link
, 0));
910 if ((REG_NOTE_KIND (link
) == REG_DEAD
911 && ! sparseset_bit_p (dead_set
, regno
))
912 || (REG_NOTE_KIND (link
) == REG_UNUSED
913 && ! sparseset_bit_p (unused_set
, regno
)))
915 *link_loc
= XEXP (link
, 1);
918 if (REG_NOTE_KIND (link
) == REG_DEAD
)
919 sparseset_clear_bit (dead_set
, regno
);
920 else if (REG_NOTE_KIND (link
) == REG_UNUSED
)
921 sparseset_clear_bit (unused_set
, regno
);
923 link_loc
= &XEXP (link
, 1);
925 EXECUTE_IF_SET_IN_SPARSESET (dead_set
, j
)
926 add_reg_note (curr_insn
, REG_DEAD
, regno_reg_rtx
[j
]);
927 EXECUTE_IF_SET_IN_SPARSESET (unused_set
, j
)
928 add_reg_note (curr_insn
, REG_UNUSED
, regno_reg_rtx
[j
]);
931 #ifdef EH_RETURN_DATA_REGNO
932 if (bb_has_eh_pred (bb
))
935 unsigned int regno
= EH_RETURN_DATA_REGNO (j
);
937 if (regno
== INVALID_REGNUM
)
939 make_hard_regno_born (regno
);
943 /* Pseudos can't go in stack regs at the start of a basic block that
944 is reached by an abnormal edge. Likewise for call clobbered regs,
945 because caller-save, fixup_abnormal_edges and possibly the table
946 driven EH machinery are not quite ready to handle such pseudos
947 live across such edges. */
948 if (bb_has_abnormal_pred (bb
))
951 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live
, px
)
952 lra_reg_info
[px
].no_stack_p
= true;
953 for (px
= FIRST_STACK_REG
; px
<= LAST_STACK_REG
; px
++)
954 make_hard_regno_born (px
);
956 /* No need to record conflicts for call clobbered regs if we
957 have nonlocal labels around, as we don't ever try to
958 allocate such regs in this case. */
959 if (!cfun
->has_nonlocal_label
&& bb_has_abnormal_call_pred (bb
))
960 for (px
= 0; px
< FIRST_PSEUDO_REGISTER
; px
++)
961 if (call_used_regs
[px
])
962 make_hard_regno_born (px
);
965 bool live_change_p
= false;
966 /* Check if bb border live info was changed. */
967 unsigned int live_pseudos_num
= 0;
968 EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb
),
969 FIRST_PSEUDO_REGISTER
, j
, bi
)
972 if (! sparseset_bit_p (pseudos_live
, j
))
974 live_change_p
= true;
975 if (lra_dump_file
!= NULL
)
976 fprintf (lra_dump_file
,
977 " r%d is removed as live at bb%d start\n", j
, bb
->index
);
982 && sparseset_cardinality (pseudos_live
) != live_pseudos_num
)
984 live_change_p
= true;
985 if (lra_dump_file
!= NULL
)
986 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live
, j
)
987 if (! bitmap_bit_p (df_get_live_in (bb
), j
))
988 fprintf (lra_dump_file
,
989 " r%d is added to live at bb%d start\n", j
, bb
->index
);
991 /* See if we'll need an increment at the end of this basic block.
992 An increment is needed if the PSEUDOS_LIVE set is not empty,
993 to make sure the finish points are set up correctly. */
994 need_curr_point_incr
= (sparseset_cardinality (pseudos_live
) > 0);
996 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live
, i
)
997 mark_pseudo_dead (i
, curr_point
);
999 EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb
), FIRST_PSEUDO_REGISTER
, j
, bi
)
1001 if (sparseset_cardinality (pseudos_live_through_calls
) == 0)
1003 if (sparseset_bit_p (pseudos_live_through_calls
, j
))
1004 check_pseudos_live_through_calls (j
);
1007 if (need_curr_point_incr
)
1008 next_program_point (curr_point
, freq
);
1010 return live_change_p
;
1013 /* Compress pseudo live ranges by removing program points where
1014 nothing happens. Complexity of many algorithms in LRA is linear
1015 function of program points number. To speed up the code we try to
1016 minimize the number of the program points here. */
1018 remove_some_program_points_and_update_live_ranges (void)
1023 lra_live_range_t r
, prev_r
, next_r
;
1024 sbitmap born_or_dead
, born
, dead
;
1025 sbitmap_iterator sbi
;
1026 bool born_p
, dead_p
, prev_born_p
, prev_dead_p
;
1028 born
= sbitmap_alloc (lra_live_max_point
);
1029 dead
= sbitmap_alloc (lra_live_max_point
);
1030 bitmap_clear (born
);
1031 bitmap_clear (dead
);
1032 max_regno
= max_reg_num ();
1033 for (i
= FIRST_PSEUDO_REGISTER
; i
< (unsigned) max_regno
; i
++)
1035 for (r
= lra_reg_info
[i
].live_ranges
; r
!= NULL
; r
= r
->next
)
1037 lra_assert (r
->start
<= r
->finish
);
1038 bitmap_set_bit (born
, r
->start
);
1039 bitmap_set_bit (dead
, r
->finish
);
1042 born_or_dead
= sbitmap_alloc (lra_live_max_point
);
1043 bitmap_ior (born_or_dead
, born
, dead
);
1044 map
= XCNEWVEC (int, lra_live_max_point
);
1046 prev_born_p
= prev_dead_p
= false;
1047 EXECUTE_IF_SET_IN_BITMAP (born_or_dead
, 0, i
, sbi
)
1049 born_p
= bitmap_bit_p (born
, i
);
1050 dead_p
= bitmap_bit_p (dead
, i
);
1051 if ((prev_born_p
&& ! prev_dead_p
&& born_p
&& ! dead_p
)
1052 || (prev_dead_p
&& ! prev_born_p
&& dead_p
&& ! born_p
))
1055 lra_point_freq
[n
] = MAX (lra_point_freq
[n
], lra_point_freq
[i
]);
1060 lra_point_freq
[n
] = lra_point_freq
[i
];
1062 prev_born_p
= born_p
;
1063 prev_dead_p
= dead_p
;
1065 sbitmap_free (born_or_dead
);
1066 sbitmap_free (born
);
1067 sbitmap_free (dead
);
1069 if (lra_dump_file
!= NULL
)
1070 fprintf (lra_dump_file
, "Compressing live ranges: from %d to %d - %d%%\n",
1071 lra_live_max_point
, n
, 100 * n
/ lra_live_max_point
);
1072 if (n
< lra_live_max_point
)
1074 lra_live_max_point
= n
;
1075 for (i
= FIRST_PSEUDO_REGISTER
; i
< (unsigned) max_regno
; i
++)
1077 for (prev_r
= NULL
, r
= lra_reg_info
[i
].live_ranges
;
1082 r
->start
= map
[r
->start
];
1083 r
->finish
= map
[r
->finish
];
1084 if (prev_r
== NULL
|| prev_r
->start
> r
->finish
+ 1)
1089 prev_r
->start
= r
->start
;
1090 prev_r
->next
= next_r
;
1091 free_live_range (r
);
1098 /* Print live ranges R to file F. */
1100 lra_print_live_range_list (FILE *f
, lra_live_range_t r
)
1102 for (; r
!= NULL
; r
= r
->next
)
1103 fprintf (f
, " [%d..%d]", r
->start
, r
->finish
);
1108 debug (lra_live_range
&ref
)
1110 lra_print_live_range_list (stderr
, &ref
);
1114 debug (lra_live_range
*ptr
)
1119 fprintf (stderr
, "<nil>\n");
1122 /* Print live ranges R to stderr. */
1124 lra_debug_live_range_list (lra_live_range_t r
)
1126 lra_print_live_range_list (stderr
, r
);
1129 /* Print live ranges of pseudo REGNO to file F. */
1131 print_pseudo_live_ranges (FILE *f
, int regno
)
1133 if (lra_reg_info
[regno
].live_ranges
== NULL
)
1135 fprintf (f
, " r%d:", regno
);
1136 lra_print_live_range_list (f
, lra_reg_info
[regno
].live_ranges
);
1139 /* Print live ranges of pseudo REGNO to stderr. */
1141 lra_debug_pseudo_live_ranges (int regno
)
1143 print_pseudo_live_ranges (stderr
, regno
);
1146 /* Print live ranges of all pseudos to file F. */
1148 print_live_ranges (FILE *f
)
1152 max_regno
= max_reg_num ();
1153 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
1154 print_pseudo_live_ranges (f
, i
);
1157 /* Print live ranges of all pseudos to stderr. */
1159 lra_debug_live_ranges (void)
1161 print_live_ranges (stderr
);
1164 /* Compress pseudo live ranges. */
1166 compress_live_ranges (void)
1168 remove_some_program_points_and_update_live_ranges ();
1169 if (lra_dump_file
!= NULL
)
1171 fprintf (lra_dump_file
, "Ranges after the compression:\n");
1172 print_live_ranges (lra_dump_file
);
1178 /* The number of the current live range pass. */
1179 int lra_live_range_iter
;
1181 /* The function creates live ranges only for memory pseudos (or for
1182 all ones if ALL_P), set up CONFLICT_HARD_REGS for the pseudos. It
1183 also does dead insn elimination if DEAD_INSN_P and global live
1184 analysis only for pseudos and only if the pseudo live info was
1185 changed on a BB border. Return TRUE if the live info was
1188 lra_create_live_ranges_1 (bool all_p
, bool dead_insn_p
)
1191 int i
, hard_regno
, max_regno
= max_reg_num ();
1193 bool bb_live_change_p
, have_referenced_pseudos
= false;
1195 timevar_push (TV_LRA_CREATE_LIVE_RANGES
);
1197 complete_info_p
= all_p
;
1198 if (lra_dump_file
!= NULL
)
1199 fprintf (lra_dump_file
,
1200 "\n********** Pseudo live ranges #%d: **********\n\n",
1201 ++lra_live_range_iter
);
1202 memset (lra_hard_reg_usage
, 0, sizeof (lra_hard_reg_usage
));
1203 for (i
= 0; i
< max_regno
; i
++)
1205 lra_reg_info
[i
].live_ranges
= NULL
;
1206 CLEAR_HARD_REG_SET (lra_reg_info
[i
].conflict_hard_regs
);
1207 lra_reg_info
[i
].preferred_hard_regno1
= -1;
1208 lra_reg_info
[i
].preferred_hard_regno2
= -1;
1209 lra_reg_info
[i
].preferred_hard_regno_profit1
= 0;
1210 lra_reg_info
[i
].preferred_hard_regno_profit2
= 0;
1212 lra_reg_info
[i
].no_stack_p
= false;
1214 /* The biggest mode is already set but its value might be to
1215 conservative because of recent transformation. Here in this
1216 file we recalculate it again as it costs practically
1218 if (regno_reg_rtx
[i
] != NULL_RTX
)
1219 lra_reg_info
[i
].biggest_mode
= GET_MODE (regno_reg_rtx
[i
]);
1221 lra_reg_info
[i
].biggest_mode
= VOIDmode
;
1222 #ifdef ENABLE_CHECKING
1223 lra_reg_info
[i
].call_p
= false;
1225 if (i
>= FIRST_PSEUDO_REGISTER
1226 && lra_reg_info
[i
].nrefs
!= 0)
1228 if ((hard_regno
= reg_renumber
[i
]) >= 0)
1229 lra_hard_reg_usage
[hard_regno
] += lra_reg_info
[i
].freq
;
1230 have_referenced_pseudos
= true;
1235 /* Under some circumstances, we can have functions without pseudo
1236 registers. For such functions, lra_live_max_point will be 0,
1237 see e.g. PR55604, and there's nothing more to do for us here. */
1238 if (! have_referenced_pseudos
)
1240 timevar_pop (TV_LRA_CREATE_LIVE_RANGES
);
1244 pseudos_live
= sparseset_alloc (max_regno
);
1245 pseudos_live_through_calls
= sparseset_alloc (max_regno
);
1246 pseudos_live_through_setjumps
= sparseset_alloc (max_regno
);
1247 start_living
= sparseset_alloc (max_regno
);
1248 start_dying
= sparseset_alloc (max_regno
);
1249 dead_set
= sparseset_alloc (max_regno
);
1250 unused_set
= sparseset_alloc (max_regno
);
1252 point_freq_vec
.create (get_max_uid () * 2);
1253 lra_point_freq
= point_freq_vec
.address ();
1254 int *post_order_rev_cfg
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
1255 int n_blocks_inverted
= inverted_post_order_compute (post_order_rev_cfg
);
1256 lra_assert (n_blocks_inverted
== n_basic_blocks_for_fn (cfun
));
1257 bb_live_change_p
= false;
1258 for (i
= n_blocks_inverted
- 1; i
>= 0; --i
)
1260 bb
= BASIC_BLOCK_FOR_FN (cfun
, post_order_rev_cfg
[i
]);
1261 if (bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
) || bb
1262 == ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1264 if (process_bb_lives (bb
, curr_point
, dead_insn_p
))
1265 bb_live_change_p
= true;
1267 if (bb_live_change_p
)
1269 /* We need to clear pseudo live info as some pseudos can
1270 disappear, e.g. pseudos with used equivalences. */
1271 FOR_EACH_BB_FN (bb
, cfun
)
1273 bitmap_clear_range (df_get_live_in (bb
), FIRST_PSEUDO_REGISTER
,
1274 max_regno
- FIRST_PSEUDO_REGISTER
);
1275 bitmap_clear_range (df_get_live_out (bb
), FIRST_PSEUDO_REGISTER
,
1276 max_regno
- FIRST_PSEUDO_REGISTER
);
1278 /* As we did not change CFG since LRA start we can use
1279 DF-infrastructure solver to solve live data flow problem. */
1281 (DF_BACKWARD
, NULL
, live_con_fun_0
, live_con_fun_n
,
1282 live_trans_fun
, &all_blocks
,
1283 df_get_postorder (DF_BACKWARD
), df_get_n_blocks (DF_BACKWARD
));
1284 if (lra_dump_file
!= NULL
)
1286 fprintf (lra_dump_file
,
1287 "Global pseudo live data have been updated:\n");
1289 FOR_EACH_BB_FN (bb
, cfun
)
1291 bb_data_t bb_info
= get_bb_data (bb
);
1292 bitmap bb_livein
= df_get_live_in (bb
);
1293 bitmap bb_liveout
= df_get_live_out (bb
);
1295 fprintf (lra_dump_file
, "\nBB %d:\n", bb
->index
);
1296 lra_dump_bitmap_with_title (" gen:",
1297 &bb_info
->gen_pseudos
, bb
->index
);
1298 lra_dump_bitmap_with_title (" killed:",
1299 &bb_info
->killed_pseudos
, bb
->index
);
1300 lra_dump_bitmap_with_title (" livein:", bb_livein
, bb
->index
);
1301 lra_dump_bitmap_with_title (" liveout:", bb_liveout
, bb
->index
);
1305 free (post_order_rev_cfg
);
1306 lra_live_max_point
= curr_point
;
1307 if (lra_dump_file
!= NULL
)
1308 print_live_ranges (lra_dump_file
);
1310 sparseset_free (unused_set
);
1311 sparseset_free (dead_set
);
1312 sparseset_free (start_dying
);
1313 sparseset_free (start_living
);
1314 sparseset_free (pseudos_live_through_calls
);
1315 sparseset_free (pseudos_live_through_setjumps
);
1316 sparseset_free (pseudos_live
);
1317 compress_live_ranges ();
1318 timevar_pop (TV_LRA_CREATE_LIVE_RANGES
);
1319 return bb_live_change_p
;
1322 /* The main entry function creates live-ranges and other live info
1323 necessary for the assignment sub-pass. It uses
1324 lra_creates_live_ranges_1 -- so read comments for the
1327 lra_create_live_ranges (bool all_p
, bool dead_insn_p
)
1329 if (! lra_create_live_ranges_1 (all_p
, dead_insn_p
))
1331 if (lra_dump_file
!= NULL
)
1332 fprintf (lra_dump_file
, "Live info was changed -- recalculate it\n");
1333 /* Live info was changed on a bb border. It means that some info,
1334 e.g. about conflict regs, calls crossed, and live ranges may be
1335 wrong. We need this info for allocation. So recalculate it
1336 again but without removing dead insns which can change live info
1337 again. Repetitive live range calculations are expensive therefore
1338 we stop here as we already have correct info although some
1339 improvement in rare cases could be possible on this sub-pass if
1340 we do dead insn elimination again (still the improvement may
1342 lra_clear_live_ranges ();
1343 bool res
= lra_create_live_ranges_1 (all_p
, false);
1347 /* Finish all live ranges. */
1349 lra_clear_live_ranges (void)
1353 for (i
= 0; i
< max_reg_num (); i
++)
1354 free_live_range_list (lra_reg_info
[i
].live_ranges
);
1355 point_freq_vec
.release ();
1358 /* Initialize live ranges data once per function. */
1360 lra_live_ranges_init (void)
1362 live_range_pool
= create_alloc_pool ("live ranges",
1363 sizeof (struct lra_live_range
), 100);
1364 bitmap_initialize (&temp_bitmap
, ®_obstack
);
1365 initiate_live_solver ();
1368 /* Finish live ranges data once per function. */
1370 lra_live_ranges_finish (void)
1372 finish_live_solver ();
1373 bitmap_clear (&temp_bitmap
);
1374 free_alloc_pool (live_range_pool
);