basic-block.h (find_fallthru_edge): Define.
[gcc.git] / gcc / sched-rgn.c
1 /* Instruction scheduling pass.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2010
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)
7
8 This file is part of GCC.
9
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.
14
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.
19
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/>. */
23
24 /* This pass implements list scheduling within basic blocks. It is
25 run twice: (1) after flow analysis, but before register allocation,
26 and (2) after register allocation.
27
28 The first run performs interblock scheduling, moving insns between
29 different blocks in the same "region", and the second runs only
30 basic block scheduling.
31
32 Interblock motions performed are useful motions and speculative
33 motions, including speculative loads. Motions requiring code
34 duplication are not supported. The identification of motion type
35 and the check for validity of speculative motions requires
36 construction and analysis of the function's control flow graph.
37
38 The main entry point for this pass is schedule_insns(), called for
39 each function. The work of the scheduler is organized in three
40 levels: (1) function level: insns are subject to splitting,
41 control-flow-graph is constructed, regions are computed (after
42 reload, each region is of one block), (2) region level: control
43 flow graph attributes required for interblock scheduling are
44 computed (dominators, reachability, etc.), data dependences and
45 priorities are computed, and (3) block level: insns in the block
46 are actually scheduled. */
47 \f
48 #include "config.h"
49 #include "system.h"
50 #include "coretypes.h"
51 #include "tm.h"
52 #include "diagnostic-core.h"
53 #include "toplev.h"
54 #include "rtl.h"
55 #include "tm_p.h"
56 #include "hard-reg-set.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 "sel-sched.h"
69 #include "target.h"
70 #include "timevar.h"
71 #include "tree-pass.h"
72 #include "dbgcnt.h"
73
74 #ifdef INSN_SCHEDULING
75
76 /* Some accessor macros for h_i_d members only used within this file. */
77 #define FED_BY_SPEC_LOAD(INSN) (HID (INSN)->fed_by_spec_load)
78 #define IS_LOAD_INSN(INSN) (HID (insn)->is_load_insn)
79
80 /* nr_inter/spec counts interblock/speculative motion for the function. */
81 static int nr_inter, nr_spec;
82
83 static int is_cfg_nonregular (void);
84
85 /* Number of regions in the procedure. */
86 int nr_regions = 0;
87
88 /* Table of region descriptions. */
89 region *rgn_table = NULL;
90
91 /* Array of lists of regions' blocks. */
92 int *rgn_bb_table = NULL;
93
94 /* Topological order of blocks in the region (if b2 is reachable from
95 b1, block_to_bb[b2] > block_to_bb[b1]). Note: A basic block is
96 always referred to by either block or b, while its topological
97 order name (in the region) is referred to by bb. */
98 int *block_to_bb = NULL;
99
100 /* The number of the region containing a block. */
101 int *containing_rgn = NULL;
102
103 /* ebb_head [i] - is index in rgn_bb_table of the head basic block of i'th ebb.
104 Currently we can get a ebb only through splitting of currently
105 scheduling block, therefore, we don't need ebb_head array for every region,
106 hence, its sufficient to hold it for current one only. */
107 int *ebb_head = NULL;
108
109 /* The minimum probability of reaching a source block so that it will be
110 considered for speculative scheduling. */
111 static int min_spec_prob;
112
113 static void find_single_block_region (bool);
114 static void find_rgns (void);
115 static bool too_large (int, int *, int *);
116
117 /* Blocks of the current region being scheduled. */
118 int current_nr_blocks;
119 int current_blocks;
120
121 /* A speculative motion requires checking live information on the path
122 from 'source' to 'target'. The split blocks are those to be checked.
123 After a speculative motion, live information should be modified in
124 the 'update' blocks.
125
126 Lists of split and update blocks for each candidate of the current
127 target are in array bblst_table. */
128 static basic_block *bblst_table;
129 static int bblst_size, bblst_last;
130
131 /* Target info declarations.
132
133 The block currently being scheduled is referred to as the "target" block,
134 while other blocks in the region from which insns can be moved to the
135 target are called "source" blocks. The candidate structure holds info
136 about such sources: are they valid? Speculative? Etc. */
137 typedef struct
138 {
139 basic_block *first_member;
140 int nr_members;
141 }
142 bblst;
143
144 typedef struct
145 {
146 char is_valid;
147 char is_speculative;
148 int src_prob;
149 bblst split_bbs;
150 bblst update_bbs;
151 }
152 candidate;
153
154 static candidate *candidate_table;
155 #define IS_VALID(src) (candidate_table[src].is_valid)
156 #define IS_SPECULATIVE(src) (candidate_table[src].is_speculative)
157 #define IS_SPECULATIVE_INSN(INSN) \
158 (IS_SPECULATIVE (BLOCK_TO_BB (BLOCK_NUM (INSN))))
159 #define SRC_PROB(src) ( candidate_table[src].src_prob )
160
161 /* The bb being currently scheduled. */
162 int target_bb;
163
164 /* List of edges. */
165 typedef struct
166 {
167 edge *first_member;
168 int nr_members;
169 }
170 edgelst;
171
172 static edge *edgelst_table;
173 static int edgelst_last;
174
175 static void extract_edgelst (sbitmap, edgelst *);
176
177 /* Target info functions. */
178 static void split_edges (int, int, edgelst *);
179 static void compute_trg_info (int);
180 void debug_candidate (int);
181 void debug_candidates (int);
182
183 /* Dominators array: dom[i] contains the sbitmap of dominators of
184 bb i in the region. */
185 static sbitmap *dom;
186
187 /* bb 0 is the only region entry. */
188 #define IS_RGN_ENTRY(bb) (!bb)
189
190 /* Is bb_src dominated by bb_trg. */
191 #define IS_DOMINATED(bb_src, bb_trg) \
192 ( TEST_BIT (dom[bb_src], bb_trg) )
193
194 /* Probability: Prob[i] is an int in [0, REG_BR_PROB_BASE] which is
195 the probability of bb i relative to the region entry. */
196 static int *prob;
197
198 /* Bit-set of edges, where bit i stands for edge i. */
199 typedef sbitmap edgeset;
200
201 /* Number of edges in the region. */
202 static int rgn_nr_edges;
203
204 /* Array of size rgn_nr_edges. */
205 static edge *rgn_edges;
206
207 /* Mapping from each edge in the graph to its number in the rgn. */
208 #define EDGE_TO_BIT(edge) ((int)(size_t)(edge)->aux)
209 #define SET_EDGE_TO_BIT(edge,nr) ((edge)->aux = (void *)(size_t)(nr))
210
211 /* The split edges of a source bb is different for each target
212 bb. In order to compute this efficiently, the 'potential-split edges'
213 are computed for each bb prior to scheduling a region. This is actually
214 the split edges of each bb relative to the region entry.
215
216 pot_split[bb] is the set of potential split edges of bb. */
217 static edgeset *pot_split;
218
219 /* For every bb, a set of its ancestor edges. */
220 static edgeset *ancestor_edges;
221
222 #define INSN_PROBABILITY(INSN) (SRC_PROB (BLOCK_TO_BB (BLOCK_NUM (INSN))))
223
224 /* Speculative scheduling functions. */
225 static int check_live_1 (int, rtx);
226 static void update_live_1 (int, rtx);
227 static int is_pfree (rtx, int, int);
228 static int find_conditional_protection (rtx, int);
229 static int is_conditionally_protected (rtx, int, int);
230 static int is_prisky (rtx, int, int);
231 static int is_exception_free (rtx, int, int);
232
233 static bool sets_likely_spilled (rtx);
234 static void sets_likely_spilled_1 (rtx, const_rtx, void *);
235 static void add_branch_dependences (rtx, rtx);
236 static void compute_block_dependences (int);
237
238 static void schedule_region (int);
239 static rtx concat_INSN_LIST (rtx, rtx);
240 static void concat_insn_mem_list (rtx, rtx, rtx *, rtx *);
241 static void propagate_deps (int, struct deps_desc *);
242 static void free_pending_lists (void);
243
244 /* Functions for construction of the control flow graph. */
245
246 /* Return 1 if control flow graph should not be constructed, 0 otherwise.
247
248 We decide not to build the control flow graph if there is possibly more
249 than one entry to the function, if computed branches exist, if we
250 have nonlocal gotos, or if we have an unreachable loop. */
251
252 static int
253 is_cfg_nonregular (void)
254 {
255 basic_block b;
256 rtx insn;
257
258 /* If we have a label that could be the target of a nonlocal goto, then
259 the cfg is not well structured. */
260 if (nonlocal_goto_handler_labels)
261 return 1;
262
263 /* If we have any forced labels, then the cfg is not well structured. */
264 if (forced_labels)
265 return 1;
266
267 /* If we have exception handlers, then we consider the cfg not well
268 structured. ?!? We should be able to handle this now that we
269 compute an accurate cfg for EH. */
270 if (current_function_has_exception_handlers ())
271 return 1;
272
273 /* If we have insns which refer to labels as non-jumped-to operands,
274 then we consider the cfg not well structured. */
275 FOR_EACH_BB (b)
276 FOR_BB_INSNS (b, insn)
277 {
278 rtx note, next, set, dest;
279
280 /* If this function has a computed jump, then we consider the cfg
281 not well structured. */
282 if (JUMP_P (insn) && computed_jump_p (insn))
283 return 1;
284
285 if (!INSN_P (insn))
286 continue;
287
288 note = find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX);
289 if (note == NULL_RTX)
290 continue;
291
292 /* For that label not to be seen as a referred-to label, this
293 must be a single-set which is feeding a jump *only*. This
294 could be a conditional jump with the label split off for
295 machine-specific reasons or a casesi/tablejump. */
296 next = next_nonnote_insn (insn);
297 if (next == NULL_RTX
298 || !JUMP_P (next)
299 || (JUMP_LABEL (next) != XEXP (note, 0)
300 && find_reg_note (next, REG_LABEL_TARGET,
301 XEXP (note, 0)) == NULL_RTX)
302 || BLOCK_FOR_INSN (insn) != BLOCK_FOR_INSN (next))
303 return 1;
304
305 set = single_set (insn);
306 if (set == NULL_RTX)
307 return 1;
308
309 dest = SET_DEST (set);
310 if (!REG_P (dest) || !dead_or_set_p (next, dest))
311 return 1;
312 }
313
314 /* Unreachable loops with more than one basic block are detected
315 during the DFS traversal in find_rgns.
316
317 Unreachable loops with a single block are detected here. This
318 test is redundant with the one in find_rgns, but it's much
319 cheaper to go ahead and catch the trivial case here. */
320 FOR_EACH_BB (b)
321 {
322 if (EDGE_COUNT (b->preds) == 0
323 || (single_pred_p (b)
324 && single_pred (b) == b))
325 return 1;
326 }
327
328 /* All the tests passed. Consider the cfg well structured. */
329 return 0;
330 }
331
332 /* Extract list of edges from a bitmap containing EDGE_TO_BIT bits. */
333
334 static void
335 extract_edgelst (sbitmap set, edgelst *el)
336 {
337 unsigned int i = 0;
338 sbitmap_iterator sbi;
339
340 /* edgelst table space is reused in each call to extract_edgelst. */
341 edgelst_last = 0;
342
343 el->first_member = &edgelst_table[edgelst_last];
344 el->nr_members = 0;
345
346 /* Iterate over each word in the bitset. */
347 EXECUTE_IF_SET_IN_SBITMAP (set, 0, i, sbi)
348 {
349 edgelst_table[edgelst_last++] = rgn_edges[i];
350 el->nr_members++;
351 }
352 }
353
354 /* Functions for the construction of regions. */
355
356 /* Print the regions, for debugging purposes. Callable from debugger. */
357
358 DEBUG_FUNCTION void
359 debug_regions (void)
360 {
361 int rgn, bb;
362
363 fprintf (sched_dump, "\n;; ------------ REGIONS ----------\n\n");
364 for (rgn = 0; rgn < nr_regions; rgn++)
365 {
366 fprintf (sched_dump, ";;\trgn %d nr_blocks %d:\n", rgn,
367 rgn_table[rgn].rgn_nr_blocks);
368 fprintf (sched_dump, ";;\tbb/block: ");
369
370 /* We don't have ebb_head initialized yet, so we can't use
371 BB_TO_BLOCK (). */
372 current_blocks = RGN_BLOCKS (rgn);
373
374 for (bb = 0; bb < rgn_table[rgn].rgn_nr_blocks; bb++)
375 fprintf (sched_dump, " %d/%d ", bb, rgn_bb_table[current_blocks + bb]);
376
377 fprintf (sched_dump, "\n\n");
378 }
379 }
380
381 /* Print the region's basic blocks. */
382
383 DEBUG_FUNCTION void
384 debug_region (int rgn)
385 {
386 int bb;
387
388 fprintf (stderr, "\n;; ------------ REGION %d ----------\n\n", rgn);
389 fprintf (stderr, ";;\trgn %d nr_blocks %d:\n", rgn,
390 rgn_table[rgn].rgn_nr_blocks);
391 fprintf (stderr, ";;\tbb/block: ");
392
393 /* We don't have ebb_head initialized yet, so we can't use
394 BB_TO_BLOCK (). */
395 current_blocks = RGN_BLOCKS (rgn);
396
397 for (bb = 0; bb < rgn_table[rgn].rgn_nr_blocks; bb++)
398 fprintf (stderr, " %d/%d ", bb, rgn_bb_table[current_blocks + bb]);
399
400 fprintf (stderr, "\n\n");
401
402 for (bb = 0; bb < rgn_table[rgn].rgn_nr_blocks; bb++)
403 {
404 debug_bb_n_slim (rgn_bb_table[current_blocks + bb]);
405 fprintf (stderr, "\n");
406 }
407
408 fprintf (stderr, "\n");
409
410 }
411
412 /* True when a bb with index BB_INDEX contained in region RGN. */
413 static bool
414 bb_in_region_p (int bb_index, int rgn)
415 {
416 int i;
417
418 for (i = 0; i < rgn_table[rgn].rgn_nr_blocks; i++)
419 if (rgn_bb_table[current_blocks + i] == bb_index)
420 return true;
421
422 return false;
423 }
424
425 /* Dump region RGN to file F using dot syntax. */
426 void
427 dump_region_dot (FILE *f, int rgn)
428 {
429 int i;
430
431 fprintf (f, "digraph Region_%d {\n", rgn);
432
433 /* We don't have ebb_head initialized yet, so we can't use
434 BB_TO_BLOCK (). */
435 current_blocks = RGN_BLOCKS (rgn);
436
437 for (i = 0; i < rgn_table[rgn].rgn_nr_blocks; i++)
438 {
439 edge e;
440 edge_iterator ei;
441 int src_bb_num = rgn_bb_table[current_blocks + i];
442 struct basic_block_def *bb = BASIC_BLOCK (src_bb_num);
443
444 FOR_EACH_EDGE (e, ei, bb->succs)
445 if (bb_in_region_p (e->dest->index, rgn))
446 fprintf (f, "\t%d -> %d\n", src_bb_num, e->dest->index);
447 }
448 fprintf (f, "}\n");
449 }
450
451 /* The same, but first open a file specified by FNAME. */
452 void
453 dump_region_dot_file (const char *fname, int rgn)
454 {
455 FILE *f = fopen (fname, "wt");
456 dump_region_dot (f, rgn);
457 fclose (f);
458 }
459
460 /* Build a single block region for each basic block in the function.
461 This allows for using the same code for interblock and basic block
462 scheduling. */
463
464 static void
465 find_single_block_region (bool ebbs_p)
466 {
467 basic_block bb, ebb_start;
468 int i = 0;
469
470 nr_regions = 0;
471
472 if (ebbs_p) {
473 int probability_cutoff;
474 if (profile_info && flag_branch_probabilities)
475 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
476 else
477 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
478 probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;
479
480 FOR_EACH_BB (ebb_start)
481 {
482 RGN_NR_BLOCKS (nr_regions) = 0;
483 RGN_BLOCKS (nr_regions) = i;
484 RGN_DONT_CALC_DEPS (nr_regions) = 0;
485 RGN_HAS_REAL_EBB (nr_regions) = 0;
486
487 for (bb = ebb_start; ; bb = bb->next_bb)
488 {
489 edge e;
490
491 rgn_bb_table[i] = bb->index;
492 RGN_NR_BLOCKS (nr_regions)++;
493 CONTAINING_RGN (bb->index) = nr_regions;
494 BLOCK_TO_BB (bb->index) = i - RGN_BLOCKS (nr_regions);
495 i++;
496
497 if (bb->next_bb == EXIT_BLOCK_PTR
498 || LABEL_P (BB_HEAD (bb->next_bb)))
499 break;
500
501 e = find_fallthru_edge (bb->succs);
502 if (! e)
503 break;
504 if (e->probability <= probability_cutoff)
505 break;
506 }
507
508 ebb_start = bb;
509 nr_regions++;
510 }
511 }
512 else
513 FOR_EACH_BB (bb)
514 {
515 rgn_bb_table[nr_regions] = bb->index;
516 RGN_NR_BLOCKS (nr_regions) = 1;
517 RGN_BLOCKS (nr_regions) = nr_regions;
518 RGN_DONT_CALC_DEPS (nr_regions) = 0;
519 RGN_HAS_REAL_EBB (nr_regions) = 0;
520
521 CONTAINING_RGN (bb->index) = nr_regions;
522 BLOCK_TO_BB (bb->index) = 0;
523 nr_regions++;
524 }
525 }
526
527 /* Estimate number of the insns in the BB. */
528 static int
529 rgn_estimate_number_of_insns (basic_block bb)
530 {
531 int count;
532
533 count = INSN_LUID (BB_END (bb)) - INSN_LUID (BB_HEAD (bb));
534
535 if (MAY_HAVE_DEBUG_INSNS)
536 {
537 rtx insn;
538
539 FOR_BB_INSNS (bb, insn)
540 if (DEBUG_INSN_P (insn))
541 count--;
542 }
543
544 return count;
545 }
546
547 /* Update number of blocks and the estimate for number of insns
548 in the region. Return true if the region is "too large" for interblock
549 scheduling (compile time considerations). */
550
551 static bool
552 too_large (int block, int *num_bbs, int *num_insns)
553 {
554 (*num_bbs)++;
555 (*num_insns) += (common_sched_info->estimate_number_of_insns
556 (BASIC_BLOCK (block)));
557
558 return ((*num_bbs > PARAM_VALUE (PARAM_MAX_SCHED_REGION_BLOCKS))
559 || (*num_insns > PARAM_VALUE (PARAM_MAX_SCHED_REGION_INSNS)));
560 }
561
562 /* Update_loop_relations(blk, hdr): Check if the loop headed by max_hdr[blk]
563 is still an inner loop. Put in max_hdr[blk] the header of the most inner
564 loop containing blk. */
565 #define UPDATE_LOOP_RELATIONS(blk, hdr) \
566 { \
567 if (max_hdr[blk] == -1) \
568 max_hdr[blk] = hdr; \
569 else if (dfs_nr[max_hdr[blk]] > dfs_nr[hdr]) \
570 RESET_BIT (inner, hdr); \
571 else if (dfs_nr[max_hdr[blk]] < dfs_nr[hdr]) \
572 { \
573 RESET_BIT (inner,max_hdr[blk]); \
574 max_hdr[blk] = hdr; \
575 } \
576 }
577
578 /* Find regions for interblock scheduling.
579
580 A region for scheduling can be:
581
582 * A loop-free procedure, or
583
584 * A reducible inner loop, or
585
586 * A basic block not contained in any other region.
587
588 ?!? In theory we could build other regions based on extended basic
589 blocks or reverse extended basic blocks. Is it worth the trouble?
590
591 Loop blocks that form a region are put into the region's block list
592 in topological order.
593
594 This procedure stores its results into the following global (ick) variables
595
596 * rgn_nr
597 * rgn_table
598 * rgn_bb_table
599 * block_to_bb
600 * containing region
601
602 We use dominator relationships to avoid making regions out of non-reducible
603 loops.
604
605 This procedure needs to be converted to work on pred/succ lists instead
606 of edge tables. That would simplify it somewhat. */
607
608 static void
609 haifa_find_rgns (void)
610 {
611 int *max_hdr, *dfs_nr, *degree;
612 char no_loops = 1;
613 int node, child, loop_head, i, head, tail;
614 int count = 0, sp, idx = 0;
615 edge_iterator current_edge;
616 edge_iterator *stack;
617 int num_bbs, num_insns, unreachable;
618 int too_large_failure;
619 basic_block bb;
620
621 /* Note if a block is a natural loop header. */
622 sbitmap header;
623
624 /* Note if a block is a natural inner loop header. */
625 sbitmap inner;
626
627 /* Note if a block is in the block queue. */
628 sbitmap in_queue;
629
630 /* Note if a block is in the block queue. */
631 sbitmap in_stack;
632
633 /* Perform a DFS traversal of the cfg. Identify loop headers, inner loops
634 and a mapping from block to its loop header (if the block is contained
635 in a loop, else -1).
636
637 Store results in HEADER, INNER, and MAX_HDR respectively, these will
638 be used as inputs to the second traversal.
639
640 STACK, SP and DFS_NR are only used during the first traversal. */
641
642 /* Allocate and initialize variables for the first traversal. */
643 max_hdr = XNEWVEC (int, last_basic_block);
644 dfs_nr = XCNEWVEC (int, last_basic_block);
645 stack = XNEWVEC (edge_iterator, n_edges);
646
647 inner = sbitmap_alloc (last_basic_block);
648 sbitmap_ones (inner);
649
650 header = sbitmap_alloc (last_basic_block);
651 sbitmap_zero (header);
652
653 in_queue = sbitmap_alloc (last_basic_block);
654 sbitmap_zero (in_queue);
655
656 in_stack = sbitmap_alloc (last_basic_block);
657 sbitmap_zero (in_stack);
658
659 for (i = 0; i < last_basic_block; i++)
660 max_hdr[i] = -1;
661
662 #define EDGE_PASSED(E) (ei_end_p ((E)) || ei_edge ((E))->aux)
663 #define SET_EDGE_PASSED(E) (ei_edge ((E))->aux = ei_edge ((E)))
664
665 /* DFS traversal to find inner loops in the cfg. */
666
667 current_edge = ei_start (single_succ (ENTRY_BLOCK_PTR)->succs);
668 sp = -1;
669
670 while (1)
671 {
672 if (EDGE_PASSED (current_edge))
673 {
674 /* We have reached a leaf node or a node that was already
675 processed. Pop edges off the stack until we find
676 an edge that has not yet been processed. */
677 while (sp >= 0 && EDGE_PASSED (current_edge))
678 {
679 /* Pop entry off the stack. */
680 current_edge = stack[sp--];
681 node = ei_edge (current_edge)->src->index;
682 gcc_assert (node != ENTRY_BLOCK);
683 child = ei_edge (current_edge)->dest->index;
684 gcc_assert (child != EXIT_BLOCK);
685 RESET_BIT (in_stack, child);
686 if (max_hdr[child] >= 0 && TEST_BIT (in_stack, max_hdr[child]))
687 UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
688 ei_next (&current_edge);
689 }
690
691 /* See if have finished the DFS tree traversal. */
692 if (sp < 0 && EDGE_PASSED (current_edge))
693 break;
694
695 /* Nope, continue the traversal with the popped node. */
696 continue;
697 }
698
699 /* Process a node. */
700 node = ei_edge (current_edge)->src->index;
701 gcc_assert (node != ENTRY_BLOCK);
702 SET_BIT (in_stack, node);
703 dfs_nr[node] = ++count;
704
705 /* We don't traverse to the exit block. */
706 child = ei_edge (current_edge)->dest->index;
707 if (child == EXIT_BLOCK)
708 {
709 SET_EDGE_PASSED (current_edge);
710 ei_next (&current_edge);
711 continue;
712 }
713
714 /* If the successor is in the stack, then we've found a loop.
715 Mark the loop, if it is not a natural loop, then it will
716 be rejected during the second traversal. */
717 if (TEST_BIT (in_stack, child))
718 {
719 no_loops = 0;
720 SET_BIT (header, child);
721 UPDATE_LOOP_RELATIONS (node, child);
722 SET_EDGE_PASSED (current_edge);
723 ei_next (&current_edge);
724 continue;
725 }
726
727 /* If the child was already visited, then there is no need to visit
728 it again. Just update the loop relationships and restart
729 with a new edge. */
730 if (dfs_nr[child])
731 {
732 if (max_hdr[child] >= 0 && TEST_BIT (in_stack, max_hdr[child]))
733 UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
734 SET_EDGE_PASSED (current_edge);
735 ei_next (&current_edge);
736 continue;
737 }
738
739 /* Push an entry on the stack and continue DFS traversal. */
740 stack[++sp] = current_edge;
741 SET_EDGE_PASSED (current_edge);
742 current_edge = ei_start (ei_edge (current_edge)->dest->succs);
743 }
744
745 /* Reset ->aux field used by EDGE_PASSED. */
746 FOR_ALL_BB (bb)
747 {
748 edge_iterator ei;
749 edge e;
750 FOR_EACH_EDGE (e, ei, bb->succs)
751 e->aux = NULL;
752 }
753
754
755 /* Another check for unreachable blocks. The earlier test in
756 is_cfg_nonregular only finds unreachable blocks that do not
757 form a loop.
758
759 The DFS traversal will mark every block that is reachable from
760 the entry node by placing a nonzero value in dfs_nr. Thus if
761 dfs_nr is zero for any block, then it must be unreachable. */
762 unreachable = 0;
763 FOR_EACH_BB (bb)
764 if (dfs_nr[bb->index] == 0)
765 {
766 unreachable = 1;
767 break;
768 }
769
770 /* Gross. To avoid wasting memory, the second pass uses the dfs_nr array
771 to hold degree counts. */
772 degree = dfs_nr;
773
774 FOR_EACH_BB (bb)
775 degree[bb->index] = EDGE_COUNT (bb->preds);
776
777 /* Do not perform region scheduling if there are any unreachable
778 blocks. */
779 if (!unreachable)
780 {
781 int *queue, *degree1 = NULL;
782 /* We use EXTENDED_RGN_HEADER as an addition to HEADER and put
783 there basic blocks, which are forced to be region heads.
784 This is done to try to assemble few smaller regions
785 from a too_large region. */
786 sbitmap extended_rgn_header = NULL;
787 bool extend_regions_p;
788
789 if (no_loops)
790 SET_BIT (header, 0);
791
792 /* Second traversal:find reducible inner loops and topologically sort
793 block of each region. */
794
795 queue = XNEWVEC (int, n_basic_blocks);
796
797 extend_regions_p = PARAM_VALUE (PARAM_MAX_SCHED_EXTEND_REGIONS_ITERS) > 0;
798 if (extend_regions_p)
799 {
800 degree1 = XNEWVEC (int, last_basic_block);
801 extended_rgn_header = sbitmap_alloc (last_basic_block);
802 sbitmap_zero (extended_rgn_header);
803 }
804
805 /* Find blocks which are inner loop headers. We still have non-reducible
806 loops to consider at this point. */
807 FOR_EACH_BB (bb)
808 {
809 if (TEST_BIT (header, bb->index) && TEST_BIT (inner, bb->index))
810 {
811 edge e;
812 edge_iterator ei;
813 basic_block jbb;
814
815 /* Now check that the loop is reducible. We do this separate
816 from finding inner loops so that we do not find a reducible
817 loop which contains an inner non-reducible loop.
818
819 A simple way to find reducible/natural loops is to verify
820 that each block in the loop is dominated by the loop
821 header.
822
823 If there exists a block that is not dominated by the loop
824 header, then the block is reachable from outside the loop
825 and thus the loop is not a natural loop. */
826 FOR_EACH_BB (jbb)
827 {
828 /* First identify blocks in the loop, except for the loop
829 entry block. */
830 if (bb->index == max_hdr[jbb->index] && bb != jbb)
831 {
832 /* Now verify that the block is dominated by the loop
833 header. */
834 if (!dominated_by_p (CDI_DOMINATORS, jbb, bb))
835 break;
836 }
837 }
838
839 /* If we exited the loop early, then I is the header of
840 a non-reducible loop and we should quit processing it
841 now. */
842 if (jbb != EXIT_BLOCK_PTR)
843 continue;
844
845 /* I is a header of an inner loop, or block 0 in a subroutine
846 with no loops at all. */
847 head = tail = -1;
848 too_large_failure = 0;
849 loop_head = max_hdr[bb->index];
850
851 if (extend_regions_p)
852 /* We save degree in case when we meet a too_large region
853 and cancel it. We need a correct degree later when
854 calling extend_rgns. */
855 memcpy (degree1, degree, last_basic_block * sizeof (int));
856
857 /* Decrease degree of all I's successors for topological
858 ordering. */
859 FOR_EACH_EDGE (e, ei, bb->succs)
860 if (e->dest != EXIT_BLOCK_PTR)
861 --degree[e->dest->index];
862
863 /* Estimate # insns, and count # blocks in the region. */
864 num_bbs = 1;
865 num_insns = common_sched_info->estimate_number_of_insns (bb);
866
867 /* Find all loop latches (blocks with back edges to the loop
868 header) or all the leaf blocks in the cfg has no loops.
869
870 Place those blocks into the queue. */
871 if (no_loops)
872 {
873 FOR_EACH_BB (jbb)
874 /* Leaf nodes have only a single successor which must
875 be EXIT_BLOCK. */
876 if (single_succ_p (jbb)
877 && single_succ (jbb) == EXIT_BLOCK_PTR)
878 {
879 queue[++tail] = jbb->index;
880 SET_BIT (in_queue, jbb->index);
881
882 if (too_large (jbb->index, &num_bbs, &num_insns))
883 {
884 too_large_failure = 1;
885 break;
886 }
887 }
888 }
889 else
890 {
891 edge e;
892
893 FOR_EACH_EDGE (e, ei, bb->preds)
894 {
895 if (e->src == ENTRY_BLOCK_PTR)
896 continue;
897
898 node = e->src->index;
899
900 if (max_hdr[node] == loop_head && node != bb->index)
901 {
902 /* This is a loop latch. */
903 queue[++tail] = node;
904 SET_BIT (in_queue, node);
905
906 if (too_large (node, &num_bbs, &num_insns))
907 {
908 too_large_failure = 1;
909 break;
910 }
911 }
912 }
913 }
914
915 /* Now add all the blocks in the loop to the queue.
916
917 We know the loop is a natural loop; however the algorithm
918 above will not always mark certain blocks as being in the
919 loop. Consider:
920 node children
921 a b,c
922 b c
923 c a,d
924 d b
925
926 The algorithm in the DFS traversal may not mark B & D as part
927 of the loop (i.e. they will not have max_hdr set to A).
928
929 We know they can not be loop latches (else they would have
930 had max_hdr set since they'd have a backedge to a dominator
931 block). So we don't need them on the initial queue.
932
933 We know they are part of the loop because they are dominated
934 by the loop header and can be reached by a backwards walk of
935 the edges starting with nodes on the initial queue.
936
937 It is safe and desirable to include those nodes in the
938 loop/scheduling region. To do so we would need to decrease
939 the degree of a node if it is the target of a backedge
940 within the loop itself as the node is placed in the queue.
941
942 We do not do this because I'm not sure that the actual
943 scheduling code will properly handle this case. ?!? */
944
945 while (head < tail && !too_large_failure)
946 {
947 edge e;
948 child = queue[++head];
949
950 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (child)->preds)
951 {
952 node = e->src->index;
953
954 /* See discussion above about nodes not marked as in
955 this loop during the initial DFS traversal. */
956 if (e->src == ENTRY_BLOCK_PTR
957 || max_hdr[node] != loop_head)
958 {
959 tail = -1;
960 break;
961 }
962 else if (!TEST_BIT (in_queue, node) && node != bb->index)
963 {
964 queue[++tail] = node;
965 SET_BIT (in_queue, node);
966
967 if (too_large (node, &num_bbs, &num_insns))
968 {
969 too_large_failure = 1;
970 break;
971 }
972 }
973 }
974 }
975
976 if (tail >= 0 && !too_large_failure)
977 {
978 /* Place the loop header into list of region blocks. */
979 degree[bb->index] = -1;
980 rgn_bb_table[idx] = bb->index;
981 RGN_NR_BLOCKS (nr_regions) = num_bbs;
982 RGN_BLOCKS (nr_regions) = idx++;
983 RGN_DONT_CALC_DEPS (nr_regions) = 0;
984 RGN_HAS_REAL_EBB (nr_regions) = 0;
985 CONTAINING_RGN (bb->index) = nr_regions;
986 BLOCK_TO_BB (bb->index) = count = 0;
987
988 /* Remove blocks from queue[] when their in degree
989 becomes zero. Repeat until no blocks are left on the
990 list. This produces a topological list of blocks in
991 the region. */
992 while (tail >= 0)
993 {
994 if (head < 0)
995 head = tail;
996 child = queue[head];
997 if (degree[child] == 0)
998 {
999 edge e;
1000
1001 degree[child] = -1;
1002 rgn_bb_table[idx++] = child;
1003 BLOCK_TO_BB (child) = ++count;
1004 CONTAINING_RGN (child) = nr_regions;
1005 queue[head] = queue[tail--];
1006
1007 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (child)->succs)
1008 if (e->dest != EXIT_BLOCK_PTR)
1009 --degree[e->dest->index];
1010 }
1011 else
1012 --head;
1013 }
1014 ++nr_regions;
1015 }
1016 else if (extend_regions_p)
1017 {
1018 /* Restore DEGREE. */
1019 int *t = degree;
1020
1021 degree = degree1;
1022 degree1 = t;
1023
1024 /* And force successors of BB to be region heads.
1025 This may provide several smaller regions instead
1026 of one too_large region. */
1027 FOR_EACH_EDGE (e, ei, bb->succs)
1028 if (e->dest != EXIT_BLOCK_PTR)
1029 SET_BIT (extended_rgn_header, e->dest->index);
1030 }
1031 }
1032 }
1033 free (queue);
1034
1035 if (extend_regions_p)
1036 {
1037 free (degree1);
1038
1039 sbitmap_a_or_b (header, header, extended_rgn_header);
1040 sbitmap_free (extended_rgn_header);
1041
1042 extend_rgns (degree, &idx, header, max_hdr);
1043 }
1044 }
1045
1046 /* Any block that did not end up in a region is placed into a region
1047 by itself. */
1048 FOR_EACH_BB (bb)
1049 if (degree[bb->index] >= 0)
1050 {
1051 rgn_bb_table[idx] = bb->index;
1052 RGN_NR_BLOCKS (nr_regions) = 1;
1053 RGN_BLOCKS (nr_regions) = idx++;
1054 RGN_DONT_CALC_DEPS (nr_regions) = 0;
1055 RGN_HAS_REAL_EBB (nr_regions) = 0;
1056 CONTAINING_RGN (bb->index) = nr_regions++;
1057 BLOCK_TO_BB (bb->index) = 0;
1058 }
1059
1060 free (max_hdr);
1061 free (degree);
1062 free (stack);
1063 sbitmap_free (header);
1064 sbitmap_free (inner);
1065 sbitmap_free (in_queue);
1066 sbitmap_free (in_stack);
1067 }
1068
1069
1070 /* Wrapper function.
1071 If FLAG_SEL_SCHED_PIPELINING is set, then use custom function to form
1072 regions. Otherwise just call find_rgns_haifa. */
1073 static void
1074 find_rgns (void)
1075 {
1076 if (sel_sched_p () && flag_sel_sched_pipelining)
1077 sel_find_rgns ();
1078 else
1079 haifa_find_rgns ();
1080 }
1081
1082 static int gather_region_statistics (int **);
1083 static void print_region_statistics (int *, int, int *, int);
1084
1085 /* Calculate the histogram that shows the number of regions having the
1086 given number of basic blocks, and store it in the RSP array. Return
1087 the size of this array. */
1088 static int
1089 gather_region_statistics (int **rsp)
1090 {
1091 int i, *a = 0, a_sz = 0;
1092
1093 /* a[i] is the number of regions that have (i + 1) basic blocks. */
1094 for (i = 0; i < nr_regions; i++)
1095 {
1096 int nr_blocks = RGN_NR_BLOCKS (i);
1097
1098 gcc_assert (nr_blocks >= 1);
1099
1100 if (nr_blocks > a_sz)
1101 {
1102 a = XRESIZEVEC (int, a, nr_blocks);
1103 do
1104 a[a_sz++] = 0;
1105 while (a_sz != nr_blocks);
1106 }
1107
1108 a[nr_blocks - 1]++;
1109 }
1110
1111 *rsp = a;
1112 return a_sz;
1113 }
1114
1115 /* Print regions statistics. S1 and S2 denote the data before and after
1116 calling extend_rgns, respectively. */
1117 static void
1118 print_region_statistics (int *s1, int s1_sz, int *s2, int s2_sz)
1119 {
1120 int i;
1121
1122 /* We iterate until s2_sz because extend_rgns does not decrease
1123 the maximal region size. */
1124 for (i = 1; i < s2_sz; i++)
1125 {
1126 int n1, n2;
1127
1128 n2 = s2[i];
1129
1130 if (n2 == 0)
1131 continue;
1132
1133 if (i >= s1_sz)
1134 n1 = 0;
1135 else
1136 n1 = s1[i];
1137
1138 fprintf (sched_dump, ";; Region extension statistics: size %d: " \
1139 "was %d + %d more\n", i + 1, n1, n2 - n1);
1140 }
1141 }
1142
1143 /* Extend regions.
1144 DEGREE - Array of incoming edge count, considering only
1145 the edges, that don't have their sources in formed regions yet.
1146 IDXP - pointer to the next available index in rgn_bb_table.
1147 HEADER - set of all region heads.
1148 LOOP_HDR - mapping from block to the containing loop
1149 (two blocks can reside within one region if they have
1150 the same loop header). */
1151 void
1152 extend_rgns (int *degree, int *idxp, sbitmap header, int *loop_hdr)
1153 {
1154 int *order, i, rescan = 0, idx = *idxp, iter = 0, max_iter, *max_hdr;
1155 int nblocks = n_basic_blocks - NUM_FIXED_BLOCKS;
1156
1157 max_iter = PARAM_VALUE (PARAM_MAX_SCHED_EXTEND_REGIONS_ITERS);
1158
1159 max_hdr = XNEWVEC (int, last_basic_block);
1160
1161 order = XNEWVEC (int, last_basic_block);
1162 post_order_compute (order, false, false);
1163
1164 for (i = nblocks - 1; i >= 0; i--)
1165 {
1166 int bbn = order[i];
1167 if (degree[bbn] >= 0)
1168 {
1169 max_hdr[bbn] = bbn;
1170 rescan = 1;
1171 }
1172 else
1173 /* This block already was processed in find_rgns. */
1174 max_hdr[bbn] = -1;
1175 }
1176
1177 /* The idea is to topologically walk through CFG in top-down order.
1178 During the traversal, if all the predecessors of a node are
1179 marked to be in the same region (they all have the same max_hdr),
1180 then current node is also marked to be a part of that region.
1181 Otherwise the node starts its own region.
1182 CFG should be traversed until no further changes are made. On each
1183 iteration the set of the region heads is extended (the set of those
1184 blocks that have max_hdr[bbi] == bbi). This set is upper bounded by the
1185 set of all basic blocks, thus the algorithm is guaranteed to
1186 terminate. */
1187
1188 while (rescan && iter < max_iter)
1189 {
1190 rescan = 0;
1191
1192 for (i = nblocks - 1; i >= 0; i--)
1193 {
1194 edge e;
1195 edge_iterator ei;
1196 int bbn = order[i];
1197
1198 if (max_hdr[bbn] != -1 && !TEST_BIT (header, bbn))
1199 {
1200 int hdr = -1;
1201
1202 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (bbn)->preds)
1203 {
1204 int predn = e->src->index;
1205
1206 if (predn != ENTRY_BLOCK
1207 /* If pred wasn't processed in find_rgns. */
1208 && max_hdr[predn] != -1
1209 /* And pred and bb reside in the same loop.
1210 (Or out of any loop). */
1211 && loop_hdr[bbn] == loop_hdr[predn])
1212 {
1213 if (hdr == -1)
1214 /* Then bb extends the containing region of pred. */
1215 hdr = max_hdr[predn];
1216 else if (hdr != max_hdr[predn])
1217 /* Too bad, there are at least two predecessors
1218 that reside in different regions. Thus, BB should
1219 begin its own region. */
1220 {
1221 hdr = bbn;
1222 break;
1223 }
1224 }
1225 else
1226 /* BB starts its own region. */
1227 {
1228 hdr = bbn;
1229 break;
1230 }
1231 }
1232
1233 if (hdr == bbn)
1234 {
1235 /* If BB start its own region,
1236 update set of headers with BB. */
1237 SET_BIT (header, bbn);
1238 rescan = 1;
1239 }
1240 else
1241 gcc_assert (hdr != -1);
1242
1243 max_hdr[bbn] = hdr;
1244 }
1245 }
1246
1247 iter++;
1248 }
1249
1250 /* Statistics were gathered on the SPEC2000 package of tests with
1251 mainline weekly snapshot gcc-4.1-20051015 on ia64.
1252
1253 Statistics for SPECint:
1254 1 iteration : 1751 cases (38.7%)
1255 2 iterations: 2770 cases (61.3%)
1256 Blocks wrapped in regions by find_rgns without extension: 18295 blocks
1257 Blocks wrapped in regions by 2 iterations in extend_rgns: 23821 blocks
1258 (We don't count single block regions here).
1259
1260 Statistics for SPECfp:
1261 1 iteration : 621 cases (35.9%)
1262 2 iterations: 1110 cases (64.1%)
1263 Blocks wrapped in regions by find_rgns without extension: 6476 blocks
1264 Blocks wrapped in regions by 2 iterations in extend_rgns: 11155 blocks
1265 (We don't count single block regions here).
1266
1267 By default we do at most 2 iterations.
1268 This can be overridden with max-sched-extend-regions-iters parameter:
1269 0 - disable region extension,
1270 N > 0 - do at most N iterations. */
1271
1272 if (sched_verbose && iter != 0)
1273 fprintf (sched_dump, ";; Region extension iterations: %d%s\n", iter,
1274 rescan ? "... failed" : "");
1275
1276 if (!rescan && iter != 0)
1277 {
1278 int *s1 = NULL, s1_sz = 0;
1279
1280 /* Save the old statistics for later printout. */
1281 if (sched_verbose >= 6)
1282 s1_sz = gather_region_statistics (&s1);
1283
1284 /* We have succeeded. Now assemble the regions. */
1285 for (i = nblocks - 1; i >= 0; i--)
1286 {
1287 int bbn = order[i];
1288
1289 if (max_hdr[bbn] == bbn)
1290 /* BBN is a region head. */
1291 {
1292 edge e;
1293 edge_iterator ei;
1294 int num_bbs = 0, j, num_insns = 0, large;
1295
1296 large = too_large (bbn, &num_bbs, &num_insns);
1297
1298 degree[bbn] = -1;
1299 rgn_bb_table[idx] = bbn;
1300 RGN_BLOCKS (nr_regions) = idx++;
1301 RGN_DONT_CALC_DEPS (nr_regions) = 0;
1302 RGN_HAS_REAL_EBB (nr_regions) = 0;
1303 CONTAINING_RGN (bbn) = nr_regions;
1304 BLOCK_TO_BB (bbn) = 0;
1305
1306 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (bbn)->succs)
1307 if (e->dest != EXIT_BLOCK_PTR)
1308 degree[e->dest->index]--;
1309
1310 if (!large)
1311 /* Here we check whether the region is too_large. */
1312 for (j = i - 1; j >= 0; j--)
1313 {
1314 int succn = order[j];
1315 if (max_hdr[succn] == bbn)
1316 {
1317 if ((large = too_large (succn, &num_bbs, &num_insns)))
1318 break;
1319 }
1320 }
1321
1322 if (large)
1323 /* If the region is too_large, then wrap every block of
1324 the region into single block region.
1325 Here we wrap region head only. Other blocks are
1326 processed in the below cycle. */
1327 {
1328 RGN_NR_BLOCKS (nr_regions) = 1;
1329 nr_regions++;
1330 }
1331
1332 num_bbs = 1;
1333
1334 for (j = i - 1; j >= 0; j--)
1335 {
1336 int succn = order[j];
1337
1338 if (max_hdr[succn] == bbn)
1339 /* This cycle iterates over all basic blocks, that
1340 are supposed to be in the region with head BBN,
1341 and wraps them into that region (or in single
1342 block region). */
1343 {
1344 gcc_assert (degree[succn] == 0);
1345
1346 degree[succn] = -1;
1347 rgn_bb_table[idx] = succn;
1348 BLOCK_TO_BB (succn) = large ? 0 : num_bbs++;
1349 CONTAINING_RGN (succn) = nr_regions;
1350
1351 if (large)
1352 /* Wrap SUCCN into single block region. */
1353 {
1354 RGN_BLOCKS (nr_regions) = idx;
1355 RGN_NR_BLOCKS (nr_regions) = 1;
1356 RGN_DONT_CALC_DEPS (nr_regions) = 0;
1357 RGN_HAS_REAL_EBB (nr_regions) = 0;
1358 nr_regions++;
1359 }
1360
1361 idx++;
1362
1363 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (succn)->succs)
1364 if (e->dest != EXIT_BLOCK_PTR)
1365 degree[e->dest->index]--;
1366 }
1367 }
1368
1369 if (!large)
1370 {
1371 RGN_NR_BLOCKS (nr_regions) = num_bbs;
1372 nr_regions++;
1373 }
1374 }
1375 }
1376
1377 if (sched_verbose >= 6)
1378 {
1379 int *s2, s2_sz;
1380
1381 /* Get the new statistics and print the comparison with the
1382 one before calling this function. */
1383 s2_sz = gather_region_statistics (&s2);
1384 print_region_statistics (s1, s1_sz, s2, s2_sz);
1385 free (s1);
1386 free (s2);
1387 }
1388 }
1389
1390 free (order);
1391 free (max_hdr);
1392
1393 *idxp = idx;
1394 }
1395
1396 /* Functions for regions scheduling information. */
1397
1398 /* Compute dominators, probability, and potential-split-edges of bb.
1399 Assume that these values were already computed for bb's predecessors. */
1400
1401 static void
1402 compute_dom_prob_ps (int bb)
1403 {
1404 edge_iterator in_ei;
1405 edge in_edge;
1406
1407 /* We shouldn't have any real ebbs yet. */
1408 gcc_assert (ebb_head [bb] == bb + current_blocks);
1409
1410 if (IS_RGN_ENTRY (bb))
1411 {
1412 SET_BIT (dom[bb], 0);
1413 prob[bb] = REG_BR_PROB_BASE;
1414 return;
1415 }
1416
1417 prob[bb] = 0;
1418
1419 /* Initialize dom[bb] to '111..1'. */
1420 sbitmap_ones (dom[bb]);
1421
1422 FOR_EACH_EDGE (in_edge, in_ei, BASIC_BLOCK (BB_TO_BLOCK (bb))->preds)
1423 {
1424 int pred_bb;
1425 edge out_edge;
1426 edge_iterator out_ei;
1427
1428 if (in_edge->src == ENTRY_BLOCK_PTR)
1429 continue;
1430
1431 pred_bb = BLOCK_TO_BB (in_edge->src->index);
1432 sbitmap_a_and_b (dom[bb], dom[bb], dom[pred_bb]);
1433 sbitmap_a_or_b (ancestor_edges[bb],
1434 ancestor_edges[bb], ancestor_edges[pred_bb]);
1435
1436 SET_BIT (ancestor_edges[bb], EDGE_TO_BIT (in_edge));
1437
1438 sbitmap_a_or_b (pot_split[bb], pot_split[bb], pot_split[pred_bb]);
1439
1440 FOR_EACH_EDGE (out_edge, out_ei, in_edge->src->succs)
1441 SET_BIT (pot_split[bb], EDGE_TO_BIT (out_edge));
1442
1443 prob[bb] += ((prob[pred_bb] * in_edge->probability) / REG_BR_PROB_BASE);
1444 }
1445
1446 SET_BIT (dom[bb], bb);
1447 sbitmap_difference (pot_split[bb], pot_split[bb], ancestor_edges[bb]);
1448
1449 if (sched_verbose >= 2)
1450 fprintf (sched_dump, ";; bb_prob(%d, %d) = %3d\n", bb, BB_TO_BLOCK (bb),
1451 (100 * prob[bb]) / REG_BR_PROB_BASE);
1452 }
1453
1454 /* Functions for target info. */
1455
1456 /* Compute in BL the list of split-edges of bb_src relatively to bb_trg.
1457 Note that bb_trg dominates bb_src. */
1458
1459 static void
1460 split_edges (int bb_src, int bb_trg, edgelst *bl)
1461 {
1462 sbitmap src = sbitmap_alloc (pot_split[bb_src]->n_bits);
1463 sbitmap_copy (src, pot_split[bb_src]);
1464
1465 sbitmap_difference (src, src, pot_split[bb_trg]);
1466 extract_edgelst (src, bl);
1467 sbitmap_free (src);
1468 }
1469
1470 /* Find the valid candidate-source-blocks for the target block TRG, compute
1471 their probability, and check if they are speculative or not.
1472 For speculative sources, compute their update-blocks and split-blocks. */
1473
1474 static void
1475 compute_trg_info (int trg)
1476 {
1477 candidate *sp;
1478 edgelst el = { NULL, 0 };
1479 int i, j, k, update_idx;
1480 basic_block block;
1481 sbitmap visited;
1482 edge_iterator ei;
1483 edge e;
1484
1485 candidate_table = XNEWVEC (candidate, current_nr_blocks);
1486
1487 bblst_last = 0;
1488 /* bblst_table holds split blocks and update blocks for each block after
1489 the current one in the region. split blocks and update blocks are
1490 the TO blocks of region edges, so there can be at most rgn_nr_edges
1491 of them. */
1492 bblst_size = (current_nr_blocks - target_bb) * rgn_nr_edges;
1493 bblst_table = XNEWVEC (basic_block, bblst_size);
1494
1495 edgelst_last = 0;
1496 edgelst_table = XNEWVEC (edge, rgn_nr_edges);
1497
1498 /* Define some of the fields for the target bb as well. */
1499 sp = candidate_table + trg;
1500 sp->is_valid = 1;
1501 sp->is_speculative = 0;
1502 sp->src_prob = REG_BR_PROB_BASE;
1503
1504 visited = sbitmap_alloc (last_basic_block);
1505
1506 for (i = trg + 1; i < current_nr_blocks; i++)
1507 {
1508 sp = candidate_table + i;
1509
1510 sp->is_valid = IS_DOMINATED (i, trg);
1511 if (sp->is_valid)
1512 {
1513 int tf = prob[trg], cf = prob[i];
1514
1515 /* In CFGs with low probability edges TF can possibly be zero. */
1516 sp->src_prob = (tf ? ((cf * REG_BR_PROB_BASE) / tf) : 0);
1517 sp->is_valid = (sp->src_prob >= min_spec_prob);
1518 }
1519
1520 if (sp->is_valid)
1521 {
1522 split_edges (i, trg, &el);
1523 sp->is_speculative = (el.nr_members) ? 1 : 0;
1524 if (sp->is_speculative && !flag_schedule_speculative)
1525 sp->is_valid = 0;
1526 }
1527
1528 if (sp->is_valid)
1529 {
1530 /* Compute split blocks and store them in bblst_table.
1531 The TO block of every split edge is a split block. */
1532 sp->split_bbs.first_member = &bblst_table[bblst_last];
1533 sp->split_bbs.nr_members = el.nr_members;
1534 for (j = 0; j < el.nr_members; bblst_last++, j++)
1535 bblst_table[bblst_last] = el.first_member[j]->dest;
1536 sp->update_bbs.first_member = &bblst_table[bblst_last];
1537
1538 /* Compute update blocks and store them in bblst_table.
1539 For every split edge, look at the FROM block, and check
1540 all out edges. For each out edge that is not a split edge,
1541 add the TO block to the update block list. This list can end
1542 up with a lot of duplicates. We need to weed them out to avoid
1543 overrunning the end of the bblst_table. */
1544
1545 update_idx = 0;
1546 sbitmap_zero (visited);
1547 for (j = 0; j < el.nr_members; j++)
1548 {
1549 block = el.first_member[j]->src;
1550 FOR_EACH_EDGE (e, ei, block->succs)
1551 {
1552 if (!TEST_BIT (visited, e->dest->index))
1553 {
1554 for (k = 0; k < el.nr_members; k++)
1555 if (e == el.first_member[k])
1556 break;
1557
1558 if (k >= el.nr_members)
1559 {
1560 bblst_table[bblst_last++] = e->dest;
1561 SET_BIT (visited, e->dest->index);
1562 update_idx++;
1563 }
1564 }
1565 }
1566 }
1567 sp->update_bbs.nr_members = update_idx;
1568
1569 /* Make sure we didn't overrun the end of bblst_table. */
1570 gcc_assert (bblst_last <= bblst_size);
1571 }
1572 else
1573 {
1574 sp->split_bbs.nr_members = sp->update_bbs.nr_members = 0;
1575
1576 sp->is_speculative = 0;
1577 sp->src_prob = 0;
1578 }
1579 }
1580
1581 sbitmap_free (visited);
1582 }
1583
1584 /* Free the computed target info. */
1585 static void
1586 free_trg_info (void)
1587 {
1588 free (candidate_table);
1589 free (bblst_table);
1590 free (edgelst_table);
1591 }
1592
1593 /* Print candidates info, for debugging purposes. Callable from debugger. */
1594
1595 DEBUG_FUNCTION void
1596 debug_candidate (int i)
1597 {
1598 if (!candidate_table[i].is_valid)
1599 return;
1600
1601 if (candidate_table[i].is_speculative)
1602 {
1603 int j;
1604 fprintf (sched_dump, "src b %d bb %d speculative \n", BB_TO_BLOCK (i), i);
1605
1606 fprintf (sched_dump, "split path: ");
1607 for (j = 0; j < candidate_table[i].split_bbs.nr_members; j++)
1608 {
1609 int b = candidate_table[i].split_bbs.first_member[j]->index;
1610
1611 fprintf (sched_dump, " %d ", b);
1612 }
1613 fprintf (sched_dump, "\n");
1614
1615 fprintf (sched_dump, "update path: ");
1616 for (j = 0; j < candidate_table[i].update_bbs.nr_members; j++)
1617 {
1618 int b = candidate_table[i].update_bbs.first_member[j]->index;
1619
1620 fprintf (sched_dump, " %d ", b);
1621 }
1622 fprintf (sched_dump, "\n");
1623 }
1624 else
1625 {
1626 fprintf (sched_dump, " src %d equivalent\n", BB_TO_BLOCK (i));
1627 }
1628 }
1629
1630 /* Print candidates info, for debugging purposes. Callable from debugger. */
1631
1632 DEBUG_FUNCTION void
1633 debug_candidates (int trg)
1634 {
1635 int i;
1636
1637 fprintf (sched_dump, "----------- candidate table: target: b=%d bb=%d ---\n",
1638 BB_TO_BLOCK (trg), trg);
1639 for (i = trg + 1; i < current_nr_blocks; i++)
1640 debug_candidate (i);
1641 }
1642
1643 /* Functions for speculative scheduling. */
1644
1645 static bitmap_head not_in_df;
1646
1647 /* Return 0 if x is a set of a register alive in the beginning of one
1648 of the split-blocks of src, otherwise return 1. */
1649
1650 static int
1651 check_live_1 (int src, rtx x)
1652 {
1653 int i;
1654 int regno;
1655 rtx reg = SET_DEST (x);
1656
1657 if (reg == 0)
1658 return 1;
1659
1660 while (GET_CODE (reg) == SUBREG
1661 || GET_CODE (reg) == ZERO_EXTRACT
1662 || GET_CODE (reg) == STRICT_LOW_PART)
1663 reg = XEXP (reg, 0);
1664
1665 if (GET_CODE (reg) == PARALLEL)
1666 {
1667 int i;
1668
1669 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
1670 if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
1671 if (check_live_1 (src, XEXP (XVECEXP (reg, 0, i), 0)))
1672 return 1;
1673
1674 return 0;
1675 }
1676
1677 if (!REG_P (reg))
1678 return 1;
1679
1680 regno = REGNO (reg);
1681
1682 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
1683 {
1684 /* Global registers are assumed live. */
1685 return 0;
1686 }
1687 else
1688 {
1689 if (regno < FIRST_PSEUDO_REGISTER)
1690 {
1691 /* Check for hard registers. */
1692 int j = hard_regno_nregs[regno][GET_MODE (reg)];
1693 while (--j >= 0)
1694 {
1695 for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
1696 {
1697 basic_block b = candidate_table[src].split_bbs.first_member[i];
1698 int t = bitmap_bit_p (&not_in_df, b->index);
1699
1700 /* We can have split blocks, that were recently generated.
1701 Such blocks are always outside current region. */
1702 gcc_assert (!t || (CONTAINING_RGN (b->index)
1703 != CONTAINING_RGN (BB_TO_BLOCK (src))));
1704
1705 if (t || REGNO_REG_SET_P (df_get_live_in (b), regno + j))
1706 return 0;
1707 }
1708 }
1709 }
1710 else
1711 {
1712 /* Check for pseudo registers. */
1713 for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
1714 {
1715 basic_block b = candidate_table[src].split_bbs.first_member[i];
1716 int t = bitmap_bit_p (&not_in_df, b->index);
1717
1718 gcc_assert (!t || (CONTAINING_RGN (b->index)
1719 != CONTAINING_RGN (BB_TO_BLOCK (src))));
1720
1721 if (t || REGNO_REG_SET_P (df_get_live_in (b), regno))
1722 return 0;
1723 }
1724 }
1725 }
1726
1727 return 1;
1728 }
1729
1730 /* If x is a set of a register R, mark that R is alive in the beginning
1731 of every update-block of src. */
1732
1733 static void
1734 update_live_1 (int src, rtx x)
1735 {
1736 int i;
1737 int regno;
1738 rtx reg = SET_DEST (x);
1739
1740 if (reg == 0)
1741 return;
1742
1743 while (GET_CODE (reg) == SUBREG
1744 || GET_CODE (reg) == ZERO_EXTRACT
1745 || GET_CODE (reg) == STRICT_LOW_PART)
1746 reg = XEXP (reg, 0);
1747
1748 if (GET_CODE (reg) == PARALLEL)
1749 {
1750 int i;
1751
1752 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
1753 if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
1754 update_live_1 (src, XEXP (XVECEXP (reg, 0, i), 0));
1755
1756 return;
1757 }
1758
1759 if (!REG_P (reg))
1760 return;
1761
1762 /* Global registers are always live, so the code below does not apply
1763 to them. */
1764
1765 regno = REGNO (reg);
1766
1767 if (regno >= FIRST_PSEUDO_REGISTER || !global_regs[regno])
1768 {
1769 if (regno < FIRST_PSEUDO_REGISTER)
1770 {
1771 int j = hard_regno_nregs[regno][GET_MODE (reg)];
1772 while (--j >= 0)
1773 {
1774 for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
1775 {
1776 basic_block b = candidate_table[src].update_bbs.first_member[i];
1777
1778 SET_REGNO_REG_SET (df_get_live_in (b), regno + j);
1779 }
1780 }
1781 }
1782 else
1783 {
1784 for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
1785 {
1786 basic_block b = candidate_table[src].update_bbs.first_member[i];
1787
1788 SET_REGNO_REG_SET (df_get_live_in (b), regno);
1789 }
1790 }
1791 }
1792 }
1793
1794 /* Return 1 if insn can be speculatively moved from block src to trg,
1795 otherwise return 0. Called before first insertion of insn to
1796 ready-list or before the scheduling. */
1797
1798 static int
1799 check_live (rtx insn, int src)
1800 {
1801 /* Find the registers set by instruction. */
1802 if (GET_CODE (PATTERN (insn)) == SET
1803 || GET_CODE (PATTERN (insn)) == CLOBBER)
1804 return check_live_1 (src, PATTERN (insn));
1805 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1806 {
1807 int j;
1808 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
1809 if ((GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
1810 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
1811 && !check_live_1 (src, XVECEXP (PATTERN (insn), 0, j)))
1812 return 0;
1813
1814 return 1;
1815 }
1816
1817 return 1;
1818 }
1819
1820 /* Update the live registers info after insn was moved speculatively from
1821 block src to trg. */
1822
1823 static void
1824 update_live (rtx insn, int src)
1825 {
1826 /* Find the registers set by instruction. */
1827 if (GET_CODE (PATTERN (insn)) == SET
1828 || GET_CODE (PATTERN (insn)) == CLOBBER)
1829 update_live_1 (src, PATTERN (insn));
1830 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1831 {
1832 int j;
1833 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
1834 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
1835 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
1836 update_live_1 (src, XVECEXP (PATTERN (insn), 0, j));
1837 }
1838 }
1839
1840 /* Nonzero if block bb_to is equal to, or reachable from block bb_from. */
1841 #define IS_REACHABLE(bb_from, bb_to) \
1842 (bb_from == bb_to \
1843 || IS_RGN_ENTRY (bb_from) \
1844 || (TEST_BIT (ancestor_edges[bb_to], \
1845 EDGE_TO_BIT (single_pred_edge (BASIC_BLOCK (BB_TO_BLOCK (bb_from)))))))
1846
1847 /* Turns on the fed_by_spec_load flag for insns fed by load_insn. */
1848
1849 static void
1850 set_spec_fed (rtx load_insn)
1851 {
1852 sd_iterator_def sd_it;
1853 dep_t dep;
1854
1855 FOR_EACH_DEP (load_insn, SD_LIST_FORW, sd_it, dep)
1856 if (DEP_TYPE (dep) == REG_DEP_TRUE)
1857 FED_BY_SPEC_LOAD (DEP_CON (dep)) = 1;
1858 }
1859
1860 /* On the path from the insn to load_insn_bb, find a conditional
1861 branch depending on insn, that guards the speculative load. */
1862
1863 static int
1864 find_conditional_protection (rtx insn, int load_insn_bb)
1865 {
1866 sd_iterator_def sd_it;
1867 dep_t dep;
1868
1869 /* Iterate through DEF-USE forward dependences. */
1870 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
1871 {
1872 rtx next = DEP_CON (dep);
1873
1874 if ((CONTAINING_RGN (BLOCK_NUM (next)) ==
1875 CONTAINING_RGN (BB_TO_BLOCK (load_insn_bb)))
1876 && IS_REACHABLE (INSN_BB (next), load_insn_bb)
1877 && load_insn_bb != INSN_BB (next)
1878 && DEP_TYPE (dep) == REG_DEP_TRUE
1879 && (JUMP_P (next)
1880 || find_conditional_protection (next, load_insn_bb)))
1881 return 1;
1882 }
1883 return 0;
1884 } /* find_conditional_protection */
1885
1886 /* Returns 1 if the same insn1 that participates in the computation
1887 of load_insn's address is feeding a conditional branch that is
1888 guarding on load_insn. This is true if we find two DEF-USE
1889 chains:
1890 insn1 -> ... -> conditional-branch
1891 insn1 -> ... -> load_insn,
1892 and if a flow path exists:
1893 insn1 -> ... -> conditional-branch -> ... -> load_insn,
1894 and if insn1 is on the path
1895 region-entry -> ... -> bb_trg -> ... load_insn.
1896
1897 Locate insn1 by climbing on INSN_BACK_DEPS from load_insn.
1898 Locate the branch by following INSN_FORW_DEPS from insn1. */
1899
1900 static int
1901 is_conditionally_protected (rtx load_insn, int bb_src, int bb_trg)
1902 {
1903 sd_iterator_def sd_it;
1904 dep_t dep;
1905
1906 FOR_EACH_DEP (load_insn, SD_LIST_BACK, sd_it, dep)
1907 {
1908 rtx insn1 = DEP_PRO (dep);
1909
1910 /* Must be a DEF-USE dependence upon non-branch. */
1911 if (DEP_TYPE (dep) != REG_DEP_TRUE
1912 || JUMP_P (insn1))
1913 continue;
1914
1915 /* Must exist a path: region-entry -> ... -> bb_trg -> ... load_insn. */
1916 if (INSN_BB (insn1) == bb_src
1917 || (CONTAINING_RGN (BLOCK_NUM (insn1))
1918 != CONTAINING_RGN (BB_TO_BLOCK (bb_src)))
1919 || (!IS_REACHABLE (bb_trg, INSN_BB (insn1))
1920 && !IS_REACHABLE (INSN_BB (insn1), bb_trg)))
1921 continue;
1922
1923 /* Now search for the conditional-branch. */
1924 if (find_conditional_protection (insn1, bb_src))
1925 return 1;
1926
1927 /* Recursive step: search another insn1, "above" current insn1. */
1928 return is_conditionally_protected (insn1, bb_src, bb_trg);
1929 }
1930
1931 /* The chain does not exist. */
1932 return 0;
1933 } /* is_conditionally_protected */
1934
1935 /* Returns 1 if a clue for "similar load" 'insn2' is found, and hence
1936 load_insn can move speculatively from bb_src to bb_trg. All the
1937 following must hold:
1938
1939 (1) both loads have 1 base register (PFREE_CANDIDATEs).
1940 (2) load_insn and load1 have a def-use dependence upon
1941 the same insn 'insn1'.
1942 (3) either load2 is in bb_trg, or:
1943 - there's only one split-block, and
1944 - load1 is on the escape path, and
1945
1946 From all these we can conclude that the two loads access memory
1947 addresses that differ at most by a constant, and hence if moving
1948 load_insn would cause an exception, it would have been caused by
1949 load2 anyhow. */
1950
1951 static int
1952 is_pfree (rtx load_insn, int bb_src, int bb_trg)
1953 {
1954 sd_iterator_def back_sd_it;
1955 dep_t back_dep;
1956 candidate *candp = candidate_table + bb_src;
1957
1958 if (candp->split_bbs.nr_members != 1)
1959 /* Must have exactly one escape block. */
1960 return 0;
1961
1962 FOR_EACH_DEP (load_insn, SD_LIST_BACK, back_sd_it, back_dep)
1963 {
1964 rtx insn1 = DEP_PRO (back_dep);
1965
1966 if (DEP_TYPE (back_dep) == REG_DEP_TRUE)
1967 /* Found a DEF-USE dependence (insn1, load_insn). */
1968 {
1969 sd_iterator_def fore_sd_it;
1970 dep_t fore_dep;
1971
1972 FOR_EACH_DEP (insn1, SD_LIST_FORW, fore_sd_it, fore_dep)
1973 {
1974 rtx insn2 = DEP_CON (fore_dep);
1975
1976 if (DEP_TYPE (fore_dep) == REG_DEP_TRUE)
1977 {
1978 /* Found a DEF-USE dependence (insn1, insn2). */
1979 if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
1980 /* insn2 not guaranteed to be a 1 base reg load. */
1981 continue;
1982
1983 if (INSN_BB (insn2) == bb_trg)
1984 /* insn2 is the similar load, in the target block. */
1985 return 1;
1986
1987 if (*(candp->split_bbs.first_member) == BLOCK_FOR_INSN (insn2))
1988 /* insn2 is a similar load, in a split-block. */
1989 return 1;
1990 }
1991 }
1992 }
1993 }
1994
1995 /* Couldn't find a similar load. */
1996 return 0;
1997 } /* is_pfree */
1998
1999 /* Return 1 if load_insn is prisky (i.e. if load_insn is fed by
2000 a load moved speculatively, or if load_insn is protected by
2001 a compare on load_insn's address). */
2002
2003 static int
2004 is_prisky (rtx load_insn, int bb_src, int bb_trg)
2005 {
2006 if (FED_BY_SPEC_LOAD (load_insn))
2007 return 1;
2008
2009 if (sd_lists_empty_p (load_insn, SD_LIST_BACK))
2010 /* Dependence may 'hide' out of the region. */
2011 return 1;
2012
2013 if (is_conditionally_protected (load_insn, bb_src, bb_trg))
2014 return 1;
2015
2016 return 0;
2017 }
2018
2019 /* Insn is a candidate to be moved speculatively from bb_src to bb_trg.
2020 Return 1 if insn is exception-free (and the motion is valid)
2021 and 0 otherwise. */
2022
2023 static int
2024 is_exception_free (rtx insn, int bb_src, int bb_trg)
2025 {
2026 int insn_class = haifa_classify_insn (insn);
2027
2028 /* Handle non-load insns. */
2029 switch (insn_class)
2030 {
2031 case TRAP_FREE:
2032 return 1;
2033 case TRAP_RISKY:
2034 return 0;
2035 default:;
2036 }
2037
2038 /* Handle loads. */
2039 if (!flag_schedule_speculative_load)
2040 return 0;
2041 IS_LOAD_INSN (insn) = 1;
2042 switch (insn_class)
2043 {
2044 case IFREE:
2045 return (1);
2046 case IRISKY:
2047 return 0;
2048 case PFREE_CANDIDATE:
2049 if (is_pfree (insn, bb_src, bb_trg))
2050 return 1;
2051 /* Don't 'break' here: PFREE-candidate is also PRISKY-candidate. */
2052 case PRISKY_CANDIDATE:
2053 if (!flag_schedule_speculative_load_dangerous
2054 || is_prisky (insn, bb_src, bb_trg))
2055 return 0;
2056 break;
2057 default:;
2058 }
2059
2060 return flag_schedule_speculative_load_dangerous;
2061 }
2062 \f
2063 /* The number of insns from the current block scheduled so far. */
2064 static int sched_target_n_insns;
2065 /* The number of insns from the current block to be scheduled in total. */
2066 static int target_n_insns;
2067 /* The number of insns from the entire region scheduled so far. */
2068 static int sched_n_insns;
2069
2070 /* Implementations of the sched_info functions for region scheduling. */
2071 static void init_ready_list (void);
2072 static int can_schedule_ready_p (rtx);
2073 static void begin_schedule_ready (rtx, rtx);
2074 static ds_t new_ready (rtx, ds_t);
2075 static int schedule_more_p (void);
2076 static const char *rgn_print_insn (const_rtx, int);
2077 static int rgn_rank (rtx, rtx);
2078 static void compute_jump_reg_dependencies (rtx, regset, regset, regset);
2079
2080 /* Functions for speculative scheduling. */
2081 static void rgn_add_remove_insn (rtx, int);
2082 static void rgn_add_block (basic_block, basic_block);
2083 static void rgn_fix_recovery_cfg (int, int, int);
2084 static basic_block advance_target_bb (basic_block, rtx);
2085
2086 /* Return nonzero if there are more insns that should be scheduled. */
2087
2088 static int
2089 schedule_more_p (void)
2090 {
2091 return sched_target_n_insns < target_n_insns;
2092 }
2093
2094 /* Add all insns that are initially ready to the ready list READY. Called
2095 once before scheduling a set of insns. */
2096
2097 static void
2098 init_ready_list (void)
2099 {
2100 rtx prev_head = current_sched_info->prev_head;
2101 rtx next_tail = current_sched_info->next_tail;
2102 int bb_src;
2103 rtx insn;
2104
2105 target_n_insns = 0;
2106 sched_target_n_insns = 0;
2107 sched_n_insns = 0;
2108
2109 /* Print debugging information. */
2110 if (sched_verbose >= 5)
2111 debug_rgn_dependencies (target_bb);
2112
2113 /* Prepare current target block info. */
2114 if (current_nr_blocks > 1)
2115 compute_trg_info (target_bb);
2116
2117 /* Initialize ready list with all 'ready' insns in target block.
2118 Count number of insns in the target block being scheduled. */
2119 for (insn = NEXT_INSN (prev_head); insn != next_tail; insn = NEXT_INSN (insn))
2120 {
2121 try_ready (insn);
2122 target_n_insns++;
2123
2124 gcc_assert (!(TODO_SPEC (insn) & BEGIN_CONTROL));
2125 }
2126
2127 /* Add to ready list all 'ready' insns in valid source blocks.
2128 For speculative insns, check-live, exception-free, and
2129 issue-delay. */
2130 for (bb_src = target_bb + 1; bb_src < current_nr_blocks; bb_src++)
2131 if (IS_VALID (bb_src))
2132 {
2133 rtx src_head;
2134 rtx src_next_tail;
2135 rtx tail, head;
2136
2137 get_ebb_head_tail (EBB_FIRST_BB (bb_src), EBB_LAST_BB (bb_src),
2138 &head, &tail);
2139 src_next_tail = NEXT_INSN (tail);
2140 src_head = head;
2141
2142 for (insn = src_head; insn != src_next_tail; insn = NEXT_INSN (insn))
2143 if (INSN_P (insn) && !BOUNDARY_DEBUG_INSN_P (insn))
2144 try_ready (insn);
2145 }
2146 }
2147
2148 /* Called after taking INSN from the ready list. Returns nonzero if this
2149 insn can be scheduled, nonzero if we should silently discard it. */
2150
2151 static int
2152 can_schedule_ready_p (rtx insn)
2153 {
2154 /* An interblock motion? */
2155 if (INSN_BB (insn) != target_bb
2156 && IS_SPECULATIVE_INSN (insn)
2157 && !check_live (insn, INSN_BB (insn)))
2158 return 0;
2159 else
2160 return 1;
2161 }
2162
2163 /* Updates counter and other information. Split from can_schedule_ready_p ()
2164 because when we schedule insn speculatively then insn passed to
2165 can_schedule_ready_p () differs from the one passed to
2166 begin_schedule_ready (). */
2167 static void
2168 begin_schedule_ready (rtx insn, rtx last ATTRIBUTE_UNUSED)
2169 {
2170 /* An interblock motion? */
2171 if (INSN_BB (insn) != target_bb)
2172 {
2173 if (IS_SPECULATIVE_INSN (insn))
2174 {
2175 gcc_assert (check_live (insn, INSN_BB (insn)));
2176
2177 update_live (insn, INSN_BB (insn));
2178
2179 /* For speculative load, mark insns fed by it. */
2180 if (IS_LOAD_INSN (insn) || FED_BY_SPEC_LOAD (insn))
2181 set_spec_fed (insn);
2182
2183 nr_spec++;
2184 }
2185 nr_inter++;
2186 }
2187 else
2188 {
2189 /* In block motion. */
2190 sched_target_n_insns++;
2191 }
2192 sched_n_insns++;
2193 }
2194
2195 /* Called after INSN has all its hard dependencies resolved and the speculation
2196 of type TS is enough to overcome them all.
2197 Return nonzero if it should be moved to the ready list or the queue, or zero
2198 if we should silently discard it. */
2199 static ds_t
2200 new_ready (rtx next, ds_t ts)
2201 {
2202 if (INSN_BB (next) != target_bb)
2203 {
2204 int not_ex_free = 0;
2205
2206 /* For speculative insns, before inserting to ready/queue,
2207 check live, exception-free, and issue-delay. */
2208 if (!IS_VALID (INSN_BB (next))
2209 || CANT_MOVE (next)
2210 || (IS_SPECULATIVE_INSN (next)
2211 && ((recog_memoized (next) >= 0
2212 && min_insn_conflict_delay (curr_state, next, next)
2213 > PARAM_VALUE (PARAM_MAX_SCHED_INSN_CONFLICT_DELAY))
2214 || IS_SPECULATION_CHECK_P (next)
2215 || !check_live (next, INSN_BB (next))
2216 || (not_ex_free = !is_exception_free (next, INSN_BB (next),
2217 target_bb)))))
2218 {
2219 if (not_ex_free
2220 /* We are here because is_exception_free () == false.
2221 But we possibly can handle that with control speculation. */
2222 && sched_deps_info->generate_spec_deps
2223 && spec_info->mask & BEGIN_CONTROL)
2224 {
2225 ds_t new_ds;
2226
2227 /* Add control speculation to NEXT's dependency type. */
2228 new_ds = set_dep_weak (ts, BEGIN_CONTROL, MAX_DEP_WEAK);
2229
2230 /* Check if NEXT can be speculated with new dependency type. */
2231 if (sched_insn_is_legitimate_for_speculation_p (next, new_ds))
2232 /* Here we got new control-speculative instruction. */
2233 ts = new_ds;
2234 else
2235 /* NEXT isn't ready yet. */
2236 ts = (ts & ~SPECULATIVE) | HARD_DEP;
2237 }
2238 else
2239 /* NEXT isn't ready yet. */
2240 ts = (ts & ~SPECULATIVE) | HARD_DEP;
2241 }
2242 }
2243
2244 return ts;
2245 }
2246
2247 /* Return a string that contains the insn uid and optionally anything else
2248 necessary to identify this insn in an output. It's valid to use a
2249 static buffer for this. The ALIGNED parameter should cause the string
2250 to be formatted so that multiple output lines will line up nicely. */
2251
2252 static const char *
2253 rgn_print_insn (const_rtx insn, int aligned)
2254 {
2255 static char tmp[80];
2256
2257 if (aligned)
2258 sprintf (tmp, "b%3d: i%4d", INSN_BB (insn), INSN_UID (insn));
2259 else
2260 {
2261 if (current_nr_blocks > 1 && INSN_BB (insn) != target_bb)
2262 sprintf (tmp, "%d/b%d", INSN_UID (insn), INSN_BB (insn));
2263 else
2264 sprintf (tmp, "%d", INSN_UID (insn));
2265 }
2266 return tmp;
2267 }
2268
2269 /* Compare priority of two insns. Return a positive number if the second
2270 insn is to be preferred for scheduling, and a negative one if the first
2271 is to be preferred. Zero if they are equally good. */
2272
2273 static int
2274 rgn_rank (rtx insn1, rtx insn2)
2275 {
2276 /* Some comparison make sense in interblock scheduling only. */
2277 if (INSN_BB (insn1) != INSN_BB (insn2))
2278 {
2279 int spec_val, prob_val;
2280
2281 /* Prefer an inblock motion on an interblock motion. */
2282 if ((INSN_BB (insn2) == target_bb) && (INSN_BB (insn1) != target_bb))
2283 return 1;
2284 if ((INSN_BB (insn1) == target_bb) && (INSN_BB (insn2) != target_bb))
2285 return -1;
2286
2287 /* Prefer a useful motion on a speculative one. */
2288 spec_val = IS_SPECULATIVE_INSN (insn1) - IS_SPECULATIVE_INSN (insn2);
2289 if (spec_val)
2290 return spec_val;
2291
2292 /* Prefer a more probable (speculative) insn. */
2293 prob_val = INSN_PROBABILITY (insn2) - INSN_PROBABILITY (insn1);
2294 if (prob_val)
2295 return prob_val;
2296 }
2297 return 0;
2298 }
2299
2300 /* NEXT is an instruction that depends on INSN (a backward dependence);
2301 return nonzero if we should include this dependence in priority
2302 calculations. */
2303
2304 int
2305 contributes_to_priority (rtx next, rtx insn)
2306 {
2307 /* NEXT and INSN reside in one ebb. */
2308 return BLOCK_TO_BB (BLOCK_NUM (next)) == BLOCK_TO_BB (BLOCK_NUM (insn));
2309 }
2310
2311 /* INSN is a JUMP_INSN, COND_SET is the set of registers that are
2312 conditionally set before INSN. Store the set of registers that
2313 must be considered as used by this jump in USED and that of
2314 registers that must be considered as set in SET. */
2315
2316 static void
2317 compute_jump_reg_dependencies (rtx insn ATTRIBUTE_UNUSED,
2318 regset cond_exec ATTRIBUTE_UNUSED,
2319 regset used ATTRIBUTE_UNUSED,
2320 regset set ATTRIBUTE_UNUSED)
2321 {
2322 /* Nothing to do here, since we postprocess jumps in
2323 add_branch_dependences. */
2324 }
2325
2326 /* This variable holds common_sched_info hooks and data relevant to
2327 the interblock scheduler. */
2328 static struct common_sched_info_def rgn_common_sched_info;
2329
2330
2331 /* This holds data for the dependence analysis relevant to
2332 the interblock scheduler. */
2333 static struct sched_deps_info_def rgn_sched_deps_info;
2334
2335 /* This holds constant data used for initializing the above structure
2336 for the Haifa scheduler. */
2337 static const struct sched_deps_info_def rgn_const_sched_deps_info =
2338 {
2339 compute_jump_reg_dependencies,
2340 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2341 0, 0, 0
2342 };
2343
2344 /* Same as above, but for the selective scheduler. */
2345 static const struct sched_deps_info_def rgn_const_sel_sched_deps_info =
2346 {
2347 compute_jump_reg_dependencies,
2348 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2349 0, 0, 0
2350 };
2351
2352 /* Return true if scheduling INSN will trigger finish of scheduling
2353 current block. */
2354 static bool
2355 rgn_insn_finishes_block_p (rtx insn)
2356 {
2357 if (INSN_BB (insn) == target_bb
2358 && sched_target_n_insns + 1 == target_n_insns)
2359 /* INSN is the last not-scheduled instruction in the current block. */
2360 return true;
2361
2362 return false;
2363 }
2364
2365 /* Used in schedule_insns to initialize current_sched_info for scheduling
2366 regions (or single basic blocks). */
2367
2368 static const struct haifa_sched_info rgn_const_sched_info =
2369 {
2370 init_ready_list,
2371 can_schedule_ready_p,
2372 schedule_more_p,
2373 new_ready,
2374 rgn_rank,
2375 rgn_print_insn,
2376 contributes_to_priority,
2377 rgn_insn_finishes_block_p,
2378
2379 NULL, NULL,
2380 NULL, NULL,
2381 0, 0,
2382
2383 rgn_add_remove_insn,
2384 begin_schedule_ready,
2385 advance_target_bb,
2386 SCHED_RGN
2387 };
2388
2389 /* This variable holds the data and hooks needed to the Haifa scheduler backend
2390 for the interblock scheduler frontend. */
2391 static struct haifa_sched_info rgn_sched_info;
2392
2393 /* Returns maximum priority that an insn was assigned to. */
2394
2395 int
2396 get_rgn_sched_max_insns_priority (void)
2397 {
2398 return rgn_sched_info.sched_max_insns_priority;
2399 }
2400
2401 /* Determine if PAT sets a TARGET_CLASS_LIKELY_SPILLED_P register. */
2402
2403 static bool
2404 sets_likely_spilled (rtx pat)
2405 {
2406 bool ret = false;
2407 note_stores (pat, sets_likely_spilled_1, &ret);
2408 return ret;
2409 }
2410
2411 static void
2412 sets_likely_spilled_1 (rtx x, const_rtx pat, void *data)
2413 {
2414 bool *ret = (bool *) data;
2415
2416 if (GET_CODE (pat) == SET
2417 && REG_P (x)
2418 && HARD_REGISTER_P (x)
2419 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (x))))
2420 *ret = true;
2421 }
2422
2423 /* A bitmap to note insns that participate in any dependency. Used in
2424 add_branch_dependences. */
2425 static sbitmap insn_referenced;
2426
2427 /* Add dependences so that branches are scheduled to run last in their
2428 block. */
2429 static void
2430 add_branch_dependences (rtx head, rtx tail)
2431 {
2432 rtx insn, last;
2433
2434 /* For all branches, calls, uses, clobbers, cc0 setters, and instructions
2435 that can throw exceptions, force them to remain in order at the end of
2436 the block by adding dependencies and giving the last a high priority.
2437 There may be notes present, and prev_head may also be a note.
2438
2439 Branches must obviously remain at the end. Calls should remain at the
2440 end since moving them results in worse register allocation. Uses remain
2441 at the end to ensure proper register allocation.
2442
2443 cc0 setters remain at the end because they can't be moved away from
2444 their cc0 user.
2445
2446 COND_EXEC insns cannot be moved past a branch (see e.g. PR17808).
2447
2448 Insns setting TARGET_CLASS_LIKELY_SPILLED_P registers (usually return
2449 values) are not moved before reload because we can wind up with register
2450 allocation failures. */
2451
2452 while (tail != head && DEBUG_INSN_P (tail))
2453 tail = PREV_INSN (tail);
2454
2455 insn = tail;
2456 last = 0;
2457 while (CALL_P (insn)
2458 || JUMP_P (insn)
2459 || (NONJUMP_INSN_P (insn)
2460 && (GET_CODE (PATTERN (insn)) == USE
2461 || GET_CODE (PATTERN (insn)) == CLOBBER
2462 || can_throw_internal (insn)
2463 #ifdef HAVE_cc0
2464 || sets_cc0_p (PATTERN (insn))
2465 #endif
2466 || (!reload_completed
2467 && sets_likely_spilled (PATTERN (insn)))))
2468 || NOTE_P (insn))
2469 {
2470 if (!NOTE_P (insn))
2471 {
2472 if (last != 0
2473 && sd_find_dep_between (insn, last, false) == NULL)
2474 {
2475 if (! sched_insns_conditions_mutex_p (last, insn))
2476 add_dependence (last, insn, REG_DEP_ANTI);
2477 SET_BIT (insn_referenced, INSN_LUID (insn));
2478 }
2479
2480 CANT_MOVE (insn) = 1;
2481
2482 last = insn;
2483 }
2484
2485 /* Don't overrun the bounds of the basic block. */
2486 if (insn == head)
2487 break;
2488
2489 do
2490 insn = PREV_INSN (insn);
2491 while (insn != head && DEBUG_INSN_P (insn));
2492 }
2493
2494 /* Make sure these insns are scheduled last in their block. */
2495 insn = last;
2496 if (insn != 0)
2497 while (insn != head)
2498 {
2499 insn = prev_nonnote_insn (insn);
2500
2501 if (TEST_BIT (insn_referenced, INSN_LUID (insn))
2502 || DEBUG_INSN_P (insn))
2503 continue;
2504
2505 if (! sched_insns_conditions_mutex_p (last, insn))
2506 add_dependence (last, insn, REG_DEP_ANTI);
2507 }
2508
2509 if (!targetm.have_conditional_execution ())
2510 return;
2511
2512 /* Finally, if the block ends in a jump, and we are doing intra-block
2513 scheduling, make sure that the branch depends on any COND_EXEC insns
2514 inside the block to avoid moving the COND_EXECs past the branch insn.
2515
2516 We only have to do this after reload, because (1) before reload there
2517 are no COND_EXEC insns, and (2) the region scheduler is an intra-block
2518 scheduler after reload.
2519
2520 FIXME: We could in some cases move COND_EXEC insns past the branch if
2521 this scheduler would be a little smarter. Consider this code:
2522
2523 T = [addr]
2524 C ? addr += 4
2525 !C ? X += 12
2526 C ? T += 1
2527 C ? jump foo
2528
2529 On a target with a one cycle stall on a memory access the optimal
2530 sequence would be:
2531
2532 T = [addr]
2533 C ? addr += 4
2534 C ? T += 1
2535 C ? jump foo
2536 !C ? X += 12
2537
2538 We don't want to put the 'X += 12' before the branch because it just
2539 wastes a cycle of execution time when the branch is taken.
2540
2541 Note that in the example "!C" will always be true. That is another
2542 possible improvement for handling COND_EXECs in this scheduler: it
2543 could remove always-true predicates. */
2544
2545 if (!reload_completed || ! JUMP_P (tail))
2546 return;
2547
2548 insn = tail;
2549 while (insn != head)
2550 {
2551 insn = PREV_INSN (insn);
2552
2553 /* Note that we want to add this dependency even when
2554 sched_insns_conditions_mutex_p returns true. The whole point
2555 is that we _want_ this dependency, even if these insns really
2556 are independent. */
2557 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == COND_EXEC)
2558 add_dependence (tail, insn, REG_DEP_ANTI);
2559 }
2560 }
2561
2562 /* Data structures for the computation of data dependences in a regions. We
2563 keep one `deps' structure for every basic block. Before analyzing the
2564 data dependences for a bb, its variables are initialized as a function of
2565 the variables of its predecessors. When the analysis for a bb completes,
2566 we save the contents to the corresponding bb_deps[bb] variable. */
2567
2568 static struct deps_desc *bb_deps;
2569
2570 /* Duplicate the INSN_LIST elements of COPY and prepend them to OLD. */
2571
2572 static rtx
2573 concat_INSN_LIST (rtx copy, rtx old)
2574 {
2575 rtx new_rtx = old;
2576 for (; copy ; copy = XEXP (copy, 1))
2577 new_rtx = alloc_INSN_LIST (XEXP (copy, 0), new_rtx);
2578 return new_rtx;
2579 }
2580
2581 static void
2582 concat_insn_mem_list (rtx copy_insns, rtx copy_mems, rtx *old_insns_p,
2583 rtx *old_mems_p)
2584 {
2585 rtx new_insns = *old_insns_p;
2586 rtx new_mems = *old_mems_p;
2587
2588 while (copy_insns)
2589 {
2590 new_insns = alloc_INSN_LIST (XEXP (copy_insns, 0), new_insns);
2591 new_mems = alloc_EXPR_LIST (VOIDmode, XEXP (copy_mems, 0), new_mems);
2592 copy_insns = XEXP (copy_insns, 1);
2593 copy_mems = XEXP (copy_mems, 1);
2594 }
2595
2596 *old_insns_p = new_insns;
2597 *old_mems_p = new_mems;
2598 }
2599
2600 /* Join PRED_DEPS to the SUCC_DEPS. */
2601 void
2602 deps_join (struct deps_desc *succ_deps, struct deps_desc *pred_deps)
2603 {
2604 unsigned reg;
2605 reg_set_iterator rsi;
2606
2607 /* The reg_last lists are inherited by successor. */
2608 EXECUTE_IF_SET_IN_REG_SET (&pred_deps->reg_last_in_use, 0, reg, rsi)
2609 {
2610 struct deps_reg *pred_rl = &pred_deps->reg_last[reg];
2611 struct deps_reg *succ_rl = &succ_deps->reg_last[reg];
2612
2613 succ_rl->uses = concat_INSN_LIST (pred_rl->uses, succ_rl->uses);
2614 succ_rl->sets = concat_INSN_LIST (pred_rl->sets, succ_rl->sets);
2615 succ_rl->implicit_sets
2616 = concat_INSN_LIST (pred_rl->implicit_sets, succ_rl->implicit_sets);
2617 succ_rl->clobbers = concat_INSN_LIST (pred_rl->clobbers,
2618 succ_rl->clobbers);
2619 succ_rl->uses_length += pred_rl->uses_length;
2620 succ_rl->clobbers_length += pred_rl->clobbers_length;
2621 }
2622 IOR_REG_SET (&succ_deps->reg_last_in_use, &pred_deps->reg_last_in_use);
2623
2624 /* Mem read/write lists are inherited by successor. */
2625 concat_insn_mem_list (pred_deps->pending_read_insns,
2626 pred_deps->pending_read_mems,
2627 &succ_deps->pending_read_insns,
2628 &succ_deps->pending_read_mems);
2629 concat_insn_mem_list (pred_deps->pending_write_insns,
2630 pred_deps->pending_write_mems,
2631 &succ_deps->pending_write_insns,
2632 &succ_deps->pending_write_mems);
2633
2634 succ_deps->last_pending_memory_flush
2635 = concat_INSN_LIST (pred_deps->last_pending_memory_flush,
2636 succ_deps->last_pending_memory_flush);
2637
2638 succ_deps->pending_read_list_length += pred_deps->pending_read_list_length;
2639 succ_deps->pending_write_list_length += pred_deps->pending_write_list_length;
2640 succ_deps->pending_flush_length += pred_deps->pending_flush_length;
2641
2642 /* last_function_call is inherited by successor. */
2643 succ_deps->last_function_call
2644 = concat_INSN_LIST (pred_deps->last_function_call,
2645 succ_deps->last_function_call);
2646
2647 /* last_function_call_may_noreturn is inherited by successor. */
2648 succ_deps->last_function_call_may_noreturn
2649 = concat_INSN_LIST (pred_deps->last_function_call_may_noreturn,
2650 succ_deps->last_function_call_may_noreturn);
2651
2652 /* sched_before_next_call is inherited by successor. */
2653 succ_deps->sched_before_next_call
2654 = concat_INSN_LIST (pred_deps->sched_before_next_call,
2655 succ_deps->sched_before_next_call);
2656 }
2657
2658 /* After computing the dependencies for block BB, propagate the dependencies
2659 found in TMP_DEPS to the successors of the block. */
2660 static void
2661 propagate_deps (int bb, struct deps_desc *pred_deps)
2662 {
2663 basic_block block = BASIC_BLOCK (BB_TO_BLOCK (bb));
2664 edge_iterator ei;
2665 edge e;
2666
2667 /* bb's structures are inherited by its successors. */
2668 FOR_EACH_EDGE (e, ei, block->succs)
2669 {
2670 /* Only bbs "below" bb, in the same region, are interesting. */
2671 if (e->dest == EXIT_BLOCK_PTR
2672 || CONTAINING_RGN (block->index) != CONTAINING_RGN (e->dest->index)
2673 || BLOCK_TO_BB (e->dest->index) <= bb)
2674 continue;
2675
2676 deps_join (bb_deps + BLOCK_TO_BB (e->dest->index), pred_deps);
2677 }
2678
2679 /* These lists should point to the right place, for correct
2680 freeing later. */
2681 bb_deps[bb].pending_read_insns = pred_deps->pending_read_insns;
2682 bb_deps[bb].pending_read_mems = pred_deps->pending_read_mems;
2683 bb_deps[bb].pending_write_insns = pred_deps->pending_write_insns;
2684 bb_deps[bb].pending_write_mems = pred_deps->pending_write_mems;
2685
2686 /* Can't allow these to be freed twice. */
2687 pred_deps->pending_read_insns = 0;
2688 pred_deps->pending_read_mems = 0;
2689 pred_deps->pending_write_insns = 0;
2690 pred_deps->pending_write_mems = 0;
2691 }
2692
2693 /* Compute dependences inside bb. In a multiple blocks region:
2694 (1) a bb is analyzed after its predecessors, and (2) the lists in
2695 effect at the end of bb (after analyzing for bb) are inherited by
2696 bb's successors.
2697
2698 Specifically for reg-reg data dependences, the block insns are
2699 scanned by sched_analyze () top-to-bottom. Three lists are
2700 maintained by sched_analyze (): reg_last[].sets for register DEFs,
2701 reg_last[].implicit_sets for implicit hard register DEFs, and
2702 reg_last[].uses for register USEs.
2703
2704 When analysis is completed for bb, we update for its successors:
2705 ; - DEFS[succ] = Union (DEFS [succ], DEFS [bb])
2706 ; - IMPLICIT_DEFS[succ] = Union (IMPLICIT_DEFS [succ], IMPLICIT_DEFS [bb])
2707 ; - USES[succ] = Union (USES [succ], DEFS [bb])
2708
2709 The mechanism for computing mem-mem data dependence is very
2710 similar, and the result is interblock dependences in the region. */
2711
2712 static void
2713 compute_block_dependences (int bb)
2714 {
2715 rtx head, tail;
2716 struct deps_desc tmp_deps;
2717
2718 tmp_deps = bb_deps[bb];
2719
2720 /* Do the analysis for this block. */
2721 gcc_assert (EBB_FIRST_BB (bb) == EBB_LAST_BB (bb));
2722 get_ebb_head_tail (EBB_FIRST_BB (bb), EBB_LAST_BB (bb), &head, &tail);
2723
2724 sched_analyze (&tmp_deps, head, tail);
2725
2726 /* Selective scheduling handles control dependencies by itself. */
2727 if (!sel_sched_p ())
2728 add_branch_dependences (head, tail);
2729
2730 if (current_nr_blocks > 1)
2731 propagate_deps (bb, &tmp_deps);
2732
2733 /* Free up the INSN_LISTs. */
2734 free_deps (&tmp_deps);
2735
2736 if (targetm.sched.dependencies_evaluation_hook)
2737 targetm.sched.dependencies_evaluation_hook (head, tail);
2738 }
2739
2740 /* Free dependencies of instructions inside BB. */
2741 static void
2742 free_block_dependencies (int bb)
2743 {
2744 rtx head;
2745 rtx tail;
2746
2747 get_ebb_head_tail (EBB_FIRST_BB (bb), EBB_LAST_BB (bb), &head, &tail);
2748
2749 if (no_real_insns_p (head, tail))
2750 return;
2751
2752 sched_free_deps (head, tail, true);
2753 }
2754
2755 /* Remove all INSN_LISTs and EXPR_LISTs from the pending lists and add
2756 them to the unused_*_list variables, so that they can be reused. */
2757
2758 static void
2759 free_pending_lists (void)
2760 {
2761 int bb;
2762
2763 for (bb = 0; bb < current_nr_blocks; bb++)
2764 {
2765 free_INSN_LIST_list (&bb_deps[bb].pending_read_insns);
2766 free_INSN_LIST_list (&bb_deps[bb].pending_write_insns);
2767 free_EXPR_LIST_list (&bb_deps[bb].pending_read_mems);
2768 free_EXPR_LIST_list (&bb_deps[bb].pending_write_mems);
2769 }
2770 }
2771 \f
2772 /* Print dependences for debugging starting from FROM_BB.
2773 Callable from debugger. */
2774 /* Print dependences for debugging starting from FROM_BB.
2775 Callable from debugger. */
2776 DEBUG_FUNCTION void
2777 debug_rgn_dependencies (int from_bb)
2778 {
2779 int bb;
2780
2781 fprintf (sched_dump,
2782 ";; --------------- forward dependences: ------------ \n");
2783
2784 for (bb = from_bb; bb < current_nr_blocks; bb++)
2785 {
2786 rtx head, tail;
2787
2788 get_ebb_head_tail (EBB_FIRST_BB (bb), EBB_LAST_BB (bb), &head, &tail);
2789 fprintf (sched_dump, "\n;; --- Region Dependences --- b %d bb %d \n",
2790 BB_TO_BLOCK (bb), bb);
2791
2792 debug_dependencies (head, tail);
2793 }
2794 }
2795
2796 /* Print dependencies information for instructions between HEAD and TAIL.
2797 ??? This function would probably fit best in haifa-sched.c. */
2798 void debug_dependencies (rtx head, rtx tail)
2799 {
2800 rtx insn;
2801 rtx next_tail = NEXT_INSN (tail);
2802
2803 fprintf (sched_dump, ";; %7s%6s%6s%6s%6s%6s%14s\n",
2804 "insn", "code", "bb", "dep", "prio", "cost",
2805 "reservation");
2806 fprintf (sched_dump, ";; %7s%6s%6s%6s%6s%6s%14s\n",
2807 "----", "----", "--", "---", "----", "----",
2808 "-----------");
2809
2810 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
2811 {
2812 if (! INSN_P (insn))
2813 {
2814 int n;
2815 fprintf (sched_dump, ";; %6d ", INSN_UID (insn));
2816 if (NOTE_P (insn))
2817 {
2818 n = NOTE_KIND (insn);
2819 fprintf (sched_dump, "%s\n", GET_NOTE_INSN_NAME (n));
2820 }
2821 else
2822 fprintf (sched_dump, " {%s}\n", GET_RTX_NAME (GET_CODE (insn)));
2823 continue;
2824 }
2825
2826 fprintf (sched_dump,
2827 ";; %s%5d%6d%6d%6d%6d%6d ",
2828 (SCHED_GROUP_P (insn) ? "+" : " "),
2829 INSN_UID (insn),
2830 INSN_CODE (insn),
2831 BLOCK_NUM (insn),
2832 sched_emulate_haifa_p ? -1 : sd_lists_size (insn, SD_LIST_BACK),
2833 (sel_sched_p () ? (sched_emulate_haifa_p ? -1
2834 : INSN_PRIORITY (insn))
2835 : INSN_PRIORITY (insn)),
2836 (sel_sched_p () ? (sched_emulate_haifa_p ? -1
2837 : insn_cost (insn))
2838 : insn_cost (insn)));
2839
2840 if (recog_memoized (insn) < 0)
2841 fprintf (sched_dump, "nothing");
2842 else
2843 print_reservation (sched_dump, insn);
2844
2845 fprintf (sched_dump, "\t: ");
2846 {
2847 sd_iterator_def sd_it;
2848 dep_t dep;
2849
2850 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
2851 fprintf (sched_dump, "%d ", INSN_UID (DEP_CON (dep)));
2852 }
2853 fprintf (sched_dump, "\n");
2854 }
2855
2856 fprintf (sched_dump, "\n");
2857 }
2858 \f
2859 /* Returns true if all the basic blocks of the current region have
2860 NOTE_DISABLE_SCHED_OF_BLOCK which means not to schedule that region. */
2861 bool
2862 sched_is_disabled_for_current_region_p (void)
2863 {
2864 int bb;
2865
2866 for (bb = 0; bb < current_nr_blocks; bb++)
2867 if (!(BASIC_BLOCK (BB_TO_BLOCK (bb))->flags & BB_DISABLE_SCHEDULE))
2868 return false;
2869
2870 return true;
2871 }
2872
2873 /* Free all region dependencies saved in INSN_BACK_DEPS and
2874 INSN_RESOLVED_BACK_DEPS. The Haifa scheduler does this on the fly
2875 when scheduling, so this function is supposed to be called from
2876 the selective scheduling only. */
2877 void
2878 free_rgn_deps (void)
2879 {
2880 int bb;
2881
2882 for (bb = 0; bb < current_nr_blocks; bb++)
2883 {
2884 rtx head, tail;
2885
2886 gcc_assert (EBB_FIRST_BB (bb) == EBB_LAST_BB (bb));
2887 get_ebb_head_tail (EBB_FIRST_BB (bb), EBB_LAST_BB (bb), &head, &tail);
2888
2889 sched_free_deps (head, tail, false);
2890 }
2891 }
2892
2893 static int rgn_n_insns;
2894
2895 /* Compute insn priority for a current region. */
2896 void
2897 compute_priorities (void)
2898 {
2899 int bb;
2900
2901 current_sched_info->sched_max_insns_priority = 0;
2902 for (bb = 0; bb < current_nr_blocks; bb++)
2903 {
2904 rtx head, tail;
2905
2906 gcc_assert (EBB_FIRST_BB (bb) == EBB_LAST_BB (bb));
2907 get_ebb_head_tail (EBB_FIRST_BB (bb), EBB_LAST_BB (bb), &head, &tail);
2908
2909 if (no_real_insns_p (head, tail))
2910 continue;
2911
2912 rgn_n_insns += set_priorities (head, tail);
2913 }
2914 current_sched_info->sched_max_insns_priority++;
2915 }
2916
2917 /* Schedule a region. A region is either an inner loop, a loop-free
2918 subroutine, or a single basic block. Each bb in the region is
2919 scheduled after its flow predecessors. */
2920
2921 static void
2922 schedule_region (int rgn)
2923 {
2924 int bb;
2925 int sched_rgn_n_insns = 0;
2926
2927 rgn_n_insns = 0;
2928
2929 rgn_setup_region (rgn);
2930
2931 /* Don't schedule region that is marked by
2932 NOTE_DISABLE_SCHED_OF_BLOCK. */
2933 if (sched_is_disabled_for_current_region_p ())
2934 return;
2935
2936 sched_rgn_compute_dependencies (rgn);
2937
2938 sched_rgn_local_init (rgn);
2939
2940 /* Set priorities. */
2941 compute_priorities ();
2942
2943 sched_extend_ready_list (rgn_n_insns);
2944
2945 if (sched_pressure_p)
2946 {
2947 sched_init_region_reg_pressure_info ();
2948 for (bb = 0; bb < current_nr_blocks; bb++)
2949 {
2950 basic_block first_bb, last_bb;
2951 rtx head, tail;
2952
2953 first_bb = EBB_FIRST_BB (bb);
2954 last_bb = EBB_LAST_BB (bb);
2955
2956 get_ebb_head_tail (first_bb, last_bb, &head, &tail);
2957
2958 if (no_real_insns_p (head, tail))
2959 {
2960 gcc_assert (first_bb == last_bb);
2961 continue;
2962 }
2963 sched_setup_bb_reg_pressure_info (first_bb, PREV_INSN (head));
2964 }
2965 }
2966
2967 /* Now we can schedule all blocks. */
2968 for (bb = 0; bb < current_nr_blocks; bb++)
2969 {
2970 basic_block first_bb, last_bb, curr_bb;
2971 rtx head, tail;
2972
2973 first_bb = EBB_FIRST_BB (bb);
2974 last_bb = EBB_LAST_BB (bb);
2975
2976 get_ebb_head_tail (first_bb, last_bb, &head, &tail);
2977
2978 if (no_real_insns_p (head, tail))
2979 {
2980 gcc_assert (first_bb == last_bb);
2981 continue;
2982 }
2983
2984 current_sched_info->prev_head = PREV_INSN (head);
2985 current_sched_info->next_tail = NEXT_INSN (tail);
2986
2987 remove_notes (head, tail);
2988
2989 unlink_bb_notes (first_bb, last_bb);
2990
2991 target_bb = bb;
2992
2993 gcc_assert (flag_schedule_interblock || current_nr_blocks == 1);
2994 current_sched_info->queue_must_finish_empty = current_nr_blocks == 1;
2995
2996 curr_bb = first_bb;
2997 if (dbg_cnt (sched_block))
2998 {
2999 schedule_block (&curr_bb);
3000 gcc_assert (EBB_FIRST_BB (bb) == first_bb);
3001 sched_rgn_n_insns += sched_n_insns;
3002 }
3003 else
3004 {
3005 sched_rgn_n_insns += rgn_n_insns;
3006 }
3007
3008 /* Clean up. */
3009 if (current_nr_blocks > 1)
3010 free_trg_info ();
3011 }
3012
3013 /* Sanity check: verify that all region insns were scheduled. */
3014 gcc_assert (sched_rgn_n_insns == rgn_n_insns);
3015
3016 sched_finish_ready_list ();
3017
3018 /* Done with this region. */
3019 sched_rgn_local_finish ();
3020
3021 /* Free dependencies. */
3022 for (bb = 0; bb < current_nr_blocks; ++bb)
3023 free_block_dependencies (bb);
3024
3025 gcc_assert (haifa_recovery_bb_ever_added_p
3026 || deps_pools_are_empty_p ());
3027 }
3028
3029 /* Initialize data structures for region scheduling. */
3030
3031 void
3032 sched_rgn_init (bool single_blocks_p)
3033 {
3034 min_spec_prob = ((PARAM_VALUE (PARAM_MIN_SPEC_PROB) * REG_BR_PROB_BASE)
3035 / 100);
3036
3037 nr_inter = 0;
3038 nr_spec = 0;
3039
3040 extend_regions ();
3041
3042 CONTAINING_RGN (ENTRY_BLOCK) = -1;
3043 CONTAINING_RGN (EXIT_BLOCK) = -1;
3044
3045 /* Compute regions for scheduling. */
3046 if (single_blocks_p
3047 || n_basic_blocks == NUM_FIXED_BLOCKS + 1
3048 || !flag_schedule_interblock
3049 || is_cfg_nonregular ())
3050 {
3051 find_single_block_region (sel_sched_p ());
3052 }
3053 else
3054 {
3055 /* Compute the dominators and post dominators. */
3056 if (!sel_sched_p ())
3057 calculate_dominance_info (CDI_DOMINATORS);
3058
3059 /* Find regions. */
3060 find_rgns ();
3061
3062 if (sched_verbose >= 3)
3063 debug_regions ();
3064
3065 /* For now. This will move as more and more of haifa is converted
3066 to using the cfg code. */
3067 if (!sel_sched_p ())
3068 free_dominance_info (CDI_DOMINATORS);
3069 }
3070
3071 gcc_assert (0 < nr_regions && nr_regions <= n_basic_blocks);
3072
3073 RGN_BLOCKS (nr_regions) = (RGN_BLOCKS (nr_regions - 1) +
3074 RGN_NR_BLOCKS (nr_regions - 1));
3075 }
3076
3077 /* Free data structures for region scheduling. */
3078 void
3079 sched_rgn_finish (void)
3080 {
3081 /* Reposition the prologue and epilogue notes in case we moved the
3082 prologue/epilogue insns. */
3083 if (reload_completed)
3084 reposition_prologue_and_epilogue_notes ();
3085
3086 if (sched_verbose)
3087 {
3088 if (reload_completed == 0
3089 && flag_schedule_interblock)
3090 {
3091 fprintf (sched_dump,
3092 "\n;; Procedure interblock/speculative motions == %d/%d \n",
3093 nr_inter, nr_spec);
3094 }
3095 else
3096 gcc_assert (nr_inter <= 0);
3097 fprintf (sched_dump, "\n\n");
3098 }
3099
3100 nr_regions = 0;
3101
3102 free (rgn_table);
3103 rgn_table = NULL;
3104
3105 free (rgn_bb_table);
3106 rgn_bb_table = NULL;
3107
3108 free (block_to_bb);
3109 block_to_bb = NULL;
3110
3111 free (containing_rgn);
3112 containing_rgn = NULL;
3113
3114 free (ebb_head);
3115 ebb_head = NULL;
3116 }
3117
3118 /* Setup global variables like CURRENT_BLOCKS and CURRENT_NR_BLOCK to
3119 point to the region RGN. */
3120 void
3121 rgn_setup_region (int rgn)
3122 {
3123 int bb;
3124
3125 /* Set variables for the current region. */
3126 current_nr_blocks = RGN_NR_BLOCKS (rgn);
3127 current_blocks = RGN_BLOCKS (rgn);
3128
3129 /* EBB_HEAD is a region-scope structure. But we realloc it for
3130 each region to save time/memory/something else.
3131 See comments in add_block1, for what reasons we allocate +1 element. */
3132 ebb_head = XRESIZEVEC (int, ebb_head, current_nr_blocks + 1);
3133 for (bb = 0; bb <= current_nr_blocks; bb++)
3134 ebb_head[bb] = current_blocks + bb;
3135 }
3136
3137 /* Compute instruction dependencies in region RGN. */
3138 void
3139 sched_rgn_compute_dependencies (int rgn)
3140 {
3141 if (!RGN_DONT_CALC_DEPS (rgn))
3142 {
3143 int bb;
3144
3145 if (sel_sched_p ())
3146 sched_emulate_haifa_p = 1;
3147
3148 init_deps_global ();
3149
3150 /* Initializations for region data dependence analysis. */
3151 bb_deps = XNEWVEC (struct deps_desc, current_nr_blocks);
3152 for (bb = 0; bb < current_nr_blocks; bb++)
3153 init_deps (bb_deps + bb, false);
3154
3155 /* Initialize bitmap used in add_branch_dependences. */
3156 insn_referenced = sbitmap_alloc (sched_max_luid);
3157 sbitmap_zero (insn_referenced);
3158
3159 /* Compute backward dependencies. */
3160 for (bb = 0; bb < current_nr_blocks; bb++)
3161 compute_block_dependences (bb);
3162
3163 sbitmap_free (insn_referenced);
3164 free_pending_lists ();
3165 finish_deps_global ();
3166 free (bb_deps);
3167
3168 /* We don't want to recalculate this twice. */
3169 RGN_DONT_CALC_DEPS (rgn) = 1;
3170
3171 if (sel_sched_p ())
3172 sched_emulate_haifa_p = 0;
3173 }
3174 else
3175 /* (This is a recovery block. It is always a single block region.)
3176 OR (We use selective scheduling.) */
3177 gcc_assert (current_nr_blocks == 1 || sel_sched_p ());
3178 }
3179
3180 /* Init region data structures. Returns true if this region should
3181 not be scheduled. */
3182 void
3183 sched_rgn_local_init (int rgn)
3184 {
3185 int bb;
3186
3187 /* Compute interblock info: probabilities, split-edges, dominators, etc. */
3188 if (current_nr_blocks > 1)
3189 {
3190 basic_block block;
3191 edge e;
3192 edge_iterator ei;
3193
3194 prob = XNEWVEC (int, current_nr_blocks);
3195
3196 dom = sbitmap_vector_alloc (current_nr_blocks, current_nr_blocks);
3197 sbitmap_vector_zero (dom, current_nr_blocks);
3198
3199 /* Use ->aux to implement EDGE_TO_BIT mapping. */
3200 rgn_nr_edges = 0;
3201 FOR_EACH_BB (block)
3202 {
3203 if (CONTAINING_RGN (block->index) != rgn)
3204 continue;
3205 FOR_EACH_EDGE (e, ei, block->succs)
3206 SET_EDGE_TO_BIT (e, rgn_nr_edges++);
3207 }
3208
3209 rgn_edges = XNEWVEC (edge, rgn_nr_edges);
3210 rgn_nr_edges = 0;
3211 FOR_EACH_BB (block)
3212 {
3213 if (CONTAINING_RGN (block->index) != rgn)
3214 continue;
3215 FOR_EACH_EDGE (e, ei, block->succs)
3216 rgn_edges[rgn_nr_edges++] = e;
3217 }
3218
3219 /* Split edges. */
3220 pot_split = sbitmap_vector_alloc (current_nr_blocks, rgn_nr_edges);
3221 sbitmap_vector_zero (pot_split, current_nr_blocks);
3222 ancestor_edges = sbitmap_vector_alloc (current_nr_blocks, rgn_nr_edges);
3223 sbitmap_vector_zero (ancestor_edges, current_nr_blocks);
3224
3225 /* Compute probabilities, dominators, split_edges. */
3226 for (bb = 0; bb < current_nr_blocks; bb++)
3227 compute_dom_prob_ps (bb);
3228
3229 /* Cleanup ->aux used for EDGE_TO_BIT mapping. */
3230 /* We don't need them anymore. But we want to avoid duplication of
3231 aux fields in the newly created edges. */
3232 FOR_EACH_BB (block)
3233 {
3234 if (CONTAINING_RGN (block->index) != rgn)
3235 continue;
3236 FOR_EACH_EDGE (e, ei, block->succs)
3237 e->aux = NULL;
3238 }
3239 }
3240 }
3241
3242 /* Free data computed for the finished region. */
3243 void
3244 sched_rgn_local_free (void)
3245 {
3246 free (prob);
3247 sbitmap_vector_free (dom);
3248 sbitmap_vector_free (pot_split);
3249 sbitmap_vector_free (ancestor_edges);
3250 free (rgn_edges);
3251 }
3252
3253 /* Free data computed for the finished region. */
3254 void
3255 sched_rgn_local_finish (void)
3256 {
3257 if (current_nr_blocks > 1 && !sel_sched_p ())
3258 {
3259 sched_rgn_local_free ();
3260 }
3261 }
3262
3263 /* Setup scheduler infos. */
3264 void
3265 rgn_setup_common_sched_info (void)
3266 {
3267 memcpy (&rgn_common_sched_info, &haifa_common_sched_info,
3268 sizeof (rgn_common_sched_info));
3269
3270 rgn_common_sched_info.fix_recovery_cfg = rgn_fix_recovery_cfg;
3271 rgn_common_sched_info.add_block = rgn_add_block;
3272 rgn_common_sched_info.estimate_number_of_insns
3273 = rgn_estimate_number_of_insns;
3274 rgn_common_sched_info.sched_pass_id = SCHED_RGN_PASS;
3275
3276 common_sched_info = &rgn_common_sched_info;
3277 }
3278
3279 /* Setup all *_sched_info structures (for the Haifa frontend
3280 and for the dependence analysis) in the interblock scheduler. */
3281 void
3282 rgn_setup_sched_infos (void)
3283 {
3284 if (!sel_sched_p ())
3285 memcpy (&rgn_sched_deps_info, &rgn_const_sched_deps_info,
3286 sizeof (rgn_sched_deps_info));
3287 else
3288 memcpy (&rgn_sched_deps_info, &rgn_const_sel_sched_deps_info,
3289 sizeof (rgn_sched_deps_info));
3290
3291 sched_deps_info = &rgn_sched_deps_info;
3292
3293 memcpy (&rgn_sched_info, &rgn_const_sched_info, sizeof (rgn_sched_info));
3294 current_sched_info = &rgn_sched_info;
3295 }
3296
3297 /* The one entry point in this file. */
3298 void
3299 schedule_insns (void)
3300 {
3301 int rgn;
3302
3303 /* Taking care of this degenerate case makes the rest of
3304 this code simpler. */
3305 if (n_basic_blocks == NUM_FIXED_BLOCKS)
3306 return;
3307
3308 rgn_setup_common_sched_info ();
3309 rgn_setup_sched_infos ();
3310
3311 haifa_sched_init ();
3312 sched_rgn_init (reload_completed);
3313
3314 bitmap_initialize (&not_in_df, 0);
3315 bitmap_clear (&not_in_df);
3316
3317 /* Schedule every region in the subroutine. */
3318 for (rgn = 0; rgn < nr_regions; rgn++)
3319 if (dbg_cnt (sched_region))
3320 schedule_region (rgn);
3321
3322 /* Clean up. */
3323 sched_rgn_finish ();
3324 bitmap_clear (&not_in_df);
3325
3326 haifa_sched_finish ();
3327 }
3328
3329 /* INSN has been added to/removed from current region. */
3330 static void
3331 rgn_add_remove_insn (rtx insn, int remove_p)
3332 {
3333 if (!remove_p)
3334 rgn_n_insns++;
3335 else
3336 rgn_n_insns--;
3337
3338 if (INSN_BB (insn) == target_bb)
3339 {
3340 if (!remove_p)
3341 target_n_insns++;
3342 else
3343 target_n_insns--;
3344 }
3345 }
3346
3347 /* Extend internal data structures. */
3348 void
3349 extend_regions (void)
3350 {
3351 rgn_table = XRESIZEVEC (region, rgn_table, n_basic_blocks);
3352 rgn_bb_table = XRESIZEVEC (int, rgn_bb_table, n_basic_blocks);
3353 block_to_bb = XRESIZEVEC (int, block_to_bb, last_basic_block);
3354 containing_rgn = XRESIZEVEC (int, containing_rgn, last_basic_block);
3355 }
3356
3357 void
3358 rgn_make_new_region_out_of_new_block (basic_block bb)
3359 {
3360 int i;
3361
3362 i = RGN_BLOCKS (nr_regions);
3363 /* I - first free position in rgn_bb_table. */
3364
3365 rgn_bb_table[i] = bb->index;
3366 RGN_NR_BLOCKS (nr_regions) = 1;
3367 RGN_HAS_REAL_EBB (nr_regions) = 0;
3368 RGN_DONT_CALC_DEPS (nr_regions) = 0;
3369 CONTAINING_RGN (bb->index) = nr_regions;
3370 BLOCK_TO_BB (bb->index) = 0;
3371
3372 nr_regions++;
3373
3374 RGN_BLOCKS (nr_regions) = i + 1;
3375 }
3376
3377 /* BB was added to ebb after AFTER. */
3378 static void
3379 rgn_add_block (basic_block bb, basic_block after)
3380 {
3381 extend_regions ();
3382 bitmap_set_bit (&not_in_df, bb->index);
3383
3384 if (after == 0 || after == EXIT_BLOCK_PTR)
3385 {
3386 rgn_make_new_region_out_of_new_block (bb);
3387 RGN_DONT_CALC_DEPS (nr_regions - 1) = (after == EXIT_BLOCK_PTR);
3388 }
3389 else
3390 {
3391 int i, pos;
3392
3393 /* We need to fix rgn_table, block_to_bb, containing_rgn
3394 and ebb_head. */
3395
3396 BLOCK_TO_BB (bb->index) = BLOCK_TO_BB (after->index);
3397
3398 /* We extend ebb_head to one more position to
3399 easily find the last position of the last ebb in
3400 the current region. Thus, ebb_head[BLOCK_TO_BB (after) + 1]
3401 is _always_ valid for access. */
3402
3403 i = BLOCK_TO_BB (after->index) + 1;
3404 pos = ebb_head[i] - 1;
3405 /* Now POS is the index of the last block in the region. */
3406
3407 /* Find index of basic block AFTER. */
3408 for (; rgn_bb_table[pos] != after->index; pos--);
3409
3410 pos++;
3411 gcc_assert (pos > ebb_head[i - 1]);
3412
3413 /* i - ebb right after "AFTER". */
3414 /* ebb_head[i] - VALID. */
3415
3416 /* Source position: ebb_head[i]
3417 Destination position: ebb_head[i] + 1
3418 Last position:
3419 RGN_BLOCKS (nr_regions) - 1
3420 Number of elements to copy: (last_position) - (source_position) + 1
3421 */
3422
3423 memmove (rgn_bb_table + pos + 1,
3424 rgn_bb_table + pos,
3425 ((RGN_BLOCKS (nr_regions) - 1) - (pos) + 1)
3426 * sizeof (*rgn_bb_table));
3427
3428 rgn_bb_table[pos] = bb->index;
3429
3430 for (; i <= current_nr_blocks; i++)
3431 ebb_head [i]++;
3432
3433 i = CONTAINING_RGN (after->index);
3434 CONTAINING_RGN (bb->index) = i;
3435
3436 RGN_HAS_REAL_EBB (i) = 1;
3437
3438 for (++i; i <= nr_regions; i++)
3439 RGN_BLOCKS (i)++;
3440 }
3441 }
3442
3443 /* Fix internal data after interblock movement of jump instruction.
3444 For parameter meaning please refer to
3445 sched-int.h: struct sched_info: fix_recovery_cfg. */
3446 static void
3447 rgn_fix_recovery_cfg (int bbi, int check_bbi, int check_bb_nexti)
3448 {
3449 int old_pos, new_pos, i;
3450
3451 BLOCK_TO_BB (check_bb_nexti) = BLOCK_TO_BB (bbi);
3452
3453 for (old_pos = ebb_head[BLOCK_TO_BB (check_bbi) + 1] - 1;
3454 rgn_bb_table[old_pos] != check_bb_nexti;
3455 old_pos--);
3456 gcc_assert (old_pos > ebb_head[BLOCK_TO_BB (check_bbi)]);
3457
3458 for (new_pos = ebb_head[BLOCK_TO_BB (bbi) + 1] - 1;
3459 rgn_bb_table[new_pos] != bbi;
3460 new_pos--);
3461 new_pos++;
3462 gcc_assert (new_pos > ebb_head[BLOCK_TO_BB (bbi)]);
3463
3464 gcc_assert (new_pos < old_pos);
3465
3466 memmove (rgn_bb_table + new_pos + 1,
3467 rgn_bb_table + new_pos,
3468 (old_pos - new_pos) * sizeof (*rgn_bb_table));
3469
3470 rgn_bb_table[new_pos] = check_bb_nexti;
3471
3472 for (i = BLOCK_TO_BB (bbi) + 1; i <= BLOCK_TO_BB (check_bbi); i++)
3473 ebb_head[i]++;
3474 }
3475
3476 /* Return next block in ebb chain. For parameter meaning please refer to
3477 sched-int.h: struct sched_info: advance_target_bb. */
3478 static basic_block
3479 advance_target_bb (basic_block bb, rtx insn)
3480 {
3481 if (insn)
3482 return 0;
3483
3484 gcc_assert (BLOCK_TO_BB (bb->index) == target_bb
3485 && BLOCK_TO_BB (bb->next_bb->index) == target_bb);
3486 return bb->next_bb;
3487 }
3488
3489 #endif
3490 \f
3491 static bool
3492 gate_handle_sched (void)
3493 {
3494 #ifdef INSN_SCHEDULING
3495 return flag_schedule_insns && dbg_cnt (sched_func);
3496 #else
3497 return 0;
3498 #endif
3499 }
3500
3501 /* Run instruction scheduler. */
3502 static unsigned int
3503 rest_of_handle_sched (void)
3504 {
3505 #ifdef INSN_SCHEDULING
3506 if (flag_selective_scheduling
3507 && ! maybe_skip_selective_scheduling ())
3508 run_selective_scheduling ();
3509 else
3510 schedule_insns ();
3511 #endif
3512 return 0;
3513 }
3514
3515 static bool
3516 gate_handle_sched2 (void)
3517 {
3518 #ifdef INSN_SCHEDULING
3519 return optimize > 0 && flag_schedule_insns_after_reload
3520 && dbg_cnt (sched2_func);
3521 #else
3522 return 0;
3523 #endif
3524 }
3525
3526 /* Run second scheduling pass after reload. */
3527 static unsigned int
3528 rest_of_handle_sched2 (void)
3529 {
3530 #ifdef INSN_SCHEDULING
3531 if (flag_selective_scheduling2
3532 && ! maybe_skip_selective_scheduling ())
3533 run_selective_scheduling ();
3534 else
3535 {
3536 /* Do control and data sched analysis again,
3537 and write some more of the results to dump file. */
3538 if (flag_sched2_use_superblocks)
3539 schedule_ebbs ();
3540 else
3541 schedule_insns ();
3542 }
3543 #endif
3544 return 0;
3545 }
3546
3547 struct rtl_opt_pass pass_sched =
3548 {
3549 {
3550 RTL_PASS,
3551 "sched1", /* name */
3552 gate_handle_sched, /* gate */
3553 rest_of_handle_sched, /* execute */
3554 NULL, /* sub */
3555 NULL, /* next */
3556 0, /* static_pass_number */
3557 TV_SCHED, /* tv_id */
3558 0, /* properties_required */
3559 0, /* properties_provided */
3560 0, /* properties_destroyed */
3561 0, /* todo_flags_start */
3562 TODO_df_finish | TODO_verify_rtl_sharing |
3563 TODO_dump_func |
3564 TODO_verify_flow |
3565 TODO_ggc_collect /* todo_flags_finish */
3566 }
3567 };
3568
3569 struct rtl_opt_pass pass_sched2 =
3570 {
3571 {
3572 RTL_PASS,
3573 "sched2", /* name */
3574 gate_handle_sched2, /* gate */
3575 rest_of_handle_sched2, /* execute */
3576 NULL, /* sub */
3577 NULL, /* next */
3578 0, /* static_pass_number */
3579 TV_SCHED2, /* tv_id */
3580 0, /* properties_required */
3581 0, /* properties_provided */
3582 0, /* properties_destroyed */
3583 0, /* todo_flags_start */
3584 TODO_df_finish | TODO_verify_rtl_sharing |
3585 TODO_dump_func |
3586 TODO_verify_flow |
3587 TODO_ggc_collect /* todo_flags_finish */
3588 }
3589 };