2007-03-01 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / sched-ebb.c
blobf6dc8ec04fe673a26c0f17aa851fb432d6ad4711
1 /* Instruction scheduling pass.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
5 and currently maintained by, Jim Wilson (wilson@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "toplev.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "hard-reg-set.h"
32 #include "regs.h"
33 #include "function.h"
34 #include "flags.h"
35 #include "insn-config.h"
36 #include "insn-attr.h"
37 #include "except.h"
38 #include "toplev.h"
39 #include "recog.h"
40 #include "cfglayout.h"
41 #include "params.h"
42 #include "sched-int.h"
43 #include "target.h"
44 #include "output.h"
46 /* The number of insns scheduled so far. */
47 static int sched_n_insns;
49 /* The number of insns to be scheduled in total. */
50 static int n_insns;
52 /* Set of blocks, that already have their dependencies calculated. */
53 static bitmap_head dont_calc_deps;
54 /* Set of basic blocks, that are ebb heads of tails respectively. */
55 static bitmap_head ebb_head, ebb_tail;
57 /* Last basic block in current ebb. */
58 static basic_block last_bb;
60 /* Implementations of the sched_info functions for region scheduling. */
61 static void init_ready_list (void);
62 static void begin_schedule_ready (rtx, rtx);
63 static int schedule_more_p (void);
64 static const char *ebb_print_insn (rtx, int);
65 static int rank (rtx, rtx);
66 static int contributes_to_priority (rtx, rtx);
67 static void compute_jump_reg_dependencies (rtx, regset, regset, regset);
68 static basic_block earliest_block_with_similiar_load (basic_block, rtx);
69 static void add_deps_for_risky_insns (rtx, rtx);
70 static basic_block schedule_ebb (rtx, rtx);
72 static void add_remove_insn (rtx, int);
73 static void add_block1 (basic_block, basic_block);
74 static basic_block advance_target_bb (basic_block, rtx);
75 static void fix_recovery_cfg (int, int, int);
77 #ifdef ENABLE_CHECKING
78 static int ebb_head_or_leaf_p (basic_block, int);
79 #endif
81 /* Return nonzero if there are more insns that should be scheduled. */
83 static int
84 schedule_more_p (void)
86 return sched_n_insns < n_insns;
89 /* Add all insns that are initially ready to the ready list READY. Called
90 once before scheduling a set of insns. */
92 static void
93 init_ready_list (void)
95 int n = 0;
96 rtx prev_head = current_sched_info->prev_head;
97 rtx next_tail = current_sched_info->next_tail;
98 rtx insn;
100 sched_n_insns = 0;
102 #if 0
103 /* Print debugging information. */
104 if (sched_verbose >= 5)
105 debug_dependencies ();
106 #endif
108 /* Initialize ready list with all 'ready' insns in target block.
109 Count number of insns in the target block being scheduled. */
110 for (insn = NEXT_INSN (prev_head); insn != next_tail; insn = NEXT_INSN (insn))
112 try_ready (insn);
113 n++;
116 gcc_assert (n == n_insns);
119 /* INSN is being scheduled after LAST. Update counters. */
120 static void
121 begin_schedule_ready (rtx insn, rtx last)
123 sched_n_insns++;
125 if (BLOCK_FOR_INSN (insn) == last_bb
126 /* INSN is a jump in the last block, ... */
127 && control_flow_insn_p (insn)
128 /* that is going to be moved over some instructions. */
129 && last != PREV_INSN (insn))
131 edge e;
132 edge_iterator ei;
133 basic_block bb;
135 /* An obscure special case, where we do have partially dead
136 instruction scheduled after last control flow instruction.
137 In this case we can create new basic block. It is
138 always exactly one basic block last in the sequence. */
140 FOR_EACH_EDGE (e, ei, last_bb->succs)
141 if (e->flags & EDGE_FALLTHRU)
142 break;
144 #ifdef ENABLE_CHECKING
145 gcc_assert (!e || !(e->flags & EDGE_COMPLEX));
147 gcc_assert (BLOCK_FOR_INSN (insn) == last_bb
148 && !IS_SPECULATION_CHECK_P (insn)
149 && BB_HEAD (last_bb) != insn
150 && BB_END (last_bb) == insn);
153 rtx x;
155 x = NEXT_INSN (insn);
156 if (e)
157 gcc_assert (NOTE_P (x) || LABEL_P (x));
158 else
159 gcc_assert (BARRIER_P (x));
161 #endif
163 if (e)
165 bb = split_edge (e);
166 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (BB_END (bb)));
168 else
169 /* Create an empty unreachable block after the INSN. */
170 bb = create_basic_block (NEXT_INSN (insn), NULL_RTX, last_bb);
172 /* split_edge () creates BB before E->DEST. Keep in mind, that
173 this operation extends scheduling region till the end of BB.
174 Hence, we need to shift NEXT_TAIL, so haifa-sched.c won't go out
175 of the scheduling region. */
176 current_sched_info->next_tail = NEXT_INSN (BB_END (bb));
177 gcc_assert (current_sched_info->next_tail);
179 add_block (bb, last_bb);
180 gcc_assert (last_bb == bb);
184 /* Return a string that contains the insn uid and optionally anything else
185 necessary to identify this insn in an output. It's valid to use a
186 static buffer for this. The ALIGNED parameter should cause the string
187 to be formatted so that multiple output lines will line up nicely. */
189 static const char *
190 ebb_print_insn (rtx insn, int aligned ATTRIBUTE_UNUSED)
192 static char tmp[80];
194 sprintf (tmp, "%4d", INSN_UID (insn));
195 return tmp;
198 /* Compare priority of two insns. Return a positive number if the second
199 insn is to be preferred for scheduling, and a negative one if the first
200 is to be preferred. Zero if they are equally good. */
202 static int
203 rank (rtx insn1, rtx insn2)
205 basic_block bb1 = BLOCK_FOR_INSN (insn1);
206 basic_block bb2 = BLOCK_FOR_INSN (insn2);
208 if (bb1->count > bb2->count
209 || bb1->frequency > bb2->frequency)
210 return -1;
211 if (bb1->count < bb2->count
212 || bb1->frequency < bb2->frequency)
213 return 1;
214 return 0;
217 /* NEXT is an instruction that depends on INSN (a backward dependence);
218 return nonzero if we should include this dependence in priority
219 calculations. */
221 static int
222 contributes_to_priority (rtx next ATTRIBUTE_UNUSED,
223 rtx insn ATTRIBUTE_UNUSED)
225 return 1;
228 /* INSN is a JUMP_INSN, COND_SET is the set of registers that are
229 conditionally set before INSN. Store the set of registers that
230 must be considered as used by this jump in USED and that of
231 registers that must be considered as set in SET. */
233 static void
234 compute_jump_reg_dependencies (rtx insn, regset cond_set, regset used,
235 regset set)
237 basic_block b = BLOCK_FOR_INSN (insn);
238 edge e;
239 edge_iterator ei;
241 FOR_EACH_EDGE (e, ei, b->succs)
242 if (e->flags & EDGE_FALLTHRU)
243 /* The jump may be a by-product of a branch that has been merged
244 in the main codepath after being conditionalized. Therefore
245 it may guard the fallthrough block from using a value that has
246 conditionally overwritten that of the main codepath. So we
247 consider that it restores the value of the main codepath. */
248 bitmap_and (set, glat_start [e->dest->index], cond_set);
249 else
250 bitmap_ior_into (used, glat_start [e->dest->index]);
253 /* Used in schedule_insns to initialize current_sched_info for scheduling
254 regions (or single basic blocks). */
256 static struct sched_info ebb_sched_info =
258 init_ready_list,
259 NULL,
260 schedule_more_p,
261 NULL,
262 rank,
263 ebb_print_insn,
264 contributes_to_priority,
265 compute_jump_reg_dependencies,
267 NULL, NULL,
268 NULL, NULL,
269 0, 1, 0,
271 add_remove_insn,
272 begin_schedule_ready,
273 add_block1,
274 advance_target_bb,
275 fix_recovery_cfg,
276 #ifdef ENABLE_CHECKING
277 ebb_head_or_leaf_p,
278 #endif
279 /* We need to DETACH_LIVE_INFO to be able to create new basic blocks.
280 See begin_schedule_ready (). */
281 SCHED_EBB | USE_GLAT | DETACH_LIFE_INFO
284 /* Returns the earliest block in EBB currently being processed where a
285 "similar load" 'insn2' is found, and hence LOAD_INSN can move
286 speculatively into the found block. All the following must hold:
288 (1) both loads have 1 base register (PFREE_CANDIDATEs).
289 (2) load_insn and load2 have a def-use dependence upon
290 the same insn 'insn1'.
292 From all these we can conclude that the two loads access memory
293 addresses that differ at most by a constant, and hence if moving
294 load_insn would cause an exception, it would have been caused by
295 load2 anyhow.
297 The function uses list (given by LAST_BLOCK) of already processed
298 blocks in EBB. The list is formed in `add_deps_for_risky_insns'. */
300 static basic_block
301 earliest_block_with_similiar_load (basic_block last_block, rtx load_insn)
303 dep_link_t back_link;
304 basic_block bb, earliest_block = NULL;
306 FOR_EACH_DEP_LINK (back_link, INSN_BACK_DEPS (load_insn))
308 rtx insn1 = DEP_LINK_PRO (back_link);
310 if (DEP_LINK_KIND (back_link) == REG_DEP_TRUE)
312 /* Found a DEF-USE dependence (insn1, load_insn). */
313 dep_link_t fore_link;
315 FOR_EACH_DEP_LINK (fore_link, INSN_FORW_DEPS (insn1))
317 rtx insn2 = DEP_LINK_CON (fore_link);
318 basic_block insn2_block = BLOCK_FOR_INSN (insn2);
320 if (DEP_LINK_KIND (fore_link) == REG_DEP_TRUE)
322 if (earliest_block != NULL
323 && earliest_block->index < insn2_block->index)
324 continue;
326 /* Found a DEF-USE dependence (insn1, insn2). */
327 if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
328 /* insn2 not guaranteed to be a 1 base reg load. */
329 continue;
331 for (bb = last_block; bb; bb = bb->aux)
332 if (insn2_block == bb)
333 break;
335 if (!bb)
336 /* insn2 is the similar load. */
337 earliest_block = insn2_block;
343 return earliest_block;
346 /* The following function adds dependencies between jumps and risky
347 insns in given ebb. */
349 static void
350 add_deps_for_risky_insns (rtx head, rtx tail)
352 rtx insn, prev;
353 int class;
354 rtx last_jump = NULL_RTX;
355 rtx next_tail = NEXT_INSN (tail);
356 basic_block last_block = NULL, bb;
358 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
359 if (control_flow_insn_p (insn))
361 bb = BLOCK_FOR_INSN (insn);
362 bb->aux = last_block;
363 last_block = bb;
364 last_jump = insn;
366 else if (INSN_P (insn) && last_jump != NULL_RTX)
368 class = haifa_classify_insn (insn);
369 prev = last_jump;
370 switch (class)
372 case PFREE_CANDIDATE:
373 if (flag_schedule_speculative_load)
375 bb = earliest_block_with_similiar_load (last_block, insn);
376 if (bb)
378 bb = bb->aux;
379 if (!bb)
380 break;
381 prev = BB_END (bb);
384 /* Fall through. */
385 case TRAP_RISKY:
386 case IRISKY:
387 case PRISKY_CANDIDATE:
388 /* ??? We could implement better checking PRISKY_CANDIDATEs
389 analogous to sched-rgn.c. */
390 /* We can not change the mode of the backward
391 dependency because REG_DEP_ANTI has the lowest
392 rank. */
393 if (! sched_insns_conditions_mutex_p (insn, prev))
395 if (!(current_sched_info->flags & DO_SPECULATION))
397 enum DEPS_ADJUST_RESULT res;
399 res = add_or_update_back_dep (insn, prev,
400 REG_DEP_ANTI, DEP_ANTI);
402 if (res == DEP_CREATED)
403 add_forw_dep (DEPS_LIST_FIRST (INSN_BACK_DEPS (insn)));
404 else
405 gcc_assert (res != DEP_CHANGED);
407 else
408 add_or_update_back_forw_dep (insn, prev, REG_DEP_ANTI,
409 set_dep_weak (DEP_ANTI,
410 BEGIN_CONTROL,
411 MAX_DEP_WEAK));
414 break;
416 default:
417 break;
420 /* Maintain the invariant that bb->aux is clear after use. */
421 while (last_block)
423 bb = last_block->aux;
424 last_block->aux = NULL;
425 last_block = bb;
429 /* Schedule a single extended basic block, defined by the boundaries HEAD
430 and TAIL. */
432 static basic_block
433 schedule_ebb (rtx head, rtx tail)
435 basic_block first_bb, target_bb;
436 struct deps tmp_deps;
438 first_bb = BLOCK_FOR_INSN (head);
439 last_bb = BLOCK_FOR_INSN (tail);
441 if (no_real_insns_p (head, tail))
442 return BLOCK_FOR_INSN (tail);
444 gcc_assert (INSN_P (head) && INSN_P (tail));
446 if (!bitmap_bit_p (&dont_calc_deps, first_bb->index))
448 init_deps_global ();
450 /* Compute backward dependencies. */
451 init_deps (&tmp_deps);
452 sched_analyze (&tmp_deps, head, tail);
453 free_deps (&tmp_deps);
455 /* Compute forward dependencies. */
456 compute_forward_dependences (head, tail);
458 add_deps_for_risky_insns (head, tail);
460 if (targetm.sched.dependencies_evaluation_hook)
461 targetm.sched.dependencies_evaluation_hook (head, tail);
463 finish_deps_global ();
465 else
466 /* Only recovery blocks can have their dependencies already calculated,
467 and they always are single block ebbs. */
468 gcc_assert (first_bb == last_bb);
470 /* Set priorities. */
471 current_sched_info->sched_max_insns_priority = 0;
472 n_insns = set_priorities (head, tail);
473 current_sched_info->sched_max_insns_priority++;
475 current_sched_info->prev_head = PREV_INSN (head);
476 current_sched_info->next_tail = NEXT_INSN (tail);
478 /* rm_other_notes only removes notes which are _inside_ the
479 block---that is, it won't remove notes before the first real insn
480 or after the last real insn of the block. So if the first insn
481 has a REG_SAVE_NOTE which would otherwise be emitted before the
482 insn, it is redundant with the note before the start of the
483 block, and so we have to take it out. */
484 if (INSN_P (head))
486 rtx note;
488 for (note = REG_NOTES (head); note; note = XEXP (note, 1))
489 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
490 remove_note (head, note);
493 /* Remove remaining note insns from the block, save them in
494 note_list. These notes are restored at the end of
495 schedule_block (). */
496 rm_other_notes (head, tail);
498 unlink_bb_notes (first_bb, last_bb);
500 current_sched_info->queue_must_finish_empty = 1;
502 target_bb = first_bb;
503 schedule_block (&target_bb, n_insns);
505 /* We might pack all instructions into fewer blocks,
506 so we may made some of them empty. Can't assert (b == last_bb). */
508 /* Sanity check: verify that all region insns were scheduled. */
509 gcc_assert (sched_n_insns == n_insns);
510 head = current_sched_info->head;
511 tail = current_sched_info->tail;
513 if (EDGE_COUNT (last_bb->preds) == 0)
514 /* LAST_BB is unreachable. */
516 gcc_assert (first_bb != last_bb
517 && EDGE_COUNT (last_bb->succs) == 0);
518 last_bb = last_bb->prev_bb;
519 delete_basic_block (last_bb->next_bb);
522 return last_bb;
525 /* The one entry point in this file. */
527 void
528 schedule_ebbs (void)
530 basic_block bb;
531 int probability_cutoff;
532 rtx tail;
533 sbitmap large_region_blocks, blocks;
534 int any_large_regions;
536 if (profile_info && flag_branch_probabilities)
537 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
538 else
539 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
540 probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;
542 /* Taking care of this degenerate case makes the rest of
543 this code simpler. */
544 if (n_basic_blocks == NUM_FIXED_BLOCKS)
545 return;
547 /* We need current_sched_info in init_dependency_caches, which is
548 invoked via sched_init. */
549 current_sched_info = &ebb_sched_info;
551 sched_init ();
553 compute_bb_for_insn ();
555 /* Initialize DONT_CALC_DEPS and ebb-{start, end} markers. */
556 bitmap_initialize (&dont_calc_deps, 0);
557 bitmap_clear (&dont_calc_deps);
558 bitmap_initialize (&ebb_head, 0);
559 bitmap_clear (&ebb_head);
560 bitmap_initialize (&ebb_tail, 0);
561 bitmap_clear (&ebb_tail);
563 /* Schedule every region in the subroutine. */
564 FOR_EACH_BB (bb)
566 rtx head = BB_HEAD (bb);
568 for (;;)
570 edge e;
571 edge_iterator ei;
572 tail = BB_END (bb);
573 if (bb->next_bb == EXIT_BLOCK_PTR
574 || LABEL_P (BB_HEAD (bb->next_bb)))
575 break;
576 FOR_EACH_EDGE (e, ei, bb->succs)
577 if ((e->flags & EDGE_FALLTHRU) != 0)
578 break;
579 if (! e)
580 break;
581 if (e->probability <= probability_cutoff)
582 break;
583 bb = bb->next_bb;
586 /* Blah. We should fix the rest of the code not to get confused by
587 a note or two. */
588 while (head != tail)
590 if (NOTE_P (head))
591 head = NEXT_INSN (head);
592 else if (NOTE_P (tail))
593 tail = PREV_INSN (tail);
594 else if (LABEL_P (head))
595 head = NEXT_INSN (head);
596 else
597 break;
600 bitmap_set_bit (&ebb_head, BLOCK_NUM (head));
601 bb = schedule_ebb (head, tail);
602 bitmap_set_bit (&ebb_tail, bb->index);
604 bitmap_clear (&dont_calc_deps);
606 gcc_assert (current_sched_info->flags & DETACH_LIFE_INFO);
607 /* We can create new basic blocks during scheduling, and
608 attach_life_info () will create regsets for them
609 (along with attaching existing info back). */
610 attach_life_info ();
612 /* Updating register live information. */
613 allocate_reg_life_data ();
615 any_large_regions = 0;
616 large_region_blocks = sbitmap_alloc (last_basic_block);
617 sbitmap_zero (large_region_blocks);
618 FOR_EACH_BB (bb)
619 SET_BIT (large_region_blocks, bb->index);
621 blocks = sbitmap_alloc (last_basic_block);
622 sbitmap_zero (blocks);
624 /* Update life information. For regions consisting of multiple blocks
625 we've possibly done interblock scheduling that affects global liveness.
626 For regions consisting of single blocks we need to do only local
627 liveness. */
628 FOR_EACH_BB (bb)
630 int bbi;
632 bbi = bb->index;
634 if (!bitmap_bit_p (&ebb_head, bbi)
635 || !bitmap_bit_p (&ebb_tail, bbi)
636 /* New blocks (e.g. recovery blocks) should be processed
637 as parts of large regions. */
638 || !glat_start[bbi])
639 any_large_regions = 1;
640 else
642 SET_BIT (blocks, bbi);
643 RESET_BIT (large_region_blocks, bbi);
647 update_life_info (blocks, UPDATE_LIFE_LOCAL, 0);
648 sbitmap_free (blocks);
650 if (any_large_regions)
652 update_life_info (large_region_blocks, UPDATE_LIFE_GLOBAL, 0);
654 #ifdef ENABLE_CHECKING
655 /* !!! We can't check reg_live_info here because of the fact,
656 that destination registers of COND_EXEC's may be dead
657 before scheduling (while they should be alive). Don't know why. */
658 /*check_reg_live (true);*/
659 #endif
661 sbitmap_free (large_region_blocks);
663 bitmap_clear (&ebb_head);
664 bitmap_clear (&ebb_tail);
666 /* Reposition the prologue and epilogue notes in case we moved the
667 prologue/epilogue insns. */
668 if (reload_completed)
669 reposition_prologue_and_epilogue_notes (get_insns ());
671 sched_finish ();
674 /* INSN has been added to/removed from current ebb. */
675 static void
676 add_remove_insn (rtx insn ATTRIBUTE_UNUSED, int remove_p)
678 if (!remove_p)
679 n_insns++;
680 else
681 n_insns--;
684 /* BB was added to ebb after AFTER. */
685 static void
686 add_block1 (basic_block bb, basic_block after)
688 /* Recovery blocks are always bounded by BARRIERS,
689 therefore, they always form single block EBB,
690 therefore, we can use rec->index to identify such EBBs. */
691 if (after == EXIT_BLOCK_PTR)
692 bitmap_set_bit (&dont_calc_deps, bb->index);
693 else if (after == last_bb)
694 last_bb = bb;
697 /* Return next block in ebb chain. For parameter meaning please refer to
698 sched-int.h: struct sched_info: advance_target_bb. */
699 static basic_block
700 advance_target_bb (basic_block bb, rtx insn)
702 if (insn)
704 if (BLOCK_FOR_INSN (insn) != bb
705 && control_flow_insn_p (insn)
706 /* We handle interblock movement of the speculation check
707 or over a speculation check in
708 haifa-sched.c: move_block_after_check (). */
709 && !IS_SPECULATION_BRANCHY_CHECK_P (insn)
710 && !IS_SPECULATION_BRANCHY_CHECK_P (BB_END (bb)))
712 /* Assert that we don't move jumps across blocks. */
713 gcc_assert (!control_flow_insn_p (BB_END (bb))
714 && NOTE_INSN_BASIC_BLOCK_P (BB_HEAD (bb->next_bb)));
715 return bb;
717 else
718 return 0;
720 else
721 /* Return next non empty block. */
725 gcc_assert (bb != last_bb);
727 bb = bb->next_bb;
729 while (bb_note (bb) == BB_END (bb));
731 return bb;
735 /* Fix internal data after interblock movement of jump instruction.
736 For parameter meaning please refer to
737 sched-int.h: struct sched_info: fix_recovery_cfg. */
738 static void
739 fix_recovery_cfg (int bbi ATTRIBUTE_UNUSED, int jump_bbi, int jump_bb_nexti)
741 gcc_assert (last_bb->index != bbi);
743 if (jump_bb_nexti == last_bb->index)
744 last_bb = BASIC_BLOCK (jump_bbi);
747 #ifdef ENABLE_CHECKING
748 /* Return non zero, if BB is first or last (depending of LEAF_P) block in
749 current ebb. For more information please refer to
750 sched-int.h: struct sched_info: region_head_or_leaf_p. */
751 static int
752 ebb_head_or_leaf_p (basic_block bb, int leaf_p)
754 if (!leaf_p)
755 return bitmap_bit_p (&ebb_head, bb->index);
756 else
757 return bitmap_bit_p (&ebb_tail, bb->index);
759 #endif /* ENABLE_CHECKING */