* config/h8300/clzsi2.c: Remove.
[official-gcc.git] / gcc / haifa-sched.c
bloba06c2f9307179f9fd8c17555abb50affa470dfee
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_BASIC_BLOCK
1250 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_BEG
1251 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_END)
1253 /* Insert the note at the end of the notes list. */
1254 PREV_INSN (insn) = note_list;
1255 if (note_list)
1256 NEXT_INSN (note_list) = insn;
1257 note_list = insn;
1260 insn = next;
1262 return insn;
1265 /* Delete line notes beginning with INSN. Record line-number notes so
1266 they can be reused. Returns the insn following the notes. */
1268 static rtx
1269 unlink_line_notes (insn, tail)
1270 rtx insn, tail;
1272 rtx prev = PREV_INSN (insn);
1274 while (insn != tail && GET_CODE (insn) == NOTE)
1276 rtx next = NEXT_INSN (insn);
1278 if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
1280 /* Delete the note from its current position. */
1281 if (prev)
1282 NEXT_INSN (prev) = next;
1283 if (next)
1284 PREV_INSN (next) = prev;
1286 /* Record line-number notes so they can be reused. */
1287 LINE_NOTE (insn) = insn;
1289 else
1290 prev = insn;
1292 insn = next;
1294 return insn;
1297 /* Return the head and tail pointers of BB. */
1299 void
1300 get_block_head_tail (b, headp, tailp)
1301 int b;
1302 rtx *headp;
1303 rtx *tailp;
1305 /* HEAD and TAIL delimit the basic block being scheduled. */
1306 rtx head = BLOCK_HEAD (b);
1307 rtx tail = BLOCK_END (b);
1309 /* Don't include any notes or labels at the beginning of the
1310 basic block, or notes at the ends of basic blocks. */
1311 while (head != tail)
1313 if (GET_CODE (head) == NOTE)
1314 head = NEXT_INSN (head);
1315 else if (GET_CODE (tail) == NOTE)
1316 tail = PREV_INSN (tail);
1317 else if (GET_CODE (head) == CODE_LABEL)
1318 head = NEXT_INSN (head);
1319 else
1320 break;
1323 *headp = head;
1324 *tailp = tail;
1327 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ]. */
1330 no_real_insns_p (head, tail)
1331 rtx head, tail;
1333 while (head != NEXT_INSN (tail))
1335 if (GET_CODE (head) != NOTE && GET_CODE (head) != CODE_LABEL)
1336 return 0;
1337 head = NEXT_INSN (head);
1339 return 1;
1342 /* Delete line notes from one block. Save them so they can be later restored
1343 (in restore_line_notes). HEAD and TAIL are the boundaries of the
1344 block in which notes should be processed. */
1346 void
1347 rm_line_notes (head, tail)
1348 rtx head, tail;
1350 rtx next_tail;
1351 rtx insn;
1353 next_tail = NEXT_INSN (tail);
1354 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1356 rtx prev;
1358 /* Farm out notes, and maybe save them in NOTE_LIST.
1359 This is needed to keep the debugger from
1360 getting completely deranged. */
1361 if (GET_CODE (insn) == NOTE)
1363 prev = insn;
1364 insn = unlink_line_notes (insn, next_tail);
1366 if (prev == tail)
1367 abort ();
1368 if (prev == head)
1369 abort ();
1370 if (insn == next_tail)
1371 abort ();
1376 /* Save line number notes for each insn in block B. HEAD and TAIL are
1377 the boundaries of the block in which notes should be processed. */
1379 void
1380 save_line_notes (b, head, tail)
1381 int b;
1382 rtx head, tail;
1384 rtx next_tail;
1386 /* We must use the true line number for the first insn in the block
1387 that was computed and saved at the start of this pass. We can't
1388 use the current line number, because scheduling of the previous
1389 block may have changed the current line number. */
1391 rtx line = line_note_head[b];
1392 rtx insn;
1394 next_tail = NEXT_INSN (tail);
1396 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1397 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1398 line = insn;
1399 else
1400 LINE_NOTE (insn) = line;
1403 /* After a block was scheduled, insert line notes into the insns list.
1404 HEAD and TAIL are the boundaries of the block in which notes should
1405 be processed. */
1407 void
1408 restore_line_notes (head, tail)
1409 rtx head, tail;
1411 rtx line, note, prev, new;
1412 int added_notes = 0;
1413 rtx next_tail, insn;
1415 head = head;
1416 next_tail = NEXT_INSN (tail);
1418 /* Determine the current line-number. We want to know the current
1419 line number of the first insn of the block here, in case it is
1420 different from the true line number that was saved earlier. If
1421 different, then we need a line number note before the first insn
1422 of this block. If it happens to be the same, then we don't want to
1423 emit another line number note here. */
1424 for (line = head; line; line = PREV_INSN (line))
1425 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
1426 break;
1428 /* Walk the insns keeping track of the current line-number and inserting
1429 the line-number notes as needed. */
1430 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1431 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1432 line = insn;
1433 /* This used to emit line number notes before every non-deleted note.
1434 However, this confuses a debugger, because line notes not separated
1435 by real instructions all end up at the same address. I can find no
1436 use for line number notes before other notes, so none are emitted. */
1437 else if (GET_CODE (insn) != NOTE
1438 && INSN_UID (insn) < old_max_uid
1439 && (note = LINE_NOTE (insn)) != 0
1440 && note != line
1441 && (line == 0
1442 || NOTE_LINE_NUMBER (note) != NOTE_LINE_NUMBER (line)
1443 || NOTE_SOURCE_FILE (note) != NOTE_SOURCE_FILE (line)))
1445 line = note;
1446 prev = PREV_INSN (insn);
1447 if (LINE_NOTE (note))
1449 /* Re-use the original line-number note. */
1450 LINE_NOTE (note) = 0;
1451 PREV_INSN (note) = prev;
1452 NEXT_INSN (prev) = note;
1453 PREV_INSN (insn) = note;
1454 NEXT_INSN (note) = insn;
1456 else
1458 added_notes++;
1459 new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
1460 NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
1461 RTX_INTEGRATED_P (new) = RTX_INTEGRATED_P (note);
1464 if (sched_verbose && added_notes)
1465 fprintf (sched_dump, ";; added %d line-number notes\n", added_notes);
1468 /* After scheduling the function, delete redundant line notes from the
1469 insns list. */
1471 void
1472 rm_redundant_line_notes ()
1474 rtx line = 0;
1475 rtx insn = get_insns ();
1476 int active_insn = 0;
1477 int notes = 0;
1479 /* Walk the insns deleting redundant line-number notes. Many of these
1480 are already present. The remainder tend to occur at basic
1481 block boundaries. */
1482 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1483 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1485 /* If there are no active insns following, INSN is redundant. */
1486 if (active_insn == 0)
1488 notes++;
1489 NOTE_SOURCE_FILE (insn) = 0;
1490 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1492 /* If the line number is unchanged, LINE is redundant. */
1493 else if (line
1494 && NOTE_LINE_NUMBER (line) == NOTE_LINE_NUMBER (insn)
1495 && NOTE_SOURCE_FILE (line) == NOTE_SOURCE_FILE (insn))
1497 notes++;
1498 NOTE_SOURCE_FILE (line) = 0;
1499 NOTE_LINE_NUMBER (line) = NOTE_INSN_DELETED;
1500 line = insn;
1502 else
1503 line = insn;
1504 active_insn = 0;
1506 else if (!((GET_CODE (insn) == NOTE
1507 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
1508 || (GET_CODE (insn) == INSN
1509 && (GET_CODE (PATTERN (insn)) == USE
1510 || GET_CODE (PATTERN (insn)) == CLOBBER))))
1511 active_insn++;
1513 if (sched_verbose && notes)
1514 fprintf (sched_dump, ";; deleted %d line-number notes\n", notes);
1517 /* Delete notes between HEAD and TAIL and put them in the chain
1518 of notes ended by NOTE_LIST. */
1520 void
1521 rm_other_notes (head, tail)
1522 rtx head;
1523 rtx tail;
1525 rtx next_tail;
1526 rtx insn;
1528 note_list = 0;
1529 if (head == tail && (! INSN_P (head)))
1530 return;
1532 next_tail = NEXT_INSN (tail);
1533 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1535 rtx prev;
1537 /* Farm out notes, and maybe save them in NOTE_LIST.
1538 This is needed to keep the debugger from
1539 getting completely deranged. */
1540 if (GET_CODE (insn) == NOTE)
1542 prev = insn;
1544 insn = unlink_other_notes (insn, next_tail);
1546 if (prev == tail)
1547 abort ();
1548 if (prev == head)
1549 abort ();
1550 if (insn == next_tail)
1551 abort ();
1556 /* Functions for computation of registers live/usage info. */
1558 /* This function looks for a new register being defined.
1559 If the destination register is already used by the source,
1560 a new register is not needed. */
1562 static int
1563 find_set_reg_weight (x)
1564 rtx x;
1566 if (GET_CODE (x) == CLOBBER
1567 && register_operand (SET_DEST (x), VOIDmode))
1568 return 1;
1569 if (GET_CODE (x) == SET
1570 && register_operand (SET_DEST (x), VOIDmode))
1572 if (GET_CODE (SET_DEST (x)) == REG)
1574 if (!reg_mentioned_p (SET_DEST (x), SET_SRC (x)))
1575 return 1;
1576 else
1577 return 0;
1579 return 1;
1581 return 0;
1584 /* Calculate INSN_REG_WEIGHT for all insns of a block. */
1586 static void
1587 find_insn_reg_weight (b)
1588 int b;
1590 rtx insn, next_tail, head, tail;
1592 get_block_head_tail (b, &head, &tail);
1593 next_tail = NEXT_INSN (tail);
1595 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1597 int reg_weight = 0;
1598 rtx x;
1600 /* Handle register life information. */
1601 if (! INSN_P (insn))
1602 continue;
1604 /* Increment weight for each register born here. */
1605 x = PATTERN (insn);
1606 reg_weight += find_set_reg_weight (x);
1607 if (GET_CODE (x) == PARALLEL)
1609 int j;
1610 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
1612 x = XVECEXP (PATTERN (insn), 0, j);
1613 reg_weight += find_set_reg_weight (x);
1616 /* Decrement weight for each register that dies here. */
1617 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1619 if (REG_NOTE_KIND (x) == REG_DEAD
1620 || REG_NOTE_KIND (x) == REG_UNUSED)
1621 reg_weight--;
1624 INSN_REG_WEIGHT (insn) = reg_weight;
1628 /* Scheduling clock, modified in schedule_block() and queue_to_ready (). */
1629 static int clock_var;
1631 /* Move insns that became ready to fire from queue to ready list. */
1633 static void
1634 queue_to_ready (ready)
1635 struct ready_list *ready;
1637 rtx insn;
1638 rtx link;
1640 q_ptr = NEXT_Q (q_ptr);
1642 /* Add all pending insns that can be scheduled without stalls to the
1643 ready list. */
1644 for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
1646 insn = XEXP (link, 0);
1647 q_size -= 1;
1649 if (sched_verbose >= 2)
1650 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1651 (*current_sched_info->print_insn) (insn, 0));
1653 ready_add (ready, insn);
1654 if (sched_verbose >= 2)
1655 fprintf (sched_dump, "moving to ready without stalls\n");
1657 insn_queue[q_ptr] = 0;
1659 /* If there are no ready insns, stall until one is ready and add all
1660 of the pending insns at that point to the ready list. */
1661 if (ready->n_ready == 0)
1663 int stalls;
1665 for (stalls = 1; stalls <= MAX_INSN_QUEUE_INDEX; stalls++)
1667 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
1669 for (; link; link = XEXP (link, 1))
1671 insn = XEXP (link, 0);
1672 q_size -= 1;
1674 if (sched_verbose >= 2)
1675 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1676 (*current_sched_info->print_insn) (insn, 0));
1678 ready_add (ready, insn);
1679 if (sched_verbose >= 2)
1680 fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
1682 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = 0;
1684 advance_one_cycle ();
1686 break;
1689 advance_one_cycle ();
1692 if ((!targetm.sched.use_dfa_pipeline_interface
1693 || !(*targetm.sched.use_dfa_pipeline_interface) ())
1694 && sched_verbose && stalls)
1695 visualize_stall_cycles (stalls);
1697 q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
1698 clock_var += stalls;
1702 /* Print the ready list for debugging purposes. Callable from debugger. */
1704 static void
1705 debug_ready_list (ready)
1706 struct ready_list *ready;
1708 rtx *p;
1709 int i;
1711 if (ready->n_ready == 0)
1713 fprintf (sched_dump, "\n");
1714 return;
1717 p = ready_lastpos (ready);
1718 for (i = 0; i < ready->n_ready; i++)
1719 fprintf (sched_dump, " %s", (*current_sched_info->print_insn) (p[i], 0));
1720 fprintf (sched_dump, "\n");
1723 /* move_insn1: Remove INSN from insn chain, and link it after LAST insn. */
1725 static rtx
1726 move_insn1 (insn, last)
1727 rtx insn, last;
1729 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
1730 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
1732 NEXT_INSN (insn) = NEXT_INSN (last);
1733 PREV_INSN (NEXT_INSN (last)) = insn;
1735 NEXT_INSN (last) = insn;
1736 PREV_INSN (insn) = last;
1738 return insn;
1741 /* Search INSN for REG_SAVE_NOTE note pairs for
1742 NOTE_INSN_{LOOP,EHREGION}_{BEG,END}; and convert them back into
1743 NOTEs. The REG_SAVE_NOTE note following first one is contains the
1744 saved value for NOTE_BLOCK_NUMBER which is useful for
1745 NOTE_INSN_EH_REGION_{BEG,END} NOTEs. LAST is the last instruction
1746 output by the instruction scheduler. Return the new value of LAST. */
1748 static rtx
1749 reemit_notes (insn, last)
1750 rtx insn;
1751 rtx last;
1753 rtx note, retval;
1755 retval = last;
1756 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1758 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
1760 enum insn_note note_type = INTVAL (XEXP (note, 0));
1762 last = emit_note_before (note_type, last);
1763 remove_note (insn, note);
1764 note = XEXP (note, 1);
1765 if (note_type == NOTE_INSN_EH_REGION_BEG
1766 || note_type == NOTE_INSN_EH_REGION_END)
1767 NOTE_EH_HANDLER (last) = INTVAL (XEXP (note, 0));
1768 remove_note (insn, note);
1771 return retval;
1774 /* Move INSN. Reemit notes if needed.
1776 Return the last insn emitted by the scheduler, which is the
1777 return value from the first call to reemit_notes. */
1779 static rtx
1780 move_insn (insn, last)
1781 rtx insn, last;
1783 rtx retval = NULL;
1785 move_insn1 (insn, last);
1787 /* If this is the first call to reemit_notes, then record
1788 its return value. */
1789 if (retval == NULL_RTX)
1790 retval = reemit_notes (insn, insn);
1791 else
1792 reemit_notes (insn, insn);
1794 SCHED_GROUP_P (insn) = 0;
1796 return retval;
1799 /* The following structure describe an entry of the stack of choices. */
1800 struct choice_entry
1802 /* Ordinal number of the issued insn in the ready queue. */
1803 int index;
1804 /* The number of the rest insns whose issues we should try. */
1805 int rest;
1806 /* The number of issued essential insns. */
1807 int n;
1808 /* State after issuing the insn. */
1809 state_t state;
1812 /* The following array is used to implement a stack of choices used in
1813 function max_issue. */
1814 static struct choice_entry *choice_stack;
1816 /* The following variable value is number of essential insns issued on
1817 the current cycle. An insn is essential one if it changes the
1818 processors state. */
1819 static int cycle_issued_insns;
1821 /* The following function returns maximal (or close to maximal) number
1822 of insns which can be issued on the same cycle and one of which
1823 insns is insns with the best rank (the first insn in READY). To
1824 make this function tries different samples of ready insns. READY
1825 is current queue `ready'. Global array READY_TRY reflects what
1826 insns are already issued in this try. INDEX will contain index
1827 of the best insn in READY. The following function is used only for
1828 first cycle multipass scheduling. */
1829 static int
1830 max_issue (ready, index)
1831 struct ready_list *ready;
1832 int *index;
1834 int n, i, all, n_ready, lookahead, best, delay;
1835 struct choice_entry *top;
1836 rtx insn;
1838 lookahead = (*targetm.sched.first_cycle_multipass_dfa_lookahead) ();
1839 best = 0;
1840 memcpy (choice_stack->state, curr_state, dfa_state_size);
1841 top = choice_stack;
1842 top->rest = lookahead;
1843 top->n = 0;
1844 n_ready = ready->n_ready;
1845 for (all = i = 0; i < n_ready; i++)
1846 if (!ready_try [i])
1847 all++;
1848 i = 0;
1849 for (;;)
1851 if (top->rest == 0 || i >= n_ready)
1853 if (top == choice_stack)
1854 break;
1855 if (best < top - choice_stack && ready_try [0])
1857 best = top - choice_stack;
1858 *index = choice_stack [1].index;
1859 if (top->n == issue_rate - cycle_issued_insns || best == all)
1860 break;
1862 i = top->index;
1863 ready_try [i] = 0;
1864 top--;
1865 memcpy (curr_state, top->state, dfa_state_size);
1867 else if (!ready_try [i])
1869 insn = ready_element (ready, i);
1870 delay = state_transition (curr_state, insn);
1871 if (delay < 0)
1873 if (state_dead_lock_p (curr_state))
1874 top->rest = 0;
1875 else
1876 top->rest--;
1877 n = top->n;
1878 if (memcmp (top->state, curr_state, dfa_state_size) != 0)
1879 n++;
1880 top++;
1881 top->rest = lookahead;
1882 top->index = i;
1883 top->n = n;
1884 memcpy (top->state, curr_state, dfa_state_size);
1885 ready_try [i] = 1;
1886 i = -1;
1889 i++;
1891 while (top != choice_stack)
1893 ready_try [top->index] = 0;
1894 top--;
1896 memcpy (curr_state, choice_stack->state, dfa_state_size);
1897 return best;
1900 /* The following function chooses insn from READY and modifies
1901 *N_READY and READY. The following function is used only for first
1902 cycle multipass scheduling. */
1904 static rtx
1905 choose_ready (ready)
1906 struct ready_list *ready;
1908 if (!targetm.sched.first_cycle_multipass_dfa_lookahead
1909 || (*targetm.sched.first_cycle_multipass_dfa_lookahead) () <= 0
1910 || SCHED_GROUP_P (ready_element (ready, 0)))
1911 return ready_remove_first (ready);
1912 else
1914 /* Try to choose the better insn. */
1915 int index, i;
1916 rtx insn;
1918 insn = ready_element (ready, 0);
1919 if (INSN_CODE (insn) < 0)
1920 return ready_remove_first (ready);
1921 for (i = 1; i < ready->n_ready; i++)
1923 insn = ready_element (ready, i);
1924 ready_try [i]
1925 = (INSN_CODE (insn) < 0
1926 || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard
1927 && !(*targetm.sched.first_cycle_multipass_dfa_lookahead_guard) (insn)));
1929 if (max_issue (ready, &index) == 0)
1930 return ready_remove_first (ready);
1931 else
1932 return ready_remove (ready, index);
1936 /* Called from backends from targetm.sched.reorder to emit stuff into
1937 the instruction stream. */
1940 sched_emit_insn (pat)
1941 rtx pat;
1943 rtx insn = emit_insn_after (pat, last_scheduled_insn);
1944 last_scheduled_insn = insn;
1945 return insn;
1948 /* Use forward list scheduling to rearrange insns of block B in region RGN,
1949 possibly bringing insns from subsequent blocks in the same region. */
1951 void
1952 schedule_block (b, rgn_n_insns)
1953 int b;
1954 int rgn_n_insns;
1956 struct ready_list ready;
1957 int i, first_cycle_insn_p;
1958 int can_issue_more;
1959 state_t temp_state = NULL; /* It is used for multipass scheduling. */
1960 int sort_p, advance, start_clock_var;
1962 /* Head/tail info for this block. */
1963 rtx prev_head = current_sched_info->prev_head;
1964 rtx next_tail = current_sched_info->next_tail;
1965 rtx head = NEXT_INSN (prev_head);
1966 rtx tail = PREV_INSN (next_tail);
1968 /* We used to have code to avoid getting parameters moved from hard
1969 argument registers into pseudos.
1971 However, it was removed when it proved to be of marginal benefit
1972 and caused problems because schedule_block and compute_forward_dependences
1973 had different notions of what the "head" insn was. */
1975 if (head == tail && (! INSN_P (head)))
1976 abort ();
1978 /* Debug info. */
1979 if (sched_verbose)
1981 fprintf (sched_dump, ";; ======================================================\n");
1982 fprintf (sched_dump,
1983 ";; -- basic block %d from %d to %d -- %s reload\n",
1984 b, INSN_UID (head), INSN_UID (tail),
1985 (reload_completed ? "after" : "before"));
1986 fprintf (sched_dump, ";; ======================================================\n");
1987 fprintf (sched_dump, "\n");
1989 visualize_alloc ();
1990 init_block_visualization ();
1993 if (targetm.sched.use_dfa_pipeline_interface
1994 && (*targetm.sched.use_dfa_pipeline_interface) ())
1995 state_reset (curr_state);
1996 else
1997 clear_units ();
1999 /* Allocate the ready list. */
2000 ready.veclen = rgn_n_insns + 1 + issue_rate;
2001 ready.first = ready.veclen - 1;
2002 ready.vec = (rtx *) xmalloc (ready.veclen * sizeof (rtx));
2003 ready.n_ready = 0;
2005 if (targetm.sched.use_dfa_pipeline_interface
2006 && (*targetm.sched.use_dfa_pipeline_interface) ())
2008 /* It is used for first cycle multipass scheduling. */
2009 temp_state = alloca (dfa_state_size);
2010 ready_try = (char *) xmalloc ((rgn_n_insns + 1) * sizeof (char));
2011 memset (ready_try, 0, (rgn_n_insns + 1) * sizeof (char));
2012 choice_stack
2013 = (struct choice_entry *) xmalloc ((rgn_n_insns + 1)
2014 * sizeof (struct choice_entry));
2015 for (i = 0; i <= rgn_n_insns; i++)
2016 choice_stack[i].state = (state_t) xmalloc (dfa_state_size);
2019 (*current_sched_info->init_ready_list) (&ready);
2021 if (targetm.sched.md_init)
2022 (*targetm.sched.md_init) (sched_dump, sched_verbose, ready.veclen);
2024 /* We start inserting insns after PREV_HEAD. */
2025 last_scheduled_insn = prev_head;
2027 /* Initialize INSN_QUEUE. Q_SIZE is the total number of insns in the
2028 queue. */
2029 q_ptr = 0;
2030 q_size = 0;
2032 if (!targetm.sched.use_dfa_pipeline_interface
2033 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2034 max_insn_queue_index_macro_value = INSN_QUEUE_SIZE - 1;
2035 else
2036 max_insn_queue_index_macro_value = max_insn_queue_index;
2038 insn_queue = (rtx *) alloca ((MAX_INSN_QUEUE_INDEX + 1) * sizeof (rtx));
2039 memset ((char *) insn_queue, 0, (MAX_INSN_QUEUE_INDEX + 1) * sizeof (rtx));
2040 last_clock_var = -1;
2042 /* Start just before the beginning of time. */
2043 clock_var = -1;
2044 advance = 0;
2046 sort_p = TRUE;
2047 /* Loop until all the insns in BB are scheduled. */
2048 while ((*current_sched_info->schedule_more_p) ())
2052 start_clock_var = clock_var;
2054 clock_var++;
2056 advance_one_cycle ();
2058 /* Add to the ready list all pending insns that can be issued now.
2059 If there are no ready insns, increment clock until one
2060 is ready and add all pending insns at that point to the ready
2061 list. */
2062 queue_to_ready (&ready);
2064 if (ready.n_ready == 0)
2065 abort ();
2067 if (sched_verbose >= 2)
2069 fprintf (sched_dump, ";;\t\tReady list after queue_to_ready: ");
2070 debug_ready_list (&ready);
2072 advance -= clock_var - start_clock_var;
2074 while (advance > 0);
2076 if (sort_p)
2078 /* Sort the ready list based on priority. */
2079 ready_sort (&ready);
2081 if (sched_verbose >= 2)
2083 fprintf (sched_dump, ";;\t\tReady list after ready_sort: ");
2084 debug_ready_list (&ready);
2088 /* Allow the target to reorder the list, typically for
2089 better instruction bundling. */
2090 if (targetm.sched.reorder
2091 && (ready.n_ready == 0
2092 || !SCHED_GROUP_P (ready_element (&ready, 0))))
2093 can_issue_more =
2094 (*targetm.sched.reorder) (sched_dump, sched_verbose,
2095 ready_lastpos (&ready),
2096 &ready.n_ready, clock_var);
2097 else
2098 can_issue_more = issue_rate;
2100 first_cycle_insn_p = 1;
2101 cycle_issued_insns = 0;
2102 for (;;)
2104 rtx insn;
2105 int cost;
2107 if (sched_verbose >= 2)
2109 fprintf (sched_dump, ";;\tReady list (t =%3d): ",
2110 clock_var);
2111 debug_ready_list (&ready);
2114 if (!targetm.sched.use_dfa_pipeline_interface
2115 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2117 if (ready.n_ready == 0 || !can_issue_more
2118 || !(*current_sched_info->schedule_more_p) ())
2119 break;
2120 insn = choose_ready (&ready);
2121 cost = actual_hazard (insn_unit (insn), insn, clock_var, 0);
2123 else
2125 if (ready.n_ready == 0 || !can_issue_more
2126 || state_dead_lock_p (curr_state)
2127 || !(*current_sched_info->schedule_more_p) ())
2128 break;
2130 /* Select and remove the insn from the ready list. */
2131 if (sort_p)
2132 insn = choose_ready (&ready);
2133 else
2134 insn = ready_remove_first (&ready);
2136 if (targetm.sched.dfa_new_cycle
2137 && (*targetm.sched.dfa_new_cycle) (sched_dump, sched_verbose,
2138 insn, last_clock_var,
2139 clock_var, &sort_p))
2141 ready_add (&ready, insn);
2142 break;
2145 sort_p = TRUE;
2146 memcpy (temp_state, curr_state, dfa_state_size);
2147 if (recog_memoized (insn) < 0)
2149 if (!first_cycle_insn_p
2150 && (GET_CODE (PATTERN (insn)) == ASM_INPUT
2151 || asm_noperands (PATTERN (insn)) >= 0))
2152 /* This is asm insn which is tryed to be issued on the
2153 cycle not first. Issue it on the next cycle. */
2154 cost = 1;
2155 else
2156 /* A USE insn, or something else we don't need to
2157 understand. We can't pass these directly to
2158 state_transition because it will trigger a
2159 fatal error for unrecognizable insns. */
2160 cost = 0;
2162 else
2164 cost = state_transition (temp_state, insn);
2166 if (targetm.sched.first_cycle_multipass_dfa_lookahead
2167 && targetm.sched.dfa_bubble)
2169 if (cost == 0)
2171 int j;
2172 rtx bubble;
2174 for (j = 0;
2175 (bubble = (*targetm.sched.dfa_bubble) (j))
2176 != NULL_RTX;
2177 j++)
2179 memcpy (temp_state, curr_state, dfa_state_size);
2181 if (state_transition (temp_state, bubble) < 0
2182 && state_transition (temp_state, insn) < 0)
2183 break;
2186 if (bubble != NULL_RTX)
2188 if (insert_schedule_bubbles_p)
2190 rtx copy;
2192 copy = copy_rtx (PATTERN (bubble));
2193 emit_insn_after (copy, last_scheduled_insn);
2194 last_scheduled_insn
2195 = NEXT_INSN (last_scheduled_insn);
2196 INSN_CODE (last_scheduled_insn)
2197 = INSN_CODE (bubble);
2199 /* Annotate the same for the first insns
2200 scheduling by using mode. */
2201 PUT_MODE (last_scheduled_insn,
2202 (clock_var > last_clock_var
2203 ? clock_var - last_clock_var
2204 : VOIDmode));
2205 last_clock_var = clock_var;
2207 if (sched_verbose >= 2)
2209 fprintf (sched_dump,
2210 ";;\t\t--> scheduling bubble insn <<<%d>>>:reservation ",
2211 INSN_UID (last_scheduled_insn));
2213 if (recog_memoized (last_scheduled_insn)
2214 < 0)
2215 fprintf (sched_dump, "nothing");
2216 else
2217 print_reservation
2218 (sched_dump, last_scheduled_insn);
2220 fprintf (sched_dump, "\n");
2223 cost = -1;
2228 if (cost < 0)
2229 cost = 0;
2230 else if (cost == 0)
2231 cost = 1;
2236 if (cost >= 1)
2238 queue_insn (insn, cost);
2239 continue;
2242 if (! (*current_sched_info->can_schedule_ready_p) (insn))
2243 goto next;
2245 last_scheduled_insn = move_insn (insn, last_scheduled_insn);
2247 if (targetm.sched.use_dfa_pipeline_interface
2248 && (*targetm.sched.use_dfa_pipeline_interface) ())
2250 if (memcmp (curr_state, temp_state, dfa_state_size) != 0)
2251 cycle_issued_insns++;
2252 memcpy (curr_state, temp_state, dfa_state_size);
2255 if (targetm.sched.variable_issue)
2256 can_issue_more =
2257 (*targetm.sched.variable_issue) (sched_dump, sched_verbose,
2258 insn, can_issue_more);
2259 /* A naked CLOBBER or USE generates no instruction, so do
2260 not count them against the issue rate. */
2261 else if (GET_CODE (PATTERN (insn)) != USE
2262 && GET_CODE (PATTERN (insn)) != CLOBBER)
2263 can_issue_more--;
2265 advance = schedule_insn (insn, &ready, clock_var);
2266 if (advance != 0)
2267 break;
2269 next:
2270 first_cycle_insn_p = 0;
2272 /* Sort the ready list based on priority. This must be
2273 redone here, as schedule_insn may have readied additional
2274 insns that will not be sorted correctly. */
2275 if (ready.n_ready > 0)
2276 ready_sort (&ready);
2278 if (targetm.sched.reorder2
2279 && (ready.n_ready == 0
2280 || !SCHED_GROUP_P (ready_element (&ready, 0))))
2282 can_issue_more =
2283 (*targetm.sched.reorder2) (sched_dump, sched_verbose,
2284 ready.n_ready
2285 ? ready_lastpos (&ready) : NULL,
2286 &ready.n_ready, clock_var);
2290 if ((!targetm.sched.use_dfa_pipeline_interface
2291 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2292 && sched_verbose)
2293 /* Debug info. */
2294 visualize_scheduled_insns (clock_var);
2297 if (targetm.sched.md_finish)
2298 (*targetm.sched.md_finish) (sched_dump, sched_verbose);
2300 /* Debug info. */
2301 if (sched_verbose)
2303 fprintf (sched_dump, ";;\tReady list (final): ");
2304 debug_ready_list (&ready);
2305 if (!targetm.sched.use_dfa_pipeline_interface
2306 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2307 print_block_visualization ("");
2310 /* Sanity check -- queue must be empty now. Meaningless if region has
2311 multiple bbs. */
2312 if (current_sched_info->queue_must_finish_empty && q_size != 0)
2313 abort ();
2315 /* Update head/tail boundaries. */
2316 head = NEXT_INSN (prev_head);
2317 tail = last_scheduled_insn;
2319 if (!reload_completed)
2321 rtx insn, link, next;
2323 /* INSN_TICK (minimum clock tick at which the insn becomes
2324 ready) may be not correct for the insn in the subsequent
2325 blocks of the region. We should use a correct value of
2326 `clock_var' or modify INSN_TICK. It is better to keep
2327 clock_var value equal to 0 at the start of a basic block.
2328 Therefore we modify INSN_TICK here. */
2329 for (insn = head; insn != tail; insn = NEXT_INSN (insn))
2330 if (INSN_P (insn))
2332 for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
2334 next = XEXP (link, 0);
2335 INSN_TICK (next) -= clock_var;
2340 /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
2341 previously found among the insns. Insert them at the beginning
2342 of the insns. */
2343 if (note_list != 0)
2345 rtx note_head = note_list;
2347 while (PREV_INSN (note_head))
2349 note_head = PREV_INSN (note_head);
2352 PREV_INSN (note_head) = PREV_INSN (head);
2353 NEXT_INSN (PREV_INSN (head)) = note_head;
2354 PREV_INSN (head) = note_list;
2355 NEXT_INSN (note_list) = head;
2356 head = note_head;
2359 /* Debugging. */
2360 if (sched_verbose)
2362 fprintf (sched_dump, ";; total time = %d\n;; new head = %d\n",
2363 clock_var, INSN_UID (head));
2364 fprintf (sched_dump, ";; new tail = %d\n\n",
2365 INSN_UID (tail));
2366 visualize_free ();
2369 current_sched_info->head = head;
2370 current_sched_info->tail = tail;
2372 free (ready.vec);
2374 if (targetm.sched.use_dfa_pipeline_interface
2375 && (*targetm.sched.use_dfa_pipeline_interface) ())
2377 free (ready_try);
2378 for (i = 0; i <= rgn_n_insns; i++)
2379 free (choice_stack [i].state);
2380 free (choice_stack);
2384 /* Set_priorities: compute priority of each insn in the block. */
2387 set_priorities (head, tail)
2388 rtx head, tail;
2390 rtx insn;
2391 int n_insn;
2393 rtx prev_head;
2395 prev_head = PREV_INSN (head);
2397 if (head == tail && (! INSN_P (head)))
2398 return 0;
2400 n_insn = 0;
2401 for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
2403 if (GET_CODE (insn) == NOTE)
2404 continue;
2406 n_insn++;
2407 (void) priority (insn);
2410 return n_insn;
2413 /* Initialize some global state for the scheduler. DUMP_FILE is to be used
2414 for debugging output. */
2416 void
2417 sched_init (dump_file)
2418 FILE *dump_file;
2420 int luid;
2421 basic_block b;
2422 rtx insn;
2423 int i;
2425 /* Disable speculative loads in their presence if cc0 defined. */
2426 #ifdef HAVE_cc0
2427 flag_schedule_speculative_load = 0;
2428 #endif
2430 /* Set dump and sched_verbose for the desired debugging output. If no
2431 dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
2432 For -fsched-verbose=N, N>=10, print everything to stderr. */
2433 sched_verbose = sched_verbose_param;
2434 if (sched_verbose_param == 0 && dump_file)
2435 sched_verbose = 1;
2436 sched_dump = ((sched_verbose_param >= 10 || !dump_file)
2437 ? stderr : dump_file);
2439 /* Initialize issue_rate. */
2440 if (targetm.sched.issue_rate)
2441 issue_rate = (*targetm.sched.issue_rate) ();
2442 else
2443 issue_rate = 1;
2445 /* We use LUID 0 for the fake insn (UID 0) which holds dependencies for
2446 pseudos which do not cross calls. */
2447 old_max_uid = get_max_uid () + 1;
2449 h_i_d = (struct haifa_insn_data *) xcalloc (old_max_uid, sizeof (*h_i_d));
2451 for (i = 0; i < old_max_uid; i++)
2452 h_i_d [i].cost = -1;
2454 if (targetm.sched.use_dfa_pipeline_interface
2455 && (*targetm.sched.use_dfa_pipeline_interface) ())
2457 if (targetm.sched.init_dfa_pre_cycle_insn)
2458 (*targetm.sched.init_dfa_pre_cycle_insn) ();
2460 if (targetm.sched.init_dfa_post_cycle_insn)
2461 (*targetm.sched.init_dfa_post_cycle_insn) ();
2463 if (targetm.sched.first_cycle_multipass_dfa_lookahead
2464 && targetm.sched.init_dfa_bubbles)
2465 (*targetm.sched.init_dfa_bubbles) ();
2467 dfa_start ();
2468 dfa_state_size = state_size ();
2469 curr_state = xmalloc (dfa_state_size);
2472 h_i_d[0].luid = 0;
2473 luid = 1;
2474 FOR_EACH_BB (b)
2475 for (insn = b->head;; insn = NEXT_INSN (insn))
2477 INSN_LUID (insn) = luid;
2479 /* Increment the next luid, unless this is a note. We don't
2480 really need separate IDs for notes and we don't want to
2481 schedule differently depending on whether or not there are
2482 line-number notes, i.e., depending on whether or not we're
2483 generating debugging information. */
2484 if (GET_CODE (insn) != NOTE)
2485 ++luid;
2487 if (insn == b->end)
2488 break;
2491 init_dependency_caches (luid);
2493 init_alias_analysis ();
2495 if (write_symbols != NO_DEBUG)
2497 rtx line;
2499 line_note_head = (rtx *) xcalloc (last_basic_block, sizeof (rtx));
2501 /* Save-line-note-head:
2502 Determine the line-number at the start of each basic block.
2503 This must be computed and saved now, because after a basic block's
2504 predecessor has been scheduled, it is impossible to accurately
2505 determine the correct line number for the first insn of the block. */
2507 FOR_EACH_BB (b)
2509 for (line = b->head; line; line = PREV_INSN (line))
2510 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
2512 line_note_head[b->index] = line;
2513 break;
2515 /* Do a forward search as well, since we won't get to see the first
2516 notes in a basic block. */
2517 for (line = b->head; line; line = NEXT_INSN (line))
2519 if (INSN_P (line))
2520 break;
2521 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
2522 line_note_head[b->index] = line;
2527 if ((!targetm.sched.use_dfa_pipeline_interface
2528 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2529 && sched_verbose)
2530 /* Find units used in this function, for visualization. */
2531 init_target_units ();
2533 /* ??? Add a NOTE after the last insn of the last basic block. It is not
2534 known why this is done. */
2536 insn = EXIT_BLOCK_PTR->prev_bb->end;
2537 if (NEXT_INSN (insn) == 0
2538 || (GET_CODE (insn) != NOTE
2539 && GET_CODE (insn) != CODE_LABEL
2540 /* Don't emit a NOTE if it would end up before a BARRIER. */
2541 && GET_CODE (NEXT_INSN (insn)) != BARRIER))
2543 emit_note_after (NOTE_INSN_DELETED, EXIT_BLOCK_PTR->prev_bb->end);
2544 /* Make insn to appear outside BB. */
2545 EXIT_BLOCK_PTR->prev_bb->end = PREV_INSN (EXIT_BLOCK_PTR->prev_bb->end);
2548 /* Compute INSN_REG_WEIGHT for all blocks. We must do this before
2549 removing death notes. */
2550 FOR_EACH_BB_REVERSE (b)
2551 find_insn_reg_weight (b->index);
2554 /* Free global data used during insn scheduling. */
2556 void
2557 sched_finish ()
2559 free (h_i_d);
2561 if (targetm.sched.use_dfa_pipeline_interface
2562 && (*targetm.sched.use_dfa_pipeline_interface) ())
2564 free (curr_state);
2565 dfa_finish ();
2567 free_dependency_caches ();
2568 end_alias_analysis ();
2569 if (write_symbols != NO_DEBUG)
2570 free (line_note_head);
2572 #endif /* INSN_SCHEDULING */