2007-03-16 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ira-call.c
blob9f9f975d919de9ae4fa8a516b8dd1080d29e7e18
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 /* ??? TODO: Abnormal edges */
43 /* The following structure contains basic block data flow information
44 used to calculate registers to save restore. */
45 struct bb_info
47 /* Local bb info: */
48 /* Registers mentioned in the BB. */
49 bitmap kill;
50 /* Registers needed to be saved and this save not killed (see above)
51 by an insn in the BB before that. */
52 bitmap saveloc;
53 /* Registers needed to be restored and this restore not killed by an
54 insn in the BB after that. */
55 bitmap restoreloc;
56 /* Global save and restore info. */
57 bitmap savein, saveout, restorein, restoreout;
60 /* Macros for accessing data flow information of basic blocks. */
61 #define BB_INFO(BB) ((struct bb_info *) (BB)->aux)
62 #define BB_INFO_BY_INDEX(N) BB_INFO (BASIC_BLOCK(N))
64 /* DF infrastructure data. */
65 static struct df_problem problem;
66 static struct dataflow dflow;
68 /* Basic blocks in postorder. */
69 static int *postorder;
70 /* The size of the previous array. */
71 static int n_blocks;
72 /* Bitmap of all basic blocks. */
73 static bitmap current_all_blocks;
75 static rtx *reg_map;
77 /* Numbers of currently live pseudos. */
78 static bitmap regs_live;
80 /* Numbers of all registers which should be split around calls. */
81 static bitmap regs_to_save_restore;
83 /* Bitmap used to collect numbers of referenced regs inside a rtx. */
84 static bitmap referenced_regs;
86 /* Array of bitmaps for each loop node. The bitmap contains numbers
87 of registers mentioned int the corresponding loop (and all its
88 subloops). */
89 static bitmap *loop_referenced_regs_array;
90 /* The size of the previous array. */
91 static int loop_referenced_regs_array_len;
93 /* Bitmaps used for saving intermediate results. */
94 static bitmap temp_bitmap;
95 static bitmap temp_bitmap2;
97 /* Set of hard regs (except eliminable ones) currently live (during
98 scan of all insns). */
99 static HARD_REG_SET hard_regs_live;
101 /* Allocate and initialize data used for splitting pseudos around
102 calls. */
103 static void
104 init_ira_call_data (void)
106 int i;
108 postorder = ira_allocate (sizeof (int) * last_basic_block);
109 current_all_blocks = ira_allocate_bitmap ();
111 n_blocks = post_order_compute (postorder, true);
113 if (n_blocks != n_basic_blocks)
114 delete_unreachable_blocks ();
116 alloc_aux_for_blocks (sizeof (struct bb_info));
117 for (i = 0; i < n_blocks; i++)
119 struct bb_info *bb_info;
121 bitmap_set_bit (current_all_blocks, postorder [i]);
122 bb_info = BB_INFO_BY_INDEX (postorder [i]);
123 bb_info->kill = ira_allocate_bitmap ();
124 bb_info->saveloc = ira_allocate_bitmap ();
125 bb_info->restoreloc = ira_allocate_bitmap ();
126 bb_info->savein = ira_allocate_bitmap ();
127 bb_info->saveout = ira_allocate_bitmap ();
128 bb_info->restorein = ira_allocate_bitmap ();
129 bb_info->restoreout = ira_allocate_bitmap ();
132 loop_referenced_regs_array_len = VEC_length (loop_p, ira_loops.larray);
133 loop_referenced_regs_array
134 = ira_allocate (loop_referenced_regs_array_len * sizeof (bitmap));
135 for (i = 0; i < loop_referenced_regs_array_len; i++)
136 loop_referenced_regs_array [i] = ira_allocate_bitmap ();
138 memset (&problem, 0, sizeof (problem));
139 memset (&dflow, 0, sizeof (dflow));
140 dflow.problem= &problem;
142 reg_map = ira_allocate (sizeof (rtx) * max_reg_num ());
143 memset (reg_map, 0, sizeof (rtx) * max_reg_num ());
144 regs_live = ira_allocate_bitmap ();
145 referenced_regs = ira_allocate_bitmap ();
146 regs_to_save_restore = ira_allocate_bitmap ();
147 temp_bitmap = ira_allocate_bitmap ();
148 temp_bitmap2 = ira_allocate_bitmap ();
151 /* Print bitmap B with TITLE to file F. */
152 static void
153 print_bitmap (FILE *f, bitmap b, const char *title)
155 unsigned int j;
156 bitmap_iterator bi;
158 fprintf (f, "%s:", title);
159 EXECUTE_IF_SET_IN_BITMAP (b, FIRST_PSEUDO_REGISTER, j, bi)
160 fprintf (f, " %d", j);
161 fprintf (f, "\n");
164 /* Print data used for splitting pseudos around calls to file F. */
165 static void
166 print_ira_call_data (FILE *f)
168 int i;
169 basic_block bb;
171 print_bitmap (f, regs_to_save_restore, "to save/restore") ;
172 for (i = 0; i < loop_referenced_regs_array_len; i++)
174 fprintf (f, "Loop %d -- ", i);
175 print_bitmap (f, loop_referenced_regs_array [i], "referenced");
177 FOR_EACH_BB (bb)
179 struct bb_info *bb_info;
181 bb_info = BB_INFO (bb);
182 fprintf (f, "BB %d (loop %d)\n", bb->index, bb->loop_father->num);
183 print_bitmap (f, bb_info->kill, " kill") ;
184 print_bitmap (f, bb_info->saveloc, " saveloc") ;
185 print_bitmap (f, bb_info->restoreloc, " restoreloc") ;
186 print_bitmap (f, bb_info->savein, " savein") ;
187 print_bitmap (f, bb_info->saveout, " saveout") ;
188 print_bitmap (f, bb_info->restorein, " restorein") ;
189 print_bitmap (f, bb_info->restoreout, " restoreout") ;
193 /* Print data used for splitting pseudos around calls to STDERR. */
194 extern void
195 debug_ira_call_data (void)
197 print_ira_call_data (stderr);
200 /* Finish data used for splitting pseudos around calls. */
201 static void
202 finish_ira_call_data (void)
204 int i;
206 ira_free_bitmap (temp_bitmap2);
207 ira_free_bitmap (temp_bitmap);
208 ira_free_bitmap (regs_to_save_restore);
209 ira_free_bitmap (referenced_regs);
210 ira_free_bitmap (regs_live);
211 ira_free (reg_map);
212 for (i = 0; i < loop_referenced_regs_array_len; i++)
213 ira_free_bitmap (loop_referenced_regs_array [i]);
214 ira_free (loop_referenced_regs_array);
215 for (i = 0; i < n_blocks; i++)
217 struct bb_info *bb_info;
219 bb_info = BB_INFO_BY_INDEX (postorder [i]);
220 ira_free_bitmap (bb_info->restoreout);
221 ira_free_bitmap (bb_info->restorein);
222 ira_free_bitmap (bb_info->saveout);
223 ira_free_bitmap (bb_info->savein);
224 ira_free_bitmap (bb_info->restoreloc);
225 ira_free_bitmap (bb_info->saveloc);
226 ira_free_bitmap (bb_info->kill);
228 free_aux_for_blocks ();
229 ira_free_bitmap (current_all_blocks);
230 ira_free (postorder);
233 /* TRUE if insns and new registers are created. */
234 static int change_p;
236 /* Record all regs that are set in any one insn. Communication from
237 mark_reg_{store,clobber}. */
238 static rtx *regs_set;
240 /* Number elelments in the previous array. */
241 static int n_regs_set;
243 /* Handle the case where REG is set by the insn being scanned. Store
244 a 1 in hard_regs_live or regs_live for this register.
246 REG might actually be something other than a register; if so, we do
247 nothing.
249 SETTER is 0 if this register was modified by an auto-increment
250 (i.e., a REG_INC note was found for it). */
251 static void
252 mark_reg_store (rtx reg, rtx setter ATTRIBUTE_UNUSED,
253 void *data ATTRIBUTE_UNUSED)
255 int regno;
257 if (GET_CODE (reg) == SUBREG)
258 reg = SUBREG_REG (reg);
260 if (! REG_P (reg))
261 return;
263 regs_set [n_regs_set++] = reg;
265 regno = REGNO (reg);
267 if (regno >= FIRST_PSEUDO_REGISTER)
268 bitmap_set_bit (regs_live, regno);
269 else if (! TEST_HARD_REG_BIT (no_alloc_regs, regno))
271 int last = regno + hard_regno_nregs [regno] [GET_MODE (reg)];
273 while (regno < last)
275 if (! TEST_HARD_REG_BIT (eliminable_regset, regno))
276 SET_HARD_REG_BIT (hard_regs_live, regno);
277 regno++;
282 /* Like mark_reg_store except notice just CLOBBERs; ignore SETs. */
283 static void
284 mark_reg_clobber (rtx reg, rtx setter, void *data)
286 if (GET_CODE (setter) == CLOBBER)
287 mark_reg_store (reg, setter, data);
290 /* Mark REG as being dead (following the insn being scanned now).
291 Store a 0 in hard_regs_live or regs_live for this register. */
292 static void
293 mark_reg_death (rtx reg)
295 int regno = REGNO (reg);
297 if (regno >= FIRST_PSEUDO_REGISTER)
298 bitmap_clear_bit (regs_live, regno);
299 else if (! TEST_HARD_REG_BIT (no_alloc_regs, regno))
301 int last = regno + hard_regno_nregs [regno] [GET_MODE (reg)];
303 while (regno < last)
305 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
306 regno++;
311 /* The recursive function walks X and records all referenced registers
312 in REFERENCED_REGS. */
313 static void
314 mark_referenced_regs (rtx x)
316 enum rtx_code code = GET_CODE (x);
317 const char *fmt;
318 int i, j;
320 if (code == SET)
321 mark_referenced_regs (SET_SRC (x));
322 if (code == SET || code == CLOBBER)
324 x = SET_DEST (x);
325 code = GET_CODE (x);
326 if ((code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
327 || code == PC || code == CC0)
328 return;
330 if (code == MEM || code == SUBREG)
332 x = XEXP (x, 0);
333 code = GET_CODE (x);
336 if (code == REG)
338 int regno = REGNO (x);
340 bitmap_set_bit (referenced_regs, regno);
341 return;
344 fmt = GET_RTX_FORMAT (code);
345 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
347 if (fmt[i] == 'e')
348 mark_referenced_regs (XEXP (x, i));
349 else if (fmt[i] == 'E')
350 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
351 mark_referenced_regs (XVECEXP (x, i, j));
355 /* The function sets up REFERENCED_REGS for rtx X and all their
356 equivalences. */
357 static void
358 mark_all_referenced_regs (rtx x)
360 rtx list, note;
361 unsigned int j;
362 bitmap_iterator bi;
364 mark_referenced_regs (x);
365 bitmap_copy (temp_bitmap, referenced_regs);
366 bitmap_copy (temp_bitmap2, referenced_regs);
367 for (;;)
369 bitmap_clear (referenced_regs);
370 EXECUTE_IF_SET_IN_BITMAP (temp_bitmap2, FIRST_PSEUDO_REGISTER, j, bi)
371 if (j < (unsigned) ira_max_regno_before)
372 for (list = reg_equiv_init [j];
373 list != NULL_RTX;
374 list = XEXP (list, 1))
376 note = find_reg_note (XEXP (list, 0), REG_EQUIV, NULL_RTX);
378 if (note == NULL_RTX)
379 continue;
381 mark_referenced_regs (XEXP (note, 0));
383 bitmap_and_compl (temp_bitmap2, temp_bitmap, referenced_regs);
384 if (! bitmap_ior_into (temp_bitmap, referenced_regs))
385 break;
387 bitmap_copy (referenced_regs, temp_bitmap);
390 /* The function emits a new save/restore insn with pattern PATH before
391 (if BEFORE_P) or after INSN. */
392 static void
393 insert_one_insn (rtx insn, int before_p, rtx pat)
395 rtx new_insn;
397 change_p = TRUE;
398 #ifdef HAVE_cc0
399 /* If INSN references CC0, put our insns in front of the insn that
400 sets CC0. This is always safe, since the only way we could be
401 passed an insn that references CC0 is for a restore, and doing a
402 restore earlier isn't a problem. We do, however, assume here
403 that CALL_INSNs don't reference CC0. Guard against non-INSN's
404 like CODE_LABEL. */
406 if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
407 && before_p && reg_referenced_p (cc0_rtx, PATTERN (insn)))
408 insn = PREV_INSN (insn);
409 #endif
411 if (before_p)
413 new_insn = emit_insn_before (pat, insn);
414 if (insn == BB_HEAD (BLOCK_FOR_INSN (insn)))
415 BB_HEAD (BLOCK_FOR_INSN (insn)) = new_insn;
417 else
419 if (GET_CODE (insn) != CODE_LABEL)
420 new_insn = emit_insn_after (pat, insn);
421 else
423 /* Put the insn after bb note in a empty basic block. */
424 gcc_assert (NEXT_INSN (insn) && NOTE_P (NEXT_INSN (insn)));
425 new_insn = emit_insn_after (pat, NEXT_INSN (insn));
427 if (insn == BB_END (BLOCK_FOR_INSN (insn)))
428 BB_END (BLOCK_FOR_INSN (insn)) = new_insn;
430 if (ira_dump_file != NULL)
431 fprintf (ira_dump_file,
432 "Generating save/restore insn %d:%d<-%d in bb %d\n",
433 INSN_UID (new_insn), REGNO (SET_DEST (pat)),
434 REGNO (SET_SRC (pat)), BLOCK_FOR_INSN (insn)->index);
437 /* The function creates a new register (if it is not created yet) and
438 returns it for pseudo with REGNO. */
439 static rtx
440 get_new_reg (unsigned int regno)
442 rtx reg, newreg;
444 reg = regno_reg_rtx [regno];
445 if ((newreg = reg_map [regno]) == NULL_RTX)
447 newreg = gen_reg_rtx (GET_MODE (reg));
448 REG_USERVAR_P (newreg) = REG_USERVAR_P (reg);
449 REG_POINTER (newreg) = REG_POINTER (reg);
450 REG_ATTRS (newreg) = REG_ATTRS (reg);
451 reg_map [regno] = newreg;
453 return newreg;
456 /* The function inserts save/restore code which can be placed in any
457 case inside the BB and caclulate local bb info (kill, saveloc,
458 restoreloc). */
459 static void
460 put_save_restore_and_calculate_local_info (void)
462 int i;
463 unsigned int j;
464 basic_block bb;
465 rtx insn, reg, newreg, pat;
466 bitmap_iterator bi;
467 bitmap reg_live_in, reg_live_out, saveloc, restoreloc, kill;
469 /* Make a vector that mark_reg_{store,clobber} will store in. */
470 regs_set = ira_allocate (sizeof (rtx) * max_parallel * 2);
471 FOR_EACH_BB (bb)
473 rtx first_insn, last_insn;
474 struct loop *loop;
475 struct bb_info *bb_info = BB_INFO (bb);
477 saveloc = bb_info->saveloc;
478 restoreloc = bb_info->restoreloc;
479 kill = bb_info->kill;
480 reg_live_in = DF_UPWARD_LIVE_IN (build_df, bb);
481 reg_live_out = DF_UPWARD_LIVE_OUT (build_df, bb);
482 REG_SET_TO_HARD_REG_SET (hard_regs_live, reg_live_in);
483 AND_COMPL_HARD_REG_SET (hard_regs_live, eliminable_regset);
484 bitmap_copy (regs_live, reg_live_in);
485 first_insn = last_insn = NULL_RTX;
486 /* Scan the code of this basic block, noting which regs and hard
487 regs are born or die. */
488 FOR_BB_INSNS (bb, insn)
490 rtx link;
492 if (! INSN_P (insn))
493 continue;
495 if (first_insn == NULL_RTX)
496 first_insn = insn;
497 last_insn = insn;
499 bitmap_clear (referenced_regs);
500 mark_all_referenced_regs (PATTERN (insn));
502 EXECUTE_IF_SET_IN_BITMAP (restoreloc, FIRST_PSEUDO_REGISTER, j, bi)
503 if (bitmap_bit_p (referenced_regs, j))
504 insert_one_insn (insn, TRUE,
505 gen_rtx_SET (VOIDmode, regno_reg_rtx [j],
506 get_new_reg (j)));
508 bitmap_ior_into (kill, referenced_regs);
509 bitmap_and_compl_into (restoreloc, referenced_regs);
511 /* Make regs_set an empty set. */
512 n_regs_set = 0;
514 /* Mark any regs clobbered by INSN as live, so they
515 conflict with the inputs. */
516 note_stores (PATTERN (insn), mark_reg_clobber, NULL);
518 /* Mark any regs dead after INSN as dead now. */
519 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
520 if (REG_NOTE_KIND (link) == REG_DEAD)
521 mark_reg_death (XEXP (link, 0));
523 if (CALL_P (insn) && ! find_reg_note (insn, REG_NORETURN, NULL))
525 EXECUTE_IF_SET_IN_BITMAP (regs_live, FIRST_PSEUDO_REGISTER,
526 j, bi)
527 if ((j >= (unsigned) ira_max_regno_before
528 || (reg_equiv_const [j] == NULL_RTX
529 && ! reg_equiv_invariant_p [j]))
530 && reg_renumber [j] >= 0
531 && ! hard_reg_not_in_set_p (reg_renumber [j],
532 GET_MODE (regno_reg_rtx [j]),
533 call_used_reg_set))
535 bitmap_set_bit (regs_to_save_restore, j);
536 if (! bitmap_bit_p (restoreloc, j)
537 && bitmap_bit_p (kill, j))
539 for (i = 0; i < n_regs_set; i++)
540 if (REGNO (regs_set [n_regs_set]) == j)
541 break;
542 if (i < n_regs_set)
543 continue;
544 /* Insert save insn */
545 reg = regno_reg_rtx [j];
546 newreg = get_new_reg (j);
547 pat = gen_rtx_SET (VOIDmode, newreg, reg);
548 insert_one_insn (insn, TRUE, pat);
550 if (! bitmap_bit_p (kill, j))
551 bitmap_set_bit (saveloc, j);
552 bitmap_set_bit (restoreloc, j);
556 /* Mark any regs set in INSN as live. */
557 note_stores (PATTERN (insn), mark_reg_store, NULL);
559 #ifdef AUTO_INC_DEC
560 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
561 if (REG_NOTE_KIND (link) == REG_INC)
562 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
563 #endif
565 /* Mark any regs set in INSN and then never used. */
566 while (n_regs_set-- > 0)
568 rtx note = find_regno_note (insn, REG_UNUSED,
569 REGNO (regs_set [n_regs_set]));
570 if (note)
571 mark_reg_death (XEXP (note, 0));
574 if (! flag_ira_move_spills)
576 EXECUTE_IF_SET_IN_BITMAP (saveloc, FIRST_PSEUDO_REGISTER, j, bi)
577 insert_one_insn (first_insn, TRUE,
578 gen_rtx_SET (VOIDmode, get_new_reg (j),
579 regno_reg_rtx [j]));
580 EXECUTE_IF_SET_IN_BITMAP (restoreloc, FIRST_PSEUDO_REGISTER, j, bi)
581 insert_one_insn (last_insn, JUMP_P (last_insn),
582 gen_rtx_SET (VOIDmode, regno_reg_rtx [j],
583 get_new_reg (j)));
585 for (loop = bb->loop_father; loop != NULL; loop = loop->outer)
586 bitmap_ior_into (loop_referenced_regs_array [loop->num], kill);
588 ira_free (regs_set);
591 /* The function used as initialization function by the DF equation
592 solver. */
593 static void
594 do_init_nothing (struct dataflow *dummy ATTRIBUTE_UNUSED,
595 bitmap to_init ATTRIBUTE_UNUSED)
601 /* The function used by the DF equation solver to propagate restore
602 info through block with BB_INDEX. */
603 static bool
604 save_trans_fun (struct dataflow *dummy ATTRIBUTE_UNUSED, int bb_index)
606 struct bb_info *bb_info = BB_INFO_BY_INDEX (bb_index);
607 bitmap in = bb_info->savein;
608 bitmap out = bb_info->saveout;
609 bitmap loc = bb_info->saveloc;
610 bitmap kill = bb_info->kill;
612 return bitmap_ior_and_compl (in, loc, out, kill);
615 /* The function used by the DF equation solver to set up save info
616 for a block BB without successors. */
617 static void
618 save_con_fun_0 (struct dataflow *dummy ATTRIBUTE_UNUSED, basic_block bb)
620 bitmap_clear (BB_INFO (bb)->saveout);
623 /* The function used by the DF equation solver to propagate save info
624 from successor to predecessor on edge E. */
625 static void
626 save_con_fun_n (struct dataflow *dummy ATTRIBUTE_UNUSED, edge e)
628 bitmap op1 = BB_INFO (e->src)->saveout;
629 bitmap op2 = BB_INFO (e->dest)->savein;
631 if (e->src->loop_depth > e->dest->loop_depth)
633 bitmap_and_into (op1, op2);
634 bitmap_and_compl_into
635 (op1, loop_referenced_regs_array [e->src->loop_father->num]);
637 else
638 bitmap_and_into (op1, op2);
641 /* The function calculates savein/saveout sets. */
642 static void
643 calculate_save (void)
645 basic_block bb;
647 problem.dir = DF_BACKWARD;
648 problem.init_fun = do_init_nothing;
649 problem.dataflow_fun = df_iterative_dataflow;
650 problem.trans_fun = save_trans_fun;
651 problem.con_fun_0 = save_con_fun_0;
652 problem.con_fun_n = save_con_fun_n;
654 /* Initialize relations to find maximal solution. */
655 FOR_ALL_BB (bb)
657 bitmap_copy (BB_INFO (bb)->savein, regs_to_save_restore);
658 bitmap_copy (BB_INFO (bb)->saveout, regs_to_save_restore);
660 df_analyze_problem (&dflow, current_all_blocks, NULL, NULL,
661 postorder, n_blocks, false);
666 /* The function used by the DF equation solver to propagate restore
667 info through block with BB_INDEX. */
668 static bool
669 restore_trans_fun (struct dataflow *dummy ATTRIBUTE_UNUSED, int bb_index)
671 struct bb_info *bb_info = BB_INFO_BY_INDEX (bb_index);
672 bitmap in = bb_info->restorein;
673 bitmap out = bb_info->restoreout;
674 bitmap loc = bb_info->restoreloc;
675 bitmap kill = bb_info->kill;
677 return bitmap_ior_and_compl (out, loc, in, kill);
680 /* The function used by the DF equation solver to set up restore info
681 for a block BB without predecessors. */
682 static void
683 restore_con_fun_0 (struct dataflow *dummy ATTRIBUTE_UNUSED, basic_block bb)
685 bitmap_clear (BB_INFO (bb)->restorein);
688 /* The function used by the DF equation solver to propagate restore
689 info from predecessor to successor on edge E. */
690 static void
691 restore_con_fun_n (struct dataflow *dummy ATTRIBUTE_UNUSED, edge e)
693 bitmap op1 = BB_INFO (e->dest)->restorein;
694 bitmap op2 = BB_INFO (e->src)->restoreout;
696 if (e->dest->loop_depth > e->src->loop_depth)
698 bitmap_and_into (op1, op2);
699 bitmap_and_compl_into
700 (op1, loop_referenced_regs_array [e->dest->loop_father->num]);
702 else
703 bitmap_and_into (op1, op2);
706 /* The function calculates restorein/restoreout sets. */
707 static void
708 calculate_restore (void)
710 basic_block bb;
712 problem.dir = DF_FORWARD;
713 problem.init_fun = do_init_nothing;
714 problem.dataflow_fun = df_iterative_dataflow;
715 problem.trans_fun = restore_trans_fun;
716 problem.con_fun_0 = restore_con_fun_0;
717 problem.con_fun_n = restore_con_fun_n;
719 /* Initialize relations to find maximal solution. */
720 FOR_ALL_BB (bb)
722 bitmap_copy (BB_INFO (bb)->restoreout, regs_to_save_restore);
723 bitmap_copy (BB_INFO (bb)->restorein, regs_to_save_restore);
725 df_analyze_problem (&dflow, current_all_blocks, NULL, NULL,
726 postorder, n_blocks, false);
731 /* The function put save/restores insn according to the calculated
732 equation. The function even puts the insns inside blocks to
733 increase live range of pseudos which will be in memory. */
734 static void
735 put_save_restore (void)
737 basic_block bb;
738 edge e;
739 edge_iterator ei;
740 unsigned int j;
741 bitmap_iterator bi;
742 rtx pat;
743 int first_p;
744 bitmap save_at_end, restore_at_start;
746 save_at_end = ira_allocate_bitmap ();
747 restore_at_start = ira_allocate_bitmap ();
748 FOR_EACH_BB (bb)
750 struct bb_info *bb_info = BB_INFO (bb);
751 bitmap kill = bb_info->kill;
752 bitmap restoreout = bb_info->restoreout;
753 bitmap saveout = bb_info->saveout;
754 bitmap savein = bb_info->savein;
755 bitmap restorein = bb_info->restorein;
756 bitmap live_at_start = DF_UPWARD_LIVE_IN (build_df, bb);
757 rtx bb_head, bb_end;
758 rtx insn, next_insn;
760 for (bb_head = BB_HEAD (bb);
761 bb_head != BB_END (bb) && ! INSN_P (bb_head);
762 bb_head = NEXT_INSN (bb_head))
764 for (bb_end = BB_END (bb);
765 bb_end != BB_HEAD (bb) && ! INSN_P (bb_end);
766 bb_end = PREV_INSN (bb_end))
769 bitmap_clear (save_at_end);
770 first_p = TRUE;
771 FOR_EACH_EDGE (e, ei, bb->succs)
773 bitmap savein = BB_INFO (e->dest)->savein;
775 /* (savein - restoreout) ^ (kill U ! saveout) ==
776 (savein - restoreout) ^ kill U (savein - restoreout) - saveout
778 bitmap_and_compl (temp_bitmap2, savein, restoreout);
779 bitmap_and (temp_bitmap, temp_bitmap2, kill);
780 bitmap_ior_and_compl_into (temp_bitmap, temp_bitmap2, saveout);
781 if (first_p)
783 bitmap_copy (save_at_end, temp_bitmap);
784 first_p = FALSE;
786 else
787 bitmap_and_into (save_at_end, temp_bitmap);
790 for (insn = BB_END (bb);
791 insn != PREV_INSN (BB_HEAD (bb));
792 insn = next_insn)
794 next_insn = PREV_INSN (insn);
795 if (! INSN_P (insn))
796 continue;
797 bitmap_clear (referenced_regs);
798 mark_all_referenced_regs (PATTERN (insn));
799 EXECUTE_IF_SET_IN_BITMAP (referenced_regs, FIRST_PSEUDO_REGISTER,
800 j, bi)
801 if (bitmap_bit_p (save_at_end, j))
803 pat = gen_rtx_SET (VOIDmode, get_new_reg (j),
804 regno_reg_rtx [j]);
805 insert_one_insn (insn, JUMP_P (insn), pat);
806 bitmap_clear_bit (save_at_end, j);
809 /* When we don't move info inside the loop, save_at_end can
810 be not empty here. */
811 EXECUTE_IF_SET_IN_BITMAP (save_at_end, FIRST_PSEUDO_REGISTER, j, bi)
813 pat = gen_rtx_SET (VOIDmode, get_new_reg (j), regno_reg_rtx [j]);
814 insert_one_insn (bb_head, TRUE, pat);
817 bitmap_clear (restore_at_start);
818 first_p = TRUE;
819 FOR_EACH_EDGE (e, ei, bb->preds)
821 bitmap restoreout = BB_INFO (e->src)->restoreout;
823 /* (restoreout - savein) ^ (kill U ! restorein)
824 ^ live_at_start ==
825 (((restoreout - savein) ^ live_at_start) ^ kill
826 U ((restoreout - savein) ^ live_at_start) - restorein)
828 bitmap_and_compl (temp_bitmap2, restoreout, savein);
829 bitmap_and_into (temp_bitmap2, live_at_start);
830 bitmap_and (temp_bitmap, temp_bitmap2, kill);
831 bitmap_ior_and_compl_into (temp_bitmap, temp_bitmap2, restorein);
832 if (first_p)
834 bitmap_copy (restore_at_start, temp_bitmap);
835 first_p = FALSE;
837 else
838 bitmap_and_into (restore_at_start, temp_bitmap);
841 for (insn = BB_HEAD (bb);
842 insn != NEXT_INSN (BB_END (bb));
843 insn = next_insn)
845 next_insn = NEXT_INSN (insn);
846 if (! INSN_P (insn))
847 continue;
848 bitmap_clear (referenced_regs);
849 mark_all_referenced_regs (PATTERN (insn));
850 EXECUTE_IF_SET_IN_BITMAP (referenced_regs, FIRST_PSEUDO_REGISTER,
851 j, bi)
852 if (bitmap_bit_p (restore_at_start, j))
854 pat = gen_rtx_SET (VOIDmode, regno_reg_rtx [j],
855 get_new_reg (j));
856 insert_one_insn (insn, TRUE, pat);
857 bitmap_clear_bit (restore_at_start, j);
860 /* When we don't move info inside the loop, restore_at_start can
861 be not empty here. */
862 EXECUTE_IF_SET_IN_BITMAP (restore_at_start, FIRST_PSEUDO_REGISTER, j, bi)
864 pat = gen_rtx_SET (VOIDmode, regno_reg_rtx [j], get_new_reg (j));
865 insert_one_insn (bb_end, JUMP_P (bb_end), pat);
868 FOR_EACH_EDGE (e, ei, bb->succs)
870 bitmap savein = BB_INFO (e->dest)->savein;
872 EXECUTE_IF_SET_IN_BITMAP (savein, FIRST_PSEUDO_REGISTER, j, bi)
873 if (! bitmap_bit_p (restoreout, j)
874 && (bitmap_bit_p (kill, j)
875 || ! bitmap_bit_p (saveout, j))
876 && ! bitmap_bit_p (save_at_end, j))
878 pat = gen_rtx_SET (VOIDmode, get_new_reg (j),
879 regno_reg_rtx [j]);
880 insert_insn_on_edge (pat, e);
881 change_p = TRUE;
882 if (ira_dump_file != NULL)
883 fprintf
884 (ira_dump_file,
885 "Generating save/restore insn %d<-%d on edge %d->%d\n",
886 REGNO (SET_DEST (pat)), REGNO (SET_SRC (pat)),
887 e->src->index, e->dest->index);
891 FOR_EACH_EDGE (e, ei, bb->preds)
893 bitmap restoreout = BB_INFO (e->src)->restoreout;
895 EXECUTE_IF_SET_IN_BITMAP (restoreout, FIRST_PSEUDO_REGISTER, j, bi)
896 if (! bitmap_bit_p (savein, j)
897 && (bitmap_bit_p (kill, j)
898 || ! bitmap_bit_p (restorein, j))
899 && ! bitmap_bit_p (restore_at_start, j)
900 && bitmap_bit_p (live_at_start, j))
902 pat = gen_rtx_SET (VOIDmode, regno_reg_rtx [j],
903 get_new_reg (j));
904 insert_insn_on_edge (pat, e);
905 change_p = TRUE;
906 if (ira_dump_file != NULL)
907 fprintf
908 (ira_dump_file,
909 "Generating save/restore insn %d<-%d on edge %d->%d\n",
910 REGNO (SET_DEST (pat)), REGNO (SET_SRC (pat)),
911 e->src->index, e->dest->index);
915 ira_free_bitmap (save_at_end);
916 ira_free_bitmap (restore_at_start);
919 /* The function splits pseudos living through calls and assigned to a
920 call used register. If FLAG_IRA_MOVE_SPILLS, the function moves
921 save/restore insns to correspondingly to the top and bottom of the
922 CFG but not moving them to more frequently executed places. */
924 split_around_calls (void)
926 change_p = FALSE;
927 init_ira_call_data ();
928 put_save_restore_and_calculate_local_info ();
929 if (flag_ira_move_spills)
931 calculate_save ();
932 calculate_restore ();
933 put_save_restore ();
935 finish_ira_call_data ();
936 fixup_abnormal_edges ();
937 commit_edge_insertions ();
938 return change_p;
943 /* The function returns regno of living through call pseudo which is
944 result of splitting pseudo with ORIGINAL_REGNO. If there is no
945 such regno, the function returns -1. */
947 get_around_calls_regno (int original_regno)
949 pseudo_t p, another_p;
950 struct pseudo_copy *cp, *next_cp;
952 if (original_regno >= ira_max_regno_call_before)
953 return -1;
954 p = regno_pseudo_map [original_regno];
955 for (cp = PSEUDO_COPIES (p); cp != NULL; cp = next_cp)
957 if (cp->first == p)
959 another_p = cp->second;
960 next_cp = cp->next_first_pseudo_copy;
962 else
964 another_p = cp->first;
965 next_cp = cp->next_second_pseudo_copy;
967 if (cp->move_insn == NULL_RTX)
968 continue;
969 if (PSEUDO_REGNO (another_p) >= ira_max_regno_call_before)
970 return PSEUDO_REGNO (another_p);
972 return -1;