* config/ia64/fde-glibc.c (_GNU_SOURCE): Define to 1 instead of
[official-gcc.git] / gcc / haifa-sched.c
blobf75894ac3be385fcd897e6b4fb6090fd3e710ed9
1 /* Instruction scheduling pass.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
5 and currently maintained by, Jim Wilson (wilson@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
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.
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.
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.
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.
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.
64 The following list shows the order in which we want to break ties
65 among insns in the ready list:
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
77 7 choose the insn which has the most insns dependent on it.
78 8. choose insn with lowest UID.
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.
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 ().
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.
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.
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.
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.
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.
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).
124 This pass must update information that subsequent passes expect to
125 be correct. Namely: reg_n_refs, reg_n_sets, reg_n_deaths,
126 reg_n_calls_crossed, and reg_live_length. Also, BLOCK_HEAD,
127 BLOCK_END.
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
133 beginning of basic blocks and regions that have been scheduled. */
135 #include "config.h"
136 #include "system.h"
137 #include "coretypes.h"
138 #include "tm.h"
139 #include "toplev.h"
140 #include "rtl.h"
141 #include "tm_p.h"
142 #include "hard-reg-set.h"
143 #include "basic-block.h"
144 #include "regs.h"
145 #include "function.h"
146 #include "flags.h"
147 #include "insn-config.h"
148 #include "insn-attr.h"
149 #include "except.h"
150 #include "toplev.h"
151 #include "recog.h"
152 #include "sched-int.h"
153 #include "target.h"
155 #ifdef INSN_SCHEDULING
157 /* issue_rate is the number of insns that can be scheduled in the same
158 machine cycle. It can be defined in the config/mach/mach.h file,
159 otherwise we set it to 1. */
161 static int issue_rate;
163 /* If the following variable value is nonzero, the scheduler inserts
164 bubbles (nop insns). The value of variable affects on scheduler
165 behavior only if automaton pipeline interface with multipass
166 scheduling is used and hook dfa_bubble is defined. */
167 int insert_schedule_bubbles_p = 0;
169 /* sched-verbose controls the amount of debugging output the
170 scheduler prints. It is controlled by -fsched-verbose=N:
171 N>0 and no -DSR : the output is directed to stderr.
172 N>=10 will direct the printouts to stderr (regardless of -dSR).
173 N=1: same as -dSR.
174 N=2: bb's probabilities, detailed ready list info, unit/insn info.
175 N=3: rtl at abort point, control-flow, regions info.
176 N=5: dependences info. */
178 static int sched_verbose_param = 0;
179 int sched_verbose = 0;
181 /* Debugging file. All printouts are sent to dump, which is always set,
182 either to stderr, or to the dump listing file (-dRS). */
183 FILE *sched_dump = 0;
185 /* Highest uid before scheduling. */
186 static int old_max_uid;
188 /* fix_sched_param() is called from toplev.c upon detection
189 of the -fsched-verbose=N option. */
191 void
192 fix_sched_param (const char *param, const char *val)
194 if (!strcmp (param, "verbose"))
195 sched_verbose_param = atoi (val);
196 else
197 warning ("fix_sched_param: unknown param: %s", param);
200 struct haifa_insn_data *h_i_d;
202 #define LINE_NOTE(INSN) (h_i_d[INSN_UID (INSN)].line_note)
203 #define INSN_TICK(INSN) (h_i_d[INSN_UID (INSN)].tick)
205 /* Vector indexed by basic block number giving the starting line-number
206 for each basic block. */
207 static rtx *line_note_head;
209 /* List of important notes we must keep around. This is a pointer to the
210 last element in the list. */
211 static rtx note_list;
213 /* Queues, etc. */
215 /* An instruction is ready to be scheduled when all insns preceding it
216 have already been scheduled. It is important to ensure that all
217 insns which use its result will not be executed until its result
218 has been computed. An insn is maintained in one of four structures:
220 (P) the "Pending" set of insns which cannot be scheduled until
221 their dependencies have been satisfied.
222 (Q) the "Queued" set of insns that can be scheduled when sufficient
223 time has passed.
224 (R) the "Ready" list of unscheduled, uncommitted insns.
225 (S) the "Scheduled" list of insns.
227 Initially, all insns are either "Pending" or "Ready" depending on
228 whether their dependencies are satisfied.
230 Insns move from the "Ready" list to the "Scheduled" list as they
231 are committed to the schedule. As this occurs, the insns in the
232 "Pending" list have their dependencies satisfied and move to either
233 the "Ready" list or the "Queued" set depending on whether
234 sufficient time has passed to make them ready. As time passes,
235 insns move from the "Queued" set to the "Ready" list. Insns may
236 move from the "Ready" list to the "Queued" set if they are blocked
237 due to a function unit conflict.
239 The "Pending" list (P) are the insns in the INSN_DEPEND of the unscheduled
240 insns, i.e., those that are ready, queued, and pending.
241 The "Queued" set (Q) is implemented by the variable `insn_queue'.
242 The "Ready" list (R) is implemented by the variables `ready' and
243 `n_ready'.
244 The "Scheduled" list (S) is the new insn chain built by this pass.
246 The transition (R->S) is implemented in the scheduling loop in
247 `schedule_block' when the best insn to schedule is chosen.
248 The transition (R->Q) is implemented in `queue_insn' when an
249 insn is found to have a function unit conflict with the already
250 committed insns.
251 The transitions (P->R and P->Q) are implemented in `schedule_insn' as
252 insns move from the ready list to the scheduled list.
253 The transition (Q->R) is implemented in 'queue_to_insn' as time
254 passes or stalls are introduced. */
256 /* Implement a circular buffer to delay instructions until sufficient
257 time has passed. For the old pipeline description interface,
258 INSN_QUEUE_SIZE is a power of two larger than MAX_BLOCKAGE and
259 MAX_READY_COST computed by genattr.c. For the new pipeline
260 description interface, MAX_INSN_QUEUE_INDEX is a power of two minus
261 one which is larger than maximal time of instruction execution
262 computed by genattr.c on the base maximal time of functional unit
263 reservations and geting a result. This is the longest time an
264 insn may be queued. */
266 #define MAX_INSN_QUEUE_INDEX max_insn_queue_index_macro_value
268 static rtx *insn_queue;
269 static int q_ptr = 0;
270 static int q_size = 0;
271 #define NEXT_Q(X) (((X)+1) & MAX_INSN_QUEUE_INDEX)
272 #define NEXT_Q_AFTER(X, C) (((X)+C) & MAX_INSN_QUEUE_INDEX)
274 /* The following variable defines value for macro
275 MAX_INSN_QUEUE_INDEX. */
276 static int max_insn_queue_index_macro_value;
278 /* The following variable value refers for all current and future
279 reservations of the processor units. */
280 state_t curr_state;
282 /* The following variable value is size of memory representing all
283 current and future reservations of the processor units. It is used
284 only by DFA based scheduler. */
285 static size_t dfa_state_size;
287 /* The following array is used to find the best insn from ready when
288 the automaton pipeline interface is used. */
289 static char *ready_try;
291 /* Describe the ready list of the scheduler.
292 VEC holds space enough for all insns in the current region. VECLEN
293 says how many exactly.
294 FIRST is the index of the element with the highest priority; i.e. the
295 last one in the ready list, since elements are ordered by ascending
296 priority.
297 N_READY determines how many insns are on the ready list. */
299 struct ready_list
301 rtx *vec;
302 int veclen;
303 int first;
304 int n_ready;
307 static int may_trap_exp (rtx, int);
309 /* Nonzero iff the address is comprised from at most 1 register. */
310 #define CONST_BASED_ADDRESS_P(x) \
311 (GET_CODE (x) == REG \
312 || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS \
313 || (GET_CODE (x) == LO_SUM)) \
314 && (CONSTANT_P (XEXP (x, 0)) \
315 || CONSTANT_P (XEXP (x, 1)))))
317 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
318 as found by analyzing insn's expression. */
320 static int
321 may_trap_exp (rtx x, int is_store)
323 enum rtx_code code;
325 if (x == 0)
326 return TRAP_FREE;
327 code = GET_CODE (x);
328 if (is_store)
330 if (code == MEM && may_trap_p (x))
331 return TRAP_RISKY;
332 else
333 return TRAP_FREE;
335 if (code == MEM)
337 /* The insn uses memory: a volatile load. */
338 if (MEM_VOLATILE_P (x))
339 return IRISKY;
340 /* An exception-free load. */
341 if (!may_trap_p (x))
342 return IFREE;
343 /* A load with 1 base register, to be further checked. */
344 if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
345 return PFREE_CANDIDATE;
346 /* No info on the load, to be further checked. */
347 return PRISKY_CANDIDATE;
349 else
351 const char *fmt;
352 int i, insn_class = TRAP_FREE;
354 /* Neither store nor load, check if it may cause a trap. */
355 if (may_trap_p (x))
356 return TRAP_RISKY;
357 /* Recursive step: walk the insn... */
358 fmt = GET_RTX_FORMAT (code);
359 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
361 if (fmt[i] == 'e')
363 int tmp_class = may_trap_exp (XEXP (x, i), is_store);
364 insn_class = WORST_CLASS (insn_class, tmp_class);
366 else if (fmt[i] == 'E')
368 int j;
369 for (j = 0; j < XVECLEN (x, i); j++)
371 int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
372 insn_class = WORST_CLASS (insn_class, tmp_class);
373 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
374 break;
377 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
378 break;
380 return insn_class;
384 /* Classifies insn for the purpose of verifying that it can be
385 moved speculatively, by examining it's patterns, returning:
386 TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
387 TRAP_FREE: non-load insn.
388 IFREE: load from a globally safe location.
389 IRISKY: volatile load.
390 PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
391 being either PFREE or PRISKY. */
394 haifa_classify_insn (rtx insn)
396 rtx pat = PATTERN (insn);
397 int tmp_class = TRAP_FREE;
398 int insn_class = TRAP_FREE;
399 enum rtx_code code;
401 if (GET_CODE (pat) == PARALLEL)
403 int i, len = XVECLEN (pat, 0);
405 for (i = len - 1; i >= 0; i--)
407 code = GET_CODE (XVECEXP (pat, 0, i));
408 switch (code)
410 case CLOBBER:
411 /* Test if it is a 'store'. */
412 tmp_class = may_trap_exp (XEXP (XVECEXP (pat, 0, i), 0), 1);
413 break;
414 case SET:
415 /* Test if it is a store. */
416 tmp_class = may_trap_exp (SET_DEST (XVECEXP (pat, 0, i)), 1);
417 if (tmp_class == TRAP_RISKY)
418 break;
419 /* Test if it is a load. */
420 tmp_class
421 = WORST_CLASS (tmp_class,
422 may_trap_exp (SET_SRC (XVECEXP (pat, 0, i)),
423 0));
424 break;
425 case COND_EXEC:
426 case TRAP_IF:
427 tmp_class = TRAP_RISKY;
428 break;
429 default:
432 insn_class = WORST_CLASS (insn_class, tmp_class);
433 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
434 break;
437 else
439 code = GET_CODE (pat);
440 switch (code)
442 case CLOBBER:
443 /* Test if it is a 'store'. */
444 tmp_class = may_trap_exp (XEXP (pat, 0), 1);
445 break;
446 case SET:
447 /* Test if it is a store. */
448 tmp_class = may_trap_exp (SET_DEST (pat), 1);
449 if (tmp_class == TRAP_RISKY)
450 break;
451 /* Test if it is a load. */
452 tmp_class =
453 WORST_CLASS (tmp_class,
454 may_trap_exp (SET_SRC (pat), 0));
455 break;
456 case COND_EXEC:
457 case TRAP_IF:
458 tmp_class = TRAP_RISKY;
459 break;
460 default:;
462 insn_class = tmp_class;
465 return insn_class;
468 /* Forward declarations. */
470 /* The scheduler using only DFA description should never use the
471 following five functions: */
472 static unsigned int blockage_range (int, rtx);
473 static void clear_units (void);
474 static void schedule_unit (int, rtx, int);
475 static int actual_hazard (int, rtx, int, int);
476 static int potential_hazard (int, rtx, int);
478 static int priority (rtx);
479 static int rank_for_schedule (const void *, const void *);
480 static void swap_sort (rtx *, int);
481 static void queue_insn (rtx, int);
482 static int schedule_insn (rtx, struct ready_list *, int);
483 static int find_set_reg_weight (rtx);
484 static void find_insn_reg_weight (int);
485 static void adjust_priority (rtx);
486 static void advance_one_cycle (void);
488 /* Notes handling mechanism:
489 =========================
490 Generally, NOTES are saved before scheduling and restored after scheduling.
491 The scheduler distinguishes between three types of notes:
493 (1) LINE_NUMBER notes, generated and used for debugging. Here,
494 before scheduling a region, a pointer to the LINE_NUMBER note is
495 added to the insn following it (in save_line_notes()), and the note
496 is removed (in rm_line_notes() and unlink_line_notes()). After
497 scheduling the region, this pointer is used for regeneration of
498 the LINE_NUMBER note (in restore_line_notes()).
500 (2) LOOP_BEGIN, LOOP_END, SETJMP, EHREGION_BEG, EHREGION_END notes:
501 Before scheduling a region, a pointer to the note is added to the insn
502 that follows or precedes it. (This happens as part of the data dependence
503 computation). After scheduling an insn, the pointer contained in it is
504 used for regenerating the corresponding note (in reemit_notes).
506 (3) All other notes (e.g. INSN_DELETED): Before scheduling a block,
507 these notes are put in a list (in rm_other_notes() and
508 unlink_other_notes ()). After scheduling the block, these notes are
509 inserted at the beginning of the block (in schedule_block()). */
511 static rtx unlink_other_notes (rtx, rtx);
512 static rtx unlink_line_notes (rtx, rtx);
513 static rtx reemit_notes (rtx, rtx);
515 static rtx *ready_lastpos (struct ready_list *);
516 static void ready_sort (struct ready_list *);
517 static rtx ready_remove_first (struct ready_list *);
519 static void queue_to_ready (struct ready_list *);
520 static int early_queue_to_ready (state_t, struct ready_list *);
522 static void debug_ready_list (struct ready_list *);
524 static rtx move_insn1 (rtx, rtx);
525 static rtx move_insn (rtx, rtx);
527 /* The following functions are used to implement multi-pass scheduling
528 on the first cycle. It is used only for DFA based scheduler. */
529 static rtx ready_element (struct ready_list *, int);
530 static rtx ready_remove (struct ready_list *, int);
531 static int max_issue (struct ready_list *, int *);
533 static rtx choose_ready (struct ready_list *);
535 #endif /* INSN_SCHEDULING */
537 /* Point to state used for the current scheduling pass. */
538 struct sched_info *current_sched_info;
540 #ifndef INSN_SCHEDULING
541 void
542 schedule_insns (FILE *dump_file ATTRIBUTE_UNUSED)
545 #else
547 /* Pointer to the last instruction scheduled. Used by rank_for_schedule,
548 so that insns independent of the last scheduled insn will be preferred
549 over dependent instructions. */
551 static rtx last_scheduled_insn;
553 /* Compute the function units used by INSN. This caches the value
554 returned by function_units_used. A function unit is encoded as the
555 unit number if the value is non-negative and the complement of a
556 mask if the value is negative. A function unit index is the
557 non-negative encoding. The scheduler using only DFA description
558 should never use the following function. */
560 HAIFA_INLINE int
561 insn_unit (rtx insn)
563 int unit = INSN_UNIT (insn);
565 if (unit == 0)
567 recog_memoized (insn);
569 /* A USE insn, or something else we don't need to understand.
570 We can't pass these directly to function_units_used because it will
571 trigger a fatal error for unrecognizable insns. */
572 if (INSN_CODE (insn) < 0)
573 unit = -1;
574 else
576 unit = function_units_used (insn);
577 /* Increment non-negative values so we can cache zero. */
578 if (unit >= 0)
579 unit++;
581 /* We only cache 16 bits of the result, so if the value is out of
582 range, don't cache it. */
583 if (FUNCTION_UNITS_SIZE < HOST_BITS_PER_SHORT
584 || unit >= 0
585 || (unit & ~((1 << (HOST_BITS_PER_SHORT - 1)) - 1)) == 0)
586 INSN_UNIT (insn) = unit;
588 return (unit > 0 ? unit - 1 : unit);
591 /* Compute the blockage range for executing INSN on UNIT. This caches
592 the value returned by the blockage_range_function for the unit.
593 These values are encoded in an int where the upper half gives the
594 minimum value and the lower half gives the maximum value. The
595 scheduler using only DFA description should never use the following
596 function. */
598 HAIFA_INLINE static unsigned int
599 blockage_range (int unit, rtx insn)
601 unsigned int blockage = INSN_BLOCKAGE (insn);
602 unsigned int range;
604 if ((int) UNIT_BLOCKED (blockage) != unit + 1)
606 range = function_units[unit].blockage_range_function (insn);
607 /* We only cache the blockage range for one unit and then only if
608 the values fit. */
609 if (HOST_BITS_PER_INT >= UNIT_BITS + 2 * BLOCKAGE_BITS)
610 INSN_BLOCKAGE (insn) = ENCODE_BLOCKAGE (unit + 1, range);
612 else
613 range = BLOCKAGE_RANGE (blockage);
615 return range;
618 /* A vector indexed by function unit instance giving the last insn to
619 use the unit. The value of the function unit instance index for
620 unit U instance I is (U + I * FUNCTION_UNITS_SIZE). The scheduler
621 using only DFA description should never use the following variable. */
622 #if FUNCTION_UNITS_SIZE
623 static rtx unit_last_insn[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
624 #else
625 static rtx unit_last_insn[1];
626 #endif
628 /* A vector indexed by function unit instance giving the minimum time
629 when the unit will unblock based on the maximum blockage cost. The
630 scheduler using only DFA description should never use the following
631 variable. */
632 #if FUNCTION_UNITS_SIZE
633 static int unit_tick[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
634 #else
635 static int unit_tick[1];
636 #endif
638 /* A vector indexed by function unit number giving the number of insns
639 that remain to use the unit. The scheduler using only DFA
640 description should never use the following variable. */
641 #if FUNCTION_UNITS_SIZE
642 static int unit_n_insns[FUNCTION_UNITS_SIZE];
643 #else
644 static int unit_n_insns[1];
645 #endif
647 /* Access the unit_last_insn array. Used by the visualization code.
648 The scheduler using only DFA description should never use the
649 following function. */
652 get_unit_last_insn (int instance)
654 return unit_last_insn[instance];
657 /* Reset the function unit state to the null state. */
659 static void
660 clear_units (void)
662 memset (unit_last_insn, 0, sizeof (unit_last_insn));
663 memset (unit_tick, 0, sizeof (unit_tick));
664 memset (unit_n_insns, 0, sizeof (unit_n_insns));
667 /* Return the issue-delay of an insn. The scheduler using only DFA
668 description should never use the following function. */
670 HAIFA_INLINE int
671 insn_issue_delay (rtx insn)
673 int i, delay = 0;
674 int unit = insn_unit (insn);
676 /* Efficiency note: in fact, we are working 'hard' to compute a
677 value that was available in md file, and is not available in
678 function_units[] structure. It would be nice to have this
679 value there, too. */
680 if (unit >= 0)
682 if (function_units[unit].blockage_range_function &&
683 function_units[unit].blockage_function)
684 delay = function_units[unit].blockage_function (insn, insn);
686 else
687 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
688 if ((unit & 1) != 0 && function_units[i].blockage_range_function
689 && function_units[i].blockage_function)
690 delay = MAX (delay, function_units[i].blockage_function (insn, insn));
692 return delay;
695 /* Return the actual hazard cost of executing INSN on the unit UNIT,
696 instance INSTANCE at time CLOCK if the previous actual hazard cost
697 was COST. The scheduler using only DFA description should never
698 use the following function. */
700 HAIFA_INLINE int
701 actual_hazard_this_instance (int unit, int instance, rtx insn, int clock, int cost)
703 int tick = unit_tick[instance]; /* Issue time of the last issued insn. */
705 if (tick - clock > cost)
707 /* The scheduler is operating forward, so unit's last insn is the
708 executing insn and INSN is the candidate insn. We want a
709 more exact measure of the blockage if we execute INSN at CLOCK
710 given when we committed the execution of the unit's last insn.
712 The blockage value is given by either the unit's max blockage
713 constant, blockage range function, or blockage function. Use
714 the most exact form for the given unit. */
716 if (function_units[unit].blockage_range_function)
718 if (function_units[unit].blockage_function)
719 tick += (function_units[unit].blockage_function
720 (unit_last_insn[instance], insn)
721 - function_units[unit].max_blockage);
722 else
723 tick += ((int) MAX_BLOCKAGE_COST (blockage_range (unit, insn))
724 - function_units[unit].max_blockage);
726 if (tick - clock > cost)
727 cost = tick - clock;
729 return cost;
732 /* Record INSN as having begun execution on the units encoded by UNIT
733 at time CLOCK. The scheduler using only DFA description should
734 never use the following function. */
736 HAIFA_INLINE static void
737 schedule_unit (int unit, rtx insn, int clock)
739 int i;
741 if (unit >= 0)
743 int instance = unit;
744 #if MAX_MULTIPLICITY > 1
745 /* Find the first free instance of the function unit and use that
746 one. We assume that one is free. */
747 for (i = function_units[unit].multiplicity - 1; i > 0; i--)
749 if (!actual_hazard_this_instance (unit, instance, insn, clock, 0))
750 break;
751 instance += FUNCTION_UNITS_SIZE;
753 #endif
754 unit_last_insn[instance] = insn;
755 unit_tick[instance] = (clock + function_units[unit].max_blockage);
757 else
758 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
759 if ((unit & 1) != 0)
760 schedule_unit (i, insn, clock);
763 /* Return the actual hazard cost of executing INSN on the units
764 encoded by UNIT at time CLOCK if the previous actual hazard cost
765 was COST. The scheduler using only DFA description should never
766 use the following function. */
768 HAIFA_INLINE static int
769 actual_hazard (int unit, rtx insn, int clock, int cost)
771 int i;
773 if (unit >= 0)
775 /* Find the instance of the function unit with the minimum hazard. */
776 int instance = unit;
777 int best_cost = actual_hazard_this_instance (unit, instance, insn,
778 clock, cost);
779 #if MAX_MULTIPLICITY > 1
780 int this_cost;
782 if (best_cost > cost)
784 for (i = function_units[unit].multiplicity - 1; i > 0; i--)
786 instance += FUNCTION_UNITS_SIZE;
787 this_cost = actual_hazard_this_instance (unit, instance, insn,
788 clock, cost);
789 if (this_cost < best_cost)
791 best_cost = this_cost;
792 if (this_cost <= cost)
793 break;
797 #endif
798 cost = MAX (cost, best_cost);
800 else
801 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
802 if ((unit & 1) != 0)
803 cost = actual_hazard (i, insn, clock, cost);
805 return cost;
808 /* Return the potential hazard cost of executing an instruction on the
809 units encoded by UNIT if the previous potential hazard cost was
810 COST. An insn with a large blockage time is chosen in preference
811 to one with a smaller time; an insn that uses a unit that is more
812 likely to be used is chosen in preference to one with a unit that
813 is less used. We are trying to minimize a subsequent actual
814 hazard. The scheduler using only DFA description should never use
815 the following function. */
817 HAIFA_INLINE static int
818 potential_hazard (int unit, rtx insn, int cost)
820 int i, ncost;
821 unsigned int minb, maxb;
823 if (unit >= 0)
825 minb = maxb = function_units[unit].max_blockage;
826 if (maxb > 1)
828 if (function_units[unit].blockage_range_function)
830 maxb = minb = blockage_range (unit, insn);
831 maxb = MAX_BLOCKAGE_COST (maxb);
832 minb = MIN_BLOCKAGE_COST (minb);
835 if (maxb > 1)
837 /* Make the number of instructions left dominate. Make the
838 minimum delay dominate the maximum delay. If all these
839 are the same, use the unit number to add an arbitrary
840 ordering. Other terms can be added. */
841 ncost = minb * 0x40 + maxb;
842 ncost *= (unit_n_insns[unit] - 1) * 0x1000 + unit;
843 if (ncost > cost)
844 cost = ncost;
848 else
849 for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
850 if ((unit & 1) != 0)
851 cost = potential_hazard (i, insn, cost);
853 return cost;
856 /* Compute cost of executing INSN given the dependence LINK on the insn USED.
857 This is the number of cycles between instruction issue and
858 instruction results. */
860 HAIFA_INLINE int
861 insn_cost (rtx insn, rtx link, rtx used)
863 int cost = INSN_COST (insn);
865 if (cost < 0)
867 /* A USE insn, or something else we don't need to
868 understand. We can't pass these directly to
869 result_ready_cost or insn_default_latency because it will
870 trigger a fatal error for unrecognizable insns. */
871 if (recog_memoized (insn) < 0)
873 INSN_COST (insn) = 0;
874 return 0;
876 else
878 if (targetm.sched.use_dfa_pipeline_interface
879 && (*targetm.sched.use_dfa_pipeline_interface) ())
880 cost = insn_default_latency (insn);
881 else
882 cost = result_ready_cost (insn);
884 if (cost < 0)
885 cost = 0;
887 INSN_COST (insn) = cost;
891 /* In this case estimate cost without caring how insn is used. */
892 if (link == 0 || used == 0)
893 return cost;
895 /* A USE insn should never require the value used to be computed.
896 This allows the computation of a function's result and parameter
897 values to overlap the return and call. */
898 if (recog_memoized (used) < 0)
899 cost = 0;
900 else
902 if (targetm.sched.use_dfa_pipeline_interface
903 && (*targetm.sched.use_dfa_pipeline_interface) ())
905 if (INSN_CODE (insn) >= 0)
907 if (REG_NOTE_KIND (link) == REG_DEP_ANTI)
908 cost = 0;
909 else if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
911 cost = (insn_default_latency (insn)
912 - insn_default_latency (used));
913 if (cost <= 0)
914 cost = 1;
916 else if (bypass_p (insn))
917 cost = insn_latency (insn, used);
921 if (targetm.sched.adjust_cost)
922 cost = (*targetm.sched.adjust_cost) (used, link, insn, cost);
924 if (cost < 0)
925 cost = 0;
928 return cost;
931 /* Compute the priority number for INSN. */
933 static int
934 priority (rtx insn)
936 rtx link;
938 if (! INSN_P (insn))
939 return 0;
941 if (! INSN_PRIORITY_KNOWN (insn))
943 int this_priority = 0;
945 if (INSN_DEPEND (insn) == 0)
946 this_priority = insn_cost (insn, 0, 0);
947 else
949 for (link = INSN_DEPEND (insn); link; link = XEXP (link, 1))
951 rtx next;
952 int next_priority;
954 if (RTX_INTEGRATED_P (link))
955 continue;
957 next = XEXP (link, 0);
959 /* Critical path is meaningful in block boundaries only. */
960 if (! (*current_sched_info->contributes_to_priority) (next, insn))
961 continue;
963 next_priority = insn_cost (insn, link, next) + priority (next);
964 if (next_priority > this_priority)
965 this_priority = next_priority;
968 INSN_PRIORITY (insn) = this_priority;
969 INSN_PRIORITY_KNOWN (insn) = 1;
972 return INSN_PRIORITY (insn);
975 /* Macros and functions for keeping the priority queue sorted, and
976 dealing with queueing and dequeueing of instructions. */
978 #define SCHED_SORT(READY, N_READY) \
979 do { if ((N_READY) == 2) \
980 swap_sort (READY, N_READY); \
981 else if ((N_READY) > 2) \
982 qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); } \
983 while (0)
985 /* Returns a positive value if x is preferred; returns a negative value if
986 y is preferred. Should never return 0, since that will make the sort
987 unstable. */
989 static int
990 rank_for_schedule (const void *x, const void *y)
992 rtx tmp = *(const rtx *) y;
993 rtx tmp2 = *(const rtx *) x;
994 rtx link;
995 int tmp_class, tmp2_class, depend_count1, depend_count2;
996 int val, priority_val, weight_val, info_val;
998 /* The insn in a schedule group should be issued the first. */
999 if (SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
1000 return SCHED_GROUP_P (tmp2) ? 1 : -1;
1002 /* Prefer insn with higher priority. */
1003 priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
1005 if (priority_val)
1006 return priority_val;
1008 /* Prefer an insn with smaller contribution to registers-pressure. */
1009 if (!reload_completed &&
1010 (weight_val = INSN_REG_WEIGHT (tmp) - INSN_REG_WEIGHT (tmp2)))
1011 return weight_val;
1013 info_val = (*current_sched_info->rank) (tmp, tmp2);
1014 if (info_val)
1015 return info_val;
1017 /* Compare insns based on their relation to the last-scheduled-insn. */
1018 if (last_scheduled_insn)
1020 /* Classify the instructions into three classes:
1021 1) Data dependent on last schedule insn.
1022 2) Anti/Output dependent on last scheduled insn.
1023 3) Independent of last scheduled insn, or has latency of one.
1024 Choose the insn from the highest numbered class if different. */
1025 link = find_insn_list (tmp, INSN_DEPEND (last_scheduled_insn));
1026 if (link == 0 || insn_cost (last_scheduled_insn, link, tmp) == 1)
1027 tmp_class = 3;
1028 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
1029 tmp_class = 1;
1030 else
1031 tmp_class = 2;
1033 link = find_insn_list (tmp2, INSN_DEPEND (last_scheduled_insn));
1034 if (link == 0 || insn_cost (last_scheduled_insn, link, tmp2) == 1)
1035 tmp2_class = 3;
1036 else if (REG_NOTE_KIND (link) == 0) /* Data dependence. */
1037 tmp2_class = 1;
1038 else
1039 tmp2_class = 2;
1041 if ((val = tmp2_class - tmp_class))
1042 return val;
1045 /* Prefer the insn which has more later insns that depend on it.
1046 This gives the scheduler more freedom when scheduling later
1047 instructions at the expense of added register pressure. */
1048 depend_count1 = 0;
1049 for (link = INSN_DEPEND (tmp); link; link = XEXP (link, 1))
1050 depend_count1++;
1052 depend_count2 = 0;
1053 for (link = INSN_DEPEND (tmp2); link; link = XEXP (link, 1))
1054 depend_count2++;
1056 val = depend_count2 - depend_count1;
1057 if (val)
1058 return val;
1060 /* If insns are equally good, sort by INSN_LUID (original insn order),
1061 so that we make the sort stable. This minimizes instruction movement,
1062 thus minimizing sched's effect on debugging and cross-jumping. */
1063 return INSN_LUID (tmp) - INSN_LUID (tmp2);
1066 /* Resort the array A in which only element at index N may be out of order. */
1068 HAIFA_INLINE static void
1069 swap_sort (rtx *a, int n)
1071 rtx insn = a[n - 1];
1072 int i = n - 2;
1074 while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
1076 a[i + 1] = a[i];
1077 i -= 1;
1079 a[i + 1] = insn;
1082 /* Add INSN to the insn queue so that it can be executed at least
1083 N_CYCLES after the currently executing insn. Preserve insns
1084 chain for debugging purposes. */
1086 HAIFA_INLINE static void
1087 queue_insn (rtx insn, int n_cycles)
1089 int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
1090 rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
1091 insn_queue[next_q] = link;
1092 q_size += 1;
1094 if (sched_verbose >= 2)
1096 fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
1097 (*current_sched_info->print_insn) (insn, 0));
1099 fprintf (sched_dump, "queued for %d cycles.\n", n_cycles);
1103 /* Return a pointer to the bottom of the ready list, i.e. the insn
1104 with the lowest priority. */
1106 HAIFA_INLINE static rtx *
1107 ready_lastpos (struct ready_list *ready)
1109 if (ready->n_ready == 0)
1110 abort ();
1111 return ready->vec + ready->first - ready->n_ready + 1;
1114 /* Add an element INSN to the ready list so that it ends up with the lowest
1115 priority. */
1117 HAIFA_INLINE void
1118 ready_add (struct ready_list *ready, rtx insn)
1120 if (ready->first == ready->n_ready)
1122 memmove (ready->vec + ready->veclen - ready->n_ready,
1123 ready_lastpos (ready),
1124 ready->n_ready * sizeof (rtx));
1125 ready->first = ready->veclen - 1;
1127 ready->vec[ready->first - ready->n_ready] = insn;
1128 ready->n_ready++;
1131 /* Remove the element with the highest priority from the ready list and
1132 return it. */
1134 HAIFA_INLINE static rtx
1135 ready_remove_first (struct ready_list *ready)
1137 rtx t;
1138 if (ready->n_ready == 0)
1139 abort ();
1140 t = ready->vec[ready->first--];
1141 ready->n_ready--;
1142 /* If the queue becomes empty, reset it. */
1143 if (ready->n_ready == 0)
1144 ready->first = ready->veclen - 1;
1145 return t;
1148 /* The following code implements multi-pass scheduling for the first
1149 cycle. In other words, we will try to choose ready insn which
1150 permits to start maximum number of insns on the same cycle. */
1152 /* Return a pointer to the element INDEX from the ready. INDEX for
1153 insn with the highest priority is 0, and the lowest priority has
1154 N_READY - 1. */
1156 HAIFA_INLINE static rtx
1157 ready_element (struct ready_list *ready, int index)
1159 #ifdef ENABLE_CHECKING
1160 if (ready->n_ready == 0 || index >= ready->n_ready)
1161 abort ();
1162 #endif
1163 return ready->vec[ready->first - index];
1166 /* Remove the element INDEX from the ready list and return it. INDEX
1167 for insn with the highest priority is 0, and the lowest priority
1168 has N_READY - 1. */
1170 HAIFA_INLINE static rtx
1171 ready_remove (struct ready_list *ready, int index)
1173 rtx t;
1174 int i;
1176 if (index == 0)
1177 return ready_remove_first (ready);
1178 if (ready->n_ready == 0 || index >= ready->n_ready)
1179 abort ();
1180 t = ready->vec[ready->first - index];
1181 ready->n_ready--;
1182 for (i = index; i < ready->n_ready; i++)
1183 ready->vec[ready->first - i] = ready->vec[ready->first - i - 1];
1184 return t;
1188 /* Sort the ready list READY by ascending priority, using the SCHED_SORT
1189 macro. */
1191 HAIFA_INLINE static void
1192 ready_sort (struct ready_list *ready)
1194 rtx *first = ready_lastpos (ready);
1195 SCHED_SORT (first, ready->n_ready);
1198 /* PREV is an insn that is ready to execute. Adjust its priority if that
1199 will help shorten or lengthen register lifetimes as appropriate. Also
1200 provide a hook for the target to tweek itself. */
1202 HAIFA_INLINE static void
1203 adjust_priority (rtx prev)
1205 /* ??? There used to be code here to try and estimate how an insn
1206 affected register lifetimes, but it did it by looking at REG_DEAD
1207 notes, which we removed in schedule_region. Nor did it try to
1208 take into account register pressure or anything useful like that.
1210 Revisit when we have a machine model to work with and not before. */
1212 if (targetm.sched.adjust_priority)
1213 INSN_PRIORITY (prev) =
1214 (*targetm.sched.adjust_priority) (prev, INSN_PRIORITY (prev));
1217 /* Advance time on one cycle. */
1218 HAIFA_INLINE static void
1219 advance_one_cycle (void)
1221 if (targetm.sched.use_dfa_pipeline_interface
1222 && (*targetm.sched.use_dfa_pipeline_interface) ())
1224 if (targetm.sched.dfa_pre_cycle_insn)
1225 state_transition (curr_state,
1226 (*targetm.sched.dfa_pre_cycle_insn) ());
1228 state_transition (curr_state, NULL);
1230 if (targetm.sched.dfa_post_cycle_insn)
1231 state_transition (curr_state,
1232 (*targetm.sched.dfa_post_cycle_insn) ());
1236 /* Clock at which the previous instruction was issued. */
1237 static int last_clock_var;
1239 /* INSN is the "currently executing insn". Launch each insn which was
1240 waiting on INSN. READY is the ready list which contains the insns
1241 that are ready to fire. CLOCK is the current cycle. The function
1242 returns necessary cycle advance after issuing the insn (it is not
1243 zero for insns in a schedule group). */
1245 static int
1246 schedule_insn (rtx insn, struct ready_list *ready, int clock)
1248 rtx link;
1249 int advance = 0;
1250 int unit = 0;
1251 int premature_issue = 0;
1253 if (!targetm.sched.use_dfa_pipeline_interface
1254 || !(*targetm.sched.use_dfa_pipeline_interface) ())
1255 unit = insn_unit (insn);
1257 if (targetm.sched.use_dfa_pipeline_interface
1258 && (*targetm.sched.use_dfa_pipeline_interface) ()
1259 && sched_verbose >= 1)
1261 char buf[2048];
1263 print_insn (buf, insn, 0);
1264 buf[40] = 0;
1265 fprintf (sched_dump, ";;\t%3i--> %-40s:", clock, buf);
1267 if (recog_memoized (insn) < 0)
1268 fprintf (sched_dump, "nothing");
1269 else
1270 print_reservation (sched_dump, insn);
1271 fputc ('\n', sched_dump);
1273 else if (sched_verbose >= 2)
1275 fprintf (sched_dump, ";;\t\t--> scheduling insn <<<%d>>> on unit ",
1276 INSN_UID (insn));
1277 insn_print_units (insn);
1278 fputc ('\n', sched_dump);
1281 if (!targetm.sched.use_dfa_pipeline_interface
1282 || !(*targetm.sched.use_dfa_pipeline_interface) ())
1284 if (sched_verbose && unit == -1)
1285 visualize_no_unit (insn);
1288 if (MAX_BLOCKAGE > 1 || issue_rate > 1 || sched_verbose)
1289 schedule_unit (unit, insn, clock);
1291 if (INSN_DEPEND (insn) == 0)
1292 return 0;
1295 if (INSN_TICK (insn) > clock)
1297 /* 'insn' has been prematurely moved from the queue to the
1298 ready list. */
1299 premature_issue = INSN_TICK (insn) - clock;
1302 for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
1304 rtx next = XEXP (link, 0);
1305 int cost = insn_cost (insn, link, next);
1307 INSN_TICK (next) = MAX (INSN_TICK (next), clock + cost + premature_issue);
1309 if ((INSN_DEP_COUNT (next) -= 1) == 0)
1311 int effective_cost = INSN_TICK (next) - clock;
1313 if (! (*current_sched_info->new_ready) (next))
1314 continue;
1316 if (sched_verbose >= 2)
1318 fprintf (sched_dump, ";;\t\tdependences resolved: insn %s ",
1319 (*current_sched_info->print_insn) (next, 0));
1321 if (effective_cost < 1)
1322 fprintf (sched_dump, "into ready\n");
1323 else
1324 fprintf (sched_dump, "into queue with cost=%d\n",
1325 effective_cost);
1328 /* Adjust the priority of NEXT and either put it on the ready
1329 list or queue it. */
1330 adjust_priority (next);
1331 if (effective_cost < 1)
1332 ready_add (ready, next);
1333 else
1335 queue_insn (next, effective_cost);
1337 if (SCHED_GROUP_P (next) && advance < effective_cost)
1338 advance = effective_cost;
1343 /* Annotate the instruction with issue information -- TImode
1344 indicates that the instruction is expected not to be able
1345 to issue on the same cycle as the previous insn. A machine
1346 may use this information to decide how the instruction should
1347 be aligned. */
1348 if (issue_rate > 1
1349 && GET_CODE (PATTERN (insn)) != USE
1350 && GET_CODE (PATTERN (insn)) != CLOBBER)
1352 if (reload_completed)
1353 PUT_MODE (insn, clock > last_clock_var ? TImode : VOIDmode);
1354 last_clock_var = clock;
1356 return advance;
1359 /* Functions for handling of notes. */
1361 /* Delete notes beginning with INSN and put them in the chain
1362 of notes ended by NOTE_LIST.
1363 Returns the insn following the notes. */
1365 static rtx
1366 unlink_other_notes (rtx insn, rtx tail)
1368 rtx prev = PREV_INSN (insn);
1370 while (insn != tail && GET_CODE (insn) == NOTE)
1372 rtx next = NEXT_INSN (insn);
1373 /* Delete the note from its current position. */
1374 if (prev)
1375 NEXT_INSN (prev) = next;
1376 if (next)
1377 PREV_INSN (next) = prev;
1379 /* See sched_analyze to see how these are handled. */
1380 if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG
1381 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_END
1382 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK
1383 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_BEG
1384 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_END)
1386 /* Insert the note at the end of the notes list. */
1387 PREV_INSN (insn) = note_list;
1388 if (note_list)
1389 NEXT_INSN (note_list) = insn;
1390 note_list = insn;
1393 insn = next;
1395 return insn;
1398 /* Delete line notes beginning with INSN. Record line-number notes so
1399 they can be reused. Returns the insn following the notes. */
1401 static rtx
1402 unlink_line_notes (rtx insn, rtx tail)
1404 rtx prev = PREV_INSN (insn);
1406 while (insn != tail && GET_CODE (insn) == NOTE)
1408 rtx next = NEXT_INSN (insn);
1410 if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
1412 /* Delete the note from its current position. */
1413 if (prev)
1414 NEXT_INSN (prev) = next;
1415 if (next)
1416 PREV_INSN (next) = prev;
1418 /* Record line-number notes so they can be reused. */
1419 LINE_NOTE (insn) = insn;
1421 else
1422 prev = insn;
1424 insn = next;
1426 return insn;
1429 /* Return the head and tail pointers of BB. */
1431 void
1432 get_block_head_tail (int b, rtx *headp, rtx *tailp)
1434 /* HEAD and TAIL delimit the basic block being scheduled. */
1435 rtx head = BLOCK_HEAD (b);
1436 rtx tail = BLOCK_END (b);
1438 /* Don't include any notes or labels at the beginning of the
1439 basic block, or notes at the ends of basic blocks. */
1440 while (head != tail)
1442 if (GET_CODE (head) == NOTE)
1443 head = NEXT_INSN (head);
1444 else if (GET_CODE (tail) == NOTE)
1445 tail = PREV_INSN (tail);
1446 else if (GET_CODE (head) == CODE_LABEL)
1447 head = NEXT_INSN (head);
1448 else
1449 break;
1452 *headp = head;
1453 *tailp = tail;
1456 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ]. */
1459 no_real_insns_p (rtx head, rtx tail)
1461 while (head != NEXT_INSN (tail))
1463 if (GET_CODE (head) != NOTE && GET_CODE (head) != CODE_LABEL)
1464 return 0;
1465 head = NEXT_INSN (head);
1467 return 1;
1470 /* Delete line notes from one block. Save them so they can be later restored
1471 (in restore_line_notes). HEAD and TAIL are the boundaries of the
1472 block in which notes should be processed. */
1474 void
1475 rm_line_notes (rtx head, rtx tail)
1477 rtx next_tail;
1478 rtx insn;
1480 next_tail = NEXT_INSN (tail);
1481 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1483 rtx prev;
1485 /* Farm out notes, and maybe save them in NOTE_LIST.
1486 This is needed to keep the debugger from
1487 getting completely deranged. */
1488 if (GET_CODE (insn) == NOTE)
1490 prev = insn;
1491 insn = unlink_line_notes (insn, next_tail);
1493 if (prev == tail)
1494 abort ();
1495 if (prev == head)
1496 abort ();
1497 if (insn == next_tail)
1498 abort ();
1503 /* Save line number notes for each insn in block B. HEAD and TAIL are
1504 the boundaries of the block in which notes should be processed. */
1506 void
1507 save_line_notes (int b, rtx head, rtx tail)
1509 rtx next_tail;
1511 /* We must use the true line number for the first insn in the block
1512 that was computed and saved at the start of this pass. We can't
1513 use the current line number, because scheduling of the previous
1514 block may have changed the current line number. */
1516 rtx line = line_note_head[b];
1517 rtx insn;
1519 next_tail = NEXT_INSN (tail);
1521 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1522 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1523 line = insn;
1524 else
1525 LINE_NOTE (insn) = line;
1528 /* After a block was scheduled, insert line notes into the insns list.
1529 HEAD and TAIL are the boundaries of the block in which notes should
1530 be processed. */
1532 void
1533 restore_line_notes (rtx head, rtx tail)
1535 rtx line, note, prev, new;
1536 int added_notes = 0;
1537 rtx next_tail, insn;
1539 head = head;
1540 next_tail = NEXT_INSN (tail);
1542 /* Determine the current line-number. We want to know the current
1543 line number of the first insn of the block here, in case it is
1544 different from the true line number that was saved earlier. If
1545 different, then we need a line number note before the first insn
1546 of this block. If it happens to be the same, then we don't want to
1547 emit another line number note here. */
1548 for (line = head; line; line = PREV_INSN (line))
1549 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
1550 break;
1552 /* Walk the insns keeping track of the current line-number and inserting
1553 the line-number notes as needed. */
1554 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1555 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1556 line = insn;
1557 /* This used to emit line number notes before every non-deleted note.
1558 However, this confuses a debugger, because line notes not separated
1559 by real instructions all end up at the same address. I can find no
1560 use for line number notes before other notes, so none are emitted. */
1561 else if (GET_CODE (insn) != NOTE
1562 && INSN_UID (insn) < old_max_uid
1563 && (note = LINE_NOTE (insn)) != 0
1564 && note != line
1565 && (line == 0
1566 || NOTE_LINE_NUMBER (note) != NOTE_LINE_NUMBER (line)
1567 || NOTE_SOURCE_FILE (note) != NOTE_SOURCE_FILE (line)))
1569 line = note;
1570 prev = PREV_INSN (insn);
1571 if (LINE_NOTE (note))
1573 /* Re-use the original line-number note. */
1574 LINE_NOTE (note) = 0;
1575 PREV_INSN (note) = prev;
1576 NEXT_INSN (prev) = note;
1577 PREV_INSN (insn) = note;
1578 NEXT_INSN (note) = insn;
1580 else
1582 added_notes++;
1583 new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
1584 NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
1585 RTX_INTEGRATED_P (new) = RTX_INTEGRATED_P (note);
1588 if (sched_verbose && added_notes)
1589 fprintf (sched_dump, ";; added %d line-number notes\n", added_notes);
1592 /* After scheduling the function, delete redundant line notes from the
1593 insns list. */
1595 void
1596 rm_redundant_line_notes (void)
1598 rtx line = 0;
1599 rtx insn = get_insns ();
1600 int active_insn = 0;
1601 int notes = 0;
1603 /* Walk the insns deleting redundant line-number notes. Many of these
1604 are already present. The remainder tend to occur at basic
1605 block boundaries. */
1606 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1607 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
1609 /* If there are no active insns following, INSN is redundant. */
1610 if (active_insn == 0)
1612 notes++;
1613 NOTE_SOURCE_FILE (insn) = 0;
1614 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1616 /* If the line number is unchanged, LINE is redundant. */
1617 else if (line
1618 && NOTE_LINE_NUMBER (line) == NOTE_LINE_NUMBER (insn)
1619 && NOTE_SOURCE_FILE (line) == NOTE_SOURCE_FILE (insn))
1621 notes++;
1622 NOTE_SOURCE_FILE (line) = 0;
1623 NOTE_LINE_NUMBER (line) = NOTE_INSN_DELETED;
1624 line = insn;
1626 else
1627 line = insn;
1628 active_insn = 0;
1630 else if (!((GET_CODE (insn) == NOTE
1631 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
1632 || (GET_CODE (insn) == INSN
1633 && (GET_CODE (PATTERN (insn)) == USE
1634 || GET_CODE (PATTERN (insn)) == CLOBBER))))
1635 active_insn++;
1637 if (sched_verbose && notes)
1638 fprintf (sched_dump, ";; deleted %d line-number notes\n", notes);
1641 /* Delete notes between HEAD and TAIL and put them in the chain
1642 of notes ended by NOTE_LIST. */
1644 void
1645 rm_other_notes (rtx head, rtx tail)
1647 rtx next_tail;
1648 rtx insn;
1650 note_list = 0;
1651 if (head == tail && (! INSN_P (head)))
1652 return;
1654 next_tail = NEXT_INSN (tail);
1655 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1657 rtx prev;
1659 /* Farm out notes, and maybe save them in NOTE_LIST.
1660 This is needed to keep the debugger from
1661 getting completely deranged. */
1662 if (GET_CODE (insn) == NOTE)
1664 prev = insn;
1666 insn = unlink_other_notes (insn, next_tail);
1668 if (prev == tail)
1669 abort ();
1670 if (prev == head)
1671 abort ();
1672 if (insn == next_tail)
1673 abort ();
1678 /* Functions for computation of registers live/usage info. */
1680 /* This function looks for a new register being defined.
1681 If the destination register is already used by the source,
1682 a new register is not needed. */
1684 static int
1685 find_set_reg_weight (rtx x)
1687 if (GET_CODE (x) == CLOBBER
1688 && register_operand (SET_DEST (x), VOIDmode))
1689 return 1;
1690 if (GET_CODE (x) == SET
1691 && register_operand (SET_DEST (x), VOIDmode))
1693 if (GET_CODE (SET_DEST (x)) == REG)
1695 if (!reg_mentioned_p (SET_DEST (x), SET_SRC (x)))
1696 return 1;
1697 else
1698 return 0;
1700 return 1;
1702 return 0;
1705 /* Calculate INSN_REG_WEIGHT for all insns of a block. */
1707 static void
1708 find_insn_reg_weight (int b)
1710 rtx insn, next_tail, head, tail;
1712 get_block_head_tail (b, &head, &tail);
1713 next_tail = NEXT_INSN (tail);
1715 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1717 int reg_weight = 0;
1718 rtx x;
1720 /* Handle register life information. */
1721 if (! INSN_P (insn))
1722 continue;
1724 /* Increment weight for each register born here. */
1725 x = PATTERN (insn);
1726 reg_weight += find_set_reg_weight (x);
1727 if (GET_CODE (x) == PARALLEL)
1729 int j;
1730 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
1732 x = XVECEXP (PATTERN (insn), 0, j);
1733 reg_weight += find_set_reg_weight (x);
1736 /* Decrement weight for each register that dies here. */
1737 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1739 if (REG_NOTE_KIND (x) == REG_DEAD
1740 || REG_NOTE_KIND (x) == REG_UNUSED)
1741 reg_weight--;
1744 INSN_REG_WEIGHT (insn) = reg_weight;
1748 /* Scheduling clock, modified in schedule_block() and queue_to_ready (). */
1749 static int clock_var;
1751 /* Move insns that became ready to fire from queue to ready list. */
1753 static void
1754 queue_to_ready (struct ready_list *ready)
1756 rtx insn;
1757 rtx link;
1759 q_ptr = NEXT_Q (q_ptr);
1761 /* Add all pending insns that can be scheduled without stalls to the
1762 ready list. */
1763 for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
1765 insn = XEXP (link, 0);
1766 q_size -= 1;
1768 if (sched_verbose >= 2)
1769 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1770 (*current_sched_info->print_insn) (insn, 0));
1772 ready_add (ready, insn);
1773 if (sched_verbose >= 2)
1774 fprintf (sched_dump, "moving to ready without stalls\n");
1776 insn_queue[q_ptr] = 0;
1778 /* If there are no ready insns, stall until one is ready and add all
1779 of the pending insns at that point to the ready list. */
1780 if (ready->n_ready == 0)
1782 int stalls;
1784 for (stalls = 1; stalls <= MAX_INSN_QUEUE_INDEX; stalls++)
1786 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
1788 for (; link; link = XEXP (link, 1))
1790 insn = XEXP (link, 0);
1791 q_size -= 1;
1793 if (sched_verbose >= 2)
1794 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1795 (*current_sched_info->print_insn) (insn, 0));
1797 ready_add (ready, insn);
1798 if (sched_verbose >= 2)
1799 fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
1801 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = 0;
1803 advance_one_cycle ();
1805 break;
1808 advance_one_cycle ();
1811 if ((!targetm.sched.use_dfa_pipeline_interface
1812 || !(*targetm.sched.use_dfa_pipeline_interface) ())
1813 && sched_verbose && stalls)
1814 visualize_stall_cycles (stalls);
1816 q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
1817 clock_var += stalls;
1821 /* Used by early_queue_to_ready. Determines whether it is "ok" to
1822 prematurely move INSN from the queue to the ready list. Currently,
1823 if a target defines the hook 'is_costly_dependence', this function
1824 uses the hook to check whether there exist any dependences which are
1825 considered costly by the target, between INSN and other insns that
1826 have already been scheduled. Dependences are checked up to Y cycles
1827 back, with default Y=1; The flag -fsched-stalled-insns-dep=Y allows
1828 controlling this value.
1829 (Other considerations could be taken into account instead (or in
1830 addition) depending on user flags and target hooks. */
1832 static bool
1833 ok_for_early_queue_removal (rtx insn)
1835 int n_cycles;
1836 rtx prev_insn = last_scheduled_insn;
1838 if (targetm.sched.is_costly_dependence)
1840 for (n_cycles = flag_sched_stalled_insns_dep; n_cycles; n_cycles--)
1842 for ( ; prev_insn; prev_insn = PREV_INSN (prev_insn))
1844 rtx dep_link = 0;
1845 int dep_cost;
1847 if (GET_CODE (prev_insn) != NOTE)
1849 dep_link = find_insn_list (insn, INSN_DEPEND (prev_insn));
1850 if (dep_link)
1852 dep_cost = insn_cost (prev_insn, dep_link, insn) ;
1853 if (targetm.sched.is_costly_dependence (prev_insn, insn,
1854 dep_link, dep_cost,
1855 flag_sched_stalled_insns_dep - n_cycles))
1856 return false;
1860 if (GET_MODE (prev_insn) == TImode) /* end of dispatch group */
1861 break;
1864 if (!prev_insn)
1865 break;
1866 prev_insn = PREV_INSN (prev_insn);
1870 return true;
1874 /* Remove insns from the queue, before they become "ready" with respect
1875 to FU latency considerations. */
1877 static int
1878 early_queue_to_ready (state_t state, struct ready_list *ready)
1880 rtx insn;
1881 rtx link;
1882 rtx next_link;
1883 rtx prev_link;
1884 bool move_to_ready;
1885 int cost;
1886 state_t temp_state = alloca (dfa_state_size);
1887 int stalls;
1888 int insns_removed = 0;
1891 Flag '-fsched-stalled-insns=X' determines the aggressiveness of this
1892 function:
1894 X == 0: There is no limit on how many queued insns can be removed
1895 prematurely. (flag_sched_stalled_insns = -1).
1897 X >= 1: Only X queued insns can be removed prematurely in each
1898 invocation. (flag_sched_stalled_insns = X).
1900 Otherwise: Early queue removal is disabled.
1901 (flag_sched_stalled_insns = 0)
1904 if (! flag_sched_stalled_insns)
1905 return 0;
1907 for (stalls = 0; stalls <= MAX_INSN_QUEUE_INDEX; stalls++)
1909 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
1911 if (sched_verbose > 6)
1912 fprintf (sched_dump, ";; look at index %d + %d\n", q_ptr, stalls);
1914 prev_link = 0;
1915 while (link)
1917 next_link = XEXP (link, 1);
1918 insn = XEXP (link, 0);
1919 if (insn && sched_verbose > 6)
1920 print_rtl_single (sched_dump, insn);
1922 memcpy (temp_state, state, dfa_state_size);
1923 if (recog_memoized (insn) < 0)
1924 /* non-negative to indicate that it's not ready
1925 to avoid infinite Q->R->Q->R... */
1926 cost = 0;
1927 else
1928 cost = state_transition (temp_state, insn);
1930 if (sched_verbose >= 6)
1931 fprintf (sched_dump, "transition cost = %d\n", cost);
1933 move_to_ready = false;
1934 if (cost < 0)
1936 move_to_ready = ok_for_early_queue_removal (insn);
1937 if (move_to_ready == true)
1939 /* move from Q to R */
1940 q_size -= 1;
1941 ready_add (ready, insn);
1943 if (prev_link)
1944 XEXP (prev_link, 1) = next_link;
1945 else
1946 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = next_link;
1948 free_INSN_LIST_node (link);
1950 if (sched_verbose >= 2)
1951 fprintf (sched_dump, ";;\t\tEarly Q-->Ready: insn %s\n",
1952 (*current_sched_info->print_insn) (insn, 0));
1954 insns_removed++;
1955 if (insns_removed == flag_sched_stalled_insns)
1956 /* remove only one insn from Q at a time */
1957 return insns_removed;
1961 if (move_to_ready == false)
1962 prev_link = link;
1964 link = next_link;
1965 } /* while link */
1966 } /* if link */
1968 } /* for stalls.. */
1970 return insns_removed;
1974 /* Print the ready list for debugging purposes. Callable from debugger. */
1976 static void
1977 debug_ready_list (struct ready_list *ready)
1979 rtx *p;
1980 int i;
1982 if (ready->n_ready == 0)
1984 fprintf (sched_dump, "\n");
1985 return;
1988 p = ready_lastpos (ready);
1989 for (i = 0; i < ready->n_ready; i++)
1990 fprintf (sched_dump, " %s", (*current_sched_info->print_insn) (p[i], 0));
1991 fprintf (sched_dump, "\n");
1994 /* move_insn1: Remove INSN from insn chain, and link it after LAST insn. */
1996 static rtx
1997 move_insn1 (rtx insn, rtx last)
1999 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2000 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2002 NEXT_INSN (insn) = NEXT_INSN (last);
2003 PREV_INSN (NEXT_INSN (last)) = insn;
2005 NEXT_INSN (last) = insn;
2006 PREV_INSN (insn) = last;
2008 return insn;
2011 /* Search INSN for REG_SAVE_NOTE note pairs for
2012 NOTE_INSN_{LOOP,EHREGION}_{BEG,END}; and convert them back into
2013 NOTEs. The REG_SAVE_NOTE note following first one is contains the
2014 saved value for NOTE_BLOCK_NUMBER which is useful for
2015 NOTE_INSN_EH_REGION_{BEG,END} NOTEs. LAST is the last instruction
2016 output by the instruction scheduler. Return the new value of LAST. */
2018 static rtx
2019 reemit_notes (rtx insn, rtx last)
2021 rtx note, retval;
2023 retval = last;
2024 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2026 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
2028 enum insn_note note_type = INTVAL (XEXP (note, 0));
2030 last = emit_note_before (note_type, last);
2031 remove_note (insn, note);
2032 note = XEXP (note, 1);
2033 if (note_type == NOTE_INSN_EH_REGION_BEG
2034 || note_type == NOTE_INSN_EH_REGION_END)
2035 NOTE_EH_HANDLER (last) = INTVAL (XEXP (note, 0));
2036 remove_note (insn, note);
2039 return retval;
2042 /* Move INSN. Reemit notes if needed.
2044 Return the last insn emitted by the scheduler, which is the
2045 return value from the first call to reemit_notes. */
2047 static rtx
2048 move_insn (rtx insn, rtx last)
2050 rtx retval = NULL;
2052 move_insn1 (insn, last);
2054 /* If this is the first call to reemit_notes, then record
2055 its return value. */
2056 if (retval == NULL_RTX)
2057 retval = reemit_notes (insn, insn);
2058 else
2059 reemit_notes (insn, insn);
2061 SCHED_GROUP_P (insn) = 0;
2063 return retval;
2066 /* The following structure describe an entry of the stack of choices. */
2067 struct choice_entry
2069 /* Ordinal number of the issued insn in the ready queue. */
2070 int index;
2071 /* The number of the rest insns whose issues we should try. */
2072 int rest;
2073 /* The number of issued essential insns. */
2074 int n;
2075 /* State after issuing the insn. */
2076 state_t state;
2079 /* The following array is used to implement a stack of choices used in
2080 function max_issue. */
2081 static struct choice_entry *choice_stack;
2083 /* The following variable value is number of essential insns issued on
2084 the current cycle. An insn is essential one if it changes the
2085 processors state. */
2086 static int cycle_issued_insns;
2088 /* The following variable value is maximal number of tries of issuing
2089 insns for the first cycle multipass insn scheduling. We define
2090 this value as constant*(DFA_LOOKAHEAD**ISSUE_RATE). We would not
2091 need this constraint if all real insns (with non-negative codes)
2092 had reservations because in this case the algorithm complexity is
2093 O(DFA_LOOKAHEAD**ISSUE_RATE). Unfortunately, the dfa descriptions
2094 might be incomplete and such insn might occur. For such
2095 descriptions, the complexity of algorithm (without the constraint)
2096 could achieve DFA_LOOKAHEAD ** N , where N is the queue length. */
2097 static int max_lookahead_tries;
2099 /* The following value is value of hook
2100 `first_cycle_multipass_dfa_lookahead' at the last call of
2101 `max_issue'. */
2102 static int cached_first_cycle_multipass_dfa_lookahead = 0;
2104 /* The following value is value of `issue_rate' at the last call of
2105 `sched_init'. */
2106 static int cached_issue_rate = 0;
2108 /* The following function returns maximal (or close to maximal) number
2109 of insns which can be issued on the same cycle and one of which
2110 insns is insns with the best rank (the first insn in READY). To
2111 make this function tries different samples of ready insns. READY
2112 is current queue `ready'. Global array READY_TRY reflects what
2113 insns are already issued in this try. INDEX will contain index
2114 of the best insn in READY. The following function is used only for
2115 first cycle multipass scheduling. */
2116 static int
2117 max_issue (struct ready_list *ready, int *index)
2119 int n, i, all, n_ready, best, delay, tries_num;
2120 struct choice_entry *top;
2121 rtx insn;
2123 best = 0;
2124 memcpy (choice_stack->state, curr_state, dfa_state_size);
2125 top = choice_stack;
2126 top->rest = cached_first_cycle_multipass_dfa_lookahead;
2127 top->n = 0;
2128 n_ready = ready->n_ready;
2129 for (all = i = 0; i < n_ready; i++)
2130 if (!ready_try [i])
2131 all++;
2132 i = 0;
2133 tries_num = 0;
2134 for (;;)
2136 if (top->rest == 0 || i >= n_ready)
2138 if (top == choice_stack)
2139 break;
2140 if (best < top - choice_stack && ready_try [0])
2142 best = top - choice_stack;
2143 *index = choice_stack [1].index;
2144 if (top->n == issue_rate - cycle_issued_insns || best == all)
2145 break;
2147 i = top->index;
2148 ready_try [i] = 0;
2149 top--;
2150 memcpy (curr_state, top->state, dfa_state_size);
2152 else if (!ready_try [i])
2154 tries_num++;
2155 if (tries_num > max_lookahead_tries)
2156 break;
2157 insn = ready_element (ready, i);
2158 delay = state_transition (curr_state, insn);
2159 if (delay < 0)
2161 if (state_dead_lock_p (curr_state))
2162 top->rest = 0;
2163 else
2164 top->rest--;
2165 n = top->n;
2166 if (memcmp (top->state, curr_state, dfa_state_size) != 0)
2167 n++;
2168 top++;
2169 top->rest = cached_first_cycle_multipass_dfa_lookahead;
2170 top->index = i;
2171 top->n = n;
2172 memcpy (top->state, curr_state, dfa_state_size);
2173 ready_try [i] = 1;
2174 i = -1;
2177 i++;
2179 while (top != choice_stack)
2181 ready_try [top->index] = 0;
2182 top--;
2184 memcpy (curr_state, choice_stack->state, dfa_state_size);
2185 return best;
2188 /* The following function chooses insn from READY and modifies
2189 *N_READY and READY. The following function is used only for first
2190 cycle multipass scheduling. */
2192 static rtx
2193 choose_ready (struct ready_list *ready)
2195 int lookahead = 0;
2197 if (targetm.sched.first_cycle_multipass_dfa_lookahead)
2198 lookahead = (*targetm.sched.first_cycle_multipass_dfa_lookahead) ();
2199 if (lookahead <= 0 || SCHED_GROUP_P (ready_element (ready, 0)))
2200 return ready_remove_first (ready);
2201 else
2203 /* Try to choose the better insn. */
2204 int index = 0, i;
2205 rtx insn;
2207 if (cached_first_cycle_multipass_dfa_lookahead != lookahead)
2209 cached_first_cycle_multipass_dfa_lookahead = lookahead;
2210 max_lookahead_tries = 100;
2211 for (i = 0; i < issue_rate; i++)
2212 max_lookahead_tries *= lookahead;
2214 insn = ready_element (ready, 0);
2215 if (INSN_CODE (insn) < 0)
2216 return ready_remove_first (ready);
2217 for (i = 1; i < ready->n_ready; i++)
2219 insn = ready_element (ready, i);
2220 ready_try [i]
2221 = (INSN_CODE (insn) < 0
2222 || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard
2223 && !(*targetm.sched.first_cycle_multipass_dfa_lookahead_guard) (insn)));
2225 if (max_issue (ready, &index) == 0)
2226 return ready_remove_first (ready);
2227 else
2228 return ready_remove (ready, index);
2232 /* Called from backends from targetm.sched.reorder to emit stuff into
2233 the instruction stream. */
2236 sched_emit_insn (rtx pat)
2238 rtx insn = emit_insn_after (pat, last_scheduled_insn);
2239 last_scheduled_insn = insn;
2240 return insn;
2243 /* Use forward list scheduling to rearrange insns of block B in region RGN,
2244 possibly bringing insns from subsequent blocks in the same region. */
2246 void
2247 schedule_block (int b, int rgn_n_insns)
2249 struct ready_list ready;
2250 int i, first_cycle_insn_p;
2251 int can_issue_more;
2252 state_t temp_state = NULL; /* It is used for multipass scheduling. */
2253 int sort_p, advance, start_clock_var;
2255 /* Head/tail info for this block. */
2256 rtx prev_head = current_sched_info->prev_head;
2257 rtx next_tail = current_sched_info->next_tail;
2258 rtx head = NEXT_INSN (prev_head);
2259 rtx tail = PREV_INSN (next_tail);
2261 /* We used to have code to avoid getting parameters moved from hard
2262 argument registers into pseudos.
2264 However, it was removed when it proved to be of marginal benefit
2265 and caused problems because schedule_block and compute_forward_dependences
2266 had different notions of what the "head" insn was. */
2268 if (head == tail && (! INSN_P (head)))
2269 abort ();
2271 /* Debug info. */
2272 if (sched_verbose)
2274 fprintf (sched_dump, ";; ======================================================\n");
2275 fprintf (sched_dump,
2276 ";; -- basic block %d from %d to %d -- %s reload\n",
2277 b, INSN_UID (head), INSN_UID (tail),
2278 (reload_completed ? "after" : "before"));
2279 fprintf (sched_dump, ";; ======================================================\n");
2280 fprintf (sched_dump, "\n");
2282 visualize_alloc ();
2283 init_block_visualization ();
2286 if (targetm.sched.use_dfa_pipeline_interface
2287 && (*targetm.sched.use_dfa_pipeline_interface) ())
2288 state_reset (curr_state);
2289 else
2290 clear_units ();
2292 /* Allocate the ready list. */
2293 ready.veclen = rgn_n_insns + 1 + issue_rate;
2294 ready.first = ready.veclen - 1;
2295 ready.vec = xmalloc (ready.veclen * sizeof (rtx));
2296 ready.n_ready = 0;
2298 if (targetm.sched.use_dfa_pipeline_interface
2299 && (*targetm.sched.use_dfa_pipeline_interface) ())
2301 /* It is used for first cycle multipass scheduling. */
2302 temp_state = alloca (dfa_state_size);
2303 ready_try = xcalloc ((rgn_n_insns + 1), sizeof (char));
2304 choice_stack = xmalloc ((rgn_n_insns + 1)
2305 * sizeof (struct choice_entry));
2306 for (i = 0; i <= rgn_n_insns; i++)
2307 choice_stack[i].state = xmalloc (dfa_state_size);
2310 (*current_sched_info->init_ready_list) (&ready);
2312 if (targetm.sched.md_init)
2313 (*targetm.sched.md_init) (sched_dump, sched_verbose, ready.veclen);
2315 /* We start inserting insns after PREV_HEAD. */
2316 last_scheduled_insn = prev_head;
2318 /* Initialize INSN_QUEUE. Q_SIZE is the total number of insns in the
2319 queue. */
2320 q_ptr = 0;
2321 q_size = 0;
2323 if (!targetm.sched.use_dfa_pipeline_interface
2324 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2325 max_insn_queue_index_macro_value = INSN_QUEUE_SIZE - 1;
2326 else
2327 max_insn_queue_index_macro_value = max_insn_queue_index;
2329 insn_queue = alloca ((MAX_INSN_QUEUE_INDEX + 1) * sizeof (rtx));
2330 memset (insn_queue, 0, (MAX_INSN_QUEUE_INDEX + 1) * sizeof (rtx));
2331 last_clock_var = -1;
2333 /* Start just before the beginning of time. */
2334 clock_var = -1;
2335 advance = 0;
2337 sort_p = TRUE;
2338 /* Loop until all the insns in BB are scheduled. */
2339 while ((*current_sched_info->schedule_more_p) ())
2343 start_clock_var = clock_var;
2345 clock_var++;
2347 advance_one_cycle ();
2349 /* Add to the ready list all pending insns that can be issued now.
2350 If there are no ready insns, increment clock until one
2351 is ready and add all pending insns at that point to the ready
2352 list. */
2353 queue_to_ready (&ready);
2355 if (ready.n_ready == 0)
2356 abort ();
2358 if (sched_verbose >= 2)
2360 fprintf (sched_dump, ";;\t\tReady list after queue_to_ready: ");
2361 debug_ready_list (&ready);
2363 advance -= clock_var - start_clock_var;
2365 while (advance > 0);
2367 if (sort_p)
2369 /* Sort the ready list based on priority. */
2370 ready_sort (&ready);
2372 if (sched_verbose >= 2)
2374 fprintf (sched_dump, ";;\t\tReady list after ready_sort: ");
2375 debug_ready_list (&ready);
2379 /* Allow the target to reorder the list, typically for
2380 better instruction bundling. */
2381 if (sort_p && targetm.sched.reorder
2382 && (ready.n_ready == 0
2383 || !SCHED_GROUP_P (ready_element (&ready, 0))))
2384 can_issue_more =
2385 (*targetm.sched.reorder) (sched_dump, sched_verbose,
2386 ready_lastpos (&ready),
2387 &ready.n_ready, clock_var);
2388 else
2389 can_issue_more = issue_rate;
2391 first_cycle_insn_p = 1;
2392 cycle_issued_insns = 0;
2393 for (;;)
2395 rtx insn;
2396 int cost;
2398 if (sched_verbose >= 2)
2400 fprintf (sched_dump, ";;\tReady list (t =%3d): ",
2401 clock_var);
2402 debug_ready_list (&ready);
2405 if (!targetm.sched.use_dfa_pipeline_interface
2406 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2408 if (ready.n_ready == 0 || !can_issue_more
2409 || !(*current_sched_info->schedule_more_p) ())
2410 break;
2411 insn = ready_remove_first (&ready);
2412 cost = actual_hazard (insn_unit (insn), insn, clock_var, 0);
2414 else
2416 if (ready.n_ready == 0
2417 && can_issue_more
2418 && reload_completed)
2420 /* Allow scheduling insns directly from the queue in case
2421 there's nothing better to do (ready list is empty) but
2422 there are still vacant dispatch slots in the current cycle. */
2423 if (sched_verbose >= 6)
2424 fprintf(sched_dump,";;\t\tSecond chance\n");
2425 memcpy (temp_state, curr_state, dfa_state_size);
2426 if (early_queue_to_ready (temp_state, &ready))
2427 ready_sort (&ready);
2430 if (ready.n_ready == 0 || !can_issue_more
2431 || state_dead_lock_p (curr_state)
2432 || !(*current_sched_info->schedule_more_p) ())
2433 break;
2435 /* Select and remove the insn from the ready list. */
2436 if (sort_p)
2437 insn = choose_ready (&ready);
2438 else
2439 insn = ready_remove_first (&ready);
2441 if (targetm.sched.dfa_new_cycle
2442 && (*targetm.sched.dfa_new_cycle) (sched_dump, sched_verbose,
2443 insn, last_clock_var,
2444 clock_var, &sort_p))
2446 ready_add (&ready, insn);
2447 break;
2450 sort_p = TRUE;
2451 memcpy (temp_state, curr_state, dfa_state_size);
2452 if (recog_memoized (insn) < 0)
2454 if (!first_cycle_insn_p
2455 && (GET_CODE (PATTERN (insn)) == ASM_INPUT
2456 || asm_noperands (PATTERN (insn)) >= 0))
2457 /* This is asm insn which is tryed to be issued on the
2458 cycle not first. Issue it on the next cycle. */
2459 cost = 1;
2460 else
2461 /* A USE insn, or something else we don't need to
2462 understand. We can't pass these directly to
2463 state_transition because it will trigger a
2464 fatal error for unrecognizable insns. */
2465 cost = 0;
2467 else
2469 cost = state_transition (temp_state, insn);
2471 if (targetm.sched.first_cycle_multipass_dfa_lookahead
2472 && targetm.sched.dfa_bubble)
2474 if (cost == 0)
2476 int j;
2477 rtx bubble;
2479 for (j = 0;
2480 (bubble = (*targetm.sched.dfa_bubble) (j))
2481 != NULL_RTX;
2482 j++)
2484 memcpy (temp_state, curr_state, dfa_state_size);
2486 if (state_transition (temp_state, bubble) < 0
2487 && state_transition (temp_state, insn) < 0)
2488 break;
2491 if (bubble != NULL_RTX)
2493 if (insert_schedule_bubbles_p)
2495 rtx copy;
2497 copy = copy_rtx (PATTERN (bubble));
2498 emit_insn_after (copy, last_scheduled_insn);
2499 last_scheduled_insn
2500 = NEXT_INSN (last_scheduled_insn);
2501 INSN_CODE (last_scheduled_insn)
2502 = INSN_CODE (bubble);
2504 /* Annotate the same for the first insns
2505 scheduling by using mode. */
2506 PUT_MODE (last_scheduled_insn,
2507 (clock_var > last_clock_var
2508 ? clock_var - last_clock_var
2509 : VOIDmode));
2510 last_clock_var = clock_var;
2512 if (sched_verbose >= 2)
2514 fprintf (sched_dump,
2515 ";;\t\t--> scheduling bubble insn <<<%d>>>:reservation ",
2516 INSN_UID (last_scheduled_insn));
2518 if (recog_memoized (last_scheduled_insn)
2519 < 0)
2520 fprintf (sched_dump, "nothing");
2521 else
2522 print_reservation
2523 (sched_dump, last_scheduled_insn);
2525 fprintf (sched_dump, "\n");
2528 cost = -1;
2533 if (cost < 0)
2534 cost = 0;
2535 else if (cost == 0)
2536 cost = 1;
2541 if (cost >= 1)
2543 queue_insn (insn, cost);
2544 continue;
2547 if (! (*current_sched_info->can_schedule_ready_p) (insn))
2548 goto next;
2550 last_scheduled_insn = move_insn (insn, last_scheduled_insn);
2552 if (targetm.sched.use_dfa_pipeline_interface
2553 && (*targetm.sched.use_dfa_pipeline_interface) ())
2555 if (memcmp (curr_state, temp_state, dfa_state_size) != 0)
2556 cycle_issued_insns++;
2557 memcpy (curr_state, temp_state, dfa_state_size);
2560 if (targetm.sched.variable_issue)
2561 can_issue_more =
2562 (*targetm.sched.variable_issue) (sched_dump, sched_verbose,
2563 insn, can_issue_more);
2564 /* A naked CLOBBER or USE generates no instruction, so do
2565 not count them against the issue rate. */
2566 else if (GET_CODE (PATTERN (insn)) != USE
2567 && GET_CODE (PATTERN (insn)) != CLOBBER)
2568 can_issue_more--;
2570 advance = schedule_insn (insn, &ready, clock_var);
2571 if (advance != 0)
2572 break;
2574 next:
2575 first_cycle_insn_p = 0;
2577 /* Sort the ready list based on priority. This must be
2578 redone here, as schedule_insn may have readied additional
2579 insns that will not be sorted correctly. */
2580 if (ready.n_ready > 0)
2581 ready_sort (&ready);
2583 if (targetm.sched.reorder2
2584 && (ready.n_ready == 0
2585 || !SCHED_GROUP_P (ready_element (&ready, 0))))
2587 can_issue_more =
2588 (*targetm.sched.reorder2) (sched_dump, sched_verbose,
2589 ready.n_ready
2590 ? ready_lastpos (&ready) : NULL,
2591 &ready.n_ready, clock_var);
2595 if ((!targetm.sched.use_dfa_pipeline_interface
2596 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2597 && sched_verbose)
2598 /* Debug info. */
2599 visualize_scheduled_insns (clock_var);
2602 if (targetm.sched.md_finish)
2603 (*targetm.sched.md_finish) (sched_dump, sched_verbose);
2605 /* Debug info. */
2606 if (sched_verbose)
2608 fprintf (sched_dump, ";;\tReady list (final): ");
2609 debug_ready_list (&ready);
2610 if (!targetm.sched.use_dfa_pipeline_interface
2611 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2612 print_block_visualization ("");
2615 /* Sanity check -- queue must be empty now. Meaningless if region has
2616 multiple bbs. */
2617 if (current_sched_info->queue_must_finish_empty && q_size != 0)
2618 abort ();
2620 /* Update head/tail boundaries. */
2621 head = NEXT_INSN (prev_head);
2622 tail = last_scheduled_insn;
2624 if (!reload_completed)
2626 rtx insn, link, next;
2628 /* INSN_TICK (minimum clock tick at which the insn becomes
2629 ready) may be not correct for the insn in the subsequent
2630 blocks of the region. We should use a correct value of
2631 `clock_var' or modify INSN_TICK. It is better to keep
2632 clock_var value equal to 0 at the start of a basic block.
2633 Therefore we modify INSN_TICK here. */
2634 for (insn = head; insn != tail; insn = NEXT_INSN (insn))
2635 if (INSN_P (insn))
2637 for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
2639 next = XEXP (link, 0);
2640 INSN_TICK (next) -= clock_var;
2645 /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
2646 previously found among the insns. Insert them at the beginning
2647 of the insns. */
2648 if (note_list != 0)
2650 rtx note_head = note_list;
2652 while (PREV_INSN (note_head))
2654 note_head = PREV_INSN (note_head);
2657 PREV_INSN (note_head) = PREV_INSN (head);
2658 NEXT_INSN (PREV_INSN (head)) = note_head;
2659 PREV_INSN (head) = note_list;
2660 NEXT_INSN (note_list) = head;
2661 head = note_head;
2664 /* Debugging. */
2665 if (sched_verbose)
2667 fprintf (sched_dump, ";; total time = %d\n;; new head = %d\n",
2668 clock_var, INSN_UID (head));
2669 fprintf (sched_dump, ";; new tail = %d\n\n",
2670 INSN_UID (tail));
2671 visualize_free ();
2674 current_sched_info->head = head;
2675 current_sched_info->tail = tail;
2677 free (ready.vec);
2679 if (targetm.sched.use_dfa_pipeline_interface
2680 && (*targetm.sched.use_dfa_pipeline_interface) ())
2682 free (ready_try);
2683 for (i = 0; i <= rgn_n_insns; i++)
2684 free (choice_stack [i].state);
2685 free (choice_stack);
2689 /* Set_priorities: compute priority of each insn in the block. */
2692 set_priorities (rtx head, rtx tail)
2694 rtx insn;
2695 int n_insn;
2696 int sched_max_insns_priority =
2697 current_sched_info->sched_max_insns_priority;
2698 rtx prev_head;
2700 prev_head = PREV_INSN (head);
2702 if (head == tail && (! INSN_P (head)))
2703 return 0;
2705 n_insn = 0;
2706 sched_max_insns_priority = 0;
2707 for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
2709 if (GET_CODE (insn) == NOTE)
2710 continue;
2712 n_insn++;
2713 (void) priority (insn);
2715 if (INSN_PRIORITY_KNOWN (insn))
2716 sched_max_insns_priority =
2717 MAX (sched_max_insns_priority, INSN_PRIORITY (insn));
2719 sched_max_insns_priority += 1;
2720 current_sched_info->sched_max_insns_priority =
2721 sched_max_insns_priority;
2723 return n_insn;
2726 /* Initialize some global state for the scheduler. DUMP_FILE is to be used
2727 for debugging output. */
2729 void
2730 sched_init (FILE *dump_file)
2732 int luid;
2733 basic_block b;
2734 rtx insn;
2735 int i;
2737 /* Disable speculative loads in their presence if cc0 defined. */
2738 #ifdef HAVE_cc0
2739 flag_schedule_speculative_load = 0;
2740 #endif
2742 /* Set dump and sched_verbose for the desired debugging output. If no
2743 dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
2744 For -fsched-verbose=N, N>=10, print everything to stderr. */
2745 sched_verbose = sched_verbose_param;
2746 if (sched_verbose_param == 0 && dump_file)
2747 sched_verbose = 1;
2748 sched_dump = ((sched_verbose_param >= 10 || !dump_file)
2749 ? stderr : dump_file);
2751 /* Initialize issue_rate. */
2752 if (targetm.sched.issue_rate)
2753 issue_rate = (*targetm.sched.issue_rate) ();
2754 else
2755 issue_rate = 1;
2757 if (cached_issue_rate != issue_rate)
2759 cached_issue_rate = issue_rate;
2760 /* To invalidate max_lookahead_tries: */
2761 cached_first_cycle_multipass_dfa_lookahead = 0;
2764 /* We use LUID 0 for the fake insn (UID 0) which holds dependencies for
2765 pseudos which do not cross calls. */
2766 old_max_uid = get_max_uid () + 1;
2768 h_i_d = xcalloc (old_max_uid, sizeof (*h_i_d));
2770 for (i = 0; i < old_max_uid; i++)
2771 h_i_d [i].cost = -1;
2773 if (targetm.sched.use_dfa_pipeline_interface
2774 && (*targetm.sched.use_dfa_pipeline_interface) ())
2776 if (targetm.sched.init_dfa_pre_cycle_insn)
2777 (*targetm.sched.init_dfa_pre_cycle_insn) ();
2779 if (targetm.sched.init_dfa_post_cycle_insn)
2780 (*targetm.sched.init_dfa_post_cycle_insn) ();
2782 if (targetm.sched.first_cycle_multipass_dfa_lookahead
2783 && targetm.sched.init_dfa_bubbles)
2784 (*targetm.sched.init_dfa_bubbles) ();
2786 dfa_start ();
2787 dfa_state_size = state_size ();
2788 curr_state = xmalloc (dfa_state_size);
2791 h_i_d[0].luid = 0;
2792 luid = 1;
2793 FOR_EACH_BB (b)
2794 for (insn = b->head;; insn = NEXT_INSN (insn))
2796 INSN_LUID (insn) = luid;
2798 /* Increment the next luid, unless this is a note. We don't
2799 really need separate IDs for notes and we don't want to
2800 schedule differently depending on whether or not there are
2801 line-number notes, i.e., depending on whether or not we're
2802 generating debugging information. */
2803 if (GET_CODE (insn) != NOTE)
2804 ++luid;
2806 if (insn == b->end)
2807 break;
2810 init_dependency_caches (luid);
2812 init_alias_analysis ();
2814 if (write_symbols != NO_DEBUG)
2816 rtx line;
2818 line_note_head = xcalloc (last_basic_block, sizeof (rtx));
2820 /* Save-line-note-head:
2821 Determine the line-number at the start of each basic block.
2822 This must be computed and saved now, because after a basic block's
2823 predecessor has been scheduled, it is impossible to accurately
2824 determine the correct line number for the first insn of the block. */
2826 FOR_EACH_BB (b)
2828 for (line = b->head; line; line = PREV_INSN (line))
2829 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
2831 line_note_head[b->index] = line;
2832 break;
2834 /* Do a forward search as well, since we won't get to see the first
2835 notes in a basic block. */
2836 for (line = b->head; line; line = NEXT_INSN (line))
2838 if (INSN_P (line))
2839 break;
2840 if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
2841 line_note_head[b->index] = line;
2846 if ((!targetm.sched.use_dfa_pipeline_interface
2847 || !(*targetm.sched.use_dfa_pipeline_interface) ())
2848 && sched_verbose)
2849 /* Find units used in this function, for visualization. */
2850 init_target_units ();
2852 /* ??? Add a NOTE after the last insn of the last basic block. It is not
2853 known why this is done. */
2855 insn = EXIT_BLOCK_PTR->prev_bb->end;
2856 if (NEXT_INSN (insn) == 0
2857 || (GET_CODE (insn) != NOTE
2858 && GET_CODE (insn) != CODE_LABEL
2859 /* Don't emit a NOTE if it would end up before a BARRIER. */
2860 && GET_CODE (NEXT_INSN (insn)) != BARRIER))
2862 emit_note_after (NOTE_INSN_DELETED, EXIT_BLOCK_PTR->prev_bb->end);
2863 /* Make insn to appear outside BB. */
2864 EXIT_BLOCK_PTR->prev_bb->end = PREV_INSN (EXIT_BLOCK_PTR->prev_bb->end);
2867 /* Compute INSN_REG_WEIGHT for all blocks. We must do this before
2868 removing death notes. */
2869 FOR_EACH_BB_REVERSE (b)
2870 find_insn_reg_weight (b->index);
2873 /* Free global data used during insn scheduling. */
2875 void
2876 sched_finish (void)
2878 free (h_i_d);
2880 if (targetm.sched.use_dfa_pipeline_interface
2881 && (*targetm.sched.use_dfa_pipeline_interface) ())
2883 free (curr_state);
2884 dfa_finish ();
2886 free_dependency_caches ();
2887 end_alias_analysis ();
2888 if (write_symbols != NO_DEBUG)
2889 free (line_note_head);
2891 #endif /* INSN_SCHEDULING */