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