]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lra-lives.c
Daily bump.
[thirdparty/gcc.git] / gcc / lra-lives.c
CommitLineData
55a2c322 1/* Build live ranges for pseudos.
5624e564 2 Copyright (C) 2010-2015 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"
39#include "function.h"
40e23961 40#include "symtab.h"
36566b39 41#include "flags.h"
36566b39 42#include "alias.h"
36566b39
PK
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"
55a2c322 51#include "expr.h"
60393bbc
AM
52#include "predict.h"
53#include "dominance.h"
54#include "cfg.h"
55#include "cfganal.h"
55a2c322
VM
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
4ab74a01
VM
109/* Bitmap used for holding intermediate bitmap operation results. */
110static bitmap_head temp_bitmap;
111
55a2c322 112/* Pool for pseudo live ranges. */
cb8abb1c 113pool_allocator <lra_live_range> lra_live_range::pool ("live ranges", 100);
55a2c322
VM
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;
cb8abb1c 124 delete lr;
55a2c322
VM
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{
cb8abb1c 133 lra_live_range_t p = new lra_live_range;
55a2c322
VM
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{
cb8abb1c 145 return new lra_live_range (*r);
55a2c322
VM
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{
fab27f52 171 lra_live_range_t first, last;
55a2c322
VM
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)
fab27f52
MM
180 std::swap (r1, r2);
181
55a2c322
VM
182 if (r1->start == r2->finish + 1)
183 {
184 /* Joint ranges: merge r1 and r2 into r1. */
185 r1->start = r2->start;
fab27f52 186 lra_live_range_t temp = r2;
55a2c322 187 r2 = r2->next;
cb8abb1c 188 delete temp;
55a2c322
VM
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
5c8bae59
VM
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. */
55a2c322 244static void
5c8bae59 245make_hard_regno_born (int regno, bool check_pic_pseudo_p ATTRIBUTE_UNUSED)
55a2c322
VM
246{
247 unsigned int i;
248
249 lra_assert (regno < FIRST_PSEUDO_REGISTER);
d9cf932c 250 if (TEST_HARD_REG_BIT (hard_regs_live, regno))
55a2c322
VM
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)
5c8bae59
VM
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);
55a2c322
VM
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);
d9cf932c 270 if (! TEST_HARD_REG_BIT (hard_regs_live, regno))
55a2c322
VM
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
8160cd3e
VM
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
18ea3d61 322 program point POINT. Update BB_GEN_PSEUDOS.
8160cd3e
VM
323 Return TRUE if the liveness tracking sets were modified, or FALSE
324 if nothing changed. */
55a2c322 325static bool
18ea3d61 326mark_regno_live (int regno, machine_mode mode, int point)
55a2c322
VM
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++)
5c8bae59 336 make_hard_regno_born (regno, false);
55a2c322 337 }
8160cd3e 338 else
55a2c322 339 {
8160cd3e
VM
340 if (! sparseset_bit_p (pseudos_live, regno))
341 {
342 mark_pseudo_live (regno, point);
343 changed = true;
344 }
18ea3d61 345 bitmap_set_bit (bb_gen_pseudos, regno);
55a2c322
VM
346 }
347 return changed;
348}
349
350
8160cd3e 351/* Mark register REGNO in MODE as dead at program point POINT. Update
18ea3d61
VM
352 BB_GEN_PSEUDOS and BB_KILLED_PSEUDOS. Return TRUE if the liveness
353 tracking sets were modified, or FALSE if nothing changed. */
55a2c322 354static bool
18ea3d61 355mark_regno_dead (int regno, machine_mode mode, int point)
55a2c322
VM
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 }
8160cd3e 367 else
55a2c322 368 {
8160cd3e
VM
369 if (sparseset_bit_p (pseudos_live, regno))
370 {
371 mark_pseudo_dead (regno, point);
372 changed = true;
373 }
18ea3d61
VM
374 bitmap_clear_bit (bb_gen_pseudos, regno);
375 bitmap_set_bit (bb_killed_pseudos, regno);
55a2c322
VM
376 }
377 return changed;
378}
379
8160cd3e
VM
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. */
152914cd 389struct bb_data_pseudos
8160cd3e
VM
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. */
152914cd 398typedef struct bb_data_pseudos *bb_data_t;
8160cd3e
VM
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
8160cd3e
VM
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);
cb8abb1c 459
8160cd3e
VM
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{
8160cd3e
VM
472 bitmap_initialize (&all_hard_regs_bitmap, &reg_obstack);
473 bitmap_set_range (&all_hard_regs_bitmap, 0, FIRST_PSEUDO_REGISTER);
152914cd 474 bb_data = XNEWVEC (struct bb_data_pseudos, last_basic_block_for_fn (cfun));
8160cd3e
VM
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);
8160cd3e
VM
503}
504
505\f
506
55a2c322 507/* Insn currently scanned. */
cfa434f6 508static rtx_insn *curr_insn;
55a2c322
VM
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. */
9771b263 531static vec<int> point_freq_vec;
55a2c322
VM
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{
9771b263
DN
541 point_freq_vec.safe_push (freq);
542 lra_point_freq = point_freq_vec.address ();
55a2c322
VM
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 {
6b4db501
MM
575 std::swap (lra_reg_info[regno].preferred_hard_regno1,
576 lra_reg_info[regno].preferred_hard_regno2);
577 std::swap (lra_reg_info[regno].preferred_hard_regno_profit1,
578 lra_reg_info[regno].preferred_hard_regno_profit2);
55a2c322
VM
579 }
580 if (lra_dump_file != NULL)
581 {
582 if ((hard_regno = lra_reg_info[regno].preferred_hard_regno1) >= 0)
583 fprintf (lra_dump_file,
584 " Hard reg %d is preferable by r%d with profit %d\n",
585 hard_regno, regno,
586 lra_reg_info[regno].preferred_hard_regno_profit1);
587 if ((hard_regno = lra_reg_info[regno].preferred_hard_regno2) >= 0)
588 fprintf (lra_dump_file,
589 " Hard reg %d is preferable by r%d with profit %d\n",
590 hard_regno, regno,
591 lra_reg_info[regno].preferred_hard_regno_profit2);
592 }
593}
594
595/* Check that REGNO living through calls and setjumps, set up conflict
596 regs, and clear corresponding bits in PSEUDOS_LIVE_THROUGH_CALLS and
597 PSEUDOS_LIVE_THROUGH_SETJUMPS. */
598static inline void
599check_pseudos_live_through_calls (int regno)
600{
8a26ad39
VM
601 int hr;
602
55a2c322
VM
603 if (! sparseset_bit_p (pseudos_live_through_calls, regno))
604 return;
605 sparseset_clear_bit (pseudos_live_through_calls, regno);
606 IOR_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs,
261cb0d3 607 call_used_reg_set);
8a26ad39
VM
608
609 for (hr = 0; hr < FIRST_PSEUDO_REGISTER; hr++)
610 if (HARD_REGNO_CALL_PART_CLOBBERED (hr, PSEUDO_REGNO_MODE (regno)))
611 SET_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hr);
55a2c322
VM
612#ifdef ENABLE_CHECKING
613 lra_reg_info[regno].call_p = true;
614#endif
615 if (! sparseset_bit_p (pseudos_live_through_setjumps, regno))
616 return;
617 sparseset_clear_bit (pseudos_live_through_setjumps, regno);
618 /* Don't allocate pseudos that cross setjmps or any call, if this
619 function receives a nonlocal goto. */
620 SET_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs);
621}
622
623/* Process insns of the basic block BB to update pseudo live ranges,
624 pseudo hard register conflicts, and insn notes. We do it on
625 backward scan of BB insns. CURR_POINT is the program point where
626 BB ends. The function updates this counter and returns in
8160cd3e 627 CURR_POINT the program point where BB starts. The function also
4ab74a01 628 does local live info updates and can delete the dead insns if
18ea3d61 629 DEAD_INSN_P. It returns true if pseudo live info was
8160cd3e
VM
630 changed at the BB start. */
631static bool
18ea3d61 632process_bb_lives (basic_block bb, int &curr_point, bool dead_insn_p)
55a2c322
VM
633{
634 int i, regno, freq;
635 unsigned int j;
636 bitmap_iterator bi;
637 bitmap reg_live_out;
638 unsigned int px;
8160cd3e 639 rtx_insn *next;
55a2c322
VM
640 rtx link, *link_loc;
641 bool need_curr_point_incr;
642
643 reg_live_out = df_get_live_out (bb);
644 sparseset_clear (pseudos_live);
645 sparseset_clear (pseudos_live_through_calls);
646 sparseset_clear (pseudos_live_through_setjumps);
647 REG_SET_TO_HARD_REG_SET (hard_regs_live, reg_live_out);
648 AND_COMPL_HARD_REG_SET (hard_regs_live, eliminable_regset);
55a2c322
VM
649 EXECUTE_IF_SET_IN_BITMAP (reg_live_out, FIRST_PSEUDO_REGISTER, j, bi)
650 mark_pseudo_live (j, curr_point);
651
18ea3d61
VM
652 bb_gen_pseudos = &get_bb_data (bb)->gen_pseudos;
653 bb_killed_pseudos = &get_bb_data (bb)->killed_pseudos;
654 bitmap_clear (bb_gen_pseudos);
655 bitmap_clear (bb_killed_pseudos);
55a2c322
VM
656 freq = REG_FREQ_FROM_BB (bb);
657
658 if (lra_dump_file != NULL)
659 fprintf (lra_dump_file, " BB %d\n", bb->index);
660
661 /* Scan the code of this basic block, noting which pseudos and hard
662 regs are born or die.
663
664 Note that this loop treats uninitialized values as live until the
665 beginning of the block. For example, if an instruction uses
666 (reg:DI foo), and only (subreg:SI (reg:DI foo) 0) is ever set,
667 FOO will remain live until the beginning of the block. Likewise
668 if FOO is not set at all. This is unnecessarily pessimistic, but
669 it probably doesn't matter much in practice. */
8160cd3e 670 FOR_BB_INSNS_REVERSE_SAFE (bb, curr_insn, next)
55a2c322
VM
671 {
672 bool call_p;
673 int dst_regno, src_regno;
674 rtx set;
675 struct lra_insn_reg *reg;
676
677 if (!NONDEBUG_INSN_P (curr_insn))
678 continue;
679
680 curr_id = lra_get_insn_recog_data (curr_insn);
681 curr_static_id = curr_id->insn_static_data;
682 if (lra_dump_file != NULL)
683 fprintf (lra_dump_file, " Insn %u: point = %d\n",
684 INSN_UID (curr_insn), curr_point);
685
8160cd3e
VM
686 set = single_set (curr_insn);
687
18ea3d61 688 if (dead_insn_p && set != NULL_RTX
6750565c
VM
689 && REG_P (SET_DEST (set)) && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
690 && find_reg_note (curr_insn, REG_EH_REGION, NULL_RTX) == NULL_RTX
691 && ! may_trap_p (PATTERN (curr_insn))
692 /* Don't do premature remove of pic offset pseudo as we can
693 start to use it after some reload generation. */
694 && (pic_offset_table_rtx == NULL_RTX
695 || pic_offset_table_rtx != SET_DEST (set)))
8160cd3e 696 {
18ea3d61 697 bool remove_p = true;
8160cd3e
VM
698
699 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
700 if (reg->type != OP_IN && sparseset_bit_p (pseudos_live, reg->regno))
701 {
18ea3d61 702 remove_p = false;
8160cd3e
VM
703 break;
704 }
705 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
706 if (reg->type != OP_IN)
707 {
18ea3d61 708 remove_p = false;
8160cd3e
VM
709 break;
710 }
18ea3d61 711 if (remove_p && ! volatile_refs_p (PATTERN (curr_insn)))
8160cd3e
VM
712 {
713 dst_regno = REGNO (SET_DEST (set));
714 if (lra_dump_file != NULL)
715 fprintf (lra_dump_file, " Deleting dead insn %u\n",
716 INSN_UID (curr_insn));
717 lra_set_insn_deleted (curr_insn);
718 if (lra_reg_info[dst_regno].nrefs == 0)
719 {
720 /* There might be some debug insns with the pseudo. */
721 unsigned int uid;
722 rtx_insn *insn;
723
4ab74a01
VM
724 bitmap_copy (&temp_bitmap, &lra_reg_info[dst_regno].insn_bitmap);
725 EXECUTE_IF_SET_IN_BITMAP (&temp_bitmap, 0, uid, bi)
8160cd3e
VM
726 {
727 insn = lra_insn_recog_data[uid]->insn;
728 lra_substitute_pseudo_within_insn (insn, dst_regno,
ef87312e 729 SET_SRC (set), true);
8160cd3e
VM
730 lra_update_insn_regno_info (insn);
731 }
732 }
733 continue;
734 }
735 }
736
55a2c322
VM
737 /* Update max ref width and hard reg usage. */
738 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
739 if (reg->regno >= FIRST_PSEUDO_REGISTER
740 && (GET_MODE_SIZE (reg->biggest_mode)
741 > GET_MODE_SIZE (lra_reg_info[reg->regno].biggest_mode)))
742 lra_reg_info[reg->regno].biggest_mode = reg->biggest_mode;
743 else if (reg->regno < FIRST_PSEUDO_REGISTER)
744 lra_hard_reg_usage[reg->regno] += freq;
745
746 call_p = CALL_P (curr_insn);
747 if (complete_info_p
8160cd3e 748 && set != NULL_RTX
55a2c322
VM
749 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set))
750 /* Check that source regno does not conflict with
751 destination regno to exclude most impossible
752 preferences. */
753 && ((((src_regno = REGNO (SET_SRC (set))) >= FIRST_PSEUDO_REGISTER
754 && ! sparseset_bit_p (pseudos_live, src_regno))
755 || (src_regno < FIRST_PSEUDO_REGISTER
756 && ! TEST_HARD_REG_BIT (hard_regs_live, src_regno)))
757 /* It might be 'inheritance pseudo <- reload pseudo'. */
758 || (src_regno >= lra_constraint_new_regno_start
759 && ((int) REGNO (SET_DEST (set))
debd8f30
CLT
760 >= lra_constraint_new_regno_start)
761 /* Remember to skip special cases where src/dest regnos are
762 the same, e.g. insn SET pattern has matching constraints
763 like =r,0. */
764 && src_regno != (int) REGNO (SET_DEST (set)))))
55a2c322
VM
765 {
766 int hard_regno = -1, regno = -1;
767
768 dst_regno = REGNO (SET_DEST (set));
769 if (dst_regno >= lra_constraint_new_regno_start
770 && src_regno >= lra_constraint_new_regno_start)
a42e72d1
VM
771 {
772 /* It might be still an original (non-reload) insn with
773 one unused output and a constraint requiring to use
774 the same reg for input/output operands. In this case
775 dst_regno and src_regno have the same value, we don't
776 need a misleading copy for this case. */
777 if (dst_regno != src_regno)
778 lra_create_copy (dst_regno, src_regno, freq);
779 }
55a2c322
VM
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)
5c8bae59 819 make_hard_regno_born (reg->regno, false);
55a2c322
VM
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 {
1e288103 838 if (flag_ipa_ra)
10e1bdb2
TV
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)
5c8bae59 878 make_hard_regno_born (reg->regno, false);
55a2c322
VM
879
880 if (curr_id->arg_hard_regs != NULL)
5c8bae59
VM
881 /* Make argument hard registers live. Don't create conflict
882 of used REAL_PIC_OFFSET_TABLE_REGNUM and the pic pseudo. */
55a2c322 883 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5c8bae59 884 make_hard_regno_born (regno, true);
55a2c322
VM
885
886 sparseset_and_compl (dead_set, start_living, start_dying);
887
888 /* Mark early clobber outputs dead. */
889 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
890 if (reg->type == OP_OUT && reg->early_clobber && ! reg->subreg_p)
4ab74a01
VM
891 need_curr_point_incr
892 |= mark_regno_dead (reg->regno, reg->biggest_mode,
18ea3d61 893 curr_point);
55a2c322
VM
894
895 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
896 if (reg->type == OP_OUT && reg->early_clobber && ! reg->subreg_p)
897 make_hard_regno_dead (reg->regno);
898
899 if (need_curr_point_incr)
900 next_program_point (curr_point, freq);
901
902 /* Update notes. */
903 for (link_loc = &REG_NOTES (curr_insn); (link = *link_loc) != NULL_RTX;)
904 {
905 if (REG_NOTE_KIND (link) != REG_DEAD
906 && REG_NOTE_KIND (link) != REG_UNUSED)
907 ;
908 else if (REG_P (XEXP (link, 0)))
909 {
910 regno = REGNO (XEXP (link, 0));
911 if ((REG_NOTE_KIND (link) == REG_DEAD
912 && ! sparseset_bit_p (dead_set, regno))
913 || (REG_NOTE_KIND (link) == REG_UNUSED
914 && ! sparseset_bit_p (unused_set, regno)))
915 {
916 *link_loc = XEXP (link, 1);
917 continue;
918 }
919 if (REG_NOTE_KIND (link) == REG_DEAD)
920 sparseset_clear_bit (dead_set, regno);
921 else if (REG_NOTE_KIND (link) == REG_UNUSED)
922 sparseset_clear_bit (unused_set, regno);
923 }
924 link_loc = &XEXP (link, 1);
925 }
926 EXECUTE_IF_SET_IN_SPARSESET (dead_set, j)
927 add_reg_note (curr_insn, REG_DEAD, regno_reg_rtx[j]);
928 EXECUTE_IF_SET_IN_SPARSESET (unused_set, j)
929 add_reg_note (curr_insn, REG_UNUSED, regno_reg_rtx[j]);
930 }
931
55a2c322
VM
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;
5c8bae59 939 make_hard_regno_born (regno, false);
55a2c322 940 }
55a2c322
VM
941
942 /* Pseudos can't go in stack regs at the start of a basic block that
943 is reached by an abnormal edge. Likewise for call clobbered regs,
944 because caller-save, fixup_abnormal_edges and possibly the table
945 driven EH machinery are not quite ready to handle such pseudos
946 live across such edges. */
947 if (bb_has_abnormal_pred (bb))
948 {
949#ifdef STACK_REGS
950 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, px)
951 lra_reg_info[px].no_stack_p = true;
952 for (px = FIRST_STACK_REG; px <= LAST_STACK_REG; px++)
5c8bae59 953 make_hard_regno_born (px, false);
55a2c322
VM
954#endif
955 /* No need to record conflicts for call clobbered regs if we
956 have nonlocal labels around, as we don't ever try to
957 allocate such regs in this case. */
958 if (!cfun->has_nonlocal_label && bb_has_abnormal_call_pred (bb))
959 for (px = 0; px < FIRST_PSEUDO_REGISTER; px++)
960 if (call_used_regs[px])
5c8bae59 961 make_hard_regno_born (px, false);
55a2c322
VM
962 }
963
8160cd3e 964 bool live_change_p = false;
18ea3d61
VM
965 /* Check if bb border live info was changed. */
966 unsigned int live_pseudos_num = 0;
967 EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb),
968 FIRST_PSEUDO_REGISTER, j, bi)
8160cd3e 969 {
18ea3d61
VM
970 live_pseudos_num++;
971 if (! sparseset_bit_p (pseudos_live, j))
8160cd3e 972 {
9503ade2
VM
973 live_change_p = true;
974 if (lra_dump_file != NULL)
975 fprintf (lra_dump_file,
976 " r%d is removed as live at bb%d start\n", j, bb->index);
18ea3d61 977 break;
8160cd3e
VM
978 }
979 }
9503ade2
VM
980 if (! live_change_p
981 && sparseset_cardinality (pseudos_live) != live_pseudos_num)
982 {
983 live_change_p = true;
984 if (lra_dump_file != NULL)
985 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, j)
986 if (! bitmap_bit_p (df_get_live_in (bb), j))
987 fprintf (lra_dump_file,
988 " r%d is added to live at bb%d start\n", j, bb->index);
989 }
55a2c322
VM
990 /* See if we'll need an increment at the end of this basic block.
991 An increment is needed if the PSEUDOS_LIVE set is not empty,
992 to make sure the finish points are set up correctly. */
993 need_curr_point_incr = (sparseset_cardinality (pseudos_live) > 0);
994
995 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, i)
996 mark_pseudo_dead (i, curr_point);
997
998 EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb), FIRST_PSEUDO_REGISTER, j, bi)
999 {
1000 if (sparseset_cardinality (pseudos_live_through_calls) == 0)
1001 break;
1002 if (sparseset_bit_p (pseudos_live_through_calls, j))
1003 check_pseudos_live_through_calls (j);
1004 }
cb8abb1c 1005
55a2c322
VM
1006 if (need_curr_point_incr)
1007 next_program_point (curr_point, freq);
8160cd3e
VM
1008
1009 return live_change_p;
55a2c322
VM
1010}
1011
1012/* Compress pseudo live ranges by removing program points where
1013 nothing happens. Complexity of many algorithms in LRA is linear
1014 function of program points number. To speed up the code we try to
1015 minimize the number of the program points here. */
1016static void
1017remove_some_program_points_and_update_live_ranges (void)
1018{
1019 unsigned i;
1020 int n, max_regno;
1021 int *map;
1022 lra_live_range_t r, prev_r, next_r;
1023 sbitmap born_or_dead, born, dead;
1024 sbitmap_iterator sbi;
1025 bool born_p, dead_p, prev_born_p, prev_dead_p;
1026
1027 born = sbitmap_alloc (lra_live_max_point);
1028 dead = sbitmap_alloc (lra_live_max_point);
f61e445a
LC
1029 bitmap_clear (born);
1030 bitmap_clear (dead);
55a2c322
VM
1031 max_regno = max_reg_num ();
1032 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
1033 {
1034 for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
1035 {
1036 lra_assert (r->start <= r->finish);
d7c028c0
LC
1037 bitmap_set_bit (born, r->start);
1038 bitmap_set_bit (dead, r->finish);
55a2c322
VM
1039 }
1040 }
1041 born_or_dead = sbitmap_alloc (lra_live_max_point);
f61e445a 1042 bitmap_ior (born_or_dead, born, dead);
55a2c322
VM
1043 map = XCNEWVEC (int, lra_live_max_point);
1044 n = -1;
1045 prev_born_p = prev_dead_p = false;
d4ac4ce2 1046 EXECUTE_IF_SET_IN_BITMAP (born_or_dead, 0, i, sbi)
55a2c322 1047 {
d7c028c0
LC
1048 born_p = bitmap_bit_p (born, i);
1049 dead_p = bitmap_bit_p (dead, i);
55a2c322
VM
1050 if ((prev_born_p && ! prev_dead_p && born_p && ! dead_p)
1051 || (prev_dead_p && ! prev_born_p && dead_p && ! born_p))
1052 {
1053 map[i] = n;
1054 lra_point_freq[n] = MAX (lra_point_freq[n], lra_point_freq[i]);
1055 }
1056 else
1057 {
1058 map[i] = ++n;
1059 lra_point_freq[n] = lra_point_freq[i];
1060 }
1061 prev_born_p = born_p;
1062 prev_dead_p = dead_p;
1063 }
1064 sbitmap_free (born_or_dead);
1065 sbitmap_free (born);
1066 sbitmap_free (dead);
1067 n++;
1068 if (lra_dump_file != NULL)
1069 fprintf (lra_dump_file, "Compressing live ranges: from %d to %d - %d%%\n",
1070 lra_live_max_point, n, 100 * n / lra_live_max_point);
1071 if (n < lra_live_max_point)
1072 {
1073 lra_live_max_point = n;
1074 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
1075 {
1076 for (prev_r = NULL, r = lra_reg_info[i].live_ranges;
1077 r != NULL;
1078 r = next_r)
1079 {
1080 next_r = r->next;
1081 r->start = map[r->start];
1082 r->finish = map[r->finish];
1083 if (prev_r == NULL || prev_r->start > r->finish + 1)
1084 {
1085 prev_r = r;
1086 continue;
1087 }
1088 prev_r->start = r->start;
1089 prev_r->next = next_r;
cb8abb1c 1090 delete r;
55a2c322
VM
1091 }
1092 }
1093 }
1094 free (map);
1095}
1096
1097/* Print live ranges R to file F. */
1098void
1099lra_print_live_range_list (FILE *f, lra_live_range_t r)
1100{
1101 for (; r != NULL; r = r->next)
1102 fprintf (f, " [%d..%d]", r->start, r->finish);
1103 fprintf (f, "\n");
1104}
1105
7b3b6ae4
LC
1106DEBUG_FUNCTION void
1107debug (lra_live_range &ref)
1108{
1109 lra_print_live_range_list (stderr, &ref);
1110}
1111
1112DEBUG_FUNCTION void
1113debug (lra_live_range *ptr)
1114{
1115 if (ptr)
1116 debug (*ptr);
1117 else
1118 fprintf (stderr, "<nil>\n");
1119}
1120
55a2c322
VM
1121/* Print live ranges R to stderr. */
1122void
1123lra_debug_live_range_list (lra_live_range_t r)
1124{
1125 lra_print_live_range_list (stderr, r);
1126}
1127
1128/* Print live ranges of pseudo REGNO to file F. */
1129static void
1130print_pseudo_live_ranges (FILE *f, int regno)
1131{
1132 if (lra_reg_info[regno].live_ranges == NULL)
1133 return;
1134 fprintf (f, " r%d:", regno);
1135 lra_print_live_range_list (f, lra_reg_info[regno].live_ranges);
1136}
1137
1138/* Print live ranges of pseudo REGNO to stderr. */
1139void
1140lra_debug_pseudo_live_ranges (int regno)
1141{
1142 print_pseudo_live_ranges (stderr, regno);
1143}
1144
1145/* Print live ranges of all pseudos to file F. */
1146static void
1147print_live_ranges (FILE *f)
1148{
1149 int i, max_regno;
1150
1151 max_regno = max_reg_num ();
1152 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1153 print_pseudo_live_ranges (f, i);
1154}
1155
1156/* Print live ranges of all pseudos to stderr. */
1157void
1158lra_debug_live_ranges (void)
1159{
1160 print_live_ranges (stderr);
1161}
1162
1163/* Compress pseudo live ranges. */
1164static void
1165compress_live_ranges (void)
1166{
1167 remove_some_program_points_and_update_live_ranges ();
1168 if (lra_dump_file != NULL)
1169 {
1170 fprintf (lra_dump_file, "Ranges after the compression:\n");
1171 print_live_ranges (lra_dump_file);
1172 }
1173}
1174
8160cd3e
VM
1175\f
1176
55a2c322
VM
1177/* The number of the current live range pass. */
1178int lra_live_range_iter;
1179
18ea3d61
VM
1180/* The function creates live ranges only for memory pseudos (or for
1181 all ones if ALL_P), set up CONFLICT_HARD_REGS for the pseudos. It
1182 also does dead insn elimination if DEAD_INSN_P and global live
1183 analysis only for pseudos and only if the pseudo live info was
1184 changed on a BB border. Return TRUE if the live info was
1185 changed. */
1186static bool
1187lra_create_live_ranges_1 (bool all_p, bool dead_insn_p)
55a2c322
VM
1188{
1189 basic_block bb;
1190 int i, hard_regno, max_regno = max_reg_num ();
1191 int curr_point;
8160cd3e 1192 bool bb_live_change_p, have_referenced_pseudos = false;
55a2c322
VM
1193
1194 timevar_push (TV_LRA_CREATE_LIVE_RANGES);
1195
1196 complete_info_p = all_p;
1197 if (lra_dump_file != NULL)
1198 fprintf (lra_dump_file,
1199 "\n********** Pseudo live ranges #%d: **********\n\n",
1200 ++lra_live_range_iter);
1201 memset (lra_hard_reg_usage, 0, sizeof (lra_hard_reg_usage));
1202 for (i = 0; i < max_regno; i++)
1203 {
1204 lra_reg_info[i].live_ranges = NULL;
1205 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1206 lra_reg_info[i].preferred_hard_regno1 = -1;
1207 lra_reg_info[i].preferred_hard_regno2 = -1;
1208 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1209 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1210#ifdef STACK_REGS
1211 lra_reg_info[i].no_stack_p = false;
1212#endif
b28ece32
VM
1213 /* The biggest mode is already set but its value might be to
1214 conservative because of recent transformation. Here in this
1215 file we recalculate it again as it costs practically
1216 nothing. */
55a2c322
VM
1217 if (regno_reg_rtx[i] != NULL_RTX)
1218 lra_reg_info[i].biggest_mode = GET_MODE (regno_reg_rtx[i]);
1219 else
1220 lra_reg_info[i].biggest_mode = VOIDmode;
1221#ifdef ENABLE_CHECKING
1222 lra_reg_info[i].call_p = false;
1223#endif
1224 if (i >= FIRST_PSEUDO_REGISTER
85f9ce67
SB
1225 && lra_reg_info[i].nrefs != 0)
1226 {
1227 if ((hard_regno = reg_renumber[i]) >= 0)
1228 lra_hard_reg_usage[hard_regno] += lra_reg_info[i].freq;
1229 have_referenced_pseudos = true;
1230 }
55a2c322
VM
1231 }
1232 lra_free_copies ();
cb8abb1c 1233
85f9ce67
SB
1234 /* Under some circumstances, we can have functions without pseudo
1235 registers. For such functions, lra_live_max_point will be 0,
1236 see e.g. PR55604, and there's nothing more to do for us here. */
1237 if (! have_referenced_pseudos)
1238 {
1239 timevar_pop (TV_LRA_CREATE_LIVE_RANGES);
18ea3d61 1240 return false;
85f9ce67
SB
1241 }
1242
55a2c322
VM
1243 pseudos_live = sparseset_alloc (max_regno);
1244 pseudos_live_through_calls = sparseset_alloc (max_regno);
1245 pseudos_live_through_setjumps = sparseset_alloc (max_regno);
1246 start_living = sparseset_alloc (max_regno);
1247 start_dying = sparseset_alloc (max_regno);
1248 dead_set = sparseset_alloc (max_regno);
1249 unused_set = sparseset_alloc (max_regno);
1250 curr_point = 0;
9771b263
DN
1251 point_freq_vec.create (get_max_uid () * 2);
1252 lra_point_freq = point_freq_vec.address ();
8b1c6fd7 1253 int *post_order_rev_cfg = XNEWVEC (int, last_basic_block_for_fn (cfun));
55a2c322 1254 int n_blocks_inverted = inverted_post_order_compute (post_order_rev_cfg);
0cae8d31 1255 lra_assert (n_blocks_inverted == n_basic_blocks_for_fn (cfun));
8160cd3e 1256 bb_live_change_p = false;
55a2c322
VM
1257 for (i = n_blocks_inverted - 1; i >= 0; --i)
1258 {
06e28de2 1259 bb = BASIC_BLOCK_FOR_FN (cfun, post_order_rev_cfg[i]);
fefa31b5
DM
1260 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb
1261 == ENTRY_BLOCK_PTR_FOR_FN (cfun))
55a2c322 1262 continue;
18ea3d61 1263 if (process_bb_lives (bb, curr_point, dead_insn_p))
8160cd3e
VM
1264 bb_live_change_p = true;
1265 }
1266 if (bb_live_change_p)
1267 {
1268 /* We need to clear pseudo live info as some pseudos can
1269 disappear, e.g. pseudos with used equivalences. */
1270 FOR_EACH_BB_FN (bb, cfun)
1271 {
1272 bitmap_clear_range (df_get_live_in (bb), FIRST_PSEUDO_REGISTER,
1273 max_regno - FIRST_PSEUDO_REGISTER);
1274 bitmap_clear_range (df_get_live_out (bb), FIRST_PSEUDO_REGISTER,
1275 max_regno - FIRST_PSEUDO_REGISTER);
1276 }
1277 /* As we did not change CFG since LRA start we can use
1278 DF-infrastructure solver to solve live data flow problem. */
1279 df_simple_dataflow
1280 (DF_BACKWARD, NULL, live_con_fun_0, live_con_fun_n,
1281 live_trans_fun, &all_blocks,
1282 df_get_postorder (DF_BACKWARD), df_get_n_blocks (DF_BACKWARD));
1283 if (lra_dump_file != NULL)
1284 {
6750565c
VM
1285 fprintf (lra_dump_file,
1286 "Global pseudo live data have been updated:\n");
8160cd3e
VM
1287 basic_block bb;
1288 FOR_EACH_BB_FN (bb, cfun)
1289 {
1290 bb_data_t bb_info = get_bb_data (bb);
1291 bitmap bb_livein = df_get_live_in (bb);
1292 bitmap bb_liveout = df_get_live_out (bb);
1293
1294 fprintf (lra_dump_file, "\nBB %d:\n", bb->index);
1295 lra_dump_bitmap_with_title (" gen:",
1296 &bb_info->gen_pseudos, bb->index);
1297 lra_dump_bitmap_with_title (" killed:",
1298 &bb_info->killed_pseudos, bb->index);
1299 lra_dump_bitmap_with_title (" livein:", bb_livein, bb->index);
1300 lra_dump_bitmap_with_title (" liveout:", bb_liveout, bb->index);
1301 }
1302 }
55a2c322
VM
1303 }
1304 free (post_order_rev_cfg);
1305 lra_live_max_point = curr_point;
1306 if (lra_dump_file != NULL)
1307 print_live_ranges (lra_dump_file);
1308 /* Clean up. */
1309 sparseset_free (unused_set);
1310 sparseset_free (dead_set);
1311 sparseset_free (start_dying);
1312 sparseset_free (start_living);
1313 sparseset_free (pseudos_live_through_calls);
1314 sparseset_free (pseudos_live_through_setjumps);
1315 sparseset_free (pseudos_live);
1316 compress_live_ranges ();
1317 timevar_pop (TV_LRA_CREATE_LIVE_RANGES);
18ea3d61
VM
1318 return bb_live_change_p;
1319}
1320
1321/* The main entry function creates live-ranges and other live info
1322 necessary for the assignment sub-pass. It uses
1323 lra_creates_live_ranges_1 -- so read comments for the
1324 function. */
1325void
1326lra_create_live_ranges (bool all_p, bool dead_insn_p)
1327{
1328 if (! lra_create_live_ranges_1 (all_p, dead_insn_p))
1329 return;
1330 if (lra_dump_file != NULL)
1331 fprintf (lra_dump_file, "Live info was changed -- recalculate it\n");
1332 /* Live info was changed on a bb border. It means that some info,
9503ade2
VM
1333 e.g. about conflict regs, calls crossed, and live ranges may be
1334 wrong. We need this info for allocation. So recalculate it
1335 again but without removing dead insns which can change live info
1336 again. Repetitive live range calculations are expensive therefore
1337 we stop here as we already have correct info although some
1338 improvement in rare cases could be possible on this sub-pass if
1339 we do dead insn elimination again (still the improvement may
1340 happen later). */
18ea3d61 1341 lra_clear_live_ranges ();
9503ade2 1342 bool res = lra_create_live_ranges_1 (all_p, false);
18ea3d61 1343 lra_assert (! res);
55a2c322
VM
1344}
1345
1346/* Finish all live ranges. */
1347void
1348lra_clear_live_ranges (void)
1349{
1350 int i;
1351
1352 for (i = 0; i < max_reg_num (); i++)
1353 free_live_range_list (lra_reg_info[i].live_ranges);
9771b263 1354 point_freq_vec.release ();
55a2c322
VM
1355}
1356
1357/* Initialize live ranges data once per function. */
1358void
1359lra_live_ranges_init (void)
1360{
4ab74a01 1361 bitmap_initialize (&temp_bitmap, &reg_obstack);
8160cd3e 1362 initiate_live_solver ();
55a2c322
VM
1363}
1364
1365/* Finish live ranges data once per function. */
1366void
1367lra_live_ranges_finish (void)
1368{
8160cd3e 1369 finish_live_solver ();
4ab74a01 1370 bitmap_clear (&temp_bitmap);
cb8abb1c 1371 lra_live_range::pool.release ();
55a2c322 1372}