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