]> gcc.gnu.org Git - gcc.git/blame - gcc/haifa-sched.c
BasicMapEntry.java: Re-added.
[gcc.git] / gcc / haifa-sched.c
CommitLineData
8c660648 1/* Instruction scheduling pass.
d050d723
JL
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
8c660648
JL
4 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
5 and currently maintained by, Jim Wilson (wilson@cygnus.com)
6
5d14e356
RK
7This file is part of GNU CC.
8
9GNU CC is free software; you can redistribute it and/or modify it
10under the terms of the GNU General Public License as published by the
11Free Software Foundation; either version 2, or (at your option) any
12later version.
13
14GNU CC is distributed in the hope that it will be useful, but WITHOUT
15ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
18
19You should have received a copy of the GNU General Public License
20along with GNU CC; see the file COPYING. If not, write to the Free
21the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2202111-1307, USA. */
8c660648 23
b4ead7d4
BS
24/* Instruction scheduling pass. This file, along with sched-deps.c,
25 contains the generic parts. The actual entry point is found for
26 the normal instruction scheduling pass is found in sched-rgn.c.
8c660648
JL
27
28 We compute insn priorities based on data dependencies. Flow
29 analysis only creates a fraction of the data-dependencies we must
30 observe: namely, only those dependencies which the combiner can be
31 expected to use. For this pass, we must therefore create the
32 remaining dependencies we need to observe: register dependencies,
33 memory dependencies, dependencies to keep function calls in order,
34 and the dependence between a conditional branch and the setting of
35 condition codes are all dealt with here.
36
37 The scheduler first traverses the data flow graph, starting with
38 the last instruction, and proceeding to the first, assigning values
39 to insn_priority as it goes. This sorts the instructions
40 topologically by data dependence.
41
42 Once priorities have been established, we order the insns using
43 list scheduling. This works as follows: starting with a list of
44 all the ready insns, and sorted according to priority number, we
45 schedule the insn from the end of the list by placing its
46 predecessors in the list according to their priority order. We
47 consider this insn scheduled by setting the pointer to the "end" of
48 the list to point to the previous insn. When an insn has no
49 predecessors, we either queue it until sufficient time has elapsed
50 or add it to the ready list. As the instructions are scheduled or
51 when stalls are introduced, the queue advances and dumps insns into
52 the ready list. When all insns down to the lowest priority have
53 been scheduled, the critical path of the basic block has been made
54 as short as possible. The remaining insns are then scheduled in
55 remaining slots.
56
57 Function unit conflicts are resolved during forward list scheduling
58 by tracking the time when each insn is committed to the schedule
59 and from that, the time the function units it uses must be free.
60 As insns on the ready list are considered for scheduling, those
61 that would result in a blockage of the already committed insns are
62 queued until no blockage will result.
63
64 The following list shows the order in which we want to break ties
65 among insns in the ready list:
66
67 1. choose insn with the longest path to end of bb, ties
68 broken by
69 2. choose insn with least contribution to register pressure,
70 ties broken by
71 3. prefer in-block upon interblock motion, ties broken by
72 4. prefer useful upon speculative motion, ties broken by
73 5. choose insn with largest control flow probability, ties
74 broken by
75 6. choose insn with the least dependences upon the previously
76 scheduled insn, or finally
2db45993
JL
77 7 choose the insn which has the most insns dependent on it.
78 8. choose insn with lowest UID.
8c660648
JL
79
80 Memory references complicate matters. Only if we can be certain
81 that memory references are not part of the data dependency graph
82 (via true, anti, or output dependence), can we move operations past
83 memory references. To first approximation, reads can be done
84 independently, while writes introduce dependencies. Better
85 approximations will yield fewer dependencies.
86
87 Before reload, an extended analysis of interblock data dependences
88 is required for interblock scheduling. This is performed in
89 compute_block_backward_dependences ().
90
91 Dependencies set up by memory references are treated in exactly the
92 same way as other dependencies, by using LOG_LINKS backward
93 dependences. LOG_LINKS are translated into INSN_DEPEND forward
94 dependences for the purpose of forward list scheduling.
95
96 Having optimized the critical path, we may have also unduly
97 extended the lifetimes of some registers. If an operation requires
98 that constants be loaded into registers, it is certainly desirable
99 to load those constants as early as necessary, but no earlier.
100 I.e., it will not do to load up a bunch of registers at the
101 beginning of a basic block only to use them at the end, if they
102 could be loaded later, since this may result in excessive register
103 utilization.
104
105 Note that since branches are never in basic blocks, but only end
106 basic blocks, this pass will not move branches. But that is ok,
107 since we can use GNU's delayed branch scheduling pass to take care
108 of this case.
109
110 Also note that no further optimizations based on algebraic
111 identities are performed, so this pass would be a good one to
112 perform instruction splitting, such as breaking up a multiply
113 instruction into shifts and adds where that is profitable.
114
115 Given the memory aliasing analysis that this pass should perform,
116 it should be possible to remove redundant stores to memory, and to
117 load values from registers instead of hitting memory.
118
119 Before reload, speculative insns are moved only if a 'proof' exists
120 that no exception will be caused by this, and if no live registers
121 exist that inhibit the motion (live registers constraints are not
122 represented by data dependence edges).
123
124 This pass must update information that subsequent passes expect to
125 be correct. Namely: reg_n_refs, reg_n_sets, reg_n_deaths,
3b413743
RH
126 reg_n_calls_crossed, and reg_live_length. Also, BLOCK_HEAD,
127 BLOCK_END.
8c660648
JL
128
129 The information in the line number notes is carefully retained by
130 this pass. Notes that refer to the starting and ending of
131 exception regions are also carefully retained by this pass. All
132 other NOTE insns are grouped in their same relative order at the
b4ead7d4 133 beginning of basic blocks and regions that have been scheduled. */
8c660648 134\f
8c660648 135#include "config.h"
5835e573 136#include "system.h"
01198c2f 137#include "toplev.h"
8c660648 138#include "rtl.h"
6baf1cc8 139#include "tm_p.h"
efc9bd41 140#include "hard-reg-set.h"
8c660648
JL
141#include "basic-block.h"
142#include "regs.h"
49ad7cfa 143#include "function.h"
8c660648
JL
144#include "flags.h"
145#include "insn-config.h"
146#include "insn-attr.h"
18e720b3 147#include "insn-flags.h"
8c660648 148#include "except.h"
487a6e06 149#include "toplev.h"
79c9824e 150#include "recog.h"
1708fd40 151#include "sched-int.h"
8c660648 152
8c660648
JL
153#ifdef INSN_SCHEDULING
154
8c660648
JL
155/* issue_rate is the number of insns that can be scheduled in the same
156 machine cycle. It can be defined in the config/mach/mach.h file,
157 otherwise we set it to 1. */
158
159static int issue_rate;
160
62d65906
JL
161#ifndef ISSUE_RATE
162#define ISSUE_RATE 1
8c660648
JL
163#endif
164
cc132865 165/* sched-verbose controls the amount of debugging output the
409f8483 166 scheduler prints. It is controlled by -fsched-verbose=N:
8c660648
JL
167 N>0 and no -DSR : the output is directed to stderr.
168 N>=10 will direct the printouts to stderr (regardless of -dSR).
169 N=1: same as -dSR.
170 N=2: bb's probabilities, detailed ready list info, unit/insn info.
171 N=3: rtl at abort point, control-flow, regions info.
cc132865 172 N=5: dependences info. */
8c660648 173
8c660648 174static int sched_verbose_param = 0;
b4ead7d4 175int sched_verbose = 0;
8c660648 176
63de6c74 177/* Debugging file. All printouts are sent to dump, which is always set,
8c660648 178 either to stderr, or to the dump listing file (-dRS). */
c62c2659 179FILE *sched_dump = 0;
a88f02e7
BS
180
181/* Highest uid before scheduling. */
182static int old_max_uid;
8c660648
JL
183
184/* fix_sched_param() is called from toplev.c upon detection
409f8483 185 of the -fsched-verbose=N option. */
8c660648
JL
186
187void
188fix_sched_param (param, val)
5f06c983 189 const char *param, *val;
8c660648 190{
cc132865 191 if (!strcmp (param, "verbose"))
8c660648 192 sched_verbose_param = atoi (val);
8c660648
JL
193 else
194 warning ("fix_sched_param: unknown param: %s", param);
195}
196
16f6ece6 197struct haifa_insn_data *h_i_d;
f66d83e1 198
8c660648
JL
199#define DONE_PRIORITY -1
200#define MAX_PRIORITY 0x7fffffff
201#define TAIL_PRIORITY 0x7ffffffe
202#define LAUNCH_PRIORITY 0x7f000001
203#define DONE_PRIORITY_P(INSN) (INSN_PRIORITY (INSN) < 0)
204#define LOW_PRIORITY_P(INSN) ((INSN_PRIORITY (INSN) & 0x7f000000) == 0)
205
f66d83e1
RH
206#define LINE_NOTE(INSN) (h_i_d[INSN_UID (INSN)].line_note)
207#define INSN_TICK(INSN) (h_i_d[INSN_UID (INSN)].tick)
8c660648
JL
208
209/* Vector indexed by basic block number giving the starting line-number
210 for each basic block. */
211static rtx *line_note_head;
212
213/* List of important notes we must keep around. This is a pointer to the
214 last element in the list. */
215static rtx note_list;
216
8c660648
JL
217/* Queues, etc. */
218
219/* An instruction is ready to be scheduled when all insns preceding it
220 have already been scheduled. It is important to ensure that all
221 insns which use its result will not be executed until its result
222 has been computed. An insn is maintained in one of four structures:
223
224 (P) the "Pending" set of insns which cannot be scheduled until
225 their dependencies have been satisfied.
226 (Q) the "Queued" set of insns that can be scheduled when sufficient
227 time has passed.
228 (R) the "Ready" list of unscheduled, uncommitted insns.
229 (S) the "Scheduled" list of insns.
230
231 Initially, all insns are either "Pending" or "Ready" depending on
232 whether their dependencies are satisfied.
233
234 Insns move from the "Ready" list to the "Scheduled" list as they
235 are committed to the schedule. As this occurs, the insns in the
236 "Pending" list have their dependencies satisfied and move to either
237 the "Ready" list or the "Queued" set depending on whether
238 sufficient time has passed to make them ready. As time passes,
239 insns move from the "Queued" set to the "Ready" list. Insns may
240 move from the "Ready" list to the "Queued" set if they are blocked
241 due to a function unit conflict.
242
243 The "Pending" list (P) are the insns in the INSN_DEPEND of the unscheduled
244 insns, i.e., those that are ready, queued, and pending.
245 The "Queued" set (Q) is implemented by the variable `insn_queue'.
246 The "Ready" list (R) is implemented by the variables `ready' and
247 `n_ready'.
248 The "Scheduled" list (S) is the new insn chain built by this pass.
249
250 The transition (R->S) is implemented in the scheduling loop in
251 `schedule_block' when the best insn to schedule is chosen.
252 The transition (R->Q) is implemented in `queue_insn' when an
38e01259 253 insn is found to have a function unit conflict with the already
8c660648
JL
254 committed insns.
255 The transitions (P->R and P->Q) are implemented in `schedule_insn' as
256 insns move from the ready list to the scheduled list.
257 The transition (Q->R) is implemented in 'queue_to_insn' as time
258 passes or stalls are introduced. */
259
260/* Implement a circular buffer to delay instructions until sufficient
261 time has passed. INSN_QUEUE_SIZE is a power of two larger than
262 MAX_BLOCKAGE and MAX_READY_COST computed by genattr.c. This is the
263 longest time an isnsn may be queued. */
264static rtx insn_queue[INSN_QUEUE_SIZE];
265static int q_ptr = 0;
266static int q_size = 0;
267#define NEXT_Q(X) (((X)+1) & (INSN_QUEUE_SIZE-1))
268#define NEXT_Q_AFTER(X, C) (((X)+C) & (INSN_QUEUE_SIZE-1))
269
176f9a7b
BS
270/* Describe the ready list of the scheduler.
271 VEC holds space enough for all insns in the current region. VECLEN
272 says how many exactly.
273 FIRST is the index of the element with the highest priority; i.e. the
274 last one in the ready list, since elements are ordered by ascending
275 priority.
276 N_READY determines how many insns are on the ready list. */
277
278struct ready_list
279{
280 rtx *vec;
281 int veclen;
282 int first;
283 int n_ready;
284};
285
8c660648 286/* Forward declarations. */
3fe41456
KG
287static unsigned int blockage_range PARAMS ((int, rtx));
288static void clear_units PARAMS ((void));
3fe41456
KG
289static void schedule_unit PARAMS ((int, rtx, int));
290static int actual_hazard PARAMS ((int, rtx, int, int));
291static int potential_hazard PARAMS ((int, rtx, int));
3fe41456 292static int priority PARAMS ((rtx));
3fe41456
KG
293static int rank_for_schedule PARAMS ((const PTR, const PTR));
294static void swap_sort PARAMS ((rtx *, int));
295static void queue_insn PARAMS ((rtx, int));
176f9a7b 296static void schedule_insn PARAMS ((rtx, struct ready_list *, int));
3fe41456 297static void find_insn_reg_weight PARAMS ((int));
3fe41456 298static void adjust_priority PARAMS ((rtx));
8c660648 299
8c660648
JL
300/* Notes handling mechanism:
301 =========================
302 Generally, NOTES are saved before scheduling and restored after scheduling.
303 The scheduler distinguishes between three types of notes:
304
305 (1) LINE_NUMBER notes, generated and used for debugging. Here,
306 before scheduling a region, a pointer to the LINE_NUMBER note is
307 added to the insn following it (in save_line_notes()), and the note
308 is removed (in rm_line_notes() and unlink_line_notes()). After
309 scheduling the region, this pointer is used for regeneration of
310 the LINE_NUMBER note (in restore_line_notes()).
311
312 (2) LOOP_BEGIN, LOOP_END, SETJMP, EHREGION_BEG, EHREGION_END notes:
313 Before scheduling a region, a pointer to the note is added to the insn
314 that follows or precedes it. (This happens as part of the data dependence
315 computation). After scheduling an insn, the pointer contained in it is
316 used for regenerating the corresponding note (in reemit_notes).
317
318 (3) All other notes (e.g. INSN_DELETED): Before scheduling a block,
319 these notes are put in a list (in rm_other_notes() and
320 unlink_other_notes ()). After scheduling the block, these notes are
321 inserted at the beginning of the block (in schedule_block()). */
322
3fe41456
KG
323static rtx unlink_other_notes PARAMS ((rtx, rtx));
324static rtx unlink_line_notes PARAMS ((rtx, rtx));
3fe41456
KG
325static rtx reemit_notes PARAMS ((rtx, rtx));
326
176f9a7b
BS
327static rtx *ready_lastpos PARAMS ((struct ready_list *));
328static void ready_sort PARAMS ((struct ready_list *));
329static rtx ready_remove_first PARAMS ((struct ready_list *));
3fe41456 330
176f9a7b
BS
331static void queue_to_ready PARAMS ((struct ready_list *));
332
333static void debug_ready_list PARAMS ((struct ready_list *));
3fe41456
KG
334
335static rtx move_insn1 PARAMS ((rtx, rtx));
336static rtx move_insn PARAMS ((rtx, rtx));
8c660648
JL
337
338#endif /* INSN_SCHEDULING */
339\f
1708fd40
BS
340/* Point to state used for the current scheduling pass. */
341struct sched_info *current_sched_info;
8c660648
JL
342\f
343#ifndef INSN_SCHEDULING
344void
345schedule_insns (dump_file)
7bdb32b9 346 FILE *dump_file ATTRIBUTE_UNUSED;
8c660648
JL
347{
348}
349#else
cbb13457 350
8c660648
JL
351/* Pointer to the last instruction scheduled. Used by rank_for_schedule,
352 so that insns independent of the last scheduled insn will be preferred
353 over dependent instructions. */
354
355static rtx last_scheduled_insn;
356
b4ead7d4
BS
357/* Compute the function units used by INSN. This caches the value
358 returned by function_units_used. A function unit is encoded as the
359 unit number if the value is non-negative and the compliment of a
360 mask if the value is negative. A function unit index is the
361 non-negative encoding. */
168cbdf9 362
b4ead7d4
BS
363HAIFA_INLINE int
364insn_unit (insn)
365 rtx insn;
8c660648 366{
b4ead7d4 367 register int unit = INSN_UNIT (insn);
168cbdf9 368
b4ead7d4 369 if (unit == 0)
6b8cf0c5 370 {
b4ead7d4 371 recog_memoized (insn);
8c660648 372
b4ead7d4
BS
373 /* A USE insn, or something else we don't need to understand.
374 We can't pass these directly to function_units_used because it will
375 trigger a fatal error for unrecognizable insns. */
376 if (INSN_CODE (insn) < 0)
377 unit = -1;
378 else
8c660648 379 {
b4ead7d4
BS
380 unit = function_units_used (insn);
381 /* Increment non-negative values so we can cache zero. */
382 if (unit >= 0)
383 unit++;
8c660648 384 }
b4ead7d4
BS
385 /* We only cache 16 bits of the result, so if the value is out of
386 range, don't cache it. */
387 if (FUNCTION_UNITS_SIZE < HOST_BITS_PER_SHORT
388 || unit >= 0
389 || (unit & ~((1 << (HOST_BITS_PER_SHORT - 1)) - 1)) == 0)
390 INSN_UNIT (insn) = unit;
8c660648 391 }
b4ead7d4
BS
392 return (unit > 0 ? unit - 1 : unit);
393}
8c660648 394
b4ead7d4
BS
395/* Compute the blockage range for executing INSN on UNIT. This caches
396 the value returned by the blockage_range_function for the unit.
397 These values are encoded in an int where the upper half gives the
398 minimum value and the lower half gives the maximum value. */
8c660648 399
b4ead7d4
BS
400HAIFA_INLINE static unsigned int
401blockage_range (unit, insn)
402 int unit;
403 rtx insn;
404{
405 unsigned int blockage = INSN_BLOCKAGE (insn);
406 unsigned int range;
8c660648 407
b4ead7d4 408 if ((int) UNIT_BLOCKED (blockage) != unit + 1)
8c660648 409 {
b4ead7d4
BS
410 range = function_units[unit].blockage_range_function (insn);
411 /* We only cache the blockage range for one unit and then only if
412 the values fit. */
413 if (HOST_BITS_PER_INT >= UNIT_BITS + 2 * BLOCKAGE_BITS)
414 INSN_BLOCKAGE (insn) = ENCODE_BLOCKAGE (unit + 1, range);
8c660648
JL
415 }
416 else
b4ead7d4 417 range = BLOCKAGE_RANGE (blockage);
8c660648 418
b4ead7d4 419 return range;
8c660648
JL
420}
421
b4ead7d4
BS
422/* A vector indexed by function unit instance giving the last insn to use
423 the unit. The value of the function unit instance index for unit U
424 instance I is (U + I * FUNCTION_UNITS_SIZE). */
425static rtx unit_last_insn[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
8c660648 426
b4ead7d4
BS
427/* A vector indexed by function unit instance giving the minimum time when
428 the unit will unblock based on the maximum blockage cost. */
429static int unit_tick[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
430
431/* A vector indexed by function unit number giving the number of insns
432 that remain to use the unit. */
433static int unit_n_insns[FUNCTION_UNITS_SIZE];
8c660648 434
b4ead7d4 435/* Access the unit_last_insn array. Used by the visualization code. */
8c660648 436
b4ead7d4
BS
437rtx
438get_unit_last_insn (instance)
439 int instance;
8c660648 440{
b4ead7d4 441 return unit_last_insn[instance];
8c660648
JL
442}
443
b4ead7d4 444/* Reset the function unit state to the null state. */
8c660648
JL
445
446static void
b4ead7d4 447clear_units ()
8c660648 448{
b4ead7d4
BS
449 memset ((char *) unit_last_insn, 0, sizeof (unit_last_insn));
450 memset ((char *) unit_tick, 0, sizeof (unit_tick));
451 memset ((char *) unit_n_insns, 0, sizeof (unit_n_insns));
452}
8c660648 453
b4ead7d4 454/* Return the issue-delay of an insn. */
8c660648 455
b4ead7d4
BS
456HAIFA_INLINE int
457insn_issue_delay (insn)
458 rtx insn;
459{
460 int i, delay = 0;
461 int unit = insn_unit (insn);
8c660648 462
b4ead7d4
BS
463 /* Efficiency note: in fact, we are working 'hard' to compute a
464 value that was available in md file, and is not available in
465 function_units[] structure. It would be nice to have this
466 value there, too. */
467 if (unit >= 0)
8c660648 468 {
b4ead7d4
BS
469 if (function_units[unit].blockage_range_function &&
470 function_units[unit].blockage_function)
471 delay = function_units[unit].blockage_function (insn, insn);
8c660648 472 }
b4ead7d4
BS
473 else
474 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
475 if ((unit & 1) != 0 && function_units[i].blockage_range_function
476 && function_units[i].blockage_function)
477 delay = MAX (delay, function_units[i].blockage_function (insn, insn));
8c660648 478
b4ead7d4 479 return delay;
8c660648
JL
480}
481
b4ead7d4
BS
482/* Return the actual hazard cost of executing INSN on the unit UNIT,
483 instance INSTANCE at time CLOCK if the previous actual hazard cost
484 was COST. */
8c660648 485
b4ead7d4
BS
486HAIFA_INLINE int
487actual_hazard_this_instance (unit, instance, insn, clock, cost)
488 int unit, instance, clock, cost;
489 rtx insn;
8c660648 490{
b4ead7d4 491 int tick = unit_tick[instance]; /* Issue time of the last issued insn. */
8c660648 492
b4ead7d4 493 if (tick - clock > cost)
8c660648 494 {
b4ead7d4
BS
495 /* The scheduler is operating forward, so unit's last insn is the
496 executing insn and INSN is the candidate insn. We want a
497 more exact measure of the blockage if we execute INSN at CLOCK
498 given when we committed the execution of the unit's last insn.
8c660648 499
b4ead7d4
BS
500 The blockage value is given by either the unit's max blockage
501 constant, blockage range function, or blockage function. Use
502 the most exact form for the given unit. */
8c660648 503
b4ead7d4
BS
504 if (function_units[unit].blockage_range_function)
505 {
506 if (function_units[unit].blockage_function)
507 tick += (function_units[unit].blockage_function
508 (unit_last_insn[instance], insn)
509 - function_units[unit].max_blockage);
510 else
511 tick += ((int) MAX_BLOCKAGE_COST (blockage_range (unit, insn))
512 - function_units[unit].max_blockage);
8c660648 513 }
b4ead7d4
BS
514 if (tick - clock > cost)
515 cost = tick - clock;
8c660648 516 }
b4ead7d4 517 return cost;
8c660648
JL
518}
519
b4ead7d4
BS
520/* Record INSN as having begun execution on the units encoded by UNIT at
521 time CLOCK. */
8c660648 522
cbb13457 523HAIFA_INLINE static void
8c660648
JL
524schedule_unit (unit, insn, clock)
525 int unit, clock;
526 rtx insn;
527{
528 int i;
529
530 if (unit >= 0)
531 {
532 int instance = unit;
533#if MAX_MULTIPLICITY > 1
534 /* Find the first free instance of the function unit and use that
535 one. We assume that one is free. */
536 for (i = function_units[unit].multiplicity - 1; i > 0; i--)
537 {
538 if (!actual_hazard_this_instance (unit, instance, insn, clock, 0))
539 break;
540 instance += FUNCTION_UNITS_SIZE;
541 }
542#endif
543 unit_last_insn[instance] = insn;
544 unit_tick[instance] = (clock + function_units[unit].max_blockage);
545 }
546 else
547 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
548 if ((unit & 1) != 0)
549 schedule_unit (i, insn, clock);
550}
551
552/* Return the actual hazard cost of executing INSN on the units encoded by
553 UNIT at time CLOCK if the previous actual hazard cost was COST. */
554
cbb13457 555HAIFA_INLINE static int
8c660648
JL
556actual_hazard (unit, insn, clock, cost)
557 int unit, clock, cost;
558 rtx insn;
559{
560 int i;
561
562 if (unit >= 0)
563 {
564 /* Find the instance of the function unit with the minimum hazard. */
565 int instance = unit;
566 int best_cost = actual_hazard_this_instance (unit, instance, insn,
567 clock, cost);
1eda7a81 568#if MAX_MULTIPLICITY > 1
8c660648
JL
569 int this_cost;
570
8c660648
JL
571 if (best_cost > cost)
572 {
573 for (i = function_units[unit].multiplicity - 1; i > 0; i--)
574 {
575 instance += FUNCTION_UNITS_SIZE;
576 this_cost = actual_hazard_this_instance (unit, instance, insn,
577 clock, cost);
578 if (this_cost < best_cost)
579 {
580 best_cost = this_cost;
581 if (this_cost <= cost)
582 break;
583 }
584 }
585 }
586#endif
587 cost = MAX (cost, best_cost);
588 }
589 else
590 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
591 if ((unit & 1) != 0)
592 cost = actual_hazard (i, insn, clock, cost);
593
594 return cost;
595}
596
597/* Return the potential hazard cost of executing an instruction on the
598 units encoded by UNIT if the previous potential hazard cost was COST.
599 An insn with a large blockage time is chosen in preference to one
600 with a smaller time; an insn that uses a unit that is more likely
601 to be used is chosen in preference to one with a unit that is less
602 used. We are trying to minimize a subsequent actual hazard. */
603
cbb13457 604HAIFA_INLINE static int
8c660648
JL
605potential_hazard (unit, insn, cost)
606 int unit, cost;
607 rtx insn;
608{
609 int i, ncost;
610 unsigned int minb, maxb;
611
612 if (unit >= 0)
613 {
614 minb = maxb = function_units[unit].max_blockage;
615 if (maxb > 1)
616 {
617 if (function_units[unit].blockage_range_function)
618 {
619 maxb = minb = blockage_range (unit, insn);
620 maxb = MAX_BLOCKAGE_COST (maxb);
621 minb = MIN_BLOCKAGE_COST (minb);
622 }
623
624 if (maxb > 1)
625 {
626 /* Make the number of instructions left dominate. Make the
627 minimum delay dominate the maximum delay. If all these
628 are the same, use the unit number to add an arbitrary
629 ordering. Other terms can be added. */
630 ncost = minb * 0x40 + maxb;
631 ncost *= (unit_n_insns[unit] - 1) * 0x1000 + unit;
632 if (ncost > cost)
633 cost = ncost;
634 }
635 }
636 }
637 else
638 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
639 if ((unit & 1) != 0)
640 cost = potential_hazard (i, insn, cost);
641
642 return cost;
643}
644
645/* Compute cost of executing INSN given the dependence LINK on the insn USED.
646 This is the number of cycles between instruction issue and
647 instruction results. */
648
b4ead7d4 649HAIFA_INLINE int
8c660648
JL
650insn_cost (insn, link, used)
651 rtx insn, link, used;
652{
653 register int cost = INSN_COST (insn);
654
655 if (cost == 0)
656 {
657 recog_memoized (insn);
658
659 /* A USE insn, or something else we don't need to understand.
660 We can't pass these directly to result_ready_cost because it will
661 trigger a fatal error for unrecognizable insns. */
662 if (INSN_CODE (insn) < 0)
663 {
664 INSN_COST (insn) = 1;
665 return 1;
666 }
667 else
668 {
669 cost = result_ready_cost (insn);
670
671 if (cost < 1)
672 cost = 1;
673
674 INSN_COST (insn) = cost;
675 }
676 }
677
63de6c74 678 /* In this case estimate cost without caring how insn is used. */
8c660648
JL
679 if (link == 0 && used == 0)
680 return cost;
681
682 /* A USE insn should never require the value used to be computed. This
683 allows the computation of a function's result and parameter values to
684 overlap the return and call. */
685 recog_memoized (used);
686 if (INSN_CODE (used) < 0)
687 LINK_COST_FREE (link) = 1;
688
689 /* If some dependencies vary the cost, compute the adjustment. Most
690 commonly, the adjustment is complete: either the cost is ignored
691 (in the case of an output- or anti-dependence), or the cost is
692 unchanged. These values are cached in the link as LINK_COST_FREE
693 and LINK_COST_ZERO. */
694
695 if (LINK_COST_FREE (link))
197043f5 696 cost = 0;
8c660648
JL
697#ifdef ADJUST_COST
698 else if (!LINK_COST_ZERO (link))
699 {
700 int ncost = cost;
701
702 ADJUST_COST (used, link, insn, ncost);
197043f5
RH
703 if (ncost < 1)
704 {
705 LINK_COST_FREE (link) = 1;
706 ncost = 0;
707 }
8c660648
JL
708 if (cost == ncost)
709 LINK_COST_ZERO (link) = 1;
710 cost = ncost;
711 }
712#endif
713 return cost;
714}
715
716/* Compute the priority number for INSN. */
717
718static int
719priority (insn)
720 rtx insn;
721{
722 int this_priority;
723 rtx link;
724
2c3c49de 725 if (! INSN_P (insn))
8c660648
JL
726 return 0;
727
728 if ((this_priority = INSN_PRIORITY (insn)) == 0)
729 {
730 if (INSN_DEPEND (insn) == 0)
731 this_priority = insn_cost (insn, 0, 0);
732 else
733 for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
734 {
735 rtx next;
736 int next_priority;
737
6d8ccdbb
JL
738 if (RTX_INTEGRATED_P (link))
739 continue;
740
8c660648
JL
741 next = XEXP (link, 0);
742
63de6c74 743 /* Critical path is meaningful in block boundaries only. */
18e720b3 744 if (! (*current_sched_info->contributes_to_priority) (next, insn))
8c660648
JL
745 continue;
746
747 next_priority = insn_cost (insn, link, next) + priority (next);
748 if (next_priority > this_priority)
749 this_priority = next_priority;
750 }
751 INSN_PRIORITY (insn) = this_priority;
752 }
753 return this_priority;
754}
755\f
8c660648
JL
756/* Macros and functions for keeping the priority queue sorted, and
757 dealing with queueing and dequeueing of instructions. */
758
759#define SCHED_SORT(READY, N_READY) \
760do { if ((N_READY) == 2) \
761 swap_sort (READY, N_READY); \
762 else if ((N_READY) > 2) \
763 qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); } \
764while (0)
765
766/* Returns a positive value if x is preferred; returns a negative value if
767 y is preferred. Should never return 0, since that will make the sort
768 unstable. */
769
770static int
771rank_for_schedule (x, y)
e1b6684c
KG
772 const PTR x;
773 const PTR y;
8c660648 774{
7a403706
KH
775 rtx tmp = *(const rtx *) y;
776 rtx tmp2 = *(const rtx *) x;
8c660648 777 rtx link;
2db45993 778 int tmp_class, tmp2_class, depend_count1, depend_count2;
1708fd40 779 int val, priority_val, weight_val, info_val;
8c660648 780
63de6c74 781 /* Prefer insn with higher priority. */
8c660648
JL
782 priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
783 if (priority_val)
784 return priority_val;
785
63de6c74 786 /* Prefer an insn with smaller contribution to registers-pressure. */
8c660648
JL
787 if (!reload_completed &&
788 (weight_val = INSN_REG_WEIGHT (tmp) - INSN_REG_WEIGHT (tmp2)))
789 return (weight_val);
790
1708fd40
BS
791 info_val = (*current_sched_info->rank) (tmp, tmp2);
792 if (info_val)
793 return info_val;
8c660648 794
63de6c74 795 /* Compare insns based on their relation to the last-scheduled-insn. */
8c660648
JL
796 if (last_scheduled_insn)
797 {
798 /* Classify the instructions into three classes:
799 1) Data dependent on last schedule insn.
800 2) Anti/Output dependent on last scheduled insn.
801 3) Independent of last scheduled insn, or has latency of one.
802 Choose the insn from the highest numbered class if different. */
803 link = find_insn_list (tmp, INSN_DEPEND (last_scheduled_insn));
804 if (link == 0 || insn_cost (last_scheduled_insn, link, tmp) == 1)
805 tmp_class = 3;
806 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
807 tmp_class = 1;
808 else
809 tmp_class = 2;
810
811 link = find_insn_list (tmp2, INSN_DEPEND (last_scheduled_insn));
812 if (link == 0 || insn_cost (last_scheduled_insn, link, tmp2) == 1)
813 tmp2_class = 3;
814 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
815 tmp2_class = 1;
816 else
817 tmp2_class = 2;
818
819 if ((val = tmp2_class - tmp_class))
820 return val;
821 }
822
7a403706 823 /* Prefer the insn which has more later insns that depend on it.
2db45993
JL
824 This gives the scheduler more freedom when scheduling later
825 instructions at the expense of added register pressure. */
826 depend_count1 = 0;
827 for (link = INSN_DEPEND (tmp); link; link = XEXP (link, 1))
828 depend_count1++;
829
830 depend_count2 = 0;
831 for (link = INSN_DEPEND (tmp2); link; link = XEXP (link, 1))
832 depend_count2++;
833
834 val = depend_count2 - depend_count1;
835 if (val)
836 return val;
7a403706 837
8c660648
JL
838 /* If insns are equally good, sort by INSN_LUID (original insn order),
839 so that we make the sort stable. This minimizes instruction movement,
840 thus minimizing sched's effect on debugging and cross-jumping. */
841 return INSN_LUID (tmp) - INSN_LUID (tmp2);
842}
843
844/* Resort the array A in which only element at index N may be out of order. */
845
cbb13457 846HAIFA_INLINE static void
8c660648
JL
847swap_sort (a, n)
848 rtx *a;
849 int n;
850{
851 rtx insn = a[n - 1];
852 int i = n - 2;
853
854 while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
855 {
856 a[i + 1] = a[i];
857 i -= 1;
858 }
859 a[i + 1] = insn;
860}
861
8c660648
JL
862/* Add INSN to the insn queue so that it can be executed at least
863 N_CYCLES after the currently executing insn. Preserve insns
864 chain for debugging purposes. */
865
cbb13457 866HAIFA_INLINE static void
8c660648
JL
867queue_insn (insn, n_cycles)
868 rtx insn;
869 int n_cycles;
870{
871 int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
ebb7b10b 872 rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
8c660648
JL
873 insn_queue[next_q] = link;
874 q_size += 1;
875
876 if (sched_verbose >= 2)
877 {
1708fd40
BS
878 fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
879 (*current_sched_info->print_insn) (insn, 0));
8c660648 880
a88f02e7 881 fprintf (sched_dump, "queued for %d cycles.\n", n_cycles);
8c660648 882 }
176f9a7b
BS
883}
884
885/* Return a pointer to the bottom of the ready list, i.e. the insn
886 with the lowest priority. */
887
888HAIFA_INLINE static rtx *
889ready_lastpos (ready)
890 struct ready_list *ready;
891{
892 if (ready->n_ready == 0)
893 abort ();
894 return ready->vec + ready->first - ready->n_ready + 1;
895}
896
897/* Add an element INSN to the ready list so that it ends up with the lowest
898 priority. */
899
b4ead7d4 900HAIFA_INLINE void
176f9a7b
BS
901ready_add (ready, insn)
902 struct ready_list *ready;
903 rtx insn;
904{
905 if (ready->first == ready->n_ready)
906 {
907 memmove (ready->vec + ready->veclen - ready->n_ready,
908 ready_lastpos (ready),
909 ready->n_ready * sizeof (rtx));
910 ready->first = ready->veclen - 1;
911 }
912 ready->vec[ready->first - ready->n_ready] = insn;
913 ready->n_ready++;
914}
8c660648 915
176f9a7b
BS
916/* Remove the element with the highest priority from the ready list and
917 return it. */
918
919HAIFA_INLINE static rtx
920ready_remove_first (ready)
921 struct ready_list *ready;
922{
923 rtx t;
924 if (ready->n_ready == 0)
925 abort ();
926 t = ready->vec[ready->first--];
927 ready->n_ready--;
928 /* If the queue becomes empty, reset it. */
929 if (ready->n_ready == 0)
930 ready->first = ready->veclen - 1;
931 return t;
932}
933
934/* Sort the ready list READY by ascending priority, using the SCHED_SORT
935 macro. */
936
937HAIFA_INLINE static void
938ready_sort (ready)
939 struct ready_list *ready;
940{
941 rtx *first = ready_lastpos (ready);
942 SCHED_SORT (first, ready->n_ready);
8c660648
JL
943}
944
8c660648 945/* PREV is an insn that is ready to execute. Adjust its priority if that
c46a37c4
RH
946 will help shorten or lengthen register lifetimes as appropriate. Also
947 provide a hook for the target to tweek itself. */
8c660648 948
cbb13457 949HAIFA_INLINE static void
8c660648 950adjust_priority (prev)
c46a37c4 951 rtx prev ATTRIBUTE_UNUSED;
8c660648 952{
c46a37c4
RH
953 /* ??? There used to be code here to try and estimate how an insn
954 affected register lifetimes, but it did it by looking at REG_DEAD
7a403706 955 notes, which we removed in schedule_region. Nor did it try to
c46a37c4 956 take into account register pressure or anything useful like that.
8c660648 957
c46a37c4 958 Revisit when we have a machine model to work with and not before. */
197043f5 959
8c660648 960#ifdef ADJUST_PRIORITY
197043f5 961 ADJUST_PRIORITY (prev);
8c660648 962#endif
8c660648
JL
963}
964
4bdc8810
RH
965/* Clock at which the previous instruction was issued. */
966static int last_clock_var;
967
8c660648 968/* INSN is the "currently executing insn". Launch each insn which was
176f9a7b
BS
969 waiting on INSN. READY is the ready list which contains the insns
970 that are ready to fire. CLOCK is the current cycle.
971 */
8c660648 972
176f9a7b
BS
973static void
974schedule_insn (insn, ready, clock)
8c660648 975 rtx insn;
176f9a7b 976 struct ready_list *ready;
8c660648
JL
977 int clock;
978{
979 rtx link;
980 int unit;
981
982 unit = insn_unit (insn);
983
984 if (sched_verbose >= 2)
985 {
a88f02e7 986 fprintf (sched_dump, ";;\t\t--> scheduling insn <<<%d>>> on unit ",
63de6c74 987 INSN_UID (insn));
8c660648 988 insn_print_units (insn);
a88f02e7 989 fprintf (sched_dump, "\n");
8c660648
JL
990 }
991
992 if (sched_verbose && unit == -1)
993 visualize_no_unit (insn);
994
995 if (MAX_BLOCKAGE > 1 || issue_rate > 1 || sched_verbose)
996 schedule_unit (unit, insn, clock);
997
998 if (INSN_DEPEND (insn) == 0)
176f9a7b 999 return;
8c660648
JL
1000
1001 for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
1002 {
1003 rtx next = XEXP (link, 0);
1004 int cost = insn_cost (insn, link, next);
1005
1006 INSN_TICK (next) = MAX (INSN_TICK (next), clock + cost);
1007
1008 if ((INSN_DEP_COUNT (next) -= 1) == 0)
1009 {
1010 int effective_cost = INSN_TICK (next) - clock;
1011
1708fd40 1012 if (! (*current_sched_info->new_ready) (next))
8c660648
JL
1013 continue;
1014
1015 if (sched_verbose >= 2)
1016 {
1708fd40
BS
1017 fprintf (sched_dump, ";;\t\tdependences resolved: insn %s ",
1018 (*current_sched_info->print_insn) (next, 0));
8c660648 1019
197043f5 1020 if (effective_cost < 1)
a88f02e7 1021 fprintf (sched_dump, "into ready\n");
8c660648 1022 else
a88f02e7 1023 fprintf (sched_dump, "into queue with cost=%d\n", effective_cost);
8c660648
JL
1024 }
1025
1026 /* Adjust the priority of NEXT and either put it on the ready
1027 list or queue it. */
1028 adjust_priority (next);
197043f5 1029 if (effective_cost < 1)
176f9a7b 1030 ready_add (ready, next);
8c660648
JL
1031 else
1032 queue_insn (next, effective_cost);
1033 }
1034 }
1035
7a403706 1036 /* Annotate the instruction with issue information -- TImode
4bdc8810
RH
1037 indicates that the instruction is expected not to be able
1038 to issue on the same cycle as the previous insn. A machine
1039 may use this information to decide how the instruction should
1040 be aligned. */
1041 if (reload_completed && issue_rate > 1)
1042 {
1043 PUT_MODE (insn, clock > last_clock_var ? TImode : VOIDmode);
1044 last_clock_var = clock;
1045 }
8c660648
JL
1046}
1047
63de6c74 1048/* Functions for handling of notes. */
8c660648
JL
1049
1050/* Delete notes beginning with INSN and put them in the chain
1051 of notes ended by NOTE_LIST.
1052 Returns the insn following the notes. */
1053
1054static rtx
1055unlink_other_notes (insn, tail)
1056 rtx insn, tail;
1057{
1058 rtx prev = PREV_INSN (insn);
1059
1060 while (insn != tail && GET_CODE (insn) == NOTE)
1061 {
1062 rtx next = NEXT_INSN (insn);
1063 /* Delete the note from its current position. */
1064 if (prev)
1065 NEXT_INSN (prev) = next;
1066 if (next)
1067 PREV_INSN (next) = prev;
1068
c46a37c4 1069 /* See sched_analyze to see how these are handled. */
8c660648
JL
1070 if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_SETJMP
1071 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG
1072 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_END
b3b42a4d 1073 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_RANGE_BEG
0dfa1860 1074 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_RANGE_END
8c660648
JL
1075 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_BEG
1076 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_END)
1077 {
1078 /* Insert the note at the end of the notes list. */
1079 PREV_INSN (insn) = note_list;
1080 if (note_list)
1081 NEXT_INSN (note_list) = insn;
1082 note_list = insn;
1083 }
1084
1085 insn = next;
1086 }
1087 return insn;
1088}
1089
1090/* Delete line notes beginning with INSN. Record line-number notes so
1091 they can be reused. Returns the insn following the notes. */
1092
1093static rtx
1094unlink_line_notes (insn, tail)
1095 rtx insn, tail;
1096{
1097 rtx prev = PREV_INSN (insn);
1098
1099 while (insn != tail && GET_CODE (insn) == NOTE)
1100 {
1101 rtx next = NEXT_INSN (insn);
1102
1103 if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
1104 {
1105 /* Delete the note from its current position. */
1106 if (prev)
1107 NEXT_INSN (prev) = next;
1108 if (next)
1109 PREV_INSN (next) = prev;
1110
1111 /* Record line-number notes so they can be reused. */
1112 LINE_NOTE (insn) = insn;
1113 }
1114 else
1115 prev = insn;
1116
1117 insn = next;
1118 }
1119 return insn;
1120}
1121
1122/* Return the head and tail pointers of BB. */
1123
b4ead7d4 1124void
49c3bb12
RH
1125get_block_head_tail (b, headp, tailp)
1126 int b;
8c660648
JL
1127 rtx *headp;
1128 rtx *tailp;
1129{
8c660648 1130 /* HEAD and TAIL delimit the basic block being scheduled. */
1708fd40
BS
1131 rtx head = BLOCK_HEAD (b);
1132 rtx tail = BLOCK_END (b);
8c660648
JL
1133
1134 /* Don't include any notes or labels at the beginning of the
1135 basic block, or notes at the ends of basic blocks. */
1136 while (head != tail)
1137 {
1138 if (GET_CODE (head) == NOTE)
1139 head = NEXT_INSN (head);
1140 else if (GET_CODE (tail) == NOTE)
1141 tail = PREV_INSN (tail);
1142 else if (GET_CODE (head) == CODE_LABEL)
1143 head = NEXT_INSN (head);
1144 else
1145 break;
1146 }
1147
1148 *headp = head;
1149 *tailp = tail;
1150}
1151
1708fd40
BS
1152/* Return nonzero if there are no real insns in the range [ HEAD, TAIL ]. */
1153
b4ead7d4 1154int
1708fd40
BS
1155no_real_insns_p (head, tail)
1156 rtx head, tail;
1157{
1158 while (head != NEXT_INSN (tail))
1159 {
1160 if (GET_CODE (head) != NOTE && GET_CODE (head) != CODE_LABEL)
1161 return 0;
1162 head = NEXT_INSN (head);
1163 }
1164 return 1;
1165}
1166
79c2ffde
BS
1167/* Delete line notes from one block. Save them so they can be later restored
1168 (in restore_line_notes). HEAD and TAIL are the boundaries of the
1169 block in which notes should be processed. */
8c660648 1170
b4ead7d4 1171void
79c2ffde
BS
1172rm_line_notes (head, tail)
1173 rtx head, tail;
8c660648
JL
1174{
1175 rtx next_tail;
8c660648
JL
1176 rtx insn;
1177
8c660648
JL
1178 next_tail = NEXT_INSN (tail);
1179 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1180 {
1181 rtx prev;
1182
1183 /* Farm out notes, and maybe save them in NOTE_LIST.
1184 This is needed to keep the debugger from
1185 getting completely deranged. */
1186 if (GET_CODE (insn) == NOTE)
1187 {
1188 prev = insn;
1189 insn = unlink_line_notes (insn, next_tail);
1190
1191 if (prev == tail)
1192 abort ();
1193 if (prev == head)
1194 abort ();
1195 if (insn == next_tail)
1196 abort ();
1197 }
1198 }
1199}
1200
79c2ffde
BS
1201/* Save line number notes for each insn in block B. HEAD and TAIL are
1202 the boundaries of the block in which notes should be processed.*/
8c660648 1203
b4ead7d4 1204void
79c2ffde 1205save_line_notes (b, head, tail)
b4ead7d4 1206 int b;
79c2ffde 1207 rtx head, tail;
8c660648 1208{
8c660648
JL
1209 rtx next_tail;
1210
1211 /* We must use the true line number for the first insn in the block
1212 that was computed and saved at the start of this pass. We can't
1213 use the current line number, because scheduling of the previous
1214 block may have changed the current line number. */
1215
b4ead7d4 1216 rtx line = line_note_head[b];
8c660648
JL
1217 rtx insn;
1218
8c660648
JL
1219 next_tail = NEXT_INSN (tail);
1220
79c2ffde 1221 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
8c660648
JL
1222 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1223 line = insn;
1224 else
1225 LINE_NOTE (insn) = line;
1226}
1227
79c2ffde
BS
1228/* After block B was scheduled, insert line notes into the insns list.
1229 HEAD and TAIL are the boundaries of the block in which notes should
1230 be processed.*/
8c660648 1231
b4ead7d4 1232void
79c2ffde 1233restore_line_notes (b, head, tail)
b4ead7d4 1234 int b;
79c2ffde 1235 rtx head, tail;
8c660648
JL
1236{
1237 rtx line, note, prev, new;
1238 int added_notes = 0;
79c2ffde 1239 rtx next_tail, insn;
8c660648 1240
79c2ffde
BS
1241 head = head;
1242 next_tail = NEXT_INSN (tail);
8c660648
JL
1243
1244 /* Determine the current line-number. We want to know the current
1245 line number of the first insn of the block here, in case it is
1246 different from the true line number that was saved earlier. If
1247 different, then we need a line number note before the first insn
1248 of this block. If it happens to be the same, then we don't want to
1249 emit another line number note here. */
1250 for (line = head; line; line = PREV_INSN (line))
1251 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
1252 break;
1253
1254 /* Walk the insns keeping track of the current line-number and inserting
1255 the line-number notes as needed. */
1256 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1257 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1258 line = insn;
1259 /* This used to emit line number notes before every non-deleted note.
1260 However, this confuses a debugger, because line notes not separated
1261 by real instructions all end up at the same address. I can find no
1262 use for line number notes before other notes, so none are emitted. */
1263 else if (GET_CODE (insn) != NOTE
79c2ffde 1264 && INSN_UID (insn) < old_max_uid
8c660648
JL
1265 && (note = LINE_NOTE (insn)) != 0
1266 && note != line
1267 && (line == 0
1268 || NOTE_LINE_NUMBER (note) != NOTE_LINE_NUMBER (line)
1269 || NOTE_SOURCE_FILE (note) != NOTE_SOURCE_FILE (line)))
1270 {
1271 line = note;
1272 prev = PREV_INSN (insn);
1273 if (LINE_NOTE (note))
1274 {
1275 /* Re-use the original line-number note. */
1276 LINE_NOTE (note) = 0;
1277 PREV_INSN (note) = prev;
1278 NEXT_INSN (prev) = note;
1279 PREV_INSN (insn) = note;
1280 NEXT_INSN (note) = insn;
1281 }
1282 else
1283 {
1284 added_notes++;
1285 new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
1286 NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
1287 RTX_INTEGRATED_P (new) = RTX_INTEGRATED_P (note);
1288 }
1289 }
1290 if (sched_verbose && added_notes)
a88f02e7 1291 fprintf (sched_dump, ";; added %d line-number notes\n", added_notes);
8c660648
JL
1292}
1293
1294/* After scheduling the function, delete redundant line notes from the
1295 insns list. */
1296
b4ead7d4 1297void
8c660648
JL
1298rm_redundant_line_notes ()
1299{
1300 rtx line = 0;
1301 rtx insn = get_insns ();
1302 int active_insn = 0;
1303 int notes = 0;
1304
1305 /* Walk the insns deleting redundant line-number notes. Many of these
1306 are already present. The remainder tend to occur at basic
1307 block boundaries. */
1308 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1309 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1310 {
1311 /* If there are no active insns following, INSN is redundant. */
1312 if (active_insn == 0)
1313 {
1314 notes++;
1315 NOTE_SOURCE_FILE (insn) = 0;
1316 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1317 }
1318 /* If the line number is unchanged, LINE is redundant. */
1319 else if (line
1320 && NOTE_LINE_NUMBER (line) == NOTE_LINE_NUMBER (insn)
1321 && NOTE_SOURCE_FILE (line) == NOTE_SOURCE_FILE (insn))
1322 {
1323 notes++;
1324 NOTE_SOURCE_FILE (line) = 0;
1325 NOTE_LINE_NUMBER (line) = NOTE_INSN_DELETED;
1326 line = insn;
1327 }
1328 else
1329 line = insn;
1330 active_insn = 0;
1331 }
1332 else if (!((GET_CODE (insn) == NOTE
1333 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
1334 || (GET_CODE (insn) == INSN
1335 && (GET_CODE (PATTERN (insn)) == USE
1336 || GET_CODE (PATTERN (insn)) == CLOBBER))))
1337 active_insn++;
1338
1339 if (sched_verbose && notes)
a88f02e7 1340 fprintf (sched_dump, ";; deleted %d line-number notes\n", notes);
8c660648
JL
1341}
1342
79c2ffde 1343/* Delete notes between HEAD and TAIL and put them in the chain
8c660648
JL
1344 of notes ended by NOTE_LIST. */
1345
b4ead7d4 1346void
8c660648
JL
1347rm_other_notes (head, tail)
1348 rtx head;
1349 rtx tail;
1350{
1351 rtx next_tail;
1352 rtx insn;
1353
b4ead7d4 1354 note_list = 0;
2c3c49de 1355 if (head == tail && (! INSN_P (head)))
8c660648
JL
1356 return;
1357
1358 next_tail = NEXT_INSN (tail);
1359 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1360 {
1361 rtx prev;
1362
1363 /* Farm out notes, and maybe save them in NOTE_LIST.
1364 This is needed to keep the debugger from
1365 getting completely deranged. */
1366 if (GET_CODE (insn) == NOTE)
1367 {
1368 prev = insn;
1369
1370 insn = unlink_other_notes (insn, next_tail);
1371
1372 if (prev == tail)
1373 abort ();
1374 if (prev == head)
1375 abort ();
1376 if (insn == next_tail)
1377 abort ();
1378 }
1379 }
1380}
1381
63de6c74 1382/* Functions for computation of registers live/usage info. */
8c660648 1383
c46a37c4 1384/* Calculate INSN_REG_WEIGHT for all insns of a block. */
8c660648
JL
1385
1386static void
49c3bb12 1387find_insn_reg_weight (b)
7a403706 1388 int b;
8c660648
JL
1389{
1390 rtx insn, next_tail, head, tail;
8c660648 1391
49c3bb12 1392 get_block_head_tail (b, &head, &tail);
8c660648
JL
1393 next_tail = NEXT_INSN (tail);
1394
1395 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1396 {
8c660648 1397 int reg_weight = 0;
c46a37c4 1398 rtx x;
8c660648
JL
1399
1400 /* Handle register life information. */
2c3c49de 1401 if (! INSN_P (insn))
8c660648
JL
1402 continue;
1403
c46a37c4
RH
1404 /* Increment weight for each register born here. */
1405 x = PATTERN (insn);
1406 if ((GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
1407 && register_operand (SET_DEST (x), VOIDmode))
1408 reg_weight++;
1409 else if (GET_CODE (x) == PARALLEL)
8c660648 1410 {
c46a37c4
RH
1411 int j;
1412 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
1413 {
1414 x = XVECEXP (PATTERN (insn), 0, j);
1415 if ((GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
1416 && register_operand (SET_DEST (x), VOIDmode))
1417 reg_weight++;
1418 }
8c660648
JL
1419 }
1420
c46a37c4
RH
1421 /* Decrement weight for each register that dies here. */
1422 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
8c660648 1423 {
c46a37c4
RH
1424 if (REG_NOTE_KIND (x) == REG_DEAD
1425 || REG_NOTE_KIND (x) == REG_UNUSED)
1426 reg_weight--;
8c660648
JL
1427 }
1428
c46a37c4 1429 INSN_REG_WEIGHT (insn) = reg_weight;
8c660648 1430 }
8c660648
JL
1431}
1432
63de6c74 1433/* Scheduling clock, modified in schedule_block() and queue_to_ready (). */
8c660648
JL
1434static int clock_var;
1435
1436/* Move insns that became ready to fire from queue to ready list. */
1437
176f9a7b
BS
1438static void
1439queue_to_ready (ready)
1440 struct ready_list *ready;
8c660648
JL
1441{
1442 rtx insn;
1443 rtx link;
1444
1445 q_ptr = NEXT_Q (q_ptr);
1446
1447 /* Add all pending insns that can be scheduled without stalls to the
b4ead7d4
BS
1448 ready list. */
1449 for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
1450 {
1451 insn = XEXP (link, 0);
1452 q_size -= 1;
1708fd40 1453
b4ead7d4
BS
1454 if (sched_verbose >= 2)
1455 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1456 (*current_sched_info->print_insn) (insn, 0));
1708fd40 1457
b4ead7d4
BS
1458 ready_add (ready, insn);
1459 if (sched_verbose >= 2)
1460 fprintf (sched_dump, "moving to ready without stalls\n");
1708fd40 1461 }
b4ead7d4
BS
1462 insn_queue[q_ptr] = 0;
1463
1464 /* If there are no ready insns, stall until one is ready and add all
1465 of the pending insns at that point to the ready list. */
1466 if (ready->n_ready == 0)
1708fd40 1467 {
b4ead7d4 1468 register int stalls;
1708fd40 1469
b4ead7d4
BS
1470 for (stalls = 1; stalls < INSN_QUEUE_SIZE; stalls++)
1471 {
1472 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
1473 {
1474 for (; link; link = XEXP (link, 1))
1475 {
1476 insn = XEXP (link, 0);
1477 q_size -= 1;
1708fd40 1478
b4ead7d4
BS
1479 if (sched_verbose >= 2)
1480 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1481 (*current_sched_info->print_insn) (insn, 0));
1708fd40 1482
b4ead7d4
BS
1483 ready_add (ready, insn);
1484 if (sched_verbose >= 2)
1485 fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
1486 }
1487 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = 0;
1708fd40 1488
b4ead7d4
BS
1489 if (ready->n_ready)
1490 break;
1491 }
1492 }
1708fd40 1493
b4ead7d4
BS
1494 if (sched_verbose && stalls)
1495 visualize_stall_cycles (stalls);
1496 q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
1497 clock_var += stalls;
1708fd40 1498 }
1708fd40
BS
1499}
1500
b4ead7d4 1501/* Print the ready list for debugging purposes. Callable from debugger. */
1708fd40 1502
b4ead7d4
BS
1503static void
1504debug_ready_list (ready)
1505 struct ready_list *ready;
1708fd40 1506{
b4ead7d4
BS
1507 rtx *p;
1508 int i;
1708fd40 1509
b4ead7d4
BS
1510 if (ready->n_ready == 0)
1511 return;
1708fd40 1512
b4ead7d4
BS
1513 p = ready_lastpos (ready);
1514 for (i = 0; i < ready->n_ready; i++)
1515 fprintf (sched_dump, " %s", (*current_sched_info->print_insn) (p[i], 0));
1516 fprintf (sched_dump, "\n");
1517}
1708fd40 1518
63de6c74 1519/* move_insn1: Remove INSN from insn chain, and link it after LAST insn. */
8c660648
JL
1520
1521static rtx
1522move_insn1 (insn, last)
1523 rtx insn, last;
1524{
1525 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
1526 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
1527
1528 NEXT_INSN (insn) = NEXT_INSN (last);
1529 PREV_INSN (NEXT_INSN (last)) = insn;
1530
1531 NEXT_INSN (last) = insn;
1532 PREV_INSN (insn) = last;
1533
1534 return insn;
1535}
1536
c46a37c4 1537/* Search INSN for REG_SAVE_NOTE note pairs for NOTE_INSN_SETJMP,
8c660648 1538 NOTE_INSN_{LOOP,EHREGION}_{BEG,END}; and convert them back into
c46a37c4
RH
1539 NOTEs. The REG_SAVE_NOTE note following first one is contains the
1540 saved value for NOTE_BLOCK_NUMBER which is useful for
8c660648
JL
1541 NOTE_INSN_EH_REGION_{BEG,END} NOTEs. LAST is the last instruction
1542 output by the instruction scheduler. Return the new value of LAST. */
1543
1544static rtx
1545reemit_notes (insn, last)
1546 rtx insn;
1547 rtx last;
1548{
1549 rtx note, retval;
1550
1551 retval = last;
1552 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1553 {
c46a37c4 1554 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
8c660648 1555 {
b3b42a4d
RK
1556 enum insn_note note_type = INTVAL (XEXP (note, 0));
1557
6dfdecdb 1558 if (note_type == NOTE_INSN_SETJMP)
8c660648 1559 {
6dfdecdb 1560 retval = emit_note_after (NOTE_INSN_SETJMP, insn);
8c660648 1561 CONST_CALL_P (retval) = CONST_CALL_P (note);
7bd41ea6
MM
1562 remove_note (insn, note);
1563 note = XEXP (note, 1);
8c660648 1564 }
b3b42a4d 1565 else if (note_type == NOTE_INSN_RANGE_BEG
6dfdecdb
RH
1566 || note_type == NOTE_INSN_RANGE_END)
1567 {
1568 last = emit_note_before (note_type, last);
1569 remove_note (insn, note);
1570 note = XEXP (note, 1);
1571 NOTE_RANGE_INFO (last) = XEXP (note, 0);
1572 }
8c660648
JL
1573 else
1574 {
19699da4 1575 last = emit_note_before (note_type, last);
7bd41ea6
MM
1576 remove_note (insn, note);
1577 note = XEXP (note, 1);
1a4450c7
MM
1578 if (note_type == NOTE_INSN_EH_REGION_BEG
1579 || note_type == NOTE_INSN_EH_REGION_END)
7bd41ea6 1580 NOTE_EH_HANDLER (last) = INTVAL (XEXP (note, 0));
8c660648
JL
1581 }
1582 remove_note (insn, note);
1583 }
1584 }
1585 return retval;
1586}
1587
1588/* Move INSN, and all insns which should be issued before it,
c9e03727
JL
1589 due to SCHED_GROUP_P flag. Reemit notes if needed.
1590
1591 Return the last insn emitted by the scheduler, which is the
1592 return value from the first call to reemit_notes. */
8c660648
JL
1593
1594static rtx
1595move_insn (insn, last)
1596 rtx insn, last;
1597{
c9e03727 1598 rtx retval = NULL;
8c660648 1599
c9e03727
JL
1600 /* If INSN has SCHED_GROUP_P set, then issue it and any other
1601 insns with SCHED_GROUP_P set first. */
8c660648
JL
1602 while (SCHED_GROUP_P (insn))
1603 {
1604 rtx prev = PREV_INSN (insn);
c9e03727
JL
1605
1606 /* Move a SCHED_GROUP_P insn. */
8c660648 1607 move_insn1 (insn, last);
c9e03727
JL
1608 /* If this is the first call to reemit_notes, then record
1609 its return value. */
1610 if (retval == NULL_RTX)
1611 retval = reemit_notes (insn, insn);
1612 else
1613 reemit_notes (insn, insn);
8c660648
JL
1614 insn = prev;
1615 }
1616
c9e03727 1617 /* Now move the first non SCHED_GROUP_P insn. */
8c660648 1618 move_insn1 (insn, last);
c9e03727
JL
1619
1620 /* If this is the first call to reemit_notes, then record
1621 its return value. */
1622 if (retval == NULL_RTX)
1623 retval = reemit_notes (insn, insn);
1624 else
1625 reemit_notes (insn, insn);
1626
1627 return retval;
8c660648
JL
1628}
1629
b4ead7d4 1630/* Use forward list scheduling to rearrange insns of block B in region RGN,
1708fd40 1631 possibly bringing insns from subsequent blocks in the same region. */
8c660648 1632
b4ead7d4
BS
1633void
1634schedule_block (b, rgn_n_insns)
1635 int b;
8c660648
JL
1636 int rgn_n_insns;
1637{
1708fd40 1638 rtx last;
176f9a7b 1639 struct ready_list ready;
8c660648
JL
1640 int can_issue_more;
1641
63de6c74 1642 /* Head/tail info for this block. */
1708fd40
BS
1643 rtx prev_head = current_sched_info->prev_head;
1644 rtx next_tail = current_sched_info->next_tail;
1645 rtx head = NEXT_INSN (prev_head);
1646 rtx tail = PREV_INSN (next_tail);
8c660648 1647
484df988
JL
1648 /* We used to have code to avoid getting parameters moved from hard
1649 argument registers into pseudos.
8c660648 1650
484df988
JL
1651 However, it was removed when it proved to be of marginal benefit
1652 and caused problems because schedule_block and compute_forward_dependences
1653 had different notions of what the "head" insn was. */
8c660648 1654
2c3c49de 1655 if (head == tail && (! INSN_P (head)))
1708fd40 1656 abort ();
8c660648 1657
63de6c74 1658 /* Debug info. */
8c660648
JL
1659 if (sched_verbose)
1660 {
a88f02e7
BS
1661 fprintf (sched_dump, ";; ======================================================\n");
1662 fprintf (sched_dump,
8c660648 1663 ";; -- basic block %d from %d to %d -- %s reload\n",
79c2ffde 1664 b, INSN_UID (head), INSN_UID (tail),
8c660648 1665 (reload_completed ? "after" : "before"));
a88f02e7
BS
1666 fprintf (sched_dump, ";; ======================================================\n");
1667 fprintf (sched_dump, "\n");
8c660648 1668
c62c2659 1669 visualize_alloc ();
8c660648
JL
1670 init_block_visualization ();
1671 }
1672
8c660648
JL
1673 clear_units ();
1674
63de6c74 1675 /* Allocate the ready list. */
176f9a7b
BS
1676 ready.veclen = rgn_n_insns + 1 + ISSUE_RATE;
1677 ready.first = ready.veclen - 1;
1678 ready.vec = (rtx *) xmalloc (ready.veclen * sizeof (rtx));
1679 ready.n_ready = 0;
8c660648 1680
1708fd40 1681 (*current_sched_info->init_ready_list) (&ready);
8c660648 1682
e4da5f6d 1683#ifdef MD_SCHED_INIT
79c2ffde 1684 MD_SCHED_INIT (sched_dump, sched_verbose, ready.veclen);
e4da5f6d
MM
1685#endif
1686
63de6c74 1687 /* No insns scheduled in this block yet. */
8c660648
JL
1688 last_scheduled_insn = 0;
1689
1708fd40
BS
1690 /* Initialize INSN_QUEUE. Q_SIZE is the total number of insns in the
1691 queue. */
8c660648
JL
1692 q_ptr = 0;
1693 q_size = 0;
4bdc8810 1694 last_clock_var = 0;
961192e1 1695 memset ((char *) insn_queue, 0, sizeof (insn_queue));
8c660648 1696
197043f5
RH
1697 /* Start just before the beginning of time. */
1698 clock_var = -1;
1699
8c660648
JL
1700 /* We start inserting insns after PREV_HEAD. */
1701 last = prev_head;
1702
63de6c74 1703 /* Loop until all the insns in BB are scheduled. */
1708fd40 1704 while ((*current_sched_info->schedule_more_p) ())
8c660648 1705 {
8c660648
JL
1706 clock_var++;
1707
1708 /* Add to the ready list all pending insns that can be issued now.
1709 If there are no ready insns, increment clock until one
1710 is ready and add all pending insns at that point to the ready
1711 list. */
176f9a7b 1712 queue_to_ready (&ready);
8c660648 1713
79c2ffde
BS
1714#ifdef HAVE_cycle_display
1715 if (HAVE_cycle_display)
1716 last = emit_insn_after (gen_cycle_display (GEN_INT (clock_var)), last);
1717#endif
1718
176f9a7b 1719 if (ready.n_ready == 0)
8c660648
JL
1720 abort ();
1721
1722 if (sched_verbose >= 2)
1723 {
a88f02e7 1724 fprintf (sched_dump, ";;\t\tReady list after queue_to_ready: ");
176f9a7b 1725 debug_ready_list (&ready);
8c660648
JL
1726 }
1727
197043f5 1728 /* Sort the ready list based on priority. */
176f9a7b 1729 ready_sort (&ready);
197043f5 1730
b4ead7d4
BS
1731 /* Allow the target to reorder the list, typically for
1732 better instruction bundling. */
1733#ifdef MD_SCHED_REORDER
1734 MD_SCHED_REORDER (sched_dump, sched_verbose, ready_lastpos (&ready),
1735 ready.n_ready, clock_var, can_issue_more);
1736#else
1737 can_issue_more = issue_rate;
1738#endif
e1306f49 1739
b4ead7d4 1740 if (sched_verbose)
e1306f49 1741 {
b4ead7d4
BS
1742 fprintf (sched_dump, "\n;;\tReady list (t =%3d): ", clock_var);
1743 debug_ready_list (&ready);
e1306f49
BS
1744 }
1745
b4ead7d4 1746 /* Issue insns from ready list. */
79c2ffde
BS
1747 while (ready.n_ready != 0
1748 && can_issue_more
1749 && (*current_sched_info->schedule_more_p) ())
b4ead7d4
BS
1750 {
1751 /* Select and remove the insn from the ready list. */
1752 rtx insn = ready_remove_first (&ready);
1753 int cost = actual_hazard (insn_unit (insn), insn, clock_var, 0);
e1306f49 1754
b4ead7d4
BS
1755 if (cost >= 1)
1756 {
1757 queue_insn (insn, cost);
1758 continue;
1759 }
e1306f49 1760
b4ead7d4
BS
1761 if (! (*current_sched_info->can_schedule_ready_p) (insn))
1762 goto next;
8c660648 1763
b4ead7d4
BS
1764 last_scheduled_insn = insn;
1765 last = move_insn (insn, last);
8c660648 1766
b4ead7d4
BS
1767#ifdef MD_SCHED_VARIABLE_ISSUE
1768 MD_SCHED_VARIABLE_ISSUE (sched_dump, sched_verbose, insn,
1769 can_issue_more);
1770#else
1771 can_issue_more--;
1772#endif
8c660648 1773
b4ead7d4 1774 schedule_insn (insn, &ready, clock_var);
8c660648 1775
b4ead7d4 1776 next:
79c2ffde
BS
1777#ifdef MD_SCHED_REORDER2
1778 /* Sort the ready list based on priority. */
1779 if (ready.n_ready > 0)
1780 ready_sort (&ready);
1781 MD_SCHED_REORDER2 (sched_dump, sched_verbose,
1782 ready.n_ready ? ready_lastpos (&ready) : NULL,
1783 ready.n_ready, clock_var, can_issue_more);
1784#endif
b4ead7d4 1785 }
8c660648 1786
b4ead7d4
BS
1787 /* Debug info. */
1788 if (sched_verbose)
1789 visualize_scheduled_insns (clock_var);
1790 }
8c660648 1791
79c2ffde
BS
1792#ifdef MD_SCHED_FINISH
1793 MD_SCHED_FINISH (sched_dump, sched_verbose);
1794#endif
1795
b4ead7d4
BS
1796 /* Debug info. */
1797 if (sched_verbose)
1798 {
1799 fprintf (sched_dump, ";;\tReady list (final): ");
1800 debug_ready_list (&ready);
1801 print_block_visualization ("");
1802 }
8c660648 1803
b4ead7d4
BS
1804 /* Sanity check -- queue must be empty now. Meaningless if region has
1805 multiple bbs. */
1806 if (current_sched_info->queue_must_finish_empty && q_size != 0)
1807 abort ();
ebb7b10b 1808
b4ead7d4
BS
1809 /* Update head/tail boundaries. */
1810 head = NEXT_INSN (prev_head);
1811 tail = last;
ebb7b10b 1812
b4ead7d4
BS
1813 /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
1814 previously found among the insns. Insert them at the beginning
1815 of the insns. */
1816 if (note_list != 0)
1817 {
1818 rtx note_head = note_list;
8c660648 1819
b4ead7d4
BS
1820 while (PREV_INSN (note_head))
1821 {
1822 note_head = PREV_INSN (note_head);
1823 }
8c660648 1824
b4ead7d4
BS
1825 PREV_INSN (note_head) = PREV_INSN (head);
1826 NEXT_INSN (PREV_INSN (head)) = note_head;
1827 PREV_INSN (head) = note_list;
1828 NEXT_INSN (note_list) = head;
1829 head = note_head;
1830 }
8c660648 1831
b4ead7d4
BS
1832 /* Debugging. */
1833 if (sched_verbose)
8c660648 1834 {
b4ead7d4
BS
1835 fprintf (sched_dump, ";; total time = %d\n;; new head = %d\n",
1836 clock_var, INSN_UID (head));
1837 fprintf (sched_dump, ";; new tail = %d\n\n",
1838 INSN_UID (tail));
1839 visualize_free ();
1840 }
8c660648 1841
b4ead7d4
BS
1842 current_sched_info->head = head;
1843 current_sched_info->tail = tail;
8c660648 1844
b4ead7d4 1845 free (ready.vec);
8c660648 1846}
b4ead7d4 1847\f
63de6c74 1848/* Set_priorities: compute priority of each insn in the block. */
8c660648 1849
b4ead7d4 1850int
79c2ffde
BS
1851set_priorities (head, tail)
1852 rtx head, tail;
8c660648
JL
1853{
1854 rtx insn;
1855 int n_insn;
1856
8c660648 1857 rtx prev_head;
8c660648 1858
8c660648
JL
1859 prev_head = PREV_INSN (head);
1860
2c3c49de 1861 if (head == tail && (! INSN_P (head)))
8c660648
JL
1862 return 0;
1863
1864 n_insn = 0;
1865 for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
1866 {
8c660648
JL
1867 if (GET_CODE (insn) == NOTE)
1868 continue;
1869
1870 if (!(SCHED_GROUP_P (insn)))
1871 n_insn++;
1872 (void) priority (insn);
1873 }
1874
1875 return n_insn;
1876}
1877
a88f02e7
BS
1878/* Initialize some global state for the scheduler. DUMP_FILE is to be used
1879 for debugging output. */
8c660648 1880
b4ead7d4 1881void
a88f02e7 1882sched_init (dump_file)
8c660648
JL
1883 FILE *dump_file;
1884{
a88f02e7 1885 int luid, b;
8c660648 1886 rtx insn;
8c660648 1887
63de6c74 1888 /* Disable speculative loads in their presence if cc0 defined. */
8c660648
JL
1889#ifdef HAVE_cc0
1890 flag_schedule_speculative_load = 0;
1891#endif
1892
63de6c74 1893 /* Set dump and sched_verbose for the desired debugging output. If no
409f8483
DE
1894 dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
1895 For -fsched-verbose=N, N>=10, print everything to stderr. */
8c660648
JL
1896 sched_verbose = sched_verbose_param;
1897 if (sched_verbose_param == 0 && dump_file)
1898 sched_verbose = 1;
a88f02e7
BS
1899 sched_dump = ((sched_verbose_param >= 10 || !dump_file)
1900 ? stderr : dump_file);
8c660648 1901
63de6c74 1902 /* Initialize issue_rate. */
62d65906 1903 issue_rate = ISSUE_RATE;
8c660648 1904
d3a923ee 1905 split_all_insns (1);
8c660648 1906
c88e8206
RH
1907 /* We use LUID 0 for the fake insn (UID 0) which holds dependencies for
1908 pseudos which do not cross calls. */
a88f02e7 1909 old_max_uid = get_max_uid () + 1;
8c660648 1910
a88f02e7 1911 h_i_d = (struct haifa_insn_data *) xcalloc (old_max_uid, sizeof (*h_i_d));
8c660648 1912
f66d83e1 1913 h_i_d[0].luid = 0;
356edbd7 1914 luid = 1;
8c660648 1915 for (b = 0; b < n_basic_blocks; b++)
3b413743 1916 for (insn = BLOCK_HEAD (b);; insn = NEXT_INSN (insn))
8c660648 1917 {
f77e39fc
MM
1918 INSN_LUID (insn) = luid;
1919
1920 /* Increment the next luid, unless this is a note. We don't
1921 really need separate IDs for notes and we don't want to
1922 schedule differently depending on whether or not there are
1923 line-number notes, i.e., depending on whether or not we're
1924 generating debugging information. */
1925 if (GET_CODE (insn) != NOTE)
1926 ++luid;
1927
3b413743 1928 if (insn == BLOCK_END (b))
8c660648
JL
1929 break;
1930 }
7a403706 1931
a88f02e7
BS
1932 init_dependency_caches (luid);
1933
1934 compute_bb_for_insn (old_max_uid);
1935
1936 init_alias_analysis ();
1937
1938 if (write_symbols != NO_DEBUG)
aae0390e 1939 {
a88f02e7
BS
1940 rtx line;
1941
1942 line_note_head = (rtx *) xcalloc (n_basic_blocks, sizeof (rtx));
1943
1944 /* Save-line-note-head:
1945 Determine the line-number at the start of each basic block.
1946 This must be computed and saved now, because after a basic block's
1947 predecessor has been scheduled, it is impossible to accurately
1948 determine the correct line number for the first insn of the block. */
1949
1950 for (b = 0; b < n_basic_blocks; b++)
79c2ffde
BS
1951 {
1952 for (line = BLOCK_HEAD (b); line; line = PREV_INSN (line))
1953 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
1954 {
1955 line_note_head[b] = line;
1956 break;
1957 }
1958 /* Do a forward search as well, since we won't get to see the first
1959 notes in a basic block. */
1960 for (line = BLOCK_HEAD (b); line; line = NEXT_INSN (line))
a88f02e7 1961 {
79c2ffde
BS
1962 if (INSN_P (line))
1963 break;
1964 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
1965 line_note_head[b] = line;
a88f02e7 1966 }
79c2ffde 1967 }
aae0390e 1968 }
8c660648 1969
a88f02e7
BS
1970 /* Find units used in this fuction, for visualization. */
1971 if (sched_verbose)
1972 init_target_units ();
1973
1974 /* ??? Add a NOTE after the last insn of the last basic block. It is not
1975 known why this is done. */
1976
1977 insn = BLOCK_END (n_basic_blocks - 1);
1978 if (NEXT_INSN (insn) == 0
1979 || (GET_CODE (insn) != NOTE
1980 && GET_CODE (insn) != CODE_LABEL
4cf37b4a
R
1981 /* Don't emit a NOTE if it would end up before a BARRIER. */
1982 && GET_CODE (NEXT_INSN (insn)) != BARRIER))
a88f02e7
BS
1983 emit_note_after (NOTE_INSN_DELETED, BLOCK_END (n_basic_blocks - 1));
1984
1985 /* Compute INSN_REG_WEIGHT for all blocks. We must do this before
1986 removing death notes. */
1987 for (b = n_basic_blocks - 1; b >= 0; b--)
1988 find_insn_reg_weight (b);
1989}
1990
b4ead7d4 1991/* Free global data used during insn scheduling. */
8c660648 1992
a88f02e7 1993void
b4ead7d4 1994sched_finish ()
a88f02e7 1995{
f66d83e1 1996 free (h_i_d);
b4ead7d4
BS
1997 free_dependency_caches ();
1998 end_alias_analysis ();
7c74b010 1999 if (write_symbols != NO_DEBUG)
f66d83e1 2000 free (line_note_head);
8c660648
JL
2001}
2002#endif /* INSN_SCHEDULING */
This page took 1.051828 seconds and 5 git commands to generate.