OSDN Git Service

2003-01-09 Vladimir Makarov <vmakarov@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / haifa-sched.c
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)
6
7 This file is part of GCC.
8
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.
13
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.
18
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.  */
23
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.
27
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.
36
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.
41
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.
56
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.
63
64    The following list shows the order in which we want to break ties
65    among insns in the ready list:
66
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.
79
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.
86
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 ().
90
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.
95
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.
104
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.
109
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.
114
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.
118
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).
123
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.
128
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.  */
134 \f
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"
154
155 #ifdef INSN_SCHEDULING
156
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.  */
160
161 static int issue_rate;
162
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;
168
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.  */
177
178 static int sched_verbose_param = 0;
179 int sched_verbose = 0;
180
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;
184
185 /* Highest uid before scheduling.  */
186 static int old_max_uid;
187
188 /* fix_sched_param() is called from toplev.c upon detection
189    of the -fsched-verbose=N option.  */
190
191 void
192 fix_sched_param (param, val)
193      const char *param, *val;
194 {
195   if (!strcmp (param, "verbose"))
196     sched_verbose_param = atoi (val);
197   else
198     warning ("fix_sched_param: unknown param: %s", param);
199 }
200
201 struct haifa_insn_data *h_i_d;
202
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)
205
206 /* Vector indexed by basic block number giving the starting line-number
207    for each basic block.  */
208 static rtx *line_note_head;
209
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;
213
214 /* Queues, etc.  */
215
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:
220
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.
227
228    Initially, all insns are either "Pending" or "Ready" depending on
229    whether their dependencies are satisfied.
230
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.
239
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.
246
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.  */
256
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.  */
266
267 #define MAX_INSN_QUEUE_INDEX max_insn_queue_index_macro_value
268
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)
274
275 /* The following variable defines value for macro
276    MAX_INSN_QUEUE_INDEX.  */
277 static int max_insn_queue_index_macro_value;
278
279 /* The following variable value refers for all current and future
280    reservations of the processor units.  */
281 state_t curr_state;
282
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;
287
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;
291
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.  */
299
300 struct ready_list
301 {
302   rtx *vec;
303   int veclen;
304   int first;
305   int n_ready;
306 };
307
308 /* Forward declarations.  */
309
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));
317
318 static int priority PARAMS ((rtx));
319 static int rank_for_schedule PARAMS ((const PTR, const PTR));
320 static void swap_sort PARAMS ((rtx *, int));
321 static void queue_insn PARAMS ((rtx, int));
322 static void schedule_insn PARAMS ((rtx, struct ready_list *, int));
323 static int find_set_reg_weight PARAMS ((rtx));
324 static void find_insn_reg_weight PARAMS ((int));
325 static void adjust_priority PARAMS ((rtx));
326 static void advance_one_cycle PARAMS ((void));
327
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:
332
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()).
339
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).
345
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()).  */
350
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));
354
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 *));
358
359 static void queue_to_ready PARAMS ((struct ready_list *));
360
361 static void debug_ready_list PARAMS ((struct ready_list *));
362
363 static rtx move_insn1 PARAMS ((rtx, rtx));
364 static rtx move_insn PARAMS ((rtx, rtx));
365
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 *));
371
372 static rtx choose_ready PARAMS ((struct ready_list *));
373
374 #endif /* INSN_SCHEDULING */
375 \f
376 /* Point to state used for the current scheduling pass.  */
377 struct sched_info *current_sched_info;
378 \f
379 #ifndef INSN_SCHEDULING
380 void
381 schedule_insns (dump_file)
382      FILE *dump_file ATTRIBUTE_UNUSED;
383 {
384 }
385 #else
386
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.  */
390
391 static rtx last_scheduled_insn;
392
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.  */
399
400 HAIFA_INLINE int
401 insn_unit (insn)
402      rtx insn;
403 {
404   int unit = INSN_UNIT (insn);
405
406   if (unit == 0)
407     {
408       recog_memoized (insn);
409
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
416         {
417           unit = function_units_used (insn);
418           /* Increment non-negative values so we can cache zero.  */
419           if (unit >= 0)
420             unit++;
421         }
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;
428     }
429   return (unit > 0 ? unit - 1 : unit);
430 }
431
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.  */
438
439 HAIFA_INLINE static unsigned int
440 blockage_range (unit, insn)
441      int unit;
442      rtx insn;
443 {
444   unsigned int blockage = INSN_BLOCKAGE (insn);
445   unsigned int range;
446
447   if ((int) UNIT_BLOCKED (blockage) != unit + 1)
448     {
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);
454     }
455   else
456     range = BLOCKAGE_RANGE (blockage);
457
458   return range;
459 }
460
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
470
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
480
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
489
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.  */
493
494 rtx
495 get_unit_last_insn (instance)
496      int instance;
497 {
498   return unit_last_insn[instance];
499 }
500
501 /* Reset the function unit state to the null state.  */
502
503 static void
504 clear_units ()
505 {
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));
509 }
510
511 /* Return the issue-delay of an insn.  The scheduler using only DFA
512    description should never use the following function.  */
513
514 HAIFA_INLINE int
515 insn_issue_delay (insn)
516      rtx insn;
517 {
518   int i, delay = 0;
519   int unit = insn_unit (insn);
520
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)
526     {
527       if (function_units[unit].blockage_range_function &&
528           function_units[unit].blockage_function)
529         delay = function_units[unit].blockage_function (insn, insn);
530     }
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));
536
537   return delay;
538 }
539
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.  */
544
545 HAIFA_INLINE int
546 actual_hazard_this_instance (unit, instance, insn, clock, cost)
547      int unit, instance, clock, cost;
548      rtx insn;
549 {
550   int tick = unit_tick[instance]; /* Issue time of the last issued insn.  */
551
552   if (tick - clock > cost)
553     {
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.
558
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.  */
562
563       if (function_units[unit].blockage_range_function)
564         {
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);
572         }
573       if (tick - clock > cost)
574         cost = tick - clock;
575     }
576   return cost;
577 }
578
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.  */
582
583 HAIFA_INLINE static void
584 schedule_unit (unit, insn, clock)
585      int unit, clock;
586      rtx insn;
587 {
588   int i;
589
590   if (unit >= 0)
591     {
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--)
597         {
598           if (!actual_hazard_this_instance (unit, instance, insn, clock, 0))
599             break;
600           instance += FUNCTION_UNITS_SIZE;
601         }
602 #endif
603       unit_last_insn[instance] = insn;
604       unit_tick[instance] = (clock + function_units[unit].max_blockage);
605     }
606   else
607     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
608       if ((unit & 1) != 0)
609         schedule_unit (i, insn, clock);
610 }
611
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.  */
616
617 HAIFA_INLINE static int
618 actual_hazard (unit, insn, clock, cost)
619      int unit, clock, cost;
620      rtx insn;
621 {
622   int i;
623
624   if (unit >= 0)
625     {
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;
632
633       if (best_cost > cost)
634         {
635           for (i = function_units[unit].multiplicity - 1; i > 0; i--)
636             {
637               instance += FUNCTION_UNITS_SIZE;
638               this_cost = actual_hazard_this_instance (unit, instance, insn,
639                                                        clock, cost);
640               if (this_cost < best_cost)
641                 {
642                   best_cost = this_cost;
643                   if (this_cost <= cost)
644                     break;
645                 }
646             }
647         }
648 #endif
649       cost = MAX (cost, best_cost);
650     }
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);
655
656   return cost;
657 }
658
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.  */
667
668 HAIFA_INLINE static int
669 potential_hazard (unit, insn, cost)
670      int unit, cost;
671      rtx insn;
672 {
673   int i, ncost;
674   unsigned int minb, maxb;
675
676   if (unit >= 0)
677     {
678       minb = maxb = function_units[unit].max_blockage;
679       if (maxb > 1)
680         {
681           if (function_units[unit].blockage_range_function)
682             {
683               maxb = minb = blockage_range (unit, insn);
684               maxb = MAX_BLOCKAGE_COST (maxb);
685               minb = MIN_BLOCKAGE_COST (minb);
686             }
687
688           if (maxb > 1)
689             {
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;
698             }
699         }
700     }
701   else
702     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
703       if ((unit & 1) != 0)
704         cost = potential_hazard (i, insn, cost);
705
706   return cost;
707 }
708
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.  */
712
713 HAIFA_INLINE int
714 insn_cost (insn, link, used)
715      rtx insn, link, used;
716 {
717   int cost = INSN_COST (insn);
718
719   if (cost < 0)
720     {
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)
726         {
727           INSN_COST (insn) = 0;
728           return 0;
729         }
730       else
731         {
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);
737           
738           if (cost < 0)
739             cost = 0;
740           
741           INSN_COST (insn) = cost;
742         }
743     }
744
745   /* In this case estimate cost without caring how insn is used.  */
746   if (link == 0 || used == 0)
747     return cost;
748
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
755     {
756       if (targetm.sched.use_dfa_pipeline_interface
757           && (*targetm.sched.use_dfa_pipeline_interface) ())
758         {
759           if (INSN_CODE (insn) >= 0)
760             {
761               if (REG_NOTE_KIND (link) == REG_DEP_ANTI)
762                 cost = 0;
763               else if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
764                 {
765                   cost = (insn_default_latency (insn)
766                           - insn_default_latency (used));
767                   if (cost <= 0)
768                     cost = 1;
769                 }
770               else if (bypass_p (insn))
771                 cost = insn_latency (insn, used);
772             }
773         }
774
775       if (targetm.sched.adjust_cost)
776         cost = (*targetm.sched.adjust_cost) (used, link, insn, cost);
777
778       if (cost < 0)
779         cost = 0;
780     }
781   
782   return cost;
783 }
784
785 /* Compute the priority number for INSN.  */
786
787 static int
788 priority (insn)
789      rtx insn;
790 {
791   rtx link;
792
793   if (! INSN_P (insn))
794     return 0;
795
796   if (! INSN_PRIORITY_KNOWN (insn))
797     {
798       int this_priority = 0;
799
800       if (INSN_DEPEND (insn) == 0)
801         this_priority = insn_cost (insn, 0, 0);
802       else
803         {
804           for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
805             {
806               rtx next;
807               int next_priority;
808
809               if (RTX_INTEGRATED_P (link))
810                 continue;
811
812               next = XEXP (link, 0);
813
814               /* Critical path is meaningful in block boundaries only.  */
815               if (! (*current_sched_info->contributes_to_priority) (next, insn))
816                 continue;
817
818               next_priority = insn_cost (insn, link, next) + priority (next);
819               if (next_priority > this_priority)
820                 this_priority = next_priority;
821             }
822         }
823       INSN_PRIORITY (insn) = this_priority;
824       INSN_PRIORITY_KNOWN (insn) = 1;
825     }
826
827   return INSN_PRIORITY (insn);
828 }
829 \f
830 /* Macros and functions for keeping the priority queue sorted, and
831    dealing with queueing and dequeueing of instructions.  */
832
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)
839
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.  */
843
844 static int
845 rank_for_schedule (x, y)
846      const PTR x;
847      const PTR y;
848 {
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;
854
855   /* Prefer insn with higher priority.  */
856   priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
857
858   if (priority_val)
859     return priority_val;
860
861   /* Prefer an insn with smaller contribution to registers-pressure.  */
862   if (!reload_completed &&
863       (weight_val = INSN_REG_WEIGHT (tmp) - INSN_REG_WEIGHT (tmp2)))
864     return weight_val;
865
866   info_val = (*current_sched_info->rank) (tmp, tmp2);
867   if (info_val)
868     return info_val;
869
870   /* Compare insns based on their relation to the last-scheduled-insn.  */
871   if (last_scheduled_insn)
872     {
873       /* Classify the instructions into three classes:
874          1) Data dependent on last schedule insn.
875          2) Anti/Output dependent on last scheduled insn.
876          3) Independent of last scheduled insn, or has latency of one.
877          Choose the insn from the highest numbered class if different.  */
878       link = find_insn_list (tmp, INSN_DEPEND (last_scheduled_insn));
879       if (link == 0 || insn_cost (last_scheduled_insn, link, tmp) == 1)
880         tmp_class = 3;
881       else if (REG_NOTE_KIND (link) == 0)       /* Data dependence.  */
882         tmp_class = 1;
883       else
884         tmp_class = 2;
885
886       link = find_insn_list (tmp2, INSN_DEPEND (last_scheduled_insn));
887       if (link == 0 || insn_cost (last_scheduled_insn, link, tmp2) == 1)
888         tmp2_class = 3;
889       else if (REG_NOTE_KIND (link) == 0)       /* Data dependence.  */
890         tmp2_class = 1;
891       else
892         tmp2_class = 2;
893
894       if ((val = tmp2_class - tmp_class))
895         return val;
896     }
897
898   /* Prefer the insn which has more later insns that depend on it.
899      This gives the scheduler more freedom when scheduling later
900      instructions at the expense of added register pressure.  */
901   depend_count1 = 0;
902   for (link = INSN_DEPEND (tmp); link; link = XEXP (link, 1))
903     depend_count1++;
904
905   depend_count2 = 0;
906   for (link = INSN_DEPEND (tmp2); link; link = XEXP (link, 1))
907     depend_count2++;
908
909   val = depend_count2 - depend_count1;
910   if (val)
911     return val;
912
913   /* If insns are equally good, sort by INSN_LUID (original insn order),
914      so that we make the sort stable.  This minimizes instruction movement,
915      thus minimizing sched's effect on debugging and cross-jumping.  */
916   return INSN_LUID (tmp) - INSN_LUID (tmp2);
917 }
918
919 /* Resort the array A in which only element at index N may be out of order.  */
920
921 HAIFA_INLINE static void
922 swap_sort (a, n)
923      rtx *a;
924      int n;
925 {
926   rtx insn = a[n - 1];
927   int i = n - 2;
928
929   while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
930     {
931       a[i + 1] = a[i];
932       i -= 1;
933     }
934   a[i + 1] = insn;
935 }
936
937 /* Add INSN to the insn queue so that it can be executed at least
938    N_CYCLES after the currently executing insn.  Preserve insns
939    chain for debugging purposes.  */
940
941 HAIFA_INLINE static void
942 queue_insn (insn, n_cycles)
943      rtx insn;
944      int n_cycles;
945 {
946   int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
947   rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
948   insn_queue[next_q] = link;
949   q_size += 1;
950
951   if (sched_verbose >= 2)
952     {
953       fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
954                (*current_sched_info->print_insn) (insn, 0));
955
956       fprintf (sched_dump, "queued for %d cycles.\n", n_cycles);
957     }
958 }
959
960 /* Return a pointer to the bottom of the ready list, i.e. the insn
961    with the lowest priority.  */
962
963 HAIFA_INLINE static rtx *
964 ready_lastpos (ready)
965      struct ready_list *ready;
966 {
967   if (ready->n_ready == 0)
968     abort ();
969   return ready->vec + ready->first - ready->n_ready + 1;
970 }
971
972 /* Add an element INSN to the ready list so that it ends up with the lowest
973    priority.  */
974
975 HAIFA_INLINE void
976 ready_add (ready, insn)
977      struct ready_list *ready;
978      rtx insn;
979 {
980   if (ready->first == ready->n_ready)
981     {
982       memmove (ready->vec + ready->veclen - ready->n_ready,
983                ready_lastpos (ready),
984                ready->n_ready * sizeof (rtx));
985       ready->first = ready->veclen - 1;
986     }
987   ready->vec[ready->first - ready->n_ready] = insn;
988   ready->n_ready++;
989 }
990
991 /* Remove the element with the highest priority from the ready list and
992    return it.  */
993
994 HAIFA_INLINE static rtx
995 ready_remove_first (ready)
996      struct ready_list *ready;
997 {
998   rtx t;
999   if (ready->n_ready == 0)
1000     abort ();
1001   t = ready->vec[ready->first--];
1002   ready->n_ready--;
1003   /* If the queue becomes empty, reset it.  */
1004   if (ready->n_ready == 0)
1005     ready->first = ready->veclen - 1;
1006   return t;
1007 }
1008
1009 /* The following code implements multi-pass scheduling for the first
1010    cycle.  In other words, we will try to choose ready insn which
1011    permits to start maximum number of insns on the same cycle.  */
1012
1013 /* Return a pointer to the element INDEX from the ready.  INDEX for
1014    insn with the highest priority is 0, and the lowest priority has
1015    N_READY - 1.  */
1016
1017 HAIFA_INLINE static rtx
1018 ready_element (ready, index)
1019      struct ready_list *ready;
1020      int index;
1021 {
1022 #ifdef ENABLE_CHECKING
1023   if (ready->n_ready == 0 || index >= ready->n_ready)
1024     abort ();
1025 #endif
1026   return ready->vec[ready->first - index];
1027 }
1028
1029 /* Remove the element INDEX from the ready list and return it.  INDEX
1030    for insn with the highest priority is 0, and the lowest priority
1031    has N_READY - 1.  */
1032
1033 HAIFA_INLINE static rtx
1034 ready_remove (ready, index)
1035      struct ready_list *ready;
1036      int index;
1037 {
1038   rtx t;
1039   int i;
1040
1041   if (index == 0)
1042     return ready_remove_first (ready);
1043   if (ready->n_ready == 0 || index >= ready->n_ready)
1044     abort ();
1045   t = ready->vec[ready->first - index];
1046   ready->n_ready--;
1047   for (i = index; i < ready->n_ready; i++)
1048     ready->vec[ready->first - i] = ready->vec[ready->first - i - 1];
1049   return t;
1050 }
1051
1052
1053 /* Sort the ready list READY by ascending priority, using the SCHED_SORT
1054    macro.  */
1055
1056 HAIFA_INLINE static void
1057 ready_sort (ready)
1058      struct ready_list *ready;
1059 {
1060   rtx *first = ready_lastpos (ready);
1061   SCHED_SORT (first, ready->n_ready);
1062 }
1063
1064 /* PREV is an insn that is ready to execute.  Adjust its priority if that
1065    will help shorten or lengthen register lifetimes as appropriate.  Also
1066    provide a hook for the target to tweek itself.  */
1067
1068 HAIFA_INLINE static void
1069 adjust_priority (prev)
1070      rtx prev;
1071 {
1072   /* ??? There used to be code here to try and estimate how an insn
1073      affected register lifetimes, but it did it by looking at REG_DEAD
1074      notes, which we removed in schedule_region.  Nor did it try to
1075      take into account register pressure or anything useful like that.
1076
1077      Revisit when we have a machine model to work with and not before.  */
1078
1079   if (targetm.sched.adjust_priority)
1080     INSN_PRIORITY (prev) =
1081       (*targetm.sched.adjust_priority) (prev, INSN_PRIORITY (prev));
1082 }
1083
1084 /* Advance time on one cycle.  */
1085 HAIFA_INLINE static void
1086 advance_one_cycle ()
1087 {
1088   if (targetm.sched.use_dfa_pipeline_interface
1089       && (*targetm.sched.use_dfa_pipeline_interface) ())
1090     {
1091       if (targetm.sched.dfa_pre_cycle_insn)
1092         state_transition (curr_state,
1093                           (*targetm.sched.dfa_pre_cycle_insn) ());
1094
1095       state_transition (curr_state, NULL);
1096
1097       if (targetm.sched.dfa_post_cycle_insn)
1098         state_transition (curr_state,
1099                           (*targetm.sched.dfa_post_cycle_insn) ());
1100     }
1101 }
1102
1103 /* Clock at which the previous instruction was issued.  */
1104 static int last_clock_var;
1105
1106 /* INSN is the "currently executing insn".  Launch each insn which was
1107    waiting on INSN.  READY is the ready list which contains the insns
1108    that are ready to fire.  CLOCK is the current cycle.
1109    */
1110
1111 static void
1112 schedule_insn (insn, ready, clock)
1113      rtx insn;
1114      struct ready_list *ready;
1115      int clock;
1116 {
1117   rtx link;
1118   int unit = 0;
1119
1120   if (!targetm.sched.use_dfa_pipeline_interface
1121       || !(*targetm.sched.use_dfa_pipeline_interface) ())
1122     unit = insn_unit (insn);
1123
1124   if (targetm.sched.use_dfa_pipeline_interface
1125       && (*targetm.sched.use_dfa_pipeline_interface) ()
1126       && sched_verbose >= 1)
1127     {
1128       char buf[2048];
1129
1130       print_insn (buf, insn, 0);
1131       buf[40]=0;
1132       fprintf (sched_dump, ";;\t%3i--> %-40s:", clock, buf);
1133
1134       if (recog_memoized (insn) < 0)
1135         fprintf (sched_dump, "nothing");
1136       else
1137         print_reservation (sched_dump, insn);
1138       fputc ('\n', sched_dump);
1139     }
1140   else if (sched_verbose >= 2)
1141     {
1142       fprintf (sched_dump, ";;\t\t--> scheduling insn <<<%d>>> on unit ",
1143                INSN_UID (insn));
1144       insn_print_units (insn);
1145       fputc ('\n', sched_dump);
1146     }
1147
1148   if (!targetm.sched.use_dfa_pipeline_interface
1149       || !(*targetm.sched.use_dfa_pipeline_interface) ())
1150     {
1151       if (sched_verbose && unit == -1)
1152         visualize_no_unit (insn);
1153
1154
1155       if (MAX_BLOCKAGE > 1 || issue_rate > 1 || sched_verbose)
1156         schedule_unit (unit, insn, clock);
1157       
1158       if (INSN_DEPEND (insn) == 0)
1159         return;
1160     }
1161
1162   for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
1163     {
1164       rtx next = XEXP (link, 0);
1165       int cost = insn_cost (insn, link, next);
1166
1167       INSN_TICK (next) = MAX (INSN_TICK (next), clock + cost);
1168
1169       if ((INSN_DEP_COUNT (next) -= 1) == 0)
1170         {
1171           int effective_cost = INSN_TICK (next) - clock;
1172
1173           if (! (*current_sched_info->new_ready) (next))
1174             continue;
1175
1176           if (sched_verbose >= 2)
1177             {
1178               fprintf (sched_dump, ";;\t\tdependences resolved: insn %s ",
1179                        (*current_sched_info->print_insn) (next, 0));
1180
1181               if (effective_cost < 1)
1182                 fprintf (sched_dump, "into ready\n");
1183               else
1184                 fprintf (sched_dump, "into queue with cost=%d\n", effective_cost);
1185             }
1186
1187           /* Adjust the priority of NEXT and either put it on the ready
1188              list or queue it.  */
1189           adjust_priority (next);
1190           if (effective_cost < 1)
1191             ready_add (ready, next);
1192           else
1193             queue_insn (next, effective_cost);
1194         }
1195     }
1196
1197   /* Annotate the instruction with issue information -- TImode
1198      indicates that the instruction is expected not to be able
1199      to issue on the same cycle as the previous insn.  A machine
1200      may use this information to decide how the instruction should
1201      be aligned.  */
1202   if (issue_rate > 1
1203       && GET_CODE (PATTERN (insn)) != USE
1204       && GET_CODE (PATTERN (insn)) != CLOBBER)
1205     {
1206       if (reload_completed)
1207         PUT_MODE (insn, clock > last_clock_var ? TImode : VOIDmode);
1208       last_clock_var = clock;
1209     }
1210 }
1211
1212 /* Functions for handling of notes.  */
1213
1214 /* Delete notes beginning with INSN and put them in the chain
1215    of notes ended by NOTE_LIST.
1216    Returns the insn following the notes.  */
1217
1218 static rtx
1219 unlink_other_notes (insn, tail)
1220      rtx insn, tail;
1221 {
1222   rtx prev = PREV_INSN (insn);
1223
1224   while (insn != tail && GET_CODE (insn) == NOTE)
1225     {
1226       rtx next = NEXT_INSN (insn);
1227       /* Delete the note from its current position.  */
1228       if (prev)
1229         NEXT_INSN (prev) = next;
1230       if (next)
1231         PREV_INSN (next) = prev;
1232
1233       /* See sched_analyze to see how these are handled.  */
1234       if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG
1235           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_END
1236           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_BEG
1237           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_END)
1238         {
1239           /* Insert the note at the end of the notes list.  */
1240           PREV_INSN (insn) = note_list;
1241           if (note_list)
1242             NEXT_INSN (note_list) = insn;
1243           note_list = insn;
1244         }
1245
1246       insn = next;
1247     }
1248   return insn;
1249 }
1250
1251 /* Delete line notes beginning with INSN. Record line-number notes so
1252    they can be reused.  Returns the insn following the notes.  */
1253
1254 static rtx
1255 unlink_line_notes (insn, tail)
1256      rtx insn, tail;
1257 {
1258   rtx prev = PREV_INSN (insn);
1259
1260   while (insn != tail && GET_CODE (insn) == NOTE)
1261     {
1262       rtx next = NEXT_INSN (insn);
1263
1264       if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
1265         {
1266           /* Delete the note from its current position.  */
1267           if (prev)
1268             NEXT_INSN (prev) = next;
1269           if (next)
1270             PREV_INSN (next) = prev;
1271
1272           /* Record line-number notes so they can be reused.  */
1273           LINE_NOTE (insn) = insn;
1274         }
1275       else
1276         prev = insn;
1277
1278       insn = next;
1279     }
1280   return insn;
1281 }
1282
1283 /* Return the head and tail pointers of BB.  */
1284
1285 void
1286 get_block_head_tail (b, headp, tailp)
1287      int b;
1288      rtx *headp;
1289      rtx *tailp;
1290 {
1291   /* HEAD and TAIL delimit the basic block being scheduled.  */
1292   rtx head = BLOCK_HEAD (b);
1293   rtx tail = BLOCK_END (b);
1294
1295   /* Don't include any notes or labels at the beginning of the
1296      basic block, or notes at the ends of basic blocks.  */
1297   while (head != tail)
1298     {
1299       if (GET_CODE (head) == NOTE)
1300         head = NEXT_INSN (head);
1301       else if (GET_CODE (tail) == NOTE)
1302         tail = PREV_INSN (tail);
1303       else if (GET_CODE (head) == CODE_LABEL)
1304         head = NEXT_INSN (head);
1305       else
1306         break;
1307     }
1308
1309   *headp = head;
1310   *tailp = tail;
1311 }
1312
1313 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ].  */
1314
1315 int
1316 no_real_insns_p (head, tail)
1317      rtx head, tail;
1318 {
1319   while (head != NEXT_INSN (tail))
1320     {
1321       if (GET_CODE (head) != NOTE && GET_CODE (head) != CODE_LABEL)
1322         return 0;
1323       head = NEXT_INSN (head);
1324     }
1325   return 1;
1326 }
1327
1328 /* Delete line notes from one block. Save them so they can be later restored
1329    (in restore_line_notes).  HEAD and TAIL are the boundaries of the
1330    block in which notes should be processed.  */
1331
1332 void
1333 rm_line_notes (head, tail)
1334      rtx head, tail;
1335 {
1336   rtx next_tail;
1337   rtx insn;
1338
1339   next_tail = NEXT_INSN (tail);
1340   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1341     {
1342       rtx prev;
1343
1344       /* Farm out notes, and maybe save them in NOTE_LIST.
1345          This is needed to keep the debugger from
1346          getting completely deranged.  */
1347       if (GET_CODE (insn) == NOTE)
1348         {
1349           prev = insn;
1350           insn = unlink_line_notes (insn, next_tail);
1351
1352           if (prev == tail)
1353             abort ();
1354           if (prev == head)
1355             abort ();
1356           if (insn == next_tail)
1357             abort ();
1358         }
1359     }
1360 }
1361
1362 /* Save line number notes for each insn in block B.  HEAD and TAIL are
1363    the boundaries of the block in which notes should be processed.  */
1364
1365 void
1366 save_line_notes (b, head, tail)
1367      int b;
1368      rtx head, tail;
1369 {
1370   rtx next_tail;
1371
1372   /* We must use the true line number for the first insn in the block
1373      that was computed and saved at the start of this pass.  We can't
1374      use the current line number, because scheduling of the previous
1375      block may have changed the current line number.  */
1376
1377   rtx line = line_note_head[b];
1378   rtx insn;
1379
1380   next_tail = NEXT_INSN (tail);
1381
1382   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1383     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1384       line = insn;
1385     else
1386       LINE_NOTE (insn) = line;
1387 }
1388
1389 /* After a block was scheduled, insert line notes into the insns list.
1390    HEAD and TAIL are the boundaries of the block in which notes should
1391    be processed.  */
1392
1393 void
1394 restore_line_notes (head, tail)
1395      rtx head, tail;
1396 {
1397   rtx line, note, prev, new;
1398   int added_notes = 0;
1399   rtx next_tail, insn;
1400
1401   head = head;
1402   next_tail = NEXT_INSN (tail);
1403
1404   /* Determine the current line-number.  We want to know the current
1405      line number of the first insn of the block here, in case it is
1406      different from the true line number that was saved earlier.  If
1407      different, then we need a line number note before the first insn
1408      of this block.  If it happens to be the same, then we don't want to
1409      emit another line number note here.  */
1410   for (line = head; line; line = PREV_INSN (line))
1411     if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
1412       break;
1413
1414   /* Walk the insns keeping track of the current line-number and inserting
1415      the line-number notes as needed.  */
1416   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1417     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1418       line = insn;
1419   /* This used to emit line number notes before every non-deleted note.
1420      However, this confuses a debugger, because line notes not separated
1421      by real instructions all end up at the same address.  I can find no
1422      use for line number notes before other notes, so none are emitted.  */
1423     else if (GET_CODE (insn) != NOTE
1424              && INSN_UID (insn) < old_max_uid
1425              && (note = LINE_NOTE (insn)) != 0
1426              && note != line
1427              && (line == 0
1428                  || NOTE_LINE_NUMBER (note) != NOTE_LINE_NUMBER (line)
1429                  || NOTE_SOURCE_FILE (note) != NOTE_SOURCE_FILE (line)))
1430       {
1431         line = note;
1432         prev = PREV_INSN (insn);
1433         if (LINE_NOTE (note))
1434           {
1435             /* Re-use the original line-number note.  */
1436             LINE_NOTE (note) = 0;
1437             PREV_INSN (note) = prev;
1438             NEXT_INSN (prev) = note;
1439             PREV_INSN (insn) = note;
1440             NEXT_INSN (note) = insn;
1441           }
1442         else
1443           {
1444             added_notes++;
1445             new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
1446             NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
1447             RTX_INTEGRATED_P (new) = RTX_INTEGRATED_P (note);
1448           }
1449       }
1450   if (sched_verbose && added_notes)
1451     fprintf (sched_dump, ";; added %d line-number notes\n", added_notes);
1452 }
1453
1454 /* After scheduling the function, delete redundant line notes from the
1455    insns list.  */
1456
1457 void
1458 rm_redundant_line_notes ()
1459 {
1460   rtx line = 0;
1461   rtx insn = get_insns ();
1462   int active_insn = 0;
1463   int notes = 0;
1464
1465   /* Walk the insns deleting redundant line-number notes.  Many of these
1466      are already present.  The remainder tend to occur at basic
1467      block boundaries.  */
1468   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1469     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1470       {
1471         /* If there are no active insns following, INSN is redundant.  */
1472         if (active_insn == 0)
1473           {
1474             notes++;
1475             NOTE_SOURCE_FILE (insn) = 0;
1476             NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1477           }
1478         /* If the line number is unchanged, LINE is redundant.  */
1479         else if (line
1480                  && NOTE_LINE_NUMBER (line) == NOTE_LINE_NUMBER (insn)
1481                  && NOTE_SOURCE_FILE (line) == NOTE_SOURCE_FILE (insn))
1482           {
1483             notes++;
1484             NOTE_SOURCE_FILE (line) = 0;
1485             NOTE_LINE_NUMBER (line) = NOTE_INSN_DELETED;
1486             line = insn;
1487           }
1488         else
1489           line = insn;
1490         active_insn = 0;
1491       }
1492     else if (!((GET_CODE (insn) == NOTE
1493                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
1494                || (GET_CODE (insn) == INSN
1495                    && (GET_CODE (PATTERN (insn)) == USE
1496                        || GET_CODE (PATTERN (insn)) == CLOBBER))))
1497       active_insn++;
1498
1499   if (sched_verbose && notes)
1500     fprintf (sched_dump, ";; deleted %d line-number notes\n", notes);
1501 }
1502
1503 /* Delete notes between HEAD and TAIL and put them in the chain
1504    of notes ended by NOTE_LIST.  */
1505
1506 void
1507 rm_other_notes (head, tail)
1508      rtx head;
1509      rtx tail;
1510 {
1511   rtx next_tail;
1512   rtx insn;
1513
1514   note_list = 0;
1515   if (head == tail && (! INSN_P (head)))
1516     return;
1517
1518   next_tail = NEXT_INSN (tail);
1519   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1520     {
1521       rtx prev;
1522
1523       /* Farm out notes, and maybe save them in NOTE_LIST.
1524          This is needed to keep the debugger from
1525          getting completely deranged.  */
1526       if (GET_CODE (insn) == NOTE)
1527         {
1528           prev = insn;
1529
1530           insn = unlink_other_notes (insn, next_tail);
1531
1532           if (prev == tail)
1533             abort ();
1534           if (prev == head)
1535             abort ();
1536           if (insn == next_tail)
1537             abort ();
1538         }
1539     }
1540 }
1541
1542 /* Functions for computation of registers live/usage info.  */
1543
1544 /* This function looks for a new register being defined.
1545    If the destination register is already used by the source,
1546    a new register is not needed. */
1547
1548 static int
1549 find_set_reg_weight (x)
1550     rtx x;
1551 {
1552   if (GET_CODE (x) == CLOBBER
1553       && register_operand (SET_DEST (x), VOIDmode))
1554     return 1;
1555   if (GET_CODE (x) == SET
1556       && register_operand (SET_DEST (x), VOIDmode))
1557     {
1558       if (GET_CODE (SET_DEST (x)) == REG)
1559         {
1560           if (!reg_mentioned_p (SET_DEST (x), SET_SRC (x)))
1561             return 1;
1562           else
1563             return 0;
1564         }
1565       return 1;
1566     }
1567   return 0;
1568 }
1569
1570 /* Calculate INSN_REG_WEIGHT for all insns of a block.  */
1571
1572 static void
1573 find_insn_reg_weight (b)
1574      int b;
1575 {
1576   rtx insn, next_tail, head, tail;
1577
1578   get_block_head_tail (b, &head, &tail);
1579   next_tail = NEXT_INSN (tail);
1580
1581   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1582     {
1583       int reg_weight = 0;
1584       rtx x;
1585
1586       /* Handle register life information.  */
1587       if (! INSN_P (insn))
1588         continue;
1589
1590       /* Increment weight for each register born here.  */
1591       x = PATTERN (insn);
1592       reg_weight += find_set_reg_weight (x);
1593       if (GET_CODE (x) == PARALLEL)
1594         {
1595           int j;
1596           for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
1597             {
1598               x = XVECEXP (PATTERN (insn), 0, j);
1599               reg_weight += find_set_reg_weight (x);
1600             }
1601         }
1602       /* Decrement weight for each register that dies here.  */
1603       for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1604         {
1605           if (REG_NOTE_KIND (x) == REG_DEAD
1606               || REG_NOTE_KIND (x) == REG_UNUSED)
1607             reg_weight--;
1608         }
1609
1610       INSN_REG_WEIGHT (insn) = reg_weight;
1611     }
1612 }
1613
1614 /* Scheduling clock, modified in schedule_block() and queue_to_ready ().  */
1615 static int clock_var;
1616
1617 /* Move insns that became ready to fire from queue to ready list.  */
1618
1619 static void
1620 queue_to_ready (ready)
1621      struct ready_list *ready;
1622 {
1623   rtx insn;
1624   rtx link;
1625
1626   q_ptr = NEXT_Q (q_ptr);
1627
1628   /* Add all pending insns that can be scheduled without stalls to the
1629      ready list.  */
1630   for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
1631     {
1632       insn = XEXP (link, 0);
1633       q_size -= 1;
1634
1635       if (sched_verbose >= 2)
1636         fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1637                  (*current_sched_info->print_insn) (insn, 0));
1638
1639       ready_add (ready, insn);
1640       if (sched_verbose >= 2)
1641         fprintf (sched_dump, "moving to ready without stalls\n");
1642     }
1643   insn_queue[q_ptr] = 0;
1644
1645   /* If there are no ready insns, stall until one is ready and add all
1646      of the pending insns at that point to the ready list.  */
1647   if (ready->n_ready == 0)
1648     {
1649       int stalls;
1650
1651       for (stalls = 1; stalls <= MAX_INSN_QUEUE_INDEX; stalls++)
1652         {
1653           if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
1654             {
1655               for (; link; link = XEXP (link, 1))
1656                 {
1657                   insn = XEXP (link, 0);
1658                   q_size -= 1;
1659
1660                   if (sched_verbose >= 2)
1661                     fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1662                              (*current_sched_info->print_insn) (insn, 0));
1663
1664                   ready_add (ready, insn);
1665                   if (sched_verbose >= 2)
1666                     fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
1667                 }
1668               insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = 0;
1669
1670               advance_one_cycle ();
1671
1672               break;
1673             }
1674
1675           advance_one_cycle ();
1676         }
1677
1678       if ((!targetm.sched.use_dfa_pipeline_interface
1679            || !(*targetm.sched.use_dfa_pipeline_interface) ())
1680           && sched_verbose && stalls)
1681         visualize_stall_cycles (stalls);
1682
1683       q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
1684       clock_var += stalls;
1685     }
1686 }
1687
1688 /* Print the ready list for debugging purposes.  Callable from debugger.  */
1689
1690 static void
1691 debug_ready_list (ready)
1692      struct ready_list *ready;
1693 {
1694   rtx *p;
1695   int i;
1696
1697   if (ready->n_ready == 0)
1698     {
1699       fprintf (sched_dump, "\n");
1700       return;
1701     }
1702
1703   p = ready_lastpos (ready);
1704   for (i = 0; i < ready->n_ready; i++)
1705     fprintf (sched_dump, "  %s", (*current_sched_info->print_insn) (p[i], 0));
1706   fprintf (sched_dump, "\n");
1707 }
1708
1709 /* move_insn1: Remove INSN from insn chain, and link it after LAST insn.  */
1710
1711 static rtx
1712 move_insn1 (insn, last)
1713      rtx insn, last;
1714 {
1715   NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
1716   PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
1717
1718   NEXT_INSN (insn) = NEXT_INSN (last);
1719   PREV_INSN (NEXT_INSN (last)) = insn;
1720
1721   NEXT_INSN (last) = insn;
1722   PREV_INSN (insn) = last;
1723
1724   return insn;
1725 }
1726
1727 /* Search INSN for REG_SAVE_NOTE note pairs for
1728    NOTE_INSN_{LOOP,EHREGION}_{BEG,END}; and convert them back into
1729    NOTEs.  The REG_SAVE_NOTE note following first one is contains the
1730    saved value for NOTE_BLOCK_NUMBER which is useful for
1731    NOTE_INSN_EH_REGION_{BEG,END} NOTEs.  LAST is the last instruction
1732    output by the instruction scheduler.  Return the new value of LAST.  */
1733
1734 static rtx
1735 reemit_notes (insn, last)
1736      rtx insn;
1737      rtx last;
1738 {
1739   rtx note, retval;
1740
1741   retval = last;
1742   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1743     {
1744       if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
1745         {
1746           enum insn_note note_type = INTVAL (XEXP (note, 0));
1747
1748           last = emit_note_before (note_type, last);
1749           remove_note (insn, note);
1750           note = XEXP (note, 1);
1751           if (note_type == NOTE_INSN_EH_REGION_BEG
1752               || note_type == NOTE_INSN_EH_REGION_END)
1753             NOTE_EH_HANDLER (last) = INTVAL (XEXP (note, 0));
1754           remove_note (insn, note);
1755         }
1756     }
1757   return retval;
1758 }
1759
1760 /* Move INSN, and all insns which should be issued before it,
1761    due to SCHED_GROUP_P flag.  Reemit notes if needed.
1762
1763    Return the last insn emitted by the scheduler, which is the
1764    return value from the first call to reemit_notes.  */
1765
1766 static rtx
1767 move_insn (insn, last)
1768      rtx insn, last;
1769 {
1770   rtx retval = NULL;
1771
1772   /* Now move the first non SCHED_GROUP_P insn.  */
1773   move_insn1 (insn, last);
1774
1775   /* If this is the first call to reemit_notes, then record
1776      its return value.  */
1777   if (retval == NULL_RTX)
1778     retval = reemit_notes (insn, insn);
1779   else
1780     reemit_notes (insn, insn);
1781
1782   SCHED_GROUP_P (insn) = 0;
1783
1784   return retval;
1785 }
1786
1787 /* The following structure describe an entry of the stack of choices.  */
1788 struct choice_entry
1789 {
1790   /* Ordinal number of the issued insn in the ready queue.  */
1791   int index;
1792   /* The number of the rest insns whose issues we should try.  */
1793   int rest;
1794   /* The number of issued essential insns.  */
1795   int n;
1796   /* State after issuing the insn.  */
1797   state_t state;
1798 };
1799
1800 /* The following array is used to implement a stack of choices used in
1801    function max_issue.  */
1802 static struct choice_entry *choice_stack;
1803
1804 /* The following variable value is number of essential insns issued on
1805    the current cycle.  An insn is essential one if it changes the
1806    processors state.  */
1807 static int cycle_issued_insns;
1808
1809 /* The following function returns maximal (or close to maximal) number
1810    of insns which can be issued on the same cycle and one of which
1811    insns is insns with the best rank (the first insn in READY).  To
1812    make this function tries different samples of ready insns.  READY
1813    is current queue `ready'.  Global array READY_TRY reflects what
1814    insns are already issued in this try.  INDEX will contain index
1815    of the best insn in READY.  The following function is used only for
1816    first cycle multipass scheduling.  */
1817 static int
1818 max_issue (ready, index)
1819   struct ready_list *ready;
1820   int *index;
1821 {
1822   int n, i, all, n_ready, lookahead, best, delay;
1823   struct choice_entry *top;
1824   rtx insn;
1825
1826   lookahead = (*targetm.sched.first_cycle_multipass_dfa_lookahead) ();
1827   best = 0;
1828   memcpy (choice_stack->state, curr_state, dfa_state_size);
1829   top = choice_stack;
1830   top->rest = lookahead;
1831   top->n = 0;
1832   n_ready = ready->n_ready;
1833   for (all = i = 0; i < n_ready; i++)
1834     if (!ready_try [i])
1835       all++;
1836   i = 0;
1837   for (;;)
1838     {
1839       if (top->rest == 0 || i >= n_ready)
1840         {
1841           if (top == choice_stack)
1842             break;
1843           if (best < top - choice_stack && ready_try [0])
1844             {
1845               best = top - choice_stack;
1846               *index = choice_stack [1].index;
1847               if (top->n == issue_rate - cycle_issued_insns || best == all)
1848                 break;
1849             }
1850           i = top->index;
1851           ready_try [i] = 0;
1852           top--;
1853           memcpy (curr_state, top->state, dfa_state_size);
1854         }
1855       else if (!ready_try [i])
1856         {
1857           insn = ready_element (ready, i);
1858           delay = state_transition (curr_state, insn);
1859           if (delay < 0)
1860             {
1861               if (state_dead_lock_p (curr_state))
1862                 top->rest = 0;
1863               else
1864                 top->rest--;
1865               n = top->n;
1866               if (memcmp (top->state, curr_state, dfa_state_size) != 0)
1867                 n++;
1868               top++;
1869               top->rest = lookahead;
1870               top->index = i;
1871               top->n = n;
1872               memcpy (top->state, curr_state, dfa_state_size);
1873               ready_try [i] = 1;
1874               i = -1;
1875             }
1876         }
1877       i++;
1878     }
1879   while (top != choice_stack)
1880     {
1881       ready_try [top->index] = 0;
1882       top--;
1883     }
1884   memcpy (curr_state, choice_stack->state, dfa_state_size);
1885   return best;
1886 }
1887
1888 /* The following function chooses insn from READY and modifies
1889    *N_READY and READY.  The following function is used only for first
1890    cycle multipass scheduling.  */
1891
1892 static rtx
1893 choose_ready (ready)
1894      struct ready_list *ready;
1895 {
1896   if (!targetm.sched.first_cycle_multipass_dfa_lookahead
1897       || (*targetm.sched.first_cycle_multipass_dfa_lookahead) () <= 0)
1898     return ready_remove_first (ready);
1899   else
1900     {
1901       /* Try to choose the better insn.  */
1902       int index, i;
1903       rtx insn;
1904
1905       insn = ready_element (ready, 0);
1906       if (INSN_CODE (insn) < 0)
1907         return ready_remove_first (ready);
1908       for (i = 1; i < ready->n_ready; i++)
1909         {
1910           insn = ready_element (ready, i);
1911           ready_try [i]
1912             = (INSN_CODE (insn) < 0
1913                || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard
1914                    && !(*targetm.sched.first_cycle_multipass_dfa_lookahead_guard) (insn)));
1915         }
1916       if (max_issue (ready, &index) == 0)
1917         return ready_remove_first (ready);
1918       else
1919         return ready_remove (ready, index);
1920     }
1921 }
1922
1923 /* Called from backends from targetm.sched.reorder to emit stuff into
1924    the instruction stream.  */
1925
1926 rtx
1927 sched_emit_insn (pat)
1928      rtx pat;
1929 {
1930   rtx insn = emit_insn_after (pat, last_scheduled_insn);
1931   last_scheduled_insn = insn;
1932   return insn;
1933 }
1934
1935 /* Use forward list scheduling to rearrange insns of block B in region RGN,
1936    possibly bringing insns from subsequent blocks in the same region.  */
1937
1938 void
1939 schedule_block (b, rgn_n_insns)
1940      int b;
1941      int rgn_n_insns;
1942 {
1943   struct ready_list ready;
1944   int i, first_cycle_insn_p;
1945   int can_issue_more;
1946   state_t temp_state = NULL;  /* It is used for multipass scheduling.  */
1947   int sort_p;
1948
1949   /* Head/tail info for this block.  */
1950   rtx prev_head = current_sched_info->prev_head;
1951   rtx next_tail = current_sched_info->next_tail;
1952   rtx head = NEXT_INSN (prev_head);
1953   rtx tail = PREV_INSN (next_tail);
1954
1955   /* We used to have code to avoid getting parameters moved from hard
1956      argument registers into pseudos.
1957
1958      However, it was removed when it proved to be of marginal benefit
1959      and caused problems because schedule_block and compute_forward_dependences
1960      had different notions of what the "head" insn was.  */
1961
1962   if (head == tail && (! INSN_P (head)))
1963     abort ();
1964
1965   /* Debug info.  */
1966   if (sched_verbose)
1967     {
1968       fprintf (sched_dump, ";;   ======================================================\n");
1969       fprintf (sched_dump,
1970                ";;   -- basic block %d from %d to %d -- %s reload\n",
1971                b, INSN_UID (head), INSN_UID (tail),
1972                (reload_completed ? "after" : "before"));
1973       fprintf (sched_dump, ";;   ======================================================\n");
1974       fprintf (sched_dump, "\n");
1975
1976       visualize_alloc ();
1977       init_block_visualization ();
1978     }
1979
1980   if (targetm.sched.use_dfa_pipeline_interface
1981       && (*targetm.sched.use_dfa_pipeline_interface) ())
1982     state_reset (curr_state);
1983   else
1984     clear_units ();
1985
1986   /* Allocate the ready list.  */
1987   ready.veclen = rgn_n_insns + 1 + issue_rate;
1988   ready.first = ready.veclen - 1;
1989   ready.vec = (rtx *) xmalloc (ready.veclen * sizeof (rtx));
1990   ready.n_ready = 0;
1991
1992   if (targetm.sched.use_dfa_pipeline_interface
1993       && (*targetm.sched.use_dfa_pipeline_interface) ())
1994     {
1995       /* It is used for first cycle multipass scheduling.  */
1996       temp_state = alloca (dfa_state_size);
1997       ready_try = (char *) xmalloc ((rgn_n_insns + 1) * sizeof (char));
1998       memset (ready_try, 0, (rgn_n_insns + 1) * sizeof (char));
1999       choice_stack
2000         = (struct choice_entry *) xmalloc ((rgn_n_insns + 1)
2001                                            * sizeof (struct choice_entry));
2002       for (i = 0; i <= rgn_n_insns; i++)
2003         choice_stack[i].state = (state_t) xmalloc (dfa_state_size);
2004     }
2005
2006   (*current_sched_info->init_ready_list) (&ready);
2007
2008   if (targetm.sched.md_init)
2009     (*targetm.sched.md_init) (sched_dump, sched_verbose, ready.veclen);
2010
2011   /* We start inserting insns after PREV_HEAD.  */
2012   last_scheduled_insn = prev_head;
2013
2014   /* Initialize INSN_QUEUE.  Q_SIZE is the total number of insns in the
2015      queue.  */
2016   q_ptr = 0;
2017   q_size = 0;
2018
2019   if (!targetm.sched.use_dfa_pipeline_interface
2020       || !(*targetm.sched.use_dfa_pipeline_interface) ())
2021     max_insn_queue_index_macro_value = INSN_QUEUE_SIZE - 1;
2022   else
2023     max_insn_queue_index_macro_value = max_insn_queue_index;
2024
2025   insn_queue = (rtx *) alloca ((MAX_INSN_QUEUE_INDEX + 1) * sizeof (rtx));
2026   memset ((char *) insn_queue, 0, (MAX_INSN_QUEUE_INDEX + 1) * sizeof (rtx));
2027   last_clock_var = -1;
2028
2029   /* Start just before the beginning of time.  */
2030   clock_var = -1;
2031
2032   sort_p = TRUE;
2033   /* Loop until all the insns in BB are scheduled.  */
2034   while ((*current_sched_info->schedule_more_p) ())
2035     {
2036       clock_var++;
2037
2038       advance_one_cycle ();
2039
2040       /* Add to the ready list all pending insns that can be issued now.
2041          If there are no ready insns, increment clock until one
2042          is ready and add all pending insns at that point to the ready
2043          list.  */
2044       queue_to_ready (&ready);
2045
2046       if (ready.n_ready == 0)
2047         abort ();
2048
2049       if (sched_verbose >= 2)
2050         {
2051           fprintf (sched_dump, ";;\t\tReady list after queue_to_ready:  ");
2052           debug_ready_list (&ready);
2053         }
2054
2055       if (sort_p)
2056         {
2057           /* Sort the ready list based on priority.  */
2058           ready_sort (&ready);
2059           
2060           if (sched_verbose >= 2)
2061             {
2062               fprintf (sched_dump, ";;\t\tReady list after ready_sort:  ");
2063               debug_ready_list (&ready);
2064             }
2065         }
2066
2067       /* Allow the target to reorder the list, typically for
2068          better instruction bundling.  */
2069       if (targetm.sched.reorder)
2070         can_issue_more =
2071           (*targetm.sched.reorder) (sched_dump, sched_verbose,
2072                                     ready_lastpos (&ready),
2073                                     &ready.n_ready, clock_var);
2074       else
2075         can_issue_more = issue_rate;
2076
2077       first_cycle_insn_p = 1;
2078       cycle_issued_insns = 0;
2079       for (;;)
2080         {
2081           rtx insn;
2082           int cost;
2083
2084           if (sched_verbose >= 2)
2085             {
2086               fprintf (sched_dump, ";;\tReady list (t =%3d):  ",
2087                        clock_var);
2088               debug_ready_list (&ready);
2089             }
2090
2091           if (!targetm.sched.use_dfa_pipeline_interface
2092               || !(*targetm.sched.use_dfa_pipeline_interface) ())
2093             {
2094               if (ready.n_ready == 0 || !can_issue_more
2095                   || !(*current_sched_info->schedule_more_p) ())
2096                 break;
2097               insn = choose_ready (&ready);
2098               cost = actual_hazard (insn_unit (insn), insn, clock_var, 0);
2099             }
2100           else
2101             {
2102               if (ready.n_ready == 0 || !can_issue_more
2103                   || state_dead_lock_p (curr_state)
2104                   || !(*current_sched_info->schedule_more_p) ())
2105                 break;
2106               
2107               /* Select and remove the insn from the ready list.  */
2108               if (sort_p)
2109                 insn = choose_ready (&ready);
2110               else
2111                 insn = ready_remove_first (&ready);
2112               
2113               if (targetm.sched.dfa_new_cycle
2114                   && (*targetm.sched.dfa_new_cycle) (sched_dump, sched_verbose,
2115                                                      insn, last_clock_var,
2116                                                      clock_var, &sort_p))
2117                 {
2118                   ready_add (&ready, insn);
2119                   break;
2120                 }
2121             
2122               sort_p = TRUE;
2123               memcpy (temp_state, curr_state, dfa_state_size);
2124               if (recog_memoized (insn) < 0)
2125                 {
2126                   if (!first_cycle_insn_p
2127                       && (GET_CODE (PATTERN (insn)) == ASM_INPUT
2128                           || asm_noperands (PATTERN (insn)) >= 0))
2129                     /* This is asm insn which is tryed to be issued on the
2130                        cycle not first.  Issue it on the next cycle.  */
2131                     cost = 1;
2132                   else
2133                     /* A USE insn, or something else we don't need to
2134                        understand.  We can't pass these directly to
2135                        state_transition because it will trigger a
2136                        fatal error for unrecognizable insns.  */
2137                     cost = 0;
2138                 }
2139               else
2140                 {
2141                   cost = state_transition (temp_state, insn);
2142
2143                   if (targetm.sched.first_cycle_multipass_dfa_lookahead
2144                       && targetm.sched.dfa_bubble)
2145                     {
2146                       if (cost == 0)
2147                         {
2148                           int j;
2149                           rtx bubble;
2150                           
2151                           for (j = 0;
2152                                (bubble = (*targetm.sched.dfa_bubble) (j))
2153                                  != NULL_RTX;
2154                                j++)
2155                             {
2156                               memcpy (temp_state, curr_state, dfa_state_size);
2157                               
2158                               if (state_transition (temp_state, bubble) < 0
2159                                   && state_transition (temp_state, insn) < 0)
2160                                 break;
2161                             }
2162                           
2163                           if (bubble != NULL_RTX)
2164                             {
2165                               if (insert_schedule_bubbles_p)
2166                                 {
2167                                   rtx copy;
2168                                   
2169                                   copy = copy_rtx (PATTERN (bubble));
2170                                   emit_insn_after (copy, last_scheduled_insn);
2171                                   last_scheduled_insn
2172                                     = NEXT_INSN (last_scheduled_insn);
2173                                   INSN_CODE (last_scheduled_insn)
2174                                     = INSN_CODE (bubble);
2175                                   
2176                                   /* Annotate the same for the first insns
2177                                      scheduling by using mode.  */
2178                                   PUT_MODE (last_scheduled_insn,
2179                                             (clock_var > last_clock_var
2180                                              ? clock_var - last_clock_var
2181                                              : VOIDmode));
2182                                   last_clock_var = clock_var;
2183                                   
2184                                   if (sched_verbose >= 2)
2185                                     {
2186                                       fprintf (sched_dump,
2187                                                ";;\t\t--> scheduling bubble insn <<<%d>>>:reservation ",
2188                                                INSN_UID (last_scheduled_insn));
2189                                       
2190                                       if (recog_memoized (last_scheduled_insn)
2191                                           < 0)
2192                                         fprintf (sched_dump, "nothing");
2193                                       else
2194                                         print_reservation
2195                                           (sched_dump, last_scheduled_insn);
2196                                       
2197                                       fprintf (sched_dump, "\n");
2198                                     }
2199                                 }
2200                               cost = -1;
2201                             }
2202                         }
2203                     }
2204
2205                   if (cost < 0)
2206                     cost = 0;
2207                   else if (cost == 0)
2208                     cost = 1;
2209                 }
2210             }
2211
2212
2213           if (cost >= 1)
2214             {
2215               queue_insn (insn, cost);
2216               continue;
2217             }
2218
2219           if (! (*current_sched_info->can_schedule_ready_p) (insn))
2220             goto next;
2221
2222           last_scheduled_insn = move_insn (insn, last_scheduled_insn);
2223
2224           if (targetm.sched.use_dfa_pipeline_interface
2225               && (*targetm.sched.use_dfa_pipeline_interface) ())
2226             {
2227               if (memcmp (curr_state, temp_state, dfa_state_size) != 0)
2228                 cycle_issued_insns++;
2229               memcpy (curr_state, temp_state, dfa_state_size);
2230             }
2231             
2232           if (targetm.sched.variable_issue)
2233             can_issue_more =
2234               (*targetm.sched.variable_issue) (sched_dump, sched_verbose,
2235                                                insn, can_issue_more);
2236           /* A naked CLOBBER or USE generates no instruction, so do
2237              not count them against the issue rate.  */
2238           else if (GET_CODE (PATTERN (insn)) != USE
2239                    && GET_CODE (PATTERN (insn)) != CLOBBER)
2240             can_issue_more--;
2241
2242           schedule_insn (insn, &ready, clock_var);
2243
2244         next:
2245           first_cycle_insn_p = 0;
2246
2247           /* Sort the ready list based on priority.  This must be
2248              redone here, as schedule_insn may have readied additional
2249              insns that will not be sorted correctly. */
2250           if (ready.n_ready > 0)
2251             ready_sort (&ready);
2252
2253           if (targetm.sched.reorder2)
2254             {
2255               can_issue_more =
2256                 (*targetm.sched.reorder2) (sched_dump, sched_verbose,
2257                                            ready.n_ready
2258                                            ? ready_lastpos (&ready) : NULL,
2259                                            &ready.n_ready, clock_var);
2260             }
2261         }
2262
2263       if ((!targetm.sched.use_dfa_pipeline_interface
2264            || !(*targetm.sched.use_dfa_pipeline_interface) ())
2265           && sched_verbose)
2266         /* Debug info.  */
2267         visualize_scheduled_insns (clock_var);
2268     }
2269
2270   if (targetm.sched.md_finish)
2271     (*targetm.sched.md_finish) (sched_dump, sched_verbose);
2272
2273   /* Debug info.  */
2274   if (sched_verbose)
2275     {
2276       fprintf (sched_dump, ";;\tReady list (final):  ");
2277       debug_ready_list (&ready);
2278       if (!targetm.sched.use_dfa_pipeline_interface
2279           || !(*targetm.sched.use_dfa_pipeline_interface) ())
2280         print_block_visualization ("");
2281     }
2282
2283   /* Sanity check -- queue must be empty now.  Meaningless if region has
2284      multiple bbs.  */
2285   if (current_sched_info->queue_must_finish_empty && q_size != 0)
2286       abort ();
2287
2288   /* Update head/tail boundaries.  */
2289   head = NEXT_INSN (prev_head);
2290   tail = last_scheduled_insn;
2291
2292   if (!reload_completed)
2293     {
2294       rtx insn, link, next;
2295       
2296       /* INSN_TICK (minimum clock tick at which the insn becomes
2297          ready) may be not correct for the insn in the subsequent
2298          blocks of the region.  We should use a correct value of
2299          `clock_var' or modify INSN_TICK.  It is better to keep
2300          clock_var value equal to 0 at the start of a basic block.
2301          Therefore we modify INSN_TICK here.  */
2302       for (insn = head; insn != tail; insn = NEXT_INSN (insn))
2303         if (INSN_P (insn))
2304           {
2305             for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
2306               {
2307                 next = XEXP (link, 0);
2308                 INSN_TICK (next) -= clock_var;
2309               }
2310           }
2311     }
2312
2313   /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
2314      previously found among the insns.  Insert them at the beginning
2315      of the insns.  */
2316   if (note_list != 0)
2317     {
2318       rtx note_head = note_list;
2319
2320       while (PREV_INSN (note_head))
2321         {
2322           note_head = PREV_INSN (note_head);
2323         }
2324
2325       PREV_INSN (note_head) = PREV_INSN (head);
2326       NEXT_INSN (PREV_INSN (head)) = note_head;
2327       PREV_INSN (head) = note_list;
2328       NEXT_INSN (note_list) = head;
2329       head = note_head;
2330     }
2331
2332   /* Debugging.  */
2333   if (sched_verbose)
2334     {
2335       fprintf (sched_dump, ";;   total time = %d\n;;   new head = %d\n",
2336                clock_var, INSN_UID (head));
2337       fprintf (sched_dump, ";;   new tail = %d\n\n",
2338                INSN_UID (tail));
2339       visualize_free ();
2340     }
2341
2342   current_sched_info->head = head;
2343   current_sched_info->tail = tail;
2344
2345   free (ready.vec);
2346
2347   if (targetm.sched.use_dfa_pipeline_interface
2348       && (*targetm.sched.use_dfa_pipeline_interface) ())
2349     {
2350       free (ready_try);
2351       for (i = 0; i <= rgn_n_insns; i++)
2352         free (choice_stack [i].state);
2353       free (choice_stack);
2354     }
2355 }
2356 \f
2357 /* Set_priorities: compute priority of each insn in the block.  */
2358
2359 int
2360 set_priorities (head, tail)
2361      rtx head, tail;
2362 {
2363   rtx insn;
2364   int n_insn;
2365
2366   rtx prev_head;
2367
2368   prev_head = PREV_INSN (head);
2369
2370   if (head == tail && (! INSN_P (head)))
2371     return 0;
2372
2373   n_insn = 0;
2374   for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
2375     {
2376       if (GET_CODE (insn) == NOTE)
2377         continue;
2378
2379       n_insn++;
2380       (void) priority (insn);
2381     }
2382
2383   return n_insn;
2384 }
2385
2386 /* Initialize some global state for the scheduler.  DUMP_FILE is to be used
2387    for debugging output.  */
2388
2389 void
2390 sched_init (dump_file)
2391      FILE *dump_file;
2392 {
2393   int luid;
2394   basic_block b;
2395   rtx insn;
2396   int i;
2397
2398   /* Disable speculative loads in their presence if cc0 defined.  */
2399 #ifdef HAVE_cc0
2400   flag_schedule_speculative_load = 0;
2401 #endif
2402
2403   /* Set dump and sched_verbose for the desired debugging output.  If no
2404      dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
2405      For -fsched-verbose=N, N>=10, print everything to stderr.  */
2406   sched_verbose = sched_verbose_param;
2407   if (sched_verbose_param == 0 && dump_file)
2408     sched_verbose = 1;
2409   sched_dump = ((sched_verbose_param >= 10 || !dump_file)
2410                 ? stderr : dump_file);
2411
2412   /* Initialize issue_rate.  */
2413   if (targetm.sched.issue_rate)
2414     issue_rate = (*targetm.sched.issue_rate) ();
2415   else
2416     issue_rate = 1;
2417
2418   /* We use LUID 0 for the fake insn (UID 0) which holds dependencies for
2419      pseudos which do not cross calls.  */
2420   old_max_uid = get_max_uid () + 1;
2421
2422   h_i_d = (struct haifa_insn_data *) xcalloc (old_max_uid, sizeof (*h_i_d));
2423
2424   for (i = 0; i < old_max_uid; i++)
2425     h_i_d [i].cost = -1;
2426
2427   if (targetm.sched.use_dfa_pipeline_interface
2428       && (*targetm.sched.use_dfa_pipeline_interface) ())
2429     {
2430       if (targetm.sched.init_dfa_pre_cycle_insn)
2431         (*targetm.sched.init_dfa_pre_cycle_insn) ();
2432       
2433       if (targetm.sched.init_dfa_post_cycle_insn)
2434         (*targetm.sched.init_dfa_post_cycle_insn) ();
2435       
2436       if (targetm.sched.first_cycle_multipass_dfa_lookahead
2437           && targetm.sched.init_dfa_bubbles)
2438         (*targetm.sched.init_dfa_bubbles) ();
2439       
2440       dfa_start ();
2441       dfa_state_size = state_size ();
2442       curr_state = xmalloc (dfa_state_size);
2443     }
2444
2445   h_i_d[0].luid = 0;
2446   luid = 1;
2447   FOR_EACH_BB (b)
2448     for (insn = b->head;; insn = NEXT_INSN (insn))
2449       {
2450         INSN_LUID (insn) = luid;
2451
2452         /* Increment the next luid, unless this is a note.  We don't
2453            really need separate IDs for notes and we don't want to
2454            schedule differently depending on whether or not there are
2455            line-number notes, i.e., depending on whether or not we're
2456            generating debugging information.  */
2457         if (GET_CODE (insn) != NOTE)
2458           ++luid;
2459
2460         if (insn == b->end)
2461           break;
2462       }
2463
2464   init_dependency_caches (luid);
2465
2466   init_alias_analysis ();
2467
2468   if (write_symbols != NO_DEBUG)
2469     {
2470       rtx line;
2471
2472       line_note_head = (rtx *) xcalloc (last_basic_block, sizeof (rtx));
2473
2474       /* Save-line-note-head:
2475          Determine the line-number at the start of each basic block.
2476          This must be computed and saved now, because after a basic block's
2477          predecessor has been scheduled, it is impossible to accurately
2478          determine the correct line number for the first insn of the block.  */
2479
2480       FOR_EACH_BB (b)
2481         {
2482           for (line = b->head; line; line = PREV_INSN (line))
2483             if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
2484               {
2485                 line_note_head[b->index] = line;
2486                 break;
2487               }
2488           /* Do a forward search as well, since we won't get to see the first
2489              notes in a basic block.  */
2490           for (line = b->head; line; line = NEXT_INSN (line))
2491             {
2492               if (INSN_P (line))
2493                 break;
2494               if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
2495                 line_note_head[b->index] = line;
2496             }
2497         }
2498     }
2499
2500   if ((!targetm.sched.use_dfa_pipeline_interface
2501        || !(*targetm.sched.use_dfa_pipeline_interface) ())
2502       && sched_verbose)
2503     /* Find units used in this function, for visualization.  */
2504     init_target_units ();
2505
2506   /* ??? Add a NOTE after the last insn of the last basic block.  It is not
2507      known why this is done.  */
2508
2509   insn = EXIT_BLOCK_PTR->prev_bb->end;
2510   if (NEXT_INSN (insn) == 0
2511       || (GET_CODE (insn) != NOTE
2512           && GET_CODE (insn) != CODE_LABEL
2513           /* Don't emit a NOTE if it would end up before a BARRIER.  */
2514           && GET_CODE (NEXT_INSN (insn)) != BARRIER))
2515     {
2516       emit_note_after (NOTE_INSN_DELETED, EXIT_BLOCK_PTR->prev_bb->end);
2517       /* Make insn to appear outside BB.  */
2518       EXIT_BLOCK_PTR->prev_bb->end = PREV_INSN (EXIT_BLOCK_PTR->prev_bb->end);
2519     }
2520
2521   /* Compute INSN_REG_WEIGHT for all blocks.  We must do this before
2522      removing death notes.  */
2523   FOR_EACH_BB_REVERSE (b)
2524     find_insn_reg_weight (b->index);
2525 }
2526
2527 /* Free global data used during insn scheduling.  */
2528
2529 void
2530 sched_finish ()
2531 {
2532   free (h_i_d);
2533
2534   if (targetm.sched.use_dfa_pipeline_interface
2535       && (*targetm.sched.use_dfa_pipeline_interface) ())
2536     {
2537       free (curr_state);
2538       dfa_finish ();
2539     }
2540   free_dependency_caches ();
2541   end_alias_analysis ();
2542   if (write_symbols != NO_DEBUG)
2543     free (line_note_head);
2544 }
2545 #endif /* INSN_SCHEDULING */