2003-02-28 Aldy Hernandez <aldyh@redhat.com>
[official-gcc.git] / gcc / haifa-sched.c
blob8ea2481ce81693bf31e2fa64d524e9f7e3cbd485
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
12 version.
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
17 for more details.
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
22 02111-1307, USA. */
24 /* Instruction scheduling pass. This file, along with sched-deps.c,
25 contains the generic parts. The actual entry point is found for
26 the normal instruction scheduling pass is found in sched-rgn.c.
28 We compute insn priorities based on data dependencies. Flow
29 analysis only creates a fraction of the data-dependencies we must
30 observe: namely, only those dependencies which the combiner can be
31 expected to use. For this pass, we must therefore create the
32 remaining dependencies we need to observe: register dependencies,
33 memory dependencies, dependencies to keep function calls in order,
34 and the dependence between a conditional branch and the setting of
35 condition codes are all dealt with here.
37 The scheduler first traverses the data flow graph, starting with
38 the last instruction, and proceeding to the first, assigning values
39 to insn_priority as it goes. This sorts the instructions
40 topologically by data dependence.
42 Once priorities have been established, we order the insns using
43 list scheduling. This works as follows: starting with a list of
44 all the ready insns, and sorted according to priority number, we
45 schedule the insn from the end of the list by placing its
46 predecessors in the list according to their priority order. We
47 consider this insn scheduled by setting the pointer to the "end" of
48 the list to point to the previous insn. When an insn has no
49 predecessors, we either queue it until sufficient time has elapsed
50 or add it to the ready list. As the instructions are scheduled or
51 when stalls are introduced, the queue advances and dumps insns into
52 the ready list. When all insns down to the lowest priority have
53 been scheduled, the critical path of the basic block has been made
54 as short as possible. The remaining insns are then scheduled in
55 remaining slots.
57 Function unit conflicts are resolved during forward list scheduling
58 by tracking the time when each insn is committed to the schedule
59 and from that, the time the function units it uses must be free.
60 As insns on the ready list are considered for scheduling, those
61 that would result in a blockage of the already committed insns are
62 queued until no blockage will result.
64 The following list shows the order in which we want to break ties
65 among insns in the ready list:
67 1. choose insn with the longest path to end of bb, ties
68 broken by
69 2. choose insn with least contribution to register pressure,
70 ties broken by
71 3. prefer in-block upon interblock motion, ties broken by
72 4. prefer useful upon speculative motion, ties broken by
73 5. choose insn with largest control flow probability, ties
74 broken by
75 6. choose insn with the least dependences upon the previously
76 scheduled insn, or finally
77 7 choose the insn which has the most insns dependent on it.
78 8. choose insn with lowest UID.
80 Memory references complicate matters. Only if we can be certain
81 that memory references are not part of the data dependency graph
82 (via true, anti, or output dependence), can we move operations past
83 memory references. To first approximation, reads can be done
84 independently, while writes introduce dependencies. Better
85 approximations will yield fewer dependencies.
87 Before reload, an extended analysis of interblock data dependences
88 is required for interblock scheduling. This is performed in
89 compute_block_backward_dependences ().
91 Dependencies set up by memory references are treated in exactly the
92 same way as other dependencies, by using LOG_LINKS backward
93 dependences. LOG_LINKS are translated into INSN_DEPEND forward
94 dependences for the purpose of forward list scheduling.
96 Having optimized the critical path, we may have also unduly
97 extended the lifetimes of some registers. If an operation requires
98 that constants be loaded into registers, it is certainly desirable
99 to load those constants as early as necessary, but no earlier.
100 I.e., it will not do to load up a bunch of registers at the
101 beginning of a basic block only to use them at the end, if they
102 could be loaded later, since this may result in excessive register
103 utilization.
105 Note that since branches are never in basic blocks, but only end
106 basic blocks, this pass will not move branches. But that is ok,
107 since we can use GNU's delayed branch scheduling pass to take care
108 of this case.
110 Also note that no further optimizations based on algebraic
111 identities are performed, so this pass would be a good one to
112 perform instruction splitting, such as breaking up a multiply
113 instruction into shifts and adds where that is profitable.
115 Given the memory aliasing analysis that this pass should perform,
116 it should be possible to remove redundant stores to memory, and to
117 load values from registers instead of hitting memory.
119 Before reload, speculative insns are moved only if a 'proof' exists
120 that no exception will be caused by this, and if no live registers
121 exist that inhibit the motion (live registers constraints are not
122 represented by data dependence edges).
124 This pass must update information that subsequent passes expect to
125 be correct. Namely: reg_n_refs, reg_n_sets, reg_n_deaths,
126 reg_n_calls_crossed, and reg_live_length. Also, BLOCK_HEAD,
127 BLOCK_END.
129 The information in the line number notes is carefully retained by
130 this pass. Notes that refer to the starting and ending of
131 exception regions are also carefully retained by this pass. All
132 other NOTE insns are grouped in their same relative order at the
133 beginning of basic blocks and regions that have been scheduled. */
135 #include "config.h"
136 #include "system.h"
137 #include "coretypes.h"
138 #include "tm.h"
139 #include "toplev.h"
140 #include "rtl.h"
141 #include "tm_p.h"
142 #include "hard-reg-set.h"
143 #include "basic-block.h"
144 #include "regs.h"
145 #include "function.h"
146 #include "flags.h"
147 #include "insn-config.h"
148 #include "insn-attr.h"
149 #include "except.h"
150 #include "toplev.h"
151 #include "recog.h"
152 #include "sched-int.h"
153 #include "target.h"
155 #ifdef INSN_SCHEDULING
157 /* issue_rate is the number of insns that can be scheduled in the same
158 machine cycle. It can be defined in the config/mach/mach.h file,
159 otherwise we set it to 1. */
161 static int issue_rate;
163 /* If the following variable value is nonzero, the scheduler inserts
164 bubbles (nop insns). The value of variable affects on scheduler
165 behavior only if automaton pipeline interface with multipass
166 scheduling is used and hook dfa_bubble is defined. */
167 int insert_schedule_bubbles_p = 0;
169 /* sched-verbose controls the amount of debugging output the
170 scheduler prints. It is controlled by -fsched-verbose=N:
171 N>0 and no -DSR : the output is directed to stderr.
172 N>=10 will direct the printouts to stderr (regardless of -dSR).
173 N=1: same as -dSR.
174 N=2: bb's probabilities, detailed ready list info, unit/insn info.
175 N=3: rtl at abort point, control-flow, regions info.
176 N=5: dependences info. */
178 static int sched_verbose_param = 0;
179 int sched_verbose = 0;
181 /* Debugging file. All printouts are sent to dump, which is always set,
182 either to stderr, or to the dump listing file (-dRS). */
183 FILE *sched_dump = 0;
185 /* Highest uid before scheduling. */
186 static int old_max_uid;
188 /* fix_sched_param() is called from toplev.c upon detection
189 of the -fsched-verbose=N option. */
191 void
192 fix_sched_param (param, val)
193 const char *param, *val;
195 if (!strcmp (param, "verbose"))
196 sched_verbose_param = atoi (val);
197 else
198 warning ("fix_sched_param: unknown param: %s", param);
201 struct haifa_insn_data *h_i_d;
203 #define LINE_NOTE(INSN) (h_i_d[INSN_UID (INSN)].line_note)
204 #define INSN_TICK(INSN) (h_i_d[INSN_UID (INSN)].tick)
206 /* Vector indexed by basic block number giving the starting line-number
207 for each basic block. */
208 static rtx *line_note_head;
210 /* List of important notes we must keep around. This is a pointer to the
211 last element in the list. */
212 static rtx note_list;
214 /* Queues, etc. */
216 /* An instruction is ready to be scheduled when all insns preceding it
217 have already been scheduled. It is important to ensure that all
218 insns which use its result will not be executed until its result
219 has been computed. An insn is maintained in one of four structures:
221 (P) the "Pending" set of insns which cannot be scheduled until
222 their dependencies have been satisfied.
223 (Q) the "Queued" set of insns that can be scheduled when sufficient
224 time has passed.
225 (R) the "Ready" list of unscheduled, uncommitted insns.
226 (S) the "Scheduled" list of insns.
228 Initially, all insns are either "Pending" or "Ready" depending on
229 whether their dependencies are satisfied.
231 Insns move from the "Ready" list to the "Scheduled" list as they
232 are committed to the schedule. As this occurs, the insns in the
233 "Pending" list have their dependencies satisfied and move to either
234 the "Ready" list or the "Queued" set depending on whether
235 sufficient time has passed to make them ready. As time passes,
236 insns move from the "Queued" set to the "Ready" list. Insns may
237 move from the "Ready" list to the "Queued" set if they are blocked
238 due to a function unit conflict.
240 The "Pending" list (P) are the insns in the INSN_DEPEND of the unscheduled
241 insns, i.e., those that are ready, queued, and pending.
242 The "Queued" set (Q) is implemented by the variable `insn_queue'.
243 The "Ready" list (R) is implemented by the variables `ready' and
244 `n_ready'.
245 The "Scheduled" list (S) is the new insn chain built by this pass.
247 The transition (R->S) is implemented in the scheduling loop in
248 `schedule_block' when the best insn to schedule is chosen.
249 The transition (R->Q) is implemented in `queue_insn' when an
250 insn is found to have a function unit conflict with the already
251 committed insns.
252 The transitions (P->R and P->Q) are implemented in `schedule_insn' as
253 insns move from the ready list to the scheduled list.
254 The transition (Q->R) is implemented in 'queue_to_insn' as time
255 passes or stalls are introduced. */
257 /* Implement a circular buffer to delay instructions until sufficient
258 time has passed. For the old pipeline description interface,
259 INSN_QUEUE_SIZE is a power of two larger than MAX_BLOCKAGE and
260 MAX_READY_COST computed by genattr.c. For the new pipeline
261 description interface, MAX_INSN_QUEUE_INDEX is a power of two minus
262 one which is larger than maximal time of instruction execution
263 computed by genattr.c on the base maximal time of functional unit
264 reservations and geting a result. This is the longest time an
265 insn may be queued. */
267 #define MAX_INSN_QUEUE_INDEX max_insn_queue_index_macro_value
269 static rtx *insn_queue;
270 static int q_ptr = 0;
271 static int q_size = 0;
272 #define NEXT_Q(X) (((X)+1) & MAX_INSN_QUEUE_INDEX)
273 #define NEXT_Q_AFTER(X, C) (((X)+C) & MAX_INSN_QUEUE_INDEX)
275 /* The following variable defines value for macro
276 MAX_INSN_QUEUE_INDEX. */
277 static int max_insn_queue_index_macro_value;
279 /* The following variable value refers for all current and future
280 reservations of the processor units. */
281 state_t curr_state;
283 /* The following variable value is size of memory representing all
284 current and future reservations of the processor units. It is used
285 only by DFA based scheduler. */
286 static size_t dfa_state_size;
288 /* The following array is used to find the best insn from ready when
289 the automaton pipeline interface is used. */
290 static char *ready_try;
292 /* Describe the ready list of the scheduler.
293 VEC holds space enough for all insns in the current region. VECLEN
294 says how many exactly.
295 FIRST is the index of the element with the highest priority; i.e. the
296 last one in the ready list, since elements are ordered by ascending
297 priority.
298 N_READY determines how many insns are on the ready list. */
300 struct ready_list
302 rtx *vec;
303 int veclen;
304 int first;
305 int n_ready;
308 static int may_trap_exp PARAMS ((rtx, int));
310 /* Nonzero iff the address is comprised from at most 1 register. */
311 #define CONST_BASED_ADDRESS_P(x) \
312 (GET_CODE (x) == REG \
313 || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS \
314 || (GET_CODE (x) == LO_SUM)) \
315 && (CONSTANT_P (XEXP (x, 0)) \
316 || CONSTANT_P (XEXP (x, 1)))))
318 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
319 as found by analyzing insn's expression. */
321 static int
322 may_trap_exp (x, is_store)
323 rtx x;
324 int is_store;
326 enum rtx_code code;
328 if (x == 0)
329 return TRAP_FREE;
330 code = GET_CODE (x);
331 if (is_store)
333 if (code == MEM && may_trap_p (x))
334 return TRAP_RISKY;
335 else
336 return TRAP_FREE;
338 if (code == MEM)
340 /* The insn uses memory: a volatile load. */
341 if (MEM_VOLATILE_P (x))
342 return IRISKY;
343 /* An exception-free load. */
344 if (!may_trap_p (x))
345 return IFREE;
346 /* A load with 1 base register, to be further checked. */
347 if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
348 return PFREE_CANDIDATE;
349 /* No info on the load, to be further checked. */
350 return PRISKY_CANDIDATE;
352 else
354 const char *fmt;
355 int i, insn_class = TRAP_FREE;
357 /* Neither store nor load, check if it may cause a trap. */
358 if (may_trap_p (x))
359 return TRAP_RISKY;
360 /* Recursive step: walk the insn... */
361 fmt = GET_RTX_FORMAT (code);
362 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
364 if (fmt[i] == 'e')
366 int tmp_class = may_trap_exp (XEXP (x, i), is_store);
367 insn_class = WORST_CLASS (insn_class, tmp_class);
369 else if (fmt[i] == 'E')
371 int j;
372 for (j = 0; j < XVECLEN (x, i); j++)
374 int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
375 insn_class = WORST_CLASS (insn_class, tmp_class);
376 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
377 break;
380 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
381 break;
383 return insn_class;
387 /* Classifies insn for the purpose of verifying that it can be
388 moved speculatively, by examining it's patterns, returning:
389 TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
390 TRAP_FREE: non-load insn.
391 IFREE: load from a globaly safe location.
392 IRISKY: volatile load.
393 PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
394 being either PFREE or PRISKY. */
397 haifa_classify_insn (insn)
398 rtx insn;
400 rtx pat = PATTERN (insn);
401 int tmp_class = TRAP_FREE;
402 int insn_class = TRAP_FREE;
403 enum rtx_code code;
405 if (GET_CODE (pat) == PARALLEL)
407 int i, len = XVECLEN (pat, 0);
409 for (i = len - 1; i >= 0; i--)
411 code = GET_CODE (XVECEXP (pat, 0, i));
412 switch (code)
414 case CLOBBER:
415 /* Test if it is a 'store'. */
416 tmp_class = may_trap_exp (XEXP (XVECEXP (pat, 0, i), 0), 1);
417 break;
418 case SET:
419 /* Test if it is a store. */
420 tmp_class = may_trap_exp (SET_DEST (XVECEXP (pat, 0, i)), 1);
421 if (tmp_class == TRAP_RISKY)
422 break;
423 /* Test if it is a load. */
424 tmp_class
425 = WORST_CLASS (tmp_class,
426 may_trap_exp (SET_SRC (XVECEXP (pat, 0, i)),
427 0));
428 break;
429 case COND_EXEC:
430 case TRAP_IF:
431 tmp_class = TRAP_RISKY;
432 break;
433 default:
436 insn_class = WORST_CLASS (insn_class, tmp_class);
437 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
438 break;
441 else
443 code = GET_CODE (pat);
444 switch (code)
446 case CLOBBER:
447 /* Test if it is a 'store'. */
448 tmp_class = may_trap_exp (XEXP (pat, 0), 1);
449 break;
450 case SET:
451 /* Test if it is a store. */
452 tmp_class = may_trap_exp (SET_DEST (pat), 1);
453 if (tmp_class == TRAP_RISKY)
454 break;
455 /* Test if it is a load. */
456 tmp_class =
457 WORST_CLASS (tmp_class,
458 may_trap_exp (SET_SRC (pat), 0));
459 break;
460 case COND_EXEC:
461 case TRAP_IF:
462 tmp_class = TRAP_RISKY;
463 break;
464 default:;
466 insn_class = tmp_class;
469 return insn_class;
472 /* Forward declarations. */
474 /* The scheduler using only DFA description should never use the
475 following five functions: */
476 static unsigned int blockage_range PARAMS ((int, rtx));
477 static void clear_units PARAMS ((void));
478 static void schedule_unit PARAMS ((int, rtx, int));
479 static int actual_hazard PARAMS ((int, rtx, int, int));
480 static int potential_hazard PARAMS ((int, rtx, int));
482 static int priority PARAMS ((rtx));
483 static int rank_for_schedule PARAMS ((const PTR, const PTR));
484 static void swap_sort PARAMS ((rtx *, int));
485 static void queue_insn PARAMS ((rtx, int));
486 static int schedule_insn PARAMS ((rtx, struct ready_list *, int));
487 static int find_set_reg_weight PARAMS ((rtx));
488 static void find_insn_reg_weight PARAMS ((int));
489 static void adjust_priority PARAMS ((rtx));
490 static void advance_one_cycle PARAMS ((void));
492 /* Notes handling mechanism:
493 =========================
494 Generally, NOTES are saved before scheduling and restored after scheduling.
495 The scheduler distinguishes between three types of notes:
497 (1) LINE_NUMBER notes, generated and used for debugging. Here,
498 before scheduling a region, a pointer to the LINE_NUMBER note is
499 added to the insn following it (in save_line_notes()), and the note
500 is removed (in rm_line_notes() and unlink_line_notes()). After
501 scheduling the region, this pointer is used for regeneration of
502 the LINE_NUMBER note (in restore_line_notes()).
504 (2) LOOP_BEGIN, LOOP_END, SETJMP, EHREGION_BEG, EHREGION_END notes:
505 Before scheduling a region, a pointer to the note is added to the insn
506 that follows or precedes it. (This happens as part of the data dependence
507 computation). After scheduling an insn, the pointer contained in it is
508 used for regenerating the corresponding note (in reemit_notes).
510 (3) All other notes (e.g. INSN_DELETED): Before scheduling a block,
511 these notes are put in a list (in rm_other_notes() and
512 unlink_other_notes ()). After scheduling the block, these notes are
513 inserted at the beginning of the block (in schedule_block()). */
515 static rtx unlink_other_notes PARAMS ((rtx, rtx));
516 static rtx unlink_line_notes PARAMS ((rtx, rtx));
517 static rtx reemit_notes PARAMS ((rtx, rtx));
519 static rtx *ready_lastpos PARAMS ((struct ready_list *));
520 static void ready_sort PARAMS ((struct ready_list *));
521 static rtx ready_remove_first PARAMS ((struct ready_list *));
523 static void queue_to_ready PARAMS ((struct ready_list *));
525 static void debug_ready_list PARAMS ((struct ready_list *));
527 static rtx move_insn1 PARAMS ((rtx, rtx));
528 static rtx move_insn PARAMS ((rtx, rtx));
530 /* The following functions are used to implement multi-pass scheduling
531 on the first cycle. It is used only for DFA based scheduler. */
532 static rtx ready_element PARAMS ((struct ready_list *, int));
533 static rtx ready_remove PARAMS ((struct ready_list *, int));
534 static int max_issue PARAMS ((struct ready_list *, int *));
536 static rtx choose_ready PARAMS ((struct ready_list *));
538 #endif /* INSN_SCHEDULING */
540 /* Point to state used for the current scheduling pass. */
541 struct sched_info *current_sched_info;
543 #ifndef INSN_SCHEDULING
544 void
545 schedule_insns (dump_file)
546 FILE *dump_file ATTRIBUTE_UNUSED;
549 #else
551 /* Pointer to the last instruction scheduled. Used by rank_for_schedule,
552 so that insns independent of the last scheduled insn will be preferred
553 over dependent instructions. */
555 static rtx last_scheduled_insn;
557 /* Compute the function units used by INSN. This caches the value
558 returned by function_units_used. A function unit is encoded as the
559 unit number if the value is non-negative and the complement of a
560 mask if the value is negative. A function unit index is the
561 non-negative encoding. The scheduler using only DFA description
562 should never use the following function. */
564 HAIFA_INLINE int
565 insn_unit (insn)
566 rtx insn;
568 int unit = INSN_UNIT (insn);
570 if (unit == 0)
572 recog_memoized (insn);
574 /* A USE insn, or something else we don't need to understand.
575 We can't pass these directly to function_units_used because it will
576 trigger a fatal error for unrecognizable insns. */
577 if (INSN_CODE (insn) < 0)
578 unit = -1;
579 else
581 unit = function_units_used (insn);
582 /* Increment non-negative values so we can cache zero. */
583 if (unit >= 0)
584 unit++;
586 /* We only cache 16 bits of the result, so if the value is out of
587 range, don't cache it. */
588 if (FUNCTION_UNITS_SIZE < HOST_BITS_PER_SHORT
589 || unit >= 0
590 || (unit & ~((1 << (HOST_BITS_PER_SHORT - 1)) - 1)) == 0)
591 INSN_UNIT (insn) = unit;
593 return (unit > 0 ? unit - 1 : unit);
596 /* Compute the blockage range for executing INSN on UNIT. This caches
597 the value returned by the blockage_range_function for the unit.
598 These values are encoded in an int where the upper half gives the
599 minimum value and the lower half gives the maximum value. The
600 scheduler using only DFA description should never use the following
601 function. */
603 HAIFA_INLINE static unsigned int
604 blockage_range (unit, insn)
605 int unit;
606 rtx insn;
608 unsigned int blockage = INSN_BLOCKAGE (insn);
609 unsigned int range;
611 if ((int) UNIT_BLOCKED (blockage) != unit + 1)
613 range = function_units[unit].blockage_range_function (insn);
614 /* We only cache the blockage range for one unit and then only if
615 the values fit. */
616 if (HOST_BITS_PER_INT >= UNIT_BITS + 2 * BLOCKAGE_BITS)
617 INSN_BLOCKAGE (insn) = ENCODE_BLOCKAGE (unit + 1, range);
619 else
620 range = BLOCKAGE_RANGE (blockage);
622 return range;
625 /* A vector indexed by function unit instance giving the last insn to
626 use the unit. The value of the function unit instance index for
627 unit U instance I is (U + I * FUNCTION_UNITS_SIZE). The scheduler
628 using only DFA description should never use the following variable. */
629 #if FUNCTION_UNITS_SIZE
630 static rtx unit_last_insn[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
631 #else
632 static rtx unit_last_insn[1];
633 #endif
635 /* A vector indexed by function unit instance giving the minimum time
636 when the unit will unblock based on the maximum blockage cost. The
637 scheduler using only DFA description should never use the following
638 variable. */
639 #if FUNCTION_UNITS_SIZE
640 static int unit_tick[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
641 #else
642 static int unit_tick[1];
643 #endif
645 /* A vector indexed by function unit number giving the number of insns
646 that remain to use the unit. The scheduler using only DFA
647 description should never use the following variable. */
648 #if FUNCTION_UNITS_SIZE
649 static int unit_n_insns[FUNCTION_UNITS_SIZE];
650 #else
651 static int unit_n_insns[1];
652 #endif
654 /* Access the unit_last_insn array. Used by the visualization code.
655 The scheduler using only DFA description should never use the
656 following function. */
659 get_unit_last_insn (instance)
660 int instance;
662 return unit_last_insn[instance];
665 /* Reset the function unit state to the null state. */
667 static void
668 clear_units ()
670 memset ((char *) unit_last_insn, 0, sizeof (unit_last_insn));
671 memset ((char *) unit_tick, 0, sizeof (unit_tick));
672 memset ((char *) unit_n_insns, 0, sizeof (unit_n_insns));
675 /* Return the issue-delay of an insn. The scheduler using only DFA
676 description should never use the following function. */
678 HAIFA_INLINE int
679 insn_issue_delay (insn)
680 rtx insn;
682 int i, delay = 0;
683 int unit = insn_unit (insn);
685 /* Efficiency note: in fact, we are working 'hard' to compute a
686 value that was available in md file, and is not available in
687 function_units[] structure. It would be nice to have this
688 value there, too. */
689 if (unit >= 0)
691 if (function_units[unit].blockage_range_function &&
692 function_units[unit].blockage_function)
693 delay = function_units[unit].blockage_function (insn, insn);
695 else
696 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
697 if ((unit & 1) != 0 && function_units[i].blockage_range_function
698 && function_units[i].blockage_function)
699 delay = MAX (delay, function_units[i].blockage_function (insn, insn));
701 return delay;
704 /* Return the actual hazard cost of executing INSN on the unit UNIT,
705 instance INSTANCE at time CLOCK if the previous actual hazard cost
706 was COST. The scheduler using only DFA description should never
707 use the following function. */
709 HAIFA_INLINE int
710 actual_hazard_this_instance (unit, instance, insn, clock, cost)
711 int unit, instance, clock, cost;
712 rtx insn;
714 int tick = unit_tick[instance]; /* Issue time of the last issued insn. */
716 if (tick - clock > cost)
718 /* The scheduler is operating forward, so unit's last insn is the
719 executing insn and INSN is the candidate insn. We want a
720 more exact measure of the blockage if we execute INSN at CLOCK
721 given when we committed the execution of the unit's last insn.
723 The blockage value is given by either the unit's max blockage
724 constant, blockage range function, or blockage function. Use
725 the most exact form for the given unit. */
727 if (function_units[unit].blockage_range_function)
729 if (function_units[unit].blockage_function)
730 tick += (function_units[unit].blockage_function
731 (unit_last_insn[instance], insn)
732 - function_units[unit].max_blockage);
733 else
734 tick += ((int) MAX_BLOCKAGE_COST (blockage_range (unit, insn))
735 - function_units[unit].max_blockage);
737 if (tick - clock > cost)
738 cost = tick - clock;
740 return cost;
743 /* Record INSN as having begun execution on the units encoded by UNIT
744 at time CLOCK. The scheduler using only DFA description should
745 never use the following function. */
747 HAIFA_INLINE static void
748 schedule_unit (unit, insn, clock)
749 int unit, clock;
750 rtx insn;
752 int i;
754 if (unit >= 0)
756 int instance = unit;
757 #if MAX_MULTIPLICITY > 1
758 /* Find the first free instance of the function unit and use that
759 one. We assume that one is free. */
760 for (i = function_units[unit].multiplicity - 1; i > 0; i--)
762 if (!actual_hazard_this_instance (unit, instance, insn, clock, 0))
763 break;
764 instance += FUNCTION_UNITS_SIZE;
766 #endif
767 unit_last_insn[instance] = insn;
768 unit_tick[instance] = (clock + function_units[unit].max_blockage);
770 else
771 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
772 if ((unit & 1) != 0)
773 schedule_unit (i, insn, clock);
776 /* Return the actual hazard cost of executing INSN on the units
777 encoded by UNIT at time CLOCK if the previous actual hazard cost
778 was COST. The scheduler using only DFA description should never
779 use the following function. */
781 HAIFA_INLINE static int
782 actual_hazard (unit, insn, clock, cost)
783 int unit, clock, cost;
784 rtx insn;
786 int i;
788 if (unit >= 0)
790 /* Find the instance of the function unit with the minimum hazard. */
791 int instance = unit;
792 int best_cost = actual_hazard_this_instance (unit, instance, insn,
793 clock, cost);
794 #if MAX_MULTIPLICITY > 1
795 int this_cost;
797 if (best_cost > cost)
799 for (i = function_units[unit].multiplicity - 1; i > 0; i--)
801 instance += FUNCTION_UNITS_SIZE;
802 this_cost = actual_hazard_this_instance (unit, instance, insn,
803 clock, cost);
804 if (this_cost < best_cost)
806 best_cost = this_cost;
807 if (this_cost <= cost)
808 break;
812 #endif
813 cost = MAX (cost, best_cost);
815 else
816 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
817 if ((unit & 1) != 0)
818 cost = actual_hazard (i, insn, clock, cost);
820 return cost;
823 /* Return the potential hazard cost of executing an instruction on the
824 units encoded by UNIT if the previous potential hazard cost was
825 COST. An insn with a large blockage time is chosen in preference
826 to one with a smaller time; an insn that uses a unit that is more
827 likely to be used is chosen in preference to one with a unit that
828 is less used. We are trying to minimize a subsequent actual
829 hazard. The scheduler using only DFA description should never use
830 the following function. */
832 HAIFA_INLINE static int
833 potential_hazard (unit, insn, cost)
834 int unit, cost;
835 rtx insn;
837 int i, ncost;
838 unsigned int minb, maxb;
840 if (unit >= 0)
842 minb = maxb = function_units[unit].max_blockage;
843 if (maxb > 1)
845 if (function_units[unit].blockage_range_function)
847 maxb = minb = blockage_range (unit, insn);
848 maxb = MAX_BLOCKAGE_COST (maxb);
849 minb = MIN_BLOCKAGE_COST (minb);
852 if (maxb > 1)
854 /* Make the number of instructions left dominate. Make the
855 minimum delay dominate the maximum delay. If all these
856 are the same, use the unit number to add an arbitrary
857 ordering. Other terms can be added. */
858 ncost = minb * 0x40 + maxb;
859 ncost *= (unit_n_insns[unit] - 1) * 0x1000 + unit;
860 if (ncost > cost)
861 cost = ncost;
865 else
866 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
867 if ((unit & 1) != 0)
868 cost = potential_hazard (i, insn, cost);
870 return cost;
873 /* Compute cost of executing INSN given the dependence LINK on the insn USED.
874 This is the number of cycles between instruction issue and
875 instruction results. */
877 HAIFA_INLINE int
878 insn_cost (insn, link, used)
879 rtx insn, link, used;
881 int cost = INSN_COST (insn);
883 if (cost < 0)
885 /* A USE insn, or something else we don't need to
886 understand. We can't pass these directly to
887 result_ready_cost or insn_default_latency because it will
888 trigger a fatal error for unrecognizable insns. */
889 if (recog_memoized (insn) < 0)
891 INSN_COST (insn) = 0;
892 return 0;
894 else
896 if (targetm.sched.use_dfa_pipeline_interface
897 && (*targetm.sched.use_dfa_pipeline_interface) ())
898 cost = insn_default_latency (insn);
899 else
900 cost = result_ready_cost (insn);
902 if (cost < 0)
903 cost = 0;
905 INSN_COST (insn) = cost;
909 /* In this case estimate cost without caring how insn is used. */
910 if (link == 0 || used == 0)
911 return cost;
913 /* A USE insn should never require the value used to be computed.
914 This allows the computation of a function's result and parameter
915 values to overlap the return and call. */
916 if (recog_memoized (used) < 0)
917 cost = 0;
918 else
920 if (targetm.sched.use_dfa_pipeline_interface
921 && (*targetm.sched.use_dfa_pipeline_interface) ())
923 if (INSN_CODE (insn) >= 0)
925 if (REG_NOTE_KIND (link) == REG_DEP_ANTI)
926 cost = 0;
927 else if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
929 cost = (insn_default_latency (insn)
930 - insn_default_latency (used));
931 if (cost <= 0)
932 cost = 1;
934 else if (bypass_p (insn))
935 cost = insn_latency (insn, used);
939 if (targetm.sched.adjust_cost)
940 cost = (*targetm.sched.adjust_cost) (used, link, insn, cost);
942 if (cost < 0)
943 cost = 0;
946 return cost;
949 /* Compute the priority number for INSN. */
951 static int
952 priority (insn)
953 rtx insn;
955 rtx link;
957 if (! INSN_P (insn))
958 return 0;
960 if (! INSN_PRIORITY_KNOWN (insn))
962 int this_priority = 0;
964 if (INSN_DEPEND (insn) == 0)
965 this_priority = insn_cost (insn, 0, 0);
966 else
968 for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
970 rtx next;
971 int next_priority;
973 if (RTX_INTEGRATED_P (link))
974 continue;
976 next = XEXP (link, 0);
978 /* Critical path is meaningful in block boundaries only. */
979 if (! (*current_sched_info->contributes_to_priority) (next, insn))
980 continue;
982 next_priority = insn_cost (insn, link, next) + priority (next);
983 if (next_priority > this_priority)
984 this_priority = next_priority;
987 INSN_PRIORITY (insn) = this_priority;
988 INSN_PRIORITY_KNOWN (insn) = 1;
991 return INSN_PRIORITY (insn);
994 /* Macros and functions for keeping the priority queue sorted, and
995 dealing with queueing and dequeueing of instructions. */
997 #define SCHED_SORT(READY, N_READY) \
998 do { if ((N_READY) == 2) \
999 swap_sort (READY, N_READY); \
1000 else if ((N_READY) > 2) \
1001 qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); } \
1002 while (0)
1004 /* Returns a positive value if x is preferred; returns a negative value if
1005 y is preferred. Should never return 0, since that will make the sort
1006 unstable. */
1008 static int
1009 rank_for_schedule (x, y)
1010 const PTR x;
1011 const PTR y;
1013 rtx tmp = *(const rtx *) y;
1014 rtx tmp2 = *(const rtx *) x;
1015 rtx link;
1016 int tmp_class, tmp2_class, depend_count1, depend_count2;
1017 int val, priority_val, weight_val, info_val;
1019 /* The insn in a schedule group should be issued the first. */
1020 if (SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
1021 return SCHED_GROUP_P (tmp2) ? 1 : -1;
1023 /* Prefer insn with higher priority. */
1024 priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
1026 if (priority_val)
1027 return priority_val;
1029 /* Prefer an insn with smaller contribution to registers-pressure. */
1030 if (!reload_completed &&
1031 (weight_val = INSN_REG_WEIGHT (tmp) - INSN_REG_WEIGHT (tmp2)))
1032 return weight_val;
1034 info_val = (*current_sched_info->rank) (tmp, tmp2);
1035 if (info_val)
1036 return info_val;
1038 /* Compare insns based on their relation to the last-scheduled-insn. */
1039 if (last_scheduled_insn)
1041 /* Classify the instructions into three classes:
1042 1) Data dependent on last schedule insn.
1043 2) Anti/Output dependent on last scheduled insn.
1044 3) Independent of last scheduled insn, or has latency of one.
1045 Choose the insn from the highest numbered class if different. */
1046 link = find_insn_list (tmp, INSN_DEPEND (last_scheduled_insn));
1047 if (link == 0 || insn_cost (last_scheduled_insn, link, tmp) == 1)
1048 tmp_class = 3;
1049 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
1050 tmp_class = 1;
1051 else
1052 tmp_class = 2;
1054 link = find_insn_list (tmp2, INSN_DEPEND (last_scheduled_insn));
1055 if (link == 0 || insn_cost (last_scheduled_insn, link, tmp2) == 1)
1056 tmp2_class = 3;
1057 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
1058 tmp2_class = 1;
1059 else
1060 tmp2_class = 2;
1062 if ((val = tmp2_class - tmp_class))
1063 return val;
1066 /* Prefer the insn which has more later insns that depend on it.
1067 This gives the scheduler more freedom when scheduling later
1068 instructions at the expense of added register pressure. */
1069 depend_count1 = 0;
1070 for (link = INSN_DEPEND (tmp); link; link = XEXP (link, 1))
1071 depend_count1++;
1073 depend_count2 = 0;
1074 for (link = INSN_DEPEND (tmp2); link; link = XEXP (link, 1))
1075 depend_count2++;
1077 val = depend_count2 - depend_count1;
1078 if (val)
1079 return val;
1081 /* If insns are equally good, sort by INSN_LUID (original insn order),
1082 so that we make the sort stable. This minimizes instruction movement,
1083 thus minimizing sched's effect on debugging and cross-jumping. */
1084 return INSN_LUID (tmp) - INSN_LUID (tmp2);
1087 /* Resort the array A in which only element at index N may be out of order. */
1089 HAIFA_INLINE static void
1090 swap_sort (a, n)
1091 rtx *a;
1092 int n;
1094 rtx insn = a[n - 1];
1095 int i = n - 2;
1097 while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
1099 a[i + 1] = a[i];
1100 i -= 1;
1102 a[i + 1] = insn;
1105 /* Add INSN to the insn queue so that it can be executed at least
1106 N_CYCLES after the currently executing insn. Preserve insns
1107 chain for debugging purposes. */
1109 HAIFA_INLINE static void
1110 queue_insn (insn, n_cycles)
1111 rtx insn;
1112 int n_cycles;
1114 int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
1115 rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
1116 insn_queue[next_q] = link;
1117 q_size += 1;
1119 if (sched_verbose >= 2)
1121 fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
1122 (*current_sched_info->print_insn) (insn, 0));
1124 fprintf (sched_dump, "queued for %d cycles.\n", n_cycles);
1128 /* Return a pointer to the bottom of the ready list, i.e. the insn
1129 with the lowest priority. */
1131 HAIFA_INLINE static rtx *
1132 ready_lastpos (ready)
1133 struct ready_list *ready;
1135 if (ready->n_ready == 0)
1136 abort ();
1137 return ready->vec + ready->first - ready->n_ready + 1;
1140 /* Add an element INSN to the ready list so that it ends up with the lowest
1141 priority. */
1143 HAIFA_INLINE void
1144 ready_add (ready, insn)
1145 struct ready_list *ready;
1146 rtx insn;
1148 if (ready->first == ready->n_ready)
1150 memmove (ready->vec + ready->veclen - ready->n_ready,
1151 ready_lastpos (ready),
1152 ready->n_ready * sizeof (rtx));
1153 ready->first = ready->veclen - 1;
1155 ready->vec[ready->first - ready->n_ready] = insn;
1156 ready->n_ready++;
1159 /* Remove the element with the highest priority from the ready list and
1160 return it. */
1162 HAIFA_INLINE static rtx
1163 ready_remove_first (ready)
1164 struct ready_list *ready;
1166 rtx t;
1167 if (ready->n_ready == 0)
1168 abort ();
1169 t = ready->vec[ready->first--];
1170 ready->n_ready--;
1171 /* If the queue becomes empty, reset it. */
1172 if (ready->n_ready == 0)
1173 ready->first = ready->veclen - 1;
1174 return t;
1177 /* The following code implements multi-pass scheduling for the first
1178 cycle. In other words, we will try to choose ready insn which
1179 permits to start maximum number of insns on the same cycle. */
1181 /* Return a pointer to the element INDEX from the ready. INDEX for
1182 insn with the highest priority is 0, and the lowest priority has
1183 N_READY - 1. */
1185 HAIFA_INLINE static rtx
1186 ready_element (ready, index)
1187 struct ready_list *ready;
1188 int index;
1190 #ifdef ENABLE_CHECKING
1191 if (ready->n_ready == 0 || index >= ready->n_ready)
1192 abort ();
1193 #endif
1194 return ready->vec[ready->first - index];
1197 /* Remove the element INDEX from the ready list and return it. INDEX
1198 for insn with the highest priority is 0, and the lowest priority
1199 has N_READY - 1. */
1201 HAIFA_INLINE static rtx
1202 ready_remove (ready, index)
1203 struct ready_list *ready;
1204 int index;
1206 rtx t;
1207 int i;
1209 if (index == 0)
1210 return ready_remove_first (ready);
1211 if (ready->n_ready == 0 || index >= ready->n_ready)
1212 abort ();
1213 t = ready->vec[ready->first - index];
1214 ready->n_ready--;
1215 for (i = index; i < ready->n_ready; i++)
1216 ready->vec[ready->first - i] = ready->vec[ready->first - i - 1];
1217 return t;
1221 /* Sort the ready list READY by ascending priority, using the SCHED_SORT
1222 macro. */
1224 HAIFA_INLINE static void
1225 ready_sort (ready)
1226 struct ready_list *ready;
1228 rtx *first = ready_lastpos (ready);
1229 SCHED_SORT (first, ready->n_ready);
1232 /* PREV is an insn that is ready to execute. Adjust its priority if that
1233 will help shorten or lengthen register lifetimes as appropriate. Also
1234 provide a hook for the target to tweek itself. */
1236 HAIFA_INLINE static void
1237 adjust_priority (prev)
1238 rtx prev;
1240 /* ??? There used to be code here to try and estimate how an insn
1241 affected register lifetimes, but it did it by looking at REG_DEAD
1242 notes, which we removed in schedule_region. Nor did it try to
1243 take into account register pressure or anything useful like that.
1245 Revisit when we have a machine model to work with and not before. */
1247 if (targetm.sched.adjust_priority)
1248 INSN_PRIORITY (prev) =
1249 (*targetm.sched.adjust_priority) (prev, INSN_PRIORITY (prev));
1252 /* Advance time on one cycle. */
1253 HAIFA_INLINE static void
1254 advance_one_cycle ()
1256 if (targetm.sched.use_dfa_pipeline_interface
1257 && (*targetm.sched.use_dfa_pipeline_interface) ())
1259 if (targetm.sched.dfa_pre_cycle_insn)
1260 state_transition (curr_state,
1261 (*targetm.sched.dfa_pre_cycle_insn) ());
1263 state_transition (curr_state, NULL);
1265 if (targetm.sched.dfa_post_cycle_insn)
1266 state_transition (curr_state,
1267 (*targetm.sched.dfa_post_cycle_insn) ());
1271 /* Clock at which the previous instruction was issued. */
1272 static int last_clock_var;
1274 /* INSN is the "currently executing insn". Launch each insn which was
1275 waiting on INSN. READY is the ready list which contains the insns
1276 that are ready to fire. CLOCK is the current cycle. The function
1277 returns necessary cycle advance after issuing the insn (it is not
1278 zero for insns in a schedule group). */
1280 static int
1281 schedule_insn (insn, ready, clock)
1282 rtx insn;
1283 struct ready_list *ready;
1284 int clock;
1286 rtx link;
1287 int advance = 0;
1288 int unit = 0;
1290 if (!targetm.sched.use_dfa_pipeline_interface
1291 || !(*targetm.sched.use_dfa_pipeline_interface) ())
1292 unit = insn_unit (insn);
1294 if (targetm.sched.use_dfa_pipeline_interface
1295 && (*targetm.sched.use_dfa_pipeline_interface) ()
1296 && sched_verbose >= 1)
1298 char buf[2048];
1300 print_insn (buf, insn, 0);
1301 buf[40]=0;
1302 fprintf (sched_dump, ";;\t%3i--> %-40s:", clock, buf);
1304 if (recog_memoized (insn) < 0)
1305 fprintf (sched_dump, "nothing");
1306 else
1307 print_reservation (sched_dump, insn);
1308 fputc ('\n', sched_dump);
1310 else if (sched_verbose >= 2)
1312 fprintf (sched_dump, ";;\t\t--> scheduling insn <<<%d>>> on unit ",
1313 INSN_UID (insn));
1314 insn_print_units (insn);
1315 fputc ('\n', sched_dump);
1318 if (!targetm.sched.use_dfa_pipeline_interface
1319 || !(*targetm.sched.use_dfa_pipeline_interface) ())
1321 if (sched_verbose && unit == -1)
1322 visualize_no_unit (insn);
1325 if (MAX_BLOCKAGE > 1 || issue_rate > 1 || sched_verbose)
1326 schedule_unit (unit, insn, clock);
1328 if (INSN_DEPEND (insn) == 0)
1329 return 0;
1332 for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
1334 rtx next = XEXP (link, 0);
1335 int cost = insn_cost (insn, link, next);
1337 INSN_TICK (next) = MAX (INSN_TICK (next), clock + cost);
1339 if ((INSN_DEP_COUNT (next) -= 1) == 0)
1341 int effective_cost = INSN_TICK (next) - clock;
1343 if (! (*current_sched_info->new_ready) (next))
1344 continue;
1346 if (sched_verbose >= 2)
1348 fprintf (sched_dump, ";;\t\tdependences resolved: insn %s ",
1349 (*current_sched_info->print_insn) (next, 0));
1351 if (effective_cost < 1)
1352 fprintf (sched_dump, "into ready\n");
1353 else
1354 fprintf (sched_dump, "into queue with cost=%d\n",
1355 effective_cost);
1358 /* Adjust the priority of NEXT and either put it on the ready
1359 list or queue it. */
1360 adjust_priority (next);
1361 if (effective_cost < 1)
1362 ready_add (ready, next);
1363 else
1365 queue_insn (next, effective_cost);
1367 if (SCHED_GROUP_P (next) && advance < effective_cost)
1368 advance = effective_cost;
1373 /* Annotate the instruction with issue information -- TImode
1374 indicates that the instruction is expected not to be able
1375 to issue on the same cycle as the previous insn. A machine
1376 may use this information to decide how the instruction should
1377 be aligned. */
1378 if (issue_rate > 1
1379 && GET_CODE (PATTERN (insn)) != USE
1380 && GET_CODE (PATTERN (insn)) != CLOBBER)
1382 if (reload_completed)
1383 PUT_MODE (insn, clock > last_clock_var ? TImode : VOIDmode);
1384 last_clock_var = clock;
1386 return advance;
1389 /* Functions for handling of notes. */
1391 /* Delete notes beginning with INSN and put them in the chain
1392 of notes ended by NOTE_LIST.
1393 Returns the insn following the notes. */
1395 static rtx
1396 unlink_other_notes (insn, tail)
1397 rtx insn, tail;
1399 rtx prev = PREV_INSN (insn);
1401 while (insn != tail && GET_CODE (insn) == NOTE)
1403 rtx next = NEXT_INSN (insn);
1404 /* Delete the note from its current position. */
1405 if (prev)
1406 NEXT_INSN (prev) = next;
1407 if (next)
1408 PREV_INSN (next) = prev;
1410 /* See sched_analyze to see how these are handled. */
1411 if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG
1412 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_END
1413 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK
1414 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_BEG
1415 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_END)
1417 /* Insert the note at the end of the notes list. */
1418 PREV_INSN (insn) = note_list;
1419 if (note_list)
1420 NEXT_INSN (note_list) = insn;
1421 note_list = insn;
1424 insn = next;
1426 return insn;
1429 /* Delete line notes beginning with INSN. Record line-number notes so
1430 they can be reused. Returns the insn following the notes. */
1432 static rtx
1433 unlink_line_notes (insn, tail)
1434 rtx insn, tail;
1436 rtx prev = PREV_INSN (insn);
1438 while (insn != tail && GET_CODE (insn) == NOTE)
1440 rtx next = NEXT_INSN (insn);
1442 if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
1444 /* Delete the note from its current position. */
1445 if (prev)
1446 NEXT_INSN (prev) = next;
1447 if (next)
1448 PREV_INSN (next) = prev;
1450 /* Record line-number notes so they can be reused. */
1451 LINE_NOTE (insn) = insn;
1453 else
1454 prev = insn;
1456 insn = next;
1458 return insn;
1461 /* Return the head and tail pointers of BB. */
1463 void
1464 get_block_head_tail (b, headp, tailp)
1465 int b;
1466 rtx *headp;
1467 rtx *tailp;
1469 /* HEAD and TAIL delimit the basic block being scheduled. */
1470 rtx head = BLOCK_HEAD (b);
1471 rtx tail = BLOCK_END (b);
1473 /* Don't include any notes or labels at the beginning of the
1474 basic block, or notes at the ends of basic blocks. */
1475 while (head != tail)
1477 if (GET_CODE (head) == NOTE)
1478 head = NEXT_INSN (head);
1479 else if (GET_CODE (tail) == NOTE)
1480 tail = PREV_INSN (tail);
1481 else if (GET_CODE (head) == CODE_LABEL)
1482 head = NEXT_INSN (head);
1483 else
1484 break;
1487 *headp = head;
1488 *tailp = tail;
1491 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ]. */
1494 no_real_insns_p (head, tail)
1495 rtx head, tail;
1497 while (head != NEXT_INSN (tail))
1499 if (GET_CODE (head) != NOTE && GET_CODE (head) != CODE_LABEL)
1500 return 0;
1501 head = NEXT_INSN (head);
1503 return 1;
1506 /* Delete line notes from one block. Save them so they can be later restored
1507 (in restore_line_notes). HEAD and TAIL are the boundaries of the
1508 block in which notes should be processed. */
1510 void
1511 rm_line_notes (head, tail)
1512 rtx head, tail;
1514 rtx next_tail;
1515 rtx insn;
1517 next_tail = NEXT_INSN (tail);
1518 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1520 rtx prev;
1522 /* Farm out notes, and maybe save them in NOTE_LIST.
1523 This is needed to keep the debugger from
1524 getting completely deranged. */
1525 if (GET_CODE (insn) == NOTE)
1527 prev = insn;
1528 insn = unlink_line_notes (insn, next_tail);
1530 if (prev == tail)
1531 abort ();
1532 if (prev == head)
1533 abort ();
1534 if (insn == next_tail)
1535 abort ();
1540 /* Save line number notes for each insn in block B. HEAD and TAIL are
1541 the boundaries of the block in which notes should be processed. */
1543 void
1544 save_line_notes (b, head, tail)
1545 int b;
1546 rtx head, tail;
1548 rtx next_tail;
1550 /* We must use the true line number for the first insn in the block
1551 that was computed and saved at the start of this pass. We can't
1552 use the current line number, because scheduling of the previous
1553 block may have changed the current line number. */
1555 rtx line = line_note_head[b];
1556 rtx insn;
1558 next_tail = NEXT_INSN (tail);
1560 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1561 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1562 line = insn;
1563 else
1564 LINE_NOTE (insn) = line;
1567 /* After a block was scheduled, insert line notes into the insns list.
1568 HEAD and TAIL are the boundaries of the block in which notes should
1569 be processed. */
1571 void
1572 restore_line_notes (head, tail)
1573 rtx head, tail;
1575 rtx line, note, prev, new;
1576 int added_notes = 0;
1577 rtx next_tail, insn;
1579 head = head;
1580 next_tail = NEXT_INSN (tail);
1582 /* Determine the current line-number. We want to know the current
1583 line number of the first insn of the block here, in case it is
1584 different from the true line number that was saved earlier. If
1585 different, then we need a line number note before the first insn
1586 of this block. If it happens to be the same, then we don't want to
1587 emit another line number note here. */
1588 for (line = head; line; line = PREV_INSN (line))
1589 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
1590 break;
1592 /* Walk the insns keeping track of the current line-number and inserting
1593 the line-number notes as needed. */
1594 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1595 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1596 line = insn;
1597 /* This used to emit line number notes before every non-deleted note.
1598 However, this confuses a debugger, because line notes not separated
1599 by real instructions all end up at the same address. I can find no
1600 use for line number notes before other notes, so none are emitted. */
1601 else if (GET_CODE (insn) != NOTE
1602 && INSN_UID (insn) < old_max_uid
1603 && (note = LINE_NOTE (insn)) != 0
1604 && note != line
1605 && (line == 0
1606 || NOTE_LINE_NUMBER (note) != NOTE_LINE_NUMBER (line)
1607 || NOTE_SOURCE_FILE (note) != NOTE_SOURCE_FILE (line)))
1609 line = note;
1610 prev = PREV_INSN (insn);
1611 if (LINE_NOTE (note))
1613 /* Re-use the original line-number note. */
1614 LINE_NOTE (note) = 0;
1615 PREV_INSN (note) = prev;
1616 NEXT_INSN (prev) = note;
1617 PREV_INSN (insn) = note;
1618 NEXT_INSN (note) = insn;
1620 else
1622 added_notes++;
1623 new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
1624 NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
1625 RTX_INTEGRATED_P (new) = RTX_INTEGRATED_P (note);
1628 if (sched_verbose && added_notes)
1629 fprintf (sched_dump, ";; added %d line-number notes\n", added_notes);
1632 /* After scheduling the function, delete redundant line notes from the
1633 insns list. */
1635 void
1636 rm_redundant_line_notes ()
1638 rtx line = 0;
1639 rtx insn = get_insns ();
1640 int active_insn = 0;
1641 int notes = 0;
1643 /* Walk the insns deleting redundant line-number notes. Many of these
1644 are already present. The remainder tend to occur at basic
1645 block boundaries. */
1646 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1647 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1649 /* If there are no active insns following, INSN is redundant. */
1650 if (active_insn == 0)
1652 notes++;
1653 NOTE_SOURCE_FILE (insn) = 0;
1654 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1656 /* If the line number is unchanged, LINE is redundant. */
1657 else if (line
1658 && NOTE_LINE_NUMBER (line) == NOTE_LINE_NUMBER (insn)
1659 && NOTE_SOURCE_FILE (line) == NOTE_SOURCE_FILE (insn))
1661 notes++;
1662 NOTE_SOURCE_FILE (line) = 0;
1663 NOTE_LINE_NUMBER (line) = NOTE_INSN_DELETED;
1664 line = insn;
1666 else
1667 line = insn;
1668 active_insn = 0;
1670 else if (!((GET_CODE (insn) == NOTE
1671 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
1672 || (GET_CODE (insn) == INSN
1673 && (GET_CODE (PATTERN (insn)) == USE
1674 || GET_CODE (PATTERN (insn)) == CLOBBER))))
1675 active_insn++;
1677 if (sched_verbose && notes)
1678 fprintf (sched_dump, ";; deleted %d line-number notes\n", notes);
1681 /* Delete notes between HEAD and TAIL and put them in the chain
1682 of notes ended by NOTE_LIST. */
1684 void
1685 rm_other_notes (head, tail)
1686 rtx head;
1687 rtx tail;
1689 rtx next_tail;
1690 rtx insn;
1692 note_list = 0;
1693 if (head == tail && (! INSN_P (head)))
1694 return;
1696 next_tail = NEXT_INSN (tail);
1697 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1699 rtx prev;
1701 /* Farm out notes, and maybe save them in NOTE_LIST.
1702 This is needed to keep the debugger from
1703 getting completely deranged. */
1704 if (GET_CODE (insn) == NOTE)
1706 prev = insn;
1708 insn = unlink_other_notes (insn, next_tail);
1710 if (prev == tail)
1711 abort ();
1712 if (prev == head)
1713 abort ();
1714 if (insn == next_tail)
1715 abort ();
1720 /* Functions for computation of registers live/usage info. */
1722 /* This function looks for a new register being defined.
1723 If the destination register is already used by the source,
1724 a new register is not needed. */
1726 static int
1727 find_set_reg_weight (x)
1728 rtx x;
1730 if (GET_CODE (x) == CLOBBER
1731 && register_operand (SET_DEST (x), VOIDmode))
1732 return 1;
1733 if (GET_CODE (x) == SET
1734 && register_operand (SET_DEST (x), VOIDmode))
1736 if (GET_CODE (SET_DEST (x)) == REG)
1738 if (!reg_mentioned_p (SET_DEST (x), SET_SRC (x)))
1739 return 1;
1740 else
1741 return 0;
1743 return 1;
1745 return 0;
1748 /* Calculate INSN_REG_WEIGHT for all insns of a block. */
1750 static void
1751 find_insn_reg_weight (b)
1752 int b;
1754 rtx insn, next_tail, head, tail;
1756 get_block_head_tail (b, &head, &tail);
1757 next_tail = NEXT_INSN (tail);
1759 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1761 int reg_weight = 0;
1762 rtx x;
1764 /* Handle register life information. */
1765 if (! INSN_P (insn))
1766 continue;
1768 /* Increment weight for each register born here. */
1769 x = PATTERN (insn);
1770 reg_weight += find_set_reg_weight (x);
1771 if (GET_CODE (x) == PARALLEL)
1773 int j;
1774 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
1776 x = XVECEXP (PATTERN (insn), 0, j);
1777 reg_weight += find_set_reg_weight (x);
1780 /* Decrement weight for each register that dies here. */
1781 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1783 if (REG_NOTE_KIND (x) == REG_DEAD
1784 || REG_NOTE_KIND (x) == REG_UNUSED)
1785 reg_weight--;
1788 INSN_REG_WEIGHT (insn) = reg_weight;
1792 /* Scheduling clock, modified in schedule_block() and queue_to_ready (). */
1793 static int clock_var;
1795 /* Move insns that became ready to fire from queue to ready list. */
1797 static void
1798 queue_to_ready (ready)
1799 struct ready_list *ready;
1801 rtx insn;
1802 rtx link;
1804 q_ptr = NEXT_Q (q_ptr);
1806 /* Add all pending insns that can be scheduled without stalls to the
1807 ready list. */
1808 for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
1810 insn = XEXP (link, 0);
1811 q_size -= 1;
1813 if (sched_verbose >= 2)
1814 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1815 (*current_sched_info->print_insn) (insn, 0));
1817 ready_add (ready, insn);
1818 if (sched_verbose >= 2)
1819 fprintf (sched_dump, "moving to ready without stalls\n");
1821 insn_queue[q_ptr] = 0;
1823 /* If there are no ready insns, stall until one is ready and add all
1824 of the pending insns at that point to the ready list. */
1825 if (ready->n_ready == 0)
1827 int stalls;
1829 for (stalls = 1; stalls <= MAX_INSN_QUEUE_INDEX; stalls++)
1831 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
1833 for (; link; link = XEXP (link, 1))
1835 insn = XEXP (link, 0);
1836 q_size -= 1;
1838 if (sched_verbose >= 2)
1839 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1840 (*current_sched_info->print_insn) (insn, 0));
1842 ready_add (ready, insn);
1843 if (sched_verbose >= 2)
1844 fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
1846 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = 0;
1848 advance_one_cycle ();
1850 break;
1853 advance_one_cycle ();
1856 if ((!targetm.sched.use_dfa_pipeline_interface
1857 || !(*targetm.sched.use_dfa_pipeline_interface) ())
1858 && sched_verbose && stalls)
1859 visualize_stall_cycles (stalls);
1861 q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
1862 clock_var += stalls;
1866 /* Print the ready list for debugging purposes. Callable from debugger. */
1868 static void
1869 debug_ready_list (ready)
1870 struct ready_list *ready;
1872 rtx *p;
1873 int i;
1875 if (ready->n_ready == 0)
1877 fprintf (sched_dump, "\n");
1878 return;
1881 p = ready_lastpos (ready);
1882 for (i = 0; i < ready->n_ready; i++)
1883 fprintf (sched_dump, " %s", (*current_sched_info->print_insn) (p[i], 0));
1884 fprintf (sched_dump, "\n");
1887 /* move_insn1: Remove INSN from insn chain, and link it after LAST insn. */
1889 static rtx
1890 move_insn1 (insn, last)
1891 rtx insn, last;
1893 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
1894 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
1896 NEXT_INSN (insn) = NEXT_INSN (last);
1897 PREV_INSN (NEXT_INSN (last)) = insn;
1899 NEXT_INSN (last) = insn;
1900 PREV_INSN (insn) = last;
1902 return insn;
1905 /* Search INSN for REG_SAVE_NOTE note pairs for
1906 NOTE_INSN_{LOOP,EHREGION}_{BEG,END}; and convert them back into
1907 NOTEs. The REG_SAVE_NOTE note following first one is contains the
1908 saved value for NOTE_BLOCK_NUMBER which is useful for
1909 NOTE_INSN_EH_REGION_{BEG,END} NOTEs. LAST is the last instruction
1910 output by the instruction scheduler. Return the new value of LAST. */
1912 static rtx
1913 reemit_notes (insn, last)
1914 rtx insn;
1915 rtx last;
1917 rtx note, retval;
1919 retval = last;
1920 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1922 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
1924 enum insn_note note_type = INTVAL (XEXP (note, 0));
1926 last = emit_note_before (note_type, last);
1927 remove_note (insn, note);
1928 note = XEXP (note, 1);
1929 if (note_type == NOTE_INSN_EH_REGION_BEG
1930 || note_type == NOTE_INSN_EH_REGION_END)
1931 NOTE_EH_HANDLER (last) = INTVAL (XEXP (note, 0));
1932 remove_note (insn, note);
1935 return retval;
1938 /* Move INSN. Reemit notes if needed.
1940 Return the last insn emitted by the scheduler, which is the
1941 return value from the first call to reemit_notes. */
1943 static rtx
1944 move_insn (insn, last)
1945 rtx insn, last;
1947 rtx retval = NULL;
1949 move_insn1 (insn, last);
1951 /* If this is the first call to reemit_notes, then record
1952 its return value. */
1953 if (retval == NULL_RTX)
1954 retval = reemit_notes (insn, insn);
1955 else
1956 reemit_notes (insn, insn);
1958 SCHED_GROUP_P (insn) = 0;
1960 return retval;
1963 /* The following structure describe an entry of the stack of choices. */
1964 struct choice_entry
1966 /* Ordinal number of the issued insn in the ready queue. */
1967 int index;
1968 /* The number of the rest insns whose issues we should try. */
1969 int rest;
1970 /* The number of issued essential insns. */
1971 int n;
1972 /* State after issuing the insn. */
1973 state_t state;
1976 /* The following array is used to implement a stack of choices used in
1977 function max_issue. */
1978 static struct choice_entry *choice_stack;
1980 /* The following variable value is number of essential insns issued on
1981 the current cycle. An insn is essential one if it changes the
1982 processors state. */
1983 static int cycle_issued_insns;
1985 /* The following function returns maximal (or close to maximal) number
1986 of insns which can be issued on the same cycle and one of which
1987 insns is insns with the best rank (the first insn in READY). To
1988 make this function tries different samples of ready insns. READY
1989 is current queue `ready'. Global array READY_TRY reflects what
1990 insns are already issued in this try. INDEX will contain index
1991 of the best insn in READY. The following function is used only for
1992 first cycle multipass scheduling. */
1993 static int
1994 max_issue (ready, index)
1995 struct ready_list *ready;
1996 int *index;
1998 int n, i, all, n_ready, lookahead, best, delay;
1999 struct choice_entry *top;
2000 rtx insn;
2002 lookahead = (*targetm.sched.first_cycle_multipass_dfa_lookahead) ();
2003 best = 0;
2004 memcpy (choice_stack->state, curr_state, dfa_state_size);
2005 top = choice_stack;
2006 top->rest = lookahead;
2007 top->n = 0;
2008 n_ready = ready->n_ready;
2009 for (all = i = 0; i < n_ready; i++)
2010 if (!ready_try [i])
2011 all++;
2012 i = 0;
2013 for (;;)
2015 if (top->rest == 0 || i >= n_ready)
2017 if (top == choice_stack)
2018 break;
2019 if (best < top - choice_stack && ready_try [0])
2021 best = top - choice_stack;
2022 *index = choice_stack [1].index;
2023 if (top->n == issue_rate - cycle_issued_insns || best == all)
2024 break;
2026 i = top->index;
2027 ready_try [i] = 0;
2028 top--;
2029 memcpy (curr_state, top->state, dfa_state_size);
2031 else if (!ready_try [i])
2033 insn = ready_element (ready, i);
2034 delay = state_transition (curr_state, insn);
2035 if (delay < 0)
2037 if (state_dead_lock_p (curr_state))
2038 top->rest = 0;
2039 else
2040 top->rest--;
2041 n = top->n;
2042 if (memcmp (top->state, curr_state, dfa_state_size) != 0)
2043 n++;
2044 top++;
2045 top->rest = lookahead;
2046 top->index = i;
2047 top->n = n;
2048 memcpy (top->state, curr_state, dfa_state_size);
2049 ready_try [i] = 1;
2050 i = -1;
2053 i++;
2055 while (top != choice_stack)
2057 ready_try [top->index] = 0;
2058 top--;
2060 memcpy (curr_state, choice_stack->state, dfa_state_size);
2061 return best;
2064 /* The following function chooses insn from READY and modifies
2065 *N_READY and READY. The following function is used only for first
2066 cycle multipass scheduling. */
2068 static rtx
2069 choose_ready (ready)
2070 struct ready_list *ready;
2072 if (!targetm.sched.first_cycle_multipass_dfa_lookahead
2073 || (*targetm.sched.first_cycle_multipass_dfa_lookahead) () <= 0
2074 || SCHED_GROUP_P (ready_element (ready, 0)))
2075 return ready_remove_first (ready);
2076 else
2078 /* Try to choose the better insn. */
2079 int index, i;
2080 rtx insn;
2082 insn = ready_element (ready, 0);
2083 if (INSN_CODE (insn) < 0)
2084 return ready_remove_first (ready);
2085 for (i = 1; i < ready->n_ready; i++)
2087 insn = ready_element (ready, i);
2088 ready_try [i]
2089 = (INSN_CODE (insn) < 0
2090 || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard
2091 && !(*targetm.sched.first_cycle_multipass_dfa_lookahead_guard) (insn)));
2093 if (max_issue (ready, &index) == 0)
2094 return ready_remove_first (ready);
2095 else
2096 return ready_remove (ready, index);
2100 /* Called from backends from targetm.sched.reorder to emit stuff into
2101 the instruction stream. */
2104 sched_emit_insn (pat)
2105 rtx pat;
2107 rtx insn = emit_insn_after (pat, last_scheduled_insn);
2108 last_scheduled_insn = insn;
2109 return insn;
2112 /* Use forward list scheduling to rearrange insns of block B in region RGN,
2113 possibly bringing insns from subsequent blocks in the same region. */
2115 void
2116 schedule_block (b, rgn_n_insns)
2117 int b;
2118 int rgn_n_insns;
2120 struct ready_list ready;
2121 int i, first_cycle_insn_p;
2122 int can_issue_more;
2123 state_t temp_state = NULL; /* It is used for multipass scheduling. */
2124 int sort_p, advance, start_clock_var;
2126 /* Head/tail info for this block. */
2127 rtx prev_head = current_sched_info->prev_head;
2128 rtx next_tail = current_sched_info->next_tail;
2129 rtx head = NEXT_INSN (prev_head);
2130 rtx tail = PREV_INSN (next_tail);
2132 /* We used to have code to avoid getting parameters moved from hard
2133 argument registers into pseudos.
2135 However, it was removed when it proved to be of marginal benefit
2136 and caused problems because schedule_block and compute_forward_dependences
2137 had different notions of what the "head" insn was. */
2139 if (head == tail && (! INSN_P (head)))
2140 abort ();
2142 /* Debug info. */
2143 if (sched_verbose)
2145 fprintf (sched_dump, ";; ======================================================\n");
2146 fprintf (sched_dump,
2147 ";; -- basic block %d from %d to %d -- %s reload\n",
2148 b, INSN_UID (head), INSN_UID (tail),
2149 (reload_completed ? "after" : "before"));
2150 fprintf (sched_dump, ";; ======================================================\n");
2151 fprintf (sched_dump, "\n");
2153 visualize_alloc ();
2154 init_block_visualization ();
2157 if (targetm.sched.use_dfa_pipeline_interface
2158 && (*targetm.sched.use_dfa_pipeline_interface) ())
2159 state_reset (curr_state);
2160 else
2161 clear_units ();
2163 /* Allocate the ready list. */
2164 ready.veclen = rgn_n_insns + 1 + issue_rate;
2165 ready.first = ready.veclen - 1;
2166 ready.vec = (rtx *) xmalloc (ready.veclen * sizeof (rtx));
2167 ready.n_ready = 0;
2169 if (targetm.sched.use_dfa_pipeline_interface
2170 && (*targetm.sched.use_dfa_pipeline_interface) ())
2172 /* It is used for first cycle multipass scheduling. */
2173 temp_state = alloca (dfa_state_size);
2174 ready_try = (char *) xmalloc ((rgn_n_insns + 1) * sizeof (char));
2175 memset (ready_try, 0, (rgn_n_insns + 1) * sizeof (char));
2176 choice_stack
2177 = (struct choice_entry *) xmalloc ((rgn_n_insns + 1)
2178 * sizeof (struct choice_entry));
2179 for (i = 0; i <= rgn_n_insns; i++)
2180 choice_stack[i].state = (state_t) xmalloc (dfa_state_size);
2183 (*current_sched_info->init_ready_list) (&ready);
2185 if (targetm.sched.md_init)
2186 (*targetm.sched.md_init) (sched_dump, sched_verbose, ready.veclen);
2188 /* We start inserting insns after PREV_HEAD. */
2189 last_scheduled_insn = prev_head;
2191 /* Initialize INSN_QUEUE. Q_SIZE is the total number of insns in the
2192 queue. */
2193 q_ptr = 0;
2194 q_size = 0;
2196 if (!targetm.sched.use_dfa_pipeline_interface
2197 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2198 max_insn_queue_index_macro_value = INSN_QUEUE_SIZE - 1;
2199 else
2200 max_insn_queue_index_macro_value = max_insn_queue_index;
2202 insn_queue = (rtx *) alloca ((MAX_INSN_QUEUE_INDEX + 1) * sizeof (rtx));
2203 memset ((char *) insn_queue, 0, (MAX_INSN_QUEUE_INDEX + 1) * sizeof (rtx));
2204 last_clock_var = -1;
2206 /* Start just before the beginning of time. */
2207 clock_var = -1;
2208 advance = 0;
2210 sort_p = TRUE;
2211 /* Loop until all the insns in BB are scheduled. */
2212 while ((*current_sched_info->schedule_more_p) ())
2216 start_clock_var = clock_var;
2218 clock_var++;
2220 advance_one_cycle ();
2222 /* Add to the ready list all pending insns that can be issued now.
2223 If there are no ready insns, increment clock until one
2224 is ready and add all pending insns at that point to the ready
2225 list. */
2226 queue_to_ready (&ready);
2228 if (ready.n_ready == 0)
2229 abort ();
2231 if (sched_verbose >= 2)
2233 fprintf (sched_dump, ";;\t\tReady list after queue_to_ready: ");
2234 debug_ready_list (&ready);
2236 advance -= clock_var - start_clock_var;
2238 while (advance > 0);
2240 if (sort_p)
2242 /* Sort the ready list based on priority. */
2243 ready_sort (&ready);
2245 if (sched_verbose >= 2)
2247 fprintf (sched_dump, ";;\t\tReady list after ready_sort: ");
2248 debug_ready_list (&ready);
2252 /* Allow the target to reorder the list, typically for
2253 better instruction bundling. */
2254 if (targetm.sched.reorder
2255 && (ready.n_ready == 0
2256 || !SCHED_GROUP_P (ready_element (&ready, 0))))
2257 can_issue_more =
2258 (*targetm.sched.reorder) (sched_dump, sched_verbose,
2259 ready_lastpos (&ready),
2260 &ready.n_ready, clock_var);
2261 else
2262 can_issue_more = issue_rate;
2264 first_cycle_insn_p = 1;
2265 cycle_issued_insns = 0;
2266 for (;;)
2268 rtx insn;
2269 int cost;
2271 if (sched_verbose >= 2)
2273 fprintf (sched_dump, ";;\tReady list (t =%3d): ",
2274 clock_var);
2275 debug_ready_list (&ready);
2278 if (!targetm.sched.use_dfa_pipeline_interface
2279 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2281 if (ready.n_ready == 0 || !can_issue_more
2282 || !(*current_sched_info->schedule_more_p) ())
2283 break;
2284 insn = choose_ready (&ready);
2285 cost = actual_hazard (insn_unit (insn), insn, clock_var, 0);
2287 else
2289 if (ready.n_ready == 0 || !can_issue_more
2290 || state_dead_lock_p (curr_state)
2291 || !(*current_sched_info->schedule_more_p) ())
2292 break;
2294 /* Select and remove the insn from the ready list. */
2295 if (sort_p)
2296 insn = choose_ready (&ready);
2297 else
2298 insn = ready_remove_first (&ready);
2300 if (targetm.sched.dfa_new_cycle
2301 && (*targetm.sched.dfa_new_cycle) (sched_dump, sched_verbose,
2302 insn, last_clock_var,
2303 clock_var, &sort_p))
2305 ready_add (&ready, insn);
2306 break;
2309 sort_p = TRUE;
2310 memcpy (temp_state, curr_state, dfa_state_size);
2311 if (recog_memoized (insn) < 0)
2313 if (!first_cycle_insn_p
2314 && (GET_CODE (PATTERN (insn)) == ASM_INPUT
2315 || asm_noperands (PATTERN (insn)) >= 0))
2316 /* This is asm insn which is tryed to be issued on the
2317 cycle not first. Issue it on the next cycle. */
2318 cost = 1;
2319 else
2320 /* A USE insn, or something else we don't need to
2321 understand. We can't pass these directly to
2322 state_transition because it will trigger a
2323 fatal error for unrecognizable insns. */
2324 cost = 0;
2326 else
2328 cost = state_transition (temp_state, insn);
2330 if (targetm.sched.first_cycle_multipass_dfa_lookahead
2331 && targetm.sched.dfa_bubble)
2333 if (cost == 0)
2335 int j;
2336 rtx bubble;
2338 for (j = 0;
2339 (bubble = (*targetm.sched.dfa_bubble) (j))
2340 != NULL_RTX;
2341 j++)
2343 memcpy (temp_state, curr_state, dfa_state_size);
2345 if (state_transition (temp_state, bubble) < 0
2346 && state_transition (temp_state, insn) < 0)
2347 break;
2350 if (bubble != NULL_RTX)
2352 if (insert_schedule_bubbles_p)
2354 rtx copy;
2356 copy = copy_rtx (PATTERN (bubble));
2357 emit_insn_after (copy, last_scheduled_insn);
2358 last_scheduled_insn
2359 = NEXT_INSN (last_scheduled_insn);
2360 INSN_CODE (last_scheduled_insn)
2361 = INSN_CODE (bubble);
2363 /* Annotate the same for the first insns
2364 scheduling by using mode. */
2365 PUT_MODE (last_scheduled_insn,
2366 (clock_var > last_clock_var
2367 ? clock_var - last_clock_var
2368 : VOIDmode));
2369 last_clock_var = clock_var;
2371 if (sched_verbose >= 2)
2373 fprintf (sched_dump,
2374 ";;\t\t--> scheduling bubble insn <<<%d>>>:reservation ",
2375 INSN_UID (last_scheduled_insn));
2377 if (recog_memoized (last_scheduled_insn)
2378 < 0)
2379 fprintf (sched_dump, "nothing");
2380 else
2381 print_reservation
2382 (sched_dump, last_scheduled_insn);
2384 fprintf (sched_dump, "\n");
2387 cost = -1;
2392 if (cost < 0)
2393 cost = 0;
2394 else if (cost == 0)
2395 cost = 1;
2400 if (cost >= 1)
2402 queue_insn (insn, cost);
2403 continue;
2406 if (! (*current_sched_info->can_schedule_ready_p) (insn))
2407 goto next;
2409 last_scheduled_insn = move_insn (insn, last_scheduled_insn);
2411 if (targetm.sched.use_dfa_pipeline_interface
2412 && (*targetm.sched.use_dfa_pipeline_interface) ())
2414 if (memcmp (curr_state, temp_state, dfa_state_size) != 0)
2415 cycle_issued_insns++;
2416 memcpy (curr_state, temp_state, dfa_state_size);
2419 if (targetm.sched.variable_issue)
2420 can_issue_more =
2421 (*targetm.sched.variable_issue) (sched_dump, sched_verbose,
2422 insn, can_issue_more);
2423 /* A naked CLOBBER or USE generates no instruction, so do
2424 not count them against the issue rate. */
2425 else if (GET_CODE (PATTERN (insn)) != USE
2426 && GET_CODE (PATTERN (insn)) != CLOBBER)
2427 can_issue_more--;
2429 advance = schedule_insn (insn, &ready, clock_var);
2430 if (advance != 0)
2431 break;
2433 next:
2434 first_cycle_insn_p = 0;
2436 /* Sort the ready list based on priority. This must be
2437 redone here, as schedule_insn may have readied additional
2438 insns that will not be sorted correctly. */
2439 if (ready.n_ready > 0)
2440 ready_sort (&ready);
2442 if (targetm.sched.reorder2
2443 && (ready.n_ready == 0
2444 || !SCHED_GROUP_P (ready_element (&ready, 0))))
2446 can_issue_more =
2447 (*targetm.sched.reorder2) (sched_dump, sched_verbose,
2448 ready.n_ready
2449 ? ready_lastpos (&ready) : NULL,
2450 &ready.n_ready, clock_var);
2454 if ((!targetm.sched.use_dfa_pipeline_interface
2455 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2456 && sched_verbose)
2457 /* Debug info. */
2458 visualize_scheduled_insns (clock_var);
2461 if (targetm.sched.md_finish)
2462 (*targetm.sched.md_finish) (sched_dump, sched_verbose);
2464 /* Debug info. */
2465 if (sched_verbose)
2467 fprintf (sched_dump, ";;\tReady list (final): ");
2468 debug_ready_list (&ready);
2469 if (!targetm.sched.use_dfa_pipeline_interface
2470 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2471 print_block_visualization ("");
2474 /* Sanity check -- queue must be empty now. Meaningless if region has
2475 multiple bbs. */
2476 if (current_sched_info->queue_must_finish_empty && q_size != 0)
2477 abort ();
2479 /* Update head/tail boundaries. */
2480 head = NEXT_INSN (prev_head);
2481 tail = last_scheduled_insn;
2483 if (!reload_completed)
2485 rtx insn, link, next;
2487 /* INSN_TICK (minimum clock tick at which the insn becomes
2488 ready) may be not correct for the insn in the subsequent
2489 blocks of the region. We should use a correct value of
2490 `clock_var' or modify INSN_TICK. It is better to keep
2491 clock_var value equal to 0 at the start of a basic block.
2492 Therefore we modify INSN_TICK here. */
2493 for (insn = head; insn != tail; insn = NEXT_INSN (insn))
2494 if (INSN_P (insn))
2496 for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
2498 next = XEXP (link, 0);
2499 INSN_TICK (next) -= clock_var;
2504 /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
2505 previously found among the insns. Insert them at the beginning
2506 of the insns. */
2507 if (note_list != 0)
2509 rtx note_head = note_list;
2511 while (PREV_INSN (note_head))
2513 note_head = PREV_INSN (note_head);
2516 PREV_INSN (note_head) = PREV_INSN (head);
2517 NEXT_INSN (PREV_INSN (head)) = note_head;
2518 PREV_INSN (head) = note_list;
2519 NEXT_INSN (note_list) = head;
2520 head = note_head;
2523 /* Debugging. */
2524 if (sched_verbose)
2526 fprintf (sched_dump, ";; total time = %d\n;; new head = %d\n",
2527 clock_var, INSN_UID (head));
2528 fprintf (sched_dump, ";; new tail = %d\n\n",
2529 INSN_UID (tail));
2530 visualize_free ();
2533 current_sched_info->head = head;
2534 current_sched_info->tail = tail;
2536 free (ready.vec);
2538 if (targetm.sched.use_dfa_pipeline_interface
2539 && (*targetm.sched.use_dfa_pipeline_interface) ())
2541 free (ready_try);
2542 for (i = 0; i <= rgn_n_insns; i++)
2543 free (choice_stack [i].state);
2544 free (choice_stack);
2548 /* Set_priorities: compute priority of each insn in the block. */
2551 set_priorities (head, tail)
2552 rtx head, tail;
2554 rtx insn;
2555 int n_insn;
2557 rtx prev_head;
2559 prev_head = PREV_INSN (head);
2561 if (head == tail && (! INSN_P (head)))
2562 return 0;
2564 n_insn = 0;
2565 for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
2567 if (GET_CODE (insn) == NOTE)
2568 continue;
2570 n_insn++;
2571 (void) priority (insn);
2574 return n_insn;
2577 /* Initialize some global state for the scheduler. DUMP_FILE is to be used
2578 for debugging output. */
2580 void
2581 sched_init (dump_file)
2582 FILE *dump_file;
2584 int luid;
2585 basic_block b;
2586 rtx insn;
2587 int i;
2589 /* Disable speculative loads in their presence if cc0 defined. */
2590 #ifdef HAVE_cc0
2591 flag_schedule_speculative_load = 0;
2592 #endif
2594 /* Set dump and sched_verbose for the desired debugging output. If no
2595 dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
2596 For -fsched-verbose=N, N>=10, print everything to stderr. */
2597 sched_verbose = sched_verbose_param;
2598 if (sched_verbose_param == 0 && dump_file)
2599 sched_verbose = 1;
2600 sched_dump = ((sched_verbose_param >= 10 || !dump_file)
2601 ? stderr : dump_file);
2603 /* Initialize issue_rate. */
2604 if (targetm.sched.issue_rate)
2605 issue_rate = (*targetm.sched.issue_rate) ();
2606 else
2607 issue_rate = 1;
2609 /* We use LUID 0 for the fake insn (UID 0) which holds dependencies for
2610 pseudos which do not cross calls. */
2611 old_max_uid = get_max_uid () + 1;
2613 h_i_d = (struct haifa_insn_data *) xcalloc (old_max_uid, sizeof (*h_i_d));
2615 for (i = 0; i < old_max_uid; i++)
2616 h_i_d [i].cost = -1;
2618 if (targetm.sched.use_dfa_pipeline_interface
2619 && (*targetm.sched.use_dfa_pipeline_interface) ())
2621 if (targetm.sched.init_dfa_pre_cycle_insn)
2622 (*targetm.sched.init_dfa_pre_cycle_insn) ();
2624 if (targetm.sched.init_dfa_post_cycle_insn)
2625 (*targetm.sched.init_dfa_post_cycle_insn) ();
2627 if (targetm.sched.first_cycle_multipass_dfa_lookahead
2628 && targetm.sched.init_dfa_bubbles)
2629 (*targetm.sched.init_dfa_bubbles) ();
2631 dfa_start ();
2632 dfa_state_size = state_size ();
2633 curr_state = xmalloc (dfa_state_size);
2636 h_i_d[0].luid = 0;
2637 luid = 1;
2638 FOR_EACH_BB (b)
2639 for (insn = b->head;; insn = NEXT_INSN (insn))
2641 INSN_LUID (insn) = luid;
2643 /* Increment the next luid, unless this is a note. We don't
2644 really need separate IDs for notes and we don't want to
2645 schedule differently depending on whether or not there are
2646 line-number notes, i.e., depending on whether or not we're
2647 generating debugging information. */
2648 if (GET_CODE (insn) != NOTE)
2649 ++luid;
2651 if (insn == b->end)
2652 break;
2655 init_dependency_caches (luid);
2657 init_alias_analysis ();
2659 if (write_symbols != NO_DEBUG)
2661 rtx line;
2663 line_note_head = (rtx *) xcalloc (last_basic_block, sizeof (rtx));
2665 /* Save-line-note-head:
2666 Determine the line-number at the start of each basic block.
2667 This must be computed and saved now, because after a basic block's
2668 predecessor has been scheduled, it is impossible to accurately
2669 determine the correct line number for the first insn of the block. */
2671 FOR_EACH_BB (b)
2673 for (line = b->head; line; line = PREV_INSN (line))
2674 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
2676 line_note_head[b->index] = line;
2677 break;
2679 /* Do a forward search as well, since we won't get to see the first
2680 notes in a basic block. */
2681 for (line = b->head; line; line = NEXT_INSN (line))
2683 if (INSN_P (line))
2684 break;
2685 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
2686 line_note_head[b->index] = line;
2691 if ((!targetm.sched.use_dfa_pipeline_interface
2692 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2693 && sched_verbose)
2694 /* Find units used in this function, for visualization. */
2695 init_target_units ();
2697 /* ??? Add a NOTE after the last insn of the last basic block. It is not
2698 known why this is done. */
2700 insn = EXIT_BLOCK_PTR->prev_bb->end;
2701 if (NEXT_INSN (insn) == 0
2702 || (GET_CODE (insn) != NOTE
2703 && GET_CODE (insn) != CODE_LABEL
2704 /* Don't emit a NOTE if it would end up before a BARRIER. */
2705 && GET_CODE (NEXT_INSN (insn)) != BARRIER))
2707 emit_note_after (NOTE_INSN_DELETED, EXIT_BLOCK_PTR->prev_bb->end);
2708 /* Make insn to appear outside BB. */
2709 EXIT_BLOCK_PTR->prev_bb->end = PREV_INSN (EXIT_BLOCK_PTR->prev_bb->end);
2712 /* Compute INSN_REG_WEIGHT for all blocks. We must do this before
2713 removing death notes. */
2714 FOR_EACH_BB_REVERSE (b)
2715 find_insn_reg_weight (b->index);
2718 /* Free global data used during insn scheduling. */
2720 void
2721 sched_finish ()
2723 free (h_i_d);
2725 if (targetm.sched.use_dfa_pipeline_interface
2726 && (*targetm.sched.use_dfa_pipeline_interface) ())
2728 free (curr_state);
2729 dfa_finish ();
2731 free_dependency_caches ();
2732 end_alias_analysis ();
2733 if (write_symbols != NO_DEBUG)
2734 free (line_note_head);
2736 #endif /* INSN_SCHEDULING */