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
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
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
69 2. choose insn with least contribution to register pressure,
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
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
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
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,
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. */
137 #include "coretypes.h"
142 #include "hard-reg-set.h"
143 #include "basic-block.h"
145 #include "function.h"
147 #include "insn-config.h"
148 #include "insn-attr.h"
152 #include "sched-int.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).
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. */
192 fix_sched_param (param
, val
)
193 const char *param
, *val
;
195 if (!strcmp (param
, "verbose"))
196 sched_verbose_param
= atoi (val
);
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
;
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
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
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
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. */
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
298 N_READY determines how many insns are on the ready list. */
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. */
322 may_trap_exp (x
, is_store
)
333 if (code
== MEM
&& may_trap_p (x
))
340 /* The insn uses memory: a volatile load. */
341 if (MEM_VOLATILE_P (x
))
343 /* An exception-free load. */
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
;
355 int i
, insn_class
= TRAP_FREE
;
357 /* Neither store nor load, check if it may cause a trap. */
360 /* Recursive step: walk the insn... */
361 fmt
= GET_RTX_FORMAT (code
);
362 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
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')
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
)
380 if (insn_class
== TRAP_RISKY
|| insn_class
== IRISKY
)
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
)
400 rtx pat
= PATTERN (insn
);
401 int tmp_class
= TRAP_FREE
;
402 int insn_class
= TRAP_FREE
;
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
));
415 /* Test if it is a 'store'. */
416 tmp_class
= may_trap_exp (XEXP (XVECEXP (pat
, 0, i
), 0), 1);
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
)
423 /* Test if it is a load. */
425 = WORST_CLASS (tmp_class
,
426 may_trap_exp (SET_SRC (XVECEXP (pat
, 0, i
)),
431 tmp_class
= TRAP_RISKY
;
436 insn_class
= WORST_CLASS (insn_class
, tmp_class
);
437 if (insn_class
== TRAP_RISKY
|| insn_class
== IRISKY
)
443 code
= GET_CODE (pat
);
447 /* Test if it is a 'store'. */
448 tmp_class
= may_trap_exp (XEXP (pat
, 0), 1);
451 /* Test if it is a store. */
452 tmp_class
= may_trap_exp (SET_DEST (pat
), 1);
453 if (tmp_class
== TRAP_RISKY
)
455 /* Test if it is a load. */
457 WORST_CLASS (tmp_class
,
458 may_trap_exp (SET_SRC (pat
), 0));
462 tmp_class
= TRAP_RISKY
;
466 insn_class
= tmp_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
545 schedule_insns (dump_file
)
546 FILE *dump_file ATTRIBUTE_UNUSED
;
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. */
568 int unit
= INSN_UNIT (insn
);
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)
581 unit
= function_units_used (insn
);
582 /* Increment non-negative values so we can cache zero. */
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
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
603 HAIFA_INLINE
static unsigned int
604 blockage_range (unit
, insn
)
608 unsigned int blockage
= INSN_BLOCKAGE (insn
);
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
616 if (HOST_BITS_PER_INT
>= UNIT_BITS
+ 2 * BLOCKAGE_BITS
)
617 INSN_BLOCKAGE (insn
) = ENCODE_BLOCKAGE (unit
+ 1, range
);
620 range
= BLOCKAGE_RANGE (blockage
);
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
];
632 static rtx unit_last_insn
[1];
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
639 #if FUNCTION_UNITS_SIZE
640 static int unit_tick
[FUNCTION_UNITS_SIZE
* MAX_MULTIPLICITY
];
642 static int unit_tick
[1];
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
];
651 static int unit_n_insns
[1];
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
)
662 return unit_last_insn
[instance
];
665 /* Reset the function unit state to the null state. */
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. */
679 insn_issue_delay (insn
)
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
691 if (function_units
[unit
].blockage_range_function
&&
692 function_units
[unit
].blockage_function
)
693 delay
= function_units
[unit
].blockage_function (insn
, insn
);
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
));
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. */
710 actual_hazard_this_instance (unit
, instance
, insn
, clock
, cost
)
711 int unit
, instance
, clock
, cost
;
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
);
734 tick
+= ((int) MAX_BLOCKAGE_COST (blockage_range (unit
, insn
))
735 - function_units
[unit
].max_blockage
);
737 if (tick
- clock
> 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
)
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))
764 instance
+= FUNCTION_UNITS_SIZE
;
767 unit_last_insn
[instance
] = insn
;
768 unit_tick
[instance
] = (clock
+ function_units
[unit
].max_blockage
);
771 for (i
= 0, unit
= ~unit
; unit
; i
++, unit
>>= 1)
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
;
790 /* Find the instance of the function unit with the minimum hazard. */
792 int best_cost
= actual_hazard_this_instance (unit
, instance
, insn
,
794 #if MAX_MULTIPLICITY > 1
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
,
804 if (this_cost
< best_cost
)
806 best_cost
= this_cost
;
807 if (this_cost
<= cost
)
813 cost
= MAX (cost
, best_cost
);
816 for (i
= 0, unit
= ~unit
; unit
; i
++, unit
>>= 1)
818 cost
= actual_hazard (i
, insn
, clock
, 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
)
838 unsigned int minb
, maxb
;
842 minb
= maxb
= function_units
[unit
].max_blockage
;
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
);
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
;
866 for (i
= 0, unit
= ~unit
; unit
; i
++, unit
>>= 1)
868 cost
= potential_hazard (i
, insn
, 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. */
878 insn_cost (insn
, link
, used
)
879 rtx insn
, link
, used
;
881 int cost
= INSN_COST (insn
);
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;
896 if (targetm
.sched
.use_dfa_pipeline_interface
897 && (*targetm
.sched
.use_dfa_pipeline_interface
) ())
898 cost
= insn_default_latency (insn
);
900 cost
= result_ready_cost (insn
);
905 INSN_COST (insn
) = cost
;
909 /* In this case estimate cost without caring how insn is used. */
910 if (link
== 0 || used
== 0)
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)
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
)
927 else if (REG_NOTE_KIND (link
) == REG_DEP_OUTPUT
)
929 cost
= (insn_default_latency (insn
)
930 - insn_default_latency (used
));
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
);
949 /* Compute the priority number for INSN. */
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);
968 for (link
= INSN_DEPEND (insn
); link
; link
= XEXP (link
, 1))
973 if (RTX_INTEGRATED_P (link
))
976 next
= XEXP (link
, 0);
978 /* Critical path is meaningful in block boundaries only. */
979 if (! (*current_sched_info
->contributes_to_priority
) (next
, insn
))
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); } \
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
1009 rank_for_schedule (x
, y
)
1013 rtx tmp
= *(const rtx
*) y
;
1014 rtx tmp2
= *(const rtx
*) x
;
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
);
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
)))
1034 info_val
= (*current_sched_info
->rank
) (tmp
, tmp2
);
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)
1049 else if (REG_NOTE_KIND (link
) == 0) /* Data dependence. */
1054 link
= find_insn_list (tmp2
, INSN_DEPEND (last_scheduled_insn
));
1055 if (link
== 0 || insn_cost (last_scheduled_insn
, link
, tmp2
) == 1)
1057 else if (REG_NOTE_KIND (link
) == 0) /* Data dependence. */
1062 if ((val
= tmp2_class
- tmp_class
))
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. */
1070 for (link
= INSN_DEPEND (tmp
); link
; link
= XEXP (link
, 1))
1074 for (link
= INSN_DEPEND (tmp2
); link
; link
= XEXP (link
, 1))
1077 val
= depend_count2
- depend_count1
;
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
1094 rtx insn
= a
[n
- 1];
1097 while (i
>= 0 && rank_for_schedule (a
+ i
, &insn
) >= 0)
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
)
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
;
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)
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
1144 ready_add (ready
, insn
)
1145 struct ready_list
*ready
;
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
;
1159 /* Remove the element with the highest priority from the ready list and
1162 HAIFA_INLINE
static rtx
1163 ready_remove_first (ready
)
1164 struct ready_list
*ready
;
1167 if (ready
->n_ready
== 0)
1169 t
= ready
->vec
[ready
->first
--];
1171 /* If the queue becomes empty, reset it. */
1172 if (ready
->n_ready
== 0)
1173 ready
->first
= ready
->veclen
- 1;
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
1185 HAIFA_INLINE
static rtx
1186 ready_element (ready
, index
)
1187 struct ready_list
*ready
;
1190 #ifdef ENABLE_CHECKING
1191 if (ready
->n_ready
== 0 || index
>= ready
->n_ready
)
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
1201 HAIFA_INLINE
static rtx
1202 ready_remove (ready
, index
)
1203 struct ready_list
*ready
;
1210 return ready_remove_first (ready
);
1211 if (ready
->n_ready
== 0 || index
>= ready
->n_ready
)
1213 t
= ready
->vec
[ready
->first
- index
];
1215 for (i
= index
; i
< ready
->n_ready
; i
++)
1216 ready
->vec
[ready
->first
- i
] = ready
->vec
[ready
->first
- i
- 1];
1221 /* Sort the ready list READY by ascending priority, using the SCHED_SORT
1224 HAIFA_INLINE
static void
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
)
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). */
1281 schedule_insn (insn
, ready
, clock
)
1283 struct ready_list
*ready
;
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)
1300 print_insn (buf
, insn
, 0);
1302 fprintf (sched_dump
, ";;\t%3i--> %-40s:", clock
, buf
);
1304 if (recog_memoized (insn
) < 0)
1305 fprintf (sched_dump
, "nothing");
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 ",
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)
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
))
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");
1354 fprintf (sched_dump
, "into queue with cost=%d\n",
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
);
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
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
;
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. */
1396 unlink_other_notes (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. */
1406 NEXT_INSN (prev
) = 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
;
1420 NEXT_INSN (note_list
) = insn
;
1429 /* Delete line notes beginning with INSN. Record line-number notes so
1430 they can be reused. Returns the insn following the notes. */
1433 unlink_line_notes (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. */
1446 NEXT_INSN (prev
) = next
;
1448 PREV_INSN (next
) = prev
;
1450 /* Record line-number notes so they can be reused. */
1451 LINE_NOTE (insn
) = insn
;
1461 /* Return the head and tail pointers of BB. */
1464 get_block_head_tail (b
, headp
, 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
);
1491 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ]. */
1494 no_real_insns_p (head
, tail
)
1497 while (head
!= NEXT_INSN (tail
))
1499 if (GET_CODE (head
) != NOTE
&& GET_CODE (head
) != CODE_LABEL
)
1501 head
= NEXT_INSN (head
);
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. */
1511 rm_line_notes (head
, tail
)
1517 next_tail
= NEXT_INSN (tail
);
1518 for (insn
= head
; insn
!= next_tail
; insn
= NEXT_INSN (insn
))
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
)
1528 insn
= unlink_line_notes (insn
, next_tail
);
1534 if (insn
== next_tail
)
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. */
1544 save_line_notes (b
, head
, 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
];
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)
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
1572 restore_line_notes (head
, tail
)
1575 rtx line
, note
, prev
, new;
1576 int added_notes
= 0;
1577 rtx next_tail
, insn
;
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)
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)
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
1606 || NOTE_LINE_NUMBER (note
) != NOTE_LINE_NUMBER (line
)
1607 || NOTE_SOURCE_FILE (note
) != NOTE_SOURCE_FILE (line
)))
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
;
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
1636 rm_redundant_line_notes ()
1639 rtx insn
= get_insns ();
1640 int active_insn
= 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)
1653 NOTE_SOURCE_FILE (insn
) = 0;
1654 NOTE_LINE_NUMBER (insn
) = NOTE_INSN_DELETED
;
1656 /* If the line number is unchanged, LINE is redundant. */
1658 && NOTE_LINE_NUMBER (line
) == NOTE_LINE_NUMBER (insn
)
1659 && NOTE_SOURCE_FILE (line
) == NOTE_SOURCE_FILE (insn
))
1662 NOTE_SOURCE_FILE (line
) = 0;
1663 NOTE_LINE_NUMBER (line
) = NOTE_INSN_DELETED
;
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
))))
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. */
1685 rm_other_notes (head
, tail
)
1693 if (head
== tail
&& (! INSN_P (head
)))
1696 next_tail
= NEXT_INSN (tail
);
1697 for (insn
= head
; insn
!= next_tail
; insn
= NEXT_INSN (insn
))
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
)
1708 insn
= unlink_other_notes (insn
, next_tail
);
1714 if (insn
== next_tail
)
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. */
1727 find_set_reg_weight (x
)
1730 if (GET_CODE (x
) == CLOBBER
1731 && register_operand (SET_DEST (x
), VOIDmode
))
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
)))
1748 /* Calculate INSN_REG_WEIGHT for all insns of a block. */
1751 find_insn_reg_weight (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
))
1764 /* Handle register life information. */
1765 if (! INSN_P (insn
))
1768 /* Increment weight for each register born here. */
1770 reg_weight
+= find_set_reg_weight (x
);
1771 if (GET_CODE (x
) == PARALLEL
)
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
)
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. */
1798 queue_to_ready (ready
)
1799 struct ready_list
*ready
;
1804 q_ptr
= NEXT_Q (q_ptr
);
1806 /* Add all pending insns that can be scheduled without stalls to the
1808 for (link
= insn_queue
[q_ptr
]; link
; link
= XEXP (link
, 1))
1810 insn
= XEXP (link
, 0);
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)
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);
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 ();
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. */
1869 debug_ready_list (ready
)
1870 struct ready_list
*ready
;
1875 if (ready
->n_ready
== 0)
1877 fprintf (sched_dump
, "\n");
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. */
1890 move_insn1 (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
;
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. */
1913 reemit_notes (insn
, 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
);
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. */
1944 move_insn (insn
, last
)
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
);
1956 reemit_notes (insn
, insn
);
1958 SCHED_GROUP_P (insn
) = 0;
1963 /* The following structure describe an entry of the stack of choices. */
1966 /* Ordinal number of the issued insn in the ready queue. */
1968 /* The number of the rest insns whose issues we should try. */
1970 /* The number of issued essential insns. */
1972 /* State after issuing the insn. */
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. */
1994 max_issue (ready
, index
)
1995 struct ready_list
*ready
;
1998 int n
, i
, all
, n_ready
, lookahead
, best
, delay
;
1999 struct choice_entry
*top
;
2002 lookahead
= (*targetm
.sched
.first_cycle_multipass_dfa_lookahead
) ();
2004 memcpy (choice_stack
->state
, curr_state
, dfa_state_size
);
2006 top
->rest
= lookahead
;
2008 n_ready
= ready
->n_ready
;
2009 for (all
= i
= 0; i
< n_ready
; i
++)
2015 if (top
->rest
== 0 || i
>= n_ready
)
2017 if (top
== choice_stack
)
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
)
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
);
2037 if (state_dead_lock_p (curr_state
))
2042 if (memcmp (top
->state
, curr_state
, dfa_state_size
) != 0)
2045 top
->rest
= lookahead
;
2048 memcpy (top
->state
, curr_state
, dfa_state_size
);
2055 while (top
!= choice_stack
)
2057 ready_try
[top
->index
] = 0;
2060 memcpy (curr_state
, choice_stack
->state
, dfa_state_size
);
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. */
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
);
2078 /* Try to choose the better 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
);
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
);
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
)
2107 rtx insn
= emit_insn_after (pat
, last_scheduled_insn
);
2108 last_scheduled_insn
= 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. */
2116 schedule_block (b
, rgn_n_insns
)
2120 struct ready_list ready
;
2121 int i
, first_cycle_insn_p
;
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
)))
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");
2154 init_block_visualization ();
2157 if (targetm
.sched
.use_dfa_pipeline_interface
2158 && (*targetm
.sched
.use_dfa_pipeline_interface
) ())
2159 state_reset (curr_state
);
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
));
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));
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
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;
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. */
2211 /* Loop until all the insns in BB are scheduled. */
2212 while ((*current_sched_info
->schedule_more_p
) ())
2216 start_clock_var
= 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
2226 queue_to_ready (&ready
);
2228 if (ready
.n_ready
== 0)
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);
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))))
2258 (*targetm
.sched
.reorder
) (sched_dump
, sched_verbose
,
2259 ready_lastpos (&ready
),
2260 &ready
.n_ready
, clock_var
);
2262 can_issue_more
= issue_rate
;
2264 first_cycle_insn_p
= 1;
2265 cycle_issued_insns
= 0;
2271 if (sched_verbose
>= 2)
2273 fprintf (sched_dump
, ";;\tReady list (t =%3d): ",
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
) ())
2284 insn
= choose_ready (&ready
);
2285 cost
= actual_hazard (insn_unit (insn
), insn
, clock_var
, 0);
2289 if (ready
.n_ready
== 0 || !can_issue_more
2290 || state_dead_lock_p (curr_state
)
2291 || !(*current_sched_info
->schedule_more_p
) ())
2294 /* Select and remove the insn from the ready list. */
2296 insn
= choose_ready (&ready
);
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
);
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. */
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. */
2328 cost
= state_transition (temp_state
, insn
);
2330 if (targetm
.sched
.first_cycle_multipass_dfa_lookahead
2331 && targetm
.sched
.dfa_bubble
)
2339 (bubble
= (*targetm
.sched
.dfa_bubble
) (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)
2350 if (bubble
!= NULL_RTX
)
2352 if (insert_schedule_bubbles_p
)
2356 copy
= copy_rtx (PATTERN (bubble
));
2357 emit_insn_after (copy
, 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
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
)
2379 fprintf (sched_dump
, "nothing");
2382 (sched_dump
, last_scheduled_insn
);
2384 fprintf (sched_dump
, "\n");
2402 queue_insn (insn
, cost
);
2406 if (! (*current_sched_info
->can_schedule_ready_p
) (insn
))
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
)
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
)
2429 advance
= schedule_insn (insn
, &ready
, clock_var
);
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))))
2447 (*targetm
.sched
.reorder2
) (sched_dump
, sched_verbose
,
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
) ())
2458 visualize_scheduled_insns (clock_var
);
2461 if (targetm
.sched
.md_finish
)
2462 (*targetm
.sched
.md_finish
) (sched_dump
, 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
2476 if (current_sched_info
->queue_must_finish_empty
&& q_size
!= 0)
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
))
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
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
;
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",
2533 current_sched_info
->head
= head
;
2534 current_sched_info
->tail
= tail
;
2538 if (targetm
.sched
.use_dfa_pipeline_interface
2539 && (*targetm
.sched
.use_dfa_pipeline_interface
) ())
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
)
2559 prev_head
= PREV_INSN (head
);
2561 if (head
== tail
&& (! INSN_P (head
)))
2565 for (insn
= tail
; insn
!= prev_head
; insn
= PREV_INSN (insn
))
2567 if (GET_CODE (insn
) == NOTE
)
2571 (void) priority (insn
);
2577 /* Initialize some global state for the scheduler. DUMP_FILE is to be used
2578 for debugging output. */
2581 sched_init (dump_file
)
2589 /* Disable speculative loads in their presence if cc0 defined. */
2591 flag_schedule_speculative_load
= 0;
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
)
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
) ();
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
) ();
2632 dfa_state_size
= state_size ();
2633 curr_state
= xmalloc (dfa_state_size
);
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
)
2655 init_dependency_caches (luid
);
2657 init_alias_analysis ();
2659 if (write_symbols
!= NO_DEBUG
)
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. */
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
;
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
))
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
) ())
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. */
2725 if (targetm
.sched
.use_dfa_pipeline_interface
2726 && (*targetm
.sched
.use_dfa_pipeline_interface
) ())
2731 free_dependency_caches ();
2732 end_alias_analysis ();
2733 if (write_symbols
!= NO_DEBUG
)
2734 free (line_note_head
);
2736 #endif /* INSN_SCHEDULING */