]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/sel-sched-ir.h
NEXT_INSN and PREV_INSN take a const rtx_insn
[thirdparty/gcc.git] / gcc / sel-sched-ir.h
CommitLineData
e855c69d
AB
1/* Instruction scheduling pass. This file contains definitions used
2 internally in the scheduler.
23a5b65a 3 Copyright (C) 2006-2014 Free Software Foundation, Inc.
e855c69d
AB
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#ifndef GCC_SEL_SCHED_IR_H
22#define GCC_SEL_SCHED_IR_H
23
24/* For state_t. */
25#include "insn-attr.h"
7a8cba34 26#include "regset.h"
e855c69d
AB
27#include "basic-block.h"
28/* For reg_note. */
29#include "rtl.h"
30#include "ggc.h"
31#include "bitmap.h"
e855c69d
AB
32#include "sched-int.h"
33#include "cfgloop.h"
34
35/* tc_t is a short for target context. This is a state of the target
36 backend. */
37typedef void *tc_t;
38
39/* List data types used for av sets, fences, paths, and boundaries. */
40
41/* Forward declarations for types that are part of some list nodes. */
42struct _list_node;
43
44/* List backend. */
45typedef struct _list_node *_list_t;
46#define _LIST_NEXT(L) ((L)->next)
47
48/* Instruction data that is part of vinsn type. */
49struct idata_def;
50typedef struct idata_def *idata_t;
51
52/* A virtual instruction, i.e. an instruction as seen by the scheduler. */
53struct vinsn_def;
54typedef struct vinsn_def *vinsn_t;
55
56/* RTX list.
57 This type is the backend for ilist. */
58typedef _list_t _xlist_t;
59#define _XLIST_X(L) ((L)->u.x)
60#define _XLIST_NEXT(L) (_LIST_NEXT (L))
61
62/* Instruction. */
6144a836 63typedef rtx_insn *insn_t;
e855c69d
AB
64
65/* List of insns. */
de8ea963
DM
66typedef _list_t ilist_t;
67#define ILIST_INSN(L) ((L)->u.insn)
68#define ILIST_NEXT(L) (_LIST_NEXT (L))
e855c69d 69
b8698a0f 70/* This lists possible transformations that done locally, i.e. in
e855c69d
AB
71 moveup_expr. */
72enum local_trans_type
73 {
74 TRANS_SUBSTITUTION,
75 TRANS_SPECULATION
76 };
77
b8698a0f 78/* This struct is used to record the history of expression's
e855c69d
AB
79 transformations. */
80struct expr_history_def_1
81{
82 /* UID of the insn. */
83 unsigned uid;
84
85 /* How the expression looked like. */
86 vinsn_t old_expr_vinsn;
87
88 /* How the expression looks after the transformation. */
89 vinsn_t new_expr_vinsn;
90
91 /* And its speculative status. */
92 ds_t spec_ds;
93
94 /* Type of the transformation. */
95 enum local_trans_type type;
96};
97
98typedef struct expr_history_def_1 expr_history_def;
99
e855c69d
AB
100
101/* Expression information. */
102struct _expr
103{
104 /* Insn description. */
105 vinsn_t vinsn;
106
107 /* SPEC is the degree of speculativeness.
108 FIXME: now spec is increased when an rhs is moved through a
109 conditional, thus showing only control speculativeness. In the
110 future we'd like to count data spec separately to allow a better
111 control on scheduling. */
112 int spec;
113
b8698a0f
L
114 /* Degree of speculativeness measured as probability of executing
115 instruction's original basic block given relative to
e855c69d
AB
116 the current scheduling point. */
117 int usefulness;
118
119 /* A priority of this expression. */
120 int priority;
121
122 /* A priority adjustment of this expression. */
123 int priority_adj;
124
125 /* Number of times the insn was scheduled. */
126 int sched_times;
127
b8698a0f 128 /* A basic block index this was originated from. Zero when there is
e855c69d
AB
129 more than one originator. */
130 int orig_bb_index;
131
132 /* Instruction should be of SPEC_DONE_DS type in order to be moved to this
133 point. */
134 ds_t spec_done_ds;
135
136 /* SPEC_TO_CHECK_DS hold speculation types that should be checked
137 (used only during move_op ()). */
138 ds_t spec_to_check_ds;
139
b8698a0f 140 /* Cycle on which original insn was scheduled. Zero when it has not yet
e855c69d
AB
141 been scheduled or more than one originator. */
142 int orig_sched_cycle;
143
144 /* This vector contains the history of insn's transformations. */
9771b263 145 vec<expr_history_def> history_of_changes;
e855c69d 146
b8698a0f 147 /* True (1) when original target (register or memory) of this instruction
e855c69d
AB
148 is available for scheduling, false otherwise. -1 means we're not sure;
149 please run find_used_regs to clarify. */
150 signed char target_available;
151
b8698a0f 152 /* True when this expression needs a speculation check to be scheduled.
e855c69d
AB
153 This is used during find_used_regs. */
154 BOOL_BITFIELD needs_spec_check_p : 1;
155
b8698a0f 156 /* True when the expression was substituted. Used for statistical
e855c69d
AB
157 purposes. */
158 BOOL_BITFIELD was_substituted : 1;
159
160 /* True when the expression was renamed. */
161 BOOL_BITFIELD was_renamed : 1;
162
163 /* True when expression can't be moved. */
164 BOOL_BITFIELD cant_move : 1;
165};
166
167typedef struct _expr expr_def;
168typedef expr_def *expr_t;
169
170#define EXPR_VINSN(EXPR) ((EXPR)->vinsn)
171#define EXPR_INSN_RTX(EXPR) (VINSN_INSN_RTX (EXPR_VINSN (EXPR)))
172#define EXPR_PATTERN(EXPR) (VINSN_PATTERN (EXPR_VINSN (EXPR)))
173#define EXPR_LHS(EXPR) (VINSN_LHS (EXPR_VINSN (EXPR)))
174#define EXPR_RHS(EXPR) (VINSN_RHS (EXPR_VINSN (EXPR)))
175#define EXPR_TYPE(EXPR) (VINSN_TYPE (EXPR_VINSN (EXPR)))
176#define EXPR_SEPARABLE_P(EXPR) (VINSN_SEPARABLE_P (EXPR_VINSN (EXPR)))
177
178#define EXPR_SPEC(EXPR) ((EXPR)->spec)
179#define EXPR_USEFULNESS(EXPR) ((EXPR)->usefulness)
180#define EXPR_PRIORITY(EXPR) ((EXPR)->priority)
181#define EXPR_PRIORITY_ADJ(EXPR) ((EXPR)->priority_adj)
182#define EXPR_SCHED_TIMES(EXPR) ((EXPR)->sched_times)
183#define EXPR_ORIG_BB_INDEX(EXPR) ((EXPR)->orig_bb_index)
184#define EXPR_ORIG_SCHED_CYCLE(EXPR) ((EXPR)->orig_sched_cycle)
185#define EXPR_SPEC_DONE_DS(EXPR) ((EXPR)->spec_done_ds)
186#define EXPR_SPEC_TO_CHECK_DS(EXPR) ((EXPR)->spec_to_check_ds)
187#define EXPR_HISTORY_OF_CHANGES(EXPR) ((EXPR)->history_of_changes)
188#define EXPR_TARGET_AVAILABLE(EXPR) ((EXPR)->target_available)
189#define EXPR_NEEDS_SPEC_CHECK_P(EXPR) ((EXPR)->needs_spec_check_p)
190#define EXPR_WAS_SUBSTITUTED(EXPR) ((EXPR)->was_substituted)
191#define EXPR_WAS_RENAMED(EXPR) ((EXPR)->was_renamed)
192#define EXPR_CANT_MOVE(EXPR) ((EXPR)->cant_move)
193
e855c69d
AB
194/* Insn definition for list of original insns in find_used_regs. */
195struct _def
196{
197 insn_t orig_insn;
198
199 /* FIXME: Get rid of CROSSES_CALL in each def, since if we're moving up
200 rhs from two different places, but only one of the code motion paths
b8698a0f 201 crosses a call, we can't use any of the call_used_regs, no matter which
e855c69d
AB
202 path or whether all paths crosses a call. Thus we should move CROSSES_CALL
203 to static params. */
204 bool crosses_call;
205};
206typedef struct _def *def_t;
207
208
209/* Availability sets are sets of expressions we're scheduling. */
210typedef _list_t av_set_t;
211#define _AV_SET_EXPR(L) (&(L)->u.expr)
212#define _AV_SET_NEXT(L) (_LIST_NEXT (L))
213
214
215/* Boundary of the current fence group. */
216struct _bnd
217{
218 /* The actual boundary instruction. */
219 insn_t to;
220
221 /* Its path to the fence. */
222 ilist_t ptr;
223
224 /* Availability set at the boundary. */
225 av_set_t av;
226
227 /* This set moved to the fence. */
228 av_set_t av1;
b8698a0f 229
e855c69d
AB
230 /* Deps context at this boundary. As long as we have one boundary per fence,
231 this is just a pointer to the same deps context as in the corresponding
232 fence. */
233 deps_t dc;
234};
235typedef struct _bnd *bnd_t;
6144a836 236#define BND_TO(B) ((B)->to)
e855c69d
AB
237
238/* PTR stands not for pointer as you might think, but as a Path To Root of the
239 current instruction group from boundary B. */
240#define BND_PTR(B) ((B)->ptr)
241#define BND_AV(B) ((B)->av)
242#define BND_AV1(B) ((B)->av1)
243#define BND_DC(B) ((B)->dc)
244
245/* List of boundaries. */
246typedef _list_t blist_t;
247#define BLIST_BND(L) (&(L)->u.bnd)
248#define BLIST_NEXT(L) (_LIST_NEXT (L))
249
250
251/* Fence information. A fence represents current scheduling point and also
252 blocks code motion through it when pipelining. */
253struct _fence
254{
255 /* Insn before which we gather an instruction group.*/
256 insn_t insn;
257
258 /* Modeled state of the processor pipeline. */
259 state_t state;
260
261 /* Current cycle that is being scheduled on this fence. */
262 int cycle;
263
264 /* Number of insns that were scheduled on the current cycle.
265 This information has to be local to a fence. */
266 int cycle_issued_insns;
267
268 /* At the end of fill_insns () this field holds the list of the instructions
269 that are inner boundaries of the scheduled parallel group. */
270 ilist_t bnds;
271
272 /* Deps context at this fence. It is used to model dependencies at the
273 fence so that insn ticks can be properly evaluated. */
274 deps_t dc;
275
276 /* Target context at this fence. Used to save and load any local target
277 scheduling information when changing fences. */
278 tc_t tc;
279
280 /* A vector of insns that are scheduled but not yet completed. */
6144a836 281 vec<rtx_insn *, va_gc> *executing_insns;
e855c69d 282
b8698a0f 283 /* A vector indexed by UIDs that caches the earliest cycle on which
e855c69d
AB
284 an insn can be scheduled on this fence. */
285 int *ready_ticks;
286
287 /* Its size. */
288 int ready_ticks_size;
289
290 /* Insn, which has been scheduled last on this fence. */
6144a836 291 rtx_insn *last_scheduled_insn;
e855c69d 292
136e01a3
AB
293 /* The last value of can_issue_more variable on this fence. */
294 int issue_more;
295
e855c69d 296 /* If non-NULL force the next scheduled insn to be SCHED_NEXT. */
6144a836 297 rtx_insn *sched_next;
e855c69d
AB
298
299 /* True if fill_insns processed this fence. */
300 BOOL_BITFIELD processed_p : 1;
301
302 /* True if fill_insns actually scheduled something on this fence. */
303 BOOL_BITFIELD scheduled_p : 1;
304
305 /* True when the next insn scheduled here would start a cycle. */
306 BOOL_BITFIELD starts_cycle_p : 1;
307
308 /* True when the next insn scheduled here would be scheduled after a stall. */
309 BOOL_BITFIELD after_stall_p : 1;
310};
311typedef struct _fence *fence_t;
312
313#define FENCE_INSN(F) ((F)->insn)
314#define FENCE_STATE(F) ((F)->state)
315#define FENCE_BNDS(F) ((F)->bnds)
316#define FENCE_PROCESSED_P(F) ((F)->processed_p)
317#define FENCE_SCHEDULED_P(F) ((F)->scheduled_p)
318#define FENCE_ISSUED_INSNS(F) ((F)->cycle_issued_insns)
319#define FENCE_CYCLE(F) ((F)->cycle)
320#define FENCE_STARTS_CYCLE_P(F) ((F)->starts_cycle_p)
321#define FENCE_AFTER_STALL_P(F) ((F)->after_stall_p)
322#define FENCE_DC(F) ((F)->dc)
323#define FENCE_TC(F) ((F)->tc)
324#define FENCE_LAST_SCHEDULED_INSN(F) ((F)->last_scheduled_insn)
136e01a3 325#define FENCE_ISSUE_MORE(F) ((F)->issue_more)
e855c69d
AB
326#define FENCE_EXECUTING_INSNS(F) ((F)->executing_insns)
327#define FENCE_READY_TICKS(F) ((F)->ready_ticks)
328#define FENCE_READY_TICKS_SIZE(F) ((F)->ready_ticks_size)
329#define FENCE_SCHED_NEXT(F) ((F)->sched_next)
330
331/* List of fences. */
332typedef _list_t flist_t;
333#define FLIST_FENCE(L) (&(L)->u.fence)
334#define FLIST_NEXT(L) (_LIST_NEXT (L))
335
336/* List of fences with pointer to the tail node. */
337struct flist_tail_def
338{
339 flist_t head;
340 flist_t *tailp;
341};
342
343typedef struct flist_tail_def *flist_tail_t;
344#define FLIST_TAIL_HEAD(L) ((L)->head)
345#define FLIST_TAIL_TAILP(L) ((L)->tailp)
346
347/* List node information. A list node can be any of the types above. */
348struct _list_node
349{
350 _list_t next;
351
352 union
353 {
354 rtx x;
de8ea963 355 insn_t insn;
e855c69d
AB
356 struct _bnd bnd;
357 expr_def expr;
358 struct _fence fence;
359 struct _def def;
360 void *data;
361 } u;
362};
363\f
364
365/* _list_t functions.
366 All of _*list_* functions are used through accessor macros, thus
367 we can't move them in sel-sched-ir.c. */
368extern alloc_pool sched_lists_pool;
369
370static inline _list_t
371_list_alloc (void)
372{
373 return (_list_t) pool_alloc (sched_lists_pool);
374}
375
376static inline void
377_list_add (_list_t *lp)
378{
379 _list_t l = _list_alloc ();
380
381 _LIST_NEXT (l) = *lp;
382 *lp = l;
383}
384
385static inline void
386_list_remove_nofree (_list_t *lp)
387{
388 _list_t n = *lp;
389
390 *lp = _LIST_NEXT (n);
391}
392
393static inline void
394_list_remove (_list_t *lp)
395{
396 _list_t n = *lp;
397
398 *lp = _LIST_NEXT (n);
399 pool_free (sched_lists_pool, n);
400}
401
402static inline void
403_list_clear (_list_t *l)
404{
405 while (*l)
406 _list_remove (l);
407}
408\f
409
410/* List iterator backend. */
84562394 411struct _list_iterator
e855c69d
AB
412{
413 /* The list we're iterating. */
414 _list_t *lp;
415
416 /* True when this iterator supprts removing. */
417 bool can_remove_p;
418
419 /* True when we've actually removed something. */
420 bool removed_p;
84562394 421};
e855c69d
AB
422
423static inline void
424_list_iter_start (_list_iterator *ip, _list_t *lp, bool can_remove_p)
425{
426 ip->lp = lp;
427 ip->can_remove_p = can_remove_p;
428 ip->removed_p = false;
429}
430
431static inline void
432_list_iter_next (_list_iterator *ip)
433{
434 if (!ip->removed_p)
435 ip->lp = &_LIST_NEXT (*ip->lp);
436 else
437 ip->removed_p = false;
438}
439
440static inline void
441_list_iter_remove (_list_iterator *ip)
442{
443 gcc_assert (!ip->removed_p && ip->can_remove_p);
444 _list_remove (ip->lp);
445 ip->removed_p = true;
446}
447
448static inline void
449_list_iter_remove_nofree (_list_iterator *ip)
450{
451 gcc_assert (!ip->removed_p && ip->can_remove_p);
452 _list_remove_nofree (ip->lp);
453 ip->removed_p = true;
454}
455
456/* General macros to traverse a list. FOR_EACH_* interfaces are
457 implemented using these. */
458#define _FOR_EACH(TYPE, ELEM, I, L) \
459 for (_list_iter_start (&(I), &(L), false); \
460 _list_iter_cond_##TYPE (*(I).lp, &(ELEM)); \
461 _list_iter_next (&(I)))
462
463#define _FOR_EACH_1(TYPE, ELEM, I, LP) \
464 for (_list_iter_start (&(I), (LP), true); \
465 _list_iter_cond_##TYPE (*(I).lp, &(ELEM)); \
b8698a0f 466 _list_iter_next (&(I)))
e855c69d
AB
467\f
468
469/* _xlist_t functions. */
470
471static inline void
472_xlist_add (_xlist_t *lp, rtx x)
473{
474 _list_add (lp);
475 _XLIST_X (*lp) = x;
476}
477
478#define _xlist_remove(LP) (_list_remove (LP))
479#define _xlist_clear(LP) (_list_clear (LP))
480
481static inline bool
482_xlist_is_in_p (_xlist_t l, rtx x)
483{
484 while (l)
485 {
486 if (_XLIST_X (l) == x)
487 return true;
488 l = _XLIST_NEXT (l);
489 }
490
491 return false;
492}
493
494/* Used through _FOR_EACH. */
495static inline bool
496_list_iter_cond_x (_xlist_t l, rtx *xp)
497{
498 if (l)
499 {
500 *xp = _XLIST_X (l);
501 return true;
502 }
503
504 return false;
505}
506
507#define _xlist_iter_remove(IP) (_list_iter_remove (IP))
508
509typedef _list_iterator _xlist_iterator;
510#define _FOR_EACH_X(X, I, L) _FOR_EACH (x, (X), (I), (L))
511#define _FOR_EACH_X_1(X, I, LP) _FOR_EACH_1 (x, (X), (I), (LP))
512\f
513
de8ea963 514/* ilist_t functions. */
e855c69d 515
de8ea963
DM
516static inline void
517ilist_add (ilist_t *lp, insn_t insn)
518{
519 _list_add (lp);
520 ILIST_INSN (*lp) = insn;
521}
522#define ilist_remove(LP) (_list_remove (LP))
523#define ilist_clear(LP) (_list_clear (LP))
524
525static inline bool
526ilist_is_in_p (ilist_t l, insn_t insn)
527{
528 while (l)
529 {
530 if (ILIST_INSN (l) == insn)
531 return true;
532 l = ILIST_NEXT (l);
533 }
534
535 return false;
536}
537
538/* Used through _FOR_EACH. */
539static inline bool
540_list_iter_cond_insn (ilist_t l, insn_t *ip)
541{
542 if (l)
543 {
544 *ip = ILIST_INSN (l);
545 return true;
546 }
547
548 return false;
549}
550
551#define ilist_iter_remove(IP) (_list_iter_remove (IP))
e855c69d 552
de8ea963
DM
553typedef _list_iterator ilist_iterator;
554#define FOR_EACH_INSN(INSN, I, L) _FOR_EACH (insn, (INSN), (I), (L))
555#define FOR_EACH_INSN_1(INSN, I, LP) _FOR_EACH_1 (insn, (INSN), (I), (LP))
e855c69d
AB
556\f
557
558/* Av set iterators. */
559typedef _list_iterator av_set_iterator;
560#define FOR_EACH_EXPR(EXPR, I, AV) _FOR_EACH (expr, (EXPR), (I), (AV))
561#define FOR_EACH_EXPR_1(EXPR, I, AV) _FOR_EACH_1 (expr, (EXPR), (I), (AV))
562
7b3b6ae4 563inline bool
e855c69d
AB
564_list_iter_cond_expr (av_set_t av, expr_t *exprp)
565{
566 if (av)
567 {
568 *exprp = _AV_SET_EXPR (av);
569 return true;
570 }
571
572 return false;
573}
574\f
575
576/* Def list iterators. */
577typedef _list_t def_list_t;
578typedef _list_iterator def_list_iterator;
579
580#define DEF_LIST_NEXT(L) (_LIST_NEXT (L))
581#define DEF_LIST_DEF(L) (&(L)->u.def)
582
583#define FOR_EACH_DEF(DEF, I, DEF_LIST) _FOR_EACH (def, (DEF), (I), (DEF_LIST))
584
585static inline bool
586_list_iter_cond_def (def_list_t def_list, def_t *def)
587{
588 if (def_list)
589 {
590 *def = DEF_LIST_DEF (def_list);
591 return true;
592 }
593
594 return false;
595}
596\f
597
598/* InstructionData. Contains information about insn pattern. */
599struct idata_def
600{
601 /* Type of the insn.
602 o CALL_INSN - Call insn
603 o JUMP_INSN - Jump insn
604 o INSN - INSN that cannot be cloned
605 o USE - INSN that can be cloned
606 o SET - INSN that can be cloned and separable into lhs and rhs
607 o PC - simplejump. Insns that simply redirect control flow should not
608 have any dependencies. Sched-deps.c, though, might consider them as
609 producers or consumers of certain registers. To avoid that we handle
610 dependency for simple jumps ourselves. */
611 int type;
612
613 /* If insn is a SET, this is its left hand side. */
614 rtx lhs;
615
616 /* If insn is a SET, this is its right hand side. */
617 rtx rhs;
618
619 /* Registers that are set/used by this insn. This info is now gathered
620 via sched-deps.c. The downside of this is that we also use live info
621 from flow that is accumulated in the basic blocks. These two infos
622 can be slightly inconsistent, hence in the beginning we make a pass
623 through CFG and calculating the conservative solution for the info in
624 basic blocks. When this scheduler will be switched to use dataflow,
625 this can be unified as df gives us both per basic block and per
626 instruction info. Actually, we don't do that pass and just hope
627 for the best. */
628 regset reg_sets;
629
630 regset reg_clobbers;
631
632 regset reg_uses;
633};
634
635#define IDATA_TYPE(ID) ((ID)->type)
636#define IDATA_LHS(ID) ((ID)->lhs)
637#define IDATA_RHS(ID) ((ID)->rhs)
638#define IDATA_REG_SETS(ID) ((ID)->reg_sets)
639#define IDATA_REG_USES(ID) ((ID)->reg_uses)
640#define IDATA_REG_CLOBBERS(ID) ((ID)->reg_clobbers)
641
642/* Type to represent all needed info to emit an insn.
643 This is a virtual equivalent of the insn.
644 Every insn in the stream has an associated vinsn. This is used
645 to reduce memory consumption basing on the fact that many insns
646 don't change through the scheduler.
647
648 vinsn can be either normal or unique.
649 * Normal vinsn is the one, that can be cloned multiple times and typically
650 corresponds to normal instruction.
651
652 * Unique vinsn derivates from CALL, ASM, JUMP (for a while) and other
653 unusual stuff. Such a vinsn is described by its INSN field, which is a
654 reference to the original instruction. */
655struct vinsn_def
656{
657 /* Associated insn. */
3d30f4e8 658 rtx_insn *insn_rtx;
e855c69d
AB
659
660 /* Its description. */
661 struct idata_def id;
662
663 /* Hash of vinsn. It is computed either from pattern or from rhs using
664 hash_rtx. It is not placed in ID for faster compares. */
665 unsigned hash;
666
667 /* Hash of the insn_rtx pattern. */
668 unsigned hash_rtx;
669
670 /* Smart pointer counter. */
671 int count;
672
673 /* Cached cost of the vinsn. To access it please use vinsn_cost (). */
674 int cost;
675
676 /* Mark insns that may trap so we don't move them through jumps. */
677 bool may_trap_p;
678};
679
3d30f4e8 680#define VINSN_INSN_RTX(VI) ((VI)->insn_rtx)
e855c69d
AB
681#define VINSN_PATTERN(VI) (PATTERN (VINSN_INSN_RTX (VI)))
682
683#define VINSN_ID(VI) (&((VI)->id))
684#define VINSN_HASH(VI) ((VI)->hash)
685#define VINSN_HASH_RTX(VI) ((VI)->hash_rtx)
686#define VINSN_TYPE(VI) (IDATA_TYPE (VINSN_ID (VI)))
687#define VINSN_SEPARABLE_P(VI) (VINSN_TYPE (VI) == SET)
688#define VINSN_CLONABLE_P(VI) (VINSN_SEPARABLE_P (VI) || VINSN_TYPE (VI) == USE)
689#define VINSN_UNIQUE_P(VI) (!VINSN_CLONABLE_P (VI))
690#define VINSN_LHS(VI) (IDATA_LHS (VINSN_ID (VI)))
691#define VINSN_RHS(VI) (IDATA_RHS (VINSN_ID (VI)))
692#define VINSN_REG_SETS(VI) (IDATA_REG_SETS (VINSN_ID (VI)))
693#define VINSN_REG_USES(VI) (IDATA_REG_USES (VINSN_ID (VI)))
694#define VINSN_REG_CLOBBERS(VI) (IDATA_REG_CLOBBERS (VINSN_ID (VI)))
695#define VINSN_COUNT(VI) ((VI)->count)
696#define VINSN_MAY_TRAP_P(VI) ((VI)->may_trap_p)
697\f
698
b8698a0f 699/* An entry of the hashtable describing transformations happened when
e855c69d
AB
700 moving up through an insn. */
701struct transformed_insns
702{
703 /* Previous vinsn. Used to find the proper element. */
704 vinsn_t vinsn_old;
705
706 /* A new vinsn. */
707 vinsn_t vinsn_new;
708
709 /* Speculative status. */
710 ds_t ds;
711
712 /* Type of transformation happened. */
713 enum local_trans_type type;
714
715 /* Whether a conflict on the target register happened. */
716 BOOL_BITFIELD was_target_conflict : 1;
717
718 /* Whether a check was needed. */
719 BOOL_BITFIELD needs_check : 1;
720};
721
722/* Indexed by INSN_LUID, the collection of all data associated with
723 a single instruction that is in the stream. */
724struct _sel_insn_data
725{
726 /* The expression that contains vinsn for this insn and some
727 flow-sensitive data like priority. */
728 expr_def expr;
729
730 /* If (WS_LEVEL == GLOBAL_LEVEL) then AV is empty. */
731 int ws_level;
732
733 /* A number that helps in defining a traversing order for a region. */
734 int seqno;
735
736 /* A liveness data computed above this insn. */
737 regset live;
738
739 /* An INSN_UID bit is set when deps analysis result is already known. */
740 bitmap analyzed_deps;
741
b8698a0f 742 /* An INSN_UID bit is set when a hard dep was found, not set when
e855c69d
AB
743 no dependence is found. This is meaningful only when the analyzed_deps
744 bitmap has its bit set. */
745 bitmap found_deps;
746
b8698a0f 747 /* An INSN_UID bit is set when this is a bookkeeping insn generated from
14f30b87
AM
748 a parent with this uid. If a parent is a bookkeeping copy, all its
749 originators are transitively included in this set. */
e855c69d
AB
750 bitmap originators;
751
752 /* A hashtable caching the result of insn transformations through this one. */
753 htab_t transformed_insns;
b8698a0f 754
e855c69d 755 /* A context incapsulating this insn. */
88302d54 756 struct deps_desc deps_context;
e855c69d
AB
757
758 /* This field is initialized at the beginning of scheduling and is used
759 to handle sched group instructions. If it is non-null, then it points
760 to the instruction, which should be forced to schedule next. Such
761 instructions are unique. */
762 insn_t sched_next;
763
764 /* Cycle at which insn was scheduled. It is greater than zero if insn was
765 scheduled. This is used for bundling. */
766 int sched_cycle;
767
768 /* Cycle at which insn's data will be fully ready. */
769 int ready_cycle;
770
771 /* Speculations that are being checked by this insn. */
772 ds_t spec_checked_ds;
773
774 /* Whether the live set valid or not. */
775 BOOL_BITFIELD live_valid_p : 1;
776 /* Insn is an ASM. */
777 BOOL_BITFIELD asm_p : 1;
778
779 /* True when an insn is scheduled after we've determined that a stall is
780 required.
781 This is used when emulating the Haifa scheduler for bundling. */
782 BOOL_BITFIELD after_stall_p : 1;
783};
784
785typedef struct _sel_insn_data sel_insn_data_def;
786typedef sel_insn_data_def *sel_insn_data_t;
787
9771b263 788extern vec<sel_insn_data_def> s_i_d;
e855c69d
AB
789
790/* Accessor macros for s_i_d. */
9771b263
DN
791#define SID(INSN) (&s_i_d[INSN_LUID (INSN)])
792#define SID_BY_UID(UID) (&s_i_d[LUID_BY_UID (UID)])
e855c69d
AB
793
794extern sel_insn_data_def insn_sid (insn_t);
795
796#define INSN_ASM_P(INSN) (SID (INSN)->asm_p)
797#define INSN_SCHED_NEXT(INSN) (SID (INSN)->sched_next)
798#define INSN_ANALYZED_DEPS(INSN) (SID (INSN)->analyzed_deps)
b8698a0f
L
799#define INSN_FOUND_DEPS(INSN) (SID (INSN)->found_deps)
800#define INSN_DEPS_CONTEXT(INSN) (SID (INSN)->deps_context)
e855c69d
AB
801#define INSN_ORIGINATORS(INSN) (SID (INSN)->originators)
802#define INSN_ORIGINATORS_BY_UID(UID) (SID_BY_UID (UID)->originators)
803#define INSN_TRANSFORMED_INSNS(INSN) (SID (INSN)->transformed_insns)
804
805#define INSN_EXPR(INSN) (&SID (INSN)->expr)
806#define INSN_LIVE(INSN) (SID (INSN)->live)
807#define INSN_LIVE_VALID_P(INSN) (SID (INSN)->live_valid_p)
808#define INSN_VINSN(INSN) (EXPR_VINSN (INSN_EXPR (INSN)))
809#define INSN_TYPE(INSN) (VINSN_TYPE (INSN_VINSN (INSN)))
810#define INSN_SIMPLEJUMP_P(INSN) (INSN_TYPE (INSN) == PC)
811#define INSN_LHS(INSN) (VINSN_LHS (INSN_VINSN (INSN)))
812#define INSN_RHS(INSN) (VINSN_RHS (INSN_VINSN (INSN)))
813#define INSN_REG_SETS(INSN) (VINSN_REG_SETS (INSN_VINSN (INSN)))
814#define INSN_REG_CLOBBERS(INSN) (VINSN_REG_CLOBBERS (INSN_VINSN (INSN)))
815#define INSN_REG_USES(INSN) (VINSN_REG_USES (INSN_VINSN (INSN)))
816#define INSN_SCHED_TIMES(INSN) (EXPR_SCHED_TIMES (INSN_EXPR (INSN)))
817#define INSN_SEQNO(INSN) (SID (INSN)->seqno)
818#define INSN_AFTER_STALL_P(INSN) (SID (INSN)->after_stall_p)
819#define INSN_SCHED_CYCLE(INSN) (SID (INSN)->sched_cycle)
820#define INSN_READY_CYCLE(INSN) (SID (INSN)->ready_cycle)
821#define INSN_SPEC_CHECKED_DS(INSN) (SID (INSN)->spec_checked_ds)
822
823/* A global level shows whether an insn is valid or not. */
824extern int global_level;
825
826#define INSN_WS_LEVEL(INSN) (SID (INSN)->ws_level)
827
828extern av_set_t get_av_set (insn_t);
829extern int get_av_level (insn_t);
830
831#define AV_SET(INSN) (get_av_set (INSN))
832#define AV_LEVEL(INSN) (get_av_level (INSN))
833#define AV_SET_VALID_P(INSN) (AV_LEVEL (INSN) == global_level)
834
835/* A list of fences currently in the works. */
836extern flist_t fences;
837
838/* A NOP pattern used as a placeholder for real insns. */
839extern rtx nop_pattern;
840
841/* An insn that 'contained' in EXIT block. */
c5db5458 842extern rtx_insn *exit_insn;
e855c69d
AB
843
844/* Provide a separate luid for the insn. */
845#define INSN_INIT_TODO_LUID (1)
846
847/* Initialize s_s_i_d. */
848#define INSN_INIT_TODO_SSID (2)
849
850/* Initialize data for simplejump. */
851#define INSN_INIT_TODO_SIMPLEJUMP (4)
852
853/* Return true if INSN is a local NOP. The nop is local in the sense that
854 it was emitted by the scheduler as a temporary insn and will soon be
855 deleted. These nops are identified by their pattern. */
856#define INSN_NOP_P(INSN) (PATTERN (INSN) == nop_pattern)
857
858/* Return true if INSN is linked into instruction stream.
859 NB: It is impossible for INSN to have one field null and the other not
860 null: gcc_assert ((PREV_INSN (INSN) == NULL_RTX)
861 == (NEXT_INSN (INSN) == NULL_RTX)) is valid. */
862#define INSN_IN_STREAM_P(INSN) (PREV_INSN (INSN) && NEXT_INSN (INSN))
863
864/* Return true if INSN is in current fence. */
865#define IN_CURRENT_FENCE_P(INSN) (flist_lookup (fences, INSN) != NULL)
866
867/* Marks loop as being considered for pipelining. */
868#define MARK_LOOP_FOR_PIPELINING(LOOP) ((LOOP)->aux = (void *)(size_t)(1))
869#define LOOP_MARKED_FOR_PIPELINING_P(LOOP) ((size_t)((LOOP)->aux))
870
871/* Saved loop preheader to transfer when scheduling the loop. */
872#define LOOP_PREHEADER_BLOCKS(LOOP) ((size_t)((LOOP)->aux) == 1 \
873 ? NULL \
9771b263 874 : ((vec<basic_block> *) (LOOP)->aux))
e855c69d
AB
875#define SET_LOOP_PREHEADER_BLOCKS(LOOP,BLOCKS) ((LOOP)->aux \
876 = (BLOCKS != NULL \
877 ? BLOCKS \
878 : (LOOP)->aux))
879
880extern bitmap blocks_to_reschedule;
881\f
882
883/* A variable to track which part of rtx we are scanning in
884 sched-deps.c: sched_analyze_insn (). */
84562394
OE
885enum deps_where_t
886{
887 DEPS_IN_INSN,
888 DEPS_IN_LHS,
889 DEPS_IN_RHS,
890 DEPS_IN_NOWHERE
891};
e855c69d
AB
892\f
893
894/* Per basic block data for the whole CFG. */
84562394 895struct sel_global_bb_info_def
e855c69d
AB
896{
897 /* For each bb header this field contains a set of live registers.
898 For all other insns this field has a NULL.
073a8998 899 We also need to know LV sets for the instructions, that are immediately
e855c69d
AB
900 after the border of the region. */
901 regset lv_set;
902
903 /* Status of LV_SET.
904 true - block has usable LV_SET.
905 false - block's LV_SET should be recomputed. */
906 bool lv_set_valid_p;
84562394 907};
e855c69d
AB
908
909typedef sel_global_bb_info_def *sel_global_bb_info_t;
910
e855c69d
AB
911
912/* Per basic block data. This array is indexed by basic block index. */
9771b263 913extern vec<sel_global_bb_info_def> sel_global_bb_info;
e855c69d
AB
914
915extern void sel_extend_global_bb_info (void);
916extern void sel_finish_global_bb_info (void);
917
918/* Get data for BB. */
919#define SEL_GLOBAL_BB_INFO(BB) \
9771b263 920 (&sel_global_bb_info[(BB)->index])
e855c69d
AB
921
922/* Access macros. */
923#define BB_LV_SET(BB) (SEL_GLOBAL_BB_INFO (BB)->lv_set)
924#define BB_LV_SET_VALID_P(BB) (SEL_GLOBAL_BB_INFO (BB)->lv_set_valid_p)
925
926/* Per basic block data for the region. */
84562394 927struct sel_region_bb_info_def
e855c69d
AB
928{
929 /* This insn stream is constructed in such a way that it should be
930 traversed by PREV_INSN field - (*not* NEXT_INSN). */
b311fd0f 931 rtx_insn *note_list;
e855c69d
AB
932
933 /* Cached availability set at the beginning of a block.
934 See also AV_LEVEL () for conditions when this av_set can be used. */
935 av_set_t av_set;
936
937 /* If (AV_LEVEL == GLOBAL_LEVEL) then AV is valid. */
938 int av_level;
84562394 939};
e855c69d
AB
940
941typedef sel_region_bb_info_def *sel_region_bb_info_t;
942
e855c69d
AB
943
944/* Per basic block data. This array is indexed by basic block index. */
9771b263 945extern vec<sel_region_bb_info_def> sel_region_bb_info;
e855c69d
AB
946
947/* Get data for BB. */
9771b263 948#define SEL_REGION_BB_INFO(BB) (&sel_region_bb_info[(BB)->index])
e855c69d
AB
949
950/* Get BB's note_list.
951 A note_list is a list of various notes that was scattered across BB
952 before scheduling, and will be appended at the beginning of BB after
953 scheduling is finished. */
b311fd0f 954#define BB_NOTE_LIST(BB) (SEL_REGION_BB_INFO (BB)->note_list)
e855c69d
AB
955
956#define BB_AV_SET(BB) (SEL_REGION_BB_INFO (BB)->av_set)
957#define BB_AV_LEVEL(BB) (SEL_REGION_BB_INFO (BB)->av_level)
958#define BB_AV_SET_VALID_P(BB) (BB_AV_LEVEL (BB) == global_level)
959
960/* Used in bb_in_ebb_p. */
961extern bitmap_head *forced_ebb_heads;
962
963/* The loop nest being pipelined. */
964extern struct loop *current_loop_nest;
965
966/* Saves pipelined blocks. Bitmap is indexed by bb->index. */
967extern sbitmap bbs_pipelined;
968
969/* Various flags. */
970extern bool enable_moveup_set_path_p;
971extern bool pipelining_p;
972extern bool bookkeeping_p;
b8698a0f 973extern int max_insns_to_rename;
e855c69d
AB
974extern bool preheader_removed;
975
976/* Software lookahead window size.
b8698a0f 977 According to the results in Nakatani and Ebcioglu [1993], window size of 16
e855c69d
AB
978 is enough to extract most ILP in integer code. */
979#define MAX_WS (PARAM_VALUE (PARAM_SELSCHED_MAX_LOOKAHEAD))
980
981extern regset sel_all_regs;
982\f
983
984/* Successor iterator backend. */
84562394 985struct succ_iterator
e855c69d
AB
986{
987 /* True if we're at BB end. */
988 bool bb_end;
989
990 /* An edge on which we're iterating. */
991 edge e1;
992
993 /* The previous edge saved after skipping empty blocks. */
994 edge e2;
b8698a0f 995
e855c69d
AB
996 /* Edge iterator used when there are successors in other basic blocks. */
997 edge_iterator ei;
998
999 /* Successor block we're traversing. */
1000 basic_block bb;
1001
1002 /* Flags that are passed to the iterator. We return only successors
1003 that comply to these flags. */
1004 short flags;
b8698a0f
L
1005
1006 /* When flags include SUCCS_ALL, this will be set to the exact type
073a8998 1007 of the successor we're traversing now. */
e855c69d
AB
1008 short current_flags;
1009
1010 /* If skip to loop exits, save here information about loop exits. */
1011 int current_exit;
9771b263 1012 vec<edge> loop_exits;
84562394 1013};
e855c69d
AB
1014
1015/* A structure returning all successor's information. */
1016struct succs_info
1017{
1018 /* Flags that these succcessors were computed with. */
1019 short flags;
1020
1021 /* Successors that correspond to the flags. */
1022 insn_vec_t succs_ok;
1023
b8698a0f 1024 /* Their probabilities. As of now, we don't need this for other
e855c69d 1025 successors. */
9771b263 1026 vec<int> probs_ok;
e855c69d
AB
1027
1028 /* Other successors. */
1029 insn_vec_t succs_other;
1030
1031 /* Probability of all successors. */
1032 int all_prob;
1033
1034 /* The number of all successors. */
1035 int all_succs_n;
1036
1037 /* The number of good successors. */
1038 int succs_ok_n;
1039};
1040
1041/* Some needed definitions. */
1042extern basic_block after_recovery;
1043
c5db5458
DM
1044extern rtx_insn *sel_bb_head (basic_block);
1045extern rtx_insn *sel_bb_end (basic_block);
e855c69d
AB
1046extern bool sel_bb_empty_p (basic_block);
1047extern bool in_current_region_p (basic_block);
1048
1049/* True when BB is a header of the inner loop. */
1050static inline bool
1051inner_loop_header_p (basic_block bb)
1052{
b8698a0f 1053 struct loop *inner_loop;
e855c69d
AB
1054
1055 if (!current_loop_nest)
1056 return false;
1057
fefa31b5 1058 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
e855c69d
AB
1059 return false;
1060
1061 inner_loop = bb->loop_father;
1062 if (inner_loop == current_loop_nest)
1063 return false;
1064
1065 /* If successor belongs to another loop. */
1066 if (bb == inner_loop->header
1067 && flow_bb_inside_loop_p (current_loop_nest, bb))
1068 {
1069 /* Could be '=' here because of wrong loop depths. */
1070 gcc_assert (loop_depth (inner_loop) >= loop_depth (current_loop_nest));
1071 return true;
1072 }
1073
b8698a0f 1074 return false;
e855c69d
AB
1075}
1076
1077/* Return exit edges of LOOP, filtering out edges with the same dest bb. */
9771b263 1078static inline vec<edge>
e855c69d
AB
1079get_loop_exit_edges_unique_dests (const struct loop *loop)
1080{
6e1aa848 1081 vec<edge> edges = vNULL;
e855c69d
AB
1082 struct loop_exit *exit;
1083
fefa31b5 1084 gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun)
e855c69d
AB
1085 && current_loops->state & LOOPS_HAVE_RECORDED_EXITS);
1086
1087 for (exit = loop->exits->next; exit->e; exit = exit->next)
1088 {
1089 int i;
1090 edge e;
1091 bool was_dest = false;
b8698a0f 1092
9771b263 1093 for (i = 0; edges.iterate (i, &e); i++)
e855c69d
AB
1094 if (e->dest == exit->e->dest)
1095 {
1096 was_dest = true;
1097 break;
1098 }
1099
1100 if (!was_dest)
9771b263 1101 edges.safe_push (exit->e);
e855c69d
AB
1102 }
1103 return edges;
1104}
1105
b5b8b0ac
AO
1106static bool
1107sel_bb_empty_or_nop_p (basic_block bb)
1108{
1109 insn_t first = sel_bb_head (bb), last;
1110
1111 if (first == NULL_RTX)
1112 return true;
1113
1114 if (!INSN_NOP_P (first))
1115 return false;
1116
fefa31b5 1117 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
b5b8b0ac
AO
1118 return false;
1119
1120 last = sel_bb_end (bb);
1121 if (first != last)
1122 return false;
1123
1124 return true;
1125}
1126
b8698a0f 1127/* Collect all loop exits recursively, skipping empty BBs between them.
e855c69d
AB
1128 E.g. if BB is a loop header which has several loop exits,
1129 traverse all of them and if any of them turns out to be another loop header
b8698a0f 1130 (after skipping empty BBs), add its loop exits to the resulting vector
e855c69d 1131 as well. */
9771b263 1132static inline vec<edge>
e855c69d
AB
1133get_all_loop_exits (basic_block bb)
1134{
6e1aa848 1135 vec<edge> exits = vNULL;
e855c69d
AB
1136
1137 /* If bb is empty, and we're skipping to loop exits, then
1138 consider bb as a possible gate to the inner loop now. */
b5b8b0ac 1139 while (sel_bb_empty_or_nop_p (bb)
3dbdd1b9
AB
1140 && in_current_region_p (bb)
1141 && EDGE_COUNT (bb->succs) > 0)
e855c69d
AB
1142 {
1143 bb = single_succ (bb);
1144
1145 /* This empty block could only lead outside the region. */
1146 gcc_assert (! in_current_region_p (bb));
1147 }
1148
1149 /* And now check whether we should skip over inner loop. */
1150 if (inner_loop_header_p (bb))
1151 {
1152 struct loop *this_loop;
1153 struct loop *pred_loop = NULL;
1154 int i;
1155 edge e;
b8698a0f 1156
e855c69d
AB
1157 for (this_loop = bb->loop_father;
1158 this_loop && this_loop != current_loop_nest;
1159 this_loop = loop_outer (this_loop))
1160 pred_loop = this_loop;
b8698a0f 1161
e855c69d
AB
1162 this_loop = pred_loop;
1163 gcc_assert (this_loop != NULL);
1164
1165 exits = get_loop_exit_edges_unique_dests (this_loop);
1166
1167 /* Traverse all loop headers. */
9771b263 1168 for (i = 0; exits.iterate (i, &e); i++)
65b659ff
AB
1169 if (in_current_region_p (e->dest)
1170 || inner_loop_header_p (e->dest))
e855c69d 1171 {
9771b263 1172 vec<edge> next_exits = get_all_loop_exits (e->dest);
b8698a0f 1173
9771b263 1174 if (next_exits.exists ())
e855c69d
AB
1175 {
1176 int j;
1177 edge ne;
b8698a0f 1178
e855c69d
AB
1179 /* Add all loop exits for the current edge into the
1180 resulting vector. */
9771b263
DN
1181 for (j = 0; next_exits.iterate (j, &ne); j++)
1182 exits.safe_push (ne);
b8698a0f 1183
e855c69d 1184 /* Remove the original edge. */
9771b263 1185 exits.ordered_remove (i);
e855c69d
AB
1186
1187 /* Decrease the loop counter so we won't skip anything. */
1188 i--;
1189 continue;
1190 }
1191 }
1192 }
1193
1194 return exits;
1195}
1196
1197/* Flags to pass to compute_succs_info and FOR_EACH_SUCC.
1198 Any successor will fall into exactly one category. */
1199
1200/* Include normal successors. */
1201#define SUCCS_NORMAL (1)
1202
1203/* Include back-edge successors. */
1204#define SUCCS_BACK (2)
1205
1206/* Include successors that are outside of the current region. */
1207#define SUCCS_OUT (4)
1208
b8698a0f 1209/* When pipelining of the outer loops is enabled, skip innermost loops
e855c69d
AB
1210 to their exits. */
1211#define SUCCS_SKIP_TO_LOOP_EXITS (8)
1212
1213/* Include all successors. */
1214#define SUCCS_ALL (SUCCS_NORMAL | SUCCS_BACK | SUCCS_OUT)
1215
1216/* We need to return a succ_iterator to avoid 'unitialized' warning
1217 during bootstrap. */
1218static inline succ_iterator
1219_succ_iter_start (insn_t *succp, insn_t insn, int flags)
1220{
1221 succ_iterator i;
1222
1223 basic_block bb = BLOCK_FOR_INSN (insn);
1224
1225 gcc_assert (INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn));
1226
1227 i.flags = flags;
1228
1229 /* Avoid 'uninitialized' warning. */
1230 *succp = NULL;
1231 i.e1 = NULL;
1232 i.e2 = NULL;
1233 i.bb = bb;
1234 i.current_flags = 0;
1235 i.current_exit = -1;
9771b263 1236 i.loop_exits.create (0);
e855c69d 1237
fefa31b5 1238 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun) && BB_END (bb) != insn)
e855c69d
AB
1239 {
1240 i.bb_end = false;
1241
1242 /* Avoid 'uninitialized' warning. */
1243 i.ei.index = 0;
9771b263 1244 i.ei.container = 0;
e855c69d
AB
1245 }
1246 else
1247 {
1248 i.ei = ei_start (bb->succs);
1249 i.bb_end = true;
1250 }
1251
1252 return i;
1253}
1254
1255static inline bool
6144a836 1256_succ_iter_cond (succ_iterator *ip, insn_t *succp, insn_t insn,
e855c69d
AB
1257 bool check (edge, succ_iterator *))
1258{
1259 if (!ip->bb_end)
1260 {
1261 /* When we're in a middle of a basic block, return
1262 the next insn immediately, but only when SUCCS_NORMAL is set. */
1263 if (*succp != NULL || (ip->flags & SUCCS_NORMAL) == 0)
1264 return false;
1265
1266 *succp = NEXT_INSN (insn);
1267 ip->current_flags = SUCCS_NORMAL;
1268 return true;
1269 }
1270 else
1271 {
b8698a0f 1272 while (1)
e855c69d
AB
1273 {
1274 edge e_tmp = NULL;
1275
1276 /* First, try loop exits, if we have them. */
9771b263 1277 if (ip->loop_exits.exists ())
e855c69d
AB
1278 {
1279 do
1280 {
9771b263 1281 ip->loop_exits.iterate (ip->current_exit, &e_tmp);
e855c69d
AB
1282 ip->current_exit++;
1283 }
1284 while (e_tmp && !check (e_tmp, ip));
b8698a0f 1285
e855c69d 1286 if (!e_tmp)
9771b263 1287 ip->loop_exits.release ();
e855c69d
AB
1288 }
1289
1290 /* If we have found a successor, then great. */
1291 if (e_tmp)
1292 {
1293 ip->e1 = e_tmp;
1294 break;
1295 }
1296
1297 /* If not, then try the next edge. */
1298 while (ei_cond (ip->ei, &(ip->e1)))
1299 {
1300 basic_block bb = ip->e1->dest;
1301
1302 /* Consider bb as a possible loop header. */
1303 if ((ip->flags & SUCCS_SKIP_TO_LOOP_EXITS)
1304 && flag_sel_sched_pipelining_outer_loops
b8698a0f
L
1305 && (!in_current_region_p (bb)
1306 || BLOCK_TO_BB (ip->bb->index)
e855c69d
AB
1307 < BLOCK_TO_BB (bb->index)))
1308 {
1309 /* Get all loop exits recursively. */
1310 ip->loop_exits = get_all_loop_exits (bb);
1311
9771b263 1312 if (ip->loop_exits.exists ())
e855c69d
AB
1313 {
1314 ip->current_exit = 0;
b8698a0f 1315 /* Move the iterator now, because we won't do
e855c69d
AB
1316 succ_iter_next until loop exits will end. */
1317 ei_next (&(ip->ei));
1318 break;
1319 }
1320 }
1321
1322 /* bb is not a loop header, check as usual. */
1323 if (check (ip->e1, ip))
1324 break;
1325
1326 ei_next (&(ip->ei));
1327 }
1328
1329 /* If loop_exits are non null, we have found an inner loop;
1330 do one more iteration to fetch an edge from these exits. */
9771b263 1331 if (ip->loop_exits.exists ())
e855c69d
AB
1332 continue;
1333
1334 /* Otherwise, we've found an edge in a usual way. Break now. */
1335 break;
1336 }
1337
1338 if (ip->e1)
1339 {
1340 basic_block bb = ip->e2->dest;
1341
fefa31b5 1342 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb == after_recovery)
e855c69d
AB
1343 *succp = exit_insn;
1344 else
1345 {
1346 *succp = sel_bb_head (bb);
1347
1348 gcc_assert (ip->flags != SUCCS_NORMAL
1349 || *succp == NEXT_INSN (bb_note (bb)));
1350 gcc_assert (BLOCK_FOR_INSN (*succp) == bb);
1351 }
1352
1353 return true;
1354 }
1355 else
1356 return false;
1357 }
1358}
1359
1360static inline void
1361_succ_iter_next (succ_iterator *ip)
1362{
1363 gcc_assert (!ip->e2 || ip->e1);
1364
9771b263 1365 if (ip->bb_end && ip->e1 && !ip->loop_exits.exists ())
e855c69d
AB
1366 ei_next (&(ip->ei));
1367}
1368
1369/* Returns true when E1 is an eligible successor edge, possibly skipping
1370 empty blocks. When E2P is not null, the resulting edge is written there.
1371 FLAGS are used to specify whether back edges and out-of-region edges
1372 should be considered. */
1373static inline bool
1374_eligible_successor_edge_p (edge e1, succ_iterator *ip)
1375{
1376 edge e2 = e1;
1377 basic_block bb;
1378 int flags = ip->flags;
1379 bool src_outside_rgn = !in_current_region_p (e1->src);
1380
1381 gcc_assert (flags != 0);
1382
1383 if (src_outside_rgn)
1384 {
1385 /* Any successor of the block that is outside current region is
1386 ineligible, except when we're skipping to loop exits. */
1387 gcc_assert (flags & (SUCCS_OUT | SUCCS_SKIP_TO_LOOP_EXITS));
1388
1389 if (flags & SUCCS_OUT)
1390 return false;
1391 }
1392
1393 bb = e2->dest;
1394
1395 /* Skip empty blocks, but be careful not to leave the region. */
1396 while (1)
1397 {
1398 if (!sel_bb_empty_p (bb))
b5b8b0ac
AO
1399 {
1400 edge ne;
1401 basic_block nbb;
1402
1403 if (!sel_bb_empty_or_nop_p (bb))
1404 break;
1405
1406 ne = EDGE_SUCC (bb, 0);
1407 nbb = ne->dest;
1408
1409 if (!in_current_region_p (nbb)
1410 && !(flags & SUCCS_OUT))
1411 break;
1412
1413 e2 = ne;
1414 bb = nbb;
1415 continue;
1416 }
b8698a0f
L
1417
1418 if (!in_current_region_p (bb)
e855c69d
AB
1419 && !(flags & SUCCS_OUT))
1420 return false;
1421
4b2e464e
SE
1422 if (EDGE_COUNT (bb->succs) == 0)
1423 return false;
1424
e855c69d
AB
1425 e2 = EDGE_SUCC (bb, 0);
1426 bb = e2->dest;
e855c69d 1427 }
b8698a0f 1428
e855c69d
AB
1429 /* Save the second edge for later checks. */
1430 ip->e2 = e2;
1431
1432 if (in_current_region_p (bb))
1433 {
b8698a0f
L
1434 /* BLOCK_TO_BB sets topological order of the region here.
1435 It is important to use real predecessor here, which is ip->bb,
1436 as we may well have e1->src outside current region,
e855c69d
AB
1437 when skipping to loop exits. */
1438 bool succeeds_in_top_order = (BLOCK_TO_BB (ip->bb->index)
1439 < BLOCK_TO_BB (bb->index));
1440
1441 /* This is true for the all cases except the last one. */
1442 ip->current_flags = SUCCS_NORMAL;
b8698a0f 1443
e855c69d
AB
1444 /* We are advancing forward in the region, as usual. */
1445 if (succeeds_in_top_order)
1446 {
1447 /* We are skipping to loop exits here. */
1448 gcc_assert (!src_outside_rgn
1449 || flag_sel_sched_pipelining_outer_loops);
1450 return !!(flags & SUCCS_NORMAL);
1451 }
1452
b8698a0f 1453 /* This is a back edge. During pipelining we ignore back edges,
e855c69d 1454 but only when it leads to the same loop. It can lead to the header
b8698a0f 1455 of the outer loop, which will also be the preheader of
e855c69d
AB
1456 the current loop. */
1457 if (pipelining_p
1458 && e1->src->loop_father == bb->loop_father)
1459 return !!(flags & SUCCS_NORMAL);
1460
1461 /* A back edge should be requested explicitly. */
1462 ip->current_flags = SUCCS_BACK;
1463 return !!(flags & SUCCS_BACK);
1464 }
1465
1466 ip->current_flags = SUCCS_OUT;
1467 return !!(flags & SUCCS_OUT);
1468}
1469
1470#define FOR_EACH_SUCC_1(SUCC, ITER, INSN, FLAGS) \
1471 for ((ITER) = _succ_iter_start (&(SUCC), (INSN), (FLAGS)); \
1472 _succ_iter_cond (&(ITER), &(SUCC), (INSN), _eligible_successor_edge_p); \
1473 _succ_iter_next (&(ITER)))
1474
1475#define FOR_EACH_SUCC(SUCC, ITER, INSN) \
1476 FOR_EACH_SUCC_1 (SUCC, ITER, INSN, SUCCS_NORMAL)
1477
1478/* Return the current edge along which a successor was built. */
1479#define SUCC_ITER_EDGE(ITER) ((ITER)->e1)
1480
1481/* Return the next block of BB not running into inconsistencies. */
1482static inline basic_block
1483bb_next_bb (basic_block bb)
1484{
1485 switch (EDGE_COUNT (bb->succs))
1486 {
1487 case 0:
1488 return bb->next_bb;
1489
b8698a0f 1490 case 1:
e855c69d
AB
1491 return single_succ (bb);
1492
1493 case 2:
1494 return FALLTHRU_EDGE (bb)->dest;
b8698a0f 1495
e855c69d
AB
1496 default:
1497 return bb->next_bb;
1498 }
1499
1500 gcc_unreachable ();
1501}
1502
1503\f
1504
1505/* Functions that are used in sel-sched.c. */
1506
1507/* List functions. */
1508extern ilist_t ilist_copy (ilist_t);
1509extern ilist_t ilist_invert (ilist_t);
1510extern void blist_add (blist_t *, insn_t, ilist_t, deps_t);
1511extern void blist_remove (blist_t *);
1512extern void flist_tail_init (flist_tail_t);
1513
1514extern fence_t flist_lookup (flist_t, insn_t);
1515extern void flist_clear (flist_t *);
1516extern void def_list_add (def_list_t *, insn_t, bool);
1517
1518/* Target context functions. */
1519extern tc_t create_target_context (bool);
1520extern void set_target_context (tc_t);
1521extern void reset_target_context (tc_t, bool);
1522
1523/* Deps context functions. */
1524extern void advance_deps_context (deps_t, insn_t);
1525
1526/* Fences functions. */
1527extern void init_fences (insn_t);
1528extern void add_clean_fence_to_fences (flist_tail_t, insn_t, fence_t);
1529extern void add_dirty_fence_to_fences (flist_tail_t, insn_t, fence_t);
1530extern void move_fence_to_fences (flist_t, flist_tail_t);
1531
1532/* Pool functions. */
1533extern regset get_regset_from_pool (void);
1534extern regset get_clear_regset_from_pool (void);
1535extern void return_regset_to_pool (regset);
1536extern void free_regset_pool (void);
1537
1538extern insn_t get_nop_from_pool (insn_t);
b5b8b0ac 1539extern void return_nop_to_pool (insn_t, bool);
e855c69d
AB
1540extern void free_nop_pool (void);
1541
1542/* Vinsns functions. */
1543extern bool vinsn_separable_p (vinsn_t);
1544extern bool vinsn_cond_branch_p (vinsn_t);
1545extern void recompute_vinsn_lhs_rhs (vinsn_t);
1546extern int sel_vinsn_cost (vinsn_t);
1547extern insn_t sel_gen_insn_from_rtx_after (rtx, expr_t, int, insn_t);
1548extern insn_t sel_gen_recovery_insn_from_rtx_after (rtx, expr_t, int, insn_t);
1549extern insn_t sel_gen_insn_from_expr_after (expr_t, vinsn_t, int, insn_t);
1550extern insn_t sel_move_insn (expr_t, int, insn_t);
1551extern void vinsn_attach (vinsn_t);
1552extern void vinsn_detach (vinsn_t);
1553extern vinsn_t vinsn_copy (vinsn_t, bool);
1554extern bool vinsn_equal_p (vinsn_t, vinsn_t);
1555
1556/* EXPR functions. */
1557extern void copy_expr (expr_t, expr_t);
1558extern void copy_expr_onside (expr_t, expr_t);
1559extern void merge_expr_data (expr_t, expr_t, insn_t);
1560extern void merge_expr (expr_t, expr_t, insn_t);
1561extern void clear_expr (expr_t);
1562extern unsigned expr_dest_regno (expr_t);
b8698a0f 1563extern rtx expr_dest_reg (expr_t);
9771b263 1564extern int find_in_history_vect (vec<expr_history_def> ,
e855c69d 1565 rtx, vinsn_t, bool);
9771b263 1566extern void insert_in_history_vect (vec<expr_history_def> *,
b8698a0f 1567 unsigned, enum local_trans_type,
e855c69d
AB
1568 vinsn_t, vinsn_t, ds_t);
1569extern void mark_unavailable_targets (av_set_t, av_set_t, regset);
1570extern int speculate_expr (expr_t, ds_t);
1571
1572/* Av set functions. */
1573extern void av_set_add (av_set_t *, expr_t);
1574extern void av_set_iter_remove (av_set_iterator *);
1575extern expr_t av_set_lookup (av_set_t, vinsn_t);
1576extern expr_t merge_with_other_exprs (av_set_t *, av_set_iterator *, expr_t);
1577extern bool av_set_is_in_p (av_set_t, vinsn_t);
1578extern av_set_t av_set_copy (av_set_t);
1579extern void av_set_union_and_clear (av_set_t *, av_set_t *, insn_t);
1580extern void av_set_union_and_live (av_set_t *, av_set_t *, regset, regset, insn_t);
1581extern void av_set_clear (av_set_t *);
1582extern void av_set_leave_one_nonspec (av_set_t *);
1583extern expr_t av_set_element (av_set_t, int);
1584extern void av_set_substract_cond_branches (av_set_t *);
1585extern void av_set_split_usefulness (av_set_t, int, int);
5d369d58 1586extern void av_set_code_motion_filter (av_set_t *, av_set_t);
e855c69d
AB
1587
1588extern void sel_save_haifa_priorities (void);
1589
1590extern void sel_init_global_and_expr (bb_vec_t);
1591extern void sel_finish_global_and_expr (void);
1592
1593extern regset compute_live (insn_t);
cf3d5824 1594extern bool register_unavailable_p (regset, rtx);
e855c69d
AB
1595
1596/* Dependence analysis functions. */
1597extern void sel_clear_has_dependence (void);
1598extern ds_t has_dependence_p (expr_t, insn_t, ds_t **);
1599
1600extern int tick_check_p (expr_t, deps_t, fence_t);
1601
1602/* Functions to work with insns. */
1603extern bool lhs_of_insn_equals_to_dest_p (insn_t, rtx);
1604extern bool insn_eligible_for_subst_p (insn_t);
1605extern void get_dest_and_mode (rtx, rtx *, enum machine_mode *);
1606
1607extern bool bookkeeping_can_be_created_if_moved_through_p (insn_t);
1608extern bool sel_remove_insn (insn_t, bool, bool);
1609extern bool bb_header_p (insn_t);
1610extern void sel_init_invalid_data_sets (insn_t);
1611extern bool insn_at_boundary_p (insn_t);
e855c69d
AB
1612
1613/* Basic block and CFG functions. */
1614
c5db5458 1615extern rtx_insn *sel_bb_head (basic_block);
e855c69d 1616extern bool sel_bb_head_p (insn_t);
c5db5458 1617extern rtx_insn *sel_bb_end (basic_block);
e855c69d
AB
1618extern bool sel_bb_end_p (insn_t);
1619extern bool sel_bb_empty_p (basic_block);
1620
1621extern bool in_current_region_p (basic_block);
1622extern basic_block fallthru_bb_of_jump (rtx);
1623
a95b23b4 1624extern void sel_init_bbs (bb_vec_t);
e855c69d
AB
1625extern void sel_finish_bbs (void);
1626
1627extern struct succs_info * compute_succs_info (insn_t, short);
1628extern void free_succs_info (struct succs_info *);
1629extern bool sel_insn_has_single_succ_p (insn_t, int);
1630extern bool sel_num_cfg_preds_gt_1 (insn_t);
b32d5189 1631extern int get_seqno_by_preds (rtx_insn *);
e855c69d
AB
1632
1633extern bool bb_ends_ebb_p (basic_block);
1634extern bool in_same_ebb_p (insn_t, insn_t);
1635
1636extern bool tidy_control_flow (basic_block, bool);
1637extern void free_bb_note_pool (void);
1638
b59ab570 1639extern void purge_empty_blocks (void);
e855c69d
AB
1640extern basic_block sel_split_edge (edge);
1641extern basic_block sel_create_recovery_block (insn_t);
b59ab570 1642extern bool sel_redirect_edge_and_branch (edge, basic_block);
e855c69d
AB
1643extern void sel_redirect_edge_and_branch_force (edge, basic_block);
1644extern void sel_init_pipelining (void);
1645extern void sel_finish_pipelining (void);
1646extern void sel_sched_region (int);
e855c69d
AB
1647extern loop_p get_loop_nest_for_rgn (unsigned int);
1648extern bool considered_for_pipelining_p (struct loop *);
9771b263 1649extern void make_region_from_loop_preheader (vec<basic_block> *&);
ea4d630f 1650extern void sel_add_loop_preheaders (bb_vec_t *);
e855c69d
AB
1651extern bool sel_is_loop_preheader_p (basic_block);
1652extern void clear_outdated_rtx_info (basic_block);
1653extern void free_data_sets (basic_block);
1654extern void exchange_data_sets (basic_block, basic_block);
1655extern void copy_data_sets (basic_block, basic_block);
1656
1657extern void sel_register_cfg_hooks (void);
1658extern void sel_unregister_cfg_hooks (void);
1659
1660/* Expression transformation routines. */
9c068b73 1661extern rtx_insn *create_insn_rtx_from_pattern (rtx, rtx);
6144a836 1662extern vinsn_t create_vinsn_from_insn_rtx (rtx_insn *, bool);
9c068b73 1663extern rtx_insn *create_copy_of_insn_rtx (rtx);
e855c69d
AB
1664extern void change_vinsn_in_expr (expr_t, vinsn_t);
1665
1666/* Various initialization functions. */
1667extern void init_lv_sets (void);
1668extern void free_lv_sets (void);
1669extern void setup_nop_and_exit_insns (void);
1670extern void free_nop_and_exit_insns (void);
bcf33775 1671extern void free_data_for_scheduled_insn (insn_t);
e855c69d
AB
1672extern void setup_nop_vinsn (void);
1673extern void free_nop_vinsn (void);
1674extern void sel_set_sched_flags (void);
1675extern void sel_setup_sched_infos (void);
1676extern void alloc_sched_pools (void);
1677extern void free_sched_pools (void);
1678
1679#endif /* GCC_SEL_SCHED_IR_H */
1680
1681
1682
1683
1684
1685
1686
1687