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