PR c++/15745
[official-gcc.git] / gcc / sched-rgn.c
blob6df5c33ec2544e8557deec8b6cb661ed6895758b
1 /* Instruction scheduling pass.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
6 and currently maintained by, Jim Wilson (wilson@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
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 "regs.h"
57 #include "function.h"
58 #include "flags.h"
59 #include "insn-config.h"
60 #include "insn-attr.h"
61 #include "except.h"
62 #include "toplev.h"
63 #include "recog.h"
64 #include "cfglayout.h"
65 #include "params.h"
66 #include "sched-int.h"
67 #include "target.h"
68 #include "timevar.h"
69 #include "tree-pass.h"
70 #include "dbgcnt.h"
72 #ifdef INSN_SCHEDULING
73 /* Some accessor macros for h_i_d members only used within this file. */
74 #define INSN_REF_COUNT(INSN) (h_i_d[INSN_UID (INSN)].ref_count)
75 #define FED_BY_SPEC_LOAD(insn) (h_i_d[INSN_UID (insn)].fed_by_spec_load)
76 #define IS_LOAD_INSN(insn) (h_i_d[INSN_UID (insn)].is_load_insn)
78 /* nr_inter/spec counts interblock/speculative motion for the function. */
79 static int nr_inter, nr_spec;
81 static int is_cfg_nonregular (void);
82 static bool sched_is_disabled_for_current_region_p (void);
84 /* A region is the main entity for interblock scheduling: insns
85 are allowed to move between blocks in the same region, along
86 control flow graph edges, in the 'up' direction. */
87 typedef struct
89 /* Number of extended basic blocks in region. */
90 int rgn_nr_blocks;
91 /* cblocks in the region (actually index in rgn_bb_table). */
92 int rgn_blocks;
93 /* Dependencies for this region are already computed. Basically, indicates,
94 that this is a recovery block. */
95 unsigned int dont_calc_deps : 1;
96 /* This region has at least one non-trivial ebb. */
97 unsigned int has_real_ebb : 1;
99 region;
101 /* Number of regions in the procedure. */
102 static int nr_regions;
104 /* Table of region descriptions. */
105 static region *rgn_table;
107 /* Array of lists of regions' blocks. */
108 static int *rgn_bb_table;
110 /* Topological order of blocks in the region (if b2 is reachable from
111 b1, block_to_bb[b2] > block_to_bb[b1]). Note: A basic block is
112 always referred to by either block or b, while its topological
113 order name (in the region) is referred to by bb. */
114 static int *block_to_bb;
116 /* The number of the region containing a block. */
117 static int *containing_rgn;
119 /* The minimum probability of reaching a source block so that it will be
120 considered for speculative scheduling. */
121 static int min_spec_prob;
123 #define RGN_NR_BLOCKS(rgn) (rgn_table[rgn].rgn_nr_blocks)
124 #define RGN_BLOCKS(rgn) (rgn_table[rgn].rgn_blocks)
125 #define RGN_DONT_CALC_DEPS(rgn) (rgn_table[rgn].dont_calc_deps)
126 #define RGN_HAS_REAL_EBB(rgn) (rgn_table[rgn].has_real_ebb)
127 #define BLOCK_TO_BB(block) (block_to_bb[block])
128 #define CONTAINING_RGN(block) (containing_rgn[block])
130 void debug_regions (void);
131 static void find_single_block_region (void);
132 static void find_rgns (void);
133 static void extend_rgns (int *, int *, sbitmap, int *);
134 static bool too_large (int, int *, int *);
136 extern void debug_live (int, int);
138 /* Blocks of the current region being scheduled. */
139 static int current_nr_blocks;
140 static int current_blocks;
142 static int rgn_n_insns;
144 /* The mapping from ebb to block. */
145 /* ebb_head [i] - is index in rgn_bb_table, while
146 EBB_HEAD (i) - is basic block index.
147 BASIC_BLOCK (EBB_HEAD (i)) - head of ebb. */
148 #define BB_TO_BLOCK(ebb) (rgn_bb_table[ebb_head[ebb]])
149 #define EBB_FIRST_BB(ebb) BASIC_BLOCK (BB_TO_BLOCK (ebb))
150 #define EBB_LAST_BB(ebb) BASIC_BLOCK (rgn_bb_table[ebb_head[ebb + 1] - 1])
152 /* Target info declarations.
154 The block currently being scheduled is referred to as the "target" block,
155 while other blocks in the region from which insns can be moved to the
156 target are called "source" blocks. The candidate structure holds info
157 about such sources: are they valid? Speculative? Etc. */
158 typedef struct
160 basic_block *first_member;
161 int nr_members;
163 bblst;
165 typedef struct
167 char is_valid;
168 char is_speculative;
169 int src_prob;
170 bblst split_bbs;
171 bblst update_bbs;
173 candidate;
175 static candidate *candidate_table;
177 /* A speculative motion requires checking live information on the path
178 from 'source' to 'target'. The split blocks are those to be checked.
179 After a speculative motion, live information should be modified in
180 the 'update' blocks.
182 Lists of split and update blocks for each candidate of the current
183 target are in array bblst_table. */
184 static basic_block *bblst_table;
185 static int bblst_size, bblst_last;
187 #define IS_VALID(src) ( candidate_table[src].is_valid )
188 #define IS_SPECULATIVE(src) ( candidate_table[src].is_speculative )
189 #define SRC_PROB(src) ( candidate_table[src].src_prob )
191 /* The bb being currently scheduled. */
192 static int target_bb;
194 /* List of edges. */
195 typedef struct
197 edge *first_member;
198 int nr_members;
200 edgelst;
202 static edge *edgelst_table;
203 static int edgelst_last;
205 static void extract_edgelst (sbitmap, edgelst *);
208 /* Target info functions. */
209 static void split_edges (int, int, edgelst *);
210 static void compute_trg_info (int);
211 void debug_candidate (int);
212 void debug_candidates (int);
214 /* Dominators array: dom[i] contains the sbitmap of dominators of
215 bb i in the region. */
216 static sbitmap *dom;
218 /* bb 0 is the only region entry. */
219 #define IS_RGN_ENTRY(bb) (!bb)
221 /* Is bb_src dominated by bb_trg. */
222 #define IS_DOMINATED(bb_src, bb_trg) \
223 ( TEST_BIT (dom[bb_src], bb_trg) )
225 /* Probability: Prob[i] is an int in [0, REG_BR_PROB_BASE] which is
226 the probability of bb i relative to the region entry. */
227 static int *prob;
229 /* Bit-set of edges, where bit i stands for edge i. */
230 typedef sbitmap edgeset;
232 /* Number of edges in the region. */
233 static int rgn_nr_edges;
235 /* Array of size rgn_nr_edges. */
236 static edge *rgn_edges;
238 /* Mapping from each edge in the graph to its number in the rgn. */
239 #define EDGE_TO_BIT(edge) ((int)(size_t)(edge)->aux)
240 #define SET_EDGE_TO_BIT(edge,nr) ((edge)->aux = (void *)(size_t)(nr))
242 /* The split edges of a source bb is different for each target
243 bb. In order to compute this efficiently, the 'potential-split edges'
244 are computed for each bb prior to scheduling a region. This is actually
245 the split edges of each bb relative to the region entry.
247 pot_split[bb] is the set of potential split edges of bb. */
248 static edgeset *pot_split;
250 /* For every bb, a set of its ancestor edges. */
251 static edgeset *ancestor_edges;
253 /* Array of EBBs sizes. Currently we can get a ebb only through
254 splitting of currently scheduling block, therefore, we don't need
255 ebb_head array for every region, its sufficient to hold it only
256 for current one. */
257 static int *ebb_head;
259 static void compute_dom_prob_ps (int);
261 #define INSN_PROBABILITY(INSN) (SRC_PROB (BLOCK_TO_BB (BLOCK_NUM (INSN))))
262 #define IS_SPECULATIVE_INSN(INSN) (IS_SPECULATIVE (BLOCK_TO_BB (BLOCK_NUM (INSN))))
263 #define INSN_BB(INSN) (BLOCK_TO_BB (BLOCK_NUM (INSN)))
265 /* Speculative scheduling functions. */
266 static int check_live_1 (int, rtx);
267 static void update_live_1 (int, rtx);
268 static int check_live (rtx, int);
269 static void update_live (rtx, int);
270 static void set_spec_fed (rtx);
271 static int is_pfree (rtx, int, int);
272 static int find_conditional_protection (rtx, int);
273 static int is_conditionally_protected (rtx, int, int);
274 static int is_prisky (rtx, int, int);
275 static int is_exception_free (rtx, int, int);
277 static bool sets_likely_spilled (rtx);
278 static void sets_likely_spilled_1 (rtx, const_rtx, void *);
279 static void add_branch_dependences (rtx, rtx);
280 static void compute_block_dependences (int);
282 static void init_regions (void);
283 static void schedule_region (int);
284 static rtx concat_INSN_LIST (rtx, rtx);
285 static void concat_insn_mem_list (rtx, rtx, rtx *, rtx *);
286 static void propagate_deps (int, struct deps *);
287 static void free_pending_lists (void);
289 /* Functions for construction of the control flow graph. */
291 /* Return 1 if control flow graph should not be constructed, 0 otherwise.
293 We decide not to build the control flow graph if there is possibly more
294 than one entry to the function, if computed branches exist, if we
295 have nonlocal gotos, or if we have an unreachable loop. */
297 static int
298 is_cfg_nonregular (void)
300 basic_block b;
301 rtx insn;
303 /* If we have a label that could be the target of a nonlocal goto, then
304 the cfg is not well structured. */
305 if (nonlocal_goto_handler_labels)
306 return 1;
308 /* If we have any forced labels, then the cfg is not well structured. */
309 if (forced_labels)
310 return 1;
312 /* If we have exception handlers, then we consider the cfg not well
313 structured. ?!? We should be able to handle this now that we
314 compute an accurate cfg for EH. */
315 if (current_function_has_exception_handlers ())
316 return 1;
318 /* If we have non-jumping insns which refer to labels, then we consider
319 the cfg not well structured. */
320 FOR_EACH_BB (b)
321 FOR_BB_INSNS (b, insn)
323 /* Check for labels referred by non-jump insns. */
324 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
326 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
327 if (note
328 && ! (JUMP_P (NEXT_INSN (insn))
329 && find_reg_note (NEXT_INSN (insn), REG_LABEL,
330 XEXP (note, 0))))
331 return 1;
333 /* If this function has a computed jump, then we consider the cfg
334 not well structured. */
335 else if (JUMP_P (insn) && computed_jump_p (insn))
336 return 1;
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 || (single_pred_p (b)
349 && single_pred (b) == b))
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 unsigned int i = 0;
363 sbitmap_iterator sbi;
365 /* edgelst table space is reused in each call to extract_edgelst. */
366 edgelst_last = 0;
368 el->first_member = &edgelst_table[edgelst_last];
369 el->nr_members = 0;
371 /* Iterate over each word in the bitset. */
372 EXECUTE_IF_SET_IN_SBITMAP (set, 0, i, sbi)
374 edgelst_table[edgelst_last++] = rgn_edges[i];
375 el->nr_members++;
379 /* Functions for the construction of regions. */
381 /* Print the regions, for debugging purposes. Callable from debugger. */
383 void
384 debug_regions (void)
386 int rgn, bb;
388 fprintf (sched_dump, "\n;; ------------ REGIONS ----------\n\n");
389 for (rgn = 0; rgn < nr_regions; rgn++)
391 fprintf (sched_dump, ";;\trgn %d nr_blocks %d:\n", rgn,
392 rgn_table[rgn].rgn_nr_blocks);
393 fprintf (sched_dump, ";;\tbb/block: ");
395 /* We don't have ebb_head initialized yet, so we can't use
396 BB_TO_BLOCK (). */
397 current_blocks = RGN_BLOCKS (rgn);
399 for (bb = 0; bb < rgn_table[rgn].rgn_nr_blocks; bb++)
400 fprintf (sched_dump, " %d/%d ", bb, rgn_bb_table[current_blocks + 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 RGN_DONT_CALC_DEPS (nr_regions) = 0;
423 RGN_HAS_REAL_EBB (nr_regions) = 0;
424 CONTAINING_RGN (bb->index) = nr_regions;
425 BLOCK_TO_BB (bb->index) = 0;
426 nr_regions++;
430 /* Update number of blocks and the estimate for number of insns
431 in the region. Return true if the region is "too large" for interblock
432 scheduling (compile time considerations). */
434 static bool
435 too_large (int block, int *num_bbs, int *num_insns)
437 (*num_bbs)++;
438 (*num_insns) += (INSN_LUID (BB_END (BASIC_BLOCK (block)))
439 - INSN_LUID (BB_HEAD (BASIC_BLOCK (block))));
441 return ((*num_bbs > PARAM_VALUE (PARAM_MAX_SCHED_REGION_BLOCKS))
442 || (*num_insns > PARAM_VALUE (PARAM_MAX_SCHED_REGION_INSNS)));
445 /* Update_loop_relations(blk, hdr): Check if the loop headed by max_hdr[blk]
446 is still an inner loop. Put in max_hdr[blk] the header of the most inner
447 loop containing blk. */
448 #define UPDATE_LOOP_RELATIONS(blk, hdr) \
450 if (max_hdr[blk] == -1) \
451 max_hdr[blk] = hdr; \
452 else if (dfs_nr[max_hdr[blk]] > dfs_nr[hdr]) \
453 RESET_BIT (inner, hdr); \
454 else if (dfs_nr[max_hdr[blk]] < dfs_nr[hdr]) \
456 RESET_BIT (inner,max_hdr[blk]); \
457 max_hdr[blk] = hdr; \
461 /* Find regions for interblock scheduling.
463 A region for scheduling can be:
465 * A loop-free procedure, or
467 * A reducible inner loop, or
469 * A basic block not contained in any other region.
471 ?!? In theory we could build other regions based on extended basic
472 blocks or reverse extended basic blocks. Is it worth the trouble?
474 Loop blocks that form a region are put into the region's block list
475 in topological order.
477 This procedure stores its results into the following global (ick) variables
479 * rgn_nr
480 * rgn_table
481 * rgn_bb_table
482 * block_to_bb
483 * containing region
485 We use dominator relationships to avoid making regions out of non-reducible
486 loops.
488 This procedure needs to be converted to work on pred/succ lists instead
489 of edge tables. That would simplify it somewhat. */
491 static void
492 find_rgns (void)
494 int *max_hdr, *dfs_nr, *degree;
495 char no_loops = 1;
496 int node, child, loop_head, i, head, tail;
497 int count = 0, sp, idx = 0;
498 edge_iterator current_edge;
499 edge_iterator *stack;
500 int num_bbs, num_insns, unreachable;
501 int too_large_failure;
502 basic_block bb;
504 /* Note if a block is a natural loop header. */
505 sbitmap header;
507 /* Note if a block is a natural inner loop header. */
508 sbitmap inner;
510 /* Note if a block is in the block queue. */
511 sbitmap in_queue;
513 /* Note if a block is in the block queue. */
514 sbitmap in_stack;
516 /* Perform a DFS traversal of the cfg. Identify loop headers, inner loops
517 and a mapping from block to its loop header (if the block is contained
518 in a loop, else -1).
520 Store results in HEADER, INNER, and MAX_HDR respectively, these will
521 be used as inputs to the second traversal.
523 STACK, SP and DFS_NR are only used during the first traversal. */
525 /* Allocate and initialize variables for the first traversal. */
526 max_hdr = XNEWVEC (int, last_basic_block);
527 dfs_nr = XCNEWVEC (int, last_basic_block);
528 stack = XNEWVEC (edge_iterator, n_edges);
530 inner = sbitmap_alloc (last_basic_block);
531 sbitmap_ones (inner);
533 header = sbitmap_alloc (last_basic_block);
534 sbitmap_zero (header);
536 in_queue = sbitmap_alloc (last_basic_block);
537 sbitmap_zero (in_queue);
539 in_stack = sbitmap_alloc (last_basic_block);
540 sbitmap_zero (in_stack);
542 for (i = 0; i < last_basic_block; i++)
543 max_hdr[i] = -1;
545 #define EDGE_PASSED(E) (ei_end_p ((E)) || ei_edge ((E))->aux)
546 #define SET_EDGE_PASSED(E) (ei_edge ((E))->aux = ei_edge ((E)))
548 /* DFS traversal to find inner loops in the cfg. */
550 current_edge = ei_start (single_succ (ENTRY_BLOCK_PTR)->succs);
551 sp = -1;
553 while (1)
555 if (EDGE_PASSED (current_edge))
557 /* We have reached a leaf node or a node that was already
558 processed. Pop edges off the stack until we find
559 an edge that has not yet been processed. */
560 while (sp >= 0 && EDGE_PASSED (current_edge))
562 /* Pop entry off the stack. */
563 current_edge = stack[sp--];
564 node = ei_edge (current_edge)->src->index;
565 gcc_assert (node != ENTRY_BLOCK);
566 child = ei_edge (current_edge)->dest->index;
567 gcc_assert (child != EXIT_BLOCK);
568 RESET_BIT (in_stack, child);
569 if (max_hdr[child] >= 0 && TEST_BIT (in_stack, max_hdr[child]))
570 UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
571 ei_next (&current_edge);
574 /* See if have finished the DFS tree traversal. */
575 if (sp < 0 && EDGE_PASSED (current_edge))
576 break;
578 /* Nope, continue the traversal with the popped node. */
579 continue;
582 /* Process a node. */
583 node = ei_edge (current_edge)->src->index;
584 gcc_assert (node != ENTRY_BLOCK);
585 SET_BIT (in_stack, node);
586 dfs_nr[node] = ++count;
588 /* We don't traverse to the exit block. */
589 child = ei_edge (current_edge)->dest->index;
590 if (child == EXIT_BLOCK)
592 SET_EDGE_PASSED (current_edge);
593 ei_next (&current_edge);
594 continue;
597 /* If the successor is in the stack, then we've found a loop.
598 Mark the loop, if it is not a natural loop, then it will
599 be rejected during the second traversal. */
600 if (TEST_BIT (in_stack, child))
602 no_loops = 0;
603 SET_BIT (header, child);
604 UPDATE_LOOP_RELATIONS (node, child);
605 SET_EDGE_PASSED (current_edge);
606 ei_next (&current_edge);
607 continue;
610 /* If the child was already visited, then there is no need to visit
611 it again. Just update the loop relationships and restart
612 with a new edge. */
613 if (dfs_nr[child])
615 if (max_hdr[child] >= 0 && TEST_BIT (in_stack, max_hdr[child]))
616 UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
617 SET_EDGE_PASSED (current_edge);
618 ei_next (&current_edge);
619 continue;
622 /* Push an entry on the stack and continue DFS traversal. */
623 stack[++sp] = current_edge;
624 SET_EDGE_PASSED (current_edge);
625 current_edge = ei_start (ei_edge (current_edge)->dest->succs);
628 /* Reset ->aux field used by EDGE_PASSED. */
629 FOR_ALL_BB (bb)
631 edge_iterator ei;
632 edge e;
633 FOR_EACH_EDGE (e, ei, bb->succs)
634 e->aux = NULL;
638 /* Another check for unreachable blocks. The earlier test in
639 is_cfg_nonregular only finds unreachable blocks that do not
640 form a loop.
642 The DFS traversal will mark every block that is reachable from
643 the entry node by placing a nonzero value in dfs_nr. Thus if
644 dfs_nr is zero for any block, then it must be unreachable. */
645 unreachable = 0;
646 FOR_EACH_BB (bb)
647 if (dfs_nr[bb->index] == 0)
649 unreachable = 1;
650 break;
653 /* Gross. To avoid wasting memory, the second pass uses the dfs_nr array
654 to hold degree counts. */
655 degree = dfs_nr;
657 FOR_EACH_BB (bb)
658 degree[bb->index] = EDGE_COUNT (bb->preds);
660 /* Do not perform region scheduling if there are any unreachable
661 blocks. */
662 if (!unreachable)
664 int *queue, *degree1 = NULL;
665 /* We use EXTENDED_RGN_HEADER as an addition to HEADER and put
666 there basic blocks, which are forced to be region heads.
667 This is done to try to assemble few smaller regions
668 from a too_large region. */
669 sbitmap extended_rgn_header = NULL;
670 bool extend_regions_p;
672 if (no_loops)
673 SET_BIT (header, 0);
675 /* Second traversal:find reducible inner loops and topologically sort
676 block of each region. */
678 queue = XNEWVEC (int, n_basic_blocks);
680 extend_regions_p = PARAM_VALUE (PARAM_MAX_SCHED_EXTEND_REGIONS_ITERS) > 0;
681 if (extend_regions_p)
683 degree1 = xmalloc (last_basic_block * sizeof (int));
684 extended_rgn_header = sbitmap_alloc (last_basic_block);
685 sbitmap_zero (extended_rgn_header);
688 /* Find blocks which are inner loop headers. We still have non-reducible
689 loops to consider at this point. */
690 FOR_EACH_BB (bb)
692 if (TEST_BIT (header, bb->index) && TEST_BIT (inner, bb->index))
694 edge e;
695 edge_iterator ei;
696 basic_block jbb;
698 /* Now check that the loop is reducible. We do this separate
699 from finding inner loops so that we do not find a reducible
700 loop which contains an inner non-reducible loop.
702 A simple way to find reducible/natural loops is to verify
703 that each block in the loop is dominated by the loop
704 header.
706 If there exists a block that is not dominated by the loop
707 header, then the block is reachable from outside the loop
708 and thus the loop is not a natural loop. */
709 FOR_EACH_BB (jbb)
711 /* First identify blocks in the loop, except for the loop
712 entry block. */
713 if (bb->index == max_hdr[jbb->index] && bb != jbb)
715 /* Now verify that the block is dominated by the loop
716 header. */
717 if (!dominated_by_p (CDI_DOMINATORS, jbb, bb))
718 break;
722 /* If we exited the loop early, then I is the header of
723 a non-reducible loop and we should quit processing it
724 now. */
725 if (jbb != EXIT_BLOCK_PTR)
726 continue;
728 /* I is a header of an inner loop, or block 0 in a subroutine
729 with no loops at all. */
730 head = tail = -1;
731 too_large_failure = 0;
732 loop_head = max_hdr[bb->index];
734 if (extend_regions_p)
735 /* We save degree in case when we meet a too_large region
736 and cancel it. We need a correct degree later when
737 calling extend_rgns. */
738 memcpy (degree1, degree, last_basic_block * sizeof (int));
740 /* Decrease degree of all I's successors for topological
741 ordering. */
742 FOR_EACH_EDGE (e, ei, bb->succs)
743 if (e->dest != EXIT_BLOCK_PTR)
744 --degree[e->dest->index];
746 /* Estimate # insns, and count # blocks in the region. */
747 num_bbs = 1;
748 num_insns = (INSN_LUID (BB_END (bb))
749 - INSN_LUID (BB_HEAD (bb)));
751 /* Find all loop latches (blocks with back edges to the loop
752 header) or all the leaf blocks in the cfg has no loops.
754 Place those blocks into the queue. */
755 if (no_loops)
757 FOR_EACH_BB (jbb)
758 /* Leaf nodes have only a single successor which must
759 be EXIT_BLOCK. */
760 if (single_succ_p (jbb)
761 && single_succ (jbb) == EXIT_BLOCK_PTR)
763 queue[++tail] = jbb->index;
764 SET_BIT (in_queue, jbb->index);
766 if (too_large (jbb->index, &num_bbs, &num_insns))
768 too_large_failure = 1;
769 break;
773 else
775 edge e;
777 FOR_EACH_EDGE (e, ei, bb->preds)
779 if (e->src == ENTRY_BLOCK_PTR)
780 continue;
782 node = e->src->index;
784 if (max_hdr[node] == loop_head && node != bb->index)
786 /* This is a loop latch. */
787 queue[++tail] = node;
788 SET_BIT (in_queue, node);
790 if (too_large (node, &num_bbs, &num_insns))
792 too_large_failure = 1;
793 break;
799 /* Now add all the blocks in the loop to the queue.
801 We know the loop is a natural loop; however the algorithm
802 above will not always mark certain blocks as being in the
803 loop. Consider:
804 node children
805 a b,c
807 c a,d
810 The algorithm in the DFS traversal may not mark B & D as part
811 of the loop (i.e. they will not have max_hdr set to A).
813 We know they can not be loop latches (else they would have
814 had max_hdr set since they'd have a backedge to a dominator
815 block). So we don't need them on the initial queue.
817 We know they are part of the loop because they are dominated
818 by the loop header and can be reached by a backwards walk of
819 the edges starting with nodes on the initial queue.
821 It is safe and desirable to include those nodes in the
822 loop/scheduling region. To do so we would need to decrease
823 the degree of a node if it is the target of a backedge
824 within the loop itself as the node is placed in the queue.
826 We do not do this because I'm not sure that the actual
827 scheduling code will properly handle this case. ?!? */
829 while (head < tail && !too_large_failure)
831 edge e;
832 child = queue[++head];
834 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (child)->preds)
836 node = e->src->index;
838 /* See discussion above about nodes not marked as in
839 this loop during the initial DFS traversal. */
840 if (e->src == ENTRY_BLOCK_PTR
841 || max_hdr[node] != loop_head)
843 tail = -1;
844 break;
846 else if (!TEST_BIT (in_queue, node) && node != bb->index)
848 queue[++tail] = node;
849 SET_BIT (in_queue, node);
851 if (too_large (node, &num_bbs, &num_insns))
853 too_large_failure = 1;
854 break;
860 if (tail >= 0 && !too_large_failure)
862 /* Place the loop header into list of region blocks. */
863 degree[bb->index] = -1;
864 rgn_bb_table[idx] = bb->index;
865 RGN_NR_BLOCKS (nr_regions) = num_bbs;
866 RGN_BLOCKS (nr_regions) = idx++;
867 RGN_DONT_CALC_DEPS (nr_regions) = 0;
868 RGN_HAS_REAL_EBB (nr_regions) = 0;
869 CONTAINING_RGN (bb->index) = nr_regions;
870 BLOCK_TO_BB (bb->index) = count = 0;
872 /* Remove blocks from queue[] when their in degree
873 becomes zero. Repeat until no blocks are left on the
874 list. This produces a topological list of blocks in
875 the region. */
876 while (tail >= 0)
878 if (head < 0)
879 head = tail;
880 child = queue[head];
881 if (degree[child] == 0)
883 edge e;
885 degree[child] = -1;
886 rgn_bb_table[idx++] = child;
887 BLOCK_TO_BB (child) = ++count;
888 CONTAINING_RGN (child) = nr_regions;
889 queue[head] = queue[tail--];
891 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (child)->succs)
892 if (e->dest != EXIT_BLOCK_PTR)
893 --degree[e->dest->index];
895 else
896 --head;
898 ++nr_regions;
900 else if (extend_regions_p)
902 /* Restore DEGREE. */
903 int *t = degree;
905 degree = degree1;
906 degree1 = t;
908 /* And force successors of BB to be region heads.
909 This may provide several smaller regions instead
910 of one too_large region. */
911 FOR_EACH_EDGE (e, ei, bb->succs)
912 if (e->dest != EXIT_BLOCK_PTR)
913 SET_BIT (extended_rgn_header, e->dest->index);
917 free (queue);
919 if (extend_regions_p)
921 free (degree1);
923 sbitmap_a_or_b (header, header, extended_rgn_header);
924 sbitmap_free (extended_rgn_header);
926 extend_rgns (degree, &idx, header, max_hdr);
930 /* Any block that did not end up in a region is placed into a region
931 by itself. */
932 FOR_EACH_BB (bb)
933 if (degree[bb->index] >= 0)
935 rgn_bb_table[idx] = bb->index;
936 RGN_NR_BLOCKS (nr_regions) = 1;
937 RGN_BLOCKS (nr_regions) = idx++;
938 RGN_DONT_CALC_DEPS (nr_regions) = 0;
939 RGN_HAS_REAL_EBB (nr_regions) = 0;
940 CONTAINING_RGN (bb->index) = nr_regions++;
941 BLOCK_TO_BB (bb->index) = 0;
944 free (max_hdr);
945 free (degree);
946 free (stack);
947 sbitmap_free (header);
948 sbitmap_free (inner);
949 sbitmap_free (in_queue);
950 sbitmap_free (in_stack);
953 static int gather_region_statistics (int **);
954 static void print_region_statistics (int *, int, int *, int);
956 /* Calculate the histogram that shows the number of regions having the
957 given number of basic blocks, and store it in the RSP array. Return
958 the size of this array. */
959 static int
960 gather_region_statistics (int **rsp)
962 int i, *a = 0, a_sz = 0;
964 /* a[i] is the number of regions that have (i + 1) basic blocks. */
965 for (i = 0; i < nr_regions; i++)
967 int nr_blocks = RGN_NR_BLOCKS (i);
969 gcc_assert (nr_blocks >= 1);
971 if (nr_blocks > a_sz)
973 a = xrealloc (a, nr_blocks * sizeof (*a));
975 a[a_sz++] = 0;
976 while (a_sz != nr_blocks);
979 a[nr_blocks - 1]++;
982 *rsp = a;
983 return a_sz;
986 /* Print regions statistics. S1 and S2 denote the data before and after
987 calling extend_rgns, respectively. */
988 static void
989 print_region_statistics (int *s1, int s1_sz, int *s2, int s2_sz)
991 int i;
993 /* We iterate until s2_sz because extend_rgns does not decrease
994 the maximal region size. */
995 for (i = 1; i < s2_sz; i++)
997 int n1, n2;
999 n2 = s2[i];
1001 if (n2 == 0)
1002 continue;
1004 if (i >= s1_sz)
1005 n1 = 0;
1006 else
1007 n1 = s1[i];
1009 fprintf (sched_dump, ";; Region extension statistics: size %d: " \
1010 "was %d + %d more\n", i + 1, n1, n2 - n1);
1014 /* Extend regions.
1015 DEGREE - Array of incoming edge count, considering only
1016 the edges, that don't have their sources in formed regions yet.
1017 IDXP - pointer to the next available index in rgn_bb_table.
1018 HEADER - set of all region heads.
1019 LOOP_HDR - mapping from block to the containing loop
1020 (two blocks can reside within one region if they have
1021 the same loop header). */
1022 static void
1023 extend_rgns (int *degree, int *idxp, sbitmap header, int *loop_hdr)
1025 int *order, i, rescan = 0, idx = *idxp, iter = 0, max_iter, *max_hdr;
1026 int nblocks = n_basic_blocks - NUM_FIXED_BLOCKS;
1028 max_iter = PARAM_VALUE (PARAM_MAX_SCHED_EXTEND_REGIONS_ITERS);
1030 max_hdr = xmalloc (last_basic_block * sizeof (*max_hdr));
1032 order = xmalloc (last_basic_block * sizeof (*order));
1033 post_order_compute (order, false, false);
1035 for (i = nblocks - 1; i >= 0; i--)
1037 int bbn = order[i];
1038 if (degree[bbn] >= 0)
1040 max_hdr[bbn] = bbn;
1041 rescan = 1;
1043 else
1044 /* This block already was processed in find_rgns. */
1045 max_hdr[bbn] = -1;
1048 /* The idea is to topologically walk through CFG in top-down order.
1049 During the traversal, if all the predecessors of a node are
1050 marked to be in the same region (they all have the same max_hdr),
1051 then current node is also marked to be a part of that region.
1052 Otherwise the node starts its own region.
1053 CFG should be traversed until no further changes are made. On each
1054 iteration the set of the region heads is extended (the set of those
1055 blocks that have max_hdr[bbi] == bbi). This set is upper bounded by the
1056 set of all basic blocks, thus the algorithm is guaranteed to terminate. */
1058 while (rescan && iter < max_iter)
1060 rescan = 0;
1062 for (i = nblocks - 1; i >= 0; i--)
1064 edge e;
1065 edge_iterator ei;
1066 int bbn = order[i];
1068 if (max_hdr[bbn] != -1 && !TEST_BIT (header, bbn))
1070 int hdr = -1;
1072 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (bbn)->preds)
1074 int predn = e->src->index;
1076 if (predn != ENTRY_BLOCK
1077 /* If pred wasn't processed in find_rgns. */
1078 && max_hdr[predn] != -1
1079 /* And pred and bb reside in the same loop.
1080 (Or out of any loop). */
1081 && loop_hdr[bbn] == loop_hdr[predn])
1083 if (hdr == -1)
1084 /* Then bb extends the containing region of pred. */
1085 hdr = max_hdr[predn];
1086 else if (hdr != max_hdr[predn])
1087 /* Too bad, there are at least two predecessors
1088 that reside in different regions. Thus, BB should
1089 begin its own region. */
1091 hdr = bbn;
1092 break;
1095 else
1096 /* BB starts its own region. */
1098 hdr = bbn;
1099 break;
1103 if (hdr == bbn)
1105 /* If BB start its own region,
1106 update set of headers with BB. */
1107 SET_BIT (header, bbn);
1108 rescan = 1;
1110 else
1111 gcc_assert (hdr != -1);
1113 max_hdr[bbn] = hdr;
1117 iter++;
1120 /* Statistics were gathered on the SPEC2000 package of tests with
1121 mainline weekly snapshot gcc-4.1-20051015 on ia64.
1123 Statistics for SPECint:
1124 1 iteration : 1751 cases (38.7%)
1125 2 iterations: 2770 cases (61.3%)
1126 Blocks wrapped in regions by find_rgns without extension: 18295 blocks
1127 Blocks wrapped in regions by 2 iterations in extend_rgns: 23821 blocks
1128 (We don't count single block regions here).
1130 Statistics for SPECfp:
1131 1 iteration : 621 cases (35.9%)
1132 2 iterations: 1110 cases (64.1%)
1133 Blocks wrapped in regions by find_rgns without extension: 6476 blocks
1134 Blocks wrapped in regions by 2 iterations in extend_rgns: 11155 blocks
1135 (We don't count single block regions here).
1137 By default we do at most 2 iterations.
1138 This can be overridden with max-sched-extend-regions-iters parameter:
1139 0 - disable region extension,
1140 N > 0 - do at most N iterations. */
1142 if (sched_verbose && iter != 0)
1143 fprintf (sched_dump, ";; Region extension iterations: %d%s\n", iter,
1144 rescan ? "... failed" : "");
1146 if (!rescan && iter != 0)
1148 int *s1 = NULL, s1_sz = 0;
1150 /* Save the old statistics for later printout. */
1151 if (sched_verbose >= 6)
1152 s1_sz = gather_region_statistics (&s1);
1154 /* We have succeeded. Now assemble the regions. */
1155 for (i = nblocks - 1; i >= 0; i--)
1157 int bbn = order[i];
1159 if (max_hdr[bbn] == bbn)
1160 /* BBN is a region head. */
1162 edge e;
1163 edge_iterator ei;
1164 int num_bbs = 0, j, num_insns = 0, large;
1166 large = too_large (bbn, &num_bbs, &num_insns);
1168 degree[bbn] = -1;
1169 rgn_bb_table[idx] = bbn;
1170 RGN_BLOCKS (nr_regions) = idx++;
1171 RGN_DONT_CALC_DEPS (nr_regions) = 0;
1172 RGN_HAS_REAL_EBB (nr_regions) = 0;
1173 CONTAINING_RGN (bbn) = nr_regions;
1174 BLOCK_TO_BB (bbn) = 0;
1176 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (bbn)->succs)
1177 if (e->dest != EXIT_BLOCK_PTR)
1178 degree[e->dest->index]--;
1180 if (!large)
1181 /* Here we check whether the region is too_large. */
1182 for (j = i - 1; j >= 0; j--)
1184 int succn = order[j];
1185 if (max_hdr[succn] == bbn)
1187 if ((large = too_large (succn, &num_bbs, &num_insns)))
1188 break;
1192 if (large)
1193 /* If the region is too_large, then wrap every block of
1194 the region into single block region.
1195 Here we wrap region head only. Other blocks are
1196 processed in the below cycle. */
1198 RGN_NR_BLOCKS (nr_regions) = 1;
1199 nr_regions++;
1202 num_bbs = 1;
1204 for (j = i - 1; j >= 0; j--)
1206 int succn = order[j];
1208 if (max_hdr[succn] == bbn)
1209 /* This cycle iterates over all basic blocks, that
1210 are supposed to be in the region with head BBN,
1211 and wraps them into that region (or in single
1212 block region). */
1214 gcc_assert (degree[succn] == 0);
1216 degree[succn] = -1;
1217 rgn_bb_table[idx] = succn;
1218 BLOCK_TO_BB (succn) = large ? 0 : num_bbs++;
1219 CONTAINING_RGN (succn) = nr_regions;
1221 if (large)
1222 /* Wrap SUCCN into single block region. */
1224 RGN_BLOCKS (nr_regions) = idx;
1225 RGN_NR_BLOCKS (nr_regions) = 1;
1226 RGN_DONT_CALC_DEPS (nr_regions) = 0;
1227 RGN_HAS_REAL_EBB (nr_regions) = 0;
1228 nr_regions++;
1231 idx++;
1233 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (succn)->succs)
1234 if (e->dest != EXIT_BLOCK_PTR)
1235 degree[e->dest->index]--;
1239 if (!large)
1241 RGN_NR_BLOCKS (nr_regions) = num_bbs;
1242 nr_regions++;
1247 if (sched_verbose >= 6)
1249 int *s2, s2_sz;
1251 /* Get the new statistics and print the comparison with the
1252 one before calling this function. */
1253 s2_sz = gather_region_statistics (&s2);
1254 print_region_statistics (s1, s1_sz, s2, s2_sz);
1255 free (s1);
1256 free (s2);
1260 free (order);
1261 free (max_hdr);
1263 *idxp = idx;
1266 /* Functions for regions scheduling information. */
1268 /* Compute dominators, probability, and potential-split-edges of bb.
1269 Assume that these values were already computed for bb's predecessors. */
1271 static void
1272 compute_dom_prob_ps (int bb)
1274 edge_iterator in_ei;
1275 edge in_edge;
1277 /* We shouldn't have any real ebbs yet. */
1278 gcc_assert (ebb_head [bb] == bb + current_blocks);
1280 if (IS_RGN_ENTRY (bb))
1282 SET_BIT (dom[bb], 0);
1283 prob[bb] = REG_BR_PROB_BASE;
1284 return;
1287 prob[bb] = 0;
1289 /* Initialize dom[bb] to '111..1'. */
1290 sbitmap_ones (dom[bb]);
1292 FOR_EACH_EDGE (in_edge, in_ei, BASIC_BLOCK (BB_TO_BLOCK (bb))->preds)
1294 int pred_bb;
1295 edge out_edge;
1296 edge_iterator out_ei;
1298 if (in_edge->src == ENTRY_BLOCK_PTR)
1299 continue;
1301 pred_bb = BLOCK_TO_BB (in_edge->src->index);
1302 sbitmap_a_and_b (dom[bb], dom[bb], dom[pred_bb]);
1303 sbitmap_a_or_b (ancestor_edges[bb],
1304 ancestor_edges[bb], ancestor_edges[pred_bb]);
1306 SET_BIT (ancestor_edges[bb], EDGE_TO_BIT (in_edge));
1308 sbitmap_a_or_b (pot_split[bb], pot_split[bb], pot_split[pred_bb]);
1310 FOR_EACH_EDGE (out_edge, out_ei, in_edge->src->succs)
1311 SET_BIT (pot_split[bb], EDGE_TO_BIT (out_edge));
1313 prob[bb] += ((prob[pred_bb] * in_edge->probability) / REG_BR_PROB_BASE);
1316 SET_BIT (dom[bb], bb);
1317 sbitmap_difference (pot_split[bb], pot_split[bb], ancestor_edges[bb]);
1319 if (sched_verbose >= 2)
1320 fprintf (sched_dump, ";; bb_prob(%d, %d) = %3d\n", bb, BB_TO_BLOCK (bb),
1321 (100 * prob[bb]) / REG_BR_PROB_BASE);
1324 /* Functions for target info. */
1326 /* Compute in BL the list of split-edges of bb_src relatively to bb_trg.
1327 Note that bb_trg dominates bb_src. */
1329 static void
1330 split_edges (int bb_src, int bb_trg, edgelst *bl)
1332 sbitmap src = sbitmap_alloc (pot_split[bb_src]->n_bits);
1333 sbitmap_copy (src, pot_split[bb_src]);
1335 sbitmap_difference (src, src, pot_split[bb_trg]);
1336 extract_edgelst (src, bl);
1337 sbitmap_free (src);
1340 /* Find the valid candidate-source-blocks for the target block TRG, compute
1341 their probability, and check if they are speculative or not.
1342 For speculative sources, compute their update-blocks and split-blocks. */
1344 static void
1345 compute_trg_info (int trg)
1347 candidate *sp;
1348 edgelst el;
1349 int i, j, k, update_idx;
1350 basic_block block;
1351 sbitmap visited;
1352 edge_iterator ei;
1353 edge e;
1355 /* Define some of the fields for the target bb as well. */
1356 sp = candidate_table + trg;
1357 sp->is_valid = 1;
1358 sp->is_speculative = 0;
1359 sp->src_prob = REG_BR_PROB_BASE;
1361 visited = sbitmap_alloc (last_basic_block);
1363 for (i = trg + 1; i < current_nr_blocks; i++)
1365 sp = candidate_table + i;
1367 sp->is_valid = IS_DOMINATED (i, trg);
1368 if (sp->is_valid)
1370 int tf = prob[trg], cf = prob[i];
1372 /* In CFGs with low probability edges TF can possibly be zero. */
1373 sp->src_prob = (tf ? ((cf * REG_BR_PROB_BASE) / tf) : 0);
1374 sp->is_valid = (sp->src_prob >= min_spec_prob);
1377 if (sp->is_valid)
1379 split_edges (i, trg, &el);
1380 sp->is_speculative = (el.nr_members) ? 1 : 0;
1381 if (sp->is_speculative && !flag_schedule_speculative)
1382 sp->is_valid = 0;
1385 if (sp->is_valid)
1387 /* Compute split blocks and store them in bblst_table.
1388 The TO block of every split edge is a split block. */
1389 sp->split_bbs.first_member = &bblst_table[bblst_last];
1390 sp->split_bbs.nr_members = el.nr_members;
1391 for (j = 0; j < el.nr_members; bblst_last++, j++)
1392 bblst_table[bblst_last] = el.first_member[j]->dest;
1393 sp->update_bbs.first_member = &bblst_table[bblst_last];
1395 /* Compute update blocks and store them in bblst_table.
1396 For every split edge, look at the FROM block, and check
1397 all out edges. For each out edge that is not a split edge,
1398 add the TO block to the update block list. This list can end
1399 up with a lot of duplicates. We need to weed them out to avoid
1400 overrunning the end of the bblst_table. */
1402 update_idx = 0;
1403 sbitmap_zero (visited);
1404 for (j = 0; j < el.nr_members; j++)
1406 block = el.first_member[j]->src;
1407 FOR_EACH_EDGE (e, ei, block->succs)
1409 if (!TEST_BIT (visited, e->dest->index))
1411 for (k = 0; k < el.nr_members; k++)
1412 if (e == el.first_member[k])
1413 break;
1415 if (k >= el.nr_members)
1417 bblst_table[bblst_last++] = e->dest;
1418 SET_BIT (visited, e->dest->index);
1419 update_idx++;
1424 sp->update_bbs.nr_members = update_idx;
1426 /* Make sure we didn't overrun the end of bblst_table. */
1427 gcc_assert (bblst_last <= bblst_size);
1429 else
1431 sp->split_bbs.nr_members = sp->update_bbs.nr_members = 0;
1433 sp->is_speculative = 0;
1434 sp->src_prob = 0;
1438 sbitmap_free (visited);
1441 /* Print candidates info, for debugging purposes. Callable from debugger. */
1443 void
1444 debug_candidate (int i)
1446 if (!candidate_table[i].is_valid)
1447 return;
1449 if (candidate_table[i].is_speculative)
1451 int j;
1452 fprintf (sched_dump, "src b %d bb %d speculative \n", BB_TO_BLOCK (i), i);
1454 fprintf (sched_dump, "split path: ");
1455 for (j = 0; j < candidate_table[i].split_bbs.nr_members; j++)
1457 int b = candidate_table[i].split_bbs.first_member[j]->index;
1459 fprintf (sched_dump, " %d ", b);
1461 fprintf (sched_dump, "\n");
1463 fprintf (sched_dump, "update path: ");
1464 for (j = 0; j < candidate_table[i].update_bbs.nr_members; j++)
1466 int b = candidate_table[i].update_bbs.first_member[j]->index;
1468 fprintf (sched_dump, " %d ", b);
1470 fprintf (sched_dump, "\n");
1472 else
1474 fprintf (sched_dump, " src %d equivalent\n", BB_TO_BLOCK (i));
1478 /* Print candidates info, for debugging purposes. Callable from debugger. */
1480 void
1481 debug_candidates (int trg)
1483 int i;
1485 fprintf (sched_dump, "----------- candidate table: target: b=%d bb=%d ---\n",
1486 BB_TO_BLOCK (trg), trg);
1487 for (i = trg + 1; i < current_nr_blocks; i++)
1488 debug_candidate (i);
1491 /* Functions for speculative scheduling. */
1493 static bitmap_head not_in_df;
1495 /* Return 0 if x is a set of a register alive in the beginning of one
1496 of the split-blocks of src, otherwise return 1. */
1498 static int
1499 check_live_1 (int src, rtx x)
1501 int i;
1502 int regno;
1503 rtx reg = SET_DEST (x);
1505 if (reg == 0)
1506 return 1;
1508 while (GET_CODE (reg) == SUBREG
1509 || GET_CODE (reg) == ZERO_EXTRACT
1510 || GET_CODE (reg) == STRICT_LOW_PART)
1511 reg = XEXP (reg, 0);
1513 if (GET_CODE (reg) == PARALLEL)
1515 int i;
1517 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
1518 if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
1519 if (check_live_1 (src, XEXP (XVECEXP (reg, 0, i), 0)))
1520 return 1;
1522 return 0;
1525 if (!REG_P (reg))
1526 return 1;
1528 regno = REGNO (reg);
1530 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
1532 /* Global registers are assumed live. */
1533 return 0;
1535 else
1537 if (regno < FIRST_PSEUDO_REGISTER)
1539 /* Check for hard registers. */
1540 int j = hard_regno_nregs[regno][GET_MODE (reg)];
1541 while (--j >= 0)
1543 for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
1545 basic_block b = candidate_table[src].split_bbs.first_member[i];
1546 int t = bitmap_bit_p (&not_in_df, b->index);
1548 /* We can have split blocks, that were recently generated.
1549 such blocks are always outside current region. */
1550 gcc_assert (!t || (CONTAINING_RGN (b->index)
1551 != CONTAINING_RGN (BB_TO_BLOCK (src))));
1553 if (t || REGNO_REG_SET_P (df_get_live_in (b), regno + j))
1554 return 0;
1558 else
1560 /* Check for pseudo registers. */
1561 for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
1563 basic_block b = candidate_table[src].split_bbs.first_member[i];
1564 int t = bitmap_bit_p (&not_in_df, b->index);
1566 gcc_assert (!t || (CONTAINING_RGN (b->index)
1567 != CONTAINING_RGN (BB_TO_BLOCK (src))));
1569 if (t || REGNO_REG_SET_P (df_get_live_in (b), regno))
1570 return 0;
1575 return 1;
1578 /* If x is a set of a register R, mark that R is alive in the beginning
1579 of every update-block of src. */
1581 static void
1582 update_live_1 (int src, rtx x)
1584 int i;
1585 int regno;
1586 rtx reg = SET_DEST (x);
1588 if (reg == 0)
1589 return;
1591 while (GET_CODE (reg) == SUBREG
1592 || GET_CODE (reg) == ZERO_EXTRACT
1593 || GET_CODE (reg) == STRICT_LOW_PART)
1594 reg = XEXP (reg, 0);
1596 if (GET_CODE (reg) == PARALLEL)
1598 int i;
1600 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
1601 if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
1602 update_live_1 (src, XEXP (XVECEXP (reg, 0, i), 0));
1604 return;
1607 if (!REG_P (reg))
1608 return;
1610 /* Global registers are always live, so the code below does not apply
1611 to them. */
1613 regno = REGNO (reg);
1615 if (regno >= FIRST_PSEUDO_REGISTER || !global_regs[regno])
1617 if (regno < FIRST_PSEUDO_REGISTER)
1619 int j = hard_regno_nregs[regno][GET_MODE (reg)];
1620 while (--j >= 0)
1622 for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
1624 basic_block b = candidate_table[src].update_bbs.first_member[i];
1626 SET_REGNO_REG_SET (df_get_live_in (b), regno + j);
1630 else
1632 for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
1634 basic_block b = candidate_table[src].update_bbs.first_member[i];
1636 SET_REGNO_REG_SET (df_get_live_in (b), regno);
1642 /* Return 1 if insn can be speculatively moved from block src to trg,
1643 otherwise return 0. Called before first insertion of insn to
1644 ready-list or before the scheduling. */
1646 static int
1647 check_live (rtx insn, int src)
1649 /* Find the registers set by instruction. */
1650 if (GET_CODE (PATTERN (insn)) == SET
1651 || GET_CODE (PATTERN (insn)) == CLOBBER)
1652 return check_live_1 (src, PATTERN (insn));
1653 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1655 int j;
1656 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
1657 if ((GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
1658 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
1659 && !check_live_1 (src, XVECEXP (PATTERN (insn), 0, j)))
1660 return 0;
1662 return 1;
1665 return 1;
1668 /* Update the live registers info after insn was moved speculatively from
1669 block src to trg. */
1671 static void
1672 update_live (rtx insn, int src)
1674 /* Find the registers set by instruction. */
1675 if (GET_CODE (PATTERN (insn)) == SET
1676 || GET_CODE (PATTERN (insn)) == CLOBBER)
1677 update_live_1 (src, PATTERN (insn));
1678 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1680 int j;
1681 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
1682 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
1683 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
1684 update_live_1 (src, XVECEXP (PATTERN (insn), 0, j));
1688 /* Nonzero if block bb_to is equal to, or reachable from block bb_from. */
1689 #define IS_REACHABLE(bb_from, bb_to) \
1690 (bb_from == bb_to \
1691 || IS_RGN_ENTRY (bb_from) \
1692 || (TEST_BIT (ancestor_edges[bb_to], \
1693 EDGE_TO_BIT (single_pred_edge (BASIC_BLOCK (BB_TO_BLOCK (bb_from)))))))
1695 /* Turns on the fed_by_spec_load flag for insns fed by load_insn. */
1697 static void
1698 set_spec_fed (rtx load_insn)
1700 sd_iterator_def sd_it;
1701 dep_t dep;
1703 FOR_EACH_DEP (load_insn, SD_LIST_FORW, sd_it, dep)
1704 if (DEP_TYPE (dep) == REG_DEP_TRUE)
1705 FED_BY_SPEC_LOAD (DEP_CON (dep)) = 1;
1708 /* On the path from the insn to load_insn_bb, find a conditional
1709 branch depending on insn, that guards the speculative load. */
1711 static int
1712 find_conditional_protection (rtx insn, int load_insn_bb)
1714 sd_iterator_def sd_it;
1715 dep_t dep;
1717 /* Iterate through DEF-USE forward dependences. */
1718 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
1720 rtx next = DEP_CON (dep);
1722 if ((CONTAINING_RGN (BLOCK_NUM (next)) ==
1723 CONTAINING_RGN (BB_TO_BLOCK (load_insn_bb)))
1724 && IS_REACHABLE (INSN_BB (next), load_insn_bb)
1725 && load_insn_bb != INSN_BB (next)
1726 && DEP_TYPE (dep) == REG_DEP_TRUE
1727 && (JUMP_P (next)
1728 || find_conditional_protection (next, load_insn_bb)))
1729 return 1;
1731 return 0;
1732 } /* find_conditional_protection */
1734 /* Returns 1 if the same insn1 that participates in the computation
1735 of load_insn's address is feeding a conditional branch that is
1736 guarding on load_insn. This is true if we find a the two DEF-USE
1737 chains:
1738 insn1 -> ... -> conditional-branch
1739 insn1 -> ... -> load_insn,
1740 and if a flow path exist:
1741 insn1 -> ... -> conditional-branch -> ... -> load_insn,
1742 and if insn1 is on the path
1743 region-entry -> ... -> bb_trg -> ... load_insn.
1745 Locate insn1 by climbing on INSN_BACK_DEPS from load_insn.
1746 Locate the branch by following INSN_FORW_DEPS from insn1. */
1748 static int
1749 is_conditionally_protected (rtx load_insn, int bb_src, int bb_trg)
1751 sd_iterator_def sd_it;
1752 dep_t dep;
1754 FOR_EACH_DEP (load_insn, SD_LIST_BACK, sd_it, dep)
1756 rtx insn1 = DEP_PRO (dep);
1758 /* Must be a DEF-USE dependence upon non-branch. */
1759 if (DEP_TYPE (dep) != REG_DEP_TRUE
1760 || JUMP_P (insn1))
1761 continue;
1763 /* Must exist a path: region-entry -> ... -> bb_trg -> ... load_insn. */
1764 if (INSN_BB (insn1) == bb_src
1765 || (CONTAINING_RGN (BLOCK_NUM (insn1))
1766 != CONTAINING_RGN (BB_TO_BLOCK (bb_src)))
1767 || (!IS_REACHABLE (bb_trg, INSN_BB (insn1))
1768 && !IS_REACHABLE (INSN_BB (insn1), bb_trg)))
1769 continue;
1771 /* Now search for the conditional-branch. */
1772 if (find_conditional_protection (insn1, bb_src))
1773 return 1;
1775 /* Recursive step: search another insn1, "above" current insn1. */
1776 return is_conditionally_protected (insn1, bb_src, bb_trg);
1779 /* The chain does not exist. */
1780 return 0;
1781 } /* is_conditionally_protected */
1783 /* Returns 1 if a clue for "similar load" 'insn2' is found, and hence
1784 load_insn can move speculatively from bb_src to bb_trg. All the
1785 following must hold:
1787 (1) both loads have 1 base register (PFREE_CANDIDATEs).
1788 (2) load_insn and load1 have a def-use dependence upon
1789 the same insn 'insn1'.
1790 (3) either load2 is in bb_trg, or:
1791 - there's only one split-block, and
1792 - load1 is on the escape path, and
1794 From all these we can conclude that the two loads access memory
1795 addresses that differ at most by a constant, and hence if moving
1796 load_insn would cause an exception, it would have been caused by
1797 load2 anyhow. */
1799 static int
1800 is_pfree (rtx load_insn, int bb_src, int bb_trg)
1802 sd_iterator_def back_sd_it;
1803 dep_t back_dep;
1804 candidate *candp = candidate_table + bb_src;
1806 if (candp->split_bbs.nr_members != 1)
1807 /* Must have exactly one escape block. */
1808 return 0;
1810 FOR_EACH_DEP (load_insn, SD_LIST_BACK, back_sd_it, back_dep)
1812 rtx insn1 = DEP_PRO (back_dep);
1814 if (DEP_TYPE (back_dep) == REG_DEP_TRUE)
1815 /* Found a DEF-USE dependence (insn1, load_insn). */
1817 sd_iterator_def fore_sd_it;
1818 dep_t fore_dep;
1820 FOR_EACH_DEP (insn1, SD_LIST_FORW, fore_sd_it, fore_dep)
1822 rtx insn2 = DEP_CON (fore_dep);
1824 if (DEP_TYPE (fore_dep) == REG_DEP_TRUE)
1826 /* Found a DEF-USE dependence (insn1, insn2). */
1827 if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
1828 /* insn2 not guaranteed to be a 1 base reg load. */
1829 continue;
1831 if (INSN_BB (insn2) == bb_trg)
1832 /* insn2 is the similar load, in the target block. */
1833 return 1;
1835 if (*(candp->split_bbs.first_member) == BLOCK_FOR_INSN (insn2))
1836 /* insn2 is a similar load, in a split-block. */
1837 return 1;
1843 /* Couldn't find a similar load. */
1844 return 0;
1845 } /* is_pfree */
1847 /* Return 1 if load_insn is prisky (i.e. if load_insn is fed by
1848 a load moved speculatively, or if load_insn is protected by
1849 a compare on load_insn's address). */
1851 static int
1852 is_prisky (rtx load_insn, int bb_src, int bb_trg)
1854 if (FED_BY_SPEC_LOAD (load_insn))
1855 return 1;
1857 if (sd_lists_empty_p (load_insn, SD_LIST_BACK))
1858 /* Dependence may 'hide' out of the region. */
1859 return 1;
1861 if (is_conditionally_protected (load_insn, bb_src, bb_trg))
1862 return 1;
1864 return 0;
1867 /* Insn is a candidate to be moved speculatively from bb_src to bb_trg.
1868 Return 1 if insn is exception-free (and the motion is valid)
1869 and 0 otherwise. */
1871 static int
1872 is_exception_free (rtx insn, int bb_src, int bb_trg)
1874 int insn_class = haifa_classify_insn (insn);
1876 /* Handle non-load insns. */
1877 switch (insn_class)
1879 case TRAP_FREE:
1880 return 1;
1881 case TRAP_RISKY:
1882 return 0;
1883 default:;
1886 /* Handle loads. */
1887 if (!flag_schedule_speculative_load)
1888 return 0;
1889 IS_LOAD_INSN (insn) = 1;
1890 switch (insn_class)
1892 case IFREE:
1893 return (1);
1894 case IRISKY:
1895 return 0;
1896 case PFREE_CANDIDATE:
1897 if (is_pfree (insn, bb_src, bb_trg))
1898 return 1;
1899 /* Don't 'break' here: PFREE-candidate is also PRISKY-candidate. */
1900 case PRISKY_CANDIDATE:
1901 if (!flag_schedule_speculative_load_dangerous
1902 || is_prisky (insn, bb_src, bb_trg))
1903 return 0;
1904 break;
1905 default:;
1908 return flag_schedule_speculative_load_dangerous;
1911 /* The number of insns from the current block scheduled so far. */
1912 static int sched_target_n_insns;
1913 /* The number of insns from the current block to be scheduled in total. */
1914 static int target_n_insns;
1915 /* The number of insns from the entire region scheduled so far. */
1916 static int sched_n_insns;
1918 /* Implementations of the sched_info functions for region scheduling. */
1919 static void init_ready_list (void);
1920 static int can_schedule_ready_p (rtx);
1921 static void begin_schedule_ready (rtx, rtx);
1922 static ds_t new_ready (rtx, ds_t);
1923 static int schedule_more_p (void);
1924 static const char *rgn_print_insn (rtx, int);
1925 static int rgn_rank (rtx, rtx);
1926 static int contributes_to_priority (rtx, rtx);
1927 static void compute_jump_reg_dependencies (rtx, regset, regset, regset);
1929 /* Functions for speculative scheduling. */
1930 static void add_remove_insn (rtx, int);
1931 static void extend_regions (void);
1932 static void add_block1 (basic_block, basic_block);
1933 static void fix_recovery_cfg (int, int, int);
1934 static basic_block advance_target_bb (basic_block, rtx);
1936 static void debug_rgn_dependencies (int);
1938 /* Return nonzero if there are more insns that should be scheduled. */
1940 static int
1941 schedule_more_p (void)
1943 return sched_target_n_insns < target_n_insns;
1946 /* Add all insns that are initially ready to the ready list READY. Called
1947 once before scheduling a set of insns. */
1949 static void
1950 init_ready_list (void)
1952 rtx prev_head = current_sched_info->prev_head;
1953 rtx next_tail = current_sched_info->next_tail;
1954 int bb_src;
1955 rtx insn;
1957 target_n_insns = 0;
1958 sched_target_n_insns = 0;
1959 sched_n_insns = 0;
1961 /* Print debugging information. */
1962 if (sched_verbose >= 5)
1963 debug_rgn_dependencies (target_bb);
1965 /* Prepare current target block info. */
1966 if (current_nr_blocks > 1)
1968 candidate_table = XNEWVEC (candidate, current_nr_blocks);
1970 bblst_last = 0;
1971 /* bblst_table holds split blocks and update blocks for each block after
1972 the current one in the region. split blocks and update blocks are
1973 the TO blocks of region edges, so there can be at most rgn_nr_edges
1974 of them. */
1975 bblst_size = (current_nr_blocks - target_bb) * rgn_nr_edges;
1976 bblst_table = XNEWVEC (basic_block, bblst_size);
1978 edgelst_last = 0;
1979 edgelst_table = XNEWVEC (edge, rgn_nr_edges);
1981 compute_trg_info (target_bb);
1984 /* Initialize ready list with all 'ready' insns in target block.
1985 Count number of insns in the target block being scheduled. */
1986 for (insn = NEXT_INSN (prev_head); insn != next_tail; insn = NEXT_INSN (insn))
1988 try_ready (insn);
1989 target_n_insns++;
1991 gcc_assert (!(TODO_SPEC (insn) & BEGIN_CONTROL));
1994 /* Add to ready list all 'ready' insns in valid source blocks.
1995 For speculative insns, check-live, exception-free, and
1996 issue-delay. */
1997 for (bb_src = target_bb + 1; bb_src < current_nr_blocks; bb_src++)
1998 if (IS_VALID (bb_src))
2000 rtx src_head;
2001 rtx src_next_tail;
2002 rtx tail, head;
2004 get_ebb_head_tail (EBB_FIRST_BB (bb_src), EBB_LAST_BB (bb_src),
2005 &head, &tail);
2006 src_next_tail = NEXT_INSN (tail);
2007 src_head = head;
2009 for (insn = src_head; insn != src_next_tail; insn = NEXT_INSN (insn))
2010 if (INSN_P (insn))
2011 try_ready (insn);
2015 /* Called after taking INSN from the ready list. Returns nonzero if this
2016 insn can be scheduled, nonzero if we should silently discard it. */
2018 static int
2019 can_schedule_ready_p (rtx insn)
2021 /* An interblock motion? */
2022 if (INSN_BB (insn) != target_bb
2023 && IS_SPECULATIVE_INSN (insn)
2024 && !check_live (insn, INSN_BB (insn)))
2025 return 0;
2026 else
2027 return 1;
2030 /* Updates counter and other information. Split from can_schedule_ready_p ()
2031 because when we schedule insn speculatively then insn passed to
2032 can_schedule_ready_p () differs from the one passed to
2033 begin_schedule_ready (). */
2034 static void
2035 begin_schedule_ready (rtx insn, rtx last ATTRIBUTE_UNUSED)
2037 /* An interblock motion? */
2038 if (INSN_BB (insn) != target_bb)
2040 if (IS_SPECULATIVE_INSN (insn))
2042 gcc_assert (check_live (insn, INSN_BB (insn)));
2044 update_live (insn, INSN_BB (insn));
2046 /* For speculative load, mark insns fed by it. */
2047 if (IS_LOAD_INSN (insn) || FED_BY_SPEC_LOAD (insn))
2048 set_spec_fed (insn);
2050 nr_spec++;
2052 nr_inter++;
2054 else
2056 /* In block motion. */
2057 sched_target_n_insns++;
2059 sched_n_insns++;
2062 /* Called after INSN has all its hard dependencies resolved and the speculation
2063 of type TS is enough to overcome them all.
2064 Return nonzero if it should be moved to the ready list or the queue, or zero
2065 if we should silently discard it. */
2066 static ds_t
2067 new_ready (rtx next, ds_t ts)
2069 if (INSN_BB (next) != target_bb)
2071 int not_ex_free = 0;
2073 /* For speculative insns, before inserting to ready/queue,
2074 check live, exception-free, and issue-delay. */
2075 if (!IS_VALID (INSN_BB (next))
2076 || CANT_MOVE (next)
2077 || (IS_SPECULATIVE_INSN (next)
2078 && ((recog_memoized (next) >= 0
2079 && min_insn_conflict_delay (curr_state, next, next)
2080 > PARAM_VALUE (PARAM_MAX_SCHED_INSN_CONFLICT_DELAY))
2081 || IS_SPECULATION_CHECK_P (next)
2082 || !check_live (next, INSN_BB (next))
2083 || (not_ex_free = !is_exception_free (next, INSN_BB (next),
2084 target_bb)))))
2086 if (not_ex_free
2087 /* We are here because is_exception_free () == false.
2088 But we possibly can handle that with control speculation. */
2089 && (current_sched_info->flags & DO_SPECULATION)
2090 && (spec_info->mask & BEGIN_CONTROL))
2091 /* Here we got new control-speculative instruction. */
2092 ts = set_dep_weak (ts, BEGIN_CONTROL, MAX_DEP_WEAK);
2093 else
2094 ts = (ts & ~SPECULATIVE) | HARD_DEP;
2098 return ts;
2101 /* Return a string that contains the insn uid and optionally anything else
2102 necessary to identify this insn in an output. It's valid to use a
2103 static buffer for this. The ALIGNED parameter should cause the string
2104 to be formatted so that multiple output lines will line up nicely. */
2106 static const char *
2107 rgn_print_insn (rtx insn, int aligned)
2109 static char tmp[80];
2111 if (aligned)
2112 sprintf (tmp, "b%3d: i%4d", INSN_BB (insn), INSN_UID (insn));
2113 else
2115 if (current_nr_blocks > 1 && INSN_BB (insn) != target_bb)
2116 sprintf (tmp, "%d/b%d", INSN_UID (insn), INSN_BB (insn));
2117 else
2118 sprintf (tmp, "%d", INSN_UID (insn));
2120 return tmp;
2123 /* Compare priority of two insns. Return a positive number if the second
2124 insn is to be preferred for scheduling, and a negative one if the first
2125 is to be preferred. Zero if they are equally good. */
2127 static int
2128 rgn_rank (rtx insn1, rtx insn2)
2130 /* Some comparison make sense in interblock scheduling only. */
2131 if (INSN_BB (insn1) != INSN_BB (insn2))
2133 int spec_val, prob_val;
2135 /* Prefer an inblock motion on an interblock motion. */
2136 if ((INSN_BB (insn2) == target_bb) && (INSN_BB (insn1) != target_bb))
2137 return 1;
2138 if ((INSN_BB (insn1) == target_bb) && (INSN_BB (insn2) != target_bb))
2139 return -1;
2141 /* Prefer a useful motion on a speculative one. */
2142 spec_val = IS_SPECULATIVE_INSN (insn1) - IS_SPECULATIVE_INSN (insn2);
2143 if (spec_val)
2144 return spec_val;
2146 /* Prefer a more probable (speculative) insn. */
2147 prob_val = INSN_PROBABILITY (insn2) - INSN_PROBABILITY (insn1);
2148 if (prob_val)
2149 return prob_val;
2151 return 0;
2154 /* NEXT is an instruction that depends on INSN (a backward dependence);
2155 return nonzero if we should include this dependence in priority
2156 calculations. */
2158 static int
2159 contributes_to_priority (rtx next, rtx insn)
2161 /* NEXT and INSN reside in one ebb. */
2162 return BLOCK_TO_BB (BLOCK_NUM (next)) == BLOCK_TO_BB (BLOCK_NUM (insn));
2165 /* INSN is a JUMP_INSN, COND_SET is the set of registers that are
2166 conditionally set before INSN. Store the set of registers that
2167 must be considered as used by this jump in USED and that of
2168 registers that must be considered as set in SET. */
2170 static void
2171 compute_jump_reg_dependencies (rtx insn ATTRIBUTE_UNUSED,
2172 regset cond_exec ATTRIBUTE_UNUSED,
2173 regset used ATTRIBUTE_UNUSED,
2174 regset set ATTRIBUTE_UNUSED)
2176 /* Nothing to do here, since we postprocess jumps in
2177 add_branch_dependences. */
2180 /* Used in schedule_insns to initialize current_sched_info for scheduling
2181 regions (or single basic blocks). */
2183 static struct sched_info region_sched_info =
2185 init_ready_list,
2186 can_schedule_ready_p,
2187 schedule_more_p,
2188 new_ready,
2189 rgn_rank,
2190 rgn_print_insn,
2191 contributes_to_priority,
2192 compute_jump_reg_dependencies,
2194 NULL, NULL,
2195 NULL, NULL,
2196 0, 0, 0,
2198 add_remove_insn,
2199 begin_schedule_ready,
2200 add_block1,
2201 advance_target_bb,
2202 fix_recovery_cfg,
2203 SCHED_RGN
2206 /* Determine if PAT sets a CLASS_LIKELY_SPILLED_P register. */
2208 static bool
2209 sets_likely_spilled (rtx pat)
2211 bool ret = false;
2212 note_stores (pat, sets_likely_spilled_1, &ret);
2213 return ret;
2216 static void
2217 sets_likely_spilled_1 (rtx x, const_rtx pat, void *data)
2219 bool *ret = (bool *) data;
2221 if (GET_CODE (pat) == SET
2222 && REG_P (x)
2223 && REGNO (x) < FIRST_PSEUDO_REGISTER
2224 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (x))))
2225 *ret = true;
2228 /* Add dependences so that branches are scheduled to run last in their
2229 block. */
2231 static void
2232 add_branch_dependences (rtx head, rtx tail)
2234 rtx insn, last;
2236 /* For all branches, calls, uses, clobbers, cc0 setters, and instructions
2237 that can throw exceptions, force them to remain in order at the end of
2238 the block by adding dependencies and giving the last a high priority.
2239 There may be notes present, and prev_head may also be a note.
2241 Branches must obviously remain at the end. Calls should remain at the
2242 end since moving them results in worse register allocation. Uses remain
2243 at the end to ensure proper register allocation.
2245 cc0 setters remain at the end because they can't be moved away from
2246 their cc0 user.
2248 COND_EXEC insns cannot be moved past a branch (see e.g. PR17808).
2250 Insns setting CLASS_LIKELY_SPILLED_P registers (usually return values)
2251 are not moved before reload because we can wind up with register
2252 allocation failures. */
2254 insn = tail;
2255 last = 0;
2256 while (CALL_P (insn)
2257 || JUMP_P (insn)
2258 || (NONJUMP_INSN_P (insn)
2259 && (GET_CODE (PATTERN (insn)) == USE
2260 || GET_CODE (PATTERN (insn)) == CLOBBER
2261 || can_throw_internal (insn)
2262 #ifdef HAVE_cc0
2263 || sets_cc0_p (PATTERN (insn))
2264 #endif
2265 || (!reload_completed
2266 && sets_likely_spilled (PATTERN (insn)))))
2267 || NOTE_P (insn))
2269 if (!NOTE_P (insn))
2271 if (last != 0
2272 && sd_find_dep_between (insn, last, false) == NULL)
2274 if (! sched_insns_conditions_mutex_p (last, insn))
2275 add_dependence (last, insn, REG_DEP_ANTI);
2276 INSN_REF_COUNT (insn)++;
2279 CANT_MOVE (insn) = 1;
2281 last = insn;
2284 /* Don't overrun the bounds of the basic block. */
2285 if (insn == head)
2286 break;
2288 insn = PREV_INSN (insn);
2291 /* Make sure these insns are scheduled last in their block. */
2292 insn = last;
2293 if (insn != 0)
2294 while (insn != head)
2296 insn = prev_nonnote_insn (insn);
2298 if (INSN_REF_COUNT (insn) != 0)
2299 continue;
2301 if (! sched_insns_conditions_mutex_p (last, insn))
2302 add_dependence (last, insn, REG_DEP_ANTI);
2303 INSN_REF_COUNT (insn) = 1;
2306 #ifdef HAVE_conditional_execution
2307 /* Finally, if the block ends in a jump, and we are doing intra-block
2308 scheduling, make sure that the branch depends on any COND_EXEC insns
2309 inside the block to avoid moving the COND_EXECs past the branch insn.
2311 We only have to do this after reload, because (1) before reload there
2312 are no COND_EXEC insns, and (2) the region scheduler is an intra-block
2313 scheduler after reload.
2315 FIXME: We could in some cases move COND_EXEC insns past the branch if
2316 this scheduler would be a little smarter. Consider this code:
2318 T = [addr]
2319 C ? addr += 4
2320 !C ? X += 12
2321 C ? T += 1
2322 C ? jump foo
2324 On a target with a one cycle stall on a memory access the optimal
2325 sequence would be:
2327 T = [addr]
2328 C ? addr += 4
2329 C ? T += 1
2330 C ? jump foo
2331 !C ? X += 12
2333 We don't want to put the 'X += 12' before the branch because it just
2334 wastes a cycle of execution time when the branch is taken.
2336 Note that in the example "!C" will always be true. That is another
2337 possible improvement for handling COND_EXECs in this scheduler: it
2338 could remove always-true predicates. */
2340 if (!reload_completed || ! JUMP_P (tail))
2341 return;
2343 insn = tail;
2344 while (insn != head)
2346 insn = PREV_INSN (insn);
2348 /* Note that we want to add this dependency even when
2349 sched_insns_conditions_mutex_p returns true. The whole point
2350 is that we _want_ this dependency, even if these insns really
2351 are independent. */
2352 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == COND_EXEC)
2353 add_dependence (tail, insn, REG_DEP_ANTI);
2355 #endif
2358 /* Data structures for the computation of data dependences in a regions. We
2359 keep one `deps' structure for every basic block. Before analyzing the
2360 data dependences for a bb, its variables are initialized as a function of
2361 the variables of its predecessors. When the analysis for a bb completes,
2362 we save the contents to the corresponding bb_deps[bb] variable. */
2364 static struct deps *bb_deps;
2366 /* Duplicate the INSN_LIST elements of COPY and prepend them to OLD. */
2368 static rtx
2369 concat_INSN_LIST (rtx copy, rtx old)
2371 rtx new = old;
2372 for (; copy ; copy = XEXP (copy, 1))
2373 new = alloc_INSN_LIST (XEXP (copy, 0), new);
2374 return new;
2377 static void
2378 concat_insn_mem_list (rtx copy_insns, rtx copy_mems, rtx *old_insns_p,
2379 rtx *old_mems_p)
2381 rtx new_insns = *old_insns_p;
2382 rtx new_mems = *old_mems_p;
2384 while (copy_insns)
2386 new_insns = alloc_INSN_LIST (XEXP (copy_insns, 0), new_insns);
2387 new_mems = alloc_EXPR_LIST (VOIDmode, XEXP (copy_mems, 0), new_mems);
2388 copy_insns = XEXP (copy_insns, 1);
2389 copy_mems = XEXP (copy_mems, 1);
2392 *old_insns_p = new_insns;
2393 *old_mems_p = new_mems;
2396 /* After computing the dependencies for block BB, propagate the dependencies
2397 found in TMP_DEPS to the successors of the block. */
2398 static void
2399 propagate_deps (int bb, struct deps *pred_deps)
2401 basic_block block = BASIC_BLOCK (BB_TO_BLOCK (bb));
2402 edge_iterator ei;
2403 edge e;
2405 /* bb's structures are inherited by its successors. */
2406 FOR_EACH_EDGE (e, ei, block->succs)
2408 struct deps *succ_deps;
2409 unsigned reg;
2410 reg_set_iterator rsi;
2412 /* Only bbs "below" bb, in the same region, are interesting. */
2413 if (e->dest == EXIT_BLOCK_PTR
2414 || CONTAINING_RGN (block->index) != CONTAINING_RGN (e->dest->index)
2415 || BLOCK_TO_BB (e->dest->index) <= bb)
2416 continue;
2418 succ_deps = bb_deps + BLOCK_TO_BB (e->dest->index);
2420 /* The reg_last lists are inherited by successor. */
2421 EXECUTE_IF_SET_IN_REG_SET (&pred_deps->reg_last_in_use, 0, reg, rsi)
2423 struct deps_reg *pred_rl = &pred_deps->reg_last[reg];
2424 struct deps_reg *succ_rl = &succ_deps->reg_last[reg];
2426 succ_rl->uses = concat_INSN_LIST (pred_rl->uses, succ_rl->uses);
2427 succ_rl->sets = concat_INSN_LIST (pred_rl->sets, succ_rl->sets);
2428 succ_rl->clobbers = concat_INSN_LIST (pred_rl->clobbers,
2429 succ_rl->clobbers);
2430 succ_rl->uses_length += pred_rl->uses_length;
2431 succ_rl->clobbers_length += pred_rl->clobbers_length;
2433 IOR_REG_SET (&succ_deps->reg_last_in_use, &pred_deps->reg_last_in_use);
2435 /* Mem read/write lists are inherited by successor. */
2436 concat_insn_mem_list (pred_deps->pending_read_insns,
2437 pred_deps->pending_read_mems,
2438 &succ_deps->pending_read_insns,
2439 &succ_deps->pending_read_mems);
2440 concat_insn_mem_list (pred_deps->pending_write_insns,
2441 pred_deps->pending_write_mems,
2442 &succ_deps->pending_write_insns,
2443 &succ_deps->pending_write_mems);
2445 succ_deps->last_pending_memory_flush
2446 = concat_INSN_LIST (pred_deps->last_pending_memory_flush,
2447 succ_deps->last_pending_memory_flush);
2449 succ_deps->pending_read_list_length
2450 += pred_deps->pending_read_list_length;
2451 succ_deps->pending_write_list_length
2452 += pred_deps->pending_write_list_length;
2453 succ_deps->pending_flush_length += pred_deps->pending_flush_length;
2455 /* last_function_call is inherited by successor. */
2456 succ_deps->last_function_call
2457 = concat_INSN_LIST (pred_deps->last_function_call,
2458 succ_deps->last_function_call);
2460 /* sched_before_next_call is inherited by successor. */
2461 succ_deps->sched_before_next_call
2462 = concat_INSN_LIST (pred_deps->sched_before_next_call,
2463 succ_deps->sched_before_next_call);
2466 /* These lists should point to the right place, for correct
2467 freeing later. */
2468 bb_deps[bb].pending_read_insns = pred_deps->pending_read_insns;
2469 bb_deps[bb].pending_read_mems = pred_deps->pending_read_mems;
2470 bb_deps[bb].pending_write_insns = pred_deps->pending_write_insns;
2471 bb_deps[bb].pending_write_mems = pred_deps->pending_write_mems;
2473 /* Can't allow these to be freed twice. */
2474 pred_deps->pending_read_insns = 0;
2475 pred_deps->pending_read_mems = 0;
2476 pred_deps->pending_write_insns = 0;
2477 pred_deps->pending_write_mems = 0;
2480 /* Compute dependences inside bb. In a multiple blocks region:
2481 (1) a bb is analyzed after its predecessors, and (2) the lists in
2482 effect at the end of bb (after analyzing for bb) are inherited by
2483 bb's successors.
2485 Specifically for reg-reg data dependences, the block insns are
2486 scanned by sched_analyze () top-to-bottom. Two lists are
2487 maintained by sched_analyze (): reg_last[].sets for register DEFs,
2488 and reg_last[].uses for register USEs.
2490 When analysis is completed for bb, we update for its successors:
2491 ; - DEFS[succ] = Union (DEFS [succ], DEFS [bb])
2492 ; - USES[succ] = Union (USES [succ], DEFS [bb])
2494 The mechanism for computing mem-mem data dependence is very
2495 similar, and the result is interblock dependences in the region. */
2497 static void
2498 compute_block_dependences (int bb)
2500 rtx head, tail;
2501 struct deps tmp_deps;
2503 tmp_deps = bb_deps[bb];
2505 /* Do the analysis for this block. */
2506 gcc_assert (EBB_FIRST_BB (bb) == EBB_LAST_BB (bb));
2507 get_ebb_head_tail (EBB_FIRST_BB (bb), EBB_LAST_BB (bb), &head, &tail);
2509 sched_analyze (&tmp_deps, head, tail);
2510 add_branch_dependences (head, tail);
2512 if (current_nr_blocks > 1)
2513 propagate_deps (bb, &tmp_deps);
2515 /* Free up the INSN_LISTs. */
2516 free_deps (&tmp_deps);
2518 if (targetm.sched.dependencies_evaluation_hook)
2519 targetm.sched.dependencies_evaluation_hook (head, tail);
2522 /* Free dependencies of instructions inside BB. */
2523 static void
2524 free_block_dependencies (int bb)
2526 rtx head;
2527 rtx tail;
2529 get_ebb_head_tail (EBB_FIRST_BB (bb), EBB_LAST_BB (bb), &head, &tail);
2531 sched_free_deps (head, tail, true);
2534 /* Remove all INSN_LISTs and EXPR_LISTs from the pending lists and add
2535 them to the unused_*_list variables, so that they can be reused. */
2537 static void
2538 free_pending_lists (void)
2540 int bb;
2542 for (bb = 0; bb < current_nr_blocks; bb++)
2544 free_INSN_LIST_list (&bb_deps[bb].pending_read_insns);
2545 free_INSN_LIST_list (&bb_deps[bb].pending_write_insns);
2546 free_EXPR_LIST_list (&bb_deps[bb].pending_read_mems);
2547 free_EXPR_LIST_list (&bb_deps[bb].pending_write_mems);
2551 /* Print dependences for debugging starting from FROM_BB.
2552 Callable from debugger. */
2553 /* Print dependences for debugging starting from FROM_BB.
2554 Callable from debugger. */
2555 void
2556 debug_rgn_dependencies (int from_bb)
2558 int bb;
2560 fprintf (sched_dump,
2561 ";; --------------- forward dependences: ------------ \n");
2563 for (bb = from_bb; bb < current_nr_blocks; bb++)
2565 rtx head, tail;
2567 gcc_assert (EBB_FIRST_BB (bb) == EBB_LAST_BB (bb));
2568 get_ebb_head_tail (EBB_FIRST_BB (bb), EBB_LAST_BB (bb), &head, &tail);
2569 fprintf (sched_dump, "\n;; --- Region Dependences --- b %d bb %d \n",
2570 BB_TO_BLOCK (bb), bb);
2572 debug_dependencies (head, tail);
2576 /* Print dependencies information for instructions between HEAD and TAIL.
2577 ??? This function would probably fit best in haifa-sched.c. */
2578 void debug_dependencies (rtx head, rtx tail)
2580 rtx insn;
2581 rtx next_tail = NEXT_INSN (tail);
2583 fprintf (sched_dump, ";; %7s%6s%6s%6s%6s%6s%14s\n",
2584 "insn", "code", "bb", "dep", "prio", "cost",
2585 "reservation");
2586 fprintf (sched_dump, ";; %7s%6s%6s%6s%6s%6s%14s\n",
2587 "----", "----", "--", "---", "----", "----",
2588 "-----------");
2590 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
2592 if (! INSN_P (insn))
2594 int n;
2595 fprintf (sched_dump, ";; %6d ", INSN_UID (insn));
2596 if (NOTE_P (insn))
2598 n = NOTE_KIND (insn);
2599 fprintf (sched_dump, "%s\n", GET_NOTE_INSN_NAME (n));
2601 else
2602 fprintf (sched_dump, " {%s}\n", GET_RTX_NAME (GET_CODE (insn)));
2603 continue;
2606 fprintf (sched_dump,
2607 ";; %s%5d%6d%6d%6d%6d%6d ",
2608 (SCHED_GROUP_P (insn) ? "+" : " "),
2609 INSN_UID (insn),
2610 INSN_CODE (insn),
2611 BLOCK_NUM (insn),
2612 sd_lists_size (insn, SD_LIST_BACK),
2613 INSN_PRIORITY (insn),
2614 insn_cost (insn));
2616 if (recog_memoized (insn) < 0)
2617 fprintf (sched_dump, "nothing");
2618 else
2619 print_reservation (sched_dump, insn);
2621 fprintf (sched_dump, "\t: ");
2623 sd_iterator_def sd_it;
2624 dep_t dep;
2626 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
2627 fprintf (sched_dump, "%d ", INSN_UID (DEP_CON (dep)));
2629 fprintf (sched_dump, "\n");
2632 fprintf (sched_dump, "\n");
2635 /* Returns true if all the basic blocks of the current region have
2636 NOTE_DISABLE_SCHED_OF_BLOCK which means not to schedule that region. */
2637 static bool
2638 sched_is_disabled_for_current_region_p (void)
2640 int bb;
2642 for (bb = 0; bb < current_nr_blocks; bb++)
2643 if (!(BASIC_BLOCK (BB_TO_BLOCK (bb))->flags & BB_DISABLE_SCHEDULE))
2644 return false;
2646 return true;
2649 /* Schedule a region. A region is either an inner loop, a loop-free
2650 subroutine, or a single basic block. Each bb in the region is
2651 scheduled after its flow predecessors. */
2653 static void
2654 schedule_region (int rgn)
2656 basic_block block;
2657 edge_iterator ei;
2658 edge e;
2659 int bb;
2660 int sched_rgn_n_insns = 0;
2662 rgn_n_insns = 0;
2663 /* Set variables for the current region. */
2664 current_nr_blocks = RGN_NR_BLOCKS (rgn);
2665 current_blocks = RGN_BLOCKS (rgn);
2667 /* See comments in add_block1, for what reasons we allocate +1 element. */
2668 ebb_head = xrealloc (ebb_head, (current_nr_blocks + 1) * sizeof (*ebb_head));
2669 for (bb = 0; bb <= current_nr_blocks; bb++)
2670 ebb_head[bb] = current_blocks + bb;
2672 /* Don't schedule region that is marked by
2673 NOTE_DISABLE_SCHED_OF_BLOCK. */
2674 if (sched_is_disabled_for_current_region_p ())
2675 return;
2677 if (!RGN_DONT_CALC_DEPS (rgn))
2679 init_deps_global ();
2681 /* Initializations for region data dependence analysis. */
2682 bb_deps = XNEWVEC (struct deps, current_nr_blocks);
2683 for (bb = 0; bb < current_nr_blocks; bb++)
2684 init_deps (bb_deps + bb);
2686 /* Compute dependencies. */
2687 for (bb = 0; bb < current_nr_blocks; bb++)
2688 compute_block_dependences (bb);
2690 free_pending_lists ();
2692 finish_deps_global ();
2694 free (bb_deps);
2696 else
2697 /* This is a recovery block. It is always a single block region. */
2698 gcc_assert (current_nr_blocks == 1);
2700 /* Set priorities. */
2701 current_sched_info->sched_max_insns_priority = 0;
2702 for (bb = 0; bb < current_nr_blocks; bb++)
2704 rtx head, tail;
2706 gcc_assert (EBB_FIRST_BB (bb) == EBB_LAST_BB (bb));
2707 get_ebb_head_tail (EBB_FIRST_BB (bb), EBB_LAST_BB (bb), &head, &tail);
2709 rgn_n_insns += set_priorities (head, tail);
2711 current_sched_info->sched_max_insns_priority++;
2713 /* Compute interblock info: probabilities, split-edges, dominators, etc. */
2714 if (current_nr_blocks > 1)
2716 prob = XNEWVEC (int, current_nr_blocks);
2718 dom = sbitmap_vector_alloc (current_nr_blocks, current_nr_blocks);
2719 sbitmap_vector_zero (dom, current_nr_blocks);
2721 /* Use ->aux to implement EDGE_TO_BIT mapping. */
2722 rgn_nr_edges = 0;
2723 FOR_EACH_BB (block)
2725 if (CONTAINING_RGN (block->index) != rgn)
2726 continue;
2727 FOR_EACH_EDGE (e, ei, block->succs)
2728 SET_EDGE_TO_BIT (e, rgn_nr_edges++);
2731 rgn_edges = XNEWVEC (edge, rgn_nr_edges);
2732 rgn_nr_edges = 0;
2733 FOR_EACH_BB (block)
2735 if (CONTAINING_RGN (block->index) != rgn)
2736 continue;
2737 FOR_EACH_EDGE (e, ei, block->succs)
2738 rgn_edges[rgn_nr_edges++] = e;
2741 /* Split edges. */
2742 pot_split = sbitmap_vector_alloc (current_nr_blocks, rgn_nr_edges);
2743 sbitmap_vector_zero (pot_split, current_nr_blocks);
2744 ancestor_edges = sbitmap_vector_alloc (current_nr_blocks, rgn_nr_edges);
2745 sbitmap_vector_zero (ancestor_edges, current_nr_blocks);
2747 /* Compute probabilities, dominators, split_edges. */
2748 for (bb = 0; bb < current_nr_blocks; bb++)
2749 compute_dom_prob_ps (bb);
2751 /* Cleanup ->aux used for EDGE_TO_BIT mapping. */
2752 /* We don't need them anymore. But we want to avoid duplication of
2753 aux fields in the newly created edges. */
2754 FOR_EACH_BB (block)
2756 if (CONTAINING_RGN (block->index) != rgn)
2757 continue;
2758 FOR_EACH_EDGE (e, ei, block->succs)
2759 e->aux = NULL;
2763 /* Now we can schedule all blocks. */
2764 for (bb = 0; bb < current_nr_blocks; bb++)
2766 basic_block first_bb, last_bb, curr_bb;
2767 rtx head, tail;
2769 first_bb = EBB_FIRST_BB (bb);
2770 last_bb = EBB_LAST_BB (bb);
2772 get_ebb_head_tail (first_bb, last_bb, &head, &tail);
2774 if (no_real_insns_p (head, tail))
2776 gcc_assert (first_bb == last_bb);
2777 continue;
2780 current_sched_info->prev_head = PREV_INSN (head);
2781 current_sched_info->next_tail = NEXT_INSN (tail);
2784 /* rm_other_notes only removes notes which are _inside_ the
2785 block---that is, it won't remove notes before the first real insn
2786 or after the last real insn of the block. So if the first insn
2787 has a REG_SAVE_NOTE which would otherwise be emitted before the
2788 insn, it is redundant with the note before the start of the
2789 block, and so we have to take it out. */
2790 if (INSN_P (head))
2792 rtx note;
2794 for (note = REG_NOTES (head); note; note = XEXP (note, 1))
2795 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
2796 remove_note (head, note);
2798 else
2799 /* This means that first block in ebb is empty.
2800 It looks to me as an impossible thing. There at least should be
2801 a recovery check, that caused the splitting. */
2802 gcc_unreachable ();
2804 /* Remove remaining note insns from the block, save them in
2805 note_list. These notes are restored at the end of
2806 schedule_block (). */
2807 rm_other_notes (head, tail);
2809 unlink_bb_notes (first_bb, last_bb);
2811 target_bb = bb;
2813 gcc_assert (flag_schedule_interblock || current_nr_blocks == 1);
2814 current_sched_info->queue_must_finish_empty = current_nr_blocks == 1;
2816 curr_bb = first_bb;
2817 if (dbg_cnt (sched_block))
2819 schedule_block (&curr_bb, rgn_n_insns);
2820 gcc_assert (EBB_FIRST_BB (bb) == first_bb);
2821 sched_rgn_n_insns += sched_n_insns;
2823 else
2825 sched_rgn_n_insns += rgn_n_insns;
2828 /* Clean up. */
2829 if (current_nr_blocks > 1)
2831 free (candidate_table);
2832 free (bblst_table);
2833 free (edgelst_table);
2837 /* Sanity check: verify that all region insns were scheduled. */
2838 gcc_assert (sched_rgn_n_insns == rgn_n_insns);
2840 /* Done with this region. */
2842 if (current_nr_blocks > 1)
2844 free (prob);
2845 sbitmap_vector_free (dom);
2846 sbitmap_vector_free (pot_split);
2847 sbitmap_vector_free (ancestor_edges);
2848 free (rgn_edges);
2851 /* Free dependencies. */
2852 for (bb = 0; bb < current_nr_blocks; ++bb)
2853 free_block_dependencies (bb);
2855 gcc_assert (haifa_recovery_bb_ever_added_p
2856 || deps_pools_are_empty_p ());
2859 /* Initialize data structures for region scheduling. */
2861 static void
2862 init_regions (void)
2864 nr_regions = 0;
2865 rgn_table = 0;
2866 rgn_bb_table = 0;
2867 block_to_bb = 0;
2868 containing_rgn = 0;
2869 extend_regions ();
2871 /* Compute regions for scheduling. */
2872 if (reload_completed
2873 || n_basic_blocks == NUM_FIXED_BLOCKS + 1
2874 || !flag_schedule_interblock
2875 || is_cfg_nonregular ())
2877 find_single_block_region ();
2879 else
2881 /* Compute the dominators and post dominators. */
2882 calculate_dominance_info (CDI_DOMINATORS);
2884 /* Find regions. */
2885 find_rgns ();
2887 if (sched_verbose >= 3)
2888 debug_regions ();
2890 /* For now. This will move as more and more of haifa is converted
2891 to using the cfg code. */
2892 free_dominance_info (CDI_DOMINATORS);
2894 RGN_BLOCKS (nr_regions) = RGN_BLOCKS (nr_regions - 1) +
2895 RGN_NR_BLOCKS (nr_regions - 1);
2898 /* The one entry point in this file. */
2900 void
2901 schedule_insns (void)
2903 int rgn;
2905 /* Taking care of this degenerate case makes the rest of
2906 this code simpler. */
2907 if (n_basic_blocks == NUM_FIXED_BLOCKS)
2908 return;
2910 nr_inter = 0;
2911 nr_spec = 0;
2913 /* We need current_sched_info in init_dependency_caches, which is
2914 invoked via sched_init. */
2915 current_sched_info = &region_sched_info;
2917 df_set_flags (DF_LR_RUN_DCE);
2918 df_note_add_problem ();
2919 df_analyze ();
2920 regstat_compute_calls_crossed ();
2922 sched_init ();
2924 bitmap_initialize (&not_in_df, 0);
2925 bitmap_clear (&not_in_df);
2927 min_spec_prob = ((PARAM_VALUE (PARAM_MIN_SPEC_PROB) * REG_BR_PROB_BASE)
2928 / 100);
2930 init_regions ();
2932 /* EBB_HEAD is a region-scope structure. But we realloc it for
2933 each region to save time/memory/something else. */
2934 ebb_head = 0;
2936 /* Schedule every region in the subroutine. */
2937 for (rgn = 0; rgn < nr_regions; rgn++)
2938 if (dbg_cnt (sched_region))
2939 schedule_region (rgn);
2941 free(ebb_head);
2942 /* Reposition the prologue and epilogue notes in case we moved the
2943 prologue/epilogue insns. */
2944 if (reload_completed)
2945 reposition_prologue_and_epilogue_notes ();
2947 if (sched_verbose)
2949 if (reload_completed == 0 && flag_schedule_interblock)
2951 fprintf (sched_dump,
2952 "\n;; Procedure interblock/speculative motions == %d/%d \n",
2953 nr_inter, nr_spec);
2955 else
2956 gcc_assert (nr_inter <= 0);
2957 fprintf (sched_dump, "\n\n");
2960 /* Clean up. */
2961 free (rgn_table);
2962 free (rgn_bb_table);
2963 free (block_to_bb);
2964 free (containing_rgn);
2966 regstat_free_calls_crossed ();
2968 bitmap_clear (&not_in_df);
2970 sched_finish ();
2973 /* INSN has been added to/removed from current region. */
2974 static void
2975 add_remove_insn (rtx insn, int remove_p)
2977 if (!remove_p)
2978 rgn_n_insns++;
2979 else
2980 rgn_n_insns--;
2982 if (INSN_BB (insn) == target_bb)
2984 if (!remove_p)
2985 target_n_insns++;
2986 else
2987 target_n_insns--;
2991 /* Extend internal data structures. */
2992 static void
2993 extend_regions (void)
2995 rgn_table = XRESIZEVEC (region, rgn_table, n_basic_blocks);
2996 rgn_bb_table = XRESIZEVEC (int, rgn_bb_table, n_basic_blocks);
2997 block_to_bb = XRESIZEVEC (int, block_to_bb, last_basic_block);
2998 containing_rgn = XRESIZEVEC (int, containing_rgn, last_basic_block);
3001 /* BB was added to ebb after AFTER. */
3002 static void
3003 add_block1 (basic_block bb, basic_block after)
3005 extend_regions ();
3007 bitmap_set_bit (&not_in_df, bb->index);
3009 if (after == 0 || after == EXIT_BLOCK_PTR)
3011 int i;
3013 i = RGN_BLOCKS (nr_regions);
3014 /* I - first free position in rgn_bb_table. */
3016 rgn_bb_table[i] = bb->index;
3017 RGN_NR_BLOCKS (nr_regions) = 1;
3018 RGN_DONT_CALC_DEPS (nr_regions) = after == EXIT_BLOCK_PTR;
3019 RGN_HAS_REAL_EBB (nr_regions) = 0;
3020 CONTAINING_RGN (bb->index) = nr_regions;
3021 BLOCK_TO_BB (bb->index) = 0;
3023 nr_regions++;
3025 RGN_BLOCKS (nr_regions) = i + 1;
3027 else
3029 int i, pos;
3031 /* We need to fix rgn_table, block_to_bb, containing_rgn
3032 and ebb_head. */
3034 BLOCK_TO_BB (bb->index) = BLOCK_TO_BB (after->index);
3036 /* We extend ebb_head to one more position to
3037 easily find the last position of the last ebb in
3038 the current region. Thus, ebb_head[BLOCK_TO_BB (after) + 1]
3039 is _always_ valid for access. */
3041 i = BLOCK_TO_BB (after->index) + 1;
3042 pos = ebb_head[i] - 1;
3043 /* Now POS is the index of the last block in the region. */
3045 /* Find index of basic block AFTER. */
3046 for (; rgn_bb_table[pos] != after->index; pos--);
3048 pos++;
3049 gcc_assert (pos > ebb_head[i - 1]);
3051 /* i - ebb right after "AFTER". */
3052 /* ebb_head[i] - VALID. */
3054 /* Source position: ebb_head[i]
3055 Destination position: ebb_head[i] + 1
3056 Last position:
3057 RGN_BLOCKS (nr_regions) - 1
3058 Number of elements to copy: (last_position) - (source_position) + 1
3061 memmove (rgn_bb_table + pos + 1,
3062 rgn_bb_table + pos,
3063 ((RGN_BLOCKS (nr_regions) - 1) - (pos) + 1)
3064 * sizeof (*rgn_bb_table));
3066 rgn_bb_table[pos] = bb->index;
3068 for (; i <= current_nr_blocks; i++)
3069 ebb_head [i]++;
3071 i = CONTAINING_RGN (after->index);
3072 CONTAINING_RGN (bb->index) = i;
3074 RGN_HAS_REAL_EBB (i) = 1;
3076 for (++i; i <= nr_regions; i++)
3077 RGN_BLOCKS (i)++;
3081 /* Fix internal data after interblock movement of jump instruction.
3082 For parameter meaning please refer to
3083 sched-int.h: struct sched_info: fix_recovery_cfg. */
3084 static void
3085 fix_recovery_cfg (int bbi, int check_bbi, int check_bb_nexti)
3087 int old_pos, new_pos, i;
3089 BLOCK_TO_BB (check_bb_nexti) = BLOCK_TO_BB (bbi);
3091 for (old_pos = ebb_head[BLOCK_TO_BB (check_bbi) + 1] - 1;
3092 rgn_bb_table[old_pos] != check_bb_nexti;
3093 old_pos--);
3094 gcc_assert (old_pos > ebb_head[BLOCK_TO_BB (check_bbi)]);
3096 for (new_pos = ebb_head[BLOCK_TO_BB (bbi) + 1] - 1;
3097 rgn_bb_table[new_pos] != bbi;
3098 new_pos--);
3099 new_pos++;
3100 gcc_assert (new_pos > ebb_head[BLOCK_TO_BB (bbi)]);
3102 gcc_assert (new_pos < old_pos);
3104 memmove (rgn_bb_table + new_pos + 1,
3105 rgn_bb_table + new_pos,
3106 (old_pos - new_pos) * sizeof (*rgn_bb_table));
3108 rgn_bb_table[new_pos] = check_bb_nexti;
3110 for (i = BLOCK_TO_BB (bbi) + 1; i <= BLOCK_TO_BB (check_bbi); i++)
3111 ebb_head[i]++;
3114 /* Return next block in ebb chain. For parameter meaning please refer to
3115 sched-int.h: struct sched_info: advance_target_bb. */
3116 static basic_block
3117 advance_target_bb (basic_block bb, rtx insn)
3119 if (insn)
3120 return 0;
3122 gcc_assert (BLOCK_TO_BB (bb->index) == target_bb
3123 && BLOCK_TO_BB (bb->next_bb->index) == target_bb);
3124 return bb->next_bb;
3127 #endif
3129 static bool
3130 gate_handle_sched (void)
3132 #ifdef INSN_SCHEDULING
3133 return flag_schedule_insns && dbg_cnt (sched_func);
3134 #else
3135 return 0;
3136 #endif
3139 /* Run instruction scheduler. */
3140 static unsigned int
3141 rest_of_handle_sched (void)
3143 #ifdef INSN_SCHEDULING
3144 schedule_insns ();
3145 #endif
3146 return 0;
3149 static bool
3150 gate_handle_sched2 (void)
3152 #ifdef INSN_SCHEDULING
3153 return optimize > 0 && flag_schedule_insns_after_reload
3154 && dbg_cnt (sched2_func);
3155 #else
3156 return 0;
3157 #endif
3160 /* Run second scheduling pass after reload. */
3161 static unsigned int
3162 rest_of_handle_sched2 (void)
3164 #ifdef INSN_SCHEDULING
3165 /* Do control and data sched analysis again,
3166 and write some more of the results to dump file. */
3167 if (flag_sched2_use_superblocks || flag_sched2_use_traces)
3168 schedule_ebbs ();
3169 else
3170 schedule_insns ();
3171 #endif
3172 return 0;
3175 struct tree_opt_pass pass_sched =
3177 "sched1", /* name */
3178 gate_handle_sched, /* gate */
3179 rest_of_handle_sched, /* execute */
3180 NULL, /* sub */
3181 NULL, /* next */
3182 0, /* static_pass_number */
3183 TV_SCHED, /* tv_id */
3184 0, /* properties_required */
3185 0, /* properties_provided */
3186 0, /* properties_destroyed */
3187 0, /* todo_flags_start */
3188 TODO_df_finish | TODO_verify_rtl_sharing |
3189 TODO_dump_func |
3190 TODO_verify_flow |
3191 TODO_ggc_collect, /* todo_flags_finish */
3192 'S' /* letter */
3195 struct tree_opt_pass pass_sched2 =
3197 "sched2", /* name */
3198 gate_handle_sched2, /* gate */
3199 rest_of_handle_sched2, /* execute */
3200 NULL, /* sub */
3201 NULL, /* next */
3202 0, /* static_pass_number */
3203 TV_SCHED2, /* tv_id */
3204 0, /* properties_required */
3205 0, /* properties_provided */
3206 0, /* properties_destroyed */
3207 0, /* todo_flags_start */
3208 TODO_df_finish | TODO_verify_rtl_sharing |
3209 TODO_dump_func |
3210 TODO_verify_flow |
3211 TODO_ggc_collect, /* todo_flags_finish */
3212 'R' /* letter */