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