OSDN Git Service

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