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