OSDN Git Service

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