Merge from mainline (165734:167278).
[official-gcc/graphite-test-results.git] / gcc / sel-sched-ir.c
blob7956cd8f682fc8bac79df55d10fcfa8fa4450c64
1 /* Instruction scheduling pass. Selective scheduler and pipeliner.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010 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 "diagnostic-core.h"
25 #include "toplev.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "hard-reg-set.h"
29 #include "regs.h"
30 #include "function.h"
31 #include "flags.h"
32 #include "insn-config.h"
33 #include "insn-attr.h"
34 #include "except.h"
35 #include "toplev.h"
36 #include "recog.h"
37 #include "params.h"
38 #include "target.h"
39 #include "timevar.h"
40 #include "tree-pass.h"
41 #include "sched-int.h"
42 #include "ggc.h"
43 #include "tree.h"
44 #include "vec.h"
45 #include "langhooks.h"
46 #include "rtlhooks-def.h"
47 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
49 #ifdef INSN_SCHEDULING
50 #include "sel-sched-ir.h"
51 /* We don't have to use it except for sel_print_insn. */
52 #include "sel-sched-dump.h"
54 /* A vector holding bb info for whole scheduling pass. */
55 VEC(sel_global_bb_info_def, heap) *sel_global_bb_info = NULL;
57 /* A vector holding bb info. */
58 VEC(sel_region_bb_info_def, heap) *sel_region_bb_info = NULL;
60 /* A pool for allocating all lists. */
61 alloc_pool sched_lists_pool;
63 /* This contains information about successors for compute_av_set. */
64 struct succs_info current_succs;
66 /* Data structure to describe interaction with the generic scheduler utils. */
67 static struct common_sched_info_def sel_common_sched_info;
69 /* The loop nest being pipelined. */
70 struct loop *current_loop_nest;
72 /* LOOP_NESTS is a vector containing the corresponding loop nest for
73 each region. */
74 static VEC(loop_p, heap) *loop_nests = NULL;
76 /* Saves blocks already in loop regions, indexed by bb->index. */
77 static sbitmap bbs_in_loop_rgns = NULL;
79 /* CFG hooks that are saved before changing create_basic_block hook. */
80 static struct cfg_hooks orig_cfg_hooks;
83 /* Array containing reverse topological index of function basic blocks,
84 indexed by BB->INDEX. */
85 static int *rev_top_order_index = NULL;
87 /* Length of the above array. */
88 static int rev_top_order_index_len = -1;
90 /* A regset pool structure. */
91 static struct
93 /* The stack to which regsets are returned. */
94 regset *v;
96 /* Its pointer. */
97 int n;
99 /* Its size. */
100 int s;
102 /* In VV we save all generated regsets so that, when destructing the
103 pool, we can compare it with V and check that every regset was returned
104 back to pool. */
105 regset *vv;
107 /* The pointer of VV stack. */
108 int nn;
110 /* Its size. */
111 int ss;
113 /* The difference between allocated and returned regsets. */
114 int diff;
115 } regset_pool = { NULL, 0, 0, NULL, 0, 0, 0 };
117 /* This represents the nop pool. */
118 static struct
120 /* The vector which holds previously emitted nops. */
121 insn_t *v;
123 /* Its pointer. */
124 int n;
126 /* Its size. */
127 int s;
128 } nop_pool = { NULL, 0, 0 };
130 /* The pool for basic block notes. */
131 static rtx_vec_t bb_note_pool;
133 /* A NOP pattern used to emit placeholder insns. */
134 rtx nop_pattern = NULL_RTX;
135 /* A special instruction that resides in EXIT_BLOCK.
136 EXIT_INSN is successor of the insns that lead to EXIT_BLOCK. */
137 rtx exit_insn = NULL_RTX;
139 /* TRUE if while scheduling current region, which is loop, its preheader
140 was removed. */
141 bool preheader_removed = false;
144 /* Forward static declarations. */
145 static void fence_clear (fence_t);
147 static void deps_init_id (idata_t, insn_t, bool);
148 static void init_id_from_df (idata_t, insn_t, bool);
149 static expr_t set_insn_init (expr_t, vinsn_t, int);
151 static void cfg_preds (basic_block, insn_t **, int *);
152 static void prepare_insn_expr (insn_t, int);
153 static void free_history_vect (VEC (expr_history_def, heap) **);
155 static void move_bb_info (basic_block, basic_block);
156 static void remove_empty_bb (basic_block, bool);
157 static void sel_merge_blocks (basic_block, basic_block);
158 static void sel_remove_loop_preheader (void);
160 static bool insn_is_the_only_one_in_bb_p (insn_t);
161 static void create_initial_data_sets (basic_block);
163 static void free_av_set (basic_block);
164 static void invalidate_av_set (basic_block);
165 static void extend_insn_data (void);
166 static void sel_init_new_insn (insn_t, int);
167 static void finish_insns (void);
169 /* Various list functions. */
171 /* Copy an instruction list L. */
172 ilist_t
173 ilist_copy (ilist_t l)
175 ilist_t head = NULL, *tailp = &head;
177 while (l)
179 ilist_add (tailp, ILIST_INSN (l));
180 tailp = &ILIST_NEXT (*tailp);
181 l = ILIST_NEXT (l);
184 return head;
187 /* Invert an instruction list L. */
188 ilist_t
189 ilist_invert (ilist_t l)
191 ilist_t res = NULL;
193 while (l)
195 ilist_add (&res, ILIST_INSN (l));
196 l = ILIST_NEXT (l);
199 return res;
202 /* Add a new boundary to the LP list with parameters TO, PTR, and DC. */
203 void
204 blist_add (blist_t *lp, insn_t to, ilist_t ptr, deps_t dc)
206 bnd_t bnd;
208 _list_add (lp);
209 bnd = BLIST_BND (*lp);
211 BND_TO (bnd) = to;
212 BND_PTR (bnd) = ptr;
213 BND_AV (bnd) = NULL;
214 BND_AV1 (bnd) = NULL;
215 BND_DC (bnd) = dc;
218 /* Remove the list note pointed to by LP. */
219 void
220 blist_remove (blist_t *lp)
222 bnd_t b = BLIST_BND (*lp);
224 av_set_clear (&BND_AV (b));
225 av_set_clear (&BND_AV1 (b));
226 ilist_clear (&BND_PTR (b));
228 _list_remove (lp);
231 /* Init a fence tail L. */
232 void
233 flist_tail_init (flist_tail_t l)
235 FLIST_TAIL_HEAD (l) = NULL;
236 FLIST_TAIL_TAILP (l) = &FLIST_TAIL_HEAD (l);
239 /* Try to find fence corresponding to INSN in L. */
240 fence_t
241 flist_lookup (flist_t l, insn_t insn)
243 while (l)
245 if (FENCE_INSN (FLIST_FENCE (l)) == insn)
246 return FLIST_FENCE (l);
248 l = FLIST_NEXT (l);
251 return NULL;
254 /* Init the fields of F before running fill_insns. */
255 static void
256 init_fence_for_scheduling (fence_t f)
258 FENCE_BNDS (f) = NULL;
259 FENCE_PROCESSED_P (f) = false;
260 FENCE_SCHEDULED_P (f) = false;
263 /* Add new fence consisting of INSN and STATE to the list pointed to by LP. */
264 static void
265 flist_add (flist_t *lp, insn_t insn, state_t state, deps_t dc, void *tc,
266 insn_t last_scheduled_insn, VEC(rtx,gc) *executing_insns,
267 int *ready_ticks, int ready_ticks_size, insn_t sched_next,
268 int cycle, int cycle_issued_insns, int issue_more,
269 bool starts_cycle_p, bool after_stall_p)
271 fence_t f;
273 _list_add (lp);
274 f = FLIST_FENCE (*lp);
276 FENCE_INSN (f) = insn;
278 gcc_assert (state != NULL);
279 FENCE_STATE (f) = state;
281 FENCE_CYCLE (f) = cycle;
282 FENCE_ISSUED_INSNS (f) = cycle_issued_insns;
283 FENCE_STARTS_CYCLE_P (f) = starts_cycle_p;
284 FENCE_AFTER_STALL_P (f) = after_stall_p;
286 gcc_assert (dc != NULL);
287 FENCE_DC (f) = dc;
289 gcc_assert (tc != NULL || targetm.sched.alloc_sched_context == NULL);
290 FENCE_TC (f) = tc;
292 FENCE_LAST_SCHEDULED_INSN (f) = last_scheduled_insn;
293 FENCE_ISSUE_MORE (f) = issue_more;
294 FENCE_EXECUTING_INSNS (f) = executing_insns;
295 FENCE_READY_TICKS (f) = ready_ticks;
296 FENCE_READY_TICKS_SIZE (f) = ready_ticks_size;
297 FENCE_SCHED_NEXT (f) = sched_next;
299 init_fence_for_scheduling (f);
302 /* Remove the head node of the list pointed to by LP. */
303 static void
304 flist_remove (flist_t *lp)
306 if (FENCE_INSN (FLIST_FENCE (*lp)))
307 fence_clear (FLIST_FENCE (*lp));
308 _list_remove (lp);
311 /* Clear the fence list pointed to by LP. */
312 void
313 flist_clear (flist_t *lp)
315 while (*lp)
316 flist_remove (lp);
319 /* Add ORIGINAL_INSN the def list DL honoring CROSSES_CALL. */
320 void
321 def_list_add (def_list_t *dl, insn_t original_insn, bool crosses_call)
323 def_t d;
325 _list_add (dl);
326 d = DEF_LIST_DEF (*dl);
328 d->orig_insn = original_insn;
329 d->crosses_call = crosses_call;
333 /* Functions to work with target contexts. */
335 /* Bulk target context. It is convenient for debugging purposes to ensure
336 that there are no uninitialized (null) target contexts. */
337 static tc_t bulk_tc = (tc_t) 1;
339 /* Target hooks wrappers. In the future we can provide some default
340 implementations for them. */
342 /* Allocate a store for the target context. */
343 static tc_t
344 alloc_target_context (void)
346 return (targetm.sched.alloc_sched_context
347 ? targetm.sched.alloc_sched_context () : bulk_tc);
350 /* Init target context TC.
351 If CLEAN_P is true, then make TC as it is beginning of the scheduler.
352 Overwise, copy current backend context to TC. */
353 static void
354 init_target_context (tc_t tc, bool clean_p)
356 if (targetm.sched.init_sched_context)
357 targetm.sched.init_sched_context (tc, clean_p);
360 /* Allocate and initialize a target context. Meaning of CLEAN_P is the same as
361 int init_target_context (). */
362 tc_t
363 create_target_context (bool clean_p)
365 tc_t tc = alloc_target_context ();
367 init_target_context (tc, clean_p);
368 return tc;
371 /* Copy TC to the current backend context. */
372 void
373 set_target_context (tc_t tc)
375 if (targetm.sched.set_sched_context)
376 targetm.sched.set_sched_context (tc);
379 /* TC is about to be destroyed. Free any internal data. */
380 static void
381 clear_target_context (tc_t tc)
383 if (targetm.sched.clear_sched_context)
384 targetm.sched.clear_sched_context (tc);
387 /* Clear and free it. */
388 static void
389 delete_target_context (tc_t tc)
391 clear_target_context (tc);
393 if (targetm.sched.free_sched_context)
394 targetm.sched.free_sched_context (tc);
397 /* Make a copy of FROM in TO.
398 NB: May be this should be a hook. */
399 static void
400 copy_target_context (tc_t to, tc_t from)
402 tc_t tmp = create_target_context (false);
404 set_target_context (from);
405 init_target_context (to, false);
407 set_target_context (tmp);
408 delete_target_context (tmp);
411 /* Create a copy of TC. */
412 static tc_t
413 create_copy_of_target_context (tc_t tc)
415 tc_t copy = alloc_target_context ();
417 copy_target_context (copy, tc);
419 return copy;
422 /* Clear TC and initialize it according to CLEAN_P. The meaning of CLEAN_P
423 is the same as in init_target_context (). */
424 void
425 reset_target_context (tc_t tc, bool clean_p)
427 clear_target_context (tc);
428 init_target_context (tc, clean_p);
431 /* Functions to work with dependence contexts.
432 Dc (aka deps context, aka deps_t, aka struct deps_desc *) is short for dependence
433 context. It accumulates information about processed insns to decide if
434 current insn is dependent on the processed ones. */
436 /* Make a copy of FROM in TO. */
437 static void
438 copy_deps_context (deps_t to, deps_t from)
440 init_deps (to, false);
441 deps_join (to, from);
444 /* Allocate store for dep context. */
445 static deps_t
446 alloc_deps_context (void)
448 return XNEW (struct deps_desc);
451 /* Allocate and initialize dep context. */
452 static deps_t
453 create_deps_context (void)
455 deps_t dc = alloc_deps_context ();
457 init_deps (dc, false);
458 return dc;
461 /* Create a copy of FROM. */
462 static deps_t
463 create_copy_of_deps_context (deps_t from)
465 deps_t to = alloc_deps_context ();
467 copy_deps_context (to, from);
468 return to;
471 /* Clean up internal data of DC. */
472 static void
473 clear_deps_context (deps_t dc)
475 free_deps (dc);
478 /* Clear and free DC. */
479 static void
480 delete_deps_context (deps_t dc)
482 clear_deps_context (dc);
483 free (dc);
486 /* Clear and init DC. */
487 static void
488 reset_deps_context (deps_t dc)
490 clear_deps_context (dc);
491 init_deps (dc, false);
494 /* This structure describes the dependence analysis hooks for advancing
495 dependence context. */
496 static struct sched_deps_info_def advance_deps_context_sched_deps_info =
498 NULL,
500 NULL, /* start_insn */
501 NULL, /* finish_insn */
502 NULL, /* start_lhs */
503 NULL, /* finish_lhs */
504 NULL, /* start_rhs */
505 NULL, /* finish_rhs */
506 haifa_note_reg_set,
507 haifa_note_reg_clobber,
508 haifa_note_reg_use,
509 NULL, /* note_mem_dep */
510 NULL, /* note_dep */
512 0, 0, 0
515 /* Process INSN and add its impact on DC. */
516 void
517 advance_deps_context (deps_t dc, insn_t insn)
519 sched_deps_info = &advance_deps_context_sched_deps_info;
520 deps_analyze_insn (dc, insn);
524 /* Functions to work with DFA states. */
526 /* Allocate store for a DFA state. */
527 static state_t
528 state_alloc (void)
530 return xmalloc (dfa_state_size);
533 /* Allocate and initialize DFA state. */
534 static state_t
535 state_create (void)
537 state_t state = state_alloc ();
539 state_reset (state);
540 advance_state (state);
541 return state;
544 /* Free DFA state. */
545 static void
546 state_free (state_t state)
548 free (state);
551 /* Make a copy of FROM in TO. */
552 static void
553 state_copy (state_t to, state_t from)
555 memcpy (to, from, dfa_state_size);
558 /* Create a copy of FROM. */
559 static state_t
560 state_create_copy (state_t from)
562 state_t to = state_alloc ();
564 state_copy (to, from);
565 return to;
569 /* Functions to work with fences. */
571 /* Clear the fence. */
572 static void
573 fence_clear (fence_t f)
575 state_t s = FENCE_STATE (f);
576 deps_t dc = FENCE_DC (f);
577 void *tc = FENCE_TC (f);
579 ilist_clear (&FENCE_BNDS (f));
581 gcc_assert ((s != NULL && dc != NULL && tc != NULL)
582 || (s == NULL && dc == NULL && tc == NULL));
584 if (s != NULL)
585 free (s);
587 if (dc != NULL)
588 delete_deps_context (dc);
590 if (tc != NULL)
591 delete_target_context (tc);
592 VEC_free (rtx, gc, FENCE_EXECUTING_INSNS (f));
593 free (FENCE_READY_TICKS (f));
594 FENCE_READY_TICKS (f) = NULL;
597 /* Init a list of fences with successors of OLD_FENCE. */
598 void
599 init_fences (insn_t old_fence)
601 insn_t succ;
602 succ_iterator si;
603 bool first = true;
604 int ready_ticks_size = get_max_uid () + 1;
606 FOR_EACH_SUCC_1 (succ, si, old_fence,
607 SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
610 if (first)
611 first = false;
612 else
613 gcc_assert (flag_sel_sched_pipelining_outer_loops);
615 flist_add (&fences, succ,
616 state_create (),
617 create_deps_context () /* dc */,
618 create_target_context (true) /* tc */,
619 NULL_RTX /* last_scheduled_insn */,
620 NULL, /* executing_insns */
621 XCNEWVEC (int, ready_ticks_size), /* ready_ticks */
622 ready_ticks_size,
623 NULL_RTX /* sched_next */,
624 1 /* cycle */, 0 /* cycle_issued_insns */,
625 issue_rate, /* issue_more */
626 1 /* starts_cycle_p */, 0 /* after_stall_p */);
630 /* Merges two fences (filling fields of fence F with resulting values) by
631 following rules: 1) state, target context and last scheduled insn are
632 propagated from fallthrough edge if it is available;
633 2) deps context and cycle is propagated from more probable edge;
634 3) all other fields are set to corresponding constant values.
636 INSN, STATE, DC, TC, LAST_SCHEDULED_INSN, EXECUTING_INSNS,
637 READY_TICKS, READY_TICKS_SIZE, SCHED_NEXT, CYCLE, ISSUE_MORE
638 and AFTER_STALL_P are the corresponding fields of the second fence. */
639 static void
640 merge_fences (fence_t f, insn_t insn,
641 state_t state, deps_t dc, void *tc,
642 rtx last_scheduled_insn, VEC(rtx, gc) *executing_insns,
643 int *ready_ticks, int ready_ticks_size,
644 rtx sched_next, int cycle, int issue_more, bool after_stall_p)
646 insn_t last_scheduled_insn_old = FENCE_LAST_SCHEDULED_INSN (f);
648 gcc_assert (sel_bb_head_p (FENCE_INSN (f))
649 && !sched_next && !FENCE_SCHED_NEXT (f));
651 /* Check if we can decide which path fences came.
652 If we can't (or don't want to) - reset all. */
653 if (last_scheduled_insn == NULL
654 || last_scheduled_insn_old == NULL
655 /* This is a case when INSN is reachable on several paths from
656 one insn (this can happen when pipelining of outer loops is on and
657 there are two edges: one going around of inner loop and the other -
658 right through it; in such case just reset everything). */
659 || last_scheduled_insn == last_scheduled_insn_old)
661 state_reset (FENCE_STATE (f));
662 state_free (state);
664 reset_deps_context (FENCE_DC (f));
665 delete_deps_context (dc);
667 reset_target_context (FENCE_TC (f), true);
668 delete_target_context (tc);
670 if (cycle > FENCE_CYCLE (f))
671 FENCE_CYCLE (f) = cycle;
673 FENCE_LAST_SCHEDULED_INSN (f) = NULL;
674 FENCE_ISSUE_MORE (f) = issue_rate;
675 VEC_free (rtx, gc, executing_insns);
676 free (ready_ticks);
677 if (FENCE_EXECUTING_INSNS (f))
678 VEC_block_remove (rtx, FENCE_EXECUTING_INSNS (f), 0,
679 VEC_length (rtx, FENCE_EXECUTING_INSNS (f)));
680 if (FENCE_READY_TICKS (f))
681 memset (FENCE_READY_TICKS (f), 0, FENCE_READY_TICKS_SIZE (f));
683 else
685 edge edge_old = NULL, edge_new = NULL;
686 edge candidate;
687 succ_iterator si;
688 insn_t succ;
690 /* Find fallthrough edge. */
691 gcc_assert (BLOCK_FOR_INSN (insn)->prev_bb);
692 candidate = find_fallthru_edge_from (BLOCK_FOR_INSN (insn)->prev_bb);
694 if (!candidate
695 || (candidate->src != BLOCK_FOR_INSN (last_scheduled_insn)
696 && candidate->src != BLOCK_FOR_INSN (last_scheduled_insn_old)))
698 /* No fallthrough edge leading to basic block of INSN. */
699 state_reset (FENCE_STATE (f));
700 state_free (state);
702 reset_target_context (FENCE_TC (f), true);
703 delete_target_context (tc);
705 FENCE_LAST_SCHEDULED_INSN (f) = NULL;
706 FENCE_ISSUE_MORE (f) = issue_rate;
708 else
709 if (candidate->src == BLOCK_FOR_INSN (last_scheduled_insn))
711 /* Would be weird if same insn is successor of several fallthrough
712 edges. */
713 gcc_assert (BLOCK_FOR_INSN (insn)->prev_bb
714 != BLOCK_FOR_INSN (last_scheduled_insn_old));
716 state_free (FENCE_STATE (f));
717 FENCE_STATE (f) = state;
719 delete_target_context (FENCE_TC (f));
720 FENCE_TC (f) = tc;
722 FENCE_LAST_SCHEDULED_INSN (f) = last_scheduled_insn;
723 FENCE_ISSUE_MORE (f) = issue_more;
725 else
727 /* Leave STATE, TC and LAST_SCHEDULED_INSN fields untouched. */
728 state_free (state);
729 delete_target_context (tc);
731 gcc_assert (BLOCK_FOR_INSN (insn)->prev_bb
732 != BLOCK_FOR_INSN (last_scheduled_insn));
735 /* Find edge of first predecessor (last_scheduled_insn_old->insn). */
736 FOR_EACH_SUCC_1 (succ, si, last_scheduled_insn_old,
737 SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
739 if (succ == insn)
741 /* No same successor allowed from several edges. */
742 gcc_assert (!edge_old);
743 edge_old = si.e1;
746 /* Find edge of second predecessor (last_scheduled_insn->insn). */
747 FOR_EACH_SUCC_1 (succ, si, last_scheduled_insn,
748 SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
750 if (succ == insn)
752 /* No same successor allowed from several edges. */
753 gcc_assert (!edge_new);
754 edge_new = si.e1;
758 /* Check if we can choose most probable predecessor. */
759 if (edge_old == NULL || edge_new == NULL)
761 reset_deps_context (FENCE_DC (f));
762 delete_deps_context (dc);
763 VEC_free (rtx, gc, executing_insns);
764 free (ready_ticks);
766 FENCE_CYCLE (f) = MAX (FENCE_CYCLE (f), cycle);
767 if (FENCE_EXECUTING_INSNS (f))
768 VEC_block_remove (rtx, FENCE_EXECUTING_INSNS (f), 0,
769 VEC_length (rtx, FENCE_EXECUTING_INSNS (f)));
770 if (FENCE_READY_TICKS (f))
771 memset (FENCE_READY_TICKS (f), 0, FENCE_READY_TICKS_SIZE (f));
773 else
774 if (edge_new->probability > edge_old->probability)
776 delete_deps_context (FENCE_DC (f));
777 FENCE_DC (f) = dc;
778 VEC_free (rtx, gc, FENCE_EXECUTING_INSNS (f));
779 FENCE_EXECUTING_INSNS (f) = executing_insns;
780 free (FENCE_READY_TICKS (f));
781 FENCE_READY_TICKS (f) = ready_ticks;
782 FENCE_READY_TICKS_SIZE (f) = ready_ticks_size;
783 FENCE_CYCLE (f) = cycle;
785 else
787 /* Leave DC and CYCLE untouched. */
788 delete_deps_context (dc);
789 VEC_free (rtx, gc, executing_insns);
790 free (ready_ticks);
794 /* Fill remaining invariant fields. */
795 if (after_stall_p)
796 FENCE_AFTER_STALL_P (f) = 1;
798 FENCE_ISSUED_INSNS (f) = 0;
799 FENCE_STARTS_CYCLE_P (f) = 1;
800 FENCE_SCHED_NEXT (f) = NULL;
803 /* Add a new fence to NEW_FENCES list, initializing it from all
804 other parameters. */
805 static void
806 add_to_fences (flist_tail_t new_fences, insn_t insn,
807 state_t state, deps_t dc, void *tc, rtx last_scheduled_insn,
808 VEC(rtx, gc) *executing_insns, int *ready_ticks,
809 int ready_ticks_size, rtx sched_next, int cycle,
810 int cycle_issued_insns, int issue_rate,
811 bool starts_cycle_p, bool after_stall_p)
813 fence_t f = flist_lookup (FLIST_TAIL_HEAD (new_fences), insn);
815 if (! f)
817 flist_add (FLIST_TAIL_TAILP (new_fences), insn, state, dc, tc,
818 last_scheduled_insn, executing_insns, ready_ticks,
819 ready_ticks_size, sched_next, cycle, cycle_issued_insns,
820 issue_rate, starts_cycle_p, after_stall_p);
822 FLIST_TAIL_TAILP (new_fences)
823 = &FLIST_NEXT (*FLIST_TAIL_TAILP (new_fences));
825 else
827 merge_fences (f, insn, state, dc, tc, last_scheduled_insn,
828 executing_insns, ready_ticks, ready_ticks_size,
829 sched_next, cycle, issue_rate, after_stall_p);
833 /* Move the first fence in the OLD_FENCES list to NEW_FENCES. */
834 void
835 move_fence_to_fences (flist_t old_fences, flist_tail_t new_fences)
837 fence_t f, old;
838 flist_t *tailp = FLIST_TAIL_TAILP (new_fences);
840 old = FLIST_FENCE (old_fences);
841 f = flist_lookup (FLIST_TAIL_HEAD (new_fences),
842 FENCE_INSN (FLIST_FENCE (old_fences)));
843 if (f)
845 merge_fences (f, old->insn, old->state, old->dc, old->tc,
846 old->last_scheduled_insn, old->executing_insns,
847 old->ready_ticks, old->ready_ticks_size,
848 old->sched_next, old->cycle, old->issue_more,
849 old->after_stall_p);
851 else
853 _list_add (tailp);
854 FLIST_TAIL_TAILP (new_fences) = &FLIST_NEXT (*tailp);
855 *FLIST_FENCE (*tailp) = *old;
856 init_fence_for_scheduling (FLIST_FENCE (*tailp));
858 FENCE_INSN (old) = NULL;
861 /* Add a new fence to NEW_FENCES list and initialize most of its data
862 as a clean one. */
863 void
864 add_clean_fence_to_fences (flist_tail_t new_fences, insn_t succ, fence_t fence)
866 int ready_ticks_size = get_max_uid () + 1;
868 add_to_fences (new_fences,
869 succ, state_create (), create_deps_context (),
870 create_target_context (true),
871 NULL_RTX, NULL,
872 XCNEWVEC (int, ready_ticks_size), ready_ticks_size,
873 NULL_RTX, FENCE_CYCLE (fence) + 1,
874 0, issue_rate, 1, FENCE_AFTER_STALL_P (fence));
877 /* Add a new fence to NEW_FENCES list and initialize all of its data
878 from FENCE and SUCC. */
879 void
880 add_dirty_fence_to_fences (flist_tail_t new_fences, insn_t succ, fence_t fence)
882 int * new_ready_ticks
883 = XNEWVEC (int, FENCE_READY_TICKS_SIZE (fence));
885 memcpy (new_ready_ticks, FENCE_READY_TICKS (fence),
886 FENCE_READY_TICKS_SIZE (fence) * sizeof (int));
887 add_to_fences (new_fences,
888 succ, state_create_copy (FENCE_STATE (fence)),
889 create_copy_of_deps_context (FENCE_DC (fence)),
890 create_copy_of_target_context (FENCE_TC (fence)),
891 FENCE_LAST_SCHEDULED_INSN (fence),
892 VEC_copy (rtx, gc, FENCE_EXECUTING_INSNS (fence)),
893 new_ready_ticks,
894 FENCE_READY_TICKS_SIZE (fence),
895 FENCE_SCHED_NEXT (fence),
896 FENCE_CYCLE (fence),
897 FENCE_ISSUED_INSNS (fence),
898 FENCE_ISSUE_MORE (fence),
899 FENCE_STARTS_CYCLE_P (fence),
900 FENCE_AFTER_STALL_P (fence));
904 /* Functions to work with regset and nop pools. */
906 /* Returns the new regset from pool. It might have some of the bits set
907 from the previous usage. */
908 regset
909 get_regset_from_pool (void)
911 regset rs;
913 if (regset_pool.n != 0)
914 rs = regset_pool.v[--regset_pool.n];
915 else
916 /* We need to create the regset. */
918 rs = ALLOC_REG_SET (&reg_obstack);
920 if (regset_pool.nn == regset_pool.ss)
921 regset_pool.vv = XRESIZEVEC (regset, regset_pool.vv,
922 (regset_pool.ss = 2 * regset_pool.ss + 1));
923 regset_pool.vv[regset_pool.nn++] = rs;
926 regset_pool.diff++;
928 return rs;
931 /* Same as above, but returns the empty regset. */
932 regset
933 get_clear_regset_from_pool (void)
935 regset rs = get_regset_from_pool ();
937 CLEAR_REG_SET (rs);
938 return rs;
941 /* Return regset RS to the pool for future use. */
942 void
943 return_regset_to_pool (regset rs)
945 gcc_assert (rs);
946 regset_pool.diff--;
948 if (regset_pool.n == regset_pool.s)
949 regset_pool.v = XRESIZEVEC (regset, regset_pool.v,
950 (regset_pool.s = 2 * regset_pool.s + 1));
951 regset_pool.v[regset_pool.n++] = rs;
954 #ifdef ENABLE_CHECKING
955 /* This is used as a qsort callback for sorting regset pool stacks.
956 X and XX are addresses of two regsets. They are never equal. */
957 static int
958 cmp_v_in_regset_pool (const void *x, const void *xx)
960 return *((const regset *) x) - *((const regset *) xx);
962 #endif
964 /* Free the regset pool possibly checking for memory leaks. */
965 void
966 free_regset_pool (void)
968 #ifdef ENABLE_CHECKING
970 regset *v = regset_pool.v;
971 int i = 0;
972 int n = regset_pool.n;
974 regset *vv = regset_pool.vv;
975 int ii = 0;
976 int nn = regset_pool.nn;
978 int diff = 0;
980 gcc_assert (n <= nn);
982 /* Sort both vectors so it will be possible to compare them. */
983 qsort (v, n, sizeof (*v), cmp_v_in_regset_pool);
984 qsort (vv, nn, sizeof (*vv), cmp_v_in_regset_pool);
986 while (ii < nn)
988 if (v[i] == vv[ii])
989 i++;
990 else
991 /* VV[II] was lost. */
992 diff++;
994 ii++;
997 gcc_assert (diff == regset_pool.diff);
999 #endif
1001 /* If not true - we have a memory leak. */
1002 gcc_assert (regset_pool.diff == 0);
1004 while (regset_pool.n)
1006 --regset_pool.n;
1007 FREE_REG_SET (regset_pool.v[regset_pool.n]);
1010 free (regset_pool.v);
1011 regset_pool.v = NULL;
1012 regset_pool.s = 0;
1014 free (regset_pool.vv);
1015 regset_pool.vv = NULL;
1016 regset_pool.nn = 0;
1017 regset_pool.ss = 0;
1019 regset_pool.diff = 0;
1023 /* Functions to work with nop pools. NOP insns are used as temporary
1024 placeholders of the insns being scheduled to allow correct update of
1025 the data sets. When update is finished, NOPs are deleted. */
1027 /* A vinsn that is used to represent a nop. This vinsn is shared among all
1028 nops sel-sched generates. */
1029 static vinsn_t nop_vinsn = NULL;
1031 /* Emit a nop before INSN, taking it from pool. */
1032 insn_t
1033 get_nop_from_pool (insn_t insn)
1035 insn_t nop;
1036 bool old_p = nop_pool.n != 0;
1037 int flags;
1039 if (old_p)
1040 nop = nop_pool.v[--nop_pool.n];
1041 else
1042 nop = nop_pattern;
1044 nop = emit_insn_before (nop, insn);
1046 if (old_p)
1047 flags = INSN_INIT_TODO_SSID;
1048 else
1049 flags = INSN_INIT_TODO_LUID | INSN_INIT_TODO_SSID;
1051 set_insn_init (INSN_EXPR (insn), nop_vinsn, INSN_SEQNO (insn));
1052 sel_init_new_insn (nop, flags);
1054 return nop;
1057 /* Remove NOP from the instruction stream and return it to the pool. */
1058 void
1059 return_nop_to_pool (insn_t nop, bool full_tidying)
1061 gcc_assert (INSN_IN_STREAM_P (nop));
1062 sel_remove_insn (nop, false, full_tidying);
1064 if (nop_pool.n == nop_pool.s)
1065 nop_pool.v = XRESIZEVEC (rtx, nop_pool.v,
1066 (nop_pool.s = 2 * nop_pool.s + 1));
1067 nop_pool.v[nop_pool.n++] = nop;
1070 /* Free the nop pool. */
1071 void
1072 free_nop_pool (void)
1074 nop_pool.n = 0;
1075 nop_pool.s = 0;
1076 free (nop_pool.v);
1077 nop_pool.v = NULL;
1081 /* Skip unspec to support ia64 speculation. Called from rtx_equal_p_cb.
1082 The callback is given two rtxes XX and YY and writes the new rtxes
1083 to NX and NY in case some needs to be skipped. */
1084 static int
1085 skip_unspecs_callback (const_rtx *xx, const_rtx *yy, rtx *nx, rtx* ny)
1087 const_rtx x = *xx;
1088 const_rtx y = *yy;
1090 if (GET_CODE (x) == UNSPEC
1091 && (targetm.sched.skip_rtx_p == NULL
1092 || targetm.sched.skip_rtx_p (x)))
1094 *nx = XVECEXP (x, 0, 0);
1095 *ny = CONST_CAST_RTX (y);
1096 return 1;
1099 if (GET_CODE (y) == UNSPEC
1100 && (targetm.sched.skip_rtx_p == NULL
1101 || targetm.sched.skip_rtx_p (y)))
1103 *nx = CONST_CAST_RTX (x);
1104 *ny = XVECEXP (y, 0, 0);
1105 return 1;
1108 return 0;
1111 /* Callback, called from hash_rtx_cb. Helps to hash UNSPEC rtx X in a correct way
1112 to support ia64 speculation. When changes are needed, new rtx X and new mode
1113 NMODE are written, and the callback returns true. */
1114 static int
1115 hash_with_unspec_callback (const_rtx x, enum machine_mode mode ATTRIBUTE_UNUSED,
1116 rtx *nx, enum machine_mode* nmode)
1118 if (GET_CODE (x) == UNSPEC
1119 && targetm.sched.skip_rtx_p
1120 && targetm.sched.skip_rtx_p (x))
1122 *nx = XVECEXP (x, 0 ,0);
1123 *nmode = VOIDmode;
1124 return 1;
1127 return 0;
1130 /* Returns LHS and RHS are ok to be scheduled separately. */
1131 static bool
1132 lhs_and_rhs_separable_p (rtx lhs, rtx rhs)
1134 if (lhs == NULL || rhs == NULL)
1135 return false;
1137 /* Do not schedule CONST, CONST_INT and CONST_DOUBLE etc as rhs: no point
1138 to use reg, if const can be used. Moreover, scheduling const as rhs may
1139 lead to mode mismatch cause consts don't have modes but they could be
1140 merged from branches where the same const used in different modes. */
1141 if (CONSTANT_P (rhs))
1142 return false;
1144 /* ??? Do not rename predicate registers to avoid ICEs in bundling. */
1145 if (COMPARISON_P (rhs))
1146 return false;
1148 /* Do not allow single REG to be an rhs. */
1149 if (REG_P (rhs))
1150 return false;
1152 /* See comment at find_used_regs_1 (*1) for explanation of this
1153 restriction. */
1154 /* FIXME: remove this later. */
1155 if (MEM_P (lhs))
1156 return false;
1158 /* This will filter all tricky things like ZERO_EXTRACT etc.
1159 For now we don't handle it. */
1160 if (!REG_P (lhs) && !MEM_P (lhs))
1161 return false;
1163 return true;
1166 /* Initialize vinsn VI for INSN. Only for use from vinsn_create (). When
1167 FORCE_UNIQUE_P is true, the resulting vinsn will not be clonable. This is
1168 used e.g. for insns from recovery blocks. */
1169 static void
1170 vinsn_init (vinsn_t vi, insn_t insn, bool force_unique_p)
1172 hash_rtx_callback_function hrcf;
1173 int insn_class;
1175 VINSN_INSN_RTX (vi) = insn;
1176 VINSN_COUNT (vi) = 0;
1177 vi->cost = -1;
1179 if (INSN_NOP_P (insn))
1180 return;
1182 if (DF_INSN_UID_SAFE_GET (INSN_UID (insn)) != NULL)
1183 init_id_from_df (VINSN_ID (vi), insn, force_unique_p);
1184 else
1185 deps_init_id (VINSN_ID (vi), insn, force_unique_p);
1187 /* Hash vinsn depending on whether it is separable or not. */
1188 hrcf = targetm.sched.skip_rtx_p ? hash_with_unspec_callback : NULL;
1189 if (VINSN_SEPARABLE_P (vi))
1191 rtx rhs = VINSN_RHS (vi);
1193 VINSN_HASH (vi) = hash_rtx_cb (rhs, GET_MODE (rhs),
1194 NULL, NULL, false, hrcf);
1195 VINSN_HASH_RTX (vi) = hash_rtx_cb (VINSN_PATTERN (vi),
1196 VOIDmode, NULL, NULL,
1197 false, hrcf);
1199 else
1201 VINSN_HASH (vi) = hash_rtx_cb (VINSN_PATTERN (vi), VOIDmode,
1202 NULL, NULL, false, hrcf);
1203 VINSN_HASH_RTX (vi) = VINSN_HASH (vi);
1206 insn_class = haifa_classify_insn (insn);
1207 if (insn_class >= 2
1208 && (!targetm.sched.get_insn_spec_ds
1209 || ((targetm.sched.get_insn_spec_ds (insn) & BEGIN_CONTROL)
1210 == 0)))
1211 VINSN_MAY_TRAP_P (vi) = true;
1212 else
1213 VINSN_MAY_TRAP_P (vi) = false;
1216 /* Indicate that VI has become the part of an rtx object. */
1217 void
1218 vinsn_attach (vinsn_t vi)
1220 /* Assert that VI is not pending for deletion. */
1221 gcc_assert (VINSN_INSN_RTX (vi));
1223 VINSN_COUNT (vi)++;
1226 /* Create and init VI from the INSN. Use UNIQUE_P for determining the correct
1227 VINSN_TYPE (VI). */
1228 static vinsn_t
1229 vinsn_create (insn_t insn, bool force_unique_p)
1231 vinsn_t vi = XCNEW (struct vinsn_def);
1233 vinsn_init (vi, insn, force_unique_p);
1234 return vi;
1237 /* Return a copy of VI. When REATTACH_P is true, detach VI and attach
1238 the copy. */
1239 vinsn_t
1240 vinsn_copy (vinsn_t vi, bool reattach_p)
1242 rtx copy;
1243 bool unique = VINSN_UNIQUE_P (vi);
1244 vinsn_t new_vi;
1246 copy = create_copy_of_insn_rtx (VINSN_INSN_RTX (vi));
1247 new_vi = create_vinsn_from_insn_rtx (copy, unique);
1248 if (reattach_p)
1250 vinsn_detach (vi);
1251 vinsn_attach (new_vi);
1254 return new_vi;
1257 /* Delete the VI vinsn and free its data. */
1258 static void
1259 vinsn_delete (vinsn_t vi)
1261 gcc_assert (VINSN_COUNT (vi) == 0);
1263 if (!INSN_NOP_P (VINSN_INSN_RTX (vi)))
1265 return_regset_to_pool (VINSN_REG_SETS (vi));
1266 return_regset_to_pool (VINSN_REG_USES (vi));
1267 return_regset_to_pool (VINSN_REG_CLOBBERS (vi));
1270 free (vi);
1273 /* Indicate that VI is no longer a part of some rtx object.
1274 Remove VI if it is no longer needed. */
1275 void
1276 vinsn_detach (vinsn_t vi)
1278 gcc_assert (VINSN_COUNT (vi) > 0);
1280 if (--VINSN_COUNT (vi) == 0)
1281 vinsn_delete (vi);
1284 /* Returns TRUE if VI is a branch. */
1285 bool
1286 vinsn_cond_branch_p (vinsn_t vi)
1288 insn_t insn;
1290 if (!VINSN_UNIQUE_P (vi))
1291 return false;
1293 insn = VINSN_INSN_RTX (vi);
1294 if (BB_END (BLOCK_FOR_INSN (insn)) != insn)
1295 return false;
1297 return control_flow_insn_p (insn);
1300 /* Return latency of INSN. */
1301 static int
1302 sel_insn_rtx_cost (rtx insn)
1304 int cost;
1306 /* A USE insn, or something else we don't need to
1307 understand. We can't pass these directly to
1308 result_ready_cost or insn_default_latency because it will
1309 trigger a fatal error for unrecognizable insns. */
1310 if (recog_memoized (insn) < 0)
1311 cost = 0;
1312 else
1314 cost = insn_default_latency (insn);
1316 if (cost < 0)
1317 cost = 0;
1320 return cost;
1323 /* Return the cost of the VI.
1324 !!! FIXME: Unify with haifa-sched.c: insn_cost (). */
1326 sel_vinsn_cost (vinsn_t vi)
1328 int cost = vi->cost;
1330 if (cost < 0)
1332 cost = sel_insn_rtx_cost (VINSN_INSN_RTX (vi));
1333 vi->cost = cost;
1336 return cost;
1340 /* Functions for insn emitting. */
1342 /* Emit new insn after AFTER based on PATTERN and initialize its data from
1343 EXPR and SEQNO. */
1344 insn_t
1345 sel_gen_insn_from_rtx_after (rtx pattern, expr_t expr, int seqno, insn_t after)
1347 insn_t new_insn;
1349 gcc_assert (EXPR_TARGET_AVAILABLE (expr) == true);
1351 new_insn = emit_insn_after (pattern, after);
1352 set_insn_init (expr, NULL, seqno);
1353 sel_init_new_insn (new_insn, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SSID);
1355 return new_insn;
1358 /* Force newly generated vinsns to be unique. */
1359 static bool init_insn_force_unique_p = false;
1361 /* Emit new speculation recovery insn after AFTER based on PATTERN and
1362 initialize its data from EXPR and SEQNO. */
1363 insn_t
1364 sel_gen_recovery_insn_from_rtx_after (rtx pattern, expr_t expr, int seqno,
1365 insn_t after)
1367 insn_t insn;
1369 gcc_assert (!init_insn_force_unique_p);
1371 init_insn_force_unique_p = true;
1372 insn = sel_gen_insn_from_rtx_after (pattern, expr, seqno, after);
1373 CANT_MOVE (insn) = 1;
1374 init_insn_force_unique_p = false;
1376 return insn;
1379 /* Emit new insn after AFTER based on EXPR and SEQNO. If VINSN is not NULL,
1380 take it as a new vinsn instead of EXPR's vinsn.
1381 We simplify insns later, after scheduling region in
1382 simplify_changed_insns. */
1383 insn_t
1384 sel_gen_insn_from_expr_after (expr_t expr, vinsn_t vinsn, int seqno,
1385 insn_t after)
1387 expr_t emit_expr;
1388 insn_t insn;
1389 int flags;
1391 emit_expr = set_insn_init (expr, vinsn ? vinsn : EXPR_VINSN (expr),
1392 seqno);
1393 insn = EXPR_INSN_RTX (emit_expr);
1394 add_insn_after (insn, after, BLOCK_FOR_INSN (insn));
1396 flags = INSN_INIT_TODO_SSID;
1397 if (INSN_LUID (insn) == 0)
1398 flags |= INSN_INIT_TODO_LUID;
1399 sel_init_new_insn (insn, flags);
1401 return insn;
1404 /* Move insn from EXPR after AFTER. */
1405 insn_t
1406 sel_move_insn (expr_t expr, int seqno, insn_t after)
1408 insn_t insn = EXPR_INSN_RTX (expr);
1409 basic_block bb = BLOCK_FOR_INSN (after);
1410 insn_t next = NEXT_INSN (after);
1412 /* Assert that in move_op we disconnected this insn properly. */
1413 gcc_assert (EXPR_VINSN (INSN_EXPR (insn)) != NULL);
1414 PREV_INSN (insn) = after;
1415 NEXT_INSN (insn) = next;
1417 NEXT_INSN (after) = insn;
1418 PREV_INSN (next) = insn;
1420 /* Update links from insn to bb and vice versa. */
1421 df_insn_change_bb (insn, bb);
1422 if (BB_END (bb) == after)
1423 BB_END (bb) = insn;
1425 prepare_insn_expr (insn, seqno);
1426 return insn;
1430 /* Functions to work with right-hand sides. */
1432 /* Search for a hash value determined by UID/NEW_VINSN in a sorted vector
1433 VECT and return true when found. Use NEW_VINSN for comparison only when
1434 COMPARE_VINSNS is true. Write to INDP the index on which
1435 the search has stopped, such that inserting the new element at INDP will
1436 retain VECT's sort order. */
1437 static bool
1438 find_in_history_vect_1 (VEC(expr_history_def, heap) *vect,
1439 unsigned uid, vinsn_t new_vinsn,
1440 bool compare_vinsns, int *indp)
1442 expr_history_def *arr;
1443 int i, j, len = VEC_length (expr_history_def, vect);
1445 if (len == 0)
1447 *indp = 0;
1448 return false;
1451 arr = VEC_address (expr_history_def, vect);
1452 i = 0, j = len - 1;
1454 while (i <= j)
1456 unsigned auid = arr[i].uid;
1457 vinsn_t avinsn = arr[i].new_expr_vinsn;
1459 if (auid == uid
1460 /* When undoing transformation on a bookkeeping copy, the new vinsn
1461 may not be exactly equal to the one that is saved in the vector.
1462 This is because the insn whose copy we're checking was possibly
1463 substituted itself. */
1464 && (! compare_vinsns
1465 || vinsn_equal_p (avinsn, new_vinsn)))
1467 *indp = i;
1468 return true;
1470 else if (auid > uid)
1471 break;
1472 i++;
1475 *indp = i;
1476 return false;
1479 /* Search for a uid of INSN and NEW_VINSN in a sorted vector VECT. Return
1480 the position found or -1, if no such value is in vector.
1481 Search also for UIDs of insn's originators, if ORIGINATORS_P is true. */
1483 find_in_history_vect (VEC(expr_history_def, heap) *vect, rtx insn,
1484 vinsn_t new_vinsn, bool originators_p)
1486 int ind;
1488 if (find_in_history_vect_1 (vect, INSN_UID (insn), new_vinsn,
1489 false, &ind))
1490 return ind;
1492 if (INSN_ORIGINATORS (insn) && originators_p)
1494 unsigned uid;
1495 bitmap_iterator bi;
1497 EXECUTE_IF_SET_IN_BITMAP (INSN_ORIGINATORS (insn), 0, uid, bi)
1498 if (find_in_history_vect_1 (vect, uid, new_vinsn, false, &ind))
1499 return ind;
1502 return -1;
1505 /* Insert new element in a sorted history vector pointed to by PVECT,
1506 if it is not there already. The element is searched using
1507 UID/NEW_EXPR_VINSN pair. TYPE, OLD_EXPR_VINSN and SPEC_DS save
1508 the history of a transformation. */
1509 void
1510 insert_in_history_vect (VEC (expr_history_def, heap) **pvect,
1511 unsigned uid, enum local_trans_type type,
1512 vinsn_t old_expr_vinsn, vinsn_t new_expr_vinsn,
1513 ds_t spec_ds)
1515 VEC(expr_history_def, heap) *vect = *pvect;
1516 expr_history_def temp;
1517 bool res;
1518 int ind;
1520 res = find_in_history_vect_1 (vect, uid, new_expr_vinsn, true, &ind);
1522 if (res)
1524 expr_history_def *phist = VEC_index (expr_history_def, vect, ind);
1526 /* It is possible that speculation types of expressions that were
1527 propagated through different paths will be different here. In this
1528 case, merge the status to get the correct check later. */
1529 if (phist->spec_ds != spec_ds)
1530 phist->spec_ds = ds_max_merge (phist->spec_ds, spec_ds);
1531 return;
1534 temp.uid = uid;
1535 temp.old_expr_vinsn = old_expr_vinsn;
1536 temp.new_expr_vinsn = new_expr_vinsn;
1537 temp.spec_ds = spec_ds;
1538 temp.type = type;
1540 vinsn_attach (old_expr_vinsn);
1541 vinsn_attach (new_expr_vinsn);
1542 VEC_safe_insert (expr_history_def, heap, vect, ind, &temp);
1543 *pvect = vect;
1546 /* Free history vector PVECT. */
1547 static void
1548 free_history_vect (VEC (expr_history_def, heap) **pvect)
1550 unsigned i;
1551 expr_history_def *phist;
1553 if (! *pvect)
1554 return;
1556 for (i = 0;
1557 VEC_iterate (expr_history_def, *pvect, i, phist);
1558 i++)
1560 vinsn_detach (phist->old_expr_vinsn);
1561 vinsn_detach (phist->new_expr_vinsn);
1564 VEC_free (expr_history_def, heap, *pvect);
1565 *pvect = NULL;
1569 /* Compare two vinsns as rhses if possible and as vinsns otherwise. */
1570 bool
1571 vinsn_equal_p (vinsn_t x, vinsn_t y)
1573 rtx_equal_p_callback_function repcf;
1575 if (x == y)
1576 return true;
1578 if (VINSN_TYPE (x) != VINSN_TYPE (y))
1579 return false;
1581 if (VINSN_HASH (x) != VINSN_HASH (y))
1582 return false;
1584 repcf = targetm.sched.skip_rtx_p ? skip_unspecs_callback : NULL;
1585 if (VINSN_SEPARABLE_P (x))
1587 /* Compare RHSes of VINSNs. */
1588 gcc_assert (VINSN_RHS (x));
1589 gcc_assert (VINSN_RHS (y));
1591 return rtx_equal_p_cb (VINSN_RHS (x), VINSN_RHS (y), repcf);
1594 return rtx_equal_p_cb (VINSN_PATTERN (x), VINSN_PATTERN (y), repcf);
1598 /* Functions for working with expressions. */
1600 /* Initialize EXPR. */
1601 static void
1602 init_expr (expr_t expr, vinsn_t vi, int spec, int use, int priority,
1603 int sched_times, int orig_bb_index, ds_t spec_done_ds,
1604 ds_t spec_to_check_ds, int orig_sched_cycle,
1605 VEC(expr_history_def, heap) *history, signed char target_available,
1606 bool was_substituted, bool was_renamed, bool needs_spec_check_p,
1607 bool cant_move)
1609 vinsn_attach (vi);
1611 EXPR_VINSN (expr) = vi;
1612 EXPR_SPEC (expr) = spec;
1613 EXPR_USEFULNESS (expr) = use;
1614 EXPR_PRIORITY (expr) = priority;
1615 EXPR_PRIORITY_ADJ (expr) = 0;
1616 EXPR_SCHED_TIMES (expr) = sched_times;
1617 EXPR_ORIG_BB_INDEX (expr) = orig_bb_index;
1618 EXPR_ORIG_SCHED_CYCLE (expr) = orig_sched_cycle;
1619 EXPR_SPEC_DONE_DS (expr) = spec_done_ds;
1620 EXPR_SPEC_TO_CHECK_DS (expr) = spec_to_check_ds;
1622 if (history)
1623 EXPR_HISTORY_OF_CHANGES (expr) = history;
1624 else
1625 EXPR_HISTORY_OF_CHANGES (expr) = NULL;
1627 EXPR_TARGET_AVAILABLE (expr) = target_available;
1628 EXPR_WAS_SUBSTITUTED (expr) = was_substituted;
1629 EXPR_WAS_RENAMED (expr) = was_renamed;
1630 EXPR_NEEDS_SPEC_CHECK_P (expr) = needs_spec_check_p;
1631 EXPR_CANT_MOVE (expr) = cant_move;
1634 /* Make a copy of the expr FROM into the expr TO. */
1635 void
1636 copy_expr (expr_t to, expr_t from)
1638 VEC(expr_history_def, heap) *temp = NULL;
1640 if (EXPR_HISTORY_OF_CHANGES (from))
1642 unsigned i;
1643 expr_history_def *phist;
1645 temp = VEC_copy (expr_history_def, heap, EXPR_HISTORY_OF_CHANGES (from));
1646 for (i = 0;
1647 VEC_iterate (expr_history_def, temp, i, phist);
1648 i++)
1650 vinsn_attach (phist->old_expr_vinsn);
1651 vinsn_attach (phist->new_expr_vinsn);
1655 init_expr (to, EXPR_VINSN (from), EXPR_SPEC (from),
1656 EXPR_USEFULNESS (from), EXPR_PRIORITY (from),
1657 EXPR_SCHED_TIMES (from), EXPR_ORIG_BB_INDEX (from),
1658 EXPR_SPEC_DONE_DS (from), EXPR_SPEC_TO_CHECK_DS (from),
1659 EXPR_ORIG_SCHED_CYCLE (from), temp,
1660 EXPR_TARGET_AVAILABLE (from), EXPR_WAS_SUBSTITUTED (from),
1661 EXPR_WAS_RENAMED (from), EXPR_NEEDS_SPEC_CHECK_P (from),
1662 EXPR_CANT_MOVE (from));
1665 /* Same, but the final expr will not ever be in av sets, so don't copy
1666 "uninteresting" data such as bitmap cache. */
1667 void
1668 copy_expr_onside (expr_t to, expr_t from)
1670 init_expr (to, EXPR_VINSN (from), EXPR_SPEC (from), EXPR_USEFULNESS (from),
1671 EXPR_PRIORITY (from), EXPR_SCHED_TIMES (from), 0,
1672 EXPR_SPEC_DONE_DS (from), EXPR_SPEC_TO_CHECK_DS (from), 0, NULL,
1673 EXPR_TARGET_AVAILABLE (from), EXPR_WAS_SUBSTITUTED (from),
1674 EXPR_WAS_RENAMED (from), EXPR_NEEDS_SPEC_CHECK_P (from),
1675 EXPR_CANT_MOVE (from));
1678 /* Prepare the expr of INSN for scheduling. Used when moving insn and when
1679 initializing new insns. */
1680 static void
1681 prepare_insn_expr (insn_t insn, int seqno)
1683 expr_t expr = INSN_EXPR (insn);
1684 ds_t ds;
1686 INSN_SEQNO (insn) = seqno;
1687 EXPR_ORIG_BB_INDEX (expr) = BLOCK_NUM (insn);
1688 EXPR_SPEC (expr) = 0;
1689 EXPR_ORIG_SCHED_CYCLE (expr) = 0;
1690 EXPR_WAS_SUBSTITUTED (expr) = 0;
1691 EXPR_WAS_RENAMED (expr) = 0;
1692 EXPR_TARGET_AVAILABLE (expr) = 1;
1693 INSN_LIVE_VALID_P (insn) = false;
1695 /* ??? If this expression is speculative, make its dependence
1696 as weak as possible. We can filter this expression later
1697 in process_spec_exprs, because we do not distinguish
1698 between the status we got during compute_av_set and the
1699 existing status. To be fixed. */
1700 ds = EXPR_SPEC_DONE_DS (expr);
1701 if (ds)
1702 EXPR_SPEC_DONE_DS (expr) = ds_get_max_dep_weak (ds);
1704 free_history_vect (&EXPR_HISTORY_OF_CHANGES (expr));
1707 /* Update target_available bits when merging exprs TO and FROM. SPLIT_POINT
1708 is non-null when expressions are merged from different successors at
1709 a split point. */
1710 static void
1711 update_target_availability (expr_t to, expr_t from, insn_t split_point)
1713 if (EXPR_TARGET_AVAILABLE (to) < 0
1714 || EXPR_TARGET_AVAILABLE (from) < 0)
1715 EXPR_TARGET_AVAILABLE (to) = -1;
1716 else
1718 /* We try to detect the case when one of the expressions
1719 can only be reached through another one. In this case,
1720 we can do better. */
1721 if (split_point == NULL)
1723 int toind, fromind;
1725 toind = EXPR_ORIG_BB_INDEX (to);
1726 fromind = EXPR_ORIG_BB_INDEX (from);
1728 if (toind && toind == fromind)
1729 /* Do nothing -- everything is done in
1730 merge_with_other_exprs. */
1732 else
1733 EXPR_TARGET_AVAILABLE (to) = -1;
1735 else
1736 EXPR_TARGET_AVAILABLE (to) &= EXPR_TARGET_AVAILABLE (from);
1740 /* Update speculation bits when merging exprs TO and FROM. SPLIT_POINT
1741 is non-null when expressions are merged from different successors at
1742 a split point. */
1743 static void
1744 update_speculative_bits (expr_t to, expr_t from, insn_t split_point)
1746 ds_t old_to_ds, old_from_ds;
1748 old_to_ds = EXPR_SPEC_DONE_DS (to);
1749 old_from_ds = EXPR_SPEC_DONE_DS (from);
1751 EXPR_SPEC_DONE_DS (to) = ds_max_merge (old_to_ds, old_from_ds);
1752 EXPR_SPEC_TO_CHECK_DS (to) |= EXPR_SPEC_TO_CHECK_DS (from);
1753 EXPR_NEEDS_SPEC_CHECK_P (to) |= EXPR_NEEDS_SPEC_CHECK_P (from);
1755 /* When merging e.g. control & data speculative exprs, or a control
1756 speculative with a control&data speculative one, we really have
1757 to change vinsn too. Also, when speculative status is changed,
1758 we also need to record this as a transformation in expr's history. */
1759 if ((old_to_ds & SPECULATIVE) || (old_from_ds & SPECULATIVE))
1761 old_to_ds = ds_get_speculation_types (old_to_ds);
1762 old_from_ds = ds_get_speculation_types (old_from_ds);
1764 if (old_to_ds != old_from_ds)
1766 ds_t record_ds;
1768 /* When both expressions are speculative, we need to change
1769 the vinsn first. */
1770 if ((old_to_ds & SPECULATIVE) && (old_from_ds & SPECULATIVE))
1772 int res;
1774 res = speculate_expr (to, EXPR_SPEC_DONE_DS (to));
1775 gcc_assert (res >= 0);
1778 if (split_point != NULL)
1780 /* Record the change with proper status. */
1781 record_ds = EXPR_SPEC_DONE_DS (to) & SPECULATIVE;
1782 record_ds &= ~(old_to_ds & SPECULATIVE);
1783 record_ds &= ~(old_from_ds & SPECULATIVE);
1785 insert_in_history_vect (&EXPR_HISTORY_OF_CHANGES (to),
1786 INSN_UID (split_point), TRANS_SPECULATION,
1787 EXPR_VINSN (from), EXPR_VINSN (to),
1788 record_ds);
1795 /* Merge bits of FROM expr to TO expr. When SPLIT_POINT is not NULL,
1796 this is done along different paths. */
1797 void
1798 merge_expr_data (expr_t to, expr_t from, insn_t split_point)
1800 int i;
1801 expr_history_def *phist;
1803 /* For now, we just set the spec of resulting expr to be minimum of the specs
1804 of merged exprs. */
1805 if (EXPR_SPEC (to) > EXPR_SPEC (from))
1806 EXPR_SPEC (to) = EXPR_SPEC (from);
1808 if (split_point)
1809 EXPR_USEFULNESS (to) += EXPR_USEFULNESS (from);
1810 else
1811 EXPR_USEFULNESS (to) = MAX (EXPR_USEFULNESS (to),
1812 EXPR_USEFULNESS (from));
1814 if (EXPR_PRIORITY (to) < EXPR_PRIORITY (from))
1815 EXPR_PRIORITY (to) = EXPR_PRIORITY (from);
1817 if (EXPR_SCHED_TIMES (to) > EXPR_SCHED_TIMES (from))
1818 EXPR_SCHED_TIMES (to) = EXPR_SCHED_TIMES (from);
1820 if (EXPR_ORIG_BB_INDEX (to) != EXPR_ORIG_BB_INDEX (from))
1821 EXPR_ORIG_BB_INDEX (to) = 0;
1823 EXPR_ORIG_SCHED_CYCLE (to) = MIN (EXPR_ORIG_SCHED_CYCLE (to),
1824 EXPR_ORIG_SCHED_CYCLE (from));
1826 /* We keep this vector sorted. */
1827 for (i = 0;
1828 VEC_iterate (expr_history_def, EXPR_HISTORY_OF_CHANGES (from),
1829 i, phist);
1830 i++)
1831 insert_in_history_vect (&EXPR_HISTORY_OF_CHANGES (to),
1832 phist->uid, phist->type,
1833 phist->old_expr_vinsn, phist->new_expr_vinsn,
1834 phist->spec_ds);
1836 EXPR_WAS_SUBSTITUTED (to) |= EXPR_WAS_SUBSTITUTED (from);
1837 EXPR_WAS_RENAMED (to) |= EXPR_WAS_RENAMED (from);
1838 EXPR_CANT_MOVE (to) |= EXPR_CANT_MOVE (from);
1840 update_target_availability (to, from, split_point);
1841 update_speculative_bits (to, from, split_point);
1844 /* Merge bits of FROM expr to TO expr. Vinsns in the exprs should be equal
1845 in terms of vinsn_equal_p. SPLIT_POINT is non-null when expressions
1846 are merged from different successors at a split point. */
1847 void
1848 merge_expr (expr_t to, expr_t from, insn_t split_point)
1850 vinsn_t to_vi = EXPR_VINSN (to);
1851 vinsn_t from_vi = EXPR_VINSN (from);
1853 gcc_assert (vinsn_equal_p (to_vi, from_vi));
1855 /* Make sure that speculative pattern is propagated into exprs that
1856 have non-speculative one. This will provide us with consistent
1857 speculative bits and speculative patterns inside expr. */
1858 if (EXPR_SPEC_DONE_DS (to) == 0
1859 && EXPR_SPEC_DONE_DS (from) != 0)
1860 change_vinsn_in_expr (to, EXPR_VINSN (from));
1862 merge_expr_data (to, from, split_point);
1863 gcc_assert (EXPR_USEFULNESS (to) <= REG_BR_PROB_BASE);
1866 /* Clear the information of this EXPR. */
1867 void
1868 clear_expr (expr_t expr)
1871 vinsn_detach (EXPR_VINSN (expr));
1872 EXPR_VINSN (expr) = NULL;
1874 free_history_vect (&EXPR_HISTORY_OF_CHANGES (expr));
1877 /* For a given LV_SET, mark EXPR having unavailable target register. */
1878 static void
1879 set_unavailable_target_for_expr (expr_t expr, regset lv_set)
1881 if (EXPR_SEPARABLE_P (expr))
1883 if (REG_P (EXPR_LHS (expr))
1884 && bitmap_bit_p (lv_set, REGNO (EXPR_LHS (expr))))
1886 /* If it's an insn like r1 = use (r1, ...), and it exists in
1887 different forms in each of the av_sets being merged, we can't say
1888 whether original destination register is available or not.
1889 However, this still works if destination register is not used
1890 in the original expression: if the branch at which LV_SET we're
1891 looking here is not actually 'other branch' in sense that same
1892 expression is available through it (but it can't be determined
1893 at computation stage because of transformations on one of the
1894 branches), it still won't affect the availability.
1895 Liveness of a register somewhere on a code motion path means
1896 it's either read somewhere on a codemotion path, live on
1897 'other' branch, live at the point immediately following
1898 the original operation, or is read by the original operation.
1899 The latter case is filtered out in the condition below.
1900 It still doesn't cover the case when register is defined and used
1901 somewhere within the code motion path, and in this case we could
1902 miss a unifying code motion along both branches using a renamed
1903 register, but it won't affect a code correctness since upon
1904 an actual code motion a bookkeeping code would be generated. */
1905 if (bitmap_bit_p (VINSN_REG_USES (EXPR_VINSN (expr)),
1906 REGNO (EXPR_LHS (expr))))
1907 EXPR_TARGET_AVAILABLE (expr) = -1;
1908 else
1909 EXPR_TARGET_AVAILABLE (expr) = false;
1912 else
1914 unsigned regno;
1915 reg_set_iterator rsi;
1917 EXECUTE_IF_SET_IN_REG_SET (VINSN_REG_SETS (EXPR_VINSN (expr)),
1918 0, regno, rsi)
1919 if (bitmap_bit_p (lv_set, regno))
1921 EXPR_TARGET_AVAILABLE (expr) = false;
1922 break;
1925 EXECUTE_IF_SET_IN_REG_SET (VINSN_REG_CLOBBERS (EXPR_VINSN (expr)),
1926 0, regno, rsi)
1927 if (bitmap_bit_p (lv_set, regno))
1929 EXPR_TARGET_AVAILABLE (expr) = false;
1930 break;
1935 /* Try to make EXPR speculative. Return 1 when EXPR's pattern
1936 or dependence status have changed, 2 when also the target register
1937 became unavailable, 0 if nothing had to be changed. */
1939 speculate_expr (expr_t expr, ds_t ds)
1941 int res;
1942 rtx orig_insn_rtx;
1943 rtx spec_pat;
1944 ds_t target_ds, current_ds;
1946 /* Obtain the status we need to put on EXPR. */
1947 target_ds = (ds & SPECULATIVE);
1948 current_ds = EXPR_SPEC_DONE_DS (expr);
1949 ds = ds_full_merge (current_ds, target_ds, NULL_RTX, NULL_RTX);
1951 orig_insn_rtx = EXPR_INSN_RTX (expr);
1953 res = sched_speculate_insn (orig_insn_rtx, ds, &spec_pat);
1955 switch (res)
1957 case 0:
1958 EXPR_SPEC_DONE_DS (expr) = ds;
1959 return current_ds != ds ? 1 : 0;
1961 case 1:
1963 rtx spec_insn_rtx = create_insn_rtx_from_pattern (spec_pat, NULL_RTX);
1964 vinsn_t spec_vinsn = create_vinsn_from_insn_rtx (spec_insn_rtx, false);
1966 change_vinsn_in_expr (expr, spec_vinsn);
1967 EXPR_SPEC_DONE_DS (expr) = ds;
1968 EXPR_NEEDS_SPEC_CHECK_P (expr) = true;
1970 /* Do not allow clobbering the address register of speculative
1971 insns. */
1972 if (bitmap_bit_p (VINSN_REG_USES (EXPR_VINSN (expr)),
1973 expr_dest_regno (expr)))
1975 EXPR_TARGET_AVAILABLE (expr) = false;
1976 return 2;
1979 return 1;
1982 case -1:
1983 return -1;
1985 default:
1986 gcc_unreachable ();
1987 return -1;
1991 /* Return a destination register, if any, of EXPR. */
1993 expr_dest_reg (expr_t expr)
1995 rtx dest = VINSN_LHS (EXPR_VINSN (expr));
1997 if (dest != NULL_RTX && REG_P (dest))
1998 return dest;
2000 return NULL_RTX;
2003 /* Returns the REGNO of the R's destination. */
2004 unsigned
2005 expr_dest_regno (expr_t expr)
2007 rtx dest = expr_dest_reg (expr);
2009 gcc_assert (dest != NULL_RTX);
2010 return REGNO (dest);
2013 /* For a given LV_SET, mark all expressions in JOIN_SET, but not present in
2014 AV_SET having unavailable target register. */
2015 void
2016 mark_unavailable_targets (av_set_t join_set, av_set_t av_set, regset lv_set)
2018 expr_t expr;
2019 av_set_iterator avi;
2021 FOR_EACH_EXPR (expr, avi, join_set)
2022 if (av_set_lookup (av_set, EXPR_VINSN (expr)) == NULL)
2023 set_unavailable_target_for_expr (expr, lv_set);
2027 /* Av set functions. */
2029 /* Add a new element to av set SETP.
2030 Return the element added. */
2031 static av_set_t
2032 av_set_add_element (av_set_t *setp)
2034 /* Insert at the beginning of the list. */
2035 _list_add (setp);
2036 return *setp;
2039 /* Add EXPR to SETP. */
2040 void
2041 av_set_add (av_set_t *setp, expr_t expr)
2043 av_set_t elem;
2045 gcc_assert (!INSN_NOP_P (EXPR_INSN_RTX (expr)));
2046 elem = av_set_add_element (setp);
2047 copy_expr (_AV_SET_EXPR (elem), expr);
2050 /* Same, but do not copy EXPR. */
2051 static void
2052 av_set_add_nocopy (av_set_t *setp, expr_t expr)
2054 av_set_t elem;
2056 elem = av_set_add_element (setp);
2057 *_AV_SET_EXPR (elem) = *expr;
2060 /* Remove expr pointed to by IP from the av_set. */
2061 void
2062 av_set_iter_remove (av_set_iterator *ip)
2064 clear_expr (_AV_SET_EXPR (*ip->lp));
2065 _list_iter_remove (ip);
2068 /* Search for an expr in SET, such that it's equivalent to SOUGHT_VINSN in the
2069 sense of vinsn_equal_p function. Return NULL if no such expr is
2070 in SET was found. */
2071 expr_t
2072 av_set_lookup (av_set_t set, vinsn_t sought_vinsn)
2074 expr_t expr;
2075 av_set_iterator i;
2077 FOR_EACH_EXPR (expr, i, set)
2078 if (vinsn_equal_p (EXPR_VINSN (expr), sought_vinsn))
2079 return expr;
2080 return NULL;
2083 /* Same, but also remove the EXPR found. */
2084 static expr_t
2085 av_set_lookup_and_remove (av_set_t *setp, vinsn_t sought_vinsn)
2087 expr_t expr;
2088 av_set_iterator i;
2090 FOR_EACH_EXPR_1 (expr, i, setp)
2091 if (vinsn_equal_p (EXPR_VINSN (expr), sought_vinsn))
2093 _list_iter_remove_nofree (&i);
2094 return expr;
2096 return NULL;
2099 /* Search for an expr in SET, such that it's equivalent to EXPR in the
2100 sense of vinsn_equal_p function of their vinsns, but not EXPR itself.
2101 Returns NULL if no such expr is in SET was found. */
2102 static expr_t
2103 av_set_lookup_other_equiv_expr (av_set_t set, expr_t expr)
2105 expr_t cur_expr;
2106 av_set_iterator i;
2108 FOR_EACH_EXPR (cur_expr, i, set)
2110 if (cur_expr == expr)
2111 continue;
2112 if (vinsn_equal_p (EXPR_VINSN (cur_expr), EXPR_VINSN (expr)))
2113 return cur_expr;
2116 return NULL;
2119 /* If other expression is already in AVP, remove one of them. */
2120 expr_t
2121 merge_with_other_exprs (av_set_t *avp, av_set_iterator *ip, expr_t expr)
2123 expr_t expr2;
2125 expr2 = av_set_lookup_other_equiv_expr (*avp, expr);
2126 if (expr2 != NULL)
2128 /* Reset target availability on merge, since taking it only from one
2129 of the exprs would be controversial for different code. */
2130 EXPR_TARGET_AVAILABLE (expr2) = -1;
2131 EXPR_USEFULNESS (expr2) = 0;
2133 merge_expr (expr2, expr, NULL);
2135 /* Fix usefulness as it should be now REG_BR_PROB_BASE. */
2136 EXPR_USEFULNESS (expr2) = REG_BR_PROB_BASE;
2138 av_set_iter_remove (ip);
2139 return expr2;
2142 return expr;
2145 /* Return true if there is an expr that correlates to VI in SET. */
2146 bool
2147 av_set_is_in_p (av_set_t set, vinsn_t vi)
2149 return av_set_lookup (set, vi) != NULL;
2152 /* Return a copy of SET. */
2153 av_set_t
2154 av_set_copy (av_set_t set)
2156 expr_t expr;
2157 av_set_iterator i;
2158 av_set_t res = NULL;
2160 FOR_EACH_EXPR (expr, i, set)
2161 av_set_add (&res, expr);
2163 return res;
2166 /* Join two av sets that do not have common elements by attaching second set
2167 (pointed to by FROMP) to the end of first set (TO_TAILP must point to
2168 _AV_SET_NEXT of first set's last element). */
2169 static void
2170 join_distinct_sets (av_set_t *to_tailp, av_set_t *fromp)
2172 gcc_assert (*to_tailp == NULL);
2173 *to_tailp = *fromp;
2174 *fromp = NULL;
2177 /* Makes set pointed to by TO to be the union of TO and FROM. Clear av_set
2178 pointed to by FROMP afterwards. */
2179 void
2180 av_set_union_and_clear (av_set_t *top, av_set_t *fromp, insn_t insn)
2182 expr_t expr1;
2183 av_set_iterator i;
2185 /* Delete from TOP all exprs, that present in FROMP. */
2186 FOR_EACH_EXPR_1 (expr1, i, top)
2188 expr_t expr2 = av_set_lookup (*fromp, EXPR_VINSN (expr1));
2190 if (expr2)
2192 merge_expr (expr2, expr1, insn);
2193 av_set_iter_remove (&i);
2197 join_distinct_sets (i.lp, fromp);
2200 /* Same as above, but also update availability of target register in
2201 TOP judging by TO_LV_SET and FROM_LV_SET. */
2202 void
2203 av_set_union_and_live (av_set_t *top, av_set_t *fromp, regset to_lv_set,
2204 regset from_lv_set, insn_t insn)
2206 expr_t expr1;
2207 av_set_iterator i;
2208 av_set_t *to_tailp, in_both_set = NULL;
2210 /* Delete from TOP all expres, that present in FROMP. */
2211 FOR_EACH_EXPR_1 (expr1, i, top)
2213 expr_t expr2 = av_set_lookup_and_remove (fromp, EXPR_VINSN (expr1));
2215 if (expr2)
2217 /* It may be that the expressions have different destination
2218 registers, in which case we need to check liveness here. */
2219 if (EXPR_SEPARABLE_P (expr1))
2221 int regno1 = (REG_P (EXPR_LHS (expr1))
2222 ? (int) expr_dest_regno (expr1) : -1);
2223 int regno2 = (REG_P (EXPR_LHS (expr2))
2224 ? (int) expr_dest_regno (expr2) : -1);
2226 /* ??? We don't have a way to check restrictions for
2227 *other* register on the current path, we did it only
2228 for the current target register. Give up. */
2229 if (regno1 != regno2)
2230 EXPR_TARGET_AVAILABLE (expr2) = -1;
2232 else if (EXPR_INSN_RTX (expr1) != EXPR_INSN_RTX (expr2))
2233 EXPR_TARGET_AVAILABLE (expr2) = -1;
2235 merge_expr (expr2, expr1, insn);
2236 av_set_add_nocopy (&in_both_set, expr2);
2237 av_set_iter_remove (&i);
2239 else
2240 /* EXPR1 is present in TOP, but not in FROMP. Check it on
2241 FROM_LV_SET. */
2242 set_unavailable_target_for_expr (expr1, from_lv_set);
2244 to_tailp = i.lp;
2246 /* These expressions are not present in TOP. Check liveness
2247 restrictions on TO_LV_SET. */
2248 FOR_EACH_EXPR (expr1, i, *fromp)
2249 set_unavailable_target_for_expr (expr1, to_lv_set);
2251 join_distinct_sets (i.lp, &in_both_set);
2252 join_distinct_sets (to_tailp, fromp);
2255 /* Clear av_set pointed to by SETP. */
2256 void
2257 av_set_clear (av_set_t *setp)
2259 expr_t expr;
2260 av_set_iterator i;
2262 FOR_EACH_EXPR_1 (expr, i, setp)
2263 av_set_iter_remove (&i);
2265 gcc_assert (*setp == NULL);
2268 /* Leave only one non-speculative element in the SETP. */
2269 void
2270 av_set_leave_one_nonspec (av_set_t *setp)
2272 expr_t expr;
2273 av_set_iterator i;
2274 bool has_one_nonspec = false;
2276 /* Keep all speculative exprs, and leave one non-speculative
2277 (the first one). */
2278 FOR_EACH_EXPR_1 (expr, i, setp)
2280 if (!EXPR_SPEC_DONE_DS (expr))
2282 if (has_one_nonspec)
2283 av_set_iter_remove (&i);
2284 else
2285 has_one_nonspec = true;
2290 /* Return the N'th element of the SET. */
2291 expr_t
2292 av_set_element (av_set_t set, int n)
2294 expr_t expr;
2295 av_set_iterator i;
2297 FOR_EACH_EXPR (expr, i, set)
2298 if (n-- == 0)
2299 return expr;
2301 gcc_unreachable ();
2302 return NULL;
2305 /* Deletes all expressions from AVP that are conditional branches (IFs). */
2306 void
2307 av_set_substract_cond_branches (av_set_t *avp)
2309 av_set_iterator i;
2310 expr_t expr;
2312 FOR_EACH_EXPR_1 (expr, i, avp)
2313 if (vinsn_cond_branch_p (EXPR_VINSN (expr)))
2314 av_set_iter_remove (&i);
2317 /* Multiplies usefulness attribute of each member of av-set *AVP by
2318 value PROB / ALL_PROB. */
2319 void
2320 av_set_split_usefulness (av_set_t av, int prob, int all_prob)
2322 av_set_iterator i;
2323 expr_t expr;
2325 FOR_EACH_EXPR (expr, i, av)
2326 EXPR_USEFULNESS (expr) = (all_prob
2327 ? (EXPR_USEFULNESS (expr) * prob) / all_prob
2328 : 0);
2331 /* Leave in AVP only those expressions, which are present in AV,
2332 and return it. */
2333 void
2334 av_set_intersect (av_set_t *avp, av_set_t av)
2336 av_set_iterator i;
2337 expr_t expr;
2339 FOR_EACH_EXPR_1 (expr, i, avp)
2340 if (av_set_lookup (av, EXPR_VINSN (expr)) == NULL)
2341 av_set_iter_remove (&i);
2346 /* Dependence hooks to initialize insn data. */
2348 /* This is used in hooks callable from dependence analysis when initializing
2349 instruction's data. */
2350 static struct
2352 /* Where the dependence was found (lhs/rhs). */
2353 deps_where_t where;
2355 /* The actual data object to initialize. */
2356 idata_t id;
2358 /* True when the insn should not be made clonable. */
2359 bool force_unique_p;
2361 /* True when insn should be treated as of type USE, i.e. never renamed. */
2362 bool force_use_p;
2363 } deps_init_id_data;
2366 /* Setup ID for INSN. FORCE_UNIQUE_P is true when INSN should not be
2367 clonable. */
2368 static void
2369 setup_id_for_insn (idata_t id, insn_t insn, bool force_unique_p)
2371 int type;
2373 /* Determine whether INSN could be cloned and return appropriate vinsn type.
2374 That clonable insns which can be separated into lhs and rhs have type SET.
2375 Other clonable insns have type USE. */
2376 type = GET_CODE (insn);
2378 /* Only regular insns could be cloned. */
2379 if (type == INSN && !force_unique_p)
2380 type = SET;
2381 else if (type == JUMP_INSN && simplejump_p (insn))
2382 type = PC;
2383 else if (type == DEBUG_INSN)
2384 type = !force_unique_p ? USE : INSN;
2386 IDATA_TYPE (id) = type;
2387 IDATA_REG_SETS (id) = get_clear_regset_from_pool ();
2388 IDATA_REG_USES (id) = get_clear_regset_from_pool ();
2389 IDATA_REG_CLOBBERS (id) = get_clear_regset_from_pool ();
2392 /* Start initializing insn data. */
2393 static void
2394 deps_init_id_start_insn (insn_t insn)
2396 gcc_assert (deps_init_id_data.where == DEPS_IN_NOWHERE);
2398 setup_id_for_insn (deps_init_id_data.id, insn,
2399 deps_init_id_data.force_unique_p);
2400 deps_init_id_data.where = DEPS_IN_INSN;
2403 /* Start initializing lhs data. */
2404 static void
2405 deps_init_id_start_lhs (rtx lhs)
2407 gcc_assert (deps_init_id_data.where == DEPS_IN_INSN);
2408 gcc_assert (IDATA_LHS (deps_init_id_data.id) == NULL);
2410 if (IDATA_TYPE (deps_init_id_data.id) == SET)
2412 IDATA_LHS (deps_init_id_data.id) = lhs;
2413 deps_init_id_data.where = DEPS_IN_LHS;
2417 /* Finish initializing lhs data. */
2418 static void
2419 deps_init_id_finish_lhs (void)
2421 deps_init_id_data.where = DEPS_IN_INSN;
2424 /* Note a set of REGNO. */
2425 static void
2426 deps_init_id_note_reg_set (int regno)
2428 haifa_note_reg_set (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_SETS (deps_init_id_data.id), regno);
2436 #ifdef STACK_REGS
2437 /* Make instructions that set stack registers to be ineligible for
2438 renaming to avoid issues with find_used_regs. */
2439 if (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG))
2440 deps_init_id_data.force_use_p = true;
2441 #endif
2444 /* Note a clobber of REGNO. */
2445 static void
2446 deps_init_id_note_reg_clobber (int regno)
2448 haifa_note_reg_clobber (regno);
2450 if (deps_init_id_data.where == DEPS_IN_RHS)
2451 deps_init_id_data.force_use_p = true;
2453 if (IDATA_TYPE (deps_init_id_data.id) != PC)
2454 SET_REGNO_REG_SET (IDATA_REG_CLOBBERS (deps_init_id_data.id), regno);
2457 /* Note a use of REGNO. */
2458 static void
2459 deps_init_id_note_reg_use (int regno)
2461 haifa_note_reg_use (regno);
2463 if (IDATA_TYPE (deps_init_id_data.id) != PC)
2464 SET_REGNO_REG_SET (IDATA_REG_USES (deps_init_id_data.id), regno);
2467 /* Start initializing rhs data. */
2468 static void
2469 deps_init_id_start_rhs (rtx rhs)
2471 gcc_assert (deps_init_id_data.where == DEPS_IN_INSN);
2473 /* And there was no sel_deps_reset_to_insn (). */
2474 if (IDATA_LHS (deps_init_id_data.id) != NULL)
2476 IDATA_RHS (deps_init_id_data.id) = rhs;
2477 deps_init_id_data.where = DEPS_IN_RHS;
2481 /* Finish initializing rhs data. */
2482 static void
2483 deps_init_id_finish_rhs (void)
2485 gcc_assert (deps_init_id_data.where == DEPS_IN_RHS
2486 || deps_init_id_data.where == DEPS_IN_INSN);
2487 deps_init_id_data.where = DEPS_IN_INSN;
2490 /* Finish initializing insn data. */
2491 static void
2492 deps_init_id_finish_insn (void)
2494 gcc_assert (deps_init_id_data.where == DEPS_IN_INSN);
2496 if (IDATA_TYPE (deps_init_id_data.id) == SET)
2498 rtx lhs = IDATA_LHS (deps_init_id_data.id);
2499 rtx rhs = IDATA_RHS (deps_init_id_data.id);
2501 if (lhs == NULL || rhs == NULL || !lhs_and_rhs_separable_p (lhs, rhs)
2502 || deps_init_id_data.force_use_p)
2504 /* This should be a USE, as we don't want to schedule its RHS
2505 separately. However, we still want to have them recorded
2506 for the purposes of substitution. That's why we don't
2507 simply call downgrade_to_use () here. */
2508 gcc_assert (IDATA_TYPE (deps_init_id_data.id) == SET);
2509 gcc_assert (!lhs == !rhs);
2511 IDATA_TYPE (deps_init_id_data.id) = USE;
2515 deps_init_id_data.where = DEPS_IN_NOWHERE;
2518 /* This is dependence info used for initializing insn's data. */
2519 static struct sched_deps_info_def deps_init_id_sched_deps_info;
2521 /* This initializes most of the static part of the above structure. */
2522 static const struct sched_deps_info_def const_deps_init_id_sched_deps_info =
2524 NULL,
2526 deps_init_id_start_insn,
2527 deps_init_id_finish_insn,
2528 deps_init_id_start_lhs,
2529 deps_init_id_finish_lhs,
2530 deps_init_id_start_rhs,
2531 deps_init_id_finish_rhs,
2532 deps_init_id_note_reg_set,
2533 deps_init_id_note_reg_clobber,
2534 deps_init_id_note_reg_use,
2535 NULL, /* note_mem_dep */
2536 NULL, /* note_dep */
2538 0, /* use_cselib */
2539 0, /* use_deps_list */
2540 0 /* generate_spec_deps */
2543 /* Initialize INSN's lhs and rhs in ID. When FORCE_UNIQUE_P is true,
2544 we don't actually need information about lhs and rhs. */
2545 static void
2546 setup_id_lhs_rhs (idata_t id, insn_t insn, bool force_unique_p)
2548 rtx pat = PATTERN (insn);
2550 if (NONJUMP_INSN_P (insn)
2551 && GET_CODE (pat) == SET
2552 && !force_unique_p)
2554 IDATA_RHS (id) = SET_SRC (pat);
2555 IDATA_LHS (id) = SET_DEST (pat);
2557 else
2558 IDATA_LHS (id) = IDATA_RHS (id) = NULL;
2561 /* Possibly downgrade INSN to USE. */
2562 static void
2563 maybe_downgrade_id_to_use (idata_t id, insn_t insn)
2565 bool must_be_use = false;
2566 unsigned uid = INSN_UID (insn);
2567 df_ref *rec;
2568 rtx lhs = IDATA_LHS (id);
2569 rtx rhs = IDATA_RHS (id);
2571 /* We downgrade only SETs. */
2572 if (IDATA_TYPE (id) != SET)
2573 return;
2575 if (!lhs || !lhs_and_rhs_separable_p (lhs, rhs))
2577 IDATA_TYPE (id) = USE;
2578 return;
2581 for (rec = DF_INSN_UID_DEFS (uid); *rec; rec++)
2583 df_ref def = *rec;
2585 if (DF_REF_INSN (def)
2586 && DF_REF_FLAGS_IS_SET (def, DF_REF_PRE_POST_MODIFY)
2587 && loc_mentioned_in_p (DF_REF_LOC (def), IDATA_RHS (id)))
2589 must_be_use = true;
2590 break;
2593 #ifdef STACK_REGS
2594 /* Make instructions that set stack registers to be ineligible for
2595 renaming to avoid issues with find_used_regs. */
2596 if (IN_RANGE (DF_REF_REGNO (def), FIRST_STACK_REG, LAST_STACK_REG))
2598 must_be_use = true;
2599 break;
2601 #endif
2604 if (must_be_use)
2605 IDATA_TYPE (id) = USE;
2608 /* Setup register sets describing INSN in ID. */
2609 static void
2610 setup_id_reg_sets (idata_t id, insn_t insn)
2612 unsigned uid = INSN_UID (insn);
2613 df_ref *rec;
2614 regset tmp = get_clear_regset_from_pool ();
2616 for (rec = DF_INSN_UID_DEFS (uid); *rec; rec++)
2618 df_ref def = *rec;
2619 unsigned int regno = DF_REF_REGNO (def);
2621 /* Post modifies are treated like clobbers by sched-deps.c. */
2622 if (DF_REF_FLAGS_IS_SET (def, (DF_REF_MUST_CLOBBER
2623 | DF_REF_PRE_POST_MODIFY)))
2624 SET_REGNO_REG_SET (IDATA_REG_CLOBBERS (id), regno);
2625 else if (! DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
2627 SET_REGNO_REG_SET (IDATA_REG_SETS (id), regno);
2629 #ifdef STACK_REGS
2630 /* For stack registers, treat writes to them as writes
2631 to the first one to be consistent with sched-deps.c. */
2632 if (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG))
2633 SET_REGNO_REG_SET (IDATA_REG_SETS (id), FIRST_STACK_REG);
2634 #endif
2636 /* Mark special refs that generate read/write def pair. */
2637 if (DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)
2638 || regno == STACK_POINTER_REGNUM)
2639 bitmap_set_bit (tmp, regno);
2642 for (rec = DF_INSN_UID_USES (uid); *rec; rec++)
2644 df_ref use = *rec;
2645 unsigned int regno = DF_REF_REGNO (use);
2647 /* When these refs are met for the first time, skip them, as
2648 these uses are just counterparts of some defs. */
2649 if (bitmap_bit_p (tmp, regno))
2650 bitmap_clear_bit (tmp, regno);
2651 else if (! DF_REF_FLAGS_IS_SET (use, DF_REF_CALL_STACK_USAGE))
2653 SET_REGNO_REG_SET (IDATA_REG_USES (id), regno);
2655 #ifdef STACK_REGS
2656 /* For stack registers, treat reads from them as reads from
2657 the first one to be consistent with sched-deps.c. */
2658 if (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG))
2659 SET_REGNO_REG_SET (IDATA_REG_USES (id), FIRST_STACK_REG);
2660 #endif
2664 return_regset_to_pool (tmp);
2667 /* Initialize instruction data for INSN in ID using DF's data. */
2668 static void
2669 init_id_from_df (idata_t id, insn_t insn, bool force_unique_p)
2671 gcc_assert (DF_INSN_UID_SAFE_GET (INSN_UID (insn)) != NULL);
2673 setup_id_for_insn (id, insn, force_unique_p);
2674 setup_id_lhs_rhs (id, insn, force_unique_p);
2676 if (INSN_NOP_P (insn))
2677 return;
2679 maybe_downgrade_id_to_use (id, insn);
2680 setup_id_reg_sets (id, insn);
2683 /* Initialize instruction data for INSN in ID. */
2684 static void
2685 deps_init_id (idata_t id, insn_t insn, bool force_unique_p)
2687 struct deps_desc _dc, *dc = &_dc;
2689 deps_init_id_data.where = DEPS_IN_NOWHERE;
2690 deps_init_id_data.id = id;
2691 deps_init_id_data.force_unique_p = force_unique_p;
2692 deps_init_id_data.force_use_p = false;
2694 init_deps (dc, false);
2696 memcpy (&deps_init_id_sched_deps_info,
2697 &const_deps_init_id_sched_deps_info,
2698 sizeof (deps_init_id_sched_deps_info));
2700 if (spec_info != NULL)
2701 deps_init_id_sched_deps_info.generate_spec_deps = 1;
2703 sched_deps_info = &deps_init_id_sched_deps_info;
2705 deps_analyze_insn (dc, insn);
2707 free_deps (dc);
2709 deps_init_id_data.id = NULL;
2714 /* Implement hooks for collecting fundamental insn properties like if insn is
2715 an ASM or is within a SCHED_GROUP. */
2717 /* True when a "one-time init" data for INSN was already inited. */
2718 static bool
2719 first_time_insn_init (insn_t insn)
2721 return INSN_LIVE (insn) == NULL;
2724 /* Hash an entry in a transformed_insns hashtable. */
2725 static hashval_t
2726 hash_transformed_insns (const void *p)
2728 return VINSN_HASH_RTX (((const struct transformed_insns *) p)->vinsn_old);
2731 /* Compare the entries in a transformed_insns hashtable. */
2732 static int
2733 eq_transformed_insns (const void *p, const void *q)
2735 rtx i1 = VINSN_INSN_RTX (((const struct transformed_insns *) p)->vinsn_old);
2736 rtx i2 = VINSN_INSN_RTX (((const struct transformed_insns *) q)->vinsn_old);
2738 if (INSN_UID (i1) == INSN_UID (i2))
2739 return 1;
2740 return rtx_equal_p (PATTERN (i1), PATTERN (i2));
2743 /* Free an entry in a transformed_insns hashtable. */
2744 static void
2745 free_transformed_insns (void *p)
2747 struct transformed_insns *pti = (struct transformed_insns *) p;
2749 vinsn_detach (pti->vinsn_old);
2750 vinsn_detach (pti->vinsn_new);
2751 free (pti);
2754 /* Init the s_i_d data for INSN which should be inited just once, when
2755 we first see the insn. */
2756 static void
2757 init_first_time_insn_data (insn_t insn)
2759 /* This should not be set if this is the first time we init data for
2760 insn. */
2761 gcc_assert (first_time_insn_init (insn));
2763 /* These are needed for nops too. */
2764 INSN_LIVE (insn) = get_regset_from_pool ();
2765 INSN_LIVE_VALID_P (insn) = false;
2767 if (!INSN_NOP_P (insn))
2769 INSN_ANALYZED_DEPS (insn) = BITMAP_ALLOC (NULL);
2770 INSN_FOUND_DEPS (insn) = BITMAP_ALLOC (NULL);
2771 INSN_TRANSFORMED_INSNS (insn)
2772 = htab_create (16, hash_transformed_insns,
2773 eq_transformed_insns, free_transformed_insns);
2774 init_deps (&INSN_DEPS_CONTEXT (insn), true);
2778 /* Free almost all above data for INSN that is scheduled already.
2779 Used for extra-large basic blocks. */
2780 void
2781 free_data_for_scheduled_insn (insn_t insn)
2783 gcc_assert (! first_time_insn_init (insn));
2785 if (! INSN_ANALYZED_DEPS (insn))
2786 return;
2788 BITMAP_FREE (INSN_ANALYZED_DEPS (insn));
2789 BITMAP_FREE (INSN_FOUND_DEPS (insn));
2790 htab_delete (INSN_TRANSFORMED_INSNS (insn));
2792 /* This is allocated only for bookkeeping insns. */
2793 if (INSN_ORIGINATORS (insn))
2794 BITMAP_FREE (INSN_ORIGINATORS (insn));
2795 free_deps (&INSN_DEPS_CONTEXT (insn));
2797 INSN_ANALYZED_DEPS (insn) = NULL;
2799 /* Clear the readonly flag so we would ICE when trying to recalculate
2800 the deps context (as we believe that it should not happen). */
2801 (&INSN_DEPS_CONTEXT (insn))->readonly = 0;
2804 /* Free the same data as above for INSN. */
2805 static void
2806 free_first_time_insn_data (insn_t insn)
2808 gcc_assert (! first_time_insn_init (insn));
2810 free_data_for_scheduled_insn (insn);
2811 return_regset_to_pool (INSN_LIVE (insn));
2812 INSN_LIVE (insn) = NULL;
2813 INSN_LIVE_VALID_P (insn) = false;
2816 /* Initialize region-scope data structures for basic blocks. */
2817 static void
2818 init_global_and_expr_for_bb (basic_block bb)
2820 if (sel_bb_empty_p (bb))
2821 return;
2823 invalidate_av_set (bb);
2826 /* Data for global dependency analysis (to initialize CANT_MOVE and
2827 SCHED_GROUP_P). */
2828 static struct
2830 /* Previous insn. */
2831 insn_t prev_insn;
2832 } init_global_data;
2834 /* Determine if INSN is in the sched_group, is an asm or should not be
2835 cloned. After that initialize its expr. */
2836 static void
2837 init_global_and_expr_for_insn (insn_t insn)
2839 if (LABEL_P (insn))
2840 return;
2842 if (NOTE_INSN_BASIC_BLOCK_P (insn))
2844 init_global_data.prev_insn = NULL_RTX;
2845 return;
2848 gcc_assert (INSN_P (insn));
2850 if (SCHED_GROUP_P (insn))
2851 /* Setup a sched_group. */
2853 insn_t prev_insn = init_global_data.prev_insn;
2855 if (prev_insn)
2856 INSN_SCHED_NEXT (prev_insn) = insn;
2858 init_global_data.prev_insn = insn;
2860 else
2861 init_global_data.prev_insn = NULL_RTX;
2863 if (GET_CODE (PATTERN (insn)) == ASM_INPUT
2864 || asm_noperands (PATTERN (insn)) >= 0)
2865 /* Mark INSN as an asm. */
2866 INSN_ASM_P (insn) = true;
2869 bool force_unique_p;
2870 ds_t spec_done_ds;
2872 /* Certain instructions cannot be cloned, and frame related insns and
2873 the insn adjacent to NOTE_INSN_EPILOGUE_BEG cannot be moved out of
2874 their block. */
2875 if (prologue_epilogue_contains (insn))
2877 if (RTX_FRAME_RELATED_P (insn))
2878 CANT_MOVE (insn) = 1;
2879 else
2881 rtx note;
2882 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2883 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE
2884 && ((enum insn_note) INTVAL (XEXP (note, 0))
2885 == NOTE_INSN_EPILOGUE_BEG))
2887 CANT_MOVE (insn) = 1;
2888 break;
2891 force_unique_p = true;
2893 else
2894 if (CANT_MOVE (insn)
2895 || INSN_ASM_P (insn)
2896 || SCHED_GROUP_P (insn)
2897 /* Exception handling insns are always unique. */
2898 || (cfun->can_throw_non_call_exceptions && can_throw_internal (insn))
2899 /* TRAP_IF though have an INSN code is control_flow_insn_p (). */
2900 || control_flow_insn_p (insn))
2901 force_unique_p = true;
2902 else
2903 force_unique_p = false;
2905 if (targetm.sched.get_insn_spec_ds)
2907 spec_done_ds = targetm.sched.get_insn_spec_ds (insn);
2908 spec_done_ds = ds_get_max_dep_weak (spec_done_ds);
2910 else
2911 spec_done_ds = 0;
2913 /* Initialize INSN's expr. */
2914 init_expr (INSN_EXPR (insn), vinsn_create (insn, force_unique_p), 0,
2915 REG_BR_PROB_BASE, INSN_PRIORITY (insn), 0, BLOCK_NUM (insn),
2916 spec_done_ds, 0, 0, NULL, true, false, false, false,
2917 CANT_MOVE (insn));
2920 init_first_time_insn_data (insn);
2923 /* Scan the region and initialize instruction data for basic blocks BBS. */
2924 void
2925 sel_init_global_and_expr (bb_vec_t bbs)
2927 /* ??? It would be nice to implement push / pop scheme for sched_infos. */
2928 const struct sched_scan_info_def ssi =
2930 NULL, /* extend_bb */
2931 init_global_and_expr_for_bb, /* init_bb */
2932 extend_insn_data, /* extend_insn */
2933 init_global_and_expr_for_insn /* init_insn */
2936 sched_scan (&ssi, bbs, NULL, NULL, NULL);
2939 /* Finalize region-scope data structures for basic blocks. */
2940 static void
2941 finish_global_and_expr_for_bb (basic_block bb)
2943 av_set_clear (&BB_AV_SET (bb));
2944 BB_AV_LEVEL (bb) = 0;
2947 /* Finalize INSN's data. */
2948 static void
2949 finish_global_and_expr_insn (insn_t insn)
2951 if (LABEL_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
2952 return;
2954 gcc_assert (INSN_P (insn));
2956 if (INSN_LUID (insn) > 0)
2958 free_first_time_insn_data (insn);
2959 INSN_WS_LEVEL (insn) = 0;
2960 CANT_MOVE (insn) = 0;
2962 /* We can no longer assert this, as vinsns of this insn could be
2963 easily live in other insn's caches. This should be changed to
2964 a counter-like approach among all vinsns. */
2965 gcc_assert (true || VINSN_COUNT (INSN_VINSN (insn)) == 1);
2966 clear_expr (INSN_EXPR (insn));
2970 /* Finalize per instruction data for the whole region. */
2971 void
2972 sel_finish_global_and_expr (void)
2975 bb_vec_t bbs;
2976 int i;
2978 bbs = VEC_alloc (basic_block, heap, current_nr_blocks);
2980 for (i = 0; i < current_nr_blocks; i++)
2981 VEC_quick_push (basic_block, bbs, BASIC_BLOCK (BB_TO_BLOCK (i)));
2983 /* Clear AV_SETs and INSN_EXPRs. */
2985 const struct sched_scan_info_def ssi =
2987 NULL, /* extend_bb */
2988 finish_global_and_expr_for_bb, /* init_bb */
2989 NULL, /* extend_insn */
2990 finish_global_and_expr_insn /* init_insn */
2993 sched_scan (&ssi, bbs, NULL, NULL, NULL);
2996 VEC_free (basic_block, heap, bbs);
2999 finish_insns ();
3003 /* In the below hooks, we merely calculate whether or not a dependence
3004 exists, and in what part of insn. However, we will need more data
3005 when we'll start caching dependence requests. */
3007 /* Container to hold information for dependency analysis. */
3008 static struct
3010 deps_t dc;
3012 /* A variable to track which part of rtx we are scanning in
3013 sched-deps.c: sched_analyze_insn (). */
3014 deps_where_t where;
3016 /* Current producer. */
3017 insn_t pro;
3019 /* Current consumer. */
3020 vinsn_t con;
3022 /* Is SEL_DEPS_HAS_DEP_P[DEPS_IN_X] is true, then X has a dependence.
3023 X is from { INSN, LHS, RHS }. */
3024 ds_t has_dep_p[DEPS_IN_NOWHERE];
3025 } has_dependence_data;
3027 /* Start analyzing dependencies of INSN. */
3028 static void
3029 has_dependence_start_insn (insn_t insn ATTRIBUTE_UNUSED)
3031 gcc_assert (has_dependence_data.where == DEPS_IN_NOWHERE);
3033 has_dependence_data.where = DEPS_IN_INSN;
3036 /* Finish analyzing dependencies of an insn. */
3037 static void
3038 has_dependence_finish_insn (void)
3040 gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
3042 has_dependence_data.where = DEPS_IN_NOWHERE;
3045 /* Start analyzing dependencies of LHS. */
3046 static void
3047 has_dependence_start_lhs (rtx lhs ATTRIBUTE_UNUSED)
3049 gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
3051 if (VINSN_LHS (has_dependence_data.con) != NULL)
3052 has_dependence_data.where = DEPS_IN_LHS;
3055 /* Finish analyzing dependencies of an lhs. */
3056 static void
3057 has_dependence_finish_lhs (void)
3059 has_dependence_data.where = DEPS_IN_INSN;
3062 /* Start analyzing dependencies of RHS. */
3063 static void
3064 has_dependence_start_rhs (rtx rhs ATTRIBUTE_UNUSED)
3066 gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
3068 if (VINSN_RHS (has_dependence_data.con) != NULL)
3069 has_dependence_data.where = DEPS_IN_RHS;
3072 /* Start analyzing dependencies of an rhs. */
3073 static void
3074 has_dependence_finish_rhs (void)
3076 gcc_assert (has_dependence_data.where == DEPS_IN_RHS
3077 || has_dependence_data.where == DEPS_IN_INSN);
3079 has_dependence_data.where = DEPS_IN_INSN;
3082 /* Note a set of REGNO. */
3083 static void
3084 has_dependence_note_reg_set (int regno)
3086 struct deps_reg *reg_last = &has_dependence_data.dc->reg_last[regno];
3088 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3089 VINSN_INSN_RTX
3090 (has_dependence_data.con)))
3092 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3094 if (reg_last->sets != NULL
3095 || reg_last->clobbers != NULL)
3096 *dsp = (*dsp & ~SPECULATIVE) | DEP_OUTPUT;
3098 if (reg_last->uses)
3099 *dsp = (*dsp & ~SPECULATIVE) | DEP_ANTI;
3103 /* Note a clobber of REGNO. */
3104 static void
3105 has_dependence_note_reg_clobber (int regno)
3107 struct deps_reg *reg_last = &has_dependence_data.dc->reg_last[regno];
3109 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3110 VINSN_INSN_RTX
3111 (has_dependence_data.con)))
3113 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3115 if (reg_last->sets)
3116 *dsp = (*dsp & ~SPECULATIVE) | DEP_OUTPUT;
3118 if (reg_last->uses)
3119 *dsp = (*dsp & ~SPECULATIVE) | DEP_ANTI;
3123 /* Note a use of REGNO. */
3124 static void
3125 has_dependence_note_reg_use (int regno)
3127 struct deps_reg *reg_last = &has_dependence_data.dc->reg_last[regno];
3129 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3130 VINSN_INSN_RTX
3131 (has_dependence_data.con)))
3133 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3135 if (reg_last->sets)
3136 *dsp = (*dsp & ~SPECULATIVE) | DEP_TRUE;
3138 if (reg_last->clobbers)
3139 *dsp = (*dsp & ~SPECULATIVE) | DEP_ANTI;
3141 /* Handle BE_IN_SPEC. */
3142 if (reg_last->uses)
3144 ds_t pro_spec_checked_ds;
3146 pro_spec_checked_ds = INSN_SPEC_CHECKED_DS (has_dependence_data.pro);
3147 pro_spec_checked_ds = ds_get_max_dep_weak (pro_spec_checked_ds);
3149 if (pro_spec_checked_ds != 0)
3150 /* Merge BE_IN_SPEC bits into *DSP. */
3151 *dsp = ds_full_merge (*dsp, pro_spec_checked_ds,
3152 NULL_RTX, NULL_RTX);
3157 /* Note a memory dependence. */
3158 static void
3159 has_dependence_note_mem_dep (rtx mem ATTRIBUTE_UNUSED,
3160 rtx pending_mem ATTRIBUTE_UNUSED,
3161 insn_t pending_insn ATTRIBUTE_UNUSED,
3162 ds_t ds ATTRIBUTE_UNUSED)
3164 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3165 VINSN_INSN_RTX (has_dependence_data.con)))
3167 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3169 *dsp = ds_full_merge (ds, *dsp, pending_mem, mem);
3173 /* Note a dependence. */
3174 static void
3175 has_dependence_note_dep (insn_t pro ATTRIBUTE_UNUSED,
3176 ds_t ds ATTRIBUTE_UNUSED)
3178 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3179 VINSN_INSN_RTX (has_dependence_data.con)))
3181 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3183 *dsp = ds_full_merge (ds, *dsp, NULL_RTX, NULL_RTX);
3187 /* Mark the insn as having a hard dependence that prevents speculation. */
3188 void
3189 sel_mark_hard_insn (rtx insn)
3191 int i;
3193 /* Only work when we're in has_dependence_p mode.
3194 ??? This is a hack, this should actually be a hook. */
3195 if (!has_dependence_data.dc || !has_dependence_data.pro)
3196 return;
3198 gcc_assert (insn == VINSN_INSN_RTX (has_dependence_data.con));
3199 gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
3201 for (i = 0; i < DEPS_IN_NOWHERE; i++)
3202 has_dependence_data.has_dep_p[i] &= ~SPECULATIVE;
3205 /* This structure holds the hooks for the dependency analysis used when
3206 actually processing dependencies in the scheduler. */
3207 static struct sched_deps_info_def has_dependence_sched_deps_info;
3209 /* This initializes most of the fields of the above structure. */
3210 static const struct sched_deps_info_def const_has_dependence_sched_deps_info =
3212 NULL,
3214 has_dependence_start_insn,
3215 has_dependence_finish_insn,
3216 has_dependence_start_lhs,
3217 has_dependence_finish_lhs,
3218 has_dependence_start_rhs,
3219 has_dependence_finish_rhs,
3220 has_dependence_note_reg_set,
3221 has_dependence_note_reg_clobber,
3222 has_dependence_note_reg_use,
3223 has_dependence_note_mem_dep,
3224 has_dependence_note_dep,
3226 0, /* use_cselib */
3227 0, /* use_deps_list */
3228 0 /* generate_spec_deps */
3231 /* Initialize has_dependence_sched_deps_info with extra spec field. */
3232 static void
3233 setup_has_dependence_sched_deps_info (void)
3235 memcpy (&has_dependence_sched_deps_info,
3236 &const_has_dependence_sched_deps_info,
3237 sizeof (has_dependence_sched_deps_info));
3239 if (spec_info != NULL)
3240 has_dependence_sched_deps_info.generate_spec_deps = 1;
3242 sched_deps_info = &has_dependence_sched_deps_info;
3245 /* Remove all dependences found and recorded in has_dependence_data array. */
3246 void
3247 sel_clear_has_dependence (void)
3249 int i;
3251 for (i = 0; i < DEPS_IN_NOWHERE; i++)
3252 has_dependence_data.has_dep_p[i] = 0;
3255 /* Return nonzero if EXPR has is dependent upon PRED. Return the pointer
3256 to the dependence information array in HAS_DEP_PP. */
3257 ds_t
3258 has_dependence_p (expr_t expr, insn_t pred, ds_t **has_dep_pp)
3260 int i;
3261 ds_t ds;
3262 struct deps_desc *dc;
3264 if (INSN_SIMPLEJUMP_P (pred))
3265 /* Unconditional jump is just a transfer of control flow.
3266 Ignore it. */
3267 return false;
3269 dc = &INSN_DEPS_CONTEXT (pred);
3271 /* We init this field lazily. */
3272 if (dc->reg_last == NULL)
3273 init_deps_reg_last (dc);
3275 if (!dc->readonly)
3277 has_dependence_data.pro = NULL;
3278 /* Initialize empty dep context with information about PRED. */
3279 advance_deps_context (dc, pred);
3280 dc->readonly = 1;
3283 has_dependence_data.where = DEPS_IN_NOWHERE;
3284 has_dependence_data.pro = pred;
3285 has_dependence_data.con = EXPR_VINSN (expr);
3286 has_dependence_data.dc = dc;
3288 sel_clear_has_dependence ();
3290 /* Now catch all dependencies that would be generated between PRED and
3291 INSN. */
3292 setup_has_dependence_sched_deps_info ();
3293 deps_analyze_insn (dc, EXPR_INSN_RTX (expr));
3294 has_dependence_data.dc = NULL;
3296 /* When a barrier was found, set DEPS_IN_INSN bits. */
3297 if (dc->last_reg_pending_barrier == TRUE_BARRIER)
3298 has_dependence_data.has_dep_p[DEPS_IN_INSN] = DEP_TRUE;
3299 else if (dc->last_reg_pending_barrier == MOVE_BARRIER)
3300 has_dependence_data.has_dep_p[DEPS_IN_INSN] = DEP_ANTI;
3302 /* Do not allow stores to memory to move through checks. Currently
3303 we don't move this to sched-deps.c as the check doesn't have
3304 obvious places to which this dependence can be attached.
3305 FIMXE: this should go to a hook. */
3306 if (EXPR_LHS (expr)
3307 && MEM_P (EXPR_LHS (expr))
3308 && sel_insn_is_speculation_check (pred))
3309 has_dependence_data.has_dep_p[DEPS_IN_INSN] = DEP_ANTI;
3311 *has_dep_pp = has_dependence_data.has_dep_p;
3312 ds = 0;
3313 for (i = 0; i < DEPS_IN_NOWHERE; i++)
3314 ds = ds_full_merge (ds, has_dependence_data.has_dep_p[i],
3315 NULL_RTX, NULL_RTX);
3317 return ds;
3321 /* Dependence hooks implementation that checks dependence latency constraints
3322 on the insns being scheduled. The entry point for these routines is
3323 tick_check_p predicate. */
3325 static struct
3327 /* An expr we are currently checking. */
3328 expr_t expr;
3330 /* A minimal cycle for its scheduling. */
3331 int cycle;
3333 /* Whether we have seen a true dependence while checking. */
3334 bool seen_true_dep_p;
3335 } tick_check_data;
3337 /* Update minimal scheduling cycle for tick_check_insn given that it depends
3338 on PRO with status DS and weight DW. */
3339 static void
3340 tick_check_dep_with_dw (insn_t pro_insn, ds_t ds, dw_t dw)
3342 expr_t con_expr = tick_check_data.expr;
3343 insn_t con_insn = EXPR_INSN_RTX (con_expr);
3345 if (con_insn != pro_insn)
3347 enum reg_note dt;
3348 int tick;
3350 if (/* PROducer was removed from above due to pipelining. */
3351 !INSN_IN_STREAM_P (pro_insn)
3352 /* Or PROducer was originally on the next iteration regarding the
3353 CONsumer. */
3354 || (INSN_SCHED_TIMES (pro_insn)
3355 - EXPR_SCHED_TIMES (con_expr)) > 1)
3356 /* Don't count this dependence. */
3357 return;
3359 dt = ds_to_dt (ds);
3360 if (dt == REG_DEP_TRUE)
3361 tick_check_data.seen_true_dep_p = true;
3363 gcc_assert (INSN_SCHED_CYCLE (pro_insn) > 0);
3366 dep_def _dep, *dep = &_dep;
3368 init_dep (dep, pro_insn, con_insn, dt);
3370 tick = INSN_SCHED_CYCLE (pro_insn) + dep_cost_1 (dep, dw);
3373 /* When there are several kinds of dependencies between pro and con,
3374 only REG_DEP_TRUE should be taken into account. */
3375 if (tick > tick_check_data.cycle
3376 && (dt == REG_DEP_TRUE || !tick_check_data.seen_true_dep_p))
3377 tick_check_data.cycle = tick;
3381 /* An implementation of note_dep hook. */
3382 static void
3383 tick_check_note_dep (insn_t pro, ds_t ds)
3385 tick_check_dep_with_dw (pro, ds, 0);
3388 /* An implementation of note_mem_dep hook. */
3389 static void
3390 tick_check_note_mem_dep (rtx mem1, rtx mem2, insn_t pro, ds_t ds)
3392 dw_t dw;
3394 dw = (ds_to_dt (ds) == REG_DEP_TRUE
3395 ? estimate_dep_weak (mem1, mem2)
3396 : 0);
3398 tick_check_dep_with_dw (pro, ds, dw);
3401 /* This structure contains hooks for dependence analysis used when determining
3402 whether an insn is ready for scheduling. */
3403 static struct sched_deps_info_def tick_check_sched_deps_info =
3405 NULL,
3407 NULL,
3408 NULL,
3409 NULL,
3410 NULL,
3411 NULL,
3412 NULL,
3413 haifa_note_reg_set,
3414 haifa_note_reg_clobber,
3415 haifa_note_reg_use,
3416 tick_check_note_mem_dep,
3417 tick_check_note_dep,
3419 0, 0, 0
3422 /* Estimate number of cycles from the current cycle of FENCE until EXPR can be
3423 scheduled. Return 0 if all data from producers in DC is ready. */
3425 tick_check_p (expr_t expr, deps_t dc, fence_t fence)
3427 int cycles_left;
3428 /* Initialize variables. */
3429 tick_check_data.expr = expr;
3430 tick_check_data.cycle = 0;
3431 tick_check_data.seen_true_dep_p = false;
3432 sched_deps_info = &tick_check_sched_deps_info;
3434 gcc_assert (!dc->readonly);
3435 dc->readonly = 1;
3436 deps_analyze_insn (dc, EXPR_INSN_RTX (expr));
3437 dc->readonly = 0;
3439 cycles_left = tick_check_data.cycle - FENCE_CYCLE (fence);
3441 return cycles_left >= 0 ? cycles_left : 0;
3445 /* Functions to work with insns. */
3447 /* Returns true if LHS of INSN is the same as DEST of an insn
3448 being moved. */
3449 bool
3450 lhs_of_insn_equals_to_dest_p (insn_t insn, rtx dest)
3452 rtx lhs = INSN_LHS (insn);
3454 if (lhs == NULL || dest == NULL)
3455 return false;
3457 return rtx_equal_p (lhs, dest);
3460 /* Return s_i_d entry of INSN. Callable from debugger. */
3461 sel_insn_data_def
3462 insn_sid (insn_t insn)
3464 return *SID (insn);
3467 /* True when INSN is a speculative check. We can tell this by looking
3468 at the data structures of the selective scheduler, not by examining
3469 the pattern. */
3470 bool
3471 sel_insn_is_speculation_check (rtx insn)
3473 return s_i_d && !! INSN_SPEC_CHECKED_DS (insn);
3476 /* Extracts machine mode MODE and destination location DST_LOC
3477 for given INSN. */
3478 void
3479 get_dest_and_mode (rtx insn, rtx *dst_loc, enum machine_mode *mode)
3481 rtx pat = PATTERN (insn);
3483 gcc_assert (dst_loc);
3484 gcc_assert (GET_CODE (pat) == SET);
3486 *dst_loc = SET_DEST (pat);
3488 gcc_assert (*dst_loc);
3489 gcc_assert (MEM_P (*dst_loc) || REG_P (*dst_loc));
3491 if (mode)
3492 *mode = GET_MODE (*dst_loc);
3495 /* Returns true when moving through JUMP will result in bookkeeping
3496 creation. */
3497 bool
3498 bookkeeping_can_be_created_if_moved_through_p (insn_t jump)
3500 insn_t succ;
3501 succ_iterator si;
3503 FOR_EACH_SUCC (succ, si, jump)
3504 if (sel_num_cfg_preds_gt_1 (succ))
3505 return true;
3507 return false;
3510 /* Return 'true' if INSN is the only one in its basic block. */
3511 static bool
3512 insn_is_the_only_one_in_bb_p (insn_t insn)
3514 return sel_bb_head_p (insn) && sel_bb_end_p (insn);
3517 #ifdef ENABLE_CHECKING
3518 /* Check that the region we're scheduling still has at most one
3519 backedge. */
3520 static void
3521 verify_backedges (void)
3523 if (pipelining_p)
3525 int i, n = 0;
3526 edge e;
3527 edge_iterator ei;
3529 for (i = 0; i < current_nr_blocks; i++)
3530 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (BB_TO_BLOCK (i))->succs)
3531 if (in_current_region_p (e->dest)
3532 && BLOCK_TO_BB (e->dest->index) < i)
3533 n++;
3535 gcc_assert (n <= 1);
3538 #endif
3541 /* Functions to work with control flow. */
3543 /* Recompute BLOCK_TO_BB and BB_FOR_BLOCK for current region so that blocks
3544 are sorted in topological order (it might have been invalidated by
3545 redirecting an edge). */
3546 static void
3547 sel_recompute_toporder (void)
3549 int i, n, rgn;
3550 int *postorder, n_blocks;
3552 postorder = XALLOCAVEC (int, n_basic_blocks);
3553 n_blocks = post_order_compute (postorder, false, false);
3555 rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
3556 for (n = 0, i = n_blocks - 1; i >= 0; i--)
3557 if (CONTAINING_RGN (postorder[i]) == rgn)
3559 BLOCK_TO_BB (postorder[i]) = n;
3560 BB_TO_BLOCK (n) = postorder[i];
3561 n++;
3564 /* Assert that we updated info for all blocks. We may miss some blocks if
3565 this function is called when redirecting an edge made a block
3566 unreachable, but that block is not deleted yet. */
3567 gcc_assert (n == RGN_NR_BLOCKS (rgn));
3570 /* Tidy the possibly empty block BB. */
3571 static bool
3572 maybe_tidy_empty_bb (basic_block bb)
3574 basic_block succ_bb, pred_bb;
3575 edge e;
3576 edge_iterator ei;
3577 bool rescan_p;
3579 /* Keep empty bb only if this block immediately precedes EXIT and
3580 has incoming non-fallthrough edge, or it has no predecessors or
3581 successors. Otherwise remove it. */
3582 if (!sel_bb_empty_p (bb)
3583 || (single_succ_p (bb)
3584 && single_succ (bb) == EXIT_BLOCK_PTR
3585 && (!single_pred_p (bb)
3586 || !(single_pred_edge (bb)->flags & EDGE_FALLTHRU)))
3587 || EDGE_COUNT (bb->preds) == 0
3588 || EDGE_COUNT (bb->succs) == 0)
3589 return false;
3591 /* Do not attempt to redirect complex edges. */
3592 FOR_EACH_EDGE (e, ei, bb->preds)
3593 if (e->flags & EDGE_COMPLEX)
3594 return false;
3596 free_data_sets (bb);
3598 /* Do not delete BB if it has more than one successor.
3599 That can occur when we moving a jump. */
3600 if (!single_succ_p (bb))
3602 gcc_assert (can_merge_blocks_p (bb->prev_bb, bb));
3603 sel_merge_blocks (bb->prev_bb, bb);
3604 return true;
3607 succ_bb = single_succ (bb);
3608 rescan_p = true;
3609 pred_bb = NULL;
3611 /* Redirect all non-fallthru edges to the next bb. */
3612 while (rescan_p)
3614 rescan_p = false;
3616 FOR_EACH_EDGE (e, ei, bb->preds)
3618 pred_bb = e->src;
3620 if (!(e->flags & EDGE_FALLTHRU))
3622 /* We can not invalidate computed topological order by moving
3623 the edge destination block (E->SUCC) along a fallthru edge. */
3624 sel_redirect_edge_and_branch (e, succ_bb);
3625 rescan_p = true;
3626 break;
3628 /* If the edge is fallthru, but PRED_BB ends in a conditional jump
3629 to BB (so there is no non-fallthru edge from PRED_BB to BB), we
3630 still have to adjust it. */
3631 else if (single_succ_p (pred_bb) && any_condjump_p (BB_END (pred_bb)))
3633 /* If possible, try to remove the unneeded conditional jump. */
3634 if (INSN_SCHED_TIMES (BB_END (pred_bb)) == 0
3635 && !IN_CURRENT_FENCE_P (BB_END (pred_bb)))
3637 if (!sel_remove_insn (BB_END (pred_bb), false, false))
3638 tidy_fallthru_edge (e);
3640 else
3641 sel_redirect_edge_and_branch (e, succ_bb);
3642 rescan_p = true;
3643 break;
3648 if (can_merge_blocks_p (bb->prev_bb, bb))
3649 sel_merge_blocks (bb->prev_bb, bb);
3650 else
3652 /* This is a block without fallthru predecessor. Just delete it. */
3653 gcc_assert (pred_bb != NULL);
3655 if (in_current_region_p (pred_bb))
3656 move_bb_info (pred_bb, bb);
3657 remove_empty_bb (bb, true);
3660 return true;
3663 /* Tidy the control flow after we have removed original insn from
3664 XBB. Return true if we have removed some blocks. When FULL_TIDYING
3665 is true, also try to optimize control flow on non-empty blocks. */
3666 bool
3667 tidy_control_flow (basic_block xbb, bool full_tidying)
3669 bool changed = true;
3670 insn_t first, last;
3672 /* First check whether XBB is empty. */
3673 changed = maybe_tidy_empty_bb (xbb);
3674 if (changed || !full_tidying)
3675 return changed;
3677 /* Check if there is a unnecessary jump after insn left. */
3678 if (jump_leads_only_to_bb_p (BB_END (xbb), xbb->next_bb)
3679 && INSN_SCHED_TIMES (BB_END (xbb)) == 0
3680 && !IN_CURRENT_FENCE_P (BB_END (xbb)))
3682 if (sel_remove_insn (BB_END (xbb), false, false))
3683 return true;
3684 tidy_fallthru_edge (EDGE_SUCC (xbb, 0));
3687 first = sel_bb_head (xbb);
3688 last = sel_bb_end (xbb);
3689 if (MAY_HAVE_DEBUG_INSNS)
3691 if (first != last && DEBUG_INSN_P (first))
3693 first = NEXT_INSN (first);
3694 while (first != last && (DEBUG_INSN_P (first) || NOTE_P (first)));
3696 if (first != last && DEBUG_INSN_P (last))
3698 last = PREV_INSN (last);
3699 while (first != last && (DEBUG_INSN_P (last) || NOTE_P (last)));
3701 /* Check if there is an unnecessary jump in previous basic block leading
3702 to next basic block left after removing INSN from stream.
3703 If it is so, remove that jump and redirect edge to current
3704 basic block (where there was INSN before deletion). This way
3705 when NOP will be deleted several instructions later with its
3706 basic block we will not get a jump to next instruction, which
3707 can be harmful. */
3708 if (first == last
3709 && !sel_bb_empty_p (xbb)
3710 && INSN_NOP_P (last)
3711 /* Flow goes fallthru from current block to the next. */
3712 && EDGE_COUNT (xbb->succs) == 1
3713 && (EDGE_SUCC (xbb, 0)->flags & EDGE_FALLTHRU)
3714 /* When successor is an EXIT block, it may not be the next block. */
3715 && single_succ (xbb) != EXIT_BLOCK_PTR
3716 /* And unconditional jump in previous basic block leads to
3717 next basic block of XBB and this jump can be safely removed. */
3718 && in_current_region_p (xbb->prev_bb)
3719 && jump_leads_only_to_bb_p (BB_END (xbb->prev_bb), xbb->next_bb)
3720 && INSN_SCHED_TIMES (BB_END (xbb->prev_bb)) == 0
3721 /* Also this jump is not at the scheduling boundary. */
3722 && !IN_CURRENT_FENCE_P (BB_END (xbb->prev_bb)))
3724 bool recompute_toporder_p;
3725 /* Clear data structures of jump - jump itself will be removed
3726 by sel_redirect_edge_and_branch. */
3727 clear_expr (INSN_EXPR (BB_END (xbb->prev_bb)));
3728 recompute_toporder_p
3729 = sel_redirect_edge_and_branch (EDGE_SUCC (xbb->prev_bb, 0), xbb);
3731 gcc_assert (EDGE_SUCC (xbb->prev_bb, 0)->flags & EDGE_FALLTHRU);
3733 /* It can turn out that after removing unused jump, basic block
3734 that contained that jump, becomes empty too. In such case
3735 remove it too. */
3736 if (sel_bb_empty_p (xbb->prev_bb))
3737 changed = maybe_tidy_empty_bb (xbb->prev_bb);
3738 if (recompute_toporder_p)
3739 sel_recompute_toporder ();
3742 #ifdef ENABLE_CHECKING
3743 verify_backedges ();
3744 #endif
3746 return changed;
3749 /* Purge meaningless empty blocks in the middle of a region. */
3750 void
3751 purge_empty_blocks (void)
3753 /* Do not attempt to delete preheader. */
3754 int i = sel_is_loop_preheader_p (BASIC_BLOCK (BB_TO_BLOCK (0))) ? 1 : 0;
3756 while (i < current_nr_blocks)
3758 basic_block b = BASIC_BLOCK (BB_TO_BLOCK (i));
3760 if (maybe_tidy_empty_bb (b))
3761 continue;
3763 i++;
3767 /* Rip-off INSN from the insn stream. When ONLY_DISCONNECT is true,
3768 do not delete insn's data, because it will be later re-emitted.
3769 Return true if we have removed some blocks afterwards. */
3770 bool
3771 sel_remove_insn (insn_t insn, bool only_disconnect, bool full_tidying)
3773 basic_block bb = BLOCK_FOR_INSN (insn);
3775 gcc_assert (INSN_IN_STREAM_P (insn));
3777 if (DEBUG_INSN_P (insn) && BB_AV_SET_VALID_P (bb))
3779 expr_t expr;
3780 av_set_iterator i;
3782 /* When we remove a debug insn that is head of a BB, it remains
3783 in the AV_SET of the block, but it shouldn't. */
3784 FOR_EACH_EXPR_1 (expr, i, &BB_AV_SET (bb))
3785 if (EXPR_INSN_RTX (expr) == insn)
3787 av_set_iter_remove (&i);
3788 break;
3792 if (only_disconnect)
3794 insn_t prev = PREV_INSN (insn);
3795 insn_t next = NEXT_INSN (insn);
3796 basic_block bb = BLOCK_FOR_INSN (insn);
3798 NEXT_INSN (prev) = next;
3799 PREV_INSN (next) = prev;
3801 if (BB_HEAD (bb) == insn)
3803 gcc_assert (BLOCK_FOR_INSN (prev) == bb);
3804 BB_HEAD (bb) = prev;
3806 if (BB_END (bb) == insn)
3807 BB_END (bb) = prev;
3809 else
3811 remove_insn (insn);
3812 clear_expr (INSN_EXPR (insn));
3815 /* It is necessary to null this fields before calling add_insn (). */
3816 PREV_INSN (insn) = NULL_RTX;
3817 NEXT_INSN (insn) = NULL_RTX;
3819 return tidy_control_flow (bb, full_tidying);
3822 /* Estimate number of the insns in BB. */
3823 static int
3824 sel_estimate_number_of_insns (basic_block bb)
3826 int res = 0;
3827 insn_t insn = NEXT_INSN (BB_HEAD (bb)), next_tail = NEXT_INSN (BB_END (bb));
3829 for (; insn != next_tail; insn = NEXT_INSN (insn))
3830 if (NONDEBUG_INSN_P (insn))
3831 res++;
3833 return res;
3836 /* We don't need separate luids for notes or labels. */
3837 static int
3838 sel_luid_for_non_insn (rtx x)
3840 gcc_assert (NOTE_P (x) || LABEL_P (x));
3842 return -1;
3845 /* Return seqno of the only predecessor of INSN. */
3846 static int
3847 get_seqno_of_a_pred (insn_t insn)
3849 int seqno;
3851 gcc_assert (INSN_SIMPLEJUMP_P (insn));
3853 if (!sel_bb_head_p (insn))
3854 seqno = INSN_SEQNO (PREV_INSN (insn));
3855 else
3857 basic_block bb = BLOCK_FOR_INSN (insn);
3859 if (single_pred_p (bb)
3860 && !in_current_region_p (single_pred (bb)))
3862 /* We can have preds outside a region when splitting edges
3863 for pipelining of an outer loop. Use succ instead.
3864 There should be only one of them. */
3865 insn_t succ = NULL;
3866 succ_iterator si;
3867 bool first = true;
3869 gcc_assert (flag_sel_sched_pipelining_outer_loops
3870 && current_loop_nest);
3871 FOR_EACH_SUCC_1 (succ, si, insn,
3872 SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
3874 gcc_assert (first);
3875 first = false;
3878 gcc_assert (succ != NULL);
3879 seqno = INSN_SEQNO (succ);
3881 else
3883 insn_t *preds;
3884 int n;
3886 cfg_preds (BLOCK_FOR_INSN (insn), &preds, &n);
3887 gcc_assert (n == 1);
3889 seqno = INSN_SEQNO (preds[0]);
3891 free (preds);
3895 return seqno;
3898 /* Find the proper seqno for inserting at INSN. Returns -1 if no predecessors
3899 with positive seqno exist. */
3901 get_seqno_by_preds (rtx insn)
3903 basic_block bb = BLOCK_FOR_INSN (insn);
3904 rtx tmp = insn, head = BB_HEAD (bb);
3905 insn_t *preds;
3906 int n, i, seqno;
3908 while (tmp != head)
3909 if (INSN_P (tmp))
3910 return INSN_SEQNO (tmp);
3911 else
3912 tmp = PREV_INSN (tmp);
3914 cfg_preds (bb, &preds, &n);
3915 for (i = 0, seqno = -1; i < n; i++)
3916 seqno = MAX (seqno, INSN_SEQNO (preds[i]));
3918 return seqno;
3923 /* Extend pass-scope data structures for basic blocks. */
3924 void
3925 sel_extend_global_bb_info (void)
3927 VEC_safe_grow_cleared (sel_global_bb_info_def, heap, sel_global_bb_info,
3928 last_basic_block);
3931 /* Extend region-scope data structures for basic blocks. */
3932 static void
3933 extend_region_bb_info (void)
3935 VEC_safe_grow_cleared (sel_region_bb_info_def, heap, sel_region_bb_info,
3936 last_basic_block);
3939 /* Extend all data structures to fit for all basic blocks. */
3940 static void
3941 extend_bb_info (void)
3943 sel_extend_global_bb_info ();
3944 extend_region_bb_info ();
3947 /* Finalize pass-scope data structures for basic blocks. */
3948 void
3949 sel_finish_global_bb_info (void)
3951 VEC_free (sel_global_bb_info_def, heap, sel_global_bb_info);
3954 /* Finalize region-scope data structures for basic blocks. */
3955 static void
3956 finish_region_bb_info (void)
3958 VEC_free (sel_region_bb_info_def, heap, sel_region_bb_info);
3962 /* Data for each insn in current region. */
3963 VEC (sel_insn_data_def, heap) *s_i_d = NULL;
3965 /* A vector for the insns we've emitted. */
3966 static insn_vec_t new_insns = NULL;
3968 /* Extend data structures for insns from current region. */
3969 static void
3970 extend_insn_data (void)
3972 int reserve;
3974 sched_extend_target ();
3975 sched_deps_init (false);
3977 /* Extend data structures for insns from current region. */
3978 reserve = (sched_max_luid + 1
3979 - VEC_length (sel_insn_data_def, s_i_d));
3980 if (reserve > 0
3981 && ! VEC_space (sel_insn_data_def, s_i_d, reserve))
3983 int size;
3985 if (sched_max_luid / 2 > 1024)
3986 size = sched_max_luid + 1024;
3987 else
3988 size = 3 * sched_max_luid / 2;
3991 VEC_safe_grow_cleared (sel_insn_data_def, heap, s_i_d, size);
3995 /* Finalize data structures for insns from current region. */
3996 static void
3997 finish_insns (void)
3999 unsigned i;
4001 /* Clear here all dependence contexts that may have left from insns that were
4002 removed during the scheduling. */
4003 for (i = 0; i < VEC_length (sel_insn_data_def, s_i_d); i++)
4005 sel_insn_data_def *sid_entry = VEC_index (sel_insn_data_def, s_i_d, i);
4007 if (sid_entry->live)
4008 return_regset_to_pool (sid_entry->live);
4009 if (sid_entry->analyzed_deps)
4011 BITMAP_FREE (sid_entry->analyzed_deps);
4012 BITMAP_FREE (sid_entry->found_deps);
4013 htab_delete (sid_entry->transformed_insns);
4014 free_deps (&sid_entry->deps_context);
4016 if (EXPR_VINSN (&sid_entry->expr))
4018 clear_expr (&sid_entry->expr);
4020 /* Also, clear CANT_MOVE bit here, because we really don't want it
4021 to be passed to the next region. */
4022 CANT_MOVE_BY_LUID (i) = 0;
4026 VEC_free (sel_insn_data_def, heap, s_i_d);
4029 /* A proxy to pass initialization data to init_insn (). */
4030 static sel_insn_data_def _insn_init_ssid;
4031 static sel_insn_data_t insn_init_ssid = &_insn_init_ssid;
4033 /* If true create a new vinsn. Otherwise use the one from EXPR. */
4034 static bool insn_init_create_new_vinsn_p;
4036 /* Set all necessary data for initialization of the new insn[s]. */
4037 static expr_t
4038 set_insn_init (expr_t expr, vinsn_t vi, int seqno)
4040 expr_t x = &insn_init_ssid->expr;
4042 copy_expr_onside (x, expr);
4043 if (vi != NULL)
4045 insn_init_create_new_vinsn_p = false;
4046 change_vinsn_in_expr (x, vi);
4048 else
4049 insn_init_create_new_vinsn_p = true;
4051 insn_init_ssid->seqno = seqno;
4052 return x;
4055 /* Init data for INSN. */
4056 static void
4057 init_insn_data (insn_t insn)
4059 expr_t expr;
4060 sel_insn_data_t ssid = insn_init_ssid;
4062 /* The fields mentioned below are special and hence are not being
4063 propagated to the new insns. */
4064 gcc_assert (!ssid->asm_p && ssid->sched_next == NULL
4065 && !ssid->after_stall_p && ssid->sched_cycle == 0);
4066 gcc_assert (INSN_P (insn) && INSN_LUID (insn) > 0);
4068 expr = INSN_EXPR (insn);
4069 copy_expr (expr, &ssid->expr);
4070 prepare_insn_expr (insn, ssid->seqno);
4072 if (insn_init_create_new_vinsn_p)
4073 change_vinsn_in_expr (expr, vinsn_create (insn, init_insn_force_unique_p));
4075 if (first_time_insn_init (insn))
4076 init_first_time_insn_data (insn);
4079 /* This is used to initialize spurious jumps generated by
4080 sel_redirect_edge (). */
4081 static void
4082 init_simplejump_data (insn_t insn)
4084 init_expr (INSN_EXPR (insn), vinsn_create (insn, false), 0,
4085 REG_BR_PROB_BASE, 0, 0, 0, 0, 0, 0, NULL, true, false, false,
4086 false, true);
4087 INSN_SEQNO (insn) = get_seqno_of_a_pred (insn);
4088 init_first_time_insn_data (insn);
4091 /* Perform deferred initialization of insns. This is used to process
4092 a new jump that may be created by redirect_edge. */
4093 void
4094 sel_init_new_insn (insn_t insn, int flags)
4096 /* We create data structures for bb when the first insn is emitted in it. */
4097 if (INSN_P (insn)
4098 && INSN_IN_STREAM_P (insn)
4099 && insn_is_the_only_one_in_bb_p (insn))
4101 extend_bb_info ();
4102 create_initial_data_sets (BLOCK_FOR_INSN (insn));
4105 if (flags & INSN_INIT_TODO_LUID)
4106 sched_init_luids (NULL, NULL, NULL, insn);
4108 if (flags & INSN_INIT_TODO_SSID)
4110 extend_insn_data ();
4111 init_insn_data (insn);
4112 clear_expr (&insn_init_ssid->expr);
4115 if (flags & INSN_INIT_TODO_SIMPLEJUMP)
4117 extend_insn_data ();
4118 init_simplejump_data (insn);
4121 gcc_assert (CONTAINING_RGN (BLOCK_NUM (insn))
4122 == CONTAINING_RGN (BB_TO_BLOCK (0)));
4126 /* Functions to init/finish work with lv sets. */
4128 /* Init BB_LV_SET of BB from DF_LR_IN set of BB. */
4129 static void
4130 init_lv_set (basic_block bb)
4132 gcc_assert (!BB_LV_SET_VALID_P (bb));
4134 BB_LV_SET (bb) = get_regset_from_pool ();
4135 COPY_REG_SET (BB_LV_SET (bb), DF_LR_IN (bb));
4136 BB_LV_SET_VALID_P (bb) = true;
4139 /* Copy liveness information to BB from FROM_BB. */
4140 static void
4141 copy_lv_set_from (basic_block bb, basic_block from_bb)
4143 gcc_assert (!BB_LV_SET_VALID_P (bb));
4145 COPY_REG_SET (BB_LV_SET (bb), BB_LV_SET (from_bb));
4146 BB_LV_SET_VALID_P (bb) = true;
4149 /* Initialize lv set of all bb headers. */
4150 void
4151 init_lv_sets (void)
4153 basic_block bb;
4155 /* Initialize of LV sets. */
4156 FOR_EACH_BB (bb)
4157 init_lv_set (bb);
4159 /* Don't forget EXIT_BLOCK. */
4160 init_lv_set (EXIT_BLOCK_PTR);
4163 /* Release lv set of HEAD. */
4164 static void
4165 free_lv_set (basic_block bb)
4167 gcc_assert (BB_LV_SET (bb) != NULL);
4169 return_regset_to_pool (BB_LV_SET (bb));
4170 BB_LV_SET (bb) = NULL;
4171 BB_LV_SET_VALID_P (bb) = false;
4174 /* Finalize lv sets of all bb headers. */
4175 void
4176 free_lv_sets (void)
4178 basic_block bb;
4180 /* Don't forget EXIT_BLOCK. */
4181 free_lv_set (EXIT_BLOCK_PTR);
4183 /* Free LV sets. */
4184 FOR_EACH_BB (bb)
4185 if (BB_LV_SET (bb))
4186 free_lv_set (bb);
4189 /* Initialize an invalid AV_SET for BB.
4190 This set will be updated next time compute_av () process BB. */
4191 static void
4192 invalidate_av_set (basic_block bb)
4194 gcc_assert (BB_AV_LEVEL (bb) <= 0
4195 && BB_AV_SET (bb) == NULL);
4197 BB_AV_LEVEL (bb) = -1;
4200 /* Create initial data sets for BB (they will be invalid). */
4201 static void
4202 create_initial_data_sets (basic_block bb)
4204 if (BB_LV_SET (bb))
4205 BB_LV_SET_VALID_P (bb) = false;
4206 else
4207 BB_LV_SET (bb) = get_regset_from_pool ();
4208 invalidate_av_set (bb);
4211 /* Free av set of BB. */
4212 static void
4213 free_av_set (basic_block bb)
4215 av_set_clear (&BB_AV_SET (bb));
4216 BB_AV_LEVEL (bb) = 0;
4219 /* Free data sets of BB. */
4220 void
4221 free_data_sets (basic_block bb)
4223 free_lv_set (bb);
4224 free_av_set (bb);
4227 /* Exchange lv sets of TO and FROM. */
4228 static void
4229 exchange_lv_sets (basic_block to, basic_block from)
4232 regset to_lv_set = BB_LV_SET (to);
4234 BB_LV_SET (to) = BB_LV_SET (from);
4235 BB_LV_SET (from) = to_lv_set;
4239 bool to_lv_set_valid_p = BB_LV_SET_VALID_P (to);
4241 BB_LV_SET_VALID_P (to) = BB_LV_SET_VALID_P (from);
4242 BB_LV_SET_VALID_P (from) = to_lv_set_valid_p;
4247 /* Exchange av sets of TO and FROM. */
4248 static void
4249 exchange_av_sets (basic_block to, basic_block from)
4252 av_set_t to_av_set = BB_AV_SET (to);
4254 BB_AV_SET (to) = BB_AV_SET (from);
4255 BB_AV_SET (from) = to_av_set;
4259 int to_av_level = BB_AV_LEVEL (to);
4261 BB_AV_LEVEL (to) = BB_AV_LEVEL (from);
4262 BB_AV_LEVEL (from) = to_av_level;
4266 /* Exchange data sets of TO and FROM. */
4267 void
4268 exchange_data_sets (basic_block to, basic_block from)
4270 exchange_lv_sets (to, from);
4271 exchange_av_sets (to, from);
4274 /* Copy data sets of FROM to TO. */
4275 void
4276 copy_data_sets (basic_block to, basic_block from)
4278 gcc_assert (!BB_LV_SET_VALID_P (to) && !BB_AV_SET_VALID_P (to));
4279 gcc_assert (BB_AV_SET (to) == NULL);
4281 BB_AV_LEVEL (to) = BB_AV_LEVEL (from);
4282 BB_LV_SET_VALID_P (to) = BB_LV_SET_VALID_P (from);
4284 if (BB_AV_SET_VALID_P (from))
4286 BB_AV_SET (to) = av_set_copy (BB_AV_SET (from));
4288 if (BB_LV_SET_VALID_P (from))
4290 gcc_assert (BB_LV_SET (to) != NULL);
4291 COPY_REG_SET (BB_LV_SET (to), BB_LV_SET (from));
4295 /* Return an av set for INSN, if any. */
4296 av_set_t
4297 get_av_set (insn_t insn)
4299 av_set_t av_set;
4301 gcc_assert (AV_SET_VALID_P (insn));
4303 if (sel_bb_head_p (insn))
4304 av_set = BB_AV_SET (BLOCK_FOR_INSN (insn));
4305 else
4306 av_set = NULL;
4308 return av_set;
4311 /* Implementation of AV_LEVEL () macro. Return AV_LEVEL () of INSN. */
4313 get_av_level (insn_t insn)
4315 int av_level;
4317 gcc_assert (INSN_P (insn));
4319 if (sel_bb_head_p (insn))
4320 av_level = BB_AV_LEVEL (BLOCK_FOR_INSN (insn));
4321 else
4322 av_level = INSN_WS_LEVEL (insn);
4324 return av_level;
4329 /* Variables to work with control-flow graph. */
4331 /* The basic block that already has been processed by the sched_data_update (),
4332 but hasn't been in sel_add_bb () yet. */
4333 static VEC (basic_block, heap) *last_added_blocks = NULL;
4335 /* A pool for allocating successor infos. */
4336 static struct
4338 /* A stack for saving succs_info structures. */
4339 struct succs_info *stack;
4341 /* Its size. */
4342 int size;
4344 /* Top of the stack. */
4345 int top;
4347 /* Maximal value of the top. */
4348 int max_top;
4349 } succs_info_pool;
4351 /* Functions to work with control-flow graph. */
4353 /* Return basic block note of BB. */
4354 insn_t
4355 sel_bb_head (basic_block bb)
4357 insn_t head;
4359 if (bb == EXIT_BLOCK_PTR)
4361 gcc_assert (exit_insn != NULL_RTX);
4362 head = exit_insn;
4364 else
4366 insn_t note;
4368 note = bb_note (bb);
4369 head = next_nonnote_insn (note);
4371 if (head && (BARRIER_P (head) || BLOCK_FOR_INSN (head) != bb))
4372 head = NULL_RTX;
4375 return head;
4378 /* Return true if INSN is a basic block header. */
4379 bool
4380 sel_bb_head_p (insn_t insn)
4382 return sel_bb_head (BLOCK_FOR_INSN (insn)) == insn;
4385 /* Return last insn of BB. */
4386 insn_t
4387 sel_bb_end (basic_block bb)
4389 if (sel_bb_empty_p (bb))
4390 return NULL_RTX;
4392 gcc_assert (bb != EXIT_BLOCK_PTR);
4394 return BB_END (bb);
4397 /* Return true if INSN is the last insn in its basic block. */
4398 bool
4399 sel_bb_end_p (insn_t insn)
4401 return insn == sel_bb_end (BLOCK_FOR_INSN (insn));
4404 /* Return true if BB consist of single NOTE_INSN_BASIC_BLOCK. */
4405 bool
4406 sel_bb_empty_p (basic_block bb)
4408 return sel_bb_head (bb) == NULL;
4411 /* True when BB belongs to the current scheduling region. */
4412 bool
4413 in_current_region_p (basic_block bb)
4415 if (bb->index < NUM_FIXED_BLOCKS)
4416 return false;
4418 return CONTAINING_RGN (bb->index) == CONTAINING_RGN (BB_TO_BLOCK (0));
4421 /* Return the block which is a fallthru bb of a conditional jump JUMP. */
4422 basic_block
4423 fallthru_bb_of_jump (rtx jump)
4425 if (!JUMP_P (jump))
4426 return NULL;
4428 if (any_uncondjump_p (jump))
4429 return single_succ (BLOCK_FOR_INSN (jump));
4431 if (!any_condjump_p (jump))
4432 return NULL;
4434 /* A basic block that ends with a conditional jump may still have one successor
4435 (and be followed by a barrier), we are not interested. */
4436 if (single_succ_p (BLOCK_FOR_INSN (jump)))
4437 return NULL;
4439 return FALLTHRU_EDGE (BLOCK_FOR_INSN (jump))->dest;
4442 /* Remove all notes from BB. */
4443 static void
4444 init_bb (basic_block bb)
4446 remove_notes (bb_note (bb), BB_END (bb));
4447 BB_NOTE_LIST (bb) = note_list;
4450 void
4451 sel_init_bbs (bb_vec_t bbs, basic_block bb)
4453 const struct sched_scan_info_def ssi =
4455 extend_bb_info, /* extend_bb */
4456 init_bb, /* init_bb */
4457 NULL, /* extend_insn */
4458 NULL /* init_insn */
4461 sched_scan (&ssi, bbs, bb, new_insns, NULL);
4464 /* Restore notes for the whole region. */
4465 static void
4466 sel_restore_notes (void)
4468 int bb;
4469 insn_t insn;
4471 for (bb = 0; bb < current_nr_blocks; bb++)
4473 basic_block first, last;
4475 first = EBB_FIRST_BB (bb);
4476 last = EBB_LAST_BB (bb)->next_bb;
4480 note_list = BB_NOTE_LIST (first);
4481 restore_other_notes (NULL, first);
4482 BB_NOTE_LIST (first) = NULL_RTX;
4484 FOR_BB_INSNS (first, insn)
4485 if (NONDEBUG_INSN_P (insn))
4486 reemit_notes (insn);
4488 first = first->next_bb;
4490 while (first != last);
4494 /* Free per-bb data structures. */
4495 void
4496 sel_finish_bbs (void)
4498 sel_restore_notes ();
4500 /* Remove current loop preheader from this loop. */
4501 if (current_loop_nest)
4502 sel_remove_loop_preheader ();
4504 finish_region_bb_info ();
4507 /* Return true if INSN has a single successor of type FLAGS. */
4508 bool
4509 sel_insn_has_single_succ_p (insn_t insn, int flags)
4511 insn_t succ;
4512 succ_iterator si;
4513 bool first_p = true;
4515 FOR_EACH_SUCC_1 (succ, si, insn, flags)
4517 if (first_p)
4518 first_p = false;
4519 else
4520 return false;
4523 return true;
4526 /* Allocate successor's info. */
4527 static struct succs_info *
4528 alloc_succs_info (void)
4530 if (succs_info_pool.top == succs_info_pool.max_top)
4532 int i;
4534 if (++succs_info_pool.max_top >= succs_info_pool.size)
4535 gcc_unreachable ();
4537 i = ++succs_info_pool.top;
4538 succs_info_pool.stack[i].succs_ok = VEC_alloc (rtx, heap, 10);
4539 succs_info_pool.stack[i].succs_other = VEC_alloc (rtx, heap, 10);
4540 succs_info_pool.stack[i].probs_ok = VEC_alloc (int, heap, 10);
4542 else
4543 succs_info_pool.top++;
4545 return &succs_info_pool.stack[succs_info_pool.top];
4548 /* Free successor's info. */
4549 void
4550 free_succs_info (struct succs_info * sinfo)
4552 gcc_assert (succs_info_pool.top >= 0
4553 && &succs_info_pool.stack[succs_info_pool.top] == sinfo);
4554 succs_info_pool.top--;
4556 /* Clear stale info. */
4557 VEC_block_remove (rtx, sinfo->succs_ok,
4558 0, VEC_length (rtx, sinfo->succs_ok));
4559 VEC_block_remove (rtx, sinfo->succs_other,
4560 0, VEC_length (rtx, sinfo->succs_other));
4561 VEC_block_remove (int, sinfo->probs_ok,
4562 0, VEC_length (int, sinfo->probs_ok));
4563 sinfo->all_prob = 0;
4564 sinfo->succs_ok_n = 0;
4565 sinfo->all_succs_n = 0;
4568 /* Compute successor info for INSN. FLAGS are the flags passed
4569 to the FOR_EACH_SUCC_1 iterator. */
4570 struct succs_info *
4571 compute_succs_info (insn_t insn, short flags)
4573 succ_iterator si;
4574 insn_t succ;
4575 struct succs_info *sinfo = alloc_succs_info ();
4577 /* Traverse *all* successors and decide what to do with each. */
4578 FOR_EACH_SUCC_1 (succ, si, insn, SUCCS_ALL)
4580 /* FIXME: this doesn't work for skipping to loop exits, as we don't
4581 perform code motion through inner loops. */
4582 short current_flags = si.current_flags & ~SUCCS_SKIP_TO_LOOP_EXITS;
4584 if (current_flags & flags)
4586 VEC_safe_push (rtx, heap, sinfo->succs_ok, succ);
4587 VEC_safe_push (int, heap, sinfo->probs_ok,
4588 /* FIXME: Improve calculation when skipping
4589 inner loop to exits. */
4590 (si.bb_end
4591 ? si.e1->probability
4592 : REG_BR_PROB_BASE));
4593 sinfo->succs_ok_n++;
4595 else
4596 VEC_safe_push (rtx, heap, sinfo->succs_other, succ);
4598 /* Compute all_prob. */
4599 if (!si.bb_end)
4600 sinfo->all_prob = REG_BR_PROB_BASE;
4601 else
4602 sinfo->all_prob += si.e1->probability;
4604 sinfo->all_succs_n++;
4607 return sinfo;
4610 /* Return the predecessors of BB in PREDS and their number in N.
4611 Empty blocks are skipped. SIZE is used to allocate PREDS. */
4612 static void
4613 cfg_preds_1 (basic_block bb, insn_t **preds, int *n, int *size)
4615 edge e;
4616 edge_iterator ei;
4618 gcc_assert (BLOCK_TO_BB (bb->index) != 0);
4620 FOR_EACH_EDGE (e, ei, bb->preds)
4622 basic_block pred_bb = e->src;
4623 insn_t bb_end = BB_END (pred_bb);
4625 if (!in_current_region_p (pred_bb))
4627 gcc_assert (flag_sel_sched_pipelining_outer_loops
4628 && current_loop_nest);
4629 continue;
4632 if (sel_bb_empty_p (pred_bb))
4633 cfg_preds_1 (pred_bb, preds, n, size);
4634 else
4636 if (*n == *size)
4637 *preds = XRESIZEVEC (insn_t, *preds,
4638 (*size = 2 * *size + 1));
4639 (*preds)[(*n)++] = bb_end;
4643 gcc_assert (*n != 0
4644 || (flag_sel_sched_pipelining_outer_loops
4645 && current_loop_nest));
4648 /* Find all predecessors of BB and record them in PREDS and their number
4649 in N. Empty blocks are skipped, and only normal (forward in-region)
4650 edges are processed. */
4651 static void
4652 cfg_preds (basic_block bb, insn_t **preds, int *n)
4654 int size = 0;
4656 *preds = NULL;
4657 *n = 0;
4658 cfg_preds_1 (bb, preds, n, &size);
4661 /* Returns true if we are moving INSN through join point. */
4662 bool
4663 sel_num_cfg_preds_gt_1 (insn_t insn)
4665 basic_block bb;
4667 if (!sel_bb_head_p (insn) || INSN_BB (insn) == 0)
4668 return false;
4670 bb = BLOCK_FOR_INSN (insn);
4672 while (1)
4674 if (EDGE_COUNT (bb->preds) > 1)
4675 return true;
4677 gcc_assert (EDGE_PRED (bb, 0)->dest == bb);
4678 bb = EDGE_PRED (bb, 0)->src;
4680 if (!sel_bb_empty_p (bb))
4681 break;
4684 return false;
4687 /* Returns true when BB should be the end of an ebb. Adapted from the
4688 code in sched-ebb.c. */
4689 bool
4690 bb_ends_ebb_p (basic_block bb)
4692 basic_block next_bb = bb_next_bb (bb);
4693 edge e;
4695 if (next_bb == EXIT_BLOCK_PTR
4696 || bitmap_bit_p (forced_ebb_heads, next_bb->index)
4697 || (LABEL_P (BB_HEAD (next_bb))
4698 /* NB: LABEL_NUSES () is not maintained outside of jump.c.
4699 Work around that. */
4700 && !single_pred_p (next_bb)))
4701 return true;
4703 if (!in_current_region_p (next_bb))
4704 return true;
4706 e = find_fallthru_edge (bb->succs);
4707 if (e)
4709 gcc_assert (e->dest == next_bb);
4711 return false;
4714 return true;
4717 /* Returns true when INSN and SUCC are in the same EBB, given that SUCC is a
4718 successor of INSN. */
4719 bool
4720 in_same_ebb_p (insn_t insn, insn_t succ)
4722 basic_block ptr = BLOCK_FOR_INSN (insn);
4724 for(;;)
4726 if (ptr == BLOCK_FOR_INSN (succ))
4727 return true;
4729 if (bb_ends_ebb_p (ptr))
4730 return false;
4732 ptr = bb_next_bb (ptr);
4735 gcc_unreachable ();
4736 return false;
4739 /* Recomputes the reverse topological order for the function and
4740 saves it in REV_TOP_ORDER_INDEX. REV_TOP_ORDER_INDEX_LEN is also
4741 modified appropriately. */
4742 static void
4743 recompute_rev_top_order (void)
4745 int *postorder;
4746 int n_blocks, i;
4748 if (!rev_top_order_index || rev_top_order_index_len < last_basic_block)
4750 rev_top_order_index_len = last_basic_block;
4751 rev_top_order_index = XRESIZEVEC (int, rev_top_order_index,
4752 rev_top_order_index_len);
4755 postorder = XNEWVEC (int, n_basic_blocks);
4757 n_blocks = post_order_compute (postorder, true, false);
4758 gcc_assert (n_basic_blocks == n_blocks);
4760 /* Build reverse function: for each basic block with BB->INDEX == K
4761 rev_top_order_index[K] is it's reverse topological sort number. */
4762 for (i = 0; i < n_blocks; i++)
4764 gcc_assert (postorder[i] < rev_top_order_index_len);
4765 rev_top_order_index[postorder[i]] = i;
4768 free (postorder);
4771 /* Clear all flags from insns in BB that could spoil its rescheduling. */
4772 void
4773 clear_outdated_rtx_info (basic_block bb)
4775 rtx insn;
4777 FOR_BB_INSNS (bb, insn)
4778 if (INSN_P (insn))
4780 SCHED_GROUP_P (insn) = 0;
4781 INSN_AFTER_STALL_P (insn) = 0;
4782 INSN_SCHED_TIMES (insn) = 0;
4783 EXPR_PRIORITY_ADJ (INSN_EXPR (insn)) = 0;
4785 /* We cannot use the changed caches, as previously we could ignore
4786 the LHS dependence due to enabled renaming and transform
4787 the expression, and currently we'll be unable to do this. */
4788 htab_empty (INSN_TRANSFORMED_INSNS (insn));
4792 /* Add BB_NOTE to the pool of available basic block notes. */
4793 static void
4794 return_bb_to_pool (basic_block bb)
4796 rtx note = bb_note (bb);
4798 gcc_assert (NOTE_BASIC_BLOCK (note) == bb
4799 && bb->aux == NULL);
4801 /* It turns out that current cfg infrastructure does not support
4802 reuse of basic blocks. Don't bother for now. */
4803 /*VEC_safe_push (rtx, heap, bb_note_pool, note);*/
4806 /* Get a bb_note from pool or return NULL_RTX if pool is empty. */
4807 static rtx
4808 get_bb_note_from_pool (void)
4810 if (VEC_empty (rtx, bb_note_pool))
4811 return NULL_RTX;
4812 else
4814 rtx note = VEC_pop (rtx, bb_note_pool);
4816 PREV_INSN (note) = NULL_RTX;
4817 NEXT_INSN (note) = NULL_RTX;
4819 return note;
4823 /* Free bb_note_pool. */
4824 void
4825 free_bb_note_pool (void)
4827 VEC_free (rtx, heap, bb_note_pool);
4830 /* Setup scheduler pool and successor structure. */
4831 void
4832 alloc_sched_pools (void)
4834 int succs_size;
4836 succs_size = MAX_WS + 1;
4837 succs_info_pool.stack = XCNEWVEC (struct succs_info, succs_size);
4838 succs_info_pool.size = succs_size;
4839 succs_info_pool.top = -1;
4840 succs_info_pool.max_top = -1;
4842 sched_lists_pool = create_alloc_pool ("sel-sched-lists",
4843 sizeof (struct _list_node), 500);
4846 /* Free the pools. */
4847 void
4848 free_sched_pools (void)
4850 int i;
4852 free_alloc_pool (sched_lists_pool);
4853 gcc_assert (succs_info_pool.top == -1);
4854 for (i = 0; i < succs_info_pool.max_top; i++)
4856 VEC_free (rtx, heap, succs_info_pool.stack[i].succs_ok);
4857 VEC_free (rtx, heap, succs_info_pool.stack[i].succs_other);
4858 VEC_free (int, heap, succs_info_pool.stack[i].probs_ok);
4860 free (succs_info_pool.stack);
4864 /* Returns a position in RGN where BB can be inserted retaining
4865 topological order. */
4866 static int
4867 find_place_to_insert_bb (basic_block bb, int rgn)
4869 bool has_preds_outside_rgn = false;
4870 edge e;
4871 edge_iterator ei;
4873 /* Find whether we have preds outside the region. */
4874 FOR_EACH_EDGE (e, ei, bb->preds)
4875 if (!in_current_region_p (e->src))
4877 has_preds_outside_rgn = true;
4878 break;
4881 /* Recompute the top order -- needed when we have > 1 pred
4882 and in case we don't have preds outside. */
4883 if (flag_sel_sched_pipelining_outer_loops
4884 && (has_preds_outside_rgn || EDGE_COUNT (bb->preds) > 1))
4886 int i, bbi = bb->index, cur_bbi;
4888 recompute_rev_top_order ();
4889 for (i = RGN_NR_BLOCKS (rgn) - 1; i >= 0; i--)
4891 cur_bbi = BB_TO_BLOCK (i);
4892 if (rev_top_order_index[bbi]
4893 < rev_top_order_index[cur_bbi])
4894 break;
4897 /* We skipped the right block, so we increase i. We accomodate
4898 it for increasing by step later, so we decrease i. */
4899 return (i + 1) - 1;
4901 else if (has_preds_outside_rgn)
4903 /* This is the case when we generate an extra empty block
4904 to serve as region head during pipelining. */
4905 e = EDGE_SUCC (bb, 0);
4906 gcc_assert (EDGE_COUNT (bb->succs) == 1
4907 && in_current_region_p (EDGE_SUCC (bb, 0)->dest)
4908 && (BLOCK_TO_BB (e->dest->index) == 0));
4909 return -1;
4912 /* We don't have preds outside the region. We should have
4913 the only pred, because the multiple preds case comes from
4914 the pipelining of outer loops, and that is handled above.
4915 Just take the bbi of this single pred. */
4916 if (EDGE_COUNT (bb->succs) > 0)
4918 int pred_bbi;
4920 gcc_assert (EDGE_COUNT (bb->preds) == 1);
4922 pred_bbi = EDGE_PRED (bb, 0)->src->index;
4923 return BLOCK_TO_BB (pred_bbi);
4925 else
4926 /* BB has no successors. It is safe to put it in the end. */
4927 return current_nr_blocks - 1;
4930 /* Deletes an empty basic block freeing its data. */
4931 static void
4932 delete_and_free_basic_block (basic_block bb)
4934 gcc_assert (sel_bb_empty_p (bb));
4936 if (BB_LV_SET (bb))
4937 free_lv_set (bb);
4939 bitmap_clear_bit (blocks_to_reschedule, bb->index);
4941 /* Can't assert av_set properties because we use sel_aremove_bb
4942 when removing loop preheader from the region. At the point of
4943 removing the preheader we already have deallocated sel_region_bb_info. */
4944 gcc_assert (BB_LV_SET (bb) == NULL
4945 && !BB_LV_SET_VALID_P (bb)
4946 && BB_AV_LEVEL (bb) == 0
4947 && BB_AV_SET (bb) == NULL);
4949 delete_basic_block (bb);
4952 /* Add BB to the current region and update the region data. */
4953 static void
4954 add_block_to_current_region (basic_block bb)
4956 int i, pos, bbi = -2, rgn;
4958 rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
4959 bbi = find_place_to_insert_bb (bb, rgn);
4960 bbi += 1;
4961 pos = RGN_BLOCKS (rgn) + bbi;
4963 gcc_assert (RGN_HAS_REAL_EBB (rgn) == 0
4964 && ebb_head[bbi] == pos);
4966 /* Make a place for the new block. */
4967 extend_regions ();
4969 for (i = RGN_BLOCKS (rgn + 1) - 1; i >= pos; i--)
4970 BLOCK_TO_BB (rgn_bb_table[i])++;
4972 memmove (rgn_bb_table + pos + 1,
4973 rgn_bb_table + pos,
4974 (RGN_BLOCKS (nr_regions) - pos) * sizeof (*rgn_bb_table));
4976 /* Initialize data for BB. */
4977 rgn_bb_table[pos] = bb->index;
4978 BLOCK_TO_BB (bb->index) = bbi;
4979 CONTAINING_RGN (bb->index) = rgn;
4981 RGN_NR_BLOCKS (rgn)++;
4983 for (i = rgn + 1; i <= nr_regions; i++)
4984 RGN_BLOCKS (i)++;
4987 /* Remove BB from the current region and update the region data. */
4988 static void
4989 remove_bb_from_region (basic_block bb)
4991 int i, pos, bbi = -2, rgn;
4993 rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
4994 bbi = BLOCK_TO_BB (bb->index);
4995 pos = RGN_BLOCKS (rgn) + bbi;
4997 gcc_assert (RGN_HAS_REAL_EBB (rgn) == 0
4998 && ebb_head[bbi] == pos);
5000 for (i = RGN_BLOCKS (rgn + 1) - 1; i >= pos; i--)
5001 BLOCK_TO_BB (rgn_bb_table[i])--;
5003 memmove (rgn_bb_table + pos,
5004 rgn_bb_table + pos + 1,
5005 (RGN_BLOCKS (nr_regions) - pos) * sizeof (*rgn_bb_table));
5007 RGN_NR_BLOCKS (rgn)--;
5008 for (i = rgn + 1; i <= nr_regions; i++)
5009 RGN_BLOCKS (i)--;
5012 /* Add BB to the current region and update all data. If BB is NULL, add all
5013 blocks from last_added_blocks vector. */
5014 static void
5015 sel_add_bb (basic_block bb)
5017 /* Extend luids so that new notes will receive zero luids. */
5018 sched_init_luids (NULL, NULL, NULL, NULL);
5019 sched_init_bbs ();
5020 sel_init_bbs (last_added_blocks, NULL);
5022 /* When bb is passed explicitly, the vector should contain
5023 the only element that equals to bb; otherwise, the vector
5024 should not be NULL. */
5025 gcc_assert (last_added_blocks != NULL);
5027 if (bb != NULL)
5029 gcc_assert (VEC_length (basic_block, last_added_blocks) == 1
5030 && VEC_index (basic_block,
5031 last_added_blocks, 0) == bb);
5032 add_block_to_current_region (bb);
5034 /* We associate creating/deleting data sets with the first insn
5035 appearing / disappearing in the bb. */
5036 if (!sel_bb_empty_p (bb) && BB_LV_SET (bb) == NULL)
5037 create_initial_data_sets (bb);
5039 VEC_free (basic_block, heap, last_added_blocks);
5041 else
5042 /* BB is NULL - process LAST_ADDED_BLOCKS instead. */
5044 int i;
5045 basic_block temp_bb = NULL;
5047 for (i = 0;
5048 VEC_iterate (basic_block, last_added_blocks, i, bb); i++)
5050 add_block_to_current_region (bb);
5051 temp_bb = bb;
5054 /* We need to fetch at least one bb so we know the region
5055 to update. */
5056 gcc_assert (temp_bb != NULL);
5057 bb = temp_bb;
5059 VEC_free (basic_block, heap, last_added_blocks);
5062 rgn_setup_region (CONTAINING_RGN (bb->index));
5065 /* Remove BB from the current region and update all data.
5066 If REMOVE_FROM_CFG_PBB is true, also remove the block cfom cfg. */
5067 static void
5068 sel_remove_bb (basic_block bb, bool remove_from_cfg_p)
5070 unsigned idx = bb->index;
5072 gcc_assert (bb != NULL && BB_NOTE_LIST (bb) == NULL_RTX);
5074 remove_bb_from_region (bb);
5075 return_bb_to_pool (bb);
5076 bitmap_clear_bit (blocks_to_reschedule, idx);
5078 if (remove_from_cfg_p)
5079 delete_and_free_basic_block (bb);
5081 rgn_setup_region (CONTAINING_RGN (idx));
5084 /* Concatenate info of EMPTY_BB to info of MERGE_BB. */
5085 static void
5086 move_bb_info (basic_block merge_bb, basic_block empty_bb)
5088 gcc_assert (in_current_region_p (merge_bb));
5090 concat_note_lists (BB_NOTE_LIST (empty_bb),
5091 &BB_NOTE_LIST (merge_bb));
5092 BB_NOTE_LIST (empty_bb) = NULL_RTX;
5096 /* Remove EMPTY_BB. If REMOVE_FROM_CFG_P is false, remove EMPTY_BB from
5097 region, but keep it in CFG. */
5098 static void
5099 remove_empty_bb (basic_block empty_bb, bool remove_from_cfg_p)
5101 /* The block should contain just a note or a label.
5102 We try to check whether it is unused below. */
5103 gcc_assert (BB_HEAD (empty_bb) == BB_END (empty_bb)
5104 || LABEL_P (BB_HEAD (empty_bb)));
5106 /* If basic block has predecessors or successors, redirect them. */
5107 if (remove_from_cfg_p
5108 && (EDGE_COUNT (empty_bb->preds) > 0
5109 || EDGE_COUNT (empty_bb->succs) > 0))
5111 basic_block pred;
5112 basic_block succ;
5114 /* We need to init PRED and SUCC before redirecting edges. */
5115 if (EDGE_COUNT (empty_bb->preds) > 0)
5117 edge e;
5119 gcc_assert (EDGE_COUNT (empty_bb->preds) == 1);
5121 e = EDGE_PRED (empty_bb, 0);
5122 gcc_assert (e->src == empty_bb->prev_bb
5123 && (e->flags & EDGE_FALLTHRU));
5125 pred = empty_bb->prev_bb;
5127 else
5128 pred = NULL;
5130 if (EDGE_COUNT (empty_bb->succs) > 0)
5132 /* We do not check fallthruness here as above, because
5133 after removing a jump the edge may actually be not fallthru. */
5134 gcc_assert (EDGE_COUNT (empty_bb->succs) == 1);
5135 succ = EDGE_SUCC (empty_bb, 0)->dest;
5137 else
5138 succ = NULL;
5140 if (EDGE_COUNT (empty_bb->preds) > 0 && succ != NULL)
5142 edge e = EDGE_PRED (empty_bb, 0);
5144 if (e->flags & EDGE_FALLTHRU)
5145 redirect_edge_succ_nodup (e, succ);
5146 else
5147 sel_redirect_edge_and_branch (EDGE_PRED (empty_bb, 0), succ);
5150 if (EDGE_COUNT (empty_bb->succs) > 0 && pred != NULL)
5152 edge e = EDGE_SUCC (empty_bb, 0);
5154 if (find_edge (pred, e->dest) == NULL)
5155 redirect_edge_pred (e, pred);
5159 /* Finish removing. */
5160 sel_remove_bb (empty_bb, remove_from_cfg_p);
5163 /* An implementation of create_basic_block hook, which additionally updates
5164 per-bb data structures. */
5165 static basic_block
5166 sel_create_basic_block (void *headp, void *endp, basic_block after)
5168 basic_block new_bb;
5169 insn_t new_bb_note;
5171 gcc_assert (flag_sel_sched_pipelining_outer_loops
5172 || last_added_blocks == NULL);
5174 new_bb_note = get_bb_note_from_pool ();
5176 if (new_bb_note == NULL_RTX)
5177 new_bb = orig_cfg_hooks.create_basic_block (headp, endp, after);
5178 else
5180 new_bb = create_basic_block_structure ((rtx) headp, (rtx) endp,
5181 new_bb_note, after);
5182 new_bb->aux = NULL;
5185 VEC_safe_push (basic_block, heap, last_added_blocks, new_bb);
5187 return new_bb;
5190 /* Implement sched_init_only_bb (). */
5191 static void
5192 sel_init_only_bb (basic_block bb, basic_block after)
5194 gcc_assert (after == NULL);
5196 extend_regions ();
5197 rgn_make_new_region_out_of_new_block (bb);
5200 /* Update the latch when we've splitted or merged it from FROM block to TO.
5201 This should be checked for all outer loops, too. */
5202 static void
5203 change_loops_latches (basic_block from, basic_block to)
5205 gcc_assert (from != to);
5207 if (current_loop_nest)
5209 struct loop *loop;
5211 for (loop = current_loop_nest; loop; loop = loop_outer (loop))
5212 if (considered_for_pipelining_p (loop) && loop->latch == from)
5214 gcc_assert (loop == current_loop_nest);
5215 loop->latch = to;
5216 gcc_assert (loop_latch_edge (loop));
5221 /* Splits BB on two basic blocks, adding it to the region and extending
5222 per-bb data structures. Returns the newly created bb. */
5223 static basic_block
5224 sel_split_block (basic_block bb, rtx after)
5226 basic_block new_bb;
5227 insn_t insn;
5229 new_bb = sched_split_block_1 (bb, after);
5230 sel_add_bb (new_bb);
5232 /* This should be called after sel_add_bb, because this uses
5233 CONTAINING_RGN for the new block, which is not yet initialized.
5234 FIXME: this function may be a no-op now. */
5235 change_loops_latches (bb, new_bb);
5237 /* Update ORIG_BB_INDEX for insns moved into the new block. */
5238 FOR_BB_INSNS (new_bb, insn)
5239 if (INSN_P (insn))
5240 EXPR_ORIG_BB_INDEX (INSN_EXPR (insn)) = new_bb->index;
5242 if (sel_bb_empty_p (bb))
5244 gcc_assert (!sel_bb_empty_p (new_bb));
5246 /* NEW_BB has data sets that need to be updated and BB holds
5247 data sets that should be removed. Exchange these data sets
5248 so that we won't lose BB's valid data sets. */
5249 exchange_data_sets (new_bb, bb);
5250 free_data_sets (bb);
5253 if (!sel_bb_empty_p (new_bb)
5254 && bitmap_bit_p (blocks_to_reschedule, bb->index))
5255 bitmap_set_bit (blocks_to_reschedule, new_bb->index);
5257 return new_bb;
5260 /* If BB ends with a jump insn whose ID is bigger then PREV_MAX_UID, return it.
5261 Otherwise returns NULL. */
5262 static rtx
5263 check_for_new_jump (basic_block bb, int prev_max_uid)
5265 rtx end;
5267 end = sel_bb_end (bb);
5268 if (end && INSN_UID (end) >= prev_max_uid)
5269 return end;
5270 return NULL;
5273 /* Look for a new jump either in FROM_BB block or in newly created JUMP_BB block.
5274 New means having UID at least equal to PREV_MAX_UID. */
5275 static rtx
5276 find_new_jump (basic_block from, basic_block jump_bb, int prev_max_uid)
5278 rtx jump;
5280 /* Return immediately if no new insns were emitted. */
5281 if (get_max_uid () == prev_max_uid)
5282 return NULL;
5284 /* Now check both blocks for new jumps. It will ever be only one. */
5285 if ((jump = check_for_new_jump (from, prev_max_uid)))
5286 return jump;
5288 if (jump_bb != NULL
5289 && (jump = check_for_new_jump (jump_bb, prev_max_uid)))
5290 return jump;
5291 return NULL;
5294 /* Splits E and adds the newly created basic block to the current region.
5295 Returns this basic block. */
5296 basic_block
5297 sel_split_edge (edge e)
5299 basic_block new_bb, src, other_bb = NULL;
5300 int prev_max_uid;
5301 rtx jump;
5303 src = e->src;
5304 prev_max_uid = get_max_uid ();
5305 new_bb = split_edge (e);
5307 if (flag_sel_sched_pipelining_outer_loops
5308 && current_loop_nest)
5310 int i;
5311 basic_block bb;
5313 /* Some of the basic blocks might not have been added to the loop.
5314 Add them here, until this is fixed in force_fallthru. */
5315 for (i = 0;
5316 VEC_iterate (basic_block, last_added_blocks, i, bb); i++)
5317 if (!bb->loop_father)
5319 add_bb_to_loop (bb, e->dest->loop_father);
5321 gcc_assert (!other_bb && (new_bb->index != bb->index));
5322 other_bb = bb;
5326 /* Add all last_added_blocks to the region. */
5327 sel_add_bb (NULL);
5329 jump = find_new_jump (src, new_bb, prev_max_uid);
5330 if (jump)
5331 sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP);
5333 /* Put the correct lv set on this block. */
5334 if (other_bb && !sel_bb_empty_p (other_bb))
5335 compute_live (sel_bb_head (other_bb));
5337 return new_bb;
5340 /* Implement sched_create_empty_bb (). */
5341 static basic_block
5342 sel_create_empty_bb (basic_block after)
5344 basic_block new_bb;
5346 new_bb = sched_create_empty_bb_1 (after);
5348 /* We'll explicitly initialize NEW_BB via sel_init_only_bb () a bit
5349 later. */
5350 gcc_assert (VEC_length (basic_block, last_added_blocks) == 1
5351 && VEC_index (basic_block, last_added_blocks, 0) == new_bb);
5353 VEC_free (basic_block, heap, last_added_blocks);
5354 return new_bb;
5357 /* Implement sched_create_recovery_block. ORIG_INSN is where block
5358 will be splitted to insert a check. */
5359 basic_block
5360 sel_create_recovery_block (insn_t orig_insn)
5362 basic_block first_bb, second_bb, recovery_block;
5363 basic_block before_recovery = NULL;
5364 rtx jump;
5366 first_bb = BLOCK_FOR_INSN (orig_insn);
5367 if (sel_bb_end_p (orig_insn))
5369 /* Avoid introducing an empty block while splitting. */
5370 gcc_assert (single_succ_p (first_bb));
5371 second_bb = single_succ (first_bb);
5373 else
5374 second_bb = sched_split_block (first_bb, orig_insn);
5376 recovery_block = sched_create_recovery_block (&before_recovery);
5377 if (before_recovery)
5378 copy_lv_set_from (before_recovery, EXIT_BLOCK_PTR);
5380 gcc_assert (sel_bb_empty_p (recovery_block));
5381 sched_create_recovery_edges (first_bb, recovery_block, second_bb);
5382 if (current_loops != NULL)
5383 add_bb_to_loop (recovery_block, first_bb->loop_father);
5385 sel_add_bb (recovery_block);
5387 jump = BB_END (recovery_block);
5388 gcc_assert (sel_bb_head (recovery_block) == jump);
5389 sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP);
5391 return recovery_block;
5394 /* Merge basic block B into basic block A. */
5395 static void
5396 sel_merge_blocks (basic_block a, basic_block b)
5398 gcc_assert (sel_bb_empty_p (b)
5399 && EDGE_COUNT (b->preds) == 1
5400 && EDGE_PRED (b, 0)->src == b->prev_bb);
5402 move_bb_info (b->prev_bb, b);
5403 remove_empty_bb (b, false);
5404 merge_blocks (a, b);
5405 change_loops_latches (b, a);
5408 /* A wrapper for redirect_edge_and_branch_force, which also initializes
5409 data structures for possibly created bb and insns. Returns the newly
5410 added bb or NULL, when a bb was not needed. */
5411 void
5412 sel_redirect_edge_and_branch_force (edge e, basic_block to)
5414 basic_block jump_bb, src;
5415 int prev_max_uid;
5416 rtx jump;
5418 gcc_assert (!sel_bb_empty_p (e->src));
5420 src = e->src;
5421 prev_max_uid = get_max_uid ();
5422 jump_bb = redirect_edge_and_branch_force (e, to);
5424 if (jump_bb != NULL)
5425 sel_add_bb (jump_bb);
5427 /* This function could not be used to spoil the loop structure by now,
5428 thus we don't care to update anything. But check it to be sure. */
5429 if (current_loop_nest
5430 && pipelining_p)
5431 gcc_assert (loop_latch_edge (current_loop_nest));
5433 jump = find_new_jump (src, jump_bb, prev_max_uid);
5434 if (jump)
5435 sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP);
5438 /* A wrapper for redirect_edge_and_branch. Return TRUE if blocks connected by
5439 redirected edge are in reverse topological order. */
5440 bool
5441 sel_redirect_edge_and_branch (edge e, basic_block to)
5443 bool latch_edge_p;
5444 basic_block src;
5445 int prev_max_uid;
5446 rtx jump;
5447 edge redirected;
5448 bool recompute_toporder_p = false;
5450 latch_edge_p = (pipelining_p
5451 && current_loop_nest
5452 && e == loop_latch_edge (current_loop_nest));
5454 src = e->src;
5455 prev_max_uid = get_max_uid ();
5457 redirected = redirect_edge_and_branch (e, to);
5459 gcc_assert (redirected && last_added_blocks == NULL);
5461 /* When we've redirected a latch edge, update the header. */
5462 if (latch_edge_p)
5464 current_loop_nest->header = to;
5465 gcc_assert (loop_latch_edge (current_loop_nest));
5468 /* In rare situations, the topological relation between the blocks connected
5469 by the redirected edge can change (see PR42245 for an example). Update
5470 block_to_bb/bb_to_block. */
5471 if (CONTAINING_RGN (e->src->index) == CONTAINING_RGN (to->index)
5472 && BLOCK_TO_BB (e->src->index) > BLOCK_TO_BB (to->index))
5473 recompute_toporder_p = true;
5475 jump = find_new_jump (src, NULL, prev_max_uid);
5476 if (jump)
5477 sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP);
5479 return recompute_toporder_p;
5482 /* This variable holds the cfg hooks used by the selective scheduler. */
5483 static struct cfg_hooks sel_cfg_hooks;
5485 /* Register sel-sched cfg hooks. */
5486 void
5487 sel_register_cfg_hooks (void)
5489 sched_split_block = sel_split_block;
5491 orig_cfg_hooks = get_cfg_hooks ();
5492 sel_cfg_hooks = orig_cfg_hooks;
5494 sel_cfg_hooks.create_basic_block = sel_create_basic_block;
5496 set_cfg_hooks (sel_cfg_hooks);
5498 sched_init_only_bb = sel_init_only_bb;
5499 sched_split_block = sel_split_block;
5500 sched_create_empty_bb = sel_create_empty_bb;
5503 /* Unregister sel-sched cfg hooks. */
5504 void
5505 sel_unregister_cfg_hooks (void)
5507 sched_create_empty_bb = NULL;
5508 sched_split_block = NULL;
5509 sched_init_only_bb = NULL;
5511 set_cfg_hooks (orig_cfg_hooks);
5515 /* Emit an insn rtx based on PATTERN. If a jump insn is wanted,
5516 LABEL is where this jump should be directed. */
5518 create_insn_rtx_from_pattern (rtx pattern, rtx label)
5520 rtx insn_rtx;
5522 gcc_assert (!INSN_P (pattern));
5524 start_sequence ();
5526 if (label == NULL_RTX)
5527 insn_rtx = emit_insn (pattern);
5528 else if (DEBUG_INSN_P (label))
5529 insn_rtx = emit_debug_insn (pattern);
5530 else
5532 insn_rtx = emit_jump_insn (pattern);
5533 JUMP_LABEL (insn_rtx) = label;
5534 ++LABEL_NUSES (label);
5537 end_sequence ();
5539 sched_init_luids (NULL, NULL, NULL, NULL);
5540 sched_extend_target ();
5541 sched_deps_init (false);
5543 /* Initialize INSN_CODE now. */
5544 recog_memoized (insn_rtx);
5545 return insn_rtx;
5548 /* Create a new vinsn for INSN_RTX. FORCE_UNIQUE_P is true when the vinsn
5549 must not be clonable. */
5550 vinsn_t
5551 create_vinsn_from_insn_rtx (rtx insn_rtx, bool force_unique_p)
5553 gcc_assert (INSN_P (insn_rtx) && !INSN_IN_STREAM_P (insn_rtx));
5555 /* If VINSN_TYPE is not USE, retain its uniqueness. */
5556 return vinsn_create (insn_rtx, force_unique_p);
5559 /* Create a copy of INSN_RTX. */
5561 create_copy_of_insn_rtx (rtx insn_rtx)
5563 rtx res;
5565 if (DEBUG_INSN_P (insn_rtx))
5566 return create_insn_rtx_from_pattern (copy_rtx (PATTERN (insn_rtx)),
5567 insn_rtx);
5569 gcc_assert (NONJUMP_INSN_P (insn_rtx));
5571 res = create_insn_rtx_from_pattern (copy_rtx (PATTERN (insn_rtx)),
5572 NULL_RTX);
5573 return res;
5576 /* Change vinsn field of EXPR to hold NEW_VINSN. */
5577 void
5578 change_vinsn_in_expr (expr_t expr, vinsn_t new_vinsn)
5580 vinsn_detach (EXPR_VINSN (expr));
5582 EXPR_VINSN (expr) = new_vinsn;
5583 vinsn_attach (new_vinsn);
5586 /* Helpers for global init. */
5587 /* This structure is used to be able to call existing bundling mechanism
5588 and calculate insn priorities. */
5589 static struct haifa_sched_info sched_sel_haifa_sched_info =
5591 NULL, /* init_ready_list */
5592 NULL, /* can_schedule_ready_p */
5593 NULL, /* schedule_more_p */
5594 NULL, /* new_ready */
5595 NULL, /* rgn_rank */
5596 sel_print_insn, /* rgn_print_insn */
5597 contributes_to_priority,
5598 NULL, /* insn_finishes_block_p */
5600 NULL, NULL,
5601 NULL, NULL,
5602 0, 0,
5604 NULL, /* add_remove_insn */
5605 NULL, /* begin_schedule_ready */
5606 NULL, /* advance_target_bb */
5607 SEL_SCHED | NEW_BBS
5610 /* Setup special insns used in the scheduler. */
5611 void
5612 setup_nop_and_exit_insns (void)
5614 gcc_assert (nop_pattern == NULL_RTX
5615 && exit_insn == NULL_RTX);
5617 nop_pattern = constm1_rtx;
5619 start_sequence ();
5620 emit_insn (nop_pattern);
5621 exit_insn = get_insns ();
5622 end_sequence ();
5623 set_block_for_insn (exit_insn, EXIT_BLOCK_PTR);
5626 /* Free special insns used in the scheduler. */
5627 void
5628 free_nop_and_exit_insns (void)
5630 exit_insn = NULL_RTX;
5631 nop_pattern = NULL_RTX;
5634 /* Setup a special vinsn used in new insns initialization. */
5635 void
5636 setup_nop_vinsn (void)
5638 nop_vinsn = vinsn_create (exit_insn, false);
5639 vinsn_attach (nop_vinsn);
5642 /* Free a special vinsn used in new insns initialization. */
5643 void
5644 free_nop_vinsn (void)
5646 gcc_assert (VINSN_COUNT (nop_vinsn) == 1);
5647 vinsn_detach (nop_vinsn);
5648 nop_vinsn = NULL;
5651 /* Call a set_sched_flags hook. */
5652 void
5653 sel_set_sched_flags (void)
5655 /* ??? This means that set_sched_flags were called, and we decided to
5656 support speculation. However, set_sched_flags also modifies flags
5657 on current_sched_info, doing this only at global init. And we
5658 sometimes change c_s_i later. So put the correct flags again. */
5659 if (spec_info && targetm.sched.set_sched_flags)
5660 targetm.sched.set_sched_flags (spec_info);
5663 /* Setup pointers to global sched info structures. */
5664 void
5665 sel_setup_sched_infos (void)
5667 rgn_setup_common_sched_info ();
5669 memcpy (&sel_common_sched_info, common_sched_info,
5670 sizeof (sel_common_sched_info));
5672 sel_common_sched_info.fix_recovery_cfg = NULL;
5673 sel_common_sched_info.add_block = NULL;
5674 sel_common_sched_info.estimate_number_of_insns
5675 = sel_estimate_number_of_insns;
5676 sel_common_sched_info.luid_for_non_insn = sel_luid_for_non_insn;
5677 sel_common_sched_info.sched_pass_id = SCHED_SEL_PASS;
5679 common_sched_info = &sel_common_sched_info;
5681 current_sched_info = &sched_sel_haifa_sched_info;
5682 current_sched_info->sched_max_insns_priority =
5683 get_rgn_sched_max_insns_priority ();
5685 sel_set_sched_flags ();
5689 /* Adds basic block BB to region RGN at the position *BB_ORD_INDEX,
5690 *BB_ORD_INDEX after that is increased. */
5691 static void
5692 sel_add_block_to_region (basic_block bb, int *bb_ord_index, int rgn)
5694 RGN_NR_BLOCKS (rgn) += 1;
5695 RGN_DONT_CALC_DEPS (rgn) = 0;
5696 RGN_HAS_REAL_EBB (rgn) = 0;
5697 CONTAINING_RGN (bb->index) = rgn;
5698 BLOCK_TO_BB (bb->index) = *bb_ord_index;
5699 rgn_bb_table[RGN_BLOCKS (rgn) + *bb_ord_index] = bb->index;
5700 (*bb_ord_index)++;
5702 /* FIXME: it is true only when not scheduling ebbs. */
5703 RGN_BLOCKS (rgn + 1) = RGN_BLOCKS (rgn) + RGN_NR_BLOCKS (rgn);
5706 /* Functions to support pipelining of outer loops. */
5708 /* Creates a new empty region and returns it's number. */
5709 static int
5710 sel_create_new_region (void)
5712 int new_rgn_number = nr_regions;
5714 RGN_NR_BLOCKS (new_rgn_number) = 0;
5716 /* FIXME: This will work only when EBBs are not created. */
5717 if (new_rgn_number != 0)
5718 RGN_BLOCKS (new_rgn_number) = RGN_BLOCKS (new_rgn_number - 1) +
5719 RGN_NR_BLOCKS (new_rgn_number - 1);
5720 else
5721 RGN_BLOCKS (new_rgn_number) = 0;
5723 /* Set the blocks of the next region so the other functions may
5724 calculate the number of blocks in the region. */
5725 RGN_BLOCKS (new_rgn_number + 1) = RGN_BLOCKS (new_rgn_number) +
5726 RGN_NR_BLOCKS (new_rgn_number);
5728 nr_regions++;
5730 return new_rgn_number;
5733 /* If X has a smaller topological sort number than Y, returns -1;
5734 if greater, returns 1. */
5735 static int
5736 bb_top_order_comparator (const void *x, const void *y)
5738 basic_block bb1 = *(const basic_block *) x;
5739 basic_block bb2 = *(const basic_block *) y;
5741 gcc_assert (bb1 == bb2
5742 || rev_top_order_index[bb1->index]
5743 != rev_top_order_index[bb2->index]);
5745 /* It's a reverse topological order in REV_TOP_ORDER_INDEX, so
5746 bbs with greater number should go earlier. */
5747 if (rev_top_order_index[bb1->index] > rev_top_order_index[bb2->index])
5748 return -1;
5749 else
5750 return 1;
5753 /* Create a region for LOOP and return its number. If we don't want
5754 to pipeline LOOP, return -1. */
5755 static int
5756 make_region_from_loop (struct loop *loop)
5758 unsigned int i;
5759 int new_rgn_number = -1;
5760 struct loop *inner;
5762 /* Basic block index, to be assigned to BLOCK_TO_BB. */
5763 int bb_ord_index = 0;
5764 basic_block *loop_blocks;
5765 basic_block preheader_block;
5767 if (loop->num_nodes
5768 > (unsigned) PARAM_VALUE (PARAM_MAX_PIPELINE_REGION_BLOCKS))
5769 return -1;
5771 /* Don't pipeline loops whose latch belongs to some of its inner loops. */
5772 for (inner = loop->inner; inner; inner = inner->inner)
5773 if (flow_bb_inside_loop_p (inner, loop->latch))
5774 return -1;
5776 loop->ninsns = num_loop_insns (loop);
5777 if ((int) loop->ninsns > PARAM_VALUE (PARAM_MAX_PIPELINE_REGION_INSNS))
5778 return -1;
5780 loop_blocks = get_loop_body_in_custom_order (loop, bb_top_order_comparator);
5782 for (i = 0; i < loop->num_nodes; i++)
5783 if (loop_blocks[i]->flags & BB_IRREDUCIBLE_LOOP)
5785 free (loop_blocks);
5786 return -1;
5789 preheader_block = loop_preheader_edge (loop)->src;
5790 gcc_assert (preheader_block);
5791 gcc_assert (loop_blocks[0] == loop->header);
5793 new_rgn_number = sel_create_new_region ();
5795 sel_add_block_to_region (preheader_block, &bb_ord_index, new_rgn_number);
5796 SET_BIT (bbs_in_loop_rgns, preheader_block->index);
5798 for (i = 0; i < loop->num_nodes; i++)
5800 /* Add only those blocks that haven't been scheduled in the inner loop.
5801 The exception is the basic blocks with bookkeeping code - they should
5802 be added to the region (and they actually don't belong to the loop
5803 body, but to the region containing that loop body). */
5805 gcc_assert (new_rgn_number >= 0);
5807 if (! TEST_BIT (bbs_in_loop_rgns, loop_blocks[i]->index))
5809 sel_add_block_to_region (loop_blocks[i], &bb_ord_index,
5810 new_rgn_number);
5811 SET_BIT (bbs_in_loop_rgns, loop_blocks[i]->index);
5815 free (loop_blocks);
5816 MARK_LOOP_FOR_PIPELINING (loop);
5818 return new_rgn_number;
5821 /* Create a new region from preheader blocks LOOP_BLOCKS. */
5822 void
5823 make_region_from_loop_preheader (VEC(basic_block, heap) **loop_blocks)
5825 unsigned int i;
5826 int new_rgn_number = -1;
5827 basic_block bb;
5829 /* Basic block index, to be assigned to BLOCK_TO_BB. */
5830 int bb_ord_index = 0;
5832 new_rgn_number = sel_create_new_region ();
5834 FOR_EACH_VEC_ELT (basic_block, *loop_blocks, i, bb)
5836 gcc_assert (new_rgn_number >= 0);
5838 sel_add_block_to_region (bb, &bb_ord_index, new_rgn_number);
5841 VEC_free (basic_block, heap, *loop_blocks);
5842 gcc_assert (*loop_blocks == NULL);
5846 /* Create region(s) from loop nest LOOP, such that inner loops will be
5847 pipelined before outer loops. Returns true when a region for LOOP
5848 is created. */
5849 static bool
5850 make_regions_from_loop_nest (struct loop *loop)
5852 struct loop *cur_loop;
5853 int rgn_number;
5855 /* Traverse all inner nodes of the loop. */
5856 for (cur_loop = loop->inner; cur_loop; cur_loop = cur_loop->next)
5857 if (! TEST_BIT (bbs_in_loop_rgns, cur_loop->header->index))
5858 return false;
5860 /* At this moment all regular inner loops should have been pipelined.
5861 Try to create a region from this loop. */
5862 rgn_number = make_region_from_loop (loop);
5864 if (rgn_number < 0)
5865 return false;
5867 VEC_safe_push (loop_p, heap, loop_nests, loop);
5868 return true;
5871 /* Initalize data structures needed. */
5872 void
5873 sel_init_pipelining (void)
5875 /* Collect loop information to be used in outer loops pipelining. */
5876 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
5877 | LOOPS_HAVE_FALLTHRU_PREHEADERS
5878 | LOOPS_HAVE_RECORDED_EXITS
5879 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS);
5880 current_loop_nest = NULL;
5882 bbs_in_loop_rgns = sbitmap_alloc (last_basic_block);
5883 sbitmap_zero (bbs_in_loop_rgns);
5885 recompute_rev_top_order ();
5888 /* Returns a struct loop for region RGN. */
5889 loop_p
5890 get_loop_nest_for_rgn (unsigned int rgn)
5892 /* Regions created with extend_rgns don't have corresponding loop nests,
5893 because they don't represent loops. */
5894 if (rgn < VEC_length (loop_p, loop_nests))
5895 return VEC_index (loop_p, loop_nests, rgn);
5896 else
5897 return NULL;
5900 /* True when LOOP was included into pipelining regions. */
5901 bool
5902 considered_for_pipelining_p (struct loop *loop)
5904 if (loop_depth (loop) == 0)
5905 return false;
5907 /* Now, the loop could be too large or irreducible. Check whether its
5908 region is in LOOP_NESTS.
5909 We determine the region number of LOOP as the region number of its
5910 latch. We can't use header here, because this header could be
5911 just removed preheader and it will give us the wrong region number.
5912 Latch can't be used because it could be in the inner loop too. */
5913 if (LOOP_MARKED_FOR_PIPELINING_P (loop))
5915 int rgn = CONTAINING_RGN (loop->latch->index);
5917 gcc_assert ((unsigned) rgn < VEC_length (loop_p, loop_nests));
5918 return true;
5921 return false;
5924 /* Makes regions from the rest of the blocks, after loops are chosen
5925 for pipelining. */
5926 static void
5927 make_regions_from_the_rest (void)
5929 int cur_rgn_blocks;
5930 int *loop_hdr;
5931 int i;
5933 basic_block bb;
5934 edge e;
5935 edge_iterator ei;
5936 int *degree;
5938 /* Index in rgn_bb_table where to start allocating new regions. */
5939 cur_rgn_blocks = nr_regions ? RGN_BLOCKS (nr_regions) : 0;
5941 /* Make regions from all the rest basic blocks - those that don't belong to
5942 any loop or belong to irreducible loops. Prepare the data structures
5943 for extend_rgns. */
5945 /* LOOP_HDR[I] == -1 if I-th bb doesn't belong to any loop,
5946 LOOP_HDR[I] == LOOP_HDR[J] iff basic blocks I and J reside within the same
5947 loop. */
5948 loop_hdr = XNEWVEC (int, last_basic_block);
5949 degree = XCNEWVEC (int, last_basic_block);
5952 /* For each basic block that belongs to some loop assign the number
5953 of innermost loop it belongs to. */
5954 for (i = 0; i < last_basic_block; i++)
5955 loop_hdr[i] = -1;
5957 FOR_EACH_BB (bb)
5959 if (bb->loop_father && !bb->loop_father->num == 0
5960 && !(bb->flags & BB_IRREDUCIBLE_LOOP))
5961 loop_hdr[bb->index] = bb->loop_father->num;
5964 /* For each basic block degree is calculated as the number of incoming
5965 edges, that are going out of bbs that are not yet scheduled.
5966 The basic blocks that are scheduled have degree value of zero. */
5967 FOR_EACH_BB (bb)
5969 degree[bb->index] = 0;
5971 if (!TEST_BIT (bbs_in_loop_rgns, bb->index))
5973 FOR_EACH_EDGE (e, ei, bb->preds)
5974 if (!TEST_BIT (bbs_in_loop_rgns, e->src->index))
5975 degree[bb->index]++;
5977 else
5978 degree[bb->index] = -1;
5981 extend_rgns (degree, &cur_rgn_blocks, bbs_in_loop_rgns, loop_hdr);
5983 /* Any block that did not end up in a region is placed into a region
5984 by itself. */
5985 FOR_EACH_BB (bb)
5986 if (degree[bb->index] >= 0)
5988 rgn_bb_table[cur_rgn_blocks] = bb->index;
5989 RGN_NR_BLOCKS (nr_regions) = 1;
5990 RGN_BLOCKS (nr_regions) = cur_rgn_blocks++;
5991 RGN_DONT_CALC_DEPS (nr_regions) = 0;
5992 RGN_HAS_REAL_EBB (nr_regions) = 0;
5993 CONTAINING_RGN (bb->index) = nr_regions++;
5994 BLOCK_TO_BB (bb->index) = 0;
5997 free (degree);
5998 free (loop_hdr);
6001 /* Free data structures used in pipelining of loops. */
6002 void sel_finish_pipelining (void)
6004 loop_iterator li;
6005 struct loop *loop;
6007 /* Release aux fields so we don't free them later by mistake. */
6008 FOR_EACH_LOOP (li, loop, 0)
6009 loop->aux = NULL;
6011 loop_optimizer_finalize ();
6013 VEC_free (loop_p, heap, loop_nests);
6015 free (rev_top_order_index);
6016 rev_top_order_index = NULL;
6019 /* This function replaces the find_rgns when
6020 FLAG_SEL_SCHED_PIPELINING_OUTER_LOOPS is set. */
6021 void
6022 sel_find_rgns (void)
6024 sel_init_pipelining ();
6025 extend_regions ();
6027 if (current_loops)
6029 loop_p loop;
6030 loop_iterator li;
6032 FOR_EACH_LOOP (li, loop, (flag_sel_sched_pipelining_outer_loops
6033 ? LI_FROM_INNERMOST
6034 : LI_ONLY_INNERMOST))
6035 make_regions_from_loop_nest (loop);
6038 /* Make regions from all the rest basic blocks and schedule them.
6039 These blocks include blocks that don't belong to any loop or belong
6040 to irreducible loops. */
6041 make_regions_from_the_rest ();
6043 /* We don't need bbs_in_loop_rgns anymore. */
6044 sbitmap_free (bbs_in_loop_rgns);
6045 bbs_in_loop_rgns = NULL;
6048 /* Adds the preheader blocks from previous loop to current region taking
6049 it from LOOP_PREHEADER_BLOCKS (current_loop_nest).
6050 This function is only used with -fsel-sched-pipelining-outer-loops. */
6051 void
6052 sel_add_loop_preheaders (void)
6054 int i;
6055 basic_block bb;
6056 VEC(basic_block, heap) *preheader_blocks
6057 = LOOP_PREHEADER_BLOCKS (current_loop_nest);
6059 for (i = 0;
6060 VEC_iterate (basic_block, preheader_blocks, i, bb);
6061 i++)
6063 VEC_safe_push (basic_block, heap, last_added_blocks, bb);
6064 sel_add_bb (bb);
6067 VEC_free (basic_block, heap, preheader_blocks);
6070 /* While pipelining outer loops, returns TRUE if BB is a loop preheader.
6071 Please note that the function should also work when pipelining_p is
6072 false, because it is used when deciding whether we should or should
6073 not reschedule pipelined code. */
6074 bool
6075 sel_is_loop_preheader_p (basic_block bb)
6077 if (current_loop_nest)
6079 struct loop *outer;
6081 if (preheader_removed)
6082 return false;
6084 /* Preheader is the first block in the region. */
6085 if (BLOCK_TO_BB (bb->index) == 0)
6086 return true;
6088 /* We used to find a preheader with the topological information.
6089 Check that the above code is equivalent to what we did before. */
6091 if (in_current_region_p (current_loop_nest->header))
6092 gcc_assert (!(BLOCK_TO_BB (bb->index)
6093 < BLOCK_TO_BB (current_loop_nest->header->index)));
6095 /* Support the situation when the latch block of outer loop
6096 could be from here. */
6097 for (outer = loop_outer (current_loop_nest);
6098 outer;
6099 outer = loop_outer (outer))
6100 if (considered_for_pipelining_p (outer) && outer->latch == bb)
6101 gcc_unreachable ();
6104 return false;
6107 /* Checks whether JUMP leads to basic block DEST_BB and no other blocks. */
6108 bool
6109 jump_leads_only_to_bb_p (insn_t jump, basic_block dest_bb)
6111 basic_block jump_bb = BLOCK_FOR_INSN (jump);
6113 /* It is not jump, jump with side-effects or jump can lead to several
6114 basic blocks. */
6115 if (!onlyjump_p (jump)
6116 || !any_uncondjump_p (jump))
6117 return false;
6119 /* Several outgoing edges, abnormal edge or destination of jump is
6120 not DEST_BB. */
6121 if (EDGE_COUNT (jump_bb->succs) != 1
6122 || EDGE_SUCC (jump_bb, 0)->flags & EDGE_ABNORMAL
6123 || EDGE_SUCC (jump_bb, 0)->dest != dest_bb)
6124 return false;
6126 /* If not anything of the upper. */
6127 return true;
6130 /* Removes the loop preheader from the current region and saves it in
6131 PREHEADER_BLOCKS of the father loop, so they will be added later to
6132 region that represents an outer loop. */
6133 static void
6134 sel_remove_loop_preheader (void)
6136 int i, old_len;
6137 int cur_rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
6138 basic_block bb;
6139 bool all_empty_p = true;
6140 VEC(basic_block, heap) *preheader_blocks
6141 = LOOP_PREHEADER_BLOCKS (loop_outer (current_loop_nest));
6143 gcc_assert (current_loop_nest);
6144 old_len = VEC_length (basic_block, preheader_blocks);
6146 /* Add blocks that aren't within the current loop to PREHEADER_BLOCKS. */
6147 for (i = 0; i < RGN_NR_BLOCKS (cur_rgn); i++)
6149 bb = BASIC_BLOCK (BB_TO_BLOCK (i));
6151 /* If the basic block belongs to region, but doesn't belong to
6152 corresponding loop, then it should be a preheader. */
6153 if (sel_is_loop_preheader_p (bb))
6155 VEC_safe_push (basic_block, heap, preheader_blocks, bb);
6156 if (BB_END (bb) != bb_note (bb))
6157 all_empty_p = false;
6161 /* Remove these blocks only after iterating over the whole region. */
6162 for (i = VEC_length (basic_block, preheader_blocks) - 1;
6163 i >= old_len;
6164 i--)
6166 bb = VEC_index (basic_block, preheader_blocks, i);
6167 sel_remove_bb (bb, false);
6170 if (!considered_for_pipelining_p (loop_outer (current_loop_nest)))
6172 if (!all_empty_p)
6173 /* Immediately create new region from preheader. */
6174 make_region_from_loop_preheader (&preheader_blocks);
6175 else
6177 /* If all preheader blocks are empty - dont create new empty region.
6178 Instead, remove them completely. */
6179 FOR_EACH_VEC_ELT (basic_block, preheader_blocks, i, bb)
6181 edge e;
6182 edge_iterator ei;
6183 basic_block prev_bb = bb->prev_bb, next_bb = bb->next_bb;
6185 /* Redirect all incoming edges to next basic block. */
6186 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
6188 if (! (e->flags & EDGE_FALLTHRU))
6189 redirect_edge_and_branch (e, bb->next_bb);
6190 else
6191 redirect_edge_succ (e, bb->next_bb);
6193 gcc_assert (BB_NOTE_LIST (bb) == NULL);
6194 delete_and_free_basic_block (bb);
6196 /* Check if after deleting preheader there is a nonconditional
6197 jump in PREV_BB that leads to the next basic block NEXT_BB.
6198 If it is so - delete this jump and clear data sets of its
6199 basic block if it becomes empty. */
6200 if (next_bb->prev_bb == prev_bb
6201 && prev_bb != ENTRY_BLOCK_PTR
6202 && jump_leads_only_to_bb_p (BB_END (prev_bb), next_bb))
6204 redirect_edge_and_branch (EDGE_SUCC (prev_bb, 0), next_bb);
6205 if (BB_END (prev_bb) == bb_note (prev_bb))
6206 free_data_sets (prev_bb);
6210 VEC_free (basic_block, heap, preheader_blocks);
6212 else
6213 /* Store preheader within the father's loop structure. */
6214 SET_LOOP_PREHEADER_BLOCKS (loop_outer (current_loop_nest),
6215 preheader_blocks);
6217 #endif