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