Fix bootstrap/PR63632
[official-gcc.git] / gcc / sched-ebb.c
blobd30fa2e30a1644bcfd117b0e510e5eb8c51bf9f6
1 /* Instruction scheduling pass.
2 Copyright (C) 1992-2014 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 "hashtab.h"
32 #include "hash-set.h"
33 #include "vec.h"
34 #include "machmode.h"
35 #include "input.h"
36 #include "function.h"
37 #include "profile.h"
38 #include "flags.h"
39 #include "insn-config.h"
40 #include "insn-attr.h"
41 #include "except.h"
42 #include "recog.h"
43 #include "params.h"
44 #include "sched-int.h"
45 #include "target.h"
48 #ifdef INSN_SCHEDULING
50 /* The number of insns to be scheduled in total. */
51 static int rgn_n_insns;
53 /* The number of insns scheduled so far. */
54 static int sched_rgn_n_insns;
56 /* Set of blocks, that already have their dependencies calculated. */
57 static bitmap_head dont_calc_deps;
59 /* Last basic block in current ebb. */
60 static basic_block last_bb;
62 /* Implementations of the sched_info functions for region scheduling. */
63 static void init_ready_list (void);
64 static void begin_schedule_ready (rtx_insn *);
65 static int schedule_more_p (void);
66 static const char *ebb_print_insn (const rtx_insn *, int);
67 static int rank (rtx_insn *, rtx_insn *);
68 static int ebb_contributes_to_priority (rtx_insn *, rtx_insn *);
69 static basic_block earliest_block_with_similiar_load (basic_block, rtx);
70 static void add_deps_for_risky_insns (rtx_insn *, rtx_insn *);
71 static void debug_ebb_dependencies (rtx_insn *, rtx_insn *);
73 static void ebb_add_remove_insn (rtx_insn *, int);
74 static void ebb_add_block (basic_block, basic_block);
75 static basic_block advance_target_bb (basic_block, rtx_insn *);
76 static void ebb_fix_recovery_cfg (int, int, int);
78 /* Allocate memory and store the state of the frontend. Return the allocated
79 memory. */
80 static void *
81 save_ebb_state (void)
83 int *p = XNEW (int);
84 *p = sched_rgn_n_insns;
85 return p;
88 /* Restore the state of the frontend from P_, then free it. */
89 static void
90 restore_ebb_state (void *p_)
92 int *p = (int *)p_;
93 sched_rgn_n_insns = *p;
94 free (p_);
97 /* Return nonzero if there are more insns that should be scheduled. */
99 static int
100 schedule_more_p (void)
102 return sched_rgn_n_insns < rgn_n_insns;
105 /* Print dependency information about ebb between HEAD and TAIL. */
106 static void
107 debug_ebb_dependencies (rtx_insn *head, rtx_insn *tail)
109 fprintf (sched_dump,
110 ";; --------------- forward dependences: ------------ \n");
112 fprintf (sched_dump, "\n;; --- EBB Dependences --- from bb%d to bb%d \n",
113 BLOCK_NUM (head), BLOCK_NUM (tail));
115 debug_dependencies (head, tail);
118 /* Add all insns that are initially ready to the ready list READY. Called
119 once before scheduling a set of insns. */
121 static void
122 init_ready_list (void)
124 int n = 0;
125 rtx_insn *prev_head = current_sched_info->prev_head;
126 rtx_insn *next_tail = current_sched_info->next_tail;
127 rtx_insn *insn;
129 sched_rgn_n_insns = 0;
131 /* Print debugging information. */
132 if (sched_verbose >= 5)
133 debug_ebb_dependencies (NEXT_INSN (prev_head), PREV_INSN (next_tail));
135 /* Initialize ready list with all 'ready' insns in target block.
136 Count number of insns in the target block being scheduled. */
137 for (insn = NEXT_INSN (prev_head); insn != next_tail; insn = NEXT_INSN (insn))
139 try_ready (insn);
140 n++;
143 gcc_assert (n == rgn_n_insns);
146 /* INSN is being scheduled after LAST. Update counters. */
147 static void
148 begin_schedule_ready (rtx_insn *insn ATTRIBUTE_UNUSED)
150 sched_rgn_n_insns++;
153 /* INSN is being moved to its place in the schedule, after LAST. */
154 static void
155 begin_move_insn (rtx_insn *insn, rtx_insn *last)
157 if (BLOCK_FOR_INSN (insn) == last_bb
158 /* INSN is a jump in the last block, ... */
159 && control_flow_insn_p (insn)
160 /* that is going to be moved over some instructions. */
161 && last != PREV_INSN (insn))
163 edge e;
164 basic_block bb;
166 /* An obscure special case, where we do have partially dead
167 instruction scheduled after last control flow instruction.
168 In this case we can create new basic block. It is
169 always exactly one basic block last in the sequence. */
171 e = find_fallthru_edge (last_bb->succs);
173 gcc_checking_assert (!e || !(e->flags & EDGE_COMPLEX));
175 gcc_checking_assert (BLOCK_FOR_INSN (insn) == last_bb
176 && !IS_SPECULATION_CHECK_P (insn)
177 && BB_HEAD (last_bb) != insn
178 && BB_END (last_bb) == insn);
181 rtx x;
183 x = NEXT_INSN (insn);
184 if (e)
185 gcc_checking_assert (NOTE_P (x) || LABEL_P (x));
186 else
187 gcc_checking_assert (BARRIER_P (x));
190 if (e)
192 bb = split_edge (e);
193 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (BB_END (bb)));
195 else
197 /* Create an empty unreachable block after the INSN. */
198 rtx_insn *next = NEXT_INSN (insn);
199 if (next && BARRIER_P (next))
200 next = NEXT_INSN (next);
201 bb = create_basic_block (next, NULL_RTX, last_bb);
204 /* split_edge () creates BB before E->DEST. Keep in mind, that
205 this operation extends scheduling region till the end of BB.
206 Hence, we need to shift NEXT_TAIL, so haifa-sched.c won't go out
207 of the scheduling region. */
208 current_sched_info->next_tail = NEXT_INSN (BB_END (bb));
209 gcc_assert (current_sched_info->next_tail);
211 /* Append new basic block to the end of the ebb. */
212 sched_init_only_bb (bb, last_bb);
213 gcc_assert (last_bb == bb);
217 /* Return a string that contains the insn uid and optionally anything else
218 necessary to identify this insn in an output. It's valid to use a
219 static buffer for this. The ALIGNED parameter should cause the string
220 to be formatted so that multiple output lines will line up nicely. */
222 static const char *
223 ebb_print_insn (const rtx_insn *insn, int aligned ATTRIBUTE_UNUSED)
225 static char tmp[80];
227 /* '+' before insn means it is a new cycle start. */
228 if (GET_MODE (insn) == TImode)
229 sprintf (tmp, "+ %4d", INSN_UID (insn));
230 else
231 sprintf (tmp, " %4d", INSN_UID (insn));
233 return tmp;
236 /* Compare priority of two insns. Return a positive number if the second
237 insn is to be preferred for scheduling, and a negative one if the first
238 is to be preferred. Zero if they are equally good. */
240 static int
241 rank (rtx_insn *insn1, rtx_insn *insn2)
243 basic_block bb1 = BLOCK_FOR_INSN (insn1);
244 basic_block bb2 = BLOCK_FOR_INSN (insn2);
246 if (bb1->count > bb2->count
247 || bb1->frequency > bb2->frequency)
248 return -1;
249 if (bb1->count < bb2->count
250 || bb1->frequency < bb2->frequency)
251 return 1;
252 return 0;
255 /* NEXT is an instruction that depends on INSN (a backward dependence);
256 return nonzero if we should include this dependence in priority
257 calculations. */
259 static int
260 ebb_contributes_to_priority (rtx_insn *next ATTRIBUTE_UNUSED,
261 rtx_insn *insn ATTRIBUTE_UNUSED)
263 return 1;
266 /* INSN is a JUMP_INSN. Store the set of registers that
267 must be considered as used by this jump in USED. */
269 void
270 ebb_compute_jump_reg_dependencies (rtx insn, regset used)
272 basic_block b = BLOCK_FOR_INSN (insn);
273 edge e;
274 edge_iterator ei;
276 FOR_EACH_EDGE (e, ei, b->succs)
277 if ((e->flags & EDGE_FALLTHRU) == 0)
278 bitmap_ior_into (used, df_get_live_in (e->dest));
281 /* Used in schedule_insns to initialize current_sched_info for scheduling
282 regions (or single basic blocks). */
284 static struct common_sched_info_def ebb_common_sched_info;
286 static struct sched_deps_info_def ebb_sched_deps_info =
288 ebb_compute_jump_reg_dependencies,
289 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
290 NULL,
291 1, 0, 0
294 static struct haifa_sched_info ebb_sched_info =
296 init_ready_list,
297 NULL,
298 schedule_more_p,
299 NULL,
300 rank,
301 ebb_print_insn,
302 ebb_contributes_to_priority,
303 NULL, /* insn_finishes_block_p */
305 NULL, NULL,
306 NULL, NULL,
307 1, 0,
309 ebb_add_remove_insn,
310 begin_schedule_ready,
311 begin_move_insn,
312 advance_target_bb,
314 save_ebb_state,
315 restore_ebb_state,
317 SCHED_EBB
318 /* We can create new blocks in begin_schedule_ready (). */
319 | NEW_BBS
322 /* Returns the earliest block in EBB currently being processed where a
323 "similar load" 'insn2' is found, and hence LOAD_INSN can move
324 speculatively into the found block. All the following must hold:
326 (1) both loads have 1 base register (PFREE_CANDIDATEs).
327 (2) load_insn and load2 have a def-use dependence upon
328 the same insn 'insn1'.
330 From all these we can conclude that the two loads access memory
331 addresses that differ at most by a constant, and hence if moving
332 load_insn would cause an exception, it would have been caused by
333 load2 anyhow.
335 The function uses list (given by LAST_BLOCK) of already processed
336 blocks in EBB. The list is formed in `add_deps_for_risky_insns'. */
338 static basic_block
339 earliest_block_with_similiar_load (basic_block last_block, rtx load_insn)
341 sd_iterator_def back_sd_it;
342 dep_t back_dep;
343 basic_block bb, earliest_block = NULL;
345 FOR_EACH_DEP (load_insn, SD_LIST_BACK, back_sd_it, back_dep)
347 rtx_insn *insn1 = DEP_PRO (back_dep);
349 if (DEP_TYPE (back_dep) == REG_DEP_TRUE)
350 /* Found a DEF-USE dependence (insn1, load_insn). */
352 sd_iterator_def fore_sd_it;
353 dep_t fore_dep;
355 FOR_EACH_DEP (insn1, SD_LIST_FORW, fore_sd_it, fore_dep)
357 rtx_insn *insn2 = DEP_CON (fore_dep);
358 basic_block insn2_block = BLOCK_FOR_INSN (insn2);
360 if (DEP_TYPE (fore_dep) == REG_DEP_TRUE)
362 if (earliest_block != NULL
363 && earliest_block->index < insn2_block->index)
364 continue;
366 /* Found a DEF-USE dependence (insn1, insn2). */
367 if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
368 /* insn2 not guaranteed to be a 1 base reg load. */
369 continue;
371 for (bb = last_block; bb; bb = (basic_block) bb->aux)
372 if (insn2_block == bb)
373 break;
375 if (!bb)
376 /* insn2 is the similar load. */
377 earliest_block = insn2_block;
383 return earliest_block;
386 /* The following function adds dependencies between jumps and risky
387 insns in given ebb. */
389 static void
390 add_deps_for_risky_insns (rtx_insn *head, rtx_insn *tail)
392 rtx_insn *insn, *prev;
393 int classification;
394 rtx_insn *last_jump = NULL;
395 rtx_insn *next_tail = NEXT_INSN (tail);
396 basic_block last_block = NULL, bb;
398 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
400 add_delay_dependencies (insn);
401 if (control_flow_insn_p (insn))
403 bb = BLOCK_FOR_INSN (insn);
404 bb->aux = last_block;
405 last_block = bb;
406 /* Ensure blocks stay in the same order. */
407 if (last_jump)
408 add_dependence (insn, last_jump, REG_DEP_ANTI);
409 last_jump = insn;
411 else if (INSN_P (insn) && last_jump != NULL_RTX)
413 classification = haifa_classify_insn (insn);
414 prev = last_jump;
416 switch (classification)
418 case PFREE_CANDIDATE:
419 if (flag_schedule_speculative_load)
421 bb = earliest_block_with_similiar_load (last_block, insn);
422 if (bb)
424 bb = (basic_block) bb->aux;
425 if (!bb)
426 break;
427 prev = BB_END (bb);
430 /* Fall through. */
431 case TRAP_RISKY:
432 case IRISKY:
433 case PRISKY_CANDIDATE:
434 /* ??? We could implement better checking PRISKY_CANDIDATEs
435 analogous to sched-rgn.c. */
436 /* We can not change the mode of the backward
437 dependency because REG_DEP_ANTI has the lowest
438 rank. */
439 if (! sched_insns_conditions_mutex_p (insn, prev))
441 if ((current_sched_info->flags & DO_SPECULATION)
442 && (spec_info->mask & BEGIN_CONTROL))
444 dep_def _dep, *dep = &_dep;
446 init_dep (dep, prev, insn, REG_DEP_ANTI);
448 if (current_sched_info->flags & USE_DEPS_LIST)
450 DEP_STATUS (dep) = set_dep_weak (DEP_ANTI, BEGIN_CONTROL,
451 MAX_DEP_WEAK);
454 sd_add_or_update_dep (dep, false);
456 else
457 add_dependence (insn, prev, REG_DEP_CONTROL);
460 break;
462 default:
463 break;
467 /* Maintain the invariant that bb->aux is clear after use. */
468 while (last_block)
470 bb = (basic_block) last_block->aux;
471 last_block->aux = NULL;
472 last_block = bb;
476 /* Schedule a single extended basic block, defined by the boundaries
477 HEAD and TAIL.
479 We change our expectations about scheduler behaviour depending on
480 whether MODULO_SCHEDULING is true. If it is, we expect that the
481 caller has already called set_modulo_params and created delay pairs
482 as appropriate. If the modulo schedule failed, we return
483 NULL_RTX. */
485 basic_block
486 schedule_ebb (rtx_insn *head, rtx_insn *tail, bool modulo_scheduling)
488 basic_block first_bb, target_bb;
489 struct deps_desc tmp_deps;
490 bool success;
492 /* Blah. We should fix the rest of the code not to get confused by
493 a note or two. */
494 while (head != tail)
496 if (NOTE_P (head) || DEBUG_INSN_P (head))
497 head = NEXT_INSN (head);
498 else if (NOTE_P (tail) || DEBUG_INSN_P (tail))
499 tail = PREV_INSN (tail);
500 else if (LABEL_P (head))
501 head = NEXT_INSN (head);
502 else
503 break;
506 first_bb = BLOCK_FOR_INSN (head);
507 last_bb = BLOCK_FOR_INSN (tail);
509 if (no_real_insns_p (head, tail))
510 return BLOCK_FOR_INSN (tail);
512 gcc_assert (INSN_P (head) && INSN_P (tail));
514 if (!bitmap_bit_p (&dont_calc_deps, first_bb->index))
516 init_deps_global ();
518 /* Compute dependencies. */
519 init_deps (&tmp_deps, false);
520 sched_analyze (&tmp_deps, head, tail);
521 free_deps (&tmp_deps);
523 add_deps_for_risky_insns (head, tail);
525 if (targetm.sched.dependencies_evaluation_hook)
526 targetm.sched.dependencies_evaluation_hook (head, tail);
528 finish_deps_global ();
530 else
531 /* Only recovery blocks can have their dependencies already calculated,
532 and they always are single block ebbs. */
533 gcc_assert (first_bb == last_bb);
535 /* Set priorities. */
536 current_sched_info->sched_max_insns_priority = 0;
537 rgn_n_insns = set_priorities (head, tail);
538 current_sched_info->sched_max_insns_priority++;
540 current_sched_info->prev_head = PREV_INSN (head);
541 current_sched_info->next_tail = NEXT_INSN (tail);
543 remove_notes (head, tail);
545 unlink_bb_notes (first_bb, last_bb);
547 target_bb = first_bb;
549 /* Make ready list big enough to hold all the instructions from the ebb. */
550 sched_extend_ready_list (rgn_n_insns);
551 success = schedule_block (&target_bb, NULL);
552 gcc_assert (success || modulo_scheduling);
554 /* Free ready list. */
555 sched_finish_ready_list ();
557 /* We might pack all instructions into fewer blocks,
558 so we may made some of them empty. Can't assert (b == last_bb). */
560 /* Sanity check: verify that all region insns were scheduled. */
561 gcc_assert (modulo_scheduling || sched_rgn_n_insns == rgn_n_insns);
563 /* Free dependencies. */
564 sched_free_deps (current_sched_info->head, current_sched_info->tail, true);
566 gcc_assert (haifa_recovery_bb_ever_added_p
567 || deps_pools_are_empty_p ());
569 if (EDGE_COUNT (last_bb->preds) == 0)
570 /* LAST_BB is unreachable. */
572 gcc_assert (first_bb != last_bb
573 && EDGE_COUNT (last_bb->succs) == 0);
574 last_bb = last_bb->prev_bb;
575 delete_basic_block (last_bb->next_bb);
578 return success ? last_bb : NULL;
581 /* Perform initializations before running schedule_ebbs or a single
582 schedule_ebb. */
583 void
584 schedule_ebbs_init (void)
586 /* Setup infos. */
588 memcpy (&ebb_common_sched_info, &haifa_common_sched_info,
589 sizeof (ebb_common_sched_info));
591 ebb_common_sched_info.fix_recovery_cfg = ebb_fix_recovery_cfg;
592 ebb_common_sched_info.add_block = ebb_add_block;
593 ebb_common_sched_info.sched_pass_id = SCHED_EBB_PASS;
595 common_sched_info = &ebb_common_sched_info;
596 sched_deps_info = &ebb_sched_deps_info;
597 current_sched_info = &ebb_sched_info;
600 haifa_sched_init ();
602 compute_bb_for_insn ();
604 /* Initialize DONT_CALC_DEPS and ebb-{start, end} markers. */
605 bitmap_initialize (&dont_calc_deps, 0);
606 bitmap_clear (&dont_calc_deps);
609 /* Perform cleanups after scheduling using schedules_ebbs or schedule_ebb. */
610 void
611 schedule_ebbs_finish (void)
613 bitmap_clear (&dont_calc_deps);
615 /* Reposition the prologue and epilogue notes in case we moved the
616 prologue/epilogue insns. */
617 if (reload_completed)
618 reposition_prologue_and_epilogue_notes ();
620 haifa_sched_finish ();
623 /* The main entry point in this file. */
625 void
626 schedule_ebbs (void)
628 basic_block bb;
629 int probability_cutoff;
630 rtx_insn *tail;
632 /* Taking care of this degenerate case makes the rest of
633 this code simpler. */
634 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
635 return;
637 if (profile_info && flag_branch_probabilities)
638 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
639 else
640 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
641 probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;
643 schedule_ebbs_init ();
645 /* Schedule every region in the subroutine. */
646 FOR_EACH_BB_FN (bb, cfun)
648 rtx_insn *head = BB_HEAD (bb);
650 if (bb->flags & BB_DISABLE_SCHEDULE)
651 continue;
653 for (;;)
655 edge e;
656 tail = BB_END (bb);
657 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
658 || LABEL_P (BB_HEAD (bb->next_bb)))
659 break;
660 e = find_fallthru_edge (bb->succs);
661 if (! e)
662 break;
663 if (e->probability <= probability_cutoff)
664 break;
665 if (e->dest->flags & BB_DISABLE_SCHEDULE)
666 break;
667 bb = bb->next_bb;
670 bb = schedule_ebb (head, tail, false);
672 schedule_ebbs_finish ();
675 /* INSN has been added to/removed from current ebb. */
676 static void
677 ebb_add_remove_insn (rtx_insn *insn ATTRIBUTE_UNUSED, int remove_p)
679 if (!remove_p)
680 rgn_n_insns++;
681 else
682 rgn_n_insns--;
685 /* BB was added to ebb after AFTER. */
686 static void
687 ebb_add_block (basic_block bb, basic_block after)
689 /* Recovery blocks are always bounded by BARRIERS,
690 therefore, they always form single block EBB,
691 therefore, we can use rec->index to identify such EBBs. */
692 if (after == EXIT_BLOCK_PTR_FOR_FN (cfun))
693 bitmap_set_bit (&dont_calc_deps, bb->index);
694 else if (after == last_bb)
695 last_bb = bb;
698 /* Return next block in ebb chain. For parameter meaning please refer to
699 sched-int.h: struct sched_info: advance_target_bb. */
700 static basic_block
701 advance_target_bb (basic_block bb, rtx_insn *insn)
703 if (insn)
705 if (BLOCK_FOR_INSN (insn) != bb
706 && control_flow_insn_p (insn)
707 /* We handle interblock movement of the speculation check
708 or over a speculation check in
709 haifa-sched.c: move_block_after_check (). */
710 && !IS_SPECULATION_BRANCHY_CHECK_P (insn)
711 && !IS_SPECULATION_BRANCHY_CHECK_P (BB_END (bb)))
713 /* Assert that we don't move jumps across blocks. */
714 gcc_assert (!control_flow_insn_p (BB_END (bb))
715 && NOTE_INSN_BASIC_BLOCK_P (BB_HEAD (bb->next_bb)));
716 return bb;
718 else
719 return 0;
721 else
722 /* Return next non empty block. */
726 gcc_assert (bb != last_bb);
728 bb = bb->next_bb;
730 while (bb_note (bb) == BB_END (bb));
732 return bb;
736 /* Fix internal data after interblock movement of jump instruction.
737 For parameter meaning please refer to
738 sched-int.h: struct sched_info: fix_recovery_cfg. */
739 static void
740 ebb_fix_recovery_cfg (int bbi ATTRIBUTE_UNUSED, int jump_bbi,
741 int jump_bb_nexti)
743 gcc_assert (last_bb->index != bbi);
745 if (jump_bb_nexti == last_bb->index)
746 last_bb = BASIC_BLOCK_FOR_FN (cfun, jump_bbi);
749 #endif /* INSN_SCHEDULING */