i386-protos.h (x86_emit_floatuns): Declare.
[official-gcc.git] / gcc / haifa-sched.c
blobef4ec0a0786ec1e828e1a60f685d2b9f2984c268
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 int 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 /* The insn in a schedule group should be issued the first. */
856 if (SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
857 return SCHED_GROUP_P (tmp2) ? 1 : -1;
859 /* Prefer insn with higher priority. */
860 priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
862 if (priority_val)
863 return priority_val;
865 /* Prefer an insn with smaller contribution to registers-pressure. */
866 if (!reload_completed &&
867 (weight_val = INSN_REG_WEIGHT (tmp) - INSN_REG_WEIGHT (tmp2)))
868 return weight_val;
870 info_val = (*current_sched_info->rank) (tmp, tmp2);
871 if (info_val)
872 return info_val;
874 /* Compare insns based on their relation to the last-scheduled-insn. */
875 if (last_scheduled_insn)
877 /* Classify the instructions into three classes:
878 1) Data dependent on last schedule insn.
879 2) Anti/Output dependent on last scheduled insn.
880 3) Independent of last scheduled insn, or has latency of one.
881 Choose the insn from the highest numbered class if different. */
882 link = find_insn_list (tmp, INSN_DEPEND (last_scheduled_insn));
883 if (link == 0 || insn_cost (last_scheduled_insn, link, tmp) == 1)
884 tmp_class = 3;
885 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
886 tmp_class = 1;
887 else
888 tmp_class = 2;
890 link = find_insn_list (tmp2, INSN_DEPEND (last_scheduled_insn));
891 if (link == 0 || insn_cost (last_scheduled_insn, link, tmp2) == 1)
892 tmp2_class = 3;
893 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
894 tmp2_class = 1;
895 else
896 tmp2_class = 2;
898 if ((val = tmp2_class - tmp_class))
899 return val;
902 /* Prefer the insn which has more later insns that depend on it.
903 This gives the scheduler more freedom when scheduling later
904 instructions at the expense of added register pressure. */
905 depend_count1 = 0;
906 for (link = INSN_DEPEND (tmp); link; link = XEXP (link, 1))
907 depend_count1++;
909 depend_count2 = 0;
910 for (link = INSN_DEPEND (tmp2); link; link = XEXP (link, 1))
911 depend_count2++;
913 val = depend_count2 - depend_count1;
914 if (val)
915 return val;
917 /* If insns are equally good, sort by INSN_LUID (original insn order),
918 so that we make the sort stable. This minimizes instruction movement,
919 thus minimizing sched's effect on debugging and cross-jumping. */
920 return INSN_LUID (tmp) - INSN_LUID (tmp2);
923 /* Resort the array A in which only element at index N may be out of order. */
925 HAIFA_INLINE static void
926 swap_sort (a, n)
927 rtx *a;
928 int n;
930 rtx insn = a[n - 1];
931 int i = n - 2;
933 while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
935 a[i + 1] = a[i];
936 i -= 1;
938 a[i + 1] = insn;
941 /* Add INSN to the insn queue so that it can be executed at least
942 N_CYCLES after the currently executing insn. Preserve insns
943 chain for debugging purposes. */
945 HAIFA_INLINE static void
946 queue_insn (insn, n_cycles)
947 rtx insn;
948 int n_cycles;
950 int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
951 rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
952 insn_queue[next_q] = link;
953 q_size += 1;
955 if (sched_verbose >= 2)
957 fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
958 (*current_sched_info->print_insn) (insn, 0));
960 fprintf (sched_dump, "queued for %d cycles.\n", n_cycles);
964 /* Return a pointer to the bottom of the ready list, i.e. the insn
965 with the lowest priority. */
967 HAIFA_INLINE static rtx *
968 ready_lastpos (ready)
969 struct ready_list *ready;
971 if (ready->n_ready == 0)
972 abort ();
973 return ready->vec + ready->first - ready->n_ready + 1;
976 /* Add an element INSN to the ready list so that it ends up with the lowest
977 priority. */
979 HAIFA_INLINE void
980 ready_add (ready, insn)
981 struct ready_list *ready;
982 rtx insn;
984 if (ready->first == ready->n_ready)
986 memmove (ready->vec + ready->veclen - ready->n_ready,
987 ready_lastpos (ready),
988 ready->n_ready * sizeof (rtx));
989 ready->first = ready->veclen - 1;
991 ready->vec[ready->first - ready->n_ready] = insn;
992 ready->n_ready++;
995 /* Remove the element with the highest priority from the ready list and
996 return it. */
998 HAIFA_INLINE static rtx
999 ready_remove_first (ready)
1000 struct ready_list *ready;
1002 rtx t;
1003 if (ready->n_ready == 0)
1004 abort ();
1005 t = ready->vec[ready->first--];
1006 ready->n_ready--;
1007 /* If the queue becomes empty, reset it. */
1008 if (ready->n_ready == 0)
1009 ready->first = ready->veclen - 1;
1010 return t;
1013 /* The following code implements multi-pass scheduling for the first
1014 cycle. In other words, we will try to choose ready insn which
1015 permits to start maximum number of insns on the same cycle. */
1017 /* Return a pointer to the element INDEX from the ready. INDEX for
1018 insn with the highest priority is 0, and the lowest priority has
1019 N_READY - 1. */
1021 HAIFA_INLINE static rtx
1022 ready_element (ready, index)
1023 struct ready_list *ready;
1024 int index;
1026 #ifdef ENABLE_CHECKING
1027 if (ready->n_ready == 0 || index >= ready->n_ready)
1028 abort ();
1029 #endif
1030 return ready->vec[ready->first - index];
1033 /* Remove the element INDEX from the ready list and return it. INDEX
1034 for insn with the highest priority is 0, and the lowest priority
1035 has N_READY - 1. */
1037 HAIFA_INLINE static rtx
1038 ready_remove (ready, index)
1039 struct ready_list *ready;
1040 int index;
1042 rtx t;
1043 int i;
1045 if (index == 0)
1046 return ready_remove_first (ready);
1047 if (ready->n_ready == 0 || index >= ready->n_ready)
1048 abort ();
1049 t = ready->vec[ready->first - index];
1050 ready->n_ready--;
1051 for (i = index; i < ready->n_ready; i++)
1052 ready->vec[ready->first - i] = ready->vec[ready->first - i - 1];
1053 return t;
1057 /* Sort the ready list READY by ascending priority, using the SCHED_SORT
1058 macro. */
1060 HAIFA_INLINE static void
1061 ready_sort (ready)
1062 struct ready_list *ready;
1064 rtx *first = ready_lastpos (ready);
1065 SCHED_SORT (first, ready->n_ready);
1068 /* PREV is an insn that is ready to execute. Adjust its priority if that
1069 will help shorten or lengthen register lifetimes as appropriate. Also
1070 provide a hook for the target to tweek itself. */
1072 HAIFA_INLINE static void
1073 adjust_priority (prev)
1074 rtx prev;
1076 /* ??? There used to be code here to try and estimate how an insn
1077 affected register lifetimes, but it did it by looking at REG_DEAD
1078 notes, which we removed in schedule_region. Nor did it try to
1079 take into account register pressure or anything useful like that.
1081 Revisit when we have a machine model to work with and not before. */
1083 if (targetm.sched.adjust_priority)
1084 INSN_PRIORITY (prev) =
1085 (*targetm.sched.adjust_priority) (prev, INSN_PRIORITY (prev));
1088 /* Advance time on one cycle. */
1089 HAIFA_INLINE static void
1090 advance_one_cycle ()
1092 if (targetm.sched.use_dfa_pipeline_interface
1093 && (*targetm.sched.use_dfa_pipeline_interface) ())
1095 if (targetm.sched.dfa_pre_cycle_insn)
1096 state_transition (curr_state,
1097 (*targetm.sched.dfa_pre_cycle_insn) ());
1099 state_transition (curr_state, NULL);
1101 if (targetm.sched.dfa_post_cycle_insn)
1102 state_transition (curr_state,
1103 (*targetm.sched.dfa_post_cycle_insn) ());
1107 /* Clock at which the previous instruction was issued. */
1108 static int last_clock_var;
1110 /* INSN is the "currently executing insn". Launch each insn which was
1111 waiting on INSN. READY is the ready list which contains the insns
1112 that are ready to fire. CLOCK is the current cycle. The function
1113 returns necessary cycle advance after issuing the insn (it is not
1114 zero for insns in a schedule group). */
1116 static int
1117 schedule_insn (insn, ready, clock)
1118 rtx insn;
1119 struct ready_list *ready;
1120 int clock;
1122 rtx link;
1123 int advance = 0;
1124 int unit = 0;
1126 if (!targetm.sched.use_dfa_pipeline_interface
1127 || !(*targetm.sched.use_dfa_pipeline_interface) ())
1128 unit = insn_unit (insn);
1130 if (targetm.sched.use_dfa_pipeline_interface
1131 && (*targetm.sched.use_dfa_pipeline_interface) ()
1132 && sched_verbose >= 1)
1134 char buf[2048];
1136 print_insn (buf, insn, 0);
1137 buf[40]=0;
1138 fprintf (sched_dump, ";;\t%3i--> %-40s:", clock, buf);
1140 if (recog_memoized (insn) < 0)
1141 fprintf (sched_dump, "nothing");
1142 else
1143 print_reservation (sched_dump, insn);
1144 fputc ('\n', sched_dump);
1146 else if (sched_verbose >= 2)
1148 fprintf (sched_dump, ";;\t\t--> scheduling insn <<<%d>>> on unit ",
1149 INSN_UID (insn));
1150 insn_print_units (insn);
1151 fputc ('\n', sched_dump);
1154 if (!targetm.sched.use_dfa_pipeline_interface
1155 || !(*targetm.sched.use_dfa_pipeline_interface) ())
1157 if (sched_verbose && unit == -1)
1158 visualize_no_unit (insn);
1161 if (MAX_BLOCKAGE > 1 || issue_rate > 1 || sched_verbose)
1162 schedule_unit (unit, insn, clock);
1164 if (INSN_DEPEND (insn) == 0)
1165 return 0;
1168 for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
1170 rtx next = XEXP (link, 0);
1171 int cost = insn_cost (insn, link, next);
1173 INSN_TICK (next) = MAX (INSN_TICK (next), clock + cost);
1175 if ((INSN_DEP_COUNT (next) -= 1) == 0)
1177 int effective_cost = INSN_TICK (next) - clock;
1179 if (! (*current_sched_info->new_ready) (next))
1180 continue;
1182 if (sched_verbose >= 2)
1184 fprintf (sched_dump, ";;\t\tdependences resolved: insn %s ",
1185 (*current_sched_info->print_insn) (next, 0));
1187 if (effective_cost < 1)
1188 fprintf (sched_dump, "into ready\n");
1189 else
1190 fprintf (sched_dump, "into queue with cost=%d\n",
1191 effective_cost);
1194 /* Adjust the priority of NEXT and either put it on the ready
1195 list or queue it. */
1196 adjust_priority (next);
1197 if (effective_cost < 1)
1198 ready_add (ready, next);
1199 else
1201 queue_insn (next, effective_cost);
1203 if (SCHED_GROUP_P (next) && advance < effective_cost)
1204 advance = effective_cost;
1209 /* Annotate the instruction with issue information -- TImode
1210 indicates that the instruction is expected not to be able
1211 to issue on the same cycle as the previous insn. A machine
1212 may use this information to decide how the instruction should
1213 be aligned. */
1214 if (issue_rate > 1
1215 && GET_CODE (PATTERN (insn)) != USE
1216 && GET_CODE (PATTERN (insn)) != CLOBBER)
1218 if (reload_completed)
1219 PUT_MODE (insn, clock > last_clock_var ? TImode : VOIDmode);
1220 last_clock_var = clock;
1222 return advance;
1225 /* Functions for handling of notes. */
1227 /* Delete notes beginning with INSN and put them in the chain
1228 of notes ended by NOTE_LIST.
1229 Returns the insn following the notes. */
1231 static rtx
1232 unlink_other_notes (insn, tail)
1233 rtx insn, tail;
1235 rtx prev = PREV_INSN (insn);
1237 while (insn != tail && GET_CODE (insn) == NOTE)
1239 rtx next = NEXT_INSN (insn);
1240 /* Delete the note from its current position. */
1241 if (prev)
1242 NEXT_INSN (prev) = next;
1243 if (next)
1244 PREV_INSN (next) = prev;
1246 /* See sched_analyze to see how these are handled. */
1247 if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG
1248 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_END
1249 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_BEG
1250 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_END)
1252 /* Insert the note at the end of the notes list. */
1253 PREV_INSN (insn) = note_list;
1254 if (note_list)
1255 NEXT_INSN (note_list) = insn;
1256 note_list = insn;
1259 insn = next;
1261 return insn;
1264 /* Delete line notes beginning with INSN. Record line-number notes so
1265 they can be reused. Returns the insn following the notes. */
1267 static rtx
1268 unlink_line_notes (insn, tail)
1269 rtx insn, tail;
1271 rtx prev = PREV_INSN (insn);
1273 while (insn != tail && GET_CODE (insn) == NOTE)
1275 rtx next = NEXT_INSN (insn);
1277 if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
1279 /* Delete the note from its current position. */
1280 if (prev)
1281 NEXT_INSN (prev) = next;
1282 if (next)
1283 PREV_INSN (next) = prev;
1285 /* Record line-number notes so they can be reused. */
1286 LINE_NOTE (insn) = insn;
1288 else
1289 prev = insn;
1291 insn = next;
1293 return insn;
1296 /* Return the head and tail pointers of BB. */
1298 void
1299 get_block_head_tail (b, headp, tailp)
1300 int b;
1301 rtx *headp;
1302 rtx *tailp;
1304 /* HEAD and TAIL delimit the basic block being scheduled. */
1305 rtx head = BLOCK_HEAD (b);
1306 rtx tail = BLOCK_END (b);
1308 /* Don't include any notes or labels at the beginning of the
1309 basic block, or notes at the ends of basic blocks. */
1310 while (head != tail)
1312 if (GET_CODE (head) == NOTE)
1313 head = NEXT_INSN (head);
1314 else if (GET_CODE (tail) == NOTE)
1315 tail = PREV_INSN (tail);
1316 else if (GET_CODE (head) == CODE_LABEL)
1317 head = NEXT_INSN (head);
1318 else
1319 break;
1322 *headp = head;
1323 *tailp = tail;
1326 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ]. */
1329 no_real_insns_p (head, tail)
1330 rtx head, tail;
1332 while (head != NEXT_INSN (tail))
1334 if (GET_CODE (head) != NOTE && GET_CODE (head) != CODE_LABEL)
1335 return 0;
1336 head = NEXT_INSN (head);
1338 return 1;
1341 /* Delete line notes from one block. Save them so they can be later restored
1342 (in restore_line_notes). HEAD and TAIL are the boundaries of the
1343 block in which notes should be processed. */
1345 void
1346 rm_line_notes (head, tail)
1347 rtx head, tail;
1349 rtx next_tail;
1350 rtx insn;
1352 next_tail = NEXT_INSN (tail);
1353 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1355 rtx prev;
1357 /* Farm out notes, and maybe save them in NOTE_LIST.
1358 This is needed to keep the debugger from
1359 getting completely deranged. */
1360 if (GET_CODE (insn) == NOTE)
1362 prev = insn;
1363 insn = unlink_line_notes (insn, next_tail);
1365 if (prev == tail)
1366 abort ();
1367 if (prev == head)
1368 abort ();
1369 if (insn == next_tail)
1370 abort ();
1375 /* Save line number notes for each insn in block B. HEAD and TAIL are
1376 the boundaries of the block in which notes should be processed. */
1378 void
1379 save_line_notes (b, head, tail)
1380 int b;
1381 rtx head, tail;
1383 rtx next_tail;
1385 /* We must use the true line number for the first insn in the block
1386 that was computed and saved at the start of this pass. We can't
1387 use the current line number, because scheduling of the previous
1388 block may have changed the current line number. */
1390 rtx line = line_note_head[b];
1391 rtx insn;
1393 next_tail = NEXT_INSN (tail);
1395 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1396 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1397 line = insn;
1398 else
1399 LINE_NOTE (insn) = line;
1402 /* After a block was scheduled, insert line notes into the insns list.
1403 HEAD and TAIL are the boundaries of the block in which notes should
1404 be processed. */
1406 void
1407 restore_line_notes (head, tail)
1408 rtx head, tail;
1410 rtx line, note, prev, new;
1411 int added_notes = 0;
1412 rtx next_tail, insn;
1414 head = head;
1415 next_tail = NEXT_INSN (tail);
1417 /* Determine the current line-number. We want to know the current
1418 line number of the first insn of the block here, in case it is
1419 different from the true line number that was saved earlier. If
1420 different, then we need a line number note before the first insn
1421 of this block. If it happens to be the same, then we don't want to
1422 emit another line number note here. */
1423 for (line = head; line; line = PREV_INSN (line))
1424 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
1425 break;
1427 /* Walk the insns keeping track of the current line-number and inserting
1428 the line-number notes as needed. */
1429 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1430 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1431 line = insn;
1432 /* This used to emit line number notes before every non-deleted note.
1433 However, this confuses a debugger, because line notes not separated
1434 by real instructions all end up at the same address. I can find no
1435 use for line number notes before other notes, so none are emitted. */
1436 else if (GET_CODE (insn) != NOTE
1437 && INSN_UID (insn) < old_max_uid
1438 && (note = LINE_NOTE (insn)) != 0
1439 && note != line
1440 && (line == 0
1441 || NOTE_LINE_NUMBER (note) != NOTE_LINE_NUMBER (line)
1442 || NOTE_SOURCE_FILE (note) != NOTE_SOURCE_FILE (line)))
1444 line = note;
1445 prev = PREV_INSN (insn);
1446 if (LINE_NOTE (note))
1448 /* Re-use the original line-number note. */
1449 LINE_NOTE (note) = 0;
1450 PREV_INSN (note) = prev;
1451 NEXT_INSN (prev) = note;
1452 PREV_INSN (insn) = note;
1453 NEXT_INSN (note) = insn;
1455 else
1457 added_notes++;
1458 new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
1459 NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
1460 RTX_INTEGRATED_P (new) = RTX_INTEGRATED_P (note);
1463 if (sched_verbose && added_notes)
1464 fprintf (sched_dump, ";; added %d line-number notes\n", added_notes);
1467 /* After scheduling the function, delete redundant line notes from the
1468 insns list. */
1470 void
1471 rm_redundant_line_notes ()
1473 rtx line = 0;
1474 rtx insn = get_insns ();
1475 int active_insn = 0;
1476 int notes = 0;
1478 /* Walk the insns deleting redundant line-number notes. Many of these
1479 are already present. The remainder tend to occur at basic
1480 block boundaries. */
1481 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1482 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1484 /* If there are no active insns following, INSN is redundant. */
1485 if (active_insn == 0)
1487 notes++;
1488 NOTE_SOURCE_FILE (insn) = 0;
1489 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1491 /* If the line number is unchanged, LINE is redundant. */
1492 else if (line
1493 && NOTE_LINE_NUMBER (line) == NOTE_LINE_NUMBER (insn)
1494 && NOTE_SOURCE_FILE (line) == NOTE_SOURCE_FILE (insn))
1496 notes++;
1497 NOTE_SOURCE_FILE (line) = 0;
1498 NOTE_LINE_NUMBER (line) = NOTE_INSN_DELETED;
1499 line = insn;
1501 else
1502 line = insn;
1503 active_insn = 0;
1505 else if (!((GET_CODE (insn) == NOTE
1506 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
1507 || (GET_CODE (insn) == INSN
1508 && (GET_CODE (PATTERN (insn)) == USE
1509 || GET_CODE (PATTERN (insn)) == CLOBBER))))
1510 active_insn++;
1512 if (sched_verbose && notes)
1513 fprintf (sched_dump, ";; deleted %d line-number notes\n", notes);
1516 /* Delete notes between HEAD and TAIL and put them in the chain
1517 of notes ended by NOTE_LIST. */
1519 void
1520 rm_other_notes (head, tail)
1521 rtx head;
1522 rtx tail;
1524 rtx next_tail;
1525 rtx insn;
1527 note_list = 0;
1528 if (head == tail && (! INSN_P (head)))
1529 return;
1531 next_tail = NEXT_INSN (tail);
1532 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1534 rtx prev;
1536 /* Farm out notes, and maybe save them in NOTE_LIST.
1537 This is needed to keep the debugger from
1538 getting completely deranged. */
1539 if (GET_CODE (insn) == NOTE)
1541 prev = insn;
1543 insn = unlink_other_notes (insn, next_tail);
1545 if (prev == tail)
1546 abort ();
1547 if (prev == head)
1548 abort ();
1549 if (insn == next_tail)
1550 abort ();
1555 /* Functions for computation of registers live/usage info. */
1557 /* This function looks for a new register being defined.
1558 If the destination register is already used by the source,
1559 a new register is not needed. */
1561 static int
1562 find_set_reg_weight (x)
1563 rtx x;
1565 if (GET_CODE (x) == CLOBBER
1566 && register_operand (SET_DEST (x), VOIDmode))
1567 return 1;
1568 if (GET_CODE (x) == SET
1569 && register_operand (SET_DEST (x), VOIDmode))
1571 if (GET_CODE (SET_DEST (x)) == REG)
1573 if (!reg_mentioned_p (SET_DEST (x), SET_SRC (x)))
1574 return 1;
1575 else
1576 return 0;
1578 return 1;
1580 return 0;
1583 /* Calculate INSN_REG_WEIGHT for all insns of a block. */
1585 static void
1586 find_insn_reg_weight (b)
1587 int b;
1589 rtx insn, next_tail, head, tail;
1591 get_block_head_tail (b, &head, &tail);
1592 next_tail = NEXT_INSN (tail);
1594 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1596 int reg_weight = 0;
1597 rtx x;
1599 /* Handle register life information. */
1600 if (! INSN_P (insn))
1601 continue;
1603 /* Increment weight for each register born here. */
1604 x = PATTERN (insn);
1605 reg_weight += find_set_reg_weight (x);
1606 if (GET_CODE (x) == PARALLEL)
1608 int j;
1609 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
1611 x = XVECEXP (PATTERN (insn), 0, j);
1612 reg_weight += find_set_reg_weight (x);
1615 /* Decrement weight for each register that dies here. */
1616 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1618 if (REG_NOTE_KIND (x) == REG_DEAD
1619 || REG_NOTE_KIND (x) == REG_UNUSED)
1620 reg_weight--;
1623 INSN_REG_WEIGHT (insn) = reg_weight;
1627 /* Scheduling clock, modified in schedule_block() and queue_to_ready (). */
1628 static int clock_var;
1630 /* Move insns that became ready to fire from queue to ready list. */
1632 static void
1633 queue_to_ready (ready)
1634 struct ready_list *ready;
1636 rtx insn;
1637 rtx link;
1639 q_ptr = NEXT_Q (q_ptr);
1641 /* Add all pending insns that can be scheduled without stalls to the
1642 ready list. */
1643 for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
1645 insn = XEXP (link, 0);
1646 q_size -= 1;
1648 if (sched_verbose >= 2)
1649 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1650 (*current_sched_info->print_insn) (insn, 0));
1652 ready_add (ready, insn);
1653 if (sched_verbose >= 2)
1654 fprintf (sched_dump, "moving to ready without stalls\n");
1656 insn_queue[q_ptr] = 0;
1658 /* If there are no ready insns, stall until one is ready and add all
1659 of the pending insns at that point to the ready list. */
1660 if (ready->n_ready == 0)
1662 int stalls;
1664 for (stalls = 1; stalls <= MAX_INSN_QUEUE_INDEX; stalls++)
1666 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
1668 for (; link; link = XEXP (link, 1))
1670 insn = XEXP (link, 0);
1671 q_size -= 1;
1673 if (sched_verbose >= 2)
1674 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1675 (*current_sched_info->print_insn) (insn, 0));
1677 ready_add (ready, insn);
1678 if (sched_verbose >= 2)
1679 fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
1681 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = 0;
1683 advance_one_cycle ();
1685 break;
1688 advance_one_cycle ();
1691 if ((!targetm.sched.use_dfa_pipeline_interface
1692 || !(*targetm.sched.use_dfa_pipeline_interface) ())
1693 && sched_verbose && stalls)
1694 visualize_stall_cycles (stalls);
1696 q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
1697 clock_var += stalls;
1701 /* Print the ready list for debugging purposes. Callable from debugger. */
1703 static void
1704 debug_ready_list (ready)
1705 struct ready_list *ready;
1707 rtx *p;
1708 int i;
1710 if (ready->n_ready == 0)
1712 fprintf (sched_dump, "\n");
1713 return;
1716 p = ready_lastpos (ready);
1717 for (i = 0; i < ready->n_ready; i++)
1718 fprintf (sched_dump, " %s", (*current_sched_info->print_insn) (p[i], 0));
1719 fprintf (sched_dump, "\n");
1722 /* move_insn1: Remove INSN from insn chain, and link it after LAST insn. */
1724 static rtx
1725 move_insn1 (insn, last)
1726 rtx insn, last;
1728 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
1729 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
1731 NEXT_INSN (insn) = NEXT_INSN (last);
1732 PREV_INSN (NEXT_INSN (last)) = insn;
1734 NEXT_INSN (last) = insn;
1735 PREV_INSN (insn) = last;
1737 return insn;
1740 /* Search INSN for REG_SAVE_NOTE note pairs for
1741 NOTE_INSN_{LOOP,EHREGION}_{BEG,END}; and convert them back into
1742 NOTEs. The REG_SAVE_NOTE note following first one is contains the
1743 saved value for NOTE_BLOCK_NUMBER which is useful for
1744 NOTE_INSN_EH_REGION_{BEG,END} NOTEs. LAST is the last instruction
1745 output by the instruction scheduler. Return the new value of LAST. */
1747 static rtx
1748 reemit_notes (insn, last)
1749 rtx insn;
1750 rtx last;
1752 rtx note, retval;
1754 retval = last;
1755 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1757 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
1759 enum insn_note note_type = INTVAL (XEXP (note, 0));
1761 last = emit_note_before (note_type, last);
1762 remove_note (insn, note);
1763 note = XEXP (note, 1);
1764 if (note_type == NOTE_INSN_EH_REGION_BEG
1765 || note_type == NOTE_INSN_EH_REGION_END)
1766 NOTE_EH_HANDLER (last) = INTVAL (XEXP (note, 0));
1767 remove_note (insn, note);
1770 return retval;
1773 /* Move INSN. Reemit notes if needed.
1775 Return the last insn emitted by the scheduler, which is the
1776 return value from the first call to reemit_notes. */
1778 static rtx
1779 move_insn (insn, last)
1780 rtx insn, last;
1782 rtx retval = NULL;
1784 move_insn1 (insn, last);
1786 /* If this is the first call to reemit_notes, then record
1787 its return value. */
1788 if (retval == NULL_RTX)
1789 retval = reemit_notes (insn, insn);
1790 else
1791 reemit_notes (insn, insn);
1793 SCHED_GROUP_P (insn) = 0;
1795 return retval;
1798 /* The following structure describe an entry of the stack of choices. */
1799 struct choice_entry
1801 /* Ordinal number of the issued insn in the ready queue. */
1802 int index;
1803 /* The number of the rest insns whose issues we should try. */
1804 int rest;
1805 /* The number of issued essential insns. */
1806 int n;
1807 /* State after issuing the insn. */
1808 state_t state;
1811 /* The following array is used to implement a stack of choices used in
1812 function max_issue. */
1813 static struct choice_entry *choice_stack;
1815 /* The following variable value is number of essential insns issued on
1816 the current cycle. An insn is essential one if it changes the
1817 processors state. */
1818 static int cycle_issued_insns;
1820 /* The following function returns maximal (or close to maximal) number
1821 of insns which can be issued on the same cycle and one of which
1822 insns is insns with the best rank (the first insn in READY). To
1823 make this function tries different samples of ready insns. READY
1824 is current queue `ready'. Global array READY_TRY reflects what
1825 insns are already issued in this try. INDEX will contain index
1826 of the best insn in READY. The following function is used only for
1827 first cycle multipass scheduling. */
1828 static int
1829 max_issue (ready, index)
1830 struct ready_list *ready;
1831 int *index;
1833 int n, i, all, n_ready, lookahead, best, delay;
1834 struct choice_entry *top;
1835 rtx insn;
1837 lookahead = (*targetm.sched.first_cycle_multipass_dfa_lookahead) ();
1838 best = 0;
1839 memcpy (choice_stack->state, curr_state, dfa_state_size);
1840 top = choice_stack;
1841 top->rest = lookahead;
1842 top->n = 0;
1843 n_ready = ready->n_ready;
1844 for (all = i = 0; i < n_ready; i++)
1845 if (!ready_try [i])
1846 all++;
1847 i = 0;
1848 for (;;)
1850 if (top->rest == 0 || i >= n_ready)
1852 if (top == choice_stack)
1853 break;
1854 if (best < top - choice_stack && ready_try [0])
1856 best = top - choice_stack;
1857 *index = choice_stack [1].index;
1858 if (top->n == issue_rate - cycle_issued_insns || best == all)
1859 break;
1861 i = top->index;
1862 ready_try [i] = 0;
1863 top--;
1864 memcpy (curr_state, top->state, dfa_state_size);
1866 else if (!ready_try [i])
1868 insn = ready_element (ready, i);
1869 delay = state_transition (curr_state, insn);
1870 if (delay < 0)
1872 if (state_dead_lock_p (curr_state))
1873 top->rest = 0;
1874 else
1875 top->rest--;
1876 n = top->n;
1877 if (memcmp (top->state, curr_state, dfa_state_size) != 0)
1878 n++;
1879 top++;
1880 top->rest = lookahead;
1881 top->index = i;
1882 top->n = n;
1883 memcpy (top->state, curr_state, dfa_state_size);
1884 ready_try [i] = 1;
1885 i = -1;
1888 i++;
1890 while (top != choice_stack)
1892 ready_try [top->index] = 0;
1893 top--;
1895 memcpy (curr_state, choice_stack->state, dfa_state_size);
1896 return best;
1899 /* The following function chooses insn from READY and modifies
1900 *N_READY and READY. The following function is used only for first
1901 cycle multipass scheduling. */
1903 static rtx
1904 choose_ready (ready)
1905 struct ready_list *ready;
1907 if (!targetm.sched.first_cycle_multipass_dfa_lookahead
1908 || (*targetm.sched.first_cycle_multipass_dfa_lookahead) () <= 0
1909 || SCHED_GROUP_P (ready_element (ready, 0)))
1910 return ready_remove_first (ready);
1911 else
1913 /* Try to choose the better insn. */
1914 int index, i;
1915 rtx insn;
1917 insn = ready_element (ready, 0);
1918 if (INSN_CODE (insn) < 0)
1919 return ready_remove_first (ready);
1920 for (i = 1; i < ready->n_ready; i++)
1922 insn = ready_element (ready, i);
1923 ready_try [i]
1924 = (INSN_CODE (insn) < 0
1925 || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard
1926 && !(*targetm.sched.first_cycle_multipass_dfa_lookahead_guard) (insn)));
1928 if (max_issue (ready, &index) == 0)
1929 return ready_remove_first (ready);
1930 else
1931 return ready_remove (ready, index);
1935 /* Called from backends from targetm.sched.reorder to emit stuff into
1936 the instruction stream. */
1939 sched_emit_insn (pat)
1940 rtx pat;
1942 rtx insn = emit_insn_after (pat, last_scheduled_insn);
1943 last_scheduled_insn = insn;
1944 return insn;
1947 /* Use forward list scheduling to rearrange insns of block B in region RGN,
1948 possibly bringing insns from subsequent blocks in the same region. */
1950 void
1951 schedule_block (b, rgn_n_insns)
1952 int b;
1953 int rgn_n_insns;
1955 struct ready_list ready;
1956 int i, first_cycle_insn_p;
1957 int can_issue_more;
1958 state_t temp_state = NULL; /* It is used for multipass scheduling. */
1959 int sort_p, advance, start_clock_var;
1961 /* Head/tail info for this block. */
1962 rtx prev_head = current_sched_info->prev_head;
1963 rtx next_tail = current_sched_info->next_tail;
1964 rtx head = NEXT_INSN (prev_head);
1965 rtx tail = PREV_INSN (next_tail);
1967 /* We used to have code to avoid getting parameters moved from hard
1968 argument registers into pseudos.
1970 However, it was removed when it proved to be of marginal benefit
1971 and caused problems because schedule_block and compute_forward_dependences
1972 had different notions of what the "head" insn was. */
1974 if (head == tail && (! INSN_P (head)))
1975 abort ();
1977 /* Debug info. */
1978 if (sched_verbose)
1980 fprintf (sched_dump, ";; ======================================================\n");
1981 fprintf (sched_dump,
1982 ";; -- basic block %d from %d to %d -- %s reload\n",
1983 b, INSN_UID (head), INSN_UID (tail),
1984 (reload_completed ? "after" : "before"));
1985 fprintf (sched_dump, ";; ======================================================\n");
1986 fprintf (sched_dump, "\n");
1988 visualize_alloc ();
1989 init_block_visualization ();
1992 if (targetm.sched.use_dfa_pipeline_interface
1993 && (*targetm.sched.use_dfa_pipeline_interface) ())
1994 state_reset (curr_state);
1995 else
1996 clear_units ();
1998 /* Allocate the ready list. */
1999 ready.veclen = rgn_n_insns + 1 + issue_rate;
2000 ready.first = ready.veclen - 1;
2001 ready.vec = (rtx *) xmalloc (ready.veclen * sizeof (rtx));
2002 ready.n_ready = 0;
2004 if (targetm.sched.use_dfa_pipeline_interface
2005 && (*targetm.sched.use_dfa_pipeline_interface) ())
2007 /* It is used for first cycle multipass scheduling. */
2008 temp_state = alloca (dfa_state_size);
2009 ready_try = (char *) xmalloc ((rgn_n_insns + 1) * sizeof (char));
2010 memset (ready_try, 0, (rgn_n_insns + 1) * sizeof (char));
2011 choice_stack
2012 = (struct choice_entry *) xmalloc ((rgn_n_insns + 1)
2013 * sizeof (struct choice_entry));
2014 for (i = 0; i <= rgn_n_insns; i++)
2015 choice_stack[i].state = (state_t) xmalloc (dfa_state_size);
2018 (*current_sched_info->init_ready_list) (&ready);
2020 if (targetm.sched.md_init)
2021 (*targetm.sched.md_init) (sched_dump, sched_verbose, ready.veclen);
2023 /* We start inserting insns after PREV_HEAD. */
2024 last_scheduled_insn = prev_head;
2026 /* Initialize INSN_QUEUE. Q_SIZE is the total number of insns in the
2027 queue. */
2028 q_ptr = 0;
2029 q_size = 0;
2031 if (!targetm.sched.use_dfa_pipeline_interface
2032 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2033 max_insn_queue_index_macro_value = INSN_QUEUE_SIZE - 1;
2034 else
2035 max_insn_queue_index_macro_value = max_insn_queue_index;
2037 insn_queue = (rtx *) alloca ((MAX_INSN_QUEUE_INDEX + 1) * sizeof (rtx));
2038 memset ((char *) insn_queue, 0, (MAX_INSN_QUEUE_INDEX + 1) * sizeof (rtx));
2039 last_clock_var = -1;
2041 /* Start just before the beginning of time. */
2042 clock_var = -1;
2043 advance = 0;
2045 sort_p = TRUE;
2046 /* Loop until all the insns in BB are scheduled. */
2047 while ((*current_sched_info->schedule_more_p) ())
2051 start_clock_var = clock_var;
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);
2071 advance -= clock_var - start_clock_var;
2073 while (advance > 0);
2075 if (sort_p)
2077 /* Sort the ready list based on priority. */
2078 ready_sort (&ready);
2080 if (sched_verbose >= 2)
2082 fprintf (sched_dump, ";;\t\tReady list after ready_sort: ");
2083 debug_ready_list (&ready);
2087 /* Allow the target to reorder the list, typically for
2088 better instruction bundling. */
2089 if (targetm.sched.reorder
2090 && (ready.n_ready == 0
2091 || !SCHED_GROUP_P (ready_element (&ready, 0))))
2092 can_issue_more =
2093 (*targetm.sched.reorder) (sched_dump, sched_verbose,
2094 ready_lastpos (&ready),
2095 &ready.n_ready, clock_var);
2096 else
2097 can_issue_more = issue_rate;
2099 first_cycle_insn_p = 1;
2100 cycle_issued_insns = 0;
2101 for (;;)
2103 rtx insn;
2104 int cost;
2106 if (sched_verbose >= 2)
2108 fprintf (sched_dump, ";;\tReady list (t =%3d): ",
2109 clock_var);
2110 debug_ready_list (&ready);
2113 if (!targetm.sched.use_dfa_pipeline_interface
2114 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2116 if (ready.n_ready == 0 || !can_issue_more
2117 || !(*current_sched_info->schedule_more_p) ())
2118 break;
2119 insn = choose_ready (&ready);
2120 cost = actual_hazard (insn_unit (insn), insn, clock_var, 0);
2122 else
2124 if (ready.n_ready == 0 || !can_issue_more
2125 || state_dead_lock_p (curr_state)
2126 || !(*current_sched_info->schedule_more_p) ())
2127 break;
2129 /* Select and remove the insn from the ready list. */
2130 if (sort_p)
2131 insn = choose_ready (&ready);
2132 else
2133 insn = ready_remove_first (&ready);
2135 if (targetm.sched.dfa_new_cycle
2136 && (*targetm.sched.dfa_new_cycle) (sched_dump, sched_verbose,
2137 insn, last_clock_var,
2138 clock_var, &sort_p))
2140 ready_add (&ready, insn);
2141 break;
2144 sort_p = TRUE;
2145 memcpy (temp_state, curr_state, dfa_state_size);
2146 if (recog_memoized (insn) < 0)
2148 if (!first_cycle_insn_p
2149 && (GET_CODE (PATTERN (insn)) == ASM_INPUT
2150 || asm_noperands (PATTERN (insn)) >= 0))
2151 /* This is asm insn which is tryed to be issued on the
2152 cycle not first. Issue it on the next cycle. */
2153 cost = 1;
2154 else
2155 /* A USE insn, or something else we don't need to
2156 understand. We can't pass these directly to
2157 state_transition because it will trigger a
2158 fatal error for unrecognizable insns. */
2159 cost = 0;
2161 else
2163 cost = state_transition (temp_state, insn);
2165 if (targetm.sched.first_cycle_multipass_dfa_lookahead
2166 && targetm.sched.dfa_bubble)
2168 if (cost == 0)
2170 int j;
2171 rtx bubble;
2173 for (j = 0;
2174 (bubble = (*targetm.sched.dfa_bubble) (j))
2175 != NULL_RTX;
2176 j++)
2178 memcpy (temp_state, curr_state, dfa_state_size);
2180 if (state_transition (temp_state, bubble) < 0
2181 && state_transition (temp_state, insn) < 0)
2182 break;
2185 if (bubble != NULL_RTX)
2187 if (insert_schedule_bubbles_p)
2189 rtx copy;
2191 copy = copy_rtx (PATTERN (bubble));
2192 emit_insn_after (copy, last_scheduled_insn);
2193 last_scheduled_insn
2194 = NEXT_INSN (last_scheduled_insn);
2195 INSN_CODE (last_scheduled_insn)
2196 = INSN_CODE (bubble);
2198 /* Annotate the same for the first insns
2199 scheduling by using mode. */
2200 PUT_MODE (last_scheduled_insn,
2201 (clock_var > last_clock_var
2202 ? clock_var - last_clock_var
2203 : VOIDmode));
2204 last_clock_var = clock_var;
2206 if (sched_verbose >= 2)
2208 fprintf (sched_dump,
2209 ";;\t\t--> scheduling bubble insn <<<%d>>>:reservation ",
2210 INSN_UID (last_scheduled_insn));
2212 if (recog_memoized (last_scheduled_insn)
2213 < 0)
2214 fprintf (sched_dump, "nothing");
2215 else
2216 print_reservation
2217 (sched_dump, last_scheduled_insn);
2219 fprintf (sched_dump, "\n");
2222 cost = -1;
2227 if (cost < 0)
2228 cost = 0;
2229 else if (cost == 0)
2230 cost = 1;
2235 if (cost >= 1)
2237 queue_insn (insn, cost);
2238 continue;
2241 if (! (*current_sched_info->can_schedule_ready_p) (insn))
2242 goto next;
2244 last_scheduled_insn = move_insn (insn, last_scheduled_insn);
2246 if (targetm.sched.use_dfa_pipeline_interface
2247 && (*targetm.sched.use_dfa_pipeline_interface) ())
2249 if (memcmp (curr_state, temp_state, dfa_state_size) != 0)
2250 cycle_issued_insns++;
2251 memcpy (curr_state, temp_state, dfa_state_size);
2254 if (targetm.sched.variable_issue)
2255 can_issue_more =
2256 (*targetm.sched.variable_issue) (sched_dump, sched_verbose,
2257 insn, can_issue_more);
2258 /* A naked CLOBBER or USE generates no instruction, so do
2259 not count them against the issue rate. */
2260 else if (GET_CODE (PATTERN (insn)) != USE
2261 && GET_CODE (PATTERN (insn)) != CLOBBER)
2262 can_issue_more--;
2264 advance = schedule_insn (insn, &ready, clock_var);
2265 if (advance != 0)
2266 break;
2268 next:
2269 first_cycle_insn_p = 0;
2271 /* Sort the ready list based on priority. This must be
2272 redone here, as schedule_insn may have readied additional
2273 insns that will not be sorted correctly. */
2274 if (ready.n_ready > 0)
2275 ready_sort (&ready);
2277 if (targetm.sched.reorder2
2278 && (ready.n_ready == 0
2279 || !SCHED_GROUP_P (ready_element (&ready, 0))))
2281 can_issue_more =
2282 (*targetm.sched.reorder2) (sched_dump, sched_verbose,
2283 ready.n_ready
2284 ? ready_lastpos (&ready) : NULL,
2285 &ready.n_ready, clock_var);
2289 if ((!targetm.sched.use_dfa_pipeline_interface
2290 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2291 && sched_verbose)
2292 /* Debug info. */
2293 visualize_scheduled_insns (clock_var);
2296 if (targetm.sched.md_finish)
2297 (*targetm.sched.md_finish) (sched_dump, sched_verbose);
2299 /* Debug info. */
2300 if (sched_verbose)
2302 fprintf (sched_dump, ";;\tReady list (final): ");
2303 debug_ready_list (&ready);
2304 if (!targetm.sched.use_dfa_pipeline_interface
2305 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2306 print_block_visualization ("");
2309 /* Sanity check -- queue must be empty now. Meaningless if region has
2310 multiple bbs. */
2311 if (current_sched_info->queue_must_finish_empty && q_size != 0)
2312 abort ();
2314 /* Update head/tail boundaries. */
2315 head = NEXT_INSN (prev_head);
2316 tail = last_scheduled_insn;
2318 if (!reload_completed)
2320 rtx insn, link, next;
2322 /* INSN_TICK (minimum clock tick at which the insn becomes
2323 ready) may be not correct for the insn in the subsequent
2324 blocks of the region. We should use a correct value of
2325 `clock_var' or modify INSN_TICK. It is better to keep
2326 clock_var value equal to 0 at the start of a basic block.
2327 Therefore we modify INSN_TICK here. */
2328 for (insn = head; insn != tail; insn = NEXT_INSN (insn))
2329 if (INSN_P (insn))
2331 for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
2333 next = XEXP (link, 0);
2334 INSN_TICK (next) -= clock_var;
2339 /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
2340 previously found among the insns. Insert them at the beginning
2341 of the insns. */
2342 if (note_list != 0)
2344 rtx note_head = note_list;
2346 while (PREV_INSN (note_head))
2348 note_head = PREV_INSN (note_head);
2351 PREV_INSN (note_head) = PREV_INSN (head);
2352 NEXT_INSN (PREV_INSN (head)) = note_head;
2353 PREV_INSN (head) = note_list;
2354 NEXT_INSN (note_list) = head;
2355 head = note_head;
2358 /* Debugging. */
2359 if (sched_verbose)
2361 fprintf (sched_dump, ";; total time = %d\n;; new head = %d\n",
2362 clock_var, INSN_UID (head));
2363 fprintf (sched_dump, ";; new tail = %d\n\n",
2364 INSN_UID (tail));
2365 visualize_free ();
2368 current_sched_info->head = head;
2369 current_sched_info->tail = tail;
2371 free (ready.vec);
2373 if (targetm.sched.use_dfa_pipeline_interface
2374 && (*targetm.sched.use_dfa_pipeline_interface) ())
2376 free (ready_try);
2377 for (i = 0; i <= rgn_n_insns; i++)
2378 free (choice_stack [i].state);
2379 free (choice_stack);
2383 /* Set_priorities: compute priority of each insn in the block. */
2386 set_priorities (head, tail)
2387 rtx head, tail;
2389 rtx insn;
2390 int n_insn;
2392 rtx prev_head;
2394 prev_head = PREV_INSN (head);
2396 if (head == tail && (! INSN_P (head)))
2397 return 0;
2399 n_insn = 0;
2400 for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
2402 if (GET_CODE (insn) == NOTE)
2403 continue;
2405 n_insn++;
2406 (void) priority (insn);
2409 return n_insn;
2412 /* Initialize some global state for the scheduler. DUMP_FILE is to be used
2413 for debugging output. */
2415 void
2416 sched_init (dump_file)
2417 FILE *dump_file;
2419 int luid;
2420 basic_block b;
2421 rtx insn;
2422 int i;
2424 /* Disable speculative loads in their presence if cc0 defined. */
2425 #ifdef HAVE_cc0
2426 flag_schedule_speculative_load = 0;
2427 #endif
2429 /* Set dump and sched_verbose for the desired debugging output. If no
2430 dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
2431 For -fsched-verbose=N, N>=10, print everything to stderr. */
2432 sched_verbose = sched_verbose_param;
2433 if (sched_verbose_param == 0 && dump_file)
2434 sched_verbose = 1;
2435 sched_dump = ((sched_verbose_param >= 10 || !dump_file)
2436 ? stderr : dump_file);
2438 /* Initialize issue_rate. */
2439 if (targetm.sched.issue_rate)
2440 issue_rate = (*targetm.sched.issue_rate) ();
2441 else
2442 issue_rate = 1;
2444 /* We use LUID 0 for the fake insn (UID 0) which holds dependencies for
2445 pseudos which do not cross calls. */
2446 old_max_uid = get_max_uid () + 1;
2448 h_i_d = (struct haifa_insn_data *) xcalloc (old_max_uid, sizeof (*h_i_d));
2450 for (i = 0; i < old_max_uid; i++)
2451 h_i_d [i].cost = -1;
2453 if (targetm.sched.use_dfa_pipeline_interface
2454 && (*targetm.sched.use_dfa_pipeline_interface) ())
2456 if (targetm.sched.init_dfa_pre_cycle_insn)
2457 (*targetm.sched.init_dfa_pre_cycle_insn) ();
2459 if (targetm.sched.init_dfa_post_cycle_insn)
2460 (*targetm.sched.init_dfa_post_cycle_insn) ();
2462 if (targetm.sched.first_cycle_multipass_dfa_lookahead
2463 && targetm.sched.init_dfa_bubbles)
2464 (*targetm.sched.init_dfa_bubbles) ();
2466 dfa_start ();
2467 dfa_state_size = state_size ();
2468 curr_state = xmalloc (dfa_state_size);
2471 h_i_d[0].luid = 0;
2472 luid = 1;
2473 FOR_EACH_BB (b)
2474 for (insn = b->head;; insn = NEXT_INSN (insn))
2476 INSN_LUID (insn) = luid;
2478 /* Increment the next luid, unless this is a note. We don't
2479 really need separate IDs for notes and we don't want to
2480 schedule differently depending on whether or not there are
2481 line-number notes, i.e., depending on whether or not we're
2482 generating debugging information. */
2483 if (GET_CODE (insn) != NOTE)
2484 ++luid;
2486 if (insn == b->end)
2487 break;
2490 init_dependency_caches (luid);
2492 init_alias_analysis ();
2494 if (write_symbols != NO_DEBUG)
2496 rtx line;
2498 line_note_head = (rtx *) xcalloc (last_basic_block, sizeof (rtx));
2500 /* Save-line-note-head:
2501 Determine the line-number at the start of each basic block.
2502 This must be computed and saved now, because after a basic block's
2503 predecessor has been scheduled, it is impossible to accurately
2504 determine the correct line number for the first insn of the block. */
2506 FOR_EACH_BB (b)
2508 for (line = b->head; line; line = PREV_INSN (line))
2509 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
2511 line_note_head[b->index] = line;
2512 break;
2514 /* Do a forward search as well, since we won't get to see the first
2515 notes in a basic block. */
2516 for (line = b->head; line; line = NEXT_INSN (line))
2518 if (INSN_P (line))
2519 break;
2520 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
2521 line_note_head[b->index] = line;
2526 if ((!targetm.sched.use_dfa_pipeline_interface
2527 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2528 && sched_verbose)
2529 /* Find units used in this function, for visualization. */
2530 init_target_units ();
2532 /* ??? Add a NOTE after the last insn of the last basic block. It is not
2533 known why this is done. */
2535 insn = EXIT_BLOCK_PTR->prev_bb->end;
2536 if (NEXT_INSN (insn) == 0
2537 || (GET_CODE (insn) != NOTE
2538 && GET_CODE (insn) != CODE_LABEL
2539 /* Don't emit a NOTE if it would end up before a BARRIER. */
2540 && GET_CODE (NEXT_INSN (insn)) != BARRIER))
2542 emit_note_after (NOTE_INSN_DELETED, EXIT_BLOCK_PTR->prev_bb->end);
2543 /* Make insn to appear outside BB. */
2544 EXIT_BLOCK_PTR->prev_bb->end = PREV_INSN (EXIT_BLOCK_PTR->prev_bb->end);
2547 /* Compute INSN_REG_WEIGHT for all blocks. We must do this before
2548 removing death notes. */
2549 FOR_EACH_BB_REVERSE (b)
2550 find_insn_reg_weight (b->index);
2553 /* Free global data used during insn scheduling. */
2555 void
2556 sched_finish ()
2558 free (h_i_d);
2560 if (targetm.sched.use_dfa_pipeline_interface
2561 && (*targetm.sched.use_dfa_pipeline_interface) ())
2563 free (curr_state);
2564 dfa_finish ();
2566 free_dependency_caches ();
2567 end_alias_analysis ();
2568 if (write_symbols != NO_DEBUG)
2569 free (line_note_head);
2571 #endif /* INSN_SCHEDULING */