]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/sched-rgn.c
2005-07-05 Paolo Bonzini <bonzini@gnu.org>
[thirdparty/gcc.git] / gcc / sched-rgn.c
CommitLineData
7a31a7bd 1/* Instruction scheduling pass.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
2b4876d2 3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
7a31a7bd 4 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
5 and currently maintained by, Jim Wilson (wilson@cygnus.com)
6
f12b58b3 7This file is part of GCC.
7a31a7bd 8
f12b58b3 9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
11Software Foundation; either version 2, or (at your option) any later
12version.
7a31a7bd 13
f12b58b3 14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
7a31a7bd 16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
18
19You should have received a copy of the GNU General Public License
f660683a 20along with GCC; see the file COPYING. If not, write to the Free
67ce556b 21Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2202110-1301, USA. */
7a31a7bd 23
24/* This pass implements list scheduling within basic blocks. It is
25 run twice: (1) after flow analysis, but before register allocation,
26 and (2) after register allocation.
27
28 The first run performs interblock scheduling, moving insns between
29 different blocks in the same "region", and the second runs only
30 basic block scheduling.
31
32 Interblock motions performed are useful motions and speculative
33 motions, including speculative loads. Motions requiring code
34 duplication are not supported. The identification of motion type
35 and the check for validity of speculative motions requires
36 construction and analysis of the function's control flow graph.
37
38 The main entry point for this pass is schedule_insns(), called for
39 each function. The work of the scheduler is organized in three
40 levels: (1) function level: insns are subject to splitting,
41 control-flow-graph is constructed, regions are computed (after
42 reload, each region is of one block), (2) region level: control
43 flow graph attributes required for interblock scheduling are
44 computed (dominators, reachability, etc.), data dependences and
45 priorities are computed, and (3) block level: insns in the block
46 are actually scheduled. */
47\f
48#include "config.h"
49#include "system.h"
805e22b2 50#include "coretypes.h"
51#include "tm.h"
7a31a7bd 52#include "toplev.h"
53#include "rtl.h"
54#include "tm_p.h"
55#include "hard-reg-set.h"
7a31a7bd 56#include "regs.h"
57#include "function.h"
58#include "flags.h"
59#include "insn-config.h"
60#include "insn-attr.h"
61#include "except.h"
62#include "toplev.h"
63#include "recog.h"
78587df3 64#include "cfglayout.h"
4c50e1f4 65#include "params.h"
7a31a7bd 66#include "sched-int.h"
bea4bad2 67#include "target.h"
77fce4cd 68#include "timevar.h"
69#include "tree-pass.h"
7a31a7bd 70
7fb47f9f 71/* Define when we want to do count REG_DEAD notes before and after scheduling
72 for sanity checking. We can't do that when conditional execution is used,
73 as REG_DEAD exist only for unconditional deaths. */
74
75#if !defined (HAVE_conditional_execution) && defined (ENABLE_CHECKING)
76#define CHECK_DEAD_NOTES 1
77#else
78#define CHECK_DEAD_NOTES 0
79#endif
80
81
cda0a5f5 82#ifdef INSN_SCHEDULING
7a31a7bd 83/* Some accessor macros for h_i_d members only used within this file. */
84#define INSN_REF_COUNT(INSN) (h_i_d[INSN_UID (INSN)].ref_count)
85#define FED_BY_SPEC_LOAD(insn) (h_i_d[INSN_UID (insn)].fed_by_spec_load)
86#define IS_LOAD_INSN(insn) (h_i_d[INSN_UID (insn)].is_load_insn)
87
7a31a7bd 88/* nr_inter/spec counts interblock/speculative motion for the function. */
89static int nr_inter, nr_spec;
90
60b8c5b3 91static int is_cfg_nonregular (void);
f045d41d 92static bool sched_is_disabled_for_current_region_p (void);
7a31a7bd 93
94/* A region is the main entity for interblock scheduling: insns
95 are allowed to move between blocks in the same region, along
96 control flow graph edges, in the 'up' direction. */
97typedef struct
98{
99 int rgn_nr_blocks; /* Number of blocks in region. */
100 int rgn_blocks; /* cblocks in the region (actually index in rgn_bb_table). */
101}
102region;
103
104/* Number of regions in the procedure. */
105static int nr_regions;
106
107/* Table of region descriptions. */
108static region *rgn_table;
109
110/* Array of lists of regions' blocks. */
111static int *rgn_bb_table;
112
113/* Topological order of blocks in the region (if b2 is reachable from
114 b1, block_to_bb[b2] > block_to_bb[b1]). Note: A basic block is
115 always referred to by either block or b, while its topological
917bbcab 116 order name (in the region) is referred to by bb. */
7a31a7bd 117static int *block_to_bb;
118
119/* The number of the region containing a block. */
120static int *containing_rgn;
121
122#define RGN_NR_BLOCKS(rgn) (rgn_table[rgn].rgn_nr_blocks)
123#define RGN_BLOCKS(rgn) (rgn_table[rgn].rgn_blocks)
124#define BLOCK_TO_BB(block) (block_to_bb[block])
125#define CONTAINING_RGN(block) (containing_rgn[block])
126
60b8c5b3 127void debug_regions (void);
128static void find_single_block_region (void);
aae97b21 129static void find_rgns (void);
4c50e1f4 130static bool too_large (int, int *, int *);
7a31a7bd 131
60b8c5b3 132extern void debug_live (int, int);
7a31a7bd 133
134/* Blocks of the current region being scheduled. */
135static int current_nr_blocks;
136static int current_blocks;
137
138/* The mapping from bb to block. */
139#define BB_TO_BLOCK(bb) (rgn_bb_table[current_blocks + (bb)])
140
7a31a7bd 141/* Target info declarations.
142
143 The block currently being scheduled is referred to as the "target" block,
144 while other blocks in the region from which insns can be moved to the
145 target are called "source" blocks. The candidate structure holds info
146 about such sources: are they valid? Speculative? Etc. */
aae97b21 147typedef struct
148{
149 basic_block *first_member;
150 int nr_members;
151}
152bblst;
153
7a31a7bd 154typedef struct
155{
156 char is_valid;
157 char is_speculative;
158 int src_prob;
159 bblst split_bbs;
160 bblst update_bbs;
161}
162candidate;
163
164static candidate *candidate_table;
165
166/* A speculative motion requires checking live information on the path
167 from 'source' to 'target'. The split blocks are those to be checked.
168 After a speculative motion, live information should be modified in
169 the 'update' blocks.
170
171 Lists of split and update blocks for each candidate of the current
172 target are in array bblst_table. */
aae97b21 173static basic_block *bblst_table;
174static int bblst_size, bblst_last;
7a31a7bd 175
176#define IS_VALID(src) ( candidate_table[src].is_valid )
177#define IS_SPECULATIVE(src) ( candidate_table[src].is_speculative )
178#define SRC_PROB(src) ( candidate_table[src].src_prob )
179
180/* The bb being currently scheduled. */
181static int target_bb;
182
183/* List of edges. */
aae97b21 184typedef struct
185{
186 edge *first_member;
187 int nr_members;
188}
189edgelst;
190
191static edge *edgelst_table;
192static int edgelst_last;
193
194static void extract_edgelst (sbitmap, edgelst *);
195
7a31a7bd 196
197/* Target info functions. */
60b8c5b3 198static void split_edges (int, int, edgelst *);
199static void compute_trg_info (int);
200void debug_candidate (int);
201void debug_candidates (int);
7a31a7bd 202
79cafa9e 203/* Dominators array: dom[i] contains the sbitmap of dominators of
7a31a7bd 204 bb i in the region. */
79cafa9e 205static sbitmap *dom;
7a31a7bd 206
207/* bb 0 is the only region entry. */
208#define IS_RGN_ENTRY(bb) (!bb)
209
210/* Is bb_src dominated by bb_trg. */
211#define IS_DOMINATED(bb_src, bb_trg) \
79cafa9e 212( TEST_BIT (dom[bb_src], bb_trg) )
7a31a7bd 213
214/* Probability: Prob[i] is a float in [0, 1] which is the probability
215 of bb i relative to the region entry. */
216static float *prob;
217
218/* The probability of bb_src, relative to bb_trg. Note, that while the
219 'prob[bb]' is a float in [0, 1], this macro returns an integer
220 in [0, 100]. */
221#define GET_SRC_PROB(bb_src, bb_trg) ((int) (100.0 * (prob[bb_src] / \
222 prob[bb_trg])))
223
224/* Bit-set of edges, where bit i stands for edge i. */
79cafa9e 225typedef sbitmap edgeset;
7a31a7bd 226
227/* Number of edges in the region. */
228static int rgn_nr_edges;
229
230/* Array of size rgn_nr_edges. */
aae97b21 231static edge *rgn_edges;
7a31a7bd 232
233/* Mapping from each edge in the graph to its number in the rgn. */
aae97b21 234#define EDGE_TO_BIT(edge) ((int)(size_t)(edge)->aux)
235#define SET_EDGE_TO_BIT(edge,nr) ((edge)->aux = (void *)(size_t)(nr))
7a31a7bd 236
237/* The split edges of a source bb is different for each target
238 bb. In order to compute this efficiently, the 'potential-split edges'
239 are computed for each bb prior to scheduling a region. This is actually
240 the split edges of each bb relative to the region entry.
241
242 pot_split[bb] is the set of potential split edges of bb. */
243static edgeset *pot_split;
244
245/* For every bb, a set of its ancestor edges. */
246static edgeset *ancestor_edges;
247
60b8c5b3 248static void compute_dom_prob_ps (int);
7a31a7bd 249
7a31a7bd 250#define INSN_PROBABILITY(INSN) (SRC_PROB (BLOCK_TO_BB (BLOCK_NUM (INSN))))
251#define IS_SPECULATIVE_INSN(INSN) (IS_SPECULATIVE (BLOCK_TO_BB (BLOCK_NUM (INSN))))
252#define INSN_BB(INSN) (BLOCK_TO_BB (BLOCK_NUM (INSN)))
253
254/* Parameters affecting the decision of rank_for_schedule().
de132707 255 ??? Nope. But MIN_PROBABILITY is used in compute_trg_info. */
7a31a7bd 256#define MIN_PROBABILITY 40
7a31a7bd 257
258/* Speculative scheduling functions. */
60b8c5b3 259static int check_live_1 (int, rtx);
260static void update_live_1 (int, rtx);
261static int check_live (rtx, int);
262static void update_live (rtx, int);
263static void set_spec_fed (rtx);
264static int is_pfree (rtx, int, int);
265static int find_conditional_protection (rtx, int);
266static int is_conditionally_protected (rtx, int, int);
267static int is_prisky (rtx, int, int);
268static int is_exception_free (rtx, int, int);
269
270static bool sets_likely_spilled (rtx);
271static void sets_likely_spilled_1 (rtx, rtx, void *);
272static void add_branch_dependences (rtx, rtx);
273static void compute_block_backward_dependences (int);
274void debug_dependencies (void);
275
276static void init_regions (void);
277static void schedule_region (int);
278static rtx concat_INSN_LIST (rtx, rtx);
279static void concat_insn_mem_list (rtx, rtx, rtx *, rtx *);
280static void propagate_deps (int, struct deps *);
281static void free_pending_lists (void);
7a31a7bd 282
283/* Functions for construction of the control flow graph. */
284
285/* Return 1 if control flow graph should not be constructed, 0 otherwise.
286
287 We decide not to build the control flow graph if there is possibly more
aae97b21 288 than one entry to the function, if computed branches exist, if we
289 have nonlocal gotos, or if we have an unreachable loop. */
7a31a7bd 290
291static int
60b8c5b3 292is_cfg_nonregular (void)
7a31a7bd 293{
4c26117a 294 basic_block b;
7a31a7bd 295 rtx insn;
7a31a7bd 296
297 /* If we have a label that could be the target of a nonlocal goto, then
298 the cfg is not well structured. */
299 if (nonlocal_goto_handler_labels)
300 return 1;
301
302 /* If we have any forced labels, then the cfg is not well structured. */
303 if (forced_labels)
304 return 1;
305
7a31a7bd 306 /* If we have exception handlers, then we consider the cfg not well
307 structured. ?!? We should be able to handle this now that flow.c
308 computes an accurate cfg for EH. */
8f8dcce4 309 if (current_function_has_exception_handlers ())
7a31a7bd 310 return 1;
311
312 /* If we have non-jumping insns which refer to labels, then we consider
313 the cfg not well structured. */
4c26117a 314 FOR_EACH_BB (b)
cd6dccd3 315 FOR_BB_INSNS (b, insn)
7a31a7bd 316 {
cd6dccd3 317 /* Check for labels referred by non-jump insns. */
318 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
7a31a7bd 319 {
01c9a77a 320 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
d3ff0f75 321 if (note
6d7dc5b9 322 && ! (JUMP_P (NEXT_INSN (insn))
01c9a77a 323 && find_reg_note (NEXT_INSN (insn), REG_LABEL,
d3ff0f75 324 XEXP (note, 0))))
325 return 1;
7a31a7bd 326 }
cd6dccd3 327 /* If this function has a computed jump, then we consider the cfg
328 not well structured. */
329 else if (JUMP_P (insn) && computed_jump_p (insn))
330 return 1;
7a31a7bd 331 }
332
7a31a7bd 333 /* Unreachable loops with more than one basic block are detected
334 during the DFS traversal in find_rgns.
335
336 Unreachable loops with a single block are detected here. This
337 test is redundant with the one in find_rgns, but it's much
aae97b21 338 cheaper to go ahead and catch the trivial case here. */
4c26117a 339 FOR_EACH_BB (b)
7a31a7bd 340 {
cd665a06 341 if (EDGE_COUNT (b->preds) == 0
ea091dfd 342 || (single_pred_p (b)
343 && single_pred (b) == b))
aae97b21 344 return 1;
7a31a7bd 345 }
346
aae97b21 347 /* All the tests passed. Consider the cfg well structured. */
348 return 0;
7a31a7bd 349}
350
aae97b21 351/* Extract list of edges from a bitmap containing EDGE_TO_BIT bits. */
7a31a7bd 352
353static void
aae97b21 354extract_edgelst (sbitmap set, edgelst *el)
7a31a7bd 355{
3e790786 356 unsigned int i;
357 sbitmap_iterator sbi;
7a31a7bd 358
aae97b21 359 /* edgelst table space is reused in each call to extract_edgelst. */
360 edgelst_last = 0;
7a31a7bd 361
aae97b21 362 el->first_member = &edgelst_table[edgelst_last];
363 el->nr_members = 0;
7a31a7bd 364
365 /* Iterate over each word in the bitset. */
3e790786 366 EXECUTE_IF_SET_IN_SBITMAP (set, 0, i, sbi)
367 {
368 edgelst_table[edgelst_last++] = rgn_edges[i];
369 el->nr_members++;
370 }
7a31a7bd 371}
372
373/* Functions for the construction of regions. */
374
375/* Print the regions, for debugging purposes. Callable from debugger. */
376
377void
60b8c5b3 378debug_regions (void)
7a31a7bd 379{
380 int rgn, bb;
381
382 fprintf (sched_dump, "\n;; ------------ REGIONS ----------\n\n");
383 for (rgn = 0; rgn < nr_regions; rgn++)
384 {
385 fprintf (sched_dump, ";;\trgn %d nr_blocks %d:\n", rgn,
386 rgn_table[rgn].rgn_nr_blocks);
387 fprintf (sched_dump, ";;\tbb/block: ");
388
389 for (bb = 0; bb < rgn_table[rgn].rgn_nr_blocks; bb++)
390 {
391 current_blocks = RGN_BLOCKS (rgn);
392
04e579b6 393 gcc_assert (bb == BLOCK_TO_BB (BB_TO_BLOCK (bb)));
7a31a7bd 394 fprintf (sched_dump, " %d/%d ", bb, BB_TO_BLOCK (bb));
395 }
396
397 fprintf (sched_dump, "\n\n");
398 }
399}
400
401/* Build a single block region for each basic block in the function.
402 This allows for using the same code for interblock and basic block
403 scheduling. */
404
405static void
60b8c5b3 406find_single_block_region (void)
7a31a7bd 407{
4c26117a 408 basic_block bb;
4c5da238 409
4c26117a 410 nr_regions = 0;
411
412 FOR_EACH_BB (bb)
7a31a7bd 413 {
4c26117a 414 rgn_bb_table[nr_regions] = bb->index;
415 RGN_NR_BLOCKS (nr_regions) = 1;
416 RGN_BLOCKS (nr_regions) = nr_regions;
417 CONTAINING_RGN (bb->index) = nr_regions;
418 BLOCK_TO_BB (bb->index) = 0;
419 nr_regions++;
7a31a7bd 420 }
7a31a7bd 421}
422
423/* Update number of blocks and the estimate for number of insns
4c50e1f4 424 in the region. Return true if the region is "too large" for interblock
425 scheduling (compile time considerations). */
7a31a7bd 426
4c50e1f4 427static bool
60b8c5b3 428too_large (int block, int *num_bbs, int *num_insns)
7a31a7bd 429{
430 (*num_bbs)++;
4c50e1f4 431 (*num_insns) += (INSN_LUID (BB_END (BASIC_BLOCK (block)))
432 - INSN_LUID (BB_HEAD (BASIC_BLOCK (block))));
433
434 return ((*num_bbs > PARAM_VALUE (PARAM_MAX_SCHED_REGION_BLOCKS))
435 || (*num_insns > PARAM_VALUE (PARAM_MAX_SCHED_REGION_INSNS)));
7a31a7bd 436}
437
438/* Update_loop_relations(blk, hdr): Check if the loop headed by max_hdr[blk]
439 is still an inner loop. Put in max_hdr[blk] the header of the most inner
440 loop containing blk. */
40734805 441#define UPDATE_LOOP_RELATIONS(blk, hdr) \
442{ \
443 if (max_hdr[blk] == -1) \
444 max_hdr[blk] = hdr; \
445 else if (dfs_nr[max_hdr[blk]] > dfs_nr[hdr]) \
446 RESET_BIT (inner, hdr); \
447 else if (dfs_nr[max_hdr[blk]] < dfs_nr[hdr]) \
448 { \
449 RESET_BIT (inner,max_hdr[blk]); \
450 max_hdr[blk] = hdr; \
451 } \
7a31a7bd 452}
453
454/* Find regions for interblock scheduling.
455
456 A region for scheduling can be:
457
458 * A loop-free procedure, or
459
460 * A reducible inner loop, or
461
462 * A basic block not contained in any other region.
463
464 ?!? In theory we could build other regions based on extended basic
465 blocks or reverse extended basic blocks. Is it worth the trouble?
466
467 Loop blocks that form a region are put into the region's block list
468 in topological order.
469
470 This procedure stores its results into the following global (ick) variables
471
472 * rgn_nr
473 * rgn_table
474 * rgn_bb_table
475 * block_to_bb
476 * containing region
477
478 We use dominator relationships to avoid making regions out of non-reducible
479 loops.
480
481 This procedure needs to be converted to work on pred/succ lists instead
482 of edge tables. That would simplify it somewhat. */
483
484static void
aae97b21 485find_rgns (void)
7a31a7bd 486{
aae97b21 487 int *max_hdr, *dfs_nr, *degree;
7a31a7bd 488 char no_loops = 1;
489 int node, child, loop_head, i, head, tail;
8deb7557 490 int count = 0, sp, idx = 0;
aae97b21 491 edge_iterator current_edge;
492 edge_iterator *stack;
7a31a7bd 493 int num_bbs, num_insns, unreachable;
494 int too_large_failure;
4c26117a 495 basic_block bb;
7a31a7bd 496
7a31a7bd 497 /* Note if a block is a natural loop header. */
498 sbitmap header;
499
edc2a478 500 /* Note if a block is a natural inner loop header. */
7a31a7bd 501 sbitmap inner;
502
503 /* Note if a block is in the block queue. */
504 sbitmap in_queue;
505
506 /* Note if a block is in the block queue. */
507 sbitmap in_stack;
508
7a31a7bd 509 /* Perform a DFS traversal of the cfg. Identify loop headers, inner loops
510 and a mapping from block to its loop header (if the block is contained
511 in a loop, else -1).
512
513 Store results in HEADER, INNER, and MAX_HDR respectively, these will
514 be used as inputs to the second traversal.
515
516 STACK, SP and DFS_NR are only used during the first traversal. */
517
518 /* Allocate and initialize variables for the first traversal. */
f0af5a88 519 max_hdr = xmalloc (last_basic_block * sizeof (int));
520 dfs_nr = xcalloc (last_basic_block, sizeof (int));
aae97b21 521 stack = xmalloc (n_edges * sizeof (edge_iterator));
7a31a7bd 522
f20183e6 523 inner = sbitmap_alloc (last_basic_block);
7a31a7bd 524 sbitmap_ones (inner);
525
f20183e6 526 header = sbitmap_alloc (last_basic_block);
7a31a7bd 527 sbitmap_zero (header);
528
f20183e6 529 in_queue = sbitmap_alloc (last_basic_block);
7a31a7bd 530 sbitmap_zero (in_queue);
531
f20183e6 532 in_stack = sbitmap_alloc (last_basic_block);
7a31a7bd 533 sbitmap_zero (in_stack);
534
3c0a32c9 535 for (i = 0; i < last_basic_block; i++)
7a31a7bd 536 max_hdr[i] = -1;
537
aae97b21 538 #define EDGE_PASSED(E) (ei_end_p ((E)) || ei_edge ((E))->aux)
539 #define SET_EDGE_PASSED(E) (ei_edge ((E))->aux = ei_edge ((E)))
540
7a31a7bd 541 /* DFS traversal to find inner loops in the cfg. */
542
ea091dfd 543 current_edge = ei_start (single_succ (ENTRY_BLOCK_PTR)->succs);
7a31a7bd 544 sp = -1;
aae97b21 545
7a31a7bd 546 while (1)
547 {
aae97b21 548 if (EDGE_PASSED (current_edge))
7a31a7bd 549 {
550 /* We have reached a leaf node or a node that was already
551 processed. Pop edges off the stack until we find
552 an edge that has not yet been processed. */
aae97b21 553 while (sp >= 0 && EDGE_PASSED (current_edge))
7a31a7bd 554 {
555 /* Pop entry off the stack. */
556 current_edge = stack[sp--];
aae97b21 557 node = ei_edge (current_edge)->src->index;
558 gcc_assert (node != ENTRY_BLOCK);
559 child = ei_edge (current_edge)->dest->index;
560 gcc_assert (child != EXIT_BLOCK);
7a31a7bd 561 RESET_BIT (in_stack, child);
562 if (max_hdr[child] >= 0 && TEST_BIT (in_stack, max_hdr[child]))
563 UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
aae97b21 564 ei_next (&current_edge);
7a31a7bd 565 }
566
567 /* See if have finished the DFS tree traversal. */
aae97b21 568 if (sp < 0 && EDGE_PASSED (current_edge))
7a31a7bd 569 break;
570
571 /* Nope, continue the traversal with the popped node. */
572 continue;
573 }
574
575 /* Process a node. */
aae97b21 576 node = ei_edge (current_edge)->src->index;
577 gcc_assert (node != ENTRY_BLOCK);
7a31a7bd 578 SET_BIT (in_stack, node);
579 dfs_nr[node] = ++count;
580
aae97b21 581 /* We don't traverse to the exit block. */
582 child = ei_edge (current_edge)->dest->index;
583 if (child == EXIT_BLOCK)
584 {
585 SET_EDGE_PASSED (current_edge);
586 ei_next (&current_edge);
587 continue;
588 }
589
7a31a7bd 590 /* If the successor is in the stack, then we've found a loop.
591 Mark the loop, if it is not a natural loop, then it will
592 be rejected during the second traversal. */
593 if (TEST_BIT (in_stack, child))
594 {
595 no_loops = 0;
596 SET_BIT (header, child);
597 UPDATE_LOOP_RELATIONS (node, child);
aae97b21 598 SET_EDGE_PASSED (current_edge);
599 ei_next (&current_edge);
7a31a7bd 600 continue;
601 }
602
603 /* If the child was already visited, then there is no need to visit
604 it again. Just update the loop relationships and restart
605 with a new edge. */
606 if (dfs_nr[child])
607 {
608 if (max_hdr[child] >= 0 && TEST_BIT (in_stack, max_hdr[child]))
609 UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
aae97b21 610 SET_EDGE_PASSED (current_edge);
611 ei_next (&current_edge);
7a31a7bd 612 continue;
613 }
614
615 /* Push an entry on the stack and continue DFS traversal. */
616 stack[++sp] = current_edge;
aae97b21 617 SET_EDGE_PASSED (current_edge);
618 current_edge = ei_start (ei_edge (current_edge)->dest->succs);
619 }
620
621 /* Reset ->aux field used by EDGE_PASSED. */
622 FOR_ALL_BB (bb)
623 {
624 edge_iterator ei;
625 edge e;
626 FOR_EACH_EDGE (e, ei, bb->succs)
627 e->aux = NULL;
7a31a7bd 628 }
629
aae97b21 630
7a31a7bd 631 /* Another check for unreachable blocks. The earlier test in
632 is_cfg_nonregular only finds unreachable blocks that do not
633 form a loop.
634
635 The DFS traversal will mark every block that is reachable from
636 the entry node by placing a nonzero value in dfs_nr. Thus if
637 dfs_nr is zero for any block, then it must be unreachable. */
638 unreachable = 0;
4c26117a 639 FOR_EACH_BB (bb)
640 if (dfs_nr[bb->index] == 0)
7a31a7bd 641 {
642 unreachable = 1;
643 break;
644 }
645
646 /* Gross. To avoid wasting memory, the second pass uses the dfs_nr array
647 to hold degree counts. */
648 degree = dfs_nr;
649
4c26117a 650 FOR_EACH_BB (bb)
aae97b21 651 degree[bb->index] = EDGE_COUNT (bb->preds);
7a31a7bd 652
653 /* Do not perform region scheduling if there are any unreachable
654 blocks. */
655 if (!unreachable)
656 {
657 int *queue;
658
659 if (no_loops)
660 SET_BIT (header, 0);
661
de132707 662 /* Second traversal:find reducible inner loops and topologically sort
7a31a7bd 663 block of each region. */
664
f0af5a88 665 queue = xmalloc (n_basic_blocks * sizeof (int));
7a31a7bd 666
667 /* Find blocks which are inner loop headers. We still have non-reducible
668 loops to consider at this point. */
4c26117a 669 FOR_EACH_BB (bb)
7a31a7bd 670 {
4c26117a 671 if (TEST_BIT (header, bb->index) && TEST_BIT (inner, bb->index))
7a31a7bd 672 {
673 edge e;
cd665a06 674 edge_iterator ei;
4c26117a 675 basic_block jbb;
7a31a7bd 676
677 /* Now check that the loop is reducible. We do this separate
678 from finding inner loops so that we do not find a reducible
679 loop which contains an inner non-reducible loop.
680
681 A simple way to find reducible/natural loops is to verify
682 that each block in the loop is dominated by the loop
683 header.
684
685 If there exists a block that is not dominated by the loop
686 header, then the block is reachable from outside the loop
687 and thus the loop is not a natural loop. */
4c26117a 688 FOR_EACH_BB (jbb)
7a31a7bd 689 {
690 /* First identify blocks in the loop, except for the loop
691 entry block. */
4c26117a 692 if (bb->index == max_hdr[jbb->index] && bb != jbb)
7a31a7bd 693 {
694 /* Now verify that the block is dominated by the loop
695 header. */
0051c76a 696 if (!dominated_by_p (CDI_DOMINATORS, jbb, bb))
7a31a7bd 697 break;
698 }
699 }
700
701 /* If we exited the loop early, then I is the header of
702 a non-reducible loop and we should quit processing it
703 now. */
4c26117a 704 if (jbb != EXIT_BLOCK_PTR)
7a31a7bd 705 continue;
706
707 /* I is a header of an inner loop, or block 0 in a subroutine
708 with no loops at all. */
709 head = tail = -1;
710 too_large_failure = 0;
4c26117a 711 loop_head = max_hdr[bb->index];
7a31a7bd 712
713 /* Decrease degree of all I's successors for topological
714 ordering. */
cd665a06 715 FOR_EACH_EDGE (e, ei, bb->succs)
7a31a7bd 716 if (e->dest != EXIT_BLOCK_PTR)
b3d6de89 717 --degree[e->dest->index];
7a31a7bd 718
719 /* Estimate # insns, and count # blocks in the region. */
720 num_bbs = 1;
5496dbfc 721 num_insns = (INSN_LUID (BB_END (bb))
722 - INSN_LUID (BB_HEAD (bb)));
7a31a7bd 723
724 /* Find all loop latches (blocks with back edges to the loop
725 header) or all the leaf blocks in the cfg has no loops.
726
727 Place those blocks into the queue. */
728 if (no_loops)
729 {
4c26117a 730 FOR_EACH_BB (jbb)
7a31a7bd 731 /* Leaf nodes have only a single successor which must
732 be EXIT_BLOCK. */
ea091dfd 733 if (single_succ_p (jbb)
734 && single_succ (jbb) == EXIT_BLOCK_PTR)
7a31a7bd 735 {
4c26117a 736 queue[++tail] = jbb->index;
737 SET_BIT (in_queue, jbb->index);
7a31a7bd 738
4c26117a 739 if (too_large (jbb->index, &num_bbs, &num_insns))
7a31a7bd 740 {
741 too_large_failure = 1;
742 break;
743 }
744 }
745 }
746 else
747 {
748 edge e;
749
cd665a06 750 FOR_EACH_EDGE (e, ei, bb->preds)
7a31a7bd 751 {
752 if (e->src == ENTRY_BLOCK_PTR)
753 continue;
754
b3d6de89 755 node = e->src->index;
7a31a7bd 756
4c26117a 757 if (max_hdr[node] == loop_head && node != bb->index)
7a31a7bd 758 {
759 /* This is a loop latch. */
760 queue[++tail] = node;
761 SET_BIT (in_queue, node);
762
763 if (too_large (node, &num_bbs, &num_insns))
764 {
765 too_large_failure = 1;
766 break;
767 }
768 }
769 }
770 }
771
772 /* Now add all the blocks in the loop to the queue.
773
774 We know the loop is a natural loop; however the algorithm
775 above will not always mark certain blocks as being in the
776 loop. Consider:
777 node children
778 a b,c
779 b c
780 c a,d
781 d b
782
783 The algorithm in the DFS traversal may not mark B & D as part
0c6d8c36 784 of the loop (i.e. they will not have max_hdr set to A).
7a31a7bd 785
786 We know they can not be loop latches (else they would have
787 had max_hdr set since they'd have a backedge to a dominator
788 block). So we don't need them on the initial queue.
789
790 We know they are part of the loop because they are dominated
791 by the loop header and can be reached by a backwards walk of
792 the edges starting with nodes on the initial queue.
793
794 It is safe and desirable to include those nodes in the
795 loop/scheduling region. To do so we would need to decrease
796 the degree of a node if it is the target of a backedge
797 within the loop itself as the node is placed in the queue.
798
799 We do not do this because I'm not sure that the actual
800 scheduling code will properly handle this case. ?!? */
801
802 while (head < tail && !too_large_failure)
803 {
804 edge e;
805 child = queue[++head];
806
cd665a06 807 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (child)->preds)
7a31a7bd 808 {
b3d6de89 809 node = e->src->index;
7a31a7bd 810
811 /* See discussion above about nodes not marked as in
812 this loop during the initial DFS traversal. */
813 if (e->src == ENTRY_BLOCK_PTR
814 || max_hdr[node] != loop_head)
815 {
816 tail = -1;
817 break;
818 }
4c26117a 819 else if (!TEST_BIT (in_queue, node) && node != bb->index)
7a31a7bd 820 {
821 queue[++tail] = node;
822 SET_BIT (in_queue, node);
823
824 if (too_large (node, &num_bbs, &num_insns))
825 {
826 too_large_failure = 1;
827 break;
828 }
829 }
830 }
831 }
832
833 if (tail >= 0 && !too_large_failure)
834 {
835 /* Place the loop header into list of region blocks. */
4c26117a 836 degree[bb->index] = -1;
837 rgn_bb_table[idx] = bb->index;
7a31a7bd 838 RGN_NR_BLOCKS (nr_regions) = num_bbs;
839 RGN_BLOCKS (nr_regions) = idx++;
4c26117a 840 CONTAINING_RGN (bb->index) = nr_regions;
841 BLOCK_TO_BB (bb->index) = count = 0;
7a31a7bd 842
843 /* Remove blocks from queue[] when their in degree
844 becomes zero. Repeat until no blocks are left on the
845 list. This produces a topological list of blocks in
846 the region. */
847 while (tail >= 0)
848 {
849 if (head < 0)
850 head = tail;
851 child = queue[head];
852 if (degree[child] == 0)
853 {
854 edge e;
855
856 degree[child] = -1;
857 rgn_bb_table[idx++] = child;
858 BLOCK_TO_BB (child) = ++count;
859 CONTAINING_RGN (child) = nr_regions;
860 queue[head] = queue[tail--];
861
cd665a06 862 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (child)->succs)
7a31a7bd 863 if (e->dest != EXIT_BLOCK_PTR)
b3d6de89 864 --degree[e->dest->index];
7a31a7bd 865 }
866 else
867 --head;
868 }
869 ++nr_regions;
870 }
871 }
872 }
873 free (queue);
874 }
875
876 /* Any block that did not end up in a region is placed into a region
877 by itself. */
4c26117a 878 FOR_EACH_BB (bb)
879 if (degree[bb->index] >= 0)
7a31a7bd 880 {
4c26117a 881 rgn_bb_table[idx] = bb->index;
7a31a7bd 882 RGN_NR_BLOCKS (nr_regions) = 1;
883 RGN_BLOCKS (nr_regions) = idx++;
4c26117a 884 CONTAINING_RGN (bb->index) = nr_regions++;
885 BLOCK_TO_BB (bb->index) = 0;
7a31a7bd 886 }
887
888 free (max_hdr);
889 free (dfs_nr);
890 free (stack);
265a9759 891 sbitmap_free (header);
892 sbitmap_free (inner);
893 sbitmap_free (in_queue);
894 sbitmap_free (in_stack);
7a31a7bd 895}
896
897/* Functions for regions scheduling information. */
898
899/* Compute dominators, probability, and potential-split-edges of bb.
900 Assume that these values were already computed for bb's predecessors. */
901
902static void
60b8c5b3 903compute_dom_prob_ps (int bb)
7a31a7bd 904{
aae97b21 905 int pred_bb;
906 int nr_out_edges, nr_rgn_out_edges;
907 edge_iterator in_ei, out_ei;
908 edge in_edge, out_edge;
7a31a7bd 909
910 prob[bb] = 0.0;
911 if (IS_RGN_ENTRY (bb))
912 {
79cafa9e 913 SET_BIT (dom[bb], 0);
7a31a7bd 914 prob[bb] = 1.0;
915 return;
916 }
917
4a82352a 918 /* Initialize dom[bb] to '111..1'. */
79cafa9e 919 sbitmap_ones (dom[bb]);
7a31a7bd 920
aae97b21 921 FOR_EACH_EDGE (in_edge, in_ei, BASIC_BLOCK (BB_TO_BLOCK (bb))->preds)
7a31a7bd 922 {
aae97b21 923 if (in_edge->src == ENTRY_BLOCK_PTR)
924 continue;
7a31a7bd 925
aae97b21 926 pred_bb = BLOCK_TO_BB (in_edge->src->index);
927 sbitmap_a_and_b (dom[bb], dom[bb], dom[pred_bb]);
928 sbitmap_a_or_b (ancestor_edges[bb],
929 ancestor_edges[bb], ancestor_edges[pred_bb]);
7a31a7bd 930
aae97b21 931 SET_BIT (ancestor_edges[bb], EDGE_TO_BIT (in_edge));
79cafa9e 932
aae97b21 933 sbitmap_a_or_b (pot_split[bb], pot_split[bb], pot_split[pred_bb]);
7a31a7bd 934
aae97b21 935 nr_out_edges = 0;
936 nr_rgn_out_edges = 0;
7a31a7bd 937
aae97b21 938 FOR_EACH_EDGE (out_edge, out_ei, in_edge->src->succs)
7a31a7bd 939 {
940 ++nr_out_edges;
aae97b21 941
7a31a7bd 942 /* The successor doesn't belong in the region? */
aae97b21 943 if (out_edge->dest != EXIT_BLOCK_PTR
944 && CONTAINING_RGN (out_edge->dest->index)
945 != CONTAINING_RGN (BB_TO_BLOCK (bb)))
7a31a7bd 946 ++nr_rgn_out_edges;
7a31a7bd 947
aae97b21 948 SET_BIT (pot_split[bb], EDGE_TO_BIT (out_edge));
7a31a7bd 949 }
950
951 /* Now nr_rgn_out_edges is the number of region-exit edges from
952 pred, and nr_out_edges will be the number of pred out edges
953 not leaving the region. */
954 nr_out_edges -= nr_rgn_out_edges;
955 if (nr_rgn_out_edges > 0)
aae97b21 956 prob[bb] += 0.9 * prob[pred_bb] / nr_out_edges;
7a31a7bd 957 else
aae97b21 958 prob[bb] += prob[pred_bb] / nr_out_edges;
7a31a7bd 959 }
7a31a7bd 960
79cafa9e 961 SET_BIT (dom[bb], bb);
962 sbitmap_difference (pot_split[bb], pot_split[bb], ancestor_edges[bb]);
7a31a7bd 963
964 if (sched_verbose >= 2)
965 fprintf (sched_dump, ";; bb_prob(%d, %d) = %3d\n", bb, BB_TO_BLOCK (bb),
966 (int) (100.0 * prob[bb]));
967}
968
969/* Functions for target info. */
970
971/* Compute in BL the list of split-edges of bb_src relatively to bb_trg.
972 Note that bb_trg dominates bb_src. */
973
974static void
60b8c5b3 975split_edges (int bb_src, int bb_trg, edgelst *bl)
7a31a7bd 976{
f0af5a88 977 sbitmap src = sbitmap_alloc (pot_split[bb_src]->n_bits);
79cafa9e 978 sbitmap_copy (src, pot_split[bb_src]);
979
980 sbitmap_difference (src, src, pot_split[bb_trg]);
aae97b21 981 extract_edgelst (src, bl);
79cafa9e 982 sbitmap_free (src);
7a31a7bd 983}
984
985/* Find the valid candidate-source-blocks for the target block TRG, compute
986 their probability, and check if they are speculative or not.
987 For speculative sources, compute their update-blocks and split-blocks. */
988
989static void
60b8c5b3 990compute_trg_info (int trg)
7a31a7bd 991{
19cb6b50 992 candidate *sp;
7a31a7bd 993 edgelst el;
aae97b21 994 int i, j, k, update_idx;
995 basic_block block;
d3129ae7 996 sbitmap visited;
aae97b21 997 edge_iterator ei;
998 edge e;
7a31a7bd 999
1000 /* Define some of the fields for the target bb as well. */
1001 sp = candidate_table + trg;
1002 sp->is_valid = 1;
1003 sp->is_speculative = 0;
1004 sp->src_prob = 100;
1005
d3129ae7 1006 visited = sbitmap_alloc (last_basic_block - (INVALID_BLOCK + 1));
1007
7a31a7bd 1008 for (i = trg + 1; i < current_nr_blocks; i++)
1009 {
1010 sp = candidate_table + i;
1011
1012 sp->is_valid = IS_DOMINATED (i, trg);
1013 if (sp->is_valid)
1014 {
1015 sp->src_prob = GET_SRC_PROB (i, trg);
1016 sp->is_valid = (sp->src_prob >= MIN_PROBABILITY);
1017 }
1018
1019 if (sp->is_valid)
1020 {
1021 split_edges (i, trg, &el);
1022 sp->is_speculative = (el.nr_members) ? 1 : 0;
1023 if (sp->is_speculative && !flag_schedule_speculative)
1024 sp->is_valid = 0;
1025 }
1026
1027 if (sp->is_valid)
1028 {
7a31a7bd 1029 /* Compute split blocks and store them in bblst_table.
1030 The TO block of every split edge is a split block. */
1031 sp->split_bbs.first_member = &bblst_table[bblst_last];
1032 sp->split_bbs.nr_members = el.nr_members;
1033 for (j = 0; j < el.nr_members; bblst_last++, j++)
aae97b21 1034 bblst_table[bblst_last] = el.first_member[j]->dest;
7a31a7bd 1035 sp->update_bbs.first_member = &bblst_table[bblst_last];
1036
1037 /* Compute update blocks and store them in bblst_table.
1038 For every split edge, look at the FROM block, and check
1039 all out edges. For each out edge that is not a split edge,
1040 add the TO block to the update block list. This list can end
1041 up with a lot of duplicates. We need to weed them out to avoid
1042 overrunning the end of the bblst_table. */
7a31a7bd 1043
1044 update_idx = 0;
d3129ae7 1045 sbitmap_zero (visited);
7a31a7bd 1046 for (j = 0; j < el.nr_members; j++)
1047 {
aae97b21 1048 block = el.first_member[j]->src;
1049 FOR_EACH_EDGE (e, ei, block->succs)
7a31a7bd 1050 {
d3129ae7 1051 if (!TEST_BIT (visited,
1052 e->dest->index - (INVALID_BLOCK + 1)))
7a31a7bd 1053 {
1054 for (k = 0; k < el.nr_members; k++)
aae97b21 1055 if (e == el.first_member[k])
7a31a7bd 1056 break;
1057
1058 if (k >= el.nr_members)
1059 {
aae97b21 1060 bblst_table[bblst_last++] = e->dest;
d3129ae7 1061 SET_BIT (visited,
1062 e->dest->index - (INVALID_BLOCK + 1));
7a31a7bd 1063 update_idx++;
1064 }
1065 }
7a31a7bd 1066 }
7a31a7bd 1067 }
1068 sp->update_bbs.nr_members = update_idx;
1069
1070 /* Make sure we didn't overrun the end of bblst_table. */
04e579b6 1071 gcc_assert (bblst_last <= bblst_size);
7a31a7bd 1072 }
1073 else
1074 {
1075 sp->split_bbs.nr_members = sp->update_bbs.nr_members = 0;
1076
1077 sp->is_speculative = 0;
1078 sp->src_prob = 0;
1079 }
1080 }
d3129ae7 1081
1082 sbitmap_free (visited);
7a31a7bd 1083}
1084
1085/* Print candidates info, for debugging purposes. Callable from debugger. */
1086
1087void
60b8c5b3 1088debug_candidate (int i)
7a31a7bd 1089{
1090 if (!candidate_table[i].is_valid)
1091 return;
1092
1093 if (candidate_table[i].is_speculative)
1094 {
1095 int j;
1096 fprintf (sched_dump, "src b %d bb %d speculative \n", BB_TO_BLOCK (i), i);
1097
1098 fprintf (sched_dump, "split path: ");
1099 for (j = 0; j < candidate_table[i].split_bbs.nr_members; j++)
1100 {
aae97b21 1101 int b = candidate_table[i].split_bbs.first_member[j]->index;
7a31a7bd 1102
1103 fprintf (sched_dump, " %d ", b);
1104 }
1105 fprintf (sched_dump, "\n");
1106
1107 fprintf (sched_dump, "update path: ");
1108 for (j = 0; j < candidate_table[i].update_bbs.nr_members; j++)
1109 {
aae97b21 1110 int b = candidate_table[i].update_bbs.first_member[j]->index;
7a31a7bd 1111
1112 fprintf (sched_dump, " %d ", b);
1113 }
1114 fprintf (sched_dump, "\n");
1115 }
1116 else
1117 {
1118 fprintf (sched_dump, " src %d equivalent\n", BB_TO_BLOCK (i));
1119 }
1120}
1121
1122/* Print candidates info, for debugging purposes. Callable from debugger. */
1123
1124void
60b8c5b3 1125debug_candidates (int trg)
7a31a7bd 1126{
1127 int i;
1128
1129 fprintf (sched_dump, "----------- candidate table: target: b=%d bb=%d ---\n",
1130 BB_TO_BLOCK (trg), trg);
1131 for (i = trg + 1; i < current_nr_blocks; i++)
1132 debug_candidate (i);
1133}
1134
de132707 1135/* Functions for speculative scheduling. */
7a31a7bd 1136
1137/* Return 0 if x is a set of a register alive in the beginning of one
1138 of the split-blocks of src, otherwise return 1. */
1139
1140static int
60b8c5b3 1141check_live_1 (int src, rtx x)
7a31a7bd 1142{
19cb6b50 1143 int i;
1144 int regno;
1145 rtx reg = SET_DEST (x);
7a31a7bd 1146
1147 if (reg == 0)
1148 return 1;
1149
476d094d 1150 while (GET_CODE (reg) == SUBREG
1151 || GET_CODE (reg) == ZERO_EXTRACT
7a31a7bd 1152 || GET_CODE (reg) == STRICT_LOW_PART)
1153 reg = XEXP (reg, 0);
1154
4b303227 1155 if (GET_CODE (reg) == PARALLEL)
7a31a7bd 1156 {
19cb6b50 1157 int i;
216b2683 1158
7a31a7bd 1159 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
4b303227 1160 if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
1161 if (check_live_1 (src, XEXP (XVECEXP (reg, 0, i), 0)))
216b2683 1162 return 1;
216b2683 1163
7a31a7bd 1164 return 0;
1165 }
1166
8ad4c111 1167 if (!REG_P (reg))
7a31a7bd 1168 return 1;
1169
1170 regno = REGNO (reg);
1171
1172 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
1173 {
1174 /* Global registers are assumed live. */
1175 return 0;
1176 }
1177 else
1178 {
1179 if (regno < FIRST_PSEUDO_REGISTER)
1180 {
1181 /* Check for hard registers. */
67d6c12b 1182 int j = hard_regno_nregs[regno][GET_MODE (reg)];
7a31a7bd 1183 while (--j >= 0)
1184 {
1185 for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
1186 {
aae97b21 1187 basic_block b = candidate_table[src].split_bbs.first_member[i];
7a31a7bd 1188
e0dde8f8 1189 if (REGNO_REG_SET_P (b->il.rtl->global_live_at_start,
1190 regno + j))
7a31a7bd 1191 {
1192 return 0;
1193 }
1194 }
1195 }
1196 }
1197 else
1198 {
f024691d 1199 /* Check for pseudo registers. */
7a31a7bd 1200 for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
1201 {
aae97b21 1202 basic_block b = candidate_table[src].split_bbs.first_member[i];
7a31a7bd 1203
e0dde8f8 1204 if (REGNO_REG_SET_P (b->il.rtl->global_live_at_start, regno))
7a31a7bd 1205 {
1206 return 0;
1207 }
1208 }
1209 }
1210 }
1211
1212 return 1;
1213}
1214
1215/* If x is a set of a register R, mark that R is alive in the beginning
1216 of every update-block of src. */
1217
1218static void
60b8c5b3 1219update_live_1 (int src, rtx x)
7a31a7bd 1220{
19cb6b50 1221 int i;
1222 int regno;
1223 rtx reg = SET_DEST (x);
7a31a7bd 1224
1225 if (reg == 0)
1226 return;
1227
476d094d 1228 while (GET_CODE (reg) == SUBREG
1229 || GET_CODE (reg) == ZERO_EXTRACT
7a31a7bd 1230 || GET_CODE (reg) == STRICT_LOW_PART)
1231 reg = XEXP (reg, 0);
1232
4b303227 1233 if (GET_CODE (reg) == PARALLEL)
7a31a7bd 1234 {
19cb6b50 1235 int i;
216b2683 1236
7a31a7bd 1237 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
4b303227 1238 if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
1239 update_live_1 (src, XEXP (XVECEXP (reg, 0, i), 0));
216b2683 1240
7a31a7bd 1241 return;
1242 }
1243
8ad4c111 1244 if (!REG_P (reg))
7a31a7bd 1245 return;
1246
1247 /* Global registers are always live, so the code below does not apply
1248 to them. */
1249
1250 regno = REGNO (reg);
1251
1252 if (regno >= FIRST_PSEUDO_REGISTER || !global_regs[regno])
1253 {
1254 if (regno < FIRST_PSEUDO_REGISTER)
1255 {
67d6c12b 1256 int j = hard_regno_nregs[regno][GET_MODE (reg)];
7a31a7bd 1257 while (--j >= 0)
1258 {
1259 for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
1260 {
aae97b21 1261 basic_block b = candidate_table[src].update_bbs.first_member[i];
7a31a7bd 1262
e0dde8f8 1263 SET_REGNO_REG_SET (b->il.rtl->global_live_at_start,
1264 regno + j);
7a31a7bd 1265 }
1266 }
1267 }
1268 else
1269 {
1270 for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
1271 {
aae97b21 1272 basic_block b = candidate_table[src].update_bbs.first_member[i];
7a31a7bd 1273
e0dde8f8 1274 SET_REGNO_REG_SET (b->il.rtl->global_live_at_start, regno);
7a31a7bd 1275 }
1276 }
1277 }
1278}
1279
1280/* Return 1 if insn can be speculatively moved from block src to trg,
1281 otherwise return 0. Called before first insertion of insn to
1282 ready-list or before the scheduling. */
1283
1284static int
60b8c5b3 1285check_live (rtx insn, int src)
7a31a7bd 1286{
1287 /* Find the registers set by instruction. */
1288 if (GET_CODE (PATTERN (insn)) == SET
1289 || GET_CODE (PATTERN (insn)) == CLOBBER)
1290 return check_live_1 (src, PATTERN (insn));
1291 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1292 {
1293 int j;
1294 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
1295 if ((GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
1296 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
1297 && !check_live_1 (src, XVECEXP (PATTERN (insn), 0, j)))
1298 return 0;
1299
1300 return 1;
1301 }
1302
1303 return 1;
1304}
1305
1306/* Update the live registers info after insn was moved speculatively from
1307 block src to trg. */
1308
1309static void
60b8c5b3 1310update_live (rtx insn, int src)
7a31a7bd 1311{
1312 /* Find the registers set by instruction. */
1313 if (GET_CODE (PATTERN (insn)) == SET
1314 || GET_CODE (PATTERN (insn)) == CLOBBER)
1315 update_live_1 (src, PATTERN (insn));
1316 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1317 {
1318 int j;
1319 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
1320 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
1321 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
1322 update_live_1 (src, XVECEXP (PATTERN (insn), 0, j));
1323 }
1324}
1325
a8b24921 1326/* Nonzero if block bb_to is equal to, or reachable from block bb_from. */
7a31a7bd 1327#define IS_REACHABLE(bb_from, bb_to) \
40734805 1328 (bb_from == bb_to \
7a31a7bd 1329 || IS_RGN_ENTRY (bb_from) \
40734805 1330 || (TEST_BIT (ancestor_edges[bb_to], \
ea091dfd 1331 EDGE_TO_BIT (single_pred_edge (BASIC_BLOCK (BB_TO_BLOCK (bb_from)))))))
7a31a7bd 1332
7a31a7bd 1333/* Turns on the fed_by_spec_load flag for insns fed by load_insn. */
1334
1335static void
60b8c5b3 1336set_spec_fed (rtx load_insn)
7a31a7bd 1337{
1338 rtx link;
1339
1340 for (link = INSN_DEPEND (load_insn); link; link = XEXP (link, 1))
1341 if (GET_MODE (link) == VOIDmode)
1342 FED_BY_SPEC_LOAD (XEXP (link, 0)) = 1;
1343} /* set_spec_fed */
1344
1345/* On the path from the insn to load_insn_bb, find a conditional
1346branch depending on insn, that guards the speculative load. */
1347
1348static int
60b8c5b3 1349find_conditional_protection (rtx insn, int load_insn_bb)
7a31a7bd 1350{
1351 rtx link;
1352
1353 /* Iterate through DEF-USE forward dependences. */
1354 for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
1355 {
1356 rtx next = XEXP (link, 0);
1357 if ((CONTAINING_RGN (BLOCK_NUM (next)) ==
1358 CONTAINING_RGN (BB_TO_BLOCK (load_insn_bb)))
1359 && IS_REACHABLE (INSN_BB (next), load_insn_bb)
1360 && load_insn_bb != INSN_BB (next)
1361 && GET_MODE (link) == VOIDmode
6d7dc5b9 1362 && (JUMP_P (next)
7a31a7bd 1363 || find_conditional_protection (next, load_insn_bb)))
1364 return 1;
1365 }
1366 return 0;
1367} /* find_conditional_protection */
1368
1369/* Returns 1 if the same insn1 that participates in the computation
1370 of load_insn's address is feeding a conditional branch that is
1371 guarding on load_insn. This is true if we find a the two DEF-USE
1372 chains:
1373 insn1 -> ... -> conditional-branch
1374 insn1 -> ... -> load_insn,
1375 and if a flow path exist:
1376 insn1 -> ... -> conditional-branch -> ... -> load_insn,
1377 and if insn1 is on the path
1378 region-entry -> ... -> bb_trg -> ... load_insn.
1379
1380 Locate insn1 by climbing on LOG_LINKS from load_insn.
1381 Locate the branch by following INSN_DEPEND from insn1. */
1382
1383static int
60b8c5b3 1384is_conditionally_protected (rtx load_insn, int bb_src, int bb_trg)
7a31a7bd 1385{
1386 rtx link;
1387
1388 for (link = LOG_LINKS (load_insn); link; link = XEXP (link, 1))
1389 {
1390 rtx insn1 = XEXP (link, 0);
1391
1392 /* Must be a DEF-USE dependence upon non-branch. */
1393 if (GET_MODE (link) != VOIDmode
6d7dc5b9 1394 || JUMP_P (insn1))
7a31a7bd 1395 continue;
1396
1397 /* Must exist a path: region-entry -> ... -> bb_trg -> ... load_insn. */
1398 if (INSN_BB (insn1) == bb_src
1399 || (CONTAINING_RGN (BLOCK_NUM (insn1))
1400 != CONTAINING_RGN (BB_TO_BLOCK (bb_src)))
1401 || (!IS_REACHABLE (bb_trg, INSN_BB (insn1))
1402 && !IS_REACHABLE (INSN_BB (insn1), bb_trg)))
1403 continue;
1404
1405 /* Now search for the conditional-branch. */
1406 if (find_conditional_protection (insn1, bb_src))
1407 return 1;
1408
1409 /* Recursive step: search another insn1, "above" current insn1. */
1410 return is_conditionally_protected (insn1, bb_src, bb_trg);
1411 }
1412
1413 /* The chain does not exist. */
1414 return 0;
1415} /* is_conditionally_protected */
1416
1417/* Returns 1 if a clue for "similar load" 'insn2' is found, and hence
1418 load_insn can move speculatively from bb_src to bb_trg. All the
1419 following must hold:
1420
1421 (1) both loads have 1 base register (PFREE_CANDIDATEs).
1422 (2) load_insn and load1 have a def-use dependence upon
1423 the same insn 'insn1'.
1424 (3) either load2 is in bb_trg, or:
1425 - there's only one split-block, and
1426 - load1 is on the escape path, and
1427
1428 From all these we can conclude that the two loads access memory
1429 addresses that differ at most by a constant, and hence if moving
1430 load_insn would cause an exception, it would have been caused by
1431 load2 anyhow. */
1432
1433static int
60b8c5b3 1434is_pfree (rtx load_insn, int bb_src, int bb_trg)
7a31a7bd 1435{
1436 rtx back_link;
19cb6b50 1437 candidate *candp = candidate_table + bb_src;
7a31a7bd 1438
1439 if (candp->split_bbs.nr_members != 1)
1440 /* Must have exactly one escape block. */
1441 return 0;
1442
1443 for (back_link = LOG_LINKS (load_insn);
1444 back_link; back_link = XEXP (back_link, 1))
1445 {
1446 rtx insn1 = XEXP (back_link, 0);
1447
1448 if (GET_MODE (back_link) == VOIDmode)
1449 {
1450 /* Found a DEF-USE dependence (insn1, load_insn). */
1451 rtx fore_link;
1452
1453 for (fore_link = INSN_DEPEND (insn1);
1454 fore_link; fore_link = XEXP (fore_link, 1))
1455 {
1456 rtx insn2 = XEXP (fore_link, 0);
1457 if (GET_MODE (fore_link) == VOIDmode)
1458 {
1459 /* Found a DEF-USE dependence (insn1, insn2). */
1460 if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
1461 /* insn2 not guaranteed to be a 1 base reg load. */
1462 continue;
1463
1464 if (INSN_BB (insn2) == bb_trg)
1465 /* insn2 is the similar load, in the target block. */
1466 return 1;
1467
aae97b21 1468 if (*(candp->split_bbs.first_member) == BLOCK_FOR_INSN (insn2))
7a31a7bd 1469 /* insn2 is a similar load, in a split-block. */
1470 return 1;
1471 }
1472 }
1473 }
1474 }
1475
1476 /* Couldn't find a similar load. */
1477 return 0;
1478} /* is_pfree */
1479
7a31a7bd 1480/* Return 1 if load_insn is prisky (i.e. if load_insn is fed by
1481 a load moved speculatively, or if load_insn is protected by
1482 a compare on load_insn's address). */
1483
1484static int
60b8c5b3 1485is_prisky (rtx load_insn, int bb_src, int bb_trg)
7a31a7bd 1486{
1487 if (FED_BY_SPEC_LOAD (load_insn))
1488 return 1;
1489
1490 if (LOG_LINKS (load_insn) == NULL)
1491 /* Dependence may 'hide' out of the region. */
1492 return 1;
1493
1494 if (is_conditionally_protected (load_insn, bb_src, bb_trg))
1495 return 1;
1496
1497 return 0;
1498}
1499
1500/* Insn is a candidate to be moved speculatively from bb_src to bb_trg.
1501 Return 1 if insn is exception-free (and the motion is valid)
1502 and 0 otherwise. */
1503
1504static int
60b8c5b3 1505is_exception_free (rtx insn, int bb_src, int bb_trg)
7a31a7bd 1506{
1507 int insn_class = haifa_classify_insn (insn);
1508
1509 /* Handle non-load insns. */
1510 switch (insn_class)
1511 {
1512 case TRAP_FREE:
1513 return 1;
1514 case TRAP_RISKY:
1515 return 0;
1516 default:;
1517 }
1518
1519 /* Handle loads. */
1520 if (!flag_schedule_speculative_load)
1521 return 0;
1522 IS_LOAD_INSN (insn) = 1;
1523 switch (insn_class)
1524 {
1525 case IFREE:
1526 return (1);
1527 case IRISKY:
1528 return 0;
1529 case PFREE_CANDIDATE:
1530 if (is_pfree (insn, bb_src, bb_trg))
1531 return 1;
1532 /* Don't 'break' here: PFREE-candidate is also PRISKY-candidate. */
1533 case PRISKY_CANDIDATE:
1534 if (!flag_schedule_speculative_load_dangerous
1535 || is_prisky (insn, bb_src, bb_trg))
1536 return 0;
1537 break;
1538 default:;
1539 }
1540
1541 return flag_schedule_speculative_load_dangerous;
1542}
1543\f
1544/* The number of insns from the current block scheduled so far. */
1545static int sched_target_n_insns;
1546/* The number of insns from the current block to be scheduled in total. */
1547static int target_n_insns;
1548/* The number of insns from the entire region scheduled so far. */
1549static int sched_n_insns;
2295df67 1550/* Nonzero if the last scheduled insn was a jump. */
1551static int last_was_jump;
7a31a7bd 1552
1553/* Implementations of the sched_info functions for region scheduling. */
60b8c5b3 1554static void init_ready_list (struct ready_list *);
1555static int can_schedule_ready_p (rtx);
1556static int new_ready (rtx);
1557static int schedule_more_p (void);
1558static const char *rgn_print_insn (rtx, int);
1559static int rgn_rank (rtx, rtx);
1560static int contributes_to_priority (rtx, rtx);
31c5c470 1561static void compute_jump_reg_dependencies (rtx, regset, regset, regset);
7a31a7bd 1562
1563/* Return nonzero if there are more insns that should be scheduled. */
1564
1565static int
60b8c5b3 1566schedule_more_p (void)
7a31a7bd 1567{
2295df67 1568 return ! last_was_jump && sched_target_n_insns < target_n_insns;
7a31a7bd 1569}
1570
1571/* Add all insns that are initially ready to the ready list READY. Called
1572 once before scheduling a set of insns. */
1573
1574static void
60b8c5b3 1575init_ready_list (struct ready_list *ready)
7a31a7bd 1576{
1577 rtx prev_head = current_sched_info->prev_head;
1578 rtx next_tail = current_sched_info->next_tail;
1579 int bb_src;
1580 rtx insn;
1581
1582 target_n_insns = 0;
1583 sched_target_n_insns = 0;
1584 sched_n_insns = 0;
2295df67 1585 last_was_jump = 0;
7a31a7bd 1586
1587 /* Print debugging information. */
1588 if (sched_verbose >= 5)
1589 debug_dependencies ();
1590
1591 /* Prepare current target block info. */
1592 if (current_nr_blocks > 1)
1593 {
f0af5a88 1594 candidate_table = xmalloc (current_nr_blocks * sizeof (candidate));
7a31a7bd 1595
1596 bblst_last = 0;
1597 /* bblst_table holds split blocks and update blocks for each block after
1598 the current one in the region. split blocks and update blocks are
1599 the TO blocks of region edges, so there can be at most rgn_nr_edges
1600 of them. */
1601 bblst_size = (current_nr_blocks - target_bb) * rgn_nr_edges;
aae97b21 1602 bblst_table = xmalloc (bblst_size * sizeof (basic_block));
7a31a7bd 1603
aae97b21 1604 edgelst_last = 0;
1605 edgelst_table = xmalloc (rgn_nr_edges * sizeof (edge));
7a31a7bd 1606
1607 compute_trg_info (target_bb);
1608 }
1609
1610 /* Initialize ready list with all 'ready' insns in target block.
1611 Count number of insns in the target block being scheduled. */
1612 for (insn = NEXT_INSN (prev_head); insn != next_tail; insn = NEXT_INSN (insn))
1613 {
e26579fc 1614 if (INSN_DEP_COUNT (insn) == 0)
09542196 1615 {
1616 ready_add (ready, insn);
1617
1618 if (targetm.sched.adjust_priority)
1619 INSN_PRIORITY (insn) =
883b2e73 1620 targetm.sched.adjust_priority (insn, INSN_PRIORITY (insn));
09542196 1621 }
e26579fc 1622 target_n_insns++;
7a31a7bd 1623 }
1624
1625 /* Add to ready list all 'ready' insns in valid source blocks.
1626 For speculative insns, check-live, exception-free, and
1627 issue-delay. */
1628 for (bb_src = target_bb + 1; bb_src < current_nr_blocks; bb_src++)
1629 if (IS_VALID (bb_src))
1630 {
1631 rtx src_head;
1632 rtx src_next_tail;
1633 rtx tail, head;
1634
1635 get_block_head_tail (BB_TO_BLOCK (bb_src), &head, &tail);
1636 src_next_tail = NEXT_INSN (tail);
1637 src_head = head;
1638
1639 for (insn = src_head; insn != src_next_tail; insn = NEXT_INSN (insn))
1640 {
1641 if (! INSN_P (insn))
1642 continue;
1643
1644 if (!CANT_MOVE (insn)
1645 && (!IS_SPECULATIVE_INSN (insn)
67900a4f 1646 || ((recog_memoized (insn) < 0
1647 || min_insn_conflict_delay (curr_state,
1648 insn, insn) <= 3)
7a31a7bd 1649 && check_live (insn, bb_src)
1650 && is_exception_free (insn, bb_src, target_bb))))
e26579fc 1651 if (INSN_DEP_COUNT (insn) == 0)
09542196 1652 {
1653 ready_add (ready, insn);
1654
1655 if (targetm.sched.adjust_priority)
1656 INSN_PRIORITY (insn) =
883b2e73 1657 targetm.sched.adjust_priority (insn, INSN_PRIORITY (insn));
09542196 1658 }
7a31a7bd 1659 }
1660 }
1661}
1662
1663/* Called after taking INSN from the ready list. Returns nonzero if this
1664 insn can be scheduled, nonzero if we should silently discard it. */
1665
1666static int
60b8c5b3 1667can_schedule_ready_p (rtx insn)
7a31a7bd 1668{
6d7dc5b9 1669 if (JUMP_P (insn))
2295df67 1670 last_was_jump = 1;
1671
7a31a7bd 1672 /* An interblock motion? */
1673 if (INSN_BB (insn) != target_bb)
1674 {
7a31a7bd 1675 basic_block b1;
1676
1677 if (IS_SPECULATIVE_INSN (insn))
1678 {
1679 if (!check_live (insn, INSN_BB (insn)))
1680 return 0;
1681 update_live (insn, INSN_BB (insn));
1682
1683 /* For speculative load, mark insns fed by it. */
1684 if (IS_LOAD_INSN (insn) || FED_BY_SPEC_LOAD (insn))
1685 set_spec_fed (insn);
1686
1687 nr_spec++;
1688 }
1689 nr_inter++;
1690
1e625a2e 1691 /* Update source block boundaries. */
e26579fc 1692 b1 = BLOCK_FOR_INSN (insn);
5496dbfc 1693 if (insn == BB_HEAD (b1) && insn == BB_END (b1))
7a31a7bd 1694 {
1695 /* We moved all the insns in the basic block.
1696 Emit a note after the last insn and update the
1697 begin/end boundaries to point to the note. */
1698 rtx note = emit_note_after (NOTE_INSN_DELETED, insn);
5496dbfc 1699 BB_HEAD (b1) = note;
1700 BB_END (b1) = note;
7a31a7bd 1701 }
5496dbfc 1702 else if (insn == BB_END (b1))
7a31a7bd 1703 {
1704 /* We took insns from the end of the basic block,
1705 so update the end of block boundary so that it
1706 points to the first insn we did not move. */
5496dbfc 1707 BB_END (b1) = PREV_INSN (insn);
7a31a7bd 1708 }
5496dbfc 1709 else if (insn == BB_HEAD (b1))
7a31a7bd 1710 {
1711 /* We took insns from the start of the basic block,
1712 so update the start of block boundary so that
1713 it points to the first insn we did not move. */
5496dbfc 1714 BB_HEAD (b1) = NEXT_INSN (insn);
7a31a7bd 1715 }
1716 }
1717 else
1718 {
1719 /* In block motion. */
1720 sched_target_n_insns++;
1721 }
1722 sched_n_insns++;
1723
1724 return 1;
1725}
1726
1727/* Called after INSN has all its dependencies resolved. Return nonzero
1728 if it should be moved to the ready list or the queue, or zero if we
1729 should silently discard it. */
1730static int
60b8c5b3 1731new_ready (rtx next)
7a31a7bd 1732{
1733 /* For speculative insns, before inserting to ready/queue,
1734 check live, exception-free, and issue-delay. */
1735 if (INSN_BB (next) != target_bb
1736 && (!IS_VALID (INSN_BB (next))
1737 || CANT_MOVE (next)
1738 || (IS_SPECULATIVE_INSN (next)
67900a4f 1739 && ((recog_memoized (next) >= 0
1740 && min_insn_conflict_delay (curr_state, next, next) > 3)
7a31a7bd 1741 || !check_live (next, INSN_BB (next))
1742 || !is_exception_free (next, INSN_BB (next), target_bb)))))
1743 return 0;
1744 return 1;
1745}
1746
1747/* Return a string that contains the insn uid and optionally anything else
1748 necessary to identify this insn in an output. It's valid to use a
1749 static buffer for this. The ALIGNED parameter should cause the string
1750 to be formatted so that multiple output lines will line up nicely. */
1751
1752static const char *
60b8c5b3 1753rgn_print_insn (rtx insn, int aligned)
7a31a7bd 1754{
1755 static char tmp[80];
1756
1757 if (aligned)
1758 sprintf (tmp, "b%3d: i%4d", INSN_BB (insn), INSN_UID (insn));
1759 else
1760 {
7a31a7bd 1761 if (current_nr_blocks > 1 && INSN_BB (insn) != target_bb)
cda0a5f5 1762 sprintf (tmp, "%d/b%d", INSN_UID (insn), INSN_BB (insn));
1763 else
1764 sprintf (tmp, "%d", INSN_UID (insn));
7a31a7bd 1765 }
1766 return tmp;
1767}
1768
1769/* Compare priority of two insns. Return a positive number if the second
1770 insn is to be preferred for scheduling, and a negative one if the first
1771 is to be preferred. Zero if they are equally good. */
1772
1773static int
60b8c5b3 1774rgn_rank (rtx insn1, rtx insn2)
7a31a7bd 1775{
1776 /* Some comparison make sense in interblock scheduling only. */
1777 if (INSN_BB (insn1) != INSN_BB (insn2))
1778 {
1779 int spec_val, prob_val;
1780
1781 /* Prefer an inblock motion on an interblock motion. */
1782 if ((INSN_BB (insn2) == target_bb) && (INSN_BB (insn1) != target_bb))
1783 return 1;
1784 if ((INSN_BB (insn1) == target_bb) && (INSN_BB (insn2) != target_bb))
1785 return -1;
1786
1787 /* Prefer a useful motion on a speculative one. */
1788 spec_val = IS_SPECULATIVE_INSN (insn1) - IS_SPECULATIVE_INSN (insn2);
1789 if (spec_val)
1790 return spec_val;
1791
1792 /* Prefer a more probable (speculative) insn. */
1793 prob_val = INSN_PROBABILITY (insn2) - INSN_PROBABILITY (insn1);
1794 if (prob_val)
1795 return prob_val;
1796 }
1797 return 0;
1798}
1799
d6141c0c 1800/* NEXT is an instruction that depends on INSN (a backward dependence);
1801 return nonzero if we should include this dependence in priority
1802 calculations. */
1803
1804static int
60b8c5b3 1805contributes_to_priority (rtx next, rtx insn)
d6141c0c 1806{
1807 return BLOCK_NUM (next) == BLOCK_NUM (insn);
1808}
1809
31c5c470 1810/* INSN is a JUMP_INSN, COND_SET is the set of registers that are
1811 conditionally set before INSN. Store the set of registers that
1812 must be considered as used by this jump in USED and that of
1813 registers that must be considered as set in SET. */
d6141c0c 1814
1815static void
60b8c5b3 1816compute_jump_reg_dependencies (rtx insn ATTRIBUTE_UNUSED,
31c5c470 1817 regset cond_exec ATTRIBUTE_UNUSED,
1818 regset used ATTRIBUTE_UNUSED,
60b8c5b3 1819 regset set ATTRIBUTE_UNUSED)
d6141c0c 1820{
1821 /* Nothing to do here, since we postprocess jumps in
1822 add_branch_dependences. */
1823}
1824
7a31a7bd 1825/* Used in schedule_insns to initialize current_sched_info for scheduling
1826 regions (or single basic blocks). */
1827
1828static struct sched_info region_sched_info =
1829{
1830 init_ready_list,
1831 can_schedule_ready_p,
1832 schedule_more_p,
1833 new_ready,
1834 rgn_rank,
1835 rgn_print_insn,
d6141c0c 1836 contributes_to_priority,
1837 compute_jump_reg_dependencies,
7a31a7bd 1838
1839 NULL, NULL,
1840 NULL, NULL,
09542196 1841 0, 0, 0
7a31a7bd 1842};
1843
cbf780cc 1844/* Determine if PAT sets a CLASS_LIKELY_SPILLED_P register. */
1845
1846static bool
60b8c5b3 1847sets_likely_spilled (rtx pat)
cbf780cc 1848{
1849 bool ret = false;
1850 note_stores (pat, sets_likely_spilled_1, &ret);
1851 return ret;
1852}
1853
1854static void
60b8c5b3 1855sets_likely_spilled_1 (rtx x, rtx pat, void *data)
cbf780cc 1856{
1857 bool *ret = (bool *) data;
1858
1859 if (GET_CODE (pat) == SET
1860 && REG_P (x)
1861 && REGNO (x) < FIRST_PSEUDO_REGISTER
1862 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (x))))
1863 *ret = true;
1864}
1865
7a31a7bd 1866/* Add dependences so that branches are scheduled to run last in their
1867 block. */
1868
1869static void
60b8c5b3 1870add_branch_dependences (rtx head, rtx tail)
7a31a7bd 1871{
1872 rtx insn, last;
1873
cbaab9a3 1874 /* For all branches, calls, uses, clobbers, cc0 setters, and instructions
1875 that can throw exceptions, force them to remain in order at the end of
1876 the block by adding dependencies and giving the last a high priority.
1877 There may be notes present, and prev_head may also be a note.
7a31a7bd 1878
1879 Branches must obviously remain at the end. Calls should remain at the
1880 end since moving them results in worse register allocation. Uses remain
cbf780cc 1881 at the end to ensure proper register allocation.
1882
40e55fbb 1883 cc0 setters remain at the end because they can't be moved away from
cbf780cc 1884 their cc0 user.
1885
1886 Insns setting CLASS_LIKELY_SPILLED_P registers (usually return values)
1887 are not moved before reload because we can wind up with register
1888 allocation failures. */
1889
7a31a7bd 1890 insn = tail;
1891 last = 0;
6d7dc5b9 1892 while (CALL_P (insn)
1893 || JUMP_P (insn)
1894 || (NONJUMP_INSN_P (insn)
7a31a7bd 1895 && (GET_CODE (PATTERN (insn)) == USE
1896 || GET_CODE (PATTERN (insn)) == CLOBBER
cbaab9a3 1897 || can_throw_internal (insn)
7a31a7bd 1898#ifdef HAVE_cc0
1899 || sets_cc0_p (PATTERN (insn))
1900#endif
cbf780cc 1901 || (!reload_completed
1902 && sets_likely_spilled (PATTERN (insn)))))
6d7dc5b9 1903 || NOTE_P (insn))
7a31a7bd 1904 {
6d7dc5b9 1905 if (!NOTE_P (insn))
7a31a7bd 1906 {
5deaeb50 1907 if (last != 0 && !find_insn_list (insn, LOG_LINKS (last)))
7a31a7bd 1908 {
1909 add_dependence (last, insn, REG_DEP_ANTI);
1910 INSN_REF_COUNT (insn)++;
1911 }
1912
1913 CANT_MOVE (insn) = 1;
1914
1915 last = insn;
7a31a7bd 1916 }
1917
1918 /* Don't overrun the bounds of the basic block. */
1919 if (insn == head)
1920 break;
1921
1922 insn = PREV_INSN (insn);
1923 }
1924
1925 /* Make sure these insns are scheduled last in their block. */
1926 insn = last;
1927 if (insn != 0)
1928 while (insn != head)
1929 {
1930 insn = prev_nonnote_insn (insn);
1931
1932 if (INSN_REF_COUNT (insn) != 0)
1933 continue;
1934
1935 add_dependence (last, insn, REG_DEP_ANTI);
1936 INSN_REF_COUNT (insn) = 1;
7a31a7bd 1937 }
1938}
1939
1940/* Data structures for the computation of data dependences in a regions. We
1941 keep one `deps' structure for every basic block. Before analyzing the
1942 data dependences for a bb, its variables are initialized as a function of
1943 the variables of its predecessors. When the analysis for a bb completes,
1944 we save the contents to the corresponding bb_deps[bb] variable. */
1945
1946static struct deps *bb_deps;
1947
5deaeb50 1948/* Duplicate the INSN_LIST elements of COPY and prepend them to OLD. */
1949
1950static rtx
60b8c5b3 1951concat_INSN_LIST (rtx copy, rtx old)
5deaeb50 1952{
1953 rtx new = old;
1954 for (; copy ; copy = XEXP (copy, 1))
1955 new = alloc_INSN_LIST (XEXP (copy, 0), new);
1956 return new;
1957}
1958
1959static void
60b8c5b3 1960concat_insn_mem_list (rtx copy_insns, rtx copy_mems, rtx *old_insns_p,
1961 rtx *old_mems_p)
5deaeb50 1962{
1963 rtx new_insns = *old_insns_p;
1964 rtx new_mems = *old_mems_p;
1965
1966 while (copy_insns)
1967 {
1968 new_insns = alloc_INSN_LIST (XEXP (copy_insns, 0), new_insns);
1969 new_mems = alloc_EXPR_LIST (VOIDmode, XEXP (copy_mems, 0), new_mems);
1970 copy_insns = XEXP (copy_insns, 1);
1971 copy_mems = XEXP (copy_mems, 1);
1972 }
1973
1974 *old_insns_p = new_insns;
1975 *old_mems_p = new_mems;
1976}
1977
7a31a7bd 1978/* After computing the dependencies for block BB, propagate the dependencies
749c6f58 1979 found in TMP_DEPS to the successors of the block. */
7a31a7bd 1980static void
60b8c5b3 1981propagate_deps (int bb, struct deps *pred_deps)
7a31a7bd 1982{
aae97b21 1983 basic_block block = BASIC_BLOCK (BB_TO_BLOCK (bb));
1984 edge_iterator ei;
1985 edge e;
7a31a7bd 1986
1987 /* bb's structures are inherited by its successors. */
aae97b21 1988 FOR_EACH_EDGE (e, ei, block->succs)
1989 {
1990 struct deps *succ_deps;
4f917ffe 1991 unsigned reg;
8c97cf13 1992 reg_set_iterator rsi;
7a31a7bd 1993
aae97b21 1994 /* Only bbs "below" bb, in the same region, are interesting. */
1995 if (e->dest == EXIT_BLOCK_PTR
1996 || CONTAINING_RGN (block->index) != CONTAINING_RGN (e->dest->index)
1997 || BLOCK_TO_BB (e->dest->index) <= bb)
1998 continue;
5deaeb50 1999
aae97b21 2000 succ_deps = bb_deps + BLOCK_TO_BB (e->dest->index);
5deaeb50 2001
aae97b21 2002 /* The reg_last lists are inherited by successor. */
8c97cf13 2003 EXECUTE_IF_SET_IN_REG_SET (&pred_deps->reg_last_in_use, 0, reg, rsi)
aae97b21 2004 {
2005 struct deps_reg *pred_rl = &pred_deps->reg_last[reg];
2006 struct deps_reg *succ_rl = &succ_deps->reg_last[reg];
2007
2008 succ_rl->uses = concat_INSN_LIST (pred_rl->uses, succ_rl->uses);
2009 succ_rl->sets = concat_INSN_LIST (pred_rl->sets, succ_rl->sets);
2010 succ_rl->clobbers = concat_INSN_LIST (pred_rl->clobbers,
2011 succ_rl->clobbers);
2012 succ_rl->uses_length += pred_rl->uses_length;
2013 succ_rl->clobbers_length += pred_rl->clobbers_length;
8c97cf13 2014 }
aae97b21 2015 IOR_REG_SET (&succ_deps->reg_last_in_use, &pred_deps->reg_last_in_use);
2016
2017 /* Mem read/write lists are inherited by successor. */
2018 concat_insn_mem_list (pred_deps->pending_read_insns,
2019 pred_deps->pending_read_mems,
2020 &succ_deps->pending_read_insns,
2021 &succ_deps->pending_read_mems);
2022 concat_insn_mem_list (pred_deps->pending_write_insns,
2023 pred_deps->pending_write_mems,
2024 &succ_deps->pending_write_insns,
2025 &succ_deps->pending_write_mems);
2026
2027 succ_deps->last_pending_memory_flush
2028 = concat_INSN_LIST (pred_deps->last_pending_memory_flush,
2029 succ_deps->last_pending_memory_flush);
2030
2031 succ_deps->pending_lists_length += pred_deps->pending_lists_length;
2032 succ_deps->pending_flush_length += pred_deps->pending_flush_length;
2033
2034 /* last_function_call is inherited by successor. */
2035 succ_deps->last_function_call
2036 = concat_INSN_LIST (pred_deps->last_function_call,
2037 succ_deps->last_function_call);
2038
2039 /* sched_before_next_call is inherited by successor. */
2040 succ_deps->sched_before_next_call
2041 = concat_INSN_LIST (pred_deps->sched_before_next_call,
2042 succ_deps->sched_before_next_call);
2043 }
7a31a7bd 2044
5deaeb50 2045 /* These lists should point to the right place, for correct
2046 freeing later. */
2047 bb_deps[bb].pending_read_insns = pred_deps->pending_read_insns;
2048 bb_deps[bb].pending_read_mems = pred_deps->pending_read_mems;
2049 bb_deps[bb].pending_write_insns = pred_deps->pending_write_insns;
2050 bb_deps[bb].pending_write_mems = pred_deps->pending_write_mems;
2051
2052 /* Can't allow these to be freed twice. */
2053 pred_deps->pending_read_insns = 0;
2054 pred_deps->pending_read_mems = 0;
2055 pred_deps->pending_write_insns = 0;
2056 pred_deps->pending_write_mems = 0;
7a31a7bd 2057}
2058
2059/* Compute backward dependences inside bb. In a multiple blocks region:
2060 (1) a bb is analyzed after its predecessors, and (2) the lists in
2061 effect at the end of bb (after analyzing for bb) are inherited by
de132707 2062 bb's successors.
7a31a7bd 2063
2064 Specifically for reg-reg data dependences, the block insns are
2065 scanned by sched_analyze () top-to-bottom. Two lists are
749c6f58 2066 maintained by sched_analyze (): reg_last[].sets for register DEFs,
2067 and reg_last[].uses for register USEs.
7a31a7bd 2068
2069 When analysis is completed for bb, we update for its successors:
2070 ; - DEFS[succ] = Union (DEFS [succ], DEFS [bb])
2071 ; - USES[succ] = Union (USES [succ], DEFS [bb])
2072
2073 The mechanism for computing mem-mem data dependence is very
2074 similar, and the result is interblock dependences in the region. */
2075
2076static void
60b8c5b3 2077compute_block_backward_dependences (int bb)
7a31a7bd 2078{
2079 rtx head, tail;
7a31a7bd 2080 struct deps tmp_deps;
2081
2082 tmp_deps = bb_deps[bb];
2083
2084 /* Do the analysis for this block. */
2085 get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
2086 sched_analyze (&tmp_deps, head, tail);
2087 add_branch_dependences (head, tail);
2088
2089 if (current_nr_blocks > 1)
749c6f58 2090 propagate_deps (bb, &tmp_deps);
7a31a7bd 2091
2092 /* Free up the INSN_LISTs. */
2093 free_deps (&tmp_deps);
7a31a7bd 2094}
749c6f58 2095
7a31a7bd 2096/* Remove all INSN_LISTs and EXPR_LISTs from the pending lists and add
2097 them to the unused_*_list variables, so that they can be reused. */
2098
2099static void
60b8c5b3 2100free_pending_lists (void)
7a31a7bd 2101{
2102 int bb;
2103
2104 for (bb = 0; bb < current_nr_blocks; bb++)
2105 {
2106 free_INSN_LIST_list (&bb_deps[bb].pending_read_insns);
2107 free_INSN_LIST_list (&bb_deps[bb].pending_write_insns);
2108 free_EXPR_LIST_list (&bb_deps[bb].pending_read_mems);
2109 free_EXPR_LIST_list (&bb_deps[bb].pending_write_mems);
2110 }
2111}
2112\f
2113/* Print dependences for debugging, callable from debugger. */
2114
2115void
60b8c5b3 2116debug_dependencies (void)
7a31a7bd 2117{
2118 int bb;
2119
2120 fprintf (sched_dump, ";; --------------- forward dependences: ------------ \n");
2121 for (bb = 0; bb < current_nr_blocks; bb++)
2122 {
67900a4f 2123 rtx head, tail;
2124 rtx next_tail;
2125 rtx insn;
2126
2127 get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
2128 next_tail = NEXT_INSN (tail);
2129 fprintf (sched_dump, "\n;; --- Region Dependences --- b %d bb %d \n",
2130 BB_TO_BLOCK (bb), bb);
2131
2132 fprintf (sched_dump, ";; %7s%6s%6s%6s%6s%6s%14s\n",
2133 "insn", "code", "bb", "dep", "prio", "cost",
2134 "reservation");
2135 fprintf (sched_dump, ";; %7s%6s%6s%6s%6s%6s%14s\n",
2136 "----", "----", "--", "---", "----", "----",
2137 "-----------");
2138
2139 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
7a31a7bd 2140 {
67900a4f 2141 rtx link;
7a31a7bd 2142
67900a4f 2143 if (! INSN_P (insn))
7a31a7bd 2144 {
67900a4f 2145 int n;
2146 fprintf (sched_dump, ";; %6d ", INSN_UID (insn));
2147 if (NOTE_P (insn))
7a31a7bd 2148 {
67900a4f 2149 n = NOTE_LINE_NUMBER (insn);
2150 if (n < 0)
2151 fprintf (sched_dump, "%s\n", GET_NOTE_INSN_NAME (n));
2152 else
7a31a7bd 2153 {
67900a4f 2154 expanded_location xloc;
2155 NOTE_EXPANDED_LOCATION (xloc, insn);
2156 fprintf (sched_dump, "line %d, file %s\n",
2157 xloc.line, xloc.file);
7a31a7bd 2158 }
bea4bad2 2159 }
2160 else
67900a4f 2161 fprintf (sched_dump, " {%s}\n", GET_RTX_NAME (GET_CODE (insn)));
2162 continue;
7a31a7bd 2163 }
67900a4f 2164
2165 fprintf (sched_dump,
2166 ";; %s%5d%6d%6d%6d%6d%6d ",
2167 (SCHED_GROUP_P (insn) ? "+" : " "),
2168 INSN_UID (insn),
2169 INSN_CODE (insn),
2170 INSN_BB (insn),
2171 INSN_DEP_COUNT (insn),
2172 INSN_PRIORITY (insn),
2173 insn_cost (insn, 0, 0));
2174
2175 if (recog_memoized (insn) < 0)
2176 fprintf (sched_dump, "nothing");
2177 else
2178 print_reservation (sched_dump, insn);
2179
2180 fprintf (sched_dump, "\t: ");
2181 for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
2182 fprintf (sched_dump, "%d ", INSN_UID (XEXP (link, 0)));
2183 fprintf (sched_dump, "\n");
7a31a7bd 2184 }
2185 }
2186 fprintf (sched_dump, "\n");
2187}
2188\f
f045d41d 2189/* Returns true if all the basic blocks of the current region have
2190 NOTE_DISABLE_SCHED_OF_BLOCK which means not to schedule that region. */
2191static bool
2192sched_is_disabled_for_current_region_p (void)
2193{
f045d41d 2194 int bb;
2195
2196 for (bb = 0; bb < current_nr_blocks; bb++)
7562ed74 2197 if (!(BASIC_BLOCK (BB_TO_BLOCK (bb))->flags & BB_DISABLE_SCHEDULE))
2198 return false;
f045d41d 2199
2200 return true;
2201}
2202
7a31a7bd 2203/* Schedule a region. A region is either an inner loop, a loop-free
2204 subroutine, or a single basic block. Each bb in the region is
2205 scheduled after its flow predecessors. */
2206
2207static void
60b8c5b3 2208schedule_region (int rgn)
7a31a7bd 2209{
aae97b21 2210 basic_block block;
2211 edge_iterator ei;
2212 edge e;
7a31a7bd 2213 int bb;
2214 int rgn_n_insns = 0;
2215 int sched_rgn_n_insns = 0;
2216
2217 /* Set variables for the current region. */
2218 current_nr_blocks = RGN_NR_BLOCKS (rgn);
2219 current_blocks = RGN_BLOCKS (rgn);
2220
f045d41d 2221 /* Don't schedule region that is marked by
2222 NOTE_DISABLE_SCHED_OF_BLOCK. */
2223 if (sched_is_disabled_for_current_region_p ())
2224 return;
2225
7a31a7bd 2226 init_deps_global ();
2227
de132707 2228 /* Initializations for region data dependence analysis. */
f0af5a88 2229 bb_deps = xmalloc (sizeof (struct deps) * current_nr_blocks);
7a31a7bd 2230 for (bb = 0; bb < current_nr_blocks; bb++)
2231 init_deps (bb_deps + bb);
2232
2233 /* Compute LOG_LINKS. */
2234 for (bb = 0; bb < current_nr_blocks; bb++)
2235 compute_block_backward_dependences (bb);
2236
2237 /* Compute INSN_DEPEND. */
2238 for (bb = current_nr_blocks - 1; bb >= 0; bb--)
2239 {
2240 rtx head, tail;
2241 get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
2242
2243 compute_forward_dependences (head, tail);
58ada791 2244
2245 if (targetm.sched.dependencies_evaluation_hook)
2246 targetm.sched.dependencies_evaluation_hook (head, tail);
2247
7a31a7bd 2248 }
2249
2250 /* Set priorities. */
2251 for (bb = 0; bb < current_nr_blocks; bb++)
2295df67 2252 {
2253 rtx head, tail;
2254 get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
2255
2256 rgn_n_insns += set_priorities (head, tail);
2257 }
7a31a7bd 2258
2259 /* Compute interblock info: probabilities, split-edges, dominators, etc. */
2260 if (current_nr_blocks > 1)
2261 {
f0af5a88 2262 prob = xmalloc ((current_nr_blocks) * sizeof (float));
7a31a7bd 2263
79cafa9e 2264 dom = sbitmap_vector_alloc (current_nr_blocks, current_nr_blocks);
2265 sbitmap_vector_zero (dom, current_nr_blocks);
aae97b21 2266
2267 /* Use ->aux to implement EDGE_TO_BIT mapping. */
7a31a7bd 2268 rgn_nr_edges = 0;
aae97b21 2269 FOR_EACH_BB (block)
2270 {
2271 if (CONTAINING_RGN (block->index) != rgn)
2272 continue;
2273 FOR_EACH_EDGE (e, ei, block->succs)
2274 SET_EDGE_TO_BIT (e, rgn_nr_edges++);
2275 }
7a31a7bd 2276
aae97b21 2277 rgn_edges = xmalloc (rgn_nr_edges * sizeof (edge));
7a31a7bd 2278 rgn_nr_edges = 0;
aae97b21 2279 FOR_EACH_BB (block)
2280 {
2281 if (CONTAINING_RGN (block->index) != rgn)
2282 continue;
2283 FOR_EACH_EDGE (e, ei, block->succs)
2284 rgn_edges[rgn_nr_edges++] = e;
2285 }
7a31a7bd 2286
2287 /* Split edges. */
79cafa9e 2288 pot_split = sbitmap_vector_alloc (current_nr_blocks, rgn_nr_edges);
2289 sbitmap_vector_zero (pot_split, current_nr_blocks);
2290 ancestor_edges = sbitmap_vector_alloc (current_nr_blocks, rgn_nr_edges);
2291 sbitmap_vector_zero (ancestor_edges, current_nr_blocks);
7a31a7bd 2292
2293 /* Compute probabilities, dominators, split_edges. */
2294 for (bb = 0; bb < current_nr_blocks; bb++)
2295 compute_dom_prob_ps (bb);
2296 }
2297
2298 /* Now we can schedule all blocks. */
2299 for (bb = 0; bb < current_nr_blocks; bb++)
2300 {
2301 rtx head, tail;
2302 int b = BB_TO_BLOCK (bb);
2303
2304 get_block_head_tail (b, &head, &tail);
2305
2306 if (no_real_insns_p (head, tail))
2307 continue;
2308
2309 current_sched_info->prev_head = PREV_INSN (head);
2310 current_sched_info->next_tail = NEXT_INSN (tail);
2311
2312 if (write_symbols != NO_DEBUG)
2313 {
2295df67 2314 save_line_notes (b, head, tail);
2315 rm_line_notes (head, tail);
7a31a7bd 2316 }
2317
2318 /* rm_other_notes only removes notes which are _inside_ the
2319 block---that is, it won't remove notes before the first real insn
60b8c5b3 2320 or after the last real insn of the block. So if the first insn
7a31a7bd 2321 has a REG_SAVE_NOTE which would otherwise be emitted before the
2322 insn, it is redundant with the note before the start of the
9239aee6 2323 block, and so we have to take it out. */
7a31a7bd 2324 if (INSN_P (head))
2325 {
2326 rtx note;
2327
2328 for (note = REG_NOTES (head); note; note = XEXP (note, 1))
2329 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
8f123a5e 2330 remove_note (head, note);
7a31a7bd 2331 }
2332
2333 /* Remove remaining note insns from the block, save them in
2334 note_list. These notes are restored at the end of
2335 schedule_block (). */
2336 rm_other_notes (head, tail);
2337
2338 target_bb = bb;
2339
2340 current_sched_info->queue_must_finish_empty
2341 = current_nr_blocks > 1 && !flag_schedule_interblock;
2342
2343 schedule_block (b, rgn_n_insns);
2344 sched_rgn_n_insns += sched_n_insns;
2345
2346 /* Update target block boundaries. */
5496dbfc 2347 if (head == BB_HEAD (BASIC_BLOCK (b)))
2348 BB_HEAD (BASIC_BLOCK (b)) = current_sched_info->head;
2349 if (tail == BB_END (BASIC_BLOCK (b)))
2350 BB_END (BASIC_BLOCK (b)) = current_sched_info->tail;
7a31a7bd 2351
2352 /* Clean up. */
2353 if (current_nr_blocks > 1)
2354 {
2355 free (candidate_table);
2356 free (bblst_table);
aae97b21 2357 free (edgelst_table);
7a31a7bd 2358 }
2359 }
2360
2361 /* Sanity check: verify that all region insns were scheduled. */
04e579b6 2362 gcc_assert (sched_rgn_n_insns == rgn_n_insns);
7a31a7bd 2363
2364 /* Restore line notes. */
2365 if (write_symbols != NO_DEBUG)
2366 {
2367 for (bb = 0; bb < current_nr_blocks; bb++)
2295df67 2368 {
2369 rtx head, tail;
2370 get_block_head_tail (BB_TO_BLOCK (bb), &head, &tail);
61ff7bd5 2371 restore_line_notes (head, tail);
2295df67 2372 }
7a31a7bd 2373 }
2374
2375 /* Done with this region. */
2376 free_pending_lists ();
2377
2378 finish_deps_global ();
2379
2380 free (bb_deps);
2381
2382 if (current_nr_blocks > 1)
2383 {
aae97b21 2384 /* Cleanup ->aux used for EDGE_TO_BIT mapping. */
2385 FOR_EACH_BB (block)
2386 {
2387 if (CONTAINING_RGN (block->index) != rgn)
2388 continue;
2389 FOR_EACH_EDGE (e, ei, block->succs)
2390 e->aux = NULL;
2391 }
2392
7a31a7bd 2393 free (prob);
79cafa9e 2394 sbitmap_vector_free (dom);
2395 sbitmap_vector_free (pot_split);
2396 sbitmap_vector_free (ancestor_edges);
7a31a7bd 2397 free (rgn_edges);
7a31a7bd 2398 }
2399}
2400
2401/* Indexed by region, holds the number of death notes found in that region.
2402 Used for consistency checks. */
2403static int *deaths_in_region;
2404
2405/* Initialize data structures for region scheduling. */
2406
2407static void
60b8c5b3 2408init_regions (void)
7a31a7bd 2409{
2410 sbitmap blocks;
2411 int rgn;
2412
2413 nr_regions = 0;
f0af5a88 2414 rgn_table = xmalloc ((n_basic_blocks) * sizeof (region));
2415 rgn_bb_table = xmalloc ((n_basic_blocks) * sizeof (int));
2416 block_to_bb = xmalloc ((last_basic_block) * sizeof (int));
2417 containing_rgn = xmalloc ((last_basic_block) * sizeof (int));
7a31a7bd 2418
7a31a7bd 2419 /* Compute regions for scheduling. */
2420 if (reload_completed
b3d6de89 2421 || n_basic_blocks == 1
aae97b21 2422 || !flag_schedule_interblock
2423 || is_cfg_nonregular ())
7a31a7bd 2424 {
2425 find_single_block_region ();
2426 }
2427 else
2428 {
aae97b21 2429 /* Compute the dominators and post dominators. */
2430 calculate_dominance_info (CDI_DOMINATORS);
7a31a7bd 2431
aae97b21 2432 /* Find regions. */
2433 find_rgns ();
7a31a7bd 2434
aae97b21 2435 if (sched_verbose >= 3)
2436 debug_regions ();
7a31a7bd 2437
aae97b21 2438 /* For now. This will move as more and more of haifa is converted
2439 to using the cfg code in flow.c. */
2440 free_dominance_info (CDI_DOMINATORS);
7a31a7bd 2441 }
2442
7a31a7bd 2443
7fb47f9f 2444 if (CHECK_DEAD_NOTES)
7a31a7bd 2445 {
f20183e6 2446 blocks = sbitmap_alloc (last_basic_block);
f0af5a88 2447 deaths_in_region = xmalloc (sizeof (int) * nr_regions);
7fb47f9f 2448 /* Remove all death notes from the subroutine. */
2449 for (rgn = 0; rgn < nr_regions; rgn++)
2450 {
2451 int b;
7a31a7bd 2452
7fb47f9f 2453 sbitmap_zero (blocks);
2454 for (b = RGN_NR_BLOCKS (rgn) - 1; b >= 0; --b)
2455 SET_BIT (blocks, rgn_bb_table[RGN_BLOCKS (rgn) + b]);
7a31a7bd 2456
7fb47f9f 2457 deaths_in_region[rgn] = count_or_remove_death_notes (blocks, 1);
2458 }
2459 sbitmap_free (blocks);
7a31a7bd 2460 }
7fb47f9f 2461 else
2462 count_or_remove_death_notes (NULL, 1);
7a31a7bd 2463}
2464
2465/* The one entry point in this file. DUMP_FILE is the dump file for
2466 this pass. */
2467
2468void
60b8c5b3 2469schedule_insns (FILE *dump_file)
7a31a7bd 2470{
2471 sbitmap large_region_blocks, blocks;
2472 int rgn;
2473 int any_large_regions;
4c26117a 2474 basic_block bb;
7a31a7bd 2475
2476 /* Taking care of this degenerate case makes the rest of
2477 this code simpler. */
b3d6de89 2478 if (n_basic_blocks == 0)
7a31a7bd 2479 return;
2480
2481 nr_inter = 0;
2482 nr_spec = 0;
2483
2484 sched_init (dump_file);
2485
2486 init_regions ();
2487
2488 current_sched_info = &region_sched_info;
40734805 2489
7a31a7bd 2490 /* Schedule every region in the subroutine. */
2491 for (rgn = 0; rgn < nr_regions; rgn++)
2492 schedule_region (rgn);
2493
2494 /* Update life analysis for the subroutine. Do single block regions
2495 first so that we can verify that live_at_start didn't change. Then
1e625a2e 2496 do all other blocks. */
7a31a7bd 2497 /* ??? There is an outside possibility that update_life_info, or more
f712a0dc 2498 to the point propagate_block, could get called with nonzero flags
7a31a7bd 2499 more than once for one basic block. This would be kinda bad if it
2500 were to happen, since REG_INFO would be accumulated twice for the
2501 block, and we'd have twice the REG_DEAD notes.
2502
2503 I'm fairly certain that this _shouldn't_ happen, since I don't think
2504 that live_at_start should change at region heads. Not sure what the
2505 best way to test for this kind of thing... */
2506
2507 allocate_reg_life_data ();
f23d9a22 2508 compute_bb_for_insn ();
7a31a7bd 2509
2510 any_large_regions = 0;
f20183e6 2511 large_region_blocks = sbitmap_alloc (last_basic_block);
4c26117a 2512 sbitmap_zero (large_region_blocks);
2513 FOR_EACH_BB (bb)
2514 SET_BIT (large_region_blocks, bb->index);
7a31a7bd 2515
f20183e6 2516 blocks = sbitmap_alloc (last_basic_block);
7fb47f9f 2517 sbitmap_zero (blocks);
7a31a7bd 2518
7fb47f9f 2519 /* Update life information. For regions consisting of multiple blocks
2520 we've possibly done interblock scheduling that affects global liveness.
2521 For regions consisting of single blocks we need to do only local
2522 liveness. */
7a31a7bd 2523 for (rgn = 0; rgn < nr_regions; rgn++)
2524 if (RGN_NR_BLOCKS (rgn) > 1)
2525 any_large_regions = 1;
2526 else
2527 {
7a31a7bd 2528 SET_BIT (blocks, rgn_bb_table[RGN_BLOCKS (rgn)]);
2529 RESET_BIT (large_region_blocks, rgn_bb_table[RGN_BLOCKS (rgn)]);
7a31a7bd 2530 }
2531
7fb47f9f 2532 /* Don't update reg info after reload, since that affects
2533 regs_ever_live, which should not change after reload. */
2534 update_life_info (blocks, UPDATE_LIFE_LOCAL,
2535 (reload_completed ? PROP_DEATH_NOTES
2536 : PROP_DEATH_NOTES | PROP_REG_INFO));
7a31a7bd 2537 if (any_large_regions)
2538 {
2539 update_life_info (large_region_blocks, UPDATE_LIFE_GLOBAL,
2540 PROP_DEATH_NOTES | PROP_REG_INFO);
2541 }
2542
7fb47f9f 2543 if (CHECK_DEAD_NOTES)
2544 {
4ef53008 2545 /* Verify the counts of basic block notes in single the basic block
2546 regions. */
7fb47f9f 2547 for (rgn = 0; rgn < nr_regions; rgn++)
2548 if (RGN_NR_BLOCKS (rgn) == 1)
2549 {
7fb47f9f 2550 sbitmap_zero (blocks);
2551 SET_BIT (blocks, rgn_bb_table[RGN_BLOCKS (rgn)]);
2552
04e579b6 2553 gcc_assert (deaths_in_region[rgn]
2554 == count_or_remove_death_notes (blocks, 0));
7fb47f9f 2555 }
2556 free (deaths_in_region);
2557 }
2558
7a31a7bd 2559 /* Reposition the prologue and epilogue notes in case we moved the
2560 prologue/epilogue insns. */
2561 if (reload_completed)
2562 reposition_prologue_and_epilogue_notes (get_insns ());
2563
2564 /* Delete redundant line notes. */
2565 if (write_symbols != NO_DEBUG)
2566 rm_redundant_line_notes ();
2567
2568 if (sched_verbose)
2569 {
2570 if (reload_completed == 0 && flag_schedule_interblock)
2571 {
2572 fprintf (sched_dump,
2573 "\n;; Procedure interblock/speculative motions == %d/%d \n",
2574 nr_inter, nr_spec);
2575 }
2576 else
04e579b6 2577 gcc_assert (nr_inter <= 0);
7a31a7bd 2578 fprintf (sched_dump, "\n\n");
2579 }
2580
2581 /* Clean up. */
2582 free (rgn_table);
2583 free (rgn_bb_table);
2584 free (block_to_bb);
2585 free (containing_rgn);
2586
2587 sched_finish ();
2588
7a31a7bd 2589 sbitmap_free (blocks);
2590 sbitmap_free (large_region_blocks);
7a31a7bd 2591}
cda0a5f5 2592#endif
77fce4cd 2593\f
2594static bool
2595gate_handle_sched (void)
2596{
2597#ifdef INSN_SCHEDULING
2598 return flag_schedule_insns;
2599#else
2600 return 0;
2601#endif
2602}
2603
2604/* Run instruction scheduler. */
2605static void
2606rest_of_handle_sched (void)
2607{
2608#ifdef INSN_SCHEDULING
2609 /* Do control and data sched analysis,
2610 and write some of the results to dump file. */
2611
2612 schedule_insns (dump_file);
2613#endif
2614}
2615
2616static bool
2617gate_handle_sched2 (void)
2618{
2619#ifdef INSN_SCHEDULING
2620 return optimize > 0 && flag_schedule_insns_after_reload;
2621#else
2622 return 0;
2623#endif
2624}
2625
2626/* Run second scheduling pass after reload. */
2627static void
2628rest_of_handle_sched2 (void)
2629{
2630#ifdef INSN_SCHEDULING
2631 /* Do control and data sched analysis again,
2632 and write some more of the results to dump file. */
2633
2634 split_all_insns (1);
2635
2636 if (flag_sched2_use_superblocks || flag_sched2_use_traces)
2637 {
2638 schedule_ebbs (dump_file);
2639 /* No liveness updating code yet, but it should be easy to do.
2640 reg-stack recomputes the liveness when needed for now. */
2641 count_or_remove_death_notes (NULL, 1);
2642 cleanup_cfg (CLEANUP_EXPENSIVE);
2643 }
2644 else
2645 schedule_insns (dump_file);
2646#endif
2647}
2648
2649struct tree_opt_pass pass_sched =
2650{
2651 "sched1", /* name */
2652 gate_handle_sched, /* gate */
2653 rest_of_handle_sched, /* execute */
2654 NULL, /* sub */
2655 NULL, /* next */
2656 0, /* static_pass_number */
2657 TV_SCHED, /* tv_id */
2658 0, /* properties_required */
2659 0, /* properties_provided */
2660 0, /* properties_destroyed */
2661 0, /* todo_flags_start */
2662 TODO_dump_func |
2663 TODO_ggc_collect, /* todo_flags_finish */
2664 'S' /* letter */
2665};
2666
2667struct tree_opt_pass pass_sched2 =
2668{
2669 "sched2", /* name */
2670 gate_handle_sched2, /* gate */
2671 rest_of_handle_sched2, /* execute */
2672 NULL, /* sub */
2673 NULL, /* next */
2674 0, /* static_pass_number */
2675 TV_SCHED2, /* tv_id */
2676 0, /* properties_required */
2677 0, /* properties_provided */
2678 0, /* properties_destroyed */
2679 0, /* todo_flags_start */
2680 TODO_dump_func |
2681 TODO_ggc_collect, /* todo_flags_finish */
2682 'R' /* letter */
2683};
2684