1 /* Instruction scheduling pass.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
5 and currently maintained by, Jim Wilson (wilson@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #include "coretypes.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
36 #include "insn-config.h"
37 #include "insn-attr.h"
41 #include "cfglayout.h"
43 #include "sched-int.h"
46 /* The number of insns to be scheduled in total. */
47 static int target_n_insns
;
48 /* The number of insns scheduled so far. */
49 static int sched_n_insns
;
51 /* Implementations of the sched_info functions for region scheduling. */
52 static void init_ready_list (struct ready_list
*);
53 static int can_schedule_ready_p (rtx
);
54 static int new_ready (rtx
);
55 static int schedule_more_p (void);
56 static const char *ebb_print_insn (rtx
, int);
57 static int rank (rtx
, rtx
);
58 static int contributes_to_priority (rtx
, rtx
);
59 static void compute_jump_reg_dependencies (rtx
, regset
, regset
, regset
);
60 static basic_block
earliest_block_with_similiar_load (basic_block
, rtx
);
61 static void add_deps_for_risky_insns (rtx
, rtx
);
62 static basic_block
schedule_ebb (rtx
, rtx
);
63 static basic_block
fix_basic_block_boundaries (basic_block
, basic_block
, rtx
,
65 static void add_missing_bbs (rtx
, basic_block
, basic_block
);
67 /* Return nonzero if there are more insns that should be scheduled. */
70 schedule_more_p (void)
72 return sched_n_insns
< target_n_insns
;
75 /* Add all insns that are initially ready to the ready list READY. Called
76 once before scheduling a set of insns. */
79 init_ready_list (struct ready_list
*ready
)
81 rtx prev_head
= current_sched_info
->prev_head
;
82 rtx next_tail
= current_sched_info
->next_tail
;
89 /* Print debugging information. */
90 if (sched_verbose
>= 5)
91 debug_dependencies ();
94 /* Initialize ready list with all 'ready' insns in target block.
95 Count number of insns in the target block being scheduled. */
96 for (insn
= NEXT_INSN (prev_head
); insn
!= next_tail
; insn
= NEXT_INSN (insn
))
98 if (INSN_DEP_COUNT (insn
) == 0)
99 ready_add (ready
, insn
);
104 /* Called after taking INSN from the ready list. Returns nonzero if this
105 insn can be scheduled, nonzero if we should silently discard it. */
108 can_schedule_ready_p (rtx insn ATTRIBUTE_UNUSED
)
114 /* Called after INSN has all its dependencies resolved. Return nonzero
115 if it should be moved to the ready list or the queue, or zero if we
116 should silently discard it. */
118 new_ready (rtx next ATTRIBUTE_UNUSED
)
123 /* Return a string that contains the insn uid and optionally anything else
124 necessary to identify this insn in an output. It's valid to use a
125 static buffer for this. The ALIGNED parameter should cause the string
126 to be formatted so that multiple output lines will line up nicely. */
129 ebb_print_insn (rtx insn
, int aligned ATTRIBUTE_UNUSED
)
133 sprintf (tmp
, "%4d", INSN_UID (insn
));
137 /* Compare priority of two insns. Return a positive number if the second
138 insn is to be preferred for scheduling, and a negative one if the first
139 is to be preferred. Zero if they are equally good. */
142 rank (rtx insn1
, rtx insn2
)
144 basic_block bb1
= BLOCK_FOR_INSN (insn1
);
145 basic_block bb2
= BLOCK_FOR_INSN (insn2
);
147 if (bb1
->count
> bb2
->count
148 || bb1
->frequency
> bb2
->frequency
)
150 if (bb1
->count
< bb2
->count
151 || bb1
->frequency
< bb2
->frequency
)
156 /* NEXT is an instruction that depends on INSN (a backward dependence);
157 return nonzero if we should include this dependence in priority
161 contributes_to_priority (rtx next ATTRIBUTE_UNUSED
,
162 rtx insn ATTRIBUTE_UNUSED
)
167 /* INSN is a JUMP_INSN, COND_SET is the set of registers that are
168 conditionally set before INSN. Store the set of registers that
169 must be considered as used by this jump in USED and that of
170 registers that must be considered as set in SET. */
173 compute_jump_reg_dependencies (rtx insn
, regset cond_set
, regset used
,
176 basic_block b
= BLOCK_FOR_INSN (insn
);
180 FOR_EACH_EDGE (e
, ei
, b
->succs
)
182 if (e
->flags
& EDGE_FALLTHRU
)
183 /* The jump may be a by-product of a branch that has been merged
184 in the main codepath after being conditionalized. Therefore
185 it may guard the fallthrough block from using a value that has
186 conditionally overwritten that of the main codepath. So we
187 consider that it restores the value of the main codepath. */
188 bitmap_operation (set
, e
->dest
->global_live_at_start
, cond_set
,
191 bitmap_operation (used
, used
, e
->dest
->global_live_at_start
,
196 /* Used in schedule_insns to initialize current_sched_info for scheduling
197 regions (or single basic blocks). */
199 static struct sched_info ebb_sched_info
=
202 can_schedule_ready_p
,
207 contributes_to_priority
,
208 compute_jump_reg_dependencies
,
215 /* It is possible that ebb scheduling eliminated some blocks.
216 Place blocks from FIRST to LAST before BEFORE. */
219 add_missing_bbs (rtx before
, basic_block first
, basic_block last
)
221 for (; last
!= first
->prev_bb
; last
= last
->prev_bb
)
223 before
= emit_note_before (NOTE_INSN_BASIC_BLOCK
, before
);
224 NOTE_BASIC_BLOCK (before
) = last
;
225 BB_HEAD (last
) = before
;
226 BB_END (last
) = before
;
227 update_bb_for_insn (last
);
231 /* Fixup the CFG after EBB scheduling. Re-recognize the basic
232 block boundaries in between HEAD and TAIL and update basic block
233 structures between BB and LAST. */
236 fix_basic_block_boundaries (basic_block bb
, basic_block last
, rtx head
,
240 rtx last_inside
= BB_HEAD (bb
);
241 rtx aftertail
= NEXT_INSN (tail
);
245 for (; insn
!= aftertail
; insn
= NEXT_INSN (insn
))
247 gcc_assert (!LABEL_P (insn
));
248 /* Create new basic blocks just before first insn. */
249 if (inside_basic_block_p (insn
))
255 /* Re-emit the basic block note for newly found BB header. */
258 note
= emit_note_after (NOTE_INSN_BASIC_BLOCK
, insn
);
264 note
= emit_note_before (NOTE_INSN_BASIC_BLOCK
, insn
);
272 /* Control flow instruction terminate basic block. It is possible
273 that we've eliminated some basic blocks (made them empty).
274 Find the proper basic block using BLOCK_FOR_INSN and arrange things in
275 a sensible way by inserting empty basic blocks as needed. */
276 if (control_flow_insn_p (insn
) || (insn
== tail
&& last_inside
))
278 basic_block curr_bb
= BLOCK_FOR_INSN (insn
);
281 if (!control_flow_insn_p (insn
))
283 if (bb
== last
->next_bb
)
289 /* An obscure special case, where we do have partially dead
290 instruction scheduled after last control flow instruction.
291 In this case we can create new basic block. It is
292 always exactly one basic block last in the sequence. Handle
293 it by splitting the edge and repositioning the block.
294 This is somewhat hackish, but at least avoid cut&paste
296 A safer solution can be to bring the code into sequence,
297 do the split and re-emit it back in case this will ever
300 FOR_EACH_EDGE (f
, ei
, bb
->prev_bb
->succs
)
302 if (f
->flags
& EDGE_FALLTHRU
)
308 last
= curr_bb
= split_edge (f
);
309 h
= BB_HEAD (curr_bb
);
310 BB_HEAD (curr_bb
) = head
;
311 BB_END (curr_bb
) = insn
;
312 /* Edge splitting created misplaced BASIC_BLOCK note, kill
316 /* It may happen that code got moved past unconditional jump in
317 case the code is completely dead. Kill it. */
320 rtx next
= next_nonnote_insn (insn
);
321 delete_insn_chain (head
, insn
);
322 /* We keep some notes in the way that may split barrier from the
324 if (BARRIER_P (next
))
326 emit_barrier_after (prev_nonnote_insn (head
));
334 BB_HEAD (curr_bb
) = head
;
335 BB_END (curr_bb
) = insn
;
336 add_missing_bbs (BB_HEAD (curr_bb
), bb
, curr_bb
->prev_bb
);
338 note
= LABEL_P (head
) ? NEXT_INSN (head
) : head
;
339 NOTE_BASIC_BLOCK (note
) = curr_bb
;
340 update_bb_for_insn (curr_bb
);
341 bb
= curr_bb
->next_bb
;
347 add_missing_bbs (BB_HEAD (last
->next_bb
), bb
, last
);
351 /* Returns the earliest block in EBB currently being processed where a
352 "similar load" 'insn2' is found, and hence LOAD_INSN can move
353 speculatively into the found block. All the following must hold:
355 (1) both loads have 1 base register (PFREE_CANDIDATEs).
356 (2) load_insn and load2 have a def-use dependence upon
357 the same insn 'insn1'.
359 From all these we can conclude that the two loads access memory
360 addresses that differ at most by a constant, and hence if moving
361 load_insn would cause an exception, it would have been caused by
364 The function uses list (given by LAST_BLOCK) of already processed
365 blocks in EBB. The list is formed in `add_deps_for_risky_insns'. */
368 earliest_block_with_similiar_load (basic_block last_block
, rtx load_insn
)
371 basic_block bb
, earliest_block
= NULL
;
373 for (back_link
= LOG_LINKS (load_insn
);
375 back_link
= XEXP (back_link
, 1))
377 rtx insn1
= XEXP (back_link
, 0);
379 if (GET_MODE (back_link
) == VOIDmode
)
381 /* Found a DEF-USE dependence (insn1, load_insn). */
384 for (fore_link
= INSN_DEPEND (insn1
);
386 fore_link
= XEXP (fore_link
, 1))
388 rtx insn2
= XEXP (fore_link
, 0);
389 basic_block insn2_block
= BLOCK_FOR_INSN (insn2
);
391 if (GET_MODE (fore_link
) == VOIDmode
)
393 if (earliest_block
!= NULL
394 && earliest_block
->index
< insn2_block
->index
)
397 /* Found a DEF-USE dependence (insn1, insn2). */
398 if (haifa_classify_insn (insn2
) != PFREE_CANDIDATE
)
399 /* insn2 not guaranteed to be a 1 base reg load. */
402 for (bb
= last_block
; bb
; bb
= bb
->aux
)
403 if (insn2_block
== bb
)
407 /* insn2 is the similar load. */
408 earliest_block
= insn2_block
;
414 return earliest_block
;
417 /* The following function adds dependencies between jumps and risky
418 insns in given ebb. */
421 add_deps_for_risky_insns (rtx head
, rtx tail
)
425 rtx last_jump
= NULL_RTX
;
426 rtx next_tail
= NEXT_INSN (tail
);
427 basic_block last_block
= NULL
, bb
;
429 for (insn
= head
; insn
!= next_tail
; insn
= NEXT_INSN (insn
))
432 bb
= BLOCK_FOR_INSN (insn
);
433 bb
->aux
= last_block
;
437 else if (INSN_P (insn
) && last_jump
!= NULL_RTX
)
439 class = haifa_classify_insn (insn
);
443 case PFREE_CANDIDATE
:
444 if (flag_schedule_speculative_load
)
446 bb
= earliest_block_with_similiar_load (last_block
, insn
);
458 case PRISKY_CANDIDATE
:
459 /* ??? We could implement better checking PRISKY_CANDIDATEs
460 analogous to sched-rgn.c. */
461 /* We can not change the mode of the backward
462 dependency because REG_DEP_ANTI has the lowest
464 if (add_dependence (insn
, prev
, REG_DEP_ANTI
))
465 add_forward_dependence (prev
, insn
, REG_DEP_ANTI
);
472 /* Maintain the invariant that bb->aux is clear after use. */
475 bb
= last_block
->aux
;
476 last_block
->aux
= NULL
;
481 /* Schedule a single extended basic block, defined by the boundaries HEAD
485 schedule_ebb (rtx head
, rtx tail
)
489 struct deps tmp_deps
;
490 basic_block first_bb
= BLOCK_FOR_INSN (head
);
491 basic_block last_bb
= BLOCK_FOR_INSN (tail
);
493 if (no_real_insns_p (head
, tail
))
494 return BLOCK_FOR_INSN (tail
);
498 /* Compute LOG_LINKS. */
499 init_deps (&tmp_deps
);
500 sched_analyze (&tmp_deps
, head
, tail
);
501 free_deps (&tmp_deps
);
503 /* Compute INSN_DEPEND. */
504 compute_forward_dependences (head
, tail
);
506 add_deps_for_risky_insns (head
, tail
);
508 if (targetm
.sched
.dependencies_evaluation_hook
)
509 targetm
.sched
.dependencies_evaluation_hook (head
, tail
);
511 /* Set priorities. */
512 n_insns
= set_priorities (head
, tail
);
514 current_sched_info
->prev_head
= PREV_INSN (head
);
515 current_sched_info
->next_tail
= NEXT_INSN (tail
);
517 if (write_symbols
!= NO_DEBUG
)
519 save_line_notes (first_bb
->index
, head
, tail
);
520 rm_line_notes (head
, tail
);
523 /* rm_other_notes only removes notes which are _inside_ the
524 block---that is, it won't remove notes before the first real insn
525 or after the last real insn of the block. So if the first insn
526 has a REG_SAVE_NOTE which would otherwise be emitted before the
527 insn, it is redundant with the note before the start of the
528 block, and so we have to take it out. */
533 for (note
= REG_NOTES (head
); note
; note
= XEXP (note
, 1))
534 if (REG_NOTE_KIND (note
) == REG_SAVE_NOTE
)
536 remove_note (head
, note
);
537 note
= XEXP (note
, 1);
538 remove_note (head
, note
);
542 /* Remove remaining note insns from the block, save them in
543 note_list. These notes are restored at the end of
544 schedule_block (). */
545 rm_other_notes (head
, tail
);
547 current_sched_info
->queue_must_finish_empty
= 1;
549 schedule_block (-1, n_insns
);
551 /* Sanity check: verify that all region insns were scheduled. */
552 gcc_assert (sched_n_insns
== n_insns
);
553 head
= current_sched_info
->head
;
554 tail
= current_sched_info
->tail
;
556 if (write_symbols
!= NO_DEBUG
)
557 restore_line_notes (head
, tail
);
558 b
= fix_basic_block_boundaries (first_bb
, last_bb
, head
, tail
);
560 finish_deps_global ();
564 /* The one entry point in this file. DUMP_FILE is the dump file for
568 schedule_ebbs (FILE *dump_file
)
571 int probability_cutoff
;
573 if (profile_info
&& flag_branch_probabilities
)
574 probability_cutoff
= PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK
);
576 probability_cutoff
= PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY
);
577 probability_cutoff
= REG_BR_PROB_BASE
/ 100 * probability_cutoff
;
579 /* Taking care of this degenerate case makes the rest of
580 this code simpler. */
581 if (n_basic_blocks
== 0)
584 sched_init (dump_file
);
586 current_sched_info
= &ebb_sched_info
;
588 compute_bb_for_insn ();
590 /* Schedule every region in the subroutine. */
593 rtx head
= BB_HEAD (bb
);
601 if (bb
->next_bb
== EXIT_BLOCK_PTR
602 || LABEL_P (BB_HEAD (bb
->next_bb
)))
604 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
606 if ((e
->flags
& EDGE_FALLTHRU
) != 0)
611 if (e
->probability
<= probability_cutoff
)
616 /* Blah. We should fix the rest of the code not to get confused by
621 head
= NEXT_INSN (head
);
622 else if (NOTE_P (tail
))
623 tail
= PREV_INSN (tail
);
624 else if (LABEL_P (head
))
625 head
= NEXT_INSN (head
);
630 bb
= schedule_ebb (head
, tail
);
633 /* Updating life info can be done by local propagation over the modified
636 /* Reposition the prologue and epilogue notes in case we moved the
637 prologue/epilogue insns. */
638 if (reload_completed
)
639 reposition_prologue_and_epilogue_notes (get_insns ());
641 if (write_symbols
!= NO_DEBUG
)
642 rm_redundant_line_notes ();