2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / ira-lives.c
blob8bd62d2dc70cd8d4daa8e1d71032bf24d202c563
1 /* IRA processing allocno lives to build allocno live ranges.
2 Copyright (C) 2006-2015 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "regs.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "target.h"
29 #include "flags.h"
30 #include "except.h"
31 #include "hard-reg-set.h"
32 #include "predict.h"
33 #include "function.h"
34 #include "basic-block.h"
35 #include "insn-config.h"
36 #include "recog.h"
37 #include "diagnostic-core.h"
38 #include "params.h"
39 #include "df.h"
40 #include "sbitmap.h"
41 #include "sparseset.h"
42 #include "ira-int.h"
44 /* The code in this file is similar to one in global but the code
45 works on the allocno basis and creates live ranges instead of
46 pseudo-register conflicts. */
48 /* Program points are enumerated by numbers from range
49 0..IRA_MAX_POINT-1. There are approximately two times more program
50 points than insns. Program points are places in the program where
51 liveness info can be changed. In most general case (there are more
52 complicated cases too) some program points correspond to places
53 where input operand dies and other ones correspond to places where
54 output operands are born. */
55 int ira_max_point;
57 /* Arrays of size IRA_MAX_POINT mapping a program point to the allocno
58 live ranges with given start/finish point. */
59 live_range_t *ira_start_point_ranges, *ira_finish_point_ranges;
61 /* Number of the current program point. */
62 static int curr_point;
64 /* Point where register pressure excess started or -1 if there is no
65 register pressure excess. Excess pressure for a register class at
66 some point means that there are more allocnos of given register
67 class living at the point than number of hard-registers of the
68 class available for the allocation. It is defined only for
69 pressure classes. */
70 static int high_pressure_start_point[N_REG_CLASSES];
72 /* Objects live at current point in the scan. */
73 static sparseset objects_live;
75 /* A temporary bitmap used in functions that wish to avoid visiting an allocno
76 multiple times. */
77 static sparseset allocnos_processed;
79 /* Set of hard regs (except eliminable ones) currently live. */
80 static HARD_REG_SET hard_regs_live;
82 /* The loop tree node corresponding to the current basic block. */
83 static ira_loop_tree_node_t curr_bb_node;
85 /* The number of the last processed call. */
86 static int last_call_num;
87 /* The number of last call at which given allocno was saved. */
88 static int *allocno_saved_at_call;
90 /* The value of get_preferred_alternatives for the current instruction,
91 supplemental to recog_data. */
92 static alternative_mask preferred_alternatives;
94 /* Record the birth of hard register REGNO, updating hard_regs_live and
95 hard reg conflict information for living allocnos. */
96 static void
97 make_hard_regno_born (int regno)
99 unsigned int i;
101 SET_HARD_REG_BIT (hard_regs_live, regno);
102 EXECUTE_IF_SET_IN_SPARSESET (objects_live, i)
104 ira_object_t obj = ira_object_id_map[i];
106 SET_HARD_REG_BIT (OBJECT_CONFLICT_HARD_REGS (obj), regno);
107 SET_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), regno);
111 /* Process the death of hard register REGNO. This updates
112 hard_regs_live. */
113 static void
114 make_hard_regno_dead (int regno)
116 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
119 /* Record the birth of object OBJ. Set a bit for it in objects_live,
120 start a new live range for it if necessary and update hard register
121 conflicts. */
122 static void
123 make_object_born (ira_object_t obj)
125 live_range_t lr = OBJECT_LIVE_RANGES (obj);
127 sparseset_set_bit (objects_live, OBJECT_CONFLICT_ID (obj));
128 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj), hard_regs_live);
129 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), hard_regs_live);
131 if (lr == NULL
132 || (lr->finish != curr_point && lr->finish + 1 != curr_point))
133 ira_add_live_range_to_object (obj, curr_point, -1);
136 /* Update ALLOCNO_EXCESS_PRESSURE_POINTS_NUM for the allocno
137 associated with object OBJ. */
138 static void
139 update_allocno_pressure_excess_length (ira_object_t obj)
141 ira_allocno_t a = OBJECT_ALLOCNO (obj);
142 int start, i;
143 enum reg_class aclass, pclass, cl;
144 live_range_t p;
146 aclass = ALLOCNO_CLASS (a);
147 pclass = ira_pressure_class_translate[aclass];
148 for (i = 0;
149 (cl = ira_reg_class_super_classes[pclass][i]) != LIM_REG_CLASSES;
150 i++)
152 if (! ira_reg_pressure_class_p[cl])
153 continue;
154 if (high_pressure_start_point[cl] < 0)
155 continue;
156 p = OBJECT_LIVE_RANGES (obj);
157 ira_assert (p != NULL);
158 start = (high_pressure_start_point[cl] > p->start
159 ? high_pressure_start_point[cl] : p->start);
160 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) += curr_point - start + 1;
164 /* Process the death of object OBJ, which is associated with allocno
165 A. This finishes the current live range for it. */
166 static void
167 make_object_dead (ira_object_t obj)
169 live_range_t lr;
171 sparseset_clear_bit (objects_live, OBJECT_CONFLICT_ID (obj));
172 lr = OBJECT_LIVE_RANGES (obj);
173 ira_assert (lr != NULL);
174 lr->finish = curr_point;
175 update_allocno_pressure_excess_length (obj);
178 /* The current register pressures for each pressure class for the current
179 basic block. */
180 static int curr_reg_pressure[N_REG_CLASSES];
182 /* Record that register pressure for PCLASS increased by N registers.
183 Update the current register pressure, maximal register pressure for
184 the current BB and the start point of the register pressure
185 excess. */
186 static void
187 inc_register_pressure (enum reg_class pclass, int n)
189 int i;
190 enum reg_class cl;
192 for (i = 0;
193 (cl = ira_reg_class_super_classes[pclass][i]) != LIM_REG_CLASSES;
194 i++)
196 if (! ira_reg_pressure_class_p[cl])
197 continue;
198 curr_reg_pressure[cl] += n;
199 if (high_pressure_start_point[cl] < 0
200 && (curr_reg_pressure[cl] > ira_class_hard_regs_num[cl]))
201 high_pressure_start_point[cl] = curr_point;
202 if (curr_bb_node->reg_pressure[cl] < curr_reg_pressure[cl])
203 curr_bb_node->reg_pressure[cl] = curr_reg_pressure[cl];
207 /* Record that register pressure for PCLASS has decreased by NREGS
208 registers; update current register pressure, start point of the
209 register pressure excess, and register pressure excess length for
210 living allocnos. */
212 static void
213 dec_register_pressure (enum reg_class pclass, int nregs)
215 int i;
216 unsigned int j;
217 enum reg_class cl;
218 bool set_p = false;
220 for (i = 0;
221 (cl = ira_reg_class_super_classes[pclass][i]) != LIM_REG_CLASSES;
222 i++)
224 if (! ira_reg_pressure_class_p[cl])
225 continue;
226 curr_reg_pressure[cl] -= nregs;
227 ira_assert (curr_reg_pressure[cl] >= 0);
228 if (high_pressure_start_point[cl] >= 0
229 && curr_reg_pressure[cl] <= ira_class_hard_regs_num[cl])
230 set_p = true;
232 if (set_p)
234 EXECUTE_IF_SET_IN_SPARSESET (objects_live, j)
235 update_allocno_pressure_excess_length (ira_object_id_map[j]);
236 for (i = 0;
237 (cl = ira_reg_class_super_classes[pclass][i]) != LIM_REG_CLASSES;
238 i++)
240 if (! ira_reg_pressure_class_p[cl])
241 continue;
242 if (high_pressure_start_point[cl] >= 0
243 && curr_reg_pressure[cl] <= ira_class_hard_regs_num[cl])
244 high_pressure_start_point[cl] = -1;
249 /* Determine from the objects_live bitmap whether REGNO is currently live,
250 and occupies only one object. Return false if we have no information. */
251 static bool
252 pseudo_regno_single_word_and_live_p (int regno)
254 ira_allocno_t a = ira_curr_regno_allocno_map[regno];
255 ira_object_t obj;
257 if (a == NULL)
258 return false;
259 if (ALLOCNO_NUM_OBJECTS (a) > 1)
260 return false;
262 obj = ALLOCNO_OBJECT (a, 0);
264 return sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj));
267 /* Mark the pseudo register REGNO as live. Update all information about
268 live ranges and register pressure. */
269 static void
270 mark_pseudo_regno_live (int regno)
272 ira_allocno_t a = ira_curr_regno_allocno_map[regno];
273 enum reg_class pclass;
274 int i, n, nregs;
276 if (a == NULL)
277 return;
279 /* Invalidate because it is referenced. */
280 allocno_saved_at_call[ALLOCNO_NUM (a)] = 0;
282 n = ALLOCNO_NUM_OBJECTS (a);
283 pclass = ira_pressure_class_translate[ALLOCNO_CLASS (a)];
284 nregs = ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
285 if (n > 1)
287 /* We track every subobject separately. */
288 gcc_assert (nregs == n);
289 nregs = 1;
292 for (i = 0; i < n; i++)
294 ira_object_t obj = ALLOCNO_OBJECT (a, i);
296 if (sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj)))
297 continue;
299 inc_register_pressure (pclass, nregs);
300 make_object_born (obj);
304 /* Like mark_pseudo_regno_live, but try to only mark one subword of
305 the pseudo as live. SUBWORD indicates which; a value of 0
306 indicates the low part. */
307 static void
308 mark_pseudo_regno_subword_live (int regno, int subword)
310 ira_allocno_t a = ira_curr_regno_allocno_map[regno];
311 int n;
312 enum reg_class pclass;
313 ira_object_t obj;
315 if (a == NULL)
316 return;
318 /* Invalidate because it is referenced. */
319 allocno_saved_at_call[ALLOCNO_NUM (a)] = 0;
321 n = ALLOCNO_NUM_OBJECTS (a);
322 if (n == 1)
324 mark_pseudo_regno_live (regno);
325 return;
328 pclass = ira_pressure_class_translate[ALLOCNO_CLASS (a)];
329 gcc_assert
330 (n == ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
331 obj = ALLOCNO_OBJECT (a, subword);
333 if (sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj)))
334 return;
336 inc_register_pressure (pclass, 1);
337 make_object_born (obj);
340 /* Mark the register REG as live. Store a 1 in hard_regs_live for
341 this register, record how many consecutive hardware registers it
342 actually needs. */
343 static void
344 mark_hard_reg_live (rtx reg)
346 int regno = REGNO (reg);
348 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
350 int last = END_REGNO (reg);
351 enum reg_class aclass, pclass;
353 while (regno < last)
355 if (! TEST_HARD_REG_BIT (hard_regs_live, regno)
356 && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
358 aclass = ira_hard_regno_allocno_class[regno];
359 pclass = ira_pressure_class_translate[aclass];
360 inc_register_pressure (pclass, 1);
361 make_hard_regno_born (regno);
363 regno++;
368 /* Mark a pseudo, or one of its subwords, as live. REGNO is the pseudo's
369 register number; ORIG_REG is the access in the insn, which may be a
370 subreg. */
371 static void
372 mark_pseudo_reg_live (rtx orig_reg, unsigned regno)
374 if (df_read_modify_subreg_p (orig_reg))
376 mark_pseudo_regno_subword_live (regno,
377 subreg_lowpart_p (orig_reg) ? 0 : 1);
379 else
380 mark_pseudo_regno_live (regno);
383 /* Mark the register referenced by use or def REF as live. */
384 static void
385 mark_ref_live (df_ref ref)
387 rtx reg = DF_REF_REG (ref);
388 rtx orig_reg = reg;
390 if (GET_CODE (reg) == SUBREG)
391 reg = SUBREG_REG (reg);
393 if (REGNO (reg) >= FIRST_PSEUDO_REGISTER)
394 mark_pseudo_reg_live (orig_reg, REGNO (reg));
395 else
396 mark_hard_reg_live (reg);
399 /* Mark the pseudo register REGNO as dead. Update all information about
400 live ranges and register pressure. */
401 static void
402 mark_pseudo_regno_dead (int regno)
404 ira_allocno_t a = ira_curr_regno_allocno_map[regno];
405 int n, i, nregs;
406 enum reg_class cl;
408 if (a == NULL)
409 return;
411 /* Invalidate because it is referenced. */
412 allocno_saved_at_call[ALLOCNO_NUM (a)] = 0;
414 n = ALLOCNO_NUM_OBJECTS (a);
415 cl = ira_pressure_class_translate[ALLOCNO_CLASS (a)];
416 nregs = ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
417 if (n > 1)
419 /* We track every subobject separately. */
420 gcc_assert (nregs == n);
421 nregs = 1;
423 for (i = 0; i < n; i++)
425 ira_object_t obj = ALLOCNO_OBJECT (a, i);
426 if (!sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj)))
427 continue;
429 dec_register_pressure (cl, nregs);
430 make_object_dead (obj);
434 /* Like mark_pseudo_regno_dead, but called when we know that only part of the
435 register dies. SUBWORD indicates which; a value of 0 indicates the low part. */
436 static void
437 mark_pseudo_regno_subword_dead (int regno, int subword)
439 ira_allocno_t a = ira_curr_regno_allocno_map[regno];
440 int n;
441 enum reg_class cl;
442 ira_object_t obj;
444 if (a == NULL)
445 return;
447 /* Invalidate because it is referenced. */
448 allocno_saved_at_call[ALLOCNO_NUM (a)] = 0;
450 n = ALLOCNO_NUM_OBJECTS (a);
451 if (n == 1)
452 /* The allocno as a whole doesn't die in this case. */
453 return;
455 cl = ira_pressure_class_translate[ALLOCNO_CLASS (a)];
456 gcc_assert
457 (n == ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
459 obj = ALLOCNO_OBJECT (a, subword);
460 if (!sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj)))
461 return;
463 dec_register_pressure (cl, 1);
464 make_object_dead (obj);
467 /* Mark the hard register REG as dead. Store a 0 in hard_regs_live for the
468 register. */
469 static void
470 mark_hard_reg_dead (rtx reg)
472 int regno = REGNO (reg);
474 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
476 int last = END_REGNO (reg);
477 enum reg_class aclass, pclass;
479 while (regno < last)
481 if (TEST_HARD_REG_BIT (hard_regs_live, regno))
483 aclass = ira_hard_regno_allocno_class[regno];
484 pclass = ira_pressure_class_translate[aclass];
485 dec_register_pressure (pclass, 1);
486 make_hard_regno_dead (regno);
488 regno++;
493 /* Mark a pseudo, or one of its subwords, as dead. REGNO is the pseudo's
494 register number; ORIG_REG is the access in the insn, which may be a
495 subreg. */
496 static void
497 mark_pseudo_reg_dead (rtx orig_reg, unsigned regno)
499 if (df_read_modify_subreg_p (orig_reg))
501 mark_pseudo_regno_subword_dead (regno,
502 subreg_lowpart_p (orig_reg) ? 0 : 1);
504 else
505 mark_pseudo_regno_dead (regno);
508 /* Mark the register referenced by definition DEF as dead, if the
509 definition is a total one. */
510 static void
511 mark_ref_dead (df_ref def)
513 rtx reg = DF_REF_REG (def);
514 rtx orig_reg = reg;
516 if (DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL))
517 return;
519 if (GET_CODE (reg) == SUBREG)
520 reg = SUBREG_REG (reg);
522 if (DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL)
523 && (GET_CODE (orig_reg) != SUBREG
524 || REGNO (reg) < FIRST_PSEUDO_REGISTER
525 || !df_read_modify_subreg_p (orig_reg)))
526 return;
528 if (REGNO (reg) >= FIRST_PSEUDO_REGISTER)
529 mark_pseudo_reg_dead (orig_reg, REGNO (reg));
530 else
531 mark_hard_reg_dead (reg);
534 /* If REG is a pseudo or a subreg of it, and the class of its allocno
535 intersects CL, make a conflict with pseudo DREG. ORIG_DREG is the
536 rtx actually accessed, it may be identical to DREG or a subreg of it.
537 Advance the current program point before making the conflict if
538 ADVANCE_P. Return TRUE if we will need to advance the current
539 program point. */
540 static bool
541 make_pseudo_conflict (rtx reg, enum reg_class cl, rtx dreg, rtx orig_dreg,
542 bool advance_p)
544 rtx orig_reg = reg;
545 ira_allocno_t a;
547 if (GET_CODE (reg) == SUBREG)
548 reg = SUBREG_REG (reg);
550 if (! REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
551 return advance_p;
553 a = ira_curr_regno_allocno_map[REGNO (reg)];
554 if (! reg_classes_intersect_p (cl, ALLOCNO_CLASS (a)))
555 return advance_p;
557 if (advance_p)
558 curr_point++;
560 mark_pseudo_reg_live (orig_reg, REGNO (reg));
561 mark_pseudo_reg_live (orig_dreg, REGNO (dreg));
562 mark_pseudo_reg_dead (orig_reg, REGNO (reg));
563 mark_pseudo_reg_dead (orig_dreg, REGNO (dreg));
565 return false;
568 /* Check and make if necessary conflicts for pseudo DREG of class
569 DEF_CL of the current insn with input operand USE of class USE_CL.
570 ORIG_DREG is the rtx actually accessed, it may be identical to
571 DREG or a subreg of it. Advance the current program point before
572 making the conflict if ADVANCE_P. Return TRUE if we will need to
573 advance the current program point. */
574 static bool
575 check_and_make_def_use_conflict (rtx dreg, rtx orig_dreg,
576 enum reg_class def_cl, int use,
577 enum reg_class use_cl, bool advance_p)
579 if (! reg_classes_intersect_p (def_cl, use_cl))
580 return advance_p;
582 advance_p = make_pseudo_conflict (recog_data.operand[use],
583 use_cl, dreg, orig_dreg, advance_p);
585 /* Reload may end up swapping commutative operands, so you
586 have to take both orderings into account. The
587 constraints for the two operands can be completely
588 different. (Indeed, if the constraints for the two
589 operands are the same for all alternatives, there's no
590 point marking them as commutative.) */
591 if (use < recog_data.n_operands - 1
592 && recog_data.constraints[use][0] == '%')
593 advance_p
594 = make_pseudo_conflict (recog_data.operand[use + 1],
595 use_cl, dreg, orig_dreg, advance_p);
596 if (use >= 1
597 && recog_data.constraints[use - 1][0] == '%')
598 advance_p
599 = make_pseudo_conflict (recog_data.operand[use - 1],
600 use_cl, dreg, orig_dreg, advance_p);
601 return advance_p;
604 /* Check and make if necessary conflicts for definition DEF of class
605 DEF_CL of the current insn with input operands. Process only
606 constraints of alternative ALT. */
607 static void
608 check_and_make_def_conflict (int alt, int def, enum reg_class def_cl)
610 int use, use_match;
611 ira_allocno_t a;
612 enum reg_class use_cl, acl;
613 bool advance_p;
614 rtx dreg = recog_data.operand[def];
615 rtx orig_dreg = dreg;
617 if (def_cl == NO_REGS)
618 return;
620 if (GET_CODE (dreg) == SUBREG)
621 dreg = SUBREG_REG (dreg);
623 if (! REG_P (dreg) || REGNO (dreg) < FIRST_PSEUDO_REGISTER)
624 return;
626 a = ira_curr_regno_allocno_map[REGNO (dreg)];
627 acl = ALLOCNO_CLASS (a);
628 if (! reg_classes_intersect_p (acl, def_cl))
629 return;
631 advance_p = true;
633 int n_operands = recog_data.n_operands;
634 const operand_alternative *op_alt = &recog_op_alt[alt * n_operands];
635 for (use = 0; use < n_operands; use++)
637 int alt1;
639 if (use == def || recog_data.operand_type[use] == OP_OUT)
640 continue;
642 if (op_alt[use].anything_ok)
643 use_cl = ALL_REGS;
644 else
645 use_cl = op_alt[use].cl;
647 /* If there's any alternative that allows USE to match DEF, do not
648 record a conflict. If that causes us to create an invalid
649 instruction due to the earlyclobber, reload must fix it up. */
650 for (alt1 = 0; alt1 < recog_data.n_alternatives; alt1++)
652 if (!TEST_BIT (preferred_alternatives, alt1))
653 continue;
654 const operand_alternative *op_alt1
655 = &recog_op_alt[alt1 * n_operands];
656 if (op_alt1[use].matches == def
657 || (use < n_operands - 1
658 && recog_data.constraints[use][0] == '%'
659 && op_alt1[use + 1].matches == def)
660 || (use >= 1
661 && recog_data.constraints[use - 1][0] == '%'
662 && op_alt1[use - 1].matches == def))
663 break;
666 if (alt1 < recog_data.n_alternatives)
667 continue;
669 advance_p = check_and_make_def_use_conflict (dreg, orig_dreg, def_cl,
670 use, use_cl, advance_p);
672 if ((use_match = op_alt[use].matches) >= 0)
674 if (use_match == def)
675 continue;
677 if (op_alt[use_match].anything_ok)
678 use_cl = ALL_REGS;
679 else
680 use_cl = op_alt[use_match].cl;
681 advance_p = check_and_make_def_use_conflict (dreg, orig_dreg, def_cl,
682 use, use_cl, advance_p);
687 /* Make conflicts of early clobber pseudo registers of the current
688 insn with its inputs. Avoid introducing unnecessary conflicts by
689 checking classes of the constraints and pseudos because otherwise
690 significant code degradation is possible for some targets. */
691 static void
692 make_early_clobber_and_input_conflicts (void)
694 int alt;
695 int def, def_match;
696 enum reg_class def_cl;
698 int n_alternatives = recog_data.n_alternatives;
699 int n_operands = recog_data.n_operands;
700 const operand_alternative *op_alt = recog_op_alt;
701 for (alt = 0; alt < n_alternatives; alt++, op_alt += n_operands)
702 if (TEST_BIT (preferred_alternatives, alt))
703 for (def = 0; def < n_operands; def++)
705 def_cl = NO_REGS;
706 if (op_alt[def].earlyclobber)
708 if (op_alt[def].anything_ok)
709 def_cl = ALL_REGS;
710 else
711 def_cl = op_alt[def].cl;
712 check_and_make_def_conflict (alt, def, def_cl);
714 if ((def_match = op_alt[def].matches) >= 0
715 && (op_alt[def_match].earlyclobber
716 || op_alt[def].earlyclobber))
718 if (op_alt[def_match].anything_ok)
719 def_cl = ALL_REGS;
720 else
721 def_cl = op_alt[def_match].cl;
722 check_and_make_def_conflict (alt, def, def_cl);
727 /* Mark early clobber hard registers of the current INSN as live (if
728 LIVE_P) or dead. Return true if there are such registers. */
729 static bool
730 mark_hard_reg_early_clobbers (rtx_insn *insn, bool live_p)
732 df_ref def;
733 bool set_p = false;
735 FOR_EACH_INSN_DEF (def, insn)
736 if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER))
738 rtx dreg = DF_REF_REG (def);
740 if (GET_CODE (dreg) == SUBREG)
741 dreg = SUBREG_REG (dreg);
742 if (! REG_P (dreg) || REGNO (dreg) >= FIRST_PSEUDO_REGISTER)
743 continue;
745 /* Hard register clobbers are believed to be early clobber
746 because there is no way to say that non-operand hard
747 register clobbers are not early ones. */
748 if (live_p)
749 mark_ref_live (def);
750 else
751 mark_ref_dead (def);
752 set_p = true;
755 return set_p;
758 /* Checks that CONSTRAINTS permits to use only one hard register. If
759 it is so, the function returns the class of the hard register.
760 Otherwise it returns NO_REGS. */
761 static enum reg_class
762 single_reg_class (const char *constraints, rtx op, rtx equiv_const)
764 int c;
765 enum reg_class cl, next_cl;
766 enum constraint_num cn;
768 cl = NO_REGS;
769 alternative_mask preferred = preferred_alternatives;
770 for (; (c = *constraints); constraints += CONSTRAINT_LEN (c, constraints))
771 if (c == '#')
772 preferred &= ~ALTERNATIVE_BIT (0);
773 else if (c == ',')
774 preferred >>= 1;
775 else if (preferred & 1)
776 switch (c)
778 case 'g':
779 return NO_REGS;
781 default:
782 /* ??? Is this the best way to handle memory constraints? */
783 cn = lookup_constraint (constraints);
784 if (insn_extra_memory_constraint (cn)
785 || insn_extra_address_constraint (cn))
786 return NO_REGS;
787 if (constraint_satisfied_p (op, cn)
788 || (equiv_const != NULL_RTX
789 && CONSTANT_P (equiv_const)
790 && constraint_satisfied_p (equiv_const, cn)))
791 return NO_REGS;
792 next_cl = reg_class_for_constraint (cn);
793 if (next_cl == NO_REGS)
794 break;
795 if (cl == NO_REGS
796 ? ira_class_singleton[next_cl][GET_MODE (op)] < 0
797 : (ira_class_singleton[cl][GET_MODE (op)]
798 != ira_class_singleton[next_cl][GET_MODE (op)]))
799 return NO_REGS;
800 cl = next_cl;
801 break;
803 case '0': case '1': case '2': case '3': case '4':
804 case '5': case '6': case '7': case '8': case '9':
805 next_cl
806 = single_reg_class (recog_data.constraints[c - '0'],
807 recog_data.operand[c - '0'], NULL_RTX);
808 if (cl == NO_REGS
809 ? ira_class_singleton[next_cl][GET_MODE (op)] < 0
810 : (ira_class_singleton[cl][GET_MODE (op)]
811 != ira_class_singleton[next_cl][GET_MODE (op)]))
812 return NO_REGS;
813 cl = next_cl;
814 break;
816 return cl;
819 /* The function checks that operand OP_NUM of the current insn can use
820 only one hard register. If it is so, the function returns the
821 class of the hard register. Otherwise it returns NO_REGS. */
822 static enum reg_class
823 single_reg_operand_class (int op_num)
825 if (op_num < 0 || recog_data.n_alternatives == 0)
826 return NO_REGS;
827 return single_reg_class (recog_data.constraints[op_num],
828 recog_data.operand[op_num], NULL_RTX);
831 /* The function sets up hard register set *SET to hard registers which
832 might be used by insn reloads because the constraints are too
833 strict. */
834 void
835 ira_implicitly_set_insn_hard_regs (HARD_REG_SET *set)
837 int i, c, regno = 0;
838 enum reg_class cl;
839 rtx op;
840 machine_mode mode;
842 CLEAR_HARD_REG_SET (*set);
843 for (i = 0; i < recog_data.n_operands; i++)
845 op = recog_data.operand[i];
847 if (GET_CODE (op) == SUBREG)
848 op = SUBREG_REG (op);
850 if (GET_CODE (op) == SCRATCH
851 || (REG_P (op) && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER))
853 const char *p = recog_data.constraints[i];
855 mode = (GET_CODE (op) == SCRATCH
856 ? GET_MODE (op) : PSEUDO_REGNO_MODE (regno));
857 cl = NO_REGS;
858 alternative_mask preferred = preferred_alternatives;
859 for (; (c = *p); p += CONSTRAINT_LEN (c, p))
860 if (c == '#')
861 preferred &= ~ALTERNATIVE_BIT (0);
862 else if (c == ',')
863 preferred >>= 1;
864 else if (preferred & 1)
866 cl = reg_class_for_constraint (lookup_constraint (p));
867 if (cl != NO_REGS)
869 /* There is no register pressure problem if all of the
870 regs in this class are fixed. */
871 int regno = ira_class_singleton[cl][mode];
872 if (regno >= 0)
873 add_to_hard_reg_set (set, mode, regno);
879 /* Processes input operands, if IN_P, or output operands otherwise of
880 the current insn with FREQ to find allocno which can use only one
881 hard register and makes other currently living allocnos conflicting
882 with the hard register. */
883 static void
884 process_single_reg_class_operands (bool in_p, int freq)
886 int i, regno;
887 unsigned int px;
888 enum reg_class cl;
889 rtx operand;
890 ira_allocno_t operand_a, a;
892 for (i = 0; i < recog_data.n_operands; i++)
894 operand = recog_data.operand[i];
895 if (in_p && recog_data.operand_type[i] != OP_IN
896 && recog_data.operand_type[i] != OP_INOUT)
897 continue;
898 if (! in_p && recog_data.operand_type[i] != OP_OUT
899 && recog_data.operand_type[i] != OP_INOUT)
900 continue;
901 cl = single_reg_operand_class (i);
902 if (cl == NO_REGS)
903 continue;
905 operand_a = NULL;
907 if (GET_CODE (operand) == SUBREG)
908 operand = SUBREG_REG (operand);
910 if (REG_P (operand)
911 && (regno = REGNO (operand)) >= FIRST_PSEUDO_REGISTER)
913 enum reg_class aclass;
915 operand_a = ira_curr_regno_allocno_map[regno];
916 aclass = ALLOCNO_CLASS (operand_a);
917 if (ira_class_subset_p[cl][aclass])
919 /* View the desired allocation of OPERAND as:
921 (REG:YMODE YREGNO),
923 a simplification of:
925 (subreg:YMODE (reg:XMODE XREGNO) OFFSET). */
926 machine_mode ymode, xmode;
927 int xregno, yregno;
928 HOST_WIDE_INT offset;
930 xmode = recog_data.operand_mode[i];
931 xregno = ira_class_singleton[cl][xmode];
932 gcc_assert (xregno >= 0);
933 ymode = ALLOCNO_MODE (operand_a);
934 offset = subreg_lowpart_offset (ymode, xmode);
935 yregno = simplify_subreg_regno (xregno, xmode, offset, ymode);
936 if (yregno >= 0
937 && ira_class_hard_reg_index[aclass][yregno] >= 0)
939 int cost;
941 ira_allocate_and_set_costs
942 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (operand_a),
943 aclass, 0);
944 ira_init_register_move_cost_if_necessary (xmode);
945 cost = freq * (in_p
946 ? ira_register_move_cost[xmode][aclass][cl]
947 : ira_register_move_cost[xmode][cl][aclass]);
948 ALLOCNO_CONFLICT_HARD_REG_COSTS (operand_a)
949 [ira_class_hard_reg_index[aclass][yregno]] -= cost;
954 EXECUTE_IF_SET_IN_SPARSESET (objects_live, px)
956 ira_object_t obj = ira_object_id_map[px];
957 a = OBJECT_ALLOCNO (obj);
958 if (a != operand_a)
960 /* We could increase costs of A instead of making it
961 conflicting with the hard register. But it works worse
962 because it will be spilled in reload in anyway. */
963 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
964 reg_class_contents[cl]);
965 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
966 reg_class_contents[cl]);
972 /* Return true when one of the predecessor edges of BB is marked with
973 EDGE_ABNORMAL_CALL or EDGE_EH. */
974 static bool
975 bb_has_abnormal_call_pred (basic_block bb)
977 edge e;
978 edge_iterator ei;
980 FOR_EACH_EDGE (e, ei, bb->preds)
982 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
983 return true;
985 return false;
988 /* Look through the CALL_INSN_FUNCTION_USAGE of a call insn INSN, and see if
989 we find a SET rtx that we can use to deduce that a register can be cheaply
990 caller-saved. Return such a register, or NULL_RTX if none is found. */
991 static rtx
992 find_call_crossed_cheap_reg (rtx_insn *insn)
994 rtx cheap_reg = NULL_RTX;
995 rtx exp = CALL_INSN_FUNCTION_USAGE (insn);
997 while (exp != NULL)
999 rtx x = XEXP (exp, 0);
1000 if (GET_CODE (x) == SET)
1002 exp = x;
1003 break;
1005 exp = XEXP (exp, 1);
1007 if (exp != NULL)
1009 basic_block bb = BLOCK_FOR_INSN (insn);
1010 rtx reg = SET_SRC (exp);
1011 rtx_insn *prev = PREV_INSN (insn);
1012 while (prev && !(INSN_P (prev)
1013 && BLOCK_FOR_INSN (prev) != bb))
1015 if (NONDEBUG_INSN_P (prev))
1017 rtx set = single_set (prev);
1019 if (set && rtx_equal_p (SET_DEST (set), reg))
1021 rtx src = SET_SRC (set);
1022 if (!REG_P (src) || HARD_REGISTER_P (src)
1023 || !pseudo_regno_single_word_and_live_p (REGNO (src)))
1024 break;
1025 if (!modified_between_p (src, prev, insn))
1026 cheap_reg = src;
1027 break;
1029 if (set && rtx_equal_p (SET_SRC (set), reg))
1031 rtx dest = SET_DEST (set);
1032 if (!REG_P (dest) || HARD_REGISTER_P (dest)
1033 || !pseudo_regno_single_word_and_live_p (REGNO (dest)))
1034 break;
1035 if (!modified_between_p (dest, prev, insn))
1036 cheap_reg = dest;
1037 break;
1040 if (reg_overlap_mentioned_p (reg, PATTERN (prev)))
1041 break;
1043 prev = PREV_INSN (prev);
1046 return cheap_reg;
1049 /* Process insns of the basic block given by its LOOP_TREE_NODE to
1050 update allocno live ranges, allocno hard register conflicts,
1051 intersected calls, and register pressure info for allocnos for the
1052 basic block for and regions containing the basic block. */
1053 static void
1054 process_bb_node_lives (ira_loop_tree_node_t loop_tree_node)
1056 int i, freq;
1057 unsigned int j;
1058 basic_block bb;
1059 rtx_insn *insn;
1060 bitmap_iterator bi;
1061 bitmap reg_live_out;
1062 unsigned int px;
1063 bool set_p;
1065 bb = loop_tree_node->bb;
1066 if (bb != NULL)
1068 for (i = 0; i < ira_pressure_classes_num; i++)
1070 curr_reg_pressure[ira_pressure_classes[i]] = 0;
1071 high_pressure_start_point[ira_pressure_classes[i]] = -1;
1073 curr_bb_node = loop_tree_node;
1074 reg_live_out = df_get_live_out (bb);
1075 sparseset_clear (objects_live);
1076 REG_SET_TO_HARD_REG_SET (hard_regs_live, reg_live_out);
1077 AND_COMPL_HARD_REG_SET (hard_regs_live, eliminable_regset);
1078 AND_COMPL_HARD_REG_SET (hard_regs_live, ira_no_alloc_regs);
1079 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1080 if (TEST_HARD_REG_BIT (hard_regs_live, i))
1082 enum reg_class aclass, pclass, cl;
1084 aclass = ira_allocno_class_translate[REGNO_REG_CLASS (i)];
1085 pclass = ira_pressure_class_translate[aclass];
1086 for (j = 0;
1087 (cl = ira_reg_class_super_classes[pclass][j])
1088 != LIM_REG_CLASSES;
1089 j++)
1091 if (! ira_reg_pressure_class_p[cl])
1092 continue;
1093 curr_reg_pressure[cl]++;
1094 if (curr_bb_node->reg_pressure[cl] < curr_reg_pressure[cl])
1095 curr_bb_node->reg_pressure[cl] = curr_reg_pressure[cl];
1096 ira_assert (curr_reg_pressure[cl]
1097 <= ira_class_hard_regs_num[cl]);
1100 EXECUTE_IF_SET_IN_BITMAP (reg_live_out, FIRST_PSEUDO_REGISTER, j, bi)
1101 mark_pseudo_regno_live (j);
1103 freq = REG_FREQ_FROM_BB (bb);
1104 if (freq == 0)
1105 freq = 1;
1107 /* Invalidate all allocno_saved_at_call entries. */
1108 last_call_num++;
1110 /* Scan the code of this basic block, noting which allocnos and
1111 hard regs are born or die.
1113 Note that this loop treats uninitialized values as live until
1114 the beginning of the block. For example, if an instruction
1115 uses (reg:DI foo), and only (subreg:SI (reg:DI foo) 0) is ever
1116 set, FOO will remain live until the beginning of the block.
1117 Likewise if FOO is not set at all. This is unnecessarily
1118 pessimistic, but it probably doesn't matter much in practice. */
1119 FOR_BB_INSNS_REVERSE (bb, insn)
1121 ira_allocno_t a;
1122 df_ref def, use;
1123 bool call_p;
1125 if (!NONDEBUG_INSN_P (insn))
1126 continue;
1128 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
1129 fprintf (ira_dump_file, " Insn %u(l%d): point = %d\n",
1130 INSN_UID (insn), loop_tree_node->parent->loop_num,
1131 curr_point);
1133 call_p = CALL_P (insn);
1134 #ifdef REAL_PIC_OFFSET_TABLE_REGNUM
1135 int regno;
1136 bool clear_pic_use_conflict_p = false;
1137 /* Processing insn usage in call insn can create conflict
1138 with pic pseudo and pic hard reg and that is wrong.
1139 Check this situation and fix it at the end of the insn
1140 processing. */
1141 if (call_p && pic_offset_table_rtx != NULL_RTX
1142 && (regno = REGNO (pic_offset_table_rtx)) >= FIRST_PSEUDO_REGISTER
1143 && (a = ira_curr_regno_allocno_map[regno]) != NULL)
1144 clear_pic_use_conflict_p
1145 = (find_regno_fusage (insn, USE, REAL_PIC_OFFSET_TABLE_REGNUM)
1146 && ! TEST_HARD_REG_BIT (OBJECT_CONFLICT_HARD_REGS
1147 (ALLOCNO_OBJECT (a, 0)),
1148 REAL_PIC_OFFSET_TABLE_REGNUM));
1149 #endif
1151 /* Mark each defined value as live. We need to do this for
1152 unused values because they still conflict with quantities
1153 that are live at the time of the definition.
1155 Ignore DF_REF_MAY_CLOBBERs on a call instruction. Such
1156 references represent the effect of the called function
1157 on a call-clobbered register. Marking the register as
1158 live would stop us from allocating it to a call-crossing
1159 allocno. */
1160 FOR_EACH_INSN_DEF (def, insn)
1161 if (!call_p || !DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
1162 mark_ref_live (def);
1164 /* If INSN has multiple outputs, then any value used in one
1165 of the outputs conflicts with the other outputs. Model this
1166 by making the used value live during the output phase.
1168 It is unsafe to use !single_set here since it will ignore
1169 an unused output. Just because an output is unused does
1170 not mean the compiler can assume the side effect will not
1171 occur. Consider if ALLOCNO appears in the address of an
1172 output and we reload the output. If we allocate ALLOCNO
1173 to the same hard register as an unused output we could
1174 set the hard register before the output reload insn. */
1175 if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
1176 FOR_EACH_INSN_USE (use, insn)
1178 int i;
1179 rtx reg;
1181 reg = DF_REF_REG (use);
1182 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1184 rtx set;
1186 set = XVECEXP (PATTERN (insn), 0, i);
1187 if (GET_CODE (set) == SET
1188 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
1190 /* After the previous loop, this is a no-op if
1191 REG is contained within SET_DEST (SET). */
1192 mark_ref_live (use);
1193 break;
1198 extract_insn (insn);
1199 preferred_alternatives = get_preferred_alternatives (insn);
1200 preprocess_constraints (insn);
1201 process_single_reg_class_operands (false, freq);
1203 /* See which defined values die here. */
1204 FOR_EACH_INSN_DEF (def, insn)
1205 if (!call_p || !DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
1206 mark_ref_dead (def);
1208 if (call_p)
1210 /* Try to find a SET in the CALL_INSN_FUNCTION_USAGE, and from
1211 there, try to find a pseudo that is live across the call but
1212 can be cheaply reconstructed from the return value. */
1213 rtx cheap_reg = find_call_crossed_cheap_reg (insn);
1214 if (cheap_reg != NULL_RTX)
1215 add_reg_note (insn, REG_RETURNED, cheap_reg);
1217 last_call_num++;
1218 sparseset_clear (allocnos_processed);
1219 /* The current set of live allocnos are live across the call. */
1220 EXECUTE_IF_SET_IN_SPARSESET (objects_live, i)
1222 ira_object_t obj = ira_object_id_map[i];
1223 a = OBJECT_ALLOCNO (obj);
1224 int num = ALLOCNO_NUM (a);
1225 HARD_REG_SET this_call_used_reg_set;
1227 get_call_reg_set_usage (insn, &this_call_used_reg_set,
1228 call_used_reg_set);
1230 /* Don't allocate allocnos that cross setjmps or any
1231 call, if this function receives a nonlocal
1232 goto. */
1233 if (cfun->has_nonlocal_label
1234 || find_reg_note (insn, REG_SETJMP,
1235 NULL_RTX) != NULL_RTX)
1237 SET_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj));
1238 SET_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
1240 if (can_throw_internal (insn))
1242 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
1243 this_call_used_reg_set);
1244 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
1245 this_call_used_reg_set);
1248 if (sparseset_bit_p (allocnos_processed, num))
1249 continue;
1250 sparseset_set_bit (allocnos_processed, num);
1252 if (allocno_saved_at_call[num] != last_call_num)
1253 /* Here we are mimicking caller-save.c behavior
1254 which does not save hard register at a call if
1255 it was saved on previous call in the same basic
1256 block and the hard register was not mentioned
1257 between the two calls. */
1258 ALLOCNO_CALL_FREQ (a) += freq;
1259 /* Mark it as saved at the next call. */
1260 allocno_saved_at_call[num] = last_call_num + 1;
1261 ALLOCNO_CALLS_CROSSED_NUM (a)++;
1262 IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a),
1263 this_call_used_reg_set);
1264 if (cheap_reg != NULL_RTX
1265 && ALLOCNO_REGNO (a) == (int) REGNO (cheap_reg))
1266 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a)++;
1270 make_early_clobber_and_input_conflicts ();
1272 curr_point++;
1274 /* Mark each used value as live. */
1275 FOR_EACH_INSN_USE (use, insn)
1276 mark_ref_live (use);
1278 process_single_reg_class_operands (true, freq);
1280 set_p = mark_hard_reg_early_clobbers (insn, true);
1282 if (set_p)
1284 mark_hard_reg_early_clobbers (insn, false);
1286 /* Mark each hard reg as live again. For example, a
1287 hard register can be in clobber and in an insn
1288 input. */
1289 FOR_EACH_INSN_USE (use, insn)
1291 rtx ureg = DF_REF_REG (use);
1293 if (GET_CODE (ureg) == SUBREG)
1294 ureg = SUBREG_REG (ureg);
1295 if (! REG_P (ureg) || REGNO (ureg) >= FIRST_PSEUDO_REGISTER)
1296 continue;
1298 mark_ref_live (use);
1302 #ifdef REAL_PIC_OFFSET_TABLE_REGNUM
1303 if (clear_pic_use_conflict_p)
1305 regno = REGNO (pic_offset_table_rtx);
1306 a = ira_curr_regno_allocno_map[regno];
1307 CLEAR_HARD_REG_BIT (OBJECT_CONFLICT_HARD_REGS (ALLOCNO_OBJECT (a, 0)),
1308 REAL_PIC_OFFSET_TABLE_REGNUM);
1309 CLEAR_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS
1310 (ALLOCNO_OBJECT (a, 0)),
1311 REAL_PIC_OFFSET_TABLE_REGNUM);
1313 #endif
1314 curr_point++;
1317 if (bb_has_eh_pred (bb))
1318 for (j = 0; ; ++j)
1320 unsigned int regno = EH_RETURN_DATA_REGNO (j);
1321 if (regno == INVALID_REGNUM)
1322 break;
1323 make_hard_regno_born (regno);
1326 /* Allocnos can't go in stack regs at the start of a basic block
1327 that is reached by an abnormal edge. Likewise for call
1328 clobbered regs, because caller-save, fixup_abnormal_edges and
1329 possibly the table driven EH machinery are not quite ready to
1330 handle such allocnos live across such edges. */
1331 if (bb_has_abnormal_pred (bb))
1333 #ifdef STACK_REGS
1334 EXECUTE_IF_SET_IN_SPARSESET (objects_live, px)
1336 ira_allocno_t a = OBJECT_ALLOCNO (ira_object_id_map[px]);
1338 ALLOCNO_NO_STACK_REG_P (a) = true;
1339 ALLOCNO_TOTAL_NO_STACK_REG_P (a) = true;
1341 for (px = FIRST_STACK_REG; px <= LAST_STACK_REG; px++)
1342 make_hard_regno_born (px);
1343 #endif
1344 /* No need to record conflicts for call clobbered regs if we
1345 have nonlocal labels around, as we don't ever try to
1346 allocate such regs in this case. */
1347 if (!cfun->has_nonlocal_label && bb_has_abnormal_call_pred (bb))
1348 for (px = 0; px < FIRST_PSEUDO_REGISTER; px++)
1349 if (call_used_regs[px])
1350 make_hard_regno_born (px);
1353 EXECUTE_IF_SET_IN_SPARSESET (objects_live, i)
1354 make_object_dead (ira_object_id_map[i]);
1356 curr_point++;
1359 /* Propagate register pressure to upper loop tree nodes. */
1360 if (loop_tree_node != ira_loop_tree_root)
1361 for (i = 0; i < ira_pressure_classes_num; i++)
1363 enum reg_class pclass;
1365 pclass = ira_pressure_classes[i];
1366 if (loop_tree_node->reg_pressure[pclass]
1367 > loop_tree_node->parent->reg_pressure[pclass])
1368 loop_tree_node->parent->reg_pressure[pclass]
1369 = loop_tree_node->reg_pressure[pclass];
1373 /* Create and set up IRA_START_POINT_RANGES and
1374 IRA_FINISH_POINT_RANGES. */
1375 static void
1376 create_start_finish_chains (void)
1378 ira_object_t obj;
1379 ira_object_iterator oi;
1380 live_range_t r;
1382 ira_start_point_ranges
1383 = (live_range_t *) ira_allocate (ira_max_point * sizeof (live_range_t));
1384 memset (ira_start_point_ranges, 0, ira_max_point * sizeof (live_range_t));
1385 ira_finish_point_ranges
1386 = (live_range_t *) ira_allocate (ira_max_point * sizeof (live_range_t));
1387 memset (ira_finish_point_ranges, 0, ira_max_point * sizeof (live_range_t));
1388 FOR_EACH_OBJECT (obj, oi)
1389 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
1391 r->start_next = ira_start_point_ranges[r->start];
1392 ira_start_point_ranges[r->start] = r;
1393 r->finish_next = ira_finish_point_ranges[r->finish];
1394 ira_finish_point_ranges[r->finish] = r;
1398 /* Rebuild IRA_START_POINT_RANGES and IRA_FINISH_POINT_RANGES after
1399 new live ranges and program points were added as a result if new
1400 insn generation. */
1401 void
1402 ira_rebuild_start_finish_chains (void)
1404 ira_free (ira_finish_point_ranges);
1405 ira_free (ira_start_point_ranges);
1406 create_start_finish_chains ();
1409 /* Compress allocno live ranges by removing program points where
1410 nothing happens. */
1411 static void
1412 remove_some_program_points_and_update_live_ranges (void)
1414 unsigned i;
1415 int n;
1416 int *map;
1417 ira_object_t obj;
1418 ira_object_iterator oi;
1419 live_range_t r, prev_r, next_r;
1420 sbitmap born_or_dead, born, dead;
1421 sbitmap_iterator sbi;
1422 bool born_p, dead_p, prev_born_p, prev_dead_p;
1424 born = sbitmap_alloc (ira_max_point);
1425 dead = sbitmap_alloc (ira_max_point);
1426 bitmap_clear (born);
1427 bitmap_clear (dead);
1428 FOR_EACH_OBJECT (obj, oi)
1429 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
1431 ira_assert (r->start <= r->finish);
1432 bitmap_set_bit (born, r->start);
1433 bitmap_set_bit (dead, r->finish);
1436 born_or_dead = sbitmap_alloc (ira_max_point);
1437 bitmap_ior (born_or_dead, born, dead);
1438 map = (int *) ira_allocate (sizeof (int) * ira_max_point);
1439 n = -1;
1440 prev_born_p = prev_dead_p = false;
1441 EXECUTE_IF_SET_IN_BITMAP (born_or_dead, 0, i, sbi)
1443 born_p = bitmap_bit_p (born, i);
1444 dead_p = bitmap_bit_p (dead, i);
1445 if ((prev_born_p && ! prev_dead_p && born_p && ! dead_p)
1446 || (prev_dead_p && ! prev_born_p && dead_p && ! born_p))
1447 map[i] = n;
1448 else
1449 map[i] = ++n;
1450 prev_born_p = born_p;
1451 prev_dead_p = dead_p;
1453 sbitmap_free (born_or_dead);
1454 sbitmap_free (born);
1455 sbitmap_free (dead);
1456 n++;
1457 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
1458 fprintf (ira_dump_file, "Compressing live ranges: from %d to %d - %d%%\n",
1459 ira_max_point, n, 100 * n / ira_max_point);
1460 ira_max_point = n;
1462 FOR_EACH_OBJECT (obj, oi)
1463 for (r = OBJECT_LIVE_RANGES (obj), prev_r = NULL; r != NULL; r = next_r)
1465 next_r = r->next;
1466 r->start = map[r->start];
1467 r->finish = map[r->finish];
1468 if (prev_r == NULL || prev_r->start > r->finish + 1)
1470 prev_r = r;
1471 continue;
1473 prev_r->start = r->start;
1474 prev_r->next = next_r;
1475 ira_finish_live_range (r);
1478 ira_free (map);
1481 /* Print live ranges R to file F. */
1482 void
1483 ira_print_live_range_list (FILE *f, live_range_t r)
1485 for (; r != NULL; r = r->next)
1486 fprintf (f, " [%d..%d]", r->start, r->finish);
1487 fprintf (f, "\n");
1490 DEBUG_FUNCTION void
1491 debug (live_range &ref)
1493 ira_print_live_range_list (stderr, &ref);
1496 DEBUG_FUNCTION void
1497 debug (live_range *ptr)
1499 if (ptr)
1500 debug (*ptr);
1501 else
1502 fprintf (stderr, "<nil>\n");
1505 /* Print live ranges R to stderr. */
1506 void
1507 ira_debug_live_range_list (live_range_t r)
1509 ira_print_live_range_list (stderr, r);
1512 /* Print live ranges of object OBJ to file F. */
1513 static void
1514 print_object_live_ranges (FILE *f, ira_object_t obj)
1516 ira_print_live_range_list (f, OBJECT_LIVE_RANGES (obj));
1519 /* Print live ranges of allocno A to file F. */
1520 static void
1521 print_allocno_live_ranges (FILE *f, ira_allocno_t a)
1523 int n = ALLOCNO_NUM_OBJECTS (a);
1524 int i;
1526 for (i = 0; i < n; i++)
1528 fprintf (f, " a%d(r%d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
1529 if (n > 1)
1530 fprintf (f, " [%d]", i);
1531 fprintf (f, "):");
1532 print_object_live_ranges (f, ALLOCNO_OBJECT (a, i));
1536 /* Print live ranges of allocno A to stderr. */
1537 void
1538 ira_debug_allocno_live_ranges (ira_allocno_t a)
1540 print_allocno_live_ranges (stderr, a);
1543 /* Print live ranges of all allocnos to file F. */
1544 static void
1545 print_live_ranges (FILE *f)
1547 ira_allocno_t a;
1548 ira_allocno_iterator ai;
1550 FOR_EACH_ALLOCNO (a, ai)
1551 print_allocno_live_ranges (f, a);
1554 /* Print live ranges of all allocnos to stderr. */
1555 void
1556 ira_debug_live_ranges (void)
1558 print_live_ranges (stderr);
1561 /* The main entry function creates live ranges, set up
1562 CONFLICT_HARD_REGS and TOTAL_CONFLICT_HARD_REGS for objects, and
1563 calculate register pressure info. */
1564 void
1565 ira_create_allocno_live_ranges (void)
1567 objects_live = sparseset_alloc (ira_objects_num);
1568 allocnos_processed = sparseset_alloc (ira_allocnos_num);
1569 curr_point = 0;
1570 last_call_num = 0;
1571 allocno_saved_at_call
1572 = (int *) ira_allocate (ira_allocnos_num * sizeof (int));
1573 memset (allocno_saved_at_call, 0, ira_allocnos_num * sizeof (int));
1574 ira_traverse_loop_tree (true, ira_loop_tree_root, NULL,
1575 process_bb_node_lives);
1576 ira_max_point = curr_point;
1577 create_start_finish_chains ();
1578 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
1579 print_live_ranges (ira_dump_file);
1580 /* Clean up. */
1581 ira_free (allocno_saved_at_call);
1582 sparseset_free (objects_live);
1583 sparseset_free (allocnos_processed);
1586 /* Compress allocno live ranges. */
1587 void
1588 ira_compress_allocno_live_ranges (void)
1590 remove_some_program_points_and_update_live_ranges ();
1591 ira_rebuild_start_finish_chains ();
1592 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
1594 fprintf (ira_dump_file, "Ranges after the compression:\n");
1595 print_live_ranges (ira_dump_file);
1599 /* Free arrays IRA_START_POINT_RANGES and IRA_FINISH_POINT_RANGES. */
1600 void
1601 ira_finish_allocno_live_ranges (void)
1603 ira_free (ira_finish_point_ranges);
1604 ira_free (ira_start_point_ranges);