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