OSDN Git Service

2009-05-13 Jonathan Wakely <jwakely.gcc@gmail.com>
[pf3gnuchains/gcc-fork.git] / gcc / sched-int.h
1 /* Instruction scheduling pass.  This file contains definitions used
2    internally in the scheduler.
3    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4    2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5    Free Software Foundation, Inc.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 #ifndef GCC_SCHED_INT_H
24 #define GCC_SCHED_INT_H
25
26 #ifdef INSN_SCHEDULING
27
28 /* For state_t.  */
29 #include "insn-attr.h"
30 #include "df.h"
31 #include "basic-block.h"
32
33 /* For VEC (int, heap).  */
34 #include "vecprim.h"
35
36 /* Identificator of a scheduler pass.  */
37 enum sched_pass_id_t { SCHED_PASS_UNKNOWN, SCHED_RGN_PASS, SCHED_EBB_PASS,
38                        SCHED_SMS_PASS, SCHED_SEL_PASS };
39
40 typedef VEC (basic_block, heap) *bb_vec_t;
41 typedef VEC (rtx, heap) *insn_vec_t;
42 typedef VEC(rtx, heap) *rtx_vec_t;
43
44 struct sched_scan_info_def
45 {
46   /* This hook notifies scheduler frontend to extend its internal per basic
47      block data structures.  This hook should be called once before a series of
48      calls to bb_init ().  */
49   void (*extend_bb) (void);
50
51   /* This hook makes scheduler frontend to initialize its internal data
52      structures for the passed basic block.  */
53   void (*init_bb) (basic_block);
54
55   /* This hook notifies scheduler frontend to extend its internal per insn data
56      structures.  This hook should be called once before a series of calls to
57      insn_init ().  */
58   void (*extend_insn) (void);
59
60   /* This hook makes scheduler frontend to initialize its internal data
61      structures for the passed insn.  */
62   void (*init_insn) (rtx);
63 };
64
65 extern const struct sched_scan_info_def *sched_scan_info;
66
67 extern void sched_scan (const struct sched_scan_info_def *,
68                         bb_vec_t, basic_block, insn_vec_t, rtx);
69
70 extern void sched_init_bbs (void);
71
72 extern void sched_init_luids (bb_vec_t, basic_block, insn_vec_t, rtx);
73 extern void sched_finish_luids (void);
74
75 extern void sched_extend_target (void);
76
77 extern void haifa_init_h_i_d (bb_vec_t, basic_block, insn_vec_t, rtx);
78 extern void haifa_finish_h_i_d (void);
79
80 /* Hooks that are common to all the schedulers.  */
81 struct common_sched_info_def
82 {
83   /* Called after blocks were rearranged due to movement of jump instruction.
84      The first parameter - index of basic block, in which jump currently is.
85      The second parameter - index of basic block, in which jump used
86      to be.
87      The third parameter - index of basic block, that follows the second
88      parameter.  */
89   void (*fix_recovery_cfg) (int, int, int);
90
91   /* Called to notify frontend, that new basic block is being added.
92      The first parameter - new basic block.
93      The second parameter - block, after which new basic block is being added,
94      or EXIT_BLOCK_PTR, if recovery block is being added,
95      or NULL, if standalone block is being added.  */
96   void (*add_block) (basic_block, basic_block);
97
98   /* Estimate number of insns in the basic block.  */
99   int (*estimate_number_of_insns) (basic_block);
100
101   /* Given a non-insn (!INSN_P (x)) return
102      -1 - if this rtx don't need a luid.
103      0 - if it should have the same luid as the previous insn.
104      1 - if it needs a separate luid.  */
105   int (*luid_for_non_insn) (rtx);
106
107   /* Scheduler pass identifier.  It is preferably used in assertions.  */
108   enum sched_pass_id_t sched_pass_id;
109 };
110
111 extern struct common_sched_info_def *common_sched_info;
112
113 extern const struct common_sched_info_def haifa_common_sched_info;
114
115 /* Return true if selective scheduling pass is working.  */
116 static inline bool
117 sel_sched_p (void)
118 {
119   return common_sched_info->sched_pass_id == SCHED_SEL_PASS;
120 }
121
122 /* Returns maximum priority that an insn was assigned to.  */
123 extern int get_rgn_sched_max_insns_priority (void);
124
125 /* Increases effective priority for INSN by AMOUNT.  */
126 extern void sel_add_to_insn_priority (rtx, int);
127
128 /* True if during selective scheduling we need to emulate some of haifa
129    scheduler behaviour.  */
130 extern int sched_emulate_haifa_p;
131
132 /* Mapping from INSN_UID to INSN_LUID.  In the end all other per insn data
133    structures should be indexed by luid.  */
134 extern VEC (int, heap) *sched_luids;
135 #define INSN_LUID(INSN) (VEC_index (int, sched_luids, INSN_UID (INSN)))
136 #define LUID_BY_UID(UID) (VEC_index (int, sched_luids, UID))
137
138 #define SET_INSN_LUID(INSN, LUID) \
139 (VEC_replace (int, sched_luids, INSN_UID (INSN), (LUID)))
140
141 /* The highest INSN_LUID.  */
142 extern int sched_max_luid;
143
144 extern int insn_luid (rtx);
145
146 /* This list holds ripped off notes from the current block.  These notes will
147    be attached to the beginning of the block when its scheduling is
148    finished.  */
149 extern rtx note_list;
150
151 extern void remove_notes (rtx, rtx);
152 extern rtx restore_other_notes (rtx, basic_block);
153 extern void sched_insns_init (rtx);
154 extern void sched_insns_finish (void);
155
156 extern void *xrecalloc (void *, size_t, size_t, size_t);
157 extern rtx bb_note (basic_block);
158
159 extern void reemit_notes (rtx);
160
161 /* Functions in haifa-sched.c.  */
162 extern int haifa_classify_insn (const_rtx);
163
164 /* Functions in sel-sched-ir.c.  */
165 extern void sel_find_rgns (void);
166 extern void sel_mark_hard_insn (rtx);
167
168 extern size_t dfa_state_size;
169
170 extern void advance_state (state_t);
171
172 extern void setup_sched_dump (void);
173 extern void sched_init (void);
174 extern void sched_finish (void);
175
176 extern bool sel_insn_is_speculation_check (rtx);
177
178 /* Describe the ready list of the scheduler.
179    VEC holds space enough for all insns in the current region.  VECLEN
180    says how many exactly.
181    FIRST is the index of the element with the highest priority; i.e. the
182    last one in the ready list, since elements are ordered by ascending
183    priority.
184    N_READY determines how many insns are on the ready list.  */
185 struct ready_list
186 {
187   rtx *vec;
188   int veclen;
189   int first;
190   int n_ready;
191 };
192
193 extern char *ready_try;
194 extern struct ready_list ready;
195
196 extern int max_issue (struct ready_list *, int, state_t, int *);
197
198 extern void ebb_compute_jump_reg_dependencies (rtx, regset, regset, regset);
199
200 extern edge find_fallthru_edge (basic_block);
201
202 extern void (* sched_init_only_bb) (basic_block, basic_block);
203 extern basic_block (* sched_split_block) (basic_block, rtx);
204 extern basic_block sched_split_block_1 (basic_block, rtx);
205 extern basic_block (* sched_create_empty_bb) (basic_block);
206 extern basic_block sched_create_empty_bb_1 (basic_block);
207
208 extern basic_block sched_create_recovery_block (basic_block *);
209 extern void sched_create_recovery_edges (basic_block, basic_block,
210                                          basic_block);
211
212 /* Pointer to data describing the current DFA state.  */
213 extern state_t curr_state;
214
215 /* Type to represent status of a dependence.  */
216 typedef int ds_t;
217
218 /* Type to represent weakness of speculative dependence.  */
219 typedef int dw_t;
220
221 extern enum reg_note ds_to_dk (ds_t);
222 extern ds_t dk_to_ds (enum reg_note);
223
224 /* Information about the dependency.  */
225 struct _dep
226 {
227   /* Producer.  */
228   rtx pro;
229
230   /* Consumer.  */
231   rtx con;
232
233   /* Dependency major type.  This field is superseded by STATUS below.
234      Though, it is still in place because some targets use it.  */
235   enum reg_note type;
236
237   /* Dependency status.  This field holds all dependency types and additional
238      information for speculative dependencies.  */
239   ds_t status;
240 };
241
242 typedef struct _dep dep_def;
243 typedef dep_def *dep_t;
244
245 #define DEP_PRO(D) ((D)->pro)
246 #define DEP_CON(D) ((D)->con)
247 #define DEP_TYPE(D) ((D)->type)
248 #define DEP_STATUS(D) ((D)->status)
249
250 /* Functions to work with dep.  */
251
252 extern void init_dep_1 (dep_t, rtx, rtx, enum reg_note, ds_t);
253 extern void init_dep (dep_t, rtx, rtx, enum reg_note);
254
255 extern void sd_debug_dep (dep_t);
256
257 /* Definition of this struct resides below.  */
258 struct _dep_node;
259 typedef struct _dep_node *dep_node_t;
260
261 /* A link in the dependency list.  This is essentially an equivalent of a
262    single {INSN, DEPS}_LIST rtx.  */
263 struct _dep_link
264 {
265   /* Dep node with all the data.  */
266   dep_node_t node;
267
268   /* Next link in the list. For the last one it is NULL.  */
269   struct _dep_link *next;
270
271   /* Pointer to the next field of the previous link in the list.
272      For the first link this points to the deps_list->first.
273
274      With help of this field it is easy to remove and insert links to the
275      list.  */
276   struct _dep_link **prev_nextp;
277 };
278 typedef struct _dep_link *dep_link_t;
279
280 #define DEP_LINK_NODE(N) ((N)->node)
281 #define DEP_LINK_NEXT(N) ((N)->next)
282 #define DEP_LINK_PREV_NEXTP(N) ((N)->prev_nextp)
283
284 /* Macros to work dep_link.  For most usecases only part of the dependency
285    information is need.  These macros conveniently provide that piece of
286    information.  */
287
288 #define DEP_LINK_DEP(N) (DEP_NODE_DEP (DEP_LINK_NODE (N)))
289 #define DEP_LINK_PRO(N) (DEP_PRO (DEP_LINK_DEP (N)))
290 #define DEP_LINK_CON(N) (DEP_CON (DEP_LINK_DEP (N)))
291 #define DEP_LINK_TYPE(N) (DEP_TYPE (DEP_LINK_DEP (N)))
292 #define DEP_LINK_STATUS(N) (DEP_STATUS (DEP_LINK_DEP (N)))
293
294 /* A list of dep_links.  */
295 struct _deps_list
296 {
297   /* First element.  */
298   dep_link_t first;
299
300   /* Total number of elements in the list.  */
301   int n_links;
302 };
303 typedef struct _deps_list *deps_list_t;
304
305 #define DEPS_LIST_FIRST(L) ((L)->first)
306 #define DEPS_LIST_N_LINKS(L) ((L)->n_links)
307
308 /* Suppose we have a dependence Y between insn pro1 and con1, where pro1 has
309    additional dependents con0 and con2, and con1 is dependent on additional
310    insns pro0 and pro1:
311
312    .con0      pro0
313    . ^         |
314    . |         |
315    . |         |
316    . X         A
317    . |         |
318    . |         |
319    . |         V
320    .pro1--Y-->con1
321    . |         ^
322    . |         |
323    . |         |
324    . Z         B
325    . |         |
326    . |         |
327    . V         |
328    .con2      pro2
329
330    This is represented using a "dep_node" for each dependence arc, which are
331    connected as follows (diagram is centered around Y which is fully shown;
332    other dep_nodes shown partially):
333
334    .          +------------+    +--------------+    +------------+
335    .          : dep_node X :    |  dep_node Y  |    : dep_node Z :
336    .          :            :    |              |    :            :
337    .          :            :    |              |    :            :
338    .          : forw       :    |  forw        |    : forw       :
339    .          : +--------+ :    |  +--------+  |    : +--------+ :
340    forw_deps  : |dep_link| :    |  |dep_link|  |    : |dep_link| :
341    +-----+    : | +----+ | :    |  | +----+ |  |    : | +----+ | :
342    |first|----->| |next|-+------+->| |next|-+--+----->| |next|-+--->NULL
343    +-----+    : | +----+ | :    |  | +----+ |  |    : | +----+ | :
344    . ^  ^     : |     ^  | :    |  |     ^  |  |    : |        | :
345    . |  |     : |     |  | :    |  |     |  |  |    : |        | :
346    . |  +--<----+--+  +--+---<--+--+--+  +--+--+--<---+--+     | :
347    . |        : |  |     | :    |  |  |     |  |    : |  |     | :
348    . |        : | +----+ | :    |  | +----+ |  |    : | +----+ | :
349    . |        : | |prev| | :    |  | |prev| |  |    : | |prev| | :
350    . |        : | |next| | :    |  | |next| |  |    : | |next| | :
351    . |        : | +----+ | :    |  | +----+ |  |    : | +----+ | :
352    . |        : |        | :<-+ |  |        |  |<-+ : |        | :<-+
353    . |        : | +----+ | :  | |  | +----+ |  |  | : | +----+ | :  |
354    . |        : | |node|-+----+ |  | |node|-+--+--+ : | |node|-+----+
355    . |        : | +----+ | :    |  | +----+ |  |    : | +----+ | :
356    . |        : |        | :    |  |        |  |    : |        | :
357    . |        : +--------+ :    |  +--------+  |    : +--------+ :
358    . |        :            :    |              |    :            :
359    . |        :  SAME pro1 :    |  +--------+  |    :  SAME pro1 :
360    . |        :  DIFF con0 :    |  |dep     |  |    :  DIFF con2 :
361    . |        :            :    |  |        |  |    :            :
362    . |                          |  | +----+ |  |
363    .RTX<------------------------+--+-|pro1| |  |
364    .pro1                        |  | +----+ |  |
365    .                            |  |        |  |
366    .                            |  | +----+ |  |
367    .RTX<------------------------+--+-|con1| |  |
368    .con1                        |  | +----+ |  |
369    . |                          |  |        |  |
370    . |                          |  | +----+ |  |
371    . |                          |  | |kind| |  |
372    . |                          |  | +----+ |  |
373    . |        :            :    |  | |stat| |  |    :            :
374    . |        :  DIFF pro0 :    |  | +----+ |  |    :  DIFF pro2 :
375    . |        :  SAME con1 :    |  |        |  |    :  SAME con1 :
376    . |        :            :    |  +--------+  |    :            :
377    . |        :            :    |              |    :            :
378    . |        : back       :    |  back        |    : back       :
379    . v        : +--------+ :    |  +--------+  |    : +--------+ :
380    back_deps  : |dep_link| :    |  |dep_link|  |    : |dep_link| :
381    +-----+    : | +----+ | :    |  | +----+ |  |    : | +----+ | :
382    |first|----->| |next|-+------+->| |next|-+--+----->| |next|-+--->NULL
383    +-----+    : | +----+ | :    |  | +----+ |  |    : | +----+ | :
384    .    ^     : |     ^  | :    |  |     ^  |  |    : |        | :
385    .    |     : |     |  | :    |  |     |  |  |    : |        | :
386    .    +--<----+--+  +--+---<--+--+--+  +--+--+--<---+--+     | :
387    .          : |  |     | :    |  |  |     |  |    : |  |     | :
388    .          : | +----+ | :    |  | +----+ |  |    : | +----+ | :
389    .          : | |prev| | :    |  | |prev| |  |    : | |prev| | :
390    .          : | |next| | :    |  | |next| |  |    : | |next| | :
391    .          : | +----+ | :    |  | +----+ |  |    : | +----+ | :
392    .          : |        | :<-+ |  |        |  |<-+ : |        | :<-+
393    .          : | +----+ | :  | |  | +----+ |  |  | : | +----+ | :  |
394    .          : | |node|-+----+ |  | |node|-+--+--+ : | |node|-+----+
395    .          : | +----+ | :    |  | +----+ |  |    : | +----+ | :
396    .          : |        | :    |  |        |  |    : |        | :
397    .          : +--------+ :    |  +--------+  |    : +--------+ :
398    .          :            :    |              |    :            :
399    .          : dep_node A :    |  dep_node Y  |    : dep_node B :
400    .          +------------+    +--------------+    +------------+
401 */
402
403 struct _dep_node
404 {
405   /* Backward link.  */
406   struct _dep_link back;
407
408   /* The dep.  */
409   struct _dep dep;
410
411   /* Forward link.  */
412   struct _dep_link forw;
413 };
414
415 #define DEP_NODE_BACK(N) (&(N)->back)
416 #define DEP_NODE_DEP(N) (&(N)->dep)
417 #define DEP_NODE_FORW(N) (&(N)->forw)
418
419 /* The following enumeration values tell us what dependencies we
420    should use to implement the barrier.  We use true-dependencies for
421    TRUE_BARRIER and anti-dependencies for MOVE_BARRIER.  */
422 enum reg_pending_barrier_mode
423 {
424   NOT_A_BARRIER = 0,
425   MOVE_BARRIER,
426   TRUE_BARRIER
427 };
428
429 /* Whether a register movement is associated with a call.  */
430 enum post_call_group
431 {
432   not_post_call,
433   post_call,
434   post_call_initial
435 };
436
437 /* Insns which affect pseudo-registers.  */
438 struct deps_reg
439 {
440   rtx uses;
441   rtx sets;
442   rtx clobbers;
443   int uses_length;
444   int clobbers_length;
445 };
446
447 /* Describe state of dependencies used during sched_analyze phase.  */
448 struct deps
449 {
450   /* The *_insns and *_mems are paired lists.  Each pending memory operation
451      will have a pointer to the MEM rtx on one list and a pointer to the
452      containing insn on the other list in the same place in the list.  */
453
454   /* We can't use add_dependence like the old code did, because a single insn
455      may have multiple memory accesses, and hence needs to be on the list
456      once for each memory access.  Add_dependence won't let you add an insn
457      to a list more than once.  */
458
459   /* An INSN_LIST containing all insns with pending read operations.  */
460   rtx pending_read_insns;
461
462   /* An EXPR_LIST containing all MEM rtx's which are pending reads.  */
463   rtx pending_read_mems;
464
465   /* An INSN_LIST containing all insns with pending write operations.  */
466   rtx pending_write_insns;
467
468   /* An EXPR_LIST containing all MEM rtx's which are pending writes.  */
469   rtx pending_write_mems;
470
471   /* We must prevent the above lists from ever growing too large since
472      the number of dependencies produced is at least O(N*N),
473      and execution time is at least O(4*N*N), as a function of the
474      length of these pending lists.  */
475
476   /* Indicates the length of the pending_read list.  */
477   int pending_read_list_length;
478
479   /* Indicates the length of the pending_write list.  */
480   int pending_write_list_length;
481
482   /* Length of the pending memory flush list. Large functions with no
483      calls may build up extremely large lists.  */
484   int pending_flush_length;
485
486   /* The last insn upon which all memory references must depend.
487      This is an insn which flushed the pending lists, creating a dependency
488      between it and all previously pending memory references.  This creates
489      a barrier (or a checkpoint) which no memory reference is allowed to cross.
490
491      This includes all non constant CALL_INSNs.  When we do interprocedural
492      alias analysis, this restriction can be relaxed.
493      This may also be an INSN that writes memory if the pending lists grow
494      too large.  */
495   rtx last_pending_memory_flush;
496
497   /* A list of the last function calls we have seen.  We use a list to
498      represent last function calls from multiple predecessor blocks.
499      Used to prevent register lifetimes from expanding unnecessarily.  */
500   rtx last_function_call;
501
502   /* A list of insns which use a pseudo register that does not already
503      cross a call.  We create dependencies between each of those insn
504      and the next call insn, to ensure that they won't cross a call after
505      scheduling is done.  */
506   rtx sched_before_next_call;
507
508   /* Used to keep post-call pseudo/hard reg movements together with
509      the call.  */
510   enum post_call_group in_post_call_group_p;
511
512   /* The maximum register number for the following arrays.  Before reload
513      this is max_reg_num; after reload it is FIRST_PSEUDO_REGISTER.  */
514   int max_reg;
515
516   /* Element N is the next insn that sets (hard or pseudo) register
517      N within the current basic block; or zero, if there is no
518      such insn.  Needed for new registers which may be introduced
519      by splitting insns.  */
520   struct deps_reg *reg_last;
521
522   /* Element N is set for each register that has any nonzero element
523      in reg_last[N].{uses,sets,clobbers}.  */
524   regset_head reg_last_in_use;
525
526   /* Element N is set for each register that is conditionally set.  */
527   regset_head reg_conditional_sets;
528
529   /* Shows the last value of reg_pending_barrier associated with the insn.  */
530   enum reg_pending_barrier_mode last_reg_pending_barrier;
531
532   /* True when this context should be treated as a readonly by 
533      the analysis.  */
534   BOOL_BITFIELD readonly : 1;
535 };
536
537 typedef struct deps *deps_t;
538
539 /* This structure holds some state of the current scheduling pass, and
540    contains some function pointers that abstract out some of the non-generic
541    functionality from functions such as schedule_block or schedule_insn.
542    There is one global variable, current_sched_info, which points to the
543    sched_info structure currently in use.  */
544 struct haifa_sched_info
545 {
546   /* Add all insns that are initially ready to the ready list.  Called once
547      before scheduling a set of insns.  */
548   void (*init_ready_list) (void);
549   /* Called after taking an insn from the ready list.  Returns nonzero if
550      this insn can be scheduled, nonzero if we should silently discard it.  */
551   int (*can_schedule_ready_p) (rtx);
552   /* Return nonzero if there are more insns that should be scheduled.  */
553   int (*schedule_more_p) (void);
554   /* Called after an insn has all its hard dependencies resolved. 
555      Adjusts status of instruction (which is passed through second parameter)
556      to indicate if instruction should be moved to the ready list or the
557      queue, or if it should silently discard it (until next resolved
558      dependence).  */
559   ds_t (*new_ready) (rtx, ds_t);
560   /* Compare priority of two insns.  Return a positive number if the second
561      insn is to be preferred for scheduling, and a negative one if the first
562      is to be preferred.  Zero if they are equally good.  */
563   int (*rank) (rtx, rtx);
564   /* Return a string that contains the insn uid and optionally anything else
565      necessary to identify this insn in an output.  It's valid to use a
566      static buffer for this.  The ALIGNED parameter should cause the string
567      to be formatted so that multiple output lines will line up nicely.  */
568   const char *(*print_insn) (const_rtx, int);
569   /* Return nonzero if an insn should be included in priority
570      calculations.  */
571   int (*contributes_to_priority) (rtx, rtx);
572
573   /* The boundaries of the set of insns to be scheduled.  */
574   rtx prev_head, next_tail;
575
576   /* Filled in after the schedule is finished; the first and last scheduled
577      insns.  */
578   rtx head, tail;
579
580   /* If nonzero, enables an additional sanity check in schedule_block.  */
581   unsigned int queue_must_finish_empty:1;
582
583   /* Maximum priority that has been assigned to an insn.  */
584   int sched_max_insns_priority;
585
586   /* Hooks to support speculative scheduling.  */
587
588   /* Called to notify frontend that instruction is being added (second
589      parameter == 0) or removed (second parameter == 1).  */     
590   void (*add_remove_insn) (rtx, int);
591
592   /* Called to notify frontend that instruction is being scheduled.
593      The first parameter - instruction to scheduled, the second parameter -
594      last scheduled instruction.  */
595   void (*begin_schedule_ready) (rtx, rtx);
596
597   /* If the second parameter is not NULL, return nonnull value, if the
598      basic block should be advanced.
599      If the second parameter is NULL, return the next basic block in EBB.
600      The first parameter is the current basic block in EBB.  */
601   basic_block (*advance_target_bb) (basic_block, rtx);
602
603   /* ??? FIXME: should use straight bitfields inside sched_info instead of
604      this flag field.  */
605   unsigned int flags;
606 };
607
608 /* This structure holds description of the properties for speculative
609    scheduling.  */
610 struct spec_info_def
611 {
612   /* Holds types of allowed speculations: BEGIN_{DATA|CONTROL},
613      BE_IN_{DATA_CONTROL}.  */
614   int mask;
615
616   /* A dump file for additional information on speculative scheduling.  */
617   FILE *dump;
618
619   /* Minimal cumulative weakness of speculative instruction's
620      dependencies, so that insn will be scheduled.  */
621   dw_t data_weakness_cutoff;
622
623   /* Minimal usefulness of speculative instruction to be considered for
624      scheduling.  */
625   int control_weakness_cutoff;
626
627   /* Flags from the enum SPEC_SCHED_FLAGS.  */
628   int flags;
629 };
630 typedef struct spec_info_def *spec_info_t;
631
632 extern spec_info_t spec_info;
633
634 extern struct haifa_sched_info *current_sched_info;
635
636 /* Indexed by INSN_UID, the collection of all data associated with
637    a single instruction.  */
638
639 struct _haifa_deps_insn_data
640 {
641   /* The number of incoming edges in the forward dependency graph.
642      As scheduling proceeds, counts are decreased.  An insn moves to
643      the ready queue when its counter reaches zero.  */
644   int dep_count;
645
646   /* Nonzero if instruction has internal dependence
647      (e.g. add_dependence was invoked with (insn == elem)).  */
648   unsigned int has_internal_dep;
649
650   /* NB: We can't place 'struct _deps_list' here instead of deps_list_t into
651      h_i_d because when h_i_d extends, addresses of the deps_list->first
652      change without updating deps_list->first->next->prev_nextp.  Thus
653      BACK_DEPS and RESOLVED_BACK_DEPS are allocated on the heap and FORW_DEPS
654      list is allocated on the obstack.  */
655
656   /* A list of hard backward dependencies.  The insn is a consumer of all the
657      deps mentioned here.  */
658   deps_list_t hard_back_deps;
659
660   /* A list of speculative (weak) dependencies.  The insn is a consumer of all
661      the deps mentioned here.  */
662   deps_list_t spec_back_deps;
663
664   /* A list of insns which depend on the instruction.  Unlike 'back_deps',
665      it represents forward dependencies.  */
666   deps_list_t forw_deps;
667
668   /* A list of scheduled producers of the instruction.  Links are being moved
669      from 'back_deps' to 'resolved_back_deps' while scheduling.  */
670   deps_list_t resolved_back_deps;
671
672   /* A list of scheduled consumers of the instruction.  Links are being moved
673      from 'forw_deps' to 'resolved_forw_deps' while scheduling to fasten the
674      search in 'forw_deps'.  */
675   deps_list_t resolved_forw_deps;
676
677   /* Some insns (e.g. call) are not allowed to move across blocks.  */
678   unsigned int cant_move : 1;
679 };
680
681 struct _haifa_insn_data
682 {
683   /* We can't place 'struct _deps_list' into h_i_d instead of deps_list_t
684      because when h_i_d extends, addresses of the deps_list->first
685      change without updating deps_list->first->next->prev_nextp.  */
686
687   /* Logical uid gives the original ordering of the insns.  */
688   int luid;
689
690   /* A priority for each insn.  */
691   int priority;
692
693   /* The minimum clock tick at which the insn becomes ready.  This is
694      used to note timing constraints for the insns in the pending list.  */
695   int tick;
696
697   /* INTER_TICK is used to adjust INSN_TICKs of instructions from the
698      subsequent blocks in a region.  */
699   int inter_tick;
700   
701   /* See comment on QUEUE_INDEX macro in haifa-sched.c.  */
702   int queue_index;
703
704   short cost;
705
706   /* This weight is an estimation of the insn's contribution to
707      register pressure.  */
708   short reg_weight;
709
710   /* Set if there's DEF-USE dependence between some speculatively
711      moved load insn and this one.  */
712   unsigned int fed_by_spec_load : 1;
713   unsigned int is_load_insn : 1;
714
715   /* '> 0' if priority is valid,
716      '== 0' if priority was not yet computed,
717      '< 0' if priority in invalid and should be recomputed.  */
718   signed char priority_status;
719
720   /* What speculations are necessary to apply to schedule the instruction.  */
721   ds_t todo_spec;
722
723   /* What speculations were already applied.  */
724   ds_t done_spec; 
725
726   /* What speculations are checked by this instruction.  */
727   ds_t check_spec;
728
729   /* Recovery block for speculation checks.  */
730   basic_block recovery_block;
731
732   /* Original pattern of the instruction.  */
733   rtx orig_pat;
734 };
735
736 typedef struct _haifa_insn_data haifa_insn_data_def;
737 typedef haifa_insn_data_def *haifa_insn_data_t;
738
739 DEF_VEC_O (haifa_insn_data_def);
740 DEF_VEC_ALLOC_O (haifa_insn_data_def, heap);
741
742 extern VEC(haifa_insn_data_def, heap) *h_i_d;
743
744 #define HID(INSN) (VEC_index (haifa_insn_data_def, h_i_d, INSN_UID (INSN)))
745
746 /* Accessor macros for h_i_d.  There are more in haifa-sched.c and
747    sched-rgn.c.  */
748 #define INSN_PRIORITY(INSN) (HID (INSN)->priority)
749 #define INSN_REG_WEIGHT(INSN) (HID (INSN)->reg_weight)
750 #define INSN_PRIORITY_STATUS(INSN) (HID (INSN)->priority_status)
751
752 typedef struct _haifa_deps_insn_data haifa_deps_insn_data_def;
753 typedef haifa_deps_insn_data_def *haifa_deps_insn_data_t;
754
755 DEF_VEC_O (haifa_deps_insn_data_def);
756 DEF_VEC_ALLOC_O (haifa_deps_insn_data_def, heap);
757
758 extern VEC(haifa_deps_insn_data_def, heap) *h_d_i_d;
759
760 #define HDID(INSN) (VEC_index (haifa_deps_insn_data_def, h_d_i_d,       \
761                                INSN_LUID (INSN)))
762 #define INSN_DEP_COUNT(INSN)    (HDID (INSN)->dep_count)
763 #define HAS_INTERNAL_DEP(INSN)  (HDID (INSN)->has_internal_dep)
764 #define INSN_FORW_DEPS(INSN) (HDID (INSN)->forw_deps)
765 #define INSN_RESOLVED_BACK_DEPS(INSN) (HDID (INSN)->resolved_back_deps)
766 #define INSN_RESOLVED_FORW_DEPS(INSN) (HDID (INSN)->resolved_forw_deps)
767 #define INSN_HARD_BACK_DEPS(INSN) (HDID (INSN)->hard_back_deps)
768 #define INSN_SPEC_BACK_DEPS(INSN) (HDID (INSN)->spec_back_deps)
769 #define CANT_MOVE(INSN) (HDID (INSN)->cant_move)
770 #define CANT_MOVE_BY_LUID(LUID) (VEC_index (haifa_deps_insn_data_def, h_d_i_d, \
771                                             LUID)->cant_move)
772
773
774 #define INSN_PRIORITY(INSN)     (HID (INSN)->priority)
775 #define INSN_PRIORITY_STATUS(INSN) (HID (INSN)->priority_status)
776 #define INSN_PRIORITY_KNOWN(INSN) (INSN_PRIORITY_STATUS (INSN) > 0)
777 #define TODO_SPEC(INSN) (HID (INSN)->todo_spec)
778 #define DONE_SPEC(INSN) (HID (INSN)->done_spec)
779 #define CHECK_SPEC(INSN) (HID (INSN)->check_spec)
780 #define RECOVERY_BLOCK(INSN) (HID (INSN)->recovery_block)
781 #define ORIG_PAT(INSN) (HID (INSN)->orig_pat)
782
783 /* INSN is either a simple or a branchy speculation check.  */
784 #define IS_SPECULATION_CHECK_P(INSN) \
785   (sel_sched_p () ? sel_insn_is_speculation_check (INSN) : RECOVERY_BLOCK (INSN) != NULL)
786
787 /* INSN is a speculation check that will simply reexecute the speculatively
788    scheduled instruction if the speculation fails.  */
789 #define IS_SPECULATION_SIMPLE_CHECK_P(INSN) \
790   (RECOVERY_BLOCK (INSN) == EXIT_BLOCK_PTR)
791
792 /* INSN is a speculation check that will branch to RECOVERY_BLOCK if the
793    speculation fails.  Insns in that block will reexecute the speculatively
794    scheduled code and then will return immediately after INSN thus preserving
795    semantics of the program.  */
796 #define IS_SPECULATION_BRANCHY_CHECK_P(INSN) \
797   (RECOVERY_BLOCK (INSN) != NULL && RECOVERY_BLOCK (INSN) != EXIT_BLOCK_PTR)
798
799 /* Dep status (aka ds_t) of the link encapsulates information, that is needed
800    for speculative scheduling.  Namely, it is 4 integers in the range
801    [0, MAX_DEP_WEAK] and 3 bits.
802    The integers correspond to the probability of the dependence to *not*
803    exist, it is the probability, that overcoming of this dependence will
804    not be followed by execution of the recovery code.  Nevertheless,
805    whatever high the probability of success is, recovery code should still
806    be generated to preserve semantics of the program.  To find a way to
807    get/set these integers, please refer to the {get, set}_dep_weak ()
808    functions in sched-deps.c .
809    The 3 bits in the DEP_STATUS correspond to 3 dependence types: true-,
810    output- and anti- dependence.  It is not enough for speculative scheduling
811    to know just the major type of all the dependence between two instructions,
812    as only true dependence can be overcome.
813    There also is the 4-th bit in the DEP_STATUS (HARD_DEP), that is reserved
814    for using to describe instruction's status.  It is set whenever instruction
815    has at least one dependence, that cannot be overcame.
816    See also: check_dep_status () in sched-deps.c .  */
817
818 /* We exclude sign bit.  */
819 #define BITS_PER_DEP_STATUS (HOST_BITS_PER_INT - 1)
820
821 /* First '4' stands for 3 dep type bits and HARD_DEP bit.
822    Second '4' stands for BEGIN_{DATA, CONTROL}, BE_IN_{DATA, CONTROL}
823    dep weakness.  */
824 #define BITS_PER_DEP_WEAK ((BITS_PER_DEP_STATUS - 4) / 4)
825
826 /* Mask of speculative weakness in dep_status.  */
827 #define DEP_WEAK_MASK ((1 << BITS_PER_DEP_WEAK) - 1)
828
829 /* This constant means that dependence is fake with 99.999...% probability.
830    This is the maximum value, that can appear in dep_status.
831    Note, that we don't want MAX_DEP_WEAK to be the same as DEP_WEAK_MASK for
832    debugging reasons.  Though, it can be set to DEP_WEAK_MASK, and, when
833    done so, we'll get fast (mul for)/(div by) NO_DEP_WEAK.  */
834 #define MAX_DEP_WEAK (DEP_WEAK_MASK - 1)
835
836 /* This constant means that dependence is 99.999...% real and it is a really
837    bad idea to overcome it (though this can be done, preserving program
838    semantics).  */
839 #define MIN_DEP_WEAK 1
840
841 /* This constant represents 100% probability.
842    E.g. it is used to represent weakness of dependence, that doesn't exist.  */
843 #define NO_DEP_WEAK (MAX_DEP_WEAK + MIN_DEP_WEAK)
844
845 /* Default weakness of speculative dependence.  Used when we can't say
846    neither bad nor good about the dependence.  */
847 #define UNCERTAIN_DEP_WEAK (MAX_DEP_WEAK - MAX_DEP_WEAK / 4)
848
849 /* Offset for speculative weaknesses in dep_status.  */
850 enum SPEC_TYPES_OFFSETS {
851   BEGIN_DATA_BITS_OFFSET = 0,
852   BE_IN_DATA_BITS_OFFSET = BEGIN_DATA_BITS_OFFSET + BITS_PER_DEP_WEAK,
853   BEGIN_CONTROL_BITS_OFFSET = BE_IN_DATA_BITS_OFFSET + BITS_PER_DEP_WEAK,
854   BE_IN_CONTROL_BITS_OFFSET = BEGIN_CONTROL_BITS_OFFSET + BITS_PER_DEP_WEAK
855 };
856
857 /* The following defines provide numerous constants used to distinguish between
858    different types of speculative dependencies.  */
859
860 /* Dependence can be overcome with generation of new data speculative
861    instruction.  */
862 #define BEGIN_DATA (((ds_t) DEP_WEAK_MASK) << BEGIN_DATA_BITS_OFFSET)
863
864 /* This dependence is to the instruction in the recovery block, that was
865    formed to recover after data-speculation failure.
866    Thus, this dependence can overcome with generating of the copy of
867    this instruction in the recovery block.  */
868 #define BE_IN_DATA (((ds_t) DEP_WEAK_MASK) << BE_IN_DATA_BITS_OFFSET)
869
870 /* Dependence can be overcome with generation of new control speculative
871    instruction.  */
872 #define BEGIN_CONTROL (((ds_t) DEP_WEAK_MASK) << BEGIN_CONTROL_BITS_OFFSET)
873
874 /* This dependence is to the instruction in the recovery block, that was
875    formed to recover after control-speculation failure.
876    Thus, this dependence can be overcome with generating of the copy of
877    this instruction in the recovery block.  */
878 #define BE_IN_CONTROL (((ds_t) DEP_WEAK_MASK) << BE_IN_CONTROL_BITS_OFFSET)
879
880 /* A few convenient combinations.  */
881 #define BEGIN_SPEC (BEGIN_DATA | BEGIN_CONTROL)
882 #define DATA_SPEC (BEGIN_DATA | BE_IN_DATA)
883 #define CONTROL_SPEC (BEGIN_CONTROL | BE_IN_CONTROL)
884 #define SPECULATIVE (DATA_SPEC | CONTROL_SPEC)
885 #define BE_IN_SPEC (BE_IN_DATA | BE_IN_CONTROL)
886
887 /* Constants, that are helpful in iterating through dep_status.  */
888 #define FIRST_SPEC_TYPE BEGIN_DATA
889 #define LAST_SPEC_TYPE BE_IN_CONTROL
890 #define SPEC_TYPE_SHIFT BITS_PER_DEP_WEAK
891
892 /* Dependence on instruction can be of multiple types
893    (e.g. true and output). This fields enhance REG_NOTE_KIND information
894    of the dependence.  */
895 #define DEP_TRUE (((ds_t) 1) << (BE_IN_CONTROL_BITS_OFFSET + BITS_PER_DEP_WEAK))
896 #define DEP_OUTPUT (DEP_TRUE << 1)
897 #define DEP_ANTI (DEP_OUTPUT << 1)
898
899 #define DEP_TYPES (DEP_TRUE | DEP_OUTPUT | DEP_ANTI)
900
901 /* Instruction has non-speculative dependence.  This bit represents the
902    property of an instruction - not the one of a dependence.
903    Therefore, it can appear only in TODO_SPEC field of an instruction.  */
904 #define HARD_DEP (DEP_ANTI << 1)
905
906 /* This represents the results of calling sched-deps.c functions, 
907    which modify dependencies.  */
908 enum DEPS_ADJUST_RESULT {
909   /* No dependence needed (e.g. producer == consumer).  */
910   DEP_NODEP,
911   /* Dependence is already present and wasn't modified.  */
912   DEP_PRESENT,
913   /* Existing dependence was modified to include additional information.  */
914   DEP_CHANGED,
915   /* New dependence has been created.  */
916   DEP_CREATED
917 };
918
919 /* Represents the bits that can be set in the flags field of the 
920    sched_info structure.  */
921 enum SCHED_FLAGS {
922   /* If set, generate links between instruction as DEPS_LIST.
923      Otherwise, generate usual INSN_LIST links.  */
924   USE_DEPS_LIST = 1,
925   /* Perform data or control (or both) speculation.
926      Results in generation of data and control speculative dependencies.
927      Requires USE_DEPS_LIST set.  */
928   DO_SPECULATION = USE_DEPS_LIST << 1,
929   SCHED_RGN = DO_SPECULATION << 1,
930   SCHED_EBB = SCHED_RGN << 1,
931   /* Scheduler can possibly create new basic blocks.  Used for assertions.  */
932   NEW_BBS = SCHED_EBB << 1,
933   SEL_SCHED = NEW_BBS << 1
934 };
935
936 enum SPEC_SCHED_FLAGS {
937   COUNT_SPEC_IN_CRITICAL_PATH = 1,
938   PREFER_NON_DATA_SPEC = COUNT_SPEC_IN_CRITICAL_PATH << 1,
939   PREFER_NON_CONTROL_SPEC = PREFER_NON_DATA_SPEC << 1,
940   SEL_SCHED_SPEC_DONT_CHECK_CONTROL = PREFER_NON_CONTROL_SPEC << 1
941 };
942
943 #define NOTE_NOT_BB_P(NOTE) (NOTE_P (NOTE) && (NOTE_KIND (NOTE) \
944                                                != NOTE_INSN_BASIC_BLOCK))
945
946 extern FILE *sched_dump;
947 extern int sched_verbose;
948
949 extern spec_info_t spec_info;
950 extern bool haifa_recovery_bb_ever_added_p;
951
952 /* Exception Free Loads:
953
954    We define five classes of speculative loads: IFREE, IRISKY,
955    PFREE, PRISKY, and MFREE.
956
957    IFREE loads are loads that are proved to be exception-free, just
958    by examining the load insn.  Examples for such loads are loads
959    from TOC and loads of global data.
960
961    IRISKY loads are loads that are proved to be exception-risky,
962    just by examining the load insn.  Examples for such loads are
963    volatile loads and loads from shared memory.
964
965    PFREE loads are loads for which we can prove, by examining other
966    insns, that they are exception-free.  Currently, this class consists
967    of loads for which we are able to find a "similar load", either in
968    the target block, or, if only one split-block exists, in that split
969    block.  Load2 is similar to load1 if both have same single base
970    register.  We identify only part of the similar loads, by finding
971    an insn upon which both load1 and load2 have a DEF-USE dependence.
972
973    PRISKY loads are loads for which we can prove, by examining other
974    insns, that they are exception-risky.  Currently we have two proofs for
975    such loads.  The first proof detects loads that are probably guarded by a
976    test on the memory address.  This proof is based on the
977    backward and forward data dependence information for the region.
978    Let load-insn be the examined load.
979    Load-insn is PRISKY iff ALL the following hold:
980
981    - insn1 is not in the same block as load-insn
982    - there is a DEF-USE dependence chain (insn1, ..., load-insn)
983    - test-insn is either a compare or a branch, not in the same block
984      as load-insn
985    - load-insn is reachable from test-insn
986    - there is a DEF-USE dependence chain (insn1, ..., test-insn)
987
988    This proof might fail when the compare and the load are fed
989    by an insn not in the region.  To solve this, we will add to this
990    group all loads that have no input DEF-USE dependence.
991
992    The second proof detects loads that are directly or indirectly
993    fed by a speculative load.  This proof is affected by the
994    scheduling process.  We will use the flag  fed_by_spec_load.
995    Initially, all insns have this flag reset.  After a speculative
996    motion of an insn, if insn is either a load, or marked as
997    fed_by_spec_load, we will also mark as fed_by_spec_load every
998    insn1 for which a DEF-USE dependence (insn, insn1) exists.  A
999    load which is fed_by_spec_load is also PRISKY.
1000
1001    MFREE (maybe-free) loads are all the remaining loads. They may be
1002    exception-free, but we cannot prove it.
1003
1004    Now, all loads in IFREE and PFREE classes are considered
1005    exception-free, while all loads in IRISKY and PRISKY classes are
1006    considered exception-risky.  As for loads in the MFREE class,
1007    these are considered either exception-free or exception-risky,
1008    depending on whether we are pessimistic or optimistic.  We have
1009    to take the pessimistic approach to assure the safety of
1010    speculative scheduling, but we can take the optimistic approach
1011    by invoking the -fsched_spec_load_dangerous option.  */
1012
1013 enum INSN_TRAP_CLASS
1014 {
1015   TRAP_FREE = 0, IFREE = 1, PFREE_CANDIDATE = 2,
1016   PRISKY_CANDIDATE = 3, IRISKY = 4, TRAP_RISKY = 5
1017 };
1018
1019 #define WORST_CLASS(class1, class2) \
1020 ((class1 > class2) ? class1 : class2)
1021
1022 #ifndef __GNUC__
1023 #define __inline
1024 #endif
1025
1026 #ifndef HAIFA_INLINE
1027 #define HAIFA_INLINE __inline
1028 #endif
1029
1030 struct sched_deps_info_def
1031 {
1032   /* Called when computing dependencies for a JUMP_INSN.  This function
1033      should store the set of registers that must be considered as set by
1034      the jump in the regset.  */
1035   void (*compute_jump_reg_dependencies) (rtx, regset, regset, regset);
1036
1037   /* Start analyzing insn.  */
1038   void (*start_insn) (rtx);
1039
1040   /* Finish analyzing insn.  */
1041   void (*finish_insn) (void);
1042
1043   /* Start analyzing insn LHS (Left Hand Side).  */
1044   void (*start_lhs) (rtx);
1045
1046   /* Finish analyzing insn LHS.  */
1047   void (*finish_lhs) (void);
1048
1049   /* Start analyzing insn RHS (Right Hand Side).  */
1050   void (*start_rhs) (rtx);
1051
1052   /* Finish analyzing insn RHS.  */
1053   void (*finish_rhs) (void);
1054
1055   /* Note set of the register.  */
1056   void (*note_reg_set) (int);
1057
1058   /* Note clobber of the register.  */
1059   void (*note_reg_clobber) (int);
1060
1061   /* Note use of the register.  */
1062   void (*note_reg_use) (int);
1063
1064   /* Note memory dependence of type DS between MEM1 and MEM2 (which is
1065      in the INSN2).  */
1066   void (*note_mem_dep) (rtx mem1, rtx mem2, rtx insn2, ds_t ds);
1067
1068   /* Note a dependence of type DS from the INSN.  */
1069   void (*note_dep) (rtx insn, ds_t ds);
1070
1071   /* Nonzero if we should use cselib for better alias analysis.  This
1072      must be 0 if the dependency information is used after sched_analyze
1073      has completed, e.g. if we're using it to initialize state for successor
1074      blocks in region scheduling.  */
1075   unsigned int use_cselib : 1;
1076
1077   /* If set, generate links between instruction as DEPS_LIST.
1078      Otherwise, generate usual INSN_LIST links.  */
1079   unsigned int use_deps_list : 1;
1080
1081   /* Generate data and control speculative dependencies.
1082      Requires USE_DEPS_LIST set.  */
1083   unsigned int generate_spec_deps : 1;
1084 };
1085
1086 extern struct sched_deps_info_def *sched_deps_info;
1087
1088
1089 /* Functions in sched-deps.c.  */
1090 extern bool sched_insns_conditions_mutex_p (const_rtx, const_rtx);
1091 extern bool sched_insn_is_legitimate_for_speculation_p (const_rtx, ds_t);
1092 extern void add_dependence (rtx, rtx, enum reg_note);
1093 extern void sched_analyze (struct deps *, rtx, rtx);
1094 extern void init_deps (struct deps *);
1095 extern void free_deps (struct deps *);
1096 extern void init_deps_global (void);
1097 extern void finish_deps_global (void);
1098 extern void deps_analyze_insn (struct deps *, rtx);
1099 extern void remove_from_deps (struct deps *, rtx);
1100
1101 extern dw_t get_dep_weak_1 (ds_t, ds_t);
1102 extern dw_t get_dep_weak (ds_t, ds_t);
1103 extern ds_t set_dep_weak (ds_t, ds_t, dw_t);
1104 extern dw_t estimate_dep_weak (rtx, rtx);
1105 extern ds_t ds_merge (ds_t, ds_t);
1106 extern ds_t ds_full_merge (ds_t, ds_t, rtx, rtx);
1107 extern ds_t ds_max_merge (ds_t, ds_t);
1108 extern dw_t ds_weak (ds_t);
1109 extern ds_t ds_get_speculation_types (ds_t);
1110 extern ds_t ds_get_max_dep_weak (ds_t);
1111
1112 extern void sched_deps_init (bool);
1113 extern void sched_deps_finish (void);
1114
1115 extern void haifa_note_reg_set (int);
1116 extern void haifa_note_reg_clobber (int);
1117 extern void haifa_note_reg_use (int);
1118
1119 extern void maybe_extend_reg_info_p (void);
1120
1121 extern void deps_start_bb (struct deps *, rtx);
1122 extern enum reg_note ds_to_dt (ds_t);
1123
1124 extern bool deps_pools_are_empty_p (void);
1125 extern void sched_free_deps (rtx, rtx, bool);
1126 extern void extend_dependency_caches (int, bool);
1127
1128 extern void debug_ds (ds_t);
1129
1130 /* Functions in haifa-sched.c.  */
1131 extern int haifa_classify_insn (const_rtx);
1132 extern void get_ebb_head_tail (basic_block, basic_block, rtx *, rtx *);
1133 extern int no_real_insns_p (const_rtx, const_rtx);
1134
1135 extern int insn_cost (rtx);
1136 extern int dep_cost_1 (dep_t, dw_t);
1137 extern int dep_cost (dep_t);
1138 extern int set_priorities (rtx, rtx);
1139
1140 extern void schedule_block (basic_block *);
1141
1142 extern int cycle_issued_insns;
1143 extern int issue_rate;
1144 extern int dfa_lookahead;
1145
1146 extern void ready_sort (struct ready_list *);
1147 extern rtx ready_element (struct ready_list *, int);
1148 extern rtx *ready_lastpos (struct ready_list *);
1149
1150 extern int try_ready (rtx);
1151 extern void sched_extend_ready_list (int);
1152 extern void sched_finish_ready_list (void);
1153 extern void sched_change_pattern (rtx, rtx);
1154 extern int sched_speculate_insn (rtx, ds_t, rtx *);
1155 extern void unlink_bb_notes (basic_block, basic_block);
1156 extern void add_block (basic_block, basic_block);
1157 extern rtx bb_note (basic_block);
1158 extern void concat_note_lists (rtx, rtx *);
1159 extern rtx sched_emit_insn (rtx);
1160 \f
1161
1162 /* Types and functions in sched-rgn.c.  */
1163
1164 /* A region is the main entity for interblock scheduling: insns
1165    are allowed to move between blocks in the same region, along
1166    control flow graph edges, in the 'up' direction.  */
1167 typedef struct
1168 {
1169   /* Number of extended basic blocks in region.  */
1170   int rgn_nr_blocks;
1171   /* cblocks in the region (actually index in rgn_bb_table).  */
1172   int rgn_blocks;
1173   /* Dependencies for this region are already computed.  Basically, indicates,
1174      that this is a recovery block.  */
1175   unsigned int dont_calc_deps : 1;
1176   /* This region has at least one non-trivial ebb.  */
1177   unsigned int has_real_ebb : 1;
1178 }
1179 region;
1180
1181 extern int nr_regions;
1182 extern region *rgn_table;
1183 extern int *rgn_bb_table;
1184 extern int *block_to_bb;
1185 extern int *containing_rgn;
1186
1187 #define RGN_NR_BLOCKS(rgn) (rgn_table[rgn].rgn_nr_blocks)
1188 #define RGN_BLOCKS(rgn) (rgn_table[rgn].rgn_blocks)
1189 #define RGN_DONT_CALC_DEPS(rgn) (rgn_table[rgn].dont_calc_deps)
1190 #define RGN_HAS_REAL_EBB(rgn) (rgn_table[rgn].has_real_ebb)
1191 #define BLOCK_TO_BB(block) (block_to_bb[block])
1192 #define CONTAINING_RGN(block) (containing_rgn[block])
1193
1194 /* The mapping from ebb to block.  */
1195 extern int *ebb_head;
1196 #define BB_TO_BLOCK(ebb) (rgn_bb_table[ebb_head[ebb]])
1197 #define EBB_FIRST_BB(ebb) BASIC_BLOCK (BB_TO_BLOCK (ebb))
1198 #define EBB_LAST_BB(ebb) BASIC_BLOCK (rgn_bb_table[ebb_head[ebb + 1] - 1])
1199 #define INSN_BB(INSN) (BLOCK_TO_BB (BLOCK_NUM (INSN)))
1200
1201 extern int current_nr_blocks;
1202 extern int current_blocks;
1203 extern int target_bb;
1204
1205 extern bool sched_is_disabled_for_current_region_p (void);
1206 extern void sched_rgn_init (bool);
1207 extern void sched_rgn_finish (void);
1208 extern void rgn_setup_region (int);
1209 extern void sched_rgn_compute_dependencies (int);
1210 extern void sched_rgn_local_init (int);
1211 extern void sched_rgn_local_finish (void);
1212 extern void sched_rgn_local_free (void);
1213 extern void extend_regions (void);
1214 extern void rgn_make_new_region_out_of_new_block (basic_block);
1215
1216 extern void compute_priorities (void);
1217 extern void increase_insn_priority (rtx, int);
1218 extern void debug_rgn_dependencies (int);
1219 extern void debug_dependencies (rtx, rtx);
1220 extern void free_rgn_deps (void);          
1221 extern int contributes_to_priority (rtx, rtx);
1222 extern void extend_rgns (int *, int *, sbitmap, int *);
1223 extern void deps_join (struct deps *, struct deps *);
1224
1225 extern void rgn_setup_common_sched_info (void);
1226 extern void rgn_setup_sched_infos (void);
1227
1228 extern void debug_regions (void);
1229 extern void debug_region (int);
1230 extern void dump_region_dot (FILE *, int);
1231 extern void dump_region_dot_file (const char *, int);
1232
1233 extern void haifa_sched_init (void);
1234 extern void haifa_sched_finish (void);
1235
1236 /* sched-deps.c interface to walk, add, search, update, resolve, delete
1237    and debug instruction dependencies.  */
1238
1239 /* Constants defining dependences lists.  */
1240
1241 /* No list.  */
1242 #define SD_LIST_NONE (0)
1243
1244 /* hard_back_deps.  */
1245 #define SD_LIST_HARD_BACK (1)
1246
1247 /* spec_back_deps.  */
1248 #define SD_LIST_SPEC_BACK (2)
1249
1250 /* forw_deps.  */
1251 #define SD_LIST_FORW (4)
1252
1253 /* resolved_back_deps.  */
1254 #define SD_LIST_RES_BACK (8)
1255
1256 /* resolved_forw_deps.  */
1257 #define SD_LIST_RES_FORW (16)
1258
1259 #define SD_LIST_BACK (SD_LIST_HARD_BACK | SD_LIST_SPEC_BACK)
1260
1261 /* A type to hold above flags.  */
1262 typedef int sd_list_types_def;
1263
1264 extern void sd_next_list (const_rtx, sd_list_types_def *, deps_list_t *, bool *);
1265
1266 /* Iterator to walk through, resolve and delete dependencies.  */
1267 struct _sd_iterator
1268 {
1269   /* What lists to walk.  Can be any combination of SD_LIST_* flags.  */
1270   sd_list_types_def types;
1271
1272   /* Instruction dependencies lists of which will be walked.  */
1273   rtx insn;
1274
1275   /* Pointer to the next field of the previous element.  This is not
1276      simply a pointer to the next element to allow easy deletion from the
1277      list.  When a dep is being removed from the list the iterator
1278      will automatically advance because the value in *linkp will start
1279      referring to the next element.  */
1280   dep_link_t *linkp;
1281
1282   /* True if the current list is a resolved one.  */
1283   bool resolved_p;
1284 };
1285
1286 typedef struct _sd_iterator sd_iterator_def;
1287
1288 /* ??? We can move some definitions that are used in below inline functions
1289    out of sched-int.h to sched-deps.c provided that the below functions will
1290    become global externals.
1291    These definitions include:
1292    * struct _deps_list: opaque pointer is needed at global scope.
1293    * struct _dep_link: opaque pointer is needed at scope of sd_iterator_def.
1294    * struct _dep_node: opaque pointer is needed at scope of
1295    struct _deps_link.  */
1296
1297 /* Return initialized iterator.  */
1298 static inline sd_iterator_def
1299 sd_iterator_start (rtx insn, sd_list_types_def types)
1300 {
1301   /* Some dep_link a pointer to which will return NULL.  */
1302   static dep_link_t null_link = NULL;
1303
1304   sd_iterator_def i;
1305
1306   i.types = types;
1307   i.insn = insn;
1308   i.linkp = &null_link;
1309
1310   /* Avoid 'uninitialized warning'.  */
1311   i.resolved_p = false;
1312
1313   return i;
1314 }
1315
1316 /* Return the current element.  */
1317 static inline bool
1318 sd_iterator_cond (sd_iterator_def *it_ptr, dep_t *dep_ptr)
1319 {
1320   dep_link_t link = *it_ptr->linkp;
1321
1322   if (link != NULL)
1323     {
1324       *dep_ptr = DEP_LINK_DEP (link);
1325       return true;
1326     }
1327   else
1328     {
1329       sd_list_types_def types = it_ptr->types;
1330
1331       if (types != SD_LIST_NONE)
1332         /* Switch to next list.  */
1333         {
1334           deps_list_t list;
1335
1336           sd_next_list (it_ptr->insn,
1337                         &it_ptr->types, &list, &it_ptr->resolved_p);
1338
1339           it_ptr->linkp = &DEPS_LIST_FIRST (list);
1340
1341           return sd_iterator_cond (it_ptr, dep_ptr);
1342         }
1343
1344       *dep_ptr = NULL;
1345       return false;
1346     }
1347 }
1348
1349 /* Advance iterator.  */
1350 static inline void
1351 sd_iterator_next (sd_iterator_def *it_ptr)
1352 {
1353   it_ptr->linkp = &DEP_LINK_NEXT (*it_ptr->linkp);
1354 }
1355
1356 /* A cycle wrapper.  */
1357 #define FOR_EACH_DEP(INSN, LIST_TYPES, ITER, DEP)               \
1358   for ((ITER) = sd_iterator_start ((INSN), (LIST_TYPES));       \
1359        sd_iterator_cond (&(ITER), &(DEP));                      \
1360        sd_iterator_next (&(ITER)))
1361
1362 extern int sd_lists_size (const_rtx, sd_list_types_def);
1363 extern bool sd_lists_empty_p (const_rtx, sd_list_types_def);
1364 extern void sd_init_insn (rtx);
1365 extern void sd_finish_insn (rtx);
1366 extern dep_t sd_find_dep_between (rtx, rtx, bool);
1367 extern void sd_add_dep (dep_t, bool);
1368 extern enum DEPS_ADJUST_RESULT sd_add_or_update_dep (dep_t, bool);
1369 extern void sd_resolve_dep (sd_iterator_def);
1370 extern void sd_copy_back_deps (rtx, rtx, bool);
1371 extern void sd_delete_dep (sd_iterator_def);
1372 extern void sd_debug_lists (rtx, sd_list_types_def);
1373
1374 #endif /* INSN_SCHEDULING */
1375
1376 /* Functions in sched-vis.c.  These must be outside INSN_SCHEDULING as 
1377    sched-vis.c is compiled always.  */
1378 extern void print_insn (char *, const_rtx, int);
1379 extern void print_pattern (char *, const_rtx, int);
1380 extern void print_value (char *, const_rtx, int);
1381
1382 #endif /* GCC_SCHED_INT_H */