OSDN Git Service

* config/i386/i386.c (ix86_expand_prologue): Use
[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, 2003 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 (const char *param, const char *val)
193 {
194   if (!strcmp (param, "verbose"))
195     sched_verbose_param = atoi (val);
196   else
197     warning ("fix_sched_param: unknown param: %s", param);
198 }
199
200 struct haifa_insn_data *h_i_d;
201
202 #define LINE_NOTE(INSN)         (h_i_d[INSN_UID (INSN)].line_note)
203 #define INSN_TICK(INSN)         (h_i_d[INSN_UID (INSN)].tick)
204
205 /* Vector indexed by basic block number giving the starting line-number
206    for each basic block.  */
207 static rtx *line_note_head;
208
209 /* List of important notes we must keep around.  This is a pointer to the
210    last element in the list.  */
211 static rtx note_list;
212
213 /* Queues, etc.  */
214
215 /* An instruction is ready to be scheduled when all insns preceding it
216    have already been scheduled.  It is important to ensure that all
217    insns which use its result will not be executed until its result
218    has been computed.  An insn is maintained in one of four structures:
219
220    (P) the "Pending" set of insns which cannot be scheduled until
221    their dependencies have been satisfied.
222    (Q) the "Queued" set of insns that can be scheduled when sufficient
223    time has passed.
224    (R) the "Ready" list of unscheduled, uncommitted insns.
225    (S) the "Scheduled" list of insns.
226
227    Initially, all insns are either "Pending" or "Ready" depending on
228    whether their dependencies are satisfied.
229
230    Insns move from the "Ready" list to the "Scheduled" list as they
231    are committed to the schedule.  As this occurs, the insns in the
232    "Pending" list have their dependencies satisfied and move to either
233    the "Ready" list or the "Queued" set depending on whether
234    sufficient time has passed to make them ready.  As time passes,
235    insns move from the "Queued" set to the "Ready" list.  Insns may
236    move from the "Ready" list to the "Queued" set if they are blocked
237    due to a function unit conflict.
238
239    The "Pending" list (P) are the insns in the INSN_DEPEND of the unscheduled
240    insns, i.e., those that are ready, queued, and pending.
241    The "Queued" set (Q) is implemented by the variable `insn_queue'.
242    The "Ready" list (R) is implemented by the variables `ready' and
243    `n_ready'.
244    The "Scheduled" list (S) is the new insn chain built by this pass.
245
246    The transition (R->S) is implemented in the scheduling loop in
247    `schedule_block' when the best insn to schedule is chosen.
248    The transition (R->Q) is implemented in `queue_insn' when an
249    insn is found to have a function unit conflict with the already
250    committed insns.
251    The transitions (P->R and P->Q) are implemented in `schedule_insn' as
252    insns move from the ready list to the scheduled list.
253    The transition (Q->R) is implemented in 'queue_to_insn' as time
254    passes or stalls are introduced.  */
255
256 /* Implement a circular buffer to delay instructions until sufficient
257    time has passed.  For the old pipeline description interface,
258    INSN_QUEUE_SIZE is a power of two larger than MAX_BLOCKAGE and
259    MAX_READY_COST computed by genattr.c.  For the new pipeline
260    description interface, MAX_INSN_QUEUE_INDEX is a power of two minus
261    one which is larger than maximal time of instruction execution
262    computed by genattr.c on the base maximal time of functional unit
263    reservations and geting a result.  This is the longest time an
264    insn may be queued.  */
265
266 #define MAX_INSN_QUEUE_INDEX max_insn_queue_index_macro_value
267
268 static rtx *insn_queue;
269 static int q_ptr = 0;
270 static int q_size = 0;
271 #define NEXT_Q(X) (((X)+1) & MAX_INSN_QUEUE_INDEX)
272 #define NEXT_Q_AFTER(X, C) (((X)+C) & MAX_INSN_QUEUE_INDEX)
273
274 /* The following variable defines value for macro
275    MAX_INSN_QUEUE_INDEX.  */
276 static int max_insn_queue_index_macro_value;
277
278 /* The following variable value refers for all current and future
279    reservations of the processor units.  */
280 state_t curr_state;
281
282 /* The following variable value is size of memory representing all
283    current and future reservations of the processor units.  It is used
284    only by DFA based scheduler.  */
285 static size_t dfa_state_size;
286
287 /* The following array is used to find the best insn from ready when
288    the automaton pipeline interface is used.  */
289 static char *ready_try;
290
291 /* Describe the ready list of the scheduler.
292    VEC holds space enough for all insns in the current region.  VECLEN
293    says how many exactly.
294    FIRST is the index of the element with the highest priority; i.e. the
295    last one in the ready list, since elements are ordered by ascending
296    priority.
297    N_READY determines how many insns are on the ready list.  */
298
299 struct ready_list
300 {
301   rtx *vec;
302   int veclen;
303   int first;
304   int n_ready;
305 };
306
307 static int may_trap_exp (rtx, int);
308
309 /* Nonzero iff the address is comprised from at most 1 register.  */
310 #define CONST_BASED_ADDRESS_P(x)                        \
311   (GET_CODE (x) == REG                                  \
312    || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS   \
313         || (GET_CODE (x) == LO_SUM))                    \
314        && (CONSTANT_P (XEXP (x, 0))                     \
315            || CONSTANT_P (XEXP (x, 1)))))
316
317 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
318    as found by analyzing insn's expression.  */
319
320 static int
321 may_trap_exp (rtx x, int is_store)
322 {
323   enum rtx_code code;
324
325   if (x == 0)
326     return TRAP_FREE;
327   code = GET_CODE (x);
328   if (is_store)
329     {
330       if (code == MEM && may_trap_p (x))
331         return TRAP_RISKY;
332       else
333         return TRAP_FREE;
334     }
335   if (code == MEM)
336     {
337       /* The insn uses memory:  a volatile load.  */
338       if (MEM_VOLATILE_P (x))
339         return IRISKY;
340       /* An exception-free load.  */
341       if (!may_trap_p (x))
342         return IFREE;
343       /* A load with 1 base register, to be further checked.  */
344       if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
345         return PFREE_CANDIDATE;
346       /* No info on the load, to be further checked.  */
347       return PRISKY_CANDIDATE;
348     }
349   else
350     {
351       const char *fmt;
352       int i, insn_class = TRAP_FREE;
353
354       /* Neither store nor load, check if it may cause a trap.  */
355       if (may_trap_p (x))
356         return TRAP_RISKY;
357       /* Recursive step: walk the insn...  */
358       fmt = GET_RTX_FORMAT (code);
359       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
360         {
361           if (fmt[i] == 'e')
362             {
363               int tmp_class = may_trap_exp (XEXP (x, i), is_store);
364               insn_class = WORST_CLASS (insn_class, tmp_class);
365             }
366           else if (fmt[i] == 'E')
367             {
368               int j;
369               for (j = 0; j < XVECLEN (x, i); j++)
370                 {
371                   int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
372                   insn_class = WORST_CLASS (insn_class, tmp_class);
373                   if (insn_class == TRAP_RISKY || insn_class == IRISKY)
374                     break;
375                 }
376             }
377           if (insn_class == TRAP_RISKY || insn_class == IRISKY)
378             break;
379         }
380       return insn_class;
381     }
382 }
383
384 /* Classifies insn for the purpose of verifying that it can be
385    moved speculatively, by examining it's patterns, returning:
386    TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
387    TRAP_FREE: non-load insn.
388    IFREE: load from a globally safe location.
389    IRISKY: volatile load.
390    PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
391    being either PFREE or PRISKY.  */
392
393 int
394 haifa_classify_insn (rtx insn)
395 {
396   rtx pat = PATTERN (insn);
397   int tmp_class = TRAP_FREE;
398   int insn_class = TRAP_FREE;
399   enum rtx_code code;
400
401   if (GET_CODE (pat) == PARALLEL)
402     {
403       int i, len = XVECLEN (pat, 0);
404
405       for (i = len - 1; i >= 0; i--)
406         {
407           code = GET_CODE (XVECEXP (pat, 0, i));
408           switch (code)
409             {
410             case CLOBBER:
411               /* Test if it is a 'store'.  */
412               tmp_class = may_trap_exp (XEXP (XVECEXP (pat, 0, i), 0), 1);
413               break;
414             case SET:
415               /* Test if it is a store.  */
416               tmp_class = may_trap_exp (SET_DEST (XVECEXP (pat, 0, i)), 1);
417               if (tmp_class == TRAP_RISKY)
418                 break;
419               /* Test if it is a load.  */
420               tmp_class
421                 = WORST_CLASS (tmp_class,
422                                may_trap_exp (SET_SRC (XVECEXP (pat, 0, i)),
423                                              0));
424               break;
425             case COND_EXEC:
426             case TRAP_IF:
427               tmp_class = TRAP_RISKY;
428               break;
429             default:
430               ;
431             }
432           insn_class = WORST_CLASS (insn_class, tmp_class);
433           if (insn_class == TRAP_RISKY || insn_class == IRISKY)
434             break;
435         }
436     }
437   else
438     {
439       code = GET_CODE (pat);
440       switch (code)
441         {
442         case CLOBBER:
443           /* Test if it is a 'store'.  */
444           tmp_class = may_trap_exp (XEXP (pat, 0), 1);
445           break;
446         case SET:
447           /* Test if it is a store.  */
448           tmp_class = may_trap_exp (SET_DEST (pat), 1);
449           if (tmp_class == TRAP_RISKY)
450             break;
451           /* Test if it is a load.  */
452           tmp_class =
453             WORST_CLASS (tmp_class,
454                          may_trap_exp (SET_SRC (pat), 0));
455           break;
456         case COND_EXEC:
457         case TRAP_IF:
458           tmp_class = TRAP_RISKY;
459           break;
460         default:;
461         }
462       insn_class = tmp_class;
463     }
464
465   return insn_class;
466 }
467
468 /* Forward declarations.  */
469
470 /* The scheduler using only DFA description should never use the
471    following five functions:  */
472 static unsigned int blockage_range (int, rtx);
473 static void clear_units (void);
474 static void schedule_unit (int, rtx, int);
475 static int actual_hazard (int, rtx, int, int);
476 static int potential_hazard (int, rtx, int);
477
478 static int priority (rtx);
479 static int rank_for_schedule (const void *, const void *);
480 static void swap_sort (rtx *, int);
481 static void queue_insn (rtx, int);
482 static int schedule_insn (rtx, struct ready_list *, int);
483 static int find_set_reg_weight (rtx);
484 static void find_insn_reg_weight (int);
485 static void adjust_priority (rtx);
486 static void advance_one_cycle (void);
487
488 /* Notes handling mechanism:
489    =========================
490    Generally, NOTES are saved before scheduling and restored after scheduling.
491    The scheduler distinguishes between three types of notes:
492
493    (1) LINE_NUMBER notes, generated and used for debugging.  Here,
494    before scheduling a region, a pointer to the LINE_NUMBER note is
495    added to the insn following it (in save_line_notes()), and the note
496    is removed (in rm_line_notes() and unlink_line_notes()).  After
497    scheduling the region, this pointer is used for regeneration of
498    the LINE_NUMBER note (in restore_line_notes()).
499
500    (2) LOOP_BEGIN, LOOP_END, SETJMP, EHREGION_BEG, EHREGION_END notes:
501    Before scheduling a region, a pointer to the note is added to the insn
502    that follows or precedes it.  (This happens as part of the data dependence
503    computation).  After scheduling an insn, the pointer contained in it is
504    used for regenerating the corresponding note (in reemit_notes).
505
506    (3) All other notes (e.g. INSN_DELETED):  Before scheduling a block,
507    these notes are put in a list (in rm_other_notes() and
508    unlink_other_notes ()).  After scheduling the block, these notes are
509    inserted at the beginning of the block (in schedule_block()).  */
510
511 static rtx unlink_other_notes (rtx, rtx);
512 static rtx unlink_line_notes (rtx, rtx);
513 static rtx reemit_notes (rtx, rtx);
514
515 static rtx *ready_lastpos (struct ready_list *);
516 static void ready_sort (struct ready_list *);
517 static rtx ready_remove_first (struct ready_list *);
518
519 static void queue_to_ready (struct ready_list *);
520 static int early_queue_to_ready (state_t, struct ready_list *);
521
522 static void debug_ready_list (struct ready_list *);
523
524 static rtx move_insn1 (rtx, rtx);
525 static rtx move_insn (rtx, rtx);
526
527 /* The following functions are used to implement multi-pass scheduling
528    on the first cycle.  It is used only for DFA based scheduler.  */
529 static rtx ready_element (struct ready_list *, int);
530 static rtx ready_remove (struct ready_list *, int);
531 static int max_issue (struct ready_list *, int *);
532
533 static rtx choose_ready (struct ready_list *);
534
535 #endif /* INSN_SCHEDULING */
536 \f
537 /* Point to state used for the current scheduling pass.  */
538 struct sched_info *current_sched_info;
539 \f
540 #ifndef INSN_SCHEDULING
541 void
542 schedule_insns (FILE *dump_file ATTRIBUTE_UNUSED)
543 {
544 }
545 #else
546
547 /* Pointer to the last instruction scheduled.  Used by rank_for_schedule,
548    so that insns independent of the last scheduled insn will be preferred
549    over dependent instructions.  */
550
551 static rtx last_scheduled_insn;
552
553 /* Compute the function units used by INSN.  This caches the value
554    returned by function_units_used.  A function unit is encoded as the
555    unit number if the value is non-negative and the complement of a
556    mask if the value is negative.  A function unit index is the
557    non-negative encoding.  The scheduler using only DFA description
558    should never use the following function.  */
559
560 HAIFA_INLINE int
561 insn_unit (rtx insn)
562 {
563   int unit = INSN_UNIT (insn);
564
565   if (unit == 0)
566     {
567       recog_memoized (insn);
568
569       /* A USE insn, or something else we don't need to understand.
570          We can't pass these directly to function_units_used because it will
571          trigger a fatal error for unrecognizable insns.  */
572       if (INSN_CODE (insn) < 0)
573         unit = -1;
574       else
575         {
576           unit = function_units_used (insn);
577           /* Increment non-negative values so we can cache zero.  */
578           if (unit >= 0)
579             unit++;
580         }
581       /* We only cache 16 bits of the result, so if the value is out of
582          range, don't cache it.  */
583       if (FUNCTION_UNITS_SIZE < HOST_BITS_PER_SHORT
584           || unit >= 0
585           || (unit & ~((1 << (HOST_BITS_PER_SHORT - 1)) - 1)) == 0)
586         INSN_UNIT (insn) = unit;
587     }
588   return (unit > 0 ? unit - 1 : unit);
589 }
590
591 /* Compute the blockage range for executing INSN on UNIT.  This caches
592    the value returned by the blockage_range_function for the unit.
593    These values are encoded in an int where the upper half gives the
594    minimum value and the lower half gives the maximum value.  The
595    scheduler using only DFA description should never use the following
596    function.  */
597
598 HAIFA_INLINE static unsigned int
599 blockage_range (int unit, rtx insn)
600 {
601   unsigned int blockage = INSN_BLOCKAGE (insn);
602   unsigned int range;
603
604   if ((int) UNIT_BLOCKED (blockage) != unit + 1)
605     {
606       range = function_units[unit].blockage_range_function (insn);
607       /* We only cache the blockage range for one unit and then only if
608          the values fit.  */
609       if (HOST_BITS_PER_INT >= UNIT_BITS + 2 * BLOCKAGE_BITS)
610         INSN_BLOCKAGE (insn) = ENCODE_BLOCKAGE (unit + 1, range);
611     }
612   else
613     range = BLOCKAGE_RANGE (blockage);
614
615   return range;
616 }
617
618 /* A vector indexed by function unit instance giving the last insn to
619    use the unit.  The value of the function unit instance index for
620    unit U instance I is (U + I * FUNCTION_UNITS_SIZE).  The scheduler
621    using only DFA description should never use the following variable.  */
622 #if FUNCTION_UNITS_SIZE
623 static rtx unit_last_insn[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
624 #else
625 static rtx unit_last_insn[1];
626 #endif
627
628 /* A vector indexed by function unit instance giving the minimum time
629    when the unit will unblock based on the maximum blockage cost.  The
630    scheduler using only DFA description should never use the following
631    variable.  */
632 #if FUNCTION_UNITS_SIZE
633 static int unit_tick[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
634 #else
635 static int unit_tick[1];
636 #endif
637
638 /* A vector indexed by function unit number giving the number of insns
639    that remain to use the unit.  The scheduler using only DFA
640    description should never use the following variable.  */
641 #if FUNCTION_UNITS_SIZE
642 static int unit_n_insns[FUNCTION_UNITS_SIZE];
643 #else
644 static int unit_n_insns[1];
645 #endif
646
647 /* Access the unit_last_insn array.  Used by the visualization code.
648    The scheduler using only DFA description should never use the
649    following function.  */
650
651 rtx
652 get_unit_last_insn (int instance)
653 {
654   return unit_last_insn[instance];
655 }
656
657 /* Reset the function unit state to the null state.  */
658
659 static void
660 clear_units (void)
661 {
662   memset (unit_last_insn, 0, sizeof (unit_last_insn));
663   memset (unit_tick, 0, sizeof (unit_tick));
664   memset (unit_n_insns, 0, sizeof (unit_n_insns));
665 }
666
667 /* Return the issue-delay of an insn.  The scheduler using only DFA
668    description should never use the following function.  */
669
670 HAIFA_INLINE int
671 insn_issue_delay (rtx insn)
672 {
673   int i, delay = 0;
674   int unit = insn_unit (insn);
675
676   /* Efficiency note: in fact, we are working 'hard' to compute a
677      value that was available in md file, and is not available in
678      function_units[] structure.  It would be nice to have this
679      value there, too.  */
680   if (unit >= 0)
681     {
682       if (function_units[unit].blockage_range_function &&
683           function_units[unit].blockage_function)
684         delay = function_units[unit].blockage_function (insn, insn);
685     }
686   else
687     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
688       if ((unit & 1) != 0 && function_units[i].blockage_range_function
689           && function_units[i].blockage_function)
690         delay = MAX (delay, function_units[i].blockage_function (insn, insn));
691
692   return delay;
693 }
694
695 /* Return the actual hazard cost of executing INSN on the unit UNIT,
696    instance INSTANCE at time CLOCK if the previous actual hazard cost
697    was COST.  The scheduler using only DFA description should never
698    use the following function.  */
699
700 HAIFA_INLINE int
701 actual_hazard_this_instance (int unit, int instance, rtx insn, int clock, int cost)
702 {
703   int tick = unit_tick[instance]; /* Issue time of the last issued insn.  */
704
705   if (tick - clock > cost)
706     {
707       /* The scheduler is operating forward, so unit's last insn is the
708          executing insn and INSN is the candidate insn.  We want a
709          more exact measure of the blockage if we execute INSN at CLOCK
710          given when we committed the execution of the unit's last insn.
711
712          The blockage value is given by either the unit's max blockage
713          constant, blockage range function, or blockage function.  Use
714          the most exact form for the given unit.  */
715
716       if (function_units[unit].blockage_range_function)
717         {
718           if (function_units[unit].blockage_function)
719             tick += (function_units[unit].blockage_function
720                      (unit_last_insn[instance], insn)
721                      - function_units[unit].max_blockage);
722           else
723             tick += ((int) MAX_BLOCKAGE_COST (blockage_range (unit, insn))
724                      - function_units[unit].max_blockage);
725         }
726       if (tick - clock > cost)
727         cost = tick - clock;
728     }
729   return cost;
730 }
731
732 /* Record INSN as having begun execution on the units encoded by UNIT
733    at time CLOCK.  The scheduler using only DFA description should
734    never use the following function.  */
735
736 HAIFA_INLINE static void
737 schedule_unit (int unit, rtx insn, int clock)
738 {
739   int i;
740
741   if (unit >= 0)
742     {
743       int instance = unit;
744 #if MAX_MULTIPLICITY > 1
745       /* Find the first free instance of the function unit and use that
746          one.  We assume that one is free.  */
747       for (i = function_units[unit].multiplicity - 1; i > 0; i--)
748         {
749           if (!actual_hazard_this_instance (unit, instance, insn, clock, 0))
750             break;
751           instance += FUNCTION_UNITS_SIZE;
752         }
753 #endif
754       unit_last_insn[instance] = insn;
755       unit_tick[instance] = (clock + function_units[unit].max_blockage);
756     }
757   else
758     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
759       if ((unit & 1) != 0)
760         schedule_unit (i, insn, clock);
761 }
762
763 /* Return the actual hazard cost of executing INSN on the units
764    encoded by UNIT at time CLOCK if the previous actual hazard cost
765    was COST.  The scheduler using only DFA description should never
766    use the following function.  */
767
768 HAIFA_INLINE static int
769 actual_hazard (int unit, rtx insn, int clock, int cost)
770 {
771   int i;
772
773   if (unit >= 0)
774     {
775       /* Find the instance of the function unit with the minimum hazard.  */
776       int instance = unit;
777       int best_cost = actual_hazard_this_instance (unit, instance, insn,
778                                                    clock, cost);
779 #if MAX_MULTIPLICITY > 1
780       int this_cost;
781
782       if (best_cost > cost)
783         {
784           for (i = function_units[unit].multiplicity - 1; i > 0; i--)
785             {
786               instance += FUNCTION_UNITS_SIZE;
787               this_cost = actual_hazard_this_instance (unit, instance, insn,
788                                                        clock, cost);
789               if (this_cost < best_cost)
790                 {
791                   best_cost = this_cost;
792                   if (this_cost <= cost)
793                     break;
794                 }
795             }
796         }
797 #endif
798       cost = MAX (cost, best_cost);
799     }
800   else
801     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
802       if ((unit & 1) != 0)
803         cost = actual_hazard (i, insn, clock, cost);
804
805   return cost;
806 }
807
808 /* Return the potential hazard cost of executing an instruction on the
809    units encoded by UNIT if the previous potential hazard cost was
810    COST.  An insn with a large blockage time is chosen in preference
811    to one with a smaller time; an insn that uses a unit that is more
812    likely to be used is chosen in preference to one with a unit that
813    is less used.  We are trying to minimize a subsequent actual
814    hazard.  The scheduler using only DFA description should never use
815    the following function.  */
816
817 HAIFA_INLINE static int
818 potential_hazard (int unit, rtx insn, int cost)
819 {
820   int i, ncost;
821   unsigned int minb, maxb;
822
823   if (unit >= 0)
824     {
825       minb = maxb = function_units[unit].max_blockage;
826       if (maxb > 1)
827         {
828           if (function_units[unit].blockage_range_function)
829             {
830               maxb = minb = blockage_range (unit, insn);
831               maxb = MAX_BLOCKAGE_COST (maxb);
832               minb = MIN_BLOCKAGE_COST (minb);
833             }
834
835           if (maxb > 1)
836             {
837               /* Make the number of instructions left dominate.  Make the
838                  minimum delay dominate the maximum delay.  If all these
839                  are the same, use the unit number to add an arbitrary
840                  ordering.  Other terms can be added.  */
841               ncost = minb * 0x40 + maxb;
842               ncost *= (unit_n_insns[unit] - 1) * 0x1000 + unit;
843               if (ncost > cost)
844                 cost = ncost;
845             }
846         }
847     }
848   else
849     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
850       if ((unit & 1) != 0)
851         cost = potential_hazard (i, insn, cost);
852
853   return cost;
854 }
855
856 /* Compute cost of executing INSN given the dependence LINK on the insn USED.
857    This is the number of cycles between instruction issue and
858    instruction results.  */
859
860 HAIFA_INLINE int
861 insn_cost (rtx insn, rtx link, rtx used)
862 {
863   int cost = INSN_COST (insn);
864
865   if (cost < 0)
866     {
867       /* A USE insn, or something else we don't need to
868          understand.  We can't pass these directly to
869          result_ready_cost or insn_default_latency because it will
870          trigger a fatal error for unrecognizable insns.  */
871       if (recog_memoized (insn) < 0)
872         {
873           INSN_COST (insn) = 0;
874           return 0;
875         }
876       else
877         {
878           if (targetm.sched.use_dfa_pipeline_interface
879               && (*targetm.sched.use_dfa_pipeline_interface) ())
880             cost = insn_default_latency (insn);
881           else
882             cost = result_ready_cost (insn);
883
884           if (cost < 0)
885             cost = 0;
886
887           INSN_COST (insn) = cost;
888         }
889     }
890
891   /* In this case estimate cost without caring how insn is used.  */
892   if (link == 0 || used == 0)
893     return cost;
894
895   /* A USE insn should never require the value used to be computed.
896      This allows the computation of a function's result and parameter
897      values to overlap the return and call.  */
898   if (recog_memoized (used) < 0)
899     cost = 0;
900   else
901     {
902       if (targetm.sched.use_dfa_pipeline_interface
903           && (*targetm.sched.use_dfa_pipeline_interface) ())
904         {
905           if (INSN_CODE (insn) >= 0)
906             {
907               if (REG_NOTE_KIND (link) == REG_DEP_ANTI)
908                 cost = 0;
909               else if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
910                 {
911                   cost = (insn_default_latency (insn)
912                           - insn_default_latency (used));
913                   if (cost <= 0)
914                     cost = 1;
915                 }
916               else if (bypass_p (insn))
917                 cost = insn_latency (insn, used);
918             }
919         }
920
921       if (targetm.sched.adjust_cost)
922         cost = (*targetm.sched.adjust_cost) (used, link, insn, cost);
923
924       if (cost < 0)
925         cost = 0;
926     }
927
928   return cost;
929 }
930
931 /* Compute the priority number for INSN.  */
932
933 static int
934 priority (rtx insn)
935 {
936   rtx link;
937
938   if (! INSN_P (insn))
939     return 0;
940
941   if (! INSN_PRIORITY_KNOWN (insn))
942     {
943       int this_priority = 0;
944
945       if (INSN_DEPEND (insn) == 0)
946         this_priority = insn_cost (insn, 0, 0);
947       else
948         {
949           for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
950             {
951               rtx next;
952               int next_priority;
953
954               if (RTX_INTEGRATED_P (link))
955                 continue;
956
957               next = XEXP (link, 0);
958
959               /* Critical path is meaningful in block boundaries only.  */
960               if (! (*current_sched_info->contributes_to_priority) (next, insn))
961                 continue;
962
963               next_priority = insn_cost (insn, link, next) + priority (next);
964               if (next_priority > this_priority)
965                 this_priority = next_priority;
966             }
967         }
968       INSN_PRIORITY (insn) = this_priority;
969       INSN_PRIORITY_KNOWN (insn) = 1;
970     }
971
972   return INSN_PRIORITY (insn);
973 }
974 \f
975 /* Macros and functions for keeping the priority queue sorted, and
976    dealing with queueing and dequeueing of instructions.  */
977
978 #define SCHED_SORT(READY, N_READY)                                   \
979 do { if ((N_READY) == 2)                                             \
980        swap_sort (READY, N_READY);                                   \
981      else if ((N_READY) > 2)                                         \
982          qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); }  \
983 while (0)
984
985 /* Returns a positive value if x is preferred; returns a negative value if
986    y is preferred.  Should never return 0, since that will make the sort
987    unstable.  */
988
989 static int
990 rank_for_schedule (const void *x, const void *y)
991 {
992   rtx tmp = *(const rtx *) y;
993   rtx tmp2 = *(const rtx *) x;
994   rtx link;
995   int tmp_class, tmp2_class, depend_count1, depend_count2;
996   int val, priority_val, weight_val, info_val;
997
998   /* The insn in a schedule group should be issued the first.  */
999   if (SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
1000     return SCHED_GROUP_P (tmp2) ? 1 : -1;
1001
1002   /* Prefer insn with higher priority.  */
1003   priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
1004
1005   if (priority_val)
1006     return priority_val;
1007
1008   /* Prefer an insn with smaller contribution to registers-pressure.  */
1009   if (!reload_completed &&
1010       (weight_val = INSN_REG_WEIGHT (tmp) - INSN_REG_WEIGHT (tmp2)))
1011     return weight_val;
1012
1013   info_val = (*current_sched_info->rank) (tmp, tmp2);
1014   if (info_val)
1015     return info_val;
1016
1017   /* Compare insns based on their relation to the last-scheduled-insn.  */
1018   if (last_scheduled_insn)
1019     {
1020       /* Classify the instructions into three classes:
1021          1) Data dependent on last schedule insn.
1022          2) Anti/Output dependent on last scheduled insn.
1023          3) Independent of last scheduled insn, or has latency of one.
1024          Choose the insn from the highest numbered class if different.  */
1025       link = find_insn_list (tmp, INSN_DEPEND (last_scheduled_insn));
1026       if (link == 0 || insn_cost (last_scheduled_insn, link, tmp) == 1)
1027         tmp_class = 3;
1028       else if (REG_NOTE_KIND (link) == 0)       /* Data dependence.  */
1029         tmp_class = 1;
1030       else
1031         tmp_class = 2;
1032
1033       link = find_insn_list (tmp2, INSN_DEPEND (last_scheduled_insn));
1034       if (link == 0 || insn_cost (last_scheduled_insn, link, tmp2) == 1)
1035         tmp2_class = 3;
1036       else if (REG_NOTE_KIND (link) == 0)       /* Data dependence.  */
1037         tmp2_class = 1;
1038       else
1039         tmp2_class = 2;
1040
1041       if ((val = tmp2_class - tmp_class))
1042         return val;
1043     }
1044
1045   /* Prefer the insn which has more later insns that depend on it.
1046      This gives the scheduler more freedom when scheduling later
1047      instructions at the expense of added register pressure.  */
1048   depend_count1 = 0;
1049   for (link = INSN_DEPEND (tmp); link; link = XEXP (link, 1))
1050     depend_count1++;
1051
1052   depend_count2 = 0;
1053   for (link = INSN_DEPEND (tmp2); link; link = XEXP (link, 1))
1054     depend_count2++;
1055
1056   val = depend_count2 - depend_count1;
1057   if (val)
1058     return val;
1059
1060   /* If insns are equally good, sort by INSN_LUID (original insn order),
1061      so that we make the sort stable.  This minimizes instruction movement,
1062      thus minimizing sched's effect on debugging and cross-jumping.  */
1063   return INSN_LUID (tmp) - INSN_LUID (tmp2);
1064 }
1065
1066 /* Resort the array A in which only element at index N may be out of order.  */
1067
1068 HAIFA_INLINE static void
1069 swap_sort (rtx *a, int n)
1070 {
1071   rtx insn = a[n - 1];
1072   int i = n - 2;
1073
1074   while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
1075     {
1076       a[i + 1] = a[i];
1077       i -= 1;
1078     }
1079   a[i + 1] = insn;
1080 }
1081
1082 /* Add INSN to the insn queue so that it can be executed at least
1083    N_CYCLES after the currently executing insn.  Preserve insns
1084    chain for debugging purposes.  */
1085
1086 HAIFA_INLINE static void
1087 queue_insn (rtx insn, int n_cycles)
1088 {
1089   int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
1090   rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
1091   insn_queue[next_q] = link;
1092   q_size += 1;
1093
1094   if (sched_verbose >= 2)
1095     {
1096       fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
1097                (*current_sched_info->print_insn) (insn, 0));
1098
1099       fprintf (sched_dump, "queued for %d cycles.\n", n_cycles);
1100     }
1101 }
1102
1103 /* Return a pointer to the bottom of the ready list, i.e. the insn
1104    with the lowest priority.  */
1105
1106 HAIFA_INLINE static rtx *
1107 ready_lastpos (struct ready_list *ready)
1108 {
1109   if (ready->n_ready == 0)
1110     abort ();
1111   return ready->vec + ready->first - ready->n_ready + 1;
1112 }
1113
1114 /* Add an element INSN to the ready list so that it ends up with the lowest
1115    priority.  */
1116
1117 HAIFA_INLINE void
1118 ready_add (struct ready_list *ready, rtx insn)
1119 {
1120   if (ready->first == ready->n_ready)
1121     {
1122       memmove (ready->vec + ready->veclen - ready->n_ready,
1123                ready_lastpos (ready),
1124                ready->n_ready * sizeof (rtx));
1125       ready->first = ready->veclen - 1;
1126     }
1127   ready->vec[ready->first - ready->n_ready] = insn;
1128   ready->n_ready++;
1129 }
1130
1131 /* Remove the element with the highest priority from the ready list and
1132    return it.  */
1133
1134 HAIFA_INLINE static rtx
1135 ready_remove_first (struct ready_list *ready)
1136 {
1137   rtx t;
1138   if (ready->n_ready == 0)
1139     abort ();
1140   t = ready->vec[ready->first--];
1141   ready->n_ready--;
1142   /* If the queue becomes empty, reset it.  */
1143   if (ready->n_ready == 0)
1144     ready->first = ready->veclen - 1;
1145   return t;
1146 }
1147
1148 /* The following code implements multi-pass scheduling for the first
1149    cycle.  In other words, we will try to choose ready insn which
1150    permits to start maximum number of insns on the same cycle.  */
1151
1152 /* Return a pointer to the element INDEX from the ready.  INDEX for
1153    insn with the highest priority is 0, and the lowest priority has
1154    N_READY - 1.  */
1155
1156 HAIFA_INLINE static rtx
1157 ready_element (struct ready_list *ready, int index)
1158 {
1159 #ifdef ENABLE_CHECKING
1160   if (ready->n_ready == 0 || index >= ready->n_ready)
1161     abort ();
1162 #endif
1163   return ready->vec[ready->first - index];
1164 }
1165
1166 /* Remove the element INDEX from the ready list and return it.  INDEX
1167    for insn with the highest priority is 0, and the lowest priority
1168    has N_READY - 1.  */
1169
1170 HAIFA_INLINE static rtx
1171 ready_remove (struct ready_list *ready, int index)
1172 {
1173   rtx t;
1174   int i;
1175
1176   if (index == 0)
1177     return ready_remove_first (ready);
1178   if (ready->n_ready == 0 || index >= ready->n_ready)
1179     abort ();
1180   t = ready->vec[ready->first - index];
1181   ready->n_ready--;
1182   for (i = index; i < ready->n_ready; i++)
1183     ready->vec[ready->first - i] = ready->vec[ready->first - i - 1];
1184   return t;
1185 }
1186
1187
1188 /* Sort the ready list READY by ascending priority, using the SCHED_SORT
1189    macro.  */
1190
1191 HAIFA_INLINE static void
1192 ready_sort (struct ready_list *ready)
1193 {
1194   rtx *first = ready_lastpos (ready);
1195   SCHED_SORT (first, ready->n_ready);
1196 }
1197
1198 /* PREV is an insn that is ready to execute.  Adjust its priority if that
1199    will help shorten or lengthen register lifetimes as appropriate.  Also
1200    provide a hook for the target to tweek itself.  */
1201
1202 HAIFA_INLINE static void
1203 adjust_priority (rtx prev)
1204 {
1205   /* ??? There used to be code here to try and estimate how an insn
1206      affected register lifetimes, but it did it by looking at REG_DEAD
1207      notes, which we removed in schedule_region.  Nor did it try to
1208      take into account register pressure or anything useful like that.
1209
1210      Revisit when we have a machine model to work with and not before.  */
1211
1212   if (targetm.sched.adjust_priority)
1213     INSN_PRIORITY (prev) =
1214       (*targetm.sched.adjust_priority) (prev, INSN_PRIORITY (prev));
1215 }
1216
1217 /* Advance time on one cycle.  */
1218 HAIFA_INLINE static void
1219 advance_one_cycle (void)
1220 {
1221   if (targetm.sched.use_dfa_pipeline_interface
1222       && (*targetm.sched.use_dfa_pipeline_interface) ())
1223     {
1224       if (targetm.sched.dfa_pre_cycle_insn)
1225         state_transition (curr_state,
1226                           (*targetm.sched.dfa_pre_cycle_insn) ());
1227
1228       state_transition (curr_state, NULL);
1229
1230       if (targetm.sched.dfa_post_cycle_insn)
1231         state_transition (curr_state,
1232                           (*targetm.sched.dfa_post_cycle_insn) ());
1233     }
1234 }
1235
1236 /* Clock at which the previous instruction was issued.  */
1237 static int last_clock_var;
1238
1239 /* INSN is the "currently executing insn".  Launch each insn which was
1240    waiting on INSN.  READY is the ready list which contains the insns
1241    that are ready to fire.  CLOCK is the current cycle.  The function
1242    returns necessary cycle advance after issuing the insn (it is not
1243    zero for insns in a schedule group).  */
1244
1245 static int
1246 schedule_insn (rtx insn, struct ready_list *ready, int clock)
1247 {
1248   rtx link;
1249   int advance = 0;
1250   int unit = 0;
1251   int premature_issue = 0;
1252
1253   if (!targetm.sched.use_dfa_pipeline_interface
1254       || !(*targetm.sched.use_dfa_pipeline_interface) ())
1255     unit = insn_unit (insn);
1256
1257   if (targetm.sched.use_dfa_pipeline_interface
1258       && (*targetm.sched.use_dfa_pipeline_interface) ()
1259       && sched_verbose >= 1)
1260     {
1261       char buf[2048];
1262
1263       print_insn (buf, insn, 0);
1264       buf[40] = 0;
1265       fprintf (sched_dump, ";;\t%3i--> %-40s:", clock, buf);
1266
1267       if (recog_memoized (insn) < 0)
1268         fprintf (sched_dump, "nothing");
1269       else
1270         print_reservation (sched_dump, insn);
1271       fputc ('\n', sched_dump);
1272     }
1273   else if (sched_verbose >= 2)
1274     {
1275       fprintf (sched_dump, ";;\t\t--> scheduling insn <<<%d>>> on unit ",
1276                INSN_UID (insn));
1277       insn_print_units (insn);
1278       fputc ('\n', sched_dump);
1279     }
1280
1281   if (!targetm.sched.use_dfa_pipeline_interface
1282       || !(*targetm.sched.use_dfa_pipeline_interface) ())
1283     {
1284       if (sched_verbose && unit == -1)
1285         visualize_no_unit (insn);
1286
1287
1288       if (MAX_BLOCKAGE > 1 || issue_rate > 1 || sched_verbose)
1289         schedule_unit (unit, insn, clock);
1290
1291       if (INSN_DEPEND (insn) == 0)
1292         return 0;
1293     }
1294
1295   if (INSN_TICK (insn) > clock)
1296     {
1297       /* 'insn' has been prematurely moved from the queue to the
1298          ready list.  */
1299       premature_issue = INSN_TICK (insn) - clock;
1300     }
1301
1302   for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
1303     {
1304       rtx next = XEXP (link, 0);
1305       int cost = insn_cost (insn, link, next);
1306
1307       INSN_TICK (next) = MAX (INSN_TICK (next), clock + cost + premature_issue);
1308
1309       if ((INSN_DEP_COUNT (next) -= 1) == 0)
1310         {
1311           int effective_cost = INSN_TICK (next) - clock;
1312
1313           if (! (*current_sched_info->new_ready) (next))
1314             continue;
1315
1316           if (sched_verbose >= 2)
1317             {
1318               fprintf (sched_dump, ";;\t\tdependences resolved: insn %s ",
1319                        (*current_sched_info->print_insn) (next, 0));
1320
1321               if (effective_cost < 1)
1322                 fprintf (sched_dump, "into ready\n");
1323               else
1324                 fprintf (sched_dump, "into queue with cost=%d\n",
1325                          effective_cost);
1326             }
1327
1328           /* Adjust the priority of NEXT and either put it on the ready
1329              list or queue it.  */
1330           adjust_priority (next);
1331           if (effective_cost < 1)
1332             ready_add (ready, next);
1333           else
1334             {
1335               queue_insn (next, effective_cost);
1336
1337               if (SCHED_GROUP_P (next) && advance < effective_cost)
1338                 advance = effective_cost;
1339             }
1340         }
1341     }
1342
1343   /* Annotate the instruction with issue information -- TImode
1344      indicates that the instruction is expected not to be able
1345      to issue on the same cycle as the previous insn.  A machine
1346      may use this information to decide how the instruction should
1347      be aligned.  */
1348   if (issue_rate > 1
1349       && GET_CODE (PATTERN (insn)) != USE
1350       && GET_CODE (PATTERN (insn)) != CLOBBER)
1351     {
1352       if (reload_completed)
1353         PUT_MODE (insn, clock > last_clock_var ? TImode : VOIDmode);
1354       last_clock_var = clock;
1355     }
1356   return advance;
1357 }
1358
1359 /* Functions for handling of notes.  */
1360
1361 /* Delete notes beginning with INSN and put them in the chain
1362    of notes ended by NOTE_LIST.
1363    Returns the insn following the notes.  */
1364
1365 static rtx
1366 unlink_other_notes (rtx insn, rtx tail)
1367 {
1368   rtx prev = PREV_INSN (insn);
1369
1370   while (insn != tail && GET_CODE (insn) == NOTE)
1371     {
1372       rtx next = NEXT_INSN (insn);
1373       /* Delete the note from its current position.  */
1374       if (prev)
1375         NEXT_INSN (prev) = next;
1376       if (next)
1377         PREV_INSN (next) = prev;
1378
1379       /* See sched_analyze to see how these are handled.  */
1380       if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG
1381           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_END
1382           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK
1383           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_BEG
1384           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_END)
1385         {
1386           /* Insert the note at the end of the notes list.  */
1387           PREV_INSN (insn) = note_list;
1388           if (note_list)
1389             NEXT_INSN (note_list) = insn;
1390           note_list = insn;
1391         }
1392
1393       insn = next;
1394     }
1395   return insn;
1396 }
1397
1398 /* Delete line notes beginning with INSN. Record line-number notes so
1399    they can be reused.  Returns the insn following the notes.  */
1400
1401 static rtx
1402 unlink_line_notes (rtx insn, rtx tail)
1403 {
1404   rtx prev = PREV_INSN (insn);
1405
1406   while (insn != tail && GET_CODE (insn) == NOTE)
1407     {
1408       rtx next = NEXT_INSN (insn);
1409
1410       if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
1411         {
1412           /* Delete the note from its current position.  */
1413           if (prev)
1414             NEXT_INSN (prev) = next;
1415           if (next)
1416             PREV_INSN (next) = prev;
1417
1418           /* Record line-number notes so they can be reused.  */
1419           LINE_NOTE (insn) = insn;
1420         }
1421       else
1422         prev = insn;
1423
1424       insn = next;
1425     }
1426   return insn;
1427 }
1428
1429 /* Return the head and tail pointers of BB.  */
1430
1431 void
1432 get_block_head_tail (int b, rtx *headp, rtx *tailp)
1433 {
1434   /* HEAD and TAIL delimit the basic block being scheduled.  */
1435   rtx head = BLOCK_HEAD (b);
1436   rtx tail = BLOCK_END (b);
1437
1438   /* Don't include any notes or labels at the beginning of the
1439      basic block, or notes at the ends of basic blocks.  */
1440   while (head != tail)
1441     {
1442       if (GET_CODE (head) == NOTE)
1443         head = NEXT_INSN (head);
1444       else if (GET_CODE (tail) == NOTE)
1445         tail = PREV_INSN (tail);
1446       else if (GET_CODE (head) == CODE_LABEL)
1447         head = NEXT_INSN (head);
1448       else
1449         break;
1450     }
1451
1452   *headp = head;
1453   *tailp = tail;
1454 }
1455
1456 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ].  */
1457
1458 int
1459 no_real_insns_p (rtx head, rtx tail)
1460 {
1461   while (head != NEXT_INSN (tail))
1462     {
1463       if (GET_CODE (head) != NOTE && GET_CODE (head) != CODE_LABEL)
1464         return 0;
1465       head = NEXT_INSN (head);
1466     }
1467   return 1;
1468 }
1469
1470 /* Delete line notes from one block. Save them so they can be later restored
1471    (in restore_line_notes).  HEAD and TAIL are the boundaries of the
1472    block in which notes should be processed.  */
1473
1474 void
1475 rm_line_notes (rtx head, rtx tail)
1476 {
1477   rtx next_tail;
1478   rtx insn;
1479
1480   next_tail = NEXT_INSN (tail);
1481   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1482     {
1483       rtx prev;
1484
1485       /* Farm out notes, and maybe save them in NOTE_LIST.
1486          This is needed to keep the debugger from
1487          getting completely deranged.  */
1488       if (GET_CODE (insn) == NOTE)
1489         {
1490           prev = insn;
1491           insn = unlink_line_notes (insn, next_tail);
1492
1493           if (prev == tail)
1494             abort ();
1495           if (prev == head)
1496             abort ();
1497           if (insn == next_tail)
1498             abort ();
1499         }
1500     }
1501 }
1502
1503 /* Save line number notes for each insn in block B.  HEAD and TAIL are
1504    the boundaries of the block in which notes should be processed.  */
1505
1506 void
1507 save_line_notes (int b, rtx head, rtx tail)
1508 {
1509   rtx next_tail;
1510
1511   /* We must use the true line number for the first insn in the block
1512      that was computed and saved at the start of this pass.  We can't
1513      use the current line number, because scheduling of the previous
1514      block may have changed the current line number.  */
1515
1516   rtx line = line_note_head[b];
1517   rtx insn;
1518
1519   next_tail = NEXT_INSN (tail);
1520
1521   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1522     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1523       line = insn;
1524     else
1525       LINE_NOTE (insn) = line;
1526 }
1527
1528 /* After a block was scheduled, insert line notes into the insns list.
1529    HEAD and TAIL are the boundaries of the block in which notes should
1530    be processed.  */
1531
1532 void
1533 restore_line_notes (rtx head, rtx tail)
1534 {
1535   rtx line, note, prev, new;
1536   int added_notes = 0;
1537   rtx next_tail, insn;
1538
1539   head = head;
1540   next_tail = NEXT_INSN (tail);
1541
1542   /* Determine the current line-number.  We want to know the current
1543      line number of the first insn of the block here, in case it is
1544      different from the true line number that was saved earlier.  If
1545      different, then we need a line number note before the first insn
1546      of this block.  If it happens to be the same, then we don't want to
1547      emit another line number note here.  */
1548   for (line = head; line; line = PREV_INSN (line))
1549     if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
1550       break;
1551
1552   /* Walk the insns keeping track of the current line-number and inserting
1553      the line-number notes as needed.  */
1554   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1555     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1556       line = insn;
1557   /* This used to emit line number notes before every non-deleted note.
1558      However, this confuses a debugger, because line notes not separated
1559      by real instructions all end up at the same address.  I can find no
1560      use for line number notes before other notes, so none are emitted.  */
1561     else if (GET_CODE (insn) != NOTE
1562              && INSN_UID (insn) < old_max_uid
1563              && (note = LINE_NOTE (insn)) != 0
1564              && note != line
1565              && (line == 0
1566                  || NOTE_LINE_NUMBER (note) != NOTE_LINE_NUMBER (line)
1567                  || NOTE_SOURCE_FILE (note) != NOTE_SOURCE_FILE (line)))
1568       {
1569         line = note;
1570         prev = PREV_INSN (insn);
1571         if (LINE_NOTE (note))
1572           {
1573             /* Re-use the original line-number note.  */
1574             LINE_NOTE (note) = 0;
1575             PREV_INSN (note) = prev;
1576             NEXT_INSN (prev) = note;
1577             PREV_INSN (insn) = note;
1578             NEXT_INSN (note) = insn;
1579           }
1580         else
1581           {
1582             added_notes++;
1583             new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
1584             NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
1585             RTX_INTEGRATED_P (new) = RTX_INTEGRATED_P (note);
1586           }
1587       }
1588   if (sched_verbose && added_notes)
1589     fprintf (sched_dump, ";; added %d line-number notes\n", added_notes);
1590 }
1591
1592 /* After scheduling the function, delete redundant line notes from the
1593    insns list.  */
1594
1595 void
1596 rm_redundant_line_notes (void)
1597 {
1598   rtx line = 0;
1599   rtx insn = get_insns ();
1600   int active_insn = 0;
1601   int notes = 0;
1602
1603   /* Walk the insns deleting redundant line-number notes.  Many of these
1604      are already present.  The remainder tend to occur at basic
1605      block boundaries.  */
1606   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1607     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1608       {
1609         /* If there are no active insns following, INSN is redundant.  */
1610         if (active_insn == 0)
1611           {
1612             notes++;
1613             NOTE_SOURCE_FILE (insn) = 0;
1614             NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1615           }
1616         /* If the line number is unchanged, LINE is redundant.  */
1617         else if (line
1618                  && NOTE_LINE_NUMBER (line) == NOTE_LINE_NUMBER (insn)
1619                  && NOTE_SOURCE_FILE (line) == NOTE_SOURCE_FILE (insn))
1620           {
1621             notes++;
1622             NOTE_SOURCE_FILE (line) = 0;
1623             NOTE_LINE_NUMBER (line) = NOTE_INSN_DELETED;
1624             line = insn;
1625           }
1626         else
1627           line = insn;
1628         active_insn = 0;
1629       }
1630     else if (!((GET_CODE (insn) == NOTE
1631                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
1632                || (GET_CODE (insn) == INSN
1633                    && (GET_CODE (PATTERN (insn)) == USE
1634                        || GET_CODE (PATTERN (insn)) == CLOBBER))))
1635       active_insn++;
1636
1637   if (sched_verbose && notes)
1638     fprintf (sched_dump, ";; deleted %d line-number notes\n", notes);
1639 }
1640
1641 /* Delete notes between HEAD and TAIL and put them in the chain
1642    of notes ended by NOTE_LIST.  */
1643
1644 void
1645 rm_other_notes (rtx head, rtx tail)
1646 {
1647   rtx next_tail;
1648   rtx insn;
1649
1650   note_list = 0;
1651   if (head == tail && (! INSN_P (head)))
1652     return;
1653
1654   next_tail = NEXT_INSN (tail);
1655   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1656     {
1657       rtx prev;
1658
1659       /* Farm out notes, and maybe save them in NOTE_LIST.
1660          This is needed to keep the debugger from
1661          getting completely deranged.  */
1662       if (GET_CODE (insn) == NOTE)
1663         {
1664           prev = insn;
1665
1666           insn = unlink_other_notes (insn, next_tail);
1667
1668           if (prev == tail)
1669             abort ();
1670           if (prev == head)
1671             abort ();
1672           if (insn == next_tail)
1673             abort ();
1674         }
1675     }
1676 }
1677
1678 /* Functions for computation of registers live/usage info.  */
1679
1680 /* This function looks for a new register being defined.
1681    If the destination register is already used by the source,
1682    a new register is not needed.  */
1683
1684 static int
1685 find_set_reg_weight (rtx x)
1686 {
1687   if (GET_CODE (x) == CLOBBER
1688       && register_operand (SET_DEST (x), VOIDmode))
1689     return 1;
1690   if (GET_CODE (x) == SET
1691       && register_operand (SET_DEST (x), VOIDmode))
1692     {
1693       if (GET_CODE (SET_DEST (x)) == REG)
1694         {
1695           if (!reg_mentioned_p (SET_DEST (x), SET_SRC (x)))
1696             return 1;
1697           else
1698             return 0;
1699         }
1700       return 1;
1701     }
1702   return 0;
1703 }
1704
1705 /* Calculate INSN_REG_WEIGHT for all insns of a block.  */
1706
1707 static void
1708 find_insn_reg_weight (int b)
1709 {
1710   rtx insn, next_tail, head, tail;
1711
1712   get_block_head_tail (b, &head, &tail);
1713   next_tail = NEXT_INSN (tail);
1714
1715   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1716     {
1717       int reg_weight = 0;
1718       rtx x;
1719
1720       /* Handle register life information.  */
1721       if (! INSN_P (insn))
1722         continue;
1723
1724       /* Increment weight for each register born here.  */
1725       x = PATTERN (insn);
1726       reg_weight += find_set_reg_weight (x);
1727       if (GET_CODE (x) == PARALLEL)
1728         {
1729           int j;
1730           for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
1731             {
1732               x = XVECEXP (PATTERN (insn), 0, j);
1733               reg_weight += find_set_reg_weight (x);
1734             }
1735         }
1736       /* Decrement weight for each register that dies here.  */
1737       for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1738         {
1739           if (REG_NOTE_KIND (x) == REG_DEAD
1740               || REG_NOTE_KIND (x) == REG_UNUSED)
1741             reg_weight--;
1742         }
1743
1744       INSN_REG_WEIGHT (insn) = reg_weight;
1745     }
1746 }
1747
1748 /* Scheduling clock, modified in schedule_block() and queue_to_ready ().  */
1749 static int clock_var;
1750
1751 /* Move insns that became ready to fire from queue to ready list.  */
1752
1753 static void
1754 queue_to_ready (struct ready_list *ready)
1755 {
1756   rtx insn;
1757   rtx link;
1758
1759   q_ptr = NEXT_Q (q_ptr);
1760
1761   /* Add all pending insns that can be scheduled without stalls to the
1762      ready list.  */
1763   for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
1764     {
1765       insn = XEXP (link, 0);
1766       q_size -= 1;
1767
1768       if (sched_verbose >= 2)
1769         fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1770                  (*current_sched_info->print_insn) (insn, 0));
1771
1772       ready_add (ready, insn);
1773       if (sched_verbose >= 2)
1774         fprintf (sched_dump, "moving to ready without stalls\n");
1775     }
1776   insn_queue[q_ptr] = 0;
1777
1778   /* If there are no ready insns, stall until one is ready and add all
1779      of the pending insns at that point to the ready list.  */
1780   if (ready->n_ready == 0)
1781     {
1782       int stalls;
1783
1784       for (stalls = 1; stalls <= MAX_INSN_QUEUE_INDEX; stalls++)
1785         {
1786           if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
1787             {
1788               for (; link; link = XEXP (link, 1))
1789                 {
1790                   insn = XEXP (link, 0);
1791                   q_size -= 1;
1792
1793                   if (sched_verbose >= 2)
1794                     fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1795                              (*current_sched_info->print_insn) (insn, 0));
1796
1797                   ready_add (ready, insn);
1798                   if (sched_verbose >= 2)
1799                     fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
1800                 }
1801               insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = 0;
1802
1803               advance_one_cycle ();
1804
1805               break;
1806             }
1807
1808           advance_one_cycle ();
1809         }
1810
1811       if ((!targetm.sched.use_dfa_pipeline_interface
1812            || !(*targetm.sched.use_dfa_pipeline_interface) ())
1813           && sched_verbose && stalls)
1814         visualize_stall_cycles (stalls);
1815
1816       q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
1817       clock_var += stalls;
1818     }
1819 }
1820
1821 /* Used by early_queue_to_ready.  Determines whether it is "ok" to
1822    prematurely move INSN from the queue to the ready list.  Currently, 
1823    if a target defines the hook 'is_costly_dependence', this function 
1824    uses the hook to check whether there exist any dependences which are
1825    considered costly by the target, between INSN and other insns that 
1826    have already been scheduled.  Dependences are checked up to Y cycles
1827    back, with default Y=1; The flag -fsched-stalled-insns-dep=Y allows
1828    controlling this value. 
1829    (Other considerations could be taken into account instead (or in 
1830    addition) depending on user flags and target hooks.  */
1831
1832 static bool 
1833 ok_for_early_queue_removal (rtx insn)
1834 {
1835   int n_cycles;
1836   rtx prev_insn = last_scheduled_insn;
1837
1838   if (targetm.sched.is_costly_dependence)
1839     {
1840       for (n_cycles = flag_sched_stalled_insns_dep; n_cycles; n_cycles--)
1841         {
1842           for ( ; prev_insn; prev_insn = PREV_INSN (prev_insn))
1843             {
1844               rtx dep_link = 0;
1845               int dep_cost;
1846
1847               if (GET_CODE (prev_insn) != NOTE)
1848                 {
1849                   dep_link = find_insn_list (insn, INSN_DEPEND (prev_insn));
1850                   if (dep_link)
1851                     {
1852                       dep_cost = insn_cost (prev_insn, dep_link, insn) ;
1853                       if (targetm.sched.is_costly_dependence (prev_insn, insn, 
1854                                 dep_link, dep_cost, 
1855                                 flag_sched_stalled_insns_dep - n_cycles))
1856                         return false;
1857                     }
1858                 }
1859
1860               if (GET_MODE (prev_insn) == TImode) /* end of dispatch group */
1861                 break;
1862             }
1863
1864           if (!prev_insn) 
1865             break;
1866           prev_insn = PREV_INSN (prev_insn);     
1867         }
1868     }
1869
1870   return true;
1871 }
1872
1873
1874 /* Remove insns from the queue, before they become "ready" with respect
1875    to FU latency considerations.   */
1876
1877 static int 
1878 early_queue_to_ready (state_t state, struct ready_list *ready)
1879 {
1880   rtx insn;
1881   rtx link;
1882   rtx next_link;
1883   rtx prev_link;
1884   bool move_to_ready;
1885   int cost;
1886   state_t temp_state = alloca (dfa_state_size);
1887   int stalls;
1888   int insns_removed = 0;
1889
1890   /*
1891      Flag '-fsched-stalled-insns=X' determines the aggressiveness of this 
1892      function: 
1893
1894      X == 0: There is no limit on how many queued insns can be removed          
1895              prematurely.  (flag_sched_stalled_insns = -1).
1896
1897      X >= 1: Only X queued insns can be removed prematurely in each 
1898              invocation.  (flag_sched_stalled_insns = X).
1899
1900      Otherwise: Early queue removal is disabled.
1901          (flag_sched_stalled_insns = 0)
1902   */
1903
1904   if (! flag_sched_stalled_insns)   
1905     return 0;
1906
1907   for (stalls = 0; stalls <= MAX_INSN_QUEUE_INDEX; stalls++)
1908     {
1909       if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
1910         {
1911           if (sched_verbose > 6)
1912             fprintf (sched_dump, ";; look at index %d + %d\n", q_ptr, stalls);
1913
1914           prev_link = 0;
1915           while (link)
1916             {
1917               next_link = XEXP (link, 1);
1918               insn = XEXP (link, 0);
1919               if (insn && sched_verbose > 6)
1920                 print_rtl_single (sched_dump, insn);
1921
1922               memcpy (temp_state, state, dfa_state_size);
1923               if (recog_memoized (insn) < 0) 
1924                 /* non-negative to indicate that it's not ready
1925                    to avoid infinite Q->R->Q->R... */
1926                 cost = 0;
1927               else
1928                 cost = state_transition (temp_state, insn);
1929
1930               if (sched_verbose >= 6)
1931                 fprintf (sched_dump, "transition cost = %d\n", cost);
1932
1933               move_to_ready = false;
1934               if (cost < 0) 
1935                 {
1936                   move_to_ready = ok_for_early_queue_removal (insn);
1937                   if (move_to_ready == true)
1938                     {
1939                       /* move from Q to R */
1940                       q_size -= 1;
1941                       ready_add (ready, insn);
1942
1943                       if (prev_link)   
1944                         XEXP (prev_link, 1) = next_link;
1945                       else
1946                         insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = next_link;
1947
1948                       free_INSN_LIST_node (link);
1949
1950                       if (sched_verbose >= 2)
1951                         fprintf (sched_dump, ";;\t\tEarly Q-->Ready: insn %s\n",
1952                                  (*current_sched_info->print_insn) (insn, 0));
1953
1954                       insns_removed++;
1955                       if (insns_removed == flag_sched_stalled_insns)
1956                         /* remove only one insn from Q at a time */
1957                         return insns_removed;
1958                     }
1959                 }
1960
1961               if (move_to_ready == false)
1962                 prev_link = link;
1963
1964               link = next_link;
1965             } /* while link */
1966         } /* if link */    
1967
1968     } /* for stalls.. */
1969
1970   return insns_removed; 
1971 }
1972
1973
1974 /* Print the ready list for debugging purposes.  Callable from debugger.  */
1975
1976 static void
1977 debug_ready_list (struct ready_list *ready)
1978 {
1979   rtx *p;
1980   int i;
1981
1982   if (ready->n_ready == 0)
1983     {
1984       fprintf (sched_dump, "\n");
1985       return;
1986     }
1987
1988   p = ready_lastpos (ready);
1989   for (i = 0; i < ready->n_ready; i++)
1990     fprintf (sched_dump, "  %s", (*current_sched_info->print_insn) (p[i], 0));
1991   fprintf (sched_dump, "\n");
1992 }
1993
1994 /* move_insn1: Remove INSN from insn chain, and link it after LAST insn.  */
1995
1996 static rtx
1997 move_insn1 (rtx insn, rtx last)
1998 {
1999   NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2000   PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2001
2002   NEXT_INSN (insn) = NEXT_INSN (last);
2003   PREV_INSN (NEXT_INSN (last)) = insn;
2004
2005   NEXT_INSN (last) = insn;
2006   PREV_INSN (insn) = last;
2007
2008   return insn;
2009 }
2010
2011 /* Search INSN for REG_SAVE_NOTE note pairs for
2012    NOTE_INSN_{LOOP,EHREGION}_{BEG,END}; and convert them back into
2013    NOTEs.  The REG_SAVE_NOTE note following first one is contains the
2014    saved value for NOTE_BLOCK_NUMBER which is useful for
2015    NOTE_INSN_EH_REGION_{BEG,END} NOTEs.  LAST is the last instruction
2016    output by the instruction scheduler.  Return the new value of LAST.  */
2017
2018 static rtx
2019 reemit_notes (rtx insn, rtx last)
2020 {
2021   rtx note, retval;
2022
2023   retval = last;
2024   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2025     {
2026       if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
2027         {
2028           enum insn_note note_type = INTVAL (XEXP (note, 0));
2029
2030           last = emit_note_before (note_type, last);
2031           remove_note (insn, note);
2032           note = XEXP (note, 1);
2033           if (note_type == NOTE_INSN_EH_REGION_BEG
2034               || note_type == NOTE_INSN_EH_REGION_END)
2035             NOTE_EH_HANDLER (last) = INTVAL (XEXP (note, 0));
2036           remove_note (insn, note);
2037         }
2038     }
2039   return retval;
2040 }
2041
2042 /* Move INSN.  Reemit notes if needed.
2043
2044    Return the last insn emitted by the scheduler, which is the
2045    return value from the first call to reemit_notes.  */
2046
2047 static rtx
2048 move_insn (rtx insn, rtx last)
2049 {
2050   rtx retval = NULL;
2051
2052   move_insn1 (insn, last);
2053
2054   /* If this is the first call to reemit_notes, then record
2055      its return value.  */
2056   if (retval == NULL_RTX)
2057     retval = reemit_notes (insn, insn);
2058   else
2059     reemit_notes (insn, insn);
2060
2061   SCHED_GROUP_P (insn) = 0;
2062
2063   return retval;
2064 }
2065
2066 /* The following structure describe an entry of the stack of choices.  */
2067 struct choice_entry
2068 {
2069   /* Ordinal number of the issued insn in the ready queue.  */
2070   int index;
2071   /* The number of the rest insns whose issues we should try.  */
2072   int rest;
2073   /* The number of issued essential insns.  */
2074   int n;
2075   /* State after issuing the insn.  */
2076   state_t state;
2077 };
2078
2079 /* The following array is used to implement a stack of choices used in
2080    function max_issue.  */
2081 static struct choice_entry *choice_stack;
2082
2083 /* The following variable value is number of essential insns issued on
2084    the current cycle.  An insn is essential one if it changes the
2085    processors state.  */
2086 static int cycle_issued_insns;
2087
2088 /* The following variable value is maximal number of tries of issuing
2089    insns for the first cycle multipass insn scheduling.  We define
2090    this value as constant*(DFA_LOOKAHEAD**ISSUE_RATE).  We would not
2091    need this constraint if all real insns (with non-negative codes)
2092    had reservations because in this case the algorithm complexity is
2093    O(DFA_LOOKAHEAD**ISSUE_RATE).  Unfortunately, the dfa descriptions
2094    might be incomplete and such insn might occur.  For such
2095    descriptions, the complexity of algorithm (without the constraint)
2096    could achieve DFA_LOOKAHEAD ** N , where N is the queue length.  */
2097 static int max_lookahead_tries;
2098
2099 /* The following value is value of hook
2100    `first_cycle_multipass_dfa_lookahead' at the last call of
2101    `max_issue'.  */
2102 static int cached_first_cycle_multipass_dfa_lookahead = 0;
2103
2104 /* The following value is value of `issue_rate' at the last call of
2105    `sched_init'.  */
2106 static int cached_issue_rate = 0;
2107
2108 /* The following function returns maximal (or close to maximal) number
2109    of insns which can be issued on the same cycle and one of which
2110    insns is insns with the best rank (the first insn in READY).  To
2111    make this function tries different samples of ready insns.  READY
2112    is current queue `ready'.  Global array READY_TRY reflects what
2113    insns are already issued in this try.  INDEX will contain index
2114    of the best insn in READY.  The following function is used only for
2115    first cycle multipass scheduling.  */
2116 static int
2117 max_issue (struct ready_list *ready, int *index)
2118 {
2119   int n, i, all, n_ready, best, delay, tries_num;
2120   struct choice_entry *top;
2121   rtx insn;
2122
2123   best = 0;
2124   memcpy (choice_stack->state, curr_state, dfa_state_size);
2125   top = choice_stack;
2126   top->rest = cached_first_cycle_multipass_dfa_lookahead;
2127   top->n = 0;
2128   n_ready = ready->n_ready;
2129   for (all = i = 0; i < n_ready; i++)
2130     if (!ready_try [i])
2131       all++;
2132   i = 0;
2133   tries_num = 0;
2134   for (;;)
2135     {
2136       if (top->rest == 0 || i >= n_ready)
2137         {
2138           if (top == choice_stack)
2139             break;
2140           if (best < top - choice_stack && ready_try [0])
2141             {
2142               best = top - choice_stack;
2143               *index = choice_stack [1].index;
2144               if (top->n == issue_rate - cycle_issued_insns || best == all)
2145                 break;
2146             }
2147           i = top->index;
2148           ready_try [i] = 0;
2149           top--;
2150           memcpy (curr_state, top->state, dfa_state_size);
2151         }
2152       else if (!ready_try [i])
2153         {
2154           tries_num++;
2155           if (tries_num > max_lookahead_tries)
2156             break;
2157           insn = ready_element (ready, i);
2158           delay = state_transition (curr_state, insn);
2159           if (delay < 0)
2160             {
2161               if (state_dead_lock_p (curr_state))
2162                 top->rest = 0;
2163               else
2164                 top->rest--;
2165               n = top->n;
2166               if (memcmp (top->state, curr_state, dfa_state_size) != 0)
2167                 n++;
2168               top++;
2169               top->rest = cached_first_cycle_multipass_dfa_lookahead;
2170               top->index = i;
2171               top->n = n;
2172               memcpy (top->state, curr_state, dfa_state_size);
2173               ready_try [i] = 1;
2174               i = -1;
2175             }
2176         }
2177       i++;
2178     }
2179   while (top != choice_stack)
2180     {
2181       ready_try [top->index] = 0;
2182       top--;
2183     }
2184   memcpy (curr_state, choice_stack->state, dfa_state_size);
2185   return best;
2186 }
2187
2188 /* The following function chooses insn from READY and modifies
2189    *N_READY and READY.  The following function is used only for first
2190    cycle multipass scheduling.  */
2191
2192 static rtx
2193 choose_ready (struct ready_list *ready)
2194 {
2195   int lookahead = 0;
2196
2197   if (targetm.sched.first_cycle_multipass_dfa_lookahead)
2198     lookahead = (*targetm.sched.first_cycle_multipass_dfa_lookahead) ();
2199   if (lookahead <= 0 || SCHED_GROUP_P (ready_element (ready, 0)))
2200     return ready_remove_first (ready);
2201   else
2202     {
2203       /* Try to choose the better insn.  */
2204       int index, i;
2205       rtx insn;
2206
2207       if (cached_first_cycle_multipass_dfa_lookahead != lookahead)
2208         {
2209           cached_first_cycle_multipass_dfa_lookahead = lookahead;
2210           max_lookahead_tries = 100;
2211           for (i = 0; i < issue_rate; i++)
2212             max_lookahead_tries *= lookahead;
2213         }
2214       insn = ready_element (ready, 0);
2215       if (INSN_CODE (insn) < 0)
2216         return ready_remove_first (ready);
2217       for (i = 1; i < ready->n_ready; i++)
2218         {
2219           insn = ready_element (ready, i);
2220           ready_try [i]
2221             = (INSN_CODE (insn) < 0
2222                || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard
2223                    && !(*targetm.sched.first_cycle_multipass_dfa_lookahead_guard) (insn)));
2224         }
2225       if (max_issue (ready, &index) == 0)
2226         return ready_remove_first (ready);
2227       else
2228         return ready_remove (ready, index);
2229     }
2230 }
2231
2232 /* Called from backends from targetm.sched.reorder to emit stuff into
2233    the instruction stream.  */
2234
2235 rtx
2236 sched_emit_insn (rtx pat)
2237 {
2238   rtx insn = emit_insn_after (pat, last_scheduled_insn);
2239   last_scheduled_insn = insn;
2240   return insn;
2241 }
2242
2243 /* Use forward list scheduling to rearrange insns of block B in region RGN,
2244    possibly bringing insns from subsequent blocks in the same region.  */
2245
2246 void
2247 schedule_block (int b, int rgn_n_insns)
2248 {
2249   struct ready_list ready;
2250   int i, first_cycle_insn_p;
2251   int can_issue_more;
2252   state_t temp_state = NULL;  /* It is used for multipass scheduling.  */
2253   int sort_p, advance, start_clock_var;
2254
2255   /* Head/tail info for this block.  */
2256   rtx prev_head = current_sched_info->prev_head;
2257   rtx next_tail = current_sched_info->next_tail;
2258   rtx head = NEXT_INSN (prev_head);
2259   rtx tail = PREV_INSN (next_tail);
2260
2261   /* We used to have code to avoid getting parameters moved from hard
2262      argument registers into pseudos.
2263
2264      However, it was removed when it proved to be of marginal benefit
2265      and caused problems because schedule_block and compute_forward_dependences
2266      had different notions of what the "head" insn was.  */
2267
2268   if (head == tail && (! INSN_P (head)))
2269     abort ();
2270
2271   /* Debug info.  */
2272   if (sched_verbose)
2273     {
2274       fprintf (sched_dump, ";;   ======================================================\n");
2275       fprintf (sched_dump,
2276                ";;   -- basic block %d from %d to %d -- %s reload\n",
2277                b, INSN_UID (head), INSN_UID (tail),
2278                (reload_completed ? "after" : "before"));
2279       fprintf (sched_dump, ";;   ======================================================\n");
2280       fprintf (sched_dump, "\n");
2281
2282       visualize_alloc ();
2283       init_block_visualization ();
2284     }
2285
2286   if (targetm.sched.use_dfa_pipeline_interface
2287       && (*targetm.sched.use_dfa_pipeline_interface) ())
2288     state_reset (curr_state);
2289   else
2290     clear_units ();
2291
2292   /* Allocate the ready list.  */
2293   ready.veclen = rgn_n_insns + 1 + issue_rate;
2294   ready.first = ready.veclen - 1;
2295   ready.vec = xmalloc (ready.veclen * sizeof (rtx));
2296   ready.n_ready = 0;
2297
2298   if (targetm.sched.use_dfa_pipeline_interface
2299       && (*targetm.sched.use_dfa_pipeline_interface) ())
2300     {
2301       /* It is used for first cycle multipass scheduling.  */
2302       temp_state = alloca (dfa_state_size);
2303       ready_try = xcalloc ((rgn_n_insns + 1), sizeof (char));
2304       choice_stack = xmalloc ((rgn_n_insns + 1)
2305                               * sizeof (struct choice_entry));
2306       for (i = 0; i <= rgn_n_insns; i++)
2307         choice_stack[i].state = xmalloc (dfa_state_size);
2308     }
2309
2310   (*current_sched_info->init_ready_list) (&ready);
2311
2312   if (targetm.sched.md_init)
2313     (*targetm.sched.md_init) (sched_dump, sched_verbose, ready.veclen);
2314
2315   /* We start inserting insns after PREV_HEAD.  */
2316   last_scheduled_insn = prev_head;
2317
2318   /* Initialize INSN_QUEUE.  Q_SIZE is the total number of insns in the
2319      queue.  */
2320   q_ptr = 0;
2321   q_size = 0;
2322
2323   if (!targetm.sched.use_dfa_pipeline_interface
2324       || !(*targetm.sched.use_dfa_pipeline_interface) ())
2325     max_insn_queue_index_macro_value = INSN_QUEUE_SIZE - 1;
2326   else
2327     max_insn_queue_index_macro_value = max_insn_queue_index;
2328
2329   insn_queue = alloca ((MAX_INSN_QUEUE_INDEX + 1) * sizeof (rtx));
2330   memset (insn_queue, 0, (MAX_INSN_QUEUE_INDEX + 1) * sizeof (rtx));
2331   last_clock_var = -1;
2332
2333   /* Start just before the beginning of time.  */
2334   clock_var = -1;
2335   advance = 0;
2336
2337   sort_p = TRUE;
2338   /* Loop until all the insns in BB are scheduled.  */
2339   while ((*current_sched_info->schedule_more_p) ())
2340     {
2341       do
2342         {
2343           start_clock_var = clock_var;
2344
2345           clock_var++;
2346
2347           advance_one_cycle ();
2348
2349           /* Add to the ready list all pending insns that can be issued now.
2350              If there are no ready insns, increment clock until one
2351              is ready and add all pending insns at that point to the ready
2352              list.  */
2353           queue_to_ready (&ready);
2354
2355           if (ready.n_ready == 0)
2356             abort ();
2357
2358           if (sched_verbose >= 2)
2359             {
2360               fprintf (sched_dump, ";;\t\tReady list after queue_to_ready:  ");
2361               debug_ready_list (&ready);
2362             }
2363           advance -= clock_var - start_clock_var;
2364         }
2365       while (advance > 0);
2366
2367       if (sort_p)
2368         {
2369           /* Sort the ready list based on priority.  */
2370           ready_sort (&ready);
2371
2372           if (sched_verbose >= 2)
2373             {
2374               fprintf (sched_dump, ";;\t\tReady list after ready_sort:  ");
2375               debug_ready_list (&ready);
2376             }
2377         }
2378
2379       /* Allow the target to reorder the list, typically for
2380          better instruction bundling.  */
2381       if (sort_p && targetm.sched.reorder
2382           && (ready.n_ready == 0
2383               || !SCHED_GROUP_P (ready_element (&ready, 0))))
2384         can_issue_more =
2385           (*targetm.sched.reorder) (sched_dump, sched_verbose,
2386                                     ready_lastpos (&ready),
2387                                     &ready.n_ready, clock_var);
2388       else
2389         can_issue_more = issue_rate;
2390
2391       first_cycle_insn_p = 1;
2392       cycle_issued_insns = 0;
2393       for (;;)
2394         {
2395           rtx insn;
2396           int cost;
2397
2398           if (sched_verbose >= 2)
2399             {
2400               fprintf (sched_dump, ";;\tReady list (t =%3d):  ",
2401                        clock_var);
2402               debug_ready_list (&ready);
2403             }
2404
2405           if (!targetm.sched.use_dfa_pipeline_interface
2406               || !(*targetm.sched.use_dfa_pipeline_interface) ())
2407             {
2408               if (ready.n_ready == 0 || !can_issue_more
2409                   || !(*current_sched_info->schedule_more_p) ())
2410                 break;
2411               insn = ready_remove_first (&ready);
2412               cost = actual_hazard (insn_unit (insn), insn, clock_var, 0);
2413             }
2414           else
2415             {
2416               if (ready.n_ready == 0 
2417                   && can_issue_more 
2418                   && reload_completed) 
2419                 {
2420                   /* Allow scheduling insns directly from the queue in case
2421                      there's nothing better to do (ready list is empty) but
2422                      there are still vacant dispatch slots in the current cycle.  */
2423                   if (sched_verbose >= 6)
2424                     fprintf(sched_dump,";;\t\tSecond chance\n");
2425                   memcpy (temp_state, curr_state, dfa_state_size);
2426                   if (early_queue_to_ready (temp_state, &ready))
2427                     ready_sort (&ready);
2428                 }
2429
2430               if (ready.n_ready == 0 || !can_issue_more
2431                   || state_dead_lock_p (curr_state)
2432                   || !(*current_sched_info->schedule_more_p) ())
2433                 break;
2434
2435               /* Select and remove the insn from the ready list.  */
2436               if (sort_p)
2437                 insn = choose_ready (&ready);
2438               else
2439                 insn = ready_remove_first (&ready);
2440
2441               if (targetm.sched.dfa_new_cycle
2442                   && (*targetm.sched.dfa_new_cycle) (sched_dump, sched_verbose,
2443                                                      insn, last_clock_var,
2444                                                      clock_var, &sort_p))
2445                 {
2446                   ready_add (&ready, insn);
2447                   break;
2448                 }
2449
2450               sort_p = TRUE;
2451               memcpy (temp_state, curr_state, dfa_state_size);
2452               if (recog_memoized (insn) < 0)
2453                 {
2454                   if (!first_cycle_insn_p
2455                       && (GET_CODE (PATTERN (insn)) == ASM_INPUT
2456                           || asm_noperands (PATTERN (insn)) >= 0))
2457                     /* This is asm insn which is tryed to be issued on the
2458                        cycle not first.  Issue it on the next cycle.  */
2459                     cost = 1;
2460                   else
2461                     /* A USE insn, or something else we don't need to
2462                        understand.  We can't pass these directly to
2463                        state_transition because it will trigger a
2464                        fatal error for unrecognizable insns.  */
2465                     cost = 0;
2466                 }
2467               else
2468                 {
2469                   cost = state_transition (temp_state, insn);
2470
2471                   if (targetm.sched.first_cycle_multipass_dfa_lookahead
2472                       && targetm.sched.dfa_bubble)
2473                     {
2474                       if (cost == 0)
2475                         {
2476                           int j;
2477                           rtx bubble;
2478
2479                           for (j = 0;
2480                                (bubble = (*targetm.sched.dfa_bubble) (j))
2481                                  != NULL_RTX;
2482                                j++)
2483                             {
2484                               memcpy (temp_state, curr_state, dfa_state_size);
2485
2486                               if (state_transition (temp_state, bubble) < 0
2487                                   && state_transition (temp_state, insn) < 0)
2488                                 break;
2489                             }
2490
2491                           if (bubble != NULL_RTX)
2492                             {
2493                               if (insert_schedule_bubbles_p)
2494                                 {
2495                                   rtx copy;
2496
2497                                   copy = copy_rtx (PATTERN (bubble));
2498                                   emit_insn_after (copy, last_scheduled_insn);
2499                                   last_scheduled_insn
2500                                     = NEXT_INSN (last_scheduled_insn);
2501                                   INSN_CODE (last_scheduled_insn)
2502                                     = INSN_CODE (bubble);
2503
2504                                   /* Annotate the same for the first insns
2505                                      scheduling by using mode.  */
2506                                   PUT_MODE (last_scheduled_insn,
2507                                             (clock_var > last_clock_var
2508                                              ? clock_var - last_clock_var
2509                                              : VOIDmode));
2510                                   last_clock_var = clock_var;
2511
2512                                   if (sched_verbose >= 2)
2513                                     {
2514                                       fprintf (sched_dump,
2515                                                ";;\t\t--> scheduling bubble insn <<<%d>>>:reservation ",
2516                                                INSN_UID (last_scheduled_insn));
2517
2518                                       if (recog_memoized (last_scheduled_insn)
2519                                           < 0)
2520                                         fprintf (sched_dump, "nothing");
2521                                       else
2522                                         print_reservation
2523                                           (sched_dump, last_scheduled_insn);
2524
2525                                       fprintf (sched_dump, "\n");
2526                                     }
2527                                 }
2528                               cost = -1;
2529                             }
2530                         }
2531                     }
2532
2533                   if (cost < 0)
2534                     cost = 0;
2535                   else if (cost == 0)
2536                     cost = 1;
2537                 }
2538             }
2539
2540
2541           if (cost >= 1)
2542             {
2543               queue_insn (insn, cost);
2544               continue;
2545             }
2546
2547           if (! (*current_sched_info->can_schedule_ready_p) (insn))
2548             goto next;
2549
2550           last_scheduled_insn = move_insn (insn, last_scheduled_insn);
2551
2552           if (targetm.sched.use_dfa_pipeline_interface
2553               && (*targetm.sched.use_dfa_pipeline_interface) ())
2554             {
2555               if (memcmp (curr_state, temp_state, dfa_state_size) != 0)
2556                 cycle_issued_insns++;
2557               memcpy (curr_state, temp_state, dfa_state_size);
2558             }
2559
2560           if (targetm.sched.variable_issue)
2561             can_issue_more =
2562               (*targetm.sched.variable_issue) (sched_dump, sched_verbose,
2563                                                insn, can_issue_more);
2564           /* A naked CLOBBER or USE generates no instruction, so do
2565              not count them against the issue rate.  */
2566           else if (GET_CODE (PATTERN (insn)) != USE
2567                    && GET_CODE (PATTERN (insn)) != CLOBBER)
2568             can_issue_more--;
2569
2570           advance = schedule_insn (insn, &ready, clock_var);
2571           if (advance != 0)
2572             break;
2573
2574         next:
2575           first_cycle_insn_p = 0;
2576
2577           /* Sort the ready list based on priority.  This must be
2578              redone here, as schedule_insn may have readied additional
2579              insns that will not be sorted correctly.  */
2580           if (ready.n_ready > 0)
2581             ready_sort (&ready);
2582
2583           if (targetm.sched.reorder2
2584               && (ready.n_ready == 0
2585                   || !SCHED_GROUP_P (ready_element (&ready, 0))))
2586             {
2587               can_issue_more =
2588                 (*targetm.sched.reorder2) (sched_dump, sched_verbose,
2589                                            ready.n_ready
2590                                            ? ready_lastpos (&ready) : NULL,
2591                                            &ready.n_ready, clock_var);
2592             }
2593         }
2594
2595       if ((!targetm.sched.use_dfa_pipeline_interface
2596            || !(*targetm.sched.use_dfa_pipeline_interface) ())
2597           && sched_verbose)
2598         /* Debug info.  */
2599         visualize_scheduled_insns (clock_var);
2600     }
2601
2602   if (targetm.sched.md_finish)
2603     (*targetm.sched.md_finish) (sched_dump, sched_verbose);
2604
2605   /* Debug info.  */
2606   if (sched_verbose)
2607     {
2608       fprintf (sched_dump, ";;\tReady list (final):  ");
2609       debug_ready_list (&ready);
2610       if (!targetm.sched.use_dfa_pipeline_interface
2611           || !(*targetm.sched.use_dfa_pipeline_interface) ())
2612         print_block_visualization ("");
2613     }
2614
2615   /* Sanity check -- queue must be empty now.  Meaningless if region has
2616      multiple bbs.  */
2617   if (current_sched_info->queue_must_finish_empty && q_size != 0)
2618       abort ();
2619
2620   /* Update head/tail boundaries.  */
2621   head = NEXT_INSN (prev_head);
2622   tail = last_scheduled_insn;
2623
2624   if (!reload_completed)
2625     {
2626       rtx insn, link, next;
2627
2628       /* INSN_TICK (minimum clock tick at which the insn becomes
2629          ready) may be not correct for the insn in the subsequent
2630          blocks of the region.  We should use a correct value of
2631          `clock_var' or modify INSN_TICK.  It is better to keep
2632          clock_var value equal to 0 at the start of a basic block.
2633          Therefore we modify INSN_TICK here.  */
2634       for (insn = head; insn != tail; insn = NEXT_INSN (insn))
2635         if (INSN_P (insn))
2636           {
2637             for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
2638               {
2639                 next = XEXP (link, 0);
2640                 INSN_TICK (next) -= clock_var;
2641               }
2642           }
2643     }
2644
2645   /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
2646      previously found among the insns.  Insert them at the beginning
2647      of the insns.  */
2648   if (note_list != 0)
2649     {
2650       rtx note_head = note_list;
2651
2652       while (PREV_INSN (note_head))
2653         {
2654           note_head = PREV_INSN (note_head);
2655         }
2656
2657       PREV_INSN (note_head) = PREV_INSN (head);
2658       NEXT_INSN (PREV_INSN (head)) = note_head;
2659       PREV_INSN (head) = note_list;
2660       NEXT_INSN (note_list) = head;
2661       head = note_head;
2662     }
2663
2664   /* Debugging.  */
2665   if (sched_verbose)
2666     {
2667       fprintf (sched_dump, ";;   total time = %d\n;;   new head = %d\n",
2668                clock_var, INSN_UID (head));
2669       fprintf (sched_dump, ";;   new tail = %d\n\n",
2670                INSN_UID (tail));
2671       visualize_free ();
2672     }
2673
2674   current_sched_info->head = head;
2675   current_sched_info->tail = tail;
2676
2677   free (ready.vec);
2678
2679   if (targetm.sched.use_dfa_pipeline_interface
2680       && (*targetm.sched.use_dfa_pipeline_interface) ())
2681     {
2682       free (ready_try);
2683       for (i = 0; i <= rgn_n_insns; i++)
2684         free (choice_stack [i].state);
2685       free (choice_stack);
2686     }
2687 }
2688 \f
2689 /* Set_priorities: compute priority of each insn in the block.  */
2690
2691 int
2692 set_priorities (rtx head, rtx tail)
2693 {
2694   rtx insn;
2695   int n_insn;
2696   int sched_max_insns_priority = 
2697         current_sched_info->sched_max_insns_priority;
2698   rtx prev_head;
2699
2700   prev_head = PREV_INSN (head);
2701
2702   if (head == tail && (! INSN_P (head)))
2703     return 0;
2704
2705   n_insn = 0;
2706   sched_max_insns_priority = 0;
2707   for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
2708     {
2709       if (GET_CODE (insn) == NOTE)
2710         continue;
2711
2712       n_insn++;
2713       (void) priority (insn);
2714
2715       if (INSN_PRIORITY_KNOWN (insn))
2716         sched_max_insns_priority =
2717           MAX (sched_max_insns_priority, INSN_PRIORITY (insn)); 
2718     }
2719   sched_max_insns_priority += 1;
2720   current_sched_info->sched_max_insns_priority =
2721         sched_max_insns_priority;
2722
2723   return n_insn;
2724 }
2725
2726 /* Initialize some global state for the scheduler.  DUMP_FILE is to be used
2727    for debugging output.  */
2728
2729 void
2730 sched_init (FILE *dump_file)
2731 {
2732   int luid;
2733   basic_block b;
2734   rtx insn;
2735   int i;
2736
2737   /* Disable speculative loads in their presence if cc0 defined.  */
2738 #ifdef HAVE_cc0
2739   flag_schedule_speculative_load = 0;
2740 #endif
2741
2742   /* Set dump and sched_verbose for the desired debugging output.  If no
2743      dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
2744      For -fsched-verbose=N, N>=10, print everything to stderr.  */
2745   sched_verbose = sched_verbose_param;
2746   if (sched_verbose_param == 0 && dump_file)
2747     sched_verbose = 1;
2748   sched_dump = ((sched_verbose_param >= 10 || !dump_file)
2749                 ? stderr : dump_file);
2750
2751   /* Initialize issue_rate.  */
2752   if (targetm.sched.issue_rate)
2753     issue_rate = (*targetm.sched.issue_rate) ();
2754   else
2755     issue_rate = 1;
2756
2757   if (cached_issue_rate != issue_rate)
2758     {
2759       cached_issue_rate = issue_rate;
2760       /* To invalidate max_lookahead_tries:  */
2761       cached_first_cycle_multipass_dfa_lookahead = 0;
2762     }
2763
2764   /* We use LUID 0 for the fake insn (UID 0) which holds dependencies for
2765      pseudos which do not cross calls.  */
2766   old_max_uid = get_max_uid () + 1;
2767
2768   h_i_d = xcalloc (old_max_uid, sizeof (*h_i_d));
2769
2770   for (i = 0; i < old_max_uid; i++)
2771     h_i_d [i].cost = -1;
2772
2773   if (targetm.sched.use_dfa_pipeline_interface
2774       && (*targetm.sched.use_dfa_pipeline_interface) ())
2775     {
2776       if (targetm.sched.init_dfa_pre_cycle_insn)
2777         (*targetm.sched.init_dfa_pre_cycle_insn) ();
2778
2779       if (targetm.sched.init_dfa_post_cycle_insn)
2780         (*targetm.sched.init_dfa_post_cycle_insn) ();
2781
2782       if (targetm.sched.first_cycle_multipass_dfa_lookahead
2783           && targetm.sched.init_dfa_bubbles)
2784         (*targetm.sched.init_dfa_bubbles) ();
2785
2786       dfa_start ();
2787       dfa_state_size = state_size ();
2788       curr_state = xmalloc (dfa_state_size);
2789     }
2790
2791   h_i_d[0].luid = 0;
2792   luid = 1;
2793   FOR_EACH_BB (b)
2794     for (insn = b->head;; insn = NEXT_INSN (insn))
2795       {
2796         INSN_LUID (insn) = luid;
2797
2798         /* Increment the next luid, unless this is a note.  We don't
2799            really need separate IDs for notes and we don't want to
2800            schedule differently depending on whether or not there are
2801            line-number notes, i.e., depending on whether or not we're
2802            generating debugging information.  */
2803         if (GET_CODE (insn) != NOTE)
2804           ++luid;
2805
2806         if (insn == b->end)
2807           break;
2808       }
2809
2810   init_dependency_caches (luid);
2811
2812   init_alias_analysis ();
2813
2814   if (write_symbols != NO_DEBUG)
2815     {
2816       rtx line;
2817
2818       line_note_head = xcalloc (last_basic_block, sizeof (rtx));
2819
2820       /* Save-line-note-head:
2821          Determine the line-number at the start of each basic block.
2822          This must be computed and saved now, because after a basic block's
2823          predecessor has been scheduled, it is impossible to accurately
2824          determine the correct line number for the first insn of the block.  */
2825
2826       FOR_EACH_BB (b)
2827         {
2828           for (line = b->head; line; line = PREV_INSN (line))
2829             if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
2830               {
2831                 line_note_head[b->index] = line;
2832                 break;
2833               }
2834           /* Do a forward search as well, since we won't get to see the first
2835              notes in a basic block.  */
2836           for (line = b->head; line; line = NEXT_INSN (line))
2837             {
2838               if (INSN_P (line))
2839                 break;
2840               if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
2841                 line_note_head[b->index] = line;
2842             }
2843         }
2844     }
2845
2846   if ((!targetm.sched.use_dfa_pipeline_interface
2847        || !(*targetm.sched.use_dfa_pipeline_interface) ())
2848       && sched_verbose)
2849     /* Find units used in this function, for visualization.  */
2850     init_target_units ();
2851
2852   /* ??? Add a NOTE after the last insn of the last basic block.  It is not
2853      known why this is done.  */
2854
2855   insn = EXIT_BLOCK_PTR->prev_bb->end;
2856   if (NEXT_INSN (insn) == 0
2857       || (GET_CODE (insn) != NOTE
2858           && GET_CODE (insn) != CODE_LABEL
2859           /* Don't emit a NOTE if it would end up before a BARRIER.  */
2860           && GET_CODE (NEXT_INSN (insn)) != BARRIER))
2861     {
2862       emit_note_after (NOTE_INSN_DELETED, EXIT_BLOCK_PTR->prev_bb->end);
2863       /* Make insn to appear outside BB.  */
2864       EXIT_BLOCK_PTR->prev_bb->end = PREV_INSN (EXIT_BLOCK_PTR->prev_bb->end);
2865     }
2866
2867   /* Compute INSN_REG_WEIGHT for all blocks.  We must do this before
2868      removing death notes.  */
2869   FOR_EACH_BB_REVERSE (b)
2870     find_insn_reg_weight (b->index);
2871 }
2872
2873 /* Free global data used during insn scheduling.  */
2874
2875 void
2876 sched_finish (void)
2877 {
2878   free (h_i_d);
2879
2880   if (targetm.sched.use_dfa_pipeline_interface
2881       && (*targetm.sched.use_dfa_pipeline_interface) ())
2882     {
2883       free (curr_state);
2884       dfa_finish ();
2885     }
2886   free_dependency_caches ();
2887   end_alias_analysis ();
2888   if (write_symbols != NO_DEBUG)
2889     free (line_note_head);
2890 }
2891 #endif /* INSN_SCHEDULING */