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