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