1 /* Instruction scheduling pass.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 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"
42 #include "sched-int.h"
45 /* The number of insns to be scheduled in total. */
46 static int target_n_insns
;
47 /* The number of insns scheduled so far. */
48 static int sched_n_insns
;
50 /* Implementations of the sched_info functions for region scheduling. */
51 static void init_ready_list
PARAMS ((struct ready_list
*));
52 static int can_schedule_ready_p
PARAMS ((rtx
));
53 static int new_ready
PARAMS ((rtx
));
54 static int schedule_more_p
PARAMS ((void));
55 static const char *ebb_print_insn
PARAMS ((rtx
, int));
56 static int rank
PARAMS ((rtx
, rtx
));
57 static int contributes_to_priority
PARAMS ((rtx
, rtx
));
58 static void compute_jump_reg_dependencies
PARAMS ((rtx
, regset
));
59 static void schedule_ebb
PARAMS ((rtx
, rtx
));
61 /* Return nonzero if there are more insns that should be scheduled. */
66 return sched_n_insns
< target_n_insns
;
69 /* Add all insns that are initially ready to the ready list READY. Called
70 once before scheduling a set of insns. */
73 init_ready_list (ready
)
74 struct ready_list
*ready
;
76 rtx prev_head
= current_sched_info
->prev_head
;
77 rtx next_tail
= current_sched_info
->next_tail
;
84 /* Print debugging information. */
85 if (sched_verbose
>= 5)
86 debug_dependencies ();
89 /* Initialize ready list with all 'ready' insns in target block.
90 Count number of insns in the target block being scheduled. */
91 for (insn
= NEXT_INSN (prev_head
); insn
!= next_tail
; insn
= NEXT_INSN (insn
))
97 next
= NEXT_INSN (insn
);
99 if (INSN_DEP_COUNT (insn
) == 0
100 && (! INSN_P (next
) || SCHED_GROUP_P (next
) == 0))
101 ready_add (ready
, insn
);
102 if (! SCHED_GROUP_P (insn
))
107 /* Called after taking INSN from the ready list. Returns nonzero if this
108 insn can be scheduled, nonzero if we should silently discard it. */
111 can_schedule_ready_p (insn
)
112 rtx insn ATTRIBUTE_UNUSED
;
118 /* Called after INSN has all its dependencies resolved. Return nonzero
119 if it should be moved to the ready list or the queue, or zero if we
120 should silently discard it. */
123 rtx next ATTRIBUTE_UNUSED
;
128 /* Return a string that contains the insn uid and optionally anything else
129 necessary to identify this insn in an output. It's valid to use a
130 static buffer for this. The ALIGNED parameter should cause the string
131 to be formatted so that multiple output lines will line up nicely. */
134 ebb_print_insn (insn
, aligned
)
136 int aligned ATTRIBUTE_UNUSED
;
140 sprintf (tmp
, "%4d", INSN_UID (insn
));
144 /* Compare priority of two insns. Return a positive number if the second
145 insn is to be preferred for scheduling, and a negative one if the first
146 is to be preferred. Zero if they are equally good. */
150 rtx insn1 ATTRIBUTE_UNUSED
, insn2 ATTRIBUTE_UNUSED
;
155 /* NEXT is an instruction that depends on INSN (a backward dependence);
156 return nonzero if we should include this dependence in priority
160 contributes_to_priority (next
, insn
)
161 rtx next ATTRIBUTE_UNUSED
, insn ATTRIBUTE_UNUSED
;
166 /* INSN is a JUMP_INSN. Store the set of registers that must be considered
167 to be set by this jump in SET. */
170 compute_jump_reg_dependencies (insn
, set
)
174 basic_block b
= BLOCK_FOR_INSN (insn
);
176 for (e
= b
->succ
; e
; e
= e
->succ_next
)
177 if ((e
->flags
& EDGE_FALLTHRU
) == 0)
179 bitmap_operation (set
, set
, e
->dest
->global_live_at_start
,
184 /* Used in schedule_insns to initialize current_sched_info for scheduling
185 regions (or single basic blocks). */
187 static struct sched_info ebb_sched_info
=
190 can_schedule_ready_p
,
195 contributes_to_priority
,
196 compute_jump_reg_dependencies
,
203 /* Schedule a single extended basic block, defined by the boundaries HEAD
207 schedule_ebb (head
, tail
)
211 struct deps tmp_deps
;
213 if (no_real_insns_p (head
, tail
))
218 /* Compute LOG_LINKS. */
219 init_deps (&tmp_deps
);
220 sched_analyze (&tmp_deps
, head
, tail
);
221 free_deps (&tmp_deps
);
223 /* Compute INSN_DEPEND. */
224 compute_forward_dependences (head
, tail
);
226 if (targetm
.sched
.dependencies_evaluation_hook
)
227 targetm
.sched
.dependencies_evaluation_hook (head
, tail
);
229 /* Set priorities. */
230 n_insns
= set_priorities (head
, tail
);
232 current_sched_info
->prev_head
= PREV_INSN (head
);
233 current_sched_info
->next_tail
= NEXT_INSN (tail
);
235 if (write_symbols
!= NO_DEBUG
)
237 save_line_notes (0, head
, tail
);
238 rm_line_notes (head
, tail
);
241 /* rm_other_notes only removes notes which are _inside_ the
242 block---that is, it won't remove notes before the first real insn
243 or after the last real insn of the block. So if the first insn
244 has a REG_SAVE_NOTE which would otherwise be emitted before the
245 insn, it is redundant with the note before the start of the
246 block, and so we have to take it out. */
251 for (note
= REG_NOTES (head
); note
; note
= XEXP (note
, 1))
252 if (REG_NOTE_KIND (note
) == REG_SAVE_NOTE
)
254 remove_note (head
, note
);
255 note
= XEXP (note
, 1);
256 remove_note (head
, note
);
260 /* Remove remaining note insns from the block, save them in
261 note_list. These notes are restored at the end of
262 schedule_block (). */
263 rm_other_notes (head
, tail
);
265 current_sched_info
->queue_must_finish_empty
= 1;
267 schedule_block (-1, n_insns
);
269 /* Sanity check: verify that all region insns were scheduled. */
270 if (sched_n_insns
!= n_insns
)
272 head
= current_sched_info
->head
;
273 tail
= current_sched_info
->tail
;
275 if (write_symbols
!= NO_DEBUG
)
276 restore_line_notes (head
, tail
);
278 finish_deps_global ();
281 /* The one entry point in this file. DUMP_FILE is the dump file for
285 schedule_ebbs (dump_file
)
290 /* Taking care of this degenerate case makes the rest of
291 this code simpler. */
292 if (n_basic_blocks
== 0)
295 sched_init (dump_file
);
297 current_sched_info
= &ebb_sched_info
;
299 allocate_reg_life_data ();
300 compute_bb_for_insn ();
302 /* Schedule every region in the subroutine. */
312 if (bb
->next_bb
== EXIT_BLOCK_PTR
313 || GET_CODE (bb
->next_bb
->head
) == CODE_LABEL
)
315 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
316 if ((e
->flags
& EDGE_FALLTHRU
) != 0)
320 if (GET_CODE (tail
) == JUMP_INSN
)
322 rtx x
= find_reg_note (tail
, REG_BR_PROB
, 0);
325 int pred_val
= INTVAL (XEXP (x
, 0));
326 if (pred_val
> REG_BR_PROB_BASE
/ 2)
334 /* Blah. We should fix the rest of the code not to get confused by
338 if (GET_CODE (head
) == NOTE
)
339 head
= NEXT_INSN (head
);
340 else if (GET_CODE (tail
) == NOTE
)
341 tail
= PREV_INSN (tail
);
342 else if (GET_CODE (head
) == CODE_LABEL
)
343 head
= NEXT_INSN (head
);
348 schedule_ebb (head
, tail
);
351 /* It doesn't make much sense to try and update life information here - we
352 probably messed up even the flow graph. */
354 /* Reposition the prologue and epilogue notes in case we moved the
355 prologue/epilogue insns. */
356 if (reload_completed
)
357 reposition_prologue_and_epilogue_notes (get_insns ());
359 if (write_symbols
!= NO_DEBUG
)
360 rm_redundant_line_notes ();