2015-09-03 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / sched-ebb.c
blob01aa808463197003fb7790f9b8efb586201410ee
1 /* Instruction scheduling pass.
2 Copyright (C) 1992-2015 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 "backend.h"
26 #include "cfghooks.h"
27 #include "rtl.h"
28 #include "df.h"
29 #include "diagnostic-core.h"
30 #include "tm_p.h"
31 #include "regs.h"
32 #include "profile.h"
33 #include "flags.h"
34 #include "insn-config.h"
35 #include "insn-attr.h"
36 #include "except.h"
37 #include "recog.h"
38 #include "params.h"
39 #include "cfgrtl.h"
40 #include "cfgbuild.h"
41 #include "sched-int.h"
42 #include "target.h"
45 #ifdef INSN_SCHEDULING
47 /* The number of insns to be scheduled in total. */
48 static int rgn_n_insns;
50 /* The number of insns scheduled so far. */
51 static int sched_rgn_n_insns;
53 /* Set of blocks, that already have their dependencies calculated. */
54 static bitmap_head dont_calc_deps;
56 /* Last basic block in current ebb. */
57 static basic_block last_bb;
59 /* Implementations of the sched_info functions for region scheduling. */
60 static void init_ready_list (void);
61 static void begin_schedule_ready (rtx_insn *);
62 static int schedule_more_p (void);
63 static const char *ebb_print_insn (const rtx_insn *, int);
64 static int rank (rtx_insn *, rtx_insn *);
65 static int ebb_contributes_to_priority (rtx_insn *, rtx_insn *);
66 static basic_block earliest_block_with_similiar_load (basic_block, rtx);
67 static void add_deps_for_risky_insns (rtx_insn *, rtx_insn *);
68 static void debug_ebb_dependencies (rtx_insn *, rtx_insn *);
70 static void ebb_add_remove_insn (rtx_insn *, int);
71 static void ebb_add_block (basic_block, basic_block);
72 static basic_block advance_target_bb (basic_block, rtx_insn *);
73 static void ebb_fix_recovery_cfg (int, int, int);
75 /* Allocate memory and store the state of the frontend. Return the allocated
76 memory. */
77 static void *
78 save_ebb_state (void)
80 int *p = XNEW (int);
81 *p = sched_rgn_n_insns;
82 return p;
85 /* Restore the state of the frontend from P_, then free it. */
86 static void
87 restore_ebb_state (void *p_)
89 int *p = (int *)p_;
90 sched_rgn_n_insns = *p;
91 free (p_);
94 /* Return nonzero if there are more insns that should be scheduled. */
96 static int
97 schedule_more_p (void)
99 return sched_rgn_n_insns < rgn_n_insns;
102 /* Print dependency information about ebb between HEAD and TAIL. */
103 static void
104 debug_ebb_dependencies (rtx_insn *head, rtx_insn *tail)
106 fprintf (sched_dump,
107 ";; --------------- forward dependences: ------------ \n");
109 fprintf (sched_dump, "\n;; --- EBB Dependences --- from bb%d to bb%d \n",
110 BLOCK_NUM (head), BLOCK_NUM (tail));
112 debug_dependencies (head, tail);
115 /* Add all insns that are initially ready to the ready list READY. Called
116 once before scheduling a set of insns. */
118 static void
119 init_ready_list (void)
121 int n = 0;
122 rtx_insn *prev_head = current_sched_info->prev_head;
123 rtx_insn *next_tail = current_sched_info->next_tail;
124 rtx_insn *insn;
126 sched_rgn_n_insns = 0;
128 /* Print debugging information. */
129 if (sched_verbose >= 5)
130 debug_ebb_dependencies (NEXT_INSN (prev_head), PREV_INSN (next_tail));
132 /* Initialize ready list with all 'ready' insns in target block.
133 Count number of insns in the target block being scheduled. */
134 for (insn = NEXT_INSN (prev_head); insn != next_tail; insn = NEXT_INSN (insn))
136 try_ready (insn);
137 n++;
140 gcc_assert (n == rgn_n_insns);
143 /* INSN is being scheduled after LAST. Update counters. */
144 static void
145 begin_schedule_ready (rtx_insn *insn ATTRIBUTE_UNUSED)
147 sched_rgn_n_insns++;
150 /* INSN is being moved to its place in the schedule, after LAST. */
151 static void
152 begin_move_insn (rtx_insn *insn, rtx_insn *last)
154 if (BLOCK_FOR_INSN (insn) == last_bb
155 /* INSN is a jump in the last block, ... */
156 && control_flow_insn_p (insn)
157 /* that is going to be moved over some instructions. */
158 && last != PREV_INSN (insn))
160 edge e;
161 basic_block bb;
163 /* An obscure special case, where we do have partially dead
164 instruction scheduled after last control flow instruction.
165 In this case we can create new basic block. It is
166 always exactly one basic block last in the sequence. */
168 e = find_fallthru_edge (last_bb->succs);
170 gcc_checking_assert (!e || !(e->flags & EDGE_COMPLEX));
172 gcc_checking_assert (BLOCK_FOR_INSN (insn) == last_bb
173 && !IS_SPECULATION_CHECK_P (insn)
174 && BB_HEAD (last_bb) != insn
175 && BB_END (last_bb) == insn);
178 rtx_insn *x = NEXT_INSN (insn);
179 if (e)
180 gcc_checking_assert (NOTE_P (x) || LABEL_P (x));
181 else
182 gcc_checking_assert (BARRIER_P (x));
185 if (e)
187 bb = split_edge (e);
188 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (BB_END (bb)));
190 else
192 /* Create an empty unreachable block after the INSN. */
193 rtx_insn *next = NEXT_INSN (insn);
194 if (next && BARRIER_P (next))
195 next = NEXT_INSN (next);
196 bb = create_basic_block (next, NULL_RTX, last_bb);
199 /* split_edge () creates BB before E->DEST. Keep in mind, that
200 this operation extends scheduling region till the end of BB.
201 Hence, we need to shift NEXT_TAIL, so haifa-sched.c won't go out
202 of the scheduling region. */
203 current_sched_info->next_tail = NEXT_INSN (BB_END (bb));
204 gcc_assert (current_sched_info->next_tail);
206 /* Append new basic block to the end of the ebb. */
207 sched_init_only_bb (bb, last_bb);
208 gcc_assert (last_bb == bb);
212 /* Return a string that contains the insn uid and optionally anything else
213 necessary to identify this insn in an output. It's valid to use a
214 static buffer for this. The ALIGNED parameter should cause the string
215 to be formatted so that multiple output lines will line up nicely. */
217 static const char *
218 ebb_print_insn (const rtx_insn *insn, int aligned ATTRIBUTE_UNUSED)
220 static char tmp[80];
222 /* '+' before insn means it is a new cycle start. */
223 if (GET_MODE (insn) == TImode)
224 sprintf (tmp, "+ %4d", INSN_UID (insn));
225 else
226 sprintf (tmp, " %4d", INSN_UID (insn));
228 return tmp;
231 /* Compare priority of two insns. Return a positive number if the second
232 insn is to be preferred for scheduling, and a negative one if the first
233 is to be preferred. Zero if they are equally good. */
235 static int
236 rank (rtx_insn *insn1, rtx_insn *insn2)
238 basic_block bb1 = BLOCK_FOR_INSN (insn1);
239 basic_block bb2 = BLOCK_FOR_INSN (insn2);
241 if (bb1->count > bb2->count
242 || bb1->frequency > bb2->frequency)
243 return -1;
244 if (bb1->count < bb2->count
245 || bb1->frequency < bb2->frequency)
246 return 1;
247 return 0;
250 /* NEXT is an instruction that depends on INSN (a backward dependence);
251 return nonzero if we should include this dependence in priority
252 calculations. */
254 static int
255 ebb_contributes_to_priority (rtx_insn *next ATTRIBUTE_UNUSED,
256 rtx_insn *insn ATTRIBUTE_UNUSED)
258 return 1;
261 /* INSN is a JUMP_INSN. Store the set of registers that
262 must be considered as used by this jump in USED. */
264 void
265 ebb_compute_jump_reg_dependencies (rtx insn, regset used)
267 basic_block b = BLOCK_FOR_INSN (insn);
268 edge e;
269 edge_iterator ei;
271 FOR_EACH_EDGE (e, ei, b->succs)
272 if ((e->flags & EDGE_FALLTHRU) == 0)
273 bitmap_ior_into (used, df_get_live_in (e->dest));
276 /* Used in schedule_insns to initialize current_sched_info for scheduling
277 regions (or single basic blocks). */
279 static struct common_sched_info_def ebb_common_sched_info;
281 static struct sched_deps_info_def ebb_sched_deps_info =
283 ebb_compute_jump_reg_dependencies,
284 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
285 NULL,
286 1, 0, 0
289 static struct haifa_sched_info ebb_sched_info =
291 init_ready_list,
292 NULL,
293 schedule_more_p,
294 NULL,
295 rank,
296 ebb_print_insn,
297 ebb_contributes_to_priority,
298 NULL, /* insn_finishes_block_p */
300 NULL, NULL,
301 NULL, NULL,
302 1, 0,
304 ebb_add_remove_insn,
305 begin_schedule_ready,
306 begin_move_insn,
307 advance_target_bb,
309 save_ebb_state,
310 restore_ebb_state,
312 SCHED_EBB
313 /* We can create new blocks in begin_schedule_ready (). */
314 | NEW_BBS
317 /* Returns the earliest block in EBB currently being processed where a
318 "similar load" 'insn2' is found, and hence LOAD_INSN can move
319 speculatively into the found block. All the following must hold:
321 (1) both loads have 1 base register (PFREE_CANDIDATEs).
322 (2) load_insn and load2 have a def-use dependence upon
323 the same insn 'insn1'.
325 From all these we can conclude that the two loads access memory
326 addresses that differ at most by a constant, and hence if moving
327 load_insn would cause an exception, it would have been caused by
328 load2 anyhow.
330 The function uses list (given by LAST_BLOCK) of already processed
331 blocks in EBB. The list is formed in `add_deps_for_risky_insns'. */
333 static basic_block
334 earliest_block_with_similiar_load (basic_block last_block, rtx load_insn)
336 sd_iterator_def back_sd_it;
337 dep_t back_dep;
338 basic_block bb, earliest_block = NULL;
340 FOR_EACH_DEP (load_insn, SD_LIST_BACK, back_sd_it, back_dep)
342 rtx_insn *insn1 = DEP_PRO (back_dep);
344 if (DEP_TYPE (back_dep) == REG_DEP_TRUE)
345 /* Found a DEF-USE dependence (insn1, load_insn). */
347 sd_iterator_def fore_sd_it;
348 dep_t fore_dep;
350 FOR_EACH_DEP (insn1, SD_LIST_FORW, fore_sd_it, fore_dep)
352 rtx_insn *insn2 = DEP_CON (fore_dep);
353 basic_block insn2_block = BLOCK_FOR_INSN (insn2);
355 if (DEP_TYPE (fore_dep) == REG_DEP_TRUE)
357 if (earliest_block != NULL
358 && earliest_block->index < insn2_block->index)
359 continue;
361 /* Found a DEF-USE dependence (insn1, insn2). */
362 if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
363 /* insn2 not guaranteed to be a 1 base reg load. */
364 continue;
366 for (bb = last_block; bb; bb = (basic_block) bb->aux)
367 if (insn2_block == bb)
368 break;
370 if (!bb)
371 /* insn2 is the similar load. */
372 earliest_block = insn2_block;
378 return earliest_block;
381 /* The following function adds dependencies between jumps and risky
382 insns in given ebb. */
384 static void
385 add_deps_for_risky_insns (rtx_insn *head, rtx_insn *tail)
387 rtx_insn *insn, *prev;
388 int classification;
389 rtx_insn *last_jump = NULL;
390 rtx_insn *next_tail = NEXT_INSN (tail);
391 basic_block last_block = NULL, bb;
393 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
395 add_delay_dependencies (insn);
396 if (control_flow_insn_p (insn))
398 bb = BLOCK_FOR_INSN (insn);
399 bb->aux = last_block;
400 last_block = bb;
401 /* Ensure blocks stay in the same order. */
402 if (last_jump)
403 add_dependence (insn, last_jump, REG_DEP_ANTI);
404 last_jump = insn;
406 else if (INSN_P (insn) && last_jump != NULL_RTX)
408 classification = haifa_classify_insn (insn);
409 prev = last_jump;
411 switch (classification)
413 case PFREE_CANDIDATE:
414 if (flag_schedule_speculative_load)
416 bb = earliest_block_with_similiar_load (last_block, insn);
417 if (bb)
419 bb = (basic_block) bb->aux;
420 if (!bb)
421 break;
422 prev = BB_END (bb);
425 /* Fall through. */
426 case TRAP_RISKY:
427 case IRISKY:
428 case PRISKY_CANDIDATE:
429 /* ??? We could implement better checking PRISKY_CANDIDATEs
430 analogous to sched-rgn.c. */
431 /* We can not change the mode of the backward
432 dependency because REG_DEP_ANTI has the lowest
433 rank. */
434 if (! sched_insns_conditions_mutex_p (insn, prev))
436 if ((current_sched_info->flags & DO_SPECULATION)
437 && (spec_info->mask & BEGIN_CONTROL))
439 dep_def _dep, *dep = &_dep;
441 init_dep (dep, prev, insn, REG_DEP_ANTI);
443 if (current_sched_info->flags & USE_DEPS_LIST)
445 DEP_STATUS (dep) = set_dep_weak (DEP_ANTI, BEGIN_CONTROL,
446 MAX_DEP_WEAK);
449 sd_add_or_update_dep (dep, false);
451 else
452 add_dependence (insn, prev, REG_DEP_CONTROL);
455 break;
457 default:
458 break;
462 /* Maintain the invariant that bb->aux is clear after use. */
463 while (last_block)
465 bb = (basic_block) last_block->aux;
466 last_block->aux = NULL;
467 last_block = bb;
471 /* Schedule a single extended basic block, defined by the boundaries
472 HEAD and TAIL.
474 We change our expectations about scheduler behaviour depending on
475 whether MODULO_SCHEDULING is true. If it is, we expect that the
476 caller has already called set_modulo_params and created delay pairs
477 as appropriate. If the modulo schedule failed, we return
478 NULL_RTX. */
480 basic_block
481 schedule_ebb (rtx_insn *head, rtx_insn *tail, bool modulo_scheduling)
483 basic_block first_bb, target_bb;
484 struct deps_desc tmp_deps;
485 bool success;
487 /* Blah. We should fix the rest of the code not to get confused by
488 a note or two. */
489 while (head != tail)
491 if (NOTE_P (head) || DEBUG_INSN_P (head))
492 head = NEXT_INSN (head);
493 else if (NOTE_P (tail) || DEBUG_INSN_P (tail))
494 tail = PREV_INSN (tail);
495 else if (LABEL_P (head))
496 head = NEXT_INSN (head);
497 else
498 break;
501 first_bb = BLOCK_FOR_INSN (head);
502 last_bb = BLOCK_FOR_INSN (tail);
504 if (no_real_insns_p (head, tail))
505 return BLOCK_FOR_INSN (tail);
507 gcc_assert (INSN_P (head) && INSN_P (tail));
509 if (!bitmap_bit_p (&dont_calc_deps, first_bb->index))
511 init_deps_global ();
513 /* Compute dependencies. */
514 init_deps (&tmp_deps, false);
515 sched_analyze (&tmp_deps, head, tail);
516 free_deps (&tmp_deps);
518 add_deps_for_risky_insns (head, tail);
520 if (targetm.sched.dependencies_evaluation_hook)
521 targetm.sched.dependencies_evaluation_hook (head, tail);
523 finish_deps_global ();
525 else
526 /* Only recovery blocks can have their dependencies already calculated,
527 and they always are single block ebbs. */
528 gcc_assert (first_bb == last_bb);
530 /* Set priorities. */
531 current_sched_info->sched_max_insns_priority = 0;
532 rgn_n_insns = set_priorities (head, tail);
533 current_sched_info->sched_max_insns_priority++;
535 current_sched_info->prev_head = PREV_INSN (head);
536 current_sched_info->next_tail = NEXT_INSN (tail);
538 remove_notes (head, tail);
540 unlink_bb_notes (first_bb, last_bb);
542 target_bb = first_bb;
544 /* Make ready list big enough to hold all the instructions from the ebb. */
545 sched_extend_ready_list (rgn_n_insns);
546 success = schedule_block (&target_bb, NULL);
547 gcc_assert (success || modulo_scheduling);
549 /* Free ready list. */
550 sched_finish_ready_list ();
552 /* We might pack all instructions into fewer blocks,
553 so we may made some of them empty. Can't assert (b == last_bb). */
555 /* Sanity check: verify that all region insns were scheduled. */
556 gcc_assert (modulo_scheduling || sched_rgn_n_insns == rgn_n_insns);
558 /* Free dependencies. */
559 sched_free_deps (current_sched_info->head, current_sched_info->tail, true);
561 gcc_assert (haifa_recovery_bb_ever_added_p
562 || deps_pools_are_empty_p ());
564 if (EDGE_COUNT (last_bb->preds) == 0)
565 /* LAST_BB is unreachable. */
567 gcc_assert (first_bb != last_bb
568 && EDGE_COUNT (last_bb->succs) == 0);
569 last_bb = last_bb->prev_bb;
570 delete_basic_block (last_bb->next_bb);
573 return success ? last_bb : NULL;
576 /* Perform initializations before running schedule_ebbs or a single
577 schedule_ebb. */
578 void
579 schedule_ebbs_init (void)
581 /* Setup infos. */
583 memcpy (&ebb_common_sched_info, &haifa_common_sched_info,
584 sizeof (ebb_common_sched_info));
586 ebb_common_sched_info.fix_recovery_cfg = ebb_fix_recovery_cfg;
587 ebb_common_sched_info.add_block = ebb_add_block;
588 ebb_common_sched_info.sched_pass_id = SCHED_EBB_PASS;
590 common_sched_info = &ebb_common_sched_info;
591 sched_deps_info = &ebb_sched_deps_info;
592 current_sched_info = &ebb_sched_info;
595 haifa_sched_init ();
597 compute_bb_for_insn ();
599 /* Initialize DONT_CALC_DEPS and ebb-{start, end} markers. */
600 bitmap_initialize (&dont_calc_deps, 0);
601 bitmap_clear (&dont_calc_deps);
604 /* Perform cleanups after scheduling using schedules_ebbs or schedule_ebb. */
605 void
606 schedule_ebbs_finish (void)
608 bitmap_clear (&dont_calc_deps);
610 /* Reposition the prologue and epilogue notes in case we moved the
611 prologue/epilogue insns. */
612 if (reload_completed)
613 reposition_prologue_and_epilogue_notes ();
615 haifa_sched_finish ();
618 /* The main entry point in this file. */
620 void
621 schedule_ebbs (void)
623 basic_block bb;
624 int probability_cutoff;
625 rtx_insn *tail;
627 /* Taking care of this degenerate case makes the rest of
628 this code simpler. */
629 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
630 return;
632 if (profile_info && flag_branch_probabilities)
633 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
634 else
635 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
636 probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;
638 schedule_ebbs_init ();
640 /* Schedule every region in the subroutine. */
641 FOR_EACH_BB_FN (bb, cfun)
643 rtx_insn *head = BB_HEAD (bb);
645 if (bb->flags & BB_DISABLE_SCHEDULE)
646 continue;
648 for (;;)
650 edge e;
651 tail = BB_END (bb);
652 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
653 || LABEL_P (BB_HEAD (bb->next_bb)))
654 break;
655 e = find_fallthru_edge (bb->succs);
656 if (! e)
657 break;
658 if (e->probability <= probability_cutoff)
659 break;
660 if (e->dest->flags & BB_DISABLE_SCHEDULE)
661 break;
662 bb = bb->next_bb;
665 bb = schedule_ebb (head, tail, false);
667 schedule_ebbs_finish ();
670 /* INSN has been added to/removed from current ebb. */
671 static void
672 ebb_add_remove_insn (rtx_insn *insn ATTRIBUTE_UNUSED, int remove_p)
674 if (!remove_p)
675 rgn_n_insns++;
676 else
677 rgn_n_insns--;
680 /* BB was added to ebb after AFTER. */
681 static void
682 ebb_add_block (basic_block bb, basic_block after)
684 /* Recovery blocks are always bounded by BARRIERS,
685 therefore, they always form single block EBB,
686 therefore, we can use rec->index to identify such EBBs. */
687 if (after == EXIT_BLOCK_PTR_FOR_FN (cfun))
688 bitmap_set_bit (&dont_calc_deps, bb->index);
689 else if (after == last_bb)
690 last_bb = bb;
693 /* Return next block in ebb chain. For parameter meaning please refer to
694 sched-int.h: struct sched_info: advance_target_bb. */
695 static basic_block
696 advance_target_bb (basic_block bb, rtx_insn *insn)
698 if (insn)
700 if (BLOCK_FOR_INSN (insn) != bb
701 && control_flow_insn_p (insn)
702 /* We handle interblock movement of the speculation check
703 or over a speculation check in
704 haifa-sched.c: move_block_after_check (). */
705 && !IS_SPECULATION_BRANCHY_CHECK_P (insn)
706 && !IS_SPECULATION_BRANCHY_CHECK_P (BB_END (bb)))
708 /* Assert that we don't move jumps across blocks. */
709 gcc_assert (!control_flow_insn_p (BB_END (bb))
710 && NOTE_INSN_BASIC_BLOCK_P (BB_HEAD (bb->next_bb)));
711 return bb;
713 else
714 return 0;
716 else
717 /* Return next non empty block. */
721 gcc_assert (bb != last_bb);
723 bb = bb->next_bb;
725 while (bb_note (bb) == BB_END (bb));
727 return bb;
731 /* Fix internal data after interblock movement of jump instruction.
732 For parameter meaning please refer to
733 sched-int.h: struct sched_info: fix_recovery_cfg. */
734 static void
735 ebb_fix_recovery_cfg (int bbi ATTRIBUTE_UNUSED, int jump_bbi,
736 int jump_bb_nexti)
738 gcc_assert (last_bb->index != bbi);
740 if (jump_bb_nexti == last_bb->index)
741 last_bb = BASIC_BLOCK_FOR_FN (cfun, jump_bbi);
744 #endif /* INSN_SCHEDULING */