Merge branch 'master' into python
[official-gcc.git] / gcc / haifa-sched.c
blobb15fe63f34a299b9312ecab9adfc9d1499f78954
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 "toplev.h"
132 #include "rtl.h"
133 #include "tm_p.h"
134 #include "hard-reg-set.h"
135 #include "regs.h"
136 #include "function.h"
137 #include "flags.h"
138 #include "insn-config.h"
139 #include "insn-attr.h"
140 #include "except.h"
141 #include "toplev.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"
152 #ifdef INSN_SCHEDULING
154 /* issue_rate is the number of insns that can be scheduled in the same
155 machine cycle. It can be defined in the config/mach/mach.h file,
156 otherwise we set it to 1. */
158 int issue_rate;
160 /* sched-verbose controls the amount of debugging output the
161 scheduler prints. It is controlled by -fsched-verbose=N:
162 N>0 and no -DSR : the output is directed to stderr.
163 N>=10 will direct the printouts to stderr (regardless of -dSR).
164 N=1: same as -dSR.
165 N=2: bb's probabilities, detailed ready list info, unit/insn info.
166 N=3: rtl at abort point, control-flow, regions info.
167 N=5: dependences info. */
169 static int sched_verbose_param = 0;
170 int sched_verbose = 0;
172 /* Debugging file. All printouts are sent to dump, which is always set,
173 either to stderr, or to the dump listing file (-dRS). */
174 FILE *sched_dump = 0;
176 /* fix_sched_param() is called from toplev.c upon detection
177 of the -fsched-verbose=N option. */
179 void
180 fix_sched_param (const char *param, const char *val)
182 if (!strcmp (param, "verbose"))
183 sched_verbose_param = atoi (val);
184 else
185 warning (0, "fix_sched_param: unknown param: %s", param);
188 /* This is a placeholder for the scheduler parameters common
189 to all schedulers. */
190 struct common_sched_info_def *common_sched_info;
192 #define INSN_TICK(INSN) (HID (INSN)->tick)
193 #define INTER_TICK(INSN) (HID (INSN)->inter_tick)
195 /* If INSN_TICK of an instruction is equal to INVALID_TICK,
196 then it should be recalculated from scratch. */
197 #define INVALID_TICK (-(max_insn_queue_index + 1))
198 /* The minimal value of the INSN_TICK of an instruction. */
199 #define MIN_TICK (-max_insn_queue_index)
201 /* Issue points are used to distinguish between instructions in max_issue ().
202 For now, all instructions are equally good. */
203 #define ISSUE_POINTS(INSN) 1
205 /* List of important notes we must keep around. This is a pointer to the
206 last element in the list. */
207 rtx note_list;
209 static struct spec_info_def spec_info_var;
210 /* Description of the speculative part of the scheduling.
211 If NULL - no speculation. */
212 spec_info_t spec_info = NULL;
214 /* True, if recovery block was added during scheduling of current block.
215 Used to determine, if we need to fix INSN_TICKs. */
216 static bool haifa_recovery_bb_recently_added_p;
218 /* True, if recovery block was added during this scheduling pass.
219 Used to determine if we should have empty memory pools of dependencies
220 after finishing current region. */
221 bool haifa_recovery_bb_ever_added_p;
223 /* Counters of different types of speculative instructions. */
224 static int nr_begin_data, nr_be_in_data, nr_begin_control, nr_be_in_control;
226 /* Array used in {unlink, restore}_bb_notes. */
227 static rtx *bb_header = 0;
229 /* Basic block after which recovery blocks will be created. */
230 static basic_block before_recovery;
232 /* Basic block just before the EXIT_BLOCK and after recovery, if we have
233 created it. */
234 basic_block after_recovery;
236 /* FALSE if we add bb to another region, so we don't need to initialize it. */
237 bool adding_bb_to_current_region_p = true;
239 /* Queues, etc. */
241 /* An instruction is ready to be scheduled when all insns preceding it
242 have already been scheduled. It is important to ensure that all
243 insns which use its result will not be executed until its result
244 has been computed. An insn is maintained in one of four structures:
246 (P) the "Pending" set of insns which cannot be scheduled until
247 their dependencies have been satisfied.
248 (Q) the "Queued" set of insns that can be scheduled when sufficient
249 time has passed.
250 (R) the "Ready" list of unscheduled, uncommitted insns.
251 (S) the "Scheduled" list of insns.
253 Initially, all insns are either "Pending" or "Ready" depending on
254 whether their dependencies are satisfied.
256 Insns move from the "Ready" list to the "Scheduled" list as they
257 are committed to the schedule. As this occurs, the insns in the
258 "Pending" list have their dependencies satisfied and move to either
259 the "Ready" list or the "Queued" set depending on whether
260 sufficient time has passed to make them ready. As time passes,
261 insns move from the "Queued" set to the "Ready" list.
263 The "Pending" list (P) are the insns in the INSN_FORW_DEPS of the
264 unscheduled insns, i.e., those that are ready, queued, and pending.
265 The "Queued" set (Q) is implemented by the variable `insn_queue'.
266 The "Ready" list (R) is implemented by the variables `ready' and
267 `n_ready'.
268 The "Scheduled" list (S) is the new insn chain built by this pass.
270 The transition (R->S) is implemented in the scheduling loop in
271 `schedule_block' when the best insn to schedule is chosen.
272 The transitions (P->R and P->Q) are implemented in `schedule_insn' as
273 insns move from the ready list to the scheduled list.
274 The transition (Q->R) is implemented in 'queue_to_insn' as time
275 passes or stalls are introduced. */
277 /* Implement a circular buffer to delay instructions until sufficient
278 time has passed. For the new pipeline description interface,
279 MAX_INSN_QUEUE_INDEX is a power of two minus one which is not less
280 than maximal time of instruction execution computed by genattr.c on
281 the base maximal time of functional unit reservations and getting a
282 result. This is the longest time an insn may be queued. */
284 static rtx *insn_queue;
285 static int q_ptr = 0;
286 static int q_size = 0;
287 #define NEXT_Q(X) (((X)+1) & max_insn_queue_index)
288 #define NEXT_Q_AFTER(X, C) (((X)+C) & max_insn_queue_index)
290 #define QUEUE_SCHEDULED (-3)
291 #define QUEUE_NOWHERE (-2)
292 #define QUEUE_READY (-1)
293 /* QUEUE_SCHEDULED - INSN is scheduled.
294 QUEUE_NOWHERE - INSN isn't scheduled yet and is neither in
295 queue or ready list.
296 QUEUE_READY - INSN is in ready list.
297 N >= 0 - INSN queued for X [where NEXT_Q_AFTER (q_ptr, X) == N] cycles. */
299 #define QUEUE_INDEX(INSN) (HID (INSN)->queue_index)
301 /* The following variable value refers for all current and future
302 reservations of the processor units. */
303 state_t curr_state;
305 /* The following variable value is size of memory representing all
306 current and future reservations of the processor units. */
307 size_t dfa_state_size;
309 /* The following array is used to find the best insn from ready when
310 the automaton pipeline interface is used. */
311 char *ready_try = NULL;
313 /* The ready list. */
314 struct ready_list ready = {NULL, 0, 0, 0, 0};
316 /* The pointer to the ready list (to be removed). */
317 static struct ready_list *readyp = &ready;
319 /* Scheduling clock. */
320 static int clock_var;
322 static int may_trap_exp (const_rtx, int);
324 /* Nonzero iff the address is comprised from at most 1 register. */
325 #define CONST_BASED_ADDRESS_P(x) \
326 (REG_P (x) \
327 || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS \
328 || (GET_CODE (x) == LO_SUM)) \
329 && (CONSTANT_P (XEXP (x, 0)) \
330 || CONSTANT_P (XEXP (x, 1)))))
332 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
333 as found by analyzing insn's expression. */
336 static int haifa_luid_for_non_insn (rtx x);
338 /* Haifa version of sched_info hooks common to all headers. */
339 const struct common_sched_info_def haifa_common_sched_info =
341 NULL, /* fix_recovery_cfg */
342 NULL, /* add_block */
343 NULL, /* estimate_number_of_insns */
344 haifa_luid_for_non_insn, /* luid_for_non_insn */
345 SCHED_PASS_UNKNOWN /* sched_pass_id */
348 const struct sched_scan_info_def *sched_scan_info;
350 /* Mapping from instruction UID to its Logical UID. */
351 VEC (int, heap) *sched_luids = NULL;
353 /* Next LUID to assign to an instruction. */
354 int sched_max_luid = 1;
356 /* Haifa Instruction Data. */
357 VEC (haifa_insn_data_def, heap) *h_i_d = NULL;
359 void (* sched_init_only_bb) (basic_block, basic_block);
361 /* Split block function. Different schedulers might use different functions
362 to handle their internal data consistent. */
363 basic_block (* sched_split_block) (basic_block, rtx);
365 /* Create empty basic block after the specified block. */
366 basic_block (* sched_create_empty_bb) (basic_block);
368 static int
369 may_trap_exp (const_rtx x, int is_store)
371 enum rtx_code code;
373 if (x == 0)
374 return TRAP_FREE;
375 code = GET_CODE (x);
376 if (is_store)
378 if (code == MEM && may_trap_p (x))
379 return TRAP_RISKY;
380 else
381 return TRAP_FREE;
383 if (code == MEM)
385 /* The insn uses memory: a volatile load. */
386 if (MEM_VOLATILE_P (x))
387 return IRISKY;
388 /* An exception-free load. */
389 if (!may_trap_p (x))
390 return IFREE;
391 /* A load with 1 base register, to be further checked. */
392 if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
393 return PFREE_CANDIDATE;
394 /* No info on the load, to be further checked. */
395 return PRISKY_CANDIDATE;
397 else
399 const char *fmt;
400 int i, insn_class = TRAP_FREE;
402 /* Neither store nor load, check if it may cause a trap. */
403 if (may_trap_p (x))
404 return TRAP_RISKY;
405 /* Recursive step: walk the insn... */
406 fmt = GET_RTX_FORMAT (code);
407 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
409 if (fmt[i] == 'e')
411 int tmp_class = may_trap_exp (XEXP (x, i), is_store);
412 insn_class = WORST_CLASS (insn_class, tmp_class);
414 else if (fmt[i] == 'E')
416 int j;
417 for (j = 0; j < XVECLEN (x, i); j++)
419 int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
420 insn_class = WORST_CLASS (insn_class, tmp_class);
421 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
422 break;
425 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
426 break;
428 return insn_class;
432 /* Classifies rtx X of an insn for the purpose of verifying that X can be
433 executed speculatively (and consequently the insn can be moved
434 speculatively), by examining X, returning:
435 TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
436 TRAP_FREE: non-load insn.
437 IFREE: load from a globally safe location.
438 IRISKY: volatile load.
439 PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
440 being either PFREE or PRISKY. */
442 static int
443 haifa_classify_rtx (const_rtx x)
445 int tmp_class = TRAP_FREE;
446 int insn_class = TRAP_FREE;
447 enum rtx_code code;
449 if (GET_CODE (x) == PARALLEL)
451 int i, len = XVECLEN (x, 0);
453 for (i = len - 1; i >= 0; i--)
455 tmp_class = haifa_classify_rtx (XVECEXP (x, 0, i));
456 insn_class = WORST_CLASS (insn_class, tmp_class);
457 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
458 break;
461 else
463 code = GET_CODE (x);
464 switch (code)
466 case CLOBBER:
467 /* Test if it is a 'store'. */
468 tmp_class = may_trap_exp (XEXP (x, 0), 1);
469 break;
470 case SET:
471 /* Test if it is a store. */
472 tmp_class = may_trap_exp (SET_DEST (x), 1);
473 if (tmp_class == TRAP_RISKY)
474 break;
475 /* Test if it is a load. */
476 tmp_class =
477 WORST_CLASS (tmp_class,
478 may_trap_exp (SET_SRC (x), 0));
479 break;
480 case COND_EXEC:
481 tmp_class = haifa_classify_rtx (COND_EXEC_CODE (x));
482 if (tmp_class == TRAP_RISKY)
483 break;
484 tmp_class = WORST_CLASS (tmp_class,
485 may_trap_exp (COND_EXEC_TEST (x), 0));
486 break;
487 case TRAP_IF:
488 tmp_class = TRAP_RISKY;
489 break;
490 default:;
492 insn_class = tmp_class;
495 return insn_class;
499 haifa_classify_insn (const_rtx insn)
501 return haifa_classify_rtx (PATTERN (insn));
504 /* Forward declarations. */
506 static int priority (rtx);
507 static int rank_for_schedule (const void *, const void *);
508 static void swap_sort (rtx *, int);
509 static void queue_insn (rtx, int);
510 static int schedule_insn (rtx);
511 static void adjust_priority (rtx);
512 static void advance_one_cycle (void);
513 static void extend_h_i_d (void);
516 /* Notes handling mechanism:
517 =========================
518 Generally, NOTES are saved before scheduling and restored after scheduling.
519 The scheduler distinguishes between two types of notes:
521 (1) LOOP_BEGIN, LOOP_END, SETJMP, EHREGION_BEG, EHREGION_END notes:
522 Before scheduling a region, a pointer to the note is added to the insn
523 that follows or precedes it. (This happens as part of the data dependence
524 computation). After scheduling an insn, the pointer contained in it is
525 used for regenerating the corresponding note (in reemit_notes).
527 (2) All other notes (e.g. INSN_DELETED): Before scheduling a block,
528 these notes are put in a list (in rm_other_notes() and
529 unlink_other_notes ()). After scheduling the block, these notes are
530 inserted at the beginning of the block (in schedule_block()). */
532 static void ready_add (struct ready_list *, rtx, bool);
533 static rtx ready_remove_first (struct ready_list *);
535 static void queue_to_ready (struct ready_list *);
536 static int early_queue_to_ready (state_t, struct ready_list *);
538 static void debug_ready_list (struct ready_list *);
540 /* The following functions are used to implement multi-pass scheduling
541 on the first cycle. */
542 static rtx ready_remove (struct ready_list *, int);
543 static void ready_remove_insn (rtx);
545 static int choose_ready (struct ready_list *, rtx *);
547 static void fix_inter_tick (rtx, rtx);
548 static int fix_tick_ready (rtx);
549 static void change_queue_index (rtx, int);
551 /* The following functions are used to implement scheduling of data/control
552 speculative instructions. */
554 static void extend_h_i_d (void);
555 static void init_h_i_d (rtx);
556 static void generate_recovery_code (rtx);
557 static void process_insn_forw_deps_be_in_spec (rtx, rtx, ds_t);
558 static void begin_speculative_block (rtx);
559 static void add_to_speculative_block (rtx);
560 static void init_before_recovery (basic_block *);
561 static void create_check_block_twin (rtx, bool);
562 static void fix_recovery_deps (basic_block);
563 static void haifa_change_pattern (rtx, rtx);
564 static void dump_new_block_header (int, basic_block, rtx, rtx);
565 static void restore_bb_notes (basic_block);
566 static void fix_jump_move (rtx);
567 static void move_block_after_check (rtx);
568 static void move_succs (VEC(edge,gc) **, basic_block);
569 static void sched_remove_insn (rtx);
570 static void clear_priorities (rtx, rtx_vec_t *);
571 static void calc_priorities (rtx_vec_t);
572 static void add_jump_dependencies (rtx, rtx);
573 #ifdef ENABLE_CHECKING
574 static int has_edge_p (VEC(edge,gc) *, int);
575 static void check_cfg (rtx, rtx);
576 #endif
578 #endif /* INSN_SCHEDULING */
580 /* Point to state used for the current scheduling pass. */
581 struct haifa_sched_info *current_sched_info;
583 #ifndef INSN_SCHEDULING
584 void
585 schedule_insns (void)
588 #else
590 /* Do register pressure sensitive insn scheduling if the flag is set
591 up. */
592 bool sched_pressure_p;
594 /* Map regno -> its cover class. The map defined only when
595 SCHED_PRESSURE_P is true. */
596 enum reg_class *sched_regno_cover_class;
598 /* The current register pressure. Only elements corresponding cover
599 classes are defined. */
600 static int curr_reg_pressure[N_REG_CLASSES];
602 /* Saved value of the previous array. */
603 static int saved_reg_pressure[N_REG_CLASSES];
605 /* Register living at given scheduling point. */
606 static bitmap curr_reg_live;
608 /* Saved value of the previous array. */
609 static bitmap saved_reg_live;
611 /* Registers mentioned in the current region. */
612 static bitmap region_ref_regs;
614 /* Initiate register pressure relative info for scheduling the current
615 region. Currently it is only clearing register mentioned in the
616 current region. */
617 void
618 sched_init_region_reg_pressure_info (void)
620 bitmap_clear (region_ref_regs);
623 /* Update current register pressure related info after birth (if
624 BIRTH_P) or death of register REGNO. */
625 static void
626 mark_regno_birth_or_death (int regno, bool birth_p)
628 enum reg_class cover_class;
630 cover_class = sched_regno_cover_class[regno];
631 if (regno >= FIRST_PSEUDO_REGISTER)
633 if (cover_class != NO_REGS)
635 if (birth_p)
637 bitmap_set_bit (curr_reg_live, regno);
638 curr_reg_pressure[cover_class]
639 += ira_reg_class_nregs[cover_class][PSEUDO_REGNO_MODE (regno)];
641 else
643 bitmap_clear_bit (curr_reg_live, regno);
644 curr_reg_pressure[cover_class]
645 -= ira_reg_class_nregs[cover_class][PSEUDO_REGNO_MODE (regno)];
649 else if (cover_class != NO_REGS
650 && ! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
652 if (birth_p)
654 bitmap_set_bit (curr_reg_live, regno);
655 curr_reg_pressure[cover_class]++;
657 else
659 bitmap_clear_bit (curr_reg_live, regno);
660 curr_reg_pressure[cover_class]--;
665 /* Initiate current register pressure related info from living
666 registers given by LIVE. */
667 static void
668 initiate_reg_pressure_info (bitmap live)
670 int i;
671 unsigned int j;
672 bitmap_iterator bi;
674 for (i = 0; i < ira_reg_class_cover_size; i++)
675 curr_reg_pressure[ira_reg_class_cover[i]] = 0;
676 bitmap_clear (curr_reg_live);
677 EXECUTE_IF_SET_IN_BITMAP (live, 0, j, bi)
678 if (current_nr_blocks == 1 || bitmap_bit_p (region_ref_regs, j))
679 mark_regno_birth_or_death (j, true);
682 /* Mark registers in X as mentioned in the current region. */
683 static void
684 setup_ref_regs (rtx x)
686 int i, j, regno;
687 const RTX_CODE code = GET_CODE (x);
688 const char *fmt;
690 if (REG_P (x))
692 regno = REGNO (x);
693 if (regno >= FIRST_PSEUDO_REGISTER)
694 bitmap_set_bit (region_ref_regs, REGNO (x));
695 else
696 for (i = hard_regno_nregs[regno][GET_MODE (x)] - 1; i >= 0; i--)
697 bitmap_set_bit (region_ref_regs, regno + i);
698 return;
700 fmt = GET_RTX_FORMAT (code);
701 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
702 if (fmt[i] == 'e')
703 setup_ref_regs (XEXP (x, i));
704 else if (fmt[i] == 'E')
706 for (j = 0; j < XVECLEN (x, i); j++)
707 setup_ref_regs (XVECEXP (x, i, j));
711 /* Initiate current register pressure related info at the start of
712 basic block BB. */
713 static void
714 initiate_bb_reg_pressure_info (basic_block bb)
716 unsigned int i;
717 rtx insn;
719 if (current_nr_blocks > 1)
720 FOR_BB_INSNS (bb, insn)
721 if (NONDEBUG_INSN_P (insn))
722 setup_ref_regs (PATTERN (insn));
723 initiate_reg_pressure_info (df_get_live_in (bb));
724 #ifdef EH_RETURN_DATA_REGNO
725 if (bb_has_eh_pred (bb))
726 for (i = 0; ; ++i)
728 unsigned int regno = EH_RETURN_DATA_REGNO (i);
730 if (regno == INVALID_REGNUM)
731 break;
732 if (! bitmap_bit_p (df_get_live_in (bb), regno))
733 mark_regno_birth_or_death (regno, true);
735 #endif
738 /* Save current register pressure related info. */
739 static void
740 save_reg_pressure (void)
742 int i;
744 for (i = 0; i < ira_reg_class_cover_size; i++)
745 saved_reg_pressure[ira_reg_class_cover[i]]
746 = curr_reg_pressure[ira_reg_class_cover[i]];
747 bitmap_copy (saved_reg_live, curr_reg_live);
750 /* Restore saved register pressure related info. */
751 static void
752 restore_reg_pressure (void)
754 int i;
756 for (i = 0; i < ira_reg_class_cover_size; i++)
757 curr_reg_pressure[ira_reg_class_cover[i]]
758 = saved_reg_pressure[ira_reg_class_cover[i]];
759 bitmap_copy (curr_reg_live, saved_reg_live);
762 /* Return TRUE if the register is dying after its USE. */
763 static bool
764 dying_use_p (struct reg_use_data *use)
766 struct reg_use_data *next;
768 for (next = use->next_regno_use; next != use; next = next->next_regno_use)
769 if (NONDEBUG_INSN_P (next->insn)
770 && QUEUE_INDEX (next->insn) != QUEUE_SCHEDULED)
771 return false;
772 return true;
775 /* Print info about the current register pressure and its excess for
776 each cover class. */
777 static void
778 print_curr_reg_pressure (void)
780 int i;
781 enum reg_class cl;
783 fprintf (sched_dump, ";;\t");
784 for (i = 0; i < ira_reg_class_cover_size; i++)
786 cl = ira_reg_class_cover[i];
787 gcc_assert (curr_reg_pressure[cl] >= 0);
788 fprintf (sched_dump, " %s:%d(%d)", reg_class_names[cl],
789 curr_reg_pressure[cl],
790 curr_reg_pressure[cl] - ira_available_class_regs[cl]);
792 fprintf (sched_dump, "\n");
795 /* Pointer to the last instruction scheduled. Used by rank_for_schedule,
796 so that insns independent of the last scheduled insn will be preferred
797 over dependent instructions. */
799 static rtx last_scheduled_insn;
801 /* Cached cost of the instruction. Use below function to get cost of the
802 insn. -1 here means that the field is not initialized. */
803 #define INSN_COST(INSN) (HID (INSN)->cost)
805 /* Compute cost of executing INSN.
806 This is the number of cycles between instruction issue and
807 instruction results. */
809 insn_cost (rtx insn)
811 int cost;
813 if (sel_sched_p ())
815 if (recog_memoized (insn) < 0)
816 return 0;
818 cost = insn_default_latency (insn);
819 if (cost < 0)
820 cost = 0;
822 return cost;
825 cost = INSN_COST (insn);
827 if (cost < 0)
829 /* A USE insn, or something else we don't need to
830 understand. We can't pass these directly to
831 result_ready_cost or insn_default_latency because it will
832 trigger a fatal error for unrecognizable insns. */
833 if (recog_memoized (insn) < 0)
835 INSN_COST (insn) = 0;
836 return 0;
838 else
840 cost = insn_default_latency (insn);
841 if (cost < 0)
842 cost = 0;
844 INSN_COST (insn) = cost;
848 return cost;
851 /* Compute cost of dependence LINK.
852 This is the number of cycles between instruction issue and
853 instruction results.
854 ??? We also use this function to call recog_memoized on all insns. */
856 dep_cost_1 (dep_t link, dw_t dw)
858 rtx insn = DEP_PRO (link);
859 rtx used = DEP_CON (link);
860 int cost;
862 /* A USE insn should never require the value used to be computed.
863 This allows the computation of a function's result and parameter
864 values to overlap the return and call. We don't care about the
865 the dependence cost when only decreasing register pressure. */
866 if (recog_memoized (used) < 0)
868 cost = 0;
869 recog_memoized (insn);
871 else
873 enum reg_note dep_type = DEP_TYPE (link);
875 cost = insn_cost (insn);
877 if (INSN_CODE (insn) >= 0)
879 if (dep_type == REG_DEP_ANTI)
880 cost = 0;
881 else if (dep_type == REG_DEP_OUTPUT)
883 cost = (insn_default_latency (insn)
884 - insn_default_latency (used));
885 if (cost <= 0)
886 cost = 1;
888 else if (bypass_p (insn))
889 cost = insn_latency (insn, used);
893 if (targetm.sched.adjust_cost_2)
894 cost = targetm.sched.adjust_cost_2 (used, (int) dep_type, insn, cost,
895 dw);
896 else if (targetm.sched.adjust_cost != NULL)
898 /* This variable is used for backward compatibility with the
899 targets. */
900 rtx dep_cost_rtx_link = alloc_INSN_LIST (NULL_RTX, NULL_RTX);
902 /* Make it self-cycled, so that if some tries to walk over this
903 incomplete list he/she will be caught in an endless loop. */
904 XEXP (dep_cost_rtx_link, 1) = dep_cost_rtx_link;
906 /* Targets use only REG_NOTE_KIND of the link. */
907 PUT_REG_NOTE_KIND (dep_cost_rtx_link, DEP_TYPE (link));
909 cost = targetm.sched.adjust_cost (used, dep_cost_rtx_link,
910 insn, cost);
912 free_INSN_LIST_node (dep_cost_rtx_link);
915 if (cost < 0)
916 cost = 0;
919 return cost;
922 /* Compute cost of dependence LINK.
923 This is the number of cycles between instruction issue and
924 instruction results. */
926 dep_cost (dep_t link)
928 return dep_cost_1 (link, 0);
931 /* Use this sel-sched.c friendly function in reorder2 instead of increasing
932 INSN_PRIORITY explicitly. */
933 void
934 increase_insn_priority (rtx insn, int amount)
936 if (!sel_sched_p ())
938 /* We're dealing with haifa-sched.c INSN_PRIORITY. */
939 if (INSN_PRIORITY_KNOWN (insn))
940 INSN_PRIORITY (insn) += amount;
942 else
944 /* In sel-sched.c INSN_PRIORITY is not kept up to date.
945 Use EXPR_PRIORITY instead. */
946 sel_add_to_insn_priority (insn, amount);
950 /* Return 'true' if DEP should be included in priority calculations. */
951 static bool
952 contributes_to_priority_p (dep_t dep)
954 if (DEBUG_INSN_P (DEP_CON (dep))
955 || DEBUG_INSN_P (DEP_PRO (dep)))
956 return false;
958 /* Critical path is meaningful in block boundaries only. */
959 if (!current_sched_info->contributes_to_priority (DEP_CON (dep),
960 DEP_PRO (dep)))
961 return false;
963 /* If flag COUNT_SPEC_IN_CRITICAL_PATH is set,
964 then speculative instructions will less likely be
965 scheduled. That is because the priority of
966 their producers will increase, and, thus, the
967 producers will more likely be scheduled, thus,
968 resolving the dependence. */
969 if (sched_deps_info->generate_spec_deps
970 && !(spec_info->flags & COUNT_SPEC_IN_CRITICAL_PATH)
971 && (DEP_STATUS (dep) & SPECULATIVE))
972 return false;
974 return true;
977 /* Compute the number of nondebug forward deps of an insn. */
979 static int
980 dep_list_size (rtx insn)
982 sd_iterator_def sd_it;
983 dep_t dep;
984 int dbgcount = 0, nodbgcount = 0;
986 if (!MAY_HAVE_DEBUG_INSNS)
987 return sd_lists_size (insn, SD_LIST_FORW);
989 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
991 if (DEBUG_INSN_P (DEP_CON (dep)))
992 dbgcount++;
993 else if (!DEBUG_INSN_P (DEP_PRO (dep)))
994 nodbgcount++;
997 gcc_assert (dbgcount + nodbgcount == sd_lists_size (insn, SD_LIST_FORW));
999 return nodbgcount;
1002 /* Compute the priority number for INSN. */
1003 static int
1004 priority (rtx insn)
1006 if (! INSN_P (insn))
1007 return 0;
1009 /* We should not be interested in priority of an already scheduled insn. */
1010 gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
1012 if (!INSN_PRIORITY_KNOWN (insn))
1014 int this_priority = -1;
1016 if (dep_list_size (insn) == 0)
1017 /* ??? We should set INSN_PRIORITY to insn_cost when and insn has
1018 some forward deps but all of them are ignored by
1019 contributes_to_priority hook. At the moment we set priority of
1020 such insn to 0. */
1021 this_priority = insn_cost (insn);
1022 else
1024 rtx prev_first, twin;
1025 basic_block rec;
1027 /* For recovery check instructions we calculate priority slightly
1028 different than that of normal instructions. Instead of walking
1029 through INSN_FORW_DEPS (check) list, we walk through
1030 INSN_FORW_DEPS list of each instruction in the corresponding
1031 recovery block. */
1033 /* Selective scheduling does not define RECOVERY_BLOCK macro. */
1034 rec = sel_sched_p () ? NULL : RECOVERY_BLOCK (insn);
1035 if (!rec || rec == EXIT_BLOCK_PTR)
1037 prev_first = PREV_INSN (insn);
1038 twin = insn;
1040 else
1042 prev_first = NEXT_INSN (BB_HEAD (rec));
1043 twin = PREV_INSN (BB_END (rec));
1048 sd_iterator_def sd_it;
1049 dep_t dep;
1051 FOR_EACH_DEP (twin, SD_LIST_FORW, sd_it, dep)
1053 rtx next;
1054 int next_priority;
1056 next = DEP_CON (dep);
1058 if (BLOCK_FOR_INSN (next) != rec)
1060 int cost;
1062 if (!contributes_to_priority_p (dep))
1063 continue;
1065 if (twin == insn)
1066 cost = dep_cost (dep);
1067 else
1069 struct _dep _dep1, *dep1 = &_dep1;
1071 init_dep (dep1, insn, next, REG_DEP_ANTI);
1073 cost = dep_cost (dep1);
1076 next_priority = cost + priority (next);
1078 if (next_priority > this_priority)
1079 this_priority = next_priority;
1083 twin = PREV_INSN (twin);
1085 while (twin != prev_first);
1088 if (this_priority < 0)
1090 gcc_assert (this_priority == -1);
1092 this_priority = insn_cost (insn);
1095 INSN_PRIORITY (insn) = this_priority;
1096 INSN_PRIORITY_STATUS (insn) = 1;
1099 return INSN_PRIORITY (insn);
1102 /* Macros and functions for keeping the priority queue sorted, and
1103 dealing with queuing and dequeuing of instructions. */
1105 #define SCHED_SORT(READY, N_READY) \
1106 do { if ((N_READY) == 2) \
1107 swap_sort (READY, N_READY); \
1108 else if ((N_READY) > 2) \
1109 qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); } \
1110 while (0)
1112 /* Setup info about the current register pressure impact of scheduling
1113 INSN at the current scheduling point. */
1114 static void
1115 setup_insn_reg_pressure_info (rtx insn)
1117 int i, change, before, after, hard_regno;
1118 int excess_cost_change;
1119 enum machine_mode mode;
1120 enum reg_class cl;
1121 struct reg_pressure_data *pressure_info;
1122 int *max_reg_pressure;
1123 struct reg_use_data *use;
1124 static int death[N_REG_CLASSES];
1126 excess_cost_change = 0;
1127 for (i = 0; i < ira_reg_class_cover_size; i++)
1128 death[ira_reg_class_cover[i]] = 0;
1129 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
1130 if (dying_use_p (use))
1132 cl = sched_regno_cover_class[use->regno];
1133 if (use->regno < FIRST_PSEUDO_REGISTER)
1134 death[cl]++;
1135 else
1136 death[cl] += ira_reg_class_nregs[cl][PSEUDO_REGNO_MODE (use->regno)];
1138 pressure_info = INSN_REG_PRESSURE (insn);
1139 max_reg_pressure = INSN_MAX_REG_PRESSURE (insn);
1140 gcc_assert (pressure_info != NULL && max_reg_pressure != NULL);
1141 for (i = 0; i < ira_reg_class_cover_size; i++)
1143 cl = ira_reg_class_cover[i];
1144 gcc_assert (curr_reg_pressure[cl] >= 0);
1145 change = (int) pressure_info[i].set_increase - death[cl];
1146 before = MAX (0, max_reg_pressure[i] - ira_available_class_regs[cl]);
1147 after = MAX (0, max_reg_pressure[i] + change
1148 - ira_available_class_regs[cl]);
1149 hard_regno = ira_class_hard_regs[cl][0];
1150 gcc_assert (hard_regno >= 0);
1151 mode = reg_raw_mode[hard_regno];
1152 excess_cost_change += ((after - before)
1153 * (ira_memory_move_cost[mode][cl][0]
1154 + ira_memory_move_cost[mode][cl][1]));
1156 INSN_REG_PRESSURE_EXCESS_COST_CHANGE (insn) = excess_cost_change;
1159 /* Returns a positive value if x is preferred; returns a negative value if
1160 y is preferred. Should never return 0, since that will make the sort
1161 unstable. */
1163 static int
1164 rank_for_schedule (const void *x, const void *y)
1166 rtx tmp = *(const rtx *) y;
1167 rtx tmp2 = *(const rtx *) x;
1168 rtx last;
1169 int tmp_class, tmp2_class;
1170 int val, priority_val, info_val;
1172 if (MAY_HAVE_DEBUG_INSNS)
1174 /* Schedule debug insns as early as possible. */
1175 if (DEBUG_INSN_P (tmp) && !DEBUG_INSN_P (tmp2))
1176 return -1;
1177 else if (DEBUG_INSN_P (tmp2))
1178 return 1;
1181 /* The insn in a schedule group should be issued the first. */
1182 if (flag_sched_group_heuristic &&
1183 SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
1184 return SCHED_GROUP_P (tmp2) ? 1 : -1;
1186 /* Make sure that priority of TMP and TMP2 are initialized. */
1187 gcc_assert (INSN_PRIORITY_KNOWN (tmp) && INSN_PRIORITY_KNOWN (tmp2));
1189 if (sched_pressure_p)
1191 int diff;
1193 /* Prefer insn whose scheduling results in the smallest register
1194 pressure excess. */
1195 if ((diff = (INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp)
1196 + (INSN_TICK (tmp) > clock_var
1197 ? INSN_TICK (tmp) - clock_var : 0)
1198 - INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp2)
1199 - (INSN_TICK (tmp2) > clock_var
1200 ? INSN_TICK (tmp2) - clock_var : 0))) != 0)
1201 return diff;
1205 if (sched_pressure_p
1206 && (INSN_TICK (tmp2) > clock_var || INSN_TICK (tmp) > clock_var))
1208 if (INSN_TICK (tmp) <= clock_var)
1209 return -1;
1210 else if (INSN_TICK (tmp2) <= clock_var)
1211 return 1;
1212 else
1213 return INSN_TICK (tmp) - INSN_TICK (tmp2);
1215 /* Prefer insn with higher priority. */
1216 priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
1218 if (flag_sched_critical_path_heuristic && priority_val)
1219 return priority_val;
1221 /* Prefer speculative insn with greater dependencies weakness. */
1222 if (flag_sched_spec_insn_heuristic && spec_info)
1224 ds_t ds1, ds2;
1225 dw_t dw1, dw2;
1226 int dw;
1228 ds1 = TODO_SPEC (tmp) & SPECULATIVE;
1229 if (ds1)
1230 dw1 = ds_weak (ds1);
1231 else
1232 dw1 = NO_DEP_WEAK;
1234 ds2 = TODO_SPEC (tmp2) & SPECULATIVE;
1235 if (ds2)
1236 dw2 = ds_weak (ds2);
1237 else
1238 dw2 = NO_DEP_WEAK;
1240 dw = dw2 - dw1;
1241 if (dw > (NO_DEP_WEAK / 8) || dw < -(NO_DEP_WEAK / 8))
1242 return dw;
1245 info_val = (*current_sched_info->rank) (tmp, tmp2);
1246 if(flag_sched_rank_heuristic && info_val)
1247 return info_val;
1249 if (flag_sched_last_insn_heuristic)
1251 last = last_scheduled_insn;
1253 if (DEBUG_INSN_P (last) && last != current_sched_info->prev_head)
1255 last = PREV_INSN (last);
1256 while (!NONDEBUG_INSN_P (last)
1257 && last != current_sched_info->prev_head);
1260 /* Compare insns based on their relation to the last scheduled
1261 non-debug insn. */
1262 if (flag_sched_last_insn_heuristic && NONDEBUG_INSN_P (last))
1264 dep_t dep1;
1265 dep_t dep2;
1267 /* Classify the instructions into three classes:
1268 1) Data dependent on last schedule insn.
1269 2) Anti/Output dependent on last scheduled insn.
1270 3) Independent of last scheduled insn, or has latency of one.
1271 Choose the insn from the highest numbered class if different. */
1272 dep1 = sd_find_dep_between (last, tmp, true);
1274 if (dep1 == NULL || dep_cost (dep1) == 1)
1275 tmp_class = 3;
1276 else if (/* Data dependence. */
1277 DEP_TYPE (dep1) == REG_DEP_TRUE)
1278 tmp_class = 1;
1279 else
1280 tmp_class = 2;
1282 dep2 = sd_find_dep_between (last, tmp2, true);
1284 if (dep2 == NULL || dep_cost (dep2) == 1)
1285 tmp2_class = 3;
1286 else if (/* Data dependence. */
1287 DEP_TYPE (dep2) == REG_DEP_TRUE)
1288 tmp2_class = 1;
1289 else
1290 tmp2_class = 2;
1292 if ((val = tmp2_class - tmp_class))
1293 return val;
1296 /* Prefer the insn which has more later insns that depend on it.
1297 This gives the scheduler more freedom when scheduling later
1298 instructions at the expense of added register pressure. */
1300 val = (dep_list_size (tmp2) - dep_list_size (tmp));
1302 if (flag_sched_dep_count_heuristic && val != 0)
1303 return val;
1305 /* If insns are equally good, sort by INSN_LUID (original insn order),
1306 so that we make the sort stable. This minimizes instruction movement,
1307 thus minimizing sched's effect on debugging and cross-jumping. */
1308 return INSN_LUID (tmp) - INSN_LUID (tmp2);
1311 /* Resort the array A in which only element at index N may be out of order. */
1313 HAIFA_INLINE static void
1314 swap_sort (rtx *a, int n)
1316 rtx insn = a[n - 1];
1317 int i = n - 2;
1319 while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
1321 a[i + 1] = a[i];
1322 i -= 1;
1324 a[i + 1] = insn;
1327 /* Add INSN to the insn queue so that it can be executed at least
1328 N_CYCLES after the currently executing insn. Preserve insns
1329 chain for debugging purposes. */
1331 HAIFA_INLINE static void
1332 queue_insn (rtx insn, int n_cycles)
1334 int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
1335 rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
1337 gcc_assert (n_cycles <= max_insn_queue_index);
1338 gcc_assert (!DEBUG_INSN_P (insn));
1340 insn_queue[next_q] = link;
1341 q_size += 1;
1343 if (sched_verbose >= 2)
1345 fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
1346 (*current_sched_info->print_insn) (insn, 0));
1348 fprintf (sched_dump, "queued for %d cycles.\n", n_cycles);
1351 QUEUE_INDEX (insn) = next_q;
1354 /* Remove INSN from queue. */
1355 static void
1356 queue_remove (rtx insn)
1358 gcc_assert (QUEUE_INDEX (insn) >= 0);
1359 remove_free_INSN_LIST_elem (insn, &insn_queue[QUEUE_INDEX (insn)]);
1360 q_size--;
1361 QUEUE_INDEX (insn) = QUEUE_NOWHERE;
1364 /* Return a pointer to the bottom of the ready list, i.e. the insn
1365 with the lowest priority. */
1367 rtx *
1368 ready_lastpos (struct ready_list *ready)
1370 gcc_assert (ready->n_ready >= 1);
1371 return ready->vec + ready->first - ready->n_ready + 1;
1374 /* Add an element INSN to the ready list so that it ends up with the
1375 lowest/highest priority depending on FIRST_P. */
1377 HAIFA_INLINE static void
1378 ready_add (struct ready_list *ready, rtx insn, bool first_p)
1380 if (!first_p)
1382 if (ready->first == ready->n_ready)
1384 memmove (ready->vec + ready->veclen - ready->n_ready,
1385 ready_lastpos (ready),
1386 ready->n_ready * sizeof (rtx));
1387 ready->first = ready->veclen - 1;
1389 ready->vec[ready->first - ready->n_ready] = insn;
1391 else
1393 if (ready->first == ready->veclen - 1)
1395 if (ready->n_ready)
1396 /* ready_lastpos() fails when called with (ready->n_ready == 0). */
1397 memmove (ready->vec + ready->veclen - ready->n_ready - 1,
1398 ready_lastpos (ready),
1399 ready->n_ready * sizeof (rtx));
1400 ready->first = ready->veclen - 2;
1402 ready->vec[++(ready->first)] = insn;
1405 ready->n_ready++;
1406 if (DEBUG_INSN_P (insn))
1407 ready->n_debug++;
1409 gcc_assert (QUEUE_INDEX (insn) != QUEUE_READY);
1410 QUEUE_INDEX (insn) = QUEUE_READY;
1413 /* Remove the element with the highest priority from the ready list and
1414 return it. */
1416 HAIFA_INLINE static rtx
1417 ready_remove_first (struct ready_list *ready)
1419 rtx t;
1421 gcc_assert (ready->n_ready);
1422 t = ready->vec[ready->first--];
1423 ready->n_ready--;
1424 if (DEBUG_INSN_P (t))
1425 ready->n_debug--;
1426 /* If the queue becomes empty, reset it. */
1427 if (ready->n_ready == 0)
1428 ready->first = ready->veclen - 1;
1430 gcc_assert (QUEUE_INDEX (t) == QUEUE_READY);
1431 QUEUE_INDEX (t) = QUEUE_NOWHERE;
1433 return t;
1436 /* The following code implements multi-pass scheduling for the first
1437 cycle. In other words, we will try to choose ready insn which
1438 permits to start maximum number of insns on the same cycle. */
1440 /* Return a pointer to the element INDEX from the ready. INDEX for
1441 insn with the highest priority is 0, and the lowest priority has
1442 N_READY - 1. */
1445 ready_element (struct ready_list *ready, int index)
1447 gcc_assert (ready->n_ready && index < ready->n_ready);
1449 return ready->vec[ready->first - index];
1452 /* Remove the element INDEX from the ready list and return it. INDEX
1453 for insn with the highest priority is 0, and the lowest priority
1454 has N_READY - 1. */
1456 HAIFA_INLINE static rtx
1457 ready_remove (struct ready_list *ready, int index)
1459 rtx t;
1460 int i;
1462 if (index == 0)
1463 return ready_remove_first (ready);
1464 gcc_assert (ready->n_ready && index < ready->n_ready);
1465 t = ready->vec[ready->first - index];
1466 ready->n_ready--;
1467 if (DEBUG_INSN_P (t))
1468 ready->n_debug--;
1469 for (i = index; i < ready->n_ready; i++)
1470 ready->vec[ready->first - i] = ready->vec[ready->first - i - 1];
1471 QUEUE_INDEX (t) = QUEUE_NOWHERE;
1472 return t;
1475 /* Remove INSN from the ready list. */
1476 static void
1477 ready_remove_insn (rtx insn)
1479 int i;
1481 for (i = 0; i < readyp->n_ready; i++)
1482 if (ready_element (readyp, i) == insn)
1484 ready_remove (readyp, i);
1485 return;
1487 gcc_unreachable ();
1490 /* Sort the ready list READY by ascending priority, using the SCHED_SORT
1491 macro. */
1493 void
1494 ready_sort (struct ready_list *ready)
1496 int i;
1497 rtx *first = ready_lastpos (ready);
1499 if (sched_pressure_p)
1501 for (i = 0; i < ready->n_ready; i++)
1502 setup_insn_reg_pressure_info (first[i]);
1504 SCHED_SORT (first, ready->n_ready);
1507 /* PREV is an insn that is ready to execute. Adjust its priority if that
1508 will help shorten or lengthen register lifetimes as appropriate. Also
1509 provide a hook for the target to tweak itself. */
1511 HAIFA_INLINE static void
1512 adjust_priority (rtx prev)
1514 /* ??? There used to be code here to try and estimate how an insn
1515 affected register lifetimes, but it did it by looking at REG_DEAD
1516 notes, which we removed in schedule_region. Nor did it try to
1517 take into account register pressure or anything useful like that.
1519 Revisit when we have a machine model to work with and not before. */
1521 if (targetm.sched.adjust_priority)
1522 INSN_PRIORITY (prev) =
1523 targetm.sched.adjust_priority (prev, INSN_PRIORITY (prev));
1526 /* Advance DFA state STATE on one cycle. */
1527 void
1528 advance_state (state_t state)
1530 if (targetm.sched.dfa_pre_advance_cycle)
1531 targetm.sched.dfa_pre_advance_cycle ();
1533 if (targetm.sched.dfa_pre_cycle_insn)
1534 state_transition (state,
1535 targetm.sched.dfa_pre_cycle_insn ());
1537 state_transition (state, NULL);
1539 if (targetm.sched.dfa_post_cycle_insn)
1540 state_transition (state,
1541 targetm.sched.dfa_post_cycle_insn ());
1543 if (targetm.sched.dfa_post_advance_cycle)
1544 targetm.sched.dfa_post_advance_cycle ();
1547 /* Advance time on one cycle. */
1548 HAIFA_INLINE static void
1549 advance_one_cycle (void)
1551 advance_state (curr_state);
1552 if (sched_verbose >= 6)
1553 fprintf (sched_dump, ";;\tAdvanced a state.\n");
1556 /* Clock at which the previous instruction was issued. */
1557 static int last_clock_var;
1559 /* Update register pressure after scheduling INSN. */
1560 static void
1561 update_register_pressure (rtx insn)
1563 struct reg_use_data *use;
1564 struct reg_set_data *set;
1566 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
1567 if (dying_use_p (use) && bitmap_bit_p (curr_reg_live, use->regno))
1568 mark_regno_birth_or_death (use->regno, false);
1569 for (set = INSN_REG_SET_LIST (insn); set != NULL; set = set->next_insn_set)
1570 mark_regno_birth_or_death (set->regno, true);
1573 /* Set up or update (if UPDATE_P) max register pressure (see its
1574 meaning in sched-int.h::_haifa_insn_data) for all current BB insns
1575 after insn AFTER. */
1576 static void
1577 setup_insn_max_reg_pressure (rtx after, bool update_p)
1579 int i, p;
1580 bool eq_p;
1581 rtx insn;
1582 static int max_reg_pressure[N_REG_CLASSES];
1584 save_reg_pressure ();
1585 for (i = 0; i < ira_reg_class_cover_size; i++)
1586 max_reg_pressure[ira_reg_class_cover[i]]
1587 = curr_reg_pressure[ira_reg_class_cover[i]];
1588 for (insn = NEXT_INSN (after);
1589 insn != NULL_RTX && ! BARRIER_P (insn)
1590 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (after);
1591 insn = NEXT_INSN (insn))
1592 if (NONDEBUG_INSN_P (insn))
1594 eq_p = true;
1595 for (i = 0; i < ira_reg_class_cover_size; i++)
1597 p = max_reg_pressure[ira_reg_class_cover[i]];
1598 if (INSN_MAX_REG_PRESSURE (insn)[i] != p)
1600 eq_p = false;
1601 INSN_MAX_REG_PRESSURE (insn)[i]
1602 = max_reg_pressure[ira_reg_class_cover[i]];
1605 if (update_p && eq_p)
1606 break;
1607 update_register_pressure (insn);
1608 for (i = 0; i < ira_reg_class_cover_size; i++)
1609 if (max_reg_pressure[ira_reg_class_cover[i]]
1610 < curr_reg_pressure[ira_reg_class_cover[i]])
1611 max_reg_pressure[ira_reg_class_cover[i]]
1612 = curr_reg_pressure[ira_reg_class_cover[i]];
1614 restore_reg_pressure ();
1617 /* Update the current register pressure after scheduling INSN. Update
1618 also max register pressure for unscheduled insns of the current
1619 BB. */
1620 static void
1621 update_reg_and_insn_max_reg_pressure (rtx insn)
1623 int i;
1624 int before[N_REG_CLASSES];
1626 for (i = 0; i < ira_reg_class_cover_size; i++)
1627 before[i] = curr_reg_pressure[ira_reg_class_cover[i]];
1628 update_register_pressure (insn);
1629 for (i = 0; i < ira_reg_class_cover_size; i++)
1630 if (curr_reg_pressure[ira_reg_class_cover[i]] != before[i])
1631 break;
1632 if (i < ira_reg_class_cover_size)
1633 setup_insn_max_reg_pressure (insn, true);
1636 /* Set up register pressure at the beginning of basic block BB whose
1637 insns starting after insn AFTER. Set up also max register pressure
1638 for all insns of the basic block. */
1639 void
1640 sched_setup_bb_reg_pressure_info (basic_block bb, rtx after)
1642 gcc_assert (sched_pressure_p);
1643 initiate_bb_reg_pressure_info (bb);
1644 setup_insn_max_reg_pressure (after, false);
1647 /* INSN is the "currently executing insn". Launch each insn which was
1648 waiting on INSN. READY is the ready list which contains the insns
1649 that are ready to fire. CLOCK is the current cycle. The function
1650 returns necessary cycle advance after issuing the insn (it is not
1651 zero for insns in a schedule group). */
1653 static int
1654 schedule_insn (rtx insn)
1656 sd_iterator_def sd_it;
1657 dep_t dep;
1658 int i;
1659 int advance = 0;
1661 if (sched_verbose >= 1)
1663 struct reg_pressure_data *pressure_info;
1664 char buf[2048];
1666 print_insn (buf, insn, 0);
1667 buf[40] = 0;
1668 fprintf (sched_dump, ";;\t%3i--> %-40s:", clock_var, buf);
1670 if (recog_memoized (insn) < 0)
1671 fprintf (sched_dump, "nothing");
1672 else
1673 print_reservation (sched_dump, insn);
1674 pressure_info = INSN_REG_PRESSURE (insn);
1675 if (pressure_info != NULL)
1677 fputc (':', sched_dump);
1678 for (i = 0; i < ira_reg_class_cover_size; i++)
1679 fprintf (sched_dump, "%s%+d(%d)",
1680 reg_class_names[ira_reg_class_cover[i]],
1681 pressure_info[i].set_increase, pressure_info[i].change);
1683 fputc ('\n', sched_dump);
1686 if (sched_pressure_p)
1687 update_reg_and_insn_max_reg_pressure (insn);
1689 /* Scheduling instruction should have all its dependencies resolved and
1690 should have been removed from the ready list. */
1691 gcc_assert (sd_lists_empty_p (insn, SD_LIST_BACK));
1693 /* Reset debug insns invalidated by moving this insn. */
1694 if (MAY_HAVE_DEBUG_INSNS && !DEBUG_INSN_P (insn))
1695 for (sd_it = sd_iterator_start (insn, SD_LIST_BACK);
1696 sd_iterator_cond (&sd_it, &dep);)
1698 rtx dbg = DEP_PRO (dep);
1699 struct reg_use_data *use, *next;
1701 gcc_assert (DEBUG_INSN_P (dbg));
1703 if (sched_verbose >= 6)
1704 fprintf (sched_dump, ";;\t\tresetting: debug insn %d\n",
1705 INSN_UID (dbg));
1707 /* ??? Rather than resetting the debug insn, we might be able
1708 to emit a debug temp before the just-scheduled insn, but
1709 this would involve checking that the expression at the
1710 point of the debug insn is equivalent to the expression
1711 before the just-scheduled insn. They might not be: the
1712 expression in the debug insn may depend on other insns not
1713 yet scheduled that set MEMs, REGs or even other debug
1714 insns. It's not clear that attempting to preserve debug
1715 information in these cases is worth the effort, given how
1716 uncommon these resets are and the likelihood that the debug
1717 temps introduced won't survive the schedule change. */
1718 INSN_VAR_LOCATION_LOC (dbg) = gen_rtx_UNKNOWN_VAR_LOC ();
1719 df_insn_rescan (dbg);
1721 /* Unknown location doesn't use any registers. */
1722 for (use = INSN_REG_USE_LIST (dbg); use != NULL; use = next)
1724 struct reg_use_data *prev = use;
1726 /* Remove use from the cyclic next_regno_use chain first. */
1727 while (prev->next_regno_use != use)
1728 prev = prev->next_regno_use;
1729 prev->next_regno_use = use->next_regno_use;
1730 next = use->next_insn_use;
1731 free (use);
1733 INSN_REG_USE_LIST (dbg) = NULL;
1735 /* We delete rather than resolve these deps, otherwise we
1736 crash in sched_free_deps(), because forward deps are
1737 expected to be released before backward deps. */
1738 sd_delete_dep (sd_it);
1741 gcc_assert (QUEUE_INDEX (insn) == QUEUE_NOWHERE);
1742 QUEUE_INDEX (insn) = QUEUE_SCHEDULED;
1744 gcc_assert (INSN_TICK (insn) >= MIN_TICK);
1745 if (INSN_TICK (insn) > clock_var)
1746 /* INSN has been prematurely moved from the queue to the ready list.
1747 This is possible only if following flag is set. */
1748 gcc_assert (flag_sched_stalled_insns);
1750 /* ??? Probably, if INSN is scheduled prematurely, we should leave
1751 INSN_TICK untouched. This is a machine-dependent issue, actually. */
1752 INSN_TICK (insn) = clock_var;
1754 /* Update dependent instructions. */
1755 for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
1756 sd_iterator_cond (&sd_it, &dep);)
1758 rtx next = DEP_CON (dep);
1760 /* Resolve the dependence between INSN and NEXT.
1761 sd_resolve_dep () moves current dep to another list thus
1762 advancing the iterator. */
1763 sd_resolve_dep (sd_it);
1765 /* Don't bother trying to mark next as ready if insn is a debug
1766 insn. If insn is the last hard dependency, it will have
1767 already been discounted. */
1768 if (DEBUG_INSN_P (insn) && !DEBUG_INSN_P (next))
1769 continue;
1771 if (!IS_SPECULATION_BRANCHY_CHECK_P (insn))
1773 int effective_cost;
1775 effective_cost = try_ready (next);
1777 if (effective_cost >= 0
1778 && SCHED_GROUP_P (next)
1779 && advance < effective_cost)
1780 advance = effective_cost;
1782 else
1783 /* Check always has only one forward dependence (to the first insn in
1784 the recovery block), therefore, this will be executed only once. */
1786 gcc_assert (sd_lists_empty_p (insn, SD_LIST_FORW));
1787 fix_recovery_deps (RECOVERY_BLOCK (insn));
1791 /* This is the place where scheduler doesn't *basically* need backward and
1792 forward dependencies for INSN anymore. Nevertheless they are used in
1793 heuristics in rank_for_schedule (), early_queue_to_ready () and in
1794 some targets (e.g. rs6000). Thus the earliest place where we *can*
1795 remove dependencies is after targetm.sched.md_finish () call in
1796 schedule_block (). But, on the other side, the safest place to remove
1797 dependencies is when we are finishing scheduling entire region. As we
1798 don't generate [many] dependencies during scheduling itself, we won't
1799 need memory until beginning of next region.
1800 Bottom line: Dependencies are removed for all insns in the end of
1801 scheduling the region. */
1803 /* Annotate the instruction with issue information -- TImode
1804 indicates that the instruction is expected not to be able
1805 to issue on the same cycle as the previous insn. A machine
1806 may use this information to decide how the instruction should
1807 be aligned. */
1808 if (issue_rate > 1
1809 && GET_CODE (PATTERN (insn)) != USE
1810 && GET_CODE (PATTERN (insn)) != CLOBBER
1811 && !DEBUG_INSN_P (insn))
1813 if (reload_completed)
1814 PUT_MODE (insn, clock_var > last_clock_var ? TImode : VOIDmode);
1815 last_clock_var = clock_var;
1818 return advance;
1821 /* Functions for handling of notes. */
1823 /* Add note list that ends on FROM_END to the end of TO_ENDP. */
1824 void
1825 concat_note_lists (rtx from_end, rtx *to_endp)
1827 rtx from_start;
1829 /* It's easy when have nothing to concat. */
1830 if (from_end == NULL)
1831 return;
1833 /* It's also easy when destination is empty. */
1834 if (*to_endp == NULL)
1836 *to_endp = from_end;
1837 return;
1840 from_start = from_end;
1841 while (PREV_INSN (from_start) != NULL)
1842 from_start = PREV_INSN (from_start);
1844 PREV_INSN (from_start) = *to_endp;
1845 NEXT_INSN (*to_endp) = from_start;
1846 *to_endp = from_end;
1849 /* Delete notes between HEAD and TAIL and put them in the chain
1850 of notes ended by NOTE_LIST. */
1851 void
1852 remove_notes (rtx head, rtx tail)
1854 rtx next_tail, insn, next;
1856 note_list = 0;
1857 if (head == tail && !INSN_P (head))
1858 return;
1860 next_tail = NEXT_INSN (tail);
1861 for (insn = head; insn != next_tail; insn = next)
1863 next = NEXT_INSN (insn);
1864 if (!NOTE_P (insn))
1865 continue;
1867 switch (NOTE_KIND (insn))
1869 case NOTE_INSN_BASIC_BLOCK:
1870 continue;
1872 case NOTE_INSN_EPILOGUE_BEG:
1873 if (insn != tail)
1875 remove_insn (insn);
1876 add_reg_note (next, REG_SAVE_NOTE,
1877 GEN_INT (NOTE_INSN_EPILOGUE_BEG));
1878 break;
1880 /* FALLTHRU */
1882 default:
1883 remove_insn (insn);
1885 /* Add the note to list that ends at NOTE_LIST. */
1886 PREV_INSN (insn) = note_list;
1887 NEXT_INSN (insn) = NULL_RTX;
1888 if (note_list)
1889 NEXT_INSN (note_list) = insn;
1890 note_list = insn;
1891 break;
1894 gcc_assert ((sel_sched_p () || insn != tail) && insn != head);
1899 /* Return the head and tail pointers of ebb starting at BEG and ending
1900 at END. */
1901 void
1902 get_ebb_head_tail (basic_block beg, basic_block end, rtx *headp, rtx *tailp)
1904 rtx beg_head = BB_HEAD (beg);
1905 rtx beg_tail = BB_END (beg);
1906 rtx end_head = BB_HEAD (end);
1907 rtx end_tail = BB_END (end);
1909 /* Don't include any notes or labels at the beginning of the BEG
1910 basic block, or notes at the end of the END basic blocks. */
1912 if (LABEL_P (beg_head))
1913 beg_head = NEXT_INSN (beg_head);
1915 while (beg_head != beg_tail)
1916 if (NOTE_P (beg_head) || BOUNDARY_DEBUG_INSN_P (beg_head))
1917 beg_head = NEXT_INSN (beg_head);
1918 else
1919 break;
1921 *headp = beg_head;
1923 if (beg == end)
1924 end_head = beg_head;
1925 else if (LABEL_P (end_head))
1926 end_head = NEXT_INSN (end_head);
1928 while (end_head != end_tail)
1929 if (NOTE_P (end_tail) || BOUNDARY_DEBUG_INSN_P (end_tail))
1930 end_tail = PREV_INSN (end_tail);
1931 else
1932 break;
1934 *tailp = end_tail;
1937 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ]. */
1940 no_real_insns_p (const_rtx head, const_rtx tail)
1942 while (head != NEXT_INSN (tail))
1944 if (!NOTE_P (head) && !LABEL_P (head)
1945 && !BOUNDARY_DEBUG_INSN_P (head))
1946 return 0;
1947 head = NEXT_INSN (head);
1949 return 1;
1952 /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
1953 previously found among the insns. Insert them just before HEAD. */
1955 restore_other_notes (rtx head, basic_block head_bb)
1957 if (note_list != 0)
1959 rtx note_head = note_list;
1961 if (head)
1962 head_bb = BLOCK_FOR_INSN (head);
1963 else
1964 head = NEXT_INSN (bb_note (head_bb));
1966 while (PREV_INSN (note_head))
1968 set_block_for_insn (note_head, head_bb);
1969 note_head = PREV_INSN (note_head);
1971 /* In the above cycle we've missed this note. */
1972 set_block_for_insn (note_head, head_bb);
1974 PREV_INSN (note_head) = PREV_INSN (head);
1975 NEXT_INSN (PREV_INSN (head)) = note_head;
1976 PREV_INSN (head) = note_list;
1977 NEXT_INSN (note_list) = head;
1979 if (BLOCK_FOR_INSN (head) != head_bb)
1980 BB_END (head_bb) = note_list;
1982 head = note_head;
1985 return head;
1988 /* Move insns that became ready to fire from queue to ready list. */
1990 static void
1991 queue_to_ready (struct ready_list *ready)
1993 rtx insn;
1994 rtx link;
1995 rtx skip_insn;
1997 q_ptr = NEXT_Q (q_ptr);
1999 if (dbg_cnt (sched_insn) == false)
2001 /* If debug counter is activated do not requeue insn next after
2002 last_scheduled_insn. */
2003 skip_insn = next_nonnote_insn (last_scheduled_insn);
2004 while (skip_insn && DEBUG_INSN_P (skip_insn))
2005 skip_insn = next_nonnote_insn (skip_insn);
2007 else
2008 skip_insn = NULL_RTX;
2010 /* Add all pending insns that can be scheduled without stalls to the
2011 ready list. */
2012 for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
2014 insn = XEXP (link, 0);
2015 q_size -= 1;
2017 if (sched_verbose >= 2)
2018 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
2019 (*current_sched_info->print_insn) (insn, 0));
2021 /* If the ready list is full, delay the insn for 1 cycle.
2022 See the comment in schedule_block for the rationale. */
2023 if (!reload_completed
2024 && ready->n_ready - ready->n_debug > MAX_SCHED_READY_INSNS
2025 && !SCHED_GROUP_P (insn)
2026 && insn != skip_insn)
2028 if (sched_verbose >= 2)
2029 fprintf (sched_dump, "requeued because ready full\n");
2030 queue_insn (insn, 1);
2032 else
2034 ready_add (ready, insn, false);
2035 if (sched_verbose >= 2)
2036 fprintf (sched_dump, "moving to ready without stalls\n");
2039 free_INSN_LIST_list (&insn_queue[q_ptr]);
2041 /* If there are no ready insns, stall until one is ready and add all
2042 of the pending insns at that point to the ready list. */
2043 if (ready->n_ready == 0)
2045 int stalls;
2047 for (stalls = 1; stalls <= max_insn_queue_index; stalls++)
2049 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
2051 for (; link; link = XEXP (link, 1))
2053 insn = XEXP (link, 0);
2054 q_size -= 1;
2056 if (sched_verbose >= 2)
2057 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
2058 (*current_sched_info->print_insn) (insn, 0));
2060 ready_add (ready, insn, false);
2061 if (sched_verbose >= 2)
2062 fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
2064 free_INSN_LIST_list (&insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]);
2066 advance_one_cycle ();
2068 break;
2071 advance_one_cycle ();
2074 q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
2075 clock_var += stalls;
2079 /* Used by early_queue_to_ready. Determines whether it is "ok" to
2080 prematurely move INSN from the queue to the ready list. Currently,
2081 if a target defines the hook 'is_costly_dependence', this function
2082 uses the hook to check whether there exist any dependences which are
2083 considered costly by the target, between INSN and other insns that
2084 have already been scheduled. Dependences are checked up to Y cycles
2085 back, with default Y=1; The flag -fsched-stalled-insns-dep=Y allows
2086 controlling this value.
2087 (Other considerations could be taken into account instead (or in
2088 addition) depending on user flags and target hooks. */
2090 static bool
2091 ok_for_early_queue_removal (rtx insn)
2093 int n_cycles;
2094 rtx prev_insn = last_scheduled_insn;
2096 if (targetm.sched.is_costly_dependence)
2098 for (n_cycles = flag_sched_stalled_insns_dep; n_cycles; n_cycles--)
2100 for ( ; prev_insn; prev_insn = PREV_INSN (prev_insn))
2102 int cost;
2104 if (prev_insn == current_sched_info->prev_head)
2106 prev_insn = NULL;
2107 break;
2110 if (!NOTE_P (prev_insn))
2112 dep_t dep;
2114 dep = sd_find_dep_between (prev_insn, insn, true);
2116 if (dep != NULL)
2118 cost = dep_cost (dep);
2120 if (targetm.sched.is_costly_dependence (dep, cost,
2121 flag_sched_stalled_insns_dep - n_cycles))
2122 return false;
2126 if (GET_MODE (prev_insn) == TImode) /* end of dispatch group */
2127 break;
2130 if (!prev_insn)
2131 break;
2132 prev_insn = PREV_INSN (prev_insn);
2136 return true;
2140 /* Remove insns from the queue, before they become "ready" with respect
2141 to FU latency considerations. */
2143 static int
2144 early_queue_to_ready (state_t state, struct ready_list *ready)
2146 rtx insn;
2147 rtx link;
2148 rtx next_link;
2149 rtx prev_link;
2150 bool move_to_ready;
2151 int cost;
2152 state_t temp_state = alloca (dfa_state_size);
2153 int stalls;
2154 int insns_removed = 0;
2157 Flag '-fsched-stalled-insns=X' determines the aggressiveness of this
2158 function:
2160 X == 0: There is no limit on how many queued insns can be removed
2161 prematurely. (flag_sched_stalled_insns = -1).
2163 X >= 1: Only X queued insns can be removed prematurely in each
2164 invocation. (flag_sched_stalled_insns = X).
2166 Otherwise: Early queue removal is disabled.
2167 (flag_sched_stalled_insns = 0)
2170 if (! flag_sched_stalled_insns)
2171 return 0;
2173 for (stalls = 0; stalls <= max_insn_queue_index; stalls++)
2175 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
2177 if (sched_verbose > 6)
2178 fprintf (sched_dump, ";; look at index %d + %d\n", q_ptr, stalls);
2180 prev_link = 0;
2181 while (link)
2183 next_link = XEXP (link, 1);
2184 insn = XEXP (link, 0);
2185 if (insn && sched_verbose > 6)
2186 print_rtl_single (sched_dump, insn);
2188 memcpy (temp_state, state, dfa_state_size);
2189 if (recog_memoized (insn) < 0)
2190 /* non-negative to indicate that it's not ready
2191 to avoid infinite Q->R->Q->R... */
2192 cost = 0;
2193 else
2194 cost = state_transition (temp_state, insn);
2196 if (sched_verbose >= 6)
2197 fprintf (sched_dump, "transition cost = %d\n", cost);
2199 move_to_ready = false;
2200 if (cost < 0)
2202 move_to_ready = ok_for_early_queue_removal (insn);
2203 if (move_to_ready == true)
2205 /* move from Q to R */
2206 q_size -= 1;
2207 ready_add (ready, insn, false);
2209 if (prev_link)
2210 XEXP (prev_link, 1) = next_link;
2211 else
2212 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = next_link;
2214 free_INSN_LIST_node (link);
2216 if (sched_verbose >= 2)
2217 fprintf (sched_dump, ";;\t\tEarly Q-->Ready: insn %s\n",
2218 (*current_sched_info->print_insn) (insn, 0));
2220 insns_removed++;
2221 if (insns_removed == flag_sched_stalled_insns)
2222 /* Remove no more than flag_sched_stalled_insns insns
2223 from Q at a time. */
2224 return insns_removed;
2228 if (move_to_ready == false)
2229 prev_link = link;
2231 link = next_link;
2232 } /* while link */
2233 } /* if link */
2235 } /* for stalls.. */
2237 return insns_removed;
2241 /* Print the ready list for debugging purposes. Callable from debugger. */
2243 static void
2244 debug_ready_list (struct ready_list *ready)
2246 rtx *p;
2247 int i;
2249 if (ready->n_ready == 0)
2251 fprintf (sched_dump, "\n");
2252 return;
2255 p = ready_lastpos (ready);
2256 for (i = 0; i < ready->n_ready; i++)
2258 fprintf (sched_dump, " %s:%d",
2259 (*current_sched_info->print_insn) (p[i], 0),
2260 INSN_LUID (p[i]));
2261 if (sched_pressure_p)
2262 fprintf (sched_dump, "(cost=%d",
2263 INSN_REG_PRESSURE_EXCESS_COST_CHANGE (p[i]));
2264 if (INSN_TICK (p[i]) > clock_var)
2265 fprintf (sched_dump, ":delay=%d", INSN_TICK (p[i]) - clock_var);
2266 if (sched_pressure_p)
2267 fprintf (sched_dump, ")");
2269 fprintf (sched_dump, "\n");
2272 /* Search INSN for REG_SAVE_NOTE notes and convert them back into insn
2273 NOTEs. This is used for NOTE_INSN_EPILOGUE_BEG, so that sched-ebb
2274 replaces the epilogue note in the correct basic block. */
2275 void
2276 reemit_notes (rtx insn)
2278 rtx note, last = insn;
2280 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2282 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
2284 enum insn_note note_type = (enum insn_note) INTVAL (XEXP (note, 0));
2286 last = emit_note_before (note_type, last);
2287 remove_note (insn, note);
2292 /* Move INSN. Reemit notes if needed. Update CFG, if needed. */
2293 static void
2294 move_insn (rtx insn, rtx last, rtx nt)
2296 if (PREV_INSN (insn) != last)
2298 basic_block bb;
2299 rtx note;
2300 int jump_p = 0;
2302 bb = BLOCK_FOR_INSN (insn);
2304 /* BB_HEAD is either LABEL or NOTE. */
2305 gcc_assert (BB_HEAD (bb) != insn);
2307 if (BB_END (bb) == insn)
2308 /* If this is last instruction in BB, move end marker one
2309 instruction up. */
2311 /* Jumps are always placed at the end of basic block. */
2312 jump_p = control_flow_insn_p (insn);
2314 gcc_assert (!jump_p
2315 || ((common_sched_info->sched_pass_id == SCHED_RGN_PASS)
2316 && IS_SPECULATION_BRANCHY_CHECK_P (insn))
2317 || (common_sched_info->sched_pass_id
2318 == SCHED_EBB_PASS));
2320 gcc_assert (BLOCK_FOR_INSN (PREV_INSN (insn)) == bb);
2322 BB_END (bb) = PREV_INSN (insn);
2325 gcc_assert (BB_END (bb) != last);
2327 if (jump_p)
2328 /* We move the block note along with jump. */
2330 gcc_assert (nt);
2332 note = NEXT_INSN (insn);
2333 while (NOTE_NOT_BB_P (note) && note != nt)
2334 note = NEXT_INSN (note);
2336 if (note != nt
2337 && (LABEL_P (note)
2338 || BARRIER_P (note)))
2339 note = NEXT_INSN (note);
2341 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
2343 else
2344 note = insn;
2346 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (note);
2347 PREV_INSN (NEXT_INSN (note)) = PREV_INSN (insn);
2349 NEXT_INSN (note) = NEXT_INSN (last);
2350 PREV_INSN (NEXT_INSN (last)) = note;
2352 NEXT_INSN (last) = insn;
2353 PREV_INSN (insn) = last;
2355 bb = BLOCK_FOR_INSN (last);
2357 if (jump_p)
2359 fix_jump_move (insn);
2361 if (BLOCK_FOR_INSN (insn) != bb)
2362 move_block_after_check (insn);
2364 gcc_assert (BB_END (bb) == last);
2367 df_insn_change_bb (insn, bb);
2369 /* Update BB_END, if needed. */
2370 if (BB_END (bb) == last)
2371 BB_END (bb) = insn;
2374 SCHED_GROUP_P (insn) = 0;
2377 /* Return true if scheduling INSN will finish current clock cycle. */
2378 static bool
2379 insn_finishes_cycle_p (rtx insn)
2381 if (SCHED_GROUP_P (insn))
2382 /* After issuing INSN, rest of the sched_group will be forced to issue
2383 in order. Don't make any plans for the rest of cycle. */
2384 return true;
2386 /* Finishing the block will, apparently, finish the cycle. */
2387 if (current_sched_info->insn_finishes_block_p
2388 && current_sched_info->insn_finishes_block_p (insn))
2389 return true;
2391 return false;
2394 /* The following structure describe an entry of the stack of choices. */
2395 struct choice_entry
2397 /* Ordinal number of the issued insn in the ready queue. */
2398 int index;
2399 /* The number of the rest insns whose issues we should try. */
2400 int rest;
2401 /* The number of issued essential insns. */
2402 int n;
2403 /* State after issuing the insn. */
2404 state_t state;
2407 /* The following array is used to implement a stack of choices used in
2408 function max_issue. */
2409 static struct choice_entry *choice_stack;
2411 /* The following variable value is number of essential insns issued on
2412 the current cycle. An insn is essential one if it changes the
2413 processors state. */
2414 int cycle_issued_insns;
2416 /* This holds the value of the target dfa_lookahead hook. */
2417 int dfa_lookahead;
2419 /* The following variable value is maximal number of tries of issuing
2420 insns for the first cycle multipass insn scheduling. We define
2421 this value as constant*(DFA_LOOKAHEAD**ISSUE_RATE). We would not
2422 need this constraint if all real insns (with non-negative codes)
2423 had reservations because in this case the algorithm complexity is
2424 O(DFA_LOOKAHEAD**ISSUE_RATE). Unfortunately, the dfa descriptions
2425 might be incomplete and such insn might occur. For such
2426 descriptions, the complexity of algorithm (without the constraint)
2427 could achieve DFA_LOOKAHEAD ** N , where N is the queue length. */
2428 static int max_lookahead_tries;
2430 /* The following value is value of hook
2431 `first_cycle_multipass_dfa_lookahead' at the last call of
2432 `max_issue'. */
2433 static int cached_first_cycle_multipass_dfa_lookahead = 0;
2435 /* The following value is value of `issue_rate' at the last call of
2436 `sched_init'. */
2437 static int cached_issue_rate = 0;
2439 /* The following function returns maximal (or close to maximal) number
2440 of insns which can be issued on the same cycle and one of which
2441 insns is insns with the best rank (the first insn in READY). To
2442 make this function tries different samples of ready insns. READY
2443 is current queue `ready'. Global array READY_TRY reflects what
2444 insns are already issued in this try. MAX_POINTS is the sum of points
2445 of all instructions in READY. The function stops immediately,
2446 if it reached the such a solution, that all instruction can be issued.
2447 INDEX will contain index of the best insn in READY. The following
2448 function is used only for first cycle multipass scheduling.
2450 PRIVILEGED_N >= 0
2452 This function expects recognized insns only. All USEs,
2453 CLOBBERs, etc must be filtered elsewhere. */
2455 max_issue (struct ready_list *ready, int privileged_n, state_t state,
2456 int *index)
2458 int n, i, all, n_ready, best, delay, tries_num, max_points;
2459 int more_issue;
2460 struct choice_entry *top;
2461 rtx insn;
2463 n_ready = ready->n_ready;
2464 gcc_assert (dfa_lookahead >= 1 && privileged_n >= 0
2465 && privileged_n <= n_ready);
2467 /* Init MAX_LOOKAHEAD_TRIES. */
2468 if (cached_first_cycle_multipass_dfa_lookahead != dfa_lookahead)
2470 cached_first_cycle_multipass_dfa_lookahead = dfa_lookahead;
2471 max_lookahead_tries = 100;
2472 for (i = 0; i < issue_rate; i++)
2473 max_lookahead_tries *= dfa_lookahead;
2476 /* Init max_points. */
2477 max_points = 0;
2478 more_issue = issue_rate - cycle_issued_insns;
2480 /* ??? We used to assert here that we never issue more insns than issue_rate.
2481 However, some targets (e.g. MIPS/SB1) claim lower issue rate than can be
2482 achieved to get better performance. Until these targets are fixed to use
2483 scheduler hooks to manipulate insns priority instead, the assert should
2484 be disabled.
2486 gcc_assert (more_issue >= 0); */
2488 for (i = 0; i < n_ready; i++)
2489 if (!ready_try [i])
2491 if (more_issue-- > 0)
2492 max_points += ISSUE_POINTS (ready_element (ready, i));
2493 else
2494 break;
2497 /* The number of the issued insns in the best solution. */
2498 best = 0;
2500 top = choice_stack;
2502 /* Set initial state of the search. */
2503 memcpy (top->state, state, dfa_state_size);
2504 top->rest = dfa_lookahead;
2505 top->n = 0;
2507 /* Count the number of the insns to search among. */
2508 for (all = i = 0; i < n_ready; i++)
2509 if (!ready_try [i])
2510 all++;
2512 /* I is the index of the insn to try next. */
2513 i = 0;
2514 tries_num = 0;
2515 for (;;)
2517 if (/* If we've reached a dead end or searched enough of what we have
2518 been asked... */
2519 top->rest == 0
2520 /* Or have nothing else to try. */
2521 || i >= n_ready)
2523 /* ??? (... || i == n_ready). */
2524 gcc_assert (i <= n_ready);
2526 if (top == choice_stack)
2527 break;
2529 if (best < top - choice_stack)
2531 if (privileged_n)
2533 n = privileged_n;
2534 /* Try to find issued privileged insn. */
2535 while (n && !ready_try[--n]);
2538 if (/* If all insns are equally good... */
2539 privileged_n == 0
2540 /* Or a privileged insn will be issued. */
2541 || ready_try[n])
2542 /* Then we have a solution. */
2544 best = top - choice_stack;
2545 /* This is the index of the insn issued first in this
2546 solution. */
2547 *index = choice_stack [1].index;
2548 if (top->n == max_points || best == all)
2549 break;
2553 /* Set ready-list index to point to the last insn
2554 ('i++' below will advance it to the next insn). */
2555 i = top->index;
2557 /* Backtrack. */
2558 ready_try [i] = 0;
2559 top--;
2560 memcpy (state, top->state, dfa_state_size);
2562 else if (!ready_try [i])
2564 tries_num++;
2565 if (tries_num > max_lookahead_tries)
2566 break;
2567 insn = ready_element (ready, i);
2568 delay = state_transition (state, insn);
2569 if (delay < 0)
2571 if (state_dead_lock_p (state)
2572 || insn_finishes_cycle_p (insn))
2573 /* We won't issue any more instructions in the next
2574 choice_state. */
2575 top->rest = 0;
2576 else
2577 top->rest--;
2579 n = top->n;
2580 if (memcmp (top->state, state, dfa_state_size) != 0)
2581 n += ISSUE_POINTS (insn);
2583 /* Advance to the next choice_entry. */
2584 top++;
2585 /* Initialize it. */
2586 top->rest = dfa_lookahead;
2587 top->index = i;
2588 top->n = n;
2589 memcpy (top->state, state, dfa_state_size);
2591 ready_try [i] = 1;
2592 i = -1;
2596 /* Increase ready-list index. */
2597 i++;
2600 /* Restore the original state of the DFA. */
2601 memcpy (state, choice_stack->state, dfa_state_size);
2603 return best;
2606 /* The following function chooses insn from READY and modifies
2607 READY. The following function is used only for first
2608 cycle multipass scheduling.
2609 Return:
2610 -1 if cycle should be advanced,
2611 0 if INSN_PTR is set to point to the desirable insn,
2612 1 if choose_ready () should be restarted without advancing the cycle. */
2613 static int
2614 choose_ready (struct ready_list *ready, rtx *insn_ptr)
2616 int lookahead;
2618 if (dbg_cnt (sched_insn) == false)
2620 rtx insn;
2622 insn = next_nonnote_insn (last_scheduled_insn);
2624 if (QUEUE_INDEX (insn) == QUEUE_READY)
2625 /* INSN is in the ready_list. */
2627 ready_remove_insn (insn);
2628 *insn_ptr = insn;
2629 return 0;
2632 /* INSN is in the queue. Advance cycle to move it to the ready list. */
2633 return -1;
2636 lookahead = 0;
2638 if (targetm.sched.first_cycle_multipass_dfa_lookahead)
2639 lookahead = targetm.sched.first_cycle_multipass_dfa_lookahead ();
2640 if (lookahead <= 0 || SCHED_GROUP_P (ready_element (ready, 0))
2641 || DEBUG_INSN_P (ready_element (ready, 0)))
2643 *insn_ptr = ready_remove_first (ready);
2644 return 0;
2646 else
2648 /* Try to choose the better insn. */
2649 int index = 0, i, n;
2650 rtx insn;
2651 int try_data = 1, try_control = 1;
2652 ds_t ts;
2654 insn = ready_element (ready, 0);
2655 if (INSN_CODE (insn) < 0)
2657 *insn_ptr = ready_remove_first (ready);
2658 return 0;
2661 if (spec_info
2662 && spec_info->flags & (PREFER_NON_DATA_SPEC
2663 | PREFER_NON_CONTROL_SPEC))
2665 for (i = 0, n = ready->n_ready; i < n; i++)
2667 rtx x;
2668 ds_t s;
2670 x = ready_element (ready, i);
2671 s = TODO_SPEC (x);
2673 if (spec_info->flags & PREFER_NON_DATA_SPEC
2674 && !(s & DATA_SPEC))
2676 try_data = 0;
2677 if (!(spec_info->flags & PREFER_NON_CONTROL_SPEC)
2678 || !try_control)
2679 break;
2682 if (spec_info->flags & PREFER_NON_CONTROL_SPEC
2683 && !(s & CONTROL_SPEC))
2685 try_control = 0;
2686 if (!(spec_info->flags & PREFER_NON_DATA_SPEC) || !try_data)
2687 break;
2692 ts = TODO_SPEC (insn);
2693 if ((ts & SPECULATIVE)
2694 && (((!try_data && (ts & DATA_SPEC))
2695 || (!try_control && (ts & CONTROL_SPEC)))
2696 || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard_spec
2697 && !targetm.sched
2698 .first_cycle_multipass_dfa_lookahead_guard_spec (insn))))
2699 /* Discard speculative instruction that stands first in the ready
2700 list. */
2702 change_queue_index (insn, 1);
2703 return 1;
2706 ready_try[0] = 0;
2708 for (i = 1; i < ready->n_ready; i++)
2710 insn = ready_element (ready, i);
2712 ready_try [i]
2713 = ((!try_data && (TODO_SPEC (insn) & DATA_SPEC))
2714 || (!try_control && (TODO_SPEC (insn) & CONTROL_SPEC)));
2717 /* Let the target filter the search space. */
2718 for (i = 1; i < ready->n_ready; i++)
2719 if (!ready_try[i])
2721 insn = ready_element (ready, i);
2723 #ifdef ENABLE_CHECKING
2724 /* If this insn is recognizable we should have already
2725 recognized it earlier.
2726 ??? Not very clear where this is supposed to be done.
2727 See dep_cost_1. */
2728 gcc_assert (INSN_CODE (insn) >= 0
2729 || recog_memoized (insn) < 0);
2730 #endif
2732 ready_try [i]
2733 = (/* INSN_CODE check can be omitted here as it is also done later
2734 in max_issue (). */
2735 INSN_CODE (insn) < 0
2736 || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard
2737 && !targetm.sched.first_cycle_multipass_dfa_lookahead_guard
2738 (insn)));
2741 if (max_issue (ready, 1, curr_state, &index) == 0)
2743 *insn_ptr = ready_remove_first (ready);
2744 if (sched_verbose >= 4)
2745 fprintf (sched_dump, ";;\t\tChosen insn (but can't issue) : %s \n",
2746 (*current_sched_info->print_insn) (*insn_ptr, 0));
2747 return 0;
2749 else
2751 if (sched_verbose >= 4)
2752 fprintf (sched_dump, ";;\t\tChosen insn : %s\n",
2753 (*current_sched_info->print_insn)
2754 (ready_element (ready, index), 0));
2756 *insn_ptr = ready_remove (ready, index);
2757 return 0;
2762 /* Use forward list scheduling to rearrange insns of block pointed to by
2763 TARGET_BB, possibly bringing insns from subsequent blocks in the same
2764 region. */
2766 void
2767 schedule_block (basic_block *target_bb)
2769 int i, first_cycle_insn_p;
2770 int can_issue_more;
2771 state_t temp_state = NULL; /* It is used for multipass scheduling. */
2772 int sort_p, advance, start_clock_var;
2774 /* Head/tail info for this block. */
2775 rtx prev_head = current_sched_info->prev_head;
2776 rtx next_tail = current_sched_info->next_tail;
2777 rtx head = NEXT_INSN (prev_head);
2778 rtx tail = PREV_INSN (next_tail);
2780 /* We used to have code to avoid getting parameters moved from hard
2781 argument registers into pseudos.
2783 However, it was removed when it proved to be of marginal benefit
2784 and caused problems because schedule_block and compute_forward_dependences
2785 had different notions of what the "head" insn was. */
2787 gcc_assert (head != tail || INSN_P (head));
2789 haifa_recovery_bb_recently_added_p = false;
2791 /* Debug info. */
2792 if (sched_verbose)
2793 dump_new_block_header (0, *target_bb, head, tail);
2795 state_reset (curr_state);
2797 /* Clear the ready list. */
2798 ready.first = ready.veclen - 1;
2799 ready.n_ready = 0;
2800 ready.n_debug = 0;
2802 /* It is used for first cycle multipass scheduling. */
2803 temp_state = alloca (dfa_state_size);
2805 if (targetm.sched.md_init)
2806 targetm.sched.md_init (sched_dump, sched_verbose, ready.veclen);
2808 /* We start inserting insns after PREV_HEAD. */
2809 last_scheduled_insn = prev_head;
2811 gcc_assert ((NOTE_P (last_scheduled_insn)
2812 || BOUNDARY_DEBUG_INSN_P (last_scheduled_insn))
2813 && BLOCK_FOR_INSN (last_scheduled_insn) == *target_bb);
2815 /* Initialize INSN_QUEUE. Q_SIZE is the total number of insns in the
2816 queue. */
2817 q_ptr = 0;
2818 q_size = 0;
2820 insn_queue = XALLOCAVEC (rtx, max_insn_queue_index + 1);
2821 memset (insn_queue, 0, (max_insn_queue_index + 1) * sizeof (rtx));
2823 /* Start just before the beginning of time. */
2824 clock_var = -1;
2826 /* We need queue and ready lists and clock_var be initialized
2827 in try_ready () (which is called through init_ready_list ()). */
2828 (*current_sched_info->init_ready_list) ();
2830 /* The algorithm is O(n^2) in the number of ready insns at any given
2831 time in the worst case. Before reload we are more likely to have
2832 big lists so truncate them to a reasonable size. */
2833 if (!reload_completed
2834 && ready.n_ready - ready.n_debug > MAX_SCHED_READY_INSNS)
2836 ready_sort (&ready);
2838 /* Find first free-standing insn past MAX_SCHED_READY_INSNS.
2839 If there are debug insns, we know they're first. */
2840 for (i = MAX_SCHED_READY_INSNS + ready.n_debug; i < ready.n_ready; i++)
2841 if (!SCHED_GROUP_P (ready_element (&ready, i)))
2842 break;
2844 if (sched_verbose >= 2)
2846 fprintf (sched_dump,
2847 ";;\t\tReady list on entry: %d insns\n", ready.n_ready);
2848 fprintf (sched_dump,
2849 ";;\t\t before reload => truncated to %d insns\n", i);
2852 /* Delay all insns past it for 1 cycle. If debug counter is
2853 activated make an exception for the insn right after
2854 last_scheduled_insn. */
2856 rtx skip_insn;
2858 if (dbg_cnt (sched_insn) == false)
2859 skip_insn = next_nonnote_insn (last_scheduled_insn);
2860 else
2861 skip_insn = NULL_RTX;
2863 while (i < ready.n_ready)
2865 rtx insn;
2867 insn = ready_remove (&ready, i);
2869 if (insn != skip_insn)
2870 queue_insn (insn, 1);
2875 /* Now we can restore basic block notes and maintain precise cfg. */
2876 restore_bb_notes (*target_bb);
2878 last_clock_var = -1;
2880 advance = 0;
2882 sort_p = TRUE;
2883 /* Loop until all the insns in BB are scheduled. */
2884 while ((*current_sched_info->schedule_more_p) ())
2888 start_clock_var = clock_var;
2890 clock_var++;
2892 advance_one_cycle ();
2894 /* Add to the ready list all pending insns that can be issued now.
2895 If there are no ready insns, increment clock until one
2896 is ready and add all pending insns at that point to the ready
2897 list. */
2898 queue_to_ready (&ready);
2900 gcc_assert (ready.n_ready);
2902 if (sched_verbose >= 2)
2904 fprintf (sched_dump, ";;\t\tReady list after queue_to_ready: ");
2905 debug_ready_list (&ready);
2907 advance -= clock_var - start_clock_var;
2909 while (advance > 0);
2911 if (sort_p)
2913 /* Sort the ready list based on priority. */
2914 ready_sort (&ready);
2916 if (sched_verbose >= 2)
2918 fprintf (sched_dump, ";;\t\tReady list after ready_sort: ");
2919 debug_ready_list (&ready);
2923 /* We don't want md sched reorder to even see debug isns, so put
2924 them out right away. */
2925 if (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0)))
2927 if (control_flow_insn_p (last_scheduled_insn))
2929 *target_bb = current_sched_info->advance_target_bb
2930 (*target_bb, 0);
2932 if (sched_verbose)
2934 rtx x;
2936 x = next_real_insn (last_scheduled_insn);
2937 gcc_assert (x);
2938 dump_new_block_header (1, *target_bb, x, tail);
2941 last_scheduled_insn = bb_note (*target_bb);
2944 while (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0)))
2946 rtx insn = ready_remove_first (&ready);
2947 gcc_assert (DEBUG_INSN_P (insn));
2948 (*current_sched_info->begin_schedule_ready) (insn,
2949 last_scheduled_insn);
2950 move_insn (insn, last_scheduled_insn,
2951 current_sched_info->next_tail);
2952 last_scheduled_insn = insn;
2953 advance = schedule_insn (insn);
2954 gcc_assert (advance == 0);
2955 if (ready.n_ready > 0)
2956 ready_sort (&ready);
2959 if (!ready.n_ready)
2960 continue;
2963 /* Allow the target to reorder the list, typically for
2964 better instruction bundling. */
2965 if (sort_p && targetm.sched.reorder
2966 && (ready.n_ready == 0
2967 || !SCHED_GROUP_P (ready_element (&ready, 0))))
2968 can_issue_more =
2969 targetm.sched.reorder (sched_dump, sched_verbose,
2970 ready_lastpos (&ready),
2971 &ready.n_ready, clock_var);
2972 else
2973 can_issue_more = issue_rate;
2975 first_cycle_insn_p = 1;
2976 cycle_issued_insns = 0;
2977 for (;;)
2979 rtx insn;
2980 int cost;
2981 bool asm_p = false;
2983 if (sched_verbose >= 2)
2985 fprintf (sched_dump, ";;\tReady list (t = %3d): ",
2986 clock_var);
2987 debug_ready_list (&ready);
2988 if (sched_pressure_p)
2989 print_curr_reg_pressure ();
2992 if (ready.n_ready == 0
2993 && can_issue_more
2994 && reload_completed)
2996 /* Allow scheduling insns directly from the queue in case
2997 there's nothing better to do (ready list is empty) but
2998 there are still vacant dispatch slots in the current cycle. */
2999 if (sched_verbose >= 6)
3000 fprintf (sched_dump,";;\t\tSecond chance\n");
3001 memcpy (temp_state, curr_state, dfa_state_size);
3002 if (early_queue_to_ready (temp_state, &ready))
3003 ready_sort (&ready);
3006 if (ready.n_ready == 0
3007 || !can_issue_more
3008 || state_dead_lock_p (curr_state)
3009 || !(*current_sched_info->schedule_more_p) ())
3010 break;
3012 /* Select and remove the insn from the ready list. */
3013 if (sort_p)
3015 int res;
3017 insn = NULL_RTX;
3018 res = choose_ready (&ready, &insn);
3020 if (res < 0)
3021 /* Finish cycle. */
3022 break;
3023 if (res > 0)
3024 /* Restart choose_ready (). */
3025 continue;
3027 gcc_assert (insn != NULL_RTX);
3029 else
3030 insn = ready_remove_first (&ready);
3032 if (sched_pressure_p && INSN_TICK (insn) > clock_var)
3034 ready_add (&ready, insn, true);
3035 advance = 1;
3036 break;
3039 if (targetm.sched.dfa_new_cycle
3040 && targetm.sched.dfa_new_cycle (sched_dump, sched_verbose,
3041 insn, last_clock_var,
3042 clock_var, &sort_p))
3043 /* SORT_P is used by the target to override sorting
3044 of the ready list. This is needed when the target
3045 has modified its internal structures expecting that
3046 the insn will be issued next. As we need the insn
3047 to have the highest priority (so it will be returned by
3048 the ready_remove_first call above), we invoke
3049 ready_add (&ready, insn, true).
3050 But, still, there is one issue: INSN can be later
3051 discarded by scheduler's front end through
3052 current_sched_info->can_schedule_ready_p, hence, won't
3053 be issued next. */
3055 ready_add (&ready, insn, true);
3056 break;
3059 sort_p = TRUE;
3060 memcpy (temp_state, curr_state, dfa_state_size);
3061 if (recog_memoized (insn) < 0)
3063 asm_p = (GET_CODE (PATTERN (insn)) == ASM_INPUT
3064 || asm_noperands (PATTERN (insn)) >= 0);
3065 if (!first_cycle_insn_p && asm_p)
3066 /* This is asm insn which is tried to be issued on the
3067 cycle not first. Issue it on the next cycle. */
3068 cost = 1;
3069 else
3070 /* A USE insn, or something else we don't need to
3071 understand. We can't pass these directly to
3072 state_transition because it will trigger a
3073 fatal error for unrecognizable insns. */
3074 cost = 0;
3076 else if (sched_pressure_p)
3077 cost = 0;
3078 else
3080 cost = state_transition (temp_state, insn);
3081 if (cost < 0)
3082 cost = 0;
3083 else if (cost == 0)
3084 cost = 1;
3087 if (cost >= 1)
3089 queue_insn (insn, cost);
3090 if (SCHED_GROUP_P (insn))
3092 advance = cost;
3093 break;
3096 continue;
3099 if (current_sched_info->can_schedule_ready_p
3100 && ! (*current_sched_info->can_schedule_ready_p) (insn))
3101 /* We normally get here only if we don't want to move
3102 insn from the split block. */
3104 TODO_SPEC (insn) = (TODO_SPEC (insn) & ~SPECULATIVE) | HARD_DEP;
3105 continue;
3108 /* DECISION is made. */
3110 if (TODO_SPEC (insn) & SPECULATIVE)
3111 generate_recovery_code (insn);
3113 if (control_flow_insn_p (last_scheduled_insn)
3114 /* This is used to switch basic blocks by request
3115 from scheduler front-end (actually, sched-ebb.c only).
3116 This is used to process blocks with single fallthru
3117 edge. If succeeding block has jump, it [jump] will try
3118 move at the end of current bb, thus corrupting CFG. */
3119 || current_sched_info->advance_target_bb (*target_bb, insn))
3121 *target_bb = current_sched_info->advance_target_bb
3122 (*target_bb, 0);
3124 if (sched_verbose)
3126 rtx x;
3128 x = next_real_insn (last_scheduled_insn);
3129 gcc_assert (x);
3130 dump_new_block_header (1, *target_bb, x, tail);
3133 last_scheduled_insn = bb_note (*target_bb);
3136 /* Update counters, etc in the scheduler's front end. */
3137 (*current_sched_info->begin_schedule_ready) (insn,
3138 last_scheduled_insn);
3140 move_insn (insn, last_scheduled_insn, current_sched_info->next_tail);
3141 reemit_notes (insn);
3142 last_scheduled_insn = insn;
3144 if (memcmp (curr_state, temp_state, dfa_state_size) != 0)
3146 cycle_issued_insns++;
3147 memcpy (curr_state, temp_state, dfa_state_size);
3150 if (targetm.sched.variable_issue)
3151 can_issue_more =
3152 targetm.sched.variable_issue (sched_dump, sched_verbose,
3153 insn, can_issue_more);
3154 /* A naked CLOBBER or USE generates no instruction, so do
3155 not count them against the issue rate. */
3156 else if (GET_CODE (PATTERN (insn)) != USE
3157 && GET_CODE (PATTERN (insn)) != CLOBBER)
3158 can_issue_more--;
3159 advance = schedule_insn (insn);
3161 /* After issuing an asm insn we should start a new cycle. */
3162 if (advance == 0 && asm_p)
3163 advance = 1;
3164 if (advance != 0)
3165 break;
3167 first_cycle_insn_p = 0;
3169 /* Sort the ready list based on priority. This must be
3170 redone here, as schedule_insn may have readied additional
3171 insns that will not be sorted correctly. */
3172 if (ready.n_ready > 0)
3173 ready_sort (&ready);
3175 /* Quickly go through debug insns such that md sched
3176 reorder2 doesn't have to deal with debug insns. */
3177 if (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0))
3178 && (*current_sched_info->schedule_more_p) ())
3180 if (control_flow_insn_p (last_scheduled_insn))
3182 *target_bb = current_sched_info->advance_target_bb
3183 (*target_bb, 0);
3185 if (sched_verbose)
3187 rtx x;
3189 x = next_real_insn (last_scheduled_insn);
3190 gcc_assert (x);
3191 dump_new_block_header (1, *target_bb, x, tail);
3194 last_scheduled_insn = bb_note (*target_bb);
3197 while (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0)))
3199 insn = ready_remove_first (&ready);
3200 gcc_assert (DEBUG_INSN_P (insn));
3201 (*current_sched_info->begin_schedule_ready)
3202 (insn, last_scheduled_insn);
3203 move_insn (insn, last_scheduled_insn,
3204 current_sched_info->next_tail);
3205 advance = schedule_insn (insn);
3206 last_scheduled_insn = insn;
3207 gcc_assert (advance == 0);
3208 if (ready.n_ready > 0)
3209 ready_sort (&ready);
3213 if (targetm.sched.reorder2
3214 && (ready.n_ready == 0
3215 || !SCHED_GROUP_P (ready_element (&ready, 0))))
3217 can_issue_more =
3218 targetm.sched.reorder2 (sched_dump, sched_verbose,
3219 ready.n_ready
3220 ? ready_lastpos (&ready) : NULL,
3221 &ready.n_ready, clock_var);
3226 /* Debug info. */
3227 if (sched_verbose)
3229 fprintf (sched_dump, ";;\tReady list (final): ");
3230 debug_ready_list (&ready);
3233 if (current_sched_info->queue_must_finish_empty)
3234 /* Sanity check -- queue must be empty now. Meaningless if region has
3235 multiple bbs. */
3236 gcc_assert (!q_size && !ready.n_ready && !ready.n_debug);
3237 else
3239 /* We must maintain QUEUE_INDEX between blocks in region. */
3240 for (i = ready.n_ready - 1; i >= 0; i--)
3242 rtx x;
3244 x = ready_element (&ready, i);
3245 QUEUE_INDEX (x) = QUEUE_NOWHERE;
3246 TODO_SPEC (x) = (TODO_SPEC (x) & ~SPECULATIVE) | HARD_DEP;
3249 if (q_size)
3250 for (i = 0; i <= max_insn_queue_index; i++)
3252 rtx link;
3253 for (link = insn_queue[i]; link; link = XEXP (link, 1))
3255 rtx x;
3257 x = XEXP (link, 0);
3258 QUEUE_INDEX (x) = QUEUE_NOWHERE;
3259 TODO_SPEC (x) = (TODO_SPEC (x) & ~SPECULATIVE) | HARD_DEP;
3261 free_INSN_LIST_list (&insn_queue[i]);
3265 if (sched_verbose)
3266 fprintf (sched_dump, ";; total time = %d\n", clock_var);
3268 if (!current_sched_info->queue_must_finish_empty
3269 || haifa_recovery_bb_recently_added_p)
3271 /* INSN_TICK (minimum clock tick at which the insn becomes
3272 ready) may be not correct for the insn in the subsequent
3273 blocks of the region. We should use a correct value of
3274 `clock_var' or modify INSN_TICK. It is better to keep
3275 clock_var value equal to 0 at the start of a basic block.
3276 Therefore we modify INSN_TICK here. */
3277 fix_inter_tick (NEXT_INSN (prev_head), last_scheduled_insn);
3280 if (targetm.sched.md_finish)
3282 targetm.sched.md_finish (sched_dump, sched_verbose);
3283 /* Target might have added some instructions to the scheduled block
3284 in its md_finish () hook. These new insns don't have any data
3285 initialized and to identify them we extend h_i_d so that they'll
3286 get zero luids. */
3287 sched_init_luids (NULL, NULL, NULL, NULL);
3290 if (sched_verbose)
3291 fprintf (sched_dump, ";; new head = %d\n;; new tail = %d\n\n",
3292 INSN_UID (head), INSN_UID (tail));
3294 /* Update head/tail boundaries. */
3295 head = NEXT_INSN (prev_head);
3296 tail = last_scheduled_insn;
3298 head = restore_other_notes (head, NULL);
3300 current_sched_info->head = head;
3301 current_sched_info->tail = tail;
3304 /* Set_priorities: compute priority of each insn in the block. */
3307 set_priorities (rtx head, rtx tail)
3309 rtx insn;
3310 int n_insn;
3311 int sched_max_insns_priority =
3312 current_sched_info->sched_max_insns_priority;
3313 rtx prev_head;
3315 if (head == tail && (! INSN_P (head) || BOUNDARY_DEBUG_INSN_P (head)))
3316 gcc_unreachable ();
3318 n_insn = 0;
3320 prev_head = PREV_INSN (head);
3321 for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
3323 if (!INSN_P (insn))
3324 continue;
3326 n_insn++;
3327 (void) priority (insn);
3329 gcc_assert (INSN_PRIORITY_KNOWN (insn));
3331 sched_max_insns_priority = MAX (sched_max_insns_priority,
3332 INSN_PRIORITY (insn));
3335 current_sched_info->sched_max_insns_priority = sched_max_insns_priority;
3337 return n_insn;
3340 /* Set dump and sched_verbose for the desired debugging output. If no
3341 dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
3342 For -fsched-verbose=N, N>=10, print everything to stderr. */
3343 void
3344 setup_sched_dump (void)
3346 sched_verbose = sched_verbose_param;
3347 if (sched_verbose_param == 0 && dump_file)
3348 sched_verbose = 1;
3349 sched_dump = ((sched_verbose_param >= 10 || !dump_file)
3350 ? stderr : dump_file);
3353 /* Initialize some global state for the scheduler. This function works
3354 with the common data shared between all the schedulers. It is called
3355 from the scheduler specific initialization routine. */
3357 void
3358 sched_init (void)
3360 /* Disable speculative loads in their presence if cc0 defined. */
3361 #ifdef HAVE_cc0
3362 flag_schedule_speculative_load = 0;
3363 #endif
3365 sched_pressure_p = (flag_sched_pressure && ! reload_completed
3366 && common_sched_info->sched_pass_id == SCHED_RGN_PASS);
3367 if (sched_pressure_p)
3368 ira_setup_eliminable_regset ();
3370 /* Initialize SPEC_INFO. */
3371 if (targetm.sched.set_sched_flags)
3373 spec_info = &spec_info_var;
3374 targetm.sched.set_sched_flags (spec_info);
3376 if (spec_info->mask != 0)
3378 spec_info->data_weakness_cutoff =
3379 (PARAM_VALUE (PARAM_SCHED_SPEC_PROB_CUTOFF) * MAX_DEP_WEAK) / 100;
3380 spec_info->control_weakness_cutoff =
3381 (PARAM_VALUE (PARAM_SCHED_SPEC_PROB_CUTOFF)
3382 * REG_BR_PROB_BASE) / 100;
3384 else
3385 /* So we won't read anything accidentally. */
3386 spec_info = NULL;
3389 else
3390 /* So we won't read anything accidentally. */
3391 spec_info = 0;
3393 /* Initialize issue_rate. */
3394 if (targetm.sched.issue_rate)
3395 issue_rate = targetm.sched.issue_rate ();
3396 else
3397 issue_rate = 1;
3399 if (cached_issue_rate != issue_rate)
3401 cached_issue_rate = issue_rate;
3402 /* To invalidate max_lookahead_tries: */
3403 cached_first_cycle_multipass_dfa_lookahead = 0;
3406 if (targetm.sched.first_cycle_multipass_dfa_lookahead)
3407 dfa_lookahead = targetm.sched.first_cycle_multipass_dfa_lookahead ();
3408 else
3409 dfa_lookahead = 0;
3411 if (targetm.sched.init_dfa_pre_cycle_insn)
3412 targetm.sched.init_dfa_pre_cycle_insn ();
3414 if (targetm.sched.init_dfa_post_cycle_insn)
3415 targetm.sched.init_dfa_post_cycle_insn ();
3417 dfa_start ();
3418 dfa_state_size = state_size ();
3420 init_alias_analysis ();
3422 df_set_flags (DF_LR_RUN_DCE);
3423 df_note_add_problem ();
3425 /* More problems needed for interloop dep calculation in SMS. */
3426 if (common_sched_info->sched_pass_id == SCHED_SMS_PASS)
3428 df_rd_add_problem ();
3429 df_chain_add_problem (DF_DU_CHAIN + DF_UD_CHAIN);
3432 df_analyze ();
3434 /* Do not run DCE after reload, as this can kill nops inserted
3435 by bundling. */
3436 if (reload_completed)
3437 df_clear_flags (DF_LR_RUN_DCE);
3439 regstat_compute_calls_crossed ();
3441 if (targetm.sched.md_init_global)
3442 targetm.sched.md_init_global (sched_dump, sched_verbose,
3443 get_max_uid () + 1);
3445 if (sched_pressure_p)
3447 int i, max_regno = max_reg_num ();
3449 ira_set_pseudo_classes (sched_verbose ? sched_dump : NULL);
3450 sched_regno_cover_class
3451 = (enum reg_class *) xmalloc (max_regno * sizeof (enum reg_class));
3452 for (i = 0; i < max_regno; i++)
3453 sched_regno_cover_class[i]
3454 = (i < FIRST_PSEUDO_REGISTER
3455 ? ira_class_translate[REGNO_REG_CLASS (i)]
3456 : reg_cover_class (i));
3457 curr_reg_live = BITMAP_ALLOC (NULL);
3458 saved_reg_live = BITMAP_ALLOC (NULL);
3459 region_ref_regs = BITMAP_ALLOC (NULL);
3462 curr_state = xmalloc (dfa_state_size);
3465 static void haifa_init_only_bb (basic_block, basic_block);
3467 /* Initialize data structures specific to the Haifa scheduler. */
3468 void
3469 haifa_sched_init (void)
3471 setup_sched_dump ();
3472 sched_init ();
3474 if (spec_info != NULL)
3476 sched_deps_info->use_deps_list = 1;
3477 sched_deps_info->generate_spec_deps = 1;
3480 /* Initialize luids, dependency caches, target and h_i_d for the
3481 whole function. */
3483 bb_vec_t bbs = VEC_alloc (basic_block, heap, n_basic_blocks);
3484 basic_block bb;
3486 sched_init_bbs ();
3488 FOR_EACH_BB (bb)
3489 VEC_quick_push (basic_block, bbs, bb);
3490 sched_init_luids (bbs, NULL, NULL, NULL);
3491 sched_deps_init (true);
3492 sched_extend_target ();
3493 haifa_init_h_i_d (bbs, NULL, NULL, NULL);
3495 VEC_free (basic_block, heap, bbs);
3498 sched_init_only_bb = haifa_init_only_bb;
3499 sched_split_block = sched_split_block_1;
3500 sched_create_empty_bb = sched_create_empty_bb_1;
3501 haifa_recovery_bb_ever_added_p = false;
3503 #ifdef ENABLE_CHECKING
3504 /* This is used preferably for finding bugs in check_cfg () itself.
3505 We must call sched_bbs_init () before check_cfg () because check_cfg ()
3506 assumes that the last insn in the last bb has a non-null successor. */
3507 check_cfg (0, 0);
3508 #endif
3510 nr_begin_data = nr_begin_control = nr_be_in_data = nr_be_in_control = 0;
3511 before_recovery = 0;
3512 after_recovery = 0;
3515 /* Finish work with the data specific to the Haifa scheduler. */
3516 void
3517 haifa_sched_finish (void)
3519 sched_create_empty_bb = NULL;
3520 sched_split_block = NULL;
3521 sched_init_only_bb = NULL;
3523 if (spec_info && spec_info->dump)
3525 char c = reload_completed ? 'a' : 'b';
3527 fprintf (spec_info->dump,
3528 ";; %s:\n", current_function_name ());
3530 fprintf (spec_info->dump,
3531 ";; Procedure %cr-begin-data-spec motions == %d\n",
3532 c, nr_begin_data);
3533 fprintf (spec_info->dump,
3534 ";; Procedure %cr-be-in-data-spec motions == %d\n",
3535 c, nr_be_in_data);
3536 fprintf (spec_info->dump,
3537 ";; Procedure %cr-begin-control-spec motions == %d\n",
3538 c, nr_begin_control);
3539 fprintf (spec_info->dump,
3540 ";; Procedure %cr-be-in-control-spec motions == %d\n",
3541 c, nr_be_in_control);
3544 /* Finalize h_i_d, dependency caches, and luids for the whole
3545 function. Target will be finalized in md_global_finish (). */
3546 sched_deps_finish ();
3547 sched_finish_luids ();
3548 current_sched_info = NULL;
3549 sched_finish ();
3552 /* Free global data used during insn scheduling. This function works with
3553 the common data shared between the schedulers. */
3555 void
3556 sched_finish (void)
3558 haifa_finish_h_i_d ();
3559 if (sched_pressure_p)
3561 free (sched_regno_cover_class);
3562 BITMAP_FREE (region_ref_regs);
3563 BITMAP_FREE (saved_reg_live);
3564 BITMAP_FREE (curr_reg_live);
3566 free (curr_state);
3568 if (targetm.sched.md_finish_global)
3569 targetm.sched.md_finish_global (sched_dump, sched_verbose);
3571 end_alias_analysis ();
3573 regstat_free_calls_crossed ();
3575 dfa_finish ();
3577 #ifdef ENABLE_CHECKING
3578 /* After reload ia64 backend clobbers CFG, so can't check anything. */
3579 if (!reload_completed)
3580 check_cfg (0, 0);
3581 #endif
3584 /* Fix INSN_TICKs of the instructions in the current block as well as
3585 INSN_TICKs of their dependents.
3586 HEAD and TAIL are the begin and the end of the current scheduled block. */
3587 static void
3588 fix_inter_tick (rtx head, rtx tail)
3590 /* Set of instructions with corrected INSN_TICK. */
3591 bitmap_head processed;
3592 /* ??? It is doubtful if we should assume that cycle advance happens on
3593 basic block boundaries. Basically insns that are unconditionally ready
3594 on the start of the block are more preferable then those which have
3595 a one cycle dependency over insn from the previous block. */
3596 int next_clock = clock_var + 1;
3598 bitmap_initialize (&processed, 0);
3600 /* Iterates over scheduled instructions and fix their INSN_TICKs and
3601 INSN_TICKs of dependent instructions, so that INSN_TICKs are consistent
3602 across different blocks. */
3603 for (tail = NEXT_INSN (tail); head != tail; head = NEXT_INSN (head))
3605 if (INSN_P (head))
3607 int tick;
3608 sd_iterator_def sd_it;
3609 dep_t dep;
3611 tick = INSN_TICK (head);
3612 gcc_assert (tick >= MIN_TICK);
3614 /* Fix INSN_TICK of instruction from just scheduled block. */
3615 if (!bitmap_bit_p (&processed, INSN_LUID (head)))
3617 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_bit_p (&processed, INSN_LUID (next)))
3639 bitmap_set_bit (&processed, INSN_LUID (next));
3640 tick -= next_clock;
3642 if (tick < MIN_TICK)
3643 tick = MIN_TICK;
3645 if (tick > INTER_TICK (next))
3646 INTER_TICK (next) = tick;
3647 else
3648 tick = INTER_TICK (next);
3650 INSN_TICK (next) = tick;
3655 bitmap_clear (&processed);
3658 static int haifa_speculate_insn (rtx, ds_t, rtx *);
3660 /* Check if NEXT is ready to be added to the ready or queue list.
3661 If "yes", add it to the proper list.
3662 Returns:
3663 -1 - is not ready yet,
3664 0 - added to the ready list,
3665 0 < N - queued for N cycles. */
3667 try_ready (rtx next)
3669 ds_t old_ts, *ts;
3671 ts = &TODO_SPEC (next);
3672 old_ts = *ts;
3674 gcc_assert (!(old_ts & ~(SPECULATIVE | HARD_DEP))
3675 && ((old_ts & HARD_DEP)
3676 || (old_ts & SPECULATIVE)));
3678 if (sd_lists_empty_p (next, SD_LIST_BACK))
3679 /* NEXT has all its dependencies resolved. */
3681 /* Remove HARD_DEP bit from NEXT's status. */
3682 *ts &= ~HARD_DEP;
3684 if (current_sched_info->flags & DO_SPECULATION)
3685 /* Remove all speculative bits from NEXT's status. */
3686 *ts &= ~SPECULATIVE;
3688 else
3690 /* One of the NEXT's dependencies has been resolved.
3691 Recalculate NEXT's status. */
3693 *ts &= ~SPECULATIVE & ~HARD_DEP;
3695 if (sd_lists_empty_p (next, SD_LIST_HARD_BACK))
3696 /* Now we've got NEXT with speculative deps only.
3697 1. Look at the deps to see what we have to do.
3698 2. Check if we can do 'todo'. */
3700 sd_iterator_def sd_it;
3701 dep_t dep;
3702 bool first_p = true;
3704 FOR_EACH_DEP (next, SD_LIST_BACK, sd_it, dep)
3706 ds_t ds = DEP_STATUS (dep) & SPECULATIVE;
3708 if (DEBUG_INSN_P (DEP_PRO (dep))
3709 && !DEBUG_INSN_P (next))
3710 continue;
3712 if (first_p)
3714 first_p = false;
3716 *ts = ds;
3718 else
3719 *ts = ds_merge (*ts, ds);
3722 if (ds_weak (*ts) < spec_info->data_weakness_cutoff)
3723 /* Too few points. */
3724 *ts = (*ts & ~SPECULATIVE) | HARD_DEP;
3726 else
3727 *ts |= HARD_DEP;
3730 if (*ts & HARD_DEP)
3731 gcc_assert (*ts == old_ts
3732 && QUEUE_INDEX (next) == QUEUE_NOWHERE);
3733 else if (current_sched_info->new_ready)
3734 *ts = current_sched_info->new_ready (next, *ts);
3736 /* * if !(old_ts & SPECULATIVE) (e.g. HARD_DEP or 0), then insn might
3737 have its original pattern or changed (speculative) one. This is due
3738 to changing ebb in region scheduling.
3739 * But if (old_ts & SPECULATIVE), then we are pretty sure that insn
3740 has speculative pattern.
3742 We can't assert (!(*ts & HARD_DEP) || *ts == old_ts) here because
3743 control-speculative NEXT could have been discarded by sched-rgn.c
3744 (the same case as when discarded by can_schedule_ready_p ()). */
3746 if ((*ts & SPECULATIVE)
3747 /* If (old_ts == *ts), then (old_ts & SPECULATIVE) and we don't
3748 need to change anything. */
3749 && *ts != old_ts)
3751 int res;
3752 rtx new_pat;
3754 gcc_assert ((*ts & SPECULATIVE) && !(*ts & ~SPECULATIVE));
3756 res = haifa_speculate_insn (next, *ts, &new_pat);
3758 switch (res)
3760 case -1:
3761 /* It would be nice to change DEP_STATUS of all dependences,
3762 which have ((DEP_STATUS & SPECULATIVE) == *ts) to HARD_DEP,
3763 so we won't reanalyze anything. */
3764 *ts = (*ts & ~SPECULATIVE) | HARD_DEP;
3765 break;
3767 case 0:
3768 /* We follow the rule, that every speculative insn
3769 has non-null ORIG_PAT. */
3770 if (!ORIG_PAT (next))
3771 ORIG_PAT (next) = PATTERN (next);
3772 break;
3774 case 1:
3775 if (!ORIG_PAT (next))
3776 /* If we gonna to overwrite the original pattern of insn,
3777 save it. */
3778 ORIG_PAT (next) = PATTERN (next);
3780 haifa_change_pattern (next, new_pat);
3781 break;
3783 default:
3784 gcc_unreachable ();
3788 /* We need to restore pattern only if (*ts == 0), because otherwise it is
3789 either correct (*ts & SPECULATIVE),
3790 or we simply don't care (*ts & HARD_DEP). */
3792 gcc_assert (!ORIG_PAT (next)
3793 || !IS_SPECULATION_BRANCHY_CHECK_P (next));
3795 if (*ts & HARD_DEP)
3797 /* We can't assert (QUEUE_INDEX (next) == QUEUE_NOWHERE) here because
3798 control-speculative NEXT could have been discarded by sched-rgn.c
3799 (the same case as when discarded by can_schedule_ready_p ()). */
3800 /*gcc_assert (QUEUE_INDEX (next) == QUEUE_NOWHERE);*/
3802 change_queue_index (next, QUEUE_NOWHERE);
3803 return -1;
3805 else if (!(*ts & BEGIN_SPEC) && ORIG_PAT (next) && !IS_SPECULATION_CHECK_P (next))
3806 /* We should change pattern of every previously speculative
3807 instruction - and we determine if NEXT was speculative by using
3808 ORIG_PAT field. Except one case - speculation checks have ORIG_PAT
3809 pat too, so skip them. */
3811 haifa_change_pattern (next, ORIG_PAT (next));
3812 ORIG_PAT (next) = 0;
3815 if (sched_verbose >= 2)
3817 int s = TODO_SPEC (next);
3819 fprintf (sched_dump, ";;\t\tdependencies resolved: insn %s",
3820 (*current_sched_info->print_insn) (next, 0));
3822 if (spec_info && spec_info->dump)
3824 if (s & BEGIN_DATA)
3825 fprintf (spec_info->dump, "; data-spec;");
3826 if (s & BEGIN_CONTROL)
3827 fprintf (spec_info->dump, "; control-spec;");
3828 if (s & BE_IN_CONTROL)
3829 fprintf (spec_info->dump, "; in-control-spec;");
3832 fprintf (sched_dump, "\n");
3835 adjust_priority (next);
3837 return fix_tick_ready (next);
3840 /* Calculate INSN_TICK of NEXT and add it to either ready or queue list. */
3841 static int
3842 fix_tick_ready (rtx next)
3844 int tick, delay;
3846 if (!sd_lists_empty_p (next, SD_LIST_RES_BACK))
3848 int full_p;
3849 sd_iterator_def sd_it;
3850 dep_t dep;
3852 tick = INSN_TICK (next);
3853 /* if tick is not equal to INVALID_TICK, then update
3854 INSN_TICK of NEXT with the most recent resolved dependence
3855 cost. Otherwise, recalculate from scratch. */
3856 full_p = (tick == INVALID_TICK);
3858 FOR_EACH_DEP (next, SD_LIST_RES_BACK, sd_it, dep)
3860 rtx pro = DEP_PRO (dep);
3861 int tick1;
3863 gcc_assert (INSN_TICK (pro) >= MIN_TICK);
3865 tick1 = INSN_TICK (pro) + dep_cost (dep);
3866 if (tick1 > tick)
3867 tick = tick1;
3869 if (!full_p)
3870 break;
3873 else
3874 tick = -1;
3876 INSN_TICK (next) = tick;
3878 delay = tick - clock_var;
3879 if (delay <= 0 || sched_pressure_p)
3880 delay = QUEUE_READY;
3882 change_queue_index (next, delay);
3884 return delay;
3887 /* Move NEXT to the proper queue list with (DELAY >= 1),
3888 or add it to the ready list (DELAY == QUEUE_READY),
3889 or remove it from ready and queue lists at all (DELAY == QUEUE_NOWHERE). */
3890 static void
3891 change_queue_index (rtx next, int delay)
3893 int i = QUEUE_INDEX (next);
3895 gcc_assert (QUEUE_NOWHERE <= delay && delay <= max_insn_queue_index
3896 && delay != 0);
3897 gcc_assert (i != QUEUE_SCHEDULED);
3899 if ((delay > 0 && NEXT_Q_AFTER (q_ptr, delay) == i)
3900 || (delay < 0 && delay == i))
3901 /* We have nothing to do. */
3902 return;
3904 /* Remove NEXT from wherever it is now. */
3905 if (i == QUEUE_READY)
3906 ready_remove_insn (next);
3907 else if (i >= 0)
3908 queue_remove (next);
3910 /* Add it to the proper place. */
3911 if (delay == QUEUE_READY)
3912 ready_add (readyp, next, false);
3913 else if (delay >= 1)
3914 queue_insn (next, delay);
3916 if (sched_verbose >= 2)
3918 fprintf (sched_dump, ";;\t\ttick updated: insn %s",
3919 (*current_sched_info->print_insn) (next, 0));
3921 if (delay == QUEUE_READY)
3922 fprintf (sched_dump, " into ready\n");
3923 else if (delay >= 1)
3924 fprintf (sched_dump, " into queue with cost=%d\n", delay);
3925 else
3926 fprintf (sched_dump, " removed from ready or queue lists\n");
3930 static int sched_ready_n_insns = -1;
3932 /* Initialize per region data structures. */
3933 void
3934 sched_extend_ready_list (int new_sched_ready_n_insns)
3936 int i;
3938 if (sched_ready_n_insns == -1)
3939 /* At the first call we need to initialize one more choice_stack
3940 entry. */
3942 i = 0;
3943 sched_ready_n_insns = 0;
3945 else
3946 i = sched_ready_n_insns + 1;
3948 ready.veclen = new_sched_ready_n_insns + issue_rate;
3949 ready.vec = XRESIZEVEC (rtx, ready.vec, ready.veclen);
3951 gcc_assert (new_sched_ready_n_insns >= sched_ready_n_insns);
3953 ready_try = (char *) xrecalloc (ready_try, new_sched_ready_n_insns,
3954 sched_ready_n_insns, sizeof (*ready_try));
3956 /* We allocate +1 element to save initial state in the choice_stack[0]
3957 entry. */
3958 choice_stack = XRESIZEVEC (struct choice_entry, choice_stack,
3959 new_sched_ready_n_insns + 1);
3961 for (; i <= new_sched_ready_n_insns; i++)
3962 choice_stack[i].state = xmalloc (dfa_state_size);
3964 sched_ready_n_insns = new_sched_ready_n_insns;
3967 /* Free per region data structures. */
3968 void
3969 sched_finish_ready_list (void)
3971 int i;
3973 free (ready.vec);
3974 ready.vec = NULL;
3975 ready.veclen = 0;
3977 free (ready_try);
3978 ready_try = NULL;
3980 for (i = 0; i <= sched_ready_n_insns; i++)
3981 free (choice_stack [i].state);
3982 free (choice_stack);
3983 choice_stack = NULL;
3985 sched_ready_n_insns = -1;
3988 static int
3989 haifa_luid_for_non_insn (rtx x)
3991 gcc_assert (NOTE_P (x) || LABEL_P (x));
3993 return 0;
3996 /* Generates recovery code for INSN. */
3997 static void
3998 generate_recovery_code (rtx insn)
4000 if (TODO_SPEC (insn) & BEGIN_SPEC)
4001 begin_speculative_block (insn);
4003 /* Here we have insn with no dependencies to
4004 instructions other then CHECK_SPEC ones. */
4006 if (TODO_SPEC (insn) & BE_IN_SPEC)
4007 add_to_speculative_block (insn);
4010 /* Helper function.
4011 Tries to add speculative dependencies of type FS between instructions
4012 in deps_list L and TWIN. */
4013 static void
4014 process_insn_forw_deps_be_in_spec (rtx insn, rtx twin, ds_t fs)
4016 sd_iterator_def sd_it;
4017 dep_t dep;
4019 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
4021 ds_t ds;
4022 rtx consumer;
4024 consumer = DEP_CON (dep);
4026 ds = DEP_STATUS (dep);
4028 if (/* If we want to create speculative dep. */
4030 /* And we can do that because this is a true dep. */
4031 && (ds & DEP_TYPES) == DEP_TRUE)
4033 gcc_assert (!(ds & BE_IN_SPEC));
4035 if (/* If this dep can be overcome with 'begin speculation'. */
4036 ds & BEGIN_SPEC)
4037 /* Then we have a choice: keep the dep 'begin speculative'
4038 or transform it into 'be in speculative'. */
4040 if (/* In try_ready we assert that if insn once became ready
4041 it can be removed from the ready (or queue) list only
4042 due to backend decision. Hence we can't let the
4043 probability of the speculative dep to decrease. */
4044 ds_weak (ds) <= ds_weak (fs))
4046 ds_t new_ds;
4048 new_ds = (ds & ~BEGIN_SPEC) | fs;
4050 if (/* consumer can 'be in speculative'. */
4051 sched_insn_is_legitimate_for_speculation_p (consumer,
4052 new_ds))
4053 /* Transform it to be in speculative. */
4054 ds = new_ds;
4057 else
4058 /* Mark the dep as 'be in speculative'. */
4059 ds |= fs;
4063 dep_def _new_dep, *new_dep = &_new_dep;
4065 init_dep_1 (new_dep, twin, consumer, DEP_TYPE (dep), ds);
4066 sd_add_dep (new_dep, false);
4071 /* Generates recovery code for BEGIN speculative INSN. */
4072 static void
4073 begin_speculative_block (rtx insn)
4075 if (TODO_SPEC (insn) & BEGIN_DATA)
4076 nr_begin_data++;
4077 if (TODO_SPEC (insn) & BEGIN_CONTROL)
4078 nr_begin_control++;
4080 create_check_block_twin (insn, false);
4082 TODO_SPEC (insn) &= ~BEGIN_SPEC;
4085 static void haifa_init_insn (rtx);
4087 /* Generates recovery code for BE_IN speculative INSN. */
4088 static void
4089 add_to_speculative_block (rtx insn)
4091 ds_t ts;
4092 sd_iterator_def sd_it;
4093 dep_t dep;
4094 rtx twins = NULL;
4095 rtx_vec_t priorities_roots;
4097 ts = TODO_SPEC (insn);
4098 gcc_assert (!(ts & ~BE_IN_SPEC));
4100 if (ts & BE_IN_DATA)
4101 nr_be_in_data++;
4102 if (ts & BE_IN_CONTROL)
4103 nr_be_in_control++;
4105 TODO_SPEC (insn) &= ~BE_IN_SPEC;
4106 gcc_assert (!TODO_SPEC (insn));
4108 DONE_SPEC (insn) |= ts;
4110 /* First we convert all simple checks to branchy. */
4111 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4112 sd_iterator_cond (&sd_it, &dep);)
4114 rtx check = DEP_PRO (dep);
4116 if (IS_SPECULATION_SIMPLE_CHECK_P (check))
4118 create_check_block_twin (check, true);
4120 /* Restart search. */
4121 sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4123 else
4124 /* Continue search. */
4125 sd_iterator_next (&sd_it);
4128 priorities_roots = NULL;
4129 clear_priorities (insn, &priorities_roots);
4131 while (1)
4133 rtx check, twin;
4134 basic_block rec;
4136 /* Get the first backward dependency of INSN. */
4137 sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4138 if (!sd_iterator_cond (&sd_it, &dep))
4139 /* INSN has no backward dependencies left. */
4140 break;
4142 gcc_assert ((DEP_STATUS (dep) & BEGIN_SPEC) == 0
4143 && (DEP_STATUS (dep) & BE_IN_SPEC) != 0
4144 && (DEP_STATUS (dep) & DEP_TYPES) == DEP_TRUE);
4146 check = DEP_PRO (dep);
4148 gcc_assert (!IS_SPECULATION_CHECK_P (check) && !ORIG_PAT (check)
4149 && QUEUE_INDEX (check) == QUEUE_NOWHERE);
4151 rec = BLOCK_FOR_INSN (check);
4153 twin = emit_insn_before (copy_insn (PATTERN (insn)), BB_END (rec));
4154 haifa_init_insn (twin);
4156 sd_copy_back_deps (twin, insn, true);
4158 if (sched_verbose && spec_info->dump)
4159 /* INSN_BB (insn) isn't determined for twin insns yet.
4160 So we can't use current_sched_info->print_insn. */
4161 fprintf (spec_info->dump, ";;\t\tGenerated twin insn : %d/rec%d\n",
4162 INSN_UID (twin), rec->index);
4164 twins = alloc_INSN_LIST (twin, twins);
4166 /* Add dependences between TWIN and all appropriate
4167 instructions from REC. */
4168 FOR_EACH_DEP (insn, SD_LIST_SPEC_BACK, sd_it, dep)
4170 rtx pro = DEP_PRO (dep);
4172 gcc_assert (DEP_TYPE (dep) == REG_DEP_TRUE);
4174 /* INSN might have dependencies from the instructions from
4175 several recovery blocks. At this iteration we process those
4176 producers that reside in REC. */
4177 if (BLOCK_FOR_INSN (pro) == rec)
4179 dep_def _new_dep, *new_dep = &_new_dep;
4181 init_dep (new_dep, pro, twin, REG_DEP_TRUE);
4182 sd_add_dep (new_dep, false);
4186 process_insn_forw_deps_be_in_spec (insn, twin, ts);
4188 /* Remove all dependencies between INSN and insns in REC. */
4189 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4190 sd_iterator_cond (&sd_it, &dep);)
4192 rtx pro = DEP_PRO (dep);
4194 if (BLOCK_FOR_INSN (pro) == rec)
4195 sd_delete_dep (sd_it);
4196 else
4197 sd_iterator_next (&sd_it);
4201 /* We couldn't have added the dependencies between INSN and TWINS earlier
4202 because that would make TWINS appear in the INSN_BACK_DEPS (INSN). */
4203 while (twins)
4205 rtx twin;
4207 twin = XEXP (twins, 0);
4210 dep_def _new_dep, *new_dep = &_new_dep;
4212 init_dep (new_dep, insn, twin, REG_DEP_OUTPUT);
4213 sd_add_dep (new_dep, false);
4216 twin = XEXP (twins, 1);
4217 free_INSN_LIST_node (twins);
4218 twins = twin;
4221 calc_priorities (priorities_roots);
4222 VEC_free (rtx, heap, priorities_roots);
4225 /* Extends and fills with zeros (only the new part) array pointed to by P. */
4226 void *
4227 xrecalloc (void *p, size_t new_nmemb, size_t old_nmemb, size_t size)
4229 gcc_assert (new_nmemb >= old_nmemb);
4230 p = XRESIZEVAR (void, p, new_nmemb * size);
4231 memset (((char *) p) + old_nmemb * size, 0, (new_nmemb - old_nmemb) * size);
4232 return p;
4235 /* Helper function.
4236 Find fallthru edge from PRED. */
4237 edge
4238 find_fallthru_edge (basic_block pred)
4240 edge e;
4241 edge_iterator ei;
4242 basic_block succ;
4244 succ = pred->next_bb;
4245 gcc_assert (succ->prev_bb == pred);
4247 if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
4249 FOR_EACH_EDGE (e, ei, pred->succs)
4250 if (e->flags & EDGE_FALLTHRU)
4252 gcc_assert (e->dest == succ);
4253 return e;
4256 else
4258 FOR_EACH_EDGE (e, ei, succ->preds)
4259 if (e->flags & EDGE_FALLTHRU)
4261 gcc_assert (e->src == pred);
4262 return e;
4266 return NULL;
4269 /* Extend per basic block data structures. */
4270 static void
4271 sched_extend_bb (void)
4273 rtx insn;
4275 /* The following is done to keep current_sched_info->next_tail non null. */
4276 insn = BB_END (EXIT_BLOCK_PTR->prev_bb);
4277 if (NEXT_INSN (insn) == 0
4278 || (!NOTE_P (insn)
4279 && !LABEL_P (insn)
4280 /* Don't emit a NOTE if it would end up before a BARRIER. */
4281 && !BARRIER_P (NEXT_INSN (insn))))
4283 rtx note = emit_note_after (NOTE_INSN_DELETED, insn);
4284 /* Make insn appear outside BB. */
4285 set_block_for_insn (note, NULL);
4286 BB_END (EXIT_BLOCK_PTR->prev_bb) = insn;
4290 /* Init per basic block data structures. */
4291 void
4292 sched_init_bbs (void)
4294 sched_extend_bb ();
4297 /* Initialize BEFORE_RECOVERY variable. */
4298 static void
4299 init_before_recovery (basic_block *before_recovery_ptr)
4301 basic_block last;
4302 edge e;
4304 last = EXIT_BLOCK_PTR->prev_bb;
4305 e = find_fallthru_edge (last);
4307 if (e)
4309 /* We create two basic blocks:
4310 1. Single instruction block is inserted right after E->SRC
4311 and has jump to
4312 2. Empty block right before EXIT_BLOCK.
4313 Between these two blocks recovery blocks will be emitted. */
4315 basic_block single, empty;
4316 rtx x, label;
4318 /* If the fallthrough edge to exit we've found is from the block we've
4319 created before, don't do anything more. */
4320 if (last == after_recovery)
4321 return;
4323 adding_bb_to_current_region_p = false;
4325 single = sched_create_empty_bb (last);
4326 empty = sched_create_empty_bb (single);
4328 /* Add new blocks to the root loop. */
4329 if (current_loops != NULL)
4331 add_bb_to_loop (single, VEC_index (loop_p, current_loops->larray, 0));
4332 add_bb_to_loop (empty, VEC_index (loop_p, current_loops->larray, 0));
4335 single->count = last->count;
4336 empty->count = last->count;
4337 single->frequency = last->frequency;
4338 empty->frequency = last->frequency;
4339 BB_COPY_PARTITION (single, last);
4340 BB_COPY_PARTITION (empty, last);
4342 redirect_edge_succ (e, single);
4343 make_single_succ_edge (single, empty, 0);
4344 make_single_succ_edge (empty, EXIT_BLOCK_PTR,
4345 EDGE_FALLTHRU | EDGE_CAN_FALLTHRU);
4347 label = block_label (empty);
4348 x = emit_jump_insn_after (gen_jump (label), BB_END (single));
4349 JUMP_LABEL (x) = label;
4350 LABEL_NUSES (label)++;
4351 haifa_init_insn (x);
4353 emit_barrier_after (x);
4355 sched_init_only_bb (empty, NULL);
4356 sched_init_only_bb (single, NULL);
4357 sched_extend_bb ();
4359 adding_bb_to_current_region_p = true;
4360 before_recovery = single;
4361 after_recovery = empty;
4363 if (before_recovery_ptr)
4364 *before_recovery_ptr = before_recovery;
4366 if (sched_verbose >= 2 && spec_info->dump)
4367 fprintf (spec_info->dump,
4368 ";;\t\tFixed fallthru to EXIT : %d->>%d->%d->>EXIT\n",
4369 last->index, single->index, empty->index);
4371 else
4372 before_recovery = last;
4375 /* Returns new recovery block. */
4376 basic_block
4377 sched_create_recovery_block (basic_block *before_recovery_ptr)
4379 rtx label;
4380 rtx barrier;
4381 basic_block rec;
4383 haifa_recovery_bb_recently_added_p = true;
4384 haifa_recovery_bb_ever_added_p = true;
4386 init_before_recovery (before_recovery_ptr);
4388 barrier = get_last_bb_insn (before_recovery);
4389 gcc_assert (BARRIER_P (barrier));
4391 label = emit_label_after (gen_label_rtx (), barrier);
4393 rec = create_basic_block (label, label, before_recovery);
4395 /* A recovery block always ends with an unconditional jump. */
4396 emit_barrier_after (BB_END (rec));
4398 if (BB_PARTITION (before_recovery) != BB_UNPARTITIONED)
4399 BB_SET_PARTITION (rec, BB_COLD_PARTITION);
4401 if (sched_verbose && spec_info->dump)
4402 fprintf (spec_info->dump, ";;\t\tGenerated recovery block rec%d\n",
4403 rec->index);
4405 return rec;
4408 /* Create edges: FIRST_BB -> REC; FIRST_BB -> SECOND_BB; REC -> SECOND_BB
4409 and emit necessary jumps. */
4410 void
4411 sched_create_recovery_edges (basic_block first_bb, basic_block rec,
4412 basic_block second_bb)
4414 rtx label;
4415 rtx jump;
4416 int edge_flags;
4418 /* This is fixing of incoming edge. */
4419 /* ??? Which other flags should be specified? */
4420 if (BB_PARTITION (first_bb) != BB_PARTITION (rec))
4421 /* Partition type is the same, if it is "unpartitioned". */
4422 edge_flags = EDGE_CROSSING;
4423 else
4424 edge_flags = 0;
4426 make_edge (first_bb, rec, edge_flags);
4427 label = block_label (second_bb);
4428 jump = emit_jump_insn_after (gen_jump (label), BB_END (rec));
4429 JUMP_LABEL (jump) = label;
4430 LABEL_NUSES (label)++;
4432 if (BB_PARTITION (second_bb) != BB_PARTITION (rec))
4433 /* Partition type is the same, if it is "unpartitioned". */
4435 /* Rewritten from cfgrtl.c. */
4436 if (flag_reorder_blocks_and_partition
4437 && targetm.have_named_sections)
4439 /* We don't need the same note for the check because
4440 any_condjump_p (check) == true. */
4441 add_reg_note (jump, REG_CROSSING_JUMP, NULL_RTX);
4443 edge_flags = EDGE_CROSSING;
4445 else
4446 edge_flags = 0;
4448 make_single_succ_edge (rec, second_bb, edge_flags);
4451 /* This function creates recovery code for INSN. If MUTATE_P is nonzero,
4452 INSN is a simple check, that should be converted to branchy one. */
4453 static void
4454 create_check_block_twin (rtx insn, bool mutate_p)
4456 basic_block rec;
4457 rtx label, check, twin;
4458 ds_t fs;
4459 sd_iterator_def sd_it;
4460 dep_t dep;
4461 dep_def _new_dep, *new_dep = &_new_dep;
4462 ds_t todo_spec;
4464 gcc_assert (ORIG_PAT (insn) != NULL_RTX);
4466 if (!mutate_p)
4467 todo_spec = TODO_SPEC (insn);
4468 else
4470 gcc_assert (IS_SPECULATION_SIMPLE_CHECK_P (insn)
4471 && (TODO_SPEC (insn) & SPECULATIVE) == 0);
4473 todo_spec = CHECK_SPEC (insn);
4476 todo_spec &= SPECULATIVE;
4478 /* Create recovery block. */
4479 if (mutate_p || targetm.sched.needs_block_p (todo_spec))
4481 rec = sched_create_recovery_block (NULL);
4482 label = BB_HEAD (rec);
4484 else
4486 rec = EXIT_BLOCK_PTR;
4487 label = NULL_RTX;
4490 /* Emit CHECK. */
4491 check = targetm.sched.gen_spec_check (insn, label, todo_spec);
4493 if (rec != EXIT_BLOCK_PTR)
4495 /* To have mem_reg alive at the beginning of second_bb,
4496 we emit check BEFORE insn, so insn after splitting
4497 insn will be at the beginning of second_bb, which will
4498 provide us with the correct life information. */
4499 check = emit_jump_insn_before (check, insn);
4500 JUMP_LABEL (check) = label;
4501 LABEL_NUSES (label)++;
4503 else
4504 check = emit_insn_before (check, insn);
4506 /* Extend data structures. */
4507 haifa_init_insn (check);
4509 /* CHECK is being added to current region. Extend ready list. */
4510 gcc_assert (sched_ready_n_insns != -1);
4511 sched_extend_ready_list (sched_ready_n_insns + 1);
4513 if (current_sched_info->add_remove_insn)
4514 current_sched_info->add_remove_insn (insn, 0);
4516 RECOVERY_BLOCK (check) = rec;
4518 if (sched_verbose && spec_info->dump)
4519 fprintf (spec_info->dump, ";;\t\tGenerated check insn : %s\n",
4520 (*current_sched_info->print_insn) (check, 0));
4522 gcc_assert (ORIG_PAT (insn));
4524 /* Initialize TWIN (twin is a duplicate of original instruction
4525 in the recovery block). */
4526 if (rec != EXIT_BLOCK_PTR)
4528 sd_iterator_def sd_it;
4529 dep_t dep;
4531 FOR_EACH_DEP (insn, SD_LIST_RES_BACK, sd_it, dep)
4532 if ((DEP_STATUS (dep) & DEP_OUTPUT) != 0)
4534 struct _dep _dep2, *dep2 = &_dep2;
4536 init_dep (dep2, DEP_PRO (dep), check, REG_DEP_TRUE);
4538 sd_add_dep (dep2, true);
4541 twin = emit_insn_after (ORIG_PAT (insn), BB_END (rec));
4542 haifa_init_insn (twin);
4544 if (sched_verbose && spec_info->dump)
4545 /* INSN_BB (insn) isn't determined for twin insns yet.
4546 So we can't use current_sched_info->print_insn. */
4547 fprintf (spec_info->dump, ";;\t\tGenerated twin insn : %d/rec%d\n",
4548 INSN_UID (twin), rec->index);
4550 else
4552 ORIG_PAT (check) = ORIG_PAT (insn);
4553 HAS_INTERNAL_DEP (check) = 1;
4554 twin = check;
4555 /* ??? We probably should change all OUTPUT dependencies to
4556 (TRUE | OUTPUT). */
4559 /* Copy all resolved back dependencies of INSN to TWIN. This will
4560 provide correct value for INSN_TICK (TWIN). */
4561 sd_copy_back_deps (twin, insn, true);
4563 if (rec != EXIT_BLOCK_PTR)
4564 /* In case of branchy check, fix CFG. */
4566 basic_block first_bb, second_bb;
4567 rtx jump;
4569 first_bb = BLOCK_FOR_INSN (check);
4570 second_bb = sched_split_block (first_bb, check);
4572 sched_create_recovery_edges (first_bb, rec, second_bb);
4574 sched_init_only_bb (second_bb, first_bb);
4575 sched_init_only_bb (rec, EXIT_BLOCK_PTR);
4577 jump = BB_END (rec);
4578 haifa_init_insn (jump);
4581 /* Move backward dependences from INSN to CHECK and
4582 move forward dependences from INSN to TWIN. */
4584 /* First, create dependencies between INSN's producers and CHECK & TWIN. */
4585 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
4587 rtx pro = DEP_PRO (dep);
4588 ds_t ds;
4590 /* If BEGIN_DATA: [insn ~~TRUE~~> producer]:
4591 check --TRUE--> producer ??? or ANTI ???
4592 twin --TRUE--> producer
4593 twin --ANTI--> check
4595 If BEGIN_CONTROL: [insn ~~ANTI~~> producer]:
4596 check --ANTI--> producer
4597 twin --ANTI--> producer
4598 twin --ANTI--> check
4600 If BE_IN_SPEC: [insn ~~TRUE~~> producer]:
4601 check ~~TRUE~~> producer
4602 twin ~~TRUE~~> producer
4603 twin --ANTI--> check */
4605 ds = DEP_STATUS (dep);
4607 if (ds & BEGIN_SPEC)
4609 gcc_assert (!mutate_p);
4610 ds &= ~BEGIN_SPEC;
4613 init_dep_1 (new_dep, pro, check, DEP_TYPE (dep), ds);
4614 sd_add_dep (new_dep, false);
4616 if (rec != EXIT_BLOCK_PTR)
4618 DEP_CON (new_dep) = twin;
4619 sd_add_dep (new_dep, false);
4623 /* Second, remove backward dependencies of INSN. */
4624 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4625 sd_iterator_cond (&sd_it, &dep);)
4627 if ((DEP_STATUS (dep) & BEGIN_SPEC)
4628 || mutate_p)
4629 /* We can delete this dep because we overcome it with
4630 BEGIN_SPECULATION. */
4631 sd_delete_dep (sd_it);
4632 else
4633 sd_iterator_next (&sd_it);
4636 /* Future Speculations. Determine what BE_IN speculations will be like. */
4637 fs = 0;
4639 /* Fields (DONE_SPEC (x) & BEGIN_SPEC) and CHECK_SPEC (x) are set only
4640 here. */
4642 gcc_assert (!DONE_SPEC (insn));
4644 if (!mutate_p)
4646 ds_t ts = TODO_SPEC (insn);
4648 DONE_SPEC (insn) = ts & BEGIN_SPEC;
4649 CHECK_SPEC (check) = ts & BEGIN_SPEC;
4651 /* Luckiness of future speculations solely depends upon initial
4652 BEGIN speculation. */
4653 if (ts & BEGIN_DATA)
4654 fs = set_dep_weak (fs, BE_IN_DATA, get_dep_weak (ts, BEGIN_DATA));
4655 if (ts & BEGIN_CONTROL)
4656 fs = set_dep_weak (fs, BE_IN_CONTROL,
4657 get_dep_weak (ts, BEGIN_CONTROL));
4659 else
4660 CHECK_SPEC (check) = CHECK_SPEC (insn);
4662 /* Future speculations: call the helper. */
4663 process_insn_forw_deps_be_in_spec (insn, twin, fs);
4665 if (rec != EXIT_BLOCK_PTR)
4667 /* Which types of dependencies should we use here is,
4668 generally, machine-dependent question... But, for now,
4669 it is not. */
4671 if (!mutate_p)
4673 init_dep (new_dep, insn, check, REG_DEP_TRUE);
4674 sd_add_dep (new_dep, false);
4676 init_dep (new_dep, insn, twin, REG_DEP_OUTPUT);
4677 sd_add_dep (new_dep, false);
4679 else
4681 if (spec_info->dump)
4682 fprintf (spec_info->dump, ";;\t\tRemoved simple check : %s\n",
4683 (*current_sched_info->print_insn) (insn, 0));
4685 /* Remove all dependencies of the INSN. */
4687 sd_it = sd_iterator_start (insn, (SD_LIST_FORW
4688 | SD_LIST_BACK
4689 | SD_LIST_RES_BACK));
4690 while (sd_iterator_cond (&sd_it, &dep))
4691 sd_delete_dep (sd_it);
4694 /* If former check (INSN) already was moved to the ready (or queue)
4695 list, add new check (CHECK) there too. */
4696 if (QUEUE_INDEX (insn) != QUEUE_NOWHERE)
4697 try_ready (check);
4699 /* Remove old check from instruction stream and free its
4700 data. */
4701 sched_remove_insn (insn);
4704 init_dep (new_dep, check, twin, REG_DEP_ANTI);
4705 sd_add_dep (new_dep, false);
4707 else
4709 init_dep_1 (new_dep, insn, check, REG_DEP_TRUE, DEP_TRUE | DEP_OUTPUT);
4710 sd_add_dep (new_dep, false);
4713 if (!mutate_p)
4714 /* Fix priorities. If MUTATE_P is nonzero, this is not necessary,
4715 because it'll be done later in add_to_speculative_block. */
4717 rtx_vec_t priorities_roots = NULL;
4719 clear_priorities (twin, &priorities_roots);
4720 calc_priorities (priorities_roots);
4721 VEC_free (rtx, heap, priorities_roots);
4725 /* Removes dependency between instructions in the recovery block REC
4726 and usual region instructions. It keeps inner dependences so it
4727 won't be necessary to recompute them. */
4728 static void
4729 fix_recovery_deps (basic_block rec)
4731 rtx note, insn, jump, ready_list = 0;
4732 bitmap_head in_ready;
4733 rtx link;
4735 bitmap_initialize (&in_ready, 0);
4737 /* NOTE - a basic block note. */
4738 note = NEXT_INSN (BB_HEAD (rec));
4739 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4740 insn = BB_END (rec);
4741 gcc_assert (JUMP_P (insn));
4742 insn = PREV_INSN (insn);
4746 sd_iterator_def sd_it;
4747 dep_t dep;
4749 for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
4750 sd_iterator_cond (&sd_it, &dep);)
4752 rtx consumer = DEP_CON (dep);
4754 if (BLOCK_FOR_INSN (consumer) != rec)
4756 sd_delete_dep (sd_it);
4758 if (!bitmap_bit_p (&in_ready, INSN_LUID (consumer)))
4760 ready_list = alloc_INSN_LIST (consumer, ready_list);
4761 bitmap_set_bit (&in_ready, INSN_LUID (consumer));
4764 else
4766 gcc_assert ((DEP_STATUS (dep) & DEP_TYPES) == DEP_TRUE);
4768 sd_iterator_next (&sd_it);
4772 insn = PREV_INSN (insn);
4774 while (insn != note);
4776 bitmap_clear (&in_ready);
4778 /* Try to add instructions to the ready or queue list. */
4779 for (link = ready_list; link; link = XEXP (link, 1))
4780 try_ready (XEXP (link, 0));
4781 free_INSN_LIST_list (&ready_list);
4783 /* Fixing jump's dependences. */
4784 insn = BB_HEAD (rec);
4785 jump = BB_END (rec);
4787 gcc_assert (LABEL_P (insn));
4788 insn = NEXT_INSN (insn);
4790 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
4791 add_jump_dependencies (insn, jump);
4794 /* Change pattern of INSN to NEW_PAT. */
4795 void
4796 sched_change_pattern (rtx insn, rtx new_pat)
4798 int t;
4800 t = validate_change (insn, &PATTERN (insn), new_pat, 0);
4801 gcc_assert (t);
4802 dfa_clear_single_insn_cache (insn);
4805 /* Change pattern of INSN to NEW_PAT. Invalidate cached haifa
4806 instruction data. */
4807 static void
4808 haifa_change_pattern (rtx insn, rtx new_pat)
4810 sched_change_pattern (insn, new_pat);
4812 /* Invalidate INSN_COST, so it'll be recalculated. */
4813 INSN_COST (insn) = -1;
4814 /* Invalidate INSN_TICK, so it'll be recalculated. */
4815 INSN_TICK (insn) = INVALID_TICK;
4818 /* -1 - can't speculate,
4819 0 - for speculation with REQUEST mode it is OK to use
4820 current instruction pattern,
4821 1 - need to change pattern for *NEW_PAT to be speculative. */
4823 sched_speculate_insn (rtx insn, ds_t request, rtx *new_pat)
4825 gcc_assert (current_sched_info->flags & DO_SPECULATION
4826 && (request & SPECULATIVE)
4827 && sched_insn_is_legitimate_for_speculation_p (insn, request));
4829 if ((request & spec_info->mask) != request)
4830 return -1;
4832 if (request & BE_IN_SPEC
4833 && !(request & BEGIN_SPEC))
4834 return 0;
4836 return targetm.sched.speculate_insn (insn, request, new_pat);
4839 static int
4840 haifa_speculate_insn (rtx insn, ds_t request, rtx *new_pat)
4842 gcc_assert (sched_deps_info->generate_spec_deps
4843 && !IS_SPECULATION_CHECK_P (insn));
4845 if (HAS_INTERNAL_DEP (insn)
4846 || SCHED_GROUP_P (insn))
4847 return -1;
4849 return sched_speculate_insn (insn, request, new_pat);
4852 /* Print some information about block BB, which starts with HEAD and
4853 ends with TAIL, before scheduling it.
4854 I is zero, if scheduler is about to start with the fresh ebb. */
4855 static void
4856 dump_new_block_header (int i, basic_block bb, rtx head, rtx tail)
4858 if (!i)
4859 fprintf (sched_dump,
4860 ";; ======================================================\n");
4861 else
4862 fprintf (sched_dump,
4863 ";; =====================ADVANCING TO=====================\n");
4864 fprintf (sched_dump,
4865 ";; -- basic block %d from %d to %d -- %s reload\n",
4866 bb->index, INSN_UID (head), INSN_UID (tail),
4867 (reload_completed ? "after" : "before"));
4868 fprintf (sched_dump,
4869 ";; ======================================================\n");
4870 fprintf (sched_dump, "\n");
4873 /* Unlink basic block notes and labels and saves them, so they
4874 can be easily restored. We unlink basic block notes in EBB to
4875 provide back-compatibility with the previous code, as target backends
4876 assume, that there'll be only instructions between
4877 current_sched_info->{head and tail}. We restore these notes as soon
4878 as we can.
4879 FIRST (LAST) is the first (last) basic block in the ebb.
4880 NB: In usual case (FIRST == LAST) nothing is really done. */
4881 void
4882 unlink_bb_notes (basic_block first, basic_block last)
4884 /* We DON'T unlink basic block notes of the first block in the ebb. */
4885 if (first == last)
4886 return;
4888 bb_header = XNEWVEC (rtx, last_basic_block);
4890 /* Make a sentinel. */
4891 if (last->next_bb != EXIT_BLOCK_PTR)
4892 bb_header[last->next_bb->index] = 0;
4894 first = first->next_bb;
4897 rtx prev, label, note, next;
4899 label = BB_HEAD (last);
4900 if (LABEL_P (label))
4901 note = NEXT_INSN (label);
4902 else
4903 note = label;
4904 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4906 prev = PREV_INSN (label);
4907 next = NEXT_INSN (note);
4908 gcc_assert (prev && next);
4910 NEXT_INSN (prev) = next;
4911 PREV_INSN (next) = prev;
4913 bb_header[last->index] = label;
4915 if (last == first)
4916 break;
4918 last = last->prev_bb;
4920 while (1);
4923 /* Restore basic block notes.
4924 FIRST is the first basic block in the ebb. */
4925 static void
4926 restore_bb_notes (basic_block first)
4928 if (!bb_header)
4929 return;
4931 /* We DON'T unlink basic block notes of the first block in the ebb. */
4932 first = first->next_bb;
4933 /* Remember: FIRST is actually a second basic block in the ebb. */
4935 while (first != EXIT_BLOCK_PTR
4936 && bb_header[first->index])
4938 rtx prev, label, note, next;
4940 label = bb_header[first->index];
4941 prev = PREV_INSN (label);
4942 next = NEXT_INSN (prev);
4944 if (LABEL_P (label))
4945 note = NEXT_INSN (label);
4946 else
4947 note = label;
4948 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4950 bb_header[first->index] = 0;
4952 NEXT_INSN (prev) = label;
4953 NEXT_INSN (note) = next;
4954 PREV_INSN (next) = note;
4956 first = first->next_bb;
4959 free (bb_header);
4960 bb_header = 0;
4963 /* Helper function.
4964 Fix CFG after both in- and inter-block movement of
4965 control_flow_insn_p JUMP. */
4966 static void
4967 fix_jump_move (rtx jump)
4969 basic_block bb, jump_bb, jump_bb_next;
4971 bb = BLOCK_FOR_INSN (PREV_INSN (jump));
4972 jump_bb = BLOCK_FOR_INSN (jump);
4973 jump_bb_next = jump_bb->next_bb;
4975 gcc_assert (common_sched_info->sched_pass_id == SCHED_EBB_PASS
4976 || IS_SPECULATION_BRANCHY_CHECK_P (jump));
4978 if (!NOTE_INSN_BASIC_BLOCK_P (BB_END (jump_bb_next)))
4979 /* if jump_bb_next is not empty. */
4980 BB_END (jump_bb) = BB_END (jump_bb_next);
4982 if (BB_END (bb) != PREV_INSN (jump))
4983 /* Then there are instruction after jump that should be placed
4984 to jump_bb_next. */
4985 BB_END (jump_bb_next) = BB_END (bb);
4986 else
4987 /* Otherwise jump_bb_next is empty. */
4988 BB_END (jump_bb_next) = NEXT_INSN (BB_HEAD (jump_bb_next));
4990 /* To make assertion in move_insn happy. */
4991 BB_END (bb) = PREV_INSN (jump);
4993 update_bb_for_insn (jump_bb_next);
4996 /* Fix CFG after interblock movement of control_flow_insn_p JUMP. */
4997 static void
4998 move_block_after_check (rtx jump)
5000 basic_block bb, jump_bb, jump_bb_next;
5001 VEC(edge,gc) *t;
5003 bb = BLOCK_FOR_INSN (PREV_INSN (jump));
5004 jump_bb = BLOCK_FOR_INSN (jump);
5005 jump_bb_next = jump_bb->next_bb;
5007 update_bb_for_insn (jump_bb);
5009 gcc_assert (IS_SPECULATION_CHECK_P (jump)
5010 || IS_SPECULATION_CHECK_P (BB_END (jump_bb_next)));
5012 unlink_block (jump_bb_next);
5013 link_block (jump_bb_next, bb);
5015 t = bb->succs;
5016 bb->succs = 0;
5017 move_succs (&(jump_bb->succs), bb);
5018 move_succs (&(jump_bb_next->succs), jump_bb);
5019 move_succs (&t, jump_bb_next);
5021 df_mark_solutions_dirty ();
5023 common_sched_info->fix_recovery_cfg
5024 (bb->index, jump_bb->index, jump_bb_next->index);
5027 /* Helper function for move_block_after_check.
5028 This functions attaches edge vector pointed to by SUCCSP to
5029 block TO. */
5030 static void
5031 move_succs (VEC(edge,gc) **succsp, basic_block to)
5033 edge e;
5034 edge_iterator ei;
5036 gcc_assert (to->succs == 0);
5038 to->succs = *succsp;
5040 FOR_EACH_EDGE (e, ei, to->succs)
5041 e->src = to;
5043 *succsp = 0;
5046 /* Remove INSN from the instruction stream.
5047 INSN should have any dependencies. */
5048 static void
5049 sched_remove_insn (rtx insn)
5051 sd_finish_insn (insn);
5053 change_queue_index (insn, QUEUE_NOWHERE);
5054 current_sched_info->add_remove_insn (insn, 1);
5055 remove_insn (insn);
5058 /* Clear priorities of all instructions, that are forward dependent on INSN.
5059 Store in vector pointed to by ROOTS_PTR insns on which priority () should
5060 be invoked to initialize all cleared priorities. */
5061 static void
5062 clear_priorities (rtx insn, rtx_vec_t *roots_ptr)
5064 sd_iterator_def sd_it;
5065 dep_t dep;
5066 bool insn_is_root_p = true;
5068 gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
5070 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
5072 rtx pro = DEP_PRO (dep);
5074 if (INSN_PRIORITY_STATUS (pro) >= 0
5075 && QUEUE_INDEX (insn) != QUEUE_SCHEDULED)
5077 /* If DEP doesn't contribute to priority then INSN itself should
5078 be added to priority roots. */
5079 if (contributes_to_priority_p (dep))
5080 insn_is_root_p = false;
5082 INSN_PRIORITY_STATUS (pro) = -1;
5083 clear_priorities (pro, roots_ptr);
5087 if (insn_is_root_p)
5088 VEC_safe_push (rtx, heap, *roots_ptr, insn);
5091 /* Recompute priorities of instructions, whose priorities might have been
5092 changed. ROOTS is a vector of instructions whose priority computation will
5093 trigger initialization of all cleared priorities. */
5094 static void
5095 calc_priorities (rtx_vec_t roots)
5097 int i;
5098 rtx insn;
5100 for (i = 0; VEC_iterate (rtx, roots, i, insn); i++)
5101 priority (insn);
5105 /* Add dependences between JUMP and other instructions in the recovery
5106 block. INSN is the first insn the recovery block. */
5107 static void
5108 add_jump_dependencies (rtx insn, rtx jump)
5112 insn = NEXT_INSN (insn);
5113 if (insn == jump)
5114 break;
5116 if (dep_list_size (insn) == 0)
5118 dep_def _new_dep, *new_dep = &_new_dep;
5120 init_dep (new_dep, insn, jump, REG_DEP_ANTI);
5121 sd_add_dep (new_dep, false);
5124 while (1);
5126 gcc_assert (!sd_lists_empty_p (jump, SD_LIST_BACK));
5129 /* Return the NOTE_INSN_BASIC_BLOCK of BB. */
5131 bb_note (basic_block bb)
5133 rtx note;
5135 note = BB_HEAD (bb);
5136 if (LABEL_P (note))
5137 note = NEXT_INSN (note);
5139 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
5140 return note;
5143 #ifdef ENABLE_CHECKING
5144 /* Helper function for check_cfg.
5145 Return nonzero, if edge vector pointed to by EL has edge with TYPE in
5146 its flags. */
5147 static int
5148 has_edge_p (VEC(edge,gc) *el, int type)
5150 edge e;
5151 edge_iterator ei;
5153 FOR_EACH_EDGE (e, ei, el)
5154 if (e->flags & type)
5155 return 1;
5156 return 0;
5159 /* Search back, starting at INSN, for an insn that is not a
5160 NOTE_INSN_VAR_LOCATION. Don't search beyond HEAD, and return it if
5161 no such insn can be found. */
5162 static inline rtx
5163 prev_non_location_insn (rtx insn, rtx head)
5165 while (insn != head && NOTE_P (insn)
5166 && NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION)
5167 insn = PREV_INSN (insn);
5169 return insn;
5172 /* Check few properties of CFG between HEAD and TAIL.
5173 If HEAD (TAIL) is NULL check from the beginning (till the end) of the
5174 instruction stream. */
5175 static void
5176 check_cfg (rtx head, rtx tail)
5178 rtx next_tail;
5179 basic_block bb = 0;
5180 int not_first = 0, not_last;
5182 if (head == NULL)
5183 head = get_insns ();
5184 if (tail == NULL)
5185 tail = get_last_insn ();
5186 next_tail = NEXT_INSN (tail);
5190 not_last = head != tail;
5192 if (not_first)
5193 gcc_assert (NEXT_INSN (PREV_INSN (head)) == head);
5194 if (not_last)
5195 gcc_assert (PREV_INSN (NEXT_INSN (head)) == head);
5197 if (LABEL_P (head)
5198 || (NOTE_INSN_BASIC_BLOCK_P (head)
5199 && (!not_first
5200 || (not_first && !LABEL_P (PREV_INSN (head))))))
5202 gcc_assert (bb == 0);
5203 bb = BLOCK_FOR_INSN (head);
5204 if (bb != 0)
5205 gcc_assert (BB_HEAD (bb) == head);
5206 else
5207 /* This is the case of jump table. See inside_basic_block_p (). */
5208 gcc_assert (LABEL_P (head) && !inside_basic_block_p (head));
5211 if (bb == 0)
5213 gcc_assert (!inside_basic_block_p (head));
5214 head = NEXT_INSN (head);
5216 else
5218 gcc_assert (inside_basic_block_p (head)
5219 || NOTE_P (head));
5220 gcc_assert (BLOCK_FOR_INSN (head) == bb);
5222 if (LABEL_P (head))
5224 head = NEXT_INSN (head);
5225 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (head));
5227 else
5229 if (control_flow_insn_p (head))
5231 gcc_assert (prev_non_location_insn (BB_END (bb), head)
5232 == head);
5234 if (any_uncondjump_p (head))
5235 gcc_assert (EDGE_COUNT (bb->succs) == 1
5236 && BARRIER_P (NEXT_INSN (head)));
5237 else if (any_condjump_p (head))
5238 gcc_assert (/* Usual case. */
5239 (EDGE_COUNT (bb->succs) > 1
5240 && !BARRIER_P (NEXT_INSN (head)))
5241 /* Or jump to the next instruction. */
5242 || (EDGE_COUNT (bb->succs) == 1
5243 && (BB_HEAD (EDGE_I (bb->succs, 0)->dest)
5244 == JUMP_LABEL (head))));
5246 if (BB_END (bb) == head)
5248 if (EDGE_COUNT (bb->succs) > 1)
5249 gcc_assert (control_flow_insn_p (prev_non_location_insn
5250 (head, BB_HEAD (bb)))
5251 || has_edge_p (bb->succs, EDGE_COMPLEX));
5252 bb = 0;
5255 head = NEXT_INSN (head);
5259 not_first = 1;
5261 while (head != next_tail);
5263 gcc_assert (bb == 0);
5266 #endif /* ENABLE_CHECKING */
5268 /* Extend per basic block data structures. */
5269 static void
5270 extend_bb (void)
5272 if (sched_scan_info->extend_bb)
5273 sched_scan_info->extend_bb ();
5276 /* Init data for BB. */
5277 static void
5278 init_bb (basic_block bb)
5280 if (sched_scan_info->init_bb)
5281 sched_scan_info->init_bb (bb);
5284 /* Extend per insn data structures. */
5285 static void
5286 extend_insn (void)
5288 if (sched_scan_info->extend_insn)
5289 sched_scan_info->extend_insn ();
5292 /* Init data structures for INSN. */
5293 static void
5294 init_insn (rtx insn)
5296 if (sched_scan_info->init_insn)
5297 sched_scan_info->init_insn (insn);
5300 /* Init all insns in BB. */
5301 static void
5302 init_insns_in_bb (basic_block bb)
5304 rtx insn;
5306 FOR_BB_INSNS (bb, insn)
5307 init_insn (insn);
5310 /* A driver function to add a set of basic blocks (BBS),
5311 a single basic block (BB), a set of insns (INSNS) or a single insn (INSN)
5312 to the scheduling region. */
5313 void
5314 sched_scan (const struct sched_scan_info_def *ssi,
5315 bb_vec_t bbs, basic_block bb, insn_vec_t insns, rtx insn)
5317 sched_scan_info = ssi;
5319 if (bbs != NULL || bb != NULL)
5321 extend_bb ();
5323 if (bbs != NULL)
5325 unsigned i;
5326 basic_block x;
5328 for (i = 0; VEC_iterate (basic_block, bbs, i, x); i++)
5329 init_bb (x);
5332 if (bb != NULL)
5333 init_bb (bb);
5336 extend_insn ();
5338 if (bbs != NULL)
5340 unsigned i;
5341 basic_block x;
5343 for (i = 0; VEC_iterate (basic_block, bbs, i, x); i++)
5344 init_insns_in_bb (x);
5347 if (bb != NULL)
5348 init_insns_in_bb (bb);
5350 if (insns != NULL)
5352 unsigned i;
5353 rtx x;
5355 for (i = 0; VEC_iterate (rtx, insns, i, x); i++)
5356 init_insn (x);
5359 if (insn != NULL)
5360 init_insn (insn);
5364 /* Extend data structures for logical insn UID. */
5365 static void
5366 luids_extend_insn (void)
5368 int new_luids_max_uid = get_max_uid () + 1;
5370 VEC_safe_grow_cleared (int, heap, sched_luids, new_luids_max_uid);
5373 /* Initialize LUID for INSN. */
5374 static void
5375 luids_init_insn (rtx insn)
5377 int i = INSN_P (insn) ? 1 : common_sched_info->luid_for_non_insn (insn);
5378 int luid;
5380 if (i >= 0)
5382 luid = sched_max_luid;
5383 sched_max_luid += i;
5385 else
5386 luid = -1;
5388 SET_INSN_LUID (insn, luid);
5391 /* Initialize luids for BBS, BB, INSNS and INSN.
5392 The hook common_sched_info->luid_for_non_insn () is used to determine
5393 if notes, labels, etc. need luids. */
5394 void
5395 sched_init_luids (bb_vec_t bbs, basic_block bb, insn_vec_t insns, rtx insn)
5397 const struct sched_scan_info_def ssi =
5399 NULL, /* extend_bb */
5400 NULL, /* init_bb */
5401 luids_extend_insn, /* extend_insn */
5402 luids_init_insn /* init_insn */
5405 sched_scan (&ssi, bbs, bb, insns, insn);
5408 /* Free LUIDs. */
5409 void
5410 sched_finish_luids (void)
5412 VEC_free (int, heap, sched_luids);
5413 sched_max_luid = 1;
5416 /* Return logical uid of INSN. Helpful while debugging. */
5418 insn_luid (rtx insn)
5420 return INSN_LUID (insn);
5423 /* Extend per insn data in the target. */
5424 void
5425 sched_extend_target (void)
5427 if (targetm.sched.h_i_d_extended)
5428 targetm.sched.h_i_d_extended ();
5431 /* Extend global scheduler structures (those, that live across calls to
5432 schedule_block) to include information about just emitted INSN. */
5433 static void
5434 extend_h_i_d (void)
5436 int reserve = (get_max_uid () + 1
5437 - VEC_length (haifa_insn_data_def, h_i_d));
5438 if (reserve > 0
5439 && ! VEC_space (haifa_insn_data_def, h_i_d, reserve))
5441 VEC_safe_grow_cleared (haifa_insn_data_def, heap, h_i_d,
5442 3 * get_max_uid () / 2);
5443 sched_extend_target ();
5447 /* Initialize h_i_d entry of the INSN with default values.
5448 Values, that are not explicitly initialized here, hold zero. */
5449 static void
5450 init_h_i_d (rtx insn)
5452 if (INSN_LUID (insn) > 0)
5454 INSN_COST (insn) = -1;
5455 QUEUE_INDEX (insn) = QUEUE_NOWHERE;
5456 INSN_TICK (insn) = INVALID_TICK;
5457 INTER_TICK (insn) = INVALID_TICK;
5458 TODO_SPEC (insn) = HARD_DEP;
5462 /* Initialize haifa_insn_data for BBS, BB, INSNS and INSN. */
5463 void
5464 haifa_init_h_i_d (bb_vec_t bbs, basic_block bb, insn_vec_t insns, rtx insn)
5466 const struct sched_scan_info_def ssi =
5468 NULL, /* extend_bb */
5469 NULL, /* init_bb */
5470 extend_h_i_d, /* extend_insn */
5471 init_h_i_d /* init_insn */
5474 sched_scan (&ssi, bbs, bb, insns, insn);
5477 /* Finalize haifa_insn_data. */
5478 void
5479 haifa_finish_h_i_d (void)
5481 int i;
5482 haifa_insn_data_t data;
5483 struct reg_use_data *use, *next;
5485 for (i = 0; VEC_iterate (haifa_insn_data_def, h_i_d, i, data); i++)
5487 if (data->reg_pressure != NULL)
5488 free (data->reg_pressure);
5489 for (use = data->reg_use_list; use != NULL; use = next)
5491 next = use->next_insn_use;
5492 free (use);
5495 VEC_free (haifa_insn_data_def, heap, h_i_d);
5498 /* Init data for the new insn INSN. */
5499 static void
5500 haifa_init_insn (rtx insn)
5502 gcc_assert (insn != NULL);
5504 sched_init_luids (NULL, NULL, NULL, insn);
5505 sched_extend_target ();
5506 sched_deps_init (false);
5507 haifa_init_h_i_d (NULL, NULL, NULL, insn);
5509 if (adding_bb_to_current_region_p)
5511 sd_init_insn (insn);
5513 /* Extend dependency caches by one element. */
5514 extend_dependency_caches (1, false);
5518 /* Init data for the new basic block BB which comes after AFTER. */
5519 static void
5520 haifa_init_only_bb (basic_block bb, basic_block after)
5522 gcc_assert (bb != NULL);
5524 sched_init_bbs ();
5526 if (common_sched_info->add_block)
5527 /* This changes only data structures of the front-end. */
5528 common_sched_info->add_block (bb, after);
5531 /* A generic version of sched_split_block (). */
5532 basic_block
5533 sched_split_block_1 (basic_block first_bb, rtx after)
5535 edge e;
5537 e = split_block (first_bb, after);
5538 gcc_assert (e->src == first_bb);
5540 /* sched_split_block emits note if *check == BB_END. Probably it
5541 is better to rip that note off. */
5543 return e->dest;
5546 /* A generic version of sched_create_empty_bb (). */
5547 basic_block
5548 sched_create_empty_bb_1 (basic_block after)
5550 return create_empty_bb (after);
5553 /* Insert PAT as an INSN into the schedule and update the necessary data
5554 structures to account for it. */
5556 sched_emit_insn (rtx pat)
5558 rtx insn = emit_insn_after (pat, last_scheduled_insn);
5559 last_scheduled_insn = insn;
5560 haifa_init_insn (insn);
5561 return insn;
5564 #endif /* INSN_SCHEDULING */