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