1 /* Swing Modulo Scheduling implementation.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Ayal Zaks and Mustafa Hagog <zaks,mustafa@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
27 #include "diagnostic-core.h"
31 #include "hard-reg-set.h"
35 #include "insn-config.h"
36 #include "insn-attr.h"
40 #include "sched-int.h"
42 #include "cfglayout.h"
50 #include "tree-pass.h"
54 #ifdef INSN_SCHEDULING
56 /* This file contains the implementation of the Swing Modulo Scheduler,
57 described in the following references:
58 [1] J. Llosa, A. Gonzalez, E. Ayguade, M. Valero., and J. Eckhardt.
59 Lifetime--sensitive modulo scheduling in a production environment.
60 IEEE Trans. on Comps., 50(3), March 2001
61 [2] J. Llosa, A. Gonzalez, E. Ayguade, and M. Valero.
62 Swing Modulo Scheduling: A Lifetime Sensitive Approach.
63 PACT '96 , pages 80-87, October 1996 (Boston - Massachusetts - USA).
65 The basic structure is:
66 1. Build a data-dependence graph (DDG) for each loop.
67 2. Use the DDG to order the insns of a loop (not in topological order
68 necessarily, but rather) trying to place each insn after all its
69 predecessors _or_ after all its successors.
70 3. Compute MII: a lower bound on the number of cycles to schedule the loop.
71 4. Use the ordering to perform list-scheduling of the loop:
72 1. Set II = MII. We will try to schedule the loop within II cycles.
73 2. Try to schedule the insns one by one according to the ordering.
74 For each insn compute an interval of cycles by considering already-
75 scheduled preds and succs (and associated latencies); try to place
76 the insn in the cycles of this window checking for potential
77 resource conflicts (using the DFA interface).
78 Note: this is different from the cycle-scheduling of schedule_insns;
79 here the insns are not scheduled monotonically top-down (nor bottom-
81 3. If failed in scheduling all insns - bump II++ and try again, unless
82 II reaches an upper bound MaxII, in which case report failure.
83 5. If we succeeded in scheduling the loop within II cycles, we now
84 generate prolog and epilog, decrease the counter of the loop, and
85 perform modulo variable expansion for live ranges that span more than
86 II cycles (i.e. use register copies to prevent a def from overwriting
87 itself before reaching the use).
89 SMS works with countable loops (1) whose control part can be easily
90 decoupled from the rest of the loop and (2) whose loop count can
91 be easily adjusted. This is because we peel a constant number of
92 iterations into a prologue and epilogue for which we want to avoid
93 emitting the control part, and a kernel which is to iterate that
94 constant number of iterations less than the original loop. So the
95 control part should be a set of insns clearly identified and having
96 its own iv, not otherwise used in the loop (at-least for now), which
97 initializes a register before the loop to the number of iterations.
98 Currently SMS relies on the do-loop pattern to recognize such loops,
99 where (1) the control part comprises of all insns defining and/or
100 using a certain 'count' register and (2) the loop count can be
101 adjusted by modifying this register prior to the loop.
102 TODO: Rely on cfgloop analysis instead. */
104 /* This page defines partial-schedule structures and functions for
105 modulo scheduling. */
107 typedef struct partial_schedule
*partial_schedule_ptr
;
108 typedef struct ps_insn
*ps_insn_ptr
;
110 /* The minimum (absolute) cycle that a node of ps was scheduled in. */
111 #define PS_MIN_CYCLE(ps) (((partial_schedule_ptr)(ps))->min_cycle)
113 /* The maximum (absolute) cycle that a node of ps was scheduled in. */
114 #define PS_MAX_CYCLE(ps) (((partial_schedule_ptr)(ps))->max_cycle)
116 /* Perform signed modulo, always returning a non-negative value. */
117 #define SMODULO(x,y) ((x) % (y) < 0 ? ((x) % (y) + (y)) : (x) % (y))
119 /* The number of different iterations the nodes in ps span, assuming
120 the stage boundaries are placed efficiently. */
121 #define PS_STAGE_COUNT(ps) ((PS_MAX_CYCLE (ps) - PS_MIN_CYCLE (ps) \
122 + 1 + (ps)->ii - 1) / (ps)->ii)
124 /* A single instruction in the partial schedule. */
127 /* The corresponding DDG_NODE. */
130 /* The (absolute) cycle in which the PS instruction is scheduled.
131 Same as SCHED_TIME (node). */
134 /* The next/prev PS_INSN in the same row. */
135 ps_insn_ptr next_in_row
,
138 /* The number of nodes in the same row that come after this node. */
142 /* Holds the partial schedule as an array of II rows. Each entry of the
143 array points to a linked list of PS_INSNs, which represents the
144 instructions that are scheduled for that row. */
145 struct partial_schedule
147 int ii
; /* Number of rows in the partial schedule. */
148 int history
; /* Threshold for conflict checking using DFA. */
150 /* rows[i] points to linked list of insns scheduled in row i (0<=i<ii). */
153 /* The earliest absolute cycle of an insn in the partial schedule. */
156 /* The latest absolute cycle of an insn in the partial schedule. */
159 ddg_ptr g
; /* The DDG of the insns in the partial schedule. */
162 /* We use this to record all the register replacements we do in
163 the kernel so we can undo SMS if it is not profitable. */
164 struct undo_replace_buff_elem
169 struct undo_replace_buff_elem
*next
;
174 static partial_schedule_ptr
create_partial_schedule (int ii
, ddg_ptr
, int history
);
175 static void free_partial_schedule (partial_schedule_ptr
);
176 static void reset_partial_schedule (partial_schedule_ptr
, int new_ii
);
177 void print_partial_schedule (partial_schedule_ptr
, FILE *);
178 static void verify_partial_schedule (partial_schedule_ptr
, sbitmap
);
179 static ps_insn_ptr
ps_add_node_check_conflicts (partial_schedule_ptr
,
180 ddg_node_ptr node
, int cycle
,
181 sbitmap must_precede
,
182 sbitmap must_follow
);
183 static void rotate_partial_schedule (partial_schedule_ptr
, int);
184 void set_row_column_for_ps (partial_schedule_ptr
);
185 static void ps_insert_empty_row (partial_schedule_ptr
, int, sbitmap
);
186 static int compute_split_row (sbitmap
, int, int, int, ddg_node_ptr
);
189 /* This page defines constants and structures for the modulo scheduling
192 static int sms_order_nodes (ddg_ptr
, int, int *, int *);
193 static void set_node_sched_params (ddg_ptr
);
194 static partial_schedule_ptr
sms_schedule_by_order (ddg_ptr
, int, int, int *);
195 static void permute_partial_schedule (partial_schedule_ptr
, rtx
);
196 static void generate_prolog_epilog (partial_schedule_ptr
, struct loop
*,
198 static void duplicate_insns_of_cycles (partial_schedule_ptr
,
201 #define SCHED_ASAP(x) (((node_sched_params_ptr)(x)->aux.info)->asap)
202 #define SCHED_TIME(x) (((node_sched_params_ptr)(x)->aux.info)->time)
203 #define SCHED_FIRST_REG_MOVE(x) \
204 (((node_sched_params_ptr)(x)->aux.info)->first_reg_move)
205 #define SCHED_NREG_MOVES(x) \
206 (((node_sched_params_ptr)(x)->aux.info)->nreg_moves)
207 #define SCHED_ROW(x) (((node_sched_params_ptr)(x)->aux.info)->row)
208 #define SCHED_STAGE(x) (((node_sched_params_ptr)(x)->aux.info)->stage)
209 #define SCHED_COLUMN(x) (((node_sched_params_ptr)(x)->aux.info)->column)
211 /* The scheduling parameters held for each node. */
212 typedef struct node_sched_params
214 int asap
; /* A lower-bound on the absolute scheduling cycle. */
215 int time
; /* The absolute scheduling cycle (time >= asap). */
217 /* The following field (first_reg_move) is a pointer to the first
218 register-move instruction added to handle the modulo-variable-expansion
219 of the register defined by this node. This register-move copies the
220 original register defined by the node. */
223 /* The number of register-move instructions added, immediately preceding
227 int row
; /* Holds time % ii. */
228 int stage
; /* Holds time / ii. */
230 /* The column of a node inside the ps. If nodes u, v are on the same row,
231 u will precede v if column (u) < column (v). */
233 } *node_sched_params_ptr
;
236 /* The following three functions are copied from the current scheduler
237 code in order to use sched_analyze() for computing the dependencies.
238 They are used when initializing the sched_info structure. */
240 sms_print_insn (const_rtx insn
, int aligned ATTRIBUTE_UNUSED
)
244 sprintf (tmp
, "i%4d", INSN_UID (insn
));
249 compute_jump_reg_dependencies (rtx insn ATTRIBUTE_UNUSED
,
250 regset cond_exec ATTRIBUTE_UNUSED
,
251 regset used ATTRIBUTE_UNUSED
,
252 regset set ATTRIBUTE_UNUSED
)
256 static struct common_sched_info_def sms_common_sched_info
;
258 static struct sched_deps_info_def sms_sched_deps_info
=
260 compute_jump_reg_dependencies
,
261 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
266 static struct haifa_sched_info sms_sched_info
=
275 NULL
, /* insn_finishes_block_p */
284 /* Given HEAD and TAIL which are the first and last insns in a loop;
285 return the register which controls the loop. Return zero if it has
286 more than one occurrence in the loop besides the control part or the
287 do-loop pattern is not of the form we expect. */
289 doloop_register_get (rtx head ATTRIBUTE_UNUSED
, rtx tail ATTRIBUTE_UNUSED
)
291 #ifdef HAVE_doloop_end
292 rtx reg
, condition
, insn
, first_insn_not_to_check
;
297 /* TODO: Free SMS's dependence on doloop_condition_get. */
298 condition
= doloop_condition_get (tail
);
302 if (REG_P (XEXP (condition
, 0)))
303 reg
= XEXP (condition
, 0);
304 else if (GET_CODE (XEXP (condition
, 0)) == PLUS
305 && REG_P (XEXP (XEXP (condition
, 0), 0)))
306 reg
= XEXP (XEXP (condition
, 0), 0);
310 /* Check that the COUNT_REG has no other occurrences in the loop
311 until the decrement. We assume the control part consists of
312 either a single (parallel) branch-on-count or a (non-parallel)
313 branch immediately preceded by a single (decrement) insn. */
314 first_insn_not_to_check
= (GET_CODE (PATTERN (tail
)) == PARALLEL
? tail
317 for (insn
= head
; insn
!= first_insn_not_to_check
; insn
= NEXT_INSN (insn
))
318 if (reg_mentioned_p (reg
, insn
))
322 fprintf (dump_file
, "SMS count_reg found ");
323 print_rtl_single (dump_file
, reg
);
324 fprintf (dump_file
, " outside control in insn:\n");
325 print_rtl_single (dump_file
, insn
);
337 /* Check if COUNT_REG is set to a constant in the PRE_HEADER block, so
338 that the number of iterations is a compile-time constant. If so,
339 return the rtx that sets COUNT_REG to a constant, and set COUNT to
340 this constant. Otherwise return 0. */
342 const_iteration_count (rtx count_reg
, basic_block pre_header
,
343 HOST_WIDEST_INT
* count
)
351 get_ebb_head_tail (pre_header
, pre_header
, &head
, &tail
);
353 for (insn
= tail
; insn
!= PREV_INSN (head
); insn
= PREV_INSN (insn
))
354 if (NONDEBUG_INSN_P (insn
) && single_set (insn
) &&
355 rtx_equal_p (count_reg
, SET_DEST (single_set (insn
))))
357 rtx pat
= single_set (insn
);
359 if (CONST_INT_P (SET_SRC (pat
)))
361 *count
= INTVAL (SET_SRC (pat
));
371 /* A very simple resource-based lower bound on the initiation interval.
372 ??? Improve the accuracy of this bound by considering the
373 utilization of various units. */
377 if (targetm
.sched
.sms_res_mii
)
378 return targetm
.sched
.sms_res_mii (g
);
380 return ((g
->num_nodes
- g
->num_debug
) / issue_rate
);
384 /* Points to the array that contains the sched data for each node. */
385 static node_sched_params_ptr node_sched_params
;
387 /* Allocate sched_params for each node and initialize it. Assumes that
388 the aux field of each node contain the asap bound (computed earlier),
389 and copies it into the sched_params field. */
391 set_node_sched_params (ddg_ptr g
)
395 /* Allocate for each node in the DDG a place to hold the "sched_data". */
396 /* Initialize ASAP/ALAP/HIGHT to zero. */
397 node_sched_params
= (node_sched_params_ptr
)
398 xcalloc (g
->num_nodes
,
399 sizeof (struct node_sched_params
));
401 /* Set the pointer of the general data of the node to point to the
402 appropriate sched_params structure. */
403 for (i
= 0; i
< g
->num_nodes
; i
++)
405 /* Watch out for aliasing problems? */
406 node_sched_params
[i
].asap
= g
->nodes
[i
].aux
.count
;
407 g
->nodes
[i
].aux
.info
= &node_sched_params
[i
];
412 print_node_sched_params (FILE *file
, int num_nodes
, ddg_ptr g
)
418 for (i
= 0; i
< num_nodes
; i
++)
420 node_sched_params_ptr nsp
= &node_sched_params
[i
];
421 rtx reg_move
= nsp
->first_reg_move
;
424 fprintf (file
, "Node = %d; INSN = %d\n", i
,
425 (INSN_UID (g
->nodes
[i
].insn
)));
426 fprintf (file
, " asap = %d:\n", nsp
->asap
);
427 fprintf (file
, " time = %d:\n", nsp
->time
);
428 fprintf (file
, " nreg_moves = %d:\n", nsp
->nreg_moves
);
429 for (j
= 0; j
< nsp
->nreg_moves
; j
++)
431 fprintf (file
, " reg_move = ");
432 print_rtl_single (file
, reg_move
);
433 reg_move
= PREV_INSN (reg_move
);
439 Breaking intra-loop register anti-dependences:
440 Each intra-loop register anti-dependence implies a cross-iteration true
441 dependence of distance 1. Therefore, we can remove such false dependencies
442 and figure out if the partial schedule broke them by checking if (for a
443 true-dependence of distance 1): SCHED_TIME (def) < SCHED_TIME (use) and
444 if so generate a register move. The number of such moves is equal to:
445 SCHED_TIME (use) - SCHED_TIME (def) { 0 broken
446 nreg_moves = ----------------------------------- + 1 - { dependence.
449 static struct undo_replace_buff_elem
*
450 generate_reg_moves (partial_schedule_ptr ps
, bool rescan
)
455 struct undo_replace_buff_elem
*reg_move_replaces
= NULL
;
457 for (i
= 0; i
< g
->num_nodes
; i
++)
459 ddg_node_ptr u
= &g
->nodes
[i
];
461 int nreg_moves
= 0, i_reg_move
;
462 sbitmap
*uses_of_defs
;
464 rtx prev_reg
, old_reg
;
466 /* Compute the number of reg_moves needed for u, by looking at life
467 ranges started at u (excluding self-loops). */
468 for (e
= u
->out
; e
; e
= e
->next_out
)
469 if (e
->type
== TRUE_DEP
&& e
->dest
!= e
->src
)
471 int nreg_moves4e
= (SCHED_TIME (e
->dest
) - SCHED_TIME (e
->src
)) / ii
;
473 if (e
->distance
== 1)
474 nreg_moves4e
= (SCHED_TIME (e
->dest
) - SCHED_TIME (e
->src
) + ii
) / ii
;
476 /* If dest precedes src in the schedule of the kernel, then dest
477 will read before src writes and we can save one reg_copy. */
478 if (SCHED_ROW (e
->dest
) == SCHED_ROW (e
->src
)
479 && SCHED_COLUMN (e
->dest
) < SCHED_COLUMN (e
->src
))
482 nreg_moves
= MAX (nreg_moves
, nreg_moves4e
);
488 /* Every use of the register defined by node may require a different
489 copy of this register, depending on the time the use is scheduled.
490 Set a bitmap vector, telling which nodes use each copy of this
492 uses_of_defs
= sbitmap_vector_alloc (nreg_moves
, g
->num_nodes
);
493 sbitmap_vector_zero (uses_of_defs
, nreg_moves
);
494 for (e
= u
->out
; e
; e
= e
->next_out
)
495 if (e
->type
== TRUE_DEP
&& e
->dest
!= e
->src
)
497 int dest_copy
= (SCHED_TIME (e
->dest
) - SCHED_TIME (e
->src
)) / ii
;
499 if (e
->distance
== 1)
500 dest_copy
= (SCHED_TIME (e
->dest
) - SCHED_TIME (e
->src
) + ii
) / ii
;
502 if (SCHED_ROW (e
->dest
) == SCHED_ROW (e
->src
)
503 && SCHED_COLUMN (e
->dest
) < SCHED_COLUMN (e
->src
))
507 SET_BIT (uses_of_defs
[dest_copy
- 1], e
->dest
->cuid
);
510 /* Now generate the reg_moves, attaching relevant uses to them. */
511 SCHED_NREG_MOVES (u
) = nreg_moves
;
512 old_reg
= prev_reg
= copy_rtx (SET_DEST (single_set (u
->insn
)));
513 /* Insert the reg-moves right before the notes which precede
514 the insn they relates to. */
515 last_reg_move
= u
->first_note
;
517 for (i_reg_move
= 0; i_reg_move
< nreg_moves
; i_reg_move
++)
519 unsigned int i_use
= 0;
520 rtx new_reg
= gen_reg_rtx (GET_MODE (prev_reg
));
521 rtx reg_move
= gen_move_insn (new_reg
, prev_reg
);
522 sbitmap_iterator sbi
;
524 add_insn_before (reg_move
, last_reg_move
, NULL
);
525 last_reg_move
= reg_move
;
527 if (!SCHED_FIRST_REG_MOVE (u
))
528 SCHED_FIRST_REG_MOVE (u
) = reg_move
;
530 EXECUTE_IF_SET_IN_SBITMAP (uses_of_defs
[i_reg_move
], 0, i_use
, sbi
)
532 struct undo_replace_buff_elem
*rep
;
534 rep
= (struct undo_replace_buff_elem
*)
535 xcalloc (1, sizeof (struct undo_replace_buff_elem
));
536 rep
->insn
= g
->nodes
[i_use
].insn
;
537 rep
->orig_reg
= old_reg
;
538 rep
->new_reg
= new_reg
;
540 if (! reg_move_replaces
)
541 reg_move_replaces
= rep
;
544 rep
->next
= reg_move_replaces
;
545 reg_move_replaces
= rep
;
548 replace_rtx (g
->nodes
[i_use
].insn
, old_reg
, new_reg
);
550 df_insn_rescan (g
->nodes
[i_use
].insn
);
555 sbitmap_vector_free (uses_of_defs
);
557 return reg_move_replaces
;
560 /* Free memory allocated for the undo buffer. */
562 free_undo_replace_buff (struct undo_replace_buff_elem
*reg_move_replaces
)
565 while (reg_move_replaces
)
567 struct undo_replace_buff_elem
*rep
= reg_move_replaces
;
569 reg_move_replaces
= reg_move_replaces
->next
;
574 /* Bump the SCHED_TIMEs of all nodes to start from zero. Set the values
575 of SCHED_ROW and SCHED_STAGE. */
577 normalize_sched_times (partial_schedule_ptr ps
)
580 int amount
= PS_MIN_CYCLE (ps
);
582 ps_insn_ptr crr_insn
;
584 for (row
= 0; row
< ii
; row
++)
585 for (crr_insn
= ps
->rows
[row
]; crr_insn
; crr_insn
= crr_insn
->next_in_row
)
587 ddg_node_ptr u
= crr_insn
->node
;
588 int normalized_time
= SCHED_TIME (u
) - amount
;
591 fprintf (dump_file
, "crr_insn->node=%d, crr_insn->cycle=%d,\
592 min_cycle=%d\n", crr_insn
->node
->cuid
, SCHED_TIME
594 gcc_assert (SCHED_TIME (u
) >= ps
->min_cycle
);
595 gcc_assert (SCHED_TIME (u
) <= ps
->max_cycle
);
596 SCHED_TIME (u
) = normalized_time
;
597 SCHED_ROW (u
) = normalized_time
% ii
;
598 SCHED_STAGE (u
) = normalized_time
/ ii
;
602 /* Set SCHED_COLUMN of each node according to its position in PS. */
604 set_columns_for_ps (partial_schedule_ptr ps
)
608 for (row
= 0; row
< ps
->ii
; row
++)
610 ps_insn_ptr cur_insn
= ps
->rows
[row
];
613 for (; cur_insn
; cur_insn
= cur_insn
->next_in_row
)
614 SCHED_COLUMN (cur_insn
->node
) = column
++;
618 /* Permute the insns according to their order in PS, from row 0 to
619 row ii-1, and position them right before LAST. This schedules
620 the insns of the loop kernel. */
622 permute_partial_schedule (partial_schedule_ptr ps
, rtx last
)
628 for (row
= 0; row
< ii
; row
++)
629 for (ps_ij
= ps
->rows
[row
]; ps_ij
; ps_ij
= ps_ij
->next_in_row
)
630 if (PREV_INSN (last
) != ps_ij
->node
->insn
)
631 reorder_insns_nobb (ps_ij
->node
->first_note
, ps_ij
->node
->insn
,
636 duplicate_insns_of_cycles (partial_schedule_ptr ps
, int from_stage
,
637 int to_stage
, int for_prolog
, rtx count_reg
)
642 for (row
= 0; row
< ps
->ii
; row
++)
643 for (ps_ij
= ps
->rows
[row
]; ps_ij
; ps_ij
= ps_ij
->next_in_row
)
645 ddg_node_ptr u_node
= ps_ij
->node
;
647 rtx reg_move
= NULL_RTX
;
649 /* Do not duplicate any insn which refers to count_reg as it
650 belongs to the control part.
651 TODO: This should be done by analyzing the control part of
653 if (reg_mentioned_p (count_reg
, u_node
->insn
))
658 /* SCHED_STAGE (u_node) >= from_stage == 0. Generate increasing
659 number of reg_moves starting with the second occurrence of
660 u_node, which is generated if its SCHED_STAGE <= to_stage. */
661 i_reg_moves
= to_stage
- SCHED_STAGE (u_node
) + 1;
662 i_reg_moves
= MAX (i_reg_moves
, 0);
663 i_reg_moves
= MIN (i_reg_moves
, SCHED_NREG_MOVES (u_node
));
665 /* The reg_moves start from the *first* reg_move backwards. */
668 reg_move
= SCHED_FIRST_REG_MOVE (u_node
);
669 for (j
= 1; j
< i_reg_moves
; j
++)
670 reg_move
= PREV_INSN (reg_move
);
673 else /* It's for the epilog. */
675 /* SCHED_STAGE (u_node) <= to_stage. Generate all reg_moves,
676 starting to decrease one stage after u_node no longer occurs;
677 that is, generate all reg_moves until
678 SCHED_STAGE (u_node) == from_stage - 1. */
679 i_reg_moves
= SCHED_NREG_MOVES (u_node
)
680 - (from_stage
- SCHED_STAGE (u_node
) - 1);
681 i_reg_moves
= MAX (i_reg_moves
, 0);
682 i_reg_moves
= MIN (i_reg_moves
, SCHED_NREG_MOVES (u_node
));
684 /* The reg_moves start from the *last* reg_move forwards. */
687 reg_move
= SCHED_FIRST_REG_MOVE (u_node
);
688 for (j
= 1; j
< SCHED_NREG_MOVES (u_node
); j
++)
689 reg_move
= PREV_INSN (reg_move
);
693 for (j
= 0; j
< i_reg_moves
; j
++, reg_move
= NEXT_INSN (reg_move
))
694 emit_insn (copy_rtx (PATTERN (reg_move
)));
695 if (SCHED_STAGE (u_node
) >= from_stage
696 && SCHED_STAGE (u_node
) <= to_stage
)
697 duplicate_insn_chain (u_node
->first_note
, u_node
->insn
);
702 /* Generate the instructions (including reg_moves) for prolog & epilog. */
704 generate_prolog_epilog (partial_schedule_ptr ps
, struct loop
*loop
,
705 rtx count_reg
, rtx count_init
)
708 int last_stage
= PS_STAGE_COUNT (ps
) - 1;
711 /* Generate the prolog, inserting its insns on the loop-entry edge. */
716 /* Generate instructions at the beginning of the prolog to
717 adjust the loop count by STAGE_COUNT. If loop count is constant
718 (count_init), this constant is adjusted by STAGE_COUNT in
719 generate_prolog_epilog function. */
720 rtx sub_reg
= NULL_RTX
;
722 sub_reg
= expand_simple_binop (GET_MODE (count_reg
), MINUS
,
723 count_reg
, GEN_INT (last_stage
),
724 count_reg
, 1, OPTAB_DIRECT
);
725 gcc_assert (REG_P (sub_reg
));
726 if (REGNO (sub_reg
) != REGNO (count_reg
))
727 emit_move_insn (count_reg
, sub_reg
);
730 for (i
= 0; i
< last_stage
; i
++)
731 duplicate_insns_of_cycles (ps
, 0, i
, 1, count_reg
);
733 /* Put the prolog on the entry edge. */
734 e
= loop_preheader_edge (loop
);
735 split_edge_and_insert (e
, get_insns ());
739 /* Generate the epilog, inserting its insns on the loop-exit edge. */
742 for (i
= 0; i
< last_stage
; i
++)
743 duplicate_insns_of_cycles (ps
, i
+ 1, last_stage
, 0, count_reg
);
745 /* Put the epilogue on the exit edge. */
746 gcc_assert (single_exit (loop
));
747 e
= single_exit (loop
);
748 split_edge_and_insert (e
, get_insns ());
752 /* Return true if all the BBs of the loop are empty except the
755 loop_single_full_bb_p (struct loop
*loop
)
758 basic_block
*bbs
= get_loop_body (loop
);
760 for (i
= 0; i
< loop
->num_nodes
; i
++)
763 bool empty_bb
= true;
765 if (bbs
[i
] == loop
->header
)
768 /* Make sure that basic blocks other than the header
769 have only notes labels or jumps. */
770 get_ebb_head_tail (bbs
[i
], bbs
[i
], &head
, &tail
);
771 for (; head
!= NEXT_INSN (tail
); head
= NEXT_INSN (head
))
773 if (NOTE_P (head
) || LABEL_P (head
)
774 || (INSN_P (head
) && (DEBUG_INSN_P (head
) || JUMP_P (head
))))
790 /* A simple loop from SMS point of view; it is a loop that is composed of
791 either a single basic block or two BBs - a header and a latch. */
792 #define SIMPLE_SMS_LOOP_P(loop) ((loop->num_nodes < 3 ) \
793 && (EDGE_COUNT (loop->latch->preds) == 1) \
794 && (EDGE_COUNT (loop->latch->succs) == 1))
796 /* Return true if the loop is in its canonical form and false if not.
797 i.e. SIMPLE_SMS_LOOP_P and have one preheader block, and single exit. */
799 loop_canon_p (struct loop
*loop
)
802 if (loop
->inner
|| !loop_outer (loop
))
805 fprintf (dump_file
, "SMS loop inner or !loop_outer\n");
809 if (!single_exit (loop
))
813 rtx insn
= BB_END (loop
->header
);
815 fprintf (dump_file
, "SMS loop many exits ");
816 fprintf (dump_file
, " %s %d (file, line)\n",
817 insn_file (insn
), insn_line (insn
));
822 if (! SIMPLE_SMS_LOOP_P (loop
) && ! loop_single_full_bb_p (loop
))
826 rtx insn
= BB_END (loop
->header
);
828 fprintf (dump_file
, "SMS loop many BBs. ");
829 fprintf (dump_file
, " %s %d (file, line)\n",
830 insn_file (insn
), insn_line (insn
));
838 /* If there are more than one entry for the loop,
839 make it one by splitting the first entry edge and
840 redirecting the others to the new BB. */
842 canon_loop (struct loop
*loop
)
847 /* Avoid annoying special cases of edges going to exit
849 FOR_EACH_EDGE (e
, i
, EXIT_BLOCK_PTR
->preds
)
850 if ((e
->flags
& EDGE_FALLTHRU
) && (EDGE_COUNT (e
->src
->succs
) > 1))
853 if (loop
->latch
== loop
->header
854 || EDGE_COUNT (loop
->latch
->succs
) > 1)
856 FOR_EACH_EDGE (e
, i
, loop
->header
->preds
)
857 if (e
->src
== loop
->latch
)
865 setup_sched_infos (void)
867 memcpy (&sms_common_sched_info
, &haifa_common_sched_info
,
868 sizeof (sms_common_sched_info
));
869 sms_common_sched_info
.sched_pass_id
= SCHED_SMS_PASS
;
870 common_sched_info
= &sms_common_sched_info
;
872 sched_deps_info
= &sms_sched_deps_info
;
873 current_sched_info
= &sms_sched_info
;
876 /* Probability in % that the sms-ed loop rolls enough so that optimized
877 version may be entered. Just a guess. */
878 #define PROB_SMS_ENOUGH_ITERATIONS 80
880 /* Used to calculate the upper bound of ii. */
881 #define MAXII_FACTOR 2
883 /* Main entry point, perform SMS scheduling on the loops of the function
884 that consist of single basic blocks. */
893 partial_schedule_ptr ps
;
894 basic_block bb
= NULL
;
896 basic_block condition_bb
= NULL
;
898 gcov_type trip_count
= 0;
900 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
901 | LOOPS_HAVE_RECORDED_EXITS
);
902 if (number_of_loops () <= 1)
904 loop_optimizer_finalize ();
905 return; /* There are no loops to schedule. */
908 /* Initialize issue_rate. */
909 if (targetm
.sched
.issue_rate
)
911 int temp
= reload_completed
;
913 reload_completed
= 1;
914 issue_rate
= targetm
.sched
.issue_rate ();
915 reload_completed
= temp
;
920 /* Initialize the scheduler. */
921 setup_sched_infos ();
924 /* Allocate memory to hold the DDG array one entry for each loop.
925 We use loop->num as index into this array. */
926 g_arr
= XCNEWVEC (ddg_ptr
, number_of_loops ());
930 fprintf (dump_file
, "\n\nSMS analysis phase\n");
931 fprintf (dump_file
, "===================\n\n");
934 /* Build DDGs for all the relevant loops and hold them in G_ARR
935 indexed by the loop index. */
936 FOR_EACH_LOOP (li
, loop
, 0)
942 if (dbg_cnt (sms_sched_loop
) == false)
945 fprintf (dump_file
, "SMS reached max limit... \n");
952 rtx insn
= BB_END (loop
->header
);
954 fprintf (dump_file
, "SMS loop num: %d, file: %s, line: %d\n",
955 loop
->num
, insn_file (insn
), insn_line (insn
));
959 if (! loop_canon_p (loop
))
962 if (! loop_single_full_bb_p (loop
))
965 fprintf (dump_file
, "SMS not loop_single_full_bb_p\n");
971 get_ebb_head_tail (bb
, bb
, &head
, &tail
);
972 latch_edge
= loop_latch_edge (loop
);
973 gcc_assert (single_exit (loop
));
974 if (single_exit (loop
)->count
)
975 trip_count
= latch_edge
->count
/ single_exit (loop
)->count
;
977 /* Perform SMS only on loops that their average count is above threshold. */
979 if ( latch_edge
->count
980 && (latch_edge
->count
< single_exit (loop
)->count
* SMS_LOOP_AVERAGE_COUNT_THRESHOLD
))
984 fprintf (dump_file
, " %s %d (file, line)\n",
985 insn_file (tail
), insn_line (tail
));
986 fprintf (dump_file
, "SMS single-bb-loop\n");
987 if (profile_info
&& flag_branch_probabilities
)
989 fprintf (dump_file
, "SMS loop-count ");
990 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
,
991 (HOST_WIDEST_INT
) bb
->count
);
992 fprintf (dump_file
, "\n");
993 fprintf (dump_file
, "SMS trip-count ");
994 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
,
995 (HOST_WIDEST_INT
) trip_count
);
996 fprintf (dump_file
, "\n");
997 fprintf (dump_file
, "SMS profile-sum-max ");
998 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
,
999 (HOST_WIDEST_INT
) profile_info
->sum_max
);
1000 fprintf (dump_file
, "\n");
1006 /* Make sure this is a doloop. */
1007 if ( !(count_reg
= doloop_register_get (head
, tail
)))
1010 fprintf (dump_file
, "SMS doloop_register_get failed\n");
1014 /* Don't handle BBs with calls or barriers, or !single_set insns,
1015 or auto-increment insns (to avoid creating invalid reg-moves
1016 for the auto-increment insns).
1017 ??? Should handle auto-increment insns.
1018 ??? Should handle insns defining subregs. */
1019 for (insn
= head
; insn
!= NEXT_INSN (tail
); insn
= NEXT_INSN (insn
))
1025 || (NONDEBUG_INSN_P (insn
) && !JUMP_P (insn
)
1026 && !single_set (insn
) && GET_CODE (PATTERN (insn
)) != USE
)
1027 || (FIND_REG_INC_NOTE (insn
, NULL_RTX
) != 0)
1028 || (INSN_P (insn
) && (set
= single_set (insn
))
1029 && GET_CODE (SET_DEST (set
)) == SUBREG
))
1033 if (insn
!= NEXT_INSN (tail
))
1038 fprintf (dump_file
, "SMS loop-with-call\n");
1039 else if (BARRIER_P (insn
))
1040 fprintf (dump_file
, "SMS loop-with-barrier\n");
1041 else if (FIND_REG_INC_NOTE (insn
, NULL_RTX
) != 0)
1042 fprintf (dump_file
, "SMS reg inc\n");
1043 else if ((NONDEBUG_INSN_P (insn
) && !JUMP_P (insn
)
1044 && !single_set (insn
) && GET_CODE (PATTERN (insn
)) != USE
))
1045 fprintf (dump_file
, "SMS loop-with-not-single-set\n");
1047 fprintf (dump_file
, "SMS loop with subreg in lhs\n");
1048 print_rtl_single (dump_file
, insn
);
1054 if (! (g
= create_ddg (bb
, 0)))
1057 fprintf (dump_file
, "SMS create_ddg failed\n");
1061 g_arr
[loop
->num
] = g
;
1063 fprintf (dump_file
, "...OK\n");
1068 fprintf (dump_file
, "\nSMS transformation phase\n");
1069 fprintf (dump_file
, "=========================\n\n");
1072 /* We don't want to perform SMS on new loops - created by versioning. */
1073 FOR_EACH_LOOP (li
, loop
, 0)
1076 rtx count_reg
, count_init
;
1078 unsigned stage_count
= 0;
1079 HOST_WIDEST_INT loop_count
= 0;
1081 if (! (g
= g_arr
[loop
->num
]))
1086 rtx insn
= BB_END (loop
->header
);
1088 fprintf (dump_file
, "SMS loop num: %d, file: %s, line: %d\n",
1089 loop
->num
, insn_file (insn
), insn_line (insn
));
1091 print_ddg (dump_file
, g
);
1094 get_ebb_head_tail (loop
->header
, loop
->header
, &head
, &tail
);
1096 latch_edge
= loop_latch_edge (loop
);
1097 gcc_assert (single_exit (loop
));
1098 if (single_exit (loop
)->count
)
1099 trip_count
= latch_edge
->count
/ single_exit (loop
)->count
;
1103 fprintf (dump_file
, " %s %d (file, line)\n",
1104 insn_file (tail
), insn_line (tail
));
1105 fprintf (dump_file
, "SMS single-bb-loop\n");
1106 if (profile_info
&& flag_branch_probabilities
)
1108 fprintf (dump_file
, "SMS loop-count ");
1109 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
,
1110 (HOST_WIDEST_INT
) bb
->count
);
1111 fprintf (dump_file
, "\n");
1112 fprintf (dump_file
, "SMS profile-sum-max ");
1113 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
,
1114 (HOST_WIDEST_INT
) profile_info
->sum_max
);
1115 fprintf (dump_file
, "\n");
1117 fprintf (dump_file
, "SMS doloop\n");
1118 fprintf (dump_file
, "SMS built-ddg %d\n", g
->num_nodes
);
1119 fprintf (dump_file
, "SMS num-loads %d\n", g
->num_loads
);
1120 fprintf (dump_file
, "SMS num-stores %d\n", g
->num_stores
);
1124 /* In case of th loop have doloop register it gets special
1126 count_init
= NULL_RTX
;
1127 if ((count_reg
= doloop_register_get (head
, tail
)))
1129 basic_block pre_header
;
1131 pre_header
= loop_preheader_edge (loop
)->src
;
1132 count_init
= const_iteration_count (count_reg
, pre_header
,
1135 gcc_assert (count_reg
);
1137 if (dump_file
&& count_init
)
1139 fprintf (dump_file
, "SMS const-doloop ");
1140 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
,
1142 fprintf (dump_file
, "\n");
1145 node_order
= XNEWVEC (int, g
->num_nodes
);
1147 mii
= 1; /* Need to pass some estimate of mii. */
1148 rec_mii
= sms_order_nodes (g
, mii
, node_order
, &max_asap
);
1149 mii
= MAX (res_MII (g
), rec_mii
);
1150 maxii
= MAX (max_asap
, MAXII_FACTOR
* mii
);
1153 fprintf (dump_file
, "SMS iis %d %d %d (rec_mii, mii, maxii)\n",
1154 rec_mii
, mii
, maxii
);
1156 /* After sms_order_nodes and before sms_schedule_by_order, to copy over
1158 set_node_sched_params (g
);
1160 ps
= sms_schedule_by_order (g
, mii
, maxii
, node_order
);
1163 stage_count
= PS_STAGE_COUNT (ps
);
1164 gcc_assert(stage_count
>= 1);
1167 /* Stage count of 1 means that there is no interleaving between
1168 iterations, let the scheduling passes do the job. */
1169 if (stage_count
<= 1
1170 || (count_init
&& (loop_count
<= stage_count
))
1171 || (flag_branch_probabilities
&& (trip_count
<= stage_count
)))
1175 fprintf (dump_file
, "SMS failed... \n");
1176 fprintf (dump_file
, "SMS sched-failed (stage-count=%d, loop-count=", stage_count
);
1177 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
, loop_count
);
1178 fprintf (dump_file
, ", trip-count=");
1179 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
, trip_count
);
1180 fprintf (dump_file
, ")\n");
1186 struct undo_replace_buff_elem
*reg_move_replaces
;
1191 "SMS succeeded %d %d (with ii, sc)\n", ps
->ii
,
1193 print_partial_schedule (ps
, dump_file
);
1195 "SMS Branch (%d) will later be scheduled at cycle %d.\n",
1196 g
->closing_branch
->cuid
, PS_MIN_CYCLE (ps
) - 1);
1199 /* Set the stage boundaries. If the DDG is built with closing_branch_deps,
1200 the closing_branch was scheduled and should appear in the last (ii-1)
1201 row. Otherwise, we are free to schedule the branch, and we let nodes
1202 that were scheduled at the first PS_MIN_CYCLE cycle appear in the first
1203 row; this should reduce stage_count to minimum.
1204 TODO: Revisit the issue of scheduling the insns of the
1205 control part relative to the branch when the control part
1206 has more than one insn. */
1207 normalize_sched_times (ps
);
1208 rotate_partial_schedule (ps
, PS_MIN_CYCLE (ps
));
1209 set_columns_for_ps (ps
);
1213 /* case the BCT count is not known , Do loop-versioning */
1214 if (count_reg
&& ! count_init
)
1216 rtx comp_rtx
= gen_rtx_fmt_ee (GT
, VOIDmode
, count_reg
,
1217 GEN_INT(stage_count
));
1218 unsigned prob
= (PROB_SMS_ENOUGH_ITERATIONS
1219 * REG_BR_PROB_BASE
) / 100;
1221 loop_version (loop
, comp_rtx
, &condition_bb
,
1222 prob
, prob
, REG_BR_PROB_BASE
- prob
,
1226 /* Set new iteration count of loop kernel. */
1227 if (count_reg
&& count_init
)
1228 SET_SRC (single_set (count_init
)) = GEN_INT (loop_count
1231 /* Now apply the scheduled kernel to the RTL of the loop. */
1232 permute_partial_schedule (ps
, g
->closing_branch
->first_note
);
1234 /* Mark this loop as software pipelined so the later
1235 scheduling passes doesn't touch it. */
1236 if (! flag_resched_modulo_sched
)
1237 g
->bb
->flags
|= BB_DISABLE_SCHEDULE
;
1238 /* The life-info is not valid any more. */
1239 df_set_bb_dirty (g
->bb
);
1241 reg_move_replaces
= generate_reg_moves (ps
, true);
1243 print_node_sched_params (dump_file
, g
->num_nodes
, g
);
1244 /* Generate prolog and epilog. */
1245 generate_prolog_epilog (ps
, loop
, count_reg
, count_init
);
1247 free_undo_replace_buff (reg_move_replaces
);
1250 free_partial_schedule (ps
);
1251 free (node_sched_params
);
1258 /* Release scheduler data, needed until now because of DFA. */
1259 haifa_sched_finish ();
1260 loop_optimizer_finalize ();
1263 /* The SMS scheduling algorithm itself
1264 -----------------------------------
1265 Input: 'O' an ordered list of insns of a loop.
1266 Output: A scheduling of the loop - kernel, prolog, and epilogue.
1268 'Q' is the empty Set
1269 'PS' is the partial schedule; it holds the currently scheduled nodes with
1271 'PSP' previously scheduled predecessors.
1272 'PSS' previously scheduled successors.
1273 't(u)' the cycle where u is scheduled.
1274 'l(u)' is the latency of u.
1275 'd(v,u)' is the dependence distance from v to u.
1276 'ASAP(u)' the earliest time at which u could be scheduled as computed in
1277 the node ordering phase.
1278 'check_hardware_resources_conflicts(u, PS, c)'
1279 run a trace around cycle/slot through DFA model
1280 to check resource conflicts involving instruction u
1281 at cycle c given the partial schedule PS.
1282 'add_to_partial_schedule_at_time(u, PS, c)'
1283 Add the node/instruction u to the partial schedule
1285 'calculate_register_pressure(PS)'
1286 Given a schedule of instructions, calculate the register
1287 pressure it implies. One implementation could be the
1288 maximum number of overlapping live ranges.
1289 'maxRP' The maximum allowed register pressure, it is usually derived from the number
1290 registers available in the hardware.
1294 3. for each node u in O in pre-computed order
1295 4. if (PSP(u) != Q && PSS(u) == Q) then
1296 5. Early_start(u) = max ( t(v) + l(v) - d(v,u)*II ) over all every v in PSP(u).
1297 6. start = Early_start; end = Early_start + II - 1; step = 1
1298 11. else if (PSP(u) == Q && PSS(u) != Q) then
1299 12. Late_start(u) = min ( t(v) - l(v) + d(v,u)*II ) over all every v in PSS(u).
1300 13. start = Late_start; end = Late_start - II + 1; step = -1
1301 14. else if (PSP(u) != Q && PSS(u) != Q) then
1302 15. Early_start(u) = max ( t(v) + l(v) - d(v,u)*II ) over all every v in PSP(u).
1303 16. Late_start(u) = min ( t(v) - l(v) + d(v,u)*II ) over all every v in PSS(u).
1304 17. start = Early_start;
1305 18. end = min(Early_start + II - 1 , Late_start);
1307 20. else "if (PSP(u) == Q && PSS(u) == Q)"
1308 21. start = ASAP(u); end = start + II - 1; step = 1
1312 24. for (c = start ; c != end ; c += step)
1313 25. if check_hardware_resources_conflicts(u, PS, c) then
1314 26. add_to_partial_schedule_at_time(u, PS, c)
1319 31. if (success == false) then
1321 33. if (II > maxII) then
1322 34. finish - failed to schedule
1327 39. if (calculate_register_pressure(PS) > maxRP) then
1330 42. compute epilogue & prologue
1331 43. finish - succeeded to schedule
1334 /* A limit on the number of cycles that resource conflicts can span. ??? Should
1335 be provided by DFA, and be dependent on the type of insn scheduled. Currently
1336 set to 0 to save compile time. */
1337 #define DFA_HISTORY SMS_DFA_HISTORY
1339 /* A threshold for the number of repeated unsuccessful attempts to insert
1340 an empty row, before we flush the partial schedule and start over. */
1341 #define MAX_SPLIT_NUM 10
1342 /* Given the partial schedule PS, this function calculates and returns the
1343 cycles in which we can schedule the node with the given index I.
1344 NOTE: Here we do the backtracking in SMS, in some special cases. We have
1345 noticed that there are several cases in which we fail to SMS the loop
1346 because the sched window of a node is empty due to tight data-deps. In
1347 such cases we want to unschedule some of the predecessors/successors
1348 until we get non-empty scheduling window. It returns -1 if the
1349 scheduling window is empty and zero otherwise. */
1352 get_sched_window (partial_schedule_ptr ps
, int *nodes_order
, int i
,
1353 sbitmap sched_nodes
, int ii
, int *start_p
, int *step_p
, int *end_p
)
1355 int start
, step
, end
;
1357 int u
= nodes_order
[i
];
1358 ddg_node_ptr u_node
= &ps
->g
->nodes
[u
];
1359 sbitmap psp
= sbitmap_alloc (ps
->g
->num_nodes
);
1360 sbitmap pss
= sbitmap_alloc (ps
->g
->num_nodes
);
1361 sbitmap u_node_preds
= NODE_PREDECESSORS (u_node
);
1362 sbitmap u_node_succs
= NODE_SUCCESSORS (u_node
);
1366 /* 1. compute sched window for u (start, end, step). */
1369 psp_not_empty
= sbitmap_a_and_b_cg (psp
, u_node_preds
, sched_nodes
);
1370 pss_not_empty
= sbitmap_a_and_b_cg (pss
, u_node_succs
, sched_nodes
);
1372 if (psp_not_empty
&& !pss_not_empty
)
1374 int early_start
= INT_MIN
;
1377 for (e
= u_node
->in
; e
!= 0; e
= e
->next_in
)
1379 ddg_node_ptr v_node
= e
->src
;
1383 fprintf (dump_file
, "\nProcessing edge: ");
1384 print_ddg_edge (dump_file
, e
);
1386 "\nScheduling %d (%d) in psp_not_empty,"
1387 " checking p %d (%d): ", u_node
->cuid
,
1388 INSN_UID (u_node
->insn
), v_node
->cuid
, INSN_UID
1392 if (TEST_BIT (sched_nodes
, v_node
->cuid
))
1394 int p_st
= SCHED_TIME (v_node
);
1397 MAX (early_start
, p_st
+ e
->latency
- (e
->distance
* ii
));
1401 "pred st = %d; early_start = %d; latency: %d",
1402 p_st
, early_start
, e
->latency
);
1404 if (e
->data_type
== MEM_DEP
)
1405 end
= MIN (end
, SCHED_TIME (v_node
) + ii
- 1);
1408 fprintf (dump_file
, "the node is not scheduled\n");
1410 start
= early_start
;
1411 end
= MIN (end
, early_start
+ ii
);
1412 /* Schedule the node close to it's predecessors. */
1417 "\nScheduling %d (%d) in a window (%d..%d) with step %d\n",
1418 u_node
->cuid
, INSN_UID (u_node
->insn
), start
, end
, step
);
1421 else if (!psp_not_empty
&& pss_not_empty
)
1423 int late_start
= INT_MAX
;
1426 for (e
= u_node
->out
; e
!= 0; e
= e
->next_out
)
1428 ddg_node_ptr v_node
= e
->dest
;
1432 fprintf (dump_file
, "\nProcessing edge:");
1433 print_ddg_edge (dump_file
, e
);
1435 "\nScheduling %d (%d) in pss_not_empty,"
1436 " checking s %d (%d): ", u_node
->cuid
,
1437 INSN_UID (u_node
->insn
), v_node
->cuid
, INSN_UID
1441 if (TEST_BIT (sched_nodes
, v_node
->cuid
))
1443 int s_st
= SCHED_TIME (v_node
);
1445 late_start
= MIN (late_start
,
1446 s_st
- e
->latency
+ (e
->distance
* ii
));
1450 "succ st = %d; late_start = %d; latency = %d",
1451 s_st
, late_start
, e
->latency
);
1453 if (e
->data_type
== MEM_DEP
)
1454 end
= MAX (end
, SCHED_TIME (v_node
) - ii
+ 1);
1456 fprintf (dump_file
, "end = %d\n", end
);
1460 fprintf (dump_file
, "the node is not scheduled\n");
1464 end
= MAX (end
, late_start
- ii
);
1465 /* Schedule the node close to it's successors. */
1470 "\nScheduling %d (%d) in a window (%d..%d) with step %d\n",
1471 u_node
->cuid
, INSN_UID (u_node
->insn
), start
, end
, step
);
1475 else if (psp_not_empty
&& pss_not_empty
)
1477 int early_start
= INT_MIN
;
1478 int late_start
= INT_MAX
;
1479 int count_preds
= 0;
1480 int count_succs
= 0;
1484 for (e
= u_node
->in
; e
!= 0; e
= e
->next_in
)
1486 ddg_node_ptr v_node
= e
->src
;
1490 fprintf (dump_file
, "\nProcessing edge:");
1491 print_ddg_edge (dump_file
, e
);
1493 "\nScheduling %d (%d) in psp_pss_not_empty,"
1494 " checking p %d (%d): ", u_node
->cuid
, INSN_UID
1495 (u_node
->insn
), v_node
->cuid
, INSN_UID
1499 if (TEST_BIT (sched_nodes
, v_node
->cuid
))
1501 int p_st
= SCHED_TIME (v_node
);
1503 early_start
= MAX (early_start
,
1505 - (e
->distance
* ii
));
1509 "pred st = %d; early_start = %d; latency = %d",
1510 p_st
, early_start
, e
->latency
);
1512 if (e
->type
== TRUE_DEP
&& e
->data_type
== REG_DEP
)
1515 if (e
->data_type
== MEM_DEP
)
1516 end
= MIN (end
, SCHED_TIME (v_node
) + ii
- 1);
1519 fprintf (dump_file
, "the node is not scheduled\n");
1522 for (e
= u_node
->out
; e
!= 0; e
= e
->next_out
)
1524 ddg_node_ptr v_node
= e
->dest
;
1528 fprintf (dump_file
, "\nProcessing edge:");
1529 print_ddg_edge (dump_file
, e
);
1531 "\nScheduling %d (%d) in psp_pss_not_empty,"
1532 " checking s %d (%d): ", u_node
->cuid
, INSN_UID
1533 (u_node
->insn
), v_node
->cuid
, INSN_UID
1537 if (TEST_BIT (sched_nodes
, v_node
->cuid
))
1539 int s_st
= SCHED_TIME (v_node
);
1541 late_start
= MIN (late_start
,
1543 + (e
->distance
* ii
));
1547 "succ st = %d; late_start = %d; latency = %d",
1548 s_st
, late_start
, e
->latency
);
1550 if (e
->type
== TRUE_DEP
&& e
->data_type
== REG_DEP
)
1553 if (e
->data_type
== MEM_DEP
)
1554 start
= MAX (start
, SCHED_TIME (v_node
) - ii
+ 1);
1557 fprintf (dump_file
, "the node is not scheduled\n");
1560 start
= MAX (start
, early_start
);
1561 end
= MIN (end
, MIN (early_start
+ ii
, late_start
+ 1));
1563 /* If there are more successors than predecessors schedule the
1564 node close to it's successors. */
1565 if (count_succs
>= count_preds
)
1567 int old_start
= start
;
1570 end
= old_start
- 1;
1574 else /* psp is empty && pss is empty. */
1576 start
= SCHED_ASAP (u_node
);
1587 if ((start
>= end
&& step
== 1) || (start
<= end
&& step
== -1))
1590 fprintf (dump_file
, "\nEmpty window: start=%d, end=%d, step=%d\n",
1598 /* Calculate MUST_PRECEDE/MUST_FOLLOW bitmaps of U_NODE; which is the
1599 node currently been scheduled. At the end of the calculation
1600 MUST_PRECEDE/MUST_FOLLOW contains all predecessors/successors of
1601 U_NODE which are (1) already scheduled in the first/last row of
1602 U_NODE's scheduling window, (2) whose dependence inequality with U
1603 becomes an equality when U is scheduled in this same row, and (3)
1604 whose dependence latency is zero.
1606 The first and last rows are calculated using the following parameters:
1607 START/END rows - The cycles that begins/ends the traversal on the window;
1608 searching for an empty cycle to schedule U_NODE.
1609 STEP - The direction in which we traverse the window.
1610 II - The initiation interval. */
1613 calculate_must_precede_follow (ddg_node_ptr u_node
, int start
, int end
,
1614 int step
, int ii
, sbitmap sched_nodes
,
1615 sbitmap must_precede
, sbitmap must_follow
)
1618 int first_cycle_in_window
, last_cycle_in_window
;
1620 gcc_assert (must_precede
&& must_follow
);
1622 /* Consider the following scheduling window:
1623 {first_cycle_in_window, first_cycle_in_window+1, ...,
1624 last_cycle_in_window}. If step is 1 then the following will be
1625 the order we traverse the window: {start=first_cycle_in_window,
1626 first_cycle_in_window+1, ..., end=last_cycle_in_window+1},
1627 or {start=last_cycle_in_window, last_cycle_in_window-1, ...,
1628 end=first_cycle_in_window-1} if step is -1. */
1629 first_cycle_in_window
= (step
== 1) ? start
: end
- step
;
1630 last_cycle_in_window
= (step
== 1) ? end
- step
: start
;
1632 sbitmap_zero (must_precede
);
1633 sbitmap_zero (must_follow
);
1636 fprintf (dump_file
, "\nmust_precede: ");
1638 /* Instead of checking if:
1639 (SMODULO (SCHED_TIME (e->src), ii) == first_row_in_window)
1640 && ((SCHED_TIME (e->src) + e->latency - (e->distance * ii)) ==
1641 first_cycle_in_window)
1643 we use the fact that latency is non-negative:
1644 SCHED_TIME (e->src) - (e->distance * ii) <=
1645 SCHED_TIME (e->src) + e->latency - (e->distance * ii)) <=
1646 first_cycle_in_window
1648 SCHED_TIME (e->src) - (e->distance * ii) == first_cycle_in_window */
1649 for (e
= u_node
->in
; e
!= 0; e
= e
->next_in
)
1650 if (TEST_BIT (sched_nodes
, e
->src
->cuid
)
1651 && ((SCHED_TIME (e
->src
) - (e
->distance
* ii
)) ==
1652 first_cycle_in_window
))
1655 fprintf (dump_file
, "%d ", e
->src
->cuid
);
1657 SET_BIT (must_precede
, e
->src
->cuid
);
1661 fprintf (dump_file
, "\nmust_follow: ");
1663 /* Instead of checking if:
1664 (SMODULO (SCHED_TIME (e->dest), ii) == last_row_in_window)
1665 && ((SCHED_TIME (e->dest) - e->latency + (e->distance * ii)) ==
1666 last_cycle_in_window)
1668 we use the fact that latency is non-negative:
1669 SCHED_TIME (e->dest) + (e->distance * ii) >=
1670 SCHED_TIME (e->dest) - e->latency + (e->distance * ii)) >=
1671 last_cycle_in_window
1673 SCHED_TIME (e->dest) + (e->distance * ii) == last_cycle_in_window */
1674 for (e
= u_node
->out
; e
!= 0; e
= e
->next_out
)
1675 if (TEST_BIT (sched_nodes
, e
->dest
->cuid
)
1676 && ((SCHED_TIME (e
->dest
) + (e
->distance
* ii
)) ==
1677 last_cycle_in_window
))
1680 fprintf (dump_file
, "%d ", e
->dest
->cuid
);
1682 SET_BIT (must_follow
, e
->dest
->cuid
);
1686 fprintf (dump_file
, "\n");
1689 /* Return 1 if U_NODE can be scheduled in CYCLE. Use the following
1690 parameters to decide if that's possible:
1691 PS - The partial schedule.
1692 U - The serial number of U_NODE.
1693 NUM_SPLITS - The number of row splits made so far.
1694 MUST_PRECEDE - The nodes that must precede U_NODE. (only valid at
1695 the first row of the scheduling window)
1696 MUST_FOLLOW - The nodes that must follow U_NODE. (only valid at the
1697 last row of the scheduling window) */
1700 try_scheduling_node_in_cycle (partial_schedule_ptr ps
, ddg_node_ptr u_node
,
1701 int u
, int cycle
, sbitmap sched_nodes
,
1702 int *num_splits
, sbitmap must_precede
,
1703 sbitmap must_follow
)
1708 verify_partial_schedule (ps
, sched_nodes
);
1709 psi
= ps_add_node_check_conflicts (ps
, u_node
, cycle
,
1710 must_precede
, must_follow
);
1713 SCHED_TIME (u_node
) = cycle
;
1714 SET_BIT (sched_nodes
, u
);
1718 fprintf (dump_file
, "Scheduled w/o split in %d\n", cycle
);
1725 /* This function implements the scheduling algorithm for SMS according to the
1727 static partial_schedule_ptr
1728 sms_schedule_by_order (ddg_ptr g
, int mii
, int maxii
, int *nodes_order
)
1731 int i
, c
, success
, num_splits
= 0;
1732 int flush_and_start_over
= true;
1733 int num_nodes
= g
->num_nodes
;
1734 int start
, end
, step
; /* Place together into one struct? */
1735 sbitmap sched_nodes
= sbitmap_alloc (num_nodes
);
1736 sbitmap must_precede
= sbitmap_alloc (num_nodes
);
1737 sbitmap must_follow
= sbitmap_alloc (num_nodes
);
1738 sbitmap tobe_scheduled
= sbitmap_alloc (num_nodes
);
1740 partial_schedule_ptr ps
= create_partial_schedule (ii
, g
, DFA_HISTORY
);
1742 sbitmap_ones (tobe_scheduled
);
1743 sbitmap_zero (sched_nodes
);
1745 while (flush_and_start_over
&& (ii
< maxii
))
1749 fprintf (dump_file
, "Starting with ii=%d\n", ii
);
1750 flush_and_start_over
= false;
1751 sbitmap_zero (sched_nodes
);
1753 for (i
= 0; i
< num_nodes
; i
++)
1755 int u
= nodes_order
[i
];
1756 ddg_node_ptr u_node
= &ps
->g
->nodes
[u
];
1757 rtx insn
= u_node
->insn
;
1759 if (!NONDEBUG_INSN_P (insn
))
1761 RESET_BIT (tobe_scheduled
, u
);
1765 if (JUMP_P (insn
)) /* Closing branch handled later. */
1767 RESET_BIT (tobe_scheduled
, u
);
1771 if (TEST_BIT (sched_nodes
, u
))
1774 /* Try to get non-empty scheduling window. */
1776 if (get_sched_window (ps
, nodes_order
, i
, sched_nodes
, ii
, &start
,
1780 fprintf (dump_file
, "\nTrying to schedule node %d \
1781 INSN = %d in (%d .. %d) step %d\n", u
, (INSN_UID
1782 (g
->nodes
[u
].insn
)), start
, end
, step
);
1784 gcc_assert ((step
> 0 && start
< end
)
1785 || (step
< 0 && start
> end
));
1787 calculate_must_precede_follow (u_node
, start
, end
, step
, ii
,
1788 sched_nodes
, must_precede
,
1791 for (c
= start
; c
!= end
; c
+= step
)
1793 sbitmap tmp_precede
= NULL
;
1794 sbitmap tmp_follow
= NULL
;
1799 tmp_precede
= must_precede
;
1800 else /* step == -1. */
1801 tmp_follow
= must_follow
;
1803 if (c
== end
- step
)
1806 tmp_follow
= must_follow
;
1807 else /* step == -1. */
1808 tmp_precede
= must_precede
;
1812 try_scheduling_node_in_cycle (ps
, u_node
, u
, c
,
1814 &num_splits
, tmp_precede
,
1820 verify_partial_schedule (ps
, sched_nodes
);
1829 if (num_splits
>= MAX_SPLIT_NUM
)
1832 flush_and_start_over
= true;
1833 verify_partial_schedule (ps
, sched_nodes
);
1834 reset_partial_schedule (ps
, ii
);
1835 verify_partial_schedule (ps
, sched_nodes
);
1840 /* The scheduling window is exclusive of 'end'
1841 whereas compute_split_window() expects an inclusive,
1844 split_row
= compute_split_row (sched_nodes
, start
, end
- 1,
1847 split_row
= compute_split_row (sched_nodes
, end
+ 1, start
,
1850 ps_insert_empty_row (ps
, split_row
, sched_nodes
);
1851 i
--; /* Go back and retry node i. */
1854 fprintf (dump_file
, "num_splits=%d\n", num_splits
);
1857 /* ??? If (success), check register pressure estimates. */
1858 } /* Continue with next node. */
1859 } /* While flush_and_start_over. */
1862 free_partial_schedule (ps
);
1866 gcc_assert (sbitmap_equal (tobe_scheduled
, sched_nodes
));
1868 sbitmap_free (sched_nodes
);
1869 sbitmap_free (must_precede
);
1870 sbitmap_free (must_follow
);
1871 sbitmap_free (tobe_scheduled
);
1876 /* This function inserts a new empty row into PS at the position
1877 according to SPLITROW, keeping all already scheduled instructions
1878 intact and updating their SCHED_TIME and cycle accordingly. */
1880 ps_insert_empty_row (partial_schedule_ptr ps
, int split_row
,
1881 sbitmap sched_nodes
)
1883 ps_insn_ptr crr_insn
;
1884 ps_insn_ptr
*rows_new
;
1886 int new_ii
= ii
+ 1;
1889 verify_partial_schedule (ps
, sched_nodes
);
1891 /* We normalize sched_time and rotate ps to have only non-negative sched
1892 times, for simplicity of updating cycles after inserting new row. */
1893 split_row
-= ps
->min_cycle
;
1894 split_row
= SMODULO (split_row
, ii
);
1896 fprintf (dump_file
, "split_row=%d\n", split_row
);
1898 normalize_sched_times (ps
);
1899 rotate_partial_schedule (ps
, ps
->min_cycle
);
1901 rows_new
= (ps_insn_ptr
*) xcalloc (new_ii
, sizeof (ps_insn_ptr
));
1902 for (row
= 0; row
< split_row
; row
++)
1904 rows_new
[row
] = ps
->rows
[row
];
1905 ps
->rows
[row
] = NULL
;
1906 for (crr_insn
= rows_new
[row
];
1907 crr_insn
; crr_insn
= crr_insn
->next_in_row
)
1909 ddg_node_ptr u
= crr_insn
->node
;
1910 int new_time
= SCHED_TIME (u
) + (SCHED_TIME (u
) / ii
);
1912 SCHED_TIME (u
) = new_time
;
1913 crr_insn
->cycle
= new_time
;
1914 SCHED_ROW (u
) = new_time
% new_ii
;
1915 SCHED_STAGE (u
) = new_time
/ new_ii
;
1920 rows_new
[split_row
] = NULL
;
1922 for (row
= split_row
; row
< ii
; row
++)
1924 rows_new
[row
+ 1] = ps
->rows
[row
];
1925 ps
->rows
[row
] = NULL
;
1926 for (crr_insn
= rows_new
[row
+ 1];
1927 crr_insn
; crr_insn
= crr_insn
->next_in_row
)
1929 ddg_node_ptr u
= crr_insn
->node
;
1930 int new_time
= SCHED_TIME (u
) + (SCHED_TIME (u
) / ii
) + 1;
1932 SCHED_TIME (u
) = new_time
;
1933 crr_insn
->cycle
= new_time
;
1934 SCHED_ROW (u
) = new_time
% new_ii
;
1935 SCHED_STAGE (u
) = new_time
/ new_ii
;
1940 ps
->min_cycle
= ps
->min_cycle
+ ps
->min_cycle
/ ii
1941 + (SMODULO (ps
->min_cycle
, ii
) >= split_row
? 1 : 0);
1942 ps
->max_cycle
= ps
->max_cycle
+ ps
->max_cycle
/ ii
1943 + (SMODULO (ps
->max_cycle
, ii
) >= split_row
? 1 : 0);
1945 ps
->rows
= rows_new
;
1947 gcc_assert (ps
->min_cycle
>= 0);
1949 verify_partial_schedule (ps
, sched_nodes
);
1952 fprintf (dump_file
, "min_cycle=%d, max_cycle=%d\n", ps
->min_cycle
,
1956 /* Given U_NODE which is the node that failed to be scheduled; LOW and
1957 UP which are the boundaries of it's scheduling window; compute using
1958 SCHED_NODES and II a row in the partial schedule that can be split
1959 which will separate a critical predecessor from a critical successor
1960 thereby expanding the window, and return it. */
1962 compute_split_row (sbitmap sched_nodes
, int low
, int up
, int ii
,
1963 ddg_node_ptr u_node
)
1966 int lower
= INT_MIN
, upper
= INT_MAX
;
1967 ddg_node_ptr crit_pred
= NULL
;
1968 ddg_node_ptr crit_succ
= NULL
;
1971 for (e
= u_node
->in
; e
!= 0; e
= e
->next_in
)
1973 ddg_node_ptr v_node
= e
->src
;
1975 if (TEST_BIT (sched_nodes
, v_node
->cuid
)
1976 && (low
== SCHED_TIME (v_node
) + e
->latency
- (e
->distance
* ii
)))
1977 if (SCHED_TIME (v_node
) > lower
)
1980 lower
= SCHED_TIME (v_node
);
1984 if (crit_pred
!= NULL
)
1986 crit_cycle
= SCHED_TIME (crit_pred
) + 1;
1987 return SMODULO (crit_cycle
, ii
);
1990 for (e
= u_node
->out
; e
!= 0; e
= e
->next_out
)
1992 ddg_node_ptr v_node
= e
->dest
;
1993 if (TEST_BIT (sched_nodes
, v_node
->cuid
)
1994 && (up
== SCHED_TIME (v_node
) - e
->latency
+ (e
->distance
* ii
)))
1995 if (SCHED_TIME (v_node
) < upper
)
1998 upper
= SCHED_TIME (v_node
);
2002 if (crit_succ
!= NULL
)
2004 crit_cycle
= SCHED_TIME (crit_succ
);
2005 return SMODULO (crit_cycle
, ii
);
2009 fprintf (dump_file
, "Both crit_pred and crit_succ are NULL\n");
2011 return SMODULO ((low
+ up
+ 1) / 2, ii
);
2015 verify_partial_schedule (partial_schedule_ptr ps
, sbitmap sched_nodes
)
2018 ps_insn_ptr crr_insn
;
2020 for (row
= 0; row
< ps
->ii
; row
++)
2021 for (crr_insn
= ps
->rows
[row
]; crr_insn
; crr_insn
= crr_insn
->next_in_row
)
2023 ddg_node_ptr u
= crr_insn
->node
;
2025 gcc_assert (TEST_BIT (sched_nodes
, u
->cuid
));
2026 /* ??? Test also that all nodes of sched_nodes are in ps, perhaps by
2027 popcount (sched_nodes) == number of insns in ps. */
2028 gcc_assert (SCHED_TIME (u
) >= ps
->min_cycle
);
2029 gcc_assert (SCHED_TIME (u
) <= ps
->max_cycle
);
2034 /* This page implements the algorithm for ordering the nodes of a DDG
2035 for modulo scheduling, activated through the
2036 "int sms_order_nodes (ddg_ptr, int mii, int * result)" API. */
2038 #define ORDER_PARAMS(x) ((struct node_order_params *) (x)->aux.info)
2039 #define ASAP(x) (ORDER_PARAMS ((x))->asap)
2040 #define ALAP(x) (ORDER_PARAMS ((x))->alap)
2041 #define HEIGHT(x) (ORDER_PARAMS ((x))->height)
2042 #define MOB(x) (ALAP ((x)) - ASAP ((x)))
2043 #define DEPTH(x) (ASAP ((x)))
2045 typedef struct node_order_params
* nopa
;
2047 static void order_nodes_of_sccs (ddg_all_sccs_ptr
, int * result
);
2048 static int order_nodes_in_scc (ddg_ptr
, sbitmap
, sbitmap
, int*, int);
2049 static nopa
calculate_order_params (ddg_ptr
, int, int *);
2050 static int find_max_asap (ddg_ptr
, sbitmap
);
2051 static int find_max_hv_min_mob (ddg_ptr
, sbitmap
);
2052 static int find_max_dv_min_mob (ddg_ptr
, sbitmap
);
2054 enum sms_direction
{BOTTOMUP
, TOPDOWN
};
2056 struct node_order_params
2063 /* Check if NODE_ORDER contains a permutation of 0 .. NUM_NODES-1. */
2065 check_nodes_order (int *node_order
, int num_nodes
)
2068 sbitmap tmp
= sbitmap_alloc (num_nodes
);
2073 fprintf (dump_file
, "SMS final nodes order: \n");
2075 for (i
= 0; i
< num_nodes
; i
++)
2077 int u
= node_order
[i
];
2080 fprintf (dump_file
, "%d ", u
);
2081 gcc_assert (u
< num_nodes
&& u
>= 0 && !TEST_BIT (tmp
, u
));
2087 fprintf (dump_file
, "\n");
2092 /* Order the nodes of G for scheduling and pass the result in
2093 NODE_ORDER. Also set aux.count of each node to ASAP.
2094 Put maximal ASAP to PMAX_ASAP. Return the recMII for the given DDG. */
2096 sms_order_nodes (ddg_ptr g
, int mii
, int * node_order
, int *pmax_asap
)
2100 ddg_all_sccs_ptr sccs
= create_ddg_all_sccs (g
);
2102 nopa nops
= calculate_order_params (g
, mii
, pmax_asap
);
2105 print_sccs (dump_file
, sccs
, g
);
2107 order_nodes_of_sccs (sccs
, node_order
);
2109 if (sccs
->num_sccs
> 0)
2110 /* First SCC has the largest recurrence_length. */
2111 rec_mii
= sccs
->sccs
[0]->recurrence_length
;
2113 /* Save ASAP before destroying node_order_params. */
2114 for (i
= 0; i
< g
->num_nodes
; i
++)
2116 ddg_node_ptr v
= &g
->nodes
[i
];
2117 v
->aux
.count
= ASAP (v
);
2121 free_ddg_all_sccs (sccs
);
2122 check_nodes_order (node_order
, g
->num_nodes
);
2128 order_nodes_of_sccs (ddg_all_sccs_ptr all_sccs
, int * node_order
)
2131 ddg_ptr g
= all_sccs
->ddg
;
2132 int num_nodes
= g
->num_nodes
;
2133 sbitmap prev_sccs
= sbitmap_alloc (num_nodes
);
2134 sbitmap on_path
= sbitmap_alloc (num_nodes
);
2135 sbitmap tmp
= sbitmap_alloc (num_nodes
);
2136 sbitmap ones
= sbitmap_alloc (num_nodes
);
2138 sbitmap_zero (prev_sccs
);
2139 sbitmap_ones (ones
);
2141 /* Perform the node ordering starting from the SCC with the highest recMII.
2142 For each SCC order the nodes according to their ASAP/ALAP/HEIGHT etc. */
2143 for (i
= 0; i
< all_sccs
->num_sccs
; i
++)
2145 ddg_scc_ptr scc
= all_sccs
->sccs
[i
];
2147 /* Add nodes on paths from previous SCCs to the current SCC. */
2148 find_nodes_on_paths (on_path
, g
, prev_sccs
, scc
->nodes
);
2149 sbitmap_a_or_b (tmp
, scc
->nodes
, on_path
);
2151 /* Add nodes on paths from the current SCC to previous SCCs. */
2152 find_nodes_on_paths (on_path
, g
, scc
->nodes
, prev_sccs
);
2153 sbitmap_a_or_b (tmp
, tmp
, on_path
);
2155 /* Remove nodes of previous SCCs from current extended SCC. */
2156 sbitmap_difference (tmp
, tmp
, prev_sccs
);
2158 pos
= order_nodes_in_scc (g
, prev_sccs
, tmp
, node_order
, pos
);
2159 /* Above call to order_nodes_in_scc updated prev_sccs |= tmp. */
2162 /* Handle the remaining nodes that do not belong to any scc. Each call
2163 to order_nodes_in_scc handles a single connected component. */
2164 while (pos
< g
->num_nodes
)
2166 sbitmap_difference (tmp
, ones
, prev_sccs
);
2167 pos
= order_nodes_in_scc (g
, prev_sccs
, tmp
, node_order
, pos
);
2169 sbitmap_free (prev_sccs
);
2170 sbitmap_free (on_path
);
2172 sbitmap_free (ones
);
2175 /* MII is needed if we consider backarcs (that do not close recursive cycles). */
2176 static struct node_order_params
*
2177 calculate_order_params (ddg_ptr g
, int mii ATTRIBUTE_UNUSED
, int *pmax_asap
)
2181 int num_nodes
= g
->num_nodes
;
2183 /* Allocate a place to hold ordering params for each node in the DDG. */
2184 nopa node_order_params_arr
;
2186 /* Initialize of ASAP/ALAP/HEIGHT to zero. */
2187 node_order_params_arr
= (nopa
) xcalloc (num_nodes
,
2188 sizeof (struct node_order_params
));
2190 /* Set the aux pointer of each node to point to its order_params structure. */
2191 for (u
= 0; u
< num_nodes
; u
++)
2192 g
->nodes
[u
].aux
.info
= &node_order_params_arr
[u
];
2194 /* Disregarding a backarc from each recursive cycle to obtain a DAG,
2195 calculate ASAP, ALAP, mobility, distance, and height for each node
2196 in the dependence (direct acyclic) graph. */
2198 /* We assume that the nodes in the array are in topological order. */
2201 for (u
= 0; u
< num_nodes
; u
++)
2203 ddg_node_ptr u_node
= &g
->nodes
[u
];
2206 for (e
= u_node
->in
; e
; e
= e
->next_in
)
2207 if (e
->distance
== 0)
2208 ASAP (u_node
) = MAX (ASAP (u_node
),
2209 ASAP (e
->src
) + e
->latency
);
2210 max_asap
= MAX (max_asap
, ASAP (u_node
));
2213 for (u
= num_nodes
- 1; u
> -1; u
--)
2215 ddg_node_ptr u_node
= &g
->nodes
[u
];
2217 ALAP (u_node
) = max_asap
;
2218 HEIGHT (u_node
) = 0;
2219 for (e
= u_node
->out
; e
; e
= e
->next_out
)
2220 if (e
->distance
== 0)
2222 ALAP (u_node
) = MIN (ALAP (u_node
),
2223 ALAP (e
->dest
) - e
->latency
);
2224 HEIGHT (u_node
) = MAX (HEIGHT (u_node
),
2225 HEIGHT (e
->dest
) + e
->latency
);
2230 fprintf (dump_file
, "\nOrder params\n");
2231 for (u
= 0; u
< num_nodes
; u
++)
2233 ddg_node_ptr u_node
= &g
->nodes
[u
];
2235 fprintf (dump_file
, "node %d, ASAP: %d, ALAP: %d, HEIGHT: %d\n", u
,
2236 ASAP (u_node
), ALAP (u_node
), HEIGHT (u_node
));
2240 *pmax_asap
= max_asap
;
2241 return node_order_params_arr
;
2245 find_max_asap (ddg_ptr g
, sbitmap nodes
)
2250 sbitmap_iterator sbi
;
2252 EXECUTE_IF_SET_IN_SBITMAP (nodes
, 0, u
, sbi
)
2254 ddg_node_ptr u_node
= &g
->nodes
[u
];
2256 if (max_asap
< ASAP (u_node
))
2258 max_asap
= ASAP (u_node
);
2266 find_max_hv_min_mob (ddg_ptr g
, sbitmap nodes
)
2270 int min_mob
= INT_MAX
;
2272 sbitmap_iterator sbi
;
2274 EXECUTE_IF_SET_IN_SBITMAP (nodes
, 0, u
, sbi
)
2276 ddg_node_ptr u_node
= &g
->nodes
[u
];
2278 if (max_hv
< HEIGHT (u_node
))
2280 max_hv
= HEIGHT (u_node
);
2281 min_mob
= MOB (u_node
);
2284 else if ((max_hv
== HEIGHT (u_node
))
2285 && (min_mob
> MOB (u_node
)))
2287 min_mob
= MOB (u_node
);
2295 find_max_dv_min_mob (ddg_ptr g
, sbitmap nodes
)
2299 int min_mob
= INT_MAX
;
2301 sbitmap_iterator sbi
;
2303 EXECUTE_IF_SET_IN_SBITMAP (nodes
, 0, u
, sbi
)
2305 ddg_node_ptr u_node
= &g
->nodes
[u
];
2307 if (max_dv
< DEPTH (u_node
))
2309 max_dv
= DEPTH (u_node
);
2310 min_mob
= MOB (u_node
);
2313 else if ((max_dv
== DEPTH (u_node
))
2314 && (min_mob
> MOB (u_node
)))
2316 min_mob
= MOB (u_node
);
2323 /* Places the nodes of SCC into the NODE_ORDER array starting
2324 at position POS, according to the SMS ordering algorithm.
2325 NODES_ORDERED (in&out parameter) holds the bitset of all nodes in
2326 the NODE_ORDER array, starting from position zero. */
2328 order_nodes_in_scc (ddg_ptr g
, sbitmap nodes_ordered
, sbitmap scc
,
2329 int * node_order
, int pos
)
2331 enum sms_direction dir
;
2332 int num_nodes
= g
->num_nodes
;
2333 sbitmap workset
= sbitmap_alloc (num_nodes
);
2334 sbitmap tmp
= sbitmap_alloc (num_nodes
);
2335 sbitmap zero_bitmap
= sbitmap_alloc (num_nodes
);
2336 sbitmap predecessors
= sbitmap_alloc (num_nodes
);
2337 sbitmap successors
= sbitmap_alloc (num_nodes
);
2339 sbitmap_zero (predecessors
);
2340 find_predecessors (predecessors
, g
, nodes_ordered
);
2342 sbitmap_zero (successors
);
2343 find_successors (successors
, g
, nodes_ordered
);
2346 if (sbitmap_a_and_b_cg (tmp
, predecessors
, scc
))
2348 sbitmap_copy (workset
, tmp
);
2351 else if (sbitmap_a_and_b_cg (tmp
, successors
, scc
))
2353 sbitmap_copy (workset
, tmp
);
2360 sbitmap_zero (workset
);
2361 if ((u
= find_max_asap (g
, scc
)) >= 0)
2362 SET_BIT (workset
, u
);
2366 sbitmap_zero (zero_bitmap
);
2367 while (!sbitmap_equal (workset
, zero_bitmap
))
2370 ddg_node_ptr v_node
;
2371 sbitmap v_node_preds
;
2372 sbitmap v_node_succs
;
2376 while (!sbitmap_equal (workset
, zero_bitmap
))
2378 v
= find_max_hv_min_mob (g
, workset
);
2379 v_node
= &g
->nodes
[v
];
2380 node_order
[pos
++] = v
;
2381 v_node_succs
= NODE_SUCCESSORS (v_node
);
2382 sbitmap_a_and_b (tmp
, v_node_succs
, scc
);
2384 /* Don't consider the already ordered successors again. */
2385 sbitmap_difference (tmp
, tmp
, nodes_ordered
);
2386 sbitmap_a_or_b (workset
, workset
, tmp
);
2387 RESET_BIT (workset
, v
);
2388 SET_BIT (nodes_ordered
, v
);
2391 sbitmap_zero (predecessors
);
2392 find_predecessors (predecessors
, g
, nodes_ordered
);
2393 sbitmap_a_and_b (workset
, predecessors
, scc
);
2397 while (!sbitmap_equal (workset
, zero_bitmap
))
2399 v
= find_max_dv_min_mob (g
, workset
);
2400 v_node
= &g
->nodes
[v
];
2401 node_order
[pos
++] = v
;
2402 v_node_preds
= NODE_PREDECESSORS (v_node
);
2403 sbitmap_a_and_b (tmp
, v_node_preds
, scc
);
2405 /* Don't consider the already ordered predecessors again. */
2406 sbitmap_difference (tmp
, tmp
, nodes_ordered
);
2407 sbitmap_a_or_b (workset
, workset
, tmp
);
2408 RESET_BIT (workset
, v
);
2409 SET_BIT (nodes_ordered
, v
);
2412 sbitmap_zero (successors
);
2413 find_successors (successors
, g
, nodes_ordered
);
2414 sbitmap_a_and_b (workset
, successors
, scc
);
2418 sbitmap_free (workset
);
2419 sbitmap_free (zero_bitmap
);
2420 sbitmap_free (predecessors
);
2421 sbitmap_free (successors
);
2426 /* This page contains functions for manipulating partial-schedules during
2427 modulo scheduling. */
2429 /* Create a partial schedule and allocate a memory to hold II rows. */
2431 static partial_schedule_ptr
2432 create_partial_schedule (int ii
, ddg_ptr g
, int history
)
2434 partial_schedule_ptr ps
= XNEW (struct partial_schedule
);
2435 ps
->rows
= (ps_insn_ptr
*) xcalloc (ii
, sizeof (ps_insn_ptr
));
2437 ps
->history
= history
;
2438 ps
->min_cycle
= INT_MAX
;
2439 ps
->max_cycle
= INT_MIN
;
2445 /* Free the PS_INSNs in rows array of the given partial schedule.
2446 ??? Consider caching the PS_INSN's. */
2448 free_ps_insns (partial_schedule_ptr ps
)
2452 for (i
= 0; i
< ps
->ii
; i
++)
2456 ps_insn_ptr ps_insn
= ps
->rows
[i
]->next_in_row
;
2459 ps
->rows
[i
] = ps_insn
;
2465 /* Free all the memory allocated to the partial schedule. */
2468 free_partial_schedule (partial_schedule_ptr ps
)
2477 /* Clear the rows array with its PS_INSNs, and create a new one with
2481 reset_partial_schedule (partial_schedule_ptr ps
, int new_ii
)
2486 if (new_ii
== ps
->ii
)
2488 ps
->rows
= (ps_insn_ptr
*) xrealloc (ps
->rows
, new_ii
2489 * sizeof (ps_insn_ptr
));
2490 memset (ps
->rows
, 0, new_ii
* sizeof (ps_insn_ptr
));
2492 ps
->min_cycle
= INT_MAX
;
2493 ps
->max_cycle
= INT_MIN
;
2496 /* Prints the partial schedule as an ii rows array, for each rows
2497 print the ids of the insns in it. */
2499 print_partial_schedule (partial_schedule_ptr ps
, FILE *dump
)
2503 for (i
= 0; i
< ps
->ii
; i
++)
2505 ps_insn_ptr ps_i
= ps
->rows
[i
];
2507 fprintf (dump
, "\n[ROW %d ]: ", i
);
2510 fprintf (dump
, "%d, ",
2511 INSN_UID (ps_i
->node
->insn
));
2512 ps_i
= ps_i
->next_in_row
;
2517 /* Creates an object of PS_INSN and initializes it to the given parameters. */
2519 create_ps_insn (ddg_node_ptr node
, int rest_count
, int cycle
)
2521 ps_insn_ptr ps_i
= XNEW (struct ps_insn
);
2524 ps_i
->next_in_row
= NULL
;
2525 ps_i
->prev_in_row
= NULL
;
2526 ps_i
->row_rest_count
= rest_count
;
2527 ps_i
->cycle
= cycle
;
2533 /* Removes the given PS_INSN from the partial schedule. Returns false if the
2534 node is not found in the partial schedule, else returns true. */
2536 remove_node_from_ps (partial_schedule_ptr ps
, ps_insn_ptr ps_i
)
2543 row
= SMODULO (ps_i
->cycle
, ps
->ii
);
2544 if (! ps_i
->prev_in_row
)
2546 if (ps_i
!= ps
->rows
[row
])
2549 ps
->rows
[row
] = ps_i
->next_in_row
;
2551 ps
->rows
[row
]->prev_in_row
= NULL
;
2555 ps_i
->prev_in_row
->next_in_row
= ps_i
->next_in_row
;
2556 if (ps_i
->next_in_row
)
2557 ps_i
->next_in_row
->prev_in_row
= ps_i
->prev_in_row
;
2563 /* Unlike what literature describes for modulo scheduling (which focuses
2564 on VLIW machines) the order of the instructions inside a cycle is
2565 important. Given the bitmaps MUST_FOLLOW and MUST_PRECEDE we know
2566 where the current instruction should go relative to the already
2567 scheduled instructions in the given cycle. Go over these
2568 instructions and find the first possible column to put it in. */
2570 ps_insn_find_column (partial_schedule_ptr ps
, ps_insn_ptr ps_i
,
2571 sbitmap must_precede
, sbitmap must_follow
)
2573 ps_insn_ptr next_ps_i
;
2574 ps_insn_ptr first_must_follow
= NULL
;
2575 ps_insn_ptr last_must_precede
= NULL
;
2581 row
= SMODULO (ps_i
->cycle
, ps
->ii
);
2583 /* Find the first must follow and the last must precede
2584 and insert the node immediately after the must precede
2585 but make sure that it there is no must follow after it. */
2586 for (next_ps_i
= ps
->rows
[row
];
2588 next_ps_i
= next_ps_i
->next_in_row
)
2590 if (must_follow
&& TEST_BIT (must_follow
, next_ps_i
->node
->cuid
)
2591 && ! first_must_follow
)
2592 first_must_follow
= next_ps_i
;
2593 if (must_precede
&& TEST_BIT (must_precede
, next_ps_i
->node
->cuid
))
2595 /* If we have already met a node that must follow, then
2596 there is no possible column. */
2597 if (first_must_follow
)
2600 last_must_precede
= next_ps_i
;
2604 /* Now insert the node after INSERT_AFTER_PSI. */
2606 if (! last_must_precede
)
2608 ps_i
->next_in_row
= ps
->rows
[row
];
2609 ps_i
->prev_in_row
= NULL
;
2610 if (ps_i
->next_in_row
)
2611 ps_i
->next_in_row
->prev_in_row
= ps_i
;
2612 ps
->rows
[row
] = ps_i
;
2616 ps_i
->next_in_row
= last_must_precede
->next_in_row
;
2617 last_must_precede
->next_in_row
= ps_i
;
2618 ps_i
->prev_in_row
= last_must_precede
;
2619 if (ps_i
->next_in_row
)
2620 ps_i
->next_in_row
->prev_in_row
= ps_i
;
2626 /* Advances the PS_INSN one column in its current row; returns false
2627 in failure and true in success. Bit N is set in MUST_FOLLOW if
2628 the node with cuid N must be come after the node pointed to by
2629 PS_I when scheduled in the same cycle. */
2631 ps_insn_advance_column (partial_schedule_ptr ps
, ps_insn_ptr ps_i
,
2632 sbitmap must_follow
)
2634 ps_insn_ptr prev
, next
;
2636 ddg_node_ptr next_node
;
2641 row
= SMODULO (ps_i
->cycle
, ps
->ii
);
2643 if (! ps_i
->next_in_row
)
2646 next_node
= ps_i
->next_in_row
->node
;
2648 /* Check if next_in_row is dependent on ps_i, both having same sched
2649 times (typically ANTI_DEP). If so, ps_i cannot skip over it. */
2650 if (must_follow
&& TEST_BIT (must_follow
, next_node
->cuid
))
2653 /* Advance PS_I over its next_in_row in the doubly linked list. */
2654 prev
= ps_i
->prev_in_row
;
2655 next
= ps_i
->next_in_row
;
2657 if (ps_i
== ps
->rows
[row
])
2658 ps
->rows
[row
] = next
;
2660 ps_i
->next_in_row
= next
->next_in_row
;
2662 if (next
->next_in_row
)
2663 next
->next_in_row
->prev_in_row
= ps_i
;
2665 next
->next_in_row
= ps_i
;
2666 ps_i
->prev_in_row
= next
;
2668 next
->prev_in_row
= prev
;
2670 prev
->next_in_row
= next
;
2675 /* Inserts a DDG_NODE to the given partial schedule at the given cycle.
2676 Returns 0 if this is not possible and a PS_INSN otherwise. Bit N is
2677 set in MUST_PRECEDE/MUST_FOLLOW if the node with cuid N must be come
2678 before/after (respectively) the node pointed to by PS_I when scheduled
2679 in the same cycle. */
2681 add_node_to_ps (partial_schedule_ptr ps
, ddg_node_ptr node
, int cycle
,
2682 sbitmap must_precede
, sbitmap must_follow
)
2686 int row
= SMODULO (cycle
, ps
->ii
);
2689 && ps
->rows
[row
]->row_rest_count
>= issue_rate
)
2693 rest_count
+= ps
->rows
[row
]->row_rest_count
;
2695 ps_i
= create_ps_insn (node
, rest_count
, cycle
);
2697 /* Finds and inserts PS_I according to MUST_FOLLOW and
2699 if (! ps_insn_find_column (ps
, ps_i
, must_precede
, must_follow
))
2708 /* Advance time one cycle. Assumes DFA is being used. */
2710 advance_one_cycle (void)
2712 if (targetm
.sched
.dfa_pre_cycle_insn
)
2713 state_transition (curr_state
,
2714 targetm
.sched
.dfa_pre_cycle_insn ());
2716 state_transition (curr_state
, NULL
);
2718 if (targetm
.sched
.dfa_post_cycle_insn
)
2719 state_transition (curr_state
,
2720 targetm
.sched
.dfa_post_cycle_insn ());
2725 /* Checks if PS has resource conflicts according to DFA, starting from
2726 FROM cycle to TO cycle; returns true if there are conflicts and false
2727 if there are no conflicts. Assumes DFA is being used. */
2729 ps_has_conflicts (partial_schedule_ptr ps
, int from
, int to
)
2733 state_reset (curr_state
);
2735 for (cycle
= from
; cycle
<= to
; cycle
++)
2737 ps_insn_ptr crr_insn
;
2738 /* Holds the remaining issue slots in the current row. */
2739 int can_issue_more
= issue_rate
;
2741 /* Walk through the DFA for the current row. */
2742 for (crr_insn
= ps
->rows
[SMODULO (cycle
, ps
->ii
)];
2744 crr_insn
= crr_insn
->next_in_row
)
2746 rtx insn
= crr_insn
->node
->insn
;
2748 if (!NONDEBUG_INSN_P (insn
))
2751 /* Check if there is room for the current insn. */
2752 if (!can_issue_more
|| state_dead_lock_p (curr_state
))
2755 /* Update the DFA state and return with failure if the DFA found
2756 resource conflicts. */
2757 if (state_transition (curr_state
, insn
) >= 0)
2760 if (targetm
.sched
.variable_issue
)
2762 targetm
.sched
.variable_issue (sched_dump
, sched_verbose
,
2763 insn
, can_issue_more
);
2764 /* A naked CLOBBER or USE generates no instruction, so don't
2765 let them consume issue slots. */
2766 else if (GET_CODE (PATTERN (insn
)) != USE
2767 && GET_CODE (PATTERN (insn
)) != CLOBBER
)
2771 /* Advance the DFA to the next cycle. */
2772 advance_one_cycle ();
2777 /* Checks if the given node causes resource conflicts when added to PS at
2778 cycle C. If not the node is added to PS and returned; otherwise zero
2779 is returned. Bit N is set in MUST_PRECEDE/MUST_FOLLOW if the node with
2780 cuid N must be come before/after (respectively) the node pointed to by
2781 PS_I when scheduled in the same cycle. */
2783 ps_add_node_check_conflicts (partial_schedule_ptr ps
, ddg_node_ptr n
,
2784 int c
, sbitmap must_precede
,
2785 sbitmap must_follow
)
2787 int has_conflicts
= 0;
2790 /* First add the node to the PS, if this succeeds check for
2791 conflicts, trying different issue slots in the same row. */
2792 if (! (ps_i
= add_node_to_ps (ps
, n
, c
, must_precede
, must_follow
)))
2793 return NULL
; /* Failed to insert the node at the given cycle. */
2795 has_conflicts
= ps_has_conflicts (ps
, c
, c
)
2797 && ps_has_conflicts (ps
,
2801 /* Try different issue slots to find one that the given node can be
2802 scheduled in without conflicts. */
2803 while (has_conflicts
)
2805 if (! ps_insn_advance_column (ps
, ps_i
, must_follow
))
2807 has_conflicts
= ps_has_conflicts (ps
, c
, c
)
2809 && ps_has_conflicts (ps
,
2816 remove_node_from_ps (ps
, ps_i
);
2820 ps
->min_cycle
= MIN (ps
->min_cycle
, c
);
2821 ps
->max_cycle
= MAX (ps
->max_cycle
, c
);
2825 /* Rotate the rows of PS such that insns scheduled at time
2826 START_CYCLE will appear in row 0. Updates max/min_cycles. */
2828 rotate_partial_schedule (partial_schedule_ptr ps
, int start_cycle
)
2830 int i
, row
, backward_rotates
;
2831 int last_row
= ps
->ii
- 1;
2833 if (start_cycle
== 0)
2836 backward_rotates
= SMODULO (start_cycle
, ps
->ii
);
2838 /* Revisit later and optimize this into a single loop. */
2839 for (i
= 0; i
< backward_rotates
; i
++)
2841 ps_insn_ptr first_row
= ps
->rows
[0];
2843 for (row
= 0; row
< last_row
; row
++)
2844 ps
->rows
[row
] = ps
->rows
[row
+1];
2846 ps
->rows
[last_row
] = first_row
;
2849 ps
->max_cycle
-= start_cycle
;
2850 ps
->min_cycle
-= start_cycle
;
2853 #endif /* INSN_SCHEDULING */
2856 gate_handle_sms (void)
2858 return (optimize
> 0 && flag_modulo_sched
);
2862 /* Run instruction scheduler. */
2863 /* Perform SMS module scheduling. */
2865 rest_of_handle_sms (void)
2867 #ifdef INSN_SCHEDULING
2870 /* Collect loop information to be used in SMS. */
2871 cfg_layout_initialize (0);
2874 /* Update the life information, because we add pseudos. */
2875 max_regno
= max_reg_num ();
2877 /* Finalize layout changes. */
2879 if (bb
->next_bb
!= EXIT_BLOCK_PTR
)
2880 bb
->aux
= bb
->next_bb
;
2881 free_dominance_info (CDI_DOMINATORS
);
2882 cfg_layout_finalize ();
2883 #endif /* INSN_SCHEDULING */
2887 struct rtl_opt_pass pass_sms
=
2892 gate_handle_sms
, /* gate */
2893 rest_of_handle_sms
, /* execute */
2896 0, /* static_pass_number */
2898 0, /* properties_required */
2899 0, /* properties_provided */
2900 0, /* properties_destroyed */
2901 TODO_dump_func
, /* todo_flags_start */
2902 TODO_df_finish
| TODO_verify_rtl_sharing
|
2904 TODO_ggc_collect
/* todo_flags_finish */