2007-10-09 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ira-call.c
blob145d00dd8fb5a17a34417423c5916c15c9d5e3e0
1 /* Splitting ranges around calls for IRA.
2 Copyright (C) 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "hard-reg-set.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "target.h"
31 #include "regs.h"
32 #include "flags.h"
33 #include "basic-block.h"
34 #include "toplev.h"
35 #include "expr.h"
36 #include "params.h"
37 #include "reload.h"
38 #include "df.h"
39 #include "output.h"
40 #include "ira-int.h"
42 /* The file is responsible for splitting the live range of
43 pseudo-registers living through calls which are assigned to
44 call-used hard-registers in two parts: one range (a new
45 pseudo-register is created for this) which lives through the calls
46 and another range (the original pseudo-register is used for the
47 range) lives between the calls. Memory is assigned to new
48 pseudo-registers. Move instructions connecting the two live ranges
49 (the original and new pseudo-registers) will be transformed into
50 load/store instructions in the reload pass.
52 This file also does global save/restore code redundancy
53 elimination. It calculates points to put save/restore instructions
54 according the following data flow equations:
56 SaveOut(b) = intersect (SaveIn(p) - SaveIgnore(pb))
57 for each p in pred(b)
59 | 0 if depth (b) <= depth (p)
60 SaveIgnore(pb) = |
61 | Ref(loop(b)) if depth (b) > depth (p)
63 SaveIn(b) = (SaveOut(b) - Kill(b)) U SaveGen(b)
65 RestoreIn(b) = intersect (RestoreOut(s) - RestoreIgnore(bs))
66 for each s in succ(b)
68 | 0 if depth (b) <= depth (s)
69 RestoreIgnore(bs) = |
70 | Ref(loop(b))if depth (b) > depth (s)
72 RestoreOut(b) = (RestoreIn(b) - Kill(b)) U RestoreGen(b)
74 Here, Kill(b) is the set of allocnos referenced in basic block b
75 and SaveGen(b) and RestoreGen(b) is the set of allocnos which
76 should be correspondingly saved and restored in basic block b and
77 which are not referenced correspondingly before the last and after
78 the first calls they live through in basic block b. SaveIn(b),
79 SaveOut(b), RestoreIn(b), RestoreOut(b) are allocnos
80 correspondingly to save and to restore at the start and the end of
81 basic block b. Save and restore code is not moved to more
82 frequently executed points (inside loops). The code can be moved
83 through a loop unless it is referenced in the loop (this set of
84 allocnos is denoted by Ref(loop)).
86 We should put code to save/restore an allocno on an edge (p,s) if
87 the allocno lives on the edge and the corresponding values of the
88 sets at end of p and at the start of s are different. In practice,
89 code unification is done: if the save/restore code should be on all
90 outgoing edges or all incoming edges, it is placed at the edge
91 source and destination correspondingly.
93 Putting live ranges living through calls into memory means that
94 some conflicting pseudo-registers (such pseudo-registers should not
95 live through calls) assigned to memory have a chance to be assigned
96 to the corresponding call-used hard-register. It is done by
97 ira-color.c:reassign_conflict_allocnos using simple priority-based
98 colouring for the conflicting pseudo-registers. The bigger the
99 live range of pseudo-register living through calls, the better such
100 a chance is. Therefore, we move spill/restore code as far as
101 possible inside basic blocks.
103 The implementation of save/restore code generation before the
104 reload pass has several advantages:
106 o simpler implementation of sharing stack slots used for spilled
107 pseudos and for saving pseudo values around calls. Actually,
108 the same code for sharing stack slots allocated for pseudos is
109 used in this case.
111 o simpler implementation of moving save/restore code to increase
112 the range of memory pseudo can be stored in.
114 o simpler implementation of improving allocation by assigning
115 hard-registers to spilled pseudos which conflict with new
116 pseudos living through calls.
118 The disadvantage of such an approach is mainly in the reload pass,
119 whose behavior is hard to predict. If the reload pass decides that
120 the original pseudos should be spilled, save/restore code will be
121 transformed into a memory-memory move. To remove such nasty moves,
122 IRA is trying to use the same stack slot for the two pseudos. It
123 is achieved using a standard preference technique to use the same
124 stack slot for pseudos involved in moves. A move between pseudos
125 assigned to the same memory could be removed by post-reload
126 optimizations, but it is implemented in the reload pass because, if
127 it is not done earlier, a hard-register would be required for this
128 and most probably a pseudo-register would be spilled by the reload
129 to free the hard-register.
133 /* ??? TODO: Abnormal edges */
134 /* The following structure contains basic block data flow information
135 used to calculate registers to save restore. */
136 struct bb_info
138 /* Local bb info: */
139 /* Registers mentioned in the BB. */
140 bitmap kill;
141 /* Registers needed to be saved and this save not killed (see above)
142 by an insn in the BB before that. */
143 bitmap saveloc;
144 /* Registers needed to be restored and this restore not killed by an
145 insn in the BB after that. */
146 bitmap restoreloc;
147 /* Global save and restore info. */
148 bitmap savein, saveout, restorein, restoreout;
151 /* Macros for accessing data flow information of basic blocks. */
152 #define BB_INFO(BB) ((struct bb_info *) (BB)->aux)
153 #define BB_INFO_BY_INDEX(N) BB_INFO (BASIC_BLOCK(N))
155 /* DF infrastructure data. */
156 static struct df_problem problem;
157 static struct dataflow dflow;
159 /* Basic blocks in postorder. */
160 static int *postorder;
161 /* The size of the previous array. */
162 static int n_blocks;
163 /* Bitmap of all basic blocks. */
164 static bitmap current_all_blocks;
166 static rtx *reg_map;
168 /* Numbers of currently live pseudo-registers. */
169 static bitmap regs_live;
171 /* Numbers of all registers which should be split around calls. */
172 static bitmap regs_to_save_restore;
174 /* Bitmap used to collect numbers of referenced regs inside a rtx. */
175 static bitmap referenced_regs;
177 /* Array of bitmaps for each loop node. The bitmap contains numbers
178 of registers mentioned int the corresponding loop (and all its
179 subloops). */
180 static bitmap *loop_referenced_regs_array;
181 /* The size of the previous array. */
182 static int loop_referenced_regs_array_len;
184 /* Bitmaps used for saving intermediate results. */
185 static bitmap temp_bitmap;
186 static bitmap temp_bitmap2;
188 /* Set of hard regs (except eliminable ones) currently live (during
189 scan of all insns). */
190 static HARD_REG_SET hard_regs_live;
192 /* Allocate and initialize data used for splitting allocnos around
193 calls. */
194 static void
195 init_ira_call_data (void)
197 int i;
199 postorder = ira_allocate (sizeof (int) * last_basic_block);
200 current_all_blocks = ira_allocate_bitmap ();
202 n_blocks = post_order_compute (postorder, true, false);
204 if (n_blocks != n_basic_blocks)
205 delete_unreachable_blocks ();
207 alloc_aux_for_blocks (sizeof (struct bb_info));
208 for (i = 0; i < n_blocks; i++)
210 struct bb_info *bb_info;
212 bitmap_set_bit (current_all_blocks, postorder [i]);
213 bb_info = BB_INFO_BY_INDEX (postorder [i]);
214 bb_info->kill = ira_allocate_bitmap ();
215 bb_info->saveloc = ira_allocate_bitmap ();
216 bb_info->restoreloc = ira_allocate_bitmap ();
217 bb_info->savein = ira_allocate_bitmap ();
218 bb_info->saveout = ira_allocate_bitmap ();
219 bb_info->restorein = ira_allocate_bitmap ();
220 bb_info->restoreout = ira_allocate_bitmap ();
223 loop_referenced_regs_array_len = VEC_length (loop_p, ira_loops.larray);
224 loop_referenced_regs_array
225 = ira_allocate (loop_referenced_regs_array_len * sizeof (bitmap));
226 for (i = 0; i < loop_referenced_regs_array_len; i++)
227 loop_referenced_regs_array [i] = ira_allocate_bitmap ();
229 memset (&problem, 0, sizeof (problem));
230 memset (&dflow, 0, sizeof (dflow));
231 dflow.problem= &problem;
233 reg_map = ira_allocate (sizeof (rtx) * max_reg_num ());
234 memset (reg_map, 0, sizeof (rtx) * max_reg_num ());
235 regs_live = ira_allocate_bitmap ();
236 referenced_regs = ira_allocate_bitmap ();
237 regs_to_save_restore = ira_allocate_bitmap ();
238 temp_bitmap = ira_allocate_bitmap ();
239 temp_bitmap2 = ira_allocate_bitmap ();
242 /* Print bitmap B with TITLE to file F. */
243 static void
244 print_bitmap (FILE *f, bitmap b, const char *title)
246 unsigned int j;
247 bitmap_iterator bi;
249 fprintf (f, "%s:", title);
250 EXECUTE_IF_SET_IN_BITMAP (b, FIRST_PSEUDO_REGISTER, j, bi)
251 fprintf (f, " %d", j);
252 fprintf (f, "\n");
255 /* Print data used for splitting allocnos around calls to file F. */
256 static void
257 print_ira_call_data (FILE *f)
259 int i;
260 basic_block bb;
262 print_bitmap (f, regs_to_save_restore, "to save/restore") ;
263 for (i = 0; i < loop_referenced_regs_array_len; i++)
265 fprintf (f, "Loop %d -- ", i);
266 print_bitmap (f, loop_referenced_regs_array [i], "referenced");
268 FOR_EACH_BB (bb)
270 struct bb_info *bb_info;
272 bb_info = BB_INFO (bb);
273 fprintf (f, "BB %d (loop %d)\n", bb->index, bb->loop_father->num);
274 print_bitmap (f, bb_info->kill, " kill") ;
275 print_bitmap (f, bb_info->saveloc, " saveloc") ;
276 print_bitmap (f, bb_info->restoreloc, " restoreloc") ;
277 print_bitmap (f, bb_info->savein, " savein") ;
278 print_bitmap (f, bb_info->saveout, " saveout") ;
279 print_bitmap (f, bb_info->restorein, " restorein") ;
280 print_bitmap (f, bb_info->restoreout, " restoreout") ;
284 /* Print data used for splitting allocnos around calls to STDERR. */
285 extern void
286 debug_ira_call_data (void)
288 print_ira_call_data (stderr);
291 /* Finish data used for splitting allocnos around calls. */
292 static void
293 finish_ira_call_data (void)
295 int i;
297 ira_free_bitmap (temp_bitmap2);
298 ira_free_bitmap (temp_bitmap);
299 ira_free_bitmap (regs_to_save_restore);
300 ira_free_bitmap (referenced_regs);
301 ira_free_bitmap (regs_live);
302 ira_free (reg_map);
303 for (i = 0; i < loop_referenced_regs_array_len; i++)
304 ira_free_bitmap (loop_referenced_regs_array [i]);
305 ira_free (loop_referenced_regs_array);
306 for (i = 0; i < n_blocks; i++)
308 struct bb_info *bb_info;
310 bb_info = BB_INFO_BY_INDEX (postorder [i]);
311 ira_free_bitmap (bb_info->restoreout);
312 ira_free_bitmap (bb_info->restorein);
313 ira_free_bitmap (bb_info->saveout);
314 ira_free_bitmap (bb_info->savein);
315 ira_free_bitmap (bb_info->restoreloc);
316 ira_free_bitmap (bb_info->saveloc);
317 ira_free_bitmap (bb_info->kill);
319 free_aux_for_blocks ();
320 ira_free_bitmap (current_all_blocks);
321 ira_free (postorder);
324 /* TRUE if insns and new registers are created. */
325 static int change_p;
327 /* Record all regs that are set in any one insn. Communication from
328 mark_reg_{store,clobber}. */
329 static VEC(rtx, heap) *regs_set;
331 /* Handle the case where REG is set by the insn being scanned. Store
332 a 1 in hard_regs_live or regs_live for this register.
334 REG might actually be something other than a register; if so, we do
335 nothing.
337 SETTER is 0 if this register was modified by an auto-increment
338 (i.e., a REG_INC note was found for it). */
339 static void
340 mark_reg_store (rtx reg, const_rtx setter ATTRIBUTE_UNUSED,
341 void *data ATTRIBUTE_UNUSED)
343 int regno;
345 if (GET_CODE (reg) == SUBREG)
346 reg = SUBREG_REG (reg);
348 if (! REG_P (reg))
349 return;
351 VEC_safe_push (rtx, heap, regs_set, reg);
353 regno = REGNO (reg);
355 if (regno >= FIRST_PSEUDO_REGISTER)
356 bitmap_set_bit (regs_live, regno);
357 else if (! TEST_HARD_REG_BIT (no_alloc_regs, regno))
359 int last = regno + hard_regno_nregs [regno] [GET_MODE (reg)];
361 while (regno < last)
363 if (! TEST_HARD_REG_BIT (eliminable_regset, regno))
364 SET_HARD_REG_BIT (hard_regs_live, regno);
365 regno++;
370 /* Like mark_reg_store except notice just CLOBBERs; ignore SETs. */
371 static void
372 mark_reg_clobber (rtx reg, const_rtx setter, void *data)
374 if (GET_CODE (setter) == CLOBBER)
375 mark_reg_store (reg, setter, data);
378 /* Mark REG as being dead (following the insn being scanned now).
379 Store a 0 in hard_regs_live or regs_live for this register. */
380 static void
381 mark_reg_death (rtx reg)
383 int regno = REGNO (reg);
385 if (regno >= FIRST_PSEUDO_REGISTER)
386 bitmap_clear_bit (regs_live, regno);
387 else if (! TEST_HARD_REG_BIT (no_alloc_regs, regno))
389 int last = regno + hard_regno_nregs [regno] [GET_MODE (reg)];
391 while (regno < last)
393 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
394 regno++;
399 /* The recursive function walks X and records all referenced registers
400 in REFERENCED_REGS. */
401 static void
402 mark_referenced_regs (rtx x)
404 enum rtx_code code;
405 const char *fmt;
406 int i, j;
408 if (x == NULL_RTX)
409 return;
410 code = GET_CODE (x);
411 if (code == SET)
412 mark_referenced_regs (SET_SRC (x));
413 if (code == SET || code == CLOBBER)
415 x = SET_DEST (x);
416 code = GET_CODE (x);
417 if ((code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
418 || code == PC || code == CC0)
419 return;
421 if (code == MEM || code == SUBREG)
423 x = XEXP (x, 0);
424 code = GET_CODE (x);
427 if (code == REG)
429 int regno = REGNO (x);
431 bitmap_set_bit (referenced_regs, regno);
432 return;
435 fmt = GET_RTX_FORMAT (code);
436 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
438 if (fmt[i] == 'e')
439 mark_referenced_regs (XEXP (x, i));
440 else if (fmt[i] == 'E')
441 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
442 mark_referenced_regs (XVECEXP (x, i, j));
446 /* The function sets up REFERENCED_REGS for rtx X and all their
447 equivalences. */
448 static void
449 mark_all_referenced_regs (rtx x)
451 rtx list, note;
452 unsigned int j;
453 bitmap_iterator bi;
455 mark_referenced_regs (x);
456 bitmap_copy (temp_bitmap, referenced_regs);
457 bitmap_copy (temp_bitmap2, referenced_regs);
458 for (;;)
460 bitmap_clear (referenced_regs);
461 EXECUTE_IF_SET_IN_BITMAP (temp_bitmap2, FIRST_PSEUDO_REGISTER, j, bi)
462 if (j < (unsigned) ira_max_regno_before)
463 for (list = reg_equiv_init [j];
464 list != NULL_RTX;
465 list = XEXP (list, 1))
467 note = find_reg_note (XEXP (list, 0), REG_EQUIV, NULL_RTX);
469 if (note == NULL_RTX)
470 continue;
472 mark_referenced_regs (XEXP (note, 0));
474 bitmap_and_compl (temp_bitmap2, temp_bitmap, referenced_regs);
475 if (! bitmap_ior_into (temp_bitmap, referenced_regs))
476 break;
478 bitmap_copy (referenced_regs, temp_bitmap);
481 /* The function emits a new save/restore insn with pattern PATH before
482 (if BEFORE_P) or after INSN. */
483 static void
484 insert_one_insn (rtx insn, int before_p, rtx pat)
486 rtx new_insn;
488 change_p = TRUE;
489 #ifdef HAVE_cc0
490 /* If INSN references CC0, put our insns in front of the insn that
491 sets CC0. This is always safe, since the only way we could be
492 passed an insn that references CC0 is for a restore, and doing a
493 restore earlier isn't a problem. We do, however, assume here
494 that CALL_INSNs don't reference CC0. Guard against non-INSN's
495 like CODE_LABEL. */
497 if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
498 && before_p && reg_referenced_p (cc0_rtx, PATTERN (insn)))
499 insn = PREV_INSN (insn);
500 #endif
502 if (before_p)
504 new_insn = emit_insn_before (pat, insn);
505 if (insn == BB_HEAD (BLOCK_FOR_INSN (insn)))
506 BB_HEAD (BLOCK_FOR_INSN (insn)) = new_insn;
508 else
510 if (GET_CODE (insn) != CODE_LABEL)
511 new_insn = emit_insn_after (pat, insn);
512 else
514 /* Put the insn after bb note in a empty basic block. */
515 gcc_assert (NEXT_INSN (insn) && NOTE_P (NEXT_INSN (insn)));
516 new_insn = emit_insn_after (pat, NEXT_INSN (insn));
518 if (insn == BB_END (BLOCK_FOR_INSN (insn)))
519 BB_END (BLOCK_FOR_INSN (insn)) = new_insn;
521 if (ira_dump_file != NULL)
522 fprintf (ira_dump_file,
523 "Generating save/restore insn %d:%d<-%d in bb %d\n",
524 INSN_UID (new_insn), REGNO (SET_DEST (pat)),
525 REGNO (SET_SRC (pat)), BLOCK_FOR_INSN (insn)->index);
528 /* The function creates a new register (if it is not created yet) and
529 returns it for allocno with REGNO. */
530 static rtx
531 get_new_reg (unsigned int regno)
533 rtx reg, newreg;
535 reg = regno_reg_rtx [regno];
536 if ((newreg = reg_map [regno]) == NULL_RTX)
538 newreg = gen_reg_rtx (GET_MODE (reg));
539 REG_USERVAR_P (newreg) = REG_USERVAR_P (reg);
540 REG_POINTER (newreg) = REG_POINTER (reg);
541 REG_ATTRS (newreg) = REG_ATTRS (reg);
542 reg_map [regno] = newreg;
544 return newreg;
547 /* Return move insn dest<-src. */
548 static rtx
549 get_move_insn (rtx src, rtx dest)
551 rtx result;
553 start_sequence ();
554 emit_move_insn (src, dest);
555 result = get_insns ();
556 end_sequence ();
557 return result;
560 /* The function inserts save/restore code which can be placed in any
561 case inside the BB and caclulate local bb info (kill, saveloc,
562 restoreloc). */
563 static void
564 put_save_restore_and_calculate_local_info (void)
566 int i;
567 unsigned int j;
568 basic_block bb;
569 rtx insn, reg, newreg, pat;
570 bitmap_iterator bi;
571 bitmap reg_live_in, reg_live_out, saveloc, restoreloc, kill;
573 /* Make a vector that mark_reg_{store,clobber} will store in. */
574 if (! regs_set)
575 regs_set = VEC_alloc (rtx, heap, 10);
576 FOR_EACH_BB (bb)
578 rtx first_insn, last_insn;
579 struct loop *loop;
580 struct bb_info *bb_info = BB_INFO (bb);
582 saveloc = bb_info->saveloc;
583 restoreloc = bb_info->restoreloc;
584 kill = bb_info->kill;
585 reg_live_in = DF_LR_IN (bb);
586 reg_live_out = DF_LR_OUT (bb);
587 REG_SET_TO_HARD_REG_SET (hard_regs_live, reg_live_in);
588 AND_COMPL_HARD_REG_SET (hard_regs_live, eliminable_regset);
589 bitmap_copy (regs_live, reg_live_in);
590 first_insn = last_insn = NULL_RTX;
591 /* Scan the code of this basic block, noting which regs and hard
592 regs are born or die. */
593 FOR_BB_INSNS (bb, insn)
595 rtx link;
597 if (! INSN_P (insn))
598 continue;
600 if (first_insn == NULL_RTX)
601 first_insn = insn;
602 last_insn = insn;
604 bitmap_clear (referenced_regs);
605 mark_all_referenced_regs (insn);
607 EXECUTE_IF_SET_IN_BITMAP (restoreloc, FIRST_PSEUDO_REGISTER, j, bi)
608 if (bitmap_bit_p (referenced_regs, j))
609 insert_one_insn (insn, TRUE,
610 get_move_insn (regno_reg_rtx [j],
611 get_new_reg (j)));
613 bitmap_ior_into (kill, referenced_regs);
614 bitmap_and_compl_into (restoreloc, referenced_regs);
616 /* Check that regs_set is an empty set. */
617 gcc_assert (VEC_empty (rtx, regs_set));
619 /* Mark any regs clobbered by INSN as live, so they
620 conflict with the inputs. */
621 note_stores (PATTERN (insn), mark_reg_clobber, NULL);
623 /* Mark any regs dead after INSN as dead now. */
624 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
625 if (REG_NOTE_KIND (link) == REG_DEAD)
626 mark_reg_death (XEXP (link, 0));
628 if (CALL_P (insn) && ! find_reg_note (insn, REG_NORETURN, NULL))
630 EXECUTE_IF_SET_IN_BITMAP (regs_live, FIRST_PSEUDO_REGISTER,
631 j, bi)
632 if ((j >= (unsigned) ira_max_regno_before
633 || (reg_equiv_const [j] == NULL_RTX
634 && ! reg_equiv_invariant_p [j]))
635 && reg_renumber [j] >= 0
636 && ! hard_reg_not_in_set_p (reg_renumber [j],
637 GET_MODE (regno_reg_rtx [j]),
638 call_used_reg_set))
640 bitmap_set_bit (regs_to_save_restore, j);
641 if (! bitmap_bit_p (restoreloc, j)
642 && bitmap_bit_p (kill, j))
644 for (i = 0; i < (int) VEC_length (rtx, regs_set); i++)
645 if (REGNO (VEC_index (rtx, regs_set, i)) == j)
646 break;
647 if (i < (int) VEC_length (rtx, regs_set))
648 continue;
649 /* Insert save insn */
650 reg = regno_reg_rtx [j];
651 newreg = get_new_reg (j);
652 pat = get_move_insn (newreg, reg);
653 insert_one_insn (insn, TRUE, pat);
655 if (! bitmap_bit_p (kill, j))
656 bitmap_set_bit (saveloc, j);
657 bitmap_set_bit (restoreloc, j);
661 /* Mark any regs set in INSN as live. */
662 note_stores (PATTERN (insn), mark_reg_store, NULL);
664 #ifdef AUTO_INC_DEC
665 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
666 if (REG_NOTE_KIND (link) == REG_INC)
667 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
668 #endif
670 /* Mark any regs set in INSN and then never used. */
671 while (! VEC_empty (rtx, regs_set))
673 rtx reg = VEC_pop (rtx, regs_set);
674 rtx note = find_regno_note (insn, REG_UNUSED, REGNO (reg));
676 if (note)
677 mark_reg_death (XEXP (note, 0));
680 if (! flag_ira_move_spills)
682 EXECUTE_IF_SET_IN_BITMAP (saveloc, FIRST_PSEUDO_REGISTER, j, bi)
683 insert_one_insn (first_insn, TRUE,
684 get_move_insn (get_new_reg (j),
685 regno_reg_rtx [j]));
686 EXECUTE_IF_SET_IN_BITMAP (restoreloc, FIRST_PSEUDO_REGISTER, j, bi)
687 insert_one_insn (last_insn, JUMP_P (last_insn),
688 get_move_insn (regno_reg_rtx [j],
689 get_new_reg (j)));
691 for (loop = bb->loop_father; loop != NULL; loop = loop_outer (loop))
692 bitmap_ior_into (loop_referenced_regs_array [loop->num], kill);
698 /* The function used by the DF equation solver to propagate restore
699 info through block with BB_INDEX. */
700 static bool
701 save_trans_fun (int bb_index)
703 struct bb_info *bb_info = BB_INFO_BY_INDEX (bb_index);
704 bitmap in = bb_info->savein;
705 bitmap out = bb_info->saveout;
706 bitmap loc = bb_info->saveloc;
707 bitmap kill = bb_info->kill;
709 return bitmap_ior_and_compl (in, loc, out, kill);
712 /* The function used by the DF equation solver to set up save info
713 for a block BB without successors. */
714 static void
715 save_con_fun_0 (basic_block bb)
717 bitmap_clear (BB_INFO (bb)->saveout);
720 /* The function used by the DF equation solver to propagate save info
721 from successor to predecessor on edge E. */
722 static void
723 save_con_fun_n (edge e)
725 bitmap op1 = BB_INFO (e->src)->saveout;
726 bitmap op2 = BB_INFO (e->dest)->savein;
728 if (e->src->loop_depth > e->dest->loop_depth)
730 bitmap_and_into (op1, op2);
731 bitmap_and_compl_into
732 (op1, loop_referenced_regs_array [e->src->loop_father->num]);
734 else
735 bitmap_and_into (op1, op2);
738 /* The function calculates savein/saveout sets. */
739 static void
740 calculate_save (void)
742 basic_block bb;
744 /* Initialize relations to find maximal solution. */
745 FOR_ALL_BB (bb)
747 bitmap_copy (BB_INFO (bb)->savein, regs_to_save_restore);
748 bitmap_copy (BB_INFO (bb)->saveout, regs_to_save_restore);
750 df_simple_dataflow (DF_BACKWARD, NULL, save_con_fun_0, save_con_fun_n,
751 save_trans_fun, current_all_blocks,
752 df_get_postorder (DF_BACKWARD),
753 df_get_n_blocks (DF_BACKWARD));
758 /* The function used by the DF equation solver to propagate restore
759 info through block with BB_INDEX. */
760 static bool
761 restore_trans_fun (int bb_index)
763 struct bb_info *bb_info = BB_INFO_BY_INDEX (bb_index);
764 bitmap in = bb_info->restorein;
765 bitmap out = bb_info->restoreout;
766 bitmap loc = bb_info->restoreloc;
767 bitmap kill = bb_info->kill;
769 return bitmap_ior_and_compl (out, loc, in, kill);
772 /* The function used by the DF equation solver to set up restore info
773 for a block BB without predecessors. */
774 static void
775 restore_con_fun_0 (basic_block bb)
777 bitmap_clear (BB_INFO (bb)->restorein);
780 /* The function used by the DF equation solver to propagate restore
781 info from predecessor to successor on edge E. */
782 static void
783 restore_con_fun_n (edge e)
785 bitmap op1 = BB_INFO (e->dest)->restorein;
786 bitmap op2 = BB_INFO (e->src)->restoreout;
788 if (e->dest->loop_depth > e->src->loop_depth)
790 bitmap_and_into (op1, op2);
791 bitmap_and_compl_into
792 (op1, loop_referenced_regs_array [e->dest->loop_father->num]);
794 else
795 bitmap_and_into (op1, op2);
798 /* The function calculates restorein/restoreout sets. */
799 static void
800 calculate_restore (void)
802 basic_block bb;
804 /* Initialize relations to find maximal solution. */
805 FOR_ALL_BB (bb)
807 bitmap_copy (BB_INFO (bb)->restoreout, regs_to_save_restore);
808 bitmap_copy (BB_INFO (bb)->restorein, regs_to_save_restore);
810 df_simple_dataflow (DF_FORWARD, NULL, restore_con_fun_0, restore_con_fun_n,
811 restore_trans_fun, current_all_blocks,
812 df_get_postorder (DF_FORWARD),
813 df_get_n_blocks (DF_FORWARD));
818 /* The function put save/restores insn according to the calculated
819 equation. The function even puts the insns inside blocks to
820 increase live range of allocnos which will be in memory. */
821 static void
822 put_save_restore (void)
824 basic_block bb;
825 edge e;
826 edge_iterator ei;
827 unsigned int j;
828 bitmap_iterator bi;
829 rtx pat;
830 int first_p;
831 bitmap save_at_end, restore_at_start, progress;
833 save_at_end = ira_allocate_bitmap ();
834 restore_at_start = ira_allocate_bitmap ();
835 progress = ira_allocate_bitmap ();
836 FOR_EACH_BB (bb)
838 struct bb_info *bb_info = BB_INFO (bb);
839 bitmap kill = bb_info->kill;
840 bitmap restoreout = bb_info->restoreout;
841 bitmap saveout = bb_info->saveout;
842 bitmap savein = bb_info->savein;
843 bitmap restorein = bb_info->restorein;
844 bitmap live_at_start;
845 rtx bb_head, bb_end;
846 rtx insn, next_insn;
848 for (bb_head = BB_HEAD (bb);
849 bb_head != BB_END (bb) && ! INSN_P (bb_head);
850 bb_head = NEXT_INSN (bb_head))
852 for (bb_end = BB_END (bb);
853 bb_end != BB_HEAD (bb) && ! INSN_P (bb_end);
854 bb_end = PREV_INSN (bb_end))
857 bitmap_clear (save_at_end);
858 first_p = TRUE;
859 FOR_EACH_EDGE (e, ei, bb->succs)
861 bitmap savein = BB_INFO (e->dest)->savein;
863 live_at_start = DF_LR_IN (e->dest);
864 /* (savein - restoreout) ^ (kill U ! saveout) ==
865 ^ live_at_start ==
866 (savein - restoreout) ^ live_at_start ^ kill
867 U (savein - restoreout) ^ live_at_start - saveout
869 bitmap_and_compl (temp_bitmap2, savein, restoreout);
870 bitmap_and_into (temp_bitmap2, live_at_start);
871 bitmap_and (temp_bitmap, temp_bitmap2, kill);
872 bitmap_ior_and_compl_into (temp_bitmap, temp_bitmap2, saveout);
873 if (first_p)
875 bitmap_copy (save_at_end, temp_bitmap);
876 first_p = FALSE;
878 else
879 bitmap_and_into (save_at_end, temp_bitmap);
882 bitmap_copy (progress, save_at_end);
883 for (insn = BB_END (bb);
884 insn != PREV_INSN (BB_HEAD (bb));
885 insn = next_insn)
887 next_insn = PREV_INSN (insn);
888 if (! INSN_P (insn))
889 continue;
890 bitmap_clear (referenced_regs);
891 mark_all_referenced_regs (insn);
892 EXECUTE_IF_SET_IN_BITMAP (referenced_regs, FIRST_PSEUDO_REGISTER,
893 j, bi)
894 if (bitmap_bit_p (progress, j))
896 pat = get_move_insn (get_new_reg (j), regno_reg_rtx [j]);
897 insert_one_insn (insn, JUMP_P (insn), pat);
898 bitmap_clear_bit (progress, j);
901 /* When we don't move info inside the loop, progress can
902 be not empty here. */
903 EXECUTE_IF_SET_IN_BITMAP (progress, FIRST_PSEUDO_REGISTER, j, bi)
905 pat = get_move_insn (get_new_reg (j), regno_reg_rtx [j]);
906 insert_one_insn (bb_head, TRUE, pat);
909 bitmap_clear (restore_at_start);
910 first_p = TRUE;
911 live_at_start = DF_LR_IN (bb);
912 FOR_EACH_EDGE (e, ei, bb->preds)
914 bitmap restoreout = BB_INFO (e->src)->restoreout;
916 /* (restoreout - savein) ^ (kill U ! restorein)
917 ^ live_at_start ==
918 (((restoreout - savein) ^ live_at_start) ^ kill
919 U ((restoreout - savein) ^ live_at_start) - restorein)
921 bitmap_and_compl (temp_bitmap2, restoreout, savein);
922 bitmap_and_into (temp_bitmap2, live_at_start);
923 bitmap_and (temp_bitmap, temp_bitmap2, kill);
924 bitmap_ior_and_compl_into (temp_bitmap, temp_bitmap2, restorein);
925 if (first_p)
927 bitmap_copy (restore_at_start, temp_bitmap);
928 first_p = FALSE;
930 else
931 bitmap_and_into (restore_at_start, temp_bitmap);
934 bitmap_copy (progress, restore_at_start);
935 for (insn = BB_HEAD (bb);
936 insn != NEXT_INSN (BB_END (bb));
937 insn = next_insn)
939 next_insn = NEXT_INSN (insn);
940 if (! INSN_P (insn))
941 continue;
942 bitmap_clear (referenced_regs);
943 mark_all_referenced_regs (insn);
944 EXECUTE_IF_SET_IN_BITMAP (referenced_regs, FIRST_PSEUDO_REGISTER,
945 j, bi)
946 if (bitmap_bit_p (progress, j))
948 pat = get_move_insn (regno_reg_rtx [j], get_new_reg (j));
949 insert_one_insn (insn, TRUE, pat);
950 bitmap_clear_bit (progress, j);
953 /* When we don't move info inside the loop, progress can
954 be not empty here. */
955 EXECUTE_IF_SET_IN_BITMAP (progress, FIRST_PSEUDO_REGISTER, j, bi)
957 pat = get_move_insn (regno_reg_rtx [j], get_new_reg (j));
958 insert_one_insn (bb_end, JUMP_P (bb_end), pat);
961 FOR_EACH_EDGE (e, ei, bb->succs)
963 bitmap savein = BB_INFO (e->dest)->savein;
965 live_at_start = DF_LR_IN (e->dest);
966 EXECUTE_IF_SET_IN_BITMAP (savein, FIRST_PSEUDO_REGISTER, j, bi)
967 if (! bitmap_bit_p (restoreout, j)
968 && (bitmap_bit_p (kill, j)
969 || ! bitmap_bit_p (saveout, j))
970 && ! bitmap_bit_p (save_at_end, j)
971 && bitmap_bit_p (live_at_start, j))
973 pat = get_move_insn (get_new_reg (j),
974 regno_reg_rtx [j]);
975 insert_insn_on_edge (pat, e);
976 change_p = TRUE;
977 if (ira_dump_file != NULL)
978 fprintf
979 (ira_dump_file,
980 "Generating save/restore insn %d<-%d on edge %d->%d\n",
981 REGNO (SET_DEST (pat)), REGNO (SET_SRC (pat)),
982 e->src->index, e->dest->index);
986 live_at_start = DF_LR_IN (bb);
987 FOR_EACH_EDGE (e, ei, bb->preds)
989 bitmap restoreout = BB_INFO (e->src)->restoreout;
991 EXECUTE_IF_SET_IN_BITMAP (restoreout, FIRST_PSEUDO_REGISTER, j, bi)
992 if (! bitmap_bit_p (savein, j)
993 && (bitmap_bit_p (kill, j)
994 || ! bitmap_bit_p (restorein, j))
995 && ! bitmap_bit_p (restore_at_start, j)
996 && bitmap_bit_p (live_at_start, j))
998 pat = get_move_insn (regno_reg_rtx [j],
999 get_new_reg (j));
1000 insert_insn_on_edge (pat, e);
1001 change_p = TRUE;
1002 if (ira_dump_file != NULL)
1003 fprintf
1004 (ira_dump_file,
1005 "Generating save/restore insn %d<-%d on edge %d->%d\n",
1006 REGNO (SET_DEST (pat)), REGNO (SET_SRC (pat)),
1007 e->src->index, e->dest->index);
1011 ira_free_bitmap (progress);
1012 ira_free_bitmap (save_at_end);
1013 ira_free_bitmap (restore_at_start);
1016 /* The function splits allocnos living through calls and assigned to a
1017 call used register. If FLAG_IRA_MOVE_SPILLS, the function moves
1018 save/restore insns to correspondingly to the top and bottom of the
1019 CFG but not moving them to more frequently executed places. */
1021 split_around_calls (void)
1023 change_p = FALSE;
1024 init_ira_call_data ();
1025 put_save_restore_and_calculate_local_info ();
1026 if (flag_ira_move_spills)
1028 calculate_save ();
1029 calculate_restore ();
1030 put_save_restore ();
1032 finish_ira_call_data ();
1033 fixup_abnormal_edges ();
1034 commit_edge_insertions ();
1035 return change_p;
1040 /* The function returns regno of living through call allocno which is
1041 result of splitting allocno with ORIGINAL_REGNO. If there is no
1042 such regno, the function returns -1. */
1044 get_around_calls_regno (int original_regno)
1046 allocno_t a, another_a;
1047 copy_t cp, next_cp;
1049 if (original_regno >= ira_max_regno_call_before)
1050 return -1;
1051 a = regno_allocno_map [original_regno];
1052 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1054 if (cp->first == a)
1056 another_a = cp->second;
1057 next_cp = cp->next_first_allocno_copy;
1059 else
1061 another_a = cp->first;
1062 next_cp = cp->next_second_allocno_copy;
1064 if (cp->move_insn == NULL_RTX)
1065 continue;
1066 if (ALLOCNO_REGNO (another_a) >= ira_max_regno_call_before)
1067 return ALLOCNO_REGNO (another_a);
1069 return -1;