OSDN Git Service

2009-10-19 Andreas Krebbel <Andreas.Krebbel@de.ibm.com>
[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, 2008, 2009
4    Free Software Foundation, Inc.
5    Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
6    and currently maintained by, Jim Wilson (wilson@cygnus.com)
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
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 "vecprim.h"
148 #include "dbgcnt.h"
149 #include "cfgloop.h"
150 #include "ira.h"
151
152 #ifdef INSN_SCHEDULING
153
154 /* issue_rate is the number of insns that can be scheduled in the same
155    machine cycle.  It can be defined in the config/mach/mach.h file,
156    otherwise we set it to 1.  */
157
158 int issue_rate;
159
160 /* sched-verbose controls the amount of debugging output the
161    scheduler prints.  It is controlled by -fsched-verbose=N:
162    N>0 and no -DSR : the output is directed to stderr.
163    N>=10 will direct the printouts to stderr (regardless of -dSR).
164    N=1: same as -dSR.
165    N=2: bb's probabilities, detailed ready list info, unit/insn info.
166    N=3: rtl at abort point, control-flow, regions info.
167    N=5: dependences info.  */
168
169 static int sched_verbose_param = 0;
170 int sched_verbose = 0;
171
172 /* Debugging file.  All printouts are sent to dump, which is always set,
173    either to stderr, or to the dump listing file (-dRS).  */
174 FILE *sched_dump = 0;
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 /* This is a placeholder for the scheduler parameters common 
189    to all schedulers.  */
190 struct common_sched_info_def *common_sched_info;
191
192 #define INSN_TICK(INSN) (HID (INSN)->tick)
193 #define INTER_TICK(INSN) (HID (INSN)->inter_tick)
194
195 /* If INSN_TICK of an instruction is equal to INVALID_TICK,
196    then it should be recalculated from scratch.  */
197 #define INVALID_TICK (-(max_insn_queue_index + 1))
198 /* The minimal value of the INSN_TICK of an instruction.  */
199 #define MIN_TICK (-max_insn_queue_index)
200
201 /* Issue points are used to distinguish between instructions in max_issue ().
202    For now, all instructions are equally good.  */
203 #define ISSUE_POINTS(INSN) 1
204
205 /* List of important notes we must keep around.  This is a pointer to the
206    last element in the list.  */
207 rtx note_list;
208
209 static struct spec_info_def spec_info_var;
210 /* Description of the speculative part of the scheduling.
211    If NULL - no speculation.  */
212 spec_info_t spec_info = NULL;
213
214 /* True, if recovery block was added during scheduling of current block.
215    Used to determine, if we need to fix INSN_TICKs.  */
216 static bool haifa_recovery_bb_recently_added_p;
217
218 /* True, if recovery block was added during this scheduling pass.
219    Used to determine if we should have empty memory pools of dependencies
220    after finishing current region.  */
221 bool haifa_recovery_bb_ever_added_p;
222
223 /* Counters of different types of speculative instructions.  */
224 static int nr_begin_data, nr_be_in_data, nr_begin_control, nr_be_in_control;
225
226 /* Array used in {unlink, restore}_bb_notes.  */
227 static rtx *bb_header = 0;
228
229 /* Basic block after which recovery blocks will be created.  */
230 static basic_block before_recovery;
231
232 /* Basic block just before the EXIT_BLOCK and after recovery, if we have
233    created it.  */
234 basic_block after_recovery;
235
236 /* FALSE if we add bb to another region, so we don't need to initialize it.  */
237 bool adding_bb_to_current_region_p = true;
238
239 /* Queues, etc.  */
240
241 /* An instruction is ready to be scheduled when all insns preceding it
242    have already been scheduled.  It is important to ensure that all
243    insns which use its result will not be executed until its result
244    has been computed.  An insn is maintained in one of four structures:
245
246    (P) the "Pending" set of insns which cannot be scheduled until
247    their dependencies have been satisfied.
248    (Q) the "Queued" set of insns that can be scheduled when sufficient
249    time has passed.
250    (R) the "Ready" list of unscheduled, uncommitted insns.
251    (S) the "Scheduled" list of insns.
252
253    Initially, all insns are either "Pending" or "Ready" depending on
254    whether their dependencies are satisfied.
255
256    Insns move from the "Ready" list to the "Scheduled" list as they
257    are committed to the schedule.  As this occurs, the insns in the
258    "Pending" list have their dependencies satisfied and move to either
259    the "Ready" list or the "Queued" set depending on whether
260    sufficient time has passed to make them ready.  As time passes,
261    insns move from the "Queued" set to the "Ready" list.
262
263    The "Pending" list (P) are the insns in the INSN_FORW_DEPS of the
264    unscheduled insns, i.e., those that are ready, queued, and pending.
265    The "Queued" set (Q) is implemented by the variable `insn_queue'.
266    The "Ready" list (R) is implemented by the variables `ready' and
267    `n_ready'.
268    The "Scheduled" list (S) is the new insn chain built by this pass.
269
270    The transition (R->S) is implemented in the scheduling loop in
271    `schedule_block' when the best insn to schedule is chosen.
272    The transitions (P->R and P->Q) are implemented in `schedule_insn' as
273    insns move from the ready list to the scheduled list.
274    The transition (Q->R) is implemented in 'queue_to_insn' as time
275    passes or stalls are introduced.  */
276
277 /* Implement a circular buffer to delay instructions until sufficient
278    time has passed.  For the new pipeline description interface,
279    MAX_INSN_QUEUE_INDEX is a power of two minus one which is not less
280    than maximal time of instruction execution computed by genattr.c on
281    the base maximal time of functional unit reservations and getting a
282    result.  This is the longest time an insn may be queued.  */
283
284 static rtx *insn_queue;
285 static int q_ptr = 0;
286 static int q_size = 0;
287 #define NEXT_Q(X) (((X)+1) & max_insn_queue_index)
288 #define NEXT_Q_AFTER(X, C) (((X)+C) & max_insn_queue_index)
289
290 #define QUEUE_SCHEDULED (-3)
291 #define QUEUE_NOWHERE   (-2)
292 #define QUEUE_READY     (-1)
293 /* QUEUE_SCHEDULED - INSN is scheduled.
294    QUEUE_NOWHERE   - INSN isn't scheduled yet and is neither in
295    queue or ready list.
296    QUEUE_READY     - INSN is in ready list.
297    N >= 0 - INSN queued for X [where NEXT_Q_AFTER (q_ptr, X) == N] cycles.  */
298    
299 #define QUEUE_INDEX(INSN) (HID (INSN)->queue_index)
300
301 /* The following variable value refers for all current and future
302    reservations of the processor units.  */
303 state_t curr_state;
304
305 /* The following variable value is size of memory representing all
306    current and future reservations of the processor units.  */
307 size_t dfa_state_size;
308
309 /* The following array is used to find the best insn from ready when
310    the automaton pipeline interface is used.  */
311 char *ready_try = NULL;
312
313 /* The ready list.  */
314 struct ready_list ready = {NULL, 0, 0, 0, 0};
315
316 /* The pointer to the ready list (to be removed).  */
317 static struct ready_list *readyp = &ready;
318
319 /* Scheduling clock.  */
320 static int clock_var;
321
322 static int may_trap_exp (const_rtx, int);
323
324 /* Nonzero iff the address is comprised from at most 1 register.  */
325 #define CONST_BASED_ADDRESS_P(x)                        \
326   (REG_P (x)                                    \
327    || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS   \
328         || (GET_CODE (x) == LO_SUM))                    \
329        && (CONSTANT_P (XEXP (x, 0))                     \
330            || CONSTANT_P (XEXP (x, 1)))))
331
332 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
333    as found by analyzing insn's expression.  */
334
335 \f
336 static int haifa_luid_for_non_insn (rtx x);
337
338 /* Haifa version of sched_info hooks common to all headers.  */
339 const struct common_sched_info_def haifa_common_sched_info = 
340   {
341     NULL, /* fix_recovery_cfg */
342     NULL, /* add_block */
343     NULL, /* estimate_number_of_insns */
344     haifa_luid_for_non_insn, /* luid_for_non_insn */
345     SCHED_PASS_UNKNOWN /* sched_pass_id */
346   };
347
348 const struct sched_scan_info_def *sched_scan_info;
349
350 /* Mapping from instruction UID to its Logical UID.  */
351 VEC (int, heap) *sched_luids = NULL;
352
353 /* Next LUID to assign to an instruction.  */
354 int sched_max_luid = 1;
355
356 /* Haifa Instruction Data.  */
357 VEC (haifa_insn_data_def, heap) *h_i_d = NULL;
358
359 void (* sched_init_only_bb) (basic_block, basic_block);
360
361 /* Split block function.  Different schedulers might use different functions
362    to handle their internal data consistent.  */
363 basic_block (* sched_split_block) (basic_block, rtx);
364
365 /* Create empty basic block after the specified block.  */
366 basic_block (* sched_create_empty_bb) (basic_block);
367
368 static int
369 may_trap_exp (const_rtx x, int is_store)
370 {
371   enum rtx_code code;
372
373   if (x == 0)
374     return TRAP_FREE;
375   code = GET_CODE (x);
376   if (is_store)
377     {
378       if (code == MEM && may_trap_p (x))
379         return TRAP_RISKY;
380       else
381         return TRAP_FREE;
382     }
383   if (code == MEM)
384     {
385       /* The insn uses memory:  a volatile load.  */
386       if (MEM_VOLATILE_P (x))
387         return IRISKY;
388       /* An exception-free load.  */
389       if (!may_trap_p (x))
390         return IFREE;
391       /* A load with 1 base register, to be further checked.  */
392       if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
393         return PFREE_CANDIDATE;
394       /* No info on the load, to be further checked.  */
395       return PRISKY_CANDIDATE;
396     }
397   else
398     {
399       const char *fmt;
400       int i, insn_class = TRAP_FREE;
401
402       /* Neither store nor load, check if it may cause a trap.  */
403       if (may_trap_p (x))
404         return TRAP_RISKY;
405       /* Recursive step: walk the insn...  */
406       fmt = GET_RTX_FORMAT (code);
407       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
408         {
409           if (fmt[i] == 'e')
410             {
411               int tmp_class = may_trap_exp (XEXP (x, i), is_store);
412               insn_class = WORST_CLASS (insn_class, tmp_class);
413             }
414           else if (fmt[i] == 'E')
415             {
416               int j;
417               for (j = 0; j < XVECLEN (x, i); j++)
418                 {
419                   int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
420                   insn_class = WORST_CLASS (insn_class, tmp_class);
421                   if (insn_class == TRAP_RISKY || insn_class == IRISKY)
422                     break;
423                 }
424             }
425           if (insn_class == TRAP_RISKY || insn_class == IRISKY)
426             break;
427         }
428       return insn_class;
429     }
430 }
431
432 /* Classifies rtx X of an insn for the purpose of verifying that X can be
433    executed speculatively (and consequently the insn can be moved
434    speculatively), by examining X, returning:
435    TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
436    TRAP_FREE: non-load insn.
437    IFREE: load from a globally safe location.
438    IRISKY: volatile load.
439    PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
440    being either PFREE or PRISKY.  */
441
442 static int
443 haifa_classify_rtx (const_rtx x)
444 {
445   int tmp_class = TRAP_FREE;
446   int insn_class = TRAP_FREE;
447   enum rtx_code code;
448
449   if (GET_CODE (x) == PARALLEL)
450     {
451       int i, len = XVECLEN (x, 0);
452
453       for (i = len - 1; i >= 0; i--)
454         {
455           tmp_class = haifa_classify_rtx (XVECEXP (x, 0, i));
456           insn_class = WORST_CLASS (insn_class, tmp_class);
457           if (insn_class == TRAP_RISKY || insn_class == IRISKY)
458             break;
459         }
460     }
461   else
462     {
463       code = GET_CODE (x);
464       switch (code)
465         {
466         case CLOBBER:
467           /* Test if it is a 'store'.  */
468           tmp_class = may_trap_exp (XEXP (x, 0), 1);
469           break;
470         case SET:
471           /* Test if it is a store.  */
472           tmp_class = may_trap_exp (SET_DEST (x), 1);
473           if (tmp_class == TRAP_RISKY)
474             break;
475           /* Test if it is a load.  */
476           tmp_class =
477             WORST_CLASS (tmp_class,
478                          may_trap_exp (SET_SRC (x), 0));
479           break;
480         case COND_EXEC:
481           tmp_class = haifa_classify_rtx (COND_EXEC_CODE (x));
482           if (tmp_class == TRAP_RISKY)
483             break;
484           tmp_class = WORST_CLASS (tmp_class,
485                                    may_trap_exp (COND_EXEC_TEST (x), 0));
486           break;
487         case TRAP_IF:
488           tmp_class = TRAP_RISKY;
489           break;
490         default:;
491         }
492       insn_class = tmp_class;
493     }
494
495   return insn_class;
496 }
497
498 int
499 haifa_classify_insn (const_rtx insn)
500 {
501   return haifa_classify_rtx (PATTERN (insn));
502 }
503
504 /* Forward declarations.  */
505
506 static int priority (rtx);
507 static int rank_for_schedule (const void *, const void *);
508 static void swap_sort (rtx *, int);
509 static void queue_insn (rtx, int);
510 static int schedule_insn (rtx);
511 static void adjust_priority (rtx);
512 static void advance_one_cycle (void);
513 static void extend_h_i_d (void);
514
515
516 /* Notes handling mechanism:
517    =========================
518    Generally, NOTES are saved before scheduling and restored after scheduling.
519    The scheduler distinguishes between two types of notes:
520
521    (1) LOOP_BEGIN, LOOP_END, SETJMP, EHREGION_BEG, EHREGION_END notes:
522    Before scheduling a region, a pointer to the note is added to the insn
523    that follows or precedes it.  (This happens as part of the data dependence
524    computation).  After scheduling an insn, the pointer contained in it is
525    used for regenerating the corresponding note (in reemit_notes).
526
527    (2) All other notes (e.g. INSN_DELETED):  Before scheduling a block,
528    these notes are put in a list (in rm_other_notes() and
529    unlink_other_notes ()).  After scheduling the block, these notes are
530    inserted at the beginning of the block (in schedule_block()).  */
531
532 static void ready_add (struct ready_list *, rtx, bool);
533 static rtx ready_remove_first (struct ready_list *);
534
535 static void queue_to_ready (struct ready_list *);
536 static int early_queue_to_ready (state_t, struct ready_list *);
537
538 static void debug_ready_list (struct ready_list *);
539
540 /* The following functions are used to implement multi-pass scheduling
541    on the first cycle.  */
542 static rtx ready_remove (struct ready_list *, int);
543 static void ready_remove_insn (rtx);
544
545 static int choose_ready (struct ready_list *, rtx *);
546
547 static void fix_inter_tick (rtx, rtx);
548 static int fix_tick_ready (rtx);
549 static void change_queue_index (rtx, int);
550
551 /* The following functions are used to implement scheduling of data/control
552    speculative instructions.  */
553
554 static void extend_h_i_d (void);
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 (rtx, rtx, ds_t);
558 static void begin_speculative_block (rtx);
559 static void add_to_speculative_block (rtx);
560 static void init_before_recovery (basic_block *);
561 static void create_check_block_twin (rtx, bool);
562 static void fix_recovery_deps (basic_block);
563 static void haifa_change_pattern (rtx, rtx);
564 static void dump_new_block_header (int, basic_block, rtx, rtx);
565 static void restore_bb_notes (basic_block);
566 static void fix_jump_move (rtx);
567 static void move_block_after_check (rtx);
568 static void move_succs (VEC(edge,gc) **, basic_block);
569 static void sched_remove_insn (rtx);
570 static void clear_priorities (rtx, rtx_vec_t *);
571 static void calc_priorities (rtx_vec_t);
572 static void add_jump_dependencies (rtx, rtx);
573 #ifdef ENABLE_CHECKING
574 static int has_edge_p (VEC(edge,gc) *, int);
575 static void check_cfg (rtx, rtx);
576 #endif
577
578 #endif /* INSN_SCHEDULING */
579 \f
580 /* Point to state used for the current scheduling pass.  */
581 struct haifa_sched_info *current_sched_info;
582 \f
583 #ifndef INSN_SCHEDULING
584 void
585 schedule_insns (void)
586 {
587 }
588 #else
589
590 /* Do register pressure sensitive insn scheduling if the flag is set
591    up.  */
592 bool sched_pressure_p;
593
594 /* Map regno -> its cover class.  The map defined only when
595    SCHED_PRESSURE_P is true.  */
596 enum reg_class *sched_regno_cover_class;
597
598 /* The current register pressure.  Only elements corresponding cover
599    classes are defined.  */
600 static int curr_reg_pressure[N_REG_CLASSES];
601
602 /* Saved value of the previous array.  */
603 static int saved_reg_pressure[N_REG_CLASSES];
604
605 /* Register living at given scheduling point.  */
606 static bitmap curr_reg_live;
607
608 /* Saved value of the previous array.  */
609 static bitmap saved_reg_live;
610
611 /* Registers mentioned in the current region.  */
612 static bitmap region_ref_regs;
613
614 /* Initiate register pressure relative info for scheduling the current
615    region.  Currently it is only clearing register mentioned in the
616    current region.  */
617 void
618 sched_init_region_reg_pressure_info (void)
619 {
620   bitmap_clear (region_ref_regs);
621 }
622
623 /* Update current register pressure related info after birth (if
624    BIRTH_P) or death of register REGNO.  */
625 static void
626 mark_regno_birth_or_death (int regno, bool birth_p)
627 {
628   enum reg_class cover_class;
629
630   cover_class = sched_regno_cover_class[regno];
631   if (regno >= FIRST_PSEUDO_REGISTER)
632     {
633       if (cover_class != NO_REGS)
634         {
635           if (birth_p)
636             {
637               bitmap_set_bit (curr_reg_live, regno);
638               curr_reg_pressure[cover_class]
639                 += ira_reg_class_nregs[cover_class][PSEUDO_REGNO_MODE (regno)];
640             }
641           else
642             {
643               bitmap_clear_bit (curr_reg_live, regno);
644               curr_reg_pressure[cover_class]
645                 -= ira_reg_class_nregs[cover_class][PSEUDO_REGNO_MODE (regno)];
646             }
647         }
648     }
649   else if (cover_class != NO_REGS
650            && ! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
651     {
652       if (birth_p)
653         {
654           bitmap_set_bit (curr_reg_live, regno);
655           curr_reg_pressure[cover_class]++;
656         }
657       else
658         {
659           bitmap_clear_bit (curr_reg_live, regno);
660           curr_reg_pressure[cover_class]--;
661         }
662     }
663 }
664
665 /* Initiate current register pressure related info from living
666    registers given by LIVE.  */
667 static void
668 initiate_reg_pressure_info (bitmap live)
669 {
670   int i;
671   unsigned int j;
672   bitmap_iterator bi;
673
674   for (i = 0; i < ira_reg_class_cover_size; i++)
675     curr_reg_pressure[ira_reg_class_cover[i]] = 0;
676   bitmap_clear (curr_reg_live);
677   EXECUTE_IF_SET_IN_BITMAP (live, 0, j, bi)
678     if (current_nr_blocks == 1 || bitmap_bit_p (region_ref_regs, j))
679       mark_regno_birth_or_death (j, true);
680 }
681
682 /* Mark registers in X as mentioned in the current region.  */
683 static void
684 setup_ref_regs (rtx x)
685 {
686   int i, j, regno;
687   const RTX_CODE code = GET_CODE (x);
688   const char *fmt;
689
690   if (REG_P (x))
691     {
692       regno = REGNO (x);
693       if (regno >= FIRST_PSEUDO_REGISTER)
694         bitmap_set_bit (region_ref_regs, REGNO (x));
695       else
696         for (i = hard_regno_nregs[regno][GET_MODE (x)] - 1; i >= 0; i--)
697           bitmap_set_bit (region_ref_regs, regno + i);
698       return;
699     }
700   fmt = GET_RTX_FORMAT (code);
701   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
702     if (fmt[i] == 'e')
703       setup_ref_regs (XEXP (x, i));
704     else if (fmt[i] == 'E')
705       {
706         for (j = 0; j < XVECLEN (x, i); j++)
707           setup_ref_regs (XVECEXP (x, i, j));
708       }
709 }
710
711 /* Initiate current register pressure related info at the start of
712    basic block BB.  */
713 static void
714 initiate_bb_reg_pressure_info (basic_block bb)
715 {
716   unsigned int i;
717   rtx insn;
718
719   if (current_nr_blocks > 1)
720     FOR_BB_INSNS (bb, insn)
721       if (INSN_P (insn))
722         setup_ref_regs (PATTERN (insn));
723   initiate_reg_pressure_info (df_get_live_in (bb));
724 #ifdef EH_RETURN_DATA_REGNO
725   if (bb_has_eh_pred (bb))
726     for (i = 0; ; ++i)
727       {
728         unsigned int regno = EH_RETURN_DATA_REGNO (i);
729         
730         if (regno == INVALID_REGNUM)
731           break;
732         if (! bitmap_bit_p (df_get_live_in (bb), regno))
733           mark_regno_birth_or_death (regno, true);
734       }
735 #endif
736 }
737
738 /* Save current register pressure related info.  */
739 static void
740 save_reg_pressure (void)
741 {
742   int i;
743   
744   for (i = 0; i < ira_reg_class_cover_size; i++)
745     saved_reg_pressure[ira_reg_class_cover[i]]
746       = curr_reg_pressure[ira_reg_class_cover[i]];
747   bitmap_copy (saved_reg_live, curr_reg_live);
748 }
749
750 /* Restore saved register pressure related info.  */
751 static void
752 restore_reg_pressure (void)
753 {
754   int i;
755   
756   for (i = 0; i < ira_reg_class_cover_size; i++)
757     curr_reg_pressure[ira_reg_class_cover[i]]
758       = saved_reg_pressure[ira_reg_class_cover[i]];
759   bitmap_copy (curr_reg_live, saved_reg_live);
760 }
761
762 /* Return TRUE if the register is dying after its USE.  */
763 static bool
764 dying_use_p (struct reg_use_data *use)
765 {
766   struct reg_use_data *next;
767
768   for (next = use->next_regno_use; next != use; next = next->next_regno_use)
769     if (QUEUE_INDEX (next->insn) != QUEUE_SCHEDULED)
770       return false;
771   return true;
772 }
773
774 /* Print info about the current register pressure and its excess for
775    each cover class.  */
776 static void
777 print_curr_reg_pressure (void)
778 {
779   int i;
780   enum reg_class cl;
781
782   fprintf (sched_dump, ";;\t");
783   for (i = 0; i < ira_reg_class_cover_size; i++)
784     {
785       cl = ira_reg_class_cover[i];
786       gcc_assert (curr_reg_pressure[cl] >= 0);
787       fprintf (sched_dump, "  %s:%d(%d)", reg_class_names[cl],
788                curr_reg_pressure[cl],
789                curr_reg_pressure[cl] - ira_available_class_regs[cl]);
790     }
791   fprintf (sched_dump, "\n");
792 }
793
794 /* Pointer to the last instruction scheduled.  Used by rank_for_schedule,
795    so that insns independent of the last scheduled insn will be preferred
796    over dependent instructions.  */
797
798 static rtx last_scheduled_insn;
799
800 /* Cached cost of the instruction.  Use below function to get cost of the
801    insn.  -1 here means that the field is not initialized.  */
802 #define INSN_COST(INSN) (HID (INSN)->cost)
803
804 /* Compute cost of executing INSN.
805    This is the number of cycles between instruction issue and
806    instruction results.  */
807 int
808 insn_cost (rtx insn)
809 {
810   int cost;
811
812   if (sel_sched_p ())
813     {
814       if (recog_memoized (insn) < 0)
815         return 0;
816
817       cost = insn_default_latency (insn);
818       if (cost < 0)
819         cost = 0;
820
821       return cost;
822     }
823
824   cost = INSN_COST (insn);
825
826   if (cost < 0)
827     {
828       /* A USE insn, or something else we don't need to
829          understand.  We can't pass these directly to
830          result_ready_cost or insn_default_latency because it will
831          trigger a fatal error for unrecognizable insns.  */
832       if (recog_memoized (insn) < 0)
833         {
834           INSN_COST (insn) = 0;
835           return 0;
836         }
837       else
838         {
839           cost = insn_default_latency (insn);
840           if (cost < 0)
841             cost = 0;
842
843           INSN_COST (insn) = cost;
844         }
845     }
846
847   return cost;
848 }
849
850 /* Compute cost of dependence LINK.
851    This is the number of cycles between instruction issue and
852    instruction results.
853    ??? We also use this function to call recog_memoized on all insns.  */
854 int
855 dep_cost_1 (dep_t link, dw_t dw)
856 {
857   rtx insn = DEP_PRO (link);
858   rtx used = DEP_CON (link);
859   int cost;
860
861   /* A USE insn should never require the value used to be computed.
862      This allows the computation of a function's result and parameter
863      values to overlap the return and call.  We don't care about the
864      the dependence cost when only decreasing register pressure.  */
865   if (recog_memoized (used) < 0)
866     {
867       cost = 0;
868       recog_memoized (insn);
869     }
870   else
871     {
872       enum reg_note dep_type = DEP_TYPE (link);
873
874       cost = insn_cost (insn);
875
876       if (INSN_CODE (insn) >= 0)
877         {
878           if (dep_type == REG_DEP_ANTI)
879             cost = 0;
880           else if (dep_type == REG_DEP_OUTPUT)
881             {
882               cost = (insn_default_latency (insn)
883                       - insn_default_latency (used));
884               if (cost <= 0)
885                 cost = 1;
886             }
887           else if (bypass_p (insn))
888             cost = insn_latency (insn, used);
889         }
890         
891
892       if (targetm.sched.adjust_cost_2)
893         cost = targetm.sched.adjust_cost_2 (used, (int) dep_type, insn, cost,
894                                             dw);
895       else if (targetm.sched.adjust_cost != NULL)
896         {
897           /* This variable is used for backward compatibility with the
898              targets.  */
899           rtx dep_cost_rtx_link = alloc_INSN_LIST (NULL_RTX, NULL_RTX);
900
901           /* Make it self-cycled, so that if some tries to walk over this
902              incomplete list he/she will be caught in an endless loop.  */
903           XEXP (dep_cost_rtx_link, 1) = dep_cost_rtx_link;
904
905           /* Targets use only REG_NOTE_KIND of the link.  */
906           PUT_REG_NOTE_KIND (dep_cost_rtx_link, DEP_TYPE (link));
907
908           cost = targetm.sched.adjust_cost (used, dep_cost_rtx_link,
909                                             insn, cost);
910
911           free_INSN_LIST_node (dep_cost_rtx_link);
912         }
913
914       if (cost < 0)
915         cost = 0;
916     }
917
918   return cost;
919 }
920
921 /* Compute cost of dependence LINK.
922    This is the number of cycles between instruction issue and
923    instruction results.  */
924 int
925 dep_cost (dep_t link)
926 {
927   return dep_cost_1 (link, 0);
928 }
929
930 /* Use this sel-sched.c friendly function in reorder2 instead of increasing
931    INSN_PRIORITY explicitly.  */
932 void
933 increase_insn_priority (rtx insn, int amount)
934 {
935   if (!sel_sched_p ())
936     {
937       /* We're dealing with haifa-sched.c INSN_PRIORITY.  */
938       if (INSN_PRIORITY_KNOWN (insn))
939           INSN_PRIORITY (insn) += amount;
940     }
941   else
942     {
943       /* In sel-sched.c INSN_PRIORITY is not kept up to date.  
944          Use EXPR_PRIORITY instead. */
945       sel_add_to_insn_priority (insn, amount);
946     }
947 }
948
949 /* Return 'true' if DEP should be included in priority calculations.  */
950 static bool
951 contributes_to_priority_p (dep_t dep)
952 {
953   if (DEBUG_INSN_P (DEP_CON (dep))
954       || DEBUG_INSN_P (DEP_PRO (dep)))
955     return false;
956
957   /* Critical path is meaningful in block boundaries only.  */
958   if (!current_sched_info->contributes_to_priority (DEP_CON (dep),
959                                                     DEP_PRO (dep)))
960     return false;
961
962   /* If flag COUNT_SPEC_IN_CRITICAL_PATH is set,
963      then speculative instructions will less likely be
964      scheduled.  That is because the priority of
965      their producers will increase, and, thus, the
966      producers will more likely be scheduled, thus,
967      resolving the dependence.  */
968   if (sched_deps_info->generate_spec_deps
969       && !(spec_info->flags & COUNT_SPEC_IN_CRITICAL_PATH)
970       && (DEP_STATUS (dep) & SPECULATIVE))
971     return false;
972
973   return true;
974 }
975
976 /* Compute the number of nondebug forward deps of an insn.  */
977
978 static int
979 dep_list_size (rtx insn)
980 {
981   sd_iterator_def sd_it;
982   dep_t dep;
983   int dbgcount = 0, nodbgcount = 0;
984
985   if (!MAY_HAVE_DEBUG_INSNS)
986     return sd_lists_size (insn, SD_LIST_FORW);
987
988   FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
989     {
990       if (DEBUG_INSN_P (DEP_CON (dep)))
991         dbgcount++;
992       else if (!DEBUG_INSN_P (DEP_PRO (dep)))
993         nodbgcount++;
994     }
995
996   gcc_assert (dbgcount + nodbgcount == sd_lists_size (insn, SD_LIST_FORW));
997
998   return nodbgcount;
999 }
1000
1001 /* Compute the priority number for INSN.  */
1002 static int
1003 priority (rtx insn)
1004 {
1005   if (! INSN_P (insn))
1006     return 0;
1007
1008   /* We should not be interested in priority of an already scheduled insn.  */
1009   gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
1010
1011   if (!INSN_PRIORITY_KNOWN (insn))
1012     {
1013       int this_priority = -1;
1014
1015       if (dep_list_size (insn) == 0)
1016         /* ??? We should set INSN_PRIORITY to insn_cost when and insn has
1017            some forward deps but all of them are ignored by
1018            contributes_to_priority hook.  At the moment we set priority of
1019            such insn to 0.  */
1020         this_priority = insn_cost (insn);
1021       else
1022         {
1023           rtx prev_first, twin;
1024           basic_block rec;
1025
1026           /* For recovery check instructions we calculate priority slightly
1027              different than that of normal instructions.  Instead of walking
1028              through INSN_FORW_DEPS (check) list, we walk through
1029              INSN_FORW_DEPS list of each instruction in the corresponding
1030              recovery block.  */ 
1031
1032           /* Selective scheduling does not define RECOVERY_BLOCK macro.  */
1033           rec = sel_sched_p () ? NULL : RECOVERY_BLOCK (insn);
1034           if (!rec || rec == EXIT_BLOCK_PTR)
1035             {
1036               prev_first = PREV_INSN (insn);
1037               twin = insn;
1038             }
1039           else
1040             {
1041               prev_first = NEXT_INSN (BB_HEAD (rec));
1042               twin = PREV_INSN (BB_END (rec));
1043             }
1044
1045           do
1046             {
1047               sd_iterator_def sd_it;
1048               dep_t dep;
1049
1050               FOR_EACH_DEP (twin, SD_LIST_FORW, sd_it, dep)
1051                 {
1052                   rtx next;
1053                   int next_priority;
1054
1055                   next = DEP_CON (dep);
1056
1057                   if (BLOCK_FOR_INSN (next) != rec)
1058                     {
1059                       int cost;
1060
1061                       if (!contributes_to_priority_p (dep))
1062                         continue;
1063
1064                       if (twin == insn)
1065                         cost = dep_cost (dep);
1066                       else
1067                         {
1068                           struct _dep _dep1, *dep1 = &_dep1;
1069
1070                           init_dep (dep1, insn, next, REG_DEP_ANTI);
1071
1072                           cost = dep_cost (dep1);
1073                         }
1074
1075                       next_priority = cost + priority (next);
1076
1077                       if (next_priority > this_priority)
1078                         this_priority = next_priority;
1079                     }
1080                 }
1081               
1082               twin = PREV_INSN (twin);
1083             }
1084           while (twin != prev_first);
1085         }
1086
1087       if (this_priority < 0)
1088         {
1089           gcc_assert (this_priority == -1);
1090
1091           this_priority = insn_cost (insn);
1092         }
1093
1094       INSN_PRIORITY (insn) = this_priority;
1095       INSN_PRIORITY_STATUS (insn) = 1;
1096     }
1097
1098   return INSN_PRIORITY (insn);
1099 }
1100 \f
1101 /* Macros and functions for keeping the priority queue sorted, and
1102    dealing with queuing and dequeuing of instructions.  */
1103
1104 #define SCHED_SORT(READY, N_READY)                                   \
1105 do { if ((N_READY) == 2)                                             \
1106        swap_sort (READY, N_READY);                                   \
1107      else if ((N_READY) > 2)                                         \
1108          qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); }  \
1109 while (0)
1110
1111 /* Setup info about the current register pressure impact of scheduling
1112    INSN at the current scheduling point.  */
1113 static void
1114 setup_insn_reg_pressure_info (rtx insn)
1115 {
1116   int i, change, before, after, hard_regno;
1117   int excess_cost_change;
1118   enum machine_mode mode;
1119   enum reg_class cl;
1120   struct reg_pressure_data *pressure_info;
1121   int *max_reg_pressure;
1122   struct reg_use_data *use;
1123   static int death[N_REG_CLASSES];
1124
1125   excess_cost_change = 0;
1126   for (i = 0; i < ira_reg_class_cover_size; i++)
1127     death[ira_reg_class_cover[i]] = 0;
1128   for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
1129     if (dying_use_p (use))
1130       {
1131         cl = sched_regno_cover_class[use->regno];
1132         if (use->regno < FIRST_PSEUDO_REGISTER)
1133           death[cl]++;
1134         else
1135           death[cl] += ira_reg_class_nregs[cl][PSEUDO_REGNO_MODE (use->regno)];
1136       }
1137   pressure_info = INSN_REG_PRESSURE (insn);
1138   max_reg_pressure = INSN_MAX_REG_PRESSURE (insn);
1139   gcc_assert (pressure_info != NULL && max_reg_pressure != NULL);
1140   for (i = 0; i < ira_reg_class_cover_size; i++)
1141     {
1142       cl = ira_reg_class_cover[i];
1143       gcc_assert (curr_reg_pressure[cl] >= 0);
1144       change = (int) pressure_info[i].set_increase - death[cl];
1145       before = MAX (0, max_reg_pressure[i] - ira_available_class_regs[cl]);
1146       after = MAX (0, max_reg_pressure[i] + change
1147                    - ira_available_class_regs[cl]);
1148       hard_regno = ira_class_hard_regs[cl][0];
1149       gcc_assert (hard_regno >= 0);
1150       mode = reg_raw_mode[hard_regno];
1151       excess_cost_change += ((after - before)
1152                              * (ira_memory_move_cost[mode][cl][0]
1153                                 + ira_memory_move_cost[mode][cl][1]));
1154     }
1155   INSN_REG_PRESSURE_EXCESS_COST_CHANGE (insn) = excess_cost_change;
1156 }
1157
1158 /* Returns a positive value if x is preferred; returns a negative value if
1159    y is preferred.  Should never return 0, since that will make the sort
1160    unstable.  */
1161
1162 static int
1163 rank_for_schedule (const void *x, const void *y)
1164 {
1165   rtx tmp = *(const rtx *) y;
1166   rtx tmp2 = *(const rtx *) x;
1167   rtx last;
1168   int tmp_class, tmp2_class;
1169   int val, priority_val, info_val;
1170
1171   if (MAY_HAVE_DEBUG_INSNS)
1172     {
1173       /* Schedule debug insns as early as possible.  */
1174       if (DEBUG_INSN_P (tmp) && !DEBUG_INSN_P (tmp2))
1175         return -1;
1176       else if (DEBUG_INSN_P (tmp2))
1177         return 1;
1178     }
1179
1180   /* The insn in a schedule group should be issued the first.  */
1181   if (flag_sched_group_heuristic && 
1182       SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
1183     return SCHED_GROUP_P (tmp2) ? 1 : -1;
1184
1185   /* Make sure that priority of TMP and TMP2 are initialized.  */
1186   gcc_assert (INSN_PRIORITY_KNOWN (tmp) && INSN_PRIORITY_KNOWN (tmp2));
1187
1188   if (sched_pressure_p)
1189     {
1190       int diff;
1191
1192       /* Prefer insn whose scheduling results in the smallest register
1193          pressure excess.  */
1194       if ((diff = (INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp)
1195                    + (INSN_TICK (tmp) > clock_var
1196                       ? INSN_TICK (tmp) - clock_var : 0)
1197                    - INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp2)
1198                    - (INSN_TICK (tmp2) > clock_var
1199                       ? INSN_TICK (tmp2) - clock_var : 0))) != 0)
1200         return diff;
1201     }
1202
1203
1204   if (sched_pressure_p
1205       && (INSN_TICK (tmp2) > clock_var || INSN_TICK (tmp) > clock_var))
1206     {
1207       if (INSN_TICK (tmp) <= clock_var)
1208         return -1;
1209       else if (INSN_TICK (tmp2) <= clock_var)
1210         return 1;
1211       else
1212         return INSN_TICK (tmp) - INSN_TICK (tmp2);
1213     }
1214   /* Prefer insn with higher priority.  */
1215   priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
1216
1217   if (flag_sched_critical_path_heuristic && priority_val)
1218     return priority_val;
1219     
1220   /* Prefer speculative insn with greater dependencies weakness.  */
1221   if (flag_sched_spec_insn_heuristic && spec_info)
1222     {
1223       ds_t ds1, ds2;
1224       dw_t dw1, dw2;
1225       int dw;
1226
1227       ds1 = TODO_SPEC (tmp) & SPECULATIVE;
1228       if (ds1)
1229         dw1 = ds_weak (ds1);
1230       else
1231         dw1 = NO_DEP_WEAK;
1232       
1233       ds2 = TODO_SPEC (tmp2) & SPECULATIVE;
1234       if (ds2)
1235         dw2 = ds_weak (ds2);
1236       else
1237         dw2 = NO_DEP_WEAK;
1238
1239       dw = dw2 - dw1;
1240       if (dw > (NO_DEP_WEAK / 8) || dw < -(NO_DEP_WEAK / 8))
1241         return dw;
1242     }
1243
1244   info_val = (*current_sched_info->rank) (tmp, tmp2);
1245   if(flag_sched_rank_heuristic && info_val)
1246     return info_val;
1247
1248   if (flag_sched_last_insn_heuristic)
1249     {
1250       last = last_scheduled_insn;
1251
1252       if (DEBUG_INSN_P (last) && last != current_sched_info->prev_head)
1253         do
1254           last = PREV_INSN (last);
1255         while (!NONDEBUG_INSN_P (last)
1256                && last != current_sched_info->prev_head);
1257     }
1258
1259   /* Compare insns based on their relation to the last scheduled
1260      non-debug insn.  */
1261   if (flag_sched_last_insn_heuristic && NONDEBUG_INSN_P (last))
1262     {
1263       dep_t dep1;
1264       dep_t dep2;
1265
1266       /* Classify the instructions into three classes:
1267          1) Data dependent on last schedule insn.
1268          2) Anti/Output dependent on last scheduled insn.
1269          3) Independent of last scheduled insn, or has latency of one.
1270          Choose the insn from the highest numbered class if different.  */
1271       dep1 = sd_find_dep_between (last, tmp, true);
1272
1273       if (dep1 == NULL || dep_cost (dep1) == 1)
1274         tmp_class = 3;
1275       else if (/* Data dependence.  */
1276                DEP_TYPE (dep1) == REG_DEP_TRUE)
1277         tmp_class = 1;
1278       else
1279         tmp_class = 2;
1280
1281       dep2 = sd_find_dep_between (last, tmp2, true);
1282
1283       if (dep2 == NULL || dep_cost (dep2)  == 1)
1284         tmp2_class = 3;
1285       else if (/* Data dependence.  */
1286                DEP_TYPE (dep2) == REG_DEP_TRUE)
1287         tmp2_class = 1;
1288       else
1289         tmp2_class = 2;
1290
1291       if ((val = tmp2_class - tmp_class))
1292         return val;
1293     }
1294
1295   /* Prefer the insn which has more later insns that depend on it.
1296      This gives the scheduler more freedom when scheduling later
1297      instructions at the expense of added register pressure.  */
1298
1299   val = (dep_list_size (tmp2) - dep_list_size (tmp));
1300
1301   if (flag_sched_dep_count_heuristic && val != 0)
1302     return val;
1303
1304   /* If insns are equally good, sort by INSN_LUID (original insn order),
1305      so that we make the sort stable.  This minimizes instruction movement,
1306      thus minimizing sched's effect on debugging and cross-jumping.  */
1307   return INSN_LUID (tmp) - INSN_LUID (tmp2);
1308 }
1309
1310 /* Resort the array A in which only element at index N may be out of order.  */
1311
1312 HAIFA_INLINE static void
1313 swap_sort (rtx *a, int n)
1314 {
1315   rtx insn = a[n - 1];
1316   int i = n - 2;
1317
1318   while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
1319     {
1320       a[i + 1] = a[i];
1321       i -= 1;
1322     }
1323   a[i + 1] = insn;
1324 }
1325
1326 /* Add INSN to the insn queue so that it can be executed at least
1327    N_CYCLES after the currently executing insn.  Preserve insns
1328    chain for debugging purposes.  */
1329
1330 HAIFA_INLINE static void
1331 queue_insn (rtx insn, int n_cycles)
1332 {
1333   int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
1334   rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
1335
1336   gcc_assert (n_cycles <= max_insn_queue_index);
1337   gcc_assert (!DEBUG_INSN_P (insn));
1338
1339   insn_queue[next_q] = link;
1340   q_size += 1;
1341
1342   if (sched_verbose >= 2)
1343     {
1344       fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
1345                (*current_sched_info->print_insn) (insn, 0));
1346
1347       fprintf (sched_dump, "queued for %d cycles.\n", n_cycles);
1348     }
1349
1350   QUEUE_INDEX (insn) = next_q;
1351 }
1352
1353 /* Remove INSN from queue.  */
1354 static void
1355 queue_remove (rtx insn)
1356 {
1357   gcc_assert (QUEUE_INDEX (insn) >= 0);
1358   remove_free_INSN_LIST_elem (insn, &insn_queue[QUEUE_INDEX (insn)]);
1359   q_size--;
1360   QUEUE_INDEX (insn) = QUEUE_NOWHERE;
1361 }
1362
1363 /* Return a pointer to the bottom of the ready list, i.e. the insn
1364    with the lowest priority.  */
1365
1366 rtx *
1367 ready_lastpos (struct ready_list *ready)
1368 {
1369   gcc_assert (ready->n_ready >= 1);
1370   return ready->vec + ready->first - ready->n_ready + 1;
1371 }
1372
1373 /* Add an element INSN to the ready list so that it ends up with the
1374    lowest/highest priority depending on FIRST_P.  */
1375
1376 HAIFA_INLINE static void
1377 ready_add (struct ready_list *ready, rtx insn, bool first_p)
1378 {
1379   if (!first_p)
1380     {
1381       if (ready->first == ready->n_ready)
1382         {
1383           memmove (ready->vec + ready->veclen - ready->n_ready,
1384                    ready_lastpos (ready),
1385                    ready->n_ready * sizeof (rtx));
1386           ready->first = ready->veclen - 1;
1387         }
1388       ready->vec[ready->first - ready->n_ready] = insn;
1389     }
1390   else
1391     {
1392       if (ready->first == ready->veclen - 1)
1393         {
1394           if (ready->n_ready)
1395             /* ready_lastpos() fails when called with (ready->n_ready == 0).  */
1396             memmove (ready->vec + ready->veclen - ready->n_ready - 1,
1397                      ready_lastpos (ready),
1398                      ready->n_ready * sizeof (rtx));
1399           ready->first = ready->veclen - 2;
1400         }
1401       ready->vec[++(ready->first)] = insn;
1402     }
1403
1404   ready->n_ready++;
1405   if (DEBUG_INSN_P (insn))
1406     ready->n_debug++;
1407
1408   gcc_assert (QUEUE_INDEX (insn) != QUEUE_READY);
1409   QUEUE_INDEX (insn) = QUEUE_READY;
1410 }
1411
1412 /* Remove the element with the highest priority from the ready list and
1413    return it.  */
1414
1415 HAIFA_INLINE static rtx
1416 ready_remove_first (struct ready_list *ready)
1417 {
1418   rtx t;
1419   
1420   gcc_assert (ready->n_ready);
1421   t = ready->vec[ready->first--];
1422   ready->n_ready--;
1423   if (DEBUG_INSN_P (t))
1424     ready->n_debug--;
1425   /* If the queue becomes empty, reset it.  */
1426   if (ready->n_ready == 0)
1427     ready->first = ready->veclen - 1;
1428
1429   gcc_assert (QUEUE_INDEX (t) == QUEUE_READY);
1430   QUEUE_INDEX (t) = QUEUE_NOWHERE;
1431
1432   return t;
1433 }
1434
1435 /* The following code implements multi-pass scheduling for the first
1436    cycle.  In other words, we will try to choose ready insn which
1437    permits to start maximum number of insns on the same cycle.  */
1438
1439 /* Return a pointer to the element INDEX from the ready.  INDEX for
1440    insn with the highest priority is 0, and the lowest priority has
1441    N_READY - 1.  */
1442
1443 rtx
1444 ready_element (struct ready_list *ready, int index)
1445 {
1446   gcc_assert (ready->n_ready && index < ready->n_ready);
1447   
1448   return ready->vec[ready->first - index];
1449 }
1450
1451 /* Remove the element INDEX from the ready list and return it.  INDEX
1452    for insn with the highest priority is 0, and the lowest priority
1453    has N_READY - 1.  */
1454
1455 HAIFA_INLINE static rtx
1456 ready_remove (struct ready_list *ready, int index)
1457 {
1458   rtx t;
1459   int i;
1460
1461   if (index == 0)
1462     return ready_remove_first (ready);
1463   gcc_assert (ready->n_ready && index < ready->n_ready);
1464   t = ready->vec[ready->first - index];
1465   ready->n_ready--;
1466   if (DEBUG_INSN_P (t))
1467     ready->n_debug--;
1468   for (i = index; i < ready->n_ready; i++)
1469     ready->vec[ready->first - i] = ready->vec[ready->first - i - 1];
1470   QUEUE_INDEX (t) = QUEUE_NOWHERE;
1471   return t;
1472 }
1473
1474 /* Remove INSN from the ready list.  */
1475 static void
1476 ready_remove_insn (rtx insn)
1477 {
1478   int i;
1479
1480   for (i = 0; i < readyp->n_ready; i++)
1481     if (ready_element (readyp, i) == insn)
1482       {
1483         ready_remove (readyp, i);
1484         return;
1485       }
1486   gcc_unreachable ();
1487 }
1488
1489 /* Sort the ready list READY by ascending priority, using the SCHED_SORT
1490    macro.  */
1491
1492 void
1493 ready_sort (struct ready_list *ready)
1494 {
1495   int i;
1496   rtx *first = ready_lastpos (ready);
1497
1498   if (sched_pressure_p)
1499     {
1500       for (i = 0; i < ready->n_ready; i++)
1501         setup_insn_reg_pressure_info (first[i]);
1502     }
1503   SCHED_SORT (first, ready->n_ready);
1504 }
1505
1506 /* PREV is an insn that is ready to execute.  Adjust its priority if that
1507    will help shorten or lengthen register lifetimes as appropriate.  Also
1508    provide a hook for the target to tweak itself.  */
1509
1510 HAIFA_INLINE static void
1511 adjust_priority (rtx prev)
1512 {
1513   /* ??? There used to be code here to try and estimate how an insn
1514      affected register lifetimes, but it did it by looking at REG_DEAD
1515      notes, which we removed in schedule_region.  Nor did it try to
1516      take into account register pressure or anything useful like that.
1517
1518      Revisit when we have a machine model to work with and not before.  */
1519
1520   if (targetm.sched.adjust_priority)
1521     INSN_PRIORITY (prev) =
1522       targetm.sched.adjust_priority (prev, INSN_PRIORITY (prev));
1523 }
1524
1525 /* Advance DFA state STATE on one cycle.  */
1526 void
1527 advance_state (state_t state)
1528 {
1529   if (targetm.sched.dfa_pre_advance_cycle)
1530     targetm.sched.dfa_pre_advance_cycle ();
1531
1532   if (targetm.sched.dfa_pre_cycle_insn)
1533     state_transition (state,
1534                       targetm.sched.dfa_pre_cycle_insn ());
1535
1536   state_transition (state, NULL);
1537   
1538   if (targetm.sched.dfa_post_cycle_insn)
1539     state_transition (state,
1540                       targetm.sched.dfa_post_cycle_insn ());
1541
1542   if (targetm.sched.dfa_post_advance_cycle)
1543     targetm.sched.dfa_post_advance_cycle ();
1544 }
1545
1546 /* Advance time on one cycle.  */
1547 HAIFA_INLINE static void
1548 advance_one_cycle (void)
1549 {
1550   advance_state (curr_state);
1551   if (sched_verbose >= 6)
1552     fprintf (sched_dump, ";;\tAdvanced a state.\n");
1553 }
1554
1555 /* Clock at which the previous instruction was issued.  */
1556 static int last_clock_var;
1557
1558 /* Update register pressure after scheduling INSN.  */
1559 static void
1560 update_register_pressure (rtx insn)
1561 {
1562   struct reg_use_data *use;
1563   struct reg_set_data *set;
1564
1565   for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
1566     if (dying_use_p (use) && bitmap_bit_p (curr_reg_live, use->regno))
1567       mark_regno_birth_or_death (use->regno, false);
1568   for (set = INSN_REG_SET_LIST (insn); set != NULL; set = set->next_insn_set)
1569     mark_regno_birth_or_death (set->regno, true);
1570 }
1571
1572 /* Set up or update (if UPDATE_P) max register pressure (see its
1573    meaning in sched-int.h::_haifa_insn_data) for all current BB insns
1574    after insn AFTER.  */
1575 static void
1576 setup_insn_max_reg_pressure (rtx after, bool update_p)
1577 {
1578   int i, p;
1579   bool eq_p;
1580   rtx insn;
1581   static int max_reg_pressure[N_REG_CLASSES];
1582
1583   save_reg_pressure ();
1584   for (i = 0; i < ira_reg_class_cover_size; i++)
1585     max_reg_pressure[ira_reg_class_cover[i]]
1586       = curr_reg_pressure[ira_reg_class_cover[i]];
1587   for (insn = NEXT_INSN (after);
1588        insn != NULL_RTX && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (after);
1589        insn = NEXT_INSN (insn))
1590     if (NONDEBUG_INSN_P (insn))
1591       {
1592         eq_p = true;
1593         for (i = 0; i < ira_reg_class_cover_size; i++)
1594           {
1595             p = max_reg_pressure[ira_reg_class_cover[i]];
1596             if (INSN_MAX_REG_PRESSURE (insn)[i] != p)
1597               {
1598                 eq_p = false;
1599                 INSN_MAX_REG_PRESSURE (insn)[i]
1600                   = max_reg_pressure[ira_reg_class_cover[i]];
1601               }
1602           }
1603         if (update_p && eq_p)
1604           break;
1605         update_register_pressure (insn);
1606         for (i = 0; i < ira_reg_class_cover_size; i++)
1607           if (max_reg_pressure[ira_reg_class_cover[i]]
1608               < curr_reg_pressure[ira_reg_class_cover[i]])
1609             max_reg_pressure[ira_reg_class_cover[i]]
1610               = curr_reg_pressure[ira_reg_class_cover[i]];
1611       }
1612   restore_reg_pressure ();
1613 }
1614
1615 /* Update the current register pressure after scheduling INSN.  Update
1616    also max register pressure for unscheduled insns of the current
1617    BB.  */
1618 static void
1619 update_reg_and_insn_max_reg_pressure (rtx insn)
1620 {
1621   int i;
1622   int before[N_REG_CLASSES];
1623
1624   for (i = 0; i < ira_reg_class_cover_size; i++)
1625     before[i] = curr_reg_pressure[ira_reg_class_cover[i]];
1626   update_register_pressure (insn);
1627   for (i = 0; i < ira_reg_class_cover_size; i++)
1628     if (curr_reg_pressure[ira_reg_class_cover[i]] != before[i])
1629       break;
1630   if (i < ira_reg_class_cover_size)
1631     setup_insn_max_reg_pressure (insn, true);
1632 }
1633
1634 /* Set up register pressure at the beginning of basic block BB whose
1635    insns starting after insn AFTER.  Set up also max register pressure
1636    for all insns of the basic block.  */
1637 void
1638 sched_setup_bb_reg_pressure_info (basic_block bb, rtx after)
1639 {
1640   gcc_assert (sched_pressure_p);
1641   initiate_bb_reg_pressure_info (bb);
1642   setup_insn_max_reg_pressure (after, false);
1643 }
1644
1645 /* INSN is the "currently executing insn".  Launch each insn which was
1646    waiting on INSN.  READY is the ready list which contains the insns
1647    that are ready to fire.  CLOCK is the current cycle.  The function
1648    returns necessary cycle advance after issuing the insn (it is not
1649    zero for insns in a schedule group).  */
1650
1651 static int
1652 schedule_insn (rtx insn)
1653 {
1654   sd_iterator_def sd_it;
1655   dep_t dep;
1656   int i;
1657   int advance = 0;
1658
1659   if (sched_verbose >= 1)
1660     {
1661       struct reg_pressure_data *pressure_info;
1662       char buf[2048];
1663
1664       print_insn (buf, insn, 0);
1665       buf[40] = 0;
1666       fprintf (sched_dump, ";;\t%3i--> %-40s:", clock_var, buf);
1667
1668       if (recog_memoized (insn) < 0)
1669         fprintf (sched_dump, "nothing");
1670       else
1671         print_reservation (sched_dump, insn);
1672       pressure_info = INSN_REG_PRESSURE (insn);
1673       if (pressure_info != NULL)
1674         {
1675           fputc (':', sched_dump);
1676           for (i = 0; i < ira_reg_class_cover_size; i++)
1677             fprintf (sched_dump, "%s%+d(%d)",
1678                      reg_class_names[ira_reg_class_cover[i]],
1679                      pressure_info[i].set_increase, pressure_info[i].change);
1680         }
1681       fputc ('\n', sched_dump);
1682     }
1683
1684   if (sched_pressure_p)
1685     update_reg_and_insn_max_reg_pressure (insn);
1686
1687   /* Scheduling instruction should have all its dependencies resolved and
1688      should have been removed from the ready list.  */
1689   gcc_assert (sd_lists_empty_p (insn, SD_LIST_BACK));
1690
1691   /* Reset debug insns invalidated by moving this insn.  */
1692   if (MAY_HAVE_DEBUG_INSNS && !DEBUG_INSN_P (insn))
1693     for (sd_it = sd_iterator_start (insn, SD_LIST_BACK);
1694          sd_iterator_cond (&sd_it, &dep);)
1695       {
1696         rtx dbg = DEP_PRO (dep);
1697
1698         gcc_assert (DEBUG_INSN_P (dbg));
1699
1700         if (sched_verbose >= 6)
1701           fprintf (sched_dump, ";;\t\tresetting: debug insn %d\n",
1702                    INSN_UID (dbg));
1703
1704         /* ??? Rather than resetting the debug insn, we might be able
1705            to emit a debug temp before the just-scheduled insn, but
1706            this would involve checking that the expression at the
1707            point of the debug insn is equivalent to the expression
1708            before the just-scheduled insn.  They might not be: the
1709            expression in the debug insn may depend on other insns not
1710            yet scheduled that set MEMs, REGs or even other debug
1711            insns.  It's not clear that attempting to preserve debug
1712            information in these cases is worth the effort, given how
1713            uncommon these resets are and the likelihood that the debug
1714            temps introduced won't survive the schedule change.  */
1715         INSN_VAR_LOCATION_LOC (dbg) = gen_rtx_UNKNOWN_VAR_LOC ();
1716         df_insn_rescan (dbg);
1717
1718         /* We delete rather than resolve these deps, otherwise we
1719            crash in sched_free_deps(), because forward deps are
1720            expected to be released before backward deps.  */
1721         sd_delete_dep (sd_it);
1722       }
1723
1724   gcc_assert (QUEUE_INDEX (insn) == QUEUE_NOWHERE);
1725   QUEUE_INDEX (insn) = QUEUE_SCHEDULED;
1726
1727   gcc_assert (INSN_TICK (insn) >= MIN_TICK);
1728   if (INSN_TICK (insn) > clock_var)
1729     /* INSN has been prematurely moved from the queue to the ready list.
1730        This is possible only if following flag is set.  */
1731     gcc_assert (flag_sched_stalled_insns);    
1732
1733   /* ??? Probably, if INSN is scheduled prematurely, we should leave
1734      INSN_TICK untouched.  This is a machine-dependent issue, actually.  */
1735   INSN_TICK (insn) = clock_var;
1736
1737   /* Update dependent instructions.  */
1738   for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
1739        sd_iterator_cond (&sd_it, &dep);)
1740     {
1741       rtx next = DEP_CON (dep);
1742
1743       /* Resolve the dependence between INSN and NEXT.
1744          sd_resolve_dep () moves current dep to another list thus
1745          advancing the iterator.  */
1746       sd_resolve_dep (sd_it);
1747
1748       /* Don't bother trying to mark next as ready if insn is a debug
1749          insn.  If insn is the last hard dependency, it will have
1750          already been discounted.  */
1751       if (DEBUG_INSN_P (insn) && !DEBUG_INSN_P (next))
1752         continue;
1753
1754       if (!IS_SPECULATION_BRANCHY_CHECK_P (insn))
1755         {
1756           int effective_cost;      
1757           
1758           effective_cost = try_ready (next);
1759           
1760           if (effective_cost >= 0
1761               && SCHED_GROUP_P (next)
1762               && advance < effective_cost)
1763             advance = effective_cost;
1764         }
1765       else
1766         /* Check always has only one forward dependence (to the first insn in
1767            the recovery block), therefore, this will be executed only once.  */
1768         {
1769           gcc_assert (sd_lists_empty_p (insn, SD_LIST_FORW));
1770           fix_recovery_deps (RECOVERY_BLOCK (insn));
1771         }
1772     }
1773
1774   /* This is the place where scheduler doesn't *basically* need backward and
1775      forward dependencies for INSN anymore.  Nevertheless they are used in
1776      heuristics in rank_for_schedule (), early_queue_to_ready () and in
1777      some targets (e.g. rs6000).  Thus the earliest place where we *can*
1778      remove dependencies is after targetm.sched.md_finish () call in
1779      schedule_block ().  But, on the other side, the safest place to remove
1780      dependencies is when we are finishing scheduling entire region.  As we
1781      don't generate [many] dependencies during scheduling itself, we won't
1782      need memory until beginning of next region.
1783      Bottom line: Dependencies are removed for all insns in the end of
1784      scheduling the region.  */
1785
1786   /* Annotate the instruction with issue information -- TImode
1787      indicates that the instruction is expected not to be able
1788      to issue on the same cycle as the previous insn.  A machine
1789      may use this information to decide how the instruction should
1790      be aligned.  */
1791   if (issue_rate > 1
1792       && GET_CODE (PATTERN (insn)) != USE
1793       && GET_CODE (PATTERN (insn)) != CLOBBER
1794       && !DEBUG_INSN_P (insn))
1795     {
1796       if (reload_completed)
1797         PUT_MODE (insn, clock_var > last_clock_var ? TImode : VOIDmode);
1798       last_clock_var = clock_var;
1799     }
1800
1801   return advance;
1802 }
1803
1804 /* Functions for handling of notes.  */
1805
1806 /* Insert the INSN note at the end of the notes list.  */
1807 static void 
1808 add_to_note_list (rtx insn, rtx *note_list_end_p)
1809 {
1810   PREV_INSN (insn) = *note_list_end_p;
1811   if (*note_list_end_p)
1812     NEXT_INSN (*note_list_end_p) = insn;
1813   *note_list_end_p = insn;
1814 }
1815
1816 /* Add note list that ends on FROM_END to the end of TO_ENDP.  */
1817 void
1818 concat_note_lists (rtx from_end, rtx *to_endp)
1819 {
1820   rtx from_start;
1821
1822   if (from_end == NULL)
1823     /* It's easy when have nothing to concat.  */
1824     return;
1825
1826   if (*to_endp == NULL)
1827     /* It's also easy when destination is empty.  */
1828     {
1829       *to_endp = from_end;
1830       return;
1831     }
1832
1833   from_start = from_end;
1834   /* A note list should be traversed via PREV_INSN.  */
1835   while (PREV_INSN (from_start) != NULL) 
1836     from_start = PREV_INSN (from_start);
1837
1838   add_to_note_list (from_start, to_endp);
1839   *to_endp = from_end;
1840 }
1841
1842 /* Delete notes beginning with INSN and put them in the chain
1843    of notes ended by NOTE_LIST.
1844    Returns the insn following the notes.  */
1845 static rtx
1846 unlink_other_notes (rtx insn, rtx tail)
1847 {
1848   rtx prev = PREV_INSN (insn);
1849
1850   while (insn != tail && NOTE_NOT_BB_P (insn))
1851     {
1852       rtx next = NEXT_INSN (insn);
1853       basic_block bb = BLOCK_FOR_INSN (insn);
1854
1855       /* Delete the note from its current position.  */
1856       if (prev)
1857         NEXT_INSN (prev) = next;
1858       if (next)
1859         PREV_INSN (next) = prev;
1860
1861       if (bb)
1862         {
1863           /* Basic block can begin with either LABEL or
1864              NOTE_INSN_BASIC_BLOCK.  */
1865           gcc_assert (BB_HEAD (bb) != insn);
1866
1867           /* Check if we are removing last insn in the BB.  */
1868           if (BB_END (bb) == insn)
1869             BB_END (bb) = prev;
1870         }
1871
1872       /* See sched_analyze to see how these are handled.  */
1873       if (NOTE_KIND (insn) != NOTE_INSN_EH_REGION_BEG
1874           && NOTE_KIND (insn) != NOTE_INSN_EH_REGION_END)
1875         add_to_note_list (insn, &note_list);
1876
1877       insn = next;
1878     }
1879
1880   if (insn == tail)
1881     {
1882       gcc_assert (sel_sched_p ());
1883       return prev;
1884     }
1885
1886   return insn;
1887 }
1888
1889 /* Return the head and tail pointers of ebb starting at BEG and ending
1890    at END.  */
1891 void
1892 get_ebb_head_tail (basic_block beg, basic_block end, rtx *headp, rtx *tailp)
1893 {
1894   rtx beg_head = BB_HEAD (beg);
1895   rtx beg_tail = BB_END (beg);
1896   rtx end_head = BB_HEAD (end);
1897   rtx end_tail = BB_END (end);
1898
1899   /* Don't include any notes or labels at the beginning of the BEG
1900      basic block, or notes at the end of the END basic blocks.  */
1901
1902   if (LABEL_P (beg_head))
1903     beg_head = NEXT_INSN (beg_head);
1904
1905   while (beg_head != beg_tail)
1906     if (NOTE_P (beg_head) || BOUNDARY_DEBUG_INSN_P (beg_head))
1907       beg_head = NEXT_INSN (beg_head);
1908     else
1909       break;
1910
1911   *headp = beg_head;
1912
1913   if (beg == end)
1914     end_head = beg_head;
1915   else if (LABEL_P (end_head))
1916     end_head = NEXT_INSN (end_head);
1917
1918   while (end_head != end_tail)
1919     if (NOTE_P (end_tail) || BOUNDARY_DEBUG_INSN_P (end_tail))
1920       end_tail = PREV_INSN (end_tail);
1921     else
1922       break;
1923
1924   *tailp = end_tail;
1925 }
1926
1927 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ].  */
1928
1929 int
1930 no_real_insns_p (const_rtx head, const_rtx tail)
1931 {
1932   while (head != NEXT_INSN (tail))
1933     {
1934       if (!NOTE_P (head) && !LABEL_P (head)
1935           && !BOUNDARY_DEBUG_INSN_P (head))
1936         return 0;
1937       head = NEXT_INSN (head);
1938     }
1939   return 1;
1940 }
1941
1942 /* Delete notes between HEAD and TAIL and put them in the chain
1943    of notes ended by NOTE_LIST.  */
1944 static void
1945 rm_other_notes (rtx head, rtx tail)
1946 {
1947   rtx next_tail;
1948   rtx insn;
1949
1950   note_list = 0;
1951   if (head == tail && (! INSN_P (head)))
1952     return;
1953
1954   next_tail = NEXT_INSN (tail);
1955   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1956     {
1957       rtx prev;
1958
1959       /* Farm out notes, and maybe save them in NOTE_LIST.
1960          This is needed to keep the debugger from
1961          getting completely deranged.  */
1962       if (NOTE_NOT_BB_P (insn))
1963         {
1964           prev = insn;
1965           insn = unlink_other_notes (insn, next_tail);
1966
1967           gcc_assert ((sel_sched_p ()
1968                        || prev != tail) && prev != head && insn != next_tail);
1969         }
1970     }
1971 }
1972
1973 /* Same as above, but also process REG_SAVE_NOTEs of HEAD.  */
1974 void
1975 remove_notes (rtx head, rtx tail)
1976 {
1977   /* rm_other_notes only removes notes which are _inside_ the
1978      block---that is, it won't remove notes before the first real insn
1979      or after the last real insn of the block.  So if the first insn
1980      has a REG_SAVE_NOTE which would otherwise be emitted before the
1981      insn, it is redundant with the note before the start of the
1982      block, and so we have to take it out.  */
1983   if (INSN_P (head))
1984     {
1985       rtx note;
1986
1987       for (note = REG_NOTES (head); note; note = XEXP (note, 1))
1988         if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
1989           remove_note (head, note);
1990     }
1991
1992   /* Remove remaining note insns from the block, save them in
1993      note_list.  These notes are restored at the end of
1994      schedule_block ().  */
1995   rm_other_notes (head, tail);
1996 }
1997
1998 /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
1999    previously found among the insns.  Insert them just before HEAD.  */
2000 rtx
2001 restore_other_notes (rtx head, basic_block head_bb)
2002 {
2003   if (note_list != 0)
2004     {
2005       rtx note_head = note_list;
2006
2007       if (head)
2008         head_bb = BLOCK_FOR_INSN (head);
2009       else
2010         head = NEXT_INSN (bb_note (head_bb));
2011
2012       while (PREV_INSN (note_head))
2013         {
2014           set_block_for_insn (note_head, head_bb);
2015           note_head = PREV_INSN (note_head);
2016         }
2017       /* In the above cycle we've missed this note.  */
2018       set_block_for_insn (note_head, head_bb);
2019
2020       PREV_INSN (note_head) = PREV_INSN (head);
2021       NEXT_INSN (PREV_INSN (head)) = note_head;
2022       PREV_INSN (head) = note_list;
2023       NEXT_INSN (note_list) = head;
2024
2025       if (BLOCK_FOR_INSN (head) != head_bb)
2026         BB_END (head_bb) = note_list;
2027
2028       head = note_head;
2029     }
2030
2031   return head;
2032 }
2033
2034 /* Move insns that became ready to fire from queue to ready list.  */
2035
2036 static void
2037 queue_to_ready (struct ready_list *ready)
2038 {
2039   rtx insn;
2040   rtx link;
2041   rtx skip_insn;
2042
2043   q_ptr = NEXT_Q (q_ptr);
2044
2045   if (dbg_cnt (sched_insn) == false)
2046     {
2047       /* If debug counter is activated do not requeue insn next after
2048          last_scheduled_insn.  */
2049       skip_insn = next_nonnote_insn (last_scheduled_insn);
2050       while (skip_insn && DEBUG_INSN_P (skip_insn))
2051         skip_insn = next_nonnote_insn (skip_insn);
2052     }
2053   else
2054     skip_insn = NULL_RTX;
2055
2056   /* Add all pending insns that can be scheduled without stalls to the
2057      ready list.  */
2058   for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
2059     {
2060       insn = XEXP (link, 0);
2061       q_size -= 1;
2062
2063       if (sched_verbose >= 2)
2064         fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
2065                  (*current_sched_info->print_insn) (insn, 0));
2066
2067       /* If the ready list is full, delay the insn for 1 cycle.
2068          See the comment in schedule_block for the rationale.  */
2069       if (!reload_completed
2070           && ready->n_ready - ready->n_debug > MAX_SCHED_READY_INSNS
2071           && !SCHED_GROUP_P (insn)
2072           && insn != skip_insn)
2073         {
2074           if (sched_verbose >= 2)
2075             fprintf (sched_dump, "requeued because ready full\n");
2076           queue_insn (insn, 1);
2077         }
2078       else
2079         {
2080           ready_add (ready, insn, false);
2081           if (sched_verbose >= 2)
2082             fprintf (sched_dump, "moving to ready without stalls\n");
2083         }
2084     }
2085   free_INSN_LIST_list (&insn_queue[q_ptr]);
2086
2087   /* If there are no ready insns, stall until one is ready and add all
2088      of the pending insns at that point to the ready list.  */
2089   if (ready->n_ready == 0)
2090     {
2091       int stalls;
2092
2093       for (stalls = 1; stalls <= max_insn_queue_index; stalls++)
2094         {
2095           if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
2096             {
2097               for (; link; link = XEXP (link, 1))
2098                 {
2099                   insn = XEXP (link, 0);
2100                   q_size -= 1;
2101
2102                   if (sched_verbose >= 2)
2103                     fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
2104                              (*current_sched_info->print_insn) (insn, 0));
2105
2106                   ready_add (ready, insn, false);
2107                   if (sched_verbose >= 2)
2108                     fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
2109                 }
2110               free_INSN_LIST_list (&insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]);
2111
2112               advance_one_cycle ();
2113
2114               break;
2115             }
2116
2117           advance_one_cycle ();
2118         }
2119
2120       q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
2121       clock_var += stalls;
2122     }
2123 }
2124
2125 /* Used by early_queue_to_ready.  Determines whether it is "ok" to
2126    prematurely move INSN from the queue to the ready list.  Currently, 
2127    if a target defines the hook 'is_costly_dependence', this function 
2128    uses the hook to check whether there exist any dependences which are
2129    considered costly by the target, between INSN and other insns that 
2130    have already been scheduled.  Dependences are checked up to Y cycles
2131    back, with default Y=1; The flag -fsched-stalled-insns-dep=Y allows
2132    controlling this value. 
2133    (Other considerations could be taken into account instead (or in 
2134    addition) depending on user flags and target hooks.  */
2135
2136 static bool 
2137 ok_for_early_queue_removal (rtx insn)
2138 {
2139   int n_cycles;
2140   rtx prev_insn = last_scheduled_insn;
2141
2142   if (targetm.sched.is_costly_dependence)
2143     {
2144       for (n_cycles = flag_sched_stalled_insns_dep; n_cycles; n_cycles--)
2145         {
2146           for ( ; prev_insn; prev_insn = PREV_INSN (prev_insn))
2147             {
2148               int cost;
2149
2150               if (prev_insn == current_sched_info->prev_head)
2151                 {
2152                   prev_insn = NULL;
2153                   break;
2154                 }
2155
2156               if (!NOTE_P (prev_insn))
2157                 {
2158                   dep_t dep;
2159
2160                   dep = sd_find_dep_between (prev_insn, insn, true);
2161
2162                   if (dep != NULL)
2163                     {
2164                       cost = dep_cost (dep);
2165
2166                       if (targetm.sched.is_costly_dependence (dep, cost,
2167                                 flag_sched_stalled_insns_dep - n_cycles))
2168                         return false;
2169                     }
2170                 }
2171
2172               if (GET_MODE (prev_insn) == TImode) /* end of dispatch group */
2173                 break;
2174             }
2175
2176           if (!prev_insn) 
2177             break;
2178           prev_insn = PREV_INSN (prev_insn);     
2179         }
2180     }
2181
2182   return true;
2183 }
2184
2185
2186 /* Remove insns from the queue, before they become "ready" with respect
2187    to FU latency considerations.  */
2188
2189 static int 
2190 early_queue_to_ready (state_t state, struct ready_list *ready)
2191 {
2192   rtx insn;
2193   rtx link;
2194   rtx next_link;
2195   rtx prev_link;
2196   bool move_to_ready;
2197   int cost;
2198   state_t temp_state = alloca (dfa_state_size);
2199   int stalls;
2200   int insns_removed = 0;
2201
2202   /*
2203      Flag '-fsched-stalled-insns=X' determines the aggressiveness of this 
2204      function: 
2205
2206      X == 0: There is no limit on how many queued insns can be removed          
2207              prematurely.  (flag_sched_stalled_insns = -1).
2208
2209      X >= 1: Only X queued insns can be removed prematurely in each 
2210              invocation.  (flag_sched_stalled_insns = X).
2211
2212      Otherwise: Early queue removal is disabled.
2213          (flag_sched_stalled_insns = 0)
2214   */
2215
2216   if (! flag_sched_stalled_insns)   
2217     return 0;
2218
2219   for (stalls = 0; stalls <= max_insn_queue_index; stalls++)
2220     {
2221       if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
2222         {
2223           if (sched_verbose > 6)
2224             fprintf (sched_dump, ";; look at index %d + %d\n", q_ptr, stalls);
2225
2226           prev_link = 0;
2227           while (link)
2228             {
2229               next_link = XEXP (link, 1);
2230               insn = XEXP (link, 0);
2231               if (insn && sched_verbose > 6)
2232                 print_rtl_single (sched_dump, insn);
2233
2234               memcpy (temp_state, state, dfa_state_size);
2235               if (recog_memoized (insn) < 0) 
2236                 /* non-negative to indicate that it's not ready
2237                    to avoid infinite Q->R->Q->R... */
2238                 cost = 0;
2239               else
2240                 cost = state_transition (temp_state, insn);
2241
2242               if (sched_verbose >= 6)
2243                 fprintf (sched_dump, "transition cost = %d\n", cost);
2244
2245               move_to_ready = false;
2246               if (cost < 0) 
2247                 {
2248                   move_to_ready = ok_for_early_queue_removal (insn);
2249                   if (move_to_ready == true)
2250                     {
2251                       /* move from Q to R */
2252                       q_size -= 1;
2253                       ready_add (ready, insn, false);
2254
2255                       if (prev_link)   
2256                         XEXP (prev_link, 1) = next_link;
2257                       else
2258                         insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = next_link;
2259
2260                       free_INSN_LIST_node (link);
2261
2262                       if (sched_verbose >= 2)
2263                         fprintf (sched_dump, ";;\t\tEarly Q-->Ready: insn %s\n",
2264                                  (*current_sched_info->print_insn) (insn, 0));
2265
2266                       insns_removed++;
2267                       if (insns_removed == flag_sched_stalled_insns)
2268                         /* Remove no more than flag_sched_stalled_insns insns
2269                            from Q at a time.  */
2270                         return insns_removed;
2271                     }
2272                 }
2273
2274               if (move_to_ready == false)
2275                 prev_link = link;
2276
2277               link = next_link;
2278             } /* while link */
2279         } /* if link */    
2280
2281     } /* for stalls.. */
2282
2283   return insns_removed; 
2284 }
2285
2286
2287 /* Print the ready list for debugging purposes.  Callable from debugger.  */
2288
2289 static void
2290 debug_ready_list (struct ready_list *ready)
2291 {
2292   rtx *p;
2293   int i;
2294
2295   if (ready->n_ready == 0)
2296     {
2297       fprintf (sched_dump, "\n");
2298       return;
2299     }
2300
2301   p = ready_lastpos (ready);
2302   for (i = 0; i < ready->n_ready; i++)
2303     {
2304       fprintf (sched_dump, "  %s:%d",
2305                (*current_sched_info->print_insn) (p[i], 0),
2306                INSN_LUID (p[i]));
2307       if (sched_pressure_p)
2308         fprintf (sched_dump, "(cost=%d",
2309                  INSN_REG_PRESSURE_EXCESS_COST_CHANGE (p[i]));
2310       if (INSN_TICK (p[i]) > clock_var)
2311         fprintf (sched_dump, ":delay=%d", INSN_TICK (p[i]) - clock_var);
2312       if (sched_pressure_p)
2313         fprintf (sched_dump, ")");
2314     }
2315   fprintf (sched_dump, "\n");
2316 }
2317
2318 /* Search INSN for REG_SAVE_NOTE note pairs for
2319    NOTE_INSN_EHREGION_{BEG,END}; and convert them back into
2320    NOTEs.  The REG_SAVE_NOTE note following first one is contains the
2321    saved value for NOTE_BLOCK_NUMBER which is useful for
2322    NOTE_INSN_EH_REGION_{BEG,END} NOTEs.  */
2323 void
2324 reemit_notes (rtx insn)
2325 {
2326   rtx note, last = insn;
2327
2328   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2329     {
2330       if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
2331         {
2332           enum insn_note note_type = (enum insn_note) INTVAL (XEXP (note, 0));
2333
2334           last = emit_note_before (note_type, last);
2335           remove_note (insn, note);
2336         }
2337     }
2338 }
2339
2340 /* Move INSN.  Reemit notes if needed.  Update CFG, if needed.  */
2341 static void
2342 move_insn (rtx insn, rtx last, rtx nt)
2343 {
2344   if (PREV_INSN (insn) != last)
2345     {
2346       basic_block bb;
2347       rtx note;
2348       int jump_p = 0;
2349
2350       bb = BLOCK_FOR_INSN (insn);
2351  
2352       /* BB_HEAD is either LABEL or NOTE.  */
2353       gcc_assert (BB_HEAD (bb) != insn);      
2354
2355       if (BB_END (bb) == insn)
2356         /* If this is last instruction in BB, move end marker one
2357            instruction up.  */
2358         {
2359           /* Jumps are always placed at the end of basic block.  */
2360           jump_p = control_flow_insn_p (insn);
2361
2362           gcc_assert (!jump_p
2363                       || ((common_sched_info->sched_pass_id == SCHED_RGN_PASS)
2364                           && IS_SPECULATION_BRANCHY_CHECK_P (insn))
2365                       || (common_sched_info->sched_pass_id
2366                           == SCHED_EBB_PASS));
2367           
2368           gcc_assert (BLOCK_FOR_INSN (PREV_INSN (insn)) == bb);
2369
2370           BB_END (bb) = PREV_INSN (insn);
2371         }
2372
2373       gcc_assert (BB_END (bb) != last);
2374
2375       if (jump_p)
2376         /* We move the block note along with jump.  */
2377         {
2378           gcc_assert (nt);
2379
2380           note = NEXT_INSN (insn);
2381           while (NOTE_NOT_BB_P (note) && note != nt)
2382             note = NEXT_INSN (note);
2383
2384           if (note != nt
2385               && (LABEL_P (note)
2386                   || BARRIER_P (note)))
2387             note = NEXT_INSN (note);
2388       
2389           gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
2390         }
2391       else
2392         note = insn;
2393
2394       NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (note);
2395       PREV_INSN (NEXT_INSN (note)) = PREV_INSN (insn);
2396
2397       NEXT_INSN (note) = NEXT_INSN (last);
2398       PREV_INSN (NEXT_INSN (last)) = note;
2399
2400       NEXT_INSN (last) = insn;
2401       PREV_INSN (insn) = last;
2402
2403       bb = BLOCK_FOR_INSN (last);
2404
2405       if (jump_p)
2406         {
2407           fix_jump_move (insn);
2408
2409           if (BLOCK_FOR_INSN (insn) != bb)
2410             move_block_after_check (insn);
2411
2412           gcc_assert (BB_END (bb) == last);
2413         }
2414
2415       df_insn_change_bb (insn, bb);
2416   
2417       /* Update BB_END, if needed.  */
2418       if (BB_END (bb) == last)
2419         BB_END (bb) = insn;  
2420     }
2421
2422   SCHED_GROUP_P (insn) = 0;  
2423 }
2424
2425 /* Return true if scheduling INSN will finish current clock cycle.  */
2426 static bool
2427 insn_finishes_cycle_p (rtx insn)
2428 {
2429   if (SCHED_GROUP_P (insn))
2430     /* After issuing INSN, rest of the sched_group will be forced to issue
2431        in order.  Don't make any plans for the rest of cycle.  */
2432     return true;
2433
2434   /* Finishing the block will, apparently, finish the cycle.  */
2435   if (current_sched_info->insn_finishes_block_p
2436       && current_sched_info->insn_finishes_block_p (insn))
2437     return true;
2438
2439   return false;
2440 }
2441
2442 /* The following structure describe an entry of the stack of choices.  */
2443 struct choice_entry
2444 {
2445   /* Ordinal number of the issued insn in the ready queue.  */
2446   int index;
2447   /* The number of the rest insns whose issues we should try.  */
2448   int rest;
2449   /* The number of issued essential insns.  */
2450   int n;
2451   /* State after issuing the insn.  */
2452   state_t state;
2453 };
2454
2455 /* The following array is used to implement a stack of choices used in
2456    function max_issue.  */
2457 static struct choice_entry *choice_stack;
2458
2459 /* The following variable value is number of essential insns issued on
2460    the current cycle.  An insn is essential one if it changes the
2461    processors state.  */
2462 int cycle_issued_insns;
2463
2464 /* This holds the value of the target dfa_lookahead hook.  */
2465 int dfa_lookahead;
2466
2467 /* The following variable value is maximal number of tries of issuing
2468    insns for the first cycle multipass insn scheduling.  We define
2469    this value as constant*(DFA_LOOKAHEAD**ISSUE_RATE).  We would not
2470    need this constraint if all real insns (with non-negative codes)
2471    had reservations because in this case the algorithm complexity is
2472    O(DFA_LOOKAHEAD**ISSUE_RATE).  Unfortunately, the dfa descriptions
2473    might be incomplete and such insn might occur.  For such
2474    descriptions, the complexity of algorithm (without the constraint)
2475    could achieve DFA_LOOKAHEAD ** N , where N is the queue length.  */
2476 static int max_lookahead_tries;
2477
2478 /* The following value is value of hook
2479    `first_cycle_multipass_dfa_lookahead' at the last call of
2480    `max_issue'.  */
2481 static int cached_first_cycle_multipass_dfa_lookahead = 0;
2482
2483 /* The following value is value of `issue_rate' at the last call of
2484    `sched_init'.  */
2485 static int cached_issue_rate = 0;
2486
2487 /* The following function returns maximal (or close to maximal) number
2488    of insns which can be issued on the same cycle and one of which
2489    insns is insns with the best rank (the first insn in READY).  To
2490    make this function tries different samples of ready insns.  READY
2491    is current queue `ready'.  Global array READY_TRY reflects what
2492    insns are already issued in this try.  MAX_POINTS is the sum of points
2493    of all instructions in READY.  The function stops immediately,
2494    if it reached the such a solution, that all instruction can be issued.
2495    INDEX will contain index of the best insn in READY.  The following
2496    function is used only for first cycle multipass scheduling.
2497
2498    PRIVILEGED_N >= 0
2499
2500    This function expects recognized insns only.  All USEs,
2501    CLOBBERs, etc must be filtered elsewhere.  */
2502 int
2503 max_issue (struct ready_list *ready, int privileged_n, state_t state,
2504            int *index)
2505 {
2506   int n, i, all, n_ready, best, delay, tries_num, points = -1, max_points;
2507   int more_issue;
2508   struct choice_entry *top;
2509   rtx insn;
2510
2511   n_ready = ready->n_ready;
2512   gcc_assert (dfa_lookahead >= 1 && privileged_n >= 0
2513               && privileged_n <= n_ready);
2514
2515   /* Init MAX_LOOKAHEAD_TRIES.  */
2516   if (cached_first_cycle_multipass_dfa_lookahead != dfa_lookahead)
2517     {
2518       cached_first_cycle_multipass_dfa_lookahead = dfa_lookahead;
2519       max_lookahead_tries = 100;
2520       for (i = 0; i < issue_rate; i++)
2521         max_lookahead_tries *= dfa_lookahead;
2522     }
2523
2524   /* Init max_points.  */
2525   max_points = 0;
2526   more_issue = issue_rate - cycle_issued_insns;
2527
2528   /* ??? We used to assert here that we never issue more insns than issue_rate.
2529      However, some targets (e.g. MIPS/SB1) claim lower issue rate than can be
2530      achieved to get better performance.  Until these targets are fixed to use
2531      scheduler hooks to manipulate insns priority instead, the assert should 
2532      be disabled.  
2533
2534      gcc_assert (more_issue >= 0);  */
2535
2536   for (i = 0; i < n_ready; i++)
2537     if (!ready_try [i])
2538       {
2539         if (more_issue-- > 0)
2540           max_points += ISSUE_POINTS (ready_element (ready, i));
2541         else
2542           break;
2543       }
2544
2545   /* The number of the issued insns in the best solution.  */
2546   best = 0;
2547
2548   top = choice_stack;
2549
2550   /* Set initial state of the search.  */
2551   memcpy (top->state, state, dfa_state_size);
2552   top->rest = dfa_lookahead;
2553   top->n = 0;
2554
2555   /* Count the number of the insns to search among.  */
2556   for (all = i = 0; i < n_ready; i++)
2557     if (!ready_try [i])
2558       all++;
2559
2560   /* I is the index of the insn to try next.  */
2561   i = 0;
2562   tries_num = 0;
2563   for (;;)
2564     {
2565       if (/* If we've reached a dead end or searched enough of what we have
2566              been asked...  */
2567           top->rest == 0
2568           /* Or have nothing else to try.  */
2569           || i >= n_ready)
2570         {
2571           /* ??? (... || i == n_ready).  */
2572           gcc_assert (i <= n_ready);
2573
2574           if (top == choice_stack)
2575             break;
2576
2577           if (best < top - choice_stack)
2578             {
2579               if (privileged_n)
2580                 {
2581                   n = privileged_n;
2582                   /* Try to find issued privileged insn.  */
2583                   while (n && !ready_try[--n]);
2584                 }
2585
2586               if (/* If all insns are equally good...  */
2587                   privileged_n == 0
2588                   /* Or a privileged insn will be issued.  */
2589                   || ready_try[n])
2590                 /* Then we have a solution.  */
2591                 {
2592                   best = top - choice_stack;
2593                   /* This is the index of the insn issued first in this
2594                      solution.  */
2595                   *index = choice_stack [1].index;
2596                   points = top->n;
2597                   if (top->n == max_points || best == all)
2598                     break;
2599                 }
2600             }
2601
2602           /* Set ready-list index to point to the last insn
2603              ('i++' below will advance it to the next insn).  */
2604           i = top->index;
2605
2606           /* Backtrack.  */
2607           ready_try [i] = 0;
2608           top--;
2609           memcpy (state, top->state, dfa_state_size);
2610         }
2611       else if (!ready_try [i])
2612         {
2613           tries_num++;
2614           if (tries_num > max_lookahead_tries)
2615             break;
2616           insn = ready_element (ready, i);
2617           delay = state_transition (state, insn);
2618           if (delay < 0)
2619             {
2620               if (state_dead_lock_p (state)
2621                   || insn_finishes_cycle_p (insn))
2622                 /* We won't issue any more instructions in the next
2623                    choice_state.  */
2624                 top->rest = 0;
2625               else
2626                 top->rest--;
2627
2628               n = top->n;
2629               if (memcmp (top->state, state, dfa_state_size) != 0)
2630                 n += ISSUE_POINTS (insn);
2631
2632               /* Advance to the next choice_entry.  */
2633               top++;
2634               /* Initialize it.  */
2635               top->rest = dfa_lookahead;
2636               top->index = i;
2637               top->n = n;
2638               memcpy (top->state, state, dfa_state_size);
2639
2640               ready_try [i] = 1;
2641               i = -1;
2642             }
2643         }
2644
2645       /* Increase ready-list index.  */
2646       i++;
2647     }
2648
2649   /* Restore the original state of the DFA.  */
2650   memcpy (state, choice_stack->state, dfa_state_size);  
2651
2652   return best;
2653 }
2654
2655 /* The following function chooses insn from READY and modifies
2656    READY.  The following function is used only for first
2657    cycle multipass scheduling.
2658    Return:
2659    -1 if cycle should be advanced,
2660    0 if INSN_PTR is set to point to the desirable insn,
2661    1 if choose_ready () should be restarted without advancing the cycle.  */
2662 static int
2663 choose_ready (struct ready_list *ready, rtx *insn_ptr)
2664 {
2665   int lookahead;
2666
2667   if (dbg_cnt (sched_insn) == false)
2668     {
2669       rtx insn;
2670
2671       insn = next_nonnote_insn (last_scheduled_insn);
2672
2673       if (QUEUE_INDEX (insn) == QUEUE_READY)
2674         /* INSN is in the ready_list.  */
2675         {
2676           ready_remove_insn (insn);
2677           *insn_ptr = insn;
2678           return 0;
2679         }
2680
2681       /* INSN is in the queue.  Advance cycle to move it to the ready list.  */
2682       return -1;
2683     }
2684
2685   lookahead = 0;
2686
2687   if (targetm.sched.first_cycle_multipass_dfa_lookahead)
2688     lookahead = targetm.sched.first_cycle_multipass_dfa_lookahead ();
2689   if (lookahead <= 0 || SCHED_GROUP_P (ready_element (ready, 0))
2690       || DEBUG_INSN_P (ready_element (ready, 0)))
2691     {
2692       *insn_ptr = ready_remove_first (ready);
2693       return 0;
2694     }
2695   else
2696     {
2697       /* Try to choose the better insn.  */
2698       int index = 0, i, n;
2699       rtx insn;
2700       int try_data = 1, try_control = 1;
2701       ds_t ts;
2702       
2703       insn = ready_element (ready, 0);
2704       if (INSN_CODE (insn) < 0)
2705         {
2706           *insn_ptr = ready_remove_first (ready);
2707           return 0;
2708         }
2709
2710       if (spec_info
2711           && spec_info->flags & (PREFER_NON_DATA_SPEC
2712                                  | PREFER_NON_CONTROL_SPEC))
2713         {
2714           for (i = 0, n = ready->n_ready; i < n; i++)
2715             {
2716               rtx x;
2717               ds_t s;
2718
2719               x = ready_element (ready, i);
2720               s = TODO_SPEC (x);
2721               
2722               if (spec_info->flags & PREFER_NON_DATA_SPEC
2723                   && !(s & DATA_SPEC))
2724                 {                 
2725                   try_data = 0;
2726                   if (!(spec_info->flags & PREFER_NON_CONTROL_SPEC)
2727                       || !try_control)
2728                     break;
2729                 }
2730               
2731               if (spec_info->flags & PREFER_NON_CONTROL_SPEC
2732                   && !(s & CONTROL_SPEC))
2733                 {
2734                   try_control = 0;
2735                   if (!(spec_info->flags & PREFER_NON_DATA_SPEC) || !try_data)
2736                     break;
2737                 }
2738             }
2739         }
2740
2741       ts = TODO_SPEC (insn);
2742       if ((ts & SPECULATIVE)
2743           && (((!try_data && (ts & DATA_SPEC))
2744                || (!try_control && (ts & CONTROL_SPEC)))
2745               || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard_spec
2746                   && !targetm.sched
2747                   .first_cycle_multipass_dfa_lookahead_guard_spec (insn))))
2748         /* Discard speculative instruction that stands first in the ready
2749            list.  */
2750         {
2751           change_queue_index (insn, 1);
2752           return 1;
2753         }
2754
2755       ready_try[0] = 0;
2756
2757       for (i = 1; i < ready->n_ready; i++)
2758         {
2759           insn = ready_element (ready, i);
2760
2761           ready_try [i]
2762             = ((!try_data && (TODO_SPEC (insn) & DATA_SPEC))
2763                || (!try_control && (TODO_SPEC (insn) & CONTROL_SPEC)));
2764         }
2765
2766       /* Let the target filter the search space.  */
2767       for (i = 1; i < ready->n_ready; i++)
2768         if (!ready_try[i])
2769           {
2770             insn = ready_element (ready, i);
2771
2772 #ifdef ENABLE_CHECKING
2773             /* If this insn is recognizable we should have already
2774                recognized it earlier.
2775                ??? Not very clear where this is supposed to be done.
2776                See dep_cost_1.  */
2777             gcc_assert (INSN_CODE (insn) >= 0
2778                         || recog_memoized (insn) < 0);
2779 #endif
2780
2781             ready_try [i]
2782               = (/* INSN_CODE check can be omitted here as it is also done later
2783                     in max_issue ().  */
2784                  INSN_CODE (insn) < 0
2785                  || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard
2786                      && !targetm.sched.first_cycle_multipass_dfa_lookahead_guard
2787                      (insn)));
2788           }
2789
2790       if (max_issue (ready, 1, curr_state, &index) == 0)
2791         {
2792           *insn_ptr = ready_remove_first (ready);
2793           if (sched_verbose >= 4)
2794             fprintf (sched_dump, ";;\t\tChosen insn (but can't issue) : %s \n", 
2795                      (*current_sched_info->print_insn) (*insn_ptr, 0));
2796           return 0;
2797         }
2798       else
2799         {
2800           if (sched_verbose >= 4)    
2801             fprintf (sched_dump, ";;\t\tChosen insn : %s\n",
2802                      (*current_sched_info->print_insn)
2803                      (ready_element (ready, index), 0));
2804           
2805           *insn_ptr = ready_remove (ready, index);
2806           return 0;
2807         }
2808     }
2809 }
2810
2811 /* Use forward list scheduling to rearrange insns of block pointed to by
2812    TARGET_BB, possibly bringing insns from subsequent blocks in the same
2813    region.  */
2814
2815 void
2816 schedule_block (basic_block *target_bb)
2817 {
2818   int i, first_cycle_insn_p;
2819   int can_issue_more;
2820   state_t temp_state = NULL;  /* It is used for multipass scheduling.  */
2821   int sort_p, advance, start_clock_var;
2822
2823   /* Head/tail info for this block.  */
2824   rtx prev_head = current_sched_info->prev_head;
2825   rtx next_tail = current_sched_info->next_tail;
2826   rtx head = NEXT_INSN (prev_head);
2827   rtx tail = PREV_INSN (next_tail);
2828
2829   /* We used to have code to avoid getting parameters moved from hard
2830      argument registers into pseudos.
2831
2832      However, it was removed when it proved to be of marginal benefit
2833      and caused problems because schedule_block and compute_forward_dependences
2834      had different notions of what the "head" insn was.  */
2835
2836   gcc_assert (head != tail || INSN_P (head));
2837
2838   haifa_recovery_bb_recently_added_p = false;
2839
2840   /* Debug info.  */
2841   if (sched_verbose)
2842     dump_new_block_header (0, *target_bb, head, tail);
2843
2844   state_reset (curr_state);
2845
2846   /* Clear the ready list.  */
2847   ready.first = ready.veclen - 1;
2848   ready.n_ready = 0;
2849   ready.n_debug = 0;
2850
2851   /* It is used for first cycle multipass scheduling.  */
2852   temp_state = alloca (dfa_state_size);
2853
2854   if (targetm.sched.md_init)
2855     targetm.sched.md_init (sched_dump, sched_verbose, ready.veclen);
2856
2857   /* We start inserting insns after PREV_HEAD.  */
2858   last_scheduled_insn = prev_head;
2859
2860   gcc_assert ((NOTE_P (last_scheduled_insn)
2861                || BOUNDARY_DEBUG_INSN_P (last_scheduled_insn))
2862               && BLOCK_FOR_INSN (last_scheduled_insn) == *target_bb);
2863
2864   /* Initialize INSN_QUEUE.  Q_SIZE is the total number of insns in the
2865      queue.  */
2866   q_ptr = 0;
2867   q_size = 0;
2868
2869   insn_queue = XALLOCAVEC (rtx, max_insn_queue_index + 1);
2870   memset (insn_queue, 0, (max_insn_queue_index + 1) * sizeof (rtx));
2871
2872   /* Start just before the beginning of time.  */
2873   clock_var = -1;
2874
2875   /* We need queue and ready lists and clock_var be initialized 
2876      in try_ready () (which is called through init_ready_list ()).  */
2877   (*current_sched_info->init_ready_list) ();
2878
2879   /* The algorithm is O(n^2) in the number of ready insns at any given
2880      time in the worst case.  Before reload we are more likely to have
2881      big lists so truncate them to a reasonable size.  */
2882   if (!reload_completed
2883       && ready.n_ready - ready.n_debug > MAX_SCHED_READY_INSNS)
2884     {
2885       ready_sort (&ready);
2886
2887       /* Find first free-standing insn past MAX_SCHED_READY_INSNS.
2888          If there are debug insns, we know they're first.  */
2889       for (i = MAX_SCHED_READY_INSNS + ready.n_debug; i < ready.n_ready; i++)
2890         if (!SCHED_GROUP_P (ready_element (&ready, i)))
2891           break;
2892
2893       if (sched_verbose >= 2)
2894         {
2895           fprintf (sched_dump,
2896                    ";;\t\tReady list on entry: %d insns\n", ready.n_ready);
2897           fprintf (sched_dump,
2898                    ";;\t\t before reload => truncated to %d insns\n", i);
2899         }
2900
2901       /* Delay all insns past it for 1 cycle.  If debug counter is
2902          activated make an exception for the insn right after
2903          last_scheduled_insn.  */
2904       {
2905         rtx skip_insn;
2906
2907         if (dbg_cnt (sched_insn) == false)
2908           skip_insn = next_nonnote_insn (last_scheduled_insn);
2909         else
2910           skip_insn = NULL_RTX;
2911
2912         while (i < ready.n_ready)
2913           {
2914             rtx insn;
2915
2916             insn = ready_remove (&ready, i);
2917
2918             if (insn != skip_insn)
2919               queue_insn (insn, 1);
2920           }
2921       }
2922     }
2923
2924   /* Now we can restore basic block notes and maintain precise cfg.  */
2925   restore_bb_notes (*target_bb);
2926
2927   last_clock_var = -1;
2928
2929   advance = 0;
2930
2931   sort_p = TRUE;
2932   /* Loop until all the insns in BB are scheduled.  */
2933   while ((*current_sched_info->schedule_more_p) ())
2934     {
2935       do
2936         {
2937           start_clock_var = clock_var;
2938
2939           clock_var++;
2940
2941           advance_one_cycle ();
2942
2943           /* Add to the ready list all pending insns that can be issued now.
2944              If there are no ready insns, increment clock until one
2945              is ready and add all pending insns at that point to the ready
2946              list.  */
2947           queue_to_ready (&ready);
2948
2949           gcc_assert (ready.n_ready);
2950
2951           if (sched_verbose >= 2)
2952             {
2953               fprintf (sched_dump, ";;\t\tReady list after queue_to_ready:  ");
2954               debug_ready_list (&ready);
2955             }
2956           advance -= clock_var - start_clock_var;
2957         }
2958       while (advance > 0);
2959
2960       if (sort_p)
2961         {
2962           /* Sort the ready list based on priority.  */
2963           ready_sort (&ready);
2964
2965           if (sched_verbose >= 2)
2966             {
2967               fprintf (sched_dump, ";;\t\tReady list after ready_sort:  ");
2968               debug_ready_list (&ready);
2969             }
2970         }
2971
2972       /* We don't want md sched reorder to even see debug isns, so put
2973          them out right away.  */
2974       if (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0)))
2975         {
2976           if (control_flow_insn_p (last_scheduled_insn))
2977             {
2978               *target_bb = current_sched_info->advance_target_bb
2979                 (*target_bb, 0);
2980
2981               if (sched_verbose)
2982                 {
2983                   rtx x;
2984
2985                   x = next_real_insn (last_scheduled_insn);
2986                   gcc_assert (x);
2987                   dump_new_block_header (1, *target_bb, x, tail);
2988                 }
2989
2990               last_scheduled_insn = bb_note (*target_bb);
2991             }
2992
2993           while (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0)))
2994             {
2995               rtx insn = ready_remove_first (&ready);
2996               gcc_assert (DEBUG_INSN_P (insn));
2997               (*current_sched_info->begin_schedule_ready) (insn,
2998                                                            last_scheduled_insn);
2999               move_insn (insn, last_scheduled_insn,
3000                          current_sched_info->next_tail);
3001               last_scheduled_insn = insn;
3002               advance = schedule_insn (insn);
3003               gcc_assert (advance == 0);
3004               if (ready.n_ready > 0)
3005                 ready_sort (&ready);
3006             }
3007
3008           if (!ready.n_ready)
3009             continue;
3010         }
3011
3012       /* Allow the target to reorder the list, typically for
3013          better instruction bundling.  */
3014       if (sort_p && targetm.sched.reorder
3015           && (ready.n_ready == 0
3016               || !SCHED_GROUP_P (ready_element (&ready, 0))))
3017         can_issue_more =
3018           targetm.sched.reorder (sched_dump, sched_verbose,
3019                                  ready_lastpos (&ready),
3020                                  &ready.n_ready, clock_var);
3021       else
3022         can_issue_more = issue_rate;
3023
3024       first_cycle_insn_p = 1;
3025       cycle_issued_insns = 0;
3026       for (;;)
3027         {
3028           rtx insn;
3029           int cost;
3030           bool asm_p = false;
3031
3032           if (sched_verbose >= 2)
3033             {
3034               fprintf (sched_dump, ";;\tReady list (t = %3d):  ",
3035                        clock_var);
3036               debug_ready_list (&ready);
3037               if (sched_pressure_p)
3038                 print_curr_reg_pressure ();
3039             }
3040
3041           if (ready.n_ready == 0 
3042               && can_issue_more 
3043               && reload_completed) 
3044             {
3045               /* Allow scheduling insns directly from the queue in case
3046                  there's nothing better to do (ready list is empty) but
3047                  there are still vacant dispatch slots in the current cycle.  */
3048               if (sched_verbose >= 6)
3049                 fprintf (sched_dump,";;\t\tSecond chance\n");
3050               memcpy (temp_state, curr_state, dfa_state_size);
3051               if (early_queue_to_ready (temp_state, &ready))
3052                 ready_sort (&ready);
3053             }
3054
3055           if (ready.n_ready == 0
3056               || !can_issue_more
3057               || state_dead_lock_p (curr_state)
3058               || !(*current_sched_info->schedule_more_p) ())
3059             break;
3060
3061           /* Select and remove the insn from the ready list.  */
3062           if (sort_p)
3063             {
3064               int res;
3065
3066               insn = NULL_RTX;
3067               res = choose_ready (&ready, &insn);
3068
3069               if (res < 0)
3070                 /* Finish cycle.  */
3071                 break;
3072               if (res > 0)
3073                 /* Restart choose_ready ().  */
3074                 continue;
3075
3076               gcc_assert (insn != NULL_RTX);
3077             }
3078           else
3079             insn = ready_remove_first (&ready);
3080
3081           if (sched_pressure_p && INSN_TICK (insn) > clock_var)
3082             {
3083               ready_add (&ready, insn, true);
3084               advance = 1;
3085               break;
3086             }
3087
3088           if (targetm.sched.dfa_new_cycle
3089               && targetm.sched.dfa_new_cycle (sched_dump, sched_verbose,
3090                                               insn, last_clock_var,
3091                                               clock_var, &sort_p))
3092             /* SORT_P is used by the target to override sorting
3093                of the ready list.  This is needed when the target
3094                has modified its internal structures expecting that
3095                the insn will be issued next.  As we need the insn
3096                to have the highest priority (so it will be returned by
3097                the ready_remove_first call above), we invoke
3098                ready_add (&ready, insn, true).
3099                But, still, there is one issue: INSN can be later 
3100                discarded by scheduler's front end through 
3101                current_sched_info->can_schedule_ready_p, hence, won't
3102                be issued next.  */ 
3103             {
3104               ready_add (&ready, insn, true);
3105               break;
3106             }
3107
3108           sort_p = TRUE;
3109           memcpy (temp_state, curr_state, dfa_state_size);
3110           if (recog_memoized (insn) < 0)
3111             {
3112               asm_p = (GET_CODE (PATTERN (insn)) == ASM_INPUT
3113                        || asm_noperands (PATTERN (insn)) >= 0);
3114               if (!first_cycle_insn_p && asm_p)
3115                 /* This is asm insn which is tried to be issued on the
3116                    cycle not first.  Issue it on the next cycle.  */
3117                 cost = 1;
3118               else
3119                 /* A USE insn, or something else we don't need to
3120                    understand.  We can't pass these directly to
3121                    state_transition because it will trigger a
3122                    fatal error for unrecognizable insns.  */
3123                 cost = 0;
3124             }
3125           else if (sched_pressure_p)
3126             cost = 0;
3127           else
3128             {
3129               cost = state_transition (temp_state, insn);
3130               if (cost < 0)
3131                 cost = 0;
3132               else if (cost == 0)
3133                 cost = 1;
3134             }
3135
3136           if (cost >= 1)
3137             {
3138               queue_insn (insn, cost);
3139               if (SCHED_GROUP_P (insn))
3140                 {
3141                   advance = cost;
3142                   break;
3143                 }
3144  
3145               continue;
3146             }
3147
3148           if (current_sched_info->can_schedule_ready_p
3149               && ! (*current_sched_info->can_schedule_ready_p) (insn))
3150             /* We normally get here only if we don't want to move
3151                insn from the split block.  */
3152             {
3153               TODO_SPEC (insn) = (TODO_SPEC (insn) & ~SPECULATIVE) | HARD_DEP;
3154               continue;
3155             }
3156
3157           /* DECISION is made.  */      
3158   
3159           if (TODO_SPEC (insn) & SPECULATIVE)
3160             generate_recovery_code (insn);
3161
3162           if (control_flow_insn_p (last_scheduled_insn)      
3163               /* This is used to switch basic blocks by request
3164                  from scheduler front-end (actually, sched-ebb.c only).
3165                  This is used to process blocks with single fallthru
3166                  edge.  If succeeding block has jump, it [jump] will try
3167                  move at the end of current bb, thus corrupting CFG.  */
3168               || current_sched_info->advance_target_bb (*target_bb, insn))
3169             {
3170               *target_bb = current_sched_info->advance_target_bb
3171                 (*target_bb, 0);
3172               
3173               if (sched_verbose)
3174                 {
3175                   rtx x;
3176
3177                   x = next_real_insn (last_scheduled_insn);
3178                   gcc_assert (x);
3179                   dump_new_block_header (1, *target_bb, x, tail);
3180                 }
3181
3182               last_scheduled_insn = bb_note (*target_bb);
3183             }
3184  
3185           /* Update counters, etc in the scheduler's front end.  */
3186           (*current_sched_info->begin_schedule_ready) (insn,
3187                                                        last_scheduled_insn);
3188  
3189           move_insn (insn, last_scheduled_insn, current_sched_info->next_tail);
3190           reemit_notes (insn);
3191           last_scheduled_insn = insn;
3192           
3193           if (memcmp (curr_state, temp_state, dfa_state_size) != 0)
3194             {
3195               cycle_issued_insns++;
3196               memcpy (curr_state, temp_state, dfa_state_size);
3197             }
3198
3199           if (targetm.sched.variable_issue)
3200             can_issue_more =
3201               targetm.sched.variable_issue (sched_dump, sched_verbose,
3202                                             insn, can_issue_more);
3203           /* A naked CLOBBER or USE generates no instruction, so do
3204              not count them against the issue rate.  */
3205           else if (GET_CODE (PATTERN (insn)) != USE
3206                    && GET_CODE (PATTERN (insn)) != CLOBBER)
3207             can_issue_more--;
3208           advance = schedule_insn (insn);
3209
3210           /* After issuing an asm insn we should start a new cycle.  */
3211           if (advance == 0 && asm_p)
3212             advance = 1;
3213           if (advance != 0)
3214             break;
3215
3216           first_cycle_insn_p = 0;
3217
3218           /* Sort the ready list based on priority.  This must be
3219              redone here, as schedule_insn may have readied additional
3220              insns that will not be sorted correctly.  */
3221           if (ready.n_ready > 0)
3222             ready_sort (&ready);
3223
3224           /* Quickly go through debug insns such that md sched
3225              reorder2 doesn't have to deal with debug insns.  */
3226           if (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0))
3227               && (*current_sched_info->schedule_more_p) ())
3228             {
3229               if (control_flow_insn_p (last_scheduled_insn))
3230                 {
3231                   *target_bb = current_sched_info->advance_target_bb
3232                     (*target_bb, 0);
3233
3234                   if (sched_verbose)
3235                     {
3236                       rtx x;
3237
3238                       x = next_real_insn (last_scheduled_insn);
3239                       gcc_assert (x);
3240                       dump_new_block_header (1, *target_bb, x, tail);
3241                     }
3242
3243                   last_scheduled_insn = bb_note (*target_bb);
3244                 }
3245
3246               while (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0)))
3247                 {
3248                   insn = ready_remove_first (&ready);
3249                   gcc_assert (DEBUG_INSN_P (insn));
3250                   (*current_sched_info->begin_schedule_ready)
3251                     (insn, last_scheduled_insn);
3252                   move_insn (insn, last_scheduled_insn,
3253                              current_sched_info->next_tail);
3254                   advance = schedule_insn (insn);
3255                   last_scheduled_insn = insn;
3256                   gcc_assert (advance == 0);
3257                   if (ready.n_ready > 0)
3258                     ready_sort (&ready);
3259                 }
3260             }
3261
3262           if (targetm.sched.reorder2
3263               && (ready.n_ready == 0
3264                   || !SCHED_GROUP_P (ready_element (&ready, 0))))
3265             {
3266               can_issue_more =
3267                 targetm.sched.reorder2 (sched_dump, sched_verbose,
3268                                         ready.n_ready
3269                                         ? ready_lastpos (&ready) : NULL,
3270                                         &ready.n_ready, clock_var);
3271             }
3272         }
3273     }
3274
3275   /* Debug info.  */
3276   if (sched_verbose)
3277     {
3278       fprintf (sched_dump, ";;\tReady list (final):  ");
3279       debug_ready_list (&ready);
3280     }
3281
3282   if (current_sched_info->queue_must_finish_empty)
3283     /* Sanity check -- queue must be empty now.  Meaningless if region has
3284        multiple bbs.  */
3285     gcc_assert (!q_size && !ready.n_ready && !ready.n_debug);
3286   else 
3287     {
3288       /* We must maintain QUEUE_INDEX between blocks in region.  */
3289       for (i = ready.n_ready - 1; i >= 0; i--)
3290         {
3291           rtx x;
3292           
3293           x = ready_element (&ready, i);
3294           QUEUE_INDEX (x) = QUEUE_NOWHERE;
3295           TODO_SPEC (x) = (TODO_SPEC (x) & ~SPECULATIVE) | HARD_DEP;
3296         }
3297
3298       if (q_size)   
3299         for (i = 0; i <= max_insn_queue_index; i++)
3300           {
3301             rtx link;
3302             for (link = insn_queue[i]; link; link = XEXP (link, 1))
3303               {
3304                 rtx x;
3305
3306                 x = XEXP (link, 0);
3307                 QUEUE_INDEX (x) = QUEUE_NOWHERE;
3308                 TODO_SPEC (x) = (TODO_SPEC (x) & ~SPECULATIVE) | HARD_DEP;
3309               }
3310             free_INSN_LIST_list (&insn_queue[i]);
3311           }
3312     }
3313
3314   if (sched_verbose)
3315     fprintf (sched_dump, ";;   total time = %d\n", clock_var);
3316
3317   if (!current_sched_info->queue_must_finish_empty
3318       || haifa_recovery_bb_recently_added_p)
3319     {
3320       /* INSN_TICK (minimum clock tick at which the insn becomes
3321          ready) may be not correct for the insn in the subsequent
3322          blocks of the region.  We should use a correct value of
3323          `clock_var' or modify INSN_TICK.  It is better to keep
3324          clock_var value equal to 0 at the start of a basic block.
3325          Therefore we modify INSN_TICK here.  */
3326       fix_inter_tick (NEXT_INSN (prev_head), last_scheduled_insn);
3327     }
3328
3329   if (targetm.sched.md_finish)
3330     {
3331       targetm.sched.md_finish (sched_dump, sched_verbose);
3332       /* Target might have added some instructions to the scheduled block
3333          in its md_finish () hook.  These new insns don't have any data
3334          initialized and to identify them we extend h_i_d so that they'll
3335          get zero luids.  */
3336       sched_init_luids (NULL, NULL, NULL, NULL);
3337     }
3338
3339   if (sched_verbose)
3340     fprintf (sched_dump, ";;   new head = %d\n;;   new tail = %d\n\n",
3341              INSN_UID (head), INSN_UID (tail));
3342
3343   /* Update head/tail boundaries.  */
3344   head = NEXT_INSN (prev_head);
3345   tail = last_scheduled_insn;
3346
3347   head = restore_other_notes (head, NULL);
3348
3349   current_sched_info->head = head;
3350   current_sched_info->tail = tail;
3351 }
3352 \f
3353 /* Set_priorities: compute priority of each insn in the block.  */
3354
3355 int
3356 set_priorities (rtx head, rtx tail)
3357 {
3358   rtx insn;
3359   int n_insn;
3360   int sched_max_insns_priority = 
3361         current_sched_info->sched_max_insns_priority;
3362   rtx prev_head;
3363
3364   if (head == tail && (! INSN_P (head) || BOUNDARY_DEBUG_INSN_P (head)))
3365     gcc_unreachable ();
3366
3367   n_insn = 0;
3368
3369   prev_head = PREV_INSN (head);
3370   for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
3371     {
3372       if (!INSN_P (insn))
3373         continue;
3374
3375       n_insn++;
3376       (void) priority (insn);
3377
3378       gcc_assert (INSN_PRIORITY_KNOWN (insn));
3379
3380       sched_max_insns_priority = MAX (sched_max_insns_priority,
3381                                       INSN_PRIORITY (insn));
3382     }
3383
3384   current_sched_info->sched_max_insns_priority = sched_max_insns_priority;
3385
3386   return n_insn;
3387 }
3388
3389 /* Set dump and sched_verbose for the desired debugging output.  If no
3390    dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
3391    For -fsched-verbose=N, N>=10, print everything to stderr.  */
3392 void
3393 setup_sched_dump (void)
3394 {
3395   sched_verbose = sched_verbose_param;
3396   if (sched_verbose_param == 0 && dump_file)
3397     sched_verbose = 1;
3398   sched_dump = ((sched_verbose_param >= 10 || !dump_file)
3399                 ? stderr : dump_file);
3400 }
3401
3402 /* Initialize some global state for the scheduler.  This function works 
3403    with the common data shared between all the schedulers.  It is called
3404    from the scheduler specific initialization routine.  */
3405
3406 void
3407 sched_init (void)
3408 {
3409   /* Disable speculative loads in their presence if cc0 defined.  */
3410 #ifdef HAVE_cc0
3411   flag_schedule_speculative_load = 0;
3412 #endif
3413
3414   sched_pressure_p = (flag_sched_pressure && ! reload_completed
3415                       && common_sched_info->sched_pass_id == SCHED_RGN_PASS);
3416   if (sched_pressure_p)
3417     ira_setup_eliminable_regset ();
3418
3419   /* Initialize SPEC_INFO.  */
3420   if (targetm.sched.set_sched_flags)
3421     {
3422       spec_info = &spec_info_var;
3423       targetm.sched.set_sched_flags (spec_info);
3424
3425       if (spec_info->mask != 0)
3426         {
3427           spec_info->data_weakness_cutoff =
3428             (PARAM_VALUE (PARAM_SCHED_SPEC_PROB_CUTOFF) * MAX_DEP_WEAK) / 100;
3429           spec_info->control_weakness_cutoff =
3430             (PARAM_VALUE (PARAM_SCHED_SPEC_PROB_CUTOFF)
3431              * REG_BR_PROB_BASE) / 100;
3432         }
3433       else
3434         /* So we won't read anything accidentally.  */
3435         spec_info = NULL;
3436
3437     }
3438   else
3439     /* So we won't read anything accidentally.  */
3440     spec_info = 0;
3441
3442   /* Initialize issue_rate.  */
3443   if (targetm.sched.issue_rate)
3444     issue_rate = targetm.sched.issue_rate ();
3445   else
3446     issue_rate = 1;
3447
3448   if (cached_issue_rate != issue_rate)
3449     {
3450       cached_issue_rate = issue_rate;
3451       /* To invalidate max_lookahead_tries:  */
3452       cached_first_cycle_multipass_dfa_lookahead = 0;
3453     }
3454
3455   if (targetm.sched.first_cycle_multipass_dfa_lookahead)
3456     dfa_lookahead = targetm.sched.first_cycle_multipass_dfa_lookahead ();
3457   else
3458     dfa_lookahead = 0;
3459
3460   if (targetm.sched.init_dfa_pre_cycle_insn)
3461     targetm.sched.init_dfa_pre_cycle_insn ();
3462
3463   if (targetm.sched.init_dfa_post_cycle_insn)
3464     targetm.sched.init_dfa_post_cycle_insn ();
3465
3466   dfa_start ();
3467   dfa_state_size = state_size ();
3468
3469   init_alias_analysis ();
3470
3471   df_set_flags (DF_LR_RUN_DCE);
3472   df_note_add_problem ();
3473
3474   /* More problems needed for interloop dep calculation in SMS.  */
3475   if (common_sched_info->sched_pass_id == SCHED_SMS_PASS)
3476     {
3477       df_rd_add_problem ();
3478       df_chain_add_problem (DF_DU_CHAIN + DF_UD_CHAIN);
3479     }
3480
3481   df_analyze ();
3482   
3483   /* Do not run DCE after reload, as this can kill nops inserted 
3484      by bundling.  */
3485   if (reload_completed)
3486     df_clear_flags (DF_LR_RUN_DCE);
3487
3488   regstat_compute_calls_crossed ();
3489
3490   if (targetm.sched.md_init_global)
3491     targetm.sched.md_init_global (sched_dump, sched_verbose,
3492                                   get_max_uid () + 1);
3493
3494   if (sched_pressure_p)
3495     {
3496       int i, max_regno = max_reg_num ();
3497
3498       ira_set_pseudo_classes (sched_verbose ? sched_dump : NULL);
3499       sched_regno_cover_class
3500         = (enum reg_class *) xmalloc (max_regno * sizeof (enum reg_class));
3501       for (i = 0; i < max_regno; i++)
3502         sched_regno_cover_class[i]
3503           = (i < FIRST_PSEUDO_REGISTER
3504              ? ira_class_translate[REGNO_REG_CLASS (i)]
3505              : reg_cover_class (i));
3506       curr_reg_live = BITMAP_ALLOC (NULL);
3507       saved_reg_live = BITMAP_ALLOC (NULL);
3508       region_ref_regs = BITMAP_ALLOC (NULL);
3509     }
3510   
3511   curr_state = xmalloc (dfa_state_size);
3512 }
3513
3514 static void haifa_init_only_bb (basic_block, basic_block);
3515
3516 /* Initialize data structures specific to the Haifa scheduler.  */
3517 void
3518 haifa_sched_init (void)
3519 {
3520   setup_sched_dump ();
3521   sched_init ();
3522
3523   if (spec_info != NULL)
3524     {
3525       sched_deps_info->use_deps_list = 1;
3526       sched_deps_info->generate_spec_deps = 1;
3527     }
3528
3529   /* Initialize luids, dependency caches, target and h_i_d for the
3530      whole function.  */
3531   {
3532     bb_vec_t bbs = VEC_alloc (basic_block, heap, n_basic_blocks);
3533     basic_block bb;
3534
3535     sched_init_bbs ();
3536
3537     FOR_EACH_BB (bb)
3538       VEC_quick_push (basic_block, bbs, bb);
3539     sched_init_luids (bbs, NULL, NULL, NULL);
3540     sched_deps_init (true);
3541     sched_extend_target ();
3542     haifa_init_h_i_d (bbs, NULL, NULL, NULL);
3543
3544     VEC_free (basic_block, heap, bbs);
3545   }
3546
3547   sched_init_only_bb = haifa_init_only_bb;
3548   sched_split_block = sched_split_block_1;
3549   sched_create_empty_bb = sched_create_empty_bb_1;
3550   haifa_recovery_bb_ever_added_p = false;
3551
3552 #ifdef ENABLE_CHECKING
3553   /* This is used preferably for finding bugs in check_cfg () itself.
3554      We must call sched_bbs_init () before check_cfg () because check_cfg ()
3555      assumes that the last insn in the last bb has a non-null successor.  */
3556   check_cfg (0, 0);
3557 #endif
3558
3559   nr_begin_data = nr_begin_control = nr_be_in_data = nr_be_in_control = 0;
3560   before_recovery = 0;
3561   after_recovery = 0;
3562 }
3563
3564 /* Finish work with the data specific to the Haifa scheduler.  */
3565 void
3566 haifa_sched_finish (void)
3567 {
3568   sched_create_empty_bb = NULL;
3569   sched_split_block = NULL;
3570   sched_init_only_bb = NULL;
3571
3572   if (spec_info && spec_info->dump)
3573     {
3574       char c = reload_completed ? 'a' : 'b';
3575
3576       fprintf (spec_info->dump,
3577                ";; %s:\n", current_function_name ());
3578
3579       fprintf (spec_info->dump,
3580                ";; Procedure %cr-begin-data-spec motions == %d\n",
3581                c, nr_begin_data);
3582       fprintf (spec_info->dump,
3583                ";; Procedure %cr-be-in-data-spec motions == %d\n",
3584                c, nr_be_in_data);
3585       fprintf (spec_info->dump,
3586                ";; Procedure %cr-begin-control-spec motions == %d\n",
3587                c, nr_begin_control);
3588       fprintf (spec_info->dump,
3589                ";; Procedure %cr-be-in-control-spec motions == %d\n",
3590                c, nr_be_in_control);
3591     }
3592
3593   /* Finalize h_i_d, dependency caches, and luids for the whole
3594      function.  Target will be finalized in md_global_finish ().  */
3595   sched_deps_finish ();
3596   sched_finish_luids ();
3597   current_sched_info = NULL;
3598   sched_finish ();
3599 }
3600
3601 /* Free global data used during insn scheduling.  This function works with 
3602    the common data shared between the schedulers.  */
3603
3604 void
3605 sched_finish (void)
3606 {
3607   haifa_finish_h_i_d ();
3608   if (sched_pressure_p)
3609     {
3610       free (sched_regno_cover_class);
3611       BITMAP_FREE (region_ref_regs);
3612       BITMAP_FREE (saved_reg_live);
3613       BITMAP_FREE (curr_reg_live);
3614     }
3615   free (curr_state);
3616
3617   if (targetm.sched.md_finish_global)
3618     targetm.sched.md_finish_global (sched_dump, sched_verbose);
3619
3620   end_alias_analysis ();
3621
3622   regstat_free_calls_crossed ();
3623
3624   dfa_finish ();
3625
3626 #ifdef ENABLE_CHECKING
3627   /* After reload ia64 backend clobbers CFG, so can't check anything.  */
3628   if (!reload_completed)
3629     check_cfg (0, 0);
3630 #endif
3631 }
3632
3633 /* Fix INSN_TICKs of the instructions in the current block as well as
3634    INSN_TICKs of their dependents.
3635    HEAD and TAIL are the begin and the end of the current scheduled block.  */
3636 static void
3637 fix_inter_tick (rtx head, rtx tail)
3638 {
3639   /* Set of instructions with corrected INSN_TICK.  */
3640   bitmap_head processed;
3641   /* ??? It is doubtful if we should assume that cycle advance happens on
3642      basic block boundaries.  Basically insns that are unconditionally ready
3643      on the start of the block are more preferable then those which have
3644      a one cycle dependency over insn from the previous block.  */
3645   int next_clock = clock_var + 1;
3646
3647   bitmap_initialize (&processed, 0);
3648   
3649   /* Iterates over scheduled instructions and fix their INSN_TICKs and
3650      INSN_TICKs of dependent instructions, so that INSN_TICKs are consistent
3651      across different blocks.  */
3652   for (tail = NEXT_INSN (tail); head != tail; head = NEXT_INSN (head))
3653     {
3654       if (INSN_P (head))
3655         {
3656           int tick;
3657           sd_iterator_def sd_it;
3658           dep_t dep;
3659                   
3660           tick = INSN_TICK (head);
3661           gcc_assert (tick >= MIN_TICK);
3662           
3663           /* Fix INSN_TICK of instruction from just scheduled block.  */
3664           if (!bitmap_bit_p (&processed, INSN_LUID (head)))
3665             {
3666               bitmap_set_bit (&processed, INSN_LUID (head));
3667               tick -= next_clock;
3668               
3669               if (tick < MIN_TICK)
3670                 tick = MIN_TICK;
3671               
3672               INSN_TICK (head) = tick;           
3673             }
3674           
3675           FOR_EACH_DEP (head, SD_LIST_RES_FORW, sd_it, dep)
3676             {
3677               rtx next;
3678               
3679               next = DEP_CON (dep);
3680               tick = INSN_TICK (next);
3681
3682               if (tick != INVALID_TICK
3683                   /* If NEXT has its INSN_TICK calculated, fix it.
3684                      If not - it will be properly calculated from
3685                      scratch later in fix_tick_ready.  */
3686                   && !bitmap_bit_p (&processed, INSN_LUID (next)))
3687                 {
3688                   bitmap_set_bit (&processed, INSN_LUID (next));
3689                   tick -= next_clock;
3690                   
3691                   if (tick < MIN_TICK)
3692                     tick = MIN_TICK;
3693                   
3694                   if (tick > INTER_TICK (next))
3695                     INTER_TICK (next) = tick;
3696                   else
3697                     tick = INTER_TICK (next);
3698
3699                   INSN_TICK (next) = tick;
3700                 }
3701             }
3702         }
3703     }
3704   bitmap_clear (&processed);
3705 }
3706
3707 static int haifa_speculate_insn (rtx, ds_t, rtx *);
3708   
3709 /* Check if NEXT is ready to be added to the ready or queue list.
3710    If "yes", add it to the proper list.
3711    Returns:
3712       -1 - is not ready yet,
3713        0 - added to the ready list,
3714    0 < N - queued for N cycles.  */
3715 int
3716 try_ready (rtx next)
3717 {  
3718   ds_t old_ts, *ts;
3719
3720   ts = &TODO_SPEC (next);
3721   old_ts = *ts;
3722
3723   gcc_assert (!(old_ts & ~(SPECULATIVE | HARD_DEP))
3724               && ((old_ts & HARD_DEP)
3725                   || (old_ts & SPECULATIVE)));
3726   
3727   if (sd_lists_empty_p (next, SD_LIST_BACK))
3728     /* NEXT has all its dependencies resolved.  */
3729     {
3730       /* Remove HARD_DEP bit from NEXT's status.  */
3731       *ts &= ~HARD_DEP;
3732
3733       if (current_sched_info->flags & DO_SPECULATION)
3734         /* Remove all speculative bits from NEXT's status.  */
3735         *ts &= ~SPECULATIVE;
3736     }
3737   else
3738     {
3739       /* One of the NEXT's dependencies has been resolved.
3740          Recalculate NEXT's status.  */
3741
3742       *ts &= ~SPECULATIVE & ~HARD_DEP;
3743
3744       if (sd_lists_empty_p (next, SD_LIST_HARD_BACK))
3745         /* Now we've got NEXT with speculative deps only.
3746            1. Look at the deps to see what we have to do.
3747            2. Check if we can do 'todo'.  */
3748         {
3749           sd_iterator_def sd_it;
3750           dep_t dep;
3751           bool first_p = true;
3752
3753           FOR_EACH_DEP (next, SD_LIST_BACK, sd_it, dep)
3754             {
3755               ds_t ds = DEP_STATUS (dep) & SPECULATIVE;
3756
3757               if (first_p)
3758                 {
3759                   first_p = false;
3760
3761                   *ts = ds;
3762                 }
3763               else
3764                 *ts = ds_merge (*ts, ds);
3765             }
3766
3767           if (ds_weak (*ts) < spec_info->data_weakness_cutoff)
3768             /* Too few points.  */
3769             *ts = (*ts & ~SPECULATIVE) | HARD_DEP;
3770         }
3771       else
3772         *ts |= HARD_DEP;
3773     }
3774
3775   if (*ts & HARD_DEP)
3776     gcc_assert (*ts == old_ts
3777                 && QUEUE_INDEX (next) == QUEUE_NOWHERE);
3778   else if (current_sched_info->new_ready)
3779     *ts = current_sched_info->new_ready (next, *ts);
3780
3781   /* * if !(old_ts & SPECULATIVE) (e.g. HARD_DEP or 0), then insn might
3782      have its original pattern or changed (speculative) one.  This is due
3783      to changing ebb in region scheduling.
3784      * But if (old_ts & SPECULATIVE), then we are pretty sure that insn
3785      has speculative pattern.
3786
3787      We can't assert (!(*ts & HARD_DEP) || *ts == old_ts) here because
3788      control-speculative NEXT could have been discarded by sched-rgn.c
3789      (the same case as when discarded by can_schedule_ready_p ()).  */
3790
3791   if ((*ts & SPECULATIVE)
3792       /* If (old_ts == *ts), then (old_ts & SPECULATIVE) and we don't
3793          need to change anything.  */
3794       && *ts != old_ts)
3795     {
3796       int res;
3797       rtx new_pat;
3798       
3799       gcc_assert ((*ts & SPECULATIVE) && !(*ts & ~SPECULATIVE));
3800       
3801       res = haifa_speculate_insn (next, *ts, &new_pat);
3802         
3803       switch (res)
3804         {
3805         case -1:
3806           /* It would be nice to change DEP_STATUS of all dependences,
3807              which have ((DEP_STATUS & SPECULATIVE) == *ts) to HARD_DEP,
3808              so we won't reanalyze anything.  */
3809           *ts = (*ts & ~SPECULATIVE) | HARD_DEP;
3810           break;
3811           
3812         case 0:
3813           /* We follow the rule, that every speculative insn
3814              has non-null ORIG_PAT.  */
3815           if (!ORIG_PAT (next))
3816             ORIG_PAT (next) = PATTERN (next);
3817           break;
3818           
3819         case 1:                  
3820           if (!ORIG_PAT (next))
3821             /* If we gonna to overwrite the original pattern of insn,
3822                save it.  */
3823             ORIG_PAT (next) = PATTERN (next);
3824           
3825           haifa_change_pattern (next, new_pat);
3826           break;
3827           
3828         default:
3829           gcc_unreachable ();
3830         }
3831     }
3832   
3833   /* We need to restore pattern only if (*ts == 0), because otherwise it is
3834      either correct (*ts & SPECULATIVE),
3835      or we simply don't care (*ts & HARD_DEP).  */
3836   
3837   gcc_assert (!ORIG_PAT (next)
3838               || !IS_SPECULATION_BRANCHY_CHECK_P (next));
3839   
3840   if (*ts & HARD_DEP)
3841     {
3842       /* We can't assert (QUEUE_INDEX (next) == QUEUE_NOWHERE) here because
3843          control-speculative NEXT could have been discarded by sched-rgn.c
3844          (the same case as when discarded by can_schedule_ready_p ()).  */
3845       /*gcc_assert (QUEUE_INDEX (next) == QUEUE_NOWHERE);*/
3846       
3847       change_queue_index (next, QUEUE_NOWHERE);
3848       return -1;
3849     }
3850   else if (!(*ts & BEGIN_SPEC) && ORIG_PAT (next) && !IS_SPECULATION_CHECK_P (next))
3851     /* We should change pattern of every previously speculative 
3852        instruction - and we determine if NEXT was speculative by using
3853        ORIG_PAT field.  Except one case - speculation checks have ORIG_PAT
3854        pat too, so skip them.  */
3855     {
3856       haifa_change_pattern (next, ORIG_PAT (next));
3857       ORIG_PAT (next) = 0;
3858     }
3859
3860   if (sched_verbose >= 2)
3861     {         
3862       int s = TODO_SPEC (next);
3863           
3864       fprintf (sched_dump, ";;\t\tdependencies resolved: insn %s",
3865                (*current_sched_info->print_insn) (next, 0));
3866           
3867       if (spec_info && spec_info->dump)
3868         {
3869           if (s & BEGIN_DATA)
3870             fprintf (spec_info->dump, "; data-spec;");
3871           if (s & BEGIN_CONTROL)
3872             fprintf (spec_info->dump, "; control-spec;");
3873           if (s & BE_IN_CONTROL)
3874             fprintf (spec_info->dump, "; in-control-spec;");
3875         }
3876
3877       fprintf (sched_dump, "\n");
3878     }          
3879   
3880   adjust_priority (next);
3881         
3882   return fix_tick_ready (next);
3883 }
3884
3885 /* Calculate INSN_TICK of NEXT and add it to either ready or queue list.  */
3886 static int
3887 fix_tick_ready (rtx next)
3888 {
3889   int tick, delay;
3890
3891   if (!sd_lists_empty_p (next, SD_LIST_RES_BACK))
3892     {
3893       int full_p;
3894       sd_iterator_def sd_it;
3895       dep_t dep;
3896
3897       tick = INSN_TICK (next);
3898       /* if tick is not equal to INVALID_TICK, then update
3899          INSN_TICK of NEXT with the most recent resolved dependence
3900          cost.  Otherwise, recalculate from scratch.  */
3901       full_p = (tick == INVALID_TICK);
3902
3903       FOR_EACH_DEP (next, SD_LIST_RES_BACK, sd_it, dep)
3904         {       
3905           rtx pro = DEP_PRO (dep);
3906           int tick1;
3907               
3908           gcc_assert (INSN_TICK (pro) >= MIN_TICK);
3909
3910           tick1 = INSN_TICK (pro) + dep_cost (dep);
3911           if (tick1 > tick)
3912             tick = tick1;
3913
3914           if (!full_p)
3915             break;
3916         }
3917     }
3918   else
3919     tick = -1;
3920
3921   INSN_TICK (next) = tick;
3922
3923   delay = tick - clock_var;
3924   if (delay <= 0 || sched_pressure_p)
3925     delay = QUEUE_READY;
3926
3927   change_queue_index (next, delay);
3928
3929   return delay;
3930 }
3931
3932 /* Move NEXT to the proper queue list with (DELAY >= 1),
3933    or add it to the ready list (DELAY == QUEUE_READY),
3934    or remove it from ready and queue lists at all (DELAY == QUEUE_NOWHERE).  */
3935 static void
3936 change_queue_index (rtx next, int delay)
3937 {
3938   int i = QUEUE_INDEX (next);
3939
3940   gcc_assert (QUEUE_NOWHERE <= delay && delay <= max_insn_queue_index 
3941               && delay != 0);
3942   gcc_assert (i != QUEUE_SCHEDULED);
3943   
3944   if ((delay > 0 && NEXT_Q_AFTER (q_ptr, delay) == i)
3945       || (delay < 0 && delay == i))
3946     /* We have nothing to do.  */
3947     return;
3948
3949   /* Remove NEXT from wherever it is now.  */
3950   if (i == QUEUE_READY)
3951     ready_remove_insn (next);
3952   else if (i >= 0)
3953     queue_remove (next);
3954     
3955   /* Add it to the proper place.  */
3956   if (delay == QUEUE_READY)
3957     ready_add (readyp, next, false);
3958   else if (delay >= 1)
3959     queue_insn (next, delay);
3960     
3961   if (sched_verbose >= 2)
3962     {         
3963       fprintf (sched_dump, ";;\t\ttick updated: insn %s",
3964                (*current_sched_info->print_insn) (next, 0));
3965       
3966       if (delay == QUEUE_READY)
3967         fprintf (sched_dump, " into ready\n");
3968       else if (delay >= 1)
3969         fprintf (sched_dump, " into queue with cost=%d\n", delay);
3970       else
3971         fprintf (sched_dump, " removed from ready or queue lists\n");
3972     }
3973 }
3974
3975 static int sched_ready_n_insns = -1;
3976
3977 /* Initialize per region data structures.  */
3978 void
3979 sched_extend_ready_list (int new_sched_ready_n_insns)
3980 {
3981   int i;
3982
3983   if (sched_ready_n_insns == -1)
3984     /* At the first call we need to initialize one more choice_stack
3985        entry.  */
3986     {
3987       i = 0;
3988       sched_ready_n_insns = 0;
3989     }
3990   else
3991     i = sched_ready_n_insns + 1;
3992
3993   ready.veclen = new_sched_ready_n_insns + issue_rate;
3994   ready.vec = XRESIZEVEC (rtx, ready.vec, ready.veclen);
3995
3996   gcc_assert (new_sched_ready_n_insns >= sched_ready_n_insns);
3997
3998   ready_try = (char *) xrecalloc (ready_try, new_sched_ready_n_insns,
3999                                   sched_ready_n_insns, sizeof (*ready_try));
4000
4001   /* We allocate +1 element to save initial state in the choice_stack[0]
4002      entry.  */
4003   choice_stack = XRESIZEVEC (struct choice_entry, choice_stack,
4004                              new_sched_ready_n_insns + 1);
4005
4006   for (; i <= new_sched_ready_n_insns; i++)
4007     choice_stack[i].state = xmalloc (dfa_state_size);
4008
4009   sched_ready_n_insns = new_sched_ready_n_insns;
4010 }
4011
4012 /* Free per region data structures.  */
4013 void
4014 sched_finish_ready_list (void)
4015 {
4016   int i;
4017
4018   free (ready.vec);
4019   ready.vec = NULL;
4020   ready.veclen = 0;
4021
4022   free (ready_try);
4023   ready_try = NULL;
4024
4025   for (i = 0; i <= sched_ready_n_insns; i++)
4026     free (choice_stack [i].state);
4027   free (choice_stack);
4028   choice_stack = NULL;
4029
4030   sched_ready_n_insns = -1;
4031 }
4032
4033 static int
4034 haifa_luid_for_non_insn (rtx x)
4035 {
4036   gcc_assert (NOTE_P (x) || LABEL_P (x));
4037
4038   return 0;
4039 }
4040
4041 /* Generates recovery code for INSN.  */
4042 static void
4043 generate_recovery_code (rtx insn)
4044 {
4045   if (TODO_SPEC (insn) & BEGIN_SPEC)
4046     begin_speculative_block (insn);
4047   
4048   /* Here we have insn with no dependencies to
4049      instructions other then CHECK_SPEC ones.  */
4050   
4051   if (TODO_SPEC (insn) & BE_IN_SPEC)
4052     add_to_speculative_block (insn);
4053 }
4054
4055 /* Helper function.
4056    Tries to add speculative dependencies of type FS between instructions
4057    in deps_list L and TWIN.  */
4058 static void
4059 process_insn_forw_deps_be_in_spec (rtx insn, rtx twin, ds_t fs)
4060 {
4061   sd_iterator_def sd_it;
4062   dep_t dep;
4063
4064   FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
4065     {
4066       ds_t ds;
4067       rtx consumer;
4068
4069       consumer = DEP_CON (dep);
4070
4071       ds = DEP_STATUS (dep);
4072
4073       if (/* If we want to create speculative dep.  */
4074           fs
4075           /* And we can do that because this is a true dep.  */
4076           && (ds & DEP_TYPES) == DEP_TRUE)
4077         {
4078           gcc_assert (!(ds & BE_IN_SPEC));
4079
4080           if (/* If this dep can be overcome with 'begin speculation'.  */
4081               ds & BEGIN_SPEC)
4082             /* Then we have a choice: keep the dep 'begin speculative'
4083                or transform it into 'be in speculative'.  */
4084             {
4085               if (/* In try_ready we assert that if insn once became ready
4086                      it can be removed from the ready (or queue) list only
4087                      due to backend decision.  Hence we can't let the
4088                      probability of the speculative dep to decrease.  */
4089                   ds_weak (ds) <= ds_weak (fs))
4090                 {
4091                   ds_t new_ds;
4092
4093                   new_ds = (ds & ~BEGIN_SPEC) | fs;
4094                   
4095                   if (/* consumer can 'be in speculative'.  */
4096                       sched_insn_is_legitimate_for_speculation_p (consumer,
4097                                                                   new_ds))
4098                     /* Transform it to be in speculative.  */
4099                     ds = new_ds;
4100                 }
4101             }
4102           else
4103             /* Mark the dep as 'be in speculative'.  */
4104             ds |= fs;
4105         }
4106
4107       {
4108         dep_def _new_dep, *new_dep = &_new_dep;
4109
4110         init_dep_1 (new_dep, twin, consumer, DEP_TYPE (dep), ds);
4111         sd_add_dep (new_dep, false);
4112       }
4113     }
4114 }
4115
4116 /* Generates recovery code for BEGIN speculative INSN.  */
4117 static void
4118 begin_speculative_block (rtx insn)
4119 {
4120   if (TODO_SPEC (insn) & BEGIN_DATA)
4121     nr_begin_data++;      
4122   if (TODO_SPEC (insn) & BEGIN_CONTROL)
4123     nr_begin_control++;
4124
4125   create_check_block_twin (insn, false);
4126
4127   TODO_SPEC (insn) &= ~BEGIN_SPEC;
4128 }
4129
4130 static void haifa_init_insn (rtx);
4131
4132 /* Generates recovery code for BE_IN speculative INSN.  */
4133 static void
4134 add_to_speculative_block (rtx insn)
4135 {
4136   ds_t ts;
4137   sd_iterator_def sd_it;
4138   dep_t dep;
4139   rtx twins = NULL;
4140   rtx_vec_t priorities_roots;
4141
4142   ts = TODO_SPEC (insn);
4143   gcc_assert (!(ts & ~BE_IN_SPEC));
4144
4145   if (ts & BE_IN_DATA)
4146     nr_be_in_data++;
4147   if (ts & BE_IN_CONTROL)
4148     nr_be_in_control++;
4149
4150   TODO_SPEC (insn) &= ~BE_IN_SPEC;
4151   gcc_assert (!TODO_SPEC (insn));
4152   
4153   DONE_SPEC (insn) |= ts;
4154
4155   /* First we convert all simple checks to branchy.  */
4156   for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4157        sd_iterator_cond (&sd_it, &dep);)
4158     {
4159       rtx check = DEP_PRO (dep);
4160
4161       if (IS_SPECULATION_SIMPLE_CHECK_P (check))
4162         {
4163           create_check_block_twin (check, true);
4164
4165           /* Restart search.  */
4166           sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4167         }
4168       else
4169         /* Continue search.  */
4170         sd_iterator_next (&sd_it);
4171     }
4172
4173   priorities_roots = NULL;
4174   clear_priorities (insn, &priorities_roots);
4175
4176   while (1)
4177     {
4178       rtx check, twin;
4179       basic_block rec;
4180
4181       /* Get the first backward dependency of INSN.  */
4182       sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4183       if (!sd_iterator_cond (&sd_it, &dep))
4184         /* INSN has no backward dependencies left.  */
4185         break;
4186
4187       gcc_assert ((DEP_STATUS (dep) & BEGIN_SPEC) == 0
4188                   && (DEP_STATUS (dep) & BE_IN_SPEC) != 0
4189                   && (DEP_STATUS (dep) & DEP_TYPES) == DEP_TRUE);
4190
4191       check = DEP_PRO (dep);
4192
4193       gcc_assert (!IS_SPECULATION_CHECK_P (check) && !ORIG_PAT (check)
4194                   && QUEUE_INDEX (check) == QUEUE_NOWHERE);
4195
4196       rec = BLOCK_FOR_INSN (check);
4197
4198       twin = emit_insn_before (copy_insn (PATTERN (insn)), BB_END (rec));
4199       haifa_init_insn (twin);
4200
4201       sd_copy_back_deps (twin, insn, true);
4202
4203       if (sched_verbose && spec_info->dump)
4204         /* INSN_BB (insn) isn't determined for twin insns yet.
4205            So we can't use current_sched_info->print_insn.  */
4206         fprintf (spec_info->dump, ";;\t\tGenerated twin insn : %d/rec%d\n",
4207                  INSN_UID (twin), rec->index);
4208
4209       twins = alloc_INSN_LIST (twin, twins);
4210
4211       /* Add dependences between TWIN and all appropriate
4212          instructions from REC.  */
4213       FOR_EACH_DEP (insn, SD_LIST_SPEC_BACK, sd_it, dep)
4214         {
4215           rtx pro = DEP_PRO (dep);
4216
4217           gcc_assert (DEP_TYPE (dep) == REG_DEP_TRUE);
4218
4219           /* INSN might have dependencies from the instructions from
4220              several recovery blocks.  At this iteration we process those
4221              producers that reside in REC.  */
4222           if (BLOCK_FOR_INSN (pro) == rec)
4223             {
4224               dep_def _new_dep, *new_dep = &_new_dep;
4225
4226               init_dep (new_dep, pro, twin, REG_DEP_TRUE);
4227               sd_add_dep (new_dep, false);
4228             }
4229         }
4230
4231       process_insn_forw_deps_be_in_spec (insn, twin, ts);
4232
4233       /* Remove all dependencies between INSN and insns in REC.  */
4234       for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4235            sd_iterator_cond (&sd_it, &dep);)
4236         {
4237           rtx pro = DEP_PRO (dep);
4238
4239           if (BLOCK_FOR_INSN (pro) == rec)
4240             sd_delete_dep (sd_it);
4241           else
4242             sd_iterator_next (&sd_it);
4243         }
4244     }
4245
4246   /* We couldn't have added the dependencies between INSN and TWINS earlier
4247      because that would make TWINS appear in the INSN_BACK_DEPS (INSN).  */
4248   while (twins)
4249     {
4250       rtx twin;
4251
4252       twin = XEXP (twins, 0);
4253
4254       {
4255         dep_def _new_dep, *new_dep = &_new_dep;
4256
4257         init_dep (new_dep, insn, twin, REG_DEP_OUTPUT);
4258         sd_add_dep (new_dep, false);
4259       }
4260
4261       twin = XEXP (twins, 1);
4262       free_INSN_LIST_node (twins);
4263       twins = twin;      
4264     }
4265
4266   calc_priorities (priorities_roots);
4267   VEC_free (rtx, heap, priorities_roots);
4268 }
4269
4270 /* Extends and fills with zeros (only the new part) array pointed to by P.  */
4271 void *
4272 xrecalloc (void *p, size_t new_nmemb, size_t old_nmemb, size_t size)
4273 {
4274   gcc_assert (new_nmemb >= old_nmemb);
4275   p = XRESIZEVAR (void, p, new_nmemb * size);
4276   memset (((char *) p) + old_nmemb * size, 0, (new_nmemb - old_nmemb) * size);
4277   return p;
4278 }
4279
4280 /* Helper function.
4281    Find fallthru edge from PRED.  */
4282 edge
4283 find_fallthru_edge (basic_block pred)
4284 {
4285   edge e;
4286   edge_iterator ei;
4287   basic_block succ;
4288
4289   succ = pred->next_bb;
4290   gcc_assert (succ->prev_bb == pred);
4291
4292   if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
4293     {
4294       FOR_EACH_EDGE (e, ei, pred->succs)
4295         if (e->flags & EDGE_FALLTHRU)
4296           {
4297             gcc_assert (e->dest == succ);
4298             return e;
4299           }
4300     }
4301   else
4302     {
4303       FOR_EACH_EDGE (e, ei, succ->preds)
4304         if (e->flags & EDGE_FALLTHRU)
4305           {
4306             gcc_assert (e->src == pred);
4307             return e;
4308           }
4309     }
4310
4311   return NULL;
4312 }
4313
4314 /* Extend per basic block data structures.  */
4315 static void
4316 sched_extend_bb (void)
4317 {
4318   rtx insn;
4319
4320   /* The following is done to keep current_sched_info->next_tail non null.  */
4321   insn = BB_END (EXIT_BLOCK_PTR->prev_bb);
4322   if (NEXT_INSN (insn) == 0
4323       || (!NOTE_P (insn)
4324           && !LABEL_P (insn)
4325           /* Don't emit a NOTE if it would end up before a BARRIER.  */
4326           && !BARRIER_P (NEXT_INSN (insn))))
4327     {
4328       rtx note = emit_note_after (NOTE_INSN_DELETED, insn);
4329       /* Make insn appear outside BB.  */
4330       set_block_for_insn (note, NULL);
4331       BB_END (EXIT_BLOCK_PTR->prev_bb) = insn;
4332     }
4333 }
4334
4335 /* Init per basic block data structures.  */
4336 void
4337 sched_init_bbs (void)
4338 {
4339   sched_extend_bb ();
4340 }
4341
4342 /* Initialize BEFORE_RECOVERY variable.  */
4343 static void
4344 init_before_recovery (basic_block *before_recovery_ptr)
4345 {
4346   basic_block last;
4347   edge e;
4348
4349   last = EXIT_BLOCK_PTR->prev_bb;
4350   e = find_fallthru_edge (last);
4351
4352   if (e)
4353     {
4354       /* We create two basic blocks: 
4355          1. Single instruction block is inserted right after E->SRC
4356          and has jump to 
4357          2. Empty block right before EXIT_BLOCK.
4358          Between these two blocks recovery blocks will be emitted.  */
4359
4360       basic_block single, empty;
4361       rtx x, label;
4362
4363       /* If the fallthrough edge to exit we've found is from the block we've 
4364          created before, don't do anything more.  */
4365       if (last == after_recovery)
4366         return;
4367
4368       adding_bb_to_current_region_p = false;
4369
4370       single = sched_create_empty_bb (last);
4371       empty = sched_create_empty_bb (single);
4372
4373       /* Add new blocks to the root loop.  */
4374       if (current_loops != NULL)
4375         {
4376           add_bb_to_loop (single, VEC_index (loop_p, current_loops->larray, 0));
4377           add_bb_to_loop (empty, VEC_index (loop_p, current_loops->larray, 0));
4378         }
4379
4380       single->count = last->count;
4381       empty->count = last->count;
4382       single->frequency = last->frequency;
4383       empty->frequency = last->frequency;
4384       BB_COPY_PARTITION (single, last);
4385       BB_COPY_PARTITION (empty, last);
4386
4387       redirect_edge_succ (e, single);
4388       make_single_succ_edge (single, empty, 0);
4389       make_single_succ_edge (empty, EXIT_BLOCK_PTR,
4390                              EDGE_FALLTHRU | EDGE_CAN_FALLTHRU);
4391
4392       label = block_label (empty);
4393       x = emit_jump_insn_after (gen_jump (label), BB_END (single));
4394       JUMP_LABEL (x) = label;
4395       LABEL_NUSES (label)++;
4396       haifa_init_insn (x);
4397           
4398       emit_barrier_after (x);
4399
4400       sched_init_only_bb (empty, NULL);
4401       sched_init_only_bb (single, NULL);
4402       sched_extend_bb ();
4403
4404       adding_bb_to_current_region_p = true;
4405       before_recovery = single;
4406       after_recovery = empty;
4407
4408       if (before_recovery_ptr)
4409         *before_recovery_ptr = before_recovery;
4410
4411       if (sched_verbose >= 2 && spec_info->dump)
4412         fprintf (spec_info->dump,
4413                  ";;\t\tFixed fallthru to EXIT : %d->>%d->%d->>EXIT\n", 
4414                  last->index, single->index, empty->index);      
4415     }
4416   else
4417     before_recovery = last;
4418 }
4419
4420 /* Returns new recovery block.  */
4421 basic_block
4422 sched_create_recovery_block (basic_block *before_recovery_ptr)
4423 {
4424   rtx label;
4425   rtx barrier;
4426   basic_block rec;
4427   
4428   haifa_recovery_bb_recently_added_p = true;
4429   haifa_recovery_bb_ever_added_p = true;
4430
4431   init_before_recovery (before_recovery_ptr);
4432
4433   barrier = get_last_bb_insn (before_recovery);
4434   gcc_assert (BARRIER_P (barrier));
4435
4436   label = emit_label_after (gen_label_rtx (), barrier);
4437
4438   rec = create_basic_block (label, label, before_recovery);
4439
4440   /* A recovery block always ends with an unconditional jump.  */
4441   emit_barrier_after (BB_END (rec));
4442
4443   if (BB_PARTITION (before_recovery) != BB_UNPARTITIONED)
4444     BB_SET_PARTITION (rec, BB_COLD_PARTITION);
4445   
4446   if (sched_verbose && spec_info->dump)    
4447     fprintf (spec_info->dump, ";;\t\tGenerated recovery block rec%d\n",
4448              rec->index);
4449
4450   return rec;
4451 }
4452
4453 /* Create edges: FIRST_BB -> REC; FIRST_BB -> SECOND_BB; REC -> SECOND_BB
4454    and emit necessary jumps.  */
4455 void
4456 sched_create_recovery_edges (basic_block first_bb, basic_block rec,
4457                              basic_block second_bb)
4458 {
4459   rtx label;
4460   rtx jump;
4461   edge e;
4462   int edge_flags;
4463
4464   /* This is fixing of incoming edge.  */
4465   /* ??? Which other flags should be specified?  */      
4466   if (BB_PARTITION (first_bb) != BB_PARTITION (rec))
4467     /* Partition type is the same, if it is "unpartitioned".  */
4468     edge_flags = EDGE_CROSSING;
4469   else
4470     edge_flags = 0;
4471       
4472   e = make_edge (first_bb, rec, edge_flags);
4473   label = block_label (second_bb);
4474   jump = emit_jump_insn_after (gen_jump (label), BB_END (rec));
4475   JUMP_LABEL (jump) = label;
4476   LABEL_NUSES (label)++;
4477
4478   if (BB_PARTITION (second_bb) != BB_PARTITION (rec))
4479     /* Partition type is the same, if it is "unpartitioned".  */
4480     {
4481       /* Rewritten from cfgrtl.c.  */
4482       if (flag_reorder_blocks_and_partition
4483           && targetm.have_named_sections)
4484         {
4485           /* We don't need the same note for the check because
4486              any_condjump_p (check) == true.  */
4487           add_reg_note (jump, REG_CROSSING_JUMP, NULL_RTX);
4488         }
4489       edge_flags = EDGE_CROSSING;
4490     }
4491   else
4492     edge_flags = 0;  
4493
4494   make_single_succ_edge (rec, second_bb, edge_flags);  
4495 }
4496
4497 /* This function creates recovery code for INSN.  If MUTATE_P is nonzero,
4498    INSN is a simple check, that should be converted to branchy one.  */
4499 static void
4500 create_check_block_twin (rtx insn, bool mutate_p)
4501 {
4502   basic_block rec;
4503   rtx label, check, twin;
4504   ds_t fs;
4505   sd_iterator_def sd_it;
4506   dep_t dep;
4507   dep_def _new_dep, *new_dep = &_new_dep;
4508   ds_t todo_spec;
4509
4510   gcc_assert (ORIG_PAT (insn) != NULL_RTX);
4511
4512   if (!mutate_p)
4513     todo_spec = TODO_SPEC (insn);
4514   else
4515     {
4516       gcc_assert (IS_SPECULATION_SIMPLE_CHECK_P (insn)
4517                   && (TODO_SPEC (insn) & SPECULATIVE) == 0);
4518
4519       todo_spec = CHECK_SPEC (insn);
4520     }
4521
4522   todo_spec &= SPECULATIVE;
4523
4524   /* Create recovery block.  */
4525   if (mutate_p || targetm.sched.needs_block_p (todo_spec))
4526     {
4527       rec = sched_create_recovery_block (NULL);
4528       label = BB_HEAD (rec);
4529     }
4530   else
4531     {
4532       rec = EXIT_BLOCK_PTR;
4533       label = NULL_RTX;
4534     }
4535
4536   /* Emit CHECK.  */
4537   check = targetm.sched.gen_spec_check (insn, label, todo_spec);
4538
4539   if (rec != EXIT_BLOCK_PTR)
4540     {
4541       /* To have mem_reg alive at the beginning of second_bb,
4542          we emit check BEFORE insn, so insn after splitting 
4543          insn will be at the beginning of second_bb, which will
4544          provide us with the correct life information.  */
4545       check = emit_jump_insn_before (check, insn);
4546       JUMP_LABEL (check) = label;
4547       LABEL_NUSES (label)++;
4548     }
4549   else
4550     check = emit_insn_before (check, insn);
4551
4552   /* Extend data structures.  */
4553   haifa_init_insn (check);
4554
4555   /* CHECK is being added to current region.  Extend ready list.  */
4556   gcc_assert (sched_ready_n_insns != -1);
4557   sched_extend_ready_list (sched_ready_n_insns + 1);
4558
4559   if (current_sched_info->add_remove_insn)
4560     current_sched_info->add_remove_insn (insn, 0);
4561
4562   RECOVERY_BLOCK (check) = rec;
4563
4564   if (sched_verbose && spec_info->dump)
4565     fprintf (spec_info->dump, ";;\t\tGenerated check insn : %s\n",
4566              (*current_sched_info->print_insn) (check, 0));
4567
4568   gcc_assert (ORIG_PAT (insn));
4569
4570   /* Initialize TWIN (twin is a duplicate of original instruction
4571      in the recovery block).  */
4572   if (rec != EXIT_BLOCK_PTR)
4573     {
4574       sd_iterator_def sd_it;
4575       dep_t dep;
4576
4577       FOR_EACH_DEP (insn, SD_LIST_RES_BACK, sd_it, dep)
4578         if ((DEP_STATUS (dep) & DEP_OUTPUT) != 0)
4579           {
4580             struct _dep _dep2, *dep2 = &_dep2;
4581
4582             init_dep (dep2, DEP_PRO (dep), check, REG_DEP_TRUE);
4583
4584             sd_add_dep (dep2, true);
4585           }
4586
4587       twin = emit_insn_after (ORIG_PAT (insn), BB_END (rec));
4588       haifa_init_insn (twin);
4589
4590       if (sched_verbose && spec_info->dump)
4591         /* INSN_BB (insn) isn't determined for twin insns yet.
4592            So we can't use current_sched_info->print_insn.  */
4593         fprintf (spec_info->dump, ";;\t\tGenerated twin insn : %d/rec%d\n",
4594                  INSN_UID (twin), rec->index);
4595     }
4596   else
4597     {
4598       ORIG_PAT (check) = ORIG_PAT (insn);
4599       HAS_INTERNAL_DEP (check) = 1;
4600       twin = check;
4601       /* ??? We probably should change all OUTPUT dependencies to
4602          (TRUE | OUTPUT).  */
4603     }
4604
4605   /* Copy all resolved back dependencies of INSN to TWIN.  This will
4606      provide correct value for INSN_TICK (TWIN).  */
4607   sd_copy_back_deps (twin, insn, true);
4608
4609   if (rec != EXIT_BLOCK_PTR)
4610     /* In case of branchy check, fix CFG.  */
4611     {
4612       basic_block first_bb, second_bb;
4613       rtx jump;
4614
4615       first_bb = BLOCK_FOR_INSN (check);
4616       second_bb = sched_split_block (first_bb, check);
4617
4618       sched_create_recovery_edges (first_bb, rec, second_bb);
4619
4620       sched_init_only_bb (second_bb, first_bb);      
4621       sched_init_only_bb (rec, EXIT_BLOCK_PTR);
4622
4623       jump = BB_END (rec);
4624       haifa_init_insn (jump);
4625     }
4626
4627   /* Move backward dependences from INSN to CHECK and 
4628      move forward dependences from INSN to TWIN.  */
4629
4630   /* First, create dependencies between INSN's producers and CHECK & TWIN.  */
4631   FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
4632     {
4633       rtx pro = DEP_PRO (dep);
4634       ds_t ds;
4635
4636       /* If BEGIN_DATA: [insn ~~TRUE~~> producer]:
4637          check --TRUE--> producer  ??? or ANTI ???
4638          twin  --TRUE--> producer
4639          twin  --ANTI--> check
4640          
4641          If BEGIN_CONTROL: [insn ~~ANTI~~> producer]:
4642          check --ANTI--> producer
4643          twin  --ANTI--> producer
4644          twin  --ANTI--> check
4645
4646          If BE_IN_SPEC: [insn ~~TRUE~~> producer]:
4647          check ~~TRUE~~> producer
4648          twin  ~~TRUE~~> producer
4649          twin  --ANTI--> check  */                
4650
4651       ds = DEP_STATUS (dep);
4652
4653       if (ds & BEGIN_SPEC)
4654         {
4655           gcc_assert (!mutate_p);
4656           ds &= ~BEGIN_SPEC;
4657         }
4658
4659       init_dep_1 (new_dep, pro, check, DEP_TYPE (dep), ds);
4660       sd_add_dep (new_dep, false);
4661
4662       if (rec != EXIT_BLOCK_PTR)
4663         {
4664           DEP_CON (new_dep) = twin;
4665           sd_add_dep (new_dep, false);
4666         }    
4667     }
4668
4669   /* Second, remove backward dependencies of INSN.  */
4670   for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4671        sd_iterator_cond (&sd_it, &dep);)
4672     {
4673       if ((DEP_STATUS (dep) & BEGIN_SPEC)
4674           || mutate_p)
4675         /* We can delete this dep because we overcome it with
4676            BEGIN_SPECULATION.  */
4677         sd_delete_dep (sd_it);
4678       else
4679         sd_iterator_next (&sd_it);
4680     }
4681
4682   /* Future Speculations.  Determine what BE_IN speculations will be like.  */
4683   fs = 0;
4684
4685   /* Fields (DONE_SPEC (x) & BEGIN_SPEC) and CHECK_SPEC (x) are set only
4686      here.  */
4687   
4688   gcc_assert (!DONE_SPEC (insn));
4689   
4690   if (!mutate_p)
4691     { 
4692       ds_t ts = TODO_SPEC (insn);
4693
4694       DONE_SPEC (insn) = ts & BEGIN_SPEC;
4695       CHECK_SPEC (check) = ts & BEGIN_SPEC;
4696
4697       /* Luckiness of future speculations solely depends upon initial
4698          BEGIN speculation.  */
4699       if (ts & BEGIN_DATA)
4700         fs = set_dep_weak (fs, BE_IN_DATA, get_dep_weak (ts, BEGIN_DATA));
4701       if (ts & BEGIN_CONTROL)
4702         fs = set_dep_weak (fs, BE_IN_CONTROL,
4703                            get_dep_weak (ts, BEGIN_CONTROL));
4704     }
4705   else
4706     CHECK_SPEC (check) = CHECK_SPEC (insn);
4707
4708   /* Future speculations: call the helper.  */
4709   process_insn_forw_deps_be_in_spec (insn, twin, fs);
4710
4711   if (rec != EXIT_BLOCK_PTR)
4712     {
4713       /* Which types of dependencies should we use here is,
4714          generally, machine-dependent question...  But, for now,
4715          it is not.  */
4716
4717       if (!mutate_p)
4718         {
4719           init_dep (new_dep, insn, check, REG_DEP_TRUE);
4720           sd_add_dep (new_dep, false);
4721
4722           init_dep (new_dep, insn, twin, REG_DEP_OUTPUT);
4723           sd_add_dep (new_dep, false);
4724         }
4725       else
4726         {
4727           if (spec_info->dump)    
4728             fprintf (spec_info->dump, ";;\t\tRemoved simple check : %s\n",
4729                      (*current_sched_info->print_insn) (insn, 0));
4730
4731           /* Remove all dependencies of the INSN.  */
4732           {
4733             sd_it = sd_iterator_start (insn, (SD_LIST_FORW
4734                                               | SD_LIST_BACK
4735                                               | SD_LIST_RES_BACK));
4736             while (sd_iterator_cond (&sd_it, &dep))
4737               sd_delete_dep (sd_it);
4738           }
4739
4740           /* If former check (INSN) already was moved to the ready (or queue)
4741              list, add new check (CHECK) there too.  */
4742           if (QUEUE_INDEX (insn) != QUEUE_NOWHERE)
4743             try_ready (check);
4744
4745           /* Remove old check from instruction stream and free its
4746              data.  */
4747           sched_remove_insn (insn);
4748         }
4749
4750       init_dep (new_dep, check, twin, REG_DEP_ANTI);
4751       sd_add_dep (new_dep, false);
4752     }
4753   else
4754     {
4755       init_dep_1 (new_dep, insn, check, REG_DEP_TRUE, DEP_TRUE | DEP_OUTPUT);
4756       sd_add_dep (new_dep, false);
4757     }
4758
4759   if (!mutate_p)
4760     /* Fix priorities.  If MUTATE_P is nonzero, this is not necessary,
4761        because it'll be done later in add_to_speculative_block.  */
4762     {
4763       rtx_vec_t priorities_roots = NULL;
4764
4765       clear_priorities (twin, &priorities_roots);
4766       calc_priorities (priorities_roots);
4767       VEC_free (rtx, heap, priorities_roots);
4768     }
4769 }
4770
4771 /* Removes dependency between instructions in the recovery block REC
4772    and usual region instructions.  It keeps inner dependences so it
4773    won't be necessary to recompute them.  */
4774 static void
4775 fix_recovery_deps (basic_block rec)
4776 {
4777   rtx note, insn, jump, ready_list = 0;
4778   bitmap_head in_ready;
4779   rtx link;
4780
4781   bitmap_initialize (&in_ready, 0);
4782   
4783   /* NOTE - a basic block note.  */
4784   note = NEXT_INSN (BB_HEAD (rec));
4785   gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4786   insn = BB_END (rec);
4787   gcc_assert (JUMP_P (insn));
4788   insn = PREV_INSN (insn);
4789
4790   do
4791     {
4792       sd_iterator_def sd_it;
4793       dep_t dep;
4794
4795       for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
4796            sd_iterator_cond (&sd_it, &dep);)
4797         {
4798           rtx consumer = DEP_CON (dep);
4799
4800           if (BLOCK_FOR_INSN (consumer) != rec)
4801             {
4802               sd_delete_dep (sd_it);
4803
4804               if (!bitmap_bit_p (&in_ready, INSN_LUID (consumer)))
4805                 {
4806                   ready_list = alloc_INSN_LIST (consumer, ready_list);
4807                   bitmap_set_bit (&in_ready, INSN_LUID (consumer));
4808                 }
4809             }
4810           else
4811             {
4812               gcc_assert ((DEP_STATUS (dep) & DEP_TYPES) == DEP_TRUE);
4813
4814               sd_iterator_next (&sd_it);
4815             }
4816         }
4817       
4818       insn = PREV_INSN (insn);
4819     }
4820   while (insn != note);
4821
4822   bitmap_clear (&in_ready);
4823
4824   /* Try to add instructions to the ready or queue list.  */
4825   for (link = ready_list; link; link = XEXP (link, 1))
4826     try_ready (XEXP (link, 0));
4827   free_INSN_LIST_list (&ready_list);
4828
4829   /* Fixing jump's dependences.  */
4830   insn = BB_HEAD (rec);
4831   jump = BB_END (rec);
4832       
4833   gcc_assert (LABEL_P (insn));
4834   insn = NEXT_INSN (insn);
4835   
4836   gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
4837   add_jump_dependencies (insn, jump);
4838 }
4839
4840 /* Change pattern of INSN to NEW_PAT.  */
4841 void
4842 sched_change_pattern (rtx insn, rtx new_pat)
4843 {
4844   int t;
4845
4846   t = validate_change (insn, &PATTERN (insn), new_pat, 0);
4847   gcc_assert (t);
4848   dfa_clear_single_insn_cache (insn);
4849 }
4850
4851 /* Change pattern of INSN to NEW_PAT.  Invalidate cached haifa
4852    instruction data.  */
4853 static void
4854 haifa_change_pattern (rtx insn, rtx new_pat)
4855 {
4856   sched_change_pattern (insn, new_pat);
4857
4858   /* Invalidate INSN_COST, so it'll be recalculated.  */
4859   INSN_COST (insn) = -1;
4860   /* Invalidate INSN_TICK, so it'll be recalculated.  */
4861   INSN_TICK (insn) = INVALID_TICK;
4862 }
4863
4864 /* -1 - can't speculate,
4865    0 - for speculation with REQUEST mode it is OK to use
4866    current instruction pattern,
4867    1 - need to change pattern for *NEW_PAT to be speculative.  */
4868 int
4869 sched_speculate_insn (rtx insn, ds_t request, rtx *new_pat)
4870 {
4871   gcc_assert (current_sched_info->flags & DO_SPECULATION
4872               && (request & SPECULATIVE)
4873               && sched_insn_is_legitimate_for_speculation_p (insn, request));
4874
4875   if ((request & spec_info->mask) != request)
4876     return -1;
4877
4878   if (request & BE_IN_SPEC
4879       && !(request & BEGIN_SPEC))
4880     return 0;
4881
4882   return targetm.sched.speculate_insn (insn, request, new_pat);
4883 }
4884
4885 static int
4886 haifa_speculate_insn (rtx insn, ds_t request, rtx *new_pat)
4887 {
4888   gcc_assert (sched_deps_info->generate_spec_deps
4889               && !IS_SPECULATION_CHECK_P (insn));
4890
4891   if (HAS_INTERNAL_DEP (insn)
4892       || SCHED_GROUP_P (insn))
4893     return -1;
4894
4895   return sched_speculate_insn (insn, request, new_pat);
4896 }
4897
4898 /* Print some information about block BB, which starts with HEAD and
4899    ends with TAIL, before scheduling it.
4900    I is zero, if scheduler is about to start with the fresh ebb.  */
4901 static void
4902 dump_new_block_header (int i, basic_block bb, rtx head, rtx tail)
4903 {
4904   if (!i)
4905     fprintf (sched_dump,
4906              ";;   ======================================================\n");
4907   else
4908     fprintf (sched_dump,
4909              ";;   =====================ADVANCING TO=====================\n");
4910   fprintf (sched_dump,
4911            ";;   -- basic block %d from %d to %d -- %s reload\n",
4912            bb->index, INSN_UID (head), INSN_UID (tail),
4913            (reload_completed ? "after" : "before"));
4914   fprintf (sched_dump,
4915            ";;   ======================================================\n");
4916   fprintf (sched_dump, "\n");
4917 }
4918
4919 /* Unlink basic block notes and labels and saves them, so they
4920    can be easily restored.  We unlink basic block notes in EBB to
4921    provide back-compatibility with the previous code, as target backends
4922    assume, that there'll be only instructions between
4923    current_sched_info->{head and tail}.  We restore these notes as soon
4924    as we can.
4925    FIRST (LAST) is the first (last) basic block in the ebb.
4926    NB: In usual case (FIRST == LAST) nothing is really done.  */
4927 void
4928 unlink_bb_notes (basic_block first, basic_block last)
4929 {
4930   /* We DON'T unlink basic block notes of the first block in the ebb.  */
4931   if (first == last)
4932     return;
4933
4934   bb_header = XNEWVEC (rtx, last_basic_block);
4935
4936   /* Make a sentinel.  */
4937   if (last->next_bb != EXIT_BLOCK_PTR)
4938     bb_header[last->next_bb->index] = 0;
4939
4940   first = first->next_bb;
4941   do
4942     {
4943       rtx prev, label, note, next;
4944
4945       label = BB_HEAD (last);
4946       if (LABEL_P (label))
4947         note = NEXT_INSN (label);
4948       else
4949         note = label;      
4950       gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4951
4952       prev = PREV_INSN (label);
4953       next = NEXT_INSN (note);
4954       gcc_assert (prev && next);
4955
4956       NEXT_INSN (prev) = next;
4957       PREV_INSN (next) = prev;
4958
4959       bb_header[last->index] = label;
4960
4961       if (last == first)
4962         break;
4963       
4964       last = last->prev_bb;
4965     }
4966   while (1);
4967 }
4968
4969 /* Restore basic block notes.
4970    FIRST is the first basic block in the ebb.  */
4971 static void
4972 restore_bb_notes (basic_block first)
4973 {
4974   if (!bb_header)
4975     return;
4976
4977   /* We DON'T unlink basic block notes of the first block in the ebb.  */
4978   first = first->next_bb;  
4979   /* Remember: FIRST is actually a second basic block in the ebb.  */
4980
4981   while (first != EXIT_BLOCK_PTR
4982          && bb_header[first->index])
4983     {
4984       rtx prev, label, note, next;
4985       
4986       label = bb_header[first->index];
4987       prev = PREV_INSN (label);
4988       next = NEXT_INSN (prev);
4989
4990       if (LABEL_P (label))
4991         note = NEXT_INSN (label);
4992       else
4993         note = label;      
4994       gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4995
4996       bb_header[first->index] = 0;
4997
4998       NEXT_INSN (prev) = label;
4999       NEXT_INSN (note) = next;
5000       PREV_INSN (next) = note;
5001       
5002       first = first->next_bb;
5003     }
5004
5005   free (bb_header);
5006   bb_header = 0;
5007 }
5008
5009 /* Helper function.
5010    Fix CFG after both in- and inter-block movement of
5011    control_flow_insn_p JUMP.  */
5012 static void
5013 fix_jump_move (rtx jump)
5014 {
5015   basic_block bb, jump_bb, jump_bb_next;
5016
5017   bb = BLOCK_FOR_INSN (PREV_INSN (jump));
5018   jump_bb = BLOCK_FOR_INSN (jump);
5019   jump_bb_next = jump_bb->next_bb;
5020
5021   gcc_assert (common_sched_info->sched_pass_id == SCHED_EBB_PASS
5022               || IS_SPECULATION_BRANCHY_CHECK_P (jump));
5023   
5024   if (!NOTE_INSN_BASIC_BLOCK_P (BB_END (jump_bb_next)))
5025     /* if jump_bb_next is not empty.  */
5026     BB_END (jump_bb) = BB_END (jump_bb_next);
5027
5028   if (BB_END (bb) != PREV_INSN (jump))
5029     /* Then there are instruction after jump that should be placed
5030        to jump_bb_next.  */
5031     BB_END (jump_bb_next) = BB_END (bb);
5032   else
5033     /* Otherwise jump_bb_next is empty.  */
5034     BB_END (jump_bb_next) = NEXT_INSN (BB_HEAD (jump_bb_next));
5035
5036   /* To make assertion in move_insn happy.  */
5037   BB_END (bb) = PREV_INSN (jump);
5038
5039   update_bb_for_insn (jump_bb_next);
5040 }
5041
5042 /* Fix CFG after interblock movement of control_flow_insn_p JUMP.  */
5043 static void
5044 move_block_after_check (rtx jump)
5045 {
5046   basic_block bb, jump_bb, jump_bb_next;
5047   VEC(edge,gc) *t;
5048
5049   bb = BLOCK_FOR_INSN (PREV_INSN (jump));
5050   jump_bb = BLOCK_FOR_INSN (jump);
5051   jump_bb_next = jump_bb->next_bb;
5052   
5053   update_bb_for_insn (jump_bb);
5054   
5055   gcc_assert (IS_SPECULATION_CHECK_P (jump)
5056               || IS_SPECULATION_CHECK_P (BB_END (jump_bb_next)));
5057
5058   unlink_block (jump_bb_next);
5059   link_block (jump_bb_next, bb);
5060
5061   t = bb->succs;
5062   bb->succs = 0;
5063   move_succs (&(jump_bb->succs), bb);
5064   move_succs (&(jump_bb_next->succs), jump_bb);
5065   move_succs (&t, jump_bb_next);
5066
5067   df_mark_solutions_dirty ();
5068   
5069   common_sched_info->fix_recovery_cfg
5070     (bb->index, jump_bb->index, jump_bb_next->index);
5071 }
5072
5073 /* Helper function for move_block_after_check.
5074    This functions attaches edge vector pointed to by SUCCSP to
5075    block TO.  */
5076 static void
5077 move_succs (VEC(edge,gc) **succsp, basic_block to)
5078 {
5079   edge e;
5080   edge_iterator ei;
5081
5082   gcc_assert (to->succs == 0);
5083
5084   to->succs = *succsp;
5085
5086   FOR_EACH_EDGE (e, ei, to->succs)
5087     e->src = to;
5088
5089   *succsp = 0;
5090 }
5091
5092 /* Remove INSN from the instruction stream.
5093    INSN should have any dependencies.  */
5094 static void
5095 sched_remove_insn (rtx insn)
5096 {
5097   sd_finish_insn (insn);
5098
5099   change_queue_index (insn, QUEUE_NOWHERE);
5100   current_sched_info->add_remove_insn (insn, 1);
5101   remove_insn (insn);
5102 }
5103
5104 /* Clear priorities of all instructions, that are forward dependent on INSN.
5105    Store in vector pointed to by ROOTS_PTR insns on which priority () should
5106    be invoked to initialize all cleared priorities.  */
5107 static void
5108 clear_priorities (rtx insn, rtx_vec_t *roots_ptr)
5109 {
5110   sd_iterator_def sd_it;
5111   dep_t dep;
5112   bool insn_is_root_p = true;
5113
5114   gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
5115
5116   FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
5117     {
5118       rtx pro = DEP_PRO (dep);
5119
5120       if (INSN_PRIORITY_STATUS (pro) >= 0
5121           && QUEUE_INDEX (insn) != QUEUE_SCHEDULED)
5122         {
5123           /* If DEP doesn't contribute to priority then INSN itself should
5124              be added to priority roots.  */
5125           if (contributes_to_priority_p (dep))
5126             insn_is_root_p = false;
5127
5128           INSN_PRIORITY_STATUS (pro) = -1;
5129           clear_priorities (pro, roots_ptr);
5130         }
5131     }
5132
5133   if (insn_is_root_p)
5134     VEC_safe_push (rtx, heap, *roots_ptr, insn);
5135 }
5136
5137 /* Recompute priorities of instructions, whose priorities might have been
5138    changed.  ROOTS is a vector of instructions whose priority computation will
5139    trigger initialization of all cleared priorities.  */
5140 static void
5141 calc_priorities (rtx_vec_t roots)
5142 {
5143   int i;
5144   rtx insn;
5145
5146   for (i = 0; VEC_iterate (rtx, roots, i, insn); i++)
5147     priority (insn);
5148 }
5149
5150
5151 /* Add dependences between JUMP and other instructions in the recovery
5152    block.  INSN is the first insn the recovery block.  */
5153 static void
5154 add_jump_dependencies (rtx insn, rtx jump)
5155 {
5156   do
5157     {
5158       insn = NEXT_INSN (insn);
5159       if (insn == jump)
5160         break;
5161       
5162       if (dep_list_size (insn) == 0)
5163         {
5164           dep_def _new_dep, *new_dep = &_new_dep;
5165
5166           init_dep (new_dep, insn, jump, REG_DEP_ANTI);
5167           sd_add_dep (new_dep, false);
5168         }
5169     }
5170   while (1);
5171
5172   gcc_assert (!sd_lists_empty_p (jump, SD_LIST_BACK));
5173 }
5174
5175 /* Return the NOTE_INSN_BASIC_BLOCK of BB.  */
5176 rtx
5177 bb_note (basic_block bb)
5178 {
5179   rtx note;
5180
5181   note = BB_HEAD (bb);
5182   if (LABEL_P (note))
5183     note = NEXT_INSN (note);
5184
5185   gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
5186   return note;
5187 }
5188
5189 #ifdef ENABLE_CHECKING
5190 /* Helper function for check_cfg.
5191    Return nonzero, if edge vector pointed to by EL has edge with TYPE in
5192    its flags.  */
5193 static int
5194 has_edge_p (VEC(edge,gc) *el, int type)
5195 {
5196   edge e;
5197   edge_iterator ei;
5198
5199   FOR_EACH_EDGE (e, ei, el)
5200     if (e->flags & type)
5201       return 1;
5202   return 0;
5203 }
5204
5205 /* Search back, starting at INSN, for an insn that is not a
5206    NOTE_INSN_VAR_LOCATION.  Don't search beyond HEAD, and return it if
5207    no such insn can be found.  */
5208 static inline rtx
5209 prev_non_location_insn (rtx insn, rtx head)
5210 {
5211   while (insn != head && NOTE_P (insn)
5212          && NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION)
5213     insn = PREV_INSN (insn);
5214
5215   return insn;
5216 }
5217
5218 /* Check few properties of CFG between HEAD and TAIL.
5219    If HEAD (TAIL) is NULL check from the beginning (till the end) of the
5220    instruction stream.  */
5221 static void
5222 check_cfg (rtx head, rtx tail)
5223 {
5224   rtx next_tail;
5225   basic_block bb = 0;
5226   int not_first = 0, not_last;
5227
5228   if (head == NULL)
5229     head = get_insns ();
5230   if (tail == NULL)
5231     tail = get_last_insn ();
5232   next_tail = NEXT_INSN (tail);
5233
5234   do
5235     {      
5236       not_last = head != tail;        
5237
5238       if (not_first)
5239         gcc_assert (NEXT_INSN (PREV_INSN (head)) == head);
5240       if (not_last)
5241         gcc_assert (PREV_INSN (NEXT_INSN (head)) == head);
5242
5243       if (LABEL_P (head) 
5244           || (NOTE_INSN_BASIC_BLOCK_P (head)
5245               && (!not_first
5246                   || (not_first && !LABEL_P (PREV_INSN (head))))))
5247         {
5248           gcc_assert (bb == 0);   
5249           bb = BLOCK_FOR_INSN (head);
5250           if (bb != 0)
5251             gcc_assert (BB_HEAD (bb) == head);      
5252           else
5253             /* This is the case of jump table.  See inside_basic_block_p ().  */
5254             gcc_assert (LABEL_P (head) && !inside_basic_block_p (head));
5255         }
5256
5257       if (bb == 0)
5258         {
5259           gcc_assert (!inside_basic_block_p (head));
5260           head = NEXT_INSN (head);
5261         }
5262       else
5263         {
5264           gcc_assert (inside_basic_block_p (head)
5265                       || NOTE_P (head));
5266           gcc_assert (BLOCK_FOR_INSN (head) == bb);
5267         
5268           if (LABEL_P (head))
5269             {
5270               head = NEXT_INSN (head);
5271               gcc_assert (NOTE_INSN_BASIC_BLOCK_P (head));
5272             }
5273           else
5274             {
5275               if (control_flow_insn_p (head))
5276                 {
5277                   gcc_assert (prev_non_location_insn (BB_END (bb), head)
5278                               == head);
5279
5280                   if (any_uncondjump_p (head))
5281                     gcc_assert (EDGE_COUNT (bb->succs) == 1
5282                                 && BARRIER_P (NEXT_INSN (head)));
5283                   else if (any_condjump_p (head))
5284                     gcc_assert (/* Usual case.  */
5285                                 (EDGE_COUNT (bb->succs) > 1
5286                                  && !BARRIER_P (NEXT_INSN (head)))
5287                                 /* Or jump to the next instruction.  */
5288                                 || (EDGE_COUNT (bb->succs) == 1
5289                                     && (BB_HEAD (EDGE_I (bb->succs, 0)->dest)
5290                                         == JUMP_LABEL (head))));
5291                 }
5292               if (BB_END (bb) == head)
5293                 {
5294                   if (EDGE_COUNT (bb->succs) > 1)
5295                     gcc_assert (control_flow_insn_p (prev_non_location_insn
5296                                                      (head, BB_HEAD (bb)))
5297                                 || has_edge_p (bb->succs, EDGE_COMPLEX));
5298                   bb = 0;
5299                 }
5300
5301               head = NEXT_INSN (head);
5302             }
5303         }
5304
5305       not_first = 1;
5306     }
5307   while (head != next_tail);
5308
5309   gcc_assert (bb == 0);
5310 }
5311
5312 #endif /* ENABLE_CHECKING */
5313
5314 /* Extend per basic block data structures.  */
5315 static void
5316 extend_bb (void)
5317 {
5318   if (sched_scan_info->extend_bb)
5319     sched_scan_info->extend_bb ();
5320 }
5321
5322 /* Init data for BB.  */
5323 static void
5324 init_bb (basic_block bb)
5325 {
5326   if (sched_scan_info->init_bb)
5327     sched_scan_info->init_bb (bb);
5328 }
5329
5330 /* Extend per insn data structures.  */
5331 static void
5332 extend_insn (void)
5333 {
5334   if (sched_scan_info->extend_insn)
5335     sched_scan_info->extend_insn ();
5336 }
5337
5338 /* Init data structures for INSN.  */
5339 static void
5340 init_insn (rtx insn)
5341 {
5342   if (sched_scan_info->init_insn)
5343     sched_scan_info->init_insn (insn);
5344 }
5345
5346 /* Init all insns in BB.  */
5347 static void
5348 init_insns_in_bb (basic_block bb)
5349 {
5350   rtx insn;
5351
5352   FOR_BB_INSNS (bb, insn)
5353     init_insn (insn);
5354 }
5355
5356 /* A driver function to add a set of basic blocks (BBS),
5357    a single basic block (BB), a set of insns (INSNS) or a single insn (INSN)
5358    to the scheduling region.  */
5359 void
5360 sched_scan (const struct sched_scan_info_def *ssi,
5361             bb_vec_t bbs, basic_block bb, insn_vec_t insns, rtx insn)
5362 {
5363   sched_scan_info = ssi;
5364
5365   if (bbs != NULL || bb != NULL)
5366     {
5367       extend_bb ();
5368
5369       if (bbs != NULL)
5370         {
5371           unsigned i;
5372           basic_block x;
5373
5374           for (i = 0; VEC_iterate (basic_block, bbs, i, x); i++)
5375             init_bb (x);
5376         }
5377
5378       if (bb != NULL)
5379         init_bb (bb);
5380     }
5381
5382   extend_insn ();
5383
5384   if (bbs != NULL)
5385     {      
5386       unsigned i;
5387       basic_block x;
5388
5389       for (i = 0; VEC_iterate (basic_block, bbs, i, x); i++)
5390         init_insns_in_bb (x);
5391     }
5392
5393   if (bb != NULL)
5394     init_insns_in_bb (bb);
5395
5396   if (insns != NULL)
5397     {
5398       unsigned i;
5399       rtx x;
5400
5401       for (i = 0; VEC_iterate (rtx, insns, i, x); i++)
5402         init_insn (x);
5403     }
5404
5405   if (insn != NULL)
5406     init_insn (insn);
5407 }
5408
5409
5410 /* Extend data structures for logical insn UID.  */
5411 static void
5412 luids_extend_insn (void)
5413 {
5414   int new_luids_max_uid = get_max_uid () + 1;
5415
5416   VEC_safe_grow_cleared (int, heap, sched_luids, new_luids_max_uid);
5417 }
5418
5419 /* Initialize LUID for INSN.  */
5420 static void
5421 luids_init_insn (rtx insn)
5422 {
5423   int i = INSN_P (insn) ? 1 : common_sched_info->luid_for_non_insn (insn);
5424   int luid;
5425
5426   if (i >= 0)
5427     {
5428       luid = sched_max_luid;
5429       sched_max_luid += i;
5430     }
5431   else
5432     luid = -1;
5433
5434   SET_INSN_LUID (insn, luid);
5435 }
5436
5437 /* Initialize luids for BBS, BB, INSNS and INSN.
5438    The hook common_sched_info->luid_for_non_insn () is used to determine
5439    if notes, labels, etc. need luids.  */
5440 void
5441 sched_init_luids (bb_vec_t bbs, basic_block bb, insn_vec_t insns, rtx insn)
5442 {
5443   const struct sched_scan_info_def ssi =
5444     {
5445       NULL, /* extend_bb */
5446       NULL, /* init_bb */
5447       luids_extend_insn, /* extend_insn */
5448       luids_init_insn /* init_insn */
5449     };
5450
5451   sched_scan (&ssi, bbs, bb, insns, insn);
5452 }
5453
5454 /* Free LUIDs.  */
5455 void
5456 sched_finish_luids (void)
5457 {
5458   VEC_free (int, heap, sched_luids);
5459   sched_max_luid = 1;
5460 }
5461
5462 /* Return logical uid of INSN.  Helpful while debugging.  */
5463 int
5464 insn_luid (rtx insn)
5465 {
5466   return INSN_LUID (insn);
5467 }
5468
5469 /* Extend per insn data in the target.  */
5470 void
5471 sched_extend_target (void)
5472 {
5473   if (targetm.sched.h_i_d_extended)
5474     targetm.sched.h_i_d_extended ();
5475 }
5476
5477 /* Extend global scheduler structures (those, that live across calls to
5478    schedule_block) to include information about just emitted INSN.  */
5479 static void
5480 extend_h_i_d (void)
5481 {
5482   int reserve = (get_max_uid () + 1 
5483                  - VEC_length (haifa_insn_data_def, h_i_d));
5484   if (reserve > 0 
5485       && ! VEC_space (haifa_insn_data_def, h_i_d, reserve))
5486     {
5487       VEC_safe_grow_cleared (haifa_insn_data_def, heap, h_i_d, 
5488                              3 * get_max_uid () / 2);
5489       sched_extend_target ();
5490     }
5491 }
5492
5493 /* Initialize h_i_d entry of the INSN with default values.
5494    Values, that are not explicitly initialized here, hold zero.  */
5495 static void
5496 init_h_i_d (rtx insn)
5497 {
5498   if (INSN_LUID (insn) > 0)
5499     {
5500       INSN_COST (insn) = -1;
5501       QUEUE_INDEX (insn) = QUEUE_NOWHERE;
5502       INSN_TICK (insn) = INVALID_TICK;
5503       INTER_TICK (insn) = INVALID_TICK;
5504       TODO_SPEC (insn) = HARD_DEP;
5505     }
5506 }
5507
5508 /* Initialize haifa_insn_data for BBS, BB, INSNS and INSN.  */
5509 void
5510 haifa_init_h_i_d (bb_vec_t bbs, basic_block bb, insn_vec_t insns, rtx insn)
5511 {
5512   const struct sched_scan_info_def ssi =
5513     {
5514       NULL, /* extend_bb */
5515       NULL, /* init_bb */
5516       extend_h_i_d, /* extend_insn */
5517       init_h_i_d /* init_insn */
5518     };
5519
5520   sched_scan (&ssi, bbs, bb, insns, insn);
5521 }
5522
5523 /* Finalize haifa_insn_data.  */
5524 void
5525 haifa_finish_h_i_d (void)
5526 {
5527   int i;
5528   haifa_insn_data_t data;
5529   struct reg_use_data *use, *next;
5530
5531   for (i = 0; VEC_iterate (haifa_insn_data_def, h_i_d, i, data); i++)
5532     {
5533       if (data->reg_pressure != NULL)
5534         free (data->reg_pressure);
5535       for (use = data->reg_use_list; use != NULL; use = next)
5536         {
5537           next = use->next_insn_use;
5538           free (use);
5539         }
5540     }
5541   VEC_free (haifa_insn_data_def, heap, h_i_d);
5542 }
5543
5544 /* Init data for the new insn INSN.  */
5545 static void
5546 haifa_init_insn (rtx insn)
5547 {
5548   gcc_assert (insn != NULL);
5549
5550   sched_init_luids (NULL, NULL, NULL, insn);
5551   sched_extend_target ();
5552   sched_deps_init (false);
5553   haifa_init_h_i_d (NULL, NULL, NULL, insn);
5554
5555   if (adding_bb_to_current_region_p)
5556     {
5557       sd_init_insn (insn);
5558
5559       /* Extend dependency caches by one element.  */
5560       extend_dependency_caches (1, false);
5561     }
5562 }
5563
5564 /* Init data for the new basic block BB which comes after AFTER.  */
5565 static void
5566 haifa_init_only_bb (basic_block bb, basic_block after)
5567 {
5568   gcc_assert (bb != NULL);
5569
5570   sched_init_bbs ();
5571
5572   if (common_sched_info->add_block)
5573     /* This changes only data structures of the front-end.  */
5574     common_sched_info->add_block (bb, after);
5575 }
5576
5577 /* A generic version of sched_split_block ().  */
5578 basic_block
5579 sched_split_block_1 (basic_block first_bb, rtx after)
5580 {
5581   edge e;
5582
5583   e = split_block (first_bb, after);
5584   gcc_assert (e->src == first_bb);
5585
5586   /* sched_split_block emits note if *check == BB_END.  Probably it 
5587      is better to rip that note off.  */
5588
5589   return e->dest;
5590 }
5591
5592 /* A generic version of sched_create_empty_bb ().  */
5593 basic_block
5594 sched_create_empty_bb_1 (basic_block after)
5595 {
5596   return create_empty_bb (after);
5597 }
5598
5599 /* Insert PAT as an INSN into the schedule and update the necessary data
5600    structures to account for it. */
5601 rtx
5602 sched_emit_insn (rtx pat)
5603 {
5604   rtx insn = emit_insn_after (pat, last_scheduled_insn);
5605   last_scheduled_insn = insn;
5606   haifa_init_insn (insn);
5607   return insn;
5608 }
5609
5610 #endif /* INSN_SCHEDULING */