]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/sched-ebb.c
* MAINTAINERS: Update my email address.
[thirdparty/gcc.git] / gcc / sched-ebb.c
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)
7
8 This file is part of GCC.
9
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.
14
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.
19
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/>. */
23 \f
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"
45
46 \f
47 #ifdef INSN_SCHEDULING
48
49 /* The number of insns scheduled so far. */
50 static int sched_n_insns;
51
52 /* The number of insns to be scheduled in total. */
53 static int n_insns;
54
55 /* Set of blocks, that already have their dependencies calculated. */
56 static bitmap_head dont_calc_deps;
57
58 /* Last basic block in current ebb. */
59 static basic_block last_bb;
60
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);
72
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);
77
78 /* Return nonzero if there are more insns that should be scheduled. */
79
80 static int
81 schedule_more_p (void)
82 {
83 return sched_n_insns < n_insns;
84 }
85
86 /* Print dependency information about ebb between HEAD and TAIL. */
87 static void
88 debug_ebb_dependencies (rtx head, rtx tail)
89 {
90 fprintf (sched_dump,
91 ";; --------------- forward dependences: ------------ \n");
92
93 fprintf (sched_dump, "\n;; --- EBB Dependences --- from bb%d to bb%d \n",
94 BLOCK_NUM (head), BLOCK_NUM (tail));
95
96 debug_dependencies (head, tail);
97 }
98
99 /* Add all insns that are initially ready to the ready list READY. Called
100 once before scheduling a set of insns. */
101
102 static void
103 init_ready_list (void)
104 {
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;
109
110 sched_n_insns = 0;
111
112 /* Print debugging information. */
113 if (sched_verbose >= 5)
114 debug_ebb_dependencies (NEXT_INSN (prev_head), PREV_INSN (next_tail));
115
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))
119 {
120 try_ready (insn);
121 n++;
122 }
123
124 gcc_assert (n == n_insns);
125 }
126
127 /* INSN is being scheduled after LAST. Update counters. */
128 static void
129 begin_schedule_ready (rtx insn, rtx last)
130 {
131 sched_n_insns++;
132
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))
138 {
139 edge e;
140 edge_iterator ei;
141 basic_block bb;
142
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. */
147
148 FOR_EACH_EDGE (e, ei, last_bb->succs)
149 if (e->flags & EDGE_FALLTHRU)
150 break;
151
152 #ifdef ENABLE_CHECKING
153 gcc_assert (!e || !(e->flags & EDGE_COMPLEX));
154
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);
159
160 {
161 rtx x;
162
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));
168 }
169 #endif
170
171 if (e)
172 {
173 bb = split_edge (e);
174 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (BB_END (bb)));
175 }
176 else
177 /* Create an empty unreachable block after the INSN. */
178 bb = create_basic_block (NEXT_INSN (insn), NULL_RTX, last_bb);
179
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);
186
187 add_block (bb, last_bb);
188 gcc_assert (last_bb == bb);
189 }
190 }
191
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. */
196
197 static const char *
198 ebb_print_insn (rtx insn, int aligned ATTRIBUTE_UNUSED)
199 {
200 static char tmp[80];
201
202 sprintf (tmp, "%4d", INSN_UID (insn));
203 return tmp;
204 }
205
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. */
209
210 static int
211 rank (rtx insn1, rtx insn2)
212 {
213 basic_block bb1 = BLOCK_FOR_INSN (insn1);
214 basic_block bb2 = BLOCK_FOR_INSN (insn2);
215
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;
223 }
224
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. */
228
229 static int
230 contributes_to_priority (rtx next ATTRIBUTE_UNUSED,
231 rtx insn ATTRIBUTE_UNUSED)
232 {
233 return 1;
234 }
235
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. */
240
241 static void
242 compute_jump_reg_dependencies (rtx insn, regset cond_set, regset used,
243 regset set)
244 {
245 basic_block b = BLOCK_FOR_INSN (insn);
246 edge e;
247 edge_iterator ei;
248
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));
259 }
260
261 /* Used in schedule_insns to initialize current_sched_info for scheduling
262 regions (or single basic blocks). */
263
264 static struct sched_info ebb_sched_info =
265 {
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,
274
275 NULL, NULL,
276 NULL, NULL,
277 0, 1, 0,
278
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
287 };
288 \f
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:
292
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'.
296
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.
301
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'. */
304
305 static basic_block
306 earliest_block_with_similiar_load (basic_block last_block, rtx load_insn)
307 {
308 sd_iterator_def back_sd_it;
309 dep_t back_dep;
310 basic_block bb, earliest_block = NULL;
311
312 FOR_EACH_DEP (load_insn, SD_LIST_BACK, back_sd_it, back_dep)
313 {
314 rtx insn1 = DEP_PRO (back_dep);
315
316 if (DEP_TYPE (back_dep) == REG_DEP_TRUE)
317 /* Found a DEF-USE dependence (insn1, load_insn). */
318 {
319 sd_iterator_def fore_sd_it;
320 dep_t fore_dep;
321
322 FOR_EACH_DEP (insn1, SD_LIST_FORW, fore_sd_it, fore_dep)
323 {
324 rtx insn2 = DEP_CON (fore_dep);
325 basic_block insn2_block = BLOCK_FOR_INSN (insn2);
326
327 if (DEP_TYPE (fore_dep) == REG_DEP_TRUE)
328 {
329 if (earliest_block != NULL
330 && earliest_block->index < insn2_block->index)
331 continue;
332
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;
337
338 for (bb = last_block; bb; bb = bb->aux)
339 if (insn2_block == bb)
340 break;
341
342 if (!bb)
343 /* insn2 is the similar load. */
344 earliest_block = insn2_block;
345 }
346 }
347 }
348 }
349
350 return earliest_block;
351 }
352
353 /* The following function adds dependencies between jumps and risky
354 insns in given ebb. */
355
356 static void
357 add_deps_for_risky_insns (rtx head, rtx tail)
358 {
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;
364
365 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
366 if (control_flow_insn_p (insn))
367 {
368 bb = BLOCK_FOR_INSN (insn);
369 bb->aux = last_block;
370 last_block = bb;
371 last_jump = insn;
372 }
373 else if (INSN_P (insn) && last_jump != NULL_RTX)
374 {
375 class = haifa_classify_insn (insn);
376 prev = last_jump;
377 switch (class)
378 {
379 case PFREE_CANDIDATE:
380 if (flag_schedule_speculative_load)
381 {
382 bb = earliest_block_with_similiar_load (last_block, insn);
383 if (bb)
384 {
385 bb = bb->aux;
386 if (!bb)
387 break;
388 prev = BB_END (bb);
389 }
390 }
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))
401 {
402 dep_def _dep, *dep = &_dep;
403
404 init_dep (dep, prev, insn, REG_DEP_ANTI);
405
406 if (!(current_sched_info->flags & USE_DEPS_LIST))
407 {
408 enum DEPS_ADJUST_RESULT res;
409
410 res = sd_add_or_update_dep (dep, false);
411
412 /* We can't change an existing dependency with
413 DEP_ANTI. */
414 gcc_assert (res != DEP_CHANGED);
415 }
416 else
417 {
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);
422
423 sd_add_or_update_dep (dep, false);
424
425 /* Dep_status could have been changed.
426 No assertion here. */
427 }
428 }
429
430 break;
431
432 default:
433 break;
434 }
435 }
436 /* Maintain the invariant that bb->aux is clear after use. */
437 while (last_block)
438 {
439 bb = last_block->aux;
440 last_block->aux = NULL;
441 last_block = bb;
442 }
443 }
444
445 /* Schedule a single extended basic block, defined by the boundaries HEAD
446 and TAIL. */
447
448 static basic_block
449 schedule_ebb (rtx head, rtx tail)
450 {
451 basic_block first_bb, target_bb;
452 struct deps tmp_deps;
453
454 first_bb = BLOCK_FOR_INSN (head);
455 last_bb = BLOCK_FOR_INSN (tail);
456
457 if (no_real_insns_p (head, tail))
458 return BLOCK_FOR_INSN (tail);
459
460 gcc_assert (INSN_P (head) && INSN_P (tail));
461
462 if (!bitmap_bit_p (&dont_calc_deps, first_bb->index))
463 {
464 init_deps_global ();
465
466 /* Compute dependencies. */
467 init_deps (&tmp_deps);
468 sched_analyze (&tmp_deps, head, tail);
469 free_deps (&tmp_deps);
470
471 add_deps_for_risky_insns (head, tail);
472
473 if (targetm.sched.dependencies_evaluation_hook)
474 targetm.sched.dependencies_evaluation_hook (head, tail);
475
476 finish_deps_global ();
477 }
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);
482
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++;
487
488 current_sched_info->prev_head = PREV_INSN (head);
489 current_sched_info->next_tail = NEXT_INSN (tail);
490
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))
498 {
499 rtx note;
500
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);
504 }
505
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);
510
511 unlink_bb_notes (first_bb, last_bb);
512
513 current_sched_info->queue_must_finish_empty = 1;
514
515 target_bb = first_bb;
516 schedule_block (&target_bb, n_insns);
517
518 /* We might pack all instructions into fewer blocks,
519 so we may made some of them empty. Can't assert (b == last_bb). */
520
521 /* Sanity check: verify that all region insns were scheduled. */
522 gcc_assert (sched_n_insns == n_insns);
523
524 /* Free dependencies. */
525 sched_free_deps (current_sched_info->head, current_sched_info->tail, true);
526
527 gcc_assert (haifa_recovery_bb_ever_added_p
528 || deps_pools_are_empty_p ());
529
530 if (EDGE_COUNT (last_bb->preds) == 0)
531 /* LAST_BB is unreachable. */
532 {
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);
537 }
538
539 return last_bb;
540 }
541
542 /* The one entry point in this file. */
543
544 void
545 schedule_ebbs (void)
546 {
547 basic_block bb;
548 int probability_cutoff;
549 rtx tail;
550
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;
556
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;
561
562 /* We need current_sched_info in init_dependency_caches, which is
563 invoked via sched_init. */
564 current_sched_info = &ebb_sched_info;
565
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 ();
572
573 compute_bb_for_insn ();
574
575 /* Initialize DONT_CALC_DEPS and ebb-{start, end} markers. */
576 bitmap_initialize (&dont_calc_deps, 0);
577 bitmap_clear (&dont_calc_deps);
578
579 /* Schedule every region in the subroutine. */
580 FOR_EACH_BB (bb)
581 {
582 rtx head = BB_HEAD (bb);
583
584 for (;;)
585 {
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;
600 }
601
602 /* Blah. We should fix the rest of the code not to get confused by
603 a note or two. */
604 while (head != tail)
605 {
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;
614 }
615
616 bb = schedule_ebb (head, tail);
617 }
618 bitmap_clear (&dont_calc_deps);
619
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 ();
624
625 sched_finish ();
626 regstat_free_calls_crossed ();
627 }
628
629 /* INSN has been added to/removed from current ebb. */
630 static void
631 add_remove_insn (rtx insn ATTRIBUTE_UNUSED, int remove_p)
632 {
633 if (!remove_p)
634 n_insns++;
635 else
636 n_insns--;
637 }
638
639 /* BB was added to ebb after AFTER. */
640 static void
641 add_block1 (basic_block bb, basic_block after)
642 {
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;
650 }
651
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)
656 {
657 if (insn)
658 {
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)))
666 {
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;
671 }
672 else
673 return 0;
674 }
675 else
676 /* Return next non empty block. */
677 {
678 do
679 {
680 gcc_assert (bb != last_bb);
681
682 bb = bb->next_bb;
683 }
684 while (bb_note (bb) == BB_END (bb));
685
686 return bb;
687 }
688 }
689
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)
695 {
696 gcc_assert (last_bb->index != bbi);
697
698 if (jump_bb_nexti == last_bb->index)
699 last_bb = BASIC_BLOCK (jump_bbi);
700 }
701
702 #endif /* INSN_SCHEDULING */