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