gcc/ChangeLog
[official-gcc.git] / gcc / sched-ebb.c
blobb70e071a7f132e0bb3238b36161786d65ef0e20d
1 /* Instruction scheduling pass.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
4 and currently maintained by, Jim Wilson (wilson@cygnus.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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "diagnostic-core.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "regs.h"
31 #include "function.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "insn-attr.h"
35 #include "except.h"
36 #include "recog.h"
37 #include "params.h"
38 #include "sched-int.h"
39 #include "target.h"
42 #ifdef INSN_SCHEDULING
44 /* The number of insns to be scheduled in total. */
45 static int rgn_n_insns;
47 /* The number of insns scheduled so far. */
48 static int sched_rgn_n_insns;
50 /* Set of blocks, that already have their dependencies calculated. */
51 static bitmap_head dont_calc_deps;
53 /* Last basic block in current ebb. */
54 static basic_block last_bb;
56 /* Implementations of the sched_info functions for region scheduling. */
57 static void init_ready_list (void);
58 static void begin_schedule_ready (rtx);
59 static int schedule_more_p (void);
60 static const char *ebb_print_insn (const_rtx, int);
61 static int rank (rtx, rtx);
62 static int ebb_contributes_to_priority (rtx, rtx);
63 static basic_block earliest_block_with_similiar_load (basic_block, rtx);
64 static void add_deps_for_risky_insns (rtx, rtx);
65 static void debug_ebb_dependencies (rtx, rtx);
67 static void ebb_add_remove_insn (rtx, int);
68 static void ebb_add_block (basic_block, basic_block);
69 static basic_block advance_target_bb (basic_block, rtx);
70 static void ebb_fix_recovery_cfg (int, int, int);
72 /* Allocate memory and store the state of the frontend. Return the allocated
73 memory. */
74 static void *
75 save_ebb_state (void)
77 int *p = XNEW (int);
78 *p = sched_rgn_n_insns;
79 return p;
82 /* Restore the state of the frontend from P_, then free it. */
83 static void
84 restore_ebb_state (void *p_)
86 int *p = (int *)p_;
87 sched_rgn_n_insns = *p;
88 free (p_);
91 /* Return nonzero if there are more insns that should be scheduled. */
93 static int
94 schedule_more_p (void)
96 return sched_rgn_n_insns < rgn_n_insns;
99 /* Print dependency information about ebb between HEAD and TAIL. */
100 static void
101 debug_ebb_dependencies (rtx head, rtx tail)
103 fprintf (sched_dump,
104 ";; --------------- forward dependences: ------------ \n");
106 fprintf (sched_dump, "\n;; --- EBB Dependences --- from bb%d to bb%d \n",
107 BLOCK_NUM (head), BLOCK_NUM (tail));
109 debug_dependencies (head, tail);
112 /* Add all insns that are initially ready to the ready list READY. Called
113 once before scheduling a set of insns. */
115 static void
116 init_ready_list (void)
118 int n = 0;
119 rtx prev_head = current_sched_info->prev_head;
120 rtx next_tail = current_sched_info->next_tail;
121 rtx insn;
123 sched_rgn_n_insns = 0;
125 /* Print debugging information. */
126 if (sched_verbose >= 5)
127 debug_ebb_dependencies (NEXT_INSN (prev_head), PREV_INSN (next_tail));
129 /* Initialize ready list with all 'ready' insns in target block.
130 Count number of insns in the target block being scheduled. */
131 for (insn = NEXT_INSN (prev_head); insn != next_tail; insn = NEXT_INSN (insn))
133 try_ready (insn);
134 n++;
137 gcc_assert (n == rgn_n_insns);
140 /* INSN is being scheduled after LAST. Update counters. */
141 static void
142 begin_schedule_ready (rtx insn ATTRIBUTE_UNUSED)
144 sched_rgn_n_insns++;
147 /* INSN is being moved to its place in the schedule, after LAST. */
148 static void
149 begin_move_insn (rtx insn, rtx last)
151 if (BLOCK_FOR_INSN (insn) == last_bb
152 /* INSN is a jump in the last block, ... */
153 && control_flow_insn_p (insn)
154 /* that is going to be moved over some instructions. */
155 && last != PREV_INSN (insn))
157 edge e;
158 basic_block bb;
160 /* An obscure special case, where we do have partially dead
161 instruction scheduled after last control flow instruction.
162 In this case we can create new basic block. It is
163 always exactly one basic block last in the sequence. */
165 e = find_fallthru_edge (last_bb->succs);
167 gcc_checking_assert (!e || !(e->flags & EDGE_COMPLEX));
169 gcc_checking_assert (BLOCK_FOR_INSN (insn) == last_bb
170 && !IS_SPECULATION_CHECK_P (insn)
171 && BB_HEAD (last_bb) != insn
172 && BB_END (last_bb) == insn);
175 rtx x;
177 x = NEXT_INSN (insn);
178 if (e)
179 gcc_checking_assert (NOTE_P (x) || LABEL_P (x));
180 else
181 gcc_checking_assert (BARRIER_P (x));
184 if (e)
186 bb = split_edge (e);
187 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (BB_END (bb)));
189 else
191 /* Create an empty unreachable block after the INSN. */
192 rtx next = NEXT_INSN (insn);
193 if (next && BARRIER_P (next))
194 next = NEXT_INSN (next);
195 bb = create_basic_block (next, NULL_RTX, last_bb);
198 /* split_edge () creates BB before E->DEST. Keep in mind, that
199 this operation extends scheduling region till the end of BB.
200 Hence, we need to shift NEXT_TAIL, so haifa-sched.c won't go out
201 of the scheduling region. */
202 current_sched_info->next_tail = NEXT_INSN (BB_END (bb));
203 gcc_assert (current_sched_info->next_tail);
205 /* Append new basic block to the end of the ebb. */
206 sched_init_only_bb (bb, last_bb);
207 gcc_assert (last_bb == bb);
211 /* Return a string that contains the insn uid and optionally anything else
212 necessary to identify this insn in an output. It's valid to use a
213 static buffer for this. The ALIGNED parameter should cause the string
214 to be formatted so that multiple output lines will line up nicely. */
216 static const char *
217 ebb_print_insn (const_rtx insn, int aligned ATTRIBUTE_UNUSED)
219 static char tmp[80];
221 /* '+' before insn means it is a new cycle start. */
222 if (GET_MODE (insn) == TImode)
223 sprintf (tmp, "+ %4d", INSN_UID (insn));
224 else
225 sprintf (tmp, " %4d", INSN_UID (insn));
227 return tmp;
230 /* Compare priority of two insns. Return a positive number if the second
231 insn is to be preferred for scheduling, and a negative one if the first
232 is to be preferred. Zero if they are equally good. */
234 static int
235 rank (rtx insn1, rtx insn2)
237 basic_block bb1 = BLOCK_FOR_INSN (insn1);
238 basic_block bb2 = BLOCK_FOR_INSN (insn2);
240 if (bb1->count > bb2->count
241 || bb1->frequency > bb2->frequency)
242 return -1;
243 if (bb1->count < bb2->count
244 || bb1->frequency < bb2->frequency)
245 return 1;
246 return 0;
249 /* NEXT is an instruction that depends on INSN (a backward dependence);
250 return nonzero if we should include this dependence in priority
251 calculations. */
253 static int
254 ebb_contributes_to_priority (rtx next ATTRIBUTE_UNUSED,
255 rtx insn ATTRIBUTE_UNUSED)
257 return 1;
260 /* INSN is a JUMP_INSN. Store the set of registers that
261 must be considered as used by this jump in USED. */
263 void
264 ebb_compute_jump_reg_dependencies (rtx insn, regset used)
266 basic_block b = BLOCK_FOR_INSN (insn);
267 edge e;
268 edge_iterator ei;
270 FOR_EACH_EDGE (e, ei, b->succs)
271 if ((e->flags & EDGE_FALLTHRU) == 0)
272 bitmap_ior_into (used, df_get_live_in (e->dest));
275 /* Used in schedule_insns to initialize current_sched_info for scheduling
276 regions (or single basic blocks). */
278 static struct common_sched_info_def ebb_common_sched_info;
280 static struct sched_deps_info_def ebb_sched_deps_info =
282 ebb_compute_jump_reg_dependencies,
283 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
284 NULL,
285 1, 0, 0
288 static struct haifa_sched_info ebb_sched_info =
290 init_ready_list,
291 NULL,
292 schedule_more_p,
293 NULL,
294 rank,
295 ebb_print_insn,
296 ebb_contributes_to_priority,
297 NULL, /* insn_finishes_block_p */
299 NULL, NULL,
300 NULL, NULL,
301 1, 0,
303 ebb_add_remove_insn,
304 begin_schedule_ready,
305 begin_move_insn,
306 advance_target_bb,
308 save_ebb_state,
309 restore_ebb_state,
311 SCHED_EBB
312 /* We can create new blocks in begin_schedule_ready (). */
313 | NEW_BBS
316 /* Returns the earliest block in EBB currently being processed where a
317 "similar load" 'insn2' is found, and hence LOAD_INSN can move
318 speculatively into the found block. All the following must hold:
320 (1) both loads have 1 base register (PFREE_CANDIDATEs).
321 (2) load_insn and load2 have a def-use dependence upon
322 the same insn 'insn1'.
324 From all these we can conclude that the two loads access memory
325 addresses that differ at most by a constant, and hence if moving
326 load_insn would cause an exception, it would have been caused by
327 load2 anyhow.
329 The function uses list (given by LAST_BLOCK) of already processed
330 blocks in EBB. The list is formed in `add_deps_for_risky_insns'. */
332 static basic_block
333 earliest_block_with_similiar_load (basic_block last_block, rtx load_insn)
335 sd_iterator_def back_sd_it;
336 dep_t back_dep;
337 basic_block bb, earliest_block = NULL;
339 FOR_EACH_DEP (load_insn, SD_LIST_BACK, back_sd_it, back_dep)
341 rtx insn1 = DEP_PRO (back_dep);
343 if (DEP_TYPE (back_dep) == REG_DEP_TRUE)
344 /* Found a DEF-USE dependence (insn1, load_insn). */
346 sd_iterator_def fore_sd_it;
347 dep_t fore_dep;
349 FOR_EACH_DEP (insn1, SD_LIST_FORW, fore_sd_it, fore_dep)
351 rtx insn2 = DEP_CON (fore_dep);
352 basic_block insn2_block = BLOCK_FOR_INSN (insn2);
354 if (DEP_TYPE (fore_dep) == REG_DEP_TRUE)
356 if (earliest_block != NULL
357 && earliest_block->index < insn2_block->index)
358 continue;
360 /* Found a DEF-USE dependence (insn1, insn2). */
361 if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
362 /* insn2 not guaranteed to be a 1 base reg load. */
363 continue;
365 for (bb = last_block; bb; bb = (basic_block) bb->aux)
366 if (insn2_block == bb)
367 break;
369 if (!bb)
370 /* insn2 is the similar load. */
371 earliest_block = insn2_block;
377 return earliest_block;
380 /* The following function adds dependencies between jumps and risky
381 insns in given ebb. */
383 static void
384 add_deps_for_risky_insns (rtx head, rtx tail)
386 rtx insn, prev;
387 int classification;
388 rtx last_jump = NULL_RTX;
389 rtx next_tail = NEXT_INSN (tail);
390 basic_block last_block = NULL, bb;
392 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
394 add_delay_dependencies (insn);
395 if (control_flow_insn_p (insn))
397 bb = BLOCK_FOR_INSN (insn);
398 bb->aux = last_block;
399 last_block = bb;
400 /* Ensure blocks stay in the same order. */
401 if (last_jump)
402 add_dependence (insn, last_jump, REG_DEP_ANTI);
403 last_jump = insn;
405 else if (INSN_P (insn) && last_jump != NULL_RTX)
407 classification = haifa_classify_insn (insn);
408 prev = last_jump;
410 switch (classification)
412 case PFREE_CANDIDATE:
413 if (flag_schedule_speculative_load)
415 bb = earliest_block_with_similiar_load (last_block, insn);
416 if (bb)
418 bb = (basic_block) bb->aux;
419 if (!bb)
420 break;
421 prev = BB_END (bb);
424 /* Fall through. */
425 case TRAP_RISKY:
426 case IRISKY:
427 case PRISKY_CANDIDATE:
428 /* ??? We could implement better checking PRISKY_CANDIDATEs
429 analogous to sched-rgn.c. */
430 /* We can not change the mode of the backward
431 dependency because REG_DEP_ANTI has the lowest
432 rank. */
433 if (! sched_insns_conditions_mutex_p (insn, prev))
435 if ((current_sched_info->flags & DO_SPECULATION)
436 && (spec_info->mask & BEGIN_CONTROL))
438 dep_def _dep, *dep = &_dep;
440 init_dep (dep, prev, insn, REG_DEP_ANTI);
442 if (current_sched_info->flags & USE_DEPS_LIST)
444 DEP_STATUS (dep) = set_dep_weak (DEP_ANTI, BEGIN_CONTROL,
445 MAX_DEP_WEAK);
448 sd_add_or_update_dep (dep, false);
450 else
451 add_dependence (insn, prev, REG_DEP_CONTROL);
454 break;
456 default:
457 break;
461 /* Maintain the invariant that bb->aux is clear after use. */
462 while (last_block)
464 bb = (basic_block) last_block->aux;
465 last_block->aux = NULL;
466 last_block = bb;
470 /* Schedule a single extended basic block, defined by the boundaries
471 HEAD and TAIL.
473 We change our expectations about scheduler behaviour depending on
474 whether MODULO_SCHEDULING is true. If it is, we expect that the
475 caller has already called set_modulo_params and created delay pairs
476 as appropriate. If the modulo schedule failed, we return
477 NULL_RTX. */
479 basic_block
480 schedule_ebb (rtx head, rtx tail, bool modulo_scheduling)
482 basic_block first_bb, target_bb;
483 struct deps_desc tmp_deps;
484 bool success;
486 /* Blah. We should fix the rest of the code not to get confused by
487 a note or two. */
488 while (head != tail)
490 if (NOTE_P (head) || DEBUG_INSN_P (head))
491 head = NEXT_INSN (head);
492 else if (NOTE_P (tail) || DEBUG_INSN_P (tail))
493 tail = PREV_INSN (tail);
494 else if (LABEL_P (head))
495 head = NEXT_INSN (head);
496 else
497 break;
500 first_bb = BLOCK_FOR_INSN (head);
501 last_bb = BLOCK_FOR_INSN (tail);
503 if (no_real_insns_p (head, tail))
504 return BLOCK_FOR_INSN (tail);
506 gcc_assert (INSN_P (head) && INSN_P (tail));
508 if (!bitmap_bit_p (&dont_calc_deps, first_bb->index))
510 init_deps_global ();
512 /* Compute dependencies. */
513 init_deps (&tmp_deps, false);
514 sched_analyze (&tmp_deps, head, tail);
515 free_deps (&tmp_deps);
517 add_deps_for_risky_insns (head, tail);
519 if (targetm.sched.dependencies_evaluation_hook)
520 targetm.sched.dependencies_evaluation_hook (head, tail);
522 finish_deps_global ();
524 else
525 /* Only recovery blocks can have their dependencies already calculated,
526 and they always are single block ebbs. */
527 gcc_assert (first_bb == last_bb);
529 /* Set priorities. */
530 current_sched_info->sched_max_insns_priority = 0;
531 rgn_n_insns = set_priorities (head, tail);
532 current_sched_info->sched_max_insns_priority++;
534 current_sched_info->prev_head = PREV_INSN (head);
535 current_sched_info->next_tail = NEXT_INSN (tail);
537 remove_notes (head, tail);
539 unlink_bb_notes (first_bb, last_bb);
541 target_bb = first_bb;
543 /* Make ready list big enough to hold all the instructions from the ebb. */
544 sched_extend_ready_list (rgn_n_insns);
545 success = schedule_block (&target_bb, NULL);
546 gcc_assert (success || modulo_scheduling);
548 /* Free ready list. */
549 sched_finish_ready_list ();
551 /* We might pack all instructions into fewer blocks,
552 so we may made some of them empty. Can't assert (b == last_bb). */
554 /* Sanity check: verify that all region insns were scheduled. */
555 gcc_assert (modulo_scheduling || sched_rgn_n_insns == rgn_n_insns);
557 /* Free dependencies. */
558 sched_free_deps (current_sched_info->head, current_sched_info->tail, true);
560 gcc_assert (haifa_recovery_bb_ever_added_p
561 || deps_pools_are_empty_p ());
563 if (EDGE_COUNT (last_bb->preds) == 0)
564 /* LAST_BB is unreachable. */
566 gcc_assert (first_bb != last_bb
567 && EDGE_COUNT (last_bb->succs) == 0);
568 last_bb = last_bb->prev_bb;
569 delete_basic_block (last_bb->next_bb);
572 return success ? last_bb : NULL;
575 /* Perform initializations before running schedule_ebbs or a single
576 schedule_ebb. */
577 void
578 schedule_ebbs_init (void)
580 /* Setup infos. */
582 memcpy (&ebb_common_sched_info, &haifa_common_sched_info,
583 sizeof (ebb_common_sched_info));
585 ebb_common_sched_info.fix_recovery_cfg = ebb_fix_recovery_cfg;
586 ebb_common_sched_info.add_block = ebb_add_block;
587 ebb_common_sched_info.sched_pass_id = SCHED_EBB_PASS;
589 common_sched_info = &ebb_common_sched_info;
590 sched_deps_info = &ebb_sched_deps_info;
591 current_sched_info = &ebb_sched_info;
594 haifa_sched_init ();
596 compute_bb_for_insn ();
598 /* Initialize DONT_CALC_DEPS and ebb-{start, end} markers. */
599 bitmap_initialize (&dont_calc_deps, 0);
600 bitmap_clear (&dont_calc_deps);
603 /* Perform cleanups after scheduling using schedules_ebbs or schedule_ebb. */
604 void
605 schedule_ebbs_finish (void)
607 bitmap_clear (&dont_calc_deps);
609 /* Reposition the prologue and epilogue notes in case we moved the
610 prologue/epilogue insns. */
611 if (reload_completed)
612 reposition_prologue_and_epilogue_notes ();
614 haifa_sched_finish ();
617 /* The main entry point in this file. */
619 void
620 schedule_ebbs (void)
622 basic_block bb;
623 int probability_cutoff;
624 rtx tail;
626 /* Taking care of this degenerate case makes the rest of
627 this code simpler. */
628 if (n_basic_blocks == NUM_FIXED_BLOCKS)
629 return;
631 if (profile_info && flag_branch_probabilities)
632 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
633 else
634 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
635 probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;
637 schedule_ebbs_init ();
639 /* Schedule every region in the subroutine. */
640 FOR_EACH_BB (bb)
642 rtx head = BB_HEAD (bb);
644 if (bb->flags & BB_DISABLE_SCHEDULE)
645 continue;
647 for (;;)
649 edge e;
650 tail = BB_END (bb);
651 if (bb->next_bb == EXIT_BLOCK_PTR
652 || LABEL_P (BB_HEAD (bb->next_bb)))
653 break;
654 e = find_fallthru_edge (bb->succs);
655 if (! e)
656 break;
657 if (e->probability <= probability_cutoff)
658 break;
659 if (e->dest->flags & BB_DISABLE_SCHEDULE)
660 break;
661 bb = bb->next_bb;
664 bb = schedule_ebb (head, tail, false);
666 schedule_ebbs_finish ();
669 /* INSN has been added to/removed from current ebb. */
670 static void
671 ebb_add_remove_insn (rtx insn ATTRIBUTE_UNUSED, int remove_p)
673 if (!remove_p)
674 rgn_n_insns++;
675 else
676 rgn_n_insns--;
679 /* BB was added to ebb after AFTER. */
680 static void
681 ebb_add_block (basic_block bb, basic_block after)
683 /* Recovery blocks are always bounded by BARRIERS,
684 therefore, they always form single block EBB,
685 therefore, we can use rec->index to identify such EBBs. */
686 if (after == EXIT_BLOCK_PTR)
687 bitmap_set_bit (&dont_calc_deps, bb->index);
688 else if (after == last_bb)
689 last_bb = bb;
692 /* Return next block in ebb chain. For parameter meaning please refer to
693 sched-int.h: struct sched_info: advance_target_bb. */
694 static basic_block
695 advance_target_bb (basic_block bb, rtx insn)
697 if (insn)
699 if (BLOCK_FOR_INSN (insn) != bb
700 && control_flow_insn_p (insn)
701 /* We handle interblock movement of the speculation check
702 or over a speculation check in
703 haifa-sched.c: move_block_after_check (). */
704 && !IS_SPECULATION_BRANCHY_CHECK_P (insn)
705 && !IS_SPECULATION_BRANCHY_CHECK_P (BB_END (bb)))
707 /* Assert that we don't move jumps across blocks. */
708 gcc_assert (!control_flow_insn_p (BB_END (bb))
709 && NOTE_INSN_BASIC_BLOCK_P (BB_HEAD (bb->next_bb)));
710 return bb;
712 else
713 return 0;
715 else
716 /* Return next non empty block. */
720 gcc_assert (bb != last_bb);
722 bb = bb->next_bb;
724 while (bb_note (bb) == BB_END (bb));
726 return bb;
730 /* Fix internal data after interblock movement of jump instruction.
731 For parameter meaning please refer to
732 sched-int.h: struct sched_info: fix_recovery_cfg. */
733 static void
734 ebb_fix_recovery_cfg (int bbi ATTRIBUTE_UNUSED, int jump_bbi,
735 int jump_bb_nexti)
737 gcc_assert (last_bb->index != bbi);
739 if (jump_bb_nexti == last_bb->index)
740 last_bb = BASIC_BLOCK (jump_bbi);
743 #endif /* INSN_SCHEDULING */