]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/sel-sched-ir.c
backport: As described in http://gcc.gnu.org/ml/gcc/2012-08/msg00015.html...
[thirdparty/gcc.git] / gcc / sel-sched-ir.c
CommitLineData
e855c69d 1/* Instruction scheduling pass. Selective scheduler and pipeliner.
86014d07
BS
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
e855c69d
AB
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
718f9c0f 25#include "diagnostic-core.h"
e855c69d
AB
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"
e855c69d
AB
35#include "recog.h"
36#include "params.h"
37#include "target.h"
e855c69d
AB
38#include "sched-int.h"
39#include "ggc.h"
40#include "tree.h"
41#include "vec.h"
42#include "langhooks.h"
43#include "rtlhooks-def.h"
5936d944 44#include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
e855c69d
AB
45
46#ifdef INSN_SCHEDULING
47#include "sel-sched-ir.h"
48/* We don't have to use it except for sel_print_insn. */
49#include "sel-sched-dump.h"
50
51/* A vector holding bb info for whole scheduling pass. */
52VEC(sel_global_bb_info_def, heap) *sel_global_bb_info = NULL;
53
54/* A vector holding bb info. */
55VEC(sel_region_bb_info_def, heap) *sel_region_bb_info = NULL;
56
57/* A pool for allocating all lists. */
58alloc_pool sched_lists_pool;
59
60/* This contains information about successors for compute_av_set. */
61struct succs_info current_succs;
62
63/* Data structure to describe interaction with the generic scheduler utils. */
64static struct common_sched_info_def sel_common_sched_info;
65
66/* The loop nest being pipelined. */
67struct loop *current_loop_nest;
68
69/* LOOP_NESTS is a vector containing the corresponding loop nest for
70 each region. */
71static VEC(loop_p, heap) *loop_nests = NULL;
72
73/* Saves blocks already in loop regions, indexed by bb->index. */
74static sbitmap bbs_in_loop_rgns = NULL;
75
76/* CFG hooks that are saved before changing create_basic_block hook. */
77static struct cfg_hooks orig_cfg_hooks;
78\f
79
80/* Array containing reverse topological index of function basic blocks,
81 indexed by BB->INDEX. */
82static int *rev_top_order_index = NULL;
83
84/* Length of the above array. */
85static int rev_top_order_index_len = -1;
86
87/* A regset pool structure. */
88static struct
89{
90 /* The stack to which regsets are returned. */
91 regset *v;
92
93 /* Its pointer. */
94 int n;
95
96 /* Its size. */
97 int s;
98
99 /* In VV we save all generated regsets so that, when destructing the
100 pool, we can compare it with V and check that every regset was returned
101 back to pool. */
102 regset *vv;
103
104 /* The pointer of VV stack. */
105 int nn;
106
107 /* Its size. */
108 int ss;
109
110 /* The difference between allocated and returned regsets. */
111 int diff;
112} regset_pool = { NULL, 0, 0, NULL, 0, 0, 0 };
113
114/* This represents the nop pool. */
115static struct
116{
117 /* The vector which holds previously emitted nops. */
118 insn_t *v;
119
120 /* Its pointer. */
121 int n;
122
123 /* Its size. */
b8698a0f 124 int s;
e855c69d
AB
125} nop_pool = { NULL, 0, 0 };
126
127/* The pool for basic block notes. */
128static rtx_vec_t bb_note_pool;
129
130/* A NOP pattern used to emit placeholder insns. */
131rtx nop_pattern = NULL_RTX;
132/* A special instruction that resides in EXIT_BLOCK.
133 EXIT_INSN is successor of the insns that lead to EXIT_BLOCK. */
134rtx exit_insn = NULL_RTX;
135
b8698a0f 136/* TRUE if while scheduling current region, which is loop, its preheader
e855c69d
AB
137 was removed. */
138bool preheader_removed = false;
139\f
140
141/* Forward static declarations. */
142static void fence_clear (fence_t);
143
144static void deps_init_id (idata_t, insn_t, bool);
145static void init_id_from_df (idata_t, insn_t, bool);
146static expr_t set_insn_init (expr_t, vinsn_t, int);
147
148static void cfg_preds (basic_block, insn_t **, int *);
149static void prepare_insn_expr (insn_t, int);
150static void free_history_vect (VEC (expr_history_def, heap) **);
151
152static void move_bb_info (basic_block, basic_block);
153static void remove_empty_bb (basic_block, bool);
262d8232 154static void sel_merge_blocks (basic_block, basic_block);
e855c69d 155static void sel_remove_loop_preheader (void);
753de8cf 156static bool bb_has_removable_jump_to_p (basic_block, basic_block);
e855c69d
AB
157
158static bool insn_is_the_only_one_in_bb_p (insn_t);
159static void create_initial_data_sets (basic_block);
160
b5b8b0ac 161static void free_av_set (basic_block);
e855c69d
AB
162static void invalidate_av_set (basic_block);
163static void extend_insn_data (void);
164static void sel_init_new_insn (insn_t, int);
165static void finish_insns (void);
166\f
167/* Various list functions. */
168
169/* Copy an instruction list L. */
170ilist_t
171ilist_copy (ilist_t l)
172{
173 ilist_t head = NULL, *tailp = &head;
174
175 while (l)
176 {
177 ilist_add (tailp, ILIST_INSN (l));
178 tailp = &ILIST_NEXT (*tailp);
179 l = ILIST_NEXT (l);
180 }
181
182 return head;
183}
184
185/* Invert an instruction list L. */
186ilist_t
187ilist_invert (ilist_t l)
188{
189 ilist_t res = NULL;
190
191 while (l)
192 {
193 ilist_add (&res, ILIST_INSN (l));
194 l = ILIST_NEXT (l);
195 }
196
197 return res;
198}
199
200/* Add a new boundary to the LP list with parameters TO, PTR, and DC. */
201void
202blist_add (blist_t *lp, insn_t to, ilist_t ptr, deps_t dc)
203{
204 bnd_t bnd;
205
206 _list_add (lp);
207 bnd = BLIST_BND (*lp);
208
209 BND_TO (bnd) = to;
210 BND_PTR (bnd) = ptr;
211 BND_AV (bnd) = NULL;
212 BND_AV1 (bnd) = NULL;
213 BND_DC (bnd) = dc;
214}
215
216/* Remove the list note pointed to by LP. */
217void
218blist_remove (blist_t *lp)
219{
220 bnd_t b = BLIST_BND (*lp);
221
222 av_set_clear (&BND_AV (b));
223 av_set_clear (&BND_AV1 (b));
224 ilist_clear (&BND_PTR (b));
225
226 _list_remove (lp);
227}
228
229/* Init a fence tail L. */
230void
231flist_tail_init (flist_tail_t l)
232{
233 FLIST_TAIL_HEAD (l) = NULL;
234 FLIST_TAIL_TAILP (l) = &FLIST_TAIL_HEAD (l);
235}
236
237/* Try to find fence corresponding to INSN in L. */
238fence_t
239flist_lookup (flist_t l, insn_t insn)
240{
241 while (l)
242 {
243 if (FENCE_INSN (FLIST_FENCE (l)) == insn)
244 return FLIST_FENCE (l);
245
246 l = FLIST_NEXT (l);
247 }
248
249 return NULL;
250}
251
252/* Init the fields of F before running fill_insns. */
253static void
254init_fence_for_scheduling (fence_t f)
255{
256 FENCE_BNDS (f) = NULL;
257 FENCE_PROCESSED_P (f) = false;
258 FENCE_SCHEDULED_P (f) = false;
259}
260
261/* Add new fence consisting of INSN and STATE to the list pointed to by LP. */
262static void
b8698a0f
L
263flist_add (flist_t *lp, insn_t insn, state_t state, deps_t dc, void *tc,
264 insn_t last_scheduled_insn, VEC(rtx,gc) *executing_insns,
265 int *ready_ticks, int ready_ticks_size, insn_t sched_next,
136e01a3 266 int cycle, int cycle_issued_insns, int issue_more,
e855c69d
AB
267 bool starts_cycle_p, bool after_stall_p)
268{
269 fence_t f;
270
271 _list_add (lp);
272 f = FLIST_FENCE (*lp);
273
274 FENCE_INSN (f) = insn;
275
276 gcc_assert (state != NULL);
277 FENCE_STATE (f) = state;
278
279 FENCE_CYCLE (f) = cycle;
280 FENCE_ISSUED_INSNS (f) = cycle_issued_insns;
281 FENCE_STARTS_CYCLE_P (f) = starts_cycle_p;
282 FENCE_AFTER_STALL_P (f) = after_stall_p;
283
284 gcc_assert (dc != NULL);
285 FENCE_DC (f) = dc;
286
287 gcc_assert (tc != NULL || targetm.sched.alloc_sched_context == NULL);
288 FENCE_TC (f) = tc;
289
290 FENCE_LAST_SCHEDULED_INSN (f) = last_scheduled_insn;
136e01a3 291 FENCE_ISSUE_MORE (f) = issue_more;
e855c69d
AB
292 FENCE_EXECUTING_INSNS (f) = executing_insns;
293 FENCE_READY_TICKS (f) = ready_ticks;
294 FENCE_READY_TICKS_SIZE (f) = ready_ticks_size;
295 FENCE_SCHED_NEXT (f) = sched_next;
296
297 init_fence_for_scheduling (f);
298}
299
300/* Remove the head node of the list pointed to by LP. */
301static void
302flist_remove (flist_t *lp)
303{
304 if (FENCE_INSN (FLIST_FENCE (*lp)))
305 fence_clear (FLIST_FENCE (*lp));
306 _list_remove (lp);
307}
308
309/* Clear the fence list pointed to by LP. */
310void
311flist_clear (flist_t *lp)
312{
313 while (*lp)
314 flist_remove (lp);
315}
316
317/* Add ORIGINAL_INSN the def list DL honoring CROSSES_CALL. */
318void
319def_list_add (def_list_t *dl, insn_t original_insn, bool crosses_call)
320{
321 def_t d;
b8698a0f 322
e855c69d
AB
323 _list_add (dl);
324 d = DEF_LIST_DEF (*dl);
325
326 d->orig_insn = original_insn;
327 d->crosses_call = crosses_call;
328}
329\f
330
331/* Functions to work with target contexts. */
332
b8698a0f 333/* Bulk target context. It is convenient for debugging purposes to ensure
e855c69d
AB
334 that there are no uninitialized (null) target contexts. */
335static tc_t bulk_tc = (tc_t) 1;
336
b8698a0f 337/* Target hooks wrappers. In the future we can provide some default
e855c69d
AB
338 implementations for them. */
339
340/* Allocate a store for the target context. */
341static tc_t
342alloc_target_context (void)
343{
344 return (targetm.sched.alloc_sched_context
345 ? targetm.sched.alloc_sched_context () : bulk_tc);
346}
347
348/* Init target context TC.
349 If CLEAN_P is true, then make TC as it is beginning of the scheduler.
350 Overwise, copy current backend context to TC. */
351static void
352init_target_context (tc_t tc, bool clean_p)
353{
354 if (targetm.sched.init_sched_context)
355 targetm.sched.init_sched_context (tc, clean_p);
356}
357
358/* Allocate and initialize a target context. Meaning of CLEAN_P is the same as
359 int init_target_context (). */
360tc_t
361create_target_context (bool clean_p)
362{
363 tc_t tc = alloc_target_context ();
364
365 init_target_context (tc, clean_p);
366 return tc;
367}
368
369/* Copy TC to the current backend context. */
370void
371set_target_context (tc_t tc)
372{
373 if (targetm.sched.set_sched_context)
374 targetm.sched.set_sched_context (tc);
375}
376
377/* TC is about to be destroyed. Free any internal data. */
378static void
379clear_target_context (tc_t tc)
380{
381 if (targetm.sched.clear_sched_context)
382 targetm.sched.clear_sched_context (tc);
383}
384
385/* Clear and free it. */
386static void
387delete_target_context (tc_t tc)
388{
389 clear_target_context (tc);
390
391 if (targetm.sched.free_sched_context)
392 targetm.sched.free_sched_context (tc);
393}
394
395/* Make a copy of FROM in TO.
396 NB: May be this should be a hook. */
397static void
398copy_target_context (tc_t to, tc_t from)
399{
400 tc_t tmp = create_target_context (false);
401
402 set_target_context (from);
403 init_target_context (to, false);
404
405 set_target_context (tmp);
406 delete_target_context (tmp);
407}
408
409/* Create a copy of TC. */
410static tc_t
411create_copy_of_target_context (tc_t tc)
412{
413 tc_t copy = alloc_target_context ();
414
415 copy_target_context (copy, tc);
416
417 return copy;
418}
419
420/* Clear TC and initialize it according to CLEAN_P. The meaning of CLEAN_P
421 is the same as in init_target_context (). */
422void
423reset_target_context (tc_t tc, bool clean_p)
424{
425 clear_target_context (tc);
426 init_target_context (tc, clean_p);
427}
428\f
b8698a0f 429/* Functions to work with dependence contexts.
88302d54 430 Dc (aka deps context, aka deps_t, aka struct deps_desc *) is short for dependence
e855c69d
AB
431 context. It accumulates information about processed insns to decide if
432 current insn is dependent on the processed ones. */
433
434/* Make a copy of FROM in TO. */
435static void
436copy_deps_context (deps_t to, deps_t from)
437{
bcf33775 438 init_deps (to, false);
e855c69d
AB
439 deps_join (to, from);
440}
441
442/* Allocate store for dep context. */
443static deps_t
444alloc_deps_context (void)
445{
88302d54 446 return XNEW (struct deps_desc);
e855c69d
AB
447}
448
449/* Allocate and initialize dep context. */
450static deps_t
451create_deps_context (void)
452{
453 deps_t dc = alloc_deps_context ();
454
bcf33775 455 init_deps (dc, false);
e855c69d
AB
456 return dc;
457}
458
459/* Create a copy of FROM. */
460static deps_t
461create_copy_of_deps_context (deps_t from)
462{
463 deps_t to = alloc_deps_context ();
464
465 copy_deps_context (to, from);
466 return to;
467}
468
469/* Clean up internal data of DC. */
470static void
471clear_deps_context (deps_t dc)
472{
473 free_deps (dc);
474}
475
476/* Clear and free DC. */
477static void
478delete_deps_context (deps_t dc)
479{
480 clear_deps_context (dc);
481 free (dc);
482}
483
484/* Clear and init DC. */
485static void
486reset_deps_context (deps_t dc)
487{
488 clear_deps_context (dc);
bcf33775 489 init_deps (dc, false);
e855c69d
AB
490}
491
b8698a0f 492/* This structure describes the dependence analysis hooks for advancing
e855c69d
AB
493 dependence context. */
494static struct sched_deps_info_def advance_deps_context_sched_deps_info =
495 {
496 NULL,
497
498 NULL, /* start_insn */
499 NULL, /* finish_insn */
500 NULL, /* start_lhs */
501 NULL, /* finish_lhs */
502 NULL, /* start_rhs */
503 NULL, /* finish_rhs */
504 haifa_note_reg_set,
505 haifa_note_reg_clobber,
506 haifa_note_reg_use,
507 NULL, /* note_mem_dep */
508 NULL, /* note_dep */
509
510 0, 0, 0
511 };
512
513/* Process INSN and add its impact on DC. */
514void
515advance_deps_context (deps_t dc, insn_t insn)
516{
517 sched_deps_info = &advance_deps_context_sched_deps_info;
518 deps_analyze_insn (dc, insn);
519}
520\f
521
522/* Functions to work with DFA states. */
523
524/* Allocate store for a DFA state. */
525static state_t
526state_alloc (void)
527{
528 return xmalloc (dfa_state_size);
529}
530
531/* Allocate and initialize DFA state. */
532static state_t
533state_create (void)
534{
535 state_t state = state_alloc ();
536
537 state_reset (state);
538 advance_state (state);
539 return state;
540}
541
542/* Free DFA state. */
543static void
544state_free (state_t state)
545{
546 free (state);
547}
548
549/* Make a copy of FROM in TO. */
550static void
551state_copy (state_t to, state_t from)
552{
553 memcpy (to, from, dfa_state_size);
554}
555
556/* Create a copy of FROM. */
557static state_t
558state_create_copy (state_t from)
559{
560 state_t to = state_alloc ();
561
562 state_copy (to, from);
563 return to;
564}
565\f
566
567/* Functions to work with fences. */
568
569/* Clear the fence. */
570static void
571fence_clear (fence_t f)
572{
573 state_t s = FENCE_STATE (f);
574 deps_t dc = FENCE_DC (f);
575 void *tc = FENCE_TC (f);
576
577 ilist_clear (&FENCE_BNDS (f));
578
579 gcc_assert ((s != NULL && dc != NULL && tc != NULL)
580 || (s == NULL && dc == NULL && tc == NULL));
581
04695783 582 free (s);
e855c69d
AB
583
584 if (dc != NULL)
585 delete_deps_context (dc);
586
587 if (tc != NULL)
588 delete_target_context (tc);
589 VEC_free (rtx, gc, FENCE_EXECUTING_INSNS (f));
590 free (FENCE_READY_TICKS (f));
591 FENCE_READY_TICKS (f) = NULL;
592}
593
594/* Init a list of fences with successors of OLD_FENCE. */
595void
596init_fences (insn_t old_fence)
597{
598 insn_t succ;
599 succ_iterator si;
600 bool first = true;
601 int ready_ticks_size = get_max_uid () + 1;
b8698a0f
L
602
603 FOR_EACH_SUCC_1 (succ, si, old_fence,
e855c69d
AB
604 SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
605 {
b8698a0f 606
e855c69d
AB
607 if (first)
608 first = false;
609 else
610 gcc_assert (flag_sel_sched_pipelining_outer_loops);
611
612 flist_add (&fences, succ,
613 state_create (),
614 create_deps_context () /* dc */,
615 create_target_context (true) /* tc */,
b8698a0f 616 NULL_RTX /* last_scheduled_insn */,
e855c69d
AB
617 NULL, /* executing_insns */
618 XCNEWVEC (int, ready_ticks_size), /* ready_ticks */
619 ready_ticks_size,
620 NULL_RTX /* sched_next */,
b8698a0f 621 1 /* cycle */, 0 /* cycle_issued_insns */,
136e01a3 622 issue_rate, /* issue_more */
b8698a0f 623 1 /* starts_cycle_p */, 0 /* after_stall_p */);
e855c69d
AB
624 }
625}
626
627/* Merges two fences (filling fields of fence F with resulting values) by
628 following rules: 1) state, target context and last scheduled insn are
b8698a0f 629 propagated from fallthrough edge if it is available;
e855c69d 630 2) deps context and cycle is propagated from more probable edge;
b8698a0f 631 3) all other fields are set to corresponding constant values.
e855c69d 632
b8698a0f 633 INSN, STATE, DC, TC, LAST_SCHEDULED_INSN, EXECUTING_INSNS,
136e01a3
AB
634 READY_TICKS, READY_TICKS_SIZE, SCHED_NEXT, CYCLE, ISSUE_MORE
635 and AFTER_STALL_P are the corresponding fields of the second fence. */
e855c69d
AB
636static void
637merge_fences (fence_t f, insn_t insn,
b8698a0f 638 state_t state, deps_t dc, void *tc,
e855c69d
AB
639 rtx last_scheduled_insn, VEC(rtx, gc) *executing_insns,
640 int *ready_ticks, int ready_ticks_size,
136e01a3 641 rtx sched_next, int cycle, int issue_more, bool after_stall_p)
e855c69d
AB
642{
643 insn_t last_scheduled_insn_old = FENCE_LAST_SCHEDULED_INSN (f);
644
645 gcc_assert (sel_bb_head_p (FENCE_INSN (f))
646 && !sched_next && !FENCE_SCHED_NEXT (f));
647
b8698a0f 648 /* Check if we can decide which path fences came.
e855c69d
AB
649 If we can't (or don't want to) - reset all. */
650 if (last_scheduled_insn == NULL
651 || last_scheduled_insn_old == NULL
b8698a0f
L
652 /* This is a case when INSN is reachable on several paths from
653 one insn (this can happen when pipelining of outer loops is on and
654 there are two edges: one going around of inner loop and the other -
e855c69d
AB
655 right through it; in such case just reset everything). */
656 || last_scheduled_insn == last_scheduled_insn_old)
657 {
658 state_reset (FENCE_STATE (f));
659 state_free (state);
b8698a0f 660
e855c69d
AB
661 reset_deps_context (FENCE_DC (f));
662 delete_deps_context (dc);
b8698a0f 663
e855c69d
AB
664 reset_target_context (FENCE_TC (f), true);
665 delete_target_context (tc);
666
667 if (cycle > FENCE_CYCLE (f))
668 FENCE_CYCLE (f) = cycle;
669
670 FENCE_LAST_SCHEDULED_INSN (f) = NULL;
136e01a3 671 FENCE_ISSUE_MORE (f) = issue_rate;
e855c69d
AB
672 VEC_free (rtx, gc, executing_insns);
673 free (ready_ticks);
674 if (FENCE_EXECUTING_INSNS (f))
b8698a0f 675 VEC_block_remove (rtx, FENCE_EXECUTING_INSNS (f), 0,
e855c69d
AB
676 VEC_length (rtx, FENCE_EXECUTING_INSNS (f)));
677 if (FENCE_READY_TICKS (f))
678 memset (FENCE_READY_TICKS (f), 0, FENCE_READY_TICKS_SIZE (f));
679 }
680 else
681 {
682 edge edge_old = NULL, edge_new = NULL;
683 edge candidate;
684 succ_iterator si;
685 insn_t succ;
b8698a0f 686
e855c69d
AB
687 /* Find fallthrough edge. */
688 gcc_assert (BLOCK_FOR_INSN (insn)->prev_bb);
0fd4b31d 689 candidate = find_fallthru_edge_from (BLOCK_FOR_INSN (insn)->prev_bb);
e855c69d
AB
690
691 if (!candidate
692 || (candidate->src != BLOCK_FOR_INSN (last_scheduled_insn)
693 && candidate->src != BLOCK_FOR_INSN (last_scheduled_insn_old)))
694 {
695 /* No fallthrough edge leading to basic block of INSN. */
696 state_reset (FENCE_STATE (f));
697 state_free (state);
b8698a0f 698
e855c69d
AB
699 reset_target_context (FENCE_TC (f), true);
700 delete_target_context (tc);
b8698a0f 701
e855c69d 702 FENCE_LAST_SCHEDULED_INSN (f) = NULL;
136e01a3 703 FENCE_ISSUE_MORE (f) = issue_rate;
e855c69d
AB
704 }
705 else
706 if (candidate->src == BLOCK_FOR_INSN (last_scheduled_insn))
707 {
b8698a0f 708 /* Would be weird if same insn is successor of several fallthrough
e855c69d
AB
709 edges. */
710 gcc_assert (BLOCK_FOR_INSN (insn)->prev_bb
711 != BLOCK_FOR_INSN (last_scheduled_insn_old));
712
713 state_free (FENCE_STATE (f));
714 FENCE_STATE (f) = state;
715
716 delete_target_context (FENCE_TC (f));
717 FENCE_TC (f) = tc;
718
719 FENCE_LAST_SCHEDULED_INSN (f) = last_scheduled_insn;
136e01a3 720 FENCE_ISSUE_MORE (f) = issue_more;
e855c69d
AB
721 }
722 else
723 {
724 /* Leave STATE, TC and LAST_SCHEDULED_INSN fields untouched. */
725 state_free (state);
726 delete_target_context (tc);
727
728 gcc_assert (BLOCK_FOR_INSN (insn)->prev_bb
729 != BLOCK_FOR_INSN (last_scheduled_insn));
730 }
731
732 /* Find edge of first predecessor (last_scheduled_insn_old->insn). */
733 FOR_EACH_SUCC_1 (succ, si, last_scheduled_insn_old,
734 SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
735 {
736 if (succ == insn)
737 {
738 /* No same successor allowed from several edges. */
739 gcc_assert (!edge_old);
740 edge_old = si.e1;
741 }
742 }
743 /* Find edge of second predecessor (last_scheduled_insn->insn). */
744 FOR_EACH_SUCC_1 (succ, si, last_scheduled_insn,
745 SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
746 {
747 if (succ == insn)
748 {
749 /* No same successor allowed from several edges. */
750 gcc_assert (!edge_new);
751 edge_new = si.e1;
752 }
753 }
754
755 /* Check if we can choose most probable predecessor. */
756 if (edge_old == NULL || edge_new == NULL)
757 {
758 reset_deps_context (FENCE_DC (f));
759 delete_deps_context (dc);
760 VEC_free (rtx, gc, executing_insns);
761 free (ready_ticks);
b8698a0f 762
e855c69d
AB
763 FENCE_CYCLE (f) = MAX (FENCE_CYCLE (f), cycle);
764 if (FENCE_EXECUTING_INSNS (f))
b8698a0f 765 VEC_block_remove (rtx, FENCE_EXECUTING_INSNS (f), 0,
e855c69d
AB
766 VEC_length (rtx, FENCE_EXECUTING_INSNS (f)));
767 if (FENCE_READY_TICKS (f))
768 memset (FENCE_READY_TICKS (f), 0, FENCE_READY_TICKS_SIZE (f));
769 }
770 else
771 if (edge_new->probability > edge_old->probability)
772 {
773 delete_deps_context (FENCE_DC (f));
774 FENCE_DC (f) = dc;
775 VEC_free (rtx, gc, FENCE_EXECUTING_INSNS (f));
776 FENCE_EXECUTING_INSNS (f) = executing_insns;
777 free (FENCE_READY_TICKS (f));
778 FENCE_READY_TICKS (f) = ready_ticks;
779 FENCE_READY_TICKS_SIZE (f) = ready_ticks_size;
780 FENCE_CYCLE (f) = cycle;
781 }
782 else
783 {
784 /* Leave DC and CYCLE untouched. */
785 delete_deps_context (dc);
786 VEC_free (rtx, gc, executing_insns);
787 free (ready_ticks);
788 }
789 }
790
791 /* Fill remaining invariant fields. */
792 if (after_stall_p)
793 FENCE_AFTER_STALL_P (f) = 1;
794
795 FENCE_ISSUED_INSNS (f) = 0;
796 FENCE_STARTS_CYCLE_P (f) = 1;
797 FENCE_SCHED_NEXT (f) = NULL;
798}
799
b8698a0f 800/* Add a new fence to NEW_FENCES list, initializing it from all
e855c69d
AB
801 other parameters. */
802static void
803add_to_fences (flist_tail_t new_fences, insn_t insn,
b8698a0f
L
804 state_t state, deps_t dc, void *tc, rtx last_scheduled_insn,
805 VEC(rtx, gc) *executing_insns, int *ready_ticks,
806 int ready_ticks_size, rtx sched_next, int cycle,
136e01a3
AB
807 int cycle_issued_insns, int issue_rate,
808 bool starts_cycle_p, bool after_stall_p)
e855c69d
AB
809{
810 fence_t f = flist_lookup (FLIST_TAIL_HEAD (new_fences), insn);
811
812 if (! f)
813 {
814 flist_add (FLIST_TAIL_TAILP (new_fences), insn, state, dc, tc,
b8698a0f 815 last_scheduled_insn, executing_insns, ready_ticks,
e855c69d 816 ready_ticks_size, sched_next, cycle, cycle_issued_insns,
136e01a3 817 issue_rate, starts_cycle_p, after_stall_p);
e855c69d
AB
818
819 FLIST_TAIL_TAILP (new_fences)
820 = &FLIST_NEXT (*FLIST_TAIL_TAILP (new_fences));
821 }
822 else
823 {
b8698a0f
L
824 merge_fences (f, insn, state, dc, tc, last_scheduled_insn,
825 executing_insns, ready_ticks, ready_ticks_size,
136e01a3 826 sched_next, cycle, issue_rate, after_stall_p);
e855c69d
AB
827 }
828}
829
830/* Move the first fence in the OLD_FENCES list to NEW_FENCES. */
831void
832move_fence_to_fences (flist_t old_fences, flist_tail_t new_fences)
833{
834 fence_t f, old;
835 flist_t *tailp = FLIST_TAIL_TAILP (new_fences);
836
837 old = FLIST_FENCE (old_fences);
b8698a0f 838 f = flist_lookup (FLIST_TAIL_HEAD (new_fences),
e855c69d
AB
839 FENCE_INSN (FLIST_FENCE (old_fences)));
840 if (f)
841 {
842 merge_fences (f, old->insn, old->state, old->dc, old->tc,
843 old->last_scheduled_insn, old->executing_insns,
844 old->ready_ticks, old->ready_ticks_size,
136e01a3 845 old->sched_next, old->cycle, old->issue_more,
e855c69d
AB
846 old->after_stall_p);
847 }
848 else
849 {
850 _list_add (tailp);
851 FLIST_TAIL_TAILP (new_fences) = &FLIST_NEXT (*tailp);
852 *FLIST_FENCE (*tailp) = *old;
853 init_fence_for_scheduling (FLIST_FENCE (*tailp));
854 }
855 FENCE_INSN (old) = NULL;
856}
857
b8698a0f 858/* Add a new fence to NEW_FENCES list and initialize most of its data
e855c69d
AB
859 as a clean one. */
860void
861add_clean_fence_to_fences (flist_tail_t new_fences, insn_t succ, fence_t fence)
862{
863 int ready_ticks_size = get_max_uid () + 1;
b8698a0f 864
e855c69d
AB
865 add_to_fences (new_fences,
866 succ, state_create (), create_deps_context (),
867 create_target_context (true),
b8698a0f 868 NULL_RTX, NULL,
e855c69d
AB
869 XCNEWVEC (int, ready_ticks_size), ready_ticks_size,
870 NULL_RTX, FENCE_CYCLE (fence) + 1,
136e01a3 871 0, issue_rate, 1, FENCE_AFTER_STALL_P (fence));
e855c69d
AB
872}
873
b8698a0f 874/* Add a new fence to NEW_FENCES list and initialize all of its data
e855c69d
AB
875 from FENCE and SUCC. */
876void
877add_dirty_fence_to_fences (flist_tail_t new_fences, insn_t succ, fence_t fence)
878{
b8698a0f 879 int * new_ready_ticks
e855c69d 880 = XNEWVEC (int, FENCE_READY_TICKS_SIZE (fence));
b8698a0f 881
e855c69d
AB
882 memcpy (new_ready_ticks, FENCE_READY_TICKS (fence),
883 FENCE_READY_TICKS_SIZE (fence) * sizeof (int));
884 add_to_fences (new_fences,
885 succ, state_create_copy (FENCE_STATE (fence)),
886 create_copy_of_deps_context (FENCE_DC (fence)),
887 create_copy_of_target_context (FENCE_TC (fence)),
b8698a0f 888 FENCE_LAST_SCHEDULED_INSN (fence),
e855c69d
AB
889 VEC_copy (rtx, gc, FENCE_EXECUTING_INSNS (fence)),
890 new_ready_ticks,
891 FENCE_READY_TICKS_SIZE (fence),
892 FENCE_SCHED_NEXT (fence),
893 FENCE_CYCLE (fence),
894 FENCE_ISSUED_INSNS (fence),
136e01a3 895 FENCE_ISSUE_MORE (fence),
e855c69d
AB
896 FENCE_STARTS_CYCLE_P (fence),
897 FENCE_AFTER_STALL_P (fence));
898}
899\f
900
901/* Functions to work with regset and nop pools. */
902
903/* Returns the new regset from pool. It might have some of the bits set
904 from the previous usage. */
905regset
906get_regset_from_pool (void)
907{
908 regset rs;
909
910 if (regset_pool.n != 0)
911 rs = regset_pool.v[--regset_pool.n];
912 else
913 /* We need to create the regset. */
914 {
915 rs = ALLOC_REG_SET (&reg_obstack);
916
917 if (regset_pool.nn == regset_pool.ss)
918 regset_pool.vv = XRESIZEVEC (regset, regset_pool.vv,
919 (regset_pool.ss = 2 * regset_pool.ss + 1));
920 regset_pool.vv[regset_pool.nn++] = rs;
921 }
922
923 regset_pool.diff++;
924
925 return rs;
926}
927
928/* Same as above, but returns the empty regset. */
929regset
930get_clear_regset_from_pool (void)
931{
932 regset rs = get_regset_from_pool ();
933
934 CLEAR_REG_SET (rs);
935 return rs;
936}
937
938/* Return regset RS to the pool for future use. */
939void
940return_regset_to_pool (regset rs)
941{
9ef1bf71 942 gcc_assert (rs);
e855c69d
AB
943 regset_pool.diff--;
944
945 if (regset_pool.n == regset_pool.s)
946 regset_pool.v = XRESIZEVEC (regset, regset_pool.v,
947 (regset_pool.s = 2 * regset_pool.s + 1));
948 regset_pool.v[regset_pool.n++] = rs;
949}
950
68ad446f 951#ifdef ENABLE_CHECKING
e855c69d
AB
952/* This is used as a qsort callback for sorting regset pool stacks.
953 X and XX are addresses of two regsets. They are never equal. */
954static int
955cmp_v_in_regset_pool (const void *x, const void *xx)
956{
d38933a0
SB
957 uintptr_t r1 = (uintptr_t) *((const regset *) x);
958 uintptr_t r2 = (uintptr_t) *((const regset *) xx);
959 if (r1 > r2)
960 return 1;
961 else if (r1 < r2)
962 return -1;
963 gcc_unreachable ();
e855c69d 964}
68ad446f 965#endif
e855c69d
AB
966
967/* Free the regset pool possibly checking for memory leaks. */
968void
969free_regset_pool (void)
970{
971#ifdef ENABLE_CHECKING
972 {
973 regset *v = regset_pool.v;
974 int i = 0;
975 int n = regset_pool.n;
b8698a0f 976
e855c69d
AB
977 regset *vv = regset_pool.vv;
978 int ii = 0;
979 int nn = regset_pool.nn;
b8698a0f 980
e855c69d 981 int diff = 0;
b8698a0f 982
e855c69d 983 gcc_assert (n <= nn);
b8698a0f 984
e855c69d
AB
985 /* Sort both vectors so it will be possible to compare them. */
986 qsort (v, n, sizeof (*v), cmp_v_in_regset_pool);
987 qsort (vv, nn, sizeof (*vv), cmp_v_in_regset_pool);
b8698a0f 988
e855c69d
AB
989 while (ii < nn)
990 {
991 if (v[i] == vv[ii])
992 i++;
993 else
994 /* VV[II] was lost. */
995 diff++;
b8698a0f 996
e855c69d
AB
997 ii++;
998 }
b8698a0f 999
e855c69d
AB
1000 gcc_assert (diff == regset_pool.diff);
1001 }
1002#endif
b8698a0f 1003
e855c69d
AB
1004 /* If not true - we have a memory leak. */
1005 gcc_assert (regset_pool.diff == 0);
b8698a0f 1006
e855c69d
AB
1007 while (regset_pool.n)
1008 {
1009 --regset_pool.n;
1010 FREE_REG_SET (regset_pool.v[regset_pool.n]);
1011 }
1012
1013 free (regset_pool.v);
1014 regset_pool.v = NULL;
1015 regset_pool.s = 0;
b8698a0f 1016
e855c69d
AB
1017 free (regset_pool.vv);
1018 regset_pool.vv = NULL;
1019 regset_pool.nn = 0;
1020 regset_pool.ss = 0;
1021
1022 regset_pool.diff = 0;
1023}
1024\f
1025
b8698a0f
L
1026/* Functions to work with nop pools. NOP insns are used as temporary
1027 placeholders of the insns being scheduled to allow correct update of
e855c69d
AB
1028 the data sets. When update is finished, NOPs are deleted. */
1029
1030/* A vinsn that is used to represent a nop. This vinsn is shared among all
1031 nops sel-sched generates. */
1032static vinsn_t nop_vinsn = NULL;
1033
1034/* Emit a nop before INSN, taking it from pool. */
1035insn_t
1036get_nop_from_pool (insn_t insn)
1037{
1038 insn_t nop;
1039 bool old_p = nop_pool.n != 0;
1040 int flags;
1041
1042 if (old_p)
1043 nop = nop_pool.v[--nop_pool.n];
1044 else
1045 nop = nop_pattern;
1046
1047 nop = emit_insn_before (nop, insn);
1048
1049 if (old_p)
1050 flags = INSN_INIT_TODO_SSID;
1051 else
1052 flags = INSN_INIT_TODO_LUID | INSN_INIT_TODO_SSID;
1053
1054 set_insn_init (INSN_EXPR (insn), nop_vinsn, INSN_SEQNO (insn));
1055 sel_init_new_insn (nop, flags);
1056
1057 return nop;
1058}
1059
1060/* Remove NOP from the instruction stream and return it to the pool. */
1061void
b5b8b0ac 1062return_nop_to_pool (insn_t nop, bool full_tidying)
e855c69d
AB
1063{
1064 gcc_assert (INSN_IN_STREAM_P (nop));
b5b8b0ac 1065 sel_remove_insn (nop, false, full_tidying);
e855c69d
AB
1066
1067 if (nop_pool.n == nop_pool.s)
b8698a0f 1068 nop_pool.v = XRESIZEVEC (rtx, nop_pool.v,
e855c69d
AB
1069 (nop_pool.s = 2 * nop_pool.s + 1));
1070 nop_pool.v[nop_pool.n++] = nop;
1071}
1072
1073/* Free the nop pool. */
1074void
1075free_nop_pool (void)
1076{
1077 nop_pool.n = 0;
1078 nop_pool.s = 0;
1079 free (nop_pool.v);
1080 nop_pool.v = NULL;
1081}
1082\f
1083
b8698a0f 1084/* Skip unspec to support ia64 speculation. Called from rtx_equal_p_cb.
e855c69d
AB
1085 The callback is given two rtxes XX and YY and writes the new rtxes
1086 to NX and NY in case some needs to be skipped. */
1087static int
1088skip_unspecs_callback (const_rtx *xx, const_rtx *yy, rtx *nx, rtx* ny)
1089{
1090 const_rtx x = *xx;
1091 const_rtx y = *yy;
b8698a0f 1092
e855c69d
AB
1093 if (GET_CODE (x) == UNSPEC
1094 && (targetm.sched.skip_rtx_p == NULL
1095 || targetm.sched.skip_rtx_p (x)))
1096 {
1097 *nx = XVECEXP (x, 0, 0);
1098 *ny = CONST_CAST_RTX (y);
1099 return 1;
1100 }
b8698a0f 1101
e855c69d
AB
1102 if (GET_CODE (y) == UNSPEC
1103 && (targetm.sched.skip_rtx_p == NULL
1104 || targetm.sched.skip_rtx_p (y)))
1105 {
1106 *nx = CONST_CAST_RTX (x);
1107 *ny = XVECEXP (y, 0, 0);
1108 return 1;
1109 }
b8698a0f 1110
e855c69d
AB
1111 return 0;
1112}
1113
b8698a0f 1114/* Callback, called from hash_rtx_cb. Helps to hash UNSPEC rtx X in a correct way
e855c69d
AB
1115 to support ia64 speculation. When changes are needed, new rtx X and new mode
1116 NMODE are written, and the callback returns true. */
1117static int
1118hash_with_unspec_callback (const_rtx x, enum machine_mode mode ATTRIBUTE_UNUSED,
1119 rtx *nx, enum machine_mode* nmode)
1120{
b8698a0f 1121 if (GET_CODE (x) == UNSPEC
e855c69d
AB
1122 && targetm.sched.skip_rtx_p
1123 && targetm.sched.skip_rtx_p (x))
1124 {
1125 *nx = XVECEXP (x, 0 ,0);
32e8bb8e 1126 *nmode = VOIDmode;
e855c69d
AB
1127 return 1;
1128 }
b8698a0f 1129
e855c69d
AB
1130 return 0;
1131}
1132
1133/* Returns LHS and RHS are ok to be scheduled separately. */
1134static bool
1135lhs_and_rhs_separable_p (rtx lhs, rtx rhs)
1136{
1137 if (lhs == NULL || rhs == NULL)
1138 return false;
1139
b8698a0f
L
1140 /* Do not schedule CONST, CONST_INT and CONST_DOUBLE etc as rhs: no point
1141 to use reg, if const can be used. Moreover, scheduling const as rhs may
1142 lead to mode mismatch cause consts don't have modes but they could be
e855c69d
AB
1143 merged from branches where the same const used in different modes. */
1144 if (CONSTANT_P (rhs))
1145 return false;
1146
1147 /* ??? Do not rename predicate registers to avoid ICEs in bundling. */
1148 if (COMPARISON_P (rhs))
1149 return false;
1150
1151 /* Do not allow single REG to be an rhs. */
1152 if (REG_P (rhs))
1153 return false;
1154
b8698a0f 1155 /* See comment at find_used_regs_1 (*1) for explanation of this
e855c69d
AB
1156 restriction. */
1157 /* FIXME: remove this later. */
1158 if (MEM_P (lhs))
1159 return false;
1160
1161 /* This will filter all tricky things like ZERO_EXTRACT etc.
1162 For now we don't handle it. */
1163 if (!REG_P (lhs) && !MEM_P (lhs))
1164 return false;
1165
1166 return true;
1167}
1168
b8698a0f
L
1169/* Initialize vinsn VI for INSN. Only for use from vinsn_create (). When
1170 FORCE_UNIQUE_P is true, the resulting vinsn will not be clonable. This is
e855c69d
AB
1171 used e.g. for insns from recovery blocks. */
1172static void
1173vinsn_init (vinsn_t vi, insn_t insn, bool force_unique_p)
1174{
1175 hash_rtx_callback_function hrcf;
1176 int insn_class;
1177
1178 VINSN_INSN_RTX (vi) = insn;
1179 VINSN_COUNT (vi) = 0;
1180 vi->cost = -1;
b8698a0f 1181
9ef1bf71
AM
1182 if (INSN_NOP_P (insn))
1183 return;
1184
e855c69d
AB
1185 if (DF_INSN_UID_SAFE_GET (INSN_UID (insn)) != NULL)
1186 init_id_from_df (VINSN_ID (vi), insn, force_unique_p);
1187 else
1188 deps_init_id (VINSN_ID (vi), insn, force_unique_p);
b8698a0f 1189
e855c69d
AB
1190 /* Hash vinsn depending on whether it is separable or not. */
1191 hrcf = targetm.sched.skip_rtx_p ? hash_with_unspec_callback : NULL;
1192 if (VINSN_SEPARABLE_P (vi))
1193 {
1194 rtx rhs = VINSN_RHS (vi);
1195
1196 VINSN_HASH (vi) = hash_rtx_cb (rhs, GET_MODE (rhs),
1197 NULL, NULL, false, hrcf);
1198 VINSN_HASH_RTX (vi) = hash_rtx_cb (VINSN_PATTERN (vi),
1199 VOIDmode, NULL, NULL,
1200 false, hrcf);
1201 }
1202 else
1203 {
1204 VINSN_HASH (vi) = hash_rtx_cb (VINSN_PATTERN (vi), VOIDmode,
1205 NULL, NULL, false, hrcf);
1206 VINSN_HASH_RTX (vi) = VINSN_HASH (vi);
1207 }
b8698a0f 1208
e855c69d
AB
1209 insn_class = haifa_classify_insn (insn);
1210 if (insn_class >= 2
1211 && (!targetm.sched.get_insn_spec_ds
1212 || ((targetm.sched.get_insn_spec_ds (insn) & BEGIN_CONTROL)
1213 == 0)))
1214 VINSN_MAY_TRAP_P (vi) = true;
1215 else
1216 VINSN_MAY_TRAP_P (vi) = false;
1217}
1218
1219/* Indicate that VI has become the part of an rtx object. */
1220void
1221vinsn_attach (vinsn_t vi)
1222{
1223 /* Assert that VI is not pending for deletion. */
1224 gcc_assert (VINSN_INSN_RTX (vi));
1225
1226 VINSN_COUNT (vi)++;
1227}
1228
b8698a0f 1229/* Create and init VI from the INSN. Use UNIQUE_P for determining the correct
e855c69d
AB
1230 VINSN_TYPE (VI). */
1231static vinsn_t
1232vinsn_create (insn_t insn, bool force_unique_p)
1233{
1234 vinsn_t vi = XCNEW (struct vinsn_def);
1235
1236 vinsn_init (vi, insn, force_unique_p);
1237 return vi;
1238}
1239
1240/* Return a copy of VI. When REATTACH_P is true, detach VI and attach
1241 the copy. */
b8698a0f 1242vinsn_t
e855c69d
AB
1243vinsn_copy (vinsn_t vi, bool reattach_p)
1244{
1245 rtx copy;
1246 bool unique = VINSN_UNIQUE_P (vi);
1247 vinsn_t new_vi;
b8698a0f 1248
e855c69d
AB
1249 copy = create_copy_of_insn_rtx (VINSN_INSN_RTX (vi));
1250 new_vi = create_vinsn_from_insn_rtx (copy, unique);
1251 if (reattach_p)
1252 {
1253 vinsn_detach (vi);
1254 vinsn_attach (new_vi);
1255 }
1256
1257 return new_vi;
1258}
1259
1260/* Delete the VI vinsn and free its data. */
1261static void
1262vinsn_delete (vinsn_t vi)
1263{
1264 gcc_assert (VINSN_COUNT (vi) == 0);
1265
9ef1bf71
AM
1266 if (!INSN_NOP_P (VINSN_INSN_RTX (vi)))
1267 {
1268 return_regset_to_pool (VINSN_REG_SETS (vi));
1269 return_regset_to_pool (VINSN_REG_USES (vi));
1270 return_regset_to_pool (VINSN_REG_CLOBBERS (vi));
1271 }
e855c69d
AB
1272
1273 free (vi);
1274}
1275
b8698a0f 1276/* Indicate that VI is no longer a part of some rtx object.
e855c69d
AB
1277 Remove VI if it is no longer needed. */
1278void
1279vinsn_detach (vinsn_t vi)
1280{
1281 gcc_assert (VINSN_COUNT (vi) > 0);
1282
1283 if (--VINSN_COUNT (vi) == 0)
1284 vinsn_delete (vi);
1285}
1286
1287/* Returns TRUE if VI is a branch. */
1288bool
1289vinsn_cond_branch_p (vinsn_t vi)
1290{
1291 insn_t insn;
1292
1293 if (!VINSN_UNIQUE_P (vi))
1294 return false;
1295
1296 insn = VINSN_INSN_RTX (vi);
1297 if (BB_END (BLOCK_FOR_INSN (insn)) != insn)
1298 return false;
1299
1300 return control_flow_insn_p (insn);
1301}
1302
1303/* Return latency of INSN. */
1304static int
1305sel_insn_rtx_cost (rtx insn)
1306{
1307 int cost;
1308
1309 /* A USE insn, or something else we don't need to
1310 understand. We can't pass these directly to
1311 result_ready_cost or insn_default_latency because it will
1312 trigger a fatal error for unrecognizable insns. */
1313 if (recog_memoized (insn) < 0)
1314 cost = 0;
1315 else
1316 {
1317 cost = insn_default_latency (insn);
1318
1319 if (cost < 0)
1320 cost = 0;
1321 }
1322
1323 return cost;
1324}
1325
1326/* Return the cost of the VI.
1327 !!! FIXME: Unify with haifa-sched.c: insn_cost (). */
1328int
1329sel_vinsn_cost (vinsn_t vi)
1330{
1331 int cost = vi->cost;
1332
1333 if (cost < 0)
1334 {
1335 cost = sel_insn_rtx_cost (VINSN_INSN_RTX (vi));
1336 vi->cost = cost;
1337 }
1338
1339 return cost;
1340}
1341\f
1342
1343/* Functions for insn emitting. */
1344
1345/* Emit new insn after AFTER based on PATTERN and initialize its data from
1346 EXPR and SEQNO. */
1347insn_t
1348sel_gen_insn_from_rtx_after (rtx pattern, expr_t expr, int seqno, insn_t after)
1349{
1350 insn_t new_insn;
1351
1352 gcc_assert (EXPR_TARGET_AVAILABLE (expr) == true);
1353
1354 new_insn = emit_insn_after (pattern, after);
1355 set_insn_init (expr, NULL, seqno);
1356 sel_init_new_insn (new_insn, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SSID);
1357
1358 return new_insn;
1359}
1360
1361/* Force newly generated vinsns to be unique. */
1362static bool init_insn_force_unique_p = false;
1363
1364/* Emit new speculation recovery insn after AFTER based on PATTERN and
1365 initialize its data from EXPR and SEQNO. */
1366insn_t
1367sel_gen_recovery_insn_from_rtx_after (rtx pattern, expr_t expr, int seqno,
1368 insn_t after)
1369{
1370 insn_t insn;
1371
1372 gcc_assert (!init_insn_force_unique_p);
1373
1374 init_insn_force_unique_p = true;
1375 insn = sel_gen_insn_from_rtx_after (pattern, expr, seqno, after);
1376 CANT_MOVE (insn) = 1;
1377 init_insn_force_unique_p = false;
1378
1379 return insn;
1380}
1381
1382/* Emit new insn after AFTER based on EXPR and SEQNO. If VINSN is not NULL,
b8698a0f
L
1383 take it as a new vinsn instead of EXPR's vinsn.
1384 We simplify insns later, after scheduling region in
e855c69d
AB
1385 simplify_changed_insns. */
1386insn_t
b8698a0f 1387sel_gen_insn_from_expr_after (expr_t expr, vinsn_t vinsn, int seqno,
e855c69d
AB
1388 insn_t after)
1389{
1390 expr_t emit_expr;
1391 insn_t insn;
1392 int flags;
b8698a0f
L
1393
1394 emit_expr = set_insn_init (expr, vinsn ? vinsn : EXPR_VINSN (expr),
e855c69d
AB
1395 seqno);
1396 insn = EXPR_INSN_RTX (emit_expr);
b8698a0f 1397 add_insn_after (insn, after, BLOCK_FOR_INSN (insn));
e855c69d
AB
1398
1399 flags = INSN_INIT_TODO_SSID;
1400 if (INSN_LUID (insn) == 0)
1401 flags |= INSN_INIT_TODO_LUID;
1402 sel_init_new_insn (insn, flags);
1403
1404 return insn;
1405}
1406
1407/* Move insn from EXPR after AFTER. */
1408insn_t
1409sel_move_insn (expr_t expr, int seqno, insn_t after)
1410{
1411 insn_t insn = EXPR_INSN_RTX (expr);
1412 basic_block bb = BLOCK_FOR_INSN (after);
1413 insn_t next = NEXT_INSN (after);
1414
1415 /* Assert that in move_op we disconnected this insn properly. */
1416 gcc_assert (EXPR_VINSN (INSN_EXPR (insn)) != NULL);
1417 PREV_INSN (insn) = after;
1418 NEXT_INSN (insn) = next;
1419
1420 NEXT_INSN (after) = insn;
1421 PREV_INSN (next) = insn;
1422
1423 /* Update links from insn to bb and vice versa. */
1424 df_insn_change_bb (insn, bb);
1425 if (BB_END (bb) == after)
1426 BB_END (bb) = insn;
b8698a0f 1427
e855c69d
AB
1428 prepare_insn_expr (insn, seqno);
1429 return insn;
1430}
1431
1432\f
1433/* Functions to work with right-hand sides. */
1434
b8698a0f 1435/* Search for a hash value determined by UID/NEW_VINSN in a sorted vector
e855c69d 1436 VECT and return true when found. Use NEW_VINSN for comparison only when
b8698a0f
L
1437 COMPARE_VINSNS is true. Write to INDP the index on which
1438 the search has stopped, such that inserting the new element at INDP will
e855c69d
AB
1439 retain VECT's sort order. */
1440static bool
b8698a0f
L
1441find_in_history_vect_1 (VEC(expr_history_def, heap) *vect,
1442 unsigned uid, vinsn_t new_vinsn,
e855c69d
AB
1443 bool compare_vinsns, int *indp)
1444{
1445 expr_history_def *arr;
1446 int i, j, len = VEC_length (expr_history_def, vect);
1447
1448 if (len == 0)
1449 {
1450 *indp = 0;
1451 return false;
1452 }
1453
1454 arr = VEC_address (expr_history_def, vect);
1455 i = 0, j = len - 1;
1456
1457 while (i <= j)
1458 {
1459 unsigned auid = arr[i].uid;
b8698a0f 1460 vinsn_t avinsn = arr[i].new_expr_vinsn;
e855c69d
AB
1461
1462 if (auid == uid
b8698a0f
L
1463 /* When undoing transformation on a bookkeeping copy, the new vinsn
1464 may not be exactly equal to the one that is saved in the vector.
e855c69d
AB
1465 This is because the insn whose copy we're checking was possibly
1466 substituted itself. */
b8698a0f 1467 && (! compare_vinsns
e855c69d
AB
1468 || vinsn_equal_p (avinsn, new_vinsn)))
1469 {
1470 *indp = i;
1471 return true;
1472 }
1473 else if (auid > uid)
1474 break;
1475 i++;
1476 }
1477
1478 *indp = i;
1479 return false;
1480}
1481
b8698a0f
L
1482/* Search for a uid of INSN and NEW_VINSN in a sorted vector VECT. Return
1483 the position found or -1, if no such value is in vector.
e855c69d
AB
1484 Search also for UIDs of insn's originators, if ORIGINATORS_P is true. */
1485int
b8698a0f 1486find_in_history_vect (VEC(expr_history_def, heap) *vect, rtx insn,
e855c69d
AB
1487 vinsn_t new_vinsn, bool originators_p)
1488{
1489 int ind;
1490
b8698a0f 1491 if (find_in_history_vect_1 (vect, INSN_UID (insn), new_vinsn,
e855c69d
AB
1492 false, &ind))
1493 return ind;
1494
1495 if (INSN_ORIGINATORS (insn) && originators_p)
1496 {
1497 unsigned uid;
1498 bitmap_iterator bi;
1499
1500 EXECUTE_IF_SET_IN_BITMAP (INSN_ORIGINATORS (insn), 0, uid, bi)
1501 if (find_in_history_vect_1 (vect, uid, new_vinsn, false, &ind))
1502 return ind;
1503 }
b8698a0f 1504
e855c69d
AB
1505 return -1;
1506}
1507
b8698a0f
L
1508/* Insert new element in a sorted history vector pointed to by PVECT,
1509 if it is not there already. The element is searched using
e855c69d
AB
1510 UID/NEW_EXPR_VINSN pair. TYPE, OLD_EXPR_VINSN and SPEC_DS save
1511 the history of a transformation. */
1512void
1513insert_in_history_vect (VEC (expr_history_def, heap) **pvect,
1514 unsigned uid, enum local_trans_type type,
b8698a0f 1515 vinsn_t old_expr_vinsn, vinsn_t new_expr_vinsn,
e855c69d
AB
1516 ds_t spec_ds)
1517{
1518 VEC(expr_history_def, heap) *vect = *pvect;
1519 expr_history_def temp;
1520 bool res;
1521 int ind;
1522
1523 res = find_in_history_vect_1 (vect, uid, new_expr_vinsn, true, &ind);
1524
1525 if (res)
1526 {
0823efed 1527 expr_history_def *phist = &VEC_index (expr_history_def, vect, ind);
e855c69d 1528
b8698a0f 1529 /* It is possible that speculation types of expressions that were
e855c69d
AB
1530 propagated through different paths will be different here. In this
1531 case, merge the status to get the correct check later. */
1532 if (phist->spec_ds != spec_ds)
1533 phist->spec_ds = ds_max_merge (phist->spec_ds, spec_ds);
1534 return;
1535 }
b8698a0f 1536
e855c69d
AB
1537 temp.uid = uid;
1538 temp.old_expr_vinsn = old_expr_vinsn;
b8698a0f 1539 temp.new_expr_vinsn = new_expr_vinsn;
e855c69d
AB
1540 temp.spec_ds = spec_ds;
1541 temp.type = type;
1542
1543 vinsn_attach (old_expr_vinsn);
1544 vinsn_attach (new_expr_vinsn);
1545 VEC_safe_insert (expr_history_def, heap, vect, ind, &temp);
1546 *pvect = vect;
1547}
1548
1549/* Free history vector PVECT. */
1550static void
1551free_history_vect (VEC (expr_history_def, heap) **pvect)
1552{
1553 unsigned i;
1554 expr_history_def *phist;
1555
1556 if (! *pvect)
1557 return;
b8698a0f
L
1558
1559 for (i = 0;
e855c69d
AB
1560 VEC_iterate (expr_history_def, *pvect, i, phist);
1561 i++)
1562 {
1563 vinsn_detach (phist->old_expr_vinsn);
1564 vinsn_detach (phist->new_expr_vinsn);
1565 }
b8698a0f 1566
e855c69d
AB
1567 VEC_free (expr_history_def, heap, *pvect);
1568 *pvect = NULL;
1569}
1570
5d369d58
AB
1571/* Merge vector FROM to PVECT. */
1572static void
1573merge_history_vect (VEC (expr_history_def, heap) **pvect,
1574 VEC (expr_history_def, heap) *from)
1575{
1576 expr_history_def *phist;
1577 int i;
1578
1579 /* We keep this vector sorted. */
1580 for (i = 0; VEC_iterate (expr_history_def, from, i, phist); i++)
1581 insert_in_history_vect (pvect, phist->uid, phist->type,
1582 phist->old_expr_vinsn, phist->new_expr_vinsn,
1583 phist->spec_ds);
1584}
e855c69d
AB
1585
1586/* Compare two vinsns as rhses if possible and as vinsns otherwise. */
1587bool
1588vinsn_equal_p (vinsn_t x, vinsn_t y)
1589{
1590 rtx_equal_p_callback_function repcf;
1591
1592 if (x == y)
1593 return true;
1594
1595 if (VINSN_TYPE (x) != VINSN_TYPE (y))
1596 return false;
1597
1598 if (VINSN_HASH (x) != VINSN_HASH (y))
1599 return false;
1600
1601 repcf = targetm.sched.skip_rtx_p ? skip_unspecs_callback : NULL;
b8698a0f 1602 if (VINSN_SEPARABLE_P (x))
e855c69d
AB
1603 {
1604 /* Compare RHSes of VINSNs. */
1605 gcc_assert (VINSN_RHS (x));
1606 gcc_assert (VINSN_RHS (y));
1607
1608 return rtx_equal_p_cb (VINSN_RHS (x), VINSN_RHS (y), repcf);
1609 }
1610
1611 return rtx_equal_p_cb (VINSN_PATTERN (x), VINSN_PATTERN (y), repcf);
1612}
1613\f
1614
1615/* Functions for working with expressions. */
1616
1617/* Initialize EXPR. */
1618static void
1619init_expr (expr_t expr, vinsn_t vi, int spec, int use, int priority,
1620 int sched_times, int orig_bb_index, ds_t spec_done_ds,
1621 ds_t spec_to_check_ds, int orig_sched_cycle,
f3764768 1622 VEC(expr_history_def, heap) *history, signed char target_available,
e855c69d
AB
1623 bool was_substituted, bool was_renamed, bool needs_spec_check_p,
1624 bool cant_move)
1625{
1626 vinsn_attach (vi);
1627
1628 EXPR_VINSN (expr) = vi;
1629 EXPR_SPEC (expr) = spec;
1630 EXPR_USEFULNESS (expr) = use;
1631 EXPR_PRIORITY (expr) = priority;
1632 EXPR_PRIORITY_ADJ (expr) = 0;
1633 EXPR_SCHED_TIMES (expr) = sched_times;
1634 EXPR_ORIG_BB_INDEX (expr) = orig_bb_index;
1635 EXPR_ORIG_SCHED_CYCLE (expr) = orig_sched_cycle;
1636 EXPR_SPEC_DONE_DS (expr) = spec_done_ds;
1637 EXPR_SPEC_TO_CHECK_DS (expr) = spec_to_check_ds;
1638
1639 if (history)
1640 EXPR_HISTORY_OF_CHANGES (expr) = history;
1641 else
1642 EXPR_HISTORY_OF_CHANGES (expr) = NULL;
1643
1644 EXPR_TARGET_AVAILABLE (expr) = target_available;
1645 EXPR_WAS_SUBSTITUTED (expr) = was_substituted;
1646 EXPR_WAS_RENAMED (expr) = was_renamed;
1647 EXPR_NEEDS_SPEC_CHECK_P (expr) = needs_spec_check_p;
1648 EXPR_CANT_MOVE (expr) = cant_move;
1649}
1650
1651/* Make a copy of the expr FROM into the expr TO. */
1652void
1653copy_expr (expr_t to, expr_t from)
1654{
1655 VEC(expr_history_def, heap) *temp = NULL;
1656
1657 if (EXPR_HISTORY_OF_CHANGES (from))
1658 {
1659 unsigned i;
1660 expr_history_def *phist;
1661
1662 temp = VEC_copy (expr_history_def, heap, EXPR_HISTORY_OF_CHANGES (from));
b8698a0f 1663 for (i = 0;
e855c69d
AB
1664 VEC_iterate (expr_history_def, temp, i, phist);
1665 i++)
1666 {
1667 vinsn_attach (phist->old_expr_vinsn);
1668 vinsn_attach (phist->new_expr_vinsn);
1669 }
1670 }
1671
b8698a0f 1672 init_expr (to, EXPR_VINSN (from), EXPR_SPEC (from),
e855c69d
AB
1673 EXPR_USEFULNESS (from), EXPR_PRIORITY (from),
1674 EXPR_SCHED_TIMES (from), EXPR_ORIG_BB_INDEX (from),
b8698a0f 1675 EXPR_SPEC_DONE_DS (from), EXPR_SPEC_TO_CHECK_DS (from),
e855c69d 1676 EXPR_ORIG_SCHED_CYCLE (from), temp,
b8698a0f 1677 EXPR_TARGET_AVAILABLE (from), EXPR_WAS_SUBSTITUTED (from),
e855c69d
AB
1678 EXPR_WAS_RENAMED (from), EXPR_NEEDS_SPEC_CHECK_P (from),
1679 EXPR_CANT_MOVE (from));
1680}
1681
b8698a0f 1682/* Same, but the final expr will not ever be in av sets, so don't copy
e855c69d
AB
1683 "uninteresting" data such as bitmap cache. */
1684void
1685copy_expr_onside (expr_t to, expr_t from)
1686{
1687 init_expr (to, EXPR_VINSN (from), EXPR_SPEC (from), EXPR_USEFULNESS (from),
1688 EXPR_PRIORITY (from), EXPR_SCHED_TIMES (from), 0,
1689 EXPR_SPEC_DONE_DS (from), EXPR_SPEC_TO_CHECK_DS (from), 0, NULL,
1690 EXPR_TARGET_AVAILABLE (from), EXPR_WAS_SUBSTITUTED (from),
1691 EXPR_WAS_RENAMED (from), EXPR_NEEDS_SPEC_CHECK_P (from),
1692 EXPR_CANT_MOVE (from));
1693}
1694
1695/* Prepare the expr of INSN for scheduling. Used when moving insn and when
1696 initializing new insns. */
1697static void
1698prepare_insn_expr (insn_t insn, int seqno)
1699{
1700 expr_t expr = INSN_EXPR (insn);
1701 ds_t ds;
b8698a0f 1702
e855c69d
AB
1703 INSN_SEQNO (insn) = seqno;
1704 EXPR_ORIG_BB_INDEX (expr) = BLOCK_NUM (insn);
1705 EXPR_SPEC (expr) = 0;
1706 EXPR_ORIG_SCHED_CYCLE (expr) = 0;
1707 EXPR_WAS_SUBSTITUTED (expr) = 0;
1708 EXPR_WAS_RENAMED (expr) = 0;
1709 EXPR_TARGET_AVAILABLE (expr) = 1;
1710 INSN_LIVE_VALID_P (insn) = false;
1711
1712 /* ??? If this expression is speculative, make its dependence
1713 as weak as possible. We can filter this expression later
1714 in process_spec_exprs, because we do not distinguish
1715 between the status we got during compute_av_set and the
1716 existing status. To be fixed. */
1717 ds = EXPR_SPEC_DONE_DS (expr);
1718 if (ds)
1719 EXPR_SPEC_DONE_DS (expr) = ds_get_max_dep_weak (ds);
1720
1721 free_history_vect (&EXPR_HISTORY_OF_CHANGES (expr));
1722}
1723
1724/* Update target_available bits when merging exprs TO and FROM. SPLIT_POINT
b8698a0f 1725 is non-null when expressions are merged from different successors at
e855c69d
AB
1726 a split point. */
1727static void
1728update_target_availability (expr_t to, expr_t from, insn_t split_point)
1729{
b8698a0f 1730 if (EXPR_TARGET_AVAILABLE (to) < 0
e855c69d
AB
1731 || EXPR_TARGET_AVAILABLE (from) < 0)
1732 EXPR_TARGET_AVAILABLE (to) = -1;
1733 else
1734 {
1735 /* We try to detect the case when one of the expressions
1736 can only be reached through another one. In this case,
1737 we can do better. */
1738 if (split_point == NULL)
1739 {
1740 int toind, fromind;
1741
1742 toind = EXPR_ORIG_BB_INDEX (to);
1743 fromind = EXPR_ORIG_BB_INDEX (from);
b8698a0f 1744
e855c69d 1745 if (toind && toind == fromind)
b8698a0f 1746 /* Do nothing -- everything is done in
e855c69d
AB
1747 merge_with_other_exprs. */
1748 ;
1749 else
1750 EXPR_TARGET_AVAILABLE (to) = -1;
1751 }
854b5fd7
AM
1752 else if (EXPR_TARGET_AVAILABLE (from) == 0
1753 && EXPR_LHS (from)
1754 && REG_P (EXPR_LHS (from))
1755 && REGNO (EXPR_LHS (to)) != REGNO (EXPR_LHS (from)))
1756 EXPR_TARGET_AVAILABLE (to) = -1;
e855c69d
AB
1757 else
1758 EXPR_TARGET_AVAILABLE (to) &= EXPR_TARGET_AVAILABLE (from);
1759 }
1760}
1761
1762/* Update speculation bits when merging exprs TO and FROM. SPLIT_POINT
b8698a0f 1763 is non-null when expressions are merged from different successors at
e855c69d
AB
1764 a split point. */
1765static void
1766update_speculative_bits (expr_t to, expr_t from, insn_t split_point)
1767{
1768 ds_t old_to_ds, old_from_ds;
1769
1770 old_to_ds = EXPR_SPEC_DONE_DS (to);
1771 old_from_ds = EXPR_SPEC_DONE_DS (from);
b8698a0f 1772
e855c69d
AB
1773 EXPR_SPEC_DONE_DS (to) = ds_max_merge (old_to_ds, old_from_ds);
1774 EXPR_SPEC_TO_CHECK_DS (to) |= EXPR_SPEC_TO_CHECK_DS (from);
1775 EXPR_NEEDS_SPEC_CHECK_P (to) |= EXPR_NEEDS_SPEC_CHECK_P (from);
1776
1777 /* When merging e.g. control & data speculative exprs, or a control
b8698a0f 1778 speculative with a control&data speculative one, we really have
e855c69d
AB
1779 to change vinsn too. Also, when speculative status is changed,
1780 we also need to record this as a transformation in expr's history. */
1781 if ((old_to_ds & SPECULATIVE) || (old_from_ds & SPECULATIVE))
1782 {
1783 old_to_ds = ds_get_speculation_types (old_to_ds);
1784 old_from_ds = ds_get_speculation_types (old_from_ds);
b8698a0f 1785
e855c69d
AB
1786 if (old_to_ds != old_from_ds)
1787 {
1788 ds_t record_ds;
b8698a0f
L
1789
1790 /* When both expressions are speculative, we need to change
e855c69d
AB
1791 the vinsn first. */
1792 if ((old_to_ds & SPECULATIVE) && (old_from_ds & SPECULATIVE))
1793 {
1794 int res;
b8698a0f 1795
e855c69d
AB
1796 res = speculate_expr (to, EXPR_SPEC_DONE_DS (to));
1797 gcc_assert (res >= 0);
1798 }
1799
1800 if (split_point != NULL)
1801 {
1802 /* Record the change with proper status. */
1803 record_ds = EXPR_SPEC_DONE_DS (to) & SPECULATIVE;
1804 record_ds &= ~(old_to_ds & SPECULATIVE);
1805 record_ds &= ~(old_from_ds & SPECULATIVE);
b8698a0f
L
1806
1807 insert_in_history_vect (&EXPR_HISTORY_OF_CHANGES (to),
1808 INSN_UID (split_point), TRANS_SPECULATION,
e855c69d
AB
1809 EXPR_VINSN (from), EXPR_VINSN (to),
1810 record_ds);
1811 }
1812 }
1813 }
1814}
1815
1816
1817/* Merge bits of FROM expr to TO expr. When SPLIT_POINT is not NULL,
1818 this is done along different paths. */
1819void
1820merge_expr_data (expr_t to, expr_t from, insn_t split_point)
1821{
bf3a40e9
DM
1822 /* Choose the maximum of the specs of merged exprs. This is required
1823 for correctness of bookkeeping. */
1824 if (EXPR_SPEC (to) < EXPR_SPEC (from))
e855c69d
AB
1825 EXPR_SPEC (to) = EXPR_SPEC (from);
1826
1827 if (split_point)
1828 EXPR_USEFULNESS (to) += EXPR_USEFULNESS (from);
1829 else
b8698a0f 1830 EXPR_USEFULNESS (to) = MAX (EXPR_USEFULNESS (to),
e855c69d
AB
1831 EXPR_USEFULNESS (from));
1832
1833 if (EXPR_PRIORITY (to) < EXPR_PRIORITY (from))
1834 EXPR_PRIORITY (to) = EXPR_PRIORITY (from);
1835
1836 if (EXPR_SCHED_TIMES (to) > EXPR_SCHED_TIMES (from))
1837 EXPR_SCHED_TIMES (to) = EXPR_SCHED_TIMES (from);
1838
1839 if (EXPR_ORIG_BB_INDEX (to) != EXPR_ORIG_BB_INDEX (from))
1840 EXPR_ORIG_BB_INDEX (to) = 0;
1841
b8698a0f 1842 EXPR_ORIG_SCHED_CYCLE (to) = MIN (EXPR_ORIG_SCHED_CYCLE (to),
e855c69d
AB
1843 EXPR_ORIG_SCHED_CYCLE (from));
1844
e855c69d
AB
1845 EXPR_WAS_SUBSTITUTED (to) |= EXPR_WAS_SUBSTITUTED (from);
1846 EXPR_WAS_RENAMED (to) |= EXPR_WAS_RENAMED (from);
1847 EXPR_CANT_MOVE (to) |= EXPR_CANT_MOVE (from);
1848
5d369d58
AB
1849 merge_history_vect (&EXPR_HISTORY_OF_CHANGES (to),
1850 EXPR_HISTORY_OF_CHANGES (from));
e855c69d
AB
1851 update_target_availability (to, from, split_point);
1852 update_speculative_bits (to, from, split_point);
1853}
1854
1855/* Merge bits of FROM expr to TO expr. Vinsns in the exprs should be equal
b8698a0f 1856 in terms of vinsn_equal_p. SPLIT_POINT is non-null when expressions
e855c69d
AB
1857 are merged from different successors at a split point. */
1858void
1859merge_expr (expr_t to, expr_t from, insn_t split_point)
1860{
1861 vinsn_t to_vi = EXPR_VINSN (to);
1862 vinsn_t from_vi = EXPR_VINSN (from);
1863
1864 gcc_assert (vinsn_equal_p (to_vi, from_vi));
1865
1866 /* Make sure that speculative pattern is propagated into exprs that
1867 have non-speculative one. This will provide us with consistent
1868 speculative bits and speculative patterns inside expr. */
1869 if (EXPR_SPEC_DONE_DS (to) == 0
1870 && EXPR_SPEC_DONE_DS (from) != 0)
1871 change_vinsn_in_expr (to, EXPR_VINSN (from));
1872
1873 merge_expr_data (to, from, split_point);
1874 gcc_assert (EXPR_USEFULNESS (to) <= REG_BR_PROB_BASE);
1875}
1876
1877/* Clear the information of this EXPR. */
1878void
1879clear_expr (expr_t expr)
1880{
b8698a0f 1881
e855c69d
AB
1882 vinsn_detach (EXPR_VINSN (expr));
1883 EXPR_VINSN (expr) = NULL;
1884
1885 free_history_vect (&EXPR_HISTORY_OF_CHANGES (expr));
1886}
1887
1888/* For a given LV_SET, mark EXPR having unavailable target register. */
1889static void
1890set_unavailable_target_for_expr (expr_t expr, regset lv_set)
1891{
1892 if (EXPR_SEPARABLE_P (expr))
1893 {
1894 if (REG_P (EXPR_LHS (expr))
cf3d5824 1895 && register_unavailable_p (lv_set, EXPR_LHS (expr)))
e855c69d 1896 {
b8698a0f
L
1897 /* If it's an insn like r1 = use (r1, ...), and it exists in
1898 different forms in each of the av_sets being merged, we can't say
1899 whether original destination register is available or not.
1900 However, this still works if destination register is not used
e855c69d
AB
1901 in the original expression: if the branch at which LV_SET we're
1902 looking here is not actually 'other branch' in sense that same
b8698a0f 1903 expression is available through it (but it can't be determined
e855c69d 1904 at computation stage because of transformations on one of the
b8698a0f
L
1905 branches), it still won't affect the availability.
1906 Liveness of a register somewhere on a code motion path means
1907 it's either read somewhere on a codemotion path, live on
e855c69d
AB
1908 'other' branch, live at the point immediately following
1909 the original operation, or is read by the original operation.
1910 The latter case is filtered out in the condition below.
1911 It still doesn't cover the case when register is defined and used
1912 somewhere within the code motion path, and in this case we could
1913 miss a unifying code motion along both branches using a renamed
1914 register, but it won't affect a code correctness since upon
1915 an actual code motion a bookkeeping code would be generated. */
cf3d5824
SG
1916 if (register_unavailable_p (VINSN_REG_USES (EXPR_VINSN (expr)),
1917 EXPR_LHS (expr)))
e855c69d
AB
1918 EXPR_TARGET_AVAILABLE (expr) = -1;
1919 else
1920 EXPR_TARGET_AVAILABLE (expr) = false;
1921 }
1922 }
1923 else
1924 {
1925 unsigned regno;
1926 reg_set_iterator rsi;
b8698a0f
L
1927
1928 EXECUTE_IF_SET_IN_REG_SET (VINSN_REG_SETS (EXPR_VINSN (expr)),
e855c69d
AB
1929 0, regno, rsi)
1930 if (bitmap_bit_p (lv_set, regno))
1931 {
1932 EXPR_TARGET_AVAILABLE (expr) = false;
1933 break;
1934 }
1935
1936 EXECUTE_IF_SET_IN_REG_SET (VINSN_REG_CLOBBERS (EXPR_VINSN (expr)),
1937 0, regno, rsi)
1938 if (bitmap_bit_p (lv_set, regno))
1939 {
1940 EXPR_TARGET_AVAILABLE (expr) = false;
1941 break;
1942 }
1943 }
1944}
1945
b8698a0f 1946/* Try to make EXPR speculative. Return 1 when EXPR's pattern
e855c69d
AB
1947 or dependence status have changed, 2 when also the target register
1948 became unavailable, 0 if nothing had to be changed. */
1949int
1950speculate_expr (expr_t expr, ds_t ds)
1951{
1952 int res;
1953 rtx orig_insn_rtx;
1954 rtx spec_pat;
1955 ds_t target_ds, current_ds;
1956
1957 /* Obtain the status we need to put on EXPR. */
1958 target_ds = (ds & SPECULATIVE);
1959 current_ds = EXPR_SPEC_DONE_DS (expr);
1960 ds = ds_full_merge (current_ds, target_ds, NULL_RTX, NULL_RTX);
1961
1962 orig_insn_rtx = EXPR_INSN_RTX (expr);
1963
1964 res = sched_speculate_insn (orig_insn_rtx, ds, &spec_pat);
1965
1966 switch (res)
1967 {
1968 case 0:
1969 EXPR_SPEC_DONE_DS (expr) = ds;
1970 return current_ds != ds ? 1 : 0;
b8698a0f 1971
e855c69d
AB
1972 case 1:
1973 {
1974 rtx spec_insn_rtx = create_insn_rtx_from_pattern (spec_pat, NULL_RTX);
1975 vinsn_t spec_vinsn = create_vinsn_from_insn_rtx (spec_insn_rtx, false);
1976
1977 change_vinsn_in_expr (expr, spec_vinsn);
1978 EXPR_SPEC_DONE_DS (expr) = ds;
1979 EXPR_NEEDS_SPEC_CHECK_P (expr) = true;
1980
b8698a0f 1981 /* Do not allow clobbering the address register of speculative
e855c69d 1982 insns. */
cf3d5824
SG
1983 if (register_unavailable_p (VINSN_REG_USES (EXPR_VINSN (expr)),
1984 expr_dest_reg (expr)))
e855c69d
AB
1985 {
1986 EXPR_TARGET_AVAILABLE (expr) = false;
1987 return 2;
1988 }
1989
1990 return 1;
1991 }
1992
1993 case -1:
1994 return -1;
1995
1996 default:
1997 gcc_unreachable ();
1998 return -1;
1999 }
2000}
2001
2002/* Return a destination register, if any, of EXPR. */
2003rtx
2004expr_dest_reg (expr_t expr)
2005{
2006 rtx dest = VINSN_LHS (EXPR_VINSN (expr));
2007
2008 if (dest != NULL_RTX && REG_P (dest))
2009 return dest;
2010
2011 return NULL_RTX;
2012}
2013
2014/* Returns the REGNO of the R's destination. */
2015unsigned
2016expr_dest_regno (expr_t expr)
2017{
2018 rtx dest = expr_dest_reg (expr);
2019
2020 gcc_assert (dest != NULL_RTX);
2021 return REGNO (dest);
2022}
2023
b8698a0f 2024/* For a given LV_SET, mark all expressions in JOIN_SET, but not present in
e855c69d
AB
2025 AV_SET having unavailable target register. */
2026void
2027mark_unavailable_targets (av_set_t join_set, av_set_t av_set, regset lv_set)
2028{
2029 expr_t expr;
2030 av_set_iterator avi;
2031
2032 FOR_EACH_EXPR (expr, avi, join_set)
2033 if (av_set_lookup (av_set, EXPR_VINSN (expr)) == NULL)
2034 set_unavailable_target_for_expr (expr, lv_set);
2035}
2036\f
2037
cf3d5824
SG
2038/* Returns true if REG (at least partially) is present in REGS. */
2039bool
2040register_unavailable_p (regset regs, rtx reg)
2041{
2042 unsigned regno, end_regno;
2043
2044 regno = REGNO (reg);
2045 if (bitmap_bit_p (regs, regno))
2046 return true;
2047
2048 end_regno = END_REGNO (reg);
2049
2050 while (++regno < end_regno)
2051 if (bitmap_bit_p (regs, regno))
2052 return true;
2053
2054 return false;
2055}
2056
e855c69d
AB
2057/* Av set functions. */
2058
2059/* Add a new element to av set SETP.
2060 Return the element added. */
2061static av_set_t
2062av_set_add_element (av_set_t *setp)
2063{
2064 /* Insert at the beginning of the list. */
2065 _list_add (setp);
2066 return *setp;
2067}
2068
2069/* Add EXPR to SETP. */
2070void
2071av_set_add (av_set_t *setp, expr_t expr)
2072{
2073 av_set_t elem;
b8698a0f 2074
e855c69d
AB
2075 gcc_assert (!INSN_NOP_P (EXPR_INSN_RTX (expr)));
2076 elem = av_set_add_element (setp);
2077 copy_expr (_AV_SET_EXPR (elem), expr);
2078}
2079
2080/* Same, but do not copy EXPR. */
2081static void
2082av_set_add_nocopy (av_set_t *setp, expr_t expr)
2083{
2084 av_set_t elem;
2085
2086 elem = av_set_add_element (setp);
2087 *_AV_SET_EXPR (elem) = *expr;
2088}
2089
2090/* Remove expr pointed to by IP from the av_set. */
2091void
2092av_set_iter_remove (av_set_iterator *ip)
2093{
2094 clear_expr (_AV_SET_EXPR (*ip->lp));
2095 _list_iter_remove (ip);
2096}
2097
2098/* Search for an expr in SET, such that it's equivalent to SOUGHT_VINSN in the
2099 sense of vinsn_equal_p function. Return NULL if no such expr is
2100 in SET was found. */
2101expr_t
2102av_set_lookup (av_set_t set, vinsn_t sought_vinsn)
2103{
2104 expr_t expr;
2105 av_set_iterator i;
2106
2107 FOR_EACH_EXPR (expr, i, set)
2108 if (vinsn_equal_p (EXPR_VINSN (expr), sought_vinsn))
2109 return expr;
2110 return NULL;
2111}
2112
2113/* Same, but also remove the EXPR found. */
2114static expr_t
2115av_set_lookup_and_remove (av_set_t *setp, vinsn_t sought_vinsn)
2116{
2117 expr_t expr;
2118 av_set_iterator i;
2119
2120 FOR_EACH_EXPR_1 (expr, i, setp)
2121 if (vinsn_equal_p (EXPR_VINSN (expr), sought_vinsn))
2122 {
2123 _list_iter_remove_nofree (&i);
2124 return expr;
2125 }
2126 return NULL;
2127}
2128
2129/* Search for an expr in SET, such that it's equivalent to EXPR in the
2130 sense of vinsn_equal_p function of their vinsns, but not EXPR itself.
2131 Returns NULL if no such expr is in SET was found. */
2132static expr_t
2133av_set_lookup_other_equiv_expr (av_set_t set, expr_t expr)
2134{
2135 expr_t cur_expr;
2136 av_set_iterator i;
2137
2138 FOR_EACH_EXPR (cur_expr, i, set)
2139 {
2140 if (cur_expr == expr)
2141 continue;
2142 if (vinsn_equal_p (EXPR_VINSN (cur_expr), EXPR_VINSN (expr)))
2143 return cur_expr;
2144 }
2145
2146 return NULL;
2147}
2148
2149/* If other expression is already in AVP, remove one of them. */
2150expr_t
2151merge_with_other_exprs (av_set_t *avp, av_set_iterator *ip, expr_t expr)
2152{
2153 expr_t expr2;
2154
2155 expr2 = av_set_lookup_other_equiv_expr (*avp, expr);
2156 if (expr2 != NULL)
2157 {
2158 /* Reset target availability on merge, since taking it only from one
2159 of the exprs would be controversial for different code. */
2160 EXPR_TARGET_AVAILABLE (expr2) = -1;
2161 EXPR_USEFULNESS (expr2) = 0;
2162
2163 merge_expr (expr2, expr, NULL);
b8698a0f 2164
e855c69d
AB
2165 /* Fix usefulness as it should be now REG_BR_PROB_BASE. */
2166 EXPR_USEFULNESS (expr2) = REG_BR_PROB_BASE;
b8698a0f 2167
e855c69d
AB
2168 av_set_iter_remove (ip);
2169 return expr2;
2170 }
2171
2172 return expr;
2173}
2174
2175/* Return true if there is an expr that correlates to VI in SET. */
2176bool
2177av_set_is_in_p (av_set_t set, vinsn_t vi)
2178{
2179 return av_set_lookup (set, vi) != NULL;
2180}
2181
2182/* Return a copy of SET. */
2183av_set_t
2184av_set_copy (av_set_t set)
2185{
2186 expr_t expr;
2187 av_set_iterator i;
2188 av_set_t res = NULL;
2189
2190 FOR_EACH_EXPR (expr, i, set)
2191 av_set_add (&res, expr);
2192
2193 return res;
2194}
2195
2196/* Join two av sets that do not have common elements by attaching second set
2197 (pointed to by FROMP) to the end of first set (TO_TAILP must point to
2198 _AV_SET_NEXT of first set's last element). */
2199static void
2200join_distinct_sets (av_set_t *to_tailp, av_set_t *fromp)
2201{
2202 gcc_assert (*to_tailp == NULL);
2203 *to_tailp = *fromp;
2204 *fromp = NULL;
2205}
2206
2207/* Makes set pointed to by TO to be the union of TO and FROM. Clear av_set
2208 pointed to by FROMP afterwards. */
2209void
2210av_set_union_and_clear (av_set_t *top, av_set_t *fromp, insn_t insn)
2211{
2212 expr_t expr1;
2213 av_set_iterator i;
2214
2215 /* Delete from TOP all exprs, that present in FROMP. */
2216 FOR_EACH_EXPR_1 (expr1, i, top)
2217 {
2218 expr_t expr2 = av_set_lookup (*fromp, EXPR_VINSN (expr1));
2219
2220 if (expr2)
2221 {
2222 merge_expr (expr2, expr1, insn);
2223 av_set_iter_remove (&i);
2224 }
2225 }
2226
2227 join_distinct_sets (i.lp, fromp);
2228}
2229
b8698a0f 2230/* Same as above, but also update availability of target register in
e855c69d
AB
2231 TOP judging by TO_LV_SET and FROM_LV_SET. */
2232void
2233av_set_union_and_live (av_set_t *top, av_set_t *fromp, regset to_lv_set,
2234 regset from_lv_set, insn_t insn)
2235{
2236 expr_t expr1;
2237 av_set_iterator i;
2238 av_set_t *to_tailp, in_both_set = NULL;
2239
2240 /* Delete from TOP all expres, that present in FROMP. */
2241 FOR_EACH_EXPR_1 (expr1, i, top)
2242 {
2243 expr_t expr2 = av_set_lookup_and_remove (fromp, EXPR_VINSN (expr1));
2244
2245 if (expr2)
2246 {
b8698a0f 2247 /* It may be that the expressions have different destination
e855c69d
AB
2248 registers, in which case we need to check liveness here. */
2249 if (EXPR_SEPARABLE_P (expr1))
2250 {
b8698a0f 2251 int regno1 = (REG_P (EXPR_LHS (expr1))
e855c69d 2252 ? (int) expr_dest_regno (expr1) : -1);
b8698a0f 2253 int regno2 = (REG_P (EXPR_LHS (expr2))
e855c69d 2254 ? (int) expr_dest_regno (expr2) : -1);
b8698a0f
L
2255
2256 /* ??? We don't have a way to check restrictions for
e855c69d
AB
2257 *other* register on the current path, we did it only
2258 for the current target register. Give up. */
2259 if (regno1 != regno2)
2260 EXPR_TARGET_AVAILABLE (expr2) = -1;
2261 }
2262 else if (EXPR_INSN_RTX (expr1) != EXPR_INSN_RTX (expr2))
2263 EXPR_TARGET_AVAILABLE (expr2) = -1;
2264
2265 merge_expr (expr2, expr1, insn);
2266 av_set_add_nocopy (&in_both_set, expr2);
2267 av_set_iter_remove (&i);
2268 }
2269 else
b8698a0f 2270 /* EXPR1 is present in TOP, but not in FROMP. Check it on
e855c69d
AB
2271 FROM_LV_SET. */
2272 set_unavailable_target_for_expr (expr1, from_lv_set);
2273 }
2274 to_tailp = i.lp;
2275
2276 /* These expressions are not present in TOP. Check liveness
2277 restrictions on TO_LV_SET. */
2278 FOR_EACH_EXPR (expr1, i, *fromp)
2279 set_unavailable_target_for_expr (expr1, to_lv_set);
2280
2281 join_distinct_sets (i.lp, &in_both_set);
2282 join_distinct_sets (to_tailp, fromp);
2283}
2284
2285/* Clear av_set pointed to by SETP. */
2286void
2287av_set_clear (av_set_t *setp)
2288{
2289 expr_t expr;
2290 av_set_iterator i;
2291
2292 FOR_EACH_EXPR_1 (expr, i, setp)
2293 av_set_iter_remove (&i);
2294
2295 gcc_assert (*setp == NULL);
2296}
2297
2298/* Leave only one non-speculative element in the SETP. */
2299void
2300av_set_leave_one_nonspec (av_set_t *setp)
2301{
2302 expr_t expr;
2303 av_set_iterator i;
2304 bool has_one_nonspec = false;
2305
b8698a0f 2306 /* Keep all speculative exprs, and leave one non-speculative
e855c69d
AB
2307 (the first one). */
2308 FOR_EACH_EXPR_1 (expr, i, setp)
2309 {
2310 if (!EXPR_SPEC_DONE_DS (expr))
2311 {
2312 if (has_one_nonspec)
2313 av_set_iter_remove (&i);
2314 else
2315 has_one_nonspec = true;
2316 }
2317 }
2318}
2319
2320/* Return the N'th element of the SET. */
2321expr_t
2322av_set_element (av_set_t set, int n)
2323{
2324 expr_t expr;
2325 av_set_iterator i;
2326
2327 FOR_EACH_EXPR (expr, i, set)
2328 if (n-- == 0)
2329 return expr;
2330
2331 gcc_unreachable ();
2332 return NULL;
2333}
2334
2335/* Deletes all expressions from AVP that are conditional branches (IFs). */
2336void
2337av_set_substract_cond_branches (av_set_t *avp)
2338{
2339 av_set_iterator i;
2340 expr_t expr;
2341
2342 FOR_EACH_EXPR_1 (expr, i, avp)
2343 if (vinsn_cond_branch_p (EXPR_VINSN (expr)))
2344 av_set_iter_remove (&i);
2345}
2346
b8698a0f 2347/* Multiplies usefulness attribute of each member of av-set *AVP by
e855c69d
AB
2348 value PROB / ALL_PROB. */
2349void
2350av_set_split_usefulness (av_set_t av, int prob, int all_prob)
2351{
2352 av_set_iterator i;
2353 expr_t expr;
2354
2355 FOR_EACH_EXPR (expr, i, av)
b8698a0f 2356 EXPR_USEFULNESS (expr) = (all_prob
e855c69d
AB
2357 ? (EXPR_USEFULNESS (expr) * prob) / all_prob
2358 : 0);
2359}
2360
2361/* Leave in AVP only those expressions, which are present in AV,
5d369d58 2362 and return it, merging history expressions. */
e855c69d 2363void
5d369d58 2364av_set_code_motion_filter (av_set_t *avp, av_set_t av)
e855c69d
AB
2365{
2366 av_set_iterator i;
5d369d58 2367 expr_t expr, expr2;
e855c69d
AB
2368
2369 FOR_EACH_EXPR_1 (expr, i, avp)
5d369d58 2370 if ((expr2 = av_set_lookup (av, EXPR_VINSN (expr))) == NULL)
e855c69d 2371 av_set_iter_remove (&i);
5d369d58
AB
2372 else
2373 /* When updating av sets in bookkeeping blocks, we can add more insns
2374 there which will be transformed but the upper av sets will not
2375 reflect those transformations. We then fail to undo those
2376 when searching for such insns. So merge the history saved
2377 in the av set of the block we are processing. */
2378 merge_history_vect (&EXPR_HISTORY_OF_CHANGES (expr),
2379 EXPR_HISTORY_OF_CHANGES (expr2));
e855c69d
AB
2380}
2381
2382\f
2383
2384/* Dependence hooks to initialize insn data. */
2385
2386/* This is used in hooks callable from dependence analysis when initializing
2387 instruction's data. */
2388static struct
2389{
2390 /* Where the dependence was found (lhs/rhs). */
2391 deps_where_t where;
2392
2393 /* The actual data object to initialize. */
2394 idata_t id;
2395
2396 /* True when the insn should not be made clonable. */
2397 bool force_unique_p;
2398
2399 /* True when insn should be treated as of type USE, i.e. never renamed. */
2400 bool force_use_p;
2401} deps_init_id_data;
2402
2403
b8698a0f 2404/* Setup ID for INSN. FORCE_UNIQUE_P is true when INSN should not be
e855c69d
AB
2405 clonable. */
2406static void
2407setup_id_for_insn (idata_t id, insn_t insn, bool force_unique_p)
2408{
2409 int type;
b8698a0f 2410
e855c69d
AB
2411 /* Determine whether INSN could be cloned and return appropriate vinsn type.
2412 That clonable insns which can be separated into lhs and rhs have type SET.
2413 Other clonable insns have type USE. */
2414 type = GET_CODE (insn);
2415
2416 /* Only regular insns could be cloned. */
2417 if (type == INSN && !force_unique_p)
2418 type = SET;
2419 else if (type == JUMP_INSN && simplejump_p (insn))
2420 type = PC;
b5b8b0ac
AO
2421 else if (type == DEBUG_INSN)
2422 type = !force_unique_p ? USE : INSN;
b8698a0f 2423
e855c69d
AB
2424 IDATA_TYPE (id) = type;
2425 IDATA_REG_SETS (id) = get_clear_regset_from_pool ();
2426 IDATA_REG_USES (id) = get_clear_regset_from_pool ();
2427 IDATA_REG_CLOBBERS (id) = get_clear_regset_from_pool ();
2428}
2429
2430/* Start initializing insn data. */
2431static void
2432deps_init_id_start_insn (insn_t insn)
2433{
2434 gcc_assert (deps_init_id_data.where == DEPS_IN_NOWHERE);
2435
2436 setup_id_for_insn (deps_init_id_data.id, insn,
2437 deps_init_id_data.force_unique_p);
2438 deps_init_id_data.where = DEPS_IN_INSN;
2439}
2440
2441/* Start initializing lhs data. */
2442static void
2443deps_init_id_start_lhs (rtx lhs)
2444{
2445 gcc_assert (deps_init_id_data.where == DEPS_IN_INSN);
2446 gcc_assert (IDATA_LHS (deps_init_id_data.id) == NULL);
2447
2448 if (IDATA_TYPE (deps_init_id_data.id) == SET)
2449 {
2450 IDATA_LHS (deps_init_id_data.id) = lhs;
2451 deps_init_id_data.where = DEPS_IN_LHS;
2452 }
2453}
2454
2455/* Finish initializing lhs data. */
2456static void
2457deps_init_id_finish_lhs (void)
2458{
2459 deps_init_id_data.where = DEPS_IN_INSN;
2460}
2461
2462/* Note a set of REGNO. */
2463static void
2464deps_init_id_note_reg_set (int regno)
2465{
2466 haifa_note_reg_set (regno);
2467
2468 if (deps_init_id_data.where == DEPS_IN_RHS)
2469 deps_init_id_data.force_use_p = true;
2470
2471 if (IDATA_TYPE (deps_init_id_data.id) != PC)
2472 SET_REGNO_REG_SET (IDATA_REG_SETS (deps_init_id_data.id), regno);
2473
2474#ifdef STACK_REGS
b8698a0f 2475 /* Make instructions that set stack registers to be ineligible for
e855c69d
AB
2476 renaming to avoid issues with find_used_regs. */
2477 if (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG))
2478 deps_init_id_data.force_use_p = true;
2479#endif
2480}
2481
2482/* Note a clobber of REGNO. */
2483static void
2484deps_init_id_note_reg_clobber (int regno)
2485{
2486 haifa_note_reg_clobber (regno);
2487
2488 if (deps_init_id_data.where == DEPS_IN_RHS)
2489 deps_init_id_data.force_use_p = true;
2490
2491 if (IDATA_TYPE (deps_init_id_data.id) != PC)
2492 SET_REGNO_REG_SET (IDATA_REG_CLOBBERS (deps_init_id_data.id), regno);
2493}
2494
2495/* Note a use of REGNO. */
2496static void
2497deps_init_id_note_reg_use (int regno)
2498{
2499 haifa_note_reg_use (regno);
2500
2501 if (IDATA_TYPE (deps_init_id_data.id) != PC)
2502 SET_REGNO_REG_SET (IDATA_REG_USES (deps_init_id_data.id), regno);
2503}
2504
2505/* Start initializing rhs data. */
2506static void
2507deps_init_id_start_rhs (rtx rhs)
2508{
2509 gcc_assert (deps_init_id_data.where == DEPS_IN_INSN);
2510
2511 /* And there was no sel_deps_reset_to_insn (). */
2512 if (IDATA_LHS (deps_init_id_data.id) != NULL)
2513 {
2514 IDATA_RHS (deps_init_id_data.id) = rhs;
2515 deps_init_id_data.where = DEPS_IN_RHS;
2516 }
2517}
2518
2519/* Finish initializing rhs data. */
2520static void
2521deps_init_id_finish_rhs (void)
2522{
2523 gcc_assert (deps_init_id_data.where == DEPS_IN_RHS
2524 || deps_init_id_data.where == DEPS_IN_INSN);
2525 deps_init_id_data.where = DEPS_IN_INSN;
2526}
2527
2528/* Finish initializing insn data. */
2529static void
2530deps_init_id_finish_insn (void)
2531{
2532 gcc_assert (deps_init_id_data.where == DEPS_IN_INSN);
2533
2534 if (IDATA_TYPE (deps_init_id_data.id) == SET)
2535 {
2536 rtx lhs = IDATA_LHS (deps_init_id_data.id);
2537 rtx rhs = IDATA_RHS (deps_init_id_data.id);
2538
2539 if (lhs == NULL || rhs == NULL || !lhs_and_rhs_separable_p (lhs, rhs)
2540 || deps_init_id_data.force_use_p)
2541 {
b8698a0f 2542 /* This should be a USE, as we don't want to schedule its RHS
e855c69d 2543 separately. However, we still want to have them recorded
b8698a0f 2544 for the purposes of substitution. That's why we don't
e855c69d
AB
2545 simply call downgrade_to_use () here. */
2546 gcc_assert (IDATA_TYPE (deps_init_id_data.id) == SET);
2547 gcc_assert (!lhs == !rhs);
2548
2549 IDATA_TYPE (deps_init_id_data.id) = USE;
2550 }
2551 }
2552
2553 deps_init_id_data.where = DEPS_IN_NOWHERE;
2554}
2555
2556/* This is dependence info used for initializing insn's data. */
2557static struct sched_deps_info_def deps_init_id_sched_deps_info;
2558
2559/* This initializes most of the static part of the above structure. */
2560static const struct sched_deps_info_def const_deps_init_id_sched_deps_info =
2561 {
2562 NULL,
2563
2564 deps_init_id_start_insn,
2565 deps_init_id_finish_insn,
2566 deps_init_id_start_lhs,
2567 deps_init_id_finish_lhs,
2568 deps_init_id_start_rhs,
2569 deps_init_id_finish_rhs,
2570 deps_init_id_note_reg_set,
2571 deps_init_id_note_reg_clobber,
2572 deps_init_id_note_reg_use,
2573 NULL, /* note_mem_dep */
2574 NULL, /* note_dep */
2575
2576 0, /* use_cselib */
2577 0, /* use_deps_list */
2578 0 /* generate_spec_deps */
2579 };
2580
2581/* Initialize INSN's lhs and rhs in ID. When FORCE_UNIQUE_P is true,
2582 we don't actually need information about lhs and rhs. */
2583static void
2584setup_id_lhs_rhs (idata_t id, insn_t insn, bool force_unique_p)
2585{
2586 rtx pat = PATTERN (insn);
b8698a0f 2587
481683e1 2588 if (NONJUMP_INSN_P (insn)
b8698a0f 2589 && GET_CODE (pat) == SET
e855c69d
AB
2590 && !force_unique_p)
2591 {
2592 IDATA_RHS (id) = SET_SRC (pat);
2593 IDATA_LHS (id) = SET_DEST (pat);
2594 }
2595 else
2596 IDATA_LHS (id) = IDATA_RHS (id) = NULL;
2597}
2598
2599/* Possibly downgrade INSN to USE. */
2600static void
2601maybe_downgrade_id_to_use (idata_t id, insn_t insn)
2602{
2603 bool must_be_use = false;
2604 unsigned uid = INSN_UID (insn);
57512f53 2605 df_ref *rec;
e855c69d
AB
2606 rtx lhs = IDATA_LHS (id);
2607 rtx rhs = IDATA_RHS (id);
b8698a0f 2608
e855c69d
AB
2609 /* We downgrade only SETs. */
2610 if (IDATA_TYPE (id) != SET)
2611 return;
2612
2613 if (!lhs || !lhs_and_rhs_separable_p (lhs, rhs))
2614 {
2615 IDATA_TYPE (id) = USE;
2616 return;
2617 }
b8698a0f 2618
e855c69d
AB
2619 for (rec = DF_INSN_UID_DEFS (uid); *rec; rec++)
2620 {
57512f53 2621 df_ref def = *rec;
b8698a0f 2622
e855c69d
AB
2623 if (DF_REF_INSN (def)
2624 && DF_REF_FLAGS_IS_SET (def, DF_REF_PRE_POST_MODIFY)
2625 && loc_mentioned_in_p (DF_REF_LOC (def), IDATA_RHS (id)))
2626 {
2627 must_be_use = true;
2628 break;
2629 }
2630
2631#ifdef STACK_REGS
b8698a0f 2632 /* Make instructions that set stack registers to be ineligible for
e855c69d
AB
2633 renaming to avoid issues with find_used_regs. */
2634 if (IN_RANGE (DF_REF_REGNO (def), FIRST_STACK_REG, LAST_STACK_REG))
2635 {
2636 must_be_use = true;
2637 break;
2638 }
2639#endif
b8698a0f
L
2640 }
2641
e855c69d
AB
2642 if (must_be_use)
2643 IDATA_TYPE (id) = USE;
2644}
2645
2646/* Setup register sets describing INSN in ID. */
2647static void
2648setup_id_reg_sets (idata_t id, insn_t insn)
2649{
2650 unsigned uid = INSN_UID (insn);
57512f53 2651 df_ref *rec;
e855c69d 2652 regset tmp = get_clear_regset_from_pool ();
b8698a0f 2653
e855c69d
AB
2654 for (rec = DF_INSN_UID_DEFS (uid); *rec; rec++)
2655 {
57512f53 2656 df_ref def = *rec;
e855c69d 2657 unsigned int regno = DF_REF_REGNO (def);
b8698a0f 2658
e855c69d
AB
2659 /* Post modifies are treated like clobbers by sched-deps.c. */
2660 if (DF_REF_FLAGS_IS_SET (def, (DF_REF_MUST_CLOBBER
2661 | DF_REF_PRE_POST_MODIFY)))
2662 SET_REGNO_REG_SET (IDATA_REG_CLOBBERS (id), regno);
2663 else if (! DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
2664 {
2665 SET_REGNO_REG_SET (IDATA_REG_SETS (id), regno);
2666
2667#ifdef STACK_REGS
b8698a0f 2668 /* For stack registers, treat writes to them as writes
e855c69d
AB
2669 to the first one to be consistent with sched-deps.c. */
2670 if (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG))
2671 SET_REGNO_REG_SET (IDATA_REG_SETS (id), FIRST_STACK_REG);
2672#endif
2673 }
2674 /* Mark special refs that generate read/write def pair. */
2675 if (DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)
2676 || regno == STACK_POINTER_REGNUM)
2677 bitmap_set_bit (tmp, regno);
2678 }
b8698a0f 2679
e855c69d
AB
2680 for (rec = DF_INSN_UID_USES (uid); *rec; rec++)
2681 {
57512f53 2682 df_ref use = *rec;
e855c69d
AB
2683 unsigned int regno = DF_REF_REGNO (use);
2684
2685 /* When these refs are met for the first time, skip them, as
2686 these uses are just counterparts of some defs. */
2687 if (bitmap_bit_p (tmp, regno))
2688 bitmap_clear_bit (tmp, regno);
2689 else if (! DF_REF_FLAGS_IS_SET (use, DF_REF_CALL_STACK_USAGE))
2690 {
2691 SET_REGNO_REG_SET (IDATA_REG_USES (id), regno);
2692
2693#ifdef STACK_REGS
b8698a0f 2694 /* For stack registers, treat reads from them as reads from
e855c69d
AB
2695 the first one to be consistent with sched-deps.c. */
2696 if (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG))
2697 SET_REGNO_REG_SET (IDATA_REG_USES (id), FIRST_STACK_REG);
2698#endif
2699 }
2700 }
2701
2702 return_regset_to_pool (tmp);
2703}
2704
2705/* Initialize instruction data for INSN in ID using DF's data. */
2706static void
2707init_id_from_df (idata_t id, insn_t insn, bool force_unique_p)
2708{
2709 gcc_assert (DF_INSN_UID_SAFE_GET (INSN_UID (insn)) != NULL);
2710
2711 setup_id_for_insn (id, insn, force_unique_p);
2712 setup_id_lhs_rhs (id, insn, force_unique_p);
2713
2714 if (INSN_NOP_P (insn))
2715 return;
2716
2717 maybe_downgrade_id_to_use (id, insn);
2718 setup_id_reg_sets (id, insn);
2719}
2720
2721/* Initialize instruction data for INSN in ID. */
2722static void
2723deps_init_id (idata_t id, insn_t insn, bool force_unique_p)
2724{
88302d54 2725 struct deps_desc _dc, *dc = &_dc;
e855c69d
AB
2726
2727 deps_init_id_data.where = DEPS_IN_NOWHERE;
2728 deps_init_id_data.id = id;
2729 deps_init_id_data.force_unique_p = force_unique_p;
2730 deps_init_id_data.force_use_p = false;
2731
bcf33775 2732 init_deps (dc, false);
e855c69d
AB
2733
2734 memcpy (&deps_init_id_sched_deps_info,
2735 &const_deps_init_id_sched_deps_info,
2736 sizeof (deps_init_id_sched_deps_info));
2737
2738 if (spec_info != NULL)
2739 deps_init_id_sched_deps_info.generate_spec_deps = 1;
2740
2741 sched_deps_info = &deps_init_id_sched_deps_info;
2742
2743 deps_analyze_insn (dc, insn);
2744
2745 free_deps (dc);
2746
2747 deps_init_id_data.id = NULL;
2748}
2749
2750\f
a95b23b4
BS
2751struct sched_scan_info_def
2752{
2753 /* This hook notifies scheduler frontend to extend its internal per basic
2754 block data structures. This hook should be called once before a series of
2755 calls to bb_init (). */
2756 void (*extend_bb) (void);
2757
2758 /* This hook makes scheduler frontend to initialize its internal data
2759 structures for the passed basic block. */
2760 void (*init_bb) (basic_block);
2761
2762 /* This hook notifies scheduler frontend to extend its internal per insn data
2763 structures. This hook should be called once before a series of calls to
2764 insn_init (). */
2765 void (*extend_insn) (void);
2766
2767 /* This hook makes scheduler frontend to initialize its internal data
2768 structures for the passed insn. */
2769 void (*init_insn) (rtx);
2770};
2771
2772/* A driver function to add a set of basic blocks (BBS) to the
2773 scheduling region. */
2774static void
2775sched_scan (const struct sched_scan_info_def *ssi, bb_vec_t bbs)
2776{
2777 unsigned i;
2778 basic_block bb;
2779
2780 if (ssi->extend_bb)
2781 ssi->extend_bb ();
2782
2783 if (ssi->init_bb)
2784 FOR_EACH_VEC_ELT (basic_block, bbs, i, bb)
2785 ssi->init_bb (bb);
2786
2787 if (ssi->extend_insn)
2788 ssi->extend_insn ();
2789
2790 if (ssi->init_insn)
2791 FOR_EACH_VEC_ELT (basic_block, bbs, i, bb)
2792 {
2793 rtx insn;
2794
2795 FOR_BB_INSNS (bb, insn)
2796 ssi->init_insn (insn);
2797 }
2798}
e855c69d
AB
2799
2800/* Implement hooks for collecting fundamental insn properties like if insn is
2801 an ASM or is within a SCHED_GROUP. */
2802
2803/* True when a "one-time init" data for INSN was already inited. */
2804static bool
2805first_time_insn_init (insn_t insn)
2806{
2807 return INSN_LIVE (insn) == NULL;
2808}
2809
2810/* Hash an entry in a transformed_insns hashtable. */
2811static hashval_t
2812hash_transformed_insns (const void *p)
2813{
2814 return VINSN_HASH_RTX (((const struct transformed_insns *) p)->vinsn_old);
2815}
2816
2817/* Compare the entries in a transformed_insns hashtable. */
2818static int
2819eq_transformed_insns (const void *p, const void *q)
2820{
2821 rtx i1 = VINSN_INSN_RTX (((const struct transformed_insns *) p)->vinsn_old);
2822 rtx i2 = VINSN_INSN_RTX (((const struct transformed_insns *) q)->vinsn_old);
2823
2824 if (INSN_UID (i1) == INSN_UID (i2))
2825 return 1;
2826 return rtx_equal_p (PATTERN (i1), PATTERN (i2));
2827}
2828
2829/* Free an entry in a transformed_insns hashtable. */
2830static void
2831free_transformed_insns (void *p)
2832{
2833 struct transformed_insns *pti = (struct transformed_insns *) p;
2834
2835 vinsn_detach (pti->vinsn_old);
2836 vinsn_detach (pti->vinsn_new);
2837 free (pti);
2838}
2839
b8698a0f 2840/* Init the s_i_d data for INSN which should be inited just once, when
e855c69d
AB
2841 we first see the insn. */
2842static void
2843init_first_time_insn_data (insn_t insn)
2844{
2845 /* This should not be set if this is the first time we init data for
2846 insn. */
2847 gcc_assert (first_time_insn_init (insn));
b8698a0f 2848
e855c69d
AB
2849 /* These are needed for nops too. */
2850 INSN_LIVE (insn) = get_regset_from_pool ();
2851 INSN_LIVE_VALID_P (insn) = false;
bcf33775 2852
e855c69d
AB
2853 if (!INSN_NOP_P (insn))
2854 {
2855 INSN_ANALYZED_DEPS (insn) = BITMAP_ALLOC (NULL);
2856 INSN_FOUND_DEPS (insn) = BITMAP_ALLOC (NULL);
b8698a0f 2857 INSN_TRANSFORMED_INSNS (insn)
e855c69d
AB
2858 = htab_create (16, hash_transformed_insns,
2859 eq_transformed_insns, free_transformed_insns);
bcf33775 2860 init_deps (&INSN_DEPS_CONTEXT (insn), true);
e855c69d
AB
2861 }
2862}
2863
b8698a0f 2864/* Free almost all above data for INSN that is scheduled already.
bcf33775
AB
2865 Used for extra-large basic blocks. */
2866void
2867free_data_for_scheduled_insn (insn_t insn)
e855c69d
AB
2868{
2869 gcc_assert (! first_time_insn_init (insn));
b8698a0f 2870
bcf33775
AB
2871 if (! INSN_ANALYZED_DEPS (insn))
2872 return;
b8698a0f 2873
e855c69d
AB
2874 BITMAP_FREE (INSN_ANALYZED_DEPS (insn));
2875 BITMAP_FREE (INSN_FOUND_DEPS (insn));
2876 htab_delete (INSN_TRANSFORMED_INSNS (insn));
b8698a0f 2877
e855c69d
AB
2878 /* This is allocated only for bookkeeping insns. */
2879 if (INSN_ORIGINATORS (insn))
2880 BITMAP_FREE (INSN_ORIGINATORS (insn));
2881 free_deps (&INSN_DEPS_CONTEXT (insn));
bcf33775
AB
2882
2883 INSN_ANALYZED_DEPS (insn) = NULL;
2884
b8698a0f 2885 /* Clear the readonly flag so we would ICE when trying to recalculate
bcf33775
AB
2886 the deps context (as we believe that it should not happen). */
2887 (&INSN_DEPS_CONTEXT (insn))->readonly = 0;
2888}
2889
2890/* Free the same data as above for INSN. */
2891static void
2892free_first_time_insn_data (insn_t insn)
2893{
2894 gcc_assert (! first_time_insn_init (insn));
2895
2896 free_data_for_scheduled_insn (insn);
2897 return_regset_to_pool (INSN_LIVE (insn));
2898 INSN_LIVE (insn) = NULL;
2899 INSN_LIVE_VALID_P (insn) = false;
e855c69d
AB
2900}
2901
2902/* Initialize region-scope data structures for basic blocks. */
2903static void
2904init_global_and_expr_for_bb (basic_block bb)
2905{
2906 if (sel_bb_empty_p (bb))
2907 return;
2908
2909 invalidate_av_set (bb);
2910}
2911
2912/* Data for global dependency analysis (to initialize CANT_MOVE and
2913 SCHED_GROUP_P). */
2914static struct
2915{
2916 /* Previous insn. */
2917 insn_t prev_insn;
2918} init_global_data;
2919
2920/* Determine if INSN is in the sched_group, is an asm or should not be
2921 cloned. After that initialize its expr. */
2922static void
2923init_global_and_expr_for_insn (insn_t insn)
2924{
2925 if (LABEL_P (insn))
2926 return;
2927
2928 if (NOTE_INSN_BASIC_BLOCK_P (insn))
2929 {
2930 init_global_data.prev_insn = NULL_RTX;
2931 return;
2932 }
2933
2934 gcc_assert (INSN_P (insn));
2935
2936 if (SCHED_GROUP_P (insn))
2937 /* Setup a sched_group. */
2938 {
2939 insn_t prev_insn = init_global_data.prev_insn;
2940
2941 if (prev_insn)
2942 INSN_SCHED_NEXT (prev_insn) = insn;
2943
2944 init_global_data.prev_insn = insn;
2945 }
2946 else
2947 init_global_data.prev_insn = NULL_RTX;
2948
2949 if (GET_CODE (PATTERN (insn)) == ASM_INPUT
2950 || asm_noperands (PATTERN (insn)) >= 0)
2951 /* Mark INSN as an asm. */
2952 INSN_ASM_P (insn) = true;
2953
2954 {
2955 bool force_unique_p;
2956 ds_t spec_done_ds;
2957
cfeb0fa8
AB
2958 /* Certain instructions cannot be cloned, and frame related insns and
2959 the insn adjacent to NOTE_INSN_EPILOGUE_BEG cannot be moved out of
2960 their block. */
2961 if (prologue_epilogue_contains (insn))
2962 {
2963 if (RTX_FRAME_RELATED_P (insn))
2964 CANT_MOVE (insn) = 1;
2965 else
2966 {
2967 rtx note;
2968 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2969 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE
2970 && ((enum insn_note) INTVAL (XEXP (note, 0))
2971 == NOTE_INSN_EPILOGUE_BEG))
2972 {
2973 CANT_MOVE (insn) = 1;
2974 break;
2975 }
2976 }
2977 force_unique_p = true;
2978 }
e855c69d 2979 else
cfeb0fa8
AB
2980 if (CANT_MOVE (insn)
2981 || INSN_ASM_P (insn)
2982 || SCHED_GROUP_P (insn)
07643d76 2983 || CALL_P (insn)
cfeb0fa8
AB
2984 /* Exception handling insns are always unique. */
2985 || (cfun->can_throw_non_call_exceptions && can_throw_internal (insn))
2986 /* TRAP_IF though have an INSN code is control_flow_insn_p (). */
6bf2d156
DM
2987 || control_flow_insn_p (insn)
2988 || volatile_insn_p (PATTERN (insn))
2989 || (targetm.cannot_copy_insn_p
2990 && targetm.cannot_copy_insn_p (insn)))
cfeb0fa8
AB
2991 force_unique_p = true;
2992 else
2993 force_unique_p = false;
e855c69d
AB
2994
2995 if (targetm.sched.get_insn_spec_ds)
2996 {
2997 spec_done_ds = targetm.sched.get_insn_spec_ds (insn);
2998 spec_done_ds = ds_get_max_dep_weak (spec_done_ds);
2999 }
3000 else
3001 spec_done_ds = 0;
3002
3003 /* Initialize INSN's expr. */
3004 init_expr (INSN_EXPR (insn), vinsn_create (insn, force_unique_p), 0,
3005 REG_BR_PROB_BASE, INSN_PRIORITY (insn), 0, BLOCK_NUM (insn),
b8698a0f 3006 spec_done_ds, 0, 0, NULL, true, false, false, false,
e855c69d
AB
3007 CANT_MOVE (insn));
3008 }
3009
3010 init_first_time_insn_data (insn);
3011}
3012
3013/* Scan the region and initialize instruction data for basic blocks BBS. */
3014void
3015sel_init_global_and_expr (bb_vec_t bbs)
3016{
3017 /* ??? It would be nice to implement push / pop scheme for sched_infos. */
3018 const struct sched_scan_info_def ssi =
3019 {
3020 NULL, /* extend_bb */
3021 init_global_and_expr_for_bb, /* init_bb */
3022 extend_insn_data, /* extend_insn */
3023 init_global_and_expr_for_insn /* init_insn */
3024 };
b8698a0f 3025
a95b23b4 3026 sched_scan (&ssi, bbs);
e855c69d
AB
3027}
3028
3029/* Finalize region-scope data structures for basic blocks. */
3030static void
3031finish_global_and_expr_for_bb (basic_block bb)
3032{
3033 av_set_clear (&BB_AV_SET (bb));
3034 BB_AV_LEVEL (bb) = 0;
3035}
3036
3037/* Finalize INSN's data. */
3038static void
3039finish_global_and_expr_insn (insn_t insn)
3040{
3041 if (LABEL_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
3042 return;
3043
3044 gcc_assert (INSN_P (insn));
3045
3046 if (INSN_LUID (insn) > 0)
3047 {
3048 free_first_time_insn_data (insn);
3049 INSN_WS_LEVEL (insn) = 0;
3050 CANT_MOVE (insn) = 0;
b8698a0f
L
3051
3052 /* We can no longer assert this, as vinsns of this insn could be
3053 easily live in other insn's caches. This should be changed to
e855c69d
AB
3054 a counter-like approach among all vinsns. */
3055 gcc_assert (true || VINSN_COUNT (INSN_VINSN (insn)) == 1);
3056 clear_expr (INSN_EXPR (insn));
3057 }
3058}
3059
3060/* Finalize per instruction data for the whole region. */
3061void
3062sel_finish_global_and_expr (void)
3063{
3064 {
3065 bb_vec_t bbs;
3066 int i;
3067
3068 bbs = VEC_alloc (basic_block, heap, current_nr_blocks);
3069
3070 for (i = 0; i < current_nr_blocks; i++)
3071 VEC_quick_push (basic_block, bbs, BASIC_BLOCK (BB_TO_BLOCK (i)));
3072
3073 /* Clear AV_SETs and INSN_EXPRs. */
3074 {
3075 const struct sched_scan_info_def ssi =
3076 {
3077 NULL, /* extend_bb */
3078 finish_global_and_expr_for_bb, /* init_bb */
3079 NULL, /* extend_insn */
3080 finish_global_and_expr_insn /* init_insn */
3081 };
3082
a95b23b4 3083 sched_scan (&ssi, bbs);
e855c69d
AB
3084 }
3085
3086 VEC_free (basic_block, heap, bbs);
3087 }
3088
3089 finish_insns ();
3090}
3091\f
3092
b8698a0f
L
3093/* In the below hooks, we merely calculate whether or not a dependence
3094 exists, and in what part of insn. However, we will need more data
e855c69d
AB
3095 when we'll start caching dependence requests. */
3096
3097/* Container to hold information for dependency analysis. */
3098static struct
3099{
3100 deps_t dc;
3101
3102 /* A variable to track which part of rtx we are scanning in
3103 sched-deps.c: sched_analyze_insn (). */
3104 deps_where_t where;
3105
3106 /* Current producer. */
3107 insn_t pro;
3108
3109 /* Current consumer. */
3110 vinsn_t con;
3111
3112 /* Is SEL_DEPS_HAS_DEP_P[DEPS_IN_X] is true, then X has a dependence.
3113 X is from { INSN, LHS, RHS }. */
3114 ds_t has_dep_p[DEPS_IN_NOWHERE];
3115} has_dependence_data;
3116
3117/* Start analyzing dependencies of INSN. */
3118static void
3119has_dependence_start_insn (insn_t insn ATTRIBUTE_UNUSED)
3120{
3121 gcc_assert (has_dependence_data.where == DEPS_IN_NOWHERE);
3122
3123 has_dependence_data.where = DEPS_IN_INSN;
3124}
3125
3126/* Finish analyzing dependencies of an insn. */
3127static void
3128has_dependence_finish_insn (void)
3129{
3130 gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
3131
3132 has_dependence_data.where = DEPS_IN_NOWHERE;
3133}
3134
3135/* Start analyzing dependencies of LHS. */
3136static void
3137has_dependence_start_lhs (rtx lhs ATTRIBUTE_UNUSED)
3138{
3139 gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
3140
3141 if (VINSN_LHS (has_dependence_data.con) != NULL)
3142 has_dependence_data.where = DEPS_IN_LHS;
3143}
3144
3145/* Finish analyzing dependencies of an lhs. */
3146static void
3147has_dependence_finish_lhs (void)
3148{
3149 has_dependence_data.where = DEPS_IN_INSN;
3150}
3151
3152/* Start analyzing dependencies of RHS. */
3153static void
3154has_dependence_start_rhs (rtx rhs ATTRIBUTE_UNUSED)
3155{
3156 gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
3157
3158 if (VINSN_RHS (has_dependence_data.con) != NULL)
3159 has_dependence_data.where = DEPS_IN_RHS;
3160}
3161
3162/* Start analyzing dependencies of an rhs. */
3163static void
3164has_dependence_finish_rhs (void)
3165{
3166 gcc_assert (has_dependence_data.where == DEPS_IN_RHS
3167 || has_dependence_data.where == DEPS_IN_INSN);
3168
3169 has_dependence_data.where = DEPS_IN_INSN;
3170}
3171
3172/* Note a set of REGNO. */
3173static void
3174has_dependence_note_reg_set (int regno)
3175{
3176 struct deps_reg *reg_last = &has_dependence_data.dc->reg_last[regno];
3177
3178 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3179 VINSN_INSN_RTX
3180 (has_dependence_data.con)))
3181 {
3182 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3183
3184 if (reg_last->sets != NULL
3185 || reg_last->clobbers != NULL)
3186 *dsp = (*dsp & ~SPECULATIVE) | DEP_OUTPUT;
3187
3188 if (reg_last->uses)
3189 *dsp = (*dsp & ~SPECULATIVE) | DEP_ANTI;
3190 }
3191}
3192
3193/* Note a clobber of REGNO. */
3194static void
3195has_dependence_note_reg_clobber (int regno)
3196{
3197 struct deps_reg *reg_last = &has_dependence_data.dc->reg_last[regno];
3198
3199 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3200 VINSN_INSN_RTX
3201 (has_dependence_data.con)))
3202 {
3203 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3204
3205 if (reg_last->sets)
3206 *dsp = (*dsp & ~SPECULATIVE) | DEP_OUTPUT;
b8698a0f 3207
e855c69d
AB
3208 if (reg_last->uses)
3209 *dsp = (*dsp & ~SPECULATIVE) | DEP_ANTI;
3210 }
3211}
3212
3213/* Note a use of REGNO. */
3214static void
3215has_dependence_note_reg_use (int regno)
3216{
3217 struct deps_reg *reg_last = &has_dependence_data.dc->reg_last[regno];
3218
3219 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3220 VINSN_INSN_RTX
3221 (has_dependence_data.con)))
3222 {
3223 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3224
3225 if (reg_last->sets)
3226 *dsp = (*dsp & ~SPECULATIVE) | DEP_TRUE;
3227
3228 if (reg_last->clobbers)
3229 *dsp = (*dsp & ~SPECULATIVE) | DEP_ANTI;
3230
0d4acd90
AB
3231 /* Merge BE_IN_SPEC bits into *DSP when the dependency producer
3232 is actually a check insn. We need to do this for any register
3233 read-read dependency with the check unless we track properly
3234 all registers written by BE_IN_SPEC-speculated insns, as
3235 we don't have explicit dependence lists. See PR 53975. */
e855c69d
AB
3236 if (reg_last->uses)
3237 {
3238 ds_t pro_spec_checked_ds;
3239
3240 pro_spec_checked_ds = INSN_SPEC_CHECKED_DS (has_dependence_data.pro);
3241 pro_spec_checked_ds = ds_get_max_dep_weak (pro_spec_checked_ds);
3242
0d4acd90 3243 if (pro_spec_checked_ds != 0)
e855c69d
AB
3244 *dsp = ds_full_merge (*dsp, pro_spec_checked_ds,
3245 NULL_RTX, NULL_RTX);
3246 }
3247 }
3248}
3249
3250/* Note a memory dependence. */
3251static void
3252has_dependence_note_mem_dep (rtx mem ATTRIBUTE_UNUSED,
3253 rtx pending_mem ATTRIBUTE_UNUSED,
3254 insn_t pending_insn ATTRIBUTE_UNUSED,
3255 ds_t ds ATTRIBUTE_UNUSED)
3256{
3257 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3258 VINSN_INSN_RTX (has_dependence_data.con)))
3259 {
3260 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3261
3262 *dsp = ds_full_merge (ds, *dsp, pending_mem, mem);
3263 }
3264}
3265
3266/* Note a dependence. */
3267static void
3268has_dependence_note_dep (insn_t pro ATTRIBUTE_UNUSED,
3269 ds_t ds ATTRIBUTE_UNUSED)
3270{
3271 if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
3272 VINSN_INSN_RTX (has_dependence_data.con)))
3273 {
3274 ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
3275
3276 *dsp = ds_full_merge (ds, *dsp, NULL_RTX, NULL_RTX);
3277 }
3278}
3279
3280/* Mark the insn as having a hard dependence that prevents speculation. */
3281void
3282sel_mark_hard_insn (rtx insn)
3283{
3284 int i;
3285
3286 /* Only work when we're in has_dependence_p mode.
3287 ??? This is a hack, this should actually be a hook. */
3288 if (!has_dependence_data.dc || !has_dependence_data.pro)
3289 return;
3290
3291 gcc_assert (insn == VINSN_INSN_RTX (has_dependence_data.con));
3292 gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
3293
3294 for (i = 0; i < DEPS_IN_NOWHERE; i++)
3295 has_dependence_data.has_dep_p[i] &= ~SPECULATIVE;
3296}
3297
3298/* This structure holds the hooks for the dependency analysis used when
3299 actually processing dependencies in the scheduler. */
3300static struct sched_deps_info_def has_dependence_sched_deps_info;
3301
3302/* This initializes most of the fields of the above structure. */
3303static const struct sched_deps_info_def const_has_dependence_sched_deps_info =
3304 {
3305 NULL,
3306
3307 has_dependence_start_insn,
3308 has_dependence_finish_insn,
3309 has_dependence_start_lhs,
3310 has_dependence_finish_lhs,
3311 has_dependence_start_rhs,
3312 has_dependence_finish_rhs,
3313 has_dependence_note_reg_set,
3314 has_dependence_note_reg_clobber,
3315 has_dependence_note_reg_use,
3316 has_dependence_note_mem_dep,
3317 has_dependence_note_dep,
3318
3319 0, /* use_cselib */
3320 0, /* use_deps_list */
3321 0 /* generate_spec_deps */
3322 };
3323
3324/* Initialize has_dependence_sched_deps_info with extra spec field. */
3325static void
3326setup_has_dependence_sched_deps_info (void)
3327{
3328 memcpy (&has_dependence_sched_deps_info,
3329 &const_has_dependence_sched_deps_info,
3330 sizeof (has_dependence_sched_deps_info));
3331
3332 if (spec_info != NULL)
3333 has_dependence_sched_deps_info.generate_spec_deps = 1;
3334
3335 sched_deps_info = &has_dependence_sched_deps_info;
3336}
3337
3338/* Remove all dependences found and recorded in has_dependence_data array. */
3339void
3340sel_clear_has_dependence (void)
3341{
3342 int i;
3343
3344 for (i = 0; i < DEPS_IN_NOWHERE; i++)
3345 has_dependence_data.has_dep_p[i] = 0;
3346}
3347
3348/* Return nonzero if EXPR has is dependent upon PRED. Return the pointer
3349 to the dependence information array in HAS_DEP_PP. */
3350ds_t
3351has_dependence_p (expr_t expr, insn_t pred, ds_t **has_dep_pp)
3352{
3353 int i;
3354 ds_t ds;
88302d54 3355 struct deps_desc *dc;
e855c69d
AB
3356
3357 if (INSN_SIMPLEJUMP_P (pred))
3358 /* Unconditional jump is just a transfer of control flow.
3359 Ignore it. */
3360 return false;
3361
3362 dc = &INSN_DEPS_CONTEXT (pred);
bcf33775
AB
3363
3364 /* We init this field lazily. */
3365 if (dc->reg_last == NULL)
3366 init_deps_reg_last (dc);
b8698a0f 3367
e855c69d
AB
3368 if (!dc->readonly)
3369 {
3370 has_dependence_data.pro = NULL;
3371 /* Initialize empty dep context with information about PRED. */
3372 advance_deps_context (dc, pred);
3373 dc->readonly = 1;
3374 }
3375
3376 has_dependence_data.where = DEPS_IN_NOWHERE;
3377 has_dependence_data.pro = pred;
3378 has_dependence_data.con = EXPR_VINSN (expr);
3379 has_dependence_data.dc = dc;
3380
3381 sel_clear_has_dependence ();
3382
3383 /* Now catch all dependencies that would be generated between PRED and
3384 INSN. */
3385 setup_has_dependence_sched_deps_info ();
3386 deps_analyze_insn (dc, EXPR_INSN_RTX (expr));
3387 has_dependence_data.dc = NULL;
3388
3389 /* When a barrier was found, set DEPS_IN_INSN bits. */
3390 if (dc->last_reg_pending_barrier == TRUE_BARRIER)
3391 has_dependence_data.has_dep_p[DEPS_IN_INSN] = DEP_TRUE;
3392 else if (dc->last_reg_pending_barrier == MOVE_BARRIER)
3393 has_dependence_data.has_dep_p[DEPS_IN_INSN] = DEP_ANTI;
3394
3395 /* Do not allow stores to memory to move through checks. Currently
3396 we don't move this to sched-deps.c as the check doesn't have
b8698a0f 3397 obvious places to which this dependence can be attached.
e855c69d
AB
3398 FIMXE: this should go to a hook. */
3399 if (EXPR_LHS (expr)
3400 && MEM_P (EXPR_LHS (expr))
3401 && sel_insn_is_speculation_check (pred))
3402 has_dependence_data.has_dep_p[DEPS_IN_INSN] = DEP_ANTI;
b8698a0f 3403
e855c69d
AB
3404 *has_dep_pp = has_dependence_data.has_dep_p;
3405 ds = 0;
3406 for (i = 0; i < DEPS_IN_NOWHERE; i++)
3407 ds = ds_full_merge (ds, has_dependence_data.has_dep_p[i],
3408 NULL_RTX, NULL_RTX);
3409
3410 return ds;
3411}
3412\f
3413
b8698a0f
L
3414/* Dependence hooks implementation that checks dependence latency constraints
3415 on the insns being scheduled. The entry point for these routines is
3416 tick_check_p predicate. */
e855c69d
AB
3417
3418static struct
3419{
3420 /* An expr we are currently checking. */
3421 expr_t expr;
3422
3423 /* A minimal cycle for its scheduling. */
3424 int cycle;
3425
3426 /* Whether we have seen a true dependence while checking. */
3427 bool seen_true_dep_p;
3428} tick_check_data;
3429
3430/* Update minimal scheduling cycle for tick_check_insn given that it depends
3431 on PRO with status DS and weight DW. */
3432static void
3433tick_check_dep_with_dw (insn_t pro_insn, ds_t ds, dw_t dw)
3434{
3435 expr_t con_expr = tick_check_data.expr;
3436 insn_t con_insn = EXPR_INSN_RTX (con_expr);
3437
3438 if (con_insn != pro_insn)
3439 {
3440 enum reg_note dt;
3441 int tick;
3442
3443 if (/* PROducer was removed from above due to pipelining. */
3444 !INSN_IN_STREAM_P (pro_insn)
3445 /* Or PROducer was originally on the next iteration regarding the
3446 CONsumer. */
3447 || (INSN_SCHED_TIMES (pro_insn)
3448 - EXPR_SCHED_TIMES (con_expr)) > 1)
3449 /* Don't count this dependence. */
3450 return;
3451
3452 dt = ds_to_dt (ds);
3453 if (dt == REG_DEP_TRUE)
3454 tick_check_data.seen_true_dep_p = true;
3455
3456 gcc_assert (INSN_SCHED_CYCLE (pro_insn) > 0);
3457
3458 {
3459 dep_def _dep, *dep = &_dep;
3460
3461 init_dep (dep, pro_insn, con_insn, dt);
3462
3463 tick = INSN_SCHED_CYCLE (pro_insn) + dep_cost_1 (dep, dw);
3464 }
3465
3466 /* When there are several kinds of dependencies between pro and con,
3467 only REG_DEP_TRUE should be taken into account. */
3468 if (tick > tick_check_data.cycle
3469 && (dt == REG_DEP_TRUE || !tick_check_data.seen_true_dep_p))
3470 tick_check_data.cycle = tick;
3471 }
3472}
3473
3474/* An implementation of note_dep hook. */
3475static void
3476tick_check_note_dep (insn_t pro, ds_t ds)
3477{
3478 tick_check_dep_with_dw (pro, ds, 0);
3479}
3480
3481/* An implementation of note_mem_dep hook. */
3482static void
3483tick_check_note_mem_dep (rtx mem1, rtx mem2, insn_t pro, ds_t ds)
3484{
3485 dw_t dw;
3486
3487 dw = (ds_to_dt (ds) == REG_DEP_TRUE
3488 ? estimate_dep_weak (mem1, mem2)
3489 : 0);
3490
3491 tick_check_dep_with_dw (pro, ds, dw);
3492}
3493
3494/* This structure contains hooks for dependence analysis used when determining
3495 whether an insn is ready for scheduling. */
3496static struct sched_deps_info_def tick_check_sched_deps_info =
3497 {
3498 NULL,
3499
3500 NULL,
3501 NULL,
3502 NULL,
3503 NULL,
3504 NULL,
3505 NULL,
3506 haifa_note_reg_set,
3507 haifa_note_reg_clobber,
3508 haifa_note_reg_use,
3509 tick_check_note_mem_dep,
3510 tick_check_note_dep,
3511
3512 0, 0, 0
3513 };
3514
3515/* Estimate number of cycles from the current cycle of FENCE until EXPR can be
3516 scheduled. Return 0 if all data from producers in DC is ready. */
3517int
3518tick_check_p (expr_t expr, deps_t dc, fence_t fence)
3519{
3520 int cycles_left;
3521 /* Initialize variables. */
3522 tick_check_data.expr = expr;
3523 tick_check_data.cycle = 0;
3524 tick_check_data.seen_true_dep_p = false;
3525 sched_deps_info = &tick_check_sched_deps_info;
b8698a0f 3526
e855c69d
AB
3527 gcc_assert (!dc->readonly);
3528 dc->readonly = 1;
3529 deps_analyze_insn (dc, EXPR_INSN_RTX (expr));
3530 dc->readonly = 0;
3531
3532 cycles_left = tick_check_data.cycle - FENCE_CYCLE (fence);
3533
3534 return cycles_left >= 0 ? cycles_left : 0;
3535}
3536\f
3537
3538/* Functions to work with insns. */
3539
3540/* Returns true if LHS of INSN is the same as DEST of an insn
3541 being moved. */
3542bool
3543lhs_of_insn_equals_to_dest_p (insn_t insn, rtx dest)
3544{
3545 rtx lhs = INSN_LHS (insn);
3546
3547 if (lhs == NULL || dest == NULL)
3548 return false;
b8698a0f 3549
e855c69d
AB
3550 return rtx_equal_p (lhs, dest);
3551}
3552
3553/* Return s_i_d entry of INSN. Callable from debugger. */
3554sel_insn_data_def
3555insn_sid (insn_t insn)
3556{
3557 return *SID (insn);
3558}
3559
3560/* True when INSN is a speculative check. We can tell this by looking
3561 at the data structures of the selective scheduler, not by examining
3562 the pattern. */
3563bool
3564sel_insn_is_speculation_check (rtx insn)
3565{
3566 return s_i_d && !! INSN_SPEC_CHECKED_DS (insn);
3567}
3568
b8698a0f 3569/* Extracts machine mode MODE and destination location DST_LOC
e855c69d
AB
3570 for given INSN. */
3571void
3572get_dest_and_mode (rtx insn, rtx *dst_loc, enum machine_mode *mode)
3573{
3574 rtx pat = PATTERN (insn);
3575
3576 gcc_assert (dst_loc);
3577 gcc_assert (GET_CODE (pat) == SET);
3578
3579 *dst_loc = SET_DEST (pat);
3580
3581 gcc_assert (*dst_loc);
3582 gcc_assert (MEM_P (*dst_loc) || REG_P (*dst_loc));
3583
3584 if (mode)
3585 *mode = GET_MODE (*dst_loc);
3586}
3587
b8698a0f 3588/* Returns true when moving through JUMP will result in bookkeeping
e855c69d
AB
3589 creation. */
3590bool
3591bookkeeping_can_be_created_if_moved_through_p (insn_t jump)
3592{
3593 insn_t succ;
3594 succ_iterator si;
3595
3596 FOR_EACH_SUCC (succ, si, jump)
3597 if (sel_num_cfg_preds_gt_1 (succ))
3598 return true;
3599
3600 return false;
3601}
3602
3603/* Return 'true' if INSN is the only one in its basic block. */
3604static bool
3605insn_is_the_only_one_in_bb_p (insn_t insn)
3606{
3607 return sel_bb_head_p (insn) && sel_bb_end_p (insn);
3608}
3609
3610#ifdef ENABLE_CHECKING
b8698a0f 3611/* Check that the region we're scheduling still has at most one
e855c69d
AB
3612 backedge. */
3613static void
3614verify_backedges (void)
3615{
3616 if (pipelining_p)
3617 {
3618 int i, n = 0;
3619 edge e;
3620 edge_iterator ei;
b8698a0f 3621
e855c69d
AB
3622 for (i = 0; i < current_nr_blocks; i++)
3623 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (BB_TO_BLOCK (i))->succs)
3624 if (in_current_region_p (e->dest)
3625 && BLOCK_TO_BB (e->dest->index) < i)
3626 n++;
b8698a0f 3627
e855c69d
AB
3628 gcc_assert (n <= 1);
3629 }
3630}
3631#endif
3632\f
3633
3634/* Functions to work with control flow. */
3635
b59ab570
AM
3636/* Recompute BLOCK_TO_BB and BB_FOR_BLOCK for current region so that blocks
3637 are sorted in topological order (it might have been invalidated by
3638 redirecting an edge). */
3639static void
3640sel_recompute_toporder (void)
3641{
3642 int i, n, rgn;
3643 int *postorder, n_blocks;
3644
3645 postorder = XALLOCAVEC (int, n_basic_blocks);
3646 n_blocks = post_order_compute (postorder, false, false);
3647
3648 rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
3649 for (n = 0, i = n_blocks - 1; i >= 0; i--)
3650 if (CONTAINING_RGN (postorder[i]) == rgn)
3651 {
3652 BLOCK_TO_BB (postorder[i]) = n;
3653 BB_TO_BLOCK (n) = postorder[i];
3654 n++;
3655 }
3656
3657 /* Assert that we updated info for all blocks. We may miss some blocks if
3658 this function is called when redirecting an edge made a block
3659 unreachable, but that block is not deleted yet. */
3660 gcc_assert (n == RGN_NR_BLOCKS (rgn));
3661}
3662
e855c69d 3663/* Tidy the possibly empty block BB. */
65592aad 3664static bool
5f33b972 3665maybe_tidy_empty_bb (basic_block bb)
e855c69d 3666{
b7b5540a 3667 basic_block succ_bb, pred_bb, note_bb;
00c4e97c 3668 VEC (basic_block, heap) *dom_bbs;
f2c45f08
AM
3669 edge e;
3670 edge_iterator ei;
e855c69d
AB
3671 bool rescan_p;
3672
3673 /* Keep empty bb only if this block immediately precedes EXIT and
762bffba
AB
3674 has incoming non-fallthrough edge, or it has no predecessors or
3675 successors. Otherwise remove it. */
b5b8b0ac 3676 if (!sel_bb_empty_p (bb)
b8698a0f 3677 || (single_succ_p (bb)
e855c69d 3678 && single_succ (bb) == EXIT_BLOCK_PTR
b8698a0f 3679 && (!single_pred_p (bb)
762bffba
AB
3680 || !(single_pred_edge (bb)->flags & EDGE_FALLTHRU)))
3681 || EDGE_COUNT (bb->preds) == 0
3682 || EDGE_COUNT (bb->succs) == 0)
e855c69d
AB
3683 return false;
3684
f2c45f08
AM
3685 /* Do not attempt to redirect complex edges. */
3686 FOR_EACH_EDGE (e, ei, bb->preds)
3687 if (e->flags & EDGE_COMPLEX)
3688 return false;
3689
e855c69d
AB
3690 free_data_sets (bb);
3691
3692 /* Do not delete BB if it has more than one successor.
3693 That can occur when we moving a jump. */
3694 if (!single_succ_p (bb))
3695 {
3696 gcc_assert (can_merge_blocks_p (bb->prev_bb, bb));
3697 sel_merge_blocks (bb->prev_bb, bb);
3698 return true;
3699 }
3700
3701 succ_bb = single_succ (bb);
3702 rescan_p = true;
3703 pred_bb = NULL;
00c4e97c 3704 dom_bbs = NULL;
e855c69d 3705
b7b5540a
AB
3706 /* Save a pred/succ from the current region to attach the notes to. */
3707 note_bb = NULL;
3708 FOR_EACH_EDGE (e, ei, bb->preds)
3709 if (in_current_region_p (e->src))
3710 {
3711 note_bb = e->src;
3712 break;
3713 }
3714 if (note_bb == NULL)
3715 note_bb = succ_bb;
3716
e855c69d
AB
3717 /* Redirect all non-fallthru edges to the next bb. */
3718 while (rescan_p)
3719 {
e855c69d
AB
3720 rescan_p = false;
3721
3722 FOR_EACH_EDGE (e, ei, bb->preds)
3723 {
3724 pred_bb = e->src;
3725
3726 if (!(e->flags & EDGE_FALLTHRU))
3727 {
5f33b972 3728 /* We can not invalidate computed topological order by moving
00c4e97c
AB
3729 the edge destination block (E->SUCC) along a fallthru edge.
3730
3731 We will update dominators here only when we'll get
3732 an unreachable block when redirecting, otherwise
3733 sel_redirect_edge_and_branch will take care of it. */
3734 if (e->dest != bb
3735 && single_pred_p (e->dest))
3736 VEC_safe_push (basic_block, heap, dom_bbs, e->dest);
5f33b972 3737 sel_redirect_edge_and_branch (e, succ_bb);
e855c69d
AB
3738 rescan_p = true;
3739 break;
3740 }
5f33b972
AM
3741 /* If the edge is fallthru, but PRED_BB ends in a conditional jump
3742 to BB (so there is no non-fallthru edge from PRED_BB to BB), we
3743 still have to adjust it. */
3744 else if (single_succ_p (pred_bb) && any_condjump_p (BB_END (pred_bb)))
3745 {
3746 /* If possible, try to remove the unneeded conditional jump. */
3747 if (INSN_SCHED_TIMES (BB_END (pred_bb)) == 0
3748 && !IN_CURRENT_FENCE_P (BB_END (pred_bb)))
3749 {
3750 if (!sel_remove_insn (BB_END (pred_bb), false, false))
3751 tidy_fallthru_edge (e);
3752 }
3753 else
3754 sel_redirect_edge_and_branch (e, succ_bb);
3755 rescan_p = true;
3756 break;
3757 }
e855c69d
AB
3758 }
3759 }
3760
e855c69d
AB
3761 if (can_merge_blocks_p (bb->prev_bb, bb))
3762 sel_merge_blocks (bb->prev_bb, bb);
3763 else
e855c69d 3764 {
262d8232 3765 /* This is a block without fallthru predecessor. Just delete it. */
b7b5540a
AB
3766 gcc_assert (note_bb);
3767 move_bb_info (note_bb, bb);
e855c69d
AB
3768 remove_empty_bb (bb, true);
3769 }
3770
00c4e97c
AB
3771 if (!VEC_empty (basic_block, dom_bbs))
3772 {
3773 VEC_safe_push (basic_block, heap, dom_bbs, succ_bb);
3774 iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, false);
3775 VEC_free (basic_block, heap, dom_bbs);
3776 }
3777
e855c69d
AB
3778 return true;
3779}
3780
b8698a0f 3781/* Tidy the control flow after we have removed original insn from
e855c69d
AB
3782 XBB. Return true if we have removed some blocks. When FULL_TIDYING
3783 is true, also try to optimize control flow on non-empty blocks. */
3784bool
3785tidy_control_flow (basic_block xbb, bool full_tidying)
3786{
3787 bool changed = true;
b5b8b0ac 3788 insn_t first, last;
b8698a0f 3789
e855c69d 3790 /* First check whether XBB is empty. */
5f33b972 3791 changed = maybe_tidy_empty_bb (xbb);
e855c69d
AB
3792 if (changed || !full_tidying)
3793 return changed;
b8698a0f 3794
e855c69d 3795 /* Check if there is a unnecessary jump after insn left. */
753de8cf 3796 if (bb_has_removable_jump_to_p (xbb, xbb->next_bb)
e855c69d
AB
3797 && INSN_SCHED_TIMES (BB_END (xbb)) == 0
3798 && !IN_CURRENT_FENCE_P (BB_END (xbb)))
3799 {
3800 if (sel_remove_insn (BB_END (xbb), false, false))
3801 return true;
3802 tidy_fallthru_edge (EDGE_SUCC (xbb, 0));
3803 }
3804
b5b8b0ac
AO
3805 first = sel_bb_head (xbb);
3806 last = sel_bb_end (xbb);
3807 if (MAY_HAVE_DEBUG_INSNS)
3808 {
3809 if (first != last && DEBUG_INSN_P (first))
3810 do
3811 first = NEXT_INSN (first);
3812 while (first != last && (DEBUG_INSN_P (first) || NOTE_P (first)));
3813
3814 if (first != last && DEBUG_INSN_P (last))
3815 do
3816 last = PREV_INSN (last);
3817 while (first != last && (DEBUG_INSN_P (last) || NOTE_P (last)));
3818 }
e855c69d 3819 /* Check if there is an unnecessary jump in previous basic block leading
b8698a0f
L
3820 to next basic block left after removing INSN from stream.
3821 If it is so, remove that jump and redirect edge to current
3822 basic block (where there was INSN before deletion). This way
3823 when NOP will be deleted several instructions later with its
3824 basic block we will not get a jump to next instruction, which
e855c69d 3825 can be harmful. */
b5b8b0ac 3826 if (first == last
e855c69d 3827 && !sel_bb_empty_p (xbb)
b5b8b0ac 3828 && INSN_NOP_P (last)
e855c69d
AB
3829 /* Flow goes fallthru from current block to the next. */
3830 && EDGE_COUNT (xbb->succs) == 1
3831 && (EDGE_SUCC (xbb, 0)->flags & EDGE_FALLTHRU)
3832 /* When successor is an EXIT block, it may not be the next block. */
3833 && single_succ (xbb) != EXIT_BLOCK_PTR
3834 /* And unconditional jump in previous basic block leads to
3835 next basic block of XBB and this jump can be safely removed. */
3836 && in_current_region_p (xbb->prev_bb)
753de8cf 3837 && bb_has_removable_jump_to_p (xbb->prev_bb, xbb->next_bb)
e855c69d
AB
3838 && INSN_SCHED_TIMES (BB_END (xbb->prev_bb)) == 0
3839 /* Also this jump is not at the scheduling boundary. */
3840 && !IN_CURRENT_FENCE_P (BB_END (xbb->prev_bb)))
3841 {
b59ab570 3842 bool recompute_toporder_p;
e855c69d
AB
3843 /* Clear data structures of jump - jump itself will be removed
3844 by sel_redirect_edge_and_branch. */
3845 clear_expr (INSN_EXPR (BB_END (xbb->prev_bb)));
b59ab570
AM
3846 recompute_toporder_p
3847 = sel_redirect_edge_and_branch (EDGE_SUCC (xbb->prev_bb, 0), xbb);
3848
e855c69d
AB
3849 gcc_assert (EDGE_SUCC (xbb->prev_bb, 0)->flags & EDGE_FALLTHRU);
3850
3851 /* It can turn out that after removing unused jump, basic block
3852 that contained that jump, becomes empty too. In such case
3853 remove it too. */
3854 if (sel_bb_empty_p (xbb->prev_bb))
5f33b972
AM
3855 changed = maybe_tidy_empty_bb (xbb->prev_bb);
3856 if (recompute_toporder_p)
b59ab570 3857 sel_recompute_toporder ();
e855c69d 3858 }
d787f788
AM
3859
3860#ifdef ENABLE_CHECKING
3861 verify_backedges ();
00c4e97c 3862 verify_dominators (CDI_DOMINATORS);
d787f788
AM
3863#endif
3864
e855c69d
AB
3865 return changed;
3866}
3867
b59ab570
AM
3868/* Purge meaningless empty blocks in the middle of a region. */
3869void
3870purge_empty_blocks (void)
3871{
9d0dcda1 3872 int i;
b59ab570 3873
9d0dcda1
AM
3874 /* Do not attempt to delete the first basic block in the region. */
3875 for (i = 1; i < current_nr_blocks; )
b59ab570
AM
3876 {
3877 basic_block b = BASIC_BLOCK (BB_TO_BLOCK (i));
3878
5f33b972 3879 if (maybe_tidy_empty_bb (b))
b59ab570
AM
3880 continue;
3881
3882 i++;
3883 }
3884}
3885
b8698a0f
L
3886/* Rip-off INSN from the insn stream. When ONLY_DISCONNECT is true,
3887 do not delete insn's data, because it will be later re-emitted.
e855c69d
AB
3888 Return true if we have removed some blocks afterwards. */
3889bool
3890sel_remove_insn (insn_t insn, bool only_disconnect, bool full_tidying)
3891{
3892 basic_block bb = BLOCK_FOR_INSN (insn);
3893
3894 gcc_assert (INSN_IN_STREAM_P (insn));
3895
b5b8b0ac
AO
3896 if (DEBUG_INSN_P (insn) && BB_AV_SET_VALID_P (bb))
3897 {
3898 expr_t expr;
3899 av_set_iterator i;
3900
3901 /* When we remove a debug insn that is head of a BB, it remains
3902 in the AV_SET of the block, but it shouldn't. */
3903 FOR_EACH_EXPR_1 (expr, i, &BB_AV_SET (bb))
3904 if (EXPR_INSN_RTX (expr) == insn)
3905 {
3906 av_set_iter_remove (&i);
3907 break;
3908 }
3909 }
3910
e855c69d
AB
3911 if (only_disconnect)
3912 {
3913 insn_t prev = PREV_INSN (insn);
3914 insn_t next = NEXT_INSN (insn);
3915 basic_block bb = BLOCK_FOR_INSN (insn);
3916
3917 NEXT_INSN (prev) = next;
3918 PREV_INSN (next) = prev;
3919
3920 if (BB_HEAD (bb) == insn)
3921 {
3922 gcc_assert (BLOCK_FOR_INSN (prev) == bb);
3923 BB_HEAD (bb) = prev;
3924 }
3925 if (BB_END (bb) == insn)
3926 BB_END (bb) = prev;
3927 }
3928 else
3929 {
3930 remove_insn (insn);
3931 clear_expr (INSN_EXPR (insn));
3932 }
3933
3934 /* It is necessary to null this fields before calling add_insn (). */
3935 PREV_INSN (insn) = NULL_RTX;
3936 NEXT_INSN (insn) = NULL_RTX;
3937
3938 return tidy_control_flow (bb, full_tidying);
3939}
3940
3941/* Estimate number of the insns in BB. */
3942static int
3943sel_estimate_number_of_insns (basic_block bb)
3944{
3945 int res = 0;
3946 insn_t insn = NEXT_INSN (BB_HEAD (bb)), next_tail = NEXT_INSN (BB_END (bb));
3947
3948 for (; insn != next_tail; insn = NEXT_INSN (insn))
b5b8b0ac 3949 if (NONDEBUG_INSN_P (insn))
e855c69d
AB
3950 res++;
3951
3952 return res;
3953}
3954
3955/* We don't need separate luids for notes or labels. */
3956static int
3957sel_luid_for_non_insn (rtx x)
3958{
3959 gcc_assert (NOTE_P (x) || LABEL_P (x));
3960
3961 return -1;
3962}
3963
0d9439b0
SG
3964/* Find the proper seqno for inserting at INSN by successors.
3965 Return -1 if no successors with positive seqno exist. */
e855c69d 3966static int
0d9439b0
SG
3967get_seqno_by_succs (rtx insn)
3968{
3969 basic_block bb = BLOCK_FOR_INSN (insn);
3970 rtx tmp = insn, end = BB_END (bb);
3971 int seqno;
3972 insn_t succ = NULL;
3973 succ_iterator si;
3974
3975 while (tmp != end)
3976 {
3977 tmp = NEXT_INSN (tmp);
3978 if (INSN_P (tmp))
3979 return INSN_SEQNO (tmp);
3980 }
3981
3982 seqno = INT_MAX;
3983
3984 FOR_EACH_SUCC_1 (succ, si, end, SUCCS_NORMAL)
3985 if (INSN_SEQNO (succ) > 0)
3986 seqno = MIN (seqno, INSN_SEQNO (succ));
3987
3988 if (seqno == INT_MAX)
3989 return -1;
3990
3991 return seqno;
3992}
3993
3994/* Compute seqno for INSN by its preds or succs. */
3995static int
3996get_seqno_for_a_jump (insn_t insn)
e855c69d
AB
3997{
3998 int seqno;
3999
4000 gcc_assert (INSN_SIMPLEJUMP_P (insn));
4001
4002 if (!sel_bb_head_p (insn))
4003 seqno = INSN_SEQNO (PREV_INSN (insn));
4004 else
4005 {
4006 basic_block bb = BLOCK_FOR_INSN (insn);
4007
4008 if (single_pred_p (bb)
4009 && !in_current_region_p (single_pred (bb)))
4010 {
4011 /* We can have preds outside a region when splitting edges
b8698a0f 4012 for pipelining of an outer loop. Use succ instead.
e855c69d
AB
4013 There should be only one of them. */
4014 insn_t succ = NULL;
4015 succ_iterator si;
4016 bool first = true;
b8698a0f 4017
e855c69d
AB
4018 gcc_assert (flag_sel_sched_pipelining_outer_loops
4019 && current_loop_nest);
b8698a0f 4020 FOR_EACH_SUCC_1 (succ, si, insn,
e855c69d
AB
4021 SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
4022 {
4023 gcc_assert (first);
4024 first = false;
4025 }
4026
4027 gcc_assert (succ != NULL);
4028 seqno = INSN_SEQNO (succ);
4029 }
4030 else
4031 {
4032 insn_t *preds;
4033 int n;
4034
4035 cfg_preds (BLOCK_FOR_INSN (insn), &preds, &n);
e855c69d 4036
0d9439b0
SG
4037 gcc_assert (n > 0);
4038 /* For one predecessor, use simple method. */
4039 if (n == 1)
4040 seqno = INSN_SEQNO (preds[0]);
4041 else
4042 seqno = get_seqno_by_preds (insn);
b8698a0f 4043
e855c69d
AB
4044 free (preds);
4045 }
4046 }
4047
0d9439b0
SG
4048 /* We were unable to find a good seqno among preds. */
4049 if (seqno < 0)
4050 seqno = get_seqno_by_succs (insn);
4051
4052 gcc_assert (seqno >= 0);
4053
e855c69d
AB
4054 return seqno;
4055}
4056
da7ba240
AB
4057/* Find the proper seqno for inserting at INSN. Returns -1 if no predecessors
4058 with positive seqno exist. */
e855c69d
AB
4059int
4060get_seqno_by_preds (rtx insn)
4061{
4062 basic_block bb = BLOCK_FOR_INSN (insn);
4063 rtx tmp = insn, head = BB_HEAD (bb);
4064 insn_t *preds;
4065 int n, i, seqno;
4066
4067 while (tmp != head)
0d9439b0 4068 {
e855c69d 4069 tmp = PREV_INSN (tmp);
0d9439b0
SG
4070 if (INSN_P (tmp))
4071 return INSN_SEQNO (tmp);
4072 }
b8698a0f 4073
e855c69d
AB
4074 cfg_preds (bb, &preds, &n);
4075 for (i = 0, seqno = -1; i < n; i++)
4076 seqno = MAX (seqno, INSN_SEQNO (preds[i]));
4077
e855c69d
AB
4078 return seqno;
4079}
4080
4081\f
4082
4083/* Extend pass-scope data structures for basic blocks. */
4084void
4085sel_extend_global_bb_info (void)
4086{
4087 VEC_safe_grow_cleared (sel_global_bb_info_def, heap, sel_global_bb_info,
4088 last_basic_block);
4089}
4090
4091/* Extend region-scope data structures for basic blocks. */
4092static void
4093extend_region_bb_info (void)
4094{
4095 VEC_safe_grow_cleared (sel_region_bb_info_def, heap, sel_region_bb_info,
4096 last_basic_block);
4097}
4098
4099/* Extend all data structures to fit for all basic blocks. */
4100static void
4101extend_bb_info (void)
4102{
4103 sel_extend_global_bb_info ();
4104 extend_region_bb_info ();
4105}
4106
4107/* Finalize pass-scope data structures for basic blocks. */
4108void
4109sel_finish_global_bb_info (void)
4110{
4111 VEC_free (sel_global_bb_info_def, heap, sel_global_bb_info);
4112}
4113
4114/* Finalize region-scope data structures for basic blocks. */
4115static void
4116finish_region_bb_info (void)
4117{
4118 VEC_free (sel_region_bb_info_def, heap, sel_region_bb_info);
4119}
4120\f
4121
4122/* Data for each insn in current region. */
4123VEC (sel_insn_data_def, heap) *s_i_d = NULL;
4124
e855c69d
AB
4125/* Extend data structures for insns from current region. */
4126static void
4127extend_insn_data (void)
4128{
4129 int reserve;
b8698a0f 4130
e855c69d
AB
4131 sched_extend_target ();
4132 sched_deps_init (false);
4133
4134 /* Extend data structures for insns from current region. */
b8698a0f 4135 reserve = (sched_max_luid + 1
e855c69d 4136 - VEC_length (sel_insn_data_def, s_i_d));
b8698a0f 4137 if (reserve > 0
e855c69d 4138 && ! VEC_space (sel_insn_data_def, s_i_d, reserve))
bcf33775
AB
4139 {
4140 int size;
4141
4142 if (sched_max_luid / 2 > 1024)
4143 size = sched_max_luid + 1024;
4144 else
4145 size = 3 * sched_max_luid / 2;
b8698a0f 4146
bcf33775
AB
4147
4148 VEC_safe_grow_cleared (sel_insn_data_def, heap, s_i_d, size);
4149 }
e855c69d
AB
4150}
4151
4152/* Finalize data structures for insns from current region. */
4153static void
4154finish_insns (void)
4155{
4156 unsigned i;
4157
4158 /* Clear here all dependence contexts that may have left from insns that were
4159 removed during the scheduling. */
4160 for (i = 0; i < VEC_length (sel_insn_data_def, s_i_d); i++)
4161 {
0823efed 4162 sel_insn_data_def *sid_entry = &VEC_index (sel_insn_data_def, s_i_d, i);
b8698a0f 4163
e855c69d
AB
4164 if (sid_entry->live)
4165 return_regset_to_pool (sid_entry->live);
4166 if (sid_entry->analyzed_deps)
4167 {
4168 BITMAP_FREE (sid_entry->analyzed_deps);
4169 BITMAP_FREE (sid_entry->found_deps);
4170 htab_delete (sid_entry->transformed_insns);
4171 free_deps (&sid_entry->deps_context);
4172 }
4173 if (EXPR_VINSN (&sid_entry->expr))
4174 {
4175 clear_expr (&sid_entry->expr);
b8698a0f 4176
e855c69d
AB
4177 /* Also, clear CANT_MOVE bit here, because we really don't want it
4178 to be passed to the next region. */
4179 CANT_MOVE_BY_LUID (i) = 0;
4180 }
4181 }
b8698a0f 4182
e855c69d
AB
4183 VEC_free (sel_insn_data_def, heap, s_i_d);
4184}
4185
4186/* A proxy to pass initialization data to init_insn (). */
4187static sel_insn_data_def _insn_init_ssid;
4188static sel_insn_data_t insn_init_ssid = &_insn_init_ssid;
4189
4190/* If true create a new vinsn. Otherwise use the one from EXPR. */
4191static bool insn_init_create_new_vinsn_p;
4192
4193/* Set all necessary data for initialization of the new insn[s]. */
4194static expr_t
4195set_insn_init (expr_t expr, vinsn_t vi, int seqno)
4196{
4197 expr_t x = &insn_init_ssid->expr;
4198
4199 copy_expr_onside (x, expr);
4200 if (vi != NULL)
4201 {
4202 insn_init_create_new_vinsn_p = false;
4203 change_vinsn_in_expr (x, vi);
4204 }
4205 else
4206 insn_init_create_new_vinsn_p = true;
4207
4208 insn_init_ssid->seqno = seqno;
4209 return x;
4210}
4211
4212/* Init data for INSN. */
4213static void
4214init_insn_data (insn_t insn)
4215{
4216 expr_t expr;
4217 sel_insn_data_t ssid = insn_init_ssid;
4218
4219 /* The fields mentioned below are special and hence are not being
4220 propagated to the new insns. */
4221 gcc_assert (!ssid->asm_p && ssid->sched_next == NULL
4222 && !ssid->after_stall_p && ssid->sched_cycle == 0);
4223 gcc_assert (INSN_P (insn) && INSN_LUID (insn) > 0);
4224
4225 expr = INSN_EXPR (insn);
4226 copy_expr (expr, &ssid->expr);
4227 prepare_insn_expr (insn, ssid->seqno);
4228
4229 if (insn_init_create_new_vinsn_p)
4230 change_vinsn_in_expr (expr, vinsn_create (insn, init_insn_force_unique_p));
b8698a0f 4231
e855c69d
AB
4232 if (first_time_insn_init (insn))
4233 init_first_time_insn_data (insn);
4234}
4235
4236/* This is used to initialize spurious jumps generated by
4237 sel_redirect_edge (). */
4238static void
4239init_simplejump_data (insn_t insn)
4240{
4241 init_expr (INSN_EXPR (insn), vinsn_create (insn, false), 0,
b8698a0f 4242 REG_BR_PROB_BASE, 0, 0, 0, 0, 0, 0, NULL, true, false, false,
e855c69d 4243 false, true);
0d9439b0 4244 INSN_SEQNO (insn) = get_seqno_for_a_jump (insn);
e855c69d
AB
4245 init_first_time_insn_data (insn);
4246}
4247
b8698a0f 4248/* Perform deferred initialization of insns. This is used to process
e855c69d
AB
4249 a new jump that may be created by redirect_edge. */
4250void
4251sel_init_new_insn (insn_t insn, int flags)
4252{
4253 /* We create data structures for bb when the first insn is emitted in it. */
4254 if (INSN_P (insn)
4255 && INSN_IN_STREAM_P (insn)
4256 && insn_is_the_only_one_in_bb_p (insn))
4257 {
4258 extend_bb_info ();
4259 create_initial_data_sets (BLOCK_FOR_INSN (insn));
4260 }
b8698a0f 4261
e855c69d 4262 if (flags & INSN_INIT_TODO_LUID)
a95b23b4
BS
4263 {
4264 sched_extend_luids ();
4265 sched_init_insn_luid (insn);
4266 }
e855c69d
AB
4267
4268 if (flags & INSN_INIT_TODO_SSID)
4269 {
4270 extend_insn_data ();
4271 init_insn_data (insn);
4272 clear_expr (&insn_init_ssid->expr);
4273 }
4274
4275 if (flags & INSN_INIT_TODO_SIMPLEJUMP)
4276 {
4277 extend_insn_data ();
4278 init_simplejump_data (insn);
4279 }
b8698a0f 4280
e855c69d
AB
4281 gcc_assert (CONTAINING_RGN (BLOCK_NUM (insn))
4282 == CONTAINING_RGN (BB_TO_BLOCK (0)));
4283}
4284\f
4285
4286/* Functions to init/finish work with lv sets. */
4287
4288/* Init BB_LV_SET of BB from DF_LR_IN set of BB. */
4289static void
4290init_lv_set (basic_block bb)
4291{
4292 gcc_assert (!BB_LV_SET_VALID_P (bb));
4293
4294 BB_LV_SET (bb) = get_regset_from_pool ();
b8698a0f 4295 COPY_REG_SET (BB_LV_SET (bb), DF_LR_IN (bb));
e855c69d
AB
4296 BB_LV_SET_VALID_P (bb) = true;
4297}
4298
4299/* Copy liveness information to BB from FROM_BB. */
4300static void
4301copy_lv_set_from (basic_block bb, basic_block from_bb)
4302{
4303 gcc_assert (!BB_LV_SET_VALID_P (bb));
b8698a0f 4304
e855c69d
AB
4305 COPY_REG_SET (BB_LV_SET (bb), BB_LV_SET (from_bb));
4306 BB_LV_SET_VALID_P (bb) = true;
b8698a0f 4307}
e855c69d
AB
4308
4309/* Initialize lv set of all bb headers. */
4310void
4311init_lv_sets (void)
4312{
4313 basic_block bb;
4314
4315 /* Initialize of LV sets. */
4316 FOR_EACH_BB (bb)
4317 init_lv_set (bb);
4318
4319 /* Don't forget EXIT_BLOCK. */
4320 init_lv_set (EXIT_BLOCK_PTR);
4321}
4322
4323/* Release lv set of HEAD. */
4324static void
4325free_lv_set (basic_block bb)
4326{
4327 gcc_assert (BB_LV_SET (bb) != NULL);
4328
4329 return_regset_to_pool (BB_LV_SET (bb));
4330 BB_LV_SET (bb) = NULL;
4331 BB_LV_SET_VALID_P (bb) = false;
4332}
4333
4334/* Finalize lv sets of all bb headers. */
4335void
4336free_lv_sets (void)
4337{
4338 basic_block bb;
4339
4340 /* Don't forget EXIT_BLOCK. */
4341 free_lv_set (EXIT_BLOCK_PTR);
4342
4343 /* Free LV sets. */
4344 FOR_EACH_BB (bb)
4345 if (BB_LV_SET (bb))
4346 free_lv_set (bb);
4347}
4348
5c416724
DM
4349/* Mark AV_SET for BB as invalid, so this set will be updated the next time
4350 compute_av() processes BB. This function is called when creating new basic
4351 blocks, as well as for blocks (either new or existing) where new jumps are
4352 created when the control flow is being updated. */
e855c69d
AB
4353static void
4354invalidate_av_set (basic_block bb)
4355{
e855c69d
AB
4356 BB_AV_LEVEL (bb) = -1;
4357}
4358
4359/* Create initial data sets for BB (they will be invalid). */
4360static void
4361create_initial_data_sets (basic_block bb)
4362{
4363 if (BB_LV_SET (bb))
4364 BB_LV_SET_VALID_P (bb) = false;
4365 else
4366 BB_LV_SET (bb) = get_regset_from_pool ();
4367 invalidate_av_set (bb);
4368}
4369
4370/* Free av set of BB. */
4371static void
4372free_av_set (basic_block bb)
4373{
4374 av_set_clear (&BB_AV_SET (bb));
4375 BB_AV_LEVEL (bb) = 0;
4376}
4377
4378/* Free data sets of BB. */
4379void
4380free_data_sets (basic_block bb)
4381{
4382 free_lv_set (bb);
4383 free_av_set (bb);
4384}
4385
4386/* Exchange lv sets of TO and FROM. */
4387static void
4388exchange_lv_sets (basic_block to, basic_block from)
4389{
4390 {
4391 regset to_lv_set = BB_LV_SET (to);
4392
4393 BB_LV_SET (to) = BB_LV_SET (from);
4394 BB_LV_SET (from) = to_lv_set;
4395 }
4396
4397 {
4398 bool to_lv_set_valid_p = BB_LV_SET_VALID_P (to);
4399
4400 BB_LV_SET_VALID_P (to) = BB_LV_SET_VALID_P (from);
4401 BB_LV_SET_VALID_P (from) = to_lv_set_valid_p;
4402 }
4403}
4404
4405
4406/* Exchange av sets of TO and FROM. */
4407static void
4408exchange_av_sets (basic_block to, basic_block from)
4409{
4410 {
4411 av_set_t to_av_set = BB_AV_SET (to);
4412
4413 BB_AV_SET (to) = BB_AV_SET (from);
4414 BB_AV_SET (from) = to_av_set;
4415 }
4416
4417 {
4418 int to_av_level = BB_AV_LEVEL (to);
4419
4420 BB_AV_LEVEL (to) = BB_AV_LEVEL (from);
4421 BB_AV_LEVEL (from) = to_av_level;
4422 }
4423}
4424
4425/* Exchange data sets of TO and FROM. */
4426void
4427exchange_data_sets (basic_block to, basic_block from)
4428{
4429 exchange_lv_sets (to, from);
4430 exchange_av_sets (to, from);
4431}
4432
4433/* Copy data sets of FROM to TO. */
4434void
4435copy_data_sets (basic_block to, basic_block from)
4436{
4437 gcc_assert (!BB_LV_SET_VALID_P (to) && !BB_AV_SET_VALID_P (to));
4438 gcc_assert (BB_AV_SET (to) == NULL);
4439
4440 BB_AV_LEVEL (to) = BB_AV_LEVEL (from);
4441 BB_LV_SET_VALID_P (to) = BB_LV_SET_VALID_P (from);
4442
4443 if (BB_AV_SET_VALID_P (from))
4444 {
4445 BB_AV_SET (to) = av_set_copy (BB_AV_SET (from));
4446 }
4447 if (BB_LV_SET_VALID_P (from))
4448 {
4449 gcc_assert (BB_LV_SET (to) != NULL);
4450 COPY_REG_SET (BB_LV_SET (to), BB_LV_SET (from));
4451 }
4452}
4453
4454/* Return an av set for INSN, if any. */
4455av_set_t
4456get_av_set (insn_t insn)
4457{
4458 av_set_t av_set;
4459
4460 gcc_assert (AV_SET_VALID_P (insn));
4461
4462 if (sel_bb_head_p (insn))
4463 av_set = BB_AV_SET (BLOCK_FOR_INSN (insn));
4464 else
4465 av_set = NULL;
4466
4467 return av_set;
4468}
4469
4470/* Implementation of AV_LEVEL () macro. Return AV_LEVEL () of INSN. */
4471int
4472get_av_level (insn_t insn)
4473{
4474 int av_level;
4475
4476 gcc_assert (INSN_P (insn));
4477
4478 if (sel_bb_head_p (insn))
4479 av_level = BB_AV_LEVEL (BLOCK_FOR_INSN (insn));
4480 else
4481 av_level = INSN_WS_LEVEL (insn);
4482
4483 return av_level;
4484}
4485
4486\f
4487
4488/* Variables to work with control-flow graph. */
4489
4490/* The basic block that already has been processed by the sched_data_update (),
4491 but hasn't been in sel_add_bb () yet. */
4492static VEC (basic_block, heap) *last_added_blocks = NULL;
4493
4494/* A pool for allocating successor infos. */
4495static struct
4496{
4497 /* A stack for saving succs_info structures. */
4498 struct succs_info *stack;
4499
4500 /* Its size. */
4501 int size;
4502
4503 /* Top of the stack. */
4504 int top;
4505
4506 /* Maximal value of the top. */
4507 int max_top;
4508} succs_info_pool;
4509
4510/* Functions to work with control-flow graph. */
4511
4512/* Return basic block note of BB. */
4513insn_t
4514sel_bb_head (basic_block bb)
4515{
4516 insn_t head;
4517
4518 if (bb == EXIT_BLOCK_PTR)
4519 {
4520 gcc_assert (exit_insn != NULL_RTX);
4521 head = exit_insn;
4522 }
4523 else
4524 {
4525 insn_t note;
4526
4527 note = bb_note (bb);
4528 head = next_nonnote_insn (note);
4529
89ad0f25 4530 if (head && (BARRIER_P (head) || BLOCK_FOR_INSN (head) != bb))
e855c69d
AB
4531 head = NULL_RTX;
4532 }
4533
4534 return head;
4535}
4536
4537/* Return true if INSN is a basic block header. */
4538bool
4539sel_bb_head_p (insn_t insn)
4540{
4541 return sel_bb_head (BLOCK_FOR_INSN (insn)) == insn;
4542}
4543
4544/* Return last insn of BB. */
4545insn_t
4546sel_bb_end (basic_block bb)
4547{
4548 if (sel_bb_empty_p (bb))
4549 return NULL_RTX;
4550
4551 gcc_assert (bb != EXIT_BLOCK_PTR);
4552
4553 return BB_END (bb);
4554}
4555
4556/* Return true if INSN is the last insn in its basic block. */
4557bool
4558sel_bb_end_p (insn_t insn)
4559{
4560 return insn == sel_bb_end (BLOCK_FOR_INSN (insn));
4561}
4562
4563/* Return true if BB consist of single NOTE_INSN_BASIC_BLOCK. */
4564bool
4565sel_bb_empty_p (basic_block bb)
4566{
4567 return sel_bb_head (bb) == NULL;
4568}
4569
4570/* True when BB belongs to the current scheduling region. */
4571bool
4572in_current_region_p (basic_block bb)
4573{
4574 if (bb->index < NUM_FIXED_BLOCKS)
4575 return false;
4576
4577 return CONTAINING_RGN (bb->index) == CONTAINING_RGN (BB_TO_BLOCK (0));
4578}
4579
4580/* Return the block which is a fallthru bb of a conditional jump JUMP. */
4581basic_block
4582fallthru_bb_of_jump (rtx jump)
4583{
4584 if (!JUMP_P (jump))
4585 return NULL;
4586
e855c69d
AB
4587 if (!any_condjump_p (jump))
4588 return NULL;
4589
268bab85
AB
4590 /* A basic block that ends with a conditional jump may still have one successor
4591 (and be followed by a barrier), we are not interested. */
4592 if (single_succ_p (BLOCK_FOR_INSN (jump)))
4593 return NULL;
4594
e855c69d
AB
4595 return FALLTHRU_EDGE (BLOCK_FOR_INSN (jump))->dest;
4596}
4597
4598/* Remove all notes from BB. */
4599static void
4600init_bb (basic_block bb)
4601{
4602 remove_notes (bb_note (bb), BB_END (bb));
4603 BB_NOTE_LIST (bb) = note_list;
4604}
4605
4606void
a95b23b4 4607sel_init_bbs (bb_vec_t bbs)
e855c69d
AB
4608{
4609 const struct sched_scan_info_def ssi =
4610 {
4611 extend_bb_info, /* extend_bb */
4612 init_bb, /* init_bb */
4613 NULL, /* extend_insn */
4614 NULL /* init_insn */
4615 };
4616
a95b23b4 4617 sched_scan (&ssi, bbs);
e855c69d
AB
4618}
4619
7898b93b 4620/* Restore notes for the whole region. */
e855c69d 4621static void
7898b93b 4622sel_restore_notes (void)
e855c69d
AB
4623{
4624 int bb;
7898b93b 4625 insn_t insn;
e855c69d
AB
4626
4627 for (bb = 0; bb < current_nr_blocks; bb++)
4628 {
4629 basic_block first, last;
4630
4631 first = EBB_FIRST_BB (bb);
4632 last = EBB_LAST_BB (bb)->next_bb;
4633
4634 do
4635 {
4636 note_list = BB_NOTE_LIST (first);
4637 restore_other_notes (NULL, first);
4638 BB_NOTE_LIST (first) = NULL_RTX;
4639
7898b93b
AM
4640 FOR_BB_INSNS (first, insn)
4641 if (NONDEBUG_INSN_P (insn))
4642 reemit_notes (insn);
4643
e855c69d
AB
4644 first = first->next_bb;
4645 }
4646 while (first != last);
4647 }
4648}
4649
4650/* Free per-bb data structures. */
4651void
4652sel_finish_bbs (void)
4653{
7898b93b 4654 sel_restore_notes ();
e855c69d
AB
4655
4656 /* Remove current loop preheader from this loop. */
4657 if (current_loop_nest)
4658 sel_remove_loop_preheader ();
4659
4660 finish_region_bb_info ();
4661}
4662
4663/* Return true if INSN has a single successor of type FLAGS. */
4664bool
4665sel_insn_has_single_succ_p (insn_t insn, int flags)
4666{
4667 insn_t succ;
4668 succ_iterator si;
4669 bool first_p = true;
4670
4671 FOR_EACH_SUCC_1 (succ, si, insn, flags)
4672 {
4673 if (first_p)
4674 first_p = false;
4675 else
4676 return false;
4677 }
4678
4679 return true;
4680}
4681
4682/* Allocate successor's info. */
4683static struct succs_info *
4684alloc_succs_info (void)
4685{
4686 if (succs_info_pool.top == succs_info_pool.max_top)
4687 {
4688 int i;
b8698a0f 4689
e855c69d
AB
4690 if (++succs_info_pool.max_top >= succs_info_pool.size)
4691 gcc_unreachable ();
4692
4693 i = ++succs_info_pool.top;
4694 succs_info_pool.stack[i].succs_ok = VEC_alloc (rtx, heap, 10);
4695 succs_info_pool.stack[i].succs_other = VEC_alloc (rtx, heap, 10);
4696 succs_info_pool.stack[i].probs_ok = VEC_alloc (int, heap, 10);
4697 }
4698 else
4699 succs_info_pool.top++;
4700
4701 return &succs_info_pool.stack[succs_info_pool.top];
4702}
4703
4704/* Free successor's info. */
4705void
4706free_succs_info (struct succs_info * sinfo)
4707{
b8698a0f 4708 gcc_assert (succs_info_pool.top >= 0
e855c69d
AB
4709 && &succs_info_pool.stack[succs_info_pool.top] == sinfo);
4710 succs_info_pool.top--;
4711
4712 /* Clear stale info. */
b8698a0f 4713 VEC_block_remove (rtx, sinfo->succs_ok,
e855c69d 4714 0, VEC_length (rtx, sinfo->succs_ok));
b8698a0f 4715 VEC_block_remove (rtx, sinfo->succs_other,
e855c69d 4716 0, VEC_length (rtx, sinfo->succs_other));
b8698a0f 4717 VEC_block_remove (int, sinfo->probs_ok,
e855c69d
AB
4718 0, VEC_length (int, sinfo->probs_ok));
4719 sinfo->all_prob = 0;
4720 sinfo->succs_ok_n = 0;
4721 sinfo->all_succs_n = 0;
4722}
4723
b8698a0f 4724/* Compute successor info for INSN. FLAGS are the flags passed
e855c69d
AB
4725 to the FOR_EACH_SUCC_1 iterator. */
4726struct succs_info *
4727compute_succs_info (insn_t insn, short flags)
4728{
4729 succ_iterator si;
4730 insn_t succ;
4731 struct succs_info *sinfo = alloc_succs_info ();
4732
4733 /* Traverse *all* successors and decide what to do with each. */
4734 FOR_EACH_SUCC_1 (succ, si, insn, SUCCS_ALL)
4735 {
4736 /* FIXME: this doesn't work for skipping to loop exits, as we don't
4737 perform code motion through inner loops. */
4738 short current_flags = si.current_flags & ~SUCCS_SKIP_TO_LOOP_EXITS;
4739
4740 if (current_flags & flags)
4741 {
4742 VEC_safe_push (rtx, heap, sinfo->succs_ok, succ);
4743 VEC_safe_push (int, heap, sinfo->probs_ok,
b8698a0f 4744 /* FIXME: Improve calculation when skipping
e855c69d 4745 inner loop to exits. */
b8698a0f
L
4746 (si.bb_end
4747 ? si.e1->probability
e855c69d
AB
4748 : REG_BR_PROB_BASE));
4749 sinfo->succs_ok_n++;
4750 }
4751 else
4752 VEC_safe_push (rtx, heap, sinfo->succs_other, succ);
4753
4754 /* Compute all_prob. */
4755 if (!si.bb_end)
4756 sinfo->all_prob = REG_BR_PROB_BASE;
4757 else
4758 sinfo->all_prob += si.e1->probability;
4759
4760 sinfo->all_succs_n++;
4761 }
4762
4763 return sinfo;
4764}
4765
b8698a0f 4766/* Return the predecessors of BB in PREDS and their number in N.
e855c69d
AB
4767 Empty blocks are skipped. SIZE is used to allocate PREDS. */
4768static void
4769cfg_preds_1 (basic_block bb, insn_t **preds, int *n, int *size)
4770{
4771 edge e;
4772 edge_iterator ei;
4773
4774 gcc_assert (BLOCK_TO_BB (bb->index) != 0);
4775
4776 FOR_EACH_EDGE (e, ei, bb->preds)
4777 {
4778 basic_block pred_bb = e->src;
4779 insn_t bb_end = BB_END (pred_bb);
4780
3e6a3f6f
AB
4781 if (!in_current_region_p (pred_bb))
4782 {
4783 gcc_assert (flag_sel_sched_pipelining_outer_loops
4784 && current_loop_nest);
4785 continue;
4786 }
e855c69d
AB
4787
4788 if (sel_bb_empty_p (pred_bb))
4789 cfg_preds_1 (pred_bb, preds, n, size);
4790 else
4791 {
4792 if (*n == *size)
b8698a0f 4793 *preds = XRESIZEVEC (insn_t, *preds,
e855c69d
AB
4794 (*size = 2 * *size + 1));
4795 (*preds)[(*n)++] = bb_end;
4796 }
4797 }
4798
3e6a3f6f
AB
4799 gcc_assert (*n != 0
4800 || (flag_sel_sched_pipelining_outer_loops
4801 && current_loop_nest));
e855c69d
AB
4802}
4803
b8698a0f
L
4804/* Find all predecessors of BB and record them in PREDS and their number
4805 in N. Empty blocks are skipped, and only normal (forward in-region)
e855c69d
AB
4806 edges are processed. */
4807static void
4808cfg_preds (basic_block bb, insn_t **preds, int *n)
4809{
4810 int size = 0;
4811
4812 *preds = NULL;
4813 *n = 0;
4814 cfg_preds_1 (bb, preds, n, &size);
4815}
4816
4817/* Returns true if we are moving INSN through join point. */
4818bool
4819sel_num_cfg_preds_gt_1 (insn_t insn)
4820{
4821 basic_block bb;
4822
4823 if (!sel_bb_head_p (insn) || INSN_BB (insn) == 0)
4824 return false;
4825
4826 bb = BLOCK_FOR_INSN (insn);
4827
4828 while (1)
4829 {
4830 if (EDGE_COUNT (bb->preds) > 1)
4831 return true;
4832
4833 gcc_assert (EDGE_PRED (bb, 0)->dest == bb);
4834 bb = EDGE_PRED (bb, 0)->src;
4835
4836 if (!sel_bb_empty_p (bb))
4837 break;
4838 }
4839
4840 return false;
4841}
4842
b8698a0f 4843/* Returns true when BB should be the end of an ebb. Adapted from the
e855c69d
AB
4844 code in sched-ebb.c. */
4845bool
4846bb_ends_ebb_p (basic_block bb)
4847{
4848 basic_block next_bb = bb_next_bb (bb);
4849 edge e;
b8698a0f 4850
e855c69d
AB
4851 if (next_bb == EXIT_BLOCK_PTR
4852 || bitmap_bit_p (forced_ebb_heads, next_bb->index)
4853 || (LABEL_P (BB_HEAD (next_bb))
4854 /* NB: LABEL_NUSES () is not maintained outside of jump.c.
4855 Work around that. */
4856 && !single_pred_p (next_bb)))
4857 return true;
4858
4859 if (!in_current_region_p (next_bb))
4860 return true;
4861
0fd4b31d
NF
4862 e = find_fallthru_edge (bb->succs);
4863 if (e)
4864 {
4865 gcc_assert (e->dest == next_bb);
4866
4867 return false;
4868 }
e855c69d
AB
4869
4870 return true;
4871}
4872
4873/* Returns true when INSN and SUCC are in the same EBB, given that SUCC is a
4874 successor of INSN. */
4875bool
4876in_same_ebb_p (insn_t insn, insn_t succ)
4877{
4878 basic_block ptr = BLOCK_FOR_INSN (insn);
4879
4880 for(;;)
4881 {
4882 if (ptr == BLOCK_FOR_INSN (succ))
4883 return true;
b8698a0f 4884
e855c69d
AB
4885 if (bb_ends_ebb_p (ptr))
4886 return false;
4887
4888 ptr = bb_next_bb (ptr);
4889 }
4890
4891 gcc_unreachable ();
4892 return false;
4893}
4894
4895/* Recomputes the reverse topological order for the function and
4896 saves it in REV_TOP_ORDER_INDEX. REV_TOP_ORDER_INDEX_LEN is also
4897 modified appropriately. */
4898static void
4899recompute_rev_top_order (void)
4900{
4901 int *postorder;
4902 int n_blocks, i;
4903
4904 if (!rev_top_order_index || rev_top_order_index_len < last_basic_block)
4905 {
b8698a0f 4906 rev_top_order_index_len = last_basic_block;
e855c69d
AB
4907 rev_top_order_index = XRESIZEVEC (int, rev_top_order_index,
4908 rev_top_order_index_len);
4909 }
4910
4911 postorder = XNEWVEC (int, n_basic_blocks);
4912
4913 n_blocks = post_order_compute (postorder, true, false);
4914 gcc_assert (n_basic_blocks == n_blocks);
4915
4916 /* Build reverse function: for each basic block with BB->INDEX == K
4917 rev_top_order_index[K] is it's reverse topological sort number. */
4918 for (i = 0; i < n_blocks; i++)
4919 {
4920 gcc_assert (postorder[i] < rev_top_order_index_len);
4921 rev_top_order_index[postorder[i]] = i;
4922 }
4923
4924 free (postorder);
4925}
4926
4927/* Clear all flags from insns in BB that could spoil its rescheduling. */
4928void
4929clear_outdated_rtx_info (basic_block bb)
4930{
4931 rtx insn;
4932
4933 FOR_BB_INSNS (bb, insn)
4934 if (INSN_P (insn))
4935 {
4936 SCHED_GROUP_P (insn) = 0;
4937 INSN_AFTER_STALL_P (insn) = 0;
4938 INSN_SCHED_TIMES (insn) = 0;
4939 EXPR_PRIORITY_ADJ (INSN_EXPR (insn)) = 0;
4940
4941 /* We cannot use the changed caches, as previously we could ignore
b8698a0f 4942 the LHS dependence due to enabled renaming and transform
e855c69d
AB
4943 the expression, and currently we'll be unable to do this. */
4944 htab_empty (INSN_TRANSFORMED_INSNS (insn));
4945 }
4946}
4947
4948/* Add BB_NOTE to the pool of available basic block notes. */
4949static void
4950return_bb_to_pool (basic_block bb)
4951{
4952 rtx note = bb_note (bb);
4953
4954 gcc_assert (NOTE_BASIC_BLOCK (note) == bb
4955 && bb->aux == NULL);
4956
4957 /* It turns out that current cfg infrastructure does not support
4958 reuse of basic blocks. Don't bother for now. */
4959 /*VEC_safe_push (rtx, heap, bb_note_pool, note);*/
4960}
4961
4962/* Get a bb_note from pool or return NULL_RTX if pool is empty. */
4963static rtx
4964get_bb_note_from_pool (void)
4965{
4966 if (VEC_empty (rtx, bb_note_pool))
4967 return NULL_RTX;
4968 else
4969 {
4970 rtx note = VEC_pop (rtx, bb_note_pool);
4971
4972 PREV_INSN (note) = NULL_RTX;
4973 NEXT_INSN (note) = NULL_RTX;
4974
4975 return note;
4976 }
4977}
4978
4979/* Free bb_note_pool. */
4980void
4981free_bb_note_pool (void)
4982{
4983 VEC_free (rtx, heap, bb_note_pool);
4984}
4985
4986/* Setup scheduler pool and successor structure. */
4987void
4988alloc_sched_pools (void)
4989{
4990 int succs_size;
4991
4992 succs_size = MAX_WS + 1;
b8698a0f 4993 succs_info_pool.stack = XCNEWVEC (struct succs_info, succs_size);
e855c69d
AB
4994 succs_info_pool.size = succs_size;
4995 succs_info_pool.top = -1;
4996 succs_info_pool.max_top = -1;
4997
b8698a0f 4998 sched_lists_pool = create_alloc_pool ("sel-sched-lists",
e855c69d
AB
4999 sizeof (struct _list_node), 500);
5000}
5001
5002/* Free the pools. */
5003void
5004free_sched_pools (void)
5005{
5006 int i;
b8698a0f 5007
e855c69d
AB
5008 free_alloc_pool (sched_lists_pool);
5009 gcc_assert (succs_info_pool.top == -1);
5010 for (i = 0; i < succs_info_pool.max_top; i++)
5011 {
5012 VEC_free (rtx, heap, succs_info_pool.stack[i].succs_ok);
5013 VEC_free (rtx, heap, succs_info_pool.stack[i].succs_other);
5014 VEC_free (int, heap, succs_info_pool.stack[i].probs_ok);
5015 }
5016 free (succs_info_pool.stack);
5017}
5018\f
5019
b8698a0f 5020/* Returns a position in RGN where BB can be inserted retaining
e855c69d
AB
5021 topological order. */
5022static int
5023find_place_to_insert_bb (basic_block bb, int rgn)
5024{
5025 bool has_preds_outside_rgn = false;
5026 edge e;
5027 edge_iterator ei;
b8698a0f 5028
e855c69d
AB
5029 /* Find whether we have preds outside the region. */
5030 FOR_EACH_EDGE (e, ei, bb->preds)
5031 if (!in_current_region_p (e->src))
5032 {
5033 has_preds_outside_rgn = true;
5034 break;
5035 }
b8698a0f 5036
e855c69d
AB
5037 /* Recompute the top order -- needed when we have > 1 pred
5038 and in case we don't have preds outside. */
5039 if (flag_sel_sched_pipelining_outer_loops
5040 && (has_preds_outside_rgn || EDGE_COUNT (bb->preds) > 1))
5041 {
5042 int i, bbi = bb->index, cur_bbi;
5043
5044 recompute_rev_top_order ();
5045 for (i = RGN_NR_BLOCKS (rgn) - 1; i >= 0; i--)
5046 {
5047 cur_bbi = BB_TO_BLOCK (i);
b8698a0f 5048 if (rev_top_order_index[bbi]
e855c69d
AB
5049 < rev_top_order_index[cur_bbi])
5050 break;
5051 }
b8698a0f 5052
073a8998 5053 /* We skipped the right block, so we increase i. We accommodate
e855c69d
AB
5054 it for increasing by step later, so we decrease i. */
5055 return (i + 1) - 1;
5056 }
5057 else if (has_preds_outside_rgn)
5058 {
5059 /* This is the case when we generate an extra empty block
5060 to serve as region head during pipelining. */
5061 e = EDGE_SUCC (bb, 0);
5062 gcc_assert (EDGE_COUNT (bb->succs) == 1
5063 && in_current_region_p (EDGE_SUCC (bb, 0)->dest)
5064 && (BLOCK_TO_BB (e->dest->index) == 0));
5065 return -1;
5066 }
5067
5068 /* We don't have preds outside the region. We should have
5069 the only pred, because the multiple preds case comes from
5070 the pipelining of outer loops, and that is handled above.
5071 Just take the bbi of this single pred. */
5072 if (EDGE_COUNT (bb->succs) > 0)
5073 {
5074 int pred_bbi;
b8698a0f 5075
e855c69d 5076 gcc_assert (EDGE_COUNT (bb->preds) == 1);
b8698a0f 5077
e855c69d
AB
5078 pred_bbi = EDGE_PRED (bb, 0)->src->index;
5079 return BLOCK_TO_BB (pred_bbi);
5080 }
5081 else
5082 /* BB has no successors. It is safe to put it in the end. */
5083 return current_nr_blocks - 1;
5084}
5085
5086/* Deletes an empty basic block freeing its data. */
5087static void
5088delete_and_free_basic_block (basic_block bb)
5089{
5090 gcc_assert (sel_bb_empty_p (bb));
5091
5092 if (BB_LV_SET (bb))
5093 free_lv_set (bb);
5094
5095 bitmap_clear_bit (blocks_to_reschedule, bb->index);
5096
b8698a0f
L
5097 /* Can't assert av_set properties because we use sel_aremove_bb
5098 when removing loop preheader from the region. At the point of
e855c69d
AB
5099 removing the preheader we already have deallocated sel_region_bb_info. */
5100 gcc_assert (BB_LV_SET (bb) == NULL
5101 && !BB_LV_SET_VALID_P (bb)
5102 && BB_AV_LEVEL (bb) == 0
5103 && BB_AV_SET (bb) == NULL);
b8698a0f 5104
e855c69d
AB
5105 delete_basic_block (bb);
5106}
5107
5108/* Add BB to the current region and update the region data. */
5109static void
5110add_block_to_current_region (basic_block bb)
5111{
5112 int i, pos, bbi = -2, rgn;
5113
5114 rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
5115 bbi = find_place_to_insert_bb (bb, rgn);
5116 bbi += 1;
5117 pos = RGN_BLOCKS (rgn) + bbi;
5118
5119 gcc_assert (RGN_HAS_REAL_EBB (rgn) == 0
5120 && ebb_head[bbi] == pos);
b8698a0f 5121
e855c69d
AB
5122 /* Make a place for the new block. */
5123 extend_regions ();
5124
5125 for (i = RGN_BLOCKS (rgn + 1) - 1; i >= pos; i--)
5126 BLOCK_TO_BB (rgn_bb_table[i])++;
b8698a0f 5127
e855c69d
AB
5128 memmove (rgn_bb_table + pos + 1,
5129 rgn_bb_table + pos,
5130 (RGN_BLOCKS (nr_regions) - pos) * sizeof (*rgn_bb_table));
5131
5132 /* Initialize data for BB. */
5133 rgn_bb_table[pos] = bb->index;
5134 BLOCK_TO_BB (bb->index) = bbi;
5135 CONTAINING_RGN (bb->index) = rgn;
5136
5137 RGN_NR_BLOCKS (rgn)++;
b8698a0f 5138
e855c69d
AB
5139 for (i = rgn + 1; i <= nr_regions; i++)
5140 RGN_BLOCKS (i)++;
5141}
5142
5143/* Remove BB from the current region and update the region data. */
5144static void
5145remove_bb_from_region (basic_block bb)
5146{
5147 int i, pos, bbi = -2, rgn;
5148
5149 rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
5150 bbi = BLOCK_TO_BB (bb->index);
5151 pos = RGN_BLOCKS (rgn) + bbi;
5152
5153 gcc_assert (RGN_HAS_REAL_EBB (rgn) == 0
5154 && ebb_head[bbi] == pos);
5155
5156 for (i = RGN_BLOCKS (rgn + 1) - 1; i >= pos; i--)
5157 BLOCK_TO_BB (rgn_bb_table[i])--;
5158
5159 memmove (rgn_bb_table + pos,
5160 rgn_bb_table + pos + 1,
5161 (RGN_BLOCKS (nr_regions) - pos) * sizeof (*rgn_bb_table));
5162
5163 RGN_NR_BLOCKS (rgn)--;
5164 for (i = rgn + 1; i <= nr_regions; i++)
5165 RGN_BLOCKS (i)--;
5166}
5167
b8698a0f 5168/* Add BB to the current region and update all data. If BB is NULL, add all
e855c69d
AB
5169 blocks from last_added_blocks vector. */
5170static void
5171sel_add_bb (basic_block bb)
5172{
5173 /* Extend luids so that new notes will receive zero luids. */
a95b23b4 5174 sched_extend_luids ();
e855c69d 5175 sched_init_bbs ();
a95b23b4 5176 sel_init_bbs (last_added_blocks);
e855c69d 5177
b8698a0f 5178 /* When bb is passed explicitly, the vector should contain
e855c69d
AB
5179 the only element that equals to bb; otherwise, the vector
5180 should not be NULL. */
5181 gcc_assert (last_added_blocks != NULL);
b8698a0f 5182
e855c69d
AB
5183 if (bb != NULL)
5184 {
5185 gcc_assert (VEC_length (basic_block, last_added_blocks) == 1
b8698a0f
L
5186 && VEC_index (basic_block,
5187 last_added_blocks, 0) == bb);
e855c69d
AB
5188 add_block_to_current_region (bb);
5189
5190 /* We associate creating/deleting data sets with the first insn
5191 appearing / disappearing in the bb. */
5192 if (!sel_bb_empty_p (bb) && BB_LV_SET (bb) == NULL)
5193 create_initial_data_sets (bb);
b8698a0f 5194
e855c69d
AB
5195 VEC_free (basic_block, heap, last_added_blocks);
5196 }
5197 else
5198 /* BB is NULL - process LAST_ADDED_BLOCKS instead. */
5199 {
5200 int i;
5201 basic_block temp_bb = NULL;
5202
b8698a0f 5203 for (i = 0;
e855c69d
AB
5204 VEC_iterate (basic_block, last_added_blocks, i, bb); i++)
5205 {
5206 add_block_to_current_region (bb);
5207 temp_bb = bb;
5208 }
5209
b8698a0f 5210 /* We need to fetch at least one bb so we know the region
e855c69d
AB
5211 to update. */
5212 gcc_assert (temp_bb != NULL);
5213 bb = temp_bb;
5214
5215 VEC_free (basic_block, heap, last_added_blocks);
5216 }
5217
5218 rgn_setup_region (CONTAINING_RGN (bb->index));
5219}
5220
b8698a0f 5221/* Remove BB from the current region and update all data.
e855c69d
AB
5222 If REMOVE_FROM_CFG_PBB is true, also remove the block cfom cfg. */
5223static void
5224sel_remove_bb (basic_block bb, bool remove_from_cfg_p)
5225{
262d8232
AB
5226 unsigned idx = bb->index;
5227
e855c69d 5228 gcc_assert (bb != NULL && BB_NOTE_LIST (bb) == NULL_RTX);
b8698a0f 5229
e855c69d
AB
5230 remove_bb_from_region (bb);
5231 return_bb_to_pool (bb);
262d8232 5232 bitmap_clear_bit (blocks_to_reschedule, idx);
b8698a0f 5233
e855c69d 5234 if (remove_from_cfg_p)
00c4e97c
AB
5235 {
5236 basic_block succ = single_succ (bb);
5237 delete_and_free_basic_block (bb);
5238 set_immediate_dominator (CDI_DOMINATORS, succ,
5239 recompute_dominator (CDI_DOMINATORS, succ));
5240 }
e855c69d 5241
262d8232 5242 rgn_setup_region (CONTAINING_RGN (idx));
e855c69d
AB
5243}
5244
5245/* Concatenate info of EMPTY_BB to info of MERGE_BB. */
5246static void
5247move_bb_info (basic_block merge_bb, basic_block empty_bb)
5248{
b7b5540a
AB
5249 if (in_current_region_p (merge_bb))
5250 concat_note_lists (BB_NOTE_LIST (empty_bb),
5251 &BB_NOTE_LIST (merge_bb));
e855c69d
AB
5252 BB_NOTE_LIST (empty_bb) = NULL_RTX;
5253
5254}
5255
e855c69d
AB
5256/* Remove EMPTY_BB. If REMOVE_FROM_CFG_P is false, remove EMPTY_BB from
5257 region, but keep it in CFG. */
5258static void
5259remove_empty_bb (basic_block empty_bb, bool remove_from_cfg_p)
5260{
5261 /* The block should contain just a note or a label.
5262 We try to check whether it is unused below. */
5263 gcc_assert (BB_HEAD (empty_bb) == BB_END (empty_bb)
5264 || LABEL_P (BB_HEAD (empty_bb)));
5265
5266 /* If basic block has predecessors or successors, redirect them. */
5267 if (remove_from_cfg_p
5268 && (EDGE_COUNT (empty_bb->preds) > 0
5269 || EDGE_COUNT (empty_bb->succs) > 0))
5270 {
5271 basic_block pred;
5272 basic_block succ;
5273
5274 /* We need to init PRED and SUCC before redirecting edges. */
5275 if (EDGE_COUNT (empty_bb->preds) > 0)
5276 {
5277 edge e;
5278
5279 gcc_assert (EDGE_COUNT (empty_bb->preds) == 1);
5280
5281 e = EDGE_PRED (empty_bb, 0);
5282 gcc_assert (e->src == empty_bb->prev_bb
5283 && (e->flags & EDGE_FALLTHRU));
5284
5285 pred = empty_bb->prev_bb;
5286 }
5287 else
5288 pred = NULL;
5289
5290 if (EDGE_COUNT (empty_bb->succs) > 0)
5291 {
5292 /* We do not check fallthruness here as above, because
5293 after removing a jump the edge may actually be not fallthru. */
5294 gcc_assert (EDGE_COUNT (empty_bb->succs) == 1);
5295 succ = EDGE_SUCC (empty_bb, 0)->dest;
5296 }
5297 else
5298 succ = NULL;
5299
5300 if (EDGE_COUNT (empty_bb->preds) > 0 && succ != NULL)
5301 {
5302 edge e = EDGE_PRED (empty_bb, 0);
5303
5304 if (e->flags & EDGE_FALLTHRU)
5305 redirect_edge_succ_nodup (e, succ);
5306 else
5307 sel_redirect_edge_and_branch (EDGE_PRED (empty_bb, 0), succ);
5308 }
5309
5310 if (EDGE_COUNT (empty_bb->succs) > 0 && pred != NULL)
5311 {
5312 edge e = EDGE_SUCC (empty_bb, 0);
5313
5314 if (find_edge (pred, e->dest) == NULL)
5315 redirect_edge_pred (e, pred);
5316 }
5317 }
5318
5319 /* Finish removing. */
5320 sel_remove_bb (empty_bb, remove_from_cfg_p);
5321}
5322
b8698a0f 5323/* An implementation of create_basic_block hook, which additionally updates
e855c69d
AB
5324 per-bb data structures. */
5325static basic_block
5326sel_create_basic_block (void *headp, void *endp, basic_block after)
5327{
5328 basic_block new_bb;
5329 insn_t new_bb_note;
b8698a0f
L
5330
5331 gcc_assert (flag_sel_sched_pipelining_outer_loops
e855c69d
AB
5332 || last_added_blocks == NULL);
5333
5334 new_bb_note = get_bb_note_from_pool ();
5335
5336 if (new_bb_note == NULL_RTX)
5337 new_bb = orig_cfg_hooks.create_basic_block (headp, endp, after);
5338 else
5339 {
5340 new_bb = create_basic_block_structure ((rtx) headp, (rtx) endp,
5341 new_bb_note, after);
5342 new_bb->aux = NULL;
5343 }
5344
5345 VEC_safe_push (basic_block, heap, last_added_blocks, new_bb);
5346
5347 return new_bb;
5348}
5349
5350/* Implement sched_init_only_bb (). */
5351static void
5352sel_init_only_bb (basic_block bb, basic_block after)
5353{
5354 gcc_assert (after == NULL);
5355
5356 extend_regions ();
5357 rgn_make_new_region_out_of_new_block (bb);
5358}
5359
5360/* Update the latch when we've splitted or merged it from FROM block to TO.
5361 This should be checked for all outer loops, too. */
5362static void
5363change_loops_latches (basic_block from, basic_block to)
5364{
5365 gcc_assert (from != to);
5366
5367 if (current_loop_nest)
5368 {
5369 struct loop *loop;
5370
5371 for (loop = current_loop_nest; loop; loop = loop_outer (loop))
5372 if (considered_for_pipelining_p (loop) && loop->latch == from)
5373 {
5374 gcc_assert (loop == current_loop_nest);
5375 loop->latch = to;
5376 gcc_assert (loop_latch_edge (loop));
5377 }
5378 }
5379}
5380
b8698a0f 5381/* Splits BB on two basic blocks, adding it to the region and extending
e855c69d
AB
5382 per-bb data structures. Returns the newly created bb. */
5383static basic_block
5384sel_split_block (basic_block bb, rtx after)
5385{
5386 basic_block new_bb;
5387 insn_t insn;
5388
5389 new_bb = sched_split_block_1 (bb, after);
5390 sel_add_bb (new_bb);
5391
5392 /* This should be called after sel_add_bb, because this uses
b8698a0f 5393 CONTAINING_RGN for the new block, which is not yet initialized.
e855c69d
AB
5394 FIXME: this function may be a no-op now. */
5395 change_loops_latches (bb, new_bb);
5396
5397 /* Update ORIG_BB_INDEX for insns moved into the new block. */
5398 FOR_BB_INSNS (new_bb, insn)
5399 if (INSN_P (insn))
5400 EXPR_ORIG_BB_INDEX (INSN_EXPR (insn)) = new_bb->index;
5401
5402 if (sel_bb_empty_p (bb))
5403 {
5404 gcc_assert (!sel_bb_empty_p (new_bb));
5405
5406 /* NEW_BB has data sets that need to be updated and BB holds
5407 data sets that should be removed. Exchange these data sets
5408 so that we won't lose BB's valid data sets. */
5409 exchange_data_sets (new_bb, bb);
5410 free_data_sets (bb);
5411 }
5412
5413 if (!sel_bb_empty_p (new_bb)
5414 && bitmap_bit_p (blocks_to_reschedule, bb->index))
5415 bitmap_set_bit (blocks_to_reschedule, new_bb->index);
5416
5417 return new_bb;
5418}
5419
5420/* If BB ends with a jump insn whose ID is bigger then PREV_MAX_UID, return it.
5421 Otherwise returns NULL. */
5422static rtx
5423check_for_new_jump (basic_block bb, int prev_max_uid)
5424{
5425 rtx end;
5426
5427 end = sel_bb_end (bb);
5428 if (end && INSN_UID (end) >= prev_max_uid)
5429 return end;
5430 return NULL;
5431}
5432
b8698a0f 5433/* Look for a new jump either in FROM_BB block or in newly created JUMP_BB block.
e855c69d
AB
5434 New means having UID at least equal to PREV_MAX_UID. */
5435static rtx
5436find_new_jump (basic_block from, basic_block jump_bb, int prev_max_uid)
5437{
5438 rtx jump;
5439
5440 /* Return immediately if no new insns were emitted. */
5441 if (get_max_uid () == prev_max_uid)
5442 return NULL;
b8698a0f 5443
e855c69d
AB
5444 /* Now check both blocks for new jumps. It will ever be only one. */
5445 if ((jump = check_for_new_jump (from, prev_max_uid)))
5446 return jump;
5447
5448 if (jump_bb != NULL
5449 && (jump = check_for_new_jump (jump_bb, prev_max_uid)))
5450 return jump;
5451 return NULL;
5452}
5453
5454/* Splits E and adds the newly created basic block to the current region.
5455 Returns this basic block. */
5456basic_block
5457sel_split_edge (edge e)
5458{
5459 basic_block new_bb, src, other_bb = NULL;
5460 int prev_max_uid;
5461 rtx jump;
5462
5463 src = e->src;
5464 prev_max_uid = get_max_uid ();
5465 new_bb = split_edge (e);
5466
b8698a0f 5467 if (flag_sel_sched_pipelining_outer_loops
e855c69d
AB
5468 && current_loop_nest)
5469 {
5470 int i;
5471 basic_block bb;
5472
b8698a0f 5473 /* Some of the basic blocks might not have been added to the loop.
e855c69d 5474 Add them here, until this is fixed in force_fallthru. */
b8698a0f 5475 for (i = 0;
e855c69d
AB
5476 VEC_iterate (basic_block, last_added_blocks, i, bb); i++)
5477 if (!bb->loop_father)
5478 {
5479 add_bb_to_loop (bb, e->dest->loop_father);
5480
5481 gcc_assert (!other_bb && (new_bb->index != bb->index));
5482 other_bb = bb;
5483 }
5484 }
5485
5486 /* Add all last_added_blocks to the region. */
5487 sel_add_bb (NULL);
5488
5489 jump = find_new_jump (src, new_bb, prev_max_uid);
5490 if (jump)
5491 sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP);
5492
5493 /* Put the correct lv set on this block. */
5494 if (other_bb && !sel_bb_empty_p (other_bb))
5495 compute_live (sel_bb_head (other_bb));
5496
5497 return new_bb;
5498}
5499
5500/* Implement sched_create_empty_bb (). */
5501static basic_block
5502sel_create_empty_bb (basic_block after)
5503{
5504 basic_block new_bb;
5505
5506 new_bb = sched_create_empty_bb_1 (after);
5507
5508 /* We'll explicitly initialize NEW_BB via sel_init_only_bb () a bit
5509 later. */
5510 gcc_assert (VEC_length (basic_block, last_added_blocks) == 1
5511 && VEC_index (basic_block, last_added_blocks, 0) == new_bb);
5512
5513 VEC_free (basic_block, heap, last_added_blocks);
5514 return new_bb;
5515}
5516
5517/* Implement sched_create_recovery_block. ORIG_INSN is where block
5518 will be splitted to insert a check. */
5519basic_block
5520sel_create_recovery_block (insn_t orig_insn)
5521{
5522 basic_block first_bb, second_bb, recovery_block;
5523 basic_block before_recovery = NULL;
5524 rtx jump;
5525
5526 first_bb = BLOCK_FOR_INSN (orig_insn);
5527 if (sel_bb_end_p (orig_insn))
5528 {
5529 /* Avoid introducing an empty block while splitting. */
5530 gcc_assert (single_succ_p (first_bb));
5531 second_bb = single_succ (first_bb);
5532 }
5533 else
5534 second_bb = sched_split_block (first_bb, orig_insn);
5535
5536 recovery_block = sched_create_recovery_block (&before_recovery);
5537 if (before_recovery)
5538 copy_lv_set_from (before_recovery, EXIT_BLOCK_PTR);
5539
5540 gcc_assert (sel_bb_empty_p (recovery_block));
5541 sched_create_recovery_edges (first_bb, recovery_block, second_bb);
5542 if (current_loops != NULL)
5543 add_bb_to_loop (recovery_block, first_bb->loop_father);
b8698a0f 5544
e855c69d 5545 sel_add_bb (recovery_block);
b8698a0f 5546
e855c69d
AB
5547 jump = BB_END (recovery_block);
5548 gcc_assert (sel_bb_head (recovery_block) == jump);
5549 sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP);
5550
5551 return recovery_block;
5552}
5553
5554/* Merge basic block B into basic block A. */
262d8232 5555static void
e855c69d
AB
5556sel_merge_blocks (basic_block a, basic_block b)
5557{
262d8232
AB
5558 gcc_assert (sel_bb_empty_p (b)
5559 && EDGE_COUNT (b->preds) == 1
5560 && EDGE_PRED (b, 0)->src == b->prev_bb);
e855c69d 5561
262d8232
AB
5562 move_bb_info (b->prev_bb, b);
5563 remove_empty_bb (b, false);
5564 merge_blocks (a, b);
e855c69d
AB
5565 change_loops_latches (b, a);
5566}
5567
5568/* A wrapper for redirect_edge_and_branch_force, which also initializes
5569 data structures for possibly created bb and insns. Returns the newly
5570 added bb or NULL, when a bb was not needed. */
5571void
5572sel_redirect_edge_and_branch_force (edge e, basic_block to)
5573{
00c4e97c 5574 basic_block jump_bb, src, orig_dest = e->dest;
e855c69d
AB
5575 int prev_max_uid;
5576 rtx jump;
b8698a0f 5577
00c4e97c
AB
5578 /* This function is now used only for bookkeeping code creation, where
5579 we'll never get the single pred of orig_dest block and thus will not
5580 hit unreachable blocks when updating dominator info. */
5581 gcc_assert (!sel_bb_empty_p (e->src)
5582 && !single_pred_p (orig_dest));
e855c69d
AB
5583 src = e->src;
5584 prev_max_uid = get_max_uid ();
5585 jump_bb = redirect_edge_and_branch_force (e, to);
5586
5587 if (jump_bb != NULL)
5588 sel_add_bb (jump_bb);
5589
5590 /* This function could not be used to spoil the loop structure by now,
5591 thus we don't care to update anything. But check it to be sure. */
5592 if (current_loop_nest
5593 && pipelining_p)
5594 gcc_assert (loop_latch_edge (current_loop_nest));
b8698a0f 5595
e855c69d
AB
5596 jump = find_new_jump (src, jump_bb, prev_max_uid);
5597 if (jump)
5598 sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP);
00c4e97c
AB
5599 set_immediate_dominator (CDI_DOMINATORS, to,
5600 recompute_dominator (CDI_DOMINATORS, to));
5601 set_immediate_dominator (CDI_DOMINATORS, orig_dest,
5602 recompute_dominator (CDI_DOMINATORS, orig_dest));
e855c69d
AB
5603}
5604
b59ab570
AM
5605/* A wrapper for redirect_edge_and_branch. Return TRUE if blocks connected by
5606 redirected edge are in reverse topological order. */
5607bool
e855c69d
AB
5608sel_redirect_edge_and_branch (edge e, basic_block to)
5609{
5610 bool latch_edge_p;
00c4e97c 5611 basic_block src, orig_dest = e->dest;
e855c69d
AB
5612 int prev_max_uid;
5613 rtx jump;
f2c45f08 5614 edge redirected;
b59ab570 5615 bool recompute_toporder_p = false;
00c4e97c 5616 bool maybe_unreachable = single_pred_p (orig_dest);
e855c69d
AB
5617
5618 latch_edge_p = (pipelining_p
5619 && current_loop_nest
5620 && e == loop_latch_edge (current_loop_nest));
5621
5622 src = e->src;
5623 prev_max_uid = get_max_uid ();
f2c45f08
AM
5624
5625 redirected = redirect_edge_and_branch (e, to);
5626
5627 gcc_assert (redirected && last_added_blocks == NULL);
e855c69d
AB
5628
5629 /* When we've redirected a latch edge, update the header. */
5630 if (latch_edge_p)
5631 {
5632 current_loop_nest->header = to;
5633 gcc_assert (loop_latch_edge (current_loop_nest));
5634 }
5635
b59ab570
AM
5636 /* In rare situations, the topological relation between the blocks connected
5637 by the redirected edge can change (see PR42245 for an example). Update
5638 block_to_bb/bb_to_block. */
5639 if (CONTAINING_RGN (e->src->index) == CONTAINING_RGN (to->index)
5640 && BLOCK_TO_BB (e->src->index) > BLOCK_TO_BB (to->index))
5641 recompute_toporder_p = true;
5642
e855c69d
AB
5643 jump = find_new_jump (src, NULL, prev_max_uid);
5644 if (jump)
5645 sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP);
b59ab570 5646
00c4e97c
AB
5647 /* Only update dominator info when we don't have unreachable blocks.
5648 Otherwise we'll update in maybe_tidy_empty_bb. */
5649 if (!maybe_unreachable)
5650 {
5651 set_immediate_dominator (CDI_DOMINATORS, to,
5652 recompute_dominator (CDI_DOMINATORS, to));
5653 set_immediate_dominator (CDI_DOMINATORS, orig_dest,
5654 recompute_dominator (CDI_DOMINATORS, orig_dest));
5655 }
b59ab570 5656 return recompute_toporder_p;
e855c69d
AB
5657}
5658
5659/* This variable holds the cfg hooks used by the selective scheduler. */
5660static struct cfg_hooks sel_cfg_hooks;
5661
5662/* Register sel-sched cfg hooks. */
5663void
5664sel_register_cfg_hooks (void)
5665{
5666 sched_split_block = sel_split_block;
5667
5668 orig_cfg_hooks = get_cfg_hooks ();
5669 sel_cfg_hooks = orig_cfg_hooks;
5670
5671 sel_cfg_hooks.create_basic_block = sel_create_basic_block;
5672
5673 set_cfg_hooks (sel_cfg_hooks);
5674
5675 sched_init_only_bb = sel_init_only_bb;
5676 sched_split_block = sel_split_block;
5677 sched_create_empty_bb = sel_create_empty_bb;
5678}
5679
5680/* Unregister sel-sched cfg hooks. */
5681void
5682sel_unregister_cfg_hooks (void)
5683{
5684 sched_create_empty_bb = NULL;
5685 sched_split_block = NULL;
5686 sched_init_only_bb = NULL;
5687
5688 set_cfg_hooks (orig_cfg_hooks);
5689}
5690\f
5691
5692/* Emit an insn rtx based on PATTERN. If a jump insn is wanted,
5693 LABEL is where this jump should be directed. */
5694rtx
5695create_insn_rtx_from_pattern (rtx pattern, rtx label)
5696{
5697 rtx insn_rtx;
5698
5699 gcc_assert (!INSN_P (pattern));
5700
5701 start_sequence ();
5702
5703 if (label == NULL_RTX)
5704 insn_rtx = emit_insn (pattern);
b5b8b0ac
AO
5705 else if (DEBUG_INSN_P (label))
5706 insn_rtx = emit_debug_insn (pattern);
e855c69d
AB
5707 else
5708 {
5709 insn_rtx = emit_jump_insn (pattern);
5710 JUMP_LABEL (insn_rtx) = label;
5711 ++LABEL_NUSES (label);
5712 }
5713
5714 end_sequence ();
5715
a95b23b4 5716 sched_extend_luids ();
e855c69d
AB
5717 sched_extend_target ();
5718 sched_deps_init (false);
5719
5720 /* Initialize INSN_CODE now. */
5721 recog_memoized (insn_rtx);
5722 return insn_rtx;
5723}
5724
5725/* Create a new vinsn for INSN_RTX. FORCE_UNIQUE_P is true when the vinsn
5726 must not be clonable. */
5727vinsn_t
5728create_vinsn_from_insn_rtx (rtx insn_rtx, bool force_unique_p)
5729{
5730 gcc_assert (INSN_P (insn_rtx) && !INSN_IN_STREAM_P (insn_rtx));
5731
5732 /* If VINSN_TYPE is not USE, retain its uniqueness. */
5733 return vinsn_create (insn_rtx, force_unique_p);
5734}
5735
5736/* Create a copy of INSN_RTX. */
5737rtx
5738create_copy_of_insn_rtx (rtx insn_rtx)
5739{
d734e6c4 5740 rtx res, link;
e855c69d 5741
b5b8b0ac
AO
5742 if (DEBUG_INSN_P (insn_rtx))
5743 return create_insn_rtx_from_pattern (copy_rtx (PATTERN (insn_rtx)),
5744 insn_rtx);
5745
e855c69d
AB
5746 gcc_assert (NONJUMP_INSN_P (insn_rtx));
5747
5748 res = create_insn_rtx_from_pattern (copy_rtx (PATTERN (insn_rtx)),
5749 NULL_RTX);
d734e6c4
JJ
5750
5751 /* Copy all REG_NOTES except REG_EQUAL/REG_EQUIV and REG_LABEL_OPERAND
5752 since mark_jump_label will make them. REG_LABEL_TARGETs are created
5753 there too, but are supposed to be sticky, so we copy them. */
5754 for (link = REG_NOTES (insn_rtx); link; link = XEXP (link, 1))
5755 if (REG_NOTE_KIND (link) != REG_LABEL_OPERAND
5756 && REG_NOTE_KIND (link) != REG_EQUAL
5757 && REG_NOTE_KIND (link) != REG_EQUIV)
5758 {
5759 if (GET_CODE (link) == EXPR_LIST)
5760 add_reg_note (res, REG_NOTE_KIND (link),
5761 copy_insn_1 (XEXP (link, 0)));
5762 else
5763 add_reg_note (res, REG_NOTE_KIND (link), XEXP (link, 0));
5764 }
5765
e855c69d
AB
5766 return res;
5767}
5768
5769/* Change vinsn field of EXPR to hold NEW_VINSN. */
5770void
5771change_vinsn_in_expr (expr_t expr, vinsn_t new_vinsn)
5772{
5773 vinsn_detach (EXPR_VINSN (expr));
5774
5775 EXPR_VINSN (expr) = new_vinsn;
5776 vinsn_attach (new_vinsn);
5777}
5778
5779/* Helpers for global init. */
5780/* This structure is used to be able to call existing bundling mechanism
5781 and calculate insn priorities. */
b8698a0f 5782static struct haifa_sched_info sched_sel_haifa_sched_info =
e855c69d
AB
5783{
5784 NULL, /* init_ready_list */
5785 NULL, /* can_schedule_ready_p */
5786 NULL, /* schedule_more_p */
5787 NULL, /* new_ready */
5788 NULL, /* rgn_rank */
5789 sel_print_insn, /* rgn_print_insn */
5790 contributes_to_priority,
356c23b3 5791 NULL, /* insn_finishes_block_p */
e855c69d
AB
5792
5793 NULL, NULL,
5794 NULL, NULL,
5795 0, 0,
5796
5797 NULL, /* add_remove_insn */
5798 NULL, /* begin_schedule_ready */
86014d07 5799 NULL, /* begin_move_insn */
e855c69d 5800 NULL, /* advance_target_bb */
26965010
BS
5801
5802 NULL,
5803 NULL,
5804
e855c69d
AB
5805 SEL_SCHED | NEW_BBS
5806};
5807
5808/* Setup special insns used in the scheduler. */
b8698a0f 5809void
e855c69d
AB
5810setup_nop_and_exit_insns (void)
5811{
5812 gcc_assert (nop_pattern == NULL_RTX
5813 && exit_insn == NULL_RTX);
5814
9ef1bf71 5815 nop_pattern = constm1_rtx;
e855c69d
AB
5816
5817 start_sequence ();
5818 emit_insn (nop_pattern);
5819 exit_insn = get_insns ();
5820 end_sequence ();
5821 set_block_for_insn (exit_insn, EXIT_BLOCK_PTR);
5822}
5823
5824/* Free special insns used in the scheduler. */
5825void
5826free_nop_and_exit_insns (void)
5827{
5828 exit_insn = NULL_RTX;
5829 nop_pattern = NULL_RTX;
5830}
5831
5832/* Setup a special vinsn used in new insns initialization. */
5833void
5834setup_nop_vinsn (void)
5835{
5836 nop_vinsn = vinsn_create (exit_insn, false);
5837 vinsn_attach (nop_vinsn);
5838}
5839
5840/* Free a special vinsn used in new insns initialization. */
5841void
5842free_nop_vinsn (void)
5843{
5844 gcc_assert (VINSN_COUNT (nop_vinsn) == 1);
5845 vinsn_detach (nop_vinsn);
5846 nop_vinsn = NULL;
5847}
5848
5849/* Call a set_sched_flags hook. */
5850void
5851sel_set_sched_flags (void)
5852{
b8698a0f 5853 /* ??? This means that set_sched_flags were called, and we decided to
e855c69d 5854 support speculation. However, set_sched_flags also modifies flags
b8698a0f 5855 on current_sched_info, doing this only at global init. And we
e855c69d
AB
5856 sometimes change c_s_i later. So put the correct flags again. */
5857 if (spec_info && targetm.sched.set_sched_flags)
5858 targetm.sched.set_sched_flags (spec_info);
5859}
5860
5861/* Setup pointers to global sched info structures. */
5862void
5863sel_setup_sched_infos (void)
5864{
5865 rgn_setup_common_sched_info ();
5866
5867 memcpy (&sel_common_sched_info, common_sched_info,
5868 sizeof (sel_common_sched_info));
5869
5870 sel_common_sched_info.fix_recovery_cfg = NULL;
5871 sel_common_sched_info.add_block = NULL;
5872 sel_common_sched_info.estimate_number_of_insns
5873 = sel_estimate_number_of_insns;
5874 sel_common_sched_info.luid_for_non_insn = sel_luid_for_non_insn;
5875 sel_common_sched_info.sched_pass_id = SCHED_SEL_PASS;
5876
5877 common_sched_info = &sel_common_sched_info;
5878
5879 current_sched_info = &sched_sel_haifa_sched_info;
b8698a0f 5880 current_sched_info->sched_max_insns_priority =
e855c69d 5881 get_rgn_sched_max_insns_priority ();
b8698a0f 5882
e855c69d
AB
5883 sel_set_sched_flags ();
5884}
5885\f
5886
5887/* Adds basic block BB to region RGN at the position *BB_ORD_INDEX,
5888 *BB_ORD_INDEX after that is increased. */
5889static void
5890sel_add_block_to_region (basic_block bb, int *bb_ord_index, int rgn)
5891{
5892 RGN_NR_BLOCKS (rgn) += 1;
5893 RGN_DONT_CALC_DEPS (rgn) = 0;
5894 RGN_HAS_REAL_EBB (rgn) = 0;
5895 CONTAINING_RGN (bb->index) = rgn;
5896 BLOCK_TO_BB (bb->index) = *bb_ord_index;
5897 rgn_bb_table[RGN_BLOCKS (rgn) + *bb_ord_index] = bb->index;
5898 (*bb_ord_index)++;
5899
5900 /* FIXME: it is true only when not scheduling ebbs. */
5901 RGN_BLOCKS (rgn + 1) = RGN_BLOCKS (rgn) + RGN_NR_BLOCKS (rgn);
5902}
5903
5904/* Functions to support pipelining of outer loops. */
5905
5906/* Creates a new empty region and returns it's number. */
5907static int
5908sel_create_new_region (void)
5909{
5910 int new_rgn_number = nr_regions;
5911
5912 RGN_NR_BLOCKS (new_rgn_number) = 0;
5913
5914 /* FIXME: This will work only when EBBs are not created. */
5915 if (new_rgn_number != 0)
b8698a0f 5916 RGN_BLOCKS (new_rgn_number) = RGN_BLOCKS (new_rgn_number - 1) +
e855c69d
AB
5917 RGN_NR_BLOCKS (new_rgn_number - 1);
5918 else
5919 RGN_BLOCKS (new_rgn_number) = 0;
5920
5921 /* Set the blocks of the next region so the other functions may
5922 calculate the number of blocks in the region. */
b8698a0f 5923 RGN_BLOCKS (new_rgn_number + 1) = RGN_BLOCKS (new_rgn_number) +
e855c69d
AB
5924 RGN_NR_BLOCKS (new_rgn_number);
5925
5926 nr_regions++;
5927
5928 return new_rgn_number;
5929}
5930
5931/* If X has a smaller topological sort number than Y, returns -1;
5932 if greater, returns 1. */
5933static int
5934bb_top_order_comparator (const void *x, const void *y)
5935{
5936 basic_block bb1 = *(const basic_block *) x;
5937 basic_block bb2 = *(const basic_block *) y;
5938
b8698a0f
L
5939 gcc_assert (bb1 == bb2
5940 || rev_top_order_index[bb1->index]
e855c69d
AB
5941 != rev_top_order_index[bb2->index]);
5942
5943 /* It's a reverse topological order in REV_TOP_ORDER_INDEX, so
5944 bbs with greater number should go earlier. */
5945 if (rev_top_order_index[bb1->index] > rev_top_order_index[bb2->index])
5946 return -1;
5947 else
5948 return 1;
5949}
5950
b8698a0f 5951/* Create a region for LOOP and return its number. If we don't want
e855c69d
AB
5952 to pipeline LOOP, return -1. */
5953static int
5954make_region_from_loop (struct loop *loop)
5955{
5956 unsigned int i;
5957 int new_rgn_number = -1;
5958 struct loop *inner;
5959
5960 /* Basic block index, to be assigned to BLOCK_TO_BB. */
5961 int bb_ord_index = 0;
5962 basic_block *loop_blocks;
5963 basic_block preheader_block;
5964
b8698a0f 5965 if (loop->num_nodes
e855c69d
AB
5966 > (unsigned) PARAM_VALUE (PARAM_MAX_PIPELINE_REGION_BLOCKS))
5967 return -1;
b8698a0f 5968
e855c69d
AB
5969 /* Don't pipeline loops whose latch belongs to some of its inner loops. */
5970 for (inner = loop->inner; inner; inner = inner->inner)
5971 if (flow_bb_inside_loop_p (inner, loop->latch))
5972 return -1;
5973
5974 loop->ninsns = num_loop_insns (loop);
5975 if ((int) loop->ninsns > PARAM_VALUE (PARAM_MAX_PIPELINE_REGION_INSNS))
5976 return -1;
5977
5978 loop_blocks = get_loop_body_in_custom_order (loop, bb_top_order_comparator);
5979
5980 for (i = 0; i < loop->num_nodes; i++)
5981 if (loop_blocks[i]->flags & BB_IRREDUCIBLE_LOOP)
5982 {
5983 free (loop_blocks);
5984 return -1;
5985 }
5986
5987 preheader_block = loop_preheader_edge (loop)->src;
5988 gcc_assert (preheader_block);
5989 gcc_assert (loop_blocks[0] == loop->header);
5990
5991 new_rgn_number = sel_create_new_region ();
5992
5993 sel_add_block_to_region (preheader_block, &bb_ord_index, new_rgn_number);
5994 SET_BIT (bbs_in_loop_rgns, preheader_block->index);
5995
5996 for (i = 0; i < loop->num_nodes; i++)
5997 {
5998 /* Add only those blocks that haven't been scheduled in the inner loop.
5999 The exception is the basic blocks with bookkeeping code - they should
b8698a0f 6000 be added to the region (and they actually don't belong to the loop
e855c69d
AB
6001 body, but to the region containing that loop body). */
6002
6003 gcc_assert (new_rgn_number >= 0);
6004
6005 if (! TEST_BIT (bbs_in_loop_rgns, loop_blocks[i]->index))
6006 {
b8698a0f 6007 sel_add_block_to_region (loop_blocks[i], &bb_ord_index,
e855c69d
AB
6008 new_rgn_number);
6009 SET_BIT (bbs_in_loop_rgns, loop_blocks[i]->index);
6010 }
6011 }
6012
6013 free (loop_blocks);
6014 MARK_LOOP_FOR_PIPELINING (loop);
6015
6016 return new_rgn_number;
6017}
6018
6019/* Create a new region from preheader blocks LOOP_BLOCKS. */
6020void
6021make_region_from_loop_preheader (VEC(basic_block, heap) **loop_blocks)
6022{
6023 unsigned int i;
6024 int new_rgn_number = -1;
6025 basic_block bb;
6026
6027 /* Basic block index, to be assigned to BLOCK_TO_BB. */
6028 int bb_ord_index = 0;
6029
6030 new_rgn_number = sel_create_new_region ();
6031
ac47786e 6032 FOR_EACH_VEC_ELT (basic_block, *loop_blocks, i, bb)
e855c69d
AB
6033 {
6034 gcc_assert (new_rgn_number >= 0);
6035
6036 sel_add_block_to_region (bb, &bb_ord_index, new_rgn_number);
6037 }
6038
6039 VEC_free (basic_block, heap, *loop_blocks);
6040 gcc_assert (*loop_blocks == NULL);
6041}
6042
6043
6044/* Create region(s) from loop nest LOOP, such that inner loops will be
b8698a0f 6045 pipelined before outer loops. Returns true when a region for LOOP
e855c69d
AB
6046 is created. */
6047static bool
6048make_regions_from_loop_nest (struct loop *loop)
b8698a0f 6049{
e855c69d
AB
6050 struct loop *cur_loop;
6051 int rgn_number;
6052
6053 /* Traverse all inner nodes of the loop. */
6054 for (cur_loop = loop->inner; cur_loop; cur_loop = cur_loop->next)
6055 if (! TEST_BIT (bbs_in_loop_rgns, cur_loop->header->index))
6056 return false;
6057
6058 /* At this moment all regular inner loops should have been pipelined.
6059 Try to create a region from this loop. */
6060 rgn_number = make_region_from_loop (loop);
6061
6062 if (rgn_number < 0)
6063 return false;
6064
6065 VEC_safe_push (loop_p, heap, loop_nests, loop);
6066 return true;
6067}
6068
6069/* Initalize data structures needed. */
6070void
6071sel_init_pipelining (void)
6072{
6073 /* Collect loop information to be used in outer loops pipelining. */
6074 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
6075 | LOOPS_HAVE_FALLTHRU_PREHEADERS
6076 | LOOPS_HAVE_RECORDED_EXITS
6077 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS);
6078 current_loop_nest = NULL;
6079
6080 bbs_in_loop_rgns = sbitmap_alloc (last_basic_block);
6081 sbitmap_zero (bbs_in_loop_rgns);
6082
6083 recompute_rev_top_order ();
6084}
6085
6086/* Returns a struct loop for region RGN. */
6087loop_p
6088get_loop_nest_for_rgn (unsigned int rgn)
6089{
6090 /* Regions created with extend_rgns don't have corresponding loop nests,
6091 because they don't represent loops. */
6092 if (rgn < VEC_length (loop_p, loop_nests))
6093 return VEC_index (loop_p, loop_nests, rgn);
6094 else
6095 return NULL;
6096}
6097
6098/* True when LOOP was included into pipelining regions. */
6099bool
6100considered_for_pipelining_p (struct loop *loop)
6101{
6102 if (loop_depth (loop) == 0)
6103 return false;
6104
b8698a0f
L
6105 /* Now, the loop could be too large or irreducible. Check whether its
6106 region is in LOOP_NESTS.
6107 We determine the region number of LOOP as the region number of its
6108 latch. We can't use header here, because this header could be
e855c69d
AB
6109 just removed preheader and it will give us the wrong region number.
6110 Latch can't be used because it could be in the inner loop too. */
8ec4d0ad 6111 if (LOOP_MARKED_FOR_PIPELINING_P (loop))
e855c69d
AB
6112 {
6113 int rgn = CONTAINING_RGN (loop->latch->index);
6114
6115 gcc_assert ((unsigned) rgn < VEC_length (loop_p, loop_nests));
6116 return true;
6117 }
b8698a0f 6118
e855c69d
AB
6119 return false;
6120}
6121
b8698a0f 6122/* Makes regions from the rest of the blocks, after loops are chosen
e855c69d
AB
6123 for pipelining. */
6124static void
6125make_regions_from_the_rest (void)
6126{
6127 int cur_rgn_blocks;
6128 int *loop_hdr;
6129 int i;
6130
6131 basic_block bb;
6132 edge e;
6133 edge_iterator ei;
6134 int *degree;
e855c69d
AB
6135
6136 /* Index in rgn_bb_table where to start allocating new regions. */
6137 cur_rgn_blocks = nr_regions ? RGN_BLOCKS (nr_regions) : 0;
e855c69d 6138
b8698a0f 6139 /* Make regions from all the rest basic blocks - those that don't belong to
e855c69d
AB
6140 any loop or belong to irreducible loops. Prepare the data structures
6141 for extend_rgns. */
6142
6143 /* LOOP_HDR[I] == -1 if I-th bb doesn't belong to any loop,
6144 LOOP_HDR[I] == LOOP_HDR[J] iff basic blocks I and J reside within the same
6145 loop. */
6146 loop_hdr = XNEWVEC (int, last_basic_block);
6147 degree = XCNEWVEC (int, last_basic_block);
6148
6149
6150 /* For each basic block that belongs to some loop assign the number
6151 of innermost loop it belongs to. */
6152 for (i = 0; i < last_basic_block; i++)
6153 loop_hdr[i] = -1;
6154
6155 FOR_EACH_BB (bb)
6156 {
6157 if (bb->loop_father && !bb->loop_father->num == 0
6158 && !(bb->flags & BB_IRREDUCIBLE_LOOP))
6159 loop_hdr[bb->index] = bb->loop_father->num;
6160 }
6161
b8698a0f 6162 /* For each basic block degree is calculated as the number of incoming
e855c69d
AB
6163 edges, that are going out of bbs that are not yet scheduled.
6164 The basic blocks that are scheduled have degree value of zero. */
b8698a0f 6165 FOR_EACH_BB (bb)
e855c69d
AB
6166 {
6167 degree[bb->index] = 0;
6168
6169 if (!TEST_BIT (bbs_in_loop_rgns, bb->index))
6170 {
6171 FOR_EACH_EDGE (e, ei, bb->preds)
6172 if (!TEST_BIT (bbs_in_loop_rgns, e->src->index))
6173 degree[bb->index]++;
6174 }
6175 else
6176 degree[bb->index] = -1;
6177 }
6178
6179 extend_rgns (degree, &cur_rgn_blocks, bbs_in_loop_rgns, loop_hdr);
6180
6181 /* Any block that did not end up in a region is placed into a region
6182 by itself. */
6183 FOR_EACH_BB (bb)
6184 if (degree[bb->index] >= 0)
6185 {
6186 rgn_bb_table[cur_rgn_blocks] = bb->index;
6187 RGN_NR_BLOCKS (nr_regions) = 1;
6188 RGN_BLOCKS (nr_regions) = cur_rgn_blocks++;
6189 RGN_DONT_CALC_DEPS (nr_regions) = 0;
6190 RGN_HAS_REAL_EBB (nr_regions) = 0;
6191 CONTAINING_RGN (bb->index) = nr_regions++;
6192 BLOCK_TO_BB (bb->index) = 0;
6193 }
6194
6195 free (degree);
6196 free (loop_hdr);
6197}
6198
6199/* Free data structures used in pipelining of loops. */
6200void sel_finish_pipelining (void)
6201{
6202 loop_iterator li;
6203 struct loop *loop;
6204
6205 /* Release aux fields so we don't free them later by mistake. */
6206 FOR_EACH_LOOP (li, loop, 0)
6207 loop->aux = NULL;
6208
6209 loop_optimizer_finalize ();
6210
6211 VEC_free (loop_p, heap, loop_nests);
6212
6213 free (rev_top_order_index);
6214 rev_top_order_index = NULL;
6215}
6216
b8698a0f 6217/* This function replaces the find_rgns when
e855c69d 6218 FLAG_SEL_SCHED_PIPELINING_OUTER_LOOPS is set. */
b8698a0f 6219void
e855c69d
AB
6220sel_find_rgns (void)
6221{
6222 sel_init_pipelining ();
6223 extend_regions ();
6224
6225 if (current_loops)
6226 {
6227 loop_p loop;
6228 loop_iterator li;
6229
6230 FOR_EACH_LOOP (li, loop, (flag_sel_sched_pipelining_outer_loops
6231 ? LI_FROM_INNERMOST
6232 : LI_ONLY_INNERMOST))
6233 make_regions_from_loop_nest (loop);
6234 }
6235
6236 /* Make regions from all the rest basic blocks and schedule them.
b8698a0f 6237 These blocks include blocks that don't belong to any loop or belong
e855c69d
AB
6238 to irreducible loops. */
6239 make_regions_from_the_rest ();
6240
6241 /* We don't need bbs_in_loop_rgns anymore. */
6242 sbitmap_free (bbs_in_loop_rgns);
6243 bbs_in_loop_rgns = NULL;
6244}
6245
ea4d630f
AM
6246/* Add the preheader blocks from previous loop to current region taking
6247 it from LOOP_PREHEADER_BLOCKS (current_loop_nest) and record them in *BBS.
e855c69d
AB
6248 This function is only used with -fsel-sched-pipelining-outer-loops. */
6249void
ea4d630f 6250sel_add_loop_preheaders (bb_vec_t *bbs)
e855c69d
AB
6251{
6252 int i;
6253 basic_block bb;
b8698a0f 6254 VEC(basic_block, heap) *preheader_blocks
e855c69d
AB
6255 = LOOP_PREHEADER_BLOCKS (current_loop_nest);
6256
6257 for (i = 0;
6258 VEC_iterate (basic_block, preheader_blocks, i, bb);
6259 i++)
8ec4d0ad 6260 {
ea4d630f 6261 VEC_safe_push (basic_block, heap, *bbs, bb);
8ec4d0ad 6262 VEC_safe_push (basic_block, heap, last_added_blocks, bb);
e855c69d 6263 sel_add_bb (bb);
8ec4d0ad 6264 }
e855c69d
AB
6265
6266 VEC_free (basic_block, heap, preheader_blocks);
6267}
6268
b8698a0f
L
6269/* While pipelining outer loops, returns TRUE if BB is a loop preheader.
6270 Please note that the function should also work when pipelining_p is
6271 false, because it is used when deciding whether we should or should
e855c69d
AB
6272 not reschedule pipelined code. */
6273bool
6274sel_is_loop_preheader_p (basic_block bb)
6275{
6276 if (current_loop_nest)
6277 {
6278 struct loop *outer;
6279
6280 if (preheader_removed)
6281 return false;
6282
6283 /* Preheader is the first block in the region. */
6284 if (BLOCK_TO_BB (bb->index) == 0)
6285 return true;
6286
6287 /* We used to find a preheader with the topological information.
6288 Check that the above code is equivalent to what we did before. */
6289
6290 if (in_current_region_p (current_loop_nest->header))
b8698a0f 6291 gcc_assert (!(BLOCK_TO_BB (bb->index)
e855c69d
AB
6292 < BLOCK_TO_BB (current_loop_nest->header->index)));
6293
6294 /* Support the situation when the latch block of outer loop
6295 could be from here. */
6296 for (outer = loop_outer (current_loop_nest);
6297 outer;
6298 outer = loop_outer (outer))
6299 if (considered_for_pipelining_p (outer) && outer->latch == bb)
6300 gcc_unreachable ();
6301 }
6302
6303 return false;
6304}
6305
753de8cf
AM
6306/* Check whether JUMP_BB ends with a jump insn that leads only to DEST_BB and
6307 can be removed, making the corresponding edge fallthrough (assuming that
6308 all basic blocks between JUMP_BB and DEST_BB are empty). */
6309static bool
6310bb_has_removable_jump_to_p (basic_block jump_bb, basic_block dest_bb)
e855c69d 6311{
b4550bf7
AM
6312 if (!onlyjump_p (BB_END (jump_bb))
6313 || tablejump_p (BB_END (jump_bb), NULL, NULL))
e855c69d
AB
6314 return false;
6315
b8698a0f 6316 /* Several outgoing edges, abnormal edge or destination of jump is
e855c69d
AB
6317 not DEST_BB. */
6318 if (EDGE_COUNT (jump_bb->succs) != 1
753de8cf 6319 || EDGE_SUCC (jump_bb, 0)->flags & (EDGE_ABNORMAL | EDGE_CROSSING)
e855c69d
AB
6320 || EDGE_SUCC (jump_bb, 0)->dest != dest_bb)
6321 return false;
6322
6323 /* If not anything of the upper. */
6324 return true;
6325}
6326
6327/* Removes the loop preheader from the current region and saves it in
b8698a0f 6328 PREHEADER_BLOCKS of the father loop, so they will be added later to
e855c69d
AB
6329 region that represents an outer loop. */
6330static void
6331sel_remove_loop_preheader (void)
6332{
6333 int i, old_len;
6334 int cur_rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
6335 basic_block bb;
6336 bool all_empty_p = true;
b8698a0f 6337 VEC(basic_block, heap) *preheader_blocks
e855c69d
AB
6338 = LOOP_PREHEADER_BLOCKS (loop_outer (current_loop_nest));
6339
6340 gcc_assert (current_loop_nest);
6341 old_len = VEC_length (basic_block, preheader_blocks);
6342
6343 /* Add blocks that aren't within the current loop to PREHEADER_BLOCKS. */
6344 for (i = 0; i < RGN_NR_BLOCKS (cur_rgn); i++)
6345 {
6346 bb = BASIC_BLOCK (BB_TO_BLOCK (i));
6347
b8698a0f 6348 /* If the basic block belongs to region, but doesn't belong to
e855c69d
AB
6349 corresponding loop, then it should be a preheader. */
6350 if (sel_is_loop_preheader_p (bb))
6351 {
6352 VEC_safe_push (basic_block, heap, preheader_blocks, bb);
6353 if (BB_END (bb) != bb_note (bb))
6354 all_empty_p = false;
6355 }
6356 }
b8698a0f 6357
e855c69d
AB
6358 /* Remove these blocks only after iterating over the whole region. */
6359 for (i = VEC_length (basic_block, preheader_blocks) - 1;
6360 i >= old_len;
6361 i--)
6362 {
b8698a0f 6363 bb = VEC_index (basic_block, preheader_blocks, i);
e855c69d
AB
6364 sel_remove_bb (bb, false);
6365 }
6366
6367 if (!considered_for_pipelining_p (loop_outer (current_loop_nest)))
6368 {
6369 if (!all_empty_p)
6370 /* Immediately create new region from preheader. */
6371 make_region_from_loop_preheader (&preheader_blocks);
6372 else
6373 {
6374 /* If all preheader blocks are empty - dont create new empty region.
6375 Instead, remove them completely. */
ac47786e 6376 FOR_EACH_VEC_ELT (basic_block, preheader_blocks, i, bb)
e855c69d
AB
6377 {
6378 edge e;
6379 edge_iterator ei;
6380 basic_block prev_bb = bb->prev_bb, next_bb = bb->next_bb;
6381
6382 /* Redirect all incoming edges to next basic block. */
6383 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
6384 {
6385 if (! (e->flags & EDGE_FALLTHRU))
6386 redirect_edge_and_branch (e, bb->next_bb);
6387 else
6388 redirect_edge_succ (e, bb->next_bb);
6389 }
6390 gcc_assert (BB_NOTE_LIST (bb) == NULL);
6391 delete_and_free_basic_block (bb);
6392
b8698a0f
L
6393 /* Check if after deleting preheader there is a nonconditional
6394 jump in PREV_BB that leads to the next basic block NEXT_BB.
6395 If it is so - delete this jump and clear data sets of its
e855c69d
AB
6396 basic block if it becomes empty. */
6397 if (next_bb->prev_bb == prev_bb
6398 && prev_bb != ENTRY_BLOCK_PTR
753de8cf 6399 && bb_has_removable_jump_to_p (prev_bb, next_bb))
e855c69d
AB
6400 {
6401 redirect_edge_and_branch (EDGE_SUCC (prev_bb, 0), next_bb);
6402 if (BB_END (prev_bb) == bb_note (prev_bb))
6403 free_data_sets (prev_bb);
6404 }
00c4e97c
AB
6405
6406 set_immediate_dominator (CDI_DOMINATORS, next_bb,
6407 recompute_dominator (CDI_DOMINATORS,
6408 next_bb));
e855c69d
AB
6409 }
6410 }
6411 VEC_free (basic_block, heap, preheader_blocks);
6412 }
6413 else
6414 /* Store preheader within the father's loop structure. */
6415 SET_LOOP_PREHEADER_BLOCKS (loop_outer (current_loop_nest),
6416 preheader_blocks);
6417}
6418#endif