OSDN Git Service

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