]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lra-lives.c
C++: more location wrapper nodes (PR c++/43064, PR c++/43486)
[thirdparty/gcc.git] / gcc / lra-lives.c
CommitLineData
c6a6cdaa 1/* Build live ranges for pseudos.
8e8f6434 2 Copyright (C) 2010-2018 Free Software Foundation, Inc.
c6a6cdaa 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"
9ef16211 31#include "backend.h"
c6a6cdaa 32#include "rtl.h"
7c29e30e 33#include "tree.h"
34#include "predict.h"
9ef16211 35#include "df.h"
ad7b10a2 36#include "memmodel.h"
c6a6cdaa 37#include "tm_p.h"
38#include "insn-config.h"
7c29e30e 39#include "regs.h"
40#include "ira.h"
c6a6cdaa 41#include "recog.h"
94ea8568 42#include "cfganal.h"
c6a6cdaa 43#include "sparseset.h"
44#include "lra-int.h"
5da94e60 45#include "target.h"
c6a6cdaa 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
dd59ba43 86 in the insn. */
c6a6cdaa 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
04472658 93/* Bitmap used for holding intermediate bitmap operation results. */
94static bitmap_head temp_bitmap;
95
c6a6cdaa 96/* Pool for pseudo live ranges. */
1dc6c44d 97static object_allocator<lra_live_range> lra_live_range_pool ("live ranges");
c6a6cdaa 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;
b7cbf36d 108 lra_live_range_pool.remove (lr);
c6a6cdaa 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{
b7cbf36d 117 lra_live_range_t p = lra_live_range_pool.allocate ();
c6a6cdaa 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{
b7cbf36d 129 return new (lra_live_range_pool) lra_live_range (*r);
c6a6cdaa 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{
dfcf26a5 155 lra_live_range_t first, last;
c6a6cdaa 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)
dfcf26a5 164 std::swap (r1, r2);
165
c6a6cdaa 166 if (r1->start == r2->finish + 1)
167 {
168 /* Joint ranges: merge r1 and r2 into r1. */
169 r1->start = r2->start;
dfcf26a5 170 lra_live_range_t temp = r2;
c6a6cdaa 171 r2 = r2->next;
b7cbf36d 172 lra_live_range_pool.remove (temp);
c6a6cdaa 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
dd59ba43 223enum point_type {
224 DEF_POINT,
225 USE_POINT
226};
227
228/* Return TRUE if set A contains a pseudo register, otherwise, return FALSE. */
229static bool
230sparseset_contains_pseudos_p (sparseset a)
231{
232 int regno;
233 EXECUTE_IF_SET_IN_SPARSESET (a, regno)
234 if (!HARD_REGISTER_NUM_P (regno))
235 return true;
236 return false;
237}
238
239/* Mark pseudo REGNO as living or dying at program point POINT, depending on
240 whether TYPE is a definition or a use. If this is the first reference to
241 REGNO that we've encountered, then create a new live range for it. */
242
243static void
244update_pseudo_point (int regno, int point, enum point_type type)
245{
246 lra_live_range_t p;
247
248 /* Don't compute points for hard registers. */
249 if (HARD_REGISTER_NUM_P (regno))
250 return;
251
252 if (complete_info_p || lra_get_regno_hard_regno (regno) < 0)
253 {
254 if (type == DEF_POINT)
255 {
256 if (sparseset_bit_p (pseudos_live, regno))
257 {
258 p = lra_reg_info[regno].live_ranges;
259 lra_assert (p != NULL);
260 p->finish = point;
261 }
262 }
263 else /* USE_POINT */
264 {
265 if (!sparseset_bit_p (pseudos_live, regno)
266 && ((p = lra_reg_info[regno].live_ranges) == NULL
267 || (p->finish != point && p->finish + 1 != point)))
268 lra_reg_info[regno].live_ranges
269 = create_live_range (regno, point, -1, p);
270 }
271 }
272}
273
d106f8f5 274/* The corresponding bitmaps of BB currently being processed. */
275static bitmap bb_killed_pseudos, bb_gen_pseudos;
276
7e203840 277/* Record hard register REGNO as now being live. It updates
278 living hard regs and START_LIVING. */
c6a6cdaa 279static void
7e203840 280make_hard_regno_live (int regno)
c6a6cdaa 281{
dd59ba43 282 lra_assert (HARD_REGISTER_NUM_P (regno));
497ba60f 283 if (TEST_HARD_REG_BIT (hard_regs_live, regno))
c6a6cdaa 284 return;
285 SET_HARD_REG_BIT (hard_regs_live, regno);
286 sparseset_set_bit (start_living, regno);
95f18d43 287 if (fixed_regs[regno] || TEST_HARD_REG_BIT (hard_regs_spilled_into, regno))
d106f8f5 288 bitmap_set_bit (bb_gen_pseudos, regno);
c6a6cdaa 289}
290
7e203840 291/* Process the definition of hard register REGNO. This updates
292 hard_regs_live, START_DYING and conflict hard regs for living
4bcfedf5 293 pseudos. */
c6a6cdaa 294static void
4bcfedf5 295make_hard_regno_dead (int regno)
c6a6cdaa 296{
dd59ba43 297 lra_assert (HARD_REGISTER_NUM_P (regno));
7e203840 298 unsigned int i;
299 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, i)
dd59ba43 300 SET_HARD_REG_BIT (lra_reg_info[i].conflict_hard_regs, regno);
301
302 if (! TEST_HARD_REG_BIT (hard_regs_live, regno))
303 return;
c6a6cdaa 304 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
dd59ba43 305 sparseset_set_bit (start_dying, regno);
95f18d43 306 if (fixed_regs[regno] || TEST_HARD_REG_BIT (hard_regs_spilled_into, regno))
d106f8f5 307 {
308 bitmap_clear_bit (bb_gen_pseudos, regno);
309 bitmap_set_bit (bb_killed_pseudos, regno);
310 }
c6a6cdaa 311}
312
dd59ba43 313/* Mark pseudo REGNO as now being live and update START_LIVING. */
c6a6cdaa 314static void
dd59ba43 315mark_pseudo_live (int regno)
c6a6cdaa 316{
dd59ba43 317 lra_assert (!HARD_REGISTER_NUM_P (regno));
318 if (sparseset_bit_p (pseudos_live, regno))
319 return;
c6a6cdaa 320
c6a6cdaa 321 sparseset_set_bit (pseudos_live, regno);
c6a6cdaa 322 sparseset_set_bit (start_living, regno);
323}
324
dd59ba43 325/* Mark pseudo REGNO as now being dead and update START_DYING. */
c6a6cdaa 326static void
dd59ba43 327mark_pseudo_dead (int regno)
c6a6cdaa 328{
dd59ba43 329 lra_assert (!HARD_REGISTER_NUM_P (regno));
330 IOR_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs, hard_regs_live);
331 if (!sparseset_bit_p (pseudos_live, regno))
332 return;
c6a6cdaa 333
c6a6cdaa 334 sparseset_clear_bit (pseudos_live, regno);
335 sparseset_set_bit (start_dying, regno);
c6a6cdaa 336}
337
dd59ba43 338/* Mark register REGNO (pseudo or hard register) in MODE as being live
339 and update BB_GEN_PSEUDOS. */
340static void
341mark_regno_live (int regno, machine_mode mode)
c6a6cdaa 342{
343 int last;
c6a6cdaa 344
dd59ba43 345 if (HARD_REGISTER_NUM_P (regno))
c6a6cdaa 346 {
16b9e38b 347 for (last = end_hard_regno (mode, regno); regno < last; regno++)
7e203840 348 make_hard_regno_live (regno);
c6a6cdaa 349 }
8c0d01a4 350 else
c6a6cdaa 351 {
dd59ba43 352 mark_pseudo_live (regno);
d3ceeae2 353 bitmap_set_bit (bb_gen_pseudos, regno);
c6a6cdaa 354 }
c6a6cdaa 355}
356
357
dd59ba43 358/* Mark register REGNO (pseudo or hard register) in MODE as being dead
359 and update BB_GEN_PSEUDOS and BB_KILLED_PSEUDOS. */
360static void
361mark_regno_dead (int regno, machine_mode mode)
c6a6cdaa 362{
363 int last;
c6a6cdaa 364
dd59ba43 365 if (HARD_REGISTER_NUM_P (regno))
c6a6cdaa 366 {
16b9e38b 367 for (last = end_hard_regno (mode, regno); regno < last; regno++)
4bcfedf5 368 make_hard_regno_dead (regno);
c6a6cdaa 369 }
8c0d01a4 370 else
c6a6cdaa 371 {
dd59ba43 372 mark_pseudo_dead (regno);
d3ceeae2 373 bitmap_clear_bit (bb_gen_pseudos, regno);
374 bitmap_set_bit (bb_killed_pseudos, regno);
c6a6cdaa 375 }
c6a6cdaa 376}
377
8c0d01a4 378\f
379
380/* This page contains code for making global live analysis of pseudos.
381 The code works only when pseudo live info is changed on a BB
382 border. That might be a consequence of some global transformations
383 in LRA, e.g. PIC pseudo reuse or rematerialization. */
384
385/* Structure describing local BB data used for pseudo
386 live-analysis. */
49ce3f71 387struct bb_data_pseudos
8c0d01a4 388{
389 /* Basic block about which the below data are. */
390 basic_block bb;
391 bitmap_head killed_pseudos; /* pseudos killed in the BB. */
392 bitmap_head gen_pseudos; /* pseudos generated in the BB. */
393};
394
395/* Array for all BB data. Indexed by the corresponding BB index. */
49ce3f71 396typedef struct bb_data_pseudos *bb_data_t;
8c0d01a4 397
398/* All basic block data are referred through the following array. */
399static bb_data_t bb_data;
400
401/* Two small functions for access to the bb data. */
402static inline bb_data_t
403get_bb_data (basic_block bb)
404{
405 return &bb_data[(bb)->index];
406}
407
408static inline bb_data_t
409get_bb_data_by_index (int index)
410{
411 return &bb_data[index];
412}
413
414/* Bitmap with all hard regs. */
415static bitmap_head all_hard_regs_bitmap;
416
8c0d01a4 417/* The transfer function used by the DF equation solver to propagate
418 live info through block with BB_INDEX according to the following
419 equation:
420
421 bb.livein = (bb.liveout - bb.kill) OR bb.gen
422*/
423static bool
424live_trans_fun (int bb_index)
425{
426 basic_block bb = get_bb_data_by_index (bb_index)->bb;
427 bitmap bb_liveout = df_get_live_out (bb);
428 bitmap bb_livein = df_get_live_in (bb);
429 bb_data_t bb_info = get_bb_data (bb);
430
431 bitmap_and_compl (&temp_bitmap, bb_liveout, &all_hard_regs_bitmap);
432 return bitmap_ior_and_compl (bb_livein, &bb_info->gen_pseudos,
433 &temp_bitmap, &bb_info->killed_pseudos);
434}
435
436/* The confluence function used by the DF equation solver to set up
437 live info for a block BB without predecessor. */
438static void
439live_con_fun_0 (basic_block bb)
440{
441 bitmap_and_into (df_get_live_out (bb), &all_hard_regs_bitmap);
442}
443
444/* The confluence function used by the DF equation solver to propagate
445 live info from successor to predecessor on edge E according to the
446 following equation:
447
448 bb.liveout = 0 for entry block | OR (livein of successors)
449 */
450static bool
451live_con_fun_n (edge e)
452{
453 basic_block bb = e->src;
454 basic_block dest = e->dest;
455 bitmap bb_liveout = df_get_live_out (bb);
456 bitmap dest_livein = df_get_live_in (dest);
a940269d 457
8c0d01a4 458 return bitmap_ior_and_compl_into (bb_liveout,
459 dest_livein, &all_hard_regs_bitmap);
460}
461
462/* Indexes of all function blocks. */
463static bitmap_head all_blocks;
464
465/* Allocate and initialize data needed for global pseudo live
466 analysis. */
467static void
468initiate_live_solver (void)
469{
8c0d01a4 470 bitmap_initialize (&all_hard_regs_bitmap, &reg_obstack);
471 bitmap_set_range (&all_hard_regs_bitmap, 0, FIRST_PSEUDO_REGISTER);
49ce3f71 472 bb_data = XNEWVEC (struct bb_data_pseudos, last_basic_block_for_fn (cfun));
8c0d01a4 473 bitmap_initialize (&all_blocks, &reg_obstack);
474
475 basic_block bb;
476 FOR_ALL_BB_FN (bb, cfun)
477 {
478 bb_data_t bb_info = get_bb_data (bb);
479 bb_info->bb = bb;
480 bitmap_initialize (&bb_info->killed_pseudos, &reg_obstack);
481 bitmap_initialize (&bb_info->gen_pseudos, &reg_obstack);
482 bitmap_set_bit (&all_blocks, bb->index);
483 }
484}
485
486/* Free all data needed for global pseudo live analysis. */
487static void
488finish_live_solver (void)
489{
490 basic_block bb;
491
492 bitmap_clear (&all_blocks);
493 FOR_ALL_BB_FN (bb, cfun)
494 {
495 bb_data_t bb_info = get_bb_data (bb);
496 bitmap_clear (&bb_info->killed_pseudos);
497 bitmap_clear (&bb_info->gen_pseudos);
498 }
499 free (bb_data);
500 bitmap_clear (&all_hard_regs_bitmap);
8c0d01a4 501}
502
503\f
504
c6a6cdaa 505/* Insn currently scanned. */
7f836b57 506static rtx_insn *curr_insn;
c6a6cdaa 507/* The insn data. */
508static lra_insn_recog_data_t curr_id;
509/* The insn static data. */
510static struct lra_static_insn_data *curr_static_id;
511
c6a6cdaa 512/* Vec containing execution frequencies of program points. */
f1f41a6c 513static vec<int> point_freq_vec;
c6a6cdaa 514
515/* The start of the above vector elements. */
516int *lra_point_freq;
517
518/* Increment the current program point POINT to the next point which has
519 execution frequency FREQ. */
520static void
521next_program_point (int &point, int freq)
522{
f1f41a6c 523 point_freq_vec.safe_push (freq);
524 lra_point_freq = point_freq_vec.address ();
c6a6cdaa 525 point++;
526}
527
528/* Update the preference of HARD_REGNO for pseudo REGNO by PROFIT. */
529void
530lra_setup_reload_pseudo_preferenced_hard_reg (int regno,
531 int hard_regno, int profit)
532{
533 lra_assert (regno >= lra_constraint_new_regno_start);
534 if (lra_reg_info[regno].preferred_hard_regno1 == hard_regno)
535 lra_reg_info[regno].preferred_hard_regno_profit1 += profit;
536 else if (lra_reg_info[regno].preferred_hard_regno2 == hard_regno)
537 lra_reg_info[regno].preferred_hard_regno_profit2 += profit;
538 else if (lra_reg_info[regno].preferred_hard_regno1 < 0)
539 {
540 lra_reg_info[regno].preferred_hard_regno1 = hard_regno;
541 lra_reg_info[regno].preferred_hard_regno_profit1 = profit;
542 }
543 else if (lra_reg_info[regno].preferred_hard_regno2 < 0
544 || profit > lra_reg_info[regno].preferred_hard_regno_profit2)
545 {
546 lra_reg_info[regno].preferred_hard_regno2 = hard_regno;
547 lra_reg_info[regno].preferred_hard_regno_profit2 = profit;
548 }
549 else
550 return;
551 /* Keep the 1st hard regno as more profitable. */
552 if (lra_reg_info[regno].preferred_hard_regno1 >= 0
553 && lra_reg_info[regno].preferred_hard_regno2 >= 0
554 && (lra_reg_info[regno].preferred_hard_regno_profit2
555 > lra_reg_info[regno].preferred_hard_regno_profit1))
556 {
a4f59596 557 std::swap (lra_reg_info[regno].preferred_hard_regno1,
558 lra_reg_info[regno].preferred_hard_regno2);
559 std::swap (lra_reg_info[regno].preferred_hard_regno_profit1,
560 lra_reg_info[regno].preferred_hard_regno_profit2);
c6a6cdaa 561 }
562 if (lra_dump_file != NULL)
563 {
564 if ((hard_regno = lra_reg_info[regno].preferred_hard_regno1) >= 0)
565 fprintf (lra_dump_file,
566 " Hard reg %d is preferable by r%d with profit %d\n",
567 hard_regno, regno,
568 lra_reg_info[regno].preferred_hard_regno_profit1);
569 if ((hard_regno = lra_reg_info[regno].preferred_hard_regno2) >= 0)
570 fprintf (lra_dump_file,
571 " Hard reg %d is preferable by r%d with profit %d\n",
572 hard_regno, regno,
573 lra_reg_info[regno].preferred_hard_regno_profit2);
574 }
575}
576
577/* Check that REGNO living through calls and setjumps, set up conflict
9628978f 578 regs using LAST_CALL_USED_REG_SET, and clear corresponding bits in
579 PSEUDOS_LIVE_THROUGH_CALLS and PSEUDOS_LIVE_THROUGH_SETJUMPS. */
c6a6cdaa 580static inline void
9628978f 581check_pseudos_live_through_calls (int regno,
582 HARD_REG_SET last_call_used_reg_set)
c6a6cdaa 583{
a766a8b0 584 int hr;
585
c6a6cdaa 586 if (! sparseset_bit_p (pseudos_live_through_calls, regno))
587 return;
588 sparseset_clear_bit (pseudos_live_through_calls, regno);
589 IOR_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs,
9628978f 590 last_call_used_reg_set);
a766a8b0 591
dd59ba43 592 for (hr = 0; HARD_REGISTER_NUM_P (hr); hr++)
5da94e60 593 if (targetm.hard_regno_call_part_clobbered (hr,
594 PSEUDO_REGNO_MODE (regno)))
fc67b24f 595 add_to_hard_reg_set (&lra_reg_info[regno].conflict_hard_regs,
596 PSEUDO_REGNO_MODE (regno), hr);
c6a6cdaa 597 lra_reg_info[regno].call_p = true;
c6a6cdaa 598 if (! sparseset_bit_p (pseudos_live_through_setjumps, regno))
599 return;
600 sparseset_clear_bit (pseudos_live_through_setjumps, regno);
601 /* Don't allocate pseudos that cross setjmps or any call, if this
602 function receives a nonlocal goto. */
603 SET_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs);
604}
605
3da302c5 606/* Return true if insn REG is an early clobber operand in alternative
607 NALT. Negative NALT means that we don't know the current insn
608 alternative. So assume the worst. */
609static inline bool
610reg_early_clobber_p (const struct lra_insn_reg *reg, int n_alt)
611{
612 return (reg->early_clobber
71d47a14 613 && (n_alt == LRA_UNKNOWN_ALT
614 || (n_alt != LRA_NON_CLOBBERED_ALT
615 && TEST_BIT (reg->early_clobber_alts, n_alt))));
3da302c5 616}
617
c6a6cdaa 618/* Process insns of the basic block BB to update pseudo live ranges,
619 pseudo hard register conflicts, and insn notes. We do it on
620 backward scan of BB insns. CURR_POINT is the program point where
621 BB ends. The function updates this counter and returns in
8c0d01a4 622 CURR_POINT the program point where BB starts. The function also
04472658 623 does local live info updates and can delete the dead insns if
d3ceeae2 624 DEAD_INSN_P. It returns true if pseudo live info was
8c0d01a4 625 changed at the BB start. */
626static bool
d3ceeae2 627process_bb_lives (basic_block bb, int &curr_point, bool dead_insn_p)
c6a6cdaa 628{
629 int i, regno, freq;
630 unsigned int j;
631 bitmap_iterator bi;
632 bitmap reg_live_out;
633 unsigned int px;
8c0d01a4 634 rtx_insn *next;
c6a6cdaa 635 rtx link, *link_loc;
636 bool need_curr_point_incr;
9628978f 637 HARD_REG_SET last_call_used_reg_set;
dd59ba43 638
c6a6cdaa 639 reg_live_out = df_get_live_out (bb);
640 sparseset_clear (pseudos_live);
641 sparseset_clear (pseudos_live_through_calls);
642 sparseset_clear (pseudos_live_through_setjumps);
9628978f 643 CLEAR_HARD_REG_SET (last_call_used_reg_set);
c6a6cdaa 644 REG_SET_TO_HARD_REG_SET (hard_regs_live, reg_live_out);
645 AND_COMPL_HARD_REG_SET (hard_regs_live, eliminable_regset);
c6a6cdaa 646 EXECUTE_IF_SET_IN_BITMAP (reg_live_out, FIRST_PSEUDO_REGISTER, j, bi)
dd59ba43 647 {
648 update_pseudo_point (j, curr_point, USE_POINT);
649 mark_pseudo_live (j);
650 }
c6a6cdaa 651
d3ceeae2 652 bb_gen_pseudos = &get_bb_data (bb)->gen_pseudos;
653 bb_killed_pseudos = &get_bb_data (bb)->killed_pseudos;
654 bitmap_clear (bb_gen_pseudos);
655 bitmap_clear (bb_killed_pseudos);
c6a6cdaa 656 freq = REG_FREQ_FROM_BB (bb);
657
658 if (lra_dump_file != NULL)
659 fprintf (lra_dump_file, " BB %d\n", bb->index);
660
661 /* Scan the code of this basic block, noting which pseudos and hard
662 regs are born or die.
663
664 Note that this loop treats uninitialized values as live until the
665 beginning of the block. For example, if an instruction uses
666 (reg:DI foo), and only (subreg:SI (reg:DI foo) 0) is ever set,
667 FOO will remain live until the beginning of the block. Likewise
668 if FOO is not set at all. This is unnecessarily pessimistic, but
669 it probably doesn't matter much in practice. */
8c0d01a4 670 FOR_BB_INSNS_REVERSE_SAFE (bb, curr_insn, next)
c6a6cdaa 671 {
672 bool call_p;
3da302c5 673 int n_alt, dst_regno, src_regno;
c6a6cdaa 674 rtx set;
0823eb36 675 struct lra_insn_reg *reg, *hr;
c6a6cdaa 676
677 if (!NONDEBUG_INSN_P (curr_insn))
678 continue;
679
680 curr_id = lra_get_insn_recog_data (curr_insn);
681 curr_static_id = curr_id->insn_static_data;
3da302c5 682 n_alt = curr_id->used_insn_alternative;
c6a6cdaa 683 if (lra_dump_file != NULL)
3da302c5 684 fprintf (lra_dump_file, " Insn %u: point = %d, n_alt = %d\n",
685 INSN_UID (curr_insn), curr_point, n_alt);
c6a6cdaa 686
8c0d01a4 687 set = single_set (curr_insn);
688
d3ceeae2 689 if (dead_insn_p && set != NULL_RTX
dd59ba43 690 && REG_P (SET_DEST (set)) && !HARD_REGISTER_P (SET_DEST (set))
0b6934b3 691 && find_reg_note (curr_insn, REG_EH_REGION, NULL_RTX) == NULL_RTX
692 && ! may_trap_p (PATTERN (curr_insn))
693 /* Don't do premature remove of pic offset pseudo as we can
694 start to use it after some reload generation. */
695 && (pic_offset_table_rtx == NULL_RTX
696 || pic_offset_table_rtx != SET_DEST (set)))
8c0d01a4 697 {
d3ceeae2 698 bool remove_p = true;
8c0d01a4 699
700 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
701 if (reg->type != OP_IN && sparseset_bit_p (pseudos_live, reg->regno))
702 {
d3ceeae2 703 remove_p = false;
8c0d01a4 704 break;
705 }
706 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
0823eb36 707 if (reg->type != OP_IN && !reg->clobber_high)
8c0d01a4 708 {
d3ceeae2 709 remove_p = false;
8c0d01a4 710 break;
711 }
0823eb36 712
d3ceeae2 713 if (remove_p && ! volatile_refs_p (PATTERN (curr_insn)))
8c0d01a4 714 {
715 dst_regno = REGNO (SET_DEST (set));
716 if (lra_dump_file != NULL)
717 fprintf (lra_dump_file, " Deleting dead insn %u\n",
718 INSN_UID (curr_insn));
719 lra_set_insn_deleted (curr_insn);
720 if (lra_reg_info[dst_regno].nrefs == 0)
721 {
722 /* There might be some debug insns with the pseudo. */
723 unsigned int uid;
724 rtx_insn *insn;
725
04472658 726 bitmap_copy (&temp_bitmap, &lra_reg_info[dst_regno].insn_bitmap);
727 EXECUTE_IF_SET_IN_BITMAP (&temp_bitmap, 0, uid, bi)
8c0d01a4 728 {
729 insn = lra_insn_recog_data[uid]->insn;
730 lra_substitute_pseudo_within_insn (insn, dst_regno,
06072e79 731 SET_SRC (set), true);
8c0d01a4 732 lra_update_insn_regno_info (insn);
733 }
734 }
735 continue;
736 }
737 }
738
c6a6cdaa 739 /* Update max ref width and hard reg usage. */
740 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
e947f9c3 741 {
93e541b8 742 int i, regno = reg->regno;
974534ab 743
744 if (partial_subreg_p (lra_reg_info[regno].biggest_mode,
745 reg->biggest_mode))
93e541b8 746 lra_reg_info[regno].biggest_mode = reg->biggest_mode;
dd59ba43 747 if (HARD_REGISTER_NUM_P (regno))
93e541b8 748 {
749 lra_hard_reg_usage[regno] += freq;
750 /* A hard register explicitly can be used in small mode,
751 but implicitly it can be used in natural mode as a
752 part of multi-register group. Process this case
753 here. */
92d2aec3 754 for (i = 1; i < hard_regno_nregs (regno, reg->biggest_mode); i++)
974534ab 755 if (partial_subreg_p (lra_reg_info[regno + i].biggest_mode,
756 GET_MODE (regno_reg_rtx[regno + i])))
93e541b8 757 lra_reg_info[regno + i].biggest_mode
758 = GET_MODE (regno_reg_rtx[regno + i]);
759 }
e947f9c3 760 }
c6a6cdaa 761
762 call_p = CALL_P (curr_insn);
dd59ba43 763
764 /* If we have a simple register copy and the source reg is live after
765 this instruction, then remove the source reg from the live set so
766 that it will not conflict with the destination reg. */
767 rtx ignore_reg = non_conflicting_reg_copy_p (curr_insn);
768 if (ignore_reg != NULL_RTX)
769 {
770 int ignore_regno = REGNO (ignore_reg);
771 if (HARD_REGISTER_NUM_P (ignore_regno)
772 && TEST_HARD_REG_BIT (hard_regs_live, ignore_regno))
773 CLEAR_HARD_REG_BIT (hard_regs_live, ignore_regno);
774 else if (!HARD_REGISTER_NUM_P (ignore_regno)
775 && sparseset_bit_p (pseudos_live, ignore_regno))
776 sparseset_clear_bit (pseudos_live, ignore_regno);
777 else
778 /* We don't need any special handling of the source reg if
779 it is dead after this instruction. */
780 ignore_reg = NULL_RTX;
781 }
782
0af99ebf 783 src_regno = (set != NULL_RTX && REG_P (SET_SRC (set))
784 ? REGNO (SET_SRC (set)) : -1);
785 dst_regno = (set != NULL_RTX && REG_P (SET_DEST (set))
786 ? REGNO (SET_DEST (set)) : -1);
c6a6cdaa 787 if (complete_info_p
0af99ebf 788 && src_regno >= 0 && dst_regno >= 0
c6a6cdaa 789 /* Check that source regno does not conflict with
790 destination regno to exclude most impossible
791 preferences. */
dd59ba43 792 && (((!HARD_REGISTER_NUM_P (src_regno)
0af99ebf 793 && (! sparseset_bit_p (pseudos_live, src_regno)
dd59ba43 794 || (!HARD_REGISTER_NUM_P (dst_regno)
0af99ebf 795 && lra_reg_val_equal_p (src_regno,
796 lra_reg_info[dst_regno].val,
797 lra_reg_info[dst_regno].offset))))
dd59ba43 798 || (HARD_REGISTER_NUM_P (src_regno)
c6a6cdaa 799 && ! TEST_HARD_REG_BIT (hard_regs_live, src_regno)))
800 /* It might be 'inheritance pseudo <- reload pseudo'. */
801 || (src_regno >= lra_constraint_new_regno_start
0af99ebf 802 && dst_regno >= lra_constraint_new_regno_start
89bf872d 803 /* Remember to skip special cases where src/dest regnos are
804 the same, e.g. insn SET pattern has matching constraints
805 like =r,0. */
0af99ebf 806 && src_regno != dst_regno)))
c6a6cdaa 807 {
808 int hard_regno = -1, regno = -1;
809
c6a6cdaa 810 if (dst_regno >= lra_constraint_new_regno_start
811 && src_regno >= lra_constraint_new_regno_start)
41e11735 812 {
813 /* It might be still an original (non-reload) insn with
814 one unused output and a constraint requiring to use
815 the same reg for input/output operands. In this case
816 dst_regno and src_regno have the same value, we don't
817 need a misleading copy for this case. */
818 if (dst_regno != src_regno)
819 lra_create_copy (dst_regno, src_regno, freq);
820 }
c6a6cdaa 821 else if (dst_regno >= lra_constraint_new_regno_start)
822 {
dd59ba43 823 if (!HARD_REGISTER_NUM_P (hard_regno = src_regno))
c6a6cdaa 824 hard_regno = reg_renumber[src_regno];
825 regno = dst_regno;
826 }
827 else if (src_regno >= lra_constraint_new_regno_start)
828 {
dd59ba43 829 if (!HARD_REGISTER_NUM_P (hard_regno = dst_regno))
c6a6cdaa 830 hard_regno = reg_renumber[dst_regno];
831 regno = src_regno;
832 }
833 if (regno >= 0 && hard_regno >= 0)
834 lra_setup_reload_pseudo_preferenced_hard_reg
835 (regno, hard_regno, freq);
836 }
837
838 sparseset_clear (start_living);
839
c6a6cdaa 840 /* Mark each defined value as live. We need to do this for
841 unused values because they still conflict with quantities
842 that are live at the time of the definition. */
843 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
0823eb36 844 {
845 if (reg->type != OP_IN)
846 {
dd59ba43 847 update_pseudo_point (reg->regno, curr_point, USE_POINT);
848 mark_regno_live (reg->regno, reg->biggest_mode);
0823eb36 849 check_pseudos_live_through_calls (reg->regno,
850 last_call_used_reg_set);
851 }
852
dd59ba43 853 if (!HARD_REGISTER_NUM_P (reg->regno))
0823eb36 854 for (hr = curr_static_id->hard_regs; hr != NULL; hr = hr->next)
855 if (hr->clobber_high
856 && maybe_gt (GET_MODE_SIZE (PSEUDO_REGNO_MODE (reg->regno)),
857 GET_MODE_SIZE (hr->biggest_mode)))
858 SET_HARD_REG_BIT (lra_reg_info[reg->regno].conflict_hard_regs,
859 hr->regno);
860 }
c6a6cdaa 861
862 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
863 if (reg->type != OP_IN)
7e203840 864 make_hard_regno_live (reg->regno);
c6a6cdaa 865
853a01d6 866 if (curr_id->arg_hard_regs != NULL)
867 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
dd59ba43 868 if (!HARD_REGISTER_NUM_P (regno))
853a01d6 869 /* It is a clobber. */
7e203840 870 make_hard_regno_live (regno - FIRST_PSEUDO_REGISTER);
853a01d6 871
c6a6cdaa 872 sparseset_copy (unused_set, start_living);
873
874 sparseset_clear (start_dying);
875
876 /* See which defined values die here. */
877 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
dd59ba43 878 if (reg->type != OP_IN
3da302c5 879 && ! reg_early_clobber_p (reg, n_alt) && ! reg->subreg_p)
dd59ba43 880 {
881 if (reg->type == OP_OUT)
882 update_pseudo_point (reg->regno, curr_point, DEF_POINT);
883 mark_regno_dead (reg->regno, reg->biggest_mode);
884 }
c6a6cdaa 885
886 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
dd59ba43 887 if (reg->type != OP_IN
3da302c5 888 && ! reg_early_clobber_p (reg, n_alt) && ! reg->subreg_p)
4bcfedf5 889 make_hard_regno_dead (reg->regno);
c6a6cdaa 890
853a01d6 891 if (curr_id->arg_hard_regs != NULL)
892 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
dd59ba43 893 if (!HARD_REGISTER_NUM_P (regno))
4bcfedf5 894 /* It is a clobber. */
895 make_hard_regno_dead (regno - FIRST_PSEUDO_REGISTER);
853a01d6 896
c6a6cdaa 897 if (call_p)
898 {
9628978f 899 if (! flag_ipa_ra)
900 COPY_HARD_REG_SET(last_call_used_reg_set, call_used_reg_set);
901 else
f2cc6708 902 {
903 HARD_REG_SET this_call_used_reg_set;
904 get_call_reg_set_usage (curr_insn, &this_call_used_reg_set,
905 call_used_reg_set);
906
9628978f 907 bool flush = (! hard_reg_set_empty_p (last_call_used_reg_set)
908 && ! hard_reg_set_equal_p (last_call_used_reg_set,
909 this_call_used_reg_set));
910
f2cc6708 911 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, j)
9628978f 912 {
913 IOR_HARD_REG_SET (lra_reg_info[j].actual_call_used_reg_set,
914 this_call_used_reg_set);
915 if (flush)
916 check_pseudos_live_through_calls
917 (j, last_call_used_reg_set);
918 }
919 COPY_HARD_REG_SET(last_call_used_reg_set, this_call_used_reg_set);
f2cc6708 920 }
921
c6a6cdaa 922 sparseset_ior (pseudos_live_through_calls,
923 pseudos_live_through_calls, pseudos_live);
924 if (cfun->has_nonlocal_label
839a4719 925 || (!targetm.setjmp_preserves_nonvolatile_regs_p ()
926 && (find_reg_note (curr_insn, REG_SETJMP, NULL_RTX)
927 != NULL_RTX)))
c6a6cdaa 928 sparseset_ior (pseudos_live_through_setjumps,
929 pseudos_live_through_setjumps, pseudos_live);
930 }
931
932 /* Increment the current program point if we must. */
dd59ba43 933 if (sparseset_contains_pseudos_p (unused_set)
934 || sparseset_contains_pseudos_p (start_dying))
c6a6cdaa 935 next_program_point (curr_point, freq);
936
dd59ba43 937 /* If we removed the source reg from a simple register copy from the
938 live set above, then add it back now so we don't accidentally add
939 it to the start_living set below. */
940 if (ignore_reg != NULL_RTX)
941 {
942 int ignore_regno = REGNO (ignore_reg);
943 if (HARD_REGISTER_NUM_P (ignore_regno))
944 SET_HARD_REG_BIT (hard_regs_live, ignore_regno);
945 else
946 sparseset_set_bit (pseudos_live, ignore_regno);
947 }
c6a6cdaa 948
dd59ba43 949 sparseset_clear (start_living);
c6a6cdaa 950
951 /* Mark each used value as live. */
952 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
dd59ba43 953 if (reg->type != OP_OUT)
c6a6cdaa 954 {
dd59ba43 955 if (reg->type == OP_IN)
956 update_pseudo_point (reg->regno, curr_point, USE_POINT);
957 mark_regno_live (reg->regno, reg->biggest_mode);
9628978f 958 check_pseudos_live_through_calls (reg->regno,
959 last_call_used_reg_set);
c6a6cdaa 960 }
961
962 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
dd59ba43 963 if (reg->type != OP_OUT)
7e203840 964 make_hard_regno_live (reg->regno);
c6a6cdaa 965
966 if (curr_id->arg_hard_regs != NULL)
4bcfedf5 967 /* Make argument hard registers live. */
c6a6cdaa 968 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
dd59ba43 969 if (HARD_REGISTER_NUM_P (regno))
7e203840 970 make_hard_regno_live (regno);
c6a6cdaa 971
972 sparseset_and_compl (dead_set, start_living, start_dying);
973
dd59ba43 974 sparseset_clear (start_dying);
975
c6a6cdaa 976 /* Mark early clobber outputs dead. */
977 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
dd59ba43 978 if (reg->type != OP_IN
3da302c5 979 && reg_early_clobber_p (reg, n_alt) && ! reg->subreg_p)
dd59ba43 980 {
981 if (reg->type == OP_OUT)
982 update_pseudo_point (reg->regno, curr_point, DEF_POINT);
983 mark_regno_dead (reg->regno, reg->biggest_mode);
984
985 /* We're done processing inputs, so make sure early clobber
986 operands that are both inputs and outputs are still live. */
987 if (reg->type == OP_INOUT)
988 mark_regno_live (reg->regno, reg->biggest_mode);
989 }
c6a6cdaa 990
991 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
dd59ba43 992 if (reg->type != OP_IN
3da302c5 993 && reg_early_clobber_p (reg, n_alt) && ! reg->subreg_p)
1bdd07cb 994 {
995 struct lra_insn_reg *reg2;
dd59ba43 996
1bdd07cb 997 /* We can have early clobbered non-operand hard reg and
998 the same hard reg as an insn input. Don't make hard
999 reg dead before the insns. */
1000 for (reg2 = curr_id->regs; reg2 != NULL; reg2 = reg2->next)
1001 if (reg2->type != OP_OUT && reg2->regno == reg->regno)
1002 break;
1003 if (reg2 == NULL)
4bcfedf5 1004 make_hard_regno_dead (reg->regno);
1bdd07cb 1005 }
c6a6cdaa 1006
dd59ba43 1007 /* Increment the current program point if we must. */
1008 if (sparseset_contains_pseudos_p (dead_set)
1009 || sparseset_contains_pseudos_p (start_dying))
c6a6cdaa 1010 next_program_point (curr_point, freq);
1011
1012 /* Update notes. */
1013 for (link_loc = &REG_NOTES (curr_insn); (link = *link_loc) != NULL_RTX;)
1014 {
1015 if (REG_NOTE_KIND (link) != REG_DEAD
1016 && REG_NOTE_KIND (link) != REG_UNUSED)
1017 ;
1018 else if (REG_P (XEXP (link, 0)))
1019 {
1020 regno = REGNO (XEXP (link, 0));
1021 if ((REG_NOTE_KIND (link) == REG_DEAD
1022 && ! sparseset_bit_p (dead_set, regno))
1023 || (REG_NOTE_KIND (link) == REG_UNUSED
1024 && ! sparseset_bit_p (unused_set, regno)))
1025 {
1026 *link_loc = XEXP (link, 1);
1027 continue;
1028 }
1029 if (REG_NOTE_KIND (link) == REG_DEAD)
1030 sparseset_clear_bit (dead_set, regno);
1031 else if (REG_NOTE_KIND (link) == REG_UNUSED)
1032 sparseset_clear_bit (unused_set, regno);
1033 }
1034 link_loc = &XEXP (link, 1);
1035 }
1036 EXECUTE_IF_SET_IN_SPARSESET (dead_set, j)
1037 add_reg_note (curr_insn, REG_DEAD, regno_reg_rtx[j]);
1038 EXECUTE_IF_SET_IN_SPARSESET (unused_set, j)
1039 add_reg_note (curr_insn, REG_UNUSED, regno_reg_rtx[j]);
1040 }
1041
c6a6cdaa 1042 if (bb_has_eh_pred (bb))
1043 for (j = 0; ; ++j)
1044 {
1045 unsigned int regno = EH_RETURN_DATA_REGNO (j);
1046
1047 if (regno == INVALID_REGNUM)
1048 break;
7e203840 1049 make_hard_regno_live (regno);
c6a6cdaa 1050 }
c6a6cdaa 1051
1052 /* Pseudos can't go in stack regs at the start of a basic block that
1053 is reached by an abnormal edge. Likewise for call clobbered regs,
1054 because caller-save, fixup_abnormal_edges and possibly the table
1055 driven EH machinery are not quite ready to handle such pseudos
1056 live across such edges. */
1057 if (bb_has_abnormal_pred (bb))
1058 {
1059#ifdef STACK_REGS
1060 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, px)
1061 lra_reg_info[px].no_stack_p = true;
1062 for (px = FIRST_STACK_REG; px <= LAST_STACK_REG; px++)
7e203840 1063 make_hard_regno_live (px);
c6a6cdaa 1064#endif
1065 /* No need to record conflicts for call clobbered regs if we
1066 have nonlocal labels around, as we don't ever try to
1067 allocate such regs in this case. */
a0a565a9 1068 if (!cfun->has_nonlocal_label
1069 && has_abnormal_call_or_eh_pred_edge_p (bb))
dd59ba43 1070 for (px = 0; HARD_REGISTER_NUM_P (px); px++)
15d96f90 1071 if (call_used_regs[px]
1072#ifdef REAL_PIC_OFFSET_TABLE_REGNUM
1073 /* We should create a conflict of PIC pseudo with PIC
1074 hard reg as PIC hard reg can have a wrong value after
1075 jump described by the abnormal edge. In this case we
1076 can not allocate PIC hard reg to PIC pseudo as PIC
1077 pseudo will also have a wrong value. */
1078 || (px == REAL_PIC_OFFSET_TABLE_REGNUM
1079 && pic_offset_table_rtx != NULL_RTX
dd59ba43 1080 && !HARD_REGISTER_P (pic_offset_table_rtx))
15d96f90 1081#endif
1082 )
7e203840 1083 make_hard_regno_live (px);
c6a6cdaa 1084 }
1085
8c0d01a4 1086 bool live_change_p = false;
d3ceeae2 1087 /* Check if bb border live info was changed. */
1088 unsigned int live_pseudos_num = 0;
1089 EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb),
1090 FIRST_PSEUDO_REGISTER, j, bi)
8c0d01a4 1091 {
d3ceeae2 1092 live_pseudos_num++;
1093 if (! sparseset_bit_p (pseudos_live, j))
8c0d01a4 1094 {
c5181370 1095 live_change_p = true;
1096 if (lra_dump_file != NULL)
1097 fprintf (lra_dump_file,
1098 " r%d is removed as live at bb%d start\n", j, bb->index);
d3ceeae2 1099 break;
8c0d01a4 1100 }
1101 }
c5181370 1102 if (! live_change_p
1103 && sparseset_cardinality (pseudos_live) != live_pseudos_num)
1104 {
1105 live_change_p = true;
1106 if (lra_dump_file != NULL)
1107 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, j)
1108 if (! bitmap_bit_p (df_get_live_in (bb), j))
1109 fprintf (lra_dump_file,
1110 " r%d is added to live at bb%d start\n", j, bb->index);
1111 }
c6a6cdaa 1112 /* See if we'll need an increment at the end of this basic block.
1113 An increment is needed if the PSEUDOS_LIVE set is not empty,
1114 to make sure the finish points are set up correctly. */
1115 need_curr_point_incr = (sparseset_cardinality (pseudos_live) > 0);
1116
1117 EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, i)
dd59ba43 1118 {
1119 update_pseudo_point (i, curr_point, DEF_POINT);
1120 mark_pseudo_dead (i);
1121 }
c6a6cdaa 1122
1123 EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb), FIRST_PSEUDO_REGISTER, j, bi)
1124 {
1125 if (sparseset_cardinality (pseudos_live_through_calls) == 0)
1126 break;
1127 if (sparseset_bit_p (pseudos_live_through_calls, j))
9628978f 1128 check_pseudos_live_through_calls (j, last_call_used_reg_set);
c6a6cdaa 1129 }
a940269d 1130
dd59ba43 1131 for (i = 0; HARD_REGISTER_NUM_P (i); ++i)
95f18d43 1132 {
1133 if (!TEST_HARD_REG_BIT (hard_regs_live, i))
1134 continue;
1135
1136 if (!TEST_HARD_REG_BIT (hard_regs_spilled_into, i))
1137 continue;
1138
1139 if (bitmap_bit_p (df_get_live_in (bb), i))
1140 continue;
1141
1142 live_change_p = true;
1143 if (lra_dump_file)
1144 fprintf (lra_dump_file,
1145 " hard reg r%d is added to live at bb%d start\n", i,
1146 bb->index);
1147 bitmap_set_bit (df_get_live_in (bb), i);
1148 }
1149
c6a6cdaa 1150 if (need_curr_point_incr)
1151 next_program_point (curr_point, freq);
8c0d01a4 1152
1153 return live_change_p;
c6a6cdaa 1154}
1155
1156/* Compress pseudo live ranges by removing program points where
1157 nothing happens. Complexity of many algorithms in LRA is linear
1158 function of program points number. To speed up the code we try to
1159 minimize the number of the program points here. */
1160static void
1161remove_some_program_points_and_update_live_ranges (void)
1162{
1163 unsigned i;
1164 int n, max_regno;
1165 int *map;
1166 lra_live_range_t r, prev_r, next_r;
c6a6cdaa 1167 sbitmap_iterator sbi;
1168 bool born_p, dead_p, prev_born_p, prev_dead_p;
1169
3c6549f8 1170 auto_sbitmap born (lra_live_max_point);
1171 auto_sbitmap dead (lra_live_max_point);
53c5d9d4 1172 bitmap_clear (born);
1173 bitmap_clear (dead);
c6a6cdaa 1174 max_regno = max_reg_num ();
1175 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
1176 {
1177 for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
1178 {
1179 lra_assert (r->start <= r->finish);
08b7917c 1180 bitmap_set_bit (born, r->start);
1181 bitmap_set_bit (dead, r->finish);
c6a6cdaa 1182 }
1183 }
3c6549f8 1184 auto_sbitmap born_or_dead (lra_live_max_point);
53c5d9d4 1185 bitmap_ior (born_or_dead, born, dead);
c6a6cdaa 1186 map = XCNEWVEC (int, lra_live_max_point);
1187 n = -1;
1188 prev_born_p = prev_dead_p = false;
0d211963 1189 EXECUTE_IF_SET_IN_BITMAP (born_or_dead, 0, i, sbi)
c6a6cdaa 1190 {
08b7917c 1191 born_p = bitmap_bit_p (born, i);
1192 dead_p = bitmap_bit_p (dead, i);
c6a6cdaa 1193 if ((prev_born_p && ! prev_dead_p && born_p && ! dead_p)
1194 || (prev_dead_p && ! prev_born_p && dead_p && ! born_p))
1195 {
1196 map[i] = n;
1197 lra_point_freq[n] = MAX (lra_point_freq[n], lra_point_freq[i]);
1198 }
1199 else
1200 {
1201 map[i] = ++n;
1202 lra_point_freq[n] = lra_point_freq[i];
1203 }
1204 prev_born_p = born_p;
1205 prev_dead_p = dead_p;
1206 }
c6a6cdaa 1207 n++;
1208 if (lra_dump_file != NULL)
1209 fprintf (lra_dump_file, "Compressing live ranges: from %d to %d - %d%%\n",
481be954 1210 lra_live_max_point, n,
1211 lra_live_max_point ? 100 * n / lra_live_max_point : 100);
c6a6cdaa 1212 if (n < lra_live_max_point)
1213 {
1214 lra_live_max_point = n;
1215 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
1216 {
1217 for (prev_r = NULL, r = lra_reg_info[i].live_ranges;
1218 r != NULL;
1219 r = next_r)
1220 {
1221 next_r = r->next;
1222 r->start = map[r->start];
1223 r->finish = map[r->finish];
1224 if (prev_r == NULL || prev_r->start > r->finish + 1)
1225 {
1226 prev_r = r;
1227 continue;
1228 }
1229 prev_r->start = r->start;
1230 prev_r->next = next_r;
b7cbf36d 1231 lra_live_range_pool.remove (r);
c6a6cdaa 1232 }
1233 }
1234 }
1235 free (map);
1236}
1237
1238/* Print live ranges R to file F. */
1239void
1240lra_print_live_range_list (FILE *f, lra_live_range_t r)
1241{
1242 for (; r != NULL; r = r->next)
1243 fprintf (f, " [%d..%d]", r->start, r->finish);
1244 fprintf (f, "\n");
1245}
1246
c7d89805 1247DEBUG_FUNCTION void
1248debug (lra_live_range &ref)
1249{
1250 lra_print_live_range_list (stderr, &ref);
1251}
1252
1253DEBUG_FUNCTION void
1254debug (lra_live_range *ptr)
1255{
1256 if (ptr)
1257 debug (*ptr);
1258 else
1259 fprintf (stderr, "<nil>\n");
1260}
1261
c6a6cdaa 1262/* Print live ranges R to stderr. */
1263void
1264lra_debug_live_range_list (lra_live_range_t r)
1265{
1266 lra_print_live_range_list (stderr, r);
1267}
1268
1269/* Print live ranges of pseudo REGNO to file F. */
1270static void
1271print_pseudo_live_ranges (FILE *f, int regno)
1272{
1273 if (lra_reg_info[regno].live_ranges == NULL)
1274 return;
1275 fprintf (f, " r%d:", regno);
1276 lra_print_live_range_list (f, lra_reg_info[regno].live_ranges);
1277}
1278
1279/* Print live ranges of pseudo REGNO to stderr. */
1280void
1281lra_debug_pseudo_live_ranges (int regno)
1282{
1283 print_pseudo_live_ranges (stderr, regno);
1284}
1285
1286/* Print live ranges of all pseudos to file F. */
1287static void
1288print_live_ranges (FILE *f)
1289{
1290 int i, max_regno;
1291
1292 max_regno = max_reg_num ();
1293 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1294 print_pseudo_live_ranges (f, i);
1295}
1296
1297/* Print live ranges of all pseudos to stderr. */
1298void
1299lra_debug_live_ranges (void)
1300{
1301 print_live_ranges (stderr);
1302}
1303
1304/* Compress pseudo live ranges. */
1305static void
1306compress_live_ranges (void)
1307{
1308 remove_some_program_points_and_update_live_ranges ();
1309 if (lra_dump_file != NULL)
1310 {
1311 fprintf (lra_dump_file, "Ranges after the compression:\n");
1312 print_live_ranges (lra_dump_file);
1313 }
1314}
1315
8c0d01a4 1316\f
1317
c6a6cdaa 1318/* The number of the current live range pass. */
1319int lra_live_range_iter;
1320
d3ceeae2 1321/* The function creates live ranges only for memory pseudos (or for
1322 all ones if ALL_P), set up CONFLICT_HARD_REGS for the pseudos. It
1323 also does dead insn elimination if DEAD_INSN_P and global live
1324 analysis only for pseudos and only if the pseudo live info was
1325 changed on a BB border. Return TRUE if the live info was
1326 changed. */
1327static bool
1328lra_create_live_ranges_1 (bool all_p, bool dead_insn_p)
c6a6cdaa 1329{
1330 basic_block bb;
1331 int i, hard_regno, max_regno = max_reg_num ();
1332 int curr_point;
8c0d01a4 1333 bool bb_live_change_p, have_referenced_pseudos = false;
c6a6cdaa 1334
1335 timevar_push (TV_LRA_CREATE_LIVE_RANGES);
1336
1337 complete_info_p = all_p;
1338 if (lra_dump_file != NULL)
1339 fprintf (lra_dump_file,
1340 "\n********** Pseudo live ranges #%d: **********\n\n",
1341 ++lra_live_range_iter);
1342 memset (lra_hard_reg_usage, 0, sizeof (lra_hard_reg_usage));
1343 for (i = 0; i < max_regno; i++)
1344 {
1345 lra_reg_info[i].live_ranges = NULL;
1346 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1347 lra_reg_info[i].preferred_hard_regno1 = -1;
1348 lra_reg_info[i].preferred_hard_regno2 = -1;
1349 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1350 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1351#ifdef STACK_REGS
1352 lra_reg_info[i].no_stack_p = false;
1353#endif
fc8a0f60 1354 /* The biggest mode is already set but its value might be to
1355 conservative because of recent transformation. Here in this
1356 file we recalculate it again as it costs practically
1357 nothing. */
dd59ba43 1358 if (!HARD_REGISTER_NUM_P (i) && regno_reg_rtx[i] != NULL_RTX)
c6a6cdaa 1359 lra_reg_info[i].biggest_mode = GET_MODE (regno_reg_rtx[i]);
1360 else
1361 lra_reg_info[i].biggest_mode = VOIDmode;
c6a6cdaa 1362 lra_reg_info[i].call_p = false;
dd59ba43 1363 if (!HARD_REGISTER_NUM_P (i)
dbb9a07c 1364 && lra_reg_info[i].nrefs != 0)
1365 {
1366 if ((hard_regno = reg_renumber[i]) >= 0)
1367 lra_hard_reg_usage[hard_regno] += lra_reg_info[i].freq;
1368 have_referenced_pseudos = true;
1369 }
c6a6cdaa 1370 }
1371 lra_free_copies ();
a940269d 1372
dbb9a07c 1373 /* Under some circumstances, we can have functions without pseudo
1374 registers. For such functions, lra_live_max_point will be 0,
1375 see e.g. PR55604, and there's nothing more to do for us here. */
1376 if (! have_referenced_pseudos)
1377 {
1378 timevar_pop (TV_LRA_CREATE_LIVE_RANGES);
d3ceeae2 1379 return false;
dbb9a07c 1380 }
1381
c6a6cdaa 1382 pseudos_live = sparseset_alloc (max_regno);
1383 pseudos_live_through_calls = sparseset_alloc (max_regno);
1384 pseudos_live_through_setjumps = sparseset_alloc (max_regno);
1385 start_living = sparseset_alloc (max_regno);
1386 start_dying = sparseset_alloc (max_regno);
1387 dead_set = sparseset_alloc (max_regno);
1388 unused_set = sparseset_alloc (max_regno);
1389 curr_point = 0;
b7cbf36d 1390 unsigned new_length = get_max_uid () * 2;
13b8babf 1391 point_freq_vec.truncate (0);
1392 point_freq_vec.reserve_exact (new_length);
f1f41a6c 1393 lra_point_freq = point_freq_vec.address ();
a4421e7b 1394 auto_vec<int, 20> post_order_rev_cfg;
1395 inverted_post_order_compute (&post_order_rev_cfg);
1396 lra_assert (post_order_rev_cfg.length () == (unsigned) n_basic_blocks_for_fn (cfun));
8c0d01a4 1397 bb_live_change_p = false;
a4421e7b 1398 for (i = post_order_rev_cfg.length () - 1; i >= 0; --i)
c6a6cdaa 1399 {
f5a6b05f 1400 bb = BASIC_BLOCK_FOR_FN (cfun, post_order_rev_cfg[i]);
34154e27 1401 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb
1402 == ENTRY_BLOCK_PTR_FOR_FN (cfun))
c6a6cdaa 1403 continue;
d3ceeae2 1404 if (process_bb_lives (bb, curr_point, dead_insn_p))
8c0d01a4 1405 bb_live_change_p = true;
1406 }
1407 if (bb_live_change_p)
1408 {
1409 /* We need to clear pseudo live info as some pseudos can
1410 disappear, e.g. pseudos with used equivalences. */
1411 FOR_EACH_BB_FN (bb, cfun)
1412 {
1413 bitmap_clear_range (df_get_live_in (bb), FIRST_PSEUDO_REGISTER,
1414 max_regno - FIRST_PSEUDO_REGISTER);
1415 bitmap_clear_range (df_get_live_out (bb), FIRST_PSEUDO_REGISTER,
1416 max_regno - FIRST_PSEUDO_REGISTER);
1417 }
1418 /* As we did not change CFG since LRA start we can use
1419 DF-infrastructure solver to solve live data flow problem. */
dd59ba43 1420 for (int i = 0; HARD_REGISTER_NUM_P (i); ++i)
95f18d43 1421 {
1422 if (TEST_HARD_REG_BIT (hard_regs_spilled_into, i))
1423 bitmap_clear_bit (&all_hard_regs_bitmap, i);
1424 }
8c0d01a4 1425 df_simple_dataflow
1426 (DF_BACKWARD, NULL, live_con_fun_0, live_con_fun_n,
1427 live_trans_fun, &all_blocks,
1428 df_get_postorder (DF_BACKWARD), df_get_n_blocks (DF_BACKWARD));
1429 if (lra_dump_file != NULL)
1430 {
0b6934b3 1431 fprintf (lra_dump_file,
1432 "Global pseudo live data have been updated:\n");
8c0d01a4 1433 basic_block bb;
1434 FOR_EACH_BB_FN (bb, cfun)
1435 {
1436 bb_data_t bb_info = get_bb_data (bb);
1437 bitmap bb_livein = df_get_live_in (bb);
1438 bitmap bb_liveout = df_get_live_out (bb);
1439
1440 fprintf (lra_dump_file, "\nBB %d:\n", bb->index);
1441 lra_dump_bitmap_with_title (" gen:",
1442 &bb_info->gen_pseudos, bb->index);
1443 lra_dump_bitmap_with_title (" killed:",
1444 &bb_info->killed_pseudos, bb->index);
1445 lra_dump_bitmap_with_title (" livein:", bb_livein, bb->index);
1446 lra_dump_bitmap_with_title (" liveout:", bb_liveout, bb->index);
1447 }
1448 }
c6a6cdaa 1449 }
c6a6cdaa 1450 lra_live_max_point = curr_point;
1451 if (lra_dump_file != NULL)
1452 print_live_ranges (lra_dump_file);
1453 /* Clean up. */
1454 sparseset_free (unused_set);
1455 sparseset_free (dead_set);
1456 sparseset_free (start_dying);
1457 sparseset_free (start_living);
1458 sparseset_free (pseudos_live_through_calls);
1459 sparseset_free (pseudos_live_through_setjumps);
1460 sparseset_free (pseudos_live);
1461 compress_live_ranges ();
1462 timevar_pop (TV_LRA_CREATE_LIVE_RANGES);
d3ceeae2 1463 return bb_live_change_p;
1464}
1465
1466/* The main entry function creates live-ranges and other live info
1467 necessary for the assignment sub-pass. It uses
1468 lra_creates_live_ranges_1 -- so read comments for the
1469 function. */
1470void
1471lra_create_live_ranges (bool all_p, bool dead_insn_p)
1472{
1473 if (! lra_create_live_ranges_1 (all_p, dead_insn_p))
1474 return;
1475 if (lra_dump_file != NULL)
1476 fprintf (lra_dump_file, "Live info was changed -- recalculate it\n");
1477 /* Live info was changed on a bb border. It means that some info,
c5181370 1478 e.g. about conflict regs, calls crossed, and live ranges may be
1479 wrong. We need this info for allocation. So recalculate it
1480 again but without removing dead insns which can change live info
1481 again. Repetitive live range calculations are expensive therefore
1482 we stop here as we already have correct info although some
1483 improvement in rare cases could be possible on this sub-pass if
1484 we do dead insn elimination again (still the improvement may
1485 happen later). */
d3ceeae2 1486 lra_clear_live_ranges ();
c5181370 1487 bool res = lra_create_live_ranges_1 (all_p, false);
d3ceeae2 1488 lra_assert (! res);
c6a6cdaa 1489}
1490
1491/* Finish all live ranges. */
1492void
1493lra_clear_live_ranges (void)
1494{
1495 int i;
1496
1497 for (i = 0; i < max_reg_num (); i++)
1498 free_live_range_list (lra_reg_info[i].live_ranges);
f1f41a6c 1499 point_freq_vec.release ();
c6a6cdaa 1500}
1501
1502/* Initialize live ranges data once per function. */
1503void
1504lra_live_ranges_init (void)
1505{
04472658 1506 bitmap_initialize (&temp_bitmap, &reg_obstack);
8c0d01a4 1507 initiate_live_solver ();
c6a6cdaa 1508}
1509
1510/* Finish live ranges data once per function. */
1511void
1512lra_live_ranges_finish (void)
1513{
8c0d01a4 1514 finish_live_solver ();
04472658 1515 bitmap_clear (&temp_bitmap);
e16712b1 1516 lra_live_range_pool.release ();
c6a6cdaa 1517}