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