* config/sh/sh.c (push_regs): Emit movml for interrupt handler
[official-gcc.git] / gcc / haifa-sched.c
blob278768547a81cb8392db733feb2f8885b0345f35
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
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 "toplev.h"
133 #include "rtl.h"
134 #include "tm_p.h"
135 #include "hard-reg-set.h"
136 #include "regs.h"
137 #include "function.h"
138 #include "flags.h"
139 #include "insn-config.h"
140 #include "insn-attr.h"
141 #include "except.h"
142 #include "recog.h"
143 #include "sched-int.h"
144 #include "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. */
153 #ifdef INSN_SCHEDULING
155 /* issue_rate is the number of insns that can be scheduled in the same
156 machine cycle. It can be defined in the config/mach/mach.h file,
157 otherwise we set it to 1. */
159 int issue_rate;
161 /* sched-verbose controls the amount of debugging output the
162 scheduler prints. It is controlled by -fsched-verbose=N:
163 N>0 and no -DSR : the output is directed to stderr.
164 N>=10 will direct the printouts to stderr (regardless of -dSR).
165 N=1: same as -dSR.
166 N=2: bb's probabilities, detailed ready list info, unit/insn info.
167 N=3: rtl at abort point, control-flow, regions info.
168 N=5: dependences info. */
170 static int sched_verbose_param = 0;
171 int sched_verbose = 0;
173 /* Debugging file. All printouts are sent to dump, which is always set,
174 either to stderr, or to the dump listing file (-dRS). */
175 FILE *sched_dump = 0;
177 /* fix_sched_param() is called from toplev.c upon detection
178 of the -fsched-verbose=N option. */
180 void
181 fix_sched_param (const char *param, const char *val)
183 if (!strcmp (param, "verbose"))
184 sched_verbose_param = atoi (val);
185 else
186 warning (0, "fix_sched_param: unknown param: %s", param);
189 /* This is a placeholder for the scheduler parameters common
190 to all schedulers. */
191 struct common_sched_info_def *common_sched_info;
193 #define INSN_TICK(INSN) (HID (INSN)->tick)
194 #define INTER_TICK(INSN) (HID (INSN)->inter_tick)
196 /* If INSN_TICK of an instruction is equal to INVALID_TICK,
197 then it should be recalculated from scratch. */
198 #define INVALID_TICK (-(max_insn_queue_index + 1))
199 /* The minimal value of the INSN_TICK of an instruction. */
200 #define MIN_TICK (-max_insn_queue_index)
202 /* Issue points are used to distinguish between instructions in max_issue ().
203 For now, all instructions are equally good. */
204 #define ISSUE_POINTS(INSN) 1
206 /* List of important notes we must keep around. This is a pointer to the
207 last element in the list. */
208 rtx note_list;
210 static struct spec_info_def spec_info_var;
211 /* Description of the speculative part of the scheduling.
212 If NULL - no speculation. */
213 spec_info_t spec_info = NULL;
215 /* True, if recovery block was added during scheduling of current block.
216 Used to determine, if we need to fix INSN_TICKs. */
217 static bool haifa_recovery_bb_recently_added_p;
219 /* True, if recovery block was added during this scheduling pass.
220 Used to determine if we should have empty memory pools of dependencies
221 after finishing current region. */
222 bool haifa_recovery_bb_ever_added_p;
224 /* Counters of different types of speculative instructions. */
225 static int nr_begin_data, nr_be_in_data, nr_begin_control, nr_be_in_control;
227 /* Array used in {unlink, restore}_bb_notes. */
228 static rtx *bb_header = 0;
230 /* Basic block after which recovery blocks will be created. */
231 static basic_block before_recovery;
233 /* Basic block just before the EXIT_BLOCK and after recovery, if we have
234 created it. */
235 basic_block after_recovery;
237 /* FALSE if we add bb to another region, so we don't need to initialize it. */
238 bool adding_bb_to_current_region_p = true;
240 /* Queues, etc. */
242 /* An instruction is ready to be scheduled when all insns preceding it
243 have already been scheduled. It is important to ensure that all
244 insns which use its result will not be executed until its result
245 has been computed. An insn is maintained in one of four structures:
247 (P) the "Pending" set of insns which cannot be scheduled until
248 their dependencies have been satisfied.
249 (Q) the "Queued" set of insns that can be scheduled when sufficient
250 time has passed.
251 (R) the "Ready" list of unscheduled, uncommitted insns.
252 (S) the "Scheduled" list of insns.
254 Initially, all insns are either "Pending" or "Ready" depending on
255 whether their dependencies are satisfied.
257 Insns move from the "Ready" list to the "Scheduled" list as they
258 are committed to the schedule. As this occurs, the insns in the
259 "Pending" list have their dependencies satisfied and move to either
260 the "Ready" list or the "Queued" set depending on whether
261 sufficient time has passed to make them ready. As time passes,
262 insns move from the "Queued" set to the "Ready" list.
264 The "Pending" list (P) are the insns in the INSN_FORW_DEPS of the
265 unscheduled insns, i.e., those that are ready, queued, and pending.
266 The "Queued" set (Q) is implemented by the variable `insn_queue'.
267 The "Ready" list (R) is implemented by the variables `ready' and
268 `n_ready'.
269 The "Scheduled" list (S) is the new insn chain built by this pass.
271 The transition (R->S) is implemented in the scheduling loop in
272 `schedule_block' when the best insn to schedule is chosen.
273 The transitions (P->R and P->Q) are implemented in `schedule_insn' as
274 insns move from the ready list to the scheduled list.
275 The transition (Q->R) is implemented in 'queue_to_insn' as time
276 passes or stalls are introduced. */
278 /* Implement a circular buffer to delay instructions until sufficient
279 time has passed. For the new pipeline description interface,
280 MAX_INSN_QUEUE_INDEX is a power of two minus one which is not less
281 than maximal time of instruction execution computed by genattr.c on
282 the base maximal time of functional unit reservations and getting a
283 result. This is the longest time an insn may be queued. */
285 static rtx *insn_queue;
286 static int q_ptr = 0;
287 static int q_size = 0;
288 #define NEXT_Q(X) (((X)+1) & max_insn_queue_index)
289 #define NEXT_Q_AFTER(X, C) (((X)+C) & max_insn_queue_index)
291 #define QUEUE_SCHEDULED (-3)
292 #define QUEUE_NOWHERE (-2)
293 #define QUEUE_READY (-1)
294 /* QUEUE_SCHEDULED - INSN is scheduled.
295 QUEUE_NOWHERE - INSN isn't scheduled yet and is neither in
296 queue or ready list.
297 QUEUE_READY - INSN is in ready list.
298 N >= 0 - INSN queued for X [where NEXT_Q_AFTER (q_ptr, X) == N] cycles. */
300 #define QUEUE_INDEX(INSN) (HID (INSN)->queue_index)
302 /* The following variable value refers for all current and future
303 reservations of the processor units. */
304 state_t curr_state;
306 /* The following variable value is size of memory representing all
307 current and future reservations of the processor units. */
308 size_t dfa_state_size;
310 /* The following array is used to find the best insn from ready when
311 the automaton pipeline interface is used. */
312 char *ready_try = NULL;
314 /* The ready list. */
315 struct ready_list ready = {NULL, 0, 0, 0, 0};
317 /* The pointer to the ready list (to be removed). */
318 static struct ready_list *readyp = &ready;
320 /* Scheduling clock. */
321 static int clock_var;
323 static int may_trap_exp (const_rtx, int);
325 /* Nonzero iff the address is comprised from at most 1 register. */
326 #define CONST_BASED_ADDRESS_P(x) \
327 (REG_P (x) \
328 || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS \
329 || (GET_CODE (x) == LO_SUM)) \
330 && (CONSTANT_P (XEXP (x, 0)) \
331 || CONSTANT_P (XEXP (x, 1)))))
333 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
334 as found by analyzing insn's expression. */
337 static int haifa_luid_for_non_insn (rtx x);
339 /* Haifa version of sched_info hooks common to all headers. */
340 const struct common_sched_info_def haifa_common_sched_info =
342 NULL, /* fix_recovery_cfg */
343 NULL, /* add_block */
344 NULL, /* estimate_number_of_insns */
345 haifa_luid_for_non_insn, /* luid_for_non_insn */
346 SCHED_PASS_UNKNOWN /* sched_pass_id */
349 const struct sched_scan_info_def *sched_scan_info;
351 /* Mapping from instruction UID to its Logical UID. */
352 VEC (int, heap) *sched_luids = NULL;
354 /* Next LUID to assign to an instruction. */
355 int sched_max_luid = 1;
357 /* Haifa Instruction Data. */
358 VEC (haifa_insn_data_def, heap) *h_i_d = NULL;
360 void (* sched_init_only_bb) (basic_block, basic_block);
362 /* Split block function. Different schedulers might use different functions
363 to handle their internal data consistent. */
364 basic_block (* sched_split_block) (basic_block, rtx);
366 /* Create empty basic block after the specified block. */
367 basic_block (* sched_create_empty_bb) (basic_block);
369 static int
370 may_trap_exp (const_rtx x, int is_store)
372 enum rtx_code code;
374 if (x == 0)
375 return TRAP_FREE;
376 code = GET_CODE (x);
377 if (is_store)
379 if (code == MEM && may_trap_p (x))
380 return TRAP_RISKY;
381 else
382 return TRAP_FREE;
384 if (code == MEM)
386 /* The insn uses memory: a volatile load. */
387 if (MEM_VOLATILE_P (x))
388 return IRISKY;
389 /* An exception-free load. */
390 if (!may_trap_p (x))
391 return IFREE;
392 /* A load with 1 base register, to be further checked. */
393 if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
394 return PFREE_CANDIDATE;
395 /* No info on the load, to be further checked. */
396 return PRISKY_CANDIDATE;
398 else
400 const char *fmt;
401 int i, insn_class = TRAP_FREE;
403 /* Neither store nor load, check if it may cause a trap. */
404 if (may_trap_p (x))
405 return TRAP_RISKY;
406 /* Recursive step: walk the insn... */
407 fmt = GET_RTX_FORMAT (code);
408 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
410 if (fmt[i] == 'e')
412 int tmp_class = may_trap_exp (XEXP (x, i), is_store);
413 insn_class = WORST_CLASS (insn_class, tmp_class);
415 else if (fmt[i] == 'E')
417 int j;
418 for (j = 0; j < XVECLEN (x, i); j++)
420 int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
421 insn_class = WORST_CLASS (insn_class, tmp_class);
422 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
423 break;
426 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
427 break;
429 return insn_class;
433 /* Classifies rtx X of an insn for the purpose of verifying that X can be
434 executed speculatively (and consequently the insn can be moved
435 speculatively), by examining X, returning:
436 TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
437 TRAP_FREE: non-load insn.
438 IFREE: load from a globally safe location.
439 IRISKY: volatile load.
440 PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
441 being either PFREE or PRISKY. */
443 static int
444 haifa_classify_rtx (const_rtx x)
446 int tmp_class = TRAP_FREE;
447 int insn_class = TRAP_FREE;
448 enum rtx_code code;
450 if (GET_CODE (x) == PARALLEL)
452 int i, len = XVECLEN (x, 0);
454 for (i = len - 1; i >= 0; i--)
456 tmp_class = haifa_classify_rtx (XVECEXP (x, 0, i));
457 insn_class = WORST_CLASS (insn_class, tmp_class);
458 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
459 break;
462 else
464 code = GET_CODE (x);
465 switch (code)
467 case CLOBBER:
468 /* Test if it is a 'store'. */
469 tmp_class = may_trap_exp (XEXP (x, 0), 1);
470 break;
471 case SET:
472 /* Test if it is a store. */
473 tmp_class = may_trap_exp (SET_DEST (x), 1);
474 if (tmp_class == TRAP_RISKY)
475 break;
476 /* Test if it is a load. */
477 tmp_class =
478 WORST_CLASS (tmp_class,
479 may_trap_exp (SET_SRC (x), 0));
480 break;
481 case COND_EXEC:
482 tmp_class = haifa_classify_rtx (COND_EXEC_CODE (x));
483 if (tmp_class == TRAP_RISKY)
484 break;
485 tmp_class = WORST_CLASS (tmp_class,
486 may_trap_exp (COND_EXEC_TEST (x), 0));
487 break;
488 case TRAP_IF:
489 tmp_class = TRAP_RISKY;
490 break;
491 default:;
493 insn_class = tmp_class;
496 return insn_class;
500 haifa_classify_insn (const_rtx insn)
502 return haifa_classify_rtx (PATTERN (insn));
505 /* Forward declarations. */
507 static int priority (rtx);
508 static int rank_for_schedule (const void *, const void *);
509 static void swap_sort (rtx *, int);
510 static void queue_insn (rtx, int);
511 static int schedule_insn (rtx);
512 static void adjust_priority (rtx);
513 static void advance_one_cycle (void);
514 static void extend_h_i_d (void);
517 /* Notes handling mechanism:
518 =========================
519 Generally, NOTES are saved before scheduling and restored after scheduling.
520 The scheduler distinguishes between two types of notes:
522 (1) LOOP_BEGIN, LOOP_END, SETJMP, EHREGION_BEG, EHREGION_END notes:
523 Before scheduling a region, a pointer to the note is added to the insn
524 that follows or precedes it. (This happens as part of the data dependence
525 computation). After scheduling an insn, the pointer contained in it is
526 used for regenerating the corresponding note (in reemit_notes).
528 (2) All other notes (e.g. INSN_DELETED): Before scheduling a block,
529 these notes are put in a list (in rm_other_notes() and
530 unlink_other_notes ()). After scheduling the block, these notes are
531 inserted at the beginning of the block (in schedule_block()). */
533 static void ready_add (struct ready_list *, rtx, bool);
534 static rtx ready_remove_first (struct ready_list *);
536 static void queue_to_ready (struct ready_list *);
537 static int early_queue_to_ready (state_t, struct ready_list *);
539 static void debug_ready_list (struct ready_list *);
541 /* The following functions are used to implement multi-pass scheduling
542 on the first cycle. */
543 static rtx ready_remove (struct ready_list *, int);
544 static void ready_remove_insn (rtx);
546 static int choose_ready (struct ready_list *, rtx *);
548 static void fix_inter_tick (rtx, rtx);
549 static int fix_tick_ready (rtx);
550 static void change_queue_index (rtx, int);
552 /* The following functions are used to implement scheduling of data/control
553 speculative instructions. */
555 static void extend_h_i_d (void);
556 static void init_h_i_d (rtx);
557 static void generate_recovery_code (rtx);
558 static void process_insn_forw_deps_be_in_spec (rtx, rtx, ds_t);
559 static void begin_speculative_block (rtx);
560 static void add_to_speculative_block (rtx);
561 static void init_before_recovery (basic_block *);
562 static void create_check_block_twin (rtx, bool);
563 static void fix_recovery_deps (basic_block);
564 static void haifa_change_pattern (rtx, rtx);
565 static void dump_new_block_header (int, basic_block, rtx, rtx);
566 static void restore_bb_notes (basic_block);
567 static void fix_jump_move (rtx);
568 static void move_block_after_check (rtx);
569 static void move_succs (VEC(edge,gc) **, basic_block);
570 static void sched_remove_insn (rtx);
571 static void clear_priorities (rtx, rtx_vec_t *);
572 static void calc_priorities (rtx_vec_t);
573 static void add_jump_dependencies (rtx, rtx);
574 #ifdef ENABLE_CHECKING
575 static int has_edge_p (VEC(edge,gc) *, int);
576 static void check_cfg (rtx, rtx);
577 #endif
579 #endif /* INSN_SCHEDULING */
581 /* Point to state used for the current scheduling pass. */
582 struct haifa_sched_info *current_sched_info;
584 #ifndef INSN_SCHEDULING
585 void
586 schedule_insns (void)
589 #else
591 /* Do register pressure sensitive insn scheduling if the flag is set
592 up. */
593 bool sched_pressure_p;
595 /* Map regno -> its cover class. The map defined only when
596 SCHED_PRESSURE_P is true. */
597 enum reg_class *sched_regno_cover_class;
599 /* The current register pressure. Only elements corresponding cover
600 classes are defined. */
601 static int curr_reg_pressure[N_REG_CLASSES];
603 /* Saved value of the previous array. */
604 static int saved_reg_pressure[N_REG_CLASSES];
606 /* Register living at given scheduling point. */
607 static bitmap curr_reg_live;
609 /* Saved value of the previous array. */
610 static bitmap saved_reg_live;
612 /* Registers mentioned in the current region. */
613 static bitmap region_ref_regs;
615 /* Initiate register pressure relative info for scheduling the current
616 region. Currently it is only clearing register mentioned in the
617 current region. */
618 void
619 sched_init_region_reg_pressure_info (void)
621 bitmap_clear (region_ref_regs);
624 /* Update current register pressure related info after birth (if
625 BIRTH_P) or death of register REGNO. */
626 static void
627 mark_regno_birth_or_death (int regno, bool birth_p)
629 enum reg_class cover_class;
631 cover_class = sched_regno_cover_class[regno];
632 if (regno >= FIRST_PSEUDO_REGISTER)
634 if (cover_class != NO_REGS)
636 if (birth_p)
638 bitmap_set_bit (curr_reg_live, regno);
639 curr_reg_pressure[cover_class]
640 += ira_reg_class_nregs[cover_class][PSEUDO_REGNO_MODE (regno)];
642 else
644 bitmap_clear_bit (curr_reg_live, regno);
645 curr_reg_pressure[cover_class]
646 -= ira_reg_class_nregs[cover_class][PSEUDO_REGNO_MODE (regno)];
650 else if (cover_class != NO_REGS
651 && ! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
653 if (birth_p)
655 bitmap_set_bit (curr_reg_live, regno);
656 curr_reg_pressure[cover_class]++;
658 else
660 bitmap_clear_bit (curr_reg_live, regno);
661 curr_reg_pressure[cover_class]--;
666 /* Initiate current register pressure related info from living
667 registers given by LIVE. */
668 static void
669 initiate_reg_pressure_info (bitmap live)
671 int i;
672 unsigned int j;
673 bitmap_iterator bi;
675 for (i = 0; i < ira_reg_class_cover_size; i++)
676 curr_reg_pressure[ira_reg_class_cover[i]] = 0;
677 bitmap_clear (curr_reg_live);
678 EXECUTE_IF_SET_IN_BITMAP (live, 0, j, bi)
679 if (current_nr_blocks == 1 || bitmap_bit_p (region_ref_regs, j))
680 mark_regno_birth_or_death (j, true);
683 /* Mark registers in X as mentioned in the current region. */
684 static void
685 setup_ref_regs (rtx x)
687 int i, j, regno;
688 const RTX_CODE code = GET_CODE (x);
689 const char *fmt;
691 if (REG_P (x))
693 regno = REGNO (x);
694 if (regno >= FIRST_PSEUDO_REGISTER)
695 bitmap_set_bit (region_ref_regs, REGNO (x));
696 else
697 for (i = hard_regno_nregs[regno][GET_MODE (x)] - 1; i >= 0; i--)
698 bitmap_set_bit (region_ref_regs, regno + i);
699 return;
701 fmt = GET_RTX_FORMAT (code);
702 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
703 if (fmt[i] == 'e')
704 setup_ref_regs (XEXP (x, i));
705 else if (fmt[i] == 'E')
707 for (j = 0; j < XVECLEN (x, i); j++)
708 setup_ref_regs (XVECEXP (x, i, j));
712 /* Initiate current register pressure related info at the start of
713 basic block BB. */
714 static void
715 initiate_bb_reg_pressure_info (basic_block bb)
717 unsigned int i;
718 rtx insn;
720 if (current_nr_blocks > 1)
721 FOR_BB_INSNS (bb, insn)
722 if (NONDEBUG_INSN_P (insn))
723 setup_ref_regs (PATTERN (insn));
724 initiate_reg_pressure_info (df_get_live_in (bb));
725 #ifdef EH_RETURN_DATA_REGNO
726 if (bb_has_eh_pred (bb))
727 for (i = 0; ; ++i)
729 unsigned int regno = EH_RETURN_DATA_REGNO (i);
731 if (regno == INVALID_REGNUM)
732 break;
733 if (! bitmap_bit_p (df_get_live_in (bb), regno))
734 mark_regno_birth_or_death (regno, true);
736 #endif
739 /* Save current register pressure related info. */
740 static void
741 save_reg_pressure (void)
743 int i;
745 for (i = 0; i < ira_reg_class_cover_size; i++)
746 saved_reg_pressure[ira_reg_class_cover[i]]
747 = curr_reg_pressure[ira_reg_class_cover[i]];
748 bitmap_copy (saved_reg_live, curr_reg_live);
751 /* Restore saved register pressure related info. */
752 static void
753 restore_reg_pressure (void)
755 int i;
757 for (i = 0; i < ira_reg_class_cover_size; i++)
758 curr_reg_pressure[ira_reg_class_cover[i]]
759 = saved_reg_pressure[ira_reg_class_cover[i]];
760 bitmap_copy (curr_reg_live, saved_reg_live);
763 /* Return TRUE if the register is dying after its USE. */
764 static bool
765 dying_use_p (struct reg_use_data *use)
767 struct reg_use_data *next;
769 for (next = use->next_regno_use; next != use; next = next->next_regno_use)
770 if (NONDEBUG_INSN_P (next->insn)
771 && QUEUE_INDEX (next->insn) != QUEUE_SCHEDULED)
772 return false;
773 return true;
776 /* Print info about the current register pressure and its excess for
777 each cover class. */
778 static void
779 print_curr_reg_pressure (void)
781 int i;
782 enum reg_class cl;
784 fprintf (sched_dump, ";;\t");
785 for (i = 0; i < ira_reg_class_cover_size; i++)
787 cl = ira_reg_class_cover[i];
788 gcc_assert (curr_reg_pressure[cl] >= 0);
789 fprintf (sched_dump, " %s:%d(%d)", reg_class_names[cl],
790 curr_reg_pressure[cl],
791 curr_reg_pressure[cl] - ira_available_class_regs[cl]);
793 fprintf (sched_dump, "\n");
796 /* Pointer to the last instruction scheduled. Used by rank_for_schedule,
797 so that insns independent of the last scheduled insn will be preferred
798 over dependent instructions. */
800 static rtx last_scheduled_insn;
802 /* Cached cost of the instruction. Use below function to get cost of the
803 insn. -1 here means that the field is not initialized. */
804 #define INSN_COST(INSN) (HID (INSN)->cost)
806 /* Compute cost of executing INSN.
807 This is the number of cycles between instruction issue and
808 instruction results. */
810 insn_cost (rtx insn)
812 int cost;
814 if (sel_sched_p ())
816 if (recog_memoized (insn) < 0)
817 return 0;
819 cost = insn_default_latency (insn);
820 if (cost < 0)
821 cost = 0;
823 return cost;
826 cost = INSN_COST (insn);
828 if (cost < 0)
830 /* A USE insn, or something else we don't need to
831 understand. We can't pass these directly to
832 result_ready_cost or insn_default_latency because it will
833 trigger a fatal error for unrecognizable insns. */
834 if (recog_memoized (insn) < 0)
836 INSN_COST (insn) = 0;
837 return 0;
839 else
841 cost = insn_default_latency (insn);
842 if (cost < 0)
843 cost = 0;
845 INSN_COST (insn) = cost;
849 return cost;
852 /* Compute cost of dependence LINK.
853 This is the number of cycles between instruction issue and
854 instruction results.
855 ??? We also use this function to call recog_memoized on all insns. */
857 dep_cost_1 (dep_t link, dw_t dw)
859 rtx insn = DEP_PRO (link);
860 rtx used = DEP_CON (link);
861 int cost;
863 /* A USE insn should never require the value used to be computed.
864 This allows the computation of a function's result and parameter
865 values to overlap the return and call. We don't care about the
866 the dependence cost when only decreasing register pressure. */
867 if (recog_memoized (used) < 0)
869 cost = 0;
870 recog_memoized (insn);
872 else
874 enum reg_note dep_type = DEP_TYPE (link);
876 cost = insn_cost (insn);
878 if (INSN_CODE (insn) >= 0)
880 if (dep_type == REG_DEP_ANTI)
881 cost = 0;
882 else if (dep_type == REG_DEP_OUTPUT)
884 cost = (insn_default_latency (insn)
885 - insn_default_latency (used));
886 if (cost <= 0)
887 cost = 1;
889 else if (bypass_p (insn))
890 cost = insn_latency (insn, used);
894 if (targetm.sched.adjust_cost_2)
895 cost = targetm.sched.adjust_cost_2 (used, (int) dep_type, insn, cost,
896 dw);
897 else if (targetm.sched.adjust_cost != NULL)
899 /* This variable is used for backward compatibility with the
900 targets. */
901 rtx dep_cost_rtx_link = alloc_INSN_LIST (NULL_RTX, NULL_RTX);
903 /* Make it self-cycled, so that if some tries to walk over this
904 incomplete list he/she will be caught in an endless loop. */
905 XEXP (dep_cost_rtx_link, 1) = dep_cost_rtx_link;
907 /* Targets use only REG_NOTE_KIND of the link. */
908 PUT_REG_NOTE_KIND (dep_cost_rtx_link, DEP_TYPE (link));
910 cost = targetm.sched.adjust_cost (used, dep_cost_rtx_link,
911 insn, cost);
913 free_INSN_LIST_node (dep_cost_rtx_link);
916 if (cost < 0)
917 cost = 0;
920 return cost;
923 /* Compute cost of dependence LINK.
924 This is the number of cycles between instruction issue and
925 instruction results. */
927 dep_cost (dep_t link)
929 return dep_cost_1 (link, 0);
932 /* Use this sel-sched.c friendly function in reorder2 instead of increasing
933 INSN_PRIORITY explicitly. */
934 void
935 increase_insn_priority (rtx insn, int amount)
937 if (!sel_sched_p ())
939 /* We're dealing with haifa-sched.c INSN_PRIORITY. */
940 if (INSN_PRIORITY_KNOWN (insn))
941 INSN_PRIORITY (insn) += amount;
943 else
945 /* In sel-sched.c INSN_PRIORITY is not kept up to date.
946 Use EXPR_PRIORITY instead. */
947 sel_add_to_insn_priority (insn, amount);
951 /* Return 'true' if DEP should be included in priority calculations. */
952 static bool
953 contributes_to_priority_p (dep_t dep)
955 if (DEBUG_INSN_P (DEP_CON (dep))
956 || DEBUG_INSN_P (DEP_PRO (dep)))
957 return false;
959 /* Critical path is meaningful in block boundaries only. */
960 if (!current_sched_info->contributes_to_priority (DEP_CON (dep),
961 DEP_PRO (dep)))
962 return false;
964 /* If flag COUNT_SPEC_IN_CRITICAL_PATH is set,
965 then speculative instructions will less likely be
966 scheduled. That is because the priority of
967 their producers will increase, and, thus, the
968 producers will more likely be scheduled, thus,
969 resolving the dependence. */
970 if (sched_deps_info->generate_spec_deps
971 && !(spec_info->flags & COUNT_SPEC_IN_CRITICAL_PATH)
972 && (DEP_STATUS (dep) & SPECULATIVE))
973 return false;
975 return true;
978 /* Compute the number of nondebug forward deps of an insn. */
980 static int
981 dep_list_size (rtx insn)
983 sd_iterator_def sd_it;
984 dep_t dep;
985 int dbgcount = 0, nodbgcount = 0;
987 if (!MAY_HAVE_DEBUG_INSNS)
988 return sd_lists_size (insn, SD_LIST_FORW);
990 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
992 if (DEBUG_INSN_P (DEP_CON (dep)))
993 dbgcount++;
994 else if (!DEBUG_INSN_P (DEP_PRO (dep)))
995 nodbgcount++;
998 gcc_assert (dbgcount + nodbgcount == sd_lists_size (insn, SD_LIST_FORW));
1000 return nodbgcount;
1003 /* Compute the priority number for INSN. */
1004 static int
1005 priority (rtx insn)
1007 if (! INSN_P (insn))
1008 return 0;
1010 /* We should not be interested in priority of an already scheduled insn. */
1011 gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
1013 if (!INSN_PRIORITY_KNOWN (insn))
1015 int this_priority = -1;
1017 if (dep_list_size (insn) == 0)
1018 /* ??? We should set INSN_PRIORITY to insn_cost when and insn has
1019 some forward deps but all of them are ignored by
1020 contributes_to_priority hook. At the moment we set priority of
1021 such insn to 0. */
1022 this_priority = insn_cost (insn);
1023 else
1025 rtx prev_first, twin;
1026 basic_block rec;
1028 /* For recovery check instructions we calculate priority slightly
1029 different than that of normal instructions. Instead of walking
1030 through INSN_FORW_DEPS (check) list, we walk through
1031 INSN_FORW_DEPS list of each instruction in the corresponding
1032 recovery block. */
1034 /* Selective scheduling does not define RECOVERY_BLOCK macro. */
1035 rec = sel_sched_p () ? NULL : RECOVERY_BLOCK (insn);
1036 if (!rec || rec == EXIT_BLOCK_PTR)
1038 prev_first = PREV_INSN (insn);
1039 twin = insn;
1041 else
1043 prev_first = NEXT_INSN (BB_HEAD (rec));
1044 twin = PREV_INSN (BB_END (rec));
1049 sd_iterator_def sd_it;
1050 dep_t dep;
1052 FOR_EACH_DEP (twin, SD_LIST_FORW, sd_it, dep)
1054 rtx next;
1055 int next_priority;
1057 next = DEP_CON (dep);
1059 if (BLOCK_FOR_INSN (next) != rec)
1061 int cost;
1063 if (!contributes_to_priority_p (dep))
1064 continue;
1066 if (twin == insn)
1067 cost = dep_cost (dep);
1068 else
1070 struct _dep _dep1, *dep1 = &_dep1;
1072 init_dep (dep1, insn, next, REG_DEP_ANTI);
1074 cost = dep_cost (dep1);
1077 next_priority = cost + priority (next);
1079 if (next_priority > this_priority)
1080 this_priority = next_priority;
1084 twin = PREV_INSN (twin);
1086 while (twin != prev_first);
1089 if (this_priority < 0)
1091 gcc_assert (this_priority == -1);
1093 this_priority = insn_cost (insn);
1096 INSN_PRIORITY (insn) = this_priority;
1097 INSN_PRIORITY_STATUS (insn) = 1;
1100 return INSN_PRIORITY (insn);
1103 /* Macros and functions for keeping the priority queue sorted, and
1104 dealing with queuing and dequeuing of instructions. */
1106 #define SCHED_SORT(READY, N_READY) \
1107 do { if ((N_READY) == 2) \
1108 swap_sort (READY, N_READY); \
1109 else if ((N_READY) > 2) \
1110 qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); } \
1111 while (0)
1113 /* Setup info about the current register pressure impact of scheduling
1114 INSN at the current scheduling point. */
1115 static void
1116 setup_insn_reg_pressure_info (rtx insn)
1118 int i, change, before, after, hard_regno;
1119 int excess_cost_change;
1120 enum machine_mode mode;
1121 enum reg_class cl;
1122 struct reg_pressure_data *pressure_info;
1123 int *max_reg_pressure;
1124 struct reg_use_data *use;
1125 static int death[N_REG_CLASSES];
1127 gcc_checking_assert (!DEBUG_INSN_P (insn));
1129 excess_cost_change = 0;
1130 for (i = 0; i < ira_reg_class_cover_size; i++)
1131 death[ira_reg_class_cover[i]] = 0;
1132 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
1133 if (dying_use_p (use))
1135 cl = sched_regno_cover_class[use->regno];
1136 if (use->regno < FIRST_PSEUDO_REGISTER)
1137 death[cl]++;
1138 else
1139 death[cl] += ira_reg_class_nregs[cl][PSEUDO_REGNO_MODE (use->regno)];
1141 pressure_info = INSN_REG_PRESSURE (insn);
1142 max_reg_pressure = INSN_MAX_REG_PRESSURE (insn);
1143 gcc_assert (pressure_info != NULL && max_reg_pressure != NULL);
1144 for (i = 0; i < ira_reg_class_cover_size; i++)
1146 cl = ira_reg_class_cover[i];
1147 gcc_assert (curr_reg_pressure[cl] >= 0);
1148 change = (int) pressure_info[i].set_increase - death[cl];
1149 before = MAX (0, max_reg_pressure[i] - ira_available_class_regs[cl]);
1150 after = MAX (0, max_reg_pressure[i] + change
1151 - ira_available_class_regs[cl]);
1152 hard_regno = ira_class_hard_regs[cl][0];
1153 gcc_assert (hard_regno >= 0);
1154 mode = reg_raw_mode[hard_regno];
1155 excess_cost_change += ((after - before)
1156 * (ira_memory_move_cost[mode][cl][0]
1157 + ira_memory_move_cost[mode][cl][1]));
1159 INSN_REG_PRESSURE_EXCESS_COST_CHANGE (insn) = excess_cost_change;
1162 /* Returns a positive value if x is preferred; returns a negative value if
1163 y is preferred. Should never return 0, since that will make the sort
1164 unstable. */
1166 static int
1167 rank_for_schedule (const void *x, const void *y)
1169 rtx tmp = *(const rtx *) y;
1170 rtx tmp2 = *(const rtx *) x;
1171 rtx last;
1172 int tmp_class, tmp2_class;
1173 int val, priority_val, info_val;
1175 if (MAY_HAVE_DEBUG_INSNS)
1177 /* Schedule debug insns as early as possible. */
1178 if (DEBUG_INSN_P (tmp) && !DEBUG_INSN_P (tmp2))
1179 return -1;
1180 else if (DEBUG_INSN_P (tmp2))
1181 return 1;
1184 /* The insn in a schedule group should be issued the first. */
1185 if (flag_sched_group_heuristic &&
1186 SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
1187 return SCHED_GROUP_P (tmp2) ? 1 : -1;
1189 /* Make sure that priority of TMP and TMP2 are initialized. */
1190 gcc_assert (INSN_PRIORITY_KNOWN (tmp) && INSN_PRIORITY_KNOWN (tmp2));
1192 if (sched_pressure_p)
1194 int diff;
1196 /* Prefer insn whose scheduling results in the smallest register
1197 pressure excess. */
1198 if ((diff = (INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp)
1199 + (INSN_TICK (tmp) > clock_var
1200 ? INSN_TICK (tmp) - clock_var : 0)
1201 - INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp2)
1202 - (INSN_TICK (tmp2) > clock_var
1203 ? INSN_TICK (tmp2) - clock_var : 0))) != 0)
1204 return diff;
1208 if (sched_pressure_p
1209 && (INSN_TICK (tmp2) > clock_var || INSN_TICK (tmp) > clock_var))
1211 if (INSN_TICK (tmp) <= clock_var)
1212 return -1;
1213 else if (INSN_TICK (tmp2) <= clock_var)
1214 return 1;
1215 else
1216 return INSN_TICK (tmp) - INSN_TICK (tmp2);
1218 /* Prefer insn with higher priority. */
1219 priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
1221 if (flag_sched_critical_path_heuristic && priority_val)
1222 return priority_val;
1224 /* Prefer speculative insn with greater dependencies weakness. */
1225 if (flag_sched_spec_insn_heuristic && spec_info)
1227 ds_t ds1, ds2;
1228 dw_t dw1, dw2;
1229 int dw;
1231 ds1 = TODO_SPEC (tmp) & SPECULATIVE;
1232 if (ds1)
1233 dw1 = ds_weak (ds1);
1234 else
1235 dw1 = NO_DEP_WEAK;
1237 ds2 = TODO_SPEC (tmp2) & SPECULATIVE;
1238 if (ds2)
1239 dw2 = ds_weak (ds2);
1240 else
1241 dw2 = NO_DEP_WEAK;
1243 dw = dw2 - dw1;
1244 if (dw > (NO_DEP_WEAK / 8) || dw < -(NO_DEP_WEAK / 8))
1245 return dw;
1248 info_val = (*current_sched_info->rank) (tmp, tmp2);
1249 if(flag_sched_rank_heuristic && info_val)
1250 return info_val;
1252 if (flag_sched_last_insn_heuristic)
1254 last = last_scheduled_insn;
1256 if (DEBUG_INSN_P (last) && last != current_sched_info->prev_head)
1258 last = PREV_INSN (last);
1259 while (!NONDEBUG_INSN_P (last)
1260 && last != current_sched_info->prev_head);
1263 /* Compare insns based on their relation to the last scheduled
1264 non-debug insn. */
1265 if (flag_sched_last_insn_heuristic && NONDEBUG_INSN_P (last))
1267 dep_t dep1;
1268 dep_t dep2;
1270 /* Classify the instructions into three classes:
1271 1) Data dependent on last schedule insn.
1272 2) Anti/Output dependent on last scheduled insn.
1273 3) Independent of last scheduled insn, or has latency of one.
1274 Choose the insn from the highest numbered class if different. */
1275 dep1 = sd_find_dep_between (last, tmp, true);
1277 if (dep1 == NULL || dep_cost (dep1) == 1)
1278 tmp_class = 3;
1279 else if (/* Data dependence. */
1280 DEP_TYPE (dep1) == REG_DEP_TRUE)
1281 tmp_class = 1;
1282 else
1283 tmp_class = 2;
1285 dep2 = sd_find_dep_between (last, tmp2, true);
1287 if (dep2 == NULL || dep_cost (dep2) == 1)
1288 tmp2_class = 3;
1289 else if (/* Data dependence. */
1290 DEP_TYPE (dep2) == REG_DEP_TRUE)
1291 tmp2_class = 1;
1292 else
1293 tmp2_class = 2;
1295 if ((val = tmp2_class - tmp_class))
1296 return val;
1299 /* Prefer the insn which has more later insns that depend on it.
1300 This gives the scheduler more freedom when scheduling later
1301 instructions at the expense of added register pressure. */
1303 val = (dep_list_size (tmp2) - dep_list_size (tmp));
1305 if (flag_sched_dep_count_heuristic && val != 0)
1306 return val;
1308 /* If insns are equally good, sort by INSN_LUID (original insn order),
1309 so that we make the sort stable. This minimizes instruction movement,
1310 thus minimizing sched's effect on debugging and cross-jumping. */
1311 return INSN_LUID (tmp) - INSN_LUID (tmp2);
1314 /* Resort the array A in which only element at index N may be out of order. */
1316 HAIFA_INLINE static void
1317 swap_sort (rtx *a, int n)
1319 rtx insn = a[n - 1];
1320 int i = n - 2;
1322 while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
1324 a[i + 1] = a[i];
1325 i -= 1;
1327 a[i + 1] = insn;
1330 /* Add INSN to the insn queue so that it can be executed at least
1331 N_CYCLES after the currently executing insn. Preserve insns
1332 chain for debugging purposes. */
1334 HAIFA_INLINE static void
1335 queue_insn (rtx insn, int n_cycles)
1337 int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
1338 rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
1340 gcc_assert (n_cycles <= max_insn_queue_index);
1341 gcc_assert (!DEBUG_INSN_P (insn));
1343 insn_queue[next_q] = link;
1344 q_size += 1;
1346 if (sched_verbose >= 2)
1348 fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
1349 (*current_sched_info->print_insn) (insn, 0));
1351 fprintf (sched_dump, "queued for %d cycles.\n", n_cycles);
1354 QUEUE_INDEX (insn) = next_q;
1357 /* Remove INSN from queue. */
1358 static void
1359 queue_remove (rtx insn)
1361 gcc_assert (QUEUE_INDEX (insn) >= 0);
1362 remove_free_INSN_LIST_elem (insn, &insn_queue[QUEUE_INDEX (insn)]);
1363 q_size--;
1364 QUEUE_INDEX (insn) = QUEUE_NOWHERE;
1367 /* Return a pointer to the bottom of the ready list, i.e. the insn
1368 with the lowest priority. */
1370 rtx *
1371 ready_lastpos (struct ready_list *ready)
1373 gcc_assert (ready->n_ready >= 1);
1374 return ready->vec + ready->first - ready->n_ready + 1;
1377 /* Add an element INSN to the ready list so that it ends up with the
1378 lowest/highest priority depending on FIRST_P. */
1380 HAIFA_INLINE static void
1381 ready_add (struct ready_list *ready, rtx insn, bool first_p)
1383 if (!first_p)
1385 if (ready->first == ready->n_ready)
1387 memmove (ready->vec + ready->veclen - ready->n_ready,
1388 ready_lastpos (ready),
1389 ready->n_ready * sizeof (rtx));
1390 ready->first = ready->veclen - 1;
1392 ready->vec[ready->first - ready->n_ready] = insn;
1394 else
1396 if (ready->first == ready->veclen - 1)
1398 if (ready->n_ready)
1399 /* ready_lastpos() fails when called with (ready->n_ready == 0). */
1400 memmove (ready->vec + ready->veclen - ready->n_ready - 1,
1401 ready_lastpos (ready),
1402 ready->n_ready * sizeof (rtx));
1403 ready->first = ready->veclen - 2;
1405 ready->vec[++(ready->first)] = insn;
1408 ready->n_ready++;
1409 if (DEBUG_INSN_P (insn))
1410 ready->n_debug++;
1412 gcc_assert (QUEUE_INDEX (insn) != QUEUE_READY);
1413 QUEUE_INDEX (insn) = QUEUE_READY;
1416 /* Remove the element with the highest priority from the ready list and
1417 return it. */
1419 HAIFA_INLINE static rtx
1420 ready_remove_first (struct ready_list *ready)
1422 rtx t;
1424 gcc_assert (ready->n_ready);
1425 t = ready->vec[ready->first--];
1426 ready->n_ready--;
1427 if (DEBUG_INSN_P (t))
1428 ready->n_debug--;
1429 /* If the queue becomes empty, reset it. */
1430 if (ready->n_ready == 0)
1431 ready->first = ready->veclen - 1;
1433 gcc_assert (QUEUE_INDEX (t) == QUEUE_READY);
1434 QUEUE_INDEX (t) = QUEUE_NOWHERE;
1436 return t;
1439 /* The following code implements multi-pass scheduling for the first
1440 cycle. In other words, we will try to choose ready insn which
1441 permits to start maximum number of insns on the same cycle. */
1443 /* Return a pointer to the element INDEX from the ready. INDEX for
1444 insn with the highest priority is 0, and the lowest priority has
1445 N_READY - 1. */
1448 ready_element (struct ready_list *ready, int index)
1450 gcc_assert (ready->n_ready && index < ready->n_ready);
1452 return ready->vec[ready->first - index];
1455 /* Remove the element INDEX from the ready list and return it. INDEX
1456 for insn with the highest priority is 0, and the lowest priority
1457 has N_READY - 1. */
1459 HAIFA_INLINE static rtx
1460 ready_remove (struct ready_list *ready, int index)
1462 rtx t;
1463 int i;
1465 if (index == 0)
1466 return ready_remove_first (ready);
1467 gcc_assert (ready->n_ready && index < ready->n_ready);
1468 t = ready->vec[ready->first - index];
1469 ready->n_ready--;
1470 if (DEBUG_INSN_P (t))
1471 ready->n_debug--;
1472 for (i = index; i < ready->n_ready; i++)
1473 ready->vec[ready->first - i] = ready->vec[ready->first - i - 1];
1474 QUEUE_INDEX (t) = QUEUE_NOWHERE;
1475 return t;
1478 /* Remove INSN from the ready list. */
1479 static void
1480 ready_remove_insn (rtx insn)
1482 int i;
1484 for (i = 0; i < readyp->n_ready; i++)
1485 if (ready_element (readyp, i) == insn)
1487 ready_remove (readyp, i);
1488 return;
1490 gcc_unreachable ();
1493 /* Sort the ready list READY by ascending priority, using the SCHED_SORT
1494 macro. */
1496 void
1497 ready_sort (struct ready_list *ready)
1499 int i;
1500 rtx *first = ready_lastpos (ready);
1502 if (sched_pressure_p)
1504 for (i = 0; i < ready->n_ready; i++)
1505 if (!DEBUG_INSN_P (first[i]))
1506 setup_insn_reg_pressure_info (first[i]);
1508 SCHED_SORT (first, ready->n_ready);
1511 /* PREV is an insn that is ready to execute. Adjust its priority if that
1512 will help shorten or lengthen register lifetimes as appropriate. Also
1513 provide a hook for the target to tweak itself. */
1515 HAIFA_INLINE static void
1516 adjust_priority (rtx prev)
1518 /* ??? There used to be code here to try and estimate how an insn
1519 affected register lifetimes, but it did it by looking at REG_DEAD
1520 notes, which we removed in schedule_region. Nor did it try to
1521 take into account register pressure or anything useful like that.
1523 Revisit when we have a machine model to work with and not before. */
1525 if (targetm.sched.adjust_priority)
1526 INSN_PRIORITY (prev) =
1527 targetm.sched.adjust_priority (prev, INSN_PRIORITY (prev));
1530 /* Advance DFA state STATE on one cycle. */
1531 void
1532 advance_state (state_t state)
1534 if (targetm.sched.dfa_pre_advance_cycle)
1535 targetm.sched.dfa_pre_advance_cycle ();
1537 if (targetm.sched.dfa_pre_cycle_insn)
1538 state_transition (state,
1539 targetm.sched.dfa_pre_cycle_insn ());
1541 state_transition (state, NULL);
1543 if (targetm.sched.dfa_post_cycle_insn)
1544 state_transition (state,
1545 targetm.sched.dfa_post_cycle_insn ());
1547 if (targetm.sched.dfa_post_advance_cycle)
1548 targetm.sched.dfa_post_advance_cycle ();
1551 /* Advance time on one cycle. */
1552 HAIFA_INLINE static void
1553 advance_one_cycle (void)
1555 advance_state (curr_state);
1556 if (sched_verbose >= 6)
1557 fprintf (sched_dump, ";;\tAdvanced a state.\n");
1560 /* Clock at which the previous instruction was issued. */
1561 static int last_clock_var;
1563 /* Update register pressure after scheduling INSN. */
1564 static void
1565 update_register_pressure (rtx insn)
1567 struct reg_use_data *use;
1568 struct reg_set_data *set;
1570 gcc_checking_assert (!DEBUG_INSN_P (insn));
1572 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
1573 if (dying_use_p (use) && bitmap_bit_p (curr_reg_live, use->regno))
1574 mark_regno_birth_or_death (use->regno, false);
1575 for (set = INSN_REG_SET_LIST (insn); set != NULL; set = set->next_insn_set)
1576 mark_regno_birth_or_death (set->regno, true);
1579 /* Set up or update (if UPDATE_P) max register pressure (see its
1580 meaning in sched-int.h::_haifa_insn_data) for all current BB insns
1581 after insn AFTER. */
1582 static void
1583 setup_insn_max_reg_pressure (rtx after, bool update_p)
1585 int i, p;
1586 bool eq_p;
1587 rtx insn;
1588 static int max_reg_pressure[N_REG_CLASSES];
1590 save_reg_pressure ();
1591 for (i = 0; i < ira_reg_class_cover_size; i++)
1592 max_reg_pressure[ira_reg_class_cover[i]]
1593 = curr_reg_pressure[ira_reg_class_cover[i]];
1594 for (insn = NEXT_INSN (after);
1595 insn != NULL_RTX && ! BARRIER_P (insn)
1596 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (after);
1597 insn = NEXT_INSN (insn))
1598 if (NONDEBUG_INSN_P (insn))
1600 eq_p = true;
1601 for (i = 0; i < ira_reg_class_cover_size; i++)
1603 p = max_reg_pressure[ira_reg_class_cover[i]];
1604 if (INSN_MAX_REG_PRESSURE (insn)[i] != p)
1606 eq_p = false;
1607 INSN_MAX_REG_PRESSURE (insn)[i]
1608 = max_reg_pressure[ira_reg_class_cover[i]];
1611 if (update_p && eq_p)
1612 break;
1613 update_register_pressure (insn);
1614 for (i = 0; i < ira_reg_class_cover_size; i++)
1615 if (max_reg_pressure[ira_reg_class_cover[i]]
1616 < curr_reg_pressure[ira_reg_class_cover[i]])
1617 max_reg_pressure[ira_reg_class_cover[i]]
1618 = curr_reg_pressure[ira_reg_class_cover[i]];
1620 restore_reg_pressure ();
1623 /* Update the current register pressure after scheduling INSN. Update
1624 also max register pressure for unscheduled insns of the current
1625 BB. */
1626 static void
1627 update_reg_and_insn_max_reg_pressure (rtx insn)
1629 int i;
1630 int before[N_REG_CLASSES];
1632 for (i = 0; i < ira_reg_class_cover_size; i++)
1633 before[i] = curr_reg_pressure[ira_reg_class_cover[i]];
1634 update_register_pressure (insn);
1635 for (i = 0; i < ira_reg_class_cover_size; i++)
1636 if (curr_reg_pressure[ira_reg_class_cover[i]] != before[i])
1637 break;
1638 if (i < ira_reg_class_cover_size)
1639 setup_insn_max_reg_pressure (insn, true);
1642 /* Set up register pressure at the beginning of basic block BB whose
1643 insns starting after insn AFTER. Set up also max register pressure
1644 for all insns of the basic block. */
1645 void
1646 sched_setup_bb_reg_pressure_info (basic_block bb, rtx after)
1648 gcc_assert (sched_pressure_p);
1649 initiate_bb_reg_pressure_info (bb);
1650 setup_insn_max_reg_pressure (after, false);
1653 /* INSN is the "currently executing insn". Launch each insn which was
1654 waiting on INSN. READY is the ready list which contains the insns
1655 that are ready to fire. CLOCK is the current cycle. The function
1656 returns necessary cycle advance after issuing the insn (it is not
1657 zero for insns in a schedule group). */
1659 static int
1660 schedule_insn (rtx insn)
1662 sd_iterator_def sd_it;
1663 dep_t dep;
1664 int i;
1665 int advance = 0;
1667 if (sched_verbose >= 1)
1669 struct reg_pressure_data *pressure_info;
1670 char buf[2048];
1672 print_insn (buf, insn, 0);
1673 buf[40] = 0;
1674 fprintf (sched_dump, ";;\t%3i--> %-40s:", clock_var, buf);
1676 if (recog_memoized (insn) < 0)
1677 fprintf (sched_dump, "nothing");
1678 else
1679 print_reservation (sched_dump, insn);
1680 pressure_info = INSN_REG_PRESSURE (insn);
1681 if (pressure_info != NULL)
1683 fputc (':', sched_dump);
1684 for (i = 0; i < ira_reg_class_cover_size; i++)
1685 fprintf (sched_dump, "%s%+d(%d)",
1686 reg_class_names[ira_reg_class_cover[i]],
1687 pressure_info[i].set_increase, pressure_info[i].change);
1689 fputc ('\n', sched_dump);
1692 if (sched_pressure_p && !DEBUG_INSN_P (insn))
1693 update_reg_and_insn_max_reg_pressure (insn);
1695 /* Scheduling instruction should have all its dependencies resolved and
1696 should have been removed from the ready list. */
1697 gcc_assert (sd_lists_empty_p (insn, SD_LIST_BACK));
1699 /* Reset debug insns invalidated by moving this insn. */
1700 if (MAY_HAVE_DEBUG_INSNS && !DEBUG_INSN_P (insn))
1701 for (sd_it = sd_iterator_start (insn, SD_LIST_BACK);
1702 sd_iterator_cond (&sd_it, &dep);)
1704 rtx dbg = DEP_PRO (dep);
1705 struct reg_use_data *use, *next;
1707 gcc_assert (DEBUG_INSN_P (dbg));
1709 if (sched_verbose >= 6)
1710 fprintf (sched_dump, ";;\t\tresetting: debug insn %d\n",
1711 INSN_UID (dbg));
1713 /* ??? Rather than resetting the debug insn, we might be able
1714 to emit a debug temp before the just-scheduled insn, but
1715 this would involve checking that the expression at the
1716 point of the debug insn is equivalent to the expression
1717 before the just-scheduled insn. They might not be: the
1718 expression in the debug insn may depend on other insns not
1719 yet scheduled that set MEMs, REGs or even other debug
1720 insns. It's not clear that attempting to preserve debug
1721 information in these cases is worth the effort, given how
1722 uncommon these resets are and the likelihood that the debug
1723 temps introduced won't survive the schedule change. */
1724 INSN_VAR_LOCATION_LOC (dbg) = gen_rtx_UNKNOWN_VAR_LOC ();
1725 df_insn_rescan (dbg);
1727 /* Unknown location doesn't use any registers. */
1728 for (use = INSN_REG_USE_LIST (dbg); use != NULL; use = next)
1730 struct reg_use_data *prev = use;
1732 /* Remove use from the cyclic next_regno_use chain first. */
1733 while (prev->next_regno_use != use)
1734 prev = prev->next_regno_use;
1735 prev->next_regno_use = use->next_regno_use;
1736 next = use->next_insn_use;
1737 free (use);
1739 INSN_REG_USE_LIST (dbg) = NULL;
1741 /* We delete rather than resolve these deps, otherwise we
1742 crash in sched_free_deps(), because forward deps are
1743 expected to be released before backward deps. */
1744 sd_delete_dep (sd_it);
1747 gcc_assert (QUEUE_INDEX (insn) == QUEUE_NOWHERE);
1748 QUEUE_INDEX (insn) = QUEUE_SCHEDULED;
1750 gcc_assert (INSN_TICK (insn) >= MIN_TICK);
1751 if (INSN_TICK (insn) > clock_var)
1752 /* INSN has been prematurely moved from the queue to the ready list.
1753 This is possible only if following flag is set. */
1754 gcc_assert (flag_sched_stalled_insns);
1756 /* ??? Probably, if INSN is scheduled prematurely, we should leave
1757 INSN_TICK untouched. This is a machine-dependent issue, actually. */
1758 INSN_TICK (insn) = clock_var;
1760 /* Update dependent instructions. */
1761 for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
1762 sd_iterator_cond (&sd_it, &dep);)
1764 rtx next = DEP_CON (dep);
1766 /* Resolve the dependence between INSN and NEXT.
1767 sd_resolve_dep () moves current dep to another list thus
1768 advancing the iterator. */
1769 sd_resolve_dep (sd_it);
1771 /* Don't bother trying to mark next as ready if insn is a debug
1772 insn. If insn is the last hard dependency, it will have
1773 already been discounted. */
1774 if (DEBUG_INSN_P (insn) && !DEBUG_INSN_P (next))
1775 continue;
1777 if (!IS_SPECULATION_BRANCHY_CHECK_P (insn))
1779 int effective_cost;
1781 effective_cost = try_ready (next);
1783 if (effective_cost >= 0
1784 && SCHED_GROUP_P (next)
1785 && advance < effective_cost)
1786 advance = effective_cost;
1788 else
1789 /* Check always has only one forward dependence (to the first insn in
1790 the recovery block), therefore, this will be executed only once. */
1792 gcc_assert (sd_lists_empty_p (insn, SD_LIST_FORW));
1793 fix_recovery_deps (RECOVERY_BLOCK (insn));
1797 /* This is the place where scheduler doesn't *basically* need backward and
1798 forward dependencies for INSN anymore. Nevertheless they are used in
1799 heuristics in rank_for_schedule (), early_queue_to_ready () and in
1800 some targets (e.g. rs6000). Thus the earliest place where we *can*
1801 remove dependencies is after targetm.sched.finish () call in
1802 schedule_block (). But, on the other side, the safest place to remove
1803 dependencies is when we are finishing scheduling entire region. As we
1804 don't generate [many] dependencies during scheduling itself, we won't
1805 need memory until beginning of next region.
1806 Bottom line: Dependencies are removed for all insns in the end of
1807 scheduling the region. */
1809 /* Annotate the instruction with issue information -- TImode
1810 indicates that the instruction is expected not to be able
1811 to issue on the same cycle as the previous insn. A machine
1812 may use this information to decide how the instruction should
1813 be aligned. */
1814 if (issue_rate > 1
1815 && GET_CODE (PATTERN (insn)) != USE
1816 && GET_CODE (PATTERN (insn)) != CLOBBER
1817 && !DEBUG_INSN_P (insn))
1819 if (reload_completed)
1820 PUT_MODE (insn, clock_var > last_clock_var ? TImode : VOIDmode);
1821 last_clock_var = clock_var;
1824 return advance;
1827 /* Functions for handling of notes. */
1829 /* Add note list that ends on FROM_END to the end of TO_ENDP. */
1830 void
1831 concat_note_lists (rtx from_end, rtx *to_endp)
1833 rtx from_start;
1835 /* It's easy when have nothing to concat. */
1836 if (from_end == NULL)
1837 return;
1839 /* It's also easy when destination is empty. */
1840 if (*to_endp == NULL)
1842 *to_endp = from_end;
1843 return;
1846 from_start = from_end;
1847 while (PREV_INSN (from_start) != NULL)
1848 from_start = PREV_INSN (from_start);
1850 PREV_INSN (from_start) = *to_endp;
1851 NEXT_INSN (*to_endp) = from_start;
1852 *to_endp = from_end;
1855 /* Delete notes between HEAD and TAIL and put them in the chain
1856 of notes ended by NOTE_LIST. */
1857 void
1858 remove_notes (rtx head, rtx tail)
1860 rtx next_tail, insn, next;
1862 note_list = 0;
1863 if (head == tail && !INSN_P (head))
1864 return;
1866 next_tail = NEXT_INSN (tail);
1867 for (insn = head; insn != next_tail; insn = next)
1869 next = NEXT_INSN (insn);
1870 if (!NOTE_P (insn))
1871 continue;
1873 switch (NOTE_KIND (insn))
1875 case NOTE_INSN_BASIC_BLOCK:
1876 continue;
1878 case NOTE_INSN_EPILOGUE_BEG:
1879 if (insn != tail)
1881 remove_insn (insn);
1882 add_reg_note (next, REG_SAVE_NOTE,
1883 GEN_INT (NOTE_INSN_EPILOGUE_BEG));
1884 break;
1886 /* FALLTHRU */
1888 default:
1889 remove_insn (insn);
1891 /* Add the note to list that ends at NOTE_LIST. */
1892 PREV_INSN (insn) = note_list;
1893 NEXT_INSN (insn) = NULL_RTX;
1894 if (note_list)
1895 NEXT_INSN (note_list) = insn;
1896 note_list = insn;
1897 break;
1900 gcc_assert ((sel_sched_p () || insn != tail) && insn != head);
1905 /* Return the head and tail pointers of ebb starting at BEG and ending
1906 at END. */
1907 void
1908 get_ebb_head_tail (basic_block beg, basic_block end, rtx *headp, rtx *tailp)
1910 rtx beg_head = BB_HEAD (beg);
1911 rtx beg_tail = BB_END (beg);
1912 rtx end_head = BB_HEAD (end);
1913 rtx end_tail = BB_END (end);
1915 /* Don't include any notes or labels at the beginning of the BEG
1916 basic block, or notes at the end of the END basic blocks. */
1918 if (LABEL_P (beg_head))
1919 beg_head = NEXT_INSN (beg_head);
1921 while (beg_head != beg_tail)
1922 if (NOTE_P (beg_head) || BOUNDARY_DEBUG_INSN_P (beg_head))
1923 beg_head = NEXT_INSN (beg_head);
1924 else
1925 break;
1927 *headp = beg_head;
1929 if (beg == end)
1930 end_head = beg_head;
1931 else if (LABEL_P (end_head))
1932 end_head = NEXT_INSN (end_head);
1934 while (end_head != end_tail)
1935 if (NOTE_P (end_tail) || BOUNDARY_DEBUG_INSN_P (end_tail))
1936 end_tail = PREV_INSN (end_tail);
1937 else
1938 break;
1940 *tailp = end_tail;
1943 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ]. */
1946 no_real_insns_p (const_rtx head, const_rtx tail)
1948 while (head != NEXT_INSN (tail))
1950 if (!NOTE_P (head) && !LABEL_P (head)
1951 && !BOUNDARY_DEBUG_INSN_P (head))
1952 return 0;
1953 head = NEXT_INSN (head);
1955 return 1;
1958 /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
1959 previously found among the insns. Insert them just before HEAD. */
1961 restore_other_notes (rtx head, basic_block head_bb)
1963 if (note_list != 0)
1965 rtx note_head = note_list;
1967 if (head)
1968 head_bb = BLOCK_FOR_INSN (head);
1969 else
1970 head = NEXT_INSN (bb_note (head_bb));
1972 while (PREV_INSN (note_head))
1974 set_block_for_insn (note_head, head_bb);
1975 note_head = PREV_INSN (note_head);
1977 /* In the above cycle we've missed this note. */
1978 set_block_for_insn (note_head, head_bb);
1980 PREV_INSN (note_head) = PREV_INSN (head);
1981 NEXT_INSN (PREV_INSN (head)) = note_head;
1982 PREV_INSN (head) = note_list;
1983 NEXT_INSN (note_list) = head;
1985 if (BLOCK_FOR_INSN (head) != head_bb)
1986 BB_END (head_bb) = note_list;
1988 head = note_head;
1991 return head;
1994 /* Move insns that became ready to fire from queue to ready list. */
1996 static void
1997 queue_to_ready (struct ready_list *ready)
1999 rtx insn;
2000 rtx link;
2001 rtx skip_insn;
2003 q_ptr = NEXT_Q (q_ptr);
2005 if (dbg_cnt (sched_insn) == false)
2006 /* If debug counter is activated do not requeue insn next after
2007 last_scheduled_insn. */
2008 skip_insn = next_nonnote_nondebug_insn (last_scheduled_insn);
2009 else
2010 skip_insn = NULL_RTX;
2012 /* Add all pending insns that can be scheduled without stalls to the
2013 ready list. */
2014 for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
2016 insn = XEXP (link, 0);
2017 q_size -= 1;
2019 if (sched_verbose >= 2)
2020 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
2021 (*current_sched_info->print_insn) (insn, 0));
2023 /* If the ready list is full, delay the insn for 1 cycle.
2024 See the comment in schedule_block for the rationale. */
2025 if (!reload_completed
2026 && ready->n_ready - ready->n_debug > MAX_SCHED_READY_INSNS
2027 && !SCHED_GROUP_P (insn)
2028 && insn != skip_insn)
2030 if (sched_verbose >= 2)
2031 fprintf (sched_dump, "requeued because ready full\n");
2032 queue_insn (insn, 1);
2034 else
2036 ready_add (ready, insn, false);
2037 if (sched_verbose >= 2)
2038 fprintf (sched_dump, "moving to ready without stalls\n");
2041 free_INSN_LIST_list (&insn_queue[q_ptr]);
2043 /* If there are no ready insns, stall until one is ready and add all
2044 of the pending insns at that point to the ready list. */
2045 if (ready->n_ready == 0)
2047 int stalls;
2049 for (stalls = 1; stalls <= max_insn_queue_index; stalls++)
2051 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
2053 for (; link; link = XEXP (link, 1))
2055 insn = XEXP (link, 0);
2056 q_size -= 1;
2058 if (sched_verbose >= 2)
2059 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
2060 (*current_sched_info->print_insn) (insn, 0));
2062 ready_add (ready, insn, false);
2063 if (sched_verbose >= 2)
2064 fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
2066 free_INSN_LIST_list (&insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]);
2068 advance_one_cycle ();
2070 break;
2073 advance_one_cycle ();
2076 q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
2077 clock_var += stalls;
2081 /* Used by early_queue_to_ready. Determines whether it is "ok" to
2082 prematurely move INSN from the queue to the ready list. Currently,
2083 if a target defines the hook 'is_costly_dependence', this function
2084 uses the hook to check whether there exist any dependences which are
2085 considered costly by the target, between INSN and other insns that
2086 have already been scheduled. Dependences are checked up to Y cycles
2087 back, with default Y=1; The flag -fsched-stalled-insns-dep=Y allows
2088 controlling this value.
2089 (Other considerations could be taken into account instead (or in
2090 addition) depending on user flags and target hooks. */
2092 static bool
2093 ok_for_early_queue_removal (rtx insn)
2095 int n_cycles;
2096 rtx prev_insn = last_scheduled_insn;
2098 if (targetm.sched.is_costly_dependence)
2100 for (n_cycles = flag_sched_stalled_insns_dep; n_cycles; n_cycles--)
2102 for ( ; prev_insn; prev_insn = PREV_INSN (prev_insn))
2104 int cost;
2106 if (prev_insn == current_sched_info->prev_head)
2108 prev_insn = NULL;
2109 break;
2112 if (!NOTE_P (prev_insn))
2114 dep_t dep;
2116 dep = sd_find_dep_between (prev_insn, insn, true);
2118 if (dep != NULL)
2120 cost = dep_cost (dep);
2122 if (targetm.sched.is_costly_dependence (dep, cost,
2123 flag_sched_stalled_insns_dep - n_cycles))
2124 return false;
2128 if (GET_MODE (prev_insn) == TImode) /* end of dispatch group */
2129 break;
2132 if (!prev_insn)
2133 break;
2134 prev_insn = PREV_INSN (prev_insn);
2138 return true;
2142 /* Remove insns from the queue, before they become "ready" with respect
2143 to FU latency considerations. */
2145 static int
2146 early_queue_to_ready (state_t state, struct ready_list *ready)
2148 rtx insn;
2149 rtx link;
2150 rtx next_link;
2151 rtx prev_link;
2152 bool move_to_ready;
2153 int cost;
2154 state_t temp_state = alloca (dfa_state_size);
2155 int stalls;
2156 int insns_removed = 0;
2159 Flag '-fsched-stalled-insns=X' determines the aggressiveness of this
2160 function:
2162 X == 0: There is no limit on how many queued insns can be removed
2163 prematurely. (flag_sched_stalled_insns = -1).
2165 X >= 1: Only X queued insns can be removed prematurely in each
2166 invocation. (flag_sched_stalled_insns = X).
2168 Otherwise: Early queue removal is disabled.
2169 (flag_sched_stalled_insns = 0)
2172 if (! flag_sched_stalled_insns)
2173 return 0;
2175 for (stalls = 0; stalls <= max_insn_queue_index; stalls++)
2177 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
2179 if (sched_verbose > 6)
2180 fprintf (sched_dump, ";; look at index %d + %d\n", q_ptr, stalls);
2182 prev_link = 0;
2183 while (link)
2185 next_link = XEXP (link, 1);
2186 insn = XEXP (link, 0);
2187 if (insn && sched_verbose > 6)
2188 print_rtl_single (sched_dump, insn);
2190 memcpy (temp_state, state, dfa_state_size);
2191 if (recog_memoized (insn) < 0)
2192 /* non-negative to indicate that it's not ready
2193 to avoid infinite Q->R->Q->R... */
2194 cost = 0;
2195 else
2196 cost = state_transition (temp_state, insn);
2198 if (sched_verbose >= 6)
2199 fprintf (sched_dump, "transition cost = %d\n", cost);
2201 move_to_ready = false;
2202 if (cost < 0)
2204 move_to_ready = ok_for_early_queue_removal (insn);
2205 if (move_to_ready == true)
2207 /* move from Q to R */
2208 q_size -= 1;
2209 ready_add (ready, insn, false);
2211 if (prev_link)
2212 XEXP (prev_link, 1) = next_link;
2213 else
2214 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = next_link;
2216 free_INSN_LIST_node (link);
2218 if (sched_verbose >= 2)
2219 fprintf (sched_dump, ";;\t\tEarly Q-->Ready: insn %s\n",
2220 (*current_sched_info->print_insn) (insn, 0));
2222 insns_removed++;
2223 if (insns_removed == flag_sched_stalled_insns)
2224 /* Remove no more than flag_sched_stalled_insns insns
2225 from Q at a time. */
2226 return insns_removed;
2230 if (move_to_ready == false)
2231 prev_link = link;
2233 link = next_link;
2234 } /* while link */
2235 } /* if link */
2237 } /* for stalls.. */
2239 return insns_removed;
2243 /* Print the ready list for debugging purposes. Callable from debugger. */
2245 static void
2246 debug_ready_list (struct ready_list *ready)
2248 rtx *p;
2249 int i;
2251 if (ready->n_ready == 0)
2253 fprintf (sched_dump, "\n");
2254 return;
2257 p = ready_lastpos (ready);
2258 for (i = 0; i < ready->n_ready; i++)
2260 fprintf (sched_dump, " %s:%d",
2261 (*current_sched_info->print_insn) (p[i], 0),
2262 INSN_LUID (p[i]));
2263 if (sched_pressure_p)
2264 fprintf (sched_dump, "(cost=%d",
2265 INSN_REG_PRESSURE_EXCESS_COST_CHANGE (p[i]));
2266 if (INSN_TICK (p[i]) > clock_var)
2267 fprintf (sched_dump, ":delay=%d", INSN_TICK (p[i]) - clock_var);
2268 if (sched_pressure_p)
2269 fprintf (sched_dump, ")");
2271 fprintf (sched_dump, "\n");
2274 /* Search INSN for REG_SAVE_NOTE notes and convert them back into insn
2275 NOTEs. This is used for NOTE_INSN_EPILOGUE_BEG, so that sched-ebb
2276 replaces the epilogue note in the correct basic block. */
2277 void
2278 reemit_notes (rtx insn)
2280 rtx note, last = insn;
2282 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2284 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
2286 enum insn_note note_type = (enum insn_note) INTVAL (XEXP (note, 0));
2288 last = emit_note_before (note_type, last);
2289 remove_note (insn, note);
2294 /* Move INSN. Reemit notes if needed. Update CFG, if needed. */
2295 static void
2296 move_insn (rtx insn, rtx last, rtx nt)
2298 if (PREV_INSN (insn) != last)
2300 basic_block bb;
2301 rtx note;
2302 int jump_p = 0;
2304 bb = BLOCK_FOR_INSN (insn);
2306 /* BB_HEAD is either LABEL or NOTE. */
2307 gcc_assert (BB_HEAD (bb) != insn);
2309 if (BB_END (bb) == insn)
2310 /* If this is last instruction in BB, move end marker one
2311 instruction up. */
2313 /* Jumps are always placed at the end of basic block. */
2314 jump_p = control_flow_insn_p (insn);
2316 gcc_assert (!jump_p
2317 || ((common_sched_info->sched_pass_id == SCHED_RGN_PASS)
2318 && IS_SPECULATION_BRANCHY_CHECK_P (insn))
2319 || (common_sched_info->sched_pass_id
2320 == SCHED_EBB_PASS));
2322 gcc_assert (BLOCK_FOR_INSN (PREV_INSN (insn)) == bb);
2324 BB_END (bb) = PREV_INSN (insn);
2327 gcc_assert (BB_END (bb) != last);
2329 if (jump_p)
2330 /* We move the block note along with jump. */
2332 gcc_assert (nt);
2334 note = NEXT_INSN (insn);
2335 while (NOTE_NOT_BB_P (note) && note != nt)
2336 note = NEXT_INSN (note);
2338 if (note != nt
2339 && (LABEL_P (note)
2340 || BARRIER_P (note)))
2341 note = NEXT_INSN (note);
2343 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
2345 else
2346 note = insn;
2348 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (note);
2349 PREV_INSN (NEXT_INSN (note)) = PREV_INSN (insn);
2351 NEXT_INSN (note) = NEXT_INSN (last);
2352 PREV_INSN (NEXT_INSN (last)) = note;
2354 NEXT_INSN (last) = insn;
2355 PREV_INSN (insn) = last;
2357 bb = BLOCK_FOR_INSN (last);
2359 if (jump_p)
2361 fix_jump_move (insn);
2363 if (BLOCK_FOR_INSN (insn) != bb)
2364 move_block_after_check (insn);
2366 gcc_assert (BB_END (bb) == last);
2369 df_insn_change_bb (insn, bb);
2371 /* Update BB_END, if needed. */
2372 if (BB_END (bb) == last)
2373 BB_END (bb) = insn;
2376 SCHED_GROUP_P (insn) = 0;
2379 /* Return true if scheduling INSN will finish current clock cycle. */
2380 static bool
2381 insn_finishes_cycle_p (rtx insn)
2383 if (SCHED_GROUP_P (insn))
2384 /* After issuing INSN, rest of the sched_group will be forced to issue
2385 in order. Don't make any plans for the rest of cycle. */
2386 return true;
2388 /* Finishing the block will, apparently, finish the cycle. */
2389 if (current_sched_info->insn_finishes_block_p
2390 && current_sched_info->insn_finishes_block_p (insn))
2391 return true;
2393 return false;
2396 /* The following structure describe an entry of the stack of choices. */
2397 struct choice_entry
2399 /* Ordinal number of the issued insn in the ready queue. */
2400 int index;
2401 /* The number of the rest insns whose issues we should try. */
2402 int rest;
2403 /* The number of issued essential insns. */
2404 int n;
2405 /* State after issuing the insn. */
2406 state_t state;
2409 /* The following array is used to implement a stack of choices used in
2410 function max_issue. */
2411 static struct choice_entry *choice_stack;
2413 /* The following variable value is number of essential insns issued on
2414 the current cycle. An insn is essential one if it changes the
2415 processors state. */
2416 int cycle_issued_insns;
2418 /* This holds the value of the target dfa_lookahead hook. */
2419 int dfa_lookahead;
2421 /* The following variable value is maximal number of tries of issuing
2422 insns for the first cycle multipass insn scheduling. We define
2423 this value as constant*(DFA_LOOKAHEAD**ISSUE_RATE). We would not
2424 need this constraint if all real insns (with non-negative codes)
2425 had reservations because in this case the algorithm complexity is
2426 O(DFA_LOOKAHEAD**ISSUE_RATE). Unfortunately, the dfa descriptions
2427 might be incomplete and such insn might occur. For such
2428 descriptions, the complexity of algorithm (without the constraint)
2429 could achieve DFA_LOOKAHEAD ** N , where N is the queue length. */
2430 static int max_lookahead_tries;
2432 /* The following value is value of hook
2433 `first_cycle_multipass_dfa_lookahead' at the last call of
2434 `max_issue'. */
2435 static int cached_first_cycle_multipass_dfa_lookahead = 0;
2437 /* The following value is value of `issue_rate' at the last call of
2438 `sched_init'. */
2439 static int cached_issue_rate = 0;
2441 /* The following function returns maximal (or close to maximal) number
2442 of insns which can be issued on the same cycle and one of which
2443 insns is insns with the best rank (the first insn in READY). To
2444 make this function tries different samples of ready insns. READY
2445 is current queue `ready'. Global array READY_TRY reflects what
2446 insns are already issued in this try. MAX_POINTS is the sum of points
2447 of all instructions in READY. The function stops immediately,
2448 if it reached the such a solution, that all instruction can be issued.
2449 INDEX will contain index of the best insn in READY. The following
2450 function is used only for first cycle multipass scheduling.
2452 PRIVILEGED_N >= 0
2454 This function expects recognized insns only. All USEs,
2455 CLOBBERs, etc must be filtered elsewhere. */
2457 max_issue (struct ready_list *ready, int privileged_n, state_t state,
2458 int *index)
2460 int n, i, all, n_ready, best, delay, tries_num, max_points;
2461 int more_issue;
2462 struct choice_entry *top;
2463 rtx insn;
2465 n_ready = ready->n_ready;
2466 gcc_assert (dfa_lookahead >= 1 && privileged_n >= 0
2467 && privileged_n <= n_ready);
2469 /* Init MAX_LOOKAHEAD_TRIES. */
2470 if (cached_first_cycle_multipass_dfa_lookahead != dfa_lookahead)
2472 cached_first_cycle_multipass_dfa_lookahead = dfa_lookahead;
2473 max_lookahead_tries = 100;
2474 for (i = 0; i < issue_rate; i++)
2475 max_lookahead_tries *= dfa_lookahead;
2478 /* Init max_points. */
2479 max_points = 0;
2480 more_issue = issue_rate - cycle_issued_insns;
2482 /* ??? We used to assert here that we never issue more insns than issue_rate.
2483 However, some targets (e.g. MIPS/SB1) claim lower issue rate than can be
2484 achieved to get better performance. Until these targets are fixed to use
2485 scheduler hooks to manipulate insns priority instead, the assert should
2486 be disabled.
2488 gcc_assert (more_issue >= 0); */
2490 for (i = 0; i < n_ready; i++)
2491 if (!ready_try [i])
2493 if (more_issue-- > 0)
2494 max_points += ISSUE_POINTS (ready_element (ready, i));
2495 else
2496 break;
2499 /* The number of the issued insns in the best solution. */
2500 best = 0;
2502 top = choice_stack;
2504 /* Set initial state of the search. */
2505 memcpy (top->state, state, dfa_state_size);
2506 top->rest = dfa_lookahead;
2507 top->n = 0;
2509 /* Count the number of the insns to search among. */
2510 for (all = i = 0; i < n_ready; i++)
2511 if (!ready_try [i])
2512 all++;
2514 /* I is the index of the insn to try next. */
2515 i = 0;
2516 tries_num = 0;
2517 for (;;)
2519 if (/* If we've reached a dead end or searched enough of what we have
2520 been asked... */
2521 top->rest == 0
2522 /* Or have nothing else to try. */
2523 || i >= n_ready)
2525 /* ??? (... || i == n_ready). */
2526 gcc_assert (i <= n_ready);
2528 if (top == choice_stack)
2529 break;
2531 if (best < top - choice_stack)
2533 if (privileged_n)
2535 n = privileged_n;
2536 /* Try to find issued privileged insn. */
2537 while (n && !ready_try[--n]);
2540 if (/* If all insns are equally good... */
2541 privileged_n == 0
2542 /* Or a privileged insn will be issued. */
2543 || ready_try[n])
2544 /* Then we have a solution. */
2546 best = top - choice_stack;
2547 /* This is the index of the insn issued first in this
2548 solution. */
2549 *index = choice_stack [1].index;
2550 if (top->n == max_points || best == all)
2551 break;
2555 /* Set ready-list index to point to the last insn
2556 ('i++' below will advance it to the next insn). */
2557 i = top->index;
2559 /* Backtrack. */
2560 ready_try [i] = 0;
2561 top--;
2562 memcpy (state, top->state, dfa_state_size);
2564 else if (!ready_try [i])
2566 tries_num++;
2567 if (tries_num > max_lookahead_tries)
2568 break;
2569 insn = ready_element (ready, i);
2570 delay = state_transition (state, insn);
2571 if (delay < 0)
2573 if (state_dead_lock_p (state)
2574 || insn_finishes_cycle_p (insn))
2575 /* We won't issue any more instructions in the next
2576 choice_state. */
2577 top->rest = 0;
2578 else
2579 top->rest--;
2581 n = top->n;
2582 if (memcmp (top->state, state, dfa_state_size) != 0)
2583 n += ISSUE_POINTS (insn);
2585 /* Advance to the next choice_entry. */
2586 top++;
2587 /* Initialize it. */
2588 top->rest = dfa_lookahead;
2589 top->index = i;
2590 top->n = n;
2591 memcpy (top->state, state, dfa_state_size);
2593 ready_try [i] = 1;
2594 i = -1;
2598 /* Increase ready-list index. */
2599 i++;
2602 /* Restore the original state of the DFA. */
2603 memcpy (state, choice_stack->state, dfa_state_size);
2605 return best;
2608 /* The following function chooses insn from READY and modifies
2609 READY. The following function is used only for first
2610 cycle multipass scheduling.
2611 Return:
2612 -1 if cycle should be advanced,
2613 0 if INSN_PTR is set to point to the desirable insn,
2614 1 if choose_ready () should be restarted without advancing the cycle. */
2615 static int
2616 choose_ready (struct ready_list *ready, rtx *insn_ptr)
2618 int lookahead;
2620 if (dbg_cnt (sched_insn) == false)
2622 rtx insn;
2624 insn = next_nonnote_insn (last_scheduled_insn);
2626 if (QUEUE_INDEX (insn) == QUEUE_READY)
2627 /* INSN is in the ready_list. */
2629 ready_remove_insn (insn);
2630 *insn_ptr = insn;
2631 return 0;
2634 /* INSN is in the queue. Advance cycle to move it to the ready list. */
2635 return -1;
2638 lookahead = 0;
2640 if (targetm.sched.first_cycle_multipass_dfa_lookahead)
2641 lookahead = targetm.sched.first_cycle_multipass_dfa_lookahead ();
2642 if (lookahead <= 0 || SCHED_GROUP_P (ready_element (ready, 0))
2643 || DEBUG_INSN_P (ready_element (ready, 0)))
2645 *insn_ptr = ready_remove_first (ready);
2646 return 0;
2648 else
2650 /* Try to choose the better insn. */
2651 int index = 0, i, n;
2652 rtx insn;
2653 int try_data = 1, try_control = 1;
2654 ds_t ts;
2656 insn = ready_element (ready, 0);
2657 if (INSN_CODE (insn) < 0)
2659 *insn_ptr = ready_remove_first (ready);
2660 return 0;
2663 if (spec_info
2664 && spec_info->flags & (PREFER_NON_DATA_SPEC
2665 | PREFER_NON_CONTROL_SPEC))
2667 for (i = 0, n = ready->n_ready; i < n; i++)
2669 rtx x;
2670 ds_t s;
2672 x = ready_element (ready, i);
2673 s = TODO_SPEC (x);
2675 if (spec_info->flags & PREFER_NON_DATA_SPEC
2676 && !(s & DATA_SPEC))
2678 try_data = 0;
2679 if (!(spec_info->flags & PREFER_NON_CONTROL_SPEC)
2680 || !try_control)
2681 break;
2684 if (spec_info->flags & PREFER_NON_CONTROL_SPEC
2685 && !(s & CONTROL_SPEC))
2687 try_control = 0;
2688 if (!(spec_info->flags & PREFER_NON_DATA_SPEC) || !try_data)
2689 break;
2694 ts = TODO_SPEC (insn);
2695 if ((ts & SPECULATIVE)
2696 && (((!try_data && (ts & DATA_SPEC))
2697 || (!try_control && (ts & CONTROL_SPEC)))
2698 || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard_spec
2699 && !targetm.sched
2700 .first_cycle_multipass_dfa_lookahead_guard_spec (insn))))
2701 /* Discard speculative instruction that stands first in the ready
2702 list. */
2704 change_queue_index (insn, 1);
2705 return 1;
2708 ready_try[0] = 0;
2710 for (i = 1; i < ready->n_ready; i++)
2712 insn = ready_element (ready, i);
2714 ready_try [i]
2715 = ((!try_data && (TODO_SPEC (insn) & DATA_SPEC))
2716 || (!try_control && (TODO_SPEC (insn) & CONTROL_SPEC)));
2719 /* Let the target filter the search space. */
2720 for (i = 1; i < ready->n_ready; i++)
2721 if (!ready_try[i])
2723 insn = ready_element (ready, i);
2725 #ifdef ENABLE_CHECKING
2726 /* If this insn is recognizable we should have already
2727 recognized it earlier.
2728 ??? Not very clear where this is supposed to be done.
2729 See dep_cost_1. */
2730 gcc_assert (INSN_CODE (insn) >= 0
2731 || recog_memoized (insn) < 0);
2732 #endif
2734 ready_try [i]
2735 = (/* INSN_CODE check can be omitted here as it is also done later
2736 in max_issue (). */
2737 INSN_CODE (insn) < 0
2738 || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard
2739 && !targetm.sched.first_cycle_multipass_dfa_lookahead_guard
2740 (insn)));
2743 if (max_issue (ready, 1, curr_state, &index) == 0)
2745 *insn_ptr = ready_remove_first (ready);
2746 if (sched_verbose >= 4)
2747 fprintf (sched_dump, ";;\t\tChosen insn (but can't issue) : %s \n",
2748 (*current_sched_info->print_insn) (*insn_ptr, 0));
2749 return 0;
2751 else
2753 if (sched_verbose >= 4)
2754 fprintf (sched_dump, ";;\t\tChosen insn : %s\n",
2755 (*current_sched_info->print_insn)
2756 (ready_element (ready, index), 0));
2758 *insn_ptr = ready_remove (ready, index);
2759 return 0;
2764 /* Use forward list scheduling to rearrange insns of block pointed to by
2765 TARGET_BB, possibly bringing insns from subsequent blocks in the same
2766 region. */
2768 void
2769 schedule_block (basic_block *target_bb)
2771 int i, first_cycle_insn_p;
2772 int can_issue_more;
2773 state_t temp_state = NULL; /* It is used for multipass scheduling. */
2774 int sort_p, advance, start_clock_var;
2776 /* Head/tail info for this block. */
2777 rtx prev_head = current_sched_info->prev_head;
2778 rtx next_tail = current_sched_info->next_tail;
2779 rtx head = NEXT_INSN (prev_head);
2780 rtx tail = PREV_INSN (next_tail);
2782 /* We used to have code to avoid getting parameters moved from hard
2783 argument registers into pseudos.
2785 However, it was removed when it proved to be of marginal benefit
2786 and caused problems because schedule_block and compute_forward_dependences
2787 had different notions of what the "head" insn was. */
2789 gcc_assert (head != tail || INSN_P (head));
2791 haifa_recovery_bb_recently_added_p = false;
2793 /* Debug info. */
2794 if (sched_verbose)
2795 dump_new_block_header (0, *target_bb, head, tail);
2797 state_reset (curr_state);
2799 /* Clear the ready list. */
2800 ready.first = ready.veclen - 1;
2801 ready.n_ready = 0;
2802 ready.n_debug = 0;
2804 /* It is used for first cycle multipass scheduling. */
2805 temp_state = alloca (dfa_state_size);
2807 if (targetm.sched.init)
2808 targetm.sched.init (sched_dump, sched_verbose, ready.veclen);
2810 /* We start inserting insns after PREV_HEAD. */
2811 last_scheduled_insn = prev_head;
2813 gcc_assert ((NOTE_P (last_scheduled_insn)
2814 || BOUNDARY_DEBUG_INSN_P (last_scheduled_insn))
2815 && BLOCK_FOR_INSN (last_scheduled_insn) == *target_bb);
2817 /* Initialize INSN_QUEUE. Q_SIZE is the total number of insns in the
2818 queue. */
2819 q_ptr = 0;
2820 q_size = 0;
2822 insn_queue = XALLOCAVEC (rtx, max_insn_queue_index + 1);
2823 memset (insn_queue, 0, (max_insn_queue_index + 1) * sizeof (rtx));
2825 /* Start just before the beginning of time. */
2826 clock_var = -1;
2828 /* We need queue and ready lists and clock_var be initialized
2829 in try_ready () (which is called through init_ready_list ()). */
2830 (*current_sched_info->init_ready_list) ();
2832 /* The algorithm is O(n^2) in the number of ready insns at any given
2833 time in the worst case. Before reload we are more likely to have
2834 big lists so truncate them to a reasonable size. */
2835 if (!reload_completed
2836 && ready.n_ready - ready.n_debug > MAX_SCHED_READY_INSNS)
2838 ready_sort (&ready);
2840 /* Find first free-standing insn past MAX_SCHED_READY_INSNS.
2841 If there are debug insns, we know they're first. */
2842 for (i = MAX_SCHED_READY_INSNS + ready.n_debug; i < ready.n_ready; i++)
2843 if (!SCHED_GROUP_P (ready_element (&ready, i)))
2844 break;
2846 if (sched_verbose >= 2)
2848 fprintf (sched_dump,
2849 ";;\t\tReady list on entry: %d insns\n", ready.n_ready);
2850 fprintf (sched_dump,
2851 ";;\t\t before reload => truncated to %d insns\n", i);
2854 /* Delay all insns past it for 1 cycle. If debug counter is
2855 activated make an exception for the insn right after
2856 last_scheduled_insn. */
2858 rtx skip_insn;
2860 if (dbg_cnt (sched_insn) == false)
2861 skip_insn = next_nonnote_insn (last_scheduled_insn);
2862 else
2863 skip_insn = NULL_RTX;
2865 while (i < ready.n_ready)
2867 rtx insn;
2869 insn = ready_remove (&ready, i);
2871 if (insn != skip_insn)
2872 queue_insn (insn, 1);
2877 /* Now we can restore basic block notes and maintain precise cfg. */
2878 restore_bb_notes (*target_bb);
2880 last_clock_var = -1;
2882 advance = 0;
2884 sort_p = TRUE;
2885 /* Loop until all the insns in BB are scheduled. */
2886 while ((*current_sched_info->schedule_more_p) ())
2890 start_clock_var = clock_var;
2892 clock_var++;
2894 advance_one_cycle ();
2896 /* Add to the ready list all pending insns that can be issued now.
2897 If there are no ready insns, increment clock until one
2898 is ready and add all pending insns at that point to the ready
2899 list. */
2900 queue_to_ready (&ready);
2902 gcc_assert (ready.n_ready);
2904 if (sched_verbose >= 2)
2906 fprintf (sched_dump, ";;\t\tReady list after queue_to_ready: ");
2907 debug_ready_list (&ready);
2909 advance -= clock_var - start_clock_var;
2911 while (advance > 0);
2913 if (sort_p)
2915 /* Sort the ready list based on priority. */
2916 ready_sort (&ready);
2918 if (sched_verbose >= 2)
2920 fprintf (sched_dump, ";;\t\tReady list after ready_sort: ");
2921 debug_ready_list (&ready);
2925 /* We don't want md sched reorder to even see debug isns, so put
2926 them out right away. */
2927 if (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0)))
2929 if (control_flow_insn_p (last_scheduled_insn))
2931 *target_bb = current_sched_info->advance_target_bb
2932 (*target_bb, 0);
2934 if (sched_verbose)
2936 rtx x;
2938 x = next_real_insn (last_scheduled_insn);
2939 gcc_assert (x);
2940 dump_new_block_header (1, *target_bb, x, tail);
2943 last_scheduled_insn = bb_note (*target_bb);
2946 while (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0)))
2948 rtx insn = ready_remove_first (&ready);
2949 gcc_assert (DEBUG_INSN_P (insn));
2950 (*current_sched_info->begin_schedule_ready) (insn,
2951 last_scheduled_insn);
2952 move_insn (insn, last_scheduled_insn,
2953 current_sched_info->next_tail);
2954 last_scheduled_insn = insn;
2955 advance = schedule_insn (insn);
2956 gcc_assert (advance == 0);
2957 if (ready.n_ready > 0)
2958 ready_sort (&ready);
2961 if (!ready.n_ready)
2962 continue;
2965 /* Allow the target to reorder the list, typically for
2966 better instruction bundling. */
2967 if (sort_p && targetm.sched.reorder
2968 && (ready.n_ready == 0
2969 || !SCHED_GROUP_P (ready_element (&ready, 0))))
2970 can_issue_more =
2971 targetm.sched.reorder (sched_dump, sched_verbose,
2972 ready_lastpos (&ready),
2973 &ready.n_ready, clock_var);
2974 else
2975 can_issue_more = issue_rate;
2977 first_cycle_insn_p = 1;
2978 cycle_issued_insns = 0;
2979 for (;;)
2981 rtx insn;
2982 int cost;
2983 bool asm_p = false;
2985 if (sched_verbose >= 2)
2987 fprintf (sched_dump, ";;\tReady list (t = %3d): ",
2988 clock_var);
2989 debug_ready_list (&ready);
2990 if (sched_pressure_p)
2991 print_curr_reg_pressure ();
2994 if (ready.n_ready == 0
2995 && can_issue_more
2996 && reload_completed)
2998 /* Allow scheduling insns directly from the queue in case
2999 there's nothing better to do (ready list is empty) but
3000 there are still vacant dispatch slots in the current cycle. */
3001 if (sched_verbose >= 6)
3002 fprintf (sched_dump,";;\t\tSecond chance\n");
3003 memcpy (temp_state, curr_state, dfa_state_size);
3004 if (early_queue_to_ready (temp_state, &ready))
3005 ready_sort (&ready);
3008 if (ready.n_ready == 0
3009 || !can_issue_more
3010 || state_dead_lock_p (curr_state)
3011 || !(*current_sched_info->schedule_more_p) ())
3012 break;
3014 /* Select and remove the insn from the ready list. */
3015 if (sort_p)
3017 int res;
3019 insn = NULL_RTX;
3020 res = choose_ready (&ready, &insn);
3022 if (res < 0)
3023 /* Finish cycle. */
3024 break;
3025 if (res > 0)
3026 /* Restart choose_ready (). */
3027 continue;
3029 gcc_assert (insn != NULL_RTX);
3031 else
3032 insn = ready_remove_first (&ready);
3034 if (sched_pressure_p && INSN_TICK (insn) > clock_var)
3036 ready_add (&ready, insn, true);
3037 advance = 1;
3038 break;
3041 if (targetm.sched.dfa_new_cycle
3042 && targetm.sched.dfa_new_cycle (sched_dump, sched_verbose,
3043 insn, last_clock_var,
3044 clock_var, &sort_p))
3045 /* SORT_P is used by the target to override sorting
3046 of the ready list. This is needed when the target
3047 has modified its internal structures expecting that
3048 the insn will be issued next. As we need the insn
3049 to have the highest priority (so it will be returned by
3050 the ready_remove_first call above), we invoke
3051 ready_add (&ready, insn, true).
3052 But, still, there is one issue: INSN can be later
3053 discarded by scheduler's front end through
3054 current_sched_info->can_schedule_ready_p, hence, won't
3055 be issued next. */
3057 ready_add (&ready, insn, true);
3058 break;
3061 sort_p = TRUE;
3062 memcpy (temp_state, curr_state, dfa_state_size);
3063 if (recog_memoized (insn) < 0)
3065 asm_p = (GET_CODE (PATTERN (insn)) == ASM_INPUT
3066 || asm_noperands (PATTERN (insn)) >= 0);
3067 if (!first_cycle_insn_p && asm_p)
3068 /* This is asm insn which is tried to be issued on the
3069 cycle not first. Issue it on the next cycle. */
3070 cost = 1;
3071 else
3072 /* A USE insn, or something else we don't need to
3073 understand. We can't pass these directly to
3074 state_transition because it will trigger a
3075 fatal error for unrecognizable insns. */
3076 cost = 0;
3078 else if (sched_pressure_p)
3079 cost = 0;
3080 else
3082 cost = state_transition (temp_state, insn);
3083 if (cost < 0)
3084 cost = 0;
3085 else if (cost == 0)
3086 cost = 1;
3089 if (cost >= 1)
3091 queue_insn (insn, cost);
3092 if (SCHED_GROUP_P (insn))
3094 advance = cost;
3095 break;
3098 continue;
3101 if (current_sched_info->can_schedule_ready_p
3102 && ! (*current_sched_info->can_schedule_ready_p) (insn))
3103 /* We normally get here only if we don't want to move
3104 insn from the split block. */
3106 TODO_SPEC (insn) = (TODO_SPEC (insn) & ~SPECULATIVE) | HARD_DEP;
3107 continue;
3110 /* DECISION is made. */
3112 if (TODO_SPEC (insn) & SPECULATIVE)
3113 generate_recovery_code (insn);
3115 if (control_flow_insn_p (last_scheduled_insn)
3116 /* This is used to switch basic blocks by request
3117 from scheduler front-end (actually, sched-ebb.c only).
3118 This is used to process blocks with single fallthru
3119 edge. If succeeding block has jump, it [jump] will try
3120 move at the end of current bb, thus corrupting CFG. */
3121 || current_sched_info->advance_target_bb (*target_bb, insn))
3123 *target_bb = current_sched_info->advance_target_bb
3124 (*target_bb, 0);
3126 if (sched_verbose)
3128 rtx x;
3130 x = next_real_insn (last_scheduled_insn);
3131 gcc_assert (x);
3132 dump_new_block_header (1, *target_bb, x, tail);
3135 last_scheduled_insn = bb_note (*target_bb);
3138 /* Update counters, etc in the scheduler's front end. */
3139 (*current_sched_info->begin_schedule_ready) (insn,
3140 last_scheduled_insn);
3142 move_insn (insn, last_scheduled_insn, current_sched_info->next_tail);
3143 reemit_notes (insn);
3144 last_scheduled_insn = insn;
3146 if (memcmp (curr_state, temp_state, dfa_state_size) != 0)
3148 cycle_issued_insns++;
3149 memcpy (curr_state, temp_state, dfa_state_size);
3152 if (targetm.sched.variable_issue)
3153 can_issue_more =
3154 targetm.sched.variable_issue (sched_dump, sched_verbose,
3155 insn, can_issue_more);
3156 /* A naked CLOBBER or USE generates no instruction, so do
3157 not count them against the issue rate. */
3158 else if (GET_CODE (PATTERN (insn)) != USE
3159 && GET_CODE (PATTERN (insn)) != CLOBBER)
3160 can_issue_more--;
3161 advance = schedule_insn (insn);
3163 /* After issuing an asm insn we should start a new cycle. */
3164 if (advance == 0 && asm_p)
3165 advance = 1;
3166 if (advance != 0)
3167 break;
3169 first_cycle_insn_p = 0;
3171 /* Sort the ready list based on priority. This must be
3172 redone here, as schedule_insn may have readied additional
3173 insns that will not be sorted correctly. */
3174 if (ready.n_ready > 0)
3175 ready_sort (&ready);
3177 /* Quickly go through debug insns such that md sched
3178 reorder2 doesn't have to deal with debug insns. */
3179 if (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0))
3180 && (*current_sched_info->schedule_more_p) ())
3182 if (control_flow_insn_p (last_scheduled_insn))
3184 *target_bb = current_sched_info->advance_target_bb
3185 (*target_bb, 0);
3187 if (sched_verbose)
3189 rtx x;
3191 x = next_real_insn (last_scheduled_insn);
3192 gcc_assert (x);
3193 dump_new_block_header (1, *target_bb, x, tail);
3196 last_scheduled_insn = bb_note (*target_bb);
3199 while (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0)))
3201 insn = ready_remove_first (&ready);
3202 gcc_assert (DEBUG_INSN_P (insn));
3203 (*current_sched_info->begin_schedule_ready)
3204 (insn, last_scheduled_insn);
3205 move_insn (insn, last_scheduled_insn,
3206 current_sched_info->next_tail);
3207 advance = schedule_insn (insn);
3208 last_scheduled_insn = insn;
3209 gcc_assert (advance == 0);
3210 if (ready.n_ready > 0)
3211 ready_sort (&ready);
3215 if (targetm.sched.reorder2
3216 && (ready.n_ready == 0
3217 || !SCHED_GROUP_P (ready_element (&ready, 0))))
3219 can_issue_more =
3220 targetm.sched.reorder2 (sched_dump, sched_verbose,
3221 ready.n_ready
3222 ? ready_lastpos (&ready) : NULL,
3223 &ready.n_ready, clock_var);
3228 /* Debug info. */
3229 if (sched_verbose)
3231 fprintf (sched_dump, ";;\tReady list (final): ");
3232 debug_ready_list (&ready);
3235 if (current_sched_info->queue_must_finish_empty)
3236 /* Sanity check -- queue must be empty now. Meaningless if region has
3237 multiple bbs. */
3238 gcc_assert (!q_size && !ready.n_ready && !ready.n_debug);
3239 else
3241 /* We must maintain QUEUE_INDEX between blocks in region. */
3242 for (i = ready.n_ready - 1; i >= 0; i--)
3244 rtx x;
3246 x = ready_element (&ready, i);
3247 QUEUE_INDEX (x) = QUEUE_NOWHERE;
3248 TODO_SPEC (x) = (TODO_SPEC (x) & ~SPECULATIVE) | HARD_DEP;
3251 if (q_size)
3252 for (i = 0; i <= max_insn_queue_index; i++)
3254 rtx link;
3255 for (link = insn_queue[i]; link; link = XEXP (link, 1))
3257 rtx x;
3259 x = XEXP (link, 0);
3260 QUEUE_INDEX (x) = QUEUE_NOWHERE;
3261 TODO_SPEC (x) = (TODO_SPEC (x) & ~SPECULATIVE) | HARD_DEP;
3263 free_INSN_LIST_list (&insn_queue[i]);
3267 if (sched_verbose)
3268 fprintf (sched_dump, ";; total time = %d\n", clock_var);
3270 if (!current_sched_info->queue_must_finish_empty
3271 || haifa_recovery_bb_recently_added_p)
3273 /* INSN_TICK (minimum clock tick at which the insn becomes
3274 ready) may be not correct for the insn in the subsequent
3275 blocks of the region. We should use a correct value of
3276 `clock_var' or modify INSN_TICK. It is better to keep
3277 clock_var value equal to 0 at the start of a basic block.
3278 Therefore we modify INSN_TICK here. */
3279 fix_inter_tick (NEXT_INSN (prev_head), last_scheduled_insn);
3282 if (targetm.sched.finish)
3284 targetm.sched.finish (sched_dump, sched_verbose);
3285 /* Target might have added some instructions to the scheduled block
3286 in its md_finish () hook. These new insns don't have any data
3287 initialized and to identify them we extend h_i_d so that they'll
3288 get zero luids. */
3289 sched_init_luids (NULL, NULL, NULL, NULL);
3292 if (sched_verbose)
3293 fprintf (sched_dump, ";; new head = %d\n;; new tail = %d\n\n",
3294 INSN_UID (head), INSN_UID (tail));
3296 /* Update head/tail boundaries. */
3297 head = NEXT_INSN (prev_head);
3298 tail = last_scheduled_insn;
3300 head = restore_other_notes (head, NULL);
3302 current_sched_info->head = head;
3303 current_sched_info->tail = tail;
3306 /* Set_priorities: compute priority of each insn in the block. */
3309 set_priorities (rtx head, rtx tail)
3311 rtx insn;
3312 int n_insn;
3313 int sched_max_insns_priority =
3314 current_sched_info->sched_max_insns_priority;
3315 rtx prev_head;
3317 if (head == tail && (! INSN_P (head) || BOUNDARY_DEBUG_INSN_P (head)))
3318 gcc_unreachable ();
3320 n_insn = 0;
3322 prev_head = PREV_INSN (head);
3323 for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
3325 if (!INSN_P (insn))
3326 continue;
3328 n_insn++;
3329 (void) priority (insn);
3331 gcc_assert (INSN_PRIORITY_KNOWN (insn));
3333 sched_max_insns_priority = MAX (sched_max_insns_priority,
3334 INSN_PRIORITY (insn));
3337 current_sched_info->sched_max_insns_priority = sched_max_insns_priority;
3339 return n_insn;
3342 /* Set dump and sched_verbose for the desired debugging output. If no
3343 dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
3344 For -fsched-verbose=N, N>=10, print everything to stderr. */
3345 void
3346 setup_sched_dump (void)
3348 sched_verbose = sched_verbose_param;
3349 if (sched_verbose_param == 0 && dump_file)
3350 sched_verbose = 1;
3351 sched_dump = ((sched_verbose_param >= 10 || !dump_file)
3352 ? stderr : dump_file);
3355 /* Initialize some global state for the scheduler. This function works
3356 with the common data shared between all the schedulers. It is called
3357 from the scheduler specific initialization routine. */
3359 void
3360 sched_init (void)
3362 /* Disable speculative loads in their presence if cc0 defined. */
3363 #ifdef HAVE_cc0
3364 flag_schedule_speculative_load = 0;
3365 #endif
3367 sched_pressure_p = (flag_sched_pressure && ! reload_completed
3368 && common_sched_info->sched_pass_id == SCHED_RGN_PASS);
3369 if (sched_pressure_p)
3370 ira_setup_eliminable_regset ();
3372 /* Initialize SPEC_INFO. */
3373 if (targetm.sched.set_sched_flags)
3375 spec_info = &spec_info_var;
3376 targetm.sched.set_sched_flags (spec_info);
3378 if (spec_info->mask != 0)
3380 spec_info->data_weakness_cutoff =
3381 (PARAM_VALUE (PARAM_SCHED_SPEC_PROB_CUTOFF) * MAX_DEP_WEAK) / 100;
3382 spec_info->control_weakness_cutoff =
3383 (PARAM_VALUE (PARAM_SCHED_SPEC_PROB_CUTOFF)
3384 * REG_BR_PROB_BASE) / 100;
3386 else
3387 /* So we won't read anything accidentally. */
3388 spec_info = NULL;
3391 else
3392 /* So we won't read anything accidentally. */
3393 spec_info = 0;
3395 /* Initialize issue_rate. */
3396 if (targetm.sched.issue_rate)
3397 issue_rate = targetm.sched.issue_rate ();
3398 else
3399 issue_rate = 1;
3401 if (cached_issue_rate != issue_rate)
3403 cached_issue_rate = issue_rate;
3404 /* To invalidate max_lookahead_tries: */
3405 cached_first_cycle_multipass_dfa_lookahead = 0;
3408 if (targetm.sched.first_cycle_multipass_dfa_lookahead)
3409 dfa_lookahead = targetm.sched.first_cycle_multipass_dfa_lookahead ();
3410 else
3411 dfa_lookahead = 0;
3413 if (targetm.sched.init_dfa_pre_cycle_insn)
3414 targetm.sched.init_dfa_pre_cycle_insn ();
3416 if (targetm.sched.init_dfa_post_cycle_insn)
3417 targetm.sched.init_dfa_post_cycle_insn ();
3419 dfa_start ();
3420 dfa_state_size = state_size ();
3422 init_alias_analysis ();
3424 df_set_flags (DF_LR_RUN_DCE);
3425 df_note_add_problem ();
3427 /* More problems needed for interloop dep calculation in SMS. */
3428 if (common_sched_info->sched_pass_id == SCHED_SMS_PASS)
3430 df_rd_add_problem ();
3431 df_chain_add_problem (DF_DU_CHAIN + DF_UD_CHAIN);
3434 df_analyze ();
3436 /* Do not run DCE after reload, as this can kill nops inserted
3437 by bundling. */
3438 if (reload_completed)
3439 df_clear_flags (DF_LR_RUN_DCE);
3441 regstat_compute_calls_crossed ();
3443 if (targetm.sched.init_global)
3444 targetm.sched.init_global (sched_dump, sched_verbose, get_max_uid () + 1);
3446 if (sched_pressure_p)
3448 int i, max_regno = max_reg_num ();
3450 ira_set_pseudo_classes (sched_verbose ? sched_dump : NULL);
3451 sched_regno_cover_class
3452 = (enum reg_class *) xmalloc (max_regno * sizeof (enum reg_class));
3453 for (i = 0; i < max_regno; i++)
3454 sched_regno_cover_class[i]
3455 = (i < FIRST_PSEUDO_REGISTER
3456 ? ira_class_translate[REGNO_REG_CLASS (i)]
3457 : reg_cover_class (i));
3458 curr_reg_live = BITMAP_ALLOC (NULL);
3459 saved_reg_live = BITMAP_ALLOC (NULL);
3460 region_ref_regs = BITMAP_ALLOC (NULL);
3463 curr_state = xmalloc (dfa_state_size);
3466 static void haifa_init_only_bb (basic_block, basic_block);
3468 /* Initialize data structures specific to the Haifa scheduler. */
3469 void
3470 haifa_sched_init (void)
3472 setup_sched_dump ();
3473 sched_init ();
3475 if (spec_info != NULL)
3477 sched_deps_info->use_deps_list = 1;
3478 sched_deps_info->generate_spec_deps = 1;
3481 /* Initialize luids, dependency caches, target and h_i_d for the
3482 whole function. */
3484 bb_vec_t bbs = VEC_alloc (basic_block, heap, n_basic_blocks);
3485 basic_block bb;
3487 sched_init_bbs ();
3489 FOR_EACH_BB (bb)
3490 VEC_quick_push (basic_block, bbs, bb);
3491 sched_init_luids (bbs, NULL, NULL, NULL);
3492 sched_deps_init (true);
3493 sched_extend_target ();
3494 haifa_init_h_i_d (bbs, NULL, NULL, NULL);
3496 VEC_free (basic_block, heap, bbs);
3499 sched_init_only_bb = haifa_init_only_bb;
3500 sched_split_block = sched_split_block_1;
3501 sched_create_empty_bb = sched_create_empty_bb_1;
3502 haifa_recovery_bb_ever_added_p = false;
3504 #ifdef ENABLE_CHECKING
3505 /* This is used preferably for finding bugs in check_cfg () itself.
3506 We must call sched_bbs_init () before check_cfg () because check_cfg ()
3507 assumes that the last insn in the last bb has a non-null successor. */
3508 check_cfg (0, 0);
3509 #endif
3511 nr_begin_data = nr_begin_control = nr_be_in_data = nr_be_in_control = 0;
3512 before_recovery = 0;
3513 after_recovery = 0;
3516 /* Finish work with the data specific to the Haifa scheduler. */
3517 void
3518 haifa_sched_finish (void)
3520 sched_create_empty_bb = NULL;
3521 sched_split_block = NULL;
3522 sched_init_only_bb = NULL;
3524 if (spec_info && spec_info->dump)
3526 char c = reload_completed ? 'a' : 'b';
3528 fprintf (spec_info->dump,
3529 ";; %s:\n", current_function_name ());
3531 fprintf (spec_info->dump,
3532 ";; Procedure %cr-begin-data-spec motions == %d\n",
3533 c, nr_begin_data);
3534 fprintf (spec_info->dump,
3535 ";; Procedure %cr-be-in-data-spec motions == %d\n",
3536 c, nr_be_in_data);
3537 fprintf (spec_info->dump,
3538 ";; Procedure %cr-begin-control-spec motions == %d\n",
3539 c, nr_begin_control);
3540 fprintf (spec_info->dump,
3541 ";; Procedure %cr-be-in-control-spec motions == %d\n",
3542 c, nr_be_in_control);
3545 /* Finalize h_i_d, dependency caches, and luids for the whole
3546 function. Target will be finalized in md_global_finish (). */
3547 sched_deps_finish ();
3548 sched_finish_luids ();
3549 current_sched_info = NULL;
3550 sched_finish ();
3553 /* Free global data used during insn scheduling. This function works with
3554 the common data shared between the schedulers. */
3556 void
3557 sched_finish (void)
3559 haifa_finish_h_i_d ();
3560 if (sched_pressure_p)
3562 free (sched_regno_cover_class);
3563 BITMAP_FREE (region_ref_regs);
3564 BITMAP_FREE (saved_reg_live);
3565 BITMAP_FREE (curr_reg_live);
3567 free (curr_state);
3569 if (targetm.sched.finish_global)
3570 targetm.sched.finish_global (sched_dump, sched_verbose);
3572 end_alias_analysis ();
3574 regstat_free_calls_crossed ();
3576 dfa_finish ();
3578 #ifdef ENABLE_CHECKING
3579 /* After reload ia64 backend clobbers CFG, so can't check anything. */
3580 if (!reload_completed)
3581 check_cfg (0, 0);
3582 #endif
3585 /* Fix INSN_TICKs of the instructions in the current block as well as
3586 INSN_TICKs of their dependents.
3587 HEAD and TAIL are the begin and the end of the current scheduled block. */
3588 static void
3589 fix_inter_tick (rtx head, rtx tail)
3591 /* Set of instructions with corrected INSN_TICK. */
3592 bitmap_head processed;
3593 /* ??? It is doubtful if we should assume that cycle advance happens on
3594 basic block boundaries. Basically insns that are unconditionally ready
3595 on the start of the block are more preferable then those which have
3596 a one cycle dependency over insn from the previous block. */
3597 int next_clock = clock_var + 1;
3599 bitmap_initialize (&processed, 0);
3601 /* Iterates over scheduled instructions and fix their INSN_TICKs and
3602 INSN_TICKs of dependent instructions, so that INSN_TICKs are consistent
3603 across different blocks. */
3604 for (tail = NEXT_INSN (tail); head != tail; head = NEXT_INSN (head))
3606 if (INSN_P (head))
3608 int tick;
3609 sd_iterator_def sd_it;
3610 dep_t dep;
3612 tick = INSN_TICK (head);
3613 gcc_assert (tick >= MIN_TICK);
3615 /* Fix INSN_TICK of instruction from just scheduled block. */
3616 if (bitmap_set_bit (&processed, INSN_LUID (head)))
3618 tick -= next_clock;
3620 if (tick < MIN_TICK)
3621 tick = MIN_TICK;
3623 INSN_TICK (head) = tick;
3626 FOR_EACH_DEP (head, SD_LIST_RES_FORW, sd_it, dep)
3628 rtx next;
3630 next = DEP_CON (dep);
3631 tick = INSN_TICK (next);
3633 if (tick != INVALID_TICK
3634 /* If NEXT has its INSN_TICK calculated, fix it.
3635 If not - it will be properly calculated from
3636 scratch later in fix_tick_ready. */
3637 && bitmap_set_bit (&processed, INSN_LUID (next)))
3639 tick -= next_clock;
3641 if (tick < MIN_TICK)
3642 tick = MIN_TICK;
3644 if (tick > INTER_TICK (next))
3645 INTER_TICK (next) = tick;
3646 else
3647 tick = INTER_TICK (next);
3649 INSN_TICK (next) = tick;
3654 bitmap_clear (&processed);
3657 static int haifa_speculate_insn (rtx, ds_t, rtx *);
3659 /* Check if NEXT is ready to be added to the ready or queue list.
3660 If "yes", add it to the proper list.
3661 Returns:
3662 -1 - is not ready yet,
3663 0 - added to the ready list,
3664 0 < N - queued for N cycles. */
3666 try_ready (rtx next)
3668 ds_t old_ts, *ts;
3670 ts = &TODO_SPEC (next);
3671 old_ts = *ts;
3673 gcc_assert (!(old_ts & ~(SPECULATIVE | HARD_DEP))
3674 && ((old_ts & HARD_DEP)
3675 || (old_ts & SPECULATIVE)));
3677 if (sd_lists_empty_p (next, SD_LIST_BACK))
3678 /* NEXT has all its dependencies resolved. */
3680 /* Remove HARD_DEP bit from NEXT's status. */
3681 *ts &= ~HARD_DEP;
3683 if (current_sched_info->flags & DO_SPECULATION)
3684 /* Remove all speculative bits from NEXT's status. */
3685 *ts &= ~SPECULATIVE;
3687 else
3689 /* One of the NEXT's dependencies has been resolved.
3690 Recalculate NEXT's status. */
3692 *ts &= ~SPECULATIVE & ~HARD_DEP;
3694 if (sd_lists_empty_p (next, SD_LIST_HARD_BACK))
3695 /* Now we've got NEXT with speculative deps only.
3696 1. Look at the deps to see what we have to do.
3697 2. Check if we can do 'todo'. */
3699 sd_iterator_def sd_it;
3700 dep_t dep;
3701 bool first_p = true;
3703 FOR_EACH_DEP (next, SD_LIST_BACK, sd_it, dep)
3705 ds_t ds = DEP_STATUS (dep) & SPECULATIVE;
3707 if (DEBUG_INSN_P (DEP_PRO (dep))
3708 && !DEBUG_INSN_P (next))
3709 continue;
3711 if (first_p)
3713 first_p = false;
3715 *ts = ds;
3717 else
3718 *ts = ds_merge (*ts, ds);
3721 if (ds_weak (*ts) < spec_info->data_weakness_cutoff)
3722 /* Too few points. */
3723 *ts = (*ts & ~SPECULATIVE) | HARD_DEP;
3725 else
3726 *ts |= HARD_DEP;
3729 if (*ts & HARD_DEP)
3730 gcc_assert (*ts == old_ts
3731 && QUEUE_INDEX (next) == QUEUE_NOWHERE);
3732 else if (current_sched_info->new_ready)
3733 *ts = current_sched_info->new_ready (next, *ts);
3735 /* * if !(old_ts & SPECULATIVE) (e.g. HARD_DEP or 0), then insn might
3736 have its original pattern or changed (speculative) one. This is due
3737 to changing ebb in region scheduling.
3738 * But if (old_ts & SPECULATIVE), then we are pretty sure that insn
3739 has speculative pattern.
3741 We can't assert (!(*ts & HARD_DEP) || *ts == old_ts) here because
3742 control-speculative NEXT could have been discarded by sched-rgn.c
3743 (the same case as when discarded by can_schedule_ready_p ()). */
3745 if ((*ts & SPECULATIVE)
3746 /* If (old_ts == *ts), then (old_ts & SPECULATIVE) and we don't
3747 need to change anything. */
3748 && *ts != old_ts)
3750 int res;
3751 rtx new_pat;
3753 gcc_assert ((*ts & SPECULATIVE) && !(*ts & ~SPECULATIVE));
3755 res = haifa_speculate_insn (next, *ts, &new_pat);
3757 switch (res)
3759 case -1:
3760 /* It would be nice to change DEP_STATUS of all dependences,
3761 which have ((DEP_STATUS & SPECULATIVE) == *ts) to HARD_DEP,
3762 so we won't reanalyze anything. */
3763 *ts = (*ts & ~SPECULATIVE) | HARD_DEP;
3764 break;
3766 case 0:
3767 /* We follow the rule, that every speculative insn
3768 has non-null ORIG_PAT. */
3769 if (!ORIG_PAT (next))
3770 ORIG_PAT (next) = PATTERN (next);
3771 break;
3773 case 1:
3774 if (!ORIG_PAT (next))
3775 /* If we gonna to overwrite the original pattern of insn,
3776 save it. */
3777 ORIG_PAT (next) = PATTERN (next);
3779 haifa_change_pattern (next, new_pat);
3780 break;
3782 default:
3783 gcc_unreachable ();
3787 /* We need to restore pattern only if (*ts == 0), because otherwise it is
3788 either correct (*ts & SPECULATIVE),
3789 or we simply don't care (*ts & HARD_DEP). */
3791 gcc_assert (!ORIG_PAT (next)
3792 || !IS_SPECULATION_BRANCHY_CHECK_P (next));
3794 if (*ts & HARD_DEP)
3796 /* We can't assert (QUEUE_INDEX (next) == QUEUE_NOWHERE) here because
3797 control-speculative NEXT could have been discarded by sched-rgn.c
3798 (the same case as when discarded by can_schedule_ready_p ()). */
3799 /*gcc_assert (QUEUE_INDEX (next) == QUEUE_NOWHERE);*/
3801 change_queue_index (next, QUEUE_NOWHERE);
3802 return -1;
3804 else if (!(*ts & BEGIN_SPEC) && ORIG_PAT (next) && !IS_SPECULATION_CHECK_P (next))
3805 /* We should change pattern of every previously speculative
3806 instruction - and we determine if NEXT was speculative by using
3807 ORIG_PAT field. Except one case - speculation checks have ORIG_PAT
3808 pat too, so skip them. */
3810 haifa_change_pattern (next, ORIG_PAT (next));
3811 ORIG_PAT (next) = 0;
3814 if (sched_verbose >= 2)
3816 int s = TODO_SPEC (next);
3818 fprintf (sched_dump, ";;\t\tdependencies resolved: insn %s",
3819 (*current_sched_info->print_insn) (next, 0));
3821 if (spec_info && spec_info->dump)
3823 if (s & BEGIN_DATA)
3824 fprintf (spec_info->dump, "; data-spec;");
3825 if (s & BEGIN_CONTROL)
3826 fprintf (spec_info->dump, "; control-spec;");
3827 if (s & BE_IN_CONTROL)
3828 fprintf (spec_info->dump, "; in-control-spec;");
3831 fprintf (sched_dump, "\n");
3834 adjust_priority (next);
3836 return fix_tick_ready (next);
3839 /* Calculate INSN_TICK of NEXT and add it to either ready or queue list. */
3840 static int
3841 fix_tick_ready (rtx next)
3843 int tick, delay;
3845 if (!sd_lists_empty_p (next, SD_LIST_RES_BACK))
3847 int full_p;
3848 sd_iterator_def sd_it;
3849 dep_t dep;
3851 tick = INSN_TICK (next);
3852 /* if tick is not equal to INVALID_TICK, then update
3853 INSN_TICK of NEXT with the most recent resolved dependence
3854 cost. Otherwise, recalculate from scratch. */
3855 full_p = (tick == INVALID_TICK);
3857 FOR_EACH_DEP (next, SD_LIST_RES_BACK, sd_it, dep)
3859 rtx pro = DEP_PRO (dep);
3860 int tick1;
3862 gcc_assert (INSN_TICK (pro) >= MIN_TICK);
3864 tick1 = INSN_TICK (pro) + dep_cost (dep);
3865 if (tick1 > tick)
3866 tick = tick1;
3868 if (!full_p)
3869 break;
3872 else
3873 tick = -1;
3875 INSN_TICK (next) = tick;
3877 delay = tick - clock_var;
3878 if (delay <= 0 || sched_pressure_p)
3879 delay = QUEUE_READY;
3881 change_queue_index (next, delay);
3883 return delay;
3886 /* Move NEXT to the proper queue list with (DELAY >= 1),
3887 or add it to the ready list (DELAY == QUEUE_READY),
3888 or remove it from ready and queue lists at all (DELAY == QUEUE_NOWHERE). */
3889 static void
3890 change_queue_index (rtx next, int delay)
3892 int i = QUEUE_INDEX (next);
3894 gcc_assert (QUEUE_NOWHERE <= delay && delay <= max_insn_queue_index
3895 && delay != 0);
3896 gcc_assert (i != QUEUE_SCHEDULED);
3898 if ((delay > 0 && NEXT_Q_AFTER (q_ptr, delay) == i)
3899 || (delay < 0 && delay == i))
3900 /* We have nothing to do. */
3901 return;
3903 /* Remove NEXT from wherever it is now. */
3904 if (i == QUEUE_READY)
3905 ready_remove_insn (next);
3906 else if (i >= 0)
3907 queue_remove (next);
3909 /* Add it to the proper place. */
3910 if (delay == QUEUE_READY)
3911 ready_add (readyp, next, false);
3912 else if (delay >= 1)
3913 queue_insn (next, delay);
3915 if (sched_verbose >= 2)
3917 fprintf (sched_dump, ";;\t\ttick updated: insn %s",
3918 (*current_sched_info->print_insn) (next, 0));
3920 if (delay == QUEUE_READY)
3921 fprintf (sched_dump, " into ready\n");
3922 else if (delay >= 1)
3923 fprintf (sched_dump, " into queue with cost=%d\n", delay);
3924 else
3925 fprintf (sched_dump, " removed from ready or queue lists\n");
3929 static int sched_ready_n_insns = -1;
3931 /* Initialize per region data structures. */
3932 void
3933 sched_extend_ready_list (int new_sched_ready_n_insns)
3935 int i;
3937 if (sched_ready_n_insns == -1)
3938 /* At the first call we need to initialize one more choice_stack
3939 entry. */
3941 i = 0;
3942 sched_ready_n_insns = 0;
3944 else
3945 i = sched_ready_n_insns + 1;
3947 ready.veclen = new_sched_ready_n_insns + issue_rate;
3948 ready.vec = XRESIZEVEC (rtx, ready.vec, ready.veclen);
3950 gcc_assert (new_sched_ready_n_insns >= sched_ready_n_insns);
3952 ready_try = (char *) xrecalloc (ready_try, new_sched_ready_n_insns,
3953 sched_ready_n_insns, sizeof (*ready_try));
3955 /* We allocate +1 element to save initial state in the choice_stack[0]
3956 entry. */
3957 choice_stack = XRESIZEVEC (struct choice_entry, choice_stack,
3958 new_sched_ready_n_insns + 1);
3960 for (; i <= new_sched_ready_n_insns; i++)
3961 choice_stack[i].state = xmalloc (dfa_state_size);
3963 sched_ready_n_insns = new_sched_ready_n_insns;
3966 /* Free per region data structures. */
3967 void
3968 sched_finish_ready_list (void)
3970 int i;
3972 free (ready.vec);
3973 ready.vec = NULL;
3974 ready.veclen = 0;
3976 free (ready_try);
3977 ready_try = NULL;
3979 for (i = 0; i <= sched_ready_n_insns; i++)
3980 free (choice_stack [i].state);
3981 free (choice_stack);
3982 choice_stack = NULL;
3984 sched_ready_n_insns = -1;
3987 static int
3988 haifa_luid_for_non_insn (rtx x)
3990 gcc_assert (NOTE_P (x) || LABEL_P (x));
3992 return 0;
3995 /* Generates recovery code for INSN. */
3996 static void
3997 generate_recovery_code (rtx insn)
3999 if (TODO_SPEC (insn) & BEGIN_SPEC)
4000 begin_speculative_block (insn);
4002 /* Here we have insn with no dependencies to
4003 instructions other then CHECK_SPEC ones. */
4005 if (TODO_SPEC (insn) & BE_IN_SPEC)
4006 add_to_speculative_block (insn);
4009 /* Helper function.
4010 Tries to add speculative dependencies of type FS between instructions
4011 in deps_list L and TWIN. */
4012 static void
4013 process_insn_forw_deps_be_in_spec (rtx insn, rtx twin, ds_t fs)
4015 sd_iterator_def sd_it;
4016 dep_t dep;
4018 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
4020 ds_t ds;
4021 rtx consumer;
4023 consumer = DEP_CON (dep);
4025 ds = DEP_STATUS (dep);
4027 if (/* If we want to create speculative dep. */
4029 /* And we can do that because this is a true dep. */
4030 && (ds & DEP_TYPES) == DEP_TRUE)
4032 gcc_assert (!(ds & BE_IN_SPEC));
4034 if (/* If this dep can be overcome with 'begin speculation'. */
4035 ds & BEGIN_SPEC)
4036 /* Then we have a choice: keep the dep 'begin speculative'
4037 or transform it into 'be in speculative'. */
4039 if (/* In try_ready we assert that if insn once became ready
4040 it can be removed from the ready (or queue) list only
4041 due to backend decision. Hence we can't let the
4042 probability of the speculative dep to decrease. */
4043 ds_weak (ds) <= ds_weak (fs))
4045 ds_t new_ds;
4047 new_ds = (ds & ~BEGIN_SPEC) | fs;
4049 if (/* consumer can 'be in speculative'. */
4050 sched_insn_is_legitimate_for_speculation_p (consumer,
4051 new_ds))
4052 /* Transform it to be in speculative. */
4053 ds = new_ds;
4056 else
4057 /* Mark the dep as 'be in speculative'. */
4058 ds |= fs;
4062 dep_def _new_dep, *new_dep = &_new_dep;
4064 init_dep_1 (new_dep, twin, consumer, DEP_TYPE (dep), ds);
4065 sd_add_dep (new_dep, false);
4070 /* Generates recovery code for BEGIN speculative INSN. */
4071 static void
4072 begin_speculative_block (rtx insn)
4074 if (TODO_SPEC (insn) & BEGIN_DATA)
4075 nr_begin_data++;
4076 if (TODO_SPEC (insn) & BEGIN_CONTROL)
4077 nr_begin_control++;
4079 create_check_block_twin (insn, false);
4081 TODO_SPEC (insn) &= ~BEGIN_SPEC;
4084 static void haifa_init_insn (rtx);
4086 /* Generates recovery code for BE_IN speculative INSN. */
4087 static void
4088 add_to_speculative_block (rtx insn)
4090 ds_t ts;
4091 sd_iterator_def sd_it;
4092 dep_t dep;
4093 rtx twins = NULL;
4094 rtx_vec_t priorities_roots;
4096 ts = TODO_SPEC (insn);
4097 gcc_assert (!(ts & ~BE_IN_SPEC));
4099 if (ts & BE_IN_DATA)
4100 nr_be_in_data++;
4101 if (ts & BE_IN_CONTROL)
4102 nr_be_in_control++;
4104 TODO_SPEC (insn) &= ~BE_IN_SPEC;
4105 gcc_assert (!TODO_SPEC (insn));
4107 DONE_SPEC (insn) |= ts;
4109 /* First we convert all simple checks to branchy. */
4110 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4111 sd_iterator_cond (&sd_it, &dep);)
4113 rtx check = DEP_PRO (dep);
4115 if (IS_SPECULATION_SIMPLE_CHECK_P (check))
4117 create_check_block_twin (check, true);
4119 /* Restart search. */
4120 sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4122 else
4123 /* Continue search. */
4124 sd_iterator_next (&sd_it);
4127 priorities_roots = NULL;
4128 clear_priorities (insn, &priorities_roots);
4130 while (1)
4132 rtx check, twin;
4133 basic_block rec;
4135 /* Get the first backward dependency of INSN. */
4136 sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4137 if (!sd_iterator_cond (&sd_it, &dep))
4138 /* INSN has no backward dependencies left. */
4139 break;
4141 gcc_assert ((DEP_STATUS (dep) & BEGIN_SPEC) == 0
4142 && (DEP_STATUS (dep) & BE_IN_SPEC) != 0
4143 && (DEP_STATUS (dep) & DEP_TYPES) == DEP_TRUE);
4145 check = DEP_PRO (dep);
4147 gcc_assert (!IS_SPECULATION_CHECK_P (check) && !ORIG_PAT (check)
4148 && QUEUE_INDEX (check) == QUEUE_NOWHERE);
4150 rec = BLOCK_FOR_INSN (check);
4152 twin = emit_insn_before (copy_insn (PATTERN (insn)), BB_END (rec));
4153 haifa_init_insn (twin);
4155 sd_copy_back_deps (twin, insn, true);
4157 if (sched_verbose && spec_info->dump)
4158 /* INSN_BB (insn) isn't determined for twin insns yet.
4159 So we can't use current_sched_info->print_insn. */
4160 fprintf (spec_info->dump, ";;\t\tGenerated twin insn : %d/rec%d\n",
4161 INSN_UID (twin), rec->index);
4163 twins = alloc_INSN_LIST (twin, twins);
4165 /* Add dependences between TWIN and all appropriate
4166 instructions from REC. */
4167 FOR_EACH_DEP (insn, SD_LIST_SPEC_BACK, sd_it, dep)
4169 rtx pro = DEP_PRO (dep);
4171 gcc_assert (DEP_TYPE (dep) == REG_DEP_TRUE);
4173 /* INSN might have dependencies from the instructions from
4174 several recovery blocks. At this iteration we process those
4175 producers that reside in REC. */
4176 if (BLOCK_FOR_INSN (pro) == rec)
4178 dep_def _new_dep, *new_dep = &_new_dep;
4180 init_dep (new_dep, pro, twin, REG_DEP_TRUE);
4181 sd_add_dep (new_dep, false);
4185 process_insn_forw_deps_be_in_spec (insn, twin, ts);
4187 /* Remove all dependencies between INSN and insns in REC. */
4188 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4189 sd_iterator_cond (&sd_it, &dep);)
4191 rtx pro = DEP_PRO (dep);
4193 if (BLOCK_FOR_INSN (pro) == rec)
4194 sd_delete_dep (sd_it);
4195 else
4196 sd_iterator_next (&sd_it);
4200 /* We couldn't have added the dependencies between INSN and TWINS earlier
4201 because that would make TWINS appear in the INSN_BACK_DEPS (INSN). */
4202 while (twins)
4204 rtx twin;
4206 twin = XEXP (twins, 0);
4209 dep_def _new_dep, *new_dep = &_new_dep;
4211 init_dep (new_dep, insn, twin, REG_DEP_OUTPUT);
4212 sd_add_dep (new_dep, false);
4215 twin = XEXP (twins, 1);
4216 free_INSN_LIST_node (twins);
4217 twins = twin;
4220 calc_priorities (priorities_roots);
4221 VEC_free (rtx, heap, priorities_roots);
4224 /* Extends and fills with zeros (only the new part) array pointed to by P. */
4225 void *
4226 xrecalloc (void *p, size_t new_nmemb, size_t old_nmemb, size_t size)
4228 gcc_assert (new_nmemb >= old_nmemb);
4229 p = XRESIZEVAR (void, p, new_nmemb * size);
4230 memset (((char *) p) + old_nmemb * size, 0, (new_nmemb - old_nmemb) * size);
4231 return p;
4234 /* Helper function.
4235 Find fallthru edge from PRED. */
4236 edge
4237 find_fallthru_edge (basic_block pred)
4239 edge e;
4240 edge_iterator ei;
4241 basic_block succ;
4243 succ = pred->next_bb;
4244 gcc_assert (succ->prev_bb == pred);
4246 if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
4248 FOR_EACH_EDGE (e, ei, pred->succs)
4249 if (e->flags & EDGE_FALLTHRU)
4251 gcc_assert (e->dest == succ);
4252 return e;
4255 else
4257 FOR_EACH_EDGE (e, ei, succ->preds)
4258 if (e->flags & EDGE_FALLTHRU)
4260 gcc_assert (e->src == pred);
4261 return e;
4265 return NULL;
4268 /* Extend per basic block data structures. */
4269 static void
4270 sched_extend_bb (void)
4272 rtx insn;
4274 /* The following is done to keep current_sched_info->next_tail non null. */
4275 insn = BB_END (EXIT_BLOCK_PTR->prev_bb);
4276 if (NEXT_INSN (insn) == 0
4277 || (!NOTE_P (insn)
4278 && !LABEL_P (insn)
4279 /* Don't emit a NOTE if it would end up before a BARRIER. */
4280 && !BARRIER_P (NEXT_INSN (insn))))
4282 rtx note = emit_note_after (NOTE_INSN_DELETED, insn);
4283 /* Make insn appear outside BB. */
4284 set_block_for_insn (note, NULL);
4285 BB_END (EXIT_BLOCK_PTR->prev_bb) = insn;
4289 /* Init per basic block data structures. */
4290 void
4291 sched_init_bbs (void)
4293 sched_extend_bb ();
4296 /* Initialize BEFORE_RECOVERY variable. */
4297 static void
4298 init_before_recovery (basic_block *before_recovery_ptr)
4300 basic_block last;
4301 edge e;
4303 last = EXIT_BLOCK_PTR->prev_bb;
4304 e = find_fallthru_edge (last);
4306 if (e)
4308 /* We create two basic blocks:
4309 1. Single instruction block is inserted right after E->SRC
4310 and has jump to
4311 2. Empty block right before EXIT_BLOCK.
4312 Between these two blocks recovery blocks will be emitted. */
4314 basic_block single, empty;
4315 rtx x, label;
4317 /* If the fallthrough edge to exit we've found is from the block we've
4318 created before, don't do anything more. */
4319 if (last == after_recovery)
4320 return;
4322 adding_bb_to_current_region_p = false;
4324 single = sched_create_empty_bb (last);
4325 empty = sched_create_empty_bb (single);
4327 /* Add new blocks to the root loop. */
4328 if (current_loops != NULL)
4330 add_bb_to_loop (single, VEC_index (loop_p, current_loops->larray, 0));
4331 add_bb_to_loop (empty, VEC_index (loop_p, current_loops->larray, 0));
4334 single->count = last->count;
4335 empty->count = last->count;
4336 single->frequency = last->frequency;
4337 empty->frequency = last->frequency;
4338 BB_COPY_PARTITION (single, last);
4339 BB_COPY_PARTITION (empty, last);
4341 redirect_edge_succ (e, single);
4342 make_single_succ_edge (single, empty, 0);
4343 make_single_succ_edge (empty, EXIT_BLOCK_PTR,
4344 EDGE_FALLTHRU | EDGE_CAN_FALLTHRU);
4346 label = block_label (empty);
4347 x = emit_jump_insn_after (gen_jump (label), BB_END (single));
4348 JUMP_LABEL (x) = label;
4349 LABEL_NUSES (label)++;
4350 haifa_init_insn (x);
4352 emit_barrier_after (x);
4354 sched_init_only_bb (empty, NULL);
4355 sched_init_only_bb (single, NULL);
4356 sched_extend_bb ();
4358 adding_bb_to_current_region_p = true;
4359 before_recovery = single;
4360 after_recovery = empty;
4362 if (before_recovery_ptr)
4363 *before_recovery_ptr = before_recovery;
4365 if (sched_verbose >= 2 && spec_info->dump)
4366 fprintf (spec_info->dump,
4367 ";;\t\tFixed fallthru to EXIT : %d->>%d->%d->>EXIT\n",
4368 last->index, single->index, empty->index);
4370 else
4371 before_recovery = last;
4374 /* Returns new recovery block. */
4375 basic_block
4376 sched_create_recovery_block (basic_block *before_recovery_ptr)
4378 rtx label;
4379 rtx barrier;
4380 basic_block rec;
4382 haifa_recovery_bb_recently_added_p = true;
4383 haifa_recovery_bb_ever_added_p = true;
4385 init_before_recovery (before_recovery_ptr);
4387 barrier = get_last_bb_insn (before_recovery);
4388 gcc_assert (BARRIER_P (barrier));
4390 label = emit_label_after (gen_label_rtx (), barrier);
4392 rec = create_basic_block (label, label, before_recovery);
4394 /* A recovery block always ends with an unconditional jump. */
4395 emit_barrier_after (BB_END (rec));
4397 if (BB_PARTITION (before_recovery) != BB_UNPARTITIONED)
4398 BB_SET_PARTITION (rec, BB_COLD_PARTITION);
4400 if (sched_verbose && spec_info->dump)
4401 fprintf (spec_info->dump, ";;\t\tGenerated recovery block rec%d\n",
4402 rec->index);
4404 return rec;
4407 /* Create edges: FIRST_BB -> REC; FIRST_BB -> SECOND_BB; REC -> SECOND_BB
4408 and emit necessary jumps. */
4409 void
4410 sched_create_recovery_edges (basic_block first_bb, basic_block rec,
4411 basic_block second_bb)
4413 rtx label;
4414 rtx jump;
4415 int edge_flags;
4417 /* This is fixing of incoming edge. */
4418 /* ??? Which other flags should be specified? */
4419 if (BB_PARTITION (first_bb) != BB_PARTITION (rec))
4420 /* Partition type is the same, if it is "unpartitioned". */
4421 edge_flags = EDGE_CROSSING;
4422 else
4423 edge_flags = 0;
4425 make_edge (first_bb, rec, edge_flags);
4426 label = block_label (second_bb);
4427 jump = emit_jump_insn_after (gen_jump (label), BB_END (rec));
4428 JUMP_LABEL (jump) = label;
4429 LABEL_NUSES (label)++;
4431 if (BB_PARTITION (second_bb) != BB_PARTITION (rec))
4432 /* Partition type is the same, if it is "unpartitioned". */
4434 /* Rewritten from cfgrtl.c. */
4435 if (flag_reorder_blocks_and_partition
4436 && targetm.have_named_sections)
4438 /* We don't need the same note for the check because
4439 any_condjump_p (check) == true. */
4440 add_reg_note (jump, REG_CROSSING_JUMP, NULL_RTX);
4442 edge_flags = EDGE_CROSSING;
4444 else
4445 edge_flags = 0;
4447 make_single_succ_edge (rec, second_bb, edge_flags);
4450 /* This function creates recovery code for INSN. If MUTATE_P is nonzero,
4451 INSN is a simple check, that should be converted to branchy one. */
4452 static void
4453 create_check_block_twin (rtx insn, bool mutate_p)
4455 basic_block rec;
4456 rtx label, check, twin;
4457 ds_t fs;
4458 sd_iterator_def sd_it;
4459 dep_t dep;
4460 dep_def _new_dep, *new_dep = &_new_dep;
4461 ds_t todo_spec;
4463 gcc_assert (ORIG_PAT (insn) != NULL_RTX);
4465 if (!mutate_p)
4466 todo_spec = TODO_SPEC (insn);
4467 else
4469 gcc_assert (IS_SPECULATION_SIMPLE_CHECK_P (insn)
4470 && (TODO_SPEC (insn) & SPECULATIVE) == 0);
4472 todo_spec = CHECK_SPEC (insn);
4475 todo_spec &= SPECULATIVE;
4477 /* Create recovery block. */
4478 if (mutate_p || targetm.sched.needs_block_p (todo_spec))
4480 rec = sched_create_recovery_block (NULL);
4481 label = BB_HEAD (rec);
4483 else
4485 rec = EXIT_BLOCK_PTR;
4486 label = NULL_RTX;
4489 /* Emit CHECK. */
4490 check = targetm.sched.gen_spec_check (insn, label, todo_spec);
4492 if (rec != EXIT_BLOCK_PTR)
4494 /* To have mem_reg alive at the beginning of second_bb,
4495 we emit check BEFORE insn, so insn after splitting
4496 insn will be at the beginning of second_bb, which will
4497 provide us with the correct life information. */
4498 check = emit_jump_insn_before (check, insn);
4499 JUMP_LABEL (check) = label;
4500 LABEL_NUSES (label)++;
4502 else
4503 check = emit_insn_before (check, insn);
4505 /* Extend data structures. */
4506 haifa_init_insn (check);
4508 /* CHECK is being added to current region. Extend ready list. */
4509 gcc_assert (sched_ready_n_insns != -1);
4510 sched_extend_ready_list (sched_ready_n_insns + 1);
4512 if (current_sched_info->add_remove_insn)
4513 current_sched_info->add_remove_insn (insn, 0);
4515 RECOVERY_BLOCK (check) = rec;
4517 if (sched_verbose && spec_info->dump)
4518 fprintf (spec_info->dump, ";;\t\tGenerated check insn : %s\n",
4519 (*current_sched_info->print_insn) (check, 0));
4521 gcc_assert (ORIG_PAT (insn));
4523 /* Initialize TWIN (twin is a duplicate of original instruction
4524 in the recovery block). */
4525 if (rec != EXIT_BLOCK_PTR)
4527 sd_iterator_def sd_it;
4528 dep_t dep;
4530 FOR_EACH_DEP (insn, SD_LIST_RES_BACK, sd_it, dep)
4531 if ((DEP_STATUS (dep) & DEP_OUTPUT) != 0)
4533 struct _dep _dep2, *dep2 = &_dep2;
4535 init_dep (dep2, DEP_PRO (dep), check, REG_DEP_TRUE);
4537 sd_add_dep (dep2, true);
4540 twin = emit_insn_after (ORIG_PAT (insn), BB_END (rec));
4541 haifa_init_insn (twin);
4543 if (sched_verbose && spec_info->dump)
4544 /* INSN_BB (insn) isn't determined for twin insns yet.
4545 So we can't use current_sched_info->print_insn. */
4546 fprintf (spec_info->dump, ";;\t\tGenerated twin insn : %d/rec%d\n",
4547 INSN_UID (twin), rec->index);
4549 else
4551 ORIG_PAT (check) = ORIG_PAT (insn);
4552 HAS_INTERNAL_DEP (check) = 1;
4553 twin = check;
4554 /* ??? We probably should change all OUTPUT dependencies to
4555 (TRUE | OUTPUT). */
4558 /* Copy all resolved back dependencies of INSN to TWIN. This will
4559 provide correct value for INSN_TICK (TWIN). */
4560 sd_copy_back_deps (twin, insn, true);
4562 if (rec != EXIT_BLOCK_PTR)
4563 /* In case of branchy check, fix CFG. */
4565 basic_block first_bb, second_bb;
4566 rtx jump;
4568 first_bb = BLOCK_FOR_INSN (check);
4569 second_bb = sched_split_block (first_bb, check);
4571 sched_create_recovery_edges (first_bb, rec, second_bb);
4573 sched_init_only_bb (second_bb, first_bb);
4574 sched_init_only_bb (rec, EXIT_BLOCK_PTR);
4576 jump = BB_END (rec);
4577 haifa_init_insn (jump);
4580 /* Move backward dependences from INSN to CHECK and
4581 move forward dependences from INSN to TWIN. */
4583 /* First, create dependencies between INSN's producers and CHECK & TWIN. */
4584 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
4586 rtx pro = DEP_PRO (dep);
4587 ds_t ds;
4589 /* If BEGIN_DATA: [insn ~~TRUE~~> producer]:
4590 check --TRUE--> producer ??? or ANTI ???
4591 twin --TRUE--> producer
4592 twin --ANTI--> check
4594 If BEGIN_CONTROL: [insn ~~ANTI~~> producer]:
4595 check --ANTI--> producer
4596 twin --ANTI--> producer
4597 twin --ANTI--> check
4599 If BE_IN_SPEC: [insn ~~TRUE~~> producer]:
4600 check ~~TRUE~~> producer
4601 twin ~~TRUE~~> producer
4602 twin --ANTI--> check */
4604 ds = DEP_STATUS (dep);
4606 if (ds & BEGIN_SPEC)
4608 gcc_assert (!mutate_p);
4609 ds &= ~BEGIN_SPEC;
4612 init_dep_1 (new_dep, pro, check, DEP_TYPE (dep), ds);
4613 sd_add_dep (new_dep, false);
4615 if (rec != EXIT_BLOCK_PTR)
4617 DEP_CON (new_dep) = twin;
4618 sd_add_dep (new_dep, false);
4622 /* Second, remove backward dependencies of INSN. */
4623 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4624 sd_iterator_cond (&sd_it, &dep);)
4626 if ((DEP_STATUS (dep) & BEGIN_SPEC)
4627 || mutate_p)
4628 /* We can delete this dep because we overcome it with
4629 BEGIN_SPECULATION. */
4630 sd_delete_dep (sd_it);
4631 else
4632 sd_iterator_next (&sd_it);
4635 /* Future Speculations. Determine what BE_IN speculations will be like. */
4636 fs = 0;
4638 /* Fields (DONE_SPEC (x) & BEGIN_SPEC) and CHECK_SPEC (x) are set only
4639 here. */
4641 gcc_assert (!DONE_SPEC (insn));
4643 if (!mutate_p)
4645 ds_t ts = TODO_SPEC (insn);
4647 DONE_SPEC (insn) = ts & BEGIN_SPEC;
4648 CHECK_SPEC (check) = ts & BEGIN_SPEC;
4650 /* Luckiness of future speculations solely depends upon initial
4651 BEGIN speculation. */
4652 if (ts & BEGIN_DATA)
4653 fs = set_dep_weak (fs, BE_IN_DATA, get_dep_weak (ts, BEGIN_DATA));
4654 if (ts & BEGIN_CONTROL)
4655 fs = set_dep_weak (fs, BE_IN_CONTROL,
4656 get_dep_weak (ts, BEGIN_CONTROL));
4658 else
4659 CHECK_SPEC (check) = CHECK_SPEC (insn);
4661 /* Future speculations: call the helper. */
4662 process_insn_forw_deps_be_in_spec (insn, twin, fs);
4664 if (rec != EXIT_BLOCK_PTR)
4666 /* Which types of dependencies should we use here is,
4667 generally, machine-dependent question... But, for now,
4668 it is not. */
4670 if (!mutate_p)
4672 init_dep (new_dep, insn, check, REG_DEP_TRUE);
4673 sd_add_dep (new_dep, false);
4675 init_dep (new_dep, insn, twin, REG_DEP_OUTPUT);
4676 sd_add_dep (new_dep, false);
4678 else
4680 if (spec_info->dump)
4681 fprintf (spec_info->dump, ";;\t\tRemoved simple check : %s\n",
4682 (*current_sched_info->print_insn) (insn, 0));
4684 /* Remove all dependencies of the INSN. */
4686 sd_it = sd_iterator_start (insn, (SD_LIST_FORW
4687 | SD_LIST_BACK
4688 | SD_LIST_RES_BACK));
4689 while (sd_iterator_cond (&sd_it, &dep))
4690 sd_delete_dep (sd_it);
4693 /* If former check (INSN) already was moved to the ready (or queue)
4694 list, add new check (CHECK) there too. */
4695 if (QUEUE_INDEX (insn) != QUEUE_NOWHERE)
4696 try_ready (check);
4698 /* Remove old check from instruction stream and free its
4699 data. */
4700 sched_remove_insn (insn);
4703 init_dep (new_dep, check, twin, REG_DEP_ANTI);
4704 sd_add_dep (new_dep, false);
4706 else
4708 init_dep_1 (new_dep, insn, check, REG_DEP_TRUE, DEP_TRUE | DEP_OUTPUT);
4709 sd_add_dep (new_dep, false);
4712 if (!mutate_p)
4713 /* Fix priorities. If MUTATE_P is nonzero, this is not necessary,
4714 because it'll be done later in add_to_speculative_block. */
4716 rtx_vec_t priorities_roots = NULL;
4718 clear_priorities (twin, &priorities_roots);
4719 calc_priorities (priorities_roots);
4720 VEC_free (rtx, heap, priorities_roots);
4724 /* Removes dependency between instructions in the recovery block REC
4725 and usual region instructions. It keeps inner dependences so it
4726 won't be necessary to recompute them. */
4727 static void
4728 fix_recovery_deps (basic_block rec)
4730 rtx note, insn, jump, ready_list = 0;
4731 bitmap_head in_ready;
4732 rtx link;
4734 bitmap_initialize (&in_ready, 0);
4736 /* NOTE - a basic block note. */
4737 note = NEXT_INSN (BB_HEAD (rec));
4738 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4739 insn = BB_END (rec);
4740 gcc_assert (JUMP_P (insn));
4741 insn = PREV_INSN (insn);
4745 sd_iterator_def sd_it;
4746 dep_t dep;
4748 for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
4749 sd_iterator_cond (&sd_it, &dep);)
4751 rtx consumer = DEP_CON (dep);
4753 if (BLOCK_FOR_INSN (consumer) != rec)
4755 sd_delete_dep (sd_it);
4757 if (bitmap_set_bit (&in_ready, INSN_LUID (consumer)))
4758 ready_list = alloc_INSN_LIST (consumer, ready_list);
4760 else
4762 gcc_assert ((DEP_STATUS (dep) & DEP_TYPES) == DEP_TRUE);
4764 sd_iterator_next (&sd_it);
4768 insn = PREV_INSN (insn);
4770 while (insn != note);
4772 bitmap_clear (&in_ready);
4774 /* Try to add instructions to the ready or queue list. */
4775 for (link = ready_list; link; link = XEXP (link, 1))
4776 try_ready (XEXP (link, 0));
4777 free_INSN_LIST_list (&ready_list);
4779 /* Fixing jump's dependences. */
4780 insn = BB_HEAD (rec);
4781 jump = BB_END (rec);
4783 gcc_assert (LABEL_P (insn));
4784 insn = NEXT_INSN (insn);
4786 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
4787 add_jump_dependencies (insn, jump);
4790 /* Change pattern of INSN to NEW_PAT. */
4791 void
4792 sched_change_pattern (rtx insn, rtx new_pat)
4794 int t;
4796 t = validate_change (insn, &PATTERN (insn), new_pat, 0);
4797 gcc_assert (t);
4798 dfa_clear_single_insn_cache (insn);
4801 /* Change pattern of INSN to NEW_PAT. Invalidate cached haifa
4802 instruction data. */
4803 static void
4804 haifa_change_pattern (rtx insn, rtx new_pat)
4806 sched_change_pattern (insn, new_pat);
4808 /* Invalidate INSN_COST, so it'll be recalculated. */
4809 INSN_COST (insn) = -1;
4810 /* Invalidate INSN_TICK, so it'll be recalculated. */
4811 INSN_TICK (insn) = INVALID_TICK;
4814 /* -1 - can't speculate,
4815 0 - for speculation with REQUEST mode it is OK to use
4816 current instruction pattern,
4817 1 - need to change pattern for *NEW_PAT to be speculative. */
4819 sched_speculate_insn (rtx insn, ds_t request, rtx *new_pat)
4821 gcc_assert (current_sched_info->flags & DO_SPECULATION
4822 && (request & SPECULATIVE)
4823 && sched_insn_is_legitimate_for_speculation_p (insn, request));
4825 if ((request & spec_info->mask) != request)
4826 return -1;
4828 if (request & BE_IN_SPEC
4829 && !(request & BEGIN_SPEC))
4830 return 0;
4832 return targetm.sched.speculate_insn (insn, request, new_pat);
4835 static int
4836 haifa_speculate_insn (rtx insn, ds_t request, rtx *new_pat)
4838 gcc_assert (sched_deps_info->generate_spec_deps
4839 && !IS_SPECULATION_CHECK_P (insn));
4841 if (HAS_INTERNAL_DEP (insn)
4842 || SCHED_GROUP_P (insn))
4843 return -1;
4845 return sched_speculate_insn (insn, request, new_pat);
4848 /* Print some information about block BB, which starts with HEAD and
4849 ends with TAIL, before scheduling it.
4850 I is zero, if scheduler is about to start with the fresh ebb. */
4851 static void
4852 dump_new_block_header (int i, basic_block bb, rtx head, rtx tail)
4854 if (!i)
4855 fprintf (sched_dump,
4856 ";; ======================================================\n");
4857 else
4858 fprintf (sched_dump,
4859 ";; =====================ADVANCING TO=====================\n");
4860 fprintf (sched_dump,
4861 ";; -- basic block %d from %d to %d -- %s reload\n",
4862 bb->index, INSN_UID (head), INSN_UID (tail),
4863 (reload_completed ? "after" : "before"));
4864 fprintf (sched_dump,
4865 ";; ======================================================\n");
4866 fprintf (sched_dump, "\n");
4869 /* Unlink basic block notes and labels and saves them, so they
4870 can be easily restored. We unlink basic block notes in EBB to
4871 provide back-compatibility with the previous code, as target backends
4872 assume, that there'll be only instructions between
4873 current_sched_info->{head and tail}. We restore these notes as soon
4874 as we can.
4875 FIRST (LAST) is the first (last) basic block in the ebb.
4876 NB: In usual case (FIRST == LAST) nothing is really done. */
4877 void
4878 unlink_bb_notes (basic_block first, basic_block last)
4880 /* We DON'T unlink basic block notes of the first block in the ebb. */
4881 if (first == last)
4882 return;
4884 bb_header = XNEWVEC (rtx, last_basic_block);
4886 /* Make a sentinel. */
4887 if (last->next_bb != EXIT_BLOCK_PTR)
4888 bb_header[last->next_bb->index] = 0;
4890 first = first->next_bb;
4893 rtx prev, label, note, next;
4895 label = BB_HEAD (last);
4896 if (LABEL_P (label))
4897 note = NEXT_INSN (label);
4898 else
4899 note = label;
4900 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4902 prev = PREV_INSN (label);
4903 next = NEXT_INSN (note);
4904 gcc_assert (prev && next);
4906 NEXT_INSN (prev) = next;
4907 PREV_INSN (next) = prev;
4909 bb_header[last->index] = label;
4911 if (last == first)
4912 break;
4914 last = last->prev_bb;
4916 while (1);
4919 /* Restore basic block notes.
4920 FIRST is the first basic block in the ebb. */
4921 static void
4922 restore_bb_notes (basic_block first)
4924 if (!bb_header)
4925 return;
4927 /* We DON'T unlink basic block notes of the first block in the ebb. */
4928 first = first->next_bb;
4929 /* Remember: FIRST is actually a second basic block in the ebb. */
4931 while (first != EXIT_BLOCK_PTR
4932 && bb_header[first->index])
4934 rtx prev, label, note, next;
4936 label = bb_header[first->index];
4937 prev = PREV_INSN (label);
4938 next = NEXT_INSN (prev);
4940 if (LABEL_P (label))
4941 note = NEXT_INSN (label);
4942 else
4943 note = label;
4944 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4946 bb_header[first->index] = 0;
4948 NEXT_INSN (prev) = label;
4949 NEXT_INSN (note) = next;
4950 PREV_INSN (next) = note;
4952 first = first->next_bb;
4955 free (bb_header);
4956 bb_header = 0;
4959 /* Helper function.
4960 Fix CFG after both in- and inter-block movement of
4961 control_flow_insn_p JUMP. */
4962 static void
4963 fix_jump_move (rtx jump)
4965 basic_block bb, jump_bb, jump_bb_next;
4967 bb = BLOCK_FOR_INSN (PREV_INSN (jump));
4968 jump_bb = BLOCK_FOR_INSN (jump);
4969 jump_bb_next = jump_bb->next_bb;
4971 gcc_assert (common_sched_info->sched_pass_id == SCHED_EBB_PASS
4972 || IS_SPECULATION_BRANCHY_CHECK_P (jump));
4974 if (!NOTE_INSN_BASIC_BLOCK_P (BB_END (jump_bb_next)))
4975 /* if jump_bb_next is not empty. */
4976 BB_END (jump_bb) = BB_END (jump_bb_next);
4978 if (BB_END (bb) != PREV_INSN (jump))
4979 /* Then there are instruction after jump that should be placed
4980 to jump_bb_next. */
4981 BB_END (jump_bb_next) = BB_END (bb);
4982 else
4983 /* Otherwise jump_bb_next is empty. */
4984 BB_END (jump_bb_next) = NEXT_INSN (BB_HEAD (jump_bb_next));
4986 /* To make assertion in move_insn happy. */
4987 BB_END (bb) = PREV_INSN (jump);
4989 update_bb_for_insn (jump_bb_next);
4992 /* Fix CFG after interblock movement of control_flow_insn_p JUMP. */
4993 static void
4994 move_block_after_check (rtx jump)
4996 basic_block bb, jump_bb, jump_bb_next;
4997 VEC(edge,gc) *t;
4999 bb = BLOCK_FOR_INSN (PREV_INSN (jump));
5000 jump_bb = BLOCK_FOR_INSN (jump);
5001 jump_bb_next = jump_bb->next_bb;
5003 update_bb_for_insn (jump_bb);
5005 gcc_assert (IS_SPECULATION_CHECK_P (jump)
5006 || IS_SPECULATION_CHECK_P (BB_END (jump_bb_next)));
5008 unlink_block (jump_bb_next);
5009 link_block (jump_bb_next, bb);
5011 t = bb->succs;
5012 bb->succs = 0;
5013 move_succs (&(jump_bb->succs), bb);
5014 move_succs (&(jump_bb_next->succs), jump_bb);
5015 move_succs (&t, jump_bb_next);
5017 df_mark_solutions_dirty ();
5019 common_sched_info->fix_recovery_cfg
5020 (bb->index, jump_bb->index, jump_bb_next->index);
5023 /* Helper function for move_block_after_check.
5024 This functions attaches edge vector pointed to by SUCCSP to
5025 block TO. */
5026 static void
5027 move_succs (VEC(edge,gc) **succsp, basic_block to)
5029 edge e;
5030 edge_iterator ei;
5032 gcc_assert (to->succs == 0);
5034 to->succs = *succsp;
5036 FOR_EACH_EDGE (e, ei, to->succs)
5037 e->src = to;
5039 *succsp = 0;
5042 /* Remove INSN from the instruction stream.
5043 INSN should have any dependencies. */
5044 static void
5045 sched_remove_insn (rtx insn)
5047 sd_finish_insn (insn);
5049 change_queue_index (insn, QUEUE_NOWHERE);
5050 current_sched_info->add_remove_insn (insn, 1);
5051 remove_insn (insn);
5054 /* Clear priorities of all instructions, that are forward dependent on INSN.
5055 Store in vector pointed to by ROOTS_PTR insns on which priority () should
5056 be invoked to initialize all cleared priorities. */
5057 static void
5058 clear_priorities (rtx insn, rtx_vec_t *roots_ptr)
5060 sd_iterator_def sd_it;
5061 dep_t dep;
5062 bool insn_is_root_p = true;
5064 gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
5066 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
5068 rtx pro = DEP_PRO (dep);
5070 if (INSN_PRIORITY_STATUS (pro) >= 0
5071 && QUEUE_INDEX (insn) != QUEUE_SCHEDULED)
5073 /* If DEP doesn't contribute to priority then INSN itself should
5074 be added to priority roots. */
5075 if (contributes_to_priority_p (dep))
5076 insn_is_root_p = false;
5078 INSN_PRIORITY_STATUS (pro) = -1;
5079 clear_priorities (pro, roots_ptr);
5083 if (insn_is_root_p)
5084 VEC_safe_push (rtx, heap, *roots_ptr, insn);
5087 /* Recompute priorities of instructions, whose priorities might have been
5088 changed. ROOTS is a vector of instructions whose priority computation will
5089 trigger initialization of all cleared priorities. */
5090 static void
5091 calc_priorities (rtx_vec_t roots)
5093 int i;
5094 rtx insn;
5096 FOR_EACH_VEC_ELT (rtx, roots, i, insn)
5097 priority (insn);
5101 /* Add dependences between JUMP and other instructions in the recovery
5102 block. INSN is the first insn the recovery block. */
5103 static void
5104 add_jump_dependencies (rtx insn, rtx jump)
5108 insn = NEXT_INSN (insn);
5109 if (insn == jump)
5110 break;
5112 if (dep_list_size (insn) == 0)
5114 dep_def _new_dep, *new_dep = &_new_dep;
5116 init_dep (new_dep, insn, jump, REG_DEP_ANTI);
5117 sd_add_dep (new_dep, false);
5120 while (1);
5122 gcc_assert (!sd_lists_empty_p (jump, SD_LIST_BACK));
5125 /* Return the NOTE_INSN_BASIC_BLOCK of BB. */
5127 bb_note (basic_block bb)
5129 rtx note;
5131 note = BB_HEAD (bb);
5132 if (LABEL_P (note))
5133 note = NEXT_INSN (note);
5135 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
5136 return note;
5139 #ifdef ENABLE_CHECKING
5140 /* Helper function for check_cfg.
5141 Return nonzero, if edge vector pointed to by EL has edge with TYPE in
5142 its flags. */
5143 static int
5144 has_edge_p (VEC(edge,gc) *el, int type)
5146 edge e;
5147 edge_iterator ei;
5149 FOR_EACH_EDGE (e, ei, el)
5150 if (e->flags & type)
5151 return 1;
5152 return 0;
5155 /* Search back, starting at INSN, for an insn that is not a
5156 NOTE_INSN_VAR_LOCATION. Don't search beyond HEAD, and return it if
5157 no such insn can be found. */
5158 static inline rtx
5159 prev_non_location_insn (rtx insn, rtx head)
5161 while (insn != head && NOTE_P (insn)
5162 && NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION)
5163 insn = PREV_INSN (insn);
5165 return insn;
5168 /* Check few properties of CFG between HEAD and TAIL.
5169 If HEAD (TAIL) is NULL check from the beginning (till the end) of the
5170 instruction stream. */
5171 static void
5172 check_cfg (rtx head, rtx tail)
5174 rtx next_tail;
5175 basic_block bb = 0;
5176 int not_first = 0, not_last;
5178 if (head == NULL)
5179 head = get_insns ();
5180 if (tail == NULL)
5181 tail = get_last_insn ();
5182 next_tail = NEXT_INSN (tail);
5186 not_last = head != tail;
5188 if (not_first)
5189 gcc_assert (NEXT_INSN (PREV_INSN (head)) == head);
5190 if (not_last)
5191 gcc_assert (PREV_INSN (NEXT_INSN (head)) == head);
5193 if (LABEL_P (head)
5194 || (NOTE_INSN_BASIC_BLOCK_P (head)
5195 && (!not_first
5196 || (not_first && !LABEL_P (PREV_INSN (head))))))
5198 gcc_assert (bb == 0);
5199 bb = BLOCK_FOR_INSN (head);
5200 if (bb != 0)
5201 gcc_assert (BB_HEAD (bb) == head);
5202 else
5203 /* This is the case of jump table. See inside_basic_block_p (). */
5204 gcc_assert (LABEL_P (head) && !inside_basic_block_p (head));
5207 if (bb == 0)
5209 gcc_assert (!inside_basic_block_p (head));
5210 head = NEXT_INSN (head);
5212 else
5214 gcc_assert (inside_basic_block_p (head)
5215 || NOTE_P (head));
5216 gcc_assert (BLOCK_FOR_INSN (head) == bb);
5218 if (LABEL_P (head))
5220 head = NEXT_INSN (head);
5221 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (head));
5223 else
5225 if (control_flow_insn_p (head))
5227 gcc_assert (prev_non_location_insn (BB_END (bb), head)
5228 == head);
5230 if (any_uncondjump_p (head))
5231 gcc_assert (EDGE_COUNT (bb->succs) == 1
5232 && BARRIER_P (NEXT_INSN (head)));
5233 else if (any_condjump_p (head))
5234 gcc_assert (/* Usual case. */
5235 (EDGE_COUNT (bb->succs) > 1
5236 && !BARRIER_P (NEXT_INSN (head)))
5237 /* Or jump to the next instruction. */
5238 || (EDGE_COUNT (bb->succs) == 1
5239 && (BB_HEAD (EDGE_I (bb->succs, 0)->dest)
5240 == JUMP_LABEL (head))));
5242 if (BB_END (bb) == head)
5244 if (EDGE_COUNT (bb->succs) > 1)
5245 gcc_assert (control_flow_insn_p (prev_non_location_insn
5246 (head, BB_HEAD (bb)))
5247 || has_edge_p (bb->succs, EDGE_COMPLEX));
5248 bb = 0;
5251 head = NEXT_INSN (head);
5255 not_first = 1;
5257 while (head != next_tail);
5259 gcc_assert (bb == 0);
5262 #endif /* ENABLE_CHECKING */
5264 /* Extend per basic block data structures. */
5265 static void
5266 extend_bb (void)
5268 if (sched_scan_info->extend_bb)
5269 sched_scan_info->extend_bb ();
5272 /* Init data for BB. */
5273 static void
5274 init_bb (basic_block bb)
5276 if (sched_scan_info->init_bb)
5277 sched_scan_info->init_bb (bb);
5280 /* Extend per insn data structures. */
5281 static void
5282 extend_insn (void)
5284 if (sched_scan_info->extend_insn)
5285 sched_scan_info->extend_insn ();
5288 /* Init data structures for INSN. */
5289 static void
5290 init_insn (rtx insn)
5292 if (sched_scan_info->init_insn)
5293 sched_scan_info->init_insn (insn);
5296 /* Init all insns in BB. */
5297 static void
5298 init_insns_in_bb (basic_block bb)
5300 rtx insn;
5302 FOR_BB_INSNS (bb, insn)
5303 init_insn (insn);
5306 /* A driver function to add a set of basic blocks (BBS),
5307 a single basic block (BB), a set of insns (INSNS) or a single insn (INSN)
5308 to the scheduling region. */
5309 void
5310 sched_scan (const struct sched_scan_info_def *ssi,
5311 bb_vec_t bbs, basic_block bb, insn_vec_t insns, rtx insn)
5313 sched_scan_info = ssi;
5315 if (bbs != NULL || bb != NULL)
5317 extend_bb ();
5319 if (bbs != NULL)
5321 unsigned i;
5322 basic_block x;
5324 FOR_EACH_VEC_ELT (basic_block, bbs, i, x)
5325 init_bb (x);
5328 if (bb != NULL)
5329 init_bb (bb);
5332 extend_insn ();
5334 if (bbs != NULL)
5336 unsigned i;
5337 basic_block x;
5339 FOR_EACH_VEC_ELT (basic_block, bbs, i, x)
5340 init_insns_in_bb (x);
5343 if (bb != NULL)
5344 init_insns_in_bb (bb);
5346 if (insns != NULL)
5348 unsigned i;
5349 rtx x;
5351 FOR_EACH_VEC_ELT (rtx, insns, i, x)
5352 init_insn (x);
5355 if (insn != NULL)
5356 init_insn (insn);
5360 /* Extend data structures for logical insn UID. */
5361 static void
5362 luids_extend_insn (void)
5364 int new_luids_max_uid = get_max_uid () + 1;
5366 VEC_safe_grow_cleared (int, heap, sched_luids, new_luids_max_uid);
5369 /* Initialize LUID for INSN. */
5370 static void
5371 luids_init_insn (rtx insn)
5373 int i = INSN_P (insn) ? 1 : common_sched_info->luid_for_non_insn (insn);
5374 int luid;
5376 if (i >= 0)
5378 luid = sched_max_luid;
5379 sched_max_luid += i;
5381 else
5382 luid = -1;
5384 SET_INSN_LUID (insn, luid);
5387 /* Initialize luids for BBS, BB, INSNS and INSN.
5388 The hook common_sched_info->luid_for_non_insn () is used to determine
5389 if notes, labels, etc. need luids. */
5390 void
5391 sched_init_luids (bb_vec_t bbs, basic_block bb, insn_vec_t insns, rtx insn)
5393 const struct sched_scan_info_def ssi =
5395 NULL, /* extend_bb */
5396 NULL, /* init_bb */
5397 luids_extend_insn, /* extend_insn */
5398 luids_init_insn /* init_insn */
5401 sched_scan (&ssi, bbs, bb, insns, insn);
5404 /* Free LUIDs. */
5405 void
5406 sched_finish_luids (void)
5408 VEC_free (int, heap, sched_luids);
5409 sched_max_luid = 1;
5412 /* Return logical uid of INSN. Helpful while debugging. */
5414 insn_luid (rtx insn)
5416 return INSN_LUID (insn);
5419 /* Extend per insn data in the target. */
5420 void
5421 sched_extend_target (void)
5423 if (targetm.sched.h_i_d_extended)
5424 targetm.sched.h_i_d_extended ();
5427 /* Extend global scheduler structures (those, that live across calls to
5428 schedule_block) to include information about just emitted INSN. */
5429 static void
5430 extend_h_i_d (void)
5432 int reserve = (get_max_uid () + 1
5433 - VEC_length (haifa_insn_data_def, h_i_d));
5434 if (reserve > 0
5435 && ! VEC_space (haifa_insn_data_def, h_i_d, reserve))
5437 VEC_safe_grow_cleared (haifa_insn_data_def, heap, h_i_d,
5438 3 * get_max_uid () / 2);
5439 sched_extend_target ();
5443 /* Initialize h_i_d entry of the INSN with default values.
5444 Values, that are not explicitly initialized here, hold zero. */
5445 static void
5446 init_h_i_d (rtx insn)
5448 if (INSN_LUID (insn) > 0)
5450 INSN_COST (insn) = -1;
5451 QUEUE_INDEX (insn) = QUEUE_NOWHERE;
5452 INSN_TICK (insn) = INVALID_TICK;
5453 INTER_TICK (insn) = INVALID_TICK;
5454 TODO_SPEC (insn) = HARD_DEP;
5458 /* Initialize haifa_insn_data for BBS, BB, INSNS and INSN. */
5459 void
5460 haifa_init_h_i_d (bb_vec_t bbs, basic_block bb, insn_vec_t insns, rtx insn)
5462 const struct sched_scan_info_def ssi =
5464 NULL, /* extend_bb */
5465 NULL, /* init_bb */
5466 extend_h_i_d, /* extend_insn */
5467 init_h_i_d /* init_insn */
5470 sched_scan (&ssi, bbs, bb, insns, insn);
5473 /* Finalize haifa_insn_data. */
5474 void
5475 haifa_finish_h_i_d (void)
5477 int i;
5478 haifa_insn_data_t data;
5479 struct reg_use_data *use, *next;
5481 FOR_EACH_VEC_ELT (haifa_insn_data_def, h_i_d, i, data)
5483 if (data->reg_pressure != NULL)
5484 free (data->reg_pressure);
5485 for (use = data->reg_use_list; use != NULL; use = next)
5487 next = use->next_insn_use;
5488 free (use);
5491 VEC_free (haifa_insn_data_def, heap, h_i_d);
5494 /* Init data for the new insn INSN. */
5495 static void
5496 haifa_init_insn (rtx insn)
5498 gcc_assert (insn != NULL);
5500 sched_init_luids (NULL, NULL, NULL, insn);
5501 sched_extend_target ();
5502 sched_deps_init (false);
5503 haifa_init_h_i_d (NULL, NULL, NULL, insn);
5505 if (adding_bb_to_current_region_p)
5507 sd_init_insn (insn);
5509 /* Extend dependency caches by one element. */
5510 extend_dependency_caches (1, false);
5514 /* Init data for the new basic block BB which comes after AFTER. */
5515 static void
5516 haifa_init_only_bb (basic_block bb, basic_block after)
5518 gcc_assert (bb != NULL);
5520 sched_init_bbs ();
5522 if (common_sched_info->add_block)
5523 /* This changes only data structures of the front-end. */
5524 common_sched_info->add_block (bb, after);
5527 /* A generic version of sched_split_block (). */
5528 basic_block
5529 sched_split_block_1 (basic_block first_bb, rtx after)
5531 edge e;
5533 e = split_block (first_bb, after);
5534 gcc_assert (e->src == first_bb);
5536 /* sched_split_block emits note if *check == BB_END. Probably it
5537 is better to rip that note off. */
5539 return e->dest;
5542 /* A generic version of sched_create_empty_bb (). */
5543 basic_block
5544 sched_create_empty_bb_1 (basic_block after)
5546 return create_empty_bb (after);
5549 /* Insert PAT as an INSN into the schedule and update the necessary data
5550 structures to account for it. */
5552 sched_emit_insn (rtx pat)
5554 rtx insn = emit_insn_after (pat, last_scheduled_insn);
5555 last_scheduled_insn = insn;
5556 haifa_init_insn (insn);
5557 return insn;
5560 #endif /* INSN_SCHEDULING */