[PATCH] Move RTL printing code from sched-vis.c into print-rtl.c
[gcc.git] / gcc / haifa-sched.c
1 /* Instruction scheduling pass.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
4 and currently maintained by, Jim Wilson (wilson@cygnus.com)
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Instruction scheduling pass. This file, along with sched-deps.c,
23 contains the generic parts. The actual entry point for
24 the normal instruction scheduling pass is found in sched-rgn.c.
25
26 We compute insn priorities based on data dependencies. Flow
27 analysis only creates a fraction of the data-dependencies we must
28 observe: namely, only those dependencies which the combiner can be
29 expected to use. For this pass, we must therefore create the
30 remaining dependencies we need to observe: register dependencies,
31 memory dependencies, dependencies to keep function calls in order,
32 and the dependence between a conditional branch and the setting of
33 condition codes are all dealt with here.
34
35 The scheduler first traverses the data flow graph, starting with
36 the last instruction, and proceeding to the first, assigning values
37 to insn_priority as it goes. This sorts the instructions
38 topologically by data dependence.
39
40 Once priorities have been established, we order the insns using
41 list scheduling. This works as follows: starting with a list of
42 all the ready insns, and sorted according to priority number, we
43 schedule the insn from the end of the list by placing its
44 predecessors in the list according to their priority order. We
45 consider this insn scheduled by setting the pointer to the "end" of
46 the list to point to the previous insn. When an insn has no
47 predecessors, we either queue it until sufficient time has elapsed
48 or add it to the ready list. As the instructions are scheduled or
49 when stalls are introduced, the queue advances and dumps insns into
50 the ready list. When all insns down to the lowest priority have
51 been scheduled, the critical path of the basic block has been made
52 as short as possible. The remaining insns are then scheduled in
53 remaining slots.
54
55 The following list shows the order in which we want to break ties
56 among insns in the ready list:
57
58 1. choose insn with the longest path to end of bb, ties
59 broken by
60 2. choose insn with least contribution to register pressure,
61 ties broken by
62 3. prefer in-block upon interblock motion, ties broken by
63 4. prefer useful upon speculative motion, ties broken by
64 5. choose insn with largest control flow probability, ties
65 broken by
66 6. choose insn with the least dependences upon the previously
67 scheduled insn, or finally
68 7 choose the insn which has the most insns dependent on it.
69 8. choose insn with lowest UID.
70
71 Memory references complicate matters. Only if we can be certain
72 that memory references are not part of the data dependency graph
73 (via true, anti, or output dependence), can we move operations past
74 memory references. To first approximation, reads can be done
75 independently, while writes introduce dependencies. Better
76 approximations will yield fewer dependencies.
77
78 Before reload, an extended analysis of interblock data dependences
79 is required for interblock scheduling. This is performed in
80 compute_block_dependences ().
81
82 Dependencies set up by memory references are treated in exactly the
83 same way as other dependencies, by using insn backward dependences
84 INSN_BACK_DEPS. INSN_BACK_DEPS are translated into forward dependences
85 INSN_FORW_DEPS for the purpose of forward list scheduling.
86
87 Having optimized the critical path, we may have also unduly
88 extended the lifetimes of some registers. If an operation requires
89 that constants be loaded into registers, it is certainly desirable
90 to load those constants as early as necessary, but no earlier.
91 I.e., it will not do to load up a bunch of registers at the
92 beginning of a basic block only to use them at the end, if they
93 could be loaded later, since this may result in excessive register
94 utilization.
95
96 Note that since branches are never in basic blocks, but only end
97 basic blocks, this pass will not move branches. But that is ok,
98 since we can use GNU's delayed branch scheduling pass to take care
99 of this case.
100
101 Also note that no further optimizations based on algebraic
102 identities are performed, so this pass would be a good one to
103 perform instruction splitting, such as breaking up a multiply
104 instruction into shifts and adds where that is profitable.
105
106 Given the memory aliasing analysis that this pass should perform,
107 it should be possible to remove redundant stores to memory, and to
108 load values from registers instead of hitting memory.
109
110 Before reload, speculative insns are moved only if a 'proof' exists
111 that no exception will be caused by this, and if no live registers
112 exist that inhibit the motion (live registers constraints are not
113 represented by data dependence edges).
114
115 This pass must update information that subsequent passes expect to
116 be correct. Namely: reg_n_refs, reg_n_sets, reg_n_deaths,
117 reg_n_calls_crossed, and reg_live_length. Also, BB_HEAD, BB_END.
118
119 The information in the line number notes is carefully retained by
120 this pass. Notes that refer to the starting and ending of
121 exception regions are also carefully retained by this pass. All
122 other NOTE insns are grouped in their same relative order at the
123 beginning of basic blocks and regions that have been scheduled. */
124 \f
125 #include "config.h"
126 #include "system.h"
127 #include "coretypes.h"
128 #include "backend.h"
129 #include "cfghooks.h"
130 #include "rtl.h"
131 #include "df.h"
132 #include "diagnostic-core.h"
133 #include "tm_p.h"
134 #include "regs.h"
135 #include "flags.h"
136 #include "insn-config.h"
137 #include "insn-attr.h"
138 #include "except.h"
139 #include "recog.h"
140 #include "cfgrtl.h"
141 #include "cfgbuild.h"
142 #include "sched-int.h"
143 #include "target.h"
144 #include "common/common-target.h"
145 #include "params.h"
146 #include "dbgcnt.h"
147 #include "cfgloop.h"
148 #include "ira.h"
149 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
150 #include "dumpfile.h"
151 #include "print-rtl.h"
152
153 #ifdef INSN_SCHEDULING
154
155 /* True if we do register pressure relief through live-range
156 shrinkage. */
157 static bool live_range_shrinkage_p;
158
159 /* Switch on live range shrinkage. */
160 void
161 initialize_live_range_shrinkage (void)
162 {
163 live_range_shrinkage_p = true;
164 }
165
166 /* Switch off live range shrinkage. */
167 void
168 finish_live_range_shrinkage (void)
169 {
170 live_range_shrinkage_p = false;
171 }
172
173 /* issue_rate is the number of insns that can be scheduled in the same
174 machine cycle. It can be defined in the config/mach/mach.h file,
175 otherwise we set it to 1. */
176
177 int issue_rate;
178
179 /* This can be set to true by a backend if the scheduler should not
180 enable a DCE pass. */
181 bool sched_no_dce;
182
183 /* The current initiation interval used when modulo scheduling. */
184 static int modulo_ii;
185
186 /* The maximum number of stages we are prepared to handle. */
187 static int modulo_max_stages;
188
189 /* The number of insns that exist in each iteration of the loop. We use this
190 to detect when we've scheduled all insns from the first iteration. */
191 static int modulo_n_insns;
192
193 /* The current count of insns in the first iteration of the loop that have
194 already been scheduled. */
195 static int modulo_insns_scheduled;
196
197 /* The maximum uid of insns from the first iteration of the loop. */
198 static int modulo_iter0_max_uid;
199
200 /* The number of times we should attempt to backtrack when modulo scheduling.
201 Decreased each time we have to backtrack. */
202 static int modulo_backtracks_left;
203
204 /* The stage in which the last insn from the original loop was
205 scheduled. */
206 static int modulo_last_stage;
207
208 /* sched-verbose controls the amount of debugging output the
209 scheduler prints. It is controlled by -fsched-verbose=N:
210 N>0 and no -DSR : the output is directed to stderr.
211 N>=10 will direct the printouts to stderr (regardless of -dSR).
212 N=1: same as -dSR.
213 N=2: bb's probabilities, detailed ready list info, unit/insn info.
214 N=3: rtl at abort point, control-flow, regions info.
215 N=5: dependences info. */
216
217 int sched_verbose = 0;
218
219 /* Debugging file. All printouts are sent to dump, which is always set,
220 either to stderr, or to the dump listing file (-dRS). */
221 FILE *sched_dump = 0;
222
223 /* This is a placeholder for the scheduler parameters common
224 to all schedulers. */
225 struct common_sched_info_def *common_sched_info;
226
227 #define INSN_TICK(INSN) (HID (INSN)->tick)
228 #define INSN_EXACT_TICK(INSN) (HID (INSN)->exact_tick)
229 #define INSN_TICK_ESTIMATE(INSN) (HID (INSN)->tick_estimate)
230 #define INTER_TICK(INSN) (HID (INSN)->inter_tick)
231 #define FEEDS_BACKTRACK_INSN(INSN) (HID (INSN)->feeds_backtrack_insn)
232 #define SHADOW_P(INSN) (HID (INSN)->shadow_p)
233 #define MUST_RECOMPUTE_SPEC_P(INSN) (HID (INSN)->must_recompute_spec)
234 /* Cached cost of the instruction. Use insn_cost to get cost of the
235 insn. -1 here means that the field is not initialized. */
236 #define INSN_COST(INSN) (HID (INSN)->cost)
237
238 /* If INSN_TICK of an instruction is equal to INVALID_TICK,
239 then it should be recalculated from scratch. */
240 #define INVALID_TICK (-(max_insn_queue_index + 1))
241 /* The minimal value of the INSN_TICK of an instruction. */
242 #define MIN_TICK (-max_insn_queue_index)
243
244 /* Original order of insns in the ready list.
245 Used to keep order of normal insns while separating DEBUG_INSNs. */
246 #define INSN_RFS_DEBUG_ORIG_ORDER(INSN) (HID (INSN)->rfs_debug_orig_order)
247
248 /* The deciding reason for INSN's place in the ready list. */
249 #define INSN_LAST_RFS_WIN(INSN) (HID (INSN)->last_rfs_win)
250
251 /* List of important notes we must keep around. This is a pointer to the
252 last element in the list. */
253 rtx_insn *note_list;
254
255 static struct spec_info_def spec_info_var;
256 /* Description of the speculative part of the scheduling.
257 If NULL - no speculation. */
258 spec_info_t spec_info = NULL;
259
260 /* True, if recovery block was added during scheduling of current block.
261 Used to determine, if we need to fix INSN_TICKs. */
262 static bool haifa_recovery_bb_recently_added_p;
263
264 /* True, if recovery block was added during this scheduling pass.
265 Used to determine if we should have empty memory pools of dependencies
266 after finishing current region. */
267 bool haifa_recovery_bb_ever_added_p;
268
269 /* Counters of different types of speculative instructions. */
270 static int nr_begin_data, nr_be_in_data, nr_begin_control, nr_be_in_control;
271
272 /* Array used in {unlink, restore}_bb_notes. */
273 static rtx_insn **bb_header = 0;
274
275 /* Basic block after which recovery blocks will be created. */
276 static basic_block before_recovery;
277
278 /* Basic block just before the EXIT_BLOCK and after recovery, if we have
279 created it. */
280 basic_block after_recovery;
281
282 /* FALSE if we add bb to another region, so we don't need to initialize it. */
283 bool adding_bb_to_current_region_p = true;
284
285 /* Queues, etc. */
286
287 /* An instruction is ready to be scheduled when all insns preceding it
288 have already been scheduled. It is important to ensure that all
289 insns which use its result will not be executed until its result
290 has been computed. An insn is maintained in one of four structures:
291
292 (P) the "Pending" set of insns which cannot be scheduled until
293 their dependencies have been satisfied.
294 (Q) the "Queued" set of insns that can be scheduled when sufficient
295 time has passed.
296 (R) the "Ready" list of unscheduled, uncommitted insns.
297 (S) the "Scheduled" list of insns.
298
299 Initially, all insns are either "Pending" or "Ready" depending on
300 whether their dependencies are satisfied.
301
302 Insns move from the "Ready" list to the "Scheduled" list as they
303 are committed to the schedule. As this occurs, the insns in the
304 "Pending" list have their dependencies satisfied and move to either
305 the "Ready" list or the "Queued" set depending on whether
306 sufficient time has passed to make them ready. As time passes,
307 insns move from the "Queued" set to the "Ready" list.
308
309 The "Pending" list (P) are the insns in the INSN_FORW_DEPS of the
310 unscheduled insns, i.e., those that are ready, queued, and pending.
311 The "Queued" set (Q) is implemented by the variable `insn_queue'.
312 The "Ready" list (R) is implemented by the variables `ready' and
313 `n_ready'.
314 The "Scheduled" list (S) is the new insn chain built by this pass.
315
316 The transition (R->S) is implemented in the scheduling loop in
317 `schedule_block' when the best insn to schedule is chosen.
318 The transitions (P->R and P->Q) are implemented in `schedule_insn' as
319 insns move from the ready list to the scheduled list.
320 The transition (Q->R) is implemented in 'queue_to_insn' as time
321 passes or stalls are introduced. */
322
323 /* Implement a circular buffer to delay instructions until sufficient
324 time has passed. For the new pipeline description interface,
325 MAX_INSN_QUEUE_INDEX is a power of two minus one which is not less
326 than maximal time of instruction execution computed by genattr.c on
327 the base maximal time of functional unit reservations and getting a
328 result. This is the longest time an insn may be queued. */
329
330 static rtx_insn_list **insn_queue;
331 static int q_ptr = 0;
332 static int q_size = 0;
333 #define NEXT_Q(X) (((X)+1) & max_insn_queue_index)
334 #define NEXT_Q_AFTER(X, C) (((X)+C) & max_insn_queue_index)
335
336 #define QUEUE_SCHEDULED (-3)
337 #define QUEUE_NOWHERE (-2)
338 #define QUEUE_READY (-1)
339 /* QUEUE_SCHEDULED - INSN is scheduled.
340 QUEUE_NOWHERE - INSN isn't scheduled yet and is neither in
341 queue or ready list.
342 QUEUE_READY - INSN is in ready list.
343 N >= 0 - INSN queued for X [where NEXT_Q_AFTER (q_ptr, X) == N] cycles. */
344
345 #define QUEUE_INDEX(INSN) (HID (INSN)->queue_index)
346
347 /* The following variable value refers for all current and future
348 reservations of the processor units. */
349 state_t curr_state;
350
351 /* The following variable value is size of memory representing all
352 current and future reservations of the processor units. */
353 size_t dfa_state_size;
354
355 /* The following array is used to find the best insn from ready when
356 the automaton pipeline interface is used. */
357 signed char *ready_try = NULL;
358
359 /* The ready list. */
360 struct ready_list ready = {NULL, 0, 0, 0, 0};
361
362 /* The pointer to the ready list (to be removed). */
363 static struct ready_list *readyp = &ready;
364
365 /* Scheduling clock. */
366 static int clock_var;
367
368 /* Clock at which the previous instruction was issued. */
369 static int last_clock_var;
370
371 /* Set to true if, when queuing a shadow insn, we discover that it would be
372 scheduled too late. */
373 static bool must_backtrack;
374
375 /* The following variable value is number of essential insns issued on
376 the current cycle. An insn is essential one if it changes the
377 processors state. */
378 int cycle_issued_insns;
379
380 /* This records the actual schedule. It is built up during the main phase
381 of schedule_block, and afterwards used to reorder the insns in the RTL. */
382 static vec<rtx_insn *> scheduled_insns;
383
384 static int may_trap_exp (const_rtx, int);
385
386 /* Nonzero iff the address is comprised from at most 1 register. */
387 #define CONST_BASED_ADDRESS_P(x) \
388 (REG_P (x) \
389 || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS \
390 || (GET_CODE (x) == LO_SUM)) \
391 && (CONSTANT_P (XEXP (x, 0)) \
392 || CONSTANT_P (XEXP (x, 1)))))
393
394 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
395 as found by analyzing insn's expression. */
396
397 \f
398 static int haifa_luid_for_non_insn (rtx x);
399
400 /* Haifa version of sched_info hooks common to all headers. */
401 const struct common_sched_info_def haifa_common_sched_info =
402 {
403 NULL, /* fix_recovery_cfg */
404 NULL, /* add_block */
405 NULL, /* estimate_number_of_insns */
406 haifa_luid_for_non_insn, /* luid_for_non_insn */
407 SCHED_PASS_UNKNOWN /* sched_pass_id */
408 };
409
410 /* Mapping from instruction UID to its Logical UID. */
411 vec<int> sched_luids = vNULL;
412
413 /* Next LUID to assign to an instruction. */
414 int sched_max_luid = 1;
415
416 /* Haifa Instruction Data. */
417 vec<haifa_insn_data_def> h_i_d = vNULL;
418
419 void (* sched_init_only_bb) (basic_block, basic_block);
420
421 /* Split block function. Different schedulers might use different functions
422 to handle their internal data consistent. */
423 basic_block (* sched_split_block) (basic_block, rtx);
424
425 /* Create empty basic block after the specified block. */
426 basic_block (* sched_create_empty_bb) (basic_block);
427
428 /* Return the number of cycles until INSN is expected to be ready.
429 Return zero if it already is. */
430 static int
431 insn_delay (rtx_insn *insn)
432 {
433 return MAX (INSN_TICK (insn) - clock_var, 0);
434 }
435
436 static int
437 may_trap_exp (const_rtx x, int is_store)
438 {
439 enum rtx_code code;
440
441 if (x == 0)
442 return TRAP_FREE;
443 code = GET_CODE (x);
444 if (is_store)
445 {
446 if (code == MEM && may_trap_p (x))
447 return TRAP_RISKY;
448 else
449 return TRAP_FREE;
450 }
451 if (code == MEM)
452 {
453 /* The insn uses memory: a volatile load. */
454 if (MEM_VOLATILE_P (x))
455 return IRISKY;
456 /* An exception-free load. */
457 if (!may_trap_p (x))
458 return IFREE;
459 /* A load with 1 base register, to be further checked. */
460 if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
461 return PFREE_CANDIDATE;
462 /* No info on the load, to be further checked. */
463 return PRISKY_CANDIDATE;
464 }
465 else
466 {
467 const char *fmt;
468 int i, insn_class = TRAP_FREE;
469
470 /* Neither store nor load, check if it may cause a trap. */
471 if (may_trap_p (x))
472 return TRAP_RISKY;
473 /* Recursive step: walk the insn... */
474 fmt = GET_RTX_FORMAT (code);
475 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
476 {
477 if (fmt[i] == 'e')
478 {
479 int tmp_class = may_trap_exp (XEXP (x, i), is_store);
480 insn_class = WORST_CLASS (insn_class, tmp_class);
481 }
482 else if (fmt[i] == 'E')
483 {
484 int j;
485 for (j = 0; j < XVECLEN (x, i); j++)
486 {
487 int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
488 insn_class = WORST_CLASS (insn_class, tmp_class);
489 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
490 break;
491 }
492 }
493 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
494 break;
495 }
496 return insn_class;
497 }
498 }
499
500 /* Classifies rtx X of an insn for the purpose of verifying that X can be
501 executed speculatively (and consequently the insn can be moved
502 speculatively), by examining X, returning:
503 TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
504 TRAP_FREE: non-load insn.
505 IFREE: load from a globally safe location.
506 IRISKY: volatile load.
507 PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
508 being either PFREE or PRISKY. */
509
510 static int
511 haifa_classify_rtx (const_rtx x)
512 {
513 int tmp_class = TRAP_FREE;
514 int insn_class = TRAP_FREE;
515 enum rtx_code code;
516
517 if (GET_CODE (x) == PARALLEL)
518 {
519 int i, len = XVECLEN (x, 0);
520
521 for (i = len - 1; i >= 0; i--)
522 {
523 tmp_class = haifa_classify_rtx (XVECEXP (x, 0, i));
524 insn_class = WORST_CLASS (insn_class, tmp_class);
525 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
526 break;
527 }
528 }
529 else
530 {
531 code = GET_CODE (x);
532 switch (code)
533 {
534 case CLOBBER:
535 /* Test if it is a 'store'. */
536 tmp_class = may_trap_exp (XEXP (x, 0), 1);
537 break;
538 case SET:
539 /* Test if it is a store. */
540 tmp_class = may_trap_exp (SET_DEST (x), 1);
541 if (tmp_class == TRAP_RISKY)
542 break;
543 /* Test if it is a load. */
544 tmp_class =
545 WORST_CLASS (tmp_class,
546 may_trap_exp (SET_SRC (x), 0));
547 break;
548 case COND_EXEC:
549 tmp_class = haifa_classify_rtx (COND_EXEC_CODE (x));
550 if (tmp_class == TRAP_RISKY)
551 break;
552 tmp_class = WORST_CLASS (tmp_class,
553 may_trap_exp (COND_EXEC_TEST (x), 0));
554 break;
555 case TRAP_IF:
556 tmp_class = TRAP_RISKY;
557 break;
558 default:;
559 }
560 insn_class = tmp_class;
561 }
562
563 return insn_class;
564 }
565
566 int
567 haifa_classify_insn (const_rtx insn)
568 {
569 return haifa_classify_rtx (PATTERN (insn));
570 }
571 \f
572 /* After the scheduler initialization function has been called, this function
573 can be called to enable modulo scheduling. II is the initiation interval
574 we should use, it affects the delays for delay_pairs that were recorded as
575 separated by a given number of stages.
576
577 MAX_STAGES provides us with a limit
578 after which we give up scheduling; the caller must have unrolled at least
579 as many copies of the loop body and recorded delay_pairs for them.
580
581 INSNS is the number of real (non-debug) insns in one iteration of
582 the loop. MAX_UID can be used to test whether an insn belongs to
583 the first iteration of the loop; all of them have a uid lower than
584 MAX_UID. */
585 void
586 set_modulo_params (int ii, int max_stages, int insns, int max_uid)
587 {
588 modulo_ii = ii;
589 modulo_max_stages = max_stages;
590 modulo_n_insns = insns;
591 modulo_iter0_max_uid = max_uid;
592 modulo_backtracks_left = PARAM_VALUE (PARAM_MAX_MODULO_BACKTRACK_ATTEMPTS);
593 }
594
595 /* A structure to record a pair of insns where the first one is a real
596 insn that has delay slots, and the second is its delayed shadow.
597 I1 is scheduled normally and will emit an assembly instruction,
598 while I2 describes the side effect that takes place at the
599 transition between cycles CYCLES and (CYCLES + 1) after I1. */
600 struct delay_pair
601 {
602 struct delay_pair *next_same_i1;
603 rtx_insn *i1, *i2;
604 int cycles;
605 /* When doing modulo scheduling, we a delay_pair can also be used to
606 show that I1 and I2 are the same insn in a different stage. If that
607 is the case, STAGES will be nonzero. */
608 int stages;
609 };
610
611 /* Helpers for delay hashing. */
612
613 struct delay_i1_hasher : nofree_ptr_hash <delay_pair>
614 {
615 typedef void *compare_type;
616 static inline hashval_t hash (const delay_pair *);
617 static inline bool equal (const delay_pair *, const void *);
618 };
619
620 /* Returns a hash value for X, based on hashing just I1. */
621
622 inline hashval_t
623 delay_i1_hasher::hash (const delay_pair *x)
624 {
625 return htab_hash_pointer (x->i1);
626 }
627
628 /* Return true if I1 of pair X is the same as that of pair Y. */
629
630 inline bool
631 delay_i1_hasher::equal (const delay_pair *x, const void *y)
632 {
633 return x->i1 == y;
634 }
635
636 struct delay_i2_hasher : free_ptr_hash <delay_pair>
637 {
638 typedef void *compare_type;
639 static inline hashval_t hash (const delay_pair *);
640 static inline bool equal (const delay_pair *, const void *);
641 };
642
643 /* Returns a hash value for X, based on hashing just I2. */
644
645 inline hashval_t
646 delay_i2_hasher::hash (const delay_pair *x)
647 {
648 return htab_hash_pointer (x->i2);
649 }
650
651 /* Return true if I2 of pair X is the same as that of pair Y. */
652
653 inline bool
654 delay_i2_hasher::equal (const delay_pair *x, const void *y)
655 {
656 return x->i2 == y;
657 }
658
659 /* Two hash tables to record delay_pairs, one indexed by I1 and the other
660 indexed by I2. */
661 static hash_table<delay_i1_hasher> *delay_htab;
662 static hash_table<delay_i2_hasher> *delay_htab_i2;
663
664 /* Called through htab_traverse. Walk the hashtable using I2 as
665 index, and delete all elements involving an UID higher than
666 that pointed to by *DATA. */
667 int
668 haifa_htab_i2_traverse (delay_pair **slot, int *data)
669 {
670 int maxuid = *data;
671 struct delay_pair *p = *slot;
672 if (INSN_UID (p->i2) >= maxuid || INSN_UID (p->i1) >= maxuid)
673 {
674 delay_htab_i2->clear_slot (slot);
675 }
676 return 1;
677 }
678
679 /* Called through htab_traverse. Walk the hashtable using I2 as
680 index, and delete all elements involving an UID higher than
681 that pointed to by *DATA. */
682 int
683 haifa_htab_i1_traverse (delay_pair **pslot, int *data)
684 {
685 int maxuid = *data;
686 struct delay_pair *p, *first, **pprev;
687
688 if (INSN_UID ((*pslot)->i1) >= maxuid)
689 {
690 delay_htab->clear_slot (pslot);
691 return 1;
692 }
693 pprev = &first;
694 for (p = *pslot; p; p = p->next_same_i1)
695 {
696 if (INSN_UID (p->i2) < maxuid)
697 {
698 *pprev = p;
699 pprev = &p->next_same_i1;
700 }
701 }
702 *pprev = NULL;
703 if (first == NULL)
704 delay_htab->clear_slot (pslot);
705 else
706 *pslot = first;
707 return 1;
708 }
709
710 /* Discard all delay pairs which involve an insn with an UID higher
711 than MAX_UID. */
712 void
713 discard_delay_pairs_above (int max_uid)
714 {
715 delay_htab->traverse <int *, haifa_htab_i1_traverse> (&max_uid);
716 delay_htab_i2->traverse <int *, haifa_htab_i2_traverse> (&max_uid);
717 }
718
719 /* This function can be called by a port just before it starts the final
720 scheduling pass. It records the fact that an instruction with delay
721 slots has been split into two insns, I1 and I2. The first one will be
722 scheduled normally and initiates the operation. The second one is a
723 shadow which must follow a specific number of cycles after I1; its only
724 purpose is to show the side effect that occurs at that cycle in the RTL.
725 If a JUMP_INSN or a CALL_INSN has been split, I1 should be a normal INSN,
726 while I2 retains the original insn type.
727
728 There are two ways in which the number of cycles can be specified,
729 involving the CYCLES and STAGES arguments to this function. If STAGES
730 is zero, we just use the value of CYCLES. Otherwise, STAGES is a factor
731 which is multiplied by MODULO_II to give the number of cycles. This is
732 only useful if the caller also calls set_modulo_params to enable modulo
733 scheduling. */
734
735 void
736 record_delay_slot_pair (rtx_insn *i1, rtx_insn *i2, int cycles, int stages)
737 {
738 struct delay_pair *p = XNEW (struct delay_pair);
739 struct delay_pair **slot;
740
741 p->i1 = i1;
742 p->i2 = i2;
743 p->cycles = cycles;
744 p->stages = stages;
745
746 if (!delay_htab)
747 {
748 delay_htab = new hash_table<delay_i1_hasher> (10);
749 delay_htab_i2 = new hash_table<delay_i2_hasher> (10);
750 }
751 slot = delay_htab->find_slot_with_hash (i1, htab_hash_pointer (i1), INSERT);
752 p->next_same_i1 = *slot;
753 *slot = p;
754 slot = delay_htab_i2->find_slot (p, INSERT);
755 *slot = p;
756 }
757
758 /* Examine the delay pair hashtable to see if INSN is a shadow for another,
759 and return the other insn if so. Return NULL otherwise. */
760 rtx_insn *
761 real_insn_for_shadow (rtx_insn *insn)
762 {
763 struct delay_pair *pair;
764
765 if (!delay_htab)
766 return NULL;
767
768 pair = delay_htab_i2->find_with_hash (insn, htab_hash_pointer (insn));
769 if (!pair || pair->stages > 0)
770 return NULL;
771 return pair->i1;
772 }
773
774 /* For a pair P of insns, return the fixed distance in cycles from the first
775 insn after which the second must be scheduled. */
776 static int
777 pair_delay (struct delay_pair *p)
778 {
779 if (p->stages == 0)
780 return p->cycles;
781 else
782 return p->stages * modulo_ii;
783 }
784
785 /* Given an insn INSN, add a dependence on its delayed shadow if it
786 has one. Also try to find situations where shadows depend on each other
787 and add dependencies to the real insns to limit the amount of backtracking
788 needed. */
789 void
790 add_delay_dependencies (rtx_insn *insn)
791 {
792 struct delay_pair *pair;
793 sd_iterator_def sd_it;
794 dep_t dep;
795
796 if (!delay_htab)
797 return;
798
799 pair = delay_htab_i2->find_with_hash (insn, htab_hash_pointer (insn));
800 if (!pair)
801 return;
802 add_dependence (insn, pair->i1, REG_DEP_ANTI);
803 if (pair->stages)
804 return;
805
806 FOR_EACH_DEP (pair->i2, SD_LIST_BACK, sd_it, dep)
807 {
808 rtx_insn *pro = DEP_PRO (dep);
809 struct delay_pair *other_pair
810 = delay_htab_i2->find_with_hash (pro, htab_hash_pointer (pro));
811 if (!other_pair || other_pair->stages)
812 continue;
813 if (pair_delay (other_pair) >= pair_delay (pair))
814 {
815 if (sched_verbose >= 4)
816 {
817 fprintf (sched_dump, ";;\tadding dependence %d <- %d\n",
818 INSN_UID (other_pair->i1),
819 INSN_UID (pair->i1));
820 fprintf (sched_dump, ";;\tpair1 %d <- %d, cost %d\n",
821 INSN_UID (pair->i1),
822 INSN_UID (pair->i2),
823 pair_delay (pair));
824 fprintf (sched_dump, ";;\tpair2 %d <- %d, cost %d\n",
825 INSN_UID (other_pair->i1),
826 INSN_UID (other_pair->i2),
827 pair_delay (other_pair));
828 }
829 add_dependence (pair->i1, other_pair->i1, REG_DEP_ANTI);
830 }
831 }
832 }
833 \f
834 /* Forward declarations. */
835
836 static int priority (rtx_insn *);
837 static int autopref_rank_for_schedule (const rtx_insn *, const rtx_insn *);
838 static int rank_for_schedule (const void *, const void *);
839 static void swap_sort (rtx_insn **, int);
840 static void queue_insn (rtx_insn *, int, const char *);
841 static int schedule_insn (rtx_insn *);
842 static void adjust_priority (rtx_insn *);
843 static void advance_one_cycle (void);
844 static void extend_h_i_d (void);
845
846
847 /* Notes handling mechanism:
848 =========================
849 Generally, NOTES are saved before scheduling and restored after scheduling.
850 The scheduler distinguishes between two types of notes:
851
852 (1) LOOP_BEGIN, LOOP_END, SETJMP, EHREGION_BEG, EHREGION_END notes:
853 Before scheduling a region, a pointer to the note is added to the insn
854 that follows or precedes it. (This happens as part of the data dependence
855 computation). After scheduling an insn, the pointer contained in it is
856 used for regenerating the corresponding note (in reemit_notes).
857
858 (2) All other notes (e.g. INSN_DELETED): Before scheduling a block,
859 these notes are put in a list (in rm_other_notes() and
860 unlink_other_notes ()). After scheduling the block, these notes are
861 inserted at the beginning of the block (in schedule_block()). */
862
863 static void ready_add (struct ready_list *, rtx_insn *, bool);
864 static rtx_insn *ready_remove_first (struct ready_list *);
865 static rtx_insn *ready_remove_first_dispatch (struct ready_list *ready);
866
867 static void queue_to_ready (struct ready_list *);
868 static int early_queue_to_ready (state_t, struct ready_list *);
869
870 /* The following functions are used to implement multi-pass scheduling
871 on the first cycle. */
872 static rtx_insn *ready_remove (struct ready_list *, int);
873 static void ready_remove_insn (rtx_insn *);
874
875 static void fix_inter_tick (rtx_insn *, rtx_insn *);
876 static int fix_tick_ready (rtx_insn *);
877 static void change_queue_index (rtx_insn *, int);
878
879 /* The following functions are used to implement scheduling of data/control
880 speculative instructions. */
881
882 static void extend_h_i_d (void);
883 static void init_h_i_d (rtx_insn *);
884 static int haifa_speculate_insn (rtx_insn *, ds_t, rtx *);
885 static void generate_recovery_code (rtx_insn *);
886 static void process_insn_forw_deps_be_in_spec (rtx_insn *, rtx_insn *, ds_t);
887 static void begin_speculative_block (rtx_insn *);
888 static void add_to_speculative_block (rtx_insn *);
889 static void init_before_recovery (basic_block *);
890 static void create_check_block_twin (rtx_insn *, bool);
891 static void fix_recovery_deps (basic_block);
892 static bool haifa_change_pattern (rtx_insn *, rtx);
893 static void dump_new_block_header (int, basic_block, rtx_insn *, rtx_insn *);
894 static void restore_bb_notes (basic_block);
895 static void fix_jump_move (rtx_insn *);
896 static void move_block_after_check (rtx_insn *);
897 static void move_succs (vec<edge, va_gc> **, basic_block);
898 static void sched_remove_insn (rtx_insn *);
899 static void clear_priorities (rtx_insn *, rtx_vec_t *);
900 static void calc_priorities (rtx_vec_t);
901 static void add_jump_dependencies (rtx_insn *, rtx_insn *);
902
903 #endif /* INSN_SCHEDULING */
904 \f
905 /* Point to state used for the current scheduling pass. */
906 struct haifa_sched_info *current_sched_info;
907 \f
908 #ifndef INSN_SCHEDULING
909 void
910 schedule_insns (void)
911 {
912 }
913 #else
914
915 /* Do register pressure sensitive insn scheduling if the flag is set
916 up. */
917 enum sched_pressure_algorithm sched_pressure;
918
919 /* Map regno -> its pressure class. The map defined only when
920 SCHED_PRESSURE != SCHED_PRESSURE_NONE. */
921 enum reg_class *sched_regno_pressure_class;
922
923 /* The current register pressure. Only elements corresponding pressure
924 classes are defined. */
925 static int curr_reg_pressure[N_REG_CLASSES];
926
927 /* Saved value of the previous array. */
928 static int saved_reg_pressure[N_REG_CLASSES];
929
930 /* Register living at given scheduling point. */
931 static bitmap curr_reg_live;
932
933 /* Saved value of the previous array. */
934 static bitmap saved_reg_live;
935
936 /* Registers mentioned in the current region. */
937 static bitmap region_ref_regs;
938
939 /* Effective number of available registers of a given class (see comment
940 in sched_pressure_start_bb). */
941 static int sched_class_regs_num[N_REG_CLASSES];
942 /* Number of call_used_regs. This is a helper for calculating of
943 sched_class_regs_num. */
944 static int call_used_regs_num[N_REG_CLASSES];
945
946 /* Initiate register pressure relative info for scheduling the current
947 region. Currently it is only clearing register mentioned in the
948 current region. */
949 void
950 sched_init_region_reg_pressure_info (void)
951 {
952 bitmap_clear (region_ref_regs);
953 }
954
955 /* PRESSURE[CL] describes the pressure on register class CL. Update it
956 for the birth (if BIRTH_P) or death (if !BIRTH_P) of register REGNO.
957 LIVE tracks the set of live registers; if it is null, assume that
958 every birth or death is genuine. */
959 static inline void
960 mark_regno_birth_or_death (bitmap live, int *pressure, int regno, bool birth_p)
961 {
962 enum reg_class pressure_class;
963
964 pressure_class = sched_regno_pressure_class[regno];
965 if (regno >= FIRST_PSEUDO_REGISTER)
966 {
967 if (pressure_class != NO_REGS)
968 {
969 if (birth_p)
970 {
971 if (!live || bitmap_set_bit (live, regno))
972 pressure[pressure_class]
973 += (ira_reg_class_max_nregs
974 [pressure_class][PSEUDO_REGNO_MODE (regno)]);
975 }
976 else
977 {
978 if (!live || bitmap_clear_bit (live, regno))
979 pressure[pressure_class]
980 -= (ira_reg_class_max_nregs
981 [pressure_class][PSEUDO_REGNO_MODE (regno)]);
982 }
983 }
984 }
985 else if (pressure_class != NO_REGS
986 && ! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
987 {
988 if (birth_p)
989 {
990 if (!live || bitmap_set_bit (live, regno))
991 pressure[pressure_class]++;
992 }
993 else
994 {
995 if (!live || bitmap_clear_bit (live, regno))
996 pressure[pressure_class]--;
997 }
998 }
999 }
1000
1001 /* Initiate current register pressure related info from living
1002 registers given by LIVE. */
1003 static void
1004 initiate_reg_pressure_info (bitmap live)
1005 {
1006 int i;
1007 unsigned int j;
1008 bitmap_iterator bi;
1009
1010 for (i = 0; i < ira_pressure_classes_num; i++)
1011 curr_reg_pressure[ira_pressure_classes[i]] = 0;
1012 bitmap_clear (curr_reg_live);
1013 EXECUTE_IF_SET_IN_BITMAP (live, 0, j, bi)
1014 if (sched_pressure == SCHED_PRESSURE_MODEL
1015 || current_nr_blocks == 1
1016 || bitmap_bit_p (region_ref_regs, j))
1017 mark_regno_birth_or_death (curr_reg_live, curr_reg_pressure, j, true);
1018 }
1019
1020 /* Mark registers in X as mentioned in the current region. */
1021 static void
1022 setup_ref_regs (rtx x)
1023 {
1024 int i, j;
1025 const RTX_CODE code = GET_CODE (x);
1026 const char *fmt;
1027
1028 if (REG_P (x))
1029 {
1030 bitmap_set_range (region_ref_regs, REGNO (x), REG_NREGS (x));
1031 return;
1032 }
1033 fmt = GET_RTX_FORMAT (code);
1034 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1035 if (fmt[i] == 'e')
1036 setup_ref_regs (XEXP (x, i));
1037 else if (fmt[i] == 'E')
1038 {
1039 for (j = 0; j < XVECLEN (x, i); j++)
1040 setup_ref_regs (XVECEXP (x, i, j));
1041 }
1042 }
1043
1044 /* Initiate current register pressure related info at the start of
1045 basic block BB. */
1046 static void
1047 initiate_bb_reg_pressure_info (basic_block bb)
1048 {
1049 unsigned int i ATTRIBUTE_UNUSED;
1050 rtx_insn *insn;
1051
1052 if (current_nr_blocks > 1)
1053 FOR_BB_INSNS (bb, insn)
1054 if (NONDEBUG_INSN_P (insn))
1055 setup_ref_regs (PATTERN (insn));
1056 initiate_reg_pressure_info (df_get_live_in (bb));
1057 if (bb_has_eh_pred (bb))
1058 for (i = 0; ; ++i)
1059 {
1060 unsigned int regno = EH_RETURN_DATA_REGNO (i);
1061
1062 if (regno == INVALID_REGNUM)
1063 break;
1064 if (! bitmap_bit_p (df_get_live_in (bb), regno))
1065 mark_regno_birth_or_death (curr_reg_live, curr_reg_pressure,
1066 regno, true);
1067 }
1068 }
1069
1070 /* Save current register pressure related info. */
1071 static void
1072 save_reg_pressure (void)
1073 {
1074 int i;
1075
1076 for (i = 0; i < ira_pressure_classes_num; i++)
1077 saved_reg_pressure[ira_pressure_classes[i]]
1078 = curr_reg_pressure[ira_pressure_classes[i]];
1079 bitmap_copy (saved_reg_live, curr_reg_live);
1080 }
1081
1082 /* Restore saved register pressure related info. */
1083 static void
1084 restore_reg_pressure (void)
1085 {
1086 int i;
1087
1088 for (i = 0; i < ira_pressure_classes_num; i++)
1089 curr_reg_pressure[ira_pressure_classes[i]]
1090 = saved_reg_pressure[ira_pressure_classes[i]];
1091 bitmap_copy (curr_reg_live, saved_reg_live);
1092 }
1093
1094 /* Return TRUE if the register is dying after its USE. */
1095 static bool
1096 dying_use_p (struct reg_use_data *use)
1097 {
1098 struct reg_use_data *next;
1099
1100 for (next = use->next_regno_use; next != use; next = next->next_regno_use)
1101 if (NONDEBUG_INSN_P (next->insn)
1102 && QUEUE_INDEX (next->insn) != QUEUE_SCHEDULED)
1103 return false;
1104 return true;
1105 }
1106
1107 /* Print info about the current register pressure and its excess for
1108 each pressure class. */
1109 static void
1110 print_curr_reg_pressure (void)
1111 {
1112 int i;
1113 enum reg_class cl;
1114
1115 fprintf (sched_dump, ";;\t");
1116 for (i = 0; i < ira_pressure_classes_num; i++)
1117 {
1118 cl = ira_pressure_classes[i];
1119 gcc_assert (curr_reg_pressure[cl] >= 0);
1120 fprintf (sched_dump, " %s:%d(%d)", reg_class_names[cl],
1121 curr_reg_pressure[cl],
1122 curr_reg_pressure[cl] - sched_class_regs_num[cl]);
1123 }
1124 fprintf (sched_dump, "\n");
1125 }
1126 \f
1127 /* Determine if INSN has a condition that is clobbered if a register
1128 in SET_REGS is modified. */
1129 static bool
1130 cond_clobbered_p (rtx_insn *insn, HARD_REG_SET set_regs)
1131 {
1132 rtx pat = PATTERN (insn);
1133 gcc_assert (GET_CODE (pat) == COND_EXEC);
1134 if (TEST_HARD_REG_BIT (set_regs, REGNO (XEXP (COND_EXEC_TEST (pat), 0))))
1135 {
1136 sd_iterator_def sd_it;
1137 dep_t dep;
1138 haifa_change_pattern (insn, ORIG_PAT (insn));
1139 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
1140 DEP_STATUS (dep) &= ~DEP_CANCELLED;
1141 TODO_SPEC (insn) = HARD_DEP;
1142 if (sched_verbose >= 2)
1143 fprintf (sched_dump,
1144 ";;\t\tdequeue insn %s because of clobbered condition\n",
1145 (*current_sched_info->print_insn) (insn, 0));
1146 return true;
1147 }
1148
1149 return false;
1150 }
1151
1152 /* This function should be called after modifying the pattern of INSN,
1153 to update scheduler data structures as needed. */
1154 static void
1155 update_insn_after_change (rtx_insn *insn)
1156 {
1157 sd_iterator_def sd_it;
1158 dep_t dep;
1159
1160 dfa_clear_single_insn_cache (insn);
1161
1162 sd_it = sd_iterator_start (insn,
1163 SD_LIST_FORW | SD_LIST_BACK | SD_LIST_RES_BACK);
1164 while (sd_iterator_cond (&sd_it, &dep))
1165 {
1166 DEP_COST (dep) = UNKNOWN_DEP_COST;
1167 sd_iterator_next (&sd_it);
1168 }
1169
1170 /* Invalidate INSN_COST, so it'll be recalculated. */
1171 INSN_COST (insn) = -1;
1172 /* Invalidate INSN_TICK, so it'll be recalculated. */
1173 INSN_TICK (insn) = INVALID_TICK;
1174
1175 /* Invalidate autoprefetch data entry. */
1176 INSN_AUTOPREF_MULTIPASS_DATA (insn)[0].status
1177 = AUTOPREF_MULTIPASS_DATA_UNINITIALIZED;
1178 INSN_AUTOPREF_MULTIPASS_DATA (insn)[1].status
1179 = AUTOPREF_MULTIPASS_DATA_UNINITIALIZED;
1180 }
1181
1182
1183 /* Two VECs, one to hold dependencies for which pattern replacements
1184 need to be applied or restored at the start of the next cycle, and
1185 another to hold an integer that is either one, to apply the
1186 corresponding replacement, or zero to restore it. */
1187 static vec<dep_t> next_cycle_replace_deps;
1188 static vec<int> next_cycle_apply;
1189
1190 static void apply_replacement (dep_t, bool);
1191 static void restore_pattern (dep_t, bool);
1192
1193 /* Look at the remaining dependencies for insn NEXT, and compute and return
1194 the TODO_SPEC value we should use for it. This is called after one of
1195 NEXT's dependencies has been resolved.
1196 We also perform pattern replacements for predication, and for broken
1197 replacement dependencies. The latter is only done if FOR_BACKTRACK is
1198 false. */
1199
1200 static ds_t
1201 recompute_todo_spec (rtx_insn *next, bool for_backtrack)
1202 {
1203 ds_t new_ds;
1204 sd_iterator_def sd_it;
1205 dep_t dep, modify_dep = NULL;
1206 int n_spec = 0;
1207 int n_control = 0;
1208 int n_replace = 0;
1209 bool first_p = true;
1210
1211 if (sd_lists_empty_p (next, SD_LIST_BACK))
1212 /* NEXT has all its dependencies resolved. */
1213 return 0;
1214
1215 if (!sd_lists_empty_p (next, SD_LIST_HARD_BACK))
1216 return HARD_DEP;
1217
1218 /* If NEXT is intended to sit adjacent to this instruction, we don't
1219 want to try to break any dependencies. Treat it as a HARD_DEP. */
1220 if (SCHED_GROUP_P (next))
1221 return HARD_DEP;
1222
1223 /* Now we've got NEXT with speculative deps only.
1224 1. Look at the deps to see what we have to do.
1225 2. Check if we can do 'todo'. */
1226 new_ds = 0;
1227
1228 FOR_EACH_DEP (next, SD_LIST_BACK, sd_it, dep)
1229 {
1230 rtx_insn *pro = DEP_PRO (dep);
1231 ds_t ds = DEP_STATUS (dep) & SPECULATIVE;
1232
1233 if (DEBUG_INSN_P (pro) && !DEBUG_INSN_P (next))
1234 continue;
1235
1236 if (ds)
1237 {
1238 n_spec++;
1239 if (first_p)
1240 {
1241 first_p = false;
1242
1243 new_ds = ds;
1244 }
1245 else
1246 new_ds = ds_merge (new_ds, ds);
1247 }
1248 else if (DEP_TYPE (dep) == REG_DEP_CONTROL)
1249 {
1250 if (QUEUE_INDEX (pro) != QUEUE_SCHEDULED)
1251 {
1252 n_control++;
1253 modify_dep = dep;
1254 }
1255 DEP_STATUS (dep) &= ~DEP_CANCELLED;
1256 }
1257 else if (DEP_REPLACE (dep) != NULL)
1258 {
1259 if (QUEUE_INDEX (pro) != QUEUE_SCHEDULED)
1260 {
1261 n_replace++;
1262 modify_dep = dep;
1263 }
1264 DEP_STATUS (dep) &= ~DEP_CANCELLED;
1265 }
1266 }
1267
1268 if (n_replace > 0 && n_control == 0 && n_spec == 0)
1269 {
1270 if (!dbg_cnt (sched_breakdep))
1271 return HARD_DEP;
1272 FOR_EACH_DEP (next, SD_LIST_BACK, sd_it, dep)
1273 {
1274 struct dep_replacement *desc = DEP_REPLACE (dep);
1275 if (desc != NULL)
1276 {
1277 if (desc->insn == next && !for_backtrack)
1278 {
1279 gcc_assert (n_replace == 1);
1280 apply_replacement (dep, true);
1281 }
1282 DEP_STATUS (dep) |= DEP_CANCELLED;
1283 }
1284 }
1285 return 0;
1286 }
1287
1288 else if (n_control == 1 && n_replace == 0 && n_spec == 0)
1289 {
1290 rtx_insn *pro, *other;
1291 rtx new_pat;
1292 rtx cond = NULL_RTX;
1293 bool success;
1294 rtx_insn *prev = NULL;
1295 int i;
1296 unsigned regno;
1297
1298 if ((current_sched_info->flags & DO_PREDICATION) == 0
1299 || (ORIG_PAT (next) != NULL_RTX
1300 && PREDICATED_PAT (next) == NULL_RTX))
1301 return HARD_DEP;
1302
1303 pro = DEP_PRO (modify_dep);
1304 other = real_insn_for_shadow (pro);
1305 if (other != NULL_RTX)
1306 pro = other;
1307
1308 cond = sched_get_reverse_condition_uncached (pro);
1309 regno = REGNO (XEXP (cond, 0));
1310
1311 /* Find the last scheduled insn that modifies the condition register.
1312 We can stop looking once we find the insn we depend on through the
1313 REG_DEP_CONTROL; if the condition register isn't modified after it,
1314 we know that it still has the right value. */
1315 if (QUEUE_INDEX (pro) == QUEUE_SCHEDULED)
1316 FOR_EACH_VEC_ELT_REVERSE (scheduled_insns, i, prev)
1317 {
1318 HARD_REG_SET t;
1319
1320 find_all_hard_reg_sets (prev, &t, true);
1321 if (TEST_HARD_REG_BIT (t, regno))
1322 return HARD_DEP;
1323 if (prev == pro)
1324 break;
1325 }
1326 if (ORIG_PAT (next) == NULL_RTX)
1327 {
1328 ORIG_PAT (next) = PATTERN (next);
1329
1330 new_pat = gen_rtx_COND_EXEC (VOIDmode, cond, PATTERN (next));
1331 success = haifa_change_pattern (next, new_pat);
1332 if (!success)
1333 return HARD_DEP;
1334 PREDICATED_PAT (next) = new_pat;
1335 }
1336 else if (PATTERN (next) != PREDICATED_PAT (next))
1337 {
1338 bool success = haifa_change_pattern (next,
1339 PREDICATED_PAT (next));
1340 gcc_assert (success);
1341 }
1342 DEP_STATUS (modify_dep) |= DEP_CANCELLED;
1343 return DEP_CONTROL;
1344 }
1345
1346 if (PREDICATED_PAT (next) != NULL_RTX)
1347 {
1348 int tick = INSN_TICK (next);
1349 bool success = haifa_change_pattern (next,
1350 ORIG_PAT (next));
1351 INSN_TICK (next) = tick;
1352 gcc_assert (success);
1353 }
1354
1355 /* We can't handle the case where there are both speculative and control
1356 dependencies, so we return HARD_DEP in such a case. Also fail if
1357 we have speculative dependencies with not enough points, or more than
1358 one control dependency. */
1359 if ((n_spec > 0 && (n_control > 0 || n_replace > 0))
1360 || (n_spec > 0
1361 /* Too few points? */
1362 && ds_weak (new_ds) < spec_info->data_weakness_cutoff)
1363 || n_control > 0
1364 || n_replace > 0)
1365 return HARD_DEP;
1366
1367 return new_ds;
1368 }
1369 \f
1370 /* Pointer to the last instruction scheduled. */
1371 static rtx_insn *last_scheduled_insn;
1372
1373 /* Pointer to the last nondebug instruction scheduled within the
1374 block, or the prev_head of the scheduling block. Used by
1375 rank_for_schedule, so that insns independent of the last scheduled
1376 insn will be preferred over dependent instructions. */
1377 static rtx_insn *last_nondebug_scheduled_insn;
1378
1379 /* Pointer that iterates through the list of unscheduled insns if we
1380 have a dbg_cnt enabled. It always points at an insn prior to the
1381 first unscheduled one. */
1382 static rtx_insn *nonscheduled_insns_begin;
1383
1384 /* Compute cost of executing INSN.
1385 This is the number of cycles between instruction issue and
1386 instruction results. */
1387 int
1388 insn_cost (rtx_insn *insn)
1389 {
1390 int cost;
1391
1392 if (sched_fusion)
1393 return 0;
1394
1395 if (sel_sched_p ())
1396 {
1397 if (recog_memoized (insn) < 0)
1398 return 0;
1399
1400 cost = insn_default_latency (insn);
1401 if (cost < 0)
1402 cost = 0;
1403
1404 return cost;
1405 }
1406
1407 cost = INSN_COST (insn);
1408
1409 if (cost < 0)
1410 {
1411 /* A USE insn, or something else we don't need to
1412 understand. We can't pass these directly to
1413 result_ready_cost or insn_default_latency because it will
1414 trigger a fatal error for unrecognizable insns. */
1415 if (recog_memoized (insn) < 0)
1416 {
1417 INSN_COST (insn) = 0;
1418 return 0;
1419 }
1420 else
1421 {
1422 cost = insn_default_latency (insn);
1423 if (cost < 0)
1424 cost = 0;
1425
1426 INSN_COST (insn) = cost;
1427 }
1428 }
1429
1430 return cost;
1431 }
1432
1433 /* Compute cost of dependence LINK.
1434 This is the number of cycles between instruction issue and
1435 instruction results.
1436 ??? We also use this function to call recog_memoized on all insns. */
1437 int
1438 dep_cost_1 (dep_t link, dw_t dw)
1439 {
1440 rtx_insn *insn = DEP_PRO (link);
1441 rtx_insn *used = DEP_CON (link);
1442 int cost;
1443
1444 if (DEP_COST (link) != UNKNOWN_DEP_COST)
1445 return DEP_COST (link);
1446
1447 if (delay_htab)
1448 {
1449 struct delay_pair *delay_entry;
1450 delay_entry
1451 = delay_htab_i2->find_with_hash (used, htab_hash_pointer (used));
1452 if (delay_entry)
1453 {
1454 if (delay_entry->i1 == insn)
1455 {
1456 DEP_COST (link) = pair_delay (delay_entry);
1457 return DEP_COST (link);
1458 }
1459 }
1460 }
1461
1462 /* A USE insn should never require the value used to be computed.
1463 This allows the computation of a function's result and parameter
1464 values to overlap the return and call. We don't care about the
1465 dependence cost when only decreasing register pressure. */
1466 if (recog_memoized (used) < 0)
1467 {
1468 cost = 0;
1469 recog_memoized (insn);
1470 }
1471 else
1472 {
1473 enum reg_note dep_type = DEP_TYPE (link);
1474
1475 cost = insn_cost (insn);
1476
1477 if (INSN_CODE (insn) >= 0)
1478 {
1479 if (dep_type == REG_DEP_ANTI)
1480 cost = 0;
1481 else if (dep_type == REG_DEP_OUTPUT)
1482 {
1483 cost = (insn_default_latency (insn)
1484 - insn_default_latency (used));
1485 if (cost <= 0)
1486 cost = 1;
1487 }
1488 else if (bypass_p (insn))
1489 cost = insn_latency (insn, used);
1490 }
1491
1492
1493 if (targetm.sched.adjust_cost_2)
1494 cost = targetm.sched.adjust_cost_2 (used, (int) dep_type, insn, cost,
1495 dw);
1496 else if (targetm.sched.adjust_cost != NULL)
1497 {
1498 /* This variable is used for backward compatibility with the
1499 targets. */
1500 rtx_insn_list *dep_cost_rtx_link =
1501 alloc_INSN_LIST (NULL_RTX, NULL);
1502
1503 /* Make it self-cycled, so that if some tries to walk over this
1504 incomplete list he/she will be caught in an endless loop. */
1505 XEXP (dep_cost_rtx_link, 1) = dep_cost_rtx_link;
1506
1507 /* Targets use only REG_NOTE_KIND of the link. */
1508 PUT_REG_NOTE_KIND (dep_cost_rtx_link, DEP_TYPE (link));
1509
1510 cost = targetm.sched.adjust_cost (used, dep_cost_rtx_link,
1511 insn, cost);
1512
1513 free_INSN_LIST_node (dep_cost_rtx_link);
1514 }
1515
1516 if (cost < 0)
1517 cost = 0;
1518 }
1519
1520 DEP_COST (link) = cost;
1521 return cost;
1522 }
1523
1524 /* Compute cost of dependence LINK.
1525 This is the number of cycles between instruction issue and
1526 instruction results. */
1527 int
1528 dep_cost (dep_t link)
1529 {
1530 return dep_cost_1 (link, 0);
1531 }
1532
1533 /* Use this sel-sched.c friendly function in reorder2 instead of increasing
1534 INSN_PRIORITY explicitly. */
1535 void
1536 increase_insn_priority (rtx_insn *insn, int amount)
1537 {
1538 if (!sel_sched_p ())
1539 {
1540 /* We're dealing with haifa-sched.c INSN_PRIORITY. */
1541 if (INSN_PRIORITY_KNOWN (insn))
1542 INSN_PRIORITY (insn) += amount;
1543 }
1544 else
1545 {
1546 /* In sel-sched.c INSN_PRIORITY is not kept up to date.
1547 Use EXPR_PRIORITY instead. */
1548 sel_add_to_insn_priority (insn, amount);
1549 }
1550 }
1551
1552 /* Return 'true' if DEP should be included in priority calculations. */
1553 static bool
1554 contributes_to_priority_p (dep_t dep)
1555 {
1556 if (DEBUG_INSN_P (DEP_CON (dep))
1557 || DEBUG_INSN_P (DEP_PRO (dep)))
1558 return false;
1559
1560 /* Critical path is meaningful in block boundaries only. */
1561 if (!current_sched_info->contributes_to_priority (DEP_CON (dep),
1562 DEP_PRO (dep)))
1563 return false;
1564
1565 if (DEP_REPLACE (dep) != NULL)
1566 return false;
1567
1568 /* If flag COUNT_SPEC_IN_CRITICAL_PATH is set,
1569 then speculative instructions will less likely be
1570 scheduled. That is because the priority of
1571 their producers will increase, and, thus, the
1572 producers will more likely be scheduled, thus,
1573 resolving the dependence. */
1574 if (sched_deps_info->generate_spec_deps
1575 && !(spec_info->flags & COUNT_SPEC_IN_CRITICAL_PATH)
1576 && (DEP_STATUS (dep) & SPECULATIVE))
1577 return false;
1578
1579 return true;
1580 }
1581
1582 /* Compute the number of nondebug deps in list LIST for INSN. */
1583
1584 static int
1585 dep_list_size (rtx_insn *insn, sd_list_types_def list)
1586 {
1587 sd_iterator_def sd_it;
1588 dep_t dep;
1589 int dbgcount = 0, nodbgcount = 0;
1590
1591 if (!MAY_HAVE_DEBUG_INSNS)
1592 return sd_lists_size (insn, list);
1593
1594 FOR_EACH_DEP (insn, list, sd_it, dep)
1595 {
1596 if (DEBUG_INSN_P (DEP_CON (dep)))
1597 dbgcount++;
1598 else if (!DEBUG_INSN_P (DEP_PRO (dep)))
1599 nodbgcount++;
1600 }
1601
1602 gcc_assert (dbgcount + nodbgcount == sd_lists_size (insn, list));
1603
1604 return nodbgcount;
1605 }
1606
1607 bool sched_fusion;
1608
1609 /* Compute the priority number for INSN. */
1610 static int
1611 priority (rtx_insn *insn)
1612 {
1613 if (! INSN_P (insn))
1614 return 0;
1615
1616 /* We should not be interested in priority of an already scheduled insn. */
1617 gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
1618
1619 if (!INSN_PRIORITY_KNOWN (insn))
1620 {
1621 int this_priority = -1;
1622
1623 if (sched_fusion)
1624 {
1625 int this_fusion_priority;
1626
1627 targetm.sched.fusion_priority (insn, FUSION_MAX_PRIORITY,
1628 &this_fusion_priority, &this_priority);
1629 INSN_FUSION_PRIORITY (insn) = this_fusion_priority;
1630 }
1631 else if (dep_list_size (insn, SD_LIST_FORW) == 0)
1632 /* ??? We should set INSN_PRIORITY to insn_cost when and insn has
1633 some forward deps but all of them are ignored by
1634 contributes_to_priority hook. At the moment we set priority of
1635 such insn to 0. */
1636 this_priority = insn_cost (insn);
1637 else
1638 {
1639 rtx_insn *prev_first, *twin;
1640 basic_block rec;
1641
1642 /* For recovery check instructions we calculate priority slightly
1643 different than that of normal instructions. Instead of walking
1644 through INSN_FORW_DEPS (check) list, we walk through
1645 INSN_FORW_DEPS list of each instruction in the corresponding
1646 recovery block. */
1647
1648 /* Selective scheduling does not define RECOVERY_BLOCK macro. */
1649 rec = sel_sched_p () ? NULL : RECOVERY_BLOCK (insn);
1650 if (!rec || rec == EXIT_BLOCK_PTR_FOR_FN (cfun))
1651 {
1652 prev_first = PREV_INSN (insn);
1653 twin = insn;
1654 }
1655 else
1656 {
1657 prev_first = NEXT_INSN (BB_HEAD (rec));
1658 twin = PREV_INSN (BB_END (rec));
1659 }
1660
1661 do
1662 {
1663 sd_iterator_def sd_it;
1664 dep_t dep;
1665
1666 FOR_EACH_DEP (twin, SD_LIST_FORW, sd_it, dep)
1667 {
1668 rtx_insn *next;
1669 int next_priority;
1670
1671 next = DEP_CON (dep);
1672
1673 if (BLOCK_FOR_INSN (next) != rec)
1674 {
1675 int cost;
1676
1677 if (!contributes_to_priority_p (dep))
1678 continue;
1679
1680 if (twin == insn)
1681 cost = dep_cost (dep);
1682 else
1683 {
1684 struct _dep _dep1, *dep1 = &_dep1;
1685
1686 init_dep (dep1, insn, next, REG_DEP_ANTI);
1687
1688 cost = dep_cost (dep1);
1689 }
1690
1691 next_priority = cost + priority (next);
1692
1693 if (next_priority > this_priority)
1694 this_priority = next_priority;
1695 }
1696 }
1697
1698 twin = PREV_INSN (twin);
1699 }
1700 while (twin != prev_first);
1701 }
1702
1703 if (this_priority < 0)
1704 {
1705 gcc_assert (this_priority == -1);
1706
1707 this_priority = insn_cost (insn);
1708 }
1709
1710 INSN_PRIORITY (insn) = this_priority;
1711 INSN_PRIORITY_STATUS (insn) = 1;
1712 }
1713
1714 return INSN_PRIORITY (insn);
1715 }
1716 \f
1717 /* Macros and functions for keeping the priority queue sorted, and
1718 dealing with queuing and dequeuing of instructions. */
1719
1720 /* For each pressure class CL, set DEATH[CL] to the number of registers
1721 in that class that die in INSN. */
1722
1723 static void
1724 calculate_reg_deaths (rtx_insn *insn, int *death)
1725 {
1726 int i;
1727 struct reg_use_data *use;
1728
1729 for (i = 0; i < ira_pressure_classes_num; i++)
1730 death[ira_pressure_classes[i]] = 0;
1731 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
1732 if (dying_use_p (use))
1733 mark_regno_birth_or_death (0, death, use->regno, true);
1734 }
1735
1736 /* Setup info about the current register pressure impact of scheduling
1737 INSN at the current scheduling point. */
1738 static void
1739 setup_insn_reg_pressure_info (rtx_insn *insn)
1740 {
1741 int i, change, before, after, hard_regno;
1742 int excess_cost_change;
1743 machine_mode mode;
1744 enum reg_class cl;
1745 struct reg_pressure_data *pressure_info;
1746 int *max_reg_pressure;
1747 static int death[N_REG_CLASSES];
1748
1749 gcc_checking_assert (!DEBUG_INSN_P (insn));
1750
1751 excess_cost_change = 0;
1752 calculate_reg_deaths (insn, death);
1753 pressure_info = INSN_REG_PRESSURE (insn);
1754 max_reg_pressure = INSN_MAX_REG_PRESSURE (insn);
1755 gcc_assert (pressure_info != NULL && max_reg_pressure != NULL);
1756 for (i = 0; i < ira_pressure_classes_num; i++)
1757 {
1758 cl = ira_pressure_classes[i];
1759 gcc_assert (curr_reg_pressure[cl] >= 0);
1760 change = (int) pressure_info[i].set_increase - death[cl];
1761 before = MAX (0, max_reg_pressure[i] - sched_class_regs_num[cl]);
1762 after = MAX (0, max_reg_pressure[i] + change
1763 - sched_class_regs_num[cl]);
1764 hard_regno = ira_class_hard_regs[cl][0];
1765 gcc_assert (hard_regno >= 0);
1766 mode = reg_raw_mode[hard_regno];
1767 excess_cost_change += ((after - before)
1768 * (ira_memory_move_cost[mode][cl][0]
1769 + ira_memory_move_cost[mode][cl][1]));
1770 }
1771 INSN_REG_PRESSURE_EXCESS_COST_CHANGE (insn) = excess_cost_change;
1772 }
1773 \f
1774 /* This is the first page of code related to SCHED_PRESSURE_MODEL.
1775 It tries to make the scheduler take register pressure into account
1776 without introducing too many unnecessary stalls. It hooks into the
1777 main scheduling algorithm at several points:
1778
1779 - Before scheduling starts, model_start_schedule constructs a
1780 "model schedule" for the current block. This model schedule is
1781 chosen solely to keep register pressure down. It does not take the
1782 target's pipeline or the original instruction order into account,
1783 except as a tie-breaker. It also doesn't work to a particular
1784 pressure limit.
1785
1786 This model schedule gives us an idea of what pressure can be
1787 achieved for the block and gives us an example of a schedule that
1788 keeps to that pressure. It also makes the final schedule less
1789 dependent on the original instruction order. This is important
1790 because the original order can either be "wide" (many values live
1791 at once, such as in user-scheduled code) or "narrow" (few values
1792 live at once, such as after loop unrolling, where several
1793 iterations are executed sequentially).
1794
1795 We do not apply this model schedule to the rtx stream. We simply
1796 record it in model_schedule. We also compute the maximum pressure,
1797 MP, that was seen during this schedule.
1798
1799 - Instructions are added to the ready queue even if they require
1800 a stall. The length of the stall is instead computed as:
1801
1802 MAX (INSN_TICK (INSN) - clock_var, 0)
1803
1804 (= insn_delay). This allows rank_for_schedule to choose between
1805 introducing a deliberate stall or increasing pressure.
1806
1807 - Before sorting the ready queue, model_set_excess_costs assigns
1808 a pressure-based cost to each ready instruction in the queue.
1809 This is the instruction's INSN_REG_PRESSURE_EXCESS_COST_CHANGE
1810 (ECC for short) and is effectively measured in cycles.
1811
1812 - rank_for_schedule ranks instructions based on:
1813
1814 ECC (insn) + insn_delay (insn)
1815
1816 then as:
1817
1818 insn_delay (insn)
1819
1820 So, for example, an instruction X1 with an ECC of 1 that can issue
1821 now will win over an instruction X0 with an ECC of zero that would
1822 introduce a stall of one cycle. However, an instruction X2 with an
1823 ECC of 2 that can issue now will lose to both X0 and X1.
1824
1825 - When an instruction is scheduled, model_recompute updates the model
1826 schedule with the new pressures (some of which might now exceed the
1827 original maximum pressure MP). model_update_limit_points then searches
1828 for the new point of maximum pressure, if not already known. */
1829
1830 /* Used to separate high-verbosity debug information for SCHED_PRESSURE_MODEL
1831 from surrounding debug information. */
1832 #define MODEL_BAR \
1833 ";;\t\t+------------------------------------------------------\n"
1834
1835 /* Information about the pressure on a particular register class at a
1836 particular point of the model schedule. */
1837 struct model_pressure_data {
1838 /* The pressure at this point of the model schedule, or -1 if the
1839 point is associated with an instruction that has already been
1840 scheduled. */
1841 int ref_pressure;
1842
1843 /* The maximum pressure during or after this point of the model schedule. */
1844 int max_pressure;
1845 };
1846
1847 /* Per-instruction information that is used while building the model
1848 schedule. Here, "schedule" refers to the model schedule rather
1849 than the main schedule. */
1850 struct model_insn_info {
1851 /* The instruction itself. */
1852 rtx_insn *insn;
1853
1854 /* If this instruction is in model_worklist, these fields link to the
1855 previous (higher-priority) and next (lower-priority) instructions
1856 in the list. */
1857 struct model_insn_info *prev;
1858 struct model_insn_info *next;
1859
1860 /* While constructing the schedule, QUEUE_INDEX describes whether an
1861 instruction has already been added to the schedule (QUEUE_SCHEDULED),
1862 is in model_worklist (QUEUE_READY), or neither (QUEUE_NOWHERE).
1863 old_queue records the value that QUEUE_INDEX had before scheduling
1864 started, so that we can restore it once the schedule is complete. */
1865 int old_queue;
1866
1867 /* The relative importance of an unscheduled instruction. Higher
1868 values indicate greater importance. */
1869 unsigned int model_priority;
1870
1871 /* The length of the longest path of satisfied true dependencies
1872 that leads to this instruction. */
1873 unsigned int depth;
1874
1875 /* The length of the longest path of dependencies of any kind
1876 that leads from this instruction. */
1877 unsigned int alap;
1878
1879 /* The number of predecessor nodes that must still be scheduled. */
1880 int unscheduled_preds;
1881 };
1882
1883 /* Information about the pressure limit for a particular register class.
1884 This structure is used when applying a model schedule to the main
1885 schedule. */
1886 struct model_pressure_limit {
1887 /* The maximum register pressure seen in the original model schedule. */
1888 int orig_pressure;
1889
1890 /* The maximum register pressure seen in the current model schedule
1891 (which excludes instructions that have already been scheduled). */
1892 int pressure;
1893
1894 /* The point of the current model schedule at which PRESSURE is first
1895 reached. It is set to -1 if the value needs to be recomputed. */
1896 int point;
1897 };
1898
1899 /* Describes a particular way of measuring register pressure. */
1900 struct model_pressure_group {
1901 /* Index PCI describes the maximum pressure on ira_pressure_classes[PCI]. */
1902 struct model_pressure_limit limits[N_REG_CLASSES];
1903
1904 /* Index (POINT * ira_num_pressure_classes + PCI) describes the pressure
1905 on register class ira_pressure_classes[PCI] at point POINT of the
1906 current model schedule. A POINT of model_num_insns describes the
1907 pressure at the end of the schedule. */
1908 struct model_pressure_data *model;
1909 };
1910
1911 /* Index POINT gives the instruction at point POINT of the model schedule.
1912 This array doesn't change during main scheduling. */
1913 static vec<rtx_insn *> model_schedule;
1914
1915 /* The list of instructions in the model worklist, sorted in order of
1916 decreasing priority. */
1917 static struct model_insn_info *model_worklist;
1918
1919 /* Index I describes the instruction with INSN_LUID I. */
1920 static struct model_insn_info *model_insns;
1921
1922 /* The number of instructions in the model schedule. */
1923 static int model_num_insns;
1924
1925 /* The index of the first instruction in model_schedule that hasn't yet been
1926 added to the main schedule, or model_num_insns if all of them have. */
1927 static int model_curr_point;
1928
1929 /* Describes the pressure before each instruction in the model schedule. */
1930 static struct model_pressure_group model_before_pressure;
1931
1932 /* The first unused model_priority value (as used in model_insn_info). */
1933 static unsigned int model_next_priority;
1934
1935
1936 /* The model_pressure_data for ira_pressure_classes[PCI] in GROUP
1937 at point POINT of the model schedule. */
1938 #define MODEL_PRESSURE_DATA(GROUP, POINT, PCI) \
1939 (&(GROUP)->model[(POINT) * ira_pressure_classes_num + (PCI)])
1940
1941 /* The maximum pressure on ira_pressure_classes[PCI] in GROUP at or
1942 after point POINT of the model schedule. */
1943 #define MODEL_MAX_PRESSURE(GROUP, POINT, PCI) \
1944 (MODEL_PRESSURE_DATA (GROUP, POINT, PCI)->max_pressure)
1945
1946 /* The pressure on ira_pressure_classes[PCI] in GROUP at point POINT
1947 of the model schedule. */
1948 #define MODEL_REF_PRESSURE(GROUP, POINT, PCI) \
1949 (MODEL_PRESSURE_DATA (GROUP, POINT, PCI)->ref_pressure)
1950
1951 /* Information about INSN that is used when creating the model schedule. */
1952 #define MODEL_INSN_INFO(INSN) \
1953 (&model_insns[INSN_LUID (INSN)])
1954
1955 /* The instruction at point POINT of the model schedule. */
1956 #define MODEL_INSN(POINT) \
1957 (model_schedule[POINT])
1958
1959
1960 /* Return INSN's index in the model schedule, or model_num_insns if it
1961 doesn't belong to that schedule. */
1962
1963 static int
1964 model_index (rtx_insn *insn)
1965 {
1966 if (INSN_MODEL_INDEX (insn) == 0)
1967 return model_num_insns;
1968 return INSN_MODEL_INDEX (insn) - 1;
1969 }
1970
1971 /* Make sure that GROUP->limits is up-to-date for the current point
1972 of the model schedule. */
1973
1974 static void
1975 model_update_limit_points_in_group (struct model_pressure_group *group)
1976 {
1977 int pci, max_pressure, point;
1978
1979 for (pci = 0; pci < ira_pressure_classes_num; pci++)
1980 {
1981 /* We may have passed the final point at which the pressure in
1982 group->limits[pci].pressure was reached. Update the limit if so. */
1983 max_pressure = MODEL_MAX_PRESSURE (group, model_curr_point, pci);
1984 group->limits[pci].pressure = max_pressure;
1985
1986 /* Find the point at which MAX_PRESSURE is first reached. We need
1987 to search in three cases:
1988
1989 - We've already moved past the previous pressure point.
1990 In this case we search forward from model_curr_point.
1991
1992 - We scheduled the previous point of maximum pressure ahead of
1993 its position in the model schedule, but doing so didn't bring
1994 the pressure point earlier. In this case we search forward
1995 from that previous pressure point.
1996
1997 - Scheduling an instruction early caused the maximum pressure
1998 to decrease. In this case we will have set the pressure
1999 point to -1, and we search forward from model_curr_point. */
2000 point = MAX (group->limits[pci].point, model_curr_point);
2001 while (point < model_num_insns
2002 && MODEL_REF_PRESSURE (group, point, pci) < max_pressure)
2003 point++;
2004 group->limits[pci].point = point;
2005
2006 gcc_assert (MODEL_REF_PRESSURE (group, point, pci) == max_pressure);
2007 gcc_assert (MODEL_MAX_PRESSURE (group, point, pci) == max_pressure);
2008 }
2009 }
2010
2011 /* Make sure that all register-pressure limits are up-to-date for the
2012 current position in the model schedule. */
2013
2014 static void
2015 model_update_limit_points (void)
2016 {
2017 model_update_limit_points_in_group (&model_before_pressure);
2018 }
2019
2020 /* Return the model_index of the last unscheduled use in chain USE
2021 outside of USE's instruction. Return -1 if there are no other uses,
2022 or model_num_insns if the register is live at the end of the block. */
2023
2024 static int
2025 model_last_use_except (struct reg_use_data *use)
2026 {
2027 struct reg_use_data *next;
2028 int last, index;
2029
2030 last = -1;
2031 for (next = use->next_regno_use; next != use; next = next->next_regno_use)
2032 if (NONDEBUG_INSN_P (next->insn)
2033 && QUEUE_INDEX (next->insn) != QUEUE_SCHEDULED)
2034 {
2035 index = model_index (next->insn);
2036 if (index == model_num_insns)
2037 return model_num_insns;
2038 if (last < index)
2039 last = index;
2040 }
2041 return last;
2042 }
2043
2044 /* An instruction with model_index POINT has just been scheduled, and it
2045 adds DELTA to the pressure on ira_pressure_classes[PCI] after POINT - 1.
2046 Update MODEL_REF_PRESSURE (GROUP, POINT, PCI) and
2047 MODEL_MAX_PRESSURE (GROUP, POINT, PCI) accordingly. */
2048
2049 static void
2050 model_start_update_pressure (struct model_pressure_group *group,
2051 int point, int pci, int delta)
2052 {
2053 int next_max_pressure;
2054
2055 if (point == model_num_insns)
2056 {
2057 /* The instruction wasn't part of the model schedule; it was moved
2058 from a different block. Update the pressure for the end of
2059 the model schedule. */
2060 MODEL_REF_PRESSURE (group, point, pci) += delta;
2061 MODEL_MAX_PRESSURE (group, point, pci) += delta;
2062 }
2063 else
2064 {
2065 /* Record that this instruction has been scheduled. Nothing now
2066 changes between POINT and POINT + 1, so get the maximum pressure
2067 from the latter. If the maximum pressure decreases, the new
2068 pressure point may be before POINT. */
2069 MODEL_REF_PRESSURE (group, point, pci) = -1;
2070 next_max_pressure = MODEL_MAX_PRESSURE (group, point + 1, pci);
2071 if (MODEL_MAX_PRESSURE (group, point, pci) > next_max_pressure)
2072 {
2073 MODEL_MAX_PRESSURE (group, point, pci) = next_max_pressure;
2074 if (group->limits[pci].point == point)
2075 group->limits[pci].point = -1;
2076 }
2077 }
2078 }
2079
2080 /* Record that scheduling a later instruction has changed the pressure
2081 at point POINT of the model schedule by DELTA (which might be 0).
2082 Update GROUP accordingly. Return nonzero if these changes might
2083 trigger changes to previous points as well. */
2084
2085 static int
2086 model_update_pressure (struct model_pressure_group *group,
2087 int point, int pci, int delta)
2088 {
2089 int ref_pressure, max_pressure, next_max_pressure;
2090
2091 /* If POINT hasn't yet been scheduled, update its pressure. */
2092 ref_pressure = MODEL_REF_PRESSURE (group, point, pci);
2093 if (ref_pressure >= 0 && delta != 0)
2094 {
2095 ref_pressure += delta;
2096 MODEL_REF_PRESSURE (group, point, pci) = ref_pressure;
2097
2098 /* Check whether the maximum pressure in the overall schedule
2099 has increased. (This means that the MODEL_MAX_PRESSURE of
2100 every point <= POINT will need to increase too; see below.) */
2101 if (group->limits[pci].pressure < ref_pressure)
2102 group->limits[pci].pressure = ref_pressure;
2103
2104 /* If we are at maximum pressure, and the maximum pressure
2105 point was previously unknown or later than POINT,
2106 bring it forward. */
2107 if (group->limits[pci].pressure == ref_pressure
2108 && !IN_RANGE (group->limits[pci].point, 0, point))
2109 group->limits[pci].point = point;
2110
2111 /* If POINT used to be the point of maximum pressure, but isn't
2112 any longer, we need to recalculate it using a forward walk. */
2113 if (group->limits[pci].pressure > ref_pressure
2114 && group->limits[pci].point == point)
2115 group->limits[pci].point = -1;
2116 }
2117
2118 /* Update the maximum pressure at POINT. Changes here might also
2119 affect the maximum pressure at POINT - 1. */
2120 next_max_pressure = MODEL_MAX_PRESSURE (group, point + 1, pci);
2121 max_pressure = MAX (ref_pressure, next_max_pressure);
2122 if (MODEL_MAX_PRESSURE (group, point, pci) != max_pressure)
2123 {
2124 MODEL_MAX_PRESSURE (group, point, pci) = max_pressure;
2125 return 1;
2126 }
2127 return 0;
2128 }
2129
2130 /* INSN has just been scheduled. Update the model schedule accordingly. */
2131
2132 static void
2133 model_recompute (rtx_insn *insn)
2134 {
2135 struct {
2136 int last_use;
2137 int regno;
2138 } uses[FIRST_PSEUDO_REGISTER + MAX_RECOG_OPERANDS];
2139 struct reg_use_data *use;
2140 struct reg_pressure_data *reg_pressure;
2141 int delta[N_REG_CLASSES];
2142 int pci, point, mix, new_last, cl, ref_pressure, queue;
2143 unsigned int i, num_uses, num_pending_births;
2144 bool print_p;
2145
2146 /* The destinations of INSN were previously live from POINT onwards, but are
2147 now live from model_curr_point onwards. Set up DELTA accordingly. */
2148 point = model_index (insn);
2149 reg_pressure = INSN_REG_PRESSURE (insn);
2150 for (pci = 0; pci < ira_pressure_classes_num; pci++)
2151 {
2152 cl = ira_pressure_classes[pci];
2153 delta[cl] = reg_pressure[pci].set_increase;
2154 }
2155
2156 /* Record which registers previously died at POINT, but which now die
2157 before POINT. Adjust DELTA so that it represents the effect of
2158 this change after POINT - 1. Set NUM_PENDING_BIRTHS to the number of
2159 registers that will be born in the range [model_curr_point, POINT). */
2160 num_uses = 0;
2161 num_pending_births = 0;
2162 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
2163 {
2164 new_last = model_last_use_except (use);
2165 if (new_last < point)
2166 {
2167 gcc_assert (num_uses < ARRAY_SIZE (uses));
2168 uses[num_uses].last_use = new_last;
2169 uses[num_uses].regno = use->regno;
2170 /* This register is no longer live after POINT - 1. */
2171 mark_regno_birth_or_death (NULL, delta, use->regno, false);
2172 num_uses++;
2173 if (new_last >= 0)
2174 num_pending_births++;
2175 }
2176 }
2177
2178 /* Update the MODEL_REF_PRESSURE and MODEL_MAX_PRESSURE for POINT.
2179 Also set each group pressure limit for POINT. */
2180 for (pci = 0; pci < ira_pressure_classes_num; pci++)
2181 {
2182 cl = ira_pressure_classes[pci];
2183 model_start_update_pressure (&model_before_pressure,
2184 point, pci, delta[cl]);
2185 }
2186
2187 /* Walk the model schedule backwards, starting immediately before POINT. */
2188 print_p = false;
2189 if (point != model_curr_point)
2190 do
2191 {
2192 point--;
2193 insn = MODEL_INSN (point);
2194 queue = QUEUE_INDEX (insn);
2195
2196 if (queue != QUEUE_SCHEDULED)
2197 {
2198 /* DELTA describes the effect of the move on the register pressure
2199 after POINT. Make it describe the effect on the pressure
2200 before POINT. */
2201 i = 0;
2202 while (i < num_uses)
2203 {
2204 if (uses[i].last_use == point)
2205 {
2206 /* This register is now live again. */
2207 mark_regno_birth_or_death (NULL, delta,
2208 uses[i].regno, true);
2209
2210 /* Remove this use from the array. */
2211 uses[i] = uses[num_uses - 1];
2212 num_uses--;
2213 num_pending_births--;
2214 }
2215 else
2216 i++;
2217 }
2218
2219 if (sched_verbose >= 5)
2220 {
2221 if (!print_p)
2222 {
2223 fprintf (sched_dump, MODEL_BAR);
2224 fprintf (sched_dump, ";;\t\t| New pressure for model"
2225 " schedule\n");
2226 fprintf (sched_dump, MODEL_BAR);
2227 print_p = true;
2228 }
2229
2230 fprintf (sched_dump, ";;\t\t| %3d %4d %-30s ",
2231 point, INSN_UID (insn),
2232 str_pattern_slim (PATTERN (insn)));
2233 for (pci = 0; pci < ira_pressure_classes_num; pci++)
2234 {
2235 cl = ira_pressure_classes[pci];
2236 ref_pressure = MODEL_REF_PRESSURE (&model_before_pressure,
2237 point, pci);
2238 fprintf (sched_dump, " %s:[%d->%d]",
2239 reg_class_names[ira_pressure_classes[pci]],
2240 ref_pressure, ref_pressure + delta[cl]);
2241 }
2242 fprintf (sched_dump, "\n");
2243 }
2244 }
2245
2246 /* Adjust the pressure at POINT. Set MIX to nonzero if POINT - 1
2247 might have changed as well. */
2248 mix = num_pending_births;
2249 for (pci = 0; pci < ira_pressure_classes_num; pci++)
2250 {
2251 cl = ira_pressure_classes[pci];
2252 mix |= delta[cl];
2253 mix |= model_update_pressure (&model_before_pressure,
2254 point, pci, delta[cl]);
2255 }
2256 }
2257 while (mix && point > model_curr_point);
2258
2259 if (print_p)
2260 fprintf (sched_dump, MODEL_BAR);
2261 }
2262
2263 /* After DEP, which was cancelled, has been resolved for insn NEXT,
2264 check whether the insn's pattern needs restoring. */
2265 static bool
2266 must_restore_pattern_p (rtx_insn *next, dep_t dep)
2267 {
2268 if (QUEUE_INDEX (next) == QUEUE_SCHEDULED)
2269 return false;
2270
2271 if (DEP_TYPE (dep) == REG_DEP_CONTROL)
2272 {
2273 gcc_assert (ORIG_PAT (next) != NULL_RTX);
2274 gcc_assert (next == DEP_CON (dep));
2275 }
2276 else
2277 {
2278 struct dep_replacement *desc = DEP_REPLACE (dep);
2279 if (desc->insn != next)
2280 {
2281 gcc_assert (*desc->loc == desc->orig);
2282 return false;
2283 }
2284 }
2285 return true;
2286 }
2287 \f
2288 /* model_spill_cost (CL, P, P') returns the cost of increasing the
2289 pressure on CL from P to P'. We use this to calculate a "base ECC",
2290 baseECC (CL, X), for each pressure class CL and each instruction X.
2291 Supposing X changes the pressure on CL from P to P', and that the
2292 maximum pressure on CL in the current model schedule is MP', then:
2293
2294 * if X occurs before or at the next point of maximum pressure in
2295 the model schedule and P' > MP', then:
2296
2297 baseECC (CL, X) = model_spill_cost (CL, MP, P')
2298
2299 The idea is that the pressure after scheduling a fixed set of
2300 instructions -- in this case, the set up to and including the
2301 next maximum pressure point -- is going to be the same regardless
2302 of the order; we simply want to keep the intermediate pressure
2303 under control. Thus X has a cost of zero unless scheduling it
2304 now would exceed MP'.
2305
2306 If all increases in the set are by the same amount, no zero-cost
2307 instruction will ever cause the pressure to exceed MP'. However,
2308 if X is instead moved past an instruction X' with pressure in the
2309 range (MP' - (P' - P), MP'), the pressure at X' will increase
2310 beyond MP'. Since baseECC is very much a heuristic anyway,
2311 it doesn't seem worth the overhead of tracking cases like these.
2312
2313 The cost of exceeding MP' is always based on the original maximum
2314 pressure MP. This is so that going 2 registers over the original
2315 limit has the same cost regardless of whether it comes from two
2316 separate +1 deltas or from a single +2 delta.
2317
2318 * if X occurs after the next point of maximum pressure in the model
2319 schedule and P' > P, then:
2320
2321 baseECC (CL, X) = model_spill_cost (CL, MP, MP' + (P' - P))
2322
2323 That is, if we move X forward across a point of maximum pressure,
2324 and if X increases the pressure by P' - P, then we conservatively
2325 assume that scheduling X next would increase the maximum pressure
2326 by P' - P. Again, the cost of doing this is based on the original
2327 maximum pressure MP, for the same reason as above.
2328
2329 * if P' < P, P > MP, and X occurs at or after the next point of
2330 maximum pressure, then:
2331
2332 baseECC (CL, X) = -model_spill_cost (CL, MAX (MP, P'), P)
2333
2334 That is, if we have already exceeded the original maximum pressure MP,
2335 and if X might reduce the maximum pressure again -- or at least push
2336 it further back, and thus allow more scheduling freedom -- it is given
2337 a negative cost to reflect the improvement.
2338
2339 * otherwise,
2340
2341 baseECC (CL, X) = 0
2342
2343 In this case, X is not expected to affect the maximum pressure MP',
2344 so it has zero cost.
2345
2346 We then create a combined value baseECC (X) that is the sum of
2347 baseECC (CL, X) for each pressure class CL.
2348
2349 baseECC (X) could itself be used as the ECC value described above.
2350 However, this is often too conservative, in the sense that it
2351 tends to make high-priority instructions that increase pressure
2352 wait too long in cases where introducing a spill would be better.
2353 For this reason the final ECC is a priority-adjusted form of
2354 baseECC (X). Specifically, we calculate:
2355
2356 P (X) = INSN_PRIORITY (X) - insn_delay (X) - baseECC (X)
2357 baseP = MAX { P (X) | baseECC (X) <= 0 }
2358
2359 Then:
2360
2361 ECC (X) = MAX (MIN (baseP - P (X), baseECC (X)), 0)
2362
2363 Thus an instruction's effect on pressure is ignored if it has a high
2364 enough priority relative to the ones that don't increase pressure.
2365 Negative values of baseECC (X) do not increase the priority of X
2366 itself, but they do make it harder for other instructions to
2367 increase the pressure further.
2368
2369 This pressure cost is deliberately timid. The intention has been
2370 to choose a heuristic that rarely interferes with the normal list
2371 scheduler in cases where that scheduler would produce good code.
2372 We simply want to curb some of its worst excesses. */
2373
2374 /* Return the cost of increasing the pressure in class CL from FROM to TO.
2375
2376 Here we use the very simplistic cost model that every register above
2377 sched_class_regs_num[CL] has a spill cost of 1. We could use other
2378 measures instead, such as one based on MEMORY_MOVE_COST. However:
2379
2380 (1) In order for an instruction to be scheduled, the higher cost
2381 would need to be justified in a single saving of that many stalls.
2382 This is overly pessimistic, because the benefit of spilling is
2383 often to avoid a sequence of several short stalls rather than
2384 a single long one.
2385
2386 (2) The cost is still arbitrary. Because we are not allocating
2387 registers during scheduling, we have no way of knowing for
2388 sure how many memory accesses will be required by each spill,
2389 where the spills will be placed within the block, or even
2390 which block(s) will contain the spills.
2391
2392 So a higher cost than 1 is often too conservative in practice,
2393 forcing blocks to contain unnecessary stalls instead of spill code.
2394 The simple cost below seems to be the best compromise. It reduces
2395 the interference with the normal list scheduler, which helps make
2396 it more suitable for a default-on option. */
2397
2398 static int
2399 model_spill_cost (int cl, int from, int to)
2400 {
2401 from = MAX (from, sched_class_regs_num[cl]);
2402 return MAX (to, from) - from;
2403 }
2404
2405 /* Return baseECC (ira_pressure_classes[PCI], POINT), given that
2406 P = curr_reg_pressure[ira_pressure_classes[PCI]] and that
2407 P' = P + DELTA. */
2408
2409 static int
2410 model_excess_group_cost (struct model_pressure_group *group,
2411 int point, int pci, int delta)
2412 {
2413 int pressure, cl;
2414
2415 cl = ira_pressure_classes[pci];
2416 if (delta < 0 && point >= group->limits[pci].point)
2417 {
2418 pressure = MAX (group->limits[pci].orig_pressure,
2419 curr_reg_pressure[cl] + delta);
2420 return -model_spill_cost (cl, pressure, curr_reg_pressure[cl]);
2421 }
2422
2423 if (delta > 0)
2424 {
2425 if (point > group->limits[pci].point)
2426 pressure = group->limits[pci].pressure + delta;
2427 else
2428 pressure = curr_reg_pressure[cl] + delta;
2429
2430 if (pressure > group->limits[pci].pressure)
2431 return model_spill_cost (cl, group->limits[pci].orig_pressure,
2432 pressure);
2433 }
2434
2435 return 0;
2436 }
2437
2438 /* Return baseECC (MODEL_INSN (INSN)). Dump the costs to sched_dump
2439 if PRINT_P. */
2440
2441 static int
2442 model_excess_cost (rtx_insn *insn, bool print_p)
2443 {
2444 int point, pci, cl, cost, this_cost, delta;
2445 struct reg_pressure_data *insn_reg_pressure;
2446 int insn_death[N_REG_CLASSES];
2447
2448 calculate_reg_deaths (insn, insn_death);
2449 point = model_index (insn);
2450 insn_reg_pressure = INSN_REG_PRESSURE (insn);
2451 cost = 0;
2452
2453 if (print_p)
2454 fprintf (sched_dump, ";;\t\t| %3d %4d | %4d %+3d |", point,
2455 INSN_UID (insn), INSN_PRIORITY (insn), insn_delay (insn));
2456
2457 /* Sum up the individual costs for each register class. */
2458 for (pci = 0; pci < ira_pressure_classes_num; pci++)
2459 {
2460 cl = ira_pressure_classes[pci];
2461 delta = insn_reg_pressure[pci].set_increase - insn_death[cl];
2462 this_cost = model_excess_group_cost (&model_before_pressure,
2463 point, pci, delta);
2464 cost += this_cost;
2465 if (print_p)
2466 fprintf (sched_dump, " %s:[%d base cost %d]",
2467 reg_class_names[cl], delta, this_cost);
2468 }
2469
2470 if (print_p)
2471 fprintf (sched_dump, "\n");
2472
2473 return cost;
2474 }
2475
2476 /* Dump the next points of maximum pressure for GROUP. */
2477
2478 static void
2479 model_dump_pressure_points (struct model_pressure_group *group)
2480 {
2481 int pci, cl;
2482
2483 fprintf (sched_dump, ";;\t\t| pressure points");
2484 for (pci = 0; pci < ira_pressure_classes_num; pci++)
2485 {
2486 cl = ira_pressure_classes[pci];
2487 fprintf (sched_dump, " %s:[%d->%d at ", reg_class_names[cl],
2488 curr_reg_pressure[cl], group->limits[pci].pressure);
2489 if (group->limits[pci].point < model_num_insns)
2490 fprintf (sched_dump, "%d:%d]", group->limits[pci].point,
2491 INSN_UID (MODEL_INSN (group->limits[pci].point)));
2492 else
2493 fprintf (sched_dump, "end]");
2494 }
2495 fprintf (sched_dump, "\n");
2496 }
2497
2498 /* Set INSN_REG_PRESSURE_EXCESS_COST_CHANGE for INSNS[0...COUNT-1]. */
2499
2500 static void
2501 model_set_excess_costs (rtx_insn **insns, int count)
2502 {
2503 int i, cost, priority_base, priority;
2504 bool print_p;
2505
2506 /* Record the baseECC value for each instruction in the model schedule,
2507 except that negative costs are converted to zero ones now rather than
2508 later. Do not assign a cost to debug instructions, since they must
2509 not change code-generation decisions. Experiments suggest we also
2510 get better results by not assigning a cost to instructions from
2511 a different block.
2512
2513 Set PRIORITY_BASE to baseP in the block comment above. This is the
2514 maximum priority of the "cheap" instructions, which should always
2515 include the next model instruction. */
2516 priority_base = 0;
2517 print_p = false;
2518 for (i = 0; i < count; i++)
2519 if (INSN_MODEL_INDEX (insns[i]))
2520 {
2521 if (sched_verbose >= 6 && !print_p)
2522 {
2523 fprintf (sched_dump, MODEL_BAR);
2524 fprintf (sched_dump, ";;\t\t| Pressure costs for ready queue\n");
2525 model_dump_pressure_points (&model_before_pressure);
2526 fprintf (sched_dump, MODEL_BAR);
2527 print_p = true;
2528 }
2529 cost = model_excess_cost (insns[i], print_p);
2530 if (cost <= 0)
2531 {
2532 priority = INSN_PRIORITY (insns[i]) - insn_delay (insns[i]) - cost;
2533 priority_base = MAX (priority_base, priority);
2534 cost = 0;
2535 }
2536 INSN_REG_PRESSURE_EXCESS_COST_CHANGE (insns[i]) = cost;
2537 }
2538 if (print_p)
2539 fprintf (sched_dump, MODEL_BAR);
2540
2541 /* Use MAX (baseECC, 0) and baseP to calculcate ECC for each
2542 instruction. */
2543 for (i = 0; i < count; i++)
2544 {
2545 cost = INSN_REG_PRESSURE_EXCESS_COST_CHANGE (insns[i]);
2546 priority = INSN_PRIORITY (insns[i]) - insn_delay (insns[i]);
2547 if (cost > 0 && priority > priority_base)
2548 {
2549 cost += priority_base - priority;
2550 INSN_REG_PRESSURE_EXCESS_COST_CHANGE (insns[i]) = MAX (cost, 0);
2551 }
2552 }
2553 }
2554 \f
2555
2556 /* Enum of rank_for_schedule heuristic decisions. */
2557 enum rfs_decision {
2558 RFS_LIVE_RANGE_SHRINK1, RFS_LIVE_RANGE_SHRINK2,
2559 RFS_SCHED_GROUP, RFS_PRESSURE_DELAY, RFS_PRESSURE_TICK,
2560 RFS_FEEDS_BACKTRACK_INSN, RFS_PRIORITY, RFS_SPECULATION,
2561 RFS_SCHED_RANK, RFS_LAST_INSN, RFS_PRESSURE_INDEX,
2562 RFS_DEP_COUNT, RFS_TIE, RFS_FUSION, RFS_N };
2563
2564 /* Corresponding strings for print outs. */
2565 static const char *rfs_str[RFS_N] = {
2566 "RFS_LIVE_RANGE_SHRINK1", "RFS_LIVE_RANGE_SHRINK2",
2567 "RFS_SCHED_GROUP", "RFS_PRESSURE_DELAY", "RFS_PRESSURE_TICK",
2568 "RFS_FEEDS_BACKTRACK_INSN", "RFS_PRIORITY", "RFS_SPECULATION",
2569 "RFS_SCHED_RANK", "RFS_LAST_INSN", "RFS_PRESSURE_INDEX",
2570 "RFS_DEP_COUNT", "RFS_TIE", "RFS_FUSION" };
2571
2572 /* Statistical breakdown of rank_for_schedule decisions. */
2573 struct rank_for_schedule_stats_t { unsigned stats[RFS_N]; };
2574 static rank_for_schedule_stats_t rank_for_schedule_stats;
2575
2576 /* Return the result of comparing insns TMP and TMP2 and update
2577 Rank_For_Schedule statistics. */
2578 static int
2579 rfs_result (enum rfs_decision decision, int result, rtx tmp, rtx tmp2)
2580 {
2581 ++rank_for_schedule_stats.stats[decision];
2582 if (result < 0)
2583 INSN_LAST_RFS_WIN (tmp) = decision;
2584 else if (result > 0)
2585 INSN_LAST_RFS_WIN (tmp2) = decision;
2586 else
2587 gcc_unreachable ();
2588 return result;
2589 }
2590
2591 /* Sorting predicate to move DEBUG_INSNs to the top of ready list, while
2592 keeping normal insns in original order. */
2593
2594 static int
2595 rank_for_schedule_debug (const void *x, const void *y)
2596 {
2597 rtx_insn *tmp = *(rtx_insn * const *) y;
2598 rtx_insn *tmp2 = *(rtx_insn * const *) x;
2599
2600 /* Schedule debug insns as early as possible. */
2601 if (DEBUG_INSN_P (tmp) && !DEBUG_INSN_P (tmp2))
2602 return -1;
2603 else if (!DEBUG_INSN_P (tmp) && DEBUG_INSN_P (tmp2))
2604 return 1;
2605 else if (DEBUG_INSN_P (tmp) && DEBUG_INSN_P (tmp2))
2606 return INSN_LUID (tmp) - INSN_LUID (tmp2);
2607 else
2608 return INSN_RFS_DEBUG_ORIG_ORDER (tmp2) - INSN_RFS_DEBUG_ORIG_ORDER (tmp);
2609 }
2610
2611 /* Returns a positive value if x is preferred; returns a negative value if
2612 y is preferred. Should never return 0, since that will make the sort
2613 unstable. */
2614
2615 static int
2616 rank_for_schedule (const void *x, const void *y)
2617 {
2618 rtx_insn *tmp = *(rtx_insn * const *) y;
2619 rtx_insn *tmp2 = *(rtx_insn * const *) x;
2620 int tmp_class, tmp2_class;
2621 int val, priority_val, info_val, diff;
2622
2623 if (live_range_shrinkage_p)
2624 {
2625 /* Don't use SCHED_PRESSURE_MODEL -- it results in much worse
2626 code. */
2627 gcc_assert (sched_pressure == SCHED_PRESSURE_WEIGHTED);
2628 if ((INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp) < 0
2629 || INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp2) < 0)
2630 && (diff = (INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp)
2631 - INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp2))) != 0)
2632 return rfs_result (RFS_LIVE_RANGE_SHRINK1, diff, tmp, tmp2);
2633 /* Sort by INSN_LUID (original insn order), so that we make the
2634 sort stable. This minimizes instruction movement, thus
2635 minimizing sched's effect on debugging and cross-jumping. */
2636 return rfs_result (RFS_LIVE_RANGE_SHRINK2,
2637 INSN_LUID (tmp) - INSN_LUID (tmp2), tmp, tmp2);
2638 }
2639
2640 /* The insn in a schedule group should be issued the first. */
2641 if (flag_sched_group_heuristic &&
2642 SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
2643 return rfs_result (RFS_SCHED_GROUP, SCHED_GROUP_P (tmp2) ? 1 : -1,
2644 tmp, tmp2);
2645
2646 /* Make sure that priority of TMP and TMP2 are initialized. */
2647 gcc_assert (INSN_PRIORITY_KNOWN (tmp) && INSN_PRIORITY_KNOWN (tmp2));
2648
2649 if (sched_fusion)
2650 {
2651 /* The instruction that has the same fusion priority as the last
2652 instruction is the instruction we picked next. If that is not
2653 the case, we sort ready list firstly by fusion priority, then
2654 by priority, and at last by INSN_LUID. */
2655 int a = INSN_FUSION_PRIORITY (tmp);
2656 int b = INSN_FUSION_PRIORITY (tmp2);
2657 int last = -1;
2658
2659 if (last_nondebug_scheduled_insn
2660 && !NOTE_P (last_nondebug_scheduled_insn)
2661 && BLOCK_FOR_INSN (tmp)
2662 == BLOCK_FOR_INSN (last_nondebug_scheduled_insn))
2663 last = INSN_FUSION_PRIORITY (last_nondebug_scheduled_insn);
2664
2665 if (a != last && b != last)
2666 {
2667 if (a == b)
2668 {
2669 a = INSN_PRIORITY (tmp);
2670 b = INSN_PRIORITY (tmp2);
2671 }
2672 if (a != b)
2673 return rfs_result (RFS_FUSION, b - a, tmp, tmp2);
2674 else
2675 return rfs_result (RFS_FUSION,
2676 INSN_LUID (tmp) - INSN_LUID (tmp2), tmp, tmp2);
2677 }
2678 else if (a == b)
2679 {
2680 gcc_assert (last_nondebug_scheduled_insn
2681 && !NOTE_P (last_nondebug_scheduled_insn));
2682 last = INSN_PRIORITY (last_nondebug_scheduled_insn);
2683
2684 a = abs (INSN_PRIORITY (tmp) - last);
2685 b = abs (INSN_PRIORITY (tmp2) - last);
2686 if (a != b)
2687 return rfs_result (RFS_FUSION, a - b, tmp, tmp2);
2688 else
2689 return rfs_result (RFS_FUSION,
2690 INSN_LUID (tmp) - INSN_LUID (tmp2), tmp, tmp2);
2691 }
2692 else if (a == last)
2693 return rfs_result (RFS_FUSION, -1, tmp, tmp2);
2694 else
2695 return rfs_result (RFS_FUSION, 1, tmp, tmp2);
2696 }
2697
2698 if (sched_pressure != SCHED_PRESSURE_NONE)
2699 {
2700 /* Prefer insn whose scheduling results in the smallest register
2701 pressure excess. */
2702 if ((diff = (INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp)
2703 + insn_delay (tmp)
2704 - INSN_REG_PRESSURE_EXCESS_COST_CHANGE (tmp2)
2705 - insn_delay (tmp2))))
2706 return rfs_result (RFS_PRESSURE_DELAY, diff, tmp, tmp2);
2707 }
2708
2709 if (sched_pressure != SCHED_PRESSURE_NONE
2710 && (INSN_TICK (tmp2) > clock_var || INSN_TICK (tmp) > clock_var)
2711 && INSN_TICK (tmp2) != INSN_TICK (tmp))
2712 {
2713 diff = INSN_TICK (tmp) - INSN_TICK (tmp2);
2714 return rfs_result (RFS_PRESSURE_TICK, diff, tmp, tmp2);
2715 }
2716
2717 /* If we are doing backtracking in this schedule, prefer insns that
2718 have forward dependencies with negative cost against an insn that
2719 was already scheduled. */
2720 if (current_sched_info->flags & DO_BACKTRACKING)
2721 {
2722 priority_val = FEEDS_BACKTRACK_INSN (tmp2) - FEEDS_BACKTRACK_INSN (tmp);
2723 if (priority_val)
2724 return rfs_result (RFS_FEEDS_BACKTRACK_INSN, priority_val, tmp, tmp2);
2725 }
2726
2727 /* Prefer insn with higher priority. */
2728 priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
2729
2730 if (flag_sched_critical_path_heuristic && priority_val)
2731 return rfs_result (RFS_PRIORITY, priority_val, tmp, tmp2);
2732
2733 if (PARAM_VALUE (PARAM_SCHED_AUTOPREF_QUEUE_DEPTH) >= 0)
2734 {
2735 int autopref = autopref_rank_for_schedule (tmp, tmp2);
2736 if (autopref != 0)
2737 return autopref;
2738 }
2739
2740 /* Prefer speculative insn with greater dependencies weakness. */
2741 if (flag_sched_spec_insn_heuristic && spec_info)
2742 {
2743 ds_t ds1, ds2;
2744 dw_t dw1, dw2;
2745 int dw;
2746
2747 ds1 = TODO_SPEC (tmp) & SPECULATIVE;
2748 if (ds1)
2749 dw1 = ds_weak (ds1);
2750 else
2751 dw1 = NO_DEP_WEAK;
2752
2753 ds2 = TODO_SPEC (tmp2) & SPECULATIVE;
2754 if (ds2)
2755 dw2 = ds_weak (ds2);
2756 else
2757 dw2 = NO_DEP_WEAK;
2758
2759 dw = dw2 - dw1;
2760 if (dw > (NO_DEP_WEAK / 8) || dw < -(NO_DEP_WEAK / 8))
2761 return rfs_result (RFS_SPECULATION, dw, tmp, tmp2);
2762 }
2763
2764 info_val = (*current_sched_info->rank) (tmp, tmp2);
2765 if (flag_sched_rank_heuristic && info_val)
2766 return rfs_result (RFS_SCHED_RANK, info_val, tmp, tmp2);
2767
2768 /* Compare insns based on their relation to the last scheduled
2769 non-debug insn. */
2770 if (flag_sched_last_insn_heuristic && last_nondebug_scheduled_insn)
2771 {
2772 dep_t dep1;
2773 dep_t dep2;
2774 rtx_insn *last = last_nondebug_scheduled_insn;
2775
2776 /* Classify the instructions into three classes:
2777 1) Data dependent on last schedule insn.
2778 2) Anti/Output dependent on last scheduled insn.
2779 3) Independent of last scheduled insn, or has latency of one.
2780 Choose the insn from the highest numbered class if different. */
2781 dep1 = sd_find_dep_between (last, tmp, true);
2782
2783 if (dep1 == NULL || dep_cost (dep1) == 1)
2784 tmp_class = 3;
2785 else if (/* Data dependence. */
2786 DEP_TYPE (dep1) == REG_DEP_TRUE)
2787 tmp_class = 1;
2788 else
2789 tmp_class = 2;
2790
2791 dep2 = sd_find_dep_between (last, tmp2, true);
2792
2793 if (dep2 == NULL || dep_cost (dep2) == 1)
2794 tmp2_class = 3;
2795 else if (/* Data dependence. */
2796 DEP_TYPE (dep2) == REG_DEP_TRUE)
2797 tmp2_class = 1;
2798 else
2799 tmp2_class = 2;
2800
2801 if ((val = tmp2_class - tmp_class))
2802 return rfs_result (RFS_LAST_INSN, val, tmp, tmp2);
2803 }
2804
2805 /* Prefer instructions that occur earlier in the model schedule. */
2806 if (sched_pressure == SCHED_PRESSURE_MODEL
2807 && INSN_BB (tmp) == target_bb && INSN_BB (tmp2) == target_bb)
2808 {
2809 diff = model_index (tmp) - model_index (tmp2);
2810 gcc_assert (diff != 0);
2811 return rfs_result (RFS_PRESSURE_INDEX, diff, tmp, tmp2);
2812 }
2813
2814 /* Prefer the insn which has more later insns that depend on it.
2815 This gives the scheduler more freedom when scheduling later
2816 instructions at the expense of added register pressure. */
2817
2818 val = (dep_list_size (tmp2, SD_LIST_FORW)
2819 - dep_list_size (tmp, SD_LIST_FORW));
2820
2821 if (flag_sched_dep_count_heuristic && val != 0)
2822 return rfs_result (RFS_DEP_COUNT, val, tmp, tmp2);
2823
2824 /* If insns are equally good, sort by INSN_LUID (original insn order),
2825 so that we make the sort stable. This minimizes instruction movement,
2826 thus minimizing sched's effect on debugging and cross-jumping. */
2827 return rfs_result (RFS_TIE, INSN_LUID (tmp) - INSN_LUID (tmp2), tmp, tmp2);
2828 }
2829
2830 /* Resort the array A in which only element at index N may be out of order. */
2831
2832 HAIFA_INLINE static void
2833 swap_sort (rtx_insn **a, int n)
2834 {
2835 rtx_insn *insn = a[n - 1];
2836 int i = n - 2;
2837
2838 while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
2839 {
2840 a[i + 1] = a[i];
2841 i -= 1;
2842 }
2843 a[i + 1] = insn;
2844 }
2845
2846 /* Add INSN to the insn queue so that it can be executed at least
2847 N_CYCLES after the currently executing insn. Preserve insns
2848 chain for debugging purposes. REASON will be printed in debugging
2849 output. */
2850
2851 HAIFA_INLINE static void
2852 queue_insn (rtx_insn *insn, int n_cycles, const char *reason)
2853 {
2854 int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
2855 rtx_insn_list *link = alloc_INSN_LIST (insn, insn_queue[next_q]);
2856 int new_tick;
2857
2858 gcc_assert (n_cycles <= max_insn_queue_index);
2859 gcc_assert (!DEBUG_INSN_P (insn));
2860
2861 insn_queue[next_q] = link;
2862 q_size += 1;
2863
2864 if (sched_verbose >= 2)
2865 {
2866 fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
2867 (*current_sched_info->print_insn) (insn, 0));
2868
2869 fprintf (sched_dump, "queued for %d cycles (%s).\n", n_cycles, reason);
2870 }
2871
2872 QUEUE_INDEX (insn) = next_q;
2873
2874 if (current_sched_info->flags & DO_BACKTRACKING)
2875 {
2876 new_tick = clock_var + n_cycles;
2877 if (INSN_TICK (insn) == INVALID_TICK || INSN_TICK (insn) < new_tick)
2878 INSN_TICK (insn) = new_tick;
2879
2880 if (INSN_EXACT_TICK (insn) != INVALID_TICK
2881 && INSN_EXACT_TICK (insn) < clock_var + n_cycles)
2882 {
2883 must_backtrack = true;
2884 if (sched_verbose >= 2)
2885 fprintf (sched_dump, ";;\t\tcausing a backtrack.\n");
2886 }
2887 }
2888 }
2889
2890 /* Remove INSN from queue. */
2891 static void
2892 queue_remove (rtx_insn *insn)
2893 {
2894 gcc_assert (QUEUE_INDEX (insn) >= 0);
2895 remove_free_INSN_LIST_elem (insn, &insn_queue[QUEUE_INDEX (insn)]);
2896 q_size--;
2897 QUEUE_INDEX (insn) = QUEUE_NOWHERE;
2898 }
2899
2900 /* Return a pointer to the bottom of the ready list, i.e. the insn
2901 with the lowest priority. */
2902
2903 rtx_insn **
2904 ready_lastpos (struct ready_list *ready)
2905 {
2906 gcc_assert (ready->n_ready >= 1);
2907 return ready->vec + ready->first - ready->n_ready + 1;
2908 }
2909
2910 /* Add an element INSN to the ready list so that it ends up with the
2911 lowest/highest priority depending on FIRST_P. */
2912
2913 HAIFA_INLINE static void
2914 ready_add (struct ready_list *ready, rtx_insn *insn, bool first_p)
2915 {
2916 if (!first_p)
2917 {
2918 if (ready->first == ready->n_ready)
2919 {
2920 memmove (ready->vec + ready->veclen - ready->n_ready,
2921 ready_lastpos (ready),
2922 ready->n_ready * sizeof (rtx));
2923 ready->first = ready->veclen - 1;
2924 }
2925 ready->vec[ready->first - ready->n_ready] = insn;
2926 }
2927 else
2928 {
2929 if (ready->first == ready->veclen - 1)
2930 {
2931 if (ready->n_ready)
2932 /* ready_lastpos() fails when called with (ready->n_ready == 0). */
2933 memmove (ready->vec + ready->veclen - ready->n_ready - 1,
2934 ready_lastpos (ready),
2935 ready->n_ready * sizeof (rtx));
2936 ready->first = ready->veclen - 2;
2937 }
2938 ready->vec[++(ready->first)] = insn;
2939 }
2940
2941 ready->n_ready++;
2942 if (DEBUG_INSN_P (insn))
2943 ready->n_debug++;
2944
2945 gcc_assert (QUEUE_INDEX (insn) != QUEUE_READY);
2946 QUEUE_INDEX (insn) = QUEUE_READY;
2947
2948 if (INSN_EXACT_TICK (insn) != INVALID_TICK
2949 && INSN_EXACT_TICK (insn) < clock_var)
2950 {
2951 must_backtrack = true;
2952 }
2953 }
2954
2955 /* Remove the element with the highest priority from the ready list and
2956 return it. */
2957
2958 HAIFA_INLINE static rtx_insn *
2959 ready_remove_first (struct ready_list *ready)
2960 {
2961 rtx_insn *t;
2962
2963 gcc_assert (ready->n_ready);
2964 t = ready->vec[ready->first--];
2965 ready->n_ready--;
2966 if (DEBUG_INSN_P (t))
2967 ready->n_debug--;
2968 /* If the queue becomes empty, reset it. */
2969 if (ready->n_ready == 0)
2970 ready->first = ready->veclen - 1;
2971
2972 gcc_assert (QUEUE_INDEX (t) == QUEUE_READY);
2973 QUEUE_INDEX (t) = QUEUE_NOWHERE;
2974
2975 return t;
2976 }
2977
2978 /* The following code implements multi-pass scheduling for the first
2979 cycle. In other words, we will try to choose ready insn which
2980 permits to start maximum number of insns on the same cycle. */
2981
2982 /* Return a pointer to the element INDEX from the ready. INDEX for
2983 insn with the highest priority is 0, and the lowest priority has
2984 N_READY - 1. */
2985
2986 rtx_insn *
2987 ready_element (struct ready_list *ready, int index)
2988 {
2989 gcc_assert (ready->n_ready && index < ready->n_ready);
2990
2991 return ready->vec[ready->first - index];
2992 }
2993
2994 /* Remove the element INDEX from the ready list and return it. INDEX
2995 for insn with the highest priority is 0, and the lowest priority
2996 has N_READY - 1. */
2997
2998 HAIFA_INLINE static rtx_insn *
2999 ready_remove (struct ready_list *ready, int index)
3000 {
3001 rtx_insn *t;
3002 int i;
3003
3004 if (index == 0)
3005 return ready_remove_first (ready);
3006 gcc_assert (ready->n_ready && index < ready->n_ready);
3007 t = ready->vec[ready->first - index];
3008 ready->n_ready--;
3009 if (DEBUG_INSN_P (t))
3010 ready->n_debug--;
3011 for (i = index; i < ready->n_ready; i++)
3012 ready->vec[ready->first - i] = ready->vec[ready->first - i - 1];
3013 QUEUE_INDEX (t) = QUEUE_NOWHERE;
3014 return t;
3015 }
3016
3017 /* Remove INSN from the ready list. */
3018 static void
3019 ready_remove_insn (rtx_insn *insn)
3020 {
3021 int i;
3022
3023 for (i = 0; i < readyp->n_ready; i++)
3024 if (ready_element (readyp, i) == insn)
3025 {
3026 ready_remove (readyp, i);
3027 return;
3028 }
3029 gcc_unreachable ();
3030 }
3031
3032 /* Calculate difference of two statistics set WAS and NOW.
3033 Result returned in WAS. */
3034 static void
3035 rank_for_schedule_stats_diff (rank_for_schedule_stats_t *was,
3036 const rank_for_schedule_stats_t *now)
3037 {
3038 for (int i = 0; i < RFS_N; ++i)
3039 was->stats[i] = now->stats[i] - was->stats[i];
3040 }
3041
3042 /* Print rank_for_schedule statistics. */
3043 static void
3044 print_rank_for_schedule_stats (const char *prefix,
3045 const rank_for_schedule_stats_t *stats,
3046 struct ready_list *ready)
3047 {
3048 for (int i = 0; i < RFS_N; ++i)
3049 if (stats->stats[i])
3050 {
3051 fprintf (sched_dump, "%s%20s: %u", prefix, rfs_str[i], stats->stats[i]);
3052
3053 if (ready != NULL)
3054 /* Print out insns that won due to RFS_<I>. */
3055 {
3056 rtx_insn **p = ready_lastpos (ready);
3057
3058 fprintf (sched_dump, ":");
3059 /* Start with 1 since least-priority insn didn't have any wins. */
3060 for (int j = 1; j < ready->n_ready; ++j)
3061 if (INSN_LAST_RFS_WIN (p[j]) == i)
3062 fprintf (sched_dump, " %s",
3063 (*current_sched_info->print_insn) (p[j], 0));
3064 }
3065 fprintf (sched_dump, "\n");
3066 }
3067 }
3068
3069 /* Separate DEBUG_INSNS from normal insns. DEBUG_INSNs go to the end
3070 of array. */
3071 static void
3072 ready_sort_debug (struct ready_list *ready)
3073 {
3074 int i;
3075 rtx_insn **first = ready_lastpos (ready);
3076
3077 for (i = 0; i < ready->n_ready; ++i)
3078 if (!DEBUG_INSN_P (first[i]))
3079 INSN_RFS_DEBUG_ORIG_ORDER (first[i]) = i;
3080
3081 qsort (first, ready->n_ready, sizeof (rtx), rank_for_schedule_debug);
3082 }
3083
3084 /* Sort non-debug insns in the ready list READY by ascending priority.
3085 Assumes that all debug insns are separated from the real insns. */
3086 static void
3087 ready_sort_real (struct ready_list *ready)
3088 {
3089 int i;
3090 rtx_insn **first = ready_lastpos (ready);
3091 int n_ready_real = ready->n_ready - ready->n_debug;
3092
3093 if (sched_pressure == SCHED_PRESSURE_WEIGHTED)
3094 for (i = 0; i < n_ready_real; ++i)
3095 setup_insn_reg_pressure_info (first[i]);
3096 else if (sched_pressure == SCHED_PRESSURE_MODEL
3097 && model_curr_point < model_num_insns)
3098 model_set_excess_costs (first, n_ready_real);
3099
3100 rank_for_schedule_stats_t stats1;
3101 if (sched_verbose >= 4)
3102 stats1 = rank_for_schedule_stats;
3103
3104 if (n_ready_real == 2)
3105 swap_sort (first, n_ready_real);
3106 else if (n_ready_real > 2)
3107 qsort (first, n_ready_real, sizeof (rtx), rank_for_schedule);
3108
3109 if (sched_verbose >= 4)
3110 {
3111 rank_for_schedule_stats_diff (&stats1, &rank_for_schedule_stats);
3112 print_rank_for_schedule_stats (";;\t\t", &stats1, ready);
3113 }
3114 }
3115
3116 /* Sort the ready list READY by ascending priority. */
3117 static void
3118 ready_sort (struct ready_list *ready)
3119 {
3120 if (ready->n_debug > 0)
3121 ready_sort_debug (ready);
3122 else
3123 ready_sort_real (ready);
3124 }
3125
3126 /* PREV is an insn that is ready to execute. Adjust its priority if that
3127 will help shorten or lengthen register lifetimes as appropriate. Also
3128 provide a hook for the target to tweak itself. */
3129
3130 HAIFA_INLINE static void
3131 adjust_priority (rtx_insn *prev)
3132 {
3133 /* ??? There used to be code here to try and estimate how an insn
3134 affected register lifetimes, but it did it by looking at REG_DEAD
3135 notes, which we removed in schedule_region. Nor did it try to
3136 take into account register pressure or anything useful like that.
3137
3138 Revisit when we have a machine model to work with and not before. */
3139
3140 if (targetm.sched.adjust_priority)
3141 INSN_PRIORITY (prev) =
3142 targetm.sched.adjust_priority (prev, INSN_PRIORITY (prev));
3143 }
3144
3145 /* Advance DFA state STATE on one cycle. */
3146 void
3147 advance_state (state_t state)
3148 {
3149 if (targetm.sched.dfa_pre_advance_cycle)
3150 targetm.sched.dfa_pre_advance_cycle ();
3151
3152 if (targetm.sched.dfa_pre_cycle_insn)
3153 state_transition (state,
3154 targetm.sched.dfa_pre_cycle_insn ());
3155
3156 state_transition (state, NULL);
3157
3158 if (targetm.sched.dfa_post_cycle_insn)
3159 state_transition (state,
3160 targetm.sched.dfa_post_cycle_insn ());
3161
3162 if (targetm.sched.dfa_post_advance_cycle)
3163 targetm.sched.dfa_post_advance_cycle ();
3164 }
3165
3166 /* Advance time on one cycle. */
3167 HAIFA_INLINE static void
3168 advance_one_cycle (void)
3169 {
3170 advance_state (curr_state);
3171 if (sched_verbose >= 4)
3172 fprintf (sched_dump, ";;\tAdvance the current state.\n");
3173 }
3174
3175 /* Update register pressure after scheduling INSN. */
3176 static void
3177 update_register_pressure (rtx_insn *insn)
3178 {
3179 struct reg_use_data *use;
3180 struct reg_set_data *set;
3181
3182 gcc_checking_assert (!DEBUG_INSN_P (insn));
3183
3184 for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
3185 if (dying_use_p (use))
3186 mark_regno_birth_or_death (curr_reg_live, curr_reg_pressure,
3187 use->regno, false);
3188 for (set = INSN_REG_SET_LIST (insn); set != NULL; set = set->next_insn_set)
3189 mark_regno_birth_or_death (curr_reg_live, curr_reg_pressure,
3190 set->regno, true);
3191 }
3192
3193 /* Set up or update (if UPDATE_P) max register pressure (see its
3194 meaning in sched-int.h::_haifa_insn_data) for all current BB insns
3195 after insn AFTER. */
3196 static void
3197 setup_insn_max_reg_pressure (rtx_insn *after, bool update_p)
3198 {
3199 int i, p;
3200 bool eq_p;
3201 rtx_insn *insn;
3202 static int max_reg_pressure[N_REG_CLASSES];
3203
3204 save_reg_pressure ();
3205 for (i = 0; i < ira_pressure_classes_num; i++)
3206 max_reg_pressure[ira_pressure_classes[i]]
3207 = curr_reg_pressure[ira_pressure_classes[i]];
3208 for (insn = NEXT_INSN (after);
3209 insn != NULL_RTX && ! BARRIER_P (insn)
3210 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (after);
3211 insn = NEXT_INSN (insn))
3212 if (NONDEBUG_INSN_P (insn))
3213 {
3214 eq_p = true;
3215 for (i = 0; i < ira_pressure_classes_num; i++)
3216 {
3217 p = max_reg_pressure[ira_pressure_classes[i]];
3218 if (INSN_MAX_REG_PRESSURE (insn)[i] != p)
3219 {
3220 eq_p = false;
3221 INSN_MAX_REG_PRESSURE (insn)[i]
3222 = max_reg_pressure[ira_pressure_classes[i]];
3223 }
3224 }
3225 if (update_p && eq_p)
3226 break;
3227 update_register_pressure (insn);
3228 for (i = 0; i < ira_pressure_classes_num; i++)
3229 if (max_reg_pressure[ira_pressure_classes[i]]
3230 < curr_reg_pressure[ira_pressure_classes[i]])
3231 max_reg_pressure[ira_pressure_classes[i]]
3232 = curr_reg_pressure[ira_pressure_classes[i]];
3233 }
3234 restore_reg_pressure ();
3235 }
3236
3237 /* Update the current register pressure after scheduling INSN. Update
3238 also max register pressure for unscheduled insns of the current
3239 BB. */
3240 static void
3241 update_reg_and_insn_max_reg_pressure (rtx_insn *insn)
3242 {
3243 int i;
3244 int before[N_REG_CLASSES];
3245
3246 for (i = 0; i < ira_pressure_classes_num; i++)
3247 before[i] = curr_reg_pressure[ira_pressure_classes[i]];
3248 update_register_pressure (insn);
3249 for (i = 0; i < ira_pressure_classes_num; i++)
3250 if (curr_reg_pressure[ira_pressure_classes[i]] != before[i])
3251 break;
3252 if (i < ira_pressure_classes_num)
3253 setup_insn_max_reg_pressure (insn, true);
3254 }
3255
3256 /* Set up register pressure at the beginning of basic block BB whose
3257 insns starting after insn AFTER. Set up also max register pressure
3258 for all insns of the basic block. */
3259 void
3260 sched_setup_bb_reg_pressure_info (basic_block bb, rtx_insn *after)
3261 {
3262 gcc_assert (sched_pressure == SCHED_PRESSURE_WEIGHTED);
3263 initiate_bb_reg_pressure_info (bb);
3264 setup_insn_max_reg_pressure (after, false);
3265 }
3266 \f
3267 /* If doing predication while scheduling, verify whether INSN, which
3268 has just been scheduled, clobbers the conditions of any
3269 instructions that must be predicated in order to break their
3270 dependencies. If so, remove them from the queues so that they will
3271 only be scheduled once their control dependency is resolved. */
3272
3273 static void
3274 check_clobbered_conditions (rtx_insn *insn)
3275 {
3276 HARD_REG_SET t;
3277 int i;
3278
3279 if ((current_sched_info->flags & DO_PREDICATION) == 0)
3280 return;
3281
3282 find_all_hard_reg_sets (insn, &t, true);
3283
3284 restart:
3285 for (i = 0; i < ready.n_ready; i++)
3286 {
3287 rtx_insn *x = ready_element (&ready, i);
3288 if (TODO_SPEC (x) == DEP_CONTROL && cond_clobbered_p (x, t))
3289 {
3290 ready_remove_insn (x);
3291 goto restart;
3292 }
3293 }
3294 for (i = 0; i <= max_insn_queue_index; i++)
3295 {
3296 rtx_insn_list *link;
3297 int q = NEXT_Q_AFTER (q_ptr, i);
3298
3299 restart_queue:
3300 for (link = insn_queue[q]; link; link = link->next ())
3301 {
3302 rtx_insn *x = link->insn ();
3303 if (TODO_SPEC (x) == DEP_CONTROL && cond_clobbered_p (x, t))
3304 {
3305 queue_remove (x);
3306 goto restart_queue;
3307 }
3308 }
3309 }
3310 }
3311 \f
3312 /* Return (in order):
3313
3314 - positive if INSN adversely affects the pressure on one
3315 register class
3316
3317 - negative if INSN reduces the pressure on one register class
3318
3319 - 0 if INSN doesn't affect the pressure on any register class. */
3320
3321 static int
3322 model_classify_pressure (struct model_insn_info *insn)
3323 {
3324 struct reg_pressure_data *reg_pressure;
3325 int death[N_REG_CLASSES];
3326 int pci, cl, sum;
3327
3328 calculate_reg_deaths (insn->insn, death);
3329 reg_pressure = INSN_REG_PRESSURE (insn->insn);
3330 sum = 0;
3331 for (pci = 0; pci < ira_pressure_classes_num; pci++)
3332 {
3333 cl = ira_pressure_classes[pci];
3334 if (death[cl] < reg_pressure[pci].set_increase)
3335 return 1;
3336 sum += reg_pressure[pci].set_increase - death[cl];
3337 }
3338 return sum;
3339 }
3340
3341 /* Return true if INSN1 should come before INSN2 in the model schedule. */
3342
3343 static int
3344 model_order_p (struct model_insn_info *insn1, struct model_insn_info *insn2)
3345 {
3346 unsigned int height1, height2;
3347 unsigned int priority1, priority2;
3348
3349 /* Prefer instructions with a higher model priority. */
3350 if (insn1->model_priority != insn2->model_priority)
3351 return insn1->model_priority > insn2->model_priority;
3352
3353 /* Combine the length of the longest path of satisfied true dependencies
3354 that leads to each instruction (depth) with the length of the longest
3355 path of any dependencies that leads from the instruction (alap).
3356 Prefer instructions with the greatest combined length. If the combined
3357 lengths are equal, prefer instructions with the greatest depth.
3358
3359 The idea is that, if we have a set S of "equal" instructions that each
3360 have ALAP value X, and we pick one such instruction I, any true-dependent
3361 successors of I that have ALAP value X - 1 should be preferred over S.
3362 This encourages the schedule to be "narrow" rather than "wide".
3363 However, if I is a low-priority instruction that we decided to
3364 schedule because of its model_classify_pressure, and if there
3365 is a set of higher-priority instructions T, the aforementioned
3366 successors of I should not have the edge over T. */
3367 height1 = insn1->depth + insn1->alap;
3368 height2 = insn2->depth + insn2->alap;
3369 if (height1 != height2)
3370 return height1 > height2;
3371 if (insn1->depth != insn2->depth)
3372 return insn1->depth > insn2->depth;
3373
3374 /* We have no real preference between INSN1 an INSN2 as far as attempts
3375 to reduce pressure go. Prefer instructions with higher priorities. */
3376 priority1 = INSN_PRIORITY (insn1->insn);
3377 priority2 = INSN_PRIORITY (insn2->insn);
3378 if (priority1 != priority2)
3379 return priority1 > priority2;
3380
3381 /* Use the original rtl sequence as a tie-breaker. */
3382 return insn1 < insn2;
3383 }
3384
3385 /* Add INSN to the model worklist immediately after PREV. Add it to the
3386 beginning of the list if PREV is null. */
3387
3388 static void
3389 model_add_to_worklist_at (struct model_insn_info *insn,
3390 struct model_insn_info *prev)
3391 {
3392 gcc_assert (QUEUE_INDEX (insn->insn) == QUEUE_NOWHERE);
3393 QUEUE_INDEX (insn->insn) = QUEUE_READY;
3394
3395 insn->prev = prev;
3396 if (prev)
3397 {
3398 insn->next = prev->next;
3399 prev->next = insn;
3400 }
3401 else
3402 {
3403 insn->next = model_worklist;
3404 model_worklist = insn;
3405 }
3406 if (insn->next)
3407 insn->next->prev = insn;
3408 }
3409
3410 /* Remove INSN from the model worklist. */
3411
3412 static void
3413 model_remove_from_worklist (struct model_insn_info *insn)
3414 {
3415 gcc_assert (QUEUE_INDEX (insn->insn) == QUEUE_READY);
3416 QUEUE_INDEX (insn->insn) = QUEUE_NOWHERE;
3417
3418 if (insn->prev)
3419 insn->prev->next = insn->next;
3420 else
3421 model_worklist = insn->next;
3422 if (insn->next)
3423 insn->next->prev = insn->prev;
3424 }
3425
3426 /* Add INSN to the model worklist. Start looking for a suitable position
3427 between neighbors PREV and NEXT, testing at most MAX_SCHED_READY_INSNS
3428 insns either side. A null PREV indicates the beginning of the list and
3429 a null NEXT indicates the end. */
3430
3431 static void
3432 model_add_to_worklist (struct model_insn_info *insn,
3433 struct model_insn_info *prev,
3434 struct model_insn_info *next)
3435 {
3436 int count;
3437
3438 count = MAX_SCHED_READY_INSNS;
3439 if (count > 0 && prev && model_order_p (insn, prev))
3440 do
3441 {
3442 count--;
3443 prev = prev->prev;
3444 }
3445 while (count > 0 && prev && model_order_p (insn, prev));
3446 else
3447 while (count > 0 && next && model_order_p (next, insn))
3448 {
3449 count--;
3450 prev = next;
3451 next = next->next;
3452 }
3453 model_add_to_worklist_at (insn, prev);
3454 }
3455
3456 /* INSN may now have a higher priority (in the model_order_p sense)
3457 than before. Move it up the worklist if necessary. */
3458
3459 static void
3460 model_promote_insn (struct model_insn_info *insn)
3461 {
3462 struct model_insn_info *prev;
3463 int count;
3464
3465 prev = insn->prev;
3466 count = MAX_SCHED_READY_INSNS;
3467 while (count > 0 && prev && model_order_p (insn, prev))
3468 {
3469 count--;
3470 prev = prev->prev;
3471 }
3472 if (prev != insn->prev)
3473 {
3474 model_remove_from_worklist (insn);
3475 model_add_to_worklist_at (insn, prev);
3476 }
3477 }
3478
3479 /* Add INSN to the end of the model schedule. */
3480
3481 static void
3482 model_add_to_schedule (rtx_insn *insn)
3483 {
3484 unsigned int point;
3485
3486 gcc_assert (QUEUE_INDEX (insn) == QUEUE_NOWHERE);
3487 QUEUE_INDEX (insn) = QUEUE_SCHEDULED;
3488
3489 point = model_schedule.length ();
3490 model_schedule.quick_push (insn);
3491 INSN_MODEL_INDEX (insn) = point + 1;
3492 }
3493
3494 /* Analyze the instructions that are to be scheduled, setting up
3495 MODEL_INSN_INFO (...) and model_num_insns accordingly. Add ready
3496 instructions to model_worklist. */
3497
3498 static void
3499 model_analyze_insns (void)
3500 {
3501 rtx_insn *start, *end, *iter;
3502 sd_iterator_def sd_it;
3503 dep_t dep;
3504 struct model_insn_info *insn, *con;
3505
3506 model_num_insns = 0;
3507 start = PREV_INSN (current_sched_info->next_tail);
3508 end = current_sched_info->prev_head;
3509 for (iter = start; iter != end; iter = PREV_INSN (iter))
3510 if (NONDEBUG_INSN_P (iter))
3511 {
3512 insn = MODEL_INSN_INFO (iter);
3513 insn->insn = iter;
3514 FOR_EACH_DEP (iter, SD_LIST_FORW, sd_it, dep)
3515 {
3516 con = MODEL_INSN_INFO (DEP_CON (dep));
3517 if (con->insn && insn->alap < con->alap + 1)
3518 insn->alap = con->alap + 1;
3519 }
3520
3521 insn->old_queue = QUEUE_INDEX (iter);
3522 QUEUE_INDEX (iter) = QUEUE_NOWHERE;
3523
3524 insn->unscheduled_preds = dep_list_size (iter, SD_LIST_HARD_BACK);
3525 if (insn->unscheduled_preds == 0)
3526 model_add_to_worklist (insn, NULL, model_worklist);
3527
3528 model_num_insns++;
3529 }
3530 }
3531
3532 /* The global state describes the register pressure at the start of the
3533 model schedule. Initialize GROUP accordingly. */
3534
3535 static void
3536 model_init_pressure_group (struct model_pressure_group *group)
3537 {
3538 int pci, cl;
3539
3540 for (pci = 0; pci < ira_pressure_classes_num; pci++)
3541 {
3542 cl = ira_pressure_classes[pci];
3543 group->limits[pci].pressure = curr_reg_pressure[cl];
3544 group->limits[pci].point = 0;
3545 }
3546 /* Use index model_num_insns to record the state after the last
3547 instruction in the model schedule. */
3548 group->model = XNEWVEC (struct model_pressure_data,
3549 (model_num_insns + 1) * ira_pressure_classes_num);
3550 }
3551
3552 /* Record that MODEL_REF_PRESSURE (GROUP, POINT, PCI) is PRESSURE.
3553 Update the maximum pressure for the whole schedule. */
3554
3555 static void
3556 model_record_pressure (struct model_pressure_group *group,
3557 int point, int pci, int pressure)
3558 {
3559 MODEL_REF_PRESSURE (group, point, pci) = pressure;
3560 if (group->limits[pci].pressure < pressure)
3561 {
3562 group->limits[pci].pressure = pressure;
3563 group->limits[pci].point = point;
3564 }
3565 }
3566
3567 /* INSN has just been added to the end of the model schedule. Record its
3568 register-pressure information. */
3569
3570 static void
3571 model_record_pressures (struct model_insn_info *insn)
3572 {
3573 struct reg_pressure_data *reg_pressure;
3574 int point, pci, cl, delta;
3575 int death[N_REG_CLASSES];
3576
3577 point = model_index (insn->insn);
3578 if (sched_verbose >= 2)
3579 {
3580 if (point == 0)
3581 {
3582 fprintf (sched_dump, "\n;;\tModel schedule:\n;;\n");
3583 fprintf (sched_dump, ";;\t| idx insn | mpri hght dpth prio |\n");
3584 }
3585 fprintf (sched_dump, ";;\t| %3d %4d | %4d %4d %4d %4d | %-30s ",
3586 point, INSN_UID (insn->insn), insn->model_priority,
3587 insn->depth + insn->alap, insn->depth,
3588 INSN_PRIORITY (insn->insn),
3589 str_pattern_slim (PATTERN (insn->insn)));
3590 }
3591 calculate_reg_deaths (insn->insn, death);
3592 reg_pressure = INSN_REG_PRESSURE (insn->insn);
3593 for (pci = 0; pci < ira_pressure_classes_num; pci++)
3594 {
3595 cl = ira_pressure_classes[pci];
3596 delta = reg_pressure[pci].set_increase - death[cl];
3597 if (sched_verbose >= 2)
3598 fprintf (sched_dump, " %s:[%d,%+d]", reg_class_names[cl],
3599 curr_reg_pressure[cl], delta);
3600 model_record_pressure (&model_before_pressure, point, pci,
3601 curr_reg_pressure[cl]);
3602 }
3603 if (sched_verbose >= 2)
3604 fprintf (sched_dump, "\n");
3605 }
3606
3607 /* All instructions have been added to the model schedule. Record the
3608 final register pressure in GROUP and set up all MODEL_MAX_PRESSUREs. */
3609
3610 static void
3611 model_record_final_pressures (struct model_pressure_group *group)
3612 {
3613 int point, pci, max_pressure, ref_pressure, cl;
3614
3615 for (pci = 0; pci < ira_pressure_classes_num; pci++)
3616 {
3617 /* Record the final pressure for this class. */
3618 cl = ira_pressure_classes[pci];
3619 point = model_num_insns;
3620 ref_pressure = curr_reg_pressure[cl];
3621 model_record_pressure (group, point, pci, ref_pressure);
3622
3623 /* Record the original maximum pressure. */
3624 group->limits[pci].orig_pressure = group->limits[pci].pressure;
3625
3626 /* Update the MODEL_MAX_PRESSURE for every point of the schedule. */
3627 max_pressure = ref_pressure;
3628 MODEL_MAX_PRESSURE (group, point, pci) = max_pressure;
3629 while (point > 0)
3630 {
3631 point--;
3632 ref_pressure = MODEL_REF_PRESSURE (group, point, pci);
3633 max_pressure = MAX (max_pressure, ref_pressure);
3634 MODEL_MAX_PRESSURE (group, point, pci) = max_pressure;
3635 }
3636 }
3637 }
3638
3639 /* Update all successors of INSN, given that INSN has just been scheduled. */
3640
3641 static void
3642 model_add_successors_to_worklist (struct model_insn_info *insn)
3643 {
3644 sd_iterator_def sd_it;
3645 struct model_insn_info *con;
3646 dep_t dep;
3647
3648 FOR_EACH_DEP (insn->insn, SD_LIST_FORW, sd_it, dep)
3649 {
3650 con = MODEL_INSN_INFO (DEP_CON (dep));
3651 /* Ignore debug instructions, and instructions from other blocks. */
3652 if (con->insn)
3653 {
3654 con->unscheduled_preds--;
3655
3656 /* Update the depth field of each true-dependent successor.
3657 Increasing the depth gives them a higher priority than
3658 before. */
3659 if (DEP_TYPE (dep) == REG_DEP_TRUE && con->depth < insn->depth + 1)
3660 {
3661 con->depth = insn->depth + 1;
3662 if (QUEUE_INDEX (con->insn) == QUEUE_READY)
3663 model_promote_insn (con);
3664 }
3665
3666 /* If this is a true dependency, or if there are no remaining
3667 dependencies for CON (meaning that CON only had non-true
3668 dependencies), make sure that CON is on the worklist.
3669 We don't bother otherwise because it would tend to fill the
3670 worklist with a lot of low-priority instructions that are not
3671 yet ready to issue. */
3672 if ((con->depth > 0 || con->unscheduled_preds == 0)
3673 && QUEUE_INDEX (con->insn) == QUEUE_NOWHERE)
3674 model_add_to_worklist (con, insn, insn->next);
3675 }
3676 }
3677 }
3678
3679 /* Give INSN a higher priority than any current instruction, then give
3680 unscheduled predecessors of INSN a higher priority still. If any of
3681 those predecessors are not on the model worklist, do the same for its
3682 predecessors, and so on. */
3683
3684 static void
3685 model_promote_predecessors (struct model_insn_info *insn)
3686 {
3687 struct model_insn_info *pro, *first;
3688 sd_iterator_def sd_it;
3689 dep_t dep;
3690
3691 if (sched_verbose >= 7)
3692 fprintf (sched_dump, ";;\t+--- priority of %d = %d, priority of",
3693 INSN_UID (insn->insn), model_next_priority);
3694 insn->model_priority = model_next_priority++;
3695 model_remove_from_worklist (insn);
3696 model_add_to_worklist_at (insn, NULL);
3697
3698 first = NULL;
3699 for (;;)
3700 {
3701 FOR_EACH_DEP (insn->insn, SD_LIST_HARD_BACK, sd_it, dep)
3702 {
3703 pro = MODEL_INSN_INFO (DEP_PRO (dep));
3704 /* The first test is to ignore debug instructions, and instructions
3705 from other blocks. */
3706 if (pro->insn
3707 && pro->model_priority != model_next_priority
3708 && QUEUE_INDEX (pro->insn) != QUEUE_SCHEDULED)
3709 {
3710 pro->model_priority = model_next_priority;
3711 if (sched_verbose >= 7)
3712 fprintf (sched_dump, " %d", INSN_UID (pro->insn));
3713 if (QUEUE_INDEX (pro->insn) == QUEUE_READY)
3714 {
3715 /* PRO is already in the worklist, but it now has
3716 a higher priority than before. Move it at the
3717 appropriate place. */
3718 model_remove_from_worklist (pro);
3719 model_add_to_worklist (pro, NULL, model_worklist);
3720 }
3721 else
3722 {
3723 /* PRO isn't in the worklist. Recursively process
3724 its predecessors until we find one that is. */
3725 pro->next = first;
3726 first = pro;
3727 }
3728 }
3729 }
3730 if (!first)
3731 break;
3732 insn = first;
3733 first = insn->next;
3734 }
3735 if (sched_verbose >= 7)
3736 fprintf (sched_dump, " = %d\n", model_next_priority);
3737 model_next_priority++;
3738 }
3739
3740 /* Pick one instruction from model_worklist and process it. */
3741
3742 static void
3743 model_choose_insn (void)
3744 {
3745 struct model_insn_info *insn, *fallback;
3746 int count;
3747
3748 if (sched_verbose >= 7)
3749 {
3750 fprintf (sched_dump, ";;\t+--- worklist:\n");
3751 insn = model_worklist;
3752 count = MAX_SCHED_READY_INSNS;
3753 while (count > 0 && insn)
3754 {
3755 fprintf (sched_dump, ";;\t+--- %d [%d, %d, %d, %d]\n",
3756 INSN_UID (insn->insn), insn->model_priority,
3757 insn->depth + insn->alap, insn->depth,
3758 INSN_PRIORITY (insn->insn));
3759 count--;
3760 insn = insn->next;
3761 }
3762 }
3763
3764 /* Look for a ready instruction whose model_classify_priority is zero
3765 or negative, picking the highest-priority one. Adding such an
3766 instruction to the schedule now should do no harm, and may actually
3767 do some good.
3768
3769 Failing that, see whether there is an instruction with the highest
3770 extant model_priority that is not yet ready, but which would reduce
3771 pressure if it became ready. This is designed to catch cases like:
3772
3773 (set (mem (reg R1)) (reg R2))
3774
3775 where the instruction is the last remaining use of R1 and where the
3776 value of R2 is not yet available (or vice versa). The death of R1
3777 means that this instruction already reduces pressure. It is of
3778 course possible that the computation of R2 involves other registers
3779 that are hard to kill, but such cases are rare enough for this
3780 heuristic to be a win in general.
3781
3782 Failing that, just pick the highest-priority instruction in the
3783 worklist. */
3784 count = MAX_SCHED_READY_INSNS;
3785 insn = model_worklist;
3786 fallback = 0;
3787 for (;;)
3788 {
3789 if (count == 0 || !insn)
3790 {
3791 insn = fallback ? fallback : model_worklist;
3792 break;
3793 }
3794 if (insn->unscheduled_preds)
3795 {
3796 if (model_worklist->model_priority == insn->model_priority
3797 && !fallback
3798 && model_classify_pressure (insn) < 0)
3799 fallback = insn;
3800 }
3801 else
3802 {
3803 if (model_classify_pressure (insn) <= 0)
3804 break;
3805 }
3806 count--;
3807 insn = insn->next;
3808 }
3809
3810 if (sched_verbose >= 7 && insn != model_worklist)
3811 {
3812 if (insn->unscheduled_preds)
3813 fprintf (sched_dump, ";;\t+--- promoting insn %d, with dependencies\n",
3814 INSN_UID (insn->insn));
3815 else
3816 fprintf (sched_dump, ";;\t+--- promoting insn %d, which is ready\n",
3817 INSN_UID (insn->insn));
3818 }
3819 if (insn->unscheduled_preds)
3820 /* INSN isn't yet ready to issue. Give all its predecessors the
3821 highest priority. */
3822 model_promote_predecessors (insn);
3823 else
3824 {
3825 /* INSN is ready. Add it to the end of model_schedule and
3826 process its successors. */
3827 model_add_successors_to_worklist (insn);
3828 model_remove_from_worklist (insn);
3829 model_add_to_schedule (insn->insn);
3830 model_record_pressures (insn);
3831 update_register_pressure (insn->insn);
3832 }
3833 }
3834
3835 /* Restore all QUEUE_INDEXs to the values that they had before
3836 model_start_schedule was called. */
3837
3838 static void
3839 model_reset_queue_indices (void)
3840 {
3841 unsigned int i;
3842 rtx_insn *insn;
3843
3844 FOR_EACH_VEC_ELT (model_schedule, i, insn)
3845 QUEUE_INDEX (insn) = MODEL_INSN_INFO (insn)->old_queue;
3846 }
3847
3848 /* We have calculated the model schedule and spill costs. Print a summary
3849 to sched_dump. */
3850
3851 static void
3852 model_dump_pressure_summary (void)
3853 {
3854 int pci, cl;
3855
3856 fprintf (sched_dump, ";; Pressure summary:");
3857 for (pci = 0; pci < ira_pressure_classes_num; pci++)
3858 {
3859 cl = ira_pressure_classes[pci];
3860 fprintf (sched_dump, " %s:%d", reg_class_names[cl],
3861 model_before_pressure.limits[pci].pressure);
3862 }
3863 fprintf (sched_dump, "\n\n");
3864 }
3865
3866 /* Initialize the SCHED_PRESSURE_MODEL information for the current
3867 scheduling region. */
3868
3869 static void
3870 model_start_schedule (basic_block bb)
3871 {
3872 model_next_priority = 1;
3873 model_schedule.create (sched_max_luid);
3874 model_insns = XCNEWVEC (struct model_insn_info, sched_max_luid);
3875
3876 gcc_assert (bb == BLOCK_FOR_INSN (NEXT_INSN (current_sched_info->prev_head)));
3877 initiate_reg_pressure_info (df_get_live_in (bb));
3878
3879 model_analyze_insns ();
3880 model_init_pressure_group (&model_before_pressure);
3881 while (model_worklist)
3882 model_choose_insn ();
3883 gcc_assert (model_num_insns == (int) model_schedule.length ());
3884 if (sched_verbose >= 2)
3885 fprintf (sched_dump, "\n");
3886
3887 model_record_final_pressures (&model_before_pressure);
3888 model_reset_queue_indices ();
3889
3890 XDELETEVEC (model_insns);
3891
3892 model_curr_point = 0;
3893 initiate_reg_pressure_info (df_get_live_in (bb));
3894 if (sched_verbose >= 1)
3895 model_dump_pressure_summary ();
3896 }
3897
3898 /* Free the information associated with GROUP. */
3899
3900 static void
3901 model_finalize_pressure_group (struct model_pressure_group *group)
3902 {
3903 XDELETEVEC (group->model);
3904 }
3905
3906 /* Free the information created by model_start_schedule. */
3907
3908 static void
3909 model_end_schedule (void)
3910 {
3911 model_finalize_pressure_group (&model_before_pressure);
3912 model_schedule.release ();
3913 }
3914
3915 /* Prepare reg pressure scheduling for basic block BB. */
3916 static void
3917 sched_pressure_start_bb (basic_block bb)
3918 {
3919 /* Set the number of available registers for each class taking into account
3920 relative probability of current basic block versus function prologue and
3921 epilogue.
3922 * If the basic block executes much more often than the prologue/epilogue
3923 (e.g., inside a hot loop), then cost of spill in the prologue is close to
3924 nil, so the effective number of available registers is
3925 (ira_class_hard_regs_num[cl] - 0).
3926 * If the basic block executes as often as the prologue/epilogue,
3927 then spill in the block is as costly as in the prologue, so the effective
3928 number of available registers is
3929 (ira_class_hard_regs_num[cl] - call_used_regs_num[cl]).
3930 Note that all-else-equal, we prefer to spill in the prologue, since that
3931 allows "extra" registers for other basic blocks of the function.
3932 * If the basic block is on the cold path of the function and executes
3933 rarely, then we should always prefer to spill in the block, rather than
3934 in the prologue/epilogue. The effective number of available register is
3935 (ira_class_hard_regs_num[cl] - call_used_regs_num[cl]). */
3936 {
3937 int i;
3938 int entry_freq = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
3939 int bb_freq = bb->frequency;
3940
3941 if (bb_freq == 0)
3942 {
3943 if (entry_freq == 0)
3944 entry_freq = bb_freq = 1;
3945 }
3946 if (bb_freq < entry_freq)
3947 bb_freq = entry_freq;
3948
3949 for (i = 0; i < ira_pressure_classes_num; ++i)
3950 {
3951 enum reg_class cl = ira_pressure_classes[i];
3952 sched_class_regs_num[cl] = ira_class_hard_regs_num[cl];
3953 sched_class_regs_num[cl]
3954 -= (call_used_regs_num[cl] * entry_freq) / bb_freq;
3955 }
3956 }
3957
3958 if (sched_pressure == SCHED_PRESSURE_MODEL)
3959 model_start_schedule (bb);
3960 }
3961 \f
3962 /* A structure that holds local state for the loop in schedule_block. */
3963 struct sched_block_state
3964 {
3965 /* True if no real insns have been scheduled in the current cycle. */
3966 bool first_cycle_insn_p;
3967 /* True if a shadow insn has been scheduled in the current cycle, which
3968 means that no more normal insns can be issued. */
3969 bool shadows_only_p;
3970 /* True if we're winding down a modulo schedule, which means that we only
3971 issue insns with INSN_EXACT_TICK set. */
3972 bool modulo_epilogue;
3973 /* Initialized with the machine's issue rate every cycle, and updated
3974 by calls to the variable_issue hook. */
3975 int can_issue_more;
3976 };
3977
3978 /* INSN is the "currently executing insn". Launch each insn which was
3979 waiting on INSN. READY is the ready list which contains the insns
3980 that are ready to fire. CLOCK is the current cycle. The function
3981 returns necessary cycle advance after issuing the insn (it is not
3982 zero for insns in a schedule group). */
3983
3984 static int
3985 schedule_insn (rtx_insn *insn)
3986 {
3987 sd_iterator_def sd_it;
3988 dep_t dep;
3989 int i;
3990 int advance = 0;
3991
3992 if (sched_verbose >= 1)
3993 {
3994 struct reg_pressure_data *pressure_info;
3995 fprintf (sched_dump, ";;\t%3i--> %s %-40s:",
3996 clock_var, (*current_sched_info->print_insn) (insn, 1),
3997 str_pattern_slim (PATTERN (insn)));
3998
3999 if (recog_memoized (insn) < 0)
4000 fprintf (sched_dump, "nothing");
4001 else
4002 print_reservation (sched_dump, insn);
4003 pressure_info = INSN_REG_PRESSURE (insn);
4004 if (pressure_info != NULL)
4005 {
4006 fputc (':', sched_dump);
4007 for (i = 0; i < ira_pressure_classes_num; i++)
4008 fprintf (sched_dump, "%s%s%+d(%d)",
4009 scheduled_insns.length () > 1
4010 && INSN_LUID (insn)
4011 < INSN_LUID (scheduled_insns[scheduled_insns.length () - 2]) ? "@" : "",
4012 reg_class_names[ira_pressure_classes[i]],
4013 pressure_info[i].set_increase, pressure_info[i].change);
4014 }
4015 if (sched_pressure == SCHED_PRESSURE_MODEL
4016 && model_curr_point < model_num_insns
4017 && model_index (insn) == model_curr_point)
4018 fprintf (sched_dump, ":model %d", model_curr_point);
4019 fputc ('\n', sched_dump);
4020 }
4021
4022 if (sched_pressure == SCHED_PRESSURE_WEIGHTED && !DEBUG_INSN_P (insn))
4023 update_reg_and_insn_max_reg_pressure (insn);
4024
4025 /* Scheduling instruction should have all its dependencies resolved and
4026 should have been removed from the ready list. */
4027 gcc_assert (sd_lists_empty_p (insn, SD_LIST_HARD_BACK));
4028
4029 /* Reset debug insns invalidated by moving this insn. */
4030 if (MAY_HAVE_DEBUG_INSNS && !DEBUG_INSN_P (insn))
4031 for (sd_it = sd_iterator_start (insn, SD_LIST_BACK);
4032 sd_iterator_cond (&sd_it, &dep);)
4033 {
4034 rtx_insn *dbg = DEP_PRO (dep);
4035 struct reg_use_data *use, *next;
4036
4037 if (DEP_STATUS (dep) & DEP_CANCELLED)
4038 {
4039 sd_iterator_next (&sd_it);
4040 continue;
4041 }
4042
4043 gcc_assert (DEBUG_INSN_P (dbg));
4044
4045 if (sched_verbose >= 6)
4046 fprintf (sched_dump, ";;\t\tresetting: debug insn %d\n",
4047 INSN_UID (dbg));
4048
4049 /* ??? Rather than resetting the debug insn, we might be able
4050 to emit a debug temp before the just-scheduled insn, but
4051 this would involve checking that the expression at the
4052 point of the debug insn is equivalent to the expression
4053 before the just-scheduled insn. They might not be: the
4054 expression in the debug insn may depend on other insns not
4055 yet scheduled that set MEMs, REGs or even other debug
4056 insns. It's not clear that attempting to preserve debug
4057 information in these cases is worth the effort, given how
4058 uncommon these resets are and the likelihood that the debug
4059 temps introduced won't survive the schedule change. */
4060 INSN_VAR_LOCATION_LOC (dbg) = gen_rtx_UNKNOWN_VAR_LOC ();
4061 df_insn_rescan (dbg);
4062
4063 /* Unknown location doesn't use any registers. */
4064 for (use = INSN_REG_USE_LIST (dbg); use != NULL; use = next)
4065 {
4066 struct reg_use_data *prev = use;
4067
4068 /* Remove use from the cyclic next_regno_use chain first. */
4069 while (prev->next_regno_use != use)
4070 prev = prev->next_regno_use;
4071 prev->next_regno_use = use->next_regno_use;
4072 next = use->next_insn_use;
4073 free (use);
4074 }
4075 INSN_REG_USE_LIST (dbg) = NULL;
4076
4077 /* We delete rather than resolve these deps, otherwise we
4078 crash in sched_free_deps(), because forward deps are
4079 expected to be released before backward deps. */
4080 sd_delete_dep (sd_it);
4081 }
4082
4083 gcc_assert (QUEUE_INDEX (insn) == QUEUE_NOWHERE);
4084 QUEUE_INDEX (insn) = QUEUE_SCHEDULED;
4085
4086 if (sched_pressure == SCHED_PRESSURE_MODEL
4087 && model_curr_point < model_num_insns
4088 && NONDEBUG_INSN_P (insn))
4089 {
4090 if (model_index (insn) == model_curr_point)
4091 do
4092 model_curr_point++;
4093 while (model_curr_point < model_num_insns
4094 && (QUEUE_INDEX (MODEL_INSN (model_curr_point))
4095 == QUEUE_SCHEDULED));
4096 else
4097 model_recompute (insn);
4098 model_update_limit_points ();
4099 update_register_pressure (insn);
4100 if (sched_verbose >= 2)
4101 print_curr_reg_pressure ();
4102 }
4103
4104 gcc_assert (INSN_TICK (insn) >= MIN_TICK);
4105 if (INSN_TICK (insn) > clock_var)
4106 /* INSN has been prematurely moved from the queue to the ready list.
4107 This is possible only if following flags are set. */
4108 gcc_assert (flag_sched_stalled_insns || sched_fusion);
4109
4110 /* ??? Probably, if INSN is scheduled prematurely, we should leave
4111 INSN_TICK untouched. This is a machine-dependent issue, actually. */
4112 INSN_TICK (insn) = clock_var;
4113
4114 check_clobbered_conditions (insn);
4115
4116 /* Update dependent instructions. First, see if by scheduling this insn
4117 now we broke a dependence in a way that requires us to change another
4118 insn. */
4119 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
4120 sd_iterator_cond (&sd_it, &dep); sd_iterator_next (&sd_it))
4121 {
4122 struct dep_replacement *desc = DEP_REPLACE (dep);
4123 rtx_insn *pro = DEP_PRO (dep);
4124 if (QUEUE_INDEX (pro) != QUEUE_SCHEDULED
4125 && desc != NULL && desc->insn == pro)
4126 apply_replacement (dep, false);
4127 }
4128
4129 /* Go through and resolve forward dependencies. */
4130 for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
4131 sd_iterator_cond (&sd_it, &dep);)
4132 {
4133 rtx_insn *next = DEP_CON (dep);
4134 bool cancelled = (DEP_STATUS (dep) & DEP_CANCELLED) != 0;
4135
4136 /* Resolve the dependence between INSN and NEXT.
4137 sd_resolve_dep () moves current dep to another list thus
4138 advancing the iterator. */
4139 sd_resolve_dep (sd_it);
4140
4141 if (cancelled)
4142 {
4143 if (must_restore_pattern_p (next, dep))
4144 restore_pattern (dep, false);
4145 continue;
4146 }
4147
4148 /* Don't bother trying to mark next as ready if insn is a debug
4149 insn. If insn is the last hard dependency, it will have
4150 already been discounted. */
4151 if (DEBUG_INSN_P (insn) && !DEBUG_INSN_P (next))
4152 continue;
4153
4154 if (!IS_SPECULATION_BRANCHY_CHECK_P (insn))
4155 {
4156 int effective_cost;
4157
4158 effective_cost = try_ready (next);
4159
4160 if (effective_cost >= 0
4161 && SCHED_GROUP_P (next)
4162 && advance < effective_cost)
4163 advance = effective_cost;
4164 }
4165 else
4166 /* Check always has only one forward dependence (to the first insn in
4167 the recovery block), therefore, this will be executed only once. */
4168 {
4169 gcc_assert (sd_lists_empty_p (insn, SD_LIST_FORW));
4170 fix_recovery_deps (RECOVERY_BLOCK (insn));
4171 }
4172 }
4173
4174 /* Annotate the instruction with issue information -- TImode
4175 indicates that the instruction is expected not to be able
4176 to issue on the same cycle as the previous insn. A machine
4177 may use this information to decide how the instruction should
4178 be aligned. */
4179 if (issue_rate > 1
4180 && GET_CODE (PATTERN (insn)) != USE
4181 && GET_CODE (PATTERN (insn)) != CLOBBER
4182 && !DEBUG_INSN_P (insn))
4183 {
4184 if (reload_completed)
4185 PUT_MODE (insn, clock_var > last_clock_var ? TImode : VOIDmode);
4186 last_clock_var = clock_var;
4187 }
4188
4189 if (nonscheduled_insns_begin != NULL_RTX)
4190 /* Indicate to debug counters that INSN is scheduled. */
4191 nonscheduled_insns_begin = insn;
4192
4193 return advance;
4194 }
4195
4196 /* Functions for handling of notes. */
4197
4198 /* Add note list that ends on FROM_END to the end of TO_ENDP. */
4199 void
4200 concat_note_lists (rtx_insn *from_end, rtx_insn **to_endp)
4201 {
4202 rtx_insn *from_start;
4203
4204 /* It's easy when have nothing to concat. */
4205 if (from_end == NULL)
4206 return;
4207
4208 /* It's also easy when destination is empty. */
4209 if (*to_endp == NULL)
4210 {
4211 *to_endp = from_end;
4212 return;
4213 }
4214
4215 from_start = from_end;
4216 while (PREV_INSN (from_start) != NULL)
4217 from_start = PREV_INSN (from_start);
4218
4219 SET_PREV_INSN (from_start) = *to_endp;
4220 SET_NEXT_INSN (*to_endp) = from_start;
4221 *to_endp = from_end;
4222 }
4223
4224 /* Delete notes between HEAD and TAIL and put them in the chain
4225 of notes ended by NOTE_LIST. */
4226 void
4227 remove_notes (rtx_insn *head, rtx_insn *tail)
4228 {
4229 rtx_insn *next_tail, *insn, *next;
4230
4231 note_list = 0;
4232 if (head == tail && !INSN_P (head))
4233 return;
4234
4235 next_tail = NEXT_INSN (tail);
4236 for (insn = head; insn != next_tail; insn = next)
4237 {
4238 next = NEXT_INSN (insn);
4239 if (!NOTE_P (insn))
4240 continue;
4241
4242 switch (NOTE_KIND (insn))
4243 {
4244 case NOTE_INSN_BASIC_BLOCK:
4245 continue;
4246
4247 case NOTE_INSN_EPILOGUE_BEG:
4248 if (insn != tail)
4249 {
4250 remove_insn (insn);
4251 add_reg_note (next, REG_SAVE_NOTE,
4252 GEN_INT (NOTE_INSN_EPILOGUE_BEG));
4253 break;
4254 }
4255 /* FALLTHRU */
4256
4257 default:
4258 remove_insn (insn);
4259
4260 /* Add the note to list that ends at NOTE_LIST. */
4261 SET_PREV_INSN (insn) = note_list;
4262 SET_NEXT_INSN (insn) = NULL_RTX;
4263 if (note_list)
4264 SET_NEXT_INSN (note_list) = insn;
4265 note_list = insn;
4266 break;
4267 }
4268
4269 gcc_assert ((sel_sched_p () || insn != tail) && insn != head);
4270 }
4271 }
4272
4273 /* A structure to record enough data to allow us to backtrack the scheduler to
4274 a previous state. */
4275 struct haifa_saved_data
4276 {
4277 /* Next entry on the list. */
4278 struct haifa_saved_data *next;
4279
4280 /* Backtracking is associated with scheduling insns that have delay slots.
4281 DELAY_PAIR points to the structure that contains the insns involved, and
4282 the number of cycles between them. */
4283 struct delay_pair *delay_pair;
4284
4285 /* Data used by the frontend (e.g. sched-ebb or sched-rgn). */
4286 void *fe_saved_data;
4287 /* Data used by the backend. */
4288 void *be_saved_data;
4289
4290 /* Copies of global state. */
4291 int clock_var, last_clock_var;
4292 struct ready_list ready;
4293 state_t curr_state;
4294
4295 rtx_insn *last_scheduled_insn;
4296 rtx_insn *last_nondebug_scheduled_insn;
4297 rtx_insn *nonscheduled_insns_begin;
4298 int cycle_issued_insns;
4299
4300 /* Copies of state used in the inner loop of schedule_block. */
4301 struct sched_block_state sched_block;
4302
4303 /* We don't need to save q_ptr, as its value is arbitrary and we can set it
4304 to 0 when restoring. */
4305 int q_size;
4306 rtx_insn_list **insn_queue;
4307
4308 /* Describe pattern replacements that occurred since this backtrack point
4309 was queued. */
4310 vec<dep_t> replacement_deps;
4311 vec<int> replace_apply;
4312
4313 /* A copy of the next-cycle replacement vectors at the time of the backtrack
4314 point. */
4315 vec<dep_t> next_cycle_deps;
4316 vec<int> next_cycle_apply;
4317 };
4318
4319 /* A record, in reverse order, of all scheduled insns which have delay slots
4320 and may require backtracking. */
4321 static struct haifa_saved_data *backtrack_queue;
4322
4323 /* For every dependency of INSN, set the FEEDS_BACKTRACK_INSN bit according
4324 to SET_P. */
4325 static void
4326 mark_backtrack_feeds (rtx_insn *insn, int set_p)
4327 {
4328 sd_iterator_def sd_it;
4329 dep_t dep;
4330 FOR_EACH_DEP (insn, SD_LIST_HARD_BACK, sd_it, dep)
4331 {
4332 FEEDS_BACKTRACK_INSN (DEP_PRO (dep)) = set_p;
4333 }
4334 }
4335
4336 /* Save the current scheduler state so that we can backtrack to it
4337 later if necessary. PAIR gives the insns that make it necessary to
4338 save this point. SCHED_BLOCK is the local state of schedule_block
4339 that need to be saved. */
4340 static void
4341 save_backtrack_point (struct delay_pair *pair,
4342 struct sched_block_state sched_block)
4343 {
4344 int i;
4345 struct haifa_saved_data *save = XNEW (struct haifa_saved_data);
4346
4347 save->curr_state = xmalloc (dfa_state_size);
4348 memcpy (save->curr_state, curr_state, dfa_state_size);
4349
4350 save->ready.first = ready.first;
4351 save->ready.n_ready = ready.n_ready;
4352 save->ready.n_debug = ready.n_debug;
4353 save->ready.veclen = ready.veclen;
4354 save->ready.vec = XNEWVEC (rtx_insn *, ready.veclen);
4355 memcpy (save->ready.vec, ready.vec, ready.veclen * sizeof (rtx));
4356
4357 save->insn_queue = XNEWVEC (rtx_insn_list *, max_insn_queue_index + 1);
4358 save->q_size = q_size;
4359 for (i = 0; i <= max_insn_queue_index; i++)
4360 {
4361 int q = NEXT_Q_AFTER (q_ptr, i);
4362 save->insn_queue[i] = copy_INSN_LIST (insn_queue[q]);
4363 }
4364
4365 save->clock_var = clock_var;
4366 save->last_clock_var = last_clock_var;
4367 save->cycle_issued_insns = cycle_issued_insns;
4368 save->last_scheduled_insn = last_scheduled_insn;
4369 save->last_nondebug_scheduled_insn = last_nondebug_scheduled_insn;
4370 save->nonscheduled_insns_begin = nonscheduled_insns_begin;
4371
4372 save->sched_block = sched_block;
4373
4374 save->replacement_deps.create (0);
4375 save->replace_apply.create (0);
4376 save->next_cycle_deps = next_cycle_replace_deps.copy ();
4377 save->next_cycle_apply = next_cycle_apply.copy ();
4378
4379 if (current_sched_info->save_state)
4380 save->fe_saved_data = (*current_sched_info->save_state) ();
4381
4382 if (targetm.sched.alloc_sched_context)
4383 {
4384 save->be_saved_data = targetm.sched.alloc_sched_context ();
4385 targetm.sched.init_sched_context (save->be_saved_data, false);
4386 }
4387 else
4388 save->be_saved_data = NULL;
4389
4390 save->delay_pair = pair;
4391
4392 save->next = backtrack_queue;
4393 backtrack_queue = save;
4394
4395 while (pair)
4396 {
4397 mark_backtrack_feeds (pair->i2, 1);
4398 INSN_TICK (pair->i2) = INVALID_TICK;
4399 INSN_EXACT_TICK (pair->i2) = clock_var + pair_delay (pair);
4400 SHADOW_P (pair->i2) = pair->stages == 0;
4401 pair = pair->next_same_i1;
4402 }
4403 }
4404
4405 /* Walk the ready list and all queues. If any insns have unresolved backwards
4406 dependencies, these must be cancelled deps, broken by predication. Set or
4407 clear (depending on SET) the DEP_CANCELLED bit in DEP_STATUS. */
4408
4409 static void
4410 toggle_cancelled_flags (bool set)
4411 {
4412 int i;
4413 sd_iterator_def sd_it;
4414 dep_t dep;
4415
4416 if (ready.n_ready > 0)
4417 {
4418 rtx_insn **first = ready_lastpos (&ready);
4419 for (i = 0; i < ready.n_ready; i++)
4420 FOR_EACH_DEP (first[i], SD_LIST_BACK, sd_it, dep)
4421 if (!DEBUG_INSN_P (DEP_PRO (dep)))
4422 {
4423 if (set)
4424 DEP_STATUS (dep) |= DEP_CANCELLED;
4425 else
4426 DEP_STATUS (dep) &= ~DEP_CANCELLED;
4427 }
4428 }
4429 for (i = 0; i <= max_insn_queue_index; i++)
4430 {
4431 int q = NEXT_Q_AFTER (q_ptr, i);
4432 rtx_insn_list *link;
4433 for (link = insn_queue[q]; link; link = link->next ())
4434 {
4435 rtx_insn *insn = link->insn ();
4436 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
4437 if (!DEBUG_INSN_P (DEP_PRO (dep)))
4438 {
4439 if (set)
4440 DEP_STATUS (dep) |= DEP_CANCELLED;
4441 else
4442 DEP_STATUS (dep) &= ~DEP_CANCELLED;
4443 }
4444 }
4445 }
4446 }
4447
4448 /* Undo the replacements that have occurred after backtrack point SAVE
4449 was placed. */
4450 static void
4451 undo_replacements_for_backtrack (struct haifa_saved_data *save)
4452 {
4453 while (!save->replacement_deps.is_empty ())
4454 {
4455 dep_t dep = save->replacement_deps.pop ();
4456 int apply_p = save->replace_apply.pop ();
4457
4458 if (apply_p)
4459 restore_pattern (dep, true);
4460 else
4461 apply_replacement (dep, true);
4462 }
4463 save->replacement_deps.release ();
4464 save->replace_apply.release ();
4465 }
4466
4467 /* Pop entries from the SCHEDULED_INSNS vector up to and including INSN.
4468 Restore their dependencies to an unresolved state, and mark them as
4469 queued nowhere. */
4470
4471 static void
4472 unschedule_insns_until (rtx_insn *insn)
4473 {
4474 auto_vec<rtx_insn *> recompute_vec;
4475
4476 /* Make two passes over the insns to be unscheduled. First, we clear out
4477 dependencies and other trivial bookkeeping. */
4478 for (;;)
4479 {
4480 rtx_insn *last;
4481 sd_iterator_def sd_it;
4482 dep_t dep;
4483
4484 last = scheduled_insns.pop ();
4485
4486 /* This will be changed by restore_backtrack_point if the insn is in
4487 any queue. */
4488 QUEUE_INDEX (last) = QUEUE_NOWHERE;
4489 if (last != insn)
4490 INSN_TICK (last) = INVALID_TICK;
4491
4492 if (modulo_ii > 0 && INSN_UID (last) < modulo_iter0_max_uid)
4493 modulo_insns_scheduled--;
4494
4495 for (sd_it = sd_iterator_start (last, SD_LIST_RES_FORW);
4496 sd_iterator_cond (&sd_it, &dep);)
4497 {
4498 rtx_insn *con = DEP_CON (dep);
4499 sd_unresolve_dep (sd_it);
4500 if (!MUST_RECOMPUTE_SPEC_P (con))
4501 {
4502 MUST_RECOMPUTE_SPEC_P (con) = 1;
4503 recompute_vec.safe_push (con);
4504 }
4505 }
4506
4507 if (last == insn)
4508 break;
4509 }
4510
4511 /* A second pass, to update ready and speculation status for insns
4512 depending on the unscheduled ones. The first pass must have
4513 popped the scheduled_insns vector up to the point where we
4514 restart scheduling, as recompute_todo_spec requires it to be
4515 up-to-date. */
4516 while (!recompute_vec.is_empty ())
4517 {
4518 rtx_insn *con;
4519
4520 con = recompute_vec.pop ();
4521 MUST_RECOMPUTE_SPEC_P (con) = 0;
4522 if (!sd_lists_empty_p (con, SD_LIST_HARD_BACK))
4523 {
4524 TODO_SPEC (con) = HARD_DEP;
4525 INSN_TICK (con) = INVALID_TICK;
4526 if (PREDICATED_PAT (con) != NULL_RTX)
4527 haifa_change_pattern (con, ORIG_PAT (con));
4528 }
4529 else if (QUEUE_INDEX (con) != QUEUE_SCHEDULED)
4530 TODO_SPEC (con) = recompute_todo_spec (con, true);
4531 }
4532 }
4533
4534 /* Restore scheduler state from the topmost entry on the backtracking queue.
4535 PSCHED_BLOCK_P points to the local data of schedule_block that we must
4536 overwrite with the saved data.
4537 The caller must already have called unschedule_insns_until. */
4538
4539 static void
4540 restore_last_backtrack_point (struct sched_block_state *psched_block)
4541 {
4542 int i;
4543 struct haifa_saved_data *save = backtrack_queue;
4544
4545 backtrack_queue = save->next;
4546
4547 if (current_sched_info->restore_state)
4548 (*current_sched_info->restore_state) (save->fe_saved_data);
4549
4550 if (targetm.sched.alloc_sched_context)
4551 {
4552 targetm.sched.set_sched_context (save->be_saved_data);
4553 targetm.sched.free_sched_context (save->be_saved_data);
4554 }
4555
4556 /* Do this first since it clobbers INSN_TICK of the involved
4557 instructions. */
4558 undo_replacements_for_backtrack (save);
4559
4560 /* Clear the QUEUE_INDEX of everything in the ready list or one
4561 of the queues. */
4562 if (ready.n_ready > 0)
4563 {
4564 rtx_insn **first = ready_lastpos (&ready);
4565 for (i = 0; i < ready.n_ready; i++)
4566 {
4567 rtx_insn *insn = first[i];
4568 QUEUE_INDEX (insn) = QUEUE_NOWHERE;
4569 INSN_TICK (insn) = INVALID_TICK;
4570 }
4571 }
4572 for (i = 0; i <= max_insn_queue_index; i++)
4573 {
4574 int q = NEXT_Q_AFTER (q_ptr, i);
4575
4576 for (rtx_insn_list *link = insn_queue[q]; link; link = link->next ())
4577 {
4578 rtx_insn *x = link->insn ();
4579 QUEUE_INDEX (x) = QUEUE_NOWHERE;
4580 INSN_TICK (x) = INVALID_TICK;
4581 }
4582 free_INSN_LIST_list (&insn_queue[q]);
4583 }
4584
4585 free (ready.vec);
4586 ready = save->ready;
4587
4588 if (ready.n_ready > 0)
4589 {
4590 rtx_insn **first = ready_lastpos (&ready);
4591 for (i = 0; i < ready.n_ready; i++)
4592 {
4593 rtx_insn *insn = first[i];
4594 QUEUE_INDEX (insn) = QUEUE_READY;
4595 TODO_SPEC (insn) = recompute_todo_spec (insn, true);
4596 INSN_TICK (insn) = save->clock_var;
4597 }
4598 }
4599
4600 q_ptr = 0;
4601 q_size = save->q_size;
4602 for (i = 0; i <= max_insn_queue_index; i++)
4603 {
4604 int q = NEXT_Q_AFTER (q_ptr, i);
4605
4606 insn_queue[q] = save->insn_queue[q];
4607
4608 for (rtx_insn_list *link = insn_queue[q]; link; link = link->next ())
4609 {
4610 rtx_insn *x = link->insn ();
4611 QUEUE_INDEX (x) = i;
4612 TODO_SPEC (x) = recompute_todo_spec (x, true);
4613 INSN_TICK (x) = save->clock_var + i;
4614 }
4615 }
4616 free (save->insn_queue);
4617
4618 toggle_cancelled_flags (true);
4619
4620 clock_var = save->clock_var;
4621 last_clock_var = save->last_clock_var;
4622 cycle_issued_insns = save->cycle_issued_insns;
4623 last_scheduled_insn = save->last_scheduled_insn;
4624 last_nondebug_scheduled_insn = save->last_nondebug_scheduled_insn;
4625 nonscheduled_insns_begin = save->nonscheduled_insns_begin;
4626
4627 *psched_block = save->sched_block;
4628
4629 memcpy (curr_state, save->curr_state, dfa_state_size);
4630 free (save->curr_state);
4631
4632 mark_backtrack_feeds (save->delay_pair->i2, 0);
4633
4634 gcc_assert (next_cycle_replace_deps.is_empty ());
4635 next_cycle_replace_deps = save->next_cycle_deps.copy ();
4636 next_cycle_apply = save->next_cycle_apply.copy ();
4637
4638 free (save);
4639
4640 for (save = backtrack_queue; save; save = save->next)
4641 {
4642 mark_backtrack_feeds (save->delay_pair->i2, 1);
4643 }
4644 }
4645
4646 /* Discard all data associated with the topmost entry in the backtrack
4647 queue. If RESET_TICK is false, we just want to free the data. If true,
4648 we are doing this because we discovered a reason to backtrack. In the
4649 latter case, also reset the INSN_TICK for the shadow insn. */
4650 static void
4651 free_topmost_backtrack_point (bool reset_tick)
4652 {
4653 struct haifa_saved_data *save = backtrack_queue;
4654 int i;
4655
4656 backtrack_queue = save->next;
4657
4658 if (reset_tick)
4659 {
4660 struct delay_pair *pair = save->delay_pair;
4661 while (pair)
4662 {
4663 INSN_TICK (pair->i2) = INVALID_TICK;
4664 INSN_EXACT_TICK (pair->i2) = INVALID_TICK;
4665 pair = pair->next_same_i1;
4666 }
4667 undo_replacements_for_backtrack (save);
4668 }
4669 else
4670 {
4671 save->replacement_deps.release ();
4672 save->replace_apply.release ();
4673 }
4674
4675 if (targetm.sched.free_sched_context)
4676 targetm.sched.free_sched_context (save->be_saved_data);
4677 if (current_sched_info->restore_state)
4678 free (save->fe_saved_data);
4679 for (i = 0; i <= max_insn_queue_index; i++)
4680 free_INSN_LIST_list (&save->insn_queue[i]);
4681 free (save->insn_queue);
4682 free (save->curr_state);
4683 free (save->ready.vec);
4684 free (save);
4685 }
4686
4687 /* Free the entire backtrack queue. */
4688 static void
4689 free_backtrack_queue (void)
4690 {
4691 while (backtrack_queue)
4692 free_topmost_backtrack_point (false);
4693 }
4694
4695 /* Apply a replacement described by DESC. If IMMEDIATELY is false, we
4696 may have to postpone the replacement until the start of the next cycle,
4697 at which point we will be called again with IMMEDIATELY true. This is
4698 only done for machines which have instruction packets with explicit
4699 parallelism however. */
4700 static void
4701 apply_replacement (dep_t dep, bool immediately)
4702 {
4703 struct dep_replacement *desc = DEP_REPLACE (dep);
4704 if (!immediately && targetm.sched.exposed_pipeline && reload_completed)
4705 {
4706 next_cycle_replace_deps.safe_push (dep);
4707 next_cycle_apply.safe_push (1);
4708 }
4709 else
4710 {
4711 bool success;
4712
4713 if (QUEUE_INDEX (desc->insn) == QUEUE_SCHEDULED)
4714 return;
4715
4716 if (sched_verbose >= 5)
4717 fprintf (sched_dump, "applying replacement for insn %d\n",
4718 INSN_UID (desc->insn));
4719
4720 success = validate_change (desc->insn, desc->loc, desc->newval, 0);
4721 gcc_assert (success);
4722
4723 update_insn_after_change (desc->insn);
4724 if ((TODO_SPEC (desc->insn) & (HARD_DEP | DEP_POSTPONED)) == 0)
4725 fix_tick_ready (desc->insn);
4726
4727 if (backtrack_queue != NULL)
4728 {
4729 backtrack_queue->replacement_deps.safe_push (dep);
4730 backtrack_queue->replace_apply.safe_push (1);
4731 }
4732 }
4733 }
4734
4735 /* We have determined that a pattern involved in DEP must be restored.
4736 If IMMEDIATELY is false, we may have to postpone the replacement
4737 until the start of the next cycle, at which point we will be called
4738 again with IMMEDIATELY true. */
4739 static void
4740 restore_pattern (dep_t dep, bool immediately)
4741 {
4742 rtx_insn *next = DEP_CON (dep);
4743 int tick = INSN_TICK (next);
4744
4745 /* If we already scheduled the insn, the modified version is
4746 correct. */
4747 if (QUEUE_INDEX (next) == QUEUE_SCHEDULED)
4748 return;
4749
4750 if (!immediately && targetm.sched.exposed_pipeline && reload_completed)
4751 {
4752 next_cycle_replace_deps.safe_push (dep);
4753 next_cycle_apply.safe_push (0);
4754 return;
4755 }
4756
4757
4758 if (DEP_TYPE (dep) == REG_DEP_CONTROL)
4759 {
4760 if (sched_verbose >= 5)
4761 fprintf (sched_dump, "restoring pattern for insn %d\n",
4762 INSN_UID (next));
4763 haifa_change_pattern (next, ORIG_PAT (next));
4764 }
4765 else
4766 {
4767 struct dep_replacement *desc = DEP_REPLACE (dep);
4768 bool success;
4769
4770 if (sched_verbose >= 5)
4771 fprintf (sched_dump, "restoring pattern for insn %d\n",
4772 INSN_UID (desc->insn));
4773 tick = INSN_TICK (desc->insn);
4774
4775 success = validate_change (desc->insn, desc->loc, desc->orig, 0);
4776 gcc_assert (success);
4777 update_insn_after_change (desc->insn);
4778 if (backtrack_queue != NULL)
4779 {
4780 backtrack_queue->replacement_deps.safe_push (dep);
4781 backtrack_queue->replace_apply.safe_push (0);
4782 }
4783 }
4784 INSN_TICK (next) = tick;
4785 if (TODO_SPEC (next) == DEP_POSTPONED)
4786 return;
4787
4788 if (sd_lists_empty_p (next, SD_LIST_BACK))
4789 TODO_SPEC (next) = 0;
4790 else if (!sd_lists_empty_p (next, SD_LIST_HARD_BACK))
4791 TODO_SPEC (next) = HARD_DEP;
4792 }
4793
4794 /* Perform pattern replacements that were queued up until the next
4795 cycle. */
4796 static void
4797 perform_replacements_new_cycle (void)
4798 {
4799 int i;
4800 dep_t dep;
4801 FOR_EACH_VEC_ELT (next_cycle_replace_deps, i, dep)
4802 {
4803 int apply_p = next_cycle_apply[i];
4804 if (apply_p)
4805 apply_replacement (dep, true);
4806 else
4807 restore_pattern (dep, true);
4808 }
4809 next_cycle_replace_deps.truncate (0);
4810 next_cycle_apply.truncate (0);
4811 }
4812
4813 /* Compute INSN_TICK_ESTIMATE for INSN. PROCESSED is a bitmap of
4814 instructions we've previously encountered, a set bit prevents
4815 recursion. BUDGET is a limit on how far ahead we look, it is
4816 reduced on recursive calls. Return true if we produced a good
4817 estimate, or false if we exceeded the budget. */
4818 static bool
4819 estimate_insn_tick (bitmap processed, rtx_insn *insn, int budget)
4820 {
4821 sd_iterator_def sd_it;
4822 dep_t dep;
4823 int earliest = INSN_TICK (insn);
4824
4825 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
4826 {
4827 rtx_insn *pro = DEP_PRO (dep);
4828 int t;
4829
4830 if (DEP_STATUS (dep) & DEP_CANCELLED)
4831 continue;
4832
4833 if (QUEUE_INDEX (pro) == QUEUE_SCHEDULED)
4834 gcc_assert (INSN_TICK (pro) + dep_cost (dep) <= INSN_TICK (insn));
4835 else
4836 {
4837 int cost = dep_cost (dep);
4838 if (cost >= budget)
4839 return false;
4840 if (!bitmap_bit_p (processed, INSN_LUID (pro)))
4841 {
4842 if (!estimate_insn_tick (processed, pro, budget - cost))
4843 return false;
4844 }
4845 gcc_assert (INSN_TICK_ESTIMATE (pro) != INVALID_TICK);
4846 t = INSN_TICK_ESTIMATE (pro) + cost;
4847 if (earliest == INVALID_TICK || t > earliest)
4848 earliest = t;
4849 }
4850 }
4851 bitmap_set_bit (processed, INSN_LUID (insn));
4852 INSN_TICK_ESTIMATE (insn) = earliest;
4853 return true;
4854 }
4855
4856 /* Examine the pair of insns in P, and estimate (optimistically, assuming
4857 infinite resources) the cycle in which the delayed shadow can be issued.
4858 Return the number of cycles that must pass before the real insn can be
4859 issued in order to meet this constraint. */
4860 static int
4861 estimate_shadow_tick (struct delay_pair *p)
4862 {
4863 bitmap_head processed;
4864 int t;
4865 bool cutoff;
4866 bitmap_initialize (&processed, 0);
4867
4868 cutoff = !estimate_insn_tick (&processed, p->i2,
4869 max_insn_queue_index + pair_delay (p));
4870 bitmap_clear (&processed);
4871 if (cutoff)
4872 return max_insn_queue_index;
4873 t = INSN_TICK_ESTIMATE (p->i2) - (clock_var + pair_delay (p) + 1);
4874 if (t > 0)
4875 return t;
4876 return 0;
4877 }
4878
4879 /* If INSN has no unresolved backwards dependencies, add it to the schedule and
4880 recursively resolve all its forward dependencies. */
4881 static void
4882 resolve_dependencies (rtx_insn *insn)
4883 {
4884 sd_iterator_def sd_it;
4885 dep_t dep;
4886
4887 /* Don't use sd_lists_empty_p; it ignores debug insns. */
4888 if (DEPS_LIST_FIRST (INSN_HARD_BACK_DEPS (insn)) != NULL
4889 || DEPS_LIST_FIRST (INSN_SPEC_BACK_DEPS (insn)) != NULL)
4890 return;
4891
4892 if (sched_verbose >= 4)
4893 fprintf (sched_dump, ";;\tquickly resolving %d\n", INSN_UID (insn));
4894
4895 if (QUEUE_INDEX (insn) >= 0)
4896 queue_remove (insn);
4897
4898 scheduled_insns.safe_push (insn);
4899
4900 /* Update dependent instructions. */
4901 for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
4902 sd_iterator_cond (&sd_it, &dep);)
4903 {
4904 rtx_insn *next = DEP_CON (dep);
4905
4906 if (sched_verbose >= 4)
4907 fprintf (sched_dump, ";;\t\tdep %d against %d\n", INSN_UID (insn),
4908 INSN_UID (next));
4909
4910 /* Resolve the dependence between INSN and NEXT.
4911 sd_resolve_dep () moves current dep to another list thus
4912 advancing the iterator. */
4913 sd_resolve_dep (sd_it);
4914
4915 if (!IS_SPECULATION_BRANCHY_CHECK_P (insn))
4916 {
4917 resolve_dependencies (next);
4918 }
4919 else
4920 /* Check always has only one forward dependence (to the first insn in
4921 the recovery block), therefore, this will be executed only once. */
4922 {
4923 gcc_assert (sd_lists_empty_p (insn, SD_LIST_FORW));
4924 }
4925 }
4926 }
4927
4928
4929 /* Return the head and tail pointers of ebb starting at BEG and ending
4930 at END. */
4931 void
4932 get_ebb_head_tail (basic_block beg, basic_block end,
4933 rtx_insn **headp, rtx_insn **tailp)
4934 {
4935 rtx_insn *beg_head = BB_HEAD (beg);
4936 rtx_insn * beg_tail = BB_END (beg);
4937 rtx_insn * end_head = BB_HEAD (end);
4938 rtx_insn * end_tail = BB_END (end);
4939
4940 /* Don't include any notes or labels at the beginning of the BEG
4941 basic block, or notes at the end of the END basic blocks. */
4942
4943 if (LABEL_P (beg_head))
4944 beg_head = NEXT_INSN (beg_head);
4945
4946 while (beg_head != beg_tail)
4947 if (NOTE_P (beg_head))
4948 beg_head = NEXT_INSN (beg_head);
4949 else if (DEBUG_INSN_P (beg_head))
4950 {
4951 rtx_insn * note, *next;
4952
4953 for (note = NEXT_INSN (beg_head);
4954 note != beg_tail;
4955 note = next)
4956 {
4957 next = NEXT_INSN (note);
4958 if (NOTE_P (note))
4959 {
4960 if (sched_verbose >= 9)
4961 fprintf (sched_dump, "reorder %i\n", INSN_UID (note));
4962
4963 reorder_insns_nobb (note, note, PREV_INSN (beg_head));
4964
4965 if (BLOCK_FOR_INSN (note) != beg)
4966 df_insn_change_bb (note, beg);
4967 }
4968 else if (!DEBUG_INSN_P (note))
4969 break;
4970 }
4971
4972 break;
4973 }
4974 else
4975 break;
4976
4977 *headp = beg_head;
4978
4979 if (beg == end)
4980 end_head = beg_head;
4981 else if (LABEL_P (end_head))
4982 end_head = NEXT_INSN (end_head);
4983
4984 while (end_head != end_tail)
4985 if (NOTE_P (end_tail))
4986 end_tail = PREV_INSN (end_tail);
4987 else if (DEBUG_INSN_P (end_tail))
4988 {
4989 rtx_insn * note, *prev;
4990
4991 for (note = PREV_INSN (end_tail);
4992 note != end_head;
4993 note = prev)
4994 {
4995 prev = PREV_INSN (note);
4996 if (NOTE_P (note))
4997 {
4998 if (sched_verbose >= 9)
4999 fprintf (sched_dump, "reorder %i\n", INSN_UID (note));
5000
5001 reorder_insns_nobb (note, note, end_tail);
5002
5003 if (end_tail == BB_END (end))
5004 BB_END (end) = note;
5005
5006 if (BLOCK_FOR_INSN (note) != end)
5007 df_insn_change_bb (note, end);
5008 }
5009 else if (!DEBUG_INSN_P (note))
5010 break;
5011 }
5012
5013 break;
5014 }
5015 else
5016 break;
5017
5018 *tailp = end_tail;
5019 }
5020
5021 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ]. */
5022
5023 int
5024 no_real_insns_p (const rtx_insn *head, const rtx_insn *tail)
5025 {
5026 while (head != NEXT_INSN (tail))
5027 {
5028 if (!NOTE_P (head) && !LABEL_P (head))
5029 return 0;
5030 head = NEXT_INSN (head);
5031 }
5032 return 1;
5033 }
5034
5035 /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
5036 previously found among the insns. Insert them just before HEAD. */
5037 rtx_insn *
5038 restore_other_notes (rtx_insn *head, basic_block head_bb)
5039 {
5040 if (note_list != 0)
5041 {
5042 rtx_insn *note_head = note_list;
5043
5044 if (head)
5045 head_bb = BLOCK_FOR_INSN (head);
5046 else
5047 head = NEXT_INSN (bb_note (head_bb));
5048
5049 while (PREV_INSN (note_head))
5050 {
5051 set_block_for_insn (note_head, head_bb);
5052 note_head = PREV_INSN (note_head);
5053 }
5054 /* In the above cycle we've missed this note. */
5055 set_block_for_insn (note_head, head_bb);
5056
5057 SET_PREV_INSN (note_head) = PREV_INSN (head);
5058 SET_NEXT_INSN (PREV_INSN (head)) = note_head;
5059 SET_PREV_INSN (head) = note_list;
5060 SET_NEXT_INSN (note_list) = head;
5061
5062 if (BLOCK_FOR_INSN (head) != head_bb)
5063 BB_END (head_bb) = note_list;
5064
5065 head = note_head;
5066 }
5067
5068 return head;
5069 }
5070
5071 /* When we know we are going to discard the schedule due to a failed attempt
5072 at modulo scheduling, undo all replacements. */
5073 static void
5074 undo_all_replacements (void)
5075 {
5076 rtx_insn *insn;
5077 int i;
5078
5079 FOR_EACH_VEC_ELT (scheduled_insns, i, insn)
5080 {
5081 sd_iterator_def sd_it;
5082 dep_t dep;
5083
5084 /* See if we must undo a replacement. */
5085 for (sd_it = sd_iterator_start (insn, SD_LIST_RES_FORW);
5086 sd_iterator_cond (&sd_it, &dep); sd_iterator_next (&sd_it))
5087 {
5088 struct dep_replacement *desc = DEP_REPLACE (dep);
5089 if (desc != NULL)
5090 validate_change (desc->insn, desc->loc, desc->orig, 0);
5091 }
5092 }
5093 }
5094
5095 /* Return first non-scheduled insn in the current scheduling block.
5096 This is mostly used for debug-counter purposes. */
5097 static rtx_insn *
5098 first_nonscheduled_insn (void)
5099 {
5100 rtx_insn *insn = (nonscheduled_insns_begin != NULL_RTX
5101 ? nonscheduled_insns_begin
5102 : current_sched_info->prev_head);
5103
5104 do
5105 {
5106 insn = next_nonnote_nondebug_insn (insn);
5107 }
5108 while (QUEUE_INDEX (insn) == QUEUE_SCHEDULED);
5109
5110 return insn;
5111 }
5112
5113 /* Move insns that became ready to fire from queue to ready list. */
5114
5115 static void
5116 queue_to_ready (struct ready_list *ready)
5117 {
5118 rtx_insn *insn;
5119 rtx_insn_list *link;
5120 rtx_insn *skip_insn;
5121
5122 q_ptr = NEXT_Q (q_ptr);
5123
5124 if (dbg_cnt (sched_insn) == false)
5125 /* If debug counter is activated do not requeue the first
5126 nonscheduled insn. */
5127 skip_insn = first_nonscheduled_insn ();
5128 else
5129 skip_insn = NULL;
5130
5131 /* Add all pending insns that can be scheduled without stalls to the
5132 ready list. */
5133 for (link = insn_queue[q_ptr]; link; link = link->next ())
5134 {
5135 insn = link->insn ();
5136 q_size -= 1;
5137
5138 if (sched_verbose >= 2)
5139 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
5140 (*current_sched_info->print_insn) (insn, 0));
5141
5142 /* If the ready list is full, delay the insn for 1 cycle.
5143 See the comment in schedule_block for the rationale. */
5144 if (!reload_completed
5145 && (ready->n_ready - ready->n_debug > MAX_SCHED_READY_INSNS
5146 || (sched_pressure == SCHED_PRESSURE_MODEL
5147 /* Limit pressure recalculations to MAX_SCHED_READY_INSNS
5148 instructions too. */
5149 && model_index (insn) > (model_curr_point
5150 + MAX_SCHED_READY_INSNS)))
5151 && !(sched_pressure == SCHED_PRESSURE_MODEL
5152 && model_curr_point < model_num_insns
5153 /* Always allow the next model instruction to issue. */
5154 && model_index (insn) == model_curr_point)
5155 && !SCHED_GROUP_P (insn)
5156 && insn != skip_insn)
5157 {
5158 if (sched_verbose >= 2)
5159 fprintf (sched_dump, "keeping in queue, ready full\n");
5160 queue_insn (insn, 1, "ready full");
5161 }
5162 else
5163 {
5164 ready_add (ready, insn, false);
5165 if (sched_verbose >= 2)
5166 fprintf (sched_dump, "moving to ready without stalls\n");
5167 }
5168 }
5169 free_INSN_LIST_list (&insn_queue[q_ptr]);
5170
5171 /* If there are no ready insns, stall until one is ready and add all
5172 of the pending insns at that point to the ready list. */
5173 if (ready->n_ready == 0)
5174 {
5175 int stalls;
5176
5177 for (stalls = 1; stalls <= max_insn_queue_index; stalls++)
5178 {
5179 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
5180 {
5181 for (; link; link = link->next ())
5182 {
5183 insn = link->insn ();
5184 q_size -= 1;
5185
5186 if (sched_verbose >= 2)
5187 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
5188 (*current_sched_info->print_insn) (insn, 0));
5189
5190 ready_add (ready, insn, false);
5191 if (sched_verbose >= 2)
5192 fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
5193 }
5194 free_INSN_LIST_list (&insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]);
5195
5196 advance_one_cycle ();
5197
5198 break;
5199 }
5200
5201 advance_one_cycle ();
5202 }
5203
5204 q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
5205 clock_var += stalls;
5206 if (sched_verbose >= 2)
5207 fprintf (sched_dump, ";;\tAdvancing clock by %d cycle[s] to %d\n",
5208 stalls, clock_var);
5209 }
5210 }
5211
5212 /* Used by early_queue_to_ready. Determines whether it is "ok" to
5213 prematurely move INSN from the queue to the ready list. Currently,
5214 if a target defines the hook 'is_costly_dependence', this function
5215 uses the hook to check whether there exist any dependences which are
5216 considered costly by the target, between INSN and other insns that
5217 have already been scheduled. Dependences are checked up to Y cycles
5218 back, with default Y=1; The flag -fsched-stalled-insns-dep=Y allows
5219 controlling this value.
5220 (Other considerations could be taken into account instead (or in
5221 addition) depending on user flags and target hooks. */
5222
5223 static bool
5224 ok_for_early_queue_removal (rtx_insn *insn)
5225 {
5226 if (targetm.sched.is_costly_dependence)
5227 {
5228 int n_cycles;
5229 int i = scheduled_insns.length ();
5230 for (n_cycles = flag_sched_stalled_insns_dep; n_cycles; n_cycles--)
5231 {
5232 while (i-- > 0)
5233 {
5234 int cost;
5235
5236 rtx_insn *prev_insn = scheduled_insns[i];
5237
5238 if (!NOTE_P (prev_insn))
5239 {
5240 dep_t dep;
5241
5242 dep = sd_find_dep_between (prev_insn, insn, true);
5243
5244 if (dep != NULL)
5245 {
5246 cost = dep_cost (dep);
5247
5248 if (targetm.sched.is_costly_dependence (dep, cost,
5249 flag_sched_stalled_insns_dep - n_cycles))
5250 return false;
5251 }
5252 }
5253
5254 if (GET_MODE (prev_insn) == TImode) /* end of dispatch group */
5255 break;
5256 }
5257
5258 if (i == 0)
5259 break;
5260 }
5261 }
5262
5263 return true;
5264 }
5265
5266
5267 /* Remove insns from the queue, before they become "ready" with respect
5268 to FU latency considerations. */
5269
5270 static int
5271 early_queue_to_ready (state_t state, struct ready_list *ready)
5272 {
5273 rtx_insn *insn;
5274 rtx_insn_list *link;
5275 rtx_insn_list *next_link;
5276 rtx_insn_list *prev_link;
5277 bool move_to_ready;
5278 int cost;
5279 state_t temp_state = alloca (dfa_state_size);
5280 int stalls;
5281 int insns_removed = 0;
5282
5283 /*
5284 Flag '-fsched-stalled-insns=X' determines the aggressiveness of this
5285 function:
5286
5287 X == 0: There is no limit on how many queued insns can be removed
5288 prematurely. (flag_sched_stalled_insns = -1).
5289
5290 X >= 1: Only X queued insns can be removed prematurely in each
5291 invocation. (flag_sched_stalled_insns = X).
5292
5293 Otherwise: Early queue removal is disabled.
5294 (flag_sched_stalled_insns = 0)
5295 */
5296
5297 if (! flag_sched_stalled_insns)
5298 return 0;
5299
5300 for (stalls = 0; stalls <= max_insn_queue_index; stalls++)
5301 {
5302 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
5303 {
5304 if (sched_verbose > 6)
5305 fprintf (sched_dump, ";; look at index %d + %d\n", q_ptr, stalls);
5306
5307 prev_link = 0;
5308 while (link)
5309 {
5310 next_link = link->next ();
5311 insn = link->insn ();
5312 if (insn && sched_verbose > 6)
5313 print_rtl_single (sched_dump, insn);
5314
5315 memcpy (temp_state, state, dfa_state_size);
5316 if (recog_memoized (insn) < 0)
5317 /* non-negative to indicate that it's not ready
5318 to avoid infinite Q->R->Q->R... */
5319 cost = 0;
5320 else
5321 cost = state_transition (temp_state, insn);
5322
5323 if (sched_verbose >= 6)
5324 fprintf (sched_dump, "transition cost = %d\n", cost);
5325
5326 move_to_ready = false;
5327 if (cost < 0)
5328 {
5329 move_to_ready = ok_for_early_queue_removal (insn);
5330 if (move_to_ready == true)
5331 {
5332 /* move from Q to R */
5333 q_size -= 1;
5334 ready_add (ready, insn, false);
5335
5336 if (prev_link)
5337 XEXP (prev_link, 1) = next_link;
5338 else
5339 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = next_link;
5340
5341 free_INSN_LIST_node (link);
5342
5343 if (sched_verbose >= 2)
5344 fprintf (sched_dump, ";;\t\tEarly Q-->Ready: insn %s\n",
5345 (*current_sched_info->print_insn) (insn, 0));
5346
5347 insns_removed++;
5348 if (insns_removed == flag_sched_stalled_insns)
5349 /* Remove no more than flag_sched_stalled_insns insns
5350 from Q at a time. */
5351 return insns_removed;
5352 }
5353 }
5354
5355 if (move_to_ready == false)
5356 prev_link = link;
5357
5358 link = next_link;
5359 } /* while link */
5360 } /* if link */
5361
5362 } /* for stalls.. */
5363
5364 return insns_removed;
5365 }
5366
5367
5368 /* Print the ready list for debugging purposes.
5369 If READY_TRY is non-zero then only print insns that max_issue
5370 will consider. */
5371 static void
5372 debug_ready_list_1 (struct ready_list *ready, signed char *ready_try)
5373 {
5374 rtx_insn **p;
5375 int i;
5376
5377 if (ready->n_ready == 0)
5378 {
5379 fprintf (sched_dump, "\n");
5380 return;
5381 }
5382
5383 p = ready_lastpos (ready);
5384 for (i = 0; i < ready->n_ready; i++)
5385 {
5386 if (ready_try != NULL && ready_try[ready->n_ready - i - 1])
5387 continue;
5388
5389 fprintf (sched_dump, " %s:%d",
5390 (*current_sched_info->print_insn) (p[i], 0),
5391 INSN_LUID (p[i]));
5392 if (sched_pressure != SCHED_PRESSURE_NONE)
5393 fprintf (sched_dump, "(cost=%d",
5394 INSN_REG_PRESSURE_EXCESS_COST_CHANGE (p[i]));
5395 fprintf (sched_dump, ":prio=%d", INSN_PRIORITY (p[i]));
5396 if (INSN_TICK (p[i]) > clock_var)
5397 fprintf (sched_dump, ":delay=%d", INSN_TICK (p[i]) - clock_var);
5398 if (sched_pressure == SCHED_PRESSURE_MODEL)
5399 fprintf (sched_dump, ":idx=%d",
5400 model_index (p[i]));
5401 if (sched_pressure != SCHED_PRESSURE_NONE)
5402 fprintf (sched_dump, ")");
5403 }
5404 fprintf (sched_dump, "\n");
5405 }
5406
5407 /* Print the ready list. Callable from debugger. */
5408 static void
5409 debug_ready_list (struct ready_list *ready)
5410 {
5411 debug_ready_list_1 (ready, NULL);
5412 }
5413
5414 /* Search INSN for REG_SAVE_NOTE notes and convert them back into insn
5415 NOTEs. This is used for NOTE_INSN_EPILOGUE_BEG, so that sched-ebb
5416 replaces the epilogue note in the correct basic block. */
5417 void
5418 reemit_notes (rtx_insn *insn)
5419 {
5420 rtx note;
5421 rtx_insn *last = insn;
5422
5423 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
5424 {
5425 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
5426 {
5427 enum insn_note note_type = (enum insn_note) INTVAL (XEXP (note, 0));
5428
5429 last = emit_note_before (note_type, last);
5430 remove_note (insn, note);
5431 }
5432 }
5433 }
5434
5435 /* Move INSN. Reemit notes if needed. Update CFG, if needed. */
5436 static void
5437 move_insn (rtx_insn *insn, rtx_insn *last, rtx nt)
5438 {
5439 if (PREV_INSN (insn) != last)
5440 {
5441 basic_block bb;
5442 rtx_insn *note;
5443 int jump_p = 0;
5444
5445 bb = BLOCK_FOR_INSN (insn);
5446
5447 /* BB_HEAD is either LABEL or NOTE. */
5448 gcc_assert (BB_HEAD (bb) != insn);
5449
5450 if (BB_END (bb) == insn)
5451 /* If this is last instruction in BB, move end marker one
5452 instruction up. */
5453 {
5454 /* Jumps are always placed at the end of basic block. */
5455 jump_p = control_flow_insn_p (insn);
5456
5457 gcc_assert (!jump_p
5458 || ((common_sched_info->sched_pass_id == SCHED_RGN_PASS)
5459 && IS_SPECULATION_BRANCHY_CHECK_P (insn))
5460 || (common_sched_info->sched_pass_id
5461 == SCHED_EBB_PASS));
5462
5463 gcc_assert (BLOCK_FOR_INSN (PREV_INSN (insn)) == bb);
5464
5465 BB_END (bb) = PREV_INSN (insn);
5466 }
5467
5468 gcc_assert (BB_END (bb) != last);
5469
5470 if (jump_p)
5471 /* We move the block note along with jump. */
5472 {
5473 gcc_assert (nt);
5474
5475 note = NEXT_INSN (insn);
5476 while (NOTE_NOT_BB_P (note) && note != nt)
5477 note = NEXT_INSN (note);
5478
5479 if (note != nt
5480 && (LABEL_P (note)
5481 || BARRIER_P (note)))
5482 note = NEXT_INSN (note);
5483
5484 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
5485 }
5486 else
5487 note = insn;
5488
5489 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (note);
5490 SET_PREV_INSN (NEXT_INSN (note)) = PREV_INSN (insn);
5491
5492 SET_NEXT_INSN (note) = NEXT_INSN (last);
5493 SET_PREV_INSN (NEXT_INSN (last)) = note;
5494
5495 SET_NEXT_INSN (last) = insn;
5496 SET_PREV_INSN (insn) = last;
5497
5498 bb = BLOCK_FOR_INSN (last);
5499
5500 if (jump_p)
5501 {
5502 fix_jump_move (insn);
5503
5504 if (BLOCK_FOR_INSN (insn) != bb)
5505 move_block_after_check (insn);
5506
5507 gcc_assert (BB_END (bb) == last);
5508 }
5509
5510 df_insn_change_bb (insn, bb);
5511
5512 /* Update BB_END, if needed. */
5513 if (BB_END (bb) == last)
5514 BB_END (bb) = insn;
5515 }
5516
5517 SCHED_GROUP_P (insn) = 0;
5518 }
5519
5520 /* Return true if scheduling INSN will finish current clock cycle. */
5521 static bool
5522 insn_finishes_cycle_p (rtx_insn *insn)
5523 {
5524 if (SCHED_GROUP_P (insn))
5525 /* After issuing INSN, rest of the sched_group will be forced to issue
5526 in order. Don't make any plans for the rest of cycle. */
5527 return true;
5528
5529 /* Finishing the block will, apparently, finish the cycle. */
5530 if (current_sched_info->insn_finishes_block_p
5531 && current_sched_info->insn_finishes_block_p (insn))
5532 return true;
5533
5534 return false;
5535 }
5536
5537 /* Helper for autopref_multipass_init. Given a SET in PAT and whether
5538 we're expecting a memory WRITE or not, check that the insn is relevant to
5539 the autoprefetcher modelling code. Return true iff that is the case.
5540 If it is relevant, record the base register of the memory op in BASE and
5541 the offset in OFFSET. */
5542
5543 static bool
5544 analyze_set_insn_for_autopref (rtx pat, bool write, rtx *base, int *offset)
5545 {
5546 if (GET_CODE (pat) != SET)
5547 return false;
5548
5549 rtx mem = write ? SET_DEST (pat) : SET_SRC (pat);
5550 if (!MEM_P (mem))
5551 return false;
5552
5553 struct address_info info;
5554 decompose_mem_address (&info, mem);
5555
5556 /* TODO: Currently only (base+const) addressing is supported. */
5557 if (info.base == NULL || !REG_P (*info.base)
5558 || (info.disp != NULL && !CONST_INT_P (*info.disp)))
5559 return false;
5560
5561 *base = *info.base;
5562 *offset = info.disp ? INTVAL (*info.disp) : 0;
5563 return true;
5564 }
5565
5566 /* Functions to model cache auto-prefetcher.
5567
5568 Some of the CPUs have cache auto-prefetcher, which /seems/ to initiate
5569 memory prefetches if it sees instructions with consequitive memory accesses
5570 in the instruction stream. Details of such hardware units are not published,
5571 so we can only guess what exactly is going on there.
5572 In the scheduler, we model abstract auto-prefetcher. If there are memory
5573 insns in the ready list (or the queue) that have same memory base, but
5574 different offsets, then we delay the insns with larger offsets until insns
5575 with smaller offsets get scheduled. If PARAM_SCHED_AUTOPREF_QUEUE_DEPTH
5576 is "1", then we look at the ready list; if it is N>1, then we also look
5577 through N-1 queue entries.
5578 If the param is N>=0, then rank_for_schedule will consider auto-prefetching
5579 among its heuristics.
5580 Param value of "-1" disables modelling of the auto-prefetcher. */
5581
5582 /* Initialize autoprefetcher model data for INSN. */
5583 static void
5584 autopref_multipass_init (const rtx_insn *insn, int write)
5585 {
5586 autopref_multipass_data_t data = &INSN_AUTOPREF_MULTIPASS_DATA (insn)[write];
5587
5588 gcc_assert (data->status == AUTOPREF_MULTIPASS_DATA_UNINITIALIZED);
5589 data->base = NULL_RTX;
5590 data->min_offset = 0;
5591 data->max_offset = 0;
5592 data->multi_mem_insn_p = false;
5593 /* Set insn entry initialized, but not relevant for auto-prefetcher. */
5594 data->status = AUTOPREF_MULTIPASS_DATA_IRRELEVANT;
5595
5596 rtx pat = PATTERN (insn);
5597
5598 /* We have a multi-set insn like a load-multiple or store-multiple.
5599 We care about these as long as all the memory ops inside the PARALLEL
5600 have the same base register. We care about the minimum and maximum
5601 offsets from that base but don't check for the order of those offsets
5602 within the PARALLEL insn itself. */
5603 if (GET_CODE (pat) == PARALLEL)
5604 {
5605 int n_elems = XVECLEN (pat, 0);
5606
5607 int i = 0;
5608 rtx prev_base = NULL_RTX;
5609 int min_offset;
5610 int max_offset;
5611
5612 for (i = 0; i < n_elems; i++)
5613 {
5614 rtx set = XVECEXP (pat, 0, i);
5615 if (GET_CODE (set) != SET)
5616 return;
5617
5618 rtx base = NULL_RTX;
5619 int offset = 0;
5620 if (!analyze_set_insn_for_autopref (set, write, &base, &offset))
5621 return;
5622
5623 if (i == 0)
5624 {
5625 prev_base = base;
5626 min_offset = offset;
5627 max_offset = offset;
5628 }
5629 /* Ensure that all memory operations in the PARALLEL use the same
5630 base register. */
5631 else if (REGNO (base) != REGNO (prev_base))
5632 return;
5633 else
5634 {
5635 min_offset = MIN (min_offset, offset);
5636 max_offset = MAX (max_offset, offset);
5637 }
5638 }
5639
5640 /* If we reached here then we have a valid PARALLEL of multiple memory
5641 ops with prev_base as the base and min_offset and max_offset
5642 containing the offsets range. */
5643 gcc_assert (prev_base);
5644 data->base = prev_base;
5645 data->min_offset = min_offset;
5646 data->max_offset = max_offset;
5647 data->multi_mem_insn_p = true;
5648 data->status = AUTOPREF_MULTIPASS_DATA_NORMAL;
5649
5650 return;
5651 }
5652
5653 /* Otherwise this is a single set memory operation. */
5654 rtx set = single_set (insn);
5655 if (set == NULL_RTX)
5656 return;
5657
5658 if (!analyze_set_insn_for_autopref (set, write, &data->base,
5659 &data->min_offset))
5660 return;
5661
5662 /* This insn is relevant for the auto-prefetcher.
5663 The base and offset fields will have been filled in the
5664 analyze_set_insn_for_autopref call above. */
5665 data->status = AUTOPREF_MULTIPASS_DATA_NORMAL;
5666 }
5667
5668
5669 /* Helper for autopref_rank_for_schedule. Given the data of two
5670 insns relevant to the auto-prefetcher modelling code DATA1 and DATA2
5671 return their comparison result. Return 0 if there is no sensible
5672 ranking order for the two insns. */
5673
5674 static int
5675 autopref_rank_data (autopref_multipass_data_t data1,
5676 autopref_multipass_data_t data2)
5677 {
5678 /* Simple case when both insns are simple single memory ops. */
5679 if (!data1->multi_mem_insn_p && !data2->multi_mem_insn_p)
5680 return data1->min_offset - data2->min_offset;
5681
5682 /* Two load/store multiple insns. Return 0 if the offset ranges
5683 overlap and the difference between the minimum offsets otherwise. */
5684 else if (data1->multi_mem_insn_p && data2->multi_mem_insn_p)
5685 {
5686 int min1 = data1->min_offset;
5687 int max1 = data1->max_offset;
5688 int min2 = data2->min_offset;
5689 int max2 = data2->max_offset;
5690
5691 if (max1 < min2 || min1 > max2)
5692 return min1 - min2;
5693 else
5694 return 0;
5695 }
5696
5697 /* The other two cases is a pair of a load/store multiple and
5698 a simple memory op. Return 0 if the single op's offset is within the
5699 range of the multi-op insn and the difference between the single offset
5700 and the minimum offset of the multi-set insn otherwise. */
5701 else if (data1->multi_mem_insn_p && !data2->multi_mem_insn_p)
5702 {
5703 int max1 = data1->max_offset;
5704 int min1 = data1->min_offset;
5705
5706 if (data2->min_offset >= min1
5707 && data2->min_offset <= max1)
5708 return 0;
5709 else
5710 return min1 - data2->min_offset;
5711 }
5712 else
5713 {
5714 int max2 = data2->max_offset;
5715 int min2 = data2->min_offset;
5716
5717 if (data1->min_offset >= min2
5718 && data1->min_offset <= max2)
5719 return 0;
5720 else
5721 return data1->min_offset - min2;
5722 }
5723 }
5724
5725 /* Helper function for rank_for_schedule sorting. */
5726 static int
5727 autopref_rank_for_schedule (const rtx_insn *insn1, const rtx_insn *insn2)
5728 {
5729 for (int write = 0; write < 2; ++write)
5730 {
5731 autopref_multipass_data_t data1
5732 = &INSN_AUTOPREF_MULTIPASS_DATA (insn1)[write];
5733 autopref_multipass_data_t data2
5734 = &INSN_AUTOPREF_MULTIPASS_DATA (insn2)[write];
5735
5736 if (data1->status == AUTOPREF_MULTIPASS_DATA_UNINITIALIZED)
5737 autopref_multipass_init (insn1, write);
5738 if (data1->status == AUTOPREF_MULTIPASS_DATA_IRRELEVANT)
5739 continue;
5740
5741 if (data2->status == AUTOPREF_MULTIPASS_DATA_UNINITIALIZED)
5742 autopref_multipass_init (insn2, write);
5743 if (data2->status == AUTOPREF_MULTIPASS_DATA_IRRELEVANT)
5744 continue;
5745
5746 if (!rtx_equal_p (data1->base, data2->base))
5747 continue;
5748
5749 return autopref_rank_data (data1, data2);
5750 }
5751
5752 return 0;
5753 }
5754
5755 /* True if header of debug dump was printed. */
5756 static bool autopref_multipass_dfa_lookahead_guard_started_dump_p;
5757
5758 /* Helper for autopref_multipass_dfa_lookahead_guard.
5759 Return "1" if INSN1 should be delayed in favor of INSN2. */
5760 static int
5761 autopref_multipass_dfa_lookahead_guard_1 (const rtx_insn *insn1,
5762 const rtx_insn *insn2, int write)
5763 {
5764 autopref_multipass_data_t data1
5765 = &INSN_AUTOPREF_MULTIPASS_DATA (insn1)[write];
5766 autopref_multipass_data_t data2
5767 = &INSN_AUTOPREF_MULTIPASS_DATA (insn2)[write];
5768
5769 if (data2->status == AUTOPREF_MULTIPASS_DATA_UNINITIALIZED)
5770 autopref_multipass_init (insn2, write);
5771 if (data2->status == AUTOPREF_MULTIPASS_DATA_IRRELEVANT)
5772 return 0;
5773
5774 if (rtx_equal_p (data1->base, data2->base)
5775 && autopref_rank_data (data1, data2) > 0)
5776 {
5777 if (sched_verbose >= 2)
5778 {
5779 if (!autopref_multipass_dfa_lookahead_guard_started_dump_p)
5780 {
5781 fprintf (sched_dump,
5782 ";;\t\tnot trying in max_issue due to autoprefetch "
5783 "model: ");
5784 autopref_multipass_dfa_lookahead_guard_started_dump_p = true;
5785 }
5786
5787 fprintf (sched_dump, " %d(%d)", INSN_UID (insn1), INSN_UID (insn2));
5788 }
5789
5790 return 1;
5791 }
5792
5793 return 0;
5794 }
5795
5796 /* General note:
5797
5798 We could have also hooked autoprefetcher model into
5799 first_cycle_multipass_backtrack / first_cycle_multipass_issue hooks
5800 to enable intelligent selection of "[r1+0]=r2; [r1+4]=r3" on the same cycle
5801 (e.g., once "[r1+0]=r2" is issued in max_issue(), "[r1+4]=r3" gets
5802 unblocked). We don't bother about this yet because target of interest
5803 (ARM Cortex-A15) can issue only 1 memory operation per cycle. */
5804
5805 /* Implementation of first_cycle_multipass_dfa_lookahead_guard hook.
5806 Return "1" if INSN1 should not be considered in max_issue due to
5807 auto-prefetcher considerations. */
5808 int
5809 autopref_multipass_dfa_lookahead_guard (rtx_insn *insn1, int ready_index)
5810 {
5811 int r = 0;
5812
5813 if (PARAM_VALUE (PARAM_SCHED_AUTOPREF_QUEUE_DEPTH) <= 0)
5814 return 0;
5815
5816 if (sched_verbose >= 2 && ready_index == 0)
5817 autopref_multipass_dfa_lookahead_guard_started_dump_p = false;
5818
5819 for (int write = 0; write < 2; ++write)
5820 {
5821 autopref_multipass_data_t data1
5822 = &INSN_AUTOPREF_MULTIPASS_DATA (insn1)[write];
5823
5824 if (data1->status == AUTOPREF_MULTIPASS_DATA_UNINITIALIZED)
5825 autopref_multipass_init (insn1, write);
5826 if (data1->status == AUTOPREF_MULTIPASS_DATA_IRRELEVANT)
5827 continue;
5828
5829 if (ready_index == 0
5830 && data1->status == AUTOPREF_MULTIPASS_DATA_DONT_DELAY)
5831 /* We allow only a single delay on priviledged instructions.
5832 Doing otherwise would cause infinite loop. */
5833 {
5834 if (sched_verbose >= 2)
5835 {
5836 if (!autopref_multipass_dfa_lookahead_guard_started_dump_p)
5837 {
5838 fprintf (sched_dump,
5839 ";;\t\tnot trying in max_issue due to autoprefetch "
5840 "model: ");
5841 autopref_multipass_dfa_lookahead_guard_started_dump_p = true;
5842 }
5843
5844 fprintf (sched_dump, " *%d*", INSN_UID (insn1));
5845 }
5846 continue;
5847 }
5848
5849 for (int i2 = 0; i2 < ready.n_ready; ++i2)
5850 {
5851 rtx_insn *insn2 = get_ready_element (i2);
5852 if (insn1 == insn2)
5853 continue;
5854 r = autopref_multipass_dfa_lookahead_guard_1 (insn1, insn2, write);
5855 if (r)
5856 {
5857 if (ready_index == 0)
5858 {
5859 r = -1;
5860 data1->status = AUTOPREF_MULTIPASS_DATA_DONT_DELAY;
5861 }
5862 goto finish;
5863 }
5864 }
5865
5866 if (PARAM_VALUE (PARAM_SCHED_AUTOPREF_QUEUE_DEPTH) == 1)
5867 continue;
5868
5869 /* Everything from the current queue slot should have been moved to
5870 the ready list. */
5871 gcc_assert (insn_queue[NEXT_Q_AFTER (q_ptr, 0)] == NULL_RTX);
5872
5873 int n_stalls = PARAM_VALUE (PARAM_SCHED_AUTOPREF_QUEUE_DEPTH) - 1;
5874 if (n_stalls > max_insn_queue_index)
5875 n_stalls = max_insn_queue_index;
5876
5877 for (int stalls = 1; stalls <= n_stalls; ++stalls)
5878 {
5879 for (rtx_insn_list *link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)];
5880 link != NULL_RTX;
5881 link = link->next ())
5882 {
5883 rtx_insn *insn2 = link->insn ();
5884 r = autopref_multipass_dfa_lookahead_guard_1 (insn1, insn2,
5885 write);
5886 if (r)
5887 {
5888 /* Queue INSN1 until INSN2 can issue. */
5889 r = -stalls;
5890 if (ready_index == 0)
5891 data1->status = AUTOPREF_MULTIPASS_DATA_DONT_DELAY;
5892 goto finish;
5893 }
5894 }
5895 }
5896 }
5897
5898 finish:
5899 if (sched_verbose >= 2
5900 && autopref_multipass_dfa_lookahead_guard_started_dump_p
5901 && (ready_index == ready.n_ready - 1 || r < 0))
5902 /* This does not /always/ trigger. We don't output EOL if the last
5903 insn is not recognized (INSN_CODE < 0) and lookahead_guard is not
5904 called. We can live with this. */
5905 fprintf (sched_dump, "\n");
5906
5907 return r;
5908 }
5909
5910 /* Define type for target data used in multipass scheduling. */
5911 #ifndef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DATA_T
5912 # define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DATA_T int
5913 #endif
5914 typedef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DATA_T first_cycle_multipass_data_t;
5915
5916 /* The following structure describe an entry of the stack of choices. */
5917 struct choice_entry
5918 {
5919 /* Ordinal number of the issued insn in the ready queue. */
5920 int index;
5921 /* The number of the rest insns whose issues we should try. */
5922 int rest;
5923 /* The number of issued essential insns. */
5924 int n;
5925 /* State after issuing the insn. */
5926 state_t state;
5927 /* Target-specific data. */
5928 first_cycle_multipass_data_t target_data;
5929 };
5930
5931 /* The following array is used to implement a stack of choices used in
5932 function max_issue. */
5933 static struct choice_entry *choice_stack;
5934
5935 /* This holds the value of the target dfa_lookahead hook. */
5936 int dfa_lookahead;
5937
5938 /* The following variable value is maximal number of tries of issuing
5939 insns for the first cycle multipass insn scheduling. We define
5940 this value as constant*(DFA_LOOKAHEAD**ISSUE_RATE). We would not
5941 need this constraint if all real insns (with non-negative codes)
5942 had reservations because in this case the algorithm complexity is
5943 O(DFA_LOOKAHEAD**ISSUE_RATE). Unfortunately, the dfa descriptions
5944 might be incomplete and such insn might occur. For such
5945 descriptions, the complexity of algorithm (without the constraint)
5946 could achieve DFA_LOOKAHEAD ** N , where N is the queue length. */
5947 static int max_lookahead_tries;
5948
5949 /* The following function returns maximal (or close to maximal) number
5950 of insns which can be issued on the same cycle and one of which
5951 insns is insns with the best rank (the first insn in READY). To
5952 make this function tries different samples of ready insns. READY
5953 is current queue `ready'. Global array READY_TRY reflects what
5954 insns are already issued in this try. The function stops immediately,
5955 if it reached the such a solution, that all instruction can be issued.
5956 INDEX will contain index of the best insn in READY. The following
5957 function is used only for first cycle multipass scheduling.
5958
5959 PRIVILEGED_N >= 0
5960
5961 This function expects recognized insns only. All USEs,
5962 CLOBBERs, etc must be filtered elsewhere. */
5963 int
5964 max_issue (struct ready_list *ready, int privileged_n, state_t state,
5965 bool first_cycle_insn_p, int *index)
5966 {
5967 int n, i, all, n_ready, best, delay, tries_num;
5968 int more_issue;
5969 struct choice_entry *top;
5970 rtx_insn *insn;
5971
5972 if (sched_fusion)
5973 return 0;
5974
5975 n_ready = ready->n_ready;
5976 gcc_assert (dfa_lookahead >= 1 && privileged_n >= 0
5977 && privileged_n <= n_ready);
5978
5979 /* Init MAX_LOOKAHEAD_TRIES. */
5980 if (max_lookahead_tries == 0)
5981 {
5982 max_lookahead_tries = 100;
5983 for (i = 0; i < issue_rate; i++)
5984 max_lookahead_tries *= dfa_lookahead;
5985 }
5986
5987 /* Init max_points. */
5988 more_issue = issue_rate - cycle_issued_insns;
5989 gcc_assert (more_issue >= 0);
5990
5991 /* The number of the issued insns in the best solution. */
5992 best = 0;
5993
5994 top = choice_stack;
5995
5996 /* Set initial state of the search. */
5997 memcpy (top->state, state, dfa_state_size);
5998 top->rest = dfa_lookahead;
5999 top->n = 0;
6000 if (targetm.sched.first_cycle_multipass_begin)
6001 targetm.sched.first_cycle_multipass_begin (&top->target_data,
6002 ready_try, n_ready,
6003 first_cycle_insn_p);
6004
6005 /* Count the number of the insns to search among. */
6006 for (all = i = 0; i < n_ready; i++)
6007 if (!ready_try [i])
6008 all++;
6009
6010 if (sched_verbose >= 2)
6011 {
6012 fprintf (sched_dump, ";;\t\tmax_issue among %d insns:", all);
6013 debug_ready_list_1 (ready, ready_try);
6014 }
6015
6016 /* I is the index of the insn to try next. */
6017 i = 0;
6018 tries_num = 0;
6019 for (;;)
6020 {
6021 if (/* If we've reached a dead end or searched enough of what we have
6022 been asked... */
6023 top->rest == 0
6024 /* or have nothing else to try... */
6025 || i >= n_ready
6026 /* or should not issue more. */
6027 || top->n >= more_issue)
6028 {
6029 /* ??? (... || i == n_ready). */
6030 gcc_assert (i <= n_ready);
6031
6032 /* We should not issue more than issue_rate instructions. */
6033 gcc_assert (top->n <= more_issue);
6034
6035 if (top == choice_stack)
6036 break;
6037
6038 if (best < top - choice_stack)
6039 {
6040 if (privileged_n)
6041 {
6042 n = privileged_n;
6043 /* Try to find issued privileged insn. */
6044 while (n && !ready_try[--n])
6045 ;
6046 }
6047
6048 if (/* If all insns are equally good... */
6049 privileged_n == 0
6050 /* Or a privileged insn will be issued. */
6051 || ready_try[n])
6052 /* Then we have a solution. */
6053 {
6054 best = top - choice_stack;
6055 /* This is the index of the insn issued first in this
6056 solution. */
6057 *index = choice_stack [1].index;
6058 if (top->n == more_issue || best == all)
6059 break;
6060 }
6061 }
6062
6063 /* Set ready-list index to point to the last insn
6064 ('i++' below will advance it to the next insn). */
6065 i = top->index;
6066
6067 /* Backtrack. */
6068 ready_try [i] = 0;
6069
6070 if (targetm.sched.first_cycle_multipass_backtrack)
6071 targetm.sched.first_cycle_multipass_backtrack (&top->target_data,
6072 ready_try, n_ready);
6073
6074 top--;
6075 memcpy (state, top->state, dfa_state_size);
6076 }
6077 else if (!ready_try [i])
6078 {
6079 tries_num++;
6080 if (tries_num > max_lookahead_tries)
6081 break;
6082 insn = ready_element (ready, i);
6083 delay = state_transition (state, insn);
6084 if (delay < 0)
6085 {
6086 if (state_dead_lock_p (state)
6087 || insn_finishes_cycle_p (insn))
6088 /* We won't issue any more instructions in the next
6089 choice_state. */
6090 top->rest = 0;
6091 else
6092 top->rest--;
6093
6094 n = top->n;
6095 if (memcmp (top->state, state, dfa_state_size) != 0)
6096 n++;
6097
6098 /* Advance to the next choice_entry. */
6099 top++;
6100 /* Initialize it. */
6101 top->rest = dfa_lookahead;
6102 top->index = i;
6103 top->n = n;
6104 memcpy (top->state, state, dfa_state_size);
6105 ready_try [i] = 1;
6106
6107 if (targetm.sched.first_cycle_multipass_issue)
6108 targetm.sched.first_cycle_multipass_issue (&top->target_data,
6109 ready_try, n_ready,
6110 insn,
6111 &((top - 1)
6112 ->target_data));
6113
6114 i = -1;
6115 }
6116 }
6117
6118 /* Increase ready-list index. */
6119 i++;
6120 }
6121
6122 if (targetm.sched.first_cycle_multipass_end)
6123 targetm.sched.first_cycle_multipass_end (best != 0
6124 ? &choice_stack[1].target_data
6125 : NULL);
6126
6127 /* Restore the original state of the DFA. */
6128 memcpy (state, choice_stack->state, dfa_state_size);
6129
6130 return best;
6131 }
6132
6133 /* The following function chooses insn from READY and modifies
6134 READY. The following function is used only for first
6135 cycle multipass scheduling.
6136 Return:
6137 -1 if cycle should be advanced,
6138 0 if INSN_PTR is set to point to the desirable insn,
6139 1 if choose_ready () should be restarted without advancing the cycle. */
6140 static int
6141 choose_ready (struct ready_list *ready, bool first_cycle_insn_p,
6142 rtx_insn **insn_ptr)
6143 {
6144 if (dbg_cnt (sched_insn) == false)
6145 {
6146 if (nonscheduled_insns_begin == NULL_RTX)
6147 nonscheduled_insns_begin = current_sched_info->prev_head;
6148
6149 rtx_insn *insn = first_nonscheduled_insn ();
6150
6151 if (QUEUE_INDEX (insn) == QUEUE_READY)
6152 /* INSN is in the ready_list. */
6153 {
6154 ready_remove_insn (insn);
6155 *insn_ptr = insn;
6156 return 0;
6157 }
6158
6159 /* INSN is in the queue. Advance cycle to move it to the ready list. */
6160 gcc_assert (QUEUE_INDEX (insn) >= 0);
6161 return -1;
6162 }
6163
6164 if (dfa_lookahead <= 0 || SCHED_GROUP_P (ready_element (ready, 0))
6165 || DEBUG_INSN_P (ready_element (ready, 0)))
6166 {
6167 if (targetm.sched.dispatch (NULL, IS_DISPATCH_ON))
6168 *insn_ptr = ready_remove_first_dispatch (ready);
6169 else
6170 *insn_ptr = ready_remove_first (ready);
6171
6172 return 0;
6173 }
6174 else
6175 {
6176 /* Try to choose the best insn. */
6177 int index = 0, i;
6178 rtx_insn *insn;
6179
6180 insn = ready_element (ready, 0);
6181 if (INSN_CODE (insn) < 0)
6182 {
6183 *insn_ptr = ready_remove_first (ready);
6184 return 0;
6185 }
6186
6187 /* Filter the search space. */
6188 for (i = 0; i < ready->n_ready; i++)
6189 {
6190 ready_try[i] = 0;
6191
6192 insn = ready_element (ready, i);
6193
6194 /* If this insn is recognizable we should have already
6195 recognized it earlier.
6196 ??? Not very clear where this is supposed to be done.
6197 See dep_cost_1. */
6198 gcc_checking_assert (INSN_CODE (insn) >= 0
6199 || recog_memoized (insn) < 0);
6200 if (INSN_CODE (insn) < 0)
6201 {
6202 /* Non-recognized insns at position 0 are handled above. */
6203 gcc_assert (i > 0);
6204 ready_try[i] = 1;
6205 continue;
6206 }
6207
6208 if (targetm.sched.first_cycle_multipass_dfa_lookahead_guard)
6209 {
6210 ready_try[i]
6211 = (targetm.sched.first_cycle_multipass_dfa_lookahead_guard
6212 (insn, i));
6213
6214 if (ready_try[i] < 0)
6215 /* Queue instruction for several cycles.
6216 We need to restart choose_ready as we have changed
6217 the ready list. */
6218 {
6219 change_queue_index (insn, -ready_try[i]);
6220 return 1;
6221 }
6222
6223 /* Make sure that we didn't end up with 0'th insn filtered out.
6224 Don't be tempted to make life easier for backends and just
6225 requeue 0'th insn if (ready_try[0] == 0) and restart
6226 choose_ready. Backends should be very considerate about
6227 requeueing instructions -- especially the highest priority
6228 one at position 0. */
6229 gcc_assert (ready_try[i] == 0 || i > 0);
6230 if (ready_try[i])
6231 continue;
6232 }
6233
6234 gcc_assert (ready_try[i] == 0);
6235 /* INSN made it through the scrutiny of filters! */
6236 }
6237
6238 if (max_issue (ready, 1, curr_state, first_cycle_insn_p, &index) == 0)
6239 {
6240 *insn_ptr = ready_remove_first (ready);
6241 if (sched_verbose >= 4)
6242 fprintf (sched_dump, ";;\t\tChosen insn (but can't issue) : %s \n",
6243 (*current_sched_info->print_insn) (*insn_ptr, 0));
6244 return 0;
6245 }
6246 else
6247 {
6248 if (sched_verbose >= 4)
6249 fprintf (sched_dump, ";;\t\tChosen insn : %s\n",
6250 (*current_sched_info->print_insn)
6251 (ready_element (ready, index), 0));
6252
6253 *insn_ptr = ready_remove (ready, index);
6254 return 0;
6255 }
6256 }
6257 }
6258
6259 /* This function is called when we have successfully scheduled a
6260 block. It uses the schedule stored in the scheduled_insns vector
6261 to rearrange the RTL. PREV_HEAD is used as the anchor to which we
6262 append the scheduled insns; TAIL is the insn after the scheduled
6263 block. TARGET_BB is the argument passed to schedule_block. */
6264
6265 static void
6266 commit_schedule (rtx_insn *prev_head, rtx_insn *tail, basic_block *target_bb)
6267 {
6268 unsigned int i;
6269 rtx_insn *insn;
6270
6271 last_scheduled_insn = prev_head;
6272 for (i = 0;
6273 scheduled_insns.iterate (i, &insn);
6274 i++)
6275 {
6276 if (control_flow_insn_p (last_scheduled_insn)
6277 || current_sched_info->advance_target_bb (*target_bb, insn))
6278 {
6279 *target_bb = current_sched_info->advance_target_bb (*target_bb, 0);
6280
6281 if (sched_verbose)
6282 {
6283 rtx_insn *x;
6284
6285 x = next_real_insn (last_scheduled_insn);
6286 gcc_assert (x);
6287 dump_new_block_header (1, *target_bb, x, tail);
6288 }
6289
6290 last_scheduled_insn = bb_note (*target_bb);
6291 }
6292
6293 if (current_sched_info->begin_move_insn)
6294 (*current_sched_info->begin_move_insn) (insn, last_scheduled_insn);
6295 move_insn (insn, last_scheduled_insn,
6296 current_sched_info->next_tail);
6297 if (!DEBUG_INSN_P (insn))
6298 reemit_notes (insn);
6299 last_scheduled_insn = insn;
6300 }
6301
6302 scheduled_insns.truncate (0);
6303 }
6304
6305 /* Examine all insns on the ready list and queue those which can't be
6306 issued in this cycle. TEMP_STATE is temporary scheduler state we
6307 can use as scratch space. If FIRST_CYCLE_INSN_P is true, no insns
6308 have been issued for the current cycle, which means it is valid to
6309 issue an asm statement.
6310
6311 If SHADOWS_ONLY_P is true, we eliminate all real insns and only
6312 leave those for which SHADOW_P is true. If MODULO_EPILOGUE is true,
6313 we only leave insns which have an INSN_EXACT_TICK. */
6314
6315 static void
6316 prune_ready_list (state_t temp_state, bool first_cycle_insn_p,
6317 bool shadows_only_p, bool modulo_epilogue_p)
6318 {
6319 int i, pass;
6320 bool sched_group_found = false;
6321 int min_cost_group = 1;
6322
6323 if (sched_fusion)
6324 return;
6325
6326 for (i = 0; i < ready.n_ready; i++)
6327 {
6328 rtx_insn *insn = ready_element (&ready, i);
6329 if (SCHED_GROUP_P (insn))
6330 {
6331 sched_group_found = true;
6332 break;
6333 }
6334 }
6335
6336 /* Make two passes if there's a SCHED_GROUP_P insn; make sure to handle
6337 such an insn first and note its cost, then schedule all other insns
6338 for one cycle later. */
6339 for (pass = sched_group_found ? 0 : 1; pass < 2; )
6340 {
6341 int n = ready.n_ready;
6342 for (i = 0; i < n; i++)
6343 {
6344 rtx_insn *insn = ready_element (&ready, i);
6345 int cost = 0;
6346 const char *reason = "resource conflict";
6347
6348 if (DEBUG_INSN_P (insn))
6349 continue;
6350
6351 if (sched_group_found && !SCHED_GROUP_P (insn))
6352 {
6353 if (pass == 0)
6354 continue;
6355 cost = min_cost_group;
6356 reason = "not in sched group";
6357 }
6358 else if (modulo_epilogue_p
6359 && INSN_EXACT_TICK (insn) == INVALID_TICK)
6360 {
6361 cost = max_insn_queue_index;
6362 reason = "not an epilogue insn";
6363 }
6364 else if (shadows_only_p && !SHADOW_P (insn))
6365 {
6366 cost = 1;
6367 reason = "not a shadow";
6368 }
6369 else if (recog_memoized (insn) < 0)
6370 {
6371 if (!first_cycle_insn_p
6372 && (GET_CODE (PATTERN (insn)) == ASM_INPUT
6373 || asm_noperands (PATTERN (insn)) >= 0))
6374 cost = 1;
6375 reason = "asm";
6376 }
6377 else if (sched_pressure != SCHED_PRESSURE_NONE)
6378 {
6379 if (sched_pressure == SCHED_PRESSURE_MODEL
6380 && INSN_TICK (insn) <= clock_var)
6381 {
6382 memcpy (temp_state, curr_state, dfa_state_size);
6383 if (state_transition (temp_state, insn) >= 0)
6384 INSN_TICK (insn) = clock_var + 1;
6385 }
6386 cost = 0;
6387 }
6388 else
6389 {
6390 int delay_cost = 0;
6391
6392 if (delay_htab)
6393 {
6394 struct delay_pair *delay_entry;
6395 delay_entry
6396 = delay_htab->find_with_hash (insn,
6397 htab_hash_pointer (insn));
6398 while (delay_entry && delay_cost == 0)
6399 {
6400 delay_cost = estimate_shadow_tick (delay_entry);
6401 if (delay_cost > max_insn_queue_index)
6402 delay_cost = max_insn_queue_index;
6403 delay_entry = delay_entry->next_same_i1;
6404 }
6405 }
6406
6407 memcpy (temp_state, curr_state, dfa_state_size);
6408 cost = state_transition (temp_state, insn);
6409 if (cost < 0)
6410 cost = 0;
6411 else if (cost == 0)
6412 cost = 1;
6413 if (cost < delay_cost)
6414 {
6415 cost = delay_cost;
6416 reason = "shadow tick";
6417 }
6418 }
6419 if (cost >= 1)
6420 {
6421 if (SCHED_GROUP_P (insn) && cost > min_cost_group)
6422 min_cost_group = cost;
6423 ready_remove (&ready, i);
6424 /* Normally we'd want to queue INSN for COST cycles. However,
6425 if SCHED_GROUP_P is set, then we must ensure that nothing
6426 else comes between INSN and its predecessor. If there is
6427 some other insn ready to fire on the next cycle, then that
6428 invariant would be broken.
6429
6430 So when SCHED_GROUP_P is set, just queue this insn for a
6431 single cycle. */
6432 queue_insn (insn, SCHED_GROUP_P (insn) ? 1 : cost, reason);
6433 if (i + 1 < n)
6434 break;
6435 }
6436 }
6437 if (i == n)
6438 pass++;
6439 }
6440 }
6441
6442 /* Called when we detect that the schedule is impossible. We examine the
6443 backtrack queue to find the earliest insn that caused this condition. */
6444
6445 static struct haifa_saved_data *
6446 verify_shadows (void)
6447 {
6448 struct haifa_saved_data *save, *earliest_fail = NULL;
6449 for (save = backtrack_queue; save; save = save->next)
6450 {
6451 int t;
6452 struct delay_pair *pair = save->delay_pair;
6453 rtx_insn *i1 = pair->i1;
6454
6455 for (; pair; pair = pair->next_same_i1)
6456 {
6457 rtx_insn *i2 = pair->i2;
6458
6459 if (QUEUE_INDEX (i2) == QUEUE_SCHEDULED)
6460 continue;
6461
6462 t = INSN_TICK (i1) + pair_delay (pair);
6463 if (t < clock_var)
6464 {
6465 if (sched_verbose >= 2)
6466 fprintf (sched_dump,
6467 ";;\t\tfailed delay requirements for %d/%d (%d->%d)"
6468 ", not ready\n",
6469 INSN_UID (pair->i1), INSN_UID (pair->i2),
6470 INSN_TICK (pair->i1), INSN_EXACT_TICK (pair->i2));
6471 earliest_fail = save;
6472 break;
6473 }
6474 if (QUEUE_INDEX (i2) >= 0)
6475 {
6476 int queued_for = INSN_TICK (i2);
6477
6478 if (t < queued_for)
6479 {
6480 if (sched_verbose >= 2)
6481 fprintf (sched_dump,
6482 ";;\t\tfailed delay requirements for %d/%d"
6483 " (%d->%d), queued too late\n",
6484 INSN_UID (pair->i1), INSN_UID (pair->i2),
6485 INSN_TICK (pair->i1), INSN_EXACT_TICK (pair->i2));
6486 earliest_fail = save;
6487 break;
6488 }
6489 }
6490 }
6491 }
6492
6493 return earliest_fail;
6494 }
6495
6496 /* Print instructions together with useful scheduling information between
6497 HEAD and TAIL (inclusive). */
6498 static void
6499 dump_insn_stream (rtx_insn *head, rtx_insn *tail)
6500 {
6501 fprintf (sched_dump, ";;\t| insn | prio |\n");
6502
6503 rtx_insn *next_tail = NEXT_INSN (tail);
6504 for (rtx_insn *insn = head; insn != next_tail; insn = NEXT_INSN (insn))
6505 {
6506 int priority = NOTE_P (insn) ? 0 : INSN_PRIORITY (insn);
6507 const char *pattern = (NOTE_P (insn)
6508 ? "note"
6509 : str_pattern_slim (PATTERN (insn)));
6510
6511 fprintf (sched_dump, ";;\t| %4d | %4d | %-30s ",
6512 INSN_UID (insn), priority, pattern);
6513
6514 if (sched_verbose >= 4)
6515 {
6516 if (NOTE_P (insn) || recog_memoized (insn) < 0)
6517 fprintf (sched_dump, "nothing");
6518 else
6519 print_reservation (sched_dump, insn);
6520 }
6521 fprintf (sched_dump, "\n");
6522 }
6523 }
6524
6525 /* Use forward list scheduling to rearrange insns of block pointed to by
6526 TARGET_BB, possibly bringing insns from subsequent blocks in the same
6527 region. */
6528
6529 bool
6530 schedule_block (basic_block *target_bb, state_t init_state)
6531 {
6532 int i;
6533 bool success = modulo_ii == 0;
6534 struct sched_block_state ls;
6535 state_t temp_state = NULL; /* It is used for multipass scheduling. */
6536 int sort_p, advance, start_clock_var;
6537
6538 /* Head/tail info for this block. */
6539 rtx_insn *prev_head = current_sched_info->prev_head;
6540 rtx_insn *next_tail = current_sched_info->next_tail;
6541 rtx_insn *head = NEXT_INSN (prev_head);
6542 rtx_insn *tail = PREV_INSN (next_tail);
6543
6544 if ((current_sched_info->flags & DONT_BREAK_DEPENDENCIES) == 0
6545 && sched_pressure != SCHED_PRESSURE_MODEL && !sched_fusion)
6546 find_modifiable_mems (head, tail);
6547
6548 /* We used to have code to avoid getting parameters moved from hard
6549 argument registers into pseudos.
6550
6551 However, it was removed when it proved to be of marginal benefit
6552 and caused problems because schedule_block and compute_forward_dependences
6553 had different notions of what the "head" insn was. */
6554
6555 gcc_assert (head != tail || INSN_P (head));
6556
6557 haifa_recovery_bb_recently_added_p = false;
6558
6559 backtrack_queue = NULL;
6560
6561 /* Debug info. */
6562 if (sched_verbose)
6563 {
6564 dump_new_block_header (0, *target_bb, head, tail);
6565
6566 if (sched_verbose >= 2)
6567 {
6568 dump_insn_stream (head, tail);
6569 memset (&rank_for_schedule_stats, 0,
6570 sizeof (rank_for_schedule_stats));
6571 }
6572 }
6573
6574 if (init_state == NULL)
6575 state_reset (curr_state);
6576 else
6577 memcpy (curr_state, init_state, dfa_state_size);
6578
6579 /* Clear the ready list. */
6580 ready.first = ready.veclen - 1;
6581 ready.n_ready = 0;
6582 ready.n_debug = 0;
6583
6584 /* It is used for first cycle multipass scheduling. */
6585 temp_state = alloca (dfa_state_size);
6586
6587 if (targetm.sched.init)
6588 targetm.sched.init (sched_dump, sched_verbose, ready.veclen);
6589
6590 /* We start inserting insns after PREV_HEAD. */
6591 last_scheduled_insn = prev_head;
6592 last_nondebug_scheduled_insn = NULL;
6593 nonscheduled_insns_begin = NULL;
6594
6595 gcc_assert ((NOTE_P (last_scheduled_insn)
6596 || DEBUG_INSN_P (last_scheduled_insn))
6597 && BLOCK_FOR_INSN (last_scheduled_insn) == *target_bb);
6598
6599 /* Initialize INSN_QUEUE. Q_SIZE is the total number of insns in the
6600 queue. */
6601 q_ptr = 0;
6602 q_size = 0;
6603
6604 insn_queue = XALLOCAVEC (rtx_insn_list *, max_insn_queue_index + 1);
6605 memset (insn_queue, 0, (max_insn_queue_index + 1) * sizeof (rtx));
6606
6607 /* Start just before the beginning of time. */
6608 clock_var = -1;
6609
6610 /* We need queue and ready lists and clock_var be initialized
6611 in try_ready () (which is called through init_ready_list ()). */
6612 (*current_sched_info->init_ready_list) ();
6613
6614 if (sched_pressure)
6615 sched_pressure_start_bb (*target_bb);
6616
6617 /* The algorithm is O(n^2) in the number of ready insns at any given
6618 time in the worst case. Before reload we are more likely to have
6619 big lists so truncate them to a reasonable size. */
6620 if (!reload_completed
6621 && ready.n_ready - ready.n_debug > MAX_SCHED_READY_INSNS)
6622 {
6623 ready_sort_debug (&ready);
6624 ready_sort_real (&ready);
6625
6626 /* Find first free-standing insn past MAX_SCHED_READY_INSNS.
6627 If there are debug insns, we know they're first. */
6628 for (i = MAX_SCHED_READY_INSNS + ready.n_debug; i < ready.n_ready; i++)
6629 if (!SCHED_GROUP_P (ready_element (&ready, i)))
6630 break;
6631
6632 if (sched_verbose >= 2)
6633 {
6634 fprintf (sched_dump,
6635 ";;\t\tReady list on entry: %d insns: ", ready.n_ready);
6636 debug_ready_list (&ready);
6637 fprintf (sched_dump,
6638 ";;\t\t before reload => truncated to %d insns\n", i);
6639 }
6640
6641 /* Delay all insns past it for 1 cycle. If debug counter is
6642 activated make an exception for the insn right after
6643 nonscheduled_insns_begin. */
6644 {
6645 rtx_insn *skip_insn;
6646
6647 if (dbg_cnt (sched_insn) == false)
6648 skip_insn = first_nonscheduled_insn ();
6649 else
6650 skip_insn = NULL;
6651
6652 while (i < ready.n_ready)
6653 {
6654 rtx_insn *insn;
6655
6656 insn = ready_remove (&ready, i);
6657
6658 if (insn != skip_insn)
6659 queue_insn (insn, 1, "list truncated");
6660 }
6661 if (skip_insn)
6662 ready_add (&ready, skip_insn, true);
6663 }
6664 }
6665
6666 /* Now we can restore basic block notes and maintain precise cfg. */
6667 restore_bb_notes (*target_bb);
6668
6669 last_clock_var = -1;
6670
6671 advance = 0;
6672
6673 gcc_assert (scheduled_insns.length () == 0);
6674 sort_p = TRUE;
6675 must_backtrack = false;
6676 modulo_insns_scheduled = 0;
6677
6678 ls.modulo_epilogue = false;
6679 ls.first_cycle_insn_p = true;
6680
6681 /* Loop until all the insns in BB are scheduled. */
6682 while ((*current_sched_info->schedule_more_p) ())
6683 {
6684 perform_replacements_new_cycle ();
6685 do
6686 {
6687 start_clock_var = clock_var;
6688
6689 clock_var++;
6690
6691 advance_one_cycle ();
6692
6693 /* Add to the ready list all pending insns that can be issued now.
6694 If there are no ready insns, increment clock until one
6695 is ready and add all pending insns at that point to the ready
6696 list. */
6697 queue_to_ready (&ready);
6698
6699 gcc_assert (ready.n_ready);
6700
6701 if (sched_verbose >= 2)
6702 {
6703 fprintf (sched_dump, ";;\t\tReady list after queue_to_ready:");
6704 debug_ready_list (&ready);
6705 }
6706 advance -= clock_var - start_clock_var;
6707 }
6708 while (advance > 0);
6709
6710 if (ls.modulo_epilogue)
6711 {
6712 int stage = clock_var / modulo_ii;
6713 if (stage > modulo_last_stage * 2 + 2)
6714 {
6715 if (sched_verbose >= 2)
6716 fprintf (sched_dump,
6717 ";;\t\tmodulo scheduled succeeded at II %d\n",
6718 modulo_ii);
6719 success = true;
6720 goto end_schedule;
6721 }
6722 }
6723 else if (modulo_ii > 0)
6724 {
6725 int stage = clock_var / modulo_ii;
6726 if (stage > modulo_max_stages)
6727 {
6728 if (sched_verbose >= 2)
6729 fprintf (sched_dump,
6730 ";;\t\tfailing schedule due to excessive stages\n");
6731 goto end_schedule;
6732 }
6733 if (modulo_n_insns == modulo_insns_scheduled
6734 && stage > modulo_last_stage)
6735 {
6736 if (sched_verbose >= 2)
6737 fprintf (sched_dump,
6738 ";;\t\tfound kernel after %d stages, II %d\n",
6739 stage, modulo_ii);
6740 ls.modulo_epilogue = true;
6741 }
6742 }
6743
6744 prune_ready_list (temp_state, true, false, ls.modulo_epilogue);
6745 if (ready.n_ready == 0)
6746 continue;
6747 if (must_backtrack)
6748 goto do_backtrack;
6749
6750 ls.shadows_only_p = false;
6751 cycle_issued_insns = 0;
6752 ls.can_issue_more = issue_rate;
6753 for (;;)
6754 {
6755 rtx_insn *insn;
6756 int cost;
6757 bool asm_p;
6758
6759 if (sort_p && ready.n_ready > 0)
6760 {
6761 /* Sort the ready list based on priority. This must be
6762 done every iteration through the loop, as schedule_insn
6763 may have readied additional insns that will not be
6764 sorted correctly. */
6765 ready_sort (&ready);
6766
6767 if (sched_verbose >= 2)
6768 {
6769 fprintf (sched_dump,
6770 ";;\t\tReady list after ready_sort: ");
6771 debug_ready_list (&ready);
6772 }
6773 }
6774
6775 /* We don't want md sched reorder to even see debug isns, so put
6776 them out right away. */
6777 if (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0))
6778 && (*current_sched_info->schedule_more_p) ())
6779 {
6780 while (ready.n_ready && DEBUG_INSN_P (ready_element (&ready, 0)))
6781 {
6782 rtx_insn *insn = ready_remove_first (&ready);
6783 gcc_assert (DEBUG_INSN_P (insn));
6784 (*current_sched_info->begin_schedule_ready) (insn);
6785 scheduled_insns.safe_push (insn);
6786 last_scheduled_insn = insn;
6787 advance = schedule_insn (insn);
6788 gcc_assert (advance == 0);
6789 if (ready.n_ready > 0)
6790 ready_sort (&ready);
6791 }
6792 }
6793
6794 if (ls.first_cycle_insn_p && !ready.n_ready)
6795 break;
6796
6797 resume_after_backtrack:
6798 /* Allow the target to reorder the list, typically for
6799 better instruction bundling. */
6800 if (sort_p
6801 && (ready.n_ready == 0
6802 || !SCHED_GROUP_P (ready_element (&ready, 0))))
6803 {
6804 if (ls.first_cycle_insn_p && targetm.sched.reorder)
6805 ls.can_issue_more
6806 = targetm.sched.reorder (sched_dump, sched_verbose,
6807 ready_lastpos (&ready),
6808 &ready.n_ready, clock_var);
6809 else if (!ls.first_cycle_insn_p && targetm.sched.reorder2)
6810 ls.can_issue_more
6811 = targetm.sched.reorder2 (sched_dump, sched_verbose,
6812 ready.n_ready
6813 ? ready_lastpos (&ready) : NULL,
6814 &ready.n_ready, clock_var);
6815 }
6816
6817 restart_choose_ready:
6818 if (sched_verbose >= 2)
6819 {
6820 fprintf (sched_dump, ";;\tReady list (t = %3d): ",
6821 clock_var);
6822 debug_ready_list (&ready);
6823 if (sched_pressure == SCHED_PRESSURE_WEIGHTED)
6824 print_curr_reg_pressure ();
6825 }
6826
6827 if (ready.n_ready == 0
6828 && ls.can_issue_more
6829 && reload_completed)
6830 {
6831 /* Allow scheduling insns directly from the queue in case
6832 there's nothing better to do (ready list is empty) but
6833 there are still vacant dispatch slots in the current cycle. */
6834 if (sched_verbose >= 6)
6835 fprintf (sched_dump,";;\t\tSecond chance\n");
6836 memcpy (temp_state, curr_state, dfa_state_size);
6837 if (early_queue_to_ready (temp_state, &ready))
6838 ready_sort (&ready);
6839 }
6840
6841 if (ready.n_ready == 0
6842 || !ls.can_issue_more
6843 || state_dead_lock_p (curr_state)
6844 || !(*current_sched_info->schedule_more_p) ())
6845 break;
6846
6847 /* Select and remove the insn from the ready list. */
6848 if (sort_p)
6849 {
6850 int res;
6851
6852 insn = NULL;
6853 res = choose_ready (&ready, ls.first_cycle_insn_p, &insn);
6854
6855 if (res < 0)
6856 /* Finish cycle. */
6857 break;
6858 if (res > 0)
6859 goto restart_choose_ready;
6860
6861 gcc_assert (insn != NULL_RTX);
6862 }
6863 else
6864 insn = ready_remove_first (&ready);
6865
6866 if (sched_pressure != SCHED_PRESSURE_NONE
6867 && INSN_TICK (insn) > clock_var)
6868 {
6869 ready_add (&ready, insn, true);
6870 advance = 1;
6871 break;
6872 }
6873
6874 if (targetm.sched.dfa_new_cycle
6875 && targetm.sched.dfa_new_cycle (sched_dump, sched_verbose,
6876 insn, last_clock_var,
6877 clock_var, &sort_p))
6878 /* SORT_P is used by the target to override sorting
6879 of the ready list. This is needed when the target
6880 has modified its internal structures expecting that
6881 the insn will be issued next. As we need the insn
6882 to have the highest priority (so it will be returned by
6883 the ready_remove_first call above), we invoke
6884 ready_add (&ready, insn, true).
6885 But, still, there is one issue: INSN can be later
6886 discarded by scheduler's front end through
6887 current_sched_info->can_schedule_ready_p, hence, won't
6888 be issued next. */
6889 {
6890 ready_add (&ready, insn, true);
6891 break;
6892 }
6893
6894 sort_p = TRUE;
6895
6896 if (current_sched_info->can_schedule_ready_p
6897 && ! (*current_sched_info->can_schedule_ready_p) (insn))
6898 /* We normally get here only if we don't want to move
6899 insn from the split block. */
6900 {
6901 TODO_SPEC (insn) = DEP_POSTPONED;
6902 goto restart_choose_ready;
6903 }
6904
6905 if (delay_htab)
6906 {
6907 /* If this insn is the first part of a delay-slot pair, record a
6908 backtrack point. */
6909 struct delay_pair *delay_entry;
6910 delay_entry
6911 = delay_htab->find_with_hash (insn, htab_hash_pointer (insn));
6912 if (delay_entry)
6913 {
6914 save_backtrack_point (delay_entry, ls);
6915 if (sched_verbose >= 2)
6916 fprintf (sched_dump, ";;\t\tsaving backtrack point\n");
6917 }
6918 }
6919
6920 /* DECISION is made. */
6921
6922 if (modulo_ii > 0 && INSN_UID (insn) < modulo_iter0_max_uid)
6923 {
6924 modulo_insns_scheduled++;
6925 modulo_last_stage = clock_var / modulo_ii;
6926 }
6927 if (TODO_SPEC (insn) & SPECULATIVE)
6928 generate_recovery_code (insn);
6929
6930 if (targetm.sched.dispatch (NULL, IS_DISPATCH_ON))
6931 targetm.sched.dispatch_do (insn, ADD_TO_DISPATCH_WINDOW);
6932
6933 /* Update counters, etc in the scheduler's front end. */
6934 (*current_sched_info->begin_schedule_ready) (insn);
6935 scheduled_insns.safe_push (insn);
6936 gcc_assert (NONDEBUG_INSN_P (insn));
6937 last_nondebug_scheduled_insn = last_scheduled_insn = insn;
6938
6939 if (recog_memoized (insn) >= 0)
6940 {
6941 memcpy (temp_state, curr_state, dfa_state_size);
6942 cost = state_transition (curr_state, insn);
6943 if (sched_pressure != SCHED_PRESSURE_WEIGHTED && !sched_fusion)
6944 gcc_assert (cost < 0);
6945 if (memcmp (temp_state, curr_state, dfa_state_size) != 0)
6946 cycle_issued_insns++;
6947 asm_p = false;
6948 }
6949 else
6950 asm_p = (GET_CODE (PATTERN (insn)) == ASM_INPUT
6951 || asm_noperands (PATTERN (insn)) >= 0);
6952
6953 if (targetm.sched.variable_issue)
6954 ls.can_issue_more =
6955 targetm.sched.variable_issue (sched_dump, sched_verbose,
6956 insn, ls.can_issue_more);
6957 /* A naked CLOBBER or USE generates no instruction, so do
6958 not count them against the issue rate. */
6959 else if (GET_CODE (PATTERN (insn)) != USE
6960 && GET_CODE (PATTERN (insn)) != CLOBBER)
6961 ls.can_issue_more--;
6962 advance = schedule_insn (insn);
6963
6964 if (SHADOW_P (insn))
6965 ls.shadows_only_p = true;
6966
6967 /* After issuing an asm insn we should start a new cycle. */
6968 if (advance == 0 && asm_p)
6969 advance = 1;
6970
6971 if (must_backtrack)
6972 break;
6973
6974 if (advance != 0)
6975 break;
6976
6977 ls.first_cycle_insn_p = false;
6978 if (ready.n_ready > 0)
6979 prune_ready_list (temp_state, false, ls.shadows_only_p,
6980 ls.modulo_epilogue);
6981 }
6982
6983 do_backtrack:
6984 if (!must_backtrack)
6985 for (i = 0; i < ready.n_ready; i++)
6986 {
6987 rtx_insn *insn = ready_element (&ready, i);
6988 if (INSN_EXACT_TICK (insn) == clock_var)
6989 {
6990 must_backtrack = true;
6991 clock_var++;
6992 break;
6993 }
6994 }
6995 if (must_backtrack && modulo_ii > 0)
6996 {
6997 if (modulo_backtracks_left == 0)
6998 goto end_schedule;
6999 modulo_backtracks_left--;
7000 }
7001 while (must_backtrack)
7002 {
7003 struct haifa_saved_data *failed;
7004 rtx_insn *failed_insn;
7005
7006 must_backtrack = false;
7007 failed = verify_shadows ();
7008 gcc_assert (failed);
7009
7010 failed_insn = failed->delay_pair->i1;
7011 /* Clear these queues. */
7012 perform_replacements_new_cycle ();
7013 toggle_cancelled_flags (false);
7014 unschedule_insns_until (failed_insn);
7015 while (failed != backtrack_queue)
7016 free_topmost_backtrack_point (true);
7017 restore_last_backtrack_point (&ls);
7018 if (sched_verbose >= 2)
7019 fprintf (sched_dump, ";;\t\trewind to cycle %d\n", clock_var);
7020 /* Delay by at least a cycle. This could cause additional
7021 backtracking. */
7022 queue_insn (failed_insn, 1, "backtracked");
7023 advance = 0;
7024 if (must_backtrack)
7025 continue;
7026 if (ready.n_ready > 0)
7027 goto resume_after_backtrack;
7028 else
7029 {
7030 if (clock_var == 0 && ls.first_cycle_insn_p)
7031 goto end_schedule;
7032 advance = 1;
7033 break;
7034 }
7035 }
7036 ls.first_cycle_insn_p = true;
7037 }
7038 if (ls.modulo_epilogue)
7039 success = true;
7040 end_schedule:
7041 if (!ls.first_cycle_insn_p || advance)
7042 advance_one_cycle ();
7043 perform_replacements_new_cycle ();
7044 if (modulo_ii > 0)
7045 {
7046 /* Once again, debug insn suckiness: they can be on the ready list
7047 even if they have unresolved dependencies. To make our view
7048 of the world consistent, remove such "ready" insns. */
7049 restart_debug_insn_loop:
7050 for (i = ready.n_ready - 1; i >= 0; i--)
7051 {
7052 rtx_insn *x;
7053
7054 x = ready_element (&ready, i);
7055 if (DEPS_LIST_FIRST (INSN_HARD_BACK_DEPS (x)) != NULL
7056 || DEPS_LIST_FIRST (INSN_SPEC_BACK_DEPS (x)) != NULL)
7057 {
7058 ready_remove (&ready, i);
7059 goto restart_debug_insn_loop;
7060 }
7061 }
7062 for (i = ready.n_ready - 1; i >= 0; i--)
7063 {
7064 rtx_insn *x;
7065
7066 x = ready_element (&ready, i);
7067 resolve_dependencies (x);
7068 }
7069 for (i = 0; i <= max_insn_queue_index; i++)
7070 {
7071 rtx_insn_list *link;
7072 while ((link = insn_queue[i]) != NULL)
7073 {
7074 rtx_insn *x = link->insn ();
7075 insn_queue[i] = link->next ();
7076 QUEUE_INDEX (x) = QUEUE_NOWHERE;
7077 free_INSN_LIST_node (link);
7078 resolve_dependencies (x);
7079 }
7080 }
7081 }
7082
7083 if (!success)
7084 undo_all_replacements ();
7085
7086 /* Debug info. */
7087 if (sched_verbose)
7088 {
7089 fprintf (sched_dump, ";;\tReady list (final): ");
7090 debug_ready_list (&ready);
7091 }
7092
7093 if (modulo_ii == 0 && current_sched_info->queue_must_finish_empty)
7094 /* Sanity check -- queue must be empty now. Meaningless if region has
7095 multiple bbs. */
7096 gcc_assert (!q_size && !ready.n_ready && !ready.n_debug);
7097 else if (modulo_ii == 0)
7098 {
7099 /* We must maintain QUEUE_INDEX between blocks in region. */
7100 for (i = ready.n_ready - 1; i >= 0; i--)
7101 {
7102 rtx_insn *x;
7103
7104 x = ready_element (&ready, i);
7105 QUEUE_INDEX (x) = QUEUE_NOWHERE;
7106 TODO_SPEC (x) = HARD_DEP;
7107 }
7108
7109 if (q_size)
7110 for (i = 0; i <= max_insn_queue_index; i++)
7111 {
7112 rtx_insn_list *link;
7113 for (link = insn_queue[i]; link; link = link->next ())
7114 {
7115 rtx_insn *x;
7116
7117 x = link->insn ();
7118 QUEUE_INDEX (x) = QUEUE_NOWHERE;
7119 TODO_SPEC (x) = HARD_DEP;
7120 }
7121 free_INSN_LIST_list (&insn_queue[i]);
7122 }
7123 }
7124
7125 if (sched_pressure == SCHED_PRESSURE_MODEL)
7126 model_end_schedule ();
7127
7128 if (success)
7129 {
7130 commit_schedule (prev_head, tail, target_bb);
7131 if (sched_verbose)
7132 fprintf (sched_dump, ";; total time = %d\n", clock_var);
7133 }
7134 else
7135 last_scheduled_insn = tail;
7136
7137 scheduled_insns.truncate (0);
7138
7139 if (!current_sched_info->queue_must_finish_empty
7140 || haifa_recovery_bb_recently_added_p)
7141 {
7142 /* INSN_TICK (minimum clock tick at which the insn becomes
7143 ready) may be not correct for the insn in the subsequent
7144 blocks of the region. We should use a correct value of
7145 `clock_var' or modify INSN_TICK. It is better to keep
7146 clock_var value equal to 0 at the start of a basic block.
7147 Therefore we modify INSN_TICK here. */
7148 fix_inter_tick (NEXT_INSN (prev_head), last_scheduled_insn);
7149 }
7150
7151 if (targetm.sched.finish)
7152 {
7153 targetm.sched.finish (sched_dump, sched_verbose);
7154 /* Target might have added some instructions to the scheduled block
7155 in its md_finish () hook. These new insns don't have any data
7156 initialized and to identify them we extend h_i_d so that they'll
7157 get zero luids. */
7158 sched_extend_luids ();
7159 }
7160
7161 /* Update head/tail boundaries. */
7162 head = NEXT_INSN (prev_head);
7163 tail = last_scheduled_insn;
7164
7165 if (sched_verbose)
7166 {
7167 fprintf (sched_dump, ";; new head = %d\n;; new tail = %d\n",
7168 INSN_UID (head), INSN_UID (tail));
7169
7170 if (sched_verbose >= 2)
7171 {
7172 dump_insn_stream (head, tail);
7173 print_rank_for_schedule_stats (";; TOTAL ", &rank_for_schedule_stats,
7174 NULL);
7175 }
7176
7177 fprintf (sched_dump, "\n");
7178 }
7179
7180 head = restore_other_notes (head, NULL);
7181
7182 current_sched_info->head = head;
7183 current_sched_info->tail = tail;
7184
7185 free_backtrack_queue ();
7186
7187 return success;
7188 }
7189 \f
7190 /* Set_priorities: compute priority of each insn in the block. */
7191
7192 int
7193 set_priorities (rtx_insn *head, rtx_insn *tail)
7194 {
7195 rtx_insn *insn;
7196 int n_insn;
7197 int sched_max_insns_priority =
7198 current_sched_info->sched_max_insns_priority;
7199 rtx_insn *prev_head;
7200
7201 if (head == tail && ! INSN_P (head))
7202 gcc_unreachable ();
7203
7204 n_insn = 0;
7205
7206 prev_head = PREV_INSN (head);
7207 for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
7208 {
7209 if (!INSN_P (insn))
7210 continue;
7211
7212 n_insn++;
7213 (void) priority (insn);
7214
7215 gcc_assert (INSN_PRIORITY_KNOWN (insn));
7216
7217 sched_max_insns_priority = MAX (sched_max_insns_priority,
7218 INSN_PRIORITY (insn));
7219 }
7220
7221 current_sched_info->sched_max_insns_priority = sched_max_insns_priority;
7222
7223 return n_insn;
7224 }
7225
7226 /* Set dump and sched_verbose for the desired debugging output. If no
7227 dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
7228 For -fsched-verbose=N, N>=10, print everything to stderr. */
7229 void
7230 setup_sched_dump (void)
7231 {
7232 sched_verbose = sched_verbose_param;
7233 if (sched_verbose_param == 0 && dump_file)
7234 sched_verbose = 1;
7235 sched_dump = ((sched_verbose_param >= 10 || !dump_file)
7236 ? stderr : dump_file);
7237 }
7238
7239 /* Allocate data for register pressure sensitive scheduling. */
7240 static void
7241 alloc_global_sched_pressure_data (void)
7242 {
7243 if (sched_pressure != SCHED_PRESSURE_NONE)
7244 {
7245 int i, max_regno = max_reg_num ();
7246
7247 if (sched_dump != NULL)
7248 /* We need info about pseudos for rtl dumps about pseudo
7249 classes and costs. */
7250 regstat_init_n_sets_and_refs ();
7251 ira_set_pseudo_classes (true, sched_verbose ? sched_dump : NULL);
7252 sched_regno_pressure_class
7253 = (enum reg_class *) xmalloc (max_regno * sizeof (enum reg_class));
7254 for (i = 0; i < max_regno; i++)
7255 sched_regno_pressure_class[i]
7256 = (i < FIRST_PSEUDO_REGISTER
7257 ? ira_pressure_class_translate[REGNO_REG_CLASS (i)]
7258 : ira_pressure_class_translate[reg_allocno_class (i)]);
7259 curr_reg_live = BITMAP_ALLOC (NULL);
7260 if (sched_pressure == SCHED_PRESSURE_WEIGHTED)
7261 {
7262 saved_reg_live = BITMAP_ALLOC (NULL);
7263 region_ref_regs = BITMAP_ALLOC (NULL);
7264 }
7265
7266 /* Calculate number of CALL_USED_REGS in register classes that
7267 we calculate register pressure for. */
7268 for (int c = 0; c < ira_pressure_classes_num; ++c)
7269 {
7270 enum reg_class cl = ira_pressure_classes[c];
7271
7272 call_used_regs_num[cl] = 0;
7273
7274 for (int i = 0; i < ira_class_hard_regs_num[cl]; ++i)
7275 if (call_used_regs[ira_class_hard_regs[cl][i]])
7276 ++call_used_regs_num[cl];
7277 }
7278 }
7279 }
7280
7281 /* Free data for register pressure sensitive scheduling. Also called
7282 from schedule_region when stopping sched-pressure early. */
7283 void
7284 free_global_sched_pressure_data (void)
7285 {
7286 if (sched_pressure != SCHED_PRESSURE_NONE)
7287 {
7288 if (regstat_n_sets_and_refs != NULL)
7289 regstat_free_n_sets_and_refs ();
7290 if (sched_pressure == SCHED_PRESSURE_WEIGHTED)
7291 {
7292 BITMAP_FREE (region_ref_regs);
7293 BITMAP_FREE (saved_reg_live);
7294 }
7295 BITMAP_FREE (curr_reg_live);
7296 free (sched_regno_pressure_class);
7297 }
7298 }
7299
7300 /* Initialize some global state for the scheduler. This function works
7301 with the common data shared between all the schedulers. It is called
7302 from the scheduler specific initialization routine. */
7303
7304 void
7305 sched_init (void)
7306 {
7307 /* Disable speculative loads in their presence if cc0 defined. */
7308 if (HAVE_cc0)
7309 flag_schedule_speculative_load = 0;
7310
7311 if (targetm.sched.dispatch (NULL, IS_DISPATCH_ON))
7312 targetm.sched.dispatch_do (NULL, DISPATCH_INIT);
7313
7314 if (live_range_shrinkage_p)
7315 sched_pressure = SCHED_PRESSURE_WEIGHTED;
7316 else if (flag_sched_pressure
7317 && !reload_completed
7318 && common_sched_info->sched_pass_id == SCHED_RGN_PASS)
7319 sched_pressure = ((enum sched_pressure_algorithm)
7320 PARAM_VALUE (PARAM_SCHED_PRESSURE_ALGORITHM));
7321 else
7322 sched_pressure = SCHED_PRESSURE_NONE;
7323
7324 if (sched_pressure != SCHED_PRESSURE_NONE)
7325 ira_setup_eliminable_regset ();
7326
7327 /* Initialize SPEC_INFO. */
7328 if (targetm.sched.set_sched_flags)
7329 {
7330 spec_info = &spec_info_var;
7331 targetm.sched.set_sched_flags (spec_info);
7332
7333 if (spec_info->mask != 0)
7334 {
7335 spec_info->data_weakness_cutoff =
7336 (PARAM_VALUE (PARAM_SCHED_SPEC_PROB_CUTOFF) * MAX_DEP_WEAK) / 100;
7337 spec_info->control_weakness_cutoff =
7338 (PARAM_VALUE (PARAM_SCHED_SPEC_PROB_CUTOFF)
7339 * REG_BR_PROB_BASE) / 100;
7340 }
7341 else
7342 /* So we won't read anything accidentally. */
7343 spec_info = NULL;
7344
7345 }
7346 else
7347 /* So we won't read anything accidentally. */
7348 spec_info = 0;
7349
7350 /* Initialize issue_rate. */
7351 if (targetm.sched.issue_rate)
7352 issue_rate = targetm.sched.issue_rate ();
7353 else
7354 issue_rate = 1;
7355
7356 if (targetm.sched.first_cycle_multipass_dfa_lookahead
7357 /* Don't use max_issue with reg_pressure scheduling. Multipass
7358 scheduling and reg_pressure scheduling undo each other's decisions. */
7359 && sched_pressure == SCHED_PRESSURE_NONE)
7360 dfa_lookahead = targetm.sched.first_cycle_multipass_dfa_lookahead ();
7361 else
7362 dfa_lookahead = 0;
7363
7364 /* Set to "0" so that we recalculate. */
7365 max_lookahead_tries = 0;
7366
7367 if (targetm.sched.init_dfa_pre_cycle_insn)
7368 targetm.sched.init_dfa_pre_cycle_insn ();
7369
7370 if (targetm.sched.init_dfa_post_cycle_insn)
7371 targetm.sched.init_dfa_post_cycle_insn ();
7372
7373 dfa_start ();
7374 dfa_state_size = state_size ();
7375
7376 init_alias_analysis ();
7377
7378 if (!sched_no_dce)
7379 df_set_flags (DF_LR_RUN_DCE);
7380 df_note_add_problem ();
7381
7382 /* More problems needed for interloop dep calculation in SMS. */
7383 if (common_sched_info->sched_pass_id == SCHED_SMS_PASS)
7384 {
7385 df_rd_add_problem ();
7386 df_chain_add_problem (DF_DU_CHAIN + DF_UD_CHAIN);
7387 }
7388
7389 df_analyze ();
7390
7391 /* Do not run DCE after reload, as this can kill nops inserted
7392 by bundling. */
7393 if (reload_completed)
7394 df_clear_flags (DF_LR_RUN_DCE);
7395
7396 regstat_compute_calls_crossed ();
7397
7398 if (targetm.sched.init_global)
7399 targetm.sched.init_global (sched_dump, sched_verbose, get_max_uid () + 1);
7400
7401 alloc_global_sched_pressure_data ();
7402
7403 curr_state = xmalloc (dfa_state_size);
7404 }
7405
7406 static void haifa_init_only_bb (basic_block, basic_block);
7407
7408 /* Initialize data structures specific to the Haifa scheduler. */
7409 void
7410 haifa_sched_init (void)
7411 {
7412 setup_sched_dump ();
7413 sched_init ();
7414
7415 scheduled_insns.create (0);
7416
7417 if (spec_info != NULL)
7418 {
7419 sched_deps_info->use_deps_list = 1;
7420 sched_deps_info->generate_spec_deps = 1;
7421 }
7422
7423 /* Initialize luids, dependency caches, target and h_i_d for the
7424 whole function. */
7425 {
7426 bb_vec_t bbs;
7427 bbs.create (n_basic_blocks_for_fn (cfun));
7428 basic_block bb;
7429
7430 sched_init_bbs ();
7431
7432 FOR_EACH_BB_FN (bb, cfun)
7433 bbs.quick_push (bb);
7434 sched_init_luids (bbs);
7435 sched_deps_init (true);
7436 sched_extend_target ();
7437 haifa_init_h_i_d (bbs);
7438
7439 bbs.release ();
7440 }
7441
7442 sched_init_only_bb = haifa_init_only_bb;
7443 sched_split_block = sched_split_block_1;
7444 sched_create_empty_bb = sched_create_empty_bb_1;
7445 haifa_recovery_bb_ever_added_p = false;
7446
7447 nr_begin_data = nr_begin_control = nr_be_in_data = nr_be_in_control = 0;
7448 before_recovery = 0;
7449 after_recovery = 0;
7450
7451 modulo_ii = 0;
7452 }
7453
7454 /* Finish work with the data specific to the Haifa scheduler. */
7455 void
7456 haifa_sched_finish (void)
7457 {
7458 sched_create_empty_bb = NULL;
7459 sched_split_block = NULL;
7460 sched_init_only_bb = NULL;
7461
7462 if (spec_info && spec_info->dump)
7463 {
7464 char c = reload_completed ? 'a' : 'b';
7465
7466 fprintf (spec_info->dump,
7467 ";; %s:\n", current_function_name ());
7468
7469 fprintf (spec_info->dump,
7470 ";; Procedure %cr-begin-data-spec motions == %d\n",
7471 c, nr_begin_data);
7472 fprintf (spec_info->dump,
7473 ";; Procedure %cr-be-in-data-spec motions == %d\n",
7474 c, nr_be_in_data);
7475 fprintf (spec_info->dump,
7476 ";; Procedure %cr-begin-control-spec motions == %d\n",
7477 c, nr_begin_control);
7478 fprintf (spec_info->dump,
7479 ";; Procedure %cr-be-in-control-spec motions == %d\n",
7480 c, nr_be_in_control);
7481 }
7482
7483 scheduled_insns.release ();
7484
7485 /* Finalize h_i_d, dependency caches, and luids for the whole
7486 function. Target will be finalized in md_global_finish (). */
7487 sched_deps_finish ();
7488 sched_finish_luids ();
7489 current_sched_info = NULL;
7490 sched_finish ();
7491 }
7492
7493 /* Free global data used during insn scheduling. This function works with
7494 the common data shared between the schedulers. */
7495
7496 void
7497 sched_finish (void)
7498 {
7499 haifa_finish_h_i_d ();
7500 free_global_sched_pressure_data ();
7501 free (curr_state);
7502
7503 if (targetm.sched.finish_global)
7504 targetm.sched.finish_global (sched_dump, sched_verbose);
7505
7506 end_alias_analysis ();
7507
7508 regstat_free_calls_crossed ();
7509
7510 dfa_finish ();
7511 }
7512
7513 /* Free all delay_pair structures that were recorded. */
7514 void
7515 free_delay_pairs (void)
7516 {
7517 if (delay_htab)
7518 {
7519 delay_htab->empty ();
7520 delay_htab_i2->empty ();
7521 }
7522 }
7523
7524 /* Fix INSN_TICKs of the instructions in the current block as well as
7525 INSN_TICKs of their dependents.
7526 HEAD and TAIL are the begin and the end of the current scheduled block. */
7527 static void
7528 fix_inter_tick (rtx_insn *head, rtx_insn *tail)
7529 {
7530 /* Set of instructions with corrected INSN_TICK. */
7531 bitmap_head processed;
7532 /* ??? It is doubtful if we should assume that cycle advance happens on
7533 basic block boundaries. Basically insns that are unconditionally ready
7534 on the start of the block are more preferable then those which have
7535 a one cycle dependency over insn from the previous block. */
7536 int next_clock = clock_var + 1;
7537
7538 bitmap_initialize (&processed, 0);
7539
7540 /* Iterates over scheduled instructions and fix their INSN_TICKs and
7541 INSN_TICKs of dependent instructions, so that INSN_TICKs are consistent
7542 across different blocks. */
7543 for (tail = NEXT_INSN (tail); head != tail; head = NEXT_INSN (head))
7544 {
7545 if (INSN_P (head))
7546 {
7547 int tick;
7548 sd_iterator_def sd_it;
7549 dep_t dep;
7550
7551 tick = INSN_TICK (head);
7552 gcc_assert (tick >= MIN_TICK);
7553
7554 /* Fix INSN_TICK of instruction from just scheduled block. */
7555 if (bitmap_set_bit (&processed, INSN_LUID (head)))
7556 {
7557 tick -= next_clock;
7558
7559 if (tick < MIN_TICK)
7560 tick = MIN_TICK;
7561
7562 INSN_TICK (head) = tick;
7563 }
7564
7565 if (DEBUG_INSN_P (head))
7566 continue;
7567
7568 FOR_EACH_DEP (head, SD_LIST_RES_FORW, sd_it, dep)
7569 {
7570 rtx_insn *next;
7571
7572 next = DEP_CON (dep);
7573 tick = INSN_TICK (next);
7574
7575 if (tick != INVALID_TICK
7576 /* If NEXT has its INSN_TICK calculated, fix it.
7577 If not - it will be properly calculated from
7578 scratch later in fix_tick_ready. */
7579 && bitmap_set_bit (&processed, INSN_LUID (next)))
7580 {
7581 tick -= next_clock;
7582
7583 if (tick < MIN_TICK)
7584 tick = MIN_TICK;
7585
7586 if (tick > INTER_TICK (next))
7587 INTER_TICK (next) = tick;
7588 else
7589 tick = INTER_TICK (next);
7590
7591 INSN_TICK (next) = tick;
7592 }
7593 }
7594 }
7595 }
7596 bitmap_clear (&processed);
7597 }
7598
7599 /* Check if NEXT is ready to be added to the ready or queue list.
7600 If "yes", add it to the proper list.
7601 Returns:
7602 -1 - is not ready yet,
7603 0 - added to the ready list,
7604 0 < N - queued for N cycles. */
7605 int
7606 try_ready (rtx_insn *next)
7607 {
7608 ds_t old_ts, new_ts;
7609
7610 old_ts = TODO_SPEC (next);
7611
7612 gcc_assert (!(old_ts & ~(SPECULATIVE | HARD_DEP | DEP_CONTROL | DEP_POSTPONED))
7613 && (old_ts == HARD_DEP
7614 || old_ts == DEP_POSTPONED
7615 || (old_ts & SPECULATIVE)
7616 || old_ts == DEP_CONTROL));
7617
7618 new_ts = recompute_todo_spec (next, false);
7619
7620 if (new_ts & (HARD_DEP | DEP_POSTPONED))
7621 gcc_assert (new_ts == old_ts
7622 && QUEUE_INDEX (next) == QUEUE_NOWHERE);
7623 else if (current_sched_info->new_ready)
7624 new_ts = current_sched_info->new_ready (next, new_ts);
7625
7626 /* * if !(old_ts & SPECULATIVE) (e.g. HARD_DEP or 0), then insn might
7627 have its original pattern or changed (speculative) one. This is due
7628 to changing ebb in region scheduling.
7629 * But if (old_ts & SPECULATIVE), then we are pretty sure that insn
7630 has speculative pattern.
7631
7632 We can't assert (!(new_ts & HARD_DEP) || new_ts == old_ts) here because
7633 control-speculative NEXT could have been discarded by sched-rgn.c
7634 (the same case as when discarded by can_schedule_ready_p ()). */
7635
7636 if ((new_ts & SPECULATIVE)
7637 /* If (old_ts == new_ts), then (old_ts & SPECULATIVE) and we don't
7638 need to change anything. */
7639 && new_ts != old_ts)
7640 {
7641 int res;
7642 rtx new_pat;
7643
7644 gcc_assert ((new_ts & SPECULATIVE) && !(new_ts & ~SPECULATIVE));
7645
7646 res = haifa_speculate_insn (next, new_ts, &new_pat);
7647
7648 switch (res)
7649 {
7650 case -1:
7651 /* It would be nice to change DEP_STATUS of all dependences,
7652 which have ((DEP_STATUS & SPECULATIVE) == new_ts) to HARD_DEP,
7653 so we won't reanalyze anything. */
7654 new_ts = HARD_DEP;
7655 break;
7656
7657 case 0:
7658 /* We follow the rule, that every speculative insn
7659 has non-null ORIG_PAT. */
7660 if (!ORIG_PAT (next))
7661 ORIG_PAT (next) = PATTERN (next);
7662 break;
7663
7664 case 1:
7665 if (!ORIG_PAT (next))
7666 /* If we gonna to overwrite the original pattern of insn,
7667 save it. */
7668 ORIG_PAT (next) = PATTERN (next);
7669
7670 res = haifa_change_pattern (next, new_pat);
7671 gcc_assert (res);
7672 break;
7673
7674 default:
7675 gcc_unreachable ();
7676 }
7677 }
7678
7679 /* We need to restore pattern only if (new_ts == 0), because otherwise it is
7680 either correct (new_ts & SPECULATIVE),
7681 or we simply don't care (new_ts & HARD_DEP). */
7682
7683 gcc_assert (!ORIG_PAT (next)
7684 || !IS_SPECULATION_BRANCHY_CHECK_P (next));
7685
7686 TODO_SPEC (next) = new_ts;
7687
7688 if (new_ts & (HARD_DEP | DEP_POSTPONED))
7689 {
7690 /* We can't assert (QUEUE_INDEX (next) == QUEUE_NOWHERE) here because
7691 control-speculative NEXT could have been discarded by sched-rgn.c
7692 (the same case as when discarded by can_schedule_ready_p ()). */
7693 /*gcc_assert (QUEUE_INDEX (next) == QUEUE_NOWHERE);*/
7694
7695 change_queue_index (next, QUEUE_NOWHERE);
7696
7697 return -1;
7698 }
7699 else if (!(new_ts & BEGIN_SPEC)
7700 && ORIG_PAT (next) && PREDICATED_PAT (next) == NULL_RTX
7701 && !IS_SPECULATION_CHECK_P (next))
7702 /* We should change pattern of every previously speculative
7703 instruction - and we determine if NEXT was speculative by using
7704 ORIG_PAT field. Except one case - speculation checks have ORIG_PAT
7705 pat too, so skip them. */
7706 {
7707 bool success = haifa_change_pattern (next, ORIG_PAT (next));
7708 gcc_assert (success);
7709 ORIG_PAT (next) = 0;
7710 }
7711
7712 if (sched_verbose >= 2)
7713 {
7714 fprintf (sched_dump, ";;\t\tdependencies resolved: insn %s",
7715 (*current_sched_info->print_insn) (next, 0));
7716
7717 if (spec_info && spec_info->dump)
7718 {
7719 if (new_ts & BEGIN_DATA)
7720 fprintf (spec_info->dump, "; data-spec;");
7721 if (new_ts & BEGIN_CONTROL)
7722 fprintf (spec_info->dump, "; control-spec;");
7723 if (new_ts & BE_IN_CONTROL)
7724 fprintf (spec_info->dump, "; in-control-spec;");
7725 }
7726 if (TODO_SPEC (next) & DEP_CONTROL)
7727 fprintf (sched_dump, " predicated");
7728 fprintf (sched_dump, "\n");
7729 }
7730
7731 adjust_priority (next);
7732
7733 return fix_tick_ready (next);
7734 }
7735
7736 /* Calculate INSN_TICK of NEXT and add it to either ready or queue list. */
7737 static int
7738 fix_tick_ready (rtx_insn *next)
7739 {
7740 int tick, delay;
7741
7742 if (!DEBUG_INSN_P (next) && !sd_lists_empty_p (next, SD_LIST_RES_BACK))
7743 {
7744 int full_p;
7745 sd_iterator_def sd_it;
7746 dep_t dep;
7747
7748 tick = INSN_TICK (next);
7749 /* if tick is not equal to INVALID_TICK, then update
7750 INSN_TICK of NEXT with the most recent resolved dependence
7751 cost. Otherwise, recalculate from scratch. */
7752 full_p = (tick == INVALID_TICK);
7753
7754 FOR_EACH_DEP (next, SD_LIST_RES_BACK, sd_it, dep)
7755 {
7756 rtx_insn *pro = DEP_PRO (dep);
7757 int tick1;
7758
7759 gcc_assert (INSN_TICK (pro) >= MIN_TICK);
7760
7761 tick1 = INSN_TICK (pro) + dep_cost (dep);
7762 if (tick1 > tick)
7763 tick = tick1;
7764
7765 if (!full_p)
7766 break;
7767 }
7768 }
7769 else
7770 tick = -1;
7771
7772 INSN_TICK (next) = tick;
7773
7774 delay = tick - clock_var;
7775 if (delay <= 0 || sched_pressure != SCHED_PRESSURE_NONE || sched_fusion)
7776 delay = QUEUE_READY;
7777
7778 change_queue_index (next, delay);
7779
7780 return delay;
7781 }
7782
7783 /* Move NEXT to the proper queue list with (DELAY >= 1),
7784 or add it to the ready list (DELAY == QUEUE_READY),
7785 or remove it from ready and queue lists at all (DELAY == QUEUE_NOWHERE). */
7786 static void
7787 change_queue_index (rtx_insn *next, int delay)
7788 {
7789 int i = QUEUE_INDEX (next);
7790
7791 gcc_assert (QUEUE_NOWHERE <= delay && delay <= max_insn_queue_index
7792 && delay != 0);
7793 gcc_assert (i != QUEUE_SCHEDULED);
7794
7795 if ((delay > 0 && NEXT_Q_AFTER (q_ptr, delay) == i)
7796 || (delay < 0 && delay == i))
7797 /* We have nothing to do. */
7798 return;
7799
7800 /* Remove NEXT from wherever it is now. */
7801 if (i == QUEUE_READY)
7802 ready_remove_insn (next);
7803 else if (i >= 0)
7804 queue_remove (next);
7805
7806 /* Add it to the proper place. */
7807 if (delay == QUEUE_READY)
7808 ready_add (readyp, next, false);
7809 else if (delay >= 1)
7810 queue_insn (next, delay, "change queue index");
7811
7812 if (sched_verbose >= 2)
7813 {
7814 fprintf (sched_dump, ";;\t\ttick updated: insn %s",
7815 (*current_sched_info->print_insn) (next, 0));
7816
7817 if (delay == QUEUE_READY)
7818 fprintf (sched_dump, " into ready\n");
7819 else if (delay >= 1)
7820 fprintf (sched_dump, " into queue with cost=%d\n", delay);
7821 else
7822 fprintf (sched_dump, " removed from ready or queue lists\n");
7823 }
7824 }
7825
7826 static int sched_ready_n_insns = -1;
7827
7828 /* Initialize per region data structures. */
7829 void
7830 sched_extend_ready_list (int new_sched_ready_n_insns)
7831 {
7832 int i;
7833
7834 if (sched_ready_n_insns == -1)
7835 /* At the first call we need to initialize one more choice_stack
7836 entry. */
7837 {
7838 i = 0;
7839 sched_ready_n_insns = 0;
7840 scheduled_insns.reserve (new_sched_ready_n_insns);
7841 }
7842 else
7843 i = sched_ready_n_insns + 1;
7844
7845 ready.veclen = new_sched_ready_n_insns + issue_rate;
7846 ready.vec = XRESIZEVEC (rtx_insn *, ready.vec, ready.veclen);
7847
7848 gcc_assert (new_sched_ready_n_insns >= sched_ready_n_insns);
7849
7850 ready_try = (signed char *) xrecalloc (ready_try, new_sched_ready_n_insns,
7851 sched_ready_n_insns,
7852 sizeof (*ready_try));
7853
7854 /* We allocate +1 element to save initial state in the choice_stack[0]
7855 entry. */
7856 choice_stack = XRESIZEVEC (struct choice_entry, choice_stack,
7857 new_sched_ready_n_insns + 1);
7858
7859 for (; i <= new_sched_ready_n_insns; i++)
7860 {
7861 choice_stack[i].state = xmalloc (dfa_state_size);
7862
7863 if (targetm.sched.first_cycle_multipass_init)
7864 targetm.sched.first_cycle_multipass_init (&(choice_stack[i]
7865 .target_data));
7866 }
7867
7868 sched_ready_n_insns = new_sched_ready_n_insns;
7869 }
7870
7871 /* Free per region data structures. */
7872 void
7873 sched_finish_ready_list (void)
7874 {
7875 int i;
7876
7877 free (ready.vec);
7878 ready.vec = NULL;
7879 ready.veclen = 0;
7880
7881 free (ready_try);
7882 ready_try = NULL;
7883
7884 for (i = 0; i <= sched_ready_n_insns; i++)
7885 {
7886 if (targetm.sched.first_cycle_multipass_fini)
7887 targetm.sched.first_cycle_multipass_fini (&(choice_stack[i]
7888 .target_data));
7889
7890 free (choice_stack [i].state);
7891 }
7892 free (choice_stack);
7893 choice_stack = NULL;
7894
7895 sched_ready_n_insns = -1;
7896 }
7897
7898 static int
7899 haifa_luid_for_non_insn (rtx x)
7900 {
7901 gcc_assert (NOTE_P (x) || LABEL_P (x));
7902
7903 return 0;
7904 }
7905
7906 /* Generates recovery code for INSN. */
7907 static void
7908 generate_recovery_code (rtx_insn *insn)
7909 {
7910 if (TODO_SPEC (insn) & BEGIN_SPEC)
7911 begin_speculative_block (insn);
7912
7913 /* Here we have insn with no dependencies to
7914 instructions other then CHECK_SPEC ones. */
7915
7916 if (TODO_SPEC (insn) & BE_IN_SPEC)
7917 add_to_speculative_block (insn);
7918 }
7919
7920 /* Helper function.
7921 Tries to add speculative dependencies of type FS between instructions
7922 in deps_list L and TWIN. */
7923 static void
7924 process_insn_forw_deps_be_in_spec (rtx_insn *insn, rtx_insn *twin, ds_t fs)
7925 {
7926 sd_iterator_def sd_it;
7927 dep_t dep;
7928
7929 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
7930 {
7931 ds_t ds;
7932 rtx_insn *consumer;
7933
7934 consumer = DEP_CON (dep);
7935
7936 ds = DEP_STATUS (dep);
7937
7938 if (/* If we want to create speculative dep. */
7939 fs
7940 /* And we can do that because this is a true dep. */
7941 && (ds & DEP_TYPES) == DEP_TRUE)
7942 {
7943 gcc_assert (!(ds & BE_IN_SPEC));
7944
7945 if (/* If this dep can be overcome with 'begin speculation'. */
7946 ds & BEGIN_SPEC)
7947 /* Then we have a choice: keep the dep 'begin speculative'
7948 or transform it into 'be in speculative'. */
7949 {
7950 if (/* In try_ready we assert that if insn once became ready
7951 it can be removed from the ready (or queue) list only
7952 due to backend decision. Hence we can't let the
7953 probability of the speculative dep to decrease. */
7954 ds_weak (ds) <= ds_weak (fs))
7955 {
7956 ds_t new_ds;
7957
7958 new_ds = (ds & ~BEGIN_SPEC) | fs;
7959
7960 if (/* consumer can 'be in speculative'. */
7961 sched_insn_is_legitimate_for_speculation_p (consumer,
7962 new_ds))
7963 /* Transform it to be in speculative. */
7964 ds = new_ds;
7965 }
7966 }
7967 else
7968 /* Mark the dep as 'be in speculative'. */
7969 ds |= fs;
7970 }
7971
7972 {
7973 dep_def _new_dep, *new_dep = &_new_dep;
7974
7975 init_dep_1 (new_dep, twin, consumer, DEP_TYPE (dep), ds);
7976 sd_add_dep (new_dep, false);
7977 }
7978 }
7979 }
7980
7981 /* Generates recovery code for BEGIN speculative INSN. */
7982 static void
7983 begin_speculative_block (rtx_insn *insn)
7984 {
7985 if (TODO_SPEC (insn) & BEGIN_DATA)
7986 nr_begin_data++;
7987 if (TODO_SPEC (insn) & BEGIN_CONTROL)
7988 nr_begin_control++;
7989
7990 create_check_block_twin (insn, false);
7991
7992 TODO_SPEC (insn) &= ~BEGIN_SPEC;
7993 }
7994
7995 static void haifa_init_insn (rtx_insn *);
7996
7997 /* Generates recovery code for BE_IN speculative INSN. */
7998 static void
7999 add_to_speculative_block (rtx_insn *insn)
8000 {
8001 ds_t ts;
8002 sd_iterator_def sd_it;
8003 dep_t dep;
8004 rtx_insn_list *twins = NULL;
8005 rtx_vec_t priorities_roots;
8006
8007 ts = TODO_SPEC (insn);
8008 gcc_assert (!(ts & ~BE_IN_SPEC));
8009
8010 if (ts & BE_IN_DATA)
8011 nr_be_in_data++;
8012 if (ts & BE_IN_CONTROL)
8013 nr_be_in_control++;
8014
8015 TODO_SPEC (insn) &= ~BE_IN_SPEC;
8016 gcc_assert (!TODO_SPEC (insn));
8017
8018 DONE_SPEC (insn) |= ts;
8019
8020 /* First we convert all simple checks to branchy. */
8021 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
8022 sd_iterator_cond (&sd_it, &dep);)
8023 {
8024 rtx_insn *check = DEP_PRO (dep);
8025
8026 if (IS_SPECULATION_SIMPLE_CHECK_P (check))
8027 {
8028 create_check_block_twin (check, true);
8029
8030 /* Restart search. */
8031 sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
8032 }
8033 else
8034 /* Continue search. */
8035 sd_iterator_next (&sd_it);
8036 }
8037
8038 priorities_roots.create (0);
8039 clear_priorities (insn, &priorities_roots);
8040
8041 while (1)
8042 {
8043 rtx_insn *check, *twin;
8044 basic_block rec;
8045
8046 /* Get the first backward dependency of INSN. */
8047 sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
8048 if (!sd_iterator_cond (&sd_it, &dep))
8049 /* INSN has no backward dependencies left. */
8050 break;
8051
8052 gcc_assert ((DEP_STATUS (dep) & BEGIN_SPEC) == 0
8053 && (DEP_STATUS (dep) & BE_IN_SPEC) != 0
8054 && (DEP_STATUS (dep) & DEP_TYPES) == DEP_TRUE);
8055
8056 check = DEP_PRO (dep);
8057
8058 gcc_assert (!IS_SPECULATION_CHECK_P (check) && !ORIG_PAT (check)
8059 && QUEUE_INDEX (check) == QUEUE_NOWHERE);
8060
8061 rec = BLOCK_FOR_INSN (check);
8062
8063 twin = emit_insn_before (copy_insn (PATTERN (insn)), BB_END (rec));
8064 haifa_init_insn (twin);
8065
8066 sd_copy_back_deps (twin, insn, true);
8067
8068 if (sched_verbose && spec_info->dump)
8069 /* INSN_BB (insn) isn't determined for twin insns yet.
8070 So we can't use current_sched_info->print_insn. */
8071 fprintf (spec_info->dump, ";;\t\tGenerated twin insn : %d/rec%d\n",
8072 INSN_UID (twin), rec->index);
8073
8074 twins = alloc_INSN_LIST (twin, twins);
8075
8076 /* Add dependences between TWIN and all appropriate
8077 instructions from REC. */
8078 FOR_EACH_DEP (insn, SD_LIST_SPEC_BACK, sd_it, dep)
8079 {
8080 rtx_insn *pro = DEP_PRO (dep);
8081
8082 gcc_assert (DEP_TYPE (dep) == REG_DEP_TRUE);
8083
8084 /* INSN might have dependencies from the instructions from
8085 several recovery blocks. At this iteration we process those
8086 producers that reside in REC. */
8087 if (BLOCK_FOR_INSN (pro) == rec)
8088 {
8089 dep_def _new_dep, *new_dep = &_new_dep;
8090
8091 init_dep (new_dep, pro, twin, REG_DEP_TRUE);
8092 sd_add_dep (new_dep, false);
8093 }
8094 }
8095
8096 process_insn_forw_deps_be_in_spec (insn, twin, ts);
8097
8098 /* Remove all dependencies between INSN and insns in REC. */
8099 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
8100 sd_iterator_cond (&sd_it, &dep);)
8101 {
8102 rtx_insn *pro = DEP_PRO (dep);
8103
8104 if (BLOCK_FOR_INSN (pro) == rec)
8105 sd_delete_dep (sd_it);
8106 else
8107 sd_iterator_next (&sd_it);
8108 }
8109 }
8110
8111 /* We couldn't have added the dependencies between INSN and TWINS earlier
8112 because that would make TWINS appear in the INSN_BACK_DEPS (INSN). */
8113 while (twins)
8114 {
8115 rtx_insn *twin;
8116 rtx_insn_list *next_node;
8117
8118 twin = twins->insn ();
8119
8120 {
8121 dep_def _new_dep, *new_dep = &_new_dep;
8122
8123 init_dep (new_dep, insn, twin, REG_DEP_OUTPUT);
8124 sd_add_dep (new_dep, false);
8125 }
8126
8127 next_node = twins->next ();
8128 free_INSN_LIST_node (twins);
8129 twins = next_node;
8130 }
8131
8132 calc_priorities (priorities_roots);
8133 priorities_roots.release ();
8134 }
8135
8136 /* Extends and fills with zeros (only the new part) array pointed to by P. */
8137 void *
8138 xrecalloc (void *p, size_t new_nmemb, size_t old_nmemb, size_t size)
8139 {
8140 gcc_assert (new_nmemb >= old_nmemb);
8141 p = XRESIZEVAR (void, p, new_nmemb * size);
8142 memset (((char *) p) + old_nmemb * size, 0, (new_nmemb - old_nmemb) * size);
8143 return p;
8144 }
8145
8146 /* Helper function.
8147 Find fallthru edge from PRED. */
8148 edge
8149 find_fallthru_edge_from (basic_block pred)
8150 {
8151 edge e;
8152 basic_block succ;
8153
8154 succ = pred->next_bb;
8155 gcc_assert (succ->prev_bb == pred);
8156
8157 if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
8158 {
8159 e = find_fallthru_edge (pred->succs);
8160
8161 if (e)
8162 {
8163 gcc_assert (e->dest == succ);
8164 return e;
8165 }
8166 }
8167 else
8168 {
8169 e = find_fallthru_edge (succ->preds);
8170
8171 if (e)
8172 {
8173 gcc_assert (e->src == pred);
8174 return e;
8175 }
8176 }
8177
8178 return NULL;
8179 }
8180
8181 /* Extend per basic block data structures. */
8182 static void
8183 sched_extend_bb (void)
8184 {
8185 /* The following is done to keep current_sched_info->next_tail non null. */
8186 rtx_insn *end = BB_END (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
8187 rtx_insn *insn = DEBUG_INSN_P (end) ? prev_nondebug_insn (end) : end;
8188 if (NEXT_INSN (end) == 0
8189 || (!NOTE_P (insn)
8190 && !LABEL_P (insn)
8191 /* Don't emit a NOTE if it would end up before a BARRIER. */
8192 && !BARRIER_P (NEXT_INSN (end))))
8193 {
8194 rtx_note *note = emit_note_after (NOTE_INSN_DELETED, end);
8195 /* Make note appear outside BB. */
8196 set_block_for_insn (note, NULL);
8197 BB_END (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb) = end;
8198 }
8199 }
8200
8201 /* Init per basic block data structures. */
8202 void
8203 sched_init_bbs (void)
8204 {
8205 sched_extend_bb ();
8206 }
8207
8208 /* Initialize BEFORE_RECOVERY variable. */
8209 static void
8210 init_before_recovery (basic_block *before_recovery_ptr)
8211 {
8212 basic_block last;
8213 edge e;
8214
8215 last = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
8216 e = find_fallthru_edge_from (last);
8217
8218 if (e)
8219 {
8220 /* We create two basic blocks:
8221 1. Single instruction block is inserted right after E->SRC
8222 and has jump to
8223 2. Empty block right before EXIT_BLOCK.
8224 Between these two blocks recovery blocks will be emitted. */
8225
8226 basic_block single, empty;
8227
8228 /* If the fallthrough edge to exit we've found is from the block we've
8229 created before, don't do anything more. */
8230 if (last == after_recovery)
8231 return;
8232
8233 adding_bb_to_current_region_p = false;
8234
8235 single = sched_create_empty_bb (last);
8236 empty = sched_create_empty_bb (single);
8237
8238 /* Add new blocks to the root loop. */
8239 if (current_loops != NULL)
8240 {
8241 add_bb_to_loop (single, (*current_loops->larray)[0]);
8242 add_bb_to_loop (empty, (*current_loops->larray)[0]);
8243 }
8244
8245 single->count = last->count;
8246 empty->count = last->count;
8247 single->frequency = last->frequency;
8248 empty->frequency = last->frequency;
8249 BB_COPY_PARTITION (single, last);
8250 BB_COPY_PARTITION (empty, last);
8251
8252 redirect_edge_succ (e, single);
8253 make_single_succ_edge (single, empty, 0);
8254 make_single_succ_edge (empty, EXIT_BLOCK_PTR_FOR_FN (cfun),
8255 EDGE_FALLTHRU);
8256
8257 rtx_code_label *label = block_label (empty);
8258 rtx_jump_insn *x = emit_jump_insn_after (targetm.gen_jump (label),
8259 BB_END (single));
8260 JUMP_LABEL (x) = label;
8261 LABEL_NUSES (label)++;
8262 haifa_init_insn (x);
8263
8264 emit_barrier_after (x);
8265
8266 sched_init_only_bb (empty, NULL);
8267 sched_init_only_bb (single, NULL);
8268 sched_extend_bb ();
8269
8270 adding_bb_to_current_region_p = true;
8271 before_recovery = single;
8272 after_recovery = empty;
8273
8274 if (before_recovery_ptr)
8275 *before_recovery_ptr = before_recovery;
8276
8277 if (sched_verbose >= 2 && spec_info->dump)
8278 fprintf (spec_info->dump,
8279 ";;\t\tFixed fallthru to EXIT : %d->>%d->%d->>EXIT\n",
8280 last->index, single->index, empty->index);
8281 }
8282 else
8283 before_recovery = last;
8284 }
8285
8286 /* Returns new recovery block. */
8287 basic_block
8288 sched_create_recovery_block (basic_block *before_recovery_ptr)
8289 {
8290 rtx_insn *barrier;
8291 basic_block rec;
8292
8293 haifa_recovery_bb_recently_added_p = true;
8294 haifa_recovery_bb_ever_added_p = true;
8295
8296 init_before_recovery (before_recovery_ptr);
8297
8298 barrier = get_last_bb_insn (before_recovery);
8299 gcc_assert (BARRIER_P (barrier));
8300
8301 rtx_insn *label = emit_label_after (gen_label_rtx (), barrier);
8302
8303 rec = create_basic_block (label, label, before_recovery);
8304
8305 /* A recovery block always ends with an unconditional jump. */
8306 emit_barrier_after (BB_END (rec));
8307
8308 if (BB_PARTITION (before_recovery) != BB_UNPARTITIONED)
8309 BB_SET_PARTITION (rec, BB_COLD_PARTITION);
8310
8311 if (sched_verbose && spec_info->dump)
8312 fprintf (spec_info->dump, ";;\t\tGenerated recovery block rec%d\n",
8313 rec->index);
8314
8315 return rec;
8316 }
8317
8318 /* Create edges: FIRST_BB -> REC; FIRST_BB -> SECOND_BB; REC -> SECOND_BB
8319 and emit necessary jumps. */
8320 void
8321 sched_create_recovery_edges (basic_block first_bb, basic_block rec,
8322 basic_block second_bb)
8323 {
8324 int edge_flags;
8325
8326 /* This is fixing of incoming edge. */
8327 /* ??? Which other flags should be specified? */
8328 if (BB_PARTITION (first_bb) != BB_PARTITION (rec))
8329 /* Partition type is the same, if it is "unpartitioned". */
8330 edge_flags = EDGE_CROSSING;
8331 else
8332 edge_flags = 0;
8333
8334 make_edge (first_bb, rec, edge_flags);
8335 rtx_code_label *label = block_label (second_bb);
8336 rtx_jump_insn *jump = emit_jump_insn_after (targetm.gen_jump (label),
8337 BB_END (rec));
8338 JUMP_LABEL (jump) = label;
8339 LABEL_NUSES (label)++;
8340
8341 if (BB_PARTITION (second_bb) != BB_PARTITION (rec))
8342 /* Partition type is the same, if it is "unpartitioned". */
8343 {
8344 /* Rewritten from cfgrtl.c. */
8345 if (flag_reorder_blocks_and_partition
8346 && targetm_common.have_named_sections)
8347 {
8348 /* We don't need the same note for the check because
8349 any_condjump_p (check) == true. */
8350 CROSSING_JUMP_P (jump) = 1;
8351 }
8352 edge_flags = EDGE_CROSSING;
8353 }
8354 else
8355 edge_flags = 0;
8356
8357 make_single_succ_edge (rec, second_bb, edge_flags);
8358 if (dom_info_available_p (CDI_DOMINATORS))
8359 set_immediate_dominator (CDI_DOMINATORS, rec, first_bb);
8360 }
8361
8362 /* This function creates recovery code for INSN. If MUTATE_P is nonzero,
8363 INSN is a simple check, that should be converted to branchy one. */
8364 static void
8365 create_check_block_twin (rtx_insn *insn, bool mutate_p)
8366 {
8367 basic_block rec;
8368 rtx_insn *label, *check, *twin;
8369 rtx check_pat;
8370 ds_t fs;
8371 sd_iterator_def sd_it;
8372 dep_t dep;
8373 dep_def _new_dep, *new_dep = &_new_dep;
8374 ds_t todo_spec;
8375
8376 gcc_assert (ORIG_PAT (insn) != NULL_RTX);
8377
8378 if (!mutate_p)
8379 todo_spec = TODO_SPEC (insn);
8380 else
8381 {
8382 gcc_assert (IS_SPECULATION_SIMPLE_CHECK_P (insn)
8383 && (TODO_SPEC (insn) & SPECULATIVE) == 0);
8384
8385 todo_spec = CHECK_SPEC (insn);
8386 }
8387
8388 todo_spec &= SPECULATIVE;
8389
8390 /* Create recovery block. */
8391 if (mutate_p || targetm.sched.needs_block_p (todo_spec))
8392 {
8393 rec = sched_create_recovery_block (NULL);
8394 label = BB_HEAD (rec);
8395 }
8396 else
8397 {
8398 rec = EXIT_BLOCK_PTR_FOR_FN (cfun);
8399 label = NULL;
8400 }
8401
8402 /* Emit CHECK. */
8403 check_pat = targetm.sched.gen_spec_check (insn, label, todo_spec);
8404
8405 if (rec != EXIT_BLOCK_PTR_FOR_FN (cfun))
8406 {
8407 /* To have mem_reg alive at the beginning of second_bb,
8408 we emit check BEFORE insn, so insn after splitting
8409 insn will be at the beginning of second_bb, which will
8410 provide us with the correct life information. */
8411 check = emit_jump_insn_before (check_pat, insn);
8412 JUMP_LABEL (check) = label;
8413 LABEL_NUSES (label)++;
8414 }
8415 else
8416 check = emit_insn_before (check_pat, insn);
8417
8418 /* Extend data structures. */
8419 haifa_init_insn (check);
8420
8421 /* CHECK is being added to current region. Extend ready list. */
8422 gcc_assert (sched_ready_n_insns != -1);
8423 sched_extend_ready_list (sched_ready_n_insns + 1);
8424
8425 if (current_sched_info->add_remove_insn)
8426 current_sched_info->add_remove_insn (insn, 0);
8427
8428 RECOVERY_BLOCK (check) = rec;
8429
8430 if (sched_verbose && spec_info->dump)
8431 fprintf (spec_info->dump, ";;\t\tGenerated check insn : %s\n",
8432 (*current_sched_info->print_insn) (check, 0));
8433
8434 gcc_assert (ORIG_PAT (insn));
8435
8436 /* Initialize TWIN (twin is a duplicate of original instruction
8437 in the recovery block). */
8438 if (rec != EXIT_BLOCK_PTR_FOR_FN (cfun))
8439 {
8440 sd_iterator_def sd_it;
8441 dep_t dep;
8442
8443 FOR_EACH_DEP (insn, SD_LIST_RES_BACK, sd_it, dep)
8444 if ((DEP_STATUS (dep) & DEP_OUTPUT) != 0)
8445 {
8446 struct _dep _dep2, *dep2 = &_dep2;
8447
8448 init_dep (dep2, DEP_PRO (dep), check, REG_DEP_TRUE);
8449
8450 sd_add_dep (dep2, true);
8451 }
8452
8453 twin = emit_insn_after (ORIG_PAT (insn), BB_END (rec));
8454 haifa_init_insn (twin);
8455
8456 if (sched_verbose && spec_info->dump)
8457 /* INSN_BB (insn) isn't determined for twin insns yet.
8458 So we can't use current_sched_info->print_insn. */
8459 fprintf (spec_info->dump, ";;\t\tGenerated twin insn : %d/rec%d\n",
8460 INSN_UID (twin), rec->index);
8461 }
8462 else
8463 {
8464 ORIG_PAT (check) = ORIG_PAT (insn);
8465 HAS_INTERNAL_DEP (check) = 1;
8466 twin = check;
8467 /* ??? We probably should change all OUTPUT dependencies to
8468 (TRUE | OUTPUT). */
8469 }
8470
8471 /* Copy all resolved back dependencies of INSN to TWIN. This will
8472 provide correct value for INSN_TICK (TWIN). */
8473 sd_copy_back_deps (twin, insn, true);
8474
8475 if (rec != EXIT_BLOCK_PTR_FOR_FN (cfun))
8476 /* In case of branchy check, fix CFG. */
8477 {
8478 basic_block first_bb, second_bb;
8479 rtx_insn *jump;
8480
8481 first_bb = BLOCK_FOR_INSN (check);
8482 second_bb = sched_split_block (first_bb, check);
8483
8484 sched_create_recovery_edges (first_bb, rec, second_bb);
8485
8486 sched_init_only_bb (second_bb, first_bb);
8487 sched_init_only_bb (rec, EXIT_BLOCK_PTR_FOR_FN (cfun));
8488
8489 jump = BB_END (rec);
8490 haifa_init_insn (jump);
8491 }
8492
8493 /* Move backward dependences from INSN to CHECK and
8494 move forward dependences from INSN to TWIN. */
8495
8496 /* First, create dependencies between INSN's producers and CHECK & TWIN. */
8497 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
8498 {
8499 rtx_insn *pro = DEP_PRO (dep);
8500 ds_t ds;
8501
8502 /* If BEGIN_DATA: [insn ~~TRUE~~> producer]:
8503 check --TRUE--> producer ??? or ANTI ???
8504 twin --TRUE--> producer
8505 twin --ANTI--> check
8506
8507 If BEGIN_CONTROL: [insn ~~ANTI~~> producer]:
8508 check --ANTI--> producer
8509 twin --ANTI--> producer
8510 twin --ANTI--> check
8511
8512 If BE_IN_SPEC: [insn ~~TRUE~~> producer]:
8513 check ~~TRUE~~> producer
8514 twin ~~TRUE~~> producer
8515 twin --ANTI--> check */
8516
8517 ds = DEP_STATUS (dep);
8518
8519 if (ds & BEGIN_SPEC)
8520 {
8521 gcc_assert (!mutate_p);
8522 ds &= ~BEGIN_SPEC;
8523 }
8524
8525 init_dep_1 (new_dep, pro, check, DEP_TYPE (dep), ds);
8526 sd_add_dep (new_dep, false);
8527
8528 if (rec != EXIT_BLOCK_PTR_FOR_FN (cfun))
8529 {
8530 DEP_CON (new_dep) = twin;
8531 sd_add_dep (new_dep, false);
8532 }
8533 }
8534
8535 /* Second, remove backward dependencies of INSN. */
8536 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
8537 sd_iterator_cond (&sd_it, &dep);)
8538 {
8539 if ((DEP_STATUS (dep) & BEGIN_SPEC)
8540 || mutate_p)
8541 /* We can delete this dep because we overcome it with
8542 BEGIN_SPECULATION. */
8543 sd_delete_dep (sd_it);
8544 else
8545 sd_iterator_next (&sd_it);
8546 }
8547
8548 /* Future Speculations. Determine what BE_IN speculations will be like. */
8549 fs = 0;
8550
8551 /* Fields (DONE_SPEC (x) & BEGIN_SPEC) and CHECK_SPEC (x) are set only
8552 here. */
8553
8554 gcc_assert (!DONE_SPEC (insn));
8555
8556 if (!mutate_p)
8557 {
8558 ds_t ts = TODO_SPEC (insn);
8559
8560 DONE_SPEC (insn) = ts & BEGIN_SPEC;
8561 CHECK_SPEC (check) = ts & BEGIN_SPEC;
8562
8563 /* Luckiness of future speculations solely depends upon initial
8564 BEGIN speculation. */
8565 if (ts & BEGIN_DATA)
8566 fs = set_dep_weak (fs, BE_IN_DATA, get_dep_weak (ts, BEGIN_DATA));
8567 if (ts & BEGIN_CONTROL)
8568 fs = set_dep_weak (fs, BE_IN_CONTROL,
8569 get_dep_weak (ts, BEGIN_CONTROL));
8570 }
8571 else
8572 CHECK_SPEC (check) = CHECK_SPEC (insn);
8573
8574 /* Future speculations: call the helper. */
8575 process_insn_forw_deps_be_in_spec (insn, twin, fs);
8576
8577 if (rec != EXIT_BLOCK_PTR_FOR_FN (cfun))
8578 {
8579 /* Which types of dependencies should we use here is,
8580 generally, machine-dependent question... But, for now,
8581 it is not. */
8582
8583 if (!mutate_p)
8584 {
8585 init_dep (new_dep, insn, check, REG_DEP_TRUE);
8586 sd_add_dep (new_dep, false);
8587
8588 init_dep (new_dep, insn, twin, REG_DEP_OUTPUT);
8589 sd_add_dep (new_dep, false);
8590 }
8591 else
8592 {
8593 if (spec_info->dump)
8594 fprintf (spec_info->dump, ";;\t\tRemoved simple check : %s\n",
8595 (*current_sched_info->print_insn) (insn, 0));
8596
8597 /* Remove all dependencies of the INSN. */
8598 {
8599 sd_it = sd_iterator_start (insn, (SD_LIST_FORW
8600 | SD_LIST_BACK
8601 | SD_LIST_RES_BACK));
8602 while (sd_iterator_cond (&sd_it, &dep))
8603 sd_delete_dep (sd_it);
8604 }
8605
8606 /* If former check (INSN) already was moved to the ready (or queue)
8607 list, add new check (CHECK) there too. */
8608 if (QUEUE_INDEX (insn) != QUEUE_NOWHERE)
8609 try_ready (check);
8610
8611 /* Remove old check from instruction stream and free its
8612 data. */
8613 sched_remove_insn (insn);
8614 }
8615
8616 init_dep (new_dep, check, twin, REG_DEP_ANTI);
8617 sd_add_dep (new_dep, false);
8618 }
8619 else
8620 {
8621 init_dep_1 (new_dep, insn, check, REG_DEP_TRUE, DEP_TRUE | DEP_OUTPUT);
8622 sd_add_dep (new_dep, false);
8623 }
8624
8625 if (!mutate_p)
8626 /* Fix priorities. If MUTATE_P is nonzero, this is not necessary,
8627 because it'll be done later in add_to_speculative_block. */
8628 {
8629 rtx_vec_t priorities_roots = rtx_vec_t ();
8630
8631 clear_priorities (twin, &priorities_roots);
8632 calc_priorities (priorities_roots);
8633 priorities_roots.release ();
8634 }
8635 }
8636
8637 /* Removes dependency between instructions in the recovery block REC
8638 and usual region instructions. It keeps inner dependences so it
8639 won't be necessary to recompute them. */
8640 static void
8641 fix_recovery_deps (basic_block rec)
8642 {
8643 rtx_insn *note, *insn, *jump;
8644 rtx_insn_list *ready_list = 0;
8645 bitmap_head in_ready;
8646 rtx_insn_list *link;
8647
8648 bitmap_initialize (&in_ready, 0);
8649
8650 /* NOTE - a basic block note. */
8651 note = NEXT_INSN (BB_HEAD (rec));
8652 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
8653 insn = BB_END (rec);
8654 gcc_assert (JUMP_P (insn));
8655 insn = PREV_INSN (insn);
8656
8657 do
8658 {
8659 sd_iterator_def sd_it;
8660 dep_t dep;
8661
8662 for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
8663 sd_iterator_cond (&sd_it, &dep);)
8664 {
8665 rtx_insn *consumer = DEP_CON (dep);
8666
8667 if (BLOCK_FOR_INSN (consumer) != rec)
8668 {
8669 sd_delete_dep (sd_it);
8670
8671 if (bitmap_set_bit (&in_ready, INSN_LUID (consumer)))
8672 ready_list = alloc_INSN_LIST (consumer, ready_list);
8673 }
8674 else
8675 {
8676 gcc_assert ((DEP_STATUS (dep) & DEP_TYPES) == DEP_TRUE);
8677
8678 sd_iterator_next (&sd_it);
8679 }
8680 }
8681
8682 insn = PREV_INSN (insn);
8683 }
8684 while (insn != note);
8685
8686 bitmap_clear (&in_ready);
8687
8688 /* Try to add instructions to the ready or queue list. */
8689 for (link = ready_list; link; link = link->next ())
8690 try_ready (link->insn ());
8691 free_INSN_LIST_list (&ready_list);
8692
8693 /* Fixing jump's dependences. */
8694 insn = BB_HEAD (rec);
8695 jump = BB_END (rec);
8696
8697 gcc_assert (LABEL_P (insn));
8698 insn = NEXT_INSN (insn);
8699
8700 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
8701 add_jump_dependencies (insn, jump);
8702 }
8703
8704 /* Change pattern of INSN to NEW_PAT. Invalidate cached haifa
8705 instruction data. */
8706 static bool
8707 haifa_change_pattern (rtx_insn *insn, rtx new_pat)
8708 {
8709 int t;
8710
8711 t = validate_change (insn, &PATTERN (insn), new_pat, 0);
8712 if (!t)
8713 return false;
8714
8715 update_insn_after_change (insn);
8716 return true;
8717 }
8718
8719 /* -1 - can't speculate,
8720 0 - for speculation with REQUEST mode it is OK to use
8721 current instruction pattern,
8722 1 - need to change pattern for *NEW_PAT to be speculative. */
8723 int
8724 sched_speculate_insn (rtx_insn *insn, ds_t request, rtx *new_pat)
8725 {
8726 gcc_assert (current_sched_info->flags & DO_SPECULATION
8727 && (request & SPECULATIVE)
8728 && sched_insn_is_legitimate_for_speculation_p (insn, request));
8729
8730 if ((request & spec_info->mask) != request)
8731 return -1;
8732
8733 if (request & BE_IN_SPEC
8734 && !(request & BEGIN_SPEC))
8735 return 0;
8736
8737 return targetm.sched.speculate_insn (insn, request, new_pat);
8738 }
8739
8740 static int
8741 haifa_speculate_insn (rtx_insn *insn, ds_t request, rtx *new_pat)
8742 {
8743 gcc_assert (sched_deps_info->generate_spec_deps
8744 && !IS_SPECULATION_CHECK_P (insn));
8745
8746 if (HAS_INTERNAL_DEP (insn)
8747 || SCHED_GROUP_P (insn))
8748 return -1;
8749
8750 return sched_speculate_insn (insn, request, new_pat);
8751 }
8752
8753 /* Print some information about block BB, which starts with HEAD and
8754 ends with TAIL, before scheduling it.
8755 I is zero, if scheduler is about to start with the fresh ebb. */
8756 static void
8757 dump_new_block_header (int i, basic_block bb, rtx_insn *head, rtx_insn *tail)
8758 {
8759 if (!i)
8760 fprintf (sched_dump,
8761 ";; ======================================================\n");
8762 else
8763 fprintf (sched_dump,
8764 ";; =====================ADVANCING TO=====================\n");
8765 fprintf (sched_dump,
8766 ";; -- basic block %d from %d to %d -- %s reload\n",
8767 bb->index, INSN_UID (head), INSN_UID (tail),
8768 (reload_completed ? "after" : "before"));
8769 fprintf (sched_dump,
8770 ";; ======================================================\n");
8771 fprintf (sched_dump, "\n");
8772 }
8773
8774 /* Unlink basic block notes and labels and saves them, so they
8775 can be easily restored. We unlink basic block notes in EBB to
8776 provide back-compatibility with the previous code, as target backends
8777 assume, that there'll be only instructions between
8778 current_sched_info->{head and tail}. We restore these notes as soon
8779 as we can.
8780 FIRST (LAST) is the first (last) basic block in the ebb.
8781 NB: In usual case (FIRST == LAST) nothing is really done. */
8782 void
8783 unlink_bb_notes (basic_block first, basic_block last)
8784 {
8785 /* We DON'T unlink basic block notes of the first block in the ebb. */
8786 if (first == last)
8787 return;
8788
8789 bb_header = XNEWVEC (rtx_insn *, last_basic_block_for_fn (cfun));
8790
8791 /* Make a sentinel. */
8792 if (last->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
8793 bb_header[last->next_bb->index] = 0;
8794
8795 first = first->next_bb;
8796 do
8797 {
8798 rtx_insn *prev, *label, *note, *next;
8799
8800 label = BB_HEAD (last);
8801 if (LABEL_P (label))
8802 note = NEXT_INSN (label);
8803 else
8804 note = label;
8805 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
8806
8807 prev = PREV_INSN (label);
8808 next = NEXT_INSN (note);
8809 gcc_assert (prev && next);
8810
8811 SET_NEXT_INSN (prev) = next;
8812 SET_PREV_INSN (next) = prev;
8813
8814 bb_header[last->index] = label;
8815
8816 if (last == first)
8817 break;
8818
8819 last = last->prev_bb;
8820 }
8821 while (1);
8822 }
8823
8824 /* Restore basic block notes.
8825 FIRST is the first basic block in the ebb. */
8826 static void
8827 restore_bb_notes (basic_block first)
8828 {
8829 if (!bb_header)
8830 return;
8831
8832 /* We DON'T unlink basic block notes of the first block in the ebb. */
8833 first = first->next_bb;
8834 /* Remember: FIRST is actually a second basic block in the ebb. */
8835
8836 while (first != EXIT_BLOCK_PTR_FOR_FN (cfun)
8837 && bb_header[first->index])
8838 {
8839 rtx_insn *prev, *label, *note, *next;
8840
8841 label = bb_header[first->index];
8842 prev = PREV_INSN (label);
8843 next = NEXT_INSN (prev);
8844
8845 if (LABEL_P (label))
8846 note = NEXT_INSN (label);
8847 else
8848 note = label;
8849 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
8850
8851 bb_header[first->index] = 0;
8852
8853 SET_NEXT_INSN (prev) = label;
8854 SET_NEXT_INSN (note) = next;
8855 SET_PREV_INSN (next) = note;
8856
8857 first = first->next_bb;
8858 }
8859
8860 free (bb_header);
8861 bb_header = 0;
8862 }
8863
8864 /* Helper function.
8865 Fix CFG after both in- and inter-block movement of
8866 control_flow_insn_p JUMP. */
8867 static void
8868 fix_jump_move (rtx_insn *jump)
8869 {
8870 basic_block bb, jump_bb, jump_bb_next;
8871
8872 bb = BLOCK_FOR_INSN (PREV_INSN (jump));
8873 jump_bb = BLOCK_FOR_INSN (jump);
8874 jump_bb_next = jump_bb->next_bb;
8875
8876 gcc_assert (common_sched_info->sched_pass_id == SCHED_EBB_PASS
8877 || IS_SPECULATION_BRANCHY_CHECK_P (jump));
8878
8879 if (!NOTE_INSN_BASIC_BLOCK_P (BB_END (jump_bb_next)))
8880 /* if jump_bb_next is not empty. */
8881 BB_END (jump_bb) = BB_END (jump_bb_next);
8882
8883 if (BB_END (bb) != PREV_INSN (jump))
8884 /* Then there are instruction after jump that should be placed
8885 to jump_bb_next. */
8886 BB_END (jump_bb_next) = BB_END (bb);
8887 else
8888 /* Otherwise jump_bb_next is empty. */
8889 BB_END (jump_bb_next) = NEXT_INSN (BB_HEAD (jump_bb_next));
8890
8891 /* To make assertion in move_insn happy. */
8892 BB_END (bb) = PREV_INSN (jump);
8893
8894 update_bb_for_insn (jump_bb_next);
8895 }
8896
8897 /* Fix CFG after interblock movement of control_flow_insn_p JUMP. */
8898 static void
8899 move_block_after_check (rtx_insn *jump)
8900 {
8901 basic_block bb, jump_bb, jump_bb_next;
8902 vec<edge, va_gc> *t;
8903
8904 bb = BLOCK_FOR_INSN (PREV_INSN (jump));
8905 jump_bb = BLOCK_FOR_INSN (jump);
8906 jump_bb_next = jump_bb->next_bb;
8907
8908 update_bb_for_insn (jump_bb);
8909
8910 gcc_assert (IS_SPECULATION_CHECK_P (jump)
8911 || IS_SPECULATION_CHECK_P (BB_END (jump_bb_next)));
8912
8913 unlink_block (jump_bb_next);
8914 link_block (jump_bb_next, bb);
8915
8916 t = bb->succs;
8917 bb->succs = 0;
8918 move_succs (&(jump_bb->succs), bb);
8919 move_succs (&(jump_bb_next->succs), jump_bb);
8920 move_succs (&t, jump_bb_next);
8921
8922 df_mark_solutions_dirty ();
8923
8924 common_sched_info->fix_recovery_cfg
8925 (bb->index, jump_bb->index, jump_bb_next->index);
8926 }
8927
8928 /* Helper function for move_block_after_check.
8929 This functions attaches edge vector pointed to by SUCCSP to
8930 block TO. */
8931 static void
8932 move_succs (vec<edge, va_gc> **succsp, basic_block to)
8933 {
8934 edge e;
8935 edge_iterator ei;
8936
8937 gcc_assert (to->succs == 0);
8938
8939 to->succs = *succsp;
8940
8941 FOR_EACH_EDGE (e, ei, to->succs)
8942 e->src = to;
8943
8944 *succsp = 0;
8945 }
8946
8947 /* Remove INSN from the instruction stream.
8948 INSN should have any dependencies. */
8949 static void
8950 sched_remove_insn (rtx_insn *insn)
8951 {
8952 sd_finish_insn (insn);
8953
8954 change_queue_index (insn, QUEUE_NOWHERE);
8955 current_sched_info->add_remove_insn (insn, 1);
8956 delete_insn (insn);
8957 }
8958
8959 /* Clear priorities of all instructions, that are forward dependent on INSN.
8960 Store in vector pointed to by ROOTS_PTR insns on which priority () should
8961 be invoked to initialize all cleared priorities. */
8962 static void
8963 clear_priorities (rtx_insn *insn, rtx_vec_t *roots_ptr)
8964 {
8965 sd_iterator_def sd_it;
8966 dep_t dep;
8967 bool insn_is_root_p = true;
8968
8969 gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
8970
8971 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
8972 {
8973 rtx_insn *pro = DEP_PRO (dep);
8974
8975 if (INSN_PRIORITY_STATUS (pro) >= 0
8976 && QUEUE_INDEX (insn) != QUEUE_SCHEDULED)
8977 {
8978 /* If DEP doesn't contribute to priority then INSN itself should
8979 be added to priority roots. */
8980 if (contributes_to_priority_p (dep))
8981 insn_is_root_p = false;
8982
8983 INSN_PRIORITY_STATUS (pro) = -1;
8984 clear_priorities (pro, roots_ptr);
8985 }
8986 }
8987
8988 if (insn_is_root_p)
8989 roots_ptr->safe_push (insn);
8990 }
8991
8992 /* Recompute priorities of instructions, whose priorities might have been
8993 changed. ROOTS is a vector of instructions whose priority computation will
8994 trigger initialization of all cleared priorities. */
8995 static void
8996 calc_priorities (rtx_vec_t roots)
8997 {
8998 int i;
8999 rtx_insn *insn;
9000
9001 FOR_EACH_VEC_ELT (roots, i, insn)
9002 priority (insn);
9003 }
9004
9005
9006 /* Add dependences between JUMP and other instructions in the recovery
9007 block. INSN is the first insn the recovery block. */
9008 static void
9009 add_jump_dependencies (rtx_insn *insn, rtx_insn *jump)
9010 {
9011 do
9012 {
9013 insn = NEXT_INSN (insn);
9014 if (insn == jump)
9015 break;
9016
9017 if (dep_list_size (insn, SD_LIST_FORW) == 0)
9018 {
9019 dep_def _new_dep, *new_dep = &_new_dep;
9020
9021 init_dep (new_dep, insn, jump, REG_DEP_ANTI);
9022 sd_add_dep (new_dep, false);
9023 }
9024 }
9025 while (1);
9026
9027 gcc_assert (!sd_lists_empty_p (jump, SD_LIST_BACK));
9028 }
9029
9030 /* Extend data structures for logical insn UID. */
9031 void
9032 sched_extend_luids (void)
9033 {
9034 int new_luids_max_uid = get_max_uid () + 1;
9035
9036 sched_luids.safe_grow_cleared (new_luids_max_uid);
9037 }
9038
9039 /* Initialize LUID for INSN. */
9040 void
9041 sched_init_insn_luid (rtx_insn *insn)
9042 {
9043 int i = INSN_P (insn) ? 1 : common_sched_info->luid_for_non_insn (insn);
9044 int luid;
9045
9046 if (i >= 0)
9047 {
9048 luid = sched_max_luid;
9049 sched_max_luid += i;
9050 }
9051 else
9052 luid = -1;
9053
9054 SET_INSN_LUID (insn, luid);
9055 }
9056
9057 /* Initialize luids for BBS.
9058 The hook common_sched_info->luid_for_non_insn () is used to determine
9059 if notes, labels, etc. need luids. */
9060 void
9061 sched_init_luids (bb_vec_t bbs)
9062 {
9063 int i;
9064 basic_block bb;
9065
9066 sched_extend_luids ();
9067 FOR_EACH_VEC_ELT (bbs, i, bb)
9068 {
9069 rtx_insn *insn;
9070
9071 FOR_BB_INSNS (bb, insn)
9072 sched_init_insn_luid (insn);
9073 }
9074 }
9075
9076 /* Free LUIDs. */
9077 void
9078 sched_finish_luids (void)
9079 {
9080 sched_luids.release ();
9081 sched_max_luid = 1;
9082 }
9083
9084 /* Return logical uid of INSN. Helpful while debugging. */
9085 int
9086 insn_luid (rtx_insn *insn)
9087 {
9088 return INSN_LUID (insn);
9089 }
9090
9091 /* Extend per insn data in the target. */
9092 void
9093 sched_extend_target (void)
9094 {
9095 if (targetm.sched.h_i_d_extended)
9096 targetm.sched.h_i_d_extended ();
9097 }
9098
9099 /* Extend global scheduler structures (those, that live across calls to
9100 schedule_block) to include information about just emitted INSN. */
9101 static void
9102 extend_h_i_d (void)
9103 {
9104 int reserve = (get_max_uid () + 1 - h_i_d.length ());
9105 if (reserve > 0
9106 && ! h_i_d.space (reserve))
9107 {
9108 h_i_d.safe_grow_cleared (3 * get_max_uid () / 2);
9109 sched_extend_target ();
9110 }
9111 }
9112
9113 /* Initialize h_i_d entry of the INSN with default values.
9114 Values, that are not explicitly initialized here, hold zero. */
9115 static void
9116 init_h_i_d (rtx_insn *insn)
9117 {
9118 if (INSN_LUID (insn) > 0)
9119 {
9120 INSN_COST (insn) = -1;
9121 QUEUE_INDEX (insn) = QUEUE_NOWHERE;
9122 INSN_TICK (insn) = INVALID_TICK;
9123 INSN_EXACT_TICK (insn) = INVALID_TICK;
9124 INTER_TICK (insn) = INVALID_TICK;
9125 TODO_SPEC (insn) = HARD_DEP;
9126 INSN_AUTOPREF_MULTIPASS_DATA (insn)[0].status
9127 = AUTOPREF_MULTIPASS_DATA_UNINITIALIZED;
9128 INSN_AUTOPREF_MULTIPASS_DATA (insn)[1].status
9129 = AUTOPREF_MULTIPASS_DATA_UNINITIALIZED;
9130 }
9131 }
9132
9133 /* Initialize haifa_insn_data for BBS. */
9134 void
9135 haifa_init_h_i_d (bb_vec_t bbs)
9136 {
9137 int i;
9138 basic_block bb;
9139
9140 extend_h_i_d ();
9141 FOR_EACH_VEC_ELT (bbs, i, bb)
9142 {
9143 rtx_insn *insn;
9144
9145 FOR_BB_INSNS (bb, insn)
9146 init_h_i_d (insn);
9147 }
9148 }
9149
9150 /* Finalize haifa_insn_data. */
9151 void
9152 haifa_finish_h_i_d (void)
9153 {
9154 int i;
9155 haifa_insn_data_t data;
9156 struct reg_use_data *use, *next;
9157
9158 FOR_EACH_VEC_ELT (h_i_d, i, data)
9159 {
9160 free (data->max_reg_pressure);
9161 free (data->reg_pressure);
9162 for (use = data->reg_use_list; use != NULL; use = next)
9163 {
9164 next = use->next_insn_use;
9165 free (use);
9166 }
9167 }
9168 h_i_d.release ();
9169 }
9170
9171 /* Init data for the new insn INSN. */
9172 static void
9173 haifa_init_insn (rtx_insn *insn)
9174 {
9175 gcc_assert (insn != NULL);
9176
9177 sched_extend_luids ();
9178 sched_init_insn_luid (insn);
9179 sched_extend_target ();
9180 sched_deps_init (false);
9181 extend_h_i_d ();
9182 init_h_i_d (insn);
9183
9184 if (adding_bb_to_current_region_p)
9185 {
9186 sd_init_insn (insn);
9187
9188 /* Extend dependency caches by one element. */
9189 extend_dependency_caches (1, false);
9190 }
9191 if (sched_pressure != SCHED_PRESSURE_NONE)
9192 init_insn_reg_pressure_info (insn);
9193 }
9194
9195 /* Init data for the new basic block BB which comes after AFTER. */
9196 static void
9197 haifa_init_only_bb (basic_block bb, basic_block after)
9198 {
9199 gcc_assert (bb != NULL);
9200
9201 sched_init_bbs ();
9202
9203 if (common_sched_info->add_block)
9204 /* This changes only data structures of the front-end. */
9205 common_sched_info->add_block (bb, after);
9206 }
9207
9208 /* A generic version of sched_split_block (). */
9209 basic_block
9210 sched_split_block_1 (basic_block first_bb, rtx after)
9211 {
9212 edge e;
9213
9214 e = split_block (first_bb, after);
9215 gcc_assert (e->src == first_bb);
9216
9217 /* sched_split_block emits note if *check == BB_END. Probably it
9218 is better to rip that note off. */
9219
9220 return e->dest;
9221 }
9222
9223 /* A generic version of sched_create_empty_bb (). */
9224 basic_block
9225 sched_create_empty_bb_1 (basic_block after)
9226 {
9227 return create_empty_bb (after);
9228 }
9229
9230 /* Insert PAT as an INSN into the schedule and update the necessary data
9231 structures to account for it. */
9232 rtx_insn *
9233 sched_emit_insn (rtx pat)
9234 {
9235 rtx_insn *insn = emit_insn_before (pat, first_nonscheduled_insn ());
9236 haifa_init_insn (insn);
9237
9238 if (current_sched_info->add_remove_insn)
9239 current_sched_info->add_remove_insn (insn, 0);
9240
9241 (*current_sched_info->begin_schedule_ready) (insn);
9242 scheduled_insns.safe_push (insn);
9243
9244 last_scheduled_insn = insn;
9245 return insn;
9246 }
9247
9248 /* This function returns a candidate satisfying dispatch constraints from
9249 the ready list. */
9250
9251 static rtx_insn *
9252 ready_remove_first_dispatch (struct ready_list *ready)
9253 {
9254 int i;
9255 rtx_insn *insn = ready_element (ready, 0);
9256
9257 if (ready->n_ready == 1
9258 || !INSN_P (insn)
9259 || INSN_CODE (insn) < 0
9260 || !active_insn_p (insn)
9261 || targetm.sched.dispatch (insn, FITS_DISPATCH_WINDOW))
9262 return ready_remove_first (ready);
9263
9264 for (i = 1; i < ready->n_ready; i++)
9265 {
9266 insn = ready_element (ready, i);
9267
9268 if (!INSN_P (insn)
9269 || INSN_CODE (insn) < 0
9270 || !active_insn_p (insn))
9271 continue;
9272
9273 if (targetm.sched.dispatch (insn, FITS_DISPATCH_WINDOW))
9274 {
9275 /* Return ith element of ready. */
9276 insn = ready_remove (ready, i);
9277 return insn;
9278 }
9279 }
9280
9281 if (targetm.sched.dispatch (NULL, DISPATCH_VIOLATION))
9282 return ready_remove_first (ready);
9283
9284 for (i = 1; i < ready->n_ready; i++)
9285 {
9286 insn = ready_element (ready, i);
9287
9288 if (!INSN_P (insn)
9289 || INSN_CODE (insn) < 0
9290 || !active_insn_p (insn))
9291 continue;
9292
9293 /* Return i-th element of ready. */
9294 if (targetm.sched.dispatch (insn, IS_CMP))
9295 return ready_remove (ready, i);
9296 }
9297
9298 return ready_remove_first (ready);
9299 }
9300
9301 /* Get number of ready insn in the ready list. */
9302
9303 int
9304 number_in_ready (void)
9305 {
9306 return ready.n_ready;
9307 }
9308
9309 /* Get number of ready's in the ready list. */
9310
9311 rtx_insn *
9312 get_ready_element (int i)
9313 {
9314 return ready_element (&ready, i);
9315 }
9316
9317 #endif /* INSN_SCHEDULING */