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