* inclhack.def (aix_complex): Redefine _Complex_I. Do not
[official-gcc.git] / gcc / sel-sched-ir.c
blob2932800f569fe90bf55b0607520c48857e8b71f0
1 /* Instruction scheduling pass. Selective scheduler and pipeliner.
2 Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "toplev.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "hard-reg-set.h"
28 #include "regs.h"
29 #include "function.h"
30 #include "flags.h"
31 #include "insn-config.h"
32 #include "insn-attr.h"
33 #include "except.h"
34 #include "toplev.h"
35 #include "recog.h"
36 #include "params.h"
37 #include "target.h"
38 #include "timevar.h"
39 #include "tree-pass.h"
40 #include "sched-int.h"
41 #include "ggc.h"
42 #include "tree.h"
43 #include "vec.h"
44 #include "langhooks.h"
45 #include "rtlhooks-def.h"
47 #ifdef INSN_SCHEDULING
48 #include "sel-sched-ir.h"
49 /* We don't have to use it except for sel_print_insn. */
50 #include "sel-sched-dump.h"
52 /* A vector holding bb info for whole scheduling pass. */
53 VEC(sel_global_bb_info_def, heap) *sel_global_bb_info = NULL;
55 /* A vector holding bb info. */
56 VEC(sel_region_bb_info_def, heap) *sel_region_bb_info = NULL;
58 /* A pool for allocating all lists. */
59 alloc_pool sched_lists_pool;
61 /* This contains information about successors for compute_av_set. */
62 struct succs_info current_succs;
64 /* Data structure to describe interaction with the generic scheduler utils. */
65 static struct common_sched_info_def sel_common_sched_info;
67 /* The loop nest being pipelined. */
68 struct loop *current_loop_nest;
70 /* LOOP_NESTS is a vector containing the corresponding loop nest for
71 each region. */
72 static VEC(loop_p, heap) *loop_nests = NULL;
74 /* Saves blocks already in loop regions, indexed by bb->index. */
75 static sbitmap bbs_in_loop_rgns = NULL;
77 /* CFG hooks that are saved before changing create_basic_block hook. */
78 static struct cfg_hooks orig_cfg_hooks;
81 /* Array containing reverse topological index of function basic blocks,
82 indexed by BB->INDEX. */
83 static int *rev_top_order_index = NULL;
85 /* Length of the above array. */
86 static int rev_top_order_index_len = -1;
88 /* A regset pool structure. */
89 static struct
91 /* The stack to which regsets are returned. */
92 regset *v;
94 /* Its pointer. */
95 int n;
97 /* Its size. */
98 int s;
100 /* In VV we save all generated regsets so that, when destructing the
101 pool, we can compare it with V and check that every regset was returned
102 back to pool. */
103 regset *vv;
105 /* The pointer of VV stack. */
106 int nn;
108 /* Its size. */
109 int ss;
111 /* The difference between allocated and returned regsets. */
112 int diff;
113 } regset_pool = { NULL, 0, 0, NULL, 0, 0, 0 };
115 /* This represents the nop pool. */
116 static struct
118 /* The vector which holds previously emitted nops. */
119 insn_t *v;
121 /* Its pointer. */
122 int n;
124 /* Its size. */
125 int s;
126 } nop_pool = { NULL, 0, 0 };
128 /* The pool for basic block notes. */
129 static rtx_vec_t bb_note_pool;
131 /* A NOP pattern used to emit placeholder insns. */
132 rtx nop_pattern = NULL_RTX;
133 /* A special instruction that resides in EXIT_BLOCK.
134 EXIT_INSN is successor of the insns that lead to EXIT_BLOCK. */
135 rtx exit_insn = NULL_RTX;
137 /* TRUE if while scheduling current region, which is loop, its preheader
138 was removed. */
139 bool preheader_removed = false;
142 /* Forward static declarations. */
143 static void fence_clear (fence_t);
145 static void deps_init_id (idata_t, insn_t, bool);
146 static void init_id_from_df (idata_t, insn_t, bool);
147 static expr_t set_insn_init (expr_t, vinsn_t, int);
149 static void cfg_preds (basic_block, insn_t **, int *);
150 static void prepare_insn_expr (insn_t, int);
151 static void free_history_vect (VEC (expr_history_def, heap) **);
153 static void move_bb_info (basic_block, basic_block);
154 static void remove_empty_bb (basic_block, bool);
155 static void sel_remove_loop_preheader (void);
157 static bool insn_is_the_only_one_in_bb_p (insn_t);
158 static void create_initial_data_sets (basic_block);
160 static void invalidate_av_set (basic_block);
161 static void extend_insn_data (void);
162 static void sel_init_new_insn (insn_t, int);
163 static void finish_insns (void);
165 /* Various list functions. */
167 /* Copy an instruction list L. */
168 ilist_t
169 ilist_copy (ilist_t l)
171 ilist_t head = NULL, *tailp = &head;
173 while (l)
175 ilist_add (tailp, ILIST_INSN (l));
176 tailp = &ILIST_NEXT (*tailp);
177 l = ILIST_NEXT (l);
180 return head;
183 /* Invert an instruction list L. */
184 ilist_t
185 ilist_invert (ilist_t l)
187 ilist_t res = NULL;
189 while (l)
191 ilist_add (&res, ILIST_INSN (l));
192 l = ILIST_NEXT (l);
195 return res;
198 /* Add a new boundary to the LP list with parameters TO, PTR, and DC. */
199 void
200 blist_add (blist_t *lp, insn_t to, ilist_t ptr, deps_t dc)
202 bnd_t bnd;
204 _list_add (lp);
205 bnd = BLIST_BND (*lp);
207 BND_TO (bnd) = to;
208 BND_PTR (bnd) = ptr;
209 BND_AV (bnd) = NULL;
210 BND_AV1 (bnd) = NULL;
211 BND_DC (bnd) = dc;
214 /* Remove the list note pointed to by LP. */
215 void
216 blist_remove (blist_t *lp)
218 bnd_t b = BLIST_BND (*lp);
220 av_set_clear (&BND_AV (b));
221 av_set_clear (&BND_AV1 (b));
222 ilist_clear (&BND_PTR (b));
224 _list_remove (lp);
227 /* Init a fence tail L. */
228 void
229 flist_tail_init (flist_tail_t l)
231 FLIST_TAIL_HEAD (l) = NULL;
232 FLIST_TAIL_TAILP (l) = &FLIST_TAIL_HEAD (l);
235 /* Try to find fence corresponding to INSN in L. */
236 fence_t
237 flist_lookup (flist_t l, insn_t insn)
239 while (l)
241 if (FENCE_INSN (FLIST_FENCE (l)) == insn)
242 return FLIST_FENCE (l);
244 l = FLIST_NEXT (l);
247 return NULL;
250 /* Init the fields of F before running fill_insns. */
251 static void
252 init_fence_for_scheduling (fence_t f)
254 FENCE_BNDS (f) = NULL;
255 FENCE_PROCESSED_P (f) = false;
256 FENCE_SCHEDULED_P (f) = false;
259 /* Add new fence consisting of INSN and STATE to the list pointed to by LP. */
260 static void
261 flist_add (flist_t *lp, insn_t insn, state_t state, deps_t dc, void *tc,
262 insn_t last_scheduled_insn, VEC(rtx,gc) *executing_insns,
263 int *ready_ticks, int ready_ticks_size, insn_t sched_next,
264 int cycle, int cycle_issued_insns,
265 bool starts_cycle_p, bool after_stall_p)
267 fence_t f;
269 _list_add (lp);
270 f = FLIST_FENCE (*lp);
272 FENCE_INSN (f) = insn;
274 gcc_assert (state != NULL);
275 FENCE_STATE (f) = state;
277 FENCE_CYCLE (f) = cycle;
278 FENCE_ISSUED_INSNS (f) = cycle_issued_insns;
279 FENCE_STARTS_CYCLE_P (f) = starts_cycle_p;
280 FENCE_AFTER_STALL_P (f) = after_stall_p;
282 gcc_assert (dc != NULL);
283 FENCE_DC (f) = dc;
285 gcc_assert (tc != NULL || targetm.sched.alloc_sched_context == NULL);
286 FENCE_TC (f) = tc;
288 FENCE_LAST_SCHEDULED_INSN (f) = last_scheduled_insn;
289 FENCE_EXECUTING_INSNS (f) = executing_insns;
290 FENCE_READY_TICKS (f) = ready_ticks;
291 FENCE_READY_TICKS_SIZE (f) = ready_ticks_size;
292 FENCE_SCHED_NEXT (f) = sched_next;
294 init_fence_for_scheduling (f);
297 /* Remove the head node of the list pointed to by LP. */
298 static void
299 flist_remove (flist_t *lp)
301 if (FENCE_INSN (FLIST_FENCE (*lp)))
302 fence_clear (FLIST_FENCE (*lp));
303 _list_remove (lp);
306 /* Clear the fence list pointed to by LP. */
307 void
308 flist_clear (flist_t *lp)
310 while (*lp)
311 flist_remove (lp);
314 /* Add ORIGINAL_INSN the def list DL honoring CROSSES_CALL. */
315 void
316 def_list_add (def_list_t *dl, insn_t original_insn, bool crosses_call)
318 def_t d;
320 _list_add (dl);
321 d = DEF_LIST_DEF (*dl);
323 d->orig_insn = original_insn;
324 d->crosses_call = crosses_call;
328 /* Functions to work with target contexts. */
330 /* Bulk target context. It is convenient for debugging purposes to ensure
331 that there are no uninitialized (null) target contexts. */
332 static tc_t bulk_tc = (tc_t) 1;
334 /* Target hooks wrappers. In the future we can provide some default
335 implementations for them. */
337 /* Allocate a store for the target context. */
338 static tc_t
339 alloc_target_context (void)
341 return (targetm.sched.alloc_sched_context
342 ? targetm.sched.alloc_sched_context () : bulk_tc);
345 /* Init target context TC.
346 If CLEAN_P is true, then make TC as it is beginning of the scheduler.
347 Overwise, copy current backend context to TC. */
348 static void
349 init_target_context (tc_t tc, bool clean_p)
351 if (targetm.sched.init_sched_context)
352 targetm.sched.init_sched_context (tc, clean_p);
355 /* Allocate and initialize a target context. Meaning of CLEAN_P is the same as
356 int init_target_context (). */
357 tc_t
358 create_target_context (bool clean_p)
360 tc_t tc = alloc_target_context ();
362 init_target_context (tc, clean_p);
363 return tc;
366 /* Copy TC to the current backend context. */
367 void
368 set_target_context (tc_t tc)
370 if (targetm.sched.set_sched_context)
371 targetm.sched.set_sched_context (tc);
374 /* TC is about to be destroyed. Free any internal data. */
375 static void
376 clear_target_context (tc_t tc)
378 if (targetm.sched.clear_sched_context)
379 targetm.sched.clear_sched_context (tc);
382 /* Clear and free it. */
383 static void
384 delete_target_context (tc_t tc)
386 clear_target_context (tc);
388 if (targetm.sched.free_sched_context)
389 targetm.sched.free_sched_context (tc);
392 /* Make a copy of FROM in TO.
393 NB: May be this should be a hook. */
394 static void
395 copy_target_context (tc_t to, tc_t from)
397 tc_t tmp = create_target_context (false);
399 set_target_context (from);
400 init_target_context (to, false);
402 set_target_context (tmp);
403 delete_target_context (tmp);
406 /* Create a copy of TC. */
407 static tc_t
408 create_copy_of_target_context (tc_t tc)
410 tc_t copy = alloc_target_context ();
412 copy_target_context (copy, tc);
414 return copy;
417 /* Clear TC and initialize it according to CLEAN_P. The meaning of CLEAN_P
418 is the same as in init_target_context (). */
419 void
420 reset_target_context (tc_t tc, bool clean_p)
422 clear_target_context (tc);
423 init_target_context (tc, clean_p);
426 /* Functions to work with dependence contexts.
427 Dc (aka deps context, aka deps_t, aka struct deps *) is short for dependence
428 context. It accumulates information about processed insns to decide if
429 current insn is dependent on the processed ones. */
431 /* Make a copy of FROM in TO. */
432 static void
433 copy_deps_context (deps_t to, deps_t from)
435 init_deps (to);
436 deps_join (to, from);
439 /* Allocate store for dep context. */
440 static deps_t
441 alloc_deps_context (void)
443 return XNEW (struct deps);
446 /* Allocate and initialize dep context. */
447 static deps_t
448 create_deps_context (void)
450 deps_t dc = alloc_deps_context ();
452 init_deps (dc);
453 return dc;
456 /* Create a copy of FROM. */
457 static deps_t
458 create_copy_of_deps_context (deps_t from)
460 deps_t to = alloc_deps_context ();
462 copy_deps_context (to, from);
463 return to;
466 /* Clean up internal data of DC. */
467 static void
468 clear_deps_context (deps_t dc)
470 free_deps (dc);
473 /* Clear and free DC. */
474 static void
475 delete_deps_context (deps_t dc)
477 clear_deps_context (dc);
478 free (dc);
481 /* Clear and init DC. */
482 static void
483 reset_deps_context (deps_t dc)
485 clear_deps_context (dc);
486 init_deps (dc);
489 /* This structure describes the dependence analysis hooks for advancing
490 dependence context. */
491 static struct sched_deps_info_def advance_deps_context_sched_deps_info =
493 NULL,
495 NULL, /* start_insn */
496 NULL, /* finish_insn */
497 NULL, /* start_lhs */
498 NULL, /* finish_lhs */
499 NULL, /* start_rhs */
500 NULL, /* finish_rhs */
501 haifa_note_reg_set,
502 haifa_note_reg_clobber,
503 haifa_note_reg_use,
504 NULL, /* note_mem_dep */
505 NULL, /* note_dep */
507 0, 0, 0
510 /* Process INSN and add its impact on DC. */
511 void
512 advance_deps_context (deps_t dc, insn_t insn)
514 sched_deps_info = &advance_deps_context_sched_deps_info;
515 deps_analyze_insn (dc, insn);
519 /* Functions to work with DFA states. */
521 /* Allocate store for a DFA state. */
522 static state_t
523 state_alloc (void)
525 return xmalloc (dfa_state_size);
528 /* Allocate and initialize DFA state. */
529 static state_t
530 state_create (void)
532 state_t state = state_alloc ();
534 state_reset (state);
535 advance_state (state);
536 return state;
539 /* Free DFA state. */
540 static void
541 state_free (state_t state)
543 free (state);
546 /* Make a copy of FROM in TO. */
547 static void
548 state_copy (state_t to, state_t from)
550 memcpy (to, from, dfa_state_size);
553 /* Create a copy of FROM. */
554 static state_t
555 state_create_copy (state_t from)
557 state_t to = state_alloc ();
559 state_copy (to, from);
560 return to;
564 /* Functions to work with fences. */
566 /* Clear the fence. */
567 static void
568 fence_clear (fence_t f)
570 state_t s = FENCE_STATE (f);
571 deps_t dc = FENCE_DC (f);
572 void *tc = FENCE_TC (f);
574 ilist_clear (&FENCE_BNDS (f));
576 gcc_assert ((s != NULL && dc != NULL && tc != NULL)
577 || (s == NULL && dc == NULL && tc == NULL));
579 if (s != NULL)
580 free (s);
582 if (dc != NULL)
583 delete_deps_context (dc);
585 if (tc != NULL)
586 delete_target_context (tc);
587 VEC_free (rtx, gc, FENCE_EXECUTING_INSNS (f));
588 free (FENCE_READY_TICKS (f));
589 FENCE_READY_TICKS (f) = NULL;
592 /* Init a list of fences with successors of OLD_FENCE. */
593 void
594 init_fences (insn_t old_fence)
596 insn_t succ;
597 succ_iterator si;
598 bool first = true;
599 int ready_ticks_size = get_max_uid () + 1;
601 FOR_EACH_SUCC_1 (succ, si, old_fence,
602 SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
605 if (first)
606 first = false;
607 else
608 gcc_assert (flag_sel_sched_pipelining_outer_loops);
610 flist_add (&fences, succ,
611 state_create (),
612 create_deps_context () /* dc */,
613 create_target_context (true) /* tc */,
614 NULL_RTX /* last_scheduled_insn */,
615 NULL, /* executing_insns */
616 XCNEWVEC (int, ready_ticks_size), /* ready_ticks */
617 ready_ticks_size,
618 NULL_RTX /* sched_next */,
619 1 /* cycle */, 0 /* cycle_issued_insns */,
620 1 /* starts_cycle_p */, 0 /* after_stall_p */);
624 /* Merges two fences (filling fields of fence F with resulting values) by
625 following rules: 1) state, target context and last scheduled insn are
626 propagated from fallthrough edge if it is available;
627 2) deps context and cycle is propagated from more probable edge;
628 3) all other fields are set to corresponding constant values.
630 INSN, STATE, DC, TC, LAST_SCHEDULED_INSN, EXECUTING_INSNS,
631 READY_TICKS, READY_TICKS_SIZE, SCHED_NEXT, CYCLE and AFTER_STALL_P
632 are the corresponding fields of the second fence. */
633 static void
634 merge_fences (fence_t f, insn_t insn,
635 state_t state, deps_t dc, void *tc,
636 rtx last_scheduled_insn, VEC(rtx, gc) *executing_insns,
637 int *ready_ticks, int ready_ticks_size,
638 rtx sched_next, int cycle, bool after_stall_p)
640 insn_t last_scheduled_insn_old = FENCE_LAST_SCHEDULED_INSN (f);
642 gcc_assert (sel_bb_head_p (FENCE_INSN (f))
643 && !sched_next && !FENCE_SCHED_NEXT (f));
645 /* Check if we can decide which path fences came.
646 If we can't (or don't want to) - reset all. */
647 if (last_scheduled_insn == NULL
648 || last_scheduled_insn_old == NULL
649 /* This is a case when INSN is reachable on several paths from
650 one insn (this can happen when pipelining of outer loops is on and
651 there are two edges: one going around of inner loop and the other -
652 right through it; in such case just reset everything). */
653 || last_scheduled_insn == last_scheduled_insn_old)
655 state_reset (FENCE_STATE (f));
656 state_free (state);
658 reset_deps_context (FENCE_DC (f));
659 delete_deps_context (dc);
661 reset_target_context (FENCE_TC (f), true);
662 delete_target_context (tc);
664 if (cycle > FENCE_CYCLE (f))
665 FENCE_CYCLE (f) = cycle;
667 FENCE_LAST_SCHEDULED_INSN (f) = NULL;
668 VEC_free (rtx, gc, executing_insns);
669 free (ready_ticks);
670 if (FENCE_EXECUTING_INSNS (f))
671 VEC_block_remove (rtx, FENCE_EXECUTING_INSNS (f), 0,
672 VEC_length (rtx, FENCE_EXECUTING_INSNS (f)));
673 if (FENCE_READY_TICKS (f))
674 memset (FENCE_READY_TICKS (f), 0, FENCE_READY_TICKS_SIZE (f));
676 else
678 edge edge_old = NULL, edge_new = NULL;
679 edge candidate;
680 succ_iterator si;
681 insn_t succ;
683 /* Find fallthrough edge. */
684 gcc_assert (BLOCK_FOR_INSN (insn)->prev_bb);
685 candidate = find_fallthru_edge (BLOCK_FOR_INSN (insn)->prev_bb);
687 if (!candidate
688 || (candidate->src != BLOCK_FOR_INSN (last_scheduled_insn)
689 && candidate->src != BLOCK_FOR_INSN (last_scheduled_insn_old)))
691 /* No fallthrough edge leading to basic block of INSN. */
692 state_reset (FENCE_STATE (f));
693 state_free (state);
695 reset_target_context (FENCE_TC (f), true);
696 delete_target_context (tc);
698 FENCE_LAST_SCHEDULED_INSN (f) = NULL;
700 else
701 if (candidate->src == BLOCK_FOR_INSN (last_scheduled_insn))
703 /* Would be weird if same insn is successor of several fallthrough
704 edges. */
705 gcc_assert (BLOCK_FOR_INSN (insn)->prev_bb
706 != BLOCK_FOR_INSN (last_scheduled_insn_old));
708 state_free (FENCE_STATE (f));
709 FENCE_STATE (f) = state;
711 delete_target_context (FENCE_TC (f));
712 FENCE_TC (f) = tc;
714 FENCE_LAST_SCHEDULED_INSN (f) = last_scheduled_insn;
716 else
718 /* Leave STATE, TC and LAST_SCHEDULED_INSN fields untouched. */
719 state_free (state);
720 delete_target_context (tc);
722 gcc_assert (BLOCK_FOR_INSN (insn)->prev_bb
723 != BLOCK_FOR_INSN (last_scheduled_insn));
726 /* Find edge of first predecessor (last_scheduled_insn_old->insn). */
727 FOR_EACH_SUCC_1 (succ, si, last_scheduled_insn_old,
728 SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
730 if (succ == insn)
732 /* No same successor allowed from several edges. */
733 gcc_assert (!edge_old);
734 edge_old = si.e1;
737 /* Find edge of second predecessor (last_scheduled_insn->insn). */
738 FOR_EACH_SUCC_1 (succ, si, last_scheduled_insn,
739 SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
741 if (succ == insn)
743 /* No same successor allowed from several edges. */
744 gcc_assert (!edge_new);
745 edge_new = si.e1;
749 /* Check if we can choose most probable predecessor. */
750 if (edge_old == NULL || edge_new == NULL)
752 reset_deps_context (FENCE_DC (f));
753 delete_deps_context (dc);
754 VEC_free (rtx, gc, executing_insns);
755 free (ready_ticks);
757 FENCE_CYCLE (f) = MAX (FENCE_CYCLE (f), cycle);
758 if (FENCE_EXECUTING_INSNS (f))
759 VEC_block_remove (rtx, FENCE_EXECUTING_INSNS (f), 0,
760 VEC_length (rtx, FENCE_EXECUTING_INSNS (f)));
761 if (FENCE_READY_TICKS (f))
762 memset (FENCE_READY_TICKS (f), 0, FENCE_READY_TICKS_SIZE (f));
764 else
765 if (edge_new->probability > edge_old->probability)
767 delete_deps_context (FENCE_DC (f));
768 FENCE_DC (f) = dc;
769 VEC_free (rtx, gc, FENCE_EXECUTING_INSNS (f));
770 FENCE_EXECUTING_INSNS (f) = executing_insns;
771 free (FENCE_READY_TICKS (f));
772 FENCE_READY_TICKS (f) = ready_ticks;
773 FENCE_READY_TICKS_SIZE (f) = ready_ticks_size;
774 FENCE_CYCLE (f) = cycle;
776 else
778 /* Leave DC and CYCLE untouched. */
779 delete_deps_context (dc);
780 VEC_free (rtx, gc, executing_insns);
781 free (ready_ticks);
785 /* Fill remaining invariant fields. */
786 if (after_stall_p)
787 FENCE_AFTER_STALL_P (f) = 1;
789 FENCE_ISSUED_INSNS (f) = 0;
790 FENCE_STARTS_CYCLE_P (f) = 1;
791 FENCE_SCHED_NEXT (f) = NULL;
794 /* Add a new fence to NEW_FENCES list, initializing it from all
795 other parameters. */
796 static void
797 add_to_fences (flist_tail_t new_fences, insn_t insn,
798 state_t state, deps_t dc, void *tc, rtx last_scheduled_insn,
799 VEC(rtx, gc) *executing_insns, int *ready_ticks,
800 int ready_ticks_size, rtx sched_next, int cycle,
801 int cycle_issued_insns, bool starts_cycle_p, bool after_stall_p)
803 fence_t f = flist_lookup (FLIST_TAIL_HEAD (new_fences), insn);
805 if (! f)
807 flist_add (FLIST_TAIL_TAILP (new_fences), insn, state, dc, tc,
808 last_scheduled_insn, executing_insns, ready_ticks,
809 ready_ticks_size, sched_next, cycle, cycle_issued_insns,
810 starts_cycle_p, after_stall_p);
812 FLIST_TAIL_TAILP (new_fences)
813 = &FLIST_NEXT (*FLIST_TAIL_TAILP (new_fences));
815 else
817 merge_fences (f, insn, state, dc, tc, last_scheduled_insn,
818 executing_insns, ready_ticks, ready_ticks_size,
819 sched_next, cycle, after_stall_p);
823 /* Move the first fence in the OLD_FENCES list to NEW_FENCES. */
824 void
825 move_fence_to_fences (flist_t old_fences, flist_tail_t new_fences)
827 fence_t f, old;
828 flist_t *tailp = FLIST_TAIL_TAILP (new_fences);
830 old = FLIST_FENCE (old_fences);
831 f = flist_lookup (FLIST_TAIL_HEAD (new_fences),
832 FENCE_INSN (FLIST_FENCE (old_fences)));
833 if (f)
835 merge_fences (f, old->insn, old->state, old->dc, old->tc,
836 old->last_scheduled_insn, old->executing_insns,
837 old->ready_ticks, old->ready_ticks_size,
838 old->sched_next, old->cycle,
839 old->after_stall_p);
841 else
843 _list_add (tailp);
844 FLIST_TAIL_TAILP (new_fences) = &FLIST_NEXT (*tailp);
845 *FLIST_FENCE (*tailp) = *old;
846 init_fence_for_scheduling (FLIST_FENCE (*tailp));
848 FENCE_INSN (old) = NULL;
851 /* Add a new fence to NEW_FENCES list and initialize most of its data
852 as a clean one. */
853 void
854 add_clean_fence_to_fences (flist_tail_t new_fences, insn_t succ, fence_t fence)
856 int ready_ticks_size = get_max_uid () + 1;
858 add_to_fences (new_fences,
859 succ, state_create (), create_deps_context (),
860 create_target_context (true),
861 NULL_RTX, NULL,
862 XCNEWVEC (int, ready_ticks_size), ready_ticks_size,
863 NULL_RTX, FENCE_CYCLE (fence) + 1,
864 0, 1, FENCE_AFTER_STALL_P (fence));
867 /* Add a new fence to NEW_FENCES list and initialize all of its data
868 from FENCE and SUCC. */
869 void
870 add_dirty_fence_to_fences (flist_tail_t new_fences, insn_t succ, fence_t fence)
872 int * new_ready_ticks
873 = XNEWVEC (int, FENCE_READY_TICKS_SIZE (fence));
875 memcpy (new_ready_ticks, FENCE_READY_TICKS (fence),
876 FENCE_READY_TICKS_SIZE (fence) * sizeof (int));
877 add_to_fences (new_fences,
878 succ, state_create_copy (FENCE_STATE (fence)),
879 create_copy_of_deps_context (FENCE_DC (fence)),
880 create_copy_of_target_context (FENCE_TC (fence)),
881 FENCE_LAST_SCHEDULED_INSN (fence),
882 VEC_copy (rtx, gc, FENCE_EXECUTING_INSNS (fence)),
883 new_ready_ticks,
884 FENCE_READY_TICKS_SIZE (fence),
885 FENCE_SCHED_NEXT (fence),
886 FENCE_CYCLE (fence),
887 FENCE_ISSUED_INSNS (fence),
888 FENCE_STARTS_CYCLE_P (fence),
889 FENCE_AFTER_STALL_P (fence));
893 /* Functions to work with regset and nop pools. */
895 /* Returns the new regset from pool. It might have some of the bits set
896 from the previous usage. */
897 regset
898 get_regset_from_pool (void)
900 regset rs;
902 if (regset_pool.n != 0)
903 rs = regset_pool.v[--regset_pool.n];
904 else
905 /* We need to create the regset. */
907 rs = ALLOC_REG_SET (&reg_obstack);
909 if (regset_pool.nn == regset_pool.ss)
910 regset_pool.vv = XRESIZEVEC (regset, regset_pool.vv,
911 (regset_pool.ss = 2 * regset_pool.ss + 1));
912 regset_pool.vv[regset_pool.nn++] = rs;
915 regset_pool.diff++;
917 return rs;
920 /* Same as above, but returns the empty regset. */
921 regset
922 get_clear_regset_from_pool (void)
924 regset rs = get_regset_from_pool ();
926 CLEAR_REG_SET (rs);
927 return rs;
930 /* Return regset RS to the pool for future use. */
931 void
932 return_regset_to_pool (regset rs)
934 regset_pool.diff--;
936 if (regset_pool.n == regset_pool.s)
937 regset_pool.v = XRESIZEVEC (regset, regset_pool.v,
938 (regset_pool.s = 2 * regset_pool.s + 1));
939 regset_pool.v[regset_pool.n++] = rs;
942 #ifdef ENABLE_CHECKING
943 /* This is used as a qsort callback for sorting regset pool stacks.
944 X and XX are addresses of two regsets. They are never equal. */
945 static int
946 cmp_v_in_regset_pool (const void *x, const void *xx)
948 return *((const regset *) x) - *((const regset *) xx);
950 #endif
952 /* Free the regset pool possibly checking for memory leaks. */
953 void
954 free_regset_pool (void)
956 #ifdef ENABLE_CHECKING
958 regset *v = regset_pool.v;
959 int i = 0;
960 int n = regset_pool.n;
962 regset *vv = regset_pool.vv;
963 int ii = 0;
964 int nn = regset_pool.nn;
966 int diff = 0;
968 gcc_assert (n <= nn);
970 /* Sort both vectors so it will be possible to compare them. */
971 qsort (v, n, sizeof (*v), cmp_v_in_regset_pool);
972 qsort (vv, nn, sizeof (*vv), cmp_v_in_regset_pool);
974 while (ii < nn)
976 if (v[i] == vv[ii])
977 i++;
978 else
979 /* VV[II] was lost. */
980 diff++;
982 ii++;
985 gcc_assert (diff == regset_pool.diff);
987 #endif
989 /* If not true - we have a memory leak. */
990 gcc_assert (regset_pool.diff == 0);
992 while (regset_pool.n)
994 --regset_pool.n;
995 FREE_REG_SET (regset_pool.v[regset_pool.n]);
998 free (regset_pool.v);
999 regset_pool.v = NULL;
1000 regset_pool.s = 0;
1002 free (regset_pool.vv);
1003 regset_pool.vv = NULL;
1004 regset_pool.nn = 0;
1005 regset_pool.ss = 0;
1007 regset_pool.diff = 0;
1011 /* Functions to work with nop pools. NOP insns are used as temporary
1012 placeholders of the insns being scheduled to allow correct update of
1013 the data sets. When update is finished, NOPs are deleted. */
1015 /* A vinsn that is used to represent a nop. This vinsn is shared among all
1016 nops sel-sched generates. */
1017 static vinsn_t nop_vinsn = NULL;
1019 /* Emit a nop before INSN, taking it from pool. */
1020 insn_t
1021 get_nop_from_pool (insn_t insn)
1023 insn_t nop;
1024 bool old_p = nop_pool.n != 0;
1025 int flags;
1027 if (old_p)
1028 nop = nop_pool.v[--nop_pool.n];
1029 else
1030 nop = nop_pattern;
1032 nop = emit_insn_before (nop, insn);
1034 if (old_p)
1035 flags = INSN_INIT_TODO_SSID;
1036 else
1037 flags = INSN_INIT_TODO_LUID | INSN_INIT_TODO_SSID;
1039 set_insn_init (INSN_EXPR (insn), nop_vinsn, INSN_SEQNO (insn));
1040 sel_init_new_insn (nop, flags);
1042 return nop;
1045 /* Remove NOP from the instruction stream and return it to the pool. */
1046 void
1047 return_nop_to_pool (insn_t nop)
1049 gcc_assert (INSN_IN_STREAM_P (nop));
1050 sel_remove_insn (nop, false, true);
1052 if (nop_pool.n == nop_pool.s)
1053 nop_pool.v = XRESIZEVEC (rtx, nop_pool.v,
1054 (nop_pool.s = 2 * nop_pool.s + 1));
1055 nop_pool.v[nop_pool.n++] = nop;
1058 /* Free the nop pool. */
1059 void
1060 free_nop_pool (void)
1062 nop_pool.n = 0;
1063 nop_pool.s = 0;
1064 free (nop_pool.v);
1065 nop_pool.v = NULL;
1069 /* Skip unspec to support ia64 speculation. Called from rtx_equal_p_cb.
1070 The callback is given two rtxes XX and YY and writes the new rtxes
1071 to NX and NY in case some needs to be skipped. */
1072 static int
1073 skip_unspecs_callback (const_rtx *xx, const_rtx *yy, rtx *nx, rtx* ny)
1075 const_rtx x = *xx;
1076 const_rtx y = *yy;
1078 if (GET_CODE (x) == UNSPEC
1079 && (targetm.sched.skip_rtx_p == NULL
1080 || targetm.sched.skip_rtx_p (x)))
1082 *nx = XVECEXP (x, 0, 0);
1083 *ny = CONST_CAST_RTX (y);
1084 return 1;
1087 if (GET_CODE (y) == UNSPEC
1088 && (targetm.sched.skip_rtx_p == NULL
1089 || targetm.sched.skip_rtx_p (y)))
1091 *nx = CONST_CAST_RTX (x);
1092 *ny = XVECEXP (y, 0, 0);
1093 return 1;
1096 return 0;
1099 /* Callback, called from hash_rtx_cb. Helps to hash UNSPEC rtx X in a correct way
1100 to support ia64 speculation. When changes are needed, new rtx X and new mode
1101 NMODE are written, and the callback returns true. */
1102 static int
1103 hash_with_unspec_callback (const_rtx x, enum machine_mode mode ATTRIBUTE_UNUSED,
1104 rtx *nx, enum machine_mode* nmode)
1106 if (GET_CODE (x) == UNSPEC
1107 && targetm.sched.skip_rtx_p
1108 && targetm.sched.skip_rtx_p (x))
1110 *nx = XVECEXP (x, 0 ,0);
1111 *nmode = VOIDmode;
1112 return 1;
1115 return 0;
1118 /* Returns LHS and RHS are ok to be scheduled separately. */
1119 static bool
1120 lhs_and_rhs_separable_p (rtx lhs, rtx rhs)
1122 if (lhs == NULL || rhs == NULL)
1123 return false;
1125 /* Do not schedule CONST, CONST_INT and CONST_DOUBLE etc as rhs: no point
1126 to use reg, if const can be used. Moreover, scheduling const as rhs may
1127 lead to mode mismatch cause consts don't have modes but they could be
1128 merged from branches where the same const used in different modes. */
1129 if (CONSTANT_P (rhs))
1130 return false;
1132 /* ??? Do not rename predicate registers to avoid ICEs in bundling. */
1133 if (COMPARISON_P (rhs))
1134 return false;
1136 /* Do not allow single REG to be an rhs. */
1137 if (REG_P (rhs))
1138 return false;
1140 /* See comment at find_used_regs_1 (*1) for explanation of this
1141 restriction. */
1142 /* FIXME: remove this later. */
1143 if (MEM_P (lhs))
1144 return false;
1146 /* This will filter all tricky things like ZERO_EXTRACT etc.
1147 For now we don't handle it. */
1148 if (!REG_P (lhs) && !MEM_P (lhs))
1149 return false;
1151 return true;
1154 /* Initialize vinsn VI for INSN. Only for use from vinsn_create (). When
1155 FORCE_UNIQUE_P is true, the resulting vinsn will not be clonable. This is
1156 used e.g. for insns from recovery blocks. */
1157 static void
1158 vinsn_init (vinsn_t vi, insn_t insn, bool force_unique_p)
1160 hash_rtx_callback_function hrcf;
1161 int insn_class;
1163 VINSN_INSN_RTX (vi) = insn;
1164 VINSN_COUNT (vi) = 0;
1165 vi->cost = -1;
1167 if (DF_INSN_UID_SAFE_GET (INSN_UID (insn)) != NULL)
1168 init_id_from_df (VINSN_ID (vi), insn, force_unique_p);
1169 else
1170 deps_init_id (VINSN_ID (vi), insn, force_unique_p);
1172 /* Hash vinsn depending on whether it is separable or not. */
1173 hrcf = targetm.sched.skip_rtx_p ? hash_with_unspec_callback : NULL;
1174 if (VINSN_SEPARABLE_P (vi))
1176 rtx rhs = VINSN_RHS (vi);
1178 VINSN_HASH (vi) = hash_rtx_cb (rhs, GET_MODE (rhs),
1179 NULL, NULL, false, hrcf);
1180 VINSN_HASH_RTX (vi) = hash_rtx_cb (VINSN_PATTERN (vi),
1181 VOIDmode, NULL, NULL,
1182 false, hrcf);
1184 else
1186 VINSN_HASH (vi) = hash_rtx_cb (VINSN_PATTERN (vi), VOIDmode,
1187 NULL, NULL, false, hrcf);
1188 VINSN_HASH_RTX (vi) = VINSN_HASH (vi);
1191 insn_class = haifa_classify_insn (insn);
1192 if (insn_class >= 2
1193 && (!targetm.sched.get_insn_spec_ds
1194 || ((targetm.sched.get_insn_spec_ds (insn) & BEGIN_CONTROL)
1195 == 0)))
1196 VINSN_MAY_TRAP_P (vi) = true;
1197 else
1198 VINSN_MAY_TRAP_P (vi) = false;
1201 /* Indicate that VI has become the part of an rtx object. */
1202 void
1203 vinsn_attach (vinsn_t vi)
1205 /* Assert that VI is not pending for deletion. */
1206 gcc_assert (VINSN_INSN_RTX (vi));
1208 VINSN_COUNT (vi)++;
1211 /* Create and init VI from the INSN. Use UNIQUE_P for determining the correct
1212 VINSN_TYPE (VI). */
1213 static vinsn_t
1214 vinsn_create (insn_t insn, bool force_unique_p)
1216 vinsn_t vi = XCNEW (struct vinsn_def);
1218 vinsn_init (vi, insn, force_unique_p);
1219 return vi;
1222 /* Return a copy of VI. When REATTACH_P is true, detach VI and attach
1223 the copy. */
1224 vinsn_t
1225 vinsn_copy (vinsn_t vi, bool reattach_p)
1227 rtx copy;
1228 bool unique = VINSN_UNIQUE_P (vi);
1229 vinsn_t new_vi;
1231 copy = create_copy_of_insn_rtx (VINSN_INSN_RTX (vi));
1232 new_vi = create_vinsn_from_insn_rtx (copy, unique);
1233 if (reattach_p)
1235 vinsn_detach (vi);
1236 vinsn_attach (new_vi);
1239 return new_vi;
1242 /* Delete the VI vinsn and free its data. */
1243 static void
1244 vinsn_delete (vinsn_t vi)
1246 gcc_assert (VINSN_COUNT (vi) == 0);
1248 return_regset_to_pool (VINSN_REG_SETS (vi));
1249 return_regset_to_pool (VINSN_REG_USES (vi));
1250 return_regset_to_pool (VINSN_REG_CLOBBERS (vi));
1252 free (vi);
1255 /* Indicate that VI is no longer a part of some rtx object.
1256 Remove VI if it is no longer needed. */
1257 void
1258 vinsn_detach (vinsn_t vi)
1260 gcc_assert (VINSN_COUNT (vi) > 0);
1262 if (--VINSN_COUNT (vi) == 0)
1263 vinsn_delete (vi);
1266 /* Returns TRUE if VI is a branch. */
1267 bool
1268 vinsn_cond_branch_p (vinsn_t vi)
1270 insn_t insn;
1272 if (!VINSN_UNIQUE_P (vi))
1273 return false;
1275 insn = VINSN_INSN_RTX (vi);
1276 if (BB_END (BLOCK_FOR_INSN (insn)) != insn)
1277 return false;
1279 return control_flow_insn_p (insn);
1282 /* Return latency of INSN. */
1283 static int
1284 sel_insn_rtx_cost (rtx insn)
1286 int cost;
1288 /* A USE insn, or something else we don't need to
1289 understand. We can't pass these directly to
1290 result_ready_cost or insn_default_latency because it will
1291 trigger a fatal error for unrecognizable insns. */
1292 if (recog_memoized (insn) < 0)
1293 cost = 0;
1294 else
1296 cost = insn_default_latency (insn);
1298 if (cost < 0)
1299 cost = 0;
1302 return cost;
1305 /* Return the cost of the VI.
1306 !!! FIXME: Unify with haifa-sched.c: insn_cost (). */
1308 sel_vinsn_cost (vinsn_t vi)
1310 int cost = vi->cost;
1312 if (cost < 0)
1314 cost = sel_insn_rtx_cost (VINSN_INSN_RTX (vi));
1315 vi->cost = cost;
1318 return cost;
1322 /* Functions for insn emitting. */
1324 /* Emit new insn after AFTER based on PATTERN and initialize its data from
1325 EXPR and SEQNO. */
1326 insn_t
1327 sel_gen_insn_from_rtx_after (rtx pattern, expr_t expr, int seqno, insn_t after)
1329 insn_t new_insn;
1331 gcc_assert (EXPR_TARGET_AVAILABLE (expr) == true);
1333 new_insn = emit_insn_after (pattern, after);
1334 set_insn_init (expr, NULL, seqno);
1335 sel_init_new_insn (new_insn, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SSID);
1337 return new_insn;
1340 /* Force newly generated vinsns to be unique. */
1341 static bool init_insn_force_unique_p = false;
1343 /* Emit new speculation recovery insn after AFTER based on PATTERN and
1344 initialize its data from EXPR and SEQNO. */
1345 insn_t
1346 sel_gen_recovery_insn_from_rtx_after (rtx pattern, expr_t expr, int seqno,
1347 insn_t after)
1349 insn_t insn;
1351 gcc_assert (!init_insn_force_unique_p);
1353 init_insn_force_unique_p = true;
1354 insn = sel_gen_insn_from_rtx_after (pattern, expr, seqno, after);
1355 CANT_MOVE (insn) = 1;
1356 init_insn_force_unique_p = false;
1358 return insn;
1361 /* Emit new insn after AFTER based on EXPR and SEQNO. If VINSN is not NULL,
1362 take it as a new vinsn instead of EXPR's vinsn.
1363 We simplify insns later, after scheduling region in
1364 simplify_changed_insns. */
1365 insn_t
1366 sel_gen_insn_from_expr_after (expr_t expr, vinsn_t vinsn, int seqno,
1367 insn_t after)
1369 expr_t emit_expr;
1370 insn_t insn;
1371 int flags;
1373 emit_expr = set_insn_init (expr, vinsn ? vinsn : EXPR_VINSN (expr),
1374 seqno);
1375 insn = EXPR_INSN_RTX (emit_expr);
1376 add_insn_after (insn, after, BLOCK_FOR_INSN (insn));
1378 flags = INSN_INIT_TODO_SSID;
1379 if (INSN_LUID (insn) == 0)
1380 flags |= INSN_INIT_TODO_LUID;
1381 sel_init_new_insn (insn, flags);
1383 return insn;
1386 /* Move insn from EXPR after AFTER. */
1387 insn_t
1388 sel_move_insn (expr_t expr, int seqno, insn_t after)
1390 insn_t insn = EXPR_INSN_RTX (expr);
1391 basic_block bb = BLOCK_FOR_INSN (after);
1392 insn_t next = NEXT_INSN (after);
1394 /* Assert that in move_op we disconnected this insn properly. */
1395 gcc_assert (EXPR_VINSN (INSN_EXPR (insn)) != NULL);
1396 PREV_INSN (insn) = after;
1397 NEXT_INSN (insn) = next;
1399 NEXT_INSN (after) = insn;
1400 PREV_INSN (next) = insn;
1402 /* Update links from insn to bb and vice versa. */
1403 df_insn_change_bb (insn, bb);
1404 if (BB_END (bb) == after)
1405 BB_END (bb) = insn;
1407 prepare_insn_expr (insn, seqno);
1408 return insn;
1412 /* Functions to work with right-hand sides. */
1414 /* Search for a hash value determined by UID/NEW_VINSN in a sorted vector
1415 VECT and return true when found. Use NEW_VINSN for comparison only when
1416 COMPARE_VINSNS is true. Write to INDP the index on which
1417 the search has stopped, such that inserting the new element at INDP will
1418 retain VECT's sort order. */
1419 static bool
1420 find_in_history_vect_1 (VEC(expr_history_def, heap) *vect,
1421 unsigned uid, vinsn_t new_vinsn,
1422 bool compare_vinsns, int *indp)
1424 expr_history_def *arr;
1425 int i, j, len = VEC_length (expr_history_def, vect);
1427 if (len == 0)
1429 *indp = 0;
1430 return false;
1433 arr = VEC_address (expr_history_def, vect);
1434 i = 0, j = len - 1;
1436 while (i <= j)
1438 unsigned auid = arr[i].uid;
1439 vinsn_t avinsn = arr[i].new_expr_vinsn;
1441 if (auid == uid
1442 /* When undoing transformation on a bookkeeping copy, the new vinsn
1443 may not be exactly equal to the one that is saved in the vector.
1444 This is because the insn whose copy we're checking was possibly
1445 substituted itself. */
1446 && (! compare_vinsns
1447 || vinsn_equal_p (avinsn, new_vinsn)))
1449 *indp = i;
1450 return true;
1452 else if (auid > uid)
1453 break;
1454 i++;
1457 *indp = i;
1458 return false;
1461 /* Search for a uid of INSN and NEW_VINSN in a sorted vector VECT. Return
1462 the position found or -1, if no such value is in vector.
1463 Search also for UIDs of insn's originators, if ORIGINATORS_P is true. */
1465 find_in_history_vect (VEC(expr_history_def, heap) *vect, rtx insn,
1466 vinsn_t new_vinsn, bool originators_p)
1468 int ind;
1470 if (find_in_history_vect_1 (vect, INSN_UID (insn), new_vinsn,
1471 false, &ind))
1472 return ind;
1474 if (INSN_ORIGINATORS (insn) && originators_p)
1476 unsigned uid;
1477 bitmap_iterator bi;
1479 EXECUTE_IF_SET_IN_BITMAP (INSN_ORIGINATORS (insn), 0, uid, bi)
1480 if (find_in_history_vect_1 (vect, uid, new_vinsn, false, &ind))
1481 return ind;
1484 return -1;
1487 /* Insert new element in a sorted history vector pointed to by PVECT,
1488 if it is not there already. The element is searched using
1489 UID/NEW_EXPR_VINSN pair. TYPE, OLD_EXPR_VINSN and SPEC_DS save
1490 the history of a transformation. */
1491 void
1492 insert_in_history_vect (VEC (expr_history_def, heap) **pvect,
1493 unsigned uid, enum local_trans_type type,
1494 vinsn_t old_expr_vinsn, vinsn_t new_expr_vinsn,
1495 ds_t spec_ds)
1497 VEC(expr_history_def, heap) *vect = *pvect;
1498 expr_history_def temp;
1499 bool res;
1500 int ind;
1502 res = find_in_history_vect_1 (vect, uid, new_expr_vinsn, true, &ind);
1504 if (res)
1506 expr_history_def *phist = VEC_index (expr_history_def, vect, ind);
1508 /* It is possible that speculation types of expressions that were
1509 propagated through different paths will be different here. In this
1510 case, merge the status to get the correct check later. */
1511 if (phist->spec_ds != spec_ds)
1512 phist->spec_ds = ds_max_merge (phist->spec_ds, spec_ds);
1513 return;
1516 temp.uid = uid;
1517 temp.old_expr_vinsn = old_expr_vinsn;
1518 temp.new_expr_vinsn = new_expr_vinsn;
1519 temp.spec_ds = spec_ds;
1520 temp.type = type;
1522 vinsn_attach (old_expr_vinsn);
1523 vinsn_attach (new_expr_vinsn);
1524 VEC_safe_insert (expr_history_def, heap, vect, ind, &temp);
1525 *pvect = vect;
1528 /* Free history vector PVECT. */
1529 static void
1530 free_history_vect (VEC (expr_history_def, heap) **pvect)
1532 unsigned i;
1533 expr_history_def *phist;
1535 if (! *pvect)
1536 return;
1538 for (i = 0;
1539 VEC_iterate (expr_history_def, *pvect, i, phist);
1540 i++)
1542 vinsn_detach (phist->old_expr_vinsn);
1543 vinsn_detach (phist->new_expr_vinsn);
1546 VEC_free (expr_history_def, heap, *pvect);
1547 *pvect = NULL;
1551 /* Compare two vinsns as rhses if possible and as vinsns otherwise. */
1552 bool
1553 vinsn_equal_p (vinsn_t x, vinsn_t y)
1555 rtx_equal_p_callback_function repcf;
1557 if (x == y)
1558 return true;
1560 if (VINSN_TYPE (x) != VINSN_TYPE (y))
1561 return false;
1563 if (VINSN_HASH (x) != VINSN_HASH (y))
1564 return false;
1566 repcf = targetm.sched.skip_rtx_p ? skip_unspecs_callback : NULL;
1567 if (VINSN_SEPARABLE_P (x))
1569 /* Compare RHSes of VINSNs. */
1570 gcc_assert (VINSN_RHS (x));
1571 gcc_assert (VINSN_RHS (y));
1573 return rtx_equal_p_cb (VINSN_RHS (x), VINSN_RHS (y), repcf);
1576 return rtx_equal_p_cb (VINSN_PATTERN (x), VINSN_PATTERN (y), repcf);
1580 /* Functions for working with expressions. */
1582 /* Initialize EXPR. */
1583 static void
1584 init_expr (expr_t expr, vinsn_t vi, int spec, int use, int priority,
1585 int sched_times, int orig_bb_index, ds_t spec_done_ds,
1586 ds_t spec_to_check_ds, int orig_sched_cycle,
1587 VEC(expr_history_def, heap) *history, bool target_available,
1588 bool was_substituted, bool was_renamed, bool needs_spec_check_p,
1589 bool cant_move)
1591 vinsn_attach (vi);
1593 EXPR_VINSN (expr) = vi;
1594 EXPR_SPEC (expr) = spec;
1595 EXPR_USEFULNESS (expr) = use;
1596 EXPR_PRIORITY (expr) = priority;
1597 EXPR_PRIORITY_ADJ (expr) = 0;
1598 EXPR_SCHED_TIMES (expr) = sched_times;
1599 EXPR_ORIG_BB_INDEX (expr) = orig_bb_index;
1600 EXPR_ORIG_SCHED_CYCLE (expr) = orig_sched_cycle;
1601 EXPR_SPEC_DONE_DS (expr) = spec_done_ds;
1602 EXPR_SPEC_TO_CHECK_DS (expr) = spec_to_check_ds;
1604 if (history)
1605 EXPR_HISTORY_OF_CHANGES (expr) = history;
1606 else
1607 EXPR_HISTORY_OF_CHANGES (expr) = NULL;
1609 EXPR_TARGET_AVAILABLE (expr) = target_available;
1610 EXPR_WAS_SUBSTITUTED (expr) = was_substituted;
1611 EXPR_WAS_RENAMED (expr) = was_renamed;
1612 EXPR_NEEDS_SPEC_CHECK_P (expr) = needs_spec_check_p;
1613 EXPR_CANT_MOVE (expr) = cant_move;
1616 /* Make a copy of the expr FROM into the expr TO. */
1617 void
1618 copy_expr (expr_t to, expr_t from)
1620 VEC(expr_history_def, heap) *temp = NULL;
1622 if (EXPR_HISTORY_OF_CHANGES (from))
1624 unsigned i;
1625 expr_history_def *phist;
1627 temp = VEC_copy (expr_history_def, heap, EXPR_HISTORY_OF_CHANGES (from));
1628 for (i = 0;
1629 VEC_iterate (expr_history_def, temp, i, phist);
1630 i++)
1632 vinsn_attach (phist->old_expr_vinsn);
1633 vinsn_attach (phist->new_expr_vinsn);
1637 init_expr (to, EXPR_VINSN (from), EXPR_SPEC (from),
1638 EXPR_USEFULNESS (from), EXPR_PRIORITY (from),
1639 EXPR_SCHED_TIMES (from), EXPR_ORIG_BB_INDEX (from),
1640 EXPR_SPEC_DONE_DS (from), EXPR_SPEC_TO_CHECK_DS (from),
1641 EXPR_ORIG_SCHED_CYCLE (from), temp,
1642 EXPR_TARGET_AVAILABLE (from), EXPR_WAS_SUBSTITUTED (from),
1643 EXPR_WAS_RENAMED (from), EXPR_NEEDS_SPEC_CHECK_P (from),
1644 EXPR_CANT_MOVE (from));
1647 /* Same, but the final expr will not ever be in av sets, so don't copy
1648 "uninteresting" data such as bitmap cache. */
1649 void
1650 copy_expr_onside (expr_t to, expr_t from)
1652 init_expr (to, EXPR_VINSN (from), EXPR_SPEC (from), EXPR_USEFULNESS (from),
1653 EXPR_PRIORITY (from), EXPR_SCHED_TIMES (from), 0,
1654 EXPR_SPEC_DONE_DS (from), EXPR_SPEC_TO_CHECK_DS (from), 0, NULL,
1655 EXPR_TARGET_AVAILABLE (from), EXPR_WAS_SUBSTITUTED (from),
1656 EXPR_WAS_RENAMED (from), EXPR_NEEDS_SPEC_CHECK_P (from),
1657 EXPR_CANT_MOVE (from));
1660 /* Prepare the expr of INSN for scheduling. Used when moving insn and when
1661 initializing new insns. */
1662 static void
1663 prepare_insn_expr (insn_t insn, int seqno)
1665 expr_t expr = INSN_EXPR (insn);
1666 ds_t ds;
1668 INSN_SEQNO (insn) = seqno;
1669 EXPR_ORIG_BB_INDEX (expr) = BLOCK_NUM (insn);
1670 EXPR_SPEC (expr) = 0;
1671 EXPR_ORIG_SCHED_CYCLE (expr) = 0;
1672 EXPR_WAS_SUBSTITUTED (expr) = 0;
1673 EXPR_WAS_RENAMED (expr) = 0;
1674 EXPR_TARGET_AVAILABLE (expr) = 1;
1675 INSN_LIVE_VALID_P (insn) = false;
1677 /* ??? If this expression is speculative, make its dependence
1678 as weak as possible. We can filter this expression later
1679 in process_spec_exprs, because we do not distinguish
1680 between the status we got during compute_av_set and the
1681 existing status. To be fixed. */
1682 ds = EXPR_SPEC_DONE_DS (expr);
1683 if (ds)
1684 EXPR_SPEC_DONE_DS (expr) = ds_get_max_dep_weak (ds);
1686 free_history_vect (&EXPR_HISTORY_OF_CHANGES (expr));
1689 /* Update target_available bits when merging exprs TO and FROM. SPLIT_POINT
1690 is non-null when expressions are merged from different successors at
1691 a split point. */
1692 static void
1693 update_target_availability (expr_t to, expr_t from, insn_t split_point)
1695 if (EXPR_TARGET_AVAILABLE (to) < 0
1696 || EXPR_TARGET_AVAILABLE (from) < 0)
1697 EXPR_TARGET_AVAILABLE (to) = -1;
1698 else
1700 /* We try to detect the case when one of the expressions
1701 can only be reached through another one. In this case,
1702 we can do better. */
1703 if (split_point == NULL)
1705 int toind, fromind;
1707 toind = EXPR_ORIG_BB_INDEX (to);
1708 fromind = EXPR_ORIG_BB_INDEX (from);
1710 if (toind && toind == fromind)
1711 /* Do nothing -- everything is done in
1712 merge_with_other_exprs. */
1714 else
1715 EXPR_TARGET_AVAILABLE (to) = -1;
1717 else
1718 EXPR_TARGET_AVAILABLE (to) &= EXPR_TARGET_AVAILABLE (from);
1722 /* Update speculation bits when merging exprs TO and FROM. SPLIT_POINT
1723 is non-null when expressions are merged from different successors at
1724 a split point. */
1725 static void
1726 update_speculative_bits (expr_t to, expr_t from, insn_t split_point)
1728 ds_t old_to_ds, old_from_ds;
1730 old_to_ds = EXPR_SPEC_DONE_DS (to);
1731 old_from_ds = EXPR_SPEC_DONE_DS (from);
1733 EXPR_SPEC_DONE_DS (to) = ds_max_merge (old_to_ds, old_from_ds);
1734 EXPR_SPEC_TO_CHECK_DS (to) |= EXPR_SPEC_TO_CHECK_DS (from);
1735 EXPR_NEEDS_SPEC_CHECK_P (to) |= EXPR_NEEDS_SPEC_CHECK_P (from);
1737 /* When merging e.g. control & data speculative exprs, or a control
1738 speculative with a control&data speculative one, we really have
1739 to change vinsn too. Also, when speculative status is changed,
1740 we also need to record this as a transformation in expr's history. */
1741 if ((old_to_ds & SPECULATIVE) || (old_from_ds & SPECULATIVE))
1743 old_to_ds = ds_get_speculation_types (old_to_ds);
1744 old_from_ds = ds_get_speculation_types (old_from_ds);
1746 if (old_to_ds != old_from_ds)
1748 ds_t record_ds;
1750 /* When both expressions are speculative, we need to change
1751 the vinsn first. */
1752 if ((old_to_ds & SPECULATIVE) && (old_from_ds & SPECULATIVE))
1754 int res;
1756 res = speculate_expr (to, EXPR_SPEC_DONE_DS (to));
1757 gcc_assert (res >= 0);
1760 if (split_point != NULL)
1762 /* Record the change with proper status. */
1763 record_ds = EXPR_SPEC_DONE_DS (to) & SPECULATIVE;
1764 record_ds &= ~(old_to_ds & SPECULATIVE);
1765 record_ds &= ~(old_from_ds & SPECULATIVE);
1767 insert_in_history_vect (&EXPR_HISTORY_OF_CHANGES (to),
1768 INSN_UID (split_point), TRANS_SPECULATION,
1769 EXPR_VINSN (from), EXPR_VINSN (to),
1770 record_ds);
1777 /* Merge bits of FROM expr to TO expr. When SPLIT_POINT is not NULL,
1778 this is done along different paths. */
1779 void
1780 merge_expr_data (expr_t to, expr_t from, insn_t split_point)
1782 int i;
1783 expr_history_def *phist;
1785 /* For now, we just set the spec of resulting expr to be minimum of the specs
1786 of merged exprs. */
1787 if (EXPR_SPEC (to) > EXPR_SPEC (from))
1788 EXPR_SPEC (to) = EXPR_SPEC (from);
1790 if (split_point)
1791 EXPR_USEFULNESS (to) += EXPR_USEFULNESS (from);
1792 else
1793 EXPR_USEFULNESS (to) = MAX (EXPR_USEFULNESS (to),
1794 EXPR_USEFULNESS (from));
1796 if (EXPR_PRIORITY (to) < EXPR_PRIORITY (from))
1797 EXPR_PRIORITY (to) = EXPR_PRIORITY (from);
1799 if (EXPR_SCHED_TIMES (to) > EXPR_SCHED_TIMES (from))
1800 EXPR_SCHED_TIMES (to) = EXPR_SCHED_TIMES (from);
1802 if (EXPR_ORIG_BB_INDEX (to) != EXPR_ORIG_BB_INDEX (from))
1803 EXPR_ORIG_BB_INDEX (to) = 0;
1805 EXPR_ORIG_SCHED_CYCLE (to) = MIN (EXPR_ORIG_SCHED_CYCLE (to),
1806 EXPR_ORIG_SCHED_CYCLE (from));
1808 /* We keep this vector sorted. */
1809 for (i = 0;
1810 VEC_iterate (expr_history_def, EXPR_HISTORY_OF_CHANGES (from),
1811 i, phist);
1812 i++)
1813 insert_in_history_vect (&EXPR_HISTORY_OF_CHANGES (to),
1814 phist->uid, phist->type,
1815 phist->old_expr_vinsn, phist->new_expr_vinsn,
1816 phist->spec_ds);
1818 EXPR_WAS_SUBSTITUTED (to) |= EXPR_WAS_SUBSTITUTED (from);
1819 EXPR_WAS_RENAMED (to) |= EXPR_WAS_RENAMED (from);
1820 EXPR_CANT_MOVE (to) |= EXPR_CANT_MOVE (from);
1822 update_target_availability (to, from, split_point);
1823 update_speculative_bits (to, from, split_point);
1826 /* Merge bits of FROM expr to TO expr. Vinsns in the exprs should be equal
1827 in terms of vinsn_equal_p. SPLIT_POINT is non-null when expressions
1828 are merged from different successors at a split point. */
1829 void
1830 merge_expr (expr_t to, expr_t from, insn_t split_point)
1832 vinsn_t to_vi = EXPR_VINSN (to);
1833 vinsn_t from_vi = EXPR_VINSN (from);
1835 gcc_assert (vinsn_equal_p (to_vi, from_vi));
1837 /* Make sure that speculative pattern is propagated into exprs that
1838 have non-speculative one. This will provide us with consistent
1839 speculative bits and speculative patterns inside expr. */
1840 if (EXPR_SPEC_DONE_DS (to) == 0
1841 && EXPR_SPEC_DONE_DS (from) != 0)
1842 change_vinsn_in_expr (to, EXPR_VINSN (from));
1844 merge_expr_data (to, from, split_point);
1845 gcc_assert (EXPR_USEFULNESS (to) <= REG_BR_PROB_BASE);
1848 /* Clear the information of this EXPR. */
1849 void
1850 clear_expr (expr_t expr)
1853 vinsn_detach (EXPR_VINSN (expr));
1854 EXPR_VINSN (expr) = NULL;
1856 free_history_vect (&EXPR_HISTORY_OF_CHANGES (expr));
1859 /* For a given LV_SET, mark EXPR having unavailable target register. */
1860 static void
1861 set_unavailable_target_for_expr (expr_t expr, regset lv_set)
1863 if (EXPR_SEPARABLE_P (expr))
1865 if (REG_P (EXPR_LHS (expr))
1866 && bitmap_bit_p (lv_set, REGNO (EXPR_LHS (expr))))
1868 /* If it's an insn like r1 = use (r1, ...), and it exists in
1869 different forms in each of the av_sets being merged, we can't say
1870 whether original destination register is available or not.
1871 However, this still works if destination register is not used
1872 in the original expression: if the branch at which LV_SET we're
1873 looking here is not actually 'other branch' in sense that same
1874 expression is available through it (but it can't be determined
1875 at computation stage because of transformations on one of the
1876 branches), it still won't affect the availability.
1877 Liveness of a register somewhere on a code motion path means
1878 it's either read somewhere on a codemotion path, live on
1879 'other' branch, live at the point immediately following
1880 the original operation, or is read by the original operation.
1881 The latter case is filtered out in the condition below.
1882 It still doesn't cover the case when register is defined and used
1883 somewhere within the code motion path, and in this case we could
1884 miss a unifying code motion along both branches using a renamed
1885 register, but it won't affect a code correctness since upon
1886 an actual code motion a bookkeeping code would be generated. */
1887 if (bitmap_bit_p (VINSN_REG_USES (EXPR_VINSN (expr)),
1888 REGNO (EXPR_LHS (expr))))
1889 EXPR_TARGET_AVAILABLE (expr) = -1;
1890 else
1891 EXPR_TARGET_AVAILABLE (expr) = false;
1894 else
1896 unsigned regno;
1897 reg_set_iterator rsi;
1899 EXECUTE_IF_SET_IN_REG_SET (VINSN_REG_SETS (EXPR_VINSN (expr)),
1900 0, regno, rsi)
1901 if (bitmap_bit_p (lv_set, regno))
1903 EXPR_TARGET_AVAILABLE (expr) = false;
1904 break;
1907 EXECUTE_IF_SET_IN_REG_SET (VINSN_REG_CLOBBERS (EXPR_VINSN (expr)),
1908 0, regno, rsi)
1909 if (bitmap_bit_p (lv_set, regno))
1911 EXPR_TARGET_AVAILABLE (expr) = false;
1912 break;
1917 /* Try to make EXPR speculative. Return 1 when EXPR's pattern
1918 or dependence status have changed, 2 when also the target register
1919 became unavailable, 0 if nothing had to be changed. */
1921 speculate_expr (expr_t expr, ds_t ds)
1923 int res;
1924 rtx orig_insn_rtx;
1925 rtx spec_pat;
1926 ds_t target_ds, current_ds;
1928 /* Obtain the status we need to put on EXPR. */
1929 target_ds = (ds & SPECULATIVE);
1930 current_ds = EXPR_SPEC_DONE_DS (expr);
1931 ds = ds_full_merge (current_ds, target_ds, NULL_RTX, NULL_RTX);
1933 orig_insn_rtx = EXPR_INSN_RTX (expr);
1935 res = sched_speculate_insn (orig_insn_rtx, ds, &spec_pat);
1937 switch (res)
1939 case 0:
1940 EXPR_SPEC_DONE_DS (expr) = ds;
1941 return current_ds != ds ? 1 : 0;
1943 case 1:
1945 rtx spec_insn_rtx = create_insn_rtx_from_pattern (spec_pat, NULL_RTX);
1946 vinsn_t spec_vinsn = create_vinsn_from_insn_rtx (spec_insn_rtx, false);
1948 change_vinsn_in_expr (expr, spec_vinsn);
1949 EXPR_SPEC_DONE_DS (expr) = ds;
1950 EXPR_NEEDS_SPEC_CHECK_P (expr) = true;
1952 /* Do not allow clobbering the address register of speculative
1953 insns. */
1954 if (bitmap_bit_p (VINSN_REG_USES (EXPR_VINSN (expr)),
1955 expr_dest_regno (expr)))
1957 EXPR_TARGET_AVAILABLE (expr) = false;
1958 return 2;
1961 return 1;
1964 case -1:
1965 return -1;
1967 default:
1968 gcc_unreachable ();
1969 return -1;
1973 /* Return a destination register, if any, of EXPR. */
1975 expr_dest_reg (expr_t expr)
1977 rtx dest = VINSN_LHS (EXPR_VINSN (expr));
1979 if (dest != NULL_RTX && REG_P (dest))
1980 return dest;
1982 return NULL_RTX;
1985 /* Returns the REGNO of the R's destination. */
1986 unsigned
1987 expr_dest_regno (expr_t expr)
1989 rtx dest = expr_dest_reg (expr);
1991 gcc_assert (dest != NULL_RTX);
1992 return REGNO (dest);
1995 /* For a given LV_SET, mark all expressions in JOIN_SET, but not present in
1996 AV_SET having unavailable target register. */
1997 void
1998 mark_unavailable_targets (av_set_t join_set, av_set_t av_set, regset lv_set)
2000 expr_t expr;
2001 av_set_iterator avi;
2003 FOR_EACH_EXPR (expr, avi, join_set)
2004 if (av_set_lookup (av_set, EXPR_VINSN (expr)) == NULL)
2005 set_unavailable_target_for_expr (expr, lv_set);
2009 /* Av set functions. */
2011 /* Add a new element to av set SETP.
2012 Return the element added. */
2013 static av_set_t
2014 av_set_add_element (av_set_t *setp)
2016 /* Insert at the beginning of the list. */
2017 _list_add (setp);
2018 return *setp;
2021 /* Add EXPR to SETP. */
2022 void
2023 av_set_add (av_set_t *setp, expr_t expr)
2025 av_set_t elem;
2027 gcc_assert (!INSN_NOP_P (EXPR_INSN_RTX (expr)));
2028 elem = av_set_add_element (setp);
2029 copy_expr (_AV_SET_EXPR (elem), expr);
2032 /* Same, but do not copy EXPR. */
2033 static void
2034 av_set_add_nocopy (av_set_t *setp, expr_t expr)
2036 av_set_t elem;
2038 elem = av_set_add_element (setp);
2039 *_AV_SET_EXPR (elem) = *expr;
2042 /* Remove expr pointed to by IP from the av_set. */
2043 void
2044 av_set_iter_remove (av_set_iterator *ip)
2046 clear_expr (_AV_SET_EXPR (*ip->lp));
2047 _list_iter_remove (ip);
2050 /* Search for an expr in SET, such that it's equivalent to SOUGHT_VINSN in the
2051 sense of vinsn_equal_p function. Return NULL if no such expr is
2052 in SET was found. */
2053 expr_t
2054 av_set_lookup (av_set_t set, vinsn_t sought_vinsn)
2056 expr_t expr;
2057 av_set_iterator i;
2059 FOR_EACH_EXPR (expr, i, set)
2060 if (vinsn_equal_p (EXPR_VINSN (expr), sought_vinsn))
2061 return expr;
2062 return NULL;
2065 /* Same, but also remove the EXPR found. */
2066 static expr_t
2067 av_set_lookup_and_remove (av_set_t *setp, vinsn_t sought_vinsn)
2069 expr_t expr;
2070 av_set_iterator i;
2072 FOR_EACH_EXPR_1 (expr, i, setp)
2073 if (vinsn_equal_p (EXPR_VINSN (expr), sought_vinsn))
2075 _list_iter_remove_nofree (&i);
2076 return expr;
2078 return NULL;
2081 /* Search for an expr in SET, such that it's equivalent to EXPR in the
2082 sense of vinsn_equal_p function of their vinsns, but not EXPR itself.
2083 Returns NULL if no such expr is in SET was found. */
2084 static expr_t
2085 av_set_lookup_other_equiv_expr (av_set_t set, expr_t expr)
2087 expr_t cur_expr;
2088 av_set_iterator i;
2090 FOR_EACH_EXPR (cur_expr, i, set)
2092 if (cur_expr == expr)
2093 continue;
2094 if (vinsn_equal_p (EXPR_VINSN (cur_expr), EXPR_VINSN (expr)))
2095 return cur_expr;
2098 return NULL;
2101 /* If other expression is already in AVP, remove one of them. */
2102 expr_t
2103 merge_with_other_exprs (av_set_t *avp, av_set_iterator *ip, expr_t expr)
2105 expr_t expr2;
2107 expr2 = av_set_lookup_other_equiv_expr (*avp, expr);
2108 if (expr2 != NULL)
2110 /* Reset target availability on merge, since taking it only from one
2111 of the exprs would be controversial for different code. */
2112 EXPR_TARGET_AVAILABLE (expr2) = -1;
2113 EXPR_USEFULNESS (expr2) = 0;
2115 merge_expr (expr2, expr, NULL);
2117 /* Fix usefulness as it should be now REG_BR_PROB_BASE. */
2118 EXPR_USEFULNESS (expr2) = REG_BR_PROB_BASE;
2120 av_set_iter_remove (ip);
2121 return expr2;
2124 return expr;
2127 /* Return true if there is an expr that correlates to VI in SET. */
2128 bool
2129 av_set_is_in_p (av_set_t set, vinsn_t vi)
2131 return av_set_lookup (set, vi) != NULL;
2134 /* Return a copy of SET. */
2135 av_set_t
2136 av_set_copy (av_set_t set)
2138 expr_t expr;
2139 av_set_iterator i;
2140 av_set_t res = NULL;
2142 FOR_EACH_EXPR (expr, i, set)
2143 av_set_add (&res, expr);
2145 return res;
2148 /* Join two av sets that do not have common elements by attaching second set
2149 (pointed to by FROMP) to the end of first set (TO_TAILP must point to
2150 _AV_SET_NEXT of first set's last element). */
2151 static void
2152 join_distinct_sets (av_set_t *to_tailp, av_set_t *fromp)
2154 gcc_assert (*to_tailp == NULL);
2155 *to_tailp = *fromp;
2156 *fromp = NULL;
2159 /* Makes set pointed to by TO to be the union of TO and FROM. Clear av_set
2160 pointed to by FROMP afterwards. */
2161 void
2162 av_set_union_and_clear (av_set_t *top, av_set_t *fromp, insn_t insn)
2164 expr_t expr1;
2165 av_set_iterator i;
2167 /* Delete from TOP all exprs, that present in FROMP. */
2168 FOR_EACH_EXPR_1 (expr1, i, top)
2170 expr_t expr2 = av_set_lookup (*fromp, EXPR_VINSN (expr1));
2172 if (expr2)
2174 merge_expr (expr2, expr1, insn);
2175 av_set_iter_remove (&i);
2179 join_distinct_sets (i.lp, fromp);
2182 /* Same as above, but also update availability of target register in
2183 TOP judging by TO_LV_SET and FROM_LV_SET. */
2184 void
2185 av_set_union_and_live (av_set_t *top, av_set_t *fromp, regset to_lv_set,
2186 regset from_lv_set, insn_t insn)
2188 expr_t expr1;
2189 av_set_iterator i;
2190 av_set_t *to_tailp, in_both_set = NULL;
2192 /* Delete from TOP all expres, that present in FROMP. */
2193 FOR_EACH_EXPR_1 (expr1, i, top)
2195 expr_t expr2 = av_set_lookup_and_remove (fromp, EXPR_VINSN (expr1));
2197 if (expr2)
2199 /* It may be that the expressions have different destination
2200 registers, in which case we need to check liveness here. */
2201 if (EXPR_SEPARABLE_P (expr1))
2203 int regno1 = (REG_P (EXPR_LHS (expr1))
2204 ? (int) expr_dest_regno (expr1) : -1);
2205 int regno2 = (REG_P (EXPR_LHS (expr2))
2206 ? (int) expr_dest_regno (expr2) : -1);
2208 /* ??? We don't have a way to check restrictions for
2209 *other* register on the current path, we did it only
2210 for the current target register. Give up. */
2211 if (regno1 != regno2)
2212 EXPR_TARGET_AVAILABLE (expr2) = -1;
2214 else if (EXPR_INSN_RTX (expr1) != EXPR_INSN_RTX (expr2))
2215 EXPR_TARGET_AVAILABLE (expr2) = -1;
2217 merge_expr (expr2, expr1, insn);
2218 av_set_add_nocopy (&in_both_set, expr2);
2219 av_set_iter_remove (&i);
2221 else
2222 /* EXPR1 is present in TOP, but not in FROMP. Check it on
2223 FROM_LV_SET. */
2224 set_unavailable_target_for_expr (expr1, from_lv_set);
2226 to_tailp = i.lp;
2228 /* These expressions are not present in TOP. Check liveness
2229 restrictions on TO_LV_SET. */
2230 FOR_EACH_EXPR (expr1, i, *fromp)
2231 set_unavailable_target_for_expr (expr1, to_lv_set);
2233 join_distinct_sets (i.lp, &in_both_set);
2234 join_distinct_sets (to_tailp, fromp);
2237 /* Clear av_set pointed to by SETP. */
2238 void
2239 av_set_clear (av_set_t *setp)
2241 expr_t expr;
2242 av_set_iterator i;
2244 FOR_EACH_EXPR_1 (expr, i, setp)
2245 av_set_iter_remove (&i);
2247 gcc_assert (*setp == NULL);
2250 /* Leave only one non-speculative element in the SETP. */
2251 void
2252 av_set_leave_one_nonspec (av_set_t *setp)
2254 expr_t expr;
2255 av_set_iterator i;
2256 bool has_one_nonspec = false;
2258 /* Keep all speculative exprs, and leave one non-speculative
2259 (the first one). */
2260 FOR_EACH_EXPR_1 (expr, i, setp)
2262 if (!EXPR_SPEC_DONE_DS (expr))
2264 if (has_one_nonspec)
2265 av_set_iter_remove (&i);
2266 else
2267 has_one_nonspec = true;
2272 /* Return the N'th element of the SET. */
2273 expr_t
2274 av_set_element (av_set_t set, int n)
2276 expr_t expr;
2277 av_set_iterator i;
2279 FOR_EACH_EXPR (expr, i, set)
2280 if (n-- == 0)
2281 return expr;
2283 gcc_unreachable ();
2284 return NULL;
2287 /* Deletes all expressions from AVP that are conditional branches (IFs). */
2288 void
2289 av_set_substract_cond_branches (av_set_t *avp)
2291 av_set_iterator i;
2292 expr_t expr;
2294 FOR_EACH_EXPR_1 (expr, i, avp)
2295 if (vinsn_cond_branch_p (EXPR_VINSN (expr)))
2296 av_set_iter_remove (&i);
2299 /* Multiplies usefulness attribute of each member of av-set *AVP by
2300 value PROB / ALL_PROB. */
2301 void
2302 av_set_split_usefulness (av_set_t av, int prob, int all_prob)
2304 av_set_iterator i;
2305 expr_t expr;
2307 FOR_EACH_EXPR (expr, i, av)
2308 EXPR_USEFULNESS (expr) = (all_prob
2309 ? (EXPR_USEFULNESS (expr) * prob) / all_prob
2310 : 0);
2313 /* Leave in AVP only those expressions, which are present in AV,
2314 and return it. */
2315 void
2316 av_set_intersect (av_set_t *avp, av_set_t av)
2318 av_set_iterator i;
2319 expr_t expr;
2321 FOR_EACH_EXPR_1 (expr, i, avp)
2322 if (av_set_lookup (av, EXPR_VINSN (expr)) == NULL)
2323 av_set_iter_remove (&i);
2328 /* Dependence hooks to initialize insn data. */
2330 /* This is used in hooks callable from dependence analysis when initializing
2331 instruction's data. */
2332 static struct
2334 /* Where the dependence was found (lhs/rhs). */
2335 deps_where_t where;
2337 /* The actual data object to initialize. */
2338 idata_t id;
2340 /* True when the insn should not be made clonable. */
2341 bool force_unique_p;
2343 /* True when insn should be treated as of type USE, i.e. never renamed. */
2344 bool force_use_p;
2345 } deps_init_id_data;
2348 /* Setup ID for INSN. FORCE_UNIQUE_P is true when INSN should not be
2349 clonable. */
2350 static void
2351 setup_id_for_insn (idata_t id, insn_t insn, bool force_unique_p)
2353 int type;
2355 /* Determine whether INSN could be cloned and return appropriate vinsn type.
2356 That clonable insns which can be separated into lhs and rhs have type SET.
2357 Other clonable insns have type USE. */
2358 type = GET_CODE (insn);
2360 /* Only regular insns could be cloned. */
2361 if (type == INSN && !force_unique_p)
2362 type = SET;
2363 else if (type == JUMP_INSN && simplejump_p (insn))
2364 type = PC;
2366 IDATA_TYPE (id) = type;
2367 IDATA_REG_SETS (id) = get_clear_regset_from_pool ();
2368 IDATA_REG_USES (id) = get_clear_regset_from_pool ();
2369 IDATA_REG_CLOBBERS (id) = get_clear_regset_from_pool ();
2372 /* Start initializing insn data. */
2373 static void
2374 deps_init_id_start_insn (insn_t insn)
2376 gcc_assert (deps_init_id_data.where == DEPS_IN_NOWHERE);
2378 setup_id_for_insn (deps_init_id_data.id, insn,
2379 deps_init_id_data.force_unique_p);
2380 deps_init_id_data.where = DEPS_IN_INSN;
2383 /* Start initializing lhs data. */
2384 static void
2385 deps_init_id_start_lhs (rtx lhs)
2387 gcc_assert (deps_init_id_data.where == DEPS_IN_INSN);
2388 gcc_assert (IDATA_LHS (deps_init_id_data.id) == NULL);
2390 if (IDATA_TYPE (deps_init_id_data.id) == SET)
2392 IDATA_LHS (deps_init_id_data.id) = lhs;
2393 deps_init_id_data.where = DEPS_IN_LHS;
2397 /* Finish initializing lhs data. */
2398 static void
2399 deps_init_id_finish_lhs (void)
2401 deps_init_id_data.where = DEPS_IN_INSN;
2404 /* Note a set of REGNO. */
2405 static void
2406 deps_init_id_note_reg_set (int regno)
2408 haifa_note_reg_set (regno);
2410 if (deps_init_id_data.where == DEPS_IN_RHS)
2411 deps_init_id_data.force_use_p = true;
2413 if (IDATA_TYPE (deps_init_id_data.id) != PC)
2414 SET_REGNO_REG_SET (IDATA_REG_SETS (deps_init_id_data.id), regno);
2416 #ifdef STACK_REGS
2417 /* Make instructions that set stack registers to be ineligible for
2418 renaming to avoid issues with find_used_regs. */
2419 if (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG))
2420 deps_init_id_data.force_use_p = true;
2421 #endif
2424 /* Note a clobber of REGNO. */
2425 static void
2426 deps_init_id_note_reg_clobber (int regno)
2428 haifa_note_reg_clobber (regno);
2430 if (deps_init_id_data.where == DEPS_IN_RHS)
2431 deps_init_id_data.force_use_p = true;
2433 if (IDATA_TYPE (deps_init_id_data.id) != PC)
2434 SET_REGNO_REG_SET (IDATA_REG_CLOBBERS (deps_init_id_data.id), regno);
2437 /* Note a use of REGNO. */
2438 static void
2439 deps_init_id_note_reg_use (int regno)
2441 haifa_note_reg_use (regno);
2443 if (IDATA_TYPE (deps_init_id_data.id) != PC)
2444 SET_REGNO_REG_SET (IDATA_REG_USES (deps_init_id_data.id), regno);
2447 /* Start initializing rhs data. */
2448 static void
2449 deps_init_id_start_rhs (rtx rhs)
2451 gcc_assert (deps_init_id_data.where == DEPS_IN_INSN);
2453 /* And there was no sel_deps_reset_to_insn (). */
2454 if (IDATA_LHS (deps_init_id_data.id) != NULL)
2456 IDATA_RHS (deps_init_id_data.id) = rhs;
2457 deps_init_id_data.where = DEPS_IN_RHS;
2461 /* Finish initializing rhs data. */
2462 static void
2463 deps_init_id_finish_rhs (void)
2465 gcc_assert (deps_init_id_data.where == DEPS_IN_RHS
2466 || deps_init_id_data.where == DEPS_IN_INSN);
2467 deps_init_id_data.where = DEPS_IN_INSN;
2470 /* Finish initializing insn data. */
2471 static void
2472 deps_init_id_finish_insn (void)
2474 gcc_assert (deps_init_id_data.where == DEPS_IN_INSN);
2476 if (IDATA_TYPE (deps_init_id_data.id) == SET)
2478 rtx lhs = IDATA_LHS (deps_init_id_data.id);
2479 rtx rhs = IDATA_RHS (deps_init_id_data.id);
2481 if (lhs == NULL || rhs == NULL || !lhs_and_rhs_separable_p (lhs, rhs)
2482 || deps_init_id_data.force_use_p)
2484 /* This should be a USE, as we don't want to schedule its RHS
2485 separately. However, we still want to have them recorded
2486 for the purposes of substitution. That's why we don't
2487 simply call downgrade_to_use () here. */
2488 gcc_assert (IDATA_TYPE (deps_init_id_data.id) == SET);
2489 gcc_assert (!lhs == !rhs);
2491 IDATA_TYPE (deps_init_id_data.id) = USE;
2495 deps_init_id_data.where = DEPS_IN_NOWHERE;
2498 /* This is dependence info used for initializing insn's data. */
2499 static struct sched_deps_info_def deps_init_id_sched_deps_info;
2501 /* This initializes most of the static part of the above structure. */
2502 static const struct sched_deps_info_def const_deps_init_id_sched_deps_info =
2504 NULL,
2506 deps_init_id_start_insn,
2507 deps_init_id_finish_insn,
2508 deps_init_id_start_lhs,
2509 deps_init_id_finish_lhs,
2510 deps_init_id_start_rhs,
2511 deps_init_id_finish_rhs,
2512 deps_init_id_note_reg_set,
2513 deps_init_id_note_reg_clobber,
2514 deps_init_id_note_reg_use,
2515 NULL, /* note_mem_dep */
2516 NULL, /* note_dep */
2518 0, /* use_cselib */
2519 0, /* use_deps_list */
2520 0 /* generate_spec_deps */
2523 /* Initialize INSN's lhs and rhs in ID. When FORCE_UNIQUE_P is true,
2524 we don't actually need information about lhs and rhs. */
2525 static void
2526 setup_id_lhs_rhs (idata_t id, insn_t insn, bool force_unique_p)
2528 rtx pat = PATTERN (insn);
2530 if (NONJUMP_INSN_P (insn)
2531 && GET_CODE (pat) == SET
2532 && !force_unique_p)
2534 IDATA_RHS (id) = SET_SRC (pat);
2535 IDATA_LHS (id) = SET_DEST (pat);
2537 else
2538 IDATA_LHS (id) = IDATA_RHS (id) = NULL;
2541 /* Possibly downgrade INSN to USE. */
2542 static void
2543 maybe_downgrade_id_to_use (idata_t id, insn_t insn)
2545 bool must_be_use = false;
2546 unsigned uid = INSN_UID (insn);
2547 df_ref *rec;
2548 rtx lhs = IDATA_LHS (id);
2549 rtx rhs = IDATA_RHS (id);
2551 /* We downgrade only SETs. */
2552 if (IDATA_TYPE (id) != SET)
2553 return;
2555 if (!lhs || !lhs_and_rhs_separable_p (lhs, rhs))
2557 IDATA_TYPE (id) = USE;
2558 return;
2561 for (rec = DF_INSN_UID_DEFS (uid); *rec; rec++)
2563 df_ref def = *rec;
2565 if (DF_REF_INSN (def)
2566 && DF_REF_FLAGS_IS_SET (def, DF_REF_PRE_POST_MODIFY)
2567 && loc_mentioned_in_p (DF_REF_LOC (def), IDATA_RHS (id)))
2569 must_be_use = true;
2570 break;
2573 #ifdef STACK_REGS
2574 /* Make instructions that set stack registers to be ineligible for
2575 renaming to avoid issues with find_used_regs. */
2576 if (IN_RANGE (DF_REF_REGNO (def), FIRST_STACK_REG, LAST_STACK_REG))
2578 must_be_use = true;
2579 break;
2581 #endif
2584 if (must_be_use)
2585 IDATA_TYPE (id) = USE;
2588 /* Setup register sets describing INSN in ID. */
2589 static void
2590 setup_id_reg_sets (idata_t id, insn_t insn)
2592 unsigned uid = INSN_UID (insn);
2593 df_ref *rec;
2594 regset tmp = get_clear_regset_from_pool ();
2596 for (rec = DF_INSN_UID_DEFS (uid); *rec; rec++)
2598 df_ref def = *rec;
2599 unsigned int regno = DF_REF_REGNO (def);
2601 /* Post modifies are treated like clobbers by sched-deps.c. */
2602 if (DF_REF_FLAGS_IS_SET (def, (DF_REF_MUST_CLOBBER
2603 | DF_REF_PRE_POST_MODIFY)))
2604 SET_REGNO_REG_SET (IDATA_REG_CLOBBERS (id), regno);
2605 else if (! DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
2607 SET_REGNO_REG_SET (IDATA_REG_SETS (id), regno);
2609 #ifdef STACK_REGS
2610 /* For stack registers, treat writes to them as writes
2611 to the first one to be consistent with sched-deps.c. */
2612 if (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG))
2613 SET_REGNO_REG_SET (IDATA_REG_SETS (id), FIRST_STACK_REG);
2614 #endif
2616 /* Mark special refs that generate read/write def pair. */
2617 if (DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)
2618 || regno == STACK_POINTER_REGNUM)
2619 bitmap_set_bit (tmp, regno);
2622 for (rec = DF_INSN_UID_USES (uid); *rec; rec++)
2624 df_ref use = *rec;
2625 unsigned int regno = DF_REF_REGNO (use);
2627 /* When these refs are met for the first time, skip them, as
2628 these uses are just counterparts of some defs. */
2629 if (bitmap_bit_p (tmp, regno))
2630 bitmap_clear_bit (tmp, regno);
2631 else if (! DF_REF_FLAGS_IS_SET (use, DF_REF_CALL_STACK_USAGE))
2633 SET_REGNO_REG_SET (IDATA_REG_USES (id), regno);
2635 #ifdef STACK_REGS
2636 /* For stack registers, treat reads from them as reads from
2637 the first one to be consistent with sched-deps.c. */
2638 if (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG))
2639 SET_REGNO_REG_SET (IDATA_REG_USES (id), FIRST_STACK_REG);
2640 #endif
2644 return_regset_to_pool (tmp);
2647 /* Initialize instruction data for INSN in ID using DF's data. */
2648 static void
2649 init_id_from_df (idata_t id, insn_t insn, bool force_unique_p)
2651 gcc_assert (DF_INSN_UID_SAFE_GET (INSN_UID (insn)) != NULL);
2653 setup_id_for_insn (id, insn, force_unique_p);
2654 setup_id_lhs_rhs (id, insn, force_unique_p);
2656 if (INSN_NOP_P (insn))
2657 return;
2659 maybe_downgrade_id_to_use (id, insn);
2660 setup_id_reg_sets (id, insn);
2663 /* Initialize instruction data for INSN in ID. */
2664 static void
2665 deps_init_id (idata_t id, insn_t insn, bool force_unique_p)
2667 struct deps _dc, *dc = &_dc;
2669 deps_init_id_data.where = DEPS_IN_NOWHERE;
2670 deps_init_id_data.id = id;
2671 deps_init_id_data.force_unique_p = force_unique_p;
2672 deps_init_id_data.force_use_p = false;
2674 init_deps (dc);
2676 memcpy (&deps_init_id_sched_deps_info,
2677 &const_deps_init_id_sched_deps_info,
2678 sizeof (deps_init_id_sched_deps_info));
2680 if (spec_info != NULL)
2681 deps_init_id_sched_deps_info.generate_spec_deps = 1;
2683 sched_deps_info = &deps_init_id_sched_deps_info;
2685 deps_analyze_insn (dc, insn);
2687 free_deps (dc);
2689 deps_init_id_data.id = NULL;
2694 /* Implement hooks for collecting fundamental insn properties like if insn is
2695 an ASM or is within a SCHED_GROUP. */
2697 /* True when a "one-time init" data for INSN was already inited. */
2698 static bool
2699 first_time_insn_init (insn_t insn)
2701 return INSN_LIVE (insn) == NULL;
2704 /* Hash an entry in a transformed_insns hashtable. */
2705 static hashval_t
2706 hash_transformed_insns (const void *p)
2708 return VINSN_HASH_RTX (((const struct transformed_insns *) p)->vinsn_old);
2711 /* Compare the entries in a transformed_insns hashtable. */
2712 static int
2713 eq_transformed_insns (const void *p, const void *q)
2715 rtx i1 = VINSN_INSN_RTX (((const struct transformed_insns *) p)->vinsn_old);
2716 rtx i2 = VINSN_INSN_RTX (((const struct transformed_insns *) q)->vinsn_old);
2718 if (INSN_UID (i1) == INSN_UID (i2))
2719 return 1;
2720 return rtx_equal_p (PATTERN (i1), PATTERN (i2));
2723 /* Free an entry in a transformed_insns hashtable. */
2724 static void
2725 free_transformed_insns (void *p)
2727 struct transformed_insns *pti = (struct transformed_insns *) p;
2729 vinsn_detach (pti->vinsn_old);
2730 vinsn_detach (pti->vinsn_new);
2731 free (pti);
2734 /* Init the s_i_d data for INSN which should be inited just once, when
2735 we first see the insn. */
2736 static void
2737 init_first_time_insn_data (insn_t insn)
2739 /* This should not be set if this is the first time we init data for
2740 insn. */
2741 gcc_assert (first_time_insn_init (insn));
2743 /* These are needed for nops too. */
2744 INSN_LIVE (insn) = get_regset_from_pool ();
2745 INSN_LIVE_VALID_P (insn) = false;
2747 if (!INSN_NOP_P (insn))
2749 INSN_ANALYZED_DEPS (insn) = BITMAP_ALLOC (NULL);
2750 INSN_FOUND_DEPS (insn) = BITMAP_ALLOC (NULL);
2751 INSN_TRANSFORMED_INSNS (insn)
2752 = htab_create (16, hash_transformed_insns,
2753 eq_transformed_insns, free_transformed_insns);
2754 init_deps (&INSN_DEPS_CONTEXT (insn));
2758 /* Free the same data as above for INSN. */
2759 static void
2760 free_first_time_insn_data (insn_t insn)
2762 gcc_assert (! first_time_insn_init (insn));
2764 BITMAP_FREE (INSN_ANALYZED_DEPS (insn));
2765 BITMAP_FREE (INSN_FOUND_DEPS (insn));
2766 htab_delete (INSN_TRANSFORMED_INSNS (insn));
2767 return_regset_to_pool (INSN_LIVE (insn));
2768 INSN_LIVE (insn) = NULL;
2769 INSN_LIVE_VALID_P (insn) = false;
2771 /* This is allocated only for bookkeeping insns. */
2772 if (INSN_ORIGINATORS (insn))
2773 BITMAP_FREE (INSN_ORIGINATORS (insn));
2774 free_deps (&INSN_DEPS_CONTEXT (insn));
2777 /* Initialize region-scope data structures for basic blocks. */
2778 static void
2779 init_global_and_expr_for_bb (basic_block bb)
2781 if (sel_bb_empty_p (bb))
2782 return;
2784 invalidate_av_set (bb);
2787 /* Data for global dependency analysis (to initialize CANT_MOVE and
2788 SCHED_GROUP_P). */
2789 static struct
2791 /* Previous insn. */
2792 insn_t prev_insn;
2793 } init_global_data;
2795 /* Determine if INSN is in the sched_group, is an asm or should not be
2796 cloned. After that initialize its expr. */
2797 static void
2798 init_global_and_expr_for_insn (insn_t insn)
2800 if (LABEL_P (insn))
2801 return;
2803 if (NOTE_INSN_BASIC_BLOCK_P (insn))
2805 init_global_data.prev_insn = NULL_RTX;
2806 return;
2809 gcc_assert (INSN_P (insn));
2811 if (SCHED_GROUP_P (insn))
2812 /* Setup a sched_group. */
2814 insn_t prev_insn = init_global_data.prev_insn;
2816 if (prev_insn)
2817 INSN_SCHED_NEXT (prev_insn) = insn;
2819 init_global_data.prev_insn = insn;
2821 else
2822 init_global_data.prev_insn = NULL_RTX;
2824 if (GET_CODE (PATTERN (insn)) == ASM_INPUT
2825 || asm_noperands (PATTERN (insn)) >= 0)
2826 /* Mark INSN as an asm. */
2827 INSN_ASM_P (insn) = true;
2830 bool force_unique_p;
2831 ds_t spec_done_ds;
2833 /* Certain instructions cannot be cloned. */
2834 if (CANT_MOVE (insn)
2835 || INSN_ASM_P (insn)
2836 || SCHED_GROUP_P (insn)
2837 || prologue_epilogue_contains (insn)
2838 /* Exception handling insns are always unique. */
2839 || (flag_non_call_exceptions && can_throw_internal (insn))
2840 /* TRAP_IF though have an INSN code is control_flow_insn_p (). */
2841 || control_flow_insn_p (insn))
2842 force_unique_p = true;
2843 else
2844 force_unique_p = false;
2846 if (targetm.sched.get_insn_spec_ds)
2848 spec_done_ds = targetm.sched.get_insn_spec_ds (insn);
2849 spec_done_ds = ds_get_max_dep_weak (spec_done_ds);
2851 else
2852 spec_done_ds = 0;
2854 /* Initialize INSN's expr. */
2855 init_expr (INSN_EXPR (insn), vinsn_create (insn, force_unique_p), 0,
2856 REG_BR_PROB_BASE, INSN_PRIORITY (insn), 0, BLOCK_NUM (insn),
2857 spec_done_ds, 0, 0, NULL, true, false, false, false,
2858 CANT_MOVE (insn));
2861 init_first_time_insn_data (insn);
2864 /* Scan the region and initialize instruction data for basic blocks BBS. */
2865 void
2866 sel_init_global_and_expr (bb_vec_t bbs)
2868 /* ??? It would be nice to implement push / pop scheme for sched_infos. */
2869 const struct sched_scan_info_def ssi =
2871 NULL, /* extend_bb */
2872 init_global_and_expr_for_bb, /* init_bb */
2873 extend_insn_data, /* extend_insn */
2874 init_global_and_expr_for_insn /* init_insn */
2877 sched_scan (&ssi, bbs, NULL, NULL, NULL);
2880 /* Finalize region-scope data structures for basic blocks. */
2881 static void
2882 finish_global_and_expr_for_bb (basic_block bb)
2884 av_set_clear (&BB_AV_SET (bb));
2885 BB_AV_LEVEL (bb) = 0;
2888 /* Finalize INSN's data. */
2889 static void
2890 finish_global_and_expr_insn (insn_t insn)
2892 if (LABEL_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
2893 return;
2895 gcc_assert (INSN_P (insn));
2897 if (INSN_LUID (insn) > 0)
2899 free_first_time_insn_data (insn);
2900 INSN_WS_LEVEL (insn) = 0;
2901 CANT_MOVE (insn) = 0;
2903 /* We can no longer assert this, as vinsns of this insn could be
2904 easily live in other insn's caches. This should be changed to
2905 a counter-like approach among all vinsns. */
2906 gcc_assert (true || VINSN_COUNT (INSN_VINSN (insn)) == 1);
2907 clear_expr (INSN_EXPR (insn));
2911 /* Finalize per instruction data for the whole region. */
2912 void
2913 sel_finish_global_and_expr (void)
2916 bb_vec_t bbs;
2917 int i;
2919 bbs = VEC_alloc (basic_block, heap, current_nr_blocks);
2921 for (i = 0; i < current_nr_blocks; i++)
2922 VEC_quick_push (basic_block, bbs, BASIC_BLOCK (BB_TO_BLOCK (i)));
2924 /* Clear AV_SETs and INSN_EXPRs. */
2926 const struct sched_scan_info_def ssi =
2928 NULL, /* extend_bb */
2929 finish_global_and_expr_for_bb, /* init_bb */
2930 NULL, /* extend_insn */
2931 finish_global_and_expr_insn /* init_insn */
2934 sched_scan (&ssi, bbs, NULL, NULL, NULL);
2937 VEC_free (basic_block, heap, bbs);
2940 finish_insns ();
2944 /* In the below hooks, we merely calculate whether or not a dependence
2945 exists, and in what part of insn. However, we will need more data
2946 when we'll start caching dependence requests. */
2948 /* Container to hold information for dependency analysis. */
2949 static struct
2951 deps_t dc;
2953 /* A variable to track which part of rtx we are scanning in
2954 sched-deps.c: sched_analyze_insn (). */
2955 deps_where_t where;
2957 /* Current producer. */
2958 insn_t pro;
2960 /* Current consumer. */
2961 vinsn_t con;
2963 /* Is SEL_DEPS_HAS_DEP_P[DEPS_IN_X] is true, then X has a dependence.
2964 X is from { INSN, LHS, RHS }. */
2965 ds_t has_dep_p[DEPS_IN_NOWHERE];
2966 } has_dependence_data;
2968 /* Start analyzing dependencies of INSN. */
2969 static void
2970 has_dependence_start_insn (insn_t insn ATTRIBUTE_UNUSED)
2972 gcc_assert (has_dependence_data.where == DEPS_IN_NOWHERE);
2974 has_dependence_data.where = DEPS_IN_INSN;
2977 /* Finish analyzing dependencies of an insn. */
2978 static void
2979 has_dependence_finish_insn (void)
2981 gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
2983 has_dependence_data.where = DEPS_IN_NOWHERE;
2986 /* Start analyzing dependencies of LHS. */
2987 static void
2988 has_dependence_start_lhs (rtx lhs ATTRIBUTE_UNUSED)
2990 gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
2992 if (VINSN_LHS (has_dependence_data.con) != NULL)
2993 has_dependence_data.where = DEPS_IN_LHS;
2996 /* Finish analyzing dependencies of an lhs. */
2997 static void
2998 has_dependence_finish_lhs (void)
3000 has_dependence_data.where = DEPS_IN_INSN;
3003 /* Start analyzing dependencies of RHS. */
3004 static void
3005 has_dependence_start_rhs (rtx rhs ATTRIBUTE_UNUSED)
3007 gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
3009 if (VINSN_RHS (has_dependence_data.con) != NULL)
3010 has_dependence_data.where = DEPS_IN_RHS;
3013 /* Start analyzing dependencies of an rhs. */
3014 static void
3015 has_dependence_finish_rhs (void)
3017 gcc_assert (has_dependence_data.where == DEPS_IN_RHS
3018 || has_dependence_data.where == DEPS_IN_INSN);
3020 has_dependence_data.where = DEPS_IN_INSN;
3023 /* Note a set of REGNO. */
3024 static void
3025 has_dependence_note_reg_set (int regno)
3027 struct deps_reg *reg_last = &has_dependence_data.dc->reg_last[regno];
3029 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3030 VINSN_INSN_RTX
3031 (has_dependence_data.con)))
3033 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3035 if (reg_last->sets != NULL
3036 || reg_last->clobbers != NULL)
3037 *dsp = (*dsp & ~SPECULATIVE) | DEP_OUTPUT;
3039 if (reg_last->uses)
3040 *dsp = (*dsp & ~SPECULATIVE) | DEP_ANTI;
3044 /* Note a clobber of REGNO. */
3045 static void
3046 has_dependence_note_reg_clobber (int regno)
3048 struct deps_reg *reg_last = &has_dependence_data.dc->reg_last[regno];
3050 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3051 VINSN_INSN_RTX
3052 (has_dependence_data.con)))
3054 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3056 if (reg_last->sets)
3057 *dsp = (*dsp & ~SPECULATIVE) | DEP_OUTPUT;
3059 if (reg_last->uses)
3060 *dsp = (*dsp & ~SPECULATIVE) | DEP_ANTI;
3064 /* Note a use of REGNO. */
3065 static void
3066 has_dependence_note_reg_use (int regno)
3068 struct deps_reg *reg_last = &has_dependence_data.dc->reg_last[regno];
3070 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3071 VINSN_INSN_RTX
3072 (has_dependence_data.con)))
3074 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3076 if (reg_last->sets)
3077 *dsp = (*dsp & ~SPECULATIVE) | DEP_TRUE;
3079 if (reg_last->clobbers)
3080 *dsp = (*dsp & ~SPECULATIVE) | DEP_ANTI;
3082 /* Handle BE_IN_SPEC. */
3083 if (reg_last->uses)
3085 ds_t pro_spec_checked_ds;
3087 pro_spec_checked_ds = INSN_SPEC_CHECKED_DS (has_dependence_data.pro);
3088 pro_spec_checked_ds = ds_get_max_dep_weak (pro_spec_checked_ds);
3090 if (pro_spec_checked_ds != 0)
3091 /* Merge BE_IN_SPEC bits into *DSP. */
3092 *dsp = ds_full_merge (*dsp, pro_spec_checked_ds,
3093 NULL_RTX, NULL_RTX);
3098 /* Note a memory dependence. */
3099 static void
3100 has_dependence_note_mem_dep (rtx mem ATTRIBUTE_UNUSED,
3101 rtx pending_mem ATTRIBUTE_UNUSED,
3102 insn_t pending_insn ATTRIBUTE_UNUSED,
3103 ds_t ds ATTRIBUTE_UNUSED)
3105 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3106 VINSN_INSN_RTX (has_dependence_data.con)))
3108 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3110 *dsp = ds_full_merge (ds, *dsp, pending_mem, mem);
3114 /* Note a dependence. */
3115 static void
3116 has_dependence_note_dep (insn_t pro ATTRIBUTE_UNUSED,
3117 ds_t ds ATTRIBUTE_UNUSED)
3119 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3120 VINSN_INSN_RTX (has_dependence_data.con)))
3122 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3124 *dsp = ds_full_merge (ds, *dsp, NULL_RTX, NULL_RTX);
3128 /* Mark the insn as having a hard dependence that prevents speculation. */
3129 void
3130 sel_mark_hard_insn (rtx insn)
3132 int i;
3134 /* Only work when we're in has_dependence_p mode.
3135 ??? This is a hack, this should actually be a hook. */
3136 if (!has_dependence_data.dc || !has_dependence_data.pro)
3137 return;
3139 gcc_assert (insn == VINSN_INSN_RTX (has_dependence_data.con));
3140 gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
3142 for (i = 0; i < DEPS_IN_NOWHERE; i++)
3143 has_dependence_data.has_dep_p[i] &= ~SPECULATIVE;
3146 /* This structure holds the hooks for the dependency analysis used when
3147 actually processing dependencies in the scheduler. */
3148 static struct sched_deps_info_def has_dependence_sched_deps_info;
3150 /* This initializes most of the fields of the above structure. */
3151 static const struct sched_deps_info_def const_has_dependence_sched_deps_info =
3153 NULL,
3155 has_dependence_start_insn,
3156 has_dependence_finish_insn,
3157 has_dependence_start_lhs,
3158 has_dependence_finish_lhs,
3159 has_dependence_start_rhs,
3160 has_dependence_finish_rhs,
3161 has_dependence_note_reg_set,
3162 has_dependence_note_reg_clobber,
3163 has_dependence_note_reg_use,
3164 has_dependence_note_mem_dep,
3165 has_dependence_note_dep,
3167 0, /* use_cselib */
3168 0, /* use_deps_list */
3169 0 /* generate_spec_deps */
3172 /* Initialize has_dependence_sched_deps_info with extra spec field. */
3173 static void
3174 setup_has_dependence_sched_deps_info (void)
3176 memcpy (&has_dependence_sched_deps_info,
3177 &const_has_dependence_sched_deps_info,
3178 sizeof (has_dependence_sched_deps_info));
3180 if (spec_info != NULL)
3181 has_dependence_sched_deps_info.generate_spec_deps = 1;
3183 sched_deps_info = &has_dependence_sched_deps_info;
3186 /* Remove all dependences found and recorded in has_dependence_data array. */
3187 void
3188 sel_clear_has_dependence (void)
3190 int i;
3192 for (i = 0; i < DEPS_IN_NOWHERE; i++)
3193 has_dependence_data.has_dep_p[i] = 0;
3196 /* Return nonzero if EXPR has is dependent upon PRED. Return the pointer
3197 to the dependence information array in HAS_DEP_PP. */
3198 ds_t
3199 has_dependence_p (expr_t expr, insn_t pred, ds_t **has_dep_pp)
3201 int i;
3202 ds_t ds;
3203 struct deps *dc;
3205 if (INSN_SIMPLEJUMP_P (pred))
3206 /* Unconditional jump is just a transfer of control flow.
3207 Ignore it. */
3208 return false;
3210 dc = &INSN_DEPS_CONTEXT (pred);
3211 if (!dc->readonly)
3213 has_dependence_data.pro = NULL;
3214 /* Initialize empty dep context with information about PRED. */
3215 advance_deps_context (dc, pred);
3216 dc->readonly = 1;
3219 has_dependence_data.where = DEPS_IN_NOWHERE;
3220 has_dependence_data.pro = pred;
3221 has_dependence_data.con = EXPR_VINSN (expr);
3222 has_dependence_data.dc = dc;
3224 sel_clear_has_dependence ();
3226 /* Now catch all dependencies that would be generated between PRED and
3227 INSN. */
3228 setup_has_dependence_sched_deps_info ();
3229 deps_analyze_insn (dc, EXPR_INSN_RTX (expr));
3230 has_dependence_data.dc = NULL;
3232 /* When a barrier was found, set DEPS_IN_INSN bits. */
3233 if (dc->last_reg_pending_barrier == TRUE_BARRIER)
3234 has_dependence_data.has_dep_p[DEPS_IN_INSN] = DEP_TRUE;
3235 else if (dc->last_reg_pending_barrier == MOVE_BARRIER)
3236 has_dependence_data.has_dep_p[DEPS_IN_INSN] = DEP_ANTI;
3238 /* Do not allow stores to memory to move through checks. Currently
3239 we don't move this to sched-deps.c as the check doesn't have
3240 obvious places to which this dependence can be attached.
3241 FIMXE: this should go to a hook. */
3242 if (EXPR_LHS (expr)
3243 && MEM_P (EXPR_LHS (expr))
3244 && sel_insn_is_speculation_check (pred))
3245 has_dependence_data.has_dep_p[DEPS_IN_INSN] = DEP_ANTI;
3247 *has_dep_pp = has_dependence_data.has_dep_p;
3248 ds = 0;
3249 for (i = 0; i < DEPS_IN_NOWHERE; i++)
3250 ds = ds_full_merge (ds, has_dependence_data.has_dep_p[i],
3251 NULL_RTX, NULL_RTX);
3253 return ds;
3257 /* Dependence hooks implementation that checks dependence latency constraints
3258 on the insns being scheduled. The entry point for these routines is
3259 tick_check_p predicate. */
3261 static struct
3263 /* An expr we are currently checking. */
3264 expr_t expr;
3266 /* A minimal cycle for its scheduling. */
3267 int cycle;
3269 /* Whether we have seen a true dependence while checking. */
3270 bool seen_true_dep_p;
3271 } tick_check_data;
3273 /* Update minimal scheduling cycle for tick_check_insn given that it depends
3274 on PRO with status DS and weight DW. */
3275 static void
3276 tick_check_dep_with_dw (insn_t pro_insn, ds_t ds, dw_t dw)
3278 expr_t con_expr = tick_check_data.expr;
3279 insn_t con_insn = EXPR_INSN_RTX (con_expr);
3281 if (con_insn != pro_insn)
3283 enum reg_note dt;
3284 int tick;
3286 if (/* PROducer was removed from above due to pipelining. */
3287 !INSN_IN_STREAM_P (pro_insn)
3288 /* Or PROducer was originally on the next iteration regarding the
3289 CONsumer. */
3290 || (INSN_SCHED_TIMES (pro_insn)
3291 - EXPR_SCHED_TIMES (con_expr)) > 1)
3292 /* Don't count this dependence. */
3293 return;
3295 dt = ds_to_dt (ds);
3296 if (dt == REG_DEP_TRUE)
3297 tick_check_data.seen_true_dep_p = true;
3299 gcc_assert (INSN_SCHED_CYCLE (pro_insn) > 0);
3302 dep_def _dep, *dep = &_dep;
3304 init_dep (dep, pro_insn, con_insn, dt);
3306 tick = INSN_SCHED_CYCLE (pro_insn) + dep_cost_1 (dep, dw);
3309 /* When there are several kinds of dependencies between pro and con,
3310 only REG_DEP_TRUE should be taken into account. */
3311 if (tick > tick_check_data.cycle
3312 && (dt == REG_DEP_TRUE || !tick_check_data.seen_true_dep_p))
3313 tick_check_data.cycle = tick;
3317 /* An implementation of note_dep hook. */
3318 static void
3319 tick_check_note_dep (insn_t pro, ds_t ds)
3321 tick_check_dep_with_dw (pro, ds, 0);
3324 /* An implementation of note_mem_dep hook. */
3325 static void
3326 tick_check_note_mem_dep (rtx mem1, rtx mem2, insn_t pro, ds_t ds)
3328 dw_t dw;
3330 dw = (ds_to_dt (ds) == REG_DEP_TRUE
3331 ? estimate_dep_weak (mem1, mem2)
3332 : 0);
3334 tick_check_dep_with_dw (pro, ds, dw);
3337 /* This structure contains hooks for dependence analysis used when determining
3338 whether an insn is ready for scheduling. */
3339 static struct sched_deps_info_def tick_check_sched_deps_info =
3341 NULL,
3343 NULL,
3344 NULL,
3345 NULL,
3346 NULL,
3347 NULL,
3348 NULL,
3349 haifa_note_reg_set,
3350 haifa_note_reg_clobber,
3351 haifa_note_reg_use,
3352 tick_check_note_mem_dep,
3353 tick_check_note_dep,
3355 0, 0, 0
3358 /* Estimate number of cycles from the current cycle of FENCE until EXPR can be
3359 scheduled. Return 0 if all data from producers in DC is ready. */
3361 tick_check_p (expr_t expr, deps_t dc, fence_t fence)
3363 int cycles_left;
3364 /* Initialize variables. */
3365 tick_check_data.expr = expr;
3366 tick_check_data.cycle = 0;
3367 tick_check_data.seen_true_dep_p = false;
3368 sched_deps_info = &tick_check_sched_deps_info;
3370 gcc_assert (!dc->readonly);
3371 dc->readonly = 1;
3372 deps_analyze_insn (dc, EXPR_INSN_RTX (expr));
3373 dc->readonly = 0;
3375 cycles_left = tick_check_data.cycle - FENCE_CYCLE (fence);
3377 return cycles_left >= 0 ? cycles_left : 0;
3381 /* Functions to work with insns. */
3383 /* Returns true if LHS of INSN is the same as DEST of an insn
3384 being moved. */
3385 bool
3386 lhs_of_insn_equals_to_dest_p (insn_t insn, rtx dest)
3388 rtx lhs = INSN_LHS (insn);
3390 if (lhs == NULL || dest == NULL)
3391 return false;
3393 return rtx_equal_p (lhs, dest);
3396 /* Return s_i_d entry of INSN. Callable from debugger. */
3397 sel_insn_data_def
3398 insn_sid (insn_t insn)
3400 return *SID (insn);
3403 /* True when INSN is a speculative check. We can tell this by looking
3404 at the data structures of the selective scheduler, not by examining
3405 the pattern. */
3406 bool
3407 sel_insn_is_speculation_check (rtx insn)
3409 return s_i_d && !! INSN_SPEC_CHECKED_DS (insn);
3412 /* Extracts machine mode MODE and destination location DST_LOC
3413 for given INSN. */
3414 void
3415 get_dest_and_mode (rtx insn, rtx *dst_loc, enum machine_mode *mode)
3417 rtx pat = PATTERN (insn);
3419 gcc_assert (dst_loc);
3420 gcc_assert (GET_CODE (pat) == SET);
3422 *dst_loc = SET_DEST (pat);
3424 gcc_assert (*dst_loc);
3425 gcc_assert (MEM_P (*dst_loc) || REG_P (*dst_loc));
3427 if (mode)
3428 *mode = GET_MODE (*dst_loc);
3431 /* Returns true when moving through JUMP will result in bookkeeping
3432 creation. */
3433 bool
3434 bookkeeping_can_be_created_if_moved_through_p (insn_t jump)
3436 insn_t succ;
3437 succ_iterator si;
3439 FOR_EACH_SUCC (succ, si, jump)
3440 if (sel_num_cfg_preds_gt_1 (succ))
3441 return true;
3443 return false;
3446 /* Return 'true' if INSN is the only one in its basic block. */
3447 static bool
3448 insn_is_the_only_one_in_bb_p (insn_t insn)
3450 return sel_bb_head_p (insn) && sel_bb_end_p (insn);
3453 #ifdef ENABLE_CHECKING
3454 /* Check that the region we're scheduling still has at most one
3455 backedge. */
3456 static void
3457 verify_backedges (void)
3459 if (pipelining_p)
3461 int i, n = 0;
3462 edge e;
3463 edge_iterator ei;
3465 for (i = 0; i < current_nr_blocks; i++)
3466 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (BB_TO_BLOCK (i))->succs)
3467 if (in_current_region_p (e->dest)
3468 && BLOCK_TO_BB (e->dest->index) < i)
3469 n++;
3471 gcc_assert (n <= 1);
3474 #endif
3477 /* Functions to work with control flow. */
3479 /* Tidy the possibly empty block BB. */
3480 bool
3481 maybe_tidy_empty_bb (basic_block bb)
3483 basic_block succ_bb, pred_bb;
3484 edge e;
3485 edge_iterator ei;
3486 bool rescan_p;
3488 /* Keep empty bb only if this block immediately precedes EXIT and
3489 has incoming non-fallthrough edge. Otherwise remove it. */
3490 if (!sel_bb_empty_p (bb)
3491 || (single_succ_p (bb)
3492 && single_succ (bb) == EXIT_BLOCK_PTR
3493 && (!single_pred_p (bb)
3494 || !(single_pred_edge (bb)->flags & EDGE_FALLTHRU))))
3495 return false;
3497 /* Do not attempt to redirect complex edges. */
3498 FOR_EACH_EDGE (e, ei, bb->preds)
3499 if (e->flags & EDGE_COMPLEX)
3500 return false;
3502 free_data_sets (bb);
3504 /* Do not delete BB if it has more than one successor.
3505 That can occur when we moving a jump. */
3506 if (!single_succ_p (bb))
3508 gcc_assert (can_merge_blocks_p (bb->prev_bb, bb));
3509 sel_merge_blocks (bb->prev_bb, bb);
3510 return true;
3513 succ_bb = single_succ (bb);
3514 rescan_p = true;
3515 pred_bb = NULL;
3517 /* Redirect all non-fallthru edges to the next bb. */
3518 while (rescan_p)
3520 rescan_p = false;
3522 FOR_EACH_EDGE (e, ei, bb->preds)
3524 pred_bb = e->src;
3526 if (!(e->flags & EDGE_FALLTHRU))
3528 sel_redirect_edge_and_branch (e, succ_bb);
3529 rescan_p = true;
3530 break;
3535 /* If it is possible - merge BB with its predecessor. */
3536 if (can_merge_blocks_p (bb->prev_bb, bb))
3537 sel_merge_blocks (bb->prev_bb, bb);
3538 else
3539 /* Otherwise this is a block without fallthru predecessor.
3540 Just delete it. */
3542 gcc_assert (pred_bb != NULL);
3544 move_bb_info (pred_bb, bb);
3545 remove_empty_bb (bb, true);
3548 #ifdef ENABLE_CHECKING
3549 verify_backedges ();
3550 #endif
3552 return true;
3555 /* Tidy the control flow after we have removed original insn from
3556 XBB. Return true if we have removed some blocks. When FULL_TIDYING
3557 is true, also try to optimize control flow on non-empty blocks. */
3558 bool
3559 tidy_control_flow (basic_block xbb, bool full_tidying)
3561 bool changed = true;
3563 /* First check whether XBB is empty. */
3564 changed = maybe_tidy_empty_bb (xbb);
3565 if (changed || !full_tidying)
3566 return changed;
3568 /* Check if there is a unnecessary jump after insn left. */
3569 if (jump_leads_only_to_bb_p (BB_END (xbb), xbb->next_bb)
3570 && INSN_SCHED_TIMES (BB_END (xbb)) == 0
3571 && !IN_CURRENT_FENCE_P (BB_END (xbb)))
3573 if (sel_remove_insn (BB_END (xbb), false, false))
3574 return true;
3575 tidy_fallthru_edge (EDGE_SUCC (xbb, 0));
3578 /* Check if there is an unnecessary jump in previous basic block leading
3579 to next basic block left after removing INSN from stream.
3580 If it is so, remove that jump and redirect edge to current
3581 basic block (where there was INSN before deletion). This way
3582 when NOP will be deleted several instructions later with its
3583 basic block we will not get a jump to next instruction, which
3584 can be harmful. */
3585 if (sel_bb_head (xbb) == sel_bb_end (xbb)
3586 && !sel_bb_empty_p (xbb)
3587 && INSN_NOP_P (sel_bb_end (xbb))
3588 /* Flow goes fallthru from current block to the next. */
3589 && EDGE_COUNT (xbb->succs) == 1
3590 && (EDGE_SUCC (xbb, 0)->flags & EDGE_FALLTHRU)
3591 /* When successor is an EXIT block, it may not be the next block. */
3592 && single_succ (xbb) != EXIT_BLOCK_PTR
3593 /* And unconditional jump in previous basic block leads to
3594 next basic block of XBB and this jump can be safely removed. */
3595 && in_current_region_p (xbb->prev_bb)
3596 && jump_leads_only_to_bb_p (BB_END (xbb->prev_bb), xbb->next_bb)
3597 && INSN_SCHED_TIMES (BB_END (xbb->prev_bb)) == 0
3598 /* Also this jump is not at the scheduling boundary. */
3599 && !IN_CURRENT_FENCE_P (BB_END (xbb->prev_bb)))
3601 /* Clear data structures of jump - jump itself will be removed
3602 by sel_redirect_edge_and_branch. */
3603 clear_expr (INSN_EXPR (BB_END (xbb->prev_bb)));
3604 sel_redirect_edge_and_branch (EDGE_SUCC (xbb->prev_bb, 0), xbb);
3605 gcc_assert (EDGE_SUCC (xbb->prev_bb, 0)->flags & EDGE_FALLTHRU);
3607 /* It can turn out that after removing unused jump, basic block
3608 that contained that jump, becomes empty too. In such case
3609 remove it too. */
3610 if (sel_bb_empty_p (xbb->prev_bb))
3611 changed = maybe_tidy_empty_bb (xbb->prev_bb);
3614 return changed;
3617 /* Rip-off INSN from the insn stream. When ONLY_DISCONNECT is true,
3618 do not delete insn's data, because it will be later re-emitted.
3619 Return true if we have removed some blocks afterwards. */
3620 bool
3621 sel_remove_insn (insn_t insn, bool only_disconnect, bool full_tidying)
3623 basic_block bb = BLOCK_FOR_INSN (insn);
3625 gcc_assert (INSN_IN_STREAM_P (insn));
3627 if (only_disconnect)
3629 insn_t prev = PREV_INSN (insn);
3630 insn_t next = NEXT_INSN (insn);
3631 basic_block bb = BLOCK_FOR_INSN (insn);
3633 NEXT_INSN (prev) = next;
3634 PREV_INSN (next) = prev;
3636 if (BB_HEAD (bb) == insn)
3638 gcc_assert (BLOCK_FOR_INSN (prev) == bb);
3639 BB_HEAD (bb) = prev;
3641 if (BB_END (bb) == insn)
3642 BB_END (bb) = prev;
3644 else
3646 remove_insn (insn);
3647 clear_expr (INSN_EXPR (insn));
3650 /* It is necessary to null this fields before calling add_insn (). */
3651 PREV_INSN (insn) = NULL_RTX;
3652 NEXT_INSN (insn) = NULL_RTX;
3654 return tidy_control_flow (bb, full_tidying);
3657 /* Estimate number of the insns in BB. */
3658 static int
3659 sel_estimate_number_of_insns (basic_block bb)
3661 int res = 0;
3662 insn_t insn = NEXT_INSN (BB_HEAD (bb)), next_tail = NEXT_INSN (BB_END (bb));
3664 for (; insn != next_tail; insn = NEXT_INSN (insn))
3665 if (INSN_P (insn))
3666 res++;
3668 return res;
3671 /* We don't need separate luids for notes or labels. */
3672 static int
3673 sel_luid_for_non_insn (rtx x)
3675 gcc_assert (NOTE_P (x) || LABEL_P (x));
3677 return -1;
3680 /* Return seqno of the only predecessor of INSN. */
3681 static int
3682 get_seqno_of_a_pred (insn_t insn)
3684 int seqno;
3686 gcc_assert (INSN_SIMPLEJUMP_P (insn));
3688 if (!sel_bb_head_p (insn))
3689 seqno = INSN_SEQNO (PREV_INSN (insn));
3690 else
3692 basic_block bb = BLOCK_FOR_INSN (insn);
3694 if (single_pred_p (bb)
3695 && !in_current_region_p (single_pred (bb)))
3697 /* We can have preds outside a region when splitting edges
3698 for pipelining of an outer loop. Use succ instead.
3699 There should be only one of them. */
3700 insn_t succ = NULL;
3701 succ_iterator si;
3702 bool first = true;
3704 gcc_assert (flag_sel_sched_pipelining_outer_loops
3705 && current_loop_nest);
3706 FOR_EACH_SUCC_1 (succ, si, insn,
3707 SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
3709 gcc_assert (first);
3710 first = false;
3713 gcc_assert (succ != NULL);
3714 seqno = INSN_SEQNO (succ);
3716 else
3718 insn_t *preds;
3719 int n;
3721 cfg_preds (BLOCK_FOR_INSN (insn), &preds, &n);
3722 gcc_assert (n == 1);
3724 seqno = INSN_SEQNO (preds[0]);
3726 free (preds);
3730 return seqno;
3733 /* Find the proper seqno for inserting at INSN. Returns -1 if no predecessors
3734 with positive seqno exist. */
3736 get_seqno_by_preds (rtx insn)
3738 basic_block bb = BLOCK_FOR_INSN (insn);
3739 rtx tmp = insn, head = BB_HEAD (bb);
3740 insn_t *preds;
3741 int n, i, seqno;
3743 while (tmp != head)
3744 if (INSN_P (tmp))
3745 return INSN_SEQNO (tmp);
3746 else
3747 tmp = PREV_INSN (tmp);
3749 cfg_preds (bb, &preds, &n);
3750 for (i = 0, seqno = -1; i < n; i++)
3751 seqno = MAX (seqno, INSN_SEQNO (preds[i]));
3753 return seqno;
3758 /* Extend pass-scope data structures for basic blocks. */
3759 void
3760 sel_extend_global_bb_info (void)
3762 VEC_safe_grow_cleared (sel_global_bb_info_def, heap, sel_global_bb_info,
3763 last_basic_block);
3766 /* Extend region-scope data structures for basic blocks. */
3767 static void
3768 extend_region_bb_info (void)
3770 VEC_safe_grow_cleared (sel_region_bb_info_def, heap, sel_region_bb_info,
3771 last_basic_block);
3774 /* Extend all data structures to fit for all basic blocks. */
3775 static void
3776 extend_bb_info (void)
3778 sel_extend_global_bb_info ();
3779 extend_region_bb_info ();
3782 /* Finalize pass-scope data structures for basic blocks. */
3783 void
3784 sel_finish_global_bb_info (void)
3786 VEC_free (sel_global_bb_info_def, heap, sel_global_bb_info);
3789 /* Finalize region-scope data structures for basic blocks. */
3790 static void
3791 finish_region_bb_info (void)
3793 VEC_free (sel_region_bb_info_def, heap, sel_region_bb_info);
3797 /* Data for each insn in current region. */
3798 VEC (sel_insn_data_def, heap) *s_i_d = NULL;
3800 /* A vector for the insns we've emitted. */
3801 static insn_vec_t new_insns = NULL;
3803 /* Extend data structures for insns from current region. */
3804 static void
3805 extend_insn_data (void)
3807 int reserve;
3809 sched_extend_target ();
3810 sched_deps_init (false);
3812 /* Extend data structures for insns from current region. */
3813 reserve = (sched_max_luid + 1
3814 - VEC_length (sel_insn_data_def, s_i_d));
3815 if (reserve > 0
3816 && ! VEC_space (sel_insn_data_def, s_i_d, reserve))
3817 VEC_safe_grow_cleared (sel_insn_data_def, heap, s_i_d,
3818 3 * sched_max_luid / 2);
3821 /* Finalize data structures for insns from current region. */
3822 static void
3823 finish_insns (void)
3825 unsigned i;
3827 /* Clear here all dependence contexts that may have left from insns that were
3828 removed during the scheduling. */
3829 for (i = 0; i < VEC_length (sel_insn_data_def, s_i_d); i++)
3831 sel_insn_data_def *sid_entry = VEC_index (sel_insn_data_def, s_i_d, i);
3833 if (sid_entry->live)
3834 return_regset_to_pool (sid_entry->live);
3835 if (sid_entry->analyzed_deps)
3837 BITMAP_FREE (sid_entry->analyzed_deps);
3838 BITMAP_FREE (sid_entry->found_deps);
3839 htab_delete (sid_entry->transformed_insns);
3840 free_deps (&sid_entry->deps_context);
3842 if (EXPR_VINSN (&sid_entry->expr))
3844 clear_expr (&sid_entry->expr);
3846 /* Also, clear CANT_MOVE bit here, because we really don't want it
3847 to be passed to the next region. */
3848 CANT_MOVE_BY_LUID (i) = 0;
3852 VEC_free (sel_insn_data_def, heap, s_i_d);
3855 /* A proxy to pass initialization data to init_insn (). */
3856 static sel_insn_data_def _insn_init_ssid;
3857 static sel_insn_data_t insn_init_ssid = &_insn_init_ssid;
3859 /* If true create a new vinsn. Otherwise use the one from EXPR. */
3860 static bool insn_init_create_new_vinsn_p;
3862 /* Set all necessary data for initialization of the new insn[s]. */
3863 static expr_t
3864 set_insn_init (expr_t expr, vinsn_t vi, int seqno)
3866 expr_t x = &insn_init_ssid->expr;
3868 copy_expr_onside (x, expr);
3869 if (vi != NULL)
3871 insn_init_create_new_vinsn_p = false;
3872 change_vinsn_in_expr (x, vi);
3874 else
3875 insn_init_create_new_vinsn_p = true;
3877 insn_init_ssid->seqno = seqno;
3878 return x;
3881 /* Init data for INSN. */
3882 static void
3883 init_insn_data (insn_t insn)
3885 expr_t expr;
3886 sel_insn_data_t ssid = insn_init_ssid;
3888 /* The fields mentioned below are special and hence are not being
3889 propagated to the new insns. */
3890 gcc_assert (!ssid->asm_p && ssid->sched_next == NULL
3891 && !ssid->after_stall_p && ssid->sched_cycle == 0);
3892 gcc_assert (INSN_P (insn) && INSN_LUID (insn) > 0);
3894 expr = INSN_EXPR (insn);
3895 copy_expr (expr, &ssid->expr);
3896 prepare_insn_expr (insn, ssid->seqno);
3898 if (insn_init_create_new_vinsn_p)
3899 change_vinsn_in_expr (expr, vinsn_create (insn, init_insn_force_unique_p));
3901 if (first_time_insn_init (insn))
3902 init_first_time_insn_data (insn);
3905 /* This is used to initialize spurious jumps generated by
3906 sel_redirect_edge (). */
3907 static void
3908 init_simplejump_data (insn_t insn)
3910 init_expr (INSN_EXPR (insn), vinsn_create (insn, false), 0,
3911 REG_BR_PROB_BASE, 0, 0, 0, 0, 0, 0, NULL, true, false, false,
3912 false, true);
3913 INSN_SEQNO (insn) = get_seqno_of_a_pred (insn);
3914 init_first_time_insn_data (insn);
3917 /* Perform deferred initialization of insns. This is used to process
3918 a new jump that may be created by redirect_edge. */
3919 void
3920 sel_init_new_insn (insn_t insn, int flags)
3922 /* We create data structures for bb when the first insn is emitted in it. */
3923 if (INSN_P (insn)
3924 && INSN_IN_STREAM_P (insn)
3925 && insn_is_the_only_one_in_bb_p (insn))
3927 extend_bb_info ();
3928 create_initial_data_sets (BLOCK_FOR_INSN (insn));
3931 if (flags & INSN_INIT_TODO_LUID)
3932 sched_init_luids (NULL, NULL, NULL, insn);
3934 if (flags & INSN_INIT_TODO_SSID)
3936 extend_insn_data ();
3937 init_insn_data (insn);
3938 clear_expr (&insn_init_ssid->expr);
3941 if (flags & INSN_INIT_TODO_SIMPLEJUMP)
3943 extend_insn_data ();
3944 init_simplejump_data (insn);
3947 gcc_assert (CONTAINING_RGN (BLOCK_NUM (insn))
3948 == CONTAINING_RGN (BB_TO_BLOCK (0)));
3952 /* Functions to init/finish work with lv sets. */
3954 /* Init BB_LV_SET of BB from DF_LR_IN set of BB. */
3955 static void
3956 init_lv_set (basic_block bb)
3958 gcc_assert (!BB_LV_SET_VALID_P (bb));
3960 BB_LV_SET (bb) = get_regset_from_pool ();
3961 COPY_REG_SET (BB_LV_SET (bb), DF_LR_IN (bb));
3962 BB_LV_SET_VALID_P (bb) = true;
3965 /* Copy liveness information to BB from FROM_BB. */
3966 static void
3967 copy_lv_set_from (basic_block bb, basic_block from_bb)
3969 gcc_assert (!BB_LV_SET_VALID_P (bb));
3971 COPY_REG_SET (BB_LV_SET (bb), BB_LV_SET (from_bb));
3972 BB_LV_SET_VALID_P (bb) = true;
3975 /* Initialize lv set of all bb headers. */
3976 void
3977 init_lv_sets (void)
3979 basic_block bb;
3981 /* Initialize of LV sets. */
3982 FOR_EACH_BB (bb)
3983 init_lv_set (bb);
3985 /* Don't forget EXIT_BLOCK. */
3986 init_lv_set (EXIT_BLOCK_PTR);
3989 /* Release lv set of HEAD. */
3990 static void
3991 free_lv_set (basic_block bb)
3993 gcc_assert (BB_LV_SET (bb) != NULL);
3995 return_regset_to_pool (BB_LV_SET (bb));
3996 BB_LV_SET (bb) = NULL;
3997 BB_LV_SET_VALID_P (bb) = false;
4000 /* Finalize lv sets of all bb headers. */
4001 void
4002 free_lv_sets (void)
4004 basic_block bb;
4006 /* Don't forget EXIT_BLOCK. */
4007 free_lv_set (EXIT_BLOCK_PTR);
4009 /* Free LV sets. */
4010 FOR_EACH_BB (bb)
4011 if (BB_LV_SET (bb))
4012 free_lv_set (bb);
4015 /* Initialize an invalid AV_SET for BB.
4016 This set will be updated next time compute_av () process BB. */
4017 static void
4018 invalidate_av_set (basic_block bb)
4020 gcc_assert (BB_AV_LEVEL (bb) <= 0
4021 && BB_AV_SET (bb) == NULL);
4023 BB_AV_LEVEL (bb) = -1;
4026 /* Create initial data sets for BB (they will be invalid). */
4027 static void
4028 create_initial_data_sets (basic_block bb)
4030 if (BB_LV_SET (bb))
4031 BB_LV_SET_VALID_P (bb) = false;
4032 else
4033 BB_LV_SET (bb) = get_regset_from_pool ();
4034 invalidate_av_set (bb);
4037 /* Free av set of BB. */
4038 static void
4039 free_av_set (basic_block bb)
4041 av_set_clear (&BB_AV_SET (bb));
4042 BB_AV_LEVEL (bb) = 0;
4045 /* Free data sets of BB. */
4046 void
4047 free_data_sets (basic_block bb)
4049 free_lv_set (bb);
4050 free_av_set (bb);
4053 /* Exchange lv sets of TO and FROM. */
4054 static void
4055 exchange_lv_sets (basic_block to, basic_block from)
4058 regset to_lv_set = BB_LV_SET (to);
4060 BB_LV_SET (to) = BB_LV_SET (from);
4061 BB_LV_SET (from) = to_lv_set;
4065 bool to_lv_set_valid_p = BB_LV_SET_VALID_P (to);
4067 BB_LV_SET_VALID_P (to) = BB_LV_SET_VALID_P (from);
4068 BB_LV_SET_VALID_P (from) = to_lv_set_valid_p;
4073 /* Exchange av sets of TO and FROM. */
4074 static void
4075 exchange_av_sets (basic_block to, basic_block from)
4078 av_set_t to_av_set = BB_AV_SET (to);
4080 BB_AV_SET (to) = BB_AV_SET (from);
4081 BB_AV_SET (from) = to_av_set;
4085 int to_av_level = BB_AV_LEVEL (to);
4087 BB_AV_LEVEL (to) = BB_AV_LEVEL (from);
4088 BB_AV_LEVEL (from) = to_av_level;
4092 /* Exchange data sets of TO and FROM. */
4093 void
4094 exchange_data_sets (basic_block to, basic_block from)
4096 exchange_lv_sets (to, from);
4097 exchange_av_sets (to, from);
4100 /* Copy data sets of FROM to TO. */
4101 void
4102 copy_data_sets (basic_block to, basic_block from)
4104 gcc_assert (!BB_LV_SET_VALID_P (to) && !BB_AV_SET_VALID_P (to));
4105 gcc_assert (BB_AV_SET (to) == NULL);
4107 BB_AV_LEVEL (to) = BB_AV_LEVEL (from);
4108 BB_LV_SET_VALID_P (to) = BB_LV_SET_VALID_P (from);
4110 if (BB_AV_SET_VALID_P (from))
4112 BB_AV_SET (to) = av_set_copy (BB_AV_SET (from));
4114 if (BB_LV_SET_VALID_P (from))
4116 gcc_assert (BB_LV_SET (to) != NULL);
4117 COPY_REG_SET (BB_LV_SET (to), BB_LV_SET (from));
4121 /* Return an av set for INSN, if any. */
4122 av_set_t
4123 get_av_set (insn_t insn)
4125 av_set_t av_set;
4127 gcc_assert (AV_SET_VALID_P (insn));
4129 if (sel_bb_head_p (insn))
4130 av_set = BB_AV_SET (BLOCK_FOR_INSN (insn));
4131 else
4132 av_set = NULL;
4134 return av_set;
4137 /* Implementation of AV_LEVEL () macro. Return AV_LEVEL () of INSN. */
4139 get_av_level (insn_t insn)
4141 int av_level;
4143 gcc_assert (INSN_P (insn));
4145 if (sel_bb_head_p (insn))
4146 av_level = BB_AV_LEVEL (BLOCK_FOR_INSN (insn));
4147 else
4148 av_level = INSN_WS_LEVEL (insn);
4150 return av_level;
4155 /* Variables to work with control-flow graph. */
4157 /* The basic block that already has been processed by the sched_data_update (),
4158 but hasn't been in sel_add_bb () yet. */
4159 static VEC (basic_block, heap) *last_added_blocks = NULL;
4161 /* A pool for allocating successor infos. */
4162 static struct
4164 /* A stack for saving succs_info structures. */
4165 struct succs_info *stack;
4167 /* Its size. */
4168 int size;
4170 /* Top of the stack. */
4171 int top;
4173 /* Maximal value of the top. */
4174 int max_top;
4175 } succs_info_pool;
4177 /* Functions to work with control-flow graph. */
4179 /* Return basic block note of BB. */
4180 insn_t
4181 sel_bb_head (basic_block bb)
4183 insn_t head;
4185 if (bb == EXIT_BLOCK_PTR)
4187 gcc_assert (exit_insn != NULL_RTX);
4188 head = exit_insn;
4190 else
4192 insn_t note;
4194 note = bb_note (bb);
4195 head = next_nonnote_insn (note);
4197 if (head && BLOCK_FOR_INSN (head) != bb)
4198 head = NULL_RTX;
4201 return head;
4204 /* Return true if INSN is a basic block header. */
4205 bool
4206 sel_bb_head_p (insn_t insn)
4208 return sel_bb_head (BLOCK_FOR_INSN (insn)) == insn;
4211 /* Return last insn of BB. */
4212 insn_t
4213 sel_bb_end (basic_block bb)
4215 if (sel_bb_empty_p (bb))
4216 return NULL_RTX;
4218 gcc_assert (bb != EXIT_BLOCK_PTR);
4220 return BB_END (bb);
4223 /* Return true if INSN is the last insn in its basic block. */
4224 bool
4225 sel_bb_end_p (insn_t insn)
4227 return insn == sel_bb_end (BLOCK_FOR_INSN (insn));
4230 /* Return true if BB consist of single NOTE_INSN_BASIC_BLOCK. */
4231 bool
4232 sel_bb_empty_p (basic_block bb)
4234 return sel_bb_head (bb) == NULL;
4237 /* True when BB belongs to the current scheduling region. */
4238 bool
4239 in_current_region_p (basic_block bb)
4241 if (bb->index < NUM_FIXED_BLOCKS)
4242 return false;
4244 return CONTAINING_RGN (bb->index) == CONTAINING_RGN (BB_TO_BLOCK (0));
4247 /* Return the block which is a fallthru bb of a conditional jump JUMP. */
4248 basic_block
4249 fallthru_bb_of_jump (rtx jump)
4251 if (!JUMP_P (jump))
4252 return NULL;
4254 if (any_uncondjump_p (jump))
4255 return single_succ (BLOCK_FOR_INSN (jump));
4257 if (!any_condjump_p (jump))
4258 return NULL;
4260 return FALLTHRU_EDGE (BLOCK_FOR_INSN (jump))->dest;
4263 /* Remove all notes from BB. */
4264 static void
4265 init_bb (basic_block bb)
4267 remove_notes (bb_note (bb), BB_END (bb));
4268 BB_NOTE_LIST (bb) = note_list;
4271 void
4272 sel_init_bbs (bb_vec_t bbs, basic_block bb)
4274 const struct sched_scan_info_def ssi =
4276 extend_bb_info, /* extend_bb */
4277 init_bb, /* init_bb */
4278 NULL, /* extend_insn */
4279 NULL /* init_insn */
4282 sched_scan (&ssi, bbs, bb, new_insns, NULL);
4285 /* Restore other notes for the whole region. */
4286 static void
4287 sel_restore_other_notes (void)
4289 int bb;
4291 for (bb = 0; bb < current_nr_blocks; bb++)
4293 basic_block first, last;
4295 first = EBB_FIRST_BB (bb);
4296 last = EBB_LAST_BB (bb)->next_bb;
4300 note_list = BB_NOTE_LIST (first);
4301 restore_other_notes (NULL, first);
4302 BB_NOTE_LIST (first) = NULL_RTX;
4304 first = first->next_bb;
4306 while (first != last);
4310 /* Free per-bb data structures. */
4311 void
4312 sel_finish_bbs (void)
4314 sel_restore_other_notes ();
4316 /* Remove current loop preheader from this loop. */
4317 if (current_loop_nest)
4318 sel_remove_loop_preheader ();
4320 finish_region_bb_info ();
4323 /* Return true if INSN has a single successor of type FLAGS. */
4324 bool
4325 sel_insn_has_single_succ_p (insn_t insn, int flags)
4327 insn_t succ;
4328 succ_iterator si;
4329 bool first_p = true;
4331 FOR_EACH_SUCC_1 (succ, si, insn, flags)
4333 if (first_p)
4334 first_p = false;
4335 else
4336 return false;
4339 return true;
4342 /* Allocate successor's info. */
4343 static struct succs_info *
4344 alloc_succs_info (void)
4346 if (succs_info_pool.top == succs_info_pool.max_top)
4348 int i;
4350 if (++succs_info_pool.max_top >= succs_info_pool.size)
4351 gcc_unreachable ();
4353 i = ++succs_info_pool.top;
4354 succs_info_pool.stack[i].succs_ok = VEC_alloc (rtx, heap, 10);
4355 succs_info_pool.stack[i].succs_other = VEC_alloc (rtx, heap, 10);
4356 succs_info_pool.stack[i].probs_ok = VEC_alloc (int, heap, 10);
4358 else
4359 succs_info_pool.top++;
4361 return &succs_info_pool.stack[succs_info_pool.top];
4364 /* Free successor's info. */
4365 void
4366 free_succs_info (struct succs_info * sinfo)
4368 gcc_assert (succs_info_pool.top >= 0
4369 && &succs_info_pool.stack[succs_info_pool.top] == sinfo);
4370 succs_info_pool.top--;
4372 /* Clear stale info. */
4373 VEC_block_remove (rtx, sinfo->succs_ok,
4374 0, VEC_length (rtx, sinfo->succs_ok));
4375 VEC_block_remove (rtx, sinfo->succs_other,
4376 0, VEC_length (rtx, sinfo->succs_other));
4377 VEC_block_remove (int, sinfo->probs_ok,
4378 0, VEC_length (int, sinfo->probs_ok));
4379 sinfo->all_prob = 0;
4380 sinfo->succs_ok_n = 0;
4381 sinfo->all_succs_n = 0;
4384 /* Compute successor info for INSN. FLAGS are the flags passed
4385 to the FOR_EACH_SUCC_1 iterator. */
4386 struct succs_info *
4387 compute_succs_info (insn_t insn, short flags)
4389 succ_iterator si;
4390 insn_t succ;
4391 struct succs_info *sinfo = alloc_succs_info ();
4393 /* Traverse *all* successors and decide what to do with each. */
4394 FOR_EACH_SUCC_1 (succ, si, insn, SUCCS_ALL)
4396 /* FIXME: this doesn't work for skipping to loop exits, as we don't
4397 perform code motion through inner loops. */
4398 short current_flags = si.current_flags & ~SUCCS_SKIP_TO_LOOP_EXITS;
4400 if (current_flags & flags)
4402 VEC_safe_push (rtx, heap, sinfo->succs_ok, succ);
4403 VEC_safe_push (int, heap, sinfo->probs_ok,
4404 /* FIXME: Improve calculation when skipping
4405 inner loop to exits. */
4406 (si.bb_end
4407 ? si.e1->probability
4408 : REG_BR_PROB_BASE));
4409 sinfo->succs_ok_n++;
4411 else
4412 VEC_safe_push (rtx, heap, sinfo->succs_other, succ);
4414 /* Compute all_prob. */
4415 if (!si.bb_end)
4416 sinfo->all_prob = REG_BR_PROB_BASE;
4417 else
4418 sinfo->all_prob += si.e1->probability;
4420 sinfo->all_succs_n++;
4423 return sinfo;
4426 /* Return the predecessors of BB in PREDS and their number in N.
4427 Empty blocks are skipped. SIZE is used to allocate PREDS. */
4428 static void
4429 cfg_preds_1 (basic_block bb, insn_t **preds, int *n, int *size)
4431 edge e;
4432 edge_iterator ei;
4434 gcc_assert (BLOCK_TO_BB (bb->index) != 0);
4436 FOR_EACH_EDGE (e, ei, bb->preds)
4438 basic_block pred_bb = e->src;
4439 insn_t bb_end = BB_END (pred_bb);
4441 /* ??? This code is not supposed to walk out of a region. */
4442 gcc_assert (in_current_region_p (pred_bb));
4444 if (sel_bb_empty_p (pred_bb))
4445 cfg_preds_1 (pred_bb, preds, n, size);
4446 else
4448 if (*n == *size)
4449 *preds = XRESIZEVEC (insn_t, *preds,
4450 (*size = 2 * *size + 1));
4451 (*preds)[(*n)++] = bb_end;
4455 gcc_assert (*n != 0);
4458 /* Find all predecessors of BB and record them in PREDS and their number
4459 in N. Empty blocks are skipped, and only normal (forward in-region)
4460 edges are processed. */
4461 static void
4462 cfg_preds (basic_block bb, insn_t **preds, int *n)
4464 int size = 0;
4466 *preds = NULL;
4467 *n = 0;
4468 cfg_preds_1 (bb, preds, n, &size);
4471 /* Returns true if we are moving INSN through join point. */
4472 bool
4473 sel_num_cfg_preds_gt_1 (insn_t insn)
4475 basic_block bb;
4477 if (!sel_bb_head_p (insn) || INSN_BB (insn) == 0)
4478 return false;
4480 bb = BLOCK_FOR_INSN (insn);
4482 while (1)
4484 if (EDGE_COUNT (bb->preds) > 1)
4485 return true;
4487 gcc_assert (EDGE_PRED (bb, 0)->dest == bb);
4488 bb = EDGE_PRED (bb, 0)->src;
4490 if (!sel_bb_empty_p (bb))
4491 break;
4494 return false;
4497 /* Returns true when BB should be the end of an ebb. Adapted from the
4498 code in sched-ebb.c. */
4499 bool
4500 bb_ends_ebb_p (basic_block bb)
4502 basic_block next_bb = bb_next_bb (bb);
4503 edge e;
4504 edge_iterator ei;
4506 if (next_bb == EXIT_BLOCK_PTR
4507 || bitmap_bit_p (forced_ebb_heads, next_bb->index)
4508 || (LABEL_P (BB_HEAD (next_bb))
4509 /* NB: LABEL_NUSES () is not maintained outside of jump.c.
4510 Work around that. */
4511 && !single_pred_p (next_bb)))
4512 return true;
4514 if (!in_current_region_p (next_bb))
4515 return true;
4517 FOR_EACH_EDGE (e, ei, bb->succs)
4518 if ((e->flags & EDGE_FALLTHRU) != 0)
4520 gcc_assert (e->dest == next_bb);
4522 return false;
4525 return true;
4528 /* Returns true when INSN and SUCC are in the same EBB, given that SUCC is a
4529 successor of INSN. */
4530 bool
4531 in_same_ebb_p (insn_t insn, insn_t succ)
4533 basic_block ptr = BLOCK_FOR_INSN (insn);
4535 for(;;)
4537 if (ptr == BLOCK_FOR_INSN (succ))
4538 return true;
4540 if (bb_ends_ebb_p (ptr))
4541 return false;
4543 ptr = bb_next_bb (ptr);
4546 gcc_unreachable ();
4547 return false;
4550 /* Recomputes the reverse topological order for the function and
4551 saves it in REV_TOP_ORDER_INDEX. REV_TOP_ORDER_INDEX_LEN is also
4552 modified appropriately. */
4553 static void
4554 recompute_rev_top_order (void)
4556 int *postorder;
4557 int n_blocks, i;
4559 if (!rev_top_order_index || rev_top_order_index_len < last_basic_block)
4561 rev_top_order_index_len = last_basic_block;
4562 rev_top_order_index = XRESIZEVEC (int, rev_top_order_index,
4563 rev_top_order_index_len);
4566 postorder = XNEWVEC (int, n_basic_blocks);
4568 n_blocks = post_order_compute (postorder, true, false);
4569 gcc_assert (n_basic_blocks == n_blocks);
4571 /* Build reverse function: for each basic block with BB->INDEX == K
4572 rev_top_order_index[K] is it's reverse topological sort number. */
4573 for (i = 0; i < n_blocks; i++)
4575 gcc_assert (postorder[i] < rev_top_order_index_len);
4576 rev_top_order_index[postorder[i]] = i;
4579 free (postorder);
4582 /* Clear all flags from insns in BB that could spoil its rescheduling. */
4583 void
4584 clear_outdated_rtx_info (basic_block bb)
4586 rtx insn;
4588 FOR_BB_INSNS (bb, insn)
4589 if (INSN_P (insn))
4591 SCHED_GROUP_P (insn) = 0;
4592 INSN_AFTER_STALL_P (insn) = 0;
4593 INSN_SCHED_TIMES (insn) = 0;
4594 EXPR_PRIORITY_ADJ (INSN_EXPR (insn)) = 0;
4596 /* We cannot use the changed caches, as previously we could ignore
4597 the LHS dependence due to enabled renaming and transform
4598 the expression, and currently we'll be unable to do this. */
4599 htab_empty (INSN_TRANSFORMED_INSNS (insn));
4603 /* Add BB_NOTE to the pool of available basic block notes. */
4604 static void
4605 return_bb_to_pool (basic_block bb)
4607 rtx note = bb_note (bb);
4609 gcc_assert (NOTE_BASIC_BLOCK (note) == bb
4610 && bb->aux == NULL);
4612 /* It turns out that current cfg infrastructure does not support
4613 reuse of basic blocks. Don't bother for now. */
4614 /*VEC_safe_push (rtx, heap, bb_note_pool, note);*/
4617 /* Get a bb_note from pool or return NULL_RTX if pool is empty. */
4618 static rtx
4619 get_bb_note_from_pool (void)
4621 if (VEC_empty (rtx, bb_note_pool))
4622 return NULL_RTX;
4623 else
4625 rtx note = VEC_pop (rtx, bb_note_pool);
4627 PREV_INSN (note) = NULL_RTX;
4628 NEXT_INSN (note) = NULL_RTX;
4630 return note;
4634 /* Free bb_note_pool. */
4635 void
4636 free_bb_note_pool (void)
4638 VEC_free (rtx, heap, bb_note_pool);
4641 /* Setup scheduler pool and successor structure. */
4642 void
4643 alloc_sched_pools (void)
4645 int succs_size;
4647 succs_size = MAX_WS + 1;
4648 succs_info_pool.stack = XCNEWVEC (struct succs_info, succs_size);
4649 succs_info_pool.size = succs_size;
4650 succs_info_pool.top = -1;
4651 succs_info_pool.max_top = -1;
4653 sched_lists_pool = create_alloc_pool ("sel-sched-lists",
4654 sizeof (struct _list_node), 500);
4657 /* Free the pools. */
4658 void
4659 free_sched_pools (void)
4661 int i;
4663 free_alloc_pool (sched_lists_pool);
4664 gcc_assert (succs_info_pool.top == -1);
4665 for (i = 0; i < succs_info_pool.max_top; i++)
4667 VEC_free (rtx, heap, succs_info_pool.stack[i].succs_ok);
4668 VEC_free (rtx, heap, succs_info_pool.stack[i].succs_other);
4669 VEC_free (int, heap, succs_info_pool.stack[i].probs_ok);
4671 free (succs_info_pool.stack);
4675 /* Returns a position in RGN where BB can be inserted retaining
4676 topological order. */
4677 static int
4678 find_place_to_insert_bb (basic_block bb, int rgn)
4680 bool has_preds_outside_rgn = false;
4681 edge e;
4682 edge_iterator ei;
4684 /* Find whether we have preds outside the region. */
4685 FOR_EACH_EDGE (e, ei, bb->preds)
4686 if (!in_current_region_p (e->src))
4688 has_preds_outside_rgn = true;
4689 break;
4692 /* Recompute the top order -- needed when we have > 1 pred
4693 and in case we don't have preds outside. */
4694 if (flag_sel_sched_pipelining_outer_loops
4695 && (has_preds_outside_rgn || EDGE_COUNT (bb->preds) > 1))
4697 int i, bbi = bb->index, cur_bbi;
4699 recompute_rev_top_order ();
4700 for (i = RGN_NR_BLOCKS (rgn) - 1; i >= 0; i--)
4702 cur_bbi = BB_TO_BLOCK (i);
4703 if (rev_top_order_index[bbi]
4704 < rev_top_order_index[cur_bbi])
4705 break;
4708 /* We skipped the right block, so we increase i. We accomodate
4709 it for increasing by step later, so we decrease i. */
4710 return (i + 1) - 1;
4712 else if (has_preds_outside_rgn)
4714 /* This is the case when we generate an extra empty block
4715 to serve as region head during pipelining. */
4716 e = EDGE_SUCC (bb, 0);
4717 gcc_assert (EDGE_COUNT (bb->succs) == 1
4718 && in_current_region_p (EDGE_SUCC (bb, 0)->dest)
4719 && (BLOCK_TO_BB (e->dest->index) == 0));
4720 return -1;
4723 /* We don't have preds outside the region. We should have
4724 the only pred, because the multiple preds case comes from
4725 the pipelining of outer loops, and that is handled above.
4726 Just take the bbi of this single pred. */
4727 if (EDGE_COUNT (bb->succs) > 0)
4729 int pred_bbi;
4731 gcc_assert (EDGE_COUNT (bb->preds) == 1);
4733 pred_bbi = EDGE_PRED (bb, 0)->src->index;
4734 return BLOCK_TO_BB (pred_bbi);
4736 else
4737 /* BB has no successors. It is safe to put it in the end. */
4738 return current_nr_blocks - 1;
4741 /* Deletes an empty basic block freeing its data. */
4742 static void
4743 delete_and_free_basic_block (basic_block bb)
4745 gcc_assert (sel_bb_empty_p (bb));
4747 if (BB_LV_SET (bb))
4748 free_lv_set (bb);
4750 bitmap_clear_bit (blocks_to_reschedule, bb->index);
4752 /* Can't assert av_set properties because we use sel_aremove_bb
4753 when removing loop preheader from the region. At the point of
4754 removing the preheader we already have deallocated sel_region_bb_info. */
4755 gcc_assert (BB_LV_SET (bb) == NULL
4756 && !BB_LV_SET_VALID_P (bb)
4757 && BB_AV_LEVEL (bb) == 0
4758 && BB_AV_SET (bb) == NULL);
4760 delete_basic_block (bb);
4763 /* Add BB to the current region and update the region data. */
4764 static void
4765 add_block_to_current_region (basic_block bb)
4767 int i, pos, bbi = -2, rgn;
4769 rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
4770 bbi = find_place_to_insert_bb (bb, rgn);
4771 bbi += 1;
4772 pos = RGN_BLOCKS (rgn) + bbi;
4774 gcc_assert (RGN_HAS_REAL_EBB (rgn) == 0
4775 && ebb_head[bbi] == pos);
4777 /* Make a place for the new block. */
4778 extend_regions ();
4780 for (i = RGN_BLOCKS (rgn + 1) - 1; i >= pos; i--)
4781 BLOCK_TO_BB (rgn_bb_table[i])++;
4783 memmove (rgn_bb_table + pos + 1,
4784 rgn_bb_table + pos,
4785 (RGN_BLOCKS (nr_regions) - pos) * sizeof (*rgn_bb_table));
4787 /* Initialize data for BB. */
4788 rgn_bb_table[pos] = bb->index;
4789 BLOCK_TO_BB (bb->index) = bbi;
4790 CONTAINING_RGN (bb->index) = rgn;
4792 RGN_NR_BLOCKS (rgn)++;
4794 for (i = rgn + 1; i <= nr_regions; i++)
4795 RGN_BLOCKS (i)++;
4798 /* Remove BB from the current region and update the region data. */
4799 static void
4800 remove_bb_from_region (basic_block bb)
4802 int i, pos, bbi = -2, rgn;
4804 rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
4805 bbi = BLOCK_TO_BB (bb->index);
4806 pos = RGN_BLOCKS (rgn) + bbi;
4808 gcc_assert (RGN_HAS_REAL_EBB (rgn) == 0
4809 && ebb_head[bbi] == pos);
4811 for (i = RGN_BLOCKS (rgn + 1) - 1; i >= pos; i--)
4812 BLOCK_TO_BB (rgn_bb_table[i])--;
4814 memmove (rgn_bb_table + pos,
4815 rgn_bb_table + pos + 1,
4816 (RGN_BLOCKS (nr_regions) - pos) * sizeof (*rgn_bb_table));
4818 RGN_NR_BLOCKS (rgn)--;
4819 for (i = rgn + 1; i <= nr_regions; i++)
4820 RGN_BLOCKS (i)--;
4823 /* Add BB to the current region and update all data. If BB is NULL, add all
4824 blocks from last_added_blocks vector. */
4825 static void
4826 sel_add_bb (basic_block bb)
4828 /* Extend luids so that new notes will receive zero luids. */
4829 sched_init_luids (NULL, NULL, NULL, NULL);
4830 sched_init_bbs ();
4831 sel_init_bbs (last_added_blocks, NULL);
4833 /* When bb is passed explicitly, the vector should contain
4834 the only element that equals to bb; otherwise, the vector
4835 should not be NULL. */
4836 gcc_assert (last_added_blocks != NULL);
4838 if (bb != NULL)
4840 gcc_assert (VEC_length (basic_block, last_added_blocks) == 1
4841 && VEC_index (basic_block,
4842 last_added_blocks, 0) == bb);
4843 add_block_to_current_region (bb);
4845 /* We associate creating/deleting data sets with the first insn
4846 appearing / disappearing in the bb. */
4847 if (!sel_bb_empty_p (bb) && BB_LV_SET (bb) == NULL)
4848 create_initial_data_sets (bb);
4850 VEC_free (basic_block, heap, last_added_blocks);
4852 else
4853 /* BB is NULL - process LAST_ADDED_BLOCKS instead. */
4855 int i;
4856 basic_block temp_bb = NULL;
4858 for (i = 0;
4859 VEC_iterate (basic_block, last_added_blocks, i, bb); i++)
4861 add_block_to_current_region (bb);
4862 temp_bb = bb;
4865 /* We need to fetch at least one bb so we know the region
4866 to update. */
4867 gcc_assert (temp_bb != NULL);
4868 bb = temp_bb;
4870 VEC_free (basic_block, heap, last_added_blocks);
4873 rgn_setup_region (CONTAINING_RGN (bb->index));
4876 /* Remove BB from the current region and update all data.
4877 If REMOVE_FROM_CFG_PBB is true, also remove the block cfom cfg. */
4878 static void
4879 sel_remove_bb (basic_block bb, bool remove_from_cfg_p)
4881 gcc_assert (bb != NULL && BB_NOTE_LIST (bb) == NULL_RTX);
4883 remove_bb_from_region (bb);
4884 return_bb_to_pool (bb);
4885 bitmap_clear_bit (blocks_to_reschedule, bb->index);
4887 if (remove_from_cfg_p)
4888 delete_and_free_basic_block (bb);
4890 rgn_setup_region (CONTAINING_RGN (bb->index));
4893 /* Concatenate info of EMPTY_BB to info of MERGE_BB. */
4894 static void
4895 move_bb_info (basic_block merge_bb, basic_block empty_bb)
4897 gcc_assert (in_current_region_p (merge_bb));
4899 concat_note_lists (BB_NOTE_LIST (empty_bb),
4900 &BB_NOTE_LIST (merge_bb));
4901 BB_NOTE_LIST (empty_bb) = NULL_RTX;
4905 /* Remove an empty basic block EMPTY_BB. When MERGE_UP_P is true, we put
4906 EMPTY_BB's note lists into its predecessor instead of putting them
4907 into the successor. When REMOVE_FROM_CFG_P is true, also remove
4908 the empty block. */
4909 void
4910 sel_remove_empty_bb (basic_block empty_bb, bool merge_up_p,
4911 bool remove_from_cfg_p)
4913 basic_block merge_bb;
4915 gcc_assert (sel_bb_empty_p (empty_bb));
4917 if (merge_up_p)
4919 merge_bb = empty_bb->prev_bb;
4920 gcc_assert (EDGE_COUNT (empty_bb->preds) == 1
4921 && EDGE_PRED (empty_bb, 0)->src == merge_bb);
4923 else
4925 edge e;
4926 edge_iterator ei;
4928 merge_bb = bb_next_bb (empty_bb);
4930 /* Redirect incoming edges (except fallthrough one) of EMPTY_BB to its
4931 successor block. */
4932 for (ei = ei_start (empty_bb->preds);
4933 (e = ei_safe_edge (ei)); )
4935 if (! (e->flags & EDGE_FALLTHRU))
4936 sel_redirect_edge_and_branch (e, merge_bb);
4937 else
4938 ei_next (&ei);
4941 gcc_assert (EDGE_COUNT (empty_bb->succs) == 1
4942 && EDGE_SUCC (empty_bb, 0)->dest == merge_bb);
4945 move_bb_info (merge_bb, empty_bb);
4946 remove_empty_bb (empty_bb, remove_from_cfg_p);
4949 /* Remove EMPTY_BB. If REMOVE_FROM_CFG_P is false, remove EMPTY_BB from
4950 region, but keep it in CFG. */
4951 static void
4952 remove_empty_bb (basic_block empty_bb, bool remove_from_cfg_p)
4954 /* The block should contain just a note or a label.
4955 We try to check whether it is unused below. */
4956 gcc_assert (BB_HEAD (empty_bb) == BB_END (empty_bb)
4957 || LABEL_P (BB_HEAD (empty_bb)));
4959 /* If basic block has predecessors or successors, redirect them. */
4960 if (remove_from_cfg_p
4961 && (EDGE_COUNT (empty_bb->preds) > 0
4962 || EDGE_COUNT (empty_bb->succs) > 0))
4964 basic_block pred;
4965 basic_block succ;
4967 /* We need to init PRED and SUCC before redirecting edges. */
4968 if (EDGE_COUNT (empty_bb->preds) > 0)
4970 edge e;
4972 gcc_assert (EDGE_COUNT (empty_bb->preds) == 1);
4974 e = EDGE_PRED (empty_bb, 0);
4975 gcc_assert (e->src == empty_bb->prev_bb
4976 && (e->flags & EDGE_FALLTHRU));
4978 pred = empty_bb->prev_bb;
4980 else
4981 pred = NULL;
4983 if (EDGE_COUNT (empty_bb->succs) > 0)
4985 /* We do not check fallthruness here as above, because
4986 after removing a jump the edge may actually be not fallthru. */
4987 gcc_assert (EDGE_COUNT (empty_bb->succs) == 1);
4988 succ = EDGE_SUCC (empty_bb, 0)->dest;
4990 else
4991 succ = NULL;
4993 if (EDGE_COUNT (empty_bb->preds) > 0 && succ != NULL)
4995 edge e = EDGE_PRED (empty_bb, 0);
4997 if (e->flags & EDGE_FALLTHRU)
4998 redirect_edge_succ_nodup (e, succ);
4999 else
5000 sel_redirect_edge_and_branch (EDGE_PRED (empty_bb, 0), succ);
5003 if (EDGE_COUNT (empty_bb->succs) > 0 && pred != NULL)
5005 edge e = EDGE_SUCC (empty_bb, 0);
5007 if (find_edge (pred, e->dest) == NULL)
5008 redirect_edge_pred (e, pred);
5012 /* Finish removing. */
5013 sel_remove_bb (empty_bb, remove_from_cfg_p);
5016 /* An implementation of create_basic_block hook, which additionally updates
5017 per-bb data structures. */
5018 static basic_block
5019 sel_create_basic_block (void *headp, void *endp, basic_block after)
5021 basic_block new_bb;
5022 insn_t new_bb_note;
5024 gcc_assert (flag_sel_sched_pipelining_outer_loops
5025 || last_added_blocks == NULL);
5027 new_bb_note = get_bb_note_from_pool ();
5029 if (new_bb_note == NULL_RTX)
5030 new_bb = orig_cfg_hooks.create_basic_block (headp, endp, after);
5031 else
5033 new_bb = create_basic_block_structure ((rtx) headp, (rtx) endp,
5034 new_bb_note, after);
5035 new_bb->aux = NULL;
5038 VEC_safe_push (basic_block, heap, last_added_blocks, new_bb);
5040 return new_bb;
5043 /* Implement sched_init_only_bb (). */
5044 static void
5045 sel_init_only_bb (basic_block bb, basic_block after)
5047 gcc_assert (after == NULL);
5049 extend_regions ();
5050 rgn_make_new_region_out_of_new_block (bb);
5053 /* Update the latch when we've splitted or merged it from FROM block to TO.
5054 This should be checked for all outer loops, too. */
5055 static void
5056 change_loops_latches (basic_block from, basic_block to)
5058 gcc_assert (from != to);
5060 if (current_loop_nest)
5062 struct loop *loop;
5064 for (loop = current_loop_nest; loop; loop = loop_outer (loop))
5065 if (considered_for_pipelining_p (loop) && loop->latch == from)
5067 gcc_assert (loop == current_loop_nest);
5068 loop->latch = to;
5069 gcc_assert (loop_latch_edge (loop));
5074 /* Splits BB on two basic blocks, adding it to the region and extending
5075 per-bb data structures. Returns the newly created bb. */
5076 static basic_block
5077 sel_split_block (basic_block bb, rtx after)
5079 basic_block new_bb;
5080 insn_t insn;
5082 new_bb = sched_split_block_1 (bb, after);
5083 sel_add_bb (new_bb);
5085 /* This should be called after sel_add_bb, because this uses
5086 CONTAINING_RGN for the new block, which is not yet initialized.
5087 FIXME: this function may be a no-op now. */
5088 change_loops_latches (bb, new_bb);
5090 /* Update ORIG_BB_INDEX for insns moved into the new block. */
5091 FOR_BB_INSNS (new_bb, insn)
5092 if (INSN_P (insn))
5093 EXPR_ORIG_BB_INDEX (INSN_EXPR (insn)) = new_bb->index;
5095 if (sel_bb_empty_p (bb))
5097 gcc_assert (!sel_bb_empty_p (new_bb));
5099 /* NEW_BB has data sets that need to be updated and BB holds
5100 data sets that should be removed. Exchange these data sets
5101 so that we won't lose BB's valid data sets. */
5102 exchange_data_sets (new_bb, bb);
5103 free_data_sets (bb);
5106 if (!sel_bb_empty_p (new_bb)
5107 && bitmap_bit_p (blocks_to_reschedule, bb->index))
5108 bitmap_set_bit (blocks_to_reschedule, new_bb->index);
5110 return new_bb;
5113 /* If BB ends with a jump insn whose ID is bigger then PREV_MAX_UID, return it.
5114 Otherwise returns NULL. */
5115 static rtx
5116 check_for_new_jump (basic_block bb, int prev_max_uid)
5118 rtx end;
5120 end = sel_bb_end (bb);
5121 if (end && INSN_UID (end) >= prev_max_uid)
5122 return end;
5123 return NULL;
5126 /* Look for a new jump either in FROM_BB block or in newly created JUMP_BB block.
5127 New means having UID at least equal to PREV_MAX_UID. */
5128 static rtx
5129 find_new_jump (basic_block from, basic_block jump_bb, int prev_max_uid)
5131 rtx jump;
5133 /* Return immediately if no new insns were emitted. */
5134 if (get_max_uid () == prev_max_uid)
5135 return NULL;
5137 /* Now check both blocks for new jumps. It will ever be only one. */
5138 if ((jump = check_for_new_jump (from, prev_max_uid)))
5139 return jump;
5141 if (jump_bb != NULL
5142 && (jump = check_for_new_jump (jump_bb, prev_max_uid)))
5143 return jump;
5144 return NULL;
5147 /* Splits E and adds the newly created basic block to the current region.
5148 Returns this basic block. */
5149 basic_block
5150 sel_split_edge (edge e)
5152 basic_block new_bb, src, other_bb = NULL;
5153 int prev_max_uid;
5154 rtx jump;
5156 src = e->src;
5157 prev_max_uid = get_max_uid ();
5158 new_bb = split_edge (e);
5160 if (flag_sel_sched_pipelining_outer_loops
5161 && current_loop_nest)
5163 int i;
5164 basic_block bb;
5166 /* Some of the basic blocks might not have been added to the loop.
5167 Add them here, until this is fixed in force_fallthru. */
5168 for (i = 0;
5169 VEC_iterate (basic_block, last_added_blocks, i, bb); i++)
5170 if (!bb->loop_father)
5172 add_bb_to_loop (bb, e->dest->loop_father);
5174 gcc_assert (!other_bb && (new_bb->index != bb->index));
5175 other_bb = bb;
5179 /* Add all last_added_blocks to the region. */
5180 sel_add_bb (NULL);
5182 jump = find_new_jump (src, new_bb, prev_max_uid);
5183 if (jump)
5184 sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP);
5186 /* Put the correct lv set on this block. */
5187 if (other_bb && !sel_bb_empty_p (other_bb))
5188 compute_live (sel_bb_head (other_bb));
5190 return new_bb;
5193 /* Implement sched_create_empty_bb (). */
5194 static basic_block
5195 sel_create_empty_bb (basic_block after)
5197 basic_block new_bb;
5199 new_bb = sched_create_empty_bb_1 (after);
5201 /* We'll explicitly initialize NEW_BB via sel_init_only_bb () a bit
5202 later. */
5203 gcc_assert (VEC_length (basic_block, last_added_blocks) == 1
5204 && VEC_index (basic_block, last_added_blocks, 0) == new_bb);
5206 VEC_free (basic_block, heap, last_added_blocks);
5207 return new_bb;
5210 /* Implement sched_create_recovery_block. ORIG_INSN is where block
5211 will be splitted to insert a check. */
5212 basic_block
5213 sel_create_recovery_block (insn_t orig_insn)
5215 basic_block first_bb, second_bb, recovery_block;
5216 basic_block before_recovery = NULL;
5217 rtx jump;
5219 first_bb = BLOCK_FOR_INSN (orig_insn);
5220 if (sel_bb_end_p (orig_insn))
5222 /* Avoid introducing an empty block while splitting. */
5223 gcc_assert (single_succ_p (first_bb));
5224 second_bb = single_succ (first_bb);
5226 else
5227 second_bb = sched_split_block (first_bb, orig_insn);
5229 recovery_block = sched_create_recovery_block (&before_recovery);
5230 if (before_recovery)
5231 copy_lv_set_from (before_recovery, EXIT_BLOCK_PTR);
5233 gcc_assert (sel_bb_empty_p (recovery_block));
5234 sched_create_recovery_edges (first_bb, recovery_block, second_bb);
5235 if (current_loops != NULL)
5236 add_bb_to_loop (recovery_block, first_bb->loop_father);
5238 sel_add_bb (recovery_block);
5240 jump = BB_END (recovery_block);
5241 gcc_assert (sel_bb_head (recovery_block) == jump);
5242 sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP);
5244 return recovery_block;
5247 /* Merge basic block B into basic block A. */
5248 void
5249 sel_merge_blocks (basic_block a, basic_block b)
5251 sel_remove_empty_bb (b, true, false);
5252 merge_blocks (a, b);
5254 change_loops_latches (b, a);
5257 /* A wrapper for redirect_edge_and_branch_force, which also initializes
5258 data structures for possibly created bb and insns. Returns the newly
5259 added bb or NULL, when a bb was not needed. */
5260 void
5261 sel_redirect_edge_and_branch_force (edge e, basic_block to)
5263 basic_block jump_bb, src;
5264 int prev_max_uid;
5265 rtx jump;
5267 gcc_assert (!sel_bb_empty_p (e->src));
5269 src = e->src;
5270 prev_max_uid = get_max_uid ();
5271 jump_bb = redirect_edge_and_branch_force (e, to);
5273 if (jump_bb != NULL)
5274 sel_add_bb (jump_bb);
5276 /* This function could not be used to spoil the loop structure by now,
5277 thus we don't care to update anything. But check it to be sure. */
5278 if (current_loop_nest
5279 && pipelining_p)
5280 gcc_assert (loop_latch_edge (current_loop_nest));
5282 jump = find_new_jump (src, jump_bb, prev_max_uid);
5283 if (jump)
5284 sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP);
5287 /* A wrapper for redirect_edge_and_branch. */
5288 void
5289 sel_redirect_edge_and_branch (edge e, basic_block to)
5291 bool latch_edge_p;
5292 basic_block src;
5293 int prev_max_uid;
5294 rtx jump;
5295 edge redirected;
5297 latch_edge_p = (pipelining_p
5298 && current_loop_nest
5299 && e == loop_latch_edge (current_loop_nest));
5301 src = e->src;
5302 prev_max_uid = get_max_uid ();
5304 redirected = redirect_edge_and_branch (e, to);
5306 gcc_assert (redirected && last_added_blocks == NULL);
5308 /* When we've redirected a latch edge, update the header. */
5309 if (latch_edge_p)
5311 current_loop_nest->header = to;
5312 gcc_assert (loop_latch_edge (current_loop_nest));
5315 jump = find_new_jump (src, NULL, prev_max_uid);
5316 if (jump)
5317 sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP);
5320 /* This variable holds the cfg hooks used by the selective scheduler. */
5321 static struct cfg_hooks sel_cfg_hooks;
5323 /* Register sel-sched cfg hooks. */
5324 void
5325 sel_register_cfg_hooks (void)
5327 sched_split_block = sel_split_block;
5329 orig_cfg_hooks = get_cfg_hooks ();
5330 sel_cfg_hooks = orig_cfg_hooks;
5332 sel_cfg_hooks.create_basic_block = sel_create_basic_block;
5334 set_cfg_hooks (sel_cfg_hooks);
5336 sched_init_only_bb = sel_init_only_bb;
5337 sched_split_block = sel_split_block;
5338 sched_create_empty_bb = sel_create_empty_bb;
5341 /* Unregister sel-sched cfg hooks. */
5342 void
5343 sel_unregister_cfg_hooks (void)
5345 sched_create_empty_bb = NULL;
5346 sched_split_block = NULL;
5347 sched_init_only_bb = NULL;
5349 set_cfg_hooks (orig_cfg_hooks);
5353 /* Emit an insn rtx based on PATTERN. If a jump insn is wanted,
5354 LABEL is where this jump should be directed. */
5356 create_insn_rtx_from_pattern (rtx pattern, rtx label)
5358 rtx insn_rtx;
5360 gcc_assert (!INSN_P (pattern));
5362 start_sequence ();
5364 if (label == NULL_RTX)
5365 insn_rtx = emit_insn (pattern);
5366 else
5368 insn_rtx = emit_jump_insn (pattern);
5369 JUMP_LABEL (insn_rtx) = label;
5370 ++LABEL_NUSES (label);
5373 end_sequence ();
5375 sched_init_luids (NULL, NULL, NULL, NULL);
5376 sched_extend_target ();
5377 sched_deps_init (false);
5379 /* Initialize INSN_CODE now. */
5380 recog_memoized (insn_rtx);
5381 return insn_rtx;
5384 /* Create a new vinsn for INSN_RTX. FORCE_UNIQUE_P is true when the vinsn
5385 must not be clonable. */
5386 vinsn_t
5387 create_vinsn_from_insn_rtx (rtx insn_rtx, bool force_unique_p)
5389 gcc_assert (INSN_P (insn_rtx) && !INSN_IN_STREAM_P (insn_rtx));
5391 /* If VINSN_TYPE is not USE, retain its uniqueness. */
5392 return vinsn_create (insn_rtx, force_unique_p);
5395 /* Create a copy of INSN_RTX. */
5397 create_copy_of_insn_rtx (rtx insn_rtx)
5399 rtx res;
5401 gcc_assert (NONJUMP_INSN_P (insn_rtx));
5403 res = create_insn_rtx_from_pattern (copy_rtx (PATTERN (insn_rtx)),
5404 NULL_RTX);
5405 return res;
5408 /* Change vinsn field of EXPR to hold NEW_VINSN. */
5409 void
5410 change_vinsn_in_expr (expr_t expr, vinsn_t new_vinsn)
5412 vinsn_detach (EXPR_VINSN (expr));
5414 EXPR_VINSN (expr) = new_vinsn;
5415 vinsn_attach (new_vinsn);
5418 /* Helpers for global init. */
5419 /* This structure is used to be able to call existing bundling mechanism
5420 and calculate insn priorities. */
5421 static struct haifa_sched_info sched_sel_haifa_sched_info =
5423 NULL, /* init_ready_list */
5424 NULL, /* can_schedule_ready_p */
5425 NULL, /* schedule_more_p */
5426 NULL, /* new_ready */
5427 NULL, /* rgn_rank */
5428 sel_print_insn, /* rgn_print_insn */
5429 contributes_to_priority,
5430 NULL, /* insn_finishes_block_p */
5432 NULL, NULL,
5433 NULL, NULL,
5434 0, 0,
5436 NULL, /* add_remove_insn */
5437 NULL, /* begin_schedule_ready */
5438 NULL, /* advance_target_bb */
5439 SEL_SCHED | NEW_BBS
5442 /* Setup special insns used in the scheduler. */
5443 void
5444 setup_nop_and_exit_insns (void)
5446 gcc_assert (nop_pattern == NULL_RTX
5447 && exit_insn == NULL_RTX);
5449 nop_pattern = gen_nop ();
5451 start_sequence ();
5452 emit_insn (nop_pattern);
5453 exit_insn = get_insns ();
5454 end_sequence ();
5455 set_block_for_insn (exit_insn, EXIT_BLOCK_PTR);
5458 /* Free special insns used in the scheduler. */
5459 void
5460 free_nop_and_exit_insns (void)
5462 exit_insn = NULL_RTX;
5463 nop_pattern = NULL_RTX;
5466 /* Setup a special vinsn used in new insns initialization. */
5467 void
5468 setup_nop_vinsn (void)
5470 nop_vinsn = vinsn_create (exit_insn, false);
5471 vinsn_attach (nop_vinsn);
5474 /* Free a special vinsn used in new insns initialization. */
5475 void
5476 free_nop_vinsn (void)
5478 gcc_assert (VINSN_COUNT (nop_vinsn) == 1);
5479 vinsn_detach (nop_vinsn);
5480 nop_vinsn = NULL;
5483 /* Call a set_sched_flags hook. */
5484 void
5485 sel_set_sched_flags (void)
5487 /* ??? This means that set_sched_flags were called, and we decided to
5488 support speculation. However, set_sched_flags also modifies flags
5489 on current_sched_info, doing this only at global init. And we
5490 sometimes change c_s_i later. So put the correct flags again. */
5491 if (spec_info && targetm.sched.set_sched_flags)
5492 targetm.sched.set_sched_flags (spec_info);
5495 /* Setup pointers to global sched info structures. */
5496 void
5497 sel_setup_sched_infos (void)
5499 rgn_setup_common_sched_info ();
5501 memcpy (&sel_common_sched_info, common_sched_info,
5502 sizeof (sel_common_sched_info));
5504 sel_common_sched_info.fix_recovery_cfg = NULL;
5505 sel_common_sched_info.add_block = NULL;
5506 sel_common_sched_info.estimate_number_of_insns
5507 = sel_estimate_number_of_insns;
5508 sel_common_sched_info.luid_for_non_insn = sel_luid_for_non_insn;
5509 sel_common_sched_info.sched_pass_id = SCHED_SEL_PASS;
5511 common_sched_info = &sel_common_sched_info;
5513 current_sched_info = &sched_sel_haifa_sched_info;
5514 current_sched_info->sched_max_insns_priority =
5515 get_rgn_sched_max_insns_priority ();
5517 sel_set_sched_flags ();
5521 /* Adds basic block BB to region RGN at the position *BB_ORD_INDEX,
5522 *BB_ORD_INDEX after that is increased. */
5523 static void
5524 sel_add_block_to_region (basic_block bb, int *bb_ord_index, int rgn)
5526 RGN_NR_BLOCKS (rgn) += 1;
5527 RGN_DONT_CALC_DEPS (rgn) = 0;
5528 RGN_HAS_REAL_EBB (rgn) = 0;
5529 CONTAINING_RGN (bb->index) = rgn;
5530 BLOCK_TO_BB (bb->index) = *bb_ord_index;
5531 rgn_bb_table[RGN_BLOCKS (rgn) + *bb_ord_index] = bb->index;
5532 (*bb_ord_index)++;
5534 /* FIXME: it is true only when not scheduling ebbs. */
5535 RGN_BLOCKS (rgn + 1) = RGN_BLOCKS (rgn) + RGN_NR_BLOCKS (rgn);
5538 /* Functions to support pipelining of outer loops. */
5540 /* Creates a new empty region and returns it's number. */
5541 static int
5542 sel_create_new_region (void)
5544 int new_rgn_number = nr_regions;
5546 RGN_NR_BLOCKS (new_rgn_number) = 0;
5548 /* FIXME: This will work only when EBBs are not created. */
5549 if (new_rgn_number != 0)
5550 RGN_BLOCKS (new_rgn_number) = RGN_BLOCKS (new_rgn_number - 1) +
5551 RGN_NR_BLOCKS (new_rgn_number - 1);
5552 else
5553 RGN_BLOCKS (new_rgn_number) = 0;
5555 /* Set the blocks of the next region so the other functions may
5556 calculate the number of blocks in the region. */
5557 RGN_BLOCKS (new_rgn_number + 1) = RGN_BLOCKS (new_rgn_number) +
5558 RGN_NR_BLOCKS (new_rgn_number);
5560 nr_regions++;
5562 return new_rgn_number;
5565 /* If X has a smaller topological sort number than Y, returns -1;
5566 if greater, returns 1. */
5567 static int
5568 bb_top_order_comparator (const void *x, const void *y)
5570 basic_block bb1 = *(const basic_block *) x;
5571 basic_block bb2 = *(const basic_block *) y;
5573 gcc_assert (bb1 == bb2
5574 || rev_top_order_index[bb1->index]
5575 != rev_top_order_index[bb2->index]);
5577 /* It's a reverse topological order in REV_TOP_ORDER_INDEX, so
5578 bbs with greater number should go earlier. */
5579 if (rev_top_order_index[bb1->index] > rev_top_order_index[bb2->index])
5580 return -1;
5581 else
5582 return 1;
5585 /* Create a region for LOOP and return its number. If we don't want
5586 to pipeline LOOP, return -1. */
5587 static int
5588 make_region_from_loop (struct loop *loop)
5590 unsigned int i;
5591 int new_rgn_number = -1;
5592 struct loop *inner;
5594 /* Basic block index, to be assigned to BLOCK_TO_BB. */
5595 int bb_ord_index = 0;
5596 basic_block *loop_blocks;
5597 basic_block preheader_block;
5599 if (loop->num_nodes
5600 > (unsigned) PARAM_VALUE (PARAM_MAX_PIPELINE_REGION_BLOCKS))
5601 return -1;
5603 /* Don't pipeline loops whose latch belongs to some of its inner loops. */
5604 for (inner = loop->inner; inner; inner = inner->inner)
5605 if (flow_bb_inside_loop_p (inner, loop->latch))
5606 return -1;
5608 loop->ninsns = num_loop_insns (loop);
5609 if ((int) loop->ninsns > PARAM_VALUE (PARAM_MAX_PIPELINE_REGION_INSNS))
5610 return -1;
5612 loop_blocks = get_loop_body_in_custom_order (loop, bb_top_order_comparator);
5614 for (i = 0; i < loop->num_nodes; i++)
5615 if (loop_blocks[i]->flags & BB_IRREDUCIBLE_LOOP)
5617 free (loop_blocks);
5618 return -1;
5621 preheader_block = loop_preheader_edge (loop)->src;
5622 gcc_assert (preheader_block);
5623 gcc_assert (loop_blocks[0] == loop->header);
5625 new_rgn_number = sel_create_new_region ();
5627 sel_add_block_to_region (preheader_block, &bb_ord_index, new_rgn_number);
5628 SET_BIT (bbs_in_loop_rgns, preheader_block->index);
5630 for (i = 0; i < loop->num_nodes; i++)
5632 /* Add only those blocks that haven't been scheduled in the inner loop.
5633 The exception is the basic blocks with bookkeeping code - they should
5634 be added to the region (and they actually don't belong to the loop
5635 body, but to the region containing that loop body). */
5637 gcc_assert (new_rgn_number >= 0);
5639 if (! TEST_BIT (bbs_in_loop_rgns, loop_blocks[i]->index))
5641 sel_add_block_to_region (loop_blocks[i], &bb_ord_index,
5642 new_rgn_number);
5643 SET_BIT (bbs_in_loop_rgns, loop_blocks[i]->index);
5647 free (loop_blocks);
5648 MARK_LOOP_FOR_PIPELINING (loop);
5650 return new_rgn_number;
5653 /* Create a new region from preheader blocks LOOP_BLOCKS. */
5654 void
5655 make_region_from_loop_preheader (VEC(basic_block, heap) **loop_blocks)
5657 unsigned int i;
5658 int new_rgn_number = -1;
5659 basic_block bb;
5661 /* Basic block index, to be assigned to BLOCK_TO_BB. */
5662 int bb_ord_index = 0;
5664 new_rgn_number = sel_create_new_region ();
5666 for (i = 0; VEC_iterate (basic_block, *loop_blocks, i, bb); i++)
5668 gcc_assert (new_rgn_number >= 0);
5670 sel_add_block_to_region (bb, &bb_ord_index, new_rgn_number);
5673 VEC_free (basic_block, heap, *loop_blocks);
5674 gcc_assert (*loop_blocks == NULL);
5678 /* Create region(s) from loop nest LOOP, such that inner loops will be
5679 pipelined before outer loops. Returns true when a region for LOOP
5680 is created. */
5681 static bool
5682 make_regions_from_loop_nest (struct loop *loop)
5684 struct loop *cur_loop;
5685 int rgn_number;
5687 /* Traverse all inner nodes of the loop. */
5688 for (cur_loop = loop->inner; cur_loop; cur_loop = cur_loop->next)
5689 if (! TEST_BIT (bbs_in_loop_rgns, cur_loop->header->index))
5690 return false;
5692 /* At this moment all regular inner loops should have been pipelined.
5693 Try to create a region from this loop. */
5694 rgn_number = make_region_from_loop (loop);
5696 if (rgn_number < 0)
5697 return false;
5699 VEC_safe_push (loop_p, heap, loop_nests, loop);
5700 return true;
5703 /* Initalize data structures needed. */
5704 void
5705 sel_init_pipelining (void)
5707 /* Collect loop information to be used in outer loops pipelining. */
5708 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
5709 | LOOPS_HAVE_FALLTHRU_PREHEADERS
5710 | LOOPS_HAVE_RECORDED_EXITS
5711 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS);
5712 current_loop_nest = NULL;
5714 bbs_in_loop_rgns = sbitmap_alloc (last_basic_block);
5715 sbitmap_zero (bbs_in_loop_rgns);
5717 recompute_rev_top_order ();
5720 /* Returns a struct loop for region RGN. */
5721 loop_p
5722 get_loop_nest_for_rgn (unsigned int rgn)
5724 /* Regions created with extend_rgns don't have corresponding loop nests,
5725 because they don't represent loops. */
5726 if (rgn < VEC_length (loop_p, loop_nests))
5727 return VEC_index (loop_p, loop_nests, rgn);
5728 else
5729 return NULL;
5732 /* True when LOOP was included into pipelining regions. */
5733 bool
5734 considered_for_pipelining_p (struct loop *loop)
5736 if (loop_depth (loop) == 0)
5737 return false;
5739 /* Now, the loop could be too large or irreducible. Check whether its
5740 region is in LOOP_NESTS.
5741 We determine the region number of LOOP as the region number of its
5742 latch. We can't use header here, because this header could be
5743 just removed preheader and it will give us the wrong region number.
5744 Latch can't be used because it could be in the inner loop too. */
5745 if (LOOP_MARKED_FOR_PIPELINING_P (loop) && pipelining_p)
5747 int rgn = CONTAINING_RGN (loop->latch->index);
5749 gcc_assert ((unsigned) rgn < VEC_length (loop_p, loop_nests));
5750 return true;
5753 return false;
5756 /* Makes regions from the rest of the blocks, after loops are chosen
5757 for pipelining. */
5758 static void
5759 make_regions_from_the_rest (void)
5761 int cur_rgn_blocks;
5762 int *loop_hdr;
5763 int i;
5765 basic_block bb;
5766 edge e;
5767 edge_iterator ei;
5768 int *degree;
5769 int new_regions;
5771 /* Index in rgn_bb_table where to start allocating new regions. */
5772 cur_rgn_blocks = nr_regions ? RGN_BLOCKS (nr_regions) : 0;
5773 new_regions = nr_regions;
5775 /* Make regions from all the rest basic blocks - those that don't belong to
5776 any loop or belong to irreducible loops. Prepare the data structures
5777 for extend_rgns. */
5779 /* LOOP_HDR[I] == -1 if I-th bb doesn't belong to any loop,
5780 LOOP_HDR[I] == LOOP_HDR[J] iff basic blocks I and J reside within the same
5781 loop. */
5782 loop_hdr = XNEWVEC (int, last_basic_block);
5783 degree = XCNEWVEC (int, last_basic_block);
5786 /* For each basic block that belongs to some loop assign the number
5787 of innermost loop it belongs to. */
5788 for (i = 0; i < last_basic_block; i++)
5789 loop_hdr[i] = -1;
5791 FOR_EACH_BB (bb)
5793 if (bb->loop_father && !bb->loop_father->num == 0
5794 && !(bb->flags & BB_IRREDUCIBLE_LOOP))
5795 loop_hdr[bb->index] = bb->loop_father->num;
5798 /* For each basic block degree is calculated as the number of incoming
5799 edges, that are going out of bbs that are not yet scheduled.
5800 The basic blocks that are scheduled have degree value of zero. */
5801 FOR_EACH_BB (bb)
5803 degree[bb->index] = 0;
5805 if (!TEST_BIT (bbs_in_loop_rgns, bb->index))
5807 FOR_EACH_EDGE (e, ei, bb->preds)
5808 if (!TEST_BIT (bbs_in_loop_rgns, e->src->index))
5809 degree[bb->index]++;
5811 else
5812 degree[bb->index] = -1;
5815 extend_rgns (degree, &cur_rgn_blocks, bbs_in_loop_rgns, loop_hdr);
5817 /* Any block that did not end up in a region is placed into a region
5818 by itself. */
5819 FOR_EACH_BB (bb)
5820 if (degree[bb->index] >= 0)
5822 rgn_bb_table[cur_rgn_blocks] = bb->index;
5823 RGN_NR_BLOCKS (nr_regions) = 1;
5824 RGN_BLOCKS (nr_regions) = cur_rgn_blocks++;
5825 RGN_DONT_CALC_DEPS (nr_regions) = 0;
5826 RGN_HAS_REAL_EBB (nr_regions) = 0;
5827 CONTAINING_RGN (bb->index) = nr_regions++;
5828 BLOCK_TO_BB (bb->index) = 0;
5831 free (degree);
5832 free (loop_hdr);
5835 /* Free data structures used in pipelining of loops. */
5836 void sel_finish_pipelining (void)
5838 loop_iterator li;
5839 struct loop *loop;
5841 /* Release aux fields so we don't free them later by mistake. */
5842 FOR_EACH_LOOP (li, loop, 0)
5843 loop->aux = NULL;
5845 loop_optimizer_finalize ();
5847 VEC_free (loop_p, heap, loop_nests);
5849 free (rev_top_order_index);
5850 rev_top_order_index = NULL;
5853 /* This function replaces the find_rgns when
5854 FLAG_SEL_SCHED_PIPELINING_OUTER_LOOPS is set. */
5855 void
5856 sel_find_rgns (void)
5858 sel_init_pipelining ();
5859 extend_regions ();
5861 if (current_loops)
5863 loop_p loop;
5864 loop_iterator li;
5866 FOR_EACH_LOOP (li, loop, (flag_sel_sched_pipelining_outer_loops
5867 ? LI_FROM_INNERMOST
5868 : LI_ONLY_INNERMOST))
5869 make_regions_from_loop_nest (loop);
5872 /* Make regions from all the rest basic blocks and schedule them.
5873 These blocks include blocks that don't belong to any loop or belong
5874 to irreducible loops. */
5875 make_regions_from_the_rest ();
5877 /* We don't need bbs_in_loop_rgns anymore. */
5878 sbitmap_free (bbs_in_loop_rgns);
5879 bbs_in_loop_rgns = NULL;
5882 /* Adds the preheader blocks from previous loop to current region taking
5883 it from LOOP_PREHEADER_BLOCKS (current_loop_nest).
5884 This function is only used with -fsel-sched-pipelining-outer-loops. */
5885 void
5886 sel_add_loop_preheaders (void)
5888 int i;
5889 basic_block bb;
5890 VEC(basic_block, heap) *preheader_blocks
5891 = LOOP_PREHEADER_BLOCKS (current_loop_nest);
5893 for (i = 0;
5894 VEC_iterate (basic_block, preheader_blocks, i, bb);
5895 i++)
5896 sel_add_bb (bb);
5898 VEC_free (basic_block, heap, preheader_blocks);
5901 /* While pipelining outer loops, returns TRUE if BB is a loop preheader.
5902 Please note that the function should also work when pipelining_p is
5903 false, because it is used when deciding whether we should or should
5904 not reschedule pipelined code. */
5905 bool
5906 sel_is_loop_preheader_p (basic_block bb)
5908 if (current_loop_nest)
5910 struct loop *outer;
5912 if (preheader_removed)
5913 return false;
5915 /* Preheader is the first block in the region. */
5916 if (BLOCK_TO_BB (bb->index) == 0)
5917 return true;
5919 /* We used to find a preheader with the topological information.
5920 Check that the above code is equivalent to what we did before. */
5922 if (in_current_region_p (current_loop_nest->header))
5923 gcc_assert (!(BLOCK_TO_BB (bb->index)
5924 < BLOCK_TO_BB (current_loop_nest->header->index)));
5926 /* Support the situation when the latch block of outer loop
5927 could be from here. */
5928 for (outer = loop_outer (current_loop_nest);
5929 outer;
5930 outer = loop_outer (outer))
5931 if (considered_for_pipelining_p (outer) && outer->latch == bb)
5932 gcc_unreachable ();
5935 return false;
5938 /* Checks whether JUMP leads to basic block DEST_BB and no other blocks. */
5939 bool
5940 jump_leads_only_to_bb_p (insn_t jump, basic_block dest_bb)
5942 basic_block jump_bb = BLOCK_FOR_INSN (jump);
5944 /* It is not jump, jump with side-effects or jump can lead to several
5945 basic blocks. */
5946 if (!onlyjump_p (jump)
5947 || !any_uncondjump_p (jump))
5948 return false;
5950 /* Several outgoing edges, abnormal edge or destination of jump is
5951 not DEST_BB. */
5952 if (EDGE_COUNT (jump_bb->succs) != 1
5953 || EDGE_SUCC (jump_bb, 0)->flags & EDGE_ABNORMAL
5954 || EDGE_SUCC (jump_bb, 0)->dest != dest_bb)
5955 return false;
5957 /* If not anything of the upper. */
5958 return true;
5961 /* Removes the loop preheader from the current region and saves it in
5962 PREHEADER_BLOCKS of the father loop, so they will be added later to
5963 region that represents an outer loop. */
5964 static void
5965 sel_remove_loop_preheader (void)
5967 int i, old_len;
5968 int cur_rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
5969 basic_block bb;
5970 bool all_empty_p = true;
5971 VEC(basic_block, heap) *preheader_blocks
5972 = LOOP_PREHEADER_BLOCKS (loop_outer (current_loop_nest));
5974 gcc_assert (current_loop_nest);
5975 old_len = VEC_length (basic_block, preheader_blocks);
5977 /* Add blocks that aren't within the current loop to PREHEADER_BLOCKS. */
5978 for (i = 0; i < RGN_NR_BLOCKS (cur_rgn); i++)
5980 bb = BASIC_BLOCK (BB_TO_BLOCK (i));
5982 /* If the basic block belongs to region, but doesn't belong to
5983 corresponding loop, then it should be a preheader. */
5984 if (sel_is_loop_preheader_p (bb))
5986 VEC_safe_push (basic_block, heap, preheader_blocks, bb);
5987 if (BB_END (bb) != bb_note (bb))
5988 all_empty_p = false;
5992 /* Remove these blocks only after iterating over the whole region. */
5993 for (i = VEC_length (basic_block, preheader_blocks) - 1;
5994 i >= old_len;
5995 i--)
5997 bb = VEC_index (basic_block, preheader_blocks, i);
5998 sel_remove_bb (bb, false);
6001 if (!considered_for_pipelining_p (loop_outer (current_loop_nest)))
6003 if (!all_empty_p)
6004 /* Immediately create new region from preheader. */
6005 make_region_from_loop_preheader (&preheader_blocks);
6006 else
6008 /* If all preheader blocks are empty - dont create new empty region.
6009 Instead, remove them completely. */
6010 for (i = 0; VEC_iterate (basic_block, preheader_blocks, i, bb); i++)
6012 edge e;
6013 edge_iterator ei;
6014 basic_block prev_bb = bb->prev_bb, next_bb = bb->next_bb;
6016 /* Redirect all incoming edges to next basic block. */
6017 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
6019 if (! (e->flags & EDGE_FALLTHRU))
6020 redirect_edge_and_branch (e, bb->next_bb);
6021 else
6022 redirect_edge_succ (e, bb->next_bb);
6024 gcc_assert (BB_NOTE_LIST (bb) == NULL);
6025 delete_and_free_basic_block (bb);
6027 /* Check if after deleting preheader there is a nonconditional
6028 jump in PREV_BB that leads to the next basic block NEXT_BB.
6029 If it is so - delete this jump and clear data sets of its
6030 basic block if it becomes empty. */
6031 if (next_bb->prev_bb == prev_bb
6032 && prev_bb != ENTRY_BLOCK_PTR
6033 && jump_leads_only_to_bb_p (BB_END (prev_bb), next_bb))
6035 redirect_edge_and_branch (EDGE_SUCC (prev_bb, 0), next_bb);
6036 if (BB_END (prev_bb) == bb_note (prev_bb))
6037 free_data_sets (prev_bb);
6041 VEC_free (basic_block, heap, preheader_blocks);
6043 else
6044 /* Store preheader within the father's loop structure. */
6045 SET_LOOP_PREHEADER_BLOCKS (loop_outer (current_loop_nest),
6046 preheader_blocks);
6048 #endif