gcc/
[official-gcc.git] / gcc / sched-ebb.c
blob7e22874601c164c702eb909f061746849c33f9dc
1 /* Instruction scheduling pass.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
6 and currently maintained by, Jim Wilson (wilson@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
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"
47 #ifdef INSN_SCHEDULING
49 /* The number of insns scheduled so far. */
50 static int sched_n_insns;
52 /* The number of insns to be scheduled in total. */
53 static int n_insns;
55 /* Set of blocks, that already have their dependencies calculated. */
56 static bitmap_head dont_calc_deps;
58 /* Last basic block in current ebb. */
59 static basic_block last_bb;
61 /* Implementations of the sched_info functions for region scheduling. */
62 static void init_ready_list (void);
63 static void begin_schedule_ready (rtx, rtx);
64 static int schedule_more_p (void);
65 static const char *ebb_print_insn (rtx, int);
66 static int rank (rtx, rtx);
67 static int contributes_to_priority (rtx, rtx);
68 static void compute_jump_reg_dependencies (rtx, regset, regset, regset);
69 static basic_block earliest_block_with_similiar_load (basic_block, rtx);
70 static void add_deps_for_risky_insns (rtx, rtx);
71 static basic_block schedule_ebb (rtx, rtx);
73 static void add_remove_insn (rtx, int);
74 static void add_block1 (basic_block, basic_block);
75 static basic_block advance_target_bb (basic_block, rtx);
76 static void fix_recovery_cfg (int, int, int);
78 /* Return nonzero if there are more insns that should be scheduled. */
80 static int
81 schedule_more_p (void)
83 return sched_n_insns < n_insns;
86 /* Print dependency information about ebb between HEAD and TAIL. */
87 static void
88 debug_ebb_dependencies (rtx head, rtx tail)
90 fprintf (sched_dump,
91 ";; --------------- forward dependences: ------------ \n");
93 fprintf (sched_dump, "\n;; --- EBB Dependences --- from bb%d to bb%d \n",
94 BLOCK_NUM (head), BLOCK_NUM (tail));
96 debug_dependencies (head, tail);
99 /* Add all insns that are initially ready to the ready list READY. Called
100 once before scheduling a set of insns. */
102 static void
103 init_ready_list (void)
105 int n = 0;
106 rtx prev_head = current_sched_info->prev_head;
107 rtx next_tail = current_sched_info->next_tail;
108 rtx insn;
110 sched_n_insns = 0;
112 /* Print debugging information. */
113 if (sched_verbose >= 5)
114 debug_ebb_dependencies (NEXT_INSN (prev_head), PREV_INSN (next_tail));
116 /* Initialize ready list with all 'ready' insns in target block.
117 Count number of insns in the target block being scheduled. */
118 for (insn = NEXT_INSN (prev_head); insn != next_tail; insn = NEXT_INSN (insn))
120 try_ready (insn);
121 n++;
124 gcc_assert (n == n_insns);
127 /* INSN is being scheduled after LAST. Update counters. */
128 static void
129 begin_schedule_ready (rtx insn, rtx last)
131 sched_n_insns++;
133 if (BLOCK_FOR_INSN (insn) == last_bb
134 /* INSN is a jump in the last block, ... */
135 && control_flow_insn_p (insn)
136 /* that is going to be moved over some instructions. */
137 && last != PREV_INSN (insn))
139 edge e;
140 edge_iterator ei;
141 basic_block bb;
143 /* An obscure special case, where we do have partially dead
144 instruction scheduled after last control flow instruction.
145 In this case we can create new basic block. It is
146 always exactly one basic block last in the sequence. */
148 FOR_EACH_EDGE (e, ei, last_bb->succs)
149 if (e->flags & EDGE_FALLTHRU)
150 break;
152 #ifdef ENABLE_CHECKING
153 gcc_assert (!e || !(e->flags & EDGE_COMPLEX));
155 gcc_assert (BLOCK_FOR_INSN (insn) == last_bb
156 && !IS_SPECULATION_CHECK_P (insn)
157 && BB_HEAD (last_bb) != insn
158 && BB_END (last_bb) == insn);
161 rtx x;
163 x = NEXT_INSN (insn);
164 if (e)
165 gcc_assert (NOTE_P (x) || LABEL_P (x));
166 else
167 gcc_assert (BARRIER_P (x));
169 #endif
171 if (e)
173 bb = split_edge (e);
174 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (BB_END (bb)));
176 else
177 /* Create an empty unreachable block after the INSN. */
178 bb = create_basic_block (NEXT_INSN (insn), NULL_RTX, last_bb);
180 /* split_edge () creates BB before E->DEST. Keep in mind, that
181 this operation extends scheduling region till the end of BB.
182 Hence, we need to shift NEXT_TAIL, so haifa-sched.c won't go out
183 of the scheduling region. */
184 current_sched_info->next_tail = NEXT_INSN (BB_END (bb));
185 gcc_assert (current_sched_info->next_tail);
187 add_block (bb, last_bb);
188 gcc_assert (last_bb == bb);
192 /* Return a string that contains the insn uid and optionally anything else
193 necessary to identify this insn in an output. It's valid to use a
194 static buffer for this. The ALIGNED parameter should cause the string
195 to be formatted so that multiple output lines will line up nicely. */
197 static const char *
198 ebb_print_insn (rtx insn, int aligned ATTRIBUTE_UNUSED)
200 static char tmp[80];
202 sprintf (tmp, "%4d", INSN_UID (insn));
203 return tmp;
206 /* Compare priority of two insns. Return a positive number if the second
207 insn is to be preferred for scheduling, and a negative one if the first
208 is to be preferred. Zero if they are equally good. */
210 static int
211 rank (rtx insn1, rtx insn2)
213 basic_block bb1 = BLOCK_FOR_INSN (insn1);
214 basic_block bb2 = BLOCK_FOR_INSN (insn2);
216 if (bb1->count > bb2->count
217 || bb1->frequency > bb2->frequency)
218 return -1;
219 if (bb1->count < bb2->count
220 || bb1->frequency < bb2->frequency)
221 return 1;
222 return 0;
225 /* NEXT is an instruction that depends on INSN (a backward dependence);
226 return nonzero if we should include this dependence in priority
227 calculations. */
229 static int
230 contributes_to_priority (rtx next ATTRIBUTE_UNUSED,
231 rtx insn ATTRIBUTE_UNUSED)
233 return 1;
236 /* INSN is a JUMP_INSN, COND_SET is the set of registers that are
237 conditionally set before INSN. Store the set of registers that
238 must be considered as used by this jump in USED and that of
239 registers that must be considered as set in SET. */
241 static void
242 compute_jump_reg_dependencies (rtx insn, regset cond_set, regset used,
243 regset set)
245 basic_block b = BLOCK_FOR_INSN (insn);
246 edge e;
247 edge_iterator ei;
249 FOR_EACH_EDGE (e, ei, b->succs)
250 if (e->flags & EDGE_FALLTHRU)
251 /* The jump may be a by-product of a branch that has been merged
252 in the main codepath after being conditionalized. Therefore
253 it may guard the fallthrough block from using a value that has
254 conditionally overwritten that of the main codepath. So we
255 consider that it restores the value of the main codepath. */
256 bitmap_and (set, df_get_live_in (e->dest), cond_set);
257 else
258 bitmap_ior_into (used, df_get_live_in (e->dest));
261 /* Used in schedule_insns to initialize current_sched_info for scheduling
262 regions (or single basic blocks). */
264 static struct sched_info ebb_sched_info =
266 init_ready_list,
267 NULL,
268 schedule_more_p,
269 NULL,
270 rank,
271 ebb_print_insn,
272 contributes_to_priority,
273 compute_jump_reg_dependencies,
275 NULL, NULL,
276 NULL, NULL,
277 0, 1, 0,
279 add_remove_insn,
280 begin_schedule_ready,
281 add_block1,
282 advance_target_bb,
283 fix_recovery_cfg,
284 SCHED_EBB
285 /* We can create new blocks in begin_schedule_ready (). */
286 | NEW_BBS
289 /* Returns the earliest block in EBB currently being processed where a
290 "similar load" 'insn2' is found, and hence LOAD_INSN can move
291 speculatively into the found block. All the following must hold:
293 (1) both loads have 1 base register (PFREE_CANDIDATEs).
294 (2) load_insn and load2 have a def-use dependence upon
295 the same insn 'insn1'.
297 From all these we can conclude that the two loads access memory
298 addresses that differ at most by a constant, and hence if moving
299 load_insn would cause an exception, it would have been caused by
300 load2 anyhow.
302 The function uses list (given by LAST_BLOCK) of already processed
303 blocks in EBB. The list is formed in `add_deps_for_risky_insns'. */
305 static basic_block
306 earliest_block_with_similiar_load (basic_block last_block, rtx load_insn)
308 sd_iterator_def back_sd_it;
309 dep_t back_dep;
310 basic_block bb, earliest_block = NULL;
312 FOR_EACH_DEP (load_insn, SD_LIST_BACK, back_sd_it, back_dep)
314 rtx insn1 = DEP_PRO (back_dep);
316 if (DEP_TYPE (back_dep) == REG_DEP_TRUE)
317 /* Found a DEF-USE dependence (insn1, load_insn). */
319 sd_iterator_def fore_sd_it;
320 dep_t fore_dep;
322 FOR_EACH_DEP (insn1, SD_LIST_FORW, fore_sd_it, fore_dep)
324 rtx insn2 = DEP_CON (fore_dep);
325 basic_block insn2_block = BLOCK_FOR_INSN (insn2);
327 if (DEP_TYPE (fore_dep) == REG_DEP_TRUE)
329 if (earliest_block != NULL
330 && earliest_block->index < insn2_block->index)
331 continue;
333 /* Found a DEF-USE dependence (insn1, insn2). */
334 if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
335 /* insn2 not guaranteed to be a 1 base reg load. */
336 continue;
338 for (bb = last_block; bb; bb = bb->aux)
339 if (insn2_block == bb)
340 break;
342 if (!bb)
343 /* insn2 is the similar load. */
344 earliest_block = insn2_block;
350 return earliest_block;
353 /* The following function adds dependencies between jumps and risky
354 insns in given ebb. */
356 static void
357 add_deps_for_risky_insns (rtx head, rtx tail)
359 rtx insn, prev;
360 int class;
361 rtx last_jump = NULL_RTX;
362 rtx next_tail = NEXT_INSN (tail);
363 basic_block last_block = NULL, bb;
365 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
366 if (control_flow_insn_p (insn))
368 bb = BLOCK_FOR_INSN (insn);
369 bb->aux = last_block;
370 last_block = bb;
371 last_jump = insn;
373 else if (INSN_P (insn) && last_jump != NULL_RTX)
375 class = haifa_classify_insn (insn);
376 prev = last_jump;
377 switch (class)
379 case PFREE_CANDIDATE:
380 if (flag_schedule_speculative_load)
382 bb = earliest_block_with_similiar_load (last_block, insn);
383 if (bb)
385 bb = bb->aux;
386 if (!bb)
387 break;
388 prev = BB_END (bb);
391 /* Fall through. */
392 case TRAP_RISKY:
393 case IRISKY:
394 case PRISKY_CANDIDATE:
395 /* ??? We could implement better checking PRISKY_CANDIDATEs
396 analogous to sched-rgn.c. */
397 /* We can not change the mode of the backward
398 dependency because REG_DEP_ANTI has the lowest
399 rank. */
400 if (! sched_insns_conditions_mutex_p (insn, prev))
402 dep_def _dep, *dep = &_dep;
404 init_dep (dep, prev, insn, REG_DEP_ANTI);
406 if (!(current_sched_info->flags & USE_DEPS_LIST))
408 enum DEPS_ADJUST_RESULT res;
410 res = sd_add_or_update_dep (dep, false);
412 /* We can't change an existing dependency with
413 DEP_ANTI. */
414 gcc_assert (res != DEP_CHANGED);
416 else
418 if ((current_sched_info->flags & DO_SPECULATION)
419 && (spec_info->mask & BEGIN_CONTROL))
420 DEP_STATUS (dep) = set_dep_weak (DEP_ANTI, BEGIN_CONTROL,
421 MAX_DEP_WEAK);
423 sd_add_or_update_dep (dep, false);
425 /* Dep_status could have been changed.
426 No assertion here. */
430 break;
432 default:
433 break;
436 /* Maintain the invariant that bb->aux is clear after use. */
437 while (last_block)
439 bb = last_block->aux;
440 last_block->aux = NULL;
441 last_block = bb;
445 /* Schedule a single extended basic block, defined by the boundaries HEAD
446 and TAIL. */
448 static basic_block
449 schedule_ebb (rtx head, rtx tail)
451 basic_block first_bb, target_bb;
452 struct deps tmp_deps;
454 first_bb = BLOCK_FOR_INSN (head);
455 last_bb = BLOCK_FOR_INSN (tail);
457 if (no_real_insns_p (head, tail))
458 return BLOCK_FOR_INSN (tail);
460 gcc_assert (INSN_P (head) && INSN_P (tail));
462 if (!bitmap_bit_p (&dont_calc_deps, first_bb->index))
464 init_deps_global ();
466 /* Compute dependencies. */
467 init_deps (&tmp_deps);
468 sched_analyze (&tmp_deps, head, tail);
469 free_deps (&tmp_deps);
471 add_deps_for_risky_insns (head, tail);
473 if (targetm.sched.dependencies_evaluation_hook)
474 targetm.sched.dependencies_evaluation_hook (head, tail);
476 finish_deps_global ();
478 else
479 /* Only recovery blocks can have their dependencies already calculated,
480 and they always are single block ebbs. */
481 gcc_assert (first_bb == last_bb);
483 /* Set priorities. */
484 current_sched_info->sched_max_insns_priority = 0;
485 n_insns = set_priorities (head, tail);
486 current_sched_info->sched_max_insns_priority++;
488 current_sched_info->prev_head = PREV_INSN (head);
489 current_sched_info->next_tail = NEXT_INSN (tail);
491 /* rm_other_notes only removes notes which are _inside_ the
492 block---that is, it won't remove notes before the first real insn
493 or after the last real insn of the block. So if the first insn
494 has a REG_SAVE_NOTE which would otherwise be emitted before the
495 insn, it is redundant with the note before the start of the
496 block, and so we have to take it out. */
497 if (INSN_P (head))
499 rtx note;
501 for (note = REG_NOTES (head); note; note = XEXP (note, 1))
502 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
503 remove_note (head, note);
506 /* Remove remaining note insns from the block, save them in
507 note_list. These notes are restored at the end of
508 schedule_block (). */
509 rm_other_notes (head, tail);
511 unlink_bb_notes (first_bb, last_bb);
513 current_sched_info->queue_must_finish_empty = 1;
515 target_bb = first_bb;
516 schedule_block (&target_bb, n_insns);
518 /* We might pack all instructions into fewer blocks,
519 so we may made some of them empty. Can't assert (b == last_bb). */
521 /* Sanity check: verify that all region insns were scheduled. */
522 gcc_assert (sched_n_insns == n_insns);
524 /* Free dependencies. */
525 sched_free_deps (current_sched_info->head, current_sched_info->tail, true);
527 gcc_assert (haifa_recovery_bb_ever_added_p
528 || deps_pools_are_empty_p ());
530 if (EDGE_COUNT (last_bb->preds) == 0)
531 /* LAST_BB is unreachable. */
533 gcc_assert (first_bb != last_bb
534 && EDGE_COUNT (last_bb->succs) == 0);
535 last_bb = last_bb->prev_bb;
536 delete_basic_block (last_bb->next_bb);
539 return last_bb;
542 /* The one entry point in this file. */
544 void
545 schedule_ebbs (void)
547 basic_block bb;
548 int probability_cutoff;
549 rtx tail;
551 if (profile_info && flag_branch_probabilities)
552 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
553 else
554 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
555 probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;
557 /* Taking care of this degenerate case makes the rest of
558 this code simpler. */
559 if (n_basic_blocks == NUM_FIXED_BLOCKS)
560 return;
562 /* We need current_sched_info in init_dependency_caches, which is
563 invoked via sched_init. */
564 current_sched_info = &ebb_sched_info;
566 df_set_flags (DF_LR_RUN_DCE);
567 df_note_add_problem ();
568 df_analyze ();
569 df_clear_flags (DF_LR_RUN_DCE);
570 regstat_compute_calls_crossed ();
571 sched_init ();
573 compute_bb_for_insn ();
575 /* Initialize DONT_CALC_DEPS and ebb-{start, end} markers. */
576 bitmap_initialize (&dont_calc_deps, 0);
577 bitmap_clear (&dont_calc_deps);
579 /* Schedule every region in the subroutine. */
580 FOR_EACH_BB (bb)
582 rtx head = BB_HEAD (bb);
584 for (;;)
586 edge e;
587 edge_iterator ei;
588 tail = BB_END (bb);
589 if (bb->next_bb == EXIT_BLOCK_PTR
590 || LABEL_P (BB_HEAD (bb->next_bb)))
591 break;
592 FOR_EACH_EDGE (e, ei, bb->succs)
593 if ((e->flags & EDGE_FALLTHRU) != 0)
594 break;
595 if (! e)
596 break;
597 if (e->probability <= probability_cutoff)
598 break;
599 bb = bb->next_bb;
602 /* Blah. We should fix the rest of the code not to get confused by
603 a note or two. */
604 while (head != tail)
606 if (NOTE_P (head))
607 head = NEXT_INSN (head);
608 else if (NOTE_P (tail))
609 tail = PREV_INSN (tail);
610 else if (LABEL_P (head))
611 head = NEXT_INSN (head);
612 else
613 break;
616 bb = schedule_ebb (head, tail);
618 bitmap_clear (&dont_calc_deps);
620 /* Reposition the prologue and epilogue notes in case we moved the
621 prologue/epilogue insns. */
622 if (reload_completed)
623 reposition_prologue_and_epilogue_notes ();
625 sched_finish ();
626 regstat_free_calls_crossed ();
629 /* INSN has been added to/removed from current ebb. */
630 static void
631 add_remove_insn (rtx insn ATTRIBUTE_UNUSED, int remove_p)
633 if (!remove_p)
634 n_insns++;
635 else
636 n_insns--;
639 /* BB was added to ebb after AFTER. */
640 static void
641 add_block1 (basic_block bb, basic_block after)
643 /* Recovery blocks are always bounded by BARRIERS,
644 therefore, they always form single block EBB,
645 therefore, we can use rec->index to identify such EBBs. */
646 if (after == EXIT_BLOCK_PTR)
647 bitmap_set_bit (&dont_calc_deps, bb->index);
648 else if (after == last_bb)
649 last_bb = bb;
652 /* Return next block in ebb chain. For parameter meaning please refer to
653 sched-int.h: struct sched_info: advance_target_bb. */
654 static basic_block
655 advance_target_bb (basic_block bb, rtx insn)
657 if (insn)
659 if (BLOCK_FOR_INSN (insn) != bb
660 && control_flow_insn_p (insn)
661 /* We handle interblock movement of the speculation check
662 or over a speculation check in
663 haifa-sched.c: move_block_after_check (). */
664 && !IS_SPECULATION_BRANCHY_CHECK_P (insn)
665 && !IS_SPECULATION_BRANCHY_CHECK_P (BB_END (bb)))
667 /* Assert that we don't move jumps across blocks. */
668 gcc_assert (!control_flow_insn_p (BB_END (bb))
669 && NOTE_INSN_BASIC_BLOCK_P (BB_HEAD (bb->next_bb)));
670 return bb;
672 else
673 return 0;
675 else
676 /* Return next non empty block. */
680 gcc_assert (bb != last_bb);
682 bb = bb->next_bb;
684 while (bb_note (bb) == BB_END (bb));
686 return bb;
690 /* Fix internal data after interblock movement of jump instruction.
691 For parameter meaning please refer to
692 sched-int.h: struct sched_info: fix_recovery_cfg. */
693 static void
694 fix_recovery_cfg (int bbi ATTRIBUTE_UNUSED, int jump_bbi, int jump_bb_nexti)
696 gcc_assert (last_bb->index != bbi);
698 if (jump_bb_nexti == last_bb->index)
699 last_bb = BASIC_BLOCK (jump_bbi);
702 #endif /* INSN_SCHEDULING */