OSDN Git Service

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