Fix for g++ failure reported by Brendan.
[platform/upstream/gcc.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 static int insn_issue_delay PROTO ((rtx));
457 static int birthing_insn_p PROTO ((rtx));
458 static void adjust_priority PROTO ((rtx));
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 static void debug_ready_list PROTO ((rtx[], int));
747 static void init_target_units PROTO ((void));
748 static void insn_print_units PROTO ((rtx));
749 static int get_visual_tbl_length PROTO ((void));
750 static void init_block_visualization PROTO ((void));
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_expr_list)
836     {
837       r = unused_expr_list;
838       unused_expr_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) == PARALLEL
2121       && GET_MODE (reg) == BLKmode)
2122     {
2123       register int i;
2124       for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
2125         if (check_live_1 (src, XVECEXP (reg, 0, i)))
2126           return 1;
2127       return 0;
2128     }
2129
2130   if (GET_CODE (reg) != REG)
2131     return 1;
2132
2133   regno = REGNO (reg);
2134
2135   if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2136     {
2137       /* Global registers are assumed live */
2138       return 0;
2139     }
2140   else
2141     {
2142       if (regno < FIRST_PSEUDO_REGISTER)
2143         {
2144           /* check for hard registers */
2145           int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
2146           while (--j >= 0)
2147             {
2148               for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
2149                 {
2150                   int b = candidate_table[src].split_bbs.first_member[i];
2151
2152                   if (REGNO_REG_SET_P (basic_block_live_at_start[b], regno + j))
2153                     {
2154                       return 0;
2155                     }
2156                 }
2157             }
2158         }
2159       else
2160         {
2161           /* check for psuedo registers */
2162           for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
2163             {
2164               int b = candidate_table[src].split_bbs.first_member[i];
2165
2166               if (REGNO_REG_SET_P (basic_block_live_at_start[b], regno))
2167                 {
2168                   return 0;
2169                 }
2170             }
2171         }
2172     }
2173
2174   return 1;
2175 }
2176
2177
2178 /* If x is a set of a register R, mark that R is alive in the beginning
2179    of every update-block of src.  */
2180
2181 static void
2182 update_live_1 (src, x)
2183      int src;
2184      rtx x;
2185 {
2186   register int i;
2187   register int regno;
2188   register rtx reg = SET_DEST (x);
2189
2190   if (reg == 0)
2191     return;
2192
2193   while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
2194          || GET_CODE (reg) == SIGN_EXTRACT
2195          || GET_CODE (reg) == STRICT_LOW_PART)
2196     reg = XEXP (reg, 0);
2197
2198   if (GET_CODE (reg) == PARALLEL
2199       && GET_MODE (reg) == BLKmode)
2200     {
2201       register int i;
2202       for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
2203         update_live_1 (src, XVECEXP (reg, 0, i));
2204       return;
2205     }
2206
2207   if (GET_CODE (reg) != REG)
2208     return;
2209
2210   /* Global registers are always live, so the code below does not apply
2211      to them.  */
2212
2213   regno = REGNO (reg);
2214
2215   if (regno >= FIRST_PSEUDO_REGISTER || !global_regs[regno])
2216     {
2217       if (regno < FIRST_PSEUDO_REGISTER)
2218         {
2219           int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
2220           while (--j >= 0)
2221             {
2222               for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
2223                 {
2224                   int b = candidate_table[src].update_bbs.first_member[i];
2225
2226                   SET_REGNO_REG_SET (basic_block_live_at_start[b], regno + j);
2227                 }
2228             }
2229         }
2230       else
2231         {
2232           for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
2233             {
2234               int b = candidate_table[src].update_bbs.first_member[i];
2235
2236               SET_REGNO_REG_SET (basic_block_live_at_start[b], regno);
2237             }
2238         }
2239     }
2240 }
2241
2242
2243 /* Return 1 if insn can be speculatively moved from block src to trg,
2244    otherwise return 0.  Called before first insertion of insn to
2245    ready-list or before the scheduling.  */
2246
2247 static int
2248 check_live (insn, src)
2249      rtx insn;
2250      int src;
2251 {
2252   /* find the registers set by instruction */
2253   if (GET_CODE (PATTERN (insn)) == SET
2254       || GET_CODE (PATTERN (insn)) == CLOBBER)
2255     return check_live_1 (src, PATTERN (insn));
2256   else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2257     {
2258       int j;
2259       for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
2260         if ((GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
2261              || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
2262             && !check_live_1 (src, XVECEXP (PATTERN (insn), 0, j)))
2263           return 0;
2264
2265       return 1;
2266     }
2267
2268   return 1;
2269 }
2270
2271
2272 /* Update the live registers info after insn was moved speculatively from
2273    block src to trg.  */
2274
2275 static void
2276 update_live (insn, src)
2277      rtx insn;
2278      int src;
2279 {
2280   /* find the registers set by instruction */
2281   if (GET_CODE (PATTERN (insn)) == SET
2282       || GET_CODE (PATTERN (insn)) == CLOBBER)
2283     update_live_1 (src, PATTERN (insn));
2284   else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2285     {
2286       int j;
2287       for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
2288         if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
2289             || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
2290           update_live_1 (src, XVECEXP (PATTERN (insn), 0, j));
2291     }
2292 }
2293
2294 /* Exception Free Loads:
2295
2296    We define five classes of speculative loads: IFREE, IRISKY,
2297    PFREE, PRISKY, and MFREE.
2298
2299    IFREE loads are loads that are proved to be exception-free, just
2300    by examining the load insn.  Examples for such loads are loads
2301    from TOC and loads of global data.
2302
2303    IRISKY loads are loads that are proved to be exception-risky,
2304    just by examining the load insn.  Examples for such loads are
2305    volatile loads and loads from shared memory.
2306
2307    PFREE loads are loads for which we can prove, by examining other
2308    insns, that they are exception-free.  Currently, this class consists
2309    of loads for which we are able to find a "similar load", either in
2310    the target block, or, if only one split-block exists, in that split
2311    block.  Load2 is similar to load1 if both have same single base
2312    register.  We identify only part of the similar loads, by finding
2313    an insn upon which both load1 and load2 have a DEF-USE dependence.
2314
2315    PRISKY loads are loads for which we can prove, by examining other
2316    insns, that they are exception-risky.  Currently we have two proofs for
2317    such loads.  The first proof detects loads that are probably guarded by a
2318    test on the memory address.  This proof is based on the
2319    backward and forward data dependence information for the region.
2320    Let load-insn be the examined load.
2321    Load-insn is PRISKY iff ALL the following hold:
2322
2323    - insn1 is not in the same block as load-insn
2324    - there is a DEF-USE dependence chain (insn1, ..., load-insn)
2325    - test-insn is either a compare or a branch, not in the same block as load-insn
2326    - load-insn is reachable from test-insn
2327    - there is a DEF-USE dependence chain (insn1, ..., test-insn)
2328
2329    This proof might fail when the compare and the load are fed
2330    by an insn not in the region.  To solve this, we will add to this
2331    group all loads that have no input DEF-USE dependence.
2332
2333    The second proof detects loads that are directly or indirectly
2334    fed by a speculative load.  This proof is affected by the
2335    scheduling process.  We will use the flag  fed_by_spec_load.
2336    Initially, all insns have this flag reset.  After a speculative
2337    motion of an insn, if insn is either a load, or marked as
2338    fed_by_spec_load, we will also mark as fed_by_spec_load every
2339    insn1 for which a DEF-USE dependence (insn, insn1) exists.  A
2340    load which is fed_by_spec_load is also PRISKY.
2341
2342    MFREE (maybe-free) loads are all the remaining loads. They may be
2343    exception-free, but we cannot prove it.
2344
2345    Now, all loads in IFREE and PFREE classes are considered
2346    exception-free, while all loads in IRISKY and PRISKY classes are
2347    considered exception-risky.  As for loads in the MFREE class,
2348    these are considered either exception-free or exception-risky,
2349    depending on whether we are pessimistic or optimistic.  We have
2350    to take the pessimistic approach to assure the safety of
2351    speculative scheduling, but we can take the optimistic approach
2352    by invoking the -fsched_spec_load_dangerous option.  */
2353
2354 enum INSN_TRAP_CLASS
2355 {
2356   TRAP_FREE = 0, IFREE = 1, PFREE_CANDIDATE = 2,
2357   PRISKY_CANDIDATE = 3, IRISKY = 4, TRAP_RISKY = 5
2358 };
2359
2360 #define WORST_CLASS(class1, class2) \
2361 ((class1 > class2) ? class1 : class2)
2362
2363 /* Indexed by INSN_UID, and set if there's DEF-USE dependence between */
2364 /* some speculatively moved load insn and this one.  */
2365 char *fed_by_spec_load;
2366 char *is_load_insn;
2367
2368 /* Non-zero if block bb_to is equal to, or reachable from block bb_from.  */
2369 #define IS_REACHABLE(bb_from, bb_to)                                    \
2370 (bb_from == bb_to                                                       \
2371    || IS_RGN_ENTRY (bb_from)                                            \
2372    || (bitset_member (ancestor_edges[bb_to],                            \
2373                       EDGE_TO_BIT (IN_EDGES (BB_TO_BLOCK (bb_from))),   \
2374                       edgeset_size)))
2375 #define FED_BY_SPEC_LOAD(insn) (fed_by_spec_load[INSN_UID (insn)])
2376 #define IS_LOAD_INSN(insn) (is_load_insn[INSN_UID (insn)])
2377
2378 /* Non-zero iff the address is comprised from at most 1 register */
2379 #define CONST_BASED_ADDRESS_P(x)                        \
2380   (GET_CODE (x) == REG                                  \
2381    || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS   \
2382         || (GET_CODE (x) == LO_SUM))                    \
2383        && (GET_CODE (XEXP (x, 0)) == CONST_INT          \
2384            || GET_CODE (XEXP (x, 1)) == CONST_INT)))
2385
2386 /* Turns on the fed_by_spec_load flag for insns fed by load_insn.  */
2387
2388 static void
2389 set_spec_fed (load_insn)
2390      rtx load_insn;
2391 {
2392   rtx link;
2393
2394   for (link = INSN_DEPEND (load_insn); link; link = XEXP (link, 1))
2395     if (GET_MODE (link) == VOIDmode)
2396       FED_BY_SPEC_LOAD (XEXP (link, 0)) = 1;
2397 }                               /* set_spec_fed */
2398
2399 /* On the path from the insn to load_insn_bb, find a conditional branch */
2400 /* depending on insn, that guards the speculative load.  */
2401
2402 static int
2403 find_conditional_protection (insn, load_insn_bb)
2404      rtx insn;
2405      int load_insn_bb;
2406 {
2407   rtx link;
2408
2409   /* iterate through DEF-USE forward dependences */
2410   for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
2411     {
2412       rtx next = XEXP (link, 0);
2413       if ((CONTAINING_RGN (INSN_BLOCK (next)) ==
2414            CONTAINING_RGN (BB_TO_BLOCK (load_insn_bb)))
2415           && IS_REACHABLE (INSN_BB (next), load_insn_bb)
2416           && load_insn_bb != INSN_BB (next)
2417           && GET_MODE (link) == VOIDmode
2418           && (GET_CODE (next) == JUMP_INSN
2419               || find_conditional_protection (next, load_insn_bb)))
2420         return 1;
2421     }
2422   return 0;
2423 }                               /* find_conditional_protection */
2424
2425 /* Returns 1 if the same insn1 that participates in the computation
2426    of load_insn's address is feeding a conditional branch that is
2427    guarding on load_insn. This is true if we find a the two DEF-USE
2428    chains:
2429    insn1 -> ... -> conditional-branch
2430    insn1 -> ... -> load_insn,
2431    and if a flow path exist:
2432    insn1 -> ... -> conditional-branch -> ... -> load_insn,
2433    and if insn1 is on the path
2434    region-entry -> ... -> bb_trg -> ... load_insn.
2435
2436    Locate insn1 by climbing on LOG_LINKS from load_insn.
2437    Locate the branch by following INSN_DEPEND from insn1.  */
2438
2439 static int
2440 is_conditionally_protected (load_insn, bb_src, bb_trg)
2441      rtx load_insn;
2442      int bb_src, bb_trg;
2443 {
2444   rtx link;
2445
2446   for (link = LOG_LINKS (load_insn); link; link = XEXP (link, 1))
2447     {
2448       rtx insn1 = XEXP (link, 0);
2449
2450       /* must be a DEF-USE dependence upon non-branch */
2451       if (GET_MODE (link) != VOIDmode
2452           || GET_CODE (insn1) == JUMP_INSN)
2453         continue;
2454
2455       /* must exist a path: region-entry -> ... -> bb_trg -> ... load_insn */
2456       if (INSN_BB (insn1) == bb_src
2457           || (CONTAINING_RGN (INSN_BLOCK (insn1))
2458               != CONTAINING_RGN (BB_TO_BLOCK (bb_src)))
2459           || (!IS_REACHABLE (bb_trg, INSN_BB (insn1))
2460               && !IS_REACHABLE (INSN_BB (insn1), bb_trg)))
2461         continue;
2462
2463       /* now search for the conditional-branch */
2464       if (find_conditional_protection (insn1, bb_src))
2465         return 1;
2466
2467       /* recursive step: search another insn1, "above" current insn1.  */
2468       return is_conditionally_protected (insn1, bb_src, bb_trg);
2469     }
2470
2471   /* the chain does not exsist */
2472   return 0;
2473 }                               /* is_conditionally_protected */
2474
2475 /* Returns 1 if a clue for "similar load" 'insn2' is found, and hence
2476    load_insn can move speculatively from bb_src to bb_trg.  All the
2477    following must hold:
2478
2479    (1) both loads have 1 base register (PFREE_CANDIDATEs).
2480    (2) load_insn and load1 have a def-use dependence upon
2481    the same insn 'insn1'.
2482    (3) either load2 is in bb_trg, or:
2483    - there's only one split-block, and
2484    - load1 is on the escape path, and
2485
2486    From all these we can conclude that the two loads access memory
2487    addresses that differ at most by a constant, and hence if moving
2488    load_insn would cause an exception, it would have been caused by
2489    load2 anyhow.  */
2490
2491 static int
2492 is_pfree (load_insn, bb_src, bb_trg)
2493      rtx load_insn;
2494      int bb_src, bb_trg;
2495 {
2496   rtx back_link;
2497   register candidate *candp = candidate_table + bb_src;
2498
2499   if (candp->split_bbs.nr_members != 1)
2500     /* must have exactly one escape block */
2501     return 0;
2502
2503   for (back_link = LOG_LINKS (load_insn);
2504        back_link; back_link = XEXP (back_link, 1))
2505     {
2506       rtx insn1 = XEXP (back_link, 0);
2507
2508       if (GET_MODE (back_link) == VOIDmode)
2509         {
2510           /* found a DEF-USE dependence (insn1, load_insn) */
2511           rtx fore_link;
2512
2513           for (fore_link = INSN_DEPEND (insn1);
2514                fore_link; fore_link = XEXP (fore_link, 1))
2515             {
2516               rtx insn2 = XEXP (fore_link, 0);
2517               if (GET_MODE (fore_link) == VOIDmode)
2518                 {
2519                   /* found a DEF-USE dependence (insn1, insn2) */
2520                   if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
2521                     /* insn2 not guaranteed to be a 1 base reg load */
2522                     continue;
2523
2524                   if (INSN_BB (insn2) == bb_trg)
2525                     /* insn2 is the similar load, in the target block */
2526                     return 1;
2527
2528                   if (*(candp->split_bbs.first_member) == INSN_BLOCK (insn2))
2529                     /* insn2 is a similar load, in a split-block */
2530                     return 1;
2531                 }
2532             }
2533         }
2534     }
2535
2536   /* couldn't find a similar load */
2537   return 0;
2538 }                               /* is_pfree */
2539
2540 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
2541    as found by analyzing insn's expression.  */
2542
2543 static int
2544 may_trap_exp (x, is_store)
2545      rtx x;
2546      int is_store;
2547 {
2548   enum rtx_code code;
2549
2550   if (x == 0)
2551     return TRAP_FREE;
2552   code = GET_CODE (x);
2553   if (is_store)
2554     {
2555       if (code == MEM)
2556         return TRAP_RISKY;
2557       else
2558         return TRAP_FREE;
2559     }
2560   if (code == MEM)
2561     {
2562       /* The insn uses memory */
2563       /* a volatile load */
2564       if (MEM_VOLATILE_P (x))
2565         return IRISKY;
2566       /* an exception-free load */
2567       if (!may_trap_p (x))
2568         return IFREE;
2569       /* a load with 1 base register, to be further checked */
2570       if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
2571         return PFREE_CANDIDATE;
2572       /* no info on the load, to be further checked */
2573       return PRISKY_CANDIDATE;
2574     }
2575   else
2576     {
2577       char *fmt;
2578       int i, insn_class = TRAP_FREE;
2579
2580       /* neither store nor load, check if it may cause a trap */
2581       if (may_trap_p (x))
2582         return TRAP_RISKY;
2583       /* recursive step: walk the insn...  */
2584       fmt = GET_RTX_FORMAT (code);
2585       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2586         {
2587           if (fmt[i] == 'e')
2588             {
2589               int tmp_class = may_trap_exp (XEXP (x, i), is_store);
2590               insn_class = WORST_CLASS (insn_class, tmp_class);
2591             }
2592           else if (fmt[i] == 'E')
2593             {
2594               int j;
2595               for (j = 0; j < XVECLEN (x, i); j++)
2596                 {
2597                   int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
2598                   insn_class = WORST_CLASS (insn_class, tmp_class);
2599                   if (insn_class == TRAP_RISKY || insn_class == IRISKY)
2600                     break;
2601                 }
2602             }
2603           if (insn_class == TRAP_RISKY || insn_class == IRISKY)
2604             break;
2605         }
2606       return insn_class;
2607     }
2608 }                               /* may_trap_exp */
2609
2610
2611 /* Classifies insn for the purpose of verifying that it can be
2612    moved speculatively, by examining it's patterns, returning:
2613    TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
2614    TRAP_FREE: non-load insn.
2615    IFREE: load from a globaly safe location.
2616    IRISKY: volatile load.
2617    PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
2618    being either PFREE or PRISKY.  */
2619
2620 static int
2621 haifa_classify_insn (insn)
2622      rtx insn;
2623 {
2624   rtx pat = PATTERN (insn);
2625   int tmp_class = TRAP_FREE;
2626   int insn_class = TRAP_FREE;
2627   enum rtx_code code;
2628
2629   if (GET_CODE (pat) == PARALLEL)
2630     {
2631       int i, len = XVECLEN (pat, 0);
2632
2633       for (i = len - 1; i >= 0; i--)
2634         {
2635           code = GET_CODE (XVECEXP (pat, 0, i));
2636           switch (code)
2637             {
2638             case CLOBBER:
2639               /* test if it is a 'store' */
2640               tmp_class = may_trap_exp (XEXP (XVECEXP (pat, 0, i), 0), 1);
2641               break;
2642             case SET:
2643               /* test if it is a store */
2644               tmp_class = may_trap_exp (SET_DEST (XVECEXP (pat, 0, i)), 1);
2645               if (tmp_class == TRAP_RISKY)
2646                 break;
2647               /* test if it is a load  */
2648               tmp_class =
2649                 WORST_CLASS (tmp_class,
2650                            may_trap_exp (SET_SRC (XVECEXP (pat, 0, i)), 0));
2651               break;
2652             case TRAP_IF:
2653               tmp_class = TRAP_RISKY;
2654               break;
2655             default:;
2656             }
2657           insn_class = WORST_CLASS (insn_class, tmp_class);
2658           if (insn_class == TRAP_RISKY || insn_class == IRISKY)
2659             break;
2660         }
2661     }
2662   else
2663     {
2664       code = GET_CODE (pat);
2665       switch (code)
2666         {
2667         case CLOBBER:
2668           /* test if it is a 'store' */
2669           tmp_class = may_trap_exp (XEXP (pat, 0), 1);
2670           break;
2671         case SET:
2672           /* test if it is a store */
2673           tmp_class = may_trap_exp (SET_DEST (pat), 1);
2674           if (tmp_class == TRAP_RISKY)
2675             break;
2676           /* test if it is a load  */
2677           tmp_class =
2678             WORST_CLASS (tmp_class,
2679                          may_trap_exp (SET_SRC (pat), 0));
2680           break;
2681         case TRAP_IF:
2682           tmp_class = TRAP_RISKY;
2683           break;
2684         default:;
2685         }
2686       insn_class = tmp_class;
2687     }
2688
2689   return insn_class;
2690
2691 }                               /* haifa_classify_insn */
2692
2693 /* Return 1 if load_insn is prisky (i.e. if load_insn is fed by
2694    a load moved speculatively, or if load_insn is protected by
2695    a compare on load_insn's address).  */
2696
2697 static int
2698 is_prisky (load_insn, bb_src, bb_trg)
2699      rtx load_insn;
2700      int bb_src, bb_trg;
2701 {
2702   if (FED_BY_SPEC_LOAD (load_insn))
2703     return 1;
2704
2705   if (LOG_LINKS (load_insn) == NULL)
2706     /* dependence may 'hide' out of the region.  */
2707     return 1;
2708
2709   if (is_conditionally_protected (load_insn, bb_src, bb_trg))
2710     return 1;
2711
2712   return 0;
2713 }                               /* is_prisky */
2714
2715 /* Insn is a candidate to be moved speculatively from bb_src to bb_trg.
2716    Return 1 if insn is exception-free (and the motion is valid)
2717    and 0 otherwise.  */
2718
2719 static int
2720 is_exception_free (insn, bb_src, bb_trg)
2721      rtx insn;
2722      int bb_src, bb_trg;
2723 {
2724   int insn_class = haifa_classify_insn (insn);
2725
2726   /* handle non-load insns */
2727   switch (insn_class)
2728     {
2729     case TRAP_FREE:
2730       return 1;
2731     case TRAP_RISKY:
2732       return 0;
2733     default:;
2734     }
2735
2736   /* handle loads */
2737   if (!flag_schedule_speculative_load)
2738     return 0;
2739   IS_LOAD_INSN (insn) = 1;
2740   switch (insn_class)
2741     {
2742     case IFREE:
2743       return (1);
2744     case IRISKY:
2745       return 0;
2746     case PFREE_CANDIDATE:
2747       if (is_pfree (insn, bb_src, bb_trg))
2748         return 1;
2749       /* don't 'break' here: PFREE-candidate is also PRISKY-candidate */
2750     case PRISKY_CANDIDATE:
2751       if (!flag_schedule_speculative_load_dangerous
2752           || is_prisky (insn, bb_src, bb_trg))
2753         return 0;
2754       break;
2755     default:;
2756     }
2757
2758   return flag_schedule_speculative_load_dangerous;
2759 }                               /* is_exception_free */
2760
2761
2762 /* Process an insn's memory dependencies.  There are four kinds of
2763    dependencies:
2764
2765    (0) read dependence: read follows read
2766    (1) true dependence: read follows write
2767    (2) anti dependence: write follows read
2768    (3) output dependence: write follows write
2769
2770    We are careful to build only dependencies which actually exist, and
2771    use transitivity to avoid building too many links.  */
2772 \f
2773 /* Return the INSN_LIST containing INSN in LIST, or NULL
2774    if LIST does not contain INSN.  */
2775
2776 HAIFA_INLINE static rtx
2777 find_insn_list (insn, list)
2778      rtx insn;
2779      rtx list;
2780 {
2781   while (list)
2782     {
2783       if (XEXP (list, 0) == insn)
2784         return list;
2785       list = XEXP (list, 1);
2786     }
2787   return 0;
2788 }
2789
2790
2791 /* Return 1 if the pair (insn, x) is found in (LIST, LIST1), or 0 otherwise.  */
2792
2793 HAIFA_INLINE static char
2794 find_insn_mem_list (insn, x, list, list1)
2795      rtx insn, x;
2796      rtx list, list1;
2797 {
2798   while (list)
2799     {
2800       if (XEXP (list, 0) == insn
2801           && XEXP (list1, 0) == x)
2802         return 1;
2803       list = XEXP (list, 1);
2804       list1 = XEXP (list1, 1);
2805     }
2806   return 0;
2807 }
2808
2809
2810 /* Compute the function units used by INSN.  This caches the value
2811    returned by function_units_used.  A function unit is encoded as the
2812    unit number if the value is non-negative and the compliment of a
2813    mask if the value is negative.  A function unit index is the
2814    non-negative encoding.  */
2815
2816 HAIFA_INLINE static int
2817 insn_unit (insn)
2818      rtx insn;
2819 {
2820   register int unit = INSN_UNIT (insn);
2821
2822   if (unit == 0)
2823     {
2824       recog_memoized (insn);
2825
2826       /* A USE insn, or something else we don't need to understand.
2827          We can't pass these directly to function_units_used because it will
2828          trigger a fatal error for unrecognizable insns.  */
2829       if (INSN_CODE (insn) < 0)
2830         unit = -1;
2831       else
2832         {
2833           unit = function_units_used (insn);
2834           /* Increment non-negative values so we can cache zero.  */
2835           if (unit >= 0)
2836             unit++;
2837         }
2838       /* We only cache 16 bits of the result, so if the value is out of
2839          range, don't cache it.  */
2840       if (FUNCTION_UNITS_SIZE < HOST_BITS_PER_SHORT
2841           || unit >= 0
2842           || (~unit & ((1 << (HOST_BITS_PER_SHORT - 1)) - 1)) == 0)
2843         INSN_UNIT (insn) = unit;
2844     }
2845   return (unit > 0 ? unit - 1 : unit);
2846 }
2847
2848 /* Compute the blockage range for executing INSN on UNIT.  This caches
2849    the value returned by the blockage_range_function for the unit.
2850    These values are encoded in an int where the upper half gives the
2851    minimum value and the lower half gives the maximum value.  */
2852
2853 HAIFA_INLINE static unsigned int
2854 blockage_range (unit, insn)
2855      int unit;
2856      rtx insn;
2857 {
2858   unsigned int blockage = INSN_BLOCKAGE (insn);
2859   unsigned int range;
2860
2861   if (UNIT_BLOCKED (blockage) != unit + 1)
2862     {
2863       range = function_units[unit].blockage_range_function (insn);
2864       /* We only cache the blockage range for one unit and then only if
2865          the values fit.  */
2866       if (HOST_BITS_PER_INT >= UNIT_BITS + 2 * BLOCKAGE_BITS)
2867         INSN_BLOCKAGE (insn) = ENCODE_BLOCKAGE (unit + 1, range);
2868     }
2869   else
2870     range = BLOCKAGE_RANGE (blockage);
2871
2872   return range;
2873 }
2874
2875 /* A vector indexed by function unit instance giving the last insn to use
2876    the unit.  The value of the function unit instance index for unit U
2877    instance I is (U + I * FUNCTION_UNITS_SIZE).  */
2878 static rtx unit_last_insn[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
2879
2880 /* A vector indexed by function unit instance giving the minimum time when
2881    the unit will unblock based on the maximum blockage cost.  */
2882 static int unit_tick[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
2883
2884 /* A vector indexed by function unit number giving the number of insns
2885    that remain to use the unit.  */
2886 static int unit_n_insns[FUNCTION_UNITS_SIZE];
2887
2888 /* Reset the function unit state to the null state.  */
2889
2890 static void
2891 clear_units ()
2892 {
2893   bzero ((char *) unit_last_insn, sizeof (unit_last_insn));
2894   bzero ((char *) unit_tick, sizeof (unit_tick));
2895   bzero ((char *) unit_n_insns, sizeof (unit_n_insns));
2896 }
2897
2898 /* Return the issue-delay of an insn */
2899
2900 HAIFA_INLINE static int
2901 insn_issue_delay (insn)
2902      rtx insn;
2903 {
2904   int i, delay = 0;
2905   int unit = insn_unit (insn);
2906
2907   /* efficiency note: in fact, we are working 'hard' to compute a
2908      value that was available in md file, and is not available in
2909      function_units[] structure.  It would be nice to have this
2910      value there, too.  */
2911   if (unit >= 0)
2912     {
2913       if (function_units[unit].blockage_range_function &&
2914           function_units[unit].blockage_function)
2915         delay = function_units[unit].blockage_function (insn, insn);
2916     }
2917   else
2918     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
2919       if ((unit & 1) != 0 && function_units[i].blockage_range_function
2920           && function_units[i].blockage_function)
2921         delay = MAX (delay, function_units[i].blockage_function (insn, insn));
2922
2923   return delay;
2924 }
2925
2926 /* Return the actual hazard cost of executing INSN on the unit UNIT,
2927    instance INSTANCE at time CLOCK if the previous actual hazard cost
2928    was COST.  */
2929
2930 HAIFA_INLINE static int
2931 actual_hazard_this_instance (unit, instance, insn, clock, cost)
2932      int unit, instance, clock, cost;
2933      rtx insn;
2934 {
2935   int tick = unit_tick[instance];       /* issue time of the last issued insn */
2936
2937   if (tick - clock > cost)
2938     {
2939       /* The scheduler is operating forward, so unit's last insn is the
2940          executing insn and INSN is the candidate insn.  We want a
2941          more exact measure of the blockage if we execute INSN at CLOCK
2942          given when we committed the execution of the unit's last insn.
2943
2944          The blockage value is given by either the unit's max blockage
2945          constant, blockage range function, or blockage function.  Use
2946          the most exact form for the given unit.  */
2947
2948       if (function_units[unit].blockage_range_function)
2949         {
2950           if (function_units[unit].blockage_function)
2951             tick += (function_units[unit].blockage_function
2952                      (unit_last_insn[instance], insn)
2953                      - function_units[unit].max_blockage);
2954           else
2955             tick += ((int) MAX_BLOCKAGE_COST (blockage_range (unit, insn))
2956                      - function_units[unit].max_blockage);
2957         }
2958       if (tick - clock > cost)
2959         cost = tick - clock;
2960     }
2961   return cost;
2962 }
2963
2964 /* Record INSN as having begun execution on the units encoded by UNIT at
2965    time CLOCK.  */
2966
2967 HAIFA_INLINE static void
2968 schedule_unit (unit, insn, clock)
2969      int unit, clock;
2970      rtx insn;
2971 {
2972   int i;
2973
2974   if (unit >= 0)
2975     {
2976       int instance = unit;
2977 #if MAX_MULTIPLICITY > 1
2978       /* Find the first free instance of the function unit and use that
2979          one.  We assume that one is free.  */
2980       for (i = function_units[unit].multiplicity - 1; i > 0; i--)
2981         {
2982           if (!actual_hazard_this_instance (unit, instance, insn, clock, 0))
2983             break;
2984           instance += FUNCTION_UNITS_SIZE;
2985         }
2986 #endif
2987       unit_last_insn[instance] = insn;
2988       unit_tick[instance] = (clock + function_units[unit].max_blockage);
2989     }
2990   else
2991     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
2992       if ((unit & 1) != 0)
2993         schedule_unit (i, insn, clock);
2994 }
2995
2996 /* Return the actual hazard cost of executing INSN on the units encoded by
2997    UNIT at time CLOCK if the previous actual hazard cost was COST.  */
2998
2999 HAIFA_INLINE static int
3000 actual_hazard (unit, insn, clock, cost)
3001      int unit, clock, cost;
3002      rtx insn;
3003 {
3004   int i;
3005
3006   if (unit >= 0)
3007     {
3008       /* Find the instance of the function unit with the minimum hazard.  */
3009       int instance = unit;
3010       int best_cost = actual_hazard_this_instance (unit, instance, insn,
3011                                                    clock, cost);
3012       int this_cost;
3013
3014 #if MAX_MULTIPLICITY > 1
3015       if (best_cost > cost)
3016         {
3017           for (i = function_units[unit].multiplicity - 1; i > 0; i--)
3018             {
3019               instance += FUNCTION_UNITS_SIZE;
3020               this_cost = actual_hazard_this_instance (unit, instance, insn,
3021                                                        clock, cost);
3022               if (this_cost < best_cost)
3023                 {
3024                   best_cost = this_cost;
3025                   if (this_cost <= cost)
3026                     break;
3027                 }
3028             }
3029         }
3030 #endif
3031       cost = MAX (cost, best_cost);
3032     }
3033   else
3034     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
3035       if ((unit & 1) != 0)
3036         cost = actual_hazard (i, insn, clock, cost);
3037
3038   return cost;
3039 }
3040
3041 /* Return the potential hazard cost of executing an instruction on the
3042    units encoded by UNIT if the previous potential hazard cost was COST.
3043    An insn with a large blockage time is chosen in preference to one
3044    with a smaller time; an insn that uses a unit that is more likely
3045    to be used is chosen in preference to one with a unit that is less
3046    used.  We are trying to minimize a subsequent actual hazard.  */
3047
3048 HAIFA_INLINE static int
3049 potential_hazard (unit, insn, cost)
3050      int unit, cost;
3051      rtx insn;
3052 {
3053   int i, ncost;
3054   unsigned int minb, maxb;
3055
3056   if (unit >= 0)
3057     {
3058       minb = maxb = function_units[unit].max_blockage;
3059       if (maxb > 1)
3060         {
3061           if (function_units[unit].blockage_range_function)
3062             {
3063               maxb = minb = blockage_range (unit, insn);
3064               maxb = MAX_BLOCKAGE_COST (maxb);
3065               minb = MIN_BLOCKAGE_COST (minb);
3066             }
3067
3068           if (maxb > 1)
3069             {
3070               /* Make the number of instructions left dominate.  Make the
3071                  minimum delay dominate the maximum delay.  If all these
3072                  are the same, use the unit number to add an arbitrary
3073                  ordering.  Other terms can be added.  */
3074               ncost = minb * 0x40 + maxb;
3075               ncost *= (unit_n_insns[unit] - 1) * 0x1000 + unit;
3076               if (ncost > cost)
3077                 cost = ncost;
3078             }
3079         }
3080     }
3081   else
3082     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
3083       if ((unit & 1) != 0)
3084         cost = potential_hazard (i, insn, cost);
3085
3086   return cost;
3087 }
3088
3089 /* Compute cost of executing INSN given the dependence LINK on the insn USED.
3090    This is the number of cycles between instruction issue and
3091    instruction results.  */
3092
3093 HAIFA_INLINE static int
3094 insn_cost (insn, link, used)
3095      rtx insn, link, used;
3096 {
3097   register int cost = INSN_COST (insn);
3098
3099   if (cost == 0)
3100     {
3101       recog_memoized (insn);
3102
3103       /* A USE insn, or something else we don't need to understand.
3104          We can't pass these directly to result_ready_cost because it will
3105          trigger a fatal error for unrecognizable insns.  */
3106       if (INSN_CODE (insn) < 0)
3107         {
3108           INSN_COST (insn) = 1;
3109           return 1;
3110         }
3111       else
3112         {
3113           cost = result_ready_cost (insn);
3114
3115           if (cost < 1)
3116             cost = 1;
3117
3118           INSN_COST (insn) = cost;
3119         }
3120     }
3121
3122   /* in this case estimate cost without caring how insn is used.  */
3123   if (link == 0 && used == 0)
3124     return cost;
3125
3126   /* A USE insn should never require the value used to be computed.  This
3127      allows the computation of a function's result and parameter values to
3128      overlap the return and call.  */
3129   recog_memoized (used);
3130   if (INSN_CODE (used) < 0)
3131     LINK_COST_FREE (link) = 1;
3132
3133   /* If some dependencies vary the cost, compute the adjustment.  Most
3134      commonly, the adjustment is complete: either the cost is ignored
3135      (in the case of an output- or anti-dependence), or the cost is
3136      unchanged.  These values are cached in the link as LINK_COST_FREE
3137      and LINK_COST_ZERO.  */
3138
3139   if (LINK_COST_FREE (link))
3140     cost = 1;
3141 #ifdef ADJUST_COST
3142   else if (!LINK_COST_ZERO (link))
3143     {
3144       int ncost = cost;
3145
3146       ADJUST_COST (used, link, insn, ncost);
3147       if (ncost <= 1)
3148         LINK_COST_FREE (link) = ncost = 1;
3149       if (cost == ncost)
3150         LINK_COST_ZERO (link) = 1;
3151       cost = ncost;
3152     }
3153 #endif
3154   return cost;
3155 }
3156
3157 /* Compute the priority number for INSN.  */
3158
3159 static int
3160 priority (insn)
3161      rtx insn;
3162 {
3163   int this_priority;
3164   rtx link;
3165
3166   if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
3167     return 0;
3168
3169   if ((this_priority = INSN_PRIORITY (insn)) == 0)
3170     {
3171       if (INSN_DEPEND (insn) == 0)
3172         this_priority = insn_cost (insn, 0, 0);
3173       else
3174         for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
3175           {
3176             rtx next;
3177             int next_priority;
3178
3179             if (RTX_INTEGRATED_P (link))
3180               continue;
3181
3182             next = XEXP (link, 0);
3183
3184             /* critical path is meaningful in block boundaries only */
3185             if (INSN_BLOCK (next) != INSN_BLOCK (insn))
3186               continue;
3187
3188             next_priority = insn_cost (insn, link, next) + priority (next);
3189             if (next_priority > this_priority)
3190               this_priority = next_priority;
3191           }
3192       INSN_PRIORITY (insn) = this_priority;
3193     }
3194   return this_priority;
3195 }
3196 \f
3197
3198 /* Remove all INSN_LISTs and EXPR_LISTs from the pending lists and add
3199    them to the unused_*_list variables, so that they can be reused.  */
3200
3201 static void
3202 free_pending_lists ()
3203 {
3204   if (current_nr_blocks <= 1)
3205     {
3206       free_list (&pending_read_insns, &unused_insn_list);
3207       free_list (&pending_write_insns, &unused_insn_list);
3208       free_list (&pending_read_mems, &unused_expr_list);
3209       free_list (&pending_write_mems, &unused_expr_list);
3210     }
3211   else
3212     {
3213       /* interblock scheduling */
3214       int bb;
3215
3216       for (bb = 0; bb < current_nr_blocks; bb++)
3217         {
3218           free_list (&bb_pending_read_insns[bb], &unused_insn_list);
3219           free_list (&bb_pending_write_insns[bb], &unused_insn_list);
3220           free_list (&bb_pending_read_mems[bb], &unused_expr_list);
3221           free_list (&bb_pending_write_mems[bb], &unused_expr_list);
3222         }
3223     }
3224 }
3225
3226 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
3227    The MEM is a memory reference contained within INSN, which we are saving
3228    so that we can do memory aliasing on it.  */
3229
3230 static void
3231 add_insn_mem_dependence (insn_list, mem_list, insn, mem)
3232      rtx *insn_list, *mem_list, insn, mem;
3233 {
3234   register rtx link;
3235
3236   link = alloc_INSN_LIST (insn, *insn_list);
3237   *insn_list = link;
3238
3239   link = alloc_EXPR_LIST (VOIDmode, mem, *mem_list);
3240   *mem_list = link;
3241
3242   pending_lists_length++;
3243 }
3244 \f
3245
3246 /* Make a dependency between every memory reference on the pending lists
3247    and INSN, thus flushing the pending lists.  If ONLY_WRITE, don't flush
3248    the read list.  */
3249
3250 static void
3251 flush_pending_lists (insn, only_write)
3252      rtx insn;
3253      int only_write;
3254 {
3255   rtx u;
3256   rtx link;
3257
3258   while (pending_read_insns && ! only_write)
3259     {
3260       add_dependence (insn, XEXP (pending_read_insns, 0), REG_DEP_ANTI);
3261
3262       link = pending_read_insns;
3263       pending_read_insns = XEXP (pending_read_insns, 1);
3264       XEXP (link, 1) = unused_insn_list;
3265       unused_insn_list = link;
3266
3267       link = pending_read_mems;
3268       pending_read_mems = XEXP (pending_read_mems, 1);
3269       XEXP (link, 1) = unused_expr_list;
3270       unused_expr_list = link;
3271     }
3272   while (pending_write_insns)
3273     {
3274       add_dependence (insn, XEXP (pending_write_insns, 0), REG_DEP_ANTI);
3275
3276       link = pending_write_insns;
3277       pending_write_insns = XEXP (pending_write_insns, 1);
3278       XEXP (link, 1) = unused_insn_list;
3279       unused_insn_list = link;
3280
3281       link = pending_write_mems;
3282       pending_write_mems = XEXP (pending_write_mems, 1);
3283       XEXP (link, 1) = unused_expr_list;
3284       unused_expr_list = link;
3285     }
3286   pending_lists_length = 0;
3287
3288   /* last_pending_memory_flush is now a list of insns */
3289   for (u = last_pending_memory_flush; u; u = XEXP (u, 1))
3290     add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3291
3292   free_list (&last_pending_memory_flush, &unused_insn_list);
3293   last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
3294 }
3295
3296 /* Analyze a single SET or CLOBBER rtx, X, creating all dependencies generated
3297    by the write to the destination of X, and reads of everything mentioned.  */
3298
3299 static void
3300 sched_analyze_1 (x, insn)
3301      rtx x;
3302      rtx insn;
3303 {
3304   register int regno;
3305   register rtx dest = SET_DEST (x);
3306
3307   if (dest == 0)
3308     return;
3309
3310   if (GET_CODE (dest) == PARALLEL
3311       && GET_MODE (dest) == BLKmode)
3312     {
3313       register int i;
3314       for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
3315         sched_analyze_1 (XVECEXP (dest, 0, i), insn);
3316       if (GET_CODE (x) == SET)
3317         sched_analyze_2 (SET_SRC (x), insn);
3318       return;
3319     }
3320
3321   while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
3322       || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
3323     {
3324       if (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
3325         {
3326           /* The second and third arguments are values read by this insn.  */
3327           sched_analyze_2 (XEXP (dest, 1), insn);
3328           sched_analyze_2 (XEXP (dest, 2), insn);
3329         }
3330       dest = SUBREG_REG (dest);
3331     }
3332
3333   if (GET_CODE (dest) == REG)
3334     {
3335       register int i;
3336
3337       regno = REGNO (dest);
3338
3339       /* A hard reg in a wide mode may really be multiple registers.
3340          If so, mark all of them just like the first.  */
3341       if (regno < FIRST_PSEUDO_REGISTER)
3342         {
3343           i = HARD_REGNO_NREGS (regno, GET_MODE (dest));
3344           while (--i >= 0)
3345             {
3346               rtx u;
3347
3348               for (u = reg_last_uses[regno + i]; u; u = XEXP (u, 1))
3349                 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3350               reg_last_uses[regno + i] = 0;
3351
3352               for (u = reg_last_sets[regno + i]; u; u = XEXP (u, 1))
3353                 add_dependence (insn, XEXP (u, 0), REG_DEP_OUTPUT);
3354
3355               SET_REGNO_REG_SET (reg_pending_sets, regno + i);
3356
3357               if ((call_used_regs[regno + i] || global_regs[regno + i]))
3358                 /* Function calls clobber all call_used regs.  */
3359                 for (u = last_function_call; u; u = XEXP (u, 1))
3360                   add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3361             }
3362         }
3363       else
3364         {
3365           rtx u;
3366
3367           for (u = reg_last_uses[regno]; u; u = XEXP (u, 1))
3368             add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3369           reg_last_uses[regno] = 0;
3370
3371           for (u = reg_last_sets[regno]; u; u = XEXP (u, 1))
3372             add_dependence (insn, XEXP (u, 0), REG_DEP_OUTPUT);
3373
3374           SET_REGNO_REG_SET (reg_pending_sets, regno);
3375
3376           /* Pseudos that are REG_EQUIV to something may be replaced
3377              by that during reloading.  We need only add dependencies for
3378              the address in the REG_EQUIV note.  */
3379           if (!reload_completed
3380               && reg_known_equiv_p[regno]
3381               && GET_CODE (reg_known_value[regno]) == MEM)
3382             sched_analyze_2 (XEXP (reg_known_value[regno], 0), insn);
3383
3384           /* Don't let it cross a call after scheduling if it doesn't
3385              already cross one.  */
3386
3387           if (REG_N_CALLS_CROSSED (regno) == 0)
3388             for (u = last_function_call; u; u = XEXP (u, 1))
3389               add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3390         }
3391     }
3392   else if (GET_CODE (dest) == MEM)
3393     {
3394       /* Writing memory.  */
3395
3396       if (pending_lists_length > 32)
3397         {
3398           /* Flush all pending reads and writes to prevent the pending lists
3399              from getting any larger.  Insn scheduling runs too slowly when
3400              these lists get long.  The number 32 was chosen because it
3401              seems like a reasonable number.  When compiling GCC with itself,
3402              this flush occurs 8 times for sparc, and 10 times for m88k using
3403              the number 32.  */
3404           flush_pending_lists (insn, 0);
3405         }
3406       else
3407         {
3408           rtx u;
3409           rtx pending, pending_mem;
3410
3411           pending = pending_read_insns;
3412           pending_mem = pending_read_mems;
3413           while (pending)
3414             {
3415               /* If a dependency already exists, don't create a new one.  */
3416               if (!find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
3417                 if (anti_dependence (XEXP (pending_mem, 0), dest))
3418                   add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
3419
3420               pending = XEXP (pending, 1);
3421               pending_mem = XEXP (pending_mem, 1);
3422             }
3423
3424           pending = pending_write_insns;
3425           pending_mem = pending_write_mems;
3426           while (pending)
3427             {
3428               /* If a dependency already exists, don't create a new one.  */
3429               if (!find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
3430                 if (output_dependence (XEXP (pending_mem, 0), dest))
3431                   add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
3432
3433               pending = XEXP (pending, 1);
3434               pending_mem = XEXP (pending_mem, 1);
3435             }
3436
3437           for (u = last_pending_memory_flush; u; u = XEXP (u, 1))
3438             add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3439
3440           add_insn_mem_dependence (&pending_write_insns, &pending_write_mems,
3441                                    insn, dest);
3442         }
3443       sched_analyze_2 (XEXP (dest, 0), insn);
3444     }
3445
3446   /* Analyze reads.  */
3447   if (GET_CODE (x) == SET)
3448     sched_analyze_2 (SET_SRC (x), insn);
3449 }
3450
3451 /* Analyze the uses of memory and registers in rtx X in INSN.  */
3452
3453 static void
3454 sched_analyze_2 (x, insn)
3455      rtx x;
3456      rtx insn;
3457 {
3458   register int i;
3459   register int j;
3460   register enum rtx_code code;
3461   register char *fmt;
3462
3463   if (x == 0)
3464     return;
3465
3466   code = GET_CODE (x);
3467
3468   switch (code)
3469     {
3470     case CONST_INT:
3471     case CONST_DOUBLE:
3472     case SYMBOL_REF:
3473     case CONST:
3474     case LABEL_REF:
3475       /* Ignore constants.  Note that we must handle CONST_DOUBLE here
3476          because it may have a cc0_rtx in its CONST_DOUBLE_CHAIN field, but
3477          this does not mean that this insn is using cc0.  */
3478       return;
3479
3480 #ifdef HAVE_cc0
3481     case CC0:
3482       {
3483         rtx link, prev;
3484
3485         /* User of CC0 depends on immediately preceding insn.  */
3486         SCHED_GROUP_P (insn) = 1;
3487
3488         /* There may be a note before this insn now, but all notes will
3489            be removed before we actually try to schedule the insns, so
3490            it won't cause a problem later.  We must avoid it here though.  */
3491         prev = prev_nonnote_insn (insn);
3492
3493         /* Make a copy of all dependencies on the immediately previous insn,
3494            and add to this insn.  This is so that all the dependencies will
3495            apply to the group.  Remove an explicit dependence on this insn
3496            as SCHED_GROUP_P now represents it.  */
3497
3498         if (find_insn_list (prev, LOG_LINKS (insn)))
3499           remove_dependence (insn, prev);
3500
3501         for (link = LOG_LINKS (prev); link; link = XEXP (link, 1))
3502           add_dependence (insn, XEXP (link, 0), REG_NOTE_KIND (link));
3503
3504         return;
3505       }
3506 #endif
3507
3508     case REG:
3509       {
3510         rtx u;
3511         int regno = REGNO (x);
3512         if (regno < FIRST_PSEUDO_REGISTER)
3513           {
3514             int i;
3515
3516             i = HARD_REGNO_NREGS (regno, GET_MODE (x));
3517             while (--i >= 0)
3518               {
3519                 reg_last_uses[regno + i]
3520                   = alloc_INSN_LIST (insn, reg_last_uses[regno + i]);
3521
3522                 for (u = reg_last_sets[regno + i]; u; u = XEXP (u, 1))
3523                   add_dependence (insn, XEXP (u, 0), 0);
3524
3525                 if ((call_used_regs[regno + i] || global_regs[regno + i]))
3526                   /* Function calls clobber all call_used regs.  */
3527                   for (u = last_function_call; u; u = XEXP (u, 1))
3528                     add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3529               }
3530           }
3531         else
3532           {
3533             reg_last_uses[regno] = alloc_INSN_LIST (insn, reg_last_uses[regno]);
3534
3535             for (u = reg_last_sets[regno]; u; u = XEXP (u, 1))
3536               add_dependence (insn, XEXP (u, 0), 0);
3537
3538             /* Pseudos that are REG_EQUIV to something may be replaced
3539                by that during reloading.  We need only add dependencies for
3540                the address in the REG_EQUIV note.  */
3541             if (!reload_completed
3542                 && reg_known_equiv_p[regno]
3543                 && GET_CODE (reg_known_value[regno]) == MEM)
3544               sched_analyze_2 (XEXP (reg_known_value[regno], 0), insn);
3545
3546             /* If the register does not already cross any calls, then add this
3547                insn to the sched_before_next_call list so that it will still
3548                not cross calls after scheduling.  */
3549             if (REG_N_CALLS_CROSSED (regno) == 0)
3550               add_dependence (sched_before_next_call, insn, REG_DEP_ANTI);
3551           }
3552         return;
3553       }
3554
3555     case MEM:
3556       {
3557         /* Reading memory.  */
3558         rtx u;
3559         rtx pending, pending_mem;
3560
3561         pending = pending_read_insns;
3562         pending_mem = pending_read_mems;
3563         while (pending)
3564           {
3565             /* If a dependency already exists, don't create a new one.  */
3566             if (!find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
3567               if (read_dependence (XEXP (pending_mem, 0), x))
3568                 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
3569
3570             pending = XEXP (pending, 1);
3571             pending_mem = XEXP (pending_mem, 1);
3572           }
3573
3574         pending = pending_write_insns;
3575         pending_mem = pending_write_mems;
3576         while (pending)
3577           {
3578             /* If a dependency already exists, don't create a new one.  */
3579             if (!find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
3580               if (true_dependence (XEXP (pending_mem, 0), VOIDmode,
3581                   x, rtx_varies_p))
3582                 add_dependence (insn, XEXP (pending, 0), 0);
3583
3584             pending = XEXP (pending, 1);
3585             pending_mem = XEXP (pending_mem, 1);
3586           }
3587
3588         for (u = last_pending_memory_flush; u; u = XEXP (u, 1))
3589           add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3590
3591         /* Always add these dependencies to pending_reads, since
3592            this insn may be followed by a write.  */
3593         add_insn_mem_dependence (&pending_read_insns, &pending_read_mems,
3594                                  insn, x);
3595
3596         /* Take advantage of tail recursion here.  */
3597         sched_analyze_2 (XEXP (x, 0), insn);
3598         return;
3599       }
3600
3601     /* Force pending stores to memory in case a trap handler needs them.  */
3602     case TRAP_IF:
3603       flush_pending_lists (insn, 1);
3604       break;
3605
3606     case ASM_OPERANDS:
3607     case ASM_INPUT:
3608     case UNSPEC_VOLATILE:
3609       {
3610         rtx u;
3611
3612         /* Traditional and volatile asm instructions must be considered to use
3613            and clobber all hard registers, all pseudo-registers and all of
3614            memory.  So must TRAP_IF and UNSPEC_VOLATILE operations.
3615
3616            Consider for instance a volatile asm that changes the fpu rounding
3617            mode.  An insn should not be moved across this even if it only uses
3618            pseudo-regs because it might give an incorrectly rounded result.  */
3619         if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
3620           {
3621             int max_reg = max_reg_num ();
3622             for (i = 0; i < max_reg; i++)
3623               {
3624                 for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
3625                   add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3626                 reg_last_uses[i] = 0;
3627
3628                 /* reg_last_sets[r] is now a list of insns */
3629                 for (u = reg_last_sets[i]; u; u = XEXP (u, 1))
3630                   add_dependence (insn, XEXP (u, 0), 0);
3631               }
3632             reg_pending_sets_all = 1;
3633
3634             flush_pending_lists (insn, 0);
3635           }
3636
3637         /* For all ASM_OPERANDS, we must traverse the vector of input operands.
3638            We can not just fall through here since then we would be confused
3639            by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
3640            traditional asms unlike their normal usage.  */
3641
3642         if (code == ASM_OPERANDS)
3643           {
3644             for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
3645               sched_analyze_2 (ASM_OPERANDS_INPUT (x, j), insn);
3646             return;
3647           }
3648         break;
3649       }
3650
3651     case PRE_DEC:
3652     case POST_DEC:
3653     case PRE_INC:
3654     case POST_INC:
3655       /* These both read and modify the result.  We must handle them as writes
3656          to get proper dependencies for following instructions.  We must handle
3657          them as reads to get proper dependencies from this to previous
3658          instructions.  Thus we need to pass them to both sched_analyze_1
3659          and sched_analyze_2.  We must call sched_analyze_2 first in order
3660          to get the proper antecedent for the read.  */
3661       sched_analyze_2 (XEXP (x, 0), insn);
3662       sched_analyze_1 (x, insn);
3663       return;
3664
3665     default:
3666       break;
3667     }
3668
3669   /* Other cases: walk the insn.  */
3670   fmt = GET_RTX_FORMAT (code);
3671   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3672     {
3673       if (fmt[i] == 'e')
3674         sched_analyze_2 (XEXP (x, i), insn);
3675       else if (fmt[i] == 'E')
3676         for (j = 0; j < XVECLEN (x, i); j++)
3677           sched_analyze_2 (XVECEXP (x, i, j), insn);
3678     }
3679 }
3680
3681 /* Analyze an INSN with pattern X to find all dependencies.  */
3682
3683 static void
3684 sched_analyze_insn (x, insn, loop_notes)
3685      rtx x, insn;
3686      rtx loop_notes;
3687 {
3688   register RTX_CODE code = GET_CODE (x);
3689   rtx link;
3690   int maxreg = max_reg_num ();
3691   int i;
3692
3693   if (code == SET || code == CLOBBER)
3694     sched_analyze_1 (x, insn);
3695   else if (code == PARALLEL)
3696     {
3697       register int i;
3698       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3699         {
3700           code = GET_CODE (XVECEXP (x, 0, i));
3701           if (code == SET || code == CLOBBER)
3702             sched_analyze_1 (XVECEXP (x, 0, i), insn);
3703           else
3704             sched_analyze_2 (XVECEXP (x, 0, i), insn);
3705         }
3706     }
3707   else
3708     sched_analyze_2 (x, insn);
3709
3710   /* Mark registers CLOBBERED or used by called function.  */
3711   if (GET_CODE (insn) == CALL_INSN)
3712     for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
3713       {
3714         if (GET_CODE (XEXP (link, 0)) == CLOBBER)
3715           sched_analyze_1 (XEXP (link, 0), insn);
3716         else
3717           sched_analyze_2 (XEXP (link, 0), insn);
3718       }
3719
3720   /* If there is a {LOOP,EHREGION}_{BEG,END} note in the middle of a basic
3721      block, then we must be sure that no instructions are scheduled across it.
3722      Otherwise, the reg_n_refs info (which depends on loop_depth) would
3723      become incorrect.  */
3724
3725   if (loop_notes)
3726     {
3727       int max_reg = max_reg_num ();
3728       int schedule_barrier_found = 0;
3729       rtx link;
3730
3731       /* Update loop_notes with any notes from this insn.  Also determine
3732          if any of the notes on the list correspond to instruction scheduling
3733          barriers (loop, eh & setjmp notes, but not range notes.  */
3734       link = loop_notes;
3735       while (XEXP (link, 1))
3736         {
3737           if (INTVAL (XEXP (link, 0)) == NOTE_INSN_LOOP_BEG
3738               || INTVAL (XEXP (link, 0)) == NOTE_INSN_LOOP_END
3739               || INTVAL (XEXP (link, 0)) == NOTE_INSN_EH_REGION_BEG
3740               || INTVAL (XEXP (link, 0)) == NOTE_INSN_EH_REGION_END
3741               || INTVAL (XEXP (link, 0)) == NOTE_INSN_SETJMP)
3742             schedule_barrier_found = 1;
3743
3744           link = XEXP (link, 1);
3745         }
3746       XEXP (link, 1) = REG_NOTES (insn);
3747       REG_NOTES (insn) = loop_notes;
3748
3749       /* Add dependencies if a scheduling barrier was found.  */
3750       if (schedule_barrier_found)
3751         {
3752           for (i = 0; i < max_reg; i++)
3753             {
3754               rtx u;
3755               for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
3756                 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3757               reg_last_uses[i] = 0;
3758
3759               /* reg_last_sets[r] is now a list of insns */
3760               for (u = reg_last_sets[i]; u; u = XEXP (u, 1))
3761                 add_dependence (insn, XEXP (u, 0), 0);
3762             }
3763           reg_pending_sets_all = 1;
3764
3765           flush_pending_lists (insn, 0);
3766         }
3767
3768     }
3769
3770   /* After reload, it is possible for an instruction to have a REG_DEAD note
3771      for a register that actually dies a few instructions earlier.  For
3772      example, this can happen with SECONDARY_MEMORY_NEEDED reloads.
3773      In this case, we must consider the insn to use the register mentioned
3774      in the REG_DEAD note.  Otherwise, we may accidentally move this insn
3775      after another insn that sets the register, thus getting obviously invalid
3776      rtl.  This confuses reorg which believes that REG_DEAD notes are still
3777      meaningful.
3778
3779      ??? We would get better code if we fixed reload to put the REG_DEAD
3780      notes in the right places, but that may not be worth the effort.  */
3781
3782   if (reload_completed)
3783     {
3784       rtx note;
3785
3786       for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3787         if (REG_NOTE_KIND (note) == REG_DEAD)
3788           sched_analyze_2 (XEXP (note, 0), insn);
3789     }
3790
3791   EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i,
3792                              {
3793                                /* reg_last_sets[r] is now a list of insns */
3794                                free_list (&reg_last_sets[i], &unused_insn_list);
3795                                reg_last_sets[i]
3796                                  = alloc_INSN_LIST (insn, NULL_RTX);
3797                              });
3798   CLEAR_REG_SET (reg_pending_sets);
3799
3800   if (reg_pending_sets_all)
3801     {
3802       for (i = 0; i < maxreg; i++)
3803         {
3804           /* reg_last_sets[r] is now a list of insns */
3805           free_list (&reg_last_sets[i], &unused_insn_list);
3806           reg_last_sets[i] = alloc_INSN_LIST (insn, NULL_RTX);
3807         }
3808
3809       reg_pending_sets_all = 0;
3810     }
3811
3812   /* Handle function calls and function returns created by the epilogue
3813      threading code.  */
3814   if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN)
3815     {
3816       rtx dep_insn;
3817       rtx prev_dep_insn;
3818
3819       /* When scheduling instructions, we make sure calls don't lose their
3820          accompanying USE insns by depending them one on another in order.
3821
3822          Also, we must do the same thing for returns created by the epilogue
3823          threading code.  Note this code works only in this special case,
3824          because other passes make no guarantee that they will never emit
3825          an instruction between a USE and a RETURN.  There is such a guarantee
3826          for USE instructions immediately before a call.  */
3827
3828       prev_dep_insn = insn;
3829       dep_insn = PREV_INSN (insn);
3830       while (GET_CODE (dep_insn) == INSN
3831              && GET_CODE (PATTERN (dep_insn)) == USE
3832              && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == REG)
3833         {
3834           SCHED_GROUP_P (prev_dep_insn) = 1;
3835
3836           /* Make a copy of all dependencies on dep_insn, and add to insn.
3837              This is so that all of the dependencies will apply to the
3838              group.  */
3839
3840           for (link = LOG_LINKS (dep_insn); link; link = XEXP (link, 1))
3841             add_dependence (insn, XEXP (link, 0), REG_NOTE_KIND (link));
3842
3843           prev_dep_insn = dep_insn;
3844           dep_insn = PREV_INSN (dep_insn);
3845         }
3846     }
3847 }
3848
3849 /* Analyze every insn between HEAD and TAIL inclusive, creating LOG_LINKS
3850    for every dependency.  */
3851
3852 static void
3853 sched_analyze (head, tail)
3854      rtx head, tail;
3855 {
3856   register rtx insn;
3857   register rtx u;
3858   rtx loop_notes = 0;
3859
3860   for (insn = head;; insn = NEXT_INSN (insn))
3861     {
3862       if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
3863         {
3864           /* Make each JUMP_INSN a scheduling barrier for memory references.  */
3865           if (GET_CODE (insn) == JUMP_INSN)
3866             last_pending_memory_flush
3867               = alloc_INSN_LIST (insn, last_pending_memory_flush);
3868           sched_analyze_insn (PATTERN (insn), insn, loop_notes);
3869           loop_notes = 0;
3870         }
3871       else if (GET_CODE (insn) == CALL_INSN)
3872         {
3873           rtx x;
3874           register int i;
3875
3876           CANT_MOVE (insn) = 1;
3877
3878           /* Any instruction using a hard register which may get clobbered
3879              by a call needs to be marked as dependent on this call.
3880              This prevents a use of a hard return reg from being moved
3881              past a void call (i.e. it does not explicitly set the hard
3882              return reg).  */
3883
3884           /* If this call is followed by a NOTE_INSN_SETJMP, then assume that
3885              all registers, not just hard registers, may be clobbered by this
3886              call.  */
3887
3888           /* Insn, being a CALL_INSN, magically depends on
3889              `last_function_call' already.  */
3890
3891           if (NEXT_INSN (insn) && GET_CODE (NEXT_INSN (insn)) == NOTE
3892               && NOTE_LINE_NUMBER (NEXT_INSN (insn)) == NOTE_INSN_SETJMP)
3893             {
3894               int max_reg = max_reg_num ();
3895               for (i = 0; i < max_reg; i++)
3896                 {
3897                   for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
3898                     add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3899
3900                   reg_last_uses[i] = 0;
3901
3902                   /* reg_last_sets[r] is now a list of insns */
3903                   for (u = reg_last_sets[i]; u; u = XEXP (u, 1))
3904                     add_dependence (insn, XEXP (u, 0), 0);
3905                 }
3906               reg_pending_sets_all = 1;
3907
3908               /* Add a pair of fake REG_NOTE which we will later
3909                  convert back into a NOTE_INSN_SETJMP note.  See
3910                  reemit_notes for why we use a pair of NOTEs.  */
3911               REG_NOTES (insn) = alloc_EXPR_LIST (REG_DEAD,
3912                                                   GEN_INT (0),
3913                                                   REG_NOTES (insn));
3914               REG_NOTES (insn) = alloc_EXPR_LIST (REG_DEAD,
3915                                                   GEN_INT (NOTE_INSN_SETJMP),
3916                                                   REG_NOTES (insn));
3917             }
3918           else
3919             {
3920               for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3921                 if (call_used_regs[i] || global_regs[i])
3922                   {
3923                     for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
3924                       add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3925                     reg_last_uses[i] = 0;
3926
3927                     /* reg_last_sets[r] is now a list of insns */
3928                     for (u = reg_last_sets[i]; u; u = XEXP (u, 1))
3929                       add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3930
3931                     SET_REGNO_REG_SET (reg_pending_sets, i);
3932                   }
3933             }
3934
3935           /* For each insn which shouldn't cross a call, add a dependence
3936              between that insn and this call insn.  */
3937           x = LOG_LINKS (sched_before_next_call);
3938           while (x)
3939             {
3940               add_dependence (insn, XEXP (x, 0), REG_DEP_ANTI);
3941               x = XEXP (x, 1);
3942             }
3943           LOG_LINKS (sched_before_next_call) = 0;
3944
3945           sched_analyze_insn (PATTERN (insn), insn, loop_notes);
3946           loop_notes = 0;
3947
3948           /* In the absence of interprocedural alias analysis, we must flush
3949              all pending reads and writes, and start new dependencies starting
3950              from here.  But only flush writes for constant calls (which may
3951              be passed a pointer to something we haven't written yet).  */
3952           flush_pending_lists (insn, CONST_CALL_P (insn));
3953
3954           /* Depend this function call (actually, the user of this
3955              function call) on all hard register clobberage.  */
3956
3957           /* last_function_call is now a list of insns */
3958           free_list(&last_function_call, &unused_insn_list);
3959           last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
3960         }
3961
3962       /* See comments on reemit_notes as to why we do this.  */
3963       /* ??? Actually, the reemit_notes just say what is done, not why.  */
3964
3965       else if (GET_CODE (insn) == NOTE
3966                && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_RANGE_START
3967                    || NOTE_LINE_NUMBER (insn) == NOTE_INSN_RANGE_END))
3968         {
3969           loop_notes = alloc_EXPR_LIST (REG_DEAD, NOTE_RANGE_INFO (insn),
3970                                         loop_notes);
3971           loop_notes = alloc_EXPR_LIST (REG_DEAD,
3972                                         GEN_INT (NOTE_LINE_NUMBER (insn)),
3973                                         loop_notes);
3974         }
3975       else if (GET_CODE (insn) == NOTE
3976                && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
3977                    || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
3978                    || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
3979                    || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END
3980                    || (NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP
3981                        && GET_CODE (PREV_INSN (insn)) != CALL_INSN)))
3982         {
3983           loop_notes = alloc_EXPR_LIST (REG_DEAD,
3984                                         GEN_INT (NOTE_BLOCK_NUMBER (insn)),
3985                                         loop_notes);
3986           loop_notes = alloc_EXPR_LIST (REG_DEAD,
3987                                         GEN_INT (NOTE_LINE_NUMBER (insn)),
3988                                         loop_notes);
3989           CONST_CALL_P (loop_notes) = CONST_CALL_P (insn);
3990         }
3991
3992       if (insn == tail)
3993         return;
3994     }
3995   abort ();
3996 }
3997 \f
3998 /* Called when we see a set of a register.  If death is true, then we are
3999    scanning backwards.  Mark that register as unborn.  If nobody says
4000    otherwise, that is how things will remain.  If death is false, then we
4001    are scanning forwards.  Mark that register as being born.  */
4002
4003 static void
4004 sched_note_set (x, death)
4005      rtx x;
4006      int death;
4007 {
4008   register int regno;
4009   register rtx reg = SET_DEST (x);
4010   int subreg_p = 0;
4011
4012   if (reg == 0)
4013     return;
4014
4015   if (GET_CODE (reg) == PARALLEL
4016       && GET_MODE (reg) == BLKmode)
4017     {
4018       register int i;
4019       for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
4020         sched_note_set (XVECEXP (reg, 0, i), death);
4021       return;
4022     }
4023
4024   while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == STRICT_LOW_PART
4025          || GET_CODE (reg) == SIGN_EXTRACT || GET_CODE (reg) == ZERO_EXTRACT)
4026     {
4027       /* Must treat modification of just one hardware register of a multi-reg
4028          value or just a byte field of a register exactly the same way that
4029          mark_set_1 in flow.c does, i.e. anything except a paradoxical subreg
4030          does not kill the entire register.  */
4031       if (GET_CODE (reg) != SUBREG
4032           || REG_SIZE (SUBREG_REG (reg)) > REG_SIZE (reg))
4033         subreg_p = 1;
4034
4035       reg = SUBREG_REG (reg);
4036     }
4037
4038   if (GET_CODE (reg) != REG)
4039     return;
4040
4041   /* Global registers are always live, so the code below does not apply
4042      to them.  */
4043
4044   regno = REGNO (reg);
4045   if (regno >= FIRST_PSEUDO_REGISTER || !global_regs[regno])
4046     {
4047       if (death)
4048         {
4049           /* If we only set part of the register, then this set does not
4050              kill it.  */
4051           if (subreg_p)
4052             return;
4053
4054           /* Try killing this register.  */
4055           if (regno < FIRST_PSEUDO_REGISTER)
4056             {
4057               int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
4058               while (--j >= 0)
4059                 {
4060                   CLEAR_REGNO_REG_SET (bb_live_regs, regno + j);
4061                 }
4062             }
4063           else
4064             {
4065               /* Recompute REG_BASIC_BLOCK as we update all the other
4066                  dataflow information.  */
4067               if (sched_reg_basic_block[regno] == REG_BLOCK_UNKNOWN)
4068                 sched_reg_basic_block[regno] = current_block_num;
4069               else if (sched_reg_basic_block[regno] != current_block_num)
4070                 sched_reg_basic_block[regno] = REG_BLOCK_GLOBAL;
4071
4072               CLEAR_REGNO_REG_SET (bb_live_regs, regno);
4073             }
4074         }
4075       else
4076         {
4077           /* Make the register live again.  */
4078           if (regno < FIRST_PSEUDO_REGISTER)
4079             {
4080               int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
4081               while (--j >= 0)
4082                 {
4083                   SET_REGNO_REG_SET (bb_live_regs, regno + j);
4084                 }
4085             }
4086           else
4087             {
4088               SET_REGNO_REG_SET (bb_live_regs, regno);
4089             }
4090         }
4091     }
4092 }
4093 \f
4094 /* Macros and functions for keeping the priority queue sorted, and
4095    dealing with queueing and dequeueing of instructions.  */
4096
4097 #define SCHED_SORT(READY, N_READY)                                   \
4098 do { if ((N_READY) == 2)                                             \
4099        swap_sort (READY, N_READY);                                   \
4100      else if ((N_READY) > 2)                                         \
4101          qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); }  \
4102 while (0)
4103
4104 /* Returns a positive value if x is preferred; returns a negative value if
4105    y is preferred.  Should never return 0, since that will make the sort
4106    unstable.  */
4107
4108 static int
4109 rank_for_schedule (x, y)
4110      const GENERIC_PTR x;
4111      const GENERIC_PTR y;
4112 {
4113   rtx tmp = *(rtx *)y;
4114   rtx tmp2 = *(rtx *)x;
4115   rtx link;
4116   int tmp_class, tmp2_class, depend_count1, depend_count2;
4117   int val, priority_val, spec_val, prob_val, weight_val;
4118
4119
4120   /* prefer insn with higher priority */
4121   priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
4122   if (priority_val)
4123     return priority_val;
4124
4125   /* prefer an insn with smaller contribution to registers-pressure */
4126   if (!reload_completed &&
4127       (weight_val = INSN_REG_WEIGHT (tmp) - INSN_REG_WEIGHT (tmp2)))
4128     return (weight_val);
4129
4130   /* some comparison make sense in interblock scheduling only */
4131   if (INSN_BB (tmp) != INSN_BB (tmp2))
4132     {
4133       /* prefer an inblock motion on an interblock motion */
4134       if ((INSN_BB (tmp2) == target_bb) && (INSN_BB (tmp) != target_bb))
4135         return 1;
4136       if ((INSN_BB (tmp) == target_bb) && (INSN_BB (tmp2) != target_bb))
4137         return -1;
4138
4139       /* prefer a useful motion on a speculative one */
4140       if ((spec_val = IS_SPECULATIVE_INSN (tmp) - IS_SPECULATIVE_INSN (tmp2)))
4141         return (spec_val);
4142
4143       /* prefer a more probable (speculative) insn */
4144       prob_val = INSN_PROBABILITY (tmp2) - INSN_PROBABILITY (tmp);
4145       if (prob_val)
4146         return (prob_val);
4147     }
4148
4149   /* compare insns based on their relation to the last-scheduled-insn */
4150   if (last_scheduled_insn)
4151     {
4152       /* Classify the instructions into three classes:
4153          1) Data dependent on last schedule insn.
4154          2) Anti/Output dependent on last scheduled insn.
4155          3) Independent of last scheduled insn, or has latency of one.
4156          Choose the insn from the highest numbered class if different.  */
4157       link = find_insn_list (tmp, INSN_DEPEND (last_scheduled_insn));
4158       if (link == 0 || insn_cost (last_scheduled_insn, link, tmp) == 1)
4159         tmp_class = 3;
4160       else if (REG_NOTE_KIND (link) == 0)       /* Data dependence.  */
4161         tmp_class = 1;
4162       else
4163         tmp_class = 2;
4164
4165       link = find_insn_list (tmp2, INSN_DEPEND (last_scheduled_insn));
4166       if (link == 0 || insn_cost (last_scheduled_insn, link, tmp2) == 1)
4167         tmp2_class = 3;
4168       else if (REG_NOTE_KIND (link) == 0)       /* Data dependence.  */
4169         tmp2_class = 1;
4170       else
4171         tmp2_class = 2;
4172
4173       if ((val = tmp2_class - tmp_class))
4174         return val;
4175     }
4176
4177   /* Prefer the insn which has more later insns that depend on it. 
4178      This gives the scheduler more freedom when scheduling later
4179      instructions at the expense of added register pressure.  */
4180   depend_count1 = 0;
4181   for (link = INSN_DEPEND (tmp); link; link = XEXP (link, 1))
4182     depend_count1++;
4183
4184   depend_count2 = 0;
4185   for (link = INSN_DEPEND (tmp2); link; link = XEXP (link, 1))
4186     depend_count2++;
4187
4188   val = depend_count2 - depend_count1;
4189   if (val)
4190     return val;
4191   
4192   /* If insns are equally good, sort by INSN_LUID (original insn order),
4193      so that we make the sort stable.  This minimizes instruction movement,
4194      thus minimizing sched's effect on debugging and cross-jumping.  */
4195   return INSN_LUID (tmp) - INSN_LUID (tmp2);
4196 }
4197
4198 /* Resort the array A in which only element at index N may be out of order.  */
4199
4200 HAIFA_INLINE static void
4201 swap_sort (a, n)
4202      rtx *a;
4203      int n;
4204 {
4205   rtx insn = a[n - 1];
4206   int i = n - 2;
4207
4208   while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
4209     {
4210       a[i + 1] = a[i];
4211       i -= 1;
4212     }
4213   a[i + 1] = insn;
4214 }
4215
4216 static int max_priority;
4217
4218 /* Add INSN to the insn queue so that it can be executed at least
4219    N_CYCLES after the currently executing insn.  Preserve insns
4220    chain for debugging purposes.  */
4221
4222 HAIFA_INLINE static void
4223 queue_insn (insn, n_cycles)
4224      rtx insn;
4225      int n_cycles;
4226 {
4227   int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
4228   rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
4229   insn_queue[next_q] = link;
4230   q_size += 1;
4231
4232   if (sched_verbose >= 2)
4233     {
4234       fprintf (dump, ";;\t\tReady-->Q: insn %d: ", INSN_UID (insn));
4235
4236       if (INSN_BB (insn) != target_bb)
4237         fprintf (dump, "(b%d) ", INSN_BLOCK (insn));
4238
4239       fprintf (dump, "queued for %d cycles.\n", n_cycles);
4240     }
4241
4242 }
4243
4244 /* Return nonzero if PAT is the pattern of an insn which makes a
4245    register live.  */
4246
4247 HAIFA_INLINE static int
4248 birthing_insn_p (pat)
4249      rtx pat;
4250 {
4251   int j;
4252
4253   if (reload_completed == 1)
4254     return 0;
4255
4256   if (GET_CODE (pat) == SET
4257       && (GET_CODE (SET_DEST (pat)) == REG
4258           || (GET_CODE (SET_DEST (pat)) == PARALLEL
4259               && GET_MODE (SET_DEST (pat)) == BLKmode)))
4260     {
4261       rtx dest = SET_DEST (pat);
4262       int i;
4263
4264       /* It would be more accurate to use refers_to_regno_p or
4265          reg_mentioned_p to determine when the dest is not live before this
4266          insn.  */
4267       if (GET_CODE (dest) == REG)
4268         {
4269           i = REGNO (dest);
4270           if (REGNO_REG_SET_P (bb_live_regs, i))
4271             return (REG_N_SETS (i) == 1);
4272         }
4273       else
4274         {
4275           for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
4276             {
4277               int regno = REGNO (SET_DEST (XVECEXP (dest, 0, i)));
4278               if (REGNO_REG_SET_P (bb_live_regs, regno))
4279                 return (REG_N_SETS (regno) == 1);
4280             }
4281         }
4282       return 0;
4283     }
4284   if (GET_CODE (pat) == PARALLEL)
4285     {
4286       for (j = 0; j < XVECLEN (pat, 0); j++)
4287         if (birthing_insn_p (XVECEXP (pat, 0, j)))
4288           return 1;
4289     }
4290   return 0;
4291 }
4292
4293 /* PREV is an insn that is ready to execute.  Adjust its priority if that
4294    will help shorten register lifetimes.  */
4295
4296 HAIFA_INLINE static void
4297 adjust_priority (prev)
4298      rtx prev;
4299 {
4300   /* Trying to shorten register lives after reload has completed
4301      is useless and wrong.  It gives inaccurate schedules.  */
4302   if (reload_completed == 0)
4303     {
4304       rtx note;
4305       int n_deaths = 0;
4306
4307       /* ??? This code has no effect, because REG_DEAD notes are removed
4308          before we ever get here.  */
4309       for (note = REG_NOTES (prev); note; note = XEXP (note, 1))
4310         if (REG_NOTE_KIND (note) == REG_DEAD)
4311           n_deaths += 1;
4312
4313       /* Defer scheduling insns which kill registers, since that
4314          shortens register lives.  Prefer scheduling insns which
4315          make registers live for the same reason.  */
4316       switch (n_deaths)
4317         {
4318         default:
4319           INSN_PRIORITY (prev) >>= 3;
4320           break;
4321         case 3:
4322           INSN_PRIORITY (prev) >>= 2;
4323           break;
4324         case 2:
4325         case 1:
4326           INSN_PRIORITY (prev) >>= 1;
4327           break;
4328         case 0:
4329           if (birthing_insn_p (PATTERN (prev)))
4330             {
4331               int max = max_priority;
4332
4333               if (max > INSN_PRIORITY (prev))
4334                 INSN_PRIORITY (prev) = max;
4335             }
4336           break;
4337         }
4338 #ifdef ADJUST_PRIORITY
4339       ADJUST_PRIORITY (prev);
4340 #endif
4341     }
4342 }
4343
4344 /* Clock at which the previous instruction was issued.  */
4345 static int last_clock_var;
4346
4347 /* INSN is the "currently executing insn".  Launch each insn which was
4348    waiting on INSN.  READY is a vector of insns which are ready to fire.
4349    N_READY is the number of elements in READY.  CLOCK is the current
4350    cycle.  */
4351
4352 static int
4353 schedule_insn (insn, ready, n_ready, clock)
4354      rtx insn;
4355      rtx *ready;
4356      int n_ready;
4357      int clock;
4358 {
4359   rtx link;
4360   int unit;
4361
4362   unit = insn_unit (insn);
4363
4364   if (sched_verbose >= 2)
4365     {
4366       fprintf (dump, ";;\t\t--> scheduling insn <<<%d>>> on unit ", INSN_UID (insn));
4367       insn_print_units (insn);
4368       fprintf (dump, "\n");
4369     }
4370
4371   if (sched_verbose && unit == -1)
4372     visualize_no_unit (insn);
4373
4374   if (MAX_BLOCKAGE > 1 || issue_rate > 1 || sched_verbose)
4375     schedule_unit (unit, insn, clock);
4376
4377   if (INSN_DEPEND (insn) == 0)
4378     return n_ready;
4379
4380   /* This is used by the function adjust_priority above.  */
4381   if (n_ready > 0)
4382     max_priority = MAX (INSN_PRIORITY (ready[0]), INSN_PRIORITY (insn));
4383   else
4384     max_priority = INSN_PRIORITY (insn);
4385
4386   for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
4387     {
4388       rtx next = XEXP (link, 0);
4389       int cost = insn_cost (insn, link, next);
4390
4391       INSN_TICK (next) = MAX (INSN_TICK (next), clock + cost);
4392
4393       if ((INSN_DEP_COUNT (next) -= 1) == 0)
4394         {
4395           int effective_cost = INSN_TICK (next) - clock;
4396
4397           /* For speculative insns, before inserting to ready/queue,
4398              check live, exception-free, and issue-delay */
4399           if (INSN_BB (next) != target_bb
4400               && (!IS_VALID (INSN_BB (next))
4401                   || CANT_MOVE (next)
4402                   || (IS_SPECULATIVE_INSN (next)
4403                       && (insn_issue_delay (next) > 3
4404                           || !check_live (next, INSN_BB (next))
4405                  || !is_exception_free (next, INSN_BB (next), target_bb)))))
4406             continue;
4407
4408           if (sched_verbose >= 2)
4409             {
4410               fprintf (dump, ";;\t\tdependences resolved: insn %d ", INSN_UID (next));
4411
4412               if (current_nr_blocks > 1 && INSN_BB (next) != target_bb)
4413                 fprintf (dump, "/b%d ", INSN_BLOCK (next));
4414
4415               if (effective_cost <= 1)
4416                 fprintf (dump, "into ready\n");
4417               else
4418                 fprintf (dump, "into queue with cost=%d\n", effective_cost);
4419             }
4420
4421           /* Adjust the priority of NEXT and either put it on the ready
4422              list or queue it.  */
4423           adjust_priority (next);
4424           if (effective_cost <= 1)
4425             ready[n_ready++] = next;
4426           else
4427             queue_insn (next, effective_cost);
4428         }
4429     }
4430
4431   /* Annotate the instruction with issue information -- TImode 
4432      indicates that the instruction is expected not to be able
4433      to issue on the same cycle as the previous insn.  A machine
4434      may use this information to decide how the instruction should
4435      be aligned.  */
4436   if (reload_completed && issue_rate > 1)
4437     {
4438       PUT_MODE (insn, clock > last_clock_var ? TImode : VOIDmode);
4439       last_clock_var = clock;
4440     }
4441
4442   return n_ready;
4443 }
4444
4445
4446 /* Add a REG_DEAD note for REG to INSN, reusing a REG_DEAD note from the
4447    dead_notes list.  */
4448
4449 static void
4450 create_reg_dead_note (reg, insn)
4451      rtx reg, insn;
4452 {
4453   rtx link;
4454
4455   /* The number of registers killed after scheduling must be the same as the
4456      number of registers killed before scheduling.  The number of REG_DEAD
4457      notes may not be conserved, i.e. two SImode hard register REG_DEAD notes
4458      might become one DImode hard register REG_DEAD note, but the number of
4459      registers killed will be conserved.
4460
4461      We carefully remove REG_DEAD notes from the dead_notes list, so that
4462      there will be none left at the end.  If we run out early, then there
4463      is a bug somewhere in flow, combine and/or sched.  */
4464
4465   if (dead_notes == 0)
4466     {
4467       if (current_nr_blocks <= 1)
4468         abort ();
4469       else
4470         link = alloc_EXPR_LIST (REG_DEAD, NULL_RTX, NULL_RTX);
4471     }
4472   else
4473     {
4474       /* Number of regs killed by REG.  */
4475       int regs_killed = (REGNO (reg) >= FIRST_PSEUDO_REGISTER ? 1
4476                          : HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)));
4477       /* Number of regs killed by REG_DEAD notes taken off the list.  */
4478       int reg_note_regs;
4479
4480       link = dead_notes;
4481       reg_note_regs = (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
4482                        : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
4483                                            GET_MODE (XEXP (link, 0))));
4484       while (reg_note_regs < regs_killed)
4485         {
4486           link = XEXP (link, 1);
4487
4488           /* LINK might be zero if we killed more registers after scheduling
4489              than before, and the last hard register we kill is actually
4490              multiple hard regs. 
4491
4492              This is normal for interblock scheduling, so deal with it in
4493              that case, else abort.  */
4494           if (link == NULL_RTX && current_nr_blocks <= 1)
4495             abort ();
4496           else if (link == NULL_RTX)
4497             link = alloc_EXPR_LIST (REG_DEAD, gen_rtx_REG (word_mode, 0),
4498                                     NULL_RTX);
4499              
4500           reg_note_regs += (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
4501                             : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
4502                                                 GET_MODE (XEXP (link, 0))));
4503         }
4504       dead_notes = XEXP (link, 1);
4505
4506       /* If we took too many regs kills off, put the extra ones back.  */
4507       while (reg_note_regs > regs_killed)
4508         {
4509           rtx temp_reg, temp_link;
4510
4511           temp_reg = gen_rtx_REG (word_mode, 0);
4512           temp_link = alloc_EXPR_LIST (REG_DEAD, temp_reg, dead_notes);
4513           dead_notes = temp_link;
4514           reg_note_regs--;
4515         }
4516     }
4517
4518   XEXP (link, 0) = reg;
4519   XEXP (link, 1) = REG_NOTES (insn);
4520   REG_NOTES (insn) = link;
4521 }
4522
4523 /* Subroutine on attach_deaths_insn--handles the recursive search
4524    through INSN.  If SET_P is true, then x is being modified by the insn.  */
4525
4526 static void
4527 attach_deaths (x, insn, set_p)
4528      rtx x;
4529      rtx insn;
4530      int set_p;
4531 {
4532   register int i;
4533   register int j;
4534   register enum rtx_code code;
4535   register char *fmt;
4536
4537   if (x == 0)
4538     return;
4539
4540   code = GET_CODE (x);
4541
4542   switch (code)
4543     {
4544     case CONST_INT:
4545     case CONST_DOUBLE:
4546     case LABEL_REF:
4547     case SYMBOL_REF:
4548     case CONST:
4549     case CODE_LABEL:
4550     case PC:
4551     case CC0:
4552       /* Get rid of the easy cases first.  */
4553       return;
4554
4555     case REG:
4556       {
4557         /* If the register dies in this insn, queue that note, and mark
4558            this register as needing to die.  */
4559         /* This code is very similar to mark_used_1 (if set_p is false)
4560            and mark_set_1 (if set_p is true) in flow.c.  */
4561
4562         register int regno;
4563         int some_needed;
4564         int all_needed;
4565
4566         if (set_p)
4567           return;
4568
4569         regno = REGNO (x);
4570         all_needed = some_needed = REGNO_REG_SET_P (old_live_regs, regno);
4571         if (regno < FIRST_PSEUDO_REGISTER)
4572           {
4573             int n;
4574
4575             n = HARD_REGNO_NREGS (regno, GET_MODE (x));
4576             while (--n > 0)
4577               {
4578                 int needed = (REGNO_REG_SET_P (old_live_regs, regno + n));
4579                 some_needed |= needed;
4580                 all_needed &= needed;
4581               }
4582           }
4583
4584         /* If it wasn't live before we started, then add a REG_DEAD note.
4585            We must check the previous lifetime info not the current info,
4586            because we may have to execute this code several times, e.g.
4587            once for a clobber (which doesn't add a note) and later
4588            for a use (which does add a note).
4589
4590            Always make the register live.  We must do this even if it was
4591            live before, because this may be an insn which sets and uses
4592            the same register, in which case the register has already been
4593            killed, so we must make it live again.
4594
4595            Global registers are always live, and should never have a REG_DEAD
4596            note added for them, so none of the code below applies to them.  */
4597
4598         if (regno >= FIRST_PSEUDO_REGISTER || ! global_regs[regno])
4599           {
4600             /* Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
4601                STACK_POINTER_REGNUM, since these are always considered to be
4602                live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
4603             if (regno != FRAME_POINTER_REGNUM
4604 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
4605                 && ! (regno == HARD_FRAME_POINTER_REGNUM)
4606 #endif
4607 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
4608                 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
4609 #endif
4610                 && regno != STACK_POINTER_REGNUM)
4611               {
4612                 if (! all_needed && ! dead_or_set_p (insn, x))
4613                   {
4614                     /* Check for the case where the register dying partially
4615                        overlaps the register set by this insn.  */
4616                     if (regno < FIRST_PSEUDO_REGISTER
4617                         && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
4618                       {
4619                         int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
4620                         while (--n >= 0)
4621                           some_needed |= dead_or_set_regno_p (insn, regno + n);
4622                       }
4623
4624                     /* If none of the words in X is needed, make a REG_DEAD
4625                        note.  Otherwise, we must make partial REG_DEAD
4626                        notes.  */
4627                     if (! some_needed)
4628                       create_reg_dead_note (x, insn);
4629                     else
4630                       {
4631                         int i;
4632
4633                         /* Don't make a REG_DEAD note for a part of a
4634                            register that is set in the insn.  */
4635                         for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
4636                              i >= 0; i--)
4637                           if (! REGNO_REG_SET_P (old_live_regs, regno+i)
4638                               && ! dead_or_set_regno_p (insn, regno + i))
4639                             create_reg_dead_note (gen_rtx_REG (reg_raw_mode[regno + i],
4640                                                                regno + i),
4641                                                   insn);
4642                       }
4643                   }
4644               }
4645
4646             if (regno < FIRST_PSEUDO_REGISTER)
4647               {
4648                 int j = HARD_REGNO_NREGS (regno, GET_MODE (x));
4649                 while (--j >= 0)
4650                   {
4651                     SET_REGNO_REG_SET (bb_live_regs, regno + j);
4652                   }
4653               }
4654             else
4655               {
4656                 /* Recompute REG_BASIC_BLOCK as we update all the other
4657                    dataflow information.  */
4658                 if (sched_reg_basic_block[regno] == REG_BLOCK_UNKNOWN)
4659                   sched_reg_basic_block[regno] = current_block_num;
4660                 else if (sched_reg_basic_block[regno] != current_block_num)
4661                   sched_reg_basic_block[regno] = REG_BLOCK_GLOBAL;
4662
4663                 SET_REGNO_REG_SET (bb_live_regs, regno);
4664               }
4665           }
4666         return;
4667       }
4668
4669     case MEM:
4670       /* Handle tail-recursive case.  */
4671       attach_deaths (XEXP (x, 0), insn, 0);
4672       return;
4673
4674     case SUBREG:
4675       attach_deaths (SUBREG_REG (x), insn,
4676                      set_p && ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
4677                                 <= UNITS_PER_WORD)
4678                                || (GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
4679                                    == GET_MODE_SIZE (GET_MODE ((x))))));
4680       return;
4681
4682     case STRICT_LOW_PART:
4683       attach_deaths (XEXP (x, 0), insn, 0);
4684       return;
4685
4686     case ZERO_EXTRACT:
4687     case SIGN_EXTRACT:
4688       attach_deaths (XEXP (x, 0), insn, 0);
4689       attach_deaths (XEXP (x, 1), insn, 0);
4690       attach_deaths (XEXP (x, 2), insn, 0);
4691       return;
4692
4693     case PARALLEL:
4694       if (set_p
4695           && GET_MODE (x) == BLKmode)
4696         {
4697           for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
4698             attach_deaths (SET_DEST (XVECEXP (x, 0, i)), insn, 1);
4699           return;
4700         }
4701
4702       /* fallthrough */
4703     default:
4704       /* Other cases: walk the insn.  */
4705       fmt = GET_RTX_FORMAT (code);
4706       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4707         {
4708           if (fmt[i] == 'e')
4709             attach_deaths (XEXP (x, i), insn, 0);
4710           else if (fmt[i] == 'E')
4711             for (j = 0; j < XVECLEN (x, i); j++)
4712               attach_deaths (XVECEXP (x, i, j), insn, 0);
4713         }
4714     }
4715 }
4716
4717 /* After INSN has executed, add register death notes for each register
4718    that is dead after INSN.  */
4719
4720 static void
4721 attach_deaths_insn (insn)
4722      rtx insn;
4723 {
4724   rtx x = PATTERN (insn);
4725   register RTX_CODE code = GET_CODE (x);
4726   rtx link;
4727
4728   if (code == SET)
4729     {
4730       attach_deaths (SET_SRC (x), insn, 0);
4731
4732       /* A register might die here even if it is the destination, e.g.
4733          it is the target of a volatile read and is otherwise unused.
4734          Hence we must always call attach_deaths for the SET_DEST.  */
4735       attach_deaths (SET_DEST (x), insn, 1);
4736     }
4737   else if (code == PARALLEL)
4738     {
4739       register int i;
4740       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
4741         {
4742           code = GET_CODE (XVECEXP (x, 0, i));
4743           if (code == SET)
4744             {
4745               attach_deaths (SET_SRC (XVECEXP (x, 0, i)), insn, 0);
4746
4747               attach_deaths (SET_DEST (XVECEXP (x, 0, i)), insn, 1);
4748             }
4749           /* Flow does not add REG_DEAD notes to registers that die in
4750              clobbers, so we can't either.  */
4751           else if (code != CLOBBER)
4752             attach_deaths (XVECEXP (x, 0, i), insn, 0);
4753         }
4754     }
4755   /* If this is a CLOBBER, only add REG_DEAD notes to registers inside a
4756      MEM being clobbered, just like flow.  */
4757   else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == MEM)
4758     attach_deaths (XEXP (XEXP (x, 0), 0), insn, 0);
4759   /* Otherwise don't add a death note to things being clobbered.  */
4760   else if (code != CLOBBER)
4761     attach_deaths (x, insn, 0);
4762
4763   /* Make death notes for things used in the called function.  */
4764   if (GET_CODE (insn) == CALL_INSN)
4765     for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
4766       attach_deaths (XEXP (XEXP (link, 0), 0), insn,
4767                      GET_CODE (XEXP (link, 0)) == CLOBBER);
4768 }
4769
4770 /* functions for handlnig of notes */
4771
4772 /* Delete notes beginning with INSN and put them in the chain
4773    of notes ended by NOTE_LIST.
4774    Returns the insn following the notes.  */
4775
4776 static rtx
4777 unlink_other_notes (insn, tail)
4778      rtx insn, tail;
4779 {
4780   rtx prev = PREV_INSN (insn);
4781
4782   while (insn != tail && GET_CODE (insn) == NOTE)
4783     {
4784       rtx next = NEXT_INSN (insn);
4785       /* Delete the note from its current position.  */
4786       if (prev)
4787         NEXT_INSN (prev) = next;
4788       if (next)
4789         PREV_INSN (next) = prev;
4790
4791       /* Don't save away NOTE_INSN_SETJMPs, because they must remain
4792          immediately after the call they follow.  We use a fake
4793          (REG_DEAD (const_int -1)) note to remember them.
4794          Likewise with NOTE_INSN_{LOOP,EHREGION}_{BEG, END}.  */
4795       if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_SETJMP
4796           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG
4797           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_END
4798           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_RANGE_START
4799           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_RANGE_END
4800           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_BEG
4801           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_END)
4802         {
4803           /* Insert the note at the end of the notes list.  */
4804           PREV_INSN (insn) = note_list;
4805           if (note_list)
4806             NEXT_INSN (note_list) = insn;
4807           note_list = insn;
4808         }
4809
4810       insn = next;
4811     }
4812   return insn;
4813 }
4814
4815 /* Delete line notes beginning with INSN. Record line-number notes so
4816    they can be reused.  Returns the insn following the notes.  */
4817
4818 static rtx
4819 unlink_line_notes (insn, tail)
4820      rtx insn, tail;
4821 {
4822   rtx prev = PREV_INSN (insn);
4823
4824   while (insn != tail && GET_CODE (insn) == NOTE)
4825     {
4826       rtx next = NEXT_INSN (insn);
4827
4828       if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
4829         {
4830           /* Delete the note from its current position.  */
4831           if (prev)
4832             NEXT_INSN (prev) = next;
4833           if (next)
4834             PREV_INSN (next) = prev;
4835
4836           /* Record line-number notes so they can be reused.  */
4837           LINE_NOTE (insn) = insn;
4838         }
4839       else
4840         prev = insn;
4841
4842       insn = next;
4843     }
4844   return insn;
4845 }
4846
4847 /* Return the head and tail pointers of BB.  */
4848
4849 HAIFA_INLINE static void
4850 get_block_head_tail (bb, headp, tailp)
4851      int bb;
4852      rtx *headp;
4853      rtx *tailp;
4854 {
4855
4856   rtx head;
4857   rtx tail;
4858   int b;
4859
4860   b = BB_TO_BLOCK (bb);
4861
4862   /* HEAD and TAIL delimit the basic block being scheduled.  */
4863   head = basic_block_head[b];
4864   tail = basic_block_end[b];
4865
4866   /* Don't include any notes or labels at the beginning of the
4867      basic block, or notes at the ends of basic blocks.  */
4868   while (head != tail)
4869     {
4870       if (GET_CODE (head) == NOTE)
4871         head = NEXT_INSN (head);
4872       else if (GET_CODE (tail) == NOTE)
4873         tail = PREV_INSN (tail);
4874       else if (GET_CODE (head) == CODE_LABEL)
4875         head = NEXT_INSN (head);
4876       else
4877         break;
4878     }
4879
4880   *headp = head;
4881   *tailp = tail;
4882 }
4883
4884 /* Delete line notes from bb. Save them so they can be later restored
4885    (in restore_line_notes ()).  */
4886
4887 static void
4888 rm_line_notes (bb)
4889      int bb;
4890 {
4891   rtx next_tail;
4892   rtx tail;
4893   rtx head;
4894   rtx insn;
4895
4896   get_block_head_tail (bb, &head, &tail);
4897
4898   if (head == tail
4899       && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
4900     return;
4901
4902   next_tail = NEXT_INSN (tail);
4903   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
4904     {
4905       rtx prev;
4906
4907       /* Farm out notes, and maybe save them in NOTE_LIST.
4908          This is needed to keep the debugger from
4909          getting completely deranged.  */
4910       if (GET_CODE (insn) == NOTE)
4911         {
4912           prev = insn;
4913           insn = unlink_line_notes (insn, next_tail);
4914
4915           if (prev == tail)
4916             abort ();
4917           if (prev == head)
4918             abort ();
4919           if (insn == next_tail)
4920             abort ();
4921         }
4922     }
4923 }
4924
4925 /* Save line number notes for each insn in bb.  */
4926
4927 static void
4928 save_line_notes (bb)
4929      int bb;
4930 {
4931   rtx head, tail;
4932   rtx next_tail;
4933
4934   /* We must use the true line number for the first insn in the block
4935      that was computed and saved at the start of this pass.  We can't
4936      use the current line number, because scheduling of the previous
4937      block may have changed the current line number.  */
4938
4939   rtx line = line_note_head[BB_TO_BLOCK (bb)];
4940   rtx insn;
4941
4942   get_block_head_tail (bb, &head, &tail);
4943   next_tail = NEXT_INSN (tail);
4944
4945   for (insn = basic_block_head[BB_TO_BLOCK (bb)];
4946        insn != next_tail;
4947        insn = NEXT_INSN (insn))
4948     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
4949       line = insn;
4950     else
4951       LINE_NOTE (insn) = line;
4952 }
4953
4954
4955 /* After bb was scheduled, insert line notes into the insns list.  */
4956
4957 static void
4958 restore_line_notes (bb)
4959      int bb;
4960 {
4961   rtx line, note, prev, new;
4962   int added_notes = 0;
4963   int b;
4964   rtx head, next_tail, insn;
4965
4966   b = BB_TO_BLOCK (bb);
4967
4968   head = basic_block_head[b];
4969   next_tail = NEXT_INSN (basic_block_end[b]);
4970
4971   /* Determine the current line-number.  We want to know the current
4972      line number of the first insn of the block here, in case it is
4973      different from the true line number that was saved earlier.  If
4974      different, then we need a line number note before the first insn
4975      of this block.  If it happens to be the same, then we don't want to
4976      emit another line number note here.  */
4977   for (line = head; line; line = PREV_INSN (line))
4978     if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
4979       break;
4980
4981   /* Walk the insns keeping track of the current line-number and inserting
4982      the line-number notes as needed.  */
4983   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
4984     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
4985       line = insn;
4986   /* This used to emit line number notes before every non-deleted note.
4987      However, this confuses a debugger, because line notes not separated
4988      by real instructions all end up at the same address.  I can find no
4989      use for line number notes before other notes, so none are emitted.  */
4990     else if (GET_CODE (insn) != NOTE
4991              && (note = LINE_NOTE (insn)) != 0
4992              && note != line
4993              && (line == 0
4994                  || NOTE_LINE_NUMBER (note) != NOTE_LINE_NUMBER (line)
4995                  || NOTE_SOURCE_FILE (note) != NOTE_SOURCE_FILE (line)))
4996       {
4997         line = note;
4998         prev = PREV_INSN (insn);
4999         if (LINE_NOTE (note))
5000           {
5001             /* Re-use the original line-number note.  */
5002             LINE_NOTE (note) = 0;
5003             PREV_INSN (note) = prev;
5004             NEXT_INSN (prev) = note;
5005             PREV_INSN (insn) = note;
5006             NEXT_INSN (note) = insn;
5007           }
5008         else
5009           {
5010             added_notes++;
5011             new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
5012             NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
5013             RTX_INTEGRATED_P (new) = RTX_INTEGRATED_P (note);
5014           }
5015       }
5016   if (sched_verbose && added_notes)
5017     fprintf (dump, ";; added %d line-number notes\n", added_notes);
5018 }
5019
5020 /* After scheduling the function, delete redundant line notes from the
5021    insns list.  */
5022
5023 static void
5024 rm_redundant_line_notes ()
5025 {
5026   rtx line = 0;
5027   rtx insn = get_insns ();
5028   int active_insn = 0;
5029   int notes = 0;
5030
5031   /* Walk the insns deleting redundant line-number notes.  Many of these
5032      are already present.  The remainder tend to occur at basic
5033      block boundaries.  */
5034   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
5035     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
5036       {
5037         /* If there are no active insns following, INSN is redundant.  */
5038         if (active_insn == 0)
5039           {
5040             notes++;
5041             NOTE_SOURCE_FILE (insn) = 0;
5042             NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
5043           }
5044         /* If the line number is unchanged, LINE is redundant.  */
5045         else if (line
5046                  && NOTE_LINE_NUMBER (line) == NOTE_LINE_NUMBER (insn)
5047                  && NOTE_SOURCE_FILE (line) == NOTE_SOURCE_FILE (insn))
5048           {
5049             notes++;
5050             NOTE_SOURCE_FILE (line) = 0;
5051             NOTE_LINE_NUMBER (line) = NOTE_INSN_DELETED;
5052             line = insn;
5053           }
5054         else
5055           line = insn;
5056         active_insn = 0;
5057       }
5058     else if (!((GET_CODE (insn) == NOTE
5059                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
5060                || (GET_CODE (insn) == INSN
5061                    && (GET_CODE (PATTERN (insn)) == USE
5062                        || GET_CODE (PATTERN (insn)) == CLOBBER))))
5063       active_insn++;
5064
5065   if (sched_verbose && notes)
5066     fprintf (dump, ";; deleted %d line-number notes\n", notes);
5067 }
5068
5069 /* Delete notes between head and tail and put them in the chain
5070    of notes ended by NOTE_LIST.  */
5071
5072 static void
5073 rm_other_notes (head, tail)
5074      rtx head;
5075      rtx tail;
5076 {
5077   rtx next_tail;
5078   rtx insn;
5079
5080   if (head == tail
5081       && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
5082     return;
5083
5084   next_tail = NEXT_INSN (tail);
5085   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
5086     {
5087       rtx prev;
5088
5089       /* Farm out notes, and maybe save them in NOTE_LIST.
5090          This is needed to keep the debugger from
5091          getting completely deranged.  */
5092       if (GET_CODE (insn) == NOTE)
5093         {
5094           prev = insn;
5095
5096           insn = unlink_other_notes (insn, next_tail);
5097
5098           if (prev == tail)
5099             abort ();
5100           if (prev == head)
5101             abort ();
5102           if (insn == next_tail)
5103             abort ();
5104         }
5105     }
5106 }
5107
5108 /* Constructor for `sometimes' data structure.  */
5109
5110 static int
5111 new_sometimes_live (regs_sometimes_live, regno, sometimes_max)
5112      struct sometimes *regs_sometimes_live;
5113      int regno;
5114      int sometimes_max;
5115 {
5116   register struct sometimes *p;
5117
5118   /* There should never be a register greater than max_regno here.  If there
5119      is, it means that a define_split has created a new pseudo reg.  This
5120      is not allowed, since there will not be flow info available for any
5121      new register, so catch the error here.  */
5122   if (regno >= max_regno)
5123     abort ();
5124
5125   p = &regs_sometimes_live[sometimes_max];
5126   p->regno = regno;
5127   p->live_length = 0;
5128   p->calls_crossed = 0;
5129   sometimes_max++;
5130   return sometimes_max;
5131 }
5132
5133 /* Count lengths of all regs we are currently tracking,
5134    and find new registers no longer live.  */
5135
5136 static void
5137 finish_sometimes_live (regs_sometimes_live, sometimes_max)
5138      struct sometimes *regs_sometimes_live;
5139      int sometimes_max;
5140 {
5141   int i;
5142
5143   for (i = 0; i < sometimes_max; i++)
5144     {
5145       register struct sometimes *p = &regs_sometimes_live[i];
5146       int regno = p->regno;
5147
5148       sched_reg_live_length[regno] += p->live_length;
5149       sched_reg_n_calls_crossed[regno] += p->calls_crossed;
5150     }
5151 }
5152
5153 /* functions for computation of registers live/usage info */
5154
5155 /* It is assumed that prior to scheduling basic_block_live_at_start (b)
5156    contains the registers that are alive at the entry to b.
5157
5158    Two passes follow: The first pass is performed before the scheduling
5159    of a region. It scans each block of the region forward, computing
5160    the set of registers alive at the end of the basic block and
5161    discard REG_DEAD notes (done by find_pre_sched_live ()).
5162
5163    The second path is invoked after scheduling all region blocks.
5164    It scans each block of the region backward, a block being traversed
5165    only after its succesors in the region. When the set of registers
5166    live at the end of a basic block may be changed by the scheduling
5167    (this may happen for multiple blocks region), it is computed as
5168    the union of the registers live at the start of its succesors.
5169    The last-use information is updated by inserting REG_DEAD notes.
5170    (done by find_post_sched_live ()) */
5171
5172 /* Scan all the insns to be scheduled, removing register death notes.
5173    Register death notes end up in DEAD_NOTES.
5174    Recreate the register life information for the end of this basic
5175    block.  */
5176
5177 static void
5178 find_pre_sched_live (bb)
5179      int bb;
5180 {
5181   rtx insn, next_tail, head, tail;
5182   int b = BB_TO_BLOCK (bb);
5183
5184   get_block_head_tail (bb, &head, &tail);
5185   COPY_REG_SET (bb_live_regs, basic_block_live_at_start[b]);
5186   next_tail = NEXT_INSN (tail);
5187
5188   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
5189     {
5190       rtx prev, next, link;
5191       int reg_weight = 0;
5192
5193       /* Handle register life information.  */
5194       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
5195         {
5196           /* See if the register gets born here.  */
5197           /* We must check for registers being born before we check for
5198              registers dying.  It is possible for a register to be born and
5199              die in the same insn, e.g. reading from a volatile memory
5200              location into an otherwise unused register.  Such a register
5201              must be marked as dead after this insn.  */
5202           if (GET_CODE (PATTERN (insn)) == SET
5203               || GET_CODE (PATTERN (insn)) == CLOBBER)
5204             {
5205               sched_note_set (PATTERN (insn), 0);
5206               reg_weight++;
5207             }
5208
5209           else if (GET_CODE (PATTERN (insn)) == PARALLEL)
5210             {
5211               int j;
5212               for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
5213                 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
5214                     || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
5215                   {
5216                     sched_note_set (XVECEXP (PATTERN (insn), 0, j), 0);
5217                     reg_weight++;
5218                   }
5219
5220               /* ??? This code is obsolete and should be deleted.  It
5221                  is harmless though, so we will leave it in for now.  */
5222               for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
5223                 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == USE)
5224                   sched_note_set (XVECEXP (PATTERN (insn), 0, j), 0);
5225             }
5226
5227           /* Each call cobbers (makes live) all call-clobbered regs
5228              that are not global or fixed.  Note that the function-value
5229              reg is a call_clobbered reg.  */
5230           if (GET_CODE (insn) == CALL_INSN)
5231             {
5232               int j;
5233               for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
5234                 if (call_used_regs[j] && !global_regs[j]
5235                     && ! fixed_regs[j])
5236                   {
5237                     SET_REGNO_REG_SET (bb_live_regs, j);
5238                   }
5239             }
5240
5241           /* Need to know what registers this insn kills.  */
5242           for (prev = 0, link = REG_NOTES (insn); link; link = next)
5243             {
5244               next = XEXP (link, 1);
5245               if ((REG_NOTE_KIND (link) == REG_DEAD
5246                    || REG_NOTE_KIND (link) == REG_UNUSED)
5247               /* Verify that the REG_NOTE has a valid value.  */
5248                   && GET_CODE (XEXP (link, 0)) == REG)
5249                 {
5250                   register int regno = REGNO (XEXP (link, 0));
5251
5252                   reg_weight--;
5253
5254                   /* Only unlink REG_DEAD notes; leave REG_UNUSED notes
5255                      alone.  */
5256                   if (REG_NOTE_KIND (link) == REG_DEAD)
5257                     {
5258                       if (prev)
5259                         XEXP (prev, 1) = next;
5260                       else
5261                         REG_NOTES (insn) = next;
5262                       XEXP (link, 1) = dead_notes;
5263                       dead_notes = link;
5264                     }
5265                   else
5266                     prev = link;
5267
5268                   if (regno < FIRST_PSEUDO_REGISTER)
5269                     {
5270                       int j = HARD_REGNO_NREGS (regno,
5271                                                 GET_MODE (XEXP (link, 0)));
5272                       while (--j >= 0)
5273                         {
5274                           CLEAR_REGNO_REG_SET (bb_live_regs, regno+j);
5275                         }
5276                     }
5277                   else
5278                     {
5279                       CLEAR_REGNO_REG_SET (bb_live_regs, regno);
5280                     }
5281                 }
5282               else
5283                 prev = link;
5284             }
5285         }
5286
5287       INSN_REG_WEIGHT (insn) = reg_weight;
5288     }
5289 }
5290
5291 /* Update register life and usage information for block bb
5292    after scheduling.  Put register dead notes back in the code.  */
5293
5294 static void
5295 find_post_sched_live (bb)
5296      int bb;
5297 {
5298   int sometimes_max;
5299   int j, i;
5300   int b;
5301   rtx insn;
5302   rtx head, tail, prev_head, next_tail;
5303
5304   register struct sometimes *regs_sometimes_live;
5305
5306   b = BB_TO_BLOCK (bb);
5307
5308   /* compute live regs at the end of bb as a function of its successors.  */
5309   if (current_nr_blocks > 1)
5310     {
5311       int e;
5312       int first_edge;
5313
5314       first_edge = e = OUT_EDGES (b);
5315       CLEAR_REG_SET (bb_live_regs);
5316
5317       if (e)
5318         do
5319           {
5320             int b_succ;
5321
5322             b_succ = TO_BLOCK (e);
5323             IOR_REG_SET (bb_live_regs, basic_block_live_at_start[b_succ]);
5324             e = NEXT_OUT (e);
5325           }
5326         while (e != first_edge);
5327     }
5328
5329   get_block_head_tail (bb, &head, &tail);
5330   next_tail = NEXT_INSN (tail);
5331   prev_head = PREV_INSN (head);
5332
5333   EXECUTE_IF_SET_IN_REG_SET (bb_live_regs, FIRST_PSEUDO_REGISTER, i,
5334                              {
5335                                sched_reg_basic_block[i] = REG_BLOCK_GLOBAL;
5336                              });
5337
5338   /* if the block is empty, same regs are alive at its end and its start.
5339      since this is not guaranteed after interblock scheduling, make sure they
5340      are truly identical.  */
5341   if (NEXT_INSN (prev_head) == tail
5342       && (GET_RTX_CLASS (GET_CODE (tail)) != 'i'))
5343     {
5344       if (current_nr_blocks > 1)
5345         COPY_REG_SET (basic_block_live_at_start[b], bb_live_regs);
5346
5347       return;
5348     }
5349
5350   b = BB_TO_BLOCK (bb);
5351   current_block_num = b;
5352
5353   /* Keep track of register lives.  */
5354   old_live_regs = ALLOCA_REG_SET ();
5355   regs_sometimes_live
5356     = (struct sometimes *) alloca (max_regno * sizeof (struct sometimes));
5357   sometimes_max = 0;
5358
5359   /* initiate "sometimes" data, starting with registers live at end */
5360   sometimes_max = 0;
5361   COPY_REG_SET (old_live_regs, bb_live_regs);
5362   EXECUTE_IF_SET_IN_REG_SET (bb_live_regs, 0, j,
5363                              {
5364                                sometimes_max
5365                                  = new_sometimes_live (regs_sometimes_live,
5366                                                        j, sometimes_max);
5367                              });
5368
5369   /* scan insns back, computing regs live info */
5370   for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
5371     {
5372       /* First we kill registers set by this insn, and then we
5373          make registers used by this insn live.  This is the opposite
5374          order used above because we are traversing the instructions
5375          backwards.  */
5376
5377       /* Strictly speaking, we should scan REG_UNUSED notes and make
5378          every register mentioned there live, however, we will just
5379          kill them again immediately below, so there doesn't seem to
5380          be any reason why we bother to do this.  */
5381
5382       /* See if this is the last notice we must take of a register.  */
5383       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
5384         continue;
5385
5386       if (GET_CODE (PATTERN (insn)) == SET
5387           || GET_CODE (PATTERN (insn)) == CLOBBER)
5388         sched_note_set (PATTERN (insn), 1);
5389       else if (GET_CODE (PATTERN (insn)) == PARALLEL)
5390         {
5391           for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
5392             if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
5393                 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
5394               sched_note_set (XVECEXP (PATTERN (insn), 0, j), 1);
5395         }
5396
5397       /* This code keeps life analysis information up to date.  */
5398       if (GET_CODE (insn) == CALL_INSN)
5399         {
5400           register struct sometimes *p;
5401
5402           /* A call kills all call used registers that are not
5403              global or fixed, except for those mentioned in the call
5404              pattern which will be made live again later.  */
5405           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5406             if (call_used_regs[i] && ! global_regs[i]
5407                 && ! fixed_regs[i])
5408               {
5409                 CLEAR_REGNO_REG_SET (bb_live_regs, i);
5410               }
5411
5412           /* Regs live at the time of a call instruction must not
5413              go in a register clobbered by calls.  Record this for
5414              all regs now live.  Note that insns which are born or
5415              die in a call do not cross a call, so this must be done
5416              after the killings (above) and before the births
5417              (below).  */
5418           p = regs_sometimes_live;
5419           for (i = 0; i < sometimes_max; i++, p++)
5420             if (REGNO_REG_SET_P (bb_live_regs, p->regno))
5421               p->calls_crossed += 1;
5422         }
5423
5424       /* Make every register used live, and add REG_DEAD notes for
5425          registers which were not live before we started.  */
5426       attach_deaths_insn (insn);
5427
5428       /* Find registers now made live by that instruction.  */
5429       EXECUTE_IF_AND_COMPL_IN_REG_SET (bb_live_regs, old_live_regs, 0, j,
5430                                  {
5431                                    sometimes_max
5432                                      = new_sometimes_live (regs_sometimes_live,
5433                                                            j, sometimes_max);
5434                                  });
5435       IOR_REG_SET (old_live_regs, bb_live_regs);
5436
5437       /* Count lengths of all regs we are worrying about now,
5438          and handle registers no longer live.  */
5439
5440       for (i = 0; i < sometimes_max; i++)
5441         {
5442           register struct sometimes *p = &regs_sometimes_live[i];
5443           int regno = p->regno;
5444
5445           p->live_length += 1;
5446
5447           if (!REGNO_REG_SET_P (bb_live_regs, regno))
5448             {
5449               /* This is the end of one of this register's lifetime
5450                  segments.  Save the lifetime info collected so far,
5451                  and clear its bit in the old_live_regs entry.  */
5452               sched_reg_live_length[regno] += p->live_length;
5453               sched_reg_n_calls_crossed[regno] += p->calls_crossed;
5454               CLEAR_REGNO_REG_SET (old_live_regs, p->regno);
5455
5456               /* Delete the reg_sometimes_live entry for this reg by
5457                  copying the last entry over top of it.  */
5458               *p = regs_sometimes_live[--sometimes_max];
5459               /* ...and decrement i so that this newly copied entry
5460                  will be processed.  */
5461               i--;
5462             }
5463         }
5464     }
5465
5466   finish_sometimes_live (regs_sometimes_live, sometimes_max);
5467
5468   /* In interblock scheduling, basic_block_live_at_start may have changed.  */
5469   if (current_nr_blocks > 1)
5470     COPY_REG_SET (basic_block_live_at_start[b], bb_live_regs);
5471
5472
5473   FREE_REG_SET (old_live_regs);
5474 }                               /* find_post_sched_live */
5475
5476 /* After scheduling the subroutine, restore information about uses of
5477    registers.  */
5478
5479 static void
5480 update_reg_usage ()
5481 {
5482   int regno;
5483
5484   if (n_basic_blocks > 0)
5485     EXECUTE_IF_SET_IN_REG_SET (bb_live_regs, FIRST_PSEUDO_REGISTER, regno,
5486                                {
5487                                  sched_reg_basic_block[regno]
5488                                    = REG_BLOCK_GLOBAL;
5489                                });
5490
5491   for (regno = 0; regno < max_regno; regno++)
5492     if (sched_reg_live_length[regno])
5493       {
5494         if (sched_verbose)
5495           {
5496             if (REG_LIVE_LENGTH (regno) > sched_reg_live_length[regno])
5497               fprintf (dump,
5498                        ";; register %d life shortened from %d to %d\n",
5499                        regno, REG_LIVE_LENGTH (regno),
5500                        sched_reg_live_length[regno]);
5501             /* Negative values are special; don't overwrite the current
5502                reg_live_length value if it is negative.  */
5503             else if (REG_LIVE_LENGTH (regno) < sched_reg_live_length[regno]
5504                      && REG_LIVE_LENGTH (regno) >= 0)
5505               fprintf (dump,
5506                        ";; register %d life extended from %d to %d\n",
5507                        regno, REG_LIVE_LENGTH (regno),
5508                        sched_reg_live_length[regno]);
5509
5510             if (!REG_N_CALLS_CROSSED (regno)
5511                 && sched_reg_n_calls_crossed[regno])
5512               fprintf (dump,
5513                        ";; register %d now crosses calls\n", regno);
5514             else if (REG_N_CALLS_CROSSED (regno)
5515                      && !sched_reg_n_calls_crossed[regno]
5516                      && REG_BASIC_BLOCK (regno) != REG_BLOCK_GLOBAL)
5517               fprintf (dump,
5518                        ";; register %d no longer crosses calls\n", regno);
5519
5520             if (REG_BASIC_BLOCK (regno) != sched_reg_basic_block[regno]
5521                 && sched_reg_basic_block[regno] != REG_BLOCK_UNKNOWN
5522                 && REG_BASIC_BLOCK(regno) != REG_BLOCK_UNKNOWN)
5523               fprintf (dump,
5524                        ";; register %d changed basic block from %d to %d\n",
5525                         regno, REG_BASIC_BLOCK(regno),
5526                         sched_reg_basic_block[regno]);
5527
5528           }
5529         /* Negative values are special; don't overwrite the current
5530            reg_live_length value if it is negative.  */
5531         if (REG_LIVE_LENGTH (regno) >= 0)
5532           REG_LIVE_LENGTH (regno) = sched_reg_live_length[regno];
5533
5534         if (sched_reg_basic_block[regno] != REG_BLOCK_UNKNOWN
5535             && REG_BASIC_BLOCK(regno) != REG_BLOCK_UNKNOWN)
5536           REG_BASIC_BLOCK(regno) = sched_reg_basic_block[regno];
5537
5538         /* We can't change the value of reg_n_calls_crossed to zero for
5539            pseudos which are live in more than one block.
5540
5541            This is because combine might have made an optimization which
5542            invalidated basic_block_live_at_start and reg_n_calls_crossed,
5543            but it does not update them.  If we update reg_n_calls_crossed
5544            here, the two variables are now inconsistent, and this might
5545            confuse the caller-save code into saving a register that doesn't
5546            need to be saved.  This is only a problem when we zero calls
5547            crossed for a pseudo live in multiple basic blocks.
5548
5549            Alternatively, we could try to correctly update basic block live
5550            at start here in sched, but that seems complicated.
5551
5552            Note: it is possible that a global register became local, as result
5553            of interblock motion, but will remain marked as a global register.  */
5554         if (sched_reg_n_calls_crossed[regno]
5555             || REG_BASIC_BLOCK (regno) != REG_BLOCK_GLOBAL)
5556           REG_N_CALLS_CROSSED (regno) = sched_reg_n_calls_crossed[regno];
5557
5558       }
5559 }
5560
5561 /* Scheduling clock, modified in schedule_block() and queue_to_ready () */
5562 static int clock_var;
5563
5564 /* Move insns that became ready to fire from queue to ready list.  */
5565
5566 static int
5567 queue_to_ready (ready, n_ready)
5568      rtx ready[];
5569      int n_ready;
5570 {
5571   rtx insn;
5572   rtx link;
5573
5574   q_ptr = NEXT_Q (q_ptr);
5575
5576   /* Add all pending insns that can be scheduled without stalls to the
5577      ready list.  */
5578   for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
5579     {
5580
5581       insn = XEXP (link, 0);
5582       q_size -= 1;
5583
5584       if (sched_verbose >= 2)
5585         fprintf (dump, ";;\t\tQ-->Ready: insn %d: ", INSN_UID (insn));
5586
5587       if (sched_verbose >= 2 && INSN_BB (insn) != target_bb)
5588         fprintf (dump, "(b%d) ", INSN_BLOCK (insn));
5589
5590       ready[n_ready++] = insn;
5591       if (sched_verbose >= 2)
5592         fprintf (dump, "moving to ready without stalls\n");
5593     }
5594   insn_queue[q_ptr] = 0;
5595
5596   /* If there are no ready insns, stall until one is ready and add all
5597      of the pending insns at that point to the ready list.  */
5598   if (n_ready == 0)
5599     {
5600       register int stalls;
5601
5602       for (stalls = 1; stalls < INSN_QUEUE_SIZE; stalls++)
5603         {
5604           if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
5605             {
5606               for (; link; link = XEXP (link, 1))
5607                 {
5608                   insn = XEXP (link, 0);
5609                   q_size -= 1;
5610
5611                   if (sched_verbose >= 2)
5612                     fprintf (dump, ";;\t\tQ-->Ready: insn %d: ", INSN_UID (insn));
5613
5614                   if (sched_verbose >= 2 && INSN_BB (insn) != target_bb)
5615                     fprintf (dump, "(b%d) ", INSN_BLOCK (insn));
5616
5617                   ready[n_ready++] = insn;
5618                   if (sched_verbose >= 2)
5619                     fprintf (dump, "moving to ready with %d stalls\n", stalls);
5620                 }
5621               insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = 0;
5622
5623               if (n_ready)
5624                 break;
5625             }
5626         }
5627
5628       if (sched_verbose && stalls)
5629         visualize_stall_cycles (BB_TO_BLOCK (target_bb), stalls);
5630       q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
5631       clock_var += stalls;
5632     }
5633   return n_ready;
5634 }
5635
5636 /* Print the ready list for debugging purposes. Callable from debugger.  */
5637
5638 static void
5639 debug_ready_list (ready, n_ready)
5640      rtx ready[];
5641      int n_ready;
5642 {
5643   int i;
5644
5645   for (i = 0; i < n_ready; i++)
5646     {
5647       fprintf (dump, "  %d", INSN_UID (ready[i]));
5648       if (current_nr_blocks > 1 && INSN_BB (ready[i]) != target_bb)
5649         fprintf (dump, "/b%d", INSN_BLOCK (ready[i]));
5650     }
5651   fprintf (dump, "\n");
5652 }
5653
5654 /* Print names of units on which insn can/should execute, for debugging.  */
5655
5656 static void
5657 insn_print_units (insn)
5658      rtx insn;
5659 {
5660   int i;
5661   int unit = insn_unit (insn);
5662
5663   if (unit == -1)
5664     fprintf (dump, "none");
5665   else if (unit >= 0)
5666     fprintf (dump, "%s", function_units[unit].name);
5667   else
5668     {
5669       fprintf (dump, "[");
5670       for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
5671         if (unit & 1)
5672           {
5673             fprintf (dump, "%s", function_units[i].name);
5674             if (unit != 1)
5675               fprintf (dump, " ");
5676           }
5677       fprintf (dump, "]");
5678     }
5679 }
5680
5681 /* MAX_VISUAL_LINES is the maximum number of lines in visualization table
5682    of a basic block.  If more lines are needed, table is splitted to two.
5683    n_visual_lines is the number of lines printed so far for a block.
5684    visual_tbl contains the block visualization info.
5685    vis_no_unit holds insns in a cycle that are not mapped to any unit.  */
5686 #define MAX_VISUAL_LINES 100
5687 #define INSN_LEN 30
5688 int n_visual_lines;
5689 char *visual_tbl;
5690 int n_vis_no_unit;
5691 rtx vis_no_unit[10];
5692
5693 /* Finds units that are in use in this fuction. Required only
5694    for visualization.  */
5695
5696 static void
5697 init_target_units ()
5698 {
5699   rtx insn;
5700   int unit;
5701
5702   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
5703     {
5704       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
5705         continue;
5706
5707       unit = insn_unit (insn);
5708
5709       if (unit < 0)
5710         target_units |= ~unit;
5711       else
5712         target_units |= (1 << unit);
5713     }
5714 }
5715
5716 /* Return the length of the visualization table */
5717
5718 static int
5719 get_visual_tbl_length ()
5720 {
5721   int unit, i;
5722   int n, n1;
5723   char *s;
5724
5725   /* compute length of one field in line */
5726   s = (char *) alloca (INSN_LEN + 5);
5727   sprintf (s, "  %33s", "uname");
5728   n1 = strlen (s);
5729
5730   /* compute length of one line */
5731   n = strlen (";; ");
5732   n += n1;
5733   for (unit = 0; unit < FUNCTION_UNITS_SIZE; unit++)
5734     if (function_units[unit].bitmask & target_units)
5735       for (i = 0; i < function_units[unit].multiplicity; i++)
5736         n += n1;
5737   n += n1;
5738   n += strlen ("\n") + 2;
5739
5740   /* compute length of visualization string */
5741   return (MAX_VISUAL_LINES * n);
5742 }
5743
5744 /* Init block visualization debugging info */
5745
5746 static void
5747 init_block_visualization ()
5748 {
5749   strcpy (visual_tbl, "");
5750   n_visual_lines = 0;
5751   n_vis_no_unit = 0;
5752 }
5753
5754 #define BUF_LEN 256
5755
5756 static char *
5757 safe_concat (buf, cur, str)
5758      char *buf;
5759      char *cur;
5760      char *str;
5761 {
5762   char *end = buf + BUF_LEN - 2;        /* leave room for null */
5763   int c;
5764
5765   if (cur > end)
5766     {
5767       *end = '\0';
5768       return end;
5769     }
5770
5771   while (cur < end && (c = *str++) != '\0')
5772     *cur++ = c;
5773
5774   *cur = '\0';
5775   return cur;
5776 }
5777
5778 /* This recognizes rtx, I classified as expressions. These are always */
5779 /* represent some action on values or results of other expression, */
5780 /* that may be stored in objects representing values.  */
5781
5782 static void
5783 print_exp (buf, x, verbose)
5784      char *buf;
5785      rtx x;
5786      int verbose;
5787 {
5788   char tmp[BUF_LEN];
5789   char *st[4];
5790   char *cur = buf;
5791   char *fun = (char *)0;
5792   char *sep;
5793   rtx op[4];
5794   int i;
5795
5796   for (i = 0; i < 4; i++)
5797     {
5798       st[i] = (char *)0;
5799       op[i] = NULL_RTX;
5800     }
5801
5802   switch (GET_CODE (x))
5803     {
5804     case PLUS:
5805       op[0] = XEXP (x, 0);
5806       st[1] = "+";
5807       op[1] = XEXP (x, 1);
5808       break;
5809     case LO_SUM:
5810       op[0] = XEXP (x, 0);
5811       st[1] = "+low(";
5812       op[1] = XEXP (x, 1);
5813       st[2] = ")";
5814       break;
5815     case MINUS:
5816       op[0] = XEXP (x, 0);
5817       st[1] = "-";
5818       op[1] = XEXP (x, 1);
5819       break;
5820     case COMPARE:
5821       fun = "cmp";
5822       op[0] = XEXP (x, 0);
5823       op[1] = XEXP (x, 1);
5824       break;
5825     case NEG:
5826       st[0] = "-";
5827       op[0] = XEXP (x, 0);
5828       break;
5829     case MULT:
5830       op[0] = XEXP (x, 0);
5831       st[1] = "*";
5832       op[1] = XEXP (x, 1);
5833       break;
5834     case DIV:
5835       op[0] = XEXP (x, 0);
5836       st[1] = "/";
5837       op[1] = XEXP (x, 1);
5838       break;
5839     case UDIV:
5840       fun = "udiv";
5841       op[0] = XEXP (x, 0);
5842       op[1] = XEXP (x, 1);
5843       break;
5844     case MOD:
5845       op[0] = XEXP (x, 0);
5846       st[1] = "%";
5847       op[1] = XEXP (x, 1);
5848       break;
5849     case UMOD:
5850       fun = "umod";
5851       op[0] = XEXP (x, 0);
5852       op[1] = XEXP (x, 1);
5853       break;
5854     case SMIN:
5855       fun = "smin";
5856       op[0] = XEXP (x, 0);
5857       op[1] = XEXP (x, 1);
5858       break;
5859     case SMAX:
5860       fun = "smax";
5861       op[0] = XEXP (x, 0);
5862       op[1] = XEXP (x, 1);
5863       break;
5864     case UMIN:
5865       fun = "umin";
5866       op[0] = XEXP (x, 0);
5867       op[1] = XEXP (x, 1);
5868       break;
5869     case UMAX:
5870       fun = "umax";
5871       op[0] = XEXP (x, 0);
5872       op[1] = XEXP (x, 1);
5873       break;
5874     case NOT:
5875       st[0] = "!";
5876       op[0] = XEXP (x, 0);
5877       break;
5878     case AND:
5879       op[0] = XEXP (x, 0);
5880       st[1] = "&";
5881       op[1] = XEXP (x, 1);
5882       break;
5883     case IOR:
5884       op[0] = XEXP (x, 0);
5885       st[1] = "|";
5886       op[1] = XEXP (x, 1);
5887       break;
5888     case XOR:
5889       op[0] = XEXP (x, 0);
5890       st[1] = "^";
5891       op[1] = XEXP (x, 1);
5892       break;
5893     case ASHIFT:
5894       op[0] = XEXP (x, 0);
5895       st[1] = "<<";
5896       op[1] = XEXP (x, 1);
5897       break;
5898     case LSHIFTRT:
5899       op[0] = XEXP (x, 0);
5900       st[1] = " 0>>";
5901       op[1] = XEXP (x, 1);
5902       break;
5903     case ASHIFTRT:
5904       op[0] = XEXP (x, 0);
5905       st[1] = ">>";
5906       op[1] = XEXP (x, 1);
5907       break;
5908     case ROTATE:
5909       op[0] = XEXP (x, 0);
5910       st[1] = "<-<";
5911       op[1] = XEXP (x, 1);
5912       break;
5913     case ROTATERT:
5914       op[0] = XEXP (x, 0);
5915       st[1] = ">->";
5916       op[1] = XEXP (x, 1);
5917       break;
5918     case ABS:
5919       fun = "abs";
5920       op[0] = XEXP (x, 0);
5921       break;
5922     case SQRT:
5923       fun = "sqrt";
5924       op[0] = XEXP (x, 0);
5925       break;
5926     case FFS:
5927       fun = "ffs";
5928       op[0] = XEXP (x, 0);
5929       break;
5930     case EQ:
5931       op[0] = XEXP (x, 0);
5932       st[1] = "==";
5933       op[1] = XEXP (x, 1);
5934       break;
5935     case NE:
5936       op[0] = XEXP (x, 0);
5937       st[1] = "!=";
5938       op[1] = XEXP (x, 1);
5939       break;
5940     case GT:
5941       op[0] = XEXP (x, 0);
5942       st[1] = ">";
5943       op[1] = XEXP (x, 1);
5944       break;
5945     case GTU:
5946       fun = "gtu";
5947       op[0] = XEXP (x, 0);
5948       op[1] = XEXP (x, 1);
5949       break;
5950     case LT:
5951       op[0] = XEXP (x, 0);
5952       st[1] = "<";
5953       op[1] = XEXP (x, 1);
5954       break;
5955     case LTU:
5956       fun = "ltu";
5957       op[0] = XEXP (x, 0);
5958       op[1] = XEXP (x, 1);
5959       break;
5960     case GE:
5961       op[0] = XEXP (x, 0);
5962       st[1] = ">=";
5963       op[1] = XEXP (x, 1);
5964       break;
5965     case GEU:
5966       fun = "geu";
5967       op[0] = XEXP (x, 0);
5968       op[1] = XEXP (x, 1);
5969       break;
5970     case LE:
5971       op[0] = XEXP (x, 0);
5972       st[1] = "<=";
5973       op[1] = XEXP (x, 1);
5974       break;
5975     case LEU:
5976       fun = "leu";
5977       op[0] = XEXP (x, 0);
5978       op[1] = XEXP (x, 1);
5979       break;
5980     case SIGN_EXTRACT:
5981       fun = (verbose) ? "sign_extract" : "sxt";
5982       op[0] = XEXP (x, 0);
5983       op[1] = XEXP (x, 1);
5984       op[2] = XEXP (x, 2);
5985       break;
5986     case ZERO_EXTRACT:
5987       fun = (verbose) ? "zero_extract" : "zxt";
5988       op[0] = XEXP (x, 0);
5989       op[1] = XEXP (x, 1);
5990       op[2] = XEXP (x, 2);
5991       break;
5992     case SIGN_EXTEND:
5993       fun = (verbose) ? "sign_extend" : "sxn";
5994       op[0] = XEXP (x, 0);
5995       break;
5996     case ZERO_EXTEND:
5997       fun = (verbose) ? "zero_extend" : "zxn";
5998       op[0] = XEXP (x, 0);
5999       break;
6000     case FLOAT_EXTEND:
6001       fun = (verbose) ? "float_extend" : "fxn";
6002       op[0] = XEXP (x, 0);
6003       break;
6004     case TRUNCATE:
6005       fun = (verbose) ? "trunc" : "trn";
6006       op[0] = XEXP (x, 0);
6007       break;
6008     case FLOAT_TRUNCATE:
6009       fun = (verbose) ? "float_trunc" : "ftr";
6010       op[0] = XEXP (x, 0);
6011       break;
6012     case FLOAT:
6013       fun = (verbose) ? "float" : "flt";
6014       op[0] = XEXP (x, 0);
6015       break;
6016     case UNSIGNED_FLOAT:
6017       fun = (verbose) ? "uns_float" : "ufl";
6018       op[0] = XEXP (x, 0);
6019       break;
6020     case FIX:
6021       fun = "fix";
6022       op[0] = XEXP (x, 0);
6023       break;
6024     case UNSIGNED_FIX:
6025       fun = (verbose) ? "uns_fix" : "ufx";
6026       op[0] = XEXP (x, 0);
6027       break;
6028     case PRE_DEC:
6029       st[0] = "--";
6030       op[0] = XEXP (x, 0);
6031       break;
6032     case PRE_INC:
6033       st[0] = "++";
6034       op[0] = XEXP (x, 0);
6035       break;
6036     case POST_DEC:
6037       op[0] = XEXP (x, 0);
6038       st[1] = "--";
6039       break;
6040     case POST_INC:
6041       op[0] = XEXP (x, 0);
6042       st[1] = "++";
6043       break;
6044     case CALL:
6045       st[0] = "call ";
6046       op[0] = XEXP (x, 0);
6047       if (verbose)
6048         {
6049           st[1] = " argc:";
6050           op[1] = XEXP (x, 1);
6051         }
6052       break;
6053     case IF_THEN_ELSE:
6054       st[0] = "{(";
6055       op[0] = XEXP (x, 0);
6056       st[1] = ")?";
6057       op[1] = XEXP (x, 1);
6058       st[2] = ":";
6059       op[2] = XEXP (x, 2);
6060       st[3] = "}";
6061       break;
6062     case TRAP_IF:
6063       fun = "trap_if";
6064       op[0] = TRAP_CONDITION (x);
6065       break;
6066     case UNSPEC:
6067     case UNSPEC_VOLATILE:
6068       {
6069         cur = safe_concat (buf, cur, "unspec");
6070         if (GET_CODE (x) == UNSPEC_VOLATILE)
6071           cur = safe_concat (buf, cur, "/v");
6072         cur = safe_concat (buf, cur, "[");
6073         sep = "";
6074         for (i = 0; i < XVECLEN (x, 0); i++)
6075           {
6076             print_pattern (tmp, XVECEXP (x, 0, i), verbose);
6077             cur = safe_concat (buf, cur, sep);
6078             cur = safe_concat (buf, cur, tmp);
6079             sep = ",";
6080           }
6081         cur = safe_concat (buf, cur, "] ");
6082         sprintf (tmp, "%d", XINT (x, 1));
6083         cur = safe_concat (buf, cur, tmp);
6084       }
6085       break;
6086     default:
6087       /* if (verbose) debug_rtx (x); */
6088       st[0] = GET_RTX_NAME (GET_CODE (x));
6089       break;
6090     }
6091
6092   /* Print this as a function? */
6093   if (fun)
6094     {
6095       cur = safe_concat (buf, cur, fun);
6096       cur = safe_concat (buf, cur, "(");
6097     }
6098
6099   for (i = 0; i < 4; i++)
6100     {
6101       if (st[i])
6102         cur = safe_concat (buf, cur, st[i]);
6103
6104       if (op[i])
6105         {
6106           if (fun && i != 0)
6107             cur = safe_concat (buf, cur, ",");
6108
6109           print_value (tmp, op[i], verbose);
6110           cur = safe_concat (buf, cur, tmp);
6111         }
6112     }
6113
6114   if (fun)
6115     cur = safe_concat (buf, cur, ")");
6116 }               /* print_exp */
6117
6118 /* Prints rtxes, i customly classified as values. They're constants, */
6119 /* registers, labels, symbols and memory accesses.  */
6120
6121 static void
6122 print_value (buf, x, verbose)
6123      char *buf;
6124      rtx x;
6125      int verbose;
6126 {
6127   char t[BUF_LEN];
6128   char *cur = buf;
6129
6130   switch (GET_CODE (x))
6131     {
6132     case CONST_INT:
6133       sprintf (t, "0x%lx", (long)INTVAL (x));
6134       cur = safe_concat (buf, cur, t);
6135       break;
6136     case CONST_DOUBLE:
6137       sprintf (t, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6138       cur = safe_concat (buf, cur, t);
6139       break;
6140     case CONST_STRING:
6141       cur = safe_concat (buf, cur, "\"");
6142       cur = safe_concat (buf, cur, XSTR (x, 0));
6143       cur = safe_concat (buf, cur, "\"");
6144       break;
6145     case SYMBOL_REF:
6146       cur = safe_concat (buf, cur, "`");
6147       cur = safe_concat (buf, cur, XSTR (x, 0));
6148       cur = safe_concat (buf, cur, "'");
6149       break;
6150     case LABEL_REF:
6151       sprintf (t, "L%d", INSN_UID (XEXP (x, 0)));
6152       cur = safe_concat (buf, cur, t);
6153       break;
6154     case CONST:
6155       print_value (t, XEXP (x, 0), verbose);
6156       cur = safe_concat (buf, cur, "const(");
6157       cur = safe_concat (buf, cur, t);
6158       cur = safe_concat (buf, cur, ")");
6159       break;
6160     case HIGH:
6161       print_value (t, XEXP (x, 0), verbose);
6162       cur = safe_concat (buf, cur, "high(");
6163       cur = safe_concat (buf, cur, t);
6164       cur = safe_concat (buf, cur, ")");
6165       break;
6166     case REG:
6167       if (REGNO (x) < FIRST_PSEUDO_REGISTER)
6168         {
6169           int c = reg_names[ REGNO (x) ][0];
6170           if (c >= '0' && c <= '9')
6171             cur = safe_concat (buf, cur, "%");
6172
6173           cur = safe_concat (buf, cur, reg_names[ REGNO (x) ]);
6174         }
6175       else
6176         {
6177           sprintf (t, "r%d", REGNO (x));
6178           cur = safe_concat (buf, cur, t);
6179         }
6180       break;
6181     case SUBREG:
6182       print_value (t, SUBREG_REG (x), verbose);
6183       cur = safe_concat (buf, cur, t);
6184       sprintf (t, "#%d", SUBREG_WORD (x));
6185       cur = safe_concat (buf, cur, t);
6186       break;
6187     case SCRATCH:
6188       cur = safe_concat (buf, cur, "scratch");
6189       break;
6190     case CC0:
6191       cur = safe_concat (buf, cur, "cc0");
6192       break;
6193     case PC:
6194       cur = safe_concat (buf, cur, "pc");
6195       break;
6196     case MEM:
6197       print_value (t, XEXP (x, 0), verbose);
6198       cur = safe_concat (buf, cur, "[");
6199       cur = safe_concat (buf, cur, t);
6200       cur = safe_concat (buf, cur, "]");
6201       break;
6202     default:
6203       print_exp (t, x, verbose);
6204       cur = safe_concat (buf, cur, t);
6205       break;
6206     }
6207 }                               /* print_value */
6208
6209 /* The next step in insn detalization, its pattern recognition */
6210
6211 static void
6212 print_pattern (buf, x, verbose)
6213      char *buf;
6214      rtx x;
6215      int verbose;
6216 {
6217   char t1[BUF_LEN], t2[BUF_LEN], t3[BUF_LEN];
6218
6219   switch (GET_CODE (x))
6220     {
6221     case SET:
6222       print_value (t1, SET_DEST (x), verbose);
6223       print_value (t2, SET_SRC (x), verbose);
6224       sprintf (buf, "%s=%s", t1, t2);
6225       break;
6226     case RETURN:
6227       sprintf (buf, "return");
6228       break;
6229     case CALL:
6230       print_exp (buf, x, verbose);
6231       break;
6232     case CLOBBER:
6233       print_value (t1, XEXP (x, 0), verbose);
6234       sprintf (buf, "clobber %s", t1);
6235       break;
6236     case USE:
6237       print_value (t1, XEXP (x, 0), verbose);
6238       sprintf (buf, "use %s", t1);
6239       break;
6240     case PARALLEL:
6241       {
6242         int i;
6243
6244         sprintf (t1, "{");
6245         for (i = 0; i < XVECLEN (x, 0); i++)
6246           {
6247             print_pattern (t2, XVECEXP (x, 0, i), verbose);
6248             sprintf (t3, "%s%s;", t1, t2);
6249             strcpy (t1, t3);
6250           }
6251         sprintf (buf, "%s}", t1);
6252       }
6253       break;
6254     case SEQUENCE:
6255       {
6256         int i;
6257
6258         sprintf (t1, "%%{");
6259         for (i = 0; i < XVECLEN (x, 0); i++)
6260           {
6261             print_insn (t2, XVECEXP (x, 0, i), verbose);
6262             sprintf (t3, "%s%s;", t1, t2);
6263             strcpy (t1, t3);
6264           }
6265         sprintf (buf, "%s%%}", t1);
6266       }
6267       break;
6268     case ASM_INPUT:
6269       sprintf (buf, "asm {%s}", XSTR (x, 0));
6270       break;
6271     case ADDR_VEC:
6272       break;
6273     case ADDR_DIFF_VEC:
6274       print_value (buf, XEXP (x, 0), verbose);
6275       break;
6276     case TRAP_IF:
6277       print_value (t1, TRAP_CONDITION (x), verbose);
6278       sprintf (buf, "trap_if %s", t1);
6279       break;
6280     case UNSPEC:
6281       {
6282         int i;
6283
6284         sprintf (t1, "unspec{");
6285         for (i = 0; i < XVECLEN (x, 0); i++)
6286           {
6287             print_pattern (t2, XVECEXP (x, 0, i), verbose);
6288             sprintf (t3, "%s%s;", t1, t2);
6289             strcpy (t1, t3);
6290           }
6291         sprintf (buf, "%s}", t1);
6292       }
6293       break;
6294     case UNSPEC_VOLATILE:
6295       {
6296         int i;
6297
6298         sprintf (t1, "unspec/v{");
6299         for (i = 0; i < XVECLEN (x, 0); i++)
6300           {
6301             print_pattern (t2, XVECEXP (x, 0, i), verbose);
6302             sprintf (t3, "%s%s;", t1, t2);
6303             strcpy (t1, t3);
6304           }
6305         sprintf (buf, "%s}", t1);
6306       }
6307       break;
6308     default:
6309       print_value (buf, x, verbose);
6310     }
6311 }                               /* print_pattern */
6312
6313 /* This is the main function in rtl visualization mechanism. It
6314    accepts an rtx and tries to recognize it as an insn, then prints it
6315    properly in human readable form, resembling assembler mnemonics.  */
6316 /* For every insn it prints its UID and BB the insn belongs */
6317 /* too. (probably the last "option" should be extended somehow, since */
6318 /* it depends now on sched.c inner variables ...) */
6319
6320 static void
6321 print_insn (buf, x, verbose)
6322      char *buf;
6323      rtx x;
6324      int verbose;
6325 {
6326   char t[BUF_LEN];
6327   rtx insn = x;
6328
6329   switch (GET_CODE (x))
6330     {
6331     case INSN:
6332       print_pattern (t, PATTERN (x), verbose);
6333       if (verbose)
6334         sprintf (buf, "b%d: i% 4d: %s", INSN_BB (x),
6335                  INSN_UID (x), t);
6336       else
6337         sprintf (buf, "%-4d %s", INSN_UID (x), t);
6338       break;
6339     case JUMP_INSN:
6340       print_pattern (t, PATTERN (x), verbose);
6341       if (verbose)
6342         sprintf (buf, "b%d: i% 4d: jump %s", INSN_BB (x),
6343                  INSN_UID (x), t);
6344       else
6345         sprintf (buf, "%-4d %s", INSN_UID (x), t);
6346       break;
6347     case CALL_INSN:
6348       x = PATTERN (insn);
6349       if (GET_CODE (x) == PARALLEL)
6350         {
6351           x = XVECEXP (x, 0, 0);
6352           print_pattern (t, x, verbose);
6353         }
6354       else
6355         strcpy (t, "call <...>");
6356       if (verbose)
6357         sprintf (buf, "b%d: i% 4d: %s", INSN_BB (insn),
6358                  INSN_UID (insn), t);
6359       else
6360         sprintf (buf, "%-4d %s", INSN_UID (insn), t);
6361       break;
6362     case CODE_LABEL:
6363       sprintf (buf, "L%d:", INSN_UID (x));
6364       break;
6365     case BARRIER:
6366       sprintf (buf, "i% 4d: barrier", INSN_UID (x));
6367       break;
6368     case NOTE:
6369       if (NOTE_LINE_NUMBER (x) > 0)
6370         sprintf (buf, "%4d note \"%s\" %d", INSN_UID (x),
6371                  NOTE_SOURCE_FILE (x), NOTE_LINE_NUMBER (x));
6372       else
6373         sprintf (buf, "%4d %s", INSN_UID (x),
6374                  GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (x)));
6375       break;
6376     default:
6377       if (verbose)
6378         {
6379           sprintf (buf, "Not an INSN at all\n");
6380           debug_rtx (x);
6381         }
6382       else
6383         sprintf (buf, "i%-4d  <What?>", INSN_UID (x));
6384     }
6385 }                               /* print_insn */
6386
6387 /* Print visualization debugging info */
6388
6389 static void
6390 print_block_visualization (b, s)
6391      int b;
6392      char *s;
6393 {
6394   int unit, i;
6395
6396   /* print header */
6397   fprintf (dump, "\n;;   ==================== scheduling visualization for block %d %s \n", b, s);
6398
6399   /* Print names of units */
6400   fprintf (dump, ";;   %-8s", "clock");
6401   for (unit = 0; unit < FUNCTION_UNITS_SIZE; unit++)
6402     if (function_units[unit].bitmask & target_units)
6403       for (i = 0; i < function_units[unit].multiplicity; i++)
6404         fprintf (dump, "  %-33s", function_units[unit].name);
6405   fprintf (dump, "  %-8s\n", "no-unit");
6406
6407   fprintf (dump, ";;   %-8s", "=====");
6408   for (unit = 0; unit < FUNCTION_UNITS_SIZE; unit++)
6409     if (function_units[unit].bitmask & target_units)
6410       for (i = 0; i < function_units[unit].multiplicity; i++)
6411         fprintf (dump, "  %-33s", "==============================");
6412   fprintf (dump, "  %-8s\n", "=======");
6413
6414   /* Print insns in each cycle */
6415   fprintf (dump, "%s\n", visual_tbl);
6416 }
6417
6418 /* Print insns in the 'no_unit' column of visualization */
6419
6420 static void
6421 visualize_no_unit (insn)
6422      rtx insn;
6423 {
6424   vis_no_unit[n_vis_no_unit] = insn;
6425   n_vis_no_unit++;
6426 }
6427
6428 /* Print insns scheduled in clock, for visualization.  */
6429
6430 static void
6431 visualize_scheduled_insns (b, clock)
6432      int b, clock;
6433 {
6434   int i, unit;
6435
6436   /* if no more room, split table into two */
6437   if (n_visual_lines >= MAX_VISUAL_LINES)
6438     {
6439       print_block_visualization (b, "(incomplete)");
6440       init_block_visualization ();
6441     }
6442
6443   n_visual_lines++;
6444
6445   sprintf (visual_tbl + strlen (visual_tbl), ";;   %-8d", clock);
6446   for (unit = 0; unit < FUNCTION_UNITS_SIZE; unit++)
6447     if (function_units[unit].bitmask & target_units)
6448       for (i = 0; i < function_units[unit].multiplicity; i++)
6449         {
6450           int instance = unit + i * FUNCTION_UNITS_SIZE;
6451           rtx insn = unit_last_insn[instance];
6452
6453           /* print insns that still keep the unit busy */
6454           if (insn &&
6455               actual_hazard_this_instance (unit, instance, insn, clock, 0))
6456             {
6457               char str[BUF_LEN];
6458               print_insn (str, insn, 0);
6459               str[INSN_LEN] = '\0';
6460               sprintf (visual_tbl + strlen (visual_tbl), "  %-33s", str);
6461             }
6462           else
6463             sprintf (visual_tbl + strlen (visual_tbl), "  %-33s", "------------------------------");
6464         }
6465
6466   /* print insns that are not assigned to any unit */
6467   for (i = 0; i < n_vis_no_unit; i++)
6468     sprintf (visual_tbl + strlen (visual_tbl), "  %-8d",
6469              INSN_UID (vis_no_unit[i]));
6470   n_vis_no_unit = 0;
6471
6472   sprintf (visual_tbl + strlen (visual_tbl), "\n");
6473 }
6474
6475 /* Print stalled cycles */
6476
6477 static void
6478 visualize_stall_cycles (b, stalls)
6479      int b, stalls;
6480 {
6481   int i;
6482
6483   /* if no more room, split table into two */
6484   if (n_visual_lines >= MAX_VISUAL_LINES)
6485     {
6486       print_block_visualization (b, "(incomplete)");
6487       init_block_visualization ();
6488     }
6489
6490   n_visual_lines++;
6491
6492   sprintf (visual_tbl + strlen (visual_tbl), ";;       ");
6493   for (i = 0; i < stalls; i++)
6494     sprintf (visual_tbl + strlen (visual_tbl), ".");
6495   sprintf (visual_tbl + strlen (visual_tbl), "\n");
6496 }
6497
6498 /* move_insn1: Remove INSN from insn chain, and link it after LAST insn */
6499
6500 static rtx
6501 move_insn1 (insn, last)
6502      rtx insn, last;
6503 {
6504   NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
6505   PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
6506
6507   NEXT_INSN (insn) = NEXT_INSN (last);
6508   PREV_INSN (NEXT_INSN (last)) = insn;
6509
6510   NEXT_INSN (last) = insn;
6511   PREV_INSN (insn) = last;
6512
6513   return insn;
6514 }
6515
6516 /* Search INSN for fake REG_DEAD note pairs for NOTE_INSN_SETJMP,
6517    NOTE_INSN_{LOOP,EHREGION}_{BEG,END}; and convert them back into
6518    NOTEs.  The REG_DEAD note following first one is contains the saved
6519    value for NOTE_BLOCK_NUMBER which is useful for
6520    NOTE_INSN_EH_REGION_{BEG,END} NOTEs.  LAST is the last instruction
6521    output by the instruction scheduler.  Return the new value of LAST.  */
6522
6523 static rtx
6524 reemit_notes (insn, last)
6525      rtx insn;
6526      rtx last;
6527 {
6528   rtx note, retval;
6529
6530   retval = last;
6531   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
6532     {
6533       if (REG_NOTE_KIND (note) == REG_DEAD
6534           && GET_CODE (XEXP (note, 0)) == CONST_INT)
6535         {
6536           int note_type = INTVAL (XEXP (note, 0));
6537           if (note_type == NOTE_INSN_SETJMP)
6538             {
6539               retval = emit_note_after (NOTE_INSN_SETJMP, insn);
6540               CONST_CALL_P (retval) = CONST_CALL_P (note);
6541               remove_note (insn, note);
6542               note = XEXP (note, 1);
6543             }
6544           else if (note_type == NOTE_INSN_RANGE_START
6545                    || note_type == NOTE_INSN_RANGE_END)
6546             {
6547               last = emit_note_before (note_type, last);
6548               remove_note (insn, note);
6549               note = XEXP (note, 1);
6550               NOTE_RANGE_INFO (last) = XEXP (note, 0);
6551             }
6552           else
6553             {
6554               last = emit_note_before (INTVAL (XEXP (note, 0)), last);
6555               remove_note (insn, note);
6556               note = XEXP (note, 1);
6557               NOTE_BLOCK_NUMBER (last) = INTVAL (XEXP (note, 0));
6558             }
6559           remove_note (insn, note);
6560         }
6561     }
6562   return retval;
6563 }
6564
6565 /* Move INSN, and all insns which should be issued before it,
6566    due to SCHED_GROUP_P flag.  Reemit notes if needed.
6567
6568    Return the last insn emitted by the scheduler, which is the
6569    return value from the first call to reemit_notes.  */
6570
6571 static rtx
6572 move_insn (insn, last)
6573      rtx insn, last;
6574 {
6575   rtx retval = NULL;
6576
6577   /* If INSN has SCHED_GROUP_P set, then issue it and any other
6578      insns with SCHED_GROUP_P set first.  */
6579   while (SCHED_GROUP_P (insn))
6580     {
6581       rtx prev = PREV_INSN (insn);
6582
6583       /* Move a SCHED_GROUP_P insn.  */
6584       move_insn1 (insn, last);
6585       /* If this is the first call to reemit_notes, then record
6586          its return value.  */
6587       if (retval == NULL_RTX)
6588         retval = reemit_notes (insn, insn);
6589       else
6590         reemit_notes (insn, insn);
6591       insn = prev;
6592     }
6593
6594   /* Now move the first non SCHED_GROUP_P insn.  */
6595   move_insn1 (insn, last);
6596
6597   /* If this is the first call to reemit_notes, then record
6598      its return value.  */
6599   if (retval == NULL_RTX)
6600     retval = reemit_notes (insn, insn);
6601   else
6602     reemit_notes (insn, insn);
6603
6604   return retval;
6605 }
6606
6607 /* Return an insn which represents a SCHED_GROUP, which is
6608    the last insn in the group.  */
6609
6610 static rtx
6611 group_leader (insn)
6612      rtx insn;
6613 {
6614   rtx prev;
6615
6616   do
6617     {
6618       prev = insn;
6619       insn = next_nonnote_insn (insn);
6620     }
6621   while (insn && SCHED_GROUP_P (insn) && (GET_CODE (insn) != CODE_LABEL));
6622
6623   return prev;
6624 }
6625
6626 /* Use forward list scheduling to rearrange insns of block BB in region RGN,
6627    possibly bringing insns from subsequent blocks in the same region.
6628    Return number of insns scheduled.  */
6629
6630 static int
6631 schedule_block (bb, rgn_n_insns)
6632      int bb;
6633      int rgn_n_insns;
6634 {
6635   /* Local variables.  */
6636   rtx insn, last;
6637   rtx *ready;
6638   int i;
6639   int n_ready = 0;
6640   int can_issue_more;
6641
6642   /* flow block of this bb */
6643   int b = BB_TO_BLOCK (bb);
6644
6645   /* target_n_insns == number of insns in b before scheduling starts.
6646      sched_target_n_insns == how many of b's insns were scheduled.
6647      sched_n_insns == how many insns were scheduled in b */
6648   int target_n_insns = 0;
6649   int sched_target_n_insns = 0;
6650   int sched_n_insns = 0;
6651
6652 #define NEED_NOTHING    0
6653 #define NEED_HEAD       1
6654 #define NEED_TAIL       2
6655   int new_needs;
6656
6657   /* head/tail info for this block */
6658   rtx prev_head;
6659   rtx next_tail;
6660   rtx head;
6661   rtx tail;
6662   int bb_src;
6663
6664   /* We used to have code to avoid getting parameters moved from hard
6665      argument registers into pseudos.
6666
6667      However, it was removed when it proved to be of marginal benefit
6668      and caused problems because schedule_block and compute_forward_dependences
6669      had different notions of what the "head" insn was.  */
6670   get_block_head_tail (bb, &head, &tail);
6671
6672   /* Interblock scheduling could have moved the original head insn from this
6673      block into a proceeding block.  This may also cause schedule_block and
6674      compute_forward_dependences to have different notions of what the
6675      "head" insn was.
6676
6677      If the interblock movement happened to make this block start with
6678      some notes (LOOP, EH or SETJMP) before the first real insn, then
6679      HEAD will have various special notes attached to it which must be
6680      removed so that we don't end up with extra copies of the notes.  */
6681   if (GET_RTX_CLASS (GET_CODE (head)) == 'i')
6682     {
6683       rtx note;
6684
6685       for (note = REG_NOTES (head); note; note = XEXP (note, 1))
6686         if (REG_NOTE_KIND (note) == REG_DEAD
6687             && GET_CODE (XEXP (note, 0)) == CONST_INT)
6688           remove_note (head, note);
6689     }
6690
6691   next_tail = NEXT_INSN (tail);
6692   prev_head = PREV_INSN (head);
6693
6694   /* If the only insn left is a NOTE or a CODE_LABEL, then there is no need
6695      to schedule this block.  */
6696   if (head == tail
6697       && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
6698     return (sched_n_insns);
6699
6700   /* debug info */
6701   if (sched_verbose)
6702     {
6703       fprintf (dump, ";;   ======================================================\n");
6704       fprintf (dump,
6705                ";;   -- basic block %d from %d to %d -- %s reload\n",
6706                b, INSN_UID (basic_block_head[b]),
6707                INSN_UID (basic_block_end[b]),
6708                (reload_completed ? "after" : "before"));
6709       fprintf (dump, ";;   ======================================================\n");
6710       fprintf (dump, "\n");
6711
6712       visual_tbl = (char *) alloca (get_visual_tbl_length ());
6713       init_block_visualization ();
6714     }
6715
6716   /* remove remaining note insns from the block, save them in
6717      note_list.  These notes are restored at the end of
6718      schedule_block ().  */
6719   note_list = 0;
6720   rm_other_notes (head, tail);
6721
6722   target_bb = bb;
6723
6724   /* prepare current target block info */
6725   if (current_nr_blocks > 1)
6726     {
6727       candidate_table = (candidate *) alloca (current_nr_blocks * sizeof (candidate));
6728
6729       bblst_last = 0;
6730       /* ??? It is not clear why bblst_size is computed this way.  The original
6731          number was clearly too small as it resulted in compiler failures.
6732          Multiplying by the original number by 2 (to account for update_bbs
6733          members) seems to be a reasonable solution.  */
6734       /* ??? Or perhaps there is a bug somewhere else in this file?  */
6735       bblst_size = (current_nr_blocks - bb) * rgn_nr_edges * 2;
6736       bblst_table = (int *) alloca (bblst_size * sizeof (int));
6737
6738       bitlst_table_last = 0;
6739       bitlst_table_size = rgn_nr_edges;
6740       bitlst_table = (int *) alloca (rgn_nr_edges * sizeof (int));
6741
6742       compute_trg_info (bb);
6743     }
6744
6745   clear_units ();
6746
6747   /* Allocate the ready list */
6748   ready = (rtx *) alloca ((rgn_n_insns + 1) * sizeof (rtx));
6749
6750   /* Print debugging information.  */
6751   if (sched_verbose >= 5)
6752     debug_dependencies ();
6753
6754
6755   /* Initialize ready list with all 'ready' insns in target block.
6756      Count number of insns in the target block being scheduled.  */
6757   n_ready = 0;
6758   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
6759     {
6760       rtx next;
6761
6762       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
6763         continue;
6764       next = NEXT_INSN (insn);
6765
6766       if (INSN_DEP_COUNT (insn) == 0
6767           && (SCHED_GROUP_P (next) == 0 || GET_RTX_CLASS (GET_CODE (next)) != 'i'))
6768         ready[n_ready++] = insn;
6769       if (!(SCHED_GROUP_P (insn)))
6770         target_n_insns++;
6771     }
6772
6773   /* Add to ready list all 'ready' insns in valid source blocks.
6774      For speculative insns, check-live, exception-free, and
6775      issue-delay.  */
6776   for (bb_src = bb + 1; bb_src < current_nr_blocks; bb_src++)
6777     if (IS_VALID (bb_src))
6778       {
6779         rtx src_head;
6780         rtx src_next_tail;
6781         rtx tail, head;
6782
6783         get_block_head_tail (bb_src, &head, &tail);
6784         src_next_tail = NEXT_INSN (tail);
6785         src_head = head;
6786
6787         if (head == tail
6788             && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
6789           continue;
6790
6791         for (insn = src_head; insn != src_next_tail; insn = NEXT_INSN (insn))
6792           {
6793             if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
6794               continue;
6795
6796             if (!CANT_MOVE (insn)
6797                 && (!IS_SPECULATIVE_INSN (insn)
6798                     || (insn_issue_delay (insn) <= 3
6799                         && check_live (insn, bb_src)
6800                         && is_exception_free (insn, bb_src, target_bb))))
6801
6802               {
6803                 rtx next;
6804
6805                 next = NEXT_INSN (insn);
6806                 if (INSN_DEP_COUNT (insn) == 0
6807                     && (SCHED_GROUP_P (next) == 0
6808                         || GET_RTX_CLASS (GET_CODE (next)) != 'i'))
6809                   ready[n_ready++] = insn;
6810               }
6811           }
6812       }
6813
6814 #ifdef MD_SCHED_INIT
6815   MD_SCHED_INIT (dump, sched_verbose);
6816 #endif
6817
6818   /* no insns scheduled in this block yet */
6819   last_scheduled_insn = 0;
6820
6821   /* Sort the ready list */
6822   SCHED_SORT (ready, n_ready);
6823 #ifdef MD_SCHED_REORDER
6824   MD_SCHED_REORDER (dump, sched_verbose, ready, n_ready);
6825 #endif
6826
6827   if (sched_verbose >= 2)
6828     {
6829       fprintf (dump, ";;\t\tReady list initially:             ");
6830       debug_ready_list (ready, n_ready);
6831     }
6832
6833   /* Q_SIZE is the total number of insns in the queue.  */
6834   q_ptr = 0;
6835   q_size = 0;
6836   clock_var = 0;
6837   last_clock_var = 0;
6838   bzero ((char *) insn_queue, sizeof (insn_queue));
6839
6840   /* We start inserting insns after PREV_HEAD.  */
6841   last = prev_head;
6842
6843   /* Initialize INSN_QUEUE, LIST and NEW_NEEDS.  */
6844   new_needs = (NEXT_INSN (prev_head) == basic_block_head[b]
6845                ? NEED_HEAD : NEED_NOTHING);
6846   if (PREV_INSN (next_tail) == basic_block_end[b])
6847     new_needs |= NEED_TAIL;
6848
6849   /* loop until all the insns in BB are scheduled.  */
6850   while (sched_target_n_insns < target_n_insns)
6851     {
6852       int b1;
6853
6854       clock_var++;
6855
6856       /* Add to the ready list all pending insns that can be issued now.
6857          If there are no ready insns, increment clock until one
6858          is ready and add all pending insns at that point to the ready
6859          list.  */
6860       n_ready = queue_to_ready (ready, n_ready);
6861
6862       if (n_ready == 0)
6863         abort ();
6864
6865       if (sched_verbose >= 2)
6866         {
6867           fprintf (dump, ";;\t\tReady list after queue_to_ready:  ");
6868           debug_ready_list (ready, n_ready);
6869         }
6870
6871       /* Sort the ready list.  */
6872       SCHED_SORT (ready, n_ready);
6873 #ifdef MD_SCHED_REORDER
6874       MD_SCHED_REORDER (dump, sched_verbose, ready, n_ready);
6875 #endif
6876
6877       if (sched_verbose)
6878         {
6879           fprintf (dump, "\n;;\tReady list (t =%3d):  ", clock_var);
6880           debug_ready_list (ready, n_ready);
6881         }
6882
6883       /* Issue insns from ready list.
6884          It is important to count down from n_ready, because n_ready may change
6885          as insns are issued.  */
6886       can_issue_more = issue_rate;
6887       for (i = n_ready - 1; i >= 0 && can_issue_more; i--)
6888         {
6889           rtx insn = ready[i];
6890           int cost = actual_hazard (insn_unit (insn), insn, clock_var, 0);
6891
6892           if (cost > 1)
6893             {
6894               queue_insn (insn, cost);
6895               ready[i] = ready[--n_ready];      /* remove insn from ready list */
6896             }
6897           else if (cost == 0)
6898             {
6899               /* an interblock motion? */
6900               if (INSN_BB (insn) != target_bb)
6901                 {
6902                   rtx temp;
6903
6904                   if (IS_SPECULATIVE_INSN (insn))
6905                     {
6906
6907                       if (!check_live (insn, INSN_BB (insn)))
6908                         {
6909                           /* speculative motion, live check failed, remove
6910                              insn from ready list */
6911                           ready[i] = ready[--n_ready];
6912                           continue;
6913                         }
6914                       update_live (insn, INSN_BB (insn));
6915
6916                       /* for speculative load, mark insns fed by it.  */
6917                       if (IS_LOAD_INSN (insn) || FED_BY_SPEC_LOAD (insn))
6918                         set_spec_fed (insn);
6919
6920                       nr_spec++;
6921                     }
6922                   nr_inter++;
6923
6924                   temp = insn;
6925                   while (SCHED_GROUP_P (temp))
6926                     temp = PREV_INSN (temp);
6927
6928                   /* Update source block boundaries.   */
6929                   b1 = INSN_BLOCK (temp);
6930                   if (temp == basic_block_head[b1]
6931                       && insn == basic_block_end[b1])
6932                     {
6933                       /* We moved all the insns in the basic block.
6934                          Emit a note after the last insn and update the
6935                          begin/end boundaries to point to the note.  */
6936                       emit_note_after (NOTE_INSN_DELETED, insn);
6937                       basic_block_end[b1] = NEXT_INSN (insn);
6938                       basic_block_head[b1] = NEXT_INSN (insn);
6939                     }
6940                   else if (insn == basic_block_end[b1])
6941                     {
6942                       /* We took insns from the end of the basic block,
6943                          so update the end of block boundary so that it
6944                          points to the first insn we did not move.  */
6945                       basic_block_end[b1] = PREV_INSN (temp);
6946                     }
6947                   else if (temp == basic_block_head[b1])
6948                     {
6949                       /* We took insns from the start of the basic block,
6950                          so update the start of block boundary so that
6951                          it points to the first insn we did not move.  */
6952                       basic_block_head[b1] = NEXT_INSN (insn);
6953                     }
6954                 }
6955               else
6956                 {
6957                   /* in block motion */
6958                   sched_target_n_insns++;
6959                 }
6960
6961               last_scheduled_insn = insn;
6962               last = move_insn (insn, last);
6963               sched_n_insns++;
6964
6965 #ifdef MD_SCHED_VARIABLE_ISSUE
6966               MD_SCHED_VARIABLE_ISSUE (dump, sched_verbose, insn, can_issue_more);
6967 #else
6968               can_issue_more--;
6969 #endif
6970
6971               n_ready = schedule_insn (insn, ready, n_ready, clock_var);
6972
6973               /* remove insn from ready list */
6974               ready[i] = ready[--n_ready];
6975
6976               /* close this block after scheduling its jump */
6977               if (GET_CODE (last_scheduled_insn) == JUMP_INSN)
6978                 break;
6979             }
6980         }
6981
6982       /* debug info */
6983       if (sched_verbose)
6984         {
6985           visualize_scheduled_insns (b, clock_var);
6986         }
6987     }
6988
6989   /* debug info */
6990   if (sched_verbose)
6991     {
6992       fprintf (dump, ";;\tReady list (final):  ");
6993       debug_ready_list (ready, n_ready);
6994       print_block_visualization (b, "");
6995     }
6996
6997   /* Sanity check -- queue must be empty now.  Meaningless if region has
6998      multiple bbs.  */
6999   if (current_nr_blocks > 1)
7000     if (!flag_schedule_interblock && q_size != 0)
7001       abort ();
7002
7003   /* update head/tail boundaries.  */
7004   head = NEXT_INSN (prev_head);
7005   tail = last;
7006
7007   /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
7008      previously found among the insns.  Insert them at the beginning
7009      of the insns.  */
7010   if (note_list != 0)
7011     {
7012       rtx note_head = note_list;
7013
7014       while (PREV_INSN (note_head))
7015         {
7016           note_head = PREV_INSN (note_head);
7017         }
7018
7019       PREV_INSN (note_head) = PREV_INSN (head);
7020       NEXT_INSN (PREV_INSN (head)) = note_head;
7021       PREV_INSN (head) = note_list;
7022       NEXT_INSN (note_list) = head;
7023       head = note_head;
7024     }
7025
7026   /* update target block boundaries.  */
7027   if (new_needs & NEED_HEAD)
7028     basic_block_head[b] = head;
7029
7030   if (new_needs & NEED_TAIL)
7031     basic_block_end[b] = tail;
7032
7033   /* debugging */
7034   if (sched_verbose)
7035     {
7036       fprintf (dump, ";;   total time = %d\n;;   new basic block head = %d\n",
7037                clock_var, INSN_UID (basic_block_head[b]));
7038       fprintf (dump, ";;   new basic block end = %d\n\n",
7039                INSN_UID (basic_block_end[b]));
7040     }
7041
7042   return (sched_n_insns);
7043 }                               /* schedule_block () */
7044 \f
7045
7046 /* print the bit-set of registers, S.  callable from debugger */
7047
7048 extern void
7049 debug_reg_vector (s)
7050      regset s;
7051 {
7052   int regno;
7053
7054   EXECUTE_IF_SET_IN_REG_SET (s, 0, regno,
7055                              {
7056                                fprintf (dump, " %d", regno);
7057                              });
7058
7059   fprintf (dump, "\n");
7060 }
7061
7062 /* Use the backward dependences from LOG_LINKS to build
7063    forward dependences in INSN_DEPEND.  */
7064
7065 static void
7066 compute_block_forward_dependences (bb)
7067      int bb;
7068 {
7069   rtx insn, link;
7070   rtx tail, head;
7071   rtx next_tail;
7072   enum reg_note dep_type;
7073
7074   get_block_head_tail (bb, &head, &tail);
7075   next_tail = NEXT_INSN (tail);
7076   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
7077     {
7078       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
7079         continue;
7080
7081       insn = group_leader (insn);
7082
7083       for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
7084         {
7085           rtx x = group_leader (XEXP (link, 0));
7086           rtx new_link;
7087
7088           if (x != XEXP (link, 0))
7089             continue;
7090
7091           /* Ignore dependences upon deleted insn */
7092           if (GET_CODE (x) == NOTE || INSN_DELETED_P (x))
7093             continue;
7094           if (find_insn_list (insn, INSN_DEPEND (x)))
7095             continue;
7096
7097           new_link = alloc_INSN_LIST (insn, INSN_DEPEND (x));
7098
7099           dep_type = REG_NOTE_KIND (link);
7100           PUT_REG_NOTE_KIND (new_link, dep_type);
7101
7102           INSN_DEPEND (x) = new_link;
7103           INSN_DEP_COUNT (insn) += 1;
7104         }
7105     }
7106 }
7107
7108 /* Initialize variables for region data dependence analysis.
7109    n_bbs is the number of region blocks */
7110
7111 __inline static void
7112 init_rgn_data_dependences (n_bbs)
7113      int n_bbs;
7114 {
7115   int bb;
7116
7117   /* variables for which one copy exists for each block */
7118   bzero ((char *) bb_pending_read_insns, n_bbs * sizeof (rtx));
7119   bzero ((char *) bb_pending_read_mems, n_bbs * sizeof (rtx));
7120   bzero ((char *) bb_pending_write_insns, n_bbs * sizeof (rtx));
7121   bzero ((char *) bb_pending_write_mems, n_bbs * sizeof (rtx));
7122   bzero ((char *) bb_pending_lists_length, n_bbs * sizeof (rtx));
7123   bzero ((char *) bb_last_pending_memory_flush, n_bbs * sizeof (rtx));
7124   bzero ((char *) bb_last_function_call, n_bbs * sizeof (rtx));
7125   bzero ((char *) bb_sched_before_next_call, n_bbs * sizeof (rtx));
7126
7127   /* Create an insn here so that we can hang dependencies off of it later.  */
7128   for (bb = 0; bb < n_bbs; bb++)
7129     {
7130       bb_sched_before_next_call[bb] =
7131         gen_rtx_INSN (VOIDmode, 0, NULL_RTX, NULL_RTX,
7132                       NULL_RTX, 0, NULL_RTX, NULL_RTX);
7133       LOG_LINKS (bb_sched_before_next_call[bb]) = 0;
7134     }
7135 }
7136
7137 /* Add dependences so that branches are scheduled to run last in their block */
7138
7139 static void
7140 add_branch_dependences (head, tail)
7141      rtx head, tail;
7142 {
7143
7144   rtx insn, last;
7145
7146   /* For all branches, calls, uses, and cc0 setters, force them to remain
7147      in order at the end of the block by adding dependencies and giving
7148      the last a high priority.  There may be notes present, and prev_head
7149      may also be a note.
7150
7151      Branches must obviously remain at the end.  Calls should remain at the
7152      end since moving them results in worse register allocation.  Uses remain
7153      at the end to ensure proper register allocation.  cc0 setters remaim
7154      at the end because they can't be moved away from their cc0 user.  */
7155   insn = tail;
7156   last = 0;
7157   while (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
7158          || (GET_CODE (insn) == INSN
7159              && (GET_CODE (PATTERN (insn)) == USE
7160 #ifdef HAVE_cc0
7161                  || sets_cc0_p (PATTERN (insn))
7162 #endif
7163              ))
7164          || GET_CODE (insn) == NOTE)
7165     {
7166       if (GET_CODE (insn) != NOTE)
7167         {
7168           if (last != 0
7169               && !find_insn_list (insn, LOG_LINKS (last)))
7170             {
7171               add_dependence (last, insn, REG_DEP_ANTI);
7172               INSN_REF_COUNT (insn)++;
7173             }
7174
7175           CANT_MOVE (insn) = 1;
7176
7177           last = insn;
7178           /* Skip over insns that are part of a group.
7179              Make each insn explicitly depend on the previous insn.
7180              This ensures that only the group header will ever enter
7181              the ready queue (and, when scheduled, will automatically
7182              schedule the SCHED_GROUP_P block).  */
7183           while (SCHED_GROUP_P (insn))
7184             {
7185               rtx temp = prev_nonnote_insn (insn);
7186               add_dependence (insn, temp, REG_DEP_ANTI);
7187               insn = temp;
7188             }
7189         }
7190
7191       /* Don't overrun the bounds of the basic block.  */
7192       if (insn == head)
7193         break;
7194
7195       insn = PREV_INSN (insn);
7196     }
7197
7198   /* make sure these insns are scheduled last in their block */
7199   insn = last;
7200   if (insn != 0)
7201     while (insn != head)
7202       {
7203         insn = prev_nonnote_insn (insn);
7204
7205         if (INSN_REF_COUNT (insn) != 0)
7206           continue;
7207
7208         if (!find_insn_list (last, LOG_LINKS (insn)))
7209           add_dependence (last, insn, REG_DEP_ANTI);
7210         INSN_REF_COUNT (insn) = 1;
7211
7212         /* Skip over insns that are part of a group.  */
7213         while (SCHED_GROUP_P (insn))
7214           insn = prev_nonnote_insn (insn);
7215       }
7216 }
7217
7218 /* Compute bacward dependences inside BB.  In a multiple blocks region:
7219    (1) a bb is analyzed after its predecessors, and (2) the lists in
7220    effect at the end of bb (after analyzing for bb) are inherited by
7221    bb's successrs.
7222
7223    Specifically for reg-reg data dependences, the block insns are
7224    scanned by sched_analyze () top-to-bottom.  Two lists are
7225    naintained by sched_analyze (): reg_last_defs[] for register DEFs,
7226    and reg_last_uses[] for register USEs.
7227
7228    When analysis is completed for bb, we update for its successors:
7229    ;  - DEFS[succ] = Union (DEFS [succ], DEFS [bb])
7230    ;  - USES[succ] = Union (USES [succ], DEFS [bb])
7231
7232    The mechanism for computing mem-mem data dependence is very
7233    similar, and the result is interblock dependences in the region.  */
7234
7235 static void
7236 compute_block_backward_dependences (bb)
7237      int bb;
7238 {
7239   int b;
7240   rtx x;
7241   rtx head, tail;
7242   int max_reg = max_reg_num ();
7243
7244   b = BB_TO_BLOCK (bb);
7245
7246   if (current_nr_blocks == 1)
7247     {
7248       reg_last_uses = (rtx *) alloca (max_reg * sizeof (rtx));
7249       reg_last_sets = (rtx *) alloca (max_reg * sizeof (rtx));
7250
7251       bzero ((char *) reg_last_uses, max_reg * sizeof (rtx));
7252       bzero ((char *) reg_last_sets, max_reg * sizeof (rtx));
7253
7254       pending_read_insns = 0;
7255       pending_read_mems = 0;
7256       pending_write_insns = 0;
7257       pending_write_mems = 0;
7258       pending_lists_length = 0;
7259       last_function_call = 0;
7260       last_pending_memory_flush = 0;
7261       sched_before_next_call
7262         = gen_rtx_INSN (VOIDmode, 0, NULL_RTX, NULL_RTX,
7263                         NULL_RTX, 0, NULL_RTX, NULL_RTX);
7264       LOG_LINKS (sched_before_next_call) = 0;
7265     }
7266   else
7267     {
7268       reg_last_uses = bb_reg_last_uses[bb];
7269       reg_last_sets = bb_reg_last_sets[bb];
7270
7271       pending_read_insns = bb_pending_read_insns[bb];
7272       pending_read_mems = bb_pending_read_mems[bb];
7273       pending_write_insns = bb_pending_write_insns[bb];
7274       pending_write_mems = bb_pending_write_mems[bb];
7275       pending_lists_length = bb_pending_lists_length[bb];
7276       last_function_call = bb_last_function_call[bb];
7277       last_pending_memory_flush = bb_last_pending_memory_flush[bb];
7278
7279       sched_before_next_call = bb_sched_before_next_call[bb];
7280     }
7281
7282   /* do the analysis for this block */
7283   get_block_head_tail (bb, &head, &tail);
7284   sched_analyze (head, tail);
7285   add_branch_dependences (head, tail);
7286
7287   if (current_nr_blocks > 1)
7288     {
7289       int e, first_edge;
7290       int b_succ, bb_succ;
7291       int reg;
7292       rtx link_insn, link_mem;
7293       rtx u;
7294
7295       /* these lists should point to the right place, for correct freeing later.  */
7296       bb_pending_read_insns[bb] = pending_read_insns;
7297       bb_pending_read_mems[bb] = pending_read_mems;
7298       bb_pending_write_insns[bb] = pending_write_insns;
7299       bb_pending_write_mems[bb] = pending_write_mems;
7300
7301       /* bb's structures are inherited by it's successors */
7302       first_edge = e = OUT_EDGES (b);
7303       if (e > 0)
7304         do
7305           {
7306             b_succ = TO_BLOCK (e);
7307             bb_succ = BLOCK_TO_BB (b_succ);
7308
7309             /* only bbs "below" bb, in the same region, are interesting */
7310             if (CONTAINING_RGN (b) != CONTAINING_RGN (b_succ)
7311                 || bb_succ <= bb)
7312               {
7313                 e = NEXT_OUT (e);
7314                 continue;
7315               }
7316
7317             for (reg = 0; reg < max_reg; reg++)
7318               {
7319
7320                 /* reg-last-uses lists are inherited by bb_succ */
7321                 for (u = reg_last_uses[reg]; u; u = XEXP (u, 1))
7322                   {
7323                     if (find_insn_list (XEXP (u, 0), (bb_reg_last_uses[bb_succ])[reg]))
7324                       continue;
7325
7326                     (bb_reg_last_uses[bb_succ])[reg]
7327                       = alloc_INSN_LIST (XEXP (u, 0),
7328                                          (bb_reg_last_uses[bb_succ])[reg]);
7329                   }
7330
7331                 /* reg-last-defs lists are inherited by bb_succ */
7332                 for (u = reg_last_sets[reg]; u; u = XEXP (u, 1))
7333                   {
7334                     if (find_insn_list (XEXP (u, 0), (bb_reg_last_sets[bb_succ])[reg]))
7335                       continue;
7336
7337                     (bb_reg_last_sets[bb_succ])[reg]
7338                       = alloc_INSN_LIST (XEXP (u, 0),
7339                                          (bb_reg_last_sets[bb_succ])[reg]);
7340                   }
7341               }
7342
7343             /* mem read/write lists are inherited by bb_succ */
7344             link_insn = pending_read_insns;
7345             link_mem = pending_read_mems;
7346             while (link_insn)
7347               {
7348                 if (!(find_insn_mem_list (XEXP (link_insn, 0), XEXP (link_mem, 0),
7349                                           bb_pending_read_insns[bb_succ],
7350                                           bb_pending_read_mems[bb_succ])))
7351                   add_insn_mem_dependence (&bb_pending_read_insns[bb_succ],
7352                                            &bb_pending_read_mems[bb_succ],
7353                                    XEXP (link_insn, 0), XEXP (link_mem, 0));
7354                 link_insn = XEXP (link_insn, 1);
7355                 link_mem = XEXP (link_mem, 1);
7356               }
7357
7358             link_insn = pending_write_insns;
7359             link_mem = pending_write_mems;
7360             while (link_insn)
7361               {
7362                 if (!(find_insn_mem_list (XEXP (link_insn, 0), XEXP (link_mem, 0),
7363                                           bb_pending_write_insns[bb_succ],
7364                                           bb_pending_write_mems[bb_succ])))
7365                   add_insn_mem_dependence (&bb_pending_write_insns[bb_succ],
7366                                            &bb_pending_write_mems[bb_succ],
7367                                    XEXP (link_insn, 0), XEXP (link_mem, 0));
7368
7369                 link_insn = XEXP (link_insn, 1);
7370                 link_mem = XEXP (link_mem, 1);
7371               }
7372
7373             /* last_function_call is inherited by bb_succ */
7374             for (u = last_function_call; u; u = XEXP (u, 1))
7375               {
7376                 if (find_insn_list (XEXP (u, 0), bb_last_function_call[bb_succ]))
7377                   continue;
7378
7379                 bb_last_function_call[bb_succ]
7380                   = alloc_INSN_LIST (XEXP (u, 0),
7381                                      bb_last_function_call[bb_succ]);
7382               }
7383
7384             /* last_pending_memory_flush is inherited by bb_succ */
7385             for (u = last_pending_memory_flush; u; u = XEXP (u, 1))
7386               {
7387                 if (find_insn_list (XEXP (u, 0), bb_last_pending_memory_flush[bb_succ]))
7388                   continue;
7389
7390                 bb_last_pending_memory_flush[bb_succ]
7391                   = alloc_INSN_LIST (XEXP (u, 0),
7392                                      bb_last_pending_memory_flush[bb_succ]);
7393               }
7394
7395             /* sched_before_next_call is inherited by bb_succ */
7396             x = LOG_LINKS (sched_before_next_call);
7397             for (; x; x = XEXP (x, 1))
7398               add_dependence (bb_sched_before_next_call[bb_succ],
7399                               XEXP (x, 0), REG_DEP_ANTI);
7400
7401             e = NEXT_OUT (e);
7402           }
7403         while (e != first_edge);
7404     }
7405
7406   /* Free up the INSN_LISTs 
7407
7408      Note this loop is executed max_reg * nr_regions times.  It's first 
7409      implementation accounted for over 90% of the calls to free_list.
7410      The list was empty for the vast majority of those calls.  On the PA,
7411      not calling free_list in those cases improves -O2 compile times by
7412      3-5% on average.  */
7413   for (b = 0; b < max_reg; ++b)
7414     {
7415       if (reg_last_sets[b])
7416         free_list (&reg_last_sets[b], &unused_insn_list);
7417       if (reg_last_uses[b])
7418         free_list (&reg_last_uses[b], &unused_insn_list);
7419     }
7420
7421   /* Assert that we won't need bb_reg_last_* for this block anymore.  */
7422   if (current_nr_blocks > 1)
7423     {
7424       bb_reg_last_uses[bb] = (rtx *) NULL_RTX;
7425       bb_reg_last_sets[bb] = (rtx *) NULL_RTX;
7426     }
7427 }
7428
7429 /* Print dependences for debugging, callable from debugger */
7430
7431 void
7432 debug_dependencies ()
7433 {
7434   int bb;
7435
7436   fprintf (dump, ";;   --------------- forward dependences: ------------ \n");
7437   for (bb = 0; bb < current_nr_blocks; bb++)
7438     {
7439       if (1)
7440         {
7441           rtx head, tail;
7442           rtx next_tail;
7443           rtx insn;
7444
7445           get_block_head_tail (bb, &head, &tail);
7446           next_tail = NEXT_INSN (tail);
7447           fprintf (dump, "\n;;   --- Region Dependences --- b %d bb %d \n",
7448                    BB_TO_BLOCK (bb), bb);
7449
7450           fprintf (dump, ";;   %7s%6s%6s%6s%6s%6s%11s%6s\n",
7451           "insn", "code", "bb", "dep", "prio", "cost", "blockage", "units");
7452           fprintf (dump, ";;   %7s%6s%6s%6s%6s%6s%11s%6s\n",
7453           "----", "----", "--", "---", "----", "----", "--------", "-----");
7454           for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
7455             {
7456               rtx link;
7457               int unit, range;
7458
7459               if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
7460                 {
7461                   int n;
7462                   fprintf (dump, ";;   %6d ", INSN_UID (insn));
7463                   if (GET_CODE (insn) == NOTE)
7464                     {
7465                       n = NOTE_LINE_NUMBER (insn);
7466                       if (n < 0)
7467                         fprintf (dump, "%s\n", GET_NOTE_INSN_NAME (n));
7468                       else
7469                         fprintf (dump, "line %d, file %s\n", n,
7470                                  NOTE_SOURCE_FILE (insn));
7471                     }
7472                   else
7473                     fprintf (dump, " {%s}\n", GET_RTX_NAME (GET_CODE (insn)));
7474                   continue;
7475                 }
7476
7477               unit = insn_unit (insn);
7478               range = (unit < 0
7479                  || function_units[unit].blockage_range_function == 0) ? 0 :
7480                 function_units[unit].blockage_range_function (insn);
7481               fprintf (dump,
7482                        ";;   %s%5d%6d%6d%6d%6d%6d  %3d -%3d   ",
7483                        (SCHED_GROUP_P (insn) ? "+" : " "),
7484                        INSN_UID (insn),
7485                        INSN_CODE (insn),
7486                        INSN_BB (insn),
7487                        INSN_DEP_COUNT (insn),
7488                        INSN_PRIORITY (insn),
7489                        insn_cost (insn, 0, 0),
7490                        (int) MIN_BLOCKAGE_COST (range),
7491                        (int) MAX_BLOCKAGE_COST (range));
7492               insn_print_units (insn);
7493               fprintf (dump, "\t: ");
7494               for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
7495                 fprintf (dump, "%d ", INSN_UID (XEXP (link, 0)));
7496               fprintf (dump, "\n");
7497             }
7498         }
7499     }
7500   fprintf (dump, "\n");
7501 }
7502
7503 /* Set_priorities: compute priority of each insn in the block */
7504
7505 static int
7506 set_priorities (bb)
7507      int bb;
7508 {
7509   rtx insn;
7510   int n_insn;
7511
7512   rtx tail;
7513   rtx prev_head;
7514   rtx head;
7515
7516   get_block_head_tail (bb, &head, &tail);
7517   prev_head = PREV_INSN (head);
7518
7519   if (head == tail
7520       && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
7521     return 0;
7522
7523   n_insn = 0;
7524   for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
7525     {
7526
7527       if (GET_CODE (insn) == NOTE)
7528         continue;
7529
7530       if (!(SCHED_GROUP_P (insn)))
7531         n_insn++;
7532       (void) priority (insn);
7533     }
7534
7535   return n_insn;
7536 }
7537
7538 /* Make each element of VECTOR point at an rtx-vector,
7539    taking the space for all those rtx-vectors from SPACE.
7540    SPACE is of type (rtx *), but it is really as long as NELTS rtx-vectors.
7541    BYTES_PER_ELT is the number of bytes in one rtx-vector.
7542    (this is the same as init_regset_vector () in flow.c) */
7543
7544 static void
7545 init_rtx_vector (vector, space, nelts, bytes_per_elt)
7546      rtx **vector;
7547      rtx *space;
7548      int nelts;
7549      int bytes_per_elt;
7550 {
7551   register int i;
7552   register rtx *p = space;
7553
7554   for (i = 0; i < nelts; i++)
7555     {
7556       vector[i] = p;
7557       p += bytes_per_elt / sizeof (*p);
7558     }
7559 }
7560
7561 /* Schedule a region.  A region is either an inner loop, a loop-free
7562    subroutine, or a single basic block.  Each bb in the region is
7563    scheduled after its flow predecessors.  */
7564
7565 static void
7566 schedule_region (rgn)
7567      int rgn;
7568 {
7569   int bb;
7570   int rgn_n_insns = 0;
7571   int sched_rgn_n_insns = 0;
7572
7573   /* set variables for the current region */
7574   current_nr_blocks = RGN_NR_BLOCKS (rgn);
7575   current_blocks = RGN_BLOCKS (rgn);
7576
7577   reg_pending_sets = ALLOCA_REG_SET ();
7578   reg_pending_sets_all = 0;
7579
7580   /* initializations for region data dependence analyisis */
7581   if (current_nr_blocks > 1)
7582     {
7583       rtx *space;
7584       int maxreg = max_reg_num ();
7585
7586       bb_reg_last_uses = (rtx **) alloca (current_nr_blocks * sizeof (rtx *));
7587       space = (rtx *) alloca (current_nr_blocks * maxreg * sizeof (rtx));
7588       bzero ((char *) space, current_nr_blocks * maxreg * sizeof (rtx));
7589       init_rtx_vector (bb_reg_last_uses, space, current_nr_blocks, maxreg * sizeof (rtx *));
7590
7591       bb_reg_last_sets = (rtx **) alloca (current_nr_blocks * sizeof (rtx *));
7592       space = (rtx *) alloca (current_nr_blocks * maxreg * sizeof (rtx));
7593       bzero ((char *) space, current_nr_blocks * maxreg * sizeof (rtx));
7594       init_rtx_vector (bb_reg_last_sets, space, current_nr_blocks, maxreg * sizeof (rtx *));
7595
7596       bb_pending_read_insns = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7597       bb_pending_read_mems = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7598       bb_pending_write_insns = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7599       bb_pending_write_mems = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7600       bb_pending_lists_length = (int *) alloca (current_nr_blocks * sizeof (int));
7601       bb_last_pending_memory_flush = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7602       bb_last_function_call = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7603       bb_sched_before_next_call = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7604
7605       init_rgn_data_dependences (current_nr_blocks);
7606     }
7607
7608   /* compute LOG_LINKS */
7609   for (bb = 0; bb < current_nr_blocks; bb++)
7610     compute_block_backward_dependences (bb);
7611
7612   /* compute INSN_DEPEND */
7613   for (bb = current_nr_blocks - 1; bb >= 0; bb--)
7614     compute_block_forward_dependences (bb);
7615
7616   /* Delete line notes, compute live-regs at block end, and set priorities.  */
7617   dead_notes = 0;
7618   for (bb = 0; bb < current_nr_blocks; bb++)
7619     {
7620       if (reload_completed == 0)
7621         find_pre_sched_live (bb);
7622
7623       if (write_symbols != NO_DEBUG)
7624         {
7625           save_line_notes (bb);
7626           rm_line_notes (bb);
7627         }
7628
7629       rgn_n_insns += set_priorities (bb);
7630     }
7631
7632   /* compute interblock info: probabilities, split-edges, dominators, etc.  */
7633   if (current_nr_blocks > 1)
7634     {
7635       int i;
7636
7637       prob = (float *) alloca ((current_nr_blocks) * sizeof (float));
7638
7639       bbset_size = current_nr_blocks / HOST_BITS_PER_WIDE_INT + 1;
7640       dom = (bbset *) alloca (current_nr_blocks * sizeof (bbset));
7641       for (i = 0; i < current_nr_blocks; i++)
7642         {
7643           dom[i] = (bbset) alloca (bbset_size * sizeof (HOST_WIDE_INT));
7644           bzero ((char *) dom[i], bbset_size * sizeof (HOST_WIDE_INT));
7645         }
7646
7647       /* edge to bit */
7648       rgn_nr_edges = 0;
7649       edge_to_bit = (int *) alloca (nr_edges * sizeof (int));
7650       for (i = 1; i < nr_edges; i++)
7651         if (CONTAINING_RGN (FROM_BLOCK (i)) == rgn)
7652           EDGE_TO_BIT (i) = rgn_nr_edges++;
7653       rgn_edges = (int *) alloca (rgn_nr_edges * sizeof (int));
7654
7655       rgn_nr_edges = 0;
7656       for (i = 1; i < nr_edges; i++)
7657         if (CONTAINING_RGN (FROM_BLOCK (i)) == (rgn))
7658           rgn_edges[rgn_nr_edges++] = i;
7659
7660       /* split edges */
7661       edgeset_size = rgn_nr_edges / HOST_BITS_PER_WIDE_INT + 1;
7662       pot_split = (edgeset *) alloca (current_nr_blocks * sizeof (edgeset));
7663       ancestor_edges = (edgeset *) alloca (current_nr_blocks * sizeof (edgeset));
7664       for (i = 0; i < current_nr_blocks; i++)
7665         {
7666           pot_split[i] =
7667             (edgeset) alloca (edgeset_size * sizeof (HOST_WIDE_INT));
7668           bzero ((char *) pot_split[i],
7669                  edgeset_size * sizeof (HOST_WIDE_INT));
7670           ancestor_edges[i] =
7671             (edgeset) alloca (edgeset_size * sizeof (HOST_WIDE_INT));
7672           bzero ((char *) ancestor_edges[i],
7673                  edgeset_size * sizeof (HOST_WIDE_INT));
7674         }
7675
7676       /* compute probabilities, dominators, split_edges */
7677       for (bb = 0; bb < current_nr_blocks; bb++)
7678         compute_dom_prob_ps (bb);
7679     }
7680
7681   /* now we can schedule all blocks */
7682   for (bb = 0; bb < current_nr_blocks; bb++)
7683     {
7684       sched_rgn_n_insns += schedule_block (bb, rgn_n_insns);
7685
7686 #ifdef USE_C_ALLOCA
7687       alloca (0);
7688 #endif
7689     }
7690
7691   /* sanity check: verify that all region insns were scheduled */
7692   if (sched_rgn_n_insns != rgn_n_insns)
7693     abort ();
7694
7695   /* update register life and usage information */
7696   if (reload_completed == 0)
7697     {
7698       for (bb = current_nr_blocks - 1; bb >= 0; bb--)
7699         find_post_sched_live (bb);
7700
7701       if (current_nr_blocks <= 1)
7702         /* Sanity check.  There should be no REG_DEAD notes leftover at the end.
7703            In practice, this can occur as the result of bugs in flow, combine.c,
7704            and/or sched.c.  The values of the REG_DEAD notes remaining are
7705            meaningless, because dead_notes is just used as a free list.  */
7706         if (dead_notes != 0)
7707           abort ();
7708     }
7709
7710   /* restore line notes.  */
7711   if (write_symbols != NO_DEBUG)
7712     {
7713       for (bb = 0; bb < current_nr_blocks; bb++)
7714         restore_line_notes (bb);
7715     }
7716
7717   /* Done with this region */
7718   free_pending_lists ();
7719
7720   FREE_REG_SET (reg_pending_sets);
7721 }
7722
7723 /* Subroutine of split_hard_reg_notes.  Searches X for any reference to
7724    REGNO, returning the rtx of the reference found if any.  Otherwise,
7725    returns 0.  */
7726
7727 static rtx
7728 regno_use_in (regno, x)
7729      int regno;
7730      rtx x;
7731 {
7732   register char *fmt;
7733   int i, j;
7734   rtx tem;
7735
7736   if (GET_CODE (x) == REG && REGNO (x) == regno)
7737     return x;
7738
7739   fmt = GET_RTX_FORMAT (GET_CODE (x));
7740   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
7741     {
7742       if (fmt[i] == 'e')
7743         {
7744           if ((tem = regno_use_in (regno, XEXP (x, i))))
7745             return tem;
7746         }
7747       else if (fmt[i] == 'E')
7748         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7749           if ((tem = regno_use_in (regno, XVECEXP (x, i, j))))
7750             return tem;
7751     }
7752
7753   return 0;
7754 }
7755
7756 /* Subroutine of update_flow_info.  Determines whether any new REG_NOTEs are
7757    needed for the hard register mentioned in the note.  This can happen
7758    if the reference to the hard register in the original insn was split into
7759    several smaller hard register references in the split insns.  */
7760
7761 static void
7762 split_hard_reg_notes (note, first, last)
7763      rtx note, first, last;
7764 {
7765   rtx reg, temp, link;
7766   int n_regs, i, new_reg;
7767   rtx insn;
7768
7769   /* Assume that this is a REG_DEAD note.  */
7770   if (REG_NOTE_KIND (note) != REG_DEAD)
7771     abort ();
7772
7773   reg = XEXP (note, 0);
7774
7775   n_regs = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
7776
7777   for (i = 0; i < n_regs; i++)
7778     {
7779       new_reg = REGNO (reg) + i;
7780
7781       /* Check for references to new_reg in the split insns.  */
7782       for (insn = last;; insn = PREV_INSN (insn))
7783         {
7784           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
7785               && (temp = regno_use_in (new_reg, PATTERN (insn))))
7786             {
7787               /* Create a new reg dead note ere.  */
7788               link = alloc_EXPR_LIST (REG_DEAD, temp, REG_NOTES (insn));
7789               REG_NOTES (insn) = link;
7790
7791               /* If killed multiple registers here, then add in the excess.  */
7792               i += HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)) - 1;
7793
7794               break;
7795             }
7796           /* It isn't mentioned anywhere, so no new reg note is needed for
7797              this register.  */
7798           if (insn == first)
7799             break;
7800         }
7801     }
7802 }
7803
7804 /* Subroutine of update_flow_info.  Determines whether a SET or CLOBBER in an
7805    insn created by splitting needs a REG_DEAD or REG_UNUSED note added.  */
7806
7807 static void
7808 new_insn_dead_notes (pat, insn, last, orig_insn)
7809      rtx pat, insn, last, orig_insn;
7810 {
7811   rtx dest, tem, set;
7812
7813   /* PAT is either a CLOBBER or a SET here.  */
7814   dest = XEXP (pat, 0);
7815
7816   while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
7817          || GET_CODE (dest) == STRICT_LOW_PART
7818          || GET_CODE (dest) == SIGN_EXTRACT)
7819     dest = XEXP (dest, 0);
7820
7821   if (GET_CODE (dest) == REG)
7822     {
7823       /* If the original insn already used this register, we may not add new
7824          notes for it.  One example for a split that needs this test is
7825          when a multi-word memory access with register-indirect addressing
7826          is split into multiple memory accesses with auto-increment and
7827          one adjusting add instruction for the address register.  */
7828       if (reg_referenced_p (dest, PATTERN (orig_insn)))
7829         return;
7830       for (tem = last; tem != insn; tem = PREV_INSN (tem))
7831         {
7832           if (GET_RTX_CLASS (GET_CODE (tem)) == 'i'
7833               && reg_overlap_mentioned_p (dest, PATTERN (tem))
7834               && (set = single_set (tem)))
7835             {
7836               rtx tem_dest = SET_DEST (set);
7837
7838               while (GET_CODE (tem_dest) == ZERO_EXTRACT
7839                      || GET_CODE (tem_dest) == SUBREG
7840                      || GET_CODE (tem_dest) == STRICT_LOW_PART
7841                      || GET_CODE (tem_dest) == SIGN_EXTRACT)
7842                 tem_dest = XEXP (tem_dest, 0);
7843
7844               if (!rtx_equal_p (tem_dest, dest))
7845                 {
7846                   /* Use the same scheme as combine.c, don't put both REG_DEAD
7847                      and REG_UNUSED notes on the same insn.  */
7848                   if (!find_regno_note (tem, REG_UNUSED, REGNO (dest))
7849                       && !find_regno_note (tem, REG_DEAD, REGNO (dest)))
7850                     {
7851                       rtx note = alloc_EXPR_LIST (REG_DEAD, dest,
7852                                                   REG_NOTES (tem));
7853                       REG_NOTES (tem) = note;
7854                     }
7855                   /* The reg only dies in one insn, the last one that uses
7856                      it.  */
7857                   break;
7858                 }
7859               else if (reg_overlap_mentioned_p (dest, SET_SRC (set)))
7860                 /* We found an instruction that both uses the register,
7861                    and sets it, so no new REG_NOTE is needed for this set.  */
7862                 break;
7863             }
7864         }
7865       /* If this is a set, it must die somewhere, unless it is the dest of
7866          the original insn, and hence is live after the original insn.  Abort
7867          if it isn't supposed to be live after the original insn.
7868
7869          If this is a clobber, then just add a REG_UNUSED note.  */
7870       if (tem == insn)
7871         {
7872           int live_after_orig_insn = 0;
7873           rtx pattern = PATTERN (orig_insn);
7874           int i;
7875
7876           if (GET_CODE (pat) == CLOBBER)
7877             {
7878               rtx note = alloc_EXPR_LIST (REG_UNUSED, dest, REG_NOTES (insn));
7879               REG_NOTES (insn) = note;
7880               return;
7881             }
7882
7883           /* The original insn could have multiple sets, so search the
7884              insn for all sets.  */
7885           if (GET_CODE (pattern) == SET)
7886             {
7887               if (reg_overlap_mentioned_p (dest, SET_DEST (pattern)))
7888                 live_after_orig_insn = 1;
7889             }
7890           else if (GET_CODE (pattern) == PARALLEL)
7891             {
7892               for (i = 0; i < XVECLEN (pattern, 0); i++)
7893                 if (GET_CODE (XVECEXP (pattern, 0, i)) == SET
7894                     && reg_overlap_mentioned_p (dest,
7895                                                 SET_DEST (XVECEXP (pattern,
7896                                                                    0, i))))
7897                   live_after_orig_insn = 1;
7898             }
7899
7900           if (!live_after_orig_insn)
7901             abort ();
7902         }
7903     }
7904 }
7905
7906 /* Subroutine of update_flow_info.  Update the value of reg_n_sets for all
7907    registers modified by X.  INC is -1 if the containing insn is being deleted,
7908    and is 1 if the containing insn is a newly generated insn.  */
7909
7910 static void
7911 update_n_sets (x, inc)
7912      rtx x;
7913      int inc;
7914 {
7915   rtx dest = SET_DEST (x);
7916
7917   while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
7918       || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
7919     dest = SUBREG_REG (dest);
7920
7921   if (GET_CODE (dest) == REG)
7922     {
7923       int regno = REGNO (dest);
7924
7925       if (regno < FIRST_PSEUDO_REGISTER)
7926         {
7927           register int i;
7928           int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (dest));
7929
7930           for (i = regno; i < endregno; i++)
7931             REG_N_SETS (i) += inc;
7932         }
7933       else
7934         REG_N_SETS (regno) += inc;
7935     }
7936 }
7937
7938 /* Updates all flow-analysis related quantities (including REG_NOTES) for
7939    the insns from FIRST to LAST inclusive that were created by splitting
7940    ORIG_INSN.  NOTES are the original REG_NOTES.  */
7941
7942 static void
7943 update_flow_info (notes, first, last, orig_insn)
7944      rtx notes;
7945      rtx first, last;
7946      rtx orig_insn;
7947 {
7948   rtx insn, note;
7949   rtx next;
7950   rtx orig_dest, temp;
7951   rtx set;
7952
7953   /* Get and save the destination set by the original insn.  */
7954
7955   orig_dest = single_set (orig_insn);
7956   if (orig_dest)
7957     orig_dest = SET_DEST (orig_dest);
7958
7959   /* Move REG_NOTES from the original insn to where they now belong.  */
7960
7961   for (note = notes; note; note = next)
7962     {
7963       next = XEXP (note, 1);
7964       switch (REG_NOTE_KIND (note))
7965         {
7966         case REG_DEAD:
7967         case REG_UNUSED:
7968           /* Move these notes from the original insn to the last new insn where
7969              the register is now set.  */
7970
7971           for (insn = last;; insn = PREV_INSN (insn))
7972             {
7973               if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
7974                   && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
7975                 {
7976                   /* If this note refers to a multiple word hard register, it
7977                      may have been split into several smaller hard register
7978                      references, so handle it specially.  */
7979                   temp = XEXP (note, 0);
7980                   if (REG_NOTE_KIND (note) == REG_DEAD
7981                       && GET_CODE (temp) == REG
7982                       && REGNO (temp) < FIRST_PSEUDO_REGISTER
7983                       && HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)) > 1)
7984                     split_hard_reg_notes (note, first, last);
7985                   else
7986                     {
7987                       XEXP (note, 1) = REG_NOTES (insn);
7988                       REG_NOTES (insn) = note;
7989                     }
7990
7991                   /* Sometimes need to convert REG_UNUSED notes to REG_DEAD
7992                      notes.  */
7993                   /* ??? This won't handle multiple word registers correctly,
7994                      but should be good enough for now.  */
7995                   if (REG_NOTE_KIND (note) == REG_UNUSED
7996                       && GET_CODE (XEXP (note, 0)) != SCRATCH
7997                       && !dead_or_set_p (insn, XEXP (note, 0)))
7998                     PUT_REG_NOTE_KIND (note, REG_DEAD);
7999
8000                   /* The reg only dies in one insn, the last one that uses
8001                      it.  */
8002                   break;
8003                 }
8004               /* It must die somewhere, fail it we couldn't find where it died.
8005
8006                  If this is a REG_UNUSED note, then it must be a temporary
8007                  register that was not needed by this instantiation of the
8008                  pattern, so we can safely ignore it.  */
8009               if (insn == first)
8010                 {
8011                   /* After reload, REG_DEAD notes come sometimes an
8012                      instruction after the register actually dies.  */
8013                   if (reload_completed && REG_NOTE_KIND (note) == REG_DEAD)
8014                     {
8015                       XEXP (note, 1) = REG_NOTES (insn);
8016                       REG_NOTES (insn) = note;
8017                       break;
8018                     }
8019                         
8020                   if (REG_NOTE_KIND (note) != REG_UNUSED)
8021                     abort ();
8022
8023                   break;
8024                 }
8025             }
8026           break;
8027
8028         case REG_WAS_0:
8029           /* If the insn that set the register to 0 was deleted, this
8030              note cannot be relied on any longer.  The destination might
8031              even have been moved to memory.
8032              This was observed for SH4 with execute/920501-6.c compilation,
8033              -O2 -fomit-frame-pointer -finline-functions .  */
8034           if (GET_CODE (XEXP (note, 0)) == NOTE
8035               || INSN_DELETED_P (XEXP (note, 0)))
8036             break;
8037           /* This note applies to the dest of the original insn.  Find the
8038              first new insn that now has the same dest, and move the note
8039              there.  */
8040
8041           if (!orig_dest)
8042             abort ();
8043
8044           for (insn = first;; insn = NEXT_INSN (insn))
8045             {
8046               if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
8047                   && (temp = single_set (insn))
8048                   && rtx_equal_p (SET_DEST (temp), orig_dest))
8049                 {
8050                   XEXP (note, 1) = REG_NOTES (insn);
8051                   REG_NOTES (insn) = note;
8052                   /* The reg is only zero before one insn, the first that
8053                      uses it.  */
8054                   break;
8055                 }
8056               /* If this note refers to a multiple word hard
8057                  register, it may have been split into several smaller
8058                  hard register references.  We could split the notes,
8059                  but simply dropping them is good enough.  */
8060               if (GET_CODE (orig_dest) == REG
8061                   && REGNO (orig_dest) < FIRST_PSEUDO_REGISTER
8062                   && HARD_REGNO_NREGS (REGNO (orig_dest),
8063                                        GET_MODE (orig_dest)) > 1)
8064                     break;
8065               /* It must be set somewhere, fail if we couldn't find where it
8066                  was set.  */
8067               if (insn == last)
8068                 abort ();
8069             }
8070           break;
8071
8072         case REG_EQUAL:
8073         case REG_EQUIV:
8074           /* A REG_EQUIV or REG_EQUAL note on an insn with more than one
8075              set is meaningless.  Just drop the note.  */
8076           if (!orig_dest)
8077             break;
8078
8079         case REG_NO_CONFLICT:
8080           /* These notes apply to the dest of the original insn.  Find the last
8081              new insn that now has the same dest, and move the note there.  */
8082
8083           if (!orig_dest)
8084             abort ();
8085
8086           for (insn = last;; insn = PREV_INSN (insn))
8087             {
8088               if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
8089                   && (temp = single_set (insn))
8090                   && rtx_equal_p (SET_DEST (temp), orig_dest))
8091                 {
8092                   XEXP (note, 1) = REG_NOTES (insn);
8093                   REG_NOTES (insn) = note;
8094                   /* Only put this note on one of the new insns.  */
8095                   break;
8096                 }
8097
8098               /* The original dest must still be set someplace.  Abort if we
8099                  couldn't find it.  */
8100               if (insn == first)
8101                 {
8102                   /* However, if this note refers to a multiple word hard
8103                      register, it may have been split into several smaller
8104                      hard register references.  We could split the notes,
8105                      but simply dropping them is good enough.  */
8106                   if (GET_CODE (orig_dest) == REG
8107                       && REGNO (orig_dest) < FIRST_PSEUDO_REGISTER
8108                       && HARD_REGNO_NREGS (REGNO (orig_dest),
8109                                            GET_MODE (orig_dest)) > 1)
8110                     break;
8111                   /* Likewise for multi-word memory references.  */
8112                   if (GET_CODE (orig_dest) == MEM
8113                       && SIZE_FOR_MODE (orig_dest) > UNITS_PER_WORD)
8114                     break;
8115                   abort ();
8116                 }
8117             }
8118           break;
8119
8120         case REG_LIBCALL:
8121           /* Move a REG_LIBCALL note to the first insn created, and update
8122              the corresponding REG_RETVAL note.  */
8123           XEXP (note, 1) = REG_NOTES (first);
8124           REG_NOTES (first) = note;
8125
8126           insn = XEXP (note, 0);
8127           note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
8128           if (note)
8129             XEXP (note, 0) = first;
8130           break;
8131
8132         case REG_EXEC_COUNT:
8133           /* Move a REG_EXEC_COUNT note to the first insn created.  */
8134           XEXP (note, 1) = REG_NOTES (first);
8135           REG_NOTES (first) = note;
8136           break;
8137
8138         case REG_RETVAL:
8139           /* Move a REG_RETVAL note to the last insn created, and update
8140              the corresponding REG_LIBCALL note.  */
8141           XEXP (note, 1) = REG_NOTES (last);
8142           REG_NOTES (last) = note;
8143
8144           insn = XEXP (note, 0);
8145           note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
8146           if (note)
8147             XEXP (note, 0) = last;
8148           break;
8149
8150         case REG_NONNEG:
8151         case REG_BR_PROB:
8152           /* This should be moved to whichever instruction is a JUMP_INSN.  */
8153
8154           for (insn = last;; insn = PREV_INSN (insn))
8155             {
8156               if (GET_CODE (insn) == JUMP_INSN)
8157                 {
8158                   XEXP (note, 1) = REG_NOTES (insn);
8159                   REG_NOTES (insn) = note;
8160                   /* Only put this note on one of the new insns.  */
8161                   break;
8162                 }
8163               /* Fail if we couldn't find a JUMP_INSN.  */
8164               if (insn == first)
8165                 abort ();
8166             }
8167           break;
8168
8169         case REG_INC:
8170           /* reload sometimes leaves obsolete REG_INC notes around.  */
8171           if (reload_completed)
8172             break;
8173           /* This should be moved to whichever instruction now has the
8174              increment operation.  */
8175           abort ();
8176
8177         case REG_LABEL:
8178           /* Should be moved to the new insn(s) which use the label.  */
8179           for (insn = first; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
8180             if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
8181                 && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
8182               {
8183                 REG_NOTES (insn) = alloc_EXPR_LIST (REG_LABEL,
8184                                                     XEXP (note, 0),
8185                                                     REG_NOTES (insn));
8186               }
8187           break;
8188
8189         case REG_CC_SETTER:
8190         case REG_CC_USER:
8191           /* These two notes will never appear until after reorg, so we don't
8192              have to handle them here.  */
8193         default:
8194           abort ();
8195         }
8196     }
8197
8198   /* Each new insn created, except the last, has a new set.  If the destination
8199      is a register, then this reg is now live across several insns, whereas
8200      previously the dest reg was born and died within the same insn.  To
8201      reflect this, we now need a REG_DEAD note on the insn where this
8202      dest reg dies.
8203
8204      Similarly, the new insns may have clobbers that need REG_UNUSED notes.  */
8205
8206   for (insn = first; insn != last; insn = NEXT_INSN (insn))
8207     {
8208       rtx pat;
8209       int i;
8210
8211       pat = PATTERN (insn);
8212       if (GET_CODE (pat) == SET || GET_CODE (pat) == CLOBBER)
8213         new_insn_dead_notes (pat, insn, last, orig_insn);
8214       else if (GET_CODE (pat) == PARALLEL)
8215         {
8216           for (i = 0; i < XVECLEN (pat, 0); i++)
8217             if (GET_CODE (XVECEXP (pat, 0, i)) == SET
8218                 || GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER)
8219               new_insn_dead_notes (XVECEXP (pat, 0, i), insn, last, orig_insn);
8220         }
8221     }
8222
8223   /* If any insn, except the last, uses the register set by the last insn,
8224      then we need a new REG_DEAD note on that insn.  In this case, there
8225      would not have been a REG_DEAD note for this register in the original
8226      insn because it was used and set within one insn.  */
8227
8228   set = single_set (last);
8229   if (set)
8230     {
8231       rtx dest = SET_DEST (set);
8232
8233       while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
8234              || GET_CODE (dest) == STRICT_LOW_PART
8235              || GET_CODE (dest) == SIGN_EXTRACT)
8236         dest = XEXP (dest, 0);
8237
8238       if (GET_CODE (dest) == REG
8239           /* Global registers are always live, so the code below does not
8240              apply to them.  */
8241           && (REGNO (dest) >= FIRST_PSEUDO_REGISTER
8242               || ! global_regs[REGNO (dest)]))
8243         {
8244           rtx stop_insn = PREV_INSN (first);
8245
8246           /* If the last insn uses the register that it is setting, then
8247              we don't want to put a REG_DEAD note there.  Search backwards
8248              to find the first insn that sets but does not use DEST.  */
8249
8250           insn = last;
8251           if (reg_overlap_mentioned_p (dest, SET_SRC (set)))
8252             {
8253               for (insn = PREV_INSN (insn); insn != first;
8254                    insn = PREV_INSN (insn))
8255                 {
8256                   if ((set = single_set (insn))
8257                       && reg_mentioned_p (dest, SET_DEST (set))
8258                       && ! reg_overlap_mentioned_p (dest, SET_SRC (set)))
8259                     break;
8260                 }
8261             }
8262
8263           /* Now find the first insn that uses but does not set DEST.  */
8264
8265           for (insn = PREV_INSN (insn); insn != stop_insn;
8266                insn = PREV_INSN (insn))
8267             {
8268               if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
8269                   && reg_mentioned_p (dest, PATTERN (insn))
8270                   && (set = single_set (insn)))
8271                 {
8272                   rtx insn_dest = SET_DEST (set);
8273
8274                   while (GET_CODE (insn_dest) == ZERO_EXTRACT
8275                          || GET_CODE (insn_dest) == SUBREG
8276                          || GET_CODE (insn_dest) == STRICT_LOW_PART
8277                          || GET_CODE (insn_dest) == SIGN_EXTRACT)
8278                     insn_dest = XEXP (insn_dest, 0);
8279
8280                   if (insn_dest != dest)
8281                     {
8282                       note = alloc_EXPR_LIST (REG_DEAD, dest, REG_NOTES (insn));
8283                       REG_NOTES (insn) = note;
8284                       /* The reg only dies in one insn, the last one
8285                          that uses it.  */
8286                       break;
8287                     }
8288                 }
8289             }
8290         }
8291     }
8292
8293   /* If the original dest is modifying a multiple register target, and the
8294      original instruction was split such that the original dest is now set
8295      by two or more SUBREG sets, then the split insns no longer kill the
8296      destination of the original insn.
8297
8298      In this case, if there exists an instruction in the same basic block,
8299      before the split insn, which uses the original dest, and this use is
8300      killed by the original insn, then we must remove the REG_DEAD note on
8301      this insn, because it is now superfluous.
8302
8303      This does not apply when a hard register gets split, because the code
8304      knows how to handle overlapping hard registers properly.  */
8305   if (orig_dest && GET_CODE (orig_dest) == REG)
8306     {
8307       int found_orig_dest = 0;
8308       int found_split_dest = 0;
8309
8310       for (insn = first;; insn = NEXT_INSN (insn))
8311         {
8312           rtx pat;
8313           int i;
8314
8315           /* I'm not sure if this can happen, but let's be safe.  */
8316           if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
8317             continue;
8318
8319           pat = PATTERN (insn);
8320           i = GET_CODE (pat) == PARALLEL ? XVECLEN (pat, 0) : 0;
8321           set = pat;
8322
8323           for (;;)
8324             {
8325               if (GET_CODE (set) == SET)
8326                 {
8327                   if (GET_CODE (SET_DEST (set)) == REG
8328                       && REGNO (SET_DEST (set)) == REGNO (orig_dest))
8329                     {
8330                       found_orig_dest = 1;
8331                       break;
8332                     }
8333                   else if (GET_CODE (SET_DEST (set)) == SUBREG
8334                            && SUBREG_REG (SET_DEST (set)) == orig_dest)
8335                     {
8336                       found_split_dest = 1;
8337                       break;
8338                     }
8339                 }
8340               if (--i < 0)
8341                 break;
8342               set = XVECEXP (pat, 0, i);
8343             }
8344
8345           if (insn == last)
8346             break;
8347         }
8348
8349       if (found_split_dest)
8350         {
8351           /* Search backwards from FIRST, looking for the first insn that uses
8352              the original dest.  Stop if we pass a CODE_LABEL or a JUMP_INSN.
8353              If we find an insn, and it has a REG_DEAD note, then delete the
8354              note.  */
8355
8356           for (insn = first; insn; insn = PREV_INSN (insn))
8357             {
8358               if (GET_CODE (insn) == CODE_LABEL
8359                   || GET_CODE (insn) == JUMP_INSN)
8360                 break;
8361               else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
8362                        && reg_mentioned_p (orig_dest, insn))
8363                 {
8364                   note = find_regno_note (insn, REG_DEAD, REGNO (orig_dest));
8365                   if (note)
8366                     remove_note (insn, note);
8367                 }
8368             }
8369         }
8370       else if (!found_orig_dest)
8371         {
8372           /* This should never happen.  */
8373           abort ();
8374         }
8375     }
8376
8377   /* Update reg_n_sets.  This is necessary to prevent local alloc from
8378      converting REG_EQUAL notes to REG_EQUIV when splitting has modified
8379      a reg from set once to set multiple times.  */
8380
8381   {
8382     rtx x = PATTERN (orig_insn);
8383     RTX_CODE code = GET_CODE (x);
8384
8385     if (code == SET || code == CLOBBER)
8386       update_n_sets (x, -1);
8387     else if (code == PARALLEL)
8388       {
8389         int i;
8390         for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
8391           {
8392             code = GET_CODE (XVECEXP (x, 0, i));
8393             if (code == SET || code == CLOBBER)
8394               update_n_sets (XVECEXP (x, 0, i), -1);
8395           }
8396       }
8397
8398     for (insn = first;; insn = NEXT_INSN (insn))
8399       {
8400         x = PATTERN (insn);
8401         code = GET_CODE (x);
8402
8403         if (code == SET || code == CLOBBER)
8404           update_n_sets (x, 1);
8405         else if (code == PARALLEL)
8406           {
8407             int i;
8408             for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
8409               {
8410                 code = GET_CODE (XVECEXP (x, 0, i));
8411                 if (code == SET || code == CLOBBER)
8412                   update_n_sets (XVECEXP (x, 0, i), 1);
8413               }
8414           }
8415
8416         if (insn == last)
8417           break;
8418       }
8419   }
8420 }
8421
8422 /* Do the splitting of insns in the block b.  */
8423
8424 static void
8425 split_block_insns (b)
8426      int b;
8427 {
8428   rtx insn, next;
8429
8430   for (insn = basic_block_head[b];; insn = next)
8431     {
8432       rtx set, last, first, notes;
8433
8434       /* Can't use `next_real_insn' because that
8435          might go across CODE_LABELS and short-out basic blocks.  */
8436       next = NEXT_INSN (insn);
8437       if (GET_CODE (insn) != INSN)
8438         {
8439           if (insn == basic_block_end[b])
8440             break;
8441
8442           continue;
8443         }
8444
8445       /* Don't split no-op move insns.  These should silently disappear
8446          later in final.  Splitting such insns would break the code
8447          that handles REG_NO_CONFLICT blocks.  */
8448       set = single_set (insn);
8449       if (set && rtx_equal_p (SET_SRC (set), SET_DEST (set)))
8450         {
8451           if (insn == basic_block_end[b])
8452             break;
8453
8454           /* Nops get in the way while scheduling, so delete them now if
8455              register allocation has already been done.  It is too risky
8456              to try to do this before register allocation, and there are
8457              unlikely to be very many nops then anyways.  */
8458           if (reload_completed)
8459             {
8460               PUT_CODE (insn, NOTE);
8461               NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8462               NOTE_SOURCE_FILE (insn) = 0;
8463             }
8464
8465           continue;
8466         }
8467
8468       /* Split insns here to get max fine-grain parallelism.  */
8469       first = PREV_INSN (insn);
8470       notes = REG_NOTES (insn);
8471       last = try_split (PATTERN (insn), insn, 1);
8472       if (last != insn)
8473         {
8474           /* try_split returns the NOTE that INSN became.  */
8475           first = NEXT_INSN (first);
8476           update_flow_info (notes, first, last, insn);
8477
8478           PUT_CODE (insn, NOTE);
8479           NOTE_SOURCE_FILE (insn) = 0;
8480           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8481           if (insn == basic_block_head[b])
8482             basic_block_head[b] = first;
8483           if (insn == basic_block_end[b])
8484             {
8485               basic_block_end[b] = last;
8486               break;
8487             }
8488         }
8489
8490       if (insn == basic_block_end[b])
8491         break;
8492     }
8493 }
8494
8495 /* The one entry point in this file.  DUMP_FILE is the dump file for
8496    this pass.  */
8497
8498 void
8499 schedule_insns (dump_file)
8500      FILE *dump_file;
8501 {
8502
8503   int max_uid;
8504   int b;
8505   rtx insn;
8506   int rgn;
8507
8508   int luid;
8509
8510   /* disable speculative loads in their presence if cc0 defined */
8511 #ifdef HAVE_cc0
8512   flag_schedule_speculative_load = 0;
8513 #endif
8514
8515   /* Taking care of this degenerate case makes the rest of
8516      this code simpler.  */
8517   if (n_basic_blocks == 0)
8518     return;
8519
8520   /* set dump and sched_verbose for the desired debugging output.  If no
8521      dump-file was specified, but -fsched-verbose-N (any N), print to stderr.
8522      For -fsched-verbose-N, N>=10, print everything to stderr.  */
8523   sched_verbose = sched_verbose_param;
8524   if (sched_verbose_param == 0 && dump_file)
8525     sched_verbose = 1;
8526   dump = ((sched_verbose_param >= 10 || !dump_file) ? stderr : dump_file);
8527
8528   nr_inter = 0;
8529   nr_spec = 0;
8530
8531   /* Initialize the unused_*_lists.  We can't use the ones left over from
8532      the previous function, because gcc has freed that memory.  We can use
8533      the ones left over from the first sched pass in the second pass however,
8534      so only clear them on the first sched pass.  The first pass is before
8535      reload if flag_schedule_insns is set, otherwise it is afterwards.  */
8536
8537   if (reload_completed == 0 || !flag_schedule_insns)
8538     {
8539       unused_insn_list = 0;
8540       unused_expr_list = 0;
8541     }
8542
8543   /* initialize issue_rate */
8544   issue_rate = ISSUE_RATE;
8545
8546   /* do the splitting first for all blocks */
8547   for (b = 0; b < n_basic_blocks; b++)
8548     split_block_insns (b);
8549
8550   max_uid = (get_max_uid () + 1);
8551
8552   cant_move = (char *) xmalloc (max_uid * sizeof (char));
8553   bzero ((char *) cant_move, max_uid * sizeof (char));
8554
8555   fed_by_spec_load = (char *) xmalloc (max_uid * sizeof (char));
8556   bzero ((char *) fed_by_spec_load, max_uid * sizeof (char));
8557
8558   is_load_insn = (char *) xmalloc (max_uid * sizeof (char));
8559   bzero ((char *) is_load_insn, max_uid * sizeof (char));
8560
8561   insn_orig_block = (int *) xmalloc (max_uid * sizeof (int));
8562   insn_luid = (int *) xmalloc (max_uid * sizeof (int));
8563
8564   luid = 0;
8565   for (b = 0; b < n_basic_blocks; b++)
8566     for (insn = basic_block_head[b];; insn = NEXT_INSN (insn))
8567       {
8568         INSN_BLOCK (insn) = b;
8569         INSN_LUID (insn) = luid++;
8570
8571         if (insn == basic_block_end[b])
8572           break;
8573       }
8574
8575   /* after reload, remove inter-blocks dependences computed before reload.  */
8576   if (reload_completed)
8577     {
8578       int b;
8579       rtx insn;
8580
8581       for (b = 0; b < n_basic_blocks; b++)
8582         for (insn = basic_block_head[b];; insn = NEXT_INSN (insn))
8583           {
8584             rtx link, prev;
8585
8586             if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
8587               {
8588                 prev = NULL_RTX;
8589                 link = LOG_LINKS (insn);
8590                 while (link)
8591                   {
8592                     rtx x = XEXP (link, 0);
8593
8594                     if (INSN_BLOCK (x) != b)
8595                       {
8596                         remove_dependence (insn, x);
8597                         link = prev ? XEXP (prev, 1) : LOG_LINKS (insn);
8598                       }
8599                     else
8600                       prev = link, link = XEXP (prev, 1);
8601                   }
8602               }
8603
8604             if (insn == basic_block_end[b])
8605               break;
8606           }
8607     }
8608
8609   nr_regions = 0;
8610   rgn_table = (region *) alloca ((n_basic_blocks) * sizeof (region));
8611   rgn_bb_table = (int *) alloca ((n_basic_blocks) * sizeof (int));
8612   block_to_bb = (int *) alloca ((n_basic_blocks) * sizeof (int));
8613   containing_rgn = (int *) alloca ((n_basic_blocks) * sizeof (int));
8614
8615   /* compute regions for scheduling */
8616   if (reload_completed
8617       || n_basic_blocks == 1
8618       || !flag_schedule_interblock)
8619     {
8620       find_single_block_region ();
8621     }
8622   else
8623     {
8624       /* verify that a 'good' control flow graph can be built */
8625       if (is_cfg_nonregular ())
8626         {
8627           find_single_block_region ();
8628         }
8629       else
8630         {
8631           int_list_ptr *s_preds, *s_succs;
8632           int *num_preds, *num_succs;
8633           sbitmap *dom, *pdom;
8634
8635           s_preds = (int_list_ptr *) alloca (n_basic_blocks
8636                                              * sizeof (int_list_ptr));
8637           s_succs = (int_list_ptr *) alloca (n_basic_blocks
8638                                              * sizeof (int_list_ptr));
8639           num_preds = (int *) alloca (n_basic_blocks * sizeof (int));
8640           num_succs = (int *) alloca (n_basic_blocks * sizeof (int));
8641           dom = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
8642           pdom = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
8643
8644           /* The scheduler runs after flow; therefore, we can't blindly call
8645              back into find_basic_blocks since doing so could invalidate the
8646              info in basic_block_live_at_start.
8647
8648              Consider a block consisting entirely of dead stores; after life
8649              analysis it would be a block of NOTE_INSN_DELETED notes.  If
8650              we call find_basic_blocks again, then the block would be removed
8651              entirely and invalidate our the register live information.
8652
8653              We could (should?) recompute register live information.  Doing
8654              so may even be beneficial.  */
8655
8656           compute_preds_succs (s_preds, s_succs, num_preds, num_succs);
8657
8658           /* Compute the dominators and post dominators.  We don't currently use
8659              post dominators, but we should for speculative motion analysis.  */
8660           compute_dominators (dom, pdom, s_preds, s_succs);
8661
8662           /* build_control_flow will return nonzero if it detects unreachable
8663              blocks or any other irregularity with the cfg which prevents
8664              cross block scheduling.  */
8665           if (build_control_flow (s_preds, s_succs, num_preds, num_succs) != 0)
8666             find_single_block_region ();
8667           else
8668             find_rgns (s_preds, s_succs, num_preds, num_succs, dom);
8669
8670           if (sched_verbose >= 3)
8671             debug_regions ();
8672
8673           /* For now.  This will move as more and more of haifa is converted
8674              to using the cfg code in flow.c  */
8675           free_bb_mem ();
8676           free (dom);
8677           free (pdom);
8678         }
8679     }
8680
8681   /* Allocate data for this pass.  See comments, above,
8682      for what these vectors do.
8683
8684      We use xmalloc instead of alloca, because max_uid can be very large
8685      when there is a lot of function inlining.  If we used alloca, we could
8686      exceed stack limits on some hosts for some inputs.  */
8687   insn_priority = (int *) xmalloc (max_uid * sizeof (int));
8688   insn_reg_weight = (int *) xmalloc (max_uid * sizeof (int));
8689   insn_tick = (int *) xmalloc (max_uid * sizeof (int));
8690   insn_costs = (short *) xmalloc (max_uid * sizeof (short));
8691   insn_units = (short *) xmalloc (max_uid * sizeof (short));
8692   insn_blockage = (unsigned int *) xmalloc (max_uid * sizeof (unsigned int));
8693   insn_ref_count = (int *) xmalloc (max_uid * sizeof (int));
8694
8695   /* Allocate for forward dependencies */
8696   insn_dep_count = (int *) xmalloc (max_uid * sizeof (int));
8697   insn_depend = (rtx *) xmalloc (max_uid * sizeof (rtx));
8698
8699   if (reload_completed == 0)
8700     {
8701       int i;
8702
8703       sched_reg_n_calls_crossed = (int *) alloca (max_regno * sizeof (int));
8704       sched_reg_live_length = (int *) alloca (max_regno * sizeof (int));
8705       sched_reg_basic_block = (int *) alloca (max_regno * sizeof (int));
8706       bb_live_regs = ALLOCA_REG_SET ();
8707       bzero ((char *) sched_reg_n_calls_crossed, max_regno * sizeof (int));
8708       bzero ((char *) sched_reg_live_length, max_regno * sizeof (int));
8709
8710       for (i = 0; i < max_regno; i++)
8711         sched_reg_basic_block[i] = REG_BLOCK_UNKNOWN;
8712     }
8713   else
8714     {
8715       sched_reg_n_calls_crossed = 0;
8716       sched_reg_live_length = 0;
8717       bb_live_regs = 0;
8718     }
8719   init_alias_analysis ();
8720
8721   if (write_symbols != NO_DEBUG)
8722     {
8723       rtx line;
8724
8725       line_note = (rtx *) xmalloc (max_uid * sizeof (rtx));
8726       bzero ((char *) line_note, max_uid * sizeof (rtx));
8727       line_note_head = (rtx *) alloca (n_basic_blocks * sizeof (rtx));
8728       bzero ((char *) line_note_head, n_basic_blocks * sizeof (rtx));
8729
8730       /* Save-line-note-head:
8731          Determine the line-number at the start of each basic block.
8732          This must be computed and saved now, because after a basic block's
8733          predecessor has been scheduled, it is impossible to accurately
8734          determine the correct line number for the first insn of the block.  */
8735
8736       for (b = 0; b < n_basic_blocks; b++)
8737         for (line = basic_block_head[b]; line; line = PREV_INSN (line))
8738           if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
8739             {
8740               line_note_head[b] = line;
8741               break;
8742             }
8743     }
8744
8745   bzero ((char *) insn_priority, max_uid * sizeof (int));
8746   bzero ((char *) insn_reg_weight, max_uid * sizeof (int));
8747   bzero ((char *) insn_tick, max_uid * sizeof (int));
8748   bzero ((char *) insn_costs, max_uid * sizeof (short));
8749   bzero ((char *) insn_units, max_uid * sizeof (short));
8750   bzero ((char *) insn_blockage, max_uid * sizeof (unsigned int));
8751   bzero ((char *) insn_ref_count, max_uid * sizeof (int));
8752
8753   /* Initialize for forward dependencies */
8754   bzero ((char *) insn_depend, max_uid * sizeof (rtx));
8755   bzero ((char *) insn_dep_count, max_uid * sizeof (int));
8756
8757   /* Find units used in this fuction, for visualization */
8758   if (sched_verbose)
8759     init_target_units ();
8760
8761   /* ??? Add a NOTE after the last insn of the last basic block.  It is not
8762      known why this is done.  */
8763
8764   insn = basic_block_end[n_basic_blocks - 1];
8765   if (NEXT_INSN (insn) == 0
8766       || (GET_CODE (insn) != NOTE
8767           && GET_CODE (insn) != CODE_LABEL
8768   /* Don't emit a NOTE if it would end up between an unconditional
8769      jump and a BARRIER.  */
8770           && !(GET_CODE (insn) == JUMP_INSN
8771                && GET_CODE (NEXT_INSN (insn)) == BARRIER)))
8772     emit_note_after (NOTE_INSN_DELETED, basic_block_end[n_basic_blocks - 1]);
8773
8774   /* Schedule every region in the subroutine */
8775   for (rgn = 0; rgn < nr_regions; rgn++)
8776     {
8777       schedule_region (rgn);
8778
8779 #ifdef USE_C_ALLOCA
8780       alloca (0);
8781 #endif
8782     }
8783
8784   /* Reposition the prologue and epilogue notes in case we moved the
8785      prologue/epilogue insns.  */
8786   if (reload_completed)
8787     reposition_prologue_and_epilogue_notes (get_insns ());
8788
8789   /* delete redundant line notes.  */
8790   if (write_symbols != NO_DEBUG)
8791     rm_redundant_line_notes ();
8792
8793   /* Update information about uses of registers in the subroutine.  */
8794   if (reload_completed == 0)
8795     update_reg_usage ();
8796
8797   if (sched_verbose)
8798     {
8799       if (reload_completed == 0 && flag_schedule_interblock)
8800         {
8801           fprintf (dump, "\n;; Procedure interblock/speculative motions == %d/%d \n",
8802                    nr_inter, nr_spec);
8803         }
8804       else
8805         {
8806           if (nr_inter > 0)
8807             abort ();
8808         }
8809       fprintf (dump, "\n\n");
8810     }
8811
8812   free (cant_move);
8813   free (fed_by_spec_load);
8814   free (is_load_insn);
8815   free (insn_orig_block);
8816   free (insn_luid);
8817
8818   free (insn_priority);
8819   free (insn_reg_weight);
8820   free (insn_tick);
8821   free (insn_costs);
8822   free (insn_units);
8823   free (insn_blockage);
8824   free (insn_ref_count);
8825
8826   free (insn_dep_count);
8827   free (insn_depend);
8828
8829   if (write_symbols != NO_DEBUG)
8830     free (line_note);
8831
8832   if (bb_live_regs)
8833     FREE_REG_SET (bb_live_regs);
8834
8835   if (edge_table)
8836     {
8837       free (edge_table);
8838       edge_table = NULL;
8839     }
8840
8841   if (in_edges)
8842     {
8843       free (in_edges);
8844       in_edges = NULL;
8845     }
8846   if (out_edges)
8847     {
8848       free (out_edges);
8849       out_edges = NULL;
8850     }
8851 }
8852 #endif /* INSN_SCHEDULING */