]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lra-lives.c
gcc/ada/
[thirdparty/gcc.git] / gcc / lra-lives.c
CommitLineData
c6a6cdaa 1/* Build live ranges for pseudos.
3aea1f79 2 Copyright (C) 2010-2014 Free Software Foundation, Inc.
c6a6cdaa 3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along 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"
a3020f2f 39#include "hashtab.h"
40#include "hash-set.h"
41#include "vec.h"
42#include "machmode.h"
43#include "input.h"
c6a6cdaa 44#include "function.h"
45#include "expr.h"
94ea8568 46#include "predict.h"
47#include "dominance.h"
48#include "cfg.h"
49#include "cfganal.h"
c6a6cdaa 50#include "basic-block.h"
51#include "except.h"
52#include "df.h"
53#include "ira.h"
54#include "sparseset.h"
55#include "lra-int.h"
56
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. */
64int lra_live_max_point;
65
66/* Accumulated execution frequency of all references for each hard
67 register. */
68int lra_hard_reg_usage[FIRST_PSEUDO_REGISTER];
69
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. */
76static bool complete_info_p;
77
78/* Pseudos live at current point in the RTL scan. */
79static sparseset pseudos_live;
80
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. */
88static sparseset pseudos_live_through_calls;
89static sparseset pseudos_live_through_setjumps;
90
91/* Set of hard regs (except eliminable ones) currently live. */
92static HARD_REG_SET hard_regs_live;
93
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
96 in the insn. */
97static sparseset start_living, start_dying;
98
99/* Set of pseudos and hard regs dead and unused in the current
100 insn. */
101static sparseset unused_set, dead_set;
102
103/* Pool for pseudo live ranges. */
104static alloc_pool live_range_pool;
105
106/* Free live range LR. */
107static void
108free_live_range (lra_live_range_t lr)
109{
110 pool_free (live_range_pool, lr);
111}
112
113/* Free live range list LR. */
114static void
115free_live_range_list (lra_live_range_t lr)
116{
117 lra_live_range_t next;
118
119 while (lr != NULL)
120 {
121 next = lr->next;
122 free_live_range (lr);
123 lr = next;
124 }
125}
126
127/* Create and return pseudo live range with given attributes. */
128static lra_live_range_t
129create_live_range (int regno, int start, int finish, lra_live_range_t next)
130{
131 lra_live_range_t p;
132
133 p = (lra_live_range_t) pool_alloc (live_range_pool);
134 p->regno = regno;
135 p->start = start;
136 p->finish = finish;
137 p->next = next;
138 return p;
139}
140
141/* Copy live range R and return the result. */
142static lra_live_range_t
143copy_live_range (lra_live_range_t r)
144{
145 lra_live_range_t p;
146
147 p = (lra_live_range_t) pool_alloc (live_range_pool);
148 *p = *r;
149 return p;
150}
151
152/* Copy live range list given by its head R and return the result. */
153lra_live_range_t
154lra_copy_live_range_list (lra_live_range_t r)
155{
156 lra_live_range_t p, first, *chain;
157
158 first = NULL;
159 for (chain = &first; r != NULL; r = r->next)
160 {
161 p = copy_live_range (r);
162 *chain = p;
163 chain = &p->next;
164 }
165 return first;
166}
167
168/* Merge *non-intersected* ranges R1 and R2 and returns the result.
169 The function maintains the order of ranges and tries to minimize
170 size of the result range list. Ranges R1 and R2 may not be used
171 after the call. */
172lra_live_range_t
173lra_merge_live_ranges (lra_live_range_t r1, lra_live_range_t r2)
174{
175 lra_live_range_t first, last, temp;
176
177 if (r1 == NULL)
178 return r2;
179 if (r2 == NULL)
180 return r1;
181 for (first = last = NULL; r1 != NULL && r2 != NULL;)
182 {
183 if (r1->start < r2->start)
184 {
185 temp = r1;
186 r1 = r2;
187 r2 = temp;
188 }
189 if (r1->start == r2->finish + 1)
190 {
191 /* Joint ranges: merge r1 and r2 into r1. */
192 r1->start = r2->start;
193 temp = r2;
194 r2 = r2->next;
195 pool_free (live_range_pool, temp);
196 }
197 else
198 {
199 gcc_assert (r2->finish + 1 < r1->start);
200 /* Add r1 to the result. */
201 if (first == NULL)
202 first = last = r1;
203 else
204 {
205 last->next = r1;
206 last = r1;
207 }
208 r1 = r1->next;
209 }
210 }
211 if (r1 != NULL)
212 {
213 if (first == NULL)
214 first = r1;
215 else
216 last->next = r1;
217 }
218 else
219 {
220 lra_assert (r2 != NULL);
221 if (first == NULL)
222 first = r2;
223 else
224 last->next = r2;
225 }
226 return first;
227}
228
229/* Return TRUE if live ranges R1 and R2 intersect. */
230bool
231lra_intersected_live_ranges_p (lra_live_range_t r1, lra_live_range_t r2)
232{
233 /* Remember the live ranges are always kept ordered. */
234 while (r1 != NULL && r2 != NULL)
235 {
236 if (r1->start > r2->finish)
237 r1 = r1->next;
238 else if (r2->start > r1->finish)
239 r2 = r2->next;
240 else
241 return true;
242 }
243 return false;
244}
245
246/* The function processing birth of hard register REGNO. It updates
247 living hard regs, conflict hard regs for living pseudos, and
248 START_LIVING. */
249static void
250make_hard_regno_born (int regno)
251{
252 unsigned int i;
253
254 lra_assert (regno < FIRST_PSEUDO_REGISTER);
255 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, regno)
256 || TEST_HARD_REG_BIT (hard_regs_live, regno))
257 return;
258 SET_HARD_REG_BIT (hard_regs_live, regno);
259 sparseset_set_bit (start_living, regno);
260 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, i)
261 SET_HARD_REG_BIT (lra_reg_info[i].conflict_hard_regs, regno);
262}
263
264/* Process the death of hard register REGNO. This updates
265 hard_regs_live and START_DYING. */
266static void
267make_hard_regno_dead (int regno)
268{
269 lra_assert (regno < FIRST_PSEUDO_REGISTER);
270 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, regno)
271 || ! TEST_HARD_REG_BIT (hard_regs_live, regno))
272 return;
273 sparseset_set_bit (start_dying, regno);
274 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
275}
276
277/* Mark pseudo REGNO as living at program point POINT, update conflicting
278 hard registers of the pseudo and START_LIVING, and start a new live
279 range for the pseudo corresponding to REGNO if it is necessary. */
280static void
281mark_pseudo_live (int regno, int point)
282{
283 lra_live_range_t p;
284
285 lra_assert (regno >= FIRST_PSEUDO_REGISTER);
286 lra_assert (! sparseset_bit_p (pseudos_live, regno));
287 sparseset_set_bit (pseudos_live, regno);
288 IOR_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs, hard_regs_live);
289
290 if ((complete_info_p || lra_get_regno_hard_regno (regno) < 0)
291 && ((p = lra_reg_info[regno].live_ranges) == NULL
292 || (p->finish != point && p->finish + 1 != point)))
293 lra_reg_info[regno].live_ranges
294 = create_live_range (regno, point, -1, p);
295 sparseset_set_bit (start_living, regno);
296}
297
298/* Mark pseudo REGNO as not living at program point POINT and update
299 START_DYING.
300 This finishes the current live range for the pseudo corresponding
301 to REGNO. */
302static void
303mark_pseudo_dead (int regno, int point)
304{
305 lra_live_range_t p;
306
307 lra_assert (regno >= FIRST_PSEUDO_REGISTER);
308 lra_assert (sparseset_bit_p (pseudos_live, regno));
309 sparseset_clear_bit (pseudos_live, regno);
310 sparseset_set_bit (start_dying, regno);
311 if (complete_info_p || lra_get_regno_hard_regno (regno) < 0)
312 {
313 p = lra_reg_info[regno].live_ranges;
314 lra_assert (p != NULL);
315 p->finish = point;
316 }
317}
318
319/* Mark register REGNO (pseudo or hard register) in MODE as live
320 at program point POINT.
321 Return TRUE if the liveness tracking sets were modified,
322 or FALSE if nothing changed. */
323static bool
3754d046 324mark_regno_live (int regno, machine_mode mode, int point)
c6a6cdaa 325{
326 int last;
327 bool changed = false;
328
329 if (regno < FIRST_PSEUDO_REGISTER)
330 {
331 for (last = regno + hard_regno_nregs[regno][mode];
332 regno < last;
333 regno++)
334 make_hard_regno_born (regno);
335 }
336 else if (! sparseset_bit_p (pseudos_live, regno))
337 {
338 mark_pseudo_live (regno, point);
339 changed = true;
340 }
341 return changed;
342}
343
344
345/* Mark register REGNO in MODE as dead at program point POINT.
346 Return TRUE if the liveness tracking sets were modified,
347 or FALSE if nothing changed. */
348static bool
3754d046 349mark_regno_dead (int regno, machine_mode mode, int point)
c6a6cdaa 350{
351 int last;
352 bool changed = false;
353
354 if (regno < FIRST_PSEUDO_REGISTER)
355 {
356 for (last = regno + hard_regno_nregs[regno][mode];
357 regno < last;
358 regno++)
359 make_hard_regno_dead (regno);
360 }
361 else if (sparseset_bit_p (pseudos_live, regno))
362 {
363 mark_pseudo_dead (regno, point);
364 changed = true;
365 }
366 return changed;
367}
368
369/* Insn currently scanned. */
7f836b57 370static rtx_insn *curr_insn;
c6a6cdaa 371/* The insn data. */
372static lra_insn_recog_data_t curr_id;
373/* The insn static data. */
374static struct lra_static_insn_data *curr_static_id;
375
376/* Return true when one of the predecessor edges of BB is marked with
377 EDGE_ABNORMAL_CALL or EDGE_EH. */
378static bool
379bb_has_abnormal_call_pred (basic_block bb)
380{
381 edge e;
382 edge_iterator ei;
383
384 FOR_EACH_EDGE (e, ei, bb->preds)
385 {
386 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
387 return true;
388 }
389 return false;
390}
391
392/* Vec containing execution frequencies of program points. */
f1f41a6c 393static vec<int> point_freq_vec;
c6a6cdaa 394
395/* The start of the above vector elements. */
396int *lra_point_freq;
397
398/* Increment the current program point POINT to the next point which has
399 execution frequency FREQ. */
400static void
401next_program_point (int &point, int freq)
402{
f1f41a6c 403 point_freq_vec.safe_push (freq);
404 lra_point_freq = point_freq_vec.address ();
c6a6cdaa 405 point++;
406}
407
408/* Update the preference of HARD_REGNO for pseudo REGNO by PROFIT. */
409void
410lra_setup_reload_pseudo_preferenced_hard_reg (int regno,
411 int hard_regno, int profit)
412{
413 lra_assert (regno >= lra_constraint_new_regno_start);
414 if (lra_reg_info[regno].preferred_hard_regno1 == hard_regno)
415 lra_reg_info[regno].preferred_hard_regno_profit1 += profit;
416 else if (lra_reg_info[regno].preferred_hard_regno2 == hard_regno)
417 lra_reg_info[regno].preferred_hard_regno_profit2 += profit;
418 else if (lra_reg_info[regno].preferred_hard_regno1 < 0)
419 {
420 lra_reg_info[regno].preferred_hard_regno1 = hard_regno;
421 lra_reg_info[regno].preferred_hard_regno_profit1 = profit;
422 }
423 else if (lra_reg_info[regno].preferred_hard_regno2 < 0
424 || profit > lra_reg_info[regno].preferred_hard_regno_profit2)
425 {
426 lra_reg_info[regno].preferred_hard_regno2 = hard_regno;
427 lra_reg_info[regno].preferred_hard_regno_profit2 = profit;
428 }
429 else
430 return;
431 /* Keep the 1st hard regno as more profitable. */
432 if (lra_reg_info[regno].preferred_hard_regno1 >= 0
433 && lra_reg_info[regno].preferred_hard_regno2 >= 0
434 && (lra_reg_info[regno].preferred_hard_regno_profit2
435 > lra_reg_info[regno].preferred_hard_regno_profit1))
436 {
437 int temp;
438
439 temp = lra_reg_info[regno].preferred_hard_regno1;
440 lra_reg_info[regno].preferred_hard_regno1
441 = lra_reg_info[regno].preferred_hard_regno2;
442 lra_reg_info[regno].preferred_hard_regno2 = temp;
443 temp = lra_reg_info[regno].preferred_hard_regno_profit1;
444 lra_reg_info[regno].preferred_hard_regno_profit1
445 = lra_reg_info[regno].preferred_hard_regno_profit2;
446 lra_reg_info[regno].preferred_hard_regno_profit2 = temp;
447 }
448 if (lra_dump_file != NULL)
449 {
450 if ((hard_regno = lra_reg_info[regno].preferred_hard_regno1) >= 0)
451 fprintf (lra_dump_file,
452 " Hard reg %d is preferable by r%d with profit %d\n",
453 hard_regno, regno,
454 lra_reg_info[regno].preferred_hard_regno_profit1);
455 if ((hard_regno = lra_reg_info[regno].preferred_hard_regno2) >= 0)
456 fprintf (lra_dump_file,
457 " Hard reg %d is preferable by r%d with profit %d\n",
458 hard_regno, regno,
459 lra_reg_info[regno].preferred_hard_regno_profit2);
460 }
461}
462
463/* Check that REGNO living through calls and setjumps, set up conflict
464 regs, and clear corresponding bits in PSEUDOS_LIVE_THROUGH_CALLS and
465 PSEUDOS_LIVE_THROUGH_SETJUMPS. */
466static inline void
467check_pseudos_live_through_calls (int regno)
468{
a766a8b0 469 int hr;
470
c6a6cdaa 471 if (! sparseset_bit_p (pseudos_live_through_calls, regno))
472 return;
473 sparseset_clear_bit (pseudos_live_through_calls, regno);
474 IOR_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs,
475 call_used_reg_set);
a766a8b0 476
477 for (hr = 0; hr < FIRST_PSEUDO_REGISTER; hr++)
478 if (HARD_REGNO_CALL_PART_CLOBBERED (hr, PSEUDO_REGNO_MODE (regno)))
479 SET_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hr);
c6a6cdaa 480#ifdef ENABLE_CHECKING
481 lra_reg_info[regno].call_p = true;
482#endif
483 if (! sparseset_bit_p (pseudos_live_through_setjumps, regno))
484 return;
485 sparseset_clear_bit (pseudos_live_through_setjumps, regno);
486 /* Don't allocate pseudos that cross setjmps or any call, if this
487 function receives a nonlocal goto. */
488 SET_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs);
489}
490
491/* Process insns of the basic block BB to update pseudo live ranges,
492 pseudo hard register conflicts, and insn notes. We do it on
493 backward scan of BB insns. CURR_POINT is the program point where
494 BB ends. The function updates this counter and returns in
495 CURR_POINT the program point where BB starts. */
496static void
497process_bb_lives (basic_block bb, int &curr_point)
498{
499 int i, regno, freq;
500 unsigned int j;
501 bitmap_iterator bi;
502 bitmap reg_live_out;
503 unsigned int px;
504 rtx link, *link_loc;
505 bool need_curr_point_incr;
506
507 reg_live_out = df_get_live_out (bb);
508 sparseset_clear (pseudos_live);
509 sparseset_clear (pseudos_live_through_calls);
510 sparseset_clear (pseudos_live_through_setjumps);
511 REG_SET_TO_HARD_REG_SET (hard_regs_live, reg_live_out);
512 AND_COMPL_HARD_REG_SET (hard_regs_live, eliminable_regset);
513 AND_COMPL_HARD_REG_SET (hard_regs_live, lra_no_alloc_regs);
514 EXECUTE_IF_SET_IN_BITMAP (reg_live_out, FIRST_PSEUDO_REGISTER, j, bi)
515 mark_pseudo_live (j, curr_point);
516
517 freq = REG_FREQ_FROM_BB (bb);
518
519 if (lra_dump_file != NULL)
520 fprintf (lra_dump_file, " BB %d\n", bb->index);
521
522 /* Scan the code of this basic block, noting which pseudos and hard
523 regs are born or die.
524
525 Note that this loop treats uninitialized values as live until the
526 beginning of the block. For example, if an instruction uses
527 (reg:DI foo), and only (subreg:SI (reg:DI foo) 0) is ever set,
528 FOO will remain live until the beginning of the block. Likewise
529 if FOO is not set at all. This is unnecessarily pessimistic, but
530 it probably doesn't matter much in practice. */
531 FOR_BB_INSNS_REVERSE (bb, curr_insn)
532 {
533 bool call_p;
534 int dst_regno, src_regno;
535 rtx set;
536 struct lra_insn_reg *reg;
537
538 if (!NONDEBUG_INSN_P (curr_insn))
539 continue;
540
541 curr_id = lra_get_insn_recog_data (curr_insn);
542 curr_static_id = curr_id->insn_static_data;
543 if (lra_dump_file != NULL)
544 fprintf (lra_dump_file, " Insn %u: point = %d\n",
545 INSN_UID (curr_insn), curr_point);
546
547 /* Update max ref width and hard reg usage. */
548 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
549 if (reg->regno >= FIRST_PSEUDO_REGISTER
550 && (GET_MODE_SIZE (reg->biggest_mode)
551 > GET_MODE_SIZE (lra_reg_info[reg->regno].biggest_mode)))
552 lra_reg_info[reg->regno].biggest_mode = reg->biggest_mode;
553 else if (reg->regno < FIRST_PSEUDO_REGISTER)
554 lra_hard_reg_usage[reg->regno] += freq;
555
556 call_p = CALL_P (curr_insn);
557 if (complete_info_p
558 && (set = single_set (curr_insn)) != NULL_RTX
559 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set))
560 /* Check that source regno does not conflict with
561 destination regno to exclude most impossible
562 preferences. */
563 && ((((src_regno = REGNO (SET_SRC (set))) >= FIRST_PSEUDO_REGISTER
564 && ! sparseset_bit_p (pseudos_live, src_regno))
565 || (src_regno < FIRST_PSEUDO_REGISTER
566 && ! TEST_HARD_REG_BIT (hard_regs_live, src_regno)))
567 /* It might be 'inheritance pseudo <- reload pseudo'. */
568 || (src_regno >= lra_constraint_new_regno_start
569 && ((int) REGNO (SET_DEST (set))
89bf872d 570 >= lra_constraint_new_regno_start)
571 /* Remember to skip special cases where src/dest regnos are
572 the same, e.g. insn SET pattern has matching constraints
573 like =r,0. */
574 && src_regno != (int) REGNO (SET_DEST (set)))))
c6a6cdaa 575 {
576 int hard_regno = -1, regno = -1;
577
578 dst_regno = REGNO (SET_DEST (set));
579 if (dst_regno >= lra_constraint_new_regno_start
580 && src_regno >= lra_constraint_new_regno_start)
581 lra_create_copy (dst_regno, src_regno, freq);
582 else if (dst_regno >= lra_constraint_new_regno_start)
583 {
584 if ((hard_regno = src_regno) >= FIRST_PSEUDO_REGISTER)
585 hard_regno = reg_renumber[src_regno];
586 regno = dst_regno;
587 }
588 else if (src_regno >= lra_constraint_new_regno_start)
589 {
590 if ((hard_regno = dst_regno) >= FIRST_PSEUDO_REGISTER)
591 hard_regno = reg_renumber[dst_regno];
592 regno = src_regno;
593 }
594 if (regno >= 0 && hard_regno >= 0)
595 lra_setup_reload_pseudo_preferenced_hard_reg
596 (regno, hard_regno, freq);
597 }
598
599 sparseset_clear (start_living);
600
601 /* Try to avoid unnecessary program point increments, this saves
602 a lot of time in remove_some_program_points_and_update_live_ranges.
603 We only need an increment if something becomes live or dies at this
604 program point. */
605 need_curr_point_incr = false;
606
607 /* Mark each defined value as live. We need to do this for
608 unused values because they still conflict with quantities
609 that are live at the time of the definition. */
610 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
611 if (reg->type != OP_IN)
612 {
613 need_curr_point_incr |= mark_regno_live (reg->regno,
614 reg->biggest_mode,
615 curr_point);
616 check_pseudos_live_through_calls (reg->regno);
617 }
618
619 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
620 if (reg->type != OP_IN)
621 make_hard_regno_born (reg->regno);
622
623 sparseset_copy (unused_set, start_living);
624
625 sparseset_clear (start_dying);
626
627 /* See which defined values die here. */
628 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
629 if (reg->type == OP_OUT && ! reg->early_clobber && ! reg->subreg_p)
630 need_curr_point_incr |= mark_regno_dead (reg->regno,
631 reg->biggest_mode,
632 curr_point);
633
634 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
635 if (reg->type == OP_OUT && ! reg->early_clobber && ! reg->subreg_p)
636 make_hard_regno_dead (reg->regno);
637
638 if (call_p)
639 {
f2cc6708 640 if (flag_use_caller_save)
641 {
642 HARD_REG_SET this_call_used_reg_set;
643 get_call_reg_set_usage (curr_insn, &this_call_used_reg_set,
644 call_used_reg_set);
645
646 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, j)
647 IOR_HARD_REG_SET (lra_reg_info[j].actual_call_used_reg_set,
648 this_call_used_reg_set);
649 }
650
c6a6cdaa 651 sparseset_ior (pseudos_live_through_calls,
652 pseudos_live_through_calls, pseudos_live);
653 if (cfun->has_nonlocal_label
654 || find_reg_note (curr_insn, REG_SETJMP,
655 NULL_RTX) != NULL_RTX)
656 sparseset_ior (pseudos_live_through_setjumps,
657 pseudos_live_through_setjumps, pseudos_live);
658 }
659
660 /* Increment the current program point if we must. */
661 if (need_curr_point_incr)
662 next_program_point (curr_point, freq);
663
664 sparseset_clear (start_living);
665
666 need_curr_point_incr = false;
667
668 /* Mark each used value as live. */
669 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
670 if (reg->type == OP_IN)
671 {
672 need_curr_point_incr |= mark_regno_live (reg->regno,
673 reg->biggest_mode,
674 curr_point);
675 check_pseudos_live_through_calls (reg->regno);
676 }
677
678 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
679 if (reg->type == OP_IN)
680 make_hard_regno_born (reg->regno);
681
682 if (curr_id->arg_hard_regs != NULL)
683 /* Make argument hard registers live. */
684 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
685 make_hard_regno_born (regno);
686
687 sparseset_and_compl (dead_set, start_living, start_dying);
688
689 /* Mark early clobber outputs dead. */
690 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
691 if (reg->type == OP_OUT && reg->early_clobber && ! reg->subreg_p)
37aa16f1 692 need_curr_point_incr |= mark_regno_dead (reg->regno,
693 reg->biggest_mode,
694 curr_point);
c6a6cdaa 695
696 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
697 if (reg->type == OP_OUT && reg->early_clobber && ! reg->subreg_p)
698 make_hard_regno_dead (reg->regno);
699
700 if (need_curr_point_incr)
701 next_program_point (curr_point, freq);
702
703 /* Update notes. */
704 for (link_loc = &REG_NOTES (curr_insn); (link = *link_loc) != NULL_RTX;)
705 {
706 if (REG_NOTE_KIND (link) != REG_DEAD
707 && REG_NOTE_KIND (link) != REG_UNUSED)
708 ;
709 else if (REG_P (XEXP (link, 0)))
710 {
711 regno = REGNO (XEXP (link, 0));
712 if ((REG_NOTE_KIND (link) == REG_DEAD
713 && ! sparseset_bit_p (dead_set, regno))
714 || (REG_NOTE_KIND (link) == REG_UNUSED
715 && ! sparseset_bit_p (unused_set, regno)))
716 {
717 *link_loc = XEXP (link, 1);
718 continue;
719 }
720 if (REG_NOTE_KIND (link) == REG_DEAD)
721 sparseset_clear_bit (dead_set, regno);
722 else if (REG_NOTE_KIND (link) == REG_UNUSED)
723 sparseset_clear_bit (unused_set, regno);
724 }
725 link_loc = &XEXP (link, 1);
726 }
727 EXECUTE_IF_SET_IN_SPARSESET (dead_set, j)
728 add_reg_note (curr_insn, REG_DEAD, regno_reg_rtx[j]);
729 EXECUTE_IF_SET_IN_SPARSESET (unused_set, j)
730 add_reg_note (curr_insn, REG_UNUSED, regno_reg_rtx[j]);
731 }
732
733#ifdef EH_RETURN_DATA_REGNO
734 if (bb_has_eh_pred (bb))
735 for (j = 0; ; ++j)
736 {
737 unsigned int regno = EH_RETURN_DATA_REGNO (j);
738
739 if (regno == INVALID_REGNUM)
740 break;
741 make_hard_regno_born (regno);
742 }
743#endif
744
745 /* Pseudos can't go in stack regs at the start of a basic block that
746 is reached by an abnormal edge. Likewise for call clobbered regs,
747 because caller-save, fixup_abnormal_edges and possibly the table
748 driven EH machinery are not quite ready to handle such pseudos
749 live across such edges. */
750 if (bb_has_abnormal_pred (bb))
751 {
752#ifdef STACK_REGS
753 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, px)
754 lra_reg_info[px].no_stack_p = true;
755 for (px = FIRST_STACK_REG; px <= LAST_STACK_REG; px++)
756 make_hard_regno_born (px);
757#endif
758 /* No need to record conflicts for call clobbered regs if we
759 have nonlocal labels around, as we don't ever try to
760 allocate such regs in this case. */
761 if (!cfun->has_nonlocal_label && bb_has_abnormal_call_pred (bb))
762 for (px = 0; px < FIRST_PSEUDO_REGISTER; px++)
763 if (call_used_regs[px])
764 make_hard_regno_born (px);
765 }
766
767 /* See if we'll need an increment at the end of this basic block.
768 An increment is needed if the PSEUDOS_LIVE set is not empty,
769 to make sure the finish points are set up correctly. */
770 need_curr_point_incr = (sparseset_cardinality (pseudos_live) > 0);
771
772 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, i)
773 mark_pseudo_dead (i, curr_point);
774
775 EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb), FIRST_PSEUDO_REGISTER, j, bi)
776 {
777 if (sparseset_cardinality (pseudos_live_through_calls) == 0)
778 break;
779 if (sparseset_bit_p (pseudos_live_through_calls, j))
780 check_pseudos_live_through_calls (j);
781 }
782
783 if (need_curr_point_incr)
784 next_program_point (curr_point, freq);
785}
786
787/* Compress pseudo live ranges by removing program points where
788 nothing happens. Complexity of many algorithms in LRA is linear
789 function of program points number. To speed up the code we try to
790 minimize the number of the program points here. */
791static void
792remove_some_program_points_and_update_live_ranges (void)
793{
794 unsigned i;
795 int n, max_regno;
796 int *map;
797 lra_live_range_t r, prev_r, next_r;
798 sbitmap born_or_dead, born, dead;
799 sbitmap_iterator sbi;
800 bool born_p, dead_p, prev_born_p, prev_dead_p;
801
802 born = sbitmap_alloc (lra_live_max_point);
803 dead = sbitmap_alloc (lra_live_max_point);
53c5d9d4 804 bitmap_clear (born);
805 bitmap_clear (dead);
c6a6cdaa 806 max_regno = max_reg_num ();
807 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
808 {
809 for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
810 {
811 lra_assert (r->start <= r->finish);
08b7917c 812 bitmap_set_bit (born, r->start);
813 bitmap_set_bit (dead, r->finish);
c6a6cdaa 814 }
815 }
816 born_or_dead = sbitmap_alloc (lra_live_max_point);
53c5d9d4 817 bitmap_ior (born_or_dead, born, dead);
c6a6cdaa 818 map = XCNEWVEC (int, lra_live_max_point);
819 n = -1;
820 prev_born_p = prev_dead_p = false;
0d211963 821 EXECUTE_IF_SET_IN_BITMAP (born_or_dead, 0, i, sbi)
c6a6cdaa 822 {
08b7917c 823 born_p = bitmap_bit_p (born, i);
824 dead_p = bitmap_bit_p (dead, i);
c6a6cdaa 825 if ((prev_born_p && ! prev_dead_p && born_p && ! dead_p)
826 || (prev_dead_p && ! prev_born_p && dead_p && ! born_p))
827 {
828 map[i] = n;
829 lra_point_freq[n] = MAX (lra_point_freq[n], lra_point_freq[i]);
830 }
831 else
832 {
833 map[i] = ++n;
834 lra_point_freq[n] = lra_point_freq[i];
835 }
836 prev_born_p = born_p;
837 prev_dead_p = dead_p;
838 }
839 sbitmap_free (born_or_dead);
840 sbitmap_free (born);
841 sbitmap_free (dead);
842 n++;
843 if (lra_dump_file != NULL)
844 fprintf (lra_dump_file, "Compressing live ranges: from %d to %d - %d%%\n",
845 lra_live_max_point, n, 100 * n / lra_live_max_point);
846 if (n < lra_live_max_point)
847 {
848 lra_live_max_point = n;
849 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
850 {
851 for (prev_r = NULL, r = lra_reg_info[i].live_ranges;
852 r != NULL;
853 r = next_r)
854 {
855 next_r = r->next;
856 r->start = map[r->start];
857 r->finish = map[r->finish];
858 if (prev_r == NULL || prev_r->start > r->finish + 1)
859 {
860 prev_r = r;
861 continue;
862 }
863 prev_r->start = r->start;
864 prev_r->next = next_r;
865 free_live_range (r);
866 }
867 }
868 }
869 free (map);
870}
871
872/* Print live ranges R to file F. */
873void
874lra_print_live_range_list (FILE *f, lra_live_range_t r)
875{
876 for (; r != NULL; r = r->next)
877 fprintf (f, " [%d..%d]", r->start, r->finish);
878 fprintf (f, "\n");
879}
880
c7d89805 881DEBUG_FUNCTION void
882debug (lra_live_range &ref)
883{
884 lra_print_live_range_list (stderr, &ref);
885}
886
887DEBUG_FUNCTION void
888debug (lra_live_range *ptr)
889{
890 if (ptr)
891 debug (*ptr);
892 else
893 fprintf (stderr, "<nil>\n");
894}
895
c6a6cdaa 896/* Print live ranges R to stderr. */
897void
898lra_debug_live_range_list (lra_live_range_t r)
899{
900 lra_print_live_range_list (stderr, r);
901}
902
903/* Print live ranges of pseudo REGNO to file F. */
904static void
905print_pseudo_live_ranges (FILE *f, int regno)
906{
907 if (lra_reg_info[regno].live_ranges == NULL)
908 return;
909 fprintf (f, " r%d:", regno);
910 lra_print_live_range_list (f, lra_reg_info[regno].live_ranges);
911}
912
913/* Print live ranges of pseudo REGNO to stderr. */
914void
915lra_debug_pseudo_live_ranges (int regno)
916{
917 print_pseudo_live_ranges (stderr, regno);
918}
919
920/* Print live ranges of all pseudos to file F. */
921static void
922print_live_ranges (FILE *f)
923{
924 int i, max_regno;
925
926 max_regno = max_reg_num ();
927 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
928 print_pseudo_live_ranges (f, i);
929}
930
931/* Print live ranges of all pseudos to stderr. */
932void
933lra_debug_live_ranges (void)
934{
935 print_live_ranges (stderr);
936}
937
938/* Compress pseudo live ranges. */
939static void
940compress_live_ranges (void)
941{
942 remove_some_program_points_and_update_live_ranges ();
943 if (lra_dump_file != NULL)
944 {
945 fprintf (lra_dump_file, "Ranges after the compression:\n");
946 print_live_ranges (lra_dump_file);
947 }
948}
949
950/* The number of the current live range pass. */
951int lra_live_range_iter;
952
953/* The main entry function creates live ranges only for memory pseudos
954 (or for all ones if ALL_P), set up CONFLICT_HARD_REGS for
955 the pseudos. */
956void
957lra_create_live_ranges (bool all_p)
958{
959 basic_block bb;
960 int i, hard_regno, max_regno = max_reg_num ();
961 int curr_point;
dbb9a07c 962 bool have_referenced_pseudos = false;
c6a6cdaa 963
964 timevar_push (TV_LRA_CREATE_LIVE_RANGES);
965
966 complete_info_p = all_p;
967 if (lra_dump_file != NULL)
968 fprintf (lra_dump_file,
969 "\n********** Pseudo live ranges #%d: **********\n\n",
970 ++lra_live_range_iter);
971 memset (lra_hard_reg_usage, 0, sizeof (lra_hard_reg_usage));
972 for (i = 0; i < max_regno; i++)
973 {
974 lra_reg_info[i].live_ranges = NULL;
975 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
976 lra_reg_info[i].preferred_hard_regno1 = -1;
977 lra_reg_info[i].preferred_hard_regno2 = -1;
978 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
979 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
980#ifdef STACK_REGS
981 lra_reg_info[i].no_stack_p = false;
982#endif
fc8a0f60 983 /* The biggest mode is already set but its value might be to
984 conservative because of recent transformation. Here in this
985 file we recalculate it again as it costs practically
986 nothing. */
c6a6cdaa 987 if (regno_reg_rtx[i] != NULL_RTX)
988 lra_reg_info[i].biggest_mode = GET_MODE (regno_reg_rtx[i]);
989 else
990 lra_reg_info[i].biggest_mode = VOIDmode;
991#ifdef ENABLE_CHECKING
992 lra_reg_info[i].call_p = false;
993#endif
994 if (i >= FIRST_PSEUDO_REGISTER
dbb9a07c 995 && lra_reg_info[i].nrefs != 0)
996 {
997 if ((hard_regno = reg_renumber[i]) >= 0)
998 lra_hard_reg_usage[hard_regno] += lra_reg_info[i].freq;
999 have_referenced_pseudos = true;
1000 }
c6a6cdaa 1001 }
1002 lra_free_copies ();
dbb9a07c 1003
1004 /* Under some circumstances, we can have functions without pseudo
1005 registers. For such functions, lra_live_max_point will be 0,
1006 see e.g. PR55604, and there's nothing more to do for us here. */
1007 if (! have_referenced_pseudos)
1008 {
1009 timevar_pop (TV_LRA_CREATE_LIVE_RANGES);
1010 return;
1011 }
1012
c6a6cdaa 1013 pseudos_live = sparseset_alloc (max_regno);
1014 pseudos_live_through_calls = sparseset_alloc (max_regno);
1015 pseudos_live_through_setjumps = sparseset_alloc (max_regno);
1016 start_living = sparseset_alloc (max_regno);
1017 start_dying = sparseset_alloc (max_regno);
1018 dead_set = sparseset_alloc (max_regno);
1019 unused_set = sparseset_alloc (max_regno);
1020 curr_point = 0;
f1f41a6c 1021 point_freq_vec.create (get_max_uid () * 2);
1022 lra_point_freq = point_freq_vec.address ();
fe672ac0 1023 int *post_order_rev_cfg = XNEWVEC (int, last_basic_block_for_fn (cfun));
c6a6cdaa 1024 int n_blocks_inverted = inverted_post_order_compute (post_order_rev_cfg);
a28770e1 1025 lra_assert (n_blocks_inverted == n_basic_blocks_for_fn (cfun));
c6a6cdaa 1026 for (i = n_blocks_inverted - 1; i >= 0; --i)
1027 {
f5a6b05f 1028 bb = BASIC_BLOCK_FOR_FN (cfun, post_order_rev_cfg[i]);
34154e27 1029 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb
1030 == ENTRY_BLOCK_PTR_FOR_FN (cfun))
c6a6cdaa 1031 continue;
1032 process_bb_lives (bb, curr_point);
1033 }
1034 free (post_order_rev_cfg);
1035 lra_live_max_point = curr_point;
dbb9a07c 1036 gcc_checking_assert (lra_live_max_point > 0);
c6a6cdaa 1037 if (lra_dump_file != NULL)
1038 print_live_ranges (lra_dump_file);
1039 /* Clean up. */
1040 sparseset_free (unused_set);
1041 sparseset_free (dead_set);
1042 sparseset_free (start_dying);
1043 sparseset_free (start_living);
1044 sparseset_free (pseudos_live_through_calls);
1045 sparseset_free (pseudos_live_through_setjumps);
1046 sparseset_free (pseudos_live);
1047 compress_live_ranges ();
1048 timevar_pop (TV_LRA_CREATE_LIVE_RANGES);
1049}
1050
1051/* Finish all live ranges. */
1052void
1053lra_clear_live_ranges (void)
1054{
1055 int i;
1056
1057 for (i = 0; i < max_reg_num (); i++)
1058 free_live_range_list (lra_reg_info[i].live_ranges);
f1f41a6c 1059 point_freq_vec.release ();
c6a6cdaa 1060}
1061
1062/* Initialize live ranges data once per function. */
1063void
1064lra_live_ranges_init (void)
1065{
1066 live_range_pool = create_alloc_pool ("live ranges",
1067 sizeof (struct lra_live_range), 100);
1068}
1069
1070/* Finish live ranges data once per function. */
1071void
1072lra_live_ranges_finish (void)
1073{
1074 free_alloc_pool (live_range_pool);
1075}