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