OSDN Git Service

update
[pf3gnuchains/gcc-fork.git] / gcc / haifa-sched.c
1 /* Instruction scheduling pass.
2    Copyright (C) 1992, 93-97, 1998 Free Software Foundation, Inc.
3    Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
4    and currently maintained by, Jim Wilson (wilson@cygnus.com)
5
6    This file is part of GNU CC.
7
8    GNU CC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GNU CC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GNU CC; see the file COPYING.  If not, write to the Free
20    the Free Software Foundation, 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23
24 /* Instruction scheduling pass.
25
26    This pass implements list scheduling within basic blocks.  It is
27    run twice: (1) after flow analysis, but before register allocation,
28    and (2) after register allocation.
29
30    The first run performs interblock scheduling, moving insns between
31    different blocks in the same "region", and the second runs only
32    basic block scheduling.
33
34    Interblock motions performed are useful motions and speculative
35    motions, including speculative loads.  Motions requiring code
36    duplication are not supported.  The identification of motion type
37    and the check for validity of speculative motions requires
38    construction and analysis of the function's control flow graph.
39    The scheduler works as follows:
40
41    We compute insn priorities based on data dependencies.  Flow
42    analysis only creates a fraction of the data-dependencies we must
43    observe: namely, only those dependencies which the combiner can be
44    expected to use.  For this pass, we must therefore create the
45    remaining dependencies we need to observe: register dependencies,
46    memory dependencies, dependencies to keep function calls in order,
47    and the dependence between a conditional branch and the setting of
48    condition codes are all dealt with here.
49
50    The scheduler first traverses the data flow graph, starting with
51    the last instruction, and proceeding to the first, assigning values
52    to insn_priority as it goes.  This sorts the instructions
53    topologically by data dependence.
54
55    Once priorities have been established, we order the insns using
56    list scheduling.  This works as follows: starting with a list of
57    all the ready insns, and sorted according to priority number, we
58    schedule the insn from the end of the list by placing its
59    predecessors in the list according to their priority order.  We
60    consider this insn scheduled by setting the pointer to the "end" of
61    the list to point to the previous insn.  When an insn has no
62    predecessors, we either queue it until sufficient time has elapsed
63    or add it to the ready list.  As the instructions are scheduled or
64    when stalls are introduced, the queue advances and dumps insns into
65    the ready list.  When all insns down to the lowest priority have
66    been scheduled, the critical path of the basic block has been made
67    as short as possible.  The remaining insns are then scheduled in
68    remaining slots.
69
70    Function unit conflicts are resolved during forward list scheduling
71    by tracking the time when each insn is committed to the schedule
72    and from that, the time the function units it uses must be free.
73    As insns on the ready list are considered for scheduling, those
74    that would result in a blockage of the already committed insns are
75    queued until no blockage will result.
76
77    The following list shows the order in which we want to break ties
78    among insns in the ready list:
79
80    1.  choose insn with the longest path to end of bb, ties
81    broken by
82    2.  choose insn with least contribution to register pressure,
83    ties broken by
84    3.  prefer in-block upon interblock motion, ties broken by
85    4.  prefer useful upon speculative motion, ties broken by
86    5.  choose insn with largest control flow probability, ties
87    broken by
88    6.  choose insn with the least dependences upon the previously
89    scheduled insn, or finally
90    7   choose the insn which has the most insns dependent on it.
91    8.  choose insn with lowest UID.
92
93    Memory references complicate matters.  Only if we can be certain
94    that memory references are not part of the data dependency graph
95    (via true, anti, or output dependence), can we move operations past
96    memory references.  To first approximation, reads can be done
97    independently, while writes introduce dependencies.  Better
98    approximations will yield fewer dependencies.
99
100    Before reload, an extended analysis of interblock data dependences
101    is required for interblock scheduling.  This is performed in
102    compute_block_backward_dependences ().
103
104    Dependencies set up by memory references are treated in exactly the
105    same way as other dependencies, by using LOG_LINKS backward
106    dependences.  LOG_LINKS are translated into INSN_DEPEND forward
107    dependences for the purpose of forward list scheduling.
108
109    Having optimized the critical path, we may have also unduly
110    extended the lifetimes of some registers.  If an operation requires
111    that constants be loaded into registers, it is certainly desirable
112    to load those constants as early as necessary, but no earlier.
113    I.e., it will not do to load up a bunch of registers at the
114    beginning of a basic block only to use them at the end, if they
115    could be loaded later, since this may result in excessive register
116    utilization.
117
118    Note that since branches are never in basic blocks, but only end
119    basic blocks, this pass will not move branches.  But that is ok,
120    since we can use GNU's delayed branch scheduling pass to take care
121    of this case.
122
123    Also note that no further optimizations based on algebraic
124    identities are performed, so this pass would be a good one to
125    perform instruction splitting, such as breaking up a multiply
126    instruction into shifts and adds where that is profitable.
127
128    Given the memory aliasing analysis that this pass should perform,
129    it should be possible to remove redundant stores to memory, and to
130    load values from registers instead of hitting memory.
131
132    Before reload, speculative insns are moved only if a 'proof' exists
133    that no exception will be caused by this, and if no live registers
134    exist that inhibit the motion (live registers constraints are not
135    represented by data dependence edges).
136
137    This pass must update information that subsequent passes expect to
138    be correct.  Namely: reg_n_refs, reg_n_sets, reg_n_deaths,
139    reg_n_calls_crossed, and reg_live_length.  Also, basic_block_head,
140    basic_block_end.
141
142    The information in the line number notes is carefully retained by
143    this pass.  Notes that refer to the starting and ending of
144    exception regions are also carefully retained by this pass.  All
145    other NOTE insns are grouped in their same relative order at the
146    beginning of basic blocks and regions that have been scheduled.
147
148    The main entry point for this pass is schedule_insns(), called for
149    each function.  The work of the scheduler is organized in three
150    levels: (1) function level: insns are subject to splitting,
151    control-flow-graph is constructed, regions are computed (after
152    reload, each region is of one block), (2) region level: control
153    flow graph attributes required for interblock scheduling are
154    computed (dominators, reachability, etc.), data dependences and
155    priorities are computed, and (3) block level: insns in the block
156    are actually scheduled.  */
157 \f
158 #include "config.h"
159 #include "system.h"
160 #include "rtl.h"
161 #include "basic-block.h"
162 #include "regs.h"
163 #include "hard-reg-set.h"
164 #include "flags.h"
165 #include "insn-config.h"
166 #include "insn-attr.h"
167 #include "except.h"
168 #include "toplev.h"
169
170 extern char *reg_known_equiv_p;
171 extern rtx *reg_known_value;
172
173 #ifdef INSN_SCHEDULING
174
175 /* target_units bitmask has 1 for each unit in the cpu.  It should be
176    possible to compute this variable from the machine description.
177    But currently it is computed by examinning the insn list.  Since
178    this is only needed for visualization, it seems an acceptable
179    solution.  (For understanding the mapping of bits to units, see
180    definition of function_units[] in "insn-attrtab.c") */
181
182 static int target_units = 0;
183
184 /* issue_rate is the number of insns that can be scheduled in the same
185    machine cycle.  It can be defined in the config/mach/mach.h file,
186    otherwise we set it to 1.  */
187
188 static int issue_rate;
189
190 #ifndef ISSUE_RATE
191 #define ISSUE_RATE 1
192 #endif
193
194 /* sched-verbose controls the amount of debugging output the
195    scheduler prints.  It is controlled by -fsched-verbose-N:
196    N>0 and no -DSR : the output is directed to stderr.
197    N>=10 will direct the printouts to stderr (regardless of -dSR).
198    N=1: same as -dSR.
199    N=2: bb's probabilities, detailed ready list info, unit/insn info.
200    N=3: rtl at abort point, control-flow, regions info.
201    N=5: dependences info.  */
202
203 #define MAX_RGN_BLOCKS 10
204 #define MAX_RGN_INSNS 100
205
206 static int sched_verbose_param = 0;
207 static int sched_verbose = 0;
208
209 /* nr_inter/spec counts interblock/speculative motion for the function */
210 static int nr_inter, nr_spec;
211
212
213 /* debugging file. all printouts are sent to dump, which is always set,
214    either to stderr, or to the dump listing file (-dRS).  */
215 static FILE *dump = 0;
216
217 /* fix_sched_param() is called from toplev.c upon detection
218    of the -fsched-***-N options.  */
219
220 void
221 fix_sched_param (param, val)
222      char *param, *val;
223 {
224   if (!strcmp (param, "verbose"))
225     sched_verbose_param = atoi (val);
226   else
227     warning ("fix_sched_param: unknown param: %s", param);
228 }
229
230
231 /* Arrays set up by scheduling for the same respective purposes as
232    similar-named arrays set up by flow analysis.  We work with these
233    arrays during the scheduling pass so we can compare values against
234    unscheduled code.
235
236    Values of these arrays are copied at the end of this pass into the
237    arrays set up by flow analysis.  */
238 static int *sched_reg_n_calls_crossed;
239 static int *sched_reg_live_length;
240 static int *sched_reg_basic_block;
241
242 /* We need to know the current block number during the post scheduling
243    update of live register information so that we can also update
244    REG_BASIC_BLOCK if a register changes blocks.  */
245 static int current_block_num;
246
247 /* Element N is the next insn that sets (hard or pseudo) register
248    N within the current basic block; or zero, if there is no
249    such insn.  Needed for new registers which may be introduced
250    by splitting insns.  */
251 static rtx *reg_last_uses;
252 static rtx *reg_last_sets;
253 static regset reg_pending_sets;
254 static int reg_pending_sets_all;
255
256 /* Vector indexed by INSN_UID giving the original ordering of the insns.  */
257 static int *insn_luid;
258 #define INSN_LUID(INSN) (insn_luid[INSN_UID (INSN)])
259
260 /* Vector indexed by INSN_UID giving each instruction a priority.  */
261 static int *insn_priority;
262 #define INSN_PRIORITY(INSN) (insn_priority[INSN_UID (INSN)])
263
264 static short *insn_costs;
265 #define INSN_COST(INSN) insn_costs[INSN_UID (INSN)]
266
267 /* Vector indexed by INSN_UID giving an encoding of the function units
268    used.  */
269 static short *insn_units;
270 #define INSN_UNIT(INSN) insn_units[INSN_UID (INSN)]
271
272 /* Vector indexed by INSN_UID giving each instruction a register-weight.
273    This weight is an estimation of the insn contribution to registers pressure.  */
274 static int *insn_reg_weight;
275 #define INSN_REG_WEIGHT(INSN) (insn_reg_weight[INSN_UID (INSN)])
276
277 /* Vector indexed by INSN_UID giving list of insns which
278    depend upon INSN.  Unlike LOG_LINKS, it represents forward dependences.  */
279 static rtx *insn_depend;
280 #define INSN_DEPEND(INSN) insn_depend[INSN_UID (INSN)]
281
282 /* Vector indexed by INSN_UID. Initialized to the number of incoming
283    edges in forward dependence graph (= number of LOG_LINKS).  As
284    scheduling procedes, dependence counts are decreased.  An
285    instruction moves to the ready list when its counter is zero.  */
286 static int *insn_dep_count;
287 #define INSN_DEP_COUNT(INSN) (insn_dep_count[INSN_UID (INSN)])
288
289 /* Vector indexed by INSN_UID giving an encoding of the blockage range
290    function.  The unit and the range are encoded.  */
291 static unsigned int *insn_blockage;
292 #define INSN_BLOCKAGE(INSN) insn_blockage[INSN_UID (INSN)]
293 #define UNIT_BITS 5
294 #define BLOCKAGE_MASK ((1 << BLOCKAGE_BITS) - 1)
295 #define ENCODE_BLOCKAGE(U, R)                           \
296 ((((U) << UNIT_BITS) << BLOCKAGE_BITS                   \
297   | MIN_BLOCKAGE_COST (R)) << BLOCKAGE_BITS             \
298   | MAX_BLOCKAGE_COST (R))
299 #define UNIT_BLOCKED(B) ((B) >> (2 * BLOCKAGE_BITS))
300 #define BLOCKAGE_RANGE(B)                                                \
301   (((((B) >> BLOCKAGE_BITS) & BLOCKAGE_MASK) << (HOST_BITS_PER_INT / 2)) \
302    | ((B) & BLOCKAGE_MASK))
303
304 /* Encodings of the `<name>_unit_blockage_range' function.  */
305 #define MIN_BLOCKAGE_COST(R) ((R) >> (HOST_BITS_PER_INT / 2))
306 #define MAX_BLOCKAGE_COST(R) ((R) & ((1 << (HOST_BITS_PER_INT / 2)) - 1))
307
308 #define DONE_PRIORITY   -1
309 #define MAX_PRIORITY    0x7fffffff
310 #define TAIL_PRIORITY   0x7ffffffe
311 #define LAUNCH_PRIORITY 0x7f000001
312 #define DONE_PRIORITY_P(INSN) (INSN_PRIORITY (INSN) < 0)
313 #define LOW_PRIORITY_P(INSN) ((INSN_PRIORITY (INSN) & 0x7f000000) == 0)
314
315 /* Vector indexed by INSN_UID giving number of insns referring to this insn.  */
316 static int *insn_ref_count;
317 #define INSN_REF_COUNT(INSN) (insn_ref_count[INSN_UID (INSN)])
318
319 /* Vector indexed by INSN_UID giving line-number note in effect for each
320    insn.  For line-number notes, this indicates whether the note may be
321    reused.  */
322 static rtx *line_note;
323 #define LINE_NOTE(INSN) (line_note[INSN_UID (INSN)])
324
325 /* Vector indexed by basic block number giving the starting line-number
326    for each basic block.  */
327 static rtx *line_note_head;
328
329 /* List of important notes we must keep around.  This is a pointer to the
330    last element in the list.  */
331 static rtx note_list;
332
333 /* Regsets telling whether a given register is live or dead before the last
334    scheduled insn.  Must scan the instructions once before scheduling to
335    determine what registers are live or dead at the end of the block.  */
336 static regset bb_live_regs;
337
338 /* Regset telling whether a given register is live after the insn currently
339    being scheduled.  Before processing an insn, this is equal to bb_live_regs
340    above.  This is used so that we can find registers that are newly born/dead
341    after processing an insn.  */
342 static regset old_live_regs;
343
344 /* The chain of REG_DEAD notes.  REG_DEAD notes are removed from all insns
345    during the initial scan and reused later.  If there are not exactly as
346    many REG_DEAD notes in the post scheduled code as there were in the
347    prescheduled code then we trigger an abort because this indicates a bug.  */
348 static rtx dead_notes;
349
350 /* Queues, etc.  */
351
352 /* An instruction is ready to be scheduled when all insns preceding it
353    have already been scheduled.  It is important to ensure that all
354    insns which use its result will not be executed until its result
355    has been computed.  An insn is maintained in one of four structures:
356
357    (P) the "Pending" set of insns which cannot be scheduled until
358    their dependencies have been satisfied.
359    (Q) the "Queued" set of insns that can be scheduled when sufficient
360    time has passed.
361    (R) the "Ready" list of unscheduled, uncommitted insns.
362    (S) the "Scheduled" list of insns.
363
364    Initially, all insns are either "Pending" or "Ready" depending on
365    whether their dependencies are satisfied.
366
367    Insns move from the "Ready" list to the "Scheduled" list as they
368    are committed to the schedule.  As this occurs, the insns in the
369    "Pending" list have their dependencies satisfied and move to either
370    the "Ready" list or the "Queued" set depending on whether
371    sufficient time has passed to make them ready.  As time passes,
372    insns move from the "Queued" set to the "Ready" list.  Insns may
373    move from the "Ready" list to the "Queued" set if they are blocked
374    due to a function unit conflict.
375
376    The "Pending" list (P) are the insns in the INSN_DEPEND of the unscheduled
377    insns, i.e., those that are ready, queued, and pending.
378    The "Queued" set (Q) is implemented by the variable `insn_queue'.
379    The "Ready" list (R) is implemented by the variables `ready' and
380    `n_ready'.
381    The "Scheduled" list (S) is the new insn chain built by this pass.
382
383    The transition (R->S) is implemented in the scheduling loop in
384    `schedule_block' when the best insn to schedule is chosen.
385    The transition (R->Q) is implemented in `queue_insn' when an
386    insn is found to have a function unit conflict with the already
387    committed insns.
388    The transitions (P->R and P->Q) are implemented in `schedule_insn' as
389    insns move from the ready list to the scheduled list.
390    The transition (Q->R) is implemented in 'queue_to_insn' as time
391    passes or stalls are introduced.  */
392
393 /* Implement a circular buffer to delay instructions until sufficient
394    time has passed.  INSN_QUEUE_SIZE is a power of two larger than
395    MAX_BLOCKAGE and MAX_READY_COST computed by genattr.c.  This is the
396    longest time an isnsn may be queued.  */
397 static rtx insn_queue[INSN_QUEUE_SIZE];
398 static int q_ptr = 0;
399 static int q_size = 0;
400 #define NEXT_Q(X) (((X)+1) & (INSN_QUEUE_SIZE-1))
401 #define NEXT_Q_AFTER(X, C) (((X)+C) & (INSN_QUEUE_SIZE-1))
402
403 /* Vector indexed by INSN_UID giving the minimum clock tick at which
404    the insn becomes ready.  This is used to note timing constraints for
405    insns in the pending list.  */
406 static int *insn_tick;
407 #define INSN_TICK(INSN) (insn_tick[INSN_UID (INSN)])
408
409 /* Data structure for keeping track of register information
410    during that register's life.  */
411
412 struct sometimes
413   {
414     int regno;
415     int live_length;
416     int calls_crossed;
417   };
418
419 /* Forward declarations.  */
420 static void add_dependence PROTO ((rtx, rtx, enum reg_note));
421 static void remove_dependence PROTO ((rtx, rtx));
422 static rtx find_insn_list PROTO ((rtx, rtx));
423 static int insn_unit PROTO ((rtx));
424 static unsigned int blockage_range PROTO ((int, rtx));
425 static void clear_units PROTO ((void));
426 static int actual_hazard_this_instance PROTO ((int, int, rtx, int, int));
427 static void schedule_unit PROTO ((int, rtx, int));
428 static int actual_hazard PROTO ((int, rtx, int, int));
429 static int potential_hazard PROTO ((int, rtx, int));
430 static int insn_cost PROTO ((rtx, rtx, rtx));
431 static int priority PROTO ((rtx));
432 static void free_pending_lists PROTO ((void));
433 static void add_insn_mem_dependence PROTO ((rtx *, rtx *, rtx, rtx));
434 static void flush_pending_lists PROTO ((rtx, int));
435 static void sched_analyze_1 PROTO ((rtx, rtx));
436 static void sched_analyze_2 PROTO ((rtx, rtx));
437 static void sched_analyze_insn PROTO ((rtx, rtx, rtx));
438 static void sched_analyze PROTO ((rtx, rtx));
439 static void sched_note_set PROTO ((rtx, int));
440 static int rank_for_schedule PROTO ((const GENERIC_PTR, const GENERIC_PTR));
441 static void swap_sort PROTO ((rtx *, int));
442 static void queue_insn PROTO ((rtx, int));
443 static int schedule_insn PROTO ((rtx, rtx *, int, int));
444 static void create_reg_dead_note PROTO ((rtx, rtx));
445 static void attach_deaths PROTO ((rtx, rtx, int));
446 static void attach_deaths_insn PROTO ((rtx));
447 static int new_sometimes_live PROTO ((struct sometimes *, int, int));
448 static void finish_sometimes_live PROTO ((struct sometimes *, int));
449 static int schedule_block PROTO ((int, int));
450 static rtx regno_use_in PROTO ((int, rtx));
451 static void split_hard_reg_notes PROTO ((rtx, rtx, rtx));
452 static void new_insn_dead_notes PROTO ((rtx, rtx, rtx, rtx));
453 static void update_n_sets PROTO ((rtx, int));
454 static void update_flow_info PROTO ((rtx, rtx, rtx, rtx));
455 static char *safe_concat PROTO ((char *, char *, char *));
456
457 /* Main entry point of this file.  */
458 void schedule_insns PROTO ((FILE *));
459
460 /* Mapping of insns to their original block prior to scheduling.  */
461 static int *insn_orig_block;
462 #define INSN_BLOCK(insn) (insn_orig_block[INSN_UID (insn)])
463
464 /* Some insns (e.g. call) are not allowed to move across blocks.  */
465 static char *cant_move;
466 #define CANT_MOVE(insn) (cant_move[INSN_UID (insn)])
467
468 /* Control flow graph edges are kept in circular lists.  */
469 typedef struct
470   {
471     int from_block;
472     int to_block;
473     int next_in;
474     int next_out;
475   }
476 edge;
477 static edge *edge_table;
478
479 #define NEXT_IN(edge) (edge_table[edge].next_in)
480 #define NEXT_OUT(edge) (edge_table[edge].next_out)
481 #define FROM_BLOCK(edge) (edge_table[edge].from_block)
482 #define TO_BLOCK(edge) (edge_table[edge].to_block)
483
484 /* Number of edges in the control flow graph.  (in fact larger than
485    that by 1, since edge 0 is unused.) */
486 static int nr_edges;
487
488 /* Circular list of incoming/outgoing edges of a block */
489 static int *in_edges;
490 static int *out_edges;
491
492 #define IN_EDGES(block) (in_edges[block])
493 #define OUT_EDGES(block) (out_edges[block])
494
495 /* List of labels which cannot be deleted, needed for control
496    flow graph construction.  */
497 extern rtx forced_labels;
498
499
500 static int is_cfg_nonregular PROTO ((void));
501 static int build_control_flow PROTO ((int_list_ptr *, int_list_ptr *,
502                                       int *, int *));
503 static void new_edge PROTO ((int, int));
504
505
506 /* A region is the main entity for interblock scheduling: insns
507    are allowed to move between blocks in the same region, along
508    control flow graph edges, in the 'up' direction.  */
509 typedef struct
510   {
511     int rgn_nr_blocks;          /* number of blocks in region */
512     int rgn_blocks;             /* blocks in the region (actually index in rgn_bb_table) */
513   }
514 region;
515
516 /* Number of regions in the procedure */
517 static int nr_regions;
518
519 /* Table of region descriptions */
520 static region *rgn_table;
521
522 /* Array of lists of regions' blocks */
523 static int *rgn_bb_table;
524
525 /* Topological order of blocks in the region (if b2 is reachable from
526    b1, block_to_bb[b2] > block_to_bb[b1]).
527    Note: A basic block is always referred to by either block or b,
528    while its topological order name (in the region) is refered to by
529    bb.
530  */
531 static int *block_to_bb;
532
533 /* The number of the region containing a block.  */
534 static int *containing_rgn;
535
536 #define RGN_NR_BLOCKS(rgn) (rgn_table[rgn].rgn_nr_blocks)
537 #define RGN_BLOCKS(rgn) (rgn_table[rgn].rgn_blocks)
538 #define BLOCK_TO_BB(block) (block_to_bb[block])
539 #define CONTAINING_RGN(block) (containing_rgn[block])
540
541 void debug_regions PROTO ((void));
542 static void find_single_block_region PROTO ((void));
543 static void find_rgns PROTO ((int_list_ptr *, int_list_ptr *,
544                               int *, int *, sbitmap *));
545 static int too_large PROTO ((int, int *, int *));
546
547 extern void debug_live PROTO ((int, int));
548
549 /* Blocks of the current region being scheduled.  */
550 static int current_nr_blocks;
551 static int current_blocks;
552
553 /* The mapping from bb to block */
554 #define BB_TO_BLOCK(bb) (rgn_bb_table[current_blocks + (bb)])
555
556
557 /* Bit vectors and bitset operations are needed for computations on
558    the control flow graph.  */
559
560 typedef unsigned HOST_WIDE_INT *bitset;
561 typedef struct
562   {
563     int *first_member;          /* pointer to the list start in bitlst_table.  */
564     int nr_members;             /* the number of members of the bit list.     */
565   }
566 bitlst;
567
568 static int bitlst_table_last;
569 static int bitlst_table_size;
570 static int *bitlst_table;
571
572 static char bitset_member PROTO ((bitset, int, int));
573 static void extract_bitlst PROTO ((bitset, int, bitlst *));
574
575 /* target info declarations.
576
577    The block currently being scheduled is referred to as the "target" block,
578    while other blocks in the region from which insns can be moved to the
579    target are called "source" blocks.  The candidate structure holds info
580    about such sources: are they valid?  Speculative?  Etc.  */
581 typedef bitlst bblst;
582 typedef struct
583   {
584     char is_valid;
585     char is_speculative;
586     int src_prob;
587     bblst split_bbs;
588     bblst update_bbs;
589   }
590 candidate;
591
592 static candidate *candidate_table;
593
594 /* A speculative motion requires checking live information on the path
595    from 'source' to 'target'.  The split blocks are those to be checked.
596    After a speculative motion, live information should be modified in
597    the 'update' blocks.
598
599    Lists of split and update blocks for  each candidate of the current
600    target  are  in  array bblst_table */
601 static int *bblst_table, bblst_size, bblst_last;
602
603 #define IS_VALID(src) ( candidate_table[src].is_valid )
604 #define IS_SPECULATIVE(src) ( candidate_table[src].is_speculative )
605 #define SRC_PROB(src) ( candidate_table[src].src_prob )
606
607 /* The bb being currently scheduled.  */
608 static int target_bb;
609
610 /* List of edges.  */
611 typedef bitlst edgelst;
612
613 /* target info functions */
614 static void split_edges PROTO ((int, int, edgelst *));
615 static void compute_trg_info PROTO ((int));
616 void debug_candidate PROTO ((int));
617 void debug_candidates PROTO ((int));
618
619
620 /* Bit-set of bbs, where bit 'i' stands for bb 'i'.  */
621 typedef bitset bbset;
622
623 /* Number of words of the bbset.  */
624 static int bbset_size;
625
626 /* Dominators array: dom[i] contains the bbset of dominators of
627    bb i in the region.  */
628 static bbset *dom;
629
630 /* bb 0 is the only region entry */
631 #define IS_RGN_ENTRY(bb) (!bb)
632
633 /* Is bb_src dominated by bb_trg.  */
634 #define IS_DOMINATED(bb_src, bb_trg)                                 \
635 ( bitset_member (dom[bb_src], bb_trg, bbset_size) )
636
637 /* Probability: Prob[i] is a float in [0, 1] which is the probability
638    of bb i relative to the region entry.  */
639 static float *prob;
640
641 /*  The probability of bb_src, relative to bb_trg.  Note, that while the
642    'prob[bb]' is a float in [0, 1], this macro returns an integer
643    in [0, 100].  */
644 #define GET_SRC_PROB(bb_src, bb_trg) ((int) (100.0 * (prob[bb_src] / \
645                                                       prob[bb_trg])))
646
647 /* Bit-set of edges, where bit i stands for edge i.  */
648 typedef bitset edgeset;
649
650 /* Number of edges in the region.  */
651 static int rgn_nr_edges;
652
653 /* Array of size rgn_nr_edges.    */
654 static int *rgn_edges;
655
656 /* Number of words in an edgeset.    */
657 static int edgeset_size;
658
659 /* Mapping from each edge in the graph to its number in the rgn.  */
660 static int *edge_to_bit;
661 #define EDGE_TO_BIT(edge) (edge_to_bit[edge])
662
663 /* The split edges of a source bb is different for each target
664    bb.  In order to compute this efficiently, the 'potential-split edges'
665    are computed for each bb prior to scheduling a region.  This is actually
666    the split edges of each bb relative to the region entry.
667
668    pot_split[bb] is the set of potential split edges of bb.  */
669 static edgeset *pot_split;
670
671 /* For every bb, a set of its ancestor edges.  */
672 static edgeset *ancestor_edges;
673
674 static void compute_dom_prob_ps PROTO ((int));
675
676 #define ABS_VALUE(x) (((x)<0)?(-(x)):(x))
677 #define INSN_PROBABILITY(INSN) (SRC_PROB (BLOCK_TO_BB (INSN_BLOCK (INSN))))
678 #define IS_SPECULATIVE_INSN(INSN) (IS_SPECULATIVE (BLOCK_TO_BB (INSN_BLOCK (INSN))))
679 #define INSN_BB(INSN) (BLOCK_TO_BB (INSN_BLOCK (INSN)))
680
681 /* parameters affecting the decision of rank_for_schedule() */
682 #define MIN_DIFF_PRIORITY 2
683 #define MIN_PROBABILITY 40
684 #define MIN_PROB_DIFF 10
685
686 /* speculative scheduling functions */
687 static int check_live_1 PROTO ((int, rtx));
688 static void update_live_1 PROTO ((int, rtx));
689 static int check_live PROTO ((rtx, int));
690 static void update_live PROTO ((rtx, int));
691 static void set_spec_fed PROTO ((rtx));
692 static int is_pfree PROTO ((rtx, int, int));
693 static int find_conditional_protection PROTO ((rtx, int));
694 static int is_conditionally_protected PROTO ((rtx, int, int));
695 static int may_trap_exp PROTO ((rtx, int));
696 static int haifa_classify_insn PROTO ((rtx));
697 static int is_prisky PROTO ((rtx, int, int));
698 static int is_exception_free PROTO ((rtx, int, int));
699
700 static char find_insn_mem_list PROTO ((rtx, rtx, rtx, rtx));
701 static void compute_block_forward_dependences PROTO ((int));
702 static void init_rgn_data_dependences PROTO ((int));
703 static void add_branch_dependences PROTO ((rtx, rtx));
704 static void compute_block_backward_dependences PROTO ((int));
705 void debug_dependencies PROTO ((void));
706
707 /* Notes handling mechanism:
708    =========================
709    Generally, NOTES are saved before scheduling and restored after scheduling.
710    The scheduler distinguishes between three types of notes:
711
712    (1) LINE_NUMBER notes, generated and used for debugging.  Here,
713    before scheduling a region, a pointer to the LINE_NUMBER note is
714    added to the insn following it (in save_line_notes()), and the note
715    is removed (in rm_line_notes() and unlink_line_notes()).  After
716    scheduling the region, this pointer is used for regeneration of
717    the LINE_NUMBER note (in restore_line_notes()).
718
719    (2) LOOP_BEGIN, LOOP_END, SETJMP, EHREGION_BEG, EHREGION_END notes:
720    Before scheduling a region, a pointer to the note is added to the insn
721    that follows or precedes it.  (This happens as part of the data dependence
722    computation).  After scheduling an insn, the pointer contained in it is
723    used for regenerating the corresponding note (in reemit_notes).
724
725    (3) All other notes (e.g. INSN_DELETED):  Before scheduling a block,
726    these notes are put in a list (in rm_other_notes() and
727    unlink_other_notes ()).  After scheduling the block, these notes are
728    inserted at the beginning of the block (in schedule_block()).  */
729
730 static rtx unlink_other_notes PROTO ((rtx, rtx));
731 static rtx unlink_line_notes PROTO ((rtx, rtx));
732 static void rm_line_notes PROTO ((int));
733 static void save_line_notes PROTO ((int));
734 static void restore_line_notes PROTO ((int));
735 static void rm_redundant_line_notes PROTO ((void));
736 static void rm_other_notes PROTO ((rtx, rtx));
737 static rtx reemit_notes PROTO ((rtx, rtx));
738
739 static void get_block_head_tail PROTO ((int, rtx *, rtx *));
740
741 static void find_pre_sched_live PROTO ((int));
742 static void find_post_sched_live PROTO ((int));
743 static void update_reg_usage PROTO ((void));
744 static int queue_to_ready PROTO ((rtx [], int));
745
746 void debug_ready_list PROTO ((rtx[], int));
747 static void init_target_units PROTO (());
748 static void insn_print_units PROTO ((rtx));
749 static int get_visual_tbl_length PROTO (());
750 static void init_block_visualization PROTO (());
751 static void print_block_visualization PROTO ((int, char *));
752 static void visualize_scheduled_insns PROTO ((int, int));
753 static void visualize_no_unit PROTO ((rtx));
754 static void visualize_stall_cycles PROTO ((int, int));
755 static void print_exp PROTO ((char *, rtx, int));
756 static void print_value PROTO ((char *, rtx, int));
757 static void print_pattern PROTO ((char *, rtx, int));
758 static void print_insn PROTO ((char *, rtx, int));
759 void debug_reg_vector PROTO ((regset));
760
761 static rtx move_insn1 PROTO ((rtx, rtx));
762 static rtx move_insn PROTO ((rtx, rtx));
763 static rtx group_leader PROTO ((rtx));
764 static int set_priorities PROTO ((int));
765 static void init_rtx_vector PROTO ((rtx **, rtx *, int, int));
766 static void schedule_region PROTO ((int));
767 static void split_block_insns PROTO ((int));
768
769 #endif /* INSN_SCHEDULING */
770 \f
771 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
772
773 /* Helper functions for instruction scheduling.  */
774
775 /* An INSN_LIST containing all INSN_LISTs allocated but currently unused.  */
776 static rtx unused_insn_list;
777
778 /* An EXPR_LIST containing all EXPR_LISTs allocated but currently unused.  */
779 static rtx unused_expr_list;
780
781 static void free_list PROTO ((rtx *, rtx *));
782 static rtx alloc_INSN_LIST PROTO ((rtx, rtx));
783 static rtx alloc_EXPR_LIST PROTO ((int, rtx, rtx));
784
785 static void
786 free_list (listp, unused_listp)
787      rtx *listp, *unused_listp;
788 {
789   register rtx link, prev_link;
790
791   if (*listp == 0)
792     return;
793
794   prev_link = *listp;
795   link = XEXP (prev_link, 1);
796
797   while (link)
798     {
799       prev_link = link;
800       link = XEXP (link, 1);
801     }
802
803   XEXP (prev_link, 1) = *unused_listp;
804   *unused_listp = *listp;
805   *listp = 0;
806 }
807
808 static rtx
809 alloc_INSN_LIST (val, next)
810      rtx val, next;
811 {
812   rtx r;
813
814   if (unused_insn_list)
815     {
816       r = unused_insn_list;
817       unused_insn_list = XEXP (r, 1);
818       XEXP (r, 0) = val;
819       XEXP (r, 1) = next;
820       PUT_REG_NOTE_KIND (r, VOIDmode);
821     }
822   else
823     r = gen_rtx_INSN_LIST (VOIDmode, val, next);
824
825   return r;
826 }
827
828 static rtx
829 alloc_EXPR_LIST (kind, val, next)
830      int kind;
831      rtx val, next;
832 {
833   rtx r;
834
835   if (unused_insn_list)
836     {
837       r = unused_insn_list;
838       unused_insn_list = XEXP (r, 1);
839       XEXP (r, 0) = val;
840       XEXP (r, 1) = next;
841       PUT_REG_NOTE_KIND (r, kind);
842     }
843   else
844     r = gen_rtx_EXPR_LIST (kind, val, next);
845
846   return r;
847 }
848
849 /* Add ELEM wrapped in an INSN_LIST with reg note kind DEP_TYPE to the
850    LOG_LINKS of INSN, if not already there.  DEP_TYPE indicates the type
851    of dependence that this link represents.  */
852
853 static void
854 add_dependence (insn, elem, dep_type)
855      rtx insn;
856      rtx elem;
857      enum reg_note dep_type;
858 {
859   rtx link, next;
860
861   /* Don't depend an insn on itself.  */
862   if (insn == elem)
863     return;
864
865   /* If elem is part of a sequence that must be scheduled together, then
866      make the dependence point to the last insn of the sequence.
867      When HAVE_cc0, it is possible for NOTEs to exist between users and
868      setters of the condition codes, so we must skip past notes here.
869      Otherwise, NOTEs are impossible here.  */
870
871   next = NEXT_INSN (elem);
872
873 #ifdef HAVE_cc0
874   while (next && GET_CODE (next) == NOTE)
875     next = NEXT_INSN (next);
876 #endif
877
878   if (next && SCHED_GROUP_P (next)
879       && GET_CODE (next) != CODE_LABEL)
880     {
881       /* Notes will never intervene here though, so don't bother checking
882          for them.  */
883       /* We must reject CODE_LABELs, so that we don't get confused by one
884          that has LABEL_PRESERVE_P set, which is represented by the same
885          bit in the rtl as SCHED_GROUP_P.  A CODE_LABEL can never be
886          SCHED_GROUP_P.  */
887       while (NEXT_INSN (next) && SCHED_GROUP_P (NEXT_INSN (next))
888              && GET_CODE (NEXT_INSN (next)) != CODE_LABEL)
889         next = NEXT_INSN (next);
890
891       /* Again, don't depend an insn on itself.  */
892       if (insn == next)
893         return;
894
895       /* Make the dependence to NEXT, the last insn of the group, instead
896          of the original ELEM.  */
897       elem = next;
898     }
899
900 #ifdef INSN_SCHEDULING
901   /* (This code is guarded by INSN_SCHEDULING, otherwise INSN_BB is undefined.)
902      No need for interblock dependences with calls, since
903      calls are not moved between blocks.   Note: the edge where
904      elem is a CALL is still required.  */
905   if (GET_CODE (insn) == CALL_INSN
906       && (INSN_BB (elem) != INSN_BB (insn)))
907     return;
908
909 #endif
910
911   /* Check that we don't already have this dependence.  */
912   for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
913     if (XEXP (link, 0) == elem)
914       {
915         /* If this is a more restrictive type of dependence than the existing
916            one, then change the existing dependence to this type.  */
917         if ((int) dep_type < (int) REG_NOTE_KIND (link))
918           PUT_REG_NOTE_KIND (link, dep_type);
919         return;
920       }
921   /* Might want to check one level of transitivity to save conses.  */
922
923   link = alloc_INSN_LIST (elem, LOG_LINKS (insn));
924   LOG_LINKS (insn) = link;
925
926   /* Insn dependency, not data dependency.  */
927   PUT_REG_NOTE_KIND (link, dep_type);
928 }
929
930 /* Remove ELEM wrapped in an INSN_LIST from the LOG_LINKS
931    of INSN.  Abort if not found.  */
932
933 static void
934 remove_dependence (insn, elem)
935      rtx insn;
936      rtx elem;
937 {
938   rtx prev, link, next;
939   int found = 0;
940
941   for (prev = 0, link = LOG_LINKS (insn); link; link = next)
942     {
943       next = XEXP (link, 1);
944       if (XEXP (link, 0) == elem)
945         {
946           if (prev)
947             XEXP (prev, 1) = next;
948           else
949             LOG_LINKS (insn) = next;
950
951           XEXP (link, 1) = unused_insn_list;
952           unused_insn_list = link;
953
954           found = 1;
955         }
956       else
957         prev = link;
958     }
959
960   if (!found)
961     abort ();
962   return;
963 }
964 \f
965 #ifndef INSN_SCHEDULING
966 void
967 schedule_insns (dump_file)
968      FILE *dump_file;
969 {
970 }
971 #else
972 #ifndef __GNUC__
973 #define __inline
974 #endif
975
976 #ifndef HAIFA_INLINE
977 #define HAIFA_INLINE __inline
978 #endif
979
980 /* Computation of memory dependencies.  */
981
982 /* The *_insns and *_mems are paired lists.  Each pending memory operation
983    will have a pointer to the MEM rtx on one list and a pointer to the
984    containing insn on the other list in the same place in the list.  */
985
986 /* We can't use add_dependence like the old code did, because a single insn
987    may have multiple memory accesses, and hence needs to be on the list
988    once for each memory access.  Add_dependence won't let you add an insn
989    to a list more than once.  */
990
991 /* An INSN_LIST containing all insns with pending read operations.  */
992 static rtx pending_read_insns;
993
994 /* An EXPR_LIST containing all MEM rtx's which are pending reads.  */
995 static rtx pending_read_mems;
996
997 /* An INSN_LIST containing all insns with pending write operations.  */
998 static rtx pending_write_insns;
999
1000 /* An EXPR_LIST containing all MEM rtx's which are pending writes.  */
1001 static rtx pending_write_mems;
1002
1003 /* Indicates the combined length of the two pending lists.  We must prevent
1004    these lists from ever growing too large since the number of dependencies
1005    produced is at least O(N*N), and execution time is at least O(4*N*N), as
1006    a function of the length of these pending lists.  */
1007
1008 static int pending_lists_length;
1009
1010 /* The last insn upon which all memory references must depend.
1011    This is an insn which flushed the pending lists, creating a dependency
1012    between it and all previously pending memory references.  This creates
1013    a barrier (or a checkpoint) which no memory reference is allowed to cross.
1014
1015    This includes all non constant CALL_INSNs.  When we do interprocedural
1016    alias analysis, this restriction can be relaxed.
1017    This may also be an INSN that writes memory if the pending lists grow
1018    too large.  */
1019
1020 static rtx last_pending_memory_flush;
1021
1022 /* The last function call we have seen.  All hard regs, and, of course,
1023    the last function call, must depend on this.  */
1024
1025 static rtx last_function_call;
1026
1027 /* The LOG_LINKS field of this is a list of insns which use a pseudo register
1028    that does not already cross a call.  We create dependencies between each
1029    of those insn and the next call insn, to ensure that they won't cross a call
1030    after scheduling is done.  */
1031
1032 static rtx sched_before_next_call;
1033
1034 /* Pointer to the last instruction scheduled.  Used by rank_for_schedule,
1035    so that insns independent of the last scheduled insn will be preferred
1036    over dependent instructions.  */
1037
1038 static rtx last_scheduled_insn;
1039
1040 /* Data structures for the computation of data dependences in a regions.  We
1041    keep one copy of each of the declared above variables for each bb in the
1042    region.  Before analyzing the data dependences for a bb, its variables
1043    are initialized as a function of the variables of its predecessors.  When
1044    the analysis for a bb completes, we save the contents of each variable X
1045    to a corresponding bb_X[bb] variable.  For example, pending_read_insns is
1046    copied to bb_pending_read_insns[bb].  Another change is that few
1047    variables are now a list of insns rather than a single insn:
1048    last_pending_memory_flash, last_function_call, reg_last_sets.  The
1049    manipulation of these variables was changed appropriately.  */
1050
1051 static rtx **bb_reg_last_uses;
1052 static rtx **bb_reg_last_sets;
1053
1054 static rtx *bb_pending_read_insns;
1055 static rtx *bb_pending_read_mems;
1056 static rtx *bb_pending_write_insns;
1057 static rtx *bb_pending_write_mems;
1058 static int *bb_pending_lists_length;
1059
1060 static rtx *bb_last_pending_memory_flush;
1061 static rtx *bb_last_function_call;
1062 static rtx *bb_sched_before_next_call;
1063
1064 /* functions for construction of the control flow graph.  */
1065
1066 /* Return 1 if control flow graph should not be constructed, 0 otherwise.
1067
1068    We decide not to build the control flow graph if there is possibly more
1069    than one entry to the function, if computed branches exist, of if we
1070    have nonlocal gotos.  */
1071
1072 static int
1073 is_cfg_nonregular ()
1074 {
1075   int b;
1076   rtx insn;
1077   RTX_CODE code;
1078
1079   /* If we have a label that could be the target of a nonlocal goto, then
1080      the cfg is not well structured.  */
1081   if (nonlocal_label_rtx_list () != NULL)
1082     return 1;
1083
1084   /* If we have any forced labels, then the cfg is not well structured.  */
1085   if (forced_labels)
1086     return 1;
1087
1088   /* If this function has a computed jump, then we consider the cfg
1089      not well structured.  */
1090   if (current_function_has_computed_jump)
1091     return 1;
1092
1093   /* If we have exception handlers, then we consider the cfg not well
1094      structured.  ?!?  We should be able to handle this now that flow.c
1095      computes an accurate cfg for EH.  */
1096   if (exception_handler_labels)
1097     return 1;
1098
1099   /* If we have non-jumping insns which refer to labels, then we consider
1100      the cfg not well structured.  */
1101   /* check for labels referred to other thn by jumps */
1102   for (b = 0; b < n_basic_blocks; b++)
1103     for (insn = basic_block_head[b];; insn = NEXT_INSN (insn))
1104       {
1105         code = GET_CODE (insn);
1106         if (GET_RTX_CLASS (code) == 'i')
1107           {
1108             rtx note;
1109
1110             for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1111               if (REG_NOTE_KIND (note) == REG_LABEL)
1112                 return 1;
1113           }
1114
1115         if (insn == basic_block_end[b])
1116           break;
1117       }
1118
1119   /* All the tests passed.  Consider the cfg well structured.  */
1120   return 0;
1121 }
1122
1123 /* Build the control flow graph and set nr_edges.
1124
1125    Instead of trying to build a cfg ourselves, we rely on flow to
1126    do it for us.  Stamp out useless code (and bug) duplication.
1127
1128    Return nonzero if an irregularity in the cfg is found which would
1129    prevent cross block scheduling.  */
1130
1131 static int
1132 build_control_flow (s_preds, s_succs, num_preds, num_succs)
1133      int_list_ptr *s_preds;
1134      int_list_ptr *s_succs;
1135      int *num_preds;
1136      int *num_succs;
1137 {
1138   int i;
1139   int_list_ptr succ;
1140   int unreachable;
1141
1142   /* Count the number of edges in the cfg.  */
1143   nr_edges = 0;
1144   unreachable = 0;
1145   for (i = 0; i < n_basic_blocks; i++)
1146     {
1147       nr_edges += num_succs[i];
1148
1149       /* Unreachable loops with more than one basic block are detected
1150          during the DFS traversal in find_rgns.
1151
1152          Unreachable loops with a single block are detected here.  This
1153          test is redundant with the one in find_rgns, but it's much
1154          cheaper to go ahead and catch the trivial case here.  */
1155       if (num_preds[i] == 0
1156           || (num_preds[i] == 1 && INT_LIST_VAL (s_preds[i]) == i))
1157         unreachable = 1;
1158     }
1159
1160   /* Account for entry/exit edges.  */
1161   nr_edges += 2;
1162
1163   in_edges = (int *) xmalloc (n_basic_blocks * sizeof (int));
1164   out_edges = (int *) xmalloc (n_basic_blocks * sizeof (int));
1165   bzero ((char *) in_edges, n_basic_blocks * sizeof (int));
1166   bzero ((char *) out_edges, n_basic_blocks * sizeof (int));
1167
1168   edge_table = (edge *) xmalloc ((nr_edges) * sizeof (edge));
1169   bzero ((char *) edge_table, ((nr_edges) * sizeof (edge)));
1170
1171   nr_edges = 0;
1172   for (i = 0; i < n_basic_blocks; i++)
1173     for (succ = s_succs[i]; succ; succ = succ->next)
1174       {
1175         if (INT_LIST_VAL (succ) != EXIT_BLOCK)
1176           new_edge (i, INT_LIST_VAL (succ));
1177       }
1178
1179   /* increment by 1, since edge 0 is unused.  */
1180   nr_edges++;
1181
1182   return unreachable;
1183 }
1184
1185
1186 /* Record an edge in the control flow graph from SOURCE to TARGET.
1187
1188    In theory, this is redundant with the s_succs computed above, but
1189    we have not converted all of haifa to use information from the
1190    integer lists.  */
1191
1192 static void
1193 new_edge (source, target)
1194      int source, target;
1195 {
1196   int e, next_edge;
1197   int curr_edge, fst_edge;
1198
1199   /* check for duplicates */
1200   fst_edge = curr_edge = OUT_EDGES (source);
1201   while (curr_edge)
1202     {
1203       if (FROM_BLOCK (curr_edge) == source
1204           && TO_BLOCK (curr_edge) == target)
1205         {
1206           return;
1207         }
1208
1209       curr_edge = NEXT_OUT (curr_edge);
1210
1211       if (fst_edge == curr_edge)
1212         break;
1213     }
1214
1215   e = ++nr_edges;
1216
1217   FROM_BLOCK (e) = source;
1218   TO_BLOCK (e) = target;
1219
1220   if (OUT_EDGES (source))
1221     {
1222       next_edge = NEXT_OUT (OUT_EDGES (source));
1223       NEXT_OUT (OUT_EDGES (source)) = e;
1224       NEXT_OUT (e) = next_edge;
1225     }
1226   else
1227     {
1228       OUT_EDGES (source) = e;
1229       NEXT_OUT (e) = e;
1230     }
1231
1232   if (IN_EDGES (target))
1233     {
1234       next_edge = NEXT_IN (IN_EDGES (target));
1235       NEXT_IN (IN_EDGES (target)) = e;
1236       NEXT_IN (e) = next_edge;
1237     }
1238   else
1239     {
1240       IN_EDGES (target) = e;
1241       NEXT_IN (e) = e;
1242     }
1243 }
1244
1245
1246 /* BITSET macros for operations on the control flow graph.  */
1247
1248 /* Compute  bitwise union  of two  bitsets.  */
1249 #define BITSET_UNION(set1, set2, len)                                \
1250 do { register bitset tp = set1, sp = set2;                           \
1251      register int i;                                                 \
1252      for (i = 0; i < len; i++)                                       \
1253        *(tp++) |= *(sp++); } while (0)
1254
1255 /* Compute  bitwise intersection  of two  bitsets.  */
1256 #define BITSET_INTER(set1, set2, len)                                \
1257 do { register bitset tp = set1, sp = set2;                           \
1258      register int i;                                                 \
1259      for (i = 0; i < len; i++)                                       \
1260        *(tp++) &= *(sp++); } while (0)
1261
1262 /* Compute bitwise   difference of  two bitsets.  */
1263 #define BITSET_DIFFER(set1, set2, len)                               \
1264 do { register bitset tp = set1, sp = set2;                           \
1265      register int i;                                                 \
1266      for (i = 0; i < len; i++)                                       \
1267        *(tp++) &= ~*(sp++); } while (0)
1268
1269 /* Inverts every bit of bitset 'set' */
1270 #define BITSET_INVERT(set, len)                                      \
1271 do { register bitset tmpset = set;                                   \
1272      register int i;                                                 \
1273      for (i = 0; i < len; i++, tmpset++)                             \
1274        *tmpset = ~*tmpset; } while (0)
1275
1276 /* Turn on the index'th bit in bitset set.  */
1277 #define BITSET_ADD(set, index, len)                                  \
1278 {                                                                    \
1279   if (index >= HOST_BITS_PER_WIDE_INT * len)                         \
1280     abort ();                                                        \
1281   else                                                               \
1282     set[index/HOST_BITS_PER_WIDE_INT] |=                             \
1283       1 << (index % HOST_BITS_PER_WIDE_INT);                         \
1284 }
1285
1286 /* Turn off the index'th bit in set.  */
1287 #define BITSET_REMOVE(set, index, len)                               \
1288 {                                                                    \
1289   if (index >= HOST_BITS_PER_WIDE_INT * len)                         \
1290     abort ();                                                        \
1291   else                                                               \
1292     set[index/HOST_BITS_PER_WIDE_INT] &=                             \
1293       ~(1 << (index%HOST_BITS_PER_WIDE_INT));                        \
1294 }
1295
1296
1297 /* Check if the index'th bit in bitset  set is on.  */
1298
1299 static char
1300 bitset_member (set, index, len)
1301      bitset set;
1302      int index, len;
1303 {
1304   if (index >= HOST_BITS_PER_WIDE_INT * len)
1305     abort ();
1306   return (set[index / HOST_BITS_PER_WIDE_INT] &
1307           1 << (index % HOST_BITS_PER_WIDE_INT)) ? 1 : 0;
1308 }
1309
1310
1311 /* Translate a bit-set SET to a list BL of the bit-set members.  */
1312
1313 static void
1314 extract_bitlst (set, len, bl)
1315      bitset set;
1316      int len;
1317      bitlst *bl;
1318 {
1319   int i, j, offset;
1320   unsigned HOST_WIDE_INT word;
1321
1322   /* bblst table space is reused in each call to extract_bitlst */
1323   bitlst_table_last = 0;
1324
1325   bl->first_member = &bitlst_table[bitlst_table_last];
1326   bl->nr_members = 0;
1327
1328   for (i = 0; i < len; i++)
1329     {
1330       word = set[i];
1331       offset = i * HOST_BITS_PER_WIDE_INT;
1332       for (j = 0; word; j++)
1333         {
1334           if (word & 1)
1335             {
1336               bitlst_table[bitlst_table_last++] = offset;
1337               (bl->nr_members)++;
1338             }
1339           word >>= 1;
1340           ++offset;
1341         }
1342     }
1343
1344 }
1345
1346
1347 /* functions for the construction of regions */
1348
1349 /* Print the regions, for debugging purposes.  Callable from debugger.  */
1350
1351 void
1352 debug_regions ()
1353 {
1354   int rgn, bb;
1355
1356   fprintf (dump, "\n;;   ------------ REGIONS ----------\n\n");
1357   for (rgn = 0; rgn < nr_regions; rgn++)
1358     {
1359       fprintf (dump, ";;\trgn %d nr_blocks %d:\n", rgn,
1360                rgn_table[rgn].rgn_nr_blocks);
1361       fprintf (dump, ";;\tbb/block: ");
1362
1363       for (bb = 0; bb < rgn_table[rgn].rgn_nr_blocks; bb++)
1364         {
1365           current_blocks = RGN_BLOCKS (rgn);
1366
1367           if (bb != BLOCK_TO_BB (BB_TO_BLOCK (bb)))
1368             abort ();
1369
1370           fprintf (dump, " %d/%d ", bb, BB_TO_BLOCK (bb));
1371         }
1372
1373       fprintf (dump, "\n\n");
1374     }
1375 }
1376
1377
1378 /* Build a single block region for each basic block in the function.
1379    This allows for using the same code for interblock and basic block
1380    scheduling.  */
1381
1382 static void
1383 find_single_block_region ()
1384 {
1385   int i;
1386
1387   for (i = 0; i < n_basic_blocks; i++)
1388     {
1389       rgn_bb_table[i] = i;
1390       RGN_NR_BLOCKS (i) = 1;
1391       RGN_BLOCKS (i) = i;
1392       CONTAINING_RGN (i) = i;
1393       BLOCK_TO_BB (i) = 0;
1394     }
1395   nr_regions = n_basic_blocks;
1396 }
1397
1398
1399 /* Update number of blocks and the estimate for number of insns
1400    in the region.  Return 1 if the region is "too large" for interblock
1401    scheduling (compile time considerations), otherwise return 0.  */
1402
1403 static int
1404 too_large (block, num_bbs, num_insns)
1405      int block, *num_bbs, *num_insns;
1406 {
1407   (*num_bbs)++;
1408   (*num_insns) += (INSN_LUID (basic_block_end[block]) -
1409                    INSN_LUID (basic_block_head[block]));
1410   if ((*num_bbs > MAX_RGN_BLOCKS) || (*num_insns > MAX_RGN_INSNS))
1411     return 1;
1412   else
1413     return 0;
1414 }
1415
1416
1417 /* Update_loop_relations(blk, hdr): Check if the loop headed by max_hdr[blk]
1418    is still an inner loop.  Put in max_hdr[blk] the header of the most inner
1419    loop containing blk.  */
1420 #define UPDATE_LOOP_RELATIONS(blk, hdr)                              \
1421 {                                                                    \
1422   if (max_hdr[blk] == -1)                                            \
1423     max_hdr[blk] = hdr;                                              \
1424   else if (dfs_nr[max_hdr[blk]] > dfs_nr[hdr])                       \
1425          RESET_BIT (inner, hdr);                                     \
1426   else if (dfs_nr[max_hdr[blk]] < dfs_nr[hdr])                       \
1427          {                                                           \
1428             RESET_BIT (inner,max_hdr[blk]);                          \
1429             max_hdr[blk] = hdr;                                      \
1430          }                                                           \
1431 }
1432
1433
1434 /* Find regions for interblock scheduling.
1435
1436    A region for scheduling can be:
1437
1438      * A loop-free procedure, or
1439
1440      * A reducible inner loop, or
1441
1442      * A basic block not contained in any other region.
1443
1444
1445    ?!? In theory we could build other regions based on extended basic
1446    blocks or reverse extended basic blocks.  Is it worth the trouble?
1447
1448    Loop blocks that form a region are put into the region's block list
1449    in topological order.
1450
1451    This procedure stores its results into the following global (ick) variables
1452
1453      * rgn_nr
1454      * rgn_table
1455      * rgn_bb_table
1456      * block_to_bb
1457      * containing region
1458
1459
1460    We use dominator relationships to avoid making regions out of non-reducible
1461    loops.
1462
1463    This procedure needs to be converted to work on pred/succ lists instead
1464    of edge tables.  That would simplify it somewhat.  */
1465
1466 static void
1467 find_rgns (s_preds, s_succs, num_preds, num_succs, dom)
1468      int_list_ptr *s_preds;
1469      int_list_ptr *s_succs;
1470      int *num_preds;
1471      int *num_succs;
1472      sbitmap *dom;
1473 {
1474   int *max_hdr, *dfs_nr, *stack, *queue, *degree;
1475   char no_loops = 1;
1476   int node, child, loop_head, i, head, tail;
1477   int count = 0, sp, idx = 0, current_edge = out_edges[0];
1478   int num_bbs, num_insns, unreachable;
1479   int too_large_failure;
1480
1481   /* Note if an edge has been passed.  */
1482   sbitmap passed;
1483
1484   /* Note if a block is a natural loop header.  */
1485   sbitmap header;
1486
1487   /* Note if a block is an natural inner loop header.  */
1488   sbitmap inner;
1489
1490   /* Note if a block is in the block queue. */
1491   sbitmap in_queue;
1492
1493   /* Note if a block is in the block queue. */
1494   sbitmap in_stack;
1495
1496   /* Perform a DFS traversal of the cfg.  Identify loop headers, inner loops
1497      and a mapping from block to its loop header (if the block is contained
1498      in a loop, else -1).
1499
1500      Store results in HEADER, INNER, and MAX_HDR respectively, these will
1501      be used as inputs to the second traversal.
1502
1503      STACK, SP and DFS_NR are only used during the first traversal.  */
1504
1505   /* Allocate and initialize variables for the first traversal.  */
1506   max_hdr = (int *) alloca (n_basic_blocks * sizeof (int));
1507   dfs_nr = (int *) alloca (n_basic_blocks * sizeof (int));
1508   bzero ((char *) dfs_nr, n_basic_blocks * sizeof (int));
1509   stack = (int *) alloca (nr_edges * sizeof (int));
1510
1511   inner = sbitmap_alloc (n_basic_blocks);
1512   sbitmap_ones (inner);
1513
1514   header = sbitmap_alloc (n_basic_blocks);
1515   sbitmap_zero (header);
1516
1517   passed = sbitmap_alloc (nr_edges);
1518   sbitmap_zero (passed);
1519
1520   in_queue = sbitmap_alloc (n_basic_blocks);
1521   sbitmap_zero (in_queue);
1522
1523   in_stack = sbitmap_alloc (n_basic_blocks);
1524   sbitmap_zero (in_stack);
1525
1526   for (i = 0; i < n_basic_blocks; i++)
1527     max_hdr[i] = -1;
1528
1529   /* DFS traversal to find inner loops in the cfg.  */
1530
1531   sp = -1;
1532   while (1)
1533     {
1534       if (current_edge == 0 || TEST_BIT (passed, current_edge))
1535         {
1536           /* We have reached a leaf node or a node that was already
1537              processed.  Pop edges off the stack until we find
1538              an edge that has not yet been processed.  */
1539           while (sp >= 0
1540                  && (current_edge == 0 || TEST_BIT (passed, current_edge)))
1541             {
1542               /* Pop entry off the stack.  */
1543               current_edge = stack[sp--];
1544               node = FROM_BLOCK (current_edge);
1545               child = TO_BLOCK (current_edge);
1546               RESET_BIT (in_stack, child);
1547               if (max_hdr[child] >= 0 && TEST_BIT (in_stack, max_hdr[child]))
1548                 UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
1549               current_edge = NEXT_OUT (current_edge);
1550             }
1551
1552           /* See if have finished the DFS tree traversal.  */
1553           if (sp < 0 && TEST_BIT (passed, current_edge))
1554             break;
1555
1556           /* Nope, continue the traversal with the popped node.  */
1557           continue;
1558         }
1559
1560       /* Process a node.  */
1561       node = FROM_BLOCK (current_edge);
1562       child = TO_BLOCK (current_edge);
1563       SET_BIT (in_stack, node);
1564       dfs_nr[node] = ++count;
1565
1566       /* If the successor is in the stack, then we've found a loop.
1567          Mark the loop, if it is not a natural loop, then it will
1568          be rejected during the second traversal.  */
1569       if (TEST_BIT (in_stack, child))
1570         {
1571           no_loops = 0;
1572           SET_BIT (header, child);
1573           UPDATE_LOOP_RELATIONS (node, child);
1574           SET_BIT (passed, current_edge);
1575           current_edge = NEXT_OUT (current_edge);
1576           continue;
1577         }
1578
1579       /* If the child was already visited, then there is no need to visit
1580          it again.  Just update the loop relationships and restart
1581          with a new edge.  */
1582       if (dfs_nr[child])
1583         {
1584           if (max_hdr[child] >= 0 && TEST_BIT (in_stack, max_hdr[child]))
1585             UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
1586           SET_BIT (passed, current_edge);
1587           current_edge = NEXT_OUT (current_edge);
1588           continue;
1589         }
1590
1591       /* Push an entry on the stack and continue DFS traversal.  */
1592       stack[++sp] = current_edge;
1593       SET_BIT (passed, current_edge);
1594       current_edge = OUT_EDGES (child);
1595     }
1596
1597   /* Another check for unreachable blocks.  The earlier test in
1598      is_cfg_nonregular only finds unreachable blocks that do not
1599      form a loop.
1600
1601      The DFS traversal will mark every block that is reachable from
1602      the entry node by placing a nonzero value in dfs_nr.  Thus if
1603      dfs_nr is zero for any block, then it must be unreachable.  */
1604   unreachable = 0;
1605   for (i = 0; i < n_basic_blocks; i++)
1606     if (dfs_nr[i] == 0)
1607       {
1608         unreachable = 1;
1609         break;
1610       }
1611
1612   /* Gross.  To avoid wasting memory, the second pass uses the dfs_nr array
1613      to hold degree counts.  */
1614   degree = dfs_nr;
1615
1616   /* Compute the in-degree of every block in the graph */
1617   for (i = 0; i < n_basic_blocks; i++)
1618     degree[i] = num_preds[i];
1619
1620   /* Do not perform region scheduling if there are any unreachable
1621      blocks.  */
1622   if (!unreachable)
1623     {
1624       if (no_loops)
1625         SET_BIT (header, 0);
1626
1627       /* Second travsersal:find reducible inner loops and topologically sort
1628          block of each region.  */
1629
1630       queue = (int *) alloca (n_basic_blocks * sizeof (int));
1631
1632       /* Find blocks which are inner loop headers.  We still have non-reducible
1633          loops to consider at this point.  */
1634       for (i = 0; i < n_basic_blocks; i++)
1635         {
1636           if (TEST_BIT (header, i) && TEST_BIT (inner, i))
1637             {
1638               int_list_ptr ps;
1639               int j;
1640
1641               /* Now check that the loop is reducible.  We do this separate
1642                  from finding inner loops so that we do not find a reducible
1643                  loop which contains an inner  non-reducible loop.
1644
1645                  A simple way to find reducible/natrual loops is to verify
1646                  that each block in the loop is dominated by the loop
1647                  header.
1648
1649                  If there exists a block that is not dominated by the loop
1650                  header, then the block is reachable from outside the loop
1651                  and thus the loop is not a natural loop.  */
1652               for (j = 0; j < n_basic_blocks; j++)      
1653                 {
1654                   /* First identify blocks in the loop, except for the loop
1655                      entry block.  */
1656                   if (i == max_hdr[j] && i != j)
1657                     {
1658                       /* Now verify that the block is dominated by the loop
1659                          header.  */
1660                       if (!TEST_BIT (dom[j], i))
1661                         break;
1662                     }
1663                 }
1664
1665               /* If we exited the loop early, then I is the header of a non
1666                  reducible loop and we should quit processing it now.  */
1667               if (j != n_basic_blocks)
1668                 continue;
1669
1670               /* I is a header of an inner loop, or block 0 in a subroutine
1671                  with no loops at all.  */
1672               head = tail = -1;
1673               too_large_failure = 0;
1674               loop_head = max_hdr[i];
1675
1676               /* Decrease degree of all I's successors for topological
1677                  ordering.  */
1678               for (ps = s_succs[i]; ps; ps = ps->next)
1679                 if (INT_LIST_VAL (ps) != EXIT_BLOCK
1680                     && INT_LIST_VAL (ps) != ENTRY_BLOCK)
1681                   --degree[INT_LIST_VAL(ps)];
1682
1683               /* Estimate # insns, and count # blocks in the region.  */
1684               num_bbs = 1;
1685               num_insns = (INSN_LUID (basic_block_end[i])
1686                            - INSN_LUID (basic_block_head[i]));
1687
1688
1689               /* Find all loop latches (blocks which back edges to the loop
1690                  header) or all the leaf blocks in the cfg has no loops.
1691
1692                  Place those blocks into the queue.  */
1693               if (no_loops)
1694                 {
1695                   for (j = 0; j < n_basic_blocks; j++)
1696                     /* Leaf nodes have only a single successor which must
1697                        be EXIT_BLOCK.  */
1698                     if (num_succs[j] == 1
1699                         && INT_LIST_VAL (s_succs[j]) == EXIT_BLOCK)
1700                       {
1701                         queue[++tail] = j;
1702                         SET_BIT (in_queue, j);
1703
1704                         if (too_large (j, &num_bbs, &num_insns))
1705                           {
1706                             too_large_failure = 1;
1707                             break;
1708                           }
1709                       }
1710                 }
1711               else
1712                 {
1713                   int_list_ptr ps;
1714
1715                   for (ps = s_preds[i]; ps; ps = ps->next)
1716                     {
1717                       node = INT_LIST_VAL (ps);
1718
1719                       if (node == ENTRY_BLOCK || node == EXIT_BLOCK)
1720                         continue;
1721  
1722                       if (max_hdr[node] == loop_head && node != i)
1723                         {
1724                           /* This is a loop latch.  */
1725                           queue[++tail] = node;
1726                           SET_BIT (in_queue, node);
1727
1728                           if (too_large (node, &num_bbs, &num_insns))
1729                             {
1730                               too_large_failure = 1;
1731                               break;
1732                             }
1733                         }
1734                       
1735                     }
1736                 }
1737
1738               /* Now add all the blocks in the loop to the queue.
1739
1740              We know the loop is a natural loop; however the algorithm
1741              above will not always mark certain blocks as being in the
1742              loop.  Consider:
1743                 node   children
1744                  a        b,c
1745                  b        c
1746                  c        a,d
1747                  d        b
1748
1749
1750              The algorithm in the DFS traversal may not mark B & D as part
1751              of the loop (ie they will not have max_hdr set to A).
1752
1753              We know they can not be loop latches (else they would have
1754              had max_hdr set since they'd have a backedge to a dominator
1755              block).  So we don't need them on the initial queue.
1756
1757              We know they are part of the loop because they are dominated
1758              by the loop header and can be reached by a backwards walk of
1759              the edges starting with nodes on the initial queue.
1760
1761              It is safe and desirable to include those nodes in the
1762              loop/scheduling region.  To do so we would need to decrease
1763              the degree of a node if it is the target of a backedge
1764              within the loop itself as the node is placed in the queue.
1765
1766              We do not do this because I'm not sure that the actual
1767              scheduling code will properly handle this case. ?!? */
1768         
1769               while (head < tail && !too_large_failure)
1770                 {
1771                   int_list_ptr ps;
1772                   child = queue[++head];
1773
1774                   for (ps = s_preds[child]; ps; ps = ps->next)
1775                     {
1776                       node = INT_LIST_VAL (ps);
1777
1778                       /* See discussion above about nodes not marked as in
1779                          this loop during the initial DFS traversal.  */
1780                       if (node == ENTRY_BLOCK || node == EXIT_BLOCK
1781                           || max_hdr[node] != loop_head)
1782                         {
1783                           tail = -1;
1784                           break;
1785                         }
1786                       else if (!TEST_BIT (in_queue, node) && node != i)
1787                         {
1788                           queue[++tail] = node;
1789                           SET_BIT (in_queue, node);
1790
1791                           if (too_large (node, &num_bbs, &num_insns))
1792                             {
1793                               too_large_failure = 1;
1794                               break;
1795                             }
1796                         }
1797                     }
1798                 }
1799
1800               if (tail >= 0 && !too_large_failure)
1801                 {
1802                   /* Place the loop header into list of region blocks.  */
1803                   degree[i] = -1;
1804                   rgn_bb_table[idx] = i;
1805                   RGN_NR_BLOCKS (nr_regions) = num_bbs;
1806                   RGN_BLOCKS (nr_regions) = idx++;
1807                   CONTAINING_RGN (i) = nr_regions;
1808                   BLOCK_TO_BB (i) = count = 0;
1809
1810                   /* Remove blocks from queue[] when their in degree becomes
1811                  zero.  Repeat until no blocks are left on the list.  This
1812                  produces a topological list of blocks in the region.  */
1813                   while (tail >= 0)
1814                     {
1815                       int_list_ptr ps;
1816
1817                       if (head < 0)
1818                         head = tail;
1819                       child = queue[head];
1820                       if (degree[child] == 0)
1821                         {
1822                           degree[child] = -1;
1823                           rgn_bb_table[idx++] = child;
1824                           BLOCK_TO_BB (child) = ++count;
1825                           CONTAINING_RGN (child) = nr_regions;
1826                           queue[head] = queue[tail--];
1827
1828                           for (ps = s_succs[child]; ps; ps = ps->next)
1829                             if (INT_LIST_VAL (ps) != ENTRY_BLOCK
1830                                 && INT_LIST_VAL (ps) != EXIT_BLOCK)
1831                               --degree[INT_LIST_VAL (ps)];
1832                         }
1833                       else
1834                         --head;
1835                     }
1836                   ++nr_regions;
1837                 }
1838             }
1839         }
1840     }
1841
1842   /* Any block that did not end up in a region is placed into a region
1843      by itself.  */
1844   for (i = 0; i < n_basic_blocks; i++)
1845     if (degree[i] >= 0)
1846       {
1847         rgn_bb_table[idx] = i;
1848         RGN_NR_BLOCKS (nr_regions) = 1;
1849         RGN_BLOCKS (nr_regions) = idx++;
1850         CONTAINING_RGN (i) = nr_regions++;
1851         BLOCK_TO_BB (i) = 0;
1852       }
1853
1854   free (passed);
1855   free (header);
1856   free (inner);
1857   free (in_queue);
1858   free (in_stack);
1859 }
1860
1861
1862 /* functions for regions scheduling information */
1863
1864 /* Compute dominators, probability, and potential-split-edges of bb.
1865    Assume that these values were already computed for bb's predecessors.  */
1866
1867 static void
1868 compute_dom_prob_ps (bb)
1869      int bb;
1870 {
1871   int nxt_in_edge, fst_in_edge, pred;
1872   int fst_out_edge, nxt_out_edge, nr_out_edges, nr_rgn_out_edges;
1873
1874   prob[bb] = 0.0;
1875   if (IS_RGN_ENTRY (bb))
1876     {
1877       BITSET_ADD (dom[bb], 0, bbset_size);
1878       prob[bb] = 1.0;
1879       return;
1880     }
1881
1882   fst_in_edge = nxt_in_edge = IN_EDGES (BB_TO_BLOCK (bb));
1883
1884   /* intialize dom[bb] to '111..1' */
1885   BITSET_INVERT (dom[bb], bbset_size);
1886
1887   do
1888     {
1889       pred = FROM_BLOCK (nxt_in_edge);
1890       BITSET_INTER (dom[bb], dom[BLOCK_TO_BB (pred)], bbset_size);
1891
1892       BITSET_UNION (ancestor_edges[bb], ancestor_edges[BLOCK_TO_BB (pred)],
1893                     edgeset_size);
1894
1895       BITSET_ADD (ancestor_edges[bb], EDGE_TO_BIT (nxt_in_edge), edgeset_size);
1896
1897       nr_out_edges = 1;
1898       nr_rgn_out_edges = 0;
1899       fst_out_edge = OUT_EDGES (pred);
1900       nxt_out_edge = NEXT_OUT (fst_out_edge);
1901       BITSET_UNION (pot_split[bb], pot_split[BLOCK_TO_BB (pred)],
1902                     edgeset_size);
1903
1904       BITSET_ADD (pot_split[bb], EDGE_TO_BIT (fst_out_edge), edgeset_size);
1905
1906       /* the successor doesn't belong the region? */
1907       if (CONTAINING_RGN (TO_BLOCK (fst_out_edge)) !=
1908           CONTAINING_RGN (BB_TO_BLOCK (bb)))
1909         ++nr_rgn_out_edges;
1910
1911       while (fst_out_edge != nxt_out_edge)
1912         {
1913           ++nr_out_edges;
1914           /* the successor doesn't belong the region? */
1915           if (CONTAINING_RGN (TO_BLOCK (nxt_out_edge)) !=
1916               CONTAINING_RGN (BB_TO_BLOCK (bb)))
1917             ++nr_rgn_out_edges;
1918           BITSET_ADD (pot_split[bb], EDGE_TO_BIT (nxt_out_edge), edgeset_size);
1919           nxt_out_edge = NEXT_OUT (nxt_out_edge);
1920
1921         }
1922
1923       /* now nr_rgn_out_edges is the number of region-exit edges from pred,
1924          and nr_out_edges will be the number of pred out edges not leaving
1925          the region.  */
1926       nr_out_edges -= nr_rgn_out_edges;
1927       if (nr_rgn_out_edges > 0)
1928         prob[bb] += 0.9 * prob[BLOCK_TO_BB (pred)] / nr_out_edges;
1929       else
1930         prob[bb] += prob[BLOCK_TO_BB (pred)] / nr_out_edges;
1931       nxt_in_edge = NEXT_IN (nxt_in_edge);
1932     }
1933   while (fst_in_edge != nxt_in_edge);
1934
1935   BITSET_ADD (dom[bb], bb, bbset_size);
1936   BITSET_DIFFER (pot_split[bb], ancestor_edges[bb], edgeset_size);
1937
1938   if (sched_verbose >= 2)
1939     fprintf (dump, ";;  bb_prob(%d, %d) = %3d\n", bb, BB_TO_BLOCK (bb), (int) (100.0 * prob[bb]));
1940 }                               /* compute_dom_prob_ps */
1941
1942 /* functions for target info */
1943
1944 /* Compute in BL the list of split-edges of bb_src relatively to bb_trg.
1945    Note that bb_trg dominates bb_src.  */
1946
1947 static void
1948 split_edges (bb_src, bb_trg, bl)
1949      int bb_src;
1950      int bb_trg;
1951      edgelst *bl;
1952 {
1953   int es = edgeset_size;
1954   edgeset src = (edgeset) alloca (es * sizeof (HOST_WIDE_INT));
1955
1956   while (es--)
1957     src[es] = (pot_split[bb_src])[es];
1958   BITSET_DIFFER (src, pot_split[bb_trg], edgeset_size);
1959   extract_bitlst (src, edgeset_size, bl);
1960 }
1961
1962
1963 /* Find the valid candidate-source-blocks for the target block TRG, compute
1964    their probability, and check if they are speculative or not.
1965    For speculative sources, compute their update-blocks and split-blocks.  */
1966
1967 static void
1968 compute_trg_info (trg)
1969      int trg;
1970 {
1971   register candidate *sp;
1972   edgelst el;
1973   int check_block, update_idx;
1974   int i, j, k, fst_edge, nxt_edge;
1975
1976   /* define some of the fields for the target bb as well */
1977   sp = candidate_table + trg;
1978   sp->is_valid = 1;
1979   sp->is_speculative = 0;
1980   sp->src_prob = 100;
1981
1982   for (i = trg + 1; i < current_nr_blocks; i++)
1983     {
1984       sp = candidate_table + i;
1985
1986       sp->is_valid = IS_DOMINATED (i, trg);
1987       if (sp->is_valid)
1988         {
1989           sp->src_prob = GET_SRC_PROB (i, trg);
1990           sp->is_valid = (sp->src_prob >= MIN_PROBABILITY);
1991         }
1992
1993       if (sp->is_valid)
1994         {
1995           split_edges (i, trg, &el);
1996           sp->is_speculative = (el.nr_members) ? 1 : 0;
1997           if (sp->is_speculative && !flag_schedule_speculative)
1998             sp->is_valid = 0;
1999         }
2000
2001       if (sp->is_valid)
2002         {
2003           sp->split_bbs.first_member = &bblst_table[bblst_last];
2004           sp->split_bbs.nr_members = el.nr_members;
2005           for (j = 0; j < el.nr_members; bblst_last++, j++)
2006             bblst_table[bblst_last] =
2007               TO_BLOCK (rgn_edges[el.first_member[j]]);
2008           sp->update_bbs.first_member = &bblst_table[bblst_last];
2009           update_idx = 0;
2010           for (j = 0; j < el.nr_members; j++)
2011             {
2012               check_block = FROM_BLOCK (rgn_edges[el.first_member[j]]);
2013               fst_edge = nxt_edge = OUT_EDGES (check_block);
2014               do
2015                 {
2016                   for (k = 0; k < el.nr_members; k++)
2017                     if (EDGE_TO_BIT (nxt_edge) == el.first_member[k])
2018                       break;
2019
2020                   if (k >= el.nr_members)
2021                     {
2022                       bblst_table[bblst_last++] = TO_BLOCK (nxt_edge);
2023                       update_idx++;
2024                     }
2025
2026                   nxt_edge = NEXT_OUT (nxt_edge);
2027                 }
2028               while (fst_edge != nxt_edge);
2029             }
2030           sp->update_bbs.nr_members = update_idx;
2031
2032         }
2033       else
2034         {
2035           sp->split_bbs.nr_members = sp->update_bbs.nr_members = 0;
2036
2037           sp->is_speculative = 0;
2038           sp->src_prob = 0;
2039         }
2040     }
2041 }                               /* compute_trg_info */
2042
2043
2044 /* Print candidates info, for debugging purposes.  Callable from debugger.  */
2045
2046 void
2047 debug_candidate (i)
2048      int i;
2049 {
2050   if (!candidate_table[i].is_valid)
2051     return;
2052
2053   if (candidate_table[i].is_speculative)
2054     {
2055       int j;
2056       fprintf (dump, "src b %d bb %d speculative \n", BB_TO_BLOCK (i), i);
2057
2058       fprintf (dump, "split path: ");
2059       for (j = 0; j < candidate_table[i].split_bbs.nr_members; j++)
2060         {
2061           int b = candidate_table[i].split_bbs.first_member[j];
2062
2063           fprintf (dump, " %d ", b);
2064         }
2065       fprintf (dump, "\n");
2066
2067       fprintf (dump, "update path: ");
2068       for (j = 0; j < candidate_table[i].update_bbs.nr_members; j++)
2069         {
2070           int b = candidate_table[i].update_bbs.first_member[j];
2071
2072           fprintf (dump, " %d ", b);
2073         }
2074       fprintf (dump, "\n");
2075     }
2076   else
2077     {
2078       fprintf (dump, " src %d equivalent\n", BB_TO_BLOCK (i));
2079     }
2080 }
2081
2082
2083 /* Print candidates info, for debugging purposes.  Callable from debugger.  */
2084
2085 void
2086 debug_candidates (trg)
2087      int trg;
2088 {
2089   int i;
2090
2091   fprintf (dump, "----------- candidate table: target: b=%d bb=%d ---\n",
2092            BB_TO_BLOCK (trg), trg);
2093   for (i = trg + 1; i < current_nr_blocks; i++)
2094     debug_candidate (i);
2095 }
2096
2097
2098 /* functions for speculative scheduing */
2099
2100 /* Return 0 if x is a set of a register alive in the beginning of one
2101    of the split-blocks of src, otherwise return 1.  */
2102
2103 static int
2104 check_live_1 (src, x)
2105      int src;
2106      rtx x;
2107 {
2108   register int i;
2109   register int regno;
2110   register rtx reg = SET_DEST (x);
2111
2112   if (reg == 0)
2113     return 1;
2114
2115   while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
2116          || GET_CODE (reg) == SIGN_EXTRACT
2117          || GET_CODE (reg) == STRICT_LOW_PART)
2118     reg = XEXP (reg, 0);
2119
2120   if (GET_CODE (reg) != REG)
2121     return 1;
2122
2123   regno = REGNO (reg);
2124
2125   if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2126     {
2127       /* Global registers are assumed live */
2128       return 0;
2129     }
2130   else
2131     {
2132       if (regno < FIRST_PSEUDO_REGISTER)
2133         {
2134           /* check for hard registers */
2135           int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
2136           while (--j >= 0)
2137             {
2138               for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
2139                 {
2140                   int b = candidate_table[src].split_bbs.first_member[i];
2141
2142                   if (REGNO_REG_SET_P (basic_block_live_at_start[b], regno + j))
2143                     {
2144                       return 0;
2145                     }
2146                 }
2147             }
2148         }
2149       else
2150         {
2151           /* check for psuedo registers */
2152           for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
2153             {
2154               int b = candidate_table[src].split_bbs.first_member[i];
2155
2156               if (REGNO_REG_SET_P (basic_block_live_at_start[b], regno))
2157                 {
2158                   return 0;
2159                 }
2160             }
2161         }
2162     }
2163
2164   return 1;
2165 }
2166
2167
2168 /* If x is a set of a register R, mark that R is alive in the beginning
2169    of every update-block of src.  */
2170
2171 static void
2172 update_live_1 (src, x)
2173      int src;
2174      rtx x;
2175 {
2176   register int i;
2177   register int regno;
2178   register rtx reg = SET_DEST (x);
2179
2180   if (reg == 0)
2181     return;
2182
2183   while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
2184          || GET_CODE (reg) == SIGN_EXTRACT
2185          || GET_CODE (reg) == STRICT_LOW_PART)
2186     reg = XEXP (reg, 0);
2187
2188   if (GET_CODE (reg) != REG)
2189     return;
2190
2191   /* Global registers are always live, so the code below does not apply
2192      to them.  */
2193
2194   regno = REGNO (reg);
2195
2196   if (regno >= FIRST_PSEUDO_REGISTER || !global_regs[regno])
2197     {
2198       if (regno < FIRST_PSEUDO_REGISTER)
2199         {
2200           int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
2201           while (--j >= 0)
2202             {
2203               for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
2204                 {
2205                   int b = candidate_table[src].update_bbs.first_member[i];
2206
2207                   SET_REGNO_REG_SET (basic_block_live_at_start[b], regno + j);
2208                 }
2209             }
2210         }
2211       else
2212         {
2213           for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
2214             {
2215               int b = candidate_table[src].update_bbs.first_member[i];
2216
2217               SET_REGNO_REG_SET (basic_block_live_at_start[b], regno);
2218             }
2219         }
2220     }
2221 }
2222
2223
2224 /* Return 1 if insn can be speculatively moved from block src to trg,
2225    otherwise return 0.  Called before first insertion of insn to
2226    ready-list or before the scheduling.  */
2227
2228 static int
2229 check_live (insn, src)
2230      rtx insn;
2231      int src;
2232 {
2233   /* find the registers set by instruction */
2234   if (GET_CODE (PATTERN (insn)) == SET
2235       || GET_CODE (PATTERN (insn)) == CLOBBER)
2236     return check_live_1 (src, PATTERN (insn));
2237   else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2238     {
2239       int j;
2240       for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
2241         if ((GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
2242              || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
2243             && !check_live_1 (src, XVECEXP (PATTERN (insn), 0, j)))
2244           return 0;
2245
2246       return 1;
2247     }
2248
2249   return 1;
2250 }
2251
2252
2253 /* Update the live registers info after insn was moved speculatively from
2254    block src to trg.  */
2255
2256 static void
2257 update_live (insn, src)
2258      rtx insn;
2259      int src;
2260 {
2261   /* find the registers set by instruction */
2262   if (GET_CODE (PATTERN (insn)) == SET
2263       || GET_CODE (PATTERN (insn)) == CLOBBER)
2264     update_live_1 (src, PATTERN (insn));
2265   else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2266     {
2267       int j;
2268       for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
2269         if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
2270             || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
2271           update_live_1 (src, XVECEXP (PATTERN (insn), 0, j));
2272     }
2273 }
2274
2275 /* Exception Free Loads:
2276
2277    We define five classes of speculative loads: IFREE, IRISKY,
2278    PFREE, PRISKY, and MFREE.
2279
2280    IFREE loads are loads that are proved to be exception-free, just
2281    by examining the load insn.  Examples for such loads are loads
2282    from TOC and loads of global data.
2283
2284    IRISKY loads are loads that are proved to be exception-risky,
2285    just by examining the load insn.  Examples for such loads are
2286    volatile loads and loads from shared memory.
2287
2288    PFREE loads are loads for which we can prove, by examining other
2289    insns, that they are exception-free.  Currently, this class consists
2290    of loads for which we are able to find a "similar load", either in
2291    the target block, or, if only one split-block exists, in that split
2292    block.  Load2 is similar to load1 if both have same single base
2293    register.  We identify only part of the similar loads, by finding
2294    an insn upon which both load1 and load2 have a DEF-USE dependence.
2295
2296    PRISKY loads are loads for which we can prove, by examining other
2297    insns, that they are exception-risky.  Currently we have two proofs for
2298    such loads.  The first proof detects loads that are probably guarded by a
2299    test on the memory address.  This proof is based on the
2300    backward and forward data dependence information for the region.
2301    Let load-insn be the examined load.
2302    Load-insn is PRISKY iff ALL the following hold:
2303
2304    - insn1 is not in the same block as load-insn
2305    - there is a DEF-USE dependence chain (insn1, ..., load-insn)
2306    - test-insn is either a compare or a branch, not in the same block as load-insn
2307    - load-insn is reachable from test-insn
2308    - there is a DEF-USE dependence chain (insn1, ..., test-insn)
2309
2310    This proof might fail when the compare and the load are fed
2311    by an insn not in the region.  To solve this, we will add to this
2312    group all loads that have no input DEF-USE dependence.
2313
2314    The second proof detects loads that are directly or indirectly
2315    fed by a speculative load.  This proof is affected by the
2316    scheduling process.  We will use the flag  fed_by_spec_load.
2317    Initially, all insns have this flag reset.  After a speculative
2318    motion of an insn, if insn is either a load, or marked as
2319    fed_by_spec_load, we will also mark as fed_by_spec_load every
2320    insn1 for which a DEF-USE dependence (insn, insn1) exists.  A
2321    load which is fed_by_spec_load is also PRISKY.
2322
2323    MFREE (maybe-free) loads are all the remaining loads. They may be
2324    exception-free, but we cannot prove it.
2325
2326    Now, all loads in IFREE and PFREE classes are considered
2327    exception-free, while all loads in IRISKY and PRISKY classes are
2328    considered exception-risky.  As for loads in the MFREE class,
2329    these are considered either exception-free or exception-risky,
2330    depending on whether we are pessimistic or optimistic.  We have
2331    to take the pessimistic approach to assure the safety of
2332    speculative scheduling, but we can take the optimistic approach
2333    by invoking the -fsched_spec_load_dangerous option.  */
2334
2335 enum INSN_TRAP_CLASS
2336 {
2337   TRAP_FREE = 0, IFREE = 1, PFREE_CANDIDATE = 2,
2338   PRISKY_CANDIDATE = 3, IRISKY = 4, TRAP_RISKY = 5
2339 };
2340
2341 #define WORST_CLASS(class1, class2) \
2342 ((class1 > class2) ? class1 : class2)
2343
2344 /* Indexed by INSN_UID, and set if there's DEF-USE dependence between */
2345 /* some speculatively moved load insn and this one.  */
2346 char *fed_by_spec_load;
2347 char *is_load_insn;
2348
2349 /* Non-zero if block bb_to is equal to, or reachable from block bb_from.  */
2350 #define IS_REACHABLE(bb_from, bb_to)                                    \
2351 (bb_from == bb_to                                                       \
2352    || IS_RGN_ENTRY (bb_from)                                            \
2353    || (bitset_member (ancestor_edges[bb_to],                            \
2354                       EDGE_TO_BIT (IN_EDGES (BB_TO_BLOCK (bb_from))),   \
2355                       edgeset_size)))
2356 #define FED_BY_SPEC_LOAD(insn) (fed_by_spec_load[INSN_UID (insn)])
2357 #define IS_LOAD_INSN(insn) (is_load_insn[INSN_UID (insn)])
2358
2359 /* Non-zero iff the address is comprised from at most 1 register */
2360 #define CONST_BASED_ADDRESS_P(x)                        \
2361   (GET_CODE (x) == REG                                  \
2362    || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS   \
2363         || (GET_CODE (x) == LO_SUM))                    \
2364        && (GET_CODE (XEXP (x, 0)) == CONST_INT          \
2365            || GET_CODE (XEXP (x, 1)) == CONST_INT)))
2366
2367 /* Turns on the fed_by_spec_load flag for insns fed by load_insn.  */
2368
2369 static void
2370 set_spec_fed (load_insn)
2371      rtx load_insn;
2372 {
2373   rtx link;
2374
2375   for (link = INSN_DEPEND (load_insn); link; link = XEXP (link, 1))
2376     if (GET_MODE (link) == VOIDmode)
2377       FED_BY_SPEC_LOAD (XEXP (link, 0)) = 1;
2378 }                               /* set_spec_fed */
2379
2380 /* On the path from the insn to load_insn_bb, find a conditional branch */
2381 /* depending on insn, that guards the speculative load.  */
2382
2383 static int
2384 find_conditional_protection (insn, load_insn_bb)
2385      rtx insn;
2386      int load_insn_bb;
2387 {
2388   rtx link;
2389
2390   /* iterate through DEF-USE forward dependences */
2391   for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
2392     {
2393       rtx next = XEXP (link, 0);
2394       if ((CONTAINING_RGN (INSN_BLOCK (next)) ==
2395            CONTAINING_RGN (BB_TO_BLOCK (load_insn_bb)))
2396           && IS_REACHABLE (INSN_BB (next), load_insn_bb)
2397           && load_insn_bb != INSN_BB (next)
2398           && GET_MODE (link) == VOIDmode
2399           && (GET_CODE (next) == JUMP_INSN
2400               || find_conditional_protection (next, load_insn_bb)))
2401         return 1;
2402     }
2403   return 0;
2404 }                               /* find_conditional_protection */
2405
2406 /* Returns 1 if the same insn1 that participates in the computation
2407    of load_insn's address is feeding a conditional branch that is
2408    guarding on load_insn. This is true if we find a the two DEF-USE
2409    chains:
2410    insn1 -> ... -> conditional-branch
2411    insn1 -> ... -> load_insn,
2412    and if a flow path exist:
2413    insn1 -> ... -> conditional-branch -> ... -> load_insn,
2414    and if insn1 is on the path
2415    region-entry -> ... -> bb_trg -> ... load_insn.
2416
2417    Locate insn1 by climbing on LOG_LINKS from load_insn.
2418    Locate the branch by following INSN_DEPEND from insn1.  */
2419
2420 static int
2421 is_conditionally_protected (load_insn, bb_src, bb_trg)
2422      rtx load_insn;
2423      int bb_src, bb_trg;
2424 {
2425   rtx link;
2426
2427   for (link = LOG_LINKS (load_insn); link; link = XEXP (link, 1))
2428     {
2429       rtx insn1 = XEXP (link, 0);
2430
2431       /* must be a DEF-USE dependence upon non-branch */
2432       if (GET_MODE (link) != VOIDmode
2433           || GET_CODE (insn1) == JUMP_INSN)
2434         continue;
2435
2436       /* must exist a path: region-entry -> ... -> bb_trg -> ... load_insn */
2437       if (INSN_BB (insn1) == bb_src
2438           || (CONTAINING_RGN (INSN_BLOCK (insn1))
2439               != CONTAINING_RGN (BB_TO_BLOCK (bb_src)))
2440           || (!IS_REACHABLE (bb_trg, INSN_BB (insn1))
2441               && !IS_REACHABLE (INSN_BB (insn1), bb_trg)))
2442         continue;
2443
2444       /* now search for the conditional-branch */
2445       if (find_conditional_protection (insn1, bb_src))
2446         return 1;
2447
2448       /* recursive step: search another insn1, "above" current insn1.  */
2449       return is_conditionally_protected (insn1, bb_src, bb_trg);
2450     }
2451
2452   /* the chain does not exsist */
2453   return 0;
2454 }                               /* is_conditionally_protected */
2455
2456 /* Returns 1 if a clue for "similar load" 'insn2' is found, and hence
2457    load_insn can move speculatively from bb_src to bb_trg.  All the
2458    following must hold:
2459
2460    (1) both loads have 1 base register (PFREE_CANDIDATEs).
2461    (2) load_insn and load1 have a def-use dependence upon
2462    the same insn 'insn1'.
2463    (3) either load2 is in bb_trg, or:
2464    - there's only one split-block, and
2465    - load1 is on the escape path, and
2466
2467    From all these we can conclude that the two loads access memory
2468    addresses that differ at most by a constant, and hence if moving
2469    load_insn would cause an exception, it would have been caused by
2470    load2 anyhow.  */
2471
2472 static int
2473 is_pfree (load_insn, bb_src, bb_trg)
2474      rtx load_insn;
2475      int bb_src, bb_trg;
2476 {
2477   rtx back_link;
2478   register candidate *candp = candidate_table + bb_src;
2479
2480   if (candp->split_bbs.nr_members != 1)
2481     /* must have exactly one escape block */
2482     return 0;
2483
2484   for (back_link = LOG_LINKS (load_insn);
2485        back_link; back_link = XEXP (back_link, 1))
2486     {
2487       rtx insn1 = XEXP (back_link, 0);
2488
2489       if (GET_MODE (back_link) == VOIDmode)
2490         {
2491           /* found a DEF-USE dependence (insn1, load_insn) */
2492           rtx fore_link;
2493
2494           for (fore_link = INSN_DEPEND (insn1);
2495                fore_link; fore_link = XEXP (fore_link, 1))
2496             {
2497               rtx insn2 = XEXP (fore_link, 0);
2498               if (GET_MODE (fore_link) == VOIDmode)
2499                 {
2500                   /* found a DEF-USE dependence (insn1, insn2) */
2501                   if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
2502                     /* insn2 not guaranteed to be a 1 base reg load */
2503                     continue;
2504
2505                   if (INSN_BB (insn2) == bb_trg)
2506                     /* insn2 is the similar load, in the target block */
2507                     return 1;
2508
2509                   if (*(candp->split_bbs.first_member) == INSN_BLOCK (insn2))
2510                     /* insn2 is a similar load, in a split-block */
2511                     return 1;
2512                 }
2513             }
2514         }
2515     }
2516
2517   /* couldn't find a similar load */
2518   return 0;
2519 }                               /* is_pfree */
2520
2521 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
2522    as found by analyzing insn's expression.  */
2523
2524 static int
2525 may_trap_exp (x, is_store)
2526      rtx x;
2527      int is_store;
2528 {
2529   enum rtx_code code;
2530
2531   if (x == 0)
2532     return TRAP_FREE;
2533   code = GET_CODE (x);
2534   if (is_store)
2535     {
2536       if (code == MEM)
2537         return TRAP_RISKY;
2538       else
2539         return TRAP_FREE;
2540     }
2541   if (code == MEM)
2542     {
2543       /* The insn uses memory */
2544       /* a volatile load */
2545       if (MEM_VOLATILE_P (x))
2546         return IRISKY;
2547       /* an exception-free load */
2548       if (!may_trap_p (x))
2549         return IFREE;
2550       /* a load with 1 base register, to be further checked */
2551       if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
2552         return PFREE_CANDIDATE;
2553       /* no info on the load, to be further checked */
2554       return PRISKY_CANDIDATE;
2555     }
2556   else
2557     {
2558       char *fmt;
2559       int i, insn_class = TRAP_FREE;
2560
2561       /* neither store nor load, check if it may cause a trap */
2562       if (may_trap_p (x))
2563         return TRAP_RISKY;
2564       /* recursive step: walk the insn...  */
2565       fmt = GET_RTX_FORMAT (code);
2566       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2567         {
2568           if (fmt[i] == 'e')
2569             {
2570               int tmp_class = may_trap_exp (XEXP (x, i), is_store);
2571               insn_class = WORST_CLASS (insn_class, tmp_class);
2572             }
2573           else if (fmt[i] == 'E')
2574             {
2575               int j;
2576               for (j = 0; j < XVECLEN (x, i); j++)
2577                 {
2578                   int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
2579                   insn_class = WORST_CLASS (insn_class, tmp_class);
2580                   if (insn_class == TRAP_RISKY || insn_class == IRISKY)
2581                     break;
2582                 }
2583             }
2584           if (insn_class == TRAP_RISKY || insn_class == IRISKY)
2585             break;
2586         }
2587       return insn_class;
2588     }
2589 }                               /* may_trap_exp */
2590
2591
2592 /* Classifies insn for the purpose of verifying that it can be
2593    moved speculatively, by examining it's patterns, returning:
2594    TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
2595    TRAP_FREE: non-load insn.
2596    IFREE: load from a globaly safe location.
2597    IRISKY: volatile load.
2598    PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
2599    being either PFREE or PRISKY.  */
2600
2601 static int
2602 haifa_classify_insn (insn)
2603      rtx insn;
2604 {
2605   rtx pat = PATTERN (insn);
2606   int tmp_class = TRAP_FREE;
2607   int insn_class = TRAP_FREE;
2608   enum rtx_code code;
2609
2610   if (GET_CODE (pat) == PARALLEL)
2611     {
2612       int i, len = XVECLEN (pat, 0);
2613
2614       for (i = len - 1; i >= 0; i--)
2615         {
2616           code = GET_CODE (XVECEXP (pat, 0, i));
2617           switch (code)
2618             {
2619             case CLOBBER:
2620               /* test if it is a 'store' */
2621               tmp_class = may_trap_exp (XEXP (XVECEXP (pat, 0, i), 0), 1);
2622               break;
2623             case SET:
2624               /* test if it is a store */
2625               tmp_class = may_trap_exp (SET_DEST (XVECEXP (pat, 0, i)), 1);
2626               if (tmp_class == TRAP_RISKY)
2627                 break;
2628               /* test if it is a load  */
2629               tmp_class =
2630                 WORST_CLASS (tmp_class,
2631                            may_trap_exp (SET_SRC (XVECEXP (pat, 0, i)), 0));
2632             default:;
2633             }
2634           insn_class = WORST_CLASS (insn_class, tmp_class);
2635           if (insn_class == TRAP_RISKY || insn_class == IRISKY)
2636             break;
2637         }
2638     }
2639   else
2640     {
2641       code = GET_CODE (pat);
2642       switch (code)
2643         {
2644         case CLOBBER:
2645           /* test if it is a 'store' */
2646           tmp_class = may_trap_exp (XEXP (pat, 0), 1);
2647           break;
2648         case SET:
2649           /* test if it is a store */
2650           tmp_class = may_trap_exp (SET_DEST (pat), 1);
2651           if (tmp_class == TRAP_RISKY)
2652             break;
2653           /* test if it is a load  */
2654           tmp_class =
2655             WORST_CLASS (tmp_class,
2656                          may_trap_exp (SET_SRC (pat), 0));
2657         default:;
2658         }
2659       insn_class = tmp_class;
2660     }
2661
2662   return insn_class;
2663
2664 }                               /* haifa_classify_insn */
2665
2666 /* Return 1 if load_insn is prisky (i.e. if load_insn is fed by
2667    a load moved speculatively, or if load_insn is protected by
2668    a compare on load_insn's address).  */
2669
2670 static int
2671 is_prisky (load_insn, bb_src, bb_trg)
2672      rtx load_insn;
2673      int bb_src, bb_trg;
2674 {
2675   if (FED_BY_SPEC_LOAD (load_insn))
2676     return 1;
2677
2678   if (LOG_LINKS (load_insn) == NULL)
2679     /* dependence may 'hide' out of the region.  */
2680     return 1;
2681
2682   if (is_conditionally_protected (load_insn, bb_src, bb_trg))
2683     return 1;
2684
2685   return 0;
2686 }                               /* is_prisky */
2687
2688 /* Insn is a candidate to be moved speculatively from bb_src to bb_trg.
2689    Return 1 if insn is exception-free (and the motion is valid)
2690    and 0 otherwise.  */
2691
2692 static int
2693 is_exception_free (insn, bb_src, bb_trg)
2694      rtx insn;
2695      int bb_src, bb_trg;
2696 {
2697   int insn_class = haifa_classify_insn (insn);
2698
2699   /* handle non-load insns */
2700   switch (insn_class)
2701     {
2702     case TRAP_FREE:
2703       return 1;
2704     case TRAP_RISKY:
2705       return 0;
2706     default:;
2707     }
2708
2709   /* handle loads */
2710   if (!flag_schedule_speculative_load)
2711     return 0;
2712   IS_LOAD_INSN (insn) = 1;
2713   switch (insn_class)
2714     {
2715     case IFREE:
2716       return (1);
2717     case IRISKY:
2718       return 0;
2719     case PFREE_CANDIDATE:
2720       if (is_pfree (insn, bb_src, bb_trg))
2721         return 1;
2722       /* don't 'break' here: PFREE-candidate is also PRISKY-candidate */
2723     case PRISKY_CANDIDATE:
2724       if (!flag_schedule_speculative_load_dangerous
2725           || is_prisky (insn, bb_src, bb_trg))
2726         return 0;
2727       break;
2728     default:;
2729     }
2730
2731   return flag_schedule_speculative_load_dangerous;
2732 }                               /* is_exception_free */
2733
2734
2735 /* Process an insn's memory dependencies.  There are four kinds of
2736    dependencies:
2737
2738    (0) read dependence: read follows read
2739    (1) true dependence: read follows write
2740    (2) anti dependence: write follows read
2741    (3) output dependence: write follows write
2742
2743    We are careful to build only dependencies which actually exist, and
2744    use transitivity to avoid building too many links.  */
2745 \f
2746 /* Return the INSN_LIST containing INSN in LIST, or NULL
2747    if LIST does not contain INSN.  */
2748
2749 HAIFA_INLINE static rtx
2750 find_insn_list (insn, list)
2751      rtx insn;
2752      rtx list;
2753 {
2754   while (list)
2755     {
2756       if (XEXP (list, 0) == insn)
2757         return list;
2758       list = XEXP (list, 1);
2759     }
2760   return 0;
2761 }
2762
2763
2764 /* Return 1 if the pair (insn, x) is found in (LIST, LIST1), or 0 otherwise.  */
2765
2766 HAIFA_INLINE static char
2767 find_insn_mem_list (insn, x, list, list1)
2768      rtx insn, x;
2769      rtx list, list1;
2770 {
2771   while (list)
2772     {
2773       if (XEXP (list, 0) == insn
2774           && XEXP (list1, 0) == x)
2775         return 1;
2776       list = XEXP (list, 1);
2777       list1 = XEXP (list1, 1);
2778     }
2779   return 0;
2780 }
2781
2782
2783 /* Compute the function units used by INSN.  This caches the value
2784    returned by function_units_used.  A function unit is encoded as the
2785    unit number if the value is non-negative and the compliment of a
2786    mask if the value is negative.  A function unit index is the
2787    non-negative encoding.  */
2788
2789 HAIFA_INLINE static int
2790 insn_unit (insn)
2791      rtx insn;
2792 {
2793   register int unit = INSN_UNIT (insn);
2794
2795   if (unit == 0)
2796     {
2797       recog_memoized (insn);
2798
2799       /* A USE insn, or something else we don't need to understand.
2800          We can't pass these directly to function_units_used because it will
2801          trigger a fatal error for unrecognizable insns.  */
2802       if (INSN_CODE (insn) < 0)
2803         unit = -1;
2804       else
2805         {
2806           unit = function_units_used (insn);
2807           /* Increment non-negative values so we can cache zero.  */
2808           if (unit >= 0)
2809             unit++;
2810         }
2811       /* We only cache 16 bits of the result, so if the value is out of
2812          range, don't cache it.  */
2813       if (FUNCTION_UNITS_SIZE < HOST_BITS_PER_SHORT
2814           || unit >= 0
2815           || (~unit & ((1 << (HOST_BITS_PER_SHORT - 1)) - 1)) == 0)
2816         INSN_UNIT (insn) = unit;
2817     }
2818   return (unit > 0 ? unit - 1 : unit);
2819 }
2820
2821 /* Compute the blockage range for executing INSN on UNIT.  This caches
2822    the value returned by the blockage_range_function for the unit.
2823    These values are encoded in an int where the upper half gives the
2824    minimum value and the lower half gives the maximum value.  */
2825
2826 HAIFA_INLINE static unsigned int
2827 blockage_range (unit, insn)
2828      int unit;
2829      rtx insn;
2830 {
2831   unsigned int blockage = INSN_BLOCKAGE (insn);
2832   unsigned int range;
2833
2834   if (UNIT_BLOCKED (blockage) != unit + 1)
2835     {
2836       range = function_units[unit].blockage_range_function (insn);
2837       /* We only cache the blockage range for one unit and then only if
2838          the values fit.  */
2839       if (HOST_BITS_PER_INT >= UNIT_BITS + 2 * BLOCKAGE_BITS)
2840         INSN_BLOCKAGE (insn) = ENCODE_BLOCKAGE (unit + 1, range);
2841     }
2842   else
2843     range = BLOCKAGE_RANGE (blockage);
2844
2845   return range;
2846 }
2847
2848 /* A vector indexed by function unit instance giving the last insn to use
2849    the unit.  The value of the function unit instance index for unit U
2850    instance I is (U + I * FUNCTION_UNITS_SIZE).  */
2851 static rtx unit_last_insn[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
2852
2853 /* A vector indexed by function unit instance giving the minimum time when
2854    the unit will unblock based on the maximum blockage cost.  */
2855 static int unit_tick[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
2856
2857 /* A vector indexed by function unit number giving the number of insns
2858    that remain to use the unit.  */
2859 static int unit_n_insns[FUNCTION_UNITS_SIZE];
2860
2861 /* Reset the function unit state to the null state.  */
2862
2863 static void
2864 clear_units ()
2865 {
2866   bzero ((char *) unit_last_insn, sizeof (unit_last_insn));
2867   bzero ((char *) unit_tick, sizeof (unit_tick));
2868   bzero ((char *) unit_n_insns, sizeof (unit_n_insns));
2869 }
2870
2871 /* Return the issue-delay of an insn */
2872
2873 HAIFA_INLINE static int
2874 insn_issue_delay (insn)
2875      rtx insn;
2876 {
2877   int i, delay = 0;
2878   int unit = insn_unit (insn);
2879
2880   /* efficiency note: in fact, we are working 'hard' to compute a
2881      value that was available in md file, and is not available in
2882      function_units[] structure.  It would be nice to have this
2883      value there, too.  */
2884   if (unit >= 0)
2885     {
2886       if (function_units[unit].blockage_range_function &&
2887           function_units[unit].blockage_function)
2888         delay = function_units[unit].blockage_function (insn, insn);
2889     }
2890   else
2891     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
2892       if ((unit & 1) != 0 && function_units[i].blockage_range_function
2893           && function_units[i].blockage_function)
2894         delay = MAX (delay, function_units[i].blockage_function (insn, insn));
2895
2896   return delay;
2897 }
2898
2899 /* Return the actual hazard cost of executing INSN on the unit UNIT,
2900    instance INSTANCE at time CLOCK if the previous actual hazard cost
2901    was COST.  */
2902
2903 HAIFA_INLINE static int
2904 actual_hazard_this_instance (unit, instance, insn, clock, cost)
2905      int unit, instance, clock, cost;
2906      rtx insn;
2907 {
2908   int tick = unit_tick[instance];       /* issue time of the last issued insn */
2909
2910   if (tick - clock > cost)
2911     {
2912       /* The scheduler is operating forward, so unit's last insn is the
2913          executing insn and INSN is the candidate insn.  We want a
2914          more exact measure of the blockage if we execute INSN at CLOCK
2915          given when we committed the execution of the unit's last insn.
2916
2917          The blockage value is given by either the unit's max blockage
2918          constant, blockage range function, or blockage function.  Use
2919          the most exact form for the given unit.  */
2920
2921       if (function_units[unit].blockage_range_function)
2922         {
2923           if (function_units[unit].blockage_function)
2924             tick += (function_units[unit].blockage_function
2925                      (unit_last_insn[instance], insn)
2926                      - function_units[unit].max_blockage);
2927           else
2928             tick += ((int) MAX_BLOCKAGE_COST (blockage_range (unit, insn))
2929                      - function_units[unit].max_blockage);
2930         }
2931       if (tick - clock > cost)
2932         cost = tick - clock;
2933     }
2934   return cost;
2935 }
2936
2937 /* Record INSN as having begun execution on the units encoded by UNIT at
2938    time CLOCK.  */
2939
2940 HAIFA_INLINE static void
2941 schedule_unit (unit, insn, clock)
2942      int unit, clock;
2943      rtx insn;
2944 {
2945   int i;
2946
2947   if (unit >= 0)
2948     {
2949       int instance = unit;
2950 #if MAX_MULTIPLICITY > 1
2951       /* Find the first free instance of the function unit and use that
2952          one.  We assume that one is free.  */
2953       for (i = function_units[unit].multiplicity - 1; i > 0; i--)
2954         {
2955           if (!actual_hazard_this_instance (unit, instance, insn, clock, 0))
2956             break;
2957           instance += FUNCTION_UNITS_SIZE;
2958         }
2959 #endif
2960       unit_last_insn[instance] = insn;
2961       unit_tick[instance] = (clock + function_units[unit].max_blockage);
2962     }
2963   else
2964     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
2965       if ((unit & 1) != 0)
2966         schedule_unit (i, insn, clock);
2967 }
2968
2969 /* Return the actual hazard cost of executing INSN on the units encoded by
2970    UNIT at time CLOCK if the previous actual hazard cost was COST.  */
2971
2972 HAIFA_INLINE static int
2973 actual_hazard (unit, insn, clock, cost)
2974      int unit, clock, cost;
2975      rtx insn;
2976 {
2977   int i;
2978
2979   if (unit >= 0)
2980     {
2981       /* Find the instance of the function unit with the minimum hazard.  */
2982       int instance = unit;
2983       int best_cost = actual_hazard_this_instance (unit, instance, insn,
2984                                                    clock, cost);
2985       int this_cost;
2986
2987 #if MAX_MULTIPLICITY > 1
2988       if (best_cost > cost)
2989         {
2990           for (i = function_units[unit].multiplicity - 1; i > 0; i--)
2991             {
2992               instance += FUNCTION_UNITS_SIZE;
2993               this_cost = actual_hazard_this_instance (unit, instance, insn,
2994                                                        clock, cost);
2995               if (this_cost < best_cost)
2996                 {
2997                   best_cost = this_cost;
2998                   if (this_cost <= cost)
2999                     break;
3000                 }
3001             }
3002         }
3003 #endif
3004       cost = MAX (cost, best_cost);
3005     }
3006   else
3007     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
3008       if ((unit & 1) != 0)
3009         cost = actual_hazard (i, insn, clock, cost);
3010
3011   return cost;
3012 }
3013
3014 /* Return the potential hazard cost of executing an instruction on the
3015    units encoded by UNIT if the previous potential hazard cost was COST.
3016    An insn with a large blockage time is chosen in preference to one
3017    with a smaller time; an insn that uses a unit that is more likely
3018    to be used is chosen in preference to one with a unit that is less
3019    used.  We are trying to minimize a subsequent actual hazard.  */
3020
3021 HAIFA_INLINE static int
3022 potential_hazard (unit, insn, cost)
3023      int unit, cost;
3024      rtx insn;
3025 {
3026   int i, ncost;
3027   unsigned int minb, maxb;
3028
3029   if (unit >= 0)
3030     {
3031       minb = maxb = function_units[unit].max_blockage;
3032       if (maxb > 1)
3033         {
3034           if (function_units[unit].blockage_range_function)
3035             {
3036               maxb = minb = blockage_range (unit, insn);
3037               maxb = MAX_BLOCKAGE_COST (maxb);
3038               minb = MIN_BLOCKAGE_COST (minb);
3039             }
3040
3041           if (maxb > 1)
3042             {
3043               /* Make the number of instructions left dominate.  Make the
3044                  minimum delay dominate the maximum delay.  If all these
3045                  are the same, use the unit number to add an arbitrary
3046                  ordering.  Other terms can be added.  */
3047               ncost = minb * 0x40 + maxb;
3048               ncost *= (unit_n_insns[unit] - 1) * 0x1000 + unit;
3049               if (ncost > cost)
3050                 cost = ncost;
3051             }
3052         }
3053     }
3054   else
3055     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
3056       if ((unit & 1) != 0)
3057         cost = potential_hazard (i, insn, cost);
3058
3059   return cost;
3060 }
3061
3062 /* Compute cost of executing INSN given the dependence LINK on the insn USED.
3063    This is the number of cycles between instruction issue and
3064    instruction results.  */
3065
3066 HAIFA_INLINE static int
3067 insn_cost (insn, link, used)
3068      rtx insn, link, used;
3069 {
3070   register int cost = INSN_COST (insn);
3071
3072   if (cost == 0)
3073     {
3074       recog_memoized (insn);
3075
3076       /* A USE insn, or something else we don't need to understand.
3077          We can't pass these directly to result_ready_cost because it will
3078          trigger a fatal error for unrecognizable insns.  */
3079       if (INSN_CODE (insn) < 0)
3080         {
3081           INSN_COST (insn) = 1;
3082           return 1;
3083         }
3084       else
3085         {
3086           cost = result_ready_cost (insn);
3087
3088           if (cost < 1)
3089             cost = 1;
3090
3091           INSN_COST (insn) = cost;
3092         }
3093     }
3094
3095   /* in this case estimate cost without caring how insn is used.  */
3096   if (link == 0 && used == 0)
3097     return cost;
3098
3099   /* A USE insn should never require the value used to be computed.  This
3100      allows the computation of a function's result and parameter values to
3101      overlap the return and call.  */
3102   recog_memoized (used);
3103   if (INSN_CODE (used) < 0)
3104     LINK_COST_FREE (link) = 1;
3105
3106   /* If some dependencies vary the cost, compute the adjustment.  Most
3107      commonly, the adjustment is complete: either the cost is ignored
3108      (in the case of an output- or anti-dependence), or the cost is
3109      unchanged.  These values are cached in the link as LINK_COST_FREE
3110      and LINK_COST_ZERO.  */
3111
3112   if (LINK_COST_FREE (link))
3113     cost = 1;
3114 #ifdef ADJUST_COST
3115   else if (!LINK_COST_ZERO (link))
3116     {
3117       int ncost = cost;
3118
3119       ADJUST_COST (used, link, insn, ncost);
3120       if (ncost <= 1)
3121         LINK_COST_FREE (link) = ncost = 1;
3122       if (cost == ncost)
3123         LINK_COST_ZERO (link) = 1;
3124       cost = ncost;
3125     }
3126 #endif
3127   return cost;
3128 }
3129
3130 /* Compute the priority number for INSN.  */
3131
3132 static int
3133 priority (insn)
3134      rtx insn;
3135 {
3136   int this_priority;
3137   rtx link;
3138
3139   if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
3140     return 0;
3141
3142   if ((this_priority = INSN_PRIORITY (insn)) == 0)
3143     {
3144       if (INSN_DEPEND (insn) == 0)
3145         this_priority = insn_cost (insn, 0, 0);
3146       else
3147         for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
3148           {
3149             rtx next;
3150             int next_priority;
3151
3152             if (RTX_INTEGRATED_P (link))
3153               continue;
3154
3155             next = XEXP (link, 0);
3156
3157             /* critical path is meaningful in block boundaries only */
3158             if (INSN_BLOCK (next) != INSN_BLOCK (insn))
3159               continue;
3160
3161             next_priority = insn_cost (insn, link, next) + priority (next);
3162             if (next_priority > this_priority)
3163               this_priority = next_priority;
3164           }
3165       INSN_PRIORITY (insn) = this_priority;
3166     }
3167   return this_priority;
3168 }
3169 \f
3170
3171 /* Remove all INSN_LISTs and EXPR_LISTs from the pending lists and add
3172    them to the unused_*_list variables, so that they can be reused.  */
3173
3174 static void
3175 free_pending_lists ()
3176 {
3177   if (current_nr_blocks <= 1)
3178     {
3179       free_list (&pending_read_insns, &unused_insn_list);
3180       free_list (&pending_write_insns, &unused_insn_list);
3181       free_list (&pending_read_mems, &unused_expr_list);
3182       free_list (&pending_write_mems, &unused_expr_list);
3183     }
3184   else
3185     {
3186       /* interblock scheduling */
3187       int bb;
3188
3189       for (bb = 0; bb < current_nr_blocks; bb++)
3190         {
3191           free_list (&bb_pending_read_insns[bb], &unused_insn_list);
3192           free_list (&bb_pending_write_insns[bb], &unused_insn_list);
3193           free_list (&bb_pending_read_mems[bb], &unused_expr_list);
3194           free_list (&bb_pending_write_mems[bb], &unused_expr_list);
3195         }
3196     }
3197 }
3198
3199 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
3200    The MEM is a memory reference contained within INSN, which we are saving
3201    so that we can do memory aliasing on it.  */
3202
3203 static void
3204 add_insn_mem_dependence (insn_list, mem_list, insn, mem)
3205      rtx *insn_list, *mem_list, insn, mem;
3206 {
3207   register rtx link;
3208
3209   link = alloc_INSN_LIST (insn, *insn_list);
3210   *insn_list = link;
3211
3212   link = alloc_EXPR_LIST (VOIDmode, mem, *mem_list);
3213   *mem_list = link;
3214
3215   pending_lists_length++;
3216 }
3217 \f
3218
3219 /* Make a dependency between every memory reference on the pending lists
3220    and INSN, thus flushing the pending lists.  If ONLY_WRITE, don't flush
3221    the read list.  */
3222
3223 static void
3224 flush_pending_lists (insn, only_write)
3225      rtx insn;
3226      int only_write;
3227 {
3228   rtx u;
3229   rtx link;
3230
3231   while (pending_read_insns && ! only_write)
3232     {
3233       add_dependence (insn, XEXP (pending_read_insns, 0), REG_DEP_ANTI);
3234
3235       link = pending_read_insns;
3236       pending_read_insns = XEXP (pending_read_insns, 1);
3237       XEXP (link, 1) = unused_insn_list;
3238       unused_insn_list = link;
3239
3240       link = pending_read_mems;
3241       pending_read_mems = XEXP (pending_read_mems, 1);
3242       XEXP (link, 1) = unused_expr_list;
3243       unused_expr_list = link;
3244     }
3245   while (pending_write_insns)
3246     {
3247       add_dependence (insn, XEXP (pending_write_insns, 0), REG_DEP_ANTI);
3248
3249       link = pending_write_insns;
3250       pending_write_insns = XEXP (pending_write_insns, 1);
3251       XEXP (link, 1) = unused_insn_list;
3252       unused_insn_list = link;
3253
3254       link = pending_write_mems;
3255       pending_write_mems = XEXP (pending_write_mems, 1);
3256       XEXP (link, 1) = unused_expr_list;
3257       unused_expr_list = link;
3258     }
3259   pending_lists_length = 0;
3260
3261   /* last_pending_memory_flush is now a list of insns */
3262   for (u = last_pending_memory_flush; u; u = XEXP (u, 1))
3263     add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3264
3265   free_list (&last_pending_memory_flush, &unused_insn_list);
3266   last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
3267 }
3268
3269 /* Analyze a single SET or CLOBBER rtx, X, creating all dependencies generated
3270    by the write to the destination of X, and reads of everything mentioned.  */
3271
3272 static void
3273 sched_analyze_1 (x, insn)
3274      rtx x;
3275      rtx insn;
3276 {
3277   register int regno;
3278   register rtx dest = SET_DEST (x);
3279
3280   if (dest == 0)
3281     return;
3282
3283   while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
3284       || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
3285     {
3286       if (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
3287         {
3288           /* The second and third arguments are values read by this insn.  */
3289           sched_analyze_2 (XEXP (dest, 1), insn);
3290           sched_analyze_2 (XEXP (dest, 2), insn);
3291         }
3292       dest = SUBREG_REG (dest);
3293     }
3294
3295   if (GET_CODE (dest) == REG)
3296     {
3297       register int i;
3298
3299       regno = REGNO (dest);
3300
3301       /* A hard reg in a wide mode may really be multiple registers.
3302          If so, mark all of them just like the first.  */
3303       if (regno < FIRST_PSEUDO_REGISTER)
3304         {
3305           i = HARD_REGNO_NREGS (regno, GET_MODE (dest));
3306           while (--i >= 0)
3307             {
3308               rtx u;
3309
3310               for (u = reg_last_uses[regno + i]; u; u = XEXP (u, 1))
3311                 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3312               reg_last_uses[regno + i] = 0;
3313
3314               for (u = reg_last_sets[regno + i]; u; u = XEXP (u, 1))
3315                 add_dependence (insn, XEXP (u, 0), REG_DEP_OUTPUT);
3316
3317               SET_REGNO_REG_SET (reg_pending_sets, regno + i);
3318
3319               if ((call_used_regs[regno + i] || global_regs[regno + i]))
3320                 /* Function calls clobber all call_used regs.  */
3321                 for (u = last_function_call; u; u = XEXP (u, 1))
3322                   add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3323             }
3324         }
3325       else
3326         {
3327           rtx u;
3328
3329           for (u = reg_last_uses[regno]; u; u = XEXP (u, 1))
3330             add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3331           reg_last_uses[regno] = 0;
3332
3333           for (u = reg_last_sets[regno]; u; u = XEXP (u, 1))
3334             add_dependence (insn, XEXP (u, 0), REG_DEP_OUTPUT);
3335
3336           SET_REGNO_REG_SET (reg_pending_sets, regno);
3337
3338           /* Pseudos that are REG_EQUIV to something may be replaced
3339              by that during reloading.  We need only add dependencies for
3340              the address in the REG_EQUIV note.  */
3341           if (!reload_completed
3342               && reg_known_equiv_p[regno]
3343               && GET_CODE (reg_known_value[regno]) == MEM)
3344             sched_analyze_2 (XEXP (reg_known_value[regno], 0), insn);
3345
3346           /* Don't let it cross a call after scheduling if it doesn't
3347              already cross one.  */
3348
3349           if (REG_N_CALLS_CROSSED (regno) == 0)
3350             for (u = last_function_call; u; u = XEXP (u, 1))
3351               add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3352         }
3353     }
3354   else if (GET_CODE (dest) == MEM)
3355     {
3356       /* Writing memory.  */
3357
3358       if (pending_lists_length > 32)
3359         {
3360           /* Flush all pending reads and writes to prevent the pending lists
3361              from getting any larger.  Insn scheduling runs too slowly when
3362              these lists get long.  The number 32 was chosen because it
3363              seems like a reasonable number.  When compiling GCC with itself,
3364              this flush occurs 8 times for sparc, and 10 times for m88k using
3365              the number 32.  */
3366           flush_pending_lists (insn, 0);
3367         }
3368       else
3369         {
3370           rtx u;
3371           rtx pending, pending_mem;
3372
3373           pending = pending_read_insns;
3374           pending_mem = pending_read_mems;
3375           while (pending)
3376             {
3377               /* If a dependency already exists, don't create a new one.  */
3378               if (!find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
3379                 if (anti_dependence (XEXP (pending_mem, 0), dest))
3380                   add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
3381
3382               pending = XEXP (pending, 1);
3383               pending_mem = XEXP (pending_mem, 1);
3384             }
3385
3386           pending = pending_write_insns;
3387           pending_mem = pending_write_mems;
3388           while (pending)
3389             {
3390               /* If a dependency already exists, don't create a new one.  */
3391               if (!find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
3392                 if (output_dependence (XEXP (pending_mem, 0), dest))
3393                   add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
3394
3395               pending = XEXP (pending, 1);
3396               pending_mem = XEXP (pending_mem, 1);
3397             }
3398
3399           for (u = last_pending_memory_flush; u; u = XEXP (u, 1))
3400             add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3401
3402           add_insn_mem_dependence (&pending_write_insns, &pending_write_mems,
3403                                    insn, dest);
3404         }
3405       sched_analyze_2 (XEXP (dest, 0), insn);
3406     }
3407
3408   /* Analyze reads.  */
3409   if (GET_CODE (x) == SET)
3410     sched_analyze_2 (SET_SRC (x), insn);
3411 }
3412
3413 /* Analyze the uses of memory and registers in rtx X in INSN.  */
3414
3415 static void
3416 sched_analyze_2 (x, insn)
3417      rtx x;
3418      rtx insn;
3419 {
3420   register int i;
3421   register int j;
3422   register enum rtx_code code;
3423   register char *fmt;
3424
3425   if (x == 0)
3426     return;
3427
3428   code = GET_CODE (x);
3429
3430   switch (code)
3431     {
3432     case CONST_INT:
3433     case CONST_DOUBLE:
3434     case SYMBOL_REF:
3435     case CONST:
3436     case LABEL_REF:
3437       /* Ignore constants.  Note that we must handle CONST_DOUBLE here
3438          because it may have a cc0_rtx in its CONST_DOUBLE_CHAIN field, but
3439          this does not mean that this insn is using cc0.  */
3440       return;
3441
3442 #ifdef HAVE_cc0
3443     case CC0:
3444       {
3445         rtx link, prev;
3446
3447         /* User of CC0 depends on immediately preceding insn.  */
3448         SCHED_GROUP_P (insn) = 1;
3449
3450         /* There may be a note before this insn now, but all notes will
3451            be removed before we actually try to schedule the insns, so
3452            it won't cause a problem later.  We must avoid it here though.  */
3453         prev = prev_nonnote_insn (insn);
3454
3455         /* Make a copy of all dependencies on the immediately previous insn,
3456            and add to this insn.  This is so that all the dependencies will
3457            apply to the group.  Remove an explicit dependence on this insn
3458            as SCHED_GROUP_P now represents it.  */
3459
3460         if (find_insn_list (prev, LOG_LINKS (insn)))
3461           remove_dependence (insn, prev);
3462
3463         for (link = LOG_LINKS (prev); link; link = XEXP (link, 1))
3464           add_dependence (insn, XEXP (link, 0), REG_NOTE_KIND (link));
3465
3466         return;
3467       }
3468 #endif
3469
3470     case REG:
3471       {
3472         rtx u;
3473         int regno = REGNO (x);
3474         if (regno < FIRST_PSEUDO_REGISTER)
3475           {
3476             int i;
3477
3478             i = HARD_REGNO_NREGS (regno, GET_MODE (x));
3479             while (--i >= 0)
3480               {
3481                 reg_last_uses[regno + i]
3482                   = alloc_INSN_LIST (insn, reg_last_uses[regno + i]);
3483
3484                 for (u = reg_last_sets[regno + i]; u; u = XEXP (u, 1))
3485                   add_dependence (insn, XEXP (u, 0), 0);
3486
3487                 if ((call_used_regs[regno + i] || global_regs[regno + i]))
3488                   /* Function calls clobber all call_used regs.  */
3489                   for (u = last_function_call; u; u = XEXP (u, 1))
3490                     add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3491               }
3492           }
3493         else
3494           {
3495             reg_last_uses[regno] = alloc_INSN_LIST (insn, reg_last_uses[regno]);
3496
3497             for (u = reg_last_sets[regno]; u; u = XEXP (u, 1))
3498               add_dependence (insn, XEXP (u, 0), 0);
3499
3500             /* Pseudos that are REG_EQUIV to something may be replaced
3501                by that during reloading.  We need only add dependencies for
3502                the address in the REG_EQUIV note.  */
3503             if (!reload_completed
3504                 && reg_known_equiv_p[regno]
3505                 && GET_CODE (reg_known_value[regno]) == MEM)
3506               sched_analyze_2 (XEXP (reg_known_value[regno], 0), insn);
3507
3508             /* If the register does not already cross any calls, then add this
3509                insn to the sched_before_next_call list so that it will still
3510                not cross calls after scheduling.  */
3511             if (REG_N_CALLS_CROSSED (regno) == 0)
3512               add_dependence (sched_before_next_call, insn, REG_DEP_ANTI);
3513           }
3514         return;
3515       }
3516
3517     case MEM:
3518       {
3519         /* Reading memory.  */
3520         rtx u;
3521         rtx pending, pending_mem;
3522
3523         pending = pending_read_insns;
3524         pending_mem = pending_read_mems;
3525         while (pending)
3526           {
3527             /* If a dependency already exists, don't create a new one.  */
3528             if (!find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
3529               if (read_dependence (XEXP (pending_mem, 0), x))
3530                 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
3531
3532             pending = XEXP (pending, 1);
3533             pending_mem = XEXP (pending_mem, 1);
3534           }
3535
3536         pending = pending_write_insns;
3537         pending_mem = pending_write_mems;
3538         while (pending)
3539           {
3540             /* If a dependency already exists, don't create a new one.  */
3541             if (!find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
3542               if (true_dependence (XEXP (pending_mem, 0), VOIDmode,
3543                   x, rtx_varies_p))
3544                 add_dependence (insn, XEXP (pending, 0), 0);
3545
3546             pending = XEXP (pending, 1);
3547             pending_mem = XEXP (pending_mem, 1);
3548           }
3549
3550         for (u = last_pending_memory_flush; u; u = XEXP (u, 1))
3551           add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3552
3553         /* Always add these dependencies to pending_reads, since
3554            this insn may be followed by a write.  */
3555         add_insn_mem_dependence (&pending_read_insns, &pending_read_mems,
3556                                  insn, x);
3557
3558         /* Take advantage of tail recursion here.  */
3559         sched_analyze_2 (XEXP (x, 0), insn);
3560         return;
3561       }
3562
3563     case ASM_OPERANDS:
3564     case ASM_INPUT:
3565     case UNSPEC_VOLATILE:
3566     case TRAP_IF:
3567       {
3568         rtx u;
3569
3570         /* Traditional and volatile asm instructions must be considered to use
3571            and clobber all hard registers, all pseudo-registers and all of
3572            memory.  So must TRAP_IF and UNSPEC_VOLATILE operations.
3573
3574            Consider for instance a volatile asm that changes the fpu rounding
3575            mode.  An insn should not be moved across this even if it only uses
3576            pseudo-regs because it might give an incorrectly rounded result.  */
3577         if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
3578           {
3579             int max_reg = max_reg_num ();
3580             for (i = 0; i < max_reg; i++)
3581               {
3582                 for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
3583                   add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3584                 reg_last_uses[i] = 0;
3585
3586                 /* reg_last_sets[r] is now a list of insns */
3587                 for (u = reg_last_sets[i]; u; u = XEXP (u, 1))
3588                   add_dependence (insn, XEXP (u, 0), 0);
3589               }
3590             reg_pending_sets_all = 1;
3591
3592             flush_pending_lists (insn, 0);
3593           }
3594
3595         /* For all ASM_OPERANDS, we must traverse the vector of input operands.
3596            We can not just fall through here since then we would be confused
3597            by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
3598            traditional asms unlike their normal usage.  */
3599
3600         if (code == ASM_OPERANDS)
3601           {
3602             for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
3603               sched_analyze_2 (ASM_OPERANDS_INPUT (x, j), insn);
3604             return;
3605           }
3606         break;
3607       }
3608
3609     case PRE_DEC:
3610     case POST_DEC:
3611     case PRE_INC:
3612     case POST_INC:
3613       /* These both read and modify the result.  We must handle them as writes
3614          to get proper dependencies for following instructions.  We must handle
3615          them as reads to get proper dependencies from this to previous
3616          instructions.  Thus we need to pass them to both sched_analyze_1
3617          and sched_analyze_2.  We must call sched_analyze_2 first in order
3618          to get the proper antecedent for the read.  */
3619       sched_analyze_2 (XEXP (x, 0), insn);
3620       sched_analyze_1 (x, insn);
3621       return;
3622
3623     default:
3624       break;
3625     }
3626
3627   /* Other cases: walk the insn.  */
3628   fmt = GET_RTX_FORMAT (code);
3629   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3630     {
3631       if (fmt[i] == 'e')
3632         sched_analyze_2 (XEXP (x, i), insn);
3633       else if (fmt[i] == 'E')
3634         for (j = 0; j < XVECLEN (x, i); j++)
3635           sched_analyze_2 (XVECEXP (x, i, j), insn);
3636     }
3637 }
3638
3639 /* Analyze an INSN with pattern X to find all dependencies.  */
3640
3641 static void
3642 sched_analyze_insn (x, insn, loop_notes)
3643      rtx x, insn;
3644      rtx loop_notes;
3645 {
3646   register RTX_CODE code = GET_CODE (x);
3647   rtx link;
3648   int maxreg = max_reg_num ();
3649   int i;
3650
3651   if (code == SET || code == CLOBBER)
3652     sched_analyze_1 (x, insn);
3653   else if (code == PARALLEL)
3654     {
3655       register int i;
3656       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3657         {
3658           code = GET_CODE (XVECEXP (x, 0, i));
3659           if (code == SET || code == CLOBBER)
3660             sched_analyze_1 (XVECEXP (x, 0, i), insn);
3661           else
3662             sched_analyze_2 (XVECEXP (x, 0, i), insn);
3663         }
3664     }
3665   else
3666     sched_analyze_2 (x, insn);
3667
3668   /* Mark registers CLOBBERED or used by called function.  */
3669   if (GET_CODE (insn) == CALL_INSN)
3670     for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
3671       {
3672         if (GET_CODE (XEXP (link, 0)) == CLOBBER)
3673           sched_analyze_1 (XEXP (link, 0), insn);
3674         else
3675           sched_analyze_2 (XEXP (link, 0), insn);
3676       }
3677
3678   /* If there is a {LOOP,EHREGION}_{BEG,END} note in the middle of a basic block, then
3679      we must be sure that no instructions are scheduled across it.
3680      Otherwise, the reg_n_refs info (which depends on loop_depth) would
3681      become incorrect.  */
3682
3683   if (loop_notes)
3684     {
3685       int max_reg = max_reg_num ();
3686       rtx link;
3687
3688       for (i = 0; i < max_reg; i++)
3689         {
3690           rtx u;
3691           for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
3692             add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3693           reg_last_uses[i] = 0;
3694
3695           /* reg_last_sets[r] is now a list of insns */
3696           for (u = reg_last_sets[i]; u; u = XEXP (u, 1))
3697             add_dependence (insn, XEXP (u, 0), 0);
3698         }
3699       reg_pending_sets_all = 1;
3700
3701       flush_pending_lists (insn, 0);
3702
3703       link = loop_notes;
3704       while (XEXP (link, 1))
3705         link = XEXP (link, 1);
3706       XEXP (link, 1) = REG_NOTES (insn);
3707       REG_NOTES (insn) = loop_notes;
3708     }
3709
3710   /* After reload, it is possible for an instruction to have a REG_DEAD note
3711      for a register that actually dies a few instructions earlier.  For
3712      example, this can happen with SECONDARY_MEMORY_NEEDED reloads.
3713      In this case, we must consider the insn to use the register mentioned
3714      in the REG_DEAD note.  Otherwise, we may accidentally move this insn
3715      after another insn that sets the register, thus getting obviously invalid
3716      rtl.  This confuses reorg which believes that REG_DEAD notes are still
3717      meaningful.
3718
3719      ??? We would get better code if we fixed reload to put the REG_DEAD
3720      notes in the right places, but that may not be worth the effort.  */
3721
3722   if (reload_completed)
3723     {
3724       rtx note;
3725
3726       for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3727         if (REG_NOTE_KIND (note) == REG_DEAD)
3728           sched_analyze_2 (XEXP (note, 0), insn);
3729     }
3730
3731   EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i,
3732                              {
3733                                /* reg_last_sets[r] is now a list of insns */
3734                                free_list (&reg_last_sets[i], &unused_insn_list);
3735                                reg_last_sets[i]
3736                                  = alloc_INSN_LIST (insn, NULL_RTX);
3737                              });
3738   CLEAR_REG_SET (reg_pending_sets);
3739
3740   if (reg_pending_sets_all)
3741     {
3742       for (i = 0; i < maxreg; i++)
3743         {
3744           /* reg_last_sets[r] is now a list of insns */
3745           free_list (&reg_last_sets[i], &unused_insn_list);
3746           reg_last_sets[i] = alloc_INSN_LIST (insn, NULL_RTX);
3747         }
3748
3749       reg_pending_sets_all = 0;
3750     }
3751
3752   /* Handle function calls and function returns created by the epilogue
3753      threading code.  */
3754   if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN)
3755     {
3756       rtx dep_insn;
3757       rtx prev_dep_insn;
3758
3759       /* When scheduling instructions, we make sure calls don't lose their
3760          accompanying USE insns by depending them one on another in order.
3761
3762          Also, we must do the same thing for returns created by the epilogue
3763          threading code.  Note this code works only in this special case,
3764          because other passes make no guarantee that they will never emit
3765          an instruction between a USE and a RETURN.  There is such a guarantee
3766          for USE instructions immediately before a call.  */
3767
3768       prev_dep_insn = insn;
3769       dep_insn = PREV_INSN (insn);
3770       while (GET_CODE (dep_insn) == INSN
3771              && GET_CODE (PATTERN (dep_insn)) == USE
3772              && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == REG)
3773         {
3774           SCHED_GROUP_P (prev_dep_insn) = 1;
3775
3776           /* Make a copy of all dependencies on dep_insn, and add to insn.
3777              This is so that all of the dependencies will apply to the
3778              group.  */
3779
3780           for (link = LOG_LINKS (dep_insn); link; link = XEXP (link, 1))
3781             add_dependence (insn, XEXP (link, 0), REG_NOTE_KIND (link));
3782
3783           prev_dep_insn = dep_insn;
3784           dep_insn = PREV_INSN (dep_insn);
3785         }
3786     }
3787 }
3788
3789 /* Analyze every insn between HEAD and TAIL inclusive, creating LOG_LINKS
3790    for every dependency.  */
3791
3792 static void
3793 sched_analyze (head, tail)
3794      rtx head, tail;
3795 {
3796   register rtx insn;
3797   register rtx u;
3798   rtx loop_notes = 0;
3799
3800   for (insn = head;; insn = NEXT_INSN (insn))
3801     {
3802       if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
3803         {
3804           sched_analyze_insn (PATTERN (insn), insn, loop_notes);
3805           loop_notes = 0;
3806         }
3807       else if (GET_CODE (insn) == CALL_INSN)
3808         {
3809           rtx x;
3810           register int i;
3811
3812           CANT_MOVE (insn) = 1;
3813
3814           /* Any instruction using a hard register which may get clobbered
3815              by a call needs to be marked as dependent on this call.
3816              This prevents a use of a hard return reg from being moved
3817              past a void call (i.e. it does not explicitly set the hard
3818              return reg).  */
3819
3820           /* If this call is followed by a NOTE_INSN_SETJMP, then assume that
3821              all registers, not just hard registers, may be clobbered by this
3822              call.  */
3823
3824           /* Insn, being a CALL_INSN, magically depends on
3825              `last_function_call' already.  */
3826
3827           if (NEXT_INSN (insn) && GET_CODE (NEXT_INSN (insn)) == NOTE
3828               && NOTE_LINE_NUMBER (NEXT_INSN (insn)) == NOTE_INSN_SETJMP)
3829             {
3830               int max_reg = max_reg_num ();
3831               for (i = 0; i < max_reg; i++)
3832                 {
3833                   for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
3834                     add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3835
3836                   reg_last_uses[i] = 0;
3837
3838                   /* reg_last_sets[r] is now a list of insns */
3839                   for (u = reg_last_sets[i]; u; u = XEXP (u, 1))
3840                     add_dependence (insn, XEXP (u, 0), 0);
3841                 }
3842               reg_pending_sets_all = 1;
3843
3844               /* Add a pair of fake REG_NOTE which we will later
3845                  convert back into a NOTE_INSN_SETJMP note.  See
3846                  reemit_notes for why we use a pair of NOTEs.  */
3847               REG_NOTES (insn) = alloc_EXPR_LIST (REG_DEAD,
3848                                                   GEN_INT (0),
3849                                                   REG_NOTES (insn));
3850               REG_NOTES (insn) = alloc_EXPR_LIST (REG_DEAD,
3851                                                   GEN_INT (NOTE_INSN_SETJMP),
3852                                                   REG_NOTES (insn));
3853             }
3854           else
3855             {
3856               for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3857                 if (call_used_regs[i] || global_regs[i])
3858                   {
3859                     for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
3860                       add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3861                     reg_last_uses[i] = 0;
3862
3863                     /* reg_last_sets[r] is now a list of insns */
3864                     for (u = reg_last_sets[i]; u; u = XEXP (u, 1))
3865                       add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3866
3867                     SET_REGNO_REG_SET (reg_pending_sets, i);
3868                   }
3869             }
3870
3871           /* For each insn which shouldn't cross a call, add a dependence
3872              between that insn and this call insn.  */
3873           x = LOG_LINKS (sched_before_next_call);
3874           while (x)
3875             {
3876               add_dependence (insn, XEXP (x, 0), REG_DEP_ANTI);
3877               x = XEXP (x, 1);
3878             }
3879           LOG_LINKS (sched_before_next_call) = 0;
3880
3881           sched_analyze_insn (PATTERN (insn), insn, loop_notes);
3882           loop_notes = 0;
3883
3884           /* In the absence of interprocedural alias analysis, we must flush
3885              all pending reads and writes, and start new dependencies starting
3886              from here.  But only flush writes for constant calls (which may
3887              be passed a pointer to something we haven't written yet).  */
3888           flush_pending_lists (insn, CONST_CALL_P (insn));
3889
3890           /* Depend this function call (actually, the user of this
3891              function call) on all hard register clobberage.  */
3892
3893           /* last_function_call is now a list of insns */
3894           free_list(&last_function_call, &unused_insn_list);
3895           last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
3896         }
3897
3898       /* See comments on reemit_notes as to why we do this.  */
3899       else if (GET_CODE (insn) == NOTE
3900                && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
3901                    || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
3902                    || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
3903                    || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END
3904                    || NOTE_LINE_NUMBER (insn) == NOTE_INSN_RANGE_START
3905                    || NOTE_LINE_NUMBER (insn) == NOTE_INSN_RANGE_END
3906                    || (NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP
3907                        && GET_CODE (PREV_INSN (insn)) != CALL_INSN)))
3908         {
3909           loop_notes = alloc_EXPR_LIST (REG_DEAD,
3910                                         GEN_INT (NOTE_BLOCK_NUMBER (insn)),
3911                                         loop_notes);
3912           loop_notes = alloc_EXPR_LIST (REG_DEAD,
3913                                         GEN_INT (NOTE_LINE_NUMBER (insn)),
3914                                         loop_notes);
3915           CONST_CALL_P (loop_notes) = CONST_CALL_P (insn);
3916         }
3917
3918       if (insn == tail)
3919         return;
3920     }
3921   abort ();
3922 }
3923 \f
3924 /* Called when we see a set of a register.  If death is true, then we are
3925    scanning backwards.  Mark that register as unborn.  If nobody says
3926    otherwise, that is how things will remain.  If death is false, then we
3927    are scanning forwards.  Mark that register as being born.  */
3928
3929 static void
3930 sched_note_set (x, death)
3931      rtx x;
3932      int death;
3933 {
3934   register int regno;
3935   register rtx reg = SET_DEST (x);
3936   int subreg_p = 0;
3937
3938   if (reg == 0)
3939     return;
3940
3941   while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == STRICT_LOW_PART
3942          || GET_CODE (reg) == SIGN_EXTRACT || GET_CODE (reg) == ZERO_EXTRACT)
3943     {
3944       /* Must treat modification of just one hardware register of a multi-reg
3945          value or just a byte field of a register exactly the same way that
3946          mark_set_1 in flow.c does, i.e. anything except a paradoxical subreg
3947          does not kill the entire register.  */
3948       if (GET_CODE (reg) != SUBREG
3949           || REG_SIZE (SUBREG_REG (reg)) > REG_SIZE (reg))
3950         subreg_p = 1;
3951
3952       reg = SUBREG_REG (reg);
3953     }
3954
3955   if (GET_CODE (reg) != REG)
3956     return;
3957
3958   /* Global registers are always live, so the code below does not apply
3959      to them.  */
3960
3961   regno = REGNO (reg);
3962   if (regno >= FIRST_PSEUDO_REGISTER || !global_regs[regno])
3963     {
3964       if (death)
3965         {
3966           /* If we only set part of the register, then this set does not
3967              kill it.  */
3968           if (subreg_p)
3969             return;
3970
3971           /* Try killing this register.  */
3972           if (regno < FIRST_PSEUDO_REGISTER)
3973             {
3974               int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
3975               while (--j >= 0)
3976                 {
3977                   CLEAR_REGNO_REG_SET (bb_live_regs, regno + j);
3978                 }
3979             }
3980           else
3981             {
3982               /* Recompute REG_BASIC_BLOCK as we update all the other
3983                  dataflow information.  */
3984               if (sched_reg_basic_block[regno] == REG_BLOCK_UNKNOWN)
3985                 sched_reg_basic_block[regno] = current_block_num;
3986               else if (sched_reg_basic_block[regno] != current_block_num)
3987                 sched_reg_basic_block[regno] = REG_BLOCK_GLOBAL;
3988
3989               CLEAR_REGNO_REG_SET (bb_live_regs, regno);
3990             }
3991         }
3992       else
3993         {
3994           /* Make the register live again.  */
3995           if (regno < FIRST_PSEUDO_REGISTER)
3996             {
3997               int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
3998               while (--j >= 0)
3999                 {
4000                   SET_REGNO_REG_SET (bb_live_regs, regno + j);
4001                 }
4002             }
4003           else
4004             {
4005               SET_REGNO_REG_SET (bb_live_regs, regno);
4006             }
4007         }
4008     }
4009 }
4010 \f
4011 /* Macros and functions for keeping the priority queue sorted, and
4012    dealing with queueing and dequeueing of instructions.  */
4013
4014 #define SCHED_SORT(READY, N_READY)                                   \
4015 do { if ((N_READY) == 2)                                             \
4016        swap_sort (READY, N_READY);                                   \
4017      else if ((N_READY) > 2)                                         \
4018          qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); }  \
4019 while (0)
4020
4021 /* Returns a positive value if x is preferred; returns a negative value if
4022    y is preferred.  Should never return 0, since that will make the sort
4023    unstable.  */
4024
4025 static int
4026 rank_for_schedule (x, y)
4027      const GENERIC_PTR x;
4028      const GENERIC_PTR y;
4029 {
4030   rtx tmp = *(rtx *)y;
4031   rtx tmp2 = *(rtx *)x;
4032   rtx link;
4033   int tmp_class, tmp2_class, depend_count1, depend_count2;
4034   int val, priority_val, spec_val, prob_val, weight_val;
4035
4036
4037   /* prefer insn with higher priority */
4038   priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
4039   if (priority_val)
4040     return priority_val;
4041
4042   /* prefer an insn with smaller contribution to registers-pressure */
4043   if (!reload_completed &&
4044       (weight_val = INSN_REG_WEIGHT (tmp) - INSN_REG_WEIGHT (tmp2)))
4045     return (weight_val);
4046
4047   /* some comparison make sense in interblock scheduling only */
4048   if (INSN_BB (tmp) != INSN_BB (tmp2))
4049     {
4050       /* prefer an inblock motion on an interblock motion */
4051       if ((INSN_BB (tmp2) == target_bb) && (INSN_BB (tmp) != target_bb))
4052         return 1;
4053       if ((INSN_BB (tmp) == target_bb) && (INSN_BB (tmp2) != target_bb))
4054         return -1;
4055
4056       /* prefer a useful motion on a speculative one */
4057       if ((spec_val = IS_SPECULATIVE_INSN (tmp) - IS_SPECULATIVE_INSN (tmp2)))
4058         return (spec_val);
4059
4060       /* prefer a more probable (speculative) insn */
4061       prob_val = INSN_PROBABILITY (tmp2) - INSN_PROBABILITY (tmp);
4062       if (prob_val)
4063         return (prob_val);
4064     }
4065
4066   /* compare insns based on their relation to the last-scheduled-insn */
4067   if (last_scheduled_insn)
4068     {
4069       /* Classify the instructions into three classes:
4070          1) Data dependent on last schedule insn.
4071          2) Anti/Output dependent on last scheduled insn.
4072          3) Independent of last scheduled insn, or has latency of one.
4073          Choose the insn from the highest numbered class if different.  */
4074       link = find_insn_list (tmp, INSN_DEPEND (last_scheduled_insn));
4075       if (link == 0 || insn_cost (last_scheduled_insn, link, tmp) == 1)
4076         tmp_class = 3;
4077       else if (REG_NOTE_KIND (link) == 0)       /* Data dependence.  */
4078         tmp_class = 1;
4079       else
4080         tmp_class = 2;
4081
4082       link = find_insn_list (tmp2, INSN_DEPEND (last_scheduled_insn));
4083       if (link == 0 || insn_cost (last_scheduled_insn, link, tmp2) == 1)
4084         tmp2_class = 3;
4085       else if (REG_NOTE_KIND (link) == 0)       /* Data dependence.  */
4086         tmp2_class = 1;
4087       else
4088         tmp2_class = 2;
4089
4090       if ((val = tmp2_class - tmp_class))
4091         return val;
4092     }
4093
4094   /* Prefer the insn which has more later insns that depend on it. 
4095      This gives the scheduler more freedom when scheduling later
4096      instructions at the expense of added register pressure.  */
4097   depend_count1 = 0;
4098   for (link = INSN_DEPEND (tmp); link; link = XEXP (link, 1))
4099     depend_count1++;
4100
4101   depend_count2 = 0;
4102   for (link = INSN_DEPEND (tmp2); link; link = XEXP (link, 1))
4103     depend_count2++;
4104
4105   val = depend_count2 - depend_count1;
4106   if (val)
4107     return val;
4108   
4109   /* If insns are equally good, sort by INSN_LUID (original insn order),
4110      so that we make the sort stable.  This minimizes instruction movement,
4111      thus minimizing sched's effect on debugging and cross-jumping.  */
4112   return INSN_LUID (tmp) - INSN_LUID (tmp2);
4113 }
4114
4115 /* Resort the array A in which only element at index N may be out of order.  */
4116
4117 HAIFA_INLINE static void
4118 swap_sort (a, n)
4119      rtx *a;
4120      int n;
4121 {
4122   rtx insn = a[n - 1];
4123   int i = n - 2;
4124
4125   while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
4126     {
4127       a[i + 1] = a[i];
4128       i -= 1;
4129     }
4130   a[i + 1] = insn;
4131 }
4132
4133 static int max_priority;
4134
4135 /* Add INSN to the insn queue so that it can be executed at least
4136    N_CYCLES after the currently executing insn.  Preserve insns
4137    chain for debugging purposes.  */
4138
4139 HAIFA_INLINE static void
4140 queue_insn (insn, n_cycles)
4141      rtx insn;
4142      int n_cycles;
4143 {
4144   int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
4145   rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
4146   insn_queue[next_q] = link;
4147   q_size += 1;
4148
4149   if (sched_verbose >= 2)
4150     {
4151       fprintf (dump, ";;\t\tReady-->Q: insn %d: ", INSN_UID (insn));
4152
4153       if (INSN_BB (insn) != target_bb)
4154         fprintf (dump, "(b%d) ", INSN_BLOCK (insn));
4155
4156       fprintf (dump, "queued for %d cycles.\n", n_cycles);
4157     }
4158
4159 }
4160
4161 /* Return nonzero if PAT is the pattern of an insn which makes a
4162    register live.  */
4163
4164 HAIFA_INLINE static int
4165 birthing_insn_p (pat)
4166      rtx pat;
4167 {
4168   int j;
4169
4170   if (reload_completed == 1)
4171     return 0;
4172
4173   if (GET_CODE (pat) == SET
4174       && GET_CODE (SET_DEST (pat)) == REG)
4175     {
4176       rtx dest = SET_DEST (pat);
4177       int i = REGNO (dest);
4178
4179       /* It would be more accurate to use refers_to_regno_p or
4180          reg_mentioned_p to determine when the dest is not live before this
4181          insn.  */
4182
4183       if (REGNO_REG_SET_P (bb_live_regs, i))
4184         return (REG_N_SETS (i) == 1);
4185
4186       return 0;
4187     }
4188   if (GET_CODE (pat) == PARALLEL)
4189     {
4190       for (j = 0; j < XVECLEN (pat, 0); j++)
4191         if (birthing_insn_p (XVECEXP (pat, 0, j)))
4192           return 1;
4193     }
4194   return 0;
4195 }
4196
4197 /* PREV is an insn that is ready to execute.  Adjust its priority if that
4198    will help shorten register lifetimes.  */
4199
4200 HAIFA_INLINE static void
4201 adjust_priority (prev)
4202      rtx prev;
4203 {
4204   /* Trying to shorten register lives after reload has completed
4205      is useless and wrong.  It gives inaccurate schedules.  */
4206   if (reload_completed == 0)
4207     {
4208       rtx note;
4209       int n_deaths = 0;
4210
4211       /* ??? This code has no effect, because REG_DEAD notes are removed
4212          before we ever get here.  */
4213       for (note = REG_NOTES (prev); note; note = XEXP (note, 1))
4214         if (REG_NOTE_KIND (note) == REG_DEAD)
4215           n_deaths += 1;
4216
4217       /* Defer scheduling insns which kill registers, since that
4218          shortens register lives.  Prefer scheduling insns which
4219          make registers live for the same reason.  */
4220       switch (n_deaths)
4221         {
4222         default:
4223           INSN_PRIORITY (prev) >>= 3;
4224           break;
4225         case 3:
4226           INSN_PRIORITY (prev) >>= 2;
4227           break;
4228         case 2:
4229         case 1:
4230           INSN_PRIORITY (prev) >>= 1;
4231           break;
4232         case 0:
4233           if (birthing_insn_p (PATTERN (prev)))
4234             {
4235               int max = max_priority;
4236
4237               if (max > INSN_PRIORITY (prev))
4238                 INSN_PRIORITY (prev) = max;
4239             }
4240           break;
4241         }
4242 #ifdef ADJUST_PRIORITY
4243       ADJUST_PRIORITY (prev);
4244 #endif
4245     }
4246 }
4247
4248 /* INSN is the "currently executing insn".  Launch each insn which was
4249    waiting on INSN.  READY is a vector of insns which are ready to fire.
4250    N_READY is the number of elements in READY.  CLOCK is the current
4251    cycle.  */
4252
4253 static int
4254 schedule_insn (insn, ready, n_ready, clock)
4255      rtx insn;
4256      rtx *ready;
4257      int n_ready;
4258      int clock;
4259 {
4260   rtx link;
4261   int unit;
4262
4263   unit = insn_unit (insn);
4264
4265   if (sched_verbose >= 2)
4266     {
4267       fprintf (dump, ";;\t\t--> scheduling insn <<<%d>>> on unit ", INSN_UID (insn));
4268       insn_print_units (insn);
4269       fprintf (dump, "\n");
4270     }
4271
4272   if (sched_verbose && unit == -1)
4273     visualize_no_unit (insn);
4274
4275   if (MAX_BLOCKAGE > 1 || issue_rate > 1 || sched_verbose)
4276     schedule_unit (unit, insn, clock);
4277
4278   if (INSN_DEPEND (insn) == 0)
4279     return n_ready;
4280
4281   /* This is used by the function adjust_priority above.  */
4282   if (n_ready > 0)
4283     max_priority = MAX (INSN_PRIORITY (ready[0]), INSN_PRIORITY (insn));
4284   else
4285     max_priority = INSN_PRIORITY (insn);
4286
4287   for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
4288     {
4289       rtx next = XEXP (link, 0);
4290       int cost = insn_cost (insn, link, next);
4291
4292       INSN_TICK (next) = MAX (INSN_TICK (next), clock + cost);
4293
4294       if ((INSN_DEP_COUNT (next) -= 1) == 0)
4295         {
4296           int effective_cost = INSN_TICK (next) - clock;
4297
4298           /* For speculative insns, before inserting to ready/queue,
4299              check live, exception-free, and issue-delay */
4300           if (INSN_BB (next) != target_bb
4301               && (!IS_VALID (INSN_BB (next))
4302                   || CANT_MOVE (next)
4303                   || (IS_SPECULATIVE_INSN (next)
4304                       && (insn_issue_delay (next) > 3
4305                           || !check_live (next, INSN_BB (next))
4306                  || !is_exception_free (next, INSN_BB (next), target_bb)))))
4307             continue;
4308
4309           if (sched_verbose >= 2)
4310             {
4311               fprintf (dump, ";;\t\tdependences resolved: insn %d ", INSN_UID (next));
4312
4313               if (current_nr_blocks > 1 && INSN_BB (next) != target_bb)
4314                 fprintf (dump, "/b%d ", INSN_BLOCK (next));
4315
4316               if (effective_cost <= 1)
4317                 fprintf (dump, "into ready\n");
4318               else
4319                 fprintf (dump, "into queue with cost=%d\n", effective_cost);
4320             }
4321
4322           /* Adjust the priority of NEXT and either put it on the ready
4323              list or queue it.  */
4324           adjust_priority (next);
4325           if (effective_cost <= 1)
4326             ready[n_ready++] = next;
4327           else
4328             queue_insn (next, effective_cost);
4329         }
4330     }
4331
4332   return n_ready;
4333 }
4334
4335
4336 /* Add a REG_DEAD note for REG to INSN, reusing a REG_DEAD note from the
4337    dead_notes list.  */
4338
4339 static void
4340 create_reg_dead_note (reg, insn)
4341      rtx reg, insn;
4342 {
4343   rtx link;
4344
4345   /* The number of registers killed after scheduling must be the same as the
4346      number of registers killed before scheduling.  The number of REG_DEAD
4347      notes may not be conserved, i.e. two SImode hard register REG_DEAD notes
4348      might become one DImode hard register REG_DEAD note, but the number of
4349      registers killed will be conserved.
4350
4351      We carefully remove REG_DEAD notes from the dead_notes list, so that
4352      there will be none left at the end.  If we run out early, then there
4353      is a bug somewhere in flow, combine and/or sched.  */
4354
4355   if (dead_notes == 0)
4356     {
4357       if (current_nr_blocks <= 1)
4358         abort ();
4359       else
4360         link = alloc_EXPR_LIST (REG_DEAD, NULL_RTX, NULL_RTX);
4361     }
4362   else
4363     {
4364       /* Number of regs killed by REG.  */
4365       int regs_killed = (REGNO (reg) >= FIRST_PSEUDO_REGISTER ? 1
4366                          : HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)));
4367       /* Number of regs killed by REG_DEAD notes taken off the list.  */
4368       int reg_note_regs;
4369
4370       link = dead_notes;
4371       reg_note_regs = (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
4372                        : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
4373                                            GET_MODE (XEXP (link, 0))));
4374       while (reg_note_regs < regs_killed)
4375         {
4376           link = XEXP (link, 1);
4377
4378           /* LINK might be zero if we killed more registers after scheduling
4379              than before, and the last hard register we kill is actually
4380              multiple hard regs. 
4381
4382              This is normal for interblock scheduling, so deal with it in
4383              that case, else abort.  */
4384           if (link == NULL_RTX && current_nr_blocks <= 1)
4385             abort ();
4386           else if (link == NULL_RTX)
4387             link = alloc_EXPR_LIST (REG_DEAD, gen_rtx_REG (word_mode, 0),
4388                                     NULL_RTX);
4389              
4390           reg_note_regs += (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
4391                             : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
4392                                                 GET_MODE (XEXP (link, 0))));
4393         }
4394       dead_notes = XEXP (link, 1);
4395
4396       /* If we took too many regs kills off, put the extra ones back.  */
4397       while (reg_note_regs > regs_killed)
4398         {
4399           rtx temp_reg, temp_link;
4400
4401           temp_reg = gen_rtx_REG (word_mode, 0);
4402           temp_link = alloc_EXPR_LIST (REG_DEAD, temp_reg, dead_notes);
4403           dead_notes = temp_link;
4404           reg_note_regs--;
4405         }
4406     }
4407
4408   XEXP (link, 0) = reg;
4409   XEXP (link, 1) = REG_NOTES (insn);
4410   REG_NOTES (insn) = link;
4411 }
4412
4413 /* Subroutine on attach_deaths_insn--handles the recursive search
4414    through INSN.  If SET_P is true, then x is being modified by the insn.  */
4415
4416 static void
4417 attach_deaths (x, insn, set_p)
4418      rtx x;
4419      rtx insn;
4420      int set_p;
4421 {
4422   register int i;
4423   register int j;
4424   register enum rtx_code code;
4425   register char *fmt;
4426
4427   if (x == 0)
4428     return;
4429
4430   code = GET_CODE (x);
4431
4432   switch (code)
4433     {
4434     case CONST_INT:
4435     case CONST_DOUBLE:
4436     case LABEL_REF:
4437     case SYMBOL_REF:
4438     case CONST:
4439     case CODE_LABEL:
4440     case PC:
4441     case CC0:
4442       /* Get rid of the easy cases first.  */
4443       return;
4444
4445     case REG:
4446       {
4447         /* If the register dies in this insn, queue that note, and mark
4448            this register as needing to die.  */
4449         /* This code is very similar to mark_used_1 (if set_p is false)
4450            and mark_set_1 (if set_p is true) in flow.c.  */
4451
4452         register int regno;
4453         int some_needed;
4454         int all_needed;
4455
4456         if (set_p)
4457           return;
4458
4459         regno = REGNO (x);
4460         all_needed = some_needed = REGNO_REG_SET_P (old_live_regs, regno);
4461         if (regno < FIRST_PSEUDO_REGISTER)
4462           {
4463             int n;
4464
4465             n = HARD_REGNO_NREGS (regno, GET_MODE (x));
4466             while (--n > 0)
4467               {
4468                 int needed = (REGNO_REG_SET_P (old_live_regs, regno + n));
4469                 some_needed |= needed;
4470                 all_needed &= needed;
4471               }
4472           }
4473
4474         /* If it wasn't live before we started, then add a REG_DEAD note.
4475            We must check the previous lifetime info not the current info,
4476            because we may have to execute this code several times, e.g.
4477            once for a clobber (which doesn't add a note) and later
4478            for a use (which does add a note).
4479
4480            Always make the register live.  We must do this even if it was
4481            live before, because this may be an insn which sets and uses
4482            the same register, in which case the register has already been
4483            killed, so we must make it live again.
4484
4485            Global registers are always live, and should never have a REG_DEAD
4486            note added for them, so none of the code below applies to them.  */
4487
4488         if (regno >= FIRST_PSEUDO_REGISTER || ! global_regs[regno])
4489           {
4490             /* Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
4491                STACK_POINTER_REGNUM, since these are always considered to be
4492                live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
4493             if (regno != FRAME_POINTER_REGNUM
4494 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
4495                 && ! (regno == HARD_FRAME_POINTER_REGNUM)
4496 #endif
4497 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
4498                 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
4499 #endif
4500                 && regno != STACK_POINTER_REGNUM)
4501               {
4502                 if (! all_needed && ! dead_or_set_p (insn, x))
4503                   {
4504                     /* Check for the case where the register dying partially
4505                        overlaps the register set by this insn.  */
4506                     if (regno < FIRST_PSEUDO_REGISTER
4507                         && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
4508                       {
4509                         int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
4510                         while (--n >= 0)
4511                           some_needed |= dead_or_set_regno_p (insn, regno + n);
4512                       }
4513
4514                     /* If none of the words in X is needed, make a REG_DEAD
4515                        note.  Otherwise, we must make partial REG_DEAD
4516                        notes.  */
4517                     if (! some_needed)
4518                       create_reg_dead_note (x, insn);
4519                     else
4520                       {
4521                         int i;
4522
4523                         /* Don't make a REG_DEAD note for a part of a
4524                            register that is set in the insn.  */
4525                         for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
4526                              i >= 0; i--)
4527                           if (! REGNO_REG_SET_P (old_live_regs, regno+i)
4528                               && ! dead_or_set_regno_p (insn, regno + i))
4529                             create_reg_dead_note (gen_rtx_REG (reg_raw_mode[regno + i],
4530                                                                regno + i),
4531                                                   insn);
4532                       }
4533                   }
4534               }
4535
4536             if (regno < FIRST_PSEUDO_REGISTER)
4537               {
4538                 int j = HARD_REGNO_NREGS (regno, GET_MODE (x));
4539                 while (--j >= 0)
4540                   {
4541                     SET_REGNO_REG_SET (bb_live_regs, regno + j);
4542                   }
4543               }
4544             else
4545               {
4546                 /* Recompute REG_BASIC_BLOCK as we update all the other
4547                    dataflow information.  */
4548                 if (sched_reg_basic_block[regno] == REG_BLOCK_UNKNOWN)
4549                   sched_reg_basic_block[regno] = current_block_num;
4550                 else if (sched_reg_basic_block[regno] != current_block_num)
4551                   sched_reg_basic_block[regno] = REG_BLOCK_GLOBAL;
4552
4553                 SET_REGNO_REG_SET (bb_live_regs, regno);
4554               }
4555           }
4556         return;
4557       }
4558
4559     case MEM:
4560       /* Handle tail-recursive case.  */
4561       attach_deaths (XEXP (x, 0), insn, 0);
4562       return;
4563
4564     case SUBREG:
4565       attach_deaths (SUBREG_REG (x), insn,
4566                      set_p && ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
4567                                 <= UNITS_PER_WORD)
4568                                || (GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
4569                                    == GET_MODE_SIZE (GET_MODE ((x))))));
4570       return;
4571
4572     case STRICT_LOW_PART:
4573       attach_deaths (XEXP (x, 0), insn, 0);
4574       return;
4575
4576     case ZERO_EXTRACT:
4577     case SIGN_EXTRACT:
4578       attach_deaths (XEXP (x, 0), insn, 0);
4579       attach_deaths (XEXP (x, 1), insn, 0);
4580       attach_deaths (XEXP (x, 2), insn, 0);
4581       return;
4582
4583     default:
4584       /* Other cases: walk the insn.  */
4585       fmt = GET_RTX_FORMAT (code);
4586       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4587         {
4588           if (fmt[i] == 'e')
4589             attach_deaths (XEXP (x, i), insn, 0);
4590           else if (fmt[i] == 'E')
4591             for (j = 0; j < XVECLEN (x, i); j++)
4592               attach_deaths (XVECEXP (x, i, j), insn, 0);
4593         }
4594     }
4595 }
4596
4597 /* After INSN has executed, add register death notes for each register
4598    that is dead after INSN.  */
4599
4600 static void
4601 attach_deaths_insn (insn)
4602      rtx insn;
4603 {
4604   rtx x = PATTERN (insn);
4605   register RTX_CODE code = GET_CODE (x);
4606   rtx link;
4607
4608   if (code == SET)
4609     {
4610       attach_deaths (SET_SRC (x), insn, 0);
4611
4612       /* A register might die here even if it is the destination, e.g.
4613          it is the target of a volatile read and is otherwise unused.
4614          Hence we must always call attach_deaths for the SET_DEST.  */
4615       attach_deaths (SET_DEST (x), insn, 1);
4616     }
4617   else if (code == PARALLEL)
4618     {
4619       register int i;
4620       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
4621         {
4622           code = GET_CODE (XVECEXP (x, 0, i));
4623           if (code == SET)
4624             {
4625               attach_deaths (SET_SRC (XVECEXP (x, 0, i)), insn, 0);
4626
4627               attach_deaths (SET_DEST (XVECEXP (x, 0, i)), insn, 1);
4628             }
4629           /* Flow does not add REG_DEAD notes to registers that die in
4630              clobbers, so we can't either.  */
4631           else if (code != CLOBBER)
4632             attach_deaths (XVECEXP (x, 0, i), insn, 0);
4633         }
4634     }
4635   /* If this is a CLOBBER, only add REG_DEAD notes to registers inside a
4636      MEM being clobbered, just like flow.  */
4637   else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == MEM)
4638     attach_deaths (XEXP (XEXP (x, 0), 0), insn, 0);
4639   /* Otherwise don't add a death note to things being clobbered.  */
4640   else if (code != CLOBBER)
4641     attach_deaths (x, insn, 0);
4642
4643   /* Make death notes for things used in the called function.  */
4644   if (GET_CODE (insn) == CALL_INSN)
4645     for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
4646       attach_deaths (XEXP (XEXP (link, 0), 0), insn,
4647                      GET_CODE (XEXP (link, 0)) == CLOBBER);
4648 }
4649
4650 /* functions for handlnig of notes */
4651
4652 /* Delete notes beginning with INSN and put them in the chain
4653    of notes ended by NOTE_LIST.
4654    Returns the insn following the notes.  */
4655
4656 static rtx
4657 unlink_other_notes (insn, tail)
4658      rtx insn, tail;
4659 {
4660   rtx prev = PREV_INSN (insn);
4661
4662   while (insn != tail && GET_CODE (insn) == NOTE)
4663     {
4664       rtx next = NEXT_INSN (insn);
4665       /* Delete the note from its current position.  */
4666       if (prev)
4667         NEXT_INSN (prev) = next;
4668       if (next)
4669         PREV_INSN (next) = prev;
4670
4671       /* Don't save away NOTE_INSN_SETJMPs, because they must remain
4672          immediately after the call they follow.  We use a fake
4673          (REG_DEAD (const_int -1)) note to remember them.
4674          Likewise with NOTE_INSN_{LOOP,EHREGION}_{BEG, END}.  */
4675       if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_SETJMP
4676           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG
4677           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_END
4678           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_RANGE_START
4679           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_RANGE_END
4680           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_BEG
4681           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_END)
4682         {
4683           /* Insert the note at the end of the notes list.  */
4684           PREV_INSN (insn) = note_list;
4685           if (note_list)
4686             NEXT_INSN (note_list) = insn;
4687           note_list = insn;
4688         }
4689
4690       insn = next;
4691     }
4692   return insn;
4693 }
4694
4695 /* Delete line notes beginning with INSN. Record line-number notes so
4696    they can be reused.  Returns the insn following the notes.  */
4697
4698 static rtx
4699 unlink_line_notes (insn, tail)
4700      rtx insn, tail;
4701 {
4702   rtx prev = PREV_INSN (insn);
4703
4704   while (insn != tail && GET_CODE (insn) == NOTE)
4705     {
4706       rtx next = NEXT_INSN (insn);
4707
4708       if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
4709         {
4710           /* Delete the note from its current position.  */
4711           if (prev)
4712             NEXT_INSN (prev) = next;
4713           if (next)
4714             PREV_INSN (next) = prev;
4715
4716           /* Record line-number notes so they can be reused.  */
4717           LINE_NOTE (insn) = insn;
4718         }
4719       else
4720         prev = insn;
4721
4722       insn = next;
4723     }
4724   return insn;
4725 }
4726
4727 /* Return the head and tail pointers of BB.  */
4728
4729 HAIFA_INLINE static void
4730 get_block_head_tail (bb, headp, tailp)
4731      int bb;
4732      rtx *headp;
4733      rtx *tailp;
4734 {
4735
4736   rtx head;
4737   rtx tail;
4738   int b;
4739
4740   b = BB_TO_BLOCK (bb);
4741
4742   /* HEAD and TAIL delimit the basic block being scheduled.  */
4743   head = basic_block_head[b];
4744   tail = basic_block_end[b];
4745
4746   /* Don't include any notes or labels at the beginning of the
4747      basic block, or notes at the ends of basic blocks.  */
4748   while (head != tail)
4749     {
4750       if (GET_CODE (head) == NOTE)
4751         head = NEXT_INSN (head);
4752       else if (GET_CODE (tail) == NOTE)
4753         tail = PREV_INSN (tail);
4754       else if (GET_CODE (head) == CODE_LABEL)
4755         head = NEXT_INSN (head);
4756       else
4757         break;
4758     }
4759
4760   *headp = head;
4761   *tailp = tail;
4762 }
4763
4764 /* Delete line notes from bb. Save them so they can be later restored
4765    (in restore_line_notes ()).  */
4766
4767 static void
4768 rm_line_notes (bb)
4769      int bb;
4770 {
4771   rtx next_tail;
4772   rtx tail;
4773   rtx head;
4774   rtx insn;
4775
4776   get_block_head_tail (bb, &head, &tail);
4777
4778   if (head == tail
4779       && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
4780     return;
4781
4782   next_tail = NEXT_INSN (tail);
4783   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
4784     {
4785       rtx prev;
4786
4787       /* Farm out notes, and maybe save them in NOTE_LIST.
4788          This is needed to keep the debugger from
4789          getting completely deranged.  */
4790       if (GET_CODE (insn) == NOTE)
4791         {
4792           prev = insn;
4793           insn = unlink_line_notes (insn, next_tail);
4794
4795           if (prev == tail)
4796             abort ();
4797           if (prev == head)
4798             abort ();
4799           if (insn == next_tail)
4800             abort ();
4801         }
4802     }
4803 }
4804
4805 /* Save line number notes for each insn in bb.  */
4806
4807 static void
4808 save_line_notes (bb)
4809      int bb;
4810 {
4811   rtx head, tail;
4812   rtx next_tail;
4813
4814   /* We must use the true line number for the first insn in the block
4815      that was computed and saved at the start of this pass.  We can't
4816      use the current line number, because scheduling of the previous
4817      block may have changed the current line number.  */
4818
4819   rtx line = line_note_head[BB_TO_BLOCK (bb)];
4820   rtx insn;
4821
4822   get_block_head_tail (bb, &head, &tail);
4823   next_tail = NEXT_INSN (tail);
4824
4825   for (insn = basic_block_head[BB_TO_BLOCK (bb)];
4826        insn != next_tail;
4827        insn = NEXT_INSN (insn))
4828     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
4829       line = insn;
4830     else
4831       LINE_NOTE (insn) = line;
4832 }
4833
4834
4835 /* After bb was scheduled, insert line notes into the insns list.  */
4836
4837 static void
4838 restore_line_notes (bb)
4839      int bb;
4840 {
4841   rtx line, note, prev, new;
4842   int added_notes = 0;
4843   int b;
4844   rtx head, next_tail, insn;
4845
4846   b = BB_TO_BLOCK (bb);
4847
4848   head = basic_block_head[b];
4849   next_tail = NEXT_INSN (basic_block_end[b]);
4850
4851   /* Determine the current line-number.  We want to know the current
4852      line number of the first insn of the block here, in case it is
4853      different from the true line number that was saved earlier.  If
4854      different, then we need a line number note before the first insn
4855      of this block.  If it happens to be the same, then we don't want to
4856      emit another line number note here.  */
4857   for (line = head; line; line = PREV_INSN (line))
4858     if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
4859       break;
4860
4861   /* Walk the insns keeping track of the current line-number and inserting
4862      the line-number notes as needed.  */
4863   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
4864     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
4865       line = insn;
4866   /* This used to emit line number notes before every non-deleted note.
4867      However, this confuses a debugger, because line notes not separated
4868      by real instructions all end up at the same address.  I can find no
4869      use for line number notes before other notes, so none are emitted.  */
4870     else if (GET_CODE (insn) != NOTE
4871              && (note = LINE_NOTE (insn)) != 0
4872              && note != line
4873              && (line == 0
4874                  || NOTE_LINE_NUMBER (note) != NOTE_LINE_NUMBER (line)
4875                  || NOTE_SOURCE_FILE (note) != NOTE_SOURCE_FILE (line)))
4876       {
4877         line = note;
4878         prev = PREV_INSN (insn);
4879         if (LINE_NOTE (note))
4880           {
4881             /* Re-use the original line-number note.  */
4882             LINE_NOTE (note) = 0;
4883             PREV_INSN (note) = prev;
4884             NEXT_INSN (prev) = note;
4885             PREV_INSN (insn) = note;
4886             NEXT_INSN (note) = insn;
4887           }
4888         else
4889           {
4890             added_notes++;
4891             new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
4892             NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
4893             RTX_INTEGRATED_P (new) = RTX_INTEGRATED_P (note);
4894           }
4895       }
4896   if (sched_verbose && added_notes)
4897     fprintf (dump, ";; added %d line-number notes\n", added_notes);
4898 }
4899
4900 /* After scheduling the function, delete redundant line notes from the
4901    insns list.  */
4902
4903 static void
4904 rm_redundant_line_notes ()
4905 {
4906   rtx line = 0;
4907   rtx insn = get_insns ();
4908   int active_insn = 0;
4909   int notes = 0;
4910
4911   /* Walk the insns deleting redundant line-number notes.  Many of these
4912      are already present.  The remainder tend to occur at basic
4913      block boundaries.  */
4914   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
4915     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
4916       {
4917         /* If there are no active insns following, INSN is redundant.  */
4918         if (active_insn == 0)
4919           {
4920             notes++;
4921             NOTE_SOURCE_FILE (insn) = 0;
4922             NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4923           }
4924         /* If the line number is unchanged, LINE is redundant.  */
4925         else if (line
4926                  && NOTE_LINE_NUMBER (line) == NOTE_LINE_NUMBER (insn)
4927                  && NOTE_SOURCE_FILE (line) == NOTE_SOURCE_FILE (insn))
4928           {
4929             notes++;
4930             NOTE_SOURCE_FILE (line) = 0;
4931             NOTE_LINE_NUMBER (line) = NOTE_INSN_DELETED;
4932             line = insn;
4933           }
4934         else
4935           line = insn;
4936         active_insn = 0;
4937       }
4938     else if (!((GET_CODE (insn) == NOTE
4939                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
4940                || (GET_CODE (insn) == INSN
4941                    && (GET_CODE (PATTERN (insn)) == USE
4942                        || GET_CODE (PATTERN (insn)) == CLOBBER))))
4943       active_insn++;
4944
4945   if (sched_verbose && notes)
4946     fprintf (dump, ";; deleted %d line-number notes\n", notes);
4947 }
4948
4949 /* Delete notes between head and tail and put them in the chain
4950    of notes ended by NOTE_LIST.  */
4951
4952 static void
4953 rm_other_notes (head, tail)
4954      rtx head;
4955      rtx tail;
4956 {
4957   rtx next_tail;
4958   rtx insn;
4959
4960   if (head == tail
4961       && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
4962     return;
4963
4964   next_tail = NEXT_INSN (tail);
4965   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
4966     {
4967       rtx prev;
4968
4969       /* Farm out notes, and maybe save them in NOTE_LIST.
4970          This is needed to keep the debugger from
4971          getting completely deranged.  */
4972       if (GET_CODE (insn) == NOTE)
4973         {
4974           prev = insn;
4975
4976           insn = unlink_other_notes (insn, next_tail);
4977
4978           if (prev == tail)
4979             abort ();
4980           if (prev == head)
4981             abort ();
4982           if (insn == next_tail)
4983             abort ();
4984         }
4985     }
4986 }
4987
4988 /* Constructor for `sometimes' data structure.  */
4989
4990 static int
4991 new_sometimes_live (regs_sometimes_live, regno, sometimes_max)
4992      struct sometimes *regs_sometimes_live;
4993      int regno;
4994      int sometimes_max;
4995 {
4996   register struct sometimes *p;
4997
4998   /* There should never be a register greater than max_regno here.  If there
4999      is, it means that a define_split has created a new pseudo reg.  This
5000      is not allowed, since there will not be flow info available for any
5001      new register, so catch the error here.  */
5002   if (regno >= max_regno)
5003     abort ();
5004
5005   p = &regs_sometimes_live[sometimes_max];
5006   p->regno = regno;
5007   p->live_length = 0;
5008   p->calls_crossed = 0;
5009   sometimes_max++;
5010   return sometimes_max;
5011 }
5012
5013 /* Count lengths of all regs we are currently tracking,
5014    and find new registers no longer live.  */
5015
5016 static void
5017 finish_sometimes_live (regs_sometimes_live, sometimes_max)
5018      struct sometimes *regs_sometimes_live;
5019      int sometimes_max;
5020 {
5021   int i;
5022
5023   for (i = 0; i < sometimes_max; i++)
5024     {
5025       register struct sometimes *p = &regs_sometimes_live[i];
5026       int regno = p->regno;
5027
5028       sched_reg_live_length[regno] += p->live_length;
5029       sched_reg_n_calls_crossed[regno] += p->calls_crossed;
5030     }
5031 }
5032
5033 /* functions for computation of registers live/usage info */
5034
5035 /* It is assumed that prior to scheduling basic_block_live_at_start (b)
5036    contains the registers that are alive at the entry to b.
5037
5038    Two passes follow: The first pass is performed before the scheduling
5039    of a region. It scans each block of the region forward, computing
5040    the set of registers alive at the end of the basic block and
5041    discard REG_DEAD notes (done by find_pre_sched_live ()).
5042
5043    The second path is invoked after scheduling all region blocks.
5044    It scans each block of the region backward, a block being traversed
5045    only after its succesors in the region. When the set of registers
5046    live at the end of a basic block may be changed by the scheduling
5047    (this may happen for multiple blocks region), it is computed as
5048    the union of the registers live at the start of its succesors.
5049    The last-use information is updated by inserting REG_DEAD notes.
5050    (done by find_post_sched_live ()) */
5051
5052 /* Scan all the insns to be scheduled, removing register death notes.
5053    Register death notes end up in DEAD_NOTES.
5054    Recreate the register life information for the end of this basic
5055    block.  */
5056
5057 static void
5058 find_pre_sched_live (bb)
5059      int bb;
5060 {
5061   rtx insn, next_tail, head, tail;
5062   int b = BB_TO_BLOCK (bb);
5063
5064   get_block_head_tail (bb, &head, &tail);
5065   COPY_REG_SET (bb_live_regs, basic_block_live_at_start[b]);
5066   next_tail = NEXT_INSN (tail);
5067
5068   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
5069     {
5070       rtx prev, next, link;
5071       int reg_weight = 0;
5072
5073       /* Handle register life information.  */
5074       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
5075         {
5076           /* See if the register gets born here.  */
5077           /* We must check for registers being born before we check for
5078              registers dying.  It is possible for a register to be born and
5079              die in the same insn, e.g. reading from a volatile memory
5080              location into an otherwise unused register.  Such a register
5081              must be marked as dead after this insn.  */
5082           if (GET_CODE (PATTERN (insn)) == SET
5083               || GET_CODE (PATTERN (insn)) == CLOBBER)
5084             {
5085               sched_note_set (PATTERN (insn), 0);
5086               reg_weight++;
5087             }
5088
5089           else if (GET_CODE (PATTERN (insn)) == PARALLEL)
5090             {
5091               int j;
5092               for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
5093                 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
5094                     || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
5095                   {
5096                     sched_note_set (XVECEXP (PATTERN (insn), 0, j), 0);
5097                     reg_weight++;
5098                   }
5099
5100               /* ??? This code is obsolete and should be deleted.  It
5101                  is harmless though, so we will leave it in for now.  */
5102               for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
5103                 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == USE)
5104                   sched_note_set (XVECEXP (PATTERN (insn), 0, j), 0);
5105             }
5106
5107           /* Each call cobbers (makes live) all call-clobbered regs
5108              that are not global or fixed.  Note that the function-value
5109              reg is a call_clobbered reg.  */
5110           if (GET_CODE (insn) == CALL_INSN)
5111             {
5112               int j;
5113               for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
5114                 if (call_used_regs[j] && !global_regs[j]
5115                     && ! fixed_regs[j])
5116                   {
5117                     SET_REGNO_REG_SET (bb_live_regs, j);
5118                   }
5119             }
5120
5121           /* Need to know what registers this insn kills.  */
5122           for (prev = 0, link = REG_NOTES (insn); link; link = next)
5123             {
5124               next = XEXP (link, 1);
5125               if ((REG_NOTE_KIND (link) == REG_DEAD
5126                    || REG_NOTE_KIND (link) == REG_UNUSED)
5127               /* Verify that the REG_NOTE has a valid value.  */
5128                   && GET_CODE (XEXP (link, 0)) == REG)
5129                 {
5130                   register int regno = REGNO (XEXP (link, 0));
5131
5132                   reg_weight--;
5133
5134                   /* Only unlink REG_DEAD notes; leave REG_UNUSED notes
5135                      alone.  */
5136                   if (REG_NOTE_KIND (link) == REG_DEAD)
5137                     {
5138                       if (prev)
5139                         XEXP (prev, 1) = next;
5140                       else
5141                         REG_NOTES (insn) = next;
5142                       XEXP (link, 1) = dead_notes;
5143                       dead_notes = link;
5144                     }
5145                   else
5146                     prev = link;
5147
5148                   if (regno < FIRST_PSEUDO_REGISTER)
5149                     {
5150                       int j = HARD_REGNO_NREGS (regno,
5151                                                 GET_MODE (XEXP (link, 0)));
5152                       while (--j >= 0)
5153                         {
5154                           CLEAR_REGNO_REG_SET (bb_live_regs, regno+j);
5155                         }
5156                     }
5157                   else
5158                     {
5159                       CLEAR_REGNO_REG_SET (bb_live_regs, regno);
5160                     }
5161                 }
5162               else
5163                 prev = link;
5164             }
5165         }
5166
5167       INSN_REG_WEIGHT (insn) = reg_weight;
5168     }
5169 }
5170
5171 /* Update register life and usage information for block bb
5172    after scheduling.  Put register dead notes back in the code.  */
5173
5174 static void
5175 find_post_sched_live (bb)
5176      int bb;
5177 {
5178   int sometimes_max;
5179   int j, i;
5180   int b;
5181   rtx insn;
5182   rtx head, tail, prev_head, next_tail;
5183
5184   register struct sometimes *regs_sometimes_live;
5185
5186   b = BB_TO_BLOCK (bb);
5187
5188   /* compute live regs at the end of bb as a function of its successors.  */
5189   if (current_nr_blocks > 1)
5190     {
5191       int e;
5192       int first_edge;
5193
5194       first_edge = e = OUT_EDGES (b);
5195       CLEAR_REG_SET (bb_live_regs);
5196
5197       if (e)
5198         do
5199           {
5200             int b_succ;
5201
5202             b_succ = TO_BLOCK (e);
5203             IOR_REG_SET (bb_live_regs, basic_block_live_at_start[b_succ]);
5204             e = NEXT_OUT (e);
5205           }
5206         while (e != first_edge);
5207     }
5208
5209   get_block_head_tail (bb, &head, &tail);
5210   next_tail = NEXT_INSN (tail);
5211   prev_head = PREV_INSN (head);
5212
5213   EXECUTE_IF_SET_IN_REG_SET (bb_live_regs, FIRST_PSEUDO_REGISTER, i,
5214                              {
5215                                sched_reg_basic_block[i] = REG_BLOCK_GLOBAL;
5216                              });
5217
5218   /* if the block is empty, same regs are alive at its end and its start.
5219      since this is not guaranteed after interblock scheduling, make sure they
5220      are truly identical.  */
5221   if (NEXT_INSN (prev_head) == tail
5222       && (GET_RTX_CLASS (GET_CODE (tail)) != 'i'))
5223     {
5224       if (current_nr_blocks > 1)
5225         COPY_REG_SET (basic_block_live_at_start[b], bb_live_regs);
5226
5227       return;
5228     }
5229
5230   b = BB_TO_BLOCK (bb);
5231   current_block_num = b;
5232
5233   /* Keep track of register lives.  */
5234   old_live_regs = ALLOCA_REG_SET ();
5235   regs_sometimes_live
5236     = (struct sometimes *) alloca (max_regno * sizeof (struct sometimes));
5237   sometimes_max = 0;
5238
5239   /* initiate "sometimes" data, starting with registers live at end */
5240   sometimes_max = 0;
5241   COPY_REG_SET (old_live_regs, bb_live_regs);
5242   EXECUTE_IF_SET_IN_REG_SET (bb_live_regs, 0, j,
5243                              {
5244                                sometimes_max
5245                                  = new_sometimes_live (regs_sometimes_live,
5246                                                        j, sometimes_max);
5247                              });
5248
5249   /* scan insns back, computing regs live info */
5250   for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
5251     {
5252       /* First we kill registers set by this insn, and then we
5253          make registers used by this insn live.  This is the opposite
5254          order used above because we are traversing the instructions
5255          backwards.  */
5256
5257       /* Strictly speaking, we should scan REG_UNUSED notes and make
5258          every register mentioned there live, however, we will just
5259          kill them again immediately below, so there doesn't seem to
5260          be any reason why we bother to do this.  */
5261
5262       /* See if this is the last notice we must take of a register.  */
5263       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
5264         continue;
5265
5266       if (GET_CODE (PATTERN (insn)) == SET
5267           || GET_CODE (PATTERN (insn)) == CLOBBER)
5268         sched_note_set (PATTERN (insn), 1);
5269       else if (GET_CODE (PATTERN (insn)) == PARALLEL)
5270         {
5271           for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
5272             if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
5273                 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
5274               sched_note_set (XVECEXP (PATTERN (insn), 0, j), 1);
5275         }
5276
5277       /* This code keeps life analysis information up to date.  */
5278       if (GET_CODE (insn) == CALL_INSN)
5279         {
5280           register struct sometimes *p;
5281
5282           /* A call kills all call used registers that are not
5283              global or fixed, except for those mentioned in the call
5284              pattern which will be made live again later.  */
5285           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5286             if (call_used_regs[i] && ! global_regs[i]
5287                 && ! fixed_regs[i])
5288               {
5289                 CLEAR_REGNO_REG_SET (bb_live_regs, i);
5290               }
5291
5292           /* Regs live at the time of a call instruction must not
5293              go in a register clobbered by calls.  Record this for
5294              all regs now live.  Note that insns which are born or
5295              die in a call do not cross a call, so this must be done
5296              after the killings (above) and before the births
5297              (below).  */
5298           p = regs_sometimes_live;
5299           for (i = 0; i < sometimes_max; i++, p++)
5300             if (REGNO_REG_SET_P (bb_live_regs, p->regno))
5301               p->calls_crossed += 1;
5302         }
5303
5304       /* Make every register used live, and add REG_DEAD notes for
5305          registers which were not live before we started.  */
5306       attach_deaths_insn (insn);
5307
5308       /* Find registers now made live by that instruction.  */
5309       EXECUTE_IF_AND_COMPL_IN_REG_SET (bb_live_regs, old_live_regs, 0, j,
5310                                  {
5311                                    sometimes_max
5312                                      = new_sometimes_live (regs_sometimes_live,
5313                                                            j, sometimes_max);
5314                                  });
5315       IOR_REG_SET (old_live_regs, bb_live_regs);
5316
5317       /* Count lengths of all regs we are worrying about now,
5318          and handle registers no longer live.  */
5319
5320       for (i = 0; i < sometimes_max; i++)
5321         {
5322           register struct sometimes *p = &regs_sometimes_live[i];
5323           int regno = p->regno;
5324
5325           p->live_length += 1;
5326
5327           if (!REGNO_REG_SET_P (bb_live_regs, regno))
5328             {
5329               /* This is the end of one of this register's lifetime
5330                  segments.  Save the lifetime info collected so far,
5331                  and clear its bit in the old_live_regs entry.  */
5332               sched_reg_live_length[regno] += p->live_length;
5333               sched_reg_n_calls_crossed[regno] += p->calls_crossed;
5334               CLEAR_REGNO_REG_SET (old_live_regs, p->regno);
5335
5336               /* Delete the reg_sometimes_live entry for this reg by
5337                  copying the last entry over top of it.  */
5338               *p = regs_sometimes_live[--sometimes_max];
5339               /* ...and decrement i so that this newly copied entry
5340                  will be processed.  */
5341               i--;
5342             }
5343         }
5344     }
5345
5346   finish_sometimes_live (regs_sometimes_live, sometimes_max);
5347
5348   /* In interblock scheduling, basic_block_live_at_start may have changed.  */
5349   if (current_nr_blocks > 1)
5350     COPY_REG_SET (basic_block_live_at_start[b], bb_live_regs);
5351
5352
5353   FREE_REG_SET (old_live_regs);
5354 }                               /* find_post_sched_live */
5355
5356 /* After scheduling the subroutine, restore information about uses of
5357    registers.  */
5358
5359 static void
5360 update_reg_usage ()
5361 {
5362   int regno;
5363
5364   if (n_basic_blocks > 0)
5365     EXECUTE_IF_SET_IN_REG_SET (bb_live_regs, FIRST_PSEUDO_REGISTER, regno,
5366                                {
5367                                  sched_reg_basic_block[regno]
5368                                    = REG_BLOCK_GLOBAL;
5369                                });
5370
5371   for (regno = 0; regno < max_regno; regno++)
5372     if (sched_reg_live_length[regno])
5373       {
5374         if (sched_verbose)
5375           {
5376             if (REG_LIVE_LENGTH (regno) > sched_reg_live_length[regno])
5377               fprintf (dump,
5378                        ";; register %d life shortened from %d to %d\n",
5379                        regno, REG_LIVE_LENGTH (regno),
5380                        sched_reg_live_length[regno]);
5381             /* Negative values are special; don't overwrite the current
5382                reg_live_length value if it is negative.  */
5383             else if (REG_LIVE_LENGTH (regno) < sched_reg_live_length[regno]
5384                      && REG_LIVE_LENGTH (regno) >= 0)
5385               fprintf (dump,
5386                        ";; register %d life extended from %d to %d\n",
5387                        regno, REG_LIVE_LENGTH (regno),
5388                        sched_reg_live_length[regno]);
5389
5390             if (!REG_N_CALLS_CROSSED (regno)
5391                 && sched_reg_n_calls_crossed[regno])
5392               fprintf (dump,
5393                        ";; register %d now crosses calls\n", regno);
5394             else if (REG_N_CALLS_CROSSED (regno)
5395                      && !sched_reg_n_calls_crossed[regno]
5396                      && REG_BASIC_BLOCK (regno) != REG_BLOCK_GLOBAL)
5397               fprintf (dump,
5398                        ";; register %d no longer crosses calls\n", regno);
5399
5400             if (REG_BASIC_BLOCK (regno) != sched_reg_basic_block[regno]
5401                 && sched_reg_basic_block[regno] != REG_BLOCK_UNKNOWN
5402                 && REG_BASIC_BLOCK(regno) != REG_BLOCK_UNKNOWN)
5403               fprintf (dump,
5404                        ";; register %d changed basic block from %d to %d\n",
5405                         regno, REG_BASIC_BLOCK(regno),
5406                         sched_reg_basic_block[regno]);
5407
5408           }
5409         /* Negative values are special; don't overwrite the current
5410            reg_live_length value if it is negative.  */
5411         if (REG_LIVE_LENGTH (regno) >= 0)
5412           REG_LIVE_LENGTH (regno) = sched_reg_live_length[regno];
5413
5414         if (sched_reg_basic_block[regno] != REG_BLOCK_UNKNOWN
5415             && REG_BASIC_BLOCK(regno) != REG_BLOCK_UNKNOWN)
5416           REG_BASIC_BLOCK(regno) = sched_reg_basic_block[regno];
5417
5418         /* We can't change the value of reg_n_calls_crossed to zero for
5419            pseudos which are live in more than one block.
5420
5421            This is because combine might have made an optimization which
5422            invalidated basic_block_live_at_start and reg_n_calls_crossed,
5423            but it does not update them.  If we update reg_n_calls_crossed
5424            here, the two variables are now inconsistent, and this might
5425            confuse the caller-save code into saving a register that doesn't
5426            need to be saved.  This is only a problem when we zero calls
5427            crossed for a pseudo live in multiple basic blocks.
5428
5429            Alternatively, we could try to correctly update basic block live
5430            at start here in sched, but that seems complicated.
5431
5432            Note: it is possible that a global register became local, as result
5433            of interblock motion, but will remain marked as a global register.  */
5434         if (sched_reg_n_calls_crossed[regno]
5435             || REG_BASIC_BLOCK (regno) != REG_BLOCK_GLOBAL)
5436           REG_N_CALLS_CROSSED (regno) = sched_reg_n_calls_crossed[regno];
5437
5438       }
5439 }
5440
5441 /* Scheduling clock, modified in schedule_block() and queue_to_ready () */
5442 static int clock_var;
5443
5444 /* Move insns that became ready to fire from queue to ready list.  */
5445
5446 static int
5447 queue_to_ready (ready, n_ready)
5448      rtx ready[];
5449      int n_ready;
5450 {
5451   rtx insn;
5452   rtx link;
5453
5454   q_ptr = NEXT_Q (q_ptr);
5455
5456   /* Add all pending insns that can be scheduled without stalls to the
5457      ready list.  */
5458   for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
5459     {
5460
5461       insn = XEXP (link, 0);
5462       q_size -= 1;
5463
5464       if (sched_verbose >= 2)
5465         fprintf (dump, ";;\t\tQ-->Ready: insn %d: ", INSN_UID (insn));
5466
5467       if (sched_verbose >= 2 && INSN_BB (insn) != target_bb)
5468         fprintf (dump, "(b%d) ", INSN_BLOCK (insn));
5469
5470       ready[n_ready++] = insn;
5471       if (sched_verbose >= 2)
5472         fprintf (dump, "moving to ready without stalls\n");
5473     }
5474   insn_queue[q_ptr] = 0;
5475
5476   /* If there are no ready insns, stall until one is ready and add all
5477      of the pending insns at that point to the ready list.  */
5478   if (n_ready == 0)
5479     {
5480       register int stalls;
5481
5482       for (stalls = 1; stalls < INSN_QUEUE_SIZE; stalls++)
5483         {
5484           if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
5485             {
5486               for (; link; link = XEXP (link, 1))
5487                 {
5488                   insn = XEXP (link, 0);
5489                   q_size -= 1;
5490
5491                   if (sched_verbose >= 2)
5492                     fprintf (dump, ";;\t\tQ-->Ready: insn %d: ", INSN_UID (insn));
5493
5494                   if (sched_verbose >= 2 && INSN_BB (insn) != target_bb)
5495                     fprintf (dump, "(b%d) ", INSN_BLOCK (insn));
5496
5497                   ready[n_ready++] = insn;
5498                   if (sched_verbose >= 2)
5499                     fprintf (dump, "moving to ready with %d stalls\n", stalls);
5500                 }
5501               insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = 0;
5502
5503               if (n_ready)
5504                 break;
5505             }
5506         }
5507
5508       if (sched_verbose && stalls)
5509         visualize_stall_cycles (BB_TO_BLOCK (target_bb), stalls);
5510       q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
5511       clock_var += stalls;
5512     }
5513   return n_ready;
5514 }
5515
5516 /* Print the ready list for debugging purposes. Callable from debugger.  */
5517
5518 extern void
5519 debug_ready_list (ready, n_ready)
5520      rtx ready[];
5521      int n_ready;
5522 {
5523   int i;
5524
5525   for (i = 0; i < n_ready; i++)
5526     {
5527       fprintf (dump, "  %d", INSN_UID (ready[i]));
5528       if (current_nr_blocks > 1 && INSN_BB (ready[i]) != target_bb)
5529         fprintf (dump, "/b%d", INSN_BLOCK (ready[i]));
5530     }
5531   fprintf (dump, "\n");
5532 }
5533
5534 /* Print names of units on which insn can/should execute, for debugging.  */
5535
5536 static void
5537 insn_print_units (insn)
5538      rtx insn;
5539 {
5540   int i;
5541   int unit = insn_unit (insn);
5542
5543   if (unit == -1)
5544     fprintf (dump, "none");
5545   else if (unit >= 0)
5546     fprintf (dump, "%s", function_units[unit].name);
5547   else
5548     {
5549       fprintf (dump, "[");
5550       for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
5551         if (unit & 1)
5552           {
5553             fprintf (dump, "%s", function_units[i].name);
5554             if (unit != 1)
5555               fprintf (dump, " ");
5556           }
5557       fprintf (dump, "]");
5558     }
5559 }
5560
5561 /* MAX_VISUAL_LINES is the maximum number of lines in visualization table
5562    of a basic block.  If more lines are needed, table is splitted to two.
5563    n_visual_lines is the number of lines printed so far for a block.
5564    visual_tbl contains the block visualization info.
5565    vis_no_unit holds insns in a cycle that are not mapped to any unit.  */
5566 #define MAX_VISUAL_LINES 100
5567 #define INSN_LEN 30
5568 int n_visual_lines;
5569 char *visual_tbl;
5570 int n_vis_no_unit;
5571 rtx vis_no_unit[10];
5572
5573 /* Finds units that are in use in this fuction. Required only
5574    for visualization.  */
5575
5576 static void
5577 init_target_units ()
5578 {
5579   rtx insn;
5580   int unit;
5581
5582   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
5583     {
5584       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
5585         continue;
5586
5587       unit = insn_unit (insn);
5588
5589       if (unit < 0)
5590         target_units |= ~unit;
5591       else
5592         target_units |= (1 << unit);
5593     }
5594 }
5595
5596 /* Return the length of the visualization table */
5597
5598 static int
5599 get_visual_tbl_length ()
5600 {
5601   int unit, i;
5602   int n, n1;
5603   char *s;
5604
5605   /* compute length of one field in line */
5606   s = (char *) alloca (INSN_LEN + 5);
5607   sprintf (s, "  %33s", "uname");
5608   n1 = strlen (s);
5609
5610   /* compute length of one line */
5611   n = strlen (";; ");
5612   n += n1;
5613   for (unit = 0; unit < FUNCTION_UNITS_SIZE; unit++)
5614     if (function_units[unit].bitmask & target_units)
5615       for (i = 0; i < function_units[unit].multiplicity; i++)
5616         n += n1;
5617   n += n1;
5618   n += strlen ("\n") + 2;
5619
5620   /* compute length of visualization string */
5621   return (MAX_VISUAL_LINES * n);
5622 }
5623
5624 /* Init block visualization debugging info */
5625
5626 static void
5627 init_block_visualization ()
5628 {
5629   strcpy (visual_tbl, "");
5630   n_visual_lines = 0;
5631   n_vis_no_unit = 0;
5632 }
5633
5634 #define BUF_LEN 256
5635
5636 static char *
5637 safe_concat (buf, cur, str)
5638      char *buf;
5639      char *cur;
5640      char *str;
5641 {
5642   char *end = buf + BUF_LEN - 2;        /* leave room for null */
5643   int c;
5644
5645   if (cur > end)
5646     {
5647       *end = '\0';
5648       return end;
5649     }
5650
5651   while (cur < end && (c = *str++) != '\0')
5652     *cur++ = c;
5653
5654   *cur = '\0';
5655   return cur;
5656 }
5657
5658 /* This recognizes rtx, I classified as expressions. These are always */
5659 /* represent some action on values or results of other expression, */
5660 /* that may be stored in objects representing values.  */
5661
5662 static void
5663 print_exp (buf, x, verbose)
5664      char *buf;
5665      rtx x;
5666      int verbose;
5667 {
5668   char tmp[BUF_LEN];
5669   char *st[4];
5670   char *cur = buf;
5671   char *fun = (char *)0;
5672   char *sep;
5673   rtx op[4];
5674   int i;
5675
5676   for (i = 0; i < 4; i++)
5677     {
5678       st[i] = (char *)0;
5679       op[i] = NULL_RTX;
5680     }
5681
5682   switch (GET_CODE (x))
5683     {
5684     case PLUS:
5685       op[0] = XEXP (x, 0);
5686       st[1] = "+";
5687       op[1] = XEXP (x, 1);
5688       break;
5689     case LO_SUM:
5690       op[0] = XEXP (x, 0);
5691       st[1] = "+low(";
5692       op[1] = XEXP (x, 1);
5693       st[2] = ")";
5694       break;
5695     case MINUS:
5696       op[0] = XEXP (x, 0);
5697       st[1] = "-";
5698       op[1] = XEXP (x, 1);
5699       break;
5700     case COMPARE:
5701       fun = "cmp";
5702       op[0] = XEXP (x, 0);
5703       op[1] = XEXP (x, 1);
5704       break;
5705     case NEG:
5706       st[0] = "-";
5707       op[0] = XEXP (x, 0);
5708       break;
5709     case MULT:
5710       op[0] = XEXP (x, 0);
5711       st[1] = "*";
5712       op[1] = XEXP (x, 1);
5713       break;
5714     case DIV:
5715       op[0] = XEXP (x, 0);
5716       st[1] = "/";
5717       op[1] = XEXP (x, 1);
5718       break;
5719     case UDIV:
5720       fun = "udiv";
5721       op[0] = XEXP (x, 0);
5722       op[1] = XEXP (x, 1);
5723       break;
5724     case MOD:
5725       op[0] = XEXP (x, 0);
5726       st[1] = "%";
5727       op[1] = XEXP (x, 1);
5728       break;
5729     case UMOD:
5730       fun = "umod";
5731       op[0] = XEXP (x, 0);
5732       op[1] = XEXP (x, 1);
5733       break;
5734     case SMIN:
5735       fun = "smin";
5736       op[0] = XEXP (x, 0);
5737       op[1] = XEXP (x, 1);
5738       break;
5739     case SMAX:
5740       fun = "smax";
5741       op[0] = XEXP (x, 0);
5742       op[1] = XEXP (x, 1);
5743       break;
5744     case UMIN:
5745       fun = "umin";
5746       op[0] = XEXP (x, 0);
5747       op[1] = XEXP (x, 1);
5748       break;
5749     case UMAX:
5750       fun = "umax";
5751       op[0] = XEXP (x, 0);
5752       op[1] = XEXP (x, 1);
5753       break;
5754     case NOT:
5755       st[0] = "!";
5756       op[0] = XEXP (x, 0);
5757       break;
5758     case AND:
5759       op[0] = XEXP (x, 0);
5760       st[1] = "&";
5761       op[1] = XEXP (x, 1);
5762       break;
5763     case IOR:
5764       op[0] = XEXP (x, 0);
5765       st[1] = "|";
5766       op[1] = XEXP (x, 1);
5767       break;
5768     case XOR:
5769       op[0] = XEXP (x, 0);
5770       st[1] = "^";
5771       op[1] = XEXP (x, 1);
5772       break;
5773     case ASHIFT:
5774       op[0] = XEXP (x, 0);
5775       st[1] = "<<";
5776       op[1] = XEXP (x, 1);
5777       break;
5778     case LSHIFTRT:
5779       op[0] = XEXP (x, 0);
5780       st[1] = " 0>>";
5781       op[1] = XEXP (x, 1);
5782       break;
5783     case ASHIFTRT:
5784       op[0] = XEXP (x, 0);
5785       st[1] = ">>";
5786       op[1] = XEXP (x, 1);
5787       break;
5788     case ROTATE:
5789       op[0] = XEXP (x, 0);
5790       st[1] = "<-<";
5791       op[1] = XEXP (x, 1);
5792       break;
5793     case ROTATERT:
5794       op[0] = XEXP (x, 0);
5795       st[1] = ">->";
5796       op[1] = XEXP (x, 1);
5797       break;
5798     case ABS:
5799       fun = "abs";
5800       op[0] = XEXP (x, 0);
5801       break;
5802     case SQRT:
5803       fun = "sqrt";
5804       op[0] = XEXP (x, 0);
5805       break;
5806     case FFS:
5807       fun = "ffs";
5808       op[0] = XEXP (x, 0);
5809       break;
5810     case EQ:
5811       op[0] = XEXP (x, 0);
5812       st[1] = "==";
5813       op[1] = XEXP (x, 1);
5814       break;
5815     case NE:
5816       op[0] = XEXP (x, 0);
5817       st[1] = "!=";
5818       op[1] = XEXP (x, 1);
5819       break;
5820     case GT:
5821       op[0] = XEXP (x, 0);
5822       st[1] = ">";
5823       op[1] = XEXP (x, 1);
5824       break;
5825     case GTU:
5826       fun = "gtu";
5827       op[0] = XEXP (x, 0);
5828       op[1] = XEXP (x, 1);
5829       break;
5830     case LT:
5831       op[0] = XEXP (x, 0);
5832       st[1] = "<";
5833       op[1] = XEXP (x, 1);
5834       break;
5835     case LTU:
5836       fun = "ltu";
5837       op[0] = XEXP (x, 0);
5838       op[1] = XEXP (x, 1);
5839       break;
5840     case GE:
5841       op[0] = XEXP (x, 0);
5842       st[1] = ">=";
5843       op[1] = XEXP (x, 1);
5844       break;
5845     case GEU:
5846       fun = "geu";
5847       op[0] = XEXP (x, 0);
5848       op[1] = XEXP (x, 1);
5849       break;
5850     case LE:
5851       op[0] = XEXP (x, 0);
5852       st[1] = "<=";
5853       op[1] = XEXP (x, 1);
5854       break;
5855     case LEU:
5856       fun = "leu";
5857       op[0] = XEXP (x, 0);
5858       op[1] = XEXP (x, 1);
5859       break;
5860     case SIGN_EXTRACT:
5861       fun = (verbose) ? "sign_extract" : "sxt";
5862       op[0] = XEXP (x, 0);
5863       op[1] = XEXP (x, 1);
5864       op[2] = XEXP (x, 2);
5865       break;
5866     case ZERO_EXTRACT:
5867       fun = (verbose) ? "zero_extract" : "zxt";
5868       op[0] = XEXP (x, 0);
5869       op[1] = XEXP (x, 1);
5870       op[2] = XEXP (x, 2);
5871       break;
5872     case SIGN_EXTEND:
5873       fun = (verbose) ? "sign_extend" : "sxn";
5874       op[0] = XEXP (x, 0);
5875       break;
5876     case ZERO_EXTEND:
5877       fun = (verbose) ? "zero_extend" : "zxn";
5878       op[0] = XEXP (x, 0);
5879       break;
5880     case FLOAT_EXTEND:
5881       fun = (verbose) ? "float_extend" : "fxn";
5882       op[0] = XEXP (x, 0);
5883       break;
5884     case TRUNCATE:
5885       fun = (verbose) ? "trunc" : "trn";
5886       op[0] = XEXP (x, 0);
5887       break;
5888     case FLOAT_TRUNCATE:
5889       fun = (verbose) ? "float_trunc" : "ftr";
5890       op[0] = XEXP (x, 0);
5891       break;
5892     case FLOAT:
5893       fun = (verbose) ? "float" : "flt";
5894       op[0] = XEXP (x, 0);
5895       break;
5896     case UNSIGNED_FLOAT:
5897       fun = (verbose) ? "uns_float" : "ufl";
5898       op[0] = XEXP (x, 0);
5899       break;
5900     case FIX:
5901       fun = "fix";
5902       op[0] = XEXP (x, 0);
5903       break;
5904     case UNSIGNED_FIX:
5905       fun = (verbose) ? "uns_fix" : "ufx";
5906       op[0] = XEXP (x, 0);
5907       break;
5908     case PRE_DEC:
5909       st[0] = "--";
5910       op[0] = XEXP (x, 0);
5911       break;
5912     case PRE_INC:
5913       st[0] = "++";
5914       op[0] = XEXP (x, 0);
5915       break;
5916     case POST_DEC:
5917       op[0] = XEXP (x, 0);
5918       st[1] = "--";
5919       break;
5920     case POST_INC:
5921       op[0] = XEXP (x, 0);
5922       st[1] = "++";
5923       break;
5924     case CALL:
5925       st[0] = "call ";
5926       op[0] = XEXP (x, 0);
5927       if (verbose)
5928         {
5929           st[1] = " argc:";
5930           op[1] = XEXP (x, 1);
5931         }
5932       break;
5933     case IF_THEN_ELSE:
5934       st[0] = "{(";
5935       op[0] = XEXP (x, 0);
5936       st[1] = ")?";
5937       op[1] = XEXP (x, 1);
5938       st[2] = ":";
5939       op[2] = XEXP (x, 2);
5940       st[3] = "}";
5941       break;
5942     case TRAP_IF:
5943       fun = "trap_if";
5944       op[0] = TRAP_CONDITION (x);
5945       break;
5946     case UNSPEC:
5947     case UNSPEC_VOLATILE:
5948       {
5949         cur = safe_concat (buf, cur, "unspec");
5950         if (GET_CODE (x) == UNSPEC_VOLATILE)
5951           cur = safe_concat (buf, cur, "/v");
5952         cur = safe_concat (buf, cur, "[");
5953         sep = "";
5954         for (i = 0; i < XVECLEN (x, 0); i++)
5955           {
5956             print_pattern (tmp, XVECEXP (x, 0, i), verbose);
5957             cur = safe_concat (buf, cur, sep);
5958             cur = safe_concat (buf, cur, tmp);
5959             sep = ",";
5960           }
5961         cur = safe_concat (buf, cur, "] ");
5962         sprintf (tmp, "%d", XINT (x, 1));
5963         cur = safe_concat (buf, cur, tmp);
5964       }
5965       break;
5966     default:
5967       /* if (verbose) debug_rtx (x); */
5968       st[0] = GET_RTX_NAME (GET_CODE (x));
5969       break;
5970     }
5971
5972   /* Print this as a function? */
5973   if (fun)
5974     {
5975       cur = safe_concat (buf, cur, fun);
5976       cur = safe_concat (buf, cur, "(");
5977     }
5978
5979   for (i = 0; i < 4; i++)
5980     {
5981       if (st[i])
5982         cur = safe_concat (buf, cur, st[i]);
5983
5984       if (op[i])
5985         {
5986           if (fun && i != 0)
5987             cur = safe_concat (buf, cur, ",");
5988
5989           print_value (tmp, op[i], verbose);
5990           cur = safe_concat (buf, cur, tmp);
5991         }
5992     }
5993
5994   if (fun)
5995     cur = safe_concat (buf, cur, ")");
5996 }               /* print_exp */
5997
5998 /* Prints rtxes, i customly classified as values. They're constants, */
5999 /* registers, labels, symbols and memory accesses.  */
6000
6001 static void
6002 print_value (buf, x, verbose)
6003      char *buf;
6004      rtx x;
6005      int verbose;
6006 {
6007   char t[BUF_LEN];
6008   char *cur = buf;
6009
6010   switch (GET_CODE (x))
6011     {
6012     case CONST_INT:
6013       sprintf (t, "0x%lx", (long)INTVAL (x));
6014       cur = safe_concat (buf, cur, t);
6015       break;
6016     case CONST_DOUBLE:
6017       sprintf (t, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6018       cur = safe_concat (buf, cur, t);
6019       break;
6020     case CONST_STRING:
6021       cur = safe_concat (buf, cur, "\"");
6022       cur = safe_concat (buf, cur, XSTR (x, 0));
6023       cur = safe_concat (buf, cur, "\"");
6024       break;
6025     case SYMBOL_REF:
6026       cur = safe_concat (buf, cur, "`");
6027       cur = safe_concat (buf, cur, XSTR (x, 0));
6028       cur = safe_concat (buf, cur, "'");
6029       break;
6030     case LABEL_REF:
6031       sprintf (t, "L%d", INSN_UID (XEXP (x, 0)));
6032       cur = safe_concat (buf, cur, t);
6033       break;
6034     case CONST:
6035       print_value (t, XEXP (x, 0), verbose);
6036       cur = safe_concat (buf, cur, "const(");
6037       cur = safe_concat (buf, cur, t);
6038       cur = safe_concat (buf, cur, ")");
6039       break;
6040     case HIGH:
6041       print_value (t, XEXP (x, 0), verbose);
6042       cur = safe_concat (buf, cur, "high(");
6043       cur = safe_concat (buf, cur, t);
6044       cur = safe_concat (buf, cur, ")");
6045       break;
6046     case REG:
6047       if (REGNO (x) < FIRST_PSEUDO_REGISTER)
6048         {
6049           int c = reg_names[ REGNO (x) ][0];
6050           if (c >= '0' && c <= '9')
6051             cur = safe_concat (buf, cur, "%");
6052
6053           cur = safe_concat (buf, cur, reg_names[ REGNO (x) ]);
6054         }
6055       else
6056         {
6057           sprintf (t, "r%d", REGNO (x));
6058           cur = safe_concat (buf, cur, t);
6059         }
6060       break;
6061     case SUBREG:
6062       print_value (t, SUBREG_REG (x), verbose);
6063       cur = safe_concat (buf, cur, t);
6064       sprintf (t, "#%d", SUBREG_WORD (x));
6065       cur = safe_concat (buf, cur, t);
6066       break;
6067     case SCRATCH:
6068       cur = safe_concat (buf, cur, "scratch");
6069       break;
6070     case CC0:
6071       cur = safe_concat (buf, cur, "cc0");
6072       break;
6073     case PC:
6074       cur = safe_concat (buf, cur, "pc");
6075       break;
6076     case MEM:
6077       print_value (t, XEXP (x, 0), verbose);
6078       cur = safe_concat (buf, cur, "[");
6079       cur = safe_concat (buf, cur, t);
6080       cur = safe_concat (buf, cur, "]");
6081       break;
6082     default:
6083       print_exp (t, x, verbose);
6084       cur = safe_concat (buf, cur, t);
6085       break;
6086     }
6087 }                               /* print_value */
6088
6089 /* The next step in insn detalization, its pattern recognition */
6090
6091 static void
6092 print_pattern (buf, x, verbose)
6093      char *buf;
6094      rtx x;
6095      int verbose;
6096 {
6097   char t1[BUF_LEN], t2[BUF_LEN], t3[BUF_LEN];
6098
6099   switch (GET_CODE (x))
6100     {
6101     case SET:
6102       print_value (t1, SET_DEST (x), verbose);
6103       print_value (t2, SET_SRC (x), verbose);
6104       sprintf (buf, "%s=%s", t1, t2);
6105       break;
6106     case RETURN:
6107       sprintf (buf, "return");
6108       break;
6109     case CALL:
6110       print_exp (buf, x, verbose);
6111       break;
6112     case CLOBBER:
6113       print_value (t1, XEXP (x, 0), verbose);
6114       sprintf (buf, "clobber %s", t1);
6115       break;
6116     case USE:
6117       print_value (t1, XEXP (x, 0), verbose);
6118       sprintf (buf, "use %s", t1);
6119       break;
6120     case PARALLEL:
6121       {
6122         int i;
6123
6124         sprintf (t1, "{");
6125         for (i = 0; i < XVECLEN (x, 0); i++)
6126           {
6127             print_pattern (t2, XVECEXP (x, 0, i), verbose);
6128             sprintf (t3, "%s%s;", t1, t2);
6129             strcpy (t1, t3);
6130           }
6131         sprintf (buf, "%s}", t1);
6132       }
6133       break;
6134     case SEQUENCE:
6135       {
6136         int i;
6137
6138         sprintf (t1, "%%{");
6139         for (i = 0; i < XVECLEN (x, 0); i++)
6140           {
6141             print_insn (t2, XVECEXP (x, 0, i), verbose);
6142             sprintf (t3, "%s%s;", t1, t2);
6143             strcpy (t1, t3);
6144           }
6145         sprintf (buf, "%s%%}", t1);
6146       }
6147       break;
6148     case ASM_INPUT:
6149       sprintf (buf, "asm {%s}", XSTR (x, 0));
6150       break;
6151     case ADDR_VEC:
6152       break;
6153     case ADDR_DIFF_VEC:
6154       print_value (buf, XEXP (x, 0), verbose);
6155       break;
6156     case TRAP_IF:
6157       print_value (t1, TRAP_CONDITION (x), verbose);
6158       sprintf (buf, "trap_if %s", t1);
6159       break;
6160     case UNSPEC:
6161       {
6162         int i;
6163
6164         sprintf (t1, "unspec{");
6165         for (i = 0; i < XVECLEN (x, 0); i++)
6166           {
6167             print_pattern (t2, XVECEXP (x, 0, i), verbose);
6168             sprintf (t3, "%s%s;", t1, t2);
6169             strcpy (t1, t3);
6170           }
6171         sprintf (buf, "%s}", t1);
6172       }
6173       break;
6174     case UNSPEC_VOLATILE:
6175       {
6176         int i;
6177
6178         sprintf (t1, "unspec/v{");
6179         for (i = 0; i < XVECLEN (x, 0); i++)
6180           {
6181             print_pattern (t2, XVECEXP (x, 0, i), verbose);
6182             sprintf (t3, "%s%s;", t1, t2);
6183             strcpy (t1, t3);
6184           }
6185         sprintf (buf, "%s}", t1);
6186       }
6187       break;
6188     default:
6189       print_value (buf, x, verbose);
6190     }
6191 }                               /* print_pattern */
6192
6193 /* This is the main function in rtl visualization mechanism. It
6194    accepts an rtx and tries to recognize it as an insn, then prints it
6195    properly in human readable form, resembling assembler mnemonics.  */
6196 /* For every insn it prints its UID and BB the insn belongs */
6197 /* too. (probably the last "option" should be extended somehow, since */
6198 /* it depends now on sched.c inner variables ...) */
6199
6200 static void
6201 print_insn (buf, x, verbose)
6202      char *buf;
6203      rtx x;
6204      int verbose;
6205 {
6206   char t[BUF_LEN];
6207   rtx insn = x;
6208
6209   switch (GET_CODE (x))
6210     {
6211     case INSN:
6212       print_pattern (t, PATTERN (x), verbose);
6213       if (verbose)
6214         sprintf (buf, "b%d: i% 4d: %s", INSN_BB (x),
6215                  INSN_UID (x), t);
6216       else
6217         sprintf (buf, "%-4d %s", INSN_UID (x), t);
6218       break;
6219     case JUMP_INSN:
6220       print_pattern (t, PATTERN (x), verbose);
6221       if (verbose)
6222         sprintf (buf, "b%d: i% 4d: jump %s", INSN_BB (x),
6223                  INSN_UID (x), t);
6224       else
6225         sprintf (buf, "%-4d %s", INSN_UID (x), t);
6226       break;
6227     case CALL_INSN:
6228       x = PATTERN (insn);
6229       if (GET_CODE (x) == PARALLEL)
6230         {
6231           x = XVECEXP (x, 0, 0);
6232           print_pattern (t, x, verbose);
6233         }
6234       else
6235         strcpy (t, "call <...>");
6236       if (verbose)
6237         sprintf (buf, "b%d: i% 4d: %s", INSN_BB (insn),
6238                  INSN_UID (insn), t);
6239       else
6240         sprintf (buf, "%-4d %s", INSN_UID (insn), t);
6241       break;
6242     case CODE_LABEL:
6243       sprintf (buf, "L%d:", INSN_UID (x));
6244       break;
6245     case BARRIER:
6246       sprintf (buf, "i% 4d: barrier", INSN_UID (x));
6247       break;
6248     case NOTE:
6249       if (NOTE_LINE_NUMBER (x) > 0)
6250         sprintf (buf, "%4d note \"%s\" %d", INSN_UID (x),
6251                  NOTE_SOURCE_FILE (x), NOTE_LINE_NUMBER (x));
6252       else
6253         sprintf (buf, "%4d %s", INSN_UID (x),
6254                  GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (x)));
6255       break;
6256     default:
6257       if (verbose)
6258         {
6259           sprintf (buf, "Not an INSN at all\n");
6260           debug_rtx (x);
6261         }
6262       else
6263         sprintf (buf, "i%-4d  <What?>", INSN_UID (x));
6264     }
6265 }                               /* print_insn */
6266
6267 void
6268 print_insn_chain (rtx_first)
6269      rtx rtx_first;
6270 {
6271   register rtx tmp_rtx;
6272   char str[BUF_LEN];
6273
6274   strcpy (str, "(nil)\n");
6275   if (rtx_first != 0)
6276     switch (GET_CODE (rtx_first))
6277       {
6278       case INSN:
6279       case JUMP_INSN:
6280       case CALL_INSN:
6281       case NOTE:
6282       case CODE_LABEL:
6283       case BARRIER:
6284         for (tmp_rtx = rtx_first; tmp_rtx != NULL;
6285              tmp_rtx = NEXT_INSN (tmp_rtx))
6286           {
6287             print_insn (str, tmp_rtx, 0);
6288             printf ("%s\n", str);
6289           }
6290         break;
6291       default:
6292         print_insn (str, rtx_first, 0);
6293         printf ("%s\n", str);
6294       }
6295 }                               /* print_insn_chain */
6296
6297 /* Print visualization debugging info */
6298
6299 static void
6300 print_block_visualization (b, s)
6301      int b;
6302      char *s;
6303 {
6304   int unit, i;
6305
6306   /* print header */
6307   fprintf (dump, "\n;;   ==================== scheduling visualization for block %d %s \n", b, s);
6308
6309   /* Print names of units */
6310   fprintf (dump, ";;   %-8s", "clock");
6311   for (unit = 0; unit < FUNCTION_UNITS_SIZE; unit++)
6312     if (function_units[unit].bitmask & target_units)
6313       for (i = 0; i < function_units[unit].multiplicity; i++)
6314         fprintf (dump, "  %-33s", function_units[unit].name);
6315   fprintf (dump, "  %-8s\n", "no-unit");
6316
6317   fprintf (dump, ";;   %-8s", "=====");
6318   for (unit = 0; unit < FUNCTION_UNITS_SIZE; unit++)
6319     if (function_units[unit].bitmask & target_units)
6320       for (i = 0; i < function_units[unit].multiplicity; i++)
6321         fprintf (dump, "  %-33s", "==============================");
6322   fprintf (dump, "  %-8s\n", "=======");
6323
6324   /* Print insns in each cycle */
6325   fprintf (dump, "%s\n", visual_tbl);
6326 }
6327
6328 /* Print insns in the 'no_unit' column of visualization */
6329
6330 static void
6331 visualize_no_unit (insn)
6332      rtx insn;
6333 {
6334   vis_no_unit[n_vis_no_unit] = insn;
6335   n_vis_no_unit++;
6336 }
6337
6338 /* Print insns scheduled in clock, for visualization.  */
6339
6340 static void
6341 visualize_scheduled_insns (b, clock)
6342      int b, clock;
6343 {
6344   int i, unit;
6345
6346   /* if no more room, split table into two */
6347   if (n_visual_lines >= MAX_VISUAL_LINES)
6348     {
6349       print_block_visualization (b, "(incomplete)");
6350       init_block_visualization ();
6351     }
6352
6353   n_visual_lines++;
6354
6355   sprintf (visual_tbl + strlen (visual_tbl), ";;   %-8d", clock);
6356   for (unit = 0; unit < FUNCTION_UNITS_SIZE; unit++)
6357     if (function_units[unit].bitmask & target_units)
6358       for (i = 0; i < function_units[unit].multiplicity; i++)
6359         {
6360           int instance = unit + i * FUNCTION_UNITS_SIZE;
6361           rtx insn = unit_last_insn[instance];
6362
6363           /* print insns that still keep the unit busy */
6364           if (insn &&
6365               actual_hazard_this_instance (unit, instance, insn, clock, 0))
6366             {
6367               char str[BUF_LEN];
6368               print_insn (str, insn, 0);
6369               str[INSN_LEN] = '\0';
6370               sprintf (visual_tbl + strlen (visual_tbl), "  %-33s", str);
6371             }
6372           else
6373             sprintf (visual_tbl + strlen (visual_tbl), "  %-33s", "------------------------------");
6374         }
6375
6376   /* print insns that are not assigned to any unit */
6377   for (i = 0; i < n_vis_no_unit; i++)
6378     sprintf (visual_tbl + strlen (visual_tbl), "  %-8d",
6379              INSN_UID (vis_no_unit[i]));
6380   n_vis_no_unit = 0;
6381
6382   sprintf (visual_tbl + strlen (visual_tbl), "\n");
6383 }
6384
6385 /* Print stalled cycles */
6386
6387 static void
6388 visualize_stall_cycles (b, stalls)
6389      int b, stalls;
6390 {
6391   int i;
6392
6393   /* if no more room, split table into two */
6394   if (n_visual_lines >= MAX_VISUAL_LINES)
6395     {
6396       print_block_visualization (b, "(incomplete)");
6397       init_block_visualization ();
6398     }
6399
6400   n_visual_lines++;
6401
6402   sprintf (visual_tbl + strlen (visual_tbl), ";;       ");
6403   for (i = 0; i < stalls; i++)
6404     sprintf (visual_tbl + strlen (visual_tbl), ".");
6405   sprintf (visual_tbl + strlen (visual_tbl), "\n");
6406 }
6407
6408 /* move_insn1: Remove INSN from insn chain, and link it after LAST insn */
6409
6410 static rtx
6411 move_insn1 (insn, last)
6412      rtx insn, last;
6413 {
6414   NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
6415   PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
6416
6417   NEXT_INSN (insn) = NEXT_INSN (last);
6418   PREV_INSN (NEXT_INSN (last)) = insn;
6419
6420   NEXT_INSN (last) = insn;
6421   PREV_INSN (insn) = last;
6422
6423   return insn;
6424 }
6425
6426 /* Search INSN for fake REG_DEAD note pairs for NOTE_INSN_SETJMP,
6427    NOTE_INSN_{LOOP,EHREGION}_{BEG,END}; and convert them back into
6428    NOTEs.  The REG_DEAD note following first one is contains the saved
6429    value for NOTE_BLOCK_NUMBER which is useful for
6430    NOTE_INSN_EH_REGION_{BEG,END} NOTEs.  LAST is the last instruction
6431    output by the instruction scheduler.  Return the new value of LAST.  */
6432
6433 static rtx
6434 reemit_notes (insn, last)
6435      rtx insn;
6436      rtx last;
6437 {
6438   rtx note, retval;
6439
6440   retval = last;
6441   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
6442     {
6443       if (REG_NOTE_KIND (note) == REG_DEAD
6444           && GET_CODE (XEXP (note, 0)) == CONST_INT)
6445         {
6446           if (INTVAL (XEXP (note, 0)) == NOTE_INSN_SETJMP)
6447             {
6448               retval = emit_note_after (INTVAL (XEXP (note, 0)), insn);
6449               CONST_CALL_P (retval) = CONST_CALL_P (note);
6450               remove_note (insn, note);
6451               note = XEXP (note, 1);
6452             }
6453           else
6454             {
6455               last = emit_note_before (INTVAL (XEXP (note, 0)), last);
6456               remove_note (insn, note);
6457               note = XEXP (note, 1);
6458               NOTE_BLOCK_NUMBER (last) = INTVAL (XEXP (note, 0));
6459             }
6460           remove_note (insn, note);
6461         }
6462     }
6463   return retval;
6464 }
6465
6466 /* Move INSN, and all insns which should be issued before it,
6467    due to SCHED_GROUP_P flag.  Reemit notes if needed.
6468
6469    Return the last insn emitted by the scheduler, which is the
6470    return value from the first call to reemit_notes.  */
6471
6472 static rtx
6473 move_insn (insn, last)
6474      rtx insn, last;
6475 {
6476   rtx retval = NULL;
6477
6478   /* If INSN has SCHED_GROUP_P set, then issue it and any other
6479      insns with SCHED_GROUP_P set first.  */
6480   while (SCHED_GROUP_P (insn))
6481     {
6482       rtx prev = PREV_INSN (insn);
6483
6484       /* Move a SCHED_GROUP_P insn.  */
6485       move_insn1 (insn, last);
6486       /* If this is the first call to reemit_notes, then record
6487          its return value.  */
6488       if (retval == NULL_RTX)
6489         retval = reemit_notes (insn, insn);
6490       else
6491         reemit_notes (insn, insn);
6492       insn = prev;
6493     }
6494
6495   /* Now move the first non SCHED_GROUP_P insn.  */
6496   move_insn1 (insn, last);
6497
6498   /* If this is the first call to reemit_notes, then record
6499      its return value.  */
6500   if (retval == NULL_RTX)
6501     retval = reemit_notes (insn, insn);
6502   else
6503     reemit_notes (insn, insn);
6504
6505   return retval;
6506 }
6507
6508 /* Return an insn which represents a SCHED_GROUP, which is
6509    the last insn in the group.  */
6510
6511 static rtx
6512 group_leader (insn)
6513      rtx insn;
6514 {
6515   rtx prev;
6516
6517   do
6518     {
6519       prev = insn;
6520       insn = next_nonnote_insn (insn);
6521     }
6522   while (insn && SCHED_GROUP_P (insn) && (GET_CODE (insn) != CODE_LABEL));
6523
6524   return prev;
6525 }
6526
6527 /* Use forward list scheduling to rearrange insns of block BB in region RGN,
6528    possibly bringing insns from subsequent blocks in the same region.
6529    Return number of insns scheduled.  */
6530
6531 static int
6532 schedule_block (bb, rgn_n_insns)
6533      int bb;
6534      int rgn_n_insns;
6535 {
6536   /* Local variables.  */
6537   rtx insn, last;
6538   rtx *ready;
6539   int i;
6540   int n_ready = 0;
6541   int can_issue_more;
6542
6543   /* flow block of this bb */
6544   int b = BB_TO_BLOCK (bb);
6545
6546   /* target_n_insns == number of insns in b before scheduling starts.
6547      sched_target_n_insns == how many of b's insns were scheduled.
6548      sched_n_insns == how many insns were scheduled in b */
6549   int target_n_insns = 0;
6550   int sched_target_n_insns = 0;
6551   int sched_n_insns = 0;
6552
6553 #define NEED_NOTHING    0
6554 #define NEED_HEAD       1
6555 #define NEED_TAIL       2
6556   int new_needs;
6557
6558   /* head/tail info for this block */
6559   rtx prev_head;
6560   rtx next_tail;
6561   rtx head;
6562   rtx tail;
6563   int bb_src;
6564
6565   /* We used to have code to avoid getting parameters moved from hard
6566      argument registers into pseudos.
6567
6568      However, it was removed when it proved to be of marginal benefit
6569      and caused problems because schedule_block and compute_forward_dependences
6570      had different notions of what the "head" insn was.  */
6571   get_block_head_tail (bb, &head, &tail);
6572
6573   /* Interblock scheduling could have moved the original head insn from this
6574      block into a proceeding block.  This may also cause schedule_block and
6575      compute_forward_dependences to have different notions of what the
6576      "head" insn was.
6577
6578      If the interblock movement happened to make this block start with
6579      some notes (LOOP, EH or SETJMP) before the first real insn, then
6580      HEAD will have various special notes attached to it which must be
6581      removed so that we don't end up with extra copies of the notes.  */
6582   if (GET_RTX_CLASS (GET_CODE (head)) == 'i')
6583     {
6584       rtx note;
6585
6586       for (note = REG_NOTES (head); note; note = XEXP (note, 1))
6587         if (REG_NOTE_KIND (note) == REG_DEAD
6588             && GET_CODE (XEXP (note, 0)) == CONST_INT)
6589           remove_note (head, note);
6590     }
6591
6592   next_tail = NEXT_INSN (tail);
6593   prev_head = PREV_INSN (head);
6594
6595   /* If the only insn left is a NOTE or a CODE_LABEL, then there is no need
6596      to schedule this block.  */
6597   if (head == tail
6598       && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
6599     return (sched_n_insns);
6600
6601   /* debug info */
6602   if (sched_verbose)
6603     {
6604       fprintf (dump, ";;   ======================================================\n");
6605       fprintf (dump,
6606                ";;   -- basic block %d from %d to %d -- %s reload\n",
6607                b, INSN_UID (basic_block_head[b]),
6608                INSN_UID (basic_block_end[b]),
6609                (reload_completed ? "after" : "before"));
6610       fprintf (dump, ";;   ======================================================\n");
6611       fprintf (dump, "\n");
6612
6613       visual_tbl = (char *) alloca (get_visual_tbl_length ());
6614       init_block_visualization ();
6615     }
6616
6617   /* remove remaining note insns from the block, save them in
6618      note_list.  These notes are restored at the end of
6619      schedule_block ().  */
6620   note_list = 0;
6621   rm_other_notes (head, tail);
6622
6623   target_bb = bb;
6624
6625   /* prepare current target block info */
6626   if (current_nr_blocks > 1)
6627     {
6628       candidate_table = (candidate *) alloca (current_nr_blocks * sizeof (candidate));
6629
6630       bblst_last = 0;
6631       /* ??? It is not clear why bblst_size is computed this way.  The original
6632          number was clearly too small as it resulted in compiler failures.
6633          Multiplying by the original number by 2 (to account for update_bbs
6634          members) seems to be a reasonable solution.  */
6635       /* ??? Or perhaps there is a bug somewhere else in this file?  */
6636       bblst_size = (current_nr_blocks - bb) * rgn_nr_edges * 2;
6637       bblst_table = (int *) alloca (bblst_size * sizeof (int));
6638
6639       bitlst_table_last = 0;
6640       bitlst_table_size = rgn_nr_edges;
6641       bitlst_table = (int *) alloca (rgn_nr_edges * sizeof (int));
6642
6643       compute_trg_info (bb);
6644     }
6645
6646   clear_units ();
6647
6648   /* Allocate the ready list */
6649   ready = (rtx *) alloca ((rgn_n_insns + 1) * sizeof (rtx));
6650
6651   /* Print debugging information.  */
6652   if (sched_verbose >= 5)
6653     debug_dependencies ();
6654
6655
6656   /* Initialize ready list with all 'ready' insns in target block.
6657      Count number of insns in the target block being scheduled.  */
6658   n_ready = 0;
6659   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
6660     {
6661       rtx next;
6662
6663       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
6664         continue;
6665       next = NEXT_INSN (insn);
6666
6667       if (INSN_DEP_COUNT (insn) == 0
6668           && (SCHED_GROUP_P (next) == 0 || GET_RTX_CLASS (GET_CODE (next)) != 'i'))
6669         ready[n_ready++] = insn;
6670       if (!(SCHED_GROUP_P (insn)))
6671         target_n_insns++;
6672     }
6673
6674   /* Add to ready list all 'ready' insns in valid source blocks.
6675      For speculative insns, check-live, exception-free, and
6676      issue-delay.  */
6677   for (bb_src = bb + 1; bb_src < current_nr_blocks; bb_src++)
6678     if (IS_VALID (bb_src))
6679       {
6680         rtx src_head;
6681         rtx src_next_tail;
6682         rtx tail, head;
6683
6684         get_block_head_tail (bb_src, &head, &tail);
6685         src_next_tail = NEXT_INSN (tail);
6686         src_head = head;
6687
6688         if (head == tail
6689             && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
6690           continue;
6691
6692         for (insn = src_head; insn != src_next_tail; insn = NEXT_INSN (insn))
6693           {
6694             if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
6695               continue;
6696
6697             if (!CANT_MOVE (insn)
6698                 && (!IS_SPECULATIVE_INSN (insn)
6699                     || (insn_issue_delay (insn) <= 3
6700                         && check_live (insn, bb_src)
6701                         && is_exception_free (insn, bb_src, target_bb))))
6702
6703               {
6704                 rtx next;
6705
6706                 next = NEXT_INSN (insn);
6707                 if (INSN_DEP_COUNT (insn) == 0
6708                     && (SCHED_GROUP_P (next) == 0
6709                         || GET_RTX_CLASS (GET_CODE (next)) != 'i'))
6710                   ready[n_ready++] = insn;
6711               }
6712           }
6713       }
6714
6715   /* no insns scheduled in this block yet */
6716   last_scheduled_insn = 0;
6717
6718   /* Sort the ready list */
6719   SCHED_SORT (ready, n_ready);
6720
6721   if (sched_verbose >= 2)
6722     {
6723       fprintf (dump, ";;\t\tReady list initially:             ");
6724       debug_ready_list (ready, n_ready);
6725     }
6726
6727   /* Q_SIZE is the total number of insns in the queue.  */
6728   q_ptr = 0;
6729   q_size = 0;
6730   clock_var = 0;
6731   bzero ((char *) insn_queue, sizeof (insn_queue));
6732
6733   /* We start inserting insns after PREV_HEAD.  */
6734   last = prev_head;
6735
6736   /* Initialize INSN_QUEUE, LIST and NEW_NEEDS.  */
6737   new_needs = (NEXT_INSN (prev_head) == basic_block_head[b]
6738                ? NEED_HEAD : NEED_NOTHING);
6739   if (PREV_INSN (next_tail) == basic_block_end[b])
6740     new_needs |= NEED_TAIL;
6741
6742   /* loop until all the insns in BB are scheduled.  */
6743   while (sched_target_n_insns < target_n_insns)
6744     {
6745       int b1;
6746
6747       clock_var++;
6748
6749       /* Add to the ready list all pending insns that can be issued now.
6750          If there are no ready insns, increment clock until one
6751          is ready and add all pending insns at that point to the ready
6752          list.  */
6753       n_ready = queue_to_ready (ready, n_ready);
6754
6755       if (n_ready == 0)
6756         abort ();
6757
6758       if (sched_verbose >= 2)
6759         {
6760           fprintf (dump, ";;\t\tReady list after queue_to_ready:  ");
6761           debug_ready_list (ready, n_ready);
6762         }
6763
6764       /* Sort the ready list.  */
6765       SCHED_SORT (ready, n_ready);
6766
6767       if (sched_verbose)
6768         {
6769           fprintf (dump, "\n;;\tReady list (t =%3d):  ", clock_var);
6770           debug_ready_list (ready, n_ready);
6771         }
6772
6773       /* Issue insns from ready list.
6774          It is important to count down from n_ready, because n_ready may change
6775          as insns are issued.  */
6776       can_issue_more = issue_rate;
6777       for (i = n_ready - 1; i >= 0 && can_issue_more; i--)
6778         {
6779           rtx insn = ready[i];
6780           int cost = actual_hazard (insn_unit (insn), insn, clock_var, 0);
6781
6782           if (cost > 1)
6783             {
6784               queue_insn (insn, cost);
6785               ready[i] = ready[--n_ready];      /* remove insn from ready list */
6786             }
6787           else if (cost == 0)
6788             {
6789               /* an interblock motion? */
6790               if (INSN_BB (insn) != target_bb)
6791                 {
6792                   rtx temp;
6793
6794                   if (IS_SPECULATIVE_INSN (insn))
6795                     {
6796
6797                       if (!check_live (insn, INSN_BB (insn)))
6798                         {
6799                           /* speculative motion, live check failed, remove
6800                              insn from ready list */
6801                           ready[i] = ready[--n_ready];
6802                           continue;
6803                         }
6804                       update_live (insn, INSN_BB (insn));
6805
6806                       /* for speculative load, mark insns fed by it.  */
6807                       if (IS_LOAD_INSN (insn) || FED_BY_SPEC_LOAD (insn))
6808                         set_spec_fed (insn);
6809
6810                       nr_spec++;
6811                     }
6812                   nr_inter++;
6813
6814                   temp = insn;
6815                   while (SCHED_GROUP_P (temp))
6816                     temp = PREV_INSN (temp);
6817
6818                   /* Update source block boundaries.   */
6819                   b1 = INSN_BLOCK (temp);
6820                   if (temp == basic_block_head[b1]
6821                       && insn == basic_block_end[b1])
6822                     {
6823                       /* We moved all the insns in the basic block.
6824                          Emit a note after the last insn and update the
6825                          begin/end boundaries to point to the note.  */
6826                       emit_note_after (NOTE_INSN_DELETED, insn);
6827                       basic_block_end[b1] = NEXT_INSN (insn);
6828                       basic_block_head[b1] = NEXT_INSN (insn);
6829                     }
6830                   else if (insn == basic_block_end[b1])
6831                     {
6832                       /* We took insns from the end of the basic block,
6833                          so update the end of block boundary so that it
6834                          points to the first insn we did not move.  */
6835                       basic_block_end[b1] = PREV_INSN (temp);
6836                     }
6837                   else if (temp == basic_block_head[b1])
6838                     {
6839                       /* We took insns from the start of the basic block,
6840                          so update the start of block boundary so that
6841                          it points to the first insn we did not move.  */
6842                       basic_block_head[b1] = NEXT_INSN (insn);
6843                     }
6844                 }
6845               else
6846                 {
6847                   /* in block motion */
6848                   sched_target_n_insns++;
6849                 }
6850
6851               last_scheduled_insn = insn;
6852               last = move_insn (insn, last);
6853               sched_n_insns++;
6854
6855               can_issue_more--;
6856
6857               n_ready = schedule_insn (insn, ready, n_ready, clock_var);
6858
6859               /* remove insn from ready list */
6860               ready[i] = ready[--n_ready];
6861
6862               /* close this block after scheduling its jump */
6863               if (GET_CODE (last_scheduled_insn) == JUMP_INSN)
6864                 break;
6865             }
6866         }
6867
6868       /* debug info */
6869       if (sched_verbose)
6870         {
6871           visualize_scheduled_insns (b, clock_var);
6872         }
6873     }
6874
6875   /* debug info */
6876   if (sched_verbose)
6877     {
6878       fprintf (dump, ";;\tReady list (final):  ");
6879       debug_ready_list (ready, n_ready);
6880       print_block_visualization (b, "");
6881     }
6882
6883   /* Sanity check -- queue must be empty now.  Meaningless if region has
6884      multiple bbs.  */
6885   if (current_nr_blocks > 1)
6886     if (!flag_schedule_interblock && q_size != 0)
6887       abort ();
6888
6889   /* update head/tail boundaries.  */
6890   head = NEXT_INSN (prev_head);
6891   tail = last;
6892
6893   /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
6894      previously found among the insns.  Insert them at the beginning
6895      of the insns.  */
6896   if (note_list != 0)
6897     {
6898       rtx note_head = note_list;
6899
6900       while (PREV_INSN (note_head))
6901         {
6902           note_head = PREV_INSN (note_head);
6903         }
6904
6905       PREV_INSN (note_head) = PREV_INSN (head);
6906       NEXT_INSN (PREV_INSN (head)) = note_head;
6907       PREV_INSN (head) = note_list;
6908       NEXT_INSN (note_list) = head;
6909       head = note_head;
6910     }
6911
6912   /* update target block boundaries.  */
6913   if (new_needs & NEED_HEAD)
6914     basic_block_head[b] = head;
6915
6916   if (new_needs & NEED_TAIL)
6917     basic_block_end[b] = tail;
6918
6919   /* debugging */
6920   if (sched_verbose)
6921     {
6922       fprintf (dump, ";;   total time = %d\n;;   new basic block head = %d\n",
6923                clock_var, INSN_UID (basic_block_head[b]));
6924       fprintf (dump, ";;   new basic block end = %d\n\n",
6925                INSN_UID (basic_block_end[b]));
6926     }
6927
6928   return (sched_n_insns);
6929 }                               /* schedule_block () */
6930 \f
6931
6932 /* print the bit-set of registers, S.  callable from debugger */
6933
6934 extern void
6935 debug_reg_vector (s)
6936      regset s;
6937 {
6938   int regno;
6939
6940   EXECUTE_IF_SET_IN_REG_SET (s, 0, regno,
6941                              {
6942                                fprintf (dump, " %d", regno);
6943                              });
6944
6945   fprintf (dump, "\n");
6946 }
6947
6948 /* Use the backward dependences from LOG_LINKS to build
6949    forward dependences in INSN_DEPEND.  */
6950
6951 static void
6952 compute_block_forward_dependences (bb)
6953      int bb;
6954 {
6955   rtx insn, link;
6956   rtx tail, head;
6957   rtx next_tail;
6958   enum reg_note dep_type;
6959
6960   get_block_head_tail (bb, &head, &tail);
6961   next_tail = NEXT_INSN (tail);
6962   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
6963     {
6964       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
6965         continue;
6966
6967       insn = group_leader (insn);
6968
6969       for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
6970         {
6971           rtx x = group_leader (XEXP (link, 0));
6972           rtx new_link;
6973
6974           if (x != XEXP (link, 0))
6975             continue;
6976
6977           /* Ignore dependences upon deleted insn */
6978           if (GET_CODE (x) == NOTE || INSN_DELETED_P (x))
6979             continue;
6980           if (find_insn_list (insn, INSN_DEPEND (x)))
6981             continue;
6982
6983           new_link = alloc_INSN_LIST (insn, INSN_DEPEND (x));
6984
6985           dep_type = REG_NOTE_KIND (link);
6986           PUT_REG_NOTE_KIND (new_link, dep_type);
6987
6988           INSN_DEPEND (x) = new_link;
6989           INSN_DEP_COUNT (insn) += 1;
6990         }
6991     }
6992 }
6993
6994 /* Initialize variables for region data dependence analysis.
6995    n_bbs is the number of region blocks */
6996
6997 __inline static void
6998 init_rgn_data_dependences (n_bbs)
6999      int n_bbs;
7000 {
7001   int bb;
7002
7003   /* variables for which one copy exists for each block */
7004   bzero ((char *) bb_pending_read_insns, n_bbs * sizeof (rtx));
7005   bzero ((char *) bb_pending_read_mems, n_bbs * sizeof (rtx));
7006   bzero ((char *) bb_pending_write_insns, n_bbs * sizeof (rtx));
7007   bzero ((char *) bb_pending_write_mems, n_bbs * sizeof (rtx));
7008   bzero ((char *) bb_pending_lists_length, n_bbs * sizeof (rtx));
7009   bzero ((char *) bb_last_pending_memory_flush, n_bbs * sizeof (rtx));
7010   bzero ((char *) bb_last_function_call, n_bbs * sizeof (rtx));
7011   bzero ((char *) bb_sched_before_next_call, n_bbs * sizeof (rtx));
7012
7013   /* Create an insn here so that we can hang dependencies off of it later.  */
7014   for (bb = 0; bb < n_bbs; bb++)
7015     {
7016       bb_sched_before_next_call[bb] =
7017         gen_rtx_INSN (VOIDmode, 0, NULL_RTX, NULL_RTX,
7018                       NULL_RTX, 0, NULL_RTX, NULL_RTX);
7019       LOG_LINKS (bb_sched_before_next_call[bb]) = 0;
7020     }
7021 }
7022
7023 /* Add dependences so that branches are scheduled to run last in their block */
7024
7025 static void
7026 add_branch_dependences (head, tail)
7027      rtx head, tail;
7028 {
7029
7030   rtx insn, last;
7031
7032   /* For all branches, calls, uses, and cc0 setters, force them to remain
7033      in order at the end of the block by adding dependencies and giving
7034      the last a high priority.  There may be notes present, and prev_head
7035      may also be a note.
7036
7037      Branches must obviously remain at the end.  Calls should remain at the
7038      end since moving them results in worse register allocation.  Uses remain
7039      at the end to ensure proper register allocation.  cc0 setters remaim
7040      at the end because they can't be moved away from their cc0 user.  */
7041   insn = tail;
7042   last = 0;
7043   while (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
7044          || (GET_CODE (insn) == INSN
7045              && (GET_CODE (PATTERN (insn)) == USE
7046 #ifdef HAVE_cc0
7047                  || sets_cc0_p (PATTERN (insn))
7048 #endif
7049              ))
7050          || GET_CODE (insn) == NOTE)
7051     {
7052       if (GET_CODE (insn) != NOTE)
7053         {
7054           if (last != 0
7055               && !find_insn_list (insn, LOG_LINKS (last)))
7056             {
7057               add_dependence (last, insn, REG_DEP_ANTI);
7058               INSN_REF_COUNT (insn)++;
7059             }
7060
7061           CANT_MOVE (insn) = 1;
7062
7063           last = insn;
7064           /* Skip over insns that are part of a group.
7065              Make each insn explicitly depend on the previous insn.
7066              This ensures that only the group header will ever enter
7067              the ready queue (and, when scheduled, will automatically
7068              schedule the SCHED_GROUP_P block).  */
7069           while (SCHED_GROUP_P (insn))
7070             {
7071               rtx temp = prev_nonnote_insn (insn);
7072               add_dependence (insn, temp, REG_DEP_ANTI);
7073               insn = temp;
7074             }
7075         }
7076
7077       /* Don't overrun the bounds of the basic block.  */
7078       if (insn == head)
7079         break;
7080
7081       insn = PREV_INSN (insn);
7082     }
7083
7084   /* make sure these insns are scheduled last in their block */
7085   insn = last;
7086   if (insn != 0)
7087     while (insn != head)
7088       {
7089         insn = prev_nonnote_insn (insn);
7090
7091         if (INSN_REF_COUNT (insn) != 0)
7092           continue;
7093
7094         if (!find_insn_list (last, LOG_LINKS (insn)))
7095           add_dependence (last, insn, REG_DEP_ANTI);
7096         INSN_REF_COUNT (insn) = 1;
7097
7098         /* Skip over insns that are part of a group.  */
7099         while (SCHED_GROUP_P (insn))
7100           insn = prev_nonnote_insn (insn);
7101       }
7102 }
7103
7104 /* Compute bacward dependences inside BB.  In a multiple blocks region:
7105    (1) a bb is analyzed after its predecessors, and (2) the lists in
7106    effect at the end of bb (after analyzing for bb) are inherited by
7107    bb's successrs.
7108
7109    Specifically for reg-reg data dependences, the block insns are
7110    scanned by sched_analyze () top-to-bottom.  Two lists are
7111    naintained by sched_analyze (): reg_last_defs[] for register DEFs,
7112    and reg_last_uses[] for register USEs.
7113
7114    When analysis is completed for bb, we update for its successors:
7115    ;  - DEFS[succ] = Union (DEFS [succ], DEFS [bb])
7116    ;  - USES[succ] = Union (USES [succ], DEFS [bb])
7117
7118    The mechanism for computing mem-mem data dependence is very
7119    similar, and the result is interblock dependences in the region.  */
7120
7121 static void
7122 compute_block_backward_dependences (bb)
7123      int bb;
7124 {
7125   int b;
7126   rtx x;
7127   rtx head, tail;
7128   int max_reg = max_reg_num ();
7129
7130   b = BB_TO_BLOCK (bb);
7131
7132   if (current_nr_blocks == 1)
7133     {
7134       reg_last_uses = (rtx *) alloca (max_reg * sizeof (rtx));
7135       reg_last_sets = (rtx *) alloca (max_reg * sizeof (rtx));
7136
7137       bzero ((char *) reg_last_uses, max_reg * sizeof (rtx));
7138       bzero ((char *) reg_last_sets, max_reg * sizeof (rtx));
7139
7140       pending_read_insns = 0;
7141       pending_read_mems = 0;
7142       pending_write_insns = 0;
7143       pending_write_mems = 0;
7144       pending_lists_length = 0;
7145       last_function_call = 0;
7146       last_pending_memory_flush = 0;
7147       sched_before_next_call
7148         = gen_rtx_INSN (VOIDmode, 0, NULL_RTX, NULL_RTX,
7149                         NULL_RTX, 0, NULL_RTX, NULL_RTX);
7150       LOG_LINKS (sched_before_next_call) = 0;
7151     }
7152   else
7153     {
7154       reg_last_uses = bb_reg_last_uses[bb];
7155       reg_last_sets = bb_reg_last_sets[bb];
7156
7157       pending_read_insns = bb_pending_read_insns[bb];
7158       pending_read_mems = bb_pending_read_mems[bb];
7159       pending_write_insns = bb_pending_write_insns[bb];
7160       pending_write_mems = bb_pending_write_mems[bb];
7161       pending_lists_length = bb_pending_lists_length[bb];
7162       last_function_call = bb_last_function_call[bb];
7163       last_pending_memory_flush = bb_last_pending_memory_flush[bb];
7164
7165       sched_before_next_call = bb_sched_before_next_call[bb];
7166     }
7167
7168   /* do the analysis for this block */
7169   get_block_head_tail (bb, &head, &tail);
7170   sched_analyze (head, tail);
7171   add_branch_dependences (head, tail);
7172
7173   if (current_nr_blocks > 1)
7174     {
7175       int e, first_edge;
7176       int b_succ, bb_succ;
7177       int reg;
7178       rtx link_insn, link_mem;
7179       rtx u;
7180
7181       /* these lists should point to the right place, for correct freeing later.  */
7182       bb_pending_read_insns[bb] = pending_read_insns;
7183       bb_pending_read_mems[bb] = pending_read_mems;
7184       bb_pending_write_insns[bb] = pending_write_insns;
7185       bb_pending_write_mems[bb] = pending_write_mems;
7186
7187       /* bb's structures are inherited by it's successors */
7188       first_edge = e = OUT_EDGES (b);
7189       if (e > 0)
7190         do
7191           {
7192             b_succ = TO_BLOCK (e);
7193             bb_succ = BLOCK_TO_BB (b_succ);
7194
7195             /* only bbs "below" bb, in the same region, are interesting */
7196             if (CONTAINING_RGN (b) != CONTAINING_RGN (b_succ)
7197                 || bb_succ <= bb)
7198               {
7199                 e = NEXT_OUT (e);
7200                 continue;
7201               }
7202
7203             for (reg = 0; reg < max_reg; reg++)
7204               {
7205
7206                 /* reg-last-uses lists are inherited by bb_succ */
7207                 for (u = reg_last_uses[reg]; u; u = XEXP (u, 1))
7208                   {
7209                     if (find_insn_list (XEXP (u, 0), (bb_reg_last_uses[bb_succ])[reg]))
7210                       continue;
7211
7212                     (bb_reg_last_uses[bb_succ])[reg]
7213                       = alloc_INSN_LIST (XEXP (u, 0),
7214                                          (bb_reg_last_uses[bb_succ])[reg]);
7215                   }
7216
7217                 /* reg-last-defs lists are inherited by bb_succ */
7218                 for (u = reg_last_sets[reg]; u; u = XEXP (u, 1))
7219                   {
7220                     if (find_insn_list (XEXP (u, 0), (bb_reg_last_sets[bb_succ])[reg]))
7221                       continue;
7222
7223                     (bb_reg_last_sets[bb_succ])[reg]
7224                       = alloc_INSN_LIST (XEXP (u, 0),
7225                                          (bb_reg_last_sets[bb_succ])[reg]);
7226                   }
7227               }
7228
7229             /* mem read/write lists are inherited by bb_succ */
7230             link_insn = pending_read_insns;
7231             link_mem = pending_read_mems;
7232             while (link_insn)
7233               {
7234                 if (!(find_insn_mem_list (XEXP (link_insn, 0), XEXP (link_mem, 0),
7235                                           bb_pending_read_insns[bb_succ],
7236                                           bb_pending_read_mems[bb_succ])))
7237                   add_insn_mem_dependence (&bb_pending_read_insns[bb_succ],
7238                                            &bb_pending_read_mems[bb_succ],
7239                                    XEXP (link_insn, 0), XEXP (link_mem, 0));
7240                 link_insn = XEXP (link_insn, 1);
7241                 link_mem = XEXP (link_mem, 1);
7242               }
7243
7244             link_insn = pending_write_insns;
7245             link_mem = pending_write_mems;
7246             while (link_insn)
7247               {
7248                 if (!(find_insn_mem_list (XEXP (link_insn, 0), XEXP (link_mem, 0),
7249                                           bb_pending_write_insns[bb_succ],
7250                                           bb_pending_write_mems[bb_succ])))
7251                   add_insn_mem_dependence (&bb_pending_write_insns[bb_succ],
7252                                            &bb_pending_write_mems[bb_succ],
7253                                    XEXP (link_insn, 0), XEXP (link_mem, 0));
7254
7255                 link_insn = XEXP (link_insn, 1);
7256                 link_mem = XEXP (link_mem, 1);
7257               }
7258
7259             /* last_function_call is inherited by bb_succ */
7260             for (u = last_function_call; u; u = XEXP (u, 1))
7261               {
7262                 if (find_insn_list (XEXP (u, 0), bb_last_function_call[bb_succ]))
7263                   continue;
7264
7265                 bb_last_function_call[bb_succ]
7266                   = alloc_INSN_LIST (XEXP (u, 0),
7267                                      bb_last_function_call[bb_succ]);
7268               }
7269
7270             /* last_pending_memory_flush is inherited by bb_succ */
7271             for (u = last_pending_memory_flush; u; u = XEXP (u, 1))
7272               {
7273                 if (find_insn_list (XEXP (u, 0), bb_last_pending_memory_flush[bb_succ]))
7274                   continue;
7275
7276                 bb_last_pending_memory_flush[bb_succ]
7277                   = alloc_INSN_LIST (XEXP (u, 0),
7278                                      bb_last_pending_memory_flush[bb_succ]);
7279               }
7280
7281             /* sched_before_next_call is inherited by bb_succ */
7282             x = LOG_LINKS (sched_before_next_call);
7283             for (; x; x = XEXP (x, 1))
7284               add_dependence (bb_sched_before_next_call[bb_succ],
7285                               XEXP (x, 0), REG_DEP_ANTI);
7286
7287             e = NEXT_OUT (e);
7288           }
7289         while (e != first_edge);
7290     }
7291
7292   /* Free up the INSN_LISTs 
7293
7294      Note this loop is executed max_reg * nr_regions times.  It's first 
7295      implementation accounted for over 90% of the calls to free_list.
7296      The list was empty for the vast majority of those calls.  On the PA,
7297      not calling free_list in those cases improves -O2 compile times by
7298      3-5% on average.  */
7299   for (b = 0; b < max_reg; ++b)
7300     {
7301       if (reg_last_sets[b])
7302         free_list (&reg_last_sets[b], &unused_insn_list);
7303       if (reg_last_uses[b])
7304         free_list (&reg_last_uses[b], &unused_insn_list);
7305     }
7306
7307   /* Assert that we won't need bb_reg_last_* for this block anymore.  */
7308   if (current_nr_blocks > 1)
7309     {
7310       bb_reg_last_uses[bb] = (rtx *) NULL_RTX;
7311       bb_reg_last_sets[bb] = (rtx *) NULL_RTX;
7312     }
7313 }
7314
7315 /* Print dependences for debugging, callable from debugger */
7316
7317 void
7318 debug_dependencies ()
7319 {
7320   int bb;
7321
7322   fprintf (dump, ";;   --------------- forward dependences: ------------ \n");
7323   for (bb = 0; bb < current_nr_blocks; bb++)
7324     {
7325       if (1)
7326         {
7327           rtx head, tail;
7328           rtx next_tail;
7329           rtx insn;
7330
7331           get_block_head_tail (bb, &head, &tail);
7332           next_tail = NEXT_INSN (tail);
7333           fprintf (dump, "\n;;   --- Region Dependences --- b %d bb %d \n",
7334                    BB_TO_BLOCK (bb), bb);
7335
7336           fprintf (dump, ";;   %7s%6s%6s%6s%6s%6s%11s%6s\n",
7337           "insn", "code", "bb", "dep", "prio", "cost", "blockage", "units");
7338           fprintf (dump, ";;   %7s%6s%6s%6s%6s%6s%11s%6s\n",
7339           "----", "----", "--", "---", "----", "----", "--------", "-----");
7340           for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
7341             {
7342               rtx link;
7343               int unit, range;
7344
7345               if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
7346                 {
7347                   int n;
7348                   fprintf (dump, ";;   %6d ", INSN_UID (insn));
7349                   if (GET_CODE (insn) == NOTE)
7350                     {
7351                       n = NOTE_LINE_NUMBER (insn);
7352                       if (n < 0)
7353                         fprintf (dump, "%s\n", GET_NOTE_INSN_NAME (n));
7354                       else
7355                         fprintf (dump, "line %d, file %s\n", n,
7356                                  NOTE_SOURCE_FILE (insn));
7357                     }
7358                   else
7359                     fprintf (dump, " {%s}\n", GET_RTX_NAME (GET_CODE (insn)));
7360                   continue;
7361                 }
7362
7363               unit = insn_unit (insn);
7364               range = (unit < 0
7365                  || function_units[unit].blockage_range_function == 0) ? 0 :
7366                 function_units[unit].blockage_range_function (insn);
7367               fprintf (dump,
7368                        ";;   %s%5d%6d%6d%6d%6d%6d  %3d -%3d   ",
7369                        (SCHED_GROUP_P (insn) ? "+" : " "),
7370                        INSN_UID (insn),
7371                        INSN_CODE (insn),
7372                        INSN_BB (insn),
7373                        INSN_DEP_COUNT (insn),
7374                        INSN_PRIORITY (insn),
7375                        insn_cost (insn, 0, 0),
7376                        (int) MIN_BLOCKAGE_COST (range),
7377                        (int) MAX_BLOCKAGE_COST (range));
7378               insn_print_units (insn);
7379               fprintf (dump, "\t: ");
7380               for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
7381                 fprintf (dump, "%d ", INSN_UID (XEXP (link, 0)));
7382               fprintf (dump, "\n");
7383             }
7384         }
7385     }
7386   fprintf (dump, "\n");
7387 }
7388
7389 /* Set_priorities: compute priority of each insn in the block */
7390
7391 static int
7392 set_priorities (bb)
7393      int bb;
7394 {
7395   rtx insn;
7396   int n_insn;
7397
7398   rtx tail;
7399   rtx prev_head;
7400   rtx head;
7401
7402   get_block_head_tail (bb, &head, &tail);
7403   prev_head = PREV_INSN (head);
7404
7405   if (head == tail
7406       && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
7407     return 0;
7408
7409   n_insn = 0;
7410   for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
7411     {
7412
7413       if (GET_CODE (insn) == NOTE)
7414         continue;
7415
7416       if (!(SCHED_GROUP_P (insn)))
7417         n_insn++;
7418       (void) priority (insn);
7419     }
7420
7421   return n_insn;
7422 }
7423
7424 /* Make each element of VECTOR point at an rtx-vector,
7425    taking the space for all those rtx-vectors from SPACE.
7426    SPACE is of type (rtx *), but it is really as long as NELTS rtx-vectors.
7427    BYTES_PER_ELT is the number of bytes in one rtx-vector.
7428    (this is the same as init_regset_vector () in flow.c) */
7429
7430 static void
7431 init_rtx_vector (vector, space, nelts, bytes_per_elt)
7432      rtx **vector;
7433      rtx *space;
7434      int nelts;
7435      int bytes_per_elt;
7436 {
7437   register int i;
7438   register rtx *p = space;
7439
7440   for (i = 0; i < nelts; i++)
7441     {
7442       vector[i] = p;
7443       p += bytes_per_elt / sizeof (*p);
7444     }
7445 }
7446
7447 /* Schedule a region.  A region is either an inner loop, a loop-free
7448    subroutine, or a single basic block.  Each bb in the region is
7449    scheduled after its flow predecessors.  */
7450
7451 static void
7452 schedule_region (rgn)
7453      int rgn;
7454 {
7455   int bb;
7456   int rgn_n_insns = 0;
7457   int sched_rgn_n_insns = 0;
7458
7459   /* set variables for the current region */
7460   current_nr_blocks = RGN_NR_BLOCKS (rgn);
7461   current_blocks = RGN_BLOCKS (rgn);
7462
7463   reg_pending_sets = ALLOCA_REG_SET ();
7464   reg_pending_sets_all = 0;
7465
7466   /* initializations for region data dependence analyisis */
7467   if (current_nr_blocks > 1)
7468     {
7469       rtx *space;
7470       int maxreg = max_reg_num ();
7471
7472       bb_reg_last_uses = (rtx **) alloca (current_nr_blocks * sizeof (rtx *));
7473       space = (rtx *) alloca (current_nr_blocks * maxreg * sizeof (rtx));
7474       bzero ((char *) space, current_nr_blocks * maxreg * sizeof (rtx));
7475       init_rtx_vector (bb_reg_last_uses, space, current_nr_blocks, maxreg * sizeof (rtx *));
7476
7477       bb_reg_last_sets = (rtx **) alloca (current_nr_blocks * sizeof (rtx *));
7478       space = (rtx *) alloca (current_nr_blocks * maxreg * sizeof (rtx));
7479       bzero ((char *) space, current_nr_blocks * maxreg * sizeof (rtx));
7480       init_rtx_vector (bb_reg_last_sets, space, current_nr_blocks, maxreg * sizeof (rtx *));
7481
7482       bb_pending_read_insns = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7483       bb_pending_read_mems = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7484       bb_pending_write_insns = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7485       bb_pending_write_mems = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7486       bb_pending_lists_length = (int *) alloca (current_nr_blocks * sizeof (int));
7487       bb_last_pending_memory_flush = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7488       bb_last_function_call = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7489       bb_sched_before_next_call = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7490
7491       init_rgn_data_dependences (current_nr_blocks);
7492     }
7493
7494   /* compute LOG_LINKS */
7495   for (bb = 0; bb < current_nr_blocks; bb++)
7496     compute_block_backward_dependences (bb);
7497
7498   /* compute INSN_DEPEND */
7499   for (bb = current_nr_blocks - 1; bb >= 0; bb--)
7500     compute_block_forward_dependences (bb);
7501
7502   /* Delete line notes, compute live-regs at block end, and set priorities.  */
7503   dead_notes = 0;
7504   for (bb = 0; bb < current_nr_blocks; bb++)
7505     {
7506       if (reload_completed == 0)
7507         find_pre_sched_live (bb);
7508
7509       if (write_symbols != NO_DEBUG)
7510         {
7511           save_line_notes (bb);
7512           rm_line_notes (bb);
7513         }
7514
7515       rgn_n_insns += set_priorities (bb);
7516     }
7517
7518   /* compute interblock info: probabilities, split-edges, dominators, etc.  */
7519   if (current_nr_blocks > 1)
7520     {
7521       int i;
7522
7523       prob = (float *) alloca ((current_nr_blocks) * sizeof (float));
7524
7525       bbset_size = current_nr_blocks / HOST_BITS_PER_WIDE_INT + 1;
7526       dom = (bbset *) alloca (current_nr_blocks * sizeof (bbset));
7527       for (i = 0; i < current_nr_blocks; i++)
7528         {
7529           dom[i] = (bbset) alloca (bbset_size * sizeof (HOST_WIDE_INT));
7530           bzero ((char *) dom[i], bbset_size * sizeof (HOST_WIDE_INT));
7531         }
7532
7533       /* edge to bit */
7534       rgn_nr_edges = 0;
7535       edge_to_bit = (int *) alloca (nr_edges * sizeof (int));
7536       for (i = 1; i < nr_edges; i++)
7537         if (CONTAINING_RGN (FROM_BLOCK (i)) == rgn)
7538           EDGE_TO_BIT (i) = rgn_nr_edges++;
7539       rgn_edges = (int *) alloca (rgn_nr_edges * sizeof (int));
7540
7541       rgn_nr_edges = 0;
7542       for (i = 1; i < nr_edges; i++)
7543         if (CONTAINING_RGN (FROM_BLOCK (i)) == (rgn))
7544           rgn_edges[rgn_nr_edges++] = i;
7545
7546       /* split edges */
7547       edgeset_size = rgn_nr_edges / HOST_BITS_PER_WIDE_INT + 1;
7548       pot_split = (edgeset *) alloca (current_nr_blocks * sizeof (edgeset));
7549       ancestor_edges = (edgeset *) alloca (current_nr_blocks * sizeof (edgeset));
7550       for (i = 0; i < current_nr_blocks; i++)
7551         {
7552           pot_split[i] =
7553             (edgeset) alloca (edgeset_size * sizeof (HOST_WIDE_INT));
7554           bzero ((char *) pot_split[i],
7555                  edgeset_size * sizeof (HOST_WIDE_INT));
7556           ancestor_edges[i] =
7557             (edgeset) alloca (edgeset_size * sizeof (HOST_WIDE_INT));
7558           bzero ((char *) ancestor_edges[i],
7559                  edgeset_size * sizeof (HOST_WIDE_INT));
7560         }
7561
7562       /* compute probabilities, dominators, split_edges */
7563       for (bb = 0; bb < current_nr_blocks; bb++)
7564         compute_dom_prob_ps (bb);
7565     }
7566
7567   /* now we can schedule all blocks */
7568   for (bb = 0; bb < current_nr_blocks; bb++)
7569     {
7570       sched_rgn_n_insns += schedule_block (bb, rgn_n_insns);
7571
7572 #ifdef USE_C_ALLOCA
7573       alloca (0);
7574 #endif
7575     }
7576
7577   /* sanity check: verify that all region insns were scheduled */
7578   if (sched_rgn_n_insns != rgn_n_insns)
7579     abort ();
7580
7581   /* update register life and usage information */
7582   if (reload_completed == 0)
7583     {
7584       for (bb = current_nr_blocks - 1; bb >= 0; bb--)
7585         find_post_sched_live (bb);
7586
7587       if (current_nr_blocks <= 1)
7588         /* Sanity check.  There should be no REG_DEAD notes leftover at the end.
7589            In practice, this can occur as the result of bugs in flow, combine.c,
7590            and/or sched.c.  The values of the REG_DEAD notes remaining are
7591            meaningless, because dead_notes is just used as a free list.  */
7592         if (dead_notes != 0)
7593           abort ();
7594     }
7595
7596   /* restore line notes.  */
7597   if (write_symbols != NO_DEBUG)
7598     {
7599       for (bb = 0; bb < current_nr_blocks; bb++)
7600         restore_line_notes (bb);
7601     }
7602
7603   /* Done with this region */
7604   free_pending_lists ();
7605
7606   FREE_REG_SET (reg_pending_sets);
7607 }
7608
7609 /* Subroutine of split_hard_reg_notes.  Searches X for any reference to
7610    REGNO, returning the rtx of the reference found if any.  Otherwise,
7611    returns 0.  */
7612
7613 static rtx
7614 regno_use_in (regno, x)
7615      int regno;
7616      rtx x;
7617 {
7618   register char *fmt;
7619   int i, j;
7620   rtx tem;
7621
7622   if (GET_CODE (x) == REG && REGNO (x) == regno)
7623     return x;
7624
7625   fmt = GET_RTX_FORMAT (GET_CODE (x));
7626   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
7627     {
7628       if (fmt[i] == 'e')
7629         {
7630           if ((tem = regno_use_in (regno, XEXP (x, i))))
7631             return tem;
7632         }
7633       else if (fmt[i] == 'E')
7634         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7635           if ((tem = regno_use_in (regno, XVECEXP (x, i, j))))
7636             return tem;
7637     }
7638
7639   return 0;
7640 }
7641
7642 /* Subroutine of update_flow_info.  Determines whether any new REG_NOTEs are
7643    needed for the hard register mentioned in the note.  This can happen
7644    if the reference to the hard register in the original insn was split into
7645    several smaller hard register references in the split insns.  */
7646
7647 static void
7648 split_hard_reg_notes (note, first, last)
7649      rtx note, first, last;
7650 {
7651   rtx reg, temp, link;
7652   int n_regs, i, new_reg;
7653   rtx insn;
7654
7655   /* Assume that this is a REG_DEAD note.  */
7656   if (REG_NOTE_KIND (note) != REG_DEAD)
7657     abort ();
7658
7659   reg = XEXP (note, 0);
7660
7661   n_regs = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
7662
7663   for (i = 0; i < n_regs; i++)
7664     {
7665       new_reg = REGNO (reg) + i;
7666
7667       /* Check for references to new_reg in the split insns.  */
7668       for (insn = last;; insn = PREV_INSN (insn))
7669         {
7670           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
7671               && (temp = regno_use_in (new_reg, PATTERN (insn))))
7672             {
7673               /* Create a new reg dead note ere.  */
7674               link = alloc_EXPR_LIST (REG_DEAD, temp, REG_NOTES (insn));
7675               REG_NOTES (insn) = link;
7676
7677               /* If killed multiple registers here, then add in the excess.  */
7678               i += HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)) - 1;
7679
7680               break;
7681             }
7682           /* It isn't mentioned anywhere, so no new reg note is needed for
7683              this register.  */
7684           if (insn == first)
7685             break;
7686         }
7687     }
7688 }
7689
7690 /* Subroutine of update_flow_info.  Determines whether a SET or CLOBBER in an
7691    insn created by splitting needs a REG_DEAD or REG_UNUSED note added.  */
7692
7693 static void
7694 new_insn_dead_notes (pat, insn, last, orig_insn)
7695      rtx pat, insn, last, orig_insn;
7696 {
7697   rtx dest, tem, set;
7698
7699   /* PAT is either a CLOBBER or a SET here.  */
7700   dest = XEXP (pat, 0);
7701
7702   while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
7703          || GET_CODE (dest) == STRICT_LOW_PART
7704          || GET_CODE (dest) == SIGN_EXTRACT)
7705     dest = XEXP (dest, 0);
7706
7707   if (GET_CODE (dest) == REG)
7708     {
7709       /* If the original insn already used this register, we may not add new
7710          notes for it.  One example for a split that needs this test is
7711          when a multi-word memory access with register-indirect addressing
7712          is split into multiple memory accesses with auto-increment and
7713          one adjusting add instruction for the address register.  */
7714       if (reg_referenced_p (dest, PATTERN (orig_insn)))
7715         return;
7716       for (tem = last; tem != insn; tem = PREV_INSN (tem))
7717         {
7718           if (GET_RTX_CLASS (GET_CODE (tem)) == 'i'
7719               && reg_overlap_mentioned_p (dest, PATTERN (tem))
7720               && (set = single_set (tem)))
7721             {
7722               rtx tem_dest = SET_DEST (set);
7723
7724               while (GET_CODE (tem_dest) == ZERO_EXTRACT
7725                      || GET_CODE (tem_dest) == SUBREG
7726                      || GET_CODE (tem_dest) == STRICT_LOW_PART
7727                      || GET_CODE (tem_dest) == SIGN_EXTRACT)
7728                 tem_dest = XEXP (tem_dest, 0);
7729
7730               if (!rtx_equal_p (tem_dest, dest))
7731                 {
7732                   /* Use the same scheme as combine.c, don't put both REG_DEAD
7733                      and REG_UNUSED notes on the same insn.  */
7734                   if (!find_regno_note (tem, REG_UNUSED, REGNO (dest))
7735                       && !find_regno_note (tem, REG_DEAD, REGNO (dest)))
7736                     {
7737                       rtx note = alloc_EXPR_LIST (REG_DEAD, dest,
7738                                                   REG_NOTES (tem));
7739                       REG_NOTES (tem) = note;
7740                     }
7741                   /* The reg only dies in one insn, the last one that uses
7742                      it.  */
7743                   break;
7744                 }
7745               else if (reg_overlap_mentioned_p (dest, SET_SRC (set)))
7746                 /* We found an instruction that both uses the register,
7747                    and sets it, so no new REG_NOTE is needed for this set.  */
7748                 break;
7749             }
7750         }
7751       /* If this is a set, it must die somewhere, unless it is the dest of
7752          the original insn, and hence is live after the original insn.  Abort
7753          if it isn't supposed to be live after the original insn.
7754
7755          If this is a clobber, then just add a REG_UNUSED note.  */
7756       if (tem == insn)
7757         {
7758           int live_after_orig_insn = 0;
7759           rtx pattern = PATTERN (orig_insn);
7760           int i;
7761
7762           if (GET_CODE (pat) == CLOBBER)
7763             {
7764               rtx note = alloc_EXPR_LIST (REG_UNUSED, dest, REG_NOTES (insn));
7765               REG_NOTES (insn) = note;
7766               return;
7767             }
7768
7769           /* The original insn could have multiple sets, so search the
7770              insn for all sets.  */
7771           if (GET_CODE (pattern) == SET)
7772             {
7773               if (reg_overlap_mentioned_p (dest, SET_DEST (pattern)))
7774                 live_after_orig_insn = 1;
7775             }
7776           else if (GET_CODE (pattern) == PARALLEL)
7777             {
7778               for (i = 0; i < XVECLEN (pattern, 0); i++)
7779                 if (GET_CODE (XVECEXP (pattern, 0, i)) == SET
7780                     && reg_overlap_mentioned_p (dest,
7781                                                 SET_DEST (XVECEXP (pattern,
7782                                                                    0, i))))
7783                   live_after_orig_insn = 1;
7784             }
7785
7786           if (!live_after_orig_insn)
7787             abort ();
7788         }
7789     }
7790 }
7791
7792 /* Subroutine of update_flow_info.  Update the value of reg_n_sets for all
7793    registers modified by X.  INC is -1 if the containing insn is being deleted,
7794    and is 1 if the containing insn is a newly generated insn.  */
7795
7796 static void
7797 update_n_sets (x, inc)
7798      rtx x;
7799      int inc;
7800 {
7801   rtx dest = SET_DEST (x);
7802
7803   while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
7804       || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
7805     dest = SUBREG_REG (dest);
7806
7807   if (GET_CODE (dest) == REG)
7808     {
7809       int regno = REGNO (dest);
7810
7811       if (regno < FIRST_PSEUDO_REGISTER)
7812         {
7813           register int i;
7814           int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (dest));
7815
7816           for (i = regno; i < endregno; i++)
7817             REG_N_SETS (i) += inc;
7818         }
7819       else
7820         REG_N_SETS (regno) += inc;
7821     }
7822 }
7823
7824 /* Updates all flow-analysis related quantities (including REG_NOTES) for
7825    the insns from FIRST to LAST inclusive that were created by splitting
7826    ORIG_INSN.  NOTES are the original REG_NOTES.  */
7827
7828 static void
7829 update_flow_info (notes, first, last, orig_insn)
7830      rtx notes;
7831      rtx first, last;
7832      rtx orig_insn;
7833 {
7834   rtx insn, note;
7835   rtx next;
7836   rtx orig_dest, temp;
7837   rtx set;
7838
7839   /* Get and save the destination set by the original insn.  */
7840
7841   orig_dest = single_set (orig_insn);
7842   if (orig_dest)
7843     orig_dest = SET_DEST (orig_dest);
7844
7845   /* Move REG_NOTES from the original insn to where they now belong.  */
7846
7847   for (note = notes; note; note = next)
7848     {
7849       next = XEXP (note, 1);
7850       switch (REG_NOTE_KIND (note))
7851         {
7852         case REG_DEAD:
7853         case REG_UNUSED:
7854           /* Move these notes from the original insn to the last new insn where
7855              the register is now set.  */
7856
7857           for (insn = last;; insn = PREV_INSN (insn))
7858             {
7859               if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
7860                   && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
7861                 {
7862                   /* If this note refers to a multiple word hard register, it
7863                      may have been split into several smaller hard register
7864                      references, so handle it specially.  */
7865                   temp = XEXP (note, 0);
7866                   if (REG_NOTE_KIND (note) == REG_DEAD
7867                       && GET_CODE (temp) == REG
7868                       && REGNO (temp) < FIRST_PSEUDO_REGISTER
7869                       && HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)) > 1)
7870                     split_hard_reg_notes (note, first, last);
7871                   else
7872                     {
7873                       XEXP (note, 1) = REG_NOTES (insn);
7874                       REG_NOTES (insn) = note;
7875                     }
7876
7877                   /* Sometimes need to convert REG_UNUSED notes to REG_DEAD
7878                      notes.  */
7879                   /* ??? This won't handle multiple word registers correctly,
7880                      but should be good enough for now.  */
7881                   if (REG_NOTE_KIND (note) == REG_UNUSED
7882                       && GET_CODE (XEXP (note, 0)) != SCRATCH
7883                       && !dead_or_set_p (insn, XEXP (note, 0)))
7884                     PUT_REG_NOTE_KIND (note, REG_DEAD);
7885
7886                   /* The reg only dies in one insn, the last one that uses
7887                      it.  */
7888                   break;
7889                 }
7890               /* It must die somewhere, fail it we couldn't find where it died.
7891
7892                  If this is a REG_UNUSED note, then it must be a temporary
7893                  register that was not needed by this instantiation of the
7894                  pattern, so we can safely ignore it.  */
7895               if (insn == first)
7896                 {
7897                   /* After reload, REG_DEAD notes come sometimes an
7898                      instruction after the register actually dies.  */
7899                   if (reload_completed && REG_NOTE_KIND (note) == REG_DEAD)
7900                     {
7901                       XEXP (note, 1) = REG_NOTES (insn);
7902                       REG_NOTES (insn) = note;
7903                       break;
7904                     }
7905                         
7906                   if (REG_NOTE_KIND (note) != REG_UNUSED)
7907                     abort ();
7908
7909                   break;
7910                 }
7911             }
7912           break;
7913
7914         case REG_WAS_0:
7915           /* If the insn that set the register to 0 was deleted, this
7916              note cannot be relied on any longer.  The destination might
7917              even have been moved to memory.
7918              This was observed for SH4 with execute/920501-6.c compilation,
7919              -O2 -fomit-frame-pointer -finline-functions .  */
7920           if (GET_CODE (XEXP (note, 0)) == NOTE
7921               || INSN_DELETED_P (XEXP (note, 0)))
7922             break;
7923           /* This note applies to the dest of the original insn.  Find the
7924              first new insn that now has the same dest, and move the note
7925              there.  */
7926
7927           if (!orig_dest)
7928             abort ();
7929
7930           for (insn = first;; insn = NEXT_INSN (insn))
7931             {
7932               if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
7933                   && (temp = single_set (insn))
7934                   && rtx_equal_p (SET_DEST (temp), orig_dest))
7935                 {
7936                   XEXP (note, 1) = REG_NOTES (insn);
7937                   REG_NOTES (insn) = note;
7938                   /* The reg is only zero before one insn, the first that
7939                      uses it.  */
7940                   break;
7941                 }
7942               /* If this note refers to a multiple word hard
7943                  register, it may have been split into several smaller
7944                  hard register references.  We could split the notes,
7945                  but simply dropping them is good enough.  */
7946               if (GET_CODE (orig_dest) == REG
7947                   && REGNO (orig_dest) < FIRST_PSEUDO_REGISTER
7948                   && HARD_REGNO_NREGS (REGNO (orig_dest),
7949                                        GET_MODE (orig_dest)) > 1)
7950                     break;
7951               /* It must be set somewhere, fail if we couldn't find where it
7952                  was set.  */
7953               if (insn == last)
7954                 abort ();
7955             }
7956           break;
7957
7958         case REG_EQUAL:
7959         case REG_EQUIV:
7960           /* A REG_EQUIV or REG_EQUAL note on an insn with more than one
7961              set is meaningless.  Just drop the note.  */
7962           if (!orig_dest)
7963             break;
7964
7965         case REG_NO_CONFLICT:
7966           /* These notes apply to the dest of the original insn.  Find the last
7967              new insn that now has the same dest, and move the note there.  */
7968
7969           if (!orig_dest)
7970             abort ();
7971
7972           for (insn = last;; insn = PREV_INSN (insn))
7973             {
7974               if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
7975                   && (temp = single_set (insn))
7976                   && rtx_equal_p (SET_DEST (temp), orig_dest))
7977                 {
7978                   XEXP (note, 1) = REG_NOTES (insn);
7979                   REG_NOTES (insn) = note;
7980                   /* Only put this note on one of the new insns.  */
7981                   break;
7982                 }
7983
7984               /* The original dest must still be set someplace.  Abort if we
7985                  couldn't find it.  */
7986               if (insn == first)
7987                 {
7988                   /* However, if this note refers to a multiple word hard
7989                      register, it may have been split into several smaller
7990                      hard register references.  We could split the notes,
7991                      but simply dropping them is good enough.  */
7992                   if (GET_CODE (orig_dest) == REG
7993                       && REGNO (orig_dest) < FIRST_PSEUDO_REGISTER
7994                       && HARD_REGNO_NREGS (REGNO (orig_dest),
7995                                            GET_MODE (orig_dest)) > 1)
7996                     break;
7997                   /* Likewise for multi-word memory references.  */
7998                   if (GET_CODE (orig_dest) == MEM
7999                       && SIZE_FOR_MODE (orig_dest) > UNITS_PER_WORD)
8000                     break;
8001                   abort ();
8002                 }
8003             }
8004           break;
8005
8006         case REG_LIBCALL:
8007           /* Move a REG_LIBCALL note to the first insn created, and update
8008              the corresponding REG_RETVAL note.  */
8009           XEXP (note, 1) = REG_NOTES (first);
8010           REG_NOTES (first) = note;
8011
8012           insn = XEXP (note, 0);
8013           note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
8014           if (note)
8015             XEXP (note, 0) = first;
8016           break;
8017
8018         case REG_EXEC_COUNT:
8019           /* Move a REG_EXEC_COUNT note to the first insn created.  */
8020           XEXP (note, 1) = REG_NOTES (first);
8021           REG_NOTES (first) = note;
8022           break;
8023
8024         case REG_RETVAL:
8025           /* Move a REG_RETVAL note to the last insn created, and update
8026              the corresponding REG_LIBCALL note.  */
8027           XEXP (note, 1) = REG_NOTES (last);
8028           REG_NOTES (last) = note;
8029
8030           insn = XEXP (note, 0);
8031           note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
8032           if (note)
8033             XEXP (note, 0) = last;
8034           break;
8035
8036         case REG_NONNEG:
8037         case REG_BR_PROB:
8038           /* This should be moved to whichever instruction is a JUMP_INSN.  */
8039
8040           for (insn = last;; insn = PREV_INSN (insn))
8041             {
8042               if (GET_CODE (insn) == JUMP_INSN)
8043                 {
8044                   XEXP (note, 1) = REG_NOTES (insn);
8045                   REG_NOTES (insn) = note;
8046                   /* Only put this note on one of the new insns.  */
8047                   break;
8048                 }
8049               /* Fail if we couldn't find a JUMP_INSN.  */
8050               if (insn == first)
8051                 abort ();
8052             }
8053           break;
8054
8055         case REG_INC:
8056           /* reload sometimes leaves obsolete REG_INC notes around.  */
8057           if (reload_completed)
8058             break;
8059           /* This should be moved to whichever instruction now has the
8060              increment operation.  */
8061           abort ();
8062
8063         case REG_LABEL:
8064           /* Should be moved to the new insn(s) which use the label.  */
8065           for (insn = first; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
8066             if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
8067                 && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
8068               {
8069                 REG_NOTES (insn) = alloc_EXPR_LIST (REG_LABEL,
8070                                                     XEXP (note, 0),
8071                                                     REG_NOTES (insn));
8072               }
8073           break;
8074
8075         case REG_CC_SETTER:
8076         case REG_CC_USER:
8077           /* These two notes will never appear until after reorg, so we don't
8078              have to handle them here.  */
8079         default:
8080           abort ();
8081         }
8082     }
8083
8084   /* Each new insn created, except the last, has a new set.  If the destination
8085      is a register, then this reg is now live across several insns, whereas
8086      previously the dest reg was born and died within the same insn.  To
8087      reflect this, we now need a REG_DEAD note on the insn where this
8088      dest reg dies.
8089
8090      Similarly, the new insns may have clobbers that need REG_UNUSED notes.  */
8091
8092   for (insn = first; insn != last; insn = NEXT_INSN (insn))
8093     {
8094       rtx pat;
8095       int i;
8096
8097       pat = PATTERN (insn);
8098       if (GET_CODE (pat) == SET || GET_CODE (pat) == CLOBBER)
8099         new_insn_dead_notes (pat, insn, last, orig_insn);
8100       else if (GET_CODE (pat) == PARALLEL)
8101         {
8102           for (i = 0; i < XVECLEN (pat, 0); i++)
8103             if (GET_CODE (XVECEXP (pat, 0, i)) == SET
8104                 || GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER)
8105               new_insn_dead_notes (XVECEXP (pat, 0, i), insn, last, orig_insn);
8106         }
8107     }
8108
8109   /* If any insn, except the last, uses the register set by the last insn,
8110      then we need a new REG_DEAD note on that insn.  In this case, there
8111      would not have been a REG_DEAD note for this register in the original
8112      insn because it was used and set within one insn.  */
8113
8114   set = single_set (last);
8115   if (set)
8116     {
8117       rtx dest = SET_DEST (set);
8118
8119       while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
8120              || GET_CODE (dest) == STRICT_LOW_PART
8121              || GET_CODE (dest) == SIGN_EXTRACT)
8122         dest = XEXP (dest, 0);
8123
8124       if (GET_CODE (dest) == REG
8125           /* Global registers are always live, so the code below does not
8126              apply to them.  */
8127           && (REGNO (dest) >= FIRST_PSEUDO_REGISTER
8128               || ! global_regs[REGNO (dest)]))
8129         {
8130           rtx stop_insn = PREV_INSN (first);
8131
8132           /* If the last insn uses the register that it is setting, then
8133              we don't want to put a REG_DEAD note there.  Search backwards
8134              to find the first insn that sets but does not use DEST.  */
8135
8136           insn = last;
8137           if (reg_overlap_mentioned_p (dest, SET_SRC (set)))
8138             {
8139               for (insn = PREV_INSN (insn); insn != first;
8140                    insn = PREV_INSN (insn))
8141                 {
8142                   if ((set = single_set (insn))
8143                       && reg_mentioned_p (dest, SET_DEST (set))
8144                       && ! reg_overlap_mentioned_p (dest, SET_SRC (set)))
8145                     break;
8146                 }
8147             }
8148
8149           /* Now find the first insn that uses but does not set DEST.  */
8150
8151           for (insn = PREV_INSN (insn); insn != stop_insn;
8152                insn = PREV_INSN (insn))
8153             {
8154               if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
8155                   && reg_mentioned_p (dest, PATTERN (insn))
8156                   && (set = single_set (insn)))
8157                 {
8158                   rtx insn_dest = SET_DEST (set);
8159
8160                   while (GET_CODE (insn_dest) == ZERO_EXTRACT
8161                          || GET_CODE (insn_dest) == SUBREG
8162                          || GET_CODE (insn_dest) == STRICT_LOW_PART
8163                          || GET_CODE (insn_dest) == SIGN_EXTRACT)
8164                     insn_dest = XEXP (insn_dest, 0);
8165
8166                   if (insn_dest != dest)
8167                     {
8168                       note = alloc_EXPR_LIST (REG_DEAD, dest, REG_NOTES (insn));
8169                       REG_NOTES (insn) = note;
8170                       /* The reg only dies in one insn, the last one
8171                          that uses it.  */
8172                       break;
8173                     }
8174                 }
8175             }
8176         }
8177     }
8178
8179   /* If the original dest is modifying a multiple register target, and the
8180      original instruction was split such that the original dest is now set
8181      by two or more SUBREG sets, then the split insns no longer kill the
8182      destination of the original insn.
8183
8184      In this case, if there exists an instruction in the same basic block,
8185      before the split insn, which uses the original dest, and this use is
8186      killed by the original insn, then we must remove the REG_DEAD note on
8187      this insn, because it is now superfluous.
8188
8189      This does not apply when a hard register gets split, because the code
8190      knows how to handle overlapping hard registers properly.  */
8191   if (orig_dest && GET_CODE (orig_dest) == REG)
8192     {
8193       int found_orig_dest = 0;
8194       int found_split_dest = 0;
8195
8196       for (insn = first;; insn = NEXT_INSN (insn))
8197         {
8198           rtx pat;
8199           int i;
8200
8201           /* I'm not sure if this can happen, but let's be safe.  */
8202           if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
8203             continue;
8204
8205           pat = PATTERN (insn);
8206           i = GET_CODE (pat) == PARALLEL ? XVECLEN (pat, 0) : 0;
8207           set = pat;
8208
8209           for (;;)
8210             {
8211               if (GET_CODE (set) == SET)
8212                 {
8213                   if (GET_CODE (SET_DEST (set)) == REG
8214                       && REGNO (SET_DEST (set)) == REGNO (orig_dest))
8215                     {
8216                       found_orig_dest = 1;
8217                       break;
8218                     }
8219                   else if (GET_CODE (SET_DEST (set)) == SUBREG
8220                            && SUBREG_REG (SET_DEST (set)) == orig_dest)
8221                     {
8222                       found_split_dest = 1;
8223                       break;
8224                     }
8225                 }
8226               if (--i < 0)
8227                 break;
8228               set = XVECEXP (pat, 0, i);
8229             }
8230
8231           if (insn == last)
8232             break;
8233         }
8234
8235       if (found_split_dest)
8236         {
8237           /* Search backwards from FIRST, looking for the first insn that uses
8238              the original dest.  Stop if we pass a CODE_LABEL or a JUMP_INSN.
8239              If we find an insn, and it has a REG_DEAD note, then delete the
8240              note.  */
8241
8242           for (insn = first; insn; insn = PREV_INSN (insn))
8243             {
8244               if (GET_CODE (insn) == CODE_LABEL
8245                   || GET_CODE (insn) == JUMP_INSN)
8246                 break;
8247               else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
8248                        && reg_mentioned_p (orig_dest, insn))
8249                 {
8250                   note = find_regno_note (insn, REG_DEAD, REGNO (orig_dest));
8251                   if (note)
8252                     remove_note (insn, note);
8253                 }
8254             }
8255         }
8256       else if (!found_orig_dest)
8257         {
8258           /* This should never happen.  */
8259           abort ();
8260         }
8261     }
8262
8263   /* Update reg_n_sets.  This is necessary to prevent local alloc from
8264      converting REG_EQUAL notes to REG_EQUIV when splitting has modified
8265      a reg from set once to set multiple times.  */
8266
8267   {
8268     rtx x = PATTERN (orig_insn);
8269     RTX_CODE code = GET_CODE (x);
8270
8271     if (code == SET || code == CLOBBER)
8272       update_n_sets (x, -1);
8273     else if (code == PARALLEL)
8274       {
8275         int i;
8276         for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
8277           {
8278             code = GET_CODE (XVECEXP (x, 0, i));
8279             if (code == SET || code == CLOBBER)
8280               update_n_sets (XVECEXP (x, 0, i), -1);
8281           }
8282       }
8283
8284     for (insn = first;; insn = NEXT_INSN (insn))
8285       {
8286         x = PATTERN (insn);
8287         code = GET_CODE (x);
8288
8289         if (code == SET || code == CLOBBER)
8290           update_n_sets (x, 1);
8291         else if (code == PARALLEL)
8292           {
8293             int i;
8294             for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
8295               {
8296                 code = GET_CODE (XVECEXP (x, 0, i));
8297                 if (code == SET || code == CLOBBER)
8298                   update_n_sets (XVECEXP (x, 0, i), 1);
8299               }
8300           }
8301
8302         if (insn == last)
8303           break;
8304       }
8305   }
8306 }
8307
8308 /* Do the splitting of insns in the block b.  */
8309
8310 static void
8311 split_block_insns (b)
8312      int b;
8313 {
8314   rtx insn, next;
8315
8316   for (insn = basic_block_head[b];; insn = next)
8317     {
8318       rtx set, last, first, notes;
8319
8320       /* Can't use `next_real_insn' because that
8321          might go across CODE_LABELS and short-out basic blocks.  */
8322       next = NEXT_INSN (insn);
8323       if (GET_CODE (insn) != INSN)
8324         {
8325           if (insn == basic_block_end[b])
8326             break;
8327
8328           continue;
8329         }
8330
8331       /* Don't split no-op move insns.  These should silently disappear
8332          later in final.  Splitting such insns would break the code
8333          that handles REG_NO_CONFLICT blocks.  */
8334       set = single_set (insn);
8335       if (set && rtx_equal_p (SET_SRC (set), SET_DEST (set)))
8336         {
8337           if (insn == basic_block_end[b])
8338             break;
8339
8340           /* Nops get in the way while scheduling, so delete them now if
8341              register allocation has already been done.  It is too risky
8342              to try to do this before register allocation, and there are
8343              unlikely to be very many nops then anyways.  */
8344           if (reload_completed)
8345             {
8346               PUT_CODE (insn, NOTE);
8347               NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8348               NOTE_SOURCE_FILE (insn) = 0;
8349             }
8350
8351           continue;
8352         }
8353
8354       /* Split insns here to get max fine-grain parallelism.  */
8355       first = PREV_INSN (insn);
8356       notes = REG_NOTES (insn);
8357       last = try_split (PATTERN (insn), insn, 1);
8358       if (last != insn)
8359         {
8360           /* try_split returns the NOTE that INSN became.  */
8361           first = NEXT_INSN (first);
8362           update_flow_info (notes, first, last, insn);
8363
8364           PUT_CODE (insn, NOTE);
8365           NOTE_SOURCE_FILE (insn) = 0;
8366           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8367           if (insn == basic_block_head[b])
8368             basic_block_head[b] = first;
8369           if (insn == basic_block_end[b])
8370             {
8371               basic_block_end[b] = last;
8372               break;
8373             }
8374         }
8375
8376       if (insn == basic_block_end[b])
8377         break;
8378     }
8379 }
8380
8381 /* The one entry point in this file.  DUMP_FILE is the dump file for
8382    this pass.  */
8383
8384 void
8385 schedule_insns (dump_file)
8386      FILE *dump_file;
8387 {
8388
8389   int max_uid;
8390   int b;
8391   rtx insn;
8392   int rgn;
8393
8394   int luid;
8395
8396   /* disable speculative loads in their presence if cc0 defined */
8397 #ifdef HAVE_cc0
8398   flag_schedule_speculative_load = 0;
8399 #endif
8400
8401   /* Taking care of this degenerate case makes the rest of
8402      this code simpler.  */
8403   if (n_basic_blocks == 0)
8404     return;
8405
8406   /* set dump and sched_verbose for the desired debugging output.  If no
8407      dump-file was specified, but -fsched-verbose-N (any N), print to stderr.
8408      For -fsched-verbose-N, N>=10, print everything to stderr.  */
8409   sched_verbose = sched_verbose_param;
8410   if (sched_verbose_param == 0 && dump_file)
8411     sched_verbose = 1;
8412   dump = ((sched_verbose_param >= 10 || !dump_file) ? stderr : dump_file);
8413
8414   nr_inter = 0;
8415   nr_spec = 0;
8416
8417   /* Initialize the unused_*_lists.  We can't use the ones left over from
8418      the previous function, because gcc has freed that memory.  We can use
8419      the ones left over from the first sched pass in the second pass however,
8420      so only clear them on the first sched pass.  The first pass is before
8421      reload if flag_schedule_insns is set, otherwise it is afterwards.  */
8422
8423   if (reload_completed == 0 || !flag_schedule_insns)
8424     {
8425       unused_insn_list = 0;
8426       unused_expr_list = 0;
8427     }
8428
8429   /* initialize issue_rate */
8430   issue_rate = ISSUE_RATE;
8431
8432   /* do the splitting first for all blocks */
8433   for (b = 0; b < n_basic_blocks; b++)
8434     split_block_insns (b);
8435
8436   max_uid = (get_max_uid () + 1);
8437
8438   cant_move = (char *) alloca (max_uid * sizeof (char));
8439   bzero ((char *) cant_move, max_uid * sizeof (char));
8440
8441   fed_by_spec_load = (char *) alloca (max_uid * sizeof (char));
8442   bzero ((char *) fed_by_spec_load, max_uid * sizeof (char));
8443
8444   is_load_insn = (char *) alloca (max_uid * sizeof (char));
8445   bzero ((char *) is_load_insn, max_uid * sizeof (char));
8446
8447   insn_orig_block = (int *) alloca (max_uid * sizeof (int));
8448   insn_luid = (int *) alloca (max_uid * sizeof (int));
8449
8450   luid = 0;
8451   for (b = 0; b < n_basic_blocks; b++)
8452     for (insn = basic_block_head[b];; insn = NEXT_INSN (insn))
8453       {
8454         INSN_BLOCK (insn) = b;
8455         INSN_LUID (insn) = luid++;
8456
8457         if (insn == basic_block_end[b])
8458           break;
8459       }
8460
8461   /* after reload, remove inter-blocks dependences computed before reload.  */
8462   if (reload_completed)
8463     {
8464       int b;
8465       rtx insn;
8466
8467       for (b = 0; b < n_basic_blocks; b++)
8468         for (insn = basic_block_head[b];; insn = NEXT_INSN (insn))
8469           {
8470             rtx link, prev;
8471
8472             if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
8473               {
8474                 prev = NULL_RTX;
8475                 link = LOG_LINKS (insn);
8476                 while (link)
8477                   {
8478                     rtx x = XEXP (link, 0);
8479
8480                     if (INSN_BLOCK (x) != b)
8481                       {
8482                         remove_dependence (insn, x);
8483                         link = prev ? XEXP (prev, 1) : LOG_LINKS (insn);
8484                       }
8485                     else
8486                       prev = link, link = XEXP (prev, 1);
8487                   }
8488               }
8489
8490             if (insn == basic_block_end[b])
8491               break;
8492           }
8493     }
8494
8495   nr_regions = 0;
8496   rgn_table = (region *) alloca ((n_basic_blocks) * sizeof (region));
8497   rgn_bb_table = (int *) alloca ((n_basic_blocks) * sizeof (int));
8498   block_to_bb = (int *) alloca ((n_basic_blocks) * sizeof (int));
8499   containing_rgn = (int *) alloca ((n_basic_blocks) * sizeof (int));
8500
8501   /* compute regions for scheduling */
8502   if (reload_completed
8503       || n_basic_blocks == 1
8504       || !flag_schedule_interblock)
8505     {
8506       find_single_block_region ();
8507     }
8508   else
8509     {
8510       /* verify that a 'good' control flow graph can be built */
8511       if (is_cfg_nonregular ())
8512         {
8513           find_single_block_region ();
8514         }
8515       else
8516         {
8517           int_list_ptr *s_preds, *s_succs;
8518           int *num_preds, *num_succs;
8519           sbitmap *dom, *pdom;
8520
8521           s_preds = (int_list_ptr *) alloca (n_basic_blocks
8522                                              * sizeof (int_list_ptr));
8523           s_succs = (int_list_ptr *) alloca (n_basic_blocks
8524                                              * sizeof (int_list_ptr));
8525           num_preds = (int *) alloca (n_basic_blocks * sizeof (int));
8526           num_succs = (int *) alloca (n_basic_blocks * sizeof (int));
8527           dom = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
8528           pdom = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
8529
8530           /* The scheduler runs after flow; therefore, we can't blindly call
8531              back into find_basic_blocks since doing so could invalidate the
8532              info in basic_block_live_at_start.
8533
8534              Consider a block consisting entirely of dead stores; after life
8535              analysis it would be a block of NOTE_INSN_DELETED notes.  If
8536              we call find_basic_blocks again, then the block would be removed
8537              entirely and invalidate our the register live information.
8538
8539              We could (should?) recompute register live information.  Doing
8540              so may even be beneficial.  */
8541
8542           compute_preds_succs (s_preds, s_succs, num_preds, num_succs);
8543
8544           /* Compute the dominators and post dominators.  We don't currently use
8545              post dominators, but we should for speculative motion analysis.  */
8546           compute_dominators (dom, pdom, s_preds, s_succs);
8547
8548           /* build_control_flow will return nonzero if it detects unreachable
8549              blocks or any other irregularity with the cfg which prevents
8550              cross block scheduling.  */
8551           if (build_control_flow (s_preds, s_succs, num_preds, num_succs) != 0)
8552             find_single_block_region ();
8553           else
8554             find_rgns (s_preds, s_succs, num_preds, num_succs, dom);
8555
8556           if (sched_verbose >= 3)
8557             debug_regions ();
8558
8559           /* For now.  This will move as more and more of haifa is converted
8560              to using the cfg code in flow.c  */
8561           free_bb_mem ();
8562           free (dom);
8563           free (pdom);
8564         }
8565     }
8566
8567   /* Allocate data for this pass.  See comments, above,
8568      for what these vectors do.  */
8569   insn_priority = (int *) alloca (max_uid * sizeof (int));
8570   insn_reg_weight = (int *) alloca (max_uid * sizeof (int));
8571   insn_tick = (int *) alloca (max_uid * sizeof (int));
8572   insn_costs = (short *) alloca (max_uid * sizeof (short));
8573   insn_units = (short *) alloca (max_uid * sizeof (short));
8574   insn_blockage = (unsigned int *) alloca (max_uid * sizeof (unsigned int));
8575   insn_ref_count = (int *) alloca (max_uid * sizeof (int));
8576
8577   /* Allocate for forward dependencies */
8578   insn_dep_count = (int *) alloca (max_uid * sizeof (int));
8579   insn_depend = (rtx *) alloca (max_uid * sizeof (rtx));
8580
8581   if (reload_completed == 0)
8582     {
8583       int i;
8584
8585       sched_reg_n_calls_crossed = (int *) alloca (max_regno * sizeof (int));
8586       sched_reg_live_length = (int *) alloca (max_regno * sizeof (int));
8587       sched_reg_basic_block = (int *) alloca (max_regno * sizeof (int));
8588       bb_live_regs = ALLOCA_REG_SET ();
8589       bzero ((char *) sched_reg_n_calls_crossed, max_regno * sizeof (int));
8590       bzero ((char *) sched_reg_live_length, max_regno * sizeof (int));
8591
8592       for (i = 0; i < max_regno; i++)
8593         sched_reg_basic_block[i] = REG_BLOCK_UNKNOWN;
8594     }
8595   else
8596     {
8597       sched_reg_n_calls_crossed = 0;
8598       sched_reg_live_length = 0;
8599       bb_live_regs = 0;
8600     }
8601   init_alias_analysis ();
8602
8603   if (write_symbols != NO_DEBUG)
8604     {
8605       rtx line;
8606
8607       line_note = (rtx *) alloca (max_uid * sizeof (rtx));
8608       bzero ((char *) line_note, max_uid * sizeof (rtx));
8609       line_note_head = (rtx *) alloca (n_basic_blocks * sizeof (rtx));
8610       bzero ((char *) line_note_head, n_basic_blocks * sizeof (rtx));
8611
8612       /* Save-line-note-head:
8613          Determine the line-number at the start of each basic block.
8614          This must be computed and saved now, because after a basic block's
8615          predecessor has been scheduled, it is impossible to accurately
8616          determine the correct line number for the first insn of the block.  */
8617
8618       for (b = 0; b < n_basic_blocks; b++)
8619         for (line = basic_block_head[b]; line; line = PREV_INSN (line))
8620           if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
8621             {
8622               line_note_head[b] = line;
8623               break;
8624             }
8625     }
8626
8627   bzero ((char *) insn_priority, max_uid * sizeof (int));
8628   bzero ((char *) insn_reg_weight, max_uid * sizeof (int));
8629   bzero ((char *) insn_tick, max_uid * sizeof (int));
8630   bzero ((char *) insn_costs, max_uid * sizeof (short));
8631   bzero ((char *) insn_units, max_uid * sizeof (short));
8632   bzero ((char *) insn_blockage, max_uid * sizeof (unsigned int));
8633   bzero ((char *) insn_ref_count, max_uid * sizeof (int));
8634
8635   /* Initialize for forward dependencies */
8636   bzero ((char *) insn_depend, max_uid * sizeof (rtx));
8637   bzero ((char *) insn_dep_count, max_uid * sizeof (int));
8638
8639   /* Find units used in this fuction, for visualization */
8640   if (sched_verbose)
8641     init_target_units ();
8642
8643   /* ??? Add a NOTE after the last insn of the last basic block.  It is not
8644      known why this is done.  */
8645
8646   insn = basic_block_end[n_basic_blocks - 1];
8647   if (NEXT_INSN (insn) == 0
8648       || (GET_CODE (insn) != NOTE
8649           && GET_CODE (insn) != CODE_LABEL
8650   /* Don't emit a NOTE if it would end up between an unconditional
8651      jump and a BARRIER.  */
8652           && !(GET_CODE (insn) == JUMP_INSN
8653                && GET_CODE (NEXT_INSN (insn)) == BARRIER)))
8654     emit_note_after (NOTE_INSN_DELETED, basic_block_end[n_basic_blocks - 1]);
8655
8656   /* Schedule every region in the subroutine */
8657   for (rgn = 0; rgn < nr_regions; rgn++)
8658     {
8659       schedule_region (rgn);
8660
8661 #ifdef USE_C_ALLOCA
8662       alloca (0);
8663 #endif
8664     }
8665
8666   /* Reposition the prologue and epilogue notes in case we moved the
8667      prologue/epilogue insns.  */
8668   if (reload_completed)
8669     reposition_prologue_and_epilogue_notes (get_insns ());
8670
8671   /* delete redundant line notes.  */
8672   if (write_symbols != NO_DEBUG)
8673     rm_redundant_line_notes ();
8674
8675   /* Update information about uses of registers in the subroutine.  */
8676   if (reload_completed == 0)
8677     update_reg_usage ();
8678
8679   if (sched_verbose)
8680     {
8681       if (reload_completed == 0 && flag_schedule_interblock)
8682         {
8683           fprintf (dump, "\n;; Procedure interblock/speculative motions == %d/%d \n",
8684                    nr_inter, nr_spec);
8685         }
8686       else
8687         {
8688           if (nr_inter > 0)
8689             abort ();
8690         }
8691       fprintf (dump, "\n\n");
8692     }
8693
8694   if (bb_live_regs)
8695     FREE_REG_SET (bb_live_regs);
8696
8697   if (edge_table)
8698     {
8699       free (edge_table);
8700       edge_table = NULL;
8701     }
8702
8703   if (in_edges)
8704     {
8705       free (in_edges);
8706       in_edges = NULL;
8707     }
8708   if (out_edges)
8709     {
8710       free (out_edges);
8711       out_edges = NULL;
8712     }
8713 }
8714 #endif /* INSN_SCHEDULING */