]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lra-lives.c
Pass unpromoted argument to promote_function_mode
[thirdparty/gcc.git] / gcc / lra-lives.c
CommitLineData
55a2c322 1/* Build live ranges for pseudos.
23a5b65a 2 Copyright (C) 2010-2014 Free Software Foundation, Inc.
55a2c322
VM
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"
83685514
AM
39#include "hashtab.h"
40#include "hash-set.h"
41#include "vec.h"
42#include "machmode.h"
43#include "input.h"
55a2c322
VM
44#include "function.h"
45#include "expr.h"
60393bbc
AM
46#include "predict.h"
47#include "dominance.h"
48#include "cfg.h"
49#include "cfganal.h"
55a2c322
VM
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
4ab74a01
VM
103/* Bitmap used for holding intermediate bitmap operation results. */
104static bitmap_head temp_bitmap;
105
55a2c322
VM
106/* Pool for pseudo live ranges. */
107static alloc_pool live_range_pool;
108
109/* Free live range LR. */
110static void
111free_live_range (lra_live_range_t lr)
112{
113 pool_free (live_range_pool, lr);
114}
115
116/* Free live range list LR. */
117static void
118free_live_range_list (lra_live_range_t lr)
119{
120 lra_live_range_t next;
121
122 while (lr != NULL)
123 {
124 next = lr->next;
125 free_live_range (lr);
126 lr = next;
127 }
128}
129
130/* Create and return pseudo live range with given attributes. */
131static lra_live_range_t
132create_live_range (int regno, int start, int finish, lra_live_range_t next)
133{
134 lra_live_range_t p;
135
136 p = (lra_live_range_t) pool_alloc (live_range_pool);
137 p->regno = regno;
138 p->start = start;
139 p->finish = finish;
140 p->next = next;
141 return p;
142}
143
144/* Copy live range R and return the result. */
145static lra_live_range_t
146copy_live_range (lra_live_range_t r)
147{
148 lra_live_range_t p;
149
150 p = (lra_live_range_t) pool_alloc (live_range_pool);
151 *p = *r;
152 return p;
153}
154
155/* Copy live range list given by its head R and return the result. */
156lra_live_range_t
157lra_copy_live_range_list (lra_live_range_t r)
158{
159 lra_live_range_t p, first, *chain;
160
161 first = NULL;
162 for (chain = &first; r != NULL; r = r->next)
163 {
164 p = copy_live_range (r);
165 *chain = p;
166 chain = &p->next;
167 }
168 return first;
169}
170
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
174 after the call. */
175lra_live_range_t
176lra_merge_live_ranges (lra_live_range_t r1, lra_live_range_t r2)
177{
178 lra_live_range_t first, last, temp;
179
180 if (r1 == NULL)
181 return r2;
182 if (r2 == NULL)
183 return r1;
184 for (first = last = NULL; r1 != NULL && r2 != NULL;)
185 {
186 if (r1->start < r2->start)
187 {
188 temp = r1;
189 r1 = r2;
190 r2 = temp;
191 }
192 if (r1->start == r2->finish + 1)
193 {
194 /* Joint ranges: merge r1 and r2 into r1. */
195 r1->start = r2->start;
196 temp = r2;
197 r2 = r2->next;
198 pool_free (live_range_pool, temp);
199 }
200 else
201 {
202 gcc_assert (r2->finish + 1 < r1->start);
203 /* Add r1 to the result. */
204 if (first == NULL)
205 first = last = r1;
206 else
207 {
208 last->next = r1;
209 last = r1;
210 }
211 r1 = r1->next;
212 }
213 }
214 if (r1 != NULL)
215 {
216 if (first == NULL)
217 first = r1;
218 else
219 last->next = r1;
220 }
221 else
222 {
223 lra_assert (r2 != NULL);
224 if (first == NULL)
225 first = r2;
226 else
227 last->next = r2;
228 }
229 return first;
230}
231
232/* Return TRUE if live ranges R1 and R2 intersect. */
233bool
234lra_intersected_live_ranges_p (lra_live_range_t r1, lra_live_range_t r2)
235{
236 /* Remember the live ranges are always kept ordered. */
237 while (r1 != NULL && r2 != NULL)
238 {
239 if (r1->start > r2->finish)
240 r1 = r1->next;
241 else if (r2->start > r1->finish)
242 r2 = r2->next;
243 else
244 return true;
245 }
246 return false;
247}
248
249/* The function processing birth of hard register REGNO. It updates
250 living hard regs, conflict hard regs for living pseudos, and
251 START_LIVING. */
252static void
253make_hard_regno_born (int regno)
254{
255 unsigned int i;
256
257 lra_assert (regno < FIRST_PSEUDO_REGISTER);
d9cf932c 258 if (TEST_HARD_REG_BIT (hard_regs_live, regno))
55a2c322
VM
259 return;
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);
264}
265
266/* Process the death of hard register REGNO. This updates
267 hard_regs_live and START_DYING. */
268static void
269make_hard_regno_dead (int regno)
270{
271 lra_assert (regno < FIRST_PSEUDO_REGISTER);
d9cf932c 272 if (! TEST_HARD_REG_BIT (hard_regs_live, regno))
55a2c322
VM
273 return;
274 sparseset_set_bit (start_dying, regno);
275 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
276}
277
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. */
281static void
282mark_pseudo_live (int regno, int point)
283{
284 lra_live_range_t p;
285
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);
290
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);
297}
298
299/* Mark pseudo REGNO as not living at program point POINT and update
300 START_DYING.
301 This finishes the current live range for the pseudo corresponding
302 to REGNO. */
303static void
304mark_pseudo_dead (int regno, int point)
305{
306 lra_live_range_t p;
307
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)
313 {
314 p = lra_reg_info[regno].live_ranges;
315 lra_assert (p != NULL);
316 p->finish = point;
317 }
318}
319
8160cd3e
VM
320/* The corresponding bitmaps of BB currently being processed. */
321static bitmap bb_killed_pseudos, bb_gen_pseudos;
322
323/* Mark register REGNO (pseudo or hard register) in MODE as live at
18ea3d61 324 program point POINT. Update BB_GEN_PSEUDOS.
8160cd3e
VM
325 Return TRUE if the liveness tracking sets were modified, or FALSE
326 if nothing changed. */
55a2c322 327static bool
18ea3d61 328mark_regno_live (int regno, machine_mode mode, int point)
55a2c322
VM
329{
330 int last;
331 bool changed = false;
332
333 if (regno < FIRST_PSEUDO_REGISTER)
334 {
335 for (last = regno + hard_regno_nregs[regno][mode];
336 regno < last;
337 regno++)
338 make_hard_regno_born (regno);
339 }
8160cd3e 340 else
55a2c322 341 {
8160cd3e
VM
342 if (! sparseset_bit_p (pseudos_live, regno))
343 {
344 mark_pseudo_live (regno, point);
345 changed = true;
346 }
18ea3d61 347 bitmap_set_bit (bb_gen_pseudos, regno);
55a2c322
VM
348 }
349 return changed;
350}
351
352
8160cd3e 353/* Mark register REGNO in MODE as dead at program point POINT. Update
18ea3d61
VM
354 BB_GEN_PSEUDOS and BB_KILLED_PSEUDOS. Return TRUE if the liveness
355 tracking sets were modified, or FALSE if nothing changed. */
55a2c322 356static bool
18ea3d61 357mark_regno_dead (int regno, machine_mode mode, int point)
55a2c322
VM
358{
359 int last;
360 bool changed = false;
361
362 if (regno < FIRST_PSEUDO_REGISTER)
363 {
364 for (last = regno + hard_regno_nregs[regno][mode];
365 regno < last;
366 regno++)
367 make_hard_regno_dead (regno);
368 }
8160cd3e 369 else
55a2c322 370 {
8160cd3e
VM
371 if (sparseset_bit_p (pseudos_live, regno))
372 {
373 mark_pseudo_dead (regno, point);
374 changed = true;
375 }
18ea3d61
VM
376 bitmap_clear_bit (bb_gen_pseudos, regno);
377 bitmap_set_bit (bb_killed_pseudos, regno);
55a2c322
VM
378 }
379 return changed;
380}
381
8160cd3e
VM
382\f
383
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. */
388
389/* Structure describing local BB data used for pseudo
390 live-analysis. */
152914cd 391struct bb_data_pseudos
8160cd3e
VM
392{
393 /* Basic block about which the below data are. */
394 basic_block bb;
395 bitmap_head killed_pseudos; /* pseudos killed in the BB. */
396 bitmap_head gen_pseudos; /* pseudos generated in the BB. */
397};
398
399/* Array for all BB data. Indexed by the corresponding BB index. */
152914cd 400typedef struct bb_data_pseudos *bb_data_t;
8160cd3e
VM
401
402/* All basic block data are referred through the following array. */
403static bb_data_t bb_data;
404
405/* Two small functions for access to the bb data. */
406static inline bb_data_t
407get_bb_data (basic_block bb)
408{
409 return &bb_data[(bb)->index];
410}
411
412static inline bb_data_t
413get_bb_data_by_index (int index)
414{
415 return &bb_data[index];
416}
417
418/* Bitmap with all hard regs. */
419static bitmap_head all_hard_regs_bitmap;
420
8160cd3e
VM
421/* The transfer function used by the DF equation solver to propagate
422 live info through block with BB_INDEX according to the following
423 equation:
424
425 bb.livein = (bb.liveout - bb.kill) OR bb.gen
426*/
427static bool
428live_trans_fun (int bb_index)
429{
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);
434
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);
438}
439
440/* The confluence function used by the DF equation solver to set up
441 live info for a block BB without predecessor. */
442static void
443live_con_fun_0 (basic_block bb)
444{
445 bitmap_and_into (df_get_live_out (bb), &all_hard_regs_bitmap);
446}
447
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
450 following equation:
451
452 bb.liveout = 0 for entry block | OR (livein of successors)
453 */
454static bool
455live_con_fun_n (edge e)
456{
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);
461
462 return bitmap_ior_and_compl_into (bb_liveout,
463 dest_livein, &all_hard_regs_bitmap);
464}
465
466/* Indexes of all function blocks. */
467static bitmap_head all_blocks;
468
469/* Allocate and initialize data needed for global pseudo live
470 analysis. */
471static void
472initiate_live_solver (void)
473{
8160cd3e
VM
474 bitmap_initialize (&all_hard_regs_bitmap, &reg_obstack);
475 bitmap_set_range (&all_hard_regs_bitmap, 0, FIRST_PSEUDO_REGISTER);
152914cd 476 bb_data = XNEWVEC (struct bb_data_pseudos, last_basic_block_for_fn (cfun));
8160cd3e
VM
477 bitmap_initialize (&all_blocks, &reg_obstack);
478
479 basic_block bb;
480 FOR_ALL_BB_FN (bb, cfun)
481 {
482 bb_data_t bb_info = get_bb_data (bb);
483 bb_info->bb = bb;
484 bitmap_initialize (&bb_info->killed_pseudos, &reg_obstack);
485 bitmap_initialize (&bb_info->gen_pseudos, &reg_obstack);
486 bitmap_set_bit (&all_blocks, bb->index);
487 }
488}
489
490/* Free all data needed for global pseudo live analysis. */
491static void
492finish_live_solver (void)
493{
494 basic_block bb;
495
496 bitmap_clear (&all_blocks);
497 FOR_ALL_BB_FN (bb, cfun)
498 {
499 bb_data_t bb_info = get_bb_data (bb);
500 bitmap_clear (&bb_info->killed_pseudos);
501 bitmap_clear (&bb_info->gen_pseudos);
502 }
503 free (bb_data);
504 bitmap_clear (&all_hard_regs_bitmap);
8160cd3e
VM
505}
506
507\f
508
55a2c322 509/* Insn currently scanned. */
cfa434f6 510static rtx_insn *curr_insn;
55a2c322
VM
511/* The insn data. */
512static lra_insn_recog_data_t curr_id;
513/* The insn static data. */
514static struct lra_static_insn_data *curr_static_id;
515
516/* Return true when one of the predecessor edges of BB is marked with
517 EDGE_ABNORMAL_CALL or EDGE_EH. */
518static bool
519bb_has_abnormal_call_pred (basic_block bb)
520{
521 edge e;
522 edge_iterator ei;
523
524 FOR_EACH_EDGE (e, ei, bb->preds)
525 {
526 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
527 return true;
528 }
529 return false;
530}
531
532/* Vec containing execution frequencies of program points. */
9771b263 533static vec<int> point_freq_vec;
55a2c322
VM
534
535/* The start of the above vector elements. */
536int *lra_point_freq;
537
538/* Increment the current program point POINT to the next point which has
539 execution frequency FREQ. */
540static void
541next_program_point (int &point, int freq)
542{
9771b263
DN
543 point_freq_vec.safe_push (freq);
544 lra_point_freq = point_freq_vec.address ();
55a2c322
VM
545 point++;
546}
547
548/* Update the preference of HARD_REGNO for pseudo REGNO by PROFIT. */
549void
550lra_setup_reload_pseudo_preferenced_hard_reg (int regno,
551 int hard_regno, int profit)
552{
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)
559 {
560 lra_reg_info[regno].preferred_hard_regno1 = hard_regno;
561 lra_reg_info[regno].preferred_hard_regno_profit1 = profit;
562 }
563 else if (lra_reg_info[regno].preferred_hard_regno2 < 0
564 || profit > lra_reg_info[regno].preferred_hard_regno_profit2)
565 {
566 lra_reg_info[regno].preferred_hard_regno2 = hard_regno;
567 lra_reg_info[regno].preferred_hard_regno_profit2 = profit;
568 }
569 else
570 return;
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))
576 {
577 int temp;
578
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;
587 }
588 if (lra_dump_file != NULL)
589 {
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",
593 hard_regno, regno,
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",
598 hard_regno, regno,
599 lra_reg_info[regno].preferred_hard_regno_profit2);
600 }
601}
602
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. */
606static inline void
607check_pseudos_live_through_calls (int regno)
608{
8a26ad39
VM
609 int hr;
610
55a2c322
VM
611 if (! sparseset_bit_p (pseudos_live_through_calls, regno))
612 return;
613 sparseset_clear_bit (pseudos_live_through_calls, regno);
614 IOR_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs,
615 call_used_reg_set);
8a26ad39
VM
616
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);
55a2c322
VM
620#ifdef ENABLE_CHECKING
621 lra_reg_info[regno].call_p = true;
622#endif
623 if (! sparseset_bit_p (pseudos_live_through_setjumps, regno))
624 return;
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);
629}
630
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
8160cd3e 635 CURR_POINT the program point where BB starts. The function also
4ab74a01 636 does local live info updates and can delete the dead insns if
18ea3d61 637 DEAD_INSN_P. It returns true if pseudo live info was
8160cd3e
VM
638 changed at the BB start. */
639static bool
18ea3d61 640process_bb_lives (basic_block bb, int &curr_point, bool dead_insn_p)
55a2c322
VM
641{
642 int i, regno, freq;
643 unsigned int j;
644 bitmap_iterator bi;
645 bitmap reg_live_out;
646 unsigned int px;
8160cd3e 647 rtx_insn *next;
55a2c322
VM
648 rtx link, *link_loc;
649 bool need_curr_point_incr;
650
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);
55a2c322
VM
657 EXECUTE_IF_SET_IN_BITMAP (reg_live_out, FIRST_PSEUDO_REGISTER, j, bi)
658 mark_pseudo_live (j, curr_point);
659
18ea3d61
VM
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);
55a2c322
VM
664 freq = REG_FREQ_FROM_BB (bb);
665
666 if (lra_dump_file != NULL)
667 fprintf (lra_dump_file, " BB %d\n", bb->index);
668
669 /* Scan the code of this basic block, noting which pseudos and hard
670 regs are born or die.
671
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. */
8160cd3e 678 FOR_BB_INSNS_REVERSE_SAFE (bb, curr_insn, next)
55a2c322
VM
679 {
680 bool call_p;
681 int dst_regno, src_regno;
682 rtx set;
683 struct lra_insn_reg *reg;
684
685 if (!NONDEBUG_INSN_P (curr_insn))
686 continue;
687
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);
693
8160cd3e
VM
694 set = single_set (curr_insn);
695
18ea3d61 696 if (dead_insn_p && set != NULL_RTX
6750565c
VM
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)))
8160cd3e 704 {
18ea3d61 705 bool remove_p = true;
8160cd3e
VM
706
707 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
708 if (reg->type != OP_IN && sparseset_bit_p (pseudos_live, reg->regno))
709 {
18ea3d61 710 remove_p = false;
8160cd3e
VM
711 break;
712 }
713 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
714 if (reg->type != OP_IN)
715 {
18ea3d61 716 remove_p = false;
8160cd3e
VM
717 break;
718 }
18ea3d61 719 if (remove_p && ! volatile_refs_p (PATTERN (curr_insn)))
8160cd3e
VM
720 {
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)
727 {
728 /* There might be some debug insns with the pseudo. */
729 unsigned int uid;
730 rtx_insn *insn;
731
4ab74a01
VM
732 bitmap_copy (&temp_bitmap, &lra_reg_info[dst_regno].insn_bitmap);
733 EXECUTE_IF_SET_IN_BITMAP (&temp_bitmap, 0, uid, bi)
8160cd3e
VM
734 {
735 insn = lra_insn_recog_data[uid]->insn;
736 lra_substitute_pseudo_within_insn (insn, dst_regno,
737 SET_SRC (set));
738 lra_update_insn_regno_info (insn);
739 }
740 }
741 continue;
742 }
743 }
744
55a2c322
VM
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;
753
754 call_p = CALL_P (curr_insn);
755 if (complete_info_p
8160cd3e 756 && set != NULL_RTX
55a2c322
VM
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
760 preferences. */
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))
debd8f30
CLT
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
771 like =r,0. */
772 && src_regno != (int) REGNO (SET_DEST (set)))))
55a2c322
VM
773 {
774 int hard_regno = -1, regno = -1;
775
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)
781 {
782 if ((hard_regno = src_regno) >= FIRST_PSEUDO_REGISTER)
783 hard_regno = reg_renumber[src_regno];
784 regno = dst_regno;
785 }
786 else if (src_regno >= lra_constraint_new_regno_start)
787 {
788 if ((hard_regno = dst_regno) >= FIRST_PSEUDO_REGISTER)
789 hard_regno = reg_renumber[dst_regno];
790 regno = src_regno;
791 }
792 if (regno >= 0 && hard_regno >= 0)
793 lra_setup_reload_pseudo_preferenced_hard_reg
794 (regno, hard_regno, freq);
795 }
796
797 sparseset_clear (start_living);
798
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
802 program point. */
803 need_curr_point_incr = false;
804
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)
810 {
4ab74a01
VM
811 need_curr_point_incr
812 |= mark_regno_live (reg->regno, reg->biggest_mode,
18ea3d61 813 curr_point);
55a2c322
VM
814 check_pseudos_live_through_calls (reg->regno);
815 }
816
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);
820
821 sparseset_copy (unused_set, start_living);
822
823 sparseset_clear (start_dying);
824
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)
4ab74a01
VM
828 need_curr_point_incr
829 |= mark_regno_dead (reg->regno, reg->biggest_mode,
18ea3d61 830 curr_point);
55a2c322
VM
831
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);
835
836 if (call_p)
837 {
10e1bdb2
TV
838 if (flag_use_caller_save)
839 {
840 HARD_REG_SET this_call_used_reg_set;
841 get_call_reg_set_usage (curr_insn, &this_call_used_reg_set,
842 call_used_reg_set);
843
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);
847 }
848
55a2c322
VM
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);
856 }
857
858 /* Increment the current program point if we must. */
859 if (need_curr_point_incr)
860 next_program_point (curr_point, freq);
861
862 sparseset_clear (start_living);
863
864 need_curr_point_incr = false;
865
866 /* Mark each used value as live. */
867 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
868 if (reg->type == OP_IN)
869 {
4ab74a01
VM
870 need_curr_point_incr
871 |= mark_regno_live (reg->regno, reg->biggest_mode,
18ea3d61 872 curr_point);
55a2c322
VM
873 check_pseudos_live_through_calls (reg->regno);
874 }
875
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);
879
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);
884
885 sparseset_and_compl (dead_set, start_living, start_dying);
886
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)
4ab74a01
VM
890 need_curr_point_incr
891 |= mark_regno_dead (reg->regno, reg->biggest_mode,
18ea3d61 892 curr_point);
55a2c322
VM
893
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);
897
898 if (need_curr_point_incr)
899 next_program_point (curr_point, freq);
900
901 /* Update notes. */
902 for (link_loc = &REG_NOTES (curr_insn); (link = *link_loc) != NULL_RTX;)
903 {
904 if (REG_NOTE_KIND (link) != REG_DEAD
905 && REG_NOTE_KIND (link) != REG_UNUSED)
906 ;
907 else if (REG_P (XEXP (link, 0)))
908 {
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)))
914 {
915 *link_loc = XEXP (link, 1);
916 continue;
917 }
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);
922 }
923 link_loc = &XEXP (link, 1);
924 }
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]);
929 }
930
931#ifdef EH_RETURN_DATA_REGNO
932 if (bb_has_eh_pred (bb))
933 for (j = 0; ; ++j)
934 {
935 unsigned int regno = EH_RETURN_DATA_REGNO (j);
936
937 if (regno == INVALID_REGNUM)
938 break;
939 make_hard_regno_born (regno);
940 }
941#endif
942
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))
949 {
950#ifdef STACK_REGS
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);
955#endif
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);
963 }
964
8160cd3e 965 bool live_change_p = false;
18ea3d61
VM
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)
8160cd3e 970 {
18ea3d61
VM
971 live_pseudos_num++;
972 if (! sparseset_bit_p (pseudos_live, j))
8160cd3e 973 {
18ea3d61
VM
974 live_change_p = TRUE;
975 break;
8160cd3e
VM
976 }
977 }
18ea3d61
VM
978 live_change_p
979 = (live_change_p
980 || sparseset_cardinality (pseudos_live) != live_pseudos_num);
981
55a2c322
VM
982 /* See if we'll need an increment at the end of this basic block.
983 An increment is needed if the PSEUDOS_LIVE set is not empty,
984 to make sure the finish points are set up correctly. */
985 need_curr_point_incr = (sparseset_cardinality (pseudos_live) > 0);
986
987 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, i)
988 mark_pseudo_dead (i, curr_point);
989
990 EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb), FIRST_PSEUDO_REGISTER, j, bi)
991 {
992 if (sparseset_cardinality (pseudos_live_through_calls) == 0)
993 break;
994 if (sparseset_bit_p (pseudos_live_through_calls, j))
995 check_pseudos_live_through_calls (j);
996 }
8160cd3e 997
55a2c322
VM
998 if (need_curr_point_incr)
999 next_program_point (curr_point, freq);
8160cd3e
VM
1000
1001 return live_change_p;
55a2c322
VM
1002}
1003
1004/* Compress pseudo live ranges by removing program points where
1005 nothing happens. Complexity of many algorithms in LRA is linear
1006 function of program points number. To speed up the code we try to
1007 minimize the number of the program points here. */
1008static void
1009remove_some_program_points_and_update_live_ranges (void)
1010{
1011 unsigned i;
1012 int n, max_regno;
1013 int *map;
1014 lra_live_range_t r, prev_r, next_r;
1015 sbitmap born_or_dead, born, dead;
1016 sbitmap_iterator sbi;
1017 bool born_p, dead_p, prev_born_p, prev_dead_p;
1018
1019 born = sbitmap_alloc (lra_live_max_point);
1020 dead = sbitmap_alloc (lra_live_max_point);
f61e445a
LC
1021 bitmap_clear (born);
1022 bitmap_clear (dead);
55a2c322
VM
1023 max_regno = max_reg_num ();
1024 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
1025 {
1026 for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
1027 {
1028 lra_assert (r->start <= r->finish);
d7c028c0
LC
1029 bitmap_set_bit (born, r->start);
1030 bitmap_set_bit (dead, r->finish);
55a2c322
VM
1031 }
1032 }
1033 born_or_dead = sbitmap_alloc (lra_live_max_point);
f61e445a 1034 bitmap_ior (born_or_dead, born, dead);
55a2c322
VM
1035 map = XCNEWVEC (int, lra_live_max_point);
1036 n = -1;
1037 prev_born_p = prev_dead_p = false;
d4ac4ce2 1038 EXECUTE_IF_SET_IN_BITMAP (born_or_dead, 0, i, sbi)
55a2c322 1039 {
d7c028c0
LC
1040 born_p = bitmap_bit_p (born, i);
1041 dead_p = bitmap_bit_p (dead, i);
55a2c322
VM
1042 if ((prev_born_p && ! prev_dead_p && born_p && ! dead_p)
1043 || (prev_dead_p && ! prev_born_p && dead_p && ! born_p))
1044 {
1045 map[i] = n;
1046 lra_point_freq[n] = MAX (lra_point_freq[n], lra_point_freq[i]);
1047 }
1048 else
1049 {
1050 map[i] = ++n;
1051 lra_point_freq[n] = lra_point_freq[i];
1052 }
1053 prev_born_p = born_p;
1054 prev_dead_p = dead_p;
1055 }
1056 sbitmap_free (born_or_dead);
1057 sbitmap_free (born);
1058 sbitmap_free (dead);
1059 n++;
1060 if (lra_dump_file != NULL)
1061 fprintf (lra_dump_file, "Compressing live ranges: from %d to %d - %d%%\n",
1062 lra_live_max_point, n, 100 * n / lra_live_max_point);
1063 if (n < lra_live_max_point)
1064 {
1065 lra_live_max_point = n;
1066 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
1067 {
1068 for (prev_r = NULL, r = lra_reg_info[i].live_ranges;
1069 r != NULL;
1070 r = next_r)
1071 {
1072 next_r = r->next;
1073 r->start = map[r->start];
1074 r->finish = map[r->finish];
1075 if (prev_r == NULL || prev_r->start > r->finish + 1)
1076 {
1077 prev_r = r;
1078 continue;
1079 }
1080 prev_r->start = r->start;
1081 prev_r->next = next_r;
1082 free_live_range (r);
1083 }
1084 }
1085 }
1086 free (map);
1087}
1088
1089/* Print live ranges R to file F. */
1090void
1091lra_print_live_range_list (FILE *f, lra_live_range_t r)
1092{
1093 for (; r != NULL; r = r->next)
1094 fprintf (f, " [%d..%d]", r->start, r->finish);
1095 fprintf (f, "\n");
1096}
1097
7b3b6ae4
LC
1098DEBUG_FUNCTION void
1099debug (lra_live_range &ref)
1100{
1101 lra_print_live_range_list (stderr, &ref);
1102}
1103
1104DEBUG_FUNCTION void
1105debug (lra_live_range *ptr)
1106{
1107 if (ptr)
1108 debug (*ptr);
1109 else
1110 fprintf (stderr, "<nil>\n");
1111}
1112
55a2c322
VM
1113/* Print live ranges R to stderr. */
1114void
1115lra_debug_live_range_list (lra_live_range_t r)
1116{
1117 lra_print_live_range_list (stderr, r);
1118}
1119
1120/* Print live ranges of pseudo REGNO to file F. */
1121static void
1122print_pseudo_live_ranges (FILE *f, int regno)
1123{
1124 if (lra_reg_info[regno].live_ranges == NULL)
1125 return;
1126 fprintf (f, " r%d:", regno);
1127 lra_print_live_range_list (f, lra_reg_info[regno].live_ranges);
1128}
1129
1130/* Print live ranges of pseudo REGNO to stderr. */
1131void
1132lra_debug_pseudo_live_ranges (int regno)
1133{
1134 print_pseudo_live_ranges (stderr, regno);
1135}
1136
1137/* Print live ranges of all pseudos to file F. */
1138static void
1139print_live_ranges (FILE *f)
1140{
1141 int i, max_regno;
1142
1143 max_regno = max_reg_num ();
1144 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1145 print_pseudo_live_ranges (f, i);
1146}
1147
1148/* Print live ranges of all pseudos to stderr. */
1149void
1150lra_debug_live_ranges (void)
1151{
1152 print_live_ranges (stderr);
1153}
1154
1155/* Compress pseudo live ranges. */
1156static void
1157compress_live_ranges (void)
1158{
1159 remove_some_program_points_and_update_live_ranges ();
1160 if (lra_dump_file != NULL)
1161 {
1162 fprintf (lra_dump_file, "Ranges after the compression:\n");
1163 print_live_ranges (lra_dump_file);
1164 }
1165}
1166
8160cd3e
VM
1167\f
1168
55a2c322
VM
1169/* The number of the current live range pass. */
1170int lra_live_range_iter;
1171
18ea3d61
VM
1172/* The function creates live ranges only for memory pseudos (or for
1173 all ones if ALL_P), set up CONFLICT_HARD_REGS for the pseudos. It
1174 also does dead insn elimination if DEAD_INSN_P and global live
1175 analysis only for pseudos and only if the pseudo live info was
1176 changed on a BB border. Return TRUE if the live info was
1177 changed. */
1178static bool
1179lra_create_live_ranges_1 (bool all_p, bool dead_insn_p)
55a2c322
VM
1180{
1181 basic_block bb;
1182 int i, hard_regno, max_regno = max_reg_num ();
1183 int curr_point;
8160cd3e 1184 bool bb_live_change_p, have_referenced_pseudos = false;
55a2c322
VM
1185
1186 timevar_push (TV_LRA_CREATE_LIVE_RANGES);
1187
1188 complete_info_p = all_p;
1189 if (lra_dump_file != NULL)
1190 fprintf (lra_dump_file,
1191 "\n********** Pseudo live ranges #%d: **********\n\n",
1192 ++lra_live_range_iter);
1193 memset (lra_hard_reg_usage, 0, sizeof (lra_hard_reg_usage));
1194 for (i = 0; i < max_regno; i++)
1195 {
1196 lra_reg_info[i].live_ranges = NULL;
1197 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1198 lra_reg_info[i].preferred_hard_regno1 = -1;
1199 lra_reg_info[i].preferred_hard_regno2 = -1;
1200 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1201 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1202#ifdef STACK_REGS
1203 lra_reg_info[i].no_stack_p = false;
1204#endif
b28ece32
VM
1205 /* The biggest mode is already set but its value might be to
1206 conservative because of recent transformation. Here in this
1207 file we recalculate it again as it costs practically
1208 nothing. */
55a2c322
VM
1209 if (regno_reg_rtx[i] != NULL_RTX)
1210 lra_reg_info[i].biggest_mode = GET_MODE (regno_reg_rtx[i]);
1211 else
1212 lra_reg_info[i].biggest_mode = VOIDmode;
1213#ifdef ENABLE_CHECKING
1214 lra_reg_info[i].call_p = false;
1215#endif
1216 if (i >= FIRST_PSEUDO_REGISTER
85f9ce67
SB
1217 && lra_reg_info[i].nrefs != 0)
1218 {
1219 if ((hard_regno = reg_renumber[i]) >= 0)
1220 lra_hard_reg_usage[hard_regno] += lra_reg_info[i].freq;
1221 have_referenced_pseudos = true;
1222 }
55a2c322
VM
1223 }
1224 lra_free_copies ();
85f9ce67
SB
1225
1226 /* Under some circumstances, we can have functions without pseudo
1227 registers. For such functions, lra_live_max_point will be 0,
1228 see e.g. PR55604, and there's nothing more to do for us here. */
1229 if (! have_referenced_pseudos)
1230 {
1231 timevar_pop (TV_LRA_CREATE_LIVE_RANGES);
18ea3d61 1232 return false;
85f9ce67
SB
1233 }
1234
55a2c322
VM
1235 pseudos_live = sparseset_alloc (max_regno);
1236 pseudos_live_through_calls = sparseset_alloc (max_regno);
1237 pseudos_live_through_setjumps = sparseset_alloc (max_regno);
1238 start_living = sparseset_alloc (max_regno);
1239 start_dying = sparseset_alloc (max_regno);
1240 dead_set = sparseset_alloc (max_regno);
1241 unused_set = sparseset_alloc (max_regno);
1242 curr_point = 0;
9771b263
DN
1243 point_freq_vec.create (get_max_uid () * 2);
1244 lra_point_freq = point_freq_vec.address ();
8b1c6fd7 1245 int *post_order_rev_cfg = XNEWVEC (int, last_basic_block_for_fn (cfun));
55a2c322 1246 int n_blocks_inverted = inverted_post_order_compute (post_order_rev_cfg);
0cae8d31 1247 lra_assert (n_blocks_inverted == n_basic_blocks_for_fn (cfun));
8160cd3e 1248 bb_live_change_p = false;
55a2c322
VM
1249 for (i = n_blocks_inverted - 1; i >= 0; --i)
1250 {
06e28de2 1251 bb = BASIC_BLOCK_FOR_FN (cfun, post_order_rev_cfg[i]);
fefa31b5
DM
1252 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb
1253 == ENTRY_BLOCK_PTR_FOR_FN (cfun))
55a2c322 1254 continue;
18ea3d61 1255 if (process_bb_lives (bb, curr_point, dead_insn_p))
8160cd3e
VM
1256 bb_live_change_p = true;
1257 }
1258 if (bb_live_change_p)
1259 {
1260 /* We need to clear pseudo live info as some pseudos can
1261 disappear, e.g. pseudos with used equivalences. */
1262 FOR_EACH_BB_FN (bb, cfun)
1263 {
1264 bitmap_clear_range (df_get_live_in (bb), FIRST_PSEUDO_REGISTER,
1265 max_regno - FIRST_PSEUDO_REGISTER);
1266 bitmap_clear_range (df_get_live_out (bb), FIRST_PSEUDO_REGISTER,
1267 max_regno - FIRST_PSEUDO_REGISTER);
1268 }
1269 /* As we did not change CFG since LRA start we can use
1270 DF-infrastructure solver to solve live data flow problem. */
1271 df_simple_dataflow
1272 (DF_BACKWARD, NULL, live_con_fun_0, live_con_fun_n,
1273 live_trans_fun, &all_blocks,
1274 df_get_postorder (DF_BACKWARD), df_get_n_blocks (DF_BACKWARD));
1275 if (lra_dump_file != NULL)
1276 {
6750565c
VM
1277 fprintf (lra_dump_file,
1278 "Global pseudo live data have been updated:\n");
8160cd3e
VM
1279 basic_block bb;
1280 FOR_EACH_BB_FN (bb, cfun)
1281 {
1282 bb_data_t bb_info = get_bb_data (bb);
1283 bitmap bb_livein = df_get_live_in (bb);
1284 bitmap bb_liveout = df_get_live_out (bb);
1285
1286 fprintf (lra_dump_file, "\nBB %d:\n", bb->index);
1287 lra_dump_bitmap_with_title (" gen:",
1288 &bb_info->gen_pseudos, bb->index);
1289 lra_dump_bitmap_with_title (" killed:",
1290 &bb_info->killed_pseudos, bb->index);
1291 lra_dump_bitmap_with_title (" livein:", bb_livein, bb->index);
1292 lra_dump_bitmap_with_title (" liveout:", bb_liveout, bb->index);
1293 }
1294 }
55a2c322
VM
1295 }
1296 free (post_order_rev_cfg);
1297 lra_live_max_point = curr_point;
1298 if (lra_dump_file != NULL)
1299 print_live_ranges (lra_dump_file);
1300 /* Clean up. */
1301 sparseset_free (unused_set);
1302 sparseset_free (dead_set);
1303 sparseset_free (start_dying);
1304 sparseset_free (start_living);
1305 sparseset_free (pseudos_live_through_calls);
1306 sparseset_free (pseudos_live_through_setjumps);
1307 sparseset_free (pseudos_live);
1308 compress_live_ranges ();
1309 timevar_pop (TV_LRA_CREATE_LIVE_RANGES);
18ea3d61
VM
1310 return bb_live_change_p;
1311}
1312
1313/* The main entry function creates live-ranges and other live info
1314 necessary for the assignment sub-pass. It uses
1315 lra_creates_live_ranges_1 -- so read comments for the
1316 function. */
1317void
1318lra_create_live_ranges (bool all_p, bool dead_insn_p)
1319{
1320 if (! lra_create_live_ranges_1 (all_p, dead_insn_p))
1321 return;
1322 if (lra_dump_file != NULL)
1323 fprintf (lra_dump_file, "Live info was changed -- recalculate it\n");
1324 /* Live info was changed on a bb border. It means that some info,
1325 e.g. about conflict regs, calls crossed may be wrong, live
1326 ranges. We need this info for allocation. So recalcualate it
1327 again. */
1328 lra_clear_live_ranges ();
1329 bool res = lra_create_live_ranges_1 (all_p, dead_insn_p);
1330 lra_assert (! res);
55a2c322
VM
1331}
1332
1333/* Finish all live ranges. */
1334void
1335lra_clear_live_ranges (void)
1336{
1337 int i;
1338
1339 for (i = 0; i < max_reg_num (); i++)
1340 free_live_range_list (lra_reg_info[i].live_ranges);
9771b263 1341 point_freq_vec.release ();
55a2c322
VM
1342}
1343
1344/* Initialize live ranges data once per function. */
1345void
1346lra_live_ranges_init (void)
1347{
1348 live_range_pool = create_alloc_pool ("live ranges",
1349 sizeof (struct lra_live_range), 100);
4ab74a01 1350 bitmap_initialize (&temp_bitmap, &reg_obstack);
8160cd3e 1351 initiate_live_solver ();
55a2c322
VM
1352}
1353
1354/* Finish live ranges data once per function. */
1355void
1356lra_live_ranges_finish (void)
1357{
8160cd3e 1358 finish_live_solver ();
4ab74a01 1359 bitmap_clear (&temp_bitmap);
55a2c322
VM
1360 free_alloc_pool (live_range_pool);
1361}