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