OSDN Git Service

gcc/fortran:
[pf3gnuchains/gcc-fork.git] / gcc / haifa-sched.c
1 /* Instruction scheduling pass.
2    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004, 2005, 2006, 2007 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, 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, 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    The following list shows the order in which we want to break ties
58    among insns in the ready list:
59
60    1.  choose insn with the longest path to end of bb, ties
61    broken by
62    2.  choose insn with least contribution to register pressure,
63    ties broken by
64    3.  prefer in-block upon interblock motion, ties broken by
65    4.  prefer useful upon speculative motion, ties broken by
66    5.  choose insn with largest control flow probability, ties
67    broken by
68    6.  choose insn with the least dependences upon the previously
69    scheduled insn, or finally
70    7   choose the insn which has the most insns dependent on it.
71    8.  choose insn with lowest UID.
72
73    Memory references complicate matters.  Only if we can be certain
74    that memory references are not part of the data dependency graph
75    (via true, anti, or output dependence), can we move operations past
76    memory references.  To first approximation, reads can be done
77    independently, while writes introduce dependencies.  Better
78    approximations will yield fewer dependencies.
79
80    Before reload, an extended analysis of interblock data dependences
81    is required for interblock scheduling.  This is performed in
82    compute_block_backward_dependences ().
83
84    Dependencies set up by memory references are treated in exactly the
85    same way as other dependencies, by using insn backward dependences
86    INSN_BACK_DEPS.  INSN_BACK_DEPS are translated into forward dependences
87    INSN_FORW_DEPS the purpose of forward list scheduling.
88
89    Having optimized the critical path, we may have also unduly
90    extended the lifetimes of some registers.  If an operation requires
91    that constants be loaded into registers, it is certainly desirable
92    to load those constants as early as necessary, but no earlier.
93    I.e., it will not do to load up a bunch of registers at the
94    beginning of a basic block only to use them at the end, if they
95    could be loaded later, since this may result in excessive register
96    utilization.
97
98    Note that since branches are never in basic blocks, but only end
99    basic blocks, this pass will not move branches.  But that is ok,
100    since we can use GNU's delayed branch scheduling pass to take care
101    of this case.
102
103    Also note that no further optimizations based on algebraic
104    identities are performed, so this pass would be a good one to
105    perform instruction splitting, such as breaking up a multiply
106    instruction into shifts and adds where that is profitable.
107
108    Given the memory aliasing analysis that this pass should perform,
109    it should be possible to remove redundant stores to memory, and to
110    load values from registers instead of hitting memory.
111
112    Before reload, speculative insns are moved only if a 'proof' exists
113    that no exception will be caused by this, and if no live registers
114    exist that inhibit the motion (live registers constraints are not
115    represented by data dependence edges).
116
117    This pass must update information that subsequent passes expect to
118    be correct.  Namely: reg_n_refs, reg_n_sets, reg_n_deaths,
119    reg_n_calls_crossed, and reg_live_length.  Also, BB_HEAD, BB_END.
120
121    The information in the line number notes is carefully retained by
122    this pass.  Notes that refer to the starting and ending of
123    exception regions are also carefully retained by this pass.  All
124    other NOTE insns are grouped in their same relative order at the
125    beginning of basic blocks and regions that have been scheduled.  */
126 \f
127 #include "config.h"
128 #include "system.h"
129 #include "coretypes.h"
130 #include "tm.h"
131 #include "toplev.h"
132 #include "rtl.h"
133 #include "tm_p.h"
134 #include "hard-reg-set.h"
135 #include "regs.h"
136 #include "function.h"
137 #include "flags.h"
138 #include "insn-config.h"
139 #include "insn-attr.h"
140 #include "except.h"
141 #include "toplev.h"
142 #include "recog.h"
143 #include "sched-int.h"
144 #include "target.h"
145 #include "output.h"
146 #include "params.h"
147 #include "dbgcnt.h"
148
149 #ifdef INSN_SCHEDULING
150
151 /* issue_rate is the number of insns that can be scheduled in the same
152    machine cycle.  It can be defined in the config/mach/mach.h file,
153    otherwise we set it to 1.  */
154
155 static int issue_rate;
156
157 /* sched-verbose controls the amount of debugging output the
158    scheduler prints.  It is controlled by -fsched-verbose=N:
159    N>0 and no -DSR : the output is directed to stderr.
160    N>=10 will direct the printouts to stderr (regardless of -dSR).
161    N=1: same as -dSR.
162    N=2: bb's probabilities, detailed ready list info, unit/insn info.
163    N=3: rtl at abort point, control-flow, regions info.
164    N=5: dependences info.  */
165
166 static int sched_verbose_param = 0;
167 int sched_verbose = 0;
168
169 /* Debugging file.  All printouts are sent to dump, which is always set,
170    either to stderr, or to the dump listing file (-dRS).  */
171 FILE *sched_dump = 0;
172
173 /* Highest uid before scheduling.  */
174 static int old_max_uid;
175
176 /* fix_sched_param() is called from toplev.c upon detection
177    of the -fsched-verbose=N option.  */
178
179 void
180 fix_sched_param (const char *param, const char *val)
181 {
182   if (!strcmp (param, "verbose"))
183     sched_verbose_param = atoi (val);
184   else
185     warning (0, "fix_sched_param: unknown param: %s", param);
186 }
187
188 struct haifa_insn_data *h_i_d;
189
190 #define INSN_TICK(INSN)         (h_i_d[INSN_UID (INSN)].tick)
191 #define INTER_TICK(INSN)        (h_i_d[INSN_UID (INSN)].inter_tick)
192
193 /* If INSN_TICK of an instruction is equal to INVALID_TICK,
194    then it should be recalculated from scratch.  */
195 #define INVALID_TICK (-(max_insn_queue_index + 1))
196 /* The minimal value of the INSN_TICK of an instruction.  */
197 #define MIN_TICK (-max_insn_queue_index)
198
199 /* Issue points are used to distinguish between instructions in max_issue ().
200    For now, all instructions are equally good.  */
201 #define ISSUE_POINTS(INSN) 1
202
203 /* List of important notes we must keep around.  This is a pointer to the
204    last element in the list.  */
205 static rtx note_list;
206
207 static struct spec_info_def spec_info_var;
208 /* Description of the speculative part of the scheduling.
209    If NULL - no speculation.  */
210 static spec_info_t spec_info;
211
212 /* True, if recovery block was added during scheduling of current block.
213    Used to determine, if we need to fix INSN_TICKs.  */
214 static bool added_recovery_block_p;
215
216 /* Counters of different types of speculative instructions.  */
217 static int nr_begin_data, nr_be_in_data, nr_begin_control, nr_be_in_control;
218
219 /* Array used in {unlink, restore}_bb_notes.  */
220 static rtx *bb_header = 0;
221
222 /* Number of basic_blocks.  */
223 static int old_last_basic_block;
224
225 /* Basic block after which recovery blocks will be created.  */
226 static basic_block before_recovery;
227
228 /* Queues, etc.  */
229
230 /* An instruction is ready to be scheduled when all insns preceding it
231    have already been scheduled.  It is important to ensure that all
232    insns which use its result will not be executed until its result
233    has been computed.  An insn is maintained in one of four structures:
234
235    (P) the "Pending" set of insns which cannot be scheduled until
236    their dependencies have been satisfied.
237    (Q) the "Queued" set of insns that can be scheduled when sufficient
238    time has passed.
239    (R) the "Ready" list of unscheduled, uncommitted insns.
240    (S) the "Scheduled" list of insns.
241
242    Initially, all insns are either "Pending" or "Ready" depending on
243    whether their dependencies are satisfied.
244
245    Insns move from the "Ready" list to the "Scheduled" list as they
246    are committed to the schedule.  As this occurs, the insns in the
247    "Pending" list have their dependencies satisfied and move to either
248    the "Ready" list or the "Queued" set depending on whether
249    sufficient time has passed to make them ready.  As time passes,
250    insns move from the "Queued" set to the "Ready" list.
251
252    The "Pending" list (P) are the insns in the INSN_FORW_DEPS of the
253    unscheduled insns, i.e., those that are ready, queued, and pending.
254    The "Queued" set (Q) is implemented by the variable `insn_queue'.
255    The "Ready" list (R) is implemented by the variables `ready' and
256    `n_ready'.
257    The "Scheduled" list (S) is the new insn chain built by this pass.
258
259    The transition (R->S) is implemented in the scheduling loop in
260    `schedule_block' when the best insn to schedule is chosen.
261    The transitions (P->R and P->Q) are implemented in `schedule_insn' as
262    insns move from the ready list to the scheduled list.
263    The transition (Q->R) is implemented in 'queue_to_insn' as time
264    passes or stalls are introduced.  */
265
266 /* Implement a circular buffer to delay instructions until sufficient
267    time has passed.  For the new pipeline description interface,
268    MAX_INSN_QUEUE_INDEX is a power of two minus one which is not less
269    than maximal time of instruction execution computed by genattr.c on
270    the base maximal time of functional unit reservations and getting a
271    result.  This is the longest time an insn may be queued.  */
272
273 static rtx *insn_queue;
274 static int q_ptr = 0;
275 static int q_size = 0;
276 #define NEXT_Q(X) (((X)+1) & max_insn_queue_index)
277 #define NEXT_Q_AFTER(X, C) (((X)+C) & max_insn_queue_index)
278
279 #define QUEUE_SCHEDULED (-3)
280 #define QUEUE_NOWHERE   (-2)
281 #define QUEUE_READY     (-1)
282 /* QUEUE_SCHEDULED - INSN is scheduled.
283    QUEUE_NOWHERE   - INSN isn't scheduled yet and is neither in
284    queue or ready list.
285    QUEUE_READY     - INSN is in ready list.
286    N >= 0 - INSN queued for X [where NEXT_Q_AFTER (q_ptr, X) == N] cycles.  */
287    
288 #define QUEUE_INDEX(INSN) (h_i_d[INSN_UID (INSN)].queue_index)
289
290 /* The following variable value refers for all current and future
291    reservations of the processor units.  */
292 state_t curr_state;
293
294 /* The following variable value is size of memory representing all
295    current and future reservations of the processor units.  */
296 static size_t dfa_state_size;
297
298 /* The following array is used to find the best insn from ready when
299    the automaton pipeline interface is used.  */
300 static char *ready_try;
301
302 /* Describe the ready list of the scheduler.
303    VEC holds space enough for all insns in the current region.  VECLEN
304    says how many exactly.
305    FIRST is the index of the element with the highest priority; i.e. the
306    last one in the ready list, since elements are ordered by ascending
307    priority.
308    N_READY determines how many insns are on the ready list.  */
309
310 struct ready_list
311 {
312   rtx *vec;
313   int veclen;
314   int first;
315   int n_ready;
316 };
317
318 /* The pointer to the ready list.  */
319 static struct ready_list *readyp;
320
321 /* Scheduling clock.  */
322 static int clock_var;
323
324 /* Number of instructions in current scheduling region.  */
325 static int rgn_n_insns;
326
327 static int may_trap_exp (rtx, int);
328
329 /* Nonzero iff the address is comprised from at most 1 register.  */
330 #define CONST_BASED_ADDRESS_P(x)                        \
331   (REG_P (x)                                    \
332    || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS   \
333         || (GET_CODE (x) == LO_SUM))                    \
334        && (CONSTANT_P (XEXP (x, 0))                     \
335            || CONSTANT_P (XEXP (x, 1)))))
336
337 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
338    as found by analyzing insn's expression.  */
339
340 static int
341 may_trap_exp (rtx x, int is_store)
342 {
343   enum rtx_code code;
344
345   if (x == 0)
346     return TRAP_FREE;
347   code = GET_CODE (x);
348   if (is_store)
349     {
350       if (code == MEM && may_trap_p (x))
351         return TRAP_RISKY;
352       else
353         return TRAP_FREE;
354     }
355   if (code == MEM)
356     {
357       /* The insn uses memory:  a volatile load.  */
358       if (MEM_VOLATILE_P (x))
359         return IRISKY;
360       /* An exception-free load.  */
361       if (!may_trap_p (x))
362         return IFREE;
363       /* A load with 1 base register, to be further checked.  */
364       if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
365         return PFREE_CANDIDATE;
366       /* No info on the load, to be further checked.  */
367       return PRISKY_CANDIDATE;
368     }
369   else
370     {
371       const char *fmt;
372       int i, insn_class = TRAP_FREE;
373
374       /* Neither store nor load, check if it may cause a trap.  */
375       if (may_trap_p (x))
376         return TRAP_RISKY;
377       /* Recursive step: walk the insn...  */
378       fmt = GET_RTX_FORMAT (code);
379       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
380         {
381           if (fmt[i] == 'e')
382             {
383               int tmp_class = may_trap_exp (XEXP (x, i), is_store);
384               insn_class = WORST_CLASS (insn_class, tmp_class);
385             }
386           else if (fmt[i] == 'E')
387             {
388               int j;
389               for (j = 0; j < XVECLEN (x, i); j++)
390                 {
391                   int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
392                   insn_class = WORST_CLASS (insn_class, tmp_class);
393                   if (insn_class == TRAP_RISKY || insn_class == IRISKY)
394                     break;
395                 }
396             }
397           if (insn_class == TRAP_RISKY || insn_class == IRISKY)
398             break;
399         }
400       return insn_class;
401     }
402 }
403
404 /* Classifies insn for the purpose of verifying that it can be
405    moved speculatively, by examining it's patterns, returning:
406    TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
407    TRAP_FREE: non-load insn.
408    IFREE: load from a globally safe location.
409    IRISKY: volatile load.
410    PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
411    being either PFREE or PRISKY.  */
412
413 int
414 haifa_classify_insn (rtx insn)
415 {
416   rtx pat = PATTERN (insn);
417   int tmp_class = TRAP_FREE;
418   int insn_class = TRAP_FREE;
419   enum rtx_code code;
420
421   if (GET_CODE (pat) == PARALLEL)
422     {
423       int i, len = XVECLEN (pat, 0);
424
425       for (i = len - 1; i >= 0; i--)
426         {
427           code = GET_CODE (XVECEXP (pat, 0, i));
428           switch (code)
429             {
430             case CLOBBER:
431               /* Test if it is a 'store'.  */
432               tmp_class = may_trap_exp (XEXP (XVECEXP (pat, 0, i), 0), 1);
433               break;
434             case SET:
435               /* Test if it is a store.  */
436               tmp_class = may_trap_exp (SET_DEST (XVECEXP (pat, 0, i)), 1);
437               if (tmp_class == TRAP_RISKY)
438                 break;
439               /* Test if it is a load.  */
440               tmp_class
441                 = WORST_CLASS (tmp_class,
442                                may_trap_exp (SET_SRC (XVECEXP (pat, 0, i)),
443                                              0));
444               break;
445             case COND_EXEC:
446             case TRAP_IF:
447               tmp_class = TRAP_RISKY;
448               break;
449             default:
450               ;
451             }
452           insn_class = WORST_CLASS (insn_class, tmp_class);
453           if (insn_class == TRAP_RISKY || insn_class == IRISKY)
454             break;
455         }
456     }
457   else
458     {
459       code = GET_CODE (pat);
460       switch (code)
461         {
462         case CLOBBER:
463           /* Test if it is a 'store'.  */
464           tmp_class = may_trap_exp (XEXP (pat, 0), 1);
465           break;
466         case SET:
467           /* Test if it is a store.  */
468           tmp_class = may_trap_exp (SET_DEST (pat), 1);
469           if (tmp_class == TRAP_RISKY)
470             break;
471           /* Test if it is a load.  */
472           tmp_class =
473             WORST_CLASS (tmp_class,
474                          may_trap_exp (SET_SRC (pat), 0));
475           break;
476         case COND_EXEC:
477         case TRAP_IF:
478           tmp_class = TRAP_RISKY;
479           break;
480         default:;
481         }
482       insn_class = tmp_class;
483     }
484
485   return insn_class;
486 }
487
488 /* A typedef for rtx vector.  */
489 typedef VEC(rtx, heap) *rtx_vec_t;
490
491 /* Forward declarations.  */
492
493 static int priority (rtx);
494 static int rank_for_schedule (const void *, const void *);
495 static void swap_sort (rtx *, int);
496 static void queue_insn (rtx, int);
497 static int schedule_insn (rtx);
498 static int find_set_reg_weight (rtx);
499 static void find_insn_reg_weight (basic_block);
500 static void find_insn_reg_weight1 (rtx);
501 static void adjust_priority (rtx);
502 static void advance_one_cycle (void);
503
504 /* Notes handling mechanism:
505    =========================
506    Generally, NOTES are saved before scheduling and restored after scheduling.
507    The scheduler distinguishes between two types of notes:
508
509    (1) LOOP_BEGIN, LOOP_END, SETJMP, EHREGION_BEG, EHREGION_END notes:
510    Before scheduling a region, a pointer to the note is added to the insn
511    that follows or precedes it.  (This happens as part of the data dependence
512    computation).  After scheduling an insn, the pointer contained in it is
513    used for regenerating the corresponding note (in reemit_notes).
514
515    (2) All other notes (e.g. INSN_DELETED):  Before scheduling a block,
516    these notes are put in a list (in rm_other_notes() and
517    unlink_other_notes ()).  After scheduling the block, these notes are
518    inserted at the beginning of the block (in schedule_block()).  */
519
520 static rtx unlink_other_notes (rtx, rtx);
521 static void reemit_notes (rtx);
522
523 static rtx *ready_lastpos (struct ready_list *);
524 static void ready_add (struct ready_list *, rtx, bool);
525 static void ready_sort (struct ready_list *);
526 static rtx ready_remove_first (struct ready_list *);
527
528 static void queue_to_ready (struct ready_list *);
529 static int early_queue_to_ready (state_t, struct ready_list *);
530
531 static void debug_ready_list (struct ready_list *);
532
533 static void move_insn (rtx);
534
535 /* The following functions are used to implement multi-pass scheduling
536    on the first cycle.  */
537 static rtx ready_element (struct ready_list *, int);
538 static rtx ready_remove (struct ready_list *, int);
539 static void ready_remove_insn (rtx);
540 static int max_issue (struct ready_list *, int *, int);
541
542 static int choose_ready (struct ready_list *, rtx *);
543
544 static void fix_inter_tick (rtx, rtx);
545 static int fix_tick_ready (rtx);
546 static void change_queue_index (rtx, int);
547
548 /* The following functions are used to implement scheduling of data/control
549    speculative instructions.  */
550
551 static void extend_h_i_d (void);
552 static void extend_ready (int);
553 static void extend_global (rtx);
554 static void extend_all (rtx);
555 static void init_h_i_d (rtx);
556 static void generate_recovery_code (rtx);
557 static void process_insn_forw_deps_be_in_spec (deps_list_t, rtx, ds_t);
558 static void begin_speculative_block (rtx);
559 static void add_to_speculative_block (rtx);
560 static dw_t dep_weak (ds_t);
561 static edge find_fallthru_edge (basic_block);
562 static void init_before_recovery (void);
563 static basic_block create_recovery_block (void);
564 static void create_check_block_twin (rtx, bool);
565 static void fix_recovery_deps (basic_block);
566 static void change_pattern (rtx, rtx);
567 static int speculate_insn (rtx, ds_t, rtx *);
568 static void dump_new_block_header (int, basic_block, rtx, rtx);
569 static void restore_bb_notes (basic_block);
570 static void extend_bb (void);
571 static void fix_jump_move (rtx);
572 static void move_block_after_check (rtx);
573 static void move_succs (VEC(edge,gc) **, basic_block);
574 static void sched_remove_insn (rtx);
575 static void clear_priorities (rtx, rtx_vec_t *);
576 static void calc_priorities (rtx_vec_t);
577 static void add_jump_dependencies (rtx, rtx);
578 #ifdef ENABLE_CHECKING
579 static int has_edge_p (VEC(edge,gc) *, int);
580 static void check_cfg (rtx, rtx);
581 static void check_sched_flags (void);
582 #endif
583
584 #endif /* INSN_SCHEDULING */
585 \f
586 /* Point to state used for the current scheduling pass.  */
587 struct sched_info *current_sched_info;
588 \f
589 #ifndef INSN_SCHEDULING
590 void
591 schedule_insns (void)
592 {
593 }
594 #else
595
596 /* Working copy of frontend's sched_info variable.  */
597 static struct sched_info current_sched_info_var;
598
599 /* Pointer to the last instruction scheduled.  Used by rank_for_schedule,
600    so that insns independent of the last scheduled insn will be preferred
601    over dependent instructions.  */
602
603 static rtx last_scheduled_insn;
604
605 /* Cached cost of the instruction.  Use below function to get cost of the
606    insn.  -1 here means that the field is not initialized.  */
607 #define INSN_COST(INSN)         (h_i_d[INSN_UID (INSN)].cost)
608
609 /* Compute cost of executing INSN.
610    This is the number of cycles between instruction issue and
611    instruction results.  */
612 HAIFA_INLINE int
613 insn_cost (rtx insn)
614 {
615   int cost = INSN_COST (insn);
616
617   if (cost < 0)
618     {
619       /* A USE insn, or something else we don't need to
620          understand.  We can't pass these directly to
621          result_ready_cost or insn_default_latency because it will
622          trigger a fatal error for unrecognizable insns.  */
623       if (recog_memoized (insn) < 0)
624         {
625           INSN_COST (insn) = 0;
626           return 0;
627         }
628       else
629         {
630           cost = insn_default_latency (insn);
631           if (cost < 0)
632             cost = 0;
633
634           INSN_COST (insn) = cost;
635         }
636     }
637
638   return cost;
639 }
640
641 /* Compute cost of dependence LINK.
642    This is the number of cycles between instruction issue and
643    instruction results.  */
644 int
645 dep_cost (dep_t link)
646 {
647   rtx used = DEP_CON (link);
648   int cost;
649
650   /* A USE insn should never require the value used to be computed.
651      This allows the computation of a function's result and parameter
652      values to overlap the return and call.  */
653   if (recog_memoized (used) < 0)
654     cost = 0;
655   else
656     {
657       rtx insn = DEP_PRO (link);
658       enum reg_note dep_type = DEP_KIND (link);
659
660       cost = insn_cost (insn);
661
662       if (INSN_CODE (insn) >= 0)
663         {
664           if (dep_type == REG_DEP_ANTI)
665             cost = 0;
666           else if (dep_type == REG_DEP_OUTPUT)
667             {
668               cost = (insn_default_latency (insn)
669                       - insn_default_latency (used));
670               if (cost <= 0)
671                 cost = 1;
672             }
673           else if (bypass_p (insn))
674             cost = insn_latency (insn, used);
675         }
676
677       if (targetm.sched.adjust_cost != NULL)
678         {
679           /* This variable is used for backward compatibility with the
680              targets.  */
681           rtx dep_cost_rtx_link = alloc_INSN_LIST (NULL_RTX, NULL_RTX);
682
683           /* Make it self-cycled, so that if some tries to walk over this
684              incomplete list he/she will be caught in an endless loop.  */
685           XEXP (dep_cost_rtx_link, 1) = dep_cost_rtx_link;
686
687           /* Targets use only REG_NOTE_KIND of the link.  */
688           PUT_REG_NOTE_KIND (dep_cost_rtx_link, DEP_KIND (link));
689
690           cost = targetm.sched.adjust_cost (used, dep_cost_rtx_link,
691                                             insn, cost);
692
693           free_INSN_LIST_node (dep_cost_rtx_link);
694         }
695
696       if (cost < 0)
697         cost = 0;
698     }
699
700   return cost;
701 }
702
703 /* Return 'true' if DEP should be included in priority calculations.  */
704 static bool
705 contributes_to_priority_p (dep_t dep)
706 {
707   /* Critical path is meaningful in block boundaries only.  */
708   if (!current_sched_info->contributes_to_priority (DEP_CON (dep),
709                                                     DEP_PRO (dep)))
710     return false;
711
712   /* If flag COUNT_SPEC_IN_CRITICAL_PATH is set,
713      then speculative instructions will less likely be
714      scheduled.  That is because the priority of
715      their producers will increase, and, thus, the
716      producers will more likely be scheduled, thus,
717      resolving the dependence.  */
718   if ((current_sched_info->flags & DO_SPECULATION)
719       && !(spec_info->flags & COUNT_SPEC_IN_CRITICAL_PATH)
720       && (DEP_STATUS (dep) & SPECULATIVE))
721     return false;
722
723   return true;
724 }
725
726 /* Compute the priority number for INSN.  */
727 static int
728 priority (rtx insn)
729 {
730   dep_link_t link;
731
732   if (! INSN_P (insn))
733     return 0;
734
735   /* We should not be interested in priority of an already scheduled insn.  */
736   gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
737
738   if (!INSN_PRIORITY_KNOWN (insn))
739     {
740       int this_priority = 0;
741
742       if (deps_list_empty_p (INSN_FORW_DEPS (insn)))
743         /* ??? We should set INSN_PRIORITY to insn_cost when and insn has
744            some forward deps but all of them are ignored by
745            contributes_to_priority hook.  At the moment we set priority of
746            such insn to 0.  */
747         this_priority = insn_cost (insn);
748       else
749         {
750           rtx prev_first, twin;
751           basic_block rec;
752
753           /* For recovery check instructions we calculate priority slightly
754              different than that of normal instructions.  Instead of walking
755              through INSN_FORW_DEPS (check) list, we walk through
756              INSN_FORW_DEPS list of each instruction in the corresponding
757              recovery block.  */ 
758
759           rec = RECOVERY_BLOCK (insn);
760           if (!rec || rec == EXIT_BLOCK_PTR)
761             {
762               prev_first = PREV_INSN (insn);
763               twin = insn;
764             }
765           else
766             {
767               prev_first = NEXT_INSN (BB_HEAD (rec));
768               twin = PREV_INSN (BB_END (rec));
769             }
770
771           do
772             {
773               FOR_EACH_DEP_LINK (link, INSN_FORW_DEPS (twin))
774                 {
775                   rtx next;
776                   int next_priority;
777                   dep_t dep = DEP_LINK_DEP (link);
778
779                   next = DEP_CON (dep);
780
781                   if (BLOCK_FOR_INSN (next) != rec)
782                     {
783                       int cost;
784
785                       if (!contributes_to_priority_p (dep))
786                         continue;
787
788                       if (twin == insn)
789                         cost = dep_cost (dep);
790                       else
791                         {
792                           struct _dep _dep1, *dep1 = &_dep1;
793
794                           init_dep (dep1, insn, next, REG_DEP_ANTI);
795
796                           cost = dep_cost (dep1);
797                         }
798
799                       next_priority = cost + priority (next);
800
801                       if (next_priority > this_priority)
802                         this_priority = next_priority;
803                     }
804                 }
805               
806               twin = PREV_INSN (twin);
807             }
808           while (twin != prev_first);
809         }
810       INSN_PRIORITY (insn) = this_priority;
811       INSN_PRIORITY_STATUS (insn) = 1;
812     }
813
814   return INSN_PRIORITY (insn);
815 }
816 \f
817 /* Macros and functions for keeping the priority queue sorted, and
818    dealing with queuing and dequeuing of instructions.  */
819
820 #define SCHED_SORT(READY, N_READY)                                   \
821 do { if ((N_READY) == 2)                                             \
822        swap_sort (READY, N_READY);                                   \
823      else if ((N_READY) > 2)                                         \
824          qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); }  \
825 while (0)
826
827 /* Returns a positive value if x is preferred; returns a negative value if
828    y is preferred.  Should never return 0, since that will make the sort
829    unstable.  */
830
831 static int
832 rank_for_schedule (const void *x, const void *y)
833 {
834   rtx tmp = *(const rtx *) y;
835   rtx tmp2 = *(const rtx *) x;
836   dep_link_t link1, link2;
837   int tmp_class, tmp2_class;
838   int val, priority_val, weight_val, info_val;
839
840   /* The insn in a schedule group should be issued the first.  */
841   if (SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
842     return SCHED_GROUP_P (tmp2) ? 1 : -1;
843
844   /* Make sure that priority of TMP and TMP2 are initialized.  */
845   gcc_assert (INSN_PRIORITY_KNOWN (tmp) && INSN_PRIORITY_KNOWN (tmp2));
846
847   /* Prefer insn with higher priority.  */
848   priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
849
850   if (priority_val)
851     return priority_val;
852
853   /* Prefer speculative insn with greater dependencies weakness.  */
854   if (spec_info)
855     {
856       ds_t ds1, ds2;
857       dw_t dw1, dw2;
858       int dw;
859
860       ds1 = TODO_SPEC (tmp) & SPECULATIVE;
861       if (ds1)
862         dw1 = dep_weak (ds1);
863       else
864         dw1 = NO_DEP_WEAK;
865       
866       ds2 = TODO_SPEC (tmp2) & SPECULATIVE;
867       if (ds2)
868         dw2 = dep_weak (ds2);
869       else
870         dw2 = NO_DEP_WEAK;
871
872       dw = dw2 - dw1;
873       if (dw > (NO_DEP_WEAK / 8) || dw < -(NO_DEP_WEAK / 8))
874         return dw;
875     }
876
877   /* Prefer an insn with smaller contribution to registers-pressure.  */
878   if (!reload_completed &&
879       (weight_val = INSN_REG_WEIGHT (tmp) - INSN_REG_WEIGHT (tmp2)))
880     return weight_val;
881
882   info_val = (*current_sched_info->rank) (tmp, tmp2);
883   if (info_val)
884     return info_val;
885
886   /* Compare insns based on their relation to the last-scheduled-insn.  */
887   if (INSN_P (last_scheduled_insn))
888     {
889       /* Classify the instructions into three classes:
890          1) Data dependent on last schedule insn.
891          2) Anti/Output dependent on last scheduled insn.
892          3) Independent of last scheduled insn, or has latency of one.
893          Choose the insn from the highest numbered class if different.  */
894       link1
895         = find_link_by_con_in_deps_list (INSN_FORW_DEPS (last_scheduled_insn),
896                                          tmp);
897
898       if (link1 == NULL || dep_cost (DEP_LINK_DEP (link1)) == 1)
899         tmp_class = 3;
900       else if (/* Data dependence.  */
901                DEP_LINK_KIND (link1) == REG_DEP_TRUE)
902         tmp_class = 1;
903       else
904         tmp_class = 2;
905
906       link2
907         = find_link_by_con_in_deps_list (INSN_FORW_DEPS (last_scheduled_insn),
908                                          tmp2);
909
910       if (link2 == NULL || dep_cost (DEP_LINK_DEP (link2))  == 1)
911         tmp2_class = 3;
912       else if (/* Data dependence.  */
913                DEP_LINK_KIND (link2) == REG_DEP_TRUE)
914         tmp2_class = 1;
915       else
916         tmp2_class = 2;
917
918       if ((val = tmp2_class - tmp_class))
919         return val;
920     }
921
922   /* Prefer the insn which has more later insns that depend on it.
923      This gives the scheduler more freedom when scheduling later
924      instructions at the expense of added register pressure.  */
925
926   link1 = DEPS_LIST_FIRST (INSN_FORW_DEPS (tmp));
927   link2 = DEPS_LIST_FIRST (INSN_FORW_DEPS (tmp2));
928
929   while (link1 != NULL && link2 != NULL)
930     {
931       link1 = DEP_LINK_NEXT (link1);
932       link2 = DEP_LINK_NEXT (link2);
933     }
934
935   if (link1 != NULL && link2 == NULL)
936     /* TMP (Y) has more insns that depend on it.  */
937     return -1;
938   if (link1 == NULL && link2 != NULL)
939     /* TMP2 (X) has more insns that depend on it.  */
940     return 1;
941
942   /* If insns are equally good, sort by INSN_LUID (original insn order),
943      so that we make the sort stable.  This minimizes instruction movement,
944      thus minimizing sched's effect on debugging and cross-jumping.  */
945   return INSN_LUID (tmp) - INSN_LUID (tmp2);
946 }
947
948 /* Resort the array A in which only element at index N may be out of order.  */
949
950 HAIFA_INLINE static void
951 swap_sort (rtx *a, int n)
952 {
953   rtx insn = a[n - 1];
954   int i = n - 2;
955
956   while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
957     {
958       a[i + 1] = a[i];
959       i -= 1;
960     }
961   a[i + 1] = insn;
962 }
963
964 /* Add INSN to the insn queue so that it can be executed at least
965    N_CYCLES after the currently executing insn.  Preserve insns
966    chain for debugging purposes.  */
967
968 HAIFA_INLINE static void
969 queue_insn (rtx insn, int n_cycles)
970 {
971   int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
972   rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
973
974   gcc_assert (n_cycles <= max_insn_queue_index);
975
976   insn_queue[next_q] = link;
977   q_size += 1;
978
979   if (sched_verbose >= 2)
980     {
981       fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
982                (*current_sched_info->print_insn) (insn, 0));
983
984       fprintf (sched_dump, "queued for %d cycles.\n", n_cycles);
985     }
986   
987   QUEUE_INDEX (insn) = next_q;
988 }
989
990 /* Remove INSN from queue.  */
991 static void
992 queue_remove (rtx insn)
993 {
994   gcc_assert (QUEUE_INDEX (insn) >= 0);
995   remove_free_INSN_LIST_elem (insn, &insn_queue[QUEUE_INDEX (insn)]);
996   q_size--;
997   QUEUE_INDEX (insn) = QUEUE_NOWHERE;
998 }
999
1000 /* Return a pointer to the bottom of the ready list, i.e. the insn
1001    with the lowest priority.  */
1002
1003 HAIFA_INLINE static rtx *
1004 ready_lastpos (struct ready_list *ready)
1005 {
1006   gcc_assert (ready->n_ready >= 1);
1007   return ready->vec + ready->first - ready->n_ready + 1;
1008 }
1009
1010 /* Add an element INSN to the ready list so that it ends up with the
1011    lowest/highest priority depending on FIRST_P.  */
1012
1013 HAIFA_INLINE static void
1014 ready_add (struct ready_list *ready, rtx insn, bool first_p)
1015 {
1016   if (!first_p)
1017     {
1018       if (ready->first == ready->n_ready)
1019         {
1020           memmove (ready->vec + ready->veclen - ready->n_ready,
1021                    ready_lastpos (ready),
1022                    ready->n_ready * sizeof (rtx));
1023           ready->first = ready->veclen - 1;
1024         }
1025       ready->vec[ready->first - ready->n_ready] = insn;
1026     }
1027   else
1028     {
1029       if (ready->first == ready->veclen - 1)
1030         {
1031           if (ready->n_ready)
1032             /* ready_lastpos() fails when called with (ready->n_ready == 0).  */
1033             memmove (ready->vec + ready->veclen - ready->n_ready - 1,
1034                      ready_lastpos (ready),
1035                      ready->n_ready * sizeof (rtx));
1036           ready->first = ready->veclen - 2;
1037         }
1038       ready->vec[++(ready->first)] = insn;
1039     }
1040
1041   ready->n_ready++;
1042
1043   gcc_assert (QUEUE_INDEX (insn) != QUEUE_READY);
1044   QUEUE_INDEX (insn) = QUEUE_READY;
1045 }
1046
1047 /* Remove the element with the highest priority from the ready list and
1048    return it.  */
1049
1050 HAIFA_INLINE static rtx
1051 ready_remove_first (struct ready_list *ready)
1052 {
1053   rtx t;
1054   
1055   gcc_assert (ready->n_ready);
1056   t = ready->vec[ready->first--];
1057   ready->n_ready--;
1058   /* If the queue becomes empty, reset it.  */
1059   if (ready->n_ready == 0)
1060     ready->first = ready->veclen - 1;
1061
1062   gcc_assert (QUEUE_INDEX (t) == QUEUE_READY);
1063   QUEUE_INDEX (t) = QUEUE_NOWHERE;
1064
1065   return t;
1066 }
1067
1068 /* The following code implements multi-pass scheduling for the first
1069    cycle.  In other words, we will try to choose ready insn which
1070    permits to start maximum number of insns on the same cycle.  */
1071
1072 /* Return a pointer to the element INDEX from the ready.  INDEX for
1073    insn with the highest priority is 0, and the lowest priority has
1074    N_READY - 1.  */
1075
1076 HAIFA_INLINE static rtx
1077 ready_element (struct ready_list *ready, int index)
1078 {
1079   gcc_assert (ready->n_ready && index < ready->n_ready);
1080   
1081   return ready->vec[ready->first - index];
1082 }
1083
1084 /* Remove the element INDEX from the ready list and return it.  INDEX
1085    for insn with the highest priority is 0, and the lowest priority
1086    has N_READY - 1.  */
1087
1088 HAIFA_INLINE static rtx
1089 ready_remove (struct ready_list *ready, int index)
1090 {
1091   rtx t;
1092   int i;
1093
1094   if (index == 0)
1095     return ready_remove_first (ready);
1096   gcc_assert (ready->n_ready && index < ready->n_ready);
1097   t = ready->vec[ready->first - index];
1098   ready->n_ready--;
1099   for (i = index; i < ready->n_ready; i++)
1100     ready->vec[ready->first - i] = ready->vec[ready->first - i - 1];
1101   QUEUE_INDEX (t) = QUEUE_NOWHERE;
1102   return t;
1103 }
1104
1105 /* Remove INSN from the ready list.  */
1106 static void
1107 ready_remove_insn (rtx insn)
1108 {
1109   int i;
1110
1111   for (i = 0; i < readyp->n_ready; i++)
1112     if (ready_element (readyp, i) == insn)
1113       {
1114         ready_remove (readyp, i);
1115         return;
1116       }
1117   gcc_unreachable ();
1118 }
1119
1120 /* Sort the ready list READY by ascending priority, using the SCHED_SORT
1121    macro.  */
1122
1123 HAIFA_INLINE static void
1124 ready_sort (struct ready_list *ready)
1125 {
1126   rtx *first = ready_lastpos (ready);
1127   SCHED_SORT (first, ready->n_ready);
1128 }
1129
1130 /* PREV is an insn that is ready to execute.  Adjust its priority if that
1131    will help shorten or lengthen register lifetimes as appropriate.  Also
1132    provide a hook for the target to tweek itself.  */
1133
1134 HAIFA_INLINE static void
1135 adjust_priority (rtx prev)
1136 {
1137   /* ??? There used to be code here to try and estimate how an insn
1138      affected register lifetimes, but it did it by looking at REG_DEAD
1139      notes, which we removed in schedule_region.  Nor did it try to
1140      take into account register pressure or anything useful like that.
1141
1142      Revisit when we have a machine model to work with and not before.  */
1143
1144   if (targetm.sched.adjust_priority)
1145     INSN_PRIORITY (prev) =
1146       targetm.sched.adjust_priority (prev, INSN_PRIORITY (prev));
1147 }
1148
1149 /* Advance time on one cycle.  */
1150 HAIFA_INLINE static void
1151 advance_one_cycle (void)
1152 {
1153   if (targetm.sched.dfa_pre_cycle_insn)
1154     state_transition (curr_state,
1155                       targetm.sched.dfa_pre_cycle_insn ());
1156
1157   state_transition (curr_state, NULL);
1158   
1159   if (targetm.sched.dfa_post_cycle_insn)
1160     state_transition (curr_state,
1161                       targetm.sched.dfa_post_cycle_insn ());
1162 }
1163
1164 /* Clock at which the previous instruction was issued.  */
1165 static int last_clock_var;
1166
1167 /* INSN is the "currently executing insn".  Launch each insn which was
1168    waiting on INSN.  READY is the ready list which contains the insns
1169    that are ready to fire.  CLOCK is the current cycle.  The function
1170    returns necessary cycle advance after issuing the insn (it is not
1171    zero for insns in a schedule group).  */
1172
1173 static int
1174 schedule_insn (rtx insn)
1175 {
1176   dep_link_t link;
1177   int advance = 0;
1178
1179   if (sched_verbose >= 1)
1180     {
1181       char buf[2048];
1182
1183       print_insn (buf, insn, 0);
1184       buf[40] = 0;
1185       fprintf (sched_dump, ";;\t%3i--> %-40s:", clock_var, buf);
1186
1187       if (recog_memoized (insn) < 0)
1188         fprintf (sched_dump, "nothing");
1189       else
1190         print_reservation (sched_dump, insn);
1191       fputc ('\n', sched_dump);
1192     }
1193
1194   /* Scheduling instruction should have all its dependencies resolved and
1195      should have been removed from the ready list.  */
1196   gcc_assert (INSN_DEP_COUNT (insn) == 0
1197               && deps_list_empty_p (INSN_BACK_DEPS (insn)));
1198   free_deps_list (INSN_BACK_DEPS (insn));
1199
1200   /* Now we can free INSN_RESOLVED_BACK_DEPS list.  */
1201   delete_deps_list (INSN_RESOLVED_BACK_DEPS (insn));
1202
1203   gcc_assert (QUEUE_INDEX (insn) == QUEUE_NOWHERE);
1204   QUEUE_INDEX (insn) = QUEUE_SCHEDULED;
1205
1206   gcc_assert (INSN_TICK (insn) >= MIN_TICK);
1207   if (INSN_TICK (insn) > clock_var)
1208     /* INSN has been prematurely moved from the queue to the ready list.
1209        This is possible only if following flag is set.  */
1210     gcc_assert (flag_sched_stalled_insns);    
1211
1212   /* ??? Probably, if INSN is scheduled prematurely, we should leave
1213      INSN_TICK untouched.  This is a machine-dependent issue, actually.  */
1214   INSN_TICK (insn) = clock_var;
1215
1216   /* Update dependent instructions.  */
1217   FOR_EACH_DEP_LINK (link, INSN_FORW_DEPS (insn))
1218     {
1219       rtx next = DEP_LINK_CON (link);
1220
1221       /* Resolve the dependence between INSN and NEXT.  */
1222
1223       INSN_DEP_COUNT (next)--;
1224
1225       move_dep_link (DEP_NODE_BACK (DEP_LINK_NODE (link)),
1226                         INSN_RESOLVED_BACK_DEPS (next));
1227
1228       gcc_assert ((INSN_DEP_COUNT (next) == 0)
1229                   == deps_list_empty_p (INSN_BACK_DEPS (next)));
1230
1231       if (!IS_SPECULATION_BRANCHY_CHECK_P (insn))
1232         {
1233           int effective_cost;      
1234           
1235           effective_cost = try_ready (next);
1236           
1237           if (effective_cost >= 0
1238               && SCHED_GROUP_P (next)
1239               && advance < effective_cost)
1240             advance = effective_cost;
1241         }
1242       else
1243         /* Check always has only one forward dependence (to the first insn in
1244            the recovery block), therefore, this will be executed only once.  */
1245         {
1246           gcc_assert (DEP_LINK_NEXT (link) == NULL);
1247           fix_recovery_deps (RECOVERY_BLOCK (insn));
1248         }
1249     }
1250
1251   /* Annotate the instruction with issue information -- TImode
1252      indicates that the instruction is expected not to be able
1253      to issue on the same cycle as the previous insn.  A machine
1254      may use this information to decide how the instruction should
1255      be aligned.  */
1256   if (issue_rate > 1
1257       && GET_CODE (PATTERN (insn)) != USE
1258       && GET_CODE (PATTERN (insn)) != CLOBBER)
1259     {
1260       if (reload_completed)
1261         PUT_MODE (insn, clock_var > last_clock_var ? TImode : VOIDmode);
1262       last_clock_var = clock_var;
1263     }
1264
1265   return advance;
1266 }
1267
1268 /* Functions for handling of notes.  */
1269
1270 /* Delete notes beginning with INSN and put them in the chain
1271    of notes ended by NOTE_LIST.
1272    Returns the insn following the notes.  */
1273
1274 static rtx
1275 unlink_other_notes (rtx insn, rtx tail)
1276 {
1277   rtx prev = PREV_INSN (insn);
1278
1279   while (insn != tail && NOTE_NOT_BB_P (insn))
1280     {
1281       rtx next = NEXT_INSN (insn);
1282       basic_block bb = BLOCK_FOR_INSN (insn);
1283
1284       /* Delete the note from its current position.  */
1285       if (prev)
1286         NEXT_INSN (prev) = next;
1287       if (next)
1288         PREV_INSN (next) = prev;
1289
1290       if (bb)
1291         {
1292           /* Basic block can begin with either LABEL or
1293              NOTE_INSN_BASIC_BLOCK.  */
1294           gcc_assert (BB_HEAD (bb) != insn);
1295
1296           /* Check if we are removing last insn in the BB.  */
1297           if (BB_END (bb) == insn)
1298             BB_END (bb) = prev;
1299         }
1300
1301       /* See sched_analyze to see how these are handled.  */
1302       if (NOTE_KIND (insn) != NOTE_INSN_EH_REGION_BEG
1303           && NOTE_KIND (insn) != NOTE_INSN_EH_REGION_END)
1304         {
1305           /* Insert the note at the end of the notes list.  */
1306           PREV_INSN (insn) = note_list;
1307           if (note_list)
1308             NEXT_INSN (note_list) = insn;
1309           note_list = insn;
1310         }
1311
1312       insn = next;
1313     }
1314   return insn;
1315 }
1316
1317 /* Return the head and tail pointers of ebb starting at BEG and ending
1318    at END.  */
1319
1320 void
1321 get_ebb_head_tail (basic_block beg, basic_block end, rtx *headp, rtx *tailp)
1322 {
1323   rtx beg_head = BB_HEAD (beg);
1324   rtx beg_tail = BB_END (beg);
1325   rtx end_head = BB_HEAD (end);
1326   rtx end_tail = BB_END (end);
1327
1328   /* Don't include any notes or labels at the beginning of the BEG
1329      basic block, or notes at the end of the END basic blocks.  */
1330
1331   if (LABEL_P (beg_head))
1332     beg_head = NEXT_INSN (beg_head);
1333
1334   while (beg_head != beg_tail)
1335     if (NOTE_P (beg_head))
1336       beg_head = NEXT_INSN (beg_head);
1337     else
1338       break;
1339
1340   *headp = beg_head;
1341
1342   if (beg == end)
1343     end_head = beg_head;
1344   else if (LABEL_P (end_head))
1345     end_head = NEXT_INSN (end_head);
1346
1347   while (end_head != end_tail)
1348     if (NOTE_P (end_tail))
1349       end_tail = PREV_INSN (end_tail);
1350     else
1351       break;
1352
1353   *tailp = end_tail;
1354 }
1355
1356 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ].  */
1357
1358 int
1359 no_real_insns_p (rtx head, rtx tail)
1360 {
1361   while (head != NEXT_INSN (tail))
1362     {
1363       if (!NOTE_P (head) && !LABEL_P (head))
1364         return 0;
1365       head = NEXT_INSN (head);
1366     }
1367   return 1;
1368 }
1369
1370 /* Delete notes between HEAD and TAIL and put them in the chain
1371    of notes ended by NOTE_LIST.  */
1372
1373 void
1374 rm_other_notes (rtx head, rtx tail)
1375 {
1376   rtx next_tail;
1377   rtx insn;
1378
1379   note_list = 0;
1380   if (head == tail && (! INSN_P (head)))
1381     return;
1382
1383   next_tail = NEXT_INSN (tail);
1384   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1385     {
1386       rtx prev;
1387
1388       /* Farm out notes, and maybe save them in NOTE_LIST.
1389          This is needed to keep the debugger from
1390          getting completely deranged.  */
1391       if (NOTE_NOT_BB_P (insn))
1392         {
1393           prev = insn;
1394
1395           insn = unlink_other_notes (insn, next_tail);
1396
1397           gcc_assert (prev != tail && prev != head && insn != next_tail);
1398         }
1399     }
1400 }
1401
1402 /* Functions for computation of registers live/usage info.  */
1403
1404 /* This function looks for a new register being defined.
1405    If the destination register is already used by the source,
1406    a new register is not needed.  */
1407
1408 static int
1409 find_set_reg_weight (rtx x)
1410 {
1411   if (GET_CODE (x) == CLOBBER
1412       && register_operand (SET_DEST (x), VOIDmode))
1413     return 1;
1414   if (GET_CODE (x) == SET
1415       && register_operand (SET_DEST (x), VOIDmode))
1416     {
1417       if (REG_P (SET_DEST (x)))
1418         {
1419           if (!reg_mentioned_p (SET_DEST (x), SET_SRC (x)))
1420             return 1;
1421           else
1422             return 0;
1423         }
1424       return 1;
1425     }
1426   return 0;
1427 }
1428
1429 /* Calculate INSN_REG_WEIGHT for all insns of a block.  */
1430
1431 static void
1432 find_insn_reg_weight (basic_block bb)
1433 {
1434   rtx insn, next_tail, head, tail;
1435
1436   get_ebb_head_tail (bb, bb, &head, &tail);
1437   next_tail = NEXT_INSN (tail);
1438
1439   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1440     find_insn_reg_weight1 (insn);    
1441 }
1442
1443 /* Calculate INSN_REG_WEIGHT for single instruction.
1444    Separated from find_insn_reg_weight because of need
1445    to initialize new instruction in generate_recovery_code.  */
1446 static void
1447 find_insn_reg_weight1 (rtx insn)
1448 {
1449   int reg_weight = 0;
1450   rtx x;
1451   
1452   /* Handle register life information.  */
1453   if (! INSN_P (insn))
1454     return;
1455   
1456   /* Increment weight for each register born here.  */
1457   x = PATTERN (insn);
1458   reg_weight += find_set_reg_weight (x);
1459   if (GET_CODE (x) == PARALLEL)
1460     {
1461       int j;
1462       for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
1463         {
1464           x = XVECEXP (PATTERN (insn), 0, j);
1465           reg_weight += find_set_reg_weight (x);
1466         }
1467     }
1468   /* Decrement weight for each register that dies here.  */
1469   for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1470     {
1471       if (REG_NOTE_KIND (x) == REG_DEAD
1472           || REG_NOTE_KIND (x) == REG_UNUSED)
1473         reg_weight--;
1474     }
1475   
1476   INSN_REG_WEIGHT (insn) = reg_weight;
1477 }
1478
1479 /* Move insns that became ready to fire from queue to ready list.  */
1480
1481 static void
1482 queue_to_ready (struct ready_list *ready)
1483 {
1484   rtx insn;
1485   rtx link;
1486   rtx skip_insn;
1487
1488   q_ptr = NEXT_Q (q_ptr);
1489
1490   if (dbg_cnt (sched_insn) == false)
1491     /* If debug counter is activated do not requeue insn next after
1492        last_scheduled_insn.  */
1493     skip_insn = next_nonnote_insn (last_scheduled_insn);
1494   else
1495     skip_insn = NULL_RTX;
1496
1497   /* Add all pending insns that can be scheduled without stalls to the
1498      ready list.  */
1499   for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
1500     {
1501       insn = XEXP (link, 0);
1502       q_size -= 1;
1503
1504       if (sched_verbose >= 2)
1505         fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1506                  (*current_sched_info->print_insn) (insn, 0));
1507
1508       /* If the ready list is full, delay the insn for 1 cycle.
1509          See the comment in schedule_block for the rationale.  */
1510       if (!reload_completed
1511           && ready->n_ready > MAX_SCHED_READY_INSNS
1512           && !SCHED_GROUP_P (insn)
1513           && insn != skip_insn)
1514         {
1515           if (sched_verbose >= 2)
1516             fprintf (sched_dump, "requeued because ready full\n");
1517           queue_insn (insn, 1);
1518         }
1519       else
1520         {
1521           ready_add (ready, insn, false);
1522           if (sched_verbose >= 2)
1523             fprintf (sched_dump, "moving to ready without stalls\n");
1524         }
1525     }
1526   free_INSN_LIST_list (&insn_queue[q_ptr]);
1527
1528   /* If there are no ready insns, stall until one is ready and add all
1529      of the pending insns at that point to the ready list.  */
1530   if (ready->n_ready == 0)
1531     {
1532       int stalls;
1533
1534       for (stalls = 1; stalls <= max_insn_queue_index; stalls++)
1535         {
1536           if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
1537             {
1538               for (; link; link = XEXP (link, 1))
1539                 {
1540                   insn = XEXP (link, 0);
1541                   q_size -= 1;
1542
1543                   if (sched_verbose >= 2)
1544                     fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1545                              (*current_sched_info->print_insn) (insn, 0));
1546
1547                   ready_add (ready, insn, false);
1548                   if (sched_verbose >= 2)
1549                     fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
1550                 }
1551               free_INSN_LIST_list (&insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]);
1552
1553               advance_one_cycle ();
1554
1555               break;
1556             }
1557
1558           advance_one_cycle ();
1559         }
1560
1561       q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
1562       clock_var += stalls;
1563     }
1564 }
1565
1566 /* Used by early_queue_to_ready.  Determines whether it is "ok" to
1567    prematurely move INSN from the queue to the ready list.  Currently, 
1568    if a target defines the hook 'is_costly_dependence', this function 
1569    uses the hook to check whether there exist any dependences which are
1570    considered costly by the target, between INSN and other insns that 
1571    have already been scheduled.  Dependences are checked up to Y cycles
1572    back, with default Y=1; The flag -fsched-stalled-insns-dep=Y allows
1573    controlling this value. 
1574    (Other considerations could be taken into account instead (or in 
1575    addition) depending on user flags and target hooks.  */
1576
1577 static bool 
1578 ok_for_early_queue_removal (rtx insn)
1579 {
1580   int n_cycles;
1581   rtx prev_insn = last_scheduled_insn;
1582
1583   if (targetm.sched.is_costly_dependence)
1584     {
1585       for (n_cycles = flag_sched_stalled_insns_dep; n_cycles; n_cycles--)
1586         {
1587           for ( ; prev_insn; prev_insn = PREV_INSN (prev_insn))
1588             {
1589               int cost;
1590
1591               if (!NOTE_P (prev_insn))
1592                 {
1593                   dep_link_t dep_link;
1594
1595                   dep_link = (find_link_by_con_in_deps_list
1596                               (INSN_FORW_DEPS (prev_insn), insn));
1597
1598                   if (dep_link)
1599                     {
1600                       dep_t dep = DEP_LINK_DEP (dep_link);
1601
1602                       cost = dep_cost (dep);
1603
1604                       if (targetm.sched.is_costly_dependence (dep, cost,
1605                                 flag_sched_stalled_insns_dep - n_cycles))
1606                         return false;
1607                     }
1608                 }
1609
1610               if (GET_MODE (prev_insn) == TImode) /* end of dispatch group */
1611                 break;
1612             }
1613
1614           if (!prev_insn) 
1615             break;
1616           prev_insn = PREV_INSN (prev_insn);     
1617         }
1618     }
1619
1620   return true;
1621 }
1622
1623
1624 /* Remove insns from the queue, before they become "ready" with respect
1625    to FU latency considerations.  */
1626
1627 static int 
1628 early_queue_to_ready (state_t state, struct ready_list *ready)
1629 {
1630   rtx insn;
1631   rtx link;
1632   rtx next_link;
1633   rtx prev_link;
1634   bool move_to_ready;
1635   int cost;
1636   state_t temp_state = alloca (dfa_state_size);
1637   int stalls;
1638   int insns_removed = 0;
1639
1640   /*
1641      Flag '-fsched-stalled-insns=X' determines the aggressiveness of this 
1642      function: 
1643
1644      X == 0: There is no limit on how many queued insns can be removed          
1645              prematurely.  (flag_sched_stalled_insns = -1).
1646
1647      X >= 1: Only X queued insns can be removed prematurely in each 
1648              invocation.  (flag_sched_stalled_insns = X).
1649
1650      Otherwise: Early queue removal is disabled.
1651          (flag_sched_stalled_insns = 0)
1652   */
1653
1654   if (! flag_sched_stalled_insns)   
1655     return 0;
1656
1657   for (stalls = 0; stalls <= max_insn_queue_index; stalls++)
1658     {
1659       if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
1660         {
1661           if (sched_verbose > 6)
1662             fprintf (sched_dump, ";; look at index %d + %d\n", q_ptr, stalls);
1663
1664           prev_link = 0;
1665           while (link)
1666             {
1667               next_link = XEXP (link, 1);
1668               insn = XEXP (link, 0);
1669               if (insn && sched_verbose > 6)
1670                 print_rtl_single (sched_dump, insn);
1671
1672               memcpy (temp_state, state, dfa_state_size);
1673               if (recog_memoized (insn) < 0) 
1674                 /* non-negative to indicate that it's not ready
1675                    to avoid infinite Q->R->Q->R... */
1676                 cost = 0;
1677               else
1678                 cost = state_transition (temp_state, insn);
1679
1680               if (sched_verbose >= 6)
1681                 fprintf (sched_dump, "transition cost = %d\n", cost);
1682
1683               move_to_ready = false;
1684               if (cost < 0) 
1685                 {
1686                   move_to_ready = ok_for_early_queue_removal (insn);
1687                   if (move_to_ready == true)
1688                     {
1689                       /* move from Q to R */
1690                       q_size -= 1;
1691                       ready_add (ready, insn, false);
1692
1693                       if (prev_link)   
1694                         XEXP (prev_link, 1) = next_link;
1695                       else
1696                         insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = next_link;
1697
1698                       free_INSN_LIST_node (link);
1699
1700                       if (sched_verbose >= 2)
1701                         fprintf (sched_dump, ";;\t\tEarly Q-->Ready: insn %s\n",
1702                                  (*current_sched_info->print_insn) (insn, 0));
1703
1704                       insns_removed++;
1705                       if (insns_removed == flag_sched_stalled_insns)
1706                         /* Remove no more than flag_sched_stalled_insns insns
1707                            from Q at a time.  */
1708                         return insns_removed;
1709                     }
1710                 }
1711
1712               if (move_to_ready == false)
1713                 prev_link = link;
1714
1715               link = next_link;
1716             } /* while link */
1717         } /* if link */    
1718
1719     } /* for stalls.. */
1720
1721   return insns_removed; 
1722 }
1723
1724
1725 /* Print the ready list for debugging purposes.  Callable from debugger.  */
1726
1727 static void
1728 debug_ready_list (struct ready_list *ready)
1729 {
1730   rtx *p;
1731   int i;
1732
1733   if (ready->n_ready == 0)
1734     {
1735       fprintf (sched_dump, "\n");
1736       return;
1737     }
1738
1739   p = ready_lastpos (ready);
1740   for (i = 0; i < ready->n_ready; i++)
1741     fprintf (sched_dump, "  %s", (*current_sched_info->print_insn) (p[i], 0));
1742   fprintf (sched_dump, "\n");
1743 }
1744
1745 /* Search INSN for REG_SAVE_NOTE note pairs for
1746    NOTE_INSN_EHREGION_{BEG,END}; and convert them back into
1747    NOTEs.  The REG_SAVE_NOTE note following first one is contains the
1748    saved value for NOTE_BLOCK_NUMBER which is useful for
1749    NOTE_INSN_EH_REGION_{BEG,END} NOTEs.  */
1750
1751 static void
1752 reemit_notes (rtx insn)
1753 {
1754   rtx note, last = insn;
1755
1756   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1757     {
1758       if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
1759         {
1760           enum insn_note note_type = INTVAL (XEXP (note, 0));
1761
1762           last = emit_note_before (note_type, last);
1763           remove_note (insn, note);
1764         }
1765     }
1766 }
1767
1768 /* Move INSN.  Reemit notes if needed.  Update CFG, if needed.  */
1769 static void
1770 move_insn (rtx insn)
1771 {
1772   rtx last = last_scheduled_insn;
1773
1774   if (PREV_INSN (insn) != last)
1775     {
1776       basic_block bb;
1777       rtx note;
1778       int jump_p = 0;
1779
1780       bb = BLOCK_FOR_INSN (insn);
1781  
1782       /* BB_HEAD is either LABEL or NOTE.  */
1783       gcc_assert (BB_HEAD (bb) != insn);      
1784
1785       if (BB_END (bb) == insn)
1786         /* If this is last instruction in BB, move end marker one
1787            instruction up.  */
1788         {
1789           /* Jumps are always placed at the end of basic block.  */
1790           jump_p = control_flow_insn_p (insn);
1791
1792           gcc_assert (!jump_p
1793                       || ((current_sched_info->flags & SCHED_RGN)
1794                           && IS_SPECULATION_BRANCHY_CHECK_P (insn))
1795                       || (current_sched_info->flags & SCHED_EBB));
1796           
1797           gcc_assert (BLOCK_FOR_INSN (PREV_INSN (insn)) == bb);
1798
1799           BB_END (bb) = PREV_INSN (insn);
1800         }
1801
1802       gcc_assert (BB_END (bb) != last);
1803
1804       if (jump_p)
1805         /* We move the block note along with jump.  */
1806         {
1807           /* NT is needed for assertion below.  */
1808           rtx nt = current_sched_info->next_tail;
1809
1810           note = NEXT_INSN (insn);
1811           while (NOTE_NOT_BB_P (note) && note != nt)
1812             note = NEXT_INSN (note);
1813
1814           if (note != nt
1815               && (LABEL_P (note)
1816                   || BARRIER_P (note)))
1817             note = NEXT_INSN (note);
1818       
1819           gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
1820         }
1821       else
1822         note = insn;
1823
1824       NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (note);
1825       PREV_INSN (NEXT_INSN (note)) = PREV_INSN (insn);
1826
1827       NEXT_INSN (note) = NEXT_INSN (last);
1828       PREV_INSN (NEXT_INSN (last)) = note;
1829
1830       NEXT_INSN (last) = insn;
1831       PREV_INSN (insn) = last;
1832
1833       bb = BLOCK_FOR_INSN (last);
1834
1835       if (jump_p)
1836         {
1837           fix_jump_move (insn);
1838
1839           if (BLOCK_FOR_INSN (insn) != bb)
1840             move_block_after_check (insn);
1841
1842           gcc_assert (BB_END (bb) == last);
1843         }
1844
1845       set_block_for_insn (insn, bb);    
1846       df_insn_change_bb (insn);
1847   
1848       /* Update BB_END, if needed.  */
1849       if (BB_END (bb) == last)
1850         BB_END (bb) = insn;  
1851     }
1852   
1853   reemit_notes (insn);
1854
1855   SCHED_GROUP_P (insn) = 0;  
1856 }
1857
1858 /* The following structure describe an entry of the stack of choices.  */
1859 struct choice_entry
1860 {
1861   /* Ordinal number of the issued insn in the ready queue.  */
1862   int index;
1863   /* The number of the rest insns whose issues we should try.  */
1864   int rest;
1865   /* The number of issued essential insns.  */
1866   int n;
1867   /* State after issuing the insn.  */
1868   state_t state;
1869 };
1870
1871 /* The following array is used to implement a stack of choices used in
1872    function max_issue.  */
1873 static struct choice_entry *choice_stack;
1874
1875 /* The following variable value is number of essential insns issued on
1876    the current cycle.  An insn is essential one if it changes the
1877    processors state.  */
1878 static int cycle_issued_insns;
1879
1880 /* The following variable value is maximal number of tries of issuing
1881    insns for the first cycle multipass insn scheduling.  We define
1882    this value as constant*(DFA_LOOKAHEAD**ISSUE_RATE).  We would not
1883    need this constraint if all real insns (with non-negative codes)
1884    had reservations because in this case the algorithm complexity is
1885    O(DFA_LOOKAHEAD**ISSUE_RATE).  Unfortunately, the dfa descriptions
1886    might be incomplete and such insn might occur.  For such
1887    descriptions, the complexity of algorithm (without the constraint)
1888    could achieve DFA_LOOKAHEAD ** N , where N is the queue length.  */
1889 static int max_lookahead_tries;
1890
1891 /* The following value is value of hook
1892    `first_cycle_multipass_dfa_lookahead' at the last call of
1893    `max_issue'.  */
1894 static int cached_first_cycle_multipass_dfa_lookahead = 0;
1895
1896 /* The following value is value of `issue_rate' at the last call of
1897    `sched_init'.  */
1898 static int cached_issue_rate = 0;
1899
1900 /* The following function returns maximal (or close to maximal) number
1901    of insns which can be issued on the same cycle and one of which
1902    insns is insns with the best rank (the first insn in READY).  To
1903    make this function tries different samples of ready insns.  READY
1904    is current queue `ready'.  Global array READY_TRY reflects what
1905    insns are already issued in this try.  MAX_POINTS is the sum of points
1906    of all instructions in READY.  The function stops immediately,
1907    if it reached the such a solution, that all instruction can be issued.
1908    INDEX will contain index of the best insn in READY.  The following
1909    function is used only for first cycle multipass scheduling.  */
1910 static int
1911 max_issue (struct ready_list *ready, int *index, int max_points)
1912 {
1913   int n, i, all, n_ready, best, delay, tries_num, points = -1;
1914   struct choice_entry *top;
1915   rtx insn;
1916
1917   best = 0;
1918   memcpy (choice_stack->state, curr_state, dfa_state_size);
1919   top = choice_stack;
1920   top->rest = cached_first_cycle_multipass_dfa_lookahead;
1921   top->n = 0;
1922   n_ready = ready->n_ready;
1923   for (all = i = 0; i < n_ready; i++)
1924     if (!ready_try [i])
1925       all++;
1926   i = 0;
1927   tries_num = 0;
1928   for (;;)
1929     {
1930       if (top->rest == 0 || i >= n_ready)
1931         {
1932           if (top == choice_stack)
1933             break;
1934           if (best < top - choice_stack && ready_try [0])
1935             {
1936               best = top - choice_stack;
1937               *index = choice_stack [1].index;
1938               points = top->n;
1939               if (top->n == max_points || best == all)
1940                 break;
1941             }
1942           i = top->index;
1943           ready_try [i] = 0;
1944           top--;
1945           memcpy (curr_state, top->state, dfa_state_size);
1946         }
1947       else if (!ready_try [i])
1948         {
1949           tries_num++;
1950           if (tries_num > max_lookahead_tries)
1951             break;
1952           insn = ready_element (ready, i);
1953           delay = state_transition (curr_state, insn);
1954           if (delay < 0)
1955             {
1956               if (state_dead_lock_p (curr_state))
1957                 top->rest = 0;
1958               else
1959                 top->rest--;
1960               n = top->n;
1961               if (memcmp (top->state, curr_state, dfa_state_size) != 0)
1962                 n += ISSUE_POINTS (insn);
1963               top++;
1964               top->rest = cached_first_cycle_multipass_dfa_lookahead;
1965               top->index = i;
1966               top->n = n;
1967               memcpy (top->state, curr_state, dfa_state_size);
1968               ready_try [i] = 1;
1969               i = -1;
1970             }
1971         }
1972       i++;
1973     }
1974   while (top != choice_stack)
1975     {
1976       ready_try [top->index] = 0;
1977       top--;
1978     }
1979   memcpy (curr_state, choice_stack->state, dfa_state_size);  
1980
1981   if (sched_verbose >= 4)    
1982     fprintf (sched_dump, ";;\t\tChoosed insn : %s; points: %d/%d\n",
1983              (*current_sched_info->print_insn) (ready_element (ready, *index),
1984                                                 0), 
1985              points, max_points);
1986   
1987   return best;
1988 }
1989
1990 /* The following function chooses insn from READY and modifies
1991    *N_READY and READY.  The following function is used only for first
1992    cycle multipass scheduling.
1993    Return:
1994    -1 if cycle should be advanced,
1995    0 if INSN_PTR is set to point to the desirable insn,
1996    1 if choose_ready () should be restarted without advancing the cycle.  */
1997 static int
1998 choose_ready (struct ready_list *ready, rtx *insn_ptr)
1999 {
2000   int lookahead;
2001
2002   if (dbg_cnt (sched_insn) == false)
2003     {
2004       rtx insn;
2005
2006       insn = next_nonnote_insn (last_scheduled_insn);
2007
2008       if (QUEUE_INDEX (insn) == QUEUE_READY)
2009         /* INSN is in the ready_list.  */
2010         {
2011           ready_remove_insn (insn);
2012           *insn_ptr = insn;
2013           return 0;
2014         }
2015
2016       /* INSN is in the queue.  Advance cycle to move it to the ready list.  */
2017       return -1;
2018     }
2019
2020   lookahead = 0;
2021
2022   if (targetm.sched.first_cycle_multipass_dfa_lookahead)
2023     lookahead = targetm.sched.first_cycle_multipass_dfa_lookahead ();
2024   if (lookahead <= 0 || SCHED_GROUP_P (ready_element (ready, 0)))
2025     {
2026       *insn_ptr = ready_remove_first (ready);
2027       return 0;
2028     }
2029   else
2030     {
2031       /* Try to choose the better insn.  */
2032       int index = 0, i, n;
2033       rtx insn;
2034       int more_issue, max_points, try_data = 1, try_control = 1;
2035       
2036       if (cached_first_cycle_multipass_dfa_lookahead != lookahead)
2037         {
2038           cached_first_cycle_multipass_dfa_lookahead = lookahead;
2039           max_lookahead_tries = 100;
2040           for (i = 0; i < issue_rate; i++)
2041             max_lookahead_tries *= lookahead;
2042         }
2043       insn = ready_element (ready, 0);
2044       if (INSN_CODE (insn) < 0)
2045         {
2046           *insn_ptr = ready_remove_first (ready);
2047           return 0;
2048         }
2049
2050       if (spec_info
2051           && spec_info->flags & (PREFER_NON_DATA_SPEC
2052                                  | PREFER_NON_CONTROL_SPEC))
2053         {
2054           for (i = 0, n = ready->n_ready; i < n; i++)
2055             {
2056               rtx x;
2057               ds_t s;
2058
2059               x = ready_element (ready, i);
2060               s = TODO_SPEC (x);
2061               
2062               if (spec_info->flags & PREFER_NON_DATA_SPEC
2063                   && !(s & DATA_SPEC))
2064                 {                 
2065                   try_data = 0;
2066                   if (!(spec_info->flags & PREFER_NON_CONTROL_SPEC)
2067                       || !try_control)
2068                     break;
2069                 }
2070               
2071               if (spec_info->flags & PREFER_NON_CONTROL_SPEC
2072                   && !(s & CONTROL_SPEC))
2073                 {
2074                   try_control = 0;
2075                   if (!(spec_info->flags & PREFER_NON_DATA_SPEC) || !try_data)
2076                     break;
2077                 }
2078             }
2079         }
2080
2081       if ((!try_data && (TODO_SPEC (insn) & DATA_SPEC))
2082           || (!try_control && (TODO_SPEC (insn) & CONTROL_SPEC))
2083           || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard_spec
2084               && !targetm.sched.first_cycle_multipass_dfa_lookahead_guard_spec
2085               (insn)))
2086         /* Discard speculative instruction that stands first in the ready
2087            list.  */
2088         {
2089           change_queue_index (insn, 1);
2090           return 1;
2091         }
2092
2093       max_points = ISSUE_POINTS (insn);
2094       more_issue = issue_rate - cycle_issued_insns - 1;
2095
2096       for (i = 1; i < ready->n_ready; i++)
2097         {
2098           insn = ready_element (ready, i);
2099           ready_try [i]
2100             = (INSN_CODE (insn) < 0
2101                || (!try_data && (TODO_SPEC (insn) & DATA_SPEC))
2102                || (!try_control && (TODO_SPEC (insn) & CONTROL_SPEC))
2103                || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard
2104                    && !targetm.sched.first_cycle_multipass_dfa_lookahead_guard
2105                    (insn)));
2106
2107           if (!ready_try [i] && more_issue-- > 0)
2108             max_points += ISSUE_POINTS (insn);
2109         }
2110
2111       if (max_issue (ready, &index, max_points) == 0)
2112         {
2113           *insn_ptr = ready_remove_first (ready);
2114           return 0;
2115         }
2116       else
2117         {
2118           *insn_ptr = ready_remove (ready, index);
2119           return 0;
2120         }
2121     }
2122 }
2123
2124 /* Use forward list scheduling to rearrange insns of block pointed to by
2125    TARGET_BB, possibly bringing insns from subsequent blocks in the same
2126    region.  */
2127
2128 void
2129 schedule_block (basic_block *target_bb, int rgn_n_insns1)
2130 {
2131   struct ready_list ready;
2132   int i, first_cycle_insn_p;
2133   int can_issue_more;
2134   state_t temp_state = NULL;  /* It is used for multipass scheduling.  */
2135   int sort_p, advance, start_clock_var;
2136
2137   /* Head/tail info for this block.  */
2138   rtx prev_head = current_sched_info->prev_head;
2139   rtx next_tail = current_sched_info->next_tail;
2140   rtx head = NEXT_INSN (prev_head);
2141   rtx tail = PREV_INSN (next_tail);
2142
2143   /* We used to have code to avoid getting parameters moved from hard
2144      argument registers into pseudos.
2145
2146      However, it was removed when it proved to be of marginal benefit
2147      and caused problems because schedule_block and compute_forward_dependences
2148      had different notions of what the "head" insn was.  */
2149
2150   gcc_assert (head != tail || INSN_P (head));
2151
2152   added_recovery_block_p = false;
2153
2154   /* Debug info.  */
2155   if (sched_verbose)
2156     dump_new_block_header (0, *target_bb, head, tail);
2157
2158   state_reset (curr_state);
2159
2160   /* Allocate the ready list.  */
2161   readyp = &ready;
2162   ready.vec = NULL;
2163   ready_try = NULL;
2164   choice_stack = NULL;
2165
2166   rgn_n_insns = -1;
2167   extend_ready (rgn_n_insns1 + 1);
2168
2169   ready.first = ready.veclen - 1;
2170   ready.n_ready = 0;
2171
2172   /* It is used for first cycle multipass scheduling.  */
2173   temp_state = alloca (dfa_state_size);
2174
2175   if (targetm.sched.md_init)
2176     targetm.sched.md_init (sched_dump, sched_verbose, ready.veclen);
2177
2178   /* We start inserting insns after PREV_HEAD.  */
2179   last_scheduled_insn = prev_head;
2180
2181   gcc_assert (NOTE_P (last_scheduled_insn)
2182               && BLOCK_FOR_INSN (last_scheduled_insn) == *target_bb);
2183
2184   /* Initialize INSN_QUEUE.  Q_SIZE is the total number of insns in the
2185      queue.  */
2186   q_ptr = 0;
2187   q_size = 0;
2188
2189   insn_queue = alloca ((max_insn_queue_index + 1) * sizeof (rtx));
2190   memset (insn_queue, 0, (max_insn_queue_index + 1) * sizeof (rtx));
2191
2192   /* Start just before the beginning of time.  */
2193   clock_var = -1;
2194
2195   /* We need queue and ready lists and clock_var be initialized 
2196      in try_ready () (which is called through init_ready_list ()).  */
2197   (*current_sched_info->init_ready_list) ();
2198
2199   /* The algorithm is O(n^2) in the number of ready insns at any given
2200      time in the worst case.  Before reload we are more likely to have
2201      big lists so truncate them to a reasonable size.  */
2202   if (!reload_completed && ready.n_ready > MAX_SCHED_READY_INSNS)
2203     {
2204       ready_sort (&ready);
2205
2206       /* Find first free-standing insn past MAX_SCHED_READY_INSNS.  */
2207       for (i = MAX_SCHED_READY_INSNS; i < ready.n_ready; i++)
2208         if (!SCHED_GROUP_P (ready_element (&ready, i)))
2209           break;
2210
2211       if (sched_verbose >= 2)
2212         {
2213           fprintf (sched_dump,
2214                    ";;\t\tReady list on entry: %d insns\n", ready.n_ready);
2215           fprintf (sched_dump,
2216                    ";;\t\t before reload => truncated to %d insns\n", i);
2217         }
2218
2219       /* Delay all insns past it for 1 cycle.  If debug counter is
2220          activated make an exception for the insn right after
2221          last_scheduled_insn.  */
2222       {
2223         rtx skip_insn;
2224
2225         if (dbg_cnt (sched_insn) == false)
2226           skip_insn = next_nonnote_insn (last_scheduled_insn);
2227         else
2228           skip_insn = NULL_RTX;
2229
2230         while (i < ready.n_ready)
2231           {
2232             rtx insn;
2233
2234             insn = ready_remove (&ready, i);
2235
2236             if (insn != skip_insn)
2237               queue_insn (insn, 1);
2238           }
2239       }
2240     }
2241
2242   /* Now we can restore basic block notes and maintain precise cfg.  */
2243   restore_bb_notes (*target_bb);
2244
2245   last_clock_var = -1;
2246
2247   advance = 0;
2248
2249   sort_p = TRUE;
2250   /* Loop until all the insns in BB are scheduled.  */
2251   while ((*current_sched_info->schedule_more_p) ())
2252     {
2253       do
2254         {
2255           start_clock_var = clock_var;
2256
2257           clock_var++;
2258
2259           advance_one_cycle ();
2260
2261           /* Add to the ready list all pending insns that can be issued now.
2262              If there are no ready insns, increment clock until one
2263              is ready and add all pending insns at that point to the ready
2264              list.  */
2265           queue_to_ready (&ready);
2266
2267           gcc_assert (ready.n_ready);
2268
2269           if (sched_verbose >= 2)
2270             {
2271               fprintf (sched_dump, ";;\t\tReady list after queue_to_ready:  ");
2272               debug_ready_list (&ready);
2273             }
2274           advance -= clock_var - start_clock_var;
2275         }
2276       while (advance > 0);
2277
2278       if (sort_p)
2279         {
2280           /* Sort the ready list based on priority.  */
2281           ready_sort (&ready);
2282
2283           if (sched_verbose >= 2)
2284             {
2285               fprintf (sched_dump, ";;\t\tReady list after ready_sort:  ");
2286               debug_ready_list (&ready);
2287             }
2288         }
2289
2290       /* Allow the target to reorder the list, typically for
2291          better instruction bundling.  */
2292       if (sort_p && targetm.sched.reorder
2293           && (ready.n_ready == 0
2294               || !SCHED_GROUP_P (ready_element (&ready, 0))))
2295         can_issue_more =
2296           targetm.sched.reorder (sched_dump, sched_verbose,
2297                                  ready_lastpos (&ready),
2298                                  &ready.n_ready, clock_var);
2299       else
2300         can_issue_more = issue_rate;
2301
2302       first_cycle_insn_p = 1;
2303       cycle_issued_insns = 0;
2304       for (;;)
2305         {
2306           rtx insn;
2307           int cost;
2308           bool asm_p = false;
2309
2310           if (sched_verbose >= 2)
2311             {
2312               fprintf (sched_dump, ";;\tReady list (t = %3d):  ",
2313                        clock_var);
2314               debug_ready_list (&ready);
2315             }
2316
2317           if (ready.n_ready == 0 
2318               && can_issue_more 
2319               && reload_completed) 
2320             {
2321               /* Allow scheduling insns directly from the queue in case
2322                  there's nothing better to do (ready list is empty) but
2323                  there are still vacant dispatch slots in the current cycle.  */
2324               if (sched_verbose >= 6)
2325                 fprintf (sched_dump,";;\t\tSecond chance\n");
2326               memcpy (temp_state, curr_state, dfa_state_size);
2327               if (early_queue_to_ready (temp_state, &ready))
2328                 ready_sort (&ready);
2329             }
2330
2331           if (ready.n_ready == 0 || !can_issue_more
2332               || state_dead_lock_p (curr_state)
2333               || !(*current_sched_info->schedule_more_p) ())
2334             break;
2335
2336           /* Select and remove the insn from the ready list.  */
2337           if (sort_p)
2338             {
2339               int res;
2340
2341               insn = NULL_RTX;
2342               res = choose_ready (&ready, &insn);
2343
2344               if (res < 0)
2345                 /* Finish cycle.  */
2346                 break;
2347               if (res > 0)
2348                 /* Restart choose_ready ().  */
2349                 continue;
2350
2351               gcc_assert (insn != NULL_RTX);
2352             }
2353           else
2354             insn = ready_remove_first (&ready);
2355
2356           if (targetm.sched.dfa_new_cycle
2357               && targetm.sched.dfa_new_cycle (sched_dump, sched_verbose,
2358                                               insn, last_clock_var,
2359                                               clock_var, &sort_p))
2360             /* SORT_P is used by the target to override sorting
2361                of the ready list.  This is needed when the target
2362                has modified its internal structures expecting that
2363                the insn will be issued next.  As we need the insn
2364                to have the highest priority (so it will be returned by
2365                the ready_remove_first call above), we invoke
2366                ready_add (&ready, insn, true).
2367                But, still, there is one issue: INSN can be later 
2368                discarded by scheduler's front end through 
2369                current_sched_info->can_schedule_ready_p, hence, won't
2370                be issued next.  */ 
2371             {
2372               ready_add (&ready, insn, true);
2373               break;
2374             }
2375
2376           sort_p = TRUE;
2377           memcpy (temp_state, curr_state, dfa_state_size);
2378           if (recog_memoized (insn) < 0)
2379             {
2380               asm_p = (GET_CODE (PATTERN (insn)) == ASM_INPUT
2381                        || asm_noperands (PATTERN (insn)) >= 0);
2382               if (!first_cycle_insn_p && asm_p)
2383                 /* This is asm insn which is tryed to be issued on the
2384                    cycle not first.  Issue it on the next cycle.  */
2385                 cost = 1;
2386               else
2387                 /* A USE insn, or something else we don't need to
2388                    understand.  We can't pass these directly to
2389                    state_transition because it will trigger a
2390                    fatal error for unrecognizable insns.  */
2391                 cost = 0;
2392             }
2393           else
2394             {
2395               cost = state_transition (temp_state, insn);
2396               if (cost < 0)
2397                 cost = 0;
2398               else if (cost == 0)
2399                 cost = 1;
2400             }
2401
2402           if (cost >= 1)
2403             {
2404               queue_insn (insn, cost);
2405               if (SCHED_GROUP_P (insn))
2406                 {
2407                   advance = cost;
2408                   break;
2409                 }
2410  
2411               continue;
2412             }
2413
2414           if (current_sched_info->can_schedule_ready_p
2415               && ! (*current_sched_info->can_schedule_ready_p) (insn))
2416             /* We normally get here only if we don't want to move
2417                insn from the split block.  */
2418             {
2419               TODO_SPEC (insn) = (TODO_SPEC (insn) & ~SPECULATIVE) | HARD_DEP;
2420               continue;
2421             }
2422
2423           /* DECISION is made.  */      
2424   
2425           if (TODO_SPEC (insn) & SPECULATIVE)
2426             generate_recovery_code (insn);
2427
2428           if (control_flow_insn_p (last_scheduled_insn)      
2429               /* This is used to switch basic blocks by request
2430                  from scheduler front-end (actually, sched-ebb.c only).
2431                  This is used to process blocks with single fallthru
2432                  edge.  If succeeding block has jump, it [jump] will try
2433                  move at the end of current bb, thus corrupting CFG.  */
2434               || current_sched_info->advance_target_bb (*target_bb, insn))
2435             {
2436               *target_bb = current_sched_info->advance_target_bb
2437                 (*target_bb, 0);
2438               
2439               if (sched_verbose)
2440                 {
2441                   rtx x;
2442
2443                   x = next_real_insn (last_scheduled_insn);
2444                   gcc_assert (x);
2445                   dump_new_block_header (1, *target_bb, x, tail);
2446                 }
2447
2448               last_scheduled_insn = bb_note (*target_bb);
2449             }
2450  
2451           /* Update counters, etc in the scheduler's front end.  */
2452           (*current_sched_info->begin_schedule_ready) (insn,
2453                                                        last_scheduled_insn);
2454  
2455           move_insn (insn);
2456           last_scheduled_insn = insn;
2457           
2458           if (memcmp (curr_state, temp_state, dfa_state_size) != 0)
2459             {
2460               cycle_issued_insns++;
2461               memcpy (curr_state, temp_state, dfa_state_size);
2462             }
2463
2464           if (targetm.sched.variable_issue)
2465             can_issue_more =
2466               targetm.sched.variable_issue (sched_dump, sched_verbose,
2467                                                insn, can_issue_more);
2468           /* A naked CLOBBER or USE generates no instruction, so do
2469              not count them against the issue rate.  */
2470           else if (GET_CODE (PATTERN (insn)) != USE
2471                    && GET_CODE (PATTERN (insn)) != CLOBBER)
2472             can_issue_more--;
2473
2474           advance = schedule_insn (insn);
2475
2476           /* After issuing an asm insn we should start a new cycle.  */
2477           if (advance == 0 && asm_p)
2478             advance = 1;
2479           if (advance != 0)
2480             break;
2481
2482           first_cycle_insn_p = 0;
2483
2484           /* Sort the ready list based on priority.  This must be
2485              redone here, as schedule_insn may have readied additional
2486              insns that will not be sorted correctly.  */
2487           if (ready.n_ready > 0)
2488             ready_sort (&ready);
2489
2490           if (targetm.sched.reorder2
2491               && (ready.n_ready == 0
2492                   || !SCHED_GROUP_P (ready_element (&ready, 0))))
2493             {
2494               can_issue_more =
2495                 targetm.sched.reorder2 (sched_dump, sched_verbose,
2496                                         ready.n_ready
2497                                         ? ready_lastpos (&ready) : NULL,
2498                                         &ready.n_ready, clock_var);
2499             }
2500         }
2501     }
2502
2503   /* Debug info.  */
2504   if (sched_verbose)
2505     {
2506       fprintf (sched_dump, ";;\tReady list (final):  ");
2507       debug_ready_list (&ready);
2508     }
2509
2510   if (current_sched_info->queue_must_finish_empty)
2511     /* Sanity check -- queue must be empty now.  Meaningless if region has
2512        multiple bbs.  */
2513     gcc_assert (!q_size && !ready.n_ready);
2514   else 
2515     {
2516       /* We must maintain QUEUE_INDEX between blocks in region.  */
2517       for (i = ready.n_ready - 1; i >= 0; i--)
2518         {
2519           rtx x;
2520           
2521           x = ready_element (&ready, i);
2522           QUEUE_INDEX (x) = QUEUE_NOWHERE;
2523           TODO_SPEC (x) = (TODO_SPEC (x) & ~SPECULATIVE) | HARD_DEP;
2524         }
2525
2526       if (q_size)   
2527         for (i = 0; i <= max_insn_queue_index; i++)
2528           {
2529             rtx link;
2530             for (link = insn_queue[i]; link; link = XEXP (link, 1))
2531               {
2532                 rtx x;
2533
2534                 x = XEXP (link, 0);
2535                 QUEUE_INDEX (x) = QUEUE_NOWHERE;
2536                 TODO_SPEC (x) = (TODO_SPEC (x) & ~SPECULATIVE) | HARD_DEP;
2537               }
2538             free_INSN_LIST_list (&insn_queue[i]);
2539           }
2540     }
2541
2542   if (!current_sched_info->queue_must_finish_empty
2543       || added_recovery_block_p)
2544     {
2545       /* INSN_TICK (minimum clock tick at which the insn becomes
2546          ready) may be not correct for the insn in the subsequent
2547          blocks of the region.  We should use a correct value of
2548          `clock_var' or modify INSN_TICK.  It is better to keep
2549          clock_var value equal to 0 at the start of a basic block.
2550          Therefore we modify INSN_TICK here.  */
2551       fix_inter_tick (NEXT_INSN (prev_head), last_scheduled_insn);
2552     }
2553
2554   if (targetm.sched.md_finish)
2555     targetm.sched.md_finish (sched_dump, sched_verbose);
2556
2557   /* Update head/tail boundaries.  */
2558   head = NEXT_INSN (prev_head);
2559   tail = last_scheduled_insn;
2560
2561   /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
2562      previously found among the insns.  Insert them at the beginning
2563      of the insns.  */
2564   if (note_list != 0)
2565     {
2566       basic_block head_bb = BLOCK_FOR_INSN (head);
2567       rtx note_head = note_list;
2568
2569       while (PREV_INSN (note_head))
2570         {
2571           set_block_for_insn (note_head, head_bb);
2572           note_head = PREV_INSN (note_head);
2573         }
2574       /* In the above cycle we've missed this note:  */
2575       set_block_for_insn (note_head, head_bb);
2576
2577       PREV_INSN (note_head) = PREV_INSN (head);
2578       NEXT_INSN (PREV_INSN (head)) = note_head;
2579       PREV_INSN (head) = note_list;
2580       NEXT_INSN (note_list) = head;
2581       head = note_head;
2582     }
2583
2584   /* Debugging.  */
2585   if (sched_verbose)
2586     {
2587       fprintf (sched_dump, ";;   total time = %d\n;;   new head = %d\n",
2588                clock_var, INSN_UID (head));
2589       fprintf (sched_dump, ";;   new tail = %d\n\n",
2590                INSN_UID (tail));
2591     }
2592
2593   current_sched_info->head = head;
2594   current_sched_info->tail = tail;
2595
2596   free (ready.vec);
2597
2598   free (ready_try);
2599   for (i = 0; i <= rgn_n_insns; i++)
2600     free (choice_stack [i].state);
2601   free (choice_stack);
2602 }
2603 \f
2604 /* Set_priorities: compute priority of each insn in the block.  */
2605
2606 int
2607 set_priorities (rtx head, rtx tail)
2608 {
2609   rtx insn;
2610   int n_insn;
2611   int sched_max_insns_priority = 
2612         current_sched_info->sched_max_insns_priority;
2613   rtx prev_head;
2614
2615   if (head == tail && (! INSN_P (head)))
2616     return 0;
2617
2618   n_insn = 0;
2619
2620   prev_head = PREV_INSN (head);
2621   for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
2622     {
2623       if (!INSN_P (insn))
2624         continue;
2625
2626       n_insn++;
2627       (void) priority (insn);
2628
2629       gcc_assert (INSN_PRIORITY_KNOWN (insn));
2630
2631       sched_max_insns_priority = MAX (sched_max_insns_priority,
2632                                       INSN_PRIORITY (insn));
2633     }
2634
2635   current_sched_info->sched_max_insns_priority = sched_max_insns_priority;
2636
2637   return n_insn;
2638 }
2639
2640 /* Next LUID to assign to an instruction.  */
2641 static int luid;
2642
2643 /* Initialize some global state for the scheduler.  */
2644
2645 void
2646 sched_init (void)
2647 {
2648   basic_block b;
2649   rtx insn;
2650   int i;
2651
2652   /* Switch to working copy of sched_info.  */
2653   memcpy (&current_sched_info_var, current_sched_info,
2654           sizeof (current_sched_info_var));
2655   current_sched_info = &current_sched_info_var;
2656       
2657   /* Disable speculative loads in their presence if cc0 defined.  */
2658 #ifdef HAVE_cc0
2659   flag_schedule_speculative_load = 0;
2660 #endif
2661
2662   /* Set dump and sched_verbose for the desired debugging output.  If no
2663      dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
2664      For -fsched-verbose=N, N>=10, print everything to stderr.  */
2665   sched_verbose = sched_verbose_param;
2666   if (sched_verbose_param == 0 && dump_file)
2667     sched_verbose = 1;
2668   sched_dump = ((sched_verbose_param >= 10 || !dump_file)
2669                 ? stderr : dump_file);
2670
2671   /* Initialize SPEC_INFO.  */
2672   if (targetm.sched.set_sched_flags)
2673     {
2674       spec_info = &spec_info_var;
2675       targetm.sched.set_sched_flags (spec_info);
2676       if (current_sched_info->flags & DO_SPECULATION)
2677         spec_info->weakness_cutoff =
2678           (PARAM_VALUE (PARAM_SCHED_SPEC_PROB_CUTOFF) * MAX_DEP_WEAK) / 100;
2679       else
2680         /* So we won't read anything accidentally.  */
2681         spec_info = 0;
2682 #ifdef ENABLE_CHECKING
2683       check_sched_flags ();
2684 #endif
2685     }
2686   else
2687     /* So we won't read anything accidentally.  */
2688     spec_info = 0;
2689
2690   /* Initialize issue_rate.  */
2691   if (targetm.sched.issue_rate)
2692     issue_rate = targetm.sched.issue_rate ();
2693   else
2694     issue_rate = 1;
2695
2696   if (cached_issue_rate != issue_rate)
2697     {
2698       cached_issue_rate = issue_rate;
2699       /* To invalidate max_lookahead_tries:  */
2700       cached_first_cycle_multipass_dfa_lookahead = 0;
2701     }
2702
2703   old_max_uid = 0;
2704   h_i_d = 0;
2705   extend_h_i_d ();
2706
2707   for (i = 0; i < old_max_uid; i++)
2708     {
2709       h_i_d[i].cost = -1;
2710       h_i_d[i].todo_spec = HARD_DEP;
2711       h_i_d[i].queue_index = QUEUE_NOWHERE;
2712       h_i_d[i].tick = INVALID_TICK;
2713       h_i_d[i].inter_tick = INVALID_TICK;
2714     }
2715
2716   if (targetm.sched.init_dfa_pre_cycle_insn)
2717     targetm.sched.init_dfa_pre_cycle_insn ();
2718
2719   if (targetm.sched.init_dfa_post_cycle_insn)
2720     targetm.sched.init_dfa_post_cycle_insn ();
2721
2722   dfa_start ();
2723   dfa_state_size = state_size ();
2724   curr_state = xmalloc (dfa_state_size);
2725
2726   h_i_d[0].luid = 0;
2727   luid = 1;
2728   FOR_EACH_BB (b)
2729     for (insn = BB_HEAD (b); ; insn = NEXT_INSN (insn))
2730       {
2731         INSN_LUID (insn) = luid;
2732
2733         /* Increment the next luid, unless this is a note.  We don't
2734            really need separate IDs for notes and we don't want to
2735            schedule differently depending on whether or not there are
2736            line-number notes, i.e., depending on whether or not we're
2737            generating debugging information.  */
2738         if (!NOTE_P (insn))
2739           ++luid;
2740
2741         if (insn == BB_END (b))
2742           break;
2743       }
2744
2745   init_dependency_caches (luid);
2746
2747   init_alias_analysis ();
2748
2749   old_last_basic_block = 0;
2750   extend_bb ();
2751
2752   /* Compute INSN_REG_WEIGHT for all blocks.  We must do this before
2753      removing death notes.  */
2754   FOR_EACH_BB_REVERSE (b)
2755     find_insn_reg_weight (b);
2756
2757   if (targetm.sched.md_init_global)
2758       targetm.sched.md_init_global (sched_dump, sched_verbose, old_max_uid);
2759
2760   nr_begin_data = nr_begin_control = nr_be_in_data = nr_be_in_control = 0;
2761   before_recovery = 0;
2762
2763 #ifdef ENABLE_CHECKING
2764   /* This is used preferably for finding bugs in check_cfg () itself.  */
2765   check_cfg (0, 0);
2766 #endif
2767 }
2768
2769 /* Free global data used during insn scheduling.  */
2770
2771 void
2772 sched_finish (void)
2773 {
2774   free (h_i_d);
2775   free (curr_state);
2776   dfa_finish ();
2777   free_dependency_caches ();
2778   end_alias_analysis ();
2779
2780   if (targetm.sched.md_finish_global)
2781     targetm.sched.md_finish_global (sched_dump, sched_verbose);
2782   
2783   if (spec_info && spec_info->dump)
2784     {
2785       char c = reload_completed ? 'a' : 'b';
2786
2787       fprintf (spec_info->dump,
2788                ";; %s:\n", current_function_name ());
2789
2790       fprintf (spec_info->dump,
2791                ";; Procedure %cr-begin-data-spec motions == %d\n",
2792                c, nr_begin_data);
2793       fprintf (spec_info->dump,
2794                ";; Procedure %cr-be-in-data-spec motions == %d\n",
2795                c, nr_be_in_data);
2796       fprintf (spec_info->dump,
2797                ";; Procedure %cr-begin-control-spec motions == %d\n",
2798                c, nr_begin_control);
2799       fprintf (spec_info->dump,
2800                ";; Procedure %cr-be-in-control-spec motions == %d\n",
2801                c, nr_be_in_control);
2802     }
2803
2804 #ifdef ENABLE_CHECKING
2805   /* After reload ia64 backend clobbers CFG, so can't check anything.  */
2806   if (!reload_completed)
2807     check_cfg (0, 0);
2808 #endif
2809
2810   current_sched_info = NULL;
2811 }
2812
2813 /* Fix INSN_TICKs of the instructions in the current block as well as
2814    INSN_TICKs of their dependents.
2815    HEAD and TAIL are the begin and the end of the current scheduled block.  */
2816 static void
2817 fix_inter_tick (rtx head, rtx tail)
2818 {
2819   /* Set of instructions with corrected INSN_TICK.  */
2820   bitmap_head processed;
2821   int next_clock = clock_var + 1;
2822
2823   bitmap_initialize (&processed, 0);
2824   
2825   /* Iterates over scheduled instructions and fix their INSN_TICKs and
2826      INSN_TICKs of dependent instructions, so that INSN_TICKs are consistent
2827      across different blocks.  */
2828   for (tail = NEXT_INSN (tail); head != tail; head = NEXT_INSN (head))
2829     {
2830       if (INSN_P (head))
2831         {
2832           int tick;
2833           dep_link_t link;
2834                   
2835           tick = INSN_TICK (head);
2836           gcc_assert (tick >= MIN_TICK);
2837           
2838           /* Fix INSN_TICK of instruction from just scheduled block.  */
2839           if (!bitmap_bit_p (&processed, INSN_LUID (head)))
2840             {
2841               bitmap_set_bit (&processed, INSN_LUID (head));
2842               tick -= next_clock;
2843               
2844               if (tick < MIN_TICK)
2845                 tick = MIN_TICK;
2846               
2847               INSN_TICK (head) = tick;           
2848             }
2849           
2850           FOR_EACH_DEP_LINK (link, INSN_FORW_DEPS (head))
2851             {
2852               rtx next;
2853               
2854               next = DEP_LINK_CON (link);
2855               tick = INSN_TICK (next);
2856
2857               if (tick != INVALID_TICK
2858                   /* If NEXT has its INSN_TICK calculated, fix it.
2859                      If not - it will be properly calculated from
2860                      scratch later in fix_tick_ready.  */
2861                   && !bitmap_bit_p (&processed, INSN_LUID (next)))
2862                 {
2863                   bitmap_set_bit (&processed, INSN_LUID (next));
2864                   tick -= next_clock;
2865                   
2866                   if (tick < MIN_TICK)
2867                     tick = MIN_TICK;
2868                   
2869                   if (tick > INTER_TICK (next))
2870                     INTER_TICK (next) = tick;
2871                   else
2872                     tick = INTER_TICK (next);
2873                   
2874                   INSN_TICK (next) = tick;
2875                 }
2876             }
2877         }
2878     }
2879   bitmap_clear (&processed);
2880 }
2881   
2882 /* Check if NEXT is ready to be added to the ready or queue list.
2883    If "yes", add it to the proper list.
2884    Returns:
2885       -1 - is not ready yet,
2886        0 - added to the ready list,
2887    0 < N - queued for N cycles.  */
2888 int
2889 try_ready (rtx next)
2890 {  
2891   ds_t old_ts, *ts;
2892   dep_link_t link;
2893
2894   ts = &TODO_SPEC (next);
2895   old_ts = *ts;
2896
2897   gcc_assert (!(old_ts & ~(SPECULATIVE | HARD_DEP))
2898               && ((old_ts & HARD_DEP)
2899                   || (old_ts & SPECULATIVE)));
2900   
2901   if (!(current_sched_info->flags & DO_SPECULATION))
2902     {
2903       if (deps_list_empty_p (INSN_BACK_DEPS (next)))
2904         *ts &= ~HARD_DEP;
2905     }
2906   else
2907     {
2908       *ts &= ~SPECULATIVE & ~HARD_DEP;
2909
2910       link = DEPS_LIST_FIRST (INSN_BACK_DEPS (next));
2911
2912       if (link != NULL)
2913         {
2914           ds_t ds = DEP_LINK_STATUS (link) & SPECULATIVE;
2915
2916           /* Backward dependencies of the insn are maintained sorted. 
2917              So if DEP_STATUS of the first dep is SPECULATIVE,
2918              than all other deps are speculative too.  */
2919           if (ds != 0)
2920             {          
2921               /* Now we've got NEXT with speculative deps only.
2922                  1. Look at the deps to see what we have to do.
2923                  2. Check if we can do 'todo'.  */
2924               *ts = ds;
2925
2926               while ((link = DEP_LINK_NEXT (link)) != NULL)
2927                 {
2928                   ds = DEP_LINK_STATUS (link) & SPECULATIVE;
2929                   *ts = ds_merge (*ts, ds);
2930                 }
2931
2932               if (dep_weak (*ts) < spec_info->weakness_cutoff)
2933                 /* Too few points.  */
2934                 *ts = (*ts & ~SPECULATIVE) | HARD_DEP;
2935             }
2936           else
2937             *ts |= HARD_DEP;
2938         }
2939     }
2940
2941   if (*ts & HARD_DEP)
2942     gcc_assert (*ts == old_ts
2943                 && QUEUE_INDEX (next) == QUEUE_NOWHERE);
2944   else if (current_sched_info->new_ready)
2945     *ts = current_sched_info->new_ready (next, *ts);
2946
2947   /* * if !(old_ts & SPECULATIVE) (e.g. HARD_DEP or 0), then insn might
2948      have its original pattern or changed (speculative) one.  This is due
2949      to changing ebb in region scheduling.
2950      * But if (old_ts & SPECULATIVE), then we are pretty sure that insn
2951      has speculative pattern.
2952
2953      We can't assert (!(*ts & HARD_DEP) || *ts == old_ts) here because
2954      control-speculative NEXT could have been discarded by sched-rgn.c
2955      (the same case as when discarded by can_schedule_ready_p ()).  */
2956
2957   if ((*ts & SPECULATIVE)
2958       /* If (old_ts == *ts), then (old_ts & SPECULATIVE) and we don't
2959          need to change anything.  */
2960       && *ts != old_ts)
2961     {
2962       int res;
2963       rtx new_pat;
2964       
2965       gcc_assert ((*ts & SPECULATIVE) && !(*ts & ~SPECULATIVE));
2966       
2967       res = speculate_insn (next, *ts, &new_pat);
2968         
2969       switch (res)
2970         {
2971         case -1:
2972           /* It would be nice to change DEP_STATUS of all dependences,
2973              which have ((DEP_STATUS & SPECULATIVE) == *ts) to HARD_DEP,
2974              so we won't reanalyze anything.  */
2975           *ts = (*ts & ~SPECULATIVE) | HARD_DEP;
2976           break;
2977           
2978         case 0:
2979           /* We follow the rule, that every speculative insn
2980              has non-null ORIG_PAT.  */
2981           if (!ORIG_PAT (next))
2982             ORIG_PAT (next) = PATTERN (next);
2983           break;
2984           
2985         case 1:                  
2986           if (!ORIG_PAT (next))
2987             /* If we gonna to overwrite the original pattern of insn,
2988                save it.  */
2989             ORIG_PAT (next) = PATTERN (next);
2990           
2991           change_pattern (next, new_pat);
2992           break;
2993           
2994         default:
2995           gcc_unreachable ();
2996         }
2997     }
2998   
2999   /* We need to restore pattern only if (*ts == 0), because otherwise it is
3000      either correct (*ts & SPECULATIVE),
3001      or we simply don't care (*ts & HARD_DEP).  */
3002   
3003   gcc_assert (!ORIG_PAT (next)
3004               || !IS_SPECULATION_BRANCHY_CHECK_P (next));
3005   
3006   if (*ts & HARD_DEP)
3007     {
3008       /* We can't assert (QUEUE_INDEX (next) == QUEUE_NOWHERE) here because
3009          control-speculative NEXT could have been discarded by sched-rgn.c
3010          (the same case as when discarded by can_schedule_ready_p ()).  */
3011       /*gcc_assert (QUEUE_INDEX (next) == QUEUE_NOWHERE);*/
3012       
3013       change_queue_index (next, QUEUE_NOWHERE);
3014       return -1;
3015     }
3016   else if (!(*ts & BEGIN_SPEC) && ORIG_PAT (next) && !IS_SPECULATION_CHECK_P (next))
3017     /* We should change pattern of every previously speculative 
3018        instruction - and we determine if NEXT was speculative by using
3019        ORIG_PAT field.  Except one case - speculation checks have ORIG_PAT
3020        pat too, so skip them.  */
3021     {
3022       change_pattern (next, ORIG_PAT (next));
3023       ORIG_PAT (next) = 0;
3024     }
3025
3026   if (sched_verbose >= 2)
3027     {         
3028       int s = TODO_SPEC (next);
3029           
3030       fprintf (sched_dump, ";;\t\tdependencies resolved: insn %s",
3031                (*current_sched_info->print_insn) (next, 0));
3032           
3033       if (spec_info && spec_info->dump)
3034         {
3035           if (s & BEGIN_DATA)
3036             fprintf (spec_info->dump, "; data-spec;");
3037           if (s & BEGIN_CONTROL)
3038             fprintf (spec_info->dump, "; control-spec;");
3039           if (s & BE_IN_CONTROL)
3040             fprintf (spec_info->dump, "; in-control-spec;");
3041         }
3042
3043       fprintf (sched_dump, "\n");
3044     }          
3045   
3046   adjust_priority (next);
3047         
3048   return fix_tick_ready (next);
3049 }
3050
3051 /* Calculate INSN_TICK of NEXT and add it to either ready or queue list.  */
3052 static int
3053 fix_tick_ready (rtx next)
3054 {
3055   int tick, delay;
3056
3057   if (!deps_list_empty_p (INSN_RESOLVED_BACK_DEPS (next)))
3058     {
3059       int full_p;
3060       dep_link_t link;
3061
3062       tick = INSN_TICK (next);
3063       /* if tick is not equal to INVALID_TICK, then update
3064          INSN_TICK of NEXT with the most recent resolved dependence
3065          cost.  Otherwise, recalculate from scratch.  */
3066       full_p = (tick == INVALID_TICK);
3067
3068       FOR_EACH_DEP_LINK (link, INSN_RESOLVED_BACK_DEPS (next))
3069         {       
3070           dep_t dep = DEP_LINK_DEP (link);
3071           rtx pro = DEP_PRO (dep);
3072           int tick1;
3073               
3074           gcc_assert (INSN_TICK (pro) >= MIN_TICK);
3075
3076           tick1 = INSN_TICK (pro) + dep_cost (dep);
3077           if (tick1 > tick)
3078             tick = tick1;
3079
3080           if (!full_p)
3081             break;
3082         }
3083     }
3084   else
3085     tick = -1;
3086
3087   INSN_TICK (next) = tick;
3088
3089   delay = tick - clock_var;
3090   if (delay <= 0)
3091     delay = QUEUE_READY;
3092
3093   change_queue_index (next, delay);
3094
3095   return delay;
3096 }
3097
3098 /* Move NEXT to the proper queue list with (DELAY >= 1),
3099    or add it to the ready list (DELAY == QUEUE_READY),
3100    or remove it from ready and queue lists at all (DELAY == QUEUE_NOWHERE).  */
3101 static void
3102 change_queue_index (rtx next, int delay)
3103 {
3104   int i = QUEUE_INDEX (next);
3105
3106   gcc_assert (QUEUE_NOWHERE <= delay && delay <= max_insn_queue_index 
3107               && delay != 0);
3108   gcc_assert (i != QUEUE_SCHEDULED);
3109   
3110   if ((delay > 0 && NEXT_Q_AFTER (q_ptr, delay) == i)
3111       || (delay < 0 && delay == i))
3112     /* We have nothing to do.  */
3113     return;
3114
3115   /* Remove NEXT from wherever it is now.  */
3116   if (i == QUEUE_READY)
3117     ready_remove_insn (next);
3118   else if (i >= 0)
3119     queue_remove (next);
3120     
3121   /* Add it to the proper place.  */
3122   if (delay == QUEUE_READY)
3123     ready_add (readyp, next, false);
3124   else if (delay >= 1)
3125     queue_insn (next, delay);
3126     
3127   if (sched_verbose >= 2)
3128     {         
3129       fprintf (sched_dump, ";;\t\ttick updated: insn %s",
3130                (*current_sched_info->print_insn) (next, 0));
3131       
3132       if (delay == QUEUE_READY)
3133         fprintf (sched_dump, " into ready\n");
3134       else if (delay >= 1)
3135         fprintf (sched_dump, " into queue with cost=%d\n", delay);
3136       else
3137         fprintf (sched_dump, " removed from ready or queue lists\n");
3138     }
3139 }
3140
3141 /* Extend H_I_D data.  */
3142 static void
3143 extend_h_i_d (void)
3144 {
3145   /* We use LUID 0 for the fake insn (UID 0) which holds dependencies for
3146      pseudos which do not cross calls.  */
3147   int new_max_uid = get_max_uid () + 1;  
3148
3149   h_i_d = xrecalloc (h_i_d, new_max_uid, old_max_uid, sizeof (*h_i_d));
3150   old_max_uid = new_max_uid;
3151
3152   if (targetm.sched.h_i_d_extended)
3153     targetm.sched.h_i_d_extended ();
3154 }
3155
3156 /* Extend READY, READY_TRY and CHOICE_STACK arrays.
3157    N_NEW_INSNS is the number of additional elements to allocate.  */
3158 static void
3159 extend_ready (int n_new_insns)
3160 {
3161   int i;
3162
3163   readyp->veclen = rgn_n_insns + n_new_insns + 1 + issue_rate;
3164   readyp->vec = XRESIZEVEC (rtx, readyp->vec, readyp->veclen);
3165  
3166   ready_try = xrecalloc (ready_try, rgn_n_insns + n_new_insns + 1,
3167                          rgn_n_insns + 1, sizeof (char));
3168
3169   rgn_n_insns += n_new_insns;
3170
3171   choice_stack = XRESIZEVEC (struct choice_entry, choice_stack,
3172                              rgn_n_insns + 1);
3173
3174   for (i = rgn_n_insns; n_new_insns--; i--)
3175     choice_stack[i].state = xmalloc (dfa_state_size);
3176 }
3177
3178 /* Extend global scheduler structures (those, that live across calls to
3179    schedule_block) to include information about just emitted INSN.  */
3180 static void
3181 extend_global (rtx insn)
3182 {
3183   gcc_assert (INSN_P (insn));
3184   /* These structures have scheduler scope.  */
3185   extend_h_i_d ();
3186   init_h_i_d (insn);
3187
3188   extend_dependency_caches (1, 0);
3189 }
3190
3191 /* Extends global and local scheduler structures to include information
3192    about just emitted INSN.  */
3193 static void
3194 extend_all (rtx insn)
3195
3196   extend_global (insn);
3197
3198   /* These structures have block scope.  */
3199   extend_ready (1);
3200   
3201   (*current_sched_info->add_remove_insn) (insn, 0);
3202 }
3203
3204 /* Initialize h_i_d entry of the new INSN with default values.
3205    Values, that are not explicitly initialized here, hold zero.  */
3206 static void
3207 init_h_i_d (rtx insn)
3208 {
3209   INSN_LUID (insn) = luid++;
3210   INSN_COST (insn) = -1;
3211   TODO_SPEC (insn) = HARD_DEP;
3212   QUEUE_INDEX (insn) = QUEUE_NOWHERE;
3213   INSN_TICK (insn) = INVALID_TICK;
3214   INTER_TICK (insn) = INVALID_TICK;
3215   find_insn_reg_weight1 (insn);
3216
3217   /* These two lists will be freed in schedule_insn ().  */
3218   INSN_BACK_DEPS (insn) = create_deps_list (false);
3219   INSN_RESOLVED_BACK_DEPS (insn) = create_deps_list (false);
3220
3221   /* This one should be allocated on the obstack because it should live till
3222      the scheduling ends.  */
3223   INSN_FORW_DEPS (insn) = create_deps_list (true);
3224 }
3225
3226 /* Generates recovery code for INSN.  */
3227 static void
3228 generate_recovery_code (rtx insn)
3229 {
3230   if (TODO_SPEC (insn) & BEGIN_SPEC)
3231     begin_speculative_block (insn);
3232   
3233   /* Here we have insn with no dependencies to
3234      instructions other then CHECK_SPEC ones.  */
3235   
3236   if (TODO_SPEC (insn) & BE_IN_SPEC)
3237     add_to_speculative_block (insn);
3238 }
3239
3240 /* Helper function.
3241    Tries to add speculative dependencies of type FS between instructions
3242    in deps_list L and TWIN.  */
3243 static void
3244 process_insn_forw_deps_be_in_spec (deps_list_t l, rtx twin, ds_t fs)
3245 {
3246   dep_link_t link;
3247
3248   FOR_EACH_DEP_LINK (link, l)
3249     {
3250       ds_t ds;
3251       rtx consumer;
3252
3253       consumer = DEP_LINK_CON (link);
3254
3255       ds = DEP_LINK_STATUS (link);
3256
3257       if (/* If we want to create speculative dep.  */
3258           fs
3259           /* And we can do that because this is a true dep.  */
3260           && (ds & DEP_TYPES) == DEP_TRUE)
3261         {
3262           gcc_assert (!(ds & BE_IN_SPEC));
3263
3264           if (/* If this dep can be overcome with 'begin speculation'.  */
3265               ds & BEGIN_SPEC)
3266             /* Then we have a choice: keep the dep 'begin speculative'
3267                or transform it into 'be in speculative'.  */
3268             {
3269               if (/* In try_ready we assert that if insn once became ready
3270                      it can be removed from the ready (or queue) list only
3271                      due to backend decision.  Hence we can't let the
3272                      probability of the speculative dep to decrease.  */
3273                   dep_weak (ds) <= dep_weak (fs))
3274                 /* Transform it to be in speculative.  */
3275                 ds = (ds & ~BEGIN_SPEC) | fs;
3276             }
3277           else
3278             /* Mark the dep as 'be in speculative'.  */
3279             ds |= fs;
3280         }
3281
3282       add_back_forw_dep (consumer, twin, DEP_LINK_KIND (link), ds);
3283     }
3284 }
3285
3286 /* Generates recovery code for BEGIN speculative INSN.  */
3287 static void
3288 begin_speculative_block (rtx insn)
3289 {
3290   if (TODO_SPEC (insn) & BEGIN_DATA)
3291     nr_begin_data++;      
3292   if (TODO_SPEC (insn) & BEGIN_CONTROL)
3293     nr_begin_control++;
3294
3295   create_check_block_twin (insn, false);
3296
3297   TODO_SPEC (insn) &= ~BEGIN_SPEC;
3298 }
3299
3300 /* Generates recovery code for BE_IN speculative INSN.  */
3301 static void
3302 add_to_speculative_block (rtx insn)
3303 {
3304   ds_t ts;
3305   dep_link_t link;
3306   rtx twins = NULL;
3307   rtx_vec_t priorities_roots;
3308
3309   ts = TODO_SPEC (insn);
3310   gcc_assert (!(ts & ~BE_IN_SPEC));
3311
3312   if (ts & BE_IN_DATA)
3313     nr_be_in_data++;
3314   if (ts & BE_IN_CONTROL)
3315     nr_be_in_control++;
3316
3317   TODO_SPEC (insn) &= ~BE_IN_SPEC;
3318   gcc_assert (!TODO_SPEC (insn));
3319   
3320   DONE_SPEC (insn) |= ts;
3321
3322   /* First we convert all simple checks to branchy.  */
3323   for (link = DEPS_LIST_FIRST (INSN_BACK_DEPS (insn)); link != NULL;)
3324     {
3325       rtx check = DEP_LINK_PRO (link);
3326
3327       if (IS_SPECULATION_SIMPLE_CHECK_P (check))
3328         {
3329           create_check_block_twin (check, true);
3330
3331           /* Restart search.  */
3332           link = DEPS_LIST_FIRST (INSN_BACK_DEPS (insn));
3333         }
3334       else
3335         /* Continue search.  */
3336         link = DEP_LINK_NEXT (link);
3337     }
3338
3339   priorities_roots = NULL;
3340   clear_priorities (insn, &priorities_roots);
3341  
3342   do
3343     {
3344       dep_link_t link;
3345       rtx check, twin;
3346       basic_block rec;
3347
3348       link = DEPS_LIST_FIRST (INSN_BACK_DEPS (insn));
3349
3350       gcc_assert ((DEP_LINK_STATUS (link) & BEGIN_SPEC) == 0
3351                   && (DEP_LINK_STATUS (link) & BE_IN_SPEC) != 0
3352                   && (DEP_LINK_STATUS (link) & DEP_TYPES) == DEP_TRUE);
3353
3354       check = DEP_LINK_PRO (link);
3355
3356       gcc_assert (!IS_SPECULATION_CHECK_P (check) && !ORIG_PAT (check)
3357                   && QUEUE_INDEX (check) == QUEUE_NOWHERE);
3358       
3359       rec = BLOCK_FOR_INSN (check);
3360       
3361       twin = emit_insn_before (copy_insn (PATTERN (insn)), BB_END (rec));
3362       extend_global (twin);
3363
3364       copy_deps_list_change_con (INSN_RESOLVED_BACK_DEPS (twin),
3365                                  INSN_RESOLVED_BACK_DEPS (insn),
3366                                  twin);
3367
3368       if (sched_verbose && spec_info->dump)
3369         /* INSN_BB (insn) isn't determined for twin insns yet.
3370            So we can't use current_sched_info->print_insn.  */
3371         fprintf (spec_info->dump, ";;\t\tGenerated twin insn : %d/rec%d\n",
3372                  INSN_UID (twin), rec->index);
3373
3374       twins = alloc_INSN_LIST (twin, twins);
3375
3376       /* Add dependences between TWIN and all appropriate
3377          instructions from REC.  */
3378       do
3379         {         
3380           add_back_forw_dep (twin, check, REG_DEP_TRUE, DEP_TRUE);
3381           
3382           do              
3383             {  
3384               link = DEP_LINK_NEXT (link);
3385
3386               if (link != NULL)
3387                 {
3388                   check = DEP_LINK_PRO (link);
3389                   if (BLOCK_FOR_INSN (check) == rec)
3390                     break;
3391                 }
3392               else
3393                 break;
3394             }
3395           while (1);
3396         }
3397       while (link != NULL);
3398
3399       process_insn_forw_deps_be_in_spec (INSN_FORW_DEPS (insn), twin, ts);
3400
3401       /* Remove all dependencies between INSN and insns in REC.  */
3402       for (link = DEPS_LIST_FIRST (INSN_BACK_DEPS (insn)); link != NULL;)
3403         {
3404           check = DEP_LINK_PRO (link);
3405
3406           if (BLOCK_FOR_INSN (check) == rec)
3407             {
3408               delete_back_forw_dep (link);
3409
3410               /* Restart search.  */
3411               link = DEPS_LIST_FIRST (INSN_BACK_DEPS (insn));
3412             }
3413           else
3414             /* Continue search.  */
3415             link = DEP_LINK_NEXT (link);
3416         }
3417     }
3418   while (!deps_list_empty_p (INSN_BACK_DEPS (insn)));
3419
3420   /* We couldn't have added the dependencies between INSN and TWINS earlier
3421      because that would make TWINS appear in the INSN_BACK_DEPS (INSN).  */
3422   while (twins)
3423     {
3424       rtx twin;
3425
3426       twin = XEXP (twins, 0);
3427       add_back_forw_dep (twin, insn, REG_DEP_OUTPUT, DEP_OUTPUT);
3428
3429       twin = XEXP (twins, 1);
3430       free_INSN_LIST_node (twins);
3431       twins = twin;      
3432     }
3433
3434   calc_priorities (priorities_roots);
3435   VEC_free (rtx, heap, priorities_roots);
3436 }
3437
3438 /* Extends and fills with zeros (only the new part) array pointed to by P.  */
3439 void *
3440 xrecalloc (void *p, size_t new_nmemb, size_t old_nmemb, size_t size)
3441 {
3442   gcc_assert (new_nmemb >= old_nmemb);
3443   p = XRESIZEVAR (void, p, new_nmemb * size);
3444   memset (((char *) p) + old_nmemb * size, 0, (new_nmemb - old_nmemb) * size);
3445   return p;
3446 }
3447
3448 /* Return the probability of speculation success for the speculation
3449    status DS.  */
3450 static dw_t
3451 dep_weak (ds_t ds)
3452 {
3453   ds_t res = 1, dt;
3454   int n = 0;
3455
3456   dt = FIRST_SPEC_TYPE;
3457   do
3458     {
3459       if (ds & dt)
3460         {
3461           res *= (ds_t) get_dep_weak (ds, dt);
3462           n++;
3463         }
3464
3465       if (dt == LAST_SPEC_TYPE)
3466         break;
3467       dt <<= SPEC_TYPE_SHIFT;
3468     }
3469   while (1);
3470
3471   gcc_assert (n);
3472   while (--n)
3473     res /= MAX_DEP_WEAK;
3474
3475   if (res < MIN_DEP_WEAK)
3476     res = MIN_DEP_WEAK;
3477
3478   gcc_assert (res <= MAX_DEP_WEAK);
3479
3480   return (dw_t) res;
3481 }
3482
3483 /* Helper function.
3484    Find fallthru edge from PRED.  */
3485 static edge
3486 find_fallthru_edge (basic_block pred)
3487 {
3488   edge e;
3489   edge_iterator ei;
3490   basic_block succ;
3491
3492   succ = pred->next_bb;
3493   gcc_assert (succ->prev_bb == pred);
3494
3495   if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
3496     {
3497       FOR_EACH_EDGE (e, ei, pred->succs)
3498         if (e->flags & EDGE_FALLTHRU)
3499           {
3500             gcc_assert (e->dest == succ);
3501             return e;
3502           }
3503     }
3504   else
3505     {
3506       FOR_EACH_EDGE (e, ei, succ->preds)
3507         if (e->flags & EDGE_FALLTHRU)
3508           {
3509             gcc_assert (e->src == pred);
3510             return e;
3511           }
3512     }
3513
3514   return NULL;
3515 }
3516
3517 /* Initialize BEFORE_RECOVERY variable.  */
3518 static void
3519 init_before_recovery (void)
3520 {
3521   basic_block last;
3522   edge e;
3523
3524   last = EXIT_BLOCK_PTR->prev_bb;
3525   e = find_fallthru_edge (last);
3526
3527   if (e)
3528     {
3529       /* We create two basic blocks: 
3530          1. Single instruction block is inserted right after E->SRC
3531          and has jump to 
3532          2. Empty block right before EXIT_BLOCK.
3533          Between these two blocks recovery blocks will be emitted.  */
3534
3535       basic_block single, empty;
3536       rtx x, label;
3537
3538       single = create_empty_bb (last);
3539       empty = create_empty_bb (single);            
3540
3541       single->count = last->count;     
3542       empty->count = last->count;
3543       single->frequency = last->frequency;
3544       empty->frequency = last->frequency;
3545       BB_COPY_PARTITION (single, last);
3546       BB_COPY_PARTITION (empty, last);
3547
3548       redirect_edge_succ (e, single);
3549       make_single_succ_edge (single, empty, 0);
3550       make_single_succ_edge (empty, EXIT_BLOCK_PTR,
3551                              EDGE_FALLTHRU | EDGE_CAN_FALLTHRU);
3552
3553       label = block_label (empty);
3554       x = emit_jump_insn_after (gen_jump (label), BB_END (single));
3555       JUMP_LABEL (x) = label;
3556       LABEL_NUSES (label)++;
3557       extend_global (x);
3558           
3559       emit_barrier_after (x);
3560
3561       add_block (empty, 0);
3562       add_block (single, 0);
3563
3564       before_recovery = single;
3565
3566       if (sched_verbose >= 2 && spec_info->dump)
3567         fprintf (spec_info->dump,
3568                  ";;\t\tFixed fallthru to EXIT : %d->>%d->%d->>EXIT\n", 
3569                  last->index, single->index, empty->index);      
3570     }
3571   else
3572     before_recovery = last;
3573 }
3574
3575 /* Returns new recovery block.  */
3576 static basic_block
3577 create_recovery_block (void)
3578 {
3579   rtx label;
3580   rtx barrier;
3581   basic_block rec;
3582   
3583   added_recovery_block_p = true;
3584
3585   if (!before_recovery)
3586     init_before_recovery ();
3587
3588   barrier = get_last_bb_insn (before_recovery);
3589   gcc_assert (BARRIER_P (barrier));
3590
3591   label = emit_label_after (gen_label_rtx (), barrier);
3592
3593   rec = create_basic_block (label, label, before_recovery);
3594
3595   /* Recovery block always end with an unconditional jump.  */
3596   emit_barrier_after (BB_END (rec));
3597
3598   if (BB_PARTITION (before_recovery) != BB_UNPARTITIONED)
3599     BB_SET_PARTITION (rec, BB_COLD_PARTITION);
3600   
3601   if (sched_verbose && spec_info->dump)    
3602     fprintf (spec_info->dump, ";;\t\tGenerated recovery block rec%d\n",
3603              rec->index);
3604
3605   before_recovery = rec;
3606
3607   return rec;
3608 }
3609
3610 /* This function creates recovery code for INSN.  If MUTATE_P is nonzero,
3611    INSN is a simple check, that should be converted to branchy one.  */
3612 static void
3613 create_check_block_twin (rtx insn, bool mutate_p)
3614 {
3615   basic_block rec;
3616   rtx label, check, twin;
3617   dep_link_t link;
3618   ds_t fs;
3619
3620   gcc_assert (ORIG_PAT (insn)
3621               && (!mutate_p 
3622                   || (IS_SPECULATION_SIMPLE_CHECK_P (insn)
3623                       && !(TODO_SPEC (insn) & SPECULATIVE))));
3624
3625   /* Create recovery block.  */
3626   if (mutate_p || targetm.sched.needs_block_p (insn))
3627     {
3628       rec = create_recovery_block ();
3629       label = BB_HEAD (rec);
3630     }
3631   else
3632     {
3633       rec = EXIT_BLOCK_PTR;
3634       label = 0;
3635     }
3636
3637   /* Emit CHECK.  */
3638   check = targetm.sched.gen_check (insn, label, mutate_p);
3639
3640   if (rec != EXIT_BLOCK_PTR)
3641     {
3642       /* To have mem_reg alive at the beginning of second_bb,
3643          we emit check BEFORE insn, so insn after splitting 
3644          insn will be at the beginning of second_bb, which will
3645          provide us with the correct life information.  */
3646       check = emit_jump_insn_before (check, insn);
3647       JUMP_LABEL (check) = label;
3648       LABEL_NUSES (label)++;
3649     }
3650   else
3651     check = emit_insn_before (check, insn);
3652
3653   /* Extend data structures.  */
3654   extend_all (check);
3655   RECOVERY_BLOCK (check) = rec;
3656
3657   if (sched_verbose && spec_info->dump)
3658     fprintf (spec_info->dump, ";;\t\tGenerated check insn : %s\n",
3659              (*current_sched_info->print_insn) (check, 0));
3660
3661   gcc_assert (ORIG_PAT (insn));
3662
3663   /* Initialize TWIN (twin is a duplicate of original instruction
3664      in the recovery block).  */
3665   if (rec != EXIT_BLOCK_PTR)
3666     {
3667       FOR_EACH_DEP_LINK (link, INSN_RESOLVED_BACK_DEPS (insn))
3668         if ((DEP_LINK_STATUS (link) & DEP_OUTPUT) != 0)
3669           {
3670             struct _dep _dep, *dep = &_dep;
3671
3672             init_dep (dep, DEP_LINK_PRO (link), check, REG_DEP_TRUE);
3673
3674             add_back_dep_to_deps_list (INSN_RESOLVED_BACK_DEPS (check), dep);
3675           }
3676
3677       twin = emit_insn_after (ORIG_PAT (insn), BB_END (rec));
3678       extend_global (twin);
3679
3680       if (sched_verbose && spec_info->dump)
3681         /* INSN_BB (insn) isn't determined for twin insns yet.
3682            So we can't use current_sched_info->print_insn.  */
3683         fprintf (spec_info->dump, ";;\t\tGenerated twin insn : %d/rec%d\n",
3684                  INSN_UID (twin), rec->index);
3685     }
3686   else
3687     {
3688       ORIG_PAT (check) = ORIG_PAT (insn);
3689       HAS_INTERNAL_DEP (check) = 1;
3690       twin = check;
3691       /* ??? We probably should change all OUTPUT dependencies to
3692          (TRUE | OUTPUT).  */
3693     }
3694
3695   copy_deps_list_change_con (INSN_RESOLVED_BACK_DEPS (twin),
3696                              INSN_RESOLVED_BACK_DEPS (insn),
3697                              twin);
3698
3699   if (rec != EXIT_BLOCK_PTR)
3700     /* In case of branchy check, fix CFG.  */
3701     {
3702       basic_block first_bb, second_bb;
3703       rtx jump;
3704       edge e;
3705       int edge_flags;
3706
3707       first_bb = BLOCK_FOR_INSN (check);
3708       e = split_block (first_bb, check);
3709       /* split_block emits note if *check == BB_END.  Probably it 
3710          is better to rip that note off.  */
3711       gcc_assert (e->src == first_bb);
3712       second_bb = e->dest;
3713
3714       /* This is fixing of incoming edge.  */
3715       /* ??? Which other flags should be specified?  */      
3716       if (BB_PARTITION (first_bb) != BB_PARTITION (rec))
3717         /* Partition type is the same, if it is "unpartitioned".  */
3718         edge_flags = EDGE_CROSSING;
3719       else
3720         edge_flags = 0;
3721       
3722       e = make_edge (first_bb, rec, edge_flags);
3723
3724       add_block (second_bb, first_bb);
3725       
3726       gcc_assert (NOTE_INSN_BASIC_BLOCK_P (BB_HEAD (second_bb)));
3727       label = block_label (second_bb);
3728       jump = emit_jump_insn_after (gen_jump (label), BB_END (rec));
3729       JUMP_LABEL (jump) = label;
3730       LABEL_NUSES (label)++;
3731       extend_global (jump);
3732
3733       if (BB_PARTITION (second_bb) != BB_PARTITION (rec))
3734         /* Partition type is the same, if it is "unpartitioned".  */
3735         {
3736           /* Rewritten from cfgrtl.c.  */
3737           if (flag_reorder_blocks_and_partition
3738               && targetm.have_named_sections
3739               /*&& !any_condjump_p (jump)*/)
3740             /* any_condjump_p (jump) == false.
3741                We don't need the same note for the check because
3742                any_condjump_p (check) == true.  */
3743             {
3744               REG_NOTES (jump) = gen_rtx_EXPR_LIST (REG_CROSSING_JUMP,
3745                                                     NULL_RTX,
3746                                                     REG_NOTES (jump));
3747             }
3748           edge_flags = EDGE_CROSSING;
3749         }
3750       else
3751         edge_flags = 0;  
3752       
3753       make_single_succ_edge (rec, second_bb, edge_flags);  
3754       
3755       add_block (rec, EXIT_BLOCK_PTR);
3756     }
3757
3758   /* Move backward dependences from INSN to CHECK and 
3759      move forward dependences from INSN to TWIN.  */
3760   FOR_EACH_DEP_LINK (link, INSN_BACK_DEPS (insn))
3761     {
3762       rtx pro = DEP_LINK_PRO (link);
3763       enum reg_note dk = DEP_LINK_KIND (link);
3764       ds_t ds;
3765
3766       /* If BEGIN_DATA: [insn ~~TRUE~~> producer]:
3767          check --TRUE--> producer  ??? or ANTI ???
3768          twin  --TRUE--> producer
3769          twin  --ANTI--> check
3770          
3771          If BEGIN_CONTROL: [insn ~~ANTI~~> producer]:
3772          check --ANTI--> producer
3773          twin  --ANTI--> producer
3774          twin  --ANTI--> check
3775
3776          If BE_IN_SPEC: [insn ~~TRUE~~> producer]:
3777          check ~~TRUE~~> producer
3778          twin  ~~TRUE~~> producer
3779          twin  --ANTI--> check  */                
3780
3781       ds = DEP_LINK_STATUS (link);
3782
3783       if (ds & BEGIN_SPEC)
3784         {
3785           gcc_assert (!mutate_p);
3786           ds &= ~BEGIN_SPEC;
3787         }
3788
3789       if (rec != EXIT_BLOCK_PTR)
3790         {
3791           add_back_forw_dep (check, pro, dk, ds);
3792           add_back_forw_dep (twin, pro, dk, ds);
3793         }    
3794       else
3795         add_back_forw_dep (check, pro, dk, ds);
3796     }
3797
3798   for (link = DEPS_LIST_FIRST (INSN_BACK_DEPS (insn)); link != NULL;)
3799     if ((DEP_LINK_STATUS (link) & BEGIN_SPEC)
3800         || mutate_p)
3801       /* We can delete this dep only if we totally overcome it with
3802          BEGIN_SPECULATION.  */
3803       {
3804         delete_back_forw_dep (link);
3805
3806         /* Restart search.  */
3807         link = DEPS_LIST_FIRST (INSN_BACK_DEPS (insn));
3808       }
3809     else
3810       /* Continue search.  */
3811       link = DEP_LINK_NEXT (link);    
3812
3813   fs = 0;
3814
3815   /* Fields (DONE_SPEC (x) & BEGIN_SPEC) and CHECK_SPEC (x) are set only
3816      here.  */
3817   
3818   gcc_assert (!DONE_SPEC (insn));
3819   
3820   if (!mutate_p)
3821     { 
3822       ds_t ts = TODO_SPEC (insn);
3823
3824       DONE_SPEC (insn) = ts & BEGIN_SPEC;
3825       CHECK_SPEC (check) = ts & BEGIN_SPEC;
3826
3827       if (ts & BEGIN_DATA)
3828         fs = set_dep_weak (fs, BE_IN_DATA, get_dep_weak (ts, BEGIN_DATA));
3829       if (ts & BEGIN_CONTROL)
3830         fs = set_dep_weak (fs, BE_IN_CONTROL, get_dep_weak (ts, BEGIN_CONTROL));
3831     }
3832   else
3833     CHECK_SPEC (check) = CHECK_SPEC (insn);
3834
3835   /* Future speculations: call the helper.  */
3836   process_insn_forw_deps_be_in_spec (INSN_FORW_DEPS (insn), twin, fs);
3837
3838   if (rec != EXIT_BLOCK_PTR)
3839     {
3840       /* Which types of dependencies should we use here is,
3841          generally, machine-dependent question...  But, for now,
3842          it is not.  */
3843
3844       if (!mutate_p)
3845         {
3846           add_back_forw_dep (check, insn, REG_DEP_TRUE, DEP_TRUE);
3847           add_back_forw_dep (twin, insn, REG_DEP_OUTPUT, DEP_OUTPUT);
3848         }
3849       else
3850         {
3851           dep_link_t link;
3852
3853           if (spec_info->dump)    
3854             fprintf (spec_info->dump, ";;\t\tRemoved simple check : %s\n",
3855                      (*current_sched_info->print_insn) (insn, 0));
3856
3857           /* Remove all forward dependencies of the INSN.  */
3858           link = DEPS_LIST_FIRST (INSN_FORW_DEPS (insn));
3859           while (link != NULL)
3860             {
3861               delete_back_forw_dep (link);
3862               link = DEPS_LIST_FIRST (INSN_FORW_DEPS (insn));
3863             }
3864
3865           if (QUEUE_INDEX (insn) != QUEUE_NOWHERE)
3866             try_ready (check);
3867
3868           sched_remove_insn (insn);
3869         }
3870
3871       add_back_forw_dep (twin, check, REG_DEP_ANTI, DEP_ANTI);
3872     }
3873   else
3874     add_back_forw_dep (check, insn, REG_DEP_TRUE, DEP_TRUE | DEP_OUTPUT);
3875
3876   if (!mutate_p)
3877     /* Fix priorities.  If MUTATE_P is nonzero, this is not necessary,
3878        because it'll be done later in add_to_speculative_block.  */
3879     {
3880       rtx_vec_t priorities_roots = NULL;
3881
3882       clear_priorities (twin, &priorities_roots);
3883       calc_priorities (priorities_roots);
3884       VEC_free (rtx, heap, priorities_roots);
3885     }
3886 }
3887
3888 /* Removes dependency between instructions in the recovery block REC
3889    and usual region instructions.  It keeps inner dependences so it
3890    won't be necessary to recompute them.  */
3891 static void
3892 fix_recovery_deps (basic_block rec)
3893 {
3894   dep_link_t link;
3895   rtx note, insn, jump, ready_list = 0;
3896   bitmap_head in_ready;
3897   rtx link1;
3898
3899   bitmap_initialize (&in_ready, 0);
3900   
3901   /* NOTE - a basic block note.  */
3902   note = NEXT_INSN (BB_HEAD (rec));
3903   gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
3904   insn = BB_END (rec);
3905   gcc_assert (JUMP_P (insn));
3906   insn = PREV_INSN (insn);
3907
3908   do
3909     {    
3910       for (link = DEPS_LIST_FIRST (INSN_FORW_DEPS (insn)); link != NULL;)
3911         {
3912           rtx consumer;
3913
3914           consumer = DEP_LINK_CON (link);
3915
3916           if (BLOCK_FOR_INSN (consumer) != rec)
3917             {
3918               delete_back_forw_dep (link);
3919
3920               if (!bitmap_bit_p (&in_ready, INSN_LUID (consumer)))
3921                 {
3922                   ready_list = alloc_INSN_LIST (consumer, ready_list);
3923                   bitmap_set_bit (&in_ready, INSN_LUID (consumer));
3924                 }
3925
3926               /* Restart search.  */
3927               link = DEPS_LIST_FIRST (INSN_FORW_DEPS (insn));
3928             }
3929           else
3930             {
3931               gcc_assert ((DEP_LINK_STATUS (link) & DEP_TYPES) == DEP_TRUE);
3932
3933               /* Continue search.  */
3934               link = DEP_LINK_NEXT (link);
3935             }
3936         }
3937       
3938       insn = PREV_INSN (insn);
3939     }
3940   while (insn != note);
3941
3942   bitmap_clear (&in_ready);
3943
3944   /* Try to add instructions to the ready or queue list.  */
3945   for (link1 = ready_list; link1; link1 = XEXP (link1, 1))
3946     try_ready (XEXP (link1, 0));
3947   free_INSN_LIST_list (&ready_list);
3948
3949   /* Fixing jump's dependences.  */
3950   insn = BB_HEAD (rec);
3951   jump = BB_END (rec);
3952       
3953   gcc_assert (LABEL_P (insn));
3954   insn = NEXT_INSN (insn);
3955   
3956   gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
3957   add_jump_dependencies (insn, jump);
3958 }
3959
3960 /* Changes pattern of the INSN to NEW_PAT.  */
3961 static void
3962 change_pattern (rtx insn, rtx new_pat)
3963 {
3964   int t;
3965
3966   t = validate_change (insn, &PATTERN (insn), new_pat, 0);
3967   gcc_assert (t);
3968   /* Invalidate INSN_COST, so it'll be recalculated.  */
3969   INSN_COST (insn) = -1;
3970   /* Invalidate INSN_TICK, so it'll be recalculated.  */
3971   INSN_TICK (insn) = INVALID_TICK;
3972   dfa_clear_single_insn_cache (insn);
3973 }
3974
3975
3976 /* -1 - can't speculate,
3977    0 - for speculation with REQUEST mode it is OK to use
3978    current instruction pattern,
3979    1 - need to change pattern for *NEW_PAT to be speculative.  */
3980 static int
3981 speculate_insn (rtx insn, ds_t request, rtx *new_pat)
3982 {
3983   gcc_assert (current_sched_info->flags & DO_SPECULATION
3984               && (request & SPECULATIVE));
3985
3986   if (!NONJUMP_INSN_P (insn)
3987       || HAS_INTERNAL_DEP (insn)
3988       || SCHED_GROUP_P (insn)
3989       || side_effects_p (PATTERN (insn))
3990       || (request & spec_info->mask) != request)    
3991     return -1;
3992   
3993   gcc_assert (!IS_SPECULATION_CHECK_P (insn));
3994
3995   if (request & BE_IN_SPEC)
3996     {            
3997       if (may_trap_p (PATTERN (insn)))
3998         return -1;
3999       
4000       if (!(request & BEGIN_SPEC))
4001         return 0;
4002     }
4003
4004   return targetm.sched.speculate_insn (insn, request & BEGIN_SPEC, new_pat);
4005 }
4006
4007 /* Print some information about block BB, which starts with HEAD and
4008    ends with TAIL, before scheduling it.
4009    I is zero, if scheduler is about to start with the fresh ebb.  */
4010 static void
4011 dump_new_block_header (int i, basic_block bb, rtx head, rtx tail)
4012 {
4013   if (!i)
4014     fprintf (sched_dump,
4015              ";;   ======================================================\n");
4016   else
4017     fprintf (sched_dump,
4018              ";;   =====================ADVANCING TO=====================\n");
4019   fprintf (sched_dump,
4020            ";;   -- basic block %d from %d to %d -- %s reload\n",
4021            bb->index, INSN_UID (head), INSN_UID (tail),
4022            (reload_completed ? "after" : "before"));
4023   fprintf (sched_dump,
4024            ";;   ======================================================\n");
4025   fprintf (sched_dump, "\n");
4026 }
4027
4028 /* Unlink basic block notes and labels and saves them, so they
4029    can be easily restored.  We unlink basic block notes in EBB to
4030    provide back-compatibility with the previous code, as target backends
4031    assume, that there'll be only instructions between
4032    current_sched_info->{head and tail}.  We restore these notes as soon
4033    as we can.
4034    FIRST (LAST) is the first (last) basic block in the ebb.
4035    NB: In usual case (FIRST == LAST) nothing is really done.  */
4036 void
4037 unlink_bb_notes (basic_block first, basic_block last)
4038 {
4039   /* We DON'T unlink basic block notes of the first block in the ebb.  */
4040   if (first == last)
4041     return;
4042
4043   bb_header = xmalloc (last_basic_block * sizeof (*bb_header));
4044
4045   /* Make a sentinel.  */
4046   if (last->next_bb != EXIT_BLOCK_PTR)
4047     bb_header[last->next_bb->index] = 0;
4048
4049   first = first->next_bb;
4050   do
4051     {
4052       rtx prev, label, note, next;
4053
4054       label = BB_HEAD (last);
4055       if (LABEL_P (label))
4056         note = NEXT_INSN (label);
4057       else
4058         note = label;      
4059       gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4060
4061       prev = PREV_INSN (label);
4062       next = NEXT_INSN (note);
4063       gcc_assert (prev && next);
4064
4065       NEXT_INSN (prev) = next;
4066       PREV_INSN (next) = prev;
4067
4068       bb_header[last->index] = label;
4069
4070       if (last == first)
4071         break;
4072       
4073       last = last->prev_bb;
4074     }
4075   while (1);
4076 }
4077
4078 /* Restore basic block notes.
4079    FIRST is the first basic block in the ebb.  */
4080 static void
4081 restore_bb_notes (basic_block first)
4082 {
4083   if (!bb_header)
4084     return;
4085
4086   /* We DON'T unlink basic block notes of the first block in the ebb.  */
4087   first = first->next_bb;  
4088   /* Remember: FIRST is actually a second basic block in the ebb.  */
4089
4090   while (first != EXIT_BLOCK_PTR
4091          && bb_header[first->index])
4092     {
4093       rtx prev, label, note, next;
4094       
4095       label = bb_header[first->index];
4096       prev = PREV_INSN (label);
4097       next = NEXT_INSN (prev);
4098
4099       if (LABEL_P (label))
4100         note = NEXT_INSN (label);
4101       else
4102         note = label;      
4103       gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4104
4105       bb_header[first->index] = 0;
4106
4107       NEXT_INSN (prev) = label;
4108       NEXT_INSN (note) = next;
4109       PREV_INSN (next) = note;
4110       
4111       first = first->next_bb;
4112     }
4113
4114   free (bb_header);
4115   bb_header = 0;
4116 }
4117
4118 /* Extend per basic block data structures of the scheduler.
4119    If BB is NULL, initialize structures for the whole CFG.
4120    Otherwise, initialize them for the just created BB.  */
4121 static void
4122 extend_bb (void)
4123 {
4124   rtx insn;
4125
4126   old_last_basic_block = last_basic_block;
4127
4128   /* The following is done to keep current_sched_info->next_tail non null.  */
4129
4130   insn = BB_END (EXIT_BLOCK_PTR->prev_bb);
4131   if (NEXT_INSN (insn) == 0
4132       || (!NOTE_P (insn)
4133           && !LABEL_P (insn)
4134           /* Don't emit a NOTE if it would end up before a BARRIER.  */
4135           && !BARRIER_P (NEXT_INSN (insn))))
4136     {
4137       rtx note = emit_note_after (NOTE_INSN_DELETED, insn);
4138       /* Make insn appear outside BB.  */
4139       set_block_for_insn (note, NULL);
4140       BB_END (EXIT_BLOCK_PTR->prev_bb) = insn;
4141     }
4142 }
4143
4144 /* Add a basic block BB to extended basic block EBB.
4145    If EBB is EXIT_BLOCK_PTR, then BB is recovery block.
4146    If EBB is NULL, then BB should be a new region.  */
4147 void
4148 add_block (basic_block bb, basic_block ebb)
4149 {
4150   gcc_assert (current_sched_info->flags & NEW_BBS);
4151
4152   extend_bb ();
4153
4154   if (current_sched_info->add_block)
4155     /* This changes only data structures of the front-end.  */
4156     current_sched_info->add_block (bb, ebb);
4157 }
4158
4159 /* Helper function.
4160    Fix CFG after both in- and inter-block movement of
4161    control_flow_insn_p JUMP.  */
4162 static void
4163 fix_jump_move (rtx jump)
4164 {
4165   basic_block bb, jump_bb, jump_bb_next;
4166
4167   bb = BLOCK_FOR_INSN (PREV_INSN (jump));
4168   jump_bb = BLOCK_FOR_INSN (jump);
4169   jump_bb_next = jump_bb->next_bb;
4170
4171   gcc_assert (current_sched_info->flags & SCHED_EBB
4172               || IS_SPECULATION_BRANCHY_CHECK_P (jump));
4173   
4174   if (!NOTE_INSN_BASIC_BLOCK_P (BB_END (jump_bb_next)))
4175     /* if jump_bb_next is not empty.  */
4176     BB_END (jump_bb) = BB_END (jump_bb_next);
4177
4178   if (BB_END (bb) != PREV_INSN (jump))
4179     /* Then there are instruction after jump that should be placed
4180        to jump_bb_next.  */
4181     BB_END (jump_bb_next) = BB_END (bb);
4182   else
4183     /* Otherwise jump_bb_next is empty.  */
4184     BB_END (jump_bb_next) = NEXT_INSN (BB_HEAD (jump_bb_next));
4185
4186   /* To make assertion in move_insn happy.  */
4187   BB_END (bb) = PREV_INSN (jump);
4188
4189   update_bb_for_insn (jump_bb_next);
4190 }
4191
4192 /* Fix CFG after interblock movement of control_flow_insn_p JUMP.  */
4193 static void
4194 move_block_after_check (rtx jump)
4195 {
4196   basic_block bb, jump_bb, jump_bb_next;
4197   VEC(edge,gc) *t;
4198
4199   bb = BLOCK_FOR_INSN (PREV_INSN (jump));
4200   jump_bb = BLOCK_FOR_INSN (jump);
4201   jump_bb_next = jump_bb->next_bb;
4202   
4203   update_bb_for_insn (jump_bb);
4204   
4205   gcc_assert (IS_SPECULATION_CHECK_P (jump)
4206               || IS_SPECULATION_CHECK_P (BB_END (jump_bb_next)));
4207
4208   unlink_block (jump_bb_next);
4209   link_block (jump_bb_next, bb);
4210
4211   t = bb->succs;
4212   bb->succs = 0;
4213   move_succs (&(jump_bb->succs), bb);
4214   move_succs (&(jump_bb_next->succs), jump_bb);
4215   move_succs (&t, jump_bb_next);
4216
4217   df_mark_solutions_dirty ();
4218   
4219   if (current_sched_info->fix_recovery_cfg)
4220     current_sched_info->fix_recovery_cfg 
4221       (bb->index, jump_bb->index, jump_bb_next->index);
4222 }
4223
4224 /* Helper function for move_block_after_check.
4225    This functions attaches edge vector pointed to by SUCCSP to
4226    block TO.  */
4227 static void
4228 move_succs (VEC(edge,gc) **succsp, basic_block to)
4229 {
4230   edge e;
4231   edge_iterator ei;
4232
4233   gcc_assert (to->succs == 0);
4234
4235   to->succs = *succsp;
4236
4237   FOR_EACH_EDGE (e, ei, to->succs)
4238     e->src = to;
4239
4240   *succsp = 0;
4241 }
4242
4243 /* Remove INSN from the instruction stream.
4244    INSN should have any dependencies.  */
4245 static void
4246 sched_remove_insn (rtx insn)
4247 {
4248   change_queue_index (insn, QUEUE_NOWHERE);
4249   current_sched_info->add_remove_insn (insn, 1);
4250   remove_insn (insn);
4251 }
4252
4253 /* Clear priorities of all instructions, that are forward dependent on INSN.
4254    Store in vector pointed to by ROOTS_PTR insns on which priority () should
4255    be invoked to initialize all cleared priorities.  */
4256 static void
4257 clear_priorities (rtx insn, rtx_vec_t *roots_ptr)
4258 {
4259   dep_link_t link;
4260   bool insn_is_root_p = true;
4261
4262   gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
4263
4264   FOR_EACH_DEP_LINK (link, INSN_BACK_DEPS (insn))
4265     {
4266       dep_t dep = DEP_LINK_DEP (link);
4267       rtx pro = DEP_PRO (dep);
4268
4269       if (INSN_PRIORITY_STATUS (pro) >= 0
4270           && QUEUE_INDEX (insn) != QUEUE_SCHEDULED)
4271         {
4272           /* If DEP doesn't contribute to priority then INSN itself should
4273              be added to priority roots.  */
4274           if (contributes_to_priority_p (dep))
4275             insn_is_root_p = false;
4276
4277           INSN_PRIORITY_STATUS (pro) = -1;
4278           clear_priorities (pro, roots_ptr);
4279         }
4280     }
4281
4282   if (insn_is_root_p)
4283     VEC_safe_push (rtx, heap, *roots_ptr, insn);
4284 }
4285
4286 /* Recompute priorities of instructions, whose priorities might have been
4287    changed.  ROOTS is a vector of instructions whose priority computation will
4288    trigger initialization of all cleared priorities.  */
4289 static void
4290 calc_priorities (rtx_vec_t roots)
4291 {
4292   int i;
4293   rtx insn;
4294
4295   for (i = 0; VEC_iterate (rtx, roots, i, insn); i++)
4296     priority (insn);
4297 }
4298
4299
4300 /* Add dependences between JUMP and other instructions in the recovery
4301    block.  INSN is the first insn the recovery block.  */
4302 static void
4303 add_jump_dependencies (rtx insn, rtx jump)
4304 {
4305   do
4306     {
4307       insn = NEXT_INSN (insn);
4308       if (insn == jump)
4309         break;
4310       
4311       if (deps_list_empty_p (INSN_FORW_DEPS (insn)))
4312         add_back_forw_dep (jump, insn, REG_DEP_ANTI, DEP_ANTI);
4313     }
4314   while (1);
4315
4316   gcc_assert (!deps_list_empty_p (INSN_BACK_DEPS (jump)));
4317 }
4318
4319 /* Return the NOTE_INSN_BASIC_BLOCK of BB.  */
4320 rtx
4321 bb_note (basic_block bb)
4322 {
4323   rtx note;
4324
4325   note = BB_HEAD (bb);
4326   if (LABEL_P (note))
4327     note = NEXT_INSN (note);
4328
4329   gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4330   return note;
4331 }
4332
4333 #ifdef ENABLE_CHECKING
4334 extern void debug_spec_status (ds_t);
4335
4336 /* Dump information about the dependence status S.  */
4337 void
4338 debug_spec_status (ds_t s)
4339 {
4340   FILE *f = stderr;
4341
4342   if (s & BEGIN_DATA)
4343     fprintf (f, "BEGIN_DATA: %d; ", get_dep_weak (s, BEGIN_DATA));
4344   if (s & BE_IN_DATA)
4345     fprintf (f, "BE_IN_DATA: %d; ", get_dep_weak (s, BE_IN_DATA));
4346   if (s & BEGIN_CONTROL)
4347     fprintf (f, "BEGIN_CONTROL: %d; ", get_dep_weak (s, BEGIN_CONTROL));
4348   if (s & BE_IN_CONTROL)
4349     fprintf (f, "BE_IN_CONTROL: %d; ", get_dep_weak (s, BE_IN_CONTROL));
4350
4351   if (s & HARD_DEP)
4352     fprintf (f, "HARD_DEP; ");
4353
4354   if (s & DEP_TRUE)
4355     fprintf (f, "DEP_TRUE; ");
4356   if (s & DEP_ANTI)
4357     fprintf (f, "DEP_ANTI; ");
4358   if (s & DEP_OUTPUT)
4359     fprintf (f, "DEP_OUTPUT; ");
4360
4361   fprintf (f, "\n");
4362 }
4363
4364 /* Helper function for check_cfg.
4365    Return nonzero, if edge vector pointed to by EL has edge with TYPE in
4366    its flags.  */
4367 static int
4368 has_edge_p (VEC(edge,gc) *el, int type)
4369 {
4370   edge e;
4371   edge_iterator ei;
4372
4373   FOR_EACH_EDGE (e, ei, el)
4374     if (e->flags & type)
4375       return 1;
4376   return 0;
4377 }
4378
4379 /* Check few properties of CFG between HEAD and TAIL.
4380    If HEAD (TAIL) is NULL check from the beginning (till the end) of the
4381    instruction stream.  */
4382 static void
4383 check_cfg (rtx head, rtx tail)
4384 {
4385   rtx next_tail;
4386   basic_block bb = 0;
4387   int not_first = 0, not_last;
4388
4389   if (head == NULL)
4390     head = get_insns ();
4391   if (tail == NULL)
4392     tail = get_last_insn ();
4393   next_tail = NEXT_INSN (tail);
4394
4395   do
4396     {      
4397       not_last = head != tail;        
4398
4399       if (not_first)
4400         gcc_assert (NEXT_INSN (PREV_INSN (head)) == head);
4401       if (not_last)
4402         gcc_assert (PREV_INSN (NEXT_INSN (head)) == head);
4403
4404       if (LABEL_P (head) 
4405           || (NOTE_INSN_BASIC_BLOCK_P (head)
4406               && (!not_first
4407                   || (not_first && !LABEL_P (PREV_INSN (head))))))
4408         {
4409           gcc_assert (bb == 0);   
4410           bb = BLOCK_FOR_INSN (head);
4411           if (bb != 0)
4412             gcc_assert (BB_HEAD (bb) == head);      
4413           else
4414             /* This is the case of jump table.  See inside_basic_block_p ().  */
4415             gcc_assert (LABEL_P (head) && !inside_basic_block_p (head));
4416         }
4417
4418       if (bb == 0)
4419         {
4420           gcc_assert (!inside_basic_block_p (head));
4421           head = NEXT_INSN (head);
4422         }
4423       else
4424         {
4425           gcc_assert (inside_basic_block_p (head)
4426                       || NOTE_P (head));
4427           gcc_assert (BLOCK_FOR_INSN (head) == bb);
4428         
4429           if (LABEL_P (head))
4430             {
4431               head = NEXT_INSN (head);
4432               gcc_assert (NOTE_INSN_BASIC_BLOCK_P (head));
4433             }
4434           else
4435             {
4436               if (control_flow_insn_p (head))
4437                 {
4438                   gcc_assert (BB_END (bb) == head);
4439                   
4440                   if (any_uncondjump_p (head))
4441                     gcc_assert (EDGE_COUNT (bb->succs) == 1
4442                                 && BARRIER_P (NEXT_INSN (head)));
4443                   else if (any_condjump_p (head))
4444                     gcc_assert (/* Usual case.  */
4445                                 (EDGE_COUNT (bb->succs) > 1
4446                                  && !BARRIER_P (NEXT_INSN (head)))
4447                                 /* Or jump to the next instruction.  */
4448                                 || (EDGE_COUNT (bb->succs) == 1
4449                                     && (BB_HEAD (EDGE_I (bb->succs, 0)->dest)
4450                                         == JUMP_LABEL (head))));
4451                 }
4452               if (BB_END (bb) == head)
4453                 {
4454                   if (EDGE_COUNT (bb->succs) > 1)
4455                     gcc_assert (control_flow_insn_p (head)
4456                                 || has_edge_p (bb->succs, EDGE_COMPLEX));
4457                   bb = 0;
4458                 }
4459                               
4460               head = NEXT_INSN (head);
4461             }
4462         }
4463
4464       not_first = 1;
4465     }
4466   while (head != next_tail);
4467
4468   gcc_assert (bb == 0);
4469 }
4470
4471 /* Perform a few consistency checks of flags in different data structures.  */
4472 static void
4473 check_sched_flags (void)
4474 {
4475   unsigned int f = current_sched_info->flags;
4476
4477   if (flag_sched_stalled_insns)
4478     gcc_assert (!(f & DO_SPECULATION));
4479   if (f & DO_SPECULATION)
4480     gcc_assert (!flag_sched_stalled_insns
4481                 && spec_info
4482                 && spec_info->mask);
4483 }
4484 #endif /* ENABLE_CHECKING */
4485
4486 #endif /* INSN_SCHEDULING */