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