OSDN Git Service

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