* decl.c (grokdeclarator): Remove const and volatile from type after
[official-gcc.git] / gcc / haifa-sched.c
blob967e8689a37b48a3877004ec11fc1cb853529c0b
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)
6 This file is part of GNU CC.
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.
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.
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. */
24 /* Instruction scheduling pass.
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.
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.
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:
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.
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.
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.
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.
77 The following list shows the order in which we want to break ties
78 among insns in the ready list:
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.
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.
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 ().
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.
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.
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.
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.
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.
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).
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.
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.
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. */
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"
168 extern char *reg_known_equiv_p;
169 extern rtx *reg_known_value;
171 #ifdef INSN_SCHEDULING
173 /* enable interblock scheduling code */
175 /* define INTERBLOCK_DEBUG for using the -fsched-max debugging facility */
176 /* #define INTERBLOCK_DEBUG */
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") */
185 static int target_units = 0;
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. */
191 static int issue_rate;
193 #ifndef ISSUE_RATE
194 #define ISSUE_RATE 1
195 #endif
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).
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.
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 */
214 #define MAX_RGN_BLOCKS 10
215 #define MAX_RGN_INSNS 100
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;
223 /* nr_inter/spec counts interblock/speculative motion for the function */
224 static int nr_inter, nr_spec;
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;
231 /* fix_sched_param() is called from toplev.c upon detection
232 of the -fsched-***-N options. */
234 void
235 fix_sched_param (param, val)
236 char *param, *val;
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);
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.
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;
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;
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;
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)])
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)])
285 static short *insn_costs;
286 #define INSN_COST(INSN) insn_costs[INSN_UID (INSN)]
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)]
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)])
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)]
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)])
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))
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))
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)
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)])
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)])
346 /* Vector indexed by basic block number giving the starting line-number
347 for each basic block. */
348 static rtx *line_note_head;
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;
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;
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;
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;
371 /* Queues, etc. */
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:
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.
385 Initially, all insns are either "Pending" or "Ready" depending on
386 whether their dependencies are satisfied.
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.
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.
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. */
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))
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)])
430 /* Data structure for keeping track of register information
431 during that register's life. */
433 struct sometimes
435 int regno;
436 int live_length;
437 int calls_crossed;
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));
477 /* Main entry point of this file. */
478 void schedule_insns PROTO ((FILE *));
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)])
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)])
488 /* Control flow graph edges are kept in circular lists. */
489 typedef struct
491 int from_block;
492 int to_block;
493 int next_in;
494 int next_out;
496 edge;
497 static edge *edge_table;
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)
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;
508 /* Circular list of incoming/outgoing edges of a block */
509 static int *in_edges;
510 static int *out_edges;
512 #define IN_EDGES(block) (in_edges[block])
513 #define OUT_EDGES(block) (out_edges[block])
515 /* List of labels which cannot be deleted, needed for control
516 flow graph construction. */
517 extern rtx forced_labels;
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));
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
531 int rgn_nr_blocks; /* number of blocks in region */
532 int rgn_blocks; /* blocks in the region (actually index in rgn_bb_table) */
534 region;
536 /* Number of regions in the procedure */
537 static int nr_regions;
539 /* Table of region descriptions */
540 static region *rgn_table;
542 /* Array of lists of regions' blocks */
543 static int *rgn_bb_table;
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
551 static int *block_to_bb;
553 /* The number of the region containing a block. */
554 static int *containing_rgn;
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])
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 *));
566 extern void debug_live PROTO ((int, int));
568 /* Blocks of the current region being scheduled. */
569 static int current_nr_blocks;
570 static int current_blocks;
572 /* The mapping from bb to block */
573 #define BB_TO_BLOCK(bb) (rgn_bb_table[current_blocks + (bb)])
576 /* Bit vectors and bitset operations are needed for computations on
577 the control flow graph. */
579 typedef unsigned HOST_WIDE_INT *bitset;
580 typedef struct
582 int *first_member; /* pointer to the list start in bitlst_table. */
583 int nr_members; /* the number of members of the bit list. */
585 bitlst;
587 static int bitlst_table_last;
588 static int bitlst_table_size;
589 static int *bitlst_table;
591 static char bitset_member PROTO ((bitset, int, int));
592 static void extract_bitlst PROTO ((bitset, int, bitlst *));
594 /* target info declarations.
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
603 char is_valid;
604 char is_speculative;
605 int src_prob;
606 bblst split_bbs;
607 bblst update_bbs;
609 candidate;
611 static candidate *candidate_table;
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.
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;
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 )
626 /* The bb being currently scheduled. */
627 static int target_bb;
629 /* List of edges. */
630 typedef bitlst edgelst;
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));
639 /* Bit-set of bbs, where bit 'i' stands for bb 'i'. */
640 typedef bitset bbset;
642 /* Number of words of the bbset. */
643 static int bbset_size;
645 /* Dominators array: dom[i] contains the bbset of dominators of
646 bb i in the region. */
647 static bbset *dom;
649 /* bb 0 is the only region entry */
650 #define IS_RGN_ENTRY(bb) (!bb)
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) )
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;
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])))
666 /* Bit-set of edges, where bit i stands for edge i. */
667 typedef bitset edgeset;
669 /* Number of edges in the region. */
670 static int rgn_nr_edges;
672 /* Array of size rgn_nr_edges. */
673 static int *rgn_edges;
675 /* Number of words in an edgeset. */
676 static int edgeset_size;
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])
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.
687 pot_split[bb] is the set of potential split edges of bb. */
688 static edgeset *pot_split;
690 /* For every bb, a set of its ancestor edges. */
691 static edgeset *ancestor_edges;
693 static void compute_dom_prob_ps PROTO ((int));
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)))
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
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));
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));
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:
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()).
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).
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()). */
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));
757 static void get_block_head_tail PROTO ((int, rtx *, rtx *));
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));
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));
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));
786 #endif /* INSN_SCHEDULING */
788 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
790 /* Helper functions for instruction scheduling. */
792 /* An INSN_LIST containing all INSN_LISTs allocated but currently unused. */
793 static rtx unused_insn_list;
795 /* An EXPR_LIST containing all EXPR_LISTs allocated but currently unused. */
796 static rtx unused_expr_list;
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));
802 static void
803 free_list (listp, unused_listp)
804 rtx *listp, *unused_listp;
806 register rtx link, prev_link;
808 if (*listp == 0)
809 return;
811 prev_link = *listp;
812 link = XEXP (prev_link, 1);
814 while (link)
816 prev_link = link;
817 link = XEXP (link, 1);
820 XEXP (prev_link, 1) = *unused_listp;
821 *unused_listp = *listp;
822 *listp = 0;
826 alloc_INSN_LIST (val, next)
827 rtx val, next;
829 rtx r;
831 if (unused_insn_list)
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);
839 else
840 r = gen_rtx_INSN_LIST (VOIDmode, val, next);
842 return r;
846 alloc_EXPR_LIST (kind, val, next)
847 int kind;
848 rtx val, next;
850 rtx r;
852 if (unused_insn_list)
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);
860 else
861 r = gen_rtx_EXPR_LIST (kind, val, next);
863 return r;
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. */
870 static void
871 add_dependence (insn, elem, dep_type)
872 rtx insn;
873 rtx elem;
874 enum reg_note dep_type;
876 rtx link, next;
878 /* Don't depend an insn on itself. */
879 if (insn == elem)
880 return;
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. */
888 next = NEXT_INSN (elem);
890 #ifdef HAVE_cc0
891 while (next && GET_CODE (next) == NOTE)
892 next = NEXT_INSN (next);
893 #endif
895 if (next && SCHED_GROUP_P (next)
896 && GET_CODE (next) != CODE_LABEL)
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);
908 /* Again, don't depend an insn on itself. */
909 if (insn == next)
910 return;
912 /* Make the dependence to NEXT, the last insn of the group, instead
913 of the original ELEM. */
914 elem = next;
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;
926 #endif
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)
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;
938 /* Might want to check one level of transitivity to save conses. */
940 link = alloc_INSN_LIST (elem, LOG_LINKS (insn));
941 LOG_LINKS (insn) = link;
943 /* Insn dependency, not data dependency. */
944 PUT_REG_NOTE_KIND (link, dep_type);
947 /* Remove ELEM wrapped in an INSN_LIST from the LOG_LINKS
948 of INSN. Abort if not found. */
950 static void
951 remove_dependence (insn, elem)
952 rtx insn;
953 rtx elem;
955 rtx prev, link, next;
956 int found = 0;
958 for (prev = 0, link = LOG_LINKS (insn); link; link = next)
960 next = XEXP (link, 1);
961 if (XEXP (link, 0) == elem)
963 if (prev)
964 XEXP (prev, 1) = next;
965 else
966 LOG_LINKS (insn) = next;
968 XEXP (link, 1) = unused_insn_list;
969 unused_insn_list = link;
971 found = 1;
973 else
974 prev = link;
977 if (!found)
978 abort ();
979 return;
982 #ifndef INSN_SCHEDULING
983 void
984 schedule_insns (dump_file)
985 FILE *dump_file;
988 #else
989 #ifndef __GNUC__
990 #define __inline
991 #endif
993 /* Computation of memory dependencies. */
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. */
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. */
1004 /* An INSN_LIST containing all insns with pending read operations. */
1005 static rtx pending_read_insns;
1007 /* An EXPR_LIST containing all MEM rtx's which are pending reads. */
1008 static rtx pending_read_mems;
1010 /* An INSN_LIST containing all insns with pending write operations. */
1011 static rtx pending_write_insns;
1013 /* An EXPR_LIST containing all MEM rtx's which are pending writes. */
1014 static rtx pending_write_mems;
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. */
1021 static int pending_lists_length;
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.
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. */
1033 static rtx last_pending_memory_flush;
1035 /* The last function call we have seen. All hard regs, and, of course,
1036 the last function call, must depend on this. */
1038 static rtx last_function_call;
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. */
1045 static rtx sched_before_next_call;
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. */
1051 static rtx last_scheduled_insn;
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. */
1064 static rtx **bb_reg_last_uses;
1065 static rtx **bb_reg_last_sets;
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;
1073 static rtx *bb_last_pending_memory_flush;
1074 static rtx *bb_last_function_call;
1075 static rtx *bb_sched_before_next_call;
1077 /* functions for construction of the control flow graph. */
1079 /* Return 1 if control flow graph should not be constructed, 0 otherwise.
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. */
1085 static int
1086 is_cfg_nonregular ()
1088 int b;
1089 rtx insn;
1090 RTX_CODE code;
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;
1097 /* If we have any forced labels, then the cfg is not well structured. */
1098 if (forced_labels)
1099 return 1;
1101 /* If we have exception handlers, then we consider the cfg not well
1102 structured. ?!? We should be able to handle this now that flow.c
1103 computes an accurate cfg for EH. */
1104 if (exception_handler_labels)
1105 return 1;
1107 /* If we have non-jumping insns which refer to labels, then we consider
1108 the cfg not well structured. */
1109 /* check for labels referred to other thn by jumps */
1110 for (b = 0; b < n_basic_blocks; b++)
1111 for (insn = basic_block_head[b];; insn = NEXT_INSN (insn))
1113 code = GET_CODE (insn);
1114 if (GET_RTX_CLASS (code) == 'i')
1116 rtx note;
1118 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1119 if (REG_NOTE_KIND (note) == REG_LABEL)
1120 return 1;
1123 if (insn == basic_block_end[b])
1124 break;
1127 /* If this function has a computed jump, then we consider the cfg
1128 not well structured. */
1129 for (b = 0; b < n_basic_blocks; b++)
1131 for (insn = basic_block_head[b];; insn = NEXT_INSN (insn))
1133 if (computed_jump_p (insn))
1134 return 1;
1136 if (insn == basic_block_end[b])
1137 break;
1141 /* All the tests passed. Consider the cfg well structured. */
1142 return 0;
1145 /* Print the control flow graph, for debugging purposes.
1146 Callable from the debugger. */
1148 void
1149 debug_control_flow ()
1151 int i, e, next;
1153 fprintf (dump, ";; --------- CONTROL FLOW GRAPH --------- \n\n");
1155 for (i = 0; i < n_basic_blocks; i++)
1157 fprintf (dump, ";;\tBasic block %d: first insn %d, last %d.\n",
1159 INSN_UID (basic_block_head[i]),
1160 INSN_UID (basic_block_end[i]));
1162 fprintf (dump, ";;\tPredecessor blocks:");
1163 for (e = IN_EDGES (i); e; e = next)
1165 fprintf (dump, " %d", FROM_BLOCK (e));
1167 next = NEXT_IN (e);
1169 if (next == IN_EDGES (i))
1170 break;
1173 fprintf (dump, "\n;;\tSuccesor blocks:");
1174 for (e = OUT_EDGES (i); e; e = next)
1176 fprintf (dump, " %d", TO_BLOCK (e));
1178 next = NEXT_OUT (e);
1180 if (next == OUT_EDGES (i))
1181 break;
1184 fprintf (dump, " \n\n");
1190 /* Build the control flow graph and set nr_edges.
1192 Instead of trying to build a cfg ourselves, we rely on flow to
1193 do it for us. Stamp out useless code (and bug) duplication.
1195 Return nonzero if an irregularity in the cfg is found which would
1196 prevent cross block scheduling. */
1198 static int
1199 build_control_flow ()
1201 int i;
1202 int_list_ptr *s_preds;
1203 int_list_ptr *s_succs;
1204 int_list_ptr succ;
1205 int *num_preds;
1206 int *num_succs;
1207 int unreachable;
1209 /* The scheduler runs after flow; therefore, we can't blindly call
1210 back into find_basic_blocks since doing so could invalidate the
1211 info in basic_block_live_at_start.
1213 Consider a block consisting entirely of dead stores; after life
1214 analysis it would be a block of NOTE_INSN_DELETED notes. If
1215 we call find_basic_blocks again, then the block would be removed
1216 entirely and invalidate our the register live information.
1218 We could (should?) recompute register live information. Doing
1219 so may even be beneficial. */
1220 s_preds = (int_list_ptr *) alloca (n_basic_blocks * sizeof (int_list_ptr));
1221 s_succs = (int_list_ptr *) alloca (n_basic_blocks * sizeof (int_list_ptr));
1222 num_preds = (int *) alloca (n_basic_blocks * sizeof (int));
1223 num_succs = (int *) alloca (n_basic_blocks * sizeof (int));
1224 compute_preds_succs (s_preds, s_succs, num_preds, num_succs);
1226 /* Count the number of edges in the cfg. */
1227 nr_edges = 0;
1228 unreachable = 0;
1229 for (i = 0; i < n_basic_blocks; i++)
1231 nr_edges += num_succs[i];
1232 if (num_preds[i] == 0)
1233 unreachable = 1;
1236 /* Account for entry/exit edges. */
1237 nr_edges += 2;
1239 in_edges = (int *) xmalloc (n_basic_blocks * sizeof (int));
1240 out_edges = (int *) xmalloc (n_basic_blocks * sizeof (int));
1241 bzero ((char *) in_edges, n_basic_blocks * sizeof (int));
1242 bzero ((char *) out_edges, n_basic_blocks * sizeof (int));
1244 edge_table = (edge *) xmalloc ((nr_edges) * sizeof (edge));
1245 bzero ((char *) edge_table, ((nr_edges) * sizeof (edge)));
1247 nr_edges = 0;
1248 for (i = 0; i < n_basic_blocks; i++)
1249 for (succ = s_succs[i]; succ; succ = succ->next)
1251 if (INT_LIST_VAL (succ) != EXIT_BLOCK)
1252 new_edge (i, INT_LIST_VAL (succ));
1255 /* increment by 1, since edge 0 is unused. */
1256 nr_edges++;
1258 /* For now. This will move as more and more of haifa is converted
1259 to using the cfg code in flow.c */
1260 free_bb_mem ();
1261 return unreachable;
1265 /* Record an edge in the control flow graph from SOURCE to TARGET.
1267 In theory, this is redundant with the s_succs computed above, but
1268 we have not converted all of haifa to use information from the
1269 integer lists. */
1271 static void
1272 new_edge (source, target)
1273 int source, target;
1275 int e, next_edge;
1276 int curr_edge, fst_edge;
1278 /* check for duplicates */
1279 fst_edge = curr_edge = OUT_EDGES (source);
1280 while (curr_edge)
1282 if (FROM_BLOCK (curr_edge) == source
1283 && TO_BLOCK (curr_edge) == target)
1285 return;
1288 curr_edge = NEXT_OUT (curr_edge);
1290 if (fst_edge == curr_edge)
1291 break;
1294 e = ++nr_edges;
1296 FROM_BLOCK (e) = source;
1297 TO_BLOCK (e) = target;
1299 if (OUT_EDGES (source))
1301 next_edge = NEXT_OUT (OUT_EDGES (source));
1302 NEXT_OUT (OUT_EDGES (source)) = e;
1303 NEXT_OUT (e) = next_edge;
1305 else
1307 OUT_EDGES (source) = e;
1308 NEXT_OUT (e) = e;
1311 if (IN_EDGES (target))
1313 next_edge = NEXT_IN (IN_EDGES (target));
1314 NEXT_IN (IN_EDGES (target)) = e;
1315 NEXT_IN (e) = next_edge;
1317 else
1319 IN_EDGES (target) = e;
1320 NEXT_IN (e) = e;
1325 /* BITSET macros for operations on the control flow graph. */
1327 /* Compute bitwise union of two bitsets. */
1328 #define BITSET_UNION(set1, set2, len) \
1329 do { register bitset tp = set1, sp = set2; \
1330 register int i; \
1331 for (i = 0; i < len; i++) \
1332 *(tp++) |= *(sp++); } while (0)
1334 /* Compute bitwise intersection of two bitsets. */
1335 #define BITSET_INTER(set1, set2, len) \
1336 do { register bitset tp = set1, sp = set2; \
1337 register int i; \
1338 for (i = 0; i < len; i++) \
1339 *(tp++) &= *(sp++); } while (0)
1341 /* Compute bitwise difference of two bitsets. */
1342 #define BITSET_DIFFER(set1, set2, len) \
1343 do { register bitset tp = set1, sp = set2; \
1344 register int i; \
1345 for (i = 0; i < len; i++) \
1346 *(tp++) &= ~*(sp++); } while (0)
1348 /* Inverts every bit of bitset 'set' */
1349 #define BITSET_INVERT(set, len) \
1350 do { register bitset tmpset = set; \
1351 register int i; \
1352 for (i = 0; i < len; i++, tmpset++) \
1353 *tmpset = ~*tmpset; } while (0)
1355 /* Turn on the index'th bit in bitset set. */
1356 #define BITSET_ADD(set, index, len) \
1358 if (index >= HOST_BITS_PER_WIDE_INT * len) \
1359 abort (); \
1360 else \
1361 set[index/HOST_BITS_PER_WIDE_INT] |= \
1362 1 << (index % HOST_BITS_PER_WIDE_INT); \
1365 /* Turn off the index'th bit in set. */
1366 #define BITSET_REMOVE(set, index, len) \
1368 if (index >= HOST_BITS_PER_WIDE_INT * len) \
1369 abort (); \
1370 else \
1371 set[index/HOST_BITS_PER_WIDE_INT] &= \
1372 ~(1 << (index%HOST_BITS_PER_WIDE_INT)); \
1376 /* Check if the index'th bit in bitset set is on. */
1378 static char
1379 bitset_member (set, index, len)
1380 bitset set;
1381 int index, len;
1383 if (index >= HOST_BITS_PER_WIDE_INT * len)
1384 abort ();
1385 return (set[index / HOST_BITS_PER_WIDE_INT] &
1386 1 << (index % HOST_BITS_PER_WIDE_INT)) ? 1 : 0;
1390 /* Translate a bit-set SET to a list BL of the bit-set members. */
1392 static void
1393 extract_bitlst (set, len, bl)
1394 bitset set;
1395 int len;
1396 bitlst *bl;
1398 int i, j, offset;
1399 unsigned HOST_WIDE_INT word;
1401 /* bblst table space is reused in each call to extract_bitlst */
1402 bitlst_table_last = 0;
1404 bl->first_member = &bitlst_table[bitlst_table_last];
1405 bl->nr_members = 0;
1407 for (i = 0; i < len; i++)
1409 word = set[i];
1410 offset = i * HOST_BITS_PER_WIDE_INT;
1411 for (j = 0; word; j++)
1413 if (word & 1)
1415 bitlst_table[bitlst_table_last++] = offset;
1416 (bl->nr_members)++;
1418 word >>= 1;
1419 ++offset;
1426 /* functions for the construction of regions */
1428 /* Print the regions, for debugging purposes. Callable from debugger. */
1430 void
1431 debug_regions ()
1433 int rgn, bb;
1435 fprintf (dump, "\n;; ------------ REGIONS ----------\n\n");
1436 for (rgn = 0; rgn < nr_regions; rgn++)
1438 fprintf (dump, ";;\trgn %d nr_blocks %d:\n", rgn,
1439 rgn_table[rgn].rgn_nr_blocks);
1440 fprintf (dump, ";;\tbb/block: ");
1442 for (bb = 0; bb < rgn_table[rgn].rgn_nr_blocks; bb++)
1444 current_blocks = RGN_BLOCKS (rgn);
1446 if (bb != BLOCK_TO_BB (BB_TO_BLOCK (bb)))
1447 abort ();
1449 fprintf (dump, " %d/%d ", bb, BB_TO_BLOCK (bb));
1452 fprintf (dump, "\n\n");
1457 /* Build a single block region for each basic block in the function.
1458 This allows for using the same code for interblock and basic block
1459 scheduling. */
1461 static void
1462 find_single_block_region ()
1464 int i;
1466 for (i = 0; i < n_basic_blocks; i++)
1468 rgn_bb_table[i] = i;
1469 RGN_NR_BLOCKS (i) = 1;
1470 RGN_BLOCKS (i) = i;
1471 CONTAINING_RGN (i) = i;
1472 BLOCK_TO_BB (i) = 0;
1474 nr_regions = n_basic_blocks;
1478 /* Update number of blocks and the estimate for number of insns
1479 in the region. Return 1 if the region is "too large" for interblock
1480 scheduling (compile time considerations), otherwise return 0. */
1482 static int
1483 too_large (block, num_bbs, num_insns)
1484 int block, *num_bbs, *num_insns;
1486 (*num_bbs)++;
1487 (*num_insns) += (INSN_LUID (basic_block_end[block]) -
1488 INSN_LUID (basic_block_head[block]));
1489 if ((*num_bbs > max_rgn_blocks) || (*num_insns > max_rgn_insns))
1490 return 1;
1491 else
1492 return 0;
1496 /* Update_loop_relations(blk, hdr): Check if the loop headed by max_hdr[blk]
1497 is still an inner loop. Put in max_hdr[blk] the header of the most inner
1498 loop containing blk. */
1499 #define UPDATE_LOOP_RELATIONS(blk, hdr) \
1501 if (max_hdr[blk] == -1) \
1502 max_hdr[blk] = hdr; \
1503 else if (dfs_nr[max_hdr[blk]] > dfs_nr[hdr]) \
1504 inner[hdr] = 0; \
1505 else if (dfs_nr[max_hdr[blk]] < dfs_nr[hdr]) \
1507 inner[max_hdr[blk]] = 0; \
1508 max_hdr[blk] = hdr; \
1513 /* Find regions for interblock scheduling: a loop-free procedure, a reducible
1514 inner loop, or a basic block not contained in any other region.
1515 The procedures control flow graph is traversed twice.
1516 First traversal, a DFS, finds the headers of inner loops in the graph,
1517 and verifies that there are no unreacable blocks.
1518 Second traversal processes headers of inner loops, checking that the
1519 loop is reducible. The loop blocks that form a region are put into the
1520 region's blocks list in topological order.
1522 The following variables are changed by the function: rgn_nr, rgn_table,
1523 rgn_bb_table, block_to_bb and containing_rgn. */
1525 static void
1526 find_rgns ()
1528 int *max_hdr, *dfs_nr, *stack, *queue, *degree;
1529 char *header, *inner, *passed, *in_stack, *in_queue, no_loops = 1;
1530 int node, child, loop_head, i, j, fst_edge, head, tail;
1531 int count = 0, sp, idx = 0, current_edge = out_edges[0];
1532 int num_bbs, num_insns;
1533 int too_large_failure;
1536 The following data structures are computed by the first traversal and
1537 are used by the second traversal:
1538 header[i] - flag set if the block i is the header of a loop.
1539 inner[i] - initially set. It is reset if the the block i is the header
1540 of a non-inner loop.
1541 max_hdr[i] - the header of the inner loop containing block i.
1542 (for a block i not in an inner loop it may be -1 or the
1543 header of the most inner loop containing the block).
1545 These data structures are used by the first traversal only:
1546 stack - non-recursive DFS implementation which uses a stack of edges.
1547 sp - top of the stack of edges
1548 dfs_nr[i] - the DFS ordering of block i.
1549 in_stack[i] - flag set if the block i is in the DFS stack.
1551 These data structures are used by the second traversal only:
1552 queue - queue containing the blocks of the current region.
1553 head and tail - queue boundaries.
1554 in_queue[i] - flag set if the block i is in queue */
1556 /* function's inner arrays allocation and initialization */
1557 max_hdr = (int *) alloca (n_basic_blocks * sizeof (int));
1558 dfs_nr = (int *) alloca (n_basic_blocks * sizeof (int));
1559 bzero ((char *) dfs_nr, n_basic_blocks * sizeof (int));
1560 stack = (int *) alloca (nr_edges * sizeof (int));
1561 queue = (int *) alloca (n_basic_blocks * sizeof (int));
1563 inner = (char *) alloca (n_basic_blocks * sizeof (char));
1564 header = (char *) alloca (n_basic_blocks * sizeof (char));
1565 bzero ((char *) header, n_basic_blocks * sizeof (char));
1566 passed = (char *) alloca (nr_edges * sizeof (char));
1567 bzero ((char *) passed, nr_edges * sizeof (char));
1568 in_stack = (char *) alloca (nr_edges * sizeof (char));
1569 bzero ((char *) in_stack, nr_edges * sizeof (char));
1571 in_queue = (char *) alloca (n_basic_blocks * sizeof (char));
1573 for (i = 0; i < n_basic_blocks; i++)
1575 inner[i] = 1;
1576 max_hdr[i] = -1;
1579 /* First traversal: DFS, finds inner loops in control flow graph */
1581 sp = -1;
1582 while (1)
1584 if (current_edge == 0 || passed[current_edge])
1586 /* Here, if current_edge < 0, this is a leaf block.
1587 Otherwise current_edge was already passed. Note that in
1588 the latter case, not only current_edge but also all its
1589 NEXT_OUT edges are also passed. We have to "climb up on
1590 edges in the stack", looking for the first (already
1591 passed) edge whose NEXT_OUT was not passed yet. */
1593 while (sp >= 0 && (current_edge == 0 || passed[current_edge]))
1595 current_edge = stack[sp--];
1596 node = FROM_BLOCK (current_edge);
1597 child = TO_BLOCK (current_edge);
1598 in_stack[child] = 0;
1599 if (max_hdr[child] >= 0 && in_stack[max_hdr[child]])
1600 UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
1601 current_edge = NEXT_OUT (current_edge);
1604 /* stack empty - the whole graph is traversed. */
1605 if (sp < 0 && passed[current_edge])
1606 break;
1607 continue;
1610 node = FROM_BLOCK (current_edge);
1611 dfs_nr[node] = ++count;
1612 in_stack[node] = 1;
1613 child = TO_BLOCK (current_edge);
1615 /* found a loop header */
1616 if (in_stack[child])
1618 no_loops = 0;
1619 header[child] = 1;
1620 max_hdr[child] = child;
1621 UPDATE_LOOP_RELATIONS (node, child);
1622 passed[current_edge] = 1;
1623 current_edge = NEXT_OUT (current_edge);
1624 continue;
1627 /* the child was already visited once, no need to go down from
1628 it, everything is traversed there. */
1629 if (dfs_nr[child])
1631 if (max_hdr[child] >= 0 && in_stack[max_hdr[child]])
1632 UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
1633 passed[current_edge] = 1;
1634 current_edge = NEXT_OUT (current_edge);
1635 continue;
1638 /* this is a step down in the dfs traversal */
1639 stack[++sp] = current_edge;
1640 passed[current_edge] = 1;
1641 current_edge = OUT_EDGES (child);
1642 } /* while (1); */
1644 /* Second travsersal: find reducible inner loops, and sort
1645 topologically the blocks of each region */
1646 degree = dfs_nr; /* reuse dfs_nr array - it is not needed anymore */
1647 bzero ((char *) in_queue, n_basic_blocks * sizeof (char));
1649 if (no_loops)
1650 header[0] = 1;
1652 /* compute the in-degree of every block in the graph */
1653 for (i = 0; i < n_basic_blocks; i++)
1655 fst_edge = IN_EDGES (i);
1656 if (fst_edge > 0)
1658 degree[i] = 1;
1659 current_edge = NEXT_IN (fst_edge);
1660 while (fst_edge != current_edge)
1662 ++degree[i];
1663 current_edge = NEXT_IN (current_edge);
1666 else
1667 degree[i] = 0;
1670 /* pass through all graph blocks, looking for headers of inner loops */
1671 for (i = 0; i < n_basic_blocks; i++)
1674 if (header[i] && inner[i])
1677 /* i is a header of a potentially reducible inner loop, or
1678 block 0 in a subroutine with no loops at all */
1679 head = tail = -1;
1680 too_large_failure = 0;
1681 loop_head = max_hdr[i];
1683 /* decrease in_degree of all i's successors, (this is needed
1684 for the topological ordering) */
1685 fst_edge = current_edge = OUT_EDGES (i);
1686 if (fst_edge > 0)
1690 --degree[TO_BLOCK (current_edge)];
1691 current_edge = NEXT_OUT (current_edge);
1693 while (fst_edge != current_edge);
1696 /* estimate # insns, and count # blocks in the region. */
1697 num_bbs = 1;
1698 num_insns = INSN_LUID (basic_block_end[i]) - INSN_LUID (basic_block_head[i]);
1701 /* find all loop latches, if it is a true loop header, or
1702 all leaves if the graph has no loops at all */
1703 if (no_loops)
1705 for (j = 0; j < n_basic_blocks; j++)
1706 if (out_edges[j] == 0) /* a leaf */
1708 queue[++tail] = j;
1709 in_queue[j] = 1;
1711 if (too_large (j, &num_bbs, &num_insns))
1713 too_large_failure = 1;
1714 break;
1718 else
1720 fst_edge = current_edge = IN_EDGES (i);
1723 node = FROM_BLOCK (current_edge);
1724 if (max_hdr[node] == loop_head && node != i) /* a latch */
1726 queue[++tail] = node;
1727 in_queue[node] = 1;
1729 if (too_large (node, &num_bbs, &num_insns))
1731 too_large_failure = 1;
1732 break;
1735 current_edge = NEXT_IN (current_edge);
1737 while (fst_edge != current_edge);
1740 /* Put in queue[] all blocks that belong to the loop. Check
1741 that the loop is reducible, traversing back from the loop
1742 latches up to the loop header. */
1743 while (head < tail && !too_large_failure)
1745 child = queue[++head];
1746 fst_edge = current_edge = IN_EDGES (child);
1749 node = FROM_BLOCK (current_edge);
1751 if (max_hdr[node] != loop_head)
1752 { /* another entry to loop, it is irreducible */
1753 tail = -1;
1754 break;
1756 else if (!in_queue[node] && node != i)
1758 queue[++tail] = node;
1759 in_queue[node] = 1;
1761 if (too_large (node, &num_bbs, &num_insns))
1763 too_large_failure = 1;
1764 break;
1767 current_edge = NEXT_IN (current_edge);
1769 while (fst_edge != current_edge);
1772 if (tail >= 0 && !too_large_failure)
1774 /* Place the loop header into list of region blocks */
1775 degree[i] = -1;
1776 rgn_bb_table[idx] = i;
1777 RGN_NR_BLOCKS (nr_regions) = num_bbs;
1778 RGN_BLOCKS (nr_regions) = idx++;
1779 CONTAINING_RGN (i) = nr_regions;
1780 BLOCK_TO_BB (i) = count = 0;
1782 /* remove blocks from queue[], (in topological order), when
1783 their in_degree becomes 0. We scan the queue over and
1784 over again until it is empty. Note: there may be a more
1785 efficient way to do it. */
1786 while (tail >= 0)
1788 if (head < 0)
1789 head = tail;
1790 child = queue[head];
1791 if (degree[child] == 0)
1793 degree[child] = -1;
1794 rgn_bb_table[idx++] = child;
1795 BLOCK_TO_BB (child) = ++count;
1796 CONTAINING_RGN (child) = nr_regions;
1797 queue[head] = queue[tail--];
1798 fst_edge = current_edge = OUT_EDGES (child);
1800 if (fst_edge > 0)
1804 --degree[TO_BLOCK (current_edge)];
1805 current_edge = NEXT_OUT (current_edge);
1807 while (fst_edge != current_edge);
1810 else
1811 --head;
1813 ++nr_regions;
1818 /* define each of all other blocks as a region itself */
1819 for (i = 0; i < n_basic_blocks; i++)
1820 if (degree[i] >= 0)
1822 rgn_bb_table[idx] = i;
1823 RGN_NR_BLOCKS (nr_regions) = 1;
1824 RGN_BLOCKS (nr_regions) = idx++;
1825 CONTAINING_RGN (i) = nr_regions++;
1826 BLOCK_TO_BB (i) = 0;
1829 } /* find_rgns */
1832 /* functions for regions scheduling information */
1834 /* Compute dominators, probability, and potential-split-edges of bb.
1835 Assume that these values were already computed for bb's predecessors. */
1837 static void
1838 compute_dom_prob_ps (bb)
1839 int bb;
1841 int nxt_in_edge, fst_in_edge, pred;
1842 int fst_out_edge, nxt_out_edge, nr_out_edges, nr_rgn_out_edges;
1844 prob[bb] = 0.0;
1845 if (IS_RGN_ENTRY (bb))
1847 BITSET_ADD (dom[bb], 0, bbset_size);
1848 prob[bb] = 1.0;
1849 return;
1852 fst_in_edge = nxt_in_edge = IN_EDGES (BB_TO_BLOCK (bb));
1854 /* intialize dom[bb] to '111..1' */
1855 BITSET_INVERT (dom[bb], bbset_size);
1859 pred = FROM_BLOCK (nxt_in_edge);
1860 BITSET_INTER (dom[bb], dom[BLOCK_TO_BB (pred)], bbset_size);
1862 BITSET_UNION (ancestor_edges[bb], ancestor_edges[BLOCK_TO_BB (pred)],
1863 edgeset_size);
1865 BITSET_ADD (ancestor_edges[bb], EDGE_TO_BIT (nxt_in_edge), edgeset_size);
1867 nr_out_edges = 1;
1868 nr_rgn_out_edges = 0;
1869 fst_out_edge = OUT_EDGES (pred);
1870 nxt_out_edge = NEXT_OUT (fst_out_edge);
1871 BITSET_UNION (pot_split[bb], pot_split[BLOCK_TO_BB (pred)],
1872 edgeset_size);
1874 BITSET_ADD (pot_split[bb], EDGE_TO_BIT (fst_out_edge), edgeset_size);
1876 /* the successor doesn't belong the region? */
1877 if (CONTAINING_RGN (TO_BLOCK (fst_out_edge)) !=
1878 CONTAINING_RGN (BB_TO_BLOCK (bb)))
1879 ++nr_rgn_out_edges;
1881 while (fst_out_edge != nxt_out_edge)
1883 ++nr_out_edges;
1884 /* the successor doesn't belong the region? */
1885 if (CONTAINING_RGN (TO_BLOCK (nxt_out_edge)) !=
1886 CONTAINING_RGN (BB_TO_BLOCK (bb)))
1887 ++nr_rgn_out_edges;
1888 BITSET_ADD (pot_split[bb], EDGE_TO_BIT (nxt_out_edge), edgeset_size);
1889 nxt_out_edge = NEXT_OUT (nxt_out_edge);
1893 /* now nr_rgn_out_edges is the number of region-exit edges from pred,
1894 and nr_out_edges will be the number of pred out edges not leaving
1895 the region. */
1896 nr_out_edges -= nr_rgn_out_edges;
1897 if (nr_rgn_out_edges > 0)
1898 prob[bb] += 0.9 * prob[BLOCK_TO_BB (pred)] / nr_out_edges;
1899 else
1900 prob[bb] += prob[BLOCK_TO_BB (pred)] / nr_out_edges;
1901 nxt_in_edge = NEXT_IN (nxt_in_edge);
1903 while (fst_in_edge != nxt_in_edge);
1905 BITSET_ADD (dom[bb], bb, bbset_size);
1906 BITSET_DIFFER (pot_split[bb], ancestor_edges[bb], edgeset_size);
1908 if (sched_verbose >= 2)
1909 fprintf (dump, ";; bb_prob(%d, %d) = %3d\n", bb, BB_TO_BLOCK (bb), (int) (100.0 * prob[bb]));
1910 } /* compute_dom_prob_ps */
1912 /* functions for target info */
1914 /* Compute in BL the list of split-edges of bb_src relatively to bb_trg.
1915 Note that bb_trg dominates bb_src. */
1917 static void
1918 split_edges (bb_src, bb_trg, bl)
1919 int bb_src;
1920 int bb_trg;
1921 edgelst *bl;
1923 int es = edgeset_size;
1924 edgeset src = (edgeset) alloca (es * sizeof (HOST_WIDE_INT));
1926 while (es--)
1927 src[es] = (pot_split[bb_src])[es];
1928 BITSET_DIFFER (src, pot_split[bb_trg], edgeset_size);
1929 extract_bitlst (src, edgeset_size, bl);
1933 /* Find the valid candidate-source-blocks for the target block TRG, compute
1934 their probability, and check if they are speculative or not.
1935 For speculative sources, compute their update-blocks and split-blocks. */
1937 static void
1938 compute_trg_info (trg)
1939 int trg;
1941 register candidate *sp;
1942 edgelst el;
1943 int check_block, update_idx;
1944 int i, j, k, fst_edge, nxt_edge;
1946 /* define some of the fields for the target bb as well */
1947 sp = candidate_table + trg;
1948 sp->is_valid = 1;
1949 sp->is_speculative = 0;
1950 sp->src_prob = 100;
1952 for (i = trg + 1; i < current_nr_blocks; i++)
1954 sp = candidate_table + i;
1956 sp->is_valid = IS_DOMINATED (i, trg);
1957 if (sp->is_valid)
1959 sp->src_prob = GET_SRC_PROB (i, trg);
1960 sp->is_valid = (sp->src_prob >= MIN_PROBABILITY);
1963 if (sp->is_valid)
1965 split_edges (i, trg, &el);
1966 sp->is_speculative = (el.nr_members) ? 1 : 0;
1967 if (sp->is_speculative && !flag_schedule_speculative)
1968 sp->is_valid = 0;
1971 if (sp->is_valid)
1973 sp->split_bbs.first_member = &bblst_table[bblst_last];
1974 sp->split_bbs.nr_members = el.nr_members;
1975 for (j = 0; j < el.nr_members; bblst_last++, j++)
1976 bblst_table[bblst_last] =
1977 TO_BLOCK (rgn_edges[el.first_member[j]]);
1978 sp->update_bbs.first_member = &bblst_table[bblst_last];
1979 update_idx = 0;
1980 for (j = 0; j < el.nr_members; j++)
1982 check_block = FROM_BLOCK (rgn_edges[el.first_member[j]]);
1983 fst_edge = nxt_edge = OUT_EDGES (check_block);
1986 for (k = 0; k < el.nr_members; k++)
1987 if (EDGE_TO_BIT (nxt_edge) == el.first_member[k])
1988 break;
1990 if (k >= el.nr_members)
1992 bblst_table[bblst_last++] = TO_BLOCK (nxt_edge);
1993 update_idx++;
1996 nxt_edge = NEXT_OUT (nxt_edge);
1998 while (fst_edge != nxt_edge);
2000 sp->update_bbs.nr_members = update_idx;
2003 else
2005 sp->split_bbs.nr_members = sp->update_bbs.nr_members = 0;
2007 sp->is_speculative = 0;
2008 sp->src_prob = 0;
2011 } /* compute_trg_info */
2014 /* Print candidates info, for debugging purposes. Callable from debugger. */
2016 void
2017 debug_candidate (i)
2018 int i;
2020 if (!candidate_table[i].is_valid)
2021 return;
2023 if (candidate_table[i].is_speculative)
2025 int j;
2026 fprintf (dump, "src b %d bb %d speculative \n", BB_TO_BLOCK (i), i);
2028 fprintf (dump, "split path: ");
2029 for (j = 0; j < candidate_table[i].split_bbs.nr_members; j++)
2031 int b = candidate_table[i].split_bbs.first_member[j];
2033 fprintf (dump, " %d ", b);
2035 fprintf (dump, "\n");
2037 fprintf (dump, "update path: ");
2038 for (j = 0; j < candidate_table[i].update_bbs.nr_members; j++)
2040 int b = candidate_table[i].update_bbs.first_member[j];
2042 fprintf (dump, " %d ", b);
2044 fprintf (dump, "\n");
2046 else
2048 fprintf (dump, " src %d equivalent\n", BB_TO_BLOCK (i));
2053 /* Print candidates info, for debugging purposes. Callable from debugger. */
2055 void
2056 debug_candidates (trg)
2057 int trg;
2059 int i;
2061 fprintf (dump, "----------- candidate table: target: b=%d bb=%d ---\n",
2062 BB_TO_BLOCK (trg), trg);
2063 for (i = trg + 1; i < current_nr_blocks; i++)
2064 debug_candidate (i);
2068 /* functions for speculative scheduing */
2070 /* Return 0 if x is a set of a register alive in the beginning of one
2071 of the split-blocks of src, otherwise return 1. */
2073 static int
2074 check_live_1 (src, x)
2075 int src;
2076 rtx x;
2078 register int i;
2079 register int regno;
2080 register rtx reg = SET_DEST (x);
2082 if (reg == 0)
2083 return 1;
2085 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
2086 || GET_CODE (reg) == SIGN_EXTRACT
2087 || GET_CODE (reg) == STRICT_LOW_PART)
2088 reg = XEXP (reg, 0);
2090 if (GET_CODE (reg) != REG)
2091 return 1;
2093 regno = REGNO (reg);
2095 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2097 /* Global registers are assumed live */
2098 return 0;
2100 else
2102 if (regno < FIRST_PSEUDO_REGISTER)
2104 /* check for hard registers */
2105 int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
2106 while (--j >= 0)
2108 for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
2110 int b = candidate_table[src].split_bbs.first_member[i];
2112 if (REGNO_REG_SET_P (basic_block_live_at_start[b], regno + j))
2114 return 0;
2119 else
2121 /* check for psuedo registers */
2122 for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
2124 int b = candidate_table[src].split_bbs.first_member[i];
2126 if (REGNO_REG_SET_P (basic_block_live_at_start[b], regno))
2128 return 0;
2134 return 1;
2138 /* If x is a set of a register R, mark that R is alive in the beginning
2139 of every update-block of src. */
2141 static void
2142 update_live_1 (src, x)
2143 int src;
2144 rtx x;
2146 register int i;
2147 register int regno;
2148 register rtx reg = SET_DEST (x);
2150 if (reg == 0)
2151 return;
2153 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
2154 || GET_CODE (reg) == SIGN_EXTRACT
2155 || GET_CODE (reg) == STRICT_LOW_PART)
2156 reg = XEXP (reg, 0);
2158 if (GET_CODE (reg) != REG)
2159 return;
2161 /* Global registers are always live, so the code below does not apply
2162 to them. */
2164 regno = REGNO (reg);
2166 if (regno >= FIRST_PSEUDO_REGISTER || !global_regs[regno])
2168 if (regno < FIRST_PSEUDO_REGISTER)
2170 int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
2171 while (--j >= 0)
2173 for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
2175 int b = candidate_table[src].update_bbs.first_member[i];
2177 SET_REGNO_REG_SET (basic_block_live_at_start[b], regno + j);
2181 else
2183 for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
2185 int b = candidate_table[src].update_bbs.first_member[i];
2187 SET_REGNO_REG_SET (basic_block_live_at_start[b], regno);
2194 /* Return 1 if insn can be speculatively moved from block src to trg,
2195 otherwise return 0. Called before first insertion of insn to
2196 ready-list or before the scheduling. */
2198 static int
2199 check_live (insn, src)
2200 rtx insn;
2201 int src;
2203 /* find the registers set by instruction */
2204 if (GET_CODE (PATTERN (insn)) == SET
2205 || GET_CODE (PATTERN (insn)) == CLOBBER)
2206 return check_live_1 (src, PATTERN (insn));
2207 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2209 int j;
2210 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
2211 if ((GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
2212 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
2213 && !check_live_1 (src, XVECEXP (PATTERN (insn), 0, j)))
2214 return 0;
2216 return 1;
2219 return 1;
2223 /* Update the live registers info after insn was moved speculatively from
2224 block src to trg. */
2226 static void
2227 update_live (insn, src)
2228 rtx insn;
2229 int src;
2231 /* find the registers set by instruction */
2232 if (GET_CODE (PATTERN (insn)) == SET
2233 || GET_CODE (PATTERN (insn)) == CLOBBER)
2234 update_live_1 (src, PATTERN (insn));
2235 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2237 int j;
2238 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
2239 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
2240 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
2241 update_live_1 (src, XVECEXP (PATTERN (insn), 0, j));
2245 /* Exception Free Loads:
2247 We define five classes of speculative loads: IFREE, IRISKY,
2248 PFREE, PRISKY, and MFREE.
2250 IFREE loads are loads that are proved to be exception-free, just
2251 by examining the load insn. Examples for such loads are loads
2252 from TOC and loads of global data.
2254 IRISKY loads are loads that are proved to be exception-risky,
2255 just by examining the load insn. Examples for such loads are
2256 volatile loads and loads from shared memory.
2258 PFREE loads are loads for which we can prove, by examining other
2259 insns, that they are exception-free. Currently, this class consists
2260 of loads for which we are able to find a "similar load", either in
2261 the target block, or, if only one split-block exists, in that split
2262 block. Load2 is similar to load1 if both have same single base
2263 register. We identify only part of the similar loads, by finding
2264 an insn upon which both load1 and load2 have a DEF-USE dependence.
2266 PRISKY loads are loads for which we can prove, by examining other
2267 insns, that they are exception-risky. Currently we have two proofs for
2268 such loads. The first proof detects loads that are probably guarded by a
2269 test on the memory address. This proof is based on the
2270 backward and forward data dependence information for the region.
2271 Let load-insn be the examined load.
2272 Load-insn is PRISKY iff ALL the following hold:
2274 - insn1 is not in the same block as load-insn
2275 - there is a DEF-USE dependence chain (insn1, ..., load-insn)
2276 - test-insn is either a compare or a branch, not in the same block as load-insn
2277 - load-insn is reachable from test-insn
2278 - there is a DEF-USE dependence chain (insn1, ..., test-insn)
2280 This proof might fail when the compare and the load are fed
2281 by an insn not in the region. To solve this, we will add to this
2282 group all loads that have no input DEF-USE dependence.
2284 The second proof detects loads that are directly or indirectly
2285 fed by a speculative load. This proof is affected by the
2286 scheduling process. We will use the flag fed_by_spec_load.
2287 Initially, all insns have this flag reset. After a speculative
2288 motion of an insn, if insn is either a load, or marked as
2289 fed_by_spec_load, we will also mark as fed_by_spec_load every
2290 insn1 for which a DEF-USE dependence (insn, insn1) exists. A
2291 load which is fed_by_spec_load is also PRISKY.
2293 MFREE (maybe-free) loads are all the remaining loads. They may be
2294 exception-free, but we cannot prove it.
2296 Now, all loads in IFREE and PFREE classes are considered
2297 exception-free, while all loads in IRISKY and PRISKY classes are
2298 considered exception-risky. As for loads in the MFREE class,
2299 these are considered either exception-free or exception-risky,
2300 depending on whether we are pessimistic or optimistic. We have
2301 to take the pessimistic approach to assure the safety of
2302 speculative scheduling, but we can take the optimistic approach
2303 by invoking the -fsched_spec_load_dangerous option. */
2305 enum INSN_TRAP_CLASS
2307 TRAP_FREE = 0, IFREE = 1, PFREE_CANDIDATE = 2,
2308 PRISKY_CANDIDATE = 3, IRISKY = 4, TRAP_RISKY = 5
2311 #define WORST_CLASS(class1, class2) \
2312 ((class1 > class2) ? class1 : class2)
2314 /* Indexed by INSN_UID, and set if there's DEF-USE dependence between */
2315 /* some speculatively moved load insn and this one. */
2316 char *fed_by_spec_load;
2317 char *is_load_insn;
2319 /* Non-zero if block bb_to is equal to, or reachable from block bb_from. */
2320 #define IS_REACHABLE(bb_from, bb_to) \
2321 (bb_from == bb_to \
2322 || IS_RGN_ENTRY (bb_from) \
2323 || (bitset_member (ancestor_edges[bb_to], \
2324 EDGE_TO_BIT (IN_EDGES (BB_TO_BLOCK (bb_from))), \
2325 edgeset_size)))
2326 #define FED_BY_SPEC_LOAD(insn) (fed_by_spec_load[INSN_UID (insn)])
2327 #define IS_LOAD_INSN(insn) (is_load_insn[INSN_UID (insn)])
2329 /* Non-zero iff the address is comprised from at most 1 register */
2330 #define CONST_BASED_ADDRESS_P(x) \
2331 (GET_CODE (x) == REG \
2332 || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS \
2333 || (GET_CODE (x) == LO_SUM)) \
2334 && (GET_CODE (XEXP (x, 0)) == CONST_INT \
2335 || GET_CODE (XEXP (x, 1)) == CONST_INT)))
2337 /* Turns on the fed_by_spec_load flag for insns fed by load_insn. */
2339 static void
2340 set_spec_fed (load_insn)
2341 rtx load_insn;
2343 rtx link;
2345 for (link = INSN_DEPEND (load_insn); link; link = XEXP (link, 1))
2346 if (GET_MODE (link) == VOIDmode)
2347 FED_BY_SPEC_LOAD (XEXP (link, 0)) = 1;
2348 } /* set_spec_fed */
2350 /* On the path from the insn to load_insn_bb, find a conditional branch */
2351 /* depending on insn, that guards the speculative load. */
2353 static int
2354 find_conditional_protection (insn, load_insn_bb)
2355 rtx insn;
2356 int load_insn_bb;
2358 rtx link;
2360 /* iterate through DEF-USE forward dependences */
2361 for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
2363 rtx next = XEXP (link, 0);
2364 if ((CONTAINING_RGN (INSN_BLOCK (next)) ==
2365 CONTAINING_RGN (BB_TO_BLOCK (load_insn_bb)))
2366 && IS_REACHABLE (INSN_BB (next), load_insn_bb)
2367 && load_insn_bb != INSN_BB (next)
2368 && GET_MODE (link) == VOIDmode
2369 && (GET_CODE (next) == JUMP_INSN
2370 || find_conditional_protection (next, load_insn_bb)))
2371 return 1;
2373 return 0;
2374 } /* find_conditional_protection */
2376 /* Returns 1 if the same insn1 that participates in the computation
2377 of load_insn's address is feeding a conditional branch that is
2378 guarding on load_insn. This is true if we find a the two DEF-USE
2379 chains:
2380 insn1 -> ... -> conditional-branch
2381 insn1 -> ... -> load_insn,
2382 and if a flow path exist:
2383 insn1 -> ... -> conditional-branch -> ... -> load_insn,
2384 and if insn1 is on the path
2385 region-entry -> ... -> bb_trg -> ... load_insn.
2387 Locate insn1 by climbing on LOG_LINKS from load_insn.
2388 Locate the branch by following INSN_DEPEND from insn1. */
2390 static int
2391 is_conditionally_protected (load_insn, bb_src, bb_trg)
2392 rtx load_insn;
2393 int bb_src, bb_trg;
2395 rtx link;
2397 for (link = LOG_LINKS (load_insn); link; link = XEXP (link, 1))
2399 rtx insn1 = XEXP (link, 0);
2401 /* must be a DEF-USE dependence upon non-branch */
2402 if (GET_MODE (link) != VOIDmode
2403 || GET_CODE (insn1) == JUMP_INSN)
2404 continue;
2406 /* must exist a path: region-entry -> ... -> bb_trg -> ... load_insn */
2407 if (INSN_BB (insn1) == bb_src
2408 || (CONTAINING_RGN (INSN_BLOCK (insn1))
2409 != CONTAINING_RGN (BB_TO_BLOCK (bb_src)))
2410 || (!IS_REACHABLE (bb_trg, INSN_BB (insn1))
2411 && !IS_REACHABLE (INSN_BB (insn1), bb_trg)))
2412 continue;
2414 /* now search for the conditional-branch */
2415 if (find_conditional_protection (insn1, bb_src))
2416 return 1;
2418 /* recursive step: search another insn1, "above" current insn1. */
2419 return is_conditionally_protected (insn1, bb_src, bb_trg);
2422 /* the chain does not exsist */
2423 return 0;
2424 } /* is_conditionally_protected */
2426 /* Returns 1 if a clue for "similar load" 'insn2' is found, and hence
2427 load_insn can move speculatively from bb_src to bb_trg. All the
2428 following must hold:
2430 (1) both loads have 1 base register (PFREE_CANDIDATEs).
2431 (2) load_insn and load1 have a def-use dependence upon
2432 the same insn 'insn1'.
2433 (3) either load2 is in bb_trg, or:
2434 - there's only one split-block, and
2435 - load1 is on the escape path, and
2437 From all these we can conclude that the two loads access memory
2438 addresses that differ at most by a constant, and hence if moving
2439 load_insn would cause an exception, it would have been caused by
2440 load2 anyhow. */
2442 static int
2443 is_pfree (load_insn, bb_src, bb_trg)
2444 rtx load_insn;
2445 int bb_src, bb_trg;
2447 rtx back_link;
2448 register candidate *candp = candidate_table + bb_src;
2450 if (candp->split_bbs.nr_members != 1)
2451 /* must have exactly one escape block */
2452 return 0;
2454 for (back_link = LOG_LINKS (load_insn);
2455 back_link; back_link = XEXP (back_link, 1))
2457 rtx insn1 = XEXP (back_link, 0);
2459 if (GET_MODE (back_link) == VOIDmode)
2461 /* found a DEF-USE dependence (insn1, load_insn) */
2462 rtx fore_link;
2464 for (fore_link = INSN_DEPEND (insn1);
2465 fore_link; fore_link = XEXP (fore_link, 1))
2467 rtx insn2 = XEXP (fore_link, 0);
2468 if (GET_MODE (fore_link) == VOIDmode)
2470 /* found a DEF-USE dependence (insn1, insn2) */
2471 if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
2472 /* insn2 not guaranteed to be a 1 base reg load */
2473 continue;
2475 if (INSN_BB (insn2) == bb_trg)
2476 /* insn2 is the similar load, in the target block */
2477 return 1;
2479 if (*(candp->split_bbs.first_member) == INSN_BLOCK (insn2))
2480 /* insn2 is a similar load, in a split-block */
2481 return 1;
2487 /* couldn't find a similar load */
2488 return 0;
2489 } /* is_pfree */
2491 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
2492 as found by analyzing insn's expression. */
2494 static int
2495 may_trap_exp (x, is_store)
2496 rtx x;
2497 int is_store;
2499 enum rtx_code code;
2501 if (x == 0)
2502 return TRAP_FREE;
2503 code = GET_CODE (x);
2504 if (is_store)
2506 if (code == MEM)
2507 return TRAP_RISKY;
2508 else
2509 return TRAP_FREE;
2511 if (code == MEM)
2513 /* The insn uses memory */
2514 /* a volatile load */
2515 if (MEM_VOLATILE_P (x))
2516 return IRISKY;
2517 /* an exception-free load */
2518 if (!may_trap_p (x))
2519 return IFREE;
2520 /* a load with 1 base register, to be further checked */
2521 if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
2522 return PFREE_CANDIDATE;
2523 /* no info on the load, to be further checked */
2524 return PRISKY_CANDIDATE;
2526 else
2528 char *fmt;
2529 int i, insn_class = TRAP_FREE;
2531 /* neither store nor load, check if it may cause a trap */
2532 if (may_trap_p (x))
2533 return TRAP_RISKY;
2534 /* recursive step: walk the insn... */
2535 fmt = GET_RTX_FORMAT (code);
2536 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2538 if (fmt[i] == 'e')
2540 int tmp_class = may_trap_exp (XEXP (x, i), is_store);
2541 insn_class = WORST_CLASS (insn_class, tmp_class);
2543 else if (fmt[i] == 'E')
2545 int j;
2546 for (j = 0; j < XVECLEN (x, i); j++)
2548 int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
2549 insn_class = WORST_CLASS (insn_class, tmp_class);
2550 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
2551 break;
2554 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
2555 break;
2557 return insn_class;
2559 } /* may_trap_exp */
2562 /* Classifies insn for the purpose of verifying that it can be
2563 moved speculatively, by examining it's patterns, returning:
2564 TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
2565 TRAP_FREE: non-load insn.
2566 IFREE: load from a globaly safe location.
2567 IRISKY: volatile load.
2568 PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
2569 being either PFREE or PRISKY. */
2571 static int
2572 haifa_classify_insn (insn)
2573 rtx insn;
2575 rtx pat = PATTERN (insn);
2576 int tmp_class = TRAP_FREE;
2577 int insn_class = TRAP_FREE;
2578 enum rtx_code code;
2580 if (GET_CODE (pat) == PARALLEL)
2582 int i, len = XVECLEN (pat, 0);
2584 for (i = len - 1; i >= 0; i--)
2586 code = GET_CODE (XVECEXP (pat, 0, i));
2587 switch (code)
2589 case CLOBBER:
2590 /* test if it is a 'store' */
2591 tmp_class = may_trap_exp (XEXP (XVECEXP (pat, 0, i), 0), 1);
2592 break;
2593 case SET:
2594 /* test if it is a store */
2595 tmp_class = may_trap_exp (SET_DEST (XVECEXP (pat, 0, i)), 1);
2596 if (tmp_class == TRAP_RISKY)
2597 break;
2598 /* test if it is a load */
2599 tmp_class =
2600 WORST_CLASS (tmp_class,
2601 may_trap_exp (SET_SRC (XVECEXP (pat, 0, i)), 0));
2602 default:;
2604 insn_class = WORST_CLASS (insn_class, tmp_class);
2605 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
2606 break;
2609 else
2611 code = GET_CODE (pat);
2612 switch (code)
2614 case CLOBBER:
2615 /* test if it is a 'store' */
2616 tmp_class = may_trap_exp (XEXP (pat, 0), 1);
2617 break;
2618 case SET:
2619 /* test if it is a store */
2620 tmp_class = may_trap_exp (SET_DEST (pat), 1);
2621 if (tmp_class == TRAP_RISKY)
2622 break;
2623 /* test if it is a load */
2624 tmp_class =
2625 WORST_CLASS (tmp_class,
2626 may_trap_exp (SET_SRC (pat), 0));
2627 default:;
2629 insn_class = tmp_class;
2632 return insn_class;
2634 } /* haifa_classify_insn */
2636 /* Return 1 if load_insn is prisky (i.e. if load_insn is fed by
2637 a load moved speculatively, or if load_insn is protected by
2638 a compare on load_insn's address). */
2640 static int
2641 is_prisky (load_insn, bb_src, bb_trg)
2642 rtx load_insn;
2643 int bb_src, bb_trg;
2645 if (FED_BY_SPEC_LOAD (load_insn))
2646 return 1;
2648 if (LOG_LINKS (load_insn) == NULL)
2649 /* dependence may 'hide' out of the region. */
2650 return 1;
2652 if (is_conditionally_protected (load_insn, bb_src, bb_trg))
2653 return 1;
2655 return 0;
2656 } /* is_prisky */
2658 /* Insn is a candidate to be moved speculatively from bb_src to bb_trg.
2659 Return 1 if insn is exception-free (and the motion is valid)
2660 and 0 otherwise. */
2662 static int
2663 is_exception_free (insn, bb_src, bb_trg)
2664 rtx insn;
2665 int bb_src, bb_trg;
2667 int insn_class = haifa_classify_insn (insn);
2669 /* handle non-load insns */
2670 switch (insn_class)
2672 case TRAP_FREE:
2673 return 1;
2674 case TRAP_RISKY:
2675 return 0;
2676 default:;
2679 /* handle loads */
2680 if (!flag_schedule_speculative_load)
2681 return 0;
2682 IS_LOAD_INSN (insn) = 1;
2683 switch (insn_class)
2685 case IFREE:
2686 return (1);
2687 case IRISKY:
2688 return 0;
2689 case PFREE_CANDIDATE:
2690 if (is_pfree (insn, bb_src, bb_trg))
2691 return 1;
2692 /* don't 'break' here: PFREE-candidate is also PRISKY-candidate */
2693 case PRISKY_CANDIDATE:
2694 if (!flag_schedule_speculative_load_dangerous
2695 || is_prisky (insn, bb_src, bb_trg))
2696 return 0;
2697 break;
2698 default:;
2701 return flag_schedule_speculative_load_dangerous;
2702 } /* is_exception_free */
2705 /* Process an insn's memory dependencies. There are four kinds of
2706 dependencies:
2708 (0) read dependence: read follows read
2709 (1) true dependence: read follows write
2710 (2) anti dependence: write follows read
2711 (3) output dependence: write follows write
2713 We are careful to build only dependencies which actually exist, and
2714 use transitivity to avoid building too many links. */
2716 /* Return the INSN_LIST containing INSN in LIST, or NULL
2717 if LIST does not contain INSN. */
2719 __inline static rtx
2720 find_insn_list (insn, list)
2721 rtx insn;
2722 rtx list;
2724 while (list)
2726 if (XEXP (list, 0) == insn)
2727 return list;
2728 list = XEXP (list, 1);
2730 return 0;
2734 /* Return 1 if the pair (insn, x) is found in (LIST, LIST1), or 0 otherwise. */
2736 __inline static char
2737 find_insn_mem_list (insn, x, list, list1)
2738 rtx insn, x;
2739 rtx list, list1;
2741 while (list)
2743 if (XEXP (list, 0) == insn
2744 && XEXP (list1, 0) == x)
2745 return 1;
2746 list = XEXP (list, 1);
2747 list1 = XEXP (list1, 1);
2749 return 0;
2753 /* Compute the function units used by INSN. This caches the value
2754 returned by function_units_used. A function unit is encoded as the
2755 unit number if the value is non-negative and the compliment of a
2756 mask if the value is negative. A function unit index is the
2757 non-negative encoding. */
2759 __inline static int
2760 insn_unit (insn)
2761 rtx insn;
2763 register int unit = INSN_UNIT (insn);
2765 if (unit == 0)
2767 recog_memoized (insn);
2769 /* A USE insn, or something else we don't need to understand.
2770 We can't pass these directly to function_units_used because it will
2771 trigger a fatal error for unrecognizable insns. */
2772 if (INSN_CODE (insn) < 0)
2773 unit = -1;
2774 else
2776 unit = function_units_used (insn);
2777 /* Increment non-negative values so we can cache zero. */
2778 if (unit >= 0)
2779 unit++;
2781 /* We only cache 16 bits of the result, so if the value is out of
2782 range, don't cache it. */
2783 if (FUNCTION_UNITS_SIZE < HOST_BITS_PER_SHORT
2784 || unit >= 0
2785 || (~unit & ((1 << (HOST_BITS_PER_SHORT - 1)) - 1)) == 0)
2786 INSN_UNIT (insn) = unit;
2788 return (unit > 0 ? unit - 1 : unit);
2791 /* Compute the blockage range for executing INSN on UNIT. This caches
2792 the value returned by the blockage_range_function for the unit.
2793 These values are encoded in an int where the upper half gives the
2794 minimum value and the lower half gives the maximum value. */
2796 __inline static unsigned int
2797 blockage_range (unit, insn)
2798 int unit;
2799 rtx insn;
2801 unsigned int blockage = INSN_BLOCKAGE (insn);
2802 unsigned int range;
2804 if (UNIT_BLOCKED (blockage) != unit + 1)
2806 range = function_units[unit].blockage_range_function (insn);
2807 /* We only cache the blockage range for one unit and then only if
2808 the values fit. */
2809 if (HOST_BITS_PER_INT >= UNIT_BITS + 2 * BLOCKAGE_BITS)
2810 INSN_BLOCKAGE (insn) = ENCODE_BLOCKAGE (unit + 1, range);
2812 else
2813 range = BLOCKAGE_RANGE (blockage);
2815 return range;
2818 /* A vector indexed by function unit instance giving the last insn to use
2819 the unit. The value of the function unit instance index for unit U
2820 instance I is (U + I * FUNCTION_UNITS_SIZE). */
2821 static rtx unit_last_insn[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
2823 /* A vector indexed by function unit instance giving the minimum time when
2824 the unit will unblock based on the maximum blockage cost. */
2825 static int unit_tick[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
2827 /* A vector indexed by function unit number giving the number of insns
2828 that remain to use the unit. */
2829 static int unit_n_insns[FUNCTION_UNITS_SIZE];
2831 /* Reset the function unit state to the null state. */
2833 static void
2834 clear_units ()
2836 bzero ((char *) unit_last_insn, sizeof (unit_last_insn));
2837 bzero ((char *) unit_tick, sizeof (unit_tick));
2838 bzero ((char *) unit_n_insns, sizeof (unit_n_insns));
2841 /* Return the issue-delay of an insn */
2843 __inline static int
2844 insn_issue_delay (insn)
2845 rtx insn;
2847 int i, delay = 0;
2848 int unit = insn_unit (insn);
2850 /* efficiency note: in fact, we are working 'hard' to compute a
2851 value that was available in md file, and is not available in
2852 function_units[] structure. It would be nice to have this
2853 value there, too. */
2854 if (unit >= 0)
2856 if (function_units[unit].blockage_range_function &&
2857 function_units[unit].blockage_function)
2858 delay = function_units[unit].blockage_function (insn, insn);
2860 else
2861 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
2862 if ((unit & 1) != 0 && function_units[i].blockage_range_function
2863 && function_units[i].blockage_function)
2864 delay = MAX (delay, function_units[i].blockage_function (insn, insn));
2866 return delay;
2869 /* Return the actual hazard cost of executing INSN on the unit UNIT,
2870 instance INSTANCE at time CLOCK if the previous actual hazard cost
2871 was COST. */
2873 __inline static int
2874 actual_hazard_this_instance (unit, instance, insn, clock, cost)
2875 int unit, instance, clock, cost;
2876 rtx insn;
2878 int tick = unit_tick[instance]; /* issue time of the last issued insn */
2880 if (tick - clock > cost)
2882 /* The scheduler is operating forward, so unit's last insn is the
2883 executing insn and INSN is the candidate insn. We want a
2884 more exact measure of the blockage if we execute INSN at CLOCK
2885 given when we committed the execution of the unit's last insn.
2887 The blockage value is given by either the unit's max blockage
2888 constant, blockage range function, or blockage function. Use
2889 the most exact form for the given unit. */
2891 if (function_units[unit].blockage_range_function)
2893 if (function_units[unit].blockage_function)
2894 tick += (function_units[unit].blockage_function
2895 (unit_last_insn[instance], insn)
2896 - function_units[unit].max_blockage);
2897 else
2898 tick += ((int) MAX_BLOCKAGE_COST (blockage_range (unit, insn))
2899 - function_units[unit].max_blockage);
2901 if (tick - clock > cost)
2902 cost = tick - clock;
2904 return cost;
2907 /* Record INSN as having begun execution on the units encoded by UNIT at
2908 time CLOCK. */
2910 __inline static void
2911 schedule_unit (unit, insn, clock)
2912 int unit, clock;
2913 rtx insn;
2915 int i;
2917 if (unit >= 0)
2919 int instance = unit;
2920 #if MAX_MULTIPLICITY > 1
2921 /* Find the first free instance of the function unit and use that
2922 one. We assume that one is free. */
2923 for (i = function_units[unit].multiplicity - 1; i > 0; i--)
2925 if (!actual_hazard_this_instance (unit, instance, insn, clock, 0))
2926 break;
2927 instance += FUNCTION_UNITS_SIZE;
2929 #endif
2930 unit_last_insn[instance] = insn;
2931 unit_tick[instance] = (clock + function_units[unit].max_blockage);
2933 else
2934 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
2935 if ((unit & 1) != 0)
2936 schedule_unit (i, insn, clock);
2939 /* Return the actual hazard cost of executing INSN on the units encoded by
2940 UNIT at time CLOCK if the previous actual hazard cost was COST. */
2942 __inline static int
2943 actual_hazard (unit, insn, clock, cost)
2944 int unit, clock, cost;
2945 rtx insn;
2947 int i;
2949 if (unit >= 0)
2951 /* Find the instance of the function unit with the minimum hazard. */
2952 int instance = unit;
2953 int best_cost = actual_hazard_this_instance (unit, instance, insn,
2954 clock, cost);
2955 int this_cost;
2957 #if MAX_MULTIPLICITY > 1
2958 if (best_cost > cost)
2960 for (i = function_units[unit].multiplicity - 1; i > 0; i--)
2962 instance += FUNCTION_UNITS_SIZE;
2963 this_cost = actual_hazard_this_instance (unit, instance, insn,
2964 clock, cost);
2965 if (this_cost < best_cost)
2967 best_cost = this_cost;
2968 if (this_cost <= cost)
2969 break;
2973 #endif
2974 cost = MAX (cost, best_cost);
2976 else
2977 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
2978 if ((unit & 1) != 0)
2979 cost = actual_hazard (i, insn, clock, cost);
2981 return cost;
2984 /* Return the potential hazard cost of executing an instruction on the
2985 units encoded by UNIT if the previous potential hazard cost was COST.
2986 An insn with a large blockage time is chosen in preference to one
2987 with a smaller time; an insn that uses a unit that is more likely
2988 to be used is chosen in preference to one with a unit that is less
2989 used. We are trying to minimize a subsequent actual hazard. */
2991 __inline static int
2992 potential_hazard (unit, insn, cost)
2993 int unit, cost;
2994 rtx insn;
2996 int i, ncost;
2997 unsigned int minb, maxb;
2999 if (unit >= 0)
3001 minb = maxb = function_units[unit].max_blockage;
3002 if (maxb > 1)
3004 if (function_units[unit].blockage_range_function)
3006 maxb = minb = blockage_range (unit, insn);
3007 maxb = MAX_BLOCKAGE_COST (maxb);
3008 minb = MIN_BLOCKAGE_COST (minb);
3011 if (maxb > 1)
3013 /* Make the number of instructions left dominate. Make the
3014 minimum delay dominate the maximum delay. If all these
3015 are the same, use the unit number to add an arbitrary
3016 ordering. Other terms can be added. */
3017 ncost = minb * 0x40 + maxb;
3018 ncost *= (unit_n_insns[unit] - 1) * 0x1000 + unit;
3019 if (ncost > cost)
3020 cost = ncost;
3024 else
3025 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
3026 if ((unit & 1) != 0)
3027 cost = potential_hazard (i, insn, cost);
3029 return cost;
3032 /* Compute cost of executing INSN given the dependence LINK on the insn USED.
3033 This is the number of cycles between instruction issue and
3034 instruction results. */
3036 __inline static int
3037 insn_cost (insn, link, used)
3038 rtx insn, link, used;
3040 register int cost = INSN_COST (insn);
3042 if (cost == 0)
3044 recog_memoized (insn);
3046 /* A USE insn, or something else we don't need to understand.
3047 We can't pass these directly to result_ready_cost because it will
3048 trigger a fatal error for unrecognizable insns. */
3049 if (INSN_CODE (insn) < 0)
3051 INSN_COST (insn) = 1;
3052 return 1;
3054 else
3056 cost = result_ready_cost (insn);
3058 if (cost < 1)
3059 cost = 1;
3061 INSN_COST (insn) = cost;
3065 /* in this case estimate cost without caring how insn is used. */
3066 if (link == 0 && used == 0)
3067 return cost;
3069 /* A USE insn should never require the value used to be computed. This
3070 allows the computation of a function's result and parameter values to
3071 overlap the return and call. */
3072 recog_memoized (used);
3073 if (INSN_CODE (used) < 0)
3074 LINK_COST_FREE (link) = 1;
3076 /* If some dependencies vary the cost, compute the adjustment. Most
3077 commonly, the adjustment is complete: either the cost is ignored
3078 (in the case of an output- or anti-dependence), or the cost is
3079 unchanged. These values are cached in the link as LINK_COST_FREE
3080 and LINK_COST_ZERO. */
3082 if (LINK_COST_FREE (link))
3083 cost = 1;
3084 #ifdef ADJUST_COST
3085 else if (!LINK_COST_ZERO (link))
3087 int ncost = cost;
3089 ADJUST_COST (used, link, insn, ncost);
3090 if (ncost <= 1)
3091 LINK_COST_FREE (link) = ncost = 1;
3092 if (cost == ncost)
3093 LINK_COST_ZERO (link) = 1;
3094 cost = ncost;
3096 #endif
3097 return cost;
3100 /* Compute the priority number for INSN. */
3102 static int
3103 priority (insn)
3104 rtx insn;
3106 int this_priority;
3107 rtx link;
3109 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
3110 return 0;
3112 if ((this_priority = INSN_PRIORITY (insn)) == 0)
3114 if (INSN_DEPEND (insn) == 0)
3115 this_priority = insn_cost (insn, 0, 0);
3116 else
3117 for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
3119 rtx next;
3120 int next_priority;
3122 if (RTX_INTEGRATED_P (link))
3123 continue;
3125 next = XEXP (link, 0);
3127 /* critical path is meaningful in block boundaries only */
3128 if (INSN_BLOCK (next) != INSN_BLOCK (insn))
3129 continue;
3131 next_priority = insn_cost (insn, link, next) + priority (next);
3132 if (next_priority > this_priority)
3133 this_priority = next_priority;
3135 INSN_PRIORITY (insn) = this_priority;
3137 return this_priority;
3141 /* Remove all INSN_LISTs and EXPR_LISTs from the pending lists and add
3142 them to the unused_*_list variables, so that they can be reused. */
3144 static void
3145 free_pending_lists ()
3147 if (current_nr_blocks <= 1)
3149 free_list (&pending_read_insns, &unused_insn_list);
3150 free_list (&pending_write_insns, &unused_insn_list);
3151 free_list (&pending_read_mems, &unused_expr_list);
3152 free_list (&pending_write_mems, &unused_expr_list);
3154 else
3156 /* interblock scheduling */
3157 int bb;
3159 for (bb = 0; bb < current_nr_blocks; bb++)
3161 free_list (&bb_pending_read_insns[bb], &unused_insn_list);
3162 free_list (&bb_pending_write_insns[bb], &unused_insn_list);
3163 free_list (&bb_pending_read_mems[bb], &unused_expr_list);
3164 free_list (&bb_pending_write_mems[bb], &unused_expr_list);
3169 /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
3170 The MEM is a memory reference contained within INSN, which we are saving
3171 so that we can do memory aliasing on it. */
3173 static void
3174 add_insn_mem_dependence (insn_list, mem_list, insn, mem)
3175 rtx *insn_list, *mem_list, insn, mem;
3177 register rtx link;
3179 link = alloc_INSN_LIST (insn, *insn_list);
3180 *insn_list = link;
3182 link = alloc_EXPR_LIST (VOIDmode, mem, *mem_list);
3183 *mem_list = link;
3185 pending_lists_length++;
3189 /* Make a dependency between every memory reference on the pending lists
3190 and INSN, thus flushing the pending lists. If ONLY_WRITE, don't flush
3191 the read list. */
3193 static void
3194 flush_pending_lists (insn, only_write)
3195 rtx insn;
3196 int only_write;
3198 rtx u;
3199 rtx link;
3201 while (pending_read_insns && ! only_write)
3203 add_dependence (insn, XEXP (pending_read_insns, 0), REG_DEP_ANTI);
3205 link = pending_read_insns;
3206 pending_read_insns = XEXP (pending_read_insns, 1);
3207 XEXP (link, 1) = unused_insn_list;
3208 unused_insn_list = link;
3210 link = pending_read_mems;
3211 pending_read_mems = XEXP (pending_read_mems, 1);
3212 XEXP (link, 1) = unused_expr_list;
3213 unused_expr_list = link;
3215 while (pending_write_insns)
3217 add_dependence (insn, XEXP (pending_write_insns, 0), REG_DEP_ANTI);
3219 link = pending_write_insns;
3220 pending_write_insns = XEXP (pending_write_insns, 1);
3221 XEXP (link, 1) = unused_insn_list;
3222 unused_insn_list = link;
3224 link = pending_write_mems;
3225 pending_write_mems = XEXP (pending_write_mems, 1);
3226 XEXP (link, 1) = unused_expr_list;
3227 unused_expr_list = link;
3229 pending_lists_length = 0;
3231 /* last_pending_memory_flush is now a list of insns */
3232 for (u = last_pending_memory_flush; u; u = XEXP (u, 1))
3233 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3235 free_list (&last_pending_memory_flush, &unused_insn_list);
3236 last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
3239 /* Analyze a single SET or CLOBBER rtx, X, creating all dependencies generated
3240 by the write to the destination of X, and reads of everything mentioned. */
3242 static void
3243 sched_analyze_1 (x, insn)
3244 rtx x;
3245 rtx insn;
3247 register int regno;
3248 register rtx dest = SET_DEST (x);
3250 if (dest == 0)
3251 return;
3253 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
3254 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
3256 if (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
3258 /* The second and third arguments are values read by this insn. */
3259 sched_analyze_2 (XEXP (dest, 1), insn);
3260 sched_analyze_2 (XEXP (dest, 2), insn);
3262 dest = SUBREG_REG (dest);
3265 if (GET_CODE (dest) == REG)
3267 register int i;
3269 regno = REGNO (dest);
3271 /* A hard reg in a wide mode may really be multiple registers.
3272 If so, mark all of them just like the first. */
3273 if (regno < FIRST_PSEUDO_REGISTER)
3275 i = HARD_REGNO_NREGS (regno, GET_MODE (dest));
3276 while (--i >= 0)
3278 rtx u;
3280 for (u = reg_last_uses[regno + i]; u; u = XEXP (u, 1))
3281 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3282 reg_last_uses[regno + i] = 0;
3284 for (u = reg_last_sets[regno + i]; u; u = XEXP (u, 1))
3285 add_dependence (insn, XEXP (u, 0), REG_DEP_OUTPUT);
3287 SET_REGNO_REG_SET (reg_pending_sets, regno + i);
3289 if ((call_used_regs[regno + i] || global_regs[regno + i]))
3290 /* Function calls clobber all call_used regs. */
3291 for (u = last_function_call; u; u = XEXP (u, 1))
3292 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3295 else
3297 rtx u;
3299 for (u = reg_last_uses[regno]; u; u = XEXP (u, 1))
3300 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3301 reg_last_uses[regno] = 0;
3303 for (u = reg_last_sets[regno]; u; u = XEXP (u, 1))
3304 add_dependence (insn, XEXP (u, 0), REG_DEP_OUTPUT);
3306 SET_REGNO_REG_SET (reg_pending_sets, regno);
3308 /* Pseudos that are REG_EQUIV to something may be replaced
3309 by that during reloading. We need only add dependencies for
3310 the address in the REG_EQUIV note. */
3311 if (!reload_completed
3312 && reg_known_equiv_p[regno]
3313 && GET_CODE (reg_known_value[regno]) == MEM)
3314 sched_analyze_2 (XEXP (reg_known_value[regno], 0), insn);
3316 /* Don't let it cross a call after scheduling if it doesn't
3317 already cross one. */
3319 if (REG_N_CALLS_CROSSED (regno) == 0)
3320 for (u = last_function_call; u; u = XEXP (u, 1))
3321 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3324 else if (GET_CODE (dest) == MEM)
3326 /* Writing memory. */
3328 if (pending_lists_length > 32)
3330 /* Flush all pending reads and writes to prevent the pending lists
3331 from getting any larger. Insn scheduling runs too slowly when
3332 these lists get long. The number 32 was chosen because it
3333 seems like a reasonable number. When compiling GCC with itself,
3334 this flush occurs 8 times for sparc, and 10 times for m88k using
3335 the number 32. */
3336 flush_pending_lists (insn, 0);
3338 else
3340 rtx u;
3341 rtx pending, pending_mem;
3343 pending = pending_read_insns;
3344 pending_mem = pending_read_mems;
3345 while (pending)
3347 /* If a dependency already exists, don't create a new one. */
3348 if (!find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
3349 if (anti_dependence (XEXP (pending_mem, 0), dest))
3350 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
3352 pending = XEXP (pending, 1);
3353 pending_mem = XEXP (pending_mem, 1);
3356 pending = pending_write_insns;
3357 pending_mem = pending_write_mems;
3358 while (pending)
3360 /* If a dependency already exists, don't create a new one. */
3361 if (!find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
3362 if (output_dependence (XEXP (pending_mem, 0), dest))
3363 add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
3365 pending = XEXP (pending, 1);
3366 pending_mem = XEXP (pending_mem, 1);
3369 for (u = last_pending_memory_flush; u; u = XEXP (u, 1))
3370 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3372 add_insn_mem_dependence (&pending_write_insns, &pending_write_mems,
3373 insn, dest);
3375 sched_analyze_2 (XEXP (dest, 0), insn);
3378 /* Analyze reads. */
3379 if (GET_CODE (x) == SET)
3380 sched_analyze_2 (SET_SRC (x), insn);
3383 /* Analyze the uses of memory and registers in rtx X in INSN. */
3385 static void
3386 sched_analyze_2 (x, insn)
3387 rtx x;
3388 rtx insn;
3390 register int i;
3391 register int j;
3392 register enum rtx_code code;
3393 register char *fmt;
3395 if (x == 0)
3396 return;
3398 code = GET_CODE (x);
3400 switch (code)
3402 case CONST_INT:
3403 case CONST_DOUBLE:
3404 case SYMBOL_REF:
3405 case CONST:
3406 case LABEL_REF:
3407 /* Ignore constants. Note that we must handle CONST_DOUBLE here
3408 because it may have a cc0_rtx in its CONST_DOUBLE_CHAIN field, but
3409 this does not mean that this insn is using cc0. */
3410 return;
3412 #ifdef HAVE_cc0
3413 case CC0:
3415 rtx link, prev;
3417 /* User of CC0 depends on immediately preceding insn. */
3418 SCHED_GROUP_P (insn) = 1;
3420 /* There may be a note before this insn now, but all notes will
3421 be removed before we actually try to schedule the insns, so
3422 it won't cause a problem later. We must avoid it here though. */
3423 prev = prev_nonnote_insn (insn);
3425 /* Make a copy of all dependencies on the immediately previous insn,
3426 and add to this insn. This is so that all the dependencies will
3427 apply to the group. Remove an explicit dependence on this insn
3428 as SCHED_GROUP_P now represents it. */
3430 if (find_insn_list (prev, LOG_LINKS (insn)))
3431 remove_dependence (insn, prev);
3433 for (link = LOG_LINKS (prev); link; link = XEXP (link, 1))
3434 add_dependence (insn, XEXP (link, 0), REG_NOTE_KIND (link));
3436 return;
3438 #endif
3440 case REG:
3442 rtx u;
3443 int regno = REGNO (x);
3444 if (regno < FIRST_PSEUDO_REGISTER)
3446 int i;
3448 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
3449 while (--i >= 0)
3451 reg_last_uses[regno + i]
3452 = alloc_INSN_LIST (insn, reg_last_uses[regno + i]);
3454 for (u = reg_last_sets[regno + i]; u; u = XEXP (u, 1))
3455 add_dependence (insn, XEXP (u, 0), 0);
3457 if ((call_used_regs[regno + i] || global_regs[regno + i]))
3458 /* Function calls clobber all call_used regs. */
3459 for (u = last_function_call; u; u = XEXP (u, 1))
3460 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3463 else
3465 reg_last_uses[regno] = alloc_INSN_LIST (insn, reg_last_uses[regno]);
3467 for (u = reg_last_sets[regno]; u; u = XEXP (u, 1))
3468 add_dependence (insn, XEXP (u, 0), 0);
3470 /* Pseudos that are REG_EQUIV to something may be replaced
3471 by that during reloading. We need only add dependencies for
3472 the address in the REG_EQUIV note. */
3473 if (!reload_completed
3474 && reg_known_equiv_p[regno]
3475 && GET_CODE (reg_known_value[regno]) == MEM)
3476 sched_analyze_2 (XEXP (reg_known_value[regno], 0), insn);
3478 /* If the register does not already cross any calls, then add this
3479 insn to the sched_before_next_call list so that it will still
3480 not cross calls after scheduling. */
3481 if (REG_N_CALLS_CROSSED (regno) == 0)
3482 add_dependence (sched_before_next_call, insn, REG_DEP_ANTI);
3484 return;
3487 case MEM:
3489 /* Reading memory. */
3490 rtx u;
3491 rtx pending, pending_mem;
3493 pending = pending_read_insns;
3494 pending_mem = pending_read_mems;
3495 while (pending)
3497 /* If a dependency already exists, don't create a new one. */
3498 if (!find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
3499 if (read_dependence (XEXP (pending_mem, 0), x))
3500 add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
3502 pending = XEXP (pending, 1);
3503 pending_mem = XEXP (pending_mem, 1);
3506 pending = pending_write_insns;
3507 pending_mem = pending_write_mems;
3508 while (pending)
3510 /* If a dependency already exists, don't create a new one. */
3511 if (!find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
3512 if (true_dependence (XEXP (pending_mem, 0), VOIDmode,
3513 x, rtx_varies_p))
3514 add_dependence (insn, XEXP (pending, 0), 0);
3516 pending = XEXP (pending, 1);
3517 pending_mem = XEXP (pending_mem, 1);
3520 for (u = last_pending_memory_flush; u; u = XEXP (u, 1))
3521 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3523 /* Always add these dependencies to pending_reads, since
3524 this insn may be followed by a write. */
3525 add_insn_mem_dependence (&pending_read_insns, &pending_read_mems,
3526 insn, x);
3528 /* Take advantage of tail recursion here. */
3529 sched_analyze_2 (XEXP (x, 0), insn);
3530 return;
3533 case ASM_OPERANDS:
3534 case ASM_INPUT:
3535 case UNSPEC_VOLATILE:
3536 case TRAP_IF:
3538 rtx u;
3540 /* Traditional and volatile asm instructions must be considered to use
3541 and clobber all hard registers, all pseudo-registers and all of
3542 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
3544 Consider for instance a volatile asm that changes the fpu rounding
3545 mode. An insn should not be moved across this even if it only uses
3546 pseudo-regs because it might give an incorrectly rounded result. */
3547 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
3549 int max_reg = max_reg_num ();
3550 for (i = 0; i < max_reg; i++)
3552 for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
3553 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3554 reg_last_uses[i] = 0;
3556 /* reg_last_sets[r] is now a list of insns */
3557 for (u = reg_last_sets[i]; u; u = XEXP (u, 1))
3558 add_dependence (insn, XEXP (u, 0), 0);
3560 reg_pending_sets_all = 1;
3562 flush_pending_lists (insn, 0);
3565 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
3566 We can not just fall through here since then we would be confused
3567 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
3568 traditional asms unlike their normal usage. */
3570 if (code == ASM_OPERANDS)
3572 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
3573 sched_analyze_2 (ASM_OPERANDS_INPUT (x, j), insn);
3574 return;
3576 break;
3579 case PRE_DEC:
3580 case POST_DEC:
3581 case PRE_INC:
3582 case POST_INC:
3583 /* These both read and modify the result. We must handle them as writes
3584 to get proper dependencies for following instructions. We must handle
3585 them as reads to get proper dependencies from this to previous
3586 instructions. Thus we need to pass them to both sched_analyze_1
3587 and sched_analyze_2. We must call sched_analyze_2 first in order
3588 to get the proper antecedent for the read. */
3589 sched_analyze_2 (XEXP (x, 0), insn);
3590 sched_analyze_1 (x, insn);
3591 return;
3593 default:
3594 break;
3597 /* Other cases: walk the insn. */
3598 fmt = GET_RTX_FORMAT (code);
3599 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3601 if (fmt[i] == 'e')
3602 sched_analyze_2 (XEXP (x, i), insn);
3603 else if (fmt[i] == 'E')
3604 for (j = 0; j < XVECLEN (x, i); j++)
3605 sched_analyze_2 (XVECEXP (x, i, j), insn);
3609 /* Analyze an INSN with pattern X to find all dependencies. */
3611 static void
3612 sched_analyze_insn (x, insn, loop_notes)
3613 rtx x, insn;
3614 rtx loop_notes;
3616 register RTX_CODE code = GET_CODE (x);
3617 rtx link;
3618 int maxreg = max_reg_num ();
3619 int i;
3621 if (code == SET || code == CLOBBER)
3622 sched_analyze_1 (x, insn);
3623 else if (code == PARALLEL)
3625 register int i;
3626 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3628 code = GET_CODE (XVECEXP (x, 0, i));
3629 if (code == SET || code == CLOBBER)
3630 sched_analyze_1 (XVECEXP (x, 0, i), insn);
3631 else
3632 sched_analyze_2 (XVECEXP (x, 0, i), insn);
3635 else
3636 sched_analyze_2 (x, insn);
3638 /* Mark registers CLOBBERED or used by called function. */
3639 if (GET_CODE (insn) == CALL_INSN)
3640 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
3642 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
3643 sched_analyze_1 (XEXP (link, 0), insn);
3644 else
3645 sched_analyze_2 (XEXP (link, 0), insn);
3648 /* If there is a {LOOP,EHREGION}_{BEG,END} note in the middle of a basic block, then
3649 we must be sure that no instructions are scheduled across it.
3650 Otherwise, the reg_n_refs info (which depends on loop_depth) would
3651 become incorrect. */
3653 if (loop_notes)
3655 int max_reg = max_reg_num ();
3656 rtx link;
3658 for (i = 0; i < max_reg; i++)
3660 rtx u;
3661 for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
3662 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3663 reg_last_uses[i] = 0;
3665 /* reg_last_sets[r] is now a list of insns */
3666 for (u = reg_last_sets[i]; u; u = XEXP (u, 1))
3667 add_dependence (insn, XEXP (u, 0), 0);
3669 reg_pending_sets_all = 1;
3671 flush_pending_lists (insn, 0);
3673 link = loop_notes;
3674 while (XEXP (link, 1))
3675 link = XEXP (link, 1);
3676 XEXP (link, 1) = REG_NOTES (insn);
3677 REG_NOTES (insn) = loop_notes;
3680 /* After reload, it is possible for an instruction to have a REG_DEAD note
3681 for a register that actually dies a few instructions earlier. For
3682 example, this can happen with SECONDARY_MEMORY_NEEDED reloads.
3683 In this case, we must consider the insn to use the register mentioned
3684 in the REG_DEAD note. Otherwise, we may accidentally move this insn
3685 after another insn that sets the register, thus getting obviously invalid
3686 rtl. This confuses reorg which believes that REG_DEAD notes are still
3687 meaningful.
3689 ??? We would get better code if we fixed reload to put the REG_DEAD
3690 notes in the right places, but that may not be worth the effort. */
3692 if (reload_completed)
3694 rtx note;
3696 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3697 if (REG_NOTE_KIND (note) == REG_DEAD)
3698 sched_analyze_2 (XEXP (note, 0), insn);
3701 EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i,
3703 /* reg_last_sets[r] is now a list of insns */
3704 free_list (&reg_last_sets[i], &unused_insn_list);
3705 reg_last_sets[i]
3706 = alloc_INSN_LIST (insn, NULL_RTX);
3708 CLEAR_REG_SET (reg_pending_sets);
3710 if (reg_pending_sets_all)
3712 for (i = 0; i < maxreg; i++)
3714 /* reg_last_sets[r] is now a list of insns */
3715 free_list (&reg_last_sets[i], &unused_insn_list);
3716 reg_last_sets[i] = alloc_INSN_LIST (insn, NULL_RTX);
3719 reg_pending_sets_all = 0;
3722 /* Handle function calls and function returns created by the epilogue
3723 threading code. */
3724 if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN)
3726 rtx dep_insn;
3727 rtx prev_dep_insn;
3729 /* When scheduling instructions, we make sure calls don't lose their
3730 accompanying USE insns by depending them one on another in order.
3732 Also, we must do the same thing for returns created by the epilogue
3733 threading code. Note this code works only in this special case,
3734 because other passes make no guarantee that they will never emit
3735 an instruction between a USE and a RETURN. There is such a guarantee
3736 for USE instructions immediately before a call. */
3738 prev_dep_insn = insn;
3739 dep_insn = PREV_INSN (insn);
3740 while (GET_CODE (dep_insn) == INSN
3741 && GET_CODE (PATTERN (dep_insn)) == USE
3742 && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == REG)
3744 SCHED_GROUP_P (prev_dep_insn) = 1;
3746 /* Make a copy of all dependencies on dep_insn, and add to insn.
3747 This is so that all of the dependencies will apply to the
3748 group. */
3750 for (link = LOG_LINKS (dep_insn); link; link = XEXP (link, 1))
3751 add_dependence (insn, XEXP (link, 0), REG_NOTE_KIND (link));
3753 prev_dep_insn = dep_insn;
3754 dep_insn = PREV_INSN (dep_insn);
3759 /* Analyze every insn between HEAD and TAIL inclusive, creating LOG_LINKS
3760 for every dependency. */
3762 static void
3763 sched_analyze (head, tail)
3764 rtx head, tail;
3766 register rtx insn;
3767 register rtx u;
3768 rtx loop_notes = 0;
3770 for (insn = head;; insn = NEXT_INSN (insn))
3772 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
3774 sched_analyze_insn (PATTERN (insn), insn, loop_notes);
3775 loop_notes = 0;
3777 else if (GET_CODE (insn) == CALL_INSN)
3779 rtx x;
3780 register int i;
3782 CANT_MOVE (insn) = 1;
3784 /* Any instruction using a hard register which may get clobbered
3785 by a call needs to be marked as dependent on this call.
3786 This prevents a use of a hard return reg from being moved
3787 past a void call (i.e. it does not explicitly set the hard
3788 return reg). */
3790 /* If this call is followed by a NOTE_INSN_SETJMP, then assume that
3791 all registers, not just hard registers, may be clobbered by this
3792 call. */
3794 /* Insn, being a CALL_INSN, magically depends on
3795 `last_function_call' already. */
3797 if (NEXT_INSN (insn) && GET_CODE (NEXT_INSN (insn)) == NOTE
3798 && NOTE_LINE_NUMBER (NEXT_INSN (insn)) == NOTE_INSN_SETJMP)
3800 int max_reg = max_reg_num ();
3801 for (i = 0; i < max_reg; i++)
3803 for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
3804 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3806 reg_last_uses[i] = 0;
3808 /* reg_last_sets[r] is now a list of insns */
3809 for (u = reg_last_sets[i]; u; u = XEXP (u, 1))
3810 add_dependence (insn, XEXP (u, 0), 0);
3812 reg_pending_sets_all = 1;
3814 /* Add a pair of fake REG_NOTE which we will later
3815 convert back into a NOTE_INSN_SETJMP note. See
3816 reemit_notes for why we use a pair of NOTEs. */
3817 REG_NOTES (insn) = alloc_EXPR_LIST (REG_DEAD,
3818 GEN_INT (0),
3819 REG_NOTES (insn));
3820 REG_NOTES (insn) = alloc_EXPR_LIST (REG_DEAD,
3821 GEN_INT (NOTE_INSN_SETJMP),
3822 REG_NOTES (insn));
3824 else
3826 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3827 if (call_used_regs[i] || global_regs[i])
3829 for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
3830 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3831 reg_last_uses[i] = 0;
3833 /* reg_last_sets[r] is now a list of insns */
3834 for (u = reg_last_sets[i]; u; u = XEXP (u, 1))
3835 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
3837 SET_REGNO_REG_SET (reg_pending_sets, i);
3841 /* For each insn which shouldn't cross a call, add a dependence
3842 between that insn and this call insn. */
3843 x = LOG_LINKS (sched_before_next_call);
3844 while (x)
3846 add_dependence (insn, XEXP (x, 0), REG_DEP_ANTI);
3847 x = XEXP (x, 1);
3849 LOG_LINKS (sched_before_next_call) = 0;
3851 sched_analyze_insn (PATTERN (insn), insn, loop_notes);
3852 loop_notes = 0;
3854 /* In the absence of interprocedural alias analysis, we must flush
3855 all pending reads and writes, and start new dependencies starting
3856 from here. But only flush writes for constant calls (which may
3857 be passed a pointer to something we haven't written yet). */
3858 flush_pending_lists (insn, CONST_CALL_P (insn));
3860 /* Depend this function call (actually, the user of this
3861 function call) on all hard register clobberage. */
3863 /* last_function_call is now a list of insns */
3864 free_list(&last_function_call, &unused_insn_list);
3865 last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
3868 /* See comments on reemit_notes as to why we do this. */
3869 else if (GET_CODE (insn) == NOTE
3870 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
3871 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
3872 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
3873 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END
3874 || (NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP
3875 && GET_CODE (PREV_INSN (insn)) != CALL_INSN)))
3877 loop_notes = alloc_EXPR_LIST (REG_DEAD,
3878 GEN_INT (NOTE_BLOCK_NUMBER (insn)),
3879 loop_notes);
3880 loop_notes = alloc_EXPR_LIST (REG_DEAD,
3881 GEN_INT (NOTE_LINE_NUMBER (insn)),
3882 loop_notes);
3883 CONST_CALL_P (loop_notes) = CONST_CALL_P (insn);
3886 if (insn == tail)
3887 return;
3889 abort ();
3892 /* Called when we see a set of a register. If death is true, then we are
3893 scanning backwards. Mark that register as unborn. If nobody says
3894 otherwise, that is how things will remain. If death is false, then we
3895 are scanning forwards. Mark that register as being born. */
3897 static void
3898 sched_note_set (x, death)
3899 rtx x;
3900 int death;
3902 register int regno;
3903 register rtx reg = SET_DEST (x);
3904 int subreg_p = 0;
3906 if (reg == 0)
3907 return;
3909 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == STRICT_LOW_PART
3910 || GET_CODE (reg) == SIGN_EXTRACT || GET_CODE (reg) == ZERO_EXTRACT)
3912 /* Must treat modification of just one hardware register of a multi-reg
3913 value or just a byte field of a register exactly the same way that
3914 mark_set_1 in flow.c does, i.e. anything except a paradoxical subreg
3915 does not kill the entire register. */
3916 if (GET_CODE (reg) != SUBREG
3917 || REG_SIZE (SUBREG_REG (reg)) > REG_SIZE (reg))
3918 subreg_p = 1;
3920 reg = SUBREG_REG (reg);
3923 if (GET_CODE (reg) != REG)
3924 return;
3926 /* Global registers are always live, so the code below does not apply
3927 to them. */
3929 regno = REGNO (reg);
3930 if (regno >= FIRST_PSEUDO_REGISTER || !global_regs[regno])
3932 if (death)
3934 /* If we only set part of the register, then this set does not
3935 kill it. */
3936 if (subreg_p)
3937 return;
3939 /* Try killing this register. */
3940 if (regno < FIRST_PSEUDO_REGISTER)
3942 int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
3943 while (--j >= 0)
3945 CLEAR_REGNO_REG_SET (bb_live_regs, regno + j);
3948 else
3950 /* Recompute REG_BASIC_BLOCK as we update all the other
3951 dataflow information. */
3952 if (sched_reg_basic_block[regno] == REG_BLOCK_UNKNOWN)
3953 sched_reg_basic_block[regno] = current_block_num;
3954 else if (sched_reg_basic_block[regno] != current_block_num)
3955 sched_reg_basic_block[regno] = REG_BLOCK_GLOBAL;
3957 CLEAR_REGNO_REG_SET (bb_live_regs, regno);
3960 else
3962 /* Make the register live again. */
3963 if (regno < FIRST_PSEUDO_REGISTER)
3965 int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
3966 while (--j >= 0)
3968 SET_REGNO_REG_SET (bb_live_regs, regno + j);
3971 else
3973 SET_REGNO_REG_SET (bb_live_regs, regno);
3979 /* Macros and functions for keeping the priority queue sorted, and
3980 dealing with queueing and dequeueing of instructions. */
3982 #define SCHED_SORT(READY, N_READY) \
3983 do { if ((N_READY) == 2) \
3984 swap_sort (READY, N_READY); \
3985 else if ((N_READY) > 2) \
3986 qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); } \
3987 while (0)
3989 /* Returns a positive value if x is preferred; returns a negative value if
3990 y is preferred. Should never return 0, since that will make the sort
3991 unstable. */
3993 static int
3994 rank_for_schedule (x, y)
3995 const GENERIC_PTR x;
3996 const GENERIC_PTR y;
3998 rtx tmp = *(rtx *)y;
3999 rtx tmp2 = *(rtx *)x;
4000 rtx link;
4001 int tmp_class, tmp2_class;
4002 int val, priority_val, spec_val, prob_val, weight_val;
4005 /* prefer insn with higher priority */
4006 priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
4007 if (priority_val)
4008 return priority_val;
4010 /* prefer an insn with smaller contribution to registers-pressure */
4011 if (!reload_completed &&
4012 (weight_val = INSN_REG_WEIGHT (tmp) - INSN_REG_WEIGHT (tmp2)))
4013 return (weight_val);
4015 /* some comparison make sense in interblock scheduling only */
4016 if (INSN_BB (tmp) != INSN_BB (tmp2))
4018 /* prefer an inblock motion on an interblock motion */
4019 if ((INSN_BB (tmp2) == target_bb) && (INSN_BB (tmp) != target_bb))
4020 return 1;
4021 if ((INSN_BB (tmp) == target_bb) && (INSN_BB (tmp2) != target_bb))
4022 return -1;
4024 /* prefer a useful motion on a speculative one */
4025 if ((spec_val = IS_SPECULATIVE_INSN (tmp) - IS_SPECULATIVE_INSN (tmp2)))
4026 return (spec_val);
4028 /* prefer a more probable (speculative) insn */
4029 prob_val = INSN_PROBABILITY (tmp2) - INSN_PROBABILITY (tmp);
4030 if (prob_val)
4031 return (prob_val);
4034 /* compare insns based on their relation to the last-scheduled-insn */
4035 if (last_scheduled_insn)
4037 /* Classify the instructions into three classes:
4038 1) Data dependent on last schedule insn.
4039 2) Anti/Output dependent on last scheduled insn.
4040 3) Independent of last scheduled insn, or has latency of one.
4041 Choose the insn from the highest numbered class if different. */
4042 link = find_insn_list (tmp, INSN_DEPEND (last_scheduled_insn));
4043 if (link == 0 || insn_cost (last_scheduled_insn, link, tmp) == 1)
4044 tmp_class = 3;
4045 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
4046 tmp_class = 1;
4047 else
4048 tmp_class = 2;
4050 link = find_insn_list (tmp2, INSN_DEPEND (last_scheduled_insn));
4051 if (link == 0 || insn_cost (last_scheduled_insn, link, tmp2) == 1)
4052 tmp2_class = 3;
4053 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
4054 tmp2_class = 1;
4055 else
4056 tmp2_class = 2;
4058 if ((val = tmp2_class - tmp_class))
4059 return val;
4062 /* If insns are equally good, sort by INSN_LUID (original insn order),
4063 so that we make the sort stable. This minimizes instruction movement,
4064 thus minimizing sched's effect on debugging and cross-jumping. */
4065 return INSN_LUID (tmp) - INSN_LUID (tmp2);
4068 /* Resort the array A in which only element at index N may be out of order. */
4070 __inline static void
4071 swap_sort (a, n)
4072 rtx *a;
4073 int n;
4075 rtx insn = a[n - 1];
4076 int i = n - 2;
4078 while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
4080 a[i + 1] = a[i];
4081 i -= 1;
4083 a[i + 1] = insn;
4086 static int max_priority;
4088 /* Add INSN to the insn queue so that it can be executed at least
4089 N_CYCLES after the currently executing insn. Preserve insns
4090 chain for debugging purposes. */
4092 __inline static void
4093 queue_insn (insn, n_cycles)
4094 rtx insn;
4095 int n_cycles;
4097 int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
4098 rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
4099 insn_queue[next_q] = link;
4100 q_size += 1;
4102 if (sched_verbose >= 2)
4104 fprintf (dump, ";;\t\tReady-->Q: insn %d: ", INSN_UID (insn));
4106 if (INSN_BB (insn) != target_bb)
4107 fprintf (dump, "(b%d) ", INSN_BLOCK (insn));
4109 fprintf (dump, "queued for %d cycles.\n", n_cycles);
4114 /* Return nonzero if PAT is the pattern of an insn which makes a
4115 register live. */
4117 __inline static int
4118 birthing_insn_p (pat)
4119 rtx pat;
4121 int j;
4123 if (reload_completed == 1)
4124 return 0;
4126 if (GET_CODE (pat) == SET
4127 && GET_CODE (SET_DEST (pat)) == REG)
4129 rtx dest = SET_DEST (pat);
4130 int i = REGNO (dest);
4132 /* It would be more accurate to use refers_to_regno_p or
4133 reg_mentioned_p to determine when the dest is not live before this
4134 insn. */
4136 if (REGNO_REG_SET_P (bb_live_regs, i))
4137 return (REG_N_SETS (i) == 1);
4139 return 0;
4141 if (GET_CODE (pat) == PARALLEL)
4143 for (j = 0; j < XVECLEN (pat, 0); j++)
4144 if (birthing_insn_p (XVECEXP (pat, 0, j)))
4145 return 1;
4147 return 0;
4150 /* PREV is an insn that is ready to execute. Adjust its priority if that
4151 will help shorten register lifetimes. */
4153 __inline static void
4154 adjust_priority (prev)
4155 rtx prev;
4157 /* Trying to shorten register lives after reload has completed
4158 is useless and wrong. It gives inaccurate schedules. */
4159 if (reload_completed == 0)
4161 rtx note;
4162 int n_deaths = 0;
4164 /* ??? This code has no effect, because REG_DEAD notes are removed
4165 before we ever get here. */
4166 for (note = REG_NOTES (prev); note; note = XEXP (note, 1))
4167 if (REG_NOTE_KIND (note) == REG_DEAD)
4168 n_deaths += 1;
4170 /* Defer scheduling insns which kill registers, since that
4171 shortens register lives. Prefer scheduling insns which
4172 make registers live for the same reason. */
4173 switch (n_deaths)
4175 default:
4176 INSN_PRIORITY (prev) >>= 3;
4177 break;
4178 case 3:
4179 INSN_PRIORITY (prev) >>= 2;
4180 break;
4181 case 2:
4182 case 1:
4183 INSN_PRIORITY (prev) >>= 1;
4184 break;
4185 case 0:
4186 if (birthing_insn_p (PATTERN (prev)))
4188 int max = max_priority;
4190 if (max > INSN_PRIORITY (prev))
4191 INSN_PRIORITY (prev) = max;
4193 break;
4195 #ifdef ADJUST_PRIORITY
4196 ADJUST_PRIORITY (prev);
4197 #endif
4201 /* INSN is the "currently executing insn". Launch each insn which was
4202 waiting on INSN. READY is a vector of insns which are ready to fire.
4203 N_READY is the number of elements in READY. CLOCK is the current
4204 cycle. */
4206 static int
4207 schedule_insn (insn, ready, n_ready, clock)
4208 rtx insn;
4209 rtx *ready;
4210 int n_ready;
4211 int clock;
4213 rtx link;
4214 int unit;
4216 unit = insn_unit (insn);
4218 if (sched_verbose >= 2)
4220 fprintf (dump, ";;\t\t--> scheduling insn <<<%d>>> on unit ", INSN_UID (insn));
4221 insn_print_units (insn);
4222 fprintf (dump, "\n");
4225 if (sched_verbose && unit == -1)
4226 visualize_no_unit (insn);
4228 if (MAX_BLOCKAGE > 1 || issue_rate > 1 || sched_verbose)
4229 schedule_unit (unit, insn, clock);
4231 if (INSN_DEPEND (insn) == 0)
4232 return n_ready;
4234 /* This is used by the function adjust_priority above. */
4235 if (n_ready > 0)
4236 max_priority = MAX (INSN_PRIORITY (ready[0]), INSN_PRIORITY (insn));
4237 else
4238 max_priority = INSN_PRIORITY (insn);
4240 for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
4242 rtx next = XEXP (link, 0);
4243 int cost = insn_cost (insn, link, next);
4245 INSN_TICK (next) = MAX (INSN_TICK (next), clock + cost);
4247 if ((INSN_DEP_COUNT (next) -= 1) == 0)
4249 int effective_cost = INSN_TICK (next) - clock;
4251 /* For speculative insns, before inserting to ready/queue,
4252 check live, exception-free, and issue-delay */
4253 if (INSN_BB (next) != target_bb
4254 && (!IS_VALID (INSN_BB (next))
4255 || CANT_MOVE (next)
4256 || (IS_SPECULATIVE_INSN (next)
4257 && (insn_issue_delay (next) > 3
4258 || !check_live (next, INSN_BB (next))
4259 || !is_exception_free (next, INSN_BB (next), target_bb)))))
4260 continue;
4262 if (sched_verbose >= 2)
4264 fprintf (dump, ";;\t\tdependences resolved: insn %d ", INSN_UID (next));
4266 if (current_nr_blocks > 1 && INSN_BB (next) != target_bb)
4267 fprintf (dump, "/b%d ", INSN_BLOCK (next));
4269 if (effective_cost <= 1)
4270 fprintf (dump, "into ready\n");
4271 else
4272 fprintf (dump, "into queue with cost=%d\n", effective_cost);
4275 /* Adjust the priority of NEXT and either put it on the ready
4276 list or queue it. */
4277 adjust_priority (next);
4278 if (effective_cost <= 1)
4279 ready[n_ready++] = next;
4280 else
4281 queue_insn (next, effective_cost);
4285 return n_ready;
4289 /* Add a REG_DEAD note for REG to INSN, reusing a REG_DEAD note from the
4290 dead_notes list. */
4292 static void
4293 create_reg_dead_note (reg, insn)
4294 rtx reg, insn;
4296 rtx link;
4298 /* The number of registers killed after scheduling must be the same as the
4299 number of registers killed before scheduling. The number of REG_DEAD
4300 notes may not be conserved, i.e. two SImode hard register REG_DEAD notes
4301 might become one DImode hard register REG_DEAD note, but the number of
4302 registers killed will be conserved.
4304 We carefully remove REG_DEAD notes from the dead_notes list, so that
4305 there will be none left at the end. If we run out early, then there
4306 is a bug somewhere in flow, combine and/or sched. */
4308 if (dead_notes == 0)
4310 if (current_nr_blocks <= 1)
4311 abort ();
4312 else
4313 link = alloc_EXPR_LIST (REG_DEAD, NULL_RTX, NULL_RTX);
4315 else
4317 /* Number of regs killed by REG. */
4318 int regs_killed = (REGNO (reg) >= FIRST_PSEUDO_REGISTER ? 1
4319 : HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)));
4320 /* Number of regs killed by REG_DEAD notes taken off the list. */
4321 int reg_note_regs;
4323 link = dead_notes;
4324 reg_note_regs = (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
4325 : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
4326 GET_MODE (XEXP (link, 0))));
4327 while (reg_note_regs < regs_killed)
4329 link = XEXP (link, 1);
4331 /* LINK might be zero if we killed more registers after scheduling
4332 than before, and the last hard register we kill is actually
4333 multiple hard regs.
4335 This is normal for interblock scheduling, so deal with it in
4336 that case, else abort. */
4337 if (link == NULL_RTX && current_nr_blocks <= 1)
4338 abort ();
4339 else if (link == NULL_RTX)
4340 link = alloc_EXPR_LIST (REG_DEAD, gen_rtx_REG (word_mode, 0),
4341 NULL_RTX);
4343 reg_note_regs += (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
4344 : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
4345 GET_MODE (XEXP (link, 0))));
4347 dead_notes = XEXP (link, 1);
4349 /* If we took too many regs kills off, put the extra ones back. */
4350 while (reg_note_regs > regs_killed)
4352 rtx temp_reg, temp_link;
4354 temp_reg = gen_rtx_REG (word_mode, 0);
4355 temp_link = alloc_EXPR_LIST (REG_DEAD, temp_reg, dead_notes);
4356 dead_notes = temp_link;
4357 reg_note_regs--;
4361 XEXP (link, 0) = reg;
4362 XEXP (link, 1) = REG_NOTES (insn);
4363 REG_NOTES (insn) = link;
4366 /* Subroutine on attach_deaths_insn--handles the recursive search
4367 through INSN. If SET_P is true, then x is being modified by the insn. */
4369 static void
4370 attach_deaths (x, insn, set_p)
4371 rtx x;
4372 rtx insn;
4373 int set_p;
4375 register int i;
4376 register int j;
4377 register enum rtx_code code;
4378 register char *fmt;
4380 if (x == 0)
4381 return;
4383 code = GET_CODE (x);
4385 switch (code)
4387 case CONST_INT:
4388 case CONST_DOUBLE:
4389 case LABEL_REF:
4390 case SYMBOL_REF:
4391 case CONST:
4392 case CODE_LABEL:
4393 case PC:
4394 case CC0:
4395 /* Get rid of the easy cases first. */
4396 return;
4398 case REG:
4400 /* If the register dies in this insn, queue that note, and mark
4401 this register as needing to die. */
4402 /* This code is very similar to mark_used_1 (if set_p is false)
4403 and mark_set_1 (if set_p is true) in flow.c. */
4405 register int regno;
4406 int some_needed;
4407 int all_needed;
4409 if (set_p)
4410 return;
4412 regno = REGNO (x);
4413 all_needed = some_needed = REGNO_REG_SET_P (old_live_regs, regno);
4414 if (regno < FIRST_PSEUDO_REGISTER)
4416 int n;
4418 n = HARD_REGNO_NREGS (regno, GET_MODE (x));
4419 while (--n > 0)
4421 int needed = (REGNO_REG_SET_P (old_live_regs, regno + n));
4422 some_needed |= needed;
4423 all_needed &= needed;
4427 /* If it wasn't live before we started, then add a REG_DEAD note.
4428 We must check the previous lifetime info not the current info,
4429 because we may have to execute this code several times, e.g.
4430 once for a clobber (which doesn't add a note) and later
4431 for a use (which does add a note).
4433 Always make the register live. We must do this even if it was
4434 live before, because this may be an insn which sets and uses
4435 the same register, in which case the register has already been
4436 killed, so we must make it live again.
4438 Global registers are always live, and should never have a REG_DEAD
4439 note added for them, so none of the code below applies to them. */
4441 if (regno >= FIRST_PSEUDO_REGISTER || ! global_regs[regno])
4443 /* Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
4444 STACK_POINTER_REGNUM, since these are always considered to be
4445 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
4446 if (regno != FRAME_POINTER_REGNUM
4447 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
4448 && ! (regno == HARD_FRAME_POINTER_REGNUM)
4449 #endif
4450 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
4451 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
4452 #endif
4453 && regno != STACK_POINTER_REGNUM)
4455 /* ??? It is perhaps a dead_or_set_p bug that it does
4456 not check for REG_UNUSED notes itself. This is necessary
4457 for the case where the SET_DEST is a subreg of regno, as
4458 dead_or_set_p handles subregs specially. */
4459 if (! all_needed && ! dead_or_set_p (insn, x)
4460 && ! find_reg_note (insn, REG_UNUSED, x))
4462 /* Check for the case where the register dying partially
4463 overlaps the register set by this insn. */
4464 if (regno < FIRST_PSEUDO_REGISTER
4465 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
4467 int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
4468 while (--n >= 0)
4469 some_needed |= dead_or_set_regno_p (insn, regno + n);
4472 /* If none of the words in X is needed, make a REG_DEAD
4473 note. Otherwise, we must make partial REG_DEAD
4474 notes. */
4475 if (! some_needed)
4476 create_reg_dead_note (x, insn);
4477 else
4479 int i;
4481 /* Don't make a REG_DEAD note for a part of a
4482 register that is set in the insn. */
4483 for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
4484 i >= 0; i--)
4485 if (! REGNO_REG_SET_P (old_live_regs, regno+i)
4486 && ! dead_or_set_regno_p (insn, regno + i))
4487 create_reg_dead_note (gen_rtx_REG (reg_raw_mode[regno + i],
4488 regno + i),
4489 insn);
4494 if (regno < FIRST_PSEUDO_REGISTER)
4496 int j = HARD_REGNO_NREGS (regno, GET_MODE (x));
4497 while (--j >= 0)
4499 SET_REGNO_REG_SET (bb_live_regs, regno + j);
4502 else
4504 /* Recompute REG_BASIC_BLOCK as we update all the other
4505 dataflow information. */
4506 if (sched_reg_basic_block[regno] == REG_BLOCK_UNKNOWN)
4507 sched_reg_basic_block[regno] = current_block_num;
4508 else if (sched_reg_basic_block[regno] != current_block_num)
4509 sched_reg_basic_block[regno] = REG_BLOCK_GLOBAL;
4511 SET_REGNO_REG_SET (bb_live_regs, regno);
4514 return;
4517 case MEM:
4518 /* Handle tail-recursive case. */
4519 attach_deaths (XEXP (x, 0), insn, 0);
4520 return;
4522 case SUBREG:
4523 case STRICT_LOW_PART:
4524 /* These two cases preserve the value of SET_P, so handle them
4525 separately. */
4526 attach_deaths (XEXP (x, 0), insn, set_p);
4527 return;
4529 case ZERO_EXTRACT:
4530 case SIGN_EXTRACT:
4531 /* This case preserves the value of SET_P for the first operand, but
4532 clears it for the other two. */
4533 attach_deaths (XEXP (x, 0), insn, set_p);
4534 attach_deaths (XEXP (x, 1), insn, 0);
4535 attach_deaths (XEXP (x, 2), insn, 0);
4536 return;
4538 default:
4539 /* Other cases: walk the insn. */
4540 fmt = GET_RTX_FORMAT (code);
4541 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4543 if (fmt[i] == 'e')
4544 attach_deaths (XEXP (x, i), insn, 0);
4545 else if (fmt[i] == 'E')
4546 for (j = 0; j < XVECLEN (x, i); j++)
4547 attach_deaths (XVECEXP (x, i, j), insn, 0);
4552 /* After INSN has executed, add register death notes for each register
4553 that is dead after INSN. */
4555 static void
4556 attach_deaths_insn (insn)
4557 rtx insn;
4559 rtx x = PATTERN (insn);
4560 register RTX_CODE code = GET_CODE (x);
4561 rtx link;
4563 if (code == SET)
4565 attach_deaths (SET_SRC (x), insn, 0);
4567 /* A register might die here even if it is the destination, e.g.
4568 it is the target of a volatile read and is otherwise unused.
4569 Hence we must always call attach_deaths for the SET_DEST. */
4570 attach_deaths (SET_DEST (x), insn, 1);
4572 else if (code == PARALLEL)
4574 register int i;
4575 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
4577 code = GET_CODE (XVECEXP (x, 0, i));
4578 if (code == SET)
4580 attach_deaths (SET_SRC (XVECEXP (x, 0, i)), insn, 0);
4582 attach_deaths (SET_DEST (XVECEXP (x, 0, i)), insn, 1);
4584 /* Flow does not add REG_DEAD notes to registers that die in
4585 clobbers, so we can't either. */
4586 else if (code != CLOBBER)
4587 attach_deaths (XVECEXP (x, 0, i), insn, 0);
4590 /* If this is a CLOBBER, only add REG_DEAD notes to registers inside a
4591 MEM being clobbered, just like flow. */
4592 else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == MEM)
4593 attach_deaths (XEXP (XEXP (x, 0), 0), insn, 0);
4594 /* Otherwise don't add a death note to things being clobbered. */
4595 else if (code != CLOBBER)
4596 attach_deaths (x, insn, 0);
4598 /* Make death notes for things used in the called function. */
4599 if (GET_CODE (insn) == CALL_INSN)
4600 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
4601 attach_deaths (XEXP (XEXP (link, 0), 0), insn,
4602 GET_CODE (XEXP (link, 0)) == CLOBBER);
4605 /* functions for handlnig of notes */
4607 /* Delete notes beginning with INSN and put them in the chain
4608 of notes ended by NOTE_LIST.
4609 Returns the insn following the notes. */
4611 static rtx
4612 unlink_other_notes (insn, tail)
4613 rtx insn, tail;
4615 rtx prev = PREV_INSN (insn);
4617 while (insn != tail && GET_CODE (insn) == NOTE)
4619 rtx next = NEXT_INSN (insn);
4620 /* Delete the note from its current position. */
4621 if (prev)
4622 NEXT_INSN (prev) = next;
4623 if (next)
4624 PREV_INSN (next) = prev;
4626 /* Don't save away NOTE_INSN_SETJMPs, because they must remain
4627 immediately after the call they follow. We use a fake
4628 (REG_DEAD (const_int -1)) note to remember them.
4629 Likewise with NOTE_INSN_{LOOP,EHREGION}_{BEG, END}. */
4630 if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_SETJMP
4631 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG
4632 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_END
4633 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_BEG
4634 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_END)
4636 /* Insert the note at the end of the notes list. */
4637 PREV_INSN (insn) = note_list;
4638 if (note_list)
4639 NEXT_INSN (note_list) = insn;
4640 note_list = insn;
4643 insn = next;
4645 return insn;
4648 /* Delete line notes beginning with INSN. Record line-number notes so
4649 they can be reused. Returns the insn following the notes. */
4651 static rtx
4652 unlink_line_notes (insn, tail)
4653 rtx insn, tail;
4655 rtx prev = PREV_INSN (insn);
4657 while (insn != tail && GET_CODE (insn) == NOTE)
4659 rtx next = NEXT_INSN (insn);
4661 if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
4663 /* Delete the note from its current position. */
4664 if (prev)
4665 NEXT_INSN (prev) = next;
4666 if (next)
4667 PREV_INSN (next) = prev;
4669 /* Record line-number notes so they can be reused. */
4670 LINE_NOTE (insn) = insn;
4672 else
4673 prev = insn;
4675 insn = next;
4677 return insn;
4680 /* Return the head and tail pointers of BB. */
4682 __inline static void
4683 get_block_head_tail (bb, headp, tailp)
4684 int bb;
4685 rtx *headp;
4686 rtx *tailp;
4689 rtx head;
4690 rtx tail;
4691 int b;
4693 b = BB_TO_BLOCK (bb);
4695 /* HEAD and TAIL delimit the basic block being scheduled. */
4696 head = basic_block_head[b];
4697 tail = basic_block_end[b];
4699 /* Don't include any notes or labels at the beginning of the
4700 basic block, or notes at the ends of basic blocks. */
4701 while (head != tail)
4703 if (GET_CODE (head) == NOTE)
4704 head = NEXT_INSN (head);
4705 else if (GET_CODE (tail) == NOTE)
4706 tail = PREV_INSN (tail);
4707 else if (GET_CODE (head) == CODE_LABEL)
4708 head = NEXT_INSN (head);
4709 else
4710 break;
4713 *headp = head;
4714 *tailp = tail;
4717 /* Delete line notes from bb. Save them so they can be later restored
4718 (in restore_line_notes ()). */
4720 static void
4721 rm_line_notes (bb)
4722 int bb;
4724 rtx next_tail;
4725 rtx tail;
4726 rtx head;
4727 rtx insn;
4729 get_block_head_tail (bb, &head, &tail);
4731 if (head == tail
4732 && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
4733 return;
4735 next_tail = NEXT_INSN (tail);
4736 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
4738 rtx prev;
4740 /* Farm out notes, and maybe save them in NOTE_LIST.
4741 This is needed to keep the debugger from
4742 getting completely deranged. */
4743 if (GET_CODE (insn) == NOTE)
4745 prev = insn;
4746 insn = unlink_line_notes (insn, next_tail);
4748 if (prev == tail)
4749 abort ();
4750 if (prev == head)
4751 abort ();
4752 if (insn == next_tail)
4753 abort ();
4758 /* Save line number notes for each insn in bb. */
4760 static void
4761 save_line_notes (bb)
4762 int bb;
4764 rtx head, tail;
4765 rtx next_tail;
4767 /* We must use the true line number for the first insn in the block
4768 that was computed and saved at the start of this pass. We can't
4769 use the current line number, because scheduling of the previous
4770 block may have changed the current line number. */
4772 rtx line = line_note_head[BB_TO_BLOCK (bb)];
4773 rtx insn;
4775 get_block_head_tail (bb, &head, &tail);
4776 next_tail = NEXT_INSN (tail);
4778 for (insn = basic_block_head[BB_TO_BLOCK (bb)];
4779 insn != next_tail;
4780 insn = NEXT_INSN (insn))
4781 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
4782 line = insn;
4783 else
4784 LINE_NOTE (insn) = line;
4788 /* After bb was scheduled, insert line notes into the insns list. */
4790 static void
4791 restore_line_notes (bb)
4792 int bb;
4794 rtx line, note, prev, new;
4795 int added_notes = 0;
4796 int b;
4797 rtx head, next_tail, insn;
4799 b = BB_TO_BLOCK (bb);
4801 head = basic_block_head[b];
4802 next_tail = NEXT_INSN (basic_block_end[b]);
4804 /* Determine the current line-number. We want to know the current
4805 line number of the first insn of the block here, in case it is
4806 different from the true line number that was saved earlier. If
4807 different, then we need a line number note before the first insn
4808 of this block. If it happens to be the same, then we don't want to
4809 emit another line number note here. */
4810 for (line = head; line; line = PREV_INSN (line))
4811 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
4812 break;
4814 /* Walk the insns keeping track of the current line-number and inserting
4815 the line-number notes as needed. */
4816 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
4817 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
4818 line = insn;
4819 /* This used to emit line number notes before every non-deleted note.
4820 However, this confuses a debugger, because line notes not separated
4821 by real instructions all end up at the same address. I can find no
4822 use for line number notes before other notes, so none are emitted. */
4823 else if (GET_CODE (insn) != NOTE
4824 && (note = LINE_NOTE (insn)) != 0
4825 && note != line
4826 && (line == 0
4827 || NOTE_LINE_NUMBER (note) != NOTE_LINE_NUMBER (line)
4828 || NOTE_SOURCE_FILE (note) != NOTE_SOURCE_FILE (line)))
4830 line = note;
4831 prev = PREV_INSN (insn);
4832 if (LINE_NOTE (note))
4834 /* Re-use the original line-number note. */
4835 LINE_NOTE (note) = 0;
4836 PREV_INSN (note) = prev;
4837 NEXT_INSN (prev) = note;
4838 PREV_INSN (insn) = note;
4839 NEXT_INSN (note) = insn;
4841 else
4843 added_notes++;
4844 new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
4845 NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
4846 RTX_INTEGRATED_P (new) = RTX_INTEGRATED_P (note);
4849 if (sched_verbose && added_notes)
4850 fprintf (dump, ";; added %d line-number notes\n", added_notes);
4853 /* After scheduling the function, delete redundant line notes from the
4854 insns list. */
4856 static void
4857 rm_redundant_line_notes ()
4859 rtx line = 0;
4860 rtx insn = get_insns ();
4861 int active_insn = 0;
4862 int notes = 0;
4864 /* Walk the insns deleting redundant line-number notes. Many of these
4865 are already present. The remainder tend to occur at basic
4866 block boundaries. */
4867 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
4868 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
4870 /* If there are no active insns following, INSN is redundant. */
4871 if (active_insn == 0)
4873 notes++;
4874 NOTE_SOURCE_FILE (insn) = 0;
4875 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4877 /* If the line number is unchanged, LINE is redundant. */
4878 else if (line
4879 && NOTE_LINE_NUMBER (line) == NOTE_LINE_NUMBER (insn)
4880 && NOTE_SOURCE_FILE (line) == NOTE_SOURCE_FILE (insn))
4882 notes++;
4883 NOTE_SOURCE_FILE (line) = 0;
4884 NOTE_LINE_NUMBER (line) = NOTE_INSN_DELETED;
4885 line = insn;
4887 else
4888 line = insn;
4889 active_insn = 0;
4891 else if (!((GET_CODE (insn) == NOTE
4892 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
4893 || (GET_CODE (insn) == INSN
4894 && (GET_CODE (PATTERN (insn)) == USE
4895 || GET_CODE (PATTERN (insn)) == CLOBBER))))
4896 active_insn++;
4898 if (sched_verbose && notes)
4899 fprintf (dump, ";; deleted %d line-number notes\n", notes);
4902 /* Delete notes between head and tail and put them in the chain
4903 of notes ended by NOTE_LIST. */
4905 static void
4906 rm_other_notes (head, tail)
4907 rtx head;
4908 rtx tail;
4910 rtx next_tail;
4911 rtx insn;
4913 if (head == tail
4914 && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
4915 return;
4917 next_tail = NEXT_INSN (tail);
4918 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
4920 rtx prev;
4922 /* Farm out notes, and maybe save them in NOTE_LIST.
4923 This is needed to keep the debugger from
4924 getting completely deranged. */
4925 if (GET_CODE (insn) == NOTE)
4927 prev = insn;
4929 insn = unlink_other_notes (insn, next_tail);
4931 if (prev == tail)
4932 abort ();
4933 if (prev == head)
4934 abort ();
4935 if (insn == next_tail)
4936 abort ();
4941 /* Constructor for `sometimes' data structure. */
4943 static int
4944 new_sometimes_live (regs_sometimes_live, regno, sometimes_max)
4945 struct sometimes *regs_sometimes_live;
4946 int regno;
4947 int sometimes_max;
4949 register struct sometimes *p;
4951 /* There should never be a register greater than max_regno here. If there
4952 is, it means that a define_split has created a new pseudo reg. This
4953 is not allowed, since there will not be flow info available for any
4954 new register, so catch the error here. */
4955 if (regno >= max_regno)
4956 abort ();
4958 p = &regs_sometimes_live[sometimes_max];
4959 p->regno = regno;
4960 p->live_length = 0;
4961 p->calls_crossed = 0;
4962 sometimes_max++;
4963 return sometimes_max;
4966 /* Count lengths of all regs we are currently tracking,
4967 and find new registers no longer live. */
4969 static void
4970 finish_sometimes_live (regs_sometimes_live, sometimes_max)
4971 struct sometimes *regs_sometimes_live;
4972 int sometimes_max;
4974 int i;
4976 for (i = 0; i < sometimes_max; i++)
4978 register struct sometimes *p = &regs_sometimes_live[i];
4979 int regno = p->regno;
4981 sched_reg_live_length[regno] += p->live_length;
4982 sched_reg_n_calls_crossed[regno] += p->calls_crossed;
4986 /* functions for computation of registers live/usage info */
4988 /* It is assumed that prior to scheduling basic_block_live_at_start (b)
4989 contains the registers that are alive at the entry to b.
4991 Two passes follow: The first pass is performed before the scheduling
4992 of a region. It scans each block of the region forward, computing
4993 the set of registers alive at the end of the basic block and
4994 discard REG_DEAD notes (done by find_pre_sched_live ()).
4996 The second path is invoked after scheduling all region blocks.
4997 It scans each block of the region backward, a block being traversed
4998 only after its succesors in the region. When the set of registers
4999 live at the end of a basic block may be changed by the scheduling
5000 (this may happen for multiple blocks region), it is computed as
5001 the union of the registers live at the start of its succesors.
5002 The last-use information is updated by inserting REG_DEAD notes.
5003 (done by find_post_sched_live ()) */
5005 /* Scan all the insns to be scheduled, removing register death notes.
5006 Register death notes end up in DEAD_NOTES.
5007 Recreate the register life information for the end of this basic
5008 block. */
5010 static void
5011 find_pre_sched_live (bb)
5012 int bb;
5014 rtx insn, next_tail, head, tail;
5015 int b = BB_TO_BLOCK (bb);
5017 get_block_head_tail (bb, &head, &tail);
5018 COPY_REG_SET (bb_live_regs, basic_block_live_at_start[b]);
5019 next_tail = NEXT_INSN (tail);
5021 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
5023 rtx prev, next, link;
5024 int reg_weight = 0;
5026 /* Handle register life information. */
5027 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
5029 /* See if the register gets born here. */
5030 /* We must check for registers being born before we check for
5031 registers dying. It is possible for a register to be born and
5032 die in the same insn, e.g. reading from a volatile memory
5033 location into an otherwise unused register. Such a register
5034 must be marked as dead after this insn. */
5035 if (GET_CODE (PATTERN (insn)) == SET
5036 || GET_CODE (PATTERN (insn)) == CLOBBER)
5038 sched_note_set (PATTERN (insn), 0);
5039 reg_weight++;
5042 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
5044 int j;
5045 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
5046 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
5047 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
5049 sched_note_set (XVECEXP (PATTERN (insn), 0, j), 0);
5050 reg_weight++;
5053 /* ??? This code is obsolete and should be deleted. It
5054 is harmless though, so we will leave it in for now. */
5055 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
5056 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == USE)
5057 sched_note_set (XVECEXP (PATTERN (insn), 0, j), 0);
5060 /* Each call cobbers (makes live) all call-clobbered regs
5061 that are not global or fixed. Note that the function-value
5062 reg is a call_clobbered reg. */
5063 if (GET_CODE (insn) == CALL_INSN)
5065 int j;
5066 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
5067 if (call_used_regs[j] && !global_regs[j]
5068 && ! fixed_regs[j])
5070 SET_REGNO_REG_SET (bb_live_regs, j);
5074 /* Need to know what registers this insn kills. */
5075 for (prev = 0, link = REG_NOTES (insn); link; link = next)
5077 next = XEXP (link, 1);
5078 if ((REG_NOTE_KIND (link) == REG_DEAD
5079 || REG_NOTE_KIND (link) == REG_UNUSED)
5080 /* Verify that the REG_NOTE has a valid value. */
5081 && GET_CODE (XEXP (link, 0)) == REG)
5083 register int regno = REGNO (XEXP (link, 0));
5085 reg_weight--;
5087 /* Only unlink REG_DEAD notes; leave REG_UNUSED notes
5088 alone. */
5089 if (REG_NOTE_KIND (link) == REG_DEAD)
5091 if (prev)
5092 XEXP (prev, 1) = next;
5093 else
5094 REG_NOTES (insn) = next;
5095 XEXP (link, 1) = dead_notes;
5096 dead_notes = link;
5098 else
5099 prev = link;
5101 if (regno < FIRST_PSEUDO_REGISTER)
5103 int j = HARD_REGNO_NREGS (regno,
5104 GET_MODE (XEXP (link, 0)));
5105 while (--j >= 0)
5107 CLEAR_REGNO_REG_SET (bb_live_regs, regno+j);
5110 else
5112 CLEAR_REGNO_REG_SET (bb_live_regs, regno);
5115 else
5116 prev = link;
5120 INSN_REG_WEIGHT (insn) = reg_weight;
5124 /* Update register life and usage information for block bb
5125 after scheduling. Put register dead notes back in the code. */
5127 static void
5128 find_post_sched_live (bb)
5129 int bb;
5131 int sometimes_max;
5132 int j, i;
5133 int b;
5134 rtx insn;
5135 rtx head, tail, prev_head, next_tail;
5137 register struct sometimes *regs_sometimes_live;
5139 b = BB_TO_BLOCK (bb);
5141 /* compute live regs at the end of bb as a function of its successors. */
5142 if (current_nr_blocks > 1)
5144 int e;
5145 int first_edge;
5147 first_edge = e = OUT_EDGES (b);
5148 CLEAR_REG_SET (bb_live_regs);
5150 if (e)
5153 int b_succ;
5155 b_succ = TO_BLOCK (e);
5156 IOR_REG_SET (bb_live_regs, basic_block_live_at_start[b_succ]);
5157 e = NEXT_OUT (e);
5159 while (e != first_edge);
5162 get_block_head_tail (bb, &head, &tail);
5163 next_tail = NEXT_INSN (tail);
5164 prev_head = PREV_INSN (head);
5166 EXECUTE_IF_SET_IN_REG_SET (bb_live_regs, FIRST_PSEUDO_REGISTER, i,
5168 sched_reg_basic_block[i] = REG_BLOCK_GLOBAL;
5171 /* if the block is empty, same regs are alive at its end and its start.
5172 since this is not guaranteed after interblock scheduling, make sure they
5173 are truly identical. */
5174 if (NEXT_INSN (prev_head) == tail
5175 && (GET_RTX_CLASS (GET_CODE (tail)) != 'i'))
5177 if (current_nr_blocks > 1)
5178 COPY_REG_SET (basic_block_live_at_start[b], bb_live_regs);
5180 return;
5183 b = BB_TO_BLOCK (bb);
5184 current_block_num = b;
5186 /* Keep track of register lives. */
5187 old_live_regs = ALLOCA_REG_SET ();
5188 regs_sometimes_live
5189 = (struct sometimes *) alloca (max_regno * sizeof (struct sometimes));
5190 sometimes_max = 0;
5192 /* initiate "sometimes" data, starting with registers live at end */
5193 sometimes_max = 0;
5194 COPY_REG_SET (old_live_regs, bb_live_regs);
5195 EXECUTE_IF_SET_IN_REG_SET (bb_live_regs, 0, j,
5197 sometimes_max
5198 = new_sometimes_live (regs_sometimes_live,
5199 j, sometimes_max);
5202 /* scan insns back, computing regs live info */
5203 for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
5205 /* First we kill registers set by this insn, and then we
5206 make registers used by this insn live. This is the opposite
5207 order used above because we are traversing the instructions
5208 backwards. */
5210 /* Strictly speaking, we should scan REG_UNUSED notes and make
5211 every register mentioned there live, however, we will just
5212 kill them again immediately below, so there doesn't seem to
5213 be any reason why we bother to do this. */
5215 /* See if this is the last notice we must take of a register. */
5216 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
5217 continue;
5219 if (GET_CODE (PATTERN (insn)) == SET
5220 || GET_CODE (PATTERN (insn)) == CLOBBER)
5221 sched_note_set (PATTERN (insn), 1);
5222 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
5224 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
5225 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
5226 || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
5227 sched_note_set (XVECEXP (PATTERN (insn), 0, j), 1);
5230 /* This code keeps life analysis information up to date. */
5231 if (GET_CODE (insn) == CALL_INSN)
5233 register struct sometimes *p;
5235 /* A call kills all call used registers that are not
5236 global or fixed, except for those mentioned in the call
5237 pattern which will be made live again later. */
5238 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5239 if (call_used_regs[i] && ! global_regs[i]
5240 && ! fixed_regs[i])
5242 CLEAR_REGNO_REG_SET (bb_live_regs, i);
5245 /* Regs live at the time of a call instruction must not
5246 go in a register clobbered by calls. Record this for
5247 all regs now live. Note that insns which are born or
5248 die in a call do not cross a call, so this must be done
5249 after the killings (above) and before the births
5250 (below). */
5251 p = regs_sometimes_live;
5252 for (i = 0; i < sometimes_max; i++, p++)
5253 if (REGNO_REG_SET_P (bb_live_regs, p->regno))
5254 p->calls_crossed += 1;
5257 /* Make every register used live, and add REG_DEAD notes for
5258 registers which were not live before we started. */
5259 attach_deaths_insn (insn);
5261 /* Find registers now made live by that instruction. */
5262 EXECUTE_IF_AND_COMPL_IN_REG_SET (bb_live_regs, old_live_regs, 0, j,
5264 sometimes_max
5265 = new_sometimes_live (regs_sometimes_live,
5266 j, sometimes_max);
5268 IOR_REG_SET (old_live_regs, bb_live_regs);
5270 /* Count lengths of all regs we are worrying about now,
5271 and handle registers no longer live. */
5273 for (i = 0; i < sometimes_max; i++)
5275 register struct sometimes *p = &regs_sometimes_live[i];
5276 int regno = p->regno;
5278 p->live_length += 1;
5280 if (!REGNO_REG_SET_P (bb_live_regs, regno))
5282 /* This is the end of one of this register's lifetime
5283 segments. Save the lifetime info collected so far,
5284 and clear its bit in the old_live_regs entry. */
5285 sched_reg_live_length[regno] += p->live_length;
5286 sched_reg_n_calls_crossed[regno] += p->calls_crossed;
5287 CLEAR_REGNO_REG_SET (old_live_regs, p->regno);
5289 /* Delete the reg_sometimes_live entry for this reg by
5290 copying the last entry over top of it. */
5291 *p = regs_sometimes_live[--sometimes_max];
5292 /* ...and decrement i so that this newly copied entry
5293 will be processed. */
5294 i--;
5299 finish_sometimes_live (regs_sometimes_live, sometimes_max);
5301 /* In interblock scheduling, basic_block_live_at_start may have changed. */
5302 if (current_nr_blocks > 1)
5303 COPY_REG_SET (basic_block_live_at_start[b], bb_live_regs);
5306 FREE_REG_SET (old_live_regs);
5307 } /* find_post_sched_live */
5309 /* After scheduling the subroutine, restore information about uses of
5310 registers. */
5312 static void
5313 update_reg_usage ()
5315 int regno;
5317 if (n_basic_blocks > 0)
5318 EXECUTE_IF_SET_IN_REG_SET (bb_live_regs, FIRST_PSEUDO_REGISTER, regno,
5320 sched_reg_basic_block[regno]
5321 = REG_BLOCK_GLOBAL;
5324 for (regno = 0; regno < max_regno; regno++)
5325 if (sched_reg_live_length[regno])
5327 if (sched_verbose)
5329 if (REG_LIVE_LENGTH (regno) > sched_reg_live_length[regno])
5330 fprintf (dump,
5331 ";; register %d life shortened from %d to %d\n",
5332 regno, REG_LIVE_LENGTH (regno),
5333 sched_reg_live_length[regno]);
5334 /* Negative values are special; don't overwrite the current
5335 reg_live_length value if it is negative. */
5336 else if (REG_LIVE_LENGTH (regno) < sched_reg_live_length[regno]
5337 && REG_LIVE_LENGTH (regno) >= 0)
5338 fprintf (dump,
5339 ";; register %d life extended from %d to %d\n",
5340 regno, REG_LIVE_LENGTH (regno),
5341 sched_reg_live_length[regno]);
5343 if (!REG_N_CALLS_CROSSED (regno)
5344 && sched_reg_n_calls_crossed[regno])
5345 fprintf (dump,
5346 ";; register %d now crosses calls\n", regno);
5347 else if (REG_N_CALLS_CROSSED (regno)
5348 && !sched_reg_n_calls_crossed[regno]
5349 && REG_BASIC_BLOCK (regno) != REG_BLOCK_GLOBAL)
5350 fprintf (dump,
5351 ";; register %d no longer crosses calls\n", regno);
5353 if (REG_BASIC_BLOCK (regno) != sched_reg_basic_block[regno]
5354 && sched_reg_basic_block[regno] != REG_BLOCK_UNKNOWN
5355 && REG_BASIC_BLOCK(regno) != REG_BLOCK_UNKNOWN)
5356 fprintf (dump,
5357 ";; register %d changed basic block from %d to %d\n",
5358 regno, REG_BASIC_BLOCK(regno),
5359 sched_reg_basic_block[regno]);
5362 /* Negative values are special; don't overwrite the current
5363 reg_live_length value if it is negative. */
5364 if (REG_LIVE_LENGTH (regno) >= 0)
5365 REG_LIVE_LENGTH (regno) = sched_reg_live_length[regno];
5367 if (sched_reg_basic_block[regno] != REG_BLOCK_UNKNOWN
5368 && REG_BASIC_BLOCK(regno) != REG_BLOCK_UNKNOWN)
5369 REG_BASIC_BLOCK(regno) = sched_reg_basic_block[regno];
5371 /* We can't change the value of reg_n_calls_crossed to zero for
5372 pseudos which are live in more than one block.
5374 This is because combine might have made an optimization which
5375 invalidated basic_block_live_at_start and reg_n_calls_crossed,
5376 but it does not update them. If we update reg_n_calls_crossed
5377 here, the two variables are now inconsistent, and this might
5378 confuse the caller-save code into saving a register that doesn't
5379 need to be saved. This is only a problem when we zero calls
5380 crossed for a pseudo live in multiple basic blocks.
5382 Alternatively, we could try to correctly update basic block live
5383 at start here in sched, but that seems complicated.
5385 Note: it is possible that a global register became local, as result
5386 of interblock motion, but will remain marked as a global register. */
5387 if (sched_reg_n_calls_crossed[regno]
5388 || REG_BASIC_BLOCK (regno) != REG_BLOCK_GLOBAL)
5389 REG_N_CALLS_CROSSED (regno) = sched_reg_n_calls_crossed[regno];
5394 /* Scheduling clock, modified in schedule_block() and queue_to_ready () */
5395 static int clock_var;
5397 /* Move insns that became ready to fire from queue to ready list. */
5399 static int
5400 queue_to_ready (ready, n_ready)
5401 rtx ready[];
5402 int n_ready;
5404 rtx insn;
5405 rtx link;
5407 q_ptr = NEXT_Q (q_ptr);
5409 /* Add all pending insns that can be scheduled without stalls to the
5410 ready list. */
5411 for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
5414 insn = XEXP (link, 0);
5415 q_size -= 1;
5417 if (sched_verbose >= 2)
5418 fprintf (dump, ";;\t\tQ-->Ready: insn %d: ", INSN_UID (insn));
5420 if (sched_verbose >= 2 && INSN_BB (insn) != target_bb)
5421 fprintf (dump, "(b%d) ", INSN_BLOCK (insn));
5423 ready[n_ready++] = insn;
5424 if (sched_verbose >= 2)
5425 fprintf (dump, "moving to ready without stalls\n");
5427 insn_queue[q_ptr] = 0;
5429 /* If there are no ready insns, stall until one is ready and add all
5430 of the pending insns at that point to the ready list. */
5431 if (n_ready == 0)
5433 register int stalls;
5435 for (stalls = 1; stalls < INSN_QUEUE_SIZE; stalls++)
5437 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
5439 for (; link; link = XEXP (link, 1))
5441 insn = XEXP (link, 0);
5442 q_size -= 1;
5444 if (sched_verbose >= 2)
5445 fprintf (dump, ";;\t\tQ-->Ready: insn %d: ", INSN_UID (insn));
5447 if (sched_verbose >= 2 && INSN_BB (insn) != target_bb)
5448 fprintf (dump, "(b%d) ", INSN_BLOCK (insn));
5450 ready[n_ready++] = insn;
5451 if (sched_verbose >= 2)
5452 fprintf (dump, "moving to ready with %d stalls\n", stalls);
5454 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = 0;
5456 if (n_ready)
5457 break;
5461 if (sched_verbose && stalls)
5462 visualize_stall_cycles (BB_TO_BLOCK (target_bb), stalls);
5463 q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
5464 clock_var += stalls;
5466 return n_ready;
5469 /* Print the ready list for debugging purposes. Callable from debugger. */
5471 extern void
5472 debug_ready_list (ready, n_ready)
5473 rtx ready[];
5474 int n_ready;
5476 int i;
5478 for (i = 0; i < n_ready; i++)
5480 fprintf (dump, " %d", INSN_UID (ready[i]));
5481 if (current_nr_blocks > 1 && INSN_BB (ready[i]) != target_bb)
5482 fprintf (dump, "/b%d", INSN_BLOCK (ready[i]));
5484 fprintf (dump, "\n");
5487 /* Print names of units on which insn can/should execute, for debugging. */
5489 static void
5490 insn_print_units (insn)
5491 rtx insn;
5493 int i;
5494 int unit = insn_unit (insn);
5496 if (unit == -1)
5497 fprintf (dump, "none");
5498 else if (unit >= 0)
5499 fprintf (dump, "%s", function_units[unit].name);
5500 else
5502 fprintf (dump, "[");
5503 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
5504 if (unit & 1)
5506 fprintf (dump, "%s", function_units[i].name);
5507 if (unit != 1)
5508 fprintf (dump, " ");
5510 fprintf (dump, "]");
5514 /* MAX_VISUAL_LINES is the maximum number of lines in visualization table
5515 of a basic block. If more lines are needed, table is splitted to two.
5516 n_visual_lines is the number of lines printed so far for a block.
5517 visual_tbl contains the block visualization info.
5518 vis_no_unit holds insns in a cycle that are not mapped to any unit. */
5519 #define MAX_VISUAL_LINES 100
5520 #define INSN_LEN 30
5521 int n_visual_lines;
5522 char *visual_tbl;
5523 int n_vis_no_unit;
5524 rtx vis_no_unit[10];
5526 /* Finds units that are in use in this fuction. Required only
5527 for visualization. */
5529 static void
5530 init_target_units ()
5532 rtx insn;
5533 int unit;
5535 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
5537 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
5538 continue;
5540 unit = insn_unit (insn);
5542 if (unit < 0)
5543 target_units |= ~unit;
5544 else
5545 target_units |= (1 << unit);
5549 /* Return the length of the visualization table */
5551 static int
5552 get_visual_tbl_length ()
5554 int unit, i;
5555 int n, n1;
5556 char *s;
5558 /* compute length of one field in line */
5559 s = (char *) alloca (INSN_LEN + 5);
5560 sprintf (s, " %33s", "uname");
5561 n1 = strlen (s);
5563 /* compute length of one line */
5564 n = strlen (";; ");
5565 n += n1;
5566 for (unit = 0; unit < FUNCTION_UNITS_SIZE; unit++)
5567 if (function_units[unit].bitmask & target_units)
5568 for (i = 0; i < function_units[unit].multiplicity; i++)
5569 n += n1;
5570 n += n1;
5571 n += strlen ("\n") + 2;
5573 /* compute length of visualization string */
5574 return (MAX_VISUAL_LINES * n);
5577 /* Init block visualization debugging info */
5579 static void
5580 init_block_visualization ()
5582 strcpy (visual_tbl, "");
5583 n_visual_lines = 0;
5584 n_vis_no_unit = 0;
5587 #define BUF_LEN 256
5589 /* This recognizes rtx, I classified as expressions. These are always */
5590 /* represent some action on values or results of other expression, */
5591 /* that may be stored in objects representing values. */
5593 static void
5594 print_exp (buf, x, verbose)
5595 char *buf;
5596 rtx x;
5597 int verbose;
5599 char t1[BUF_LEN], t2[BUF_LEN], t3[BUF_LEN];
5601 switch (GET_CODE (x))
5603 case PLUS:
5604 print_value (t1, XEXP (x, 0), verbose);
5605 print_value (t2, XEXP (x, 1), verbose);
5606 sprintf (buf, "%s+%s", t1, t2);
5607 break;
5608 case LO_SUM:
5609 print_value (t1, XEXP (x, 0), verbose);
5610 print_value (t2, XEXP (x, 1), verbose);
5611 sprintf (buf, "%sl+%s", t1, t2);
5612 break;
5613 case MINUS:
5614 print_value (t1, XEXP (x, 0), verbose);
5615 print_value (t2, XEXP (x, 1), verbose);
5616 sprintf (buf, "%s-%s", t1, t2);
5617 break;
5618 case COMPARE:
5619 print_value (t1, XEXP (x, 0), verbose);
5620 print_value (t2, XEXP (x, 1), verbose);
5621 sprintf (buf, "%s??%s", t1, t2);
5622 break;
5623 case NEG:
5624 print_value (t1, XEXP (x, 0), verbose);
5625 sprintf (buf, "-%s", t1);
5626 break;
5627 case MULT:
5628 print_value (t1, XEXP (x, 0), verbose);
5629 print_value (t2, XEXP (x, 1), verbose);
5630 sprintf (buf, "%s*%s", t1, t2);
5631 break;
5632 case DIV:
5633 print_value (t1, XEXP (x, 0), verbose);
5634 print_value (t2, XEXP (x, 1), verbose);
5635 sprintf (buf, "%s/%s", t1, t2);
5636 break;
5637 case UDIV:
5638 print_value (t1, XEXP (x, 0), verbose);
5639 print_value (t2, XEXP (x, 1), verbose);
5640 sprintf (buf, "%su/%s", t1, t2);
5641 break;
5642 case MOD:
5643 print_value (t1, XEXP (x, 0), verbose);
5644 print_value (t2, XEXP (x, 1), verbose);
5645 sprintf (buf, "%s%%%s", t1, t2);
5646 break;
5647 case UMOD:
5648 print_value (t1, XEXP (x, 0), verbose);
5649 print_value (t2, XEXP (x, 1), verbose);
5650 sprintf (buf, "%su%%%s", t1, t2);
5651 break;
5652 case SMIN:
5653 print_value (t1, XEXP (x, 0), verbose);
5654 print_value (t2, XEXP (x, 1), verbose);
5655 sprintf (buf, "smin (%s, %s)", t1, t2);
5656 break;
5657 case SMAX:
5658 print_value (t1, XEXP (x, 0), verbose);
5659 print_value (t2, XEXP (x, 1), verbose);
5660 sprintf (buf, "smax(%s,%s)", t1, t2);
5661 break;
5662 case UMIN:
5663 print_value (t1, XEXP (x, 0), verbose);
5664 print_value (t2, XEXP (x, 1), verbose);
5665 sprintf (buf, "umin (%s, %s)", t1, t2);
5666 break;
5667 case UMAX:
5668 print_value (t1, XEXP (x, 0), verbose);
5669 print_value (t2, XEXP (x, 1), verbose);
5670 sprintf (buf, "umax(%s,%s)", t1, t2);
5671 break;
5672 case NOT:
5673 print_value (t1, XEXP (x, 0), verbose);
5674 sprintf (buf, "!%s", t1);
5675 break;
5676 case AND:
5677 print_value (t1, XEXP (x, 0), verbose);
5678 print_value (t2, XEXP (x, 1), verbose);
5679 sprintf (buf, "%s&%s", t1, t2);
5680 break;
5681 case IOR:
5682 print_value (t1, XEXP (x, 0), verbose);
5683 print_value (t2, XEXP (x, 1), verbose);
5684 sprintf (buf, "%s|%s", t1, t2);
5685 break;
5686 case XOR:
5687 print_value (t1, XEXP (x, 0), verbose);
5688 print_value (t2, XEXP (x, 1), verbose);
5689 sprintf (buf, "%s^%s", t1, t2);
5690 break;
5691 case ASHIFT:
5692 print_value (t1, XEXP (x, 0), verbose);
5693 print_value (t2, XEXP (x, 1), verbose);
5694 sprintf (buf, "%s<<%s", t1, t2);
5695 break;
5696 case LSHIFTRT:
5697 print_value (t1, XEXP (x, 0), verbose);
5698 print_value (t2, XEXP (x, 1), verbose);
5699 sprintf (buf, "%s0>%s", t1, t2);
5700 break;
5701 case ASHIFTRT:
5702 print_value (t1, XEXP (x, 0), verbose);
5703 print_value (t2, XEXP (x, 1), verbose);
5704 sprintf (buf, "%s>>%s", t1, t2);
5705 break;
5706 case ROTATE:
5707 print_value (t1, XEXP (x, 0), verbose);
5708 print_value (t2, XEXP (x, 1), verbose);
5709 sprintf (buf, "%s<-<%s", t1, t2);
5710 break;
5711 case ROTATERT:
5712 print_value (t1, XEXP (x, 0), verbose);
5713 print_value (t2, XEXP (x, 1), verbose);
5714 sprintf (buf, "%s>->%s", t1, t2);
5715 break;
5716 case ABS:
5717 print_value (t1, XEXP (x, 0), verbose);
5718 sprintf (buf, "abs(%s)", t1);
5719 break;
5720 case SQRT:
5721 print_value (t1, XEXP (x, 0), verbose);
5722 sprintf (buf, "sqrt(%s)", t1);
5723 break;
5724 case FFS:
5725 print_value (t1, XEXP (x, 0), verbose);
5726 sprintf (buf, "ffs(%s)", t1);
5727 break;
5728 case EQ:
5729 print_value (t1, XEXP (x, 0), verbose);
5730 print_value (t2, XEXP (x, 1), verbose);
5731 sprintf (buf, "%s == %s", t1, t2);
5732 break;
5733 case NE:
5734 print_value (t1, XEXP (x, 0), verbose);
5735 print_value (t2, XEXP (x, 1), verbose);
5736 sprintf (buf, "%s!=%s", t1, t2);
5737 break;
5738 case GT:
5739 print_value (t1, XEXP (x, 0), verbose);
5740 print_value (t2, XEXP (x, 1), verbose);
5741 sprintf (buf, "%s>%s", t1, t2);
5742 break;
5743 case GTU:
5744 print_value (t1, XEXP (x, 0), verbose);
5745 print_value (t2, XEXP (x, 1), verbose);
5746 sprintf (buf, "%s>u%s", t1, t2);
5747 break;
5748 case LT:
5749 print_value (t1, XEXP (x, 0), verbose);
5750 print_value (t2, XEXP (x, 1), verbose);
5751 sprintf (buf, "%s<%s", t1, t2);
5752 break;
5753 case LTU:
5754 print_value (t1, XEXP (x, 0), verbose);
5755 print_value (t2, XEXP (x, 1), verbose);
5756 sprintf (buf, "%s<u%s", t1, t2);
5757 break;
5758 case GE:
5759 print_value (t1, XEXP (x, 0), verbose);
5760 print_value (t2, XEXP (x, 1), verbose);
5761 sprintf (buf, "%s>=%s", t1, t2);
5762 break;
5763 case GEU:
5764 print_value (t1, XEXP (x, 0), verbose);
5765 print_value (t2, XEXP (x, 1), verbose);
5766 sprintf (buf, "%s>=u%s", t1, t2);
5767 break;
5768 case LE:
5769 print_value (t1, XEXP (x, 0), verbose);
5770 print_value (t2, XEXP (x, 1), verbose);
5771 sprintf (buf, "%s<=%s", t1, t2);
5772 break;
5773 case LEU:
5774 print_value (t1, XEXP (x, 0), verbose);
5775 print_value (t2, XEXP (x, 1), verbose);
5776 sprintf (buf, "%s<=u%s", t1, t2);
5777 break;
5778 case SIGN_EXTRACT:
5779 print_value (t1, XEXP (x, 0), verbose);
5780 print_value (t2, XEXP (x, 1), verbose);
5781 print_value (t3, XEXP (x, 2), verbose);
5782 if (verbose)
5783 sprintf (buf, "sign_extract(%s,%s,%s)", t1, t2, t3);
5784 else
5785 sprintf (buf, "sxt(%s,%s,%s)", t1, t2, t3);
5786 break;
5787 case ZERO_EXTRACT:
5788 print_value (t1, XEXP (x, 0), verbose);
5789 print_value (t2, XEXP (x, 1), verbose);
5790 print_value (t3, XEXP (x, 2), verbose);
5791 if (verbose)
5792 sprintf (buf, "zero_extract(%s,%s,%s)", t1, t2, t3);
5793 else
5794 sprintf (buf, "zxt(%s,%s,%s)", t1, t2, t3);
5795 break;
5796 case SIGN_EXTEND:
5797 print_value (t1, XEXP (x, 0), verbose);
5798 if (verbose)
5799 sprintf (buf, "sign_extend(%s)", t1);
5800 else
5801 sprintf (buf, "sxn(%s)", t1);
5802 break;
5803 case ZERO_EXTEND:
5804 print_value (t1, XEXP (x, 0), verbose);
5805 if (verbose)
5806 sprintf (buf, "zero_extend(%s)", t1);
5807 else
5808 sprintf (buf, "zxn(%s)", t1);
5809 break;
5810 case FLOAT_EXTEND:
5811 print_value (t1, XEXP (x, 0), verbose);
5812 if (verbose)
5813 sprintf (buf, "float_extend(%s)", t1);
5814 else
5815 sprintf (buf, "fxn(%s)", t1);
5816 break;
5817 case TRUNCATE:
5818 print_value (t1, XEXP (x, 0), verbose);
5819 if (verbose)
5820 sprintf (buf, "trunc(%s)", t1);
5821 else
5822 sprintf (buf, "trn(%s)", t1);
5823 break;
5824 case FLOAT_TRUNCATE:
5825 print_value (t1, XEXP (x, 0), verbose);
5826 if (verbose)
5827 sprintf (buf, "float_trunc(%s)", t1);
5828 else
5829 sprintf (buf, "ftr(%s)", t1);
5830 break;
5831 case FLOAT:
5832 print_value (t1, XEXP (x, 0), verbose);
5833 if (verbose)
5834 sprintf (buf, "float(%s)", t1);
5835 else
5836 sprintf (buf, "flt(%s)", t1);
5837 break;
5838 case UNSIGNED_FLOAT:
5839 print_value (t1, XEXP (x, 0), verbose);
5840 if (verbose)
5841 sprintf (buf, "uns_float(%s)", t1);
5842 else
5843 sprintf (buf, "ufl(%s)", t1);
5844 break;
5845 case FIX:
5846 print_value (t1, XEXP (x, 0), verbose);
5847 sprintf (buf, "fix(%s)", t1);
5848 break;
5849 case UNSIGNED_FIX:
5850 print_value (t1, XEXP (x, 0), verbose);
5851 if (verbose)
5852 sprintf (buf, "uns_fix(%s)", t1);
5853 else
5854 sprintf (buf, "ufx(%s)", t1);
5855 break;
5856 case PRE_DEC:
5857 print_value (t1, XEXP (x, 0), verbose);
5858 sprintf (buf, "--%s", t1);
5859 break;
5860 case PRE_INC:
5861 print_value (t1, XEXP (x, 0), verbose);
5862 sprintf (buf, "++%s", t1);
5863 break;
5864 case POST_DEC:
5865 print_value (t1, XEXP (x, 0), verbose);
5866 sprintf (buf, "%s--", t1);
5867 break;
5868 case POST_INC:
5869 print_value (t1, XEXP (x, 0), verbose);
5870 sprintf (buf, "%s++", t1);
5871 break;
5872 case CALL:
5873 print_value (t1, XEXP (x, 0), verbose);
5874 if (verbose)
5876 print_value (t2, XEXP (x, 1), verbose);
5877 sprintf (buf, "call %s argc:%s", t1, t2);
5879 else
5880 sprintf (buf, "call %s", t1);
5881 break;
5882 case IF_THEN_ELSE:
5883 print_exp (t1, XEXP (x, 0), verbose);
5884 print_value (t2, XEXP (x, 1), verbose);
5885 print_value (t3, XEXP (x, 2), verbose);
5886 sprintf (buf, "{(%s)?%s:%s}", t1, t2, t3);
5887 break;
5888 case TRAP_IF:
5889 print_value (t1, TRAP_CONDITION (x), verbose);
5890 sprintf (buf, "trap_if %s", t1);
5891 break;
5892 case UNSPEC:
5894 int i;
5896 sprintf (t1, "unspec{");
5897 for (i = 0; i < XVECLEN (x, 0); i++)
5899 print_pattern (t2, XVECEXP (x, 0, i), verbose);
5900 sprintf (t3, "%s%s;", t1, t2);
5901 strcpy (t1, t3);
5903 sprintf (buf, "%s}", t1);
5905 break;
5906 case UNSPEC_VOLATILE:
5908 int i;
5910 sprintf (t1, "unspec/v{");
5911 for (i = 0; i < XVECLEN (x, 0); i++)
5913 print_pattern (t2, XVECEXP (x, 0, i), verbose);
5914 sprintf (t3, "%s%s;", t1, t2);
5915 strcpy (t1, t3);
5917 sprintf (buf, "%s}", t1);
5919 break;
5920 default:
5921 /* if (verbose) debug_rtx (x); else sprintf (buf, "$$$"); */
5922 sprintf (buf, "$$$");
5924 } /* print_exp */
5926 /* Prints rtxes, i customly classified as values. They're constants, */
5927 /* registers, labels, symbols and memory accesses. */
5929 static void
5930 print_value (buf, x, verbose)
5931 char *buf;
5932 rtx x;
5933 int verbose;
5935 char t[BUF_LEN];
5937 switch (GET_CODE (x))
5939 case CONST_INT:
5940 sprintf (buf, "%Xh", INTVAL (x));
5941 break;
5942 case CONST_DOUBLE:
5943 print_value (t, XEXP (x, 0), verbose);
5944 sprintf (buf, "<%s>", t);
5945 break;
5946 case CONST_STRING:
5947 sprintf (buf, "\"%s\"", (char *) XEXP (x, 0));
5948 break;
5949 case SYMBOL_REF:
5950 sprintf (buf, "`%s'", (char *) XEXP (x, 0));
5951 break;
5952 case LABEL_REF:
5953 sprintf (buf, "L%d", INSN_UID (XEXP (x, 0)));
5954 break;
5955 case CONST:
5956 print_value (buf, XEXP (x, 0), verbose);
5957 break;
5958 case HIGH:
5959 print_value (buf, XEXP (x, 0), verbose);
5960 break;
5961 case REG:
5962 if (GET_MODE (x) == SFmode
5963 || GET_MODE (x) == DFmode
5964 || GET_MODE (x) == XFmode
5965 || GET_MODE (x) == TFmode)
5966 strcpy (t, "fr");
5967 else
5968 strcpy (t, "r");
5969 sprintf (buf, "%s%d", t, REGNO (x));
5970 break;
5971 case SUBREG:
5972 print_value (t, XEXP (x, 0), verbose);
5973 sprintf (buf, "%s#%d", t, SUBREG_WORD (x));
5974 break;
5975 case SCRATCH:
5976 sprintf (buf, "scratch");
5977 break;
5978 case CC0:
5979 sprintf (buf, "cc0");
5980 break;
5981 case PC:
5982 sprintf (buf, "pc");
5983 break;
5984 case MEM:
5985 print_value (t, XEXP (x, 0), verbose);
5986 sprintf (buf, "[%s]", t);
5987 break;
5988 default:
5989 print_exp (buf, x, verbose);
5991 } /* print_value */
5993 /* The next step in insn detalization, its pattern recognition */
5995 static void
5996 print_pattern (buf, x, verbose)
5997 char *buf;
5998 rtx x;
5999 int verbose;
6001 char t1[BUF_LEN], t2[BUF_LEN], t3[BUF_LEN];
6003 switch (GET_CODE (x))
6005 case SET:
6006 print_value (t1, SET_DEST (x), verbose);
6007 print_value (t2, SET_SRC (x), verbose);
6008 sprintf (buf, "%s=%s", t1, t2);
6009 break;
6010 case RETURN:
6011 sprintf (buf, "return");
6012 break;
6013 case CALL:
6014 print_exp (buf, x, verbose);
6015 break;
6016 case CLOBBER:
6017 print_value (t1, XEXP (x, 0), verbose);
6018 sprintf (buf, "clobber %s", t1);
6019 break;
6020 case USE:
6021 print_value (t1, XEXP (x, 0), verbose);
6022 sprintf (buf, "use %s", t1);
6023 break;
6024 case PARALLEL:
6026 int i;
6028 sprintf (t1, "{");
6029 for (i = 0; i < XVECLEN (x, 0); i++)
6031 print_pattern (t2, XVECEXP (x, 0, i), verbose);
6032 sprintf (t3, "%s%s;", t1, t2);
6033 strcpy (t1, t3);
6035 sprintf (buf, "%s}", t1);
6037 break;
6038 case SEQUENCE:
6040 int i;
6042 sprintf (t1, "%%{");
6043 for (i = 0; i < XVECLEN (x, 0); i++)
6045 print_insn (t2, XVECEXP (x, 0, i), verbose);
6046 sprintf (t3, "%s%s;", t1, t2);
6047 strcpy (t1, t3);
6049 sprintf (buf, "%s%%}", t1);
6051 break;
6052 case ASM_INPUT:
6053 sprintf (buf, "asm {%s}", XEXP (x, 0));
6054 break;
6055 case ADDR_VEC:
6056 break;
6057 case ADDR_DIFF_VEC:
6058 print_value (buf, XEXP (x, 0), verbose);
6059 break;
6060 case TRAP_IF:
6061 print_value (t1, TRAP_CONDITION (x), verbose);
6062 sprintf (buf, "trap_if %s", t1);
6063 break;
6064 case UNSPEC:
6066 int i;
6068 sprintf (t1, "unspec{");
6069 for (i = 0; i < XVECLEN (x, 0); i++)
6071 print_pattern (t2, XVECEXP (x, 0, i), verbose);
6072 sprintf (t3, "%s%s;", t1, t2);
6073 strcpy (t1, t3);
6075 sprintf (buf, "%s}", t1);
6077 break;
6078 case UNSPEC_VOLATILE:
6080 int i;
6082 sprintf (t1, "unspec/v{");
6083 for (i = 0; i < XVECLEN (x, 0); i++)
6085 print_pattern (t2, XVECEXP (x, 0, i), verbose);
6086 sprintf (t3, "%s%s;", t1, t2);
6087 strcpy (t1, t3);
6089 sprintf (buf, "%s}", t1);
6091 break;
6092 default:
6093 print_value (buf, x, verbose);
6095 } /* print_pattern */
6097 /* This is the main function in rtl visualization mechanism. It
6098 accepts an rtx and tries to recognize it as an insn, then prints it
6099 properly in human readable form, resembling assembler mnemonics. */
6100 /* For every insn it prints its UID and BB the insn belongs */
6101 /* too. (probably the last "option" should be extended somehow, since */
6102 /* it depends now on sched.c inner variables ...) */
6104 static void
6105 print_insn (buf, x, verbose)
6106 char *buf;
6107 rtx x;
6108 int verbose;
6110 char t[BUF_LEN];
6111 rtx insn = x;
6113 switch (GET_CODE (x))
6115 case INSN:
6116 print_pattern (t, PATTERN (x), verbose);
6117 if (verbose)
6118 sprintf (buf, "b%d: i% 4d: %s", INSN_BB (x),
6119 INSN_UID (x), t);
6120 else
6121 sprintf (buf, "%-4d %s", INSN_UID (x), t);
6122 break;
6123 case JUMP_INSN:
6124 print_pattern (t, PATTERN (x), verbose);
6125 if (verbose)
6126 sprintf (buf, "b%d: i% 4d: jump %s", INSN_BB (x),
6127 INSN_UID (x), t);
6128 else
6129 sprintf (buf, "%-4d %s", INSN_UID (x), t);
6130 break;
6131 case CALL_INSN:
6132 x = PATTERN (insn);
6133 if (GET_CODE (x) == PARALLEL)
6135 x = XVECEXP (x, 0, 0);
6136 print_pattern (t, x, verbose);
6138 else
6139 strcpy (t, "call <...>");
6140 if (verbose)
6141 sprintf (buf, "b%d: i% 4d: %s", INSN_BB (insn),
6142 INSN_UID (insn), t);
6143 else
6144 sprintf (buf, "%-4d %s", INSN_UID (insn), t);
6145 break;
6146 case CODE_LABEL:
6147 sprintf (buf, "L%d:", INSN_UID (x));
6148 break;
6149 case BARRIER:
6150 sprintf (buf, "i% 4d: barrier", INSN_UID (x));
6151 break;
6152 case NOTE:
6153 if (NOTE_LINE_NUMBER (x) > 0)
6154 sprintf (buf, "%4d note \"%s\" %d", INSN_UID (x),
6155 NOTE_SOURCE_FILE (x), NOTE_LINE_NUMBER (x));
6156 else
6157 sprintf (buf, "%4d %s", INSN_UID (x),
6158 GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (x)));
6159 break;
6160 default:
6161 if (verbose)
6163 sprintf (buf, "Not an INSN at all\n");
6164 debug_rtx (x);
6166 else
6167 sprintf (buf, "i%-4d <What?>", INSN_UID (x));
6169 } /* print_insn */
6171 void
6172 print_insn_chain (rtx_first)
6173 rtx rtx_first;
6175 register rtx tmp_rtx;
6176 char str[BUF_LEN];
6178 strcpy (str, "(nil)\n");
6179 if (rtx_first != 0)
6180 switch (GET_CODE (rtx_first))
6182 case INSN:
6183 case JUMP_INSN:
6184 case CALL_INSN:
6185 case NOTE:
6186 case CODE_LABEL:
6187 case BARRIER:
6188 for (tmp_rtx = rtx_first; tmp_rtx != NULL;
6189 tmp_rtx = NEXT_INSN (tmp_rtx))
6191 print_insn (str, tmp_rtx, 0);
6192 printf ("%s\n", str);
6194 break;
6195 default:
6196 print_insn (str, rtx_first, 0);
6197 printf ("%s\n", str);
6199 } /* print_insn_chain */
6201 /* Print visualization debugging info */
6203 static void
6204 print_block_visualization (b, s)
6205 int b;
6206 char *s;
6208 int unit, i;
6210 /* print header */
6211 fprintf (dump, "\n;; ==================== scheduling visualization for block %d %s \n", b, s);
6213 /* Print names of units */
6214 fprintf (dump, ";; %-8s", "clock");
6215 for (unit = 0; unit < FUNCTION_UNITS_SIZE; unit++)
6216 if (function_units[unit].bitmask & target_units)
6217 for (i = 0; i < function_units[unit].multiplicity; i++)
6218 fprintf (dump, " %-33s", function_units[unit].name);
6219 fprintf (dump, " %-8s\n", "no-unit");
6221 fprintf (dump, ";; %-8s", "=====");
6222 for (unit = 0; unit < FUNCTION_UNITS_SIZE; unit++)
6223 if (function_units[unit].bitmask & target_units)
6224 for (i = 0; i < function_units[unit].multiplicity; i++)
6225 fprintf (dump, " %-33s", "==============================");
6226 fprintf (dump, " %-8s\n", "=======");
6228 /* Print insns in each cycle */
6229 fprintf (dump, "%s\n", visual_tbl);
6232 /* Print insns in the 'no_unit' column of visualization */
6234 static void
6235 visualize_no_unit (insn)
6236 rtx insn;
6238 vis_no_unit[n_vis_no_unit] = insn;
6239 n_vis_no_unit++;
6242 /* Print insns scheduled in clock, for visualization. */
6244 static void
6245 visualize_scheduled_insns (b, clock)
6246 int b, clock;
6248 int i, unit;
6250 /* if no more room, split table into two */
6251 if (n_visual_lines >= MAX_VISUAL_LINES)
6253 print_block_visualization (b, "(incomplete)");
6254 init_block_visualization ();
6257 n_visual_lines++;
6259 sprintf (visual_tbl + strlen (visual_tbl), ";; %-8d", clock);
6260 for (unit = 0; unit < FUNCTION_UNITS_SIZE; unit++)
6261 if (function_units[unit].bitmask & target_units)
6262 for (i = 0; i < function_units[unit].multiplicity; i++)
6264 int instance = unit + i * FUNCTION_UNITS_SIZE;
6265 rtx insn = unit_last_insn[instance];
6267 /* print insns that still keep the unit busy */
6268 if (insn &&
6269 actual_hazard_this_instance (unit, instance, insn, clock, 0))
6271 char str[BUF_LEN];
6272 print_insn (str, insn, 0);
6273 str[INSN_LEN] = '\0';
6274 sprintf (visual_tbl + strlen (visual_tbl), " %-33s", str);
6276 else
6277 sprintf (visual_tbl + strlen (visual_tbl), " %-33s", "------------------------------");
6280 /* print insns that are not assigned to any unit */
6281 for (i = 0; i < n_vis_no_unit; i++)
6282 sprintf (visual_tbl + strlen (visual_tbl), " %-8d",
6283 INSN_UID (vis_no_unit[i]));
6284 n_vis_no_unit = 0;
6286 sprintf (visual_tbl + strlen (visual_tbl), "\n");
6289 /* Print stalled cycles */
6291 static void
6292 visualize_stall_cycles (b, stalls)
6293 int b, stalls;
6295 int i;
6297 /* if no more room, split table into two */
6298 if (n_visual_lines >= MAX_VISUAL_LINES)
6300 print_block_visualization (b, "(incomplete)");
6301 init_block_visualization ();
6304 n_visual_lines++;
6306 sprintf (visual_tbl + strlen (visual_tbl), ";; ");
6307 for (i = 0; i < stalls; i++)
6308 sprintf (visual_tbl + strlen (visual_tbl), ".");
6309 sprintf (visual_tbl + strlen (visual_tbl), "\n");
6312 /* move_insn1: Remove INSN from insn chain, and link it after LAST insn */
6314 static rtx
6315 move_insn1 (insn, last)
6316 rtx insn, last;
6318 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
6319 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
6321 NEXT_INSN (insn) = NEXT_INSN (last);
6322 PREV_INSN (NEXT_INSN (last)) = insn;
6324 NEXT_INSN (last) = insn;
6325 PREV_INSN (insn) = last;
6327 return insn;
6330 /* Search INSN for fake REG_DEAD note pairs for NOTE_INSN_SETJMP,
6331 NOTE_INSN_{LOOP,EHREGION}_{BEG,END}; and convert them back into
6332 NOTEs. The REG_DEAD note following first one is contains the saved
6333 value for NOTE_BLOCK_NUMBER which is useful for
6334 NOTE_INSN_EH_REGION_{BEG,END} NOTEs. LAST is the last instruction
6335 output by the instruction scheduler. Return the new value of LAST. */
6337 static rtx
6338 reemit_notes (insn, last)
6339 rtx insn;
6340 rtx last;
6342 rtx note, retval;
6344 retval = last;
6345 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
6347 if (REG_NOTE_KIND (note) == REG_DEAD
6348 && GET_CODE (XEXP (note, 0)) == CONST_INT)
6350 if (INTVAL (XEXP (note, 0)) == NOTE_INSN_SETJMP)
6352 retval = emit_note_after (INTVAL (XEXP (note, 0)), insn);
6353 CONST_CALL_P (retval) = CONST_CALL_P (note);
6354 remove_note (insn, note);
6355 note = XEXP (note, 1);
6357 else
6359 last = emit_note_before (INTVAL (XEXP (note, 0)), last);
6360 remove_note (insn, note);
6361 note = XEXP (note, 1);
6362 NOTE_BLOCK_NUMBER (last) = INTVAL (XEXP (note, 0));
6364 remove_note (insn, note);
6367 return retval;
6370 /* Move INSN, and all insns which should be issued before it,
6371 due to SCHED_GROUP_P flag. Reemit notes if needed.
6373 Return the last insn emitted by the scheduler, which is the
6374 return value from the first call to reemit_notes. */
6376 static rtx
6377 move_insn (insn, last)
6378 rtx insn, last;
6380 rtx retval = NULL;
6382 /* If INSN has SCHED_GROUP_P set, then issue it and any other
6383 insns with SCHED_GROUP_P set first. */
6384 while (SCHED_GROUP_P (insn))
6386 rtx prev = PREV_INSN (insn);
6388 /* Move a SCHED_GROUP_P insn. */
6389 move_insn1 (insn, last);
6390 /* If this is the first call to reemit_notes, then record
6391 its return value. */
6392 if (retval == NULL_RTX)
6393 retval = reemit_notes (insn, insn);
6394 else
6395 reemit_notes (insn, insn);
6396 insn = prev;
6399 /* Now move the first non SCHED_GROUP_P insn. */
6400 move_insn1 (insn, last);
6402 /* If this is the first call to reemit_notes, then record
6403 its return value. */
6404 if (retval == NULL_RTX)
6405 retval = reemit_notes (insn, insn);
6406 else
6407 reemit_notes (insn, insn);
6409 return retval;
6412 /* Return an insn which represents a SCHED_GROUP, which is
6413 the last insn in the group. */
6415 static rtx
6416 group_leader (insn)
6417 rtx insn;
6419 rtx prev;
6423 prev = insn;
6424 insn = next_nonnote_insn (insn);
6426 while (insn && SCHED_GROUP_P (insn) && (GET_CODE (insn) != CODE_LABEL));
6428 return prev;
6431 /* Use forward list scheduling to rearrange insns of block BB in region RGN,
6432 possibly bringing insns from subsequent blocks in the same region.
6433 Return number of insns scheduled. */
6435 static int
6436 schedule_block (bb, rgn_n_insns)
6437 int bb;
6438 int rgn_n_insns;
6440 /* Local variables. */
6441 rtx insn, last;
6442 rtx *ready;
6443 int i;
6444 int n_ready = 0;
6445 int can_issue_more;
6447 /* flow block of this bb */
6448 int b = BB_TO_BLOCK (bb);
6450 /* target_n_insns == number of insns in b before scheduling starts.
6451 sched_target_n_insns == how many of b's insns were scheduled.
6452 sched_n_insns == how many insns were scheduled in b */
6453 int target_n_insns = 0;
6454 int sched_target_n_insns = 0;
6455 int sched_n_insns = 0;
6457 #define NEED_NOTHING 0
6458 #define NEED_HEAD 1
6459 #define NEED_TAIL 2
6460 int new_needs;
6462 /* head/tail info for this block */
6463 rtx prev_head;
6464 rtx next_tail;
6465 rtx head;
6466 rtx tail;
6467 int bb_src;
6469 /* We used to have code to avoid getting parameters moved from hard
6470 argument registers into pseudos.
6472 However, it was removed when it proved to be of marginal benefit
6473 and caused problems because schedule_block and compute_forward_dependences
6474 had different notions of what the "head" insn was. */
6475 get_block_head_tail (bb, &head, &tail);
6477 /* Interblock scheduling could have moved the original head insn from this
6478 block into a proceeding block. This may also cause schedule_block and
6479 compute_forward_dependences to have different notions of what the
6480 "head" insn was.
6482 If the interblock movement happened to make this block start with
6483 some notes (LOOP, EH or SETJMP) before the first real insn, then
6484 HEAD will have various special notes attached to it which must be
6485 removed so that we don't end up with extra copies of the notes. */
6486 if (GET_RTX_CLASS (GET_CODE (head)) == 'i')
6488 rtx note;
6490 for (note = REG_NOTES (head); note; note = XEXP (note, 1))
6491 if (REG_NOTE_KIND (note) == REG_DEAD
6492 && GET_CODE (XEXP (note, 0)) == CONST_INT)
6493 remove_note (head, note);
6496 next_tail = NEXT_INSN (tail);
6497 prev_head = PREV_INSN (head);
6499 /* If the only insn left is a NOTE or a CODE_LABEL, then there is no need
6500 to schedule this block. */
6501 if (head == tail
6502 && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
6503 return (sched_n_insns);
6505 /* debug info */
6506 if (sched_verbose)
6508 fprintf (dump, ";; ======================================================\n");
6509 fprintf (dump,
6510 ";; -- basic block %d from %d to %d -- %s reload\n",
6511 b, INSN_UID (basic_block_head[b]),
6512 INSN_UID (basic_block_end[b]),
6513 (reload_completed ? "after" : "before"));
6514 fprintf (dump, ";; ======================================================\n");
6515 if (sched_debug_count >= 0)
6516 fprintf (dump, ";;\t -- sched_debug_count=%d\n", sched_debug_count);
6517 fprintf (dump, "\n");
6519 visual_tbl = (char *) alloca (get_visual_tbl_length ());
6520 init_block_visualization ();
6523 /* remove remaining note insns from the block, save them in
6524 note_list. These notes are restored at the end of
6525 schedule_block (). */
6526 note_list = 0;
6527 rm_other_notes (head, tail);
6529 target_bb = bb;
6531 /* prepare current target block info */
6532 if (current_nr_blocks > 1)
6534 candidate_table = (candidate *) alloca (current_nr_blocks * sizeof (candidate));
6536 bblst_last = 0;
6537 /* ??? It is not clear why bblst_size is computed this way. The original
6538 number was clearly too small as it resulted in compiler failures.
6539 Multiplying by the original number by 2 (to account for update_bbs
6540 members) seems to be a reasonable solution. */
6541 /* ??? Or perhaps there is a bug somewhere else in this file? */
6542 bblst_size = (current_nr_blocks - bb) * rgn_nr_edges * 2;
6543 bblst_table = (int *) alloca (bblst_size * sizeof (int));
6545 bitlst_table_last = 0;
6546 bitlst_table_size = rgn_nr_edges;
6547 bitlst_table = (int *) alloca (rgn_nr_edges * sizeof (int));
6549 compute_trg_info (bb);
6552 clear_units ();
6554 /* Allocate the ready list */
6555 ready = (rtx *) alloca ((rgn_n_insns + 1) * sizeof (rtx));
6557 /* Print debugging information. */
6558 if (sched_verbose >= 5)
6559 debug_dependencies ();
6562 /* Initialize ready list with all 'ready' insns in target block.
6563 Count number of insns in the target block being scheduled. */
6564 n_ready = 0;
6565 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
6567 rtx next;
6569 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
6570 continue;
6571 next = NEXT_INSN (insn);
6573 if (INSN_DEP_COUNT (insn) == 0
6574 && (SCHED_GROUP_P (next) == 0 || GET_RTX_CLASS (GET_CODE (next)) != 'i'))
6575 ready[n_ready++] = insn;
6576 if (!(SCHED_GROUP_P (insn)))
6577 target_n_insns++;
6580 /* Add to ready list all 'ready' insns in valid source blocks.
6581 For speculative insns, check-live, exception-free, and
6582 issue-delay. */
6583 for (bb_src = bb + 1; bb_src < current_nr_blocks; bb_src++)
6584 if (IS_VALID (bb_src))
6586 rtx src_head;
6587 rtx src_next_tail;
6588 rtx tail, head;
6590 get_block_head_tail (bb_src, &head, &tail);
6591 src_next_tail = NEXT_INSN (tail);
6592 src_head = head;
6594 if (head == tail
6595 && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
6596 continue;
6598 for (insn = src_head; insn != src_next_tail; insn = NEXT_INSN (insn))
6600 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
6601 continue;
6603 if (!CANT_MOVE (insn)
6604 && (!IS_SPECULATIVE_INSN (insn)
6605 || (insn_issue_delay (insn) <= 3
6606 && check_live (insn, bb_src)
6607 && is_exception_free (insn, bb_src, target_bb))))
6610 rtx next;
6612 next = NEXT_INSN (insn);
6613 if (INSN_DEP_COUNT (insn) == 0
6614 && (SCHED_GROUP_P (next) == 0
6615 || GET_RTX_CLASS (GET_CODE (next)) != 'i'))
6616 ready[n_ready++] = insn;
6621 /* no insns scheduled in this block yet */
6622 last_scheduled_insn = 0;
6624 /* Sort the ready list */
6625 SCHED_SORT (ready, n_ready);
6627 if (sched_verbose >= 2)
6629 fprintf (dump, ";;\t\tReady list initially: ");
6630 debug_ready_list (ready, n_ready);
6633 /* Q_SIZE is the total number of insns in the queue. */
6634 q_ptr = 0;
6635 q_size = 0;
6636 clock_var = 0;
6637 bzero ((char *) insn_queue, sizeof (insn_queue));
6639 /* We start inserting insns after PREV_HEAD. */
6640 last = prev_head;
6642 /* Initialize INSN_QUEUE, LIST and NEW_NEEDS. */
6643 new_needs = (NEXT_INSN (prev_head) == basic_block_head[b]
6644 ? NEED_HEAD : NEED_NOTHING);
6645 if (PREV_INSN (next_tail) == basic_block_end[b])
6646 new_needs |= NEED_TAIL;
6648 /* loop until all the insns in BB are scheduled. */
6649 while (sched_target_n_insns < target_n_insns)
6651 int b1;
6653 #ifdef INTERBLOCK_DEBUG
6654 if (sched_debug_count == 0)
6655 break;
6656 #endif
6657 clock_var++;
6659 /* Add to the ready list all pending insns that can be issued now.
6660 If there are no ready insns, increment clock until one
6661 is ready and add all pending insns at that point to the ready
6662 list. */
6663 n_ready = queue_to_ready (ready, n_ready);
6665 if (n_ready == 0)
6666 abort ();
6668 if (sched_verbose >= 2)
6670 fprintf (dump, ";;\t\tReady list after queue_to_ready: ");
6671 debug_ready_list (ready, n_ready);
6674 /* Sort the ready list. */
6675 SCHED_SORT (ready, n_ready);
6677 if (sched_verbose)
6679 fprintf (dump, ";;\tReady list (t =%3d): ", clock_var);
6680 debug_ready_list (ready, n_ready);
6683 /* Issue insns from ready list.
6684 It is important to count down from n_ready, because n_ready may change
6685 as insns are issued. */
6686 can_issue_more = issue_rate;
6687 for (i = n_ready - 1; i >= 0 && can_issue_more; i--)
6689 rtx insn = ready[i];
6690 int cost = actual_hazard (insn_unit (insn), insn, clock_var, 0);
6692 if (cost > 1)
6694 queue_insn (insn, cost);
6695 ready[i] = ready[--n_ready]; /* remove insn from ready list */
6697 else if (cost == 0)
6699 #ifdef INTERBLOCK_DEBUG
6700 if (sched_debug_count == 0)
6701 break;
6702 #endif
6704 /* an interblock motion? */
6705 if (INSN_BB (insn) != target_bb)
6707 rtx temp;
6709 if (IS_SPECULATIVE_INSN (insn))
6712 if (!check_live (insn, INSN_BB (insn)))
6714 /* speculative motion, live check failed, remove
6715 insn from ready list */
6716 ready[i] = ready[--n_ready];
6717 continue;
6719 update_live (insn, INSN_BB (insn));
6721 /* for speculative load, mark insns fed by it. */
6722 if (IS_LOAD_INSN (insn) || FED_BY_SPEC_LOAD (insn))
6723 set_spec_fed (insn);
6725 nr_spec++;
6727 nr_inter++;
6729 temp = insn;
6730 while (SCHED_GROUP_P (temp))
6731 temp = PREV_INSN (temp);
6733 /* Update source block boundaries. */
6734 b1 = INSN_BLOCK (temp);
6735 if (temp == basic_block_head[b1]
6736 && insn == basic_block_end[b1])
6738 /* We moved all the insns in the basic block.
6739 Emit a note after the last insn and update the
6740 begin/end boundaries to point to the note. */
6741 emit_note_after (NOTE_INSN_DELETED, insn);
6742 basic_block_end[b1] = NEXT_INSN (insn);
6743 basic_block_head[b1] = NEXT_INSN (insn);
6745 else if (insn == basic_block_end[b1])
6747 /* We took insns from the end of the basic block,
6748 so update the end of block boundary so that it
6749 points to the first insn we did not move. */
6750 basic_block_end[b1] = PREV_INSN (temp);
6752 else if (temp == basic_block_head[b1])
6754 /* We took insns from the start of the basic block,
6755 so update the start of block boundary so that
6756 it points to the first insn we did not move. */
6757 basic_block_head[b1] = NEXT_INSN (insn);
6760 else
6762 /* in block motion */
6763 sched_target_n_insns++;
6766 last_scheduled_insn = insn;
6767 last = move_insn (insn, last);
6768 sched_n_insns++;
6770 can_issue_more--;
6772 #ifdef INTERBLOCK_DEBUG
6773 if (sched_debug_count > 0)
6774 sched_debug_count--;
6775 #endif
6777 n_ready = schedule_insn (insn, ready, n_ready, clock_var);
6779 /* remove insn from ready list */
6780 ready[i] = ready[--n_ready];
6782 /* close this block after scheduling its jump */
6783 if (GET_CODE (last_scheduled_insn) == JUMP_INSN)
6784 break;
6788 /* debug info */
6789 if (sched_verbose)
6791 visualize_scheduled_insns (b, clock_var);
6792 #ifdef INTERBLOCK_DEBUG
6793 if (sched_debug_count == 0)
6794 fprintf (dump, "........ sched_debug_count == 0 .................\n");
6795 #endif
6799 /* debug info */
6800 if (sched_verbose)
6802 fprintf (dump, ";;\tReady list (final): ");
6803 debug_ready_list (ready, n_ready);
6804 print_block_visualization (b, "");
6807 /* Sanity check -- queue must be empty now. Meaningless if region has
6808 multiple bbs, or if scheduling stopped by sched_debug_count. */
6809 if (current_nr_blocks > 1)
6810 #ifdef INTERBLOCK_DEBUG
6811 if (sched_debug_count != 0)
6812 #endif
6813 if (!flag_schedule_interblock && q_size != 0)
6814 abort ();
6816 /* update head/tail boundaries. */
6817 head = NEXT_INSN (prev_head);
6818 tail = last;
6820 #ifdef INTERBLOCK_DEBUG
6821 if (sched_debug_count == 0)
6822 /* compensate for stopping scheduling prematurely */
6823 for (i = sched_target_n_insns; i < target_n_insns; i++)
6824 tail = move_insn (group_leader (NEXT_INSN (tail)), tail);
6825 #endif
6827 /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
6828 previously found among the insns. Insert them at the beginning
6829 of the insns. */
6830 if (note_list != 0)
6832 rtx note_head = note_list;
6834 while (PREV_INSN (note_head))
6836 note_head = PREV_INSN (note_head);
6839 PREV_INSN (note_head) = PREV_INSN (head);
6840 NEXT_INSN (PREV_INSN (head)) = note_head;
6841 PREV_INSN (head) = note_list;
6842 NEXT_INSN (note_list) = head;
6843 head = note_head;
6846 /* update target block boundaries. */
6847 if (new_needs & NEED_HEAD)
6848 basic_block_head[b] = head;
6850 if (new_needs & NEED_TAIL)
6851 basic_block_end[b] = tail;
6853 /* debugging */
6854 if (sched_verbose)
6856 fprintf (dump, ";; total time = %d\n;; new basic block head = %d\n",
6857 clock_var, INSN_UID (basic_block_head[b]));
6858 fprintf (dump, ";; new basic block end = %d\n\n",
6859 INSN_UID (basic_block_end[b]));
6862 return (sched_n_insns);
6863 } /* schedule_block () */
6866 /* print the bit-set of registers, S. callable from debugger */
6868 extern void
6869 debug_reg_vector (s)
6870 regset s;
6872 int regno;
6874 EXECUTE_IF_SET_IN_REG_SET (s, 0, regno,
6876 fprintf (dump, " %d", regno);
6879 fprintf (dump, "\n");
6882 /* Use the backward dependences from LOG_LINKS to build
6883 forward dependences in INSN_DEPEND. */
6885 static void
6886 compute_block_forward_dependences (bb)
6887 int bb;
6889 rtx insn, link;
6890 rtx tail, head;
6891 rtx next_tail;
6892 enum reg_note dep_type;
6894 get_block_head_tail (bb, &head, &tail);
6895 next_tail = NEXT_INSN (tail);
6896 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
6898 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
6899 continue;
6901 insn = group_leader (insn);
6903 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
6905 rtx x = group_leader (XEXP (link, 0));
6906 rtx new_link;
6908 if (x != XEXP (link, 0))
6909 continue;
6911 /* Ignore dependences upon deleted insn */
6912 if (GET_CODE (x) == NOTE || INSN_DELETED_P (x))
6913 continue;
6914 if (find_insn_list (insn, INSN_DEPEND (x)))
6915 continue;
6917 new_link = alloc_INSN_LIST (insn, INSN_DEPEND (x));
6919 dep_type = REG_NOTE_KIND (link);
6920 PUT_REG_NOTE_KIND (new_link, dep_type);
6922 INSN_DEPEND (x) = new_link;
6923 INSN_DEP_COUNT (insn) += 1;
6928 /* Initialize variables for region data dependence analysis.
6929 n_bbs is the number of region blocks */
6931 __inline static void
6932 init_rgn_data_dependences (n_bbs)
6933 int n_bbs;
6935 int bb;
6937 /* variables for which one copy exists for each block */
6938 bzero ((char *) bb_pending_read_insns, n_bbs * sizeof (rtx));
6939 bzero ((char *) bb_pending_read_mems, n_bbs * sizeof (rtx));
6940 bzero ((char *) bb_pending_write_insns, n_bbs * sizeof (rtx));
6941 bzero ((char *) bb_pending_write_mems, n_bbs * sizeof (rtx));
6942 bzero ((char *) bb_pending_lists_length, n_bbs * sizeof (rtx));
6943 bzero ((char *) bb_last_pending_memory_flush, n_bbs * sizeof (rtx));
6944 bzero ((char *) bb_last_function_call, n_bbs * sizeof (rtx));
6945 bzero ((char *) bb_sched_before_next_call, n_bbs * sizeof (rtx));
6947 /* Create an insn here so that we can hang dependencies off of it later. */
6948 for (bb = 0; bb < n_bbs; bb++)
6950 bb_sched_before_next_call[bb] =
6951 gen_rtx_INSN (VOIDmode, 0, NULL_RTX, NULL_RTX,
6952 NULL_RTX, 0, NULL_RTX, NULL_RTX);
6953 LOG_LINKS (bb_sched_before_next_call[bb]) = 0;
6957 /* Add dependences so that branches are scheduled to run last in their block */
6959 static void
6960 add_branch_dependences (head, tail)
6961 rtx head, tail;
6964 rtx insn, last;
6966 /* For all branches, calls, uses, and cc0 setters, force them to remain
6967 in order at the end of the block by adding dependencies and giving
6968 the last a high priority. There may be notes present, and prev_head
6969 may also be a note.
6971 Branches must obviously remain at the end. Calls should remain at the
6972 end since moving them results in worse register allocation. Uses remain
6973 at the end to ensure proper register allocation. cc0 setters remaim
6974 at the end because they can't be moved away from their cc0 user. */
6975 insn = tail;
6976 last = 0;
6977 while (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
6978 || (GET_CODE (insn) == INSN
6979 && (GET_CODE (PATTERN (insn)) == USE
6980 #ifdef HAVE_cc0
6981 || sets_cc0_p (PATTERN (insn))
6982 #endif
6984 || GET_CODE (insn) == NOTE)
6986 if (GET_CODE (insn) != NOTE)
6988 if (last != 0
6989 && !find_insn_list (insn, LOG_LINKS (last)))
6991 add_dependence (last, insn, REG_DEP_ANTI);
6992 INSN_REF_COUNT (insn)++;
6995 CANT_MOVE (insn) = 1;
6997 last = insn;
6998 /* Skip over insns that are part of a group.
6999 Make each insn explicitly depend on the previous insn.
7000 This ensures that only the group header will ever enter
7001 the ready queue (and, when scheduled, will automatically
7002 schedule the SCHED_GROUP_P block). */
7003 while (SCHED_GROUP_P (insn))
7005 rtx temp = prev_nonnote_insn (insn);
7006 add_dependence (insn, temp, REG_DEP_ANTI);
7007 insn = temp;
7011 /* Don't overrun the bounds of the basic block. */
7012 if (insn == head)
7013 break;
7015 insn = PREV_INSN (insn);
7018 /* make sure these insns are scheduled last in their block */
7019 insn = last;
7020 if (insn != 0)
7021 while (insn != head)
7023 insn = prev_nonnote_insn (insn);
7025 if (INSN_REF_COUNT (insn) != 0)
7026 continue;
7028 if (!find_insn_list (last, LOG_LINKS (insn)))
7029 add_dependence (last, insn, REG_DEP_ANTI);
7030 INSN_REF_COUNT (insn) = 1;
7032 /* Skip over insns that are part of a group. */
7033 while (SCHED_GROUP_P (insn))
7034 insn = prev_nonnote_insn (insn);
7038 /* Compute bacward dependences inside BB. In a multiple blocks region:
7039 (1) a bb is analyzed after its predecessors, and (2) the lists in
7040 effect at the end of bb (after analyzing for bb) are inherited by
7041 bb's successrs.
7043 Specifically for reg-reg data dependences, the block insns are
7044 scanned by sched_analyze () top-to-bottom. Two lists are
7045 naintained by sched_analyze (): reg_last_defs[] for register DEFs,
7046 and reg_last_uses[] for register USEs.
7048 When analysis is completed for bb, we update for its successors:
7049 ; - DEFS[succ] = Union (DEFS [succ], DEFS [bb])
7050 ; - USES[succ] = Union (USES [succ], DEFS [bb])
7052 The mechanism for computing mem-mem data dependence is very
7053 similar, and the result is interblock dependences in the region. */
7055 static void
7056 compute_block_backward_dependences (bb)
7057 int bb;
7059 int b;
7060 rtx x;
7061 rtx head, tail;
7062 int max_reg = max_reg_num ();
7064 b = BB_TO_BLOCK (bb);
7066 if (current_nr_blocks == 1)
7068 reg_last_uses = (rtx *) alloca (max_reg * sizeof (rtx));
7069 reg_last_sets = (rtx *) alloca (max_reg * sizeof (rtx));
7071 bzero ((char *) reg_last_uses, max_reg * sizeof (rtx));
7072 bzero ((char *) reg_last_sets, max_reg * sizeof (rtx));
7074 pending_read_insns = 0;
7075 pending_read_mems = 0;
7076 pending_write_insns = 0;
7077 pending_write_mems = 0;
7078 pending_lists_length = 0;
7079 last_function_call = 0;
7080 last_pending_memory_flush = 0;
7081 sched_before_next_call
7082 = gen_rtx_INSN (VOIDmode, 0, NULL_RTX, NULL_RTX,
7083 NULL_RTX, 0, NULL_RTX, NULL_RTX);
7084 LOG_LINKS (sched_before_next_call) = 0;
7086 else
7088 reg_last_uses = bb_reg_last_uses[bb];
7089 reg_last_sets = bb_reg_last_sets[bb];
7091 pending_read_insns = bb_pending_read_insns[bb];
7092 pending_read_mems = bb_pending_read_mems[bb];
7093 pending_write_insns = bb_pending_write_insns[bb];
7094 pending_write_mems = bb_pending_write_mems[bb];
7095 pending_lists_length = bb_pending_lists_length[bb];
7096 last_function_call = bb_last_function_call[bb];
7097 last_pending_memory_flush = bb_last_pending_memory_flush[bb];
7099 sched_before_next_call = bb_sched_before_next_call[bb];
7102 /* do the analysis for this block */
7103 get_block_head_tail (bb, &head, &tail);
7104 sched_analyze (head, tail);
7105 add_branch_dependences (head, tail);
7107 if (current_nr_blocks > 1)
7109 int e, first_edge;
7110 int b_succ, bb_succ;
7111 int reg;
7112 rtx link_insn, link_mem;
7113 rtx u;
7115 /* these lists should point to the right place, for correct freeing later. */
7116 bb_pending_read_insns[bb] = pending_read_insns;
7117 bb_pending_read_mems[bb] = pending_read_mems;
7118 bb_pending_write_insns[bb] = pending_write_insns;
7119 bb_pending_write_mems[bb] = pending_write_mems;
7121 /* bb's structures are inherited by it's successors */
7122 first_edge = e = OUT_EDGES (b);
7123 if (e > 0)
7126 b_succ = TO_BLOCK (e);
7127 bb_succ = BLOCK_TO_BB (b_succ);
7129 /* only bbs "below" bb, in the same region, are interesting */
7130 if (CONTAINING_RGN (b) != CONTAINING_RGN (b_succ)
7131 || bb_succ <= bb)
7133 e = NEXT_OUT (e);
7134 continue;
7137 for (reg = 0; reg < max_reg; reg++)
7140 /* reg-last-uses lists are inherited by bb_succ */
7141 for (u = reg_last_uses[reg]; u; u = XEXP (u, 1))
7143 if (find_insn_list (XEXP (u, 0), (bb_reg_last_uses[bb_succ])[reg]))
7144 continue;
7146 (bb_reg_last_uses[bb_succ])[reg]
7147 = alloc_INSN_LIST (XEXP (u, 0),
7148 (bb_reg_last_uses[bb_succ])[reg]);
7151 /* reg-last-defs lists are inherited by bb_succ */
7152 for (u = reg_last_sets[reg]; u; u = XEXP (u, 1))
7154 if (find_insn_list (XEXP (u, 0), (bb_reg_last_sets[bb_succ])[reg]))
7155 continue;
7157 (bb_reg_last_sets[bb_succ])[reg]
7158 = alloc_INSN_LIST (XEXP (u, 0),
7159 (bb_reg_last_sets[bb_succ])[reg]);
7163 /* mem read/write lists are inherited by bb_succ */
7164 link_insn = pending_read_insns;
7165 link_mem = pending_read_mems;
7166 while (link_insn)
7168 if (!(find_insn_mem_list (XEXP (link_insn, 0), XEXP (link_mem, 0),
7169 bb_pending_read_insns[bb_succ],
7170 bb_pending_read_mems[bb_succ])))
7171 add_insn_mem_dependence (&bb_pending_read_insns[bb_succ],
7172 &bb_pending_read_mems[bb_succ],
7173 XEXP (link_insn, 0), XEXP (link_mem, 0));
7174 link_insn = XEXP (link_insn, 1);
7175 link_mem = XEXP (link_mem, 1);
7178 link_insn = pending_write_insns;
7179 link_mem = pending_write_mems;
7180 while (link_insn)
7182 if (!(find_insn_mem_list (XEXP (link_insn, 0), XEXP (link_mem, 0),
7183 bb_pending_write_insns[bb_succ],
7184 bb_pending_write_mems[bb_succ])))
7185 add_insn_mem_dependence (&bb_pending_write_insns[bb_succ],
7186 &bb_pending_write_mems[bb_succ],
7187 XEXP (link_insn, 0), XEXP (link_mem, 0));
7189 link_insn = XEXP (link_insn, 1);
7190 link_mem = XEXP (link_mem, 1);
7193 /* last_function_call is inherited by bb_succ */
7194 for (u = last_function_call; u; u = XEXP (u, 1))
7196 if (find_insn_list (XEXP (u, 0), bb_last_function_call[bb_succ]))
7197 continue;
7199 bb_last_function_call[bb_succ]
7200 = alloc_INSN_LIST (XEXP (u, 0),
7201 bb_last_function_call[bb_succ]);
7204 /* last_pending_memory_flush is inherited by bb_succ */
7205 for (u = last_pending_memory_flush; u; u = XEXP (u, 1))
7207 if (find_insn_list (XEXP (u, 0), bb_last_pending_memory_flush[bb_succ]))
7208 continue;
7210 bb_last_pending_memory_flush[bb_succ]
7211 = alloc_INSN_LIST (XEXP (u, 0),
7212 bb_last_pending_memory_flush[bb_succ]);
7215 /* sched_before_next_call is inherited by bb_succ */
7216 x = LOG_LINKS (sched_before_next_call);
7217 for (; x; x = XEXP (x, 1))
7218 add_dependence (bb_sched_before_next_call[bb_succ],
7219 XEXP (x, 0), REG_DEP_ANTI);
7221 e = NEXT_OUT (e);
7223 while (e != first_edge);
7226 /* Free up the INSN_LISTs
7228 Note this loop is executed max_reg * nr_regions times. It's first
7229 implementation accounted for over 90% of the calls to free_list.
7230 The list was empty for the vast majority of those calls. On the PA,
7231 not calling free_list in those cases improves -O2 compile times by
7232 3-5% on average. */
7233 for (b = 0; b < max_reg; ++b)
7235 if (reg_last_sets[b])
7236 free_list (&reg_last_sets[b], &unused_insn_list);
7237 if (reg_last_uses[b])
7238 free_list (&reg_last_uses[b], &unused_insn_list);
7241 /* Assert that we won't need bb_reg_last_* for this block anymore. */
7242 if (current_nr_blocks > 1)
7244 bb_reg_last_uses[bb] = (rtx *) NULL_RTX;
7245 bb_reg_last_sets[bb] = (rtx *) NULL_RTX;
7249 /* Print dependences for debugging, callable from debugger */
7251 void
7252 debug_dependencies ()
7254 int bb;
7256 fprintf (dump, ";; --------------- forward dependences: ------------ \n");
7257 for (bb = 0; bb < current_nr_blocks; bb++)
7259 if (1)
7261 rtx head, tail;
7262 rtx next_tail;
7263 rtx insn;
7265 get_block_head_tail (bb, &head, &tail);
7266 next_tail = NEXT_INSN (tail);
7267 fprintf (dump, "\n;; --- Region Dependences --- b %d bb %d \n",
7268 BB_TO_BLOCK (bb), bb);
7270 fprintf (dump, ";; %7s%6s%6s%6s%6s%6s%11s%6s\n",
7271 "insn", "code", "bb", "dep", "prio", "cost", "blockage", "units");
7272 fprintf (dump, ";; %7s%6s%6s%6s%6s%6s%11s%6s\n",
7273 "----", "----", "--", "---", "----", "----", "--------", "-----");
7274 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
7276 rtx link;
7277 int unit, range;
7279 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
7281 int n;
7282 fprintf (dump, ";; %6d ", INSN_UID (insn));
7283 if (GET_CODE (insn) == NOTE)
7285 n = NOTE_LINE_NUMBER (insn);
7286 if (n < 0)
7287 fprintf (dump, "%s\n", GET_NOTE_INSN_NAME (n));
7288 else
7289 fprintf (dump, "line %d, file %s\n", n,
7290 NOTE_SOURCE_FILE (insn));
7292 else
7293 fprintf (dump, " {%s}\n", GET_RTX_NAME (GET_CODE (insn)));
7294 continue;
7297 unit = insn_unit (insn);
7298 range = (unit < 0
7299 || function_units[unit].blockage_range_function == 0) ? 0 :
7300 function_units[unit].blockage_range_function (insn);
7301 fprintf (dump,
7302 ";; %s%5d%6d%6d%6d%6d%6d %3d -%3d ",
7303 (SCHED_GROUP_P (insn) ? "+" : " "),
7304 INSN_UID (insn),
7305 INSN_CODE (insn),
7306 INSN_BB (insn),
7307 INSN_DEP_COUNT (insn),
7308 INSN_PRIORITY (insn),
7309 insn_cost (insn, 0, 0),
7310 (int) MIN_BLOCKAGE_COST (range),
7311 (int) MAX_BLOCKAGE_COST (range));
7312 insn_print_units (insn);
7313 fprintf (dump, "\t: ");
7314 for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
7315 fprintf (dump, "%d ", INSN_UID (XEXP (link, 0)));
7316 fprintf (dump, "\n");
7320 fprintf (dump, "\n");
7323 /* Set_priorities: compute priority of each insn in the block */
7325 static int
7326 set_priorities (bb)
7327 int bb;
7329 rtx insn;
7330 int n_insn;
7332 rtx tail;
7333 rtx prev_head;
7334 rtx head;
7336 get_block_head_tail (bb, &head, &tail);
7337 prev_head = PREV_INSN (head);
7339 if (head == tail
7340 && (GET_RTX_CLASS (GET_CODE (head)) != 'i'))
7341 return 0;
7343 n_insn = 0;
7344 for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
7347 if (GET_CODE (insn) == NOTE)
7348 continue;
7350 if (!(SCHED_GROUP_P (insn)))
7351 n_insn++;
7352 (void) priority (insn);
7355 return n_insn;
7358 /* Make each element of VECTOR point at an rtx-vector,
7359 taking the space for all those rtx-vectors from SPACE.
7360 SPACE is of type (rtx *), but it is really as long as NELTS rtx-vectors.
7361 BYTES_PER_ELT is the number of bytes in one rtx-vector.
7362 (this is the same as init_regset_vector () in flow.c) */
7364 static void
7365 init_rtx_vector (vector, space, nelts, bytes_per_elt)
7366 rtx **vector;
7367 rtx *space;
7368 int nelts;
7369 int bytes_per_elt;
7371 register int i;
7372 register rtx *p = space;
7374 for (i = 0; i < nelts; i++)
7376 vector[i] = p;
7377 p += bytes_per_elt / sizeof (*p);
7381 /* Schedule a region. A region is either an inner loop, a loop-free
7382 subroutine, or a single basic block. Each bb in the region is
7383 scheduled after its flow predecessors. */
7385 static void
7386 schedule_region (rgn)
7387 int rgn;
7389 int bb;
7390 int rgn_n_insns = 0;
7391 int sched_rgn_n_insns = 0;
7393 /* set variables for the current region */
7394 current_nr_blocks = RGN_NR_BLOCKS (rgn);
7395 current_blocks = RGN_BLOCKS (rgn);
7397 reg_pending_sets = ALLOCA_REG_SET ();
7398 reg_pending_sets_all = 0;
7400 /* initializations for region data dependence analyisis */
7401 if (current_nr_blocks > 1)
7403 rtx *space;
7404 int maxreg = max_reg_num ();
7406 bb_reg_last_uses = (rtx **) alloca (current_nr_blocks * sizeof (rtx *));
7407 space = (rtx *) alloca (current_nr_blocks * maxreg * sizeof (rtx));
7408 bzero ((char *) space, current_nr_blocks * maxreg * sizeof (rtx));
7409 init_rtx_vector (bb_reg_last_uses, space, current_nr_blocks, maxreg * sizeof (rtx *));
7411 bb_reg_last_sets = (rtx **) alloca (current_nr_blocks * sizeof (rtx *));
7412 space = (rtx *) alloca (current_nr_blocks * maxreg * sizeof (rtx));
7413 bzero ((char *) space, current_nr_blocks * maxreg * sizeof (rtx));
7414 init_rtx_vector (bb_reg_last_sets, space, current_nr_blocks, maxreg * sizeof (rtx *));
7416 bb_pending_read_insns = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7417 bb_pending_read_mems = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7418 bb_pending_write_insns = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7419 bb_pending_write_mems = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7420 bb_pending_lists_length = (int *) alloca (current_nr_blocks * sizeof (int));
7421 bb_last_pending_memory_flush = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7422 bb_last_function_call = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7423 bb_sched_before_next_call = (rtx *) alloca (current_nr_blocks * sizeof (rtx));
7425 init_rgn_data_dependences (current_nr_blocks);
7428 /* compute LOG_LINKS */
7429 for (bb = 0; bb < current_nr_blocks; bb++)
7430 compute_block_backward_dependences (bb);
7432 /* compute INSN_DEPEND */
7433 for (bb = current_nr_blocks - 1; bb >= 0; bb--)
7434 compute_block_forward_dependences (bb);
7436 /* Delete line notes, compute live-regs at block end, and set priorities. */
7437 dead_notes = 0;
7438 for (bb = 0; bb < current_nr_blocks; bb++)
7440 if (reload_completed == 0)
7441 find_pre_sched_live (bb);
7443 if (write_symbols != NO_DEBUG)
7445 save_line_notes (bb);
7446 rm_line_notes (bb);
7449 rgn_n_insns += set_priorities (bb);
7452 /* compute interblock info: probabilities, split-edges, dominators, etc. */
7453 if (current_nr_blocks > 1)
7455 int i;
7457 prob = (float *) alloca ((current_nr_blocks) * sizeof (float));
7459 bbset_size = current_nr_blocks / HOST_BITS_PER_WIDE_INT + 1;
7460 dom = (bbset *) alloca (current_nr_blocks * sizeof (bbset));
7461 for (i = 0; i < current_nr_blocks; i++)
7463 dom[i] = (bbset) alloca (bbset_size * sizeof (HOST_WIDE_INT));
7464 bzero ((char *) dom[i], bbset_size * sizeof (HOST_WIDE_INT));
7467 /* edge to bit */
7468 rgn_nr_edges = 0;
7469 edge_to_bit = (int *) alloca (nr_edges * sizeof (int));
7470 for (i = 1; i < nr_edges; i++)
7471 if (CONTAINING_RGN (FROM_BLOCK (i)) == rgn)
7472 EDGE_TO_BIT (i) = rgn_nr_edges++;
7473 rgn_edges = (int *) alloca (rgn_nr_edges * sizeof (int));
7475 rgn_nr_edges = 0;
7476 for (i = 1; i < nr_edges; i++)
7477 if (CONTAINING_RGN (FROM_BLOCK (i)) == (rgn))
7478 rgn_edges[rgn_nr_edges++] = i;
7480 /* split edges */
7481 edgeset_size = rgn_nr_edges / HOST_BITS_PER_WIDE_INT + 1;
7482 pot_split = (edgeset *) alloca (current_nr_blocks * sizeof (edgeset));
7483 ancestor_edges = (edgeset *) alloca (current_nr_blocks * sizeof (edgeset));
7484 for (i = 0; i < current_nr_blocks; i++)
7486 pot_split[i] =
7487 (edgeset) alloca (edgeset_size * sizeof (HOST_WIDE_INT));
7488 bzero ((char *) pot_split[i],
7489 edgeset_size * sizeof (HOST_WIDE_INT));
7490 ancestor_edges[i] =
7491 (edgeset) alloca (edgeset_size * sizeof (HOST_WIDE_INT));
7492 bzero ((char *) ancestor_edges[i],
7493 edgeset_size * sizeof (HOST_WIDE_INT));
7496 /* compute probabilities, dominators, split_edges */
7497 for (bb = 0; bb < current_nr_blocks; bb++)
7498 compute_dom_prob_ps (bb);
7501 /* now we can schedule all blocks */
7502 for (bb = 0; bb < current_nr_blocks; bb++)
7504 sched_rgn_n_insns += schedule_block (bb, rgn_n_insns);
7506 #ifdef USE_C_ALLOCA
7507 alloca (0);
7508 #endif
7511 #ifdef INTERBLOCK_DEBUG
7512 if (sched_debug_count != 0)
7513 #endif
7514 /* sanity check: verify that all region insns were scheduled */
7515 if (sched_rgn_n_insns != rgn_n_insns)
7516 abort ();
7518 /* update register life and usage information */
7519 if (reload_completed == 0)
7521 for (bb = current_nr_blocks - 1; bb >= 0; bb--)
7522 find_post_sched_live (bb);
7524 if (current_nr_blocks <= 1)
7525 /* Sanity check. There should be no REG_DEAD notes leftover at the end.
7526 In practice, this can occur as the result of bugs in flow, combine.c,
7527 and/or sched.c. The values of the REG_DEAD notes remaining are
7528 meaningless, because dead_notes is just used as a free list. */
7529 if (dead_notes != 0)
7530 abort ();
7533 /* restore line notes. */
7534 if (write_symbols != NO_DEBUG)
7536 for (bb = 0; bb < current_nr_blocks; bb++)
7537 restore_line_notes (bb);
7540 /* Done with this region */
7541 free_pending_lists ();
7543 FREE_REG_SET (reg_pending_sets);
7546 /* Subroutine of split_hard_reg_notes. Searches X for any reference to
7547 REGNO, returning the rtx of the reference found if any. Otherwise,
7548 returns 0. */
7550 static rtx
7551 regno_use_in (regno, x)
7552 int regno;
7553 rtx x;
7555 register char *fmt;
7556 int i, j;
7557 rtx tem;
7559 if (GET_CODE (x) == REG && REGNO (x) == regno)
7560 return x;
7562 fmt = GET_RTX_FORMAT (GET_CODE (x));
7563 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
7565 if (fmt[i] == 'e')
7567 if ((tem = regno_use_in (regno, XEXP (x, i))))
7568 return tem;
7570 else if (fmt[i] == 'E')
7571 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7572 if ((tem = regno_use_in (regno, XVECEXP (x, i, j))))
7573 return tem;
7576 return 0;
7579 /* Subroutine of update_flow_info. Determines whether any new REG_NOTEs are
7580 needed for the hard register mentioned in the note. This can happen
7581 if the reference to the hard register in the original insn was split into
7582 several smaller hard register references in the split insns. */
7584 static void
7585 split_hard_reg_notes (note, first, last)
7586 rtx note, first, last;
7588 rtx reg, temp, link;
7589 int n_regs, i, new_reg;
7590 rtx insn;
7592 /* Assume that this is a REG_DEAD note. */
7593 if (REG_NOTE_KIND (note) != REG_DEAD)
7594 abort ();
7596 reg = XEXP (note, 0);
7598 n_regs = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
7600 for (i = 0; i < n_regs; i++)
7602 new_reg = REGNO (reg) + i;
7604 /* Check for references to new_reg in the split insns. */
7605 for (insn = last;; insn = PREV_INSN (insn))
7607 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
7608 && (temp = regno_use_in (new_reg, PATTERN (insn))))
7610 /* Create a new reg dead note ere. */
7611 link = alloc_EXPR_LIST (REG_DEAD, temp, REG_NOTES (insn));
7612 REG_NOTES (insn) = link;
7614 /* If killed multiple registers here, then add in the excess. */
7615 i += HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)) - 1;
7617 break;
7619 /* It isn't mentioned anywhere, so no new reg note is needed for
7620 this register. */
7621 if (insn == first)
7622 break;
7627 /* Subroutine of update_flow_info. Determines whether a SET or CLOBBER in an
7628 insn created by splitting needs a REG_DEAD or REG_UNUSED note added. */
7630 static void
7631 new_insn_dead_notes (pat, insn, last, orig_insn)
7632 rtx pat, insn, last, orig_insn;
7634 rtx dest, tem, set;
7636 /* PAT is either a CLOBBER or a SET here. */
7637 dest = XEXP (pat, 0);
7639 while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
7640 || GET_CODE (dest) == STRICT_LOW_PART
7641 || GET_CODE (dest) == SIGN_EXTRACT)
7642 dest = XEXP (dest, 0);
7644 if (GET_CODE (dest) == REG)
7646 for (tem = last; tem != insn; tem = PREV_INSN (tem))
7648 if (GET_RTX_CLASS (GET_CODE (tem)) == 'i'
7649 && reg_overlap_mentioned_p (dest, PATTERN (tem))
7650 && (set = single_set (tem)))
7652 rtx tem_dest = SET_DEST (set);
7654 while (GET_CODE (tem_dest) == ZERO_EXTRACT
7655 || GET_CODE (tem_dest) == SUBREG
7656 || GET_CODE (tem_dest) == STRICT_LOW_PART
7657 || GET_CODE (tem_dest) == SIGN_EXTRACT)
7658 tem_dest = XEXP (tem_dest, 0);
7660 if (!rtx_equal_p (tem_dest, dest))
7662 /* Use the same scheme as combine.c, don't put both REG_DEAD
7663 and REG_UNUSED notes on the same insn. */
7664 if (!find_regno_note (tem, REG_UNUSED, REGNO (dest))
7665 && !find_regno_note (tem, REG_DEAD, REGNO (dest)))
7667 rtx note = alloc_EXPR_LIST (REG_DEAD, dest,
7668 REG_NOTES (tem));
7669 REG_NOTES (tem) = note;
7671 /* The reg only dies in one insn, the last one that uses
7672 it. */
7673 break;
7675 else if (reg_overlap_mentioned_p (dest, SET_SRC (set)))
7676 /* We found an instruction that both uses the register,
7677 and sets it, so no new REG_NOTE is needed for this set. */
7678 break;
7681 /* If this is a set, it must die somewhere, unless it is the dest of
7682 the original insn, and hence is live after the original insn. Abort
7683 if it isn't supposed to be live after the original insn.
7685 If this is a clobber, then just add a REG_UNUSED note. */
7686 if (tem == insn)
7688 int live_after_orig_insn = 0;
7689 rtx pattern = PATTERN (orig_insn);
7690 int i;
7692 if (GET_CODE (pat) == CLOBBER)
7694 rtx note = alloc_EXPR_LIST (REG_UNUSED, dest, REG_NOTES (insn));
7695 REG_NOTES (insn) = note;
7696 return;
7699 /* The original insn could have multiple sets, so search the
7700 insn for all sets. */
7701 if (GET_CODE (pattern) == SET)
7703 if (reg_overlap_mentioned_p (dest, SET_DEST (pattern)))
7704 live_after_orig_insn = 1;
7706 else if (GET_CODE (pattern) == PARALLEL)
7708 for (i = 0; i < XVECLEN (pattern, 0); i++)
7709 if (GET_CODE (XVECEXP (pattern, 0, i)) == SET
7710 && reg_overlap_mentioned_p (dest,
7711 SET_DEST (XVECEXP (pattern,
7712 0, i))))
7713 live_after_orig_insn = 1;
7716 if (!live_after_orig_insn)
7717 abort ();
7722 /* Subroutine of update_flow_info. Update the value of reg_n_sets for all
7723 registers modified by X. INC is -1 if the containing insn is being deleted,
7724 and is 1 if the containing insn is a newly generated insn. */
7726 static void
7727 update_n_sets (x, inc)
7728 rtx x;
7729 int inc;
7731 rtx dest = SET_DEST (x);
7733 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
7734 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
7735 dest = SUBREG_REG (dest);
7737 if (GET_CODE (dest) == REG)
7739 int regno = REGNO (dest);
7741 if (regno < FIRST_PSEUDO_REGISTER)
7743 register int i;
7744 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (dest));
7746 for (i = regno; i < endregno; i++)
7747 REG_N_SETS (i) += inc;
7749 else
7750 REG_N_SETS (regno) += inc;
7754 /* Updates all flow-analysis related quantities (including REG_NOTES) for
7755 the insns from FIRST to LAST inclusive that were created by splitting
7756 ORIG_INSN. NOTES are the original REG_NOTES. */
7758 static void
7759 update_flow_info (notes, first, last, orig_insn)
7760 rtx notes;
7761 rtx first, last;
7762 rtx orig_insn;
7764 rtx insn, note;
7765 rtx next;
7766 rtx orig_dest, temp;
7767 rtx set;
7769 /* Get and save the destination set by the original insn. */
7771 orig_dest = single_set (orig_insn);
7772 if (orig_dest)
7773 orig_dest = SET_DEST (orig_dest);
7775 /* Move REG_NOTES from the original insn to where they now belong. */
7777 for (note = notes; note; note = next)
7779 next = XEXP (note, 1);
7780 switch (REG_NOTE_KIND (note))
7782 case REG_DEAD:
7783 case REG_UNUSED:
7784 /* Move these notes from the original insn to the last new insn where
7785 the register is now set. */
7787 for (insn = last;; insn = PREV_INSN (insn))
7789 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
7790 && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
7792 /* If this note refers to a multiple word hard register, it
7793 may have been split into several smaller hard register
7794 references, so handle it specially. */
7795 temp = XEXP (note, 0);
7796 if (REG_NOTE_KIND (note) == REG_DEAD
7797 && GET_CODE (temp) == REG
7798 && REGNO (temp) < FIRST_PSEUDO_REGISTER
7799 && HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)) > 1)
7800 split_hard_reg_notes (note, first, last);
7801 else
7803 XEXP (note, 1) = REG_NOTES (insn);
7804 REG_NOTES (insn) = note;
7807 /* Sometimes need to convert REG_UNUSED notes to REG_DEAD
7808 notes. */
7809 /* ??? This won't handle multiple word registers correctly,
7810 but should be good enough for now. */
7811 if (REG_NOTE_KIND (note) == REG_UNUSED
7812 && GET_CODE (XEXP (note, 0)) != SCRATCH
7813 && !dead_or_set_p (insn, XEXP (note, 0)))
7814 PUT_REG_NOTE_KIND (note, REG_DEAD);
7816 /* The reg only dies in one insn, the last one that uses
7817 it. */
7818 break;
7820 /* It must die somewhere, fail it we couldn't find where it died.
7822 If this is a REG_UNUSED note, then it must be a temporary
7823 register that was not needed by this instantiation of the
7824 pattern, so we can safely ignore it. */
7825 if (insn == first)
7827 /* After reload, REG_DEAD notes come sometimes an
7828 instruction after the register actually dies. */
7829 if (reload_completed && REG_NOTE_KIND (note) == REG_DEAD)
7831 XEXP (note, 1) = REG_NOTES (insn);
7832 REG_NOTES (insn) = note;
7833 break;
7836 if (REG_NOTE_KIND (note) != REG_UNUSED)
7837 abort ();
7839 break;
7842 break;
7844 case REG_WAS_0:
7845 /* If the insn that set the register to 0 was deleted, this
7846 note cannot be relied on any longer. The destination might
7847 even have been moved to memory.
7848 This was observed for SH4 with execute/920501-6.c compilation,
7849 -O2 -fomit-frame-pointer -finline-functions . */
7850 if (GET_CODE (XEXP (note, 0)) == NOTE
7851 || INSN_DELETED_P (XEXP (note, 0)))
7852 break;
7853 /* This note applies to the dest of the original insn. Find the
7854 first new insn that now has the same dest, and move the note
7855 there. */
7857 if (!orig_dest)
7858 abort ();
7860 for (insn = first;; insn = NEXT_INSN (insn))
7862 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
7863 && (temp = single_set (insn))
7864 && rtx_equal_p (SET_DEST (temp), orig_dest))
7866 XEXP (note, 1) = REG_NOTES (insn);
7867 REG_NOTES (insn) = note;
7868 /* The reg is only zero before one insn, the first that
7869 uses it. */
7870 break;
7872 /* If this note refers to a multiple word hard
7873 register, it may have been split into several smaller
7874 hard register references. We could split the notes,
7875 but simply dropping them is good enough. */
7876 if (GET_CODE (orig_dest) == REG
7877 && REGNO (orig_dest) < FIRST_PSEUDO_REGISTER
7878 && HARD_REGNO_NREGS (REGNO (orig_dest),
7879 GET_MODE (orig_dest)) > 1)
7880 break;
7881 /* It must be set somewhere, fail if we couldn't find where it
7882 was set. */
7883 if (insn == last)
7884 abort ();
7886 break;
7888 case REG_EQUAL:
7889 case REG_EQUIV:
7890 /* A REG_EQUIV or REG_EQUAL note on an insn with more than one
7891 set is meaningless. Just drop the note. */
7892 if (!orig_dest)
7893 break;
7895 case REG_NO_CONFLICT:
7896 /* These notes apply to the dest of the original insn. Find the last
7897 new insn that now has the same dest, and move the note there. */
7899 if (!orig_dest)
7900 abort ();
7902 for (insn = last;; insn = PREV_INSN (insn))
7904 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
7905 && (temp = single_set (insn))
7906 && rtx_equal_p (SET_DEST (temp), orig_dest))
7908 XEXP (note, 1) = REG_NOTES (insn);
7909 REG_NOTES (insn) = note;
7910 /* Only put this note on one of the new insns. */
7911 break;
7914 /* The original dest must still be set someplace. Abort if we
7915 couldn't find it. */
7916 if (insn == first)
7918 /* However, if this note refers to a multiple word hard
7919 register, it may have been split into several smaller
7920 hard register references. We could split the notes,
7921 but simply dropping them is good enough. */
7922 if (GET_CODE (orig_dest) == REG
7923 && REGNO (orig_dest) < FIRST_PSEUDO_REGISTER
7924 && HARD_REGNO_NREGS (REGNO (orig_dest),
7925 GET_MODE (orig_dest)) > 1)
7926 break;
7927 /* Likewise for multi-word memory references. */
7928 if (GET_CODE (orig_dest) == MEM
7929 && SIZE_FOR_MODE (orig_dest) > MOVE_MAX)
7930 break;
7931 abort ();
7934 break;
7936 case REG_LIBCALL:
7937 /* Move a REG_LIBCALL note to the first insn created, and update
7938 the corresponding REG_RETVAL note. */
7939 XEXP (note, 1) = REG_NOTES (first);
7940 REG_NOTES (first) = note;
7942 insn = XEXP (note, 0);
7943 note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
7944 if (note)
7945 XEXP (note, 0) = first;
7946 break;
7948 case REG_EXEC_COUNT:
7949 /* Move a REG_EXEC_COUNT note to the first insn created. */
7950 XEXP (note, 1) = REG_NOTES (first);
7951 REG_NOTES (first) = note;
7952 break;
7954 case REG_RETVAL:
7955 /* Move a REG_RETVAL note to the last insn created, and update
7956 the corresponding REG_LIBCALL note. */
7957 XEXP (note, 1) = REG_NOTES (last);
7958 REG_NOTES (last) = note;
7960 insn = XEXP (note, 0);
7961 note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
7962 if (note)
7963 XEXP (note, 0) = last;
7964 break;
7966 case REG_NONNEG:
7967 case REG_BR_PROB:
7968 /* This should be moved to whichever instruction is a JUMP_INSN. */
7970 for (insn = last;; insn = PREV_INSN (insn))
7972 if (GET_CODE (insn) == JUMP_INSN)
7974 XEXP (note, 1) = REG_NOTES (insn);
7975 REG_NOTES (insn) = note;
7976 /* Only put this note on one of the new insns. */
7977 break;
7979 /* Fail if we couldn't find a JUMP_INSN. */
7980 if (insn == first)
7981 abort ();
7983 break;
7985 case REG_INC:
7986 /* reload sometimes leaves obsolete REG_INC notes around. */
7987 if (reload_completed)
7988 break;
7989 /* This should be moved to whichever instruction now has the
7990 increment operation. */
7991 abort ();
7993 case REG_LABEL:
7994 /* Should be moved to the new insn(s) which use the label. */
7995 for (insn = first; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
7996 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
7997 && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
7999 REG_NOTES (insn) = alloc_EXPR_LIST (REG_LABEL,
8000 XEXP (note, 0),
8001 REG_NOTES (insn));
8003 break;
8005 case REG_CC_SETTER:
8006 case REG_CC_USER:
8007 /* These two notes will never appear until after reorg, so we don't
8008 have to handle them here. */
8009 default:
8010 abort ();
8014 /* Each new insn created, except the last, has a new set. If the destination
8015 is a register, then this reg is now live across several insns, whereas
8016 previously the dest reg was born and died within the same insn. To
8017 reflect this, we now need a REG_DEAD note on the insn where this
8018 dest reg dies.
8020 Similarly, the new insns may have clobbers that need REG_UNUSED notes. */
8022 for (insn = first; insn != last; insn = NEXT_INSN (insn))
8024 rtx pat;
8025 int i;
8027 pat = PATTERN (insn);
8028 if (GET_CODE (pat) == SET || GET_CODE (pat) == CLOBBER)
8029 new_insn_dead_notes (pat, insn, last, orig_insn);
8030 else if (GET_CODE (pat) == PARALLEL)
8032 for (i = 0; i < XVECLEN (pat, 0); i++)
8033 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
8034 || GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER)
8035 new_insn_dead_notes (XVECEXP (pat, 0, i), insn, last, orig_insn);
8039 /* If any insn, except the last, uses the register set by the last insn,
8040 then we need a new REG_DEAD note on that insn. In this case, there
8041 would not have been a REG_DEAD note for this register in the original
8042 insn because it was used and set within one insn. */
8044 set = single_set (last);
8045 if (set)
8047 rtx dest = SET_DEST (set);
8049 while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
8050 || GET_CODE (dest) == STRICT_LOW_PART
8051 || GET_CODE (dest) == SIGN_EXTRACT)
8052 dest = XEXP (dest, 0);
8054 if (GET_CODE (dest) == REG
8055 /* Global registers are always live, so the code below does not
8056 apply to them. */
8057 && (REGNO (dest) >= FIRST_PSEUDO_REGISTER
8058 || ! global_regs[REGNO (dest)]))
8060 rtx stop_insn = PREV_INSN (first);
8062 /* If the last insn uses the register that it is setting, then
8063 we don't want to put a REG_DEAD note there. Search backwards
8064 to find the first insn that sets but does not use DEST. */
8066 insn = last;
8067 if (reg_overlap_mentioned_p (dest, SET_SRC (set)))
8069 for (insn = PREV_INSN (insn); insn != first;
8070 insn = PREV_INSN (insn))
8072 if ((set = single_set (insn))
8073 && reg_mentioned_p (dest, SET_DEST (set))
8074 && ! reg_overlap_mentioned_p (dest, SET_SRC (set)))
8075 break;
8079 /* Now find the first insn that uses but does not set DEST. */
8081 for (insn = PREV_INSN (insn); insn != stop_insn;
8082 insn = PREV_INSN (insn))
8084 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
8085 && reg_mentioned_p (dest, PATTERN (insn))
8086 && (set = single_set (insn)))
8088 rtx insn_dest = SET_DEST (set);
8090 while (GET_CODE (insn_dest) == ZERO_EXTRACT
8091 || GET_CODE (insn_dest) == SUBREG
8092 || GET_CODE (insn_dest) == STRICT_LOW_PART
8093 || GET_CODE (insn_dest) == SIGN_EXTRACT)
8094 insn_dest = XEXP (insn_dest, 0);
8096 if (insn_dest != dest)
8098 note = alloc_EXPR_LIST (REG_DEAD, dest, REG_NOTES (insn));
8099 REG_NOTES (insn) = note;
8100 /* The reg only dies in one insn, the last one
8101 that uses it. */
8102 break;
8109 /* If the original dest is modifying a multiple register target, and the
8110 original instruction was split such that the original dest is now set
8111 by two or more SUBREG sets, then the split insns no longer kill the
8112 destination of the original insn.
8114 In this case, if there exists an instruction in the same basic block,
8115 before the split insn, which uses the original dest, and this use is
8116 killed by the original insn, then we must remove the REG_DEAD note on
8117 this insn, because it is now superfluous.
8119 This does not apply when a hard register gets split, because the code
8120 knows how to handle overlapping hard registers properly. */
8121 if (orig_dest && GET_CODE (orig_dest) == REG)
8123 int found_orig_dest = 0;
8124 int found_split_dest = 0;
8126 for (insn = first;; insn = NEXT_INSN (insn))
8128 rtx pat;
8129 int i;
8131 /* I'm not sure if this can happen, but let's be safe. */
8132 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
8133 continue;
8135 pat = PATTERN (insn);
8136 i = GET_CODE (pat) == PARALLEL ? XVECLEN (pat, 0) : 0;
8137 set = pat;
8139 for (;;)
8141 if (GET_CODE (set) == SET)
8143 if (GET_CODE (SET_DEST (set)) == REG
8144 && REGNO (SET_DEST (set)) == REGNO (orig_dest))
8146 found_orig_dest = 1;
8147 break;
8149 else if (GET_CODE (SET_DEST (set)) == SUBREG
8150 && SUBREG_REG (SET_DEST (set)) == orig_dest)
8152 found_split_dest = 1;
8153 break;
8156 if (--i < 0)
8157 break;
8158 set = XVECEXP (pat, 0, i);
8161 if (insn == last)
8162 break;
8165 if (found_split_dest)
8167 /* Search backwards from FIRST, looking for the first insn that uses
8168 the original dest. Stop if we pass a CODE_LABEL or a JUMP_INSN.
8169 If we find an insn, and it has a REG_DEAD note, then delete the
8170 note. */
8172 for (insn = first; insn; insn = PREV_INSN (insn))
8174 if (GET_CODE (insn) == CODE_LABEL
8175 || GET_CODE (insn) == JUMP_INSN)
8176 break;
8177 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
8178 && reg_mentioned_p (orig_dest, insn))
8180 note = find_regno_note (insn, REG_DEAD, REGNO (orig_dest));
8181 if (note)
8182 remove_note (insn, note);
8186 else if (!found_orig_dest)
8188 /* This should never happen. */
8189 abort ();
8193 /* Update reg_n_sets. This is necessary to prevent local alloc from
8194 converting REG_EQUAL notes to REG_EQUIV when splitting has modified
8195 a reg from set once to set multiple times. */
8198 rtx x = PATTERN (orig_insn);
8199 RTX_CODE code = GET_CODE (x);
8201 if (code == SET || code == CLOBBER)
8202 update_n_sets (x, -1);
8203 else if (code == PARALLEL)
8205 int i;
8206 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
8208 code = GET_CODE (XVECEXP (x, 0, i));
8209 if (code == SET || code == CLOBBER)
8210 update_n_sets (XVECEXP (x, 0, i), -1);
8214 for (insn = first;; insn = NEXT_INSN (insn))
8216 x = PATTERN (insn);
8217 code = GET_CODE (x);
8219 if (code == SET || code == CLOBBER)
8220 update_n_sets (x, 1);
8221 else if (code == PARALLEL)
8223 int i;
8224 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
8226 code = GET_CODE (XVECEXP (x, 0, i));
8227 if (code == SET || code == CLOBBER)
8228 update_n_sets (XVECEXP (x, 0, i), 1);
8232 if (insn == last)
8233 break;
8238 /* Do the splitting of insns in the block b. */
8240 static void
8241 split_block_insns (b)
8242 int b;
8244 rtx insn, next;
8246 for (insn = basic_block_head[b];; insn = next)
8248 rtx prev;
8249 rtx set;
8251 /* Can't use `next_real_insn' because that
8252 might go across CODE_LABELS and short-out basic blocks. */
8253 next = NEXT_INSN (insn);
8254 if (GET_CODE (insn) != INSN)
8256 if (insn == basic_block_end[b])
8257 break;
8259 continue;
8262 /* Don't split no-op move insns. These should silently disappear
8263 later in final. Splitting such insns would break the code
8264 that handles REG_NO_CONFLICT blocks. */
8265 set = single_set (insn);
8266 if (set && rtx_equal_p (SET_SRC (set), SET_DEST (set)))
8268 if (insn == basic_block_end[b])
8269 break;
8271 /* Nops get in the way while scheduling, so delete them now if
8272 register allocation has already been done. It is too risky
8273 to try to do this before register allocation, and there are
8274 unlikely to be very many nops then anyways. */
8275 if (reload_completed)
8277 PUT_CODE (insn, NOTE);
8278 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8279 NOTE_SOURCE_FILE (insn) = 0;
8282 continue;
8285 /* Split insns here to get max fine-grain parallelism. */
8286 prev = PREV_INSN (insn);
8287 /* It is probably not worthwhile to try to split again in
8288 the second pass. However, if flag_schedule_insns is not set,
8289 the first and only (if any) scheduling pass is after reload. */
8290 if (reload_completed == 0 || ! flag_schedule_insns)
8292 rtx last, first = PREV_INSN (insn);
8293 rtx notes = REG_NOTES (insn);
8294 last = try_split (PATTERN (insn), insn, 1);
8295 if (last != insn)
8297 /* try_split returns the NOTE that INSN became. */
8298 first = NEXT_INSN (first);
8299 update_flow_info (notes, first, last, insn);
8301 PUT_CODE (insn, NOTE);
8302 NOTE_SOURCE_FILE (insn) = 0;
8303 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8304 if (insn == basic_block_head[b])
8305 basic_block_head[b] = first;
8306 if (insn == basic_block_end[b])
8308 basic_block_end[b] = last;
8309 break;
8314 if (insn == basic_block_end[b])
8315 break;
8319 /* The one entry point in this file. DUMP_FILE is the dump file for
8320 this pass. */
8322 void
8323 schedule_insns (dump_file)
8324 FILE *dump_file;
8327 int max_uid;
8328 int b;
8329 rtx insn;
8330 int rgn;
8332 int luid;
8334 /* disable speculative loads in their presence if cc0 defined */
8335 #ifdef HAVE_cc0
8336 flag_schedule_speculative_load = 0;
8337 #endif
8339 /* Taking care of this degenerate case makes the rest of
8340 this code simpler. */
8341 if (n_basic_blocks == 0)
8342 return;
8344 /* set dump and sched_verbose for the desired debugging output. If no
8345 dump-file was specified, but -fsched-verbose-N (any N), print to stderr.
8346 For -fsched-verbose-N, N>=10, print everything to stderr. */
8347 sched_verbose = sched_verbose_param;
8348 if (sched_verbose_param == 0 && dump_file)
8349 sched_verbose = 1;
8350 dump = ((sched_verbose_param >= 10 || !dump_file) ? stderr : dump_file);
8352 nr_inter = 0;
8353 nr_spec = 0;
8355 /* Initialize the unused_*_lists. We can't use the ones left over from
8356 the previous function, because gcc has freed that memory. We can use
8357 the ones left over from the first sched pass in the second pass however,
8358 so only clear them on the first sched pass. The first pass is before
8359 reload if flag_schedule_insns is set, otherwise it is afterwards. */
8361 if (reload_completed == 0 || !flag_schedule_insns)
8363 unused_insn_list = 0;
8364 unused_expr_list = 0;
8367 /* initialize issue_rate */
8368 issue_rate = ISSUE_RATE;
8370 /* do the splitting first for all blocks */
8371 for (b = 0; b < n_basic_blocks; b++)
8372 split_block_insns (b);
8374 max_uid = (get_max_uid () + 1);
8376 cant_move = (char *) alloca (max_uid * sizeof (char));
8377 bzero ((char *) cant_move, max_uid * sizeof (char));
8379 fed_by_spec_load = (char *) alloca (max_uid * sizeof (char));
8380 bzero ((char *) fed_by_spec_load, max_uid * sizeof (char));
8382 is_load_insn = (char *) alloca (max_uid * sizeof (char));
8383 bzero ((char *) is_load_insn, max_uid * sizeof (char));
8385 insn_orig_block = (int *) alloca (max_uid * sizeof (int));
8386 insn_luid = (int *) alloca (max_uid * sizeof (int));
8388 luid = 0;
8389 for (b = 0; b < n_basic_blocks; b++)
8390 for (insn = basic_block_head[b];; insn = NEXT_INSN (insn))
8392 INSN_BLOCK (insn) = b;
8393 INSN_LUID (insn) = luid++;
8395 if (insn == basic_block_end[b])
8396 break;
8399 /* after reload, remove inter-blocks dependences computed before reload. */
8400 if (reload_completed)
8402 int b;
8403 rtx insn;
8405 for (b = 0; b < n_basic_blocks; b++)
8406 for (insn = basic_block_head[b];; insn = NEXT_INSN (insn))
8408 rtx link, prev;
8410 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
8412 prev = NULL_RTX;
8413 link = LOG_LINKS (insn);
8414 while (link)
8416 rtx x = XEXP (link, 0);
8418 if (INSN_BLOCK (x) != b)
8420 remove_dependence (insn, x);
8421 link = prev ? XEXP (prev, 1) : LOG_LINKS (insn);
8423 else
8424 prev = link, link = XEXP (prev, 1);
8428 if (insn == basic_block_end[b])
8429 break;
8433 nr_regions = 0;
8434 rgn_table = (region *) alloca ((n_basic_blocks) * sizeof (region));
8435 rgn_bb_table = (int *) alloca ((n_basic_blocks) * sizeof (int));
8436 block_to_bb = (int *) alloca ((n_basic_blocks) * sizeof (int));
8437 containing_rgn = (int *) alloca ((n_basic_blocks) * sizeof (int));
8439 /* compute regions for scheduling */
8440 if (reload_completed
8441 || n_basic_blocks == 1
8442 || !flag_schedule_interblock)
8444 find_single_block_region ();
8446 else
8448 /* verify that a 'good' control flow graph can be built */
8449 if (is_cfg_nonregular ())
8451 find_single_block_region ();
8453 else
8455 /* build_control_flow will return nonzero if it detects unreachable
8456 blocks or any other irregularity with the cfg which prevents
8457 cross block scheduling. */
8458 if (build_control_flow () != 0)
8459 find_single_block_region ();
8460 else
8461 find_rgns ();
8463 if (sched_verbose >= 3)
8465 debug_control_flow ();
8466 debug_regions ();
8472 /* Allocate data for this pass. See comments, above,
8473 for what these vectors do. */
8474 insn_priority = (int *) alloca (max_uid * sizeof (int));
8475 insn_reg_weight = (int *) alloca (max_uid * sizeof (int));
8476 insn_tick = (int *) alloca (max_uid * sizeof (int));
8477 insn_costs = (short *) alloca (max_uid * sizeof (short));
8478 insn_units = (short *) alloca (max_uid * sizeof (short));
8479 insn_blockage = (unsigned int *) alloca (max_uid * sizeof (unsigned int));
8480 insn_ref_count = (int *) alloca (max_uid * sizeof (int));
8482 /* Allocate for forward dependencies */
8483 insn_dep_count = (int *) alloca (max_uid * sizeof (int));
8484 insn_depend = (rtx *) alloca (max_uid * sizeof (rtx));
8486 if (reload_completed == 0)
8488 int i;
8490 sched_reg_n_calls_crossed = (int *) alloca (max_regno * sizeof (int));
8491 sched_reg_live_length = (int *) alloca (max_regno * sizeof (int));
8492 sched_reg_basic_block = (int *) alloca (max_regno * sizeof (int));
8493 bb_live_regs = ALLOCA_REG_SET ();
8494 bzero ((char *) sched_reg_n_calls_crossed, max_regno * sizeof (int));
8495 bzero ((char *) sched_reg_live_length, max_regno * sizeof (int));
8497 for (i = 0; i < max_regno; i++)
8498 sched_reg_basic_block[i] = REG_BLOCK_UNKNOWN;
8500 else
8502 sched_reg_n_calls_crossed = 0;
8503 sched_reg_live_length = 0;
8504 bb_live_regs = 0;
8506 init_alias_analysis ();
8508 if (write_symbols != NO_DEBUG)
8510 rtx line;
8512 line_note = (rtx *) alloca (max_uid * sizeof (rtx));
8513 bzero ((char *) line_note, max_uid * sizeof (rtx));
8514 line_note_head = (rtx *) alloca (n_basic_blocks * sizeof (rtx));
8515 bzero ((char *) line_note_head, n_basic_blocks * sizeof (rtx));
8517 /* Save-line-note-head:
8518 Determine the line-number at the start of each basic block.
8519 This must be computed and saved now, because after a basic block's
8520 predecessor has been scheduled, it is impossible to accurately
8521 determine the correct line number for the first insn of the block. */
8523 for (b = 0; b < n_basic_blocks; b++)
8524 for (line = basic_block_head[b]; line; line = PREV_INSN (line))
8525 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
8527 line_note_head[b] = line;
8528 break;
8532 bzero ((char *) insn_priority, max_uid * sizeof (int));
8533 bzero ((char *) insn_reg_weight, max_uid * sizeof (int));
8534 bzero ((char *) insn_tick, max_uid * sizeof (int));
8535 bzero ((char *) insn_costs, max_uid * sizeof (short));
8536 bzero ((char *) insn_units, max_uid * sizeof (short));
8537 bzero ((char *) insn_blockage, max_uid * sizeof (unsigned int));
8538 bzero ((char *) insn_ref_count, max_uid * sizeof (int));
8540 /* Initialize for forward dependencies */
8541 bzero ((char *) insn_depend, max_uid * sizeof (rtx));
8542 bzero ((char *) insn_dep_count, max_uid * sizeof (int));
8544 /* Find units used in this fuction, for visualization */
8545 if (sched_verbose)
8546 init_target_units ();
8548 /* ??? Add a NOTE after the last insn of the last basic block. It is not
8549 known why this is done. */
8551 insn = basic_block_end[n_basic_blocks - 1];
8552 if (NEXT_INSN (insn) == 0
8553 || (GET_CODE (insn) != NOTE
8554 && GET_CODE (insn) != CODE_LABEL
8555 /* Don't emit a NOTE if it would end up between an unconditional
8556 jump and a BARRIER. */
8557 && !(GET_CODE (insn) == JUMP_INSN
8558 && GET_CODE (NEXT_INSN (insn)) == BARRIER)))
8559 emit_note_after (NOTE_INSN_DELETED, basic_block_end[n_basic_blocks - 1]);
8561 /* Schedule every region in the subroutine */
8562 for (rgn = 0; rgn < nr_regions; rgn++)
8564 schedule_region (rgn);
8566 #ifdef USE_C_ALLOCA
8567 alloca (0);
8568 #endif
8571 /* Reposition the prologue and epilogue notes in case we moved the
8572 prologue/epilogue insns. */
8573 if (reload_completed)
8574 reposition_prologue_and_epilogue_notes (get_insns ());
8576 /* delete redundant line notes. */
8577 if (write_symbols != NO_DEBUG)
8578 rm_redundant_line_notes ();
8580 /* Update information about uses of registers in the subroutine. */
8581 if (reload_completed == 0)
8582 update_reg_usage ();
8584 if (sched_verbose)
8586 if (reload_completed == 0 && flag_schedule_interblock)
8588 fprintf (dump, "\n;; Procedure interblock/speculative motions == %d/%d \n",
8589 nr_inter, nr_spec);
8591 else
8593 if (nr_inter > 0)
8594 abort ();
8596 fprintf (dump, "\n\n");
8599 if (bb_live_regs)
8600 FREE_REG_SET (bb_live_regs);
8602 if (edge_table)
8604 free (edge_table);
8605 edge_table = NULL;
8608 if (in_edges)
8610 free (in_edges);
8611 in_edges = NULL;
8613 if (out_edges)
8615 free (out_edges);
8616 out_edges = NULL;
8619 #endif /* INSN_SCHEDULING */