OSDN Git Service

* sched-rgn.c (add_branch_dependences): Don't allow insns that throw
[pf3gnuchains/gcc-fork.git] / gcc / sched-rgn.c
1 /* Instruction scheduling pass.
2    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4    Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
5    and currently maintained by, Jim Wilson (wilson@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING.  If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA.  */
23
24 /* This pass implements list scheduling within basic blocks.  It is
25    run twice: (1) after flow analysis, but before register allocation,
26    and (2) after register allocation.
27
28    The first run performs interblock scheduling, moving insns between
29    different blocks in the same "region", and the second runs only
30    basic block scheduling.
31
32    Interblock motions performed are useful motions and speculative
33    motions, including speculative loads.  Motions requiring code
34    duplication are not supported.  The identification of motion type
35    and the check for validity of speculative motions requires
36    construction and analysis of the function's control flow graph.
37
38    The main entry point for this pass is schedule_insns(), called for
39    each function.  The work of the scheduler is organized in three
40    levels: (1) function level: insns are subject to splitting,
41    control-flow-graph is constructed, regions are computed (after
42    reload, each region is of one block), (2) region level: control
43    flow graph attributes required for interblock scheduling are
44    computed (dominators, reachability, etc.), data dependences and
45    priorities are computed, and (3) block level: insns in the block
46    are actually scheduled.  */
47 \f
48 #include "config.h"
49 #include "system.h"
50 #include "toplev.h"
51 #include "rtl.h"
52 #include "tm_p.h"
53 #include "hard-reg-set.h"
54 #include "basic-block.h"
55 #include "regs.h"
56 #include "function.h"
57 #include "flags.h"
58 #include "insn-config.h"
59 #include "insn-attr.h"
60 #include "except.h"
61 #include "toplev.h"
62 #include "recog.h"
63 #include "cfglayout.h"
64 #include "sched-int.h"
65
66 /* Define when we want to do count REG_DEAD notes before and after scheduling
67    for sanity checking.  We can't do that when conditional execution is used,
68    as REG_DEAD exist only for unconditional deaths.  */
69
70 #if !defined (HAVE_conditional_execution) && defined (ENABLE_CHECKING)
71 #define CHECK_DEAD_NOTES 1
72 #else
73 #define CHECK_DEAD_NOTES 0
74 #endif
75
76
77 #ifdef INSN_SCHEDULING
78 /* Some accessor macros for h_i_d members only used within this file.  */
79 #define INSN_REF_COUNT(INSN)    (h_i_d[INSN_UID (INSN)].ref_count)
80 #define FED_BY_SPEC_LOAD(insn)  (h_i_d[INSN_UID (insn)].fed_by_spec_load)
81 #define IS_LOAD_INSN(insn)      (h_i_d[INSN_UID (insn)].is_load_insn)
82
83 #define MAX_RGN_BLOCKS 10
84 #define MAX_RGN_INSNS 100
85
86 /* nr_inter/spec counts interblock/speculative motion for the function.  */
87 static int nr_inter, nr_spec;
88
89 /* Control flow graph edges are kept in circular lists.  */
90 typedef struct
91 {
92   int from_block;
93   int to_block;
94   int next_in;
95   int next_out;
96 }
97 haifa_edge;
98 static haifa_edge *edge_table;
99
100 #define NEXT_IN(edge) (edge_table[edge].next_in)
101 #define NEXT_OUT(edge) (edge_table[edge].next_out)
102 #define FROM_BLOCK(edge) (edge_table[edge].from_block)
103 #define TO_BLOCK(edge) (edge_table[edge].to_block)
104
105 /* Number of edges in the control flow graph.  (In fact, larger than
106    that by 1, since edge 0 is unused.)  */
107 static int nr_edges;
108
109 /* Circular list of incoming/outgoing edges of a block.  */
110 static int *in_edges;
111 static int *out_edges;
112
113 #define IN_EDGES(block) (in_edges[block])
114 #define OUT_EDGES(block) (out_edges[block])
115
116 static int is_cfg_nonregular PARAMS ((void));
117 static int build_control_flow PARAMS ((struct edge_list *));
118 static void new_edge PARAMS ((int, int));
119
120 /* A region is the main entity for interblock scheduling: insns
121    are allowed to move between blocks in the same region, along
122    control flow graph edges, in the 'up' direction.  */
123 typedef struct
124 {
125   int rgn_nr_blocks;            /* Number of blocks in region.  */
126   int rgn_blocks;               /* cblocks in the region (actually index in rgn_bb_table).  */
127 }
128 region;
129
130 /* Number of regions in the procedure.  */
131 static int nr_regions;
132
133 /* Table of region descriptions.  */
134 static region *rgn_table;
135
136 /* Array of lists of regions' blocks.  */
137 static int *rgn_bb_table;
138
139 /* Topological order of blocks in the region (if b2 is reachable from
140    b1, block_to_bb[b2] > block_to_bb[b1]).  Note: A basic block is
141    always referred to by either block or b, while its topological
142    order name (in the region) is refered to by bb.  */
143 static int *block_to_bb;
144
145 /* The number of the region containing a block.  */
146 static int *containing_rgn;
147
148 #define RGN_NR_BLOCKS(rgn) (rgn_table[rgn].rgn_nr_blocks)
149 #define RGN_BLOCKS(rgn) (rgn_table[rgn].rgn_blocks)
150 #define BLOCK_TO_BB(block) (block_to_bb[block])
151 #define CONTAINING_RGN(block) (containing_rgn[block])
152
153 void debug_regions PARAMS ((void));
154 static void find_single_block_region PARAMS ((void));
155 static void find_rgns PARAMS ((struct edge_list *, sbitmap *));
156 static int too_large PARAMS ((int, int *, int *));
157
158 extern void debug_live PARAMS ((int, int));
159
160 /* Blocks of the current region being scheduled.  */
161 static int current_nr_blocks;
162 static int current_blocks;
163
164 /* The mapping from bb to block.  */
165 #define BB_TO_BLOCK(bb) (rgn_bb_table[current_blocks + (bb)])
166
167 typedef struct
168 {
169   int *first_member;            /* Pointer to the list start in bitlst_table.  */
170   int nr_members;               /* The number of members of the bit list.  */
171 }
172 bitlst;
173
174 static int bitlst_table_last;
175 static int bitlst_table_size;
176 static int *bitlst_table;
177
178 static void extract_bitlst PARAMS ((sbitmap, bitlst *));
179
180 /* Target info declarations.
181
182    The block currently being scheduled is referred to as the "target" block,
183    while other blocks in the region from which insns can be moved to the
184    target are called "source" blocks.  The candidate structure holds info
185    about such sources: are they valid?  Speculative?  Etc.  */
186 typedef bitlst bblst;
187 typedef struct
188 {
189   char is_valid;
190   char is_speculative;
191   int src_prob;
192   bblst split_bbs;
193   bblst update_bbs;
194 }
195 candidate;
196
197 static candidate *candidate_table;
198
199 /* A speculative motion requires checking live information on the path
200    from 'source' to 'target'.  The split blocks are those to be checked.
201    After a speculative motion, live information should be modified in
202    the 'update' blocks.
203
204    Lists of split and update blocks for each candidate of the current
205    target are in array bblst_table.  */
206 static int *bblst_table, bblst_size, bblst_last;
207
208 #define IS_VALID(src) ( candidate_table[src].is_valid )
209 #define IS_SPECULATIVE(src) ( candidate_table[src].is_speculative )
210 #define SRC_PROB(src) ( candidate_table[src].src_prob )
211
212 /* The bb being currently scheduled.  */
213 static int target_bb;
214
215 /* List of edges.  */
216 typedef bitlst edgelst;
217
218 /* Target info functions.  */
219 static void split_edges PARAMS ((int, int, edgelst *));
220 static void compute_trg_info PARAMS ((int));
221 void debug_candidate PARAMS ((int));
222 void debug_candidates PARAMS ((int));
223
224 /* Dominators array: dom[i] contains the sbitmap of dominators of
225    bb i in the region.  */
226 static sbitmap *dom;
227
228 /* bb 0 is the only region entry.  */
229 #define IS_RGN_ENTRY(bb) (!bb)
230
231 /* Is bb_src dominated by bb_trg.  */
232 #define IS_DOMINATED(bb_src, bb_trg)                                 \
233 ( TEST_BIT (dom[bb_src], bb_trg) )
234
235 /* Probability: Prob[i] is a float in [0, 1] which is the probability
236    of bb i relative to the region entry.  */
237 static float *prob;
238
239 /* The probability of bb_src, relative to bb_trg.  Note, that while the
240    'prob[bb]' is a float in [0, 1], this macro returns an integer
241    in [0, 100].  */
242 #define GET_SRC_PROB(bb_src, bb_trg) ((int) (100.0 * (prob[bb_src] / \
243                                                       prob[bb_trg])))
244
245 /* Bit-set of edges, where bit i stands for edge i.  */
246 typedef sbitmap edgeset;
247
248 /* Number of edges in the region.  */
249 static int rgn_nr_edges;
250
251 /* Array of size rgn_nr_edges.  */
252 static int *rgn_edges;
253
254
255 /* Mapping from each edge in the graph to its number in the rgn.  */
256 static int *edge_to_bit;
257 #define EDGE_TO_BIT(edge) (edge_to_bit[edge])
258
259 /* The split edges of a source bb is different for each target
260    bb.  In order to compute this efficiently, the 'potential-split edges'
261    are computed for each bb prior to scheduling a region.  This is actually
262    the split edges of each bb relative to the region entry.
263
264    pot_split[bb] is the set of potential split edges of bb.  */
265 static edgeset *pot_split;
266
267 /* For every bb, a set of its ancestor edges.  */
268 static edgeset *ancestor_edges;
269
270 static void compute_dom_prob_ps PARAMS ((int));
271
272 #define ABS_VALUE(x) (((x)<0)?(-(x)):(x))
273 #define INSN_PROBABILITY(INSN) (SRC_PROB (BLOCK_TO_BB (BLOCK_NUM (INSN))))
274 #define IS_SPECULATIVE_INSN(INSN) (IS_SPECULATIVE (BLOCK_TO_BB (BLOCK_NUM (INSN))))
275 #define INSN_BB(INSN) (BLOCK_TO_BB (BLOCK_NUM (INSN)))
276
277 /* Parameters affecting the decision of rank_for_schedule().
278    ??? Nope.  But MIN_PROBABILITY is used in copmute_trg_info.  */
279 #define MIN_DIFF_PRIORITY 2
280 #define MIN_PROBABILITY 40
281 #define MIN_PROB_DIFF 10
282
283 /* Speculative scheduling functions.  */
284 static int check_live_1 PARAMS ((int, rtx));
285 static void update_live_1 PARAMS ((int, rtx));
286 static int check_live PARAMS ((rtx, int));
287 static void update_live PARAMS ((rtx, int));
288 static void set_spec_fed PARAMS ((rtx));
289 static int is_pfree PARAMS ((rtx, int, int));
290 static int find_conditional_protection PARAMS ((rtx, int));
291 static int is_conditionally_protected PARAMS ((rtx, int, int));
292 static int may_trap_exp PARAMS ((rtx, int));
293 static int haifa_classify_insn PARAMS ((rtx));
294 static int is_prisky PARAMS ((rtx, int, int));
295 static int is_exception_free PARAMS ((rtx, int, int));
296
297 static void add_branch_dependences PARAMS ((rtx, rtx));
298 static void compute_block_backward_dependences PARAMS ((int));
299 void debug_dependencies PARAMS ((void));
300
301 static void init_regions PARAMS ((void));
302 static void schedule_region PARAMS ((int));
303 static rtx concat_INSN_LIST PARAMS ((rtx, rtx));
304 static void concat_insn_mem_list PARAMS ((rtx, rtx, rtx *, rtx *));
305 static void propagate_deps PARAMS ((int, struct deps *));
306 static void free_pending_lists PARAMS ((void));
307
308 /* Functions for construction of the control flow graph.  */
309
310 /* Return 1 if control flow graph should not be constructed, 0 otherwise.
311
312    We decide not to build the control flow graph if there is possibly more
313    than one entry to the function, if computed branches exist, of if we
314    have nonlocal gotos.  */
315
316 static int
317 is_cfg_nonregular ()
318 {
319   int b;
320   rtx insn;
321   RTX_CODE code;
322
323   /* If we have a label that could be the target of a nonlocal goto, then
324      the cfg is not well structured.  */
325   if (nonlocal_goto_handler_labels)
326     return 1;
327
328   /* If we have any forced labels, then the cfg is not well structured.  */
329   if (forced_labels)
330     return 1;
331
332   /* If this function has a computed jump, then we consider the cfg
333      not well structured.  */
334   if (current_function_has_computed_jump)
335     return 1;
336
337   /* If we have exception handlers, then we consider the cfg not well
338      structured.  ?!?  We should be able to handle this now that flow.c
339      computes an accurate cfg for EH.  */
340   if (exception_handler_labels)
341     return 1;
342
343   /* If we have non-jumping insns which refer to labels, then we consider
344      the cfg not well structured.  */
345   /* Check for labels referred to other thn by jumps.  */
346   for (b = 0; b < n_basic_blocks; b++)
347     for (insn = BLOCK_HEAD (b);; insn = NEXT_INSN (insn))
348       {
349         code = GET_CODE (insn);
350         if (GET_RTX_CLASS (code) == 'i' && code != JUMP_INSN)
351           {
352             rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
353
354             if (note
355                 && ! (GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
356                       && find_reg_note (NEXT_INSN (insn), REG_LABEL,
357                                         XEXP (note, 0))))
358               return 1;
359           }
360
361         if (insn == BLOCK_END (b))
362           break;
363       }
364
365   /* All the tests passed.  Consider the cfg well structured.  */
366   return 0;
367 }
368
369 /* Build the control flow graph and set nr_edges.
370
371    Instead of trying to build a cfg ourselves, we rely on flow to
372    do it for us.  Stamp out useless code (and bug) duplication.
373
374    Return nonzero if an irregularity in the cfg is found which would
375    prevent cross block scheduling.  */
376
377 static int
378 build_control_flow (edge_list)
379      struct edge_list *edge_list;
380 {
381   int i, unreachable, num_edges;
382
383   /* This already accounts for entry/exit edges.  */
384   num_edges = NUM_EDGES (edge_list);
385
386   /* Unreachable loops with more than one basic block are detected
387      during the DFS traversal in find_rgns.
388
389      Unreachable loops with a single block are detected here.  This
390      test is redundant with the one in find_rgns, but it's much
391     cheaper to go ahead and catch the trivial case here.  */
392   unreachable = 0;
393   for (i = 0; i < n_basic_blocks; i++)
394     {
395       basic_block b = BASIC_BLOCK (i);
396
397       if (b->pred == NULL
398           || (b->pred->src == b
399               && b->pred->pred_next == NULL))
400         unreachable = 1;
401     }
402
403   /* ??? We can kill these soon.  */
404   in_edges = (int *) xcalloc (n_basic_blocks, sizeof (int));
405   out_edges = (int *) xcalloc (n_basic_blocks, sizeof (int));
406   edge_table = (haifa_edge *) xcalloc (num_edges, sizeof (haifa_edge));
407
408   nr_edges = 0;
409   for (i = 0; i < num_edges; i++)
410     {
411       edge e = INDEX_EDGE (edge_list, i);
412
413       if (e->dest != EXIT_BLOCK_PTR
414           && e->src != ENTRY_BLOCK_PTR)
415         new_edge (e->src->index, e->dest->index);
416     }
417
418   /* Increment by 1, since edge 0 is unused.  */
419   nr_edges++;
420
421   return unreachable;
422 }
423
424 /* Record an edge in the control flow graph from SOURCE to TARGET.
425
426    In theory, this is redundant with the s_succs computed above, but
427    we have not converted all of haifa to use information from the
428    integer lists.  */
429
430 static void
431 new_edge (source, target)
432      int source, target;
433 {
434   int e, next_edge;
435   int curr_edge, fst_edge;
436
437   /* Check for duplicates.  */
438   fst_edge = curr_edge = OUT_EDGES (source);
439   while (curr_edge)
440     {
441       if (FROM_BLOCK (curr_edge) == source
442           && TO_BLOCK (curr_edge) == target)
443         {
444           return;
445         }
446
447       curr_edge = NEXT_OUT (curr_edge);
448
449       if (fst_edge == curr_edge)
450         break;
451     }
452
453   e = ++nr_edges;
454
455   FROM_BLOCK (e) = source;
456   TO_BLOCK (e) = target;
457
458   if (OUT_EDGES (source))
459     {
460       next_edge = NEXT_OUT (OUT_EDGES (source));
461       NEXT_OUT (OUT_EDGES (source)) = e;
462       NEXT_OUT (e) = next_edge;
463     }
464   else
465     {
466       OUT_EDGES (source) = e;
467       NEXT_OUT (e) = e;
468     }
469
470   if (IN_EDGES (target))
471     {
472       next_edge = NEXT_IN (IN_EDGES (target));
473       NEXT_IN (IN_EDGES (target)) = e;
474       NEXT_IN (e) = next_edge;
475     }
476   else
477     {
478       IN_EDGES (target) = e;
479       NEXT_IN (e) = e;
480     }
481 }
482
483 /* Translate a bit-set SET to a list BL of the bit-set members.  */
484
485 static void
486 extract_bitlst (set, bl)
487      sbitmap set;
488      bitlst *bl;
489 {
490   int i;
491
492   /* bblst table space is reused in each call to extract_bitlst.  */
493   bitlst_table_last = 0;
494
495   bl->first_member = &bitlst_table[bitlst_table_last];
496   bl->nr_members = 0;
497
498   /* Iterate over each word in the bitset.  */
499   EXECUTE_IF_SET_IN_SBITMAP (set, 0, i,
500   {
501     bitlst_table[bitlst_table_last++] = i;
502     (bl->nr_members)++;
503   });
504
505 }
506
507 /* Functions for the construction of regions.  */
508
509 /* Print the regions, for debugging purposes.  Callable from debugger.  */
510
511 void
512 debug_regions ()
513 {
514   int rgn, bb;
515
516   fprintf (sched_dump, "\n;;   ------------ REGIONS ----------\n\n");
517   for (rgn = 0; rgn < nr_regions; rgn++)
518     {
519       fprintf (sched_dump, ";;\trgn %d nr_blocks %d:\n", rgn,
520                rgn_table[rgn].rgn_nr_blocks);
521       fprintf (sched_dump, ";;\tbb/block: ");
522
523       for (bb = 0; bb < rgn_table[rgn].rgn_nr_blocks; bb++)
524         {
525           current_blocks = RGN_BLOCKS (rgn);
526
527           if (bb != BLOCK_TO_BB (BB_TO_BLOCK (bb)))
528             abort ();
529
530           fprintf (sched_dump, " %d/%d ", bb, BB_TO_BLOCK (bb));
531         }
532
533       fprintf (sched_dump, "\n\n");
534     }
535 }
536
537 /* Build a single block region for each basic block in the function.
538    This allows for using the same code for interblock and basic block
539    scheduling.  */
540
541 static void
542 find_single_block_region ()
543 {
544   int i;
545
546   for (i = 0; i < n_basic_blocks; i++)
547     {
548       rgn_bb_table[i] = i;
549       RGN_NR_BLOCKS (i) = 1;
550       RGN_BLOCKS (i) = i;
551       CONTAINING_RGN (i) = i;
552       BLOCK_TO_BB (i) = 0;
553     }
554   nr_regions = n_basic_blocks;
555 }
556
557 /* Update number of blocks and the estimate for number of insns
558    in the region.  Return 1 if the region is "too large" for interblock
559    scheduling (compile time considerations), otherwise return 0.  */
560
561 static int
562 too_large (block, num_bbs, num_insns)
563      int block, *num_bbs, *num_insns;
564 {
565   (*num_bbs)++;
566   (*num_insns) += (INSN_LUID (BLOCK_END (block)) -
567                    INSN_LUID (BLOCK_HEAD (block)));
568   if ((*num_bbs > MAX_RGN_BLOCKS) || (*num_insns > MAX_RGN_INSNS))
569     return 1;
570   else
571     return 0;
572 }
573
574 /* Update_loop_relations(blk, hdr): Check if the loop headed by max_hdr[blk]
575    is still an inner loop.  Put in max_hdr[blk] the header of the most inner
576    loop containing blk.  */
577 #define UPDATE_LOOP_RELATIONS(blk, hdr)                              \
578 {                                                                    \
579   if (max_hdr[blk] == -1)                                            \
580     max_hdr[blk] = hdr;                                              \
581   else if (dfs_nr[max_hdr[blk]] > dfs_nr[hdr])                       \
582          RESET_BIT (inner, hdr);                                     \
583   else if (dfs_nr[max_hdr[blk]] < dfs_nr[hdr])                       \
584          {                                                           \
585             RESET_BIT (inner,max_hdr[blk]);                          \
586             max_hdr[blk] = hdr;                                      \
587          }                                                           \
588 }
589
590 /* Find regions for interblock scheduling.
591
592    A region for scheduling can be:
593
594      * A loop-free procedure, or
595
596      * A reducible inner loop, or
597
598      * A basic block not contained in any other region.
599
600    ?!? In theory we could build other regions based on extended basic
601    blocks or reverse extended basic blocks.  Is it worth the trouble?
602
603    Loop blocks that form a region are put into the region's block list
604    in topological order.
605
606    This procedure stores its results into the following global (ick) variables
607
608      * rgn_nr
609      * rgn_table
610      * rgn_bb_table
611      * block_to_bb
612      * containing region
613
614    We use dominator relationships to avoid making regions out of non-reducible
615    loops.
616
617    This procedure needs to be converted to work on pred/succ lists instead
618    of edge tables.  That would simplify it somewhat.  */
619
620 static void
621 find_rgns (edge_list, dom)
622      struct edge_list *edge_list;
623      sbitmap *dom;
624 {
625   int *max_hdr, *dfs_nr, *stack, *degree;
626   char no_loops = 1;
627   int node, child, loop_head, i, head, tail;
628   int count = 0, sp, idx = 0, current_edge = out_edges[0];
629   int num_bbs, num_insns, unreachable;
630   int too_large_failure;
631
632   /* Note if an edge has been passed.  */
633   sbitmap passed;
634
635   /* Note if a block is a natural loop header.  */
636   sbitmap header;
637
638   /* Note if a block is an natural inner loop header.  */
639   sbitmap inner;
640
641   /* Note if a block is in the block queue.  */
642   sbitmap in_queue;
643
644   /* Note if a block is in the block queue.  */
645   sbitmap in_stack;
646
647   int num_edges = NUM_EDGES (edge_list);
648
649   /* Perform a DFS traversal of the cfg.  Identify loop headers, inner loops
650      and a mapping from block to its loop header (if the block is contained
651      in a loop, else -1).
652
653      Store results in HEADER, INNER, and MAX_HDR respectively, these will
654      be used as inputs to the second traversal.
655
656      STACK, SP and DFS_NR are only used during the first traversal.  */
657
658   /* Allocate and initialize variables for the first traversal.  */
659   max_hdr = (int *) xmalloc (n_basic_blocks * sizeof (int));
660   dfs_nr = (int *) xcalloc (n_basic_blocks, sizeof (int));
661   stack = (int *) xmalloc (nr_edges * sizeof (int));
662
663   inner = sbitmap_alloc (n_basic_blocks);
664   sbitmap_ones (inner);
665
666   header = sbitmap_alloc (n_basic_blocks);
667   sbitmap_zero (header);
668
669   passed = sbitmap_alloc (nr_edges);
670   sbitmap_zero (passed);
671
672   in_queue = sbitmap_alloc (n_basic_blocks);
673   sbitmap_zero (in_queue);
674
675   in_stack = sbitmap_alloc (n_basic_blocks);
676   sbitmap_zero (in_stack);
677
678   for (i = 0; i < n_basic_blocks; i++)
679     max_hdr[i] = -1;
680
681   /* DFS traversal to find inner loops in the cfg.  */
682
683   sp = -1;
684   while (1)
685     {
686       if (current_edge == 0 || TEST_BIT (passed, current_edge))
687         {
688           /* We have reached a leaf node or a node that was already
689              processed.  Pop edges off the stack until we find
690              an edge that has not yet been processed.  */
691           while (sp >= 0
692                  && (current_edge == 0 || TEST_BIT (passed, current_edge)))
693             {
694               /* Pop entry off the stack.  */
695               current_edge = stack[sp--];
696               node = FROM_BLOCK (current_edge);
697               child = TO_BLOCK (current_edge);
698               RESET_BIT (in_stack, child);
699               if (max_hdr[child] >= 0 && TEST_BIT (in_stack, max_hdr[child]))
700                 UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
701               current_edge = NEXT_OUT (current_edge);
702             }
703
704           /* See if have finished the DFS tree traversal.  */
705           if (sp < 0 && TEST_BIT (passed, current_edge))
706             break;
707
708           /* Nope, continue the traversal with the popped node.  */
709           continue;
710         }
711
712       /* Process a node.  */
713       node = FROM_BLOCK (current_edge);
714       child = TO_BLOCK (current_edge);
715       SET_BIT (in_stack, node);
716       dfs_nr[node] = ++count;
717
718       /* If the successor is in the stack, then we've found a loop.
719          Mark the loop, if it is not a natural loop, then it will
720          be rejected during the second traversal.  */
721       if (TEST_BIT (in_stack, child))
722         {
723           no_loops = 0;
724           SET_BIT (header, child);
725           UPDATE_LOOP_RELATIONS (node, child);
726           SET_BIT (passed, current_edge);
727           current_edge = NEXT_OUT (current_edge);
728           continue;
729         }
730
731       /* If the child was already visited, then there is no need to visit
732          it again.  Just update the loop relationships and restart
733          with a new edge.  */
734       if (dfs_nr[child])
735         {
736           if (max_hdr[child] >= 0 && TEST_BIT (in_stack, max_hdr[child]))
737             UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
738           SET_BIT (passed, current_edge);
739           current_edge = NEXT_OUT (current_edge);
740           continue;
741         }
742
743       /* Push an entry on the stack and continue DFS traversal.  */
744       stack[++sp] = current_edge;
745       SET_BIT (passed, current_edge);
746       current_edge = OUT_EDGES (child);
747
748       /* This is temporary until haifa is converted to use rth's new
749          cfg routines which have true entry/exit blocks and the
750          appropriate edges from/to those blocks.
751
752          Generally we update dfs_nr for a node when we process its
753          out edge.  However, if the node has no out edge then we will
754          not set dfs_nr for that node.  This can confuse the scheduler
755          into thinking that we have unreachable blocks, which in turn
756          disables cross block scheduling.
757
758          So, if we have a node with no out edges, go ahead and mark it
759          as reachable now.  */
760       if (current_edge == 0)
761         dfs_nr[child] = ++count;
762     }
763
764   /* Another check for unreachable blocks.  The earlier test in
765      is_cfg_nonregular only finds unreachable blocks that do not
766      form a loop.
767
768      The DFS traversal will mark every block that is reachable from
769      the entry node by placing a nonzero value in dfs_nr.  Thus if
770      dfs_nr is zero for any block, then it must be unreachable.  */
771   unreachable = 0;
772   for (i = 0; i < n_basic_blocks; i++)
773     if (dfs_nr[i] == 0)
774       {
775         unreachable = 1;
776         break;
777       }
778
779   /* Gross.  To avoid wasting memory, the second pass uses the dfs_nr array
780      to hold degree counts.  */
781   degree = dfs_nr;
782
783   for (i = 0; i < n_basic_blocks; i++)
784     degree[i] = 0;
785   for (i = 0; i < num_edges; i++)
786     {
787       edge e = INDEX_EDGE (edge_list, i);
788
789       if (e->dest != EXIT_BLOCK_PTR)
790         degree[e->dest->index]++;
791     }
792
793   /* Do not perform region scheduling if there are any unreachable
794      blocks.  */
795   if (!unreachable)
796     {
797       int *queue;
798
799       if (no_loops)
800         SET_BIT (header, 0);
801
802       /* Second travsersal:find reducible inner loops and topologically sort
803          block of each region.  */
804
805       queue = (int *) xmalloc (n_basic_blocks * sizeof (int));
806
807       /* Find blocks which are inner loop headers.  We still have non-reducible
808          loops to consider at this point.  */
809       for (i = 0; i < n_basic_blocks; i++)
810         {
811           if (TEST_BIT (header, i) && TEST_BIT (inner, i))
812             {
813               edge e;
814               int j;
815
816               /* Now check that the loop is reducible.  We do this separate
817                  from finding inner loops so that we do not find a reducible
818                  loop which contains an inner non-reducible loop.
819
820                  A simple way to find reducible/natural loops is to verify
821                  that each block in the loop is dominated by the loop
822                  header.
823
824                  If there exists a block that is not dominated by the loop
825                  header, then the block is reachable from outside the loop
826                  and thus the loop is not a natural loop.  */
827               for (j = 0; j < n_basic_blocks; j++)
828                 {
829                   /* First identify blocks in the loop, except for the loop
830                      entry block.  */
831                   if (i == max_hdr[j] && i != j)
832                     {
833                       /* Now verify that the block is dominated by the loop
834                          header.  */
835                       if (!TEST_BIT (dom[j], i))
836                         break;
837                     }
838                 }
839
840               /* If we exited the loop early, then I is the header of
841                  a non-reducible loop and we should quit processing it
842                  now.  */
843               if (j != n_basic_blocks)
844                 continue;
845
846               /* I is a header of an inner loop, or block 0 in a subroutine
847                  with no loops at all.  */
848               head = tail = -1;
849               too_large_failure = 0;
850               loop_head = max_hdr[i];
851
852               /* Decrease degree of all I's successors for topological
853                  ordering.  */
854               for (e = BASIC_BLOCK (i)->succ; e; e = e->succ_next)
855                 if (e->dest != EXIT_BLOCK_PTR)
856                   --degree[e->dest->index];
857
858               /* Estimate # insns, and count # blocks in the region.  */
859               num_bbs = 1;
860               num_insns = (INSN_LUID (BLOCK_END (i))
861                            - INSN_LUID (BLOCK_HEAD (i)));
862
863               /* Find all loop latches (blocks with back edges to the loop
864                  header) or all the leaf blocks in the cfg has no loops.
865
866                  Place those blocks into the queue.  */
867               if (no_loops)
868                 {
869                   for (j = 0; j < n_basic_blocks; j++)
870                     /* Leaf nodes have only a single successor which must
871                        be EXIT_BLOCK.  */
872                     if (BASIC_BLOCK (j)->succ
873                         && BASIC_BLOCK (j)->succ->dest == EXIT_BLOCK_PTR
874                         && BASIC_BLOCK (j)->succ->succ_next == NULL)
875                       {
876                         queue[++tail] = j;
877                         SET_BIT (in_queue, j);
878
879                         if (too_large (j, &num_bbs, &num_insns))
880                           {
881                             too_large_failure = 1;
882                             break;
883                           }
884                       }
885                 }
886               else
887                 {
888                   edge e;
889
890                   for (e = BASIC_BLOCK (i)->pred; e; e = e->pred_next)
891                     {
892                       if (e->src == ENTRY_BLOCK_PTR)
893                         continue;
894
895                       node = e->src->index;
896
897                       if (max_hdr[node] == loop_head && node != i)
898                         {
899                           /* This is a loop latch.  */
900                           queue[++tail] = node;
901                           SET_BIT (in_queue, node);
902
903                           if (too_large (node, &num_bbs, &num_insns))
904                             {
905                               too_large_failure = 1;
906                               break;
907                             }
908                         }
909                     }
910                 }
911
912               /* Now add all the blocks in the loop to the queue.
913
914              We know the loop is a natural loop; however the algorithm
915              above will not always mark certain blocks as being in the
916              loop.  Consider:
917                 node   children
918                  a        b,c
919                  b        c
920                  c        a,d
921                  d        b
922
923              The algorithm in the DFS traversal may not mark B & D as part
924              of the loop (ie they will not have max_hdr set to A).
925
926              We know they can not be loop latches (else they would have
927              had max_hdr set since they'd have a backedge to a dominator
928              block).  So we don't need them on the initial queue.
929
930              We know they are part of the loop because they are dominated
931              by the loop header and can be reached by a backwards walk of
932              the edges starting with nodes on the initial queue.
933
934              It is safe and desirable to include those nodes in the
935              loop/scheduling region.  To do so we would need to decrease
936              the degree of a node if it is the target of a backedge
937              within the loop itself as the node is placed in the queue.
938
939              We do not do this because I'm not sure that the actual
940              scheduling code will properly handle this case. ?!? */
941
942               while (head < tail && !too_large_failure)
943                 {
944                   edge e;
945                   child = queue[++head];
946
947                   for (e = BASIC_BLOCK (child)->pred; e; e = e->pred_next)
948                     {
949                       node = e->src->index;
950
951                       /* See discussion above about nodes not marked as in
952                          this loop during the initial DFS traversal.  */
953                       if (e->src == ENTRY_BLOCK_PTR
954                           || max_hdr[node] != loop_head)
955                         {
956                           tail = -1;
957                           break;
958                         }
959                       else if (!TEST_BIT (in_queue, node) && node != i)
960                         {
961                           queue[++tail] = node;
962                           SET_BIT (in_queue, node);
963
964                           if (too_large (node, &num_bbs, &num_insns))
965                             {
966                               too_large_failure = 1;
967                               break;
968                             }
969                         }
970                     }
971                 }
972
973               if (tail >= 0 && !too_large_failure)
974                 {
975                   /* Place the loop header into list of region blocks.  */
976                   degree[i] = -1;
977                   rgn_bb_table[idx] = i;
978                   RGN_NR_BLOCKS (nr_regions) = num_bbs;
979                   RGN_BLOCKS (nr_regions) = idx++;
980                   CONTAINING_RGN (i) = nr_regions;
981                   BLOCK_TO_BB (i) = count = 0;
982
983                   /* Remove blocks from queue[] when their in degree
984                      becomes zero.  Repeat until no blocks are left on the
985                      list.  This produces a topological list of blocks in
986                      the region.  */
987                   while (tail >= 0)
988                     {
989                       if (head < 0)
990                         head = tail;
991                       child = queue[head];
992                       if (degree[child] == 0)
993                         {
994                           edge e;
995
996                           degree[child] = -1;
997                           rgn_bb_table[idx++] = child;
998                           BLOCK_TO_BB (child) = ++count;
999                           CONTAINING_RGN (child) = nr_regions;
1000                           queue[head] = queue[tail--];
1001
1002                           for (e = BASIC_BLOCK (child)->succ;
1003                                e;
1004                                e = e->succ_next)
1005                             if (e->dest != EXIT_BLOCK_PTR)
1006                               --degree[e->dest->index];
1007                         }
1008                       else
1009                         --head;
1010                     }
1011                   ++nr_regions;
1012                 }
1013             }
1014         }
1015       free (queue);
1016     }
1017
1018   /* Any block that did not end up in a region is placed into a region
1019      by itself.  */
1020   for (i = 0; i < n_basic_blocks; i++)
1021     if (degree[i] >= 0)
1022       {
1023         rgn_bb_table[idx] = i;
1024         RGN_NR_BLOCKS (nr_regions) = 1;
1025         RGN_BLOCKS (nr_regions) = idx++;
1026         CONTAINING_RGN (i) = nr_regions++;
1027         BLOCK_TO_BB (i) = 0;
1028       }
1029
1030   free (max_hdr);
1031   free (dfs_nr);
1032   free (stack);
1033   sbitmap_free (passed);
1034   sbitmap_free (header);
1035   sbitmap_free (inner);
1036   sbitmap_free (in_queue);
1037   sbitmap_free (in_stack);
1038 }
1039
1040 /* Functions for regions scheduling information.  */
1041
1042 /* Compute dominators, probability, and potential-split-edges of bb.
1043    Assume that these values were already computed for bb's predecessors.  */
1044
1045 static void
1046 compute_dom_prob_ps (bb)
1047      int bb;
1048 {
1049   int nxt_in_edge, fst_in_edge, pred;
1050   int fst_out_edge, nxt_out_edge, nr_out_edges, nr_rgn_out_edges;
1051
1052   prob[bb] = 0.0;
1053   if (IS_RGN_ENTRY (bb))
1054     {
1055       SET_BIT (dom[bb], 0);
1056       prob[bb] = 1.0;
1057       return;
1058     }
1059
1060   fst_in_edge = nxt_in_edge = IN_EDGES (BB_TO_BLOCK (bb));
1061
1062   /* Initialize dom[bb] to '111..1'.  */
1063   sbitmap_ones (dom[bb]);
1064
1065   do
1066     {
1067       pred = FROM_BLOCK (nxt_in_edge);
1068       sbitmap_a_and_b (dom[bb], dom[bb], dom[BLOCK_TO_BB (pred)]);
1069       sbitmap_a_or_b (ancestor_edges[bb], ancestor_edges[bb], ancestor_edges[BLOCK_TO_BB (pred)]);
1070
1071       SET_BIT (ancestor_edges[bb], EDGE_TO_BIT (nxt_in_edge));
1072
1073       nr_out_edges = 1;
1074       nr_rgn_out_edges = 0;
1075       fst_out_edge = OUT_EDGES (pred);
1076       nxt_out_edge = NEXT_OUT (fst_out_edge);
1077
1078       sbitmap_a_or_b (pot_split[bb], pot_split[bb], pot_split[BLOCK_TO_BB (pred)]);
1079
1080       SET_BIT (pot_split[bb], EDGE_TO_BIT (fst_out_edge));
1081
1082       /* The successor doesn't belong in the region?  */
1083       if (CONTAINING_RGN (TO_BLOCK (fst_out_edge)) !=
1084           CONTAINING_RGN (BB_TO_BLOCK (bb)))
1085         ++nr_rgn_out_edges;
1086
1087       while (fst_out_edge != nxt_out_edge)
1088         {
1089           ++nr_out_edges;
1090           /* The successor doesn't belong in the region?  */
1091           if (CONTAINING_RGN (TO_BLOCK (nxt_out_edge)) !=
1092               CONTAINING_RGN (BB_TO_BLOCK (bb)))
1093             ++nr_rgn_out_edges;
1094         SET_BIT (pot_split[bb], EDGE_TO_BIT (nxt_out_edge));
1095           nxt_out_edge = NEXT_OUT (nxt_out_edge);
1096
1097         }
1098
1099       /* Now nr_rgn_out_edges is the number of region-exit edges from
1100          pred, and nr_out_edges will be the number of pred out edges
1101          not leaving the region.  */
1102       nr_out_edges -= nr_rgn_out_edges;
1103       if (nr_rgn_out_edges > 0)
1104         prob[bb] += 0.9 * prob[BLOCK_TO_BB (pred)] / nr_out_edges;
1105       else
1106         prob[bb] += prob[BLOCK_TO_BB (pred)] / nr_out_edges;
1107       nxt_in_edge = NEXT_IN (nxt_in_edge);
1108     }
1109   while (fst_in_edge != nxt_in_edge);
1110
1111   SET_BIT (dom[bb], bb);
1112   sbitmap_difference (pot_split[bb], pot_split[bb], ancestor_edges[bb]);
1113
1114   if (sched_verbose >= 2)
1115     fprintf (sched_dump, ";;  bb_prob(%d, %d) = %3d\n", bb, BB_TO_BLOCK (bb),
1116              (int) (100.0 * prob[bb]));
1117 }
1118
1119 /* Functions for target info.  */
1120
1121 /* Compute in BL the list of split-edges of bb_src relatively to bb_trg.
1122    Note that bb_trg dominates bb_src.  */
1123
1124 static void
1125 split_edges (bb_src, bb_trg, bl)
1126      int bb_src;
1127      int bb_trg;
1128      edgelst *bl;
1129 {
1130   sbitmap src = (edgeset) sbitmap_alloc (pot_split[bb_src]->n_bits);
1131   sbitmap_copy (src, pot_split[bb_src]);
1132
1133   sbitmap_difference (src, src, pot_split[bb_trg]);
1134   extract_bitlst (src, bl);
1135   sbitmap_free (src);
1136 }
1137
1138 /* Find the valid candidate-source-blocks for the target block TRG, compute
1139    their probability, and check if they are speculative or not.
1140    For speculative sources, compute their update-blocks and split-blocks.  */
1141
1142 static void
1143 compute_trg_info (trg)
1144      int trg;
1145 {
1146   candidate *sp;
1147   edgelst el;
1148   int check_block, update_idx;
1149   int i, j, k, fst_edge, nxt_edge;
1150
1151   /* Define some of the fields for the target bb as well.  */
1152   sp = candidate_table + trg;
1153   sp->is_valid = 1;
1154   sp->is_speculative = 0;
1155   sp->src_prob = 100;
1156
1157   for (i = trg + 1; i < current_nr_blocks; i++)
1158     {
1159       sp = candidate_table + i;
1160
1161       sp->is_valid = IS_DOMINATED (i, trg);
1162       if (sp->is_valid)
1163         {
1164           sp->src_prob = GET_SRC_PROB (i, trg);
1165           sp->is_valid = (sp->src_prob >= MIN_PROBABILITY);
1166         }
1167
1168       if (sp->is_valid)
1169         {
1170           split_edges (i, trg, &el);
1171           sp->is_speculative = (el.nr_members) ? 1 : 0;
1172           if (sp->is_speculative && !flag_schedule_speculative)
1173             sp->is_valid = 0;
1174         }
1175
1176       if (sp->is_valid)
1177         {
1178           char *update_blocks;
1179
1180           /* Compute split blocks and store them in bblst_table.
1181              The TO block of every split edge is a split block.  */
1182           sp->split_bbs.first_member = &bblst_table[bblst_last];
1183           sp->split_bbs.nr_members = el.nr_members;
1184           for (j = 0; j < el.nr_members; bblst_last++, j++)
1185             bblst_table[bblst_last] =
1186               TO_BLOCK (rgn_edges[el.first_member[j]]);
1187           sp->update_bbs.first_member = &bblst_table[bblst_last];
1188
1189           /* Compute update blocks and store them in bblst_table.
1190              For every split edge, look at the FROM block, and check
1191              all out edges.  For each out edge that is not a split edge,
1192              add the TO block to the update block list.  This list can end
1193              up with a lot of duplicates.  We need to weed them out to avoid
1194              overrunning the end of the bblst_table.  */
1195           update_blocks = (char *) alloca (n_basic_blocks);
1196           memset (update_blocks, 0, n_basic_blocks);
1197
1198           update_idx = 0;
1199           for (j = 0; j < el.nr_members; j++)
1200             {
1201               check_block = FROM_BLOCK (rgn_edges[el.first_member[j]]);
1202               fst_edge = nxt_edge = OUT_EDGES (check_block);
1203               do
1204                 {
1205                   if (! update_blocks[TO_BLOCK (nxt_edge)])
1206                     {
1207                       for (k = 0; k < el.nr_members; k++)
1208                         if (EDGE_TO_BIT (nxt_edge) == el.first_member[k])
1209                           break;
1210
1211                       if (k >= el.nr_members)
1212                         {
1213                           bblst_table[bblst_last++] = TO_BLOCK (nxt_edge);
1214                           update_blocks[TO_BLOCK (nxt_edge)] = 1;
1215                           update_idx++;
1216                         }
1217                     }
1218
1219                   nxt_edge = NEXT_OUT (nxt_edge);
1220                 }
1221               while (fst_edge != nxt_edge);
1222             }
1223           sp->update_bbs.nr_members = update_idx;
1224
1225           /* Make sure we didn't overrun the end of bblst_table.  */
1226           if (bblst_last > bblst_size)
1227             abort ();
1228         }
1229       else
1230         {
1231           sp->split_bbs.nr_members = sp->update_bbs.nr_members = 0;
1232
1233           sp->is_speculative = 0;
1234           sp->src_prob = 0;
1235         }
1236     }
1237 }
1238
1239 /* Print candidates info, for debugging purposes.  Callable from debugger.  */
1240
1241 void
1242 debug_candidate (i)
1243      int i;
1244 {
1245   if (!candidate_table[i].is_valid)
1246     return;
1247
1248   if (candidate_table[i].is_speculative)
1249     {
1250       int j;
1251       fprintf (sched_dump, "src b %d bb %d speculative \n", BB_TO_BLOCK (i), i);
1252
1253       fprintf (sched_dump, "split path: ");
1254       for (j = 0; j < candidate_table[i].split_bbs.nr_members; j++)
1255         {
1256           int b = candidate_table[i].split_bbs.first_member[j];
1257
1258           fprintf (sched_dump, " %d ", b);
1259         }
1260       fprintf (sched_dump, "\n");
1261
1262       fprintf (sched_dump, "update path: ");
1263       for (j = 0; j < candidate_table[i].update_bbs.nr_members; j++)
1264         {
1265           int b = candidate_table[i].update_bbs.first_member[j];
1266
1267           fprintf (sched_dump, " %d ", b);
1268         }
1269       fprintf (sched_dump, "\n");
1270     }
1271   else
1272     {
1273       fprintf (sched_dump, " src %d equivalent\n", BB_TO_BLOCK (i));
1274     }
1275 }
1276
1277 /* Print candidates info, for debugging purposes.  Callable from debugger.  */
1278
1279 void
1280 debug_candidates (trg)
1281      int trg;
1282 {
1283   int i;
1284
1285   fprintf (sched_dump, "----------- candidate table: target: b=%d bb=%d ---\n",
1286            BB_TO_BLOCK (trg), trg);
1287   for (i = trg + 1; i < current_nr_blocks; i++)
1288     debug_candidate (i);
1289 }
1290
1291 /* Functions for speculative scheduing.  */
1292
1293 /* Return 0 if x is a set of a register alive in the beginning of one
1294    of the split-blocks of src, otherwise return 1.  */
1295
1296 static int
1297 check_live_1 (src, x)
1298      int src;
1299      rtx x;
1300 {
1301   int i;
1302   int regno;
1303   rtx reg = SET_DEST (x);
1304
1305   if (reg == 0)
1306     return 1;
1307
1308   while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
1309          || GET_CODE (reg) == SIGN_EXTRACT
1310          || GET_CODE (reg) == STRICT_LOW_PART)
1311     reg = XEXP (reg, 0);
1312
1313   if (GET_CODE (reg) == PARALLEL)
1314     {
1315       int i;
1316
1317       for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
1318         if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
1319           if (check_live_1 (src, XEXP (XVECEXP (reg, 0, i), 0)))
1320             return 1;
1321
1322       return 0;
1323     }
1324
1325   if (GET_CODE (reg) != REG)
1326     return 1;
1327
1328   regno = REGNO (reg);
1329
1330   if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
1331     {
1332       /* Global registers are assumed live.  */
1333       return 0;
1334     }
1335   else
1336     {
1337       if (regno < FIRST_PSEUDO_REGISTER)
1338         {
1339           /* Check for hard registers.  */
1340           int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
1341           while (--j >= 0)
1342             {
1343               for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
1344                 {
1345                   int b = candidate_table[src].split_bbs.first_member[i];
1346
1347                   if (REGNO_REG_SET_P (BASIC_BLOCK (b)->global_live_at_start,
1348                                        regno + j))
1349                     {
1350                       return 0;
1351                     }
1352                 }
1353             }
1354         }
1355       else
1356         {
1357           /* Check for psuedo registers.  */
1358           for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
1359             {
1360               int b = candidate_table[src].split_bbs.first_member[i];
1361
1362               if (REGNO_REG_SET_P (BASIC_BLOCK (b)->global_live_at_start, regno))
1363                 {
1364                   return 0;
1365                 }
1366             }
1367         }
1368     }
1369
1370   return 1;
1371 }
1372
1373 /* If x is a set of a register R, mark that R is alive in the beginning
1374    of every update-block of src.  */
1375
1376 static void
1377 update_live_1 (src, x)
1378      int src;
1379      rtx x;
1380 {
1381   int i;
1382   int regno;
1383   rtx reg = SET_DEST (x);
1384
1385   if (reg == 0)
1386     return;
1387
1388   while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
1389          || GET_CODE (reg) == SIGN_EXTRACT
1390          || GET_CODE (reg) == STRICT_LOW_PART)
1391     reg = XEXP (reg, 0);
1392
1393   if (GET_CODE (reg) == PARALLEL)
1394     {
1395       int i;
1396
1397       for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
1398         if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
1399           update_live_1 (src, XEXP (XVECEXP (reg, 0, i), 0));
1400
1401       return;
1402     }
1403
1404   if (GET_CODE (reg) != REG)
1405     return;
1406
1407   /* Global registers are always live, so the code below does not apply
1408      to them.  */
1409
1410   regno = REGNO (reg);
1411
1412   if (regno >= FIRST_PSEUDO_REGISTER || !global_regs[regno])
1413     {
1414       if (regno < FIRST_PSEUDO_REGISTER)
1415         {
1416           int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
1417           while (--j >= 0)
1418             {
1419               for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
1420                 {
1421                   int b = candidate_table[src].update_bbs.first_member[i];
1422
1423                   SET_REGNO_REG_SET (BASIC_BLOCK (b)->global_live_at_start,
1424                                      regno + j);
1425                 }
1426             }
1427         }
1428       else
1429         {
1430           for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
1431             {
1432               int b = candidate_table[src].update_bbs.first_member[i];
1433
1434               SET_REGNO_REG_SET (BASIC_BLOCK (b)->global_live_at_start, regno);
1435             }
1436         }
1437     }
1438 }
1439
1440 /* Return 1 if insn can be speculatively moved from block src to trg,
1441    otherwise return 0.  Called before first insertion of insn to
1442    ready-list or before the scheduling.  */
1443
1444 static int
1445 check_live (insn, src)
1446      rtx insn;
1447      int src;
1448 {
1449   /* Find the registers set by instruction.  */
1450   if (GET_CODE (PATTERN (insn)) == SET
1451       || GET_CODE (PATTERN (insn)) == CLOBBER)
1452     return check_live_1 (src, PATTERN (insn));
1453   else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1454     {
1455       int j;
1456       for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
1457         if ((GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
1458              || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
1459             && !check_live_1 (src, XVECEXP (PATTERN (insn), 0, j)))
1460           return 0;
1461
1462       return 1;
1463     }
1464
1465   return 1;
1466 }
1467
1468 /* Update the live registers info after insn was moved speculatively from
1469    block src to trg.  */
1470
1471 static void
1472 update_live (insn, src)
1473      rtx insn;
1474      int src;
1475 {
1476   /* Find the registers set by instruction.  */
1477   if (GET_CODE (PATTERN (insn)) == SET
1478       || GET_CODE (PATTERN (insn)) == CLOBBER)
1479     update_live_1 (src, PATTERN (insn));
1480   else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1481     {
1482       int j;
1483       for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
1484         if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
1485             || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
1486           update_live_1 (src, XVECEXP (PATTERN (insn), 0, j));
1487     }
1488 }
1489
1490 /* Exception Free Loads:
1491
1492    We define five classes of speculative loads: IFREE, IRISKY,
1493    PFREE, PRISKY, and MFREE.
1494
1495    IFREE loads are loads that are proved to be exception-free, just
1496    by examining the load insn.  Examples for such loads are loads
1497    from TOC and loads of global data.
1498
1499    IRISKY loads are loads that are proved to be exception-risky,
1500    just by examining the load insn.  Examples for such loads are
1501    volatile loads and loads from shared memory.
1502
1503    PFREE loads are loads for which we can prove, by examining other
1504    insns, that they are exception-free.  Currently, this class consists
1505    of loads for which we are able to find a "similar load", either in
1506    the target block, or, if only one split-block exists, in that split
1507    block.  Load2 is similar to load1 if both have same single base
1508    register.  We identify only part of the similar loads, by finding
1509    an insn upon which both load1 and load2 have a DEF-USE dependence.
1510
1511    PRISKY loads are loads for which we can prove, by examining other
1512    insns, that they are exception-risky.  Currently we have two proofs for
1513    such loads.  The first proof detects loads that are probably guarded by a
1514    test on the memory address.  This proof is based on the
1515    backward and forward data dependence information for the region.
1516    Let load-insn be the examined load.
1517    Load-insn is PRISKY iff ALL the following hold:
1518
1519    - insn1 is not in the same block as load-insn
1520    - there is a DEF-USE dependence chain (insn1, ..., load-insn)
1521    - test-insn is either a compare or a branch, not in the same block
1522      as load-insn
1523    - load-insn is reachable from test-insn
1524    - there is a DEF-USE dependence chain (insn1, ..., test-insn)
1525
1526    This proof might fail when the compare and the load are fed
1527    by an insn not in the region.  To solve this, we will add to this
1528    group all loads that have no input DEF-USE dependence.
1529
1530    The second proof detects loads that are directly or indirectly
1531    fed by a speculative load.  This proof is affected by the
1532    scheduling process.  We will use the flag  fed_by_spec_load.
1533    Initially, all insns have this flag reset.  After a speculative
1534    motion of an insn, if insn is either a load, or marked as
1535    fed_by_spec_load, we will also mark as fed_by_spec_load every
1536    insn1 for which a DEF-USE dependence (insn, insn1) exists.  A
1537    load which is fed_by_spec_load is also PRISKY.
1538
1539    MFREE (maybe-free) loads are all the remaining loads. They may be
1540    exception-free, but we cannot prove it.
1541
1542    Now, all loads in IFREE and PFREE classes are considered
1543    exception-free, while all loads in IRISKY and PRISKY classes are
1544    considered exception-risky.  As for loads in the MFREE class,
1545    these are considered either exception-free or exception-risky,
1546    depending on whether we are pessimistic or optimistic.  We have
1547    to take the pessimistic approach to assure the safety of
1548    speculative scheduling, but we can take the optimistic approach
1549    by invoking the -fsched_spec_load_dangerous option.  */
1550
1551 enum INSN_TRAP_CLASS
1552 {
1553   TRAP_FREE = 0, IFREE = 1, PFREE_CANDIDATE = 2,
1554   PRISKY_CANDIDATE = 3, IRISKY = 4, TRAP_RISKY = 5
1555 };
1556
1557 #define WORST_CLASS(class1, class2) \
1558 ((class1 > class2) ? class1 : class2)
1559
1560 /* Non-zero if block bb_to is equal to, or reachable from block bb_from.  */
1561 #define IS_REACHABLE(bb_from, bb_to)                                    \
1562 (bb_from == bb_to                                                       \
1563    || IS_RGN_ENTRY (bb_from)                                            \
1564    || (TEST_BIT (ancestor_edges[bb_to],                               \
1565                     EDGE_TO_BIT (IN_EDGES (BB_TO_BLOCK (bb_from))))))
1566
1567 /* Non-zero iff the address is comprised from at most 1 register.  */
1568 #define CONST_BASED_ADDRESS_P(x)                        \
1569   (GET_CODE (x) == REG                                  \
1570    || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS   \
1571         || (GET_CODE (x) == LO_SUM))                    \
1572        && (CONSTANT_P (XEXP (x, 0))             \
1573            || CONSTANT_P (XEXP (x, 1)))))
1574
1575 /* Turns on the fed_by_spec_load flag for insns fed by load_insn.  */
1576
1577 static void
1578 set_spec_fed (load_insn)
1579      rtx load_insn;
1580 {
1581   rtx link;
1582
1583   for (link = INSN_DEPEND (load_insn); link; link = XEXP (link, 1))
1584     if (GET_MODE (link) == VOIDmode)
1585       FED_BY_SPEC_LOAD (XEXP (link, 0)) = 1;
1586 }                               /* set_spec_fed */
1587
1588 /* On the path from the insn to load_insn_bb, find a conditional
1589 branch depending on insn, that guards the speculative load.  */
1590
1591 static int
1592 find_conditional_protection (insn, load_insn_bb)
1593      rtx insn;
1594      int load_insn_bb;
1595 {
1596   rtx link;
1597
1598   /* Iterate through DEF-USE forward dependences.  */
1599   for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
1600     {
1601       rtx next = XEXP (link, 0);
1602       if ((CONTAINING_RGN (BLOCK_NUM (next)) ==
1603            CONTAINING_RGN (BB_TO_BLOCK (load_insn_bb)))
1604           && IS_REACHABLE (INSN_BB (next), load_insn_bb)
1605           && load_insn_bb != INSN_BB (next)
1606           && GET_MODE (link) == VOIDmode
1607           && (GET_CODE (next) == JUMP_INSN
1608               || find_conditional_protection (next, load_insn_bb)))
1609         return 1;
1610     }
1611   return 0;
1612 }                               /* find_conditional_protection */
1613
1614 /* Returns 1 if the same insn1 that participates in the computation
1615    of load_insn's address is feeding a conditional branch that is
1616    guarding on load_insn. This is true if we find a the two DEF-USE
1617    chains:
1618    insn1 -> ... -> conditional-branch
1619    insn1 -> ... -> load_insn,
1620    and if a flow path exist:
1621    insn1 -> ... -> conditional-branch -> ... -> load_insn,
1622    and if insn1 is on the path
1623    region-entry -> ... -> bb_trg -> ... load_insn.
1624
1625    Locate insn1 by climbing on LOG_LINKS from load_insn.
1626    Locate the branch by following INSN_DEPEND from insn1.  */
1627
1628 static int
1629 is_conditionally_protected (load_insn, bb_src, bb_trg)
1630      rtx load_insn;
1631      int bb_src, bb_trg;
1632 {
1633   rtx link;
1634
1635   for (link = LOG_LINKS (load_insn); link; link = XEXP (link, 1))
1636     {
1637       rtx insn1 = XEXP (link, 0);
1638
1639       /* Must be a DEF-USE dependence upon non-branch.  */
1640       if (GET_MODE (link) != VOIDmode
1641           || GET_CODE (insn1) == JUMP_INSN)
1642         continue;
1643
1644       /* Must exist a path: region-entry -> ... -> bb_trg -> ... load_insn.  */
1645       if (INSN_BB (insn1) == bb_src
1646           || (CONTAINING_RGN (BLOCK_NUM (insn1))
1647               != CONTAINING_RGN (BB_TO_BLOCK (bb_src)))
1648           || (!IS_REACHABLE (bb_trg, INSN_BB (insn1))
1649               && !IS_REACHABLE (INSN_BB (insn1), bb_trg)))
1650         continue;
1651
1652       /* Now search for the conditional-branch.  */
1653       if (find_conditional_protection (insn1, bb_src))
1654         return 1;
1655
1656       /* Recursive step: search another insn1, "above" current insn1.  */
1657       return is_conditionally_protected (insn1, bb_src, bb_trg);
1658     }
1659
1660   /* The chain does not exist.  */
1661   return 0;
1662 }                               /* is_conditionally_protected */
1663
1664 /* Returns 1 if a clue for "similar load" 'insn2' is found, and hence
1665    load_insn can move speculatively from bb_src to bb_trg.  All the
1666    following must hold:
1667
1668    (1) both loads have 1 base register (PFREE_CANDIDATEs).
1669    (2) load_insn and load1 have a def-use dependence upon
1670    the same insn 'insn1'.
1671    (3) either load2 is in bb_trg, or:
1672    - there's only one split-block, and
1673    - load1 is on the escape path, and
1674
1675    From all these we can conclude that the two loads access memory
1676    addresses that differ at most by a constant, and hence if moving
1677    load_insn would cause an exception, it would have been caused by
1678    load2 anyhow.  */
1679
1680 static int
1681 is_pfree (load_insn, bb_src, bb_trg)
1682      rtx load_insn;
1683      int bb_src, bb_trg;
1684 {
1685   rtx back_link;
1686   candidate *candp = candidate_table + bb_src;
1687
1688   if (candp->split_bbs.nr_members != 1)
1689     /* Must have exactly one escape block.  */
1690     return 0;
1691
1692   for (back_link = LOG_LINKS (load_insn);
1693        back_link; back_link = XEXP (back_link, 1))
1694     {
1695       rtx insn1 = XEXP (back_link, 0);
1696
1697       if (GET_MODE (back_link) == VOIDmode)
1698         {
1699           /* Found a DEF-USE dependence (insn1, load_insn).  */
1700           rtx fore_link;
1701
1702           for (fore_link = INSN_DEPEND (insn1);
1703                fore_link; fore_link = XEXP (fore_link, 1))
1704             {
1705               rtx insn2 = XEXP (fore_link, 0);
1706               if (GET_MODE (fore_link) == VOIDmode)
1707                 {
1708                   /* Found a DEF-USE dependence (insn1, insn2).  */
1709                   if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
1710                     /* insn2 not guaranteed to be a 1 base reg load.  */
1711                     continue;
1712
1713                   if (INSN_BB (insn2) == bb_trg)
1714                     /* insn2 is the similar load, in the target block.  */
1715                     return 1;
1716
1717                   if (*(candp->split_bbs.first_member) == BLOCK_NUM (insn2))
1718                     /* insn2 is a similar load, in a split-block.  */
1719                     return 1;
1720                 }
1721             }
1722         }
1723     }
1724
1725   /* Couldn't find a similar load.  */
1726   return 0;
1727 }                               /* is_pfree */
1728
1729 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
1730    as found by analyzing insn's expression.  */
1731
1732 static int
1733 may_trap_exp (x, is_store)
1734      rtx x;
1735      int is_store;
1736 {
1737   enum rtx_code code;
1738
1739   if (x == 0)
1740     return TRAP_FREE;
1741   code = GET_CODE (x);
1742   if (is_store)
1743     {
1744       if (code == MEM && may_trap_p (x))
1745         return TRAP_RISKY;
1746       else
1747         return TRAP_FREE;
1748     }
1749   if (code == MEM)
1750     {
1751       /* The insn uses memory:  a volatile load.  */
1752       if (MEM_VOLATILE_P (x))
1753         return IRISKY;
1754       /* An exception-free load.  */
1755       if (!may_trap_p (x))
1756         return IFREE;
1757       /* A load with 1 base register, to be further checked.  */
1758       if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
1759         return PFREE_CANDIDATE;
1760       /* No info on the load, to be further checked.  */
1761       return PRISKY_CANDIDATE;
1762     }
1763   else
1764     {
1765       const char *fmt;
1766       int i, insn_class = TRAP_FREE;
1767
1768       /* Neither store nor load, check if it may cause a trap.  */
1769       if (may_trap_p (x))
1770         return TRAP_RISKY;
1771       /* Recursive step: walk the insn...  */
1772       fmt = GET_RTX_FORMAT (code);
1773       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1774         {
1775           if (fmt[i] == 'e')
1776             {
1777               int tmp_class = may_trap_exp (XEXP (x, i), is_store);
1778               insn_class = WORST_CLASS (insn_class, tmp_class);
1779             }
1780           else if (fmt[i] == 'E')
1781             {
1782               int j;
1783               for (j = 0; j < XVECLEN (x, i); j++)
1784                 {
1785                   int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
1786                   insn_class = WORST_CLASS (insn_class, tmp_class);
1787                   if (insn_class == TRAP_RISKY || insn_class == IRISKY)
1788                     break;
1789                 }
1790             }
1791           if (insn_class == TRAP_RISKY || insn_class == IRISKY)
1792             break;
1793         }
1794       return insn_class;
1795     }
1796 }
1797
1798 /* Classifies insn for the purpose of verifying that it can be
1799    moved speculatively, by examining it's patterns, returning:
1800    TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
1801    TRAP_FREE: non-load insn.
1802    IFREE: load from a globaly safe location.
1803    IRISKY: volatile load.
1804    PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
1805    being either PFREE or PRISKY.  */
1806
1807 static int
1808 haifa_classify_insn (insn)
1809      rtx insn;
1810 {
1811   rtx pat = PATTERN (insn);
1812   int tmp_class = TRAP_FREE;
1813   int insn_class = TRAP_FREE;
1814   enum rtx_code code;
1815
1816   if (GET_CODE (pat) == PARALLEL)
1817     {
1818       int i, len = XVECLEN (pat, 0);
1819
1820       for (i = len - 1; i >= 0; i--)
1821         {
1822           code = GET_CODE (XVECEXP (pat, 0, i));
1823           switch (code)
1824             {
1825             case CLOBBER:
1826               /* Test if it is a 'store'.  */
1827               tmp_class = may_trap_exp (XEXP (XVECEXP (pat, 0, i), 0), 1);
1828               break;
1829             case SET:
1830               /* Test if it is a store.  */
1831               tmp_class = may_trap_exp (SET_DEST (XVECEXP (pat, 0, i)), 1);
1832               if (tmp_class == TRAP_RISKY)
1833                 break;
1834               /* Test if it is a load.  */
1835               tmp_class
1836                 = WORST_CLASS (tmp_class,
1837                                may_trap_exp (SET_SRC (XVECEXP (pat, 0, i)),
1838                                              0));
1839               break;
1840             case COND_EXEC:
1841             case TRAP_IF:
1842               tmp_class = TRAP_RISKY;
1843               break;
1844             default:
1845               ;
1846             }
1847           insn_class = WORST_CLASS (insn_class, tmp_class);
1848           if (insn_class == TRAP_RISKY || insn_class == IRISKY)
1849             break;
1850         }
1851     }
1852   else
1853     {
1854       code = GET_CODE (pat);
1855       switch (code)
1856         {
1857         case CLOBBER:
1858           /* Test if it is a 'store'.  */
1859           tmp_class = may_trap_exp (XEXP (pat, 0), 1);
1860           break;
1861         case SET:
1862           /* Test if it is a store.  */
1863           tmp_class = may_trap_exp (SET_DEST (pat), 1);
1864           if (tmp_class == TRAP_RISKY)
1865             break;
1866           /* Test if it is a load.  */
1867           tmp_class =
1868             WORST_CLASS (tmp_class,
1869                          may_trap_exp (SET_SRC (pat), 0));
1870           break;
1871         case COND_EXEC:
1872         case TRAP_IF:
1873           tmp_class = TRAP_RISKY;
1874           break;
1875         default:;
1876         }
1877       insn_class = tmp_class;
1878     }
1879
1880   return insn_class;
1881 }
1882
1883 /* Return 1 if load_insn is prisky (i.e. if load_insn is fed by
1884    a load moved speculatively, or if load_insn is protected by
1885    a compare on load_insn's address).  */
1886
1887 static int
1888 is_prisky (load_insn, bb_src, bb_trg)
1889      rtx load_insn;
1890      int bb_src, bb_trg;
1891 {
1892   if (FED_BY_SPEC_LOAD (load_insn))
1893     return 1;
1894
1895   if (LOG_LINKS (load_insn) == NULL)
1896     /* Dependence may 'hide' out of the region.  */
1897     return 1;
1898
1899   if (is_conditionally_protected (load_insn, bb_src, bb_trg))
1900     return 1;
1901
1902   return 0;
1903 }
1904
1905 /* Insn is a candidate to be moved speculatively from bb_src to bb_trg.
1906    Return 1 if insn is exception-free (and the motion is valid)
1907    and 0 otherwise.  */
1908
1909 static int
1910 is_exception_free (insn, bb_src, bb_trg)
1911      rtx insn;
1912      int bb_src, bb_trg;
1913 {
1914   int insn_class = haifa_classify_insn (insn);
1915
1916   /* Handle non-load insns.  */
1917   switch (insn_class)
1918     {
1919     case TRAP_FREE:
1920       return 1;
1921     case TRAP_RISKY:
1922       return 0;
1923     default:;
1924     }
1925
1926   /* Handle loads.  */
1927   if (!flag_schedule_speculative_load)
1928     return 0;
1929   IS_LOAD_INSN (insn) = 1;
1930   switch (insn_class)
1931     {
1932     case IFREE:
1933       return (1);
1934     case IRISKY:
1935       return 0;
1936     case PFREE_CANDIDATE:
1937       if (is_pfree (insn, bb_src, bb_trg))
1938         return 1;
1939       /* Don't 'break' here: PFREE-candidate is also PRISKY-candidate.  */
1940     case PRISKY_CANDIDATE:
1941       if (!flag_schedule_speculative_load_dangerous
1942           || is_prisky (insn, bb_src, bb_trg))
1943         return 0;
1944       break;
1945     default:;
1946     }
1947
1948   return flag_schedule_speculative_load_dangerous;
1949 }
1950 \f
1951 /* The number of insns from the current block scheduled so far.  */
1952 static int sched_target_n_insns;
1953 /* The number of insns from the current block to be scheduled in total.  */
1954 static int target_n_insns;
1955 /* The number of insns from the entire region scheduled so far.  */
1956 static int sched_n_insns;
1957 /* Nonzero if the last scheduled insn was a jump.  */
1958 static int last_was_jump;
1959
1960 /* Implementations of the sched_info functions for region scheduling.  */
1961 static void init_ready_list PARAMS ((struct ready_list *));
1962 static int can_schedule_ready_p PARAMS ((rtx));
1963 static int new_ready PARAMS ((rtx));
1964 static int schedule_more_p PARAMS ((void));
1965 static const char *rgn_print_insn PARAMS ((rtx, int));
1966 static int rgn_rank PARAMS ((rtx, rtx));
1967 static int contributes_to_priority PARAMS ((rtx, rtx));
1968 static void compute_jump_reg_dependencies PARAMS ((rtx, regset));
1969
1970 /* Return nonzero if there are more insns that should be scheduled.  */
1971
1972 static int
1973 schedule_more_p ()
1974 {
1975   return ! last_was_jump && sched_target_n_insns < target_n_insns;
1976 }
1977
1978 /* Add all insns that are initially ready to the ready list READY.  Called
1979    once before scheduling a set of insns.  */
1980
1981 static void
1982 init_ready_list (ready)
1983      struct ready_list *ready;
1984 {
1985   rtx prev_head = current_sched_info->prev_head;
1986   rtx next_tail = current_sched_info->next_tail;
1987   int bb_src;
1988   rtx insn;
1989
1990   target_n_insns = 0;
1991   sched_target_n_insns = 0;
1992   sched_n_insns = 0;
1993   last_was_jump = 0;
1994
1995   /* Print debugging information.  */
1996   if (sched_verbose >= 5)
1997     debug_dependencies ();
1998
1999   /* Prepare current target block info.  */
2000   if (current_nr_blocks > 1)
2001     {
2002       candidate_table = (candidate *) xmalloc (current_nr_blocks
2003                                                * sizeof (candidate));
2004
2005       bblst_last = 0;
2006       /* bblst_table holds split blocks and update blocks for each block after
2007          the current one in the region.  split blocks and update blocks are
2008          the TO blocks of region edges, so there can be at most rgn_nr_edges
2009          of them.  */
2010       bblst_size = (current_nr_blocks - target_bb) * rgn_nr_edges;
2011       bblst_table = (int *) xmalloc (bblst_size * sizeof (int));
2012
2013       bitlst_table_last = 0;
2014       bitlst_table_size = rgn_nr_edges;
2015       bitlst_table = (int *) xmalloc (rgn_nr_edges * sizeof (int));
2016
2017       compute_trg_info (target_bb);
2018     }
2019
2020   /* Initialize ready list with all 'ready' insns in target block.
2021      Count number of insns in the target block being scheduled.  */
2022   for (insn = NEXT_INSN (prev_head); insn != next_tail; insn = NEXT_INSN (insn))
2023     {
2024       rtx next;
2025
2026       if (! INSN_P (insn))
2027         continue;
2028       next = NEXT_INSN (insn);
2029
2030       if (INSN_DEP_COUNT (insn) == 0
2031           && (SCHED_GROUP_P (next) == 0 || ! INSN_P (next)))
2032         ready_add (ready, insn);
2033       if (!(SCHED_GROUP_P (insn)))
2034         target_n_insns++;
2035     }
2036
2037   /* Add to ready list all 'ready' insns in valid source blocks.
2038      For speculative insns, check-live, exception-free, and
2039      issue-delay.  */
2040   for (bb_src = target_bb + 1; bb_src < current_nr_blocks; bb_src++)
2041     if (IS_VALID (bb_src))
2042       {
2043         rtx src_head;
2044         rtx src_next_tail;
2045         rtx tail, head;
2046
2047         get_block_head_tail (BB_TO_BLOCK (bb_src), &head, &tail);
2048         src_next_tail = NEXT_INSN (tail);
2049         src_head = head;
2050
2051         for (insn = src_head; insn != src_next_tail; insn = NEXT_INSN (insn))
2052           {
2053             if (! INSN_P (insn))
2054               continue;
2055
2056             if (!CANT_MOVE (insn)
2057                 && (!IS_SPECULATIVE_INSN (insn)
2058                     || (insn_issue_delay (insn) <= 3
2059                         && check_live (insn, bb_src)
2060                         && is_exception_free (insn, bb_src, target_bb))))
2061               {
2062                 rtx next;
2063
2064                 /* Note that we haven't squirreled away the notes for
2065                    blocks other than the current.  So if this is a
2066                    speculative insn, NEXT might otherwise be a note.  */
2067                 next = next_nonnote_insn (insn);
2068                 if (INSN_DEP_COUNT (insn) == 0
2069                     && (! next
2070                         || SCHED_GROUP_P (next) == 0
2071                         || ! INSN_P (next)))
2072                   ready_add (ready, insn);
2073               }
2074           }
2075       }
2076 }
2077
2078 /* Called after taking INSN from the ready list.  Returns nonzero if this
2079    insn can be scheduled, nonzero if we should silently discard it.  */
2080
2081 static int
2082 can_schedule_ready_p (insn)
2083      rtx insn;
2084 {
2085   if (GET_CODE (insn) == JUMP_INSN)
2086     last_was_jump = 1;
2087
2088   /* An interblock motion?  */
2089   if (INSN_BB (insn) != target_bb)
2090     {
2091       rtx temp;
2092       basic_block b1;
2093
2094       if (IS_SPECULATIVE_INSN (insn))
2095         {
2096           if (!check_live (insn, INSN_BB (insn)))
2097             return 0;
2098           update_live (insn, INSN_BB (insn));
2099
2100           /* For speculative load, mark insns fed by it.  */
2101           if (IS_LOAD_INSN (insn) || FED_BY_SPEC_LOAD (insn))
2102             set_spec_fed (insn);
2103
2104           nr_spec++;
2105         }
2106       nr_inter++;
2107
2108       /* Find the beginning of the scheduling group.  */
2109       /* ??? Ought to update basic block here, but later bits of
2110          schedule_block assumes the original insn block is
2111          still intact.  */
2112
2113       temp = insn;
2114       while (SCHED_GROUP_P (temp))
2115         temp = PREV_INSN (temp);
2116
2117       /* Update source block boundaries.  */
2118       b1 = BLOCK_FOR_INSN (temp);
2119       if (temp == b1->head && insn == b1->end)
2120         {
2121           /* We moved all the insns in the basic block.
2122              Emit a note after the last insn and update the
2123              begin/end boundaries to point to the note.  */
2124           rtx note = emit_note_after (NOTE_INSN_DELETED, insn);
2125           b1->head = note;
2126           b1->end = note;
2127         }
2128       else if (insn == b1->end)
2129         {
2130           /* We took insns from the end of the basic block,
2131              so update the end of block boundary so that it
2132              points to the first insn we did not move.  */
2133           b1->end = PREV_INSN (temp);
2134         }
2135       else if (temp == b1->head)
2136         {
2137           /* We took insns from the start of the basic block,
2138              so update the start of block boundary so that
2139              it points to the first insn we did not move.  */
2140           b1->head = NEXT_INSN (insn);
2141         }
2142     }
2143   else
2144     {
2145       /* In block motion.  */
2146       sched_target_n_insns++;
2147     }
2148   sched_n_insns++;
2149
2150   return 1;
2151 }
2152
2153 /* Called after INSN has all its dependencies resolved.  Return nonzero
2154    if it should be moved to the ready list or the queue, or zero if we
2155    should silently discard it.  */
2156 static int
2157 new_ready (next)
2158      rtx next;
2159 {
2160   /* For speculative insns, before inserting to ready/queue,
2161      check live, exception-free, and issue-delay.  */
2162   if (INSN_BB (next) != target_bb
2163       && (!IS_VALID (INSN_BB (next))
2164           || CANT_MOVE (next)
2165           || (IS_SPECULATIVE_INSN (next)
2166               && (insn_issue_delay (next) > 3
2167                   || !check_live (next, INSN_BB (next))
2168                   || !is_exception_free (next, INSN_BB (next), target_bb)))))
2169     return 0;
2170   return 1;
2171 }
2172
2173 /* Return a string that contains the insn uid and optionally anything else
2174    necessary to identify this insn in an output.  It's valid to use a
2175    static buffer for this.  The ALIGNED parameter should cause the string
2176    to be formatted so that multiple output lines will line up nicely.  */
2177
2178 static const char *
2179 rgn_print_insn (insn, aligned)
2180      rtx insn;
2181      int aligned;
2182 {
2183   static char tmp[80];
2184
2185   if (aligned)
2186     sprintf (tmp, "b%3d: i%4d", INSN_BB (insn), INSN_UID (insn));
2187   else
2188     {
2189       if (current_nr_blocks > 1 && INSN_BB (insn) != target_bb)
2190         sprintf (tmp, "%d/b%d", INSN_UID (insn), INSN_BB (insn));
2191       else
2192         sprintf (tmp, "%d", INSN_UID (insn));
2193     }
2194   return tmp;
2195 }
2196
2197 /* Compare priority of two insns.  Return a positive number if the second
2198    insn is to be preferred for scheduling, and a negative one if the first
2199    is to be preferred.  Zero if they are equally good.  */
2200
2201 static int
2202 rgn_rank (insn1, insn2)
2203      rtx insn1, insn2;
2204 {
2205   /* Some comparison make sense in interblock scheduling only.  */
2206   if (INSN_BB (insn1) != INSN_BB (insn2))
2207     {
2208       int spec_val, prob_val;
2209
2210       /* Prefer an inblock motion on an interblock motion.  */
2211       if ((INSN_BB (insn2) == target_bb) && (INSN_BB (insn1) != target_bb))
2212         return 1;
2213       if ((INSN_BB (insn1) == target_bb) && (INSN_BB (insn2) != target_bb))
2214         return -1;
2215
2216       /* Prefer a useful motion on a speculative one.  */
2217       spec_val = IS_SPECULATIVE_INSN (insn1) - IS_SPECULATIVE_INSN (insn2);
2218       if (spec_val)
2219         return spec_val;
2220
2221       /* Prefer a more probable (speculative) insn.  */
2222       prob_val = INSN_PROBABILITY (insn2) - INSN_PROBABILITY (insn1);
2223       if (prob_val)
2224         return prob_val;
2225     }
2226   return 0;
2227 }
2228
2229 /* NEXT is an instruction that depends on INSN (a backward dependence);
2230    return nonzero if we should include this dependence in priority
2231    calculations.  */
2232
2233 static int
2234 contributes_to_priority (next, insn)
2235      rtx next, insn;
2236 {
2237   return BLOCK_NUM (next) == BLOCK_NUM (insn);
2238 }
2239
2240 /* INSN is a JUMP_INSN.  Store the set of registers that must be considered
2241    to be set by this jump in SET.  */
2242
2243 static void
2244 compute_jump_reg_dependencies (insn, set)
2245      rtx insn ATTRIBUTE_UNUSED;
2246      regset set ATTRIBUTE_UNUSED;
2247 {
2248   /* Nothing to do here, since we postprocess jumps in
2249      add_branch_dependences.  */
2250 }
2251
2252 /* Used in schedule_insns to initialize current_sched_info for scheduling
2253    regions (or single basic blocks).  */
2254
2255 static struct sched_info region_sched_info =
2256 {
2257   init_ready_list,
2258   can_schedule_ready_p,
2259   schedule_more_p,
2260   new_ready,
2261   rgn_rank,
2262   rgn_print_insn,
2263   contributes_to_priority,
2264   compute_jump_reg_dependencies,
2265
2266   NULL, NULL,
2267   NULL, NULL,
2268   0, 0
2269 };
2270
2271 /* Add dependences so that branches are scheduled to run last in their
2272    block.  */
2273
2274 static void
2275 add_branch_dependences (head, tail)
2276      rtx head, tail;
2277 {
2278   rtx insn, last;
2279
2280   /* For all branches, calls, uses, clobbers, cc0 setters, and instructions
2281      that can throw exceptions, force them to remain in order at the end of
2282      the block by adding dependencies and giving the last a high priority.
2283      There may be notes present, and prev_head may also be a note.
2284
2285      Branches must obviously remain at the end.  Calls should remain at the
2286      end since moving them results in worse register allocation.  Uses remain
2287      at the end to ensure proper register allocation.  cc0 setters remaim
2288      at the end because they can't be moved away from their cc0 user.  */
2289   insn = tail;
2290   last = 0;
2291   while (GET_CODE (insn) == CALL_INSN
2292          || GET_CODE (insn) == JUMP_INSN
2293          || (GET_CODE (insn) == INSN
2294              && (GET_CODE (PATTERN (insn)) == USE
2295                  || GET_CODE (PATTERN (insn)) == CLOBBER
2296                  || can_throw_internal (insn)
2297 #ifdef HAVE_cc0
2298                  || sets_cc0_p (PATTERN (insn))
2299 #endif
2300              ))
2301          || GET_CODE (insn) == NOTE)
2302     {
2303       if (GET_CODE (insn) != NOTE)
2304         {
2305           if (last != 0 && !find_insn_list (insn, LOG_LINKS (last)))
2306             {
2307               add_dependence (last, insn, REG_DEP_ANTI);
2308               INSN_REF_COUNT (insn)++;
2309             }
2310
2311           CANT_MOVE (insn) = 1;
2312
2313           last = insn;
2314           /* Skip over insns that are part of a group.
2315              Make each insn explicitly depend on the previous insn.
2316              This ensures that only the group header will ever enter
2317              the ready queue (and, when scheduled, will automatically
2318              schedule the SCHED_GROUP_P block).  */
2319           while (SCHED_GROUP_P (insn))
2320             {
2321               rtx temp = prev_nonnote_insn (insn);
2322               add_dependence (insn, temp, REG_DEP_ANTI);
2323               insn = temp;
2324             }
2325         }
2326
2327       /* Don't overrun the bounds of the basic block.  */
2328       if (insn == head)
2329         break;
2330
2331       insn = PREV_INSN (insn);
2332     }
2333
2334   /* Make sure these insns are scheduled last in their block.  */
2335   insn = last;
2336   if (insn != 0)
2337     while (insn != head)
2338       {
2339         insn = prev_nonnote_insn (insn);
2340
2341         if (INSN_REF_COUNT (insn) != 0)
2342           continue;
2343
2344         add_dependence (last, insn, REG_DEP_ANTI);
2345         INSN_REF_COUNT (insn) = 1;
2346
2347         /* Skip over insns that are part of a group.  */
2348         while (SCHED_GROUP_P (insn))
2349           insn = prev_nonnote_insn (insn);
2350       }
2351 }
2352
2353 /* Data structures for the computation of data dependences in a regions.  We
2354    keep one `deps' structure for every basic block.  Before analyzing the
2355    data dependences for a bb, its variables are initialized as a function of
2356    the variables of its predecessors.  When the analysis for a bb completes,
2357    we save the contents to the corresponding bb_deps[bb] variable.  */
2358
2359 static struct deps *bb_deps;
2360
2361 /* Duplicate the INSN_LIST elements of COPY and prepend them to OLD.  */
2362
2363 static rtx
2364 concat_INSN_LIST (copy, old)
2365      rtx copy, old;
2366 {
2367   rtx new = old;
2368   for (; copy ; copy = XEXP (copy, 1))
2369     new = alloc_INSN_LIST (XEXP (copy, 0), new);
2370   return new;
2371 }
2372
2373 static void
2374 concat_insn_mem_list (copy_insns, copy_mems, old_insns_p, old_mems_p)
2375      rtx copy_insns, copy_mems;
2376      rtx *old_insns_p, *old_mems_p;
2377 {
2378   rtx new_insns = *old_insns_p;
2379   rtx new_mems = *old_mems_p;
2380
2381   while (copy_insns)
2382     {
2383       new_insns = alloc_INSN_LIST (XEXP (copy_insns, 0), new_insns);
2384       new_mems = alloc_EXPR_LIST (VOIDmode, XEXP (copy_mems, 0), new_mems);
2385       copy_insns = XEXP (copy_insns, 1);
2386       copy_mems = XEXP (copy_mems, 1);
2387     }
2388
2389   *old_insns_p = new_insns;
2390   *old_mems_p = new_mems;
2391 }
2392
2393 /* After computing the dependencies for block BB, propagate the dependencies
2394    found in TMP_DEPS to the successors of the block.  */
2395 static void
2396 propagate_deps (bb, pred_deps)
2397      int bb;
2398      struct deps *pred_deps;
2399 {
2400   int b = BB_TO_BLOCK (bb);
2401   int e, first_edge;
2402
2403   /* bb's structures are inherited by its successors.  */
2404   first_edge = e = OUT_EDGES (b);
2405   if (e > 0)
2406     do
2407       {
2408         int b_succ = TO_BLOCK (e);
2409         int bb_succ = BLOCK_TO_BB (b_succ);
2410         struct deps *succ_deps = bb_deps + bb_succ;
2411         int reg;
2412
2413         /* Only bbs "below" bb, in the same region, are interesting.  */
2414         if (CONTAINING_RGN (b) != CONTAINING_RGN (b_succ)
2415             || bb_succ <= bb)
2416           {
2417             e = NEXT_OUT (e);
2418             continue;
2419           }
2420
2421         /* The reg_last lists are inherited by bb_succ.  */
2422         EXECUTE_IF_SET_IN_REG_SET (&pred_deps->reg_last_in_use, 0, reg,
2423           {
2424             struct deps_reg *pred_rl = &pred_deps->reg_last[reg];
2425             struct deps_reg *succ_rl = &succ_deps->reg_last[reg];
2426
2427             succ_rl->uses = concat_INSN_LIST (pred_rl->uses, succ_rl->uses);
2428             succ_rl->sets = concat_INSN_LIST (pred_rl->sets, succ_rl->sets);
2429             succ_rl->clobbers = concat_INSN_LIST (pred_rl->clobbers,
2430                                                   succ_rl->clobbers);
2431             succ_rl->uses_length += pred_rl->uses_length;
2432             succ_rl->clobbers_length += pred_rl->clobbers_length;
2433           });
2434         IOR_REG_SET (&succ_deps->reg_last_in_use, &pred_deps->reg_last_in_use);
2435
2436         /* Mem read/write lists are inherited by bb_succ.  */
2437         concat_insn_mem_list (pred_deps->pending_read_insns,
2438                               pred_deps->pending_read_mems,
2439                               &succ_deps->pending_read_insns,
2440                               &succ_deps->pending_read_mems);
2441         concat_insn_mem_list (pred_deps->pending_write_insns,
2442                               pred_deps->pending_write_mems,
2443                               &succ_deps->pending_write_insns,
2444                               &succ_deps->pending_write_mems);
2445
2446         succ_deps->last_pending_memory_flush
2447           = concat_INSN_LIST (pred_deps->last_pending_memory_flush,
2448                               succ_deps->last_pending_memory_flush);
2449         
2450         succ_deps->pending_lists_length += pred_deps->pending_lists_length;
2451         succ_deps->pending_flush_length += pred_deps->pending_flush_length;
2452
2453         /* last_function_call is inherited by bb_succ.  */
2454         succ_deps->last_function_call
2455           = concat_INSN_LIST (pred_deps->last_function_call,
2456                               succ_deps->last_function_call);
2457
2458         /* sched_before_next_call is inherited by bb_succ.  */
2459         succ_deps->sched_before_next_call
2460           = concat_INSN_LIST (pred_deps->sched_before_next_call,
2461                               succ_deps->sched_before_next_call);
2462
2463         e = NEXT_OUT (e);
2464       }
2465     while (e != first_edge);
2466
2467   /* These lists should point to the right place, for correct
2468      freeing later.  */
2469   bb_deps[bb].pending_read_insns = pred_deps->pending_read_insns;
2470   bb_deps[bb].pending_read_mems = pred_deps->pending_read_mems;
2471   bb_deps[bb].pending_write_insns = pred_deps->pending_write_insns;
2472   bb_deps[bb].pending_write_mems = pred_deps->pending_write_mems;
2473
2474   /* Can't allow these to be freed twice.  */
2475   pred_deps->pending_read_insns = 0;
2476   pred_deps->pending_read_mems = 0;
2477   pred_deps->pending_write_insns = 0;
2478   pred_deps->pending_write_mems = 0;
2479 }
2480
2481 /* Compute backward dependences inside bb.  In a multiple blocks region:
2482    (1) a bb is analyzed after its predecessors, and (2) the lists in
2483    effect at the end of bb (after analyzing for bb) are inherited by
2484    bb's successrs.
2485
2486    Specifically for reg-reg data dependences, the block insns are
2487    scanned by sched_analyze () top-to-bottom.  Two lists are
2488    maintained by sched_analyze (): reg_last[].sets for register DEFs,
2489    and reg_last[].uses for register USEs.
2490
2491    When analysis is completed for bb, we update for its successors:
2492    ;  - DEFS[succ] = Union (DEFS [succ], DEFS [bb])
2493    ;  - USES[succ] = Union (USES [succ], DEFS [bb])
2494
2495    The mechanism for computing mem-mem data dependence is very
2496    similar, and the result is interblock dependences in the region.  */
2497
2498 static void
2499 compute_block_backward_dependences (bb)
2500      int bb;
2501 {
2502   rtx head, tail;
2503   struct deps tmp_deps;
2504
2505   tmp_deps = bb_deps[bb];
2506
2507   /* Do the analysis for this block.  */
2508   get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
2509   sched_analyze (&tmp_deps, head, tail);
2510   add_branch_dependences (head, tail);
2511
2512   if (current_nr_blocks > 1)
2513     propagate_deps (bb, &tmp_deps);
2514
2515   /* Free up the INSN_LISTs.  */
2516   free_deps (&tmp_deps);
2517 }
2518
2519 /* Remove all INSN_LISTs and EXPR_LISTs from the pending lists and add
2520    them to the unused_*_list variables, so that they can be reused.  */
2521
2522 static void
2523 free_pending_lists ()
2524 {
2525   int bb;
2526
2527   for (bb = 0; bb < current_nr_blocks; bb++)
2528     {
2529       free_INSN_LIST_list (&bb_deps[bb].pending_read_insns);
2530       free_INSN_LIST_list (&bb_deps[bb].pending_write_insns);
2531       free_EXPR_LIST_list (&bb_deps[bb].pending_read_mems);
2532       free_EXPR_LIST_list (&bb_deps[bb].pending_write_mems);
2533     }
2534 }
2535 \f
2536 /* Print dependences for debugging, callable from debugger.  */
2537
2538 void
2539 debug_dependencies ()
2540 {
2541   int bb;
2542
2543   fprintf (sched_dump, ";;   --------------- forward dependences: ------------ \n");
2544   for (bb = 0; bb < current_nr_blocks; bb++)
2545     {
2546       if (1)
2547         {
2548           rtx head, tail;
2549           rtx next_tail;
2550           rtx insn;
2551
2552           get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
2553           next_tail = NEXT_INSN (tail);
2554           fprintf (sched_dump, "\n;;   --- Region Dependences --- b %d bb %d \n",
2555                    BB_TO_BLOCK (bb), bb);
2556
2557           fprintf (sched_dump, ";;   %7s%6s%6s%6s%6s%6s%11s%6s\n",
2558           "insn", "code", "bb", "dep", "prio", "cost", "blockage", "units");
2559           fprintf (sched_dump, ";;   %7s%6s%6s%6s%6s%6s%11s%6s\n",
2560           "----", "----", "--", "---", "----", "----", "--------", "-----");
2561           for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
2562             {
2563               rtx link;
2564               int unit, range;
2565
2566               if (! INSN_P (insn))
2567                 {
2568                   int n;
2569                   fprintf (sched_dump, ";;   %6d ", INSN_UID (insn));
2570                   if (GET_CODE (insn) == NOTE)
2571                     {
2572                       n = NOTE_LINE_NUMBER (insn);
2573                       if (n < 0)
2574                         fprintf (sched_dump, "%s\n", GET_NOTE_INSN_NAME (n));
2575                       else
2576                         fprintf (sched_dump, "line %d, file %s\n", n,
2577                                  NOTE_SOURCE_FILE (insn));
2578                     }
2579                   else
2580                     fprintf (sched_dump, " {%s}\n", GET_RTX_NAME (GET_CODE (insn)));
2581                   continue;
2582                 }
2583
2584               unit = insn_unit (insn);
2585               range = (unit < 0
2586                  || function_units[unit].blockage_range_function == 0) ? 0 :
2587                 function_units[unit].blockage_range_function (insn);
2588               fprintf (sched_dump,
2589                        ";;   %s%5d%6d%6d%6d%6d%6d  %3d -%3d   ",
2590                        (SCHED_GROUP_P (insn) ? "+" : " "),
2591                        INSN_UID (insn),
2592                        INSN_CODE (insn),
2593                        INSN_BB (insn),
2594                        INSN_DEP_COUNT (insn),
2595                        INSN_PRIORITY (insn),
2596                        insn_cost (insn, 0, 0),
2597                        (int) MIN_BLOCKAGE_COST (range),
2598                        (int) MAX_BLOCKAGE_COST (range));
2599               insn_print_units (insn);
2600               fprintf (sched_dump, "\t: ");
2601               for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
2602                 fprintf (sched_dump, "%d ", INSN_UID (XEXP (link, 0)));
2603               fprintf (sched_dump, "\n");
2604             }
2605         }
2606     }
2607   fprintf (sched_dump, "\n");
2608 }
2609 \f
2610 /* Schedule a region.  A region is either an inner loop, a loop-free
2611    subroutine, or a single basic block.  Each bb in the region is
2612    scheduled after its flow predecessors.  */
2613
2614 static void
2615 schedule_region (rgn)
2616      int rgn;
2617 {
2618   int bb;
2619   int rgn_n_insns = 0;
2620   int sched_rgn_n_insns = 0;
2621
2622   /* Set variables for the current region.  */
2623   current_nr_blocks = RGN_NR_BLOCKS (rgn);
2624   current_blocks = RGN_BLOCKS (rgn);
2625
2626   init_deps_global ();
2627
2628   /* Initializations for region data dependence analyisis.  */
2629   bb_deps = (struct deps *) xmalloc (sizeof (struct deps) * current_nr_blocks);
2630   for (bb = 0; bb < current_nr_blocks; bb++)
2631     init_deps (bb_deps + bb);
2632
2633   /* Compute LOG_LINKS.  */
2634   for (bb = 0; bb < current_nr_blocks; bb++)
2635     compute_block_backward_dependences (bb);
2636
2637   /* Compute INSN_DEPEND.  */
2638   for (bb = current_nr_blocks - 1; bb >= 0; bb--)
2639     {
2640       rtx head, tail;
2641       get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
2642
2643       compute_forward_dependences (head, tail);
2644     }
2645
2646   /* Set priorities.  */
2647   for (bb = 0; bb < current_nr_blocks; bb++)
2648     {
2649       rtx head, tail;
2650       get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
2651
2652       rgn_n_insns += set_priorities (head, tail);
2653     }
2654
2655   /* Compute interblock info: probabilities, split-edges, dominators, etc.  */
2656   if (current_nr_blocks > 1)
2657     {
2658       int i;
2659
2660       prob = (float *) xmalloc ((current_nr_blocks) * sizeof (float));
2661
2662       dom = sbitmap_vector_alloc (current_nr_blocks, current_nr_blocks);
2663       sbitmap_vector_zero (dom, current_nr_blocks);
2664       /* Edge to bit.  */
2665       rgn_nr_edges = 0;
2666       edge_to_bit = (int *) xmalloc (nr_edges * sizeof (int));
2667       for (i = 1; i < nr_edges; i++)
2668         if (CONTAINING_RGN (FROM_BLOCK (i)) == rgn)
2669           EDGE_TO_BIT (i) = rgn_nr_edges++;
2670       rgn_edges = (int *) xmalloc (rgn_nr_edges * sizeof (int));
2671
2672       rgn_nr_edges = 0;
2673       for (i = 1; i < nr_edges; i++)
2674         if (CONTAINING_RGN (FROM_BLOCK (i)) == (rgn))
2675           rgn_edges[rgn_nr_edges++] = i;
2676
2677       /* Split edges.  */
2678       pot_split = sbitmap_vector_alloc (current_nr_blocks, rgn_nr_edges);
2679       sbitmap_vector_zero (pot_split, current_nr_blocks);
2680       ancestor_edges = sbitmap_vector_alloc (current_nr_blocks, rgn_nr_edges);
2681       sbitmap_vector_zero (ancestor_edges, current_nr_blocks);
2682
2683       /* Compute probabilities, dominators, split_edges.  */
2684       for (bb = 0; bb < current_nr_blocks; bb++)
2685         compute_dom_prob_ps (bb);
2686     }
2687
2688   /* Now we can schedule all blocks.  */
2689   for (bb = 0; bb < current_nr_blocks; bb++)
2690     {
2691       rtx head, tail;
2692       int b = BB_TO_BLOCK (bb);
2693
2694       get_block_head_tail (b, &head, &tail);
2695
2696       if (no_real_insns_p (head, tail))
2697         continue;
2698
2699       current_sched_info->prev_head = PREV_INSN (head);
2700       current_sched_info->next_tail = NEXT_INSN (tail);
2701
2702       if (write_symbols != NO_DEBUG)
2703         {
2704           save_line_notes (b, head, tail);
2705           rm_line_notes (head, tail);
2706         }
2707
2708       /* rm_other_notes only removes notes which are _inside_ the
2709          block---that is, it won't remove notes before the first real insn
2710          or after the last real insn of the block.  So if the first insn
2711          has a REG_SAVE_NOTE which would otherwise be emitted before the
2712          insn, it is redundant with the note before the start of the
2713          block, and so we have to take it out.  */
2714       if (INSN_P (head))
2715         {
2716           rtx note;
2717
2718           for (note = REG_NOTES (head); note; note = XEXP (note, 1))
2719             if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
2720               {
2721                 remove_note (head, note);
2722                 note = XEXP (note, 1);
2723                 remove_note (head, note);
2724               }
2725         }
2726
2727       /* Remove remaining note insns from the block, save them in
2728          note_list.  These notes are restored at the end of
2729          schedule_block ().  */
2730       rm_other_notes (head, tail);
2731
2732       target_bb = bb;
2733
2734       current_sched_info->queue_must_finish_empty
2735         = current_nr_blocks > 1 && !flag_schedule_interblock;
2736
2737       schedule_block (b, rgn_n_insns);
2738       sched_rgn_n_insns += sched_n_insns;
2739
2740       /* Update target block boundaries.  */
2741       if (head == BLOCK_HEAD (b))
2742         BLOCK_HEAD (b) = current_sched_info->head;
2743       if (tail == BLOCK_END (b))
2744         BLOCK_END (b) = current_sched_info->tail;
2745
2746       /* Clean up.  */
2747       if (current_nr_blocks > 1)
2748         {
2749           free (candidate_table);
2750           free (bblst_table);
2751           free (bitlst_table);
2752         }
2753     }
2754
2755   /* Sanity check: verify that all region insns were scheduled.  */
2756   if (sched_rgn_n_insns != rgn_n_insns)
2757     abort ();
2758
2759   /* Restore line notes.  */
2760   if (write_symbols != NO_DEBUG)
2761     {
2762       for (bb = 0; bb < current_nr_blocks; bb++)
2763         {
2764           rtx head, tail;
2765           get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
2766           restore_line_notes (head, tail);
2767         }
2768     }
2769
2770   /* Done with this region.  */
2771   free_pending_lists ();
2772
2773   finish_deps_global ();
2774
2775   free (bb_deps);
2776
2777   if (current_nr_blocks > 1)
2778     {
2779       free (prob);
2780       sbitmap_vector_free (dom);
2781       sbitmap_vector_free (pot_split);
2782       sbitmap_vector_free (ancestor_edges);
2783       free (edge_to_bit);
2784       free (rgn_edges);
2785     }
2786 }
2787
2788 /* Indexed by region, holds the number of death notes found in that region.
2789    Used for consistency checks.  */
2790 static int *deaths_in_region;
2791
2792 /* Initialize data structures for region scheduling.  */
2793
2794 static void
2795 init_regions ()
2796 {
2797   sbitmap blocks;
2798   int rgn;
2799
2800   nr_regions = 0;
2801   rgn_table = (region *) xmalloc ((n_basic_blocks) * sizeof (region));
2802   rgn_bb_table = (int *) xmalloc ((n_basic_blocks) * sizeof (int));
2803   block_to_bb = (int *) xmalloc ((n_basic_blocks) * sizeof (int));
2804   containing_rgn = (int *) xmalloc ((n_basic_blocks) * sizeof (int));
2805
2806   /* Compute regions for scheduling.  */
2807   if (reload_completed
2808       || n_basic_blocks == 1
2809       || !flag_schedule_interblock)
2810     {
2811       find_single_block_region ();
2812     }
2813   else
2814     {
2815       /* Verify that a 'good' control flow graph can be built.  */
2816       if (is_cfg_nonregular ())
2817         {
2818           find_single_block_region ();
2819         }
2820       else
2821         {
2822           sbitmap *dom;
2823           struct edge_list *edge_list;
2824
2825           dom = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
2826
2827           /* The scheduler runs after flow; therefore, we can't blindly call
2828              back into find_basic_blocks since doing so could invalidate the
2829              info in global_live_at_start.
2830
2831              Consider a block consisting entirely of dead stores; after life
2832              analysis it would be a block of NOTE_INSN_DELETED notes.  If
2833              we call find_basic_blocks again, then the block would be removed
2834              entirely and invalidate our the register live information.
2835
2836              We could (should?) recompute register live information.  Doing
2837              so may even be beneficial.  */
2838           edge_list = create_edge_list ();
2839
2840           /* Compute the dominators and post dominators.  */
2841           calculate_dominance_info (NULL, dom, CDI_DOMINATORS);
2842
2843           /* build_control_flow will return nonzero if it detects unreachable
2844              blocks or any other irregularity with the cfg which prevents
2845              cross block scheduling.  */
2846           if (build_control_flow (edge_list) != 0)
2847             find_single_block_region ();
2848           else
2849             find_rgns (edge_list, dom);
2850
2851           if (sched_verbose >= 3)
2852             debug_regions ();
2853
2854           /* We are done with flow's edge list.  */
2855           free_edge_list (edge_list);
2856
2857           /* For now.  This will move as more and more of haifa is converted
2858              to using the cfg code in flow.c.  */
2859           free (dom);
2860         }
2861     }
2862
2863
2864   if (CHECK_DEAD_NOTES)
2865     {
2866       blocks = sbitmap_alloc (n_basic_blocks);
2867       deaths_in_region = (int *) xmalloc (sizeof (int) * nr_regions);
2868       /* Remove all death notes from the subroutine.  */
2869       for (rgn = 0; rgn < nr_regions; rgn++)
2870         {
2871           int b;
2872
2873           sbitmap_zero (blocks);
2874           for (b = RGN_NR_BLOCKS (rgn) - 1; b >= 0; --b)
2875             SET_BIT (blocks, rgn_bb_table[RGN_BLOCKS (rgn) + b]);
2876
2877           deaths_in_region[rgn] = count_or_remove_death_notes (blocks, 1);
2878         }
2879       sbitmap_free (blocks);
2880     }
2881   else
2882     count_or_remove_death_notes (NULL, 1);
2883 }
2884
2885 /* The one entry point in this file.  DUMP_FILE is the dump file for
2886    this pass.  */
2887
2888 void
2889 schedule_insns (dump_file)
2890      FILE *dump_file;
2891 {
2892   sbitmap large_region_blocks, blocks;
2893   int rgn;
2894   int any_large_regions;
2895
2896   /* Taking care of this degenerate case makes the rest of
2897      this code simpler.  */
2898   if (n_basic_blocks == 0)
2899     return;
2900
2901   scope_to_insns_initialize ();
2902
2903   nr_inter = 0;
2904   nr_spec = 0;
2905
2906   sched_init (dump_file);
2907
2908   init_regions ();
2909
2910   current_sched_info = &region_sched_info;
2911   
2912   /* Schedule every region in the subroutine.  */
2913   for (rgn = 0; rgn < nr_regions; rgn++)
2914     schedule_region (rgn);
2915
2916   /* Update life analysis for the subroutine.  Do single block regions
2917      first so that we can verify that live_at_start didn't change.  Then
2918      do all other blocks.  */
2919   /* ??? There is an outside possibility that update_life_info, or more
2920      to the point propagate_block, could get called with non-zero flags
2921      more than once for one basic block.  This would be kinda bad if it
2922      were to happen, since REG_INFO would be accumulated twice for the
2923      block, and we'd have twice the REG_DEAD notes.
2924
2925      I'm fairly certain that this _shouldn't_ happen, since I don't think
2926      that live_at_start should change at region heads.  Not sure what the
2927      best way to test for this kind of thing...  */
2928
2929   allocate_reg_life_data ();
2930   compute_bb_for_insn (get_max_uid ());
2931
2932   any_large_regions = 0;
2933   large_region_blocks = sbitmap_alloc (n_basic_blocks);
2934   sbitmap_ones (large_region_blocks);
2935
2936   blocks = sbitmap_alloc (n_basic_blocks);
2937   sbitmap_zero (blocks);
2938
2939   /* Update life information.  For regions consisting of multiple blocks
2940      we've possibly done interblock scheduling that affects global liveness.
2941      For regions consisting of single blocks we need to do only local
2942      liveness.  */
2943   for (rgn = 0; rgn < nr_regions; rgn++)
2944     if (RGN_NR_BLOCKS (rgn) > 1)
2945       any_large_regions = 1;
2946     else
2947       {
2948         SET_BIT (blocks, rgn_bb_table[RGN_BLOCKS (rgn)]);
2949         RESET_BIT (large_region_blocks, rgn_bb_table[RGN_BLOCKS (rgn)]);
2950       }
2951
2952   /* Don't update reg info after reload, since that affects
2953      regs_ever_live, which should not change after reload.  */
2954   update_life_info (blocks, UPDATE_LIFE_LOCAL,
2955                     (reload_completed ? PROP_DEATH_NOTES
2956                      : PROP_DEATH_NOTES | PROP_REG_INFO));
2957   if (any_large_regions)
2958     {
2959       update_life_info (large_region_blocks, UPDATE_LIFE_GLOBAL,
2960                         PROP_DEATH_NOTES | PROP_REG_INFO);
2961     }
2962
2963   if (CHECK_DEAD_NOTES)
2964     {
2965       /* Verify the counts of basic block notes in single the basic block
2966          regions.  */
2967       for (rgn = 0; rgn < nr_regions; rgn++)
2968         if (RGN_NR_BLOCKS (rgn) == 1)
2969           {
2970             sbitmap_zero (blocks);
2971             SET_BIT (blocks, rgn_bb_table[RGN_BLOCKS (rgn)]);
2972
2973             if (deaths_in_region[rgn]
2974                 != count_or_remove_death_notes (blocks, 0))
2975               abort ();
2976           }
2977       free (deaths_in_region);
2978     }
2979
2980   /* Reposition the prologue and epilogue notes in case we moved the
2981      prologue/epilogue insns.  */
2982   if (reload_completed)
2983     reposition_prologue_and_epilogue_notes (get_insns ());
2984
2985   /* Delete redundant line notes.  */
2986   if (write_symbols != NO_DEBUG)
2987     rm_redundant_line_notes ();
2988
2989   scope_to_insns_finalize ();
2990
2991   if (sched_verbose)
2992     {
2993       if (reload_completed == 0 && flag_schedule_interblock)
2994         {
2995           fprintf (sched_dump,
2996                    "\n;; Procedure interblock/speculative motions == %d/%d \n",
2997                    nr_inter, nr_spec);
2998         }
2999       else
3000         {
3001           if (nr_inter > 0)
3002             abort ();
3003         }
3004       fprintf (sched_dump, "\n\n");
3005     }
3006
3007   /* Clean up.  */
3008   free (rgn_table);
3009   free (rgn_bb_table);
3010   free (block_to_bb);
3011   free (containing_rgn);
3012
3013   sched_finish ();
3014
3015   if (edge_table)
3016     {
3017       free (edge_table);
3018       edge_table = NULL;
3019     }
3020
3021   if (in_edges)
3022     {
3023       free (in_edges);
3024       in_edges = NULL;
3025     }
3026   if (out_edges)
3027     {
3028       free (out_edges);
3029       out_edges = NULL;
3030     }
3031
3032   sbitmap_free (blocks);
3033   sbitmap_free (large_region_blocks);
3034 }
3035 #endif