new tests
[official-gcc.git] / gcc / sched-ebb.c
blob6bb223bbd3bb32065dd652c5820c39574ceae385
1 /* Instruction scheduling pass.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 "diagnostic-core.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 "recog.h"
39 #include "cfglayout.h"
40 #include "params.h"
41 #include "sched-int.h"
42 #include "target.h"
43 #include "output.h"
46 #ifdef INSN_SCHEDULING
48 /* The number of insns to be scheduled in total. */
49 static int rgn_n_insns;
51 /* The number of insns scheduled so far. */
52 static int sched_rgn_n_insns;
54 /* Set of blocks, that already have their dependencies calculated. */
55 static bitmap_head dont_calc_deps;
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);
63 static int schedule_more_p (void);
64 static const char *ebb_print_insn (const_rtx, int);
65 static int rank (rtx, rtx);
66 static int ebb_contributes_to_priority (rtx, rtx);
67 static basic_block earliest_block_with_similiar_load (basic_block, rtx);
68 static void add_deps_for_risky_insns (rtx, rtx);
69 static basic_block schedule_ebb (rtx, rtx);
70 static void debug_ebb_dependencies (rtx, rtx);
72 static void ebb_add_remove_insn (rtx, int);
73 static void ebb_add_block (basic_block, basic_block);
74 static basic_block advance_target_bb (basic_block, rtx);
75 static void ebb_fix_recovery_cfg (int, int, int);
77 /* Return nonzero if there are more insns that should be scheduled. */
79 static int
80 schedule_more_p (void)
82 return sched_rgn_n_insns < rgn_n_insns;
85 /* Print dependency information about ebb between HEAD and TAIL. */
86 static void
87 debug_ebb_dependencies (rtx head, rtx tail)
89 fprintf (sched_dump,
90 ";; --------------- forward dependences: ------------ \n");
92 fprintf (sched_dump, "\n;; --- EBB Dependences --- from bb%d to bb%d \n",
93 BLOCK_NUM (head), BLOCK_NUM (tail));
95 debug_dependencies (head, tail);
98 /* Add all insns that are initially ready to the ready list READY. Called
99 once before scheduling a set of insns. */
101 static void
102 init_ready_list (void)
104 int n = 0;
105 rtx prev_head = current_sched_info->prev_head;
106 rtx next_tail = current_sched_info->next_tail;
107 rtx insn;
109 sched_rgn_n_insns = 0;
111 /* Print debugging information. */
112 if (sched_verbose >= 5)
113 debug_ebb_dependencies (NEXT_INSN (prev_head), PREV_INSN (next_tail));
115 /* Initialize ready list with all 'ready' insns in target block.
116 Count number of insns in the target block being scheduled. */
117 for (insn = NEXT_INSN (prev_head); insn != next_tail; insn = NEXT_INSN (insn))
119 try_ready (insn);
120 n++;
123 gcc_assert (n == rgn_n_insns);
126 /* INSN is being scheduled after LAST. Update counters. */
127 static void
128 begin_schedule_ready (rtx insn ATTRIBUTE_UNUSED)
130 sched_rgn_n_insns++;
133 /* INSN is being moved to its place in the schedule, after LAST. */
134 static void
135 begin_move_insn (rtx insn, rtx last)
137 if (BLOCK_FOR_INSN (insn) == last_bb
138 /* INSN is a jump in the last block, ... */
139 && control_flow_insn_p (insn)
140 /* that is going to be moved over some instructions. */
141 && last != PREV_INSN (insn))
143 edge e;
144 basic_block bb;
146 /* An obscure special case, where we do have partially dead
147 instruction scheduled after last control flow instruction.
148 In this case we can create new basic block. It is
149 always exactly one basic block last in the sequence. */
151 e = find_fallthru_edge (last_bb->succs);
153 gcc_checking_assert (!e || !(e->flags & EDGE_COMPLEX));
155 gcc_checking_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_checking_assert (NOTE_P (x) || LABEL_P (x));
166 else
167 gcc_checking_assert (BARRIER_P (x));
170 if (e)
172 bb = split_edge (e);
173 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (BB_END (bb)));
175 else
176 /* Create an empty unreachable block after the INSN. */
177 bb = create_basic_block (NEXT_INSN (insn), NULL_RTX, last_bb);
179 /* split_edge () creates BB before E->DEST. Keep in mind, that
180 this operation extends scheduling region till the end of BB.
181 Hence, we need to shift NEXT_TAIL, so haifa-sched.c won't go out
182 of the scheduling region. */
183 current_sched_info->next_tail = NEXT_INSN (BB_END (bb));
184 gcc_assert (current_sched_info->next_tail);
186 /* Append new basic block to the end of the ebb. */
187 sched_init_only_bb (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 (const_rtx insn, int aligned ATTRIBUTE_UNUSED)
200 static char tmp[80];
202 /* '+' before insn means it is a new cycle start. */
203 if (GET_MODE (insn) == TImode)
204 sprintf (tmp, "+ %4d", INSN_UID (insn));
205 else
206 sprintf (tmp, " %4d", INSN_UID (insn));
208 return tmp;
211 /* Compare priority of two insns. Return a positive number if the second
212 insn is to be preferred for scheduling, and a negative one if the first
213 is to be preferred. Zero if they are equally good. */
215 static int
216 rank (rtx insn1, rtx insn2)
218 basic_block bb1 = BLOCK_FOR_INSN (insn1);
219 basic_block bb2 = BLOCK_FOR_INSN (insn2);
221 if (bb1->count > bb2->count
222 || bb1->frequency > bb2->frequency)
223 return -1;
224 if (bb1->count < bb2->count
225 || bb1->frequency < bb2->frequency)
226 return 1;
227 return 0;
230 /* NEXT is an instruction that depends on INSN (a backward dependence);
231 return nonzero if we should include this dependence in priority
232 calculations. */
234 static int
235 ebb_contributes_to_priority (rtx next ATTRIBUTE_UNUSED,
236 rtx insn ATTRIBUTE_UNUSED)
238 return 1;
241 /* INSN is a JUMP_INSN, COND_SET is the set of registers that are
242 conditionally set before INSN. Store the set of registers that
243 must be considered as used by this jump in USED and that of
244 registers that must be considered as set in SET. */
246 void
247 ebb_compute_jump_reg_dependencies (rtx insn, regset cond_set, regset used,
248 regset set)
250 basic_block b = BLOCK_FOR_INSN (insn);
251 edge e;
252 edge_iterator ei;
254 FOR_EACH_EDGE (e, ei, b->succs)
255 if (e->flags & EDGE_FALLTHRU)
256 /* The jump may be a by-product of a branch that has been merged
257 in the main codepath after being conditionalized. Therefore
258 it may guard the fallthrough block from using a value that has
259 conditionally overwritten that of the main codepath. So we
260 consider that it restores the value of the main codepath. */
261 bitmap_and (set, df_get_live_in (e->dest), cond_set);
262 else
263 bitmap_ior_into (used, df_get_live_in (e->dest));
266 /* Used in schedule_insns to initialize current_sched_info for scheduling
267 regions (or single basic blocks). */
269 static struct common_sched_info_def ebb_common_sched_info;
271 static struct sched_deps_info_def ebb_sched_deps_info =
273 ebb_compute_jump_reg_dependencies,
274 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
275 NULL,
276 1, 0, 0
279 static struct haifa_sched_info ebb_sched_info =
281 init_ready_list,
282 NULL,
283 schedule_more_p,
284 NULL,
285 rank,
286 ebb_print_insn,
287 ebb_contributes_to_priority,
288 NULL, /* insn_finishes_block_p */
290 NULL, NULL,
291 NULL, NULL,
292 1, 0,
294 ebb_add_remove_insn,
295 begin_schedule_ready,
296 begin_move_insn,
297 advance_target_bb,
298 SCHED_EBB
299 /* We can create new blocks in begin_schedule_ready (). */
300 | NEW_BBS
303 /* Returns the earliest block in EBB currently being processed where a
304 "similar load" 'insn2' is found, and hence LOAD_INSN can move
305 speculatively into the found block. All the following must hold:
307 (1) both loads have 1 base register (PFREE_CANDIDATEs).
308 (2) load_insn and load2 have a def-use dependence upon
309 the same insn 'insn1'.
311 From all these we can conclude that the two loads access memory
312 addresses that differ at most by a constant, and hence if moving
313 load_insn would cause an exception, it would have been caused by
314 load2 anyhow.
316 The function uses list (given by LAST_BLOCK) of already processed
317 blocks in EBB. The list is formed in `add_deps_for_risky_insns'. */
319 static basic_block
320 earliest_block_with_similiar_load (basic_block last_block, rtx load_insn)
322 sd_iterator_def back_sd_it;
323 dep_t back_dep;
324 basic_block bb, earliest_block = NULL;
326 FOR_EACH_DEP (load_insn, SD_LIST_BACK, back_sd_it, back_dep)
328 rtx insn1 = DEP_PRO (back_dep);
330 if (DEP_TYPE (back_dep) == REG_DEP_TRUE)
331 /* Found a DEF-USE dependence (insn1, load_insn). */
333 sd_iterator_def fore_sd_it;
334 dep_t fore_dep;
336 FOR_EACH_DEP (insn1, SD_LIST_FORW, fore_sd_it, fore_dep)
338 rtx insn2 = DEP_CON (fore_dep);
339 basic_block insn2_block = BLOCK_FOR_INSN (insn2);
341 if (DEP_TYPE (fore_dep) == REG_DEP_TRUE)
343 if (earliest_block != NULL
344 && earliest_block->index < insn2_block->index)
345 continue;
347 /* Found a DEF-USE dependence (insn1, insn2). */
348 if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
349 /* insn2 not guaranteed to be a 1 base reg load. */
350 continue;
352 for (bb = last_block; bb; bb = (basic_block) bb->aux)
353 if (insn2_block == bb)
354 break;
356 if (!bb)
357 /* insn2 is the similar load. */
358 earliest_block = insn2_block;
364 return earliest_block;
367 /* The following function adds dependencies between jumps and risky
368 insns in given ebb. */
370 static void
371 add_deps_for_risky_insns (rtx head, rtx tail)
373 rtx insn, prev;
374 int classification;
375 rtx last_jump = NULL_RTX;
376 rtx next_tail = NEXT_INSN (tail);
377 basic_block last_block = NULL, bb;
379 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
380 if (control_flow_insn_p (insn))
382 bb = BLOCK_FOR_INSN (insn);
383 bb->aux = last_block;
384 last_block = bb;
385 last_jump = insn;
387 else if (INSN_P (insn) && last_jump != NULL_RTX)
389 classification = haifa_classify_insn (insn);
390 prev = last_jump;
391 switch (classification)
393 case PFREE_CANDIDATE:
394 if (flag_schedule_speculative_load)
396 bb = earliest_block_with_similiar_load (last_block, insn);
397 if (bb)
399 bb = (basic_block) bb->aux;
400 if (!bb)
401 break;
402 prev = BB_END (bb);
405 /* Fall through. */
406 case TRAP_RISKY:
407 case IRISKY:
408 case PRISKY_CANDIDATE:
409 /* ??? We could implement better checking PRISKY_CANDIDATEs
410 analogous to sched-rgn.c. */
411 /* We can not change the mode of the backward
412 dependency because REG_DEP_ANTI has the lowest
413 rank. */
414 if (! sched_insns_conditions_mutex_p (insn, prev))
416 dep_def _dep, *dep = &_dep;
418 init_dep (dep, prev, insn, REG_DEP_ANTI);
420 if (!(current_sched_info->flags & USE_DEPS_LIST))
422 enum DEPS_ADJUST_RESULT res;
424 res = sd_add_or_update_dep (dep, false);
426 /* We can't change an existing dependency with
427 DEP_ANTI. */
428 gcc_assert (res != DEP_CHANGED);
430 else
432 if ((current_sched_info->flags & DO_SPECULATION)
433 && (spec_info->mask & BEGIN_CONTROL))
434 DEP_STATUS (dep) = set_dep_weak (DEP_ANTI, BEGIN_CONTROL,
435 MAX_DEP_WEAK);
437 sd_add_or_update_dep (dep, false);
439 /* Dep_status could have been changed.
440 No assertion here. */
444 break;
446 default:
447 break;
450 /* Maintain the invariant that bb->aux is clear after use. */
451 while (last_block)
453 bb = (basic_block) last_block->aux;
454 last_block->aux = NULL;
455 last_block = bb;
459 /* Schedule a single extended basic block, defined by the boundaries HEAD
460 and TAIL. */
462 static basic_block
463 schedule_ebb (rtx head, rtx tail)
465 basic_block first_bb, target_bb;
466 struct deps_desc tmp_deps;
468 first_bb = BLOCK_FOR_INSN (head);
469 last_bb = BLOCK_FOR_INSN (tail);
471 if (no_real_insns_p (head, tail))
472 return BLOCK_FOR_INSN (tail);
474 gcc_assert (INSN_P (head) && INSN_P (tail));
476 if (!bitmap_bit_p (&dont_calc_deps, first_bb->index))
478 init_deps_global ();
480 /* Compute dependencies. */
481 init_deps (&tmp_deps, false);
482 sched_analyze (&tmp_deps, head, tail);
483 free_deps (&tmp_deps);
485 add_deps_for_risky_insns (head, tail);
487 if (targetm.sched.dependencies_evaluation_hook)
488 targetm.sched.dependencies_evaluation_hook (head, tail);
490 finish_deps_global ();
492 else
493 /* Only recovery blocks can have their dependencies already calculated,
494 and they always are single block ebbs. */
495 gcc_assert (first_bb == last_bb);
497 /* Set priorities. */
498 current_sched_info->sched_max_insns_priority = 0;
499 rgn_n_insns = set_priorities (head, tail);
500 current_sched_info->sched_max_insns_priority++;
502 current_sched_info->prev_head = PREV_INSN (head);
503 current_sched_info->next_tail = NEXT_INSN (tail);
505 remove_notes (head, tail);
507 unlink_bb_notes (first_bb, last_bb);
509 target_bb = first_bb;
511 /* Make ready list big enough to hold all the instructions from the ebb. */
512 sched_extend_ready_list (rgn_n_insns);
513 schedule_block (&target_bb);
514 /* Free ready list. */
515 sched_finish_ready_list ();
517 /* We might pack all instructions into fewer blocks,
518 so we may made some of them empty. Can't assert (b == last_bb). */
520 /* Sanity check: verify that all region insns were scheduled. */
521 gcc_assert (sched_rgn_n_insns == rgn_n_insns);
523 /* Free dependencies. */
524 sched_free_deps (current_sched_info->head, current_sched_info->tail, true);
526 gcc_assert (haifa_recovery_bb_ever_added_p
527 || deps_pools_are_empty_p ());
529 if (EDGE_COUNT (last_bb->preds) == 0)
530 /* LAST_BB is unreachable. */
532 gcc_assert (first_bb != last_bb
533 && EDGE_COUNT (last_bb->succs) == 0);
534 last_bb = last_bb->prev_bb;
535 delete_basic_block (last_bb->next_bb);
538 return last_bb;
541 /* The one entry point in this file. */
543 void
544 schedule_ebbs (void)
546 basic_block bb;
547 int probability_cutoff;
548 rtx tail;
550 if (profile_info && flag_branch_probabilities)
551 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
552 else
553 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
554 probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;
556 /* Taking care of this degenerate case makes the rest of
557 this code simpler. */
558 if (n_basic_blocks == NUM_FIXED_BLOCKS)
559 return;
561 /* Setup infos. */
563 memcpy (&ebb_common_sched_info, &haifa_common_sched_info,
564 sizeof (ebb_common_sched_info));
566 ebb_common_sched_info.fix_recovery_cfg = ebb_fix_recovery_cfg;
567 ebb_common_sched_info.add_block = ebb_add_block;
568 ebb_common_sched_info.sched_pass_id = SCHED_EBB_PASS;
570 common_sched_info = &ebb_common_sched_info;
571 sched_deps_info = &ebb_sched_deps_info;
572 current_sched_info = &ebb_sched_info;
575 haifa_sched_init ();
577 compute_bb_for_insn ();
579 /* Initialize DONT_CALC_DEPS and ebb-{start, end} markers. */
580 bitmap_initialize (&dont_calc_deps, 0);
581 bitmap_clear (&dont_calc_deps);
583 /* Schedule every region in the subroutine. */
584 FOR_EACH_BB (bb)
586 rtx head = BB_HEAD (bb);
588 if (bb->flags & BB_DISABLE_SCHEDULE)
589 continue;
591 for (;;)
593 edge e;
594 tail = BB_END (bb);
595 if (bb->next_bb == EXIT_BLOCK_PTR
596 || LABEL_P (BB_HEAD (bb->next_bb)))
597 break;
598 e = find_fallthru_edge (bb->succs);
599 if (! e)
600 break;
601 if (e->probability <= probability_cutoff)
602 break;
603 if (e->dest->flags & BB_DISABLE_SCHEDULE)
604 break;
605 bb = bb->next_bb;
608 /* Blah. We should fix the rest of the code not to get confused by
609 a note or two. */
610 while (head != tail)
612 if (NOTE_P (head) || DEBUG_INSN_P (head))
613 head = NEXT_INSN (head);
614 else if (NOTE_P (tail) || DEBUG_INSN_P (tail))
615 tail = PREV_INSN (tail);
616 else if (LABEL_P (head))
617 head = NEXT_INSN (head);
618 else
619 break;
622 bb = schedule_ebb (head, tail);
624 bitmap_clear (&dont_calc_deps);
626 /* Reposition the prologue and epilogue notes in case we moved the
627 prologue/epilogue insns. */
628 if (reload_completed)
629 reposition_prologue_and_epilogue_notes ();
631 haifa_sched_finish ();
634 /* INSN has been added to/removed from current ebb. */
635 static void
636 ebb_add_remove_insn (rtx insn ATTRIBUTE_UNUSED, int remove_p)
638 if (!remove_p)
639 rgn_n_insns++;
640 else
641 rgn_n_insns--;
644 /* BB was added to ebb after AFTER. */
645 static void
646 ebb_add_block (basic_block bb, basic_block after)
648 /* Recovery blocks are always bounded by BARRIERS,
649 therefore, they always form single block EBB,
650 therefore, we can use rec->index to identify such EBBs. */
651 if (after == EXIT_BLOCK_PTR)
652 bitmap_set_bit (&dont_calc_deps, bb->index);
653 else if (after == last_bb)
654 last_bb = bb;
657 /* Return next block in ebb chain. For parameter meaning please refer to
658 sched-int.h: struct sched_info: advance_target_bb. */
659 static basic_block
660 advance_target_bb (basic_block bb, rtx insn)
662 if (insn)
664 if (BLOCK_FOR_INSN (insn) != bb
665 && control_flow_insn_p (insn)
666 /* We handle interblock movement of the speculation check
667 or over a speculation check in
668 haifa-sched.c: move_block_after_check (). */
669 && !IS_SPECULATION_BRANCHY_CHECK_P (insn)
670 && !IS_SPECULATION_BRANCHY_CHECK_P (BB_END (bb)))
672 /* Assert that we don't move jumps across blocks. */
673 gcc_assert (!control_flow_insn_p (BB_END (bb))
674 && NOTE_INSN_BASIC_BLOCK_P (BB_HEAD (bb->next_bb)));
675 return bb;
677 else
678 return 0;
680 else
681 /* Return next non empty block. */
685 gcc_assert (bb != last_bb);
687 bb = bb->next_bb;
689 while (bb_note (bb) == BB_END (bb));
691 return bb;
695 /* Fix internal data after interblock movement of jump instruction.
696 For parameter meaning please refer to
697 sched-int.h: struct sched_info: fix_recovery_cfg. */
698 static void
699 ebb_fix_recovery_cfg (int bbi ATTRIBUTE_UNUSED, int jump_bbi,
700 int jump_bb_nexti)
702 gcc_assert (last_bb->index != bbi);
704 if (jump_bb_nexti == last_bb->index)
705 last_bb = BASIC_BLOCK (jump_bbi);
708 #endif /* INSN_SCHEDULING */