PR target/49486
[official-gcc.git] / gcc / haifa-sched.c
blob34a692fd712aade89cd6e760d33788966e07a82b
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
13 version.
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
18 for more details.
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
55 remaining slots.
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
61 broken by
62 2. choose insn with least contribution to register pressure,
63 ties broken by
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
67 broken by
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
96 utilization.
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
101 of this case.
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. */
127 #include "config.h"
128 #include "system.h"
129 #include "coretypes.h"
130 #include "tm.h"
131 #include "diagnostic-core.h"
132 #include "rtl.h"
133 #include "tm_p.h"
134 #include "hard-reg-set.h"
135 #include "regs.h"
136 #include "function.h"
137 #include "flags.h"
138 #include "insn-config.h"
139 #include "insn-attr.h"
140 #include "except.h"
141 #include "recog.h"
142 #include "sched-int.h"
143 #include "target.h"
144 #include "common/common-target.h"
145 #include "output.h"
146 #include "params.h"
147 #include "vecprim.h"
148 #include "dbgcnt.h"
149 #include "cfgloop.h"
150 #include "ira.h"
151 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
152 #include "hashtab.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. */
160 int issue_rate;
162 /* This can be set to true by a backend if the scheduler should not
163 enable a DCE pass. */
164 bool sched_no_dce;
166 /* sched-verbose controls the amount of debugging output the
167 scheduler prints. It is controlled by -fsched-verbose=N:
168 N>0 and no -DSR : the output is directed to stderr.
169 N>=10 will direct the printouts to stderr (regardless of -dSR).
170 N=1: same as -dSR.
171 N=2: bb's probabilities, detailed ready list info, unit/insn info.
172 N=3: rtl at abort point, control-flow, regions info.
173 N=5: dependences info. */
175 int sched_verbose = 0;
177 /* Debugging file. All printouts are sent to dump, which is always set,
178 either to stderr, or to the dump listing file (-dRS). */
179 FILE *sched_dump = 0;
181 /* This is a placeholder for the scheduler parameters common
182 to all schedulers. */
183 struct common_sched_info_def *common_sched_info;
185 #define INSN_TICK(INSN) (HID (INSN)->tick)
186 #define INSN_EXACT_TICK(INSN) (HID (INSN)->exact_tick)
187 #define INSN_TICK_ESTIMATE(INSN) (HID (INSN)->tick_estimate)
188 #define INTER_TICK(INSN) (HID (INSN)->inter_tick)
189 #define FEEDS_BACKTRACK_INSN(INSN) (HID (INSN)->feeds_backtrack_insn)
190 #define SHADOW_P(INSN) (HID (INSN)->shadow_p)
192 /* If INSN_TICK of an instruction is equal to INVALID_TICK,
193 then it should be recalculated from scratch. */
194 #define INVALID_TICK (-(max_insn_queue_index + 1))
195 /* The minimal value of the INSN_TICK of an instruction. */
196 #define MIN_TICK (-max_insn_queue_index)
198 /* List of important notes we must keep around. This is a pointer to the
199 last element in the list. */
200 rtx note_list;
202 static struct spec_info_def spec_info_var;
203 /* Description of the speculative part of the scheduling.
204 If NULL - no speculation. */
205 spec_info_t spec_info = NULL;
207 /* True, if recovery block was added during scheduling of current block.
208 Used to determine, if we need to fix INSN_TICKs. */
209 static bool haifa_recovery_bb_recently_added_p;
211 /* True, if recovery block was added during this scheduling pass.
212 Used to determine if we should have empty memory pools of dependencies
213 after finishing current region. */
214 bool haifa_recovery_bb_ever_added_p;
216 /* Counters of different types of speculative instructions. */
217 static int nr_begin_data, nr_be_in_data, nr_begin_control, nr_be_in_control;
219 /* Array used in {unlink, restore}_bb_notes. */
220 static rtx *bb_header = 0;
222 /* Basic block after which recovery blocks will be created. */
223 static basic_block before_recovery;
225 /* Basic block just before the EXIT_BLOCK and after recovery, if we have
226 created it. */
227 basic_block after_recovery;
229 /* FALSE if we add bb to another region, so we don't need to initialize it. */
230 bool adding_bb_to_current_region_p = true;
232 /* Queues, etc. */
234 /* An instruction is ready to be scheduled when all insns preceding it
235 have already been scheduled. It is important to ensure that all
236 insns which use its result will not be executed until its result
237 has been computed. An insn is maintained in one of four structures:
239 (P) the "Pending" set of insns which cannot be scheduled until
240 their dependencies have been satisfied.
241 (Q) the "Queued" set of insns that can be scheduled when sufficient
242 time has passed.
243 (R) the "Ready" list of unscheduled, uncommitted insns.
244 (S) the "Scheduled" list of insns.
246 Initially, all insns are either "Pending" or "Ready" depending on
247 whether their dependencies are satisfied.
249 Insns move from the "Ready" list to the "Scheduled" list as they
250 are committed to the schedule. As this occurs, the insns in the
251 "Pending" list have their dependencies satisfied and move to either
252 the "Ready" list or the "Queued" set depending on whether
253 sufficient time has passed to make them ready. As time passes,
254 insns move from the "Queued" set to the "Ready" list.
256 The "Pending" list (P) are the insns in the INSN_FORW_DEPS of the
257 unscheduled insns, i.e., those that are ready, queued, and pending.
258 The "Queued" set (Q) is implemented by the variable `insn_queue'.
259 The "Ready" list (R) is implemented by the variables `ready' and
260 `n_ready'.
261 The "Scheduled" list (S) is the new insn chain built by this pass.
263 The transition (R->S) is implemented in the scheduling loop in
264 `schedule_block' when the best insn to schedule is chosen.
265 The transitions (P->R and P->Q) are implemented in `schedule_insn' as
266 insns move from the ready list to the scheduled list.
267 The transition (Q->R) is implemented in 'queue_to_insn' as time
268 passes or stalls are introduced. */
270 /* Implement a circular buffer to delay instructions until sufficient
271 time has passed. For the new pipeline description interface,
272 MAX_INSN_QUEUE_INDEX is a power of two minus one which is not less
273 than maximal time of instruction execution computed by genattr.c on
274 the base maximal time of functional unit reservations and getting a
275 result. This is the longest time an insn may be queued. */
277 static rtx *insn_queue;
278 static int q_ptr = 0;
279 static int q_size = 0;
280 #define NEXT_Q(X) (((X)+1) & max_insn_queue_index)
281 #define NEXT_Q_AFTER(X, C) (((X)+C) & max_insn_queue_index)
283 #define QUEUE_SCHEDULED (-3)
284 #define QUEUE_NOWHERE (-2)
285 #define QUEUE_READY (-1)
286 /* QUEUE_SCHEDULED - INSN is scheduled.
287 QUEUE_NOWHERE - INSN isn't scheduled yet and is neither in
288 queue or ready list.
289 QUEUE_READY - INSN is in ready list.
290 N >= 0 - INSN queued for X [where NEXT_Q_AFTER (q_ptr, X) == N] cycles. */
292 #define QUEUE_INDEX(INSN) (HID (INSN)->queue_index)
294 /* The following variable value refers for all current and future
295 reservations of the processor units. */
296 state_t curr_state;
298 /* The following variable value is size of memory representing all
299 current and future reservations of the processor units. */
300 size_t dfa_state_size;
302 /* The following array is used to find the best insn from ready when
303 the automaton pipeline interface is used. */
304 char *ready_try = NULL;
306 /* The ready list. */
307 struct ready_list ready = {NULL, 0, 0, 0, 0};
309 /* The pointer to the ready list (to be removed). */
310 static struct ready_list *readyp = &ready;
312 /* Scheduling clock. */
313 static int clock_var;
315 /* Clock at which the previous instruction was issued. */
316 static int last_clock_var;
318 /* Set to true if, when queuing a shadow insn, we discover that it would be
319 scheduled too late. */
320 static bool must_backtrack;
322 /* The following variable value is number of essential insns issued on
323 the current cycle. An insn is essential one if it changes the
324 processors state. */
325 int cycle_issued_insns;
327 /* This records the actual schedule. It is built up during the main phase
328 of schedule_block, and afterwards used to reorder the insns in the RTL. */
329 static VEC(rtx, heap) *scheduled_insns;
331 static int may_trap_exp (const_rtx, int);
333 /* Nonzero iff the address is comprised from at most 1 register. */
334 #define CONST_BASED_ADDRESS_P(x) \
335 (REG_P (x) \
336 || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS \
337 || (GET_CODE (x) == LO_SUM)) \
338 && (CONSTANT_P (XEXP (x, 0)) \
339 || CONSTANT_P (XEXP (x, 1)))))
341 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
342 as found by analyzing insn's expression. */
345 static int haifa_luid_for_non_insn (rtx x);
347 /* Haifa version of sched_info hooks common to all headers. */
348 const struct common_sched_info_def haifa_common_sched_info =
350 NULL, /* fix_recovery_cfg */
351 NULL, /* add_block */
352 NULL, /* estimate_number_of_insns */
353 haifa_luid_for_non_insn, /* luid_for_non_insn */
354 SCHED_PASS_UNKNOWN /* sched_pass_id */
357 /* Mapping from instruction UID to its Logical UID. */
358 VEC (int, heap) *sched_luids = NULL;
360 /* Next LUID to assign to an instruction. */
361 int sched_max_luid = 1;
363 /* Haifa Instruction Data. */
364 VEC (haifa_insn_data_def, heap) *h_i_d = NULL;
366 void (* sched_init_only_bb) (basic_block, basic_block);
368 /* Split block function. Different schedulers might use different functions
369 to handle their internal data consistent. */
370 basic_block (* sched_split_block) (basic_block, rtx);
372 /* Create empty basic block after the specified block. */
373 basic_block (* sched_create_empty_bb) (basic_block);
375 static int
376 may_trap_exp (const_rtx x, int is_store)
378 enum rtx_code code;
380 if (x == 0)
381 return TRAP_FREE;
382 code = GET_CODE (x);
383 if (is_store)
385 if (code == MEM && may_trap_p (x))
386 return TRAP_RISKY;
387 else
388 return TRAP_FREE;
390 if (code == MEM)
392 /* The insn uses memory: a volatile load. */
393 if (MEM_VOLATILE_P (x))
394 return IRISKY;
395 /* An exception-free load. */
396 if (!may_trap_p (x))
397 return IFREE;
398 /* A load with 1 base register, to be further checked. */
399 if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
400 return PFREE_CANDIDATE;
401 /* No info on the load, to be further checked. */
402 return PRISKY_CANDIDATE;
404 else
406 const char *fmt;
407 int i, insn_class = TRAP_FREE;
409 /* Neither store nor load, check if it may cause a trap. */
410 if (may_trap_p (x))
411 return TRAP_RISKY;
412 /* Recursive step: walk the insn... */
413 fmt = GET_RTX_FORMAT (code);
414 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
416 if (fmt[i] == 'e')
418 int tmp_class = may_trap_exp (XEXP (x, i), is_store);
419 insn_class = WORST_CLASS (insn_class, tmp_class);
421 else if (fmt[i] == 'E')
423 int j;
424 for (j = 0; j < XVECLEN (x, i); j++)
426 int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
427 insn_class = WORST_CLASS (insn_class, tmp_class);
428 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
429 break;
432 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
433 break;
435 return insn_class;
439 /* Classifies rtx X of an insn for the purpose of verifying that X can be
440 executed speculatively (and consequently the insn can be moved
441 speculatively), by examining X, returning:
442 TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
443 TRAP_FREE: non-load insn.
444 IFREE: load from a globally safe location.
445 IRISKY: volatile load.
446 PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
447 being either PFREE or PRISKY. */
449 static int
450 haifa_classify_rtx (const_rtx x)
452 int tmp_class = TRAP_FREE;
453 int insn_class = TRAP_FREE;
454 enum rtx_code code;
456 if (GET_CODE (x) == PARALLEL)
458 int i, len = XVECLEN (x, 0);
460 for (i = len - 1; i >= 0; i--)
462 tmp_class = haifa_classify_rtx (XVECEXP (x, 0, i));
463 insn_class = WORST_CLASS (insn_class, tmp_class);
464 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
465 break;
468 else
470 code = GET_CODE (x);
471 switch (code)
473 case CLOBBER:
474 /* Test if it is a 'store'. */
475 tmp_class = may_trap_exp (XEXP (x, 0), 1);
476 break;
477 case SET:
478 /* Test if it is a store. */
479 tmp_class = may_trap_exp (SET_DEST (x), 1);
480 if (tmp_class == TRAP_RISKY)
481 break;
482 /* Test if it is a load. */
483 tmp_class =
484 WORST_CLASS (tmp_class,
485 may_trap_exp (SET_SRC (x), 0));
486 break;
487 case COND_EXEC:
488 tmp_class = haifa_classify_rtx (COND_EXEC_CODE (x));
489 if (tmp_class == TRAP_RISKY)
490 break;
491 tmp_class = WORST_CLASS (tmp_class,
492 may_trap_exp (COND_EXEC_TEST (x), 0));
493 break;
494 case TRAP_IF:
495 tmp_class = TRAP_RISKY;
496 break;
497 default:;
499 insn_class = tmp_class;
502 return insn_class;
506 haifa_classify_insn (const_rtx insn)
508 return haifa_classify_rtx (PATTERN (insn));
511 /* A structure to record a pair of insns where the first one is a real
512 insn that has delay slots, and the second is its delayed shadow.
513 I1 is scheduled normally and will emit an assembly instruction,
514 while I2 describes the side effect that takes place at the
515 transition between cycles CYCLES and (CYCLES + 1) after I1. */
516 struct delay_pair
518 struct delay_pair *next_same_i1;
519 rtx i1, i2;
520 int cycles;
523 /* Two hash tables to record delay_pairs, one indexed by I1 and the other
524 indexed by I2. */
525 static htab_t delay_htab;
526 static htab_t delay_htab_i2;
528 /* Returns a hash value for X (which really is a delay_pair), based on
529 hashing just I1. */
530 static hashval_t
531 delay_hash_i1 (const void *x)
533 return htab_hash_pointer (((const struct delay_pair *) x)->i1);
536 /* Returns a hash value for X (which really is a delay_pair), based on
537 hashing just I2. */
538 static hashval_t
539 delay_hash_i2 (const void *x)
541 return htab_hash_pointer (((const struct delay_pair *) x)->i2);
544 /* Return nonzero if I1 of pair X is the same as that of pair Y. */
545 static int
546 delay_i1_eq (const void *x, const void *y)
548 return ((const struct delay_pair *) x)->i1 == y;
551 /* Return nonzero if I2 of pair X is the same as that of pair Y. */
552 static int
553 delay_i2_eq (const void *x, const void *y)
555 return ((const struct delay_pair *) x)->i2 == y;
558 /* This function can be called by a port just before it starts the
559 final scheduling pass. It records the fact that an instruction
560 with delay slots has been split into two insns, I1 and I2. The
561 first one will be scheduled normally and initiates the operation.
562 The second one is a shadow which must follow a specific number of
563 CYCLES after I1; its only purpose is to show the side effect that
564 occurs at that cycle in the RTL. If a JUMP_INSN or a CALL_INSN has
565 been split, I1 should be a normal INSN, while I2 retains the
566 original insn type. */
568 void
569 record_delay_slot_pair (rtx i1, rtx i2, int cycles)
571 struct delay_pair *p = XNEW (struct delay_pair);
572 struct delay_pair **slot;
574 p->i1 = i1;
575 p->i2 = i2;
576 p->cycles = cycles;
578 if (!delay_htab)
580 delay_htab = htab_create (10, delay_hash_i1, delay_i1_eq, NULL);
581 delay_htab_i2 = htab_create (10, delay_hash_i2, delay_i2_eq, free);
583 slot = ((struct delay_pair **)
584 htab_find_slot_with_hash (delay_htab, i1, htab_hash_pointer (i1),
585 INSERT));
586 p->next_same_i1 = *slot;
587 *slot = p;
588 slot = ((struct delay_pair **)
589 htab_find_slot_with_hash (delay_htab_i2, i2, htab_hash_pointer (i2),
590 INSERT));
591 *slot = p;
594 /* For a pair P of insns, return the fixed distance in cycles from the first
595 insn after which the second must be scheduled. */
596 static int
597 pair_delay (struct delay_pair *p)
599 return p->cycles;
602 /* Given an insn INSN, add a dependence on its delayed shadow if it
603 has one. Also try to find situations where shadows depend on each other
604 and add dependencies to the real insns to limit the amount of backtracking
605 needed. */
606 void
607 add_delay_dependencies (rtx insn)
609 struct delay_pair *pair;
610 sd_iterator_def sd_it;
611 dep_t dep;
613 if (!delay_htab)
614 return;
616 pair
617 = (struct delay_pair *)htab_find_with_hash (delay_htab_i2, insn,
618 htab_hash_pointer (insn));
619 if (!pair)
620 return;
621 add_dependence (insn, pair->i1, REG_DEP_ANTI);
623 FOR_EACH_DEP (pair->i2, SD_LIST_BACK, sd_it, dep)
625 rtx pro = DEP_PRO (dep);
626 struct delay_pair *other_pair
627 = (struct delay_pair *)htab_find_with_hash (delay_htab_i2, pro,
628 htab_hash_pointer (pro));
629 if (!other_pair)
630 continue;
631 if (pair_delay (other_pair) >= pair_delay (pair))
633 if (sched_verbose >= 4)
635 fprintf (sched_dump, ";;\tadding dependence %d <- %d\n",
636 INSN_UID (other_pair->i1),
637 INSN_UID (pair->i1));
638 fprintf (sched_dump, ";;\tpair1 %d <- %d, cost %d\n",
639 INSN_UID (pair->i1),
640 INSN_UID (pair->i2),
641 pair_delay (pair));
642 fprintf (sched_dump, ";;\tpair2 %d <- %d, cost %d\n",
643 INSN_UID (other_pair->i1),
644 INSN_UID (other_pair->i2),
645 pair_delay (other_pair));
647 add_dependence (pair->i1, other_pair->i1, REG_DEP_ANTI);
652 /* Forward declarations. */
654 static int priority (rtx);
655 static int rank_for_schedule (const void *, const void *);
656 static void swap_sort (rtx *, int);
657 static void queue_insn (rtx, int, const char *);
658 static int schedule_insn (rtx);
659 static void adjust_priority (rtx);
660 static void advance_one_cycle (void);
661 static void extend_h_i_d (void);
664 /* Notes handling mechanism:
665 =========================
666 Generally, NOTES are saved before scheduling and restored after scheduling.
667 The scheduler distinguishes between two types of notes:
669 (1) LOOP_BEGIN, LOOP_END, SETJMP, EHREGION_BEG, EHREGION_END notes:
670 Before scheduling a region, a pointer to the note is added to the insn
671 that follows or precedes it. (This happens as part of the data dependence
672 computation). After scheduling an insn, the pointer contained in it is
673 used for regenerating the corresponding note (in reemit_notes).
675 (2) All other notes (e.g. INSN_DELETED): Before scheduling a block,
676 these notes are put in a list (in rm_other_notes() and
677 unlink_other_notes ()). After scheduling the block, these notes are
678 inserted at the beginning of the block (in schedule_block()). */
680 static void ready_add (struct ready_list *, rtx, bool);
681 static rtx ready_remove_first (struct ready_list *);
682 static rtx ready_remove_first_dispatch (struct ready_list *ready);
684 static void queue_to_ready (struct ready_list *);
685 static int early_queue_to_ready (state_t, struct ready_list *);
687 static void debug_ready_list (struct ready_list *);
689 /* The following functions are used to implement multi-pass scheduling
690 on the first cycle. */
691 static rtx ready_remove (struct ready_list *, int);
692 static void ready_remove_insn (rtx);
694 static void fix_inter_tick (rtx, rtx);
695 static int fix_tick_ready (rtx);
696 static void change_queue_index (rtx, int);
698 /* The following functions are used to implement scheduling of data/control
699 speculative instructions. */
701 static void extend_h_i_d (void);
702 static void init_h_i_d (rtx);
703 static void generate_recovery_code (rtx);
704 static void process_insn_forw_deps_be_in_spec (rtx, rtx, ds_t);
705 static void begin_speculative_block (rtx);
706 static void add_to_speculative_block (rtx);
707 static void init_before_recovery (basic_block *);
708 static void create_check_block_twin (rtx, bool);
709 static void fix_recovery_deps (basic_block);
710 static void haifa_change_pattern (rtx, rtx);
711 static void dump_new_block_header (int, basic_block, rtx, rtx);
712 static void restore_bb_notes (basic_block);
713 static void fix_jump_move (rtx);
714 static void move_block_after_check (rtx);
715 static void move_succs (VEC(edge,gc) **, basic_block);
716 static void sched_remove_insn (rtx);
717 static void clear_priorities (rtx, rtx_vec_t *);
718 static void calc_priorities (rtx_vec_t);
719 static void add_jump_dependencies (rtx, rtx);
721 #endif /* INSN_SCHEDULING */
723 /* Point to state used for the current scheduling pass. */
724 struct haifa_sched_info *current_sched_info;
726 #ifndef INSN_SCHEDULING
727 void
728 schedule_insns (void)
731 #else
733 /* Do register pressure sensitive insn scheduling if the flag is set
734 up. */
735 bool sched_pressure_p;
737 /* Map regno -> its pressure class. The map defined only when
738 SCHED_PRESSURE_P is true. */
739 enum reg_class *sched_regno_pressure_class;
741 /* The current register pressure. Only elements corresponding pressure
742 classes are defined. */
743 static int curr_reg_pressure[N_REG_CLASSES];
745 /* Saved value of the previous array. */
746 static int saved_reg_pressure[N_REG_CLASSES];
748 /* Register living at given scheduling point. */
749 static bitmap curr_reg_live;
751 /* Saved value of the previous array. */
752 static bitmap saved_reg_live;
754 /* Registers mentioned in the current region. */
755 static bitmap region_ref_regs;
757 /* Initiate register pressure relative info for scheduling the current
758 region. Currently it is only clearing register mentioned in the
759 current region. */
760 void
761 sched_init_region_reg_pressure_info (void)
763 bitmap_clear (region_ref_regs);
766 /* Update current register pressure related info after birth (if
767 BIRTH_P) or death of register REGNO. */
768 static void
769 mark_regno_birth_or_death (int regno, bool birth_p)
771 enum reg_class pressure_class;
773 pressure_class = sched_regno_pressure_class[regno];
774 if (regno >= FIRST_PSEUDO_REGISTER)
776 if (pressure_class != NO_REGS)
778 if (birth_p)
780 bitmap_set_bit (curr_reg_live, regno);
781 curr_reg_pressure[pressure_class]
782 += (ira_reg_class_max_nregs
783 [pressure_class][PSEUDO_REGNO_MODE (regno)]);
785 else
787 bitmap_clear_bit (curr_reg_live, regno);
788 curr_reg_pressure[pressure_class]
789 -= (ira_reg_class_max_nregs
790 [pressure_class][PSEUDO_REGNO_MODE (regno)]);
794 else if (pressure_class != NO_REGS
795 && ! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
797 if (birth_p)
799 bitmap_set_bit (curr_reg_live, regno);
800 curr_reg_pressure[pressure_class]++;
802 else
804 bitmap_clear_bit (curr_reg_live, regno);
805 curr_reg_pressure[pressure_class]--;
810 /* Initiate current register pressure related info from living
811 registers given by LIVE. */
812 static void
813 initiate_reg_pressure_info (bitmap live)
815 int i;
816 unsigned int j;
817 bitmap_iterator bi;
819 for (i = 0; i < ira_pressure_classes_num; i++)
820 curr_reg_pressure[ira_pressure_classes[i]] = 0;
821 bitmap_clear (curr_reg_live);
822 EXECUTE_IF_SET_IN_BITMAP (live, 0, j, bi)
823 if (current_nr_blocks == 1 || bitmap_bit_p (region_ref_regs, j))
824 mark_regno_birth_or_death (j, true);
827 /* Mark registers in X as mentioned in the current region. */
828 static void
829 setup_ref_regs (rtx x)
831 int i, j, regno;
832 const RTX_CODE code = GET_CODE (x);
833 const char *fmt;
835 if (REG_P (x))
837 regno = REGNO (x);
838 if (HARD_REGISTER_NUM_P (regno))
839 bitmap_set_range (region_ref_regs, regno,
840 hard_regno_nregs[regno][GET_MODE (x)]);
841 else
842 bitmap_set_bit (region_ref_regs, REGNO (x));
843 return;
845 fmt = GET_RTX_FORMAT (code);
846 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
847 if (fmt[i] == 'e')
848 setup_ref_regs (XEXP (x, i));
849 else if (fmt[i] == 'E')
851 for (j = 0; j < XVECLEN (x, i); j++)
852 setup_ref_regs (XVECEXP (x, i, j));
856 /* Initiate current register pressure related info at the start of
857 basic block BB. */
858 static void
859 initiate_bb_reg_pressure_info (basic_block bb)
861 unsigned int i ATTRIBUTE_UNUSED;
862 rtx insn;
864 if (current_nr_blocks > 1)
865 FOR_BB_INSNS (bb, insn)
866 if (NONDEBUG_INSN_P (insn))
867 setup_ref_regs (PATTERN (insn));
868 initiate_reg_pressure_info (df_get_live_in (bb));
869 #ifdef EH_RETURN_DATA_REGNO
870 if (bb_has_eh_pred (bb))
871 for (i = 0; ; ++i)
873 unsigned int regno = EH_RETURN_DATA_REGNO (i);
875 if (regno == INVALID_REGNUM)
876 break;
877 if (! bitmap_bit_p (df_get_live_in (bb), regno))
878 mark_regno_birth_or_death (regno, true);
880 #endif
883 /* Save current register pressure related info. */
884 static void
885 save_reg_pressure (void)
887 int i;
889 for (i = 0; i < ira_pressure_classes_num; i++)
890 saved_reg_pressure[ira_pressure_classes[i]]
891 = curr_reg_pressure[ira_pressure_classes[i]];
892 bitmap_copy (saved_reg_live, curr_reg_live);
895 /* Restore saved register pressure related info. */
896 static void
897 restore_reg_pressure (void)
899 int i;
901 for (i = 0; i < ira_pressure_classes_num; i++)
902 curr_reg_pressure[ira_pressure_classes[i]]
903 = saved_reg_pressure[ira_pressure_classes[i]];
904 bitmap_copy (curr_reg_live, saved_reg_live);
907 /* Return TRUE if the register is dying after its USE. */
908 static bool
909 dying_use_p (struct reg_use_data *use)
911 struct reg_use_data *next;
913 for (next = use->next_regno_use; next != use; next = next->next_regno_use)
914 if (NONDEBUG_INSN_P (next->insn)
915 && QUEUE_INDEX (next->insn) != QUEUE_SCHEDULED)
916 return false;
917 return true;
920 /* Print info about the current register pressure and its excess for
921 each pressure class. */
922 static void
923 print_curr_reg_pressure (void)
925 int i;
926 enum reg_class cl;
928 fprintf (sched_dump, ";;\t");
929 for (i = 0; i < ira_pressure_classes_num; i++)
931 cl = ira_pressure_classes[i];
932 gcc_assert (curr_reg_pressure[cl] >= 0);
933 fprintf (sched_dump, " %s:%d(%d)", reg_class_names[cl],
934 curr_reg_pressure[cl],
935 curr_reg_pressure[cl] - ira_available_class_regs[cl]);
937 fprintf (sched_dump, "\n");
940 /* Pointer to the last instruction scheduled. */
941 static rtx last_scheduled_insn;
943 /* Pointer to the last nondebug instruction scheduled within the
944 block, or the prev_head of the scheduling block. Used by
945 rank_for_schedule, so that insns independent of the last scheduled
946 insn will be preferred over dependent instructions. */
947 static rtx last_nondebug_scheduled_insn;
949 /* Pointer that iterates through the list of unscheduled insns if we
950 have a dbg_cnt enabled. It always points at an insn prior to the
951 first unscheduled one. */
952 static rtx nonscheduled_insns_begin;
954 /* Cached cost of the instruction. Use below function to get cost of the
955 insn. -1 here means that the field is not initialized. */
956 #define INSN_COST(INSN) (HID (INSN)->cost)
958 /* Compute cost of executing INSN.
959 This is the number of cycles between instruction issue and
960 instruction results. */
962 insn_cost (rtx insn)
964 int cost;
966 if (sel_sched_p ())
968 if (recog_memoized (insn) < 0)
969 return 0;
971 cost = insn_default_latency (insn);
972 if (cost < 0)
973 cost = 0;
975 return cost;
978 cost = INSN_COST (insn);
980 if (cost < 0)
982 /* A USE insn, or something else we don't need to
983 understand. We can't pass these directly to
984 result_ready_cost or insn_default_latency because it will
985 trigger a fatal error for unrecognizable insns. */
986 if (recog_memoized (insn) < 0)
988 INSN_COST (insn) = 0;
989 return 0;
991 else
993 cost = insn_default_latency (insn);
994 if (cost < 0)
995 cost = 0;
997 INSN_COST (insn) = cost;
1001 return cost;
1004 /* Compute cost of dependence LINK.
1005 This is the number of cycles between instruction issue and
1006 instruction results.
1007 ??? We also use this function to call recog_memoized on all insns. */
1009 dep_cost_1 (dep_t link, dw_t dw)
1011 rtx insn = DEP_PRO (link);
1012 rtx used = DEP_CON (link);
1013 int cost;
1015 if (DEP_COST (link) != UNKNOWN_DEP_COST)
1016 return DEP_COST (link);
1018 if (delay_htab)
1020 struct delay_pair *delay_entry;
1021 delay_entry
1022 = (struct delay_pair *)htab_find_with_hash (delay_htab_i2, used,
1023 htab_hash_pointer (used));
1024 if (delay_entry)
1026 if (delay_entry->i1 == insn)
1028 DEP_COST (link) = pair_delay (delay_entry);
1029 return DEP_COST (link);
1034 /* A USE insn should never require the value used to be computed.
1035 This allows the computation of a function's result and parameter
1036 values to overlap the return and call. We don't care about the
1037 dependence cost when only decreasing register pressure. */
1038 if (recog_memoized (used) < 0)
1040 cost = 0;
1041 recog_memoized (insn);
1043 else
1045 enum reg_note dep_type = DEP_TYPE (link);
1047 cost = insn_cost (insn);
1049 if (INSN_CODE (insn) >= 0)
1051 if (dep_type == REG_DEP_ANTI)
1052 cost = 0;
1053 else if (dep_type == REG_DEP_OUTPUT)
1055 cost = (insn_default_latency (insn)
1056 - insn_default_latency (used));
1057 if (cost <= 0)
1058 cost = 1;
1060 else if (bypass_p (insn))
1061 cost = insn_latency (insn, used);
1065 if (targetm.sched.adjust_cost_2)
1066 cost = targetm.sched.adjust_cost_2 (used, (int) dep_type, insn, cost,
1067 dw);
1068 else if (targetm.sched.adjust_cost != NULL)
1070 /* This variable is used for backward compatibility with the
1071 targets. */
1072 rtx dep_cost_rtx_link = alloc_INSN_LIST (NULL_RTX, NULL_RTX);
1074 /* Make it self-cycled, so that if some tries to walk over this
1075 incomplete list he/she will be caught in an endless loop. */
1076 XEXP (dep_cost_rtx_link, 1) = dep_cost_rtx_link;
1078 /* Targets use only REG_NOTE_KIND of the link. */
1079 PUT_REG_NOTE_KIND (dep_cost_rtx_link, DEP_TYPE (link));
1081 cost = targetm.sched.adjust_cost (used, dep_cost_rtx_link,
1082 insn, cost);
1084 free_INSN_LIST_node (dep_cost_rtx_link);
1087 if (cost < 0)
1088 cost = 0;
1091 DEP_COST (link) = cost;
1092 return cost;
1095 /* Compute cost of dependence LINK.
1096 This is the number of cycles between instruction issue and
1097 instruction results. */
1099 dep_cost (dep_t link)
1101 return dep_cost_1 (link, 0);
1104 /* Use this sel-sched.c friendly function in reorder2 instead of increasing
1105 INSN_PRIORITY explicitly. */
1106 void
1107 increase_insn_priority (rtx insn, int amount)
1109 if (!sel_sched_p ())
1111 /* We're dealing with haifa-sched.c INSN_PRIORITY. */
1112 if (INSN_PRIORITY_KNOWN (insn))
1113 INSN_PRIORITY (insn) += amount;
1115 else
1117 /* In sel-sched.c INSN_PRIORITY is not kept up to date.
1118 Use EXPR_PRIORITY instead. */
1119 sel_add_to_insn_priority (insn, amount);
1123 /* Return 'true' if DEP should be included in priority calculations. */
1124 static bool
1125 contributes_to_priority_p (dep_t dep)
1127 if (DEBUG_INSN_P (DEP_CON (dep))
1128 || DEBUG_INSN_P (DEP_PRO (dep)))
1129 return false;
1131 /* Critical path is meaningful in block boundaries only. */
1132 if (!current_sched_info->contributes_to_priority (DEP_CON (dep),
1133 DEP_PRO (dep)))
1134 return false;
1136 /* If flag COUNT_SPEC_IN_CRITICAL_PATH is set,
1137 then speculative instructions will less likely be
1138 scheduled. That is because the priority of
1139 their producers will increase, and, thus, the
1140 producers will more likely be scheduled, thus,
1141 resolving the dependence. */
1142 if (sched_deps_info->generate_spec_deps
1143 && !(spec_info->flags & COUNT_SPEC_IN_CRITICAL_PATH)
1144 && (DEP_STATUS (dep) & SPECULATIVE))
1145 return false;
1147 return true;
1150 /* Compute the number of nondebug forward deps of an insn. */
1152 static int
1153 dep_list_size (rtx insn)
1155 sd_iterator_def sd_it;
1156 dep_t dep;
1157 int dbgcount = 0, nodbgcount = 0;
1159 if (!MAY_HAVE_DEBUG_INSNS)
1160 return sd_lists_size (insn, SD_LIST_FORW);
1162 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
1164 if (DEBUG_INSN_P (DEP_CON (dep)))
1165 dbgcount++;
1166 else if (!DEBUG_INSN_P (DEP_PRO (dep)))
1167 nodbgcount++;
1170 gcc_assert (dbgcount + nodbgcount == sd_lists_size (insn, SD_LIST_FORW));
1172 return nodbgcount;
1175 /* Compute the priority number for INSN. */
1176 static int
1177 priority (rtx insn)
1179 if (! INSN_P (insn))
1180 return 0;
1182 /* We should not be interested in priority of an already scheduled insn. */
1183 gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
1185 if (!INSN_PRIORITY_KNOWN (insn))
1187 int this_priority = -1;
1189 if (dep_list_size (insn) == 0)
1190 /* ??? We should set INSN_PRIORITY to insn_cost when and insn has
1191 some forward deps but all of them are ignored by
1192 contributes_to_priority hook. At the moment we set priority of
1193 such insn to 0. */
1194 this_priority = insn_cost (insn);
1195 else
1197 rtx prev_first, twin;
1198 basic_block rec;
1200 /* For recovery check instructions we calculate priority slightly
1201 different than that of normal instructions. Instead of walking
1202 through INSN_FORW_DEPS (check) list, we walk through
1203 INSN_FORW_DEPS list of each instruction in the corresponding
1204 recovery block. */
1206 /* Selective scheduling does not define RECOVERY_BLOCK macro. */
1207 rec = sel_sched_p () ? NULL : RECOVERY_BLOCK (insn);
1208 if (!rec || rec == EXIT_BLOCK_PTR)
1210 prev_first = PREV_INSN (insn);
1211 twin = insn;
1213 else
1215 prev_first = NEXT_INSN (BB_HEAD (rec));
1216 twin = PREV_INSN (BB_END (rec));
1221 sd_iterator_def sd_it;
1222 dep_t dep;
1224 FOR_EACH_DEP (twin, SD_LIST_FORW, sd_it, dep)
1226 rtx next;
1227 int next_priority;
1229 next = DEP_CON (dep);
1231 if (BLOCK_FOR_INSN (next) != rec)
1233 int cost;
1235 if (!contributes_to_priority_p (dep))
1236 continue;
1238 if (twin == insn)
1239 cost = dep_cost (dep);
1240 else
1242 struct _dep _dep1, *dep1 = &_dep1;
1244 init_dep (dep1, insn, next, REG_DEP_ANTI);
1246 cost = dep_cost (dep1);
1249 next_priority = cost + priority (next);
1251 if (next_priority > this_priority)
1252 this_priority = next_priority;
1256 twin = PREV_INSN (twin);
1258 while (twin != prev_first);
1261 if (this_priority < 0)
1263 gcc_assert (this_priority == -1);
1265 this_priority = insn_cost (insn);
1268 INSN_PRIORITY (insn) = this_priority;
1269 INSN_PRIORITY_STATUS (insn) = 1;
1272 return INSN_PRIORITY (insn);
1275 /* Macros and functions for keeping the priority queue sorted, and
1276 dealing with queuing and dequeuing of instructions. */
1278 #define SCHED_SORT(READY, N_READY) \
1279 do { if ((N_READY) == 2) \
1280 swap_sort (READY, N_READY); \
1281 else if ((N_READY) > 2) \
1282 qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); } \
1283 while (0)
1285 /* Setup info about the current register pressure impact of scheduling
1286 INSN at the current scheduling point. */
1287 static void
1288 setup_insn_reg_pressure_info (rtx insn)
1290 int i, change, before, after, hard_regno;
1291 int excess_cost_change;
1292 enum machine_mode mode;
1293 enum reg_class cl;
1294 struct reg_pressure_data *pressure_info;
1295 int *max_reg_pressure;
1296 struct reg_use_data *use;
1297 static int death[N_REG_CLASSES];
1299 gcc_checking_assert (!DEBUG_INSN_P (insn));
1301 excess_cost_change = 0;
1302 for (i = 0; i < ira_pressure_classes_num; i++)
1303 death[ira_pressure_classes[i]] = 0;
1304 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
1305 if (dying_use_p (use))
1307 cl = sched_regno_pressure_class[use->regno];
1308 if (use->regno < FIRST_PSEUDO_REGISTER)
1309 death[cl]++;
1310 else
1311 death[cl]
1312 += ira_reg_class_max_nregs[cl][PSEUDO_REGNO_MODE (use->regno)];
1314 pressure_info = INSN_REG_PRESSURE (insn);
1315 max_reg_pressure = INSN_MAX_REG_PRESSURE (insn);
1316 gcc_assert (pressure_info != NULL && max_reg_pressure != NULL);
1317 for (i = 0; i < ira_pressure_classes_num; i++)
1319 cl = ira_pressure_classes[i];
1320 gcc_assert (curr_reg_pressure[cl] >= 0);
1321 change = (int) pressure_info[i].set_increase - death[cl];
1322 before = MAX (0, max_reg_pressure[i] - ira_available_class_regs[cl]);
1323 after = MAX (0, max_reg_pressure[i] + change
1324 - ira_available_class_regs[cl]);
1325 hard_regno = ira_class_hard_regs[cl][0];
1326 gcc_assert (hard_regno >= 0);
1327 mode = reg_raw_mode[hard_regno];
1328 excess_cost_change += ((after - before)
1329 * (ira_memory_move_cost[mode][cl][0]
1330 + ira_memory_move_cost[mode][cl][1]));
1332 INSN_REG_PRESSURE_EXCESS_COST_CHANGE (insn) = excess_cost_change;
1335 /* Returns a positive value if x is preferred; returns a negative value if
1336 y is preferred. Should never return 0, since that will make the sort
1337 unstable. */
1339 static int
1340 rank_for_schedule (const void *x, const void *y)
1342 rtx tmp = *(const rtx *) y;
1343 rtx tmp2 = *(const rtx *) x;
1344 int tmp_class, tmp2_class;
1345 int val, priority_val, info_val;
1347 if (MAY_HAVE_DEBUG_INSNS)
1349 /* Schedule debug insns as early as possible. */
1350 if (DEBUG_INSN_P (tmp) && !DEBUG_INSN_P (tmp2))
1351 return -1;
1352 else if (DEBUG_INSN_P (tmp2))
1353 return 1;
1356 /* The insn in a schedule group should be issued the first. */
1357 if (flag_sched_group_heuristic &&
1358 SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
1359 return SCHED_GROUP_P (tmp2) ? 1 : -1;
1361 /* Make sure that priority of TMP and TMP2 are initialized. */
1362 gcc_assert (INSN_PRIORITY_KNOWN (tmp) && INSN_PRIORITY_KNOWN (tmp2));
1364 if (sched_pressure_p)
1366 int diff;
1368 /* Prefer insn whose scheduling results in the smallest register
1369 pressure excess. */
1370 if ((diff = (INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp)
1371 + (INSN_TICK (tmp) > clock_var
1372 ? INSN_TICK (tmp) - clock_var : 0)
1373 - INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp2)
1374 - (INSN_TICK (tmp2) > clock_var
1375 ? INSN_TICK (tmp2) - clock_var : 0))) != 0)
1376 return diff;
1380 if (sched_pressure_p
1381 && (INSN_TICK (tmp2) > clock_var || INSN_TICK (tmp) > clock_var))
1383 if (INSN_TICK (tmp) <= clock_var)
1384 return -1;
1385 else if (INSN_TICK (tmp2) <= clock_var)
1386 return 1;
1387 else
1388 return INSN_TICK (tmp) - INSN_TICK (tmp2);
1391 /* If we are doing backtracking in this schedule, prefer insns that
1392 have forward dependencies with negative cost against an insn that
1393 was already scheduled. */
1394 if (current_sched_info->flags & DO_BACKTRACKING)
1396 priority_val = FEEDS_BACKTRACK_INSN (tmp2) - FEEDS_BACKTRACK_INSN (tmp);
1397 if (priority_val)
1398 return priority_val;
1401 /* Prefer insn with higher priority. */
1402 priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
1404 if (flag_sched_critical_path_heuristic && priority_val)
1405 return priority_val;
1407 /* Prefer speculative insn with greater dependencies weakness. */
1408 if (flag_sched_spec_insn_heuristic && spec_info)
1410 ds_t ds1, ds2;
1411 dw_t dw1, dw2;
1412 int dw;
1414 ds1 = TODO_SPEC (tmp) & SPECULATIVE;
1415 if (ds1)
1416 dw1 = ds_weak (ds1);
1417 else
1418 dw1 = NO_DEP_WEAK;
1420 ds2 = TODO_SPEC (tmp2) & SPECULATIVE;
1421 if (ds2)
1422 dw2 = ds_weak (ds2);
1423 else
1424 dw2 = NO_DEP_WEAK;
1426 dw = dw2 - dw1;
1427 if (dw > (NO_DEP_WEAK / 8) || dw < -(NO_DEP_WEAK / 8))
1428 return dw;
1431 info_val = (*current_sched_info->rank) (tmp, tmp2);
1432 if(flag_sched_rank_heuristic && info_val)
1433 return info_val;
1435 /* Compare insns based on their relation to the last scheduled
1436 non-debug insn. */
1437 if (flag_sched_last_insn_heuristic && last_nondebug_scheduled_insn)
1439 dep_t dep1;
1440 dep_t dep2;
1441 rtx last = last_nondebug_scheduled_insn;
1443 /* Classify the instructions into three classes:
1444 1) Data dependent on last schedule insn.
1445 2) Anti/Output dependent on last scheduled insn.
1446 3) Independent of last scheduled insn, or has latency of one.
1447 Choose the insn from the highest numbered class if different. */
1448 dep1 = sd_find_dep_between (last, tmp, true);
1450 if (dep1 == NULL || dep_cost (dep1) == 1)
1451 tmp_class = 3;
1452 else if (/* Data dependence. */
1453 DEP_TYPE (dep1) == REG_DEP_TRUE)
1454 tmp_class = 1;
1455 else
1456 tmp_class = 2;
1458 dep2 = sd_find_dep_between (last, tmp2, true);
1460 if (dep2 == NULL || dep_cost (dep2) == 1)
1461 tmp2_class = 3;
1462 else if (/* Data dependence. */
1463 DEP_TYPE (dep2) == REG_DEP_TRUE)
1464 tmp2_class = 1;
1465 else
1466 tmp2_class = 2;
1468 if ((val = tmp2_class - tmp_class))
1469 return val;
1472 /* Prefer the insn which has more later insns that depend on it.
1473 This gives the scheduler more freedom when scheduling later
1474 instructions at the expense of added register pressure. */
1476 val = (dep_list_size (tmp2) - dep_list_size (tmp));
1478 if (flag_sched_dep_count_heuristic && val != 0)
1479 return val;
1481 /* If insns are equally good, sort by INSN_LUID (original insn order),
1482 so that we make the sort stable. This minimizes instruction movement,
1483 thus minimizing sched's effect on debugging and cross-jumping. */
1484 return INSN_LUID (tmp) - INSN_LUID (tmp2);
1487 /* Resort the array A in which only element at index N may be out of order. */
1489 HAIFA_INLINE static void
1490 swap_sort (rtx *a, int n)
1492 rtx insn = a[n - 1];
1493 int i = n - 2;
1495 while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
1497 a[i + 1] = a[i];
1498 i -= 1;
1500 a[i + 1] = insn;
1503 /* Add INSN to the insn queue so that it can be executed at least
1504 N_CYCLES after the currently executing insn. Preserve insns
1505 chain for debugging purposes. REASON will be printed in debugging
1506 output. */
1508 HAIFA_INLINE static void
1509 queue_insn (rtx insn, int n_cycles, const char *reason)
1511 int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
1512 rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
1513 int new_tick;
1515 gcc_assert (n_cycles <= max_insn_queue_index);
1516 gcc_assert (!DEBUG_INSN_P (insn));
1518 insn_queue[next_q] = link;
1519 q_size += 1;
1521 if (sched_verbose >= 2)
1523 fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
1524 (*current_sched_info->print_insn) (insn, 0));
1526 fprintf (sched_dump, "queued for %d cycles (%s).\n", n_cycles, reason);
1529 QUEUE_INDEX (insn) = next_q;
1531 if (current_sched_info->flags & DO_BACKTRACKING)
1533 new_tick = clock_var + n_cycles;
1534 if (INSN_TICK (insn) == INVALID_TICK || INSN_TICK (insn) < new_tick)
1535 INSN_TICK (insn) = new_tick;
1537 if (INSN_EXACT_TICK (insn) != INVALID_TICK
1538 && INSN_EXACT_TICK (insn) < clock_var + n_cycles)
1540 must_backtrack = true;
1541 if (sched_verbose >= 2)
1542 fprintf (sched_dump, ";;\t\tcausing a backtrack.\n");
1547 /* Remove INSN from queue. */
1548 static void
1549 queue_remove (rtx insn)
1551 gcc_assert (QUEUE_INDEX (insn) >= 0);
1552 remove_free_INSN_LIST_elem (insn, &insn_queue[QUEUE_INDEX (insn)]);
1553 q_size--;
1554 QUEUE_INDEX (insn) = QUEUE_NOWHERE;
1557 /* Return a pointer to the bottom of the ready list, i.e. the insn
1558 with the lowest priority. */
1560 rtx *
1561 ready_lastpos (struct ready_list *ready)
1563 gcc_assert (ready->n_ready >= 1);
1564 return ready->vec + ready->first - ready->n_ready + 1;
1567 /* Add an element INSN to the ready list so that it ends up with the
1568 lowest/highest priority depending on FIRST_P. */
1570 HAIFA_INLINE static void
1571 ready_add (struct ready_list *ready, rtx insn, bool first_p)
1573 if (!first_p)
1575 if (ready->first == ready->n_ready)
1577 memmove (ready->vec + ready->veclen - ready->n_ready,
1578 ready_lastpos (ready),
1579 ready->n_ready * sizeof (rtx));
1580 ready->first = ready->veclen - 1;
1582 ready->vec[ready->first - ready->n_ready] = insn;
1584 else
1586 if (ready->first == ready->veclen - 1)
1588 if (ready->n_ready)
1589 /* ready_lastpos() fails when called with (ready->n_ready == 0). */
1590 memmove (ready->vec + ready->veclen - ready->n_ready - 1,
1591 ready_lastpos (ready),
1592 ready->n_ready * sizeof (rtx));
1593 ready->first = ready->veclen - 2;
1595 ready->vec[++(ready->first)] = insn;
1598 ready->n_ready++;
1599 if (DEBUG_INSN_P (insn))
1600 ready->n_debug++;
1602 gcc_assert (QUEUE_INDEX (insn) != QUEUE_READY);
1603 QUEUE_INDEX (insn) = QUEUE_READY;
1605 if (INSN_EXACT_TICK (insn) != INVALID_TICK
1606 && INSN_EXACT_TICK (insn) < clock_var)
1608 must_backtrack = true;
1612 /* Remove the element with the highest priority from the ready list and
1613 return it. */
1615 HAIFA_INLINE static rtx
1616 ready_remove_first (struct ready_list *ready)
1618 rtx t;
1620 gcc_assert (ready->n_ready);
1621 t = ready->vec[ready->first--];
1622 ready->n_ready--;
1623 if (DEBUG_INSN_P (t))
1624 ready->n_debug--;
1625 /* If the queue becomes empty, reset it. */
1626 if (ready->n_ready == 0)
1627 ready->first = ready->veclen - 1;
1629 gcc_assert (QUEUE_INDEX (t) == QUEUE_READY);
1630 QUEUE_INDEX (t) = QUEUE_NOWHERE;
1632 return t;
1635 /* The following code implements multi-pass scheduling for the first
1636 cycle. In other words, we will try to choose ready insn which
1637 permits to start maximum number of insns on the same cycle. */
1639 /* Return a pointer to the element INDEX from the ready. INDEX for
1640 insn with the highest priority is 0, and the lowest priority has
1641 N_READY - 1. */
1644 ready_element (struct ready_list *ready, int index)
1646 gcc_assert (ready->n_ready && index < ready->n_ready);
1648 return ready->vec[ready->first - index];
1651 /* Remove the element INDEX from the ready list and return it. INDEX
1652 for insn with the highest priority is 0, and the lowest priority
1653 has N_READY - 1. */
1655 HAIFA_INLINE static rtx
1656 ready_remove (struct ready_list *ready, int index)
1658 rtx t;
1659 int i;
1661 if (index == 0)
1662 return ready_remove_first (ready);
1663 gcc_assert (ready->n_ready && index < ready->n_ready);
1664 t = ready->vec[ready->first - index];
1665 ready->n_ready--;
1666 if (DEBUG_INSN_P (t))
1667 ready->n_debug--;
1668 for (i = index; i < ready->n_ready; i++)
1669 ready->vec[ready->first - i] = ready->vec[ready->first - i - 1];
1670 QUEUE_INDEX (t) = QUEUE_NOWHERE;
1671 return t;
1674 /* Remove INSN from the ready list. */
1675 static void
1676 ready_remove_insn (rtx insn)
1678 int i;
1680 for (i = 0; i < readyp->n_ready; i++)
1681 if (ready_element (readyp, i) == insn)
1683 ready_remove (readyp, i);
1684 return;
1686 gcc_unreachable ();
1689 /* Sort the ready list READY by ascending priority, using the SCHED_SORT
1690 macro. */
1692 void
1693 ready_sort (struct ready_list *ready)
1695 int i;
1696 rtx *first = ready_lastpos (ready);
1698 if (sched_pressure_p)
1700 for (i = 0; i < ready->n_ready; i++)
1701 if (!DEBUG_INSN_P (first[i]))
1702 setup_insn_reg_pressure_info (first[i]);
1704 SCHED_SORT (first, ready->n_ready);
1707 /* PREV is an insn that is ready to execute. Adjust its priority if that
1708 will help shorten or lengthen register lifetimes as appropriate. Also
1709 provide a hook for the target to tweak itself. */
1711 HAIFA_INLINE static void
1712 adjust_priority (rtx prev)
1714 /* ??? There used to be code here to try and estimate how an insn
1715 affected register lifetimes, but it did it by looking at REG_DEAD
1716 notes, which we removed in schedule_region. Nor did it try to
1717 take into account register pressure or anything useful like that.
1719 Revisit when we have a machine model to work with and not before. */
1721 if (targetm.sched.adjust_priority)
1722 INSN_PRIORITY (prev) =
1723 targetm.sched.adjust_priority (prev, INSN_PRIORITY (prev));
1726 /* Advance DFA state STATE on one cycle. */
1727 void
1728 advance_state (state_t state)
1730 if (targetm.sched.dfa_pre_advance_cycle)
1731 targetm.sched.dfa_pre_advance_cycle ();
1733 if (targetm.sched.dfa_pre_cycle_insn)
1734 state_transition (state,
1735 targetm.sched.dfa_pre_cycle_insn ());
1737 state_transition (state, NULL);
1739 if (targetm.sched.dfa_post_cycle_insn)
1740 state_transition (state,
1741 targetm.sched.dfa_post_cycle_insn ());
1743 if (targetm.sched.dfa_post_advance_cycle)
1744 targetm.sched.dfa_post_advance_cycle ();
1747 /* Advance time on one cycle. */
1748 HAIFA_INLINE static void
1749 advance_one_cycle (void)
1751 advance_state (curr_state);
1752 if (sched_verbose >= 6)
1753 fprintf (sched_dump, ";;\tAdvanced a state.\n");
1756 /* Update register pressure after scheduling INSN. */
1757 static void
1758 update_register_pressure (rtx insn)
1760 struct reg_use_data *use;
1761 struct reg_set_data *set;
1763 gcc_checking_assert (!DEBUG_INSN_P (insn));
1765 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
1766 if (dying_use_p (use) && bitmap_bit_p (curr_reg_live, use->regno))
1767 mark_regno_birth_or_death (use->regno, false);
1768 for (set = INSN_REG_SET_LIST (insn); set != NULL; set = set->next_insn_set)
1769 mark_regno_birth_or_death (set->regno, true);
1772 /* Set up or update (if UPDATE_P) max register pressure (see its
1773 meaning in sched-int.h::_haifa_insn_data) for all current BB insns
1774 after insn AFTER. */
1775 static void
1776 setup_insn_max_reg_pressure (rtx after, bool update_p)
1778 int i, p;
1779 bool eq_p;
1780 rtx insn;
1781 static int max_reg_pressure[N_REG_CLASSES];
1783 save_reg_pressure ();
1784 for (i = 0; i < ira_pressure_classes_num; i++)
1785 max_reg_pressure[ira_pressure_classes[i]]
1786 = curr_reg_pressure[ira_pressure_classes[i]];
1787 for (insn = NEXT_INSN (after);
1788 insn != NULL_RTX && ! BARRIER_P (insn)
1789 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (after);
1790 insn = NEXT_INSN (insn))
1791 if (NONDEBUG_INSN_P (insn))
1793 eq_p = true;
1794 for (i = 0; i < ira_pressure_classes_num; i++)
1796 p = max_reg_pressure[ira_pressure_classes[i]];
1797 if (INSN_MAX_REG_PRESSURE (insn)[i] != p)
1799 eq_p = false;
1800 INSN_MAX_REG_PRESSURE (insn)[i]
1801 = max_reg_pressure[ira_pressure_classes[i]];
1804 if (update_p && eq_p)
1805 break;
1806 update_register_pressure (insn);
1807 for (i = 0; i < ira_pressure_classes_num; i++)
1808 if (max_reg_pressure[ira_pressure_classes[i]]
1809 < curr_reg_pressure[ira_pressure_classes[i]])
1810 max_reg_pressure[ira_pressure_classes[i]]
1811 = curr_reg_pressure[ira_pressure_classes[i]];
1813 restore_reg_pressure ();
1816 /* Update the current register pressure after scheduling INSN. Update
1817 also max register pressure for unscheduled insns of the current
1818 BB. */
1819 static void
1820 update_reg_and_insn_max_reg_pressure (rtx insn)
1822 int i;
1823 int before[N_REG_CLASSES];
1825 for (i = 0; i < ira_pressure_classes_num; i++)
1826 before[i] = curr_reg_pressure[ira_pressure_classes[i]];
1827 update_register_pressure (insn);
1828 for (i = 0; i < ira_pressure_classes_num; i++)
1829 if (curr_reg_pressure[ira_pressure_classes[i]] != before[i])
1830 break;
1831 if (i < ira_pressure_classes_num)
1832 setup_insn_max_reg_pressure (insn, true);
1835 /* Set up register pressure at the beginning of basic block BB whose
1836 insns starting after insn AFTER. Set up also max register pressure
1837 for all insns of the basic block. */
1838 void
1839 sched_setup_bb_reg_pressure_info (basic_block bb, rtx after)
1841 gcc_assert (sched_pressure_p);
1842 initiate_bb_reg_pressure_info (bb);
1843 setup_insn_max_reg_pressure (after, false);
1846 /* A structure that holds local state for the loop in schedule_block. */
1847 struct sched_block_state
1849 /* True if no real insns have been scheduled in the current cycle. */
1850 bool first_cycle_insn_p;
1851 /* True if a shadow insn has been scheduled in the current cycle, which
1852 means that no more normal insns can be issued. */
1853 bool shadows_only_p;
1854 /* Initialized with the machine's issue rate every cycle, and updated
1855 by calls to the variable_issue hook. */
1856 int can_issue_more;
1859 /* INSN is the "currently executing insn". Launch each insn which was
1860 waiting on INSN. READY is the ready list which contains the insns
1861 that are ready to fire. CLOCK is the current cycle. The function
1862 returns necessary cycle advance after issuing the insn (it is not
1863 zero for insns in a schedule group). */
1865 static int
1866 schedule_insn (rtx insn)
1868 sd_iterator_def sd_it;
1869 dep_t dep;
1870 int i;
1871 int advance = 0;
1873 if (sched_verbose >= 1)
1875 struct reg_pressure_data *pressure_info;
1876 char buf[2048];
1878 print_insn (buf, insn, 0);
1879 buf[40] = 0;
1880 fprintf (sched_dump, ";;\t%3i--> %-40s:", clock_var, buf);
1882 if (recog_memoized (insn) < 0)
1883 fprintf (sched_dump, "nothing");
1884 else
1885 print_reservation (sched_dump, insn);
1886 pressure_info = INSN_REG_PRESSURE (insn);
1887 if (pressure_info != NULL)
1889 fputc (':', sched_dump);
1890 for (i = 0; i < ira_pressure_classes_num; i++)
1891 fprintf (sched_dump, "%s%+d(%d)",
1892 reg_class_names[ira_pressure_classes[i]],
1893 pressure_info[i].set_increase, pressure_info[i].change);
1895 fputc ('\n', sched_dump);
1898 if (sched_pressure_p && !DEBUG_INSN_P (insn))
1899 update_reg_and_insn_max_reg_pressure (insn);
1901 /* Scheduling instruction should have all its dependencies resolved and
1902 should have been removed from the ready list. */
1903 gcc_assert (sd_lists_empty_p (insn, SD_LIST_BACK));
1905 /* Reset debug insns invalidated by moving this insn. */
1906 if (MAY_HAVE_DEBUG_INSNS && !DEBUG_INSN_P (insn))
1907 for (sd_it = sd_iterator_start (insn, SD_LIST_BACK);
1908 sd_iterator_cond (&sd_it, &dep);)
1910 rtx dbg = DEP_PRO (dep);
1911 struct reg_use_data *use, *next;
1913 gcc_assert (DEBUG_INSN_P (dbg));
1915 if (sched_verbose >= 6)
1916 fprintf (sched_dump, ";;\t\tresetting: debug insn %d\n",
1917 INSN_UID (dbg));
1919 /* ??? Rather than resetting the debug insn, we might be able
1920 to emit a debug temp before the just-scheduled insn, but
1921 this would involve checking that the expression at the
1922 point of the debug insn is equivalent to the expression
1923 before the just-scheduled insn. They might not be: the
1924 expression in the debug insn may depend on other insns not
1925 yet scheduled that set MEMs, REGs or even other debug
1926 insns. It's not clear that attempting to preserve debug
1927 information in these cases is worth the effort, given how
1928 uncommon these resets are and the likelihood that the debug
1929 temps introduced won't survive the schedule change. */
1930 INSN_VAR_LOCATION_LOC (dbg) = gen_rtx_UNKNOWN_VAR_LOC ();
1931 df_insn_rescan (dbg);
1933 /* Unknown location doesn't use any registers. */
1934 for (use = INSN_REG_USE_LIST (dbg); use != NULL; use = next)
1936 struct reg_use_data *prev = use;
1938 /* Remove use from the cyclic next_regno_use chain first. */
1939 while (prev->next_regno_use != use)
1940 prev = prev->next_regno_use;
1941 prev->next_regno_use = use->next_regno_use;
1942 next = use->next_insn_use;
1943 free (use);
1945 INSN_REG_USE_LIST (dbg) = NULL;
1947 /* We delete rather than resolve these deps, otherwise we
1948 crash in sched_free_deps(), because forward deps are
1949 expected to be released before backward deps. */
1950 sd_delete_dep (sd_it);
1953 gcc_assert (QUEUE_INDEX (insn) == QUEUE_NOWHERE);
1954 QUEUE_INDEX (insn) = QUEUE_SCHEDULED;
1956 gcc_assert (INSN_TICK (insn) >= MIN_TICK);
1957 if (INSN_TICK (insn) > clock_var)
1958 /* INSN has been prematurely moved from the queue to the ready list.
1959 This is possible only if following flag is set. */
1960 gcc_assert (flag_sched_stalled_insns);
1962 /* ??? Probably, if INSN is scheduled prematurely, we should leave
1963 INSN_TICK untouched. This is a machine-dependent issue, actually. */
1964 INSN_TICK (insn) = clock_var;
1966 /* Update dependent instructions. */
1967 for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
1968 sd_iterator_cond (&sd_it, &dep);)
1970 rtx next = DEP_CON (dep);
1972 /* Resolve the dependence between INSN and NEXT.
1973 sd_resolve_dep () moves current dep to another list thus
1974 advancing the iterator. */
1975 sd_resolve_dep (sd_it);
1977 /* Don't bother trying to mark next as ready if insn is a debug
1978 insn. If insn is the last hard dependency, it will have
1979 already been discounted. */
1980 if (DEBUG_INSN_P (insn) && !DEBUG_INSN_P (next))
1981 continue;
1983 if (!IS_SPECULATION_BRANCHY_CHECK_P (insn))
1985 int effective_cost;
1987 effective_cost = try_ready (next);
1989 if (effective_cost >= 0
1990 && SCHED_GROUP_P (next)
1991 && advance < effective_cost)
1992 advance = effective_cost;
1994 else
1995 /* Check always has only one forward dependence (to the first insn in
1996 the recovery block), therefore, this will be executed only once. */
1998 gcc_assert (sd_lists_empty_p (insn, SD_LIST_FORW));
1999 fix_recovery_deps (RECOVERY_BLOCK (insn));
2003 /* Annotate the instruction with issue information -- TImode
2004 indicates that the instruction is expected not to be able
2005 to issue on the same cycle as the previous insn. A machine
2006 may use this information to decide how the instruction should
2007 be aligned. */
2008 if (issue_rate > 1
2009 && GET_CODE (PATTERN (insn)) != USE
2010 && GET_CODE (PATTERN (insn)) != CLOBBER
2011 && !DEBUG_INSN_P (insn))
2013 if (reload_completed)
2014 PUT_MODE (insn, clock_var > last_clock_var ? TImode : VOIDmode);
2015 last_clock_var = clock_var;
2018 return advance;
2021 /* Functions for handling of notes. */
2023 /* Add note list that ends on FROM_END to the end of TO_ENDP. */
2024 void
2025 concat_note_lists (rtx from_end, rtx *to_endp)
2027 rtx from_start;
2029 /* It's easy when have nothing to concat. */
2030 if (from_end == NULL)
2031 return;
2033 /* It's also easy when destination is empty. */
2034 if (*to_endp == NULL)
2036 *to_endp = from_end;
2037 return;
2040 from_start = from_end;
2041 while (PREV_INSN (from_start) != NULL)
2042 from_start = PREV_INSN (from_start);
2044 PREV_INSN (from_start) = *to_endp;
2045 NEXT_INSN (*to_endp) = from_start;
2046 *to_endp = from_end;
2049 /* Delete notes between HEAD and TAIL and put them in the chain
2050 of notes ended by NOTE_LIST. */
2051 void
2052 remove_notes (rtx head, rtx tail)
2054 rtx next_tail, insn, next;
2056 note_list = 0;
2057 if (head == tail && !INSN_P (head))
2058 return;
2060 next_tail = NEXT_INSN (tail);
2061 for (insn = head; insn != next_tail; insn = next)
2063 next = NEXT_INSN (insn);
2064 if (!NOTE_P (insn))
2065 continue;
2067 switch (NOTE_KIND (insn))
2069 case NOTE_INSN_BASIC_BLOCK:
2070 continue;
2072 case NOTE_INSN_EPILOGUE_BEG:
2073 if (insn != tail)
2075 remove_insn (insn);
2076 add_reg_note (next, REG_SAVE_NOTE,
2077 GEN_INT (NOTE_INSN_EPILOGUE_BEG));
2078 break;
2080 /* FALLTHRU */
2082 default:
2083 remove_insn (insn);
2085 /* Add the note to list that ends at NOTE_LIST. */
2086 PREV_INSN (insn) = note_list;
2087 NEXT_INSN (insn) = NULL_RTX;
2088 if (note_list)
2089 NEXT_INSN (note_list) = insn;
2090 note_list = insn;
2091 break;
2094 gcc_assert ((sel_sched_p () || insn != tail) && insn != head);
2098 /* A structure to record enough data to allow us to backtrack the scheduler to
2099 a previous state. */
2100 struct haifa_saved_data
2102 /* Next entry on the list. */
2103 struct haifa_saved_data *next;
2105 /* Backtracking is associated with scheduling insns that have delay slots.
2106 DELAY_PAIR points to the structure that contains the insns involved, and
2107 the number of cycles between them. */
2108 struct delay_pair *delay_pair;
2110 /* Data used by the frontend (e.g. sched-ebb or sched-rgn). */
2111 void *fe_saved_data;
2112 /* Data used by the backend. */
2113 void *be_saved_data;
2115 /* Copies of global state. */
2116 int clock_var, last_clock_var;
2117 struct ready_list ready;
2118 state_t curr_state;
2120 rtx last_scheduled_insn;
2121 rtx last_nondebug_scheduled_insn;
2122 int cycle_issued_insns;
2124 /* Copies of state used in the inner loop of schedule_block. */
2125 struct sched_block_state sched_block;
2127 /* We don't need to save q_ptr, as its value is arbitrary and we can set it
2128 to 0 when restoring. */
2129 int q_size;
2130 rtx *insn_queue;
2133 /* A record, in reverse order, of all scheduled insns which have delay slots
2134 and may require backtracking. */
2135 static struct haifa_saved_data *backtrack_queue;
2137 /* For every dependency of INSN, set the FEEDS_BACKTRACK_INSN bit according
2138 to SET_P. */
2139 static void
2140 mark_backtrack_feeds (rtx insn, int set_p)
2142 sd_iterator_def sd_it;
2143 dep_t dep;
2144 FOR_EACH_DEP (insn, SD_LIST_HARD_BACK, sd_it, dep)
2146 FEEDS_BACKTRACK_INSN (DEP_PRO (dep)) = set_p;
2150 /* Make a copy of the INSN_LIST list LINK and return it. */
2151 static rtx
2152 copy_insn_list (rtx link)
2154 rtx new_queue;
2155 rtx *pqueue = &new_queue;
2157 for (; link; link = XEXP (link, 1))
2159 rtx x = XEXP (link, 0);
2160 rtx newlink = alloc_INSN_LIST (x, NULL);
2161 *pqueue = newlink;
2162 pqueue = &XEXP (newlink, 1);
2164 *pqueue = NULL_RTX;
2165 return new_queue;
2168 /* Save the current scheduler state so that we can backtrack to it
2169 later if necessary. PAIR gives the insns that make it necessary to
2170 save this point. SCHED_BLOCK is the local state of schedule_block
2171 that need to be saved. */
2172 static void
2173 save_backtrack_point (struct delay_pair *pair,
2174 struct sched_block_state sched_block)
2176 int i;
2177 struct haifa_saved_data *save = XNEW (struct haifa_saved_data);
2179 save->curr_state = xmalloc (dfa_state_size);
2180 memcpy (save->curr_state, curr_state, dfa_state_size);
2182 save->ready.first = ready.first;
2183 save->ready.n_ready = ready.n_ready;
2184 save->ready.n_debug = ready.n_debug;
2185 save->ready.veclen = ready.veclen;
2186 save->ready.vec = XNEWVEC (rtx, ready.veclen);
2187 memcpy (save->ready.vec, ready.vec, ready.veclen * sizeof (rtx));
2189 save->insn_queue = XNEWVEC (rtx, max_insn_queue_index + 1);
2190 save->q_size = q_size;
2191 for (i = 0; i <= max_insn_queue_index; i++)
2193 int q = NEXT_Q_AFTER (q_ptr, i);
2194 save->insn_queue[i] = copy_insn_list (insn_queue[q]);
2197 save->clock_var = clock_var;
2198 save->last_clock_var = last_clock_var;
2199 save->cycle_issued_insns = cycle_issued_insns;
2200 save->last_scheduled_insn = last_scheduled_insn;
2201 save->last_nondebug_scheduled_insn = last_nondebug_scheduled_insn;
2203 save->sched_block = sched_block;
2205 if (current_sched_info->save_state)
2206 save->fe_saved_data = (*current_sched_info->save_state) ();
2208 if (targetm.sched.alloc_sched_context)
2210 save->be_saved_data = targetm.sched.alloc_sched_context ();
2211 targetm.sched.init_sched_context (save->be_saved_data, false);
2213 else
2214 save->be_saved_data = NULL;
2216 save->delay_pair = pair;
2218 save->next = backtrack_queue;
2219 backtrack_queue = save;
2221 while (pair)
2223 mark_backtrack_feeds (pair->i2, 1);
2224 INSN_TICK (pair->i2) = INVALID_TICK;
2225 INSN_EXACT_TICK (pair->i2) = clock_var + pair_delay (pair);
2226 SHADOW_P (pair->i2) = true;
2227 pair = pair->next_same_i1;
2231 /* Pop entries from the SCHEDULED_INSNS vector up to and including INSN.
2232 Restore their dependencies to an unresolved state, and mark them as
2233 queued nowhere. */
2235 static void
2236 unschedule_insns_until (rtx insn)
2238 for (;;)
2240 rtx last;
2241 sd_iterator_def sd_it;
2242 dep_t dep;
2244 last = VEC_pop (rtx, scheduled_insns);
2246 /* This will be changed by restore_backtrack_point if the insn is in
2247 any queue. */
2248 QUEUE_INDEX (last) = QUEUE_NOWHERE;
2249 if (last != insn)
2250 INSN_TICK (last) = INVALID_TICK;
2252 for (sd_it = sd_iterator_start (last, SD_LIST_RES_FORW);
2253 sd_iterator_cond (&sd_it, &dep);)
2255 rtx con = DEP_CON (dep);
2256 TODO_SPEC (con) |= HARD_DEP;
2257 INSN_TICK (con) = INVALID_TICK;
2258 sd_unresolve_dep (sd_it);
2261 if (last == insn)
2262 break;
2266 /* Restore scheduler state from the topmost entry on the backtracking queue.
2267 PSCHED_BLOCK_P points to the local data of schedule_block that we must
2268 overwrite with the saved data.
2269 The caller must already have called unschedule_insns_until. */
2271 static void
2272 restore_last_backtrack_point (struct sched_block_state *psched_block)
2275 rtx link;
2276 int i;
2277 struct haifa_saved_data *save = backtrack_queue;
2279 backtrack_queue = save->next;
2281 if (current_sched_info->restore_state)
2282 (*current_sched_info->restore_state) (save->fe_saved_data);
2284 if (targetm.sched.alloc_sched_context)
2286 targetm.sched.set_sched_context (save->be_saved_data);
2287 targetm.sched.free_sched_context (save->be_saved_data);
2290 /* Clear the QUEUE_INDEX of everything in the ready list or one
2291 of the queues. */
2292 if (ready.n_ready > 0)
2294 rtx *first = ready_lastpos (&ready);
2295 for (i = 0; i < ready.n_ready; i++)
2297 QUEUE_INDEX (first[i]) = QUEUE_NOWHERE;
2298 INSN_TICK (first[i]) = INVALID_TICK;
2301 for (i = 0; i <= max_insn_queue_index; i++)
2303 int q = NEXT_Q_AFTER (q_ptr, i);
2305 for (link = insn_queue[q]; link; link = XEXP (link, 1))
2307 rtx x = XEXP (link, 0);
2308 QUEUE_INDEX (x) = QUEUE_NOWHERE;
2309 INSN_TICK (x) = INVALID_TICK;
2311 free_INSN_LIST_list (&insn_queue[q]);
2314 free (ready.vec);
2315 ready = save->ready;
2317 if (ready.n_ready > 0)
2319 rtx *first = ready_lastpos (&ready);
2320 for (i = 0; i < ready.n_ready; i++)
2322 QUEUE_INDEX (first[i]) = QUEUE_READY;
2323 INSN_TICK (first[i]) = save->clock_var;
2327 q_ptr = 0;
2328 q_size = save->q_size;
2329 for (i = 0; i <= max_insn_queue_index; i++)
2331 int q = NEXT_Q_AFTER (q_ptr, i);
2333 insn_queue[q] = save->insn_queue[q];
2335 for (link = insn_queue[q]; link; link = XEXP (link, 1))
2337 rtx x = XEXP (link, 0);
2338 QUEUE_INDEX (x) = i;
2339 INSN_TICK (x) = save->clock_var + i;
2342 free (save->insn_queue);
2344 clock_var = save->clock_var;
2345 last_clock_var = save->last_clock_var;
2346 cycle_issued_insns = save->cycle_issued_insns;
2347 last_scheduled_insn = save->last_scheduled_insn;
2348 last_nondebug_scheduled_insn = save->last_nondebug_scheduled_insn;
2350 *psched_block = save->sched_block;
2352 memcpy (curr_state, save->curr_state, dfa_state_size);
2353 free (save->curr_state);
2355 mark_backtrack_feeds (save->delay_pair->i2, 0);
2357 free (save);
2359 for (save = backtrack_queue; save; save = save->next)
2361 mark_backtrack_feeds (save->delay_pair->i2, 1);
2365 /* Discard all data associated with the topmost entry in the backtrack
2366 queue. If RESET_TICK is false, we just want to free the data. If true,
2367 we are doing this because we discovered a reason to backtrack. In the
2368 latter case, also reset the INSN_TICK for the shadow insn. */
2369 static void
2370 free_topmost_backtrack_point (bool reset_tick)
2372 struct haifa_saved_data *save = backtrack_queue;
2373 int i;
2375 backtrack_queue = save->next;
2377 if (reset_tick)
2379 struct delay_pair *pair = save->delay_pair;
2380 while (pair)
2382 INSN_TICK (pair->i2) = INVALID_TICK;
2383 INSN_EXACT_TICK (pair->i2) = INVALID_TICK;
2384 pair = pair->next_same_i1;
2387 if (targetm.sched.free_sched_context)
2388 targetm.sched.free_sched_context (save->be_saved_data);
2389 if (current_sched_info->restore_state)
2390 free (save->fe_saved_data);
2391 for (i = 0; i <= max_insn_queue_index; i++)
2392 free_INSN_LIST_list (&save->insn_queue[i]);
2393 free (save->insn_queue);
2394 free (save->curr_state);
2395 free (save->ready.vec);
2396 free (save);
2399 /* Free the entire backtrack queue. */
2400 static void
2401 free_backtrack_queue (void)
2403 while (backtrack_queue)
2404 free_topmost_backtrack_point (false);
2407 /* Compute INSN_TICK_ESTIMATE for INSN. PROCESSED is a bitmap of
2408 instructions we've previously encountered, a set bit prevents
2409 recursion. BUDGET is a limit on how far ahead we look, it is
2410 reduced on recursive calls. Return true if we produced a good
2411 estimate, or false if we exceeded the budget. */
2412 static bool
2413 estimate_insn_tick (bitmap processed, rtx insn, int budget)
2415 sd_iterator_def sd_it;
2416 dep_t dep;
2417 int earliest = INSN_TICK (insn);
2419 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
2421 rtx pro = DEP_PRO (dep);
2422 int t;
2424 if (QUEUE_INDEX (pro) == QUEUE_SCHEDULED)
2425 gcc_assert (INSN_TICK (pro) + dep_cost (dep) <= INSN_TICK (insn));
2426 else
2428 int cost = dep_cost (dep);
2429 if (cost >= budget)
2430 return false;
2431 if (!bitmap_bit_p (processed, INSN_LUID (pro)))
2433 if (!estimate_insn_tick (processed, pro, budget - cost))
2434 return false;
2436 gcc_assert (INSN_TICK_ESTIMATE (pro) != INVALID_TICK);
2437 t = INSN_TICK_ESTIMATE (pro) + cost;
2438 if (earliest == INVALID_TICK || t > earliest)
2439 earliest = t;
2442 bitmap_set_bit (processed, INSN_LUID (insn));
2443 INSN_TICK_ESTIMATE (insn) = earliest;
2444 return true;
2447 /* Examine the pair of insns in P, and estimate (optimistically, assuming
2448 infinite resources) the cycle in which the delayed shadow can be issued.
2449 Return the number of cycles that must pass before the real insn can be
2450 issued in order to meet this constraint. */
2451 static int
2452 estimate_shadow_tick (struct delay_pair *p)
2454 bitmap_head processed;
2455 int t;
2456 bool cutoff;
2457 bitmap_initialize (&processed, 0);
2459 cutoff = !estimate_insn_tick (&processed, p->i2,
2460 max_insn_queue_index + pair_delay (p));
2461 bitmap_clear (&processed);
2462 if (cutoff)
2463 return max_insn_queue_index;
2464 t = INSN_TICK_ESTIMATE (p->i2) - (clock_var + pair_delay (p) + 1);
2465 if (t > 0)
2466 return t;
2467 return 0;
2470 /* Return the head and tail pointers of ebb starting at BEG and ending
2471 at END. */
2472 void
2473 get_ebb_head_tail (basic_block beg, basic_block end, rtx *headp, rtx *tailp)
2475 rtx beg_head = BB_HEAD (beg);
2476 rtx beg_tail = BB_END (beg);
2477 rtx end_head = BB_HEAD (end);
2478 rtx end_tail = BB_END (end);
2480 /* Don't include any notes or labels at the beginning of the BEG
2481 basic block, or notes at the end of the END basic blocks. */
2483 if (LABEL_P (beg_head))
2484 beg_head = NEXT_INSN (beg_head);
2486 while (beg_head != beg_tail)
2487 if (NOTE_P (beg_head))
2488 beg_head = NEXT_INSN (beg_head);
2489 else if (DEBUG_INSN_P (beg_head))
2491 rtx note, next;
2493 for (note = NEXT_INSN (beg_head);
2494 note != beg_tail;
2495 note = next)
2497 next = NEXT_INSN (note);
2498 if (NOTE_P (note))
2500 if (sched_verbose >= 9)
2501 fprintf (sched_dump, "reorder %i\n", INSN_UID (note));
2503 reorder_insns_nobb (note, note, PREV_INSN (beg_head));
2505 if (BLOCK_FOR_INSN (note) != beg)
2506 df_insn_change_bb (note, beg);
2508 else if (!DEBUG_INSN_P (note))
2509 break;
2512 break;
2514 else
2515 break;
2517 *headp = beg_head;
2519 if (beg == end)
2520 end_head = beg_head;
2521 else if (LABEL_P (end_head))
2522 end_head = NEXT_INSN (end_head);
2524 while (end_head != end_tail)
2525 if (NOTE_P (end_tail))
2526 end_tail = PREV_INSN (end_tail);
2527 else if (DEBUG_INSN_P (end_tail))
2529 rtx note, prev;
2531 for (note = PREV_INSN (end_tail);
2532 note != end_head;
2533 note = prev)
2535 prev = PREV_INSN (note);
2536 if (NOTE_P (note))
2538 if (sched_verbose >= 9)
2539 fprintf (sched_dump, "reorder %i\n", INSN_UID (note));
2541 reorder_insns_nobb (note, note, end_tail);
2543 if (end_tail == BB_END (end))
2544 BB_END (end) = note;
2546 if (BLOCK_FOR_INSN (note) != end)
2547 df_insn_change_bb (note, end);
2549 else if (!DEBUG_INSN_P (note))
2550 break;
2553 break;
2555 else
2556 break;
2558 *tailp = end_tail;
2561 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ]. */
2564 no_real_insns_p (const_rtx head, const_rtx tail)
2566 while (head != NEXT_INSN (tail))
2568 if (!NOTE_P (head) && !LABEL_P (head))
2569 return 0;
2570 head = NEXT_INSN (head);
2572 return 1;
2575 /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
2576 previously found among the insns. Insert them just before HEAD. */
2578 restore_other_notes (rtx head, basic_block head_bb)
2580 if (note_list != 0)
2582 rtx note_head = note_list;
2584 if (head)
2585 head_bb = BLOCK_FOR_INSN (head);
2586 else
2587 head = NEXT_INSN (bb_note (head_bb));
2589 while (PREV_INSN (note_head))
2591 set_block_for_insn (note_head, head_bb);
2592 note_head = PREV_INSN (note_head);
2594 /* In the above cycle we've missed this note. */
2595 set_block_for_insn (note_head, head_bb);
2597 PREV_INSN (note_head) = PREV_INSN (head);
2598 NEXT_INSN (PREV_INSN (head)) = note_head;
2599 PREV_INSN (head) = note_list;
2600 NEXT_INSN (note_list) = head;
2602 if (BLOCK_FOR_INSN (head) != head_bb)
2603 BB_END (head_bb) = note_list;
2605 head = note_head;
2608 return head;
2611 /* Move insns that became ready to fire from queue to ready list. */
2613 static void
2614 queue_to_ready (struct ready_list *ready)
2616 rtx insn;
2617 rtx link;
2618 rtx skip_insn;
2620 q_ptr = NEXT_Q (q_ptr);
2622 if (dbg_cnt (sched_insn) == false)
2624 /* If debug counter is activated do not requeue the first
2625 nonscheduled insn. */
2626 skip_insn = nonscheduled_insns_begin;
2629 skip_insn = next_nonnote_nondebug_insn (skip_insn);
2631 while (QUEUE_INDEX (skip_insn) == QUEUE_SCHEDULED);
2633 else
2634 skip_insn = NULL_RTX;
2636 /* Add all pending insns that can be scheduled without stalls to the
2637 ready list. */
2638 for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
2640 insn = XEXP (link, 0);
2641 q_size -= 1;
2643 if (sched_verbose >= 2)
2644 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
2645 (*current_sched_info->print_insn) (insn, 0));
2647 /* If the ready list is full, delay the insn for 1 cycle.
2648 See the comment in schedule_block for the rationale. */
2649 if (!reload_completed
2650 && ready->n_ready - ready->n_debug > MAX_SCHED_READY_INSNS
2651 && !SCHED_GROUP_P (insn)
2652 && insn != skip_insn)
2653 queue_insn (insn, 1, "ready full");
2654 else
2656 ready_add (ready, insn, false);
2657 if (sched_verbose >= 2)
2658 fprintf (sched_dump, "moving to ready without stalls\n");
2661 free_INSN_LIST_list (&insn_queue[q_ptr]);
2663 /* If there are no ready insns, stall until one is ready and add all
2664 of the pending insns at that point to the ready list. */
2665 if (ready->n_ready == 0)
2667 int stalls;
2669 for (stalls = 1; stalls <= max_insn_queue_index; stalls++)
2671 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
2673 for (; link; link = XEXP (link, 1))
2675 insn = XEXP (link, 0);
2676 q_size -= 1;
2678 if (sched_verbose >= 2)
2679 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
2680 (*current_sched_info->print_insn) (insn, 0));
2682 ready_add (ready, insn, false);
2683 if (sched_verbose >= 2)
2684 fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
2686 free_INSN_LIST_list (&insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]);
2688 advance_one_cycle ();
2690 break;
2693 advance_one_cycle ();
2696 q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
2697 clock_var += stalls;
2701 /* Used by early_queue_to_ready. Determines whether it is "ok" to
2702 prematurely move INSN from the queue to the ready list. Currently,
2703 if a target defines the hook 'is_costly_dependence', this function
2704 uses the hook to check whether there exist any dependences which are
2705 considered costly by the target, between INSN and other insns that
2706 have already been scheduled. Dependences are checked up to Y cycles
2707 back, with default Y=1; The flag -fsched-stalled-insns-dep=Y allows
2708 controlling this value.
2709 (Other considerations could be taken into account instead (or in
2710 addition) depending on user flags and target hooks. */
2712 static bool
2713 ok_for_early_queue_removal (rtx insn)
2715 if (targetm.sched.is_costly_dependence)
2717 rtx prev_insn;
2718 int n_cycles;
2719 int i = VEC_length (rtx, scheduled_insns);
2720 for (n_cycles = flag_sched_stalled_insns_dep; n_cycles; n_cycles--)
2722 while (i-- > 0)
2724 int cost;
2726 prev_insn = VEC_index (rtx, scheduled_insns, i);
2728 if (!NOTE_P (prev_insn))
2730 dep_t dep;
2732 dep = sd_find_dep_between (prev_insn, insn, true);
2734 if (dep != NULL)
2736 cost = dep_cost (dep);
2738 if (targetm.sched.is_costly_dependence (dep, cost,
2739 flag_sched_stalled_insns_dep - n_cycles))
2740 return false;
2744 if (GET_MODE (prev_insn) == TImode) /* end of dispatch group */
2745 break;
2748 if (i == 0)
2749 break;
2753 return true;
2757 /* Remove insns from the queue, before they become "ready" with respect
2758 to FU latency considerations. */
2760 static int
2761 early_queue_to_ready (state_t state, struct ready_list *ready)
2763 rtx insn;
2764 rtx link;
2765 rtx next_link;
2766 rtx prev_link;
2767 bool move_to_ready;
2768 int cost;
2769 state_t temp_state = alloca (dfa_state_size);
2770 int stalls;
2771 int insns_removed = 0;
2774 Flag '-fsched-stalled-insns=X' determines the aggressiveness of this
2775 function:
2777 X == 0: There is no limit on how many queued insns can be removed
2778 prematurely. (flag_sched_stalled_insns = -1).
2780 X >= 1: Only X queued insns can be removed prematurely in each
2781 invocation. (flag_sched_stalled_insns = X).
2783 Otherwise: Early queue removal is disabled.
2784 (flag_sched_stalled_insns = 0)
2787 if (! flag_sched_stalled_insns)
2788 return 0;
2790 for (stalls = 0; stalls <= max_insn_queue_index; stalls++)
2792 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
2794 if (sched_verbose > 6)
2795 fprintf (sched_dump, ";; look at index %d + %d\n", q_ptr, stalls);
2797 prev_link = 0;
2798 while (link)
2800 next_link = XEXP (link, 1);
2801 insn = XEXP (link, 0);
2802 if (insn && sched_verbose > 6)
2803 print_rtl_single (sched_dump, insn);
2805 memcpy (temp_state, state, dfa_state_size);
2806 if (recog_memoized (insn) < 0)
2807 /* non-negative to indicate that it's not ready
2808 to avoid infinite Q->R->Q->R... */
2809 cost = 0;
2810 else
2811 cost = state_transition (temp_state, insn);
2813 if (sched_verbose >= 6)
2814 fprintf (sched_dump, "transition cost = %d\n", cost);
2816 move_to_ready = false;
2817 if (cost < 0)
2819 move_to_ready = ok_for_early_queue_removal (insn);
2820 if (move_to_ready == true)
2822 /* move from Q to R */
2823 q_size -= 1;
2824 ready_add (ready, insn, false);
2826 if (prev_link)
2827 XEXP (prev_link, 1) = next_link;
2828 else
2829 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = next_link;
2831 free_INSN_LIST_node (link);
2833 if (sched_verbose >= 2)
2834 fprintf (sched_dump, ";;\t\tEarly Q-->Ready: insn %s\n",
2835 (*current_sched_info->print_insn) (insn, 0));
2837 insns_removed++;
2838 if (insns_removed == flag_sched_stalled_insns)
2839 /* Remove no more than flag_sched_stalled_insns insns
2840 from Q at a time. */
2841 return insns_removed;
2845 if (move_to_ready == false)
2846 prev_link = link;
2848 link = next_link;
2849 } /* while link */
2850 } /* if link */
2852 } /* for stalls.. */
2854 return insns_removed;
2858 /* Print the ready list for debugging purposes. Callable from debugger. */
2860 static void
2861 debug_ready_list (struct ready_list *ready)
2863 rtx *p;
2864 int i;
2866 if (ready->n_ready == 0)
2868 fprintf (sched_dump, "\n");
2869 return;
2872 p = ready_lastpos (ready);
2873 for (i = 0; i < ready->n_ready; i++)
2875 fprintf (sched_dump, " %s:%d",
2876 (*current_sched_info->print_insn) (p[i], 0),
2877 INSN_LUID (p[i]));
2878 if (sched_pressure_p)
2879 fprintf (sched_dump, "(cost=%d",
2880 INSN_REG_PRESSURE_EXCESS_COST_CHANGE (p[i]));
2881 if (INSN_TICK (p[i]) > clock_var)
2882 fprintf (sched_dump, ":delay=%d", INSN_TICK (p[i]) - clock_var);
2883 if (sched_pressure_p)
2884 fprintf (sched_dump, ")");
2886 fprintf (sched_dump, "\n");
2889 /* Search INSN for REG_SAVE_NOTE notes and convert them back into insn
2890 NOTEs. This is used for NOTE_INSN_EPILOGUE_BEG, so that sched-ebb
2891 replaces the epilogue note in the correct basic block. */
2892 void
2893 reemit_notes (rtx insn)
2895 rtx note, last = insn;
2897 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2899 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
2901 enum insn_note note_type = (enum insn_note) INTVAL (XEXP (note, 0));
2903 last = emit_note_before (note_type, last);
2904 remove_note (insn, note);
2909 /* Move INSN. Reemit notes if needed. Update CFG, if needed. */
2910 static void
2911 move_insn (rtx insn, rtx last, rtx nt)
2913 if (PREV_INSN (insn) != last)
2915 basic_block bb;
2916 rtx note;
2917 int jump_p = 0;
2919 bb = BLOCK_FOR_INSN (insn);
2921 /* BB_HEAD is either LABEL or NOTE. */
2922 gcc_assert (BB_HEAD (bb) != insn);
2924 if (BB_END (bb) == insn)
2925 /* If this is last instruction in BB, move end marker one
2926 instruction up. */
2928 /* Jumps are always placed at the end of basic block. */
2929 jump_p = control_flow_insn_p (insn);
2931 gcc_assert (!jump_p
2932 || ((common_sched_info->sched_pass_id == SCHED_RGN_PASS)
2933 && IS_SPECULATION_BRANCHY_CHECK_P (insn))
2934 || (common_sched_info->sched_pass_id
2935 == SCHED_EBB_PASS));
2937 gcc_assert (BLOCK_FOR_INSN (PREV_INSN (insn)) == bb);
2939 BB_END (bb) = PREV_INSN (insn);
2942 gcc_assert (BB_END (bb) != last);
2944 if (jump_p)
2945 /* We move the block note along with jump. */
2947 gcc_assert (nt);
2949 note = NEXT_INSN (insn);
2950 while (NOTE_NOT_BB_P (note) && note != nt)
2951 note = NEXT_INSN (note);
2953 if (note != nt
2954 && (LABEL_P (note)
2955 || BARRIER_P (note)))
2956 note = NEXT_INSN (note);
2958 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
2960 else
2961 note = insn;
2963 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (note);
2964 PREV_INSN (NEXT_INSN (note)) = PREV_INSN (insn);
2966 NEXT_INSN (note) = NEXT_INSN (last);
2967 PREV_INSN (NEXT_INSN (last)) = note;
2969 NEXT_INSN (last) = insn;
2970 PREV_INSN (insn) = last;
2972 bb = BLOCK_FOR_INSN (last);
2974 if (jump_p)
2976 fix_jump_move (insn);
2978 if (BLOCK_FOR_INSN (insn) != bb)
2979 move_block_after_check (insn);
2981 gcc_assert (BB_END (bb) == last);
2984 df_insn_change_bb (insn, bb);
2986 /* Update BB_END, if needed. */
2987 if (BB_END (bb) == last)
2988 BB_END (bb) = insn;
2991 SCHED_GROUP_P (insn) = 0;
2994 /* Return true if scheduling INSN will finish current clock cycle. */
2995 static bool
2996 insn_finishes_cycle_p (rtx insn)
2998 if (SCHED_GROUP_P (insn))
2999 /* After issuing INSN, rest of the sched_group will be forced to issue
3000 in order. Don't make any plans for the rest of cycle. */
3001 return true;
3003 /* Finishing the block will, apparently, finish the cycle. */
3004 if (current_sched_info->insn_finishes_block_p
3005 && current_sched_info->insn_finishes_block_p (insn))
3006 return true;
3008 return false;
3011 /* Define type for target data used in multipass scheduling. */
3012 #ifndef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DATA_T
3013 # define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DATA_T int
3014 #endif
3015 typedef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DATA_T first_cycle_multipass_data_t;
3017 /* The following structure describe an entry of the stack of choices. */
3018 struct choice_entry
3020 /* Ordinal number of the issued insn in the ready queue. */
3021 int index;
3022 /* The number of the rest insns whose issues we should try. */
3023 int rest;
3024 /* The number of issued essential insns. */
3025 int n;
3026 /* State after issuing the insn. */
3027 state_t state;
3028 /* Target-specific data. */
3029 first_cycle_multipass_data_t target_data;
3032 /* The following array is used to implement a stack of choices used in
3033 function max_issue. */
3034 static struct choice_entry *choice_stack;
3036 /* This holds the value of the target dfa_lookahead hook. */
3037 int dfa_lookahead;
3039 /* The following variable value is maximal number of tries of issuing
3040 insns for the first cycle multipass insn scheduling. We define
3041 this value as constant*(DFA_LOOKAHEAD**ISSUE_RATE). We would not
3042 need this constraint if all real insns (with non-negative codes)
3043 had reservations because in this case the algorithm complexity is
3044 O(DFA_LOOKAHEAD**ISSUE_RATE). Unfortunately, the dfa descriptions
3045 might be incomplete and such insn might occur. For such
3046 descriptions, the complexity of algorithm (without the constraint)
3047 could achieve DFA_LOOKAHEAD ** N , where N is the queue length. */
3048 static int max_lookahead_tries;
3050 /* The following value is value of hook
3051 `first_cycle_multipass_dfa_lookahead' at the last call of
3052 `max_issue'. */
3053 static int cached_first_cycle_multipass_dfa_lookahead = 0;
3055 /* The following value is value of `issue_rate' at the last call of
3056 `sched_init'. */
3057 static int cached_issue_rate = 0;
3059 /* The following function returns maximal (or close to maximal) number
3060 of insns which can be issued on the same cycle and one of which
3061 insns is insns with the best rank (the first insn in READY). To
3062 make this function tries different samples of ready insns. READY
3063 is current queue `ready'. Global array READY_TRY reflects what
3064 insns are already issued in this try. The function stops immediately,
3065 if it reached the such a solution, that all instruction can be issued.
3066 INDEX will contain index of the best insn in READY. The following
3067 function is used only for first cycle multipass scheduling.
3069 PRIVILEGED_N >= 0
3071 This function expects recognized insns only. All USEs,
3072 CLOBBERs, etc must be filtered elsewhere. */
3074 max_issue (struct ready_list *ready, int privileged_n, state_t state,
3075 bool first_cycle_insn_p, int *index)
3077 int n, i, all, n_ready, best, delay, tries_num;
3078 int more_issue;
3079 struct choice_entry *top;
3080 rtx insn;
3082 n_ready = ready->n_ready;
3083 gcc_assert (dfa_lookahead >= 1 && privileged_n >= 0
3084 && privileged_n <= n_ready);
3086 /* Init MAX_LOOKAHEAD_TRIES. */
3087 if (cached_first_cycle_multipass_dfa_lookahead != dfa_lookahead)
3089 cached_first_cycle_multipass_dfa_lookahead = dfa_lookahead;
3090 max_lookahead_tries = 100;
3091 for (i = 0; i < issue_rate; i++)
3092 max_lookahead_tries *= dfa_lookahead;
3095 /* Init max_points. */
3096 more_issue = issue_rate - cycle_issued_insns;
3097 gcc_assert (more_issue >= 0);
3099 /* The number of the issued insns in the best solution. */
3100 best = 0;
3102 top = choice_stack;
3104 /* Set initial state of the search. */
3105 memcpy (top->state, state, dfa_state_size);
3106 top->rest = dfa_lookahead;
3107 top->n = 0;
3108 if (targetm.sched.first_cycle_multipass_begin)
3109 targetm.sched.first_cycle_multipass_begin (&top->target_data,
3110 ready_try, n_ready,
3111 first_cycle_insn_p);
3113 /* Count the number of the insns to search among. */
3114 for (all = i = 0; i < n_ready; i++)
3115 if (!ready_try [i])
3116 all++;
3118 /* I is the index of the insn to try next. */
3119 i = 0;
3120 tries_num = 0;
3121 for (;;)
3123 if (/* If we've reached a dead end or searched enough of what we have
3124 been asked... */
3125 top->rest == 0
3126 /* or have nothing else to try... */
3127 || i >= n_ready
3128 /* or should not issue more. */
3129 || top->n >= more_issue)
3131 /* ??? (... || i == n_ready). */
3132 gcc_assert (i <= n_ready);
3134 /* We should not issue more than issue_rate instructions. */
3135 gcc_assert (top->n <= more_issue);
3137 if (top == choice_stack)
3138 break;
3140 if (best < top - choice_stack)
3142 if (privileged_n)
3144 n = privileged_n;
3145 /* Try to find issued privileged insn. */
3146 while (n && !ready_try[--n])
3150 if (/* If all insns are equally good... */
3151 privileged_n == 0
3152 /* Or a privileged insn will be issued. */
3153 || ready_try[n])
3154 /* Then we have a solution. */
3156 best = top - choice_stack;
3157 /* This is the index of the insn issued first in this
3158 solution. */
3159 *index = choice_stack [1].index;
3160 if (top->n == more_issue || best == all)
3161 break;
3165 /* Set ready-list index to point to the last insn
3166 ('i++' below will advance it to the next insn). */
3167 i = top->index;
3169 /* Backtrack. */
3170 ready_try [i] = 0;
3172 if (targetm.sched.first_cycle_multipass_backtrack)
3173 targetm.sched.first_cycle_multipass_backtrack (&top->target_data,
3174 ready_try, n_ready);
3176 top--;
3177 memcpy (state, top->state, dfa_state_size);
3179 else if (!ready_try [i])
3181 tries_num++;
3182 if (tries_num > max_lookahead_tries)
3183 break;
3184 insn = ready_element (ready, i);
3185 delay = state_transition (state, insn);
3186 if (delay < 0)
3188 if (state_dead_lock_p (state)
3189 || insn_finishes_cycle_p (insn))
3190 /* We won't issue any more instructions in the next
3191 choice_state. */
3192 top->rest = 0;
3193 else
3194 top->rest--;
3196 n = top->n;
3197 if (memcmp (top->state, state, dfa_state_size) != 0)
3198 n++;
3200 /* Advance to the next choice_entry. */
3201 top++;
3202 /* Initialize it. */
3203 top->rest = dfa_lookahead;
3204 top->index = i;
3205 top->n = n;
3206 memcpy (top->state, state, dfa_state_size);
3207 ready_try [i] = 1;
3209 if (targetm.sched.first_cycle_multipass_issue)
3210 targetm.sched.first_cycle_multipass_issue (&top->target_data,
3211 ready_try, n_ready,
3212 insn,
3213 &((top - 1)
3214 ->target_data));
3216 i = -1;
3220 /* Increase ready-list index. */
3221 i++;
3224 if (targetm.sched.first_cycle_multipass_end)
3225 targetm.sched.first_cycle_multipass_end (best != 0
3226 ? &choice_stack[1].target_data
3227 : NULL);
3229 /* Restore the original state of the DFA. */
3230 memcpy (state, choice_stack->state, dfa_state_size);
3232 return best;
3235 /* The following function chooses insn from READY and modifies
3236 READY. The following function is used only for first
3237 cycle multipass scheduling.
3238 Return:
3239 -1 if cycle should be advanced,
3240 0 if INSN_PTR is set to point to the desirable insn,
3241 1 if choose_ready () should be restarted without advancing the cycle. */
3242 static int
3243 choose_ready (struct ready_list *ready, bool first_cycle_insn_p,
3244 rtx *insn_ptr)
3246 int lookahead;
3248 if (dbg_cnt (sched_insn) == false)
3250 rtx insn = nonscheduled_insns_begin;
3253 insn = next_nonnote_insn (insn);
3255 while (QUEUE_INDEX (insn) == QUEUE_SCHEDULED);
3257 if (QUEUE_INDEX (insn) == QUEUE_READY)
3258 /* INSN is in the ready_list. */
3260 nonscheduled_insns_begin = insn;
3261 ready_remove_insn (insn);
3262 *insn_ptr = insn;
3263 return 0;
3266 /* INSN is in the queue. Advance cycle to move it to the ready list. */
3267 return -1;
3270 lookahead = 0;
3272 if (targetm.sched.first_cycle_multipass_dfa_lookahead)
3273 lookahead = targetm.sched.first_cycle_multipass_dfa_lookahead ();
3274 if (lookahead <= 0 || SCHED_GROUP_P (ready_element (ready, 0))
3275 || DEBUG_INSN_P (ready_element (ready, 0)))
3277 if (targetm.sched.dispatch (NULL_RTX, IS_DISPATCH_ON))
3278 *insn_ptr = ready_remove_first_dispatch (ready);
3279 else
3280 *insn_ptr = ready_remove_first (ready);
3282 return 0;
3284 else
3286 /* Try to choose the better insn. */
3287 int index = 0, i, n;
3288 rtx insn;
3289 int try_data = 1, try_control = 1;
3290 ds_t ts;
3292 insn = ready_element (ready, 0);
3293 if (INSN_CODE (insn) < 0)
3295 *insn_ptr = ready_remove_first (ready);
3296 return 0;
3299 if (spec_info
3300 && spec_info->flags & (PREFER_NON_DATA_SPEC
3301 | PREFER_NON_CONTROL_SPEC))
3303 for (i = 0, n = ready->n_ready; i < n; i++)
3305 rtx x;
3306 ds_t s;
3308 x = ready_element (ready, i);
3309 s = TODO_SPEC (x);
3311 if (spec_info->flags & PREFER_NON_DATA_SPEC
3312 && !(s & DATA_SPEC))
3314 try_data = 0;
3315 if (!(spec_info->flags & PREFER_NON_CONTROL_SPEC)
3316 || !try_control)
3317 break;
3320 if (spec_info->flags & PREFER_NON_CONTROL_SPEC
3321 && !(s & CONTROL_SPEC))
3323 try_control = 0;
3324 if (!(spec_info->flags & PREFER_NON_DATA_SPEC) || !try_data)
3325 break;
3330 ts = TODO_SPEC (insn);
3331 if ((ts & SPECULATIVE)
3332 && (((!try_data && (ts & DATA_SPEC))
3333 || (!try_control && (ts & CONTROL_SPEC)))
3334 || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard_spec
3335 && !targetm.sched
3336 .first_cycle_multipass_dfa_lookahead_guard_spec (insn))))
3337 /* Discard speculative instruction that stands first in the ready
3338 list. */
3340 change_queue_index (insn, 1);
3341 return 1;
3344 ready_try[0] = 0;
3346 for (i = 1; i < ready->n_ready; i++)
3348 insn = ready_element (ready, i);
3350 ready_try [i]
3351 = ((!try_data && (TODO_SPEC (insn) & DATA_SPEC))
3352 || (!try_control && (TODO_SPEC (insn) & CONTROL_SPEC)));
3355 /* Let the target filter the search space. */
3356 for (i = 1; i < ready->n_ready; i++)
3357 if (!ready_try[i])
3359 insn = ready_element (ready, i);
3361 /* If this insn is recognizable we should have already
3362 recognized it earlier.
3363 ??? Not very clear where this is supposed to be done.
3364 See dep_cost_1. */
3365 gcc_checking_assert (INSN_CODE (insn) >= 0
3366 || recog_memoized (insn) < 0);
3368 ready_try [i]
3369 = (/* INSN_CODE check can be omitted here as it is also done later
3370 in max_issue (). */
3371 INSN_CODE (insn) < 0
3372 || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard
3373 && !targetm.sched.first_cycle_multipass_dfa_lookahead_guard
3374 (insn)));
3377 if (max_issue (ready, 1, curr_state, first_cycle_insn_p, &index) == 0)
3379 *insn_ptr = ready_remove_first (ready);
3380 if (sched_verbose >= 4)
3381 fprintf (sched_dump, ";;\t\tChosen insn (but can't issue) : %s \n",
3382 (*current_sched_info->print_insn) (*insn_ptr, 0));
3383 return 0;
3385 else
3387 if (sched_verbose >= 4)
3388 fprintf (sched_dump, ";;\t\tChosen insn : %s\n",
3389 (*current_sched_info->print_insn)
3390 (ready_element (ready, index), 0));
3392 *insn_ptr = ready_remove (ready, index);
3393 return 0;
3398 /* This function is called when we have successfully scheduled a
3399 block. It uses the schedule stored in the scheduled_insns vector
3400 to rearrange the RTL. PREV_HEAD is used as the anchor to which we
3401 append the scheduled insns; TAIL is the insn after the scheduled
3402 block. TARGET_BB is the argument passed to schedule_block. */
3404 static void
3405 commit_schedule (rtx prev_head, rtx tail, basic_block *target_bb)
3407 unsigned int i;
3408 rtx insn;
3410 last_scheduled_insn = prev_head;
3411 for (i = 0;
3412 VEC_iterate (rtx, scheduled_insns, i, insn);
3413 i++)
3415 if (control_flow_insn_p (last_scheduled_insn)
3416 || current_sched_info->advance_target_bb (*target_bb, insn))
3418 *target_bb = current_sched_info->advance_target_bb (*target_bb, 0);
3420 if (sched_verbose)
3422 rtx x;
3424 x = next_real_insn (last_scheduled_insn);
3425 gcc_assert (x);
3426 dump_new_block_header (1, *target_bb, x, tail);
3429 last_scheduled_insn = bb_note (*target_bb);
3432 if (current_sched_info->begin_move_insn)
3433 (*current_sched_info->begin_move_insn) (insn, last_scheduled_insn);
3434 move_insn (insn, last_scheduled_insn,
3435 current_sched_info->next_tail);
3436 if (!DEBUG_INSN_P (insn))
3437 reemit_notes (insn);
3438 last_scheduled_insn = insn;
3441 VEC_truncate (rtx, scheduled_insns, 0);
3444 /* Examine all insns on the ready list and queue those which can't be
3445 issued in this cycle. TEMP_STATE is temporary scheduler state we
3446 can use as scratch space. If FIRST_CYCLE_INSN_P is true, no insns
3447 have been issued for the current cycle, which means it is valid to
3448 issue an asm statement.
3450 If SHADOWS_ONLY_P is true, we eliminate all real insns and only
3451 leave those for which SHADOW_P is true.
3453 Return the number of cycles we must
3454 advance to find the next ready instruction, or zero if there remain
3455 insns on the ready list. */
3457 static void
3458 prune_ready_list (state_t temp_state, bool first_cycle_insn_p,
3459 bool shadows_only_p)
3461 int i;
3463 restart:
3464 for (i = 0; i < ready.n_ready; i++)
3466 rtx insn = ready_element (&ready, i);
3467 int cost = 0;
3468 const char *reason = "resource conflict";
3470 if (shadows_only_p && !DEBUG_INSN_P (insn) && !SHADOW_P (insn))
3472 cost = 1;
3473 reason = "not a shadow";
3475 else if (recog_memoized (insn) < 0)
3477 if (!first_cycle_insn_p
3478 && (GET_CODE (PATTERN (insn)) == ASM_INPUT
3479 || asm_noperands (PATTERN (insn)) >= 0))
3480 cost = 1;
3481 reason = "asm";
3483 else if (sched_pressure_p)
3484 cost = 0;
3485 else
3487 int delay_cost = 0;
3489 if (delay_htab)
3491 struct delay_pair *delay_entry;
3492 delay_entry
3493 = (struct delay_pair *)htab_find_with_hash (delay_htab, insn,
3494 htab_hash_pointer (insn));
3495 while (delay_entry && delay_cost == 0)
3497 delay_cost = estimate_shadow_tick (delay_entry);
3498 if (delay_cost > max_insn_queue_index)
3499 delay_cost = max_insn_queue_index;
3500 delay_entry = delay_entry->next_same_i1;
3504 memcpy (temp_state, curr_state, dfa_state_size);
3505 cost = state_transition (temp_state, insn);
3506 if (cost < 0)
3507 cost = 0;
3508 else if (cost == 0)
3509 cost = 1;
3510 if (cost < delay_cost)
3512 cost = delay_cost;
3513 reason = "shadow tick";
3516 if (cost >= 1)
3518 ready_remove (&ready, i);
3519 queue_insn (insn, cost, reason);
3520 goto restart;
3525 /* Called when we detect that the schedule is impossible. We examine the
3526 backtrack queue to find the earliest insn that caused this condition. */
3528 static struct haifa_saved_data *
3529 verify_shadows (void)
3531 struct haifa_saved_data *save, *earliest_fail = NULL;
3532 for (save = backtrack_queue; save; save = save->next)
3534 int t;
3535 struct delay_pair *pair = save->delay_pair;
3536 rtx i1 = pair->i1;
3538 for (; pair; pair = pair->next_same_i1)
3540 rtx i2 = pair->i2;
3542 if (QUEUE_INDEX (i2) == QUEUE_SCHEDULED)
3543 continue;
3545 t = INSN_TICK (i1) + pair_delay (pair);
3546 if (t < clock_var)
3548 if (sched_verbose >= 2)
3549 fprintf (sched_dump,
3550 ";;\t\tfailed delay requirements for %d/%d (%d->%d)"
3551 ", not ready\n",
3552 INSN_UID (pair->i1), INSN_UID (pair->i2),
3553 INSN_TICK (pair->i1), INSN_EXACT_TICK (pair->i2));
3554 earliest_fail = save;
3555 break;
3557 if (QUEUE_INDEX (i2) >= 0)
3559 int queued_for = INSN_TICK (i2);
3561 if (t < queued_for)
3563 if (sched_verbose >= 2)
3564 fprintf (sched_dump,
3565 ";;\t\tfailed delay requirements for %d/%d"
3566 " (%d->%d), queued too late\n",
3567 INSN_UID (pair->i1), INSN_UID (pair->i2),
3568 INSN_TICK (pair->i1), INSN_EXACT_TICK (pair->i2));
3569 earliest_fail = save;
3570 break;
3576 return earliest_fail;
3579 /* Use forward list scheduling to rearrange insns of block pointed to by
3580 TARGET_BB, possibly bringing insns from subsequent blocks in the same
3581 region. */
3583 void
3584 schedule_block (basic_block *target_bb)
3586 int i;
3587 struct sched_block_state ls;
3588 state_t temp_state = NULL; /* It is used for multipass scheduling. */
3589 int sort_p, advance, start_clock_var;
3591 /* Head/tail info for this block. */
3592 rtx prev_head = current_sched_info->prev_head;
3593 rtx next_tail = current_sched_info->next_tail;
3594 rtx head = NEXT_INSN (prev_head);
3595 rtx tail = PREV_INSN (next_tail);
3597 /* We used to have code to avoid getting parameters moved from hard
3598 argument registers into pseudos.
3600 However, it was removed when it proved to be of marginal benefit
3601 and caused problems because schedule_block and compute_forward_dependences
3602 had different notions of what the "head" insn was. */
3604 gcc_assert (head != tail || INSN_P (head));
3606 haifa_recovery_bb_recently_added_p = false;
3608 backtrack_queue = NULL;
3610 /* Debug info. */
3611 if (sched_verbose)
3612 dump_new_block_header (0, *target_bb, head, tail);
3614 state_reset (curr_state);
3616 /* Clear the ready list. */
3617 ready.first = ready.veclen - 1;
3618 ready.n_ready = 0;
3619 ready.n_debug = 0;
3621 /* It is used for first cycle multipass scheduling. */
3622 temp_state = alloca (dfa_state_size);
3624 if (targetm.sched.init)
3625 targetm.sched.init (sched_dump, sched_verbose, ready.veclen);
3627 /* We start inserting insns after PREV_HEAD. */
3628 last_scheduled_insn = nonscheduled_insns_begin = prev_head;
3629 last_nondebug_scheduled_insn = NULL_RTX;
3631 gcc_assert ((NOTE_P (last_scheduled_insn)
3632 || DEBUG_INSN_P (last_scheduled_insn))
3633 && BLOCK_FOR_INSN (last_scheduled_insn) == *target_bb);
3635 /* Initialize INSN_QUEUE. Q_SIZE is the total number of insns in the
3636 queue. */
3637 q_ptr = 0;
3638 q_size = 0;
3640 insn_queue = XALLOCAVEC (rtx, max_insn_queue_index + 1);
3641 memset (insn_queue, 0, (max_insn_queue_index + 1) * sizeof (rtx));
3643 /* Start just before the beginning of time. */
3644 clock_var = -1;
3646 /* We need queue and ready lists and clock_var be initialized
3647 in try_ready () (which is called through init_ready_list ()). */
3648 (*current_sched_info->init_ready_list) ();
3650 /* The algorithm is O(n^2) in the number of ready insns at any given
3651 time in the worst case. Before reload we are more likely to have
3652 big lists so truncate them to a reasonable size. */
3653 if (!reload_completed
3654 && ready.n_ready - ready.n_debug > MAX_SCHED_READY_INSNS)
3656 ready_sort (&ready);
3658 /* Find first free-standing insn past MAX_SCHED_READY_INSNS.
3659 If there are debug insns, we know they're first. */
3660 for (i = MAX_SCHED_READY_INSNS + ready.n_debug; i < ready.n_ready; i++)
3661 if (!SCHED_GROUP_P (ready_element (&ready, i)))
3662 break;
3664 if (sched_verbose >= 2)
3666 fprintf (sched_dump,
3667 ";;\t\tReady list on entry: %d insns\n", ready.n_ready);
3668 fprintf (sched_dump,
3669 ";;\t\t before reload => truncated to %d insns\n", i);
3672 /* Delay all insns past it for 1 cycle. If debug counter is
3673 activated make an exception for the insn right after
3674 nonscheduled_insns_begin. */
3676 rtx skip_insn;
3678 if (dbg_cnt (sched_insn) == false)
3679 skip_insn = next_nonnote_insn (nonscheduled_insns_begin);
3680 else
3681 skip_insn = NULL_RTX;
3683 while (i < ready.n_ready)
3685 rtx insn;
3687 insn = ready_remove (&ready, i);
3689 if (insn != skip_insn)
3690 queue_insn (insn, 1, "list truncated");
3692 if (skip_insn)
3693 ready_add (&ready, skip_insn, true);
3697 /* Now we can restore basic block notes and maintain precise cfg. */
3698 restore_bb_notes (*target_bb);
3700 last_clock_var = -1;
3702 advance = 0;
3704 gcc_assert (VEC_length (rtx, scheduled_insns) == 0);
3705 sort_p = TRUE;
3706 must_backtrack = false;
3708 /* Loop until all the insns in BB are scheduled. */
3709 while ((*current_sched_info->schedule_more_p) ())
3713 start_clock_var = clock_var;
3715 clock_var++;
3717 advance_one_cycle ();
3719 /* Add to the ready list all pending insns that can be issued now.
3720 If there are no ready insns, increment clock until one
3721 is ready and add all pending insns at that point to the ready
3722 list. */
3723 queue_to_ready (&ready);
3725 gcc_assert (ready.n_ready);
3727 if (sched_verbose >= 2)
3729 fprintf (sched_dump, ";;\t\tReady list after queue_to_ready: ");
3730 debug_ready_list (&ready);
3732 advance -= clock_var - start_clock_var;
3734 while (advance > 0);
3736 if (ready.n_ready > 0)
3737 prune_ready_list (temp_state, true, false);
3738 if (ready.n_ready == 0)
3739 continue;
3740 if (must_backtrack)
3741 goto do_backtrack;
3743 ls.first_cycle_insn_p = true;
3744 ls.shadows_only_p = false;
3745 cycle_issued_insns = 0;
3746 ls.can_issue_more = issue_rate;
3747 for (;;)
3749 rtx insn;
3750 int cost;
3751 bool asm_p;
3753 if (sort_p && ready.n_ready > 0)
3755 /* Sort the ready list based on priority. This must be
3756 done every iteration through the loop, as schedule_insn
3757 may have readied additional insns that will not be
3758 sorted correctly. */
3759 ready_sort (&ready);
3761 if (sched_verbose >= 2)
3763 fprintf (sched_dump, ";;\t\tReady list after ready_sort: ");
3764 debug_ready_list (&ready);
3768 /* We don't want md sched reorder to even see debug isns, so put
3769 them out right away. */
3770 if (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0))
3771 && (*current_sched_info->schedule_more_p) ())
3773 while (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0)))
3775 rtx insn = ready_remove_first (&ready);
3776 gcc_assert (DEBUG_INSN_P (insn));
3777 (*current_sched_info->begin_schedule_ready) (insn);
3778 VEC_safe_push (rtx, heap, scheduled_insns, insn);
3779 last_scheduled_insn = insn;
3780 advance = schedule_insn (insn);
3781 gcc_assert (advance == 0);
3782 if (ready.n_ready > 0)
3783 ready_sort (&ready);
3787 if (ls.first_cycle_insn_p && !ready.n_ready)
3788 break;
3790 resume_after_backtrack:
3791 /* Allow the target to reorder the list, typically for
3792 better instruction bundling. */
3793 if (sort_p
3794 && (ready.n_ready == 0
3795 || !SCHED_GROUP_P (ready_element (&ready, 0))))
3797 if (ls.first_cycle_insn_p && targetm.sched.reorder)
3798 ls.can_issue_more
3799 = targetm.sched.reorder (sched_dump, sched_verbose,
3800 ready_lastpos (&ready),
3801 &ready.n_ready, clock_var);
3802 else if (!ls.first_cycle_insn_p && targetm.sched.reorder2)
3803 ls.can_issue_more
3804 = targetm.sched.reorder2 (sched_dump, sched_verbose,
3805 ready.n_ready
3806 ? ready_lastpos (&ready) : NULL,
3807 &ready.n_ready, clock_var);
3810 restart_choose_ready:
3811 if (sched_verbose >= 2)
3813 fprintf (sched_dump, ";;\tReady list (t = %3d): ",
3814 clock_var);
3815 debug_ready_list (&ready);
3816 if (sched_pressure_p)
3817 print_curr_reg_pressure ();
3820 if (ready.n_ready == 0
3821 && ls.can_issue_more
3822 && reload_completed)
3824 /* Allow scheduling insns directly from the queue in case
3825 there's nothing better to do (ready list is empty) but
3826 there are still vacant dispatch slots in the current cycle. */
3827 if (sched_verbose >= 6)
3828 fprintf (sched_dump,";;\t\tSecond chance\n");
3829 memcpy (temp_state, curr_state, dfa_state_size);
3830 if (early_queue_to_ready (temp_state, &ready))
3831 ready_sort (&ready);
3834 if (ready.n_ready == 0
3835 || !ls.can_issue_more
3836 || state_dead_lock_p (curr_state)
3837 || !(*current_sched_info->schedule_more_p) ())
3838 break;
3840 /* Select and remove the insn from the ready list. */
3841 if (sort_p)
3843 int res;
3845 insn = NULL_RTX;
3846 res = choose_ready (&ready, ls.first_cycle_insn_p, &insn);
3848 if (res < 0)
3849 /* Finish cycle. */
3850 break;
3851 if (res > 0)
3852 goto restart_choose_ready;
3854 gcc_assert (insn != NULL_RTX);
3856 else
3857 insn = ready_remove_first (&ready);
3859 if (sched_pressure_p && INSN_TICK (insn) > clock_var)
3861 ready_add (&ready, insn, true);
3862 advance = 1;
3863 break;
3866 if (targetm.sched.dfa_new_cycle
3867 && targetm.sched.dfa_new_cycle (sched_dump, sched_verbose,
3868 insn, last_clock_var,
3869 clock_var, &sort_p))
3870 /* SORT_P is used by the target to override sorting
3871 of the ready list. This is needed when the target
3872 has modified its internal structures expecting that
3873 the insn will be issued next. As we need the insn
3874 to have the highest priority (so it will be returned by
3875 the ready_remove_first call above), we invoke
3876 ready_add (&ready, insn, true).
3877 But, still, there is one issue: INSN can be later
3878 discarded by scheduler's front end through
3879 current_sched_info->can_schedule_ready_p, hence, won't
3880 be issued next. */
3882 ready_add (&ready, insn, true);
3883 break;
3886 sort_p = TRUE;
3888 if (current_sched_info->can_schedule_ready_p
3889 && ! (*current_sched_info->can_schedule_ready_p) (insn))
3890 /* We normally get here only if we don't want to move
3891 insn from the split block. */
3893 TODO_SPEC (insn) = HARD_DEP;
3894 goto restart_choose_ready;
3897 if (delay_htab)
3899 /* If this insn is the first part of a delay-slot pair, record a
3900 backtrack point. */
3901 struct delay_pair *delay_entry;
3902 delay_entry
3903 = (struct delay_pair *)htab_find_with_hash (delay_htab, insn,
3904 htab_hash_pointer (insn));
3905 if (delay_entry)
3907 save_backtrack_point (delay_entry, ls);
3908 if (sched_verbose >= 2)
3909 fprintf (sched_dump, ";;\t\tsaving backtrack point\n");
3913 /* DECISION is made. */
3915 if (TODO_SPEC (insn) & SPECULATIVE)
3916 generate_recovery_code (insn);
3918 if (targetm.sched.dispatch (NULL_RTX, IS_DISPATCH_ON))
3919 targetm.sched.dispatch_do (insn, ADD_TO_DISPATCH_WINDOW);
3921 /* Update counters, etc in the scheduler's front end. */
3922 (*current_sched_info->begin_schedule_ready) (insn);
3923 VEC_safe_push (rtx, heap, scheduled_insns, insn);
3924 gcc_assert (NONDEBUG_INSN_P (insn));
3925 last_nondebug_scheduled_insn = last_scheduled_insn = insn;
3927 if (recog_memoized (insn) >= 0)
3929 memcpy (temp_state, curr_state, dfa_state_size);
3930 cost = state_transition (curr_state, insn);
3931 if (!sched_pressure_p)
3932 gcc_assert (cost < 0);
3933 if (memcmp (temp_state, curr_state, dfa_state_size) != 0)
3934 cycle_issued_insns++;
3935 asm_p = false;
3937 else
3938 asm_p = (GET_CODE (PATTERN (insn)) == ASM_INPUT
3939 || asm_noperands (PATTERN (insn)) >= 0);
3941 if (targetm.sched.variable_issue)
3942 ls.can_issue_more =
3943 targetm.sched.variable_issue (sched_dump, sched_verbose,
3944 insn, ls.can_issue_more);
3945 /* A naked CLOBBER or USE generates no instruction, so do
3946 not count them against the issue rate. */
3947 else if (GET_CODE (PATTERN (insn)) != USE
3948 && GET_CODE (PATTERN (insn)) != CLOBBER)
3949 ls.can_issue_more--;
3950 advance = schedule_insn (insn);
3952 if (SHADOW_P (insn))
3953 ls.shadows_only_p = true;
3955 /* After issuing an asm insn we should start a new cycle. */
3956 if (advance == 0 && asm_p)
3957 advance = 1;
3959 if (must_backtrack)
3960 break;
3962 if (advance != 0)
3963 break;
3965 ls.first_cycle_insn_p = false;
3966 if (ready.n_ready > 0)
3967 prune_ready_list (temp_state, false, ls.shadows_only_p);
3970 do_backtrack:
3971 if (!must_backtrack)
3972 for (i = 0; i < ready.n_ready; i++)
3974 rtx insn = ready_element (&ready, i);
3975 if (INSN_EXACT_TICK (insn) == clock_var)
3977 must_backtrack = true;
3978 clock_var++;
3979 break;
3982 while (must_backtrack)
3984 struct haifa_saved_data *failed;
3985 rtx failed_insn;
3987 must_backtrack = false;
3988 failed = verify_shadows ();
3989 gcc_assert (failed);
3991 failed_insn = failed->delay_pair->i1;
3992 unschedule_insns_until (failed_insn);
3993 while (failed != backtrack_queue)
3994 free_topmost_backtrack_point (true);
3995 restore_last_backtrack_point (&ls);
3996 if (sched_verbose >= 2)
3997 fprintf (sched_dump, ";;\t\trewind to cycle %d\n", clock_var);
3998 /* Delay by at least a cycle. This could cause additional
3999 backtracking. */
4000 queue_insn (failed_insn, 1, "backtracked");
4001 advance = 0;
4002 if (must_backtrack)
4003 continue;
4004 if (ready.n_ready > 0)
4005 goto resume_after_backtrack;
4006 else
4008 if (clock_var == 0 && ls.first_cycle_insn_p)
4009 goto end_schedule;
4010 advance = 1;
4011 break;
4015 end_schedule:
4016 /* Debug info. */
4017 if (sched_verbose)
4019 fprintf (sched_dump, ";;\tReady list (final): ");
4020 debug_ready_list (&ready);
4023 if (current_sched_info->queue_must_finish_empty)
4024 /* Sanity check -- queue must be empty now. Meaningless if region has
4025 multiple bbs. */
4026 gcc_assert (!q_size && !ready.n_ready && !ready.n_debug);
4027 else
4029 /* We must maintain QUEUE_INDEX between blocks in region. */
4030 for (i = ready.n_ready - 1; i >= 0; i--)
4032 rtx x;
4034 x = ready_element (&ready, i);
4035 QUEUE_INDEX (x) = QUEUE_NOWHERE;
4036 TODO_SPEC (x) = HARD_DEP;
4039 if (q_size)
4040 for (i = 0; i <= max_insn_queue_index; i++)
4042 rtx link;
4043 for (link = insn_queue[i]; link; link = XEXP (link, 1))
4045 rtx x;
4047 x = XEXP (link, 0);
4048 QUEUE_INDEX (x) = QUEUE_NOWHERE;
4049 TODO_SPEC (x) = HARD_DEP;
4051 free_INSN_LIST_list (&insn_queue[i]);
4055 commit_schedule (prev_head, tail, target_bb);
4056 if (sched_verbose)
4057 fprintf (sched_dump, ";; total time = %d\n", clock_var);
4059 if (!current_sched_info->queue_must_finish_empty
4060 || haifa_recovery_bb_recently_added_p)
4062 /* INSN_TICK (minimum clock tick at which the insn becomes
4063 ready) may be not correct for the insn in the subsequent
4064 blocks of the region. We should use a correct value of
4065 `clock_var' or modify INSN_TICK. It is better to keep
4066 clock_var value equal to 0 at the start of a basic block.
4067 Therefore we modify INSN_TICK here. */
4068 fix_inter_tick (NEXT_INSN (prev_head), last_scheduled_insn);
4071 if (targetm.sched.finish)
4073 targetm.sched.finish (sched_dump, sched_verbose);
4074 /* Target might have added some instructions to the scheduled block
4075 in its md_finish () hook. These new insns don't have any data
4076 initialized and to identify them we extend h_i_d so that they'll
4077 get zero luids. */
4078 sched_extend_luids ();
4081 if (sched_verbose)
4082 fprintf (sched_dump, ";; new head = %d\n;; new tail = %d\n\n",
4083 INSN_UID (head), INSN_UID (tail));
4085 /* Update head/tail boundaries. */
4086 head = NEXT_INSN (prev_head);
4087 tail = last_scheduled_insn;
4089 head = restore_other_notes (head, NULL);
4091 current_sched_info->head = head;
4092 current_sched_info->tail = tail;
4094 free_backtrack_queue ();
4097 /* Set_priorities: compute priority of each insn in the block. */
4100 set_priorities (rtx head, rtx tail)
4102 rtx insn;
4103 int n_insn;
4104 int sched_max_insns_priority =
4105 current_sched_info->sched_max_insns_priority;
4106 rtx prev_head;
4108 if (head == tail && ! INSN_P (head))
4109 gcc_unreachable ();
4111 n_insn = 0;
4113 prev_head = PREV_INSN (head);
4114 for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
4116 if (!INSN_P (insn))
4117 continue;
4119 n_insn++;
4120 (void) priority (insn);
4122 gcc_assert (INSN_PRIORITY_KNOWN (insn));
4124 sched_max_insns_priority = MAX (sched_max_insns_priority,
4125 INSN_PRIORITY (insn));
4128 current_sched_info->sched_max_insns_priority = sched_max_insns_priority;
4130 return n_insn;
4133 /* Set dump and sched_verbose for the desired debugging output. If no
4134 dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
4135 For -fsched-verbose=N, N>=10, print everything to stderr. */
4136 void
4137 setup_sched_dump (void)
4139 sched_verbose = sched_verbose_param;
4140 if (sched_verbose_param == 0 && dump_file)
4141 sched_verbose = 1;
4142 sched_dump = ((sched_verbose_param >= 10 || !dump_file)
4143 ? stderr : dump_file);
4146 /* Initialize some global state for the scheduler. This function works
4147 with the common data shared between all the schedulers. It is called
4148 from the scheduler specific initialization routine. */
4150 void
4151 sched_init (void)
4153 /* Disable speculative loads in their presence if cc0 defined. */
4154 #ifdef HAVE_cc0
4155 flag_schedule_speculative_load = 0;
4156 #endif
4158 if (targetm.sched.dispatch (NULL_RTX, IS_DISPATCH_ON))
4159 targetm.sched.dispatch_do (NULL_RTX, DISPATCH_INIT);
4161 sched_pressure_p = (flag_sched_pressure && ! reload_completed
4162 && common_sched_info->sched_pass_id == SCHED_RGN_PASS);
4164 if (sched_pressure_p)
4165 ira_setup_eliminable_regset ();
4167 /* Initialize SPEC_INFO. */
4168 if (targetm.sched.set_sched_flags)
4170 spec_info = &spec_info_var;
4171 targetm.sched.set_sched_flags (spec_info);
4173 if (spec_info->mask != 0)
4175 spec_info->data_weakness_cutoff =
4176 (PARAM_VALUE (PARAM_SCHED_SPEC_PROB_CUTOFF) * MAX_DEP_WEAK) / 100;
4177 spec_info->control_weakness_cutoff =
4178 (PARAM_VALUE (PARAM_SCHED_SPEC_PROB_CUTOFF)
4179 * REG_BR_PROB_BASE) / 100;
4181 else
4182 /* So we won't read anything accidentally. */
4183 spec_info = NULL;
4186 else
4187 /* So we won't read anything accidentally. */
4188 spec_info = 0;
4190 /* Initialize issue_rate. */
4191 if (targetm.sched.issue_rate)
4192 issue_rate = targetm.sched.issue_rate ();
4193 else
4194 issue_rate = 1;
4196 if (cached_issue_rate != issue_rate)
4198 cached_issue_rate = issue_rate;
4199 /* To invalidate max_lookahead_tries: */
4200 cached_first_cycle_multipass_dfa_lookahead = 0;
4203 if (targetm.sched.first_cycle_multipass_dfa_lookahead)
4204 dfa_lookahead = targetm.sched.first_cycle_multipass_dfa_lookahead ();
4205 else
4206 dfa_lookahead = 0;
4208 if (targetm.sched.init_dfa_pre_cycle_insn)
4209 targetm.sched.init_dfa_pre_cycle_insn ();
4211 if (targetm.sched.init_dfa_post_cycle_insn)
4212 targetm.sched.init_dfa_post_cycle_insn ();
4214 dfa_start ();
4215 dfa_state_size = state_size ();
4217 init_alias_analysis ();
4219 if (!sched_no_dce)
4220 df_set_flags (DF_LR_RUN_DCE);
4221 df_note_add_problem ();
4223 /* More problems needed for interloop dep calculation in SMS. */
4224 if (common_sched_info->sched_pass_id == SCHED_SMS_PASS)
4226 df_rd_add_problem ();
4227 df_chain_add_problem (DF_DU_CHAIN + DF_UD_CHAIN);
4230 df_analyze ();
4232 /* Do not run DCE after reload, as this can kill nops inserted
4233 by bundling. */
4234 if (reload_completed)
4235 df_clear_flags (DF_LR_RUN_DCE);
4237 regstat_compute_calls_crossed ();
4239 if (targetm.sched.init_global)
4240 targetm.sched.init_global (sched_dump, sched_verbose, get_max_uid () + 1);
4242 if (sched_pressure_p)
4244 int i, max_regno = max_reg_num ();
4246 ira_set_pseudo_classes (sched_verbose ? sched_dump : NULL);
4247 sched_regno_pressure_class
4248 = (enum reg_class *) xmalloc (max_regno * sizeof (enum reg_class));
4249 for (i = 0; i < max_regno; i++)
4250 sched_regno_pressure_class[i]
4251 = (i < FIRST_PSEUDO_REGISTER
4252 ? ira_pressure_class_translate[REGNO_REG_CLASS (i)]
4253 : ira_pressure_class_translate[reg_allocno_class (i)]);
4254 curr_reg_live = BITMAP_ALLOC (NULL);
4255 saved_reg_live = BITMAP_ALLOC (NULL);
4256 region_ref_regs = BITMAP_ALLOC (NULL);
4259 curr_state = xmalloc (dfa_state_size);
4262 static void haifa_init_only_bb (basic_block, basic_block);
4264 /* Initialize data structures specific to the Haifa scheduler. */
4265 void
4266 haifa_sched_init (void)
4268 setup_sched_dump ();
4269 sched_init ();
4271 scheduled_insns = VEC_alloc (rtx, heap, 0);
4273 if (spec_info != NULL)
4275 sched_deps_info->use_deps_list = 1;
4276 sched_deps_info->generate_spec_deps = 1;
4279 /* Initialize luids, dependency caches, target and h_i_d for the
4280 whole function. */
4282 bb_vec_t bbs = VEC_alloc (basic_block, heap, n_basic_blocks);
4283 basic_block bb;
4285 sched_init_bbs ();
4287 FOR_EACH_BB (bb)
4288 VEC_quick_push (basic_block, bbs, bb);
4289 sched_init_luids (bbs);
4290 sched_deps_init (true);
4291 sched_extend_target ();
4292 haifa_init_h_i_d (bbs);
4294 VEC_free (basic_block, heap, bbs);
4297 sched_init_only_bb = haifa_init_only_bb;
4298 sched_split_block = sched_split_block_1;
4299 sched_create_empty_bb = sched_create_empty_bb_1;
4300 haifa_recovery_bb_ever_added_p = false;
4302 nr_begin_data = nr_begin_control = nr_be_in_data = nr_be_in_control = 0;
4303 before_recovery = 0;
4304 after_recovery = 0;
4307 /* Finish work with the data specific to the Haifa scheduler. */
4308 void
4309 haifa_sched_finish (void)
4311 sched_create_empty_bb = NULL;
4312 sched_split_block = NULL;
4313 sched_init_only_bb = NULL;
4315 if (spec_info && spec_info->dump)
4317 char c = reload_completed ? 'a' : 'b';
4319 fprintf (spec_info->dump,
4320 ";; %s:\n", current_function_name ());
4322 fprintf (spec_info->dump,
4323 ";; Procedure %cr-begin-data-spec motions == %d\n",
4324 c, nr_begin_data);
4325 fprintf (spec_info->dump,
4326 ";; Procedure %cr-be-in-data-spec motions == %d\n",
4327 c, nr_be_in_data);
4328 fprintf (spec_info->dump,
4329 ";; Procedure %cr-begin-control-spec motions == %d\n",
4330 c, nr_begin_control);
4331 fprintf (spec_info->dump,
4332 ";; Procedure %cr-be-in-control-spec motions == %d\n",
4333 c, nr_be_in_control);
4336 VEC_free (rtx, heap, scheduled_insns);
4338 /* Finalize h_i_d, dependency caches, and luids for the whole
4339 function. Target will be finalized in md_global_finish (). */
4340 sched_deps_finish ();
4341 sched_finish_luids ();
4342 current_sched_info = NULL;
4343 sched_finish ();
4346 /* Free global data used during insn scheduling. This function works with
4347 the common data shared between the schedulers. */
4349 void
4350 sched_finish (void)
4352 haifa_finish_h_i_d ();
4353 if (sched_pressure_p)
4355 free (sched_regno_pressure_class);
4356 BITMAP_FREE (region_ref_regs);
4357 BITMAP_FREE (saved_reg_live);
4358 BITMAP_FREE (curr_reg_live);
4360 free (curr_state);
4362 if (targetm.sched.finish_global)
4363 targetm.sched.finish_global (sched_dump, sched_verbose);
4365 end_alias_analysis ();
4367 regstat_free_calls_crossed ();
4369 dfa_finish ();
4372 /* Free all delay_pair structures that were recorded. */
4373 void
4374 free_delay_pairs (void)
4376 if (delay_htab)
4378 htab_empty (delay_htab);
4379 htab_empty (delay_htab_i2);
4383 /* Fix INSN_TICKs of the instructions in the current block as well as
4384 INSN_TICKs of their dependents.
4385 HEAD and TAIL are the begin and the end of the current scheduled block. */
4386 static void
4387 fix_inter_tick (rtx head, rtx tail)
4389 /* Set of instructions with corrected INSN_TICK. */
4390 bitmap_head processed;
4391 /* ??? It is doubtful if we should assume that cycle advance happens on
4392 basic block boundaries. Basically insns that are unconditionally ready
4393 on the start of the block are more preferable then those which have
4394 a one cycle dependency over insn from the previous block. */
4395 int next_clock = clock_var + 1;
4397 bitmap_initialize (&processed, 0);
4399 /* Iterates over scheduled instructions and fix their INSN_TICKs and
4400 INSN_TICKs of dependent instructions, so that INSN_TICKs are consistent
4401 across different blocks. */
4402 for (tail = NEXT_INSN (tail); head != tail; head = NEXT_INSN (head))
4404 if (INSN_P (head))
4406 int tick;
4407 sd_iterator_def sd_it;
4408 dep_t dep;
4410 tick = INSN_TICK (head);
4411 gcc_assert (tick >= MIN_TICK);
4413 /* Fix INSN_TICK of instruction from just scheduled block. */
4414 if (bitmap_set_bit (&processed, INSN_LUID (head)))
4416 tick -= next_clock;
4418 if (tick < MIN_TICK)
4419 tick = MIN_TICK;
4421 INSN_TICK (head) = tick;
4424 FOR_EACH_DEP (head, SD_LIST_RES_FORW, sd_it, dep)
4426 rtx next;
4428 next = DEP_CON (dep);
4429 tick = INSN_TICK (next);
4431 if (tick != INVALID_TICK
4432 /* If NEXT has its INSN_TICK calculated, fix it.
4433 If not - it will be properly calculated from
4434 scratch later in fix_tick_ready. */
4435 && bitmap_set_bit (&processed, INSN_LUID (next)))
4437 tick -= next_clock;
4439 if (tick < MIN_TICK)
4440 tick = MIN_TICK;
4442 if (tick > INTER_TICK (next))
4443 INTER_TICK (next) = tick;
4444 else
4445 tick = INTER_TICK (next);
4447 INSN_TICK (next) = tick;
4452 bitmap_clear (&processed);
4455 static int haifa_speculate_insn (rtx, ds_t, rtx *);
4457 /* Check if NEXT is ready to be added to the ready or queue list.
4458 If "yes", add it to the proper list.
4459 Returns:
4460 -1 - is not ready yet,
4461 0 - added to the ready list,
4462 0 < N - queued for N cycles. */
4464 try_ready (rtx next)
4466 ds_t old_ts, new_ts;
4468 old_ts = TODO_SPEC (next);
4470 gcc_assert (!(old_ts & ~(SPECULATIVE | HARD_DEP))
4471 && ((old_ts & HARD_DEP)
4472 || (old_ts & SPECULATIVE)));
4474 if (sd_lists_empty_p (next, SD_LIST_BACK))
4475 /* NEXT has all its dependencies resolved. */
4476 new_ts = 0;
4477 else
4479 /* One of the NEXT's dependencies has been resolved.
4480 Recalculate NEXT's status. */
4482 if (!sd_lists_empty_p (next, SD_LIST_HARD_BACK))
4483 new_ts = HARD_DEP;
4484 else
4485 /* Now we've got NEXT with speculative deps only.
4486 1. Look at the deps to see what we have to do.
4487 2. Check if we can do 'todo'. */
4489 sd_iterator_def sd_it;
4490 dep_t dep;
4491 bool first_p = true;
4493 new_ts = 0;
4495 FOR_EACH_DEP (next, SD_LIST_BACK, sd_it, dep)
4497 ds_t ds = DEP_STATUS (dep) & SPECULATIVE;
4499 if (DEBUG_INSN_P (DEP_PRO (dep))
4500 && !DEBUG_INSN_P (next))
4501 continue;
4503 if (first_p)
4505 first_p = false;
4507 new_ts = ds;
4509 else
4510 new_ts = ds_merge (new_ts, ds);
4513 if (ds_weak (new_ts) < spec_info->data_weakness_cutoff)
4514 /* Too few points. */
4515 new_ts = HARD_DEP;
4519 if (new_ts & HARD_DEP)
4520 gcc_assert (new_ts == HARD_DEP && new_ts == old_ts
4521 && QUEUE_INDEX (next) == QUEUE_NOWHERE);
4522 else if (current_sched_info->new_ready)
4523 new_ts = current_sched_info->new_ready (next, new_ts);
4525 /* * if !(old_ts & SPECULATIVE) (e.g. HARD_DEP or 0), then insn might
4526 have its original pattern or changed (speculative) one. This is due
4527 to changing ebb in region scheduling.
4528 * But if (old_ts & SPECULATIVE), then we are pretty sure that insn
4529 has speculative pattern.
4531 We can't assert (!(new_ts & HARD_DEP) || new_ts == old_ts) here because
4532 control-speculative NEXT could have been discarded by sched-rgn.c
4533 (the same case as when discarded by can_schedule_ready_p ()). */
4535 if ((new_ts & SPECULATIVE)
4536 /* If (old_ts == new_ts), then (old_ts & SPECULATIVE) and we don't
4537 need to change anything. */
4538 && new_ts != old_ts)
4540 int res;
4541 rtx new_pat;
4543 gcc_assert (!(new_ts & ~SPECULATIVE));
4545 res = haifa_speculate_insn (next, new_ts, &new_pat);
4547 switch (res)
4549 case -1:
4550 /* It would be nice to change DEP_STATUS of all dependences,
4551 which have ((DEP_STATUS & SPECULATIVE) == new_ts) to HARD_DEP,
4552 so we won't reanalyze anything. */
4553 new_ts = HARD_DEP;
4554 break;
4556 case 0:
4557 /* We follow the rule, that every speculative insn
4558 has non-null ORIG_PAT. */
4559 if (!ORIG_PAT (next))
4560 ORIG_PAT (next) = PATTERN (next);
4561 break;
4563 case 1:
4564 if (!ORIG_PAT (next))
4565 /* If we gonna to overwrite the original pattern of insn,
4566 save it. */
4567 ORIG_PAT (next) = PATTERN (next);
4569 haifa_change_pattern (next, new_pat);
4570 break;
4572 default:
4573 gcc_unreachable ();
4577 /* We need to restore pattern only if (new_ts == 0), because otherwise it is
4578 either correct (new_ts & SPECULATIVE),
4579 or we simply don't care (new_ts & HARD_DEP). */
4581 gcc_assert (!ORIG_PAT (next)
4582 || !IS_SPECULATION_BRANCHY_CHECK_P (next));
4584 TODO_SPEC (next) = new_ts;
4586 if (new_ts & HARD_DEP)
4588 /* We can't assert (QUEUE_INDEX (next) == QUEUE_NOWHERE) here because
4589 control-speculative NEXT could have been discarded by sched-rgn.c
4590 (the same case as when discarded by can_schedule_ready_p ()). */
4591 /*gcc_assert (QUEUE_INDEX (next) == QUEUE_NOWHERE);*/
4593 change_queue_index (next, QUEUE_NOWHERE);
4594 return -1;
4596 else if (!(new_ts & BEGIN_SPEC)
4597 && ORIG_PAT (next) && !IS_SPECULATION_CHECK_P (next))
4598 /* We should change pattern of every previously speculative
4599 instruction - and we determine if NEXT was speculative by using
4600 ORIG_PAT field. Except one case - speculation checks have ORIG_PAT
4601 pat too, so skip them. */
4603 haifa_change_pattern (next, ORIG_PAT (next));
4604 ORIG_PAT (next) = 0;
4607 if (sched_verbose >= 2)
4609 fprintf (sched_dump, ";;\t\tdependencies resolved: insn %s",
4610 (*current_sched_info->print_insn) (next, 0));
4612 if (spec_info && spec_info->dump)
4614 if (new_ts & BEGIN_DATA)
4615 fprintf (spec_info->dump, "; data-spec;");
4616 if (new_ts & BEGIN_CONTROL)
4617 fprintf (spec_info->dump, "; control-spec;");
4618 if (new_ts & BE_IN_CONTROL)
4619 fprintf (spec_info->dump, "; in-control-spec;");
4622 fprintf (sched_dump, "\n");
4625 adjust_priority (next);
4627 return fix_tick_ready (next);
4630 /* Calculate INSN_TICK of NEXT and add it to either ready or queue list. */
4631 static int
4632 fix_tick_ready (rtx next)
4634 int tick, delay;
4636 if (!DEBUG_INSN_P (next) && !sd_lists_empty_p (next, SD_LIST_RES_BACK))
4638 int full_p;
4639 sd_iterator_def sd_it;
4640 dep_t dep;
4642 tick = INSN_TICK (next);
4643 /* if tick is not equal to INVALID_TICK, then update
4644 INSN_TICK of NEXT with the most recent resolved dependence
4645 cost. Otherwise, recalculate from scratch. */
4646 full_p = (tick == INVALID_TICK);
4648 FOR_EACH_DEP (next, SD_LIST_RES_BACK, sd_it, dep)
4650 rtx pro = DEP_PRO (dep);
4651 int tick1;
4653 gcc_assert (INSN_TICK (pro) >= MIN_TICK);
4655 tick1 = INSN_TICK (pro) + dep_cost (dep);
4656 if (tick1 > tick)
4657 tick = tick1;
4659 if (!full_p)
4660 break;
4663 else
4664 tick = -1;
4666 INSN_TICK (next) = tick;
4668 delay = tick - clock_var;
4669 if (delay <= 0 || sched_pressure_p)
4670 delay = QUEUE_READY;
4672 change_queue_index (next, delay);
4674 return delay;
4677 /* Move NEXT to the proper queue list with (DELAY >= 1),
4678 or add it to the ready list (DELAY == QUEUE_READY),
4679 or remove it from ready and queue lists at all (DELAY == QUEUE_NOWHERE). */
4680 static void
4681 change_queue_index (rtx next, int delay)
4683 int i = QUEUE_INDEX (next);
4685 gcc_assert (QUEUE_NOWHERE <= delay && delay <= max_insn_queue_index
4686 && delay != 0);
4687 gcc_assert (i != QUEUE_SCHEDULED);
4689 if ((delay > 0 && NEXT_Q_AFTER (q_ptr, delay) == i)
4690 || (delay < 0 && delay == i))
4691 /* We have nothing to do. */
4692 return;
4694 /* Remove NEXT from wherever it is now. */
4695 if (i == QUEUE_READY)
4696 ready_remove_insn (next);
4697 else if (i >= 0)
4698 queue_remove (next);
4700 /* Add it to the proper place. */
4701 if (delay == QUEUE_READY)
4702 ready_add (readyp, next, false);
4703 else if (delay >= 1)
4704 queue_insn (next, delay, "change queue index");
4706 if (sched_verbose >= 2)
4708 fprintf (sched_dump, ";;\t\ttick updated: insn %s",
4709 (*current_sched_info->print_insn) (next, 0));
4711 if (delay == QUEUE_READY)
4712 fprintf (sched_dump, " into ready\n");
4713 else if (delay >= 1)
4714 fprintf (sched_dump, " into queue with cost=%d\n", delay);
4715 else
4716 fprintf (sched_dump, " removed from ready or queue lists\n");
4720 static int sched_ready_n_insns = -1;
4722 /* Initialize per region data structures. */
4723 void
4724 sched_extend_ready_list (int new_sched_ready_n_insns)
4726 int i;
4728 if (sched_ready_n_insns == -1)
4729 /* At the first call we need to initialize one more choice_stack
4730 entry. */
4732 i = 0;
4733 sched_ready_n_insns = 0;
4734 VEC_reserve (rtx, heap, scheduled_insns, new_sched_ready_n_insns);
4736 else
4737 i = sched_ready_n_insns + 1;
4739 ready.veclen = new_sched_ready_n_insns + issue_rate;
4740 ready.vec = XRESIZEVEC (rtx, ready.vec, ready.veclen);
4742 gcc_assert (new_sched_ready_n_insns >= sched_ready_n_insns);
4744 ready_try = (char *) xrecalloc (ready_try, new_sched_ready_n_insns,
4745 sched_ready_n_insns, sizeof (*ready_try));
4747 /* We allocate +1 element to save initial state in the choice_stack[0]
4748 entry. */
4749 choice_stack = XRESIZEVEC (struct choice_entry, choice_stack,
4750 new_sched_ready_n_insns + 1);
4752 for (; i <= new_sched_ready_n_insns; i++)
4754 choice_stack[i].state = xmalloc (dfa_state_size);
4756 if (targetm.sched.first_cycle_multipass_init)
4757 targetm.sched.first_cycle_multipass_init (&(choice_stack[i]
4758 .target_data));
4761 sched_ready_n_insns = new_sched_ready_n_insns;
4764 /* Free per region data structures. */
4765 void
4766 sched_finish_ready_list (void)
4768 int i;
4770 free (ready.vec);
4771 ready.vec = NULL;
4772 ready.veclen = 0;
4774 free (ready_try);
4775 ready_try = NULL;
4777 for (i = 0; i <= sched_ready_n_insns; i++)
4779 if (targetm.sched.first_cycle_multipass_fini)
4780 targetm.sched.first_cycle_multipass_fini (&(choice_stack[i]
4781 .target_data));
4783 free (choice_stack [i].state);
4785 free (choice_stack);
4786 choice_stack = NULL;
4788 sched_ready_n_insns = -1;
4791 static int
4792 haifa_luid_for_non_insn (rtx x)
4794 gcc_assert (NOTE_P (x) || LABEL_P (x));
4796 return 0;
4799 /* Generates recovery code for INSN. */
4800 static void
4801 generate_recovery_code (rtx insn)
4803 if (TODO_SPEC (insn) & BEGIN_SPEC)
4804 begin_speculative_block (insn);
4806 /* Here we have insn with no dependencies to
4807 instructions other then CHECK_SPEC ones. */
4809 if (TODO_SPEC (insn) & BE_IN_SPEC)
4810 add_to_speculative_block (insn);
4813 /* Helper function.
4814 Tries to add speculative dependencies of type FS between instructions
4815 in deps_list L and TWIN. */
4816 static void
4817 process_insn_forw_deps_be_in_spec (rtx insn, rtx twin, ds_t fs)
4819 sd_iterator_def sd_it;
4820 dep_t dep;
4822 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
4824 ds_t ds;
4825 rtx consumer;
4827 consumer = DEP_CON (dep);
4829 ds = DEP_STATUS (dep);
4831 if (/* If we want to create speculative dep. */
4833 /* And we can do that because this is a true dep. */
4834 && (ds & DEP_TYPES) == DEP_TRUE)
4836 gcc_assert (!(ds & BE_IN_SPEC));
4838 if (/* If this dep can be overcome with 'begin speculation'. */
4839 ds & BEGIN_SPEC)
4840 /* Then we have a choice: keep the dep 'begin speculative'
4841 or transform it into 'be in speculative'. */
4843 if (/* In try_ready we assert that if insn once became ready
4844 it can be removed from the ready (or queue) list only
4845 due to backend decision. Hence we can't let the
4846 probability of the speculative dep to decrease. */
4847 ds_weak (ds) <= ds_weak (fs))
4849 ds_t new_ds;
4851 new_ds = (ds & ~BEGIN_SPEC) | fs;
4853 if (/* consumer can 'be in speculative'. */
4854 sched_insn_is_legitimate_for_speculation_p (consumer,
4855 new_ds))
4856 /* Transform it to be in speculative. */
4857 ds = new_ds;
4860 else
4861 /* Mark the dep as 'be in speculative'. */
4862 ds |= fs;
4866 dep_def _new_dep, *new_dep = &_new_dep;
4868 init_dep_1 (new_dep, twin, consumer, DEP_TYPE (dep), ds);
4869 sd_add_dep (new_dep, false);
4874 /* Generates recovery code for BEGIN speculative INSN. */
4875 static void
4876 begin_speculative_block (rtx insn)
4878 if (TODO_SPEC (insn) & BEGIN_DATA)
4879 nr_begin_data++;
4880 if (TODO_SPEC (insn) & BEGIN_CONTROL)
4881 nr_begin_control++;
4883 create_check_block_twin (insn, false);
4885 TODO_SPEC (insn) &= ~BEGIN_SPEC;
4888 static void haifa_init_insn (rtx);
4890 /* Generates recovery code for BE_IN speculative INSN. */
4891 static void
4892 add_to_speculative_block (rtx insn)
4894 ds_t ts;
4895 sd_iterator_def sd_it;
4896 dep_t dep;
4897 rtx twins = NULL;
4898 rtx_vec_t priorities_roots;
4900 ts = TODO_SPEC (insn);
4901 gcc_assert (!(ts & ~BE_IN_SPEC));
4903 if (ts & BE_IN_DATA)
4904 nr_be_in_data++;
4905 if (ts & BE_IN_CONTROL)
4906 nr_be_in_control++;
4908 TODO_SPEC (insn) &= ~BE_IN_SPEC;
4909 gcc_assert (!TODO_SPEC (insn));
4911 DONE_SPEC (insn) |= ts;
4913 /* First we convert all simple checks to branchy. */
4914 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4915 sd_iterator_cond (&sd_it, &dep);)
4917 rtx check = DEP_PRO (dep);
4919 if (IS_SPECULATION_SIMPLE_CHECK_P (check))
4921 create_check_block_twin (check, true);
4923 /* Restart search. */
4924 sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4926 else
4927 /* Continue search. */
4928 sd_iterator_next (&sd_it);
4931 priorities_roots = NULL;
4932 clear_priorities (insn, &priorities_roots);
4934 while (1)
4936 rtx check, twin;
4937 basic_block rec;
4939 /* Get the first backward dependency of INSN. */
4940 sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4941 if (!sd_iterator_cond (&sd_it, &dep))
4942 /* INSN has no backward dependencies left. */
4943 break;
4945 gcc_assert ((DEP_STATUS (dep) & BEGIN_SPEC) == 0
4946 && (DEP_STATUS (dep) & BE_IN_SPEC) != 0
4947 && (DEP_STATUS (dep) & DEP_TYPES) == DEP_TRUE);
4949 check = DEP_PRO (dep);
4951 gcc_assert (!IS_SPECULATION_CHECK_P (check) && !ORIG_PAT (check)
4952 && QUEUE_INDEX (check) == QUEUE_NOWHERE);
4954 rec = BLOCK_FOR_INSN (check);
4956 twin = emit_insn_before (copy_insn (PATTERN (insn)), BB_END (rec));
4957 haifa_init_insn (twin);
4959 sd_copy_back_deps (twin, insn, true);
4961 if (sched_verbose && spec_info->dump)
4962 /* INSN_BB (insn) isn't determined for twin insns yet.
4963 So we can't use current_sched_info->print_insn. */
4964 fprintf (spec_info->dump, ";;\t\tGenerated twin insn : %d/rec%d\n",
4965 INSN_UID (twin), rec->index);
4967 twins = alloc_INSN_LIST (twin, twins);
4969 /* Add dependences between TWIN and all appropriate
4970 instructions from REC. */
4971 FOR_EACH_DEP (insn, SD_LIST_SPEC_BACK, sd_it, dep)
4973 rtx pro = DEP_PRO (dep);
4975 gcc_assert (DEP_TYPE (dep) == REG_DEP_TRUE);
4977 /* INSN might have dependencies from the instructions from
4978 several recovery blocks. At this iteration we process those
4979 producers that reside in REC. */
4980 if (BLOCK_FOR_INSN (pro) == rec)
4982 dep_def _new_dep, *new_dep = &_new_dep;
4984 init_dep (new_dep, pro, twin, REG_DEP_TRUE);
4985 sd_add_dep (new_dep, false);
4989 process_insn_forw_deps_be_in_spec (insn, twin, ts);
4991 /* Remove all dependencies between INSN and insns in REC. */
4992 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4993 sd_iterator_cond (&sd_it, &dep);)
4995 rtx pro = DEP_PRO (dep);
4997 if (BLOCK_FOR_INSN (pro) == rec)
4998 sd_delete_dep (sd_it);
4999 else
5000 sd_iterator_next (&sd_it);
5004 /* We couldn't have added the dependencies between INSN and TWINS earlier
5005 because that would make TWINS appear in the INSN_BACK_DEPS (INSN). */
5006 while (twins)
5008 rtx twin;
5010 twin = XEXP (twins, 0);
5013 dep_def _new_dep, *new_dep = &_new_dep;
5015 init_dep (new_dep, insn, twin, REG_DEP_OUTPUT);
5016 sd_add_dep (new_dep, false);
5019 twin = XEXP (twins, 1);
5020 free_INSN_LIST_node (twins);
5021 twins = twin;
5024 calc_priorities (priorities_roots);
5025 VEC_free (rtx, heap, priorities_roots);
5028 /* Extends and fills with zeros (only the new part) array pointed to by P. */
5029 void *
5030 xrecalloc (void *p, size_t new_nmemb, size_t old_nmemb, size_t size)
5032 gcc_assert (new_nmemb >= old_nmemb);
5033 p = XRESIZEVAR (void, p, new_nmemb * size);
5034 memset (((char *) p) + old_nmemb * size, 0, (new_nmemb - old_nmemb) * size);
5035 return p;
5038 /* Helper function.
5039 Find fallthru edge from PRED. */
5040 edge
5041 find_fallthru_edge_from (basic_block pred)
5043 edge e;
5044 basic_block succ;
5046 succ = pred->next_bb;
5047 gcc_assert (succ->prev_bb == pred);
5049 if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
5051 e = find_fallthru_edge (pred->succs);
5053 if (e)
5055 gcc_assert (e->dest == succ);
5056 return e;
5059 else
5061 e = find_fallthru_edge (succ->preds);
5063 if (e)
5065 gcc_assert (e->src == pred);
5066 return e;
5070 return NULL;
5073 /* Extend per basic block data structures. */
5074 static void
5075 sched_extend_bb (void)
5077 rtx insn;
5079 /* The following is done to keep current_sched_info->next_tail non null. */
5080 insn = BB_END (EXIT_BLOCK_PTR->prev_bb);
5081 if (NEXT_INSN (insn) == 0
5082 || (!NOTE_P (insn)
5083 && !LABEL_P (insn)
5084 /* Don't emit a NOTE if it would end up before a BARRIER. */
5085 && !BARRIER_P (NEXT_INSN (insn))))
5087 rtx note = emit_note_after (NOTE_INSN_DELETED, insn);
5088 /* Make insn appear outside BB. */
5089 set_block_for_insn (note, NULL);
5090 BB_END (EXIT_BLOCK_PTR->prev_bb) = insn;
5094 /* Init per basic block data structures. */
5095 void
5096 sched_init_bbs (void)
5098 sched_extend_bb ();
5101 /* Initialize BEFORE_RECOVERY variable. */
5102 static void
5103 init_before_recovery (basic_block *before_recovery_ptr)
5105 basic_block last;
5106 edge e;
5108 last = EXIT_BLOCK_PTR->prev_bb;
5109 e = find_fallthru_edge_from (last);
5111 if (e)
5113 /* We create two basic blocks:
5114 1. Single instruction block is inserted right after E->SRC
5115 and has jump to
5116 2. Empty block right before EXIT_BLOCK.
5117 Between these two blocks recovery blocks will be emitted. */
5119 basic_block single, empty;
5120 rtx x, label;
5122 /* If the fallthrough edge to exit we've found is from the block we've
5123 created before, don't do anything more. */
5124 if (last == after_recovery)
5125 return;
5127 adding_bb_to_current_region_p = false;
5129 single = sched_create_empty_bb (last);
5130 empty = sched_create_empty_bb (single);
5132 /* Add new blocks to the root loop. */
5133 if (current_loops != NULL)
5135 add_bb_to_loop (single, VEC_index (loop_p, current_loops->larray, 0));
5136 add_bb_to_loop (empty, VEC_index (loop_p, current_loops->larray, 0));
5139 single->count = last->count;
5140 empty->count = last->count;
5141 single->frequency = last->frequency;
5142 empty->frequency = last->frequency;
5143 BB_COPY_PARTITION (single, last);
5144 BB_COPY_PARTITION (empty, last);
5146 redirect_edge_succ (e, single);
5147 make_single_succ_edge (single, empty, 0);
5148 make_single_succ_edge (empty, EXIT_BLOCK_PTR,
5149 EDGE_FALLTHRU | EDGE_CAN_FALLTHRU);
5151 label = block_label (empty);
5152 x = emit_jump_insn_after (gen_jump (label), BB_END (single));
5153 JUMP_LABEL (x) = label;
5154 LABEL_NUSES (label)++;
5155 haifa_init_insn (x);
5157 emit_barrier_after (x);
5159 sched_init_only_bb (empty, NULL);
5160 sched_init_only_bb (single, NULL);
5161 sched_extend_bb ();
5163 adding_bb_to_current_region_p = true;
5164 before_recovery = single;
5165 after_recovery = empty;
5167 if (before_recovery_ptr)
5168 *before_recovery_ptr = before_recovery;
5170 if (sched_verbose >= 2 && spec_info->dump)
5171 fprintf (spec_info->dump,
5172 ";;\t\tFixed fallthru to EXIT : %d->>%d->%d->>EXIT\n",
5173 last->index, single->index, empty->index);
5175 else
5176 before_recovery = last;
5179 /* Returns new recovery block. */
5180 basic_block
5181 sched_create_recovery_block (basic_block *before_recovery_ptr)
5183 rtx label;
5184 rtx barrier;
5185 basic_block rec;
5187 haifa_recovery_bb_recently_added_p = true;
5188 haifa_recovery_bb_ever_added_p = true;
5190 init_before_recovery (before_recovery_ptr);
5192 barrier = get_last_bb_insn (before_recovery);
5193 gcc_assert (BARRIER_P (barrier));
5195 label = emit_label_after (gen_label_rtx (), barrier);
5197 rec = create_basic_block (label, label, before_recovery);
5199 /* A recovery block always ends with an unconditional jump. */
5200 emit_barrier_after (BB_END (rec));
5202 if (BB_PARTITION (before_recovery) != BB_UNPARTITIONED)
5203 BB_SET_PARTITION (rec, BB_COLD_PARTITION);
5205 if (sched_verbose && spec_info->dump)
5206 fprintf (spec_info->dump, ";;\t\tGenerated recovery block rec%d\n",
5207 rec->index);
5209 return rec;
5212 /* Create edges: FIRST_BB -> REC; FIRST_BB -> SECOND_BB; REC -> SECOND_BB
5213 and emit necessary jumps. */
5214 void
5215 sched_create_recovery_edges (basic_block first_bb, basic_block rec,
5216 basic_block second_bb)
5218 rtx label;
5219 rtx jump;
5220 int edge_flags;
5222 /* This is fixing of incoming edge. */
5223 /* ??? Which other flags should be specified? */
5224 if (BB_PARTITION (first_bb) != BB_PARTITION (rec))
5225 /* Partition type is the same, if it is "unpartitioned". */
5226 edge_flags = EDGE_CROSSING;
5227 else
5228 edge_flags = 0;
5230 make_edge (first_bb, rec, edge_flags);
5231 label = block_label (second_bb);
5232 jump = emit_jump_insn_after (gen_jump (label), BB_END (rec));
5233 JUMP_LABEL (jump) = label;
5234 LABEL_NUSES (label)++;
5236 if (BB_PARTITION (second_bb) != BB_PARTITION (rec))
5237 /* Partition type is the same, if it is "unpartitioned". */
5239 /* Rewritten from cfgrtl.c. */
5240 if (flag_reorder_blocks_and_partition
5241 && targetm_common.have_named_sections)
5243 /* We don't need the same note for the check because
5244 any_condjump_p (check) == true. */
5245 add_reg_note (jump, REG_CROSSING_JUMP, NULL_RTX);
5247 edge_flags = EDGE_CROSSING;
5249 else
5250 edge_flags = 0;
5252 make_single_succ_edge (rec, second_bb, edge_flags);
5253 if (dom_info_available_p (CDI_DOMINATORS))
5254 set_immediate_dominator (CDI_DOMINATORS, rec, first_bb);
5257 /* This function creates recovery code for INSN. If MUTATE_P is nonzero,
5258 INSN is a simple check, that should be converted to branchy one. */
5259 static void
5260 create_check_block_twin (rtx insn, bool mutate_p)
5262 basic_block rec;
5263 rtx label, check, twin;
5264 ds_t fs;
5265 sd_iterator_def sd_it;
5266 dep_t dep;
5267 dep_def _new_dep, *new_dep = &_new_dep;
5268 ds_t todo_spec;
5270 gcc_assert (ORIG_PAT (insn) != NULL_RTX);
5272 if (!mutate_p)
5273 todo_spec = TODO_SPEC (insn);
5274 else
5276 gcc_assert (IS_SPECULATION_SIMPLE_CHECK_P (insn)
5277 && (TODO_SPEC (insn) & SPECULATIVE) == 0);
5279 todo_spec = CHECK_SPEC (insn);
5282 todo_spec &= SPECULATIVE;
5284 /* Create recovery block. */
5285 if (mutate_p || targetm.sched.needs_block_p (todo_spec))
5287 rec = sched_create_recovery_block (NULL);
5288 label = BB_HEAD (rec);
5290 else
5292 rec = EXIT_BLOCK_PTR;
5293 label = NULL_RTX;
5296 /* Emit CHECK. */
5297 check = targetm.sched.gen_spec_check (insn, label, todo_spec);
5299 if (rec != EXIT_BLOCK_PTR)
5301 /* To have mem_reg alive at the beginning of second_bb,
5302 we emit check BEFORE insn, so insn after splitting
5303 insn will be at the beginning of second_bb, which will
5304 provide us with the correct life information. */
5305 check = emit_jump_insn_before (check, insn);
5306 JUMP_LABEL (check) = label;
5307 LABEL_NUSES (label)++;
5309 else
5310 check = emit_insn_before (check, insn);
5312 /* Extend data structures. */
5313 haifa_init_insn (check);
5315 /* CHECK is being added to current region. Extend ready list. */
5316 gcc_assert (sched_ready_n_insns != -1);
5317 sched_extend_ready_list (sched_ready_n_insns + 1);
5319 if (current_sched_info->add_remove_insn)
5320 current_sched_info->add_remove_insn (insn, 0);
5322 RECOVERY_BLOCK (check) = rec;
5324 if (sched_verbose && spec_info->dump)
5325 fprintf (spec_info->dump, ";;\t\tGenerated check insn : %s\n",
5326 (*current_sched_info->print_insn) (check, 0));
5328 gcc_assert (ORIG_PAT (insn));
5330 /* Initialize TWIN (twin is a duplicate of original instruction
5331 in the recovery block). */
5332 if (rec != EXIT_BLOCK_PTR)
5334 sd_iterator_def sd_it;
5335 dep_t dep;
5337 FOR_EACH_DEP (insn, SD_LIST_RES_BACK, sd_it, dep)
5338 if ((DEP_STATUS (dep) & DEP_OUTPUT) != 0)
5340 struct _dep _dep2, *dep2 = &_dep2;
5342 init_dep (dep2, DEP_PRO (dep), check, REG_DEP_TRUE);
5344 sd_add_dep (dep2, true);
5347 twin = emit_insn_after (ORIG_PAT (insn), BB_END (rec));
5348 haifa_init_insn (twin);
5350 if (sched_verbose && spec_info->dump)
5351 /* INSN_BB (insn) isn't determined for twin insns yet.
5352 So we can't use current_sched_info->print_insn. */
5353 fprintf (spec_info->dump, ";;\t\tGenerated twin insn : %d/rec%d\n",
5354 INSN_UID (twin), rec->index);
5356 else
5358 ORIG_PAT (check) = ORIG_PAT (insn);
5359 HAS_INTERNAL_DEP (check) = 1;
5360 twin = check;
5361 /* ??? We probably should change all OUTPUT dependencies to
5362 (TRUE | OUTPUT). */
5365 /* Copy all resolved back dependencies of INSN to TWIN. This will
5366 provide correct value for INSN_TICK (TWIN). */
5367 sd_copy_back_deps (twin, insn, true);
5369 if (rec != EXIT_BLOCK_PTR)
5370 /* In case of branchy check, fix CFG. */
5372 basic_block first_bb, second_bb;
5373 rtx jump;
5375 first_bb = BLOCK_FOR_INSN (check);
5376 second_bb = sched_split_block (first_bb, check);
5378 sched_create_recovery_edges (first_bb, rec, second_bb);
5380 sched_init_only_bb (second_bb, first_bb);
5381 sched_init_only_bb (rec, EXIT_BLOCK_PTR);
5383 jump = BB_END (rec);
5384 haifa_init_insn (jump);
5387 /* Move backward dependences from INSN to CHECK and
5388 move forward dependences from INSN to TWIN. */
5390 /* First, create dependencies between INSN's producers and CHECK & TWIN. */
5391 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
5393 rtx pro = DEP_PRO (dep);
5394 ds_t ds;
5396 /* If BEGIN_DATA: [insn ~~TRUE~~> producer]:
5397 check --TRUE--> producer ??? or ANTI ???
5398 twin --TRUE--> producer
5399 twin --ANTI--> check
5401 If BEGIN_CONTROL: [insn ~~ANTI~~> producer]:
5402 check --ANTI--> producer
5403 twin --ANTI--> producer
5404 twin --ANTI--> check
5406 If BE_IN_SPEC: [insn ~~TRUE~~> producer]:
5407 check ~~TRUE~~> producer
5408 twin ~~TRUE~~> producer
5409 twin --ANTI--> check */
5411 ds = DEP_STATUS (dep);
5413 if (ds & BEGIN_SPEC)
5415 gcc_assert (!mutate_p);
5416 ds &= ~BEGIN_SPEC;
5419 init_dep_1 (new_dep, pro, check, DEP_TYPE (dep), ds);
5420 sd_add_dep (new_dep, false);
5422 if (rec != EXIT_BLOCK_PTR)
5424 DEP_CON (new_dep) = twin;
5425 sd_add_dep (new_dep, false);
5429 /* Second, remove backward dependencies of INSN. */
5430 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
5431 sd_iterator_cond (&sd_it, &dep);)
5433 if ((DEP_STATUS (dep) & BEGIN_SPEC)
5434 || mutate_p)
5435 /* We can delete this dep because we overcome it with
5436 BEGIN_SPECULATION. */
5437 sd_delete_dep (sd_it);
5438 else
5439 sd_iterator_next (&sd_it);
5442 /* Future Speculations. Determine what BE_IN speculations will be like. */
5443 fs = 0;
5445 /* Fields (DONE_SPEC (x) & BEGIN_SPEC) and CHECK_SPEC (x) are set only
5446 here. */
5448 gcc_assert (!DONE_SPEC (insn));
5450 if (!mutate_p)
5452 ds_t ts = TODO_SPEC (insn);
5454 DONE_SPEC (insn) = ts & BEGIN_SPEC;
5455 CHECK_SPEC (check) = ts & BEGIN_SPEC;
5457 /* Luckiness of future speculations solely depends upon initial
5458 BEGIN speculation. */
5459 if (ts & BEGIN_DATA)
5460 fs = set_dep_weak (fs, BE_IN_DATA, get_dep_weak (ts, BEGIN_DATA));
5461 if (ts & BEGIN_CONTROL)
5462 fs = set_dep_weak (fs, BE_IN_CONTROL,
5463 get_dep_weak (ts, BEGIN_CONTROL));
5465 else
5466 CHECK_SPEC (check) = CHECK_SPEC (insn);
5468 /* Future speculations: call the helper. */
5469 process_insn_forw_deps_be_in_spec (insn, twin, fs);
5471 if (rec != EXIT_BLOCK_PTR)
5473 /* Which types of dependencies should we use here is,
5474 generally, machine-dependent question... But, for now,
5475 it is not. */
5477 if (!mutate_p)
5479 init_dep (new_dep, insn, check, REG_DEP_TRUE);
5480 sd_add_dep (new_dep, false);
5482 init_dep (new_dep, insn, twin, REG_DEP_OUTPUT);
5483 sd_add_dep (new_dep, false);
5485 else
5487 if (spec_info->dump)
5488 fprintf (spec_info->dump, ";;\t\tRemoved simple check : %s\n",
5489 (*current_sched_info->print_insn) (insn, 0));
5491 /* Remove all dependencies of the INSN. */
5493 sd_it = sd_iterator_start (insn, (SD_LIST_FORW
5494 | SD_LIST_BACK
5495 | SD_LIST_RES_BACK));
5496 while (sd_iterator_cond (&sd_it, &dep))
5497 sd_delete_dep (sd_it);
5500 /* If former check (INSN) already was moved to the ready (or queue)
5501 list, add new check (CHECK) there too. */
5502 if (QUEUE_INDEX (insn) != QUEUE_NOWHERE)
5503 try_ready (check);
5505 /* Remove old check from instruction stream and free its
5506 data. */
5507 sched_remove_insn (insn);
5510 init_dep (new_dep, check, twin, REG_DEP_ANTI);
5511 sd_add_dep (new_dep, false);
5513 else
5515 init_dep_1 (new_dep, insn, check, REG_DEP_TRUE, DEP_TRUE | DEP_OUTPUT);
5516 sd_add_dep (new_dep, false);
5519 if (!mutate_p)
5520 /* Fix priorities. If MUTATE_P is nonzero, this is not necessary,
5521 because it'll be done later in add_to_speculative_block. */
5523 rtx_vec_t priorities_roots = NULL;
5525 clear_priorities (twin, &priorities_roots);
5526 calc_priorities (priorities_roots);
5527 VEC_free (rtx, heap, priorities_roots);
5531 /* Removes dependency between instructions in the recovery block REC
5532 and usual region instructions. It keeps inner dependences so it
5533 won't be necessary to recompute them. */
5534 static void
5535 fix_recovery_deps (basic_block rec)
5537 rtx note, insn, jump, ready_list = 0;
5538 bitmap_head in_ready;
5539 rtx link;
5541 bitmap_initialize (&in_ready, 0);
5543 /* NOTE - a basic block note. */
5544 note = NEXT_INSN (BB_HEAD (rec));
5545 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
5546 insn = BB_END (rec);
5547 gcc_assert (JUMP_P (insn));
5548 insn = PREV_INSN (insn);
5552 sd_iterator_def sd_it;
5553 dep_t dep;
5555 for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
5556 sd_iterator_cond (&sd_it, &dep);)
5558 rtx consumer = DEP_CON (dep);
5560 if (BLOCK_FOR_INSN (consumer) != rec)
5562 sd_delete_dep (sd_it);
5564 if (bitmap_set_bit (&in_ready, INSN_LUID (consumer)))
5565 ready_list = alloc_INSN_LIST (consumer, ready_list);
5567 else
5569 gcc_assert ((DEP_STATUS (dep) & DEP_TYPES) == DEP_TRUE);
5571 sd_iterator_next (&sd_it);
5575 insn = PREV_INSN (insn);
5577 while (insn != note);
5579 bitmap_clear (&in_ready);
5581 /* Try to add instructions to the ready or queue list. */
5582 for (link = ready_list; link; link = XEXP (link, 1))
5583 try_ready (XEXP (link, 0));
5584 free_INSN_LIST_list (&ready_list);
5586 /* Fixing jump's dependences. */
5587 insn = BB_HEAD (rec);
5588 jump = BB_END (rec);
5590 gcc_assert (LABEL_P (insn));
5591 insn = NEXT_INSN (insn);
5593 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
5594 add_jump_dependencies (insn, jump);
5597 /* Change pattern of INSN to NEW_PAT. */
5598 void
5599 sched_change_pattern (rtx insn, rtx new_pat)
5601 sd_iterator_def sd_it;
5602 dep_t dep;
5603 int t;
5605 t = validate_change (insn, &PATTERN (insn), new_pat, 0);
5606 gcc_assert (t);
5607 dfa_clear_single_insn_cache (insn);
5609 for (sd_it = sd_iterator_start (insn, (SD_LIST_FORW | SD_LIST_BACK
5610 | SD_LIST_RES_BACK));
5611 sd_iterator_cond (&sd_it, &dep);)
5613 DEP_COST (dep) = UNKNOWN_DEP_COST;
5614 sd_iterator_next (&sd_it);
5618 /* Change pattern of INSN to NEW_PAT. Invalidate cached haifa
5619 instruction data. */
5620 static void
5621 haifa_change_pattern (rtx insn, rtx new_pat)
5623 sched_change_pattern (insn, new_pat);
5625 /* Invalidate INSN_COST, so it'll be recalculated. */
5626 INSN_COST (insn) = -1;
5627 /* Invalidate INSN_TICK, so it'll be recalculated. */
5628 INSN_TICK (insn) = INVALID_TICK;
5631 /* -1 - can't speculate,
5632 0 - for speculation with REQUEST mode it is OK to use
5633 current instruction pattern,
5634 1 - need to change pattern for *NEW_PAT to be speculative. */
5636 sched_speculate_insn (rtx insn, ds_t request, rtx *new_pat)
5638 gcc_assert (current_sched_info->flags & DO_SPECULATION
5639 && (request & SPECULATIVE)
5640 && sched_insn_is_legitimate_for_speculation_p (insn, request));
5642 if ((request & spec_info->mask) != request)
5643 return -1;
5645 if (request & BE_IN_SPEC
5646 && !(request & BEGIN_SPEC))
5647 return 0;
5649 return targetm.sched.speculate_insn (insn, request, new_pat);
5652 static int
5653 haifa_speculate_insn (rtx insn, ds_t request, rtx *new_pat)
5655 gcc_assert (sched_deps_info->generate_spec_deps
5656 && !IS_SPECULATION_CHECK_P (insn));
5658 if (HAS_INTERNAL_DEP (insn)
5659 || SCHED_GROUP_P (insn))
5660 return -1;
5662 return sched_speculate_insn (insn, request, new_pat);
5665 /* Print some information about block BB, which starts with HEAD and
5666 ends with TAIL, before scheduling it.
5667 I is zero, if scheduler is about to start with the fresh ebb. */
5668 static void
5669 dump_new_block_header (int i, basic_block bb, rtx head, rtx tail)
5671 if (!i)
5672 fprintf (sched_dump,
5673 ";; ======================================================\n");
5674 else
5675 fprintf (sched_dump,
5676 ";; =====================ADVANCING TO=====================\n");
5677 fprintf (sched_dump,
5678 ";; -- basic block %d from %d to %d -- %s reload\n",
5679 bb->index, INSN_UID (head), INSN_UID (tail),
5680 (reload_completed ? "after" : "before"));
5681 fprintf (sched_dump,
5682 ";; ======================================================\n");
5683 fprintf (sched_dump, "\n");
5686 /* Unlink basic block notes and labels and saves them, so they
5687 can be easily restored. We unlink basic block notes in EBB to
5688 provide back-compatibility with the previous code, as target backends
5689 assume, that there'll be only instructions between
5690 current_sched_info->{head and tail}. We restore these notes as soon
5691 as we can.
5692 FIRST (LAST) is the first (last) basic block in the ebb.
5693 NB: In usual case (FIRST == LAST) nothing is really done. */
5694 void
5695 unlink_bb_notes (basic_block first, basic_block last)
5697 /* We DON'T unlink basic block notes of the first block in the ebb. */
5698 if (first == last)
5699 return;
5701 bb_header = XNEWVEC (rtx, last_basic_block);
5703 /* Make a sentinel. */
5704 if (last->next_bb != EXIT_BLOCK_PTR)
5705 bb_header[last->next_bb->index] = 0;
5707 first = first->next_bb;
5710 rtx prev, label, note, next;
5712 label = BB_HEAD (last);
5713 if (LABEL_P (label))
5714 note = NEXT_INSN (label);
5715 else
5716 note = label;
5717 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
5719 prev = PREV_INSN (label);
5720 next = NEXT_INSN (note);
5721 gcc_assert (prev && next);
5723 NEXT_INSN (prev) = next;
5724 PREV_INSN (next) = prev;
5726 bb_header[last->index] = label;
5728 if (last == first)
5729 break;
5731 last = last->prev_bb;
5733 while (1);
5736 /* Restore basic block notes.
5737 FIRST is the first basic block in the ebb. */
5738 static void
5739 restore_bb_notes (basic_block first)
5741 if (!bb_header)
5742 return;
5744 /* We DON'T unlink basic block notes of the first block in the ebb. */
5745 first = first->next_bb;
5746 /* Remember: FIRST is actually a second basic block in the ebb. */
5748 while (first != EXIT_BLOCK_PTR
5749 && bb_header[first->index])
5751 rtx prev, label, note, next;
5753 label = bb_header[first->index];
5754 prev = PREV_INSN (label);
5755 next = NEXT_INSN (prev);
5757 if (LABEL_P (label))
5758 note = NEXT_INSN (label);
5759 else
5760 note = label;
5761 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
5763 bb_header[first->index] = 0;
5765 NEXT_INSN (prev) = label;
5766 NEXT_INSN (note) = next;
5767 PREV_INSN (next) = note;
5769 first = first->next_bb;
5772 free (bb_header);
5773 bb_header = 0;
5776 /* Helper function.
5777 Fix CFG after both in- and inter-block movement of
5778 control_flow_insn_p JUMP. */
5779 static void
5780 fix_jump_move (rtx jump)
5782 basic_block bb, jump_bb, jump_bb_next;
5784 bb = BLOCK_FOR_INSN (PREV_INSN (jump));
5785 jump_bb = BLOCK_FOR_INSN (jump);
5786 jump_bb_next = jump_bb->next_bb;
5788 gcc_assert (common_sched_info->sched_pass_id == SCHED_EBB_PASS
5789 || IS_SPECULATION_BRANCHY_CHECK_P (jump));
5791 if (!NOTE_INSN_BASIC_BLOCK_P (BB_END (jump_bb_next)))
5792 /* if jump_bb_next is not empty. */
5793 BB_END (jump_bb) = BB_END (jump_bb_next);
5795 if (BB_END (bb) != PREV_INSN (jump))
5796 /* Then there are instruction after jump that should be placed
5797 to jump_bb_next. */
5798 BB_END (jump_bb_next) = BB_END (bb);
5799 else
5800 /* Otherwise jump_bb_next is empty. */
5801 BB_END (jump_bb_next) = NEXT_INSN (BB_HEAD (jump_bb_next));
5803 /* To make assertion in move_insn happy. */
5804 BB_END (bb) = PREV_INSN (jump);
5806 update_bb_for_insn (jump_bb_next);
5809 /* Fix CFG after interblock movement of control_flow_insn_p JUMP. */
5810 static void
5811 move_block_after_check (rtx jump)
5813 basic_block bb, jump_bb, jump_bb_next;
5814 VEC(edge,gc) *t;
5816 bb = BLOCK_FOR_INSN (PREV_INSN (jump));
5817 jump_bb = BLOCK_FOR_INSN (jump);
5818 jump_bb_next = jump_bb->next_bb;
5820 update_bb_for_insn (jump_bb);
5822 gcc_assert (IS_SPECULATION_CHECK_P (jump)
5823 || IS_SPECULATION_CHECK_P (BB_END (jump_bb_next)));
5825 unlink_block (jump_bb_next);
5826 link_block (jump_bb_next, bb);
5828 t = bb->succs;
5829 bb->succs = 0;
5830 move_succs (&(jump_bb->succs), bb);
5831 move_succs (&(jump_bb_next->succs), jump_bb);
5832 move_succs (&t, jump_bb_next);
5834 df_mark_solutions_dirty ();
5836 common_sched_info->fix_recovery_cfg
5837 (bb->index, jump_bb->index, jump_bb_next->index);
5840 /* Helper function for move_block_after_check.
5841 This functions attaches edge vector pointed to by SUCCSP to
5842 block TO. */
5843 static void
5844 move_succs (VEC(edge,gc) **succsp, basic_block to)
5846 edge e;
5847 edge_iterator ei;
5849 gcc_assert (to->succs == 0);
5851 to->succs = *succsp;
5853 FOR_EACH_EDGE (e, ei, to->succs)
5854 e->src = to;
5856 *succsp = 0;
5859 /* Remove INSN from the instruction stream.
5860 INSN should have any dependencies. */
5861 static void
5862 sched_remove_insn (rtx insn)
5864 sd_finish_insn (insn);
5866 change_queue_index (insn, QUEUE_NOWHERE);
5867 current_sched_info->add_remove_insn (insn, 1);
5868 remove_insn (insn);
5871 /* Clear priorities of all instructions, that are forward dependent on INSN.
5872 Store in vector pointed to by ROOTS_PTR insns on which priority () should
5873 be invoked to initialize all cleared priorities. */
5874 static void
5875 clear_priorities (rtx insn, rtx_vec_t *roots_ptr)
5877 sd_iterator_def sd_it;
5878 dep_t dep;
5879 bool insn_is_root_p = true;
5881 gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
5883 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
5885 rtx pro = DEP_PRO (dep);
5887 if (INSN_PRIORITY_STATUS (pro) >= 0
5888 && QUEUE_INDEX (insn) != QUEUE_SCHEDULED)
5890 /* If DEP doesn't contribute to priority then INSN itself should
5891 be added to priority roots. */
5892 if (contributes_to_priority_p (dep))
5893 insn_is_root_p = false;
5895 INSN_PRIORITY_STATUS (pro) = -1;
5896 clear_priorities (pro, roots_ptr);
5900 if (insn_is_root_p)
5901 VEC_safe_push (rtx, heap, *roots_ptr, insn);
5904 /* Recompute priorities of instructions, whose priorities might have been
5905 changed. ROOTS is a vector of instructions whose priority computation will
5906 trigger initialization of all cleared priorities. */
5907 static void
5908 calc_priorities (rtx_vec_t roots)
5910 int i;
5911 rtx insn;
5913 FOR_EACH_VEC_ELT (rtx, roots, i, insn)
5914 priority (insn);
5918 /* Add dependences between JUMP and other instructions in the recovery
5919 block. INSN is the first insn the recovery block. */
5920 static void
5921 add_jump_dependencies (rtx insn, rtx jump)
5925 insn = NEXT_INSN (insn);
5926 if (insn == jump)
5927 break;
5929 if (dep_list_size (insn) == 0)
5931 dep_def _new_dep, *new_dep = &_new_dep;
5933 init_dep (new_dep, insn, jump, REG_DEP_ANTI);
5934 sd_add_dep (new_dep, false);
5937 while (1);
5939 gcc_assert (!sd_lists_empty_p (jump, SD_LIST_BACK));
5942 /* Return the NOTE_INSN_BASIC_BLOCK of BB. */
5944 bb_note (basic_block bb)
5946 rtx note;
5948 note = BB_HEAD (bb);
5949 if (LABEL_P (note))
5950 note = NEXT_INSN (note);
5952 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
5953 return note;
5956 /* Extend data structures for logical insn UID. */
5957 void
5958 sched_extend_luids (void)
5960 int new_luids_max_uid = get_max_uid () + 1;
5962 VEC_safe_grow_cleared (int, heap, sched_luids, new_luids_max_uid);
5965 /* Initialize LUID for INSN. */
5966 void
5967 sched_init_insn_luid (rtx insn)
5969 int i = INSN_P (insn) ? 1 : common_sched_info->luid_for_non_insn (insn);
5970 int luid;
5972 if (i >= 0)
5974 luid = sched_max_luid;
5975 sched_max_luid += i;
5977 else
5978 luid = -1;
5980 SET_INSN_LUID (insn, luid);
5983 /* Initialize luids for BBS.
5984 The hook common_sched_info->luid_for_non_insn () is used to determine
5985 if notes, labels, etc. need luids. */
5986 void
5987 sched_init_luids (bb_vec_t bbs)
5989 int i;
5990 basic_block bb;
5992 sched_extend_luids ();
5993 FOR_EACH_VEC_ELT (basic_block, bbs, i, bb)
5995 rtx insn;
5997 FOR_BB_INSNS (bb, insn)
5998 sched_init_insn_luid (insn);
6002 /* Free LUIDs. */
6003 void
6004 sched_finish_luids (void)
6006 VEC_free (int, heap, sched_luids);
6007 sched_max_luid = 1;
6010 /* Return logical uid of INSN. Helpful while debugging. */
6012 insn_luid (rtx insn)
6014 return INSN_LUID (insn);
6017 /* Extend per insn data in the target. */
6018 void
6019 sched_extend_target (void)
6021 if (targetm.sched.h_i_d_extended)
6022 targetm.sched.h_i_d_extended ();
6025 /* Extend global scheduler structures (those, that live across calls to
6026 schedule_block) to include information about just emitted INSN. */
6027 static void
6028 extend_h_i_d (void)
6030 int reserve = (get_max_uid () + 1
6031 - VEC_length (haifa_insn_data_def, h_i_d));
6032 if (reserve > 0
6033 && ! VEC_space (haifa_insn_data_def, h_i_d, reserve))
6035 VEC_safe_grow_cleared (haifa_insn_data_def, heap, h_i_d,
6036 3 * get_max_uid () / 2);
6037 sched_extend_target ();
6041 /* Initialize h_i_d entry of the INSN with default values.
6042 Values, that are not explicitly initialized here, hold zero. */
6043 static void
6044 init_h_i_d (rtx insn)
6046 if (INSN_LUID (insn) > 0)
6048 INSN_COST (insn) = -1;
6049 QUEUE_INDEX (insn) = QUEUE_NOWHERE;
6050 INSN_TICK (insn) = INVALID_TICK;
6051 INSN_EXACT_TICK (insn) = INVALID_TICK;
6052 INTER_TICK (insn) = INVALID_TICK;
6053 TODO_SPEC (insn) = HARD_DEP;
6057 /* Initialize haifa_insn_data for BBS. */
6058 void
6059 haifa_init_h_i_d (bb_vec_t bbs)
6061 int i;
6062 basic_block bb;
6064 extend_h_i_d ();
6065 FOR_EACH_VEC_ELT (basic_block, bbs, i, bb)
6067 rtx insn;
6069 FOR_BB_INSNS (bb, insn)
6070 init_h_i_d (insn);
6074 /* Finalize haifa_insn_data. */
6075 void
6076 haifa_finish_h_i_d (void)
6078 int i;
6079 haifa_insn_data_t data;
6080 struct reg_use_data *use, *next;
6082 FOR_EACH_VEC_ELT (haifa_insn_data_def, h_i_d, i, data)
6084 free (data->reg_pressure);
6085 for (use = data->reg_use_list; use != NULL; use = next)
6087 next = use->next_insn_use;
6088 free (use);
6091 VEC_free (haifa_insn_data_def, heap, h_i_d);
6094 /* Init data for the new insn INSN. */
6095 static void
6096 haifa_init_insn (rtx insn)
6098 gcc_assert (insn != NULL);
6100 sched_extend_luids ();
6101 sched_init_insn_luid (insn);
6102 sched_extend_target ();
6103 sched_deps_init (false);
6104 extend_h_i_d ();
6105 init_h_i_d (insn);
6107 if (adding_bb_to_current_region_p)
6109 sd_init_insn (insn);
6111 /* Extend dependency caches by one element. */
6112 extend_dependency_caches (1, false);
6114 if (sched_pressure_p)
6115 init_insn_reg_pressure_info (insn);
6118 /* Init data for the new basic block BB which comes after AFTER. */
6119 static void
6120 haifa_init_only_bb (basic_block bb, basic_block after)
6122 gcc_assert (bb != NULL);
6124 sched_init_bbs ();
6126 if (common_sched_info->add_block)
6127 /* This changes only data structures of the front-end. */
6128 common_sched_info->add_block (bb, after);
6131 /* A generic version of sched_split_block (). */
6132 basic_block
6133 sched_split_block_1 (basic_block first_bb, rtx after)
6135 edge e;
6137 e = split_block (first_bb, after);
6138 gcc_assert (e->src == first_bb);
6140 /* sched_split_block emits note if *check == BB_END. Probably it
6141 is better to rip that note off. */
6143 return e->dest;
6146 /* A generic version of sched_create_empty_bb (). */
6147 basic_block
6148 sched_create_empty_bb_1 (basic_block after)
6150 return create_empty_bb (after);
6153 /* Insert PAT as an INSN into the schedule and update the necessary data
6154 structures to account for it. */
6156 sched_emit_insn (rtx pat)
6158 rtx insn = emit_insn_before (pat, nonscheduled_insns_begin);
6159 haifa_init_insn (insn);
6161 if (current_sched_info->add_remove_insn)
6162 current_sched_info->add_remove_insn (insn, 0);
6164 (*current_sched_info->begin_schedule_ready) (insn);
6165 VEC_safe_push (rtx, heap, scheduled_insns, insn);
6167 last_scheduled_insn = insn;
6168 return insn;
6171 /* This function returns a candidate satisfying dispatch constraints from
6172 the ready list. */
6174 static rtx
6175 ready_remove_first_dispatch (struct ready_list *ready)
6177 int i;
6178 rtx insn = ready_element (ready, 0);
6180 if (ready->n_ready == 1
6181 || INSN_CODE (insn) < 0
6182 || !INSN_P (insn)
6183 || !active_insn_p (insn)
6184 || targetm.sched.dispatch (insn, FITS_DISPATCH_WINDOW))
6185 return ready_remove_first (ready);
6187 for (i = 1; i < ready->n_ready; i++)
6189 insn = ready_element (ready, i);
6191 if (INSN_CODE (insn) < 0
6192 || !INSN_P (insn)
6193 || !active_insn_p (insn))
6194 continue;
6196 if (targetm.sched.dispatch (insn, FITS_DISPATCH_WINDOW))
6198 /* Return ith element of ready. */
6199 insn = ready_remove (ready, i);
6200 return insn;
6204 if (targetm.sched.dispatch (NULL_RTX, DISPATCH_VIOLATION))
6205 return ready_remove_first (ready);
6207 for (i = 1; i < ready->n_ready; i++)
6209 insn = ready_element (ready, i);
6211 if (INSN_CODE (insn) < 0
6212 || !INSN_P (insn)
6213 || !active_insn_p (insn))
6214 continue;
6216 /* Return i-th element of ready. */
6217 if (targetm.sched.dispatch (insn, IS_CMP))
6218 return ready_remove (ready, i);
6221 return ready_remove_first (ready);
6224 /* Get number of ready insn in the ready list. */
6227 number_in_ready (void)
6229 return ready.n_ready;
6232 /* Get number of ready's in the ready list. */
6235 get_ready_element (int i)
6237 return ready_element (&ready, i);
6240 #endif /* INSN_SCHEDULING */