2004-10-07 J"orn Rennecke <joern.rennecke@st.com>
[official-gcc.git] / gcc / sched-rgn.c
blob50b48c24a0f2930428b4e67651cb456ac469f598
1 /* Instruction scheduling pass.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
5 and currently maintained by, Jim Wilson (wilson@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
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.
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.
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.
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. */
48 #include "config.h"
49 #include "system.h"
50 #include "coretypes.h"
51 #include "tm.h"
52 #include "toplev.h"
53 #include "rtl.h"
54 #include "tm_p.h"
55 #include "hard-reg-set.h"
56 #include "basic-block.h"
57 #include "regs.h"
58 #include "function.h"
59 #include "flags.h"
60 #include "insn-config.h"
61 #include "insn-attr.h"
62 #include "except.h"
63 #include "toplev.h"
64 #include "recog.h"
65 #include "cfglayout.h"
66 #include "params.h"
67 #include "sched-int.h"
68 #include "target.h"
70 /* Define when we want to do count REG_DEAD notes before and after scheduling
71 for sanity checking. We can't do that when conditional execution is used,
72 as REG_DEAD exist only for unconditional deaths. */
74 #if !defined (HAVE_conditional_execution) && defined (ENABLE_CHECKING)
75 #define CHECK_DEAD_NOTES 1
76 #else
77 #define CHECK_DEAD_NOTES 0
78 #endif
81 #ifdef INSN_SCHEDULING
82 /* Some accessor macros for h_i_d members only used within this file. */
83 #define INSN_REF_COUNT(INSN) (h_i_d[INSN_UID (INSN)].ref_count)
84 #define FED_BY_SPEC_LOAD(insn) (h_i_d[INSN_UID (insn)].fed_by_spec_load)
85 #define IS_LOAD_INSN(insn) (h_i_d[INSN_UID (insn)].is_load_insn)
87 /* nr_inter/spec counts interblock/speculative motion for the function. */
88 static int nr_inter, nr_spec;
90 static int is_cfg_nonregular (void);
91 static bool sched_is_disabled_for_current_region_p (void);
93 /* A region is the main entity for interblock scheduling: insns
94 are allowed to move between blocks in the same region, along
95 control flow graph edges, in the 'up' direction. */
96 typedef struct
98 int rgn_nr_blocks; /* Number of blocks in region. */
99 int rgn_blocks; /* cblocks in the region (actually index in rgn_bb_table). */
101 region;
103 /* Number of regions in the procedure. */
104 static int nr_regions;
106 /* Table of region descriptions. */
107 static region *rgn_table;
109 /* Array of lists of regions' blocks. */
110 static int *rgn_bb_table;
112 /* Topological order of blocks in the region (if b2 is reachable from
113 b1, block_to_bb[b2] > block_to_bb[b1]). Note: A basic block is
114 always referred to by either block or b, while its topological
115 order name (in the region) is referred to by bb. */
116 static int *block_to_bb;
118 /* The number of the region containing a block. */
119 static int *containing_rgn;
121 #define RGN_NR_BLOCKS(rgn) (rgn_table[rgn].rgn_nr_blocks)
122 #define RGN_BLOCKS(rgn) (rgn_table[rgn].rgn_blocks)
123 #define BLOCK_TO_BB(block) (block_to_bb[block])
124 #define CONTAINING_RGN(block) (containing_rgn[block])
126 void debug_regions (void);
127 static void find_single_block_region (void);
128 static void find_rgns (void);
129 static bool too_large (int, int *, int *);
131 extern void debug_live (int, int);
133 /* Blocks of the current region being scheduled. */
134 static int current_nr_blocks;
135 static int current_blocks;
137 /* The mapping from bb to block. */
138 #define BB_TO_BLOCK(bb) (rgn_bb_table[current_blocks + (bb)])
140 /* Target info declarations.
142 The block currently being scheduled is referred to as the "target" block,
143 while other blocks in the region from which insns can be moved to the
144 target are called "source" blocks. The candidate structure holds info
145 about such sources: are they valid? Speculative? Etc. */
146 typedef struct
148 basic_block *first_member;
149 int nr_members;
151 bblst;
153 typedef struct
155 char is_valid;
156 char is_speculative;
157 int src_prob;
158 bblst split_bbs;
159 bblst update_bbs;
161 candidate;
163 static candidate *candidate_table;
165 /* A speculative motion requires checking live information on the path
166 from 'source' to 'target'. The split blocks are those to be checked.
167 After a speculative motion, live information should be modified in
168 the 'update' blocks.
170 Lists of split and update blocks for each candidate of the current
171 target are in array bblst_table. */
172 static basic_block *bblst_table;
173 static int bblst_size, bblst_last;
175 #define IS_VALID(src) ( candidate_table[src].is_valid )
176 #define IS_SPECULATIVE(src) ( candidate_table[src].is_speculative )
177 #define SRC_PROB(src) ( candidate_table[src].src_prob )
179 /* The bb being currently scheduled. */
180 static int target_bb;
182 /* List of edges. */
183 typedef struct
185 edge *first_member;
186 int nr_members;
188 edgelst;
190 static edge *edgelst_table;
191 static int edgelst_last;
193 static void extract_edgelst (sbitmap, edgelst *);
196 /* Target info functions. */
197 static void split_edges (int, int, edgelst *);
198 static void compute_trg_info (int);
199 void debug_candidate (int);
200 void debug_candidates (int);
202 /* Dominators array: dom[i] contains the sbitmap of dominators of
203 bb i in the region. */
204 static sbitmap *dom;
206 /* bb 0 is the only region entry. */
207 #define IS_RGN_ENTRY(bb) (!bb)
209 /* Is bb_src dominated by bb_trg. */
210 #define IS_DOMINATED(bb_src, bb_trg) \
211 ( TEST_BIT (dom[bb_src], bb_trg) )
213 /* Probability: Prob[i] is a float in [0, 1] which is the probability
214 of bb i relative to the region entry. */
215 static float *prob;
217 /* The probability of bb_src, relative to bb_trg. Note, that while the
218 'prob[bb]' is a float in [0, 1], this macro returns an integer
219 in [0, 100]. */
220 #define GET_SRC_PROB(bb_src, bb_trg) ((int) (100.0 * (prob[bb_src] / \
221 prob[bb_trg])))
223 /* Bit-set of edges, where bit i stands for edge i. */
224 typedef sbitmap edgeset;
226 /* Number of edges in the region. */
227 static int rgn_nr_edges;
229 /* Array of size rgn_nr_edges. */
230 static edge *rgn_edges;
232 /* Mapping from each edge in the graph to its number in the rgn. */
233 #define EDGE_TO_BIT(edge) ((int)(size_t)(edge)->aux)
234 #define SET_EDGE_TO_BIT(edge,nr) ((edge)->aux = (void *)(size_t)(nr))
236 /* The split edges of a source bb is different for each target
237 bb. In order to compute this efficiently, the 'potential-split edges'
238 are computed for each bb prior to scheduling a region. This is actually
239 the split edges of each bb relative to the region entry.
241 pot_split[bb] is the set of potential split edges of bb. */
242 static edgeset *pot_split;
244 /* For every bb, a set of its ancestor edges. */
245 static edgeset *ancestor_edges;
247 static void compute_dom_prob_ps (int);
249 #define INSN_PROBABILITY(INSN) (SRC_PROB (BLOCK_TO_BB (BLOCK_NUM (INSN))))
250 #define IS_SPECULATIVE_INSN(INSN) (IS_SPECULATIVE (BLOCK_TO_BB (BLOCK_NUM (INSN))))
251 #define INSN_BB(INSN) (BLOCK_TO_BB (BLOCK_NUM (INSN)))
253 /* Parameters affecting the decision of rank_for_schedule().
254 ??? Nope. But MIN_PROBABILITY is used in compute_trg_info. */
255 #define MIN_PROBABILITY 40
257 /* Speculative scheduling functions. */
258 static int check_live_1 (int, rtx);
259 static void update_live_1 (int, rtx);
260 static int check_live (rtx, int);
261 static void update_live (rtx, int);
262 static void set_spec_fed (rtx);
263 static int is_pfree (rtx, int, int);
264 static int find_conditional_protection (rtx, int);
265 static int is_conditionally_protected (rtx, int, int);
266 static int is_prisky (rtx, int, int);
267 static int is_exception_free (rtx, int, int);
269 static bool sets_likely_spilled (rtx);
270 static void sets_likely_spilled_1 (rtx, rtx, void *);
271 static void add_branch_dependences (rtx, rtx);
272 static void compute_block_backward_dependences (int);
273 void debug_dependencies (void);
275 static void init_regions (void);
276 static void schedule_region (int);
277 static rtx concat_INSN_LIST (rtx, rtx);
278 static void concat_insn_mem_list (rtx, rtx, rtx *, rtx *);
279 static void propagate_deps (int, struct deps *);
280 static void free_pending_lists (void);
282 /* Functions for construction of the control flow graph. */
284 /* Return 1 if control flow graph should not be constructed, 0 otherwise.
286 We decide not to build the control flow graph if there is possibly more
287 than one entry to the function, if computed branches exist, if we
288 have nonlocal gotos, or if we have an unreachable loop. */
290 static int
291 is_cfg_nonregular (void)
293 basic_block b;
294 rtx insn;
295 RTX_CODE code;
297 /* If we have a label that could be the target of a nonlocal goto, then
298 the cfg is not well structured. */
299 if (nonlocal_goto_handler_labels)
300 return 1;
302 /* If we have any forced labels, then the cfg is not well structured. */
303 if (forced_labels)
304 return 1;
306 /* If this function has a computed jump, then we consider the cfg
307 not well structured. */
308 if (current_function_has_computed_jump)
309 return 1;
311 /* If we have exception handlers, then we consider the cfg not well
312 structured. ?!? We should be able to handle this now that flow.c
313 computes an accurate cfg for EH. */
314 if (current_function_has_exception_handlers ())
315 return 1;
317 /* If we have non-jumping insns which refer to labels, then we consider
318 the cfg not well structured. */
319 /* Check for labels referred to other thn by jumps. */
320 FOR_EACH_BB (b)
321 for (insn = BB_HEAD (b); ; insn = NEXT_INSN (insn))
323 code = GET_CODE (insn);
324 if (INSN_P (insn) && code != JUMP_INSN)
326 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
328 if (note
329 && ! (JUMP_P (NEXT_INSN (insn))
330 && find_reg_note (NEXT_INSN (insn), REG_LABEL,
331 XEXP (note, 0))))
332 return 1;
335 if (insn == BB_END (b))
336 break;
339 /* Unreachable loops with more than one basic block are detected
340 during the DFS traversal in find_rgns.
342 Unreachable loops with a single block are detected here. This
343 test is redundant with the one in find_rgns, but it's much
344 cheaper to go ahead and catch the trivial case here. */
345 FOR_EACH_BB (b)
347 if (EDGE_COUNT (b->preds) == 0
348 || (EDGE_PRED (b, 0)->src == b
349 && EDGE_COUNT (b->preds) == 1))
350 return 1;
353 /* All the tests passed. Consider the cfg well structured. */
354 return 0;
357 /* Extract list of edges from a bitmap containing EDGE_TO_BIT bits. */
359 static void
360 extract_edgelst (sbitmap set, edgelst *el)
362 int i;
364 /* edgelst table space is reused in each call to extract_edgelst. */
365 edgelst_last = 0;
367 el->first_member = &edgelst_table[edgelst_last];
368 el->nr_members = 0;
370 /* Iterate over each word in the bitset. */
371 EXECUTE_IF_SET_IN_SBITMAP (set, 0, i,
373 edgelst_table[edgelst_last++] = rgn_edges[i];
374 el->nr_members++;
378 /* Functions for the construction of regions. */
380 /* Print the regions, for debugging purposes. Callable from debugger. */
382 void
383 debug_regions (void)
385 int rgn, bb;
387 fprintf (sched_dump, "\n;; ------------ REGIONS ----------\n\n");
388 for (rgn = 0; rgn < nr_regions; rgn++)
390 fprintf (sched_dump, ";;\trgn %d nr_blocks %d:\n", rgn,
391 rgn_table[rgn].rgn_nr_blocks);
392 fprintf (sched_dump, ";;\tbb/block: ");
394 for (bb = 0; bb < rgn_table[rgn].rgn_nr_blocks; bb++)
396 current_blocks = RGN_BLOCKS (rgn);
398 gcc_assert (bb == BLOCK_TO_BB (BB_TO_BLOCK (bb)));
399 fprintf (sched_dump, " %d/%d ", bb, BB_TO_BLOCK (bb));
402 fprintf (sched_dump, "\n\n");
406 /* Build a single block region for each basic block in the function.
407 This allows for using the same code for interblock and basic block
408 scheduling. */
410 static void
411 find_single_block_region (void)
413 basic_block bb;
415 nr_regions = 0;
417 FOR_EACH_BB (bb)
419 rgn_bb_table[nr_regions] = bb->index;
420 RGN_NR_BLOCKS (nr_regions) = 1;
421 RGN_BLOCKS (nr_regions) = nr_regions;
422 CONTAINING_RGN (bb->index) = nr_regions;
423 BLOCK_TO_BB (bb->index) = 0;
424 nr_regions++;
428 /* Update number of blocks and the estimate for number of insns
429 in the region. Return true if the region is "too large" for interblock
430 scheduling (compile time considerations). */
432 static bool
433 too_large (int block, int *num_bbs, int *num_insns)
435 (*num_bbs)++;
436 (*num_insns) += (INSN_LUID (BB_END (BASIC_BLOCK (block)))
437 - INSN_LUID (BB_HEAD (BASIC_BLOCK (block))));
439 return ((*num_bbs > PARAM_VALUE (PARAM_MAX_SCHED_REGION_BLOCKS))
440 || (*num_insns > PARAM_VALUE (PARAM_MAX_SCHED_REGION_INSNS)));
443 /* Update_loop_relations(blk, hdr): Check if the loop headed by max_hdr[blk]
444 is still an inner loop. Put in max_hdr[blk] the header of the most inner
445 loop containing blk. */
446 #define UPDATE_LOOP_RELATIONS(blk, hdr) \
448 if (max_hdr[blk] == -1) \
449 max_hdr[blk] = hdr; \
450 else if (dfs_nr[max_hdr[blk]] > dfs_nr[hdr]) \
451 RESET_BIT (inner, hdr); \
452 else if (dfs_nr[max_hdr[blk]] < dfs_nr[hdr]) \
454 RESET_BIT (inner,max_hdr[blk]); \
455 max_hdr[blk] = hdr; \
459 /* Find regions for interblock scheduling.
461 A region for scheduling can be:
463 * A loop-free procedure, or
465 * A reducible inner loop, or
467 * A basic block not contained in any other region.
469 ?!? In theory we could build other regions based on extended basic
470 blocks or reverse extended basic blocks. Is it worth the trouble?
472 Loop blocks that form a region are put into the region's block list
473 in topological order.
475 This procedure stores its results into the following global (ick) variables
477 * rgn_nr
478 * rgn_table
479 * rgn_bb_table
480 * block_to_bb
481 * containing region
483 We use dominator relationships to avoid making regions out of non-reducible
484 loops.
486 This procedure needs to be converted to work on pred/succ lists instead
487 of edge tables. That would simplify it somewhat. */
489 static void
490 find_rgns (void)
492 int *max_hdr, *dfs_nr, *degree;
493 char no_loops = 1;
494 int node, child, loop_head, i, head, tail;
495 int count = 0, sp, idx = 0;
496 edge_iterator current_edge;
497 edge_iterator *stack;
498 int num_bbs, num_insns, unreachable;
499 int too_large_failure;
500 basic_block bb;
502 /* Note if a block is a natural loop header. */
503 sbitmap header;
505 /* Note if a block is a natural inner loop header. */
506 sbitmap inner;
508 /* Note if a block is in the block queue. */
509 sbitmap in_queue;
511 /* Note if a block is in the block queue. */
512 sbitmap in_stack;
514 /* Perform a DFS traversal of the cfg. Identify loop headers, inner loops
515 and a mapping from block to its loop header (if the block is contained
516 in a loop, else -1).
518 Store results in HEADER, INNER, and MAX_HDR respectively, these will
519 be used as inputs to the second traversal.
521 STACK, SP and DFS_NR are only used during the first traversal. */
523 /* Allocate and initialize variables for the first traversal. */
524 max_hdr = xmalloc (last_basic_block * sizeof (int));
525 dfs_nr = xcalloc (last_basic_block, sizeof (int));
526 stack = xmalloc (n_edges * sizeof (edge_iterator));
528 inner = sbitmap_alloc (last_basic_block);
529 sbitmap_ones (inner);
531 header = sbitmap_alloc (last_basic_block);
532 sbitmap_zero (header);
534 in_queue = sbitmap_alloc (last_basic_block);
535 sbitmap_zero (in_queue);
537 in_stack = sbitmap_alloc (last_basic_block);
538 sbitmap_zero (in_stack);
540 for (i = 0; i < last_basic_block; i++)
541 max_hdr[i] = -1;
543 #define EDGE_PASSED(E) (ei_end_p ((E)) || ei_edge ((E))->aux)
544 #define SET_EDGE_PASSED(E) (ei_edge ((E))->aux = ei_edge ((E)))
546 /* DFS traversal to find inner loops in the cfg. */
548 current_edge = ei_start (EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest->succs);
549 sp = -1;
551 while (1)
553 if (EDGE_PASSED (current_edge))
555 /* We have reached a leaf node or a node that was already
556 processed. Pop edges off the stack until we find
557 an edge that has not yet been processed. */
558 while (sp >= 0 && EDGE_PASSED (current_edge))
560 /* Pop entry off the stack. */
561 current_edge = stack[sp--];
562 node = ei_edge (current_edge)->src->index;
563 gcc_assert (node != ENTRY_BLOCK);
564 child = ei_edge (current_edge)->dest->index;
565 gcc_assert (child != EXIT_BLOCK);
566 RESET_BIT (in_stack, child);
567 if (max_hdr[child] >= 0 && TEST_BIT (in_stack, max_hdr[child]))
568 UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
569 ei_next (&current_edge);
572 /* See if have finished the DFS tree traversal. */
573 if (sp < 0 && EDGE_PASSED (current_edge))
574 break;
576 /* Nope, continue the traversal with the popped node. */
577 continue;
580 /* Process a node. */
581 node = ei_edge (current_edge)->src->index;
582 gcc_assert (node != ENTRY_BLOCK);
583 SET_BIT (in_stack, node);
584 dfs_nr[node] = ++count;
586 /* We don't traverse to the exit block. */
587 child = ei_edge (current_edge)->dest->index;
588 if (child == EXIT_BLOCK)
590 SET_EDGE_PASSED (current_edge);
591 ei_next (&current_edge);
592 continue;
595 /* If the successor is in the stack, then we've found a loop.
596 Mark the loop, if it is not a natural loop, then it will
597 be rejected during the second traversal. */
598 if (TEST_BIT (in_stack, child))
600 no_loops = 0;
601 SET_BIT (header, child);
602 UPDATE_LOOP_RELATIONS (node, child);
603 SET_EDGE_PASSED (current_edge);
604 ei_next (&current_edge);
605 continue;
608 /* If the child was already visited, then there is no need to visit
609 it again. Just update the loop relationships and restart
610 with a new edge. */
611 if (dfs_nr[child])
613 if (max_hdr[child] >= 0 && TEST_BIT (in_stack, max_hdr[child]))
614 UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
615 SET_EDGE_PASSED (current_edge);
616 ei_next (&current_edge);
617 continue;
620 /* Push an entry on the stack and continue DFS traversal. */
621 stack[++sp] = current_edge;
622 SET_EDGE_PASSED (current_edge);
623 current_edge = ei_start (ei_edge (current_edge)->dest->succs);
626 /* Reset ->aux field used by EDGE_PASSED. */
627 FOR_ALL_BB (bb)
629 edge_iterator ei;
630 edge e;
631 FOR_EACH_EDGE (e, ei, bb->succs)
632 e->aux = NULL;
636 /* Another check for unreachable blocks. The earlier test in
637 is_cfg_nonregular only finds unreachable blocks that do not
638 form a loop.
640 The DFS traversal will mark every block that is reachable from
641 the entry node by placing a nonzero value in dfs_nr. Thus if
642 dfs_nr is zero for any block, then it must be unreachable. */
643 unreachable = 0;
644 FOR_EACH_BB (bb)
645 if (dfs_nr[bb->index] == 0)
647 unreachable = 1;
648 break;
651 /* Gross. To avoid wasting memory, the second pass uses the dfs_nr array
652 to hold degree counts. */
653 degree = dfs_nr;
655 FOR_EACH_BB (bb)
656 degree[bb->index] = EDGE_COUNT (bb->preds);
658 /* Do not perform region scheduling if there are any unreachable
659 blocks. */
660 if (!unreachable)
662 int *queue;
664 if (no_loops)
665 SET_BIT (header, 0);
667 /* Second traversal:find reducible inner loops and topologically sort
668 block of each region. */
670 queue = xmalloc (n_basic_blocks * sizeof (int));
672 /* Find blocks which are inner loop headers. We still have non-reducible
673 loops to consider at this point. */
674 FOR_EACH_BB (bb)
676 if (TEST_BIT (header, bb->index) && TEST_BIT (inner, bb->index))
678 edge e;
679 edge_iterator ei;
680 basic_block jbb;
682 /* Now check that the loop is reducible. We do this separate
683 from finding inner loops so that we do not find a reducible
684 loop which contains an inner non-reducible loop.
686 A simple way to find reducible/natural loops is to verify
687 that each block in the loop is dominated by the loop
688 header.
690 If there exists a block that is not dominated by the loop
691 header, then the block is reachable from outside the loop
692 and thus the loop is not a natural loop. */
693 FOR_EACH_BB (jbb)
695 /* First identify blocks in the loop, except for the loop
696 entry block. */
697 if (bb->index == max_hdr[jbb->index] && bb != jbb)
699 /* Now verify that the block is dominated by the loop
700 header. */
701 if (!dominated_by_p (CDI_DOMINATORS, jbb, bb))
702 break;
706 /* If we exited the loop early, then I is the header of
707 a non-reducible loop and we should quit processing it
708 now. */
709 if (jbb != EXIT_BLOCK_PTR)
710 continue;
712 /* I is a header of an inner loop, or block 0 in a subroutine
713 with no loops at all. */
714 head = tail = -1;
715 too_large_failure = 0;
716 loop_head = max_hdr[bb->index];
718 /* Decrease degree of all I's successors for topological
719 ordering. */
720 FOR_EACH_EDGE (e, ei, bb->succs)
721 if (e->dest != EXIT_BLOCK_PTR)
722 --degree[e->dest->index];
724 /* Estimate # insns, and count # blocks in the region. */
725 num_bbs = 1;
726 num_insns = (INSN_LUID (BB_END (bb))
727 - INSN_LUID (BB_HEAD (bb)));
729 /* Find all loop latches (blocks with back edges to the loop
730 header) or all the leaf blocks in the cfg has no loops.
732 Place those blocks into the queue. */
733 if (no_loops)
735 FOR_EACH_BB (jbb)
736 /* Leaf nodes have only a single successor which must
737 be EXIT_BLOCK. */
738 if (EDGE_COUNT (jbb->succs) == 1
739 && EDGE_SUCC (jbb, 0)->dest == EXIT_BLOCK_PTR)
741 queue[++tail] = jbb->index;
742 SET_BIT (in_queue, jbb->index);
744 if (too_large (jbb->index, &num_bbs, &num_insns))
746 too_large_failure = 1;
747 break;
751 else
753 edge e;
755 FOR_EACH_EDGE (e, ei, bb->preds)
757 if (e->src == ENTRY_BLOCK_PTR)
758 continue;
760 node = e->src->index;
762 if (max_hdr[node] == loop_head && node != bb->index)
764 /* This is a loop latch. */
765 queue[++tail] = node;
766 SET_BIT (in_queue, node);
768 if (too_large (node, &num_bbs, &num_insns))
770 too_large_failure = 1;
771 break;
777 /* Now add all the blocks in the loop to the queue.
779 We know the loop is a natural loop; however the algorithm
780 above will not always mark certain blocks as being in the
781 loop. Consider:
782 node children
783 a b,c
785 c a,d
788 The algorithm in the DFS traversal may not mark B & D as part
789 of the loop (i.e. they will not have max_hdr set to A).
791 We know they can not be loop latches (else they would have
792 had max_hdr set since they'd have a backedge to a dominator
793 block). So we don't need them on the initial queue.
795 We know they are part of the loop because they are dominated
796 by the loop header and can be reached by a backwards walk of
797 the edges starting with nodes on the initial queue.
799 It is safe and desirable to include those nodes in the
800 loop/scheduling region. To do so we would need to decrease
801 the degree of a node if it is the target of a backedge
802 within the loop itself as the node is placed in the queue.
804 We do not do this because I'm not sure that the actual
805 scheduling code will properly handle this case. ?!? */
807 while (head < tail && !too_large_failure)
809 edge e;
810 child = queue[++head];
812 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (child)->preds)
814 node = e->src->index;
816 /* See discussion above about nodes not marked as in
817 this loop during the initial DFS traversal. */
818 if (e->src == ENTRY_BLOCK_PTR
819 || max_hdr[node] != loop_head)
821 tail = -1;
822 break;
824 else if (!TEST_BIT (in_queue, node) && node != bb->index)
826 queue[++tail] = node;
827 SET_BIT (in_queue, node);
829 if (too_large (node, &num_bbs, &num_insns))
831 too_large_failure = 1;
832 break;
838 if (tail >= 0 && !too_large_failure)
840 /* Place the loop header into list of region blocks. */
841 degree[bb->index] = -1;
842 rgn_bb_table[idx] = bb->index;
843 RGN_NR_BLOCKS (nr_regions) = num_bbs;
844 RGN_BLOCKS (nr_regions) = idx++;
845 CONTAINING_RGN (bb->index) = nr_regions;
846 BLOCK_TO_BB (bb->index) = count = 0;
848 /* Remove blocks from queue[] when their in degree
849 becomes zero. Repeat until no blocks are left on the
850 list. This produces a topological list of blocks in
851 the region. */
852 while (tail >= 0)
854 if (head < 0)
855 head = tail;
856 child = queue[head];
857 if (degree[child] == 0)
859 edge e;
861 degree[child] = -1;
862 rgn_bb_table[idx++] = child;
863 BLOCK_TO_BB (child) = ++count;
864 CONTAINING_RGN (child) = nr_regions;
865 queue[head] = queue[tail--];
867 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (child)->succs)
868 if (e->dest != EXIT_BLOCK_PTR)
869 --degree[e->dest->index];
871 else
872 --head;
874 ++nr_regions;
878 free (queue);
881 /* Any block that did not end up in a region is placed into a region
882 by itself. */
883 FOR_EACH_BB (bb)
884 if (degree[bb->index] >= 0)
886 rgn_bb_table[idx] = bb->index;
887 RGN_NR_BLOCKS (nr_regions) = 1;
888 RGN_BLOCKS (nr_regions) = idx++;
889 CONTAINING_RGN (bb->index) = nr_regions++;
890 BLOCK_TO_BB (bb->index) = 0;
893 free (max_hdr);
894 free (dfs_nr);
895 free (stack);
896 sbitmap_free (header);
897 sbitmap_free (inner);
898 sbitmap_free (in_queue);
899 sbitmap_free (in_stack);
902 /* Functions for regions scheduling information. */
904 /* Compute dominators, probability, and potential-split-edges of bb.
905 Assume that these values were already computed for bb's predecessors. */
907 static void
908 compute_dom_prob_ps (int bb)
910 int pred_bb;
911 int nr_out_edges, nr_rgn_out_edges;
912 edge_iterator in_ei, out_ei;
913 edge in_edge, out_edge;
915 prob[bb] = 0.0;
916 if (IS_RGN_ENTRY (bb))
918 SET_BIT (dom[bb], 0);
919 prob[bb] = 1.0;
920 return;
923 /* Initialize dom[bb] to '111..1'. */
924 sbitmap_ones (dom[bb]);
926 FOR_EACH_EDGE (in_edge, in_ei, BASIC_BLOCK (BB_TO_BLOCK (bb))->preds)
928 if (in_edge->src == ENTRY_BLOCK_PTR)
929 continue;
931 pred_bb = BLOCK_TO_BB (in_edge->src->index);
932 sbitmap_a_and_b (dom[bb], dom[bb], dom[pred_bb]);
933 sbitmap_a_or_b (ancestor_edges[bb],
934 ancestor_edges[bb], ancestor_edges[pred_bb]);
936 SET_BIT (ancestor_edges[bb], EDGE_TO_BIT (in_edge));
938 sbitmap_a_or_b (pot_split[bb], pot_split[bb], pot_split[pred_bb]);
940 nr_out_edges = 0;
941 nr_rgn_out_edges = 0;
943 FOR_EACH_EDGE (out_edge, out_ei, in_edge->src->succs)
945 ++nr_out_edges;
947 /* The successor doesn't belong in the region? */
948 if (out_edge->dest != EXIT_BLOCK_PTR
949 && CONTAINING_RGN (out_edge->dest->index)
950 != CONTAINING_RGN (BB_TO_BLOCK (bb)))
951 ++nr_rgn_out_edges;
953 SET_BIT (pot_split[bb], EDGE_TO_BIT (out_edge));
956 /* Now nr_rgn_out_edges is the number of region-exit edges from
957 pred, and nr_out_edges will be the number of pred out edges
958 not leaving the region. */
959 nr_out_edges -= nr_rgn_out_edges;
960 if (nr_rgn_out_edges > 0)
961 prob[bb] += 0.9 * prob[pred_bb] / nr_out_edges;
962 else
963 prob[bb] += prob[pred_bb] / nr_out_edges;
966 SET_BIT (dom[bb], bb);
967 sbitmap_difference (pot_split[bb], pot_split[bb], ancestor_edges[bb]);
969 if (sched_verbose >= 2)
970 fprintf (sched_dump, ";; bb_prob(%d, %d) = %3d\n", bb, BB_TO_BLOCK (bb),
971 (int) (100.0 * prob[bb]));
974 /* Functions for target info. */
976 /* Compute in BL the list of split-edges of bb_src relatively to bb_trg.
977 Note that bb_trg dominates bb_src. */
979 static void
980 split_edges (int bb_src, int bb_trg, edgelst *bl)
982 sbitmap src = sbitmap_alloc (pot_split[bb_src]->n_bits);
983 sbitmap_copy (src, pot_split[bb_src]);
985 sbitmap_difference (src, src, pot_split[bb_trg]);
986 extract_edgelst (src, bl);
987 sbitmap_free (src);
990 /* Find the valid candidate-source-blocks for the target block TRG, compute
991 their probability, and check if they are speculative or not.
992 For speculative sources, compute their update-blocks and split-blocks. */
994 static void
995 compute_trg_info (int trg)
997 candidate *sp;
998 edgelst el;
999 int i, j, k, update_idx;
1000 basic_block block;
1001 edge_iterator ei;
1002 edge e;
1004 /* Define some of the fields for the target bb as well. */
1005 sp = candidate_table + trg;
1006 sp->is_valid = 1;
1007 sp->is_speculative = 0;
1008 sp->src_prob = 100;
1010 for (i = trg + 1; i < current_nr_blocks; i++)
1012 sp = candidate_table + i;
1014 sp->is_valid = IS_DOMINATED (i, trg);
1015 if (sp->is_valid)
1017 sp->src_prob = GET_SRC_PROB (i, trg);
1018 sp->is_valid = (sp->src_prob >= MIN_PROBABILITY);
1021 if (sp->is_valid)
1023 split_edges (i, trg, &el);
1024 sp->is_speculative = (el.nr_members) ? 1 : 0;
1025 if (sp->is_speculative && !flag_schedule_speculative)
1026 sp->is_valid = 0;
1029 if (sp->is_valid)
1031 /* Compute split blocks and store them in bblst_table.
1032 The TO block of every split edge is a split block. */
1033 sp->split_bbs.first_member = &bblst_table[bblst_last];
1034 sp->split_bbs.nr_members = el.nr_members;
1035 for (j = 0; j < el.nr_members; bblst_last++, j++)
1036 bblst_table[bblst_last] = el.first_member[j]->dest;
1037 sp->update_bbs.first_member = &bblst_table[bblst_last];
1039 /* Compute update blocks and store them in bblst_table.
1040 For every split edge, look at the FROM block, and check
1041 all out edges. For each out edge that is not a split edge,
1042 add the TO block to the update block list. This list can end
1043 up with a lot of duplicates. We need to weed them out to avoid
1044 overrunning the end of the bblst_table. */
1046 update_idx = 0;
1047 for (j = 0; j < el.nr_members; j++)
1049 block = el.first_member[j]->src;
1050 FOR_EACH_EDGE (e, ei, block->succs)
1052 if (!(e->dest->flags & BB_VISITED))
1054 for (k = 0; k < el.nr_members; k++)
1055 if (e == el.first_member[k])
1056 break;
1058 if (k >= el.nr_members)
1060 bblst_table[bblst_last++] = e->dest;
1061 e->dest->flags |= BB_VISITED;
1062 update_idx++;
1067 sp->update_bbs.nr_members = update_idx;
1069 FOR_ALL_BB (block)
1070 block->flags &= ~BB_VISITED;
1072 /* Make sure we didn't overrun the end of bblst_table. */
1073 gcc_assert (bblst_last <= bblst_size);
1075 else
1077 sp->split_bbs.nr_members = sp->update_bbs.nr_members = 0;
1079 sp->is_speculative = 0;
1080 sp->src_prob = 0;
1085 /* Print candidates info, for debugging purposes. Callable from debugger. */
1087 void
1088 debug_candidate (int i)
1090 if (!candidate_table[i].is_valid)
1091 return;
1093 if (candidate_table[i].is_speculative)
1095 int j;
1096 fprintf (sched_dump, "src b %d bb %d speculative \n", BB_TO_BLOCK (i), i);
1098 fprintf (sched_dump, "split path: ");
1099 for (j = 0; j < candidate_table[i].split_bbs.nr_members; j++)
1101 int b = candidate_table[i].split_bbs.first_member[j]->index;
1103 fprintf (sched_dump, " %d ", b);
1105 fprintf (sched_dump, "\n");
1107 fprintf (sched_dump, "update path: ");
1108 for (j = 0; j < candidate_table[i].update_bbs.nr_members; j++)
1110 int b = candidate_table[i].update_bbs.first_member[j]->index;
1112 fprintf (sched_dump, " %d ", b);
1114 fprintf (sched_dump, "\n");
1116 else
1118 fprintf (sched_dump, " src %d equivalent\n", BB_TO_BLOCK (i));
1122 /* Print candidates info, for debugging purposes. Callable from debugger. */
1124 void
1125 debug_candidates (int trg)
1127 int i;
1129 fprintf (sched_dump, "----------- candidate table: target: b=%d bb=%d ---\n",
1130 BB_TO_BLOCK (trg), trg);
1131 for (i = trg + 1; i < current_nr_blocks; i++)
1132 debug_candidate (i);
1135 /* Functions for speculative scheduling. */
1137 /* Return 0 if x is a set of a register alive in the beginning of one
1138 of the split-blocks of src, otherwise return 1. */
1140 static int
1141 check_live_1 (int src, rtx x)
1143 int i;
1144 int regno;
1145 rtx reg = SET_DEST (x);
1147 if (reg == 0)
1148 return 1;
1150 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
1151 || GET_CODE (reg) == SIGN_EXTRACT
1152 || GET_CODE (reg) == STRICT_LOW_PART)
1153 reg = XEXP (reg, 0);
1155 if (GET_CODE (reg) == PARALLEL)
1157 int i;
1159 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
1160 if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
1161 if (check_live_1 (src, XEXP (XVECEXP (reg, 0, i), 0)))
1162 return 1;
1164 return 0;
1167 if (!REG_P (reg))
1168 return 1;
1170 regno = REGNO (reg);
1172 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
1174 /* Global registers are assumed live. */
1175 return 0;
1177 else
1179 if (regno < FIRST_PSEUDO_REGISTER)
1181 /* Check for hard registers. */
1182 int j = hard_regno_nregs[regno][GET_MODE (reg)];
1183 while (--j >= 0)
1185 for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
1187 basic_block b = candidate_table[src].split_bbs.first_member[i];
1189 if (REGNO_REG_SET_P (b->global_live_at_start, regno + j))
1191 return 0;
1196 else
1198 /* Check for pseudo registers. */
1199 for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
1201 basic_block b = candidate_table[src].split_bbs.first_member[i];
1203 if (REGNO_REG_SET_P (b->global_live_at_start, regno))
1205 return 0;
1211 return 1;
1214 /* If x is a set of a register R, mark that R is alive in the beginning
1215 of every update-block of src. */
1217 static void
1218 update_live_1 (int src, rtx x)
1220 int i;
1221 int regno;
1222 rtx reg = SET_DEST (x);
1224 if (reg == 0)
1225 return;
1227 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
1228 || GET_CODE (reg) == SIGN_EXTRACT
1229 || GET_CODE (reg) == STRICT_LOW_PART)
1230 reg = XEXP (reg, 0);
1232 if (GET_CODE (reg) == PARALLEL)
1234 int i;
1236 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
1237 if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
1238 update_live_1 (src, XEXP (XVECEXP (reg, 0, i), 0));
1240 return;
1243 if (!REG_P (reg))
1244 return;
1246 /* Global registers are always live, so the code below does not apply
1247 to them. */
1249 regno = REGNO (reg);
1251 if (regno >= FIRST_PSEUDO_REGISTER || !global_regs[regno])
1253 if (regno < FIRST_PSEUDO_REGISTER)
1255 int j = hard_regno_nregs[regno][GET_MODE (reg)];
1256 while (--j >= 0)
1258 for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
1260 basic_block b = candidate_table[src].update_bbs.first_member[i];
1262 SET_REGNO_REG_SET (b->global_live_at_start, regno + j);
1266 else
1268 for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
1270 basic_block b = candidate_table[src].update_bbs.first_member[i];
1272 SET_REGNO_REG_SET (b->global_live_at_start, regno);
1278 /* Return 1 if insn can be speculatively moved from block src to trg,
1279 otherwise return 0. Called before first insertion of insn to
1280 ready-list or before the scheduling. */
1282 static int
1283 check_live (rtx insn, int src)
1285 /* Find the registers set by instruction. */
1286 if (GET_CODE (PATTERN (insn)) == SET
1287 || GET_CODE (PATTERN (insn)) == CLOBBER)
1288 return check_live_1 (src, PATTERN (insn));
1289 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1291 int j;
1292 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
1293 if ((GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
1294 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
1295 && !check_live_1 (src, XVECEXP (PATTERN (insn), 0, j)))
1296 return 0;
1298 return 1;
1301 return 1;
1304 /* Update the live registers info after insn was moved speculatively from
1305 block src to trg. */
1307 static void
1308 update_live (rtx insn, int src)
1310 /* Find the registers set by instruction. */
1311 if (GET_CODE (PATTERN (insn)) == SET
1312 || GET_CODE (PATTERN (insn)) == CLOBBER)
1313 update_live_1 (src, PATTERN (insn));
1314 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1316 int j;
1317 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
1318 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
1319 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
1320 update_live_1 (src, XVECEXP (PATTERN (insn), 0, j));
1324 /* Nonzero if block bb_to is equal to, or reachable from block bb_from. */
1325 #define IS_REACHABLE(bb_from, bb_to) \
1326 (bb_from == bb_to \
1327 || IS_RGN_ENTRY (bb_from) \
1328 || (TEST_BIT (ancestor_edges[bb_to], \
1329 EDGE_TO_BIT (EDGE_PRED (BASIC_BLOCK (BB_TO_BLOCK (bb_from)), 0)))))
1331 /* Turns on the fed_by_spec_load flag for insns fed by load_insn. */
1333 static void
1334 set_spec_fed (rtx load_insn)
1336 rtx link;
1338 for (link = INSN_DEPEND (load_insn); link; link = XEXP (link, 1))
1339 if (GET_MODE (link) == VOIDmode)
1340 FED_BY_SPEC_LOAD (XEXP (link, 0)) = 1;
1341 } /* set_spec_fed */
1343 /* On the path from the insn to load_insn_bb, find a conditional
1344 branch depending on insn, that guards the speculative load. */
1346 static int
1347 find_conditional_protection (rtx insn, int load_insn_bb)
1349 rtx link;
1351 /* Iterate through DEF-USE forward dependences. */
1352 for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
1354 rtx next = XEXP (link, 0);
1355 if ((CONTAINING_RGN (BLOCK_NUM (next)) ==
1356 CONTAINING_RGN (BB_TO_BLOCK (load_insn_bb)))
1357 && IS_REACHABLE (INSN_BB (next), load_insn_bb)
1358 && load_insn_bb != INSN_BB (next)
1359 && GET_MODE (link) == VOIDmode
1360 && (JUMP_P (next)
1361 || find_conditional_protection (next, load_insn_bb)))
1362 return 1;
1364 return 0;
1365 } /* find_conditional_protection */
1367 /* Returns 1 if the same insn1 that participates in the computation
1368 of load_insn's address is feeding a conditional branch that is
1369 guarding on load_insn. This is true if we find a the two DEF-USE
1370 chains:
1371 insn1 -> ... -> conditional-branch
1372 insn1 -> ... -> load_insn,
1373 and if a flow path exist:
1374 insn1 -> ... -> conditional-branch -> ... -> load_insn,
1375 and if insn1 is on the path
1376 region-entry -> ... -> bb_trg -> ... load_insn.
1378 Locate insn1 by climbing on LOG_LINKS from load_insn.
1379 Locate the branch by following INSN_DEPEND from insn1. */
1381 static int
1382 is_conditionally_protected (rtx load_insn, int bb_src, int bb_trg)
1384 rtx link;
1386 for (link = LOG_LINKS (load_insn); link; link = XEXP (link, 1))
1388 rtx insn1 = XEXP (link, 0);
1390 /* Must be a DEF-USE dependence upon non-branch. */
1391 if (GET_MODE (link) != VOIDmode
1392 || JUMP_P (insn1))
1393 continue;
1395 /* Must exist a path: region-entry -> ... -> bb_trg -> ... load_insn. */
1396 if (INSN_BB (insn1) == bb_src
1397 || (CONTAINING_RGN (BLOCK_NUM (insn1))
1398 != CONTAINING_RGN (BB_TO_BLOCK (bb_src)))
1399 || (!IS_REACHABLE (bb_trg, INSN_BB (insn1))
1400 && !IS_REACHABLE (INSN_BB (insn1), bb_trg)))
1401 continue;
1403 /* Now search for the conditional-branch. */
1404 if (find_conditional_protection (insn1, bb_src))
1405 return 1;
1407 /* Recursive step: search another insn1, "above" current insn1. */
1408 return is_conditionally_protected (insn1, bb_src, bb_trg);
1411 /* The chain does not exist. */
1412 return 0;
1413 } /* is_conditionally_protected */
1415 /* Returns 1 if a clue for "similar load" 'insn2' is found, and hence
1416 load_insn can move speculatively from bb_src to bb_trg. All the
1417 following must hold:
1419 (1) both loads have 1 base register (PFREE_CANDIDATEs).
1420 (2) load_insn and load1 have a def-use dependence upon
1421 the same insn 'insn1'.
1422 (3) either load2 is in bb_trg, or:
1423 - there's only one split-block, and
1424 - load1 is on the escape path, and
1426 From all these we can conclude that the two loads access memory
1427 addresses that differ at most by a constant, and hence if moving
1428 load_insn would cause an exception, it would have been caused by
1429 load2 anyhow. */
1431 static int
1432 is_pfree (rtx load_insn, int bb_src, int bb_trg)
1434 rtx back_link;
1435 candidate *candp = candidate_table + bb_src;
1437 if (candp->split_bbs.nr_members != 1)
1438 /* Must have exactly one escape block. */
1439 return 0;
1441 for (back_link = LOG_LINKS (load_insn);
1442 back_link; back_link = XEXP (back_link, 1))
1444 rtx insn1 = XEXP (back_link, 0);
1446 if (GET_MODE (back_link) == VOIDmode)
1448 /* Found a DEF-USE dependence (insn1, load_insn). */
1449 rtx fore_link;
1451 for (fore_link = INSN_DEPEND (insn1);
1452 fore_link; fore_link = XEXP (fore_link, 1))
1454 rtx insn2 = XEXP (fore_link, 0);
1455 if (GET_MODE (fore_link) == VOIDmode)
1457 /* Found a DEF-USE dependence (insn1, insn2). */
1458 if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
1459 /* insn2 not guaranteed to be a 1 base reg load. */
1460 continue;
1462 if (INSN_BB (insn2) == bb_trg)
1463 /* insn2 is the similar load, in the target block. */
1464 return 1;
1466 if (*(candp->split_bbs.first_member) == BLOCK_FOR_INSN (insn2))
1467 /* insn2 is a similar load, in a split-block. */
1468 return 1;
1474 /* Couldn't find a similar load. */
1475 return 0;
1476 } /* is_pfree */
1478 /* Return 1 if load_insn is prisky (i.e. if load_insn is fed by
1479 a load moved speculatively, or if load_insn is protected by
1480 a compare on load_insn's address). */
1482 static int
1483 is_prisky (rtx load_insn, int bb_src, int bb_trg)
1485 if (FED_BY_SPEC_LOAD (load_insn))
1486 return 1;
1488 if (LOG_LINKS (load_insn) == NULL)
1489 /* Dependence may 'hide' out of the region. */
1490 return 1;
1492 if (is_conditionally_protected (load_insn, bb_src, bb_trg))
1493 return 1;
1495 return 0;
1498 /* Insn is a candidate to be moved speculatively from bb_src to bb_trg.
1499 Return 1 if insn is exception-free (and the motion is valid)
1500 and 0 otherwise. */
1502 static int
1503 is_exception_free (rtx insn, int bb_src, int bb_trg)
1505 int insn_class = haifa_classify_insn (insn);
1507 /* Handle non-load insns. */
1508 switch (insn_class)
1510 case TRAP_FREE:
1511 return 1;
1512 case TRAP_RISKY:
1513 return 0;
1514 default:;
1517 /* Handle loads. */
1518 if (!flag_schedule_speculative_load)
1519 return 0;
1520 IS_LOAD_INSN (insn) = 1;
1521 switch (insn_class)
1523 case IFREE:
1524 return (1);
1525 case IRISKY:
1526 return 0;
1527 case PFREE_CANDIDATE:
1528 if (is_pfree (insn, bb_src, bb_trg))
1529 return 1;
1530 /* Don't 'break' here: PFREE-candidate is also PRISKY-candidate. */
1531 case PRISKY_CANDIDATE:
1532 if (!flag_schedule_speculative_load_dangerous
1533 || is_prisky (insn, bb_src, bb_trg))
1534 return 0;
1535 break;
1536 default:;
1539 return flag_schedule_speculative_load_dangerous;
1542 /* The number of insns from the current block scheduled so far. */
1543 static int sched_target_n_insns;
1544 /* The number of insns from the current block to be scheduled in total. */
1545 static int target_n_insns;
1546 /* The number of insns from the entire region scheduled so far. */
1547 static int sched_n_insns;
1548 /* Nonzero if the last scheduled insn was a jump. */
1549 static int last_was_jump;
1551 /* Implementations of the sched_info functions for region scheduling. */
1552 static void init_ready_list (struct ready_list *);
1553 static int can_schedule_ready_p (rtx);
1554 static int new_ready (rtx);
1555 static int schedule_more_p (void);
1556 static const char *rgn_print_insn (rtx, int);
1557 static int rgn_rank (rtx, rtx);
1558 static int contributes_to_priority (rtx, rtx);
1559 static void compute_jump_reg_dependencies (rtx, regset, regset, regset);
1561 /* Return nonzero if there are more insns that should be scheduled. */
1563 static int
1564 schedule_more_p (void)
1566 return ! last_was_jump && sched_target_n_insns < target_n_insns;
1569 /* Add all insns that are initially ready to the ready list READY. Called
1570 once before scheduling a set of insns. */
1572 static void
1573 init_ready_list (struct ready_list *ready)
1575 rtx prev_head = current_sched_info->prev_head;
1576 rtx next_tail = current_sched_info->next_tail;
1577 int bb_src;
1578 rtx insn;
1580 target_n_insns = 0;
1581 sched_target_n_insns = 0;
1582 sched_n_insns = 0;
1583 last_was_jump = 0;
1585 /* Print debugging information. */
1586 if (sched_verbose >= 5)
1587 debug_dependencies ();
1589 /* Prepare current target block info. */
1590 if (current_nr_blocks > 1)
1592 candidate_table = xmalloc (current_nr_blocks * sizeof (candidate));
1594 bblst_last = 0;
1595 /* bblst_table holds split blocks and update blocks for each block after
1596 the current one in the region. split blocks and update blocks are
1597 the TO blocks of region edges, so there can be at most rgn_nr_edges
1598 of them. */
1599 bblst_size = (current_nr_blocks - target_bb) * rgn_nr_edges;
1600 bblst_table = xmalloc (bblst_size * sizeof (basic_block));
1602 edgelst_last = 0;
1603 edgelst_table = xmalloc (rgn_nr_edges * sizeof (edge));
1605 compute_trg_info (target_bb);
1608 /* Initialize ready list with all 'ready' insns in target block.
1609 Count number of insns in the target block being scheduled. */
1610 for (insn = NEXT_INSN (prev_head); insn != next_tail; insn = NEXT_INSN (insn))
1612 if (INSN_DEP_COUNT (insn) == 0)
1614 ready_add (ready, insn);
1616 if (targetm.sched.adjust_priority)
1617 INSN_PRIORITY (insn) =
1618 targetm.sched.adjust_priority (insn, INSN_PRIORITY (insn));
1620 target_n_insns++;
1623 /* Add to ready list all 'ready' insns in valid source blocks.
1624 For speculative insns, check-live, exception-free, and
1625 issue-delay. */
1626 for (bb_src = target_bb + 1; bb_src < current_nr_blocks; bb_src++)
1627 if (IS_VALID (bb_src))
1629 rtx src_head;
1630 rtx src_next_tail;
1631 rtx tail, head;
1633 get_block_head_tail (BB_TO_BLOCK (bb_src), &head, &tail);
1634 src_next_tail = NEXT_INSN (tail);
1635 src_head = head;
1637 for (insn = src_head; insn != src_next_tail; insn = NEXT_INSN (insn))
1639 if (! INSN_P (insn))
1640 continue;
1642 if (!CANT_MOVE (insn)
1643 && (!IS_SPECULATIVE_INSN (insn)
1644 || ((recog_memoized (insn) < 0
1645 || min_insn_conflict_delay (curr_state,
1646 insn, insn) <= 3)
1647 && check_live (insn, bb_src)
1648 && is_exception_free (insn, bb_src, target_bb))))
1649 if (INSN_DEP_COUNT (insn) == 0)
1651 ready_add (ready, insn);
1653 if (targetm.sched.adjust_priority)
1654 INSN_PRIORITY (insn) =
1655 targetm.sched.adjust_priority (insn, INSN_PRIORITY (insn));
1661 /* Called after taking INSN from the ready list. Returns nonzero if this
1662 insn can be scheduled, nonzero if we should silently discard it. */
1664 static int
1665 can_schedule_ready_p (rtx insn)
1667 if (JUMP_P (insn))
1668 last_was_jump = 1;
1670 /* An interblock motion? */
1671 if (INSN_BB (insn) != target_bb)
1673 basic_block b1;
1675 if (IS_SPECULATIVE_INSN (insn))
1677 if (!check_live (insn, INSN_BB (insn)))
1678 return 0;
1679 update_live (insn, INSN_BB (insn));
1681 /* For speculative load, mark insns fed by it. */
1682 if (IS_LOAD_INSN (insn) || FED_BY_SPEC_LOAD (insn))
1683 set_spec_fed (insn);
1685 nr_spec++;
1687 nr_inter++;
1689 /* Update source block boundaries. */
1690 b1 = BLOCK_FOR_INSN (insn);
1691 if (insn == BB_HEAD (b1) && insn == BB_END (b1))
1693 /* We moved all the insns in the basic block.
1694 Emit a note after the last insn and update the
1695 begin/end boundaries to point to the note. */
1696 rtx note = emit_note_after (NOTE_INSN_DELETED, insn);
1697 BB_HEAD (b1) = note;
1698 BB_END (b1) = note;
1700 else if (insn == BB_END (b1))
1702 /* We took insns from the end of the basic block,
1703 so update the end of block boundary so that it
1704 points to the first insn we did not move. */
1705 BB_END (b1) = PREV_INSN (insn);
1707 else if (insn == BB_HEAD (b1))
1709 /* We took insns from the start of the basic block,
1710 so update the start of block boundary so that
1711 it points to the first insn we did not move. */
1712 BB_HEAD (b1) = NEXT_INSN (insn);
1715 else
1717 /* In block motion. */
1718 sched_target_n_insns++;
1720 sched_n_insns++;
1722 return 1;
1725 /* Called after INSN has all its dependencies resolved. Return nonzero
1726 if it should be moved to the ready list or the queue, or zero if we
1727 should silently discard it. */
1728 static int
1729 new_ready (rtx next)
1731 /* For speculative insns, before inserting to ready/queue,
1732 check live, exception-free, and issue-delay. */
1733 if (INSN_BB (next) != target_bb
1734 && (!IS_VALID (INSN_BB (next))
1735 || CANT_MOVE (next)
1736 || (IS_SPECULATIVE_INSN (next)
1737 && ((recog_memoized (next) >= 0
1738 && min_insn_conflict_delay (curr_state, next, next) > 3)
1739 || !check_live (next, INSN_BB (next))
1740 || !is_exception_free (next, INSN_BB (next), target_bb)))))
1741 return 0;
1742 return 1;
1745 /* Return a string that contains the insn uid and optionally anything else
1746 necessary to identify this insn in an output. It's valid to use a
1747 static buffer for this. The ALIGNED parameter should cause the string
1748 to be formatted so that multiple output lines will line up nicely. */
1750 static const char *
1751 rgn_print_insn (rtx insn, int aligned)
1753 static char tmp[80];
1755 if (aligned)
1756 sprintf (tmp, "b%3d: i%4d", INSN_BB (insn), INSN_UID (insn));
1757 else
1759 if (current_nr_blocks > 1 && INSN_BB (insn) != target_bb)
1760 sprintf (tmp, "%d/b%d", INSN_UID (insn), INSN_BB (insn));
1761 else
1762 sprintf (tmp, "%d", INSN_UID (insn));
1764 return tmp;
1767 /* Compare priority of two insns. Return a positive number if the second
1768 insn is to be preferred for scheduling, and a negative one if the first
1769 is to be preferred. Zero if they are equally good. */
1771 static int
1772 rgn_rank (rtx insn1, rtx insn2)
1774 /* Some comparison make sense in interblock scheduling only. */
1775 if (INSN_BB (insn1) != INSN_BB (insn2))
1777 int spec_val, prob_val;
1779 /* Prefer an inblock motion on an interblock motion. */
1780 if ((INSN_BB (insn2) == target_bb) && (INSN_BB (insn1) != target_bb))
1781 return 1;
1782 if ((INSN_BB (insn1) == target_bb) && (INSN_BB (insn2) != target_bb))
1783 return -1;
1785 /* Prefer a useful motion on a speculative one. */
1786 spec_val = IS_SPECULATIVE_INSN (insn1) - IS_SPECULATIVE_INSN (insn2);
1787 if (spec_val)
1788 return spec_val;
1790 /* Prefer a more probable (speculative) insn. */
1791 prob_val = INSN_PROBABILITY (insn2) - INSN_PROBABILITY (insn1);
1792 if (prob_val)
1793 return prob_val;
1795 return 0;
1798 /* NEXT is an instruction that depends on INSN (a backward dependence);
1799 return nonzero if we should include this dependence in priority
1800 calculations. */
1802 static int
1803 contributes_to_priority (rtx next, rtx insn)
1805 return BLOCK_NUM (next) == BLOCK_NUM (insn);
1808 /* INSN is a JUMP_INSN, COND_SET is the set of registers that are
1809 conditionally set before INSN. Store the set of registers that
1810 must be considered as used by this jump in USED and that of
1811 registers that must be considered as set in SET. */
1813 static void
1814 compute_jump_reg_dependencies (rtx insn ATTRIBUTE_UNUSED,
1815 regset cond_exec ATTRIBUTE_UNUSED,
1816 regset used ATTRIBUTE_UNUSED,
1817 regset set ATTRIBUTE_UNUSED)
1819 /* Nothing to do here, since we postprocess jumps in
1820 add_branch_dependences. */
1823 /* Used in schedule_insns to initialize current_sched_info for scheduling
1824 regions (or single basic blocks). */
1826 static struct sched_info region_sched_info =
1828 init_ready_list,
1829 can_schedule_ready_p,
1830 schedule_more_p,
1831 new_ready,
1832 rgn_rank,
1833 rgn_print_insn,
1834 contributes_to_priority,
1835 compute_jump_reg_dependencies,
1837 NULL, NULL,
1838 NULL, NULL,
1839 0, 0, 0
1842 /* Determine if PAT sets a CLASS_LIKELY_SPILLED_P register. */
1844 static bool
1845 sets_likely_spilled (rtx pat)
1847 bool ret = false;
1848 note_stores (pat, sets_likely_spilled_1, &ret);
1849 return ret;
1852 static void
1853 sets_likely_spilled_1 (rtx x, rtx pat, void *data)
1855 bool *ret = (bool *) data;
1857 if (GET_CODE (pat) == SET
1858 && REG_P (x)
1859 && REGNO (x) < FIRST_PSEUDO_REGISTER
1860 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (x))))
1861 *ret = true;
1864 /* Add dependences so that branches are scheduled to run last in their
1865 block. */
1867 static void
1868 add_branch_dependences (rtx head, rtx tail)
1870 rtx insn, last;
1872 /* For all branches, calls, uses, clobbers, cc0 setters, and instructions
1873 that can throw exceptions, force them to remain in order at the end of
1874 the block by adding dependencies and giving the last a high priority.
1875 There may be notes present, and prev_head may also be a note.
1877 Branches must obviously remain at the end. Calls should remain at the
1878 end since moving them results in worse register allocation. Uses remain
1879 at the end to ensure proper register allocation.
1881 cc0 setters remain at the end because they can't be moved away from
1882 their cc0 user.
1884 Insns setting CLASS_LIKELY_SPILLED_P registers (usually return values)
1885 are not moved before reload because we can wind up with register
1886 allocation failures. */
1888 insn = tail;
1889 last = 0;
1890 while (CALL_P (insn)
1891 || JUMP_P (insn)
1892 || (NONJUMP_INSN_P (insn)
1893 && (GET_CODE (PATTERN (insn)) == USE
1894 || GET_CODE (PATTERN (insn)) == CLOBBER
1895 || can_throw_internal (insn)
1896 #ifdef HAVE_cc0
1897 || sets_cc0_p (PATTERN (insn))
1898 #endif
1899 || (!reload_completed
1900 && sets_likely_spilled (PATTERN (insn)))))
1901 || NOTE_P (insn))
1903 if (!NOTE_P (insn))
1905 if (last != 0 && !find_insn_list (insn, LOG_LINKS (last)))
1907 add_dependence (last, insn, REG_DEP_ANTI);
1908 INSN_REF_COUNT (insn)++;
1911 CANT_MOVE (insn) = 1;
1913 last = insn;
1916 /* Don't overrun the bounds of the basic block. */
1917 if (insn == head)
1918 break;
1920 insn = PREV_INSN (insn);
1923 /* Make sure these insns are scheduled last in their block. */
1924 insn = last;
1925 if (insn != 0)
1926 while (insn != head)
1928 insn = prev_nonnote_insn (insn);
1930 if (INSN_REF_COUNT (insn) != 0)
1931 continue;
1933 add_dependence (last, insn, REG_DEP_ANTI);
1934 INSN_REF_COUNT (insn) = 1;
1938 /* Data structures for the computation of data dependences in a regions. We
1939 keep one `deps' structure for every basic block. Before analyzing the
1940 data dependences for a bb, its variables are initialized as a function of
1941 the variables of its predecessors. When the analysis for a bb completes,
1942 we save the contents to the corresponding bb_deps[bb] variable. */
1944 static struct deps *bb_deps;
1946 /* Duplicate the INSN_LIST elements of COPY and prepend them to OLD. */
1948 static rtx
1949 concat_INSN_LIST (rtx copy, rtx old)
1951 rtx new = old;
1952 for (; copy ; copy = XEXP (copy, 1))
1953 new = alloc_INSN_LIST (XEXP (copy, 0), new);
1954 return new;
1957 static void
1958 concat_insn_mem_list (rtx copy_insns, rtx copy_mems, rtx *old_insns_p,
1959 rtx *old_mems_p)
1961 rtx new_insns = *old_insns_p;
1962 rtx new_mems = *old_mems_p;
1964 while (copy_insns)
1966 new_insns = alloc_INSN_LIST (XEXP (copy_insns, 0), new_insns);
1967 new_mems = alloc_EXPR_LIST (VOIDmode, XEXP (copy_mems, 0), new_mems);
1968 copy_insns = XEXP (copy_insns, 1);
1969 copy_mems = XEXP (copy_mems, 1);
1972 *old_insns_p = new_insns;
1973 *old_mems_p = new_mems;
1976 /* After computing the dependencies for block BB, propagate the dependencies
1977 found in TMP_DEPS to the successors of the block. */
1978 static void
1979 propagate_deps (int bb, struct deps *pred_deps)
1981 basic_block block = BASIC_BLOCK (BB_TO_BLOCK (bb));
1982 edge_iterator ei;
1983 edge e;
1985 /* bb's structures are inherited by its successors. */
1986 FOR_EACH_EDGE (e, ei, block->succs)
1988 struct deps *succ_deps;
1989 int reg;
1991 /* Only bbs "below" bb, in the same region, are interesting. */
1992 if (e->dest == EXIT_BLOCK_PTR
1993 || CONTAINING_RGN (block->index) != CONTAINING_RGN (e->dest->index)
1994 || BLOCK_TO_BB (e->dest->index) <= bb)
1995 continue;
1997 succ_deps = bb_deps + BLOCK_TO_BB (e->dest->index);
1999 /* The reg_last lists are inherited by successor. */
2000 EXECUTE_IF_SET_IN_REG_SET (&pred_deps->reg_last_in_use, 0, reg,
2002 struct deps_reg *pred_rl = &pred_deps->reg_last[reg];
2003 struct deps_reg *succ_rl = &succ_deps->reg_last[reg];
2005 succ_rl->uses = concat_INSN_LIST (pred_rl->uses, succ_rl->uses);
2006 succ_rl->sets = concat_INSN_LIST (pred_rl->sets, succ_rl->sets);
2007 succ_rl->clobbers = concat_INSN_LIST (pred_rl->clobbers,
2008 succ_rl->clobbers);
2009 succ_rl->uses_length += pred_rl->uses_length;
2010 succ_rl->clobbers_length += pred_rl->clobbers_length;
2012 IOR_REG_SET (&succ_deps->reg_last_in_use, &pred_deps->reg_last_in_use);
2014 /* Mem read/write lists are inherited by successor. */
2015 concat_insn_mem_list (pred_deps->pending_read_insns,
2016 pred_deps->pending_read_mems,
2017 &succ_deps->pending_read_insns,
2018 &succ_deps->pending_read_mems);
2019 concat_insn_mem_list (pred_deps->pending_write_insns,
2020 pred_deps->pending_write_mems,
2021 &succ_deps->pending_write_insns,
2022 &succ_deps->pending_write_mems);
2024 succ_deps->last_pending_memory_flush
2025 = concat_INSN_LIST (pred_deps->last_pending_memory_flush,
2026 succ_deps->last_pending_memory_flush);
2028 succ_deps->pending_lists_length += pred_deps->pending_lists_length;
2029 succ_deps->pending_flush_length += pred_deps->pending_flush_length;
2031 /* last_function_call is inherited by successor. */
2032 succ_deps->last_function_call
2033 = concat_INSN_LIST (pred_deps->last_function_call,
2034 succ_deps->last_function_call);
2036 /* sched_before_next_call is inherited by successor. */
2037 succ_deps->sched_before_next_call
2038 = concat_INSN_LIST (pred_deps->sched_before_next_call,
2039 succ_deps->sched_before_next_call);
2042 /* These lists should point to the right place, for correct
2043 freeing later. */
2044 bb_deps[bb].pending_read_insns = pred_deps->pending_read_insns;
2045 bb_deps[bb].pending_read_mems = pred_deps->pending_read_mems;
2046 bb_deps[bb].pending_write_insns = pred_deps->pending_write_insns;
2047 bb_deps[bb].pending_write_mems = pred_deps->pending_write_mems;
2049 /* Can't allow these to be freed twice. */
2050 pred_deps->pending_read_insns = 0;
2051 pred_deps->pending_read_mems = 0;
2052 pred_deps->pending_write_insns = 0;
2053 pred_deps->pending_write_mems = 0;
2056 /* Compute backward dependences inside bb. In a multiple blocks region:
2057 (1) a bb is analyzed after its predecessors, and (2) the lists in
2058 effect at the end of bb (after analyzing for bb) are inherited by
2059 bb's successors.
2061 Specifically for reg-reg data dependences, the block insns are
2062 scanned by sched_analyze () top-to-bottom. Two lists are
2063 maintained by sched_analyze (): reg_last[].sets for register DEFs,
2064 and reg_last[].uses for register USEs.
2066 When analysis is completed for bb, we update for its successors:
2067 ; - DEFS[succ] = Union (DEFS [succ], DEFS [bb])
2068 ; - USES[succ] = Union (USES [succ], DEFS [bb])
2070 The mechanism for computing mem-mem data dependence is very
2071 similar, and the result is interblock dependences in the region. */
2073 static void
2074 compute_block_backward_dependences (int bb)
2076 rtx head, tail;
2077 struct deps tmp_deps;
2079 tmp_deps = bb_deps[bb];
2081 /* Do the analysis for this block. */
2082 get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
2083 sched_analyze (&tmp_deps, head, tail);
2084 add_branch_dependences (head, tail);
2086 if (current_nr_blocks > 1)
2087 propagate_deps (bb, &tmp_deps);
2089 /* Free up the INSN_LISTs. */
2090 free_deps (&tmp_deps);
2093 /* Remove all INSN_LISTs and EXPR_LISTs from the pending lists and add
2094 them to the unused_*_list variables, so that they can be reused. */
2096 static void
2097 free_pending_lists (void)
2099 int bb;
2101 for (bb = 0; bb < current_nr_blocks; bb++)
2103 free_INSN_LIST_list (&bb_deps[bb].pending_read_insns);
2104 free_INSN_LIST_list (&bb_deps[bb].pending_write_insns);
2105 free_EXPR_LIST_list (&bb_deps[bb].pending_read_mems);
2106 free_EXPR_LIST_list (&bb_deps[bb].pending_write_mems);
2110 /* Print dependences for debugging, callable from debugger. */
2112 void
2113 debug_dependencies (void)
2115 int bb;
2117 fprintf (sched_dump, ";; --------------- forward dependences: ------------ \n");
2118 for (bb = 0; bb < current_nr_blocks; bb++)
2120 rtx head, tail;
2121 rtx next_tail;
2122 rtx insn;
2124 get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
2125 next_tail = NEXT_INSN (tail);
2126 fprintf (sched_dump, "\n;; --- Region Dependences --- b %d bb %d \n",
2127 BB_TO_BLOCK (bb), bb);
2129 fprintf (sched_dump, ";; %7s%6s%6s%6s%6s%6s%14s\n",
2130 "insn", "code", "bb", "dep", "prio", "cost",
2131 "reservation");
2132 fprintf (sched_dump, ";; %7s%6s%6s%6s%6s%6s%14s\n",
2133 "----", "----", "--", "---", "----", "----",
2134 "-----------");
2136 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
2138 rtx link;
2140 if (! INSN_P (insn))
2142 int n;
2143 fprintf (sched_dump, ";; %6d ", INSN_UID (insn));
2144 if (NOTE_P (insn))
2146 n = NOTE_LINE_NUMBER (insn);
2147 if (n < 0)
2148 fprintf (sched_dump, "%s\n", GET_NOTE_INSN_NAME (n));
2149 else
2151 expanded_location xloc;
2152 NOTE_EXPANDED_LOCATION (xloc, insn);
2153 fprintf (sched_dump, "line %d, file %s\n",
2154 xloc.line, xloc.file);
2157 else
2158 fprintf (sched_dump, " {%s}\n", GET_RTX_NAME (GET_CODE (insn)));
2159 continue;
2162 fprintf (sched_dump,
2163 ";; %s%5d%6d%6d%6d%6d%6d ",
2164 (SCHED_GROUP_P (insn) ? "+" : " "),
2165 INSN_UID (insn),
2166 INSN_CODE (insn),
2167 INSN_BB (insn),
2168 INSN_DEP_COUNT (insn),
2169 INSN_PRIORITY (insn),
2170 insn_cost (insn, 0, 0));
2172 if (recog_memoized (insn) < 0)
2173 fprintf (sched_dump, "nothing");
2174 else
2175 print_reservation (sched_dump, insn);
2177 fprintf (sched_dump, "\t: ");
2178 for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
2179 fprintf (sched_dump, "%d ", INSN_UID (XEXP (link, 0)));
2180 fprintf (sched_dump, "\n");
2183 fprintf (sched_dump, "\n");
2186 /* Returns true if all the basic blocks of the current region have
2187 NOTE_DISABLE_SCHED_OF_BLOCK which means not to schedule that region. */
2188 static bool
2189 sched_is_disabled_for_current_region_p (void)
2191 int bb;
2193 for (bb = 0; bb < current_nr_blocks; bb++)
2194 if (!(BASIC_BLOCK (BB_TO_BLOCK (bb))->flags & BB_DISABLE_SCHEDULE))
2195 return false;
2197 return true;
2200 /* Schedule a region. A region is either an inner loop, a loop-free
2201 subroutine, or a single basic block. Each bb in the region is
2202 scheduled after its flow predecessors. */
2204 static void
2205 schedule_region (int rgn)
2207 basic_block block;
2208 edge_iterator ei;
2209 edge e;
2210 int bb;
2211 int rgn_n_insns = 0;
2212 int sched_rgn_n_insns = 0;
2214 /* Set variables for the current region. */
2215 current_nr_blocks = RGN_NR_BLOCKS (rgn);
2216 current_blocks = RGN_BLOCKS (rgn);
2218 /* Don't schedule region that is marked by
2219 NOTE_DISABLE_SCHED_OF_BLOCK. */
2220 if (sched_is_disabled_for_current_region_p ())
2221 return;
2223 init_deps_global ();
2225 /* Initializations for region data dependence analysis. */
2226 bb_deps = xmalloc (sizeof (struct deps) * current_nr_blocks);
2227 for (bb = 0; bb < current_nr_blocks; bb++)
2228 init_deps (bb_deps + bb);
2230 /* Compute LOG_LINKS. */
2231 for (bb = 0; bb < current_nr_blocks; bb++)
2232 compute_block_backward_dependences (bb);
2234 /* Compute INSN_DEPEND. */
2235 for (bb = current_nr_blocks - 1; bb >= 0; bb--)
2237 rtx head, tail;
2238 get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
2240 compute_forward_dependences (head, tail);
2242 if (targetm.sched.dependencies_evaluation_hook)
2243 targetm.sched.dependencies_evaluation_hook (head, tail);
2247 /* Set priorities. */
2248 for (bb = 0; bb < current_nr_blocks; bb++)
2250 rtx head, tail;
2251 get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
2253 rgn_n_insns += set_priorities (head, tail);
2256 /* Compute interblock info: probabilities, split-edges, dominators, etc. */
2257 if (current_nr_blocks > 1)
2259 prob = xmalloc ((current_nr_blocks) * sizeof (float));
2261 dom = sbitmap_vector_alloc (current_nr_blocks, current_nr_blocks);
2262 sbitmap_vector_zero (dom, current_nr_blocks);
2264 /* Use ->aux to implement EDGE_TO_BIT mapping. */
2265 rgn_nr_edges = 0;
2266 FOR_EACH_BB (block)
2268 if (CONTAINING_RGN (block->index) != rgn)
2269 continue;
2270 FOR_EACH_EDGE (e, ei, block->succs)
2271 SET_EDGE_TO_BIT (e, rgn_nr_edges++);
2274 rgn_edges = xmalloc (rgn_nr_edges * sizeof (edge));
2275 rgn_nr_edges = 0;
2276 FOR_EACH_BB (block)
2278 if (CONTAINING_RGN (block->index) != rgn)
2279 continue;
2280 FOR_EACH_EDGE (e, ei, block->succs)
2281 rgn_edges[rgn_nr_edges++] = e;
2284 /* Split edges. */
2285 pot_split = sbitmap_vector_alloc (current_nr_blocks, rgn_nr_edges);
2286 sbitmap_vector_zero (pot_split, current_nr_blocks);
2287 ancestor_edges = sbitmap_vector_alloc (current_nr_blocks, rgn_nr_edges);
2288 sbitmap_vector_zero (ancestor_edges, current_nr_blocks);
2290 /* Compute probabilities, dominators, split_edges. */
2291 for (bb = 0; bb < current_nr_blocks; bb++)
2292 compute_dom_prob_ps (bb);
2295 /* Now we can schedule all blocks. */
2296 for (bb = 0; bb < current_nr_blocks; bb++)
2298 rtx head, tail;
2299 int b = BB_TO_BLOCK (bb);
2301 get_block_head_tail (b, &head, &tail);
2303 if (no_real_insns_p (head, tail))
2304 continue;
2306 current_sched_info->prev_head = PREV_INSN (head);
2307 current_sched_info->next_tail = NEXT_INSN (tail);
2309 if (write_symbols != NO_DEBUG)
2311 save_line_notes (b, head, tail);
2312 rm_line_notes (head, tail);
2315 /* rm_other_notes only removes notes which are _inside_ the
2316 block---that is, it won't remove notes before the first real insn
2317 or after the last real insn of the block. So if the first insn
2318 has a REG_SAVE_NOTE which would otherwise be emitted before the
2319 insn, it is redundant with the note before the start of the
2320 block, and so we have to take it out. */
2321 if (INSN_P (head))
2323 rtx note;
2325 for (note = REG_NOTES (head); note; note = XEXP (note, 1))
2326 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
2328 remove_note (head, note);
2329 note = XEXP (note, 1);
2330 remove_note (head, note);
2334 /* Remove remaining note insns from the block, save them in
2335 note_list. These notes are restored at the end of
2336 schedule_block (). */
2337 rm_other_notes (head, tail);
2339 target_bb = bb;
2341 current_sched_info->queue_must_finish_empty
2342 = current_nr_blocks > 1 && !flag_schedule_interblock;
2344 schedule_block (b, rgn_n_insns);
2345 sched_rgn_n_insns += sched_n_insns;
2347 /* Update target block boundaries. */
2348 if (head == BB_HEAD (BASIC_BLOCK (b)))
2349 BB_HEAD (BASIC_BLOCK (b)) = current_sched_info->head;
2350 if (tail == BB_END (BASIC_BLOCK (b)))
2351 BB_END (BASIC_BLOCK (b)) = current_sched_info->tail;
2353 /* Clean up. */
2354 if (current_nr_blocks > 1)
2356 free (candidate_table);
2357 free (bblst_table);
2358 free (edgelst_table);
2362 /* Sanity check: verify that all region insns were scheduled. */
2363 gcc_assert (sched_rgn_n_insns == rgn_n_insns);
2365 /* Restore line notes. */
2366 if (write_symbols != NO_DEBUG)
2368 for (bb = 0; bb < current_nr_blocks; bb++)
2370 rtx head, tail;
2371 get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
2372 restore_line_notes (head, tail);
2376 /* Done with this region. */
2377 free_pending_lists ();
2379 finish_deps_global ();
2381 free (bb_deps);
2383 if (current_nr_blocks > 1)
2385 /* Cleanup ->aux used for EDGE_TO_BIT mapping. */
2386 FOR_EACH_BB (block)
2388 if (CONTAINING_RGN (block->index) != rgn)
2389 continue;
2390 FOR_EACH_EDGE (e, ei, block->succs)
2391 e->aux = NULL;
2394 free (prob);
2395 sbitmap_vector_free (dom);
2396 sbitmap_vector_free (pot_split);
2397 sbitmap_vector_free (ancestor_edges);
2398 free (rgn_edges);
2402 /* Indexed by region, holds the number of death notes found in that region.
2403 Used for consistency checks. */
2404 static int *deaths_in_region;
2406 /* Initialize data structures for region scheduling. */
2408 static void
2409 init_regions (void)
2411 sbitmap blocks;
2412 int rgn;
2414 nr_regions = 0;
2415 rgn_table = xmalloc ((n_basic_blocks) * sizeof (region));
2416 rgn_bb_table = xmalloc ((n_basic_blocks) * sizeof (int));
2417 block_to_bb = xmalloc ((last_basic_block) * sizeof (int));
2418 containing_rgn = xmalloc ((last_basic_block) * sizeof (int));
2420 /* Compute regions for scheduling. */
2421 if (reload_completed
2422 || n_basic_blocks == 1
2423 || !flag_schedule_interblock
2424 || is_cfg_nonregular ())
2426 find_single_block_region ();
2428 else
2430 /* Compute the dominators and post dominators. */
2431 calculate_dominance_info (CDI_DOMINATORS);
2433 /* Find regions. */
2434 find_rgns ();
2436 if (sched_verbose >= 3)
2437 debug_regions ();
2439 /* For now. This will move as more and more of haifa is converted
2440 to using the cfg code in flow.c. */
2441 free_dominance_info (CDI_DOMINATORS);
2445 if (CHECK_DEAD_NOTES)
2447 blocks = sbitmap_alloc (last_basic_block);
2448 deaths_in_region = xmalloc (sizeof (int) * nr_regions);
2449 /* Remove all death notes from the subroutine. */
2450 for (rgn = 0; rgn < nr_regions; rgn++)
2452 int b;
2454 sbitmap_zero (blocks);
2455 for (b = RGN_NR_BLOCKS (rgn) - 1; b >= 0; --b)
2456 SET_BIT (blocks, rgn_bb_table[RGN_BLOCKS (rgn) + b]);
2458 deaths_in_region[rgn] = count_or_remove_death_notes (blocks, 1);
2460 sbitmap_free (blocks);
2462 else
2463 count_or_remove_death_notes (NULL, 1);
2466 /* The one entry point in this file. DUMP_FILE is the dump file for
2467 this pass. */
2469 void
2470 schedule_insns (FILE *dump_file)
2472 sbitmap large_region_blocks, blocks;
2473 int rgn;
2474 int any_large_regions;
2475 basic_block bb;
2477 /* Taking care of this degenerate case makes the rest of
2478 this code simpler. */
2479 if (n_basic_blocks == 0)
2480 return;
2482 nr_inter = 0;
2483 nr_spec = 0;
2485 sched_init (dump_file);
2487 init_regions ();
2489 current_sched_info = &region_sched_info;
2491 /* Schedule every region in the subroutine. */
2492 for (rgn = 0; rgn < nr_regions; rgn++)
2493 schedule_region (rgn);
2495 /* Update life analysis for the subroutine. Do single block regions
2496 first so that we can verify that live_at_start didn't change. Then
2497 do all other blocks. */
2498 /* ??? There is an outside possibility that update_life_info, or more
2499 to the point propagate_block, could get called with nonzero flags
2500 more than once for one basic block. This would be kinda bad if it
2501 were to happen, since REG_INFO would be accumulated twice for the
2502 block, and we'd have twice the REG_DEAD notes.
2504 I'm fairly certain that this _shouldn't_ happen, since I don't think
2505 that live_at_start should change at region heads. Not sure what the
2506 best way to test for this kind of thing... */
2508 allocate_reg_life_data ();
2509 compute_bb_for_insn ();
2511 any_large_regions = 0;
2512 large_region_blocks = sbitmap_alloc (last_basic_block);
2513 sbitmap_zero (large_region_blocks);
2514 FOR_EACH_BB (bb)
2515 SET_BIT (large_region_blocks, bb->index);
2517 blocks = sbitmap_alloc (last_basic_block);
2518 sbitmap_zero (blocks);
2520 /* Update life information. For regions consisting of multiple blocks
2521 we've possibly done interblock scheduling that affects global liveness.
2522 For regions consisting of single blocks we need to do only local
2523 liveness. */
2524 for (rgn = 0; rgn < nr_regions; rgn++)
2525 if (RGN_NR_BLOCKS (rgn) > 1)
2526 any_large_regions = 1;
2527 else
2529 SET_BIT (blocks, rgn_bb_table[RGN_BLOCKS (rgn)]);
2530 RESET_BIT (large_region_blocks, rgn_bb_table[RGN_BLOCKS (rgn)]);
2533 /* Don't update reg info after reload, since that affects
2534 regs_ever_live, which should not change after reload. */
2535 update_life_info (blocks, UPDATE_LIFE_LOCAL,
2536 (reload_completed ? PROP_DEATH_NOTES
2537 : PROP_DEATH_NOTES | PROP_REG_INFO));
2538 if (any_large_regions)
2540 update_life_info (large_region_blocks, UPDATE_LIFE_GLOBAL,
2541 PROP_DEATH_NOTES | PROP_REG_INFO);
2544 if (CHECK_DEAD_NOTES)
2546 /* Verify the counts of basic block notes in single the basic block
2547 regions. */
2548 for (rgn = 0; rgn < nr_regions; rgn++)
2549 if (RGN_NR_BLOCKS (rgn) == 1)
2551 sbitmap_zero (blocks);
2552 SET_BIT (blocks, rgn_bb_table[RGN_BLOCKS (rgn)]);
2554 gcc_assert (deaths_in_region[rgn]
2555 == count_or_remove_death_notes (blocks, 0));
2557 free (deaths_in_region);
2560 /* Reposition the prologue and epilogue notes in case we moved the
2561 prologue/epilogue insns. */
2562 if (reload_completed)
2563 reposition_prologue_and_epilogue_notes (get_insns ());
2565 /* Delete redundant line notes. */
2566 if (write_symbols != NO_DEBUG)
2567 rm_redundant_line_notes ();
2569 if (sched_verbose)
2571 if (reload_completed == 0 && flag_schedule_interblock)
2573 fprintf (sched_dump,
2574 "\n;; Procedure interblock/speculative motions == %d/%d \n",
2575 nr_inter, nr_spec);
2577 else
2578 gcc_assert (nr_inter <= 0);
2579 fprintf (sched_dump, "\n\n");
2582 /* Clean up. */
2583 free (rgn_table);
2584 free (rgn_bb_table);
2585 free (block_to_bb);
2586 free (containing_rgn);
2588 sched_finish ();
2590 sbitmap_free (blocks);
2591 sbitmap_free (large_region_blocks);
2593 #endif