2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ira-lives.c
blobdd9614c868e36d4668fbfdf1d405ff89ee838b18
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 "input.h"
34 #include "function.h"
35 #include "basic-block.h"
36 #include "insn-config.h"
37 #include "recog.h"
38 #include "diagnostic-core.h"
39 #include "params.h"
40 #include "df.h"
41 #include "sbitmap.h"
42 #include "sparseset.h"
43 #include "ira-int.h"
45 /* The code in this file is similar to one in global but the code
46 works on the allocno basis and creates live ranges instead of
47 pseudo-register conflicts. */
49 /* Program points are enumerated by numbers from range
50 0..IRA_MAX_POINT-1. There are approximately two times more program
51 points than insns. Program points are places in the program where
52 liveness info can be changed. In most general case (there are more
53 complicated cases too) some program points correspond to places
54 where input operand dies and other ones correspond to places where
55 output operands are born. */
56 int ira_max_point;
58 /* Arrays of size IRA_MAX_POINT mapping a program point to the allocno
59 live ranges with given start/finish point. */
60 live_range_t *ira_start_point_ranges, *ira_finish_point_ranges;
62 /* Number of the current program point. */
63 static int curr_point;
65 /* Point where register pressure excess started or -1 if there is no
66 register pressure excess. Excess pressure for a register class at
67 some point means that there are more allocnos of given register
68 class living at the point than number of hard-registers of the
69 class available for the allocation. It is defined only for
70 pressure classes. */
71 static int high_pressure_start_point[N_REG_CLASSES];
73 /* Objects live at current point in the scan. */
74 static sparseset objects_live;
76 /* A temporary bitmap used in functions that wish to avoid visiting an allocno
77 multiple times. */
78 static sparseset allocnos_processed;
80 /* Set of hard regs (except eliminable ones) currently live. */
81 static HARD_REG_SET hard_regs_live;
83 /* The loop tree node corresponding to the current basic block. */
84 static ira_loop_tree_node_t curr_bb_node;
86 /* The number of the last processed call. */
87 static int last_call_num;
88 /* The number of last call at which given allocno was saved. */
89 static int *allocno_saved_at_call;
91 /* The value of get_preferred_alternatives for the current instruction,
92 supplemental to recog_data. */
93 static alternative_mask preferred_alternatives;
95 /* Record the birth of hard register REGNO, updating hard_regs_live and
96 hard reg conflict information for living allocnos. */
97 static void
98 make_hard_regno_born (int regno)
100 unsigned int i;
102 SET_HARD_REG_BIT (hard_regs_live, regno);
103 EXECUTE_IF_SET_IN_SPARSESET (objects_live, i)
105 ira_object_t obj = ira_object_id_map[i];
107 SET_HARD_REG_BIT (OBJECT_CONFLICT_HARD_REGS (obj), regno);
108 SET_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), regno);
112 /* Process the death of hard register REGNO. This updates
113 hard_regs_live. */
114 static void
115 make_hard_regno_dead (int regno)
117 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
120 /* Record the birth of object OBJ. Set a bit for it in objects_live,
121 start a new live range for it if necessary and update hard register
122 conflicts. */
123 static void
124 make_object_born (ira_object_t obj)
126 live_range_t lr = OBJECT_LIVE_RANGES (obj);
128 sparseset_set_bit (objects_live, OBJECT_CONFLICT_ID (obj));
129 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj), hard_regs_live);
130 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), hard_regs_live);
132 if (lr == NULL
133 || (lr->finish != curr_point && lr->finish + 1 != curr_point))
134 ira_add_live_range_to_object (obj, curr_point, -1);
137 /* Update ALLOCNO_EXCESS_PRESSURE_POINTS_NUM for the allocno
138 associated with object OBJ. */
139 static void
140 update_allocno_pressure_excess_length (ira_object_t obj)
142 ira_allocno_t a = OBJECT_ALLOCNO (obj);
143 int start, i;
144 enum reg_class aclass, pclass, cl;
145 live_range_t p;
147 aclass = ALLOCNO_CLASS (a);
148 pclass = ira_pressure_class_translate[aclass];
149 for (i = 0;
150 (cl = ira_reg_class_super_classes[pclass][i]) != LIM_REG_CLASSES;
151 i++)
153 if (! ira_reg_pressure_class_p[cl])
154 continue;
155 if (high_pressure_start_point[cl] < 0)
156 continue;
157 p = OBJECT_LIVE_RANGES (obj);
158 ira_assert (p != NULL);
159 start = (high_pressure_start_point[cl] > p->start
160 ? high_pressure_start_point[cl] : p->start);
161 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) += curr_point - start + 1;
165 /* Process the death of object OBJ, which is associated with allocno
166 A. This finishes the current live range for it. */
167 static void
168 make_object_dead (ira_object_t obj)
170 live_range_t lr;
172 sparseset_clear_bit (objects_live, OBJECT_CONFLICT_ID (obj));
173 lr = OBJECT_LIVE_RANGES (obj);
174 ira_assert (lr != NULL);
175 lr->finish = curr_point;
176 update_allocno_pressure_excess_length (obj);
179 /* The current register pressures for each pressure class for the current
180 basic block. */
181 static int curr_reg_pressure[N_REG_CLASSES];
183 /* Record that register pressure for PCLASS increased by N registers.
184 Update the current register pressure, maximal register pressure for
185 the current BB and the start point of the register pressure
186 excess. */
187 static void
188 inc_register_pressure (enum reg_class pclass, int n)
190 int i;
191 enum reg_class cl;
193 for (i = 0;
194 (cl = ira_reg_class_super_classes[pclass][i]) != LIM_REG_CLASSES;
195 i++)
197 if (! ira_reg_pressure_class_p[cl])
198 continue;
199 curr_reg_pressure[cl] += n;
200 if (high_pressure_start_point[cl] < 0
201 && (curr_reg_pressure[cl] > ira_class_hard_regs_num[cl]))
202 high_pressure_start_point[cl] = curr_point;
203 if (curr_bb_node->reg_pressure[cl] < curr_reg_pressure[cl])
204 curr_bb_node->reg_pressure[cl] = curr_reg_pressure[cl];
208 /* Record that register pressure for PCLASS has decreased by NREGS
209 registers; update current register pressure, start point of the
210 register pressure excess, and register pressure excess length for
211 living allocnos. */
213 static void
214 dec_register_pressure (enum reg_class pclass, int nregs)
216 int i;
217 unsigned int j;
218 enum reg_class cl;
219 bool set_p = false;
221 for (i = 0;
222 (cl = ira_reg_class_super_classes[pclass][i]) != LIM_REG_CLASSES;
223 i++)
225 if (! ira_reg_pressure_class_p[cl])
226 continue;
227 curr_reg_pressure[cl] -= nregs;
228 ira_assert (curr_reg_pressure[cl] >= 0);
229 if (high_pressure_start_point[cl] >= 0
230 && curr_reg_pressure[cl] <= ira_class_hard_regs_num[cl])
231 set_p = true;
233 if (set_p)
235 EXECUTE_IF_SET_IN_SPARSESET (objects_live, j)
236 update_allocno_pressure_excess_length (ira_object_id_map[j]);
237 for (i = 0;
238 (cl = ira_reg_class_super_classes[pclass][i]) != LIM_REG_CLASSES;
239 i++)
241 if (! ira_reg_pressure_class_p[cl])
242 continue;
243 if (high_pressure_start_point[cl] >= 0
244 && curr_reg_pressure[cl] <= ira_class_hard_regs_num[cl])
245 high_pressure_start_point[cl] = -1;
250 /* Determine from the objects_live bitmap whether REGNO is currently live,
251 and occupies only one object. Return false if we have no information. */
252 static bool
253 pseudo_regno_single_word_and_live_p (int regno)
255 ira_allocno_t a = ira_curr_regno_allocno_map[regno];
256 ira_object_t obj;
258 if (a == NULL)
259 return false;
260 if (ALLOCNO_NUM_OBJECTS (a) > 1)
261 return false;
263 obj = ALLOCNO_OBJECT (a, 0);
265 return sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj));
268 /* Mark the pseudo register REGNO as live. Update all information about
269 live ranges and register pressure. */
270 static void
271 mark_pseudo_regno_live (int regno)
273 ira_allocno_t a = ira_curr_regno_allocno_map[regno];
274 enum reg_class pclass;
275 int i, n, nregs;
277 if (a == NULL)
278 return;
280 /* Invalidate because it is referenced. */
281 allocno_saved_at_call[ALLOCNO_NUM (a)] = 0;
283 n = ALLOCNO_NUM_OBJECTS (a);
284 pclass = ira_pressure_class_translate[ALLOCNO_CLASS (a)];
285 nregs = ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
286 if (n > 1)
288 /* We track every subobject separately. */
289 gcc_assert (nregs == n);
290 nregs = 1;
293 for (i = 0; i < n; i++)
295 ira_object_t obj = ALLOCNO_OBJECT (a, i);
297 if (sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj)))
298 continue;
300 inc_register_pressure (pclass, nregs);
301 make_object_born (obj);
305 /* Like mark_pseudo_regno_live, but try to only mark one subword of
306 the pseudo as live. SUBWORD indicates which; a value of 0
307 indicates the low part. */
308 static void
309 mark_pseudo_regno_subword_live (int regno, int subword)
311 ira_allocno_t a = ira_curr_regno_allocno_map[regno];
312 int n;
313 enum reg_class pclass;
314 ira_object_t obj;
316 if (a == NULL)
317 return;
319 /* Invalidate because it is referenced. */
320 allocno_saved_at_call[ALLOCNO_NUM (a)] = 0;
322 n = ALLOCNO_NUM_OBJECTS (a);
323 if (n == 1)
325 mark_pseudo_regno_live (regno);
326 return;
329 pclass = ira_pressure_class_translate[ALLOCNO_CLASS (a)];
330 gcc_assert
331 (n == ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
332 obj = ALLOCNO_OBJECT (a, subword);
334 if (sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj)))
335 return;
337 inc_register_pressure (pclass, 1);
338 make_object_born (obj);
341 /* Mark the register REG as live. Store a 1 in hard_regs_live for
342 this register, record how many consecutive hardware registers it
343 actually needs. */
344 static void
345 mark_hard_reg_live (rtx reg)
347 int regno = REGNO (reg);
349 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
351 int last = END_REGNO (reg);
352 enum reg_class aclass, pclass;
354 while (regno < last)
356 if (! TEST_HARD_REG_BIT (hard_regs_live, regno)
357 && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
359 aclass = ira_hard_regno_allocno_class[regno];
360 pclass = ira_pressure_class_translate[aclass];
361 inc_register_pressure (pclass, 1);
362 make_hard_regno_born (regno);
364 regno++;
369 /* Mark a pseudo, or one of its subwords, as live. REGNO is the pseudo's
370 register number; ORIG_REG is the access in the insn, which may be a
371 subreg. */
372 static void
373 mark_pseudo_reg_live (rtx orig_reg, unsigned regno)
375 if (df_read_modify_subreg_p (orig_reg))
377 mark_pseudo_regno_subword_live (regno,
378 subreg_lowpart_p (orig_reg) ? 0 : 1);
380 else
381 mark_pseudo_regno_live (regno);
384 /* Mark the register referenced by use or def REF as live. */
385 static void
386 mark_ref_live (df_ref ref)
388 rtx reg = DF_REF_REG (ref);
389 rtx orig_reg = reg;
391 if (GET_CODE (reg) == SUBREG)
392 reg = SUBREG_REG (reg);
394 if (REGNO (reg) >= FIRST_PSEUDO_REGISTER)
395 mark_pseudo_reg_live (orig_reg, REGNO (reg));
396 else
397 mark_hard_reg_live (reg);
400 /* Mark the pseudo register REGNO as dead. Update all information about
401 live ranges and register pressure. */
402 static void
403 mark_pseudo_regno_dead (int regno)
405 ira_allocno_t a = ira_curr_regno_allocno_map[regno];
406 int n, i, nregs;
407 enum reg_class cl;
409 if (a == NULL)
410 return;
412 /* Invalidate because it is referenced. */
413 allocno_saved_at_call[ALLOCNO_NUM (a)] = 0;
415 n = ALLOCNO_NUM_OBJECTS (a);
416 cl = ira_pressure_class_translate[ALLOCNO_CLASS (a)];
417 nregs = ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
418 if (n > 1)
420 /* We track every subobject separately. */
421 gcc_assert (nregs == n);
422 nregs = 1;
424 for (i = 0; i < n; i++)
426 ira_object_t obj = ALLOCNO_OBJECT (a, i);
427 if (!sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj)))
428 continue;
430 dec_register_pressure (cl, nregs);
431 make_object_dead (obj);
435 /* Like mark_pseudo_regno_dead, but called when we know that only part of the
436 register dies. SUBWORD indicates which; a value of 0 indicates the low part. */
437 static void
438 mark_pseudo_regno_subword_dead (int regno, int subword)
440 ira_allocno_t a = ira_curr_regno_allocno_map[regno];
441 int n;
442 enum reg_class cl;
443 ira_object_t obj;
445 if (a == NULL)
446 return;
448 /* Invalidate because it is referenced. */
449 allocno_saved_at_call[ALLOCNO_NUM (a)] = 0;
451 n = ALLOCNO_NUM_OBJECTS (a);
452 if (n == 1)
453 /* The allocno as a whole doesn't die in this case. */
454 return;
456 cl = ira_pressure_class_translate[ALLOCNO_CLASS (a)];
457 gcc_assert
458 (n == ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
460 obj = ALLOCNO_OBJECT (a, subword);
461 if (!sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj)))
462 return;
464 dec_register_pressure (cl, 1);
465 make_object_dead (obj);
468 /* Mark the hard register REG as dead. Store a 0 in hard_regs_live for the
469 register. */
470 static void
471 mark_hard_reg_dead (rtx reg)
473 int regno = REGNO (reg);
475 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
477 int last = END_REGNO (reg);
478 enum reg_class aclass, pclass;
480 while (regno < last)
482 if (TEST_HARD_REG_BIT (hard_regs_live, regno))
484 aclass = ira_hard_regno_allocno_class[regno];
485 pclass = ira_pressure_class_translate[aclass];
486 dec_register_pressure (pclass, 1);
487 make_hard_regno_dead (regno);
489 regno++;
494 /* Mark a pseudo, or one of its subwords, as dead. REGNO is the pseudo's
495 register number; ORIG_REG is the access in the insn, which may be a
496 subreg. */
497 static void
498 mark_pseudo_reg_dead (rtx orig_reg, unsigned regno)
500 if (df_read_modify_subreg_p (orig_reg))
502 mark_pseudo_regno_subword_dead (regno,
503 subreg_lowpart_p (orig_reg) ? 0 : 1);
505 else
506 mark_pseudo_regno_dead (regno);
509 /* Mark the register referenced by definition DEF as dead, if the
510 definition is a total one. */
511 static void
512 mark_ref_dead (df_ref def)
514 rtx reg = DF_REF_REG (def);
515 rtx orig_reg = reg;
517 if (DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL))
518 return;
520 if (GET_CODE (reg) == SUBREG)
521 reg = SUBREG_REG (reg);
523 if (DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL)
524 && (GET_CODE (orig_reg) != SUBREG
525 || REGNO (reg) < FIRST_PSEUDO_REGISTER
526 || !df_read_modify_subreg_p (orig_reg)))
527 return;
529 if (REGNO (reg) >= FIRST_PSEUDO_REGISTER)
530 mark_pseudo_reg_dead (orig_reg, REGNO (reg));
531 else
532 mark_hard_reg_dead (reg);
535 /* If REG is a pseudo or a subreg of it, and the class of its allocno
536 intersects CL, make a conflict with pseudo DREG. ORIG_DREG is the
537 rtx actually accessed, it may be identical to DREG or a subreg of it.
538 Advance the current program point before making the conflict if
539 ADVANCE_P. Return TRUE if we will need to advance the current
540 program point. */
541 static bool
542 make_pseudo_conflict (rtx reg, enum reg_class cl, rtx dreg, rtx orig_dreg,
543 bool advance_p)
545 rtx orig_reg = reg;
546 ira_allocno_t a;
548 if (GET_CODE (reg) == SUBREG)
549 reg = SUBREG_REG (reg);
551 if (! REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
552 return advance_p;
554 a = ira_curr_regno_allocno_map[REGNO (reg)];
555 if (! reg_classes_intersect_p (cl, ALLOCNO_CLASS (a)))
556 return advance_p;
558 if (advance_p)
559 curr_point++;
561 mark_pseudo_reg_live (orig_reg, REGNO (reg));
562 mark_pseudo_reg_live (orig_dreg, REGNO (dreg));
563 mark_pseudo_reg_dead (orig_reg, REGNO (reg));
564 mark_pseudo_reg_dead (orig_dreg, REGNO (dreg));
566 return false;
569 /* Check and make if necessary conflicts for pseudo DREG of class
570 DEF_CL of the current insn with input operand USE of class USE_CL.
571 ORIG_DREG is the rtx actually accessed, it may be identical to
572 DREG or a subreg of it. Advance the current program point before
573 making the conflict if ADVANCE_P. Return TRUE if we will need to
574 advance the current program point. */
575 static bool
576 check_and_make_def_use_conflict (rtx dreg, rtx orig_dreg,
577 enum reg_class def_cl, int use,
578 enum reg_class use_cl, bool advance_p)
580 if (! reg_classes_intersect_p (def_cl, use_cl))
581 return advance_p;
583 advance_p = make_pseudo_conflict (recog_data.operand[use],
584 use_cl, dreg, orig_dreg, advance_p);
586 /* Reload may end up swapping commutative operands, so you
587 have to take both orderings into account. The
588 constraints for the two operands can be completely
589 different. (Indeed, if the constraints for the two
590 operands are the same for all alternatives, there's no
591 point marking them as commutative.) */
592 if (use < recog_data.n_operands - 1
593 && recog_data.constraints[use][0] == '%')
594 advance_p
595 = make_pseudo_conflict (recog_data.operand[use + 1],
596 use_cl, dreg, orig_dreg, advance_p);
597 if (use >= 1
598 && recog_data.constraints[use - 1][0] == '%')
599 advance_p
600 = make_pseudo_conflict (recog_data.operand[use - 1],
601 use_cl, dreg, orig_dreg, advance_p);
602 return advance_p;
605 /* Check and make if necessary conflicts for definition DEF of class
606 DEF_CL of the current insn with input operands. Process only
607 constraints of alternative ALT. */
608 static void
609 check_and_make_def_conflict (int alt, int def, enum reg_class def_cl)
611 int use, use_match;
612 ira_allocno_t a;
613 enum reg_class use_cl, acl;
614 bool advance_p;
615 rtx dreg = recog_data.operand[def];
616 rtx orig_dreg = dreg;
618 if (def_cl == NO_REGS)
619 return;
621 if (GET_CODE (dreg) == SUBREG)
622 dreg = SUBREG_REG (dreg);
624 if (! REG_P (dreg) || REGNO (dreg) < FIRST_PSEUDO_REGISTER)
625 return;
627 a = ira_curr_regno_allocno_map[REGNO (dreg)];
628 acl = ALLOCNO_CLASS (a);
629 if (! reg_classes_intersect_p (acl, def_cl))
630 return;
632 advance_p = true;
634 int n_operands = recog_data.n_operands;
635 const operand_alternative *op_alt = &recog_op_alt[alt * n_operands];
636 for (use = 0; use < n_operands; use++)
638 int alt1;
640 if (use == def || recog_data.operand_type[use] == OP_OUT)
641 continue;
643 if (op_alt[use].anything_ok)
644 use_cl = ALL_REGS;
645 else
646 use_cl = op_alt[use].cl;
648 /* If there's any alternative that allows USE to match DEF, do not
649 record a conflict. If that causes us to create an invalid
650 instruction due to the earlyclobber, reload must fix it up. */
651 for (alt1 = 0; alt1 < recog_data.n_alternatives; alt1++)
653 if (!TEST_BIT (preferred_alternatives, alt1))
654 continue;
655 const operand_alternative *op_alt1
656 = &recog_op_alt[alt1 * n_operands];
657 if (op_alt1[use].matches == def
658 || (use < n_operands - 1
659 && recog_data.constraints[use][0] == '%'
660 && op_alt1[use + 1].matches == def)
661 || (use >= 1
662 && recog_data.constraints[use - 1][0] == '%'
663 && op_alt1[use - 1].matches == def))
664 break;
667 if (alt1 < recog_data.n_alternatives)
668 continue;
670 advance_p = check_and_make_def_use_conflict (dreg, orig_dreg, def_cl,
671 use, use_cl, advance_p);
673 if ((use_match = op_alt[use].matches) >= 0)
675 if (use_match == def)
676 continue;
678 if (op_alt[use_match].anything_ok)
679 use_cl = ALL_REGS;
680 else
681 use_cl = op_alt[use_match].cl;
682 advance_p = check_and_make_def_use_conflict (dreg, orig_dreg, def_cl,
683 use, use_cl, advance_p);
688 /* Make conflicts of early clobber pseudo registers of the current
689 insn with its inputs. Avoid introducing unnecessary conflicts by
690 checking classes of the constraints and pseudos because otherwise
691 significant code degradation is possible for some targets. */
692 static void
693 make_early_clobber_and_input_conflicts (void)
695 int alt;
696 int def, def_match;
697 enum reg_class def_cl;
699 int n_alternatives = recog_data.n_alternatives;
700 int n_operands = recog_data.n_operands;
701 const operand_alternative *op_alt = recog_op_alt;
702 for (alt = 0; alt < n_alternatives; alt++, op_alt += n_operands)
703 if (TEST_BIT (preferred_alternatives, alt))
704 for (def = 0; def < n_operands; def++)
706 def_cl = NO_REGS;
707 if (op_alt[def].earlyclobber)
709 if (op_alt[def].anything_ok)
710 def_cl = ALL_REGS;
711 else
712 def_cl = op_alt[def].cl;
713 check_and_make_def_conflict (alt, def, def_cl);
715 if ((def_match = op_alt[def].matches) >= 0
716 && (op_alt[def_match].earlyclobber
717 || op_alt[def].earlyclobber))
719 if (op_alt[def_match].anything_ok)
720 def_cl = ALL_REGS;
721 else
722 def_cl = op_alt[def_match].cl;
723 check_and_make_def_conflict (alt, def, def_cl);
728 /* Mark early clobber hard registers of the current INSN as live (if
729 LIVE_P) or dead. Return true if there are such registers. */
730 static bool
731 mark_hard_reg_early_clobbers (rtx_insn *insn, bool live_p)
733 df_ref def;
734 bool set_p = false;
736 FOR_EACH_INSN_DEF (def, insn)
737 if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER))
739 rtx dreg = DF_REF_REG (def);
741 if (GET_CODE (dreg) == SUBREG)
742 dreg = SUBREG_REG (dreg);
743 if (! REG_P (dreg) || REGNO (dreg) >= FIRST_PSEUDO_REGISTER)
744 continue;
746 /* Hard register clobbers are believed to be early clobber
747 because there is no way to say that non-operand hard
748 register clobbers are not early ones. */
749 if (live_p)
750 mark_ref_live (def);
751 else
752 mark_ref_dead (def);
753 set_p = true;
756 return set_p;
759 /* Checks that CONSTRAINTS permits to use only one hard register. If
760 it is so, the function returns the class of the hard register.
761 Otherwise it returns NO_REGS. */
762 static enum reg_class
763 single_reg_class (const char *constraints, rtx op, rtx equiv_const)
765 int c;
766 enum reg_class cl, next_cl;
767 enum constraint_num cn;
769 cl = NO_REGS;
770 alternative_mask preferred = preferred_alternatives;
771 for (; (c = *constraints); constraints += CONSTRAINT_LEN (c, constraints))
772 if (c == '#')
773 preferred &= ~ALTERNATIVE_BIT (0);
774 else if (c == ',')
775 preferred >>= 1;
776 else if (preferred & 1)
777 switch (c)
779 case 'g':
780 return NO_REGS;
782 default:
783 /* ??? Is this the best way to handle memory constraints? */
784 cn = lookup_constraint (constraints);
785 if (insn_extra_memory_constraint (cn)
786 || insn_extra_address_constraint (cn))
787 return NO_REGS;
788 if (constraint_satisfied_p (op, cn)
789 || (equiv_const != NULL_RTX
790 && CONSTANT_P (equiv_const)
791 && constraint_satisfied_p (equiv_const, cn)))
792 return NO_REGS;
793 next_cl = reg_class_for_constraint (cn);
794 if (next_cl == NO_REGS)
795 break;
796 if (cl == NO_REGS
797 ? ira_class_singleton[next_cl][GET_MODE (op)] < 0
798 : (ira_class_singleton[cl][GET_MODE (op)]
799 != ira_class_singleton[next_cl][GET_MODE (op)]))
800 return NO_REGS;
801 cl = next_cl;
802 break;
804 case '0': case '1': case '2': case '3': case '4':
805 case '5': case '6': case '7': case '8': case '9':
806 next_cl
807 = single_reg_class (recog_data.constraints[c - '0'],
808 recog_data.operand[c - '0'], NULL_RTX);
809 if (cl == NO_REGS
810 ? ira_class_singleton[next_cl][GET_MODE (op)] < 0
811 : (ira_class_singleton[cl][GET_MODE (op)]
812 != ira_class_singleton[next_cl][GET_MODE (op)]))
813 return NO_REGS;
814 cl = next_cl;
815 break;
817 return cl;
820 /* The function checks that operand OP_NUM of the current insn can use
821 only one hard register. If it is so, the function returns the
822 class of the hard register. Otherwise it returns NO_REGS. */
823 static enum reg_class
824 single_reg_operand_class (int op_num)
826 if (op_num < 0 || recog_data.n_alternatives == 0)
827 return NO_REGS;
828 return single_reg_class (recog_data.constraints[op_num],
829 recog_data.operand[op_num], NULL_RTX);
832 /* The function sets up hard register set *SET to hard registers which
833 might be used by insn reloads because the constraints are too
834 strict. */
835 void
836 ira_implicitly_set_insn_hard_regs (HARD_REG_SET *set)
838 int i, c, regno = 0;
839 enum reg_class cl;
840 rtx op;
841 machine_mode mode;
843 CLEAR_HARD_REG_SET (*set);
844 for (i = 0; i < recog_data.n_operands; i++)
846 op = recog_data.operand[i];
848 if (GET_CODE (op) == SUBREG)
849 op = SUBREG_REG (op);
851 if (GET_CODE (op) == SCRATCH
852 || (REG_P (op) && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER))
854 const char *p = recog_data.constraints[i];
856 mode = (GET_CODE (op) == SCRATCH
857 ? GET_MODE (op) : PSEUDO_REGNO_MODE (regno));
858 cl = NO_REGS;
859 alternative_mask preferred = preferred_alternatives;
860 for (; (c = *p); p += CONSTRAINT_LEN (c, p))
861 if (c == '#')
862 preferred &= ~ALTERNATIVE_BIT (0);
863 else if (c == ',')
864 preferred >>= 1;
865 else if (preferred & 1)
867 cl = reg_class_for_constraint (lookup_constraint (p));
868 if (cl != NO_REGS)
870 /* There is no register pressure problem if all of the
871 regs in this class are fixed. */
872 int regno = ira_class_singleton[cl][mode];
873 if (regno >= 0)
874 add_to_hard_reg_set (set, mode, regno);
880 /* Processes input operands, if IN_P, or output operands otherwise of
881 the current insn with FREQ to find allocno which can use only one
882 hard register and makes other currently living allocnos conflicting
883 with the hard register. */
884 static void
885 process_single_reg_class_operands (bool in_p, int freq)
887 int i, regno;
888 unsigned int px;
889 enum reg_class cl;
890 rtx operand;
891 ira_allocno_t operand_a, a;
893 for (i = 0; i < recog_data.n_operands; i++)
895 operand = recog_data.operand[i];
896 if (in_p && recog_data.operand_type[i] != OP_IN
897 && recog_data.operand_type[i] != OP_INOUT)
898 continue;
899 if (! in_p && recog_data.operand_type[i] != OP_OUT
900 && recog_data.operand_type[i] != OP_INOUT)
901 continue;
902 cl = single_reg_operand_class (i);
903 if (cl == NO_REGS)
904 continue;
906 operand_a = NULL;
908 if (GET_CODE (operand) == SUBREG)
909 operand = SUBREG_REG (operand);
911 if (REG_P (operand)
912 && (regno = REGNO (operand)) >= FIRST_PSEUDO_REGISTER)
914 enum reg_class aclass;
916 operand_a = ira_curr_regno_allocno_map[regno];
917 aclass = ALLOCNO_CLASS (operand_a);
918 if (ira_class_subset_p[cl][aclass])
920 /* View the desired allocation of OPERAND as:
922 (REG:YMODE YREGNO),
924 a simplification of:
926 (subreg:YMODE (reg:XMODE XREGNO) OFFSET). */
927 machine_mode ymode, xmode;
928 int xregno, yregno;
929 HOST_WIDE_INT offset;
931 xmode = recog_data.operand_mode[i];
932 xregno = ira_class_singleton[cl][xmode];
933 gcc_assert (xregno >= 0);
934 ymode = ALLOCNO_MODE (operand_a);
935 offset = subreg_lowpart_offset (ymode, xmode);
936 yregno = simplify_subreg_regno (xregno, xmode, offset, ymode);
937 if (yregno >= 0
938 && ira_class_hard_reg_index[aclass][yregno] >= 0)
940 int cost;
942 ira_allocate_and_set_costs
943 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (operand_a),
944 aclass, 0);
945 ira_init_register_move_cost_if_necessary (xmode);
946 cost = freq * (in_p
947 ? ira_register_move_cost[xmode][aclass][cl]
948 : ira_register_move_cost[xmode][cl][aclass]);
949 ALLOCNO_CONFLICT_HARD_REG_COSTS (operand_a)
950 [ira_class_hard_reg_index[aclass][yregno]] -= cost;
955 EXECUTE_IF_SET_IN_SPARSESET (objects_live, px)
957 ira_object_t obj = ira_object_id_map[px];
958 a = OBJECT_ALLOCNO (obj);
959 if (a != operand_a)
961 /* We could increase costs of A instead of making it
962 conflicting with the hard register. But it works worse
963 because it will be spilled in reload in anyway. */
964 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
965 reg_class_contents[cl]);
966 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
967 reg_class_contents[cl]);
973 /* Return true when one of the predecessor edges of BB is marked with
974 EDGE_ABNORMAL_CALL or EDGE_EH. */
975 static bool
976 bb_has_abnormal_call_pred (basic_block bb)
978 edge e;
979 edge_iterator ei;
981 FOR_EACH_EDGE (e, ei, bb->preds)
983 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
984 return true;
986 return false;
989 /* Look through the CALL_INSN_FUNCTION_USAGE of a call insn INSN, and see if
990 we find a SET rtx that we can use to deduce that a register can be cheaply
991 caller-saved. Return such a register, or NULL_RTX if none is found. */
992 static rtx
993 find_call_crossed_cheap_reg (rtx_insn *insn)
995 rtx cheap_reg = NULL_RTX;
996 rtx exp = CALL_INSN_FUNCTION_USAGE (insn);
998 while (exp != NULL)
1000 rtx x = XEXP (exp, 0);
1001 if (GET_CODE (x) == SET)
1003 exp = x;
1004 break;
1006 exp = XEXP (exp, 1);
1008 if (exp != NULL)
1010 basic_block bb = BLOCK_FOR_INSN (insn);
1011 rtx reg = SET_SRC (exp);
1012 rtx_insn *prev = PREV_INSN (insn);
1013 while (prev && !(INSN_P (prev)
1014 && BLOCK_FOR_INSN (prev) != bb))
1016 if (NONDEBUG_INSN_P (prev))
1018 rtx set = single_set (prev);
1020 if (set && rtx_equal_p (SET_DEST (set), reg))
1022 rtx src = SET_SRC (set);
1023 if (!REG_P (src) || HARD_REGISTER_P (src)
1024 || !pseudo_regno_single_word_and_live_p (REGNO (src)))
1025 break;
1026 if (!modified_between_p (src, prev, insn))
1027 cheap_reg = src;
1028 break;
1030 if (set && rtx_equal_p (SET_SRC (set), reg))
1032 rtx dest = SET_DEST (set);
1033 if (!REG_P (dest) || HARD_REGISTER_P (dest)
1034 || !pseudo_regno_single_word_and_live_p (REGNO (dest)))
1035 break;
1036 if (!modified_between_p (dest, prev, insn))
1037 cheap_reg = dest;
1038 break;
1041 if (reg_overlap_mentioned_p (reg, PATTERN (prev)))
1042 break;
1044 prev = PREV_INSN (prev);
1047 return cheap_reg;
1050 /* Process insns of the basic block given by its LOOP_TREE_NODE to
1051 update allocno live ranges, allocno hard register conflicts,
1052 intersected calls, and register pressure info for allocnos for the
1053 basic block for and regions containing the basic block. */
1054 static void
1055 process_bb_node_lives (ira_loop_tree_node_t loop_tree_node)
1057 int i, freq;
1058 unsigned int j;
1059 basic_block bb;
1060 rtx_insn *insn;
1061 bitmap_iterator bi;
1062 bitmap reg_live_out;
1063 unsigned int px;
1064 bool set_p;
1066 bb = loop_tree_node->bb;
1067 if (bb != NULL)
1069 for (i = 0; i < ira_pressure_classes_num; i++)
1071 curr_reg_pressure[ira_pressure_classes[i]] = 0;
1072 high_pressure_start_point[ira_pressure_classes[i]] = -1;
1074 curr_bb_node = loop_tree_node;
1075 reg_live_out = df_get_live_out (bb);
1076 sparseset_clear (objects_live);
1077 REG_SET_TO_HARD_REG_SET (hard_regs_live, reg_live_out);
1078 AND_COMPL_HARD_REG_SET (hard_regs_live, eliminable_regset);
1079 AND_COMPL_HARD_REG_SET (hard_regs_live, ira_no_alloc_regs);
1080 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1081 if (TEST_HARD_REG_BIT (hard_regs_live, i))
1083 enum reg_class aclass, pclass, cl;
1085 aclass = ira_allocno_class_translate[REGNO_REG_CLASS (i)];
1086 pclass = ira_pressure_class_translate[aclass];
1087 for (j = 0;
1088 (cl = ira_reg_class_super_classes[pclass][j])
1089 != LIM_REG_CLASSES;
1090 j++)
1092 if (! ira_reg_pressure_class_p[cl])
1093 continue;
1094 curr_reg_pressure[cl]++;
1095 if (curr_bb_node->reg_pressure[cl] < curr_reg_pressure[cl])
1096 curr_bb_node->reg_pressure[cl] = curr_reg_pressure[cl];
1097 ira_assert (curr_reg_pressure[cl]
1098 <= ira_class_hard_regs_num[cl]);
1101 EXECUTE_IF_SET_IN_BITMAP (reg_live_out, FIRST_PSEUDO_REGISTER, j, bi)
1102 mark_pseudo_regno_live (j);
1104 freq = REG_FREQ_FROM_BB (bb);
1105 if (freq == 0)
1106 freq = 1;
1108 /* Invalidate all allocno_saved_at_call entries. */
1109 last_call_num++;
1111 /* Scan the code of this basic block, noting which allocnos and
1112 hard regs are born or die.
1114 Note that this loop treats uninitialized values as live until
1115 the beginning of the block. For example, if an instruction
1116 uses (reg:DI foo), and only (subreg:SI (reg:DI foo) 0) is ever
1117 set, FOO will remain live until the beginning of the block.
1118 Likewise if FOO is not set at all. This is unnecessarily
1119 pessimistic, but it probably doesn't matter much in practice. */
1120 FOR_BB_INSNS_REVERSE (bb, insn)
1122 ira_allocno_t a;
1123 df_ref def, use;
1124 bool call_p;
1126 if (!NONDEBUG_INSN_P (insn))
1127 continue;
1129 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
1130 fprintf (ira_dump_file, " Insn %u(l%d): point = %d\n",
1131 INSN_UID (insn), loop_tree_node->parent->loop_num,
1132 curr_point);
1134 call_p = CALL_P (insn);
1135 #ifdef REAL_PIC_OFFSET_TABLE_REGNUM
1136 int regno;
1137 bool clear_pic_use_conflict_p = false;
1138 /* Processing insn usage in call insn can create conflict
1139 with pic pseudo and pic hard reg and that is wrong.
1140 Check this situation and fix it at the end of the insn
1141 processing. */
1142 if (call_p && pic_offset_table_rtx != NULL_RTX
1143 && (regno = REGNO (pic_offset_table_rtx)) >= FIRST_PSEUDO_REGISTER
1144 && (a = ira_curr_regno_allocno_map[regno]) != NULL)
1145 clear_pic_use_conflict_p
1146 = (find_regno_fusage (insn, USE, REAL_PIC_OFFSET_TABLE_REGNUM)
1147 && ! TEST_HARD_REG_BIT (OBJECT_CONFLICT_HARD_REGS
1148 (ALLOCNO_OBJECT (a, 0)),
1149 REAL_PIC_OFFSET_TABLE_REGNUM));
1150 #endif
1152 /* Mark each defined value as live. We need to do this for
1153 unused values because they still conflict with quantities
1154 that are live at the time of the definition.
1156 Ignore DF_REF_MAY_CLOBBERs on a call instruction. Such
1157 references represent the effect of the called function
1158 on a call-clobbered register. Marking the register as
1159 live would stop us from allocating it to a call-crossing
1160 allocno. */
1161 FOR_EACH_INSN_DEF (def, insn)
1162 if (!call_p || !DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
1163 mark_ref_live (def);
1165 /* If INSN has multiple outputs, then any value used in one
1166 of the outputs conflicts with the other outputs. Model this
1167 by making the used value live during the output phase.
1169 It is unsafe to use !single_set here since it will ignore
1170 an unused output. Just because an output is unused does
1171 not mean the compiler can assume the side effect will not
1172 occur. Consider if ALLOCNO appears in the address of an
1173 output and we reload the output. If we allocate ALLOCNO
1174 to the same hard register as an unused output we could
1175 set the hard register before the output reload insn. */
1176 if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
1177 FOR_EACH_INSN_USE (use, insn)
1179 int i;
1180 rtx reg;
1182 reg = DF_REF_REG (use);
1183 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1185 rtx set;
1187 set = XVECEXP (PATTERN (insn), 0, i);
1188 if (GET_CODE (set) == SET
1189 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
1191 /* After the previous loop, this is a no-op if
1192 REG is contained within SET_DEST (SET). */
1193 mark_ref_live (use);
1194 break;
1199 extract_insn (insn);
1200 preferred_alternatives = get_preferred_alternatives (insn);
1201 preprocess_constraints (insn);
1202 process_single_reg_class_operands (false, freq);
1204 /* See which defined values die here. */
1205 FOR_EACH_INSN_DEF (def, insn)
1206 if (!call_p || !DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
1207 mark_ref_dead (def);
1209 if (call_p)
1211 /* Try to find a SET in the CALL_INSN_FUNCTION_USAGE, and from
1212 there, try to find a pseudo that is live across the call but
1213 can be cheaply reconstructed from the return value. */
1214 rtx cheap_reg = find_call_crossed_cheap_reg (insn);
1215 if (cheap_reg != NULL_RTX)
1216 add_reg_note (insn, REG_RETURNED, cheap_reg);
1218 last_call_num++;
1219 sparseset_clear (allocnos_processed);
1220 /* The current set of live allocnos are live across the call. */
1221 EXECUTE_IF_SET_IN_SPARSESET (objects_live, i)
1223 ira_object_t obj = ira_object_id_map[i];
1224 a = OBJECT_ALLOCNO (obj);
1225 int num = ALLOCNO_NUM (a);
1226 HARD_REG_SET this_call_used_reg_set;
1228 get_call_reg_set_usage (insn, &this_call_used_reg_set,
1229 call_used_reg_set);
1231 /* Don't allocate allocnos that cross setjmps or any
1232 call, if this function receives a nonlocal
1233 goto. */
1234 if (cfun->has_nonlocal_label
1235 || find_reg_note (insn, REG_SETJMP,
1236 NULL_RTX) != NULL_RTX)
1238 SET_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj));
1239 SET_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
1241 if (can_throw_internal (insn))
1243 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
1244 this_call_used_reg_set);
1245 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
1246 this_call_used_reg_set);
1249 if (sparseset_bit_p (allocnos_processed, num))
1250 continue;
1251 sparseset_set_bit (allocnos_processed, num);
1253 if (allocno_saved_at_call[num] != last_call_num)
1254 /* Here we are mimicking caller-save.c behavior
1255 which does not save hard register at a call if
1256 it was saved on previous call in the same basic
1257 block and the hard register was not mentioned
1258 between the two calls. */
1259 ALLOCNO_CALL_FREQ (a) += freq;
1260 /* Mark it as saved at the next call. */
1261 allocno_saved_at_call[num] = last_call_num + 1;
1262 ALLOCNO_CALLS_CROSSED_NUM (a)++;
1263 IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a),
1264 this_call_used_reg_set);
1265 if (cheap_reg != NULL_RTX
1266 && ALLOCNO_REGNO (a) == (int) REGNO (cheap_reg))
1267 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a)++;
1271 make_early_clobber_and_input_conflicts ();
1273 curr_point++;
1275 /* Mark each used value as live. */
1276 FOR_EACH_INSN_USE (use, insn)
1277 mark_ref_live (use);
1279 process_single_reg_class_operands (true, freq);
1281 set_p = mark_hard_reg_early_clobbers (insn, true);
1283 if (set_p)
1285 mark_hard_reg_early_clobbers (insn, false);
1287 /* Mark each hard reg as live again. For example, a
1288 hard register can be in clobber and in an insn
1289 input. */
1290 FOR_EACH_INSN_USE (use, insn)
1292 rtx ureg = DF_REF_REG (use);
1294 if (GET_CODE (ureg) == SUBREG)
1295 ureg = SUBREG_REG (ureg);
1296 if (! REG_P (ureg) || REGNO (ureg) >= FIRST_PSEUDO_REGISTER)
1297 continue;
1299 mark_ref_live (use);
1303 #ifdef REAL_PIC_OFFSET_TABLE_REGNUM
1304 if (clear_pic_use_conflict_p)
1306 regno = REGNO (pic_offset_table_rtx);
1307 a = ira_curr_regno_allocno_map[regno];
1308 CLEAR_HARD_REG_BIT (OBJECT_CONFLICT_HARD_REGS (ALLOCNO_OBJECT (a, 0)),
1309 REAL_PIC_OFFSET_TABLE_REGNUM);
1310 CLEAR_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS
1311 (ALLOCNO_OBJECT (a, 0)),
1312 REAL_PIC_OFFSET_TABLE_REGNUM);
1314 #endif
1315 curr_point++;
1318 if (bb_has_eh_pred (bb))
1319 for (j = 0; ; ++j)
1321 unsigned int regno = EH_RETURN_DATA_REGNO (j);
1322 if (regno == INVALID_REGNUM)
1323 break;
1324 make_hard_regno_born (regno);
1327 /* Allocnos can't go in stack regs at the start of a basic block
1328 that is reached by an abnormal edge. Likewise for call
1329 clobbered regs, because caller-save, fixup_abnormal_edges and
1330 possibly the table driven EH machinery are not quite ready to
1331 handle such allocnos live across such edges. */
1332 if (bb_has_abnormal_pred (bb))
1334 #ifdef STACK_REGS
1335 EXECUTE_IF_SET_IN_SPARSESET (objects_live, px)
1337 ira_allocno_t a = OBJECT_ALLOCNO (ira_object_id_map[px]);
1339 ALLOCNO_NO_STACK_REG_P (a) = true;
1340 ALLOCNO_TOTAL_NO_STACK_REG_P (a) = true;
1342 for (px = FIRST_STACK_REG; px <= LAST_STACK_REG; px++)
1343 make_hard_regno_born (px);
1344 #endif
1345 /* No need to record conflicts for call clobbered regs if we
1346 have nonlocal labels around, as we don't ever try to
1347 allocate such regs in this case. */
1348 if (!cfun->has_nonlocal_label && bb_has_abnormal_call_pred (bb))
1349 for (px = 0; px < FIRST_PSEUDO_REGISTER; px++)
1350 if (call_used_regs[px])
1351 make_hard_regno_born (px);
1354 EXECUTE_IF_SET_IN_SPARSESET (objects_live, i)
1355 make_object_dead (ira_object_id_map[i]);
1357 curr_point++;
1360 /* Propagate register pressure to upper loop tree nodes. */
1361 if (loop_tree_node != ira_loop_tree_root)
1362 for (i = 0; i < ira_pressure_classes_num; i++)
1364 enum reg_class pclass;
1366 pclass = ira_pressure_classes[i];
1367 if (loop_tree_node->reg_pressure[pclass]
1368 > loop_tree_node->parent->reg_pressure[pclass])
1369 loop_tree_node->parent->reg_pressure[pclass]
1370 = loop_tree_node->reg_pressure[pclass];
1374 /* Create and set up IRA_START_POINT_RANGES and
1375 IRA_FINISH_POINT_RANGES. */
1376 static void
1377 create_start_finish_chains (void)
1379 ira_object_t obj;
1380 ira_object_iterator oi;
1381 live_range_t r;
1383 ira_start_point_ranges
1384 = (live_range_t *) ira_allocate (ira_max_point * sizeof (live_range_t));
1385 memset (ira_start_point_ranges, 0, ira_max_point * sizeof (live_range_t));
1386 ira_finish_point_ranges
1387 = (live_range_t *) ira_allocate (ira_max_point * sizeof (live_range_t));
1388 memset (ira_finish_point_ranges, 0, ira_max_point * sizeof (live_range_t));
1389 FOR_EACH_OBJECT (obj, oi)
1390 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
1392 r->start_next = ira_start_point_ranges[r->start];
1393 ira_start_point_ranges[r->start] = r;
1394 r->finish_next = ira_finish_point_ranges[r->finish];
1395 ira_finish_point_ranges[r->finish] = r;
1399 /* Rebuild IRA_START_POINT_RANGES and IRA_FINISH_POINT_RANGES after
1400 new live ranges and program points were added as a result if new
1401 insn generation. */
1402 void
1403 ira_rebuild_start_finish_chains (void)
1405 ira_free (ira_finish_point_ranges);
1406 ira_free (ira_start_point_ranges);
1407 create_start_finish_chains ();
1410 /* Compress allocno live ranges by removing program points where
1411 nothing happens. */
1412 static void
1413 remove_some_program_points_and_update_live_ranges (void)
1415 unsigned i;
1416 int n;
1417 int *map;
1418 ira_object_t obj;
1419 ira_object_iterator oi;
1420 live_range_t r, prev_r, next_r;
1421 sbitmap born_or_dead, born, dead;
1422 sbitmap_iterator sbi;
1423 bool born_p, dead_p, prev_born_p, prev_dead_p;
1425 born = sbitmap_alloc (ira_max_point);
1426 dead = sbitmap_alloc (ira_max_point);
1427 bitmap_clear (born);
1428 bitmap_clear (dead);
1429 FOR_EACH_OBJECT (obj, oi)
1430 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
1432 ira_assert (r->start <= r->finish);
1433 bitmap_set_bit (born, r->start);
1434 bitmap_set_bit (dead, r->finish);
1437 born_or_dead = sbitmap_alloc (ira_max_point);
1438 bitmap_ior (born_or_dead, born, dead);
1439 map = (int *) ira_allocate (sizeof (int) * ira_max_point);
1440 n = -1;
1441 prev_born_p = prev_dead_p = false;
1442 EXECUTE_IF_SET_IN_BITMAP (born_or_dead, 0, i, sbi)
1444 born_p = bitmap_bit_p (born, i);
1445 dead_p = bitmap_bit_p (dead, i);
1446 if ((prev_born_p && ! prev_dead_p && born_p && ! dead_p)
1447 || (prev_dead_p && ! prev_born_p && dead_p && ! born_p))
1448 map[i] = n;
1449 else
1450 map[i] = ++n;
1451 prev_born_p = born_p;
1452 prev_dead_p = dead_p;
1454 sbitmap_free (born_or_dead);
1455 sbitmap_free (born);
1456 sbitmap_free (dead);
1457 n++;
1458 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
1459 fprintf (ira_dump_file, "Compressing live ranges: from %d to %d - %d%%\n",
1460 ira_max_point, n, 100 * n / ira_max_point);
1461 ira_max_point = n;
1463 FOR_EACH_OBJECT (obj, oi)
1464 for (r = OBJECT_LIVE_RANGES (obj), prev_r = NULL; r != NULL; r = next_r)
1466 next_r = r->next;
1467 r->start = map[r->start];
1468 r->finish = map[r->finish];
1469 if (prev_r == NULL || prev_r->start > r->finish + 1)
1471 prev_r = r;
1472 continue;
1474 prev_r->start = r->start;
1475 prev_r->next = next_r;
1476 ira_finish_live_range (r);
1479 ira_free (map);
1482 /* Print live ranges R to file F. */
1483 void
1484 ira_print_live_range_list (FILE *f, live_range_t r)
1486 for (; r != NULL; r = r->next)
1487 fprintf (f, " [%d..%d]", r->start, r->finish);
1488 fprintf (f, "\n");
1491 DEBUG_FUNCTION void
1492 debug (live_range &ref)
1494 ira_print_live_range_list (stderr, &ref);
1497 DEBUG_FUNCTION void
1498 debug (live_range *ptr)
1500 if (ptr)
1501 debug (*ptr);
1502 else
1503 fprintf (stderr, "<nil>\n");
1506 /* Print live ranges R to stderr. */
1507 void
1508 ira_debug_live_range_list (live_range_t r)
1510 ira_print_live_range_list (stderr, r);
1513 /* Print live ranges of object OBJ to file F. */
1514 static void
1515 print_object_live_ranges (FILE *f, ira_object_t obj)
1517 ira_print_live_range_list (f, OBJECT_LIVE_RANGES (obj));
1520 /* Print live ranges of allocno A to file F. */
1521 static void
1522 print_allocno_live_ranges (FILE *f, ira_allocno_t a)
1524 int n = ALLOCNO_NUM_OBJECTS (a);
1525 int i;
1527 for (i = 0; i < n; i++)
1529 fprintf (f, " a%d(r%d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
1530 if (n > 1)
1531 fprintf (f, " [%d]", i);
1532 fprintf (f, "):");
1533 print_object_live_ranges (f, ALLOCNO_OBJECT (a, i));
1537 /* Print live ranges of allocno A to stderr. */
1538 void
1539 ira_debug_allocno_live_ranges (ira_allocno_t a)
1541 print_allocno_live_ranges (stderr, a);
1544 /* Print live ranges of all allocnos to file F. */
1545 static void
1546 print_live_ranges (FILE *f)
1548 ira_allocno_t a;
1549 ira_allocno_iterator ai;
1551 FOR_EACH_ALLOCNO (a, ai)
1552 print_allocno_live_ranges (f, a);
1555 /* Print live ranges of all allocnos to stderr. */
1556 void
1557 ira_debug_live_ranges (void)
1559 print_live_ranges (stderr);
1562 /* The main entry function creates live ranges, set up
1563 CONFLICT_HARD_REGS and TOTAL_CONFLICT_HARD_REGS for objects, and
1564 calculate register pressure info. */
1565 void
1566 ira_create_allocno_live_ranges (void)
1568 objects_live = sparseset_alloc (ira_objects_num);
1569 allocnos_processed = sparseset_alloc (ira_allocnos_num);
1570 curr_point = 0;
1571 last_call_num = 0;
1572 allocno_saved_at_call
1573 = (int *) ira_allocate (ira_allocnos_num * sizeof (int));
1574 memset (allocno_saved_at_call, 0, ira_allocnos_num * sizeof (int));
1575 ira_traverse_loop_tree (true, ira_loop_tree_root, NULL,
1576 process_bb_node_lives);
1577 ira_max_point = curr_point;
1578 create_start_finish_chains ();
1579 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
1580 print_live_ranges (ira_dump_file);
1581 /* Clean up. */
1582 ira_free (allocno_saved_at_call);
1583 sparseset_free (objects_live);
1584 sparseset_free (allocnos_processed);
1587 /* Compress allocno live ranges. */
1588 void
1589 ira_compress_allocno_live_ranges (void)
1591 remove_some_program_points_and_update_live_ranges ();
1592 ira_rebuild_start_finish_chains ();
1593 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
1595 fprintf (ira_dump_file, "Ranges after the compression:\n");
1596 print_live_ranges (ira_dump_file);
1600 /* Free arrays IRA_START_POINT_RANGES and IRA_FINISH_POINT_RANGES. */
1601 void
1602 ira_finish_allocno_live_ranges (void)
1604 ira_free (ira_finish_point_ranges);
1605 ira_free (ira_start_point_ranges);