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