Add an UNSPEC_PROLOGUE_USE to prevent the link register from being considered dead.
[official-gcc.git] / gcc / haifa-sched.c
blobc5b53cffd54910b7da9455742f5413bfd23328d4
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 /* Forward declarations. */
310 /* The scheduler using only DFA description should never use the
311 following five functions: */
312 static unsigned int blockage_range PARAMS ((int, rtx));
313 static void clear_units PARAMS ((void));
314 static void schedule_unit PARAMS ((int, rtx, int));
315 static int actual_hazard PARAMS ((int, rtx, int, int));
316 static int potential_hazard PARAMS ((int, rtx, int));
318 static int priority PARAMS ((rtx));
319 static int rank_for_schedule PARAMS ((const PTR, const PTR));
320 static void swap_sort PARAMS ((rtx *, int));
321 static void queue_insn PARAMS ((rtx, int));
322 static void schedule_insn PARAMS ((rtx, struct ready_list *, int));
323 static int find_set_reg_weight PARAMS ((rtx));
324 static void find_insn_reg_weight PARAMS ((int));
325 static void adjust_priority PARAMS ((rtx));
326 static void advance_one_cycle PARAMS ((void));
328 /* Notes handling mechanism:
329 =========================
330 Generally, NOTES are saved before scheduling and restored after scheduling.
331 The scheduler distinguishes between three types of notes:
333 (1) LINE_NUMBER notes, generated and used for debugging. Here,
334 before scheduling a region, a pointer to the LINE_NUMBER note is
335 added to the insn following it (in save_line_notes()), and the note
336 is removed (in rm_line_notes() and unlink_line_notes()). After
337 scheduling the region, this pointer is used for regeneration of
338 the LINE_NUMBER note (in restore_line_notes()).
340 (2) LOOP_BEGIN, LOOP_END, SETJMP, EHREGION_BEG, EHREGION_END notes:
341 Before scheduling a region, a pointer to the note is added to the insn
342 that follows or precedes it. (This happens as part of the data dependence
343 computation). After scheduling an insn, the pointer contained in it is
344 used for regenerating the corresponding note (in reemit_notes).
346 (3) All other notes (e.g. INSN_DELETED): Before scheduling a block,
347 these notes are put in a list (in rm_other_notes() and
348 unlink_other_notes ()). After scheduling the block, these notes are
349 inserted at the beginning of the block (in schedule_block()). */
351 static rtx unlink_other_notes PARAMS ((rtx, rtx));
352 static rtx unlink_line_notes PARAMS ((rtx, rtx));
353 static rtx reemit_notes PARAMS ((rtx, rtx));
355 static rtx *ready_lastpos PARAMS ((struct ready_list *));
356 static void ready_sort PARAMS ((struct ready_list *));
357 static rtx ready_remove_first PARAMS ((struct ready_list *));
359 static void queue_to_ready PARAMS ((struct ready_list *));
361 static void debug_ready_list PARAMS ((struct ready_list *));
363 static rtx move_insn1 PARAMS ((rtx, rtx));
364 static rtx move_insn PARAMS ((rtx, rtx));
366 /* The following functions are used to implement multi-pass scheduling
367 on the first cycle. It is used only for DFA based scheduler. */
368 static rtx ready_element PARAMS ((struct ready_list *, int));
369 static rtx ready_remove PARAMS ((struct ready_list *, int));
370 static int max_issue PARAMS ((struct ready_list *, int *));
372 static rtx choose_ready PARAMS ((struct ready_list *));
374 #endif /* INSN_SCHEDULING */
376 /* Point to state used for the current scheduling pass. */
377 struct sched_info *current_sched_info;
379 #ifndef INSN_SCHEDULING
380 void
381 schedule_insns (dump_file)
382 FILE *dump_file ATTRIBUTE_UNUSED;
385 #else
387 /* Pointer to the last instruction scheduled. Used by rank_for_schedule,
388 so that insns independent of the last scheduled insn will be preferred
389 over dependent instructions. */
391 static rtx last_scheduled_insn;
393 /* Compute the function units used by INSN. This caches the value
394 returned by function_units_used. A function unit is encoded as the
395 unit number if the value is non-negative and the complement of a
396 mask if the value is negative. A function unit index is the
397 non-negative encoding. The scheduler using only DFA description
398 should never use the following function. */
400 HAIFA_INLINE int
401 insn_unit (insn)
402 rtx insn;
404 int unit = INSN_UNIT (insn);
406 if (unit == 0)
408 recog_memoized (insn);
410 /* A USE insn, or something else we don't need to understand.
411 We can't pass these directly to function_units_used because it will
412 trigger a fatal error for unrecognizable insns. */
413 if (INSN_CODE (insn) < 0)
414 unit = -1;
415 else
417 unit = function_units_used (insn);
418 /* Increment non-negative values so we can cache zero. */
419 if (unit >= 0)
420 unit++;
422 /* We only cache 16 bits of the result, so if the value is out of
423 range, don't cache it. */
424 if (FUNCTION_UNITS_SIZE < HOST_BITS_PER_SHORT
425 || unit >= 0
426 || (unit & ~((1 << (HOST_BITS_PER_SHORT - 1)) - 1)) == 0)
427 INSN_UNIT (insn) = unit;
429 return (unit > 0 ? unit - 1 : unit);
432 /* Compute the blockage range for executing INSN on UNIT. This caches
433 the value returned by the blockage_range_function for the unit.
434 These values are encoded in an int where the upper half gives the
435 minimum value and the lower half gives the maximum value. The
436 scheduler using only DFA description should never use the following
437 function. */
439 HAIFA_INLINE static unsigned int
440 blockage_range (unit, insn)
441 int unit;
442 rtx insn;
444 unsigned int blockage = INSN_BLOCKAGE (insn);
445 unsigned int range;
447 if ((int) UNIT_BLOCKED (blockage) != unit + 1)
449 range = function_units[unit].blockage_range_function (insn);
450 /* We only cache the blockage range for one unit and then only if
451 the values fit. */
452 if (HOST_BITS_PER_INT >= UNIT_BITS + 2 * BLOCKAGE_BITS)
453 INSN_BLOCKAGE (insn) = ENCODE_BLOCKAGE (unit + 1, range);
455 else
456 range = BLOCKAGE_RANGE (blockage);
458 return range;
461 /* A vector indexed by function unit instance giving the last insn to
462 use the unit. The value of the function unit instance index for
463 unit U instance I is (U + I * FUNCTION_UNITS_SIZE). The scheduler
464 using only DFA description should never use the following variable. */
465 #if FUNCTION_UNITS_SIZE
466 static rtx unit_last_insn[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
467 #else
468 static rtx unit_last_insn[1];
469 #endif
471 /* A vector indexed by function unit instance giving the minimum time
472 when the unit will unblock based on the maximum blockage cost. The
473 scheduler using only DFA description should never use the following
474 variable. */
475 #if FUNCTION_UNITS_SIZE
476 static int unit_tick[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
477 #else
478 static int unit_tick[1];
479 #endif
481 /* A vector indexed by function unit number giving the number of insns
482 that remain to use the unit. The scheduler using only DFA
483 description should never use the following variable. */
484 #if FUNCTION_UNITS_SIZE
485 static int unit_n_insns[FUNCTION_UNITS_SIZE];
486 #else
487 static int unit_n_insns[1];
488 #endif
490 /* Access the unit_last_insn array. Used by the visualization code.
491 The scheduler using only DFA description should never use the
492 following function. */
495 get_unit_last_insn (instance)
496 int instance;
498 return unit_last_insn[instance];
501 /* Reset the function unit state to the null state. */
503 static void
504 clear_units ()
506 memset ((char *) unit_last_insn, 0, sizeof (unit_last_insn));
507 memset ((char *) unit_tick, 0, sizeof (unit_tick));
508 memset ((char *) unit_n_insns, 0, sizeof (unit_n_insns));
511 /* Return the issue-delay of an insn. The scheduler using only DFA
512 description should never use the following function. */
514 HAIFA_INLINE int
515 insn_issue_delay (insn)
516 rtx insn;
518 int i, delay = 0;
519 int unit = insn_unit (insn);
521 /* Efficiency note: in fact, we are working 'hard' to compute a
522 value that was available in md file, and is not available in
523 function_units[] structure. It would be nice to have this
524 value there, too. */
525 if (unit >= 0)
527 if (function_units[unit].blockage_range_function &&
528 function_units[unit].blockage_function)
529 delay = function_units[unit].blockage_function (insn, insn);
531 else
532 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
533 if ((unit & 1) != 0 && function_units[i].blockage_range_function
534 && function_units[i].blockage_function)
535 delay = MAX (delay, function_units[i].blockage_function (insn, insn));
537 return delay;
540 /* Return the actual hazard cost of executing INSN on the unit UNIT,
541 instance INSTANCE at time CLOCK if the previous actual hazard cost
542 was COST. The scheduler using only DFA description should never
543 use the following function. */
545 HAIFA_INLINE int
546 actual_hazard_this_instance (unit, instance, insn, clock, cost)
547 int unit, instance, clock, cost;
548 rtx insn;
550 int tick = unit_tick[instance]; /* Issue time of the last issued insn. */
552 if (tick - clock > cost)
554 /* The scheduler is operating forward, so unit's last insn is the
555 executing insn and INSN is the candidate insn. We want a
556 more exact measure of the blockage if we execute INSN at CLOCK
557 given when we committed the execution of the unit's last insn.
559 The blockage value is given by either the unit's max blockage
560 constant, blockage range function, or blockage function. Use
561 the most exact form for the given unit. */
563 if (function_units[unit].blockage_range_function)
565 if (function_units[unit].blockage_function)
566 tick += (function_units[unit].blockage_function
567 (unit_last_insn[instance], insn)
568 - function_units[unit].max_blockage);
569 else
570 tick += ((int) MAX_BLOCKAGE_COST (blockage_range (unit, insn))
571 - function_units[unit].max_blockage);
573 if (tick - clock > cost)
574 cost = tick - clock;
576 return cost;
579 /* Record INSN as having begun execution on the units encoded by UNIT
580 at time CLOCK. The scheduler using only DFA description should
581 never use the following function. */
583 HAIFA_INLINE static void
584 schedule_unit (unit, insn, clock)
585 int unit, clock;
586 rtx insn;
588 int i;
590 if (unit >= 0)
592 int instance = unit;
593 #if MAX_MULTIPLICITY > 1
594 /* Find the first free instance of the function unit and use that
595 one. We assume that one is free. */
596 for (i = function_units[unit].multiplicity - 1; i > 0; i--)
598 if (!actual_hazard_this_instance (unit, instance, insn, clock, 0))
599 break;
600 instance += FUNCTION_UNITS_SIZE;
602 #endif
603 unit_last_insn[instance] = insn;
604 unit_tick[instance] = (clock + function_units[unit].max_blockage);
606 else
607 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
608 if ((unit & 1) != 0)
609 schedule_unit (i, insn, clock);
612 /* Return the actual hazard cost of executing INSN on the units
613 encoded by UNIT at time CLOCK if the previous actual hazard cost
614 was COST. The scheduler using only DFA description should never
615 use the following function. */
617 HAIFA_INLINE static int
618 actual_hazard (unit, insn, clock, cost)
619 int unit, clock, cost;
620 rtx insn;
622 int i;
624 if (unit >= 0)
626 /* Find the instance of the function unit with the minimum hazard. */
627 int instance = unit;
628 int best_cost = actual_hazard_this_instance (unit, instance, insn,
629 clock, cost);
630 #if MAX_MULTIPLICITY > 1
631 int this_cost;
633 if (best_cost > cost)
635 for (i = function_units[unit].multiplicity - 1; i > 0; i--)
637 instance += FUNCTION_UNITS_SIZE;
638 this_cost = actual_hazard_this_instance (unit, instance, insn,
639 clock, cost);
640 if (this_cost < best_cost)
642 best_cost = this_cost;
643 if (this_cost <= cost)
644 break;
648 #endif
649 cost = MAX (cost, best_cost);
651 else
652 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
653 if ((unit & 1) != 0)
654 cost = actual_hazard (i, insn, clock, cost);
656 return cost;
659 /* Return the potential hazard cost of executing an instruction on the
660 units encoded by UNIT if the previous potential hazard cost was
661 COST. An insn with a large blockage time is chosen in preference
662 to one with a smaller time; an insn that uses a unit that is more
663 likely to be used is chosen in preference to one with a unit that
664 is less used. We are trying to minimize a subsequent actual
665 hazard. The scheduler using only DFA description should never use
666 the following function. */
668 HAIFA_INLINE static int
669 potential_hazard (unit, insn, cost)
670 int unit, cost;
671 rtx insn;
673 int i, ncost;
674 unsigned int minb, maxb;
676 if (unit >= 0)
678 minb = maxb = function_units[unit].max_blockage;
679 if (maxb > 1)
681 if (function_units[unit].blockage_range_function)
683 maxb = minb = blockage_range (unit, insn);
684 maxb = MAX_BLOCKAGE_COST (maxb);
685 minb = MIN_BLOCKAGE_COST (minb);
688 if (maxb > 1)
690 /* Make the number of instructions left dominate. Make the
691 minimum delay dominate the maximum delay. If all these
692 are the same, use the unit number to add an arbitrary
693 ordering. Other terms can be added. */
694 ncost = minb * 0x40 + maxb;
695 ncost *= (unit_n_insns[unit] - 1) * 0x1000 + unit;
696 if (ncost > cost)
697 cost = ncost;
701 else
702 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
703 if ((unit & 1) != 0)
704 cost = potential_hazard (i, insn, cost);
706 return cost;
709 /* Compute cost of executing INSN given the dependence LINK on the insn USED.
710 This is the number of cycles between instruction issue and
711 instruction results. */
713 HAIFA_INLINE int
714 insn_cost (insn, link, used)
715 rtx insn, link, used;
717 int cost = INSN_COST (insn);
719 if (cost < 0)
721 /* A USE insn, or something else we don't need to
722 understand. We can't pass these directly to
723 result_ready_cost or insn_default_latency because it will
724 trigger a fatal error for unrecognizable insns. */
725 if (recog_memoized (insn) < 0)
727 INSN_COST (insn) = 0;
728 return 0;
730 else
732 if (targetm.sched.use_dfa_pipeline_interface
733 && (*targetm.sched.use_dfa_pipeline_interface) ())
734 cost = insn_default_latency (insn);
735 else
736 cost = result_ready_cost (insn);
738 if (cost < 0)
739 cost = 0;
741 INSN_COST (insn) = cost;
745 /* In this case estimate cost without caring how insn is used. */
746 if (link == 0 || used == 0)
747 return cost;
749 /* A USE insn should never require the value used to be computed.
750 This allows the computation of a function's result and parameter
751 values to overlap the return and call. */
752 if (recog_memoized (used) < 0)
753 cost = 0;
754 else
756 if (targetm.sched.use_dfa_pipeline_interface
757 && (*targetm.sched.use_dfa_pipeline_interface) ())
759 if (INSN_CODE (insn) >= 0)
761 if (REG_NOTE_KIND (link) == REG_DEP_ANTI)
762 cost = 0;
763 else if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
765 cost = (insn_default_latency (insn)
766 - insn_default_latency (used));
767 if (cost <= 0)
768 cost = 1;
770 else if (bypass_p (insn))
771 cost = insn_latency (insn, used);
775 if (targetm.sched.adjust_cost)
776 cost = (*targetm.sched.adjust_cost) (used, link, insn, cost);
778 if (cost < 0)
779 cost = 0;
782 return cost;
785 /* Compute the priority number for INSN. */
787 static int
788 priority (insn)
789 rtx insn;
791 rtx link;
793 if (! INSN_P (insn))
794 return 0;
796 if (! INSN_PRIORITY_KNOWN (insn))
798 int this_priority = 0;
800 if (INSN_DEPEND (insn) == 0)
801 this_priority = insn_cost (insn, 0, 0);
802 else
804 for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
806 rtx next;
807 int next_priority;
809 if (RTX_INTEGRATED_P (link))
810 continue;
812 next = XEXP (link, 0);
814 /* Critical path is meaningful in block boundaries only. */
815 if (! (*current_sched_info->contributes_to_priority) (next, insn))
816 continue;
818 next_priority = insn_cost (insn, link, next) + priority (next);
819 if (next_priority > this_priority)
820 this_priority = next_priority;
823 INSN_PRIORITY (insn) = this_priority;
824 INSN_PRIORITY_KNOWN (insn) = 1;
827 return INSN_PRIORITY (insn);
830 /* Macros and functions for keeping the priority queue sorted, and
831 dealing with queueing and dequeueing of instructions. */
833 #define SCHED_SORT(READY, N_READY) \
834 do { if ((N_READY) == 2) \
835 swap_sort (READY, N_READY); \
836 else if ((N_READY) > 2) \
837 qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); } \
838 while (0)
840 /* Returns a positive value if x is preferred; returns a negative value if
841 y is preferred. Should never return 0, since that will make the sort
842 unstable. */
844 static int
845 rank_for_schedule (x, y)
846 const PTR x;
847 const PTR y;
849 rtx tmp = *(const rtx *) y;
850 rtx tmp2 = *(const rtx *) x;
851 rtx link;
852 int tmp_class, tmp2_class, depend_count1, depend_count2;
853 int val, priority_val, weight_val, info_val;
855 /* Prefer insn with higher priority. */
856 priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
858 if (priority_val)
859 return priority_val;
861 /* Prefer an insn with smaller contribution to registers-pressure. */
862 if (!reload_completed &&
863 (weight_val = INSN_REG_WEIGHT (tmp) - INSN_REG_WEIGHT (tmp2)))
864 return weight_val;
866 info_val = (*current_sched_info->rank) (tmp, tmp2);
867 if (info_val)
868 return info_val;
870 /* Compare insns based on their relation to the last-scheduled-insn. */
871 if (last_scheduled_insn)
873 /* Classify the instructions into three classes:
874 1) Data dependent on last schedule insn.
875 2) Anti/Output dependent on last scheduled insn.
876 3) Independent of last scheduled insn, or has latency of one.
877 Choose the insn from the highest numbered class if different. */
878 link = find_insn_list (tmp, INSN_DEPEND (last_scheduled_insn));
879 if (link == 0 || insn_cost (last_scheduled_insn, link, tmp) == 1)
880 tmp_class = 3;
881 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
882 tmp_class = 1;
883 else
884 tmp_class = 2;
886 link = find_insn_list (tmp2, INSN_DEPEND (last_scheduled_insn));
887 if (link == 0 || insn_cost (last_scheduled_insn, link, tmp2) == 1)
888 tmp2_class = 3;
889 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
890 tmp2_class = 1;
891 else
892 tmp2_class = 2;
894 if ((val = tmp2_class - tmp_class))
895 return val;
898 /* Prefer the insn which has more later insns that depend on it.
899 This gives the scheduler more freedom when scheduling later
900 instructions at the expense of added register pressure. */
901 depend_count1 = 0;
902 for (link = INSN_DEPEND (tmp); link; link = XEXP (link, 1))
903 depend_count1++;
905 depend_count2 = 0;
906 for (link = INSN_DEPEND (tmp2); link; link = XEXP (link, 1))
907 depend_count2++;
909 val = depend_count2 - depend_count1;
910 if (val)
911 return val;
913 /* If insns are equally good, sort by INSN_LUID (original insn order),
914 so that we make the sort stable. This minimizes instruction movement,
915 thus minimizing sched's effect on debugging and cross-jumping. */
916 return INSN_LUID (tmp) - INSN_LUID (tmp2);
919 /* Resort the array A in which only element at index N may be out of order. */
921 HAIFA_INLINE static void
922 swap_sort (a, n)
923 rtx *a;
924 int n;
926 rtx insn = a[n - 1];
927 int i = n - 2;
929 while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
931 a[i + 1] = a[i];
932 i -= 1;
934 a[i + 1] = insn;
937 /* Add INSN to the insn queue so that it can be executed at least
938 N_CYCLES after the currently executing insn. Preserve insns
939 chain for debugging purposes. */
941 HAIFA_INLINE static void
942 queue_insn (insn, n_cycles)
943 rtx insn;
944 int n_cycles;
946 int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
947 rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
948 insn_queue[next_q] = link;
949 q_size += 1;
951 if (sched_verbose >= 2)
953 fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
954 (*current_sched_info->print_insn) (insn, 0));
956 fprintf (sched_dump, "queued for %d cycles.\n", n_cycles);
960 /* Return a pointer to the bottom of the ready list, i.e. the insn
961 with the lowest priority. */
963 HAIFA_INLINE static rtx *
964 ready_lastpos (ready)
965 struct ready_list *ready;
967 if (ready->n_ready == 0)
968 abort ();
969 return ready->vec + ready->first - ready->n_ready + 1;
972 /* Add an element INSN to the ready list so that it ends up with the lowest
973 priority. */
975 HAIFA_INLINE void
976 ready_add (ready, insn)
977 struct ready_list *ready;
978 rtx insn;
980 if (ready->first == ready->n_ready)
982 memmove (ready->vec + ready->veclen - ready->n_ready,
983 ready_lastpos (ready),
984 ready->n_ready * sizeof (rtx));
985 ready->first = ready->veclen - 1;
987 ready->vec[ready->first - ready->n_ready] = insn;
988 ready->n_ready++;
991 /* Remove the element with the highest priority from the ready list and
992 return it. */
994 HAIFA_INLINE static rtx
995 ready_remove_first (ready)
996 struct ready_list *ready;
998 rtx t;
999 if (ready->n_ready == 0)
1000 abort ();
1001 t = ready->vec[ready->first--];
1002 ready->n_ready--;
1003 /* If the queue becomes empty, reset it. */
1004 if (ready->n_ready == 0)
1005 ready->first = ready->veclen - 1;
1006 return t;
1009 /* The following code implements multi-pass scheduling for the first
1010 cycle. In other words, we will try to choose ready insn which
1011 permits to start maximum number of insns on the same cycle. */
1013 /* Return a pointer to the element INDEX from the ready. INDEX for
1014 insn with the highest priority is 0, and the lowest priority has
1015 N_READY - 1. */
1017 HAIFA_INLINE static rtx
1018 ready_element (ready, index)
1019 struct ready_list *ready;
1020 int index;
1022 #ifdef ENABLE_CHECKING
1023 if (ready->n_ready == 0 || index >= ready->n_ready)
1024 abort ();
1025 #endif
1026 return ready->vec[ready->first - index];
1029 /* Remove the element INDEX from the ready list and return it. INDEX
1030 for insn with the highest priority is 0, and the lowest priority
1031 has N_READY - 1. */
1033 HAIFA_INLINE static rtx
1034 ready_remove (ready, index)
1035 struct ready_list *ready;
1036 int index;
1038 rtx t;
1039 int i;
1041 if (index == 0)
1042 return ready_remove_first (ready);
1043 if (ready->n_ready == 0 || index >= ready->n_ready)
1044 abort ();
1045 t = ready->vec[ready->first - index];
1046 ready->n_ready--;
1047 for (i = index; i < ready->n_ready; i++)
1048 ready->vec[ready->first - i] = ready->vec[ready->first - i - 1];
1049 return t;
1053 /* Sort the ready list READY by ascending priority, using the SCHED_SORT
1054 macro. */
1056 HAIFA_INLINE static void
1057 ready_sort (ready)
1058 struct ready_list *ready;
1060 rtx *first = ready_lastpos (ready);
1061 SCHED_SORT (first, ready->n_ready);
1064 /* PREV is an insn that is ready to execute. Adjust its priority if that
1065 will help shorten or lengthen register lifetimes as appropriate. Also
1066 provide a hook for the target to tweek itself. */
1068 HAIFA_INLINE static void
1069 adjust_priority (prev)
1070 rtx prev;
1072 /* ??? There used to be code here to try and estimate how an insn
1073 affected register lifetimes, but it did it by looking at REG_DEAD
1074 notes, which we removed in schedule_region. Nor did it try to
1075 take into account register pressure or anything useful like that.
1077 Revisit when we have a machine model to work with and not before. */
1079 if (targetm.sched.adjust_priority)
1080 INSN_PRIORITY (prev) =
1081 (*targetm.sched.adjust_priority) (prev, INSN_PRIORITY (prev));
1084 /* Advance time on one cycle. */
1085 HAIFA_INLINE static void
1086 advance_one_cycle ()
1088 if (targetm.sched.use_dfa_pipeline_interface
1089 && (*targetm.sched.use_dfa_pipeline_interface) ())
1091 if (targetm.sched.dfa_pre_cycle_insn)
1092 state_transition (curr_state,
1093 (*targetm.sched.dfa_pre_cycle_insn) ());
1095 state_transition (curr_state, NULL);
1097 if (targetm.sched.dfa_post_cycle_insn)
1098 state_transition (curr_state,
1099 (*targetm.sched.dfa_post_cycle_insn) ());
1103 /* Clock at which the previous instruction was issued. */
1104 static int last_clock_var;
1106 /* INSN is the "currently executing insn". Launch each insn which was
1107 waiting on INSN. READY is the ready list which contains the insns
1108 that are ready to fire. CLOCK is the current cycle.
1111 static void
1112 schedule_insn (insn, ready, clock)
1113 rtx insn;
1114 struct ready_list *ready;
1115 int clock;
1117 rtx link;
1118 int unit = 0;
1120 if (!targetm.sched.use_dfa_pipeline_interface
1121 || !(*targetm.sched.use_dfa_pipeline_interface) ())
1122 unit = insn_unit (insn);
1124 if (targetm.sched.use_dfa_pipeline_interface
1125 && (*targetm.sched.use_dfa_pipeline_interface) ()
1126 && sched_verbose >= 1)
1128 char buf[2048];
1130 print_insn (buf, insn, 0);
1131 buf[40]=0;
1132 fprintf (sched_dump, ";;\t%3i--> %-40s:", clock, buf);
1134 if (recog_memoized (insn) < 0)
1135 fprintf (sched_dump, "nothing");
1136 else
1137 print_reservation (sched_dump, insn);
1138 fputc ('\n', sched_dump);
1140 else if (sched_verbose >= 2)
1142 fprintf (sched_dump, ";;\t\t--> scheduling insn <<<%d>>> on unit ",
1143 INSN_UID (insn));
1144 insn_print_units (insn);
1145 fputc ('\n', sched_dump);
1148 if (!targetm.sched.use_dfa_pipeline_interface
1149 || !(*targetm.sched.use_dfa_pipeline_interface) ())
1151 if (sched_verbose && unit == -1)
1152 visualize_no_unit (insn);
1155 if (MAX_BLOCKAGE > 1 || issue_rate > 1 || sched_verbose)
1156 schedule_unit (unit, insn, clock);
1158 if (INSN_DEPEND (insn) == 0)
1159 return;
1162 for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
1164 rtx next = XEXP (link, 0);
1165 int cost = insn_cost (insn, link, next);
1167 INSN_TICK (next) = MAX (INSN_TICK (next), clock + cost);
1169 if ((INSN_DEP_COUNT (next) -= 1) == 0)
1171 int effective_cost = INSN_TICK (next) - clock;
1173 if (! (*current_sched_info->new_ready) (next))
1174 continue;
1176 if (sched_verbose >= 2)
1178 fprintf (sched_dump, ";;\t\tdependences resolved: insn %s ",
1179 (*current_sched_info->print_insn) (next, 0));
1181 if (effective_cost < 1)
1182 fprintf (sched_dump, "into ready\n");
1183 else
1184 fprintf (sched_dump, "into queue with cost=%d\n", effective_cost);
1187 /* Adjust the priority of NEXT and either put it on the ready
1188 list or queue it. */
1189 adjust_priority (next);
1190 if (effective_cost < 1)
1191 ready_add (ready, next);
1192 else
1193 queue_insn (next, effective_cost);
1197 /* Annotate the instruction with issue information -- TImode
1198 indicates that the instruction is expected not to be able
1199 to issue on the same cycle as the previous insn. A machine
1200 may use this information to decide how the instruction should
1201 be aligned. */
1202 if (issue_rate > 1
1203 && GET_CODE (PATTERN (insn)) != USE
1204 && GET_CODE (PATTERN (insn)) != CLOBBER)
1206 if (reload_completed)
1207 PUT_MODE (insn, clock > last_clock_var ? TImode : VOIDmode);
1208 last_clock_var = clock;
1212 /* Functions for handling of notes. */
1214 /* Delete notes beginning with INSN and put them in the chain
1215 of notes ended by NOTE_LIST.
1216 Returns the insn following the notes. */
1218 static rtx
1219 unlink_other_notes (insn, tail)
1220 rtx insn, tail;
1222 rtx prev = PREV_INSN (insn);
1224 while (insn != tail && GET_CODE (insn) == NOTE)
1226 rtx next = NEXT_INSN (insn);
1227 /* Delete the note from its current position. */
1228 if (prev)
1229 NEXT_INSN (prev) = next;
1230 if (next)
1231 PREV_INSN (next) = prev;
1233 /* See sched_analyze to see how these are handled. */
1234 if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG
1235 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_END
1236 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_BEG
1237 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_END)
1239 /* Insert the note at the end of the notes list. */
1240 PREV_INSN (insn) = note_list;
1241 if (note_list)
1242 NEXT_INSN (note_list) = insn;
1243 note_list = insn;
1246 insn = next;
1248 return insn;
1251 /* Delete line notes beginning with INSN. Record line-number notes so
1252 they can be reused. Returns the insn following the notes. */
1254 static rtx
1255 unlink_line_notes (insn, tail)
1256 rtx insn, tail;
1258 rtx prev = PREV_INSN (insn);
1260 while (insn != tail && GET_CODE (insn) == NOTE)
1262 rtx next = NEXT_INSN (insn);
1264 if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
1266 /* Delete the note from its current position. */
1267 if (prev)
1268 NEXT_INSN (prev) = next;
1269 if (next)
1270 PREV_INSN (next) = prev;
1272 /* Record line-number notes so they can be reused. */
1273 LINE_NOTE (insn) = insn;
1275 else
1276 prev = insn;
1278 insn = next;
1280 return insn;
1283 /* Return the head and tail pointers of BB. */
1285 void
1286 get_block_head_tail (b, headp, tailp)
1287 int b;
1288 rtx *headp;
1289 rtx *tailp;
1291 /* HEAD and TAIL delimit the basic block being scheduled. */
1292 rtx head = BLOCK_HEAD (b);
1293 rtx tail = BLOCK_END (b);
1295 /* Don't include any notes or labels at the beginning of the
1296 basic block, or notes at the ends of basic blocks. */
1297 while (head != tail)
1299 if (GET_CODE (head) == NOTE)
1300 head = NEXT_INSN (head);
1301 else if (GET_CODE (tail) == NOTE)
1302 tail = PREV_INSN (tail);
1303 else if (GET_CODE (head) == CODE_LABEL)
1304 head = NEXT_INSN (head);
1305 else
1306 break;
1309 *headp = head;
1310 *tailp = tail;
1313 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ]. */
1316 no_real_insns_p (head, tail)
1317 rtx head, tail;
1319 while (head != NEXT_INSN (tail))
1321 if (GET_CODE (head) != NOTE && GET_CODE (head) != CODE_LABEL)
1322 return 0;
1323 head = NEXT_INSN (head);
1325 return 1;
1328 /* Delete line notes from one block. Save them so they can be later restored
1329 (in restore_line_notes). HEAD and TAIL are the boundaries of the
1330 block in which notes should be processed. */
1332 void
1333 rm_line_notes (head, tail)
1334 rtx head, tail;
1336 rtx next_tail;
1337 rtx insn;
1339 next_tail = NEXT_INSN (tail);
1340 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1342 rtx prev;
1344 /* Farm out notes, and maybe save them in NOTE_LIST.
1345 This is needed to keep the debugger from
1346 getting completely deranged. */
1347 if (GET_CODE (insn) == NOTE)
1349 prev = insn;
1350 insn = unlink_line_notes (insn, next_tail);
1352 if (prev == tail)
1353 abort ();
1354 if (prev == head)
1355 abort ();
1356 if (insn == next_tail)
1357 abort ();
1362 /* Save line number notes for each insn in block B. HEAD and TAIL are
1363 the boundaries of the block in which notes should be processed. */
1365 void
1366 save_line_notes (b, head, tail)
1367 int b;
1368 rtx head, tail;
1370 rtx next_tail;
1372 /* We must use the true line number for the first insn in the block
1373 that was computed and saved at the start of this pass. We can't
1374 use the current line number, because scheduling of the previous
1375 block may have changed the current line number. */
1377 rtx line = line_note_head[b];
1378 rtx insn;
1380 next_tail = NEXT_INSN (tail);
1382 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1383 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1384 line = insn;
1385 else
1386 LINE_NOTE (insn) = line;
1389 /* After a block was scheduled, insert line notes into the insns list.
1390 HEAD and TAIL are the boundaries of the block in which notes should
1391 be processed. */
1393 void
1394 restore_line_notes (head, tail)
1395 rtx head, tail;
1397 rtx line, note, prev, new;
1398 int added_notes = 0;
1399 rtx next_tail, insn;
1401 head = head;
1402 next_tail = NEXT_INSN (tail);
1404 /* Determine the current line-number. We want to know the current
1405 line number of the first insn of the block here, in case it is
1406 different from the true line number that was saved earlier. If
1407 different, then we need a line number note before the first insn
1408 of this block. If it happens to be the same, then we don't want to
1409 emit another line number note here. */
1410 for (line = head; line; line = PREV_INSN (line))
1411 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
1412 break;
1414 /* Walk the insns keeping track of the current line-number and inserting
1415 the line-number notes as needed. */
1416 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1417 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1418 line = insn;
1419 /* This used to emit line number notes before every non-deleted note.
1420 However, this confuses a debugger, because line notes not separated
1421 by real instructions all end up at the same address. I can find no
1422 use for line number notes before other notes, so none are emitted. */
1423 else if (GET_CODE (insn) != NOTE
1424 && INSN_UID (insn) < old_max_uid
1425 && (note = LINE_NOTE (insn)) != 0
1426 && note != line
1427 && (line == 0
1428 || NOTE_LINE_NUMBER (note) != NOTE_LINE_NUMBER (line)
1429 || NOTE_SOURCE_FILE (note) != NOTE_SOURCE_FILE (line)))
1431 line = note;
1432 prev = PREV_INSN (insn);
1433 if (LINE_NOTE (note))
1435 /* Re-use the original line-number note. */
1436 LINE_NOTE (note) = 0;
1437 PREV_INSN (note) = prev;
1438 NEXT_INSN (prev) = note;
1439 PREV_INSN (insn) = note;
1440 NEXT_INSN (note) = insn;
1442 else
1444 added_notes++;
1445 new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
1446 NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
1447 RTX_INTEGRATED_P (new) = RTX_INTEGRATED_P (note);
1450 if (sched_verbose && added_notes)
1451 fprintf (sched_dump, ";; added %d line-number notes\n", added_notes);
1454 /* After scheduling the function, delete redundant line notes from the
1455 insns list. */
1457 void
1458 rm_redundant_line_notes ()
1460 rtx line = 0;
1461 rtx insn = get_insns ();
1462 int active_insn = 0;
1463 int notes = 0;
1465 /* Walk the insns deleting redundant line-number notes. Many of these
1466 are already present. The remainder tend to occur at basic
1467 block boundaries. */
1468 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1469 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1471 /* If there are no active insns following, INSN is redundant. */
1472 if (active_insn == 0)
1474 notes++;
1475 NOTE_SOURCE_FILE (insn) = 0;
1476 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1478 /* If the line number is unchanged, LINE is redundant. */
1479 else if (line
1480 && NOTE_LINE_NUMBER (line) == NOTE_LINE_NUMBER (insn)
1481 && NOTE_SOURCE_FILE (line) == NOTE_SOURCE_FILE (insn))
1483 notes++;
1484 NOTE_SOURCE_FILE (line) = 0;
1485 NOTE_LINE_NUMBER (line) = NOTE_INSN_DELETED;
1486 line = insn;
1488 else
1489 line = insn;
1490 active_insn = 0;
1492 else if (!((GET_CODE (insn) == NOTE
1493 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
1494 || (GET_CODE (insn) == INSN
1495 && (GET_CODE (PATTERN (insn)) == USE
1496 || GET_CODE (PATTERN (insn)) == CLOBBER))))
1497 active_insn++;
1499 if (sched_verbose && notes)
1500 fprintf (sched_dump, ";; deleted %d line-number notes\n", notes);
1503 /* Delete notes between HEAD and TAIL and put them in the chain
1504 of notes ended by NOTE_LIST. */
1506 void
1507 rm_other_notes (head, tail)
1508 rtx head;
1509 rtx tail;
1511 rtx next_tail;
1512 rtx insn;
1514 note_list = 0;
1515 if (head == tail && (! INSN_P (head)))
1516 return;
1518 next_tail = NEXT_INSN (tail);
1519 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1521 rtx prev;
1523 /* Farm out notes, and maybe save them in NOTE_LIST.
1524 This is needed to keep the debugger from
1525 getting completely deranged. */
1526 if (GET_CODE (insn) == NOTE)
1528 prev = insn;
1530 insn = unlink_other_notes (insn, next_tail);
1532 if (prev == tail)
1533 abort ();
1534 if (prev == head)
1535 abort ();
1536 if (insn == next_tail)
1537 abort ();
1542 /* Functions for computation of registers live/usage info. */
1544 /* This function looks for a new register being defined.
1545 If the destination register is already used by the source,
1546 a new register is not needed. */
1548 static int
1549 find_set_reg_weight (x)
1550 rtx x;
1552 if (GET_CODE (x) == CLOBBER
1553 && register_operand (SET_DEST (x), VOIDmode))
1554 return 1;
1555 if (GET_CODE (x) == SET
1556 && register_operand (SET_DEST (x), VOIDmode))
1558 if (GET_CODE (SET_DEST (x)) == REG)
1560 if (!reg_mentioned_p (SET_DEST (x), SET_SRC (x)))
1561 return 1;
1562 else
1563 return 0;
1565 return 1;
1567 return 0;
1570 /* Calculate INSN_REG_WEIGHT for all insns of a block. */
1572 static void
1573 find_insn_reg_weight (b)
1574 int b;
1576 rtx insn, next_tail, head, tail;
1578 get_block_head_tail (b, &head, &tail);
1579 next_tail = NEXT_INSN (tail);
1581 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1583 int reg_weight = 0;
1584 rtx x;
1586 /* Handle register life information. */
1587 if (! INSN_P (insn))
1588 continue;
1590 /* Increment weight for each register born here. */
1591 x = PATTERN (insn);
1592 reg_weight += find_set_reg_weight (x);
1593 if (GET_CODE (x) == PARALLEL)
1595 int j;
1596 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
1598 x = XVECEXP (PATTERN (insn), 0, j);
1599 reg_weight += find_set_reg_weight (x);
1602 /* Decrement weight for each register that dies here. */
1603 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1605 if (REG_NOTE_KIND (x) == REG_DEAD
1606 || REG_NOTE_KIND (x) == REG_UNUSED)
1607 reg_weight--;
1610 INSN_REG_WEIGHT (insn) = reg_weight;
1614 /* Scheduling clock, modified in schedule_block() and queue_to_ready (). */
1615 static int clock_var;
1617 /* Move insns that became ready to fire from queue to ready list. */
1619 static void
1620 queue_to_ready (ready)
1621 struct ready_list *ready;
1623 rtx insn;
1624 rtx link;
1626 q_ptr = NEXT_Q (q_ptr);
1628 /* Add all pending insns that can be scheduled without stalls to the
1629 ready list. */
1630 for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
1632 insn = XEXP (link, 0);
1633 q_size -= 1;
1635 if (sched_verbose >= 2)
1636 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1637 (*current_sched_info->print_insn) (insn, 0));
1639 ready_add (ready, insn);
1640 if (sched_verbose >= 2)
1641 fprintf (sched_dump, "moving to ready without stalls\n");
1643 insn_queue[q_ptr] = 0;
1645 /* If there are no ready insns, stall until one is ready and add all
1646 of the pending insns at that point to the ready list. */
1647 if (ready->n_ready == 0)
1649 int stalls;
1651 for (stalls = 1; stalls <= MAX_INSN_QUEUE_INDEX; stalls++)
1653 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
1655 for (; link; link = XEXP (link, 1))
1657 insn = XEXP (link, 0);
1658 q_size -= 1;
1660 if (sched_verbose >= 2)
1661 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1662 (*current_sched_info->print_insn) (insn, 0));
1664 ready_add (ready, insn);
1665 if (sched_verbose >= 2)
1666 fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
1668 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = 0;
1670 advance_one_cycle ();
1672 break;
1675 advance_one_cycle ();
1678 if ((!targetm.sched.use_dfa_pipeline_interface
1679 || !(*targetm.sched.use_dfa_pipeline_interface) ())
1680 && sched_verbose && stalls)
1681 visualize_stall_cycles (stalls);
1683 q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
1684 clock_var += stalls;
1688 /* Print the ready list for debugging purposes. Callable from debugger. */
1690 static void
1691 debug_ready_list (ready)
1692 struct ready_list *ready;
1694 rtx *p;
1695 int i;
1697 if (ready->n_ready == 0)
1699 fprintf (sched_dump, "\n");
1700 return;
1703 p = ready_lastpos (ready);
1704 for (i = 0; i < ready->n_ready; i++)
1705 fprintf (sched_dump, " %s", (*current_sched_info->print_insn) (p[i], 0));
1706 fprintf (sched_dump, "\n");
1709 /* move_insn1: Remove INSN from insn chain, and link it after LAST insn. */
1711 static rtx
1712 move_insn1 (insn, last)
1713 rtx insn, last;
1715 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
1716 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
1718 NEXT_INSN (insn) = NEXT_INSN (last);
1719 PREV_INSN (NEXT_INSN (last)) = insn;
1721 NEXT_INSN (last) = insn;
1722 PREV_INSN (insn) = last;
1724 return insn;
1727 /* Search INSN for REG_SAVE_NOTE note pairs for
1728 NOTE_INSN_{LOOP,EHREGION}_{BEG,END}; and convert them back into
1729 NOTEs. The REG_SAVE_NOTE note following first one is contains the
1730 saved value for NOTE_BLOCK_NUMBER which is useful for
1731 NOTE_INSN_EH_REGION_{BEG,END} NOTEs. LAST is the last instruction
1732 output by the instruction scheduler. Return the new value of LAST. */
1734 static rtx
1735 reemit_notes (insn, last)
1736 rtx insn;
1737 rtx last;
1739 rtx note, retval;
1741 retval = last;
1742 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1744 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
1746 enum insn_note note_type = INTVAL (XEXP (note, 0));
1748 last = emit_note_before (note_type, last);
1749 remove_note (insn, note);
1750 note = XEXP (note, 1);
1751 if (note_type == NOTE_INSN_EH_REGION_BEG
1752 || note_type == NOTE_INSN_EH_REGION_END)
1753 NOTE_EH_HANDLER (last) = INTVAL (XEXP (note, 0));
1754 remove_note (insn, note);
1757 return retval;
1760 /* Move INSN, and all insns which should be issued before it,
1761 due to SCHED_GROUP_P flag. Reemit notes if needed.
1763 Return the last insn emitted by the scheduler, which is the
1764 return value from the first call to reemit_notes. */
1766 static rtx
1767 move_insn (insn, last)
1768 rtx insn, last;
1770 rtx retval = NULL;
1772 /* If INSN has SCHED_GROUP_P set, then issue it and any other
1773 insns with SCHED_GROUP_P set first. */
1774 while (SCHED_GROUP_P (insn))
1776 rtx prev = PREV_INSN (insn);
1778 /* Move a SCHED_GROUP_P insn. */
1779 move_insn1 (insn, last);
1780 /* If this is the first call to reemit_notes, then record
1781 its return value. */
1782 if (retval == NULL_RTX)
1783 retval = reemit_notes (insn, insn);
1784 else
1785 reemit_notes (insn, insn);
1786 /* Consume SCHED_GROUP_P flag. */
1787 SCHED_GROUP_P (insn) = 0;
1788 insn = prev;
1791 /* Now move the first non SCHED_GROUP_P insn. */
1792 move_insn1 (insn, last);
1794 /* If this is the first call to reemit_notes, then record
1795 its return value. */
1796 if (retval == NULL_RTX)
1797 retval = reemit_notes (insn, insn);
1798 else
1799 reemit_notes (insn, insn);
1801 return retval;
1804 /* The following structure describe an entry of the stack of choices. */
1805 struct choice_entry
1807 /* Ordinal number of the issued insn in the ready queue. */
1808 int index;
1809 /* The number of the rest insns whose issues we should try. */
1810 int rest;
1811 /* The number of issued essential insns. */
1812 int n;
1813 /* State after issuing the insn. */
1814 state_t state;
1817 /* The following array is used to implement a stack of choices used in
1818 function max_issue. */
1819 static struct choice_entry *choice_stack;
1821 /* The following variable value is number of essential insns issued on
1822 the current cycle. An insn is essential one if it changes the
1823 processors state. */
1824 static int cycle_issued_insns;
1826 /* The following function returns maximal (or close to maximal) number
1827 of insns which can be issued on the same cycle and one of which
1828 insns is insns with the best rank (the first insn in READY). To
1829 make this function tries different samples of ready insns. READY
1830 is current queue `ready'. Global array READY_TRY reflects what
1831 insns are already issued in this try. INDEX will contain index
1832 of the best insn in READY. The following function is used only for
1833 first cycle multipass scheduling. */
1834 static int
1835 max_issue (ready, index)
1836 struct ready_list *ready;
1837 int *index;
1839 int n, i, all, n_ready, lookahead, best, delay;
1840 struct choice_entry *top;
1841 rtx insn;
1843 lookahead = (*targetm.sched.first_cycle_multipass_dfa_lookahead) ();
1844 best = 0;
1845 memcpy (choice_stack->state, curr_state, dfa_state_size);
1846 top = choice_stack;
1847 top->rest = lookahead;
1848 top->n = 0;
1849 n_ready = ready->n_ready;
1850 for (all = i = 0; i < n_ready; i++)
1851 if (!ready_try [i])
1852 all++;
1853 i = 0;
1854 for (;;)
1856 if (top->rest == 0 || i >= n_ready)
1858 if (top == choice_stack)
1859 break;
1860 if (best < top - choice_stack && ready_try [0])
1862 best = top - choice_stack;
1863 *index = choice_stack [1].index;
1864 if (top->n == issue_rate - cycle_issued_insns || best == all)
1865 break;
1867 i = top->index;
1868 ready_try [i] = 0;
1869 top--;
1870 memcpy (curr_state, top->state, dfa_state_size);
1872 else if (!ready_try [i])
1874 insn = ready_element (ready, i);
1875 delay = state_transition (curr_state, insn);
1876 if (delay < 0)
1878 if (state_dead_lock_p (curr_state))
1879 top->rest = 0;
1880 else
1881 top->rest--;
1882 n = top->n;
1883 if (memcmp (top->state, curr_state, dfa_state_size) != 0)
1884 n++;
1885 top++;
1886 top->rest = lookahead;
1887 top->index = i;
1888 top->n = n;
1889 memcpy (top->state, curr_state, dfa_state_size);
1890 ready_try [i] = 1;
1891 i = -1;
1894 i++;
1896 while (top != choice_stack)
1898 ready_try [top->index] = 0;
1899 top--;
1901 memcpy (curr_state, choice_stack->state, dfa_state_size);
1902 return best;
1905 /* The following function chooses insn from READY and modifies
1906 *N_READY and READY. The following function is used only for first
1907 cycle multipass scheduling. */
1909 static rtx
1910 choose_ready (ready)
1911 struct ready_list *ready;
1913 if (!targetm.sched.first_cycle_multipass_dfa_lookahead
1914 || (*targetm.sched.first_cycle_multipass_dfa_lookahead) () <= 0)
1915 return ready_remove_first (ready);
1916 else
1918 /* Try to choose the better insn. */
1919 int index, i;
1920 rtx insn;
1922 insn = ready_element (ready, 0);
1923 if (INSN_CODE (insn) < 0)
1924 return ready_remove_first (ready);
1925 for (i = 1; i < ready->n_ready; i++)
1927 insn = ready_element (ready, i);
1928 ready_try [i]
1929 = (INSN_CODE (insn) < 0
1930 || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard
1931 && !(*targetm.sched.first_cycle_multipass_dfa_lookahead_guard) (insn)));
1933 if (max_issue (ready, &index) == 0)
1934 return ready_remove_first (ready);
1935 else
1936 return ready_remove (ready, index);
1940 /* Called from backends from targetm.sched.reorder to emit stuff into
1941 the instruction stream. */
1944 sched_emit_insn (pat)
1945 rtx pat;
1947 rtx insn = emit_insn_after (pat, last_scheduled_insn);
1948 last_scheduled_insn = insn;
1949 return insn;
1952 /* Use forward list scheduling to rearrange insns of block B in region RGN,
1953 possibly bringing insns from subsequent blocks in the same region. */
1955 void
1956 schedule_block (b, rgn_n_insns)
1957 int b;
1958 int rgn_n_insns;
1960 struct ready_list ready;
1961 int i, first_cycle_insn_p;
1962 int can_issue_more;
1963 state_t temp_state = NULL; /* It is used for multipass scheduling. */
1964 int sort_p;
1966 /* Head/tail info for this block. */
1967 rtx prev_head = current_sched_info->prev_head;
1968 rtx next_tail = current_sched_info->next_tail;
1969 rtx head = NEXT_INSN (prev_head);
1970 rtx tail = PREV_INSN (next_tail);
1972 /* We used to have code to avoid getting parameters moved from hard
1973 argument registers into pseudos.
1975 However, it was removed when it proved to be of marginal benefit
1976 and caused problems because schedule_block and compute_forward_dependences
1977 had different notions of what the "head" insn was. */
1979 if (head == tail && (! INSN_P (head)))
1980 abort ();
1982 /* Debug info. */
1983 if (sched_verbose)
1985 fprintf (sched_dump, ";; ======================================================\n");
1986 fprintf (sched_dump,
1987 ";; -- basic block %d from %d to %d -- %s reload\n",
1988 b, INSN_UID (head), INSN_UID (tail),
1989 (reload_completed ? "after" : "before"));
1990 fprintf (sched_dump, ";; ======================================================\n");
1991 fprintf (sched_dump, "\n");
1993 visualize_alloc ();
1994 init_block_visualization ();
1997 if (targetm.sched.use_dfa_pipeline_interface
1998 && (*targetm.sched.use_dfa_pipeline_interface) ())
1999 state_reset (curr_state);
2000 else
2001 clear_units ();
2003 /* Allocate the ready list. */
2004 ready.veclen = rgn_n_insns + 1 + issue_rate;
2005 ready.first = ready.veclen - 1;
2006 ready.vec = (rtx *) xmalloc (ready.veclen * sizeof (rtx));
2007 ready.n_ready = 0;
2009 if (targetm.sched.use_dfa_pipeline_interface
2010 && (*targetm.sched.use_dfa_pipeline_interface) ())
2012 /* It is used for first cycle multipass scheduling. */
2013 temp_state = alloca (dfa_state_size);
2014 ready_try = (char *) xmalloc ((rgn_n_insns + 1) * sizeof (char));
2015 memset (ready_try, 0, (rgn_n_insns + 1) * sizeof (char));
2016 choice_stack
2017 = (struct choice_entry *) xmalloc ((rgn_n_insns + 1)
2018 * sizeof (struct choice_entry));
2019 for (i = 0; i <= rgn_n_insns; i++)
2020 choice_stack[i].state = (state_t) xmalloc (dfa_state_size);
2023 (*current_sched_info->init_ready_list) (&ready);
2025 if (targetm.sched.md_init)
2026 (*targetm.sched.md_init) (sched_dump, sched_verbose, ready.veclen);
2028 /* We start inserting insns after PREV_HEAD. */
2029 last_scheduled_insn = prev_head;
2031 /* Initialize INSN_QUEUE. Q_SIZE is the total number of insns in the
2032 queue. */
2033 q_ptr = 0;
2034 q_size = 0;
2036 if (!targetm.sched.use_dfa_pipeline_interface
2037 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2038 max_insn_queue_index_macro_value = INSN_QUEUE_SIZE - 1;
2039 else
2040 max_insn_queue_index_macro_value = max_insn_queue_index;
2042 insn_queue = (rtx *) alloca ((MAX_INSN_QUEUE_INDEX + 1) * sizeof (rtx));
2043 memset ((char *) insn_queue, 0, (MAX_INSN_QUEUE_INDEX + 1) * sizeof (rtx));
2044 last_clock_var = -1;
2046 /* Start just before the beginning of time. */
2047 clock_var = -1;
2049 sort_p = TRUE;
2050 /* Loop until all the insns in BB are scheduled. */
2051 while ((*current_sched_info->schedule_more_p) ())
2053 clock_var++;
2055 advance_one_cycle ();
2057 /* Add to the ready list all pending insns that can be issued now.
2058 If there are no ready insns, increment clock until one
2059 is ready and add all pending insns at that point to the ready
2060 list. */
2061 queue_to_ready (&ready);
2063 if (ready.n_ready == 0)
2064 abort ();
2066 if (sched_verbose >= 2)
2068 fprintf (sched_dump, ";;\t\tReady list after queue_to_ready: ");
2069 debug_ready_list (&ready);
2072 if (sort_p)
2074 /* Sort the ready list based on priority. */
2075 ready_sort (&ready);
2077 if (sched_verbose >= 2)
2079 fprintf (sched_dump, ";;\t\tReady list after ready_sort: ");
2080 debug_ready_list (&ready);
2084 /* Allow the target to reorder the list, typically for
2085 better instruction bundling. */
2086 if (targetm.sched.reorder)
2087 can_issue_more =
2088 (*targetm.sched.reorder) (sched_dump, sched_verbose,
2089 ready_lastpos (&ready),
2090 &ready.n_ready, clock_var);
2091 else
2092 can_issue_more = issue_rate;
2094 first_cycle_insn_p = 1;
2095 cycle_issued_insns = 0;
2096 for (;;)
2098 rtx insn;
2099 int cost;
2101 if (sched_verbose >= 2)
2103 fprintf (sched_dump, ";;\tReady list (t =%3d): ",
2104 clock_var);
2105 debug_ready_list (&ready);
2108 if (!targetm.sched.use_dfa_pipeline_interface
2109 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2111 if (ready.n_ready == 0 || !can_issue_more
2112 || !(*current_sched_info->schedule_more_p) ())
2113 break;
2114 insn = choose_ready (&ready);
2115 cost = actual_hazard (insn_unit (insn), insn, clock_var, 0);
2117 else
2119 if (ready.n_ready == 0 || !can_issue_more
2120 || state_dead_lock_p (curr_state)
2121 || !(*current_sched_info->schedule_more_p) ())
2122 break;
2124 /* Select and remove the insn from the ready list. */
2125 if (sort_p)
2126 insn = choose_ready (&ready);
2127 else
2128 insn = ready_remove_first (&ready);
2130 if (targetm.sched.dfa_new_cycle
2131 && (*targetm.sched.dfa_new_cycle) (sched_dump, sched_verbose,
2132 insn, last_clock_var,
2133 clock_var, &sort_p))
2135 ready_add (&ready, insn);
2136 break;
2139 sort_p = TRUE;
2140 memcpy (temp_state, curr_state, dfa_state_size);
2141 if (recog_memoized (insn) < 0)
2143 if (!first_cycle_insn_p
2144 && (GET_CODE (PATTERN (insn)) == ASM_INPUT
2145 || asm_noperands (PATTERN (insn)) >= 0))
2146 /* This is asm insn which is tryed to be issued on the
2147 cycle not first. Issue it on the next cycle. */
2148 cost = 1;
2149 else
2150 /* A USE insn, or something else we don't need to
2151 understand. We can't pass these directly to
2152 state_transition because it will trigger a
2153 fatal error for unrecognizable insns. */
2154 cost = 0;
2156 else
2158 cost = state_transition (temp_state, insn);
2160 if (targetm.sched.first_cycle_multipass_dfa_lookahead
2161 && targetm.sched.dfa_bubble)
2163 if (cost == 0)
2165 int j;
2166 rtx bubble;
2168 for (j = 0;
2169 (bubble = (*targetm.sched.dfa_bubble) (j))
2170 != NULL_RTX;
2171 j++)
2173 memcpy (temp_state, curr_state, dfa_state_size);
2175 if (state_transition (temp_state, bubble) < 0
2176 && state_transition (temp_state, insn) < 0)
2177 break;
2180 if (bubble != NULL_RTX)
2182 if (insert_schedule_bubbles_p)
2184 rtx copy;
2186 copy = copy_rtx (PATTERN (bubble));
2187 emit_insn_after (copy, last_scheduled_insn);
2188 last_scheduled_insn
2189 = NEXT_INSN (last_scheduled_insn);
2190 INSN_CODE (last_scheduled_insn)
2191 = INSN_CODE (bubble);
2193 /* Annotate the same for the first insns
2194 scheduling by using mode. */
2195 PUT_MODE (last_scheduled_insn,
2196 (clock_var > last_clock_var
2197 ? clock_var - last_clock_var
2198 : VOIDmode));
2199 last_clock_var = clock_var;
2201 if (sched_verbose >= 2)
2203 fprintf (sched_dump,
2204 ";;\t\t--> scheduling bubble insn <<<%d>>>:reservation ",
2205 INSN_UID (last_scheduled_insn));
2207 if (recog_memoized (last_scheduled_insn)
2208 < 0)
2209 fprintf (sched_dump, "nothing");
2210 else
2211 print_reservation
2212 (sched_dump, last_scheduled_insn);
2214 fprintf (sched_dump, "\n");
2217 cost = -1;
2222 if (cost < 0)
2223 cost = 0;
2224 else if (cost == 0)
2225 cost = 1;
2230 if (cost >= 1)
2232 queue_insn (insn, cost);
2233 continue;
2236 if (! (*current_sched_info->can_schedule_ready_p) (insn))
2237 goto next;
2239 last_scheduled_insn = move_insn (insn, last_scheduled_insn);
2241 if (targetm.sched.use_dfa_pipeline_interface
2242 && (*targetm.sched.use_dfa_pipeline_interface) ())
2244 if (memcmp (curr_state, temp_state, dfa_state_size) != 0)
2245 cycle_issued_insns++;
2246 memcpy (curr_state, temp_state, dfa_state_size);
2249 if (targetm.sched.variable_issue)
2250 can_issue_more =
2251 (*targetm.sched.variable_issue) (sched_dump, sched_verbose,
2252 insn, can_issue_more);
2253 /* A naked CLOBBER or USE generates no instruction, so do
2254 not count them against the issue rate. */
2255 else if (GET_CODE (PATTERN (insn)) != USE
2256 && GET_CODE (PATTERN (insn)) != CLOBBER)
2257 can_issue_more--;
2259 schedule_insn (insn, &ready, clock_var);
2261 next:
2262 first_cycle_insn_p = 0;
2264 /* Sort the ready list based on priority. This must be
2265 redone here, as schedule_insn may have readied additional
2266 insns that will not be sorted correctly. */
2267 if (ready.n_ready > 0)
2268 ready_sort (&ready);
2270 if (targetm.sched.reorder2)
2272 can_issue_more =
2273 (*targetm.sched.reorder2) (sched_dump, sched_verbose,
2274 ready.n_ready
2275 ? ready_lastpos (&ready) : NULL,
2276 &ready.n_ready, clock_var);
2280 if ((!targetm.sched.use_dfa_pipeline_interface
2281 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2282 && sched_verbose)
2283 /* Debug info. */
2284 visualize_scheduled_insns (clock_var);
2287 if (targetm.sched.md_finish)
2288 (*targetm.sched.md_finish) (sched_dump, sched_verbose);
2290 /* Debug info. */
2291 if (sched_verbose)
2293 fprintf (sched_dump, ";;\tReady list (final): ");
2294 debug_ready_list (&ready);
2295 if (!targetm.sched.use_dfa_pipeline_interface
2296 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2297 print_block_visualization ("");
2300 /* Sanity check -- queue must be empty now. Meaningless if region has
2301 multiple bbs. */
2302 if (current_sched_info->queue_must_finish_empty && q_size != 0)
2303 abort ();
2305 /* Update head/tail boundaries. */
2306 head = NEXT_INSN (prev_head);
2307 tail = last_scheduled_insn;
2309 if (!reload_completed)
2311 rtx insn, link, next;
2313 /* INSN_TICK (minimum clock tick at which the insn becomes
2314 ready) may be not correct for the insn in the subsequent
2315 blocks of the region. We should use a correct value of
2316 `clock_var' or modify INSN_TICK. It is better to keep
2317 clock_var value equal to 0 at the start of a basic block.
2318 Therefore we modify INSN_TICK here. */
2319 for (insn = head; insn != tail; insn = NEXT_INSN (insn))
2320 if (INSN_P (insn))
2322 for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
2324 next = XEXP (link, 0);
2325 INSN_TICK (next) -= clock_var;
2330 /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
2331 previously found among the insns. Insert them at the beginning
2332 of the insns. */
2333 if (note_list != 0)
2335 rtx note_head = note_list;
2337 while (PREV_INSN (note_head))
2339 note_head = PREV_INSN (note_head);
2342 PREV_INSN (note_head) = PREV_INSN (head);
2343 NEXT_INSN (PREV_INSN (head)) = note_head;
2344 PREV_INSN (head) = note_list;
2345 NEXT_INSN (note_list) = head;
2346 head = note_head;
2349 /* Debugging. */
2350 if (sched_verbose)
2352 fprintf (sched_dump, ";; total time = %d\n;; new head = %d\n",
2353 clock_var, INSN_UID (head));
2354 fprintf (sched_dump, ";; new tail = %d\n\n",
2355 INSN_UID (tail));
2356 visualize_free ();
2359 current_sched_info->head = head;
2360 current_sched_info->tail = tail;
2362 free (ready.vec);
2364 if (targetm.sched.use_dfa_pipeline_interface
2365 && (*targetm.sched.use_dfa_pipeline_interface) ())
2367 free (ready_try);
2368 for (i = 0; i <= rgn_n_insns; i++)
2369 free (choice_stack [i].state);
2370 free (choice_stack);
2374 /* Set_priorities: compute priority of each insn in the block. */
2377 set_priorities (head, tail)
2378 rtx head, tail;
2380 rtx insn;
2381 int n_insn;
2383 rtx prev_head;
2385 prev_head = PREV_INSN (head);
2387 if (head == tail && (! INSN_P (head)))
2388 return 0;
2390 n_insn = 0;
2391 for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
2393 if (GET_CODE (insn) == NOTE)
2394 continue;
2396 if (! SCHED_GROUP_P (insn))
2397 n_insn++;
2398 (void) priority (insn);
2401 return n_insn;
2404 /* Initialize some global state for the scheduler. DUMP_FILE is to be used
2405 for debugging output. */
2407 void
2408 sched_init (dump_file)
2409 FILE *dump_file;
2411 int luid;
2412 basic_block b;
2413 rtx insn;
2414 int i;
2416 /* Disable speculative loads in their presence if cc0 defined. */
2417 #ifdef HAVE_cc0
2418 flag_schedule_speculative_load = 0;
2419 #endif
2421 /* Set dump and sched_verbose for the desired debugging output. If no
2422 dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
2423 For -fsched-verbose=N, N>=10, print everything to stderr. */
2424 sched_verbose = sched_verbose_param;
2425 if (sched_verbose_param == 0 && dump_file)
2426 sched_verbose = 1;
2427 sched_dump = ((sched_verbose_param >= 10 || !dump_file)
2428 ? stderr : dump_file);
2430 /* Initialize issue_rate. */
2431 if (targetm.sched.issue_rate)
2432 issue_rate = (*targetm.sched.issue_rate) ();
2433 else
2434 issue_rate = 1;
2436 /* We use LUID 0 for the fake insn (UID 0) which holds dependencies for
2437 pseudos which do not cross calls. */
2438 old_max_uid = get_max_uid () + 1;
2440 h_i_d = (struct haifa_insn_data *) xcalloc (old_max_uid, sizeof (*h_i_d));
2442 for (i = 0; i < old_max_uid; i++)
2443 h_i_d [i].cost = -1;
2445 if (targetm.sched.use_dfa_pipeline_interface
2446 && (*targetm.sched.use_dfa_pipeline_interface) ())
2448 if (targetm.sched.init_dfa_pre_cycle_insn)
2449 (*targetm.sched.init_dfa_pre_cycle_insn) ();
2451 if (targetm.sched.init_dfa_post_cycle_insn)
2452 (*targetm.sched.init_dfa_post_cycle_insn) ();
2454 if (targetm.sched.first_cycle_multipass_dfa_lookahead
2455 && targetm.sched.init_dfa_bubbles)
2456 (*targetm.sched.init_dfa_bubbles) ();
2458 dfa_start ();
2459 dfa_state_size = state_size ();
2460 curr_state = xmalloc (dfa_state_size);
2463 h_i_d[0].luid = 0;
2464 luid = 1;
2465 FOR_EACH_BB (b)
2466 for (insn = b->head;; insn = NEXT_INSN (insn))
2468 INSN_LUID (insn) = luid;
2470 /* Increment the next luid, unless this is a note. We don't
2471 really need separate IDs for notes and we don't want to
2472 schedule differently depending on whether or not there are
2473 line-number notes, i.e., depending on whether or not we're
2474 generating debugging information. */
2475 if (GET_CODE (insn) != NOTE)
2476 ++luid;
2478 if (insn == b->end)
2479 break;
2482 init_dependency_caches (luid);
2484 init_alias_analysis ();
2486 if (write_symbols != NO_DEBUG)
2488 rtx line;
2490 line_note_head = (rtx *) xcalloc (last_basic_block, sizeof (rtx));
2492 /* Save-line-note-head:
2493 Determine the line-number at the start of each basic block.
2494 This must be computed and saved now, because after a basic block's
2495 predecessor has been scheduled, it is impossible to accurately
2496 determine the correct line number for the first insn of the block. */
2498 FOR_EACH_BB (b)
2500 for (line = b->head; line; line = PREV_INSN (line))
2501 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
2503 line_note_head[b->index] = line;
2504 break;
2506 /* Do a forward search as well, since we won't get to see the first
2507 notes in a basic block. */
2508 for (line = b->head; line; line = NEXT_INSN (line))
2510 if (INSN_P (line))
2511 break;
2512 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
2513 line_note_head[b->index] = line;
2518 if ((!targetm.sched.use_dfa_pipeline_interface
2519 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2520 && sched_verbose)
2521 /* Find units used in this function, for visualization. */
2522 init_target_units ();
2524 /* ??? Add a NOTE after the last insn of the last basic block. It is not
2525 known why this is done. */
2527 insn = EXIT_BLOCK_PTR->prev_bb->end;
2528 if (NEXT_INSN (insn) == 0
2529 || (GET_CODE (insn) != NOTE
2530 && GET_CODE (insn) != CODE_LABEL
2531 /* Don't emit a NOTE if it would end up before a BARRIER. */
2532 && GET_CODE (NEXT_INSN (insn)) != BARRIER))
2534 emit_note_after (NOTE_INSN_DELETED, EXIT_BLOCK_PTR->prev_bb->end);
2535 /* Make insn to appear outside BB. */
2536 EXIT_BLOCK_PTR->prev_bb->end = PREV_INSN (EXIT_BLOCK_PTR->prev_bb->end);
2539 /* Compute INSN_REG_WEIGHT for all blocks. We must do this before
2540 removing death notes. */
2541 FOR_EACH_BB_REVERSE (b)
2542 find_insn_reg_weight (b->index);
2545 /* Free global data used during insn scheduling. */
2547 void
2548 sched_finish ()
2550 free (h_i_d);
2552 if (targetm.sched.use_dfa_pipeline_interface
2553 && (*targetm.sched.use_dfa_pipeline_interface) ())
2555 free (curr_state);
2556 dfa_finish ();
2558 free_dependency_caches ();
2559 end_alias_analysis ();
2560 if (write_symbols != NO_DEBUG)
2561 free (line_note_head);
2563 #endif /* INSN_SCHEDULING */