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