1 /* Instruction scheduling pass.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
6 and currently maintained by, Jim Wilson (wilson@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
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
57 The following list shows the order in which we want to break ties
58 among insns in the ready list:
60 1. choose insn with the longest path to end of bb, ties
62 2. choose insn with least contribution to register pressure,
64 3. prefer in-block upon interblock motion, ties broken by
65 4. prefer useful upon speculative motion, ties broken by
66 5. choose insn with largest control flow probability, ties
68 6. choose insn with the least dependences upon the previously
69 scheduled insn, or finally
70 7 choose the insn which has the most insns dependent on it.
71 8. choose insn with lowest UID.
73 Memory references complicate matters. Only if we can be certain
74 that memory references are not part of the data dependency graph
75 (via true, anti, or output dependence), can we move operations past
76 memory references. To first approximation, reads can be done
77 independently, while writes introduce dependencies. Better
78 approximations will yield fewer dependencies.
80 Before reload, an extended analysis of interblock data dependences
81 is required for interblock scheduling. This is performed in
82 compute_block_backward_dependences ().
84 Dependencies set up by memory references are treated in exactly the
85 same way as other dependencies, by using insn backward dependences
86 INSN_BACK_DEPS. INSN_BACK_DEPS are translated into forward dependences
87 INSN_FORW_DEPS the purpose of forward list scheduling.
89 Having optimized the critical path, we may have also unduly
90 extended the lifetimes of some registers. If an operation requires
91 that constants be loaded into registers, it is certainly desirable
92 to load those constants as early as necessary, but no earlier.
93 I.e., it will not do to load up a bunch of registers at the
94 beginning of a basic block only to use them at the end, if they
95 could be loaded later, since this may result in excessive register
98 Note that since branches are never in basic blocks, but only end
99 basic blocks, this pass will not move branches. But that is ok,
100 since we can use GNU's delayed branch scheduling pass to take care
103 Also note that no further optimizations based on algebraic
104 identities are performed, so this pass would be a good one to
105 perform instruction splitting, such as breaking up a multiply
106 instruction into shifts and adds where that is profitable.
108 Given the memory aliasing analysis that this pass should perform,
109 it should be possible to remove redundant stores to memory, and to
110 load values from registers instead of hitting memory.
112 Before reload, speculative insns are moved only if a 'proof' exists
113 that no exception will be caused by this, and if no live registers
114 exist that inhibit the motion (live registers constraints are not
115 represented by data dependence edges).
117 This pass must update information that subsequent passes expect to
118 be correct. Namely: reg_n_refs, reg_n_sets, reg_n_deaths,
119 reg_n_calls_crossed, and reg_live_length. Also, BB_HEAD, BB_END.
121 The information in the line number notes is carefully retained by
122 this pass. Notes that refer to the starting and ending of
123 exception regions are also carefully retained by this pass. All
124 other NOTE insns are grouped in their same relative order at the
125 beginning of basic blocks and regions that have been scheduled. */
129 #include "coretypes.h"
131 #include "diagnostic-core.h"
132 #include "hard-reg-set.h"
136 #include "function.h"
138 #include "insn-config.h"
139 #include "insn-attr.h"
142 #include "sched-int.h"
144 #include "common/common-target.h"
151 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
154 #ifdef INSN_SCHEDULING
156 /* issue_rate is the number of insns that can be scheduled in the same
157 machine cycle. It can be defined in the config/mach/mach.h file,
158 otherwise we set it to 1. */
162 /* This can be set to true by a backend if the scheduler should not
163 enable a DCE pass. */
166 /* The current initiation interval used when modulo scheduling. */
167 static int modulo_ii;
169 /* The maximum number of stages we are prepared to handle. */
170 static int modulo_max_stages;
172 /* The number of insns that exist in each iteration of the loop. We use this
173 to detect when we've scheduled all insns from the first iteration. */
174 static int modulo_n_insns;
176 /* The current count of insns in the first iteration of the loop that have
177 already been scheduled. */
178 static int modulo_insns_scheduled;
180 /* The maximum uid of insns from the first iteration of the loop. */
181 static int modulo_iter0_max_uid;
183 /* The number of times we should attempt to backtrack when modulo scheduling.
184 Decreased each time we have to backtrack. */
185 static int modulo_backtracks_left;
187 /* The stage in which the last insn from the original loop was
189 static int modulo_last_stage;
191 /* sched-verbose controls the amount of debugging output the
192 scheduler prints. It is controlled by -fsched-verbose=N:
193 N>0 and no -DSR : the output is directed to stderr.
194 N>=10 will direct the printouts to stderr (regardless of -dSR).
196 N=2: bb's probabilities, detailed ready list info, unit/insn info.
197 N=3: rtl at abort point, control-flow, regions info.
198 N=5: dependences info. */
200 int sched_verbose = 0;
202 /* Debugging file. All printouts are sent to dump, which is always set,
203 either to stderr, or to the dump listing file (-dRS). */
204 FILE *sched_dump = 0;
206 /* This is a placeholder for the scheduler parameters common
207 to all schedulers. */
208 struct common_sched_info_def *common_sched_info;
210 #define INSN_TICK(INSN) (HID (INSN)->tick)
211 #define INSN_EXACT_TICK(INSN) (HID (INSN)->exact_tick)
212 #define INSN_TICK_ESTIMATE(INSN) (HID (INSN)->tick_estimate)
213 #define INTER_TICK(INSN) (HID (INSN)->inter_tick)
214 #define FEEDS_BACKTRACK_INSN(INSN) (HID (INSN)->feeds_backtrack_insn)
215 #define SHADOW_P(INSN) (HID (INSN)->shadow_p)
216 #define MUST_RECOMPUTE_SPEC_P(INSN) (HID (INSN)->must_recompute_spec)
218 /* If INSN_TICK of an instruction is equal to INVALID_TICK,
219 then it should be recalculated from scratch. */
220 #define INVALID_TICK (-(max_insn_queue_index + 1))
221 /* The minimal value of the INSN_TICK of an instruction. */
222 #define MIN_TICK (-max_insn_queue_index)
224 /* List of important notes we must keep around. This is a pointer to the
225 last element in the list. */
228 static struct spec_info_def spec_info_var;
229 /* Description of the speculative part of the scheduling.
230 If NULL - no speculation. */
231 spec_info_t spec_info = NULL;
233 /* True, if recovery block was added during scheduling of current block.
234 Used to determine, if we need to fix INSN_TICKs. */
235 static bool haifa_recovery_bb_recently_added_p;
237 /* True, if recovery block was added during this scheduling pass.
238 Used to determine if we should have empty memory pools of dependencies
239 after finishing current region. */
240 bool haifa_recovery_bb_ever_added_p;
242 /* Counters of different types of speculative instructions. */
243 static int nr_begin_data, nr_be_in_data, nr_begin_control, nr_be_in_control;
245 /* Array used in {unlink, restore}_bb_notes. */
246 static rtx *bb_header = 0;
248 /* Basic block after which recovery blocks will be created. */
249 static basic_block before_recovery;
251 /* Basic block just before the EXIT_BLOCK and after recovery, if we have
253 basic_block after_recovery;
255 /* FALSE if we add bb to another region, so we don't need to initialize it. */
256 bool adding_bb_to_current_region_p = true;
260 /* An instruction is ready to be scheduled when all insns preceding it
261 have already been scheduled. It is important to ensure that all
262 insns which use its result will not be executed until its result
263 has been computed. An insn is maintained in one of four structures:
265 (P) the "Pending" set of insns which cannot be scheduled until
266 their dependencies have been satisfied.
267 (Q) the "Queued" set of insns that can be scheduled when sufficient
269 (R) the "Ready" list of unscheduled, uncommitted insns.
270 (S) the "Scheduled" list of insns.
272 Initially, all insns are either "Pending" or "Ready" depending on
273 whether their dependencies are satisfied.
275 Insns move from the "Ready" list to the "Scheduled" list as they
276 are committed to the schedule. As this occurs, the insns in the
277 "Pending" list have their dependencies satisfied and move to either
278 the "Ready" list or the "Queued" set depending on whether
279 sufficient time has passed to make them ready. As time passes,
280 insns move from the "Queued" set to the "Ready" list.
282 The "Pending" list (P) are the insns in the INSN_FORW_DEPS of the
283 unscheduled insns, i.e., those that are ready, queued, and pending.
284 The "Queued" set (Q) is implemented by the variable `insn_queue'.
285 The "Ready" list (R) is implemented by the variables `ready' and
287 The "Scheduled" list (S) is the new insn chain built by this pass.
289 The transition (R->S) is implemented in the scheduling loop in
290 `schedule_block' when the best insn to schedule is chosen.
291 The transitions (P->R and P->Q) are implemented in `schedule_insn' as
292 insns move from the ready list to the scheduled list.
293 The transition (Q->R) is implemented in 'queue_to_insn' as time
294 passes or stalls are introduced. */
296 /* Implement a circular buffer to delay instructions until sufficient
297 time has passed. For the new pipeline description interface,
298 MAX_INSN_QUEUE_INDEX is a power of two minus one which is not less
299 than maximal time of instruction execution computed by genattr.c on
300 the base maximal time of functional unit reservations and getting a
301 result. This is the longest time an insn may be queued. */
303 static rtx *insn_queue;
304 static int q_ptr = 0;
305 static int q_size = 0;
306 #define NEXT_Q(X) (((X)+1) & max_insn_queue_index)
307 #define NEXT_Q_AFTER(X, C) (((X)+C) & max_insn_queue_index)
309 #define QUEUE_SCHEDULED (-3)
310 #define QUEUE_NOWHERE (-2)
311 #define QUEUE_READY (-1)
312 /* QUEUE_SCHEDULED - INSN is scheduled.
313 QUEUE_NOWHERE - INSN isn't scheduled yet and is neither in
315 QUEUE_READY - INSN is in ready list.
316 N >= 0 - INSN queued for X [where NEXT_Q_AFTER (q_ptr, X) == N] cycles. */
318 #define QUEUE_INDEX(INSN) (HID (INSN)->queue_index)
320 /* The following variable value refers for all current and future
321 reservations of the processor units. */
324 /* The following variable value is size of memory representing all
325 current and future reservations of the processor units. */
326 size_t dfa_state_size;
328 /* The following array is used to find the best insn from ready when
329 the automaton pipeline interface is used. */
330 char *ready_try = NULL;
332 /* The ready list. */
333 struct ready_list ready = {NULL, 0, 0, 0, 0};
335 /* The pointer to the ready list (to be removed). */
336 static struct ready_list *readyp = &ready;
338 /* Scheduling clock. */
339 static int clock_var;
341 /* Clock at which the previous instruction was issued. */
342 static int last_clock_var;
344 /* Set to true if, when queuing a shadow insn, we discover that it would be
345 scheduled too late. */
346 static bool must_backtrack;
348 /* The following variable value is number of essential insns issued on
349 the current cycle. An insn is essential one if it changes the
351 int cycle_issued_insns;
353 /* This records the actual schedule. It is built up during the main phase
354 of schedule_block, and afterwards used to reorder the insns in the RTL. */
355 static VEC(rtx, heap) *scheduled_insns;
357 static int may_trap_exp (const_rtx, int);
359 /* Nonzero iff the address is comprised from at most 1 register. */
360 #define CONST_BASED_ADDRESS_P(x) \
362 || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS \
363 || (GET_CODE (x) == LO_SUM)) \
364 && (CONSTANT_P (XEXP (x, 0)) \
365 || CONSTANT_P (XEXP (x, 1)))))
367 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
368 as found by analyzing insn's expression. */
371 static int haifa_luid_for_non_insn (rtx x);
373 /* Haifa version of sched_info hooks common to all headers. */
374 const struct common_sched_info_def haifa_common_sched_info =
376 NULL, /* fix_recovery_cfg */
377 NULL, /* add_block */
378 NULL, /* estimate_number_of_insns */
379 haifa_luid_for_non_insn, /* luid_for_non_insn */
380 SCHED_PASS_UNKNOWN /* sched_pass_id */
383 /* Mapping from instruction UID to its Logical UID. */
384 VEC (int, heap) *sched_luids = NULL;
386 /* Next LUID to assign to an instruction. */
387 int sched_max_luid = 1;
389 /* Haifa Instruction Data. */
390 VEC (haifa_insn_data_def, heap) *h_i_d = NULL;
392 void (* sched_init_only_bb) (basic_block, basic_block);
394 /* Split block function. Different schedulers might use different functions
395 to handle their internal data consistent. */
396 basic_block (* sched_split_block) (basic_block, rtx);
398 /* Create empty basic block after the specified block. */
399 basic_block (* sched_create_empty_bb) (basic_block);
402 may_trap_exp (const_rtx x, int is_store)
411 if (code == MEM && may_trap_p (x))
418 /* The insn uses memory: a volatile load. */
419 if (MEM_VOLATILE_P (x))
421 /* An exception-free load. */
424 /* A load with 1 base register, to be further checked. */
425 if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
426 return PFREE_CANDIDATE;
427 /* No info on the load, to be further checked. */
428 return PRISKY_CANDIDATE;
433 int i, insn_class = TRAP_FREE;
435 /* Neither store nor load, check if it may cause a trap. */
438 /* Recursive step: walk the insn... */
439 fmt = GET_RTX_FORMAT (code);
440 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
444 int tmp_class = may_trap_exp (XEXP (x, i), is_store);
445 insn_class = WORST_CLASS (insn_class, tmp_class);
447 else if (fmt[i] == 'E')
450 for (j = 0; j < XVECLEN (x, i); j++)
452 int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
453 insn_class = WORST_CLASS (insn_class, tmp_class);
454 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
458 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
465 /* Classifies rtx X of an insn for the purpose of verifying that X can be
466 executed speculatively (and consequently the insn can be moved
467 speculatively), by examining X, returning:
468 TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
469 TRAP_FREE: non-load insn.
470 IFREE: load from a globally safe location.
471 IRISKY: volatile load.
472 PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
473 being either PFREE or PRISKY. */
476 haifa_classify_rtx (const_rtx x)
478 int tmp_class = TRAP_FREE;
479 int insn_class = TRAP_FREE;
482 if (GET_CODE (x) == PARALLEL)
484 int i, len = XVECLEN (x, 0);
486 for (i = len - 1; i >= 0; i--)
488 tmp_class = haifa_classify_rtx (XVECEXP (x, 0, i));
489 insn_class = WORST_CLASS (insn_class, tmp_class);
490 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
500 /* Test if it is a 'store'. */
501 tmp_class = may_trap_exp (XEXP (x, 0), 1);
504 /* Test if it is a store. */
505 tmp_class = may_trap_exp (SET_DEST (x), 1);
506 if (tmp_class == TRAP_RISKY)
508 /* Test if it is a load. */
510 WORST_CLASS (tmp_class,
511 may_trap_exp (SET_SRC (x), 0));
514 tmp_class = haifa_classify_rtx (COND_EXEC_CODE (x));
515 if (tmp_class == TRAP_RISKY)
517 tmp_class = WORST_CLASS (tmp_class,
518 may_trap_exp (COND_EXEC_TEST (x), 0));
521 tmp_class = TRAP_RISKY;
525 insn_class = tmp_class;
532 haifa_classify_insn (const_rtx insn)
534 return haifa_classify_rtx (PATTERN (insn));
537 /* After the scheduler initialization function has been called, this function
538 can be called to enable modulo scheduling. II is the initiation interval
539 we should use, it affects the delays for delay_pairs that were recorded as
540 separated by a given number of stages.
542 MAX_STAGES provides us with a limit
543 after which we give up scheduling; the caller must have unrolled at least
544 as many copies of the loop body and recorded delay_pairs for them.
546 INSNS is the number of real (non-debug) insns in one iteration of
547 the loop. MAX_UID can be used to test whether an insn belongs to
548 the first iteration of the loop; all of them have a uid lower than
551 set_modulo_params (int ii, int max_stages, int insns, int max_uid)
554 modulo_max_stages = max_stages;
555 modulo_n_insns = insns;
556 modulo_iter0_max_uid = max_uid;
557 modulo_backtracks_left = PARAM_VALUE (PARAM_MAX_MODULO_BACKTRACK_ATTEMPTS);
560 /* A structure to record a pair of insns where the first one is a real
561 insn that has delay slots, and the second is its delayed shadow.
562 I1 is scheduled normally and will emit an assembly instruction,
563 while I2 describes the side effect that takes place at the
564 transition between cycles CYCLES and (CYCLES + 1) after I1. */
567 struct delay_pair *next_same_i1;
570 /* When doing modulo scheduling, we a delay_pair can also be used to
571 show that I1 and I2 are the same insn in a different stage. If that
572 is the case, STAGES will be nonzero. */
576 /* Two hash tables to record delay_pairs, one indexed by I1 and the other
578 static htab_t delay_htab;
579 static htab_t delay_htab_i2;
581 /* Called through htab_traverse. Walk the hashtable using I2 as
582 index, and delete all elements involving an UID higher than
583 that pointed to by *DATA. */
585 htab_i2_traverse (void **slot, void *data)
587 int maxuid = *(int *)data;
588 struct delay_pair *p = *(struct delay_pair **)slot;
589 if (INSN_UID (p->i2) >= maxuid || INSN_UID (p->i1) >= maxuid)
591 htab_clear_slot (delay_htab_i2, slot);
596 /* Called through htab_traverse. Walk the hashtable using I2 as
597 index, and delete all elements involving an UID higher than
598 that pointed to by *DATA. */
600 htab_i1_traverse (void **slot, void *data)
602 int maxuid = *(int *)data;
603 struct delay_pair **pslot = (struct delay_pair **)slot;
604 struct delay_pair *p, *first, **pprev;
606 if (INSN_UID ((*pslot)->i1) >= maxuid)
608 htab_clear_slot (delay_htab, slot);
612 for (p = *pslot; p; p = p->next_same_i1)
614 if (INSN_UID (p->i2) < maxuid)
617 pprev = &p->next_same_i1;
622 htab_clear_slot (delay_htab, slot);
628 /* Discard all delay pairs which involve an insn with an UID higher
631 discard_delay_pairs_above (int max_uid)
633 htab_traverse (delay_htab, htab_i1_traverse, &max_uid);
634 htab_traverse (delay_htab_i2, htab_i2_traverse, &max_uid);
637 /* Returns a hash value for X (which really is a delay_pair), based on
640 delay_hash_i1 (const void *x)
642 return htab_hash_pointer (((const struct delay_pair *) x)->i1);
645 /* Returns a hash value for X (which really is a delay_pair), based on
648 delay_hash_i2 (const void *x)
650 return htab_hash_pointer (((const struct delay_pair *) x)->i2);
653 /* Return nonzero if I1 of pair X is the same as that of pair Y. */
655 delay_i1_eq (const void *x, const void *y)
657 return ((const struct delay_pair *) x)->i1 == y;
660 /* Return nonzero if I2 of pair X is the same as that of pair Y. */
662 delay_i2_eq (const void *x, const void *y)
664 return ((const struct delay_pair *) x)->i2 == y;
667 /* This function can be called by a port just before it starts the final
668 scheduling pass. It records the fact that an instruction with delay
669 slots has been split into two insns, I1 and I2. The first one will be
670 scheduled normally and initiates the operation. The second one is a
671 shadow which must follow a specific number of cycles after I1; its only
672 purpose is to show the side effect that occurs at that cycle in the RTL.
673 If a JUMP_INSN or a CALL_INSN has been split, I1 should be a normal INSN,
674 while I2 retains the original insn type.
676 There are two ways in which the number of cycles can be specified,
677 involving the CYCLES and STAGES arguments to this function. If STAGES
678 is zero, we just use the value of CYCLES. Otherwise, STAGES is a factor
679 which is multiplied by MODULO_II to give the number of cycles. This is
680 only useful if the caller also calls set_modulo_params to enable modulo
684 record_delay_slot_pair (rtx i1, rtx i2, int cycles, int stages)
686 struct delay_pair *p = XNEW (struct delay_pair);
687 struct delay_pair **slot;
696 delay_htab = htab_create (10, delay_hash_i1, delay_i1_eq, NULL);
697 delay_htab_i2 = htab_create (10, delay_hash_i2, delay_i2_eq, free);
699 slot = ((struct delay_pair **)
700 htab_find_slot_with_hash (delay_htab, i1, htab_hash_pointer (i1),
702 p->next_same_i1 = *slot;
704 slot = ((struct delay_pair **)
705 htab_find_slot_with_hash (delay_htab_i2, i2, htab_hash_pointer (i2),
710 /* Examine the delay pair hashtable to see if INSN is a shadow for another,
711 and return the other insn if so. Return NULL otherwise. */
713 real_insn_for_shadow (rtx insn)
715 struct delay_pair *pair;
717 if (delay_htab == NULL)
721 = (struct delay_pair *)htab_find_with_hash (delay_htab_i2, insn,
722 htab_hash_pointer (insn));
723 if (!pair || pair->stages > 0)
728 /* For a pair P of insns, return the fixed distance in cycles from the first
729 insn after which the second must be scheduled. */
731 pair_delay (struct delay_pair *p)
736 return p->stages * modulo_ii;
739 /* Given an insn INSN, add a dependence on its delayed shadow if it
740 has one. Also try to find situations where shadows depend on each other
741 and add dependencies to the real insns to limit the amount of backtracking
744 add_delay_dependencies (rtx insn)
746 struct delay_pair *pair;
747 sd_iterator_def sd_it;
754 = (struct delay_pair *)htab_find_with_hash (delay_htab_i2, insn,
755 htab_hash_pointer (insn));
758 add_dependence (insn, pair->i1, REG_DEP_ANTI);
762 FOR_EACH_DEP (pair->i2, SD_LIST_BACK, sd_it, dep)
764 rtx pro = DEP_PRO (dep);
765 struct delay_pair *other_pair
766 = (struct delay_pair *)htab_find_with_hash (delay_htab_i2, pro,
767 htab_hash_pointer (pro));
768 if (!other_pair || other_pair->stages)
770 if (pair_delay (other_pair) >= pair_delay (pair))
772 if (sched_verbose >= 4)
774 fprintf (sched_dump, ";;\tadding dependence %d <- %d\n",
775 INSN_UID (other_pair->i1),
776 INSN_UID (pair->i1));
777 fprintf (sched_dump, ";;\tpair1 %d <- %d, cost %d\n",
781 fprintf (sched_dump, ";;\tpair2 %d <- %d, cost %d\n",
782 INSN_UID (other_pair->i1),
783 INSN_UID (other_pair->i2),
784 pair_delay (other_pair));
786 add_dependence (pair->i1, other_pair->i1, REG_DEP_ANTI);
791 /* Forward declarations. */
793 static int priority (rtx);
794 static int rank_for_schedule (const void *, const void *);
795 static void swap_sort (rtx *, int);
796 static void queue_insn (rtx, int, const char *);
797 static int schedule_insn (rtx);
798 static void adjust_priority (rtx);
799 static void advance_one_cycle (void);
800 static void extend_h_i_d (void);
803 /* Notes handling mechanism:
804 =========================
805 Generally, NOTES are saved before scheduling and restored after scheduling.
806 The scheduler distinguishes between two types of notes:
808 (1) LOOP_BEGIN, LOOP_END, SETJMP, EHREGION_BEG, EHREGION_END notes:
809 Before scheduling a region, a pointer to the note is added to the insn
810 that follows or precedes it. (This happens as part of the data dependence
811 computation). After scheduling an insn, the pointer contained in it is
812 used for regenerating the corresponding note (in reemit_notes).
814 (2) All other notes (e.g. INSN_DELETED): Before scheduling a block,
815 these notes are put in a list (in rm_other_notes() and
816 unlink_other_notes ()). After scheduling the block, these notes are
817 inserted at the beginning of the block (in schedule_block()). */
819 static void ready_add (struct ready_list *, rtx, bool);
820 static rtx ready_remove_first (struct ready_list *);
821 static rtx ready_remove_first_dispatch (struct ready_list *ready);
823 static void queue_to_ready (struct ready_list *);
824 static int early_queue_to_ready (state_t, struct ready_list *);
826 static void debug_ready_list (struct ready_list *);
828 /* The following functions are used to implement multi-pass scheduling
829 on the first cycle. */
830 static rtx ready_remove (struct ready_list *, int);
831 static void ready_remove_insn (rtx);
833 static void fix_inter_tick (rtx, rtx);
834 static int fix_tick_ready (rtx);
835 static void change_queue_index (rtx, int);
837 /* The following functions are used to implement scheduling of data/control
838 speculative instructions. */
840 static void extend_h_i_d (void);
841 static void init_h_i_d (rtx);
842 static int haifa_speculate_insn (rtx, ds_t, rtx *);
843 static void generate_recovery_code (rtx);
844 static void process_insn_forw_deps_be_in_spec (rtx, rtx, ds_t);
845 static void begin_speculative_block (rtx);
846 static void add_to_speculative_block (rtx);
847 static void init_before_recovery (basic_block *);
848 static void create_check_block_twin (rtx, bool);
849 static void fix_recovery_deps (basic_block);
850 static bool haifa_change_pattern (rtx, rtx);
851 static void dump_new_block_header (int, basic_block, rtx, rtx);
852 static void restore_bb_notes (basic_block);
853 static void fix_jump_move (rtx);
854 static void move_block_after_check (rtx);
855 static void move_succs (VEC(edge,gc) **, basic_block);
856 static void sched_remove_insn (rtx);
857 static void clear_priorities (rtx, rtx_vec_t *);
858 static void calc_priorities (rtx_vec_t);
859 static void add_jump_dependencies (rtx, rtx);
861 #endif /* INSN_SCHEDULING */
863 /* Point to state used for the current scheduling pass. */
864 struct haifa_sched_info *current_sched_info;
866 #ifndef INSN_SCHEDULING
868 schedule_insns (void)
873 /* Do register pressure sensitive insn scheduling if the flag is set
875 bool sched_pressure_p;
877 /* Map regno -> its pressure class. The map defined only when
878 SCHED_PRESSURE_P is true. */
879 enum reg_class *sched_regno_pressure_class;
881 /* The current register pressure. Only elements corresponding pressure
882 classes are defined. */
883 static int curr_reg_pressure[N_REG_CLASSES];
885 /* Saved value of the previous array. */
886 static int saved_reg_pressure[N_REG_CLASSES];
888 /* Register living at given scheduling point. */
889 static bitmap curr_reg_live;
891 /* Saved value of the previous array. */
892 static bitmap saved_reg_live;
894 /* Registers mentioned in the current region. */
895 static bitmap region_ref_regs;
897 /* Initiate register pressure relative info for scheduling the current
898 region. Currently it is only clearing register mentioned in the
901 sched_init_region_reg_pressure_info (void)
903 bitmap_clear (region_ref_regs);
906 /* Update current register pressure related info after birth (if
907 BIRTH_P) or death of register REGNO. */
909 mark_regno_birth_or_death (int regno, bool birth_p)
911 enum reg_class pressure_class;
913 pressure_class = sched_regno_pressure_class[regno];
914 if (regno >= FIRST_PSEUDO_REGISTER)
916 if (pressure_class != NO_REGS)
920 bitmap_set_bit (curr_reg_live, regno);
921 curr_reg_pressure[pressure_class]
922 += (ira_reg_class_max_nregs
923 [pressure_class][PSEUDO_REGNO_MODE (regno)]);
927 bitmap_clear_bit (curr_reg_live, regno);
928 curr_reg_pressure[pressure_class]
929 -= (ira_reg_class_max_nregs
930 [pressure_class][PSEUDO_REGNO_MODE (regno)]);
934 else if (pressure_class != NO_REGS
935 && ! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
939 bitmap_set_bit (curr_reg_live, regno);
940 curr_reg_pressure[pressure_class]++;
944 bitmap_clear_bit (curr_reg_live, regno);
945 curr_reg_pressure[pressure_class]--;
950 /* Initiate current register pressure related info from living
951 registers given by LIVE. */
953 initiate_reg_pressure_info (bitmap live)
959 for (i = 0; i < ira_pressure_classes_num; i++)
960 curr_reg_pressure[ira_pressure_classes[i]] = 0;
961 bitmap_clear (curr_reg_live);
962 EXECUTE_IF_SET_IN_BITMAP (live, 0, j, bi)
963 if (current_nr_blocks == 1 || bitmap_bit_p (region_ref_regs, j))
964 mark_regno_birth_or_death (j, true);
967 /* Mark registers in X as mentioned in the current region. */
969 setup_ref_regs (rtx x)
972 const RTX_CODE code = GET_CODE (x);
978 if (HARD_REGISTER_NUM_P (regno))
979 bitmap_set_range (region_ref_regs, regno,
980 hard_regno_nregs[regno][GET_MODE (x)]);
982 bitmap_set_bit (region_ref_regs, REGNO (x));
985 fmt = GET_RTX_FORMAT (code);
986 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
988 setup_ref_regs (XEXP (x, i));
989 else if (fmt[i] == 'E')
991 for (j = 0; j < XVECLEN (x, i); j++)
992 setup_ref_regs (XVECEXP (x, i, j));
996 /* Initiate current register pressure related info at the start of
999 initiate_bb_reg_pressure_info (basic_block bb)
1001 unsigned int i ATTRIBUTE_UNUSED;
1004 if (current_nr_blocks > 1)
1005 FOR_BB_INSNS (bb, insn)
1006 if (NONDEBUG_INSN_P (insn))
1007 setup_ref_regs (PATTERN (insn));
1008 initiate_reg_pressure_info (df_get_live_in (bb));
1009 #ifdef EH_RETURN_DATA_REGNO
1010 if (bb_has_eh_pred (bb))
1013 unsigned int regno = EH_RETURN_DATA_REGNO (i);
1015 if (regno == INVALID_REGNUM)
1017 if (! bitmap_bit_p (df_get_live_in (bb), regno))
1018 mark_regno_birth_or_death (regno, true);
1023 /* Save current register pressure related info. */
1025 save_reg_pressure (void)
1029 for (i = 0; i < ira_pressure_classes_num; i++)
1030 saved_reg_pressure[ira_pressure_classes[i]]
1031 = curr_reg_pressure[ira_pressure_classes[i]];
1032 bitmap_copy (saved_reg_live, curr_reg_live);
1035 /* Restore saved register pressure related info. */
1037 restore_reg_pressure (void)
1041 for (i = 0; i < ira_pressure_classes_num; i++)
1042 curr_reg_pressure[ira_pressure_classes[i]]
1043 = saved_reg_pressure[ira_pressure_classes[i]];
1044 bitmap_copy (curr_reg_live, saved_reg_live);
1047 /* Return TRUE if the register is dying after its USE. */
1049 dying_use_p (struct reg_use_data *use)
1051 struct reg_use_data *next;
1053 for (next = use->next_regno_use; next != use; next = next->next_regno_use)
1054 if (NONDEBUG_INSN_P (next->insn)
1055 && QUEUE_INDEX (next->insn) != QUEUE_SCHEDULED)
1060 /* Print info about the current register pressure and its excess for
1061 each pressure class. */
1063 print_curr_reg_pressure (void)
1068 fprintf (sched_dump, ";;\t");
1069 for (i = 0; i < ira_pressure_classes_num; i++)
1071 cl = ira_pressure_classes[i];
1072 gcc_assert (curr_reg_pressure[cl] >= 0);
1073 fprintf (sched_dump, " %s:%d(%d)", reg_class_names[cl],
1074 curr_reg_pressure[cl],
1075 curr_reg_pressure[cl] - ira_available_class_regs[cl]);
1077 fprintf (sched_dump, "\n");
1080 /* Determine if INSN has a condition that is clobbered if a register
1081 in SET_REGS is modified. */
1083 cond_clobbered_p (rtx insn, HARD_REG_SET set_regs)
1085 rtx pat = PATTERN (insn);
1086 gcc_assert (GET_CODE (pat) == COND_EXEC);
1087 if (TEST_HARD_REG_BIT (set_regs, REGNO (XEXP (COND_EXEC_TEST (pat), 0))))
1089 sd_iterator_def sd_it;
1091 haifa_change_pattern (insn, ORIG_PAT (insn));
1092 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
1093 DEP_STATUS (dep) &= ~DEP_CANCELLED;
1094 TODO_SPEC (insn) = HARD_DEP;
1095 if (sched_verbose >= 2)
1096 fprintf (sched_dump,
1097 ";;\t\tdequeue insn %s because of clobbered condition\n",
1098 (*current_sched_info->print_insn) (insn, 0));
1105 /* Look at the remaining dependencies for insn NEXT, and compute and return
1106 the TODO_SPEC value we should use for it. This is called after one of
1107 NEXT's dependencies has been resolved. */
1110 recompute_todo_spec (rtx next)
1113 sd_iterator_def sd_it;
1114 dep_t dep, control_dep = NULL;
1117 bool first_p = true;
1119 if (sd_lists_empty_p (next, SD_LIST_BACK))
1120 /* NEXT has all its dependencies resolved. */
1123 if (!sd_lists_empty_p (next, SD_LIST_HARD_BACK))
1126 /* Now we've got NEXT with speculative deps only.
1127 1. Look at the deps to see what we have to do.
1128 2. Check if we can do 'todo'. */
1131 FOR_EACH_DEP (next, SD_LIST_BACK, sd_it, dep)
1133 ds_t ds = DEP_STATUS (dep) & SPECULATIVE;
1135 if (DEBUG_INSN_P (DEP_PRO (dep)) && !DEBUG_INSN_P (next))
1148 new_ds = ds_merge (new_ds, ds);
1150 if (DEP_TYPE (dep) == REG_DEP_CONTROL)
1154 DEP_STATUS (dep) &= ~DEP_CANCELLED;
1158 if (n_control == 1 && n_spec == 0)
1160 rtx pro, other, new_pat;
1161 rtx cond = NULL_RTX;
1163 rtx prev = NULL_RTX;
1167 if ((current_sched_info->flags & DO_PREDICATION) == 0
1168 || (ORIG_PAT (next) != NULL_RTX
1169 && PREDICATED_PAT (next) == NULL_RTX))
1172 pro = DEP_PRO (control_dep);
1173 other = real_insn_for_shadow (pro);
1174 if (other != NULL_RTX)
1177 cond = sched_get_reverse_condition_uncached (pro);
1178 regno = REGNO (XEXP (cond, 0));
1180 /* Find the last scheduled insn that modifies the condition register.
1181 We can stop looking once we find the insn we depend on through the
1182 REG_DEP_CONTROL; if the condition register isn't modified after it,
1183 we know that it still has the right value. */
1184 if (QUEUE_INDEX (pro) == QUEUE_SCHEDULED)
1185 FOR_EACH_VEC_ELT_REVERSE (rtx, scheduled_insns, i, prev)
1189 find_all_hard_reg_sets (prev, &t);
1190 if (TEST_HARD_REG_BIT (t, regno))
1195 if (ORIG_PAT (next) == NULL_RTX)
1197 ORIG_PAT (next) = PATTERN (next);
1199 new_pat = gen_rtx_COND_EXEC (VOIDmode, cond, PATTERN (next));
1200 success = haifa_change_pattern (next, new_pat);
1203 PREDICATED_PAT (next) = new_pat;
1205 else if (PATTERN (next) != PREDICATED_PAT (next))
1207 bool success = haifa_change_pattern (next,
1208 PREDICATED_PAT (next));
1209 gcc_assert (success);
1211 DEP_STATUS (control_dep) |= DEP_CANCELLED;
1215 if (PREDICATED_PAT (next) != NULL_RTX)
1217 int tick = INSN_TICK (next);
1218 bool success = haifa_change_pattern (next,
1220 INSN_TICK (next) = tick;
1221 gcc_assert (success);
1224 /* We can't handle the case where there are both speculative and control
1225 dependencies, so we return HARD_DEP in such a case. Also fail if
1226 we have speculative dependencies with not enough points, or more than
1227 one control dependency. */
1228 if ((n_spec > 0 && n_control > 0)
1230 /* Too few points? */
1231 && ds_weak (new_ds) < spec_info->data_weakness_cutoff)
1238 /* Pointer to the last instruction scheduled. */
1239 static rtx last_scheduled_insn;
1241 /* Pointer to the last nondebug instruction scheduled within the
1242 block, or the prev_head of the scheduling block. Used by
1243 rank_for_schedule, so that insns independent of the last scheduled
1244 insn will be preferred over dependent instructions. */
1245 static rtx last_nondebug_scheduled_insn;
1247 /* Pointer that iterates through the list of unscheduled insns if we
1248 have a dbg_cnt enabled. It always points at an insn prior to the
1249 first unscheduled one. */
1250 static rtx nonscheduled_insns_begin;
1252 /* Cached cost of the instruction. Use below function to get cost of the
1253 insn. -1 here means that the field is not initialized. */
1254 #define INSN_COST(INSN) (HID (INSN)->cost)
1256 /* Compute cost of executing INSN.
1257 This is the number of cycles between instruction issue and
1258 instruction results. */
1260 insn_cost (rtx insn)
1266 if (recog_memoized (insn) < 0)
1269 cost = insn_default_latency (insn);
1276 cost = INSN_COST (insn);
1280 /* A USE insn, or something else we don't need to
1281 understand. We can't pass these directly to
1282 result_ready_cost or insn_default_latency because it will
1283 trigger a fatal error for unrecognizable insns. */
1284 if (recog_memoized (insn) < 0)
1286 INSN_COST (insn) = 0;
1291 cost = insn_default_latency (insn);
1295 INSN_COST (insn) = cost;
1302 /* Compute cost of dependence LINK.
1303 This is the number of cycles between instruction issue and
1304 instruction results.
1305 ??? We also use this function to call recog_memoized on all insns. */
1307 dep_cost_1 (dep_t link, dw_t dw)
1309 rtx insn = DEP_PRO (link);
1310 rtx used = DEP_CON (link);
1313 if (DEP_COST (link) != UNKNOWN_DEP_COST)
1314 return DEP_COST (link);
1318 struct delay_pair *delay_entry;
1320 = (struct delay_pair *)htab_find_with_hash (delay_htab_i2, used,
1321 htab_hash_pointer (used));
1324 if (delay_entry->i1 == insn)
1326 DEP_COST (link) = pair_delay (delay_entry);
1327 return DEP_COST (link);
1332 /* A USE insn should never require the value used to be computed.
1333 This allows the computation of a function's result and parameter
1334 values to overlap the return and call. We don't care about the
1335 dependence cost when only decreasing register pressure. */
1336 if (recog_memoized (used) < 0)
1339 recog_memoized (insn);
1343 enum reg_note dep_type = DEP_TYPE (link);
1345 cost = insn_cost (insn);
1347 if (INSN_CODE (insn) >= 0)
1349 if (dep_type == REG_DEP_ANTI)
1351 else if (dep_type == REG_DEP_OUTPUT)
1353 cost = (insn_default_latency (insn)
1354 - insn_default_latency (used));
1358 else if (bypass_p (insn))
1359 cost = insn_latency (insn, used);
1363 if (targetm.sched.adjust_cost_2)
1364 cost = targetm.sched.adjust_cost_2 (used, (int) dep_type, insn, cost,
1366 else if (targetm.sched.adjust_cost != NULL)
1368 /* This variable is used for backward compatibility with the
1370 rtx dep_cost_rtx_link = alloc_INSN_LIST (NULL_RTX, NULL_RTX);
1372 /* Make it self-cycled, so that if some tries to walk over this
1373 incomplete list he/she will be caught in an endless loop. */
1374 XEXP (dep_cost_rtx_link, 1) = dep_cost_rtx_link;
1376 /* Targets use only REG_NOTE_KIND of the link. */
1377 PUT_REG_NOTE_KIND (dep_cost_rtx_link, DEP_TYPE (link));
1379 cost = targetm.sched.adjust_cost (used, dep_cost_rtx_link,
1382 free_INSN_LIST_node (dep_cost_rtx_link);
1389 DEP_COST (link) = cost;
1393 /* Compute cost of dependence LINK.
1394 This is the number of cycles between instruction issue and
1395 instruction results. */
1397 dep_cost (dep_t link)
1399 return dep_cost_1 (link, 0);
1402 /* Use this sel-sched.c friendly function in reorder2 instead of increasing
1403 INSN_PRIORITY explicitly. */
1405 increase_insn_priority (rtx insn, int amount)
1407 if (!sel_sched_p ())
1409 /* We're dealing with haifa-sched.c INSN_PRIORITY. */
1410 if (INSN_PRIORITY_KNOWN (insn))
1411 INSN_PRIORITY (insn) += amount;
1415 /* In sel-sched.c INSN_PRIORITY is not kept up to date.
1416 Use EXPR_PRIORITY instead. */
1417 sel_add_to_insn_priority (insn, amount);
1421 /* Return 'true' if DEP should be included in priority calculations. */
1423 contributes_to_priority_p (dep_t dep)
1425 if (DEBUG_INSN_P (DEP_CON (dep))
1426 || DEBUG_INSN_P (DEP_PRO (dep)))
1429 /* Critical path is meaningful in block boundaries only. */
1430 if (!current_sched_info->contributes_to_priority (DEP_CON (dep),
1434 /* If flag COUNT_SPEC_IN_CRITICAL_PATH is set,
1435 then speculative instructions will less likely be
1436 scheduled. That is because the priority of
1437 their producers will increase, and, thus, the
1438 producers will more likely be scheduled, thus,
1439 resolving the dependence. */
1440 if (sched_deps_info->generate_spec_deps
1441 && !(spec_info->flags & COUNT_SPEC_IN_CRITICAL_PATH)
1442 && (DEP_STATUS (dep) & SPECULATIVE))
1448 /* Compute the number of nondebug forward deps of an insn. */
1451 dep_list_size (rtx insn)
1453 sd_iterator_def sd_it;
1455 int dbgcount = 0, nodbgcount = 0;
1457 if (!MAY_HAVE_DEBUG_INSNS)
1458 return sd_lists_size (insn, SD_LIST_FORW);
1460 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
1462 if (DEBUG_INSN_P (DEP_CON (dep)))
1464 else if (!DEBUG_INSN_P (DEP_PRO (dep)))
1468 gcc_assert (dbgcount + nodbgcount == sd_lists_size (insn, SD_LIST_FORW));
1473 /* Compute the priority number for INSN. */
1477 if (! INSN_P (insn))
1480 /* We should not be interested in priority of an already scheduled insn. */
1481 gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
1483 if (!INSN_PRIORITY_KNOWN (insn))
1485 int this_priority = -1;
1487 if (dep_list_size (insn) == 0)
1488 /* ??? We should set INSN_PRIORITY to insn_cost when and insn has
1489 some forward deps but all of them are ignored by
1490 contributes_to_priority hook. At the moment we set priority of
1492 this_priority = insn_cost (insn);
1495 rtx prev_first, twin;
1498 /* For recovery check instructions we calculate priority slightly
1499 different than that of normal instructions. Instead of walking
1500 through INSN_FORW_DEPS (check) list, we walk through
1501 INSN_FORW_DEPS list of each instruction in the corresponding
1504 /* Selective scheduling does not define RECOVERY_BLOCK macro. */
1505 rec = sel_sched_p () ? NULL : RECOVERY_BLOCK (insn);
1506 if (!rec || rec == EXIT_BLOCK_PTR)
1508 prev_first = PREV_INSN (insn);
1513 prev_first = NEXT_INSN (BB_HEAD (rec));
1514 twin = PREV_INSN (BB_END (rec));
1519 sd_iterator_def sd_it;
1522 FOR_EACH_DEP (twin, SD_LIST_FORW, sd_it, dep)
1527 next = DEP_CON (dep);
1529 if (BLOCK_FOR_INSN (next) != rec)
1533 if (!contributes_to_priority_p (dep))
1537 cost = dep_cost (dep);
1540 struct _dep _dep1, *dep1 = &_dep1;
1542 init_dep (dep1, insn, next, REG_DEP_ANTI);
1544 cost = dep_cost (dep1);
1547 next_priority = cost + priority (next);
1549 if (next_priority > this_priority)
1550 this_priority = next_priority;
1554 twin = PREV_INSN (twin);
1556 while (twin != prev_first);
1559 if (this_priority < 0)
1561 gcc_assert (this_priority == -1);
1563 this_priority = insn_cost (insn);
1566 INSN_PRIORITY (insn) = this_priority;
1567 INSN_PRIORITY_STATUS (insn) = 1;
1570 return INSN_PRIORITY (insn);
1573 /* Macros and functions for keeping the priority queue sorted, and
1574 dealing with queuing and dequeuing of instructions. */
1576 #define SCHED_SORT(READY, N_READY) \
1577 do { if ((N_READY) == 2) \
1578 swap_sort (READY, N_READY); \
1579 else if ((N_READY) > 2) \
1580 qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); } \
1583 /* Setup info about the current register pressure impact of scheduling
1584 INSN at the current scheduling point. */
1586 setup_insn_reg_pressure_info (rtx insn)
1588 int i, change, before, after, hard_regno;
1589 int excess_cost_change;
1590 enum machine_mode mode;
1592 struct reg_pressure_data *pressure_info;
1593 int *max_reg_pressure;
1594 struct reg_use_data *use;
1595 static int death[N_REG_CLASSES];
1597 gcc_checking_assert (!DEBUG_INSN_P (insn));
1599 excess_cost_change = 0;
1600 for (i = 0; i < ira_pressure_classes_num; i++)
1601 death[ira_pressure_classes[i]] = 0;
1602 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
1603 if (dying_use_p (use))
1605 cl = sched_regno_pressure_class[use->regno];
1606 if (use->regno < FIRST_PSEUDO_REGISTER)
1610 += ira_reg_class_max_nregs[cl][PSEUDO_REGNO_MODE (use->regno)];
1612 pressure_info = INSN_REG_PRESSURE (insn);
1613 max_reg_pressure = INSN_MAX_REG_PRESSURE (insn);
1614 gcc_assert (pressure_info != NULL && max_reg_pressure != NULL);
1615 for (i = 0; i < ira_pressure_classes_num; i++)
1617 cl = ira_pressure_classes[i];
1618 gcc_assert (curr_reg_pressure[cl] >= 0);
1619 change = (int) pressure_info[i].set_increase - death[cl];
1620 before = MAX (0, max_reg_pressure[i] - ira_available_class_regs[cl]);
1621 after = MAX (0, max_reg_pressure[i] + change
1622 - ira_available_class_regs[cl]);
1623 hard_regno = ira_class_hard_regs[cl][0];
1624 gcc_assert (hard_regno >= 0);
1625 mode = reg_raw_mode[hard_regno];
1626 excess_cost_change += ((after - before)
1627 * (ira_memory_move_cost[mode][cl][0]
1628 + ira_memory_move_cost[mode][cl][1]));
1630 INSN_REG_PRESSURE_EXCESS_COST_CHANGE (insn) = excess_cost_change;
1633 /* Returns a positive value if x is preferred; returns a negative value if
1634 y is preferred. Should never return 0, since that will make the sort
1638 rank_for_schedule (const void *x, const void *y)
1640 rtx tmp = *(const rtx *) y;
1641 rtx tmp2 = *(const rtx *) x;
1642 int tmp_class, tmp2_class;
1643 int val, priority_val, info_val;
1645 if (MAY_HAVE_DEBUG_INSNS)
1647 /* Schedule debug insns as early as possible. */
1648 if (DEBUG_INSN_P (tmp) && !DEBUG_INSN_P (tmp2))
1650 else if (DEBUG_INSN_P (tmp2))
1654 /* The insn in a schedule group should be issued the first. */
1655 if (flag_sched_group_heuristic &&
1656 SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
1657 return SCHED_GROUP_P (tmp2) ? 1 : -1;
1659 /* Make sure that priority of TMP and TMP2 are initialized. */
1660 gcc_assert (INSN_PRIORITY_KNOWN (tmp) && INSN_PRIORITY_KNOWN (tmp2));
1662 if (sched_pressure_p)
1666 /* Prefer insn whose scheduling results in the smallest register
1668 if ((diff = (INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp)
1669 + (INSN_TICK (tmp) > clock_var
1670 ? INSN_TICK (tmp) - clock_var : 0)
1671 - INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp2)
1672 - (INSN_TICK (tmp2) > clock_var
1673 ? INSN_TICK (tmp2) - clock_var : 0))) != 0)
1678 if (sched_pressure_p
1679 && (INSN_TICK (tmp2) > clock_var || INSN_TICK (tmp) > clock_var))
1681 if (INSN_TICK (tmp) <= clock_var)
1683 else if (INSN_TICK (tmp2) <= clock_var)
1686 return INSN_TICK (tmp) - INSN_TICK (tmp2);
1689 /* If we are doing backtracking in this schedule, prefer insns that
1690 have forward dependencies with negative cost against an insn that
1691 was already scheduled. */
1692 if (current_sched_info->flags & DO_BACKTRACKING)
1694 priority_val = FEEDS_BACKTRACK_INSN (tmp2) - FEEDS_BACKTRACK_INSN (tmp);
1696 return priority_val;
1699 /* Prefer insn with higher priority. */
1700 priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
1702 if (flag_sched_critical_path_heuristic && priority_val)
1703 return priority_val;
1705 /* Prefer speculative insn with greater dependencies weakness. */
1706 if (flag_sched_spec_insn_heuristic && spec_info)
1712 ds1 = TODO_SPEC (tmp) & SPECULATIVE;
1714 dw1 = ds_weak (ds1);
1718 ds2 = TODO_SPEC (tmp2) & SPECULATIVE;
1720 dw2 = ds_weak (ds2);
1725 if (dw > (NO_DEP_WEAK / 8) || dw < -(NO_DEP_WEAK / 8))
1729 info_val = (*current_sched_info->rank) (tmp, tmp2);
1730 if(flag_sched_rank_heuristic && info_val)
1733 /* Compare insns based on their relation to the last scheduled
1735 if (flag_sched_last_insn_heuristic && last_nondebug_scheduled_insn)
1739 rtx last = last_nondebug_scheduled_insn;
1741 /* Classify the instructions into three classes:
1742 1) Data dependent on last schedule insn.
1743 2) Anti/Output dependent on last scheduled insn.
1744 3) Independent of last scheduled insn, or has latency of one.
1745 Choose the insn from the highest numbered class if different. */
1746 dep1 = sd_find_dep_between (last, tmp, true);
1748 if (dep1 == NULL || dep_cost (dep1) == 1)
1750 else if (/* Data dependence. */
1751 DEP_TYPE (dep1) == REG_DEP_TRUE)
1756 dep2 = sd_find_dep_between (last, tmp2, true);
1758 if (dep2 == NULL || dep_cost (dep2) == 1)
1760 else if (/* Data dependence. */
1761 DEP_TYPE (dep2) == REG_DEP_TRUE)
1766 if ((val = tmp2_class - tmp_class))
1770 /* Prefer the insn which has more later insns that depend on it.
1771 This gives the scheduler more freedom when scheduling later
1772 instructions at the expense of added register pressure. */
1774 val = (dep_list_size (tmp2) - dep_list_size (tmp));
1776 if (flag_sched_dep_count_heuristic && val != 0)
1779 /* If insns are equally good, sort by INSN_LUID (original insn order),
1780 so that we make the sort stable. This minimizes instruction movement,
1781 thus minimizing sched's effect on debugging and cross-jumping. */
1782 return INSN_LUID (tmp) - INSN_LUID (tmp2);
1785 /* Resort the array A in which only element at index N may be out of order. */
1787 HAIFA_INLINE static void
1788 swap_sort (rtx *a, int n)
1790 rtx insn = a[n - 1];
1793 while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
1801 /* Add INSN to the insn queue so that it can be executed at least
1802 N_CYCLES after the currently executing insn. Preserve insns
1803 chain for debugging purposes. REASON will be printed in debugging
1806 HAIFA_INLINE static void
1807 queue_insn (rtx insn, int n_cycles, const char *reason)
1809 int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
1810 rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
1813 gcc_assert (n_cycles <= max_insn_queue_index);
1814 gcc_assert (!DEBUG_INSN_P (insn));
1816 insn_queue[next_q] = link;
1819 if (sched_verbose >= 2)
1821 fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
1822 (*current_sched_info->print_insn) (insn, 0));
1824 fprintf (sched_dump, "queued for %d cycles (%s).\n", n_cycles, reason);
1827 QUEUE_INDEX (insn) = next_q;
1829 if (current_sched_info->flags & DO_BACKTRACKING)
1831 new_tick = clock_var + n_cycles;
1832 if (INSN_TICK (insn) == INVALID_TICK || INSN_TICK (insn) < new_tick)
1833 INSN_TICK (insn) = new_tick;
1835 if (INSN_EXACT_TICK (insn) != INVALID_TICK
1836 && INSN_EXACT_TICK (insn) < clock_var + n_cycles)
1838 must_backtrack = true;
1839 if (sched_verbose >= 2)
1840 fprintf (sched_dump, ";;\t\tcausing a backtrack.\n");
1845 /* Remove INSN from queue. */
1847 queue_remove (rtx insn)
1849 gcc_assert (QUEUE_INDEX (insn) >= 0);
1850 remove_free_INSN_LIST_elem (insn, &insn_queue[QUEUE_INDEX (insn)]);
1852 QUEUE_INDEX (insn) = QUEUE_NOWHERE;
1855 /* Return a pointer to the bottom of the ready list, i.e. the insn
1856 with the lowest priority. */
1859 ready_lastpos (struct ready_list *ready)
1861 gcc_assert (ready->n_ready >= 1);
1862 return ready->vec + ready->first - ready->n_ready + 1;
1865 /* Add an element INSN to the ready list so that it ends up with the
1866 lowest/highest priority depending on FIRST_P. */
1868 HAIFA_INLINE static void
1869 ready_add (struct ready_list *ready, rtx insn, bool first_p)
1873 if (ready->first == ready->n_ready)
1875 memmove (ready->vec + ready->veclen - ready->n_ready,
1876 ready_lastpos (ready),
1877 ready->n_ready * sizeof (rtx));
1878 ready->first = ready->veclen - 1;
1880 ready->vec[ready->first - ready->n_ready] = insn;
1884 if (ready->first == ready->veclen - 1)
1887 /* ready_lastpos() fails when called with (ready->n_ready == 0). */
1888 memmove (ready->vec + ready->veclen - ready->n_ready - 1,
1889 ready_lastpos (ready),
1890 ready->n_ready * sizeof (rtx));
1891 ready->first = ready->veclen - 2;
1893 ready->vec[++(ready->first)] = insn;
1897 if (DEBUG_INSN_P (insn))
1900 gcc_assert (QUEUE_INDEX (insn) != QUEUE_READY);
1901 QUEUE_INDEX (insn) = QUEUE_READY;
1903 if (INSN_EXACT_TICK (insn) != INVALID_TICK
1904 && INSN_EXACT_TICK (insn) < clock_var)
1906 must_backtrack = true;
1910 /* Remove the element with the highest priority from the ready list and
1913 HAIFA_INLINE static rtx
1914 ready_remove_first (struct ready_list *ready)
1918 gcc_assert (ready->n_ready);
1919 t = ready->vec[ready->first--];
1921 if (DEBUG_INSN_P (t))
1923 /* If the queue becomes empty, reset it. */
1924 if (ready->n_ready == 0)
1925 ready->first = ready->veclen - 1;
1927 gcc_assert (QUEUE_INDEX (t) == QUEUE_READY);
1928 QUEUE_INDEX (t) = QUEUE_NOWHERE;
1933 /* The following code implements multi-pass scheduling for the first
1934 cycle. In other words, we will try to choose ready insn which
1935 permits to start maximum number of insns on the same cycle. */
1937 /* Return a pointer to the element INDEX from the ready. INDEX for
1938 insn with the highest priority is 0, and the lowest priority has
1942 ready_element (struct ready_list *ready, int index)
1944 gcc_assert (ready->n_ready && index < ready->n_ready);
1946 return ready->vec[ready->first - index];
1949 /* Remove the element INDEX from the ready list and return it. INDEX
1950 for insn with the highest priority is 0, and the lowest priority
1953 HAIFA_INLINE static rtx
1954 ready_remove (struct ready_list *ready, int index)
1960 return ready_remove_first (ready);
1961 gcc_assert (ready->n_ready && index < ready->n_ready);
1962 t = ready->vec[ready->first - index];
1964 if (DEBUG_INSN_P (t))
1966 for (i = index; i < ready->n_ready; i++)
1967 ready->vec[ready->first - i] = ready->vec[ready->first - i - 1];
1968 QUEUE_INDEX (t) = QUEUE_NOWHERE;
1972 /* Remove INSN from the ready list. */
1974 ready_remove_insn (rtx insn)
1978 for (i = 0; i < readyp->n_ready; i++)
1979 if (ready_element (readyp, i) == insn)
1981 ready_remove (readyp, i);
1987 /* Sort the ready list READY by ascending priority, using the SCHED_SORT
1991 ready_sort (struct ready_list *ready)
1994 rtx *first = ready_lastpos (ready);
1996 if (sched_pressure_p)
1998 for (i = 0; i < ready->n_ready; i++)
1999 if (!DEBUG_INSN_P (first[i]))
2000 setup_insn_reg_pressure_info (first[i]);
2002 SCHED_SORT (first, ready->n_ready);
2005 /* PREV is an insn that is ready to execute. Adjust its priority if that
2006 will help shorten or lengthen register lifetimes as appropriate. Also
2007 provide a hook for the target to tweak itself. */
2009 HAIFA_INLINE static void
2010 adjust_priority (rtx prev)
2012 /* ??? There used to be code here to try and estimate how an insn
2013 affected register lifetimes, but it did it by looking at REG_DEAD
2014 notes, which we removed in schedule_region. Nor did it try to
2015 take into account register pressure or anything useful like that.
2017 Revisit when we have a machine model to work with and not before. */
2019 if (targetm.sched.adjust_priority)
2020 INSN_PRIORITY (prev) =
2021 targetm.sched.adjust_priority (prev, INSN_PRIORITY (prev));
2024 /* Advance DFA state STATE on one cycle. */
2026 advance_state (state_t state)
2028 if (targetm.sched.dfa_pre_advance_cycle)
2029 targetm.sched.dfa_pre_advance_cycle ();
2031 if (targetm.sched.dfa_pre_cycle_insn)
2032 state_transition (state,
2033 targetm.sched.dfa_pre_cycle_insn ());
2035 state_transition (state, NULL);
2037 if (targetm.sched.dfa_post_cycle_insn)
2038 state_transition (state,
2039 targetm.sched.dfa_post_cycle_insn ());
2041 if (targetm.sched.dfa_post_advance_cycle)
2042 targetm.sched.dfa_post_advance_cycle ();
2045 /* Advance time on one cycle. */
2046 HAIFA_INLINE static void
2047 advance_one_cycle (void)
2049 advance_state (curr_state);
2050 if (sched_verbose >= 6)
2051 fprintf (sched_dump, ";;\tAdvanced a state.\n");
2054 /* Update register pressure after scheduling INSN. */
2056 update_register_pressure (rtx insn)
2058 struct reg_use_data *use;
2059 struct reg_set_data *set;
2061 gcc_checking_assert (!DEBUG_INSN_P (insn));
2063 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
2064 if (dying_use_p (use) && bitmap_bit_p (curr_reg_live, use->regno))
2065 mark_regno_birth_or_death (use->regno, false);
2066 for (set = INSN_REG_SET_LIST (insn); set != NULL; set = set->next_insn_set)
2067 mark_regno_birth_or_death (set->regno, true);
2070 /* Set up or update (if UPDATE_P) max register pressure (see its
2071 meaning in sched-int.h::_haifa_insn_data) for all current BB insns
2072 after insn AFTER. */
2074 setup_insn_max_reg_pressure (rtx after, bool update_p)
2079 static int max_reg_pressure[N_REG_CLASSES];
2081 save_reg_pressure ();
2082 for (i = 0; i < ira_pressure_classes_num; i++)
2083 max_reg_pressure[ira_pressure_classes[i]]
2084 = curr_reg_pressure[ira_pressure_classes[i]];
2085 for (insn = NEXT_INSN (after);
2086 insn != NULL_RTX && ! BARRIER_P (insn)
2087 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (after);
2088 insn = NEXT_INSN (insn))
2089 if (NONDEBUG_INSN_P (insn))
2092 for (i = 0; i < ira_pressure_classes_num; i++)
2094 p = max_reg_pressure[ira_pressure_classes[i]];
2095 if (INSN_MAX_REG_PRESSURE (insn)[i] != p)
2098 INSN_MAX_REG_PRESSURE (insn)[i]
2099 = max_reg_pressure[ira_pressure_classes[i]];
2102 if (update_p && eq_p)
2104 update_register_pressure (insn);
2105 for (i = 0; i < ira_pressure_classes_num; i++)
2106 if (max_reg_pressure[ira_pressure_classes[i]]
2107 < curr_reg_pressure[ira_pressure_classes[i]])
2108 max_reg_pressure[ira_pressure_classes[i]]
2109 = curr_reg_pressure[ira_pressure_classes[i]];
2111 restore_reg_pressure ();
2114 /* Update the current register pressure after scheduling INSN. Update
2115 also max register pressure for unscheduled insns of the current
2118 update_reg_and_insn_max_reg_pressure (rtx insn)
2121 int before[N_REG_CLASSES];
2123 for (i = 0; i < ira_pressure_classes_num; i++)
2124 before[i] = curr_reg_pressure[ira_pressure_classes[i]];
2125 update_register_pressure (insn);
2126 for (i = 0; i < ira_pressure_classes_num; i++)
2127 if (curr_reg_pressure[ira_pressure_classes[i]] != before[i])
2129 if (i < ira_pressure_classes_num)
2130 setup_insn_max_reg_pressure (insn, true);
2133 /* Set up register pressure at the beginning of basic block BB whose
2134 insns starting after insn AFTER. Set up also max register pressure
2135 for all insns of the basic block. */
2137 sched_setup_bb_reg_pressure_info (basic_block bb, rtx after)
2139 gcc_assert (sched_pressure_p);
2140 initiate_bb_reg_pressure_info (bb);
2141 setup_insn_max_reg_pressure (after, false);
2144 /* If doing predication while scheduling, verify whether INSN, which
2145 has just been scheduled, clobbers the conditions of any
2146 instructions that must be predicated in order to break their
2147 dependencies. If so, remove them from the queues so that they will
2148 only be scheduled once their control dependency is resolved. */
2151 check_clobbered_conditions (rtx insn)
2156 if ((current_sched_info->flags & DO_PREDICATION) == 0)
2159 find_all_hard_reg_sets (insn, &t);
2162 for (i = 0; i < ready.n_ready; i++)
2164 rtx x = ready_element (&ready, i);
2165 if (TODO_SPEC (x) == DEP_CONTROL && cond_clobbered_p (x, t))
2167 ready_remove_insn (x);
2171 for (i = 0; i <= max_insn_queue_index; i++)
2174 int q = NEXT_Q_AFTER (q_ptr, i);
2177 for (link = insn_queue[q]; link; link = XEXP (link, 1))
2179 rtx x = XEXP (link, 0);
2180 if (TODO_SPEC (x) == DEP_CONTROL && cond_clobbered_p (x, t))
2189 /* A structure that holds local state for the loop in schedule_block. */
2190 struct sched_block_state
2192 /* True if no real insns have been scheduled in the current cycle. */
2193 bool first_cycle_insn_p;
2194 /* True if a shadow insn has been scheduled in the current cycle, which
2195 means that no more normal insns can be issued. */
2196 bool shadows_only_p;
2197 /* True if we're winding down a modulo schedule, which means that we only
2198 issue insns with INSN_EXACT_TICK set. */
2199 bool modulo_epilogue;
2200 /* Initialized with the machine's issue rate every cycle, and updated
2201 by calls to the variable_issue hook. */
2205 /* INSN is the "currently executing insn". Launch each insn which was
2206 waiting on INSN. READY is the ready list which contains the insns
2207 that are ready to fire. CLOCK is the current cycle. The function
2208 returns necessary cycle advance after issuing the insn (it is not
2209 zero for insns in a schedule group). */
2212 schedule_insn (rtx insn)
2214 sd_iterator_def sd_it;
2219 if (sched_verbose >= 1)
2221 struct reg_pressure_data *pressure_info;
2224 print_insn (buf, insn, 0);
2226 fprintf (sched_dump, ";;\t%3i--> %-40s:", clock_var, buf);
2228 if (recog_memoized (insn) < 0)
2229 fprintf (sched_dump, "nothing");
2231 print_reservation (sched_dump, insn);
2232 pressure_info = INSN_REG_PRESSURE (insn);
2233 if (pressure_info != NULL)
2235 fputc (':', sched_dump);
2236 for (i = 0; i < ira_pressure_classes_num; i++)
2237 fprintf (sched_dump, "%s%+d(%d)",
2238 reg_class_names[ira_pressure_classes[i]],
2239 pressure_info[i].set_increase, pressure_info[i].change);
2241 fputc ('\n', sched_dump);
2244 if (sched_pressure_p && !DEBUG_INSN_P (insn))
2245 update_reg_and_insn_max_reg_pressure (insn);
2247 /* Scheduling instruction should have all its dependencies resolved and
2248 should have been removed from the ready list. */
2249 gcc_assert (sd_lists_empty_p (insn, SD_LIST_HARD_BACK));
2251 /* Reset debug insns invalidated by moving this insn. */
2252 if (MAY_HAVE_DEBUG_INSNS && !DEBUG_INSN_P (insn))
2253 for (sd_it = sd_iterator_start (insn, SD_LIST_BACK);
2254 sd_iterator_cond (&sd_it, &dep);)
2256 rtx dbg = DEP_PRO (dep);
2257 struct reg_use_data *use, *next;
2259 if (DEP_STATUS (dep) & DEP_CANCELLED)
2261 sd_iterator_next (&sd_it);
2265 gcc_assert (DEBUG_INSN_P (dbg));
2267 if (sched_verbose >= 6)
2268 fprintf (sched_dump, ";;\t\tresetting: debug insn %d\n",
2271 /* ??? Rather than resetting the debug insn, we might be able
2272 to emit a debug temp before the just-scheduled insn, but
2273 this would involve checking that the expression at the
2274 point of the debug insn is equivalent to the expression
2275 before the just-scheduled insn. They might not be: the
2276 expression in the debug insn may depend on other insns not
2277 yet scheduled that set MEMs, REGs or even other debug
2278 insns. It's not clear that attempting to preserve debug
2279 information in these cases is worth the effort, given how
2280 uncommon these resets are and the likelihood that the debug
2281 temps introduced won't survive the schedule change. */
2282 INSN_VAR_LOCATION_LOC (dbg) = gen_rtx_UNKNOWN_VAR_LOC ();
2283 df_insn_rescan (dbg);
2285 /* Unknown location doesn't use any registers. */
2286 for (use = INSN_REG_USE_LIST (dbg); use != NULL; use = next)
2288 struct reg_use_data *prev = use;
2290 /* Remove use from the cyclic next_regno_use chain first. */
2291 while (prev->next_regno_use != use)
2292 prev = prev->next_regno_use;
2293 prev->next_regno_use = use->next_regno_use;
2294 next = use->next_insn_use;
2297 INSN_REG_USE_LIST (dbg) = NULL;
2299 /* We delete rather than resolve these deps, otherwise we
2300 crash in sched_free_deps(), because forward deps are
2301 expected to be released before backward deps. */
2302 sd_delete_dep (sd_it);
2305 gcc_assert (QUEUE_INDEX (insn) == QUEUE_NOWHERE);
2306 QUEUE_INDEX (insn) = QUEUE_SCHEDULED;
2308 gcc_assert (INSN_TICK (insn) >= MIN_TICK);
2309 if (INSN_TICK (insn) > clock_var)
2310 /* INSN has been prematurely moved from the queue to the ready list.
2311 This is possible only if following flag is set. */
2312 gcc_assert (flag_sched_stalled_insns);
2314 /* ??? Probably, if INSN is scheduled prematurely, we should leave
2315 INSN_TICK untouched. This is a machine-dependent issue, actually. */
2316 INSN_TICK (insn) = clock_var;
2318 check_clobbered_conditions (insn);
2320 /* Update dependent instructions. */
2321 for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
2322 sd_iterator_cond (&sd_it, &dep);)
2324 rtx next = DEP_CON (dep);
2325 bool cancelled = (DEP_STATUS (dep) & DEP_CANCELLED) != 0;
2327 /* Resolve the dependence between INSN and NEXT.
2328 sd_resolve_dep () moves current dep to another list thus
2329 advancing the iterator. */
2330 sd_resolve_dep (sd_it);
2334 if (QUEUE_INDEX (next) != QUEUE_SCHEDULED)
2336 int tick = INSN_TICK (next);
2337 gcc_assert (ORIG_PAT (next) != NULL_RTX);
2338 haifa_change_pattern (next, ORIG_PAT (next));
2339 INSN_TICK (next) = tick;
2340 if (sd_lists_empty_p (next, SD_LIST_BACK))
2341 TODO_SPEC (next) = 0;
2342 else if (!sd_lists_empty_p (next, SD_LIST_HARD_BACK))
2343 TODO_SPEC (next) = HARD_DEP;
2348 /* Don't bother trying to mark next as ready if insn is a debug
2349 insn. If insn is the last hard dependency, it will have
2350 already been discounted. */
2351 if (DEBUG_INSN_P (insn) && !DEBUG_INSN_P (next))
2354 if (!IS_SPECULATION_BRANCHY_CHECK_P (insn))
2358 effective_cost = try_ready (next);
2360 if (effective_cost >= 0
2361 && SCHED_GROUP_P (next)
2362 && advance < effective_cost)
2363 advance = effective_cost;
2366 /* Check always has only one forward dependence (to the first insn in
2367 the recovery block), therefore, this will be executed only once. */
2369 gcc_assert (sd_lists_empty_p (insn, SD_LIST_FORW));
2370 fix_recovery_deps (RECOVERY_BLOCK (insn));
2374 /* Annotate the instruction with issue information -- TImode
2375 indicates that the instruction is expected not to be able
2376 to issue on the same cycle as the previous insn. A machine
2377 may use this information to decide how the instruction should
2380 && GET_CODE (PATTERN (insn)) != USE
2381 && GET_CODE (PATTERN (insn)) != CLOBBER
2382 && !DEBUG_INSN_P (insn))
2384 if (reload_completed)
2385 PUT_MODE (insn, clock_var > last_clock_var ? TImode : VOIDmode);
2386 last_clock_var = clock_var;
2392 /* Functions for handling of notes. */
2394 /* Add note list that ends on FROM_END to the end of TO_ENDP. */
2396 concat_note_lists (rtx from_end, rtx *to_endp)
2400 /* It's easy when have nothing to concat. */
2401 if (from_end == NULL)
2404 /* It's also easy when destination is empty. */
2405 if (*to_endp == NULL)
2407 *to_endp = from_end;
2411 from_start = from_end;
2412 while (PREV_INSN (from_start) != NULL)
2413 from_start = PREV_INSN (from_start);
2415 PREV_INSN (from_start) = *to_endp;
2416 NEXT_INSN (*to_endp) = from_start;
2417 *to_endp = from_end;
2420 /* Delete notes between HEAD and TAIL and put them in the chain
2421 of notes ended by NOTE_LIST. */
2423 remove_notes (rtx head, rtx tail)
2425 rtx next_tail, insn, next;
2428 if (head == tail && !INSN_P (head))
2431 next_tail = NEXT_INSN (tail);
2432 for (insn = head; insn != next_tail; insn = next)
2434 next = NEXT_INSN (insn);
2438 switch (NOTE_KIND (insn))
2440 case NOTE_INSN_BASIC_BLOCK:
2443 case NOTE_INSN_EPILOGUE_BEG:
2447 add_reg_note (next, REG_SAVE_NOTE,
2448 GEN_INT (NOTE_INSN_EPILOGUE_BEG));
2456 /* Add the note to list that ends at NOTE_LIST. */
2457 PREV_INSN (insn) = note_list;
2458 NEXT_INSN (insn) = NULL_RTX;
2460 NEXT_INSN (note_list) = insn;
2465 gcc_assert ((sel_sched_p () || insn != tail) && insn != head);
2469 /* A structure to record enough data to allow us to backtrack the scheduler to
2470 a previous state. */
2471 struct haifa_saved_data
2473 /* Next entry on the list. */
2474 struct haifa_saved_data *next;
2476 /* Backtracking is associated with scheduling insns that have delay slots.
2477 DELAY_PAIR points to the structure that contains the insns involved, and
2478 the number of cycles between them. */
2479 struct delay_pair *delay_pair;
2481 /* Data used by the frontend (e.g. sched-ebb or sched-rgn). */
2482 void *fe_saved_data;
2483 /* Data used by the backend. */
2484 void *be_saved_data;
2486 /* Copies of global state. */
2487 int clock_var, last_clock_var;
2488 struct ready_list ready;
2491 rtx last_scheduled_insn;
2492 rtx last_nondebug_scheduled_insn;
2493 int cycle_issued_insns;
2495 /* Copies of state used in the inner loop of schedule_block. */
2496 struct sched_block_state sched_block;
2498 /* We don't need to save q_ptr, as its value is arbitrary and we can set it
2499 to 0 when restoring. */
2504 /* A record, in reverse order, of all scheduled insns which have delay slots
2505 and may require backtracking. */
2506 static struct haifa_saved_data *backtrack_queue;
2508 /* For every dependency of INSN, set the FEEDS_BACKTRACK_INSN bit according
2511 mark_backtrack_feeds (rtx insn, int set_p)
2513 sd_iterator_def sd_it;
2515 FOR_EACH_DEP (insn, SD_LIST_HARD_BACK, sd_it, dep)
2517 FEEDS_BACKTRACK_INSN (DEP_PRO (dep)) = set_p;
2521 /* Save the current scheduler state so that we can backtrack to it
2522 later if necessary. PAIR gives the insns that make it necessary to
2523 save this point. SCHED_BLOCK is the local state of schedule_block
2524 that need to be saved. */
2526 save_backtrack_point (struct delay_pair *pair,
2527 struct sched_block_state sched_block)
2530 struct haifa_saved_data *save = XNEW (struct haifa_saved_data);
2532 save->curr_state = xmalloc (dfa_state_size);
2533 memcpy (save->curr_state, curr_state, dfa_state_size);
2535 save->ready.first = ready.first;
2536 save->ready.n_ready = ready.n_ready;
2537 save->ready.n_debug = ready.n_debug;
2538 save->ready.veclen = ready.veclen;
2539 save->ready.vec = XNEWVEC (rtx, ready.veclen);
2540 memcpy (save->ready.vec, ready.vec, ready.veclen * sizeof (rtx));
2542 save->insn_queue = XNEWVEC (rtx, max_insn_queue_index + 1);
2543 save->q_size = q_size;
2544 for (i = 0; i <= max_insn_queue_index; i++)
2546 int q = NEXT_Q_AFTER (q_ptr, i);
2547 save->insn_queue[i] = copy_INSN_LIST (insn_queue[q]);
2550 save->clock_var = clock_var;
2551 save->last_clock_var = last_clock_var;
2552 save->cycle_issued_insns = cycle_issued_insns;
2553 save->last_scheduled_insn = last_scheduled_insn;
2554 save->last_nondebug_scheduled_insn = last_nondebug_scheduled_insn;
2556 save->sched_block = sched_block;
2558 if (current_sched_info->save_state)
2559 save->fe_saved_data = (*current_sched_info->save_state) ();
2561 if (targetm.sched.alloc_sched_context)
2563 save->be_saved_data = targetm.sched.alloc_sched_context ();
2564 targetm.sched.init_sched_context (save->be_saved_data, false);
2567 save->be_saved_data = NULL;
2569 save->delay_pair = pair;
2571 save->next = backtrack_queue;
2572 backtrack_queue = save;
2576 mark_backtrack_feeds (pair->i2, 1);
2577 INSN_TICK (pair->i2) = INVALID_TICK;
2578 INSN_EXACT_TICK (pair->i2) = clock_var + pair_delay (pair);
2579 SHADOW_P (pair->i2) = pair->stages == 0;
2580 pair = pair->next_same_i1;
2584 /* Walk the ready list and all queues. If any insns have unresolved backwards
2585 dependencies, these must be cancelled deps, broken by predication. Set or
2586 clear (depending on SET) the DEP_CANCELLED bit in DEP_STATUS. */
2589 toggle_cancelled_flags (bool set)
2592 sd_iterator_def sd_it;
2595 if (ready.n_ready > 0)
2597 rtx *first = ready_lastpos (&ready);
2598 for (i = 0; i < ready.n_ready; i++)
2599 FOR_EACH_DEP (first[i], SD_LIST_BACK, sd_it, dep)
2600 if (!DEBUG_INSN_P (DEP_PRO (dep)))
2603 DEP_STATUS (dep) |= DEP_CANCELLED;
2605 DEP_STATUS (dep) &= ~DEP_CANCELLED;
2608 for (i = 0; i <= max_insn_queue_index; i++)
2610 int q = NEXT_Q_AFTER (q_ptr, i);
2612 for (link = insn_queue[q]; link; link = XEXP (link, 1))
2614 rtx insn = XEXP (link, 0);
2615 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
2616 if (!DEBUG_INSN_P (DEP_PRO (dep)))
2619 DEP_STATUS (dep) |= DEP_CANCELLED;
2621 DEP_STATUS (dep) &= ~DEP_CANCELLED;
2627 /* Pop entries from the SCHEDULED_INSNS vector up to and including INSN.
2628 Restore their dependencies to an unresolved state, and mark them as
2632 unschedule_insns_until (rtx insn)
2634 VEC (rtx, heap) *recompute_vec;
2636 recompute_vec = VEC_alloc (rtx, heap, 0);
2638 /* Make two passes over the insns to be unscheduled. First, we clear out
2639 dependencies and other trivial bookkeeping. */
2643 sd_iterator_def sd_it;
2646 last = VEC_pop (rtx, scheduled_insns);
2648 /* This will be changed by restore_backtrack_point if the insn is in
2650 QUEUE_INDEX (last) = QUEUE_NOWHERE;
2652 INSN_TICK (last) = INVALID_TICK;
2654 if (modulo_ii > 0 && INSN_UID (last) < modulo_iter0_max_uid)
2655 modulo_insns_scheduled--;
2657 for (sd_it = sd_iterator_start (last, SD_LIST_RES_FORW);
2658 sd_iterator_cond (&sd_it, &dep);)
2660 rtx con = DEP_CON (dep);
2661 sd_unresolve_dep (sd_it);
2662 if (!MUST_RECOMPUTE_SPEC_P (con))
2664 MUST_RECOMPUTE_SPEC_P (con) = 1;
2665 VEC_safe_push (rtx, heap, recompute_vec, con);
2673 /* A second pass, to update ready and speculation status for insns
2674 depending on the unscheduled ones. The first pass must have
2675 popped the scheduled_insns vector up to the point where we
2676 restart scheduling, as recompute_todo_spec requires it to be
2678 while (!VEC_empty (rtx, recompute_vec))
2682 con = VEC_pop (rtx, recompute_vec);
2683 MUST_RECOMPUTE_SPEC_P (con) = 0;
2684 if (!sd_lists_empty_p (con, SD_LIST_HARD_BACK))
2686 TODO_SPEC (con) = HARD_DEP;
2687 INSN_TICK (con) = INVALID_TICK;
2688 if (PREDICATED_PAT (con) != NULL_RTX)
2689 haifa_change_pattern (con, ORIG_PAT (con));
2691 else if (QUEUE_INDEX (con) != QUEUE_SCHEDULED)
2692 TODO_SPEC (con) = recompute_todo_spec (con);
2694 VEC_free (rtx, heap, recompute_vec);
2697 /* Restore scheduler state from the topmost entry on the backtracking queue.
2698 PSCHED_BLOCK_P points to the local data of schedule_block that we must
2699 overwrite with the saved data.
2700 The caller must already have called unschedule_insns_until. */
2703 restore_last_backtrack_point (struct sched_block_state *psched_block)
2707 struct haifa_saved_data *save = backtrack_queue;
2709 backtrack_queue = save->next;
2711 if (current_sched_info->restore_state)
2712 (*current_sched_info->restore_state) (save->fe_saved_data);
2714 if (targetm.sched.alloc_sched_context)
2716 targetm.sched.set_sched_context (save->be_saved_data);
2717 targetm.sched.free_sched_context (save->be_saved_data);
2720 /* Clear the QUEUE_INDEX of everything in the ready list or one
2722 if (ready.n_ready > 0)
2724 rtx *first = ready_lastpos (&ready);
2725 for (i = 0; i < ready.n_ready; i++)
2727 rtx insn = first[i];
2728 QUEUE_INDEX (insn) = QUEUE_NOWHERE;
2729 INSN_TICK (insn) = INVALID_TICK;
2732 for (i = 0; i <= max_insn_queue_index; i++)
2734 int q = NEXT_Q_AFTER (q_ptr, i);
2736 for (link = insn_queue[q]; link; link = XEXP (link, 1))
2738 rtx x = XEXP (link, 0);
2739 QUEUE_INDEX (x) = QUEUE_NOWHERE;
2740 INSN_TICK (x) = INVALID_TICK;
2742 free_INSN_LIST_list (&insn_queue[q]);
2746 ready = save->ready;
2748 if (ready.n_ready > 0)
2750 rtx *first = ready_lastpos (&ready);
2751 for (i = 0; i < ready.n_ready; i++)
2753 rtx insn = first[i];
2754 QUEUE_INDEX (insn) = QUEUE_READY;
2755 TODO_SPEC (insn) = recompute_todo_spec (insn);
2756 INSN_TICK (insn) = save->clock_var;
2761 q_size = save->q_size;
2762 for (i = 0; i <= max_insn_queue_index; i++)
2764 int q = NEXT_Q_AFTER (q_ptr, i);
2766 insn_queue[q] = save->insn_queue[q];
2768 for (link = insn_queue[q]; link; link = XEXP (link, 1))
2770 rtx x = XEXP (link, 0);
2771 QUEUE_INDEX (x) = i;
2772 TODO_SPEC (x) = recompute_todo_spec (x);
2773 INSN_TICK (x) = save->clock_var + i;
2776 free (save->insn_queue);
2778 toggle_cancelled_flags (true);
2780 clock_var = save->clock_var;
2781 last_clock_var = save->last_clock_var;
2782 cycle_issued_insns = save->cycle_issued_insns;
2783 last_scheduled_insn = save->last_scheduled_insn;
2784 last_nondebug_scheduled_insn = save->last_nondebug_scheduled_insn;
2786 *psched_block = save->sched_block;
2788 memcpy (curr_state, save->curr_state, dfa_state_size);
2789 free (save->curr_state);
2791 mark_backtrack_feeds (save->delay_pair->i2, 0);
2795 for (save = backtrack_queue; save; save = save->next)
2797 mark_backtrack_feeds (save->delay_pair->i2, 1);
2801 /* Discard all data associated with the topmost entry in the backtrack
2802 queue. If RESET_TICK is false, we just want to free the data. If true,
2803 we are doing this because we discovered a reason to backtrack. In the
2804 latter case, also reset the INSN_TICK for the shadow insn. */
2806 free_topmost_backtrack_point (bool reset_tick)
2808 struct haifa_saved_data *save = backtrack_queue;
2811 backtrack_queue = save->next;
2815 struct delay_pair *pair = save->delay_pair;
2818 INSN_TICK (pair->i2) = INVALID_TICK;
2819 INSN_EXACT_TICK (pair->i2) = INVALID_TICK;
2820 pair = pair->next_same_i1;
2823 if (targetm.sched.free_sched_context)
2824 targetm.sched.free_sched_context (save->be_saved_data);
2825 if (current_sched_info->restore_state)
2826 free (save->fe_saved_data);
2827 for (i = 0; i <= max_insn_queue_index; i++)
2828 free_INSN_LIST_list (&save->insn_queue[i]);
2829 free (save->insn_queue);
2830 free (save->curr_state);
2831 free (save->ready.vec);
2835 /* Free the entire backtrack queue. */
2837 free_backtrack_queue (void)
2839 while (backtrack_queue)
2840 free_topmost_backtrack_point (false);
2843 /* Compute INSN_TICK_ESTIMATE for INSN. PROCESSED is a bitmap of
2844 instructions we've previously encountered, a set bit prevents
2845 recursion. BUDGET is a limit on how far ahead we look, it is
2846 reduced on recursive calls. Return true if we produced a good
2847 estimate, or false if we exceeded the budget. */
2849 estimate_insn_tick (bitmap processed, rtx insn, int budget)
2851 sd_iterator_def sd_it;
2853 int earliest = INSN_TICK (insn);
2855 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
2857 rtx pro = DEP_PRO (dep);
2860 if (DEP_STATUS (dep) & DEP_CANCELLED)
2863 if (QUEUE_INDEX (pro) == QUEUE_SCHEDULED)
2864 gcc_assert (INSN_TICK (pro) + dep_cost (dep) <= INSN_TICK (insn));
2867 int cost = dep_cost (dep);
2870 if (!bitmap_bit_p (processed, INSN_LUID (pro)))
2872 if (!estimate_insn_tick (processed, pro, budget - cost))
2875 gcc_assert (INSN_TICK_ESTIMATE (pro) != INVALID_TICK);
2876 t = INSN_TICK_ESTIMATE (pro) + cost;
2877 if (earliest == INVALID_TICK || t > earliest)
2881 bitmap_set_bit (processed, INSN_LUID (insn));
2882 INSN_TICK_ESTIMATE (insn) = earliest;
2886 /* Examine the pair of insns in P, and estimate (optimistically, assuming
2887 infinite resources) the cycle in which the delayed shadow can be issued.
2888 Return the number of cycles that must pass before the real insn can be
2889 issued in order to meet this constraint. */
2891 estimate_shadow_tick (struct delay_pair *p)
2893 bitmap_head processed;
2896 bitmap_initialize (&processed, 0);
2898 cutoff = !estimate_insn_tick (&processed, p->i2,
2899 max_insn_queue_index + pair_delay (p));
2900 bitmap_clear (&processed);
2902 return max_insn_queue_index;
2903 t = INSN_TICK_ESTIMATE (p->i2) - (clock_var + pair_delay (p) + 1);
2909 /* If INSN has no unresolved backwards dependencies, add it to the schedule and
2910 recursively resolve all its forward dependencies. */
2912 resolve_dependencies (rtx insn)
2914 sd_iterator_def sd_it;
2917 /* Don't use sd_lists_empty_p; it ignores debug insns. */
2918 if (DEPS_LIST_FIRST (INSN_HARD_BACK_DEPS (insn)) != NULL
2919 || DEPS_LIST_FIRST (INSN_SPEC_BACK_DEPS (insn)) != NULL)
2922 if (sched_verbose >= 4)
2923 fprintf (sched_dump, ";;\tquickly resolving %d\n", INSN_UID (insn));
2925 if (QUEUE_INDEX (insn) >= 0)
2926 queue_remove (insn);
2928 VEC_safe_push (rtx, heap, scheduled_insns, insn);
2930 /* Update dependent instructions. */
2931 for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
2932 sd_iterator_cond (&sd_it, &dep);)
2934 rtx next = DEP_CON (dep);
2936 if (sched_verbose >= 4)
2937 fprintf (sched_dump, ";;\t\tdep %d against %d\n", INSN_UID (insn),
2940 /* Resolve the dependence between INSN and NEXT.
2941 sd_resolve_dep () moves current dep to another list thus
2942 advancing the iterator. */
2943 sd_resolve_dep (sd_it);
2945 if (!IS_SPECULATION_BRANCHY_CHECK_P (insn))
2947 resolve_dependencies (next);
2950 /* Check always has only one forward dependence (to the first insn in
2951 the recovery block), therefore, this will be executed only once. */
2953 gcc_assert (sd_lists_empty_p (insn, SD_LIST_FORW));
2959 /* Return the head and tail pointers of ebb starting at BEG and ending
2962 get_ebb_head_tail (basic_block beg, basic_block end, rtx *headp, rtx *tailp)
2964 rtx beg_head = BB_HEAD (beg);
2965 rtx beg_tail = BB_END (beg);
2966 rtx end_head = BB_HEAD (end);
2967 rtx end_tail = BB_END (end);
2969 /* Don't include any notes or labels at the beginning of the BEG
2970 basic block, or notes at the end of the END basic blocks. */
2972 if (LABEL_P (beg_head))
2973 beg_head = NEXT_INSN (beg_head);
2975 while (beg_head != beg_tail)
2976 if (NOTE_P (beg_head))
2977 beg_head = NEXT_INSN (beg_head);
2978 else if (DEBUG_INSN_P (beg_head))
2982 for (note = NEXT_INSN (beg_head);
2986 next = NEXT_INSN (note);
2989 if (sched_verbose >= 9)
2990 fprintf (sched_dump, "reorder %i\n", INSN_UID (note));
2992 reorder_insns_nobb (note, note, PREV_INSN (beg_head));
2994 if (BLOCK_FOR_INSN (note) != beg)
2995 df_insn_change_bb (note, beg);
2997 else if (!DEBUG_INSN_P (note))
3009 end_head = beg_head;
3010 else if (LABEL_P (end_head))
3011 end_head = NEXT_INSN (end_head);
3013 while (end_head != end_tail)
3014 if (NOTE_P (end_tail))
3015 end_tail = PREV_INSN (end_tail);
3016 else if (DEBUG_INSN_P (end_tail))
3020 for (note = PREV_INSN (end_tail);
3024 prev = PREV_INSN (note);
3027 if (sched_verbose >= 9)
3028 fprintf (sched_dump, "reorder %i\n", INSN_UID (note));
3030 reorder_insns_nobb (note, note, end_tail);
3032 if (end_tail == BB_END (end))
3033 BB_END (end) = note;
3035 if (BLOCK_FOR_INSN (note) != end)
3036 df_insn_change_bb (note, end);
3038 else if (!DEBUG_INSN_P (note))
3050 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ]. */
3053 no_real_insns_p (const_rtx head, const_rtx tail)
3055 while (head != NEXT_INSN (tail))
3057 if (!NOTE_P (head) && !LABEL_P (head))
3059 head = NEXT_INSN (head);
3064 /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
3065 previously found among the insns. Insert them just before HEAD. */
3067 restore_other_notes (rtx head, basic_block head_bb)
3071 rtx note_head = note_list;
3074 head_bb = BLOCK_FOR_INSN (head);
3076 head = NEXT_INSN (bb_note (head_bb));
3078 while (PREV_INSN (note_head))
3080 set_block_for_insn (note_head, head_bb);
3081 note_head = PREV_INSN (note_head);
3083 /* In the above cycle we've missed this note. */
3084 set_block_for_insn (note_head, head_bb);
3086 PREV_INSN (note_head) = PREV_INSN (head);
3087 NEXT_INSN (PREV_INSN (head)) = note_head;
3088 PREV_INSN (head) = note_list;
3089 NEXT_INSN (note_list) = head;
3091 if (BLOCK_FOR_INSN (head) != head_bb)
3092 BB_END (head_bb) = note_list;
3100 /* Move insns that became ready to fire from queue to ready list. */
3103 queue_to_ready (struct ready_list *ready)
3109 q_ptr = NEXT_Q (q_ptr);
3111 if (dbg_cnt (sched_insn) == false)
3113 /* If debug counter is activated do not requeue the first
3114 nonscheduled insn. */
3115 skip_insn = nonscheduled_insns_begin;
3118 skip_insn = next_nonnote_nondebug_insn (skip_insn);
3120 while (QUEUE_INDEX (skip_insn) == QUEUE_SCHEDULED);
3123 skip_insn = NULL_RTX;
3125 /* Add all pending insns that can be scheduled without stalls to the
3127 for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
3129 insn = XEXP (link, 0);
3132 if (sched_verbose >= 2)
3133 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
3134 (*current_sched_info->print_insn) (insn, 0));
3136 /* If the ready list is full, delay the insn for 1 cycle.
3137 See the comment in schedule_block for the rationale. */
3138 if (!reload_completed
3139 && ready->n_ready - ready->n_debug > MAX_SCHED_READY_INSNS
3140 && !SCHED_GROUP_P (insn)
3141 && insn != skip_insn)
3142 queue_insn (insn, 1, "ready full");
3145 ready_add (ready, insn, false);
3146 if (sched_verbose >= 2)
3147 fprintf (sched_dump, "moving to ready without stalls\n");
3150 free_INSN_LIST_list (&insn_queue[q_ptr]);
3152 /* If there are no ready insns, stall until one is ready and add all
3153 of the pending insns at that point to the ready list. */
3154 if (ready->n_ready == 0)
3158 for (stalls = 1; stalls <= max_insn_queue_index; stalls++)
3160 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
3162 for (; link; link = XEXP (link, 1))
3164 insn = XEXP (link, 0);
3167 if (sched_verbose >= 2)
3168 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
3169 (*current_sched_info->print_insn) (insn, 0));
3171 ready_add (ready, insn, false);
3172 if (sched_verbose >= 2)
3173 fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
3175 free_INSN_LIST_list (&insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]);
3177 advance_one_cycle ();
3182 advance_one_cycle ();
3185 q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
3186 clock_var += stalls;
3190 /* Used by early_queue_to_ready. Determines whether it is "ok" to
3191 prematurely move INSN from the queue to the ready list. Currently,
3192 if a target defines the hook 'is_costly_dependence', this function
3193 uses the hook to check whether there exist any dependences which are
3194 considered costly by the target, between INSN and other insns that
3195 have already been scheduled. Dependences are checked up to Y cycles
3196 back, with default Y=1; The flag -fsched-stalled-insns-dep=Y allows
3197 controlling this value.
3198 (Other considerations could be taken into account instead (or in
3199 addition) depending on user flags and target hooks. */
3202 ok_for_early_queue_removal (rtx insn)
3204 if (targetm.sched.is_costly_dependence)
3208 int i = VEC_length (rtx, scheduled_insns);
3209 for (n_cycles = flag_sched_stalled_insns_dep; n_cycles; n_cycles--)
3215 prev_insn = VEC_index (rtx, scheduled_insns, i);
3217 if (!NOTE_P (prev_insn))
3221 dep = sd_find_dep_between (prev_insn, insn, true);
3225 cost = dep_cost (dep);
3227 if (targetm.sched.is_costly_dependence (dep, cost,
3228 flag_sched_stalled_insns_dep - n_cycles))
3233 if (GET_MODE (prev_insn) == TImode) /* end of dispatch group */
3246 /* Remove insns from the queue, before they become "ready" with respect
3247 to FU latency considerations. */
3250 early_queue_to_ready (state_t state, struct ready_list *ready)
3258 state_t temp_state = alloca (dfa_state_size);
3260 int insns_removed = 0;
3263 Flag '-fsched-stalled-insns=X' determines the aggressiveness of this
3266 X == 0: There is no limit on how many queued insns can be removed
3267 prematurely. (flag_sched_stalled_insns = -1).
3269 X >= 1: Only X queued insns can be removed prematurely in each
3270 invocation. (flag_sched_stalled_insns = X).
3272 Otherwise: Early queue removal is disabled.
3273 (flag_sched_stalled_insns = 0)
3276 if (! flag_sched_stalled_insns)
3279 for (stalls = 0; stalls <= max_insn_queue_index; stalls++)
3281 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
3283 if (sched_verbose > 6)
3284 fprintf (sched_dump, ";; look at index %d + %d\n", q_ptr, stalls);
3289 next_link = XEXP (link, 1);
3290 insn = XEXP (link, 0);
3291 if (insn && sched_verbose > 6)
3292 print_rtl_single (sched_dump, insn);
3294 memcpy (temp_state, state, dfa_state_size);
3295 if (recog_memoized (insn) < 0)
3296 /* non-negative to indicate that it's not ready
3297 to avoid infinite Q->R->Q->R... */
3300 cost = state_transition (temp_state, insn);
3302 if (sched_verbose >= 6)
3303 fprintf (sched_dump, "transition cost = %d\n", cost);
3305 move_to_ready = false;
3308 move_to_ready = ok_for_early_queue_removal (insn);
3309 if (move_to_ready == true)
3311 /* move from Q to R */
3313 ready_add (ready, insn, false);
3316 XEXP (prev_link, 1) = next_link;
3318 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = next_link;
3320 free_INSN_LIST_node (link);
3322 if (sched_verbose >= 2)
3323 fprintf (sched_dump, ";;\t\tEarly Q-->Ready: insn %s\n",
3324 (*current_sched_info->print_insn) (insn, 0));
3327 if (insns_removed == flag_sched_stalled_insns)
3328 /* Remove no more than flag_sched_stalled_insns insns
3329 from Q at a time. */
3330 return insns_removed;
3334 if (move_to_ready == false)
3341 } /* for stalls.. */
3343 return insns_removed;
3347 /* Print the ready list for debugging purposes. Callable from debugger. */
3350 debug_ready_list (struct ready_list *ready)
3355 if (ready->n_ready == 0)
3357 fprintf (sched_dump, "\n");
3361 p = ready_lastpos (ready);
3362 for (i = 0; i < ready->n_ready; i++)
3364 fprintf (sched_dump, " %s:%d",
3365 (*current_sched_info->print_insn) (p[i], 0),
3367 if (sched_pressure_p)
3368 fprintf (sched_dump, "(cost=%d",
3369 INSN_REG_PRESSURE_EXCESS_COST_CHANGE (p[i]));
3370 if (INSN_TICK (p[i]) > clock_var)
3371 fprintf (sched_dump, ":delay=%d", INSN_TICK (p[i]) - clock_var);
3372 if (sched_pressure_p)
3373 fprintf (sched_dump, ")");
3375 fprintf (sched_dump, "\n");
3378 /* Search INSN for REG_SAVE_NOTE notes and convert them back into insn
3379 NOTEs. This is used for NOTE_INSN_EPILOGUE_BEG, so that sched-ebb
3380 replaces the epilogue note in the correct basic block. */
3382 reemit_notes (rtx insn)
3384 rtx note, last = insn;
3386 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3388 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
3390 enum insn_note note_type = (enum insn_note) INTVAL (XEXP (note, 0));
3392 last = emit_note_before (note_type, last);
3393 remove_note (insn, note);
3398 /* Move INSN. Reemit notes if needed. Update CFG, if needed. */
3400 move_insn (rtx insn, rtx last, rtx nt)
3402 if (PREV_INSN (insn) != last)
3408 bb = BLOCK_FOR_INSN (insn);
3410 /* BB_HEAD is either LABEL or NOTE. */
3411 gcc_assert (BB_HEAD (bb) != insn);
3413 if (BB_END (bb) == insn)
3414 /* If this is last instruction in BB, move end marker one
3417 /* Jumps are always placed at the end of basic block. */
3418 jump_p = control_flow_insn_p (insn);
3421 || ((common_sched_info->sched_pass_id == SCHED_RGN_PASS)
3422 && IS_SPECULATION_BRANCHY_CHECK_P (insn))
3423 || (common_sched_info->sched_pass_id
3424 == SCHED_EBB_PASS));
3426 gcc_assert (BLOCK_FOR_INSN (PREV_INSN (insn)) == bb);
3428 BB_END (bb) = PREV_INSN (insn);
3431 gcc_assert (BB_END (bb) != last);
3434 /* We move the block note along with jump. */
3438 note = NEXT_INSN (insn);
3439 while (NOTE_NOT_BB_P (note) && note != nt)
3440 note = NEXT_INSN (note);
3444 || BARRIER_P (note)))
3445 note = NEXT_INSN (note);
3447 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
3452 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (note);
3453 PREV_INSN (NEXT_INSN (note)) = PREV_INSN (insn);
3455 NEXT_INSN (note) = NEXT_INSN (last);
3456 PREV_INSN (NEXT_INSN (last)) = note;
3458 NEXT_INSN (last) = insn;
3459 PREV_INSN (insn) = last;
3461 bb = BLOCK_FOR_INSN (last);
3465 fix_jump_move (insn);
3467 if (BLOCK_FOR_INSN (insn) != bb)
3468 move_block_after_check (insn);
3470 gcc_assert (BB_END (bb) == last);
3473 df_insn_change_bb (insn, bb);
3475 /* Update BB_END, if needed. */
3476 if (BB_END (bb) == last)
3480 SCHED_GROUP_P (insn) = 0;
3483 /* Return true if scheduling INSN will finish current clock cycle. */
3485 insn_finishes_cycle_p (rtx insn)
3487 if (SCHED_GROUP_P (insn))
3488 /* After issuing INSN, rest of the sched_group will be forced to issue
3489 in order. Don't make any plans for the rest of cycle. */
3492 /* Finishing the block will, apparently, finish the cycle. */
3493 if (current_sched_info->insn_finishes_block_p
3494 && current_sched_info->insn_finishes_block_p (insn))
3500 /* Define type for target data used in multipass scheduling. */
3501 #ifndef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DATA_T
3502 # define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DATA_T int
3504 typedef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DATA_T first_cycle_multipass_data_t;
3506 /* The following structure describe an entry of the stack of choices. */
3509 /* Ordinal number of the issued insn in the ready queue. */
3511 /* The number of the rest insns whose issues we should try. */
3513 /* The number of issued essential insns. */
3515 /* State after issuing the insn. */
3517 /* Target-specific data. */
3518 first_cycle_multipass_data_t target_data;
3521 /* The following array is used to implement a stack of choices used in
3522 function max_issue. */
3523 static struct choice_entry *choice_stack;
3525 /* This holds the value of the target dfa_lookahead hook. */
3528 /* The following variable value is maximal number of tries of issuing
3529 insns for the first cycle multipass insn scheduling. We define
3530 this value as constant*(DFA_LOOKAHEAD**ISSUE_RATE). We would not
3531 need this constraint if all real insns (with non-negative codes)
3532 had reservations because in this case the algorithm complexity is
3533 O(DFA_LOOKAHEAD**ISSUE_RATE). Unfortunately, the dfa descriptions
3534 might be incomplete and such insn might occur. For such
3535 descriptions, the complexity of algorithm (without the constraint)
3536 could achieve DFA_LOOKAHEAD ** N , where N is the queue length. */
3537 static int max_lookahead_tries;
3539 /* The following value is value of hook
3540 `first_cycle_multipass_dfa_lookahead' at the last call of
3542 static int cached_first_cycle_multipass_dfa_lookahead = 0;
3544 /* The following value is value of `issue_rate' at the last call of
3546 static int cached_issue_rate = 0;
3548 /* The following function returns maximal (or close to maximal) number
3549 of insns which can be issued on the same cycle and one of which
3550 insns is insns with the best rank (the first insn in READY). To
3551 make this function tries different samples of ready insns. READY
3552 is current queue `ready'. Global array READY_TRY reflects what
3553 insns are already issued in this try. The function stops immediately,
3554 if it reached the such a solution, that all instruction can be issued.
3555 INDEX will contain index of the best insn in READY. The following
3556 function is used only for first cycle multipass scheduling.
3560 This function expects recognized insns only. All USEs,
3561 CLOBBERs, etc must be filtered elsewhere. */
3563 max_issue (struct ready_list *ready, int privileged_n, state_t state,
3564 bool first_cycle_insn_p, int *index)
3566 int n, i, all, n_ready, best, delay, tries_num;
3568 struct choice_entry *top;
3571 n_ready = ready->n_ready;
3572 gcc_assert (dfa_lookahead >= 1 && privileged_n >= 0
3573 && privileged_n <= n_ready);
3575 /* Init MAX_LOOKAHEAD_TRIES. */
3576 if (cached_first_cycle_multipass_dfa_lookahead != dfa_lookahead)
3578 cached_first_cycle_multipass_dfa_lookahead = dfa_lookahead;
3579 max_lookahead_tries = 100;
3580 for (i = 0; i < issue_rate; i++)
3581 max_lookahead_tries *= dfa_lookahead;
3584 /* Init max_points. */
3585 more_issue = issue_rate - cycle_issued_insns;
3586 gcc_assert (more_issue >= 0);