tree-data-ref.c (subscript_dependence_tester_1): Call free_conflict_function.
[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 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
5 and currently maintained by, Jim Wilson (wilson@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 /* Instruction scheduling pass. This file, along with sched-deps.c,
24 contains the generic parts. The actual entry point is found for
25 the normal instruction scheduling pass is found in sched-rgn.c.
26
27 We compute insn priorities based on data dependencies. Flow
28 analysis only creates a fraction of the data-dependencies we must
29 observe: namely, only those dependencies which the combiner can be
30 expected to use. For this pass, we must therefore create the
31 remaining dependencies we need to observe: register dependencies,
32 memory dependencies, dependencies to keep function calls in order,
33 and the dependence between a conditional branch and the setting of
34 condition codes are all dealt with here.
35
36 The scheduler first traverses the data flow graph, starting with
37 the last instruction, and proceeding to the first, assigning values
38 to insn_priority as it goes. This sorts the instructions
39 topologically by data dependence.
40
41 Once priorities have been established, we order the insns using
42 list scheduling. This works as follows: starting with a list of
43 all the ready insns, and sorted according to priority number, we
44 schedule the insn from the end of the list by placing its
45 predecessors in the list according to their priority order. We
46 consider this insn scheduled by setting the pointer to the "end" of
47 the list to point to the previous insn. When an insn has no
48 predecessors, we either queue it until sufficient time has elapsed
49 or add it to the ready list. As the instructions are scheduled or
50 when stalls are introduced, the queue advances and dumps insns into
51 the ready list. When all insns down to the lowest priority have
52 been scheduled, the critical path of the basic block has been made
53 as short as possible. The remaining insns are then scheduled in
54 remaining slots.
55
56 The following list shows the order in which we want to break ties
57 among insns in the ready list:
58
59 1. choose insn with the longest path to end of bb, ties
60 broken by
61 2. choose insn with least contribution to register pressure,
62 ties broken by
63 3. prefer in-block upon interblock motion, ties broken by
64 4. prefer useful upon speculative motion, ties broken by
65 5. choose insn with largest control flow probability, ties
66 broken by
67 6. choose insn with the least dependences upon the previously
68 scheduled insn, or finally
69 7 choose the insn which has the most insns dependent on it.
70 8. choose insn with lowest UID.
71
72 Memory references complicate matters. Only if we can be certain
73 that memory references are not part of the data dependency graph
74 (via true, anti, or output dependence), can we move operations past
75 memory references. To first approximation, reads can be done
76 independently, while writes introduce dependencies. Better
77 approximations will yield fewer dependencies.
78
79 Before reload, an extended analysis of interblock data dependences
80 is required for interblock scheduling. This is performed in
81 compute_block_backward_dependences ().
82
83 Dependencies set up by memory references are treated in exactly the
84 same way as other dependencies, by using insn backward dependences
85 INSN_BACK_DEPS. INSN_BACK_DEPS are translated into forward dependences
86 INSN_FORW_DEPS the purpose of forward list scheduling.
87
88 Having optimized the critical path, we may have also unduly
89 extended the lifetimes of some registers. If an operation requires
90 that constants be loaded into registers, it is certainly desirable
91 to load those constants as early as necessary, but no earlier.
92 I.e., it will not do to load up a bunch of registers at the
93 beginning of a basic block only to use them at the end, if they
94 could be loaded later, since this may result in excessive register
95 utilization.
96
97 Note that since branches are never in basic blocks, but only end
98 basic blocks, this pass will not move branches. But that is ok,
99 since we can use GNU's delayed branch scheduling pass to take care
100 of this case.
101
102 Also note that no further optimizations based on algebraic
103 identities are performed, so this pass would be a good one to
104 perform instruction splitting, such as breaking up a multiply
105 instruction into shifts and adds where that is profitable.
106
107 Given the memory aliasing analysis that this pass should perform,
108 it should be possible to remove redundant stores to memory, and to
109 load values from registers instead of hitting memory.
110
111 Before reload, speculative insns are moved only if a 'proof' exists
112 that no exception will be caused by this, and if no live registers
113 exist that inhibit the motion (live registers constraints are not
114 represented by data dependence edges).
115
116 This pass must update information that subsequent passes expect to
117 be correct. Namely: reg_n_refs, reg_n_sets, reg_n_deaths,
118 reg_n_calls_crossed, and reg_live_length. Also, BB_HEAD, BB_END.
119
120 The information in the line number notes is carefully retained by
121 this pass. Notes that refer to the starting and ending of
122 exception regions are also carefully retained by this pass. All
123 other NOTE insns are grouped in their same relative order at the
124 beginning of basic blocks and regions that have been scheduled. */
125 \f
126 #include "config.h"
127 #include "system.h"
128 #include "coretypes.h"
129 #include "tm.h"
130 #include "toplev.h"
131 #include "rtl.h"
132 #include "tm_p.h"
133 #include "hard-reg-set.h"
134 #include "regs.h"
135 #include "function.h"
136 #include "flags.h"
137 #include "insn-config.h"
138 #include "insn-attr.h"
139 #include "except.h"
140 #include "toplev.h"
141 #include "recog.h"
142 #include "sched-int.h"
143 #include "target.h"
144 #include "output.h"
145 #include "params.h"
146 #include "dbgcnt.h"
147
148 #ifdef INSN_SCHEDULING
149
150 /* issue_rate is the number of insns that can be scheduled in the same
151 machine cycle. It can be defined in the config/mach/mach.h file,
152 otherwise we set it to 1. */
153
154 static int issue_rate;
155
156 /* sched-verbose controls the amount of debugging output the
157 scheduler prints. It is controlled by -fsched-verbose=N:
158 N>0 and no -DSR : the output is directed to stderr.
159 N>=10 will direct the printouts to stderr (regardless of -dSR).
160 N=1: same as -dSR.
161 N=2: bb's probabilities, detailed ready list info, unit/insn info.
162 N=3: rtl at abort point, control-flow, regions info.
163 N=5: dependences info. */
164
165 static int sched_verbose_param = 0;
166 int sched_verbose = 0;
167
168 /* Debugging file. All printouts are sent to dump, which is always set,
169 either to stderr, or to the dump listing file (-dRS). */
170 FILE *sched_dump = 0;
171
172 /* Highest uid before scheduling. */
173 static int old_max_uid;
174
175 /* fix_sched_param() is called from toplev.c upon detection
176 of the -fsched-verbose=N option. */
177
178 void
179 fix_sched_param (const char *param, const char *val)
180 {
181 if (!strcmp (param, "verbose"))
182 sched_verbose_param = atoi (val);
183 else
184 warning (0, "fix_sched_param: unknown param: %s", param);
185 }
186
187 struct haifa_insn_data *h_i_d;
188
189 #define INSN_TICK(INSN) (h_i_d[INSN_UID (INSN)].tick)
190 #define INTER_TICK(INSN) (h_i_d[INSN_UID (INSN)].inter_tick)
191
192 /* If INSN_TICK of an instruction is equal to INVALID_TICK,
193 then it should be recalculated from scratch. */
194 #define INVALID_TICK (-(max_insn_queue_index + 1))
195 /* The minimal value of the INSN_TICK of an instruction. */
196 #define MIN_TICK (-max_insn_queue_index)
197
198 /* Issue points are used to distinguish between instructions in max_issue ().
199 For now, all instructions are equally good. */
200 #define ISSUE_POINTS(INSN) 1
201
202 /* List of important notes we must keep around. This is a pointer to the
203 last element in the list. */
204 static rtx note_list;
205
206 static struct spec_info_def spec_info_var;
207 /* Description of the speculative part of the scheduling.
208 If NULL - no speculation. */
209 spec_info_t spec_info;
210
211 /* True, if recovery block was added during scheduling of current block.
212 Used to determine, if we need to fix INSN_TICKs. */
213 static bool haifa_recovery_bb_recently_added_p;
214
215 /* True, if recovery block was added during this scheduling pass.
216 Used to determine if we should have empty memory pools of dependencies
217 after finishing current region. */
218 bool haifa_recovery_bb_ever_added_p;
219
220 /* Counters of different types of speculative instructions. */
221 static int nr_begin_data, nr_be_in_data, nr_begin_control, nr_be_in_control;
222
223 /* Array used in {unlink, restore}_bb_notes. */
224 static rtx *bb_header = 0;
225
226 /* Number of basic_blocks. */
227 static int old_last_basic_block;
228
229 /* Basic block after which recovery blocks will be created. */
230 static basic_block before_recovery;
231
232 /* Queues, etc. */
233
234 /* An instruction is ready to be scheduled when all insns preceding it
235 have already been scheduled. It is important to ensure that all
236 insns which use its result will not be executed until its result
237 has been computed. An insn is maintained in one of four structures:
238
239 (P) the "Pending" set of insns which cannot be scheduled until
240 their dependencies have been satisfied.
241 (Q) the "Queued" set of insns that can be scheduled when sufficient
242 time has passed.
243 (R) the "Ready" list of unscheduled, uncommitted insns.
244 (S) the "Scheduled" list of insns.
245
246 Initially, all insns are either "Pending" or "Ready" depending on
247 whether their dependencies are satisfied.
248
249 Insns move from the "Ready" list to the "Scheduled" list as they
250 are committed to the schedule. As this occurs, the insns in the
251 "Pending" list have their dependencies satisfied and move to either
252 the "Ready" list or the "Queued" set depending on whether
253 sufficient time has passed to make them ready. As time passes,
254 insns move from the "Queued" set to the "Ready" list.
255
256 The "Pending" list (P) are the insns in the INSN_FORW_DEPS of the
257 unscheduled insns, i.e., those that are ready, queued, and pending.
258 The "Queued" set (Q) is implemented by the variable `insn_queue'.
259 The "Ready" list (R) is implemented by the variables `ready' and
260 `n_ready'.
261 The "Scheduled" list (S) is the new insn chain built by this pass.
262
263 The transition (R->S) is implemented in the scheduling loop in
264 `schedule_block' when the best insn to schedule is chosen.
265 The transitions (P->R and P->Q) are implemented in `schedule_insn' as
266 insns move from the ready list to the scheduled list.
267 The transition (Q->R) is implemented in 'queue_to_insn' as time
268 passes or stalls are introduced. */
269
270 /* Implement a circular buffer to delay instructions until sufficient
271 time has passed. For the new pipeline description interface,
272 MAX_INSN_QUEUE_INDEX is a power of two minus one which is not less
273 than maximal time of instruction execution computed by genattr.c on
274 the base maximal time of functional unit reservations and getting a
275 result. This is the longest time an insn may be queued. */
276
277 static rtx *insn_queue;
278 static int q_ptr = 0;
279 static int q_size = 0;
280 #define NEXT_Q(X) (((X)+1) & max_insn_queue_index)
281 #define NEXT_Q_AFTER(X, C) (((X)+C) & max_insn_queue_index)
282
283 #define QUEUE_SCHEDULED (-3)
284 #define QUEUE_NOWHERE (-2)
285 #define QUEUE_READY (-1)
286 /* QUEUE_SCHEDULED - INSN is scheduled.
287 QUEUE_NOWHERE - INSN isn't scheduled yet and is neither in
288 queue or ready list.
289 QUEUE_READY - INSN is in ready list.
290 N >= 0 - INSN queued for X [where NEXT_Q_AFTER (q_ptr, X) == N] cycles. */
291
292 #define QUEUE_INDEX(INSN) (h_i_d[INSN_UID (INSN)].queue_index)
293
294 /* The following variable value refers for all current and future
295 reservations of the processor units. */
296 state_t curr_state;
297
298 /* The following variable value is size of memory representing all
299 current and future reservations of the processor units. */
300 static size_t dfa_state_size;
301
302 /* The following array is used to find the best insn from ready when
303 the automaton pipeline interface is used. */
304 static char *ready_try;
305
306 /* Describe the ready list of the scheduler.
307 VEC holds space enough for all insns in the current region. VECLEN
308 says how many exactly.
309 FIRST is the index of the element with the highest priority; i.e. the
310 last one in the ready list, since elements are ordered by ascending
311 priority.
312 N_READY determines how many insns are on the ready list. */
313
314 struct ready_list
315 {
316 rtx *vec;
317 int veclen;
318 int first;
319 int n_ready;
320 };
321
322 /* The pointer to the ready list. */
323 static struct ready_list *readyp;
324
325 /* Scheduling clock. */
326 static int clock_var;
327
328 /* Number of instructions in current scheduling region. */
329 static int rgn_n_insns;
330
331 static int may_trap_exp (const_rtx, int);
332
333 /* Nonzero iff the address is comprised from at most 1 register. */
334 #define CONST_BASED_ADDRESS_P(x) \
335 (REG_P (x) \
336 || ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS \
337 || (GET_CODE (x) == LO_SUM)) \
338 && (CONSTANT_P (XEXP (x, 0)) \
339 || CONSTANT_P (XEXP (x, 1)))))
340
341 /* Returns a class that insn with GET_DEST(insn)=x may belong to,
342 as found by analyzing insn's expression. */
343
344 static int
345 may_trap_exp (const_rtx x, int is_store)
346 {
347 enum rtx_code code;
348
349 if (x == 0)
350 return TRAP_FREE;
351 code = GET_CODE (x);
352 if (is_store)
353 {
354 if (code == MEM && may_trap_p (x))
355 return TRAP_RISKY;
356 else
357 return TRAP_FREE;
358 }
359 if (code == MEM)
360 {
361 /* The insn uses memory: a volatile load. */
362 if (MEM_VOLATILE_P (x))
363 return IRISKY;
364 /* An exception-free load. */
365 if (!may_trap_p (x))
366 return IFREE;
367 /* A load with 1 base register, to be further checked. */
368 if (CONST_BASED_ADDRESS_P (XEXP (x, 0)))
369 return PFREE_CANDIDATE;
370 /* No info on the load, to be further checked. */
371 return PRISKY_CANDIDATE;
372 }
373 else
374 {
375 const char *fmt;
376 int i, insn_class = TRAP_FREE;
377
378 /* Neither store nor load, check if it may cause a trap. */
379 if (may_trap_p (x))
380 return TRAP_RISKY;
381 /* Recursive step: walk the insn... */
382 fmt = GET_RTX_FORMAT (code);
383 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
384 {
385 if (fmt[i] == 'e')
386 {
387 int tmp_class = may_trap_exp (XEXP (x, i), is_store);
388 insn_class = WORST_CLASS (insn_class, tmp_class);
389 }
390 else if (fmt[i] == 'E')
391 {
392 int j;
393 for (j = 0; j < XVECLEN (x, i); j++)
394 {
395 int tmp_class = may_trap_exp (XVECEXP (x, i, j), is_store);
396 insn_class = WORST_CLASS (insn_class, tmp_class);
397 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
398 break;
399 }
400 }
401 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
402 break;
403 }
404 return insn_class;
405 }
406 }
407
408 /* Classifies rtx X of an insn for the purpose of verifying that X can be
409 executed speculatively (and consequently the insn can be moved
410 speculatively), by examining X, returning:
411 TRAP_RISKY: store, or risky non-load insn (e.g. division by variable).
412 TRAP_FREE: non-load insn.
413 IFREE: load from a globally safe location.
414 IRISKY: volatile load.
415 PFREE_CANDIDATE, PRISKY_CANDIDATE: load that need to be checked for
416 being either PFREE or PRISKY. */
417
418 static int
419 haifa_classify_rtx (const_rtx x)
420 {
421 int tmp_class = TRAP_FREE;
422 int insn_class = TRAP_FREE;
423 enum rtx_code code;
424
425 if (GET_CODE (x) == PARALLEL)
426 {
427 int i, len = XVECLEN (x, 0);
428
429 for (i = len - 1; i >= 0; i--)
430 {
431 tmp_class = haifa_classify_rtx (XVECEXP (x, 0, i));
432 insn_class = WORST_CLASS (insn_class, tmp_class);
433 if (insn_class == TRAP_RISKY || insn_class == IRISKY)
434 break;
435 }
436 }
437 else
438 {
439 code = GET_CODE (x);
440 switch (code)
441 {
442 case CLOBBER:
443 /* Test if it is a 'store'. */
444 tmp_class = may_trap_exp (XEXP (x, 0), 1);
445 break;
446 case SET:
447 /* Test if it is a store. */
448 tmp_class = may_trap_exp (SET_DEST (x), 1);
449 if (tmp_class == TRAP_RISKY)
450 break;
451 /* Test if it is a load. */
452 tmp_class =
453 WORST_CLASS (tmp_class,
454 may_trap_exp (SET_SRC (x), 0));
455 break;
456 case COND_EXEC:
457 tmp_class = haifa_classify_rtx (COND_EXEC_CODE (x));
458 if (tmp_class == TRAP_RISKY)
459 break;
460 tmp_class = WORST_CLASS (tmp_class,
461 may_trap_exp (COND_EXEC_TEST (x), 0));
462 break;
463 case TRAP_IF:
464 tmp_class = TRAP_RISKY;
465 break;
466 default:;
467 }
468 insn_class = tmp_class;
469 }
470
471 return insn_class;
472 }
473
474 int
475 haifa_classify_insn (const_rtx insn)
476 {
477 return haifa_classify_rtx (PATTERN (insn));
478 }
479
480
481 /* A typedef for rtx vector. */
482 typedef VEC(rtx, heap) *rtx_vec_t;
483
484 /* Forward declarations. */
485
486 static int priority (rtx);
487 static int rank_for_schedule (const void *, const void *);
488 static void swap_sort (rtx *, int);
489 static void queue_insn (rtx, int);
490 static int schedule_insn (rtx);
491 static int find_set_reg_weight (const_rtx);
492 static void find_insn_reg_weight (basic_block);
493 static void find_insn_reg_weight1 (rtx);
494 static void adjust_priority (rtx);
495 static void advance_one_cycle (void);
496
497 /* Notes handling mechanism:
498 =========================
499 Generally, NOTES are saved before scheduling and restored after scheduling.
500 The scheduler distinguishes between two types of notes:
501
502 (1) LOOP_BEGIN, LOOP_END, SETJMP, EHREGION_BEG, EHREGION_END notes:
503 Before scheduling a region, a pointer to the note is added to the insn
504 that follows or precedes it. (This happens as part of the data dependence
505 computation). After scheduling an insn, the pointer contained in it is
506 used for regenerating the corresponding note (in reemit_notes).
507
508 (2) All other notes (e.g. INSN_DELETED): Before scheduling a block,
509 these notes are put in a list (in rm_other_notes() and
510 unlink_other_notes ()). After scheduling the block, these notes are
511 inserted at the beginning of the block (in schedule_block()). */
512
513 static rtx unlink_other_notes (rtx, rtx);
514 static void reemit_notes (rtx);
515
516 static rtx *ready_lastpos (struct ready_list *);
517 static void ready_add (struct ready_list *, rtx, bool);
518 static void ready_sort (struct ready_list *);
519 static rtx ready_remove_first (struct ready_list *);
520
521 static void queue_to_ready (struct ready_list *);
522 static int early_queue_to_ready (state_t, struct ready_list *);
523
524 static void debug_ready_list (struct ready_list *);
525
526 static void move_insn (rtx);
527
528 /* The following functions are used to implement multi-pass scheduling
529 on the first cycle. */
530 static rtx ready_element (struct ready_list *, int);
531 static rtx ready_remove (struct ready_list *, int);
532 static void ready_remove_insn (rtx);
533 static int max_issue (struct ready_list *, int *, int);
534
535 static int choose_ready (struct ready_list *, rtx *);
536
537 static void fix_inter_tick (rtx, rtx);
538 static int fix_tick_ready (rtx);
539 static void change_queue_index (rtx, int);
540
541 /* The following functions are used to implement scheduling of data/control
542 speculative instructions. */
543
544 static void extend_h_i_d (void);
545 static void extend_ready (int);
546 static void extend_global (rtx);
547 static void extend_all (rtx);
548 static void init_h_i_d (rtx);
549 static void generate_recovery_code (rtx);
550 static void process_insn_forw_deps_be_in_spec (rtx, rtx, ds_t);
551 static void begin_speculative_block (rtx);
552 static void add_to_speculative_block (rtx);
553 static dw_t dep_weak (ds_t);
554 static edge find_fallthru_edge (basic_block);
555 static void init_before_recovery (void);
556 static basic_block create_recovery_block (void);
557 static void create_check_block_twin (rtx, bool);
558 static void fix_recovery_deps (basic_block);
559 static void change_pattern (rtx, rtx);
560 static int speculate_insn (rtx, ds_t, rtx *);
561 static void dump_new_block_header (int, basic_block, rtx, rtx);
562 static void restore_bb_notes (basic_block);
563 static void extend_bb (void);
564 static void fix_jump_move (rtx);
565 static void move_block_after_check (rtx);
566 static void move_succs (VEC(edge,gc) **, basic_block);
567 static void sched_remove_insn (rtx);
568 static void clear_priorities (rtx, rtx_vec_t *);
569 static void calc_priorities (rtx_vec_t);
570 static void add_jump_dependencies (rtx, rtx);
571 #ifdef ENABLE_CHECKING
572 static int has_edge_p (VEC(edge,gc) *, int);
573 static void check_cfg (rtx, rtx);
574 #endif
575
576 #endif /* INSN_SCHEDULING */
577 \f
578 /* Point to state used for the current scheduling pass. */
579 struct sched_info *current_sched_info;
580 \f
581 #ifndef INSN_SCHEDULING
582 void
583 schedule_insns (void)
584 {
585 }
586 #else
587
588 /* Working copy of frontend's sched_info variable. */
589 static struct sched_info current_sched_info_var;
590
591 /* Pointer to the last instruction scheduled. Used by rank_for_schedule,
592 so that insns independent of the last scheduled insn will be preferred
593 over dependent instructions. */
594
595 static rtx last_scheduled_insn;
596
597 /* Cached cost of the instruction. Use below function to get cost of the
598 insn. -1 here means that the field is not initialized. */
599 #define INSN_COST(INSN) (h_i_d[INSN_UID (INSN)].cost)
600
601 /* Compute cost of executing INSN.
602 This is the number of cycles between instruction issue and
603 instruction results. */
604 HAIFA_INLINE int
605 insn_cost (rtx insn)
606 {
607 int cost = INSN_COST (insn);
608
609 if (cost < 0)
610 {
611 /* A USE insn, or something else we don't need to
612 understand. We can't pass these directly to
613 result_ready_cost or insn_default_latency because it will
614 trigger a fatal error for unrecognizable insns. */
615 if (recog_memoized (insn) < 0)
616 {
617 INSN_COST (insn) = 0;
618 return 0;
619 }
620 else
621 {
622 cost = insn_default_latency (insn);
623 if (cost < 0)
624 cost = 0;
625
626 INSN_COST (insn) = cost;
627 }
628 }
629
630 return cost;
631 }
632
633 /* Compute cost of dependence LINK.
634 This is the number of cycles between instruction issue and
635 instruction results. */
636 int
637 dep_cost (dep_t link)
638 {
639 rtx used = DEP_CON (link);
640 int cost;
641
642 /* A USE insn should never require the value used to be computed.
643 This allows the computation of a function's result and parameter
644 values to overlap the return and call. */
645 if (recog_memoized (used) < 0)
646 cost = 0;
647 else
648 {
649 rtx insn = DEP_PRO (link);
650 enum reg_note dep_type = DEP_TYPE (link);
651
652 cost = insn_cost (insn);
653
654 if (INSN_CODE (insn) >= 0)
655 {
656 if (dep_type == REG_DEP_ANTI)
657 cost = 0;
658 else if (dep_type == REG_DEP_OUTPUT)
659 {
660 cost = (insn_default_latency (insn)
661 - insn_default_latency (used));
662 if (cost <= 0)
663 cost = 1;
664 }
665 else if (bypass_p (insn))
666 cost = insn_latency (insn, used);
667 }
668
669 if (targetm.sched.adjust_cost != NULL)
670 {
671 /* This variable is used for backward compatibility with the
672 targets. */
673 rtx dep_cost_rtx_link = alloc_INSN_LIST (NULL_RTX, NULL_RTX);
674
675 /* Make it self-cycled, so that if some tries to walk over this
676 incomplete list he/she will be caught in an endless loop. */
677 XEXP (dep_cost_rtx_link, 1) = dep_cost_rtx_link;
678
679 /* Targets use only REG_NOTE_KIND of the link. */
680 PUT_REG_NOTE_KIND (dep_cost_rtx_link, DEP_TYPE (link));
681
682 cost = targetm.sched.adjust_cost (used, dep_cost_rtx_link,
683 insn, cost);
684
685 free_INSN_LIST_node (dep_cost_rtx_link);
686 }
687
688 if (cost < 0)
689 cost = 0;
690 }
691
692 return cost;
693 }
694
695 /* Return 'true' if DEP should be included in priority calculations. */
696 static bool
697 contributes_to_priority_p (dep_t dep)
698 {
699 /* Critical path is meaningful in block boundaries only. */
700 if (!current_sched_info->contributes_to_priority (DEP_CON (dep),
701 DEP_PRO (dep)))
702 return false;
703
704 /* If flag COUNT_SPEC_IN_CRITICAL_PATH is set,
705 then speculative instructions will less likely be
706 scheduled. That is because the priority of
707 their producers will increase, and, thus, the
708 producers will more likely be scheduled, thus,
709 resolving the dependence. */
710 if ((current_sched_info->flags & DO_SPECULATION)
711 && !(spec_info->flags & COUNT_SPEC_IN_CRITICAL_PATH)
712 && (DEP_STATUS (dep) & SPECULATIVE))
713 return false;
714
715 return true;
716 }
717
718 /* Compute the priority number for INSN. */
719 static int
720 priority (rtx insn)
721 {
722 if (! INSN_P (insn))
723 return 0;
724
725 /* We should not be interested in priority of an already scheduled insn. */
726 gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
727
728 if (!INSN_PRIORITY_KNOWN (insn))
729 {
730 int this_priority = 0;
731
732 if (sd_lists_empty_p (insn, SD_LIST_FORW))
733 /* ??? We should set INSN_PRIORITY to insn_cost when and insn has
734 some forward deps but all of them are ignored by
735 contributes_to_priority hook. At the moment we set priority of
736 such insn to 0. */
737 this_priority = insn_cost (insn);
738 else
739 {
740 rtx prev_first, twin;
741 basic_block rec;
742
743 /* For recovery check instructions we calculate priority slightly
744 different than that of normal instructions. Instead of walking
745 through INSN_FORW_DEPS (check) list, we walk through
746 INSN_FORW_DEPS list of each instruction in the corresponding
747 recovery block. */
748
749 rec = RECOVERY_BLOCK (insn);
750 if (!rec || rec == EXIT_BLOCK_PTR)
751 {
752 prev_first = PREV_INSN (insn);
753 twin = insn;
754 }
755 else
756 {
757 prev_first = NEXT_INSN (BB_HEAD (rec));
758 twin = PREV_INSN (BB_END (rec));
759 }
760
761 do
762 {
763 sd_iterator_def sd_it;
764 dep_t dep;
765
766 FOR_EACH_DEP (twin, SD_LIST_FORW, sd_it, dep)
767 {
768 rtx next;
769 int next_priority;
770
771 next = DEP_CON (dep);
772
773 if (BLOCK_FOR_INSN (next) != rec)
774 {
775 int cost;
776
777 if (!contributes_to_priority_p (dep))
778 continue;
779
780 if (twin == insn)
781 cost = dep_cost (dep);
782 else
783 {
784 struct _dep _dep1, *dep1 = &_dep1;
785
786 init_dep (dep1, insn, next, REG_DEP_ANTI);
787
788 cost = dep_cost (dep1);
789 }
790
791 next_priority = cost + priority (next);
792
793 if (next_priority > this_priority)
794 this_priority = next_priority;
795 }
796 }
797
798 twin = PREV_INSN (twin);
799 }
800 while (twin != prev_first);
801 }
802 INSN_PRIORITY (insn) = this_priority;
803 INSN_PRIORITY_STATUS (insn) = 1;
804 }
805
806 return INSN_PRIORITY (insn);
807 }
808 \f
809 /* Macros and functions for keeping the priority queue sorted, and
810 dealing with queuing and dequeuing of instructions. */
811
812 #define SCHED_SORT(READY, N_READY) \
813 do { if ((N_READY) == 2) \
814 swap_sort (READY, N_READY); \
815 else if ((N_READY) > 2) \
816 qsort (READY, N_READY, sizeof (rtx), rank_for_schedule); } \
817 while (0)
818
819 /* Returns a positive value if x is preferred; returns a negative value if
820 y is preferred. Should never return 0, since that will make the sort
821 unstable. */
822
823 static int
824 rank_for_schedule (const void *x, const void *y)
825 {
826 rtx tmp = *(const rtx *) y;
827 rtx tmp2 = *(const rtx *) x;
828 int tmp_class, tmp2_class;
829 int val, priority_val, weight_val, info_val;
830
831 /* The insn in a schedule group should be issued the first. */
832 if (SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
833 return SCHED_GROUP_P (tmp2) ? 1 : -1;
834
835 /* Make sure that priority of TMP and TMP2 are initialized. */
836 gcc_assert (INSN_PRIORITY_KNOWN (tmp) && INSN_PRIORITY_KNOWN (tmp2));
837
838 /* Prefer insn with higher priority. */
839 priority_val = INSN_PRIORITY (tmp2) - INSN_PRIORITY (tmp);
840
841 if (priority_val)
842 return priority_val;
843
844 /* Prefer speculative insn with greater dependencies weakness. */
845 if (spec_info)
846 {
847 ds_t ds1, ds2;
848 dw_t dw1, dw2;
849 int dw;
850
851 ds1 = TODO_SPEC (tmp) & SPECULATIVE;
852 if (ds1)
853 dw1 = dep_weak (ds1);
854 else
855 dw1 = NO_DEP_WEAK;
856
857 ds2 = TODO_SPEC (tmp2) & SPECULATIVE;
858 if (ds2)
859 dw2 = dep_weak (ds2);
860 else
861 dw2 = NO_DEP_WEAK;
862
863 dw = dw2 - dw1;
864 if (dw > (NO_DEP_WEAK / 8) || dw < -(NO_DEP_WEAK / 8))
865 return dw;
866 }
867
868 /* Prefer an insn with smaller contribution to registers-pressure. */
869 if (!reload_completed &&
870 (weight_val = INSN_REG_WEIGHT (tmp) - INSN_REG_WEIGHT (tmp2)))
871 return weight_val;
872
873 info_val = (*current_sched_info->rank) (tmp, tmp2);
874 if (info_val)
875 return info_val;
876
877 /* Compare insns based on their relation to the last-scheduled-insn. */
878 if (INSN_P (last_scheduled_insn))
879 {
880 dep_t dep1;
881 dep_t dep2;
882
883 /* Classify the instructions into three classes:
884 1) Data dependent on last schedule insn.
885 2) Anti/Output dependent on last scheduled insn.
886 3) Independent of last scheduled insn, or has latency of one.
887 Choose the insn from the highest numbered class if different. */
888 dep1 = sd_find_dep_between (last_scheduled_insn, tmp, true);
889
890 if (dep1 == NULL || dep_cost (dep1) == 1)
891 tmp_class = 3;
892 else if (/* Data dependence. */
893 DEP_TYPE (dep1) == REG_DEP_TRUE)
894 tmp_class = 1;
895 else
896 tmp_class = 2;
897
898 dep2 = sd_find_dep_between (last_scheduled_insn, tmp2, true);
899
900 if (dep2 == NULL || dep_cost (dep2) == 1)
901 tmp2_class = 3;
902 else if (/* Data dependence. */
903 DEP_TYPE (dep2) == REG_DEP_TRUE)
904 tmp2_class = 1;
905 else
906 tmp2_class = 2;
907
908 if ((val = tmp2_class - tmp_class))
909 return val;
910 }
911
912 /* Prefer the insn which has more later insns that depend on it.
913 This gives the scheduler more freedom when scheduling later
914 instructions at the expense of added register pressure. */
915
916 val = (sd_lists_size (tmp2, SD_LIST_FORW)
917 - sd_lists_size (tmp, SD_LIST_FORW));
918
919 if (val != 0)
920 return val;
921
922 /* If insns are equally good, sort by INSN_LUID (original insn order),
923 so that we make the sort stable. This minimizes instruction movement,
924 thus minimizing sched's effect on debugging and cross-jumping. */
925 return INSN_LUID (tmp) - INSN_LUID (tmp2);
926 }
927
928 /* Resort the array A in which only element at index N may be out of order. */
929
930 HAIFA_INLINE static void
931 swap_sort (rtx *a, int n)
932 {
933 rtx insn = a[n - 1];
934 int i = n - 2;
935
936 while (i >= 0 && rank_for_schedule (a + i, &insn) >= 0)
937 {
938 a[i + 1] = a[i];
939 i -= 1;
940 }
941 a[i + 1] = insn;
942 }
943
944 /* Add INSN to the insn queue so that it can be executed at least
945 N_CYCLES after the currently executing insn. Preserve insns
946 chain for debugging purposes. */
947
948 HAIFA_INLINE static void
949 queue_insn (rtx insn, int n_cycles)
950 {
951 int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
952 rtx link = alloc_INSN_LIST (insn, insn_queue[next_q]);
953
954 gcc_assert (n_cycles <= max_insn_queue_index);
955
956 insn_queue[next_q] = link;
957 q_size += 1;
958
959 if (sched_verbose >= 2)
960 {
961 fprintf (sched_dump, ";;\t\tReady-->Q: insn %s: ",
962 (*current_sched_info->print_insn) (insn, 0));
963
964 fprintf (sched_dump, "queued for %d cycles.\n", n_cycles);
965 }
966
967 QUEUE_INDEX (insn) = next_q;
968 }
969
970 /* Remove INSN from queue. */
971 static void
972 queue_remove (rtx insn)
973 {
974 gcc_assert (QUEUE_INDEX (insn) >= 0);
975 remove_free_INSN_LIST_elem (insn, &insn_queue[QUEUE_INDEX (insn)]);
976 q_size--;
977 QUEUE_INDEX (insn) = QUEUE_NOWHERE;
978 }
979
980 /* Return a pointer to the bottom of the ready list, i.e. the insn
981 with the lowest priority. */
982
983 HAIFA_INLINE static rtx *
984 ready_lastpos (struct ready_list *ready)
985 {
986 gcc_assert (ready->n_ready >= 1);
987 return ready->vec + ready->first - ready->n_ready + 1;
988 }
989
990 /* Add an element INSN to the ready list so that it ends up with the
991 lowest/highest priority depending on FIRST_P. */
992
993 HAIFA_INLINE static void
994 ready_add (struct ready_list *ready, rtx insn, bool first_p)
995 {
996 if (!first_p)
997 {
998 if (ready->first == ready->n_ready)
999 {
1000 memmove (ready->vec + ready->veclen - ready->n_ready,
1001 ready_lastpos (ready),
1002 ready->n_ready * sizeof (rtx));
1003 ready->first = ready->veclen - 1;
1004 }
1005 ready->vec[ready->first - ready->n_ready] = insn;
1006 }
1007 else
1008 {
1009 if (ready->first == ready->veclen - 1)
1010 {
1011 if (ready->n_ready)
1012 /* ready_lastpos() fails when called with (ready->n_ready == 0). */
1013 memmove (ready->vec + ready->veclen - ready->n_ready - 1,
1014 ready_lastpos (ready),
1015 ready->n_ready * sizeof (rtx));
1016 ready->first = ready->veclen - 2;
1017 }
1018 ready->vec[++(ready->first)] = insn;
1019 }
1020
1021 ready->n_ready++;
1022
1023 gcc_assert (QUEUE_INDEX (insn) != QUEUE_READY);
1024 QUEUE_INDEX (insn) = QUEUE_READY;
1025 }
1026
1027 /* Remove the element with the highest priority from the ready list and
1028 return it. */
1029
1030 HAIFA_INLINE static rtx
1031 ready_remove_first (struct ready_list *ready)
1032 {
1033 rtx t;
1034
1035 gcc_assert (ready->n_ready);
1036 t = ready->vec[ready->first--];
1037 ready->n_ready--;
1038 /* If the queue becomes empty, reset it. */
1039 if (ready->n_ready == 0)
1040 ready->first = ready->veclen - 1;
1041
1042 gcc_assert (QUEUE_INDEX (t) == QUEUE_READY);
1043 QUEUE_INDEX (t) = QUEUE_NOWHERE;
1044
1045 return t;
1046 }
1047
1048 /* The following code implements multi-pass scheduling for the first
1049 cycle. In other words, we will try to choose ready insn which
1050 permits to start maximum number of insns on the same cycle. */
1051
1052 /* Return a pointer to the element INDEX from the ready. INDEX for
1053 insn with the highest priority is 0, and the lowest priority has
1054 N_READY - 1. */
1055
1056 HAIFA_INLINE static rtx
1057 ready_element (struct ready_list *ready, int index)
1058 {
1059 gcc_assert (ready->n_ready && index < ready->n_ready);
1060
1061 return ready->vec[ready->first - index];
1062 }
1063
1064 /* Remove the element INDEX from the ready list and return it. INDEX
1065 for insn with the highest priority is 0, and the lowest priority
1066 has N_READY - 1. */
1067
1068 HAIFA_INLINE static rtx
1069 ready_remove (struct ready_list *ready, int index)
1070 {
1071 rtx t;
1072 int i;
1073
1074 if (index == 0)
1075 return ready_remove_first (ready);
1076 gcc_assert (ready->n_ready && index < ready->n_ready);
1077 t = ready->vec[ready->first - index];
1078 ready->n_ready--;
1079 for (i = index; i < ready->n_ready; i++)
1080 ready->vec[ready->first - i] = ready->vec[ready->first - i - 1];
1081 QUEUE_INDEX (t) = QUEUE_NOWHERE;
1082 return t;
1083 }
1084
1085 /* Remove INSN from the ready list. */
1086 static void
1087 ready_remove_insn (rtx insn)
1088 {
1089 int i;
1090
1091 for (i = 0; i < readyp->n_ready; i++)
1092 if (ready_element (readyp, i) == insn)
1093 {
1094 ready_remove (readyp, i);
1095 return;
1096 }
1097 gcc_unreachable ();
1098 }
1099
1100 /* Sort the ready list READY by ascending priority, using the SCHED_SORT
1101 macro. */
1102
1103 HAIFA_INLINE static void
1104 ready_sort (struct ready_list *ready)
1105 {
1106 rtx *first = ready_lastpos (ready);
1107 SCHED_SORT (first, ready->n_ready);
1108 }
1109
1110 /* PREV is an insn that is ready to execute. Adjust its priority if that
1111 will help shorten or lengthen register lifetimes as appropriate. Also
1112 provide a hook for the target to tweek itself. */
1113
1114 HAIFA_INLINE static void
1115 adjust_priority (rtx prev)
1116 {
1117 /* ??? There used to be code here to try and estimate how an insn
1118 affected register lifetimes, but it did it by looking at REG_DEAD
1119 notes, which we removed in schedule_region. Nor did it try to
1120 take into account register pressure or anything useful like that.
1121
1122 Revisit when we have a machine model to work with and not before. */
1123
1124 if (targetm.sched.adjust_priority)
1125 INSN_PRIORITY (prev) =
1126 targetm.sched.adjust_priority (prev, INSN_PRIORITY (prev));
1127 }
1128
1129 /* Advance time on one cycle. */
1130 HAIFA_INLINE static void
1131 advance_one_cycle (void)
1132 {
1133 if (targetm.sched.dfa_pre_advance_cycle)
1134 targetm.sched.dfa_pre_advance_cycle ();
1135
1136 if (targetm.sched.dfa_pre_cycle_insn)
1137 state_transition (curr_state,
1138 targetm.sched.dfa_pre_cycle_insn ());
1139
1140 state_transition (curr_state, NULL);
1141
1142 if (targetm.sched.dfa_post_cycle_insn)
1143 state_transition (curr_state,
1144 targetm.sched.dfa_post_cycle_insn ());
1145
1146 if (targetm.sched.dfa_post_advance_cycle)
1147 targetm.sched.dfa_post_advance_cycle ();
1148 }
1149
1150 /* Clock at which the previous instruction was issued. */
1151 static int last_clock_var;
1152
1153 /* INSN is the "currently executing insn". Launch each insn which was
1154 waiting on INSN. READY is the ready list which contains the insns
1155 that are ready to fire. CLOCK is the current cycle. The function
1156 returns necessary cycle advance after issuing the insn (it is not
1157 zero for insns in a schedule group). */
1158
1159 static int
1160 schedule_insn (rtx insn)
1161 {
1162 sd_iterator_def sd_it;
1163 dep_t dep;
1164 int advance = 0;
1165
1166 if (sched_verbose >= 1)
1167 {
1168 char buf[2048];
1169
1170 print_insn (buf, insn, 0);
1171 buf[40] = 0;
1172 fprintf (sched_dump, ";;\t%3i--> %-40s:", clock_var, buf);
1173
1174 if (recog_memoized (insn) < 0)
1175 fprintf (sched_dump, "nothing");
1176 else
1177 print_reservation (sched_dump, insn);
1178 fputc ('\n', sched_dump);
1179 }
1180
1181 /* Scheduling instruction should have all its dependencies resolved and
1182 should have been removed from the ready list. */
1183 gcc_assert (sd_lists_empty_p (insn, SD_LIST_BACK));
1184
1185 gcc_assert (QUEUE_INDEX (insn) == QUEUE_NOWHERE);
1186 QUEUE_INDEX (insn) = QUEUE_SCHEDULED;
1187
1188 gcc_assert (INSN_TICK (insn) >= MIN_TICK);
1189 if (INSN_TICK (insn) > clock_var)
1190 /* INSN has been prematurely moved from the queue to the ready list.
1191 This is possible only if following flag is set. */
1192 gcc_assert (flag_sched_stalled_insns);
1193
1194 /* ??? Probably, if INSN is scheduled prematurely, we should leave
1195 INSN_TICK untouched. This is a machine-dependent issue, actually. */
1196 INSN_TICK (insn) = clock_var;
1197
1198 /* Update dependent instructions. */
1199 for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
1200 sd_iterator_cond (&sd_it, &dep);)
1201 {
1202 rtx next = DEP_CON (dep);
1203
1204 /* Resolve the dependence between INSN and NEXT.
1205 sd_resolve_dep () moves current dep to another list thus
1206 advancing the iterator. */
1207 sd_resolve_dep (sd_it);
1208
1209 if (!IS_SPECULATION_BRANCHY_CHECK_P (insn))
1210 {
1211 int effective_cost;
1212
1213 effective_cost = try_ready (next);
1214
1215 if (effective_cost >= 0
1216 && SCHED_GROUP_P (next)
1217 && advance < effective_cost)
1218 advance = effective_cost;
1219 }
1220 else
1221 /* Check always has only one forward dependence (to the first insn in
1222 the recovery block), therefore, this will be executed only once. */
1223 {
1224 gcc_assert (sd_lists_empty_p (insn, SD_LIST_FORW));
1225 fix_recovery_deps (RECOVERY_BLOCK (insn));
1226 }
1227 }
1228
1229 /* This is the place where scheduler doesn't *basically* need backward and
1230 forward dependencies for INSN anymore. Nevertheless they are used in
1231 heuristics in rank_for_schedule (), early_queue_to_ready () and in
1232 some targets (e.g. rs6000). Thus the earliest place where we *can*
1233 remove dependencies is after targetm.sched.md_finish () call in
1234 schedule_block (). But, on the other side, the safest place to remove
1235 dependencies is when we are finishing scheduling entire region. As we
1236 don't generate [many] dependencies during scheduling itself, we won't
1237 need memory until beginning of next region.
1238 Bottom line: Dependencies are removed for all insns in the end of
1239 scheduling the region. */
1240
1241 /* Annotate the instruction with issue information -- TImode
1242 indicates that the instruction is expected not to be able
1243 to issue on the same cycle as the previous insn. A machine
1244 may use this information to decide how the instruction should
1245 be aligned. */
1246 if (issue_rate > 1
1247 && GET_CODE (PATTERN (insn)) != USE
1248 && GET_CODE (PATTERN (insn)) != CLOBBER)
1249 {
1250 if (reload_completed)
1251 PUT_MODE (insn, clock_var > last_clock_var ? TImode : VOIDmode);
1252 last_clock_var = clock_var;
1253 }
1254
1255 return advance;
1256 }
1257
1258 /* Functions for handling of notes. */
1259
1260 /* Delete notes beginning with INSN and put them in the chain
1261 of notes ended by NOTE_LIST.
1262 Returns the insn following the notes. */
1263
1264 static rtx
1265 unlink_other_notes (rtx insn, rtx tail)
1266 {
1267 rtx prev = PREV_INSN (insn);
1268
1269 while (insn != tail && NOTE_NOT_BB_P (insn))
1270 {
1271 rtx next = NEXT_INSN (insn);
1272 basic_block bb = BLOCK_FOR_INSN (insn);
1273
1274 /* Delete the note from its current position. */
1275 if (prev)
1276 NEXT_INSN (prev) = next;
1277 if (next)
1278 PREV_INSN (next) = prev;
1279
1280 if (bb)
1281 {
1282 /* Basic block can begin with either LABEL or
1283 NOTE_INSN_BASIC_BLOCK. */
1284 gcc_assert (BB_HEAD (bb) != insn);
1285
1286 /* Check if we are removing last insn in the BB. */
1287 if (BB_END (bb) == insn)
1288 BB_END (bb) = prev;
1289 }
1290
1291 /* See sched_analyze to see how these are handled. */
1292 if (NOTE_KIND (insn) != NOTE_INSN_EH_REGION_BEG
1293 && NOTE_KIND (insn) != NOTE_INSN_EH_REGION_END)
1294 {
1295 /* Insert the note at the end of the notes list. */
1296 PREV_INSN (insn) = note_list;
1297 if (note_list)
1298 NEXT_INSN (note_list) = insn;
1299 note_list = insn;
1300 }
1301
1302 insn = next;
1303 }
1304 return insn;
1305 }
1306
1307 /* Return the head and tail pointers of ebb starting at BEG and ending
1308 at END. */
1309
1310 void
1311 get_ebb_head_tail (basic_block beg, basic_block end, rtx *headp, rtx *tailp)
1312 {
1313 rtx beg_head = BB_HEAD (beg);
1314 rtx beg_tail = BB_END (beg);
1315 rtx end_head = BB_HEAD (end);
1316 rtx end_tail = BB_END (end);
1317
1318 /* Don't include any notes or labels at the beginning of the BEG
1319 basic block, or notes at the end of the END basic blocks. */
1320
1321 if (LABEL_P (beg_head))
1322 beg_head = NEXT_INSN (beg_head);
1323
1324 while (beg_head != beg_tail)
1325 if (NOTE_P (beg_head))
1326 beg_head = NEXT_INSN (beg_head);
1327 else
1328 break;
1329
1330 *headp = beg_head;
1331
1332 if (beg == end)
1333 end_head = beg_head;
1334 else if (LABEL_P (end_head))
1335 end_head = NEXT_INSN (end_head);
1336
1337 while (end_head != end_tail)
1338 if (NOTE_P (end_tail))
1339 end_tail = PREV_INSN (end_tail);
1340 else
1341 break;
1342
1343 *tailp = end_tail;
1344 }
1345
1346 /* Return nonzero if there are no real insns in the range [ HEAD, TAIL ]. */
1347
1348 int
1349 no_real_insns_p (const_rtx head, const_rtx tail)
1350 {
1351 while (head != NEXT_INSN (tail))
1352 {
1353 if (!NOTE_P (head) && !LABEL_P (head))
1354 return 0;
1355 head = NEXT_INSN (head);
1356 }
1357 return 1;
1358 }
1359
1360 /* Delete notes between HEAD and TAIL and put them in the chain
1361 of notes ended by NOTE_LIST. */
1362
1363 void
1364 rm_other_notes (rtx head, rtx tail)
1365 {
1366 rtx next_tail;
1367 rtx insn;
1368
1369 note_list = 0;
1370 if (head == tail && (! INSN_P (head)))
1371 return;
1372
1373 next_tail = NEXT_INSN (tail);
1374 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1375 {
1376 rtx prev;
1377
1378 /* Farm out notes, and maybe save them in NOTE_LIST.
1379 This is needed to keep the debugger from
1380 getting completely deranged. */
1381 if (NOTE_NOT_BB_P (insn))
1382 {
1383 prev = insn;
1384
1385 insn = unlink_other_notes (insn, next_tail);
1386
1387 gcc_assert (prev != tail && prev != head && insn != next_tail);
1388 }
1389 }
1390 }
1391
1392 /* Functions for computation of registers live/usage info. */
1393
1394 /* This function looks for a new register being defined.
1395 If the destination register is already used by the source,
1396 a new register is not needed. */
1397
1398 static int
1399 find_set_reg_weight (const_rtx x)
1400 {
1401 if (GET_CODE (x) == CLOBBER
1402 && register_operand (SET_DEST (x), VOIDmode))
1403 return 1;
1404 if (GET_CODE (x) == SET
1405 && register_operand (SET_DEST (x), VOIDmode))
1406 {
1407 if (REG_P (SET_DEST (x)))
1408 {
1409 if (!reg_mentioned_p (SET_DEST (x), SET_SRC (x)))
1410 return 1;
1411 else
1412 return 0;
1413 }
1414 return 1;
1415 }
1416 return 0;
1417 }
1418
1419 /* Calculate INSN_REG_WEIGHT for all insns of a block. */
1420
1421 static void
1422 find_insn_reg_weight (basic_block bb)
1423 {
1424 rtx insn, next_tail, head, tail;
1425
1426 get_ebb_head_tail (bb, bb, &head, &tail);
1427 next_tail = NEXT_INSN (tail);
1428
1429 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
1430 find_insn_reg_weight1 (insn);
1431 }
1432
1433 /* Calculate INSN_REG_WEIGHT for single instruction.
1434 Separated from find_insn_reg_weight because of need
1435 to initialize new instruction in generate_recovery_code. */
1436 static void
1437 find_insn_reg_weight1 (rtx insn)
1438 {
1439 int reg_weight = 0;
1440 rtx x;
1441
1442 /* Handle register life information. */
1443 if (! INSN_P (insn))
1444 return;
1445
1446 /* Increment weight for each register born here. */
1447 x = PATTERN (insn);
1448 reg_weight += find_set_reg_weight (x);
1449 if (GET_CODE (x) == PARALLEL)
1450 {
1451 int j;
1452 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
1453 {
1454 x = XVECEXP (PATTERN (insn), 0, j);
1455 reg_weight += find_set_reg_weight (x);
1456 }
1457 }
1458 /* Decrement weight for each register that dies here. */
1459 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1460 {
1461 if (REG_NOTE_KIND (x) == REG_DEAD
1462 || REG_NOTE_KIND (x) == REG_UNUSED)
1463 reg_weight--;
1464 }
1465
1466 INSN_REG_WEIGHT (insn) = reg_weight;
1467 }
1468
1469 /* Move insns that became ready to fire from queue to ready list. */
1470
1471 static void
1472 queue_to_ready (struct ready_list *ready)
1473 {
1474 rtx insn;
1475 rtx link;
1476 rtx skip_insn;
1477
1478 q_ptr = NEXT_Q (q_ptr);
1479
1480 if (dbg_cnt (sched_insn) == false)
1481 /* If debug counter is activated do not requeue insn next after
1482 last_scheduled_insn. */
1483 skip_insn = next_nonnote_insn (last_scheduled_insn);
1484 else
1485 skip_insn = NULL_RTX;
1486
1487 /* Add all pending insns that can be scheduled without stalls to the
1488 ready list. */
1489 for (link = insn_queue[q_ptr]; link; link = XEXP (link, 1))
1490 {
1491 insn = XEXP (link, 0);
1492 q_size -= 1;
1493
1494 if (sched_verbose >= 2)
1495 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1496 (*current_sched_info->print_insn) (insn, 0));
1497
1498 /* If the ready list is full, delay the insn for 1 cycle.
1499 See the comment in schedule_block for the rationale. */
1500 if (!reload_completed
1501 && ready->n_ready > MAX_SCHED_READY_INSNS
1502 && !SCHED_GROUP_P (insn)
1503 && insn != skip_insn)
1504 {
1505 if (sched_verbose >= 2)
1506 fprintf (sched_dump, "requeued because ready full\n");
1507 queue_insn (insn, 1);
1508 }
1509 else
1510 {
1511 ready_add (ready, insn, false);
1512 if (sched_verbose >= 2)
1513 fprintf (sched_dump, "moving to ready without stalls\n");
1514 }
1515 }
1516 free_INSN_LIST_list (&insn_queue[q_ptr]);
1517
1518 /* If there are no ready insns, stall until one is ready and add all
1519 of the pending insns at that point to the ready list. */
1520 if (ready->n_ready == 0)
1521 {
1522 int stalls;
1523
1524 for (stalls = 1; stalls <= max_insn_queue_index; stalls++)
1525 {
1526 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
1527 {
1528 for (; link; link = XEXP (link, 1))
1529 {
1530 insn = XEXP (link, 0);
1531 q_size -= 1;
1532
1533 if (sched_verbose >= 2)
1534 fprintf (sched_dump, ";;\t\tQ-->Ready: insn %s: ",
1535 (*current_sched_info->print_insn) (insn, 0));
1536
1537 ready_add (ready, insn, false);
1538 if (sched_verbose >= 2)
1539 fprintf (sched_dump, "moving to ready with %d stalls\n", stalls);
1540 }
1541 free_INSN_LIST_list (&insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]);
1542
1543 advance_one_cycle ();
1544
1545 break;
1546 }
1547
1548 advance_one_cycle ();
1549 }
1550
1551 q_ptr = NEXT_Q_AFTER (q_ptr, stalls);
1552 clock_var += stalls;
1553 }
1554 }
1555
1556 /* Used by early_queue_to_ready. Determines whether it is "ok" to
1557 prematurely move INSN from the queue to the ready list. Currently,
1558 if a target defines the hook 'is_costly_dependence', this function
1559 uses the hook to check whether there exist any dependences which are
1560 considered costly by the target, between INSN and other insns that
1561 have already been scheduled. Dependences are checked up to Y cycles
1562 back, with default Y=1; The flag -fsched-stalled-insns-dep=Y allows
1563 controlling this value.
1564 (Other considerations could be taken into account instead (or in
1565 addition) depending on user flags and target hooks. */
1566
1567 static bool
1568 ok_for_early_queue_removal (rtx insn)
1569 {
1570 int n_cycles;
1571 rtx prev_insn = last_scheduled_insn;
1572
1573 if (targetm.sched.is_costly_dependence)
1574 {
1575 for (n_cycles = flag_sched_stalled_insns_dep; n_cycles; n_cycles--)
1576 {
1577 for ( ; prev_insn; prev_insn = PREV_INSN (prev_insn))
1578 {
1579 int cost;
1580
1581 if (prev_insn == current_sched_info->prev_head)
1582 {
1583 prev_insn = NULL;
1584 break;
1585 }
1586
1587 if (!NOTE_P (prev_insn))
1588 {
1589 dep_t dep;
1590
1591 dep = sd_find_dep_between (prev_insn, insn, true);
1592
1593 if (dep != NULL)
1594 {
1595 cost = dep_cost (dep);
1596
1597 if (targetm.sched.is_costly_dependence (dep, cost,
1598 flag_sched_stalled_insns_dep - n_cycles))
1599 return false;
1600 }
1601 }
1602
1603 if (GET_MODE (prev_insn) == TImode) /* end of dispatch group */
1604 break;
1605 }
1606
1607 if (!prev_insn)
1608 break;
1609 prev_insn = PREV_INSN (prev_insn);
1610 }
1611 }
1612
1613 return true;
1614 }
1615
1616
1617 /* Remove insns from the queue, before they become "ready" with respect
1618 to FU latency considerations. */
1619
1620 static int
1621 early_queue_to_ready (state_t state, struct ready_list *ready)
1622 {
1623 rtx insn;
1624 rtx link;
1625 rtx next_link;
1626 rtx prev_link;
1627 bool move_to_ready;
1628 int cost;
1629 state_t temp_state = alloca (dfa_state_size);
1630 int stalls;
1631 int insns_removed = 0;
1632
1633 /*
1634 Flag '-fsched-stalled-insns=X' determines the aggressiveness of this
1635 function:
1636
1637 X == 0: There is no limit on how many queued insns can be removed
1638 prematurely. (flag_sched_stalled_insns = -1).
1639
1640 X >= 1: Only X queued insns can be removed prematurely in each
1641 invocation. (flag_sched_stalled_insns = X).
1642
1643 Otherwise: Early queue removal is disabled.
1644 (flag_sched_stalled_insns = 0)
1645 */
1646
1647 if (! flag_sched_stalled_insns)
1648 return 0;
1649
1650 for (stalls = 0; stalls <= max_insn_queue_index; stalls++)
1651 {
1652 if ((link = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
1653 {
1654 if (sched_verbose > 6)
1655 fprintf (sched_dump, ";; look at index %d + %d\n", q_ptr, stalls);
1656
1657 prev_link = 0;
1658 while (link)
1659 {
1660 next_link = XEXP (link, 1);
1661 insn = XEXP (link, 0);
1662 if (insn && sched_verbose > 6)
1663 print_rtl_single (sched_dump, insn);
1664
1665 memcpy (temp_state, state, dfa_state_size);
1666 if (recog_memoized (insn) < 0)
1667 /* non-negative to indicate that it's not ready
1668 to avoid infinite Q->R->Q->R... */
1669 cost = 0;
1670 else
1671 cost = state_transition (temp_state, insn);
1672
1673 if (sched_verbose >= 6)
1674 fprintf (sched_dump, "transition cost = %d\n", cost);
1675
1676 move_to_ready = false;
1677 if (cost < 0)
1678 {
1679 move_to_ready = ok_for_early_queue_removal (insn);
1680 if (move_to_ready == true)
1681 {
1682 /* move from Q to R */
1683 q_size -= 1;
1684 ready_add (ready, insn, false);
1685
1686 if (prev_link)
1687 XEXP (prev_link, 1) = next_link;
1688 else
1689 insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = next_link;
1690
1691 free_INSN_LIST_node (link);
1692
1693 if (sched_verbose >= 2)
1694 fprintf (sched_dump, ";;\t\tEarly Q-->Ready: insn %s\n",
1695 (*current_sched_info->print_insn) (insn, 0));
1696
1697 insns_removed++;
1698 if (insns_removed == flag_sched_stalled_insns)
1699 /* Remove no more than flag_sched_stalled_insns insns
1700 from Q at a time. */
1701 return insns_removed;
1702 }
1703 }
1704
1705 if (move_to_ready == false)
1706 prev_link = link;
1707
1708 link = next_link;
1709 } /* while link */
1710 } /* if link */
1711
1712 } /* for stalls.. */
1713
1714 return insns_removed;
1715 }
1716
1717
1718 /* Print the ready list for debugging purposes. Callable from debugger. */
1719
1720 static void
1721 debug_ready_list (struct ready_list *ready)
1722 {
1723 rtx *p;
1724 int i;
1725
1726 if (ready->n_ready == 0)
1727 {
1728 fprintf (sched_dump, "\n");
1729 return;
1730 }
1731
1732 p = ready_lastpos (ready);
1733 for (i = 0; i < ready->n_ready; i++)
1734 fprintf (sched_dump, " %s", (*current_sched_info->print_insn) (p[i], 0));
1735 fprintf (sched_dump, "\n");
1736 }
1737
1738 /* Search INSN for REG_SAVE_NOTE note pairs for
1739 NOTE_INSN_EHREGION_{BEG,END}; and convert them back into
1740 NOTEs. The REG_SAVE_NOTE note following first one is contains the
1741 saved value for NOTE_BLOCK_NUMBER which is useful for
1742 NOTE_INSN_EH_REGION_{BEG,END} NOTEs. */
1743
1744 static void
1745 reemit_notes (rtx insn)
1746 {
1747 rtx note, last = insn;
1748
1749 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1750 {
1751 if (REG_NOTE_KIND (note) == REG_SAVE_NOTE)
1752 {
1753 enum insn_note note_type = INTVAL (XEXP (note, 0));
1754
1755 last = emit_note_before (note_type, last);
1756 remove_note (insn, note);
1757 }
1758 }
1759 }
1760
1761 /* Move INSN. Reemit notes if needed. Update CFG, if needed. */
1762 static void
1763 move_insn (rtx insn)
1764 {
1765 rtx last = last_scheduled_insn;
1766
1767 if (PREV_INSN (insn) != last)
1768 {
1769 basic_block bb;
1770 rtx note;
1771 int jump_p = 0;
1772
1773 bb = BLOCK_FOR_INSN (insn);
1774
1775 /* BB_HEAD is either LABEL or NOTE. */
1776 gcc_assert (BB_HEAD (bb) != insn);
1777
1778 if (BB_END (bb) == insn)
1779 /* If this is last instruction in BB, move end marker one
1780 instruction up. */
1781 {
1782 /* Jumps are always placed at the end of basic block. */
1783 jump_p = control_flow_insn_p (insn);
1784
1785 gcc_assert (!jump_p
1786 || ((current_sched_info->flags & SCHED_RGN)
1787 && IS_SPECULATION_BRANCHY_CHECK_P (insn))
1788 || (current_sched_info->flags & SCHED_EBB));
1789
1790 gcc_assert (BLOCK_FOR_INSN (PREV_INSN (insn)) == bb);
1791
1792 BB_END (bb) = PREV_INSN (insn);
1793 }
1794
1795 gcc_assert (BB_END (bb) != last);
1796
1797 if (jump_p)
1798 /* We move the block note along with jump. */
1799 {
1800 /* NT is needed for assertion below. */
1801 rtx nt = current_sched_info->next_tail;
1802
1803 note = NEXT_INSN (insn);
1804 while (NOTE_NOT_BB_P (note) && note != nt)
1805 note = NEXT_INSN (note);
1806
1807 if (note != nt
1808 && (LABEL_P (note)
1809 || BARRIER_P (note)))
1810 note = NEXT_INSN (note);
1811
1812 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
1813 }
1814 else
1815 note = insn;
1816
1817 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (note);
1818 PREV_INSN (NEXT_INSN (note)) = PREV_INSN (insn);
1819
1820 NEXT_INSN (note) = NEXT_INSN (last);
1821 PREV_INSN (NEXT_INSN (last)) = note;
1822
1823 NEXT_INSN (last) = insn;
1824 PREV_INSN (insn) = last;
1825
1826 bb = BLOCK_FOR_INSN (last);
1827
1828 if (jump_p)
1829 {
1830 fix_jump_move (insn);
1831
1832 if (BLOCK_FOR_INSN (insn) != bb)
1833 move_block_after_check (insn);
1834
1835 gcc_assert (BB_END (bb) == last);
1836 }
1837
1838 set_block_for_insn (insn, bb);
1839 df_insn_change_bb (insn);
1840
1841 /* Update BB_END, if needed. */
1842 if (BB_END (bb) == last)
1843 BB_END (bb) = insn;
1844 }
1845
1846 reemit_notes (insn);
1847
1848 SCHED_GROUP_P (insn) = 0;
1849 }
1850
1851 /* The following structure describe an entry of the stack of choices. */
1852 struct choice_entry
1853 {
1854 /* Ordinal number of the issued insn in the ready queue. */
1855 int index;
1856 /* The number of the rest insns whose issues we should try. */
1857 int rest;
1858 /* The number of issued essential insns. */
1859 int n;
1860 /* State after issuing the insn. */
1861 state_t state;
1862 };
1863
1864 /* The following array is used to implement a stack of choices used in
1865 function max_issue. */
1866 static struct choice_entry *choice_stack;
1867
1868 /* The following variable value is number of essential insns issued on
1869 the current cycle. An insn is essential one if it changes the
1870 processors state. */
1871 static int cycle_issued_insns;
1872
1873 /* The following variable value is maximal number of tries of issuing
1874 insns for the first cycle multipass insn scheduling. We define
1875 this value as constant*(DFA_LOOKAHEAD**ISSUE_RATE). We would not
1876 need this constraint if all real insns (with non-negative codes)
1877 had reservations because in this case the algorithm complexity is
1878 O(DFA_LOOKAHEAD**ISSUE_RATE). Unfortunately, the dfa descriptions
1879 might be incomplete and such insn might occur. For such
1880 descriptions, the complexity of algorithm (without the constraint)
1881 could achieve DFA_LOOKAHEAD ** N , where N is the queue length. */
1882 static int max_lookahead_tries;
1883
1884 /* The following value is value of hook
1885 `first_cycle_multipass_dfa_lookahead' at the last call of
1886 `max_issue'. */
1887 static int cached_first_cycle_multipass_dfa_lookahead = 0;
1888
1889 /* The following value is value of `issue_rate' at the last call of
1890 `sched_init'. */
1891 static int cached_issue_rate = 0;
1892
1893 /* The following function returns maximal (or close to maximal) number
1894 of insns which can be issued on the same cycle and one of which
1895 insns is insns with the best rank (the first insn in READY). To
1896 make this function tries different samples of ready insns. READY
1897 is current queue `ready'. Global array READY_TRY reflects what
1898 insns are already issued in this try. MAX_POINTS is the sum of points
1899 of all instructions in READY. The function stops immediately,
1900 if it reached the such a solution, that all instruction can be issued.
1901 INDEX will contain index of the best insn in READY. The following
1902 function is used only for first cycle multipass scheduling. */
1903 static int
1904 max_issue (struct ready_list *ready, int *index, int max_points)
1905 {
1906 int n, i, all, n_ready, best, delay, tries_num, points = -1;
1907 struct choice_entry *top;
1908 rtx insn;
1909
1910 best = 0;
1911 memcpy (choice_stack->state, curr_state, dfa_state_size);
1912 top = choice_stack;
1913 top->rest = cached_first_cycle_multipass_dfa_lookahead;
1914 top->n = 0;
1915 n_ready = ready->n_ready;
1916 for (all = i = 0; i < n_ready; i++)
1917 if (!ready_try [i])
1918 all++;
1919 i = 0;
1920 tries_num = 0;
1921 for (;;)
1922 {
1923 if (top->rest == 0 || i >= n_ready)
1924 {
1925 if (top == choice_stack)
1926 break;
1927 if (best < top - choice_stack && ready_try [0])
1928 {
1929 best = top - choice_stack;
1930 *index = choice_stack [1].index;
1931 points = top->n;
1932 if (top->n == max_points || best == all)
1933 break;
1934 }
1935 i = top->index;
1936 ready_try [i] = 0;
1937 top--;
1938 memcpy (curr_state, top->state, dfa_state_size);
1939 }
1940 else if (!ready_try [i])
1941 {
1942 tries_num++;
1943 if (tries_num > max_lookahead_tries)
1944 break;
1945 insn = ready_element (ready, i);
1946 delay = state_transition (curr_state, insn);
1947 if (delay < 0)
1948 {
1949 if (state_dead_lock_p (curr_state))
1950 top->rest = 0;
1951 else
1952 top->rest--;
1953 n = top->n;
1954 if (memcmp (top->state, curr_state, dfa_state_size) != 0)
1955 n += ISSUE_POINTS (insn);
1956 top++;
1957 top->rest = cached_first_cycle_multipass_dfa_lookahead;
1958 top->index = i;
1959 top->n = n;
1960 memcpy (top->state, curr_state, dfa_state_size);
1961 ready_try [i] = 1;
1962 i = -1;
1963 }
1964 }
1965 i++;
1966 }
1967 while (top != choice_stack)
1968 {
1969 ready_try [top->index] = 0;
1970 top--;
1971 }
1972 memcpy (curr_state, choice_stack->state, dfa_state_size);
1973
1974 if (sched_verbose >= 4)
1975 fprintf (sched_dump, ";;\t\tChoosed insn : %s; points: %d/%d\n",
1976 (*current_sched_info->print_insn) (ready_element (ready, *index),
1977 0),
1978 points, max_points);
1979
1980 return best;
1981 }
1982
1983 /* The following function chooses insn from READY and modifies
1984 *N_READY and READY. The following function is used only for first
1985 cycle multipass scheduling.
1986 Return:
1987 -1 if cycle should be advanced,
1988 0 if INSN_PTR is set to point to the desirable insn,
1989 1 if choose_ready () should be restarted without advancing the cycle. */
1990 static int
1991 choose_ready (struct ready_list *ready, rtx *insn_ptr)
1992 {
1993 int lookahead;
1994
1995 if (dbg_cnt (sched_insn) == false)
1996 {
1997 rtx insn;
1998
1999 insn = next_nonnote_insn (last_scheduled_insn);
2000
2001 if (QUEUE_INDEX (insn) == QUEUE_READY)
2002 /* INSN is in the ready_list. */
2003 {
2004 ready_remove_insn (insn);
2005 *insn_ptr = insn;
2006 return 0;
2007 }
2008
2009 /* INSN is in the queue. Advance cycle to move it to the ready list. */
2010 return -1;
2011 }
2012
2013 lookahead = 0;
2014
2015 if (targetm.sched.first_cycle_multipass_dfa_lookahead)
2016 lookahead = targetm.sched.first_cycle_multipass_dfa_lookahead ();
2017 if (lookahead <= 0 || SCHED_GROUP_P (ready_element (ready, 0)))
2018 {
2019 *insn_ptr = ready_remove_first (ready);
2020 return 0;
2021 }
2022 else
2023 {
2024 /* Try to choose the better insn. */
2025 int index = 0, i, n;
2026 rtx insn;
2027 int more_issue, max_points, try_data = 1, try_control = 1;
2028
2029 if (cached_first_cycle_multipass_dfa_lookahead != lookahead)
2030 {
2031 cached_first_cycle_multipass_dfa_lookahead = lookahead;
2032 max_lookahead_tries = 100;
2033 for (i = 0; i < issue_rate; i++)
2034 max_lookahead_tries *= lookahead;
2035 }
2036 insn = ready_element (ready, 0);
2037 if (INSN_CODE (insn) < 0)
2038 {
2039 *insn_ptr = ready_remove_first (ready);
2040 return 0;
2041 }
2042
2043 if (spec_info
2044 && spec_info->flags & (PREFER_NON_DATA_SPEC
2045 | PREFER_NON_CONTROL_SPEC))
2046 {
2047 for (i = 0, n = ready->n_ready; i < n; i++)
2048 {
2049 rtx x;
2050 ds_t s;
2051
2052 x = ready_element (ready, i);
2053 s = TODO_SPEC (x);
2054
2055 if (spec_info->flags & PREFER_NON_DATA_SPEC
2056 && !(s & DATA_SPEC))
2057 {
2058 try_data = 0;
2059 if (!(spec_info->flags & PREFER_NON_CONTROL_SPEC)
2060 || !try_control)
2061 break;
2062 }
2063
2064 if (spec_info->flags & PREFER_NON_CONTROL_SPEC
2065 && !(s & CONTROL_SPEC))
2066 {
2067 try_control = 0;
2068 if (!(spec_info->flags & PREFER_NON_DATA_SPEC) || !try_data)
2069 break;
2070 }
2071 }
2072 }
2073
2074 if ((!try_data && (TODO_SPEC (insn) & DATA_SPEC))
2075 || (!try_control && (TODO_SPEC (insn) & CONTROL_SPEC))
2076 || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard_spec
2077 && !targetm.sched.first_cycle_multipass_dfa_lookahead_guard_spec
2078 (insn)))
2079 /* Discard speculative instruction that stands first in the ready
2080 list. */
2081 {
2082 change_queue_index (insn, 1);
2083 return 1;
2084 }
2085
2086 max_points = ISSUE_POINTS (insn);
2087 more_issue = issue_rate - cycle_issued_insns - 1;
2088
2089 for (i = 1; i < ready->n_ready; i++)
2090 {
2091 insn = ready_element (ready, i);
2092 ready_try [i]
2093 = (INSN_CODE (insn) < 0
2094 || (!try_data && (TODO_SPEC (insn) & DATA_SPEC))
2095 || (!try_control && (TODO_SPEC (insn) & CONTROL_SPEC))
2096 || (targetm.sched.first_cycle_multipass_dfa_lookahead_guard
2097 && !targetm.sched.first_cycle_multipass_dfa_lookahead_guard
2098 (insn)));
2099
2100 if (!ready_try [i] && more_issue-- > 0)
2101 max_points += ISSUE_POINTS (insn);
2102 }
2103
2104 if (max_issue (ready, &index, max_points) == 0)
2105 {
2106 *insn_ptr = ready_remove_first (ready);
2107 return 0;
2108 }
2109 else
2110 {
2111 *insn_ptr = ready_remove (ready, index);
2112 return 0;
2113 }
2114 }
2115 }
2116
2117 /* Use forward list scheduling to rearrange insns of block pointed to by
2118 TARGET_BB, possibly bringing insns from subsequent blocks in the same
2119 region. */
2120
2121 void
2122 schedule_block (basic_block *target_bb, int rgn_n_insns1)
2123 {
2124 struct ready_list ready;
2125 int i, first_cycle_insn_p;
2126 int can_issue_more;
2127 state_t temp_state = NULL; /* It is used for multipass scheduling. */
2128 int sort_p, advance, start_clock_var;
2129
2130 /* Head/tail info for this block. */
2131 rtx prev_head = current_sched_info->prev_head;
2132 rtx next_tail = current_sched_info->next_tail;
2133 rtx head = NEXT_INSN (prev_head);
2134 rtx tail = PREV_INSN (next_tail);
2135
2136 /* We used to have code to avoid getting parameters moved from hard
2137 argument registers into pseudos.
2138
2139 However, it was removed when it proved to be of marginal benefit
2140 and caused problems because schedule_block and compute_forward_dependences
2141 had different notions of what the "head" insn was. */
2142
2143 gcc_assert (head != tail || INSN_P (head));
2144
2145 haifa_recovery_bb_recently_added_p = false;
2146
2147 /* Debug info. */
2148 if (sched_verbose)
2149 dump_new_block_header (0, *target_bb, head, tail);
2150
2151 state_reset (curr_state);
2152
2153 /* Allocate the ready list. */
2154 readyp = &ready;
2155 ready.vec = NULL;
2156 ready_try = NULL;
2157 choice_stack = NULL;
2158
2159 rgn_n_insns = -1;
2160 extend_ready (rgn_n_insns1 + 1);
2161
2162 ready.first = ready.veclen - 1;
2163 ready.n_ready = 0;
2164
2165 /* It is used for first cycle multipass scheduling. */
2166 temp_state = alloca (dfa_state_size);
2167
2168 if (targetm.sched.md_init)
2169 targetm.sched.md_init (sched_dump, sched_verbose, ready.veclen);
2170
2171 /* We start inserting insns after PREV_HEAD. */
2172 last_scheduled_insn = prev_head;
2173
2174 gcc_assert (NOTE_P (last_scheduled_insn)
2175 && BLOCK_FOR_INSN (last_scheduled_insn) == *target_bb);
2176
2177 /* Initialize INSN_QUEUE. Q_SIZE is the total number of insns in the
2178 queue. */
2179 q_ptr = 0;
2180 q_size = 0;
2181
2182 insn_queue = alloca ((max_insn_queue_index + 1) * sizeof (rtx));
2183 memset (insn_queue, 0, (max_insn_queue_index + 1) * sizeof (rtx));
2184
2185 /* Start just before the beginning of time. */
2186 clock_var = -1;
2187
2188 /* We need queue and ready lists and clock_var be initialized
2189 in try_ready () (which is called through init_ready_list ()). */
2190 (*current_sched_info->init_ready_list) ();
2191
2192 /* The algorithm is O(n^2) in the number of ready insns at any given
2193 time in the worst case. Before reload we are more likely to have
2194 big lists so truncate them to a reasonable size. */
2195 if (!reload_completed && ready.n_ready > MAX_SCHED_READY_INSNS)
2196 {
2197 ready_sort (&ready);
2198
2199 /* Find first free-standing insn past MAX_SCHED_READY_INSNS. */
2200 for (i = MAX_SCHED_READY_INSNS; i < ready.n_ready; i++)
2201 if (!SCHED_GROUP_P (ready_element (&ready, i)))
2202 break;
2203
2204 if (sched_verbose >= 2)
2205 {
2206 fprintf (sched_dump,
2207 ";;\t\tReady list on entry: %d insns\n", ready.n_ready);
2208 fprintf (sched_dump,
2209 ";;\t\t before reload => truncated to %d insns\n", i);
2210 }
2211
2212 /* Delay all insns past it for 1 cycle. If debug counter is
2213 activated make an exception for the insn right after
2214 last_scheduled_insn. */
2215 {
2216 rtx skip_insn;
2217
2218 if (dbg_cnt (sched_insn) == false)
2219 skip_insn = next_nonnote_insn (last_scheduled_insn);
2220 else
2221 skip_insn = NULL_RTX;
2222
2223 while (i < ready.n_ready)
2224 {
2225 rtx insn;
2226
2227 insn = ready_remove (&ready, i);
2228
2229 if (insn != skip_insn)
2230 queue_insn (insn, 1);
2231 }
2232 }
2233 }
2234
2235 /* Now we can restore basic block notes and maintain precise cfg. */
2236 restore_bb_notes (*target_bb);
2237
2238 last_clock_var = -1;
2239
2240 advance = 0;
2241
2242 sort_p = TRUE;
2243 /* Loop until all the insns in BB are scheduled. */
2244 while ((*current_sched_info->schedule_more_p) ())
2245 {
2246 do
2247 {
2248 start_clock_var = clock_var;
2249
2250 clock_var++;
2251
2252 advance_one_cycle ();
2253
2254 /* Add to the ready list all pending insns that can be issued now.
2255 If there are no ready insns, increment clock until one
2256 is ready and add all pending insns at that point to the ready
2257 list. */
2258 queue_to_ready (&ready);
2259
2260 gcc_assert (ready.n_ready);
2261
2262 if (sched_verbose >= 2)
2263 {
2264 fprintf (sched_dump, ";;\t\tReady list after queue_to_ready: ");
2265 debug_ready_list (&ready);
2266 }
2267 advance -= clock_var - start_clock_var;
2268 }
2269 while (advance > 0);
2270
2271 if (sort_p)
2272 {
2273 /* Sort the ready list based on priority. */
2274 ready_sort (&ready);
2275
2276 if (sched_verbose >= 2)
2277 {
2278 fprintf (sched_dump, ";;\t\tReady list after ready_sort: ");
2279 debug_ready_list (&ready);
2280 }
2281 }
2282
2283 /* Allow the target to reorder the list, typically for
2284 better instruction bundling. */
2285 if (sort_p && targetm.sched.reorder
2286 && (ready.n_ready == 0
2287 || !SCHED_GROUP_P (ready_element (&ready, 0))))
2288 can_issue_more =
2289 targetm.sched.reorder (sched_dump, sched_verbose,
2290 ready_lastpos (&ready),
2291 &ready.n_ready, clock_var);
2292 else
2293 can_issue_more = issue_rate;
2294
2295 first_cycle_insn_p = 1;
2296 cycle_issued_insns = 0;
2297 for (;;)
2298 {
2299 rtx insn;
2300 int cost;
2301 bool asm_p = false;
2302
2303 if (sched_verbose >= 2)
2304 {
2305 fprintf (sched_dump, ";;\tReady list (t = %3d): ",
2306 clock_var);
2307 debug_ready_list (&ready);
2308 }
2309
2310 if (ready.n_ready == 0
2311 && can_issue_more
2312 && reload_completed)
2313 {
2314 /* Allow scheduling insns directly from the queue in case
2315 there's nothing better to do (ready list is empty) but
2316 there are still vacant dispatch slots in the current cycle. */
2317 if (sched_verbose >= 6)
2318 fprintf (sched_dump,";;\t\tSecond chance\n");
2319 memcpy (temp_state, curr_state, dfa_state_size);
2320 if (early_queue_to_ready (temp_state, &ready))
2321 ready_sort (&ready);
2322 }
2323
2324 if (ready.n_ready == 0 || !can_issue_more
2325 || state_dead_lock_p (curr_state)
2326 || !(*current_sched_info->schedule_more_p) ())
2327 break;
2328
2329 /* Select and remove the insn from the ready list. */
2330 if (sort_p)
2331 {
2332 int res;
2333
2334 insn = NULL_RTX;
2335 res = choose_ready (&ready, &insn);
2336
2337 if (res < 0)
2338 /* Finish cycle. */
2339 break;
2340 if (res > 0)
2341 /* Restart choose_ready (). */
2342 continue;
2343
2344 gcc_assert (insn != NULL_RTX);
2345 }
2346 else
2347 insn = ready_remove_first (&ready);
2348
2349 if (targetm.sched.dfa_new_cycle
2350 && targetm.sched.dfa_new_cycle (sched_dump, sched_verbose,
2351 insn, last_clock_var,
2352 clock_var, &sort_p))
2353 /* SORT_P is used by the target to override sorting
2354 of the ready list. This is needed when the target
2355 has modified its internal structures expecting that
2356 the insn will be issued next. As we need the insn
2357 to have the highest priority (so it will be returned by
2358 the ready_remove_first call above), we invoke
2359 ready_add (&ready, insn, true).
2360 But, still, there is one issue: INSN can be later
2361 discarded by scheduler's front end through
2362 current_sched_info->can_schedule_ready_p, hence, won't
2363 be issued next. */
2364 {
2365 ready_add (&ready, insn, true);
2366 break;
2367 }
2368
2369 sort_p = TRUE;
2370 memcpy (temp_state, curr_state, dfa_state_size);
2371 if (recog_memoized (insn) < 0)
2372 {
2373 asm_p = (GET_CODE (PATTERN (insn)) == ASM_INPUT
2374 || asm_noperands (PATTERN (insn)) >= 0);
2375 if (!first_cycle_insn_p && asm_p)
2376 /* This is asm insn which is tryed to be issued on the
2377 cycle not first. Issue it on the next cycle. */
2378 cost = 1;
2379 else
2380 /* A USE insn, or something else we don't need to
2381 understand. We can't pass these directly to
2382 state_transition because it will trigger a
2383 fatal error for unrecognizable insns. */
2384 cost = 0;
2385 }
2386 else
2387 {
2388 cost = state_transition (temp_state, insn);
2389 if (cost < 0)
2390 cost = 0;
2391 else if (cost == 0)
2392 cost = 1;
2393 }
2394
2395 if (cost >= 1)
2396 {
2397 queue_insn (insn, cost);
2398 if (SCHED_GROUP_P (insn))
2399 {
2400 advance = cost;
2401 break;
2402 }
2403
2404 continue;
2405 }
2406
2407 if (current_sched_info->can_schedule_ready_p
2408 && ! (*current_sched_info->can_schedule_ready_p) (insn))
2409 /* We normally get here only if we don't want to move
2410 insn from the split block. */
2411 {
2412 TODO_SPEC (insn) = (TODO_SPEC (insn) & ~SPECULATIVE) | HARD_DEP;
2413 continue;
2414 }
2415
2416 /* DECISION is made. */
2417
2418 if (TODO_SPEC (insn) & SPECULATIVE)
2419 generate_recovery_code (insn);
2420
2421 if (control_flow_insn_p (last_scheduled_insn)
2422 /* This is used to switch basic blocks by request
2423 from scheduler front-end (actually, sched-ebb.c only).
2424 This is used to process blocks with single fallthru
2425 edge. If succeeding block has jump, it [jump] will try
2426 move at the end of current bb, thus corrupting CFG. */
2427 || current_sched_info->advance_target_bb (*target_bb, insn))
2428 {
2429 *target_bb = current_sched_info->advance_target_bb
2430 (*target_bb, 0);
2431
2432 if (sched_verbose)
2433 {
2434 rtx x;
2435
2436 x = next_real_insn (last_scheduled_insn);
2437 gcc_assert (x);
2438 dump_new_block_header (1, *target_bb, x, tail);
2439 }
2440
2441 last_scheduled_insn = bb_note (*target_bb);
2442 }
2443
2444 /* Update counters, etc in the scheduler's front end. */
2445 (*current_sched_info->begin_schedule_ready) (insn,
2446 last_scheduled_insn);
2447
2448 move_insn (insn);
2449 last_scheduled_insn = insn;
2450
2451 if (memcmp (curr_state, temp_state, dfa_state_size) != 0)
2452 {
2453 cycle_issued_insns++;
2454 memcpy (curr_state, temp_state, dfa_state_size);
2455 }
2456
2457 if (targetm.sched.variable_issue)
2458 can_issue_more =
2459 targetm.sched.variable_issue (sched_dump, sched_verbose,
2460 insn, can_issue_more);
2461 /* A naked CLOBBER or USE generates no instruction, so do
2462 not count them against the issue rate. */
2463 else if (GET_CODE (PATTERN (insn)) != USE
2464 && GET_CODE (PATTERN (insn)) != CLOBBER)
2465 can_issue_more--;
2466
2467 advance = schedule_insn (insn);
2468
2469 /* After issuing an asm insn we should start a new cycle. */
2470 if (advance == 0 && asm_p)
2471 advance = 1;
2472 if (advance != 0)
2473 break;
2474
2475 first_cycle_insn_p = 0;
2476
2477 /* Sort the ready list based on priority. This must be
2478 redone here, as schedule_insn may have readied additional
2479 insns that will not be sorted correctly. */
2480 if (ready.n_ready > 0)
2481 ready_sort (&ready);
2482
2483 if (targetm.sched.reorder2
2484 && (ready.n_ready == 0
2485 || !SCHED_GROUP_P (ready_element (&ready, 0))))
2486 {
2487 can_issue_more =
2488 targetm.sched.reorder2 (sched_dump, sched_verbose,
2489 ready.n_ready
2490 ? ready_lastpos (&ready) : NULL,
2491 &ready.n_ready, clock_var);
2492 }
2493 }
2494 }
2495
2496 /* Debug info. */
2497 if (sched_verbose)
2498 {
2499 fprintf (sched_dump, ";;\tReady list (final): ");
2500 debug_ready_list (&ready);
2501 }
2502
2503 if (current_sched_info->queue_must_finish_empty)
2504 /* Sanity check -- queue must be empty now. Meaningless if region has
2505 multiple bbs. */
2506 gcc_assert (!q_size && !ready.n_ready);
2507 else
2508 {
2509 /* We must maintain QUEUE_INDEX between blocks in region. */
2510 for (i = ready.n_ready - 1; i >= 0; i--)
2511 {
2512 rtx x;
2513
2514 x = ready_element (&ready, i);
2515 QUEUE_INDEX (x) = QUEUE_NOWHERE;
2516 TODO_SPEC (x) = (TODO_SPEC (x) & ~SPECULATIVE) | HARD_DEP;
2517 }
2518
2519 if (q_size)
2520 for (i = 0; i <= max_insn_queue_index; i++)
2521 {
2522 rtx link;
2523 for (link = insn_queue[i]; link; link = XEXP (link, 1))
2524 {
2525 rtx x;
2526
2527 x = XEXP (link, 0);
2528 QUEUE_INDEX (x) = QUEUE_NOWHERE;
2529 TODO_SPEC (x) = (TODO_SPEC (x) & ~SPECULATIVE) | HARD_DEP;
2530 }
2531 free_INSN_LIST_list (&insn_queue[i]);
2532 }
2533 }
2534
2535 if (!current_sched_info->queue_must_finish_empty
2536 || haifa_recovery_bb_recently_added_p)
2537 {
2538 /* INSN_TICK (minimum clock tick at which the insn becomes
2539 ready) may be not correct for the insn in the subsequent
2540 blocks of the region. We should use a correct value of
2541 `clock_var' or modify INSN_TICK. It is better to keep
2542 clock_var value equal to 0 at the start of a basic block.
2543 Therefore we modify INSN_TICK here. */
2544 fix_inter_tick (NEXT_INSN (prev_head), last_scheduled_insn);
2545 }
2546
2547 if (targetm.sched.md_finish)
2548 {
2549 targetm.sched.md_finish (sched_dump, sched_verbose);
2550
2551 /* Target might have added some instructions to the scheduled block.
2552 in its md_finish () hook. These new insns don't have any data
2553 initialized and to identify them we extend h_i_d so that they'll
2554 get zero luids.*/
2555 extend_h_i_d ();
2556 }
2557
2558 /* Update head/tail boundaries. */
2559 head = NEXT_INSN (prev_head);
2560 tail = last_scheduled_insn;
2561
2562 /* Restore-other-notes: NOTE_LIST is the end of a chain of notes
2563 previously found among the insns. Insert them at the beginning
2564 of the insns. */
2565 if (note_list != 0)
2566 {
2567 basic_block head_bb = BLOCK_FOR_INSN (head);
2568 rtx note_head = note_list;
2569
2570 while (PREV_INSN (note_head))
2571 {
2572 set_block_for_insn (note_head, head_bb);
2573 note_head = PREV_INSN (note_head);
2574 }
2575 /* In the above cycle we've missed this note: */
2576 set_block_for_insn (note_head, head_bb);
2577
2578 PREV_INSN (note_head) = PREV_INSN (head);
2579 NEXT_INSN (PREV_INSN (head)) = note_head;
2580 PREV_INSN (head) = note_list;
2581 NEXT_INSN (note_list) = head;
2582 head = note_head;
2583 }
2584
2585 /* Debugging. */
2586 if (sched_verbose)
2587 {
2588 fprintf (sched_dump, ";; total time = %d\n;; new head = %d\n",
2589 clock_var, INSN_UID (head));
2590 fprintf (sched_dump, ";; new tail = %d\n\n",
2591 INSN_UID (tail));
2592 }
2593
2594 current_sched_info->head = head;
2595 current_sched_info->tail = tail;
2596
2597 free (ready.vec);
2598
2599 free (ready_try);
2600 for (i = 0; i <= rgn_n_insns; i++)
2601 free (choice_stack [i].state);
2602 free (choice_stack);
2603 }
2604 \f
2605 /* Set_priorities: compute priority of each insn in the block. */
2606
2607 int
2608 set_priorities (rtx head, rtx tail)
2609 {
2610 rtx insn;
2611 int n_insn;
2612 int sched_max_insns_priority =
2613 current_sched_info->sched_max_insns_priority;
2614 rtx prev_head;
2615
2616 if (head == tail && (! INSN_P (head)))
2617 return 0;
2618
2619 n_insn = 0;
2620
2621 prev_head = PREV_INSN (head);
2622 for (insn = tail; insn != prev_head; insn = PREV_INSN (insn))
2623 {
2624 if (!INSN_P (insn))
2625 continue;
2626
2627 n_insn++;
2628 (void) priority (insn);
2629
2630 gcc_assert (INSN_PRIORITY_KNOWN (insn));
2631
2632 sched_max_insns_priority = MAX (sched_max_insns_priority,
2633 INSN_PRIORITY (insn));
2634 }
2635
2636 current_sched_info->sched_max_insns_priority = sched_max_insns_priority;
2637
2638 return n_insn;
2639 }
2640
2641 /* Next LUID to assign to an instruction. */
2642 static int luid;
2643
2644 /* Initialize some global state for the scheduler. */
2645
2646 void
2647 sched_init (void)
2648 {
2649 basic_block b;
2650 rtx insn;
2651 int i;
2652
2653 /* Switch to working copy of sched_info. */
2654 memcpy (&current_sched_info_var, current_sched_info,
2655 sizeof (current_sched_info_var));
2656 current_sched_info = &current_sched_info_var;
2657
2658 /* Disable speculative loads in their presence if cc0 defined. */
2659 #ifdef HAVE_cc0
2660 flag_schedule_speculative_load = 0;
2661 #endif
2662
2663 /* Set dump and sched_verbose for the desired debugging output. If no
2664 dump-file was specified, but -fsched-verbose=N (any N), print to stderr.
2665 For -fsched-verbose=N, N>=10, print everything to stderr. */
2666 sched_verbose = sched_verbose_param;
2667 if (sched_verbose_param == 0 && dump_file)
2668 sched_verbose = 1;
2669 sched_dump = ((sched_verbose_param >= 10 || !dump_file)
2670 ? stderr : dump_file);
2671
2672 /* Initialize SPEC_INFO. */
2673 if (targetm.sched.set_sched_flags)
2674 {
2675 spec_info = &spec_info_var;
2676 targetm.sched.set_sched_flags (spec_info);
2677 if (current_sched_info->flags & DO_SPECULATION)
2678 spec_info->weakness_cutoff =
2679 (PARAM_VALUE (PARAM_SCHED_SPEC_PROB_CUTOFF) * MAX_DEP_WEAK) / 100;
2680 else
2681 /* So we won't read anything accidentally. */
2682 spec_info = 0;
2683 }
2684 else
2685 /* So we won't read anything accidentally. */
2686 spec_info = 0;
2687
2688 /* Initialize issue_rate. */
2689 if (targetm.sched.issue_rate)
2690 issue_rate = targetm.sched.issue_rate ();
2691 else
2692 issue_rate = 1;
2693
2694 if (cached_issue_rate != issue_rate)
2695 {
2696 cached_issue_rate = issue_rate;
2697 /* To invalidate max_lookahead_tries: */
2698 cached_first_cycle_multipass_dfa_lookahead = 0;
2699 }
2700
2701 old_max_uid = 0;
2702 h_i_d = 0;
2703 extend_h_i_d ();
2704
2705 for (i = 0; i < old_max_uid; i++)
2706 {
2707 h_i_d[i].cost = -1;
2708 h_i_d[i].todo_spec = HARD_DEP;
2709 h_i_d[i].queue_index = QUEUE_NOWHERE;
2710 h_i_d[i].tick = INVALID_TICK;
2711 h_i_d[i].inter_tick = INVALID_TICK;
2712 }
2713
2714 if (targetm.sched.init_dfa_pre_cycle_insn)
2715 targetm.sched.init_dfa_pre_cycle_insn ();
2716
2717 if (targetm.sched.init_dfa_post_cycle_insn)
2718 targetm.sched.init_dfa_post_cycle_insn ();
2719
2720 dfa_start ();
2721 dfa_state_size = state_size ();
2722 curr_state = xmalloc (dfa_state_size);
2723
2724 h_i_d[0].luid = 0;
2725 luid = 1;
2726 FOR_EACH_BB (b)
2727 for (insn = BB_HEAD (b); ; insn = NEXT_INSN (insn))
2728 {
2729 INSN_LUID (insn) = luid;
2730
2731 /* Increment the next luid, unless this is a note. We don't
2732 really need separate IDs for notes and we don't want to
2733 schedule differently depending on whether or not there are
2734 line-number notes, i.e., depending on whether or not we're
2735 generating debugging information. */
2736 if (!NOTE_P (insn))
2737 ++luid;
2738
2739 if (insn == BB_END (b))
2740 break;
2741 }
2742
2743 init_dependency_caches (luid);
2744
2745 init_alias_analysis ();
2746
2747 old_last_basic_block = 0;
2748 extend_bb ();
2749
2750 /* Compute INSN_REG_WEIGHT for all blocks. We must do this before
2751 removing death notes. */
2752 FOR_EACH_BB_REVERSE (b)
2753 find_insn_reg_weight (b);
2754
2755 if (targetm.sched.md_init_global)
2756 targetm.sched.md_init_global (sched_dump, sched_verbose, old_max_uid);
2757
2758 nr_begin_data = nr_begin_control = nr_be_in_data = nr_be_in_control = 0;
2759 before_recovery = 0;
2760
2761 haifa_recovery_bb_ever_added_p = false;
2762
2763 #ifdef ENABLE_CHECKING
2764 /* This is used preferably for finding bugs in check_cfg () itself. */
2765 check_cfg (0, 0);
2766 #endif
2767 }
2768
2769 /* Free global data used during insn scheduling. */
2770
2771 void
2772 sched_finish (void)
2773 {
2774 free (h_i_d);
2775 free (curr_state);
2776 dfa_finish ();
2777 free_dependency_caches ();
2778 end_alias_analysis ();
2779
2780 if (targetm.sched.md_finish_global)
2781 targetm.sched.md_finish_global (sched_dump, sched_verbose);
2782
2783 if (spec_info && spec_info->dump)
2784 {
2785 char c = reload_completed ? 'a' : 'b';
2786
2787 fprintf (spec_info->dump,
2788 ";; %s:\n", current_function_name ());
2789
2790 fprintf (spec_info->dump,
2791 ";; Procedure %cr-begin-data-spec motions == %d\n",
2792 c, nr_begin_data);
2793 fprintf (spec_info->dump,
2794 ";; Procedure %cr-be-in-data-spec motions == %d\n",
2795 c, nr_be_in_data);
2796 fprintf (spec_info->dump,
2797 ";; Procedure %cr-begin-control-spec motions == %d\n",
2798 c, nr_begin_control);
2799 fprintf (spec_info->dump,
2800 ";; Procedure %cr-be-in-control-spec motions == %d\n",
2801 c, nr_be_in_control);
2802 }
2803
2804 #ifdef ENABLE_CHECKING
2805 /* After reload ia64 backend clobbers CFG, so can't check anything. */
2806 if (!reload_completed)
2807 check_cfg (0, 0);
2808 #endif
2809
2810 current_sched_info = NULL;
2811 }
2812
2813 /* Fix INSN_TICKs of the instructions in the current block as well as
2814 INSN_TICKs of their dependents.
2815 HEAD and TAIL are the begin and the end of the current scheduled block. */
2816 static void
2817 fix_inter_tick (rtx head, rtx tail)
2818 {
2819 /* Set of instructions with corrected INSN_TICK. */
2820 bitmap_head processed;
2821 /* ??? It is doubtful if we should assume that cycle advance happens on
2822 basic block boundaries. Basically insns that are unconditionally ready
2823 on the start of the block are more preferable then those which have
2824 a one cycle dependency over insn from the previous block. */
2825 int next_clock = clock_var + 1;
2826
2827 bitmap_initialize (&processed, 0);
2828
2829 /* Iterates over scheduled instructions and fix their INSN_TICKs and
2830 INSN_TICKs of dependent instructions, so that INSN_TICKs are consistent
2831 across different blocks. */
2832 for (tail = NEXT_INSN (tail); head != tail; head = NEXT_INSN (head))
2833 {
2834 if (INSN_P (head))
2835 {
2836 int tick;
2837 sd_iterator_def sd_it;
2838 dep_t dep;
2839
2840 tick = INSN_TICK (head);
2841 gcc_assert (tick >= MIN_TICK);
2842
2843 /* Fix INSN_TICK of instruction from just scheduled block. */
2844 if (!bitmap_bit_p (&processed, INSN_LUID (head)))
2845 {
2846 bitmap_set_bit (&processed, INSN_LUID (head));
2847 tick -= next_clock;
2848
2849 if (tick < MIN_TICK)
2850 tick = MIN_TICK;
2851
2852 INSN_TICK (head) = tick;
2853 }
2854
2855 FOR_EACH_DEP (head, SD_LIST_RES_FORW, sd_it, dep)
2856 {
2857 rtx next;
2858
2859 next = DEP_CON (dep);
2860 tick = INSN_TICK (next);
2861
2862 if (tick != INVALID_TICK
2863 /* If NEXT has its INSN_TICK calculated, fix it.
2864 If not - it will be properly calculated from
2865 scratch later in fix_tick_ready. */
2866 && !bitmap_bit_p (&processed, INSN_LUID (next)))
2867 {
2868 bitmap_set_bit (&processed, INSN_LUID (next));
2869 tick -= next_clock;
2870
2871 if (tick < MIN_TICK)
2872 tick = MIN_TICK;
2873
2874 if (tick > INTER_TICK (next))
2875 INTER_TICK (next) = tick;
2876 else
2877 tick = INTER_TICK (next);
2878
2879 INSN_TICK (next) = tick;
2880 }
2881 }
2882 }
2883 }
2884 bitmap_clear (&processed);
2885 }
2886
2887 /* Check if NEXT is ready to be added to the ready or queue list.
2888 If "yes", add it to the proper list.
2889 Returns:
2890 -1 - is not ready yet,
2891 0 - added to the ready list,
2892 0 < N - queued for N cycles. */
2893 int
2894 try_ready (rtx next)
2895 {
2896 ds_t old_ts, *ts;
2897
2898 ts = &TODO_SPEC (next);
2899 old_ts = *ts;
2900
2901 gcc_assert (!(old_ts & ~(SPECULATIVE | HARD_DEP))
2902 && ((old_ts & HARD_DEP)
2903 || (old_ts & SPECULATIVE)));
2904
2905 if (sd_lists_empty_p (next, SD_LIST_BACK))
2906 /* NEXT has all its dependencies resolved. */
2907 {
2908 /* Remove HARD_DEP bit from NEXT's status. */
2909 *ts &= ~HARD_DEP;
2910
2911 if (current_sched_info->flags & DO_SPECULATION)
2912 /* Remove all speculative bits from NEXT's status. */
2913 *ts &= ~SPECULATIVE;
2914 }
2915 else
2916 {
2917 /* One of the NEXT's dependencies has been resolved.
2918 Recalculate NEXT's status. */
2919
2920 *ts &= ~SPECULATIVE & ~HARD_DEP;
2921
2922 if (sd_lists_empty_p (next, SD_LIST_HARD_BACK))
2923 /* Now we've got NEXT with speculative deps only.
2924 1. Look at the deps to see what we have to do.
2925 2. Check if we can do 'todo'. */
2926 {
2927 sd_iterator_def sd_it;
2928 dep_t dep;
2929 bool first_p = true;
2930
2931 FOR_EACH_DEP (next, SD_LIST_BACK, sd_it, dep)
2932 {
2933 ds_t ds = DEP_STATUS (dep) & SPECULATIVE;
2934
2935 if (first_p)
2936 {
2937 first_p = false;
2938
2939 *ts = ds;
2940 }
2941 else
2942 *ts = ds_merge (*ts, ds);
2943 }
2944
2945 if (dep_weak (*ts) < spec_info->weakness_cutoff)
2946 /* Too few points. */
2947 *ts = (*ts & ~SPECULATIVE) | HARD_DEP;
2948 }
2949 else
2950 *ts |= HARD_DEP;
2951 }
2952
2953 if (*ts & HARD_DEP)
2954 gcc_assert (*ts == old_ts
2955 && QUEUE_INDEX (next) == QUEUE_NOWHERE);
2956 else if (current_sched_info->new_ready)
2957 *ts = current_sched_info->new_ready (next, *ts);
2958
2959 /* * if !(old_ts & SPECULATIVE) (e.g. HARD_DEP or 0), then insn might
2960 have its original pattern or changed (speculative) one. This is due
2961 to changing ebb in region scheduling.
2962 * But if (old_ts & SPECULATIVE), then we are pretty sure that insn
2963 has speculative pattern.
2964
2965 We can't assert (!(*ts & HARD_DEP) || *ts == old_ts) here because
2966 control-speculative NEXT could have been discarded by sched-rgn.c
2967 (the same case as when discarded by can_schedule_ready_p ()). */
2968
2969 if ((*ts & SPECULATIVE)
2970 /* If (old_ts == *ts), then (old_ts & SPECULATIVE) and we don't
2971 need to change anything. */
2972 && *ts != old_ts)
2973 {
2974 int res;
2975 rtx new_pat;
2976
2977 gcc_assert ((*ts & SPECULATIVE) && !(*ts & ~SPECULATIVE));
2978
2979 res = speculate_insn (next, *ts, &new_pat);
2980
2981 switch (res)
2982 {
2983 case -1:
2984 /* It would be nice to change DEP_STATUS of all dependences,
2985 which have ((DEP_STATUS & SPECULATIVE) == *ts) to HARD_DEP,
2986 so we won't reanalyze anything. */
2987 *ts = (*ts & ~SPECULATIVE) | HARD_DEP;
2988 break;
2989
2990 case 0:
2991 /* We follow the rule, that every speculative insn
2992 has non-null ORIG_PAT. */
2993 if (!ORIG_PAT (next))
2994 ORIG_PAT (next) = PATTERN (next);
2995 break;
2996
2997 case 1:
2998 if (!ORIG_PAT (next))
2999 /* If we gonna to overwrite the original pattern of insn,
3000 save it. */
3001 ORIG_PAT (next) = PATTERN (next);
3002
3003 change_pattern (next, new_pat);
3004 break;
3005
3006 default:
3007 gcc_unreachable ();
3008 }
3009 }
3010
3011 /* We need to restore pattern only if (*ts == 0), because otherwise it is
3012 either correct (*ts & SPECULATIVE),
3013 or we simply don't care (*ts & HARD_DEP). */
3014
3015 gcc_assert (!ORIG_PAT (next)
3016 || !IS_SPECULATION_BRANCHY_CHECK_P (next));
3017
3018 if (*ts & HARD_DEP)
3019 {
3020 /* We can't assert (QUEUE_INDEX (next) == QUEUE_NOWHERE) here because
3021 control-speculative NEXT could have been discarded by sched-rgn.c
3022 (the same case as when discarded by can_schedule_ready_p ()). */
3023 /*gcc_assert (QUEUE_INDEX (next) == QUEUE_NOWHERE);*/
3024
3025 change_queue_index (next, QUEUE_NOWHERE);
3026 return -1;
3027 }
3028 else if (!(*ts & BEGIN_SPEC) && ORIG_PAT (next) && !IS_SPECULATION_CHECK_P (next))
3029 /* We should change pattern of every previously speculative
3030 instruction - and we determine if NEXT was speculative by using
3031 ORIG_PAT field. Except one case - speculation checks have ORIG_PAT
3032 pat too, so skip them. */
3033 {
3034 change_pattern (next, ORIG_PAT (next));
3035 ORIG_PAT (next) = 0;
3036 }
3037
3038 if (sched_verbose >= 2)
3039 {
3040 int s = TODO_SPEC (next);
3041
3042 fprintf (sched_dump, ";;\t\tdependencies resolved: insn %s",
3043 (*current_sched_info->print_insn) (next, 0));
3044
3045 if (spec_info && spec_info->dump)
3046 {
3047 if (s & BEGIN_DATA)
3048 fprintf (spec_info->dump, "; data-spec;");
3049 if (s & BEGIN_CONTROL)
3050 fprintf (spec_info->dump, "; control-spec;");
3051 if (s & BE_IN_CONTROL)
3052 fprintf (spec_info->dump, "; in-control-spec;");
3053 }
3054
3055 fprintf (sched_dump, "\n");
3056 }
3057
3058 adjust_priority (next);
3059
3060 return fix_tick_ready (next);
3061 }
3062
3063 /* Calculate INSN_TICK of NEXT and add it to either ready or queue list. */
3064 static int
3065 fix_tick_ready (rtx next)
3066 {
3067 int tick, delay;
3068
3069 if (!sd_lists_empty_p (next, SD_LIST_RES_BACK))
3070 {
3071 int full_p;
3072 sd_iterator_def sd_it;
3073 dep_t dep;
3074
3075 tick = INSN_TICK (next);
3076 /* if tick is not equal to INVALID_TICK, then update
3077 INSN_TICK of NEXT with the most recent resolved dependence
3078 cost. Otherwise, recalculate from scratch. */
3079 full_p = (tick == INVALID_TICK);
3080
3081 FOR_EACH_DEP (next, SD_LIST_RES_BACK, sd_it, dep)
3082 {
3083 rtx pro = DEP_PRO (dep);
3084 int tick1;
3085
3086 gcc_assert (INSN_TICK (pro) >= MIN_TICK);
3087
3088 tick1 = INSN_TICK (pro) + dep_cost (dep);
3089 if (tick1 > tick)
3090 tick = tick1;
3091
3092 if (!full_p)
3093 break;
3094 }
3095 }
3096 else
3097 tick = -1;
3098
3099 INSN_TICK (next) = tick;
3100
3101 delay = tick - clock_var;
3102 if (delay <= 0)
3103 delay = QUEUE_READY;
3104
3105 change_queue_index (next, delay);
3106
3107 return delay;
3108 }
3109
3110 /* Move NEXT to the proper queue list with (DELAY >= 1),
3111 or add it to the ready list (DELAY == QUEUE_READY),
3112 or remove it from ready and queue lists at all (DELAY == QUEUE_NOWHERE). */
3113 static void
3114 change_queue_index (rtx next, int delay)
3115 {
3116 int i = QUEUE_INDEX (next);
3117
3118 gcc_assert (QUEUE_NOWHERE <= delay && delay <= max_insn_queue_index
3119 && delay != 0);
3120 gcc_assert (i != QUEUE_SCHEDULED);
3121
3122 if ((delay > 0 && NEXT_Q_AFTER (q_ptr, delay) == i)
3123 || (delay < 0 && delay == i))
3124 /* We have nothing to do. */
3125 return;
3126
3127 /* Remove NEXT from wherever it is now. */
3128 if (i == QUEUE_READY)
3129 ready_remove_insn (next);
3130 else if (i >= 0)
3131 queue_remove (next);
3132
3133 /* Add it to the proper place. */
3134 if (delay == QUEUE_READY)
3135 ready_add (readyp, next, false);
3136 else if (delay >= 1)
3137 queue_insn (next, delay);
3138
3139 if (sched_verbose >= 2)
3140 {
3141 fprintf (sched_dump, ";;\t\ttick updated: insn %s",
3142 (*current_sched_info->print_insn) (next, 0));
3143
3144 if (delay == QUEUE_READY)
3145 fprintf (sched_dump, " into ready\n");
3146 else if (delay >= 1)
3147 fprintf (sched_dump, " into queue with cost=%d\n", delay);
3148 else
3149 fprintf (sched_dump, " removed from ready or queue lists\n");
3150 }
3151 }
3152
3153 /* Extend H_I_D data. */
3154 static void
3155 extend_h_i_d (void)
3156 {
3157 /* We use LUID 0 for the fake insn (UID 0) which holds dependencies for
3158 pseudos which do not cross calls. */
3159 int new_max_uid = get_max_uid () + 1;
3160
3161 h_i_d = xrecalloc (h_i_d, new_max_uid, old_max_uid, sizeof (*h_i_d));
3162 old_max_uid = new_max_uid;
3163
3164 if (targetm.sched.h_i_d_extended)
3165 targetm.sched.h_i_d_extended ();
3166 }
3167
3168 /* Extend READY, READY_TRY and CHOICE_STACK arrays.
3169 N_NEW_INSNS is the number of additional elements to allocate. */
3170 static void
3171 extend_ready (int n_new_insns)
3172 {
3173 int i;
3174
3175 readyp->veclen = rgn_n_insns + n_new_insns + 1 + issue_rate;
3176 readyp->vec = XRESIZEVEC (rtx, readyp->vec, readyp->veclen);
3177
3178 ready_try = xrecalloc (ready_try, rgn_n_insns + n_new_insns + 1,
3179 rgn_n_insns + 1, sizeof (char));
3180
3181 rgn_n_insns += n_new_insns;
3182
3183 choice_stack = XRESIZEVEC (struct choice_entry, choice_stack,
3184 rgn_n_insns + 1);
3185
3186 for (i = rgn_n_insns; n_new_insns--; i--)
3187 choice_stack[i].state = xmalloc (dfa_state_size);
3188 }
3189
3190 /* Extend global scheduler structures (those, that live across calls to
3191 schedule_block) to include information about just emitted INSN. */
3192 static void
3193 extend_global (rtx insn)
3194 {
3195 gcc_assert (INSN_P (insn));
3196
3197 /* These structures have scheduler scope. */
3198
3199 /* Init h_i_d. */
3200 extend_h_i_d ();
3201 init_h_i_d (insn);
3202
3203 /* Init data handled in sched-deps.c. */
3204 sd_init_insn (insn);
3205
3206 /* Extend dependency caches by one element. */
3207 extend_dependency_caches (1, false);
3208 }
3209
3210 /* Extends global and local scheduler structures to include information
3211 about just emitted INSN. */
3212 static void
3213 extend_all (rtx insn)
3214 {
3215 extend_global (insn);
3216
3217 /* These structures have block scope. */
3218 extend_ready (1);
3219
3220 (*current_sched_info->add_remove_insn) (insn, 0);
3221 }
3222
3223 /* Initialize h_i_d entry of the new INSN with default values.
3224 Values, that are not explicitly initialized here, hold zero. */
3225 static void
3226 init_h_i_d (rtx insn)
3227 {
3228 INSN_LUID (insn) = luid++;
3229 INSN_COST (insn) = -1;
3230 TODO_SPEC (insn) = HARD_DEP;
3231 QUEUE_INDEX (insn) = QUEUE_NOWHERE;
3232 INSN_TICK (insn) = INVALID_TICK;
3233 INTER_TICK (insn) = INVALID_TICK;
3234 find_insn_reg_weight1 (insn);
3235 }
3236
3237 /* Generates recovery code for INSN. */
3238 static void
3239 generate_recovery_code (rtx insn)
3240 {
3241 if (TODO_SPEC (insn) & BEGIN_SPEC)
3242 begin_speculative_block (insn);
3243
3244 /* Here we have insn with no dependencies to
3245 instructions other then CHECK_SPEC ones. */
3246
3247 if (TODO_SPEC (insn) & BE_IN_SPEC)
3248 add_to_speculative_block (insn);
3249 }
3250
3251 /* Helper function.
3252 Tries to add speculative dependencies of type FS between instructions
3253 in deps_list L and TWIN. */
3254 static void
3255 process_insn_forw_deps_be_in_spec (rtx insn, rtx twin, ds_t fs)
3256 {
3257 sd_iterator_def sd_it;
3258 dep_t dep;
3259
3260 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
3261 {
3262 ds_t ds;
3263 rtx consumer;
3264
3265 consumer = DEP_CON (dep);
3266
3267 ds = DEP_STATUS (dep);
3268
3269 if (/* If we want to create speculative dep. */
3270 fs
3271 /* And we can do that because this is a true dep. */
3272 && (ds & DEP_TYPES) == DEP_TRUE)
3273 {
3274 gcc_assert (!(ds & BE_IN_SPEC));
3275
3276 if (/* If this dep can be overcome with 'begin speculation'. */
3277 ds & BEGIN_SPEC)
3278 /* Then we have a choice: keep the dep 'begin speculative'
3279 or transform it into 'be in speculative'. */
3280 {
3281 if (/* In try_ready we assert that if insn once became ready
3282 it can be removed from the ready (or queue) list only
3283 due to backend decision. Hence we can't let the
3284 probability of the speculative dep to decrease. */
3285 dep_weak (ds) <= dep_weak (fs))
3286 {
3287 ds_t new_ds;
3288
3289 new_ds = (ds & ~BEGIN_SPEC) | fs;
3290
3291 if (/* consumer can 'be in speculative'. */
3292 sched_insn_is_legitimate_for_speculation_p (consumer,
3293 new_ds))
3294 /* Transform it to be in speculative. */
3295 ds = new_ds;
3296 }
3297 }
3298 else
3299 /* Mark the dep as 'be in speculative'. */
3300 ds |= fs;
3301 }
3302
3303 {
3304 dep_def _new_dep, *new_dep = &_new_dep;
3305
3306 init_dep_1 (new_dep, twin, consumer, DEP_TYPE (dep), ds);
3307 sd_add_dep (new_dep, false);
3308 }
3309 }
3310 }
3311
3312 /* Generates recovery code for BEGIN speculative INSN. */
3313 static void
3314 begin_speculative_block (rtx insn)
3315 {
3316 if (TODO_SPEC (insn) & BEGIN_DATA)
3317 nr_begin_data++;
3318 if (TODO_SPEC (insn) & BEGIN_CONTROL)
3319 nr_begin_control++;
3320
3321 create_check_block_twin (insn, false);
3322
3323 TODO_SPEC (insn) &= ~BEGIN_SPEC;
3324 }
3325
3326 /* Generates recovery code for BE_IN speculative INSN. */
3327 static void
3328 add_to_speculative_block (rtx insn)
3329 {
3330 ds_t ts;
3331 sd_iterator_def sd_it;
3332 dep_t dep;
3333 rtx twins = NULL;
3334 rtx_vec_t priorities_roots;
3335
3336 ts = TODO_SPEC (insn);
3337 gcc_assert (!(ts & ~BE_IN_SPEC));
3338
3339 if (ts & BE_IN_DATA)
3340 nr_be_in_data++;
3341 if (ts & BE_IN_CONTROL)
3342 nr_be_in_control++;
3343
3344 TODO_SPEC (insn) &= ~BE_IN_SPEC;
3345 gcc_assert (!TODO_SPEC (insn));
3346
3347 DONE_SPEC (insn) |= ts;
3348
3349 /* First we convert all simple checks to branchy. */
3350 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
3351 sd_iterator_cond (&sd_it, &dep);)
3352 {
3353 rtx check = DEP_PRO (dep);
3354
3355 if (IS_SPECULATION_SIMPLE_CHECK_P (check))
3356 {
3357 create_check_block_twin (check, true);
3358
3359 /* Restart search. */
3360 sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
3361 }
3362 else
3363 /* Continue search. */
3364 sd_iterator_next (&sd_it);
3365 }
3366
3367 priorities_roots = NULL;
3368 clear_priorities (insn, &priorities_roots);
3369
3370 while (1)
3371 {
3372 rtx check, twin;
3373 basic_block rec;
3374
3375 /* Get the first backward dependency of INSN. */
3376 sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
3377 if (!sd_iterator_cond (&sd_it, &dep))
3378 /* INSN has no backward dependencies left. */
3379 break;
3380
3381 gcc_assert ((DEP_STATUS (dep) & BEGIN_SPEC) == 0
3382 && (DEP_STATUS (dep) & BE_IN_SPEC) != 0
3383 && (DEP_STATUS (dep) & DEP_TYPES) == DEP_TRUE);
3384
3385 check = DEP_PRO (dep);
3386
3387 gcc_assert (!IS_SPECULATION_CHECK_P (check) && !ORIG_PAT (check)
3388 && QUEUE_INDEX (check) == QUEUE_NOWHERE);
3389
3390 rec = BLOCK_FOR_INSN (check);
3391
3392 twin = emit_insn_before (copy_insn (PATTERN (insn)), BB_END (rec));
3393 extend_global (twin);
3394
3395 sd_copy_back_deps (twin, insn, true);
3396
3397 if (sched_verbose && spec_info->dump)
3398 /* INSN_BB (insn) isn't determined for twin insns yet.
3399 So we can't use current_sched_info->print_insn. */
3400 fprintf (spec_info->dump, ";;\t\tGenerated twin insn : %d/rec%d\n",
3401 INSN_UID (twin), rec->index);
3402
3403 twins = alloc_INSN_LIST (twin, twins);
3404
3405 /* Add dependences between TWIN and all appropriate
3406 instructions from REC. */
3407 FOR_EACH_DEP (insn, SD_LIST_SPEC_BACK, sd_it, dep)
3408 {
3409 rtx pro = DEP_PRO (dep);
3410
3411 gcc_assert (DEP_TYPE (dep) == REG_DEP_TRUE);
3412
3413 /* INSN might have dependencies from the instructions from
3414 several recovery blocks. At this iteration we process those
3415 producers that reside in REC. */
3416 if (BLOCK_FOR_INSN (pro) == rec)
3417 {
3418 dep_def _new_dep, *new_dep = &_new_dep;
3419
3420 init_dep (new_dep, pro, twin, REG_DEP_TRUE);
3421 sd_add_dep (new_dep, false);
3422 }
3423 }
3424
3425 process_insn_forw_deps_be_in_spec (insn, twin, ts);
3426
3427 /* Remove all dependencies between INSN and insns in REC. */
3428 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
3429 sd_iterator_cond (&sd_it, &dep);)
3430 {
3431 rtx pro = DEP_PRO (dep);
3432
3433 if (BLOCK_FOR_INSN (pro) == rec)
3434 sd_delete_dep (sd_it);
3435 else
3436 sd_iterator_next (&sd_it);
3437 }
3438 }
3439
3440 /* We couldn't have added the dependencies between INSN and TWINS earlier
3441 because that would make TWINS appear in the INSN_BACK_DEPS (INSN). */
3442 while (twins)
3443 {
3444 rtx twin;
3445
3446 twin = XEXP (twins, 0);
3447
3448 {
3449 dep_def _new_dep, *new_dep = &_new_dep;
3450
3451 init_dep (new_dep, insn, twin, REG_DEP_OUTPUT);
3452 sd_add_dep (new_dep, false);
3453 }
3454
3455 twin = XEXP (twins, 1);
3456 free_INSN_LIST_node (twins);
3457 twins = twin;
3458 }
3459
3460 calc_priorities (priorities_roots);
3461 VEC_free (rtx, heap, priorities_roots);
3462 }
3463
3464 /* Extends and fills with zeros (only the new part) array pointed to by P. */
3465 void *
3466 xrecalloc (void *p, size_t new_nmemb, size_t old_nmemb, size_t size)
3467 {
3468 gcc_assert (new_nmemb >= old_nmemb);
3469 p = XRESIZEVAR (void, p, new_nmemb * size);
3470 memset (((char *) p) + old_nmemb * size, 0, (new_nmemb - old_nmemb) * size);
3471 return p;
3472 }
3473
3474 /* Return the probability of speculation success for the speculation
3475 status DS. */
3476 static dw_t
3477 dep_weak (ds_t ds)
3478 {
3479 ds_t res = 1, dt;
3480 int n = 0;
3481
3482 dt = FIRST_SPEC_TYPE;
3483 do
3484 {
3485 if (ds & dt)
3486 {
3487 res *= (ds_t) get_dep_weak (ds, dt);
3488 n++;
3489 }
3490
3491 if (dt == LAST_SPEC_TYPE)
3492 break;
3493 dt <<= SPEC_TYPE_SHIFT;
3494 }
3495 while (1);
3496
3497 gcc_assert (n);
3498 while (--n)
3499 res /= MAX_DEP_WEAK;
3500
3501 if (res < MIN_DEP_WEAK)
3502 res = MIN_DEP_WEAK;
3503
3504 gcc_assert (res <= MAX_DEP_WEAK);
3505
3506 return (dw_t) res;
3507 }
3508
3509 /* Helper function.
3510 Find fallthru edge from PRED. */
3511 static edge
3512 find_fallthru_edge (basic_block pred)
3513 {
3514 edge e;
3515 edge_iterator ei;
3516 basic_block succ;
3517
3518 succ = pred->next_bb;
3519 gcc_assert (succ->prev_bb == pred);
3520
3521 if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
3522 {
3523 FOR_EACH_EDGE (e, ei, pred->succs)
3524 if (e->flags & EDGE_FALLTHRU)
3525 {
3526 gcc_assert (e->dest == succ);
3527 return e;
3528 }
3529 }
3530 else
3531 {
3532 FOR_EACH_EDGE (e, ei, succ->preds)
3533 if (e->flags & EDGE_FALLTHRU)
3534 {
3535 gcc_assert (e->src == pred);
3536 return e;
3537 }
3538 }
3539
3540 return NULL;
3541 }
3542
3543 /* Initialize BEFORE_RECOVERY variable. */
3544 static void
3545 init_before_recovery (void)
3546 {
3547 basic_block last;
3548 edge e;
3549
3550 last = EXIT_BLOCK_PTR->prev_bb;
3551 e = find_fallthru_edge (last);
3552
3553 if (e)
3554 {
3555 /* We create two basic blocks:
3556 1. Single instruction block is inserted right after E->SRC
3557 and has jump to
3558 2. Empty block right before EXIT_BLOCK.
3559 Between these two blocks recovery blocks will be emitted. */
3560
3561 basic_block single, empty;
3562 rtx x, label;
3563
3564 single = create_empty_bb (last);
3565 empty = create_empty_bb (single);
3566
3567 single->count = last->count;
3568 empty->count = last->count;
3569 single->frequency = last->frequency;
3570 empty->frequency = last->frequency;
3571 BB_COPY_PARTITION (single, last);
3572 BB_COPY_PARTITION (empty, last);
3573
3574 redirect_edge_succ (e, single);
3575 make_single_succ_edge (single, empty, 0);
3576 make_single_succ_edge (empty, EXIT_BLOCK_PTR,
3577 EDGE_FALLTHRU | EDGE_CAN_FALLTHRU);
3578
3579 label = block_label (empty);
3580 x = emit_jump_insn_after (gen_jump (label), BB_END (single));
3581 JUMP_LABEL (x) = label;
3582 LABEL_NUSES (label)++;
3583 extend_global (x);
3584
3585 emit_barrier_after (x);
3586
3587 add_block (empty, 0);
3588 add_block (single, 0);
3589
3590 before_recovery = single;
3591
3592 if (sched_verbose >= 2 && spec_info->dump)
3593 fprintf (spec_info->dump,
3594 ";;\t\tFixed fallthru to EXIT : %d->>%d->%d->>EXIT\n",
3595 last->index, single->index, empty->index);
3596 }
3597 else
3598 before_recovery = last;
3599 }
3600
3601 /* Returns new recovery block. */
3602 static basic_block
3603 create_recovery_block (void)
3604 {
3605 rtx label;
3606 rtx barrier;
3607 basic_block rec;
3608
3609 haifa_recovery_bb_recently_added_p = true;
3610 haifa_recovery_bb_ever_added_p = true;
3611
3612 if (!before_recovery)
3613 init_before_recovery ();
3614
3615 barrier = get_last_bb_insn (before_recovery);
3616 gcc_assert (BARRIER_P (barrier));
3617
3618 label = emit_label_after (gen_label_rtx (), barrier);
3619
3620 rec = create_basic_block (label, label, before_recovery);
3621
3622 /* Recovery block always end with an unconditional jump. */
3623 emit_barrier_after (BB_END (rec));
3624
3625 if (BB_PARTITION (before_recovery) != BB_UNPARTITIONED)
3626 BB_SET_PARTITION (rec, BB_COLD_PARTITION);
3627
3628 if (sched_verbose && spec_info->dump)
3629 fprintf (spec_info->dump, ";;\t\tGenerated recovery block rec%d\n",
3630 rec->index);
3631
3632 before_recovery = rec;
3633
3634 return rec;
3635 }
3636
3637 /* This function creates recovery code for INSN. If MUTATE_P is nonzero,
3638 INSN is a simple check, that should be converted to branchy one. */
3639 static void
3640 create_check_block_twin (rtx insn, bool mutate_p)
3641 {
3642 basic_block rec;
3643 rtx label, check, twin;
3644 ds_t fs;
3645 sd_iterator_def sd_it;
3646 dep_t dep;
3647 dep_def _new_dep, *new_dep = &_new_dep;
3648
3649 gcc_assert (ORIG_PAT (insn)
3650 && (!mutate_p
3651 || (IS_SPECULATION_SIMPLE_CHECK_P (insn)
3652 && !(TODO_SPEC (insn) & SPECULATIVE))));
3653
3654 /* Create recovery block. */
3655 if (mutate_p || targetm.sched.needs_block_p (insn))
3656 {
3657 rec = create_recovery_block ();
3658 label = BB_HEAD (rec);
3659 }
3660 else
3661 {
3662 rec = EXIT_BLOCK_PTR;
3663 label = 0;
3664 }
3665
3666 /* Emit CHECK. */
3667 check = targetm.sched.gen_check (insn, label, mutate_p);
3668
3669 if (rec != EXIT_BLOCK_PTR)
3670 {
3671 /* To have mem_reg alive at the beginning of second_bb,
3672 we emit check BEFORE insn, so insn after splitting
3673 insn will be at the beginning of second_bb, which will
3674 provide us with the correct life information. */
3675 check = emit_jump_insn_before (check, insn);
3676 JUMP_LABEL (check) = label;
3677 LABEL_NUSES (label)++;
3678 }
3679 else
3680 check = emit_insn_before (check, insn);
3681
3682 /* Extend data structures. */
3683 extend_all (check);
3684 RECOVERY_BLOCK (check) = rec;
3685
3686 if (sched_verbose && spec_info->dump)
3687 fprintf (spec_info->dump, ";;\t\tGenerated check insn : %s\n",
3688 (*current_sched_info->print_insn) (check, 0));
3689
3690 gcc_assert (ORIG_PAT (insn));
3691
3692 /* Initialize TWIN (twin is a duplicate of original instruction
3693 in the recovery block). */
3694 if (rec != EXIT_BLOCK_PTR)
3695 {
3696 sd_iterator_def sd_it;
3697 dep_t dep;
3698
3699 FOR_EACH_DEP (insn, SD_LIST_RES_BACK, sd_it, dep)
3700 if ((DEP_STATUS (dep) & DEP_OUTPUT) != 0)
3701 {
3702 struct _dep _dep2, *dep2 = &_dep2;
3703
3704 init_dep (dep2, DEP_PRO (dep), check, REG_DEP_TRUE);
3705
3706 sd_add_dep (dep2, true);
3707 }
3708
3709 twin = emit_insn_after (ORIG_PAT (insn), BB_END (rec));
3710 extend_global (twin);
3711
3712 if (sched_verbose && spec_info->dump)
3713 /* INSN_BB (insn) isn't determined for twin insns yet.
3714 So we can't use current_sched_info->print_insn. */
3715 fprintf (spec_info->dump, ";;\t\tGenerated twin insn : %d/rec%d\n",
3716 INSN_UID (twin), rec->index);
3717 }
3718 else
3719 {
3720 ORIG_PAT (check) = ORIG_PAT (insn);
3721 HAS_INTERNAL_DEP (check) = 1;
3722 twin = check;
3723 /* ??? We probably should change all OUTPUT dependencies to
3724 (TRUE | OUTPUT). */
3725 }
3726
3727 /* Copy all resolved back dependencies of INSN to TWIN. This will
3728 provide correct value for INSN_TICK (TWIN). */
3729 sd_copy_back_deps (twin, insn, true);
3730
3731 if (rec != EXIT_BLOCK_PTR)
3732 /* In case of branchy check, fix CFG. */
3733 {
3734 basic_block first_bb, second_bb;
3735 rtx jump;
3736 edge e;
3737 int edge_flags;
3738
3739 first_bb = BLOCK_FOR_INSN (check);
3740 e = split_block (first_bb, check);
3741 /* split_block emits note if *check == BB_END. Probably it
3742 is better to rip that note off. */
3743 gcc_assert (e->src == first_bb);
3744 second_bb = e->dest;
3745
3746 /* This is fixing of incoming edge. */
3747 /* ??? Which other flags should be specified? */
3748 if (BB_PARTITION (first_bb) != BB_PARTITION (rec))
3749 /* Partition type is the same, if it is "unpartitioned". */
3750 edge_flags = EDGE_CROSSING;
3751 else
3752 edge_flags = 0;
3753
3754 e = make_edge (first_bb, rec, edge_flags);
3755
3756 add_block (second_bb, first_bb);
3757
3758 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (BB_HEAD (second_bb)));
3759 label = block_label (second_bb);
3760 jump = emit_jump_insn_after (gen_jump (label), BB_END (rec));
3761 JUMP_LABEL (jump) = label;
3762 LABEL_NUSES (label)++;
3763 extend_global (jump);
3764
3765 if (BB_PARTITION (second_bb) != BB_PARTITION (rec))
3766 /* Partition type is the same, if it is "unpartitioned". */
3767 {
3768 /* Rewritten from cfgrtl.c. */
3769 if (flag_reorder_blocks_and_partition
3770 && targetm.have_named_sections
3771 /*&& !any_condjump_p (jump)*/)
3772 /* any_condjump_p (jump) == false.
3773 We don't need the same note for the check because
3774 any_condjump_p (check) == true. */
3775 {
3776 REG_NOTES (jump) = gen_rtx_EXPR_LIST (REG_CROSSING_JUMP,
3777 NULL_RTX,
3778 REG_NOTES (jump));
3779 }
3780 edge_flags = EDGE_CROSSING;
3781 }
3782 else
3783 edge_flags = 0;
3784
3785 make_single_succ_edge (rec, second_bb, edge_flags);
3786
3787 add_block (rec, EXIT_BLOCK_PTR);
3788 }
3789
3790 /* Move backward dependences from INSN to CHECK and
3791 move forward dependences from INSN to TWIN. */
3792
3793 /* First, create dependencies between INSN's producers and CHECK & TWIN. */
3794 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
3795 {
3796 rtx pro = DEP_PRO (dep);
3797 ds_t ds;
3798
3799 /* If BEGIN_DATA: [insn ~~TRUE~~> producer]:
3800 check --TRUE--> producer ??? or ANTI ???
3801 twin --TRUE--> producer
3802 twin --ANTI--> check
3803
3804 If BEGIN_CONTROL: [insn ~~ANTI~~> producer]:
3805 check --ANTI--> producer
3806 twin --ANTI--> producer
3807 twin --ANTI--> check
3808
3809 If BE_IN_SPEC: [insn ~~TRUE~~> producer]:
3810 check ~~TRUE~~> producer
3811 twin ~~TRUE~~> producer
3812 twin --ANTI--> check */
3813
3814 ds = DEP_STATUS (dep);
3815
3816 if (ds & BEGIN_SPEC)
3817 {
3818 gcc_assert (!mutate_p);
3819 ds &= ~BEGIN_SPEC;
3820 }
3821
3822 init_dep_1 (new_dep, pro, check, DEP_TYPE (dep), ds);
3823 sd_add_dep (new_dep, false);
3824
3825 if (rec != EXIT_BLOCK_PTR)
3826 {
3827 DEP_CON (new_dep) = twin;
3828 sd_add_dep (new_dep, false);
3829 }
3830 }
3831
3832 /* Second, remove backward dependencies of INSN. */
3833 for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
3834 sd_iterator_cond (&sd_it, &dep);)
3835 {
3836 if ((DEP_STATUS (dep) & BEGIN_SPEC)
3837 || mutate_p)
3838 /* We can delete this dep because we overcome it with
3839 BEGIN_SPECULATION. */
3840 sd_delete_dep (sd_it);
3841 else
3842 sd_iterator_next (&sd_it);
3843 }
3844
3845 /* Future Speculations. Determine what BE_IN speculations will be like. */
3846 fs = 0;
3847
3848 /* Fields (DONE_SPEC (x) & BEGIN_SPEC) and CHECK_SPEC (x) are set only
3849 here. */
3850
3851 gcc_assert (!DONE_SPEC (insn));
3852
3853 if (!mutate_p)
3854 {
3855 ds_t ts = TODO_SPEC (insn);
3856
3857 DONE_SPEC (insn) = ts & BEGIN_SPEC;
3858 CHECK_SPEC (check) = ts & BEGIN_SPEC;
3859
3860 /* Luckiness of future speculations solely depends upon initial
3861 BEGIN speculation. */
3862 if (ts & BEGIN_DATA)
3863 fs = set_dep_weak (fs, BE_IN_DATA, get_dep_weak (ts, BEGIN_DATA));
3864 if (ts & BEGIN_CONTROL)
3865 fs = set_dep_weak (fs, BE_IN_CONTROL,
3866 get_dep_weak (ts, BEGIN_CONTROL));
3867 }
3868 else
3869 CHECK_SPEC (check) = CHECK_SPEC (insn);
3870
3871 /* Future speculations: call the helper. */
3872 process_insn_forw_deps_be_in_spec (insn, twin, fs);
3873
3874 if (rec != EXIT_BLOCK_PTR)
3875 {
3876 /* Which types of dependencies should we use here is,
3877 generally, machine-dependent question... But, for now,
3878 it is not. */
3879
3880 if (!mutate_p)
3881 {
3882 init_dep (new_dep, insn, check, REG_DEP_TRUE);
3883 sd_add_dep (new_dep, false);
3884
3885 init_dep (new_dep, insn, twin, REG_DEP_OUTPUT);
3886 sd_add_dep (new_dep, false);
3887 }
3888 else
3889 {
3890 if (spec_info->dump)
3891 fprintf (spec_info->dump, ";;\t\tRemoved simple check : %s\n",
3892 (*current_sched_info->print_insn) (insn, 0));
3893
3894 /* Remove all dependencies of the INSN. */
3895 {
3896 sd_it = sd_iterator_start (insn, (SD_LIST_FORW
3897 | SD_LIST_BACK
3898 | SD_LIST_RES_BACK));
3899 while (sd_iterator_cond (&sd_it, &dep))
3900 sd_delete_dep (sd_it);
3901 }
3902
3903 /* If former check (INSN) already was moved to the ready (or queue)
3904 list, add new check (CHECK) there too. */
3905 if (QUEUE_INDEX (insn) != QUEUE_NOWHERE)
3906 try_ready (check);
3907
3908 /* Remove old check from instruction stream and free its
3909 data. */
3910 sched_remove_insn (insn);
3911 }
3912
3913 init_dep (new_dep, check, twin, REG_DEP_ANTI);
3914 sd_add_dep (new_dep, false);
3915 }
3916 else
3917 {
3918 init_dep_1 (new_dep, insn, check, REG_DEP_TRUE, DEP_TRUE | DEP_OUTPUT);
3919 sd_add_dep (new_dep, false);
3920 }
3921
3922 if (!mutate_p)
3923 /* Fix priorities. If MUTATE_P is nonzero, this is not necessary,
3924 because it'll be done later in add_to_speculative_block. */
3925 {
3926 rtx_vec_t priorities_roots = NULL;
3927
3928 clear_priorities (twin, &priorities_roots);
3929 calc_priorities (priorities_roots);
3930 VEC_free (rtx, heap, priorities_roots);
3931 }
3932 }
3933
3934 /* Removes dependency between instructions in the recovery block REC
3935 and usual region instructions. It keeps inner dependences so it
3936 won't be necessary to recompute them. */
3937 static void
3938 fix_recovery_deps (basic_block rec)
3939 {
3940 rtx note, insn, jump, ready_list = 0;
3941 bitmap_head in_ready;
3942 rtx link;
3943
3944 bitmap_initialize (&in_ready, 0);
3945
3946 /* NOTE - a basic block note. */
3947 note = NEXT_INSN (BB_HEAD (rec));
3948 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
3949 insn = BB_END (rec);
3950 gcc_assert (JUMP_P (insn));
3951 insn = PREV_INSN (insn);
3952
3953 do
3954 {
3955 sd_iterator_def sd_it;
3956 dep_t dep;
3957
3958 for (sd_it = sd_iterator_start (insn, SD_LIST_FORW);
3959 sd_iterator_cond (&sd_it, &dep);)
3960 {
3961 rtx consumer = DEP_CON (dep);
3962
3963 if (BLOCK_FOR_INSN (consumer) != rec)
3964 {
3965 sd_delete_dep (sd_it);
3966
3967 if (!bitmap_bit_p (&in_ready, INSN_LUID (consumer)))
3968 {
3969 ready_list = alloc_INSN_LIST (consumer, ready_list);
3970 bitmap_set_bit (&in_ready, INSN_LUID (consumer));
3971 }
3972 }
3973 else
3974 {
3975 gcc_assert ((DEP_STATUS (dep) & DEP_TYPES) == DEP_TRUE);
3976
3977 sd_iterator_next (&sd_it);
3978 }
3979 }
3980
3981 insn = PREV_INSN (insn);
3982 }
3983 while (insn != note);
3984
3985 bitmap_clear (&in_ready);
3986
3987 /* Try to add instructions to the ready or queue list. */
3988 for (link = ready_list; link; link = XEXP (link, 1))
3989 try_ready (XEXP (link, 0));
3990 free_INSN_LIST_list (&ready_list);
3991
3992 /* Fixing jump's dependences. */
3993 insn = BB_HEAD (rec);
3994 jump = BB_END (rec);
3995
3996 gcc_assert (LABEL_P (insn));
3997 insn = NEXT_INSN (insn);
3998
3999 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
4000 add_jump_dependencies (insn, jump);
4001 }
4002
4003 /* Changes pattern of the INSN to NEW_PAT. */
4004 static void
4005 change_pattern (rtx insn, rtx new_pat)
4006 {
4007 int t;
4008
4009 t = validate_change (insn, &PATTERN (insn), new_pat, 0);
4010 gcc_assert (t);
4011 /* Invalidate INSN_COST, so it'll be recalculated. */
4012 INSN_COST (insn) = -1;
4013 /* Invalidate INSN_TICK, so it'll be recalculated. */
4014 INSN_TICK (insn) = INVALID_TICK;
4015 dfa_clear_single_insn_cache (insn);
4016 }
4017
4018 /* Return true if INSN can potentially be speculated with type DS. */
4019 bool
4020 sched_insn_is_legitimate_for_speculation_p (const_rtx insn, ds_t ds)
4021 {
4022 if (HAS_INTERNAL_DEP (insn))
4023 return false;
4024
4025 if (!NONJUMP_INSN_P (insn))
4026 return false;
4027
4028 if (SCHED_GROUP_P (insn))
4029 return false;
4030
4031 if (IS_SPECULATION_CHECK_P (insn))
4032 return false;
4033
4034 if (side_effects_p (PATTERN (insn)))
4035 return false;
4036
4037 if ((ds & BE_IN_SPEC)
4038 && may_trap_p (PATTERN (insn)))
4039 return false;
4040
4041 return true;
4042 }
4043
4044 /* -1 - can't speculate,
4045 0 - for speculation with REQUEST mode it is OK to use
4046 current instruction pattern,
4047 1 - need to change pattern for *NEW_PAT to be speculative. */
4048 static int
4049 speculate_insn (rtx insn, ds_t request, rtx *new_pat)
4050 {
4051 gcc_assert (current_sched_info->flags & DO_SPECULATION
4052 && (request & SPECULATIVE)
4053 && sched_insn_is_legitimate_for_speculation_p (insn, request));
4054
4055 if ((request & spec_info->mask) != request)
4056 return -1;
4057
4058 if (request & BE_IN_SPEC
4059 && !(request & BEGIN_SPEC))
4060 return 0;
4061
4062 return targetm.sched.speculate_insn (insn, request & BEGIN_SPEC, new_pat);
4063 }
4064
4065 /* Print some information about block BB, which starts with HEAD and
4066 ends with TAIL, before scheduling it.
4067 I is zero, if scheduler is about to start with the fresh ebb. */
4068 static void
4069 dump_new_block_header (int i, basic_block bb, rtx head, rtx tail)
4070 {
4071 if (!i)
4072 fprintf (sched_dump,
4073 ";; ======================================================\n");
4074 else
4075 fprintf (sched_dump,
4076 ";; =====================ADVANCING TO=====================\n");
4077 fprintf (sched_dump,
4078 ";; -- basic block %d from %d to %d -- %s reload\n",
4079 bb->index, INSN_UID (head), INSN_UID (tail),
4080 (reload_completed ? "after" : "before"));
4081 fprintf (sched_dump,
4082 ";; ======================================================\n");
4083 fprintf (sched_dump, "\n");
4084 }
4085
4086 /* Unlink basic block notes and labels and saves them, so they
4087 can be easily restored. We unlink basic block notes in EBB to
4088 provide back-compatibility with the previous code, as target backends
4089 assume, that there'll be only instructions between
4090 current_sched_info->{head and tail}. We restore these notes as soon
4091 as we can.
4092 FIRST (LAST) is the first (last) basic block in the ebb.
4093 NB: In usual case (FIRST == LAST) nothing is really done. */
4094 void
4095 unlink_bb_notes (basic_block first, basic_block last)
4096 {
4097 /* We DON'T unlink basic block notes of the first block in the ebb. */
4098 if (first == last)
4099 return;
4100
4101 bb_header = xmalloc (last_basic_block * sizeof (*bb_header));
4102
4103 /* Make a sentinel. */
4104 if (last->next_bb != EXIT_BLOCK_PTR)
4105 bb_header[last->next_bb->index] = 0;
4106
4107 first = first->next_bb;
4108 do
4109 {
4110 rtx prev, label, note, next;
4111
4112 label = BB_HEAD (last);
4113 if (LABEL_P (label))
4114 note = NEXT_INSN (label);
4115 else
4116 note = label;
4117 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4118
4119 prev = PREV_INSN (label);
4120 next = NEXT_INSN (note);
4121 gcc_assert (prev && next);
4122
4123 NEXT_INSN (prev) = next;
4124 PREV_INSN (next) = prev;
4125
4126 bb_header[last->index] = label;
4127
4128 if (last == first)
4129 break;
4130
4131 last = last->prev_bb;
4132 }
4133 while (1);
4134 }
4135
4136 /* Restore basic block notes.
4137 FIRST is the first basic block in the ebb. */
4138 static void
4139 restore_bb_notes (basic_block first)
4140 {
4141 if (!bb_header)
4142 return;
4143
4144 /* We DON'T unlink basic block notes of the first block in the ebb. */
4145 first = first->next_bb;
4146 /* Remember: FIRST is actually a second basic block in the ebb. */
4147
4148 while (first != EXIT_BLOCK_PTR
4149 && bb_header[first->index])
4150 {
4151 rtx prev, label, note, next;
4152
4153 label = bb_header[first->index];
4154 prev = PREV_INSN (label);
4155 next = NEXT_INSN (prev);
4156
4157 if (LABEL_P (label))
4158 note = NEXT_INSN (label);
4159 else
4160 note = label;
4161 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4162
4163 bb_header[first->index] = 0;
4164
4165 NEXT_INSN (prev) = label;
4166 NEXT_INSN (note) = next;
4167 PREV_INSN (next) = note;
4168
4169 first = first->next_bb;
4170 }
4171
4172 free (bb_header);
4173 bb_header = 0;
4174 }
4175
4176 /* Extend per basic block data structures of the scheduler.
4177 If BB is NULL, initialize structures for the whole CFG.
4178 Otherwise, initialize them for the just created BB. */
4179 static void
4180 extend_bb (void)
4181 {
4182 rtx insn;
4183
4184 old_last_basic_block = last_basic_block;
4185
4186 /* The following is done to keep current_sched_info->next_tail non null. */
4187
4188 insn = BB_END (EXIT_BLOCK_PTR->prev_bb);
4189 if (NEXT_INSN (insn) == 0
4190 || (!NOTE_P (insn)
4191 && !LABEL_P (insn)
4192 /* Don't emit a NOTE if it would end up before a BARRIER. */
4193 && !BARRIER_P (NEXT_INSN (insn))))
4194 {
4195 rtx note = emit_note_after (NOTE_INSN_DELETED, insn);
4196 /* Make insn appear outside BB. */
4197 set_block_for_insn (note, NULL);
4198 BB_END (EXIT_BLOCK_PTR->prev_bb) = insn;
4199 }
4200 }
4201
4202 /* Add a basic block BB to extended basic block EBB.
4203 If EBB is EXIT_BLOCK_PTR, then BB is recovery block.
4204 If EBB is NULL, then BB should be a new region. */
4205 void
4206 add_block (basic_block bb, basic_block ebb)
4207 {
4208 gcc_assert (current_sched_info->flags & NEW_BBS);
4209
4210 extend_bb ();
4211
4212 if (current_sched_info->add_block)
4213 /* This changes only data structures of the front-end. */
4214 current_sched_info->add_block (bb, ebb);
4215 }
4216
4217 /* Helper function.
4218 Fix CFG after both in- and inter-block movement of
4219 control_flow_insn_p JUMP. */
4220 static void
4221 fix_jump_move (rtx jump)
4222 {
4223 basic_block bb, jump_bb, jump_bb_next;
4224
4225 bb = BLOCK_FOR_INSN (PREV_INSN (jump));
4226 jump_bb = BLOCK_FOR_INSN (jump);
4227 jump_bb_next = jump_bb->next_bb;
4228
4229 gcc_assert (current_sched_info->flags & SCHED_EBB
4230 || IS_SPECULATION_BRANCHY_CHECK_P (jump));
4231
4232 if (!NOTE_INSN_BASIC_BLOCK_P (BB_END (jump_bb_next)))
4233 /* if jump_bb_next is not empty. */
4234 BB_END (jump_bb) = BB_END (jump_bb_next);
4235
4236 if (BB_END (bb) != PREV_INSN (jump))
4237 /* Then there are instruction after jump that should be placed
4238 to jump_bb_next. */
4239 BB_END (jump_bb_next) = BB_END (bb);
4240 else
4241 /* Otherwise jump_bb_next is empty. */
4242 BB_END (jump_bb_next) = NEXT_INSN (BB_HEAD (jump_bb_next));
4243
4244 /* To make assertion in move_insn happy. */
4245 BB_END (bb) = PREV_INSN (jump);
4246
4247 update_bb_for_insn (jump_bb_next);
4248 }
4249
4250 /* Fix CFG after interblock movement of control_flow_insn_p JUMP. */
4251 static void
4252 move_block_after_check (rtx jump)
4253 {
4254 basic_block bb, jump_bb, jump_bb_next;
4255 VEC(edge,gc) *t;
4256
4257 bb = BLOCK_FOR_INSN (PREV_INSN (jump));
4258 jump_bb = BLOCK_FOR_INSN (jump);
4259 jump_bb_next = jump_bb->next_bb;
4260
4261 update_bb_for_insn (jump_bb);
4262
4263 gcc_assert (IS_SPECULATION_CHECK_P (jump)
4264 || IS_SPECULATION_CHECK_P (BB_END (jump_bb_next)));
4265
4266 unlink_block (jump_bb_next);
4267 link_block (jump_bb_next, bb);
4268
4269 t = bb->succs;
4270 bb->succs = 0;
4271 move_succs (&(jump_bb->succs), bb);
4272 move_succs (&(jump_bb_next->succs), jump_bb);
4273 move_succs (&t, jump_bb_next);
4274
4275 df_mark_solutions_dirty ();
4276
4277 if (current_sched_info->fix_recovery_cfg)
4278 current_sched_info->fix_recovery_cfg
4279 (bb->index, jump_bb->index, jump_bb_next->index);
4280 }
4281
4282 /* Helper function for move_block_after_check.
4283 This functions attaches edge vector pointed to by SUCCSP to
4284 block TO. */
4285 static void
4286 move_succs (VEC(edge,gc) **succsp, basic_block to)
4287 {
4288 edge e;
4289 edge_iterator ei;
4290
4291 gcc_assert (to->succs == 0);
4292
4293 to->succs = *succsp;
4294
4295 FOR_EACH_EDGE (e, ei, to->succs)
4296 e->src = to;
4297
4298 *succsp = 0;
4299 }
4300
4301 /* Remove INSN from the instruction stream.
4302 INSN should have any dependencies. */
4303 static void
4304 sched_remove_insn (rtx insn)
4305 {
4306 sd_finish_insn (insn);
4307
4308 change_queue_index (insn, QUEUE_NOWHERE);
4309 current_sched_info->add_remove_insn (insn, 1);
4310 remove_insn (insn);
4311 }
4312
4313 /* Clear priorities of all instructions, that are forward dependent on INSN.
4314 Store in vector pointed to by ROOTS_PTR insns on which priority () should
4315 be invoked to initialize all cleared priorities. */
4316 static void
4317 clear_priorities (rtx insn, rtx_vec_t *roots_ptr)
4318 {
4319 sd_iterator_def sd_it;
4320 dep_t dep;
4321 bool insn_is_root_p = true;
4322
4323 gcc_assert (QUEUE_INDEX (insn) != QUEUE_SCHEDULED);
4324
4325 FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
4326 {
4327 rtx pro = DEP_PRO (dep);
4328
4329 if (INSN_PRIORITY_STATUS (pro) >= 0
4330 && QUEUE_INDEX (insn) != QUEUE_SCHEDULED)
4331 {
4332 /* If DEP doesn't contribute to priority then INSN itself should
4333 be added to priority roots. */
4334 if (contributes_to_priority_p (dep))
4335 insn_is_root_p = false;
4336
4337 INSN_PRIORITY_STATUS (pro) = -1;
4338 clear_priorities (pro, roots_ptr);
4339 }
4340 }
4341
4342 if (insn_is_root_p)
4343 VEC_safe_push (rtx, heap, *roots_ptr, insn);
4344 }
4345
4346 /* Recompute priorities of instructions, whose priorities might have been
4347 changed. ROOTS is a vector of instructions whose priority computation will
4348 trigger initialization of all cleared priorities. */
4349 static void
4350 calc_priorities (rtx_vec_t roots)
4351 {
4352 int i;
4353 rtx insn;
4354
4355 for (i = 0; VEC_iterate (rtx, roots, i, insn); i++)
4356 priority (insn);
4357 }
4358
4359
4360 /* Add dependences between JUMP and other instructions in the recovery
4361 block. INSN is the first insn the recovery block. */
4362 static void
4363 add_jump_dependencies (rtx insn, rtx jump)
4364 {
4365 do
4366 {
4367 insn = NEXT_INSN (insn);
4368 if (insn == jump)
4369 break;
4370
4371 if (sd_lists_empty_p (insn, SD_LIST_FORW))
4372 {
4373 dep_def _new_dep, *new_dep = &_new_dep;
4374
4375 init_dep (new_dep, insn, jump, REG_DEP_ANTI);
4376 sd_add_dep (new_dep, false);
4377 }
4378 }
4379 while (1);
4380
4381 gcc_assert (!sd_lists_empty_p (jump, SD_LIST_BACK));
4382 }
4383
4384 /* Return the NOTE_INSN_BASIC_BLOCK of BB. */
4385 rtx
4386 bb_note (basic_block bb)
4387 {
4388 rtx note;
4389
4390 note = BB_HEAD (bb);
4391 if (LABEL_P (note))
4392 note = NEXT_INSN (note);
4393
4394 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
4395 return note;
4396 }
4397
4398 #ifdef ENABLE_CHECKING
4399 /* Helper function for check_cfg.
4400 Return nonzero, if edge vector pointed to by EL has edge with TYPE in
4401 its flags. */
4402 static int
4403 has_edge_p (VEC(edge,gc) *el, int type)
4404 {
4405 edge e;
4406 edge_iterator ei;
4407
4408 FOR_EACH_EDGE (e, ei, el)
4409 if (e->flags & type)
4410 return 1;
4411 return 0;
4412 }
4413
4414 /* Check few properties of CFG between HEAD and TAIL.
4415 If HEAD (TAIL) is NULL check from the beginning (till the end) of the
4416 instruction stream. */
4417 static void
4418 check_cfg (rtx head, rtx tail)
4419 {
4420 rtx next_tail;
4421 basic_block bb = 0;
4422 int not_first = 0, not_last;
4423
4424 if (head == NULL)
4425 head = get_insns ();
4426 if (tail == NULL)
4427 tail = get_last_insn ();
4428 next_tail = NEXT_INSN (tail);
4429
4430 do
4431 {
4432 not_last = head != tail;
4433
4434 if (not_first)
4435 gcc_assert (NEXT_INSN (PREV_INSN (head)) == head);
4436 if (not_last)
4437 gcc_assert (PREV_INSN (NEXT_INSN (head)) == head);
4438
4439 if (LABEL_P (head)
4440 || (NOTE_INSN_BASIC_BLOCK_P (head)
4441 && (!not_first
4442 || (not_first && !LABEL_P (PREV_INSN (head))))))
4443 {
4444 gcc_assert (bb == 0);
4445 bb = BLOCK_FOR_INSN (head);
4446 if (bb != 0)
4447 gcc_assert (BB_HEAD (bb) == head);
4448 else
4449 /* This is the case of jump table. See inside_basic_block_p (). */
4450 gcc_assert (LABEL_P (head) && !inside_basic_block_p (head));
4451 }
4452
4453 if (bb == 0)
4454 {
4455 gcc_assert (!inside_basic_block_p (head));
4456 head = NEXT_INSN (head);
4457 }
4458 else
4459 {
4460 gcc_assert (inside_basic_block_p (head)
4461 || NOTE_P (head));
4462 gcc_assert (BLOCK_FOR_INSN (head) == bb);
4463
4464 if (LABEL_P (head))
4465 {
4466 head = NEXT_INSN (head);
4467 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (head));
4468 }
4469 else
4470 {
4471 if (control_flow_insn_p (head))
4472 {
4473 gcc_assert (BB_END (bb) == head);
4474
4475 if (any_uncondjump_p (head))
4476 gcc_assert (EDGE_COUNT (bb->succs) == 1
4477 && BARRIER_P (NEXT_INSN (head)));
4478 else if (any_condjump_p (head))
4479 gcc_assert (/* Usual case. */
4480 (EDGE_COUNT (bb->succs) > 1
4481 && !BARRIER_P (NEXT_INSN (head)))
4482 /* Or jump to the next instruction. */
4483 || (EDGE_COUNT (bb->succs) == 1
4484 && (BB_HEAD (EDGE_I (bb->succs, 0)->dest)
4485 == JUMP_LABEL (head))));
4486 }
4487 if (BB_END (bb) == head)
4488 {
4489 if (EDGE_COUNT (bb->succs) > 1)
4490 gcc_assert (control_flow_insn_p (head)
4491 || has_edge_p (bb->succs, EDGE_COMPLEX));
4492 bb = 0;
4493 }
4494
4495 head = NEXT_INSN (head);
4496 }
4497 }
4498
4499 not_first = 1;
4500 }
4501 while (head != next_tail);
4502
4503 gcc_assert (bb == 0);
4504 }
4505 #endif /* ENABLE_CHECKING */
4506
4507 #endif /* INSN_SCHEDULING */